summaryrefslogtreecommitdiffstats
path: root/dom/media/driftcontrol/gtest/TestDriftController.cpp
blob: 132577e44a02943f1e0b97e5314a4fcdb9e13a35 (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
/* -*- 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/. */

#include "gtest/gtest.h"

#include "DriftController.h"
#include "mozilla/Maybe.h"

using namespace mozilla;

TEST(TestDriftController, Basic)
{
  // The buffer level is the only input to the controller logic.
  constexpr uint32_t buffered = 5 * 480;
  constexpr uint32_t bufferedLow = 3 * 480;
  constexpr uint32_t bufferedHigh = 7 * 480;

  DriftController c(48000, 48000, media::TimeUnit::FromSeconds(0.05));
  EXPECT_EQ(c.GetCorrectedSourceRate(), 48000U);

  // The adjustment interval is 1s.
  const auto oneSec = media::TimeUnit(48000, 48000);

  c.UpdateClock(oneSec, oneSec, buffered, 0);
  EXPECT_EQ(c.GetCorrectedSourceRate(), 48000u);

  c.UpdateClock(oneSec, oneSec, bufferedLow, 0);
  EXPECT_EQ(c.GetCorrectedSourceRate(), 47952u);

  c.UpdateClock(oneSec, oneSec, bufferedHigh, 0);
  EXPECT_EQ(c.GetCorrectedSourceRate(), 48000u);

  c.UpdateClock(oneSec, oneSec, bufferedHigh, 0);
  EXPECT_EQ(c.GetCorrectedSourceRate(), 48048u);
}

TEST(TestDriftController, BasicResampler)
{
  // The buffer level is the only input to the controller logic.
  constexpr uint32_t buffered = 5 * 480;
  constexpr uint32_t bufferedLow = 3 * 480;
  constexpr uint32_t bufferedHigh = 7 * 480;

  DriftController c(48000, 24000, media::TimeUnit::FromSeconds(0.05));

  // The adjustment interval is 1s.
  const auto oneSec = media::TimeUnit(48000, 48000);

  c.UpdateClock(oneSec, oneSec, buffered, 0);
  EXPECT_EQ(c.GetCorrectedSourceRate(), 48000u);

  // low
  c.UpdateClock(oneSec, oneSec, bufferedLow, 0);
  EXPECT_EQ(c.GetCorrectedSourceRate(), 47952u);

  // high
  c.UpdateClock(oneSec, oneSec, bufferedHigh, 0);
  EXPECT_EQ(c.GetCorrectedSourceRate(), 48000u);

  // high
  c.UpdateClock(oneSec, oneSec, bufferedHigh, 0);
  EXPECT_EQ(c.GetCorrectedSourceRate(), 48048u);
}

TEST(TestDriftController, BufferedInput)
{
  // The buffer level is the only input to the controller logic.
  constexpr uint32_t buffered = 5 * 480;
  constexpr uint32_t bufferedLow = 3 * 480;
  constexpr uint32_t bufferedHigh = 7 * 480;

  DriftController c(48000, 48000, media::TimeUnit::FromSeconds(0.05));
  EXPECT_EQ(c.GetCorrectedSourceRate(), 48000u);

  // The adjustment interval is 1s.
  const auto oneSec = media::TimeUnit(48000, 48000);

  c.UpdateClock(oneSec, oneSec, buffered, 0);
  EXPECT_EQ(c.GetCorrectedSourceRate(), 48000u);

  // 0 buffered when updating correction
  c.UpdateClock(oneSec, oneSec, 0, 0);
  EXPECT_EQ(c.GetCorrectedSourceRate(), 47952u);

  c.UpdateClock(oneSec, oneSec, bufferedLow, 0);
  EXPECT_EQ(c.GetCorrectedSourceRate(), 48000u);

  c.UpdateClock(oneSec, oneSec, buffered, 0);
  EXPECT_EQ(c.GetCorrectedSourceRate(), 48000u);

  c.UpdateClock(oneSec, oneSec, bufferedHigh, 0);
  EXPECT_EQ(c.GetCorrectedSourceRate(), 48048u);
}

TEST(TestDriftController, BufferedInputWithResampling)
{
  // The buffer level is the only input to the controller logic.
  constexpr uint32_t buffered = 5 * 480;
  constexpr uint32_t bufferedLow = 3 * 480;
  constexpr uint32_t bufferedHigh = 7 * 480;

  DriftController c(48000, 24000, media::TimeUnit::FromSeconds(0.05));
  EXPECT_EQ(c.GetCorrectedSourceRate(), 48000u);

  // The adjustment interval is 1s.
  const auto oneSec = media::TimeUnit(24000, 24000);

  c.UpdateClock(oneSec, oneSec, buffered, 0);
  EXPECT_EQ(c.GetCorrectedSourceRate(), 48000u);

  // 0 buffered when updating correction
  c.UpdateClock(oneSec, oneSec, 0, 0);
  EXPECT_EQ(c.GetCorrectedSourceRate(), 47952u);

  c.UpdateClock(oneSec, oneSec, bufferedLow, 0);
  EXPECT_EQ(c.GetCorrectedSourceRate(), 48000u);

  c.UpdateClock(oneSec, oneSec, buffered, 0);
  EXPECT_EQ(c.GetCorrectedSourceRate(), 48000u);

  c.UpdateClock(oneSec, oneSec, bufferedHigh, 0);
  EXPECT_EQ(c.GetCorrectedSourceRate(), 48048u);
}

TEST(TestDriftController, SmallError)
{
  // The buffer level is the only input to the controller logic.
  constexpr uint32_t buffered = 5 * 480;
  constexpr uint32_t bufferedLow = buffered - 48;
  constexpr uint32_t bufferedHigh = buffered + 48;

  DriftController c(48000, 48000, media::TimeUnit::FromSeconds(0.05));
  EXPECT_EQ(c.GetCorrectedSourceRate(), 48000u);

  // The adjustment interval is 1s.
  const auto oneSec = media::TimeUnit(48000, 48000);

  c.UpdateClock(oneSec, oneSec, buffered, 0);
  EXPECT_EQ(c.GetCorrectedSourceRate(), 48000u);

  c.UpdateClock(oneSec, oneSec, bufferedLow, 0);
  EXPECT_EQ(c.GetCorrectedSourceRate(), 48000u);

  c.UpdateClock(oneSec, oneSec, bufferedHigh, 0);
  EXPECT_EQ(c.GetCorrectedSourceRate(), 48000u);
  c.UpdateClock(oneSec, oneSec, bufferedHigh, 0);
  EXPECT_EQ(c.GetCorrectedSourceRate(), 48000u);
}

TEST(TestDriftController, SmallBufferedFrames)
{
  // The buffer level is the only input to the controller logic.
  constexpr uint32_t bufferedLow = 3 * 480;

  DriftController c(48000, 48000, media::TimeUnit::FromSeconds(0.05));
  media::TimeUnit oneSec = media::TimeUnit::FromSeconds(1);
  media::TimeUnit hundredMillis = oneSec / 10;

  EXPECT_EQ(c.GetCorrectedSourceRate(), 48000U);
  for (uint32_t i = 0; i < 9; ++i) {
    c.UpdateClock(hundredMillis, hundredMillis, bufferedLow, 0);
  }
  EXPECT_EQ(c.GetCorrectedSourceRate(), 48000U);
  c.UpdateClock(hundredMillis, hundredMillis, bufferedLow, 0);
  EXPECT_EQ(c.GetCorrectedSourceRate(), 47952U);
}

TEST(TestDriftController, VerySmallBufferedFrames)
{
  // The buffer level is the only input to the controller logic.
  uint32_t bufferedLow = 1;
  uint32_t nominalRate = 48000;

  DriftController c(nominalRate, nominalRate, media::TimeUnit::FromSeconds(1));
  media::TimeUnit oneSec = media::TimeUnit::FromSeconds(1);
  media::TimeUnit sourceDuration(1, nominalRate);

  EXPECT_EQ(c.GetCorrectedSourceRate(), nominalRate);
  uint32_t previousCorrected = nominalRate;
  // Steps are limited to nominalRate/1000.
  // Perform 1001 steps to check the corrected rate does not underflow zero.
  for (uint32_t i = 0; i < 1001; ++i) {
    c.UpdateClock(sourceDuration, oneSec, bufferedLow, 0);
    uint32_t correctedRate = c.GetCorrectedSourceRate();
    EXPECT_LE(correctedRate, previousCorrected) << "for i=" << i;
    EXPECT_GT(correctedRate, 0u) << "for i=" << i;
    previousCorrected = correctedRate;
  }
}