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

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

Issue 2362703002: Adding audio network adaptor to AudioEncoderOpus. (Closed)
Patch Set: some updates Created 4 years, 2 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 <memory> 11 #include <memory>
12 12
13 #include "testing/gtest/include/gtest/gtest.h" 13 #include "testing/gtest/include/gtest/gtest.h"
14 #include "webrtc/base/checks.h" 14 #include "webrtc/base/checks.h"
15 #include "webrtc/common_types.h" 15 #include "webrtc/common_types.h"
16 #include "webrtc/modules/audio_coding/audio_network_adaptor/mock/mock_audio_netw ork_adaptor.h"
16 #include "webrtc/modules/audio_coding/codecs/opus/audio_encoder_opus.h" 17 #include "webrtc/modules/audio_coding/codecs/opus/audio_encoder_opus.h"
17 18
18 namespace webrtc { 19 namespace webrtc {
20 using ::testing::NiceMock;
21 using ::testing::Return;
19 22
20 namespace { 23 namespace {
21 const CodecInst kOpusSettings = {105, "opus", 48000, 960, 1, 32000}; 24
25 const CodecInst kDefaultOpusSettings = {105, "opus", 48000, 960, 1, 32000};
26
27 AudioEncoderOpus::Config CreateConfig(const CodecInst& codec_inst,
28 bool enable_audio_network_adaptor) {
29 AudioEncoderOpus::Config config;
30 config.frame_size_ms = rtc::CheckedDivExact(codec_inst.pacsize, 48);
31 config.num_channels = codec_inst.channels;
32 config.bitrate_bps = rtc::Optional<int>(codec_inst.rate);
33 config.payload_type = codec_inst.pltype;
34 config.application = config.num_channels == 1 ? AudioEncoderOpus::kVoip
35 : AudioEncoderOpus::kAudio;
36 config.audio_network_adaptor_enabled = enable_audio_network_adaptor;
37 return config;
38 }
39
40 struct AudioEncoderOpusStates {
41 MockAudioNetworkAdaptor* mock_audio_network_adaptor;
42 std::unique_ptr<AudioEncoderOpus> encoder;
43 };
44
45 AudioEncoderOpusStates CreateCodec(size_t num_channels,
46 bool enable_audio_network_adaptor = false) {
47 AudioEncoderOpusStates states;
48 std::unique_ptr<MockAudioNetworkAdaptor> mock_audio_network_adaptor(
49 new NiceMock<MockAudioNetworkAdaptor>());
50 EXPECT_CALL(*mock_audio_network_adaptor, Die());
51 states.mock_audio_network_adaptor = mock_audio_network_adaptor.get();
52 CodecInst codec_inst = kDefaultOpusSettings;
53 codec_inst.channels = num_channels;
54 auto config = CreateConfig(codec_inst, enable_audio_network_adaptor);
55 states.encoder.reset(
56 new AudioEncoderOpus(config, std::move(mock_audio_network_adaptor)));
57 return states;
58 }
59
60 AudioNetworkAdaptor::EncoderRuntimeConfig CreateEncoderRuntimeConfig() {
61 constexpr int kBitrate = 40000;
62 constexpr int kFrameLength = 60;
63 constexpr bool kEnableFec = true;
64 constexpr bool kEnableDtx = false;
65 constexpr size_t kNumChannels = 1;
66 constexpr float kPacketLossFraction = 0.1f;
67 AudioNetworkAdaptor::EncoderRuntimeConfig config;
68 config.bitrate_bps = rtc::Optional<int>(kBitrate);
69 config.frame_length_ms = rtc::Optional<int>(kFrameLength);
70 config.enable_fec = rtc::Optional<bool>(kEnableFec);
71 config.enable_dtx = rtc::Optional<bool>(kEnableDtx);
72 config.num_channels = rtc::Optional<size_t>(kNumChannels);
73 config.uplink_packet_loss_fraction =
74 rtc::Optional<float>(kPacketLossFraction);
75 return config;
76 }
77
78 void CheckEncoderRuntimeConfig(
79 const AudioEncoderOpus* encoder,
80 const AudioNetworkAdaptor::EncoderRuntimeConfig& config) {
81 EXPECT_EQ(*config.bitrate_bps, encoder->GetTargetBitrate());
82 EXPECT_EQ(*config.frame_length_ms, encoder->next_frame_length_ms());
83 EXPECT_EQ(*config.enable_fec, encoder->fec_enabled());
84 EXPECT_EQ(*config.enable_dtx, encoder->GetDtx());
85 EXPECT_EQ(*config.num_channels, encoder->num_channels_to_encode());
86 }
87
22 } // namespace 88 } // namespace
23 89
24 class AudioEncoderOpusTest : public ::testing::Test { 90 TEST(AudioEncoderOpusTest, DefaultApplicationModeMono) {
25 protected: 91 auto states = CreateCodec(1);
26 void CreateCodec(int num_channels) { 92 EXPECT_EQ(AudioEncoderOpus::kVoip, states.encoder->application());
27 codec_inst_.channels = num_channels;
28 encoder_.reset(new AudioEncoderOpus(codec_inst_));
29 auto expected_app =
30 num_channels == 1 ? AudioEncoderOpus::kVoip : AudioEncoderOpus::kAudio;
31 EXPECT_EQ(expected_app, encoder_->application());
32 }
33
34 CodecInst codec_inst_ = kOpusSettings;
35 std::unique_ptr<AudioEncoderOpus> encoder_;
36 };
37
38 TEST_F(AudioEncoderOpusTest, DefaultApplicationModeMono) {
39 CreateCodec(1);
40 } 93 }
41 94
42 TEST_F(AudioEncoderOpusTest, DefaultApplicationModeStereo) { 95 TEST(AudioEncoderOpusTest, DefaultApplicationModeStereo) {
43 CreateCodec(2); 96 auto states = CreateCodec(2);
97 EXPECT_EQ(AudioEncoderOpus::kAudio, states.encoder->application());
44 } 98 }
45 99
46 TEST_F(AudioEncoderOpusTest, ChangeApplicationMode) { 100 TEST(AudioEncoderOpusTest, ChangeApplicationMode) {
kwiberg-webrtc 2016/09/27 09:35:55 It's great that you remove the stateful test class
minyue-webrtc 2016/09/27 16:02:33 I thought of using a separate CL, the problem is t
47 CreateCodec(2); 101 auto states = CreateCodec(2);
48 EXPECT_TRUE(encoder_->SetApplication(AudioEncoder::Application::kSpeech)); 102 EXPECT_TRUE(
49 EXPECT_EQ(AudioEncoderOpus::kVoip, encoder_->application()); 103 states.encoder->SetApplication(AudioEncoder::Application::kSpeech));
104 EXPECT_EQ(AudioEncoderOpus::kVoip, states.encoder->application());
50 } 105 }
51 106
52 TEST_F(AudioEncoderOpusTest, ResetWontChangeApplicationMode) { 107 TEST(AudioEncoderOpusTest, ResetWontChangeApplicationMode) {
53 CreateCodec(2); 108 auto states = CreateCodec(2);
54 109
55 // Trigger a reset. 110 // Trigger a reset.
56 encoder_->Reset(); 111 states.encoder->Reset();
57 // Verify that the mode is still kAudio. 112 // Verify that the mode is still kAudio.
58 EXPECT_EQ(AudioEncoderOpus::kAudio, encoder_->application()); 113 EXPECT_EQ(AudioEncoderOpus::kAudio, states.encoder->application());
59 114
60 // Now change to kVoip. 115 // Now change to kVoip.
61 EXPECT_TRUE(encoder_->SetApplication(AudioEncoder::Application::kSpeech)); 116 EXPECT_TRUE(
62 EXPECT_EQ(AudioEncoderOpus::kVoip, encoder_->application()); 117 states.encoder->SetApplication(AudioEncoder::Application::kSpeech));
118 EXPECT_EQ(AudioEncoderOpus::kVoip, states.encoder->application());
63 119
64 // Trigger a reset again. 120 // Trigger a reset again.
65 encoder_->Reset(); 121 states.encoder->Reset();
66 // Verify that the mode is still kVoip. 122 // Verify that the mode is still kVoip.
67 EXPECT_EQ(AudioEncoderOpus::kVoip, encoder_->application()); 123 EXPECT_EQ(AudioEncoderOpus::kVoip, states.encoder->application());
68 } 124 }
69 125
70 TEST_F(AudioEncoderOpusTest, ToggleDtx) { 126 TEST(AudioEncoderOpusTest, ToggleDtx) {
71 CreateCodec(2); 127 auto states = CreateCodec(2);
72 // Enable DTX 128 // Enable DTX
73 EXPECT_TRUE(encoder_->SetDtx(true)); 129 EXPECT_TRUE(states.encoder->SetDtx(true));
74 // Verify that the mode is still kAudio. 130 // Verify that the mode is still kAudio.
75 EXPECT_EQ(AudioEncoderOpus::kAudio, encoder_->application()); 131 EXPECT_EQ(AudioEncoderOpus::kAudio, states.encoder->application());
76 // Turn off DTX. 132 // Turn off DTX.
77 EXPECT_TRUE(encoder_->SetDtx(false)); 133 EXPECT_TRUE(states.encoder->SetDtx(false));
78 } 134 }
79 135
80 TEST_F(AudioEncoderOpusTest, SetBitrate) { 136 TEST(AudioEncoderOpusTest, SetBitrate) {
81 CreateCodec(1); 137 auto states = CreateCodec(1);
82 // Constants are replicated from audio_encoder_opus.cc. 138 // Constants are replicated from audio_states.encoderopus.cc.
83 const int kMinBitrateBps = 500; 139 const int kMinBitrateBps = 500;
84 const int kMaxBitrateBps = 512000; 140 const int kMaxBitrateBps = 512000;
85 // Set a too low bitrate. 141 // Set a too low bitrate.
86 encoder_->SetTargetBitrate(kMinBitrateBps - 1); 142 states.encoder->SetTargetBitrate(kMinBitrateBps - 1);
87 EXPECT_EQ(kMinBitrateBps, encoder_->GetTargetBitrate()); 143 EXPECT_EQ(kMinBitrateBps, states.encoder->GetTargetBitrate());
88 // Set a too high bitrate. 144 // Set a too high bitrate.
89 encoder_->SetTargetBitrate(kMaxBitrateBps + 1); 145 states.encoder->SetTargetBitrate(kMaxBitrateBps + 1);
90 EXPECT_EQ(kMaxBitrateBps, encoder_->GetTargetBitrate()); 146 EXPECT_EQ(kMaxBitrateBps, states.encoder->GetTargetBitrate());
91 // Set the minimum rate. 147 // Set the minimum rate.
92 encoder_->SetTargetBitrate(kMinBitrateBps); 148 states.encoder->SetTargetBitrate(kMinBitrateBps);
93 EXPECT_EQ(kMinBitrateBps, encoder_->GetTargetBitrate()); 149 EXPECT_EQ(kMinBitrateBps, states.encoder->GetTargetBitrate());
94 // Set the maximum rate. 150 // Set the maximum rate.
95 encoder_->SetTargetBitrate(kMaxBitrateBps); 151 states.encoder->SetTargetBitrate(kMaxBitrateBps);
96 EXPECT_EQ(kMaxBitrateBps, encoder_->GetTargetBitrate()); 152 EXPECT_EQ(kMaxBitrateBps, states.encoder->GetTargetBitrate());
97 // Set rates from 1000 up to 32000 bps. 153 // Set rates from 1000 up to 32000 bps.
98 for (int rate = 1000; rate <= 32000; rate += 1000) { 154 for (int rate = 1000; rate <= 32000; rate += 1000) {
99 encoder_->SetTargetBitrate(rate); 155 states.encoder->SetTargetBitrate(rate);
100 EXPECT_EQ(rate, encoder_->GetTargetBitrate()); 156 EXPECT_EQ(rate, states.encoder->GetTargetBitrate());
101 } 157 }
102 } 158 }
103 159
104 namespace { 160 namespace {
105 161
106 // Returns a vector with the n evenly-spaced numbers a, a + (b - a)/(n - 1), 162 // Returns a vector with the n evenly-spaced numbers a, a + (b - a)/(n - 1),
107 // ..., b. 163 // ..., b.
108 std::vector<double> IntervalSteps(double a, double b, size_t n) { 164 std::vector<double> IntervalSteps(double a, double b, size_t n) {
109 RTC_DCHECK_GT(n, 1u); 165 RTC_DCHECK_GT(n, 1u);
110 const double step = (b - a) / (n - 1); 166 const double step = (b - a) / (n - 1);
(...skipping 10 matching lines...) Expand all
121 const std::vector<double>& losses, 177 const std::vector<double>& losses,
122 double expected_return) { 178 double expected_return) {
123 for (double loss : losses) { 179 for (double loss : losses) {
124 encoder->SetProjectedPacketLossRate(loss); 180 encoder->SetProjectedPacketLossRate(loss);
125 EXPECT_DOUBLE_EQ(expected_return, encoder->packet_loss_rate()); 181 EXPECT_DOUBLE_EQ(expected_return, encoder->packet_loss_rate());
126 } 182 }
127 } 183 }
128 184
129 } // namespace 185 } // namespace
130 186
131 TEST_F(AudioEncoderOpusTest, PacketLossRateOptimized) { 187 TEST(AudioEncoderOpusTest, PacketLossRateOptimized) {
132 CreateCodec(1); 188 auto states = CreateCodec(1);
133 auto I = [](double a, double b) { return IntervalSteps(a, b, 10); }; 189 auto I = [](double a, double b) { return IntervalSteps(a, b, 10); };
134 const double eps = 1e-15; 190 const double eps = 1e-15;
135 191
136 // Note that the order of the following calls is critical. 192 // Note that the order of the following calls is critical.
137 193
138 // clang-format off 194 // clang-format off
139 TestSetPacketLossRate(encoder_.get(), I(0.00 , 0.01 - eps), 0.00); 195 TestSetPacketLossRate(states.encoder.get(), I(0.00 , 0.01 - eps), 0.00);
140 TestSetPacketLossRate(encoder_.get(), I(0.01 + eps, 0.06 - eps), 0.01); 196 TestSetPacketLossRate(states.encoder.get(), I(0.01 + eps, 0.06 - eps), 0.01);
141 TestSetPacketLossRate(encoder_.get(), I(0.06 + eps, 0.11 - eps), 0.05); 197 TestSetPacketLossRate(states.encoder.get(), I(0.06 + eps, 0.11 - eps), 0.05);
142 TestSetPacketLossRate(encoder_.get(), I(0.11 + eps, 0.22 - eps), 0.10); 198 TestSetPacketLossRate(states.encoder.get(), I(0.11 + eps, 0.22 - eps), 0.10);
143 TestSetPacketLossRate(encoder_.get(), I(0.22 + eps, 1.00 ), 0.20); 199 TestSetPacketLossRate(states.encoder.get(), I(0.22 + eps, 1.00 ), 0.20);
144 200
145 TestSetPacketLossRate(encoder_.get(), I(1.00 , 0.18 + eps), 0.20); 201 TestSetPacketLossRate(states.encoder.get(), I(1.00 , 0.18 + eps), 0.20);
146 TestSetPacketLossRate(encoder_.get(), I(0.18 - eps, 0.09 + eps), 0.10); 202 TestSetPacketLossRate(states.encoder.get(), I(0.18 - eps, 0.09 + eps), 0.10);
147 TestSetPacketLossRate(encoder_.get(), I(0.09 - eps, 0.04 + eps), 0.05); 203 TestSetPacketLossRate(states.encoder.get(), I(0.09 - eps, 0.04 + eps), 0.05);
148 TestSetPacketLossRate(encoder_.get(), I(0.04 - eps, 0.01 + eps), 0.01); 204 TestSetPacketLossRate(states.encoder.get(), I(0.04 - eps, 0.01 + eps), 0.01);
149 TestSetPacketLossRate(encoder_.get(), I(0.01 - eps, 0.00 ), 0.00); 205 TestSetPacketLossRate(states.encoder.get(), I(0.01 - eps, 0.00 ), 0.00);
150 // clang-format on 206 // clang-format on
151 } 207 }
152 208
209 TEST(AudioEncoderOpusTest, InvokeAudioNetworkAdaptorOnSetUplinkBandwidth) {
210 auto states = CreateCodec(2, true);
211
212 auto config = CreateEncoderRuntimeConfig();
kwiberg-webrtc 2016/09/27 09:35:55 const?
minyue-webrtc 2016/09/27 16:02:33 ok.
minyue-webrtc 2016/09/29 15:34:25 Oh, forgot to add. Will do.
213 EXPECT_CALL(*states.mock_audio_network_adaptor, GetEncoderRuntimeConfig())
214 .WillOnce(Return(config));
215
216 // Since using mock audio network adaptor, any bandwidth value is fine.
217 constexpr int kUplinkBandwidth = 50000;
218 EXPECT_CALL(*states.mock_audio_network_adaptor,
219 SetUplinkBandwidth(kUplinkBandwidth));
220 states.encoder->OnReceivedUplinkBandwidth(kUplinkBandwidth);
221
222 CheckEncoderRuntimeConfig(states.encoder.get(), config);
223 }
224
225 TEST(AudioEncoderOpusTest,
226 InvokeAudioNetworkAdaptorOnSetUplinkPacketLossFraction) {
227 auto states = CreateCodec(2, true);
228
229 auto config = CreateEncoderRuntimeConfig();
230 EXPECT_CALL(*states.mock_audio_network_adaptor, GetEncoderRuntimeConfig())
231 .WillOnce(Return(config));
232
233 // Since using mock audio network adaptor, any packet loss fraction is fine.
234 constexpr float kUplinkPacketLoss = 0.1f;
235 EXPECT_CALL(*states.mock_audio_network_adaptor,
236 SetUplinkPacketLossFraction(kUplinkPacketLoss));
237 states.encoder->OnReceivedUplinkPacketLossFraction(kUplinkPacketLoss);
238
239 CheckEncoderRuntimeConfig(states.encoder.get(), config);
240 }
241
242 TEST(AudioEncoderOpusTest, InvokeAudioNetworkAdaptorOnSetTargetAudioBitrate) {
243 auto states = CreateCodec(2, true);
244
245 auto config = CreateEncoderRuntimeConfig();
246 EXPECT_CALL(*states.mock_audio_network_adaptor, GetEncoderRuntimeConfig())
247 .WillOnce(Return(config));
248
249 // Since using mock audio network adaptor, any target audio bitrate is fine.
250 constexpr int kTargetAudioBitrate = 30000;
251 EXPECT_CALL(*states.mock_audio_network_adaptor,
252 SetTargetAudioBitrate(kTargetAudioBitrate));
253 states.encoder->OnReceivedTargetAudioBitrate(kTargetAudioBitrate);
254
255 CheckEncoderRuntimeConfig(states.encoder.get(), config);
256 }
257
258 TEST(AudioEncoderOpusTest, InvokeAudioNetworkAdaptorOnSetRtt) {
259 auto states = CreateCodec(2, true);
260
261 auto config = CreateEncoderRuntimeConfig();
262 EXPECT_CALL(*states.mock_audio_network_adaptor, GetEncoderRuntimeConfig())
263 .WillOnce(Return(config));
264
265 // Since using mock audio network adaptor, any rtt is fine.
266 constexpr int kRtt = 30;
267 EXPECT_CALL(*states.mock_audio_network_adaptor, SetRtt(kRtt));
268 states.encoder->OnReceivedRtt(kRtt);
269
270 CheckEncoderRuntimeConfig(states.encoder.get(), config);
271 }
272
273 TEST(AudioEncoderOpusTest,
274 InvokeAudioNetworkAdaptorOnSetReceiverFrameLengthRange) {
275 auto states = CreateCodec(2, true);
276
277 auto config = CreateEncoderRuntimeConfig();
278 EXPECT_CALL(*states.mock_audio_network_adaptor, GetEncoderRuntimeConfig())
279 .WillOnce(Return(config));
280
281 constexpr int kMinFrameLength = 10;
282 constexpr int kMaxFrameLength = 60;
283 EXPECT_CALL(*states.mock_audio_network_adaptor,
284 SetReceiverFrameLengthRange(kMinFrameLength, kMaxFrameLength));
285 states.encoder->SetReceiverFrameLengthRange(kMinFrameLength, kMaxFrameLength);
286
287 CheckEncoderRuntimeConfig(states.encoder.get(), config);
288 }
289
153 } // namespace webrtc 290 } // namespace webrtc
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698