summaryrefslogtreecommitdiffstats
path: root/dom/media/MediaSegment.h
blob: 181a726c741104f538d9370db97220a81c267d49 (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
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
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
/* 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_MEDIASEGMENT_H_
#define MOZILLA_MEDIASEGMENT_H_

#include "PrincipalHandle.h"
#include "nsTArray.h"
#ifdef MOZILLA_INTERNAL_API
#  include "mozilla/TimeStamp.h"
#endif
#include <algorithm>

namespace mozilla {

/**
 * Track or graph rate in Hz. Maximum 1 << TRACK_RATE_MAX_BITS Hz. This
 * maximum avoids overflow in conversions between track rates and conversions
 * from seconds.
 */
typedef int32_t TrackRate;
const int64_t TRACK_RATE_MAX_BITS = 20;
const TrackRate TRACK_RATE_MAX = 1 << TRACK_RATE_MAX_BITS;

/**
 * A number of ticks at a rate determined by some underlying track (e.g., audio
 * sample rate). We want to make sure that multiplying TrackTicks by a TrackRate
 * doesn't overflow, so we set its max accordingly.
 * TrackTime should be used instead when we're working with MediaTrackGraph's
 * rate, but TrackTicks can be used outside MediaTracks when we have data at a
 * different rate.
 */
typedef int64_t TrackTicks;
const int64_t TRACK_TICKS_MAX = INT64_MAX >> TRACK_RATE_MAX_BITS;

/**
 * We represent media times in 64-bit audio frame counts or ticks.
 * All tracks in a MediaTrackGraph have the same rate.
 */
typedef int64_t MediaTime;
const int64_t MEDIA_TIME_MAX = TRACK_TICKS_MAX;

/**
 * Media time relative to the start of a MediaTrack.
 */
typedef MediaTime TrackTime;
const TrackTime TRACK_TIME_MAX = MEDIA_TIME_MAX;

/**
 * Media time relative to the start of the graph timeline.
 */
typedef MediaTime GraphTime;
const GraphTime GRAPH_TIME_MAX = MEDIA_TIME_MAX;

/* Time conversion helper functions */
inline TrackTicks RateConvertTicksRoundDown(TrackRate aOutRate,
                                            TrackRate aInRate,
                                            TrackTicks aTicks) {
  MOZ_ASSERT(0 < aOutRate && aOutRate <= TRACK_RATE_MAX, "Bad out rate");
  MOZ_ASSERT(0 < aInRate && aInRate <= TRACK_RATE_MAX, "Bad in rate");
  MOZ_ASSERT(0 <= aTicks && aTicks <= TRACK_TICKS_MAX, "Bad ticks");
  return (aTicks * aOutRate) / aInRate;
}

inline TrackTicks RateConvertTicksRoundUp(TrackRate aOutRate, TrackRate aInRate,
                                          TrackTicks aTicks) {
  MOZ_ASSERT(0 < aOutRate && aOutRate <= TRACK_RATE_MAX, "Bad out rate");
  MOZ_ASSERT(0 < aInRate && aInRate <= TRACK_RATE_MAX, "Bad in rate");
  MOZ_ASSERT(0 <= aTicks && aTicks <= TRACK_TICKS_MAX, "Bad ticks");
  return (aTicks * aOutRate + aInRate - 1) / aInRate;
}

/**
 * The number of chunks allocated by default for a MediaSegment.
 * Appending more chunks than this will cause further allocations.
 *
 * 16 is an arbitrary number intended to cover the most common cases in the
 * MediaTrackGraph (1 with silence and 1-2 with data for a realtime track)
 * with some margin.
 */
const size_t DEFAULT_SEGMENT_CAPACITY = 16;

/**
 * A MediaSegment is a chunk of media data sequential in time. Different
 * types of data have different subclasses of MediaSegment, all inheriting
 * from MediaSegmentBase.
 * All MediaSegment data is timed using TrackTime. The actual tick rate
 * is defined on a per-track basis. For some track types, this can be
 * a fixed constant for all tracks of that type (e.g. 1MHz for video).
 *
 * Each media segment defines a concept of "null media data" (e.g. silence
 * for audio or "no video frame" for video), which can be efficiently
 * represented. This is used for padding.
 */
class MediaSegment {
 public:
  MediaSegment(const MediaSegment&) = delete;
  MediaSegment& operator=(const MediaSegment&) = delete;

  MOZ_COUNTED_DTOR_VIRTUAL(MediaSegment)

  enum Type { AUDIO, VIDEO, TYPE_COUNT };

  /**
   * Gets the total duration of the segment.
   */
  TrackTime GetDuration() const { return mDuration; }
  Type GetType() const { return mType; }

  /**
   * Gets the last principal id that was appended to this segment.
   */
  const PrincipalHandle& GetLastPrincipalHandle() const {
    return mLastPrincipalHandle;
  }
  /**
   * Called by the MediaTrackGraph as it appends a chunk with a different
   * principal id than the current one.
   */
  void SetLastPrincipalHandle(PrincipalHandle aLastPrincipalHandle) {
    mLastPrincipalHandle = std::forward<PrincipalHandle>(aLastPrincipalHandle);
  }

  /**
   * Returns true if all chunks in this segment are null.
   */
  virtual bool IsNull() const = 0;

  /**
   * Returns true if this segment contains no chunks.
   */
  virtual bool IsEmpty() const = 0;

  /**
   * Create a MediaSegment of the same type.
   */
  virtual MediaSegment* CreateEmptyClone() const = 0;
  /**
   * Moves contents of aSource to the end of this segment.
   */
  virtual void AppendFrom(MediaSegment* aSource) = 0;
  /**
   * Append a slice of aSource to this segment.
   */
  virtual void AppendSlice(const MediaSegment& aSource, TrackTime aStart,
                           TrackTime aEnd) = 0;
  /**
   * Replace all contents up to aDuration with null data.
   */
  virtual void ForgetUpTo(TrackTime aDuration) = 0;
  /**
   * Forget all data buffered after a given point
   */
  virtual void FlushAfter(TrackTime aNewEnd) = 0;
  /**
   * Insert aDuration of null data at the start of the segment.
   */
  virtual void InsertNullDataAtStart(TrackTime aDuration) = 0;
  /**
   * Insert aDuration of null data at the end of the segment.
   */
  virtual void AppendNullData(TrackTime aDuration) = 0;
  /**
   * Replace contents with disabled (silence/black) data of the same duration
   */
  virtual void ReplaceWithDisabled() = 0;
  /**
   * Replace contents with null data of the same duration
   */
  virtual void ReplaceWithNull() = 0;
  /**
   * Remove all contents, setting duration to 0.
   */
  virtual void Clear() = 0;

  virtual size_t SizeOfExcludingThis(MallocSizeOf aMallocSizeOf) const {
    return 0;
  }

  virtual size_t SizeOfIncludingThis(MallocSizeOf aMallocSizeOf) const {
    return aMallocSizeOf(this) + SizeOfExcludingThis(aMallocSizeOf);
  }

 protected:
  explicit MediaSegment(Type aType)
      : mDuration(0),
        mType(aType),
        mLastPrincipalHandle(PRINCIPAL_HANDLE_NONE) {
    MOZ_COUNT_CTOR(MediaSegment);
  }

  MediaSegment(MediaSegment&& aSegment)
      : mDuration(std::move(aSegment.mDuration)),
        mType(std::move(aSegment.mType)),
        mLastPrincipalHandle(std::move(aSegment.mLastPrincipalHandle)) {
    MOZ_COUNT_CTOR(MediaSegment);
  }

  TrackTime mDuration;  // total of mDurations of all chunks
  Type mType;

  // The latest principal handle that the MediaTrackGraph has processed for
  // this segment.
  PrincipalHandle mLastPrincipalHandle;
};

/**
 * C is the implementation class subclassed from MediaSegmentBase.
 * C must contain a Chunk class.
 */
template <class C, class Chunk>
class MediaSegmentBase : public MediaSegment {
 public:
  bool IsNull() const override {
    for (typename C::ConstChunkIterator iter(*this); !iter.IsEnded();
         iter.Next()) {
      if (!iter->IsNull()) {
        return false;
      }
    }
    return true;
  }
  bool IsEmpty() const override { return mChunks.IsEmpty(); }
  MediaSegment* CreateEmptyClone() const override { return new C(); }
  void AppendFrom(MediaSegment* aSource) override {
    NS_ASSERTION(aSource->GetType() == C::StaticType(), "Wrong type");
    AppendFromInternal(static_cast<C*>(aSource));
  }
  void AppendFrom(C* aSource) { AppendFromInternal(aSource); }
  void AppendSlice(const MediaSegment& aSource, TrackTime aStart,
                   TrackTime aEnd) override {
    NS_ASSERTION(aSource.GetType() == C::StaticType(), "Wrong type");
    AppendSliceInternal(static_cast<const C&>(aSource), aStart, aEnd);
  }
  void AppendSlice(const C& aOther, TrackTime aStart, TrackTime aEnd) {
    AppendSliceInternal(aOther, aStart, aEnd);
  }
  /**
   * Replace the first aDuration ticks with null media data, because the data
   * will not be required again.
   */
  void ForgetUpTo(TrackTime aDuration) override {
    if (mChunks.IsEmpty() || aDuration <= 0) {
      return;
    }
    if (mChunks[0].IsNull()) {
      TrackTime extraToForget =
          std::min(aDuration, mDuration) - mChunks[0].GetDuration();
      if (extraToForget > 0) {
        RemoveLeading(extraToForget, 1);
        mChunks[0].mDuration += extraToForget;
        mDuration += extraToForget;
      }
      return;
    }
    RemoveLeading(aDuration, 0);
    mChunks.InsertElementAt(0)->SetNull(aDuration);
    mDuration += aDuration;
  }
  void FlushAfter(TrackTime aNewEnd) override {
    if (mChunks.IsEmpty()) {
      return;
    }

    if (!aNewEnd) {
      Clear();
    } else if (mChunks[0].IsNull()) {
      TrackTime extraToKeep = aNewEnd - mChunks[0].GetDuration();
      if (extraToKeep < 0) {
        // reduce the size of the Null, get rid of everthing else
        mChunks[0].SetNull(aNewEnd);
        extraToKeep = 0;
      }
      RemoveTrailing(extraToKeep, 1);
    } else {
      if (aNewEnd > mDuration) {
        NS_ASSERTION(aNewEnd <= mDuration, "can't add data in FlushAfter");
        return;
      }
      RemoveTrailing(aNewEnd, 0);
    }
    mDuration = aNewEnd;
  }
  void InsertNullDataAtStart(TrackTime aDuration) override {
    if (aDuration <= 0) {
      return;
    }
    if (!mChunks.IsEmpty() && mChunks[0].IsNull()) {
      mChunks[0].mDuration += aDuration;
    } else {
      mChunks.InsertElementAt(0)->SetNull(aDuration);
    }
    mDuration += aDuration;
  }
  void AppendNullData(TrackTime aDuration) override {
    if (aDuration <= 0) {
      return;
    }
    if (!mChunks.IsEmpty() && mChunks[mChunks.Length() - 1].IsNull()) {
      mChunks[mChunks.Length() - 1].mDuration += aDuration;
    } else {
      mChunks.AppendElement()->SetNull(aDuration);
    }
    mDuration += aDuration;
  }
  void ReplaceWithDisabled() override {
    if (GetType() != AUDIO) {
      MOZ_CRASH("Disabling unknown segment type");
    }
    ReplaceWithNull();
  }
  void ReplaceWithNull() override {
    TrackTime duration = GetDuration();
    Clear();
    AppendNullData(duration);
  }
  void Clear() override {
    mDuration = 0;
    mChunks.ClearAndRetainStorage();
    mChunks.SetCapacity(DEFAULT_SEGMENT_CAPACITY);
  }

  class ChunkIterator {
   public:
    explicit ChunkIterator(MediaSegmentBase<C, Chunk>& aSegment)
        : mSegment(aSegment), mIndex(0) {}
    bool IsEnded() { return mIndex >= mSegment.mChunks.Length(); }
    void Next() { ++mIndex; }
    Chunk& operator*() { return mSegment.mChunks[mIndex]; }
    Chunk* operator->() { return &mSegment.mChunks[mIndex]; }

   private:
    MediaSegmentBase<C, Chunk>& mSegment;
    uint32_t mIndex;
  };
  class ConstChunkIterator {
   public:
    explicit ConstChunkIterator(const MediaSegmentBase<C, Chunk>& aSegment)
        : mSegment(aSegment), mIndex(0) {}
    bool IsEnded() { return mIndex >= mSegment.mChunks.Length(); }
    void Next() { ++mIndex; }
    const Chunk& operator*() { return mSegment.mChunks[mIndex]; }
    const Chunk* operator->() { return &mSegment.mChunks[mIndex]; }

   private:
    const MediaSegmentBase<C, Chunk>& mSegment;
    uint32_t mIndex;
  };

  void RemoveLeading(TrackTime aDuration) { RemoveLeading(aDuration, 0); }

  size_t SizeOfExcludingThis(MallocSizeOf aMallocSizeOf) const override {
    size_t amount = mChunks.ShallowSizeOfExcludingThis(aMallocSizeOf);
    for (size_t i = 0; i < mChunks.Length(); i++) {
      amount += mChunks[i].SizeOfExcludingThisIfUnshared(aMallocSizeOf);
    }
    return amount;
  }

  size_t SizeOfIncludingThis(MallocSizeOf aMallocSizeOf) const override {
    return aMallocSizeOf(this) + SizeOfExcludingThis(aMallocSizeOf);
  }

  Chunk* GetLastChunk() {
    if (mChunks.IsEmpty()) {
      return nullptr;
    }
    return &mChunks[mChunks.Length() - 1];
  }

  const Chunk* GetLastChunk() const {
    if (mChunks.IsEmpty()) {
      return nullptr;
    }
    return &mChunks[mChunks.Length() - 1];
  }

 protected:
  explicit MediaSegmentBase(Type aType) : MediaSegment(aType), mChunks() {}

  MediaSegmentBase(MediaSegmentBase&& aSegment)
      : MediaSegment(std::move(aSegment)),
        mChunks(std::move(aSegment.mChunks)) {
    MOZ_ASSERT(mChunks.Capacity() >= DEFAULT_SEGMENT_CAPACITY,
               "Capacity must be retained in self after swap");
    MOZ_ASSERT(aSegment.mChunks.Capacity() >= DEFAULT_SEGMENT_CAPACITY,
               "Capacity must be retained in other after swap");
  }

  /**
   * Appends the contents of aSource to this segment, clearing aSource.
   */
  void AppendFromInternal(MediaSegmentBase<C, Chunk>* aSource) {
    MOZ_ASSERT(aSource->mDuration >= 0);
    mDuration += aSource->mDuration;
    aSource->mDuration = 0;
    size_t offset = 0;
    if (!mChunks.IsEmpty() && !aSource->mChunks.IsEmpty() &&
        mChunks[mChunks.Length() - 1].CanCombineWithFollowing(
            aSource->mChunks[0])) {
      mChunks[mChunks.Length() - 1].mDuration += aSource->mChunks[0].mDuration;
      offset = 1;
    }

    for (; offset < aSource->mChunks.Length(); ++offset) {
      mChunks.AppendElement(std::move(aSource->mChunks[offset]));
    }

    aSource->mChunks.ClearAndRetainStorage();
    MOZ_ASSERT(aSource->mChunks.Capacity() >= DEFAULT_SEGMENT_CAPACITY,
               "Capacity must be retained after appending from aSource");
  }

  void AppendSliceInternal(const MediaSegmentBase<C, Chunk>& aSource,
                           TrackTime aStart, TrackTime aEnd) {
    MOZ_ASSERT(aStart <= aEnd, "Endpoints inverted");
    NS_ASSERTION(aStart >= 0 && aEnd <= aSource.mDuration,
                 "Slice out of range");
    mDuration += aEnd - aStart;
    TrackTime offset = 0;
    for (uint32_t i = 0; i < aSource.mChunks.Length() && offset < aEnd; ++i) {
      const Chunk& c = aSource.mChunks[i];
      TrackTime start = std::max(aStart, offset);
      TrackTime nextOffset = offset + c.GetDuration();
      TrackTime end = std::min(aEnd, nextOffset);
      if (start < end) {
        if (!mChunks.IsEmpty() &&
            mChunks[mChunks.Length() - 1].CanCombineWithFollowing(c)) {
          MOZ_ASSERT(start - offset >= 0 && end - offset <= aSource.mDuration,
                     "Slice out of bounds");
          mChunks[mChunks.Length() - 1].mDuration += end - start;
        } else {
          mChunks.AppendElement(c)->SliceTo(start - offset, end - offset);
        }
      }
      offset = nextOffset;
    }
  }

  Chunk* AppendChunk(TrackTime aDuration) {
    MOZ_ASSERT(aDuration >= 0);
    Chunk* c = mChunks.AppendElement();
    c->mDuration = aDuration;
    mDuration += aDuration;
    return c;
  }

  void RemoveLeading(TrackTime aDuration, uint32_t aStartIndex) {
    NS_ASSERTION(aDuration >= 0, "Can't remove negative duration");
    TrackTime t = aDuration;
    uint32_t chunksToRemove = 0;
    for (uint32_t i = aStartIndex; i < mChunks.Length() && t > 0; ++i) {
      Chunk* c = &mChunks[i];
      if (c->GetDuration() > t) {
        c->SliceTo(t, c->GetDuration());
        t = 0;
        break;
      }
      t -= c->GetDuration();
      chunksToRemove = i + 1 - aStartIndex;
    }
    if (aStartIndex == 0 && chunksToRemove == mChunks.Length()) {
      mChunks.ClearAndRetainStorage();
    } else {
      mChunks.RemoveElementsAt(aStartIndex, chunksToRemove);
    }
    mDuration -= aDuration - t;

    MOZ_ASSERT(mChunks.Capacity() >= DEFAULT_SEGMENT_CAPACITY,
               "Capacity must be retained after removing chunks");
  }

  void RemoveTrailing(TrackTime aKeep, uint32_t aStartIndex) {
    NS_ASSERTION(aKeep >= 0, "Can't keep negative duration");
    TrackTime t = aKeep;
    uint32_t i;
    for (i = aStartIndex; i < mChunks.Length() && t; ++i) {
      Chunk* c = &mChunks[i];
      if (c->GetDuration() > t) {
        c->SliceTo(0, t);
        break;
      }
      t -= c->GetDuration();
    }
    // At this point `i` is already advanced due to last check in the loop.
    if (i < mChunks.Length()) {
      mChunks.RemoveLastElements(mChunks.Length() - i);
    }
    MOZ_ASSERT(mChunks.Capacity() >= DEFAULT_SEGMENT_CAPACITY,
               "Capacity must be retained after removing chunks");
    // Caller must adjust mDuration
  }

  AutoTArray<Chunk, DEFAULT_SEGMENT_CAPACITY> mChunks;
};

}  // namespace mozilla

#endif /* MOZILLA_MEDIASEGMENT_H_ */