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
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
|
/* -*- 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_TRACKBUFFERSMANAGER_H_
#define MOZILLA_TRACKBUFFERSMANAGER_H_
#include "SourceBufferContentManager.h"
#include "MediaDataDemuxer.h"
#include "MediaSourceDecoder.h"
#include "mozilla/Atomics.h"
#include "mozilla/Maybe.h"
#include "mozilla/Monitor.h"
#include "mozilla/Pair.h"
#include "nsProxyRelease.h"
#include "nsTArray.h"
namespace mozilla {
class ContainerParser;
class MediaLargeByteBuffer;
class MediaRawData;
class MediaSourceDemuxer;
class SourceBufferResource;
namespace dom {
class SourceBufferAttributes;
}
using media::TimeUnit;
using media::TimeInterval;
using media::TimeIntervals;
using dom::SourceBufferAppendMode;
class TrackBuffersManager : public SourceBufferContentManager {
public:
typedef MediaPromise<bool, nsresult, /* IsExclusive = */ true> CodedFrameProcessingPromise;
typedef TrackInfo::TrackType TrackType;
typedef MediaData::Type MediaType;
typedef nsTArray<nsRefPtr<MediaRawData>> TrackBuffer;
TrackBuffersManager(dom::SourceBufferAttributes* aAttributes,
MediaSourceDecoder* aParentDecoder,
const nsACString& aType);
bool AppendData(MediaLargeByteBuffer* aData,
TimeUnit aTimestampOffset) override;
nsRefPtr<AppendPromise> BufferAppend() override;
void AbortAppendData() override;
void ResetParserState() override;
nsRefPtr<RangeRemovalPromise> RangeRemoval(TimeUnit aStart,
TimeUnit aEnd) override;
EvictDataResult
EvictData(TimeUnit aPlaybackTime,
uint32_t aThreshold,
TimeUnit* aBufferStartTime) override;
void EvictBefore(TimeUnit aTime) override;
TimeIntervals Buffered() override;
int64_t GetSize() override;
void Ended() override;
void Detach() override;
AppendState GetAppendState() override
{
return mAppendState;
}
void SetGroupStartTimestamp(const TimeUnit& aGroupStartTimestamp) override;
void RestartGroupStartTimestamp() override;
TimeUnit GroupEndTimestamp() override;
// Interface for MediaSourceDemuxer
MediaInfo GetMetadata();
const TrackBuffer& GetTrackBuffer(TrackInfo::TrackType aTrack);
const TimeIntervals& Buffered(TrackInfo::TrackType);
bool IsEnded() const
{
return mEnded;
}
TimeUnit Seek(TrackInfo::TrackType aTrack,
const TimeUnit& aTime,
const TimeUnit& aFuzz);
uint32_t SkipToNextRandomAccessPoint(TrackInfo::TrackType aTrack,
const TimeUnit& aTimeThreadshold,
bool& aFound);
already_AddRefed<MediaRawData> GetSample(TrackInfo::TrackType aTrack,
const TimeUnit& aFuzz,
bool& aError);
TimeUnit GetNextRandomAccessPoint(TrackInfo::TrackType aTrack);
#if defined(DEBUG)
void Dump(const char* aPath) override;
#endif
private:
virtual ~TrackBuffersManager();
// All following functions run on the taskqueue.
nsRefPtr<AppendPromise> InitSegmentParserLoop();
void ScheduleSegmentParserLoop();
void SegmentParserLoop();
void AppendIncomingBuffers();
void InitializationSegmentReceived();
void ShutdownDemuxers();
void CreateDemuxerforMIMEType();
void NeedMoreData();
void RejectAppend(nsresult aRejectValue, const char* aName);
// Will return a promise that will be resolved once all frames of the current
// media segment have been processed.
nsRefPtr<CodedFrameProcessingPromise> CodedFrameProcessing();
void CompleteCodedFrameProcessing();
// Called by ResetParserState. Complete parsing the input buffer for the
// current media segment.
void FinishCodedFrameProcessing();
void CompleteResetParserState();
nsRefPtr<RangeRemovalPromise>
CodedFrameRemovalWithPromise(TimeInterval aInterval);
bool CodedFrameRemoval(TimeInterval aInterval);
void SetAppendState(AppendState aAppendState);
bool HasVideo() const
{
return mVideoTracks.mNumTracks > 0;
}
bool HasAudio() const
{
return mAudioTracks.mNumTracks > 0;
}
typedef Pair<nsRefPtr<MediaLargeByteBuffer>, TimeUnit> IncomingBuffer;
void AppendIncomingBuffer(IncomingBuffer aData);
nsTArray<IncomingBuffer> mIncomingBuffers;
// The input buffer as per http://w3c.github.io/media-source/index.html#sourcebuffer-input-buffer
nsRefPtr<MediaLargeByteBuffer> mInputBuffer;
// The current append state as per https://w3c.github.io/media-source/#sourcebuffer-append-state
// Accessed on both the main thread and the task queue.
Atomic<AppendState> mAppendState;
// Buffer full flag as per https://w3c.github.io/media-source/#sourcebuffer-buffer-full-flag.
// Accessed on both the main thread and the task queue.
// TODO: Unused for now.
Atomic<bool> mBufferFull;
bool mFirstInitializationSegmentReceived;
bool mActiveTrack;
Maybe<TimeUnit> mGroupStartTimestamp;
TimeUnit mGroupEndTimestamp;
nsCString mType;
// ContainerParser objects and methods.
// Those are used to parse the incoming input buffer.
// Recreate the ContainerParser and if aReuseInitData is true then
// feed it with the previous init segment found.
void RecreateParser(bool aReuseInitData);
nsAutoPtr<ContainerParser> mParser;
// Demuxer objects and methods.
void AppendDataToCurrentInputBuffer(MediaLargeByteBuffer* aData);
nsRefPtr<MediaLargeByteBuffer> mInitData;
nsRefPtr<SourceBufferResource> mCurrentInputBuffer;
nsRefPtr<MediaDataDemuxer> mInputDemuxer;
// Length already processed in current media segment.
uint64_t mProcessedInput;
void OnDemuxerInitDone(nsresult);
void OnDemuxerInitFailed(DemuxerFailureReason aFailure);
MediaPromiseRequestHolder<MediaDataDemuxer::InitPromise> mDemuxerInitRequest;
bool mEncrypted;
void OnDemuxFailed(TrackType aTrack, DemuxerFailureReason aFailure);
void DoDemuxVideo();
void OnVideoDemuxCompleted(nsRefPtr<MediaTrackDemuxer::SamplesHolder> aSamples);
void OnVideoDemuxFailed(DemuxerFailureReason aFailure)
{
mVideoTracks.mDemuxRequest.Complete();
OnDemuxFailed(TrackType::kVideoTrack, aFailure);
}
void DoDemuxAudio();
void OnAudioDemuxCompleted(nsRefPtr<MediaTrackDemuxer::SamplesHolder> aSamples);
void OnAudioDemuxFailed(DemuxerFailureReason aFailure)
{
mAudioTracks.mDemuxRequest.Complete();
OnDemuxFailed(TrackType::kAudioTrack, aFailure);
}
void DoEvictData(const TimeUnit& aPlaybackTime, uint32_t aThreshold);
struct TrackData {
TrackData()
: mNumTracks(0)
, mNeedRandomAccessPoint(true)
, mSizeBuffer(0)
{}
uint32_t mNumTracks;
// Definition of variables:
// https://w3c.github.io/media-source/#track-buffers
// Last decode timestamp variable that stores the decode timestamp of the
// last coded frame appended in the current coded frame group.
// The variable is initially unset to indicate that no coded frames have
// been appended yet.
Maybe<TimeUnit> mLastDecodeTimestamp;
// Last frame duration variable that stores the coded frame duration of the
// last coded frame appended in the current coded frame group.
// The variable is initially unset to indicate that no coded frames have
// been appended yet.
Maybe<TimeUnit> mLastFrameDuration;
// Highest end timestamp variable that stores the highest coded frame end
// timestamp across all coded frames in the current coded frame group that
// were appended to this track buffer.
// The variable is initially unset to indicate that no coded frames have
// been appended yet.
Maybe<TimeUnit> mHighestEndTimestamp;
// Longest frame duration seen in a coded frame group.
Maybe<TimeUnit> mLongestFrameDuration;
// Need random access point flag variable that keeps track of whether the
// track buffer is waiting for a random access point coded frame.
// The variable is initially set to true to indicate that random access
// point coded frame is needed before anything can be added to the track
// buffer.
bool mNeedRandomAccessPoint;
nsRefPtr<MediaTrackDemuxer> mDemuxer;
MediaPromiseRequestHolder<MediaTrackDemuxer::SamplesPromise> mDemuxRequest;
// If set, position where the next contiguous frame will be inserted.
// If a discontinuity is detected, it will be unset and recalculated upon
// the next insertion.
Maybe<size_t> mNextInsertionIndex;
// Samples just demuxed, but not yet parsed.
TrackBuffer mQueuedSamples;
// We only manage a single track of each type at this time.
nsTArray<TrackBuffer> mBuffers;
// Track buffer ranges variable that represents the presentation time ranges
// occupied by the coded frames currently stored in the track buffer.
TimeIntervals mBufferedRanges;
// Sanitized mBufferedRanges with a fuzz of half a sample's duration applied
// This buffered ranges is the basis of what is exposed to the JS.
TimeIntervals mSanitizedBufferedRanges;
// Byte size of all samples contained in this track buffer.
uint32_t mSizeBuffer;
// TrackInfo of the first metadata received.
nsRefPtr<SharedTrackInfo> mInfo;
// TrackInfo of the last metadata parsed (updated with each init segment.
nsRefPtr<SharedTrackInfo> mLastInfo;
// If set, position of the next sample to be retrieved by GetSample().
// If the position is equal to the TrackBuffer's length, it indicates that
// we've reached EOS.
Maybe<uint32_t> mNextGetSampleIndex;
// Approximation of the next sample's decode timestamp.
TimeUnit mNextSampleTimecode;
// Approximation of the next sample's presentation timestamp.
TimeUnit mNextSampleTime;
void ResetAppendState()
{
mLastDecodeTimestamp.reset();
mLastFrameDuration.reset();
mHighestEndTimestamp.reset();
mNeedRandomAccessPoint = true;
mLongestFrameDuration.reset();
mNextInsertionIndex.reset();
}
};
void CheckSequenceDiscontinuity(const TimeUnit& aPresentationTime);
void ProcessFrames(TrackBuffer& aSamples, TrackData& aTrackData);
bool CheckNextInsertionIndex(TrackData& aTrackData,
const TimeUnit& aSampleTime);
void InsertFrames(TrackBuffer& aSamples,
const TimeIntervals& aIntervals,
TrackData& aTrackData);
void RemoveFrames(const TimeIntervals& aIntervals,
TrackData& aTrackData,
uint32_t aStartIndex);
// Find index of sample. Return a negative value if not found.
uint32_t FindSampleIndex(const TrackBuffer& aTrackBuffer,
const TimeInterval& aInterval);
void UpdateBufferedRanges();
void RejectProcessing(nsresult aRejectValue, const char* aName);
void ResolveProcessing(bool aResolveValue, const char* aName);
MediaPromiseRequestHolder<CodedFrameProcessingPromise> mProcessingRequest;
MediaPromiseHolder<CodedFrameProcessingPromise> mProcessingPromise;
MediaPromiseHolder<AppendPromise> mAppendPromise;
// Set to true while SegmentParserLoop is running. This is used for diagnostic
// purposes only. We can't rely on mAppendPromise to be empty as it is only
// cleared in a follow up task.
bool mAppendRunning;
// Trackbuffers definition.
nsTArray<TrackData*> GetTracksList();
TrackData& GetTracksData(TrackType aTrack)
{
switch(aTrack) {
case TrackType::kVideoTrack:
return mVideoTracks;
case TrackType::kAudioTrack:
default:
return mAudioTracks;
}
}
TrackData mVideoTracks;
TrackData mAudioTracks;
// TaskQueue methods and objects.
AbstractThread* GetTaskQueue() {
return mTaskQueue;
}
bool OnTaskQueue()
{
return !GetTaskQueue() || GetTaskQueue()->IsCurrentThreadIn();
}
RefPtr<MediaTaskQueue> mTaskQueue;
TimeInterval mAppendWindow;
TimeUnit mTimestampOffset;
TimeUnit mLastTimestampOffset;
void RestoreCachedVariables();
// Strong references to external objects.
nsRefPtr<dom::SourceBufferAttributes> mSourceBufferAttributes;
nsMainThreadPtrHandle<MediaSourceDecoder> mParentDecoder;
// Set to true if abort is called.
Atomic<bool> mAbort;
// Set to true if mediasource state changed to ended.
Atomic<bool> mEnded;
// Global size of this source buffer content.
Atomic<int64_t> mSizeSourceBuffer;
uint32_t mEvictionThreshold;
Atomic<bool> mEvictionOccurred;
// Monitor to protect following objects accessed across multiple threads.
mutable Monitor mMonitor;
// Stable audio and video track time ranges.
TimeIntervals mVideoBufferedRanges;
TimeIntervals mAudioBufferedRanges;
TimeUnit mOfficialGroupEndTimestamp;
// MediaInfo of the first init segment read.
MediaInfo mInfo;
};
} // namespace mozilla
#endif /* MOZILLA_TRACKBUFFERSMANAGER_H_ */
|