SourceBuffer.h (6771B)
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_SourceBuffer_h_ 8 #define mozilla_dom_SourceBuffer_h_ 9 10 #include "MediaContainerType.h" 11 #include "MediaSource.h" 12 #include "SourceBufferTask.h" 13 #include "TrackBuffersManager.h" 14 #include "js/RootingAPI.h" 15 #include "mozilla/Atomics.h" 16 #include "mozilla/DOMEventTargetHelper.h" 17 #include "mozilla/MozPromise.h" 18 #include "mozilla/dom/SourceBufferBinding.h" 19 #include "mozilla/dom/TypedArray.h" 20 #include "mozilla/mozalloc.h" 21 #include "nsCOMPtr.h" 22 #include "nsCycleCollectionNoteChild.h" 23 #include "nsCycleCollectionParticipant.h" 24 #include "nsISupports.h" 25 #include "nscore.h" 26 27 class JSObject; 28 struct JSContext; 29 30 namespace mozilla { 31 32 class AbstractThread; 33 class ErrorResult; 34 class MediaByteBuffer; 35 template <typename T> 36 class AsyncEventRunner; 37 38 DDLoggedTypeName(dom::SourceBuffer); 39 40 namespace dom { 41 42 class TimeRanges; 43 44 class SourceBuffer final : public DOMEventTargetHelper, 45 public DecoderDoctorLifeLogger<SourceBuffer> { 46 public: 47 /** WebIDL Methods. */ 48 SourceBufferAppendMode Mode() const { 49 return mCurrentAttributes.GetAppendMode(); 50 } 51 52 void SetMode(SourceBufferAppendMode aMode, ErrorResult& aRv); 53 54 bool Updating() const { return mUpdating; } 55 56 TimeRanges* GetBuffered(ErrorResult& aRv); 57 media::TimeIntervals GetTimeIntervals(); 58 59 double TimestampOffset() const { 60 return mCurrentAttributes.GetApparentTimestampOffset(); 61 } 62 63 void SetTimestampOffset(double aTimestampOffset, ErrorResult& aRv); 64 65 double AppendWindowStart() const { 66 return mCurrentAttributes.GetAppendWindowStart(); 67 } 68 69 void SetAppendWindowStart(double aAppendWindowStart, ErrorResult& aRv); 70 71 double AppendWindowEnd() const { 72 return mCurrentAttributes.GetAppendWindowEnd(); 73 } 74 75 void SetAppendWindowEnd(double aAppendWindowEnd, ErrorResult& aRv); 76 77 void AppendBuffer(const ArrayBuffer& aData, ErrorResult& aRv); 78 void AppendBuffer(const ArrayBufferView& aData, ErrorResult& aRv); 79 80 already_AddRefed<Promise> AppendBufferAsync(const ArrayBuffer& aData, 81 ErrorResult& aRv); 82 already_AddRefed<Promise> AppendBufferAsync(const ArrayBufferView& aData, 83 ErrorResult& aRv); 84 85 void Abort(ErrorResult& aRv); 86 void AbortBufferAppend(); 87 88 void Remove(double aStart, double aEnd, ErrorResult& aRv); 89 90 already_AddRefed<Promise> RemoveAsync(double aStart, double aEnd, 91 ErrorResult& aRv); 92 93 void ChangeType(const nsAString& aType, ErrorResult& aRv); 94 95 IMPL_EVENT_HANDLER(updatestart); 96 IMPL_EVENT_HANDLER(update); 97 IMPL_EVENT_HANDLER(updateend); 98 IMPL_EVENT_HANDLER(error); 99 IMPL_EVENT_HANDLER(abort); 100 101 /** End WebIDL Methods. */ 102 103 NS_DECL_ISUPPORTS_INHERITED 104 NS_DECL_CYCLE_COLLECTION_CLASS_INHERITED(SourceBuffer, DOMEventTargetHelper) 105 106 SourceBuffer(MediaSource* aMediaSource, const MediaContainerType& aType); 107 108 MediaSource* GetParentObject() const; 109 110 JSObject* WrapObject(JSContext* aCx, 111 JS::Handle<JSObject*> aGivenProto) override; 112 113 // Notify the SourceBuffer that it has been detached from the 114 // MediaSource's sourceBuffer list. 115 void Detach(); 116 bool IsAttached() const { return mMediaSource != nullptr; } 117 118 void SetEnded(const Optional<MediaSourceEndOfStreamError>& aError); 119 120 media::TimeIntervals GetBufferedIntervals(); 121 media::TimeUnit GetBufferedEnd(); 122 media::TimeUnit HighestStartTime(); 123 media::TimeUnit HighestEndTime(); 124 125 // Runs the range removal algorithm as defined by the MSE spec. 126 void RangeRemoval(double aStart, double aEnd); 127 128 bool IsActive() const { return mActive; } 129 130 private: 131 ~SourceBuffer(); 132 133 friend class AsyncEventRunner<SourceBuffer>; 134 friend class BufferAppendRunnable; 135 friend class mozilla::TrackBuffersManager; 136 void DispatchSimpleEvent(const char* aName); 137 void QueueAsyncSimpleEvent(const char* aName); 138 139 // Update mUpdating and fire the appropriate events. 140 void StartUpdating(); 141 void StopUpdating(); 142 void AbortUpdating(); 143 void ResetParserState(); 144 145 // If the media segment contains data beyond the current duration, 146 // then run the duration change algorithm with new duration set to the 147 // maximum of the current duration and the group end timestamp. 148 void CheckEndTime(); 149 150 // Shared implementation of AppendBuffer overloads. 151 void AppendData(RefPtr<MediaByteBuffer>&& aData, ErrorResult& aRv); 152 // Shared implementation of AppendBufferAsync overloads. 153 already_AddRefed<Promise> AppendDataAsync(RefPtr<MediaByteBuffer>&& aData, 154 ErrorResult& aRv); 155 156 void PrepareRemove(double aStart, double aEnd, ErrorResult& aRv); 157 158 // Implement the "Append Error Algorithm". 159 // Will call endOfStream() with "decode" error if aDecodeError is true. 160 // 3.5.3 Append Error Algorithm 161 // http://w3c.github.io/media-source/#sourcebuffer-append-error 162 void AppendError(const MediaResult& aDecodeError); 163 164 // Implements the "Prepare Append Algorithm". Returns MediaByteBuffer object 165 // on success or nullptr (with aRv set) on error. 166 already_AddRefed<MediaByteBuffer> PrepareAppend(const uint8_t* aData, 167 uint32_t aLength, 168 ErrorResult& aRv); 169 template <typename T> 170 already_AddRefed<MediaByteBuffer> PrepareAppend(const T& aData, 171 ErrorResult& aRv); 172 173 void AppendDataCompletedWithSuccess( 174 const SourceBufferTask::AppendBufferResult& aResult); 175 void AppendDataErrored(const MediaResult& aError); 176 177 RefPtr<MediaSource> mMediaSource; 178 const RefPtr<AbstractThread> mAbstractMainThread; 179 180 RefPtr<TrackBuffersManager> mTrackBuffersManager; 181 SourceBufferAttributes mCurrentAttributes; 182 183 bool mUpdating; 184 185 mozilla::Atomic<bool> mActive; 186 187 MozPromiseRequestHolder<SourceBufferTask::AppendPromise> mPendingAppend; 188 MozPromiseRequestHolder<SourceBufferTask::RangeRemovalPromise> 189 mPendingRemoval; 190 MediaContainerType mType; 191 192 RefPtr<TimeRanges> mBuffered; 193 194 MozPromiseRequestHolder<MediaSource::ActiveCompletionPromise> 195 mCompletionPromise; 196 197 // Only used if MSE v2 experimental mode is active. 198 // Contains the current Promise to be resolved following use of 199 // appendBufferAsync and removeAsync. Not set of no operation is pending. 200 RefPtr<Promise> mDOMPromise; 201 }; 202 203 } // namespace dom 204 205 } // namespace mozilla 206 207 #endif /* mozilla_dom_SourceBuffer_h_ */