Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(180)

Side by Side Diff: webrtc/modules/audio_coding/codecs/opus/audio_encoder_opus_unittest.cc

Issue 2668693003: Change opus min bitrate. (Closed)
Patch Set: Fix for AudioDecoderOpusTest.SetTargetBitrate Created 3 years, 10 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
1 /* 1 /*
2 * Copyright (c) 2015 The WebRTC project authors. All Rights Reserved. 2 * Copyright (c) 2015 The WebRTC project authors. All Rights Reserved.
3 * 3 *
4 * Use of this source code is governed by a BSD-style license 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 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 6 * tree. An additional intellectual property rights grant can be found
7 * in the file PATENTS. All contributing project authors may 7 * in the file PATENTS. All contributing project authors may
8 * be found in the AUTHORS file in the root of the source tree. 8 * be found in the AUTHORS file in the root of the source tree.
9 */ 9 */
10 10
11 #include <array> 11 #include <array>
12 #include <memory> 12 #include <memory>
13 13
14 #include "webrtc/base/checks.h" 14 #include "webrtc/base/checks.h"
15 #include "webrtc/base/fakeclock.h" 15 #include "webrtc/base/fakeclock.h"
16 #include "webrtc/common_audio/mocks/mock_smoothing_filter.h" 16 #include "webrtc/common_audio/mocks/mock_smoothing_filter.h"
17 #include "webrtc/common_types.h" 17 #include "webrtc/common_types.h"
18 #include "webrtc/modules/audio_coding/audio_network_adaptor/mock/mock_audio_netw ork_adaptor.h" 18 #include "webrtc/modules/audio_coding/audio_network_adaptor/mock/mock_audio_netw ork_adaptor.h"
19 #include "webrtc/modules/audio_coding/codecs/opus/audio_encoder_opus.h" 19 #include "webrtc/modules/audio_coding/codecs/opus/audio_encoder_opus.h"
20 #include "webrtc/modules/audio_coding/neteq/tools/audio_loop.h"
20 #include "webrtc/test/field_trial.h" 21 #include "webrtc/test/field_trial.h"
21 #include "webrtc/test/gmock.h" 22 #include "webrtc/test/gmock.h"
22 #include "webrtc/test/gtest.h" 23 #include "webrtc/test/gtest.h"
24 #include "webrtc/test/testsupport/fileutils.h"
23 #include "webrtc/system_wrappers/include/clock.h" 25 #include "webrtc/system_wrappers/include/clock.h"
24 26
25 namespace webrtc { 27 namespace webrtc {
26 using ::testing::NiceMock; 28 using ::testing::NiceMock;
27 using ::testing::Return; 29 using ::testing::Return;
28 30
29 namespace { 31 namespace {
30 32
31 const CodecInst kDefaultOpusSettings = {105, "opus", 48000, 960, 1, 32000}; 33 const CodecInst kDefaultOpusSettings = {105, "opus", 48000, 960, 1, 32000};
32 constexpr int64_t kInitialTimeUs = 12345678; 34 constexpr int64_t kInitialTimeUs = 12345678;
(...skipping 73 matching lines...) Expand 10 before | Expand all | Expand 10 after
106 void CheckEncoderRuntimeConfig( 108 void CheckEncoderRuntimeConfig(
107 const AudioEncoderOpus* encoder, 109 const AudioEncoderOpus* encoder,
108 const AudioNetworkAdaptor::EncoderRuntimeConfig& config) { 110 const AudioNetworkAdaptor::EncoderRuntimeConfig& config) {
109 EXPECT_EQ(*config.bitrate_bps, encoder->GetTargetBitrate()); 111 EXPECT_EQ(*config.bitrate_bps, encoder->GetTargetBitrate());
110 EXPECT_EQ(*config.frame_length_ms, encoder->next_frame_length_ms()); 112 EXPECT_EQ(*config.frame_length_ms, encoder->next_frame_length_ms());
111 EXPECT_EQ(*config.enable_fec, encoder->fec_enabled()); 113 EXPECT_EQ(*config.enable_fec, encoder->fec_enabled());
112 EXPECT_EQ(*config.enable_dtx, encoder->GetDtx()); 114 EXPECT_EQ(*config.enable_dtx, encoder->GetDtx());
113 EXPECT_EQ(*config.num_channels, encoder->num_channels_to_encode()); 115 EXPECT_EQ(*config.num_channels, encoder->num_channels_to_encode());
114 } 116 }
115 117
118 // Create 10ms audio data blocks for a total packet size of "packet_size_ms".
119 std::unique_ptr<test::AudioLoop> Create10msAudioBlocks(
120 const std::unique_ptr<AudioEncoderOpus>& encoder,
121 int packet_size_ms) {
122 const std::string file_name =
123 test::ResourcePath("audio_coding/testfile32kHz", "pcm");
124
125 std::unique_ptr<test::AudioLoop> speech_data(new test::AudioLoop());
126 int audio_samples_per_ms =
127 rtc::CheckedDivExact(encoder->SampleRateHz(), 1000);
128 RTC_DCHECK(speech_data->Init(
hlundin-webrtc 2017/03/03 14:32:59 This is wrong for two reasons: 1. It won't call In
minyue-webrtc 2017/03/03 14:40:41 Right, thanks! Weird that the test still passes.
michaelt 2017/03/06 10:09:52 In general i like the idea of separation of test's
hlundin-webrtc 2017/03/06 10:49:13 The test still passes on the bots (Debug and Relea
michaelt 2017/03/06 12:01:32 Acknowledged.
129 file_name,
130 packet_size_ms * audio_samples_per_ms * encoder->num_channels_to_encode(),
131 10 * audio_samples_per_ms * encoder->num_channels_to_encode()));
132 return speech_data;
133 }
134
116 } // namespace 135 } // namespace
117 136
118 TEST(AudioEncoderOpusTest, DefaultApplicationModeMono) { 137 TEST(AudioEncoderOpusTest, DefaultApplicationModeMono) {
119 auto states = CreateCodec(1); 138 auto states = CreateCodec(1);
120 EXPECT_EQ(AudioEncoderOpus::kVoip, states.encoder->application()); 139 EXPECT_EQ(AudioEncoderOpus::kVoip, states.encoder->application());
121 } 140 }
122 141
123 TEST(AudioEncoderOpusTest, DefaultApplicationModeStereo) { 142 TEST(AudioEncoderOpusTest, DefaultApplicationModeStereo) {
124 auto states = CreateCodec(2); 143 auto states = CreateCodec(2);
125 EXPECT_EQ(AudioEncoderOpus::kAudio, states.encoder->application()); 144 EXPECT_EQ(AudioEncoderOpus::kAudio, states.encoder->application());
(...skipping 32 matching lines...) Expand 10 before | Expand all | Expand 10 after
158 // Verify that the mode is still kAudio. 177 // Verify that the mode is still kAudio.
159 EXPECT_EQ(AudioEncoderOpus::kAudio, states.encoder->application()); 178 EXPECT_EQ(AudioEncoderOpus::kAudio, states.encoder->application());
160 // Turn off DTX. 179 // Turn off DTX.
161 EXPECT_TRUE(states.encoder->SetDtx(false)); 180 EXPECT_TRUE(states.encoder->SetDtx(false));
162 } 181 }
163 182
164 TEST(AudioEncoderOpusTest, 183 TEST(AudioEncoderOpusTest,
165 OnReceivedUplinkBandwidthWithoutAudioNetworkAdaptor) { 184 OnReceivedUplinkBandwidthWithoutAudioNetworkAdaptor) {
166 auto states = CreateCodec(1); 185 auto states = CreateCodec(1);
167 // Constants are replicated from audio_states.encoderopus.cc. 186 // Constants are replicated from audio_states.encoderopus.cc.
168 const int kMinBitrateBps = 500; 187 const int kMinBitrateBps = 6000;
169 const int kMaxBitrateBps = 512000; 188 const int kMaxBitrateBps = 512000;
170 // Set a too low bitrate. 189 // Set a too low bitrate.
171 states.encoder->OnReceivedUplinkBandwidth(kMinBitrateBps - 1, 190 states.encoder->OnReceivedUplinkBandwidth(kMinBitrateBps - 1,
172 rtc::Optional<int64_t>()); 191 rtc::Optional<int64_t>());
173 EXPECT_EQ(kMinBitrateBps, states.encoder->GetTargetBitrate()); 192 EXPECT_EQ(kMinBitrateBps, states.encoder->GetTargetBitrate());
174 // Set a too high bitrate. 193 // Set a too high bitrate.
175 states.encoder->OnReceivedUplinkBandwidth(kMaxBitrateBps + 1, 194 states.encoder->OnReceivedUplinkBandwidth(kMaxBitrateBps + 1,
176 rtc::Optional<int64_t>()); 195 rtc::Optional<int64_t>());
177 EXPECT_EQ(kMaxBitrateBps, states.encoder->GetTargetBitrate()); 196 EXPECT_EQ(kMaxBitrateBps, states.encoder->GetTargetBitrate());
178 // Set the minimum rate. 197 // Set the minimum rate.
179 states.encoder->OnReceivedUplinkBandwidth(kMinBitrateBps, 198 states.encoder->OnReceivedUplinkBandwidth(kMinBitrateBps,
180 rtc::Optional<int64_t>()); 199 rtc::Optional<int64_t>());
181 EXPECT_EQ(kMinBitrateBps, states.encoder->GetTargetBitrate()); 200 EXPECT_EQ(kMinBitrateBps, states.encoder->GetTargetBitrate());
182 // Set the maximum rate. 201 // Set the maximum rate.
183 states.encoder->OnReceivedUplinkBandwidth(kMaxBitrateBps, 202 states.encoder->OnReceivedUplinkBandwidth(kMaxBitrateBps,
184 rtc::Optional<int64_t>()); 203 rtc::Optional<int64_t>());
185 EXPECT_EQ(kMaxBitrateBps, states.encoder->GetTargetBitrate()); 204 EXPECT_EQ(kMaxBitrateBps, states.encoder->GetTargetBitrate());
186 // Set rates from 1000 up to 32000 bps. 205 // Set rates from kMaxBitrateBps up to 32000 bps.
187 for (int rate = 1000; rate <= 32000; rate += 1000) { 206 for (int rate = kMinBitrateBps; rate <= 32000; rate += 1000) {
188 states.encoder->OnReceivedUplinkBandwidth(rate, rtc::Optional<int64_t>()); 207 states.encoder->OnReceivedUplinkBandwidth(rate, rtc::Optional<int64_t>());
189 EXPECT_EQ(rate, states.encoder->GetTargetBitrate()); 208 EXPECT_EQ(rate, states.encoder->GetTargetBitrate());
190 } 209 }
191 } 210 }
192 211
193 namespace { 212 namespace {
194 213
195 // Returns a vector with the n evenly-spaced numbers a, a + (b - a)/(n - 1), 214 // Returns a vector with the n evenly-spaced numbers a, a + (b - a)/(n - 1),
196 // ..., b. 215 // ..., b.
197 std::vector<float> IntervalSteps(float a, float b, size_t n) { 216 std::vector<float> IntervalSteps(float a, float b, size_t n) {
(...skipping 193 matching lines...) Expand 10 before | Expand all | Expand 10 after
391 int packet_rate = rtc::CheckedDivExact(48000, kDefaultOpusSettings.pacsize); 410 int packet_rate = rtc::CheckedDivExact(48000, kDefaultOpusSettings.pacsize);
392 EXPECT_EQ(kTargetBitrateBps - 411 EXPECT_EQ(kTargetBitrateBps -
393 8 * static_cast<int>(kOverheadBytesPerPacket) * packet_rate, 412 8 * static_cast<int>(kOverheadBytesPerPacket) * packet_rate,
394 states.encoder->GetTargetBitrate()); 413 states.encoder->GetTargetBitrate());
395 } 414 }
396 415
397 TEST(AudioEncoderOpusTest, BitrateBounded) { 416 TEST(AudioEncoderOpusTest, BitrateBounded) {
398 test::ScopedFieldTrials override_field_trials( 417 test::ScopedFieldTrials override_field_trials(
399 "WebRTC-SendSideBwe-WithOverhead/Enabled/"); 418 "WebRTC-SendSideBwe-WithOverhead/Enabled/");
400 419
401 constexpr int kMinBitrateBps = 500; 420 constexpr int kMinBitrateBps = 6000;
402 constexpr int kMaxBitrateBps = 512000; 421 constexpr int kMaxBitrateBps = 512000;
403 422
404 auto states = CreateCodec(2); 423 auto states = CreateCodec(2);
405 424
406 constexpr size_t kOverheadBytesPerPacket = 64; 425 constexpr size_t kOverheadBytesPerPacket = 64;
407 states.encoder->OnReceivedOverhead(kOverheadBytesPerPacket); 426 states.encoder->OnReceivedOverhead(kOverheadBytesPerPacket);
408 427
409 int packet_rate = rtc::CheckedDivExact(48000, kDefaultOpusSettings.pacsize); 428 int packet_rate = rtc::CheckedDivExact(48000, kDefaultOpusSettings.pacsize);
410 429
411 // Set a target rate that is smaller than |kMinBitrateBps| when overhead is 430 // Set a target rate that is smaller than |kMinBitrateBps| when overhead is
(...skipping 80 matching lines...) Expand 10 before | Expand all | Expand 10 after
492 // Update when it is time to update. 511 // Update when it is time to update.
493 EXPECT_CALL(*states.mock_bitrate_smoother, GetAverage()) 512 EXPECT_CALL(*states.mock_bitrate_smoother, GetAverage())
494 .WillOnce(Return(rtc::Optional<float>(40000))); 513 .WillOnce(Return(rtc::Optional<float>(40000)));
495 EXPECT_CALL(**states.mock_audio_network_adaptor, SetUplinkBandwidth(40000)); 514 EXPECT_CALL(**states.mock_audio_network_adaptor, SetUplinkBandwidth(40000));
496 fake_clock.AdvanceTime(rtc::TimeDelta::FromMilliseconds(1)); 515 fake_clock.AdvanceTime(rtc::TimeDelta::FromMilliseconds(1));
497 states.encoder->Encode( 516 states.encoder->Encode(
498 0, rtc::ArrayView<const int16_t>(audio.data(), audio.size()), &encoded); 517 0, rtc::ArrayView<const int16_t>(audio.data(), audio.size()), &encoded);
499 } 518 }
500 } 519 }
501 520
521 TEST(AudioEncoderOpusTest, EncodeAtMinBitrate) {
522 auto states = CreateCodec(1);
523 constexpr int kNumPacketsToEncode = 2;
524 auto audio_frames =
525 Create10msAudioBlocks(states.encoder, kNumPacketsToEncode * 20);
526 rtc::Buffer encoded;
527 uint32_t rtp_timestamp = 12345; // Just a number not important to this test.
528
529 states.encoder->OnReceivedUplinkBandwidth(0, rtc::Optional<int64_t>());
530 for (int packet_index = 0; packet_index < kNumPacketsToEncode;
531 packet_index++) {
532 // Make sure we are not encoding before we have enough data for
533 // a 20ms packet.
534 for (int index = 0; index < 1; index++) {
535 states.encoder->Encode(rtp_timestamp, audio_frames->GetNextBlock(),
536 &encoded);
537 EXPECT_EQ(0u, encoded.size());
538 }
539
540 // Should encode now.
541 states.encoder->Encode(rtp_timestamp, audio_frames->GetNextBlock(),
542 &encoded);
543 EXPECT_GT(encoded.size(), 0u);
544 encoded.Clear();
545 }
546 }
547
502 } // namespace webrtc 548 } // namespace webrtc
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698