summaryrefslogtreecommitdiffstats
path: root/dom/media/gtest/TestMediaQueue.cpp
blob: 5b049dc7fe73033b2ca2f11d29582b58c33bdfec (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
/* 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/. */

#include <gtest/gtest.h>

#include "MediaData.h"
#include "MediaQueue.h"

using namespace mozilla;
using mozilla::media::TimeUnit;

MediaData* CreateDataRawPtr(
    int64_t aStartTime, int64_t aEndTime,
    MediaData::Type aType = MediaData::Type::NULL_DATA) {
  const TimeUnit startTime = TimeUnit::FromMicroseconds(aStartTime);
  const TimeUnit endTime = TimeUnit::FromMicroseconds(aEndTime);
  MediaData* data;
  if (aType == MediaData::Type::AUDIO_DATA) {
    AlignedAudioBuffer samples;
    data = new AudioData(0, startTime, std::move(samples), 2, 44100);
    data->mDuration = endTime - startTime;
  } else if (aType == MediaData::Type::VIDEO_DATA) {
    data = new VideoData(0, startTime, endTime - startTime, true, startTime,
                         gfx::IntSize(), 0);
  } else {
    data = new NullData(0, startTime, endTime - startTime);
  }
  return data;
}

already_AddRefed<MediaData> CreateData(int64_t aStartTime, int64_t aEndTime) {
  RefPtr<MediaData> data = CreateDataRawPtr(aStartTime, aEndTime);
  return data.forget();
}

// Used to avoid the compile error `comparison of integers of different signs`
// when comparing 'const unsigned long' and 'const int'.
#define EXPECT_EQUAL_SIZE_T(lhs, rhs) EXPECT_EQ(size_t(lhs), size_t(rhs))

TEST(MediaQueue, BasicPopOperations)
{
  MediaQueue<MediaData> queue;
  EXPECT_EQUAL_SIZE_T(queue.GetSize(), 0);

  // Test only one element
  const RefPtr<MediaData> data = CreateDataRawPtr(0, 10);
  queue.Push(data.get());
  EXPECT_EQUAL_SIZE_T(queue.GetSize(), 1);

  RefPtr<MediaData> rv = queue.PopFront();
  EXPECT_EQ(rv, data);

  queue.Push(data.get());
  EXPECT_EQUAL_SIZE_T(queue.GetSize(), 1);

  rv = queue.PopBack();
  EXPECT_EQ(rv, data);
  EXPECT_EQUAL_SIZE_T(queue.GetSize(), 0);

  // Test multiple elements
  const RefPtr<MediaData> data1 = CreateDataRawPtr(0, 10);
  const RefPtr<MediaData> data2 = CreateDataRawPtr(11, 20);
  const RefPtr<MediaData> data3 = CreateDataRawPtr(21, 30);
  queue.Push(data1.get());
  queue.Push(data2.get());
  queue.Push(data3.get());
  EXPECT_EQUAL_SIZE_T(queue.GetSize(), 3);

  rv = queue.PopFront();
  EXPECT_EQUAL_SIZE_T(queue.GetSize(), 2);
  EXPECT_EQ(rv, data1);

  rv = queue.PopBack();
  EXPECT_EQUAL_SIZE_T(queue.GetSize(), 1);
  EXPECT_EQ(rv, data3);

  rv = queue.PopBack();
  EXPECT_EQUAL_SIZE_T(queue.GetSize(), 0);
  EXPECT_EQ(rv, data2);
}

TEST(MediaQueue, BasicPeekOperations)
{
  MediaQueue<MediaData> queue;
  EXPECT_EQUAL_SIZE_T(queue.GetSize(), 0);

  // Test only one element
  const RefPtr<MediaData> data1 = CreateDataRawPtr(0, 10);
  queue.Push(data1.get());
  EXPECT_EQUAL_SIZE_T(queue.GetSize(), 1);

  RefPtr<MediaData> rv = queue.PeekFront();
  EXPECT_EQUAL_SIZE_T(queue.GetSize(), 1);
  EXPECT_EQ(rv, data1);

  rv = queue.PeekBack();
  EXPECT_EQUAL_SIZE_T(queue.GetSize(), 1);
  EXPECT_EQ(rv, data1);

  // Test multiple elements
  const RefPtr<MediaData> data2 = CreateDataRawPtr(11, 20);
  const RefPtr<MediaData> data3 = CreateDataRawPtr(21, 30);
  queue.Push(data2.get());
  queue.Push(data3.get());
  EXPECT_EQUAL_SIZE_T(queue.GetSize(), 3);

  rv = queue.PeekFront();
  EXPECT_EQUAL_SIZE_T(queue.GetSize(), 3);
  EXPECT_EQ(rv, data1);

  rv = queue.PeekBack();
  EXPECT_EQUAL_SIZE_T(queue.GetSize(), 3);
  EXPECT_EQ(rv, data3);
}

TEST(MediaQueue, FinishQueue)
{
  MediaQueue<MediaData> queue;
  EXPECT_FALSE(queue.IsFinished());

  queue.Finish();
  EXPECT_TRUE(queue.IsFinished());
}

TEST(MediaQueue, EndOfStream)
{
  MediaQueue<MediaData> queue;
  EXPECT_FALSE(queue.IsFinished());

  queue.Push(CreateData(0, 10));
  EXPECT_EQUAL_SIZE_T(queue.GetSize(), 1);

  queue.Finish();
  EXPECT_EQUAL_SIZE_T(queue.GetSize(), 1);
  EXPECT_TRUE(queue.IsFinished());
  EXPECT_FALSE(queue.AtEndOfStream());

  RefPtr<MediaData> rv = queue.PopFront();
  EXPECT_EQUAL_SIZE_T(queue.GetSize(), 0);
  EXPECT_TRUE(queue.IsFinished());
  EXPECT_TRUE(queue.AtEndOfStream());
}

TEST(MediaQueue, QueueDuration)
{
  MediaQueue<MediaData> queue;
  EXPECT_EQUAL_SIZE_T(queue.GetSize(), 0);

  queue.Push(CreateData(0, 10));
  queue.Push(CreateData(11, 20));
  queue.Push(CreateData(21, 30));
  EXPECT_EQUAL_SIZE_T(queue.GetSize(), 3);

  const int64_t rv = queue.Duration();
  EXPECT_EQ(rv, 30);
}

TEST(MediaQueue, CallGetElementAfterOnSingleElement)
{
  MediaQueue<MediaData> queue;
  queue.Push(CreateData(0, 10));

  // Target time is earlier than data's end time
  TimeUnit targetTime = TimeUnit::FromMicroseconds(5);
  nsTArray<RefPtr<MediaData>> foundResult;
  queue.GetElementsAfter(targetTime, &foundResult);
  EXPECT_EQUAL_SIZE_T(foundResult.Length(), 1);
  EXPECT_TRUE(foundResult[0]->GetEndTime() > targetTime);

  // Target time is later than data's end time
  targetTime = TimeUnit::FromMicroseconds(15);
  nsTArray<RefPtr<MediaData>> emptyResult;
  queue.GetElementsAfter(targetTime, &emptyResult);
  EXPECT_TRUE(emptyResult.IsEmpty());
}

TEST(MediaQueue, CallGetElementAfterOnMultipleElements)
{
  MediaQueue<MediaData> queue;
  queue.Push(CreateData(0, 10));
  queue.Push(CreateData(11, 20));
  queue.Push(CreateData(21, 30));
  queue.Push(CreateData(31, 40));
  queue.Push(CreateData(41, 50));

  // Should find [21,30], [31,40] and [41,50]
  TimeUnit targetTime = TimeUnit::FromMicroseconds(25);
  nsTArray<RefPtr<MediaData>> foundResult;
  queue.GetElementsAfter(targetTime, &foundResult);
  EXPECT_EQUAL_SIZE_T(foundResult.Length(), 3);
  for (const auto& data : foundResult) {
    EXPECT_TRUE(data->GetEndTime() > targetTime);
  }

  // Should find [31,40] and [41,50]
  targetTime = TimeUnit::FromMicroseconds(30);
  foundResult.Clear();
  queue.GetElementsAfter(targetTime, &foundResult);
  EXPECT_EQUAL_SIZE_T(foundResult.Length(), 2);
  for (const auto& data : foundResult) {
    EXPECT_TRUE(data->GetEndTime() > targetTime);
  }

  // Should find no data.
  targetTime = TimeUnit::FromMicroseconds(60);
  nsTArray<RefPtr<MediaData>> emptyResult;
  queue.GetElementsAfter(targetTime, &emptyResult);
  EXPECT_TRUE(emptyResult.IsEmpty());
}

TEST(MediaQueue, TimestampAdjustmentForSupportDataType)
{
  const size_t kOffSet = 30;
  {
    MediaQueue<AudioData> audioQueue;
    audioQueue.Push(
        CreateDataRawPtr(0, 10, MediaData::Type::AUDIO_DATA)->As<AudioData>());
    audioQueue.SetOffset(TimeUnit::FromMicroseconds(kOffSet));
    audioQueue.Push(
        CreateDataRawPtr(0, 10, MediaData::Type::AUDIO_DATA)->As<AudioData>());

    // Data stored before setting the offset shouldn't be changed
    RefPtr<AudioData> data = audioQueue.PopFront();
    EXPECT_EQ(data->mTime, TimeUnit::FromMicroseconds(0));
    EXPECT_EQ(data->GetEndTime(), TimeUnit::FromMicroseconds(10));

    // Data stored after setting the offset should be changed
    data = audioQueue.PopFront();
    EXPECT_EQ(data->mTime, TimeUnit::FromMicroseconds(0 + kOffSet));
    EXPECT_EQ(data->GetEndTime(), TimeUnit::FromMicroseconds(10 + kOffSet));

    // Reset will clean the offset.
    audioQueue.Reset();
    audioQueue.Push(
        CreateDataRawPtr(0, 10, MediaData::Type::AUDIO_DATA)->As<AudioData>());
    data = audioQueue.PopFront();
    EXPECT_EQ(data->mTime, TimeUnit::FromMicroseconds(0));
    EXPECT_EQ(data->GetEndTime(), TimeUnit::FromMicroseconds(10));
  }

  // Check another supported type
  MediaQueue<VideoData> videoQueue;
  videoQueue.Push(
      CreateDataRawPtr(0, 10, MediaData::Type::VIDEO_DATA)->As<VideoData>());
  videoQueue.SetOffset(TimeUnit::FromMicroseconds(kOffSet));
  videoQueue.Push(
      CreateDataRawPtr(0, 10, MediaData::Type::VIDEO_DATA)->As<VideoData>());

  // Data stored before setting the offset shouldn't be changed
  RefPtr<VideoData> data = videoQueue.PopFront();
  EXPECT_EQ(data->mTime, TimeUnit::FromMicroseconds(0));
  EXPECT_EQ(data->GetEndTime(), TimeUnit::FromMicroseconds(10));

  // Data stored after setting the offset should be changed
  data = videoQueue.PopFront();
  EXPECT_EQ(data->mTime, TimeUnit::FromMicroseconds(0 + kOffSet));
  EXPECT_EQ(data->GetEndTime(), TimeUnit::FromMicroseconds(10 + kOffSet));

  // Reset will clean the offset.
  videoQueue.Reset();
  videoQueue.Push(
      CreateDataRawPtr(0, 10, MediaData::Type::VIDEO_DATA)->As<VideoData>());
  data = videoQueue.PopFront();
  EXPECT_EQ(data->mTime, TimeUnit::FromMicroseconds(0));
  EXPECT_EQ(data->GetEndTime(), TimeUnit::FromMicroseconds(10));
}

TEST(MediaQueue, TimestampAdjustmentForNotSupportDataType)
{
  const size_t kOffSet = 30;

  MediaQueue<MediaData> queue;
  queue.Push(CreateDataRawPtr(0, 10));
  queue.SetOffset(TimeUnit::FromMicroseconds(kOffSet));
  queue.Push(CreateDataRawPtr(0, 10));

  // Offset won't affect any data at all.
  RefPtr<MediaData> data = queue.PopFront();
  EXPECT_EQ(data->mTime, TimeUnit::FromMicroseconds(0));
  EXPECT_EQ(data->GetEndTime(), TimeUnit::FromMicroseconds(10));

  data = queue.PopFront();
  EXPECT_EQ(data->mTime, TimeUnit::FromMicroseconds(0));
  EXPECT_EQ(data->GetEndTime(), TimeUnit::FromMicroseconds(10));
}

#undef EXPECT_EQUAL_SIZE_T