summaryrefslogtreecommitdiff
path: root/dom/media/mediasource/SourceBuffer.h
blob: fe69489c2cee682dba154591c440bcddda8609e0 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
/* vim: set ts=8 sts=2 et sw=2 tw=80: */
/* This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this
 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */

#ifndef mozilla_dom_SourceBuffer_h_
#define mozilla_dom_SourceBuffer_h_

#include "MediaPromise.h"
#include "MediaSource.h"
#include "js/RootingAPI.h"
#include "mozilla/Assertions.h"
#include "mozilla/Atomics.h"
#include "mozilla/Attributes.h"
#include "mozilla/DOMEventTargetHelper.h"
#include "mozilla/dom/SourceBufferBinding.h"
#include "mozilla/dom/TypedArray.h"
#include "mozilla/mozalloc.h"
#include "nsAutoPtr.h"
#include "nsCOMPtr.h"
#include "nsCycleCollectionNoteChild.h"
#include "nsCycleCollectionParticipant.h"
#include "nsISupports.h"
#include "nsString.h"
#include "nscore.h"
#include "SourceBufferContentManager.h"
#include "mozilla/Monitor.h"

class JSObject;
struct JSContext;

namespace mozilla {

class ErrorResult;
class MediaLargeByteBuffer;
template <typename T> class AsyncEventRunner;
class TrackBuffersManager;

namespace dom {

using media::TimeUnit;
using media::TimeIntervals;

class TimeRanges;

class SourceBufferAttributes {
public:
  NS_INLINE_DECL_THREADSAFE_REFCOUNTING(SourceBufferAttributes);
  explicit SourceBufferAttributes(bool aGenerateTimestamp)
    : mGenerateTimestamps(aGenerateTimestamp)
    , mMonitor("SourceBufferAttributes")
    , mAppendWindowStart(0)
    , mAppendWindowEnd(PositiveInfinity<double>())
    , mAppendMode(SourceBufferAppendMode::Segments)
    , mApparentTimestampOffset(0)
  {}

  double GetAppendWindowStart()
  {
    MonitorAutoLock mon(mMonitor);
    return mAppendWindowStart;
  }

  double GetAppendWindowEnd()
  {
    MonitorAutoLock mon(mMonitor);
    return mAppendWindowEnd;
  }

  void SetAppendWindowStart(double aWindowStart)
  {
    MonitorAutoLock mon(mMonitor);
    mAppendWindowStart = aWindowStart;
  }

  void SetAppendWindowEnd(double aWindowEnd)
  {
    MonitorAutoLock mon(mMonitor);
    mAppendWindowEnd = aWindowEnd;
  }

  double GetApparentTimestampOffset()
  {
    MonitorAutoLock mon(mMonitor);
    return mApparentTimestampOffset;
  }

  void SetApparentTimestampOffset(double aTimestampOffset)
  {
    MonitorAutoLock mon(mMonitor);
    mApparentTimestampOffset = aTimestampOffset;
    mTimestampOffset = TimeUnit::FromSeconds(aTimestampOffset);
  }

  TimeUnit GetTimestampOffset()
  {
    MonitorAutoLock mon(mMonitor);
    return mTimestampOffset;
  }

  void SetTimestampOffset(TimeUnit& aTimestampOffset)
  {
    MonitorAutoLock mon(mMonitor);
    mTimestampOffset = aTimestampOffset;
    mApparentTimestampOffset = aTimestampOffset.ToSeconds();
  }

  SourceBufferAppendMode GetAppendMode()
  {
    MonitorAutoLock mon(mMonitor);
    return mAppendMode;
  }

  void SetAppendMode(SourceBufferAppendMode aAppendMode)
  {
    MonitorAutoLock mon(mMonitor);
    mAppendMode = aAppendMode;
  }

  // mGenerateTimestamp isn't mutable once the source buffer has been constructed
  // We don't need a monitor to protect it across threads.
  const bool mGenerateTimestamps;

private:
  ~SourceBufferAttributes() {};

  // Monitor protecting all members below.
  Monitor mMonitor;
  double mAppendWindowStart;
  double mAppendWindowEnd;
  SourceBufferAppendMode mAppendMode;
  double mApparentTimestampOffset;
  TimeUnit mTimestampOffset;
};

class SourceBuffer final : public DOMEventTargetHelper
{
public:
  /** WebIDL Methods. */
  SourceBufferAppendMode Mode() const
  {
    return mAttributes->GetAppendMode();
  }

  void SetMode(SourceBufferAppendMode aMode, ErrorResult& aRv);

  bool Updating() const
  {
    return mUpdating;
  }

  already_AddRefed<TimeRanges> GetBuffered(ErrorResult& aRv);

  double TimestampOffset() const
  {
    return mAttributes->GetApparentTimestampOffset();
  }

  void SetTimestampOffset(double aTimestampOffset, ErrorResult& aRv);

  double AppendWindowStart() const
  {
    return mAttributes->GetAppendWindowStart();
  }

  void SetAppendWindowStart(double aAppendWindowStart, ErrorResult& aRv);

  double AppendWindowEnd() const
  {
    return mAttributes->GetAppendWindowEnd();
  }

  void SetAppendWindowEnd(double aAppendWindowEnd, ErrorResult& aRv);

  void AppendBuffer(const ArrayBuffer& aData, ErrorResult& aRv);
  void AppendBuffer(const ArrayBufferView& aData, ErrorResult& aRv);

  void Abort(ErrorResult& aRv);
  void AbortBufferAppend();

  void Remove(double aStart, double aEnd, ErrorResult& aRv);

  IMPL_EVENT_HANDLER(updatestart);
  IMPL_EVENT_HANDLER(update);
  IMPL_EVENT_HANDLER(updateend);
  IMPL_EVENT_HANDLER(error);
  IMPL_EVENT_HANDLER(abort);

  /** End WebIDL Methods. */

  NS_DECL_ISUPPORTS_INHERITED
  NS_DECL_CYCLE_COLLECTION_CLASS_INHERITED(SourceBuffer, DOMEventTargetHelper)

  SourceBuffer(MediaSource* aMediaSource, const nsACString& aType);

  MediaSource* GetParentObject() const;

  JSObject* WrapObject(JSContext* aCx) override;

  // Notify the SourceBuffer that it has been detached from the
  // MediaSource's sourceBuffer list.
  void Detach();
  bool IsAttached() const
  {
    return mMediaSource != nullptr;
  }

  void Ended();

  // Evict data in the source buffer in the given time range.
  void Evict(double aStart, double aEnd);

  double GetBufferedStart();
  double GetBufferedEnd();

  // Runs the range removal algorithm as defined by the MSE spec.
  void RangeRemoval(double aStart, double aEnd);

  bool IsActive() const
  {
    return mActive;
  }

#if defined(DEBUG)
  void Dump(const char* aPath);
#endif

private:
  ~SourceBuffer();

  friend class AsyncEventRunner<SourceBuffer>;
  friend class BufferAppendRunnable;
  friend class mozilla::TrackBuffersManager;
  void DispatchSimpleEvent(const char* aName);
  void QueueAsyncSimpleEvent(const char* aName);

  // Update mUpdating and fire the appropriate events.
  void StartUpdating();
  void StopUpdating();
  void AbortUpdating();

  // If the media segment contains data beyond the current duration,
  // then run the duration change algorithm with new duration set to the
  // maximum of the current duration and the group end timestamp.
  void CheckEndTime();

  // Shared implementation of AppendBuffer overloads.
  void AppendData(const uint8_t* aData, uint32_t aLength, ErrorResult& aRv);
  void BufferAppend(uint32_t aAppendID);

  // Implement the "Append Error Algorithm".
  // Will call endOfStream() with "decode" error if aDecodeError is true.
  // 3.5.3 Append Error Algorithm
  // http://w3c.github.io/media-source/#sourcebuffer-append-error
  void AppendError(bool aDecoderError);

  // Implements the "Prepare Append Algorithm". Returns MediaLargeByteBuffer object
  // on success or nullptr (with aRv set) on error.
  already_AddRefed<MediaLargeByteBuffer> PrepareAppend(const uint8_t* aData,
                                                       uint32_t aLength,
                                                       ErrorResult& aRv);

  void AppendDataCompletedWithSuccess(bool aHasActiveTracks);
  void AppendDataErrored(nsresult aError);

  nsRefPtr<MediaSource> mMediaSource;

  uint32_t mEvictionThreshold;

  nsRefPtr<SourceBufferContentManager> mContentManager;
  nsRefPtr<SourceBufferAttributes> mAttributes;

  bool mUpdating;
  bool mIsUsingFormatReader;

  mozilla::Atomic<bool> mActive;

  // Each time mUpdating is set to true, mUpdateID will be incremented.
  // This allows for a queued AppendData task to identify if it was earlier
  // aborted and another AppendData queued.
  uint32_t mUpdateID;
  int64_t mReportedOffset;

  MediaPromiseRequestHolder<SourceBufferContentManager::AppendPromise> mPendingAppend;
  const nsCString mType;
};

} // namespace dom

} // namespace mozilla
#endif /* mozilla_dom_SourceBuffer_h_ */