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