| 1 | /* |
| 2 | * Copyright (C) 2013 Google Inc. All rights reserved. |
| 3 | * |
| 4 | * Redistribution and use in source and binary forms, with or without |
| 5 | * modification, are permitted provided that the following conditions are |
| 6 | * met: |
| 7 | * |
| 8 | * * Redistributions of source code must retain the above copyright |
| 9 | * notice, this list of conditions and the following disclaimer. |
| 10 | * * Redistributions in binary form must reproduce the above |
| 11 | * copyright notice, this list of conditions and the following disclaimer |
| 12 | * in the documentation and/or other materials provided with the |
| 13 | * distribution. |
| 14 | * * Neither the name of Google Inc. nor the names of its |
| 15 | * contributors may be used to endorse or promote products derived from |
| 16 | * this software without specific prior written permission. |
| 17 | * |
| 18 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
| 19 | * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
| 20 | * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR |
| 21 | * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT |
| 22 | * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
| 23 | * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT |
| 24 | * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
| 25 | * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
| 26 | * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
| 27 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
| 28 | * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 29 | */ |
| 30 | |
| 31 | #pragma once |
| 32 | |
| 33 | #if ENABLE(MEDIA_SOURCE) |
| 34 | |
| 35 | #include "ActiveDOMObject.h" |
| 36 | #include "EventTarget.h" |
| 37 | #include "ExceptionOr.h" |
| 38 | #include "GenericEventQueue.h" |
| 39 | #include "MediaSourcePrivateClient.h" |
| 40 | #include "URLRegistry.h" |
| 41 | #include <wtf/LoggerHelper.h> |
| 42 | |
| 43 | namespace WebCore { |
| 44 | |
| 45 | class ContentType; |
| 46 | class HTMLMediaElement; |
| 47 | class SourceBuffer; |
| 48 | class SourceBufferList; |
| 49 | class SourceBufferPrivate; |
| 50 | class TimeRanges; |
| 51 | |
| 52 | class MediaSource final |
| 53 | : public MediaSourcePrivateClient |
| 54 | , public ActiveDOMObject |
| 55 | , public EventTargetWithInlineData |
| 56 | , public URLRegistrable |
| 57 | #if !RELEASE_LOG_DISABLED |
| 58 | , private LoggerHelper |
| 59 | #endif |
| 60 | { |
| 61 | WTF_MAKE_ISO_ALLOCATED(MediaSource); |
| 62 | public: |
| 63 | static void setRegistry(URLRegistry*); |
| 64 | static MediaSource* lookup(const String& url) { return s_registry ? static_cast<MediaSource*>(s_registry->lookup(url)) : nullptr; } |
| 65 | |
| 66 | static Ref<MediaSource> create(ScriptExecutionContext&); |
| 67 | virtual ~MediaSource(); |
| 68 | |
| 69 | void addedToRegistry(); |
| 70 | void removedFromRegistry(); |
| 71 | void openIfInEndedState(); |
| 72 | bool isOpen() const; |
| 73 | bool isClosed() const; |
| 74 | bool isEnded() const; |
| 75 | void sourceBufferDidChangeActiveState(SourceBuffer&, bool); |
| 76 | |
| 77 | enum class EndOfStreamError { Network, Decode }; |
| 78 | void streamEndedWithError(Optional<EndOfStreamError>); |
| 79 | |
| 80 | MediaTime duration() const final; |
| 81 | void durationChanged(const MediaTime&) final; |
| 82 | std::unique_ptr<PlatformTimeRanges> buffered() const final; |
| 83 | |
| 84 | bool attachToElement(HTMLMediaElement&); |
| 85 | void detachFromElement(HTMLMediaElement&); |
| 86 | void monitorSourceBuffers() override; |
| 87 | bool isSeeking() const { return m_pendingSeekTime.isValid(); } |
| 88 | Ref<TimeRanges> seekable(); |
| 89 | ExceptionOr<void> setLiveSeekableRange(double start, double end); |
| 90 | ExceptionOr<void> clearLiveSeekableRange(); |
| 91 | |
| 92 | ExceptionOr<void> setDuration(double); |
| 93 | ExceptionOr<void> setDurationInternal(const MediaTime&); |
| 94 | MediaTime currentTime() const; |
| 95 | |
| 96 | enum class ReadyState { Closed, Open, Ended }; |
| 97 | ReadyState readyState() const { return m_readyState; } |
| 98 | ExceptionOr<void> endOfStream(Optional<EndOfStreamError>); |
| 99 | |
| 100 | HTMLMediaElement* mediaElement() const { return m_mediaElement; } |
| 101 | |
| 102 | SourceBufferList* sourceBuffers() { return m_sourceBuffers.get(); } |
| 103 | SourceBufferList* activeSourceBuffers() { return m_activeSourceBuffers.get(); } |
| 104 | ExceptionOr<Ref<SourceBuffer>> addSourceBuffer(const String& type); |
| 105 | ExceptionOr<void> removeSourceBuffer(SourceBuffer&); |
| 106 | static bool isTypeSupported(const String& type); |
| 107 | |
| 108 | ScriptExecutionContext* scriptExecutionContext() const final; |
| 109 | |
| 110 | using RefCounted::ref; |
| 111 | using RefCounted::deref; |
| 112 | |
| 113 | bool hasPendingActivity() const final; |
| 114 | |
| 115 | static const MediaTime& currentTimeFudgeFactor(); |
| 116 | static bool contentTypeShouldGenerateTimestamps(const ContentType&); |
| 117 | |
| 118 | #if !RELEASE_LOG_DISABLED |
| 119 | const Logger& logger() const final { return m_logger.get(); } |
| 120 | const void* logIdentifier() const final { return m_logIdentifier; } |
| 121 | const char* logClassName() const final { return "MediaSource" ; } |
| 122 | WTFLogChannel& logChannel() const final; |
| 123 | void setLogIdentifier(const void*) final; |
| 124 | #endif |
| 125 | |
| 126 | private: |
| 127 | explicit MediaSource(ScriptExecutionContext&); |
| 128 | |
| 129 | void suspend(ReasonForSuspension) final; |
| 130 | void resume() final; |
| 131 | void stop() final; |
| 132 | bool canSuspendForDocumentSuspension() const final; |
| 133 | const char* activeDOMObjectName() const final; |
| 134 | |
| 135 | void setPrivateAndOpen(Ref<MediaSourcePrivate>&&) final; |
| 136 | void seekToTime(const MediaTime&) final; |
| 137 | |
| 138 | void refEventTarget() final { ref(); } |
| 139 | void derefEventTarget() final { deref(); } |
| 140 | EventTargetInterface eventTargetInterface() const final; |
| 141 | |
| 142 | URLRegistry& registry() const final; |
| 143 | |
| 144 | void setReadyState(ReadyState); |
| 145 | void onReadyStateChange(ReadyState oldState, ReadyState newState); |
| 146 | |
| 147 | Vector<PlatformTimeRanges> activeRanges() const; |
| 148 | |
| 149 | ExceptionOr<Ref<SourceBufferPrivate>> createSourceBufferPrivate(const ContentType&); |
| 150 | void scheduleEvent(const AtomicString& eventName); |
| 151 | |
| 152 | bool hasBufferedTime(const MediaTime&); |
| 153 | bool hasCurrentTime(); |
| 154 | bool hasFutureTime(); |
| 155 | |
| 156 | void regenerateActiveSourceBuffers(); |
| 157 | |
| 158 | void completeSeek(); |
| 159 | |
| 160 | static URLRegistry* s_registry; |
| 161 | |
| 162 | RefPtr<MediaSourcePrivate> m_private; |
| 163 | RefPtr<SourceBufferList> m_sourceBuffers; |
| 164 | RefPtr<SourceBufferList> m_activeSourceBuffers; |
| 165 | mutable std::unique_ptr<PlatformTimeRanges> m_buffered; |
| 166 | std::unique_ptr<PlatformTimeRanges> m_liveSeekable; |
| 167 | HTMLMediaElement* m_mediaElement { nullptr }; |
| 168 | MediaTime m_duration; |
| 169 | MediaTime m_pendingSeekTime; |
| 170 | ReadyState m_readyState { ReadyState::Closed }; |
| 171 | GenericEventQueue m_asyncEventQueue; |
| 172 | #if !RELEASE_LOG_DISABLED |
| 173 | Ref<const Logger> m_logger; |
| 174 | const void* m_logIdentifier { nullptr }; |
| 175 | #endif |
| 176 | }; |
| 177 | |
| 178 | String convertEnumerationToString(MediaSource::EndOfStreamError); |
| 179 | String convertEnumerationToString(MediaSource::ReadyState); |
| 180 | |
| 181 | } // namespace WebCore |
| 182 | |
| 183 | namespace WTF { |
| 184 | |
| 185 | template<typename Type> |
| 186 | struct LogArgument; |
| 187 | |
| 188 | template <> |
| 189 | struct LogArgument<WebCore::MediaSource::EndOfStreamError> { |
| 190 | static String toString(const WebCore::MediaSource::EndOfStreamError error) |
| 191 | { |
| 192 | return convertEnumerationToString(error); |
| 193 | } |
| 194 | }; |
| 195 | |
| 196 | template <> |
| 197 | struct LogArgument<WebCore::MediaSource::ReadyState> { |
| 198 | static String toString(const WebCore::MediaSource::ReadyState state) |
| 199 | { |
| 200 | return convertEnumerationToString(state); |
| 201 | } |
| 202 | }; |
| 203 | |
| 204 | } // namespace WTF |
| 205 | |
| 206 | #endif |
| 207 | |