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