summaryrefslogtreecommitdiffstats
path: root/dom/media/MediaQueue.h
blob: 9c4c829c87446411dcd6df9f452ffae3b35ccfa0 (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
/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
/* vim:set ts=2 sw=2 sts=2 et cindent: */
/* 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/. */
#if !defined(MediaQueue_h_)
#  define MediaQueue_h_

#  include <type_traits>

#  include "mozilla/RecursiveMutex.h"
#  include "mozilla/TaskQueue.h"

#  include "nsDeque.h"
#  include "MediaEventSource.h"
#  include "TimeUnits.h"

namespace mozilla {

extern LazyLogModule gMediaDecoderLog;

#  define QLOG(msg, ...)                       \
    MOZ_LOG(gMediaDecoderLog, LogLevel::Debug, \
            ("MediaQueue=%p " msg, this, ##__VA_ARGS__))

class AudioData;
class VideoData;
class EncodedFrame;

template <typename T>
struct TimestampAdjustmentTrait {
  static const bool mValue = false;
};

template <>
struct TimestampAdjustmentTrait<AudioData> {
  static const bool mValue = true;
};

template <>
struct TimestampAdjustmentTrait<VideoData> {
  static const bool mValue = true;
};

template <typename T>
struct NonTimestampAdjustmentTrait {
  static const bool mValue = !TimestampAdjustmentTrait<T>::mValue;
};

template <typename T>
struct DurationTypeTrait {
  using type = media::TimeUnit;
};

template <>
struct DurationTypeTrait<EncodedFrame> {
  using type = uint64_t;
};

template <class T>
class MediaQueue : private nsRefPtrDeque<T> {
 public:
  explicit MediaQueue(bool aEnablePreciseDuration = false)
      : nsRefPtrDeque<T>(),
        mRecursiveMutex("mediaqueue"),
        mEndOfStream(false),
        mEnablePreciseDuration(aEnablePreciseDuration) {}

  ~MediaQueue() { Reset(); }

  inline size_t GetSize() const {
    RecursiveMutexAutoLock lock(mRecursiveMutex);
    return nsRefPtrDeque<T>::GetSize();
  }

  template <typename U,
            std::enable_if_t<TimestampAdjustmentTrait<U>::mValue, bool> = true>
  inline void AdjustTimeStampIfNeeded(U* aItem) {
    static_assert(std::is_same_v<U, AudioData> || std::is_same_v<U, VideoData>);
    if (mOffset != media::TimeUnit::Zero()) {
      const auto prev = aItem->mTime, prevEndTime = aItem->GetEndTime();
      aItem->mTime += mOffset;
      if (!aItem->mTime.IsValid()) {
        NS_WARNING("Reverting timestamp adjustment due to sample overflow!");
        aItem->mTime = prev;
      } else {
        QLOG("adjusted %s sample [%" PRId64 ",%" PRId64 "] -> [%" PRId64
             ",%" PRId64 "]",
             std::is_same_v<U, AudioData> ? "audio" : "video",
             prev.ToMicroseconds(), prevEndTime.ToMicroseconds(),
             aItem->mTime.ToMicroseconds(),
             aItem->GetEndTime().ToMicroseconds());
      }
    }
  }

  template <typename U, std::enable_if_t<NonTimestampAdjustmentTrait<U>::mValue,
                                         bool> = true>
  inline void AdjustTimeStampIfNeeded(U* aItem) {}

  enum class TimestampAdjustment {
    Enable,
    Disable,
  };
  inline void PushFront(
      T* aItem, TimestampAdjustment aIsEnabled = TimestampAdjustment::Enable) {
    RecursiveMutexAutoLock lock(mRecursiveMutex);
    if (aIsEnabled == TimestampAdjustment::Enable) {
      AdjustTimeStampIfNeeded(aItem);
    }
    nsRefPtrDeque<T>::PushFront(aItem);
    AddDurationToPreciseDuration(aItem);
  }

  inline void Push(T* aItem) {
    MOZ_DIAGNOSTIC_ASSERT(aItem);
    Push(do_AddRef(aItem));
  }

  inline void Push(already_AddRefed<T> aItem) {
    RecursiveMutexAutoLock lock(mRecursiveMutex);
    T* item = aItem.take();

    MOZ_DIAGNOSTIC_ASSERT(item);
    MOZ_DIAGNOSTIC_ASSERT(item->GetEndTime() >= item->mTime);
    AdjustTimeStampIfNeeded(item);
    nsRefPtrDeque<T>::Push(dont_AddRef(item));
    AddDurationToPreciseDuration(item);
    mPushEvent.Notify(RefPtr<T>(item));

    // Pushing new data after queue has ended means that the stream is active
    // again, so we should not mark it as ended.
    if (mEndOfStream) {
      mEndOfStream = false;
    }
  }

  inline already_AddRefed<T> PopFront() {
    RecursiveMutexAutoLock lock(mRecursiveMutex);
    RefPtr<T> rv = nsRefPtrDeque<T>::PopFront();
    if (rv) {
      MOZ_DIAGNOSTIC_ASSERT(rv->GetEndTime() >= rv->mTime);
      SubtractDurationFromPreciseDuration(rv);
      mPopFrontEvent.Notify(RefPtr<T>(rv));
    }
    return rv.forget();
  }

  inline already_AddRefed<T> PopBack() {
    RecursiveMutexAutoLock lock(mRecursiveMutex);
    RefPtr<T> rv = nsRefPtrDeque<T>::Pop();
    if (rv) {
      MOZ_DIAGNOSTIC_ASSERT(rv->GetEndTime() >= rv->mTime);
      SubtractDurationFromPreciseDuration(rv);
    }
    return rv.forget();
  }

  inline RefPtr<T> PeekFront() const {
    RecursiveMutexAutoLock lock(mRecursiveMutex);
    return nsRefPtrDeque<T>::PeekFront();
  }

  inline RefPtr<T> PeekBack() const {
    RecursiveMutexAutoLock lock(mRecursiveMutex);
    return nsRefPtrDeque<T>::Peek();
  }

  void Reset() {
    RecursiveMutexAutoLock lock(mRecursiveMutex);
    nsRefPtrDeque<T>::Erase();
    SetOffset(media::TimeUnit::Zero());
    mEndOfStream = false;
    ResetPreciseDuration();
  }

  bool AtEndOfStream() const {
    RecursiveMutexAutoLock lock(mRecursiveMutex);
    return GetSize() == 0 && mEndOfStream;
  }

  // Returns true if the media queue has had its last item added to it.
  // This happens when the media stream has been completely decoded. Note this
  // does not mean that the corresponding stream has finished playback.
  bool IsFinished() const {
    RecursiveMutexAutoLock lock(mRecursiveMutex);
    return mEndOfStream;
  }

  // Informs the media queue that it won't be receiving any more items.
  void Finish() {
    RecursiveMutexAutoLock lock(mRecursiveMutex);
    if (!mEndOfStream) {
      mEndOfStream = true;
      mFinishEvent.Notify();
    }
  }

  // Returns the approximate number of microseconds of items in the queue.
  int64_t Duration() const {
    RecursiveMutexAutoLock lock(mRecursiveMutex);
    if (GetSize() == 0) {
      return 0;
    }
    T* last = nsRefPtrDeque<T>::Peek();
    T* first = nsRefPtrDeque<T>::PeekFront();
    return (last->GetEndTime() - first->mTime).ToMicroseconds();
  }

  // Return a precise duration if the feature is enabled. Otherwise, return -1.
  int64_t PreciseDuration() const {
    RecursiveMutexAutoLock lock(mRecursiveMutex);
    return GetPreciseDuration();
  }

  void LockedForEach(nsDequeFunctor<T>& aFunctor) const {
    RecursiveMutexAutoLock lock(mRecursiveMutex);
    nsRefPtrDeque<T>::ForEach(aFunctor);
  }

  // Fill aResult with the elements which end later than the given time aTime.
  void GetElementsAfter(const media::TimeUnit& aTime,
                        nsTArray<RefPtr<T>>* aResult) {
    GetElementsAfterStrict(aTime.ToMicroseconds(), aResult);
  }

  void GetFirstElements(uint32_t aMaxElements, nsTArray<RefPtr<T>>* aResult) {
    RecursiveMutexAutoLock lock(mRecursiveMutex);
    for (size_t i = 0; i < aMaxElements && i < GetSize(); ++i) {
      *aResult->AppendElement() = nsRefPtrDeque<T>::ObjectAt(i);
    }
  }

  uint32_t AudioFramesCount() {
    static_assert(std::is_same_v<T, AudioData>,
                  "Only usable with MediaQueue<AudioData>");
    RecursiveMutexAutoLock lock(mRecursiveMutex);
    uint32_t frames = 0;
    for (size_t i = 0; i < GetSize(); ++i) {
      T* v = nsRefPtrDeque<T>::ObjectAt(i);
      frames += v->Frames();
    }
    return frames;
  }

  bool SetOffset(const media::TimeUnit& aOffset) {
    if (!aOffset.IsValid()) {
      QLOG("Invalid offset!");
      return false;
    }
    RecursiveMutexAutoLock lock(mRecursiveMutex);
    mOffset = aOffset;
    QLOG("Set media queue offset %" PRId64, mOffset.ToMicroseconds());
    return true;
  }

  media::TimeUnit GetOffset() const {
    RecursiveMutexAutoLock lock(mRecursiveMutex);
    return mOffset;
  }

  MediaEventSource<RefPtr<T>>& PopFrontEvent() { return mPopFrontEvent; }

  MediaEventSource<RefPtr<T>>& PushEvent() { return mPushEvent; }

  MediaEventSource<void>& FinishEvent() { return mFinishEvent; }

 private:
  // Extracts elements from the queue into aResult, in order.
  // Elements whose end time is before or equal to aTime are ignored.
  void GetElementsAfterStrict(int64_t aTime, nsTArray<RefPtr<T>>* aResult) {
    RecursiveMutexAutoLock lock(mRecursiveMutex);
    if (GetSize() == 0) return;
    size_t i;
    for (i = GetSize() - 1; i > 0; --i) {
      T* v = nsRefPtrDeque<T>::ObjectAt(i);
      if (v->GetEndTime().ToMicroseconds() < aTime) break;
    }
    for (; i < GetSize(); ++i) {
      RefPtr<T> elem = nsRefPtrDeque<T>::ObjectAt(i);
      if (elem->GetEndTime().ToMicroseconds() > aTime) {
        aResult->AppendElement(elem);
      }
    }
  }

  mutable RecursiveMutex mRecursiveMutex MOZ_UNANNOTATED;
  MediaEventProducer<RefPtr<T>> mPopFrontEvent;
  MediaEventProducer<RefPtr<T>> mPushEvent;
  MediaEventProducer<void> mFinishEvent;
  // True when we've decoded the last frame of data in the
  // bitstream for which we're queueing frame data.
  bool mEndOfStream;
  // This offset will be added to any data pushed into the queue. We use it when
  // the media queue starts receiving looped data, which timestamp needs to be
  // modified.
  media::TimeUnit mOffset;

  inline void AddDurationToPreciseDuration(T* aItem) {
    if (!mEnablePreciseDuration) {
      return;
    }
    if constexpr (std::is_same_v<typename DurationTypeTrait<T>::type,
                                 media::TimeUnit> ||
                  std::is_same_v<typename DurationTypeTrait<T>::type,
                                 uint64_t>) {
      mPreciseDuration += aItem->mDuration;
    }
  }

  inline void SubtractDurationFromPreciseDuration(T* aItem) {
    if (!mEnablePreciseDuration) {
      return;
    }
    if constexpr (std::is_same_v<typename DurationTypeTrait<T>::type,
                                 media::TimeUnit> ||
                  std::is_same_v<typename DurationTypeTrait<T>::type,
                                 uint64_t>) {
      mPreciseDuration -= aItem->mDuration;
    }
  }

  inline void ResetPreciseDuration() {
    if (!mEnablePreciseDuration) {
      return;
    }
    if constexpr (std::is_same_v<typename DurationTypeTrait<T>::type,
                                 media::TimeUnit>) {
      mPreciseDuration = media::TimeUnit::Zero();
    } else if constexpr (std::is_same_v<typename DurationTypeTrait<T>::type,
                                        uint64_t>) {
      mPreciseDuration = 0;
    }
  }

  inline int64_t GetPreciseDuration() const {
    if (mEnablePreciseDuration) {
      if constexpr (std::is_same_v<typename DurationTypeTrait<T>::type,
                                   media::TimeUnit>) {
        return mPreciseDuration.ToMicroseconds();
      } else if constexpr (std::is_same_v<typename DurationTypeTrait<T>::type,
                                          uint64_t>) {
        return mPreciseDuration;
      }
    }
    return -1;
  }

  typename DurationTypeTrait<T>::type mPreciseDuration;
  const bool mEnablePreciseDuration = false;
};

}  // namespace mozilla

#  undef QLOG

#endif