MediaSource.h (5699B)
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 "TimeUnits.h" 12 #include "js/RootingAPI.h" 13 #include "mozilla/DOMEventTargetHelper.h" 14 #include "mozilla/MozPromise.h" 15 #include "mozilla/dom/MediaSourceBinding.h" 16 #include "nsCOMPtr.h" 17 #include "nsCycleCollectionNoteChild.h" 18 #include "nsCycleCollectionParticipant.h" 19 #include "nsID.h" 20 #include "nsISupports.h" 21 #include "nscore.h" 22 23 struct JSContext; 24 class JSObject; 25 class nsPIDOMWindowInner; 26 27 namespace mozilla { 28 29 class AbstractThread; 30 class ErrorResult; 31 template <typename T> 32 class AsyncEventRunner; 33 class MediaResult; 34 35 namespace dom { 36 class MediaSource; 37 } // namespace dom 38 DDLoggedTypeName(dom::MediaSource); 39 40 namespace dom { 41 42 class GlobalObject; 43 class SourceBuffer; 44 class SourceBufferList; 45 template <typename T> 46 class Optional; 47 48 #define MOZILLA_DOM_MEDIASOURCE_IMPLEMENTATION_IID \ 49 {0x3839d699, 0x22c5, 0x439f, {0x94, 0xca, 0x0e, 0x0b, 0x26, 0xf9, 0xca, 0xbf}} 50 51 class MediaSource final : public DOMEventTargetHelper, 52 public DecoderDoctorLifeLogger<MediaSource> { 53 public: 54 /** WebIDL Methods. */ 55 static already_AddRefed<MediaSource> Constructor(const GlobalObject& aGlobal, 56 ErrorResult& aRv); 57 58 SourceBufferList* SourceBuffers(); 59 SourceBufferList* ActiveSourceBuffers(); 60 MediaSourceReadyState ReadyState(); 61 62 double Duration(); 63 void SetDuration(double aDuration, ErrorResult& aRv); 64 65 already_AddRefed<SourceBuffer> AddSourceBuffer(const nsAString& aType, 66 ErrorResult& aRv); 67 void RemoveSourceBuffer(SourceBuffer& aSourceBuffer, ErrorResult& aRv); 68 69 void EndOfStream(const Optional<MediaSourceEndOfStreamError>& aError, 70 ErrorResult& aRv); 71 void EndOfStream(const MediaResult& aError); 72 73 void SetLiveSeekableRange(double aStart, double aEnd, ErrorResult& aRv); 74 void ClearLiveSeekableRange(ErrorResult& aRv); 75 76 static bool IsTypeSupported(const GlobalObject&, const nsAString& aType); 77 // Throws on aRv if not supported. 78 static void IsTypeSupported(const nsAString& aType, 79 DecoderDoctorDiagnostics* aDiagnostics, 80 ErrorResult& aRv, 81 Maybe<bool> aShouldResistFingerprinting); 82 83 IMPL_EVENT_HANDLER(sourceopen); 84 IMPL_EVENT_HANDLER(sourceended); 85 IMPL_EVENT_HANDLER(sourceclose); 86 87 /** End WebIDL Methods. */ 88 89 NS_DECL_ISUPPORTS_INHERITED 90 NS_DECL_CYCLE_COLLECTION_CLASS_INHERITED(MediaSource, DOMEventTargetHelper) 91 NS_INLINE_DECL_STATIC_IID(MOZILLA_DOM_MEDIASOURCE_IMPLEMENTATION_IID) 92 93 nsPIDOMWindowInner* GetParentObject() const; 94 95 JSObject* WrapObject(JSContext* aCx, 96 JS::Handle<JSObject*> aGivenProto) override; 97 98 // Attach this MediaSource to Decoder aDecoder. Returns false if already 99 // attached. 100 bool Attach(MediaSourceDecoder* aDecoder); 101 void Detach(); 102 103 // Set mReadyState to aState and fire the required events at the MediaSource. 104 void SetReadyState(MediaSourceReadyState aState); 105 106 // Used by SourceBuffer to call CreateSubDecoder. 107 MediaSourceDecoder* GetDecoder() { return mDecoder; } 108 109 nsIPrincipal* GetPrincipal() { return mPrincipal; } 110 111 // Returns a structure describing the state of the MediaSource internal 112 // buffered data. Used for debugging purposes. 113 already_AddRefed<Promise> MozDebugReaderData(ErrorResult& aRv); 114 115 bool HasLiveSeekableRange() const { return mLiveSeekableRange.isSome(); } 116 media::TimeRanges LiveSeekableRange() const { 117 return mLiveSeekableRange.value(); 118 } 119 120 AbstractThread* AbstractMainThread() const { return mAbstractMainThread; } 121 122 // Resolve all CompletionPromise pending. 123 void CompletePendingTransactions(); 124 125 private: 126 // SourceBuffer uses SetDuration and SourceBufferIsActive 127 friend class mozilla::dom::SourceBuffer; 128 129 ~MediaSource(); 130 131 explicit MediaSource(nsPIDOMWindowInner* aWindow); 132 133 friend class AsyncEventRunner<MediaSource>; 134 void DispatchSimpleEvent(const char* aName); 135 void QueueAsyncSimpleEvent(const char* aName); 136 137 void DurationChangeOnEndOfStream(); 138 void DurationChange(double aNewDuration, ErrorResult& aRv); 139 140 // SetDuration with no checks. 141 void SetDuration(const media::TimeUnit& 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::TimeRanges> mLiveSeekableRange; 167 nsTArray<MozPromiseHolder<ActiveCompletionPromise>> mCompletionPromises; 168 }; 169 170 } // namespace dom 171 172 } // namespace mozilla 173 174 #endif /* mozilla_dom_MediaSource_h_ */