1 /*
2 * Copyright (c) 2014 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
11 #include "modules/audio_coding/codecs/red/audio_encoder_copy_red.h"
12
13 #include <memory>
14 #include <vector>
15
16 #include "rtc_base/checks.h"
17 #include "rtc_base/numerics/safe_conversions.h"
18 #include "test/field_trial.h"
19 #include "test/gtest.h"
20 #include "test/mock_audio_encoder.h"
21 #include "test/scoped_key_value_config.h"
22 #include "test/testsupport/rtc_expect_death.h"
23
24 using ::testing::_;
25 using ::testing::Eq;
26 using ::testing::InSequence;
27 using ::testing::Invoke;
28 using ::testing::MockFunction;
29 using ::testing::Not;
30 using ::testing::Optional;
31 using ::testing::Return;
32 using ::testing::SetArgPointee;
33
34 namespace webrtc {
35
36 namespace {
37 static const size_t kMaxNumSamples = 48 * 10 * 2; // 10 ms @ 48 kHz stereo.
38 static const size_t kRedLastHeaderLength =
39 1; // 1 byte RED header for the last element.
40 }
41
42 class AudioEncoderCopyRedTest : public ::testing::Test {
43 protected:
AudioEncoderCopyRedTest()44 AudioEncoderCopyRedTest()
45 : mock_encoder_(new MockAudioEncoder),
46 timestamp_(4711),
47 sample_rate_hz_(16000),
48 num_audio_samples_10ms(sample_rate_hz_ / 100),
49 red_payload_type_(200) {
50 AudioEncoderCopyRed::Config config;
51 config.payload_type = red_payload_type_;
52 config.speech_encoder = std::unique_ptr<AudioEncoder>(mock_encoder_);
53 red_.reset(new AudioEncoderCopyRed(std::move(config), field_trials_));
54 memset(audio_, 0, sizeof(audio_));
55 EXPECT_CALL(*mock_encoder_, NumChannels()).WillRepeatedly(Return(1U));
56 EXPECT_CALL(*mock_encoder_, SampleRateHz())
57 .WillRepeatedly(Return(sample_rate_hz_));
58 }
59
TearDown()60 void TearDown() override { red_.reset(); }
61
Encode()62 void Encode() {
63 ASSERT_TRUE(red_.get() != NULL);
64 encoded_.Clear();
65 encoded_info_ = red_->Encode(
66 timestamp_,
67 rtc::ArrayView<const int16_t>(audio_, num_audio_samples_10ms),
68 &encoded_);
69 timestamp_ += rtc::checked_cast<uint32_t>(num_audio_samples_10ms);
70 }
71
72 test::ScopedKeyValueConfig field_trials_;
73 MockAudioEncoder* mock_encoder_;
74 std::unique_ptr<AudioEncoderCopyRed> red_;
75 uint32_t timestamp_;
76 int16_t audio_[kMaxNumSamples];
77 const int sample_rate_hz_;
78 size_t num_audio_samples_10ms;
79 rtc::Buffer encoded_;
80 AudioEncoder::EncodedInfo encoded_info_;
81 const int red_payload_type_;
82 };
83
TEST_F(AudioEncoderCopyRedTest,CreateAndDestroy)84 TEST_F(AudioEncoderCopyRedTest, CreateAndDestroy) {}
85
TEST_F(AudioEncoderCopyRedTest,CheckSampleRatePropagation)86 TEST_F(AudioEncoderCopyRedTest, CheckSampleRatePropagation) {
87 EXPECT_CALL(*mock_encoder_, SampleRateHz()).WillOnce(Return(17));
88 EXPECT_EQ(17, red_->SampleRateHz());
89 }
90
TEST_F(AudioEncoderCopyRedTest,CheckNumChannelsPropagation)91 TEST_F(AudioEncoderCopyRedTest, CheckNumChannelsPropagation) {
92 EXPECT_CALL(*mock_encoder_, NumChannels()).WillOnce(Return(17U));
93 EXPECT_EQ(17U, red_->NumChannels());
94 }
95
TEST_F(AudioEncoderCopyRedTest,CheckFrameSizePropagation)96 TEST_F(AudioEncoderCopyRedTest, CheckFrameSizePropagation) {
97 EXPECT_CALL(*mock_encoder_, Num10MsFramesInNextPacket())
98 .WillOnce(Return(17U));
99 EXPECT_EQ(17U, red_->Num10MsFramesInNextPacket());
100 }
101
TEST_F(AudioEncoderCopyRedTest,CheckMaxFrameSizePropagation)102 TEST_F(AudioEncoderCopyRedTest, CheckMaxFrameSizePropagation) {
103 EXPECT_CALL(*mock_encoder_, Max10MsFramesInAPacket()).WillOnce(Return(17U));
104 EXPECT_EQ(17U, red_->Max10MsFramesInAPacket());
105 }
106
TEST_F(AudioEncoderCopyRedTest,CheckTargetAudioBitratePropagation)107 TEST_F(AudioEncoderCopyRedTest, CheckTargetAudioBitratePropagation) {
108 EXPECT_CALL(*mock_encoder_,
109 OnReceivedUplinkBandwidth(4711, absl::optional<int64_t>()));
110 red_->OnReceivedUplinkBandwidth(4711, absl::nullopt);
111 }
112
TEST_F(AudioEncoderCopyRedTest,CheckPacketLossFractionPropagation)113 TEST_F(AudioEncoderCopyRedTest, CheckPacketLossFractionPropagation) {
114 EXPECT_CALL(*mock_encoder_, OnReceivedUplinkPacketLossFraction(0.5));
115 red_->OnReceivedUplinkPacketLossFraction(0.5);
116 }
117
TEST_F(AudioEncoderCopyRedTest,CheckGetFrameLengthRangePropagation)118 TEST_F(AudioEncoderCopyRedTest, CheckGetFrameLengthRangePropagation) {
119 auto expected_range =
120 std::make_pair(TimeDelta::Millis(20), TimeDelta::Millis(20));
121 EXPECT_CALL(*mock_encoder_, GetFrameLengthRange())
122 .WillRepeatedly(Return(absl::make_optional(expected_range)));
123 EXPECT_THAT(red_->GetFrameLengthRange(), Optional(Eq(expected_range)));
124 }
125
126 // Checks that the an Encode() call is immediately propagated to the speech
127 // encoder.
TEST_F(AudioEncoderCopyRedTest,CheckImmediateEncode)128 TEST_F(AudioEncoderCopyRedTest, CheckImmediateEncode) {
129 // Interleaving the EXPECT_CALL sequence with expectations on the MockFunction
130 // check ensures that exactly one call to EncodeImpl happens in each
131 // Encode call.
132 InSequence s;
133 MockFunction<void(int check_point_id)> check;
134 for (int i = 1; i <= 6; ++i) {
135 EXPECT_CALL(*mock_encoder_, EncodeImpl(_, _, _))
136 .WillRepeatedly(Return(AudioEncoder::EncodedInfo()));
137 EXPECT_CALL(check, Call(i));
138 Encode();
139 check.Call(i);
140 }
141 }
142
143 // Checks that no output is produced if the underlying codec doesn't emit any
144 // new data, even if the RED codec is loaded with a secondary encoding.
TEST_F(AudioEncoderCopyRedTest,CheckNoOutput)145 TEST_F(AudioEncoderCopyRedTest, CheckNoOutput) {
146 static const size_t kEncodedSize = 17;
147 static const size_t kHeaderLenBytes = 5;
148 {
149 InSequence s;
150 EXPECT_CALL(*mock_encoder_, EncodeImpl(_, _, _))
151 .WillOnce(Invoke(MockAudioEncoder::FakeEncoding(kEncodedSize)))
152 .WillOnce(Invoke(MockAudioEncoder::FakeEncoding(0)))
153 .WillOnce(Invoke(MockAudioEncoder::FakeEncoding(kEncodedSize)));
154 }
155
156 // Start with one Encode() call that will produce output.
157 Encode();
158 // First call is a special case, since it does not include a secondary
159 // payload.
160 EXPECT_EQ(0u, encoded_info_.redundant.size());
161 EXPECT_EQ(kEncodedSize + kRedLastHeaderLength, encoded_info_.encoded_bytes);
162
163 // Next call to the speech encoder will not produce any output.
164 Encode();
165 EXPECT_EQ(0u, encoded_info_.encoded_bytes);
166
167 // Final call to the speech encoder will produce output.
168 Encode();
169 EXPECT_EQ(2 * kEncodedSize + kHeaderLenBytes, encoded_info_.encoded_bytes);
170 ASSERT_EQ(2u, encoded_info_.redundant.size());
171 }
172
173 // Checks that the correct payload sizes are populated into the redundancy
174 // information for a redundancy level of 1.
TEST_F(AudioEncoderCopyRedTest,CheckPayloadSizes1)175 TEST_F(AudioEncoderCopyRedTest, CheckPayloadSizes1) {
176 // Let the mock encoder return payload sizes 1, 2, 3, ..., 10 for the sequence
177 // of calls.
178 static const int kNumPackets = 10;
179 InSequence s;
180 for (int encode_size = 1; encode_size <= kNumPackets; ++encode_size) {
181 EXPECT_CALL(*mock_encoder_, EncodeImpl(_, _, _))
182 .WillOnce(Invoke(MockAudioEncoder::FakeEncoding(encode_size)));
183 }
184
185 // First call is a special case, since it does not include a secondary
186 // payload.
187 Encode();
188 EXPECT_EQ(0u, encoded_info_.redundant.size());
189 EXPECT_EQ(kRedLastHeaderLength + 1u, encoded_info_.encoded_bytes);
190
191 for (size_t i = 2; i <= kNumPackets; ++i) {
192 Encode();
193 ASSERT_EQ(2u, encoded_info_.redundant.size());
194 EXPECT_EQ(i, encoded_info_.redundant[1].encoded_bytes);
195 EXPECT_EQ(i - 1, encoded_info_.redundant[0].encoded_bytes);
196 EXPECT_EQ(5 + i + (i - 1), encoded_info_.encoded_bytes);
197 }
198 }
199
200 // Checks that the correct payload sizes are populated into the redundancy
201 // information for a redundancy level of 0.
TEST_F(AudioEncoderCopyRedTest,CheckPayloadSizes0)202 TEST_F(AudioEncoderCopyRedTest, CheckPayloadSizes0) {
203 webrtc::test::ScopedKeyValueConfig field_trials(
204 field_trials_, "WebRTC-Audio-Red-For-Opus/Enabled-0/");
205 // Recreate the RED encoder to take the new field trial setting into account.
206 AudioEncoderCopyRed::Config config;
207 config.payload_type = red_payload_type_;
208 config.speech_encoder = std::move(red_->ReclaimContainedEncoders()[0]);
209 red_.reset(new AudioEncoderCopyRed(std::move(config), field_trials));
210
211 // Let the mock encoder return payload sizes 1, 2, 3, ..., 10 for the sequence
212 // of calls.
213 static const int kNumPackets = 10;
214 InSequence s;
215 for (int encode_size = 1; encode_size <= kNumPackets; ++encode_size) {
216 EXPECT_CALL(*mock_encoder_, EncodeImpl(_, _, _))
217 .WillOnce(Invoke(MockAudioEncoder::FakeEncoding(encode_size)));
218 }
219
220 for (size_t i = 1; i <= kNumPackets; ++i) {
221 Encode();
222 ASSERT_EQ(0u, encoded_info_.redundant.size());
223 EXPECT_EQ(1 + i, encoded_info_.encoded_bytes);
224 }
225 }
226 // Checks that the correct payload sizes are populated into the redundancy
227 // information for a redundancy level of 2.
TEST_F(AudioEncoderCopyRedTest,CheckPayloadSizes2)228 TEST_F(AudioEncoderCopyRedTest, CheckPayloadSizes2) {
229 webrtc::test::ScopedKeyValueConfig field_trials(
230 field_trials_, "WebRTC-Audio-Red-For-Opus/Enabled-2/");
231 // Recreate the RED encoder to take the new field trial setting into account.
232 AudioEncoderCopyRed::Config config;
233 config.payload_type = red_payload_type_;
234 config.speech_encoder = std::move(red_->ReclaimContainedEncoders()[0]);
235 red_.reset(new AudioEncoderCopyRed(std::move(config), field_trials));
236
237 // Let the mock encoder return payload sizes 1, 2, 3, ..., 10 for the sequence
238 // of calls.
239 static const int kNumPackets = 10;
240 InSequence s;
241 for (int encode_size = 1; encode_size <= kNumPackets; ++encode_size) {
242 EXPECT_CALL(*mock_encoder_, EncodeImpl(_, _, _))
243 .WillOnce(Invoke(MockAudioEncoder::FakeEncoding(encode_size)));
244 }
245
246 // First call is a special case, since it does not include a secondary
247 // payload.
248 Encode();
249 EXPECT_EQ(0u, encoded_info_.redundant.size());
250 EXPECT_EQ(kRedLastHeaderLength + 1u, encoded_info_.encoded_bytes);
251
252 // Second call is also special since it does not include a tertiary
253 // payload.
254 Encode();
255 EXPECT_EQ(2u, encoded_info_.redundant.size());
256 EXPECT_EQ(8u, encoded_info_.encoded_bytes);
257
258 for (size_t i = 3; i <= kNumPackets; ++i) {
259 Encode();
260 ASSERT_EQ(3u, encoded_info_.redundant.size());
261 EXPECT_EQ(i, encoded_info_.redundant[2].encoded_bytes);
262 EXPECT_EQ(i - 1, encoded_info_.redundant[1].encoded_bytes);
263 EXPECT_EQ(i - 2, encoded_info_.redundant[0].encoded_bytes);
264 EXPECT_EQ(9 + i + (i - 1) + (i - 2), encoded_info_.encoded_bytes);
265 }
266 }
267
268 // Checks that the correct payload sizes are populated into the redundancy
269 // information for a redundancy level of 3.
TEST_F(AudioEncoderCopyRedTest,CheckPayloadSizes3)270 TEST_F(AudioEncoderCopyRedTest, CheckPayloadSizes3) {
271 webrtc::test::ScopedKeyValueConfig field_trials(
272 field_trials_, "WebRTC-Audio-Red-For-Opus/Enabled-3/");
273 // Recreate the RED encoder to take the new field trial setting into account.
274 AudioEncoderCopyRed::Config config;
275 config.payload_type = red_payload_type_;
276 config.speech_encoder = std::move(red_->ReclaimContainedEncoders()[0]);
277 red_.reset(new AudioEncoderCopyRed(std::move(config), field_trials_));
278
279 // Let the mock encoder return payload sizes 1, 2, 3, ..., 10 for the sequence
280 // of calls.
281 static const int kNumPackets = 10;
282 InSequence s;
283 for (int encode_size = 1; encode_size <= kNumPackets; ++encode_size) {
284 EXPECT_CALL(*mock_encoder_, EncodeImpl(_, _, _))
285 .WillOnce(Invoke(MockAudioEncoder::FakeEncoding(encode_size)));
286 }
287
288 // First call is a special case, since it does not include a secondary
289 // payload.
290 Encode();
291 EXPECT_EQ(0u, encoded_info_.redundant.size());
292 EXPECT_EQ(kRedLastHeaderLength + 1u, encoded_info_.encoded_bytes);
293
294 // Second call is also special since it does not include a tertiary
295 // payload.
296 Encode();
297 EXPECT_EQ(2u, encoded_info_.redundant.size());
298 EXPECT_EQ(8u, encoded_info_.encoded_bytes);
299
300 // Third call is also special since it does not include a quaternary
301 // payload.
302 Encode();
303 EXPECT_EQ(3u, encoded_info_.redundant.size());
304 EXPECT_EQ(15u, encoded_info_.encoded_bytes);
305
306 for (size_t i = 4; i <= kNumPackets; ++i) {
307 Encode();
308 ASSERT_EQ(4u, encoded_info_.redundant.size());
309 EXPECT_EQ(i, encoded_info_.redundant[3].encoded_bytes);
310 EXPECT_EQ(i - 1, encoded_info_.redundant[2].encoded_bytes);
311 EXPECT_EQ(i - 2, encoded_info_.redundant[1].encoded_bytes);
312 EXPECT_EQ(i - 3, encoded_info_.redundant[0].encoded_bytes);
313 EXPECT_EQ(13 + i + (i - 1) + (i - 2) + (i - 3),
314 encoded_info_.encoded_bytes);
315 }
316 }
317
318 // Checks that the correct timestamps are returned.
TEST_F(AudioEncoderCopyRedTest,CheckTimestamps)319 TEST_F(AudioEncoderCopyRedTest, CheckTimestamps) {
320 uint32_t primary_timestamp = timestamp_;
321 AudioEncoder::EncodedInfo info;
322 info.encoded_bytes = 17;
323 info.encoded_timestamp = timestamp_;
324
325 EXPECT_CALL(*mock_encoder_, EncodeImpl(_, _, _))
326 .WillOnce(Invoke(MockAudioEncoder::FakeEncoding(info)));
327
328 // First call is a special case, since it does not include a secondary
329 // payload.
330 Encode();
331 EXPECT_EQ(primary_timestamp, encoded_info_.encoded_timestamp);
332
333 uint32_t secondary_timestamp = primary_timestamp;
334 primary_timestamp = timestamp_;
335 info.encoded_timestamp = timestamp_;
336 EXPECT_CALL(*mock_encoder_, EncodeImpl(_, _, _))
337 .WillOnce(Invoke(MockAudioEncoder::FakeEncoding(info)));
338
339 Encode();
340 ASSERT_EQ(2u, encoded_info_.redundant.size());
341 EXPECT_EQ(primary_timestamp, encoded_info_.redundant[1].encoded_timestamp);
342 EXPECT_EQ(secondary_timestamp, encoded_info_.redundant[0].encoded_timestamp);
343 EXPECT_EQ(primary_timestamp, encoded_info_.encoded_timestamp);
344 }
345
346 // Checks that the primary and secondary payloads are written correctly.
TEST_F(AudioEncoderCopyRedTest,CheckPayloads)347 TEST_F(AudioEncoderCopyRedTest, CheckPayloads) {
348 // Let the mock encoder write payloads with increasing values. The first
349 // payload will have values 0, 1, 2, ..., kPayloadLenBytes - 1.
350 static const size_t kPayloadLenBytes = 5;
351 static const size_t kHeaderLenBytes = 5;
352 uint8_t payload[kPayloadLenBytes];
353 for (uint8_t i = 0; i < kPayloadLenBytes; ++i) {
354 payload[i] = i;
355 }
356 EXPECT_CALL(*mock_encoder_, EncodeImpl(_, _, _))
357 .WillRepeatedly(Invoke(MockAudioEncoder::CopyEncoding(payload)));
358
359 // First call is a special case, since it does not include a secondary
360 // payload.
361 Encode();
362 EXPECT_EQ(kRedLastHeaderLength + kPayloadLenBytes,
363 encoded_info_.encoded_bytes);
364 for (size_t i = 0; i < kPayloadLenBytes; ++i) {
365 EXPECT_EQ(i, encoded_.data()[kRedLastHeaderLength + i]);
366 }
367
368 for (int j = 0; j < 1; ++j) {
369 // Increment all values of the payload by 10.
370 for (size_t i = 0; i < kPayloadLenBytes; ++i)
371 payload[i] += 10;
372
373 Encode();
374 ASSERT_EQ(2u, encoded_info_.redundant.size());
375 EXPECT_EQ(kPayloadLenBytes, encoded_info_.redundant[0].encoded_bytes);
376 EXPECT_EQ(kPayloadLenBytes, encoded_info_.redundant[1].encoded_bytes);
377 for (size_t i = 0; i < kPayloadLenBytes; ++i) {
378 // Check secondary payload.
379 EXPECT_EQ(j * 10 + i, encoded_.data()[kHeaderLenBytes + i]);
380
381 // Check primary payload.
382 EXPECT_EQ((j + 1) * 10 + i,
383 encoded_.data()[kHeaderLenBytes + i + kPayloadLenBytes]);
384 }
385 }
386 }
387
388 // Checks correct propagation of payload type.
TEST_F(AudioEncoderCopyRedTest,CheckPayloadType)389 TEST_F(AudioEncoderCopyRedTest, CheckPayloadType) {
390 const int primary_payload_type = red_payload_type_ + 1;
391 AudioEncoder::EncodedInfo info;
392 info.encoded_bytes = 17;
393 info.payload_type = primary_payload_type;
394 EXPECT_CALL(*mock_encoder_, EncodeImpl(_, _, _))
395 .WillOnce(Invoke(MockAudioEncoder::FakeEncoding(info)));
396
397 // First call is a special case, since it does not include a secondary
398 // payload.
399 Encode();
400 ASSERT_EQ(0u, encoded_info_.redundant.size());
401
402 const int secondary_payload_type = red_payload_type_ + 2;
403 info.payload_type = secondary_payload_type;
404 EXPECT_CALL(*mock_encoder_, EncodeImpl(_, _, _))
405 .WillOnce(Invoke(MockAudioEncoder::FakeEncoding(info)));
406
407 Encode();
408 ASSERT_EQ(2u, encoded_info_.redundant.size());
409 EXPECT_EQ(secondary_payload_type, encoded_info_.redundant[1].payload_type);
410 EXPECT_EQ(primary_payload_type, encoded_info_.redundant[0].payload_type);
411 EXPECT_EQ(red_payload_type_, encoded_info_.payload_type);
412 }
413
TEST_F(AudioEncoderCopyRedTest,CheckRFC2198Header)414 TEST_F(AudioEncoderCopyRedTest, CheckRFC2198Header) {
415 const int primary_payload_type = red_payload_type_ + 1;
416 AudioEncoder::EncodedInfo info;
417 info.encoded_bytes = 10;
418 info.encoded_timestamp = timestamp_;
419 info.payload_type = primary_payload_type;
420
421 EXPECT_CALL(*mock_encoder_, EncodeImpl(_, _, _))
422 .WillOnce(Invoke(MockAudioEncoder::FakeEncoding(info)));
423 Encode();
424 info.encoded_timestamp = timestamp_; // update timestamp.
425 EXPECT_CALL(*mock_encoder_, EncodeImpl(_, _, _))
426 .WillOnce(Invoke(MockAudioEncoder::FakeEncoding(info)));
427 Encode(); // Second call will produce a redundant encoding.
428
429 EXPECT_EQ(encoded_.size(),
430 5u + 2 * 10u); // header size + two encoded payloads.
431 EXPECT_EQ(encoded_[0], primary_payload_type | 0x80);
432
433 uint32_t timestamp_delta = encoded_info_.encoded_timestamp -
434 encoded_info_.redundant[0].encoded_timestamp;
435 // Timestamp delta is encoded as a 14 bit value.
436 EXPECT_EQ(encoded_[1], timestamp_delta >> 6);
437 EXPECT_EQ(static_cast<uint8_t>(encoded_[2] >> 2), timestamp_delta & 0x3f);
438 // Redundant length is encoded as 10 bit value.
439 EXPECT_EQ(encoded_[2] & 0x3u, encoded_info_.redundant[1].encoded_bytes >> 8);
440 EXPECT_EQ(encoded_[3], encoded_info_.redundant[1].encoded_bytes & 0xff);
441 EXPECT_EQ(encoded_[4], primary_payload_type);
442
443 EXPECT_CALL(*mock_encoder_, EncodeImpl(_, _, _))
444 .WillOnce(Invoke(MockAudioEncoder::FakeEncoding(info)));
445 Encode(); // Third call will produce a redundant encoding with double
446 // redundancy.
447
448 EXPECT_EQ(encoded_.size(),
449 5u + 2 * 10u); // header size + two encoded payloads.
450 EXPECT_EQ(encoded_[0], primary_payload_type | 0x80);
451
452 timestamp_delta = encoded_info_.encoded_timestamp -
453 encoded_info_.redundant[0].encoded_timestamp;
454 // Timestamp delta is encoded as a 14 bit value.
455 EXPECT_EQ(encoded_[1], timestamp_delta >> 6);
456 EXPECT_EQ(static_cast<uint8_t>(encoded_[2] >> 2), timestamp_delta & 0x3f);
457 // Redundant length is encoded as 10 bit value.
458 EXPECT_EQ(encoded_[2] & 0x3u, encoded_info_.redundant[1].encoded_bytes >> 8);
459 EXPECT_EQ(encoded_[3], encoded_info_.redundant[1].encoded_bytes & 0xff);
460
461 EXPECT_EQ(encoded_[4], primary_payload_type | 0x80);
462 timestamp_delta = encoded_info_.encoded_timestamp -
463 encoded_info_.redundant[1].encoded_timestamp;
464 }
465
466 // Variant with a redundancy of 0.
TEST_F(AudioEncoderCopyRedTest,CheckRFC2198Header0)467 TEST_F(AudioEncoderCopyRedTest, CheckRFC2198Header0) {
468 webrtc::test::ScopedKeyValueConfig field_trials(
469 field_trials_, "WebRTC-Audio-Red-For-Opus/Enabled-0/");
470 // Recreate the RED encoder to take the new field trial setting into account.
471 AudioEncoderCopyRed::Config config;
472 config.payload_type = red_payload_type_;
473 config.speech_encoder = std::move(red_->ReclaimContainedEncoders()[0]);
474 red_.reset(new AudioEncoderCopyRed(std::move(config), field_trials));
475
476 const int primary_payload_type = red_payload_type_ + 1;
477 AudioEncoder::EncodedInfo info;
478 info.encoded_bytes = 10;
479 info.encoded_timestamp = timestamp_;
480 info.payload_type = primary_payload_type;
481
482 EXPECT_CALL(*mock_encoder_, EncodeImpl(_, _, _))
483 .WillOnce(Invoke(MockAudioEncoder::FakeEncoding(info)));
484 Encode();
485 info.encoded_timestamp = timestamp_; // update timestamp.
486 EXPECT_CALL(*mock_encoder_, EncodeImpl(_, _, _))
487 .WillOnce(Invoke(MockAudioEncoder::FakeEncoding(info)));
488 Encode(); // Second call will not produce a redundant encoding.
489
490 EXPECT_EQ(encoded_.size(),
491 1u + 1 * 10u); // header size + one encoded payloads.
492 EXPECT_EQ(encoded_[0], primary_payload_type);
493 }
494 // Variant with a redundancy of 2.
TEST_F(AudioEncoderCopyRedTest,CheckRFC2198Header2)495 TEST_F(AudioEncoderCopyRedTest, CheckRFC2198Header2) {
496 webrtc::test::ScopedKeyValueConfig field_trials(
497 field_trials_, "WebRTC-Audio-Red-For-Opus/Enabled-2/");
498 // Recreate the RED encoder to take the new field trial setting into account.
499 AudioEncoderCopyRed::Config config;
500 config.payload_type = red_payload_type_;
501 config.speech_encoder = std::move(red_->ReclaimContainedEncoders()[0]);
502 red_.reset(new AudioEncoderCopyRed(std::move(config), field_trials));
503
504 const int primary_payload_type = red_payload_type_ + 1;
505 AudioEncoder::EncodedInfo info;
506 info.encoded_bytes = 10;
507 info.encoded_timestamp = timestamp_;
508 info.payload_type = primary_payload_type;
509
510 EXPECT_CALL(*mock_encoder_, EncodeImpl(_, _, _))
511 .WillOnce(Invoke(MockAudioEncoder::FakeEncoding(info)));
512 Encode();
513 info.encoded_timestamp = timestamp_; // update timestamp.
514 EXPECT_CALL(*mock_encoder_, EncodeImpl(_, _, _))
515 .WillOnce(Invoke(MockAudioEncoder::FakeEncoding(info)));
516 Encode(); // Second call will produce a redundant encoding.
517
518 EXPECT_EQ(encoded_.size(),
519 5u + 2 * 10u); // header size + two encoded payloads.
520 EXPECT_EQ(encoded_[0], primary_payload_type | 0x80);
521
522 uint32_t timestamp_delta = encoded_info_.encoded_timestamp -
523 encoded_info_.redundant[0].encoded_timestamp;
524 // Timestamp delta is encoded as a 14 bit value.
525 EXPECT_EQ(encoded_[1], timestamp_delta >> 6);
526 EXPECT_EQ(static_cast<uint8_t>(encoded_[2] >> 2), timestamp_delta & 0x3f);
527 // Redundant length is encoded as 10 bit value.
528 EXPECT_EQ(encoded_[2] & 0x3u, encoded_info_.redundant[1].encoded_bytes >> 8);
529 EXPECT_EQ(encoded_[3], encoded_info_.redundant[1].encoded_bytes & 0xff);
530 EXPECT_EQ(encoded_[4], primary_payload_type);
531
532 EXPECT_CALL(*mock_encoder_, EncodeImpl(_, _, _))
533 .WillOnce(Invoke(MockAudioEncoder::FakeEncoding(info)));
534 Encode(); // Third call will produce a redundant encoding with double
535 // redundancy.
536
537 EXPECT_EQ(encoded_.size(),
538 9u + 3 * 10u); // header size + three encoded payloads.
539 EXPECT_EQ(encoded_[0], primary_payload_type | 0x80);
540
541 timestamp_delta = encoded_info_.encoded_timestamp -
542 encoded_info_.redundant[0].encoded_timestamp;
543 // Timestamp delta is encoded as a 14 bit value.
544 EXPECT_EQ(encoded_[1], timestamp_delta >> 6);
545 EXPECT_EQ(static_cast<uint8_t>(encoded_[2] >> 2), timestamp_delta & 0x3f);
546 // Redundant length is encoded as 10 bit value.
547 EXPECT_EQ(encoded_[2] & 0x3u, encoded_info_.redundant[1].encoded_bytes >> 8);
548 EXPECT_EQ(encoded_[3], encoded_info_.redundant[1].encoded_bytes & 0xff);
549
550 EXPECT_EQ(encoded_[4], primary_payload_type | 0x80);
551 timestamp_delta = encoded_info_.encoded_timestamp -
552 encoded_info_.redundant[1].encoded_timestamp;
553 // Timestamp delta is encoded as a 14 bit value.
554 EXPECT_EQ(encoded_[5], timestamp_delta >> 6);
555 EXPECT_EQ(static_cast<uint8_t>(encoded_[6] >> 2), timestamp_delta & 0x3f);
556 // Redundant length is encoded as 10 bit value.
557 EXPECT_EQ(encoded_[6] & 0x3u, encoded_info_.redundant[1].encoded_bytes >> 8);
558 EXPECT_EQ(encoded_[7], encoded_info_.redundant[1].encoded_bytes & 0xff);
559 EXPECT_EQ(encoded_[8], primary_payload_type);
560 }
561
TEST_F(AudioEncoderCopyRedTest,RespectsPayloadMTU)562 TEST_F(AudioEncoderCopyRedTest, RespectsPayloadMTU) {
563 const int primary_payload_type = red_payload_type_ + 1;
564 AudioEncoder::EncodedInfo info;
565 info.encoded_bytes = 600;
566 info.encoded_timestamp = timestamp_;
567 info.payload_type = primary_payload_type;
568
569 EXPECT_CALL(*mock_encoder_, EncodeImpl(_, _, _))
570 .WillOnce(Invoke(MockAudioEncoder::FakeEncoding(info)));
571 Encode();
572 info.encoded_timestamp = timestamp_; // update timestamp.
573 info.encoded_bytes = 500;
574 EXPECT_CALL(*mock_encoder_, EncodeImpl(_, _, _))
575 .WillOnce(Invoke(MockAudioEncoder::FakeEncoding(info)));
576 Encode(); // Second call will produce a redundant encoding.
577
578 EXPECT_EQ(encoded_.size(), 5u + 600u + 500u);
579
580 info.encoded_timestamp = timestamp_; // update timestamp.
581 info.encoded_bytes = 400;
582 EXPECT_CALL(*mock_encoder_, EncodeImpl(_, _, _))
583 .WillOnce(Invoke(MockAudioEncoder::FakeEncoding(info)));
584 Encode(); // Third call will drop the oldest packet.
585 EXPECT_EQ(encoded_.size(), 5u + 500u + 400u);
586 }
587
TEST_F(AudioEncoderCopyRedTest,LargeTimestampGap)588 TEST_F(AudioEncoderCopyRedTest, LargeTimestampGap) {
589 const int primary_payload_type = red_payload_type_ + 1;
590 AudioEncoder::EncodedInfo info;
591 info.encoded_bytes = 100;
592 info.encoded_timestamp = timestamp_;
593 info.payload_type = primary_payload_type;
594
595 EXPECT_CALL(*mock_encoder_, EncodeImpl(_, _, _))
596 .WillOnce(Invoke(MockAudioEncoder::FakeEncoding(info)));
597 Encode();
598 // Update timestamp to simulate a 400ms gap like the one
599 // opus DTX causes.
600 timestamp_ += 19200;
601 info.encoded_timestamp = timestamp_; // update timestamp.
602 info.encoded_bytes = 200;
603 EXPECT_CALL(*mock_encoder_, EncodeImpl(_, _, _))
604 .WillOnce(Invoke(MockAudioEncoder::FakeEncoding(info)));
605 Encode();
606
607 // The old packet will be dropped.
608 EXPECT_EQ(encoded_.size(), 1u + 200u);
609 }
610
611 #if GTEST_HAS_DEATH_TEST && !defined(WEBRTC_ANDROID)
612
613 // This test fixture tests various error conditions that makes the
614 // AudioEncoderCng die via CHECKs.
615 class AudioEncoderCopyRedDeathTest : public AudioEncoderCopyRedTest {
616 protected:
AudioEncoderCopyRedDeathTest()617 AudioEncoderCopyRedDeathTest() : AudioEncoderCopyRedTest() {}
618 };
619
TEST_F(AudioEncoderCopyRedDeathTest,WrongFrameSize)620 TEST_F(AudioEncoderCopyRedDeathTest, WrongFrameSize) {
621 num_audio_samples_10ms *= 2; // 20 ms frame.
622 RTC_EXPECT_DEATH(Encode(), "");
623 num_audio_samples_10ms = 0; // Zero samples.
624 RTC_EXPECT_DEATH(Encode(), "");
625 }
626
TEST_F(AudioEncoderCopyRedDeathTest,NullSpeechEncoder)627 TEST_F(AudioEncoderCopyRedDeathTest, NullSpeechEncoder) {
628 test::ScopedKeyValueConfig field_trials;
629 AudioEncoderCopyRed* red = NULL;
630 AudioEncoderCopyRed::Config config;
631 config.speech_encoder = NULL;
632 RTC_EXPECT_DEATH(
633 red = new AudioEncoderCopyRed(std::move(config), field_trials),
634 "Speech encoder not provided.");
635 // The delete operation is needed to avoid leak reports from memcheck.
636 delete red;
637 }
638
639 #endif // GTEST_HAS_DEATH_TEST && !defined(WEBRTC_ANDROID)
640
641 } // namespace webrtc
642