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_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 : namespace dom {
41 :
42 : class TimeRanges;
43 :
44 : class SourceBuffer final : public DOMEventTargetHelper
45 : {
46 : public:
47 : /** WebIDL Methods. */
48 0 : SourceBufferAppendMode Mode() const
49 : {
50 0 : return mCurrentAttributes.GetAppendMode();
51 : }
52 :
53 : void SetMode(SourceBufferAppendMode aMode, ErrorResult& aRv);
54 :
55 0 : bool Updating() const
56 : {
57 0 : return mUpdating;
58 : }
59 :
60 : TimeRanges* GetBuffered(ErrorResult& aRv);
61 : media::TimeIntervals GetTimeIntervals();
62 :
63 0 : double TimestampOffset() const
64 : {
65 0 : return mCurrentAttributes.GetApparentTimestampOffset();
66 : }
67 :
68 : void SetTimestampOffset(double aTimestampOffset, ErrorResult& aRv);
69 :
70 0 : double AppendWindowStart() const
71 : {
72 0 : return mCurrentAttributes.GetAppendWindowStart();
73 : }
74 :
75 : void SetAppendWindowStart(double aAppendWindowStart, ErrorResult& aRv);
76 :
77 0 : double AppendWindowEnd() const
78 : {
79 0 : return mCurrentAttributes.GetAppendWindowEnd();
80 : }
81 :
82 : void SetAppendWindowEnd(double aAppendWindowEnd, ErrorResult& aRv);
83 :
84 : void AppendBuffer(const ArrayBuffer& aData, ErrorResult& aRv);
85 : void AppendBuffer(const ArrayBufferView& aData, ErrorResult& aRv);
86 :
87 : void Abort(ErrorResult& aRv);
88 : void AbortBufferAppend();
89 :
90 : void Remove(double aStart, double aEnd, ErrorResult& aRv);
91 :
92 0 : IMPL_EVENT_HANDLER(updatestart);
93 0 : IMPL_EVENT_HANDLER(update);
94 0 : IMPL_EVENT_HANDLER(updateend);
95 0 : IMPL_EVENT_HANDLER(error);
96 0 : IMPL_EVENT_HANDLER(abort);
97 :
98 : /** End WebIDL Methods. */
99 :
100 : NS_DECL_ISUPPORTS_INHERITED
101 0 : NS_DECL_CYCLE_COLLECTION_CLASS_INHERITED(SourceBuffer, DOMEventTargetHelper)
102 :
103 : SourceBuffer(MediaSource* aMediaSource, const MediaContainerType& aType);
104 :
105 : MediaSource* GetParentObject() const;
106 :
107 : JSObject* WrapObject(JSContext* aCx, JS::Handle<JSObject*> aGivenProto) override;
108 :
109 : // Notify the SourceBuffer that it has been detached from the
110 : // MediaSource's sourceBuffer list.
111 : void Detach();
112 0 : bool IsAttached() const
113 : {
114 0 : return mMediaSource != nullptr;
115 : }
116 :
117 : void Ended();
118 :
119 : double GetBufferedStart();
120 : double GetBufferedEnd();
121 : double HighestStartTime();
122 : double HighestEndTime();
123 :
124 : // Runs the range removal algorithm as defined by the MSE spec.
125 : void RangeRemoval(double aStart, double aEnd);
126 :
127 0 : bool IsActive() const
128 : {
129 0 : return mActive;
130 : }
131 :
132 : private:
133 : ~SourceBuffer();
134 :
135 : friend class AsyncEventRunner<SourceBuffer>;
136 : friend class BufferAppendRunnable;
137 : friend class mozilla::TrackBuffersManager;
138 : void DispatchSimpleEvent(const char* aName);
139 : void QueueAsyncSimpleEvent(const char* aName);
140 :
141 : // Update mUpdating and fire the appropriate events.
142 : void StartUpdating();
143 : void StopUpdating();
144 : void AbortUpdating();
145 : void ResetParserState();
146 :
147 : // If the media segment contains data beyond the current duration,
148 : // then run the duration change algorithm with new duration set to the
149 : // maximum of the current duration and the group end timestamp.
150 : void CheckEndTime();
151 :
152 : // Shared implementation of AppendBuffer overloads.
153 : void AppendData(const uint8_t* aData, uint32_t aLength, ErrorResult& aRv);
154 :
155 : // Implement the "Append Error Algorithm".
156 : // Will call endOfStream() with "decode" error if aDecodeError is true.
157 : // 3.5.3 Append Error Algorithm
158 : // http://w3c.github.io/media-source/#sourcebuffer-append-error
159 : void AppendError(const MediaResult& aDecodeError);
160 :
161 : // Implements the "Prepare Append Algorithm". Returns MediaByteBuffer object
162 : // on success or nullptr (with aRv set) on error.
163 : already_AddRefed<MediaByteBuffer> PrepareAppend(const uint8_t* aData,
164 : uint32_t aLength,
165 : ErrorResult& aRv);
166 :
167 : void AppendDataCompletedWithSuccess(const SourceBufferTask::AppendBufferResult& aResult);
168 : void AppendDataErrored(const MediaResult& aError);
169 :
170 : RefPtr<MediaSource> mMediaSource;
171 : const RefPtr<AbstractThread> mAbstractMainThread;
172 :
173 : RefPtr<TrackBuffersManager> mTrackBuffersManager;
174 : SourceBufferAttributes mCurrentAttributes;
175 :
176 : bool mUpdating;
177 :
178 : mozilla::Atomic<bool> mActive;
179 :
180 : MozPromiseRequestHolder<SourceBufferTask::AppendPromise> mPendingAppend;
181 : MozPromiseRequestHolder<SourceBufferTask::RangeRemovalPromise> mPendingRemoval;
182 : const MediaContainerType mType;
183 :
184 : RefPtr<TimeRanges> mBuffered;
185 :
186 : MozPromiseRequestHolder<MediaSource::ActiveCompletionPromise> mCompletionPromise;
187 : };
188 :
189 : } // namespace dom
190 :
191 : } // namespace mozilla
192 :
193 : #endif /* mozilla_dom_SourceBuffer_h_ */
|