2 * Copyright (C) 2013 Google Inc. All rights reserved.
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions are
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
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.
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.
33 #if ENABLE(MEDIA_SOURCE)
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>
46 class HTMLMediaElement;
48 class SourceBufferList;
49 class SourceBufferPrivate;
52 class MediaSource final
53 : public MediaSourcePrivateClient
54 , public ActiveDOMObject
55 , public EventTargetWithInlineData
56 , public URLRegistrable
57 #if !RELEASE_LOG_DISABLED
58 , private LoggerHelper
61 WTF_MAKE_ISO_ALLOCATED(MediaSource);
63 static void setRegistry(URLRegistry*);
64 static MediaSource* lookup(const String& url) { return s_registry ? static_cast<MediaSource*>(s_registry->lookup(url)) : nullptr; }
66 static Ref<MediaSource> create(ScriptExecutionContext&);
67 virtual ~MediaSource();
69 void addedToRegistry();
70 void removedFromRegistry();
71 void openIfInEndedState();
73 bool isClosed() const;
75 void sourceBufferDidChangeActiveState(SourceBuffer&, bool);
77 enum class EndOfStreamError { Network, Decode };
78 void streamEndedWithError(Optional<EndOfStreamError>);
80 MediaTime duration() const final;
81 void durationChanged(const MediaTime&) final;
82 std::unique_ptr<PlatformTimeRanges> buffered() const final;
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();
92 ExceptionOr<void> setDuration(double);
93 ExceptionOr<void> setDurationInternal(const MediaTime&);
94 MediaTime currentTime() const;
96 enum class ReadyState { Closed, Open, Ended };
97 ReadyState readyState() const { return m_readyState; }
98 ExceptionOr<void> endOfStream(Optional<EndOfStreamError>);
100 HTMLMediaElement* mediaElement() const { return m_mediaElement; }
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);
108 ScriptExecutionContext* scriptExecutionContext() const final;
110 using RefCounted::ref;
111 using RefCounted::deref;
113 bool hasPendingActivity() const final;
115 static const MediaTime& currentTimeFudgeFactor();
116 static bool contentTypeShouldGenerateTimestamps(const ContentType&);
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;
127 explicit MediaSource(ScriptExecutionContext&);
130 bool canSuspendForDocumentSuspension() const final;
131 const char* activeDOMObjectName() const final;
133 void setPrivateAndOpen(Ref<MediaSourcePrivate>&&) final;
134 void seekToTime(const MediaTime&) final;
136 void refEventTarget() final { ref(); }
137 void derefEventTarget() final { deref(); }
138 EventTargetInterface eventTargetInterface() const final;
140 URLRegistry& registry() const final;
142 void setReadyState(ReadyState);
143 void onReadyStateChange(ReadyState oldState, ReadyState newState);
145 Vector<PlatformTimeRanges> activeRanges() const;
147 ExceptionOr<Ref<SourceBufferPrivate>> createSourceBufferPrivate(const ContentType&);
148 void scheduleEvent(const AtomString& eventName);
150 bool hasBufferedTime(const MediaTime&);
151 bool hasCurrentTime();
152 bool hasFutureTime();
154 void regenerateActiveSourceBuffers();
158 static URLRegistry* s_registry;
160 RefPtr<MediaSourcePrivate> m_private;
161 RefPtr<SourceBufferList> m_sourceBuffers;
162 RefPtr<SourceBufferList> m_activeSourceBuffers;
163 mutable std::unique_ptr<PlatformTimeRanges> m_buffered;
164 std::unique_ptr<PlatformTimeRanges> m_liveSeekable;
165 HTMLMediaElement* m_mediaElement { nullptr };
166 MediaTime m_duration;
167 MediaTime m_pendingSeekTime;
168 ReadyState m_readyState { ReadyState::Closed };
169 UniqueRef<MainThreadGenericEventQueue> m_asyncEventQueue;
170 #if !RELEASE_LOG_DISABLED
171 Ref<const Logger> m_logger;
172 const void* m_logIdentifier { nullptr };
176 String convertEnumerationToString(MediaSource::EndOfStreamError);
177 String convertEnumerationToString(MediaSource::ReadyState);
179 } // namespace WebCore
183 template<typename Type>
187 struct LogArgument<WebCore::MediaSource::EndOfStreamError> {
188 static String toString(const WebCore::MediaSource::EndOfStreamError error)
190 return convertEnumerationToString(error);
195 struct LogArgument<WebCore::MediaSource::ReadyState> {
196 static String toString(const WebCore::MediaSource::ReadyState state)
198 return convertEnumerationToString(state);