Line data Source code
1 : /* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
2 : /* vim: set ts=8 sts=2 et sw=2 tw=80: */
3 : /* This Source Code Form is subject to the terms of the Mozilla Public
4 : * License, v. 2.0. If a copy of the MPL was not distributed with this
5 : * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
6 :
7 : #ifndef mozilla_dom_MediaSource_h_
8 : #define mozilla_dom_MediaSource_h_
9 :
10 : #include "MediaSourceDecoder.h"
11 : #include "js/RootingAPI.h"
12 : #include "mozilla/Assertions.h"
13 : #include "mozilla/Attributes.h"
14 : #include "mozilla/DOMEventTargetHelper.h"
15 : #include "mozilla/MozPromise.h"
16 : #include "mozilla/dom/MediaSourceBinding.h"
17 : #include "nsCOMPtr.h"
18 : #include "nsCycleCollectionNoteChild.h"
19 : #include "nsCycleCollectionParticipant.h"
20 : #include "nsID.h"
21 : #include "nsISupports.h"
22 : #include "nscore.h"
23 : #include "TimeUnits.h"
24 :
25 : struct JSContext;
26 : class JSObject;
27 : class nsPIDOMWindowInner;
28 :
29 : namespace mozilla {
30 :
31 : class AbstractThread;
32 : class ErrorResult;
33 : template <typename T> class AsyncEventRunner;
34 : class MediaResult;
35 :
36 : namespace dom {
37 :
38 : class GlobalObject;
39 : class SourceBuffer;
40 : class SourceBufferList;
41 : template <typename T> class Optional;
42 :
43 : #define MOZILLA_DOM_MEDIASOURCE_IMPLEMENTATION_IID \
44 : { 0x3839d699, 0x22c5, 0x439f, \
45 : { 0x94, 0xca, 0x0e, 0x0b, 0x26, 0xf9, 0xca, 0xbf } }
46 :
47 : class MediaSource final : public DOMEventTargetHelper
48 : {
49 : public:
50 : /** WebIDL Methods. */
51 : static already_AddRefed<MediaSource>
52 : Constructor(const GlobalObject& aGlobal,
53 : ErrorResult& aRv);
54 :
55 : SourceBufferList* SourceBuffers();
56 : SourceBufferList* ActiveSourceBuffers();
57 : MediaSourceReadyState ReadyState();
58 :
59 : double Duration();
60 : void SetDuration(double aDuration, ErrorResult& aRv);
61 :
62 : already_AddRefed<SourceBuffer> AddSourceBuffer(const nsAString& aType, ErrorResult& aRv);
63 : void RemoveSourceBuffer(SourceBuffer& aSourceBuffer, ErrorResult& aRv);
64 :
65 : void EndOfStream(const Optional<MediaSourceEndOfStreamError>& aError, ErrorResult& aRv);
66 : void EndOfStream(const MediaResult& aError);
67 :
68 : void SetLiveSeekableRange(double aStart, double aEnd, ErrorResult& aRv);
69 : void ClearLiveSeekableRange(ErrorResult& aRv);
70 :
71 : static bool IsTypeSupported(const GlobalObject&, const nsAString& aType);
72 : static nsresult IsTypeSupported(const nsAString& aType, DecoderDoctorDiagnostics* aDiagnostics);
73 :
74 : static bool Enabled(JSContext* cx, JSObject* aGlobal);
75 :
76 0 : IMPL_EVENT_HANDLER(sourceopen);
77 0 : IMPL_EVENT_HANDLER(sourceended);
78 0 : IMPL_EVENT_HANDLER(sourceclosed);
79 :
80 : /** End WebIDL Methods. */
81 :
82 : NS_DECL_ISUPPORTS_INHERITED
83 0 : NS_DECL_CYCLE_COLLECTION_CLASS_INHERITED(MediaSource, DOMEventTargetHelper)
84 : NS_DECLARE_STATIC_IID_ACCESSOR(MOZILLA_DOM_MEDIASOURCE_IMPLEMENTATION_IID)
85 :
86 : nsPIDOMWindowInner* GetParentObject() const;
87 :
88 : JSObject* WrapObject(JSContext* aCx, JS::Handle<JSObject*> aGivenProto) override;
89 :
90 : // Attach this MediaSource to Decoder aDecoder. Returns false if already attached.
91 : bool Attach(MediaSourceDecoder* aDecoder);
92 : void Detach();
93 :
94 : // Set mReadyState to aState and fire the required events at the MediaSource.
95 : void SetReadyState(MediaSourceReadyState aState);
96 :
97 : // Used by SourceBuffer to call CreateSubDecoder.
98 0 : MediaSourceDecoder* GetDecoder()
99 : {
100 0 : return mDecoder;
101 : }
102 :
103 0 : nsIPrincipal* GetPrincipal()
104 : {
105 0 : return mPrincipal;
106 : }
107 :
108 : // Returns a string describing the state of the MediaSource internal
109 : // buffered data. Used for debugging purposes.
110 : void GetMozDebugReaderData(nsAString& aString);
111 :
112 0 : bool HasLiveSeekableRange() const { return mLiveSeekableRange.isSome(); }
113 0 : media::TimeInterval LiveSeekableRange() const
114 : {
115 0 : return mLiveSeekableRange.value();
116 : }
117 :
118 0 : AbstractThread* AbstractMainThread() const
119 : {
120 0 : return mAbstractMainThread;
121 : }
122 :
123 : // Resolve all CompletionPromise pending.
124 : void CompletePendingTransactions();
125 :
126 : private:
127 : // SourceBuffer uses SetDuration and SourceBufferIsActive
128 : friend class mozilla::dom::SourceBuffer;
129 :
130 : ~MediaSource();
131 :
132 : explicit MediaSource(nsPIDOMWindowInner* aWindow);
133 :
134 : friend class AsyncEventRunner<MediaSource>;
135 : void DispatchSimpleEvent(const char* aName);
136 : void QueueAsyncSimpleEvent(const char* aName);
137 :
138 : void DurationChange(double aNewDuration, ErrorResult& aRv);
139 :
140 : // SetDuration with no checks.
141 : void SetDuration(double aDuration);
142 :
143 : typedef MozPromise<bool, MediaResult, /* IsExclusive = */ true>
144 : ActiveCompletionPromise;
145 : // Mark SourceBuffer as active and rebuild ActiveSourceBuffers.
146 : // Return a MozPromise that will be resolved once all related operations are
147 : // completed, or can't progress any further.
148 : // Such as, transition of readyState from HAVE_NOTHING to HAVE_METADATA.
149 : RefPtr<ActiveCompletionPromise> SourceBufferIsActive(
150 : SourceBuffer* aSourceBuffer);
151 :
152 : RefPtr<SourceBufferList> mSourceBuffers;
153 : RefPtr<SourceBufferList> mActiveSourceBuffers;
154 :
155 : RefPtr<MediaSourceDecoder> mDecoder;
156 : // Ensures the media element remains alive to dispatch progress and
157 : // durationchanged events.
158 : RefPtr<HTMLMediaElement> mMediaElement;
159 :
160 : RefPtr<nsIPrincipal> mPrincipal;
161 :
162 : const RefPtr<AbstractThread> mAbstractMainThread;
163 :
164 : MediaSourceReadyState mReadyState;
165 :
166 : Maybe<media::TimeInterval> mLiveSeekableRange;
167 : nsTArray<MozPromiseHolder<ActiveCompletionPromise>> mCompletionPromises;
168 : };
169 :
170 : NS_DEFINE_STATIC_IID_ACCESSOR(MediaSource, MOZILLA_DOM_MEDIASOURCE_IMPLEMENTATION_IID)
171 :
172 : } // namespace dom
173 :
174 : } // namespace mozilla
175 :
176 : #endif /* mozilla_dom_MediaSource_h_ */
|