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
|
/*
* Copyright (c) 2012 The WebRTC project authors. All Rights Reserved.
*
* Use of this source code is governed by a BSD-style license
* that can be found in the LICENSE file in the root of the source
* tree. An additional intellectual property rights grant can be found
* in the file PATENTS. All contributing project authors may
* be found in the AUTHORS file in the root of the source tree.
*/
#include "modules/audio_coding/neteq/timestamp_scaler.h"
#include <cstddef>
#include <cstdint>
#include <optional>
#include <utility>
#include "api/audio_codecs/audio_format.h"
#include "api/audio_codecs/builtin_audio_decoder_factory.h"
#include "api/environment/environment.h"
#include "api/environment/environment_factory.h"
#include "modules/audio_coding/neteq/mock/mock_decoder_database.h"
#include "modules/audio_coding/neteq/packet.h"
#include "test/gmock.h"
#include "test/gtest.h"
using ::testing::_;
using ::testing::Return;
using ::testing::ReturnNull;
namespace webrtc {
TEST(TimestampScaler, TestNoScaling) {
const Environment env = CreateEnvironment();
MockDecoderDatabase db;
auto factory = CreateBuiltinAudioDecoderFactory();
// Use PCMu, because it doesn't use scaled timestamps.
const DecoderDatabase::DecoderInfo info(env, SdpAudioFormat("pcmu", 8000, 1),
std::nullopt, factory.get());
static const uint8_t kRtpPayloadType = 0;
EXPECT_CALL(db, GetDecoderInfo(kRtpPayloadType))
.WillRepeatedly(Return(&info));
TimestampScaler scaler(db);
// Test both sides of the timestamp wrap-around.
for (uint32_t timestamp = 0xFFFFFFFF - 5; timestamp != 5; ++timestamp) {
// Scale to internal timestamp.
EXPECT_EQ(timestamp, scaler.ToInternal(timestamp, kRtpPayloadType));
// Scale back.
EXPECT_EQ(timestamp, scaler.ToExternal(timestamp));
}
EXPECT_CALL(db, Die()); // Called when database object is deleted.
}
TEST(TimestampScaler, TestNoScalingLargeStep) {
const Environment env = CreateEnvironment();
MockDecoderDatabase db;
auto factory = CreateBuiltinAudioDecoderFactory();
// Use PCMu, because it doesn't use scaled timestamps.
const DecoderDatabase::DecoderInfo info(env, SdpAudioFormat("pcmu", 8000, 1),
std::nullopt, factory.get());
static const uint8_t kRtpPayloadType = 0;
EXPECT_CALL(db, GetDecoderInfo(kRtpPayloadType))
.WillRepeatedly(Return(&info));
TimestampScaler scaler(db);
// Test both sides of the timestamp wrap-around.
static const uint32_t kStep = 160;
uint32_t start_timestamp = 0;
// `external_timestamp` will be a large positive value.
start_timestamp = start_timestamp - 5 * kStep;
for (uint32_t timestamp = start_timestamp; timestamp != 5 * kStep;
timestamp += kStep) {
// Scale to internal timestamp.
EXPECT_EQ(timestamp, scaler.ToInternal(timestamp, kRtpPayloadType));
// Scale back.
EXPECT_EQ(timestamp, scaler.ToExternal(timestamp));
}
EXPECT_CALL(db, Die()); // Called when database object is deleted.
}
TEST(TimestampScaler, TestG722) {
const Environment env = CreateEnvironment();
MockDecoderDatabase db;
auto factory = CreateBuiltinAudioDecoderFactory();
// Use G722, which has a factor 2 scaling.
const DecoderDatabase::DecoderInfo info(env, SdpAudioFormat("g722", 8000, 1),
std::nullopt, factory.get());
static const uint8_t kRtpPayloadType = 17;
EXPECT_CALL(db, GetDecoderInfo(kRtpPayloadType))
.WillRepeatedly(Return(&info));
TimestampScaler scaler(db);
// Test both sides of the timestamp wrap-around.
uint32_t external_timestamp = 0xFFFFFFFF - 5;
uint32_t internal_timestamp = external_timestamp;
for (; external_timestamp != 5; ++external_timestamp) {
// Scale to internal timestamp.
EXPECT_EQ(internal_timestamp,
scaler.ToInternal(external_timestamp, kRtpPayloadType));
// Scale back.
EXPECT_EQ(external_timestamp, scaler.ToExternal(internal_timestamp));
internal_timestamp += 2;
}
EXPECT_CALL(db, Die()); // Called when database object is deleted.
}
TEST(TimestampScaler, TestG722LargeStep) {
const Environment env = CreateEnvironment();
MockDecoderDatabase db;
auto factory = CreateBuiltinAudioDecoderFactory();
// Use G722, which has a factor 2 scaling.
const DecoderDatabase::DecoderInfo info(env, SdpAudioFormat("g722", 8000, 1),
std::nullopt, factory.get());
static const uint8_t kRtpPayloadType = 17;
EXPECT_CALL(db, GetDecoderInfo(kRtpPayloadType))
.WillRepeatedly(Return(&info));
TimestampScaler scaler(db);
// Test both sides of the timestamp wrap-around.
static const uint32_t kStep = 320;
uint32_t external_timestamp = 0;
// `external_timestamp` will be a large positive value.
external_timestamp = external_timestamp - 5 * kStep;
uint32_t internal_timestamp = external_timestamp;
for (; external_timestamp != 5 * kStep; external_timestamp += kStep) {
// Scale to internal timestamp.
EXPECT_EQ(internal_timestamp,
scaler.ToInternal(external_timestamp, kRtpPayloadType));
// Scale back.
EXPECT_EQ(external_timestamp, scaler.ToExternal(internal_timestamp));
// Internal timestamp should be incremented with twice the step.
internal_timestamp += 2 * kStep;
}
EXPECT_CALL(db, Die()); // Called when database object is deleted.
}
TEST(TimestampScaler, TestG722WithCng) {
const Environment env = CreateEnvironment();
MockDecoderDatabase db;
auto factory = CreateBuiltinAudioDecoderFactory();
// Use G722, which has a factor 2 scaling.
const DecoderDatabase::DecoderInfo info_g722(
env, SdpAudioFormat("g722", 8000, 1), std::nullopt, factory.get());
const DecoderDatabase::DecoderInfo info_cng(
env, SdpAudioFormat("cn", 16000, 1), std::nullopt, factory.get());
static const uint8_t kRtpPayloadTypeG722 = 17;
static const uint8_t kRtpPayloadTypeCng = 13;
EXPECT_CALL(db, GetDecoderInfo(kRtpPayloadTypeG722))
.WillRepeatedly(Return(&info_g722));
EXPECT_CALL(db, GetDecoderInfo(kRtpPayloadTypeCng))
.WillRepeatedly(Return(&info_cng));
TimestampScaler scaler(db);
// Test both sides of the timestamp wrap-around.
uint32_t external_timestamp = 0xFFFFFFFF - 5;
uint32_t internal_timestamp = external_timestamp;
bool next_is_cng = false;
for (; external_timestamp != 5; ++external_timestamp) {
// Alternate between G.722 and CNG every other packet.
if (next_is_cng) {
// Scale to internal timestamp.
EXPECT_EQ(internal_timestamp,
scaler.ToInternal(external_timestamp, kRtpPayloadTypeCng));
next_is_cng = false;
} else {
// Scale to internal timestamp.
EXPECT_EQ(internal_timestamp,
scaler.ToInternal(external_timestamp, kRtpPayloadTypeG722));
next_is_cng = true;
}
// Scale back.
EXPECT_EQ(external_timestamp, scaler.ToExternal(internal_timestamp));
internal_timestamp += 2;
}
EXPECT_CALL(db, Die()); // Called when database object is deleted.
}
// Make sure that the method ToInternal(Packet* packet) is wired up correctly.
// Since it is simply calling the other ToInternal method, we are not doing
// as many tests here.
TEST(TimestampScaler, TestG722Packet) {
const Environment env = CreateEnvironment();
MockDecoderDatabase db;
auto factory = CreateBuiltinAudioDecoderFactory();
// Use G722, which has a factor 2 scaling.
const DecoderDatabase::DecoderInfo info(env, SdpAudioFormat("g722", 8000, 1),
std::nullopt, factory.get());
static const uint8_t kRtpPayloadType = 17;
EXPECT_CALL(db, GetDecoderInfo(kRtpPayloadType))
.WillRepeatedly(Return(&info));
TimestampScaler scaler(db);
// Test both sides of the timestamp wrap-around.
uint32_t external_timestamp = 0xFFFFFFFF - 5;
uint32_t internal_timestamp = external_timestamp;
Packet packet;
packet.payload_type = kRtpPayloadType;
for (; external_timestamp != 5; ++external_timestamp) {
packet.timestamp = external_timestamp;
// Scale to internal timestamp.
scaler.ToInternal(&packet);
EXPECT_EQ(internal_timestamp, packet.timestamp);
internal_timestamp += 2;
}
EXPECT_CALL(db, Die()); // Called when database object is deleted.
}
// Make sure that the method ToInternal(PacketList* packet_list) is wired up
// correctly. Since it is simply calling the ToInternal(Packet* packet) method,
// we are not doing as many tests here.
TEST(TimestampScaler, TestG722PacketList) {
const Environment env = CreateEnvironment();
MockDecoderDatabase db;
auto factory = CreateBuiltinAudioDecoderFactory();
// Use G722, which has a factor 2 scaling.
const DecoderDatabase::DecoderInfo info(env, SdpAudioFormat("g722", 8000, 1),
std::nullopt, factory.get());
static const uint8_t kRtpPayloadType = 17;
EXPECT_CALL(db, GetDecoderInfo(kRtpPayloadType))
.WillRepeatedly(Return(&info));
TimestampScaler scaler(db);
// Test both sides of the timestamp wrap-around.
uint32_t external_timestamp = 0xFFFFFFFF - 5;
uint32_t internal_timestamp = external_timestamp;
PacketList packet_list;
{
Packet packet1;
packet1.payload_type = kRtpPayloadType;
packet1.timestamp = external_timestamp;
Packet packet2;
packet2.payload_type = kRtpPayloadType;
packet2.timestamp = external_timestamp + 10;
packet_list.push_back(std::move(packet1));
packet_list.push_back(std::move(packet2));
}
scaler.ToInternal(&packet_list);
EXPECT_EQ(internal_timestamp, packet_list.front().timestamp);
packet_list.pop_front();
EXPECT_EQ(internal_timestamp + 20, packet_list.front().timestamp);
EXPECT_CALL(db, Die()); // Called when database object is deleted.
}
TEST(TimestampScaler, TestG722Reset) {
const Environment env = CreateEnvironment();
MockDecoderDatabase db;
auto factory = CreateBuiltinAudioDecoderFactory();
// Use G722, which has a factor 2 scaling.
const DecoderDatabase::DecoderInfo info(env, SdpAudioFormat("g722", 8000, 1),
std::nullopt, factory.get());
static const uint8_t kRtpPayloadType = 17;
EXPECT_CALL(db, GetDecoderInfo(kRtpPayloadType))
.WillRepeatedly(Return(&info));
TimestampScaler scaler(db);
// Test both sides of the timestamp wrap-around.
uint32_t external_timestamp = 0xFFFFFFFF - 5;
uint32_t internal_timestamp = external_timestamp;
for (; external_timestamp != 5; ++external_timestamp) {
// Scale to internal timestamp.
EXPECT_EQ(internal_timestamp,
scaler.ToInternal(external_timestamp, kRtpPayloadType));
// Scale back.
EXPECT_EQ(external_timestamp, scaler.ToExternal(internal_timestamp));
internal_timestamp += 2;
}
// Reset the scaler. After this, we expect the internal and external to start
// over at the same value again.
scaler.Reset();
internal_timestamp = external_timestamp;
for (; external_timestamp != 15; ++external_timestamp) {
// Scale to internal timestamp.
EXPECT_EQ(internal_timestamp,
scaler.ToInternal(external_timestamp, kRtpPayloadType));
// Scale back.
EXPECT_EQ(external_timestamp, scaler.ToExternal(internal_timestamp));
internal_timestamp += 2;
}
EXPECT_CALL(db, Die()); // Called when database object is deleted.
}
// TODO(minyue): This test becomes trivial since Opus does not need a timestamp
// scaler. Therefore, this test may be removed in future. There is no harm to
// keep it, since it can be taken as a test case for the situation of a trivial
// timestamp scaler.
TEST(TimestampScaler, TestOpusLargeStep) {
const Environment env = CreateEnvironment();
MockDecoderDatabase db;
auto factory = CreateBuiltinAudioDecoderFactory();
const DecoderDatabase::DecoderInfo info(env, SdpAudioFormat("opus", 48000, 2),
std::nullopt, factory.get());
static const uint8_t kRtpPayloadType = 17;
EXPECT_CALL(db, GetDecoderInfo(kRtpPayloadType))
.WillRepeatedly(Return(&info));
TimestampScaler scaler(db);
// Test both sides of the timestamp wrap-around.
static const uint32_t kStep = 960;
uint32_t external_timestamp = 0;
// `external_timestamp` will be a large positive value.
external_timestamp = external_timestamp - 5 * kStep;
uint32_t internal_timestamp = external_timestamp;
for (; external_timestamp != 5 * kStep; external_timestamp += kStep) {
// Scale to internal timestamp.
EXPECT_EQ(internal_timestamp,
scaler.ToInternal(external_timestamp, kRtpPayloadType));
// Scale back.
EXPECT_EQ(external_timestamp, scaler.ToExternal(internal_timestamp));
internal_timestamp += kStep;
}
EXPECT_CALL(db, Die()); // Called when database object is deleted.
}
TEST(TimestampScaler, Failures) {
static const uint8_t kRtpPayloadType = 17;
MockDecoderDatabase db;
EXPECT_CALL(db, GetDecoderInfo(kRtpPayloadType))
.WillOnce(ReturnNull()); // Return NULL to indicate unknown payload type.
TimestampScaler scaler(db);
uint32_t timestamp = 4711; // Some number.
EXPECT_EQ(timestamp, scaler.ToInternal(timestamp, kRtpPayloadType));
Packet* packet = nullptr;
scaler.ToInternal(packet); // Should not crash. That's all we can test.
EXPECT_CALL(db, Die()); // Called when database object is deleted.
}
} // namespace webrtc
|