henrik.lundin@webrtc.org | d94659d | 2013-01-29 12:09:21 +0000 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (c) 2012 The WebRTC project authors. All Rights Reserved. |
| 3 | * |
| 4 | * Use of this source code is governed by a BSD-style license |
| 5 | * that can be found in the LICENSE file in the root of the source |
| 6 | * tree. An additional intellectual property rights grant can be found |
| 7 | * in the file PATENTS. All contributing project authors may |
| 8 | * be found in the AUTHORS file in the root of the source tree. |
| 9 | */ |
| 10 | |
Mirko Bonadei | 92ea95e | 2017-09-15 06:47:31 +0200 | [diff] [blame^] | 11 | #include "api/audio_codecs/builtin_audio_decoder_factory.h" |
| 12 | #include "modules/audio_coding/neteq/mock/mock_decoder_database.h" |
| 13 | #include "modules/audio_coding/neteq/packet.h" |
| 14 | #include "modules/audio_coding/neteq/timestamp_scaler.h" |
| 15 | #include "test/gmock.h" |
| 16 | #include "test/gtest.h" |
henrik.lundin@webrtc.org | d94659d | 2013-01-29 12:09:21 +0000 | [diff] [blame] | 17 | |
| 18 | using ::testing::Return; |
| 19 | using ::testing::ReturnNull; |
| 20 | using ::testing::_; |
| 21 | |
| 22 | namespace webrtc { |
| 23 | |
| 24 | TEST(TimestampScaler, TestNoScaling) { |
| 25 | MockDecoderDatabase db; |
ossu | 09f1560 | 2016-08-29 03:59:05 -0700 | [diff] [blame] | 26 | auto factory = CreateBuiltinAudioDecoderFactory(); |
kwiberg | 0fa0a97 | 2016-04-19 05:03:45 -0700 | [diff] [blame] | 27 | // Use PCMu, because it doesn't use scaled timestamps. |
ossu | f1b08da | 2016-09-23 02:19:43 -0700 | [diff] [blame] | 28 | const DecoderDatabase::DecoderInfo info(NetEqDecoder::kDecoderPCMu, factory); |
henrik.lundin@webrtc.org | d94659d | 2013-01-29 12:09:21 +0000 | [diff] [blame] | 29 | static const uint8_t kRtpPayloadType = 0; |
| 30 | EXPECT_CALL(db, GetDecoderInfo(kRtpPayloadType)) |
| 31 | .WillRepeatedly(Return(&info)); |
| 32 | |
| 33 | TimestampScaler scaler(db); |
| 34 | // Test both sides of the timestamp wrap-around. |
| 35 | for (uint32_t timestamp = 0xFFFFFFFF - 5; timestamp != 5; ++timestamp) { |
| 36 | // Scale to internal timestamp. |
| 37 | EXPECT_EQ(timestamp, scaler.ToInternal(timestamp, kRtpPayloadType)); |
| 38 | // Scale back. |
| 39 | EXPECT_EQ(timestamp, scaler.ToExternal(timestamp)); |
| 40 | } |
| 41 | |
| 42 | EXPECT_CALL(db, Die()); // Called when database object is deleted. |
| 43 | } |
| 44 | |
| 45 | TEST(TimestampScaler, TestNoScalingLargeStep) { |
| 46 | MockDecoderDatabase db; |
ossu | 09f1560 | 2016-08-29 03:59:05 -0700 | [diff] [blame] | 47 | auto factory = CreateBuiltinAudioDecoderFactory(); |
kwiberg | 0fa0a97 | 2016-04-19 05:03:45 -0700 | [diff] [blame] | 48 | // Use PCMu, because it doesn't use scaled timestamps. |
ossu | f1b08da | 2016-09-23 02:19:43 -0700 | [diff] [blame] | 49 | const DecoderDatabase::DecoderInfo info(NetEqDecoder::kDecoderPCMu, factory); |
henrik.lundin@webrtc.org | d94659d | 2013-01-29 12:09:21 +0000 | [diff] [blame] | 50 | static const uint8_t kRtpPayloadType = 0; |
| 51 | EXPECT_CALL(db, GetDecoderInfo(kRtpPayloadType)) |
| 52 | .WillRepeatedly(Return(&info)); |
| 53 | |
| 54 | TimestampScaler scaler(db); |
| 55 | // Test both sides of the timestamp wrap-around. |
| 56 | static const uint32_t kStep = 160; |
| 57 | uint32_t start_timestamp = 0; |
| 58 | // |external_timestamp| will be a large positive value. |
| 59 | start_timestamp = start_timestamp - 5 * kStep; |
| 60 | for (uint32_t timestamp = start_timestamp; timestamp != 5 * kStep; |
| 61 | timestamp += kStep) { |
| 62 | // Scale to internal timestamp. |
| 63 | EXPECT_EQ(timestamp, scaler.ToInternal(timestamp, kRtpPayloadType)); |
| 64 | // Scale back. |
| 65 | EXPECT_EQ(timestamp, scaler.ToExternal(timestamp)); |
| 66 | } |
| 67 | |
| 68 | EXPECT_CALL(db, Die()); // Called when database object is deleted. |
| 69 | } |
| 70 | |
| 71 | TEST(TimestampScaler, TestG722) { |
| 72 | MockDecoderDatabase db; |
ossu | 09f1560 | 2016-08-29 03:59:05 -0700 | [diff] [blame] | 73 | auto factory = CreateBuiltinAudioDecoderFactory(); |
kwiberg | 0fa0a97 | 2016-04-19 05:03:45 -0700 | [diff] [blame] | 74 | // Use G722, which has a factor 2 scaling. |
ossu | f1b08da | 2016-09-23 02:19:43 -0700 | [diff] [blame] | 75 | const DecoderDatabase::DecoderInfo info(NetEqDecoder::kDecoderG722, factory); |
henrik.lundin@webrtc.org | d94659d | 2013-01-29 12:09:21 +0000 | [diff] [blame] | 76 | static const uint8_t kRtpPayloadType = 17; |
| 77 | EXPECT_CALL(db, GetDecoderInfo(kRtpPayloadType)) |
| 78 | .WillRepeatedly(Return(&info)); |
| 79 | |
| 80 | TimestampScaler scaler(db); |
| 81 | // Test both sides of the timestamp wrap-around. |
| 82 | uint32_t external_timestamp = 0xFFFFFFFF - 5; |
| 83 | uint32_t internal_timestamp = external_timestamp; |
| 84 | for (; external_timestamp != 5; ++external_timestamp) { |
| 85 | // Scale to internal timestamp. |
| 86 | EXPECT_EQ(internal_timestamp, |
| 87 | scaler.ToInternal(external_timestamp, kRtpPayloadType)); |
| 88 | // Scale back. |
| 89 | EXPECT_EQ(external_timestamp, scaler.ToExternal(internal_timestamp)); |
| 90 | internal_timestamp += 2; |
| 91 | } |
| 92 | |
| 93 | EXPECT_CALL(db, Die()); // Called when database object is deleted. |
| 94 | } |
| 95 | |
| 96 | TEST(TimestampScaler, TestG722LargeStep) { |
| 97 | MockDecoderDatabase db; |
ossu | 09f1560 | 2016-08-29 03:59:05 -0700 | [diff] [blame] | 98 | auto factory = CreateBuiltinAudioDecoderFactory(); |
kwiberg | 0fa0a97 | 2016-04-19 05:03:45 -0700 | [diff] [blame] | 99 | // Use G722, which has a factor 2 scaling. |
ossu | f1b08da | 2016-09-23 02:19:43 -0700 | [diff] [blame] | 100 | const DecoderDatabase::DecoderInfo info(NetEqDecoder::kDecoderG722, factory); |
henrik.lundin@webrtc.org | d94659d | 2013-01-29 12:09:21 +0000 | [diff] [blame] | 101 | static const uint8_t kRtpPayloadType = 17; |
| 102 | EXPECT_CALL(db, GetDecoderInfo(kRtpPayloadType)) |
| 103 | .WillRepeatedly(Return(&info)); |
| 104 | |
| 105 | TimestampScaler scaler(db); |
| 106 | // Test both sides of the timestamp wrap-around. |
| 107 | static const uint32_t kStep = 320; |
| 108 | uint32_t external_timestamp = 0; |
| 109 | // |external_timestamp| will be a large positive value. |
| 110 | external_timestamp = external_timestamp - 5 * kStep; |
| 111 | uint32_t internal_timestamp = external_timestamp; |
| 112 | for (; external_timestamp != 5 * kStep; external_timestamp += kStep) { |
| 113 | // Scale to internal timestamp. |
| 114 | EXPECT_EQ(internal_timestamp, |
| 115 | scaler.ToInternal(external_timestamp, kRtpPayloadType)); |
| 116 | // Scale back. |
| 117 | EXPECT_EQ(external_timestamp, scaler.ToExternal(internal_timestamp)); |
| 118 | // Internal timestamp should be incremented with twice the step. |
| 119 | internal_timestamp += 2 * kStep; |
| 120 | } |
| 121 | |
| 122 | EXPECT_CALL(db, Die()); // Called when database object is deleted. |
| 123 | } |
| 124 | |
| 125 | TEST(TimestampScaler, TestG722WithCng) { |
| 126 | MockDecoderDatabase db; |
ossu | 09f1560 | 2016-08-29 03:59:05 -0700 | [diff] [blame] | 127 | auto factory = CreateBuiltinAudioDecoderFactory(); |
kwiberg | 0fa0a97 | 2016-04-19 05:03:45 -0700 | [diff] [blame] | 128 | // Use G722, which has a factor 2 scaling. |
ossu | f1b08da | 2016-09-23 02:19:43 -0700 | [diff] [blame] | 129 | const DecoderDatabase::DecoderInfo info_g722(NetEqDecoder::kDecoderG722, |
ossu | 09f1560 | 2016-08-29 03:59:05 -0700 | [diff] [blame] | 130 | factory); |
ossu | f1b08da | 2016-09-23 02:19:43 -0700 | [diff] [blame] | 131 | const DecoderDatabase::DecoderInfo info_cng(NetEqDecoder::kDecoderCNGwb, |
ossu | 09f1560 | 2016-08-29 03:59:05 -0700 | [diff] [blame] | 132 | factory); |
henrik.lundin@webrtc.org | d94659d | 2013-01-29 12:09:21 +0000 | [diff] [blame] | 133 | static const uint8_t kRtpPayloadTypeG722 = 17; |
| 134 | static const uint8_t kRtpPayloadTypeCng = 13; |
| 135 | EXPECT_CALL(db, GetDecoderInfo(kRtpPayloadTypeG722)) |
| 136 | .WillRepeatedly(Return(&info_g722)); |
| 137 | EXPECT_CALL(db, GetDecoderInfo(kRtpPayloadTypeCng)) |
| 138 | .WillRepeatedly(Return(&info_cng)); |
| 139 | |
| 140 | TimestampScaler scaler(db); |
| 141 | // Test both sides of the timestamp wrap-around. |
| 142 | uint32_t external_timestamp = 0xFFFFFFFF - 5; |
| 143 | uint32_t internal_timestamp = external_timestamp; |
| 144 | bool next_is_cng = false; |
| 145 | for (; external_timestamp != 5; ++external_timestamp) { |
| 146 | // Alternate between G.722 and CNG every other packet. |
| 147 | if (next_is_cng) { |
| 148 | // Scale to internal timestamp. |
| 149 | EXPECT_EQ(internal_timestamp, |
| 150 | scaler.ToInternal(external_timestamp, kRtpPayloadTypeCng)); |
| 151 | next_is_cng = false; |
| 152 | } else { |
| 153 | // Scale to internal timestamp. |
| 154 | EXPECT_EQ(internal_timestamp, |
| 155 | scaler.ToInternal(external_timestamp, kRtpPayloadTypeG722)); |
| 156 | next_is_cng = true; |
| 157 | } |
| 158 | // Scale back. |
| 159 | EXPECT_EQ(external_timestamp, scaler.ToExternal(internal_timestamp)); |
| 160 | internal_timestamp += 2; |
| 161 | } |
| 162 | |
| 163 | EXPECT_CALL(db, Die()); // Called when database object is deleted. |
| 164 | } |
| 165 | |
| 166 | // Make sure that the method ToInternal(Packet* packet) is wired up correctly. |
| 167 | // Since it is simply calling the other ToInternal method, we are not doing |
| 168 | // as many tests here. |
| 169 | TEST(TimestampScaler, TestG722Packet) { |
| 170 | MockDecoderDatabase db; |
ossu | 09f1560 | 2016-08-29 03:59:05 -0700 | [diff] [blame] | 171 | auto factory = CreateBuiltinAudioDecoderFactory(); |
kwiberg | 0fa0a97 | 2016-04-19 05:03:45 -0700 | [diff] [blame] | 172 | // Use G722, which has a factor 2 scaling. |
ossu | f1b08da | 2016-09-23 02:19:43 -0700 | [diff] [blame] | 173 | const DecoderDatabase::DecoderInfo info(NetEqDecoder::kDecoderG722, factory); |
henrik.lundin@webrtc.org | d94659d | 2013-01-29 12:09:21 +0000 | [diff] [blame] | 174 | static const uint8_t kRtpPayloadType = 17; |
| 175 | EXPECT_CALL(db, GetDecoderInfo(kRtpPayloadType)) |
| 176 | .WillRepeatedly(Return(&info)); |
| 177 | |
| 178 | TimestampScaler scaler(db); |
| 179 | // Test both sides of the timestamp wrap-around. |
| 180 | uint32_t external_timestamp = 0xFFFFFFFF - 5; |
| 181 | uint32_t internal_timestamp = external_timestamp; |
| 182 | Packet packet; |
ossu | 7a37761 | 2016-10-18 04:06:13 -0700 | [diff] [blame] | 183 | packet.payload_type = kRtpPayloadType; |
henrik.lundin@webrtc.org | d94659d | 2013-01-29 12:09:21 +0000 | [diff] [blame] | 184 | for (; external_timestamp != 5; ++external_timestamp) { |
ossu | 7a37761 | 2016-10-18 04:06:13 -0700 | [diff] [blame] | 185 | packet.timestamp = external_timestamp; |
henrik.lundin@webrtc.org | d94659d | 2013-01-29 12:09:21 +0000 | [diff] [blame] | 186 | // Scale to internal timestamp. |
| 187 | scaler.ToInternal(&packet); |
ossu | 7a37761 | 2016-10-18 04:06:13 -0700 | [diff] [blame] | 188 | EXPECT_EQ(internal_timestamp, packet.timestamp); |
henrik.lundin@webrtc.org | d94659d | 2013-01-29 12:09:21 +0000 | [diff] [blame] | 189 | internal_timestamp += 2; |
| 190 | } |
| 191 | |
| 192 | EXPECT_CALL(db, Die()); // Called when database object is deleted. |
| 193 | } |
| 194 | |
| 195 | // Make sure that the method ToInternal(PacketList* packet_list) is wired up |
| 196 | // correctly. Since it is simply calling the ToInternal(Packet* packet) method, |
| 197 | // we are not doing as many tests here. |
| 198 | TEST(TimestampScaler, TestG722PacketList) { |
| 199 | MockDecoderDatabase db; |
ossu | 09f1560 | 2016-08-29 03:59:05 -0700 | [diff] [blame] | 200 | auto factory = CreateBuiltinAudioDecoderFactory(); |
kwiberg | 0fa0a97 | 2016-04-19 05:03:45 -0700 | [diff] [blame] | 201 | // Use G722, which has a factor 2 scaling. |
ossu | f1b08da | 2016-09-23 02:19:43 -0700 | [diff] [blame] | 202 | const DecoderDatabase::DecoderInfo info(NetEqDecoder::kDecoderG722, factory); |
henrik.lundin@webrtc.org | d94659d | 2013-01-29 12:09:21 +0000 | [diff] [blame] | 203 | static const uint8_t kRtpPayloadType = 17; |
| 204 | EXPECT_CALL(db, GetDecoderInfo(kRtpPayloadType)) |
| 205 | .WillRepeatedly(Return(&info)); |
| 206 | |
| 207 | TimestampScaler scaler(db); |
| 208 | // Test both sides of the timestamp wrap-around. |
| 209 | uint32_t external_timestamp = 0xFFFFFFFF - 5; |
| 210 | uint32_t internal_timestamp = external_timestamp; |
henrik.lundin@webrtc.org | d94659d | 2013-01-29 12:09:21 +0000 | [diff] [blame] | 211 | PacketList packet_list; |
ossu | a73f6c9 | 2016-10-24 08:25:28 -0700 | [diff] [blame] | 212 | { |
| 213 | Packet packet1; |
| 214 | packet1.payload_type = kRtpPayloadType; |
| 215 | packet1.timestamp = external_timestamp; |
| 216 | Packet packet2; |
| 217 | packet2.payload_type = kRtpPayloadType; |
| 218 | packet2.timestamp = external_timestamp + 10; |
| 219 | packet_list.push_back(std::move(packet1)); |
| 220 | packet_list.push_back(std::move(packet2)); |
| 221 | } |
henrik.lundin@webrtc.org | d94659d | 2013-01-29 12:09:21 +0000 | [diff] [blame] | 222 | |
| 223 | scaler.ToInternal(&packet_list); |
ossu | a73f6c9 | 2016-10-24 08:25:28 -0700 | [diff] [blame] | 224 | EXPECT_EQ(internal_timestamp, packet_list.front().timestamp); |
| 225 | packet_list.pop_front(); |
| 226 | EXPECT_EQ(internal_timestamp + 20, packet_list.front().timestamp); |
henrik.lundin@webrtc.org | d94659d | 2013-01-29 12:09:21 +0000 | [diff] [blame] | 227 | |
| 228 | EXPECT_CALL(db, Die()); // Called when database object is deleted. |
| 229 | } |
| 230 | |
| 231 | TEST(TimestampScaler, TestG722Reset) { |
| 232 | MockDecoderDatabase db; |
ossu | 09f1560 | 2016-08-29 03:59:05 -0700 | [diff] [blame] | 233 | auto factory = CreateBuiltinAudioDecoderFactory(); |
kwiberg | 0fa0a97 | 2016-04-19 05:03:45 -0700 | [diff] [blame] | 234 | // Use G722, which has a factor 2 scaling. |
ossu | f1b08da | 2016-09-23 02:19:43 -0700 | [diff] [blame] | 235 | const DecoderDatabase::DecoderInfo info(NetEqDecoder::kDecoderG722, factory); |
henrik.lundin@webrtc.org | d94659d | 2013-01-29 12:09:21 +0000 | [diff] [blame] | 236 | static const uint8_t kRtpPayloadType = 17; |
| 237 | EXPECT_CALL(db, GetDecoderInfo(kRtpPayloadType)) |
| 238 | .WillRepeatedly(Return(&info)); |
| 239 | |
| 240 | TimestampScaler scaler(db); |
| 241 | // Test both sides of the timestamp wrap-around. |
| 242 | uint32_t external_timestamp = 0xFFFFFFFF - 5; |
| 243 | uint32_t internal_timestamp = external_timestamp; |
| 244 | for (; external_timestamp != 5; ++external_timestamp) { |
| 245 | // Scale to internal timestamp. |
| 246 | EXPECT_EQ(internal_timestamp, |
| 247 | scaler.ToInternal(external_timestamp, kRtpPayloadType)); |
| 248 | // Scale back. |
| 249 | EXPECT_EQ(external_timestamp, scaler.ToExternal(internal_timestamp)); |
| 250 | internal_timestamp += 2; |
| 251 | } |
| 252 | // Reset the scaler. After this, we expect the internal and external to start |
| 253 | // over at the same value again. |
| 254 | scaler.Reset(); |
| 255 | internal_timestamp = external_timestamp; |
| 256 | for (; external_timestamp != 15; ++external_timestamp) { |
| 257 | // Scale to internal timestamp. |
| 258 | EXPECT_EQ(internal_timestamp, |
| 259 | scaler.ToInternal(external_timestamp, kRtpPayloadType)); |
| 260 | // Scale back. |
| 261 | EXPECT_EQ(external_timestamp, scaler.ToExternal(internal_timestamp)); |
| 262 | internal_timestamp += 2; |
| 263 | } |
| 264 | |
| 265 | EXPECT_CALL(db, Die()); // Called when database object is deleted. |
| 266 | } |
| 267 | |
minyue@webrtc.org | f563e85 | 2014-07-18 21:11:27 +0000 | [diff] [blame] | 268 | // TODO(minyue): This test becomes trivial since Opus does not need a timestamp |
| 269 | // scaler. Therefore, this test may be removed in future. There is no harm to |
| 270 | // keep it, since it can be taken as a test case for the situation of a trivial |
| 271 | // timestamp scaler. |
henrik.lundin@webrtc.org | ac59dba | 2013-01-31 09:55:24 +0000 | [diff] [blame] | 272 | TEST(TimestampScaler, TestOpusLargeStep) { |
| 273 | MockDecoderDatabase db; |
ossu | 09f1560 | 2016-08-29 03:59:05 -0700 | [diff] [blame] | 274 | auto factory = CreateBuiltinAudioDecoderFactory(); |
ossu | f1b08da | 2016-09-23 02:19:43 -0700 | [diff] [blame] | 275 | const DecoderDatabase::DecoderInfo info(NetEqDecoder::kDecoderOpus, factory); |
henrik.lundin@webrtc.org | ac59dba | 2013-01-31 09:55:24 +0000 | [diff] [blame] | 276 | static const uint8_t kRtpPayloadType = 17; |
| 277 | EXPECT_CALL(db, GetDecoderInfo(kRtpPayloadType)) |
| 278 | .WillRepeatedly(Return(&info)); |
| 279 | |
| 280 | TimestampScaler scaler(db); |
| 281 | // Test both sides of the timestamp wrap-around. |
| 282 | static const uint32_t kStep = 960; |
| 283 | uint32_t external_timestamp = 0; |
| 284 | // |external_timestamp| will be a large positive value. |
| 285 | external_timestamp = external_timestamp - 5 * kStep; |
| 286 | uint32_t internal_timestamp = external_timestamp; |
| 287 | for (; external_timestamp != 5 * kStep; external_timestamp += kStep) { |
| 288 | // Scale to internal timestamp. |
| 289 | EXPECT_EQ(internal_timestamp, |
| 290 | scaler.ToInternal(external_timestamp, kRtpPayloadType)); |
| 291 | // Scale back. |
| 292 | EXPECT_EQ(external_timestamp, scaler.ToExternal(internal_timestamp)); |
minyue@webrtc.org | f563e85 | 2014-07-18 21:11:27 +0000 | [diff] [blame] | 293 | internal_timestamp += kStep; |
henrik.lundin@webrtc.org | ac59dba | 2013-01-31 09:55:24 +0000 | [diff] [blame] | 294 | } |
| 295 | |
| 296 | EXPECT_CALL(db, Die()); // Called when database object is deleted. |
| 297 | } |
| 298 | |
henrik.lundin@webrtc.org | d94659d | 2013-01-29 12:09:21 +0000 | [diff] [blame] | 299 | TEST(TimestampScaler, Failures) { |
| 300 | static const uint8_t kRtpPayloadType = 17; |
| 301 | MockDecoderDatabase db; |
| 302 | EXPECT_CALL(db, GetDecoderInfo(kRtpPayloadType)) |
| 303 | .WillOnce(ReturnNull()); // Return NULL to indicate unknown payload type. |
| 304 | |
| 305 | TimestampScaler scaler(db); |
| 306 | uint32_t timestamp = 4711; // Some number. |
| 307 | EXPECT_EQ(timestamp, scaler.ToInternal(timestamp, kRtpPayloadType)); |
| 308 | |
| 309 | Packet* packet = NULL; |
| 310 | scaler.ToInternal(packet); // Should not crash. That's all we can test. |
| 311 | |
| 312 | EXPECT_CALL(db, Die()); // Called when database object is deleted. |
| 313 | } |
| 314 | |
| 315 | } // namespace webrtc |