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