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

Side by Side Diff: talk/media/webrtc/webrtcvoiceengine_unittest.cc

Issue 1587193006: Move talk/media to webrtc/media (Closed) Base URL: https://chromium.googlesource.com/external/webrtc.git@master
Patch Set: Rebased to b647aca12a884a13c1728118586245399b55fa3d (#11493) Created 4 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
« no previous file with comments | « talk/media/webrtc/webrtcvoiceengine.cc ('k') | talk/session/media/audiomonitor.h » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
(Empty)
1 /*
2 * libjingle
3 * Copyright 2008 Google Inc.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions are met:
7 *
8 * 1. Redistributions of source code must retain the above copyright notice,
9 * this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright notice,
11 * this list of conditions and the following disclaimer in the documentation
12 * and/or other materials provided with the distribution.
13 * 3. The name of the author may not be used to endorse or promote products
14 * derived from this software without specific prior written permission.
15 *
16 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED
17 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
18 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO
19 * EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
20 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
21 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
22 * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
23 * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
24 * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
25 * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26 */
27
28 #include "webrtc/base/arraysize.h"
29 #include "webrtc/base/byteorder.h"
30 #include "webrtc/base/gunit.h"
31 #include "webrtc/call.h"
32 #include "webrtc/p2p/base/faketransportcontroller.h"
33 #include "webrtc/test/field_trial.h"
34 #include "talk/media/base/constants.h"
35 #include "talk/media/base/fakemediaengine.h"
36 #include "talk/media/base/fakenetworkinterface.h"
37 #include "talk/media/base/fakertp.h"
38 #include "talk/media/webrtc/fakewebrtccall.h"
39 #include "talk/media/webrtc/fakewebrtcvoiceengine.h"
40 #include "talk/media/webrtc/webrtcvoiceengine.h"
41 #include "talk/session/media/channel.h"
42
43 using cricket::kRtpAudioLevelHeaderExtension;
44 using cricket::kRtpAbsoluteSenderTimeHeaderExtension;
45
46 namespace {
47
48 const cricket::AudioCodec kPcmuCodec(0, "PCMU", 8000, 64000, 1, 0);
49 const cricket::AudioCodec kIsacCodec(103, "ISAC", 16000, 32000, 1, 0);
50 const cricket::AudioCodec kOpusCodec(111, "opus", 48000, 64000, 2, 0);
51 const cricket::AudioCodec kG722CodecVoE(9, "G722", 16000, 64000, 1, 0);
52 const cricket::AudioCodec kG722CodecSdp(9, "G722", 8000, 64000, 1, 0);
53 const cricket::AudioCodec kRedCodec(117, "red", 8000, 0, 1, 0);
54 const cricket::AudioCodec kCn8000Codec(13, "CN", 8000, 0, 1, 0);
55 const cricket::AudioCodec kCn16000Codec(105, "CN", 16000, 0, 1, 0);
56 const cricket::AudioCodec kTelephoneEventCodec(106, "telephone-event", 8000, 0,
57 1, 0);
58 const uint32_t kSsrc1 = 0x99;
59 const uint32_t kSsrc2 = 0x98;
60 const uint32_t kSsrcs4[] = { 1, 2, 3, 4 };
61
62 class FakeVoEWrapper : public cricket::VoEWrapper {
63 public:
64 explicit FakeVoEWrapper(cricket::FakeWebRtcVoiceEngine* engine)
65 : cricket::VoEWrapper(engine, // processing
66 engine, // base
67 engine, // codec
68 engine, // hw
69 engine, // network
70 engine, // rtp
71 engine) { // volume
72 }
73 };
74 } // namespace
75
76 class FakeAudioSink : public webrtc::AudioSinkInterface {
77 public:
78 void OnData(const Data& audio) override {}
79 };
80
81 class WebRtcVoiceEngineTestFake : public testing::Test {
82 public:
83 WebRtcVoiceEngineTestFake() : WebRtcVoiceEngineTestFake("") {}
84
85 explicit WebRtcVoiceEngineTestFake(const char* field_trials)
86 : call_(webrtc::Call::Config()),
87 engine_(new FakeVoEWrapper(&voe_)),
88 channel_(nullptr),
89 override_field_trials_(field_trials) {
90 send_parameters_.codecs.push_back(kPcmuCodec);
91 recv_parameters_.codecs.push_back(kPcmuCodec);
92 }
93 bool SetupEngine() {
94 if (!engine_.Init(rtc::Thread::Current())) {
95 return false;
96 }
97 channel_ = engine_.CreateChannel(&call_, cricket::AudioOptions());
98 return (channel_ != nullptr);
99 }
100 bool SetupEngineWithRecvStream() {
101 if (!SetupEngine()) {
102 return false;
103 }
104 return channel_->AddRecvStream(
105 cricket::StreamParams::CreateLegacy(kSsrc1));
106 }
107 bool SetupEngineWithSendStream() {
108 if (!SetupEngine()) {
109 return false;
110 }
111 return channel_->AddSendStream(
112 cricket::StreamParams::CreateLegacy(kSsrc1));
113 }
114 void SetupForMultiSendStream() {
115 EXPECT_TRUE(SetupEngineWithSendStream());
116 // Remove stream added in Setup.
117 EXPECT_TRUE(call_.GetAudioSendStream(kSsrc1));
118 EXPECT_TRUE(channel_->RemoveSendStream(kSsrc1));
119 // Verify the channel does not exist.
120 EXPECT_FALSE(call_.GetAudioSendStream(kSsrc1));
121 }
122 void DeliverPacket(const void* data, int len) {
123 rtc::Buffer packet(reinterpret_cast<const uint8_t*>(data), len);
124 channel_->OnPacketReceived(&packet, rtc::PacketTime());
125 }
126 void TearDown() override {
127 delete channel_;
128 engine_.Terminate();
129 }
130
131 const cricket::FakeAudioSendStream& GetSendStream(uint32_t ssrc) {
132 const auto* send_stream = call_.GetAudioSendStream(ssrc);
133 EXPECT_TRUE(send_stream);
134 return *send_stream;
135 }
136
137 const cricket::FakeAudioReceiveStream& GetRecvStream(uint32_t ssrc) {
138 const auto* recv_stream = call_.GetAudioReceiveStream(ssrc);
139 EXPECT_TRUE(recv_stream);
140 return *recv_stream;
141 }
142
143 const webrtc::AudioSendStream::Config& GetSendStreamConfig(uint32_t ssrc) {
144 const auto* send_stream = call_.GetAudioSendStream(ssrc);
145 EXPECT_TRUE(send_stream);
146 return send_stream->GetConfig();
147 }
148
149 const webrtc::AudioReceiveStream::Config& GetRecvStreamConfig(uint32_t ssrc) {
150 const auto* recv_stream = call_.GetAudioReceiveStream(ssrc);
151 EXPECT_TRUE(recv_stream);
152 return recv_stream->GetConfig();
153 }
154
155 void TestInsertDtmf(uint32_t ssrc, bool caller) {
156 EXPECT_TRUE(engine_.Init(rtc::Thread::Current()));
157 channel_ = engine_.CreateChannel(&call_, cricket::AudioOptions());
158 EXPECT_TRUE(channel_ != nullptr);
159 if (caller) {
160 // If this is a caller, local description will be applied and add the
161 // send stream.
162 EXPECT_TRUE(channel_->AddSendStream(
163 cricket::StreamParams::CreateLegacy(kSsrc1)));
164 }
165
166 // Test we can only InsertDtmf when the other side supports telephone-event.
167 EXPECT_TRUE(channel_->SetSendParameters(send_parameters_));
168 EXPECT_TRUE(channel_->SetSend(cricket::SEND_MICROPHONE));
169 EXPECT_FALSE(channel_->CanInsertDtmf());
170 EXPECT_FALSE(channel_->InsertDtmf(ssrc, 1, 111));
171 send_parameters_.codecs.push_back(kTelephoneEventCodec);
172 EXPECT_TRUE(channel_->SetSendParameters(send_parameters_));
173 EXPECT_TRUE(channel_->CanInsertDtmf());
174
175 if (!caller) {
176 // If this is callee, there's no active send channel yet.
177 EXPECT_FALSE(channel_->InsertDtmf(ssrc, 2, 123));
178 EXPECT_TRUE(channel_->AddSendStream(
179 cricket::StreamParams::CreateLegacy(kSsrc1)));
180 }
181
182 // Check we fail if the ssrc is invalid.
183 EXPECT_FALSE(channel_->InsertDtmf(-1, 1, 111));
184
185 // Test send.
186 cricket::FakeAudioSendStream::TelephoneEvent telephone_event =
187 GetSendStream(kSsrc1).GetLatestTelephoneEvent();
188 EXPECT_EQ(-1, telephone_event.payload_type);
189 EXPECT_TRUE(channel_->InsertDtmf(ssrc, 2, 123));
190 telephone_event = GetSendStream(kSsrc1).GetLatestTelephoneEvent();
191 EXPECT_EQ(kTelephoneEventCodec.id, telephone_event.payload_type);
192 EXPECT_EQ(2, telephone_event.event_code);
193 EXPECT_EQ(123, telephone_event.duration_ms);
194 }
195
196 // Test that send bandwidth is set correctly.
197 // |codec| is the codec under test.
198 // |max_bitrate| is a parameter to set to SetMaxSendBandwidth().
199 // |expected_result| is the expected result from SetMaxSendBandwidth().
200 // |expected_bitrate| is the expected audio bitrate afterward.
201 void TestSendBandwidth(const cricket::AudioCodec& codec,
202 int max_bitrate,
203 bool expected_result,
204 int expected_bitrate) {
205 cricket::AudioSendParameters parameters;
206 parameters.codecs.push_back(codec);
207 parameters.max_bandwidth_bps = max_bitrate;
208 EXPECT_EQ(expected_result, channel_->SetSendParameters(parameters));
209
210 int channel_num = voe_.GetLastChannel();
211 webrtc::CodecInst temp_codec;
212 EXPECT_FALSE(voe_.GetSendCodec(channel_num, temp_codec));
213 EXPECT_EQ(expected_bitrate, temp_codec.rate);
214 }
215
216 void TestSetSendRtpHeaderExtensions(const std::string& ext) {
217 EXPECT_TRUE(SetupEngineWithSendStream());
218
219 // Ensure extensions are off by default.
220 EXPECT_EQ(0u, GetSendStreamConfig(kSsrc1).rtp.extensions.size());
221
222 // Ensure unknown extensions won't cause an error.
223 send_parameters_.extensions.push_back(cricket::RtpHeaderExtension(
224 "urn:ietf:params:unknownextention", 1));
225 EXPECT_TRUE(channel_->SetSendParameters(send_parameters_));
226 EXPECT_EQ(0u, GetSendStreamConfig(kSsrc1).rtp.extensions.size());
227
228 // Ensure extensions stay off with an empty list of headers.
229 send_parameters_.extensions.clear();
230 EXPECT_TRUE(channel_->SetSendParameters(send_parameters_));
231 EXPECT_EQ(0u, GetSendStreamConfig(kSsrc1).rtp.extensions.size());
232
233 // Ensure extension is set properly.
234 const int id = 1;
235 send_parameters_.extensions.push_back(cricket::RtpHeaderExtension(ext, id));
236 EXPECT_TRUE(channel_->SetSendParameters(send_parameters_));
237 EXPECT_EQ(1u, GetSendStreamConfig(kSsrc1).rtp.extensions.size());
238 EXPECT_EQ(ext, GetSendStreamConfig(kSsrc1).rtp.extensions[0].name);
239 EXPECT_EQ(id, GetSendStreamConfig(kSsrc1).rtp.extensions[0].id);
240
241 // Ensure extension is set properly on new stream.
242 EXPECT_TRUE(channel_->AddSendStream(
243 cricket::StreamParams::CreateLegacy(kSsrc2)));
244 EXPECT_NE(call_.GetAudioSendStream(kSsrc1),
245 call_.GetAudioSendStream(kSsrc2));
246 EXPECT_EQ(1u, GetSendStreamConfig(kSsrc2).rtp.extensions.size());
247 EXPECT_EQ(ext, GetSendStreamConfig(kSsrc2).rtp.extensions[0].name);
248 EXPECT_EQ(id, GetSendStreamConfig(kSsrc2).rtp.extensions[0].id);
249
250 // Ensure all extensions go back off with an empty list.
251 send_parameters_.codecs.push_back(kPcmuCodec);
252 send_parameters_.extensions.clear();
253 EXPECT_TRUE(channel_->SetSendParameters(send_parameters_));
254 EXPECT_EQ(0u, GetSendStreamConfig(kSsrc1).rtp.extensions.size());
255 EXPECT_EQ(0u, GetSendStreamConfig(kSsrc2).rtp.extensions.size());
256 }
257
258 void TestSetRecvRtpHeaderExtensions(const std::string& ext) {
259 EXPECT_TRUE(SetupEngineWithRecvStream());
260
261 // Ensure extensions are off by default.
262 EXPECT_EQ(0u, GetRecvStreamConfig(kSsrc1).rtp.extensions.size());
263
264 // Ensure unknown extensions won't cause an error.
265 recv_parameters_.extensions.push_back(cricket::RtpHeaderExtension(
266 "urn:ietf:params:unknownextention", 1));
267 EXPECT_TRUE(channel_->SetRecvParameters(recv_parameters_));
268 EXPECT_EQ(0u, GetRecvStreamConfig(kSsrc1).rtp.extensions.size());
269
270 // Ensure extensions stay off with an empty list of headers.
271 recv_parameters_.extensions.clear();
272 EXPECT_TRUE(channel_->SetRecvParameters(recv_parameters_));
273 EXPECT_EQ(0u, GetRecvStreamConfig(kSsrc1).rtp.extensions.size());
274
275 // Ensure extension is set properly.
276 const int id = 2;
277 recv_parameters_.extensions.push_back(cricket::RtpHeaderExtension(ext, id));
278 EXPECT_TRUE(channel_->SetRecvParameters(recv_parameters_));
279 EXPECT_EQ(1u, GetRecvStreamConfig(kSsrc1).rtp.extensions.size());
280 EXPECT_EQ(ext, GetRecvStreamConfig(kSsrc1).rtp.extensions[0].name);
281 EXPECT_EQ(id, GetRecvStreamConfig(kSsrc1).rtp.extensions[0].id);
282
283 // Ensure extension is set properly on new stream.
284 EXPECT_TRUE(channel_->AddRecvStream(
285 cricket::StreamParams::CreateLegacy(kSsrc2)));
286 EXPECT_NE(call_.GetAudioReceiveStream(kSsrc1),
287 call_.GetAudioReceiveStream(kSsrc2));
288 EXPECT_EQ(1u, GetRecvStreamConfig(kSsrc2).rtp.extensions.size());
289 EXPECT_EQ(ext, GetRecvStreamConfig(kSsrc2).rtp.extensions[0].name);
290 EXPECT_EQ(id, GetRecvStreamConfig(kSsrc2).rtp.extensions[0].id);
291
292 // Ensure all extensions go back off with an empty list.
293 recv_parameters_.extensions.clear();
294 EXPECT_TRUE(channel_->SetRecvParameters(recv_parameters_));
295 EXPECT_EQ(0u, GetRecvStreamConfig(kSsrc1).rtp.extensions.size());
296 EXPECT_EQ(0u, GetRecvStreamConfig(kSsrc2).rtp.extensions.size());
297 }
298
299 webrtc::AudioSendStream::Stats GetAudioSendStreamStats() const {
300 webrtc::AudioSendStream::Stats stats;
301 stats.local_ssrc = 12;
302 stats.bytes_sent = 345;
303 stats.packets_sent = 678;
304 stats.packets_lost = 9012;
305 stats.fraction_lost = 34.56f;
306 stats.codec_name = "codec_name_send";
307 stats.ext_seqnum = 789;
308 stats.jitter_ms = 12;
309 stats.rtt_ms = 345;
310 stats.audio_level = 678;
311 stats.aec_quality_min = 9.01f;
312 stats.echo_delay_median_ms = 234;
313 stats.echo_delay_std_ms = 567;
314 stats.echo_return_loss = 890;
315 stats.echo_return_loss_enhancement = 1234;
316 stats.typing_noise_detected = true;
317 return stats;
318 }
319 void SetAudioSendStreamStats() {
320 for (auto* s : call_.GetAudioSendStreams()) {
321 s->SetStats(GetAudioSendStreamStats());
322 }
323 }
324 void VerifyVoiceSenderInfo(const cricket::VoiceSenderInfo& info,
325 bool is_sending) {
326 const auto stats = GetAudioSendStreamStats();
327 EXPECT_EQ(info.ssrc(), stats.local_ssrc);
328 EXPECT_EQ(info.bytes_sent, stats.bytes_sent);
329 EXPECT_EQ(info.packets_sent, stats.packets_sent);
330 EXPECT_EQ(info.packets_lost, stats.packets_lost);
331 EXPECT_EQ(info.fraction_lost, stats.fraction_lost);
332 EXPECT_EQ(info.codec_name, stats.codec_name);
333 EXPECT_EQ(info.ext_seqnum, stats.ext_seqnum);
334 EXPECT_EQ(info.jitter_ms, stats.jitter_ms);
335 EXPECT_EQ(info.rtt_ms, stats.rtt_ms);
336 EXPECT_EQ(info.audio_level, stats.audio_level);
337 EXPECT_EQ(info.aec_quality_min, stats.aec_quality_min);
338 EXPECT_EQ(info.echo_delay_median_ms, stats.echo_delay_median_ms);
339 EXPECT_EQ(info.echo_delay_std_ms, stats.echo_delay_std_ms);
340 EXPECT_EQ(info.echo_return_loss, stats.echo_return_loss);
341 EXPECT_EQ(info.echo_return_loss_enhancement,
342 stats.echo_return_loss_enhancement);
343 EXPECT_EQ(info.typing_noise_detected,
344 stats.typing_noise_detected && is_sending);
345 }
346
347 webrtc::AudioReceiveStream::Stats GetAudioReceiveStreamStats() const {
348 webrtc::AudioReceiveStream::Stats stats;
349 stats.remote_ssrc = 123;
350 stats.bytes_rcvd = 456;
351 stats.packets_rcvd = 768;
352 stats.packets_lost = 101;
353 stats.fraction_lost = 23.45f;
354 stats.codec_name = "codec_name_recv";
355 stats.ext_seqnum = 678;
356 stats.jitter_ms = 901;
357 stats.jitter_buffer_ms = 234;
358 stats.jitter_buffer_preferred_ms = 567;
359 stats.delay_estimate_ms = 890;
360 stats.audio_level = 1234;
361 stats.expand_rate = 5.67f;
362 stats.speech_expand_rate = 8.90f;
363 stats.secondary_decoded_rate = 1.23f;
364 stats.accelerate_rate = 4.56f;
365 stats.preemptive_expand_rate = 7.89f;
366 stats.decoding_calls_to_silence_generator = 12;
367 stats.decoding_calls_to_neteq = 345;
368 stats.decoding_normal = 67890;
369 stats.decoding_plc = 1234;
370 stats.decoding_cng = 5678;
371 stats.decoding_plc_cng = 9012;
372 stats.capture_start_ntp_time_ms = 3456;
373 return stats;
374 }
375 void SetAudioReceiveStreamStats() {
376 for (auto* s : call_.GetAudioReceiveStreams()) {
377 s->SetStats(GetAudioReceiveStreamStats());
378 }
379 }
380 void VerifyVoiceReceiverInfo(const cricket::VoiceReceiverInfo& info) {
381 const auto stats = GetAudioReceiveStreamStats();
382 EXPECT_EQ(info.ssrc(), stats.remote_ssrc);
383 EXPECT_EQ(info.bytes_rcvd, stats.bytes_rcvd);
384 EXPECT_EQ(info.packets_rcvd, stats.packets_rcvd);
385 EXPECT_EQ(info.packets_lost, stats.packets_lost);
386 EXPECT_EQ(info.fraction_lost, stats.fraction_lost);
387 EXPECT_EQ(info.codec_name, stats.codec_name);
388 EXPECT_EQ(info.ext_seqnum, stats.ext_seqnum);
389 EXPECT_EQ(info.jitter_ms, stats.jitter_ms);
390 EXPECT_EQ(info.jitter_buffer_ms, stats.jitter_buffer_ms);
391 EXPECT_EQ(info.jitter_buffer_preferred_ms,
392 stats.jitter_buffer_preferred_ms);
393 EXPECT_EQ(info.delay_estimate_ms, stats.delay_estimate_ms);
394 EXPECT_EQ(info.audio_level, stats.audio_level);
395 EXPECT_EQ(info.expand_rate, stats.expand_rate);
396 EXPECT_EQ(info.speech_expand_rate, stats.speech_expand_rate);
397 EXPECT_EQ(info.secondary_decoded_rate, stats.secondary_decoded_rate);
398 EXPECT_EQ(info.accelerate_rate, stats.accelerate_rate);
399 EXPECT_EQ(info.preemptive_expand_rate, stats.preemptive_expand_rate);
400 EXPECT_EQ(info.decoding_calls_to_silence_generator,
401 stats.decoding_calls_to_silence_generator);
402 EXPECT_EQ(info.decoding_calls_to_neteq, stats.decoding_calls_to_neteq);
403 EXPECT_EQ(info.decoding_normal, stats.decoding_normal);
404 EXPECT_EQ(info.decoding_plc, stats.decoding_plc);
405 EXPECT_EQ(info.decoding_cng, stats.decoding_cng);
406 EXPECT_EQ(info.decoding_plc_cng, stats.decoding_plc_cng);
407 EXPECT_EQ(info.capture_start_ntp_time_ms, stats.capture_start_ntp_time_ms);
408 }
409
410 protected:
411 cricket::FakeCall call_;
412 cricket::FakeWebRtcVoiceEngine voe_;
413 cricket::WebRtcVoiceEngine engine_;
414 cricket::VoiceMediaChannel* channel_;
415 cricket::AudioSendParameters send_parameters_;
416 cricket::AudioRecvParameters recv_parameters_;
417
418 private:
419 webrtc::test::ScopedFieldTrials override_field_trials_;
420 };
421
422 // Tests that our stub library "works".
423 TEST_F(WebRtcVoiceEngineTestFake, StartupShutdown) {
424 EXPECT_FALSE(voe_.IsInited());
425 EXPECT_TRUE(engine_.Init(rtc::Thread::Current()));
426 EXPECT_TRUE(voe_.IsInited());
427 engine_.Terminate();
428 EXPECT_FALSE(voe_.IsInited());
429 }
430
431 // Tests that we can create and destroy a channel.
432 TEST_F(WebRtcVoiceEngineTestFake, CreateChannel) {
433 EXPECT_TRUE(engine_.Init(rtc::Thread::Current()));
434 channel_ = engine_.CreateChannel(&call_, cricket::AudioOptions());
435 EXPECT_TRUE(channel_ != nullptr);
436 }
437
438 // Tests that the list of supported codecs is created properly and ordered
439 // correctly
440 TEST_F(WebRtcVoiceEngineTestFake, CodecPreference) {
441 const std::vector<cricket::AudioCodec>& codecs = engine_.codecs();
442 ASSERT_FALSE(codecs.empty());
443 EXPECT_STRCASEEQ("opus", codecs[0].name.c_str());
444 EXPECT_EQ(48000, codecs[0].clockrate);
445 EXPECT_EQ(2, codecs[0].channels);
446 EXPECT_EQ(64000, codecs[0].bitrate);
447 int pref = codecs[0].preference;
448 for (size_t i = 1; i < codecs.size(); ++i) {
449 EXPECT_GT(pref, codecs[i].preference);
450 pref = codecs[i].preference;
451 }
452 }
453
454 TEST_F(WebRtcVoiceEngineTestFake, OpusSupportsTransportCc) {
455 const std::vector<cricket::AudioCodec>& codecs = engine_.codecs();
456 bool opus_found = false;
457 for (cricket::AudioCodec codec : codecs) {
458 if (codec.name == "opus") {
459 EXPECT_TRUE(HasTransportCc(codec));
460 opus_found = true;
461 }
462 }
463 EXPECT_TRUE(opus_found);
464 }
465
466 // Tests that we can find codecs by name or id, and that we interpret the
467 // clockrate and bitrate fields properly.
468 TEST_F(WebRtcVoiceEngineTestFake, FindCodec) {
469 cricket::AudioCodec codec;
470 webrtc::CodecInst codec_inst;
471 // Find PCMU with explicit clockrate and bitrate.
472 EXPECT_TRUE(cricket::WebRtcVoiceEngine::ToCodecInst(kPcmuCodec, &codec_inst));
473 // Find ISAC with explicit clockrate and 0 bitrate.
474 EXPECT_TRUE(cricket::WebRtcVoiceEngine::ToCodecInst(kIsacCodec, &codec_inst));
475 // Find telephone-event with explicit clockrate and 0 bitrate.
476 EXPECT_TRUE(cricket::WebRtcVoiceEngine::ToCodecInst(kTelephoneEventCodec,
477 &codec_inst));
478 // Find ISAC with a different payload id.
479 codec = kIsacCodec;
480 codec.id = 127;
481 EXPECT_TRUE(cricket::WebRtcVoiceEngine::ToCodecInst(codec, &codec_inst));
482 EXPECT_EQ(codec.id, codec_inst.pltype);
483 // Find PCMU with a 0 clockrate.
484 codec = kPcmuCodec;
485 codec.clockrate = 0;
486 EXPECT_TRUE(cricket::WebRtcVoiceEngine::ToCodecInst(codec, &codec_inst));
487 EXPECT_EQ(codec.id, codec_inst.pltype);
488 EXPECT_EQ(8000, codec_inst.plfreq);
489 // Find PCMU with a 0 bitrate.
490 codec = kPcmuCodec;
491 codec.bitrate = 0;
492 EXPECT_TRUE(cricket::WebRtcVoiceEngine::ToCodecInst(codec, &codec_inst));
493 EXPECT_EQ(codec.id, codec_inst.pltype);
494 EXPECT_EQ(64000, codec_inst.rate);
495 // Find ISAC with an explicit bitrate.
496 codec = kIsacCodec;
497 codec.bitrate = 32000;
498 EXPECT_TRUE(cricket::WebRtcVoiceEngine::ToCodecInst(codec, &codec_inst));
499 EXPECT_EQ(codec.id, codec_inst.pltype);
500 EXPECT_EQ(32000, codec_inst.rate);
501 }
502
503 // Test that we set our inbound codecs properly, including changing PT.
504 TEST_F(WebRtcVoiceEngineTestFake, SetRecvCodecs) {
505 EXPECT_TRUE(SetupEngine());
506 cricket::AudioRecvParameters parameters;
507 parameters.codecs.push_back(kIsacCodec);
508 parameters.codecs.push_back(kPcmuCodec);
509 parameters.codecs.push_back(kTelephoneEventCodec);
510 parameters.codecs[0].id = 106; // collide with existing telephone-event
511 parameters.codecs[2].id = 126;
512 EXPECT_TRUE(channel_->SetRecvParameters(parameters));
513 EXPECT_TRUE(channel_->AddRecvStream(
514 cricket::StreamParams::CreateLegacy(kSsrc1)));
515 int channel_num = voe_.GetLastChannel();
516 webrtc::CodecInst gcodec;
517 rtc::strcpyn(gcodec.plname, arraysize(gcodec.plname), "ISAC");
518 gcodec.plfreq = 16000;
519 gcodec.channels = 1;
520 EXPECT_EQ(0, voe_.GetRecPayloadType(channel_num, gcodec));
521 EXPECT_EQ(106, gcodec.pltype);
522 EXPECT_STREQ("ISAC", gcodec.plname);
523 rtc::strcpyn(gcodec.plname, arraysize(gcodec.plname), "telephone-event");
524 gcodec.plfreq = 8000;
525 EXPECT_EQ(0, voe_.GetRecPayloadType(channel_num, gcodec));
526 EXPECT_EQ(126, gcodec.pltype);
527 EXPECT_STREQ("telephone-event", gcodec.plname);
528 }
529
530 // Test that we fail to set an unknown inbound codec.
531 TEST_F(WebRtcVoiceEngineTestFake, SetRecvCodecsUnsupportedCodec) {
532 EXPECT_TRUE(SetupEngine());
533 cricket::AudioRecvParameters parameters;
534 parameters.codecs.push_back(kIsacCodec);
535 parameters.codecs.push_back(cricket::AudioCodec(127, "XYZ", 32000, 0, 1, 0));
536 EXPECT_FALSE(channel_->SetRecvParameters(parameters));
537 }
538
539 // Test that we fail if we have duplicate types in the inbound list.
540 TEST_F(WebRtcVoiceEngineTestFake, SetRecvCodecsDuplicatePayloadType) {
541 EXPECT_TRUE(SetupEngine());
542 cricket::AudioRecvParameters parameters;
543 parameters.codecs.push_back(kIsacCodec);
544 parameters.codecs.push_back(kCn16000Codec);
545 parameters.codecs[1].id = kIsacCodec.id;
546 EXPECT_FALSE(channel_->SetRecvParameters(parameters));
547 }
548
549 // Test that we can decode OPUS without stereo parameters.
550 TEST_F(WebRtcVoiceEngineTestFake, SetRecvCodecsWithOpusNoStereo) {
551 EXPECT_TRUE(SetupEngine());
552 cricket::AudioRecvParameters parameters;
553 parameters.codecs.push_back(kIsacCodec);
554 parameters.codecs.push_back(kPcmuCodec);
555 parameters.codecs.push_back(kOpusCodec);
556 EXPECT_TRUE(channel_->SetRecvParameters(parameters));
557 EXPECT_TRUE(channel_->AddRecvStream(
558 cricket::StreamParams::CreateLegacy(kSsrc1)));
559 int channel_num = voe_.GetLastChannel();
560 webrtc::CodecInst opus;
561 cricket::WebRtcVoiceEngine::ToCodecInst(kOpusCodec, &opus);
562 // Even without stereo parameters, recv codecs still specify channels = 2.
563 EXPECT_EQ(2, opus.channels);
564 EXPECT_EQ(111, opus.pltype);
565 EXPECT_STREQ("opus", opus.plname);
566 opus.pltype = 0;
567 EXPECT_EQ(0, voe_.GetRecPayloadType(channel_num, opus));
568 EXPECT_EQ(111, opus.pltype);
569 }
570
571 // Test that we can decode OPUS with stereo = 0.
572 TEST_F(WebRtcVoiceEngineTestFake, SetRecvCodecsWithOpus0Stereo) {
573 EXPECT_TRUE(SetupEngine());
574 cricket::AudioRecvParameters parameters;
575 parameters.codecs.push_back(kIsacCodec);
576 parameters.codecs.push_back(kPcmuCodec);
577 parameters.codecs.push_back(kOpusCodec);
578 parameters.codecs[2].params["stereo"] = "0";
579 EXPECT_TRUE(channel_->SetRecvParameters(parameters));
580 EXPECT_TRUE(channel_->AddRecvStream(
581 cricket::StreamParams::CreateLegacy(kSsrc1)));
582 int channel_num2 = voe_.GetLastChannel();
583 webrtc::CodecInst opus;
584 cricket::WebRtcVoiceEngine::ToCodecInst(kOpusCodec, &opus);
585 // Even when stereo is off, recv codecs still specify channels = 2.
586 EXPECT_EQ(2, opus.channels);
587 EXPECT_EQ(111, opus.pltype);
588 EXPECT_STREQ("opus", opus.plname);
589 opus.pltype = 0;
590 EXPECT_EQ(0, voe_.GetRecPayloadType(channel_num2, opus));
591 EXPECT_EQ(111, opus.pltype);
592 }
593
594 // Test that we can decode OPUS with stereo = 1.
595 TEST_F(WebRtcVoiceEngineTestFake, SetRecvCodecsWithOpus1Stereo) {
596 EXPECT_TRUE(SetupEngine());
597 cricket::AudioRecvParameters parameters;
598 parameters.codecs.push_back(kIsacCodec);
599 parameters.codecs.push_back(kPcmuCodec);
600 parameters.codecs.push_back(kOpusCodec);
601 parameters.codecs[2].params["stereo"] = "1";
602 EXPECT_TRUE(channel_->SetRecvParameters(parameters));
603 EXPECT_TRUE(channel_->AddRecvStream(
604 cricket::StreamParams::CreateLegacy(kSsrc1)));
605 int channel_num2 = voe_.GetLastChannel();
606 webrtc::CodecInst opus;
607 cricket::WebRtcVoiceEngine::ToCodecInst(kOpusCodec, &opus);
608 EXPECT_EQ(2, opus.channels);
609 EXPECT_EQ(111, opus.pltype);
610 EXPECT_STREQ("opus", opus.plname);
611 opus.pltype = 0;
612 EXPECT_EQ(0, voe_.GetRecPayloadType(channel_num2, opus));
613 EXPECT_EQ(111, opus.pltype);
614 }
615
616 // Test that changes to recv codecs are applied to all streams.
617 TEST_F(WebRtcVoiceEngineTestFake, SetRecvCodecsWithMultipleStreams) {
618 EXPECT_TRUE(SetupEngine());
619 cricket::AudioRecvParameters parameters;
620 parameters.codecs.push_back(kIsacCodec);
621 parameters.codecs.push_back(kPcmuCodec);
622 parameters.codecs.push_back(kTelephoneEventCodec);
623 parameters.codecs[0].id = 106; // collide with existing telephone-event
624 parameters.codecs[2].id = 126;
625 EXPECT_TRUE(channel_->SetRecvParameters(parameters));
626 EXPECT_TRUE(channel_->AddRecvStream(
627 cricket::StreamParams::CreateLegacy(kSsrc1)));
628 int channel_num2 = voe_.GetLastChannel();
629 webrtc::CodecInst gcodec;
630 rtc::strcpyn(gcodec.plname, arraysize(gcodec.plname), "ISAC");
631 gcodec.plfreq = 16000;
632 gcodec.channels = 1;
633 EXPECT_EQ(0, voe_.GetRecPayloadType(channel_num2, gcodec));
634 EXPECT_EQ(106, gcodec.pltype);
635 EXPECT_STREQ("ISAC", gcodec.plname);
636 rtc::strcpyn(gcodec.plname, arraysize(gcodec.plname), "telephone-event");
637 gcodec.plfreq = 8000;
638 gcodec.channels = 1;
639 EXPECT_EQ(0, voe_.GetRecPayloadType(channel_num2, gcodec));
640 EXPECT_EQ(126, gcodec.pltype);
641 EXPECT_STREQ("telephone-event", gcodec.plname);
642 }
643
644 TEST_F(WebRtcVoiceEngineTestFake, SetRecvCodecsAfterAddingStreams) {
645 EXPECT_TRUE(SetupEngineWithRecvStream());
646 cricket::AudioRecvParameters parameters;
647 parameters.codecs.push_back(kIsacCodec);
648 parameters.codecs[0].id = 106; // collide with existing telephone-event
649 EXPECT_TRUE(channel_->SetRecvParameters(parameters));
650
651 int channel_num2 = voe_.GetLastChannel();
652 webrtc::CodecInst gcodec;
653 rtc::strcpyn(gcodec.plname, arraysize(gcodec.plname), "ISAC");
654 gcodec.plfreq = 16000;
655 gcodec.channels = 1;
656 EXPECT_EQ(0, voe_.GetRecPayloadType(channel_num2, gcodec));
657 EXPECT_EQ(106, gcodec.pltype);
658 EXPECT_STREQ("ISAC", gcodec.plname);
659 }
660
661 // Test that we can apply the same set of codecs again while playing.
662 TEST_F(WebRtcVoiceEngineTestFake, SetRecvCodecsWhilePlaying) {
663 EXPECT_TRUE(SetupEngineWithRecvStream());
664 cricket::AudioRecvParameters parameters;
665 parameters.codecs.push_back(kIsacCodec);
666 parameters.codecs.push_back(kCn16000Codec);
667 EXPECT_TRUE(channel_->SetRecvParameters(parameters));
668 EXPECT_TRUE(channel_->SetPlayout(true));
669 EXPECT_TRUE(channel_->SetRecvParameters(parameters));
670
671 // Changing the payload type of a codec should fail.
672 parameters.codecs[0].id = 127;
673 EXPECT_FALSE(channel_->SetRecvParameters(parameters));
674 int channel_num = voe_.GetLastChannel();
675 EXPECT_TRUE(voe_.GetPlayout(channel_num));
676 }
677
678 // Test that we can add a codec while playing.
679 TEST_F(WebRtcVoiceEngineTestFake, AddRecvCodecsWhilePlaying) {
680 EXPECT_TRUE(SetupEngineWithRecvStream());
681 cricket::AudioRecvParameters parameters;
682 parameters.codecs.push_back(kIsacCodec);
683 parameters.codecs.push_back(kCn16000Codec);
684 EXPECT_TRUE(channel_->SetRecvParameters(parameters));
685 EXPECT_TRUE(channel_->SetPlayout(true));
686
687 parameters.codecs.push_back(kOpusCodec);
688 EXPECT_TRUE(channel_->SetRecvParameters(parameters));
689 int channel_num = voe_.GetLastChannel();
690 EXPECT_TRUE(voe_.GetPlayout(channel_num));
691 webrtc::CodecInst gcodec;
692 EXPECT_TRUE(cricket::WebRtcVoiceEngine::ToCodecInst(kOpusCodec, &gcodec));
693 EXPECT_EQ(kOpusCodec.id, gcodec.pltype);
694 }
695
696 TEST_F(WebRtcVoiceEngineTestFake, SetSendBandwidthAuto) {
697 EXPECT_TRUE(SetupEngineWithSendStream());
698
699 // Test that when autobw is enabled, bitrate is kept as the default
700 // value. autobw is enabled for the following tests because the target
701 // bitrate is <= 0.
702
703 // ISAC, default bitrate == 32000.
704 TestSendBandwidth(kIsacCodec, 0, true, 32000);
705
706 // PCMU, default bitrate == 64000.
707 TestSendBandwidth(kPcmuCodec, -1, true, 64000);
708
709 // opus, default bitrate == 64000.
710 TestSendBandwidth(kOpusCodec, -1, true, 64000);
711 }
712
713 TEST_F(WebRtcVoiceEngineTestFake, SetMaxSendBandwidthMultiRateAsCaller) {
714 EXPECT_TRUE(SetupEngineWithSendStream());
715
716 // Test that the bitrate of a multi-rate codec is always the maximum.
717
718 // ISAC, default bitrate == 32000.
719 TestSendBandwidth(kIsacCodec, 128000, true, 128000);
720 TestSendBandwidth(kIsacCodec, 16000, true, 16000);
721
722 // opus, default bitrate == 64000.
723 TestSendBandwidth(kOpusCodec, 96000, true, 96000);
724 TestSendBandwidth(kOpusCodec, 48000, true, 48000);
725 }
726
727 TEST_F(WebRtcVoiceEngineTestFake, SetMaxSendBandwidthFixedRateAsCaller) {
728 EXPECT_TRUE(SetupEngineWithSendStream());
729
730 // Test that we can only set a maximum bitrate for a fixed-rate codec
731 // if it's bigger than the fixed rate.
732
733 // PCMU, fixed bitrate == 64000.
734 TestSendBandwidth(kPcmuCodec, 0, true, 64000);
735 TestSendBandwidth(kPcmuCodec, 1, false, 64000);
736 TestSendBandwidth(kPcmuCodec, 128000, true, 64000);
737 TestSendBandwidth(kPcmuCodec, 32000, false, 64000);
738 TestSendBandwidth(kPcmuCodec, 64000, true, 64000);
739 TestSendBandwidth(kPcmuCodec, 63999, false, 64000);
740 TestSendBandwidth(kPcmuCodec, 64001, true, 64000);
741 }
742
743 TEST_F(WebRtcVoiceEngineTestFake, SetMaxSendBandwidthMultiRateAsCallee) {
744 EXPECT_TRUE(SetupEngine());
745 const int kDesiredBitrate = 128000;
746 cricket::AudioSendParameters parameters;
747 parameters.codecs = engine_.codecs();
748 parameters.max_bandwidth_bps = kDesiredBitrate;
749 EXPECT_TRUE(channel_->SetSendParameters(parameters));
750
751 EXPECT_TRUE(channel_->AddSendStream(
752 cricket::StreamParams::CreateLegacy(kSsrc1)));
753
754 int channel_num = voe_.GetLastChannel();
755 webrtc::CodecInst codec;
756 EXPECT_EQ(0, voe_.GetSendCodec(channel_num, codec));
757 EXPECT_EQ(kDesiredBitrate, codec.rate);
758 }
759
760 // Test that bitrate cannot be set for CBR codecs.
761 // Bitrate is ignored if it is higher than the fixed bitrate.
762 // Bitrate less then the fixed bitrate is an error.
763 TEST_F(WebRtcVoiceEngineTestFake, SetMaxSendBandwidthCbr) {
764 EXPECT_TRUE(SetupEngineWithSendStream());
765
766 // PCMU, default bitrate == 64000.
767 EXPECT_TRUE(channel_->SetSendParameters(send_parameters_));
768 int channel_num = voe_.GetLastChannel();
769 webrtc::CodecInst codec;
770 EXPECT_EQ(0, voe_.GetSendCodec(channel_num, codec));
771 EXPECT_EQ(64000, codec.rate);
772
773 send_parameters_.max_bandwidth_bps = 128000;
774 EXPECT_TRUE(channel_->SetSendParameters(send_parameters_));
775 EXPECT_EQ(0, voe_.GetSendCodec(channel_num, codec));
776 EXPECT_EQ(64000, codec.rate);
777
778 send_parameters_.max_bandwidth_bps = 128;
779 EXPECT_FALSE(channel_->SetSendParameters(send_parameters_));
780 EXPECT_EQ(0, voe_.GetSendCodec(channel_num, codec));
781 EXPECT_EQ(64000, codec.rate);
782 }
783
784 // Test that we apply codecs properly.
785 TEST_F(WebRtcVoiceEngineTestFake, SetSendCodecs) {
786 EXPECT_TRUE(SetupEngineWithSendStream());
787 cricket::AudioSendParameters parameters;
788 parameters.codecs.push_back(kIsacCodec);
789 parameters.codecs.push_back(kPcmuCodec);
790 parameters.codecs.push_back(kRedCodec);
791 parameters.codecs[0].id = 96;
792 parameters.codecs[0].bitrate = 48000;
793 EXPECT_TRUE(channel_->SetSendParameters(parameters));
794 EXPECT_EQ(1, voe_.GetNumSetSendCodecs());
795 int channel_num = voe_.GetLastChannel();
796 webrtc::CodecInst gcodec;
797 EXPECT_EQ(0, voe_.GetSendCodec(channel_num, gcodec));
798 EXPECT_EQ(96, gcodec.pltype);
799 EXPECT_EQ(48000, gcodec.rate);
800 EXPECT_STREQ("ISAC", gcodec.plname);
801 EXPECT_FALSE(voe_.GetVAD(channel_num));
802 EXPECT_FALSE(voe_.GetRED(channel_num));
803 EXPECT_EQ(13, voe_.GetSendCNPayloadType(channel_num, false));
804 EXPECT_EQ(105, voe_.GetSendCNPayloadType(channel_num, true));
805 EXPECT_FALSE(channel_->CanInsertDtmf());
806 }
807
808 // Test that VoE Channel doesn't call SetSendCodec again if same codec is tried
809 // to apply.
810 TEST_F(WebRtcVoiceEngineTestFake, DontResetSetSendCodec) {
811 EXPECT_TRUE(SetupEngineWithSendStream());
812 cricket::AudioSendParameters parameters;
813 parameters.codecs.push_back(kIsacCodec);
814 parameters.codecs.push_back(kPcmuCodec);
815 parameters.codecs.push_back(kRedCodec);
816 parameters.codecs[0].id = 96;
817 parameters.codecs[0].bitrate = 48000;
818 EXPECT_TRUE(channel_->SetSendParameters(parameters));
819 EXPECT_EQ(1, voe_.GetNumSetSendCodecs());
820 // Calling SetSendCodec again with same codec which is already set.
821 // In this case media channel shouldn't send codec to VoE.
822 EXPECT_TRUE(channel_->SetSendParameters(parameters));
823 EXPECT_EQ(1, voe_.GetNumSetSendCodecs());
824 }
825
826 // Verify that G722 is set with 16000 samples per second to WebRTC.
827 TEST_F(WebRtcVoiceEngineTestFake, SetSendCodecG722) {
828 EXPECT_TRUE(SetupEngineWithSendStream());
829 int channel_num = voe_.GetLastChannel();
830 cricket::AudioSendParameters parameters;
831 parameters.codecs.push_back(kG722CodecSdp);
832 EXPECT_TRUE(channel_->SetSendParameters(parameters));
833 webrtc::CodecInst gcodec;
834 EXPECT_EQ(0, voe_.GetSendCodec(channel_num, gcodec));
835 EXPECT_STREQ("G722", gcodec.plname);
836 EXPECT_EQ(1, gcodec.channels);
837 EXPECT_EQ(16000, gcodec.plfreq);
838 }
839
840 // Test that if clockrate is not 48000 for opus, we fail.
841 TEST_F(WebRtcVoiceEngineTestFake, SetSendCodecOpusBadClockrate) {
842 EXPECT_TRUE(SetupEngineWithSendStream());
843 cricket::AudioSendParameters parameters;
844 parameters.codecs.push_back(kOpusCodec);
845 parameters.codecs[0].bitrate = 0;
846 parameters.codecs[0].clockrate = 50000;
847 EXPECT_FALSE(channel_->SetSendParameters(parameters));
848 }
849
850 // Test that if channels=0 for opus, we fail.
851 TEST_F(WebRtcVoiceEngineTestFake, SetSendCodecOpusBad0ChannelsNoStereo) {
852 EXPECT_TRUE(SetupEngineWithSendStream());
853 cricket::AudioSendParameters parameters;
854 parameters.codecs.push_back(kOpusCodec);
855 parameters.codecs[0].bitrate = 0;
856 parameters.codecs[0].channels = 0;
857 EXPECT_FALSE(channel_->SetSendParameters(parameters));
858 }
859
860 // Test that if channels=0 for opus, we fail.
861 TEST_F(WebRtcVoiceEngineTestFake, SetSendCodecOpusBad0Channels1Stereo) {
862 EXPECT_TRUE(SetupEngineWithSendStream());
863 cricket::AudioSendParameters parameters;
864 parameters.codecs.push_back(kOpusCodec);
865 parameters.codecs[0].bitrate = 0;
866 parameters.codecs[0].channels = 0;
867 parameters.codecs[0].params["stereo"] = "1";
868 EXPECT_FALSE(channel_->SetSendParameters(parameters));
869 }
870
871 // Test that if channel is 1 for opus and there's no stereo, we fail.
872 TEST_F(WebRtcVoiceEngineTestFake, SetSendCodecOpus1ChannelNoStereo) {
873 EXPECT_TRUE(SetupEngineWithSendStream());
874 cricket::AudioSendParameters parameters;
875 parameters.codecs.push_back(kOpusCodec);
876 parameters.codecs[0].bitrate = 0;
877 parameters.codecs[0].channels = 1;
878 EXPECT_FALSE(channel_->SetSendParameters(parameters));
879 }
880
881 // Test that if channel is 1 for opus and stereo=0, we fail.
882 TEST_F(WebRtcVoiceEngineTestFake, SetSendCodecOpusBad1Channel0Stereo) {
883 EXPECT_TRUE(SetupEngineWithSendStream());
884 cricket::AudioSendParameters parameters;
885 parameters.codecs.push_back(kOpusCodec);
886 parameters.codecs[0].bitrate = 0;
887 parameters.codecs[0].channels = 1;
888 parameters.codecs[0].params["stereo"] = "0";
889 EXPECT_FALSE(channel_->SetSendParameters(parameters));
890 }
891
892 // Test that if channel is 1 for opus and stereo=1, we fail.
893 TEST_F(WebRtcVoiceEngineTestFake, SetSendCodecOpusBad1Channel1Stereo) {
894 EXPECT_TRUE(SetupEngineWithSendStream());
895 cricket::AudioSendParameters parameters;
896 parameters.codecs.push_back(kOpusCodec);
897 parameters.codecs[0].bitrate = 0;
898 parameters.codecs[0].channels = 1;
899 parameters.codecs[0].params["stereo"] = "1";
900 EXPECT_FALSE(channel_->SetSendParameters(parameters));
901 }
902
903 // Test that with bitrate=0 and no stereo,
904 // channels and bitrate are 1 and 32000.
905 TEST_F(WebRtcVoiceEngineTestFake, SetSendCodecOpusGood0BitrateNoStereo) {
906 EXPECT_TRUE(SetupEngineWithSendStream());
907 int channel_num = voe_.GetLastChannel();
908 cricket::AudioSendParameters parameters;
909 parameters.codecs.push_back(kOpusCodec);
910 parameters.codecs[0].bitrate = 0;
911 EXPECT_TRUE(channel_->SetSendParameters(parameters));
912 webrtc::CodecInst gcodec;
913 EXPECT_EQ(0, voe_.GetSendCodec(channel_num, gcodec));
914 EXPECT_STREQ("opus", gcodec.plname);
915 EXPECT_EQ(1, gcodec.channels);
916 EXPECT_EQ(32000, gcodec.rate);
917 }
918
919 // Test that with bitrate=0 and stereo=0,
920 // channels and bitrate are 1 and 32000.
921 TEST_F(WebRtcVoiceEngineTestFake, SetSendCodecOpusGood0Bitrate0Stereo) {
922 EXPECT_TRUE(SetupEngineWithSendStream());
923 int channel_num = voe_.GetLastChannel();
924 cricket::AudioSendParameters parameters;
925 parameters.codecs.push_back(kOpusCodec);
926 parameters.codecs[0].bitrate = 0;
927 parameters.codecs[0].params["stereo"] = "0";
928 EXPECT_TRUE(channel_->SetSendParameters(parameters));
929 webrtc::CodecInst gcodec;
930 EXPECT_EQ(0, voe_.GetSendCodec(channel_num, gcodec));
931 EXPECT_STREQ("opus", gcodec.plname);
932 EXPECT_EQ(1, gcodec.channels);
933 EXPECT_EQ(32000, gcodec.rate);
934 }
935
936 // Test that with bitrate=invalid and stereo=0,
937 // channels and bitrate are 1 and 32000.
938 TEST_F(WebRtcVoiceEngineTestFake, SetSendCodecOpusGoodXBitrate0Stereo) {
939 EXPECT_TRUE(SetupEngineWithSendStream());
940 int channel_num = voe_.GetLastChannel();
941 cricket::AudioSendParameters parameters;
942 parameters.codecs.push_back(kOpusCodec);
943 parameters.codecs[0].params["stereo"] = "0";
944 webrtc::CodecInst gcodec;
945
946 // bitrate that's out of the range between 6000 and 510000 will be clamped.
947 parameters.codecs[0].bitrate = 5999;
948 EXPECT_TRUE(channel_->SetSendParameters(parameters));
949 EXPECT_EQ(0, voe_.GetSendCodec(channel_num, gcodec));
950 EXPECT_STREQ("opus", gcodec.plname);
951 EXPECT_EQ(1, gcodec.channels);
952 EXPECT_EQ(6000, gcodec.rate);
953
954 parameters.codecs[0].bitrate = 510001;
955 EXPECT_TRUE(channel_->SetSendParameters(parameters));
956 EXPECT_EQ(0, voe_.GetSendCodec(channel_num, gcodec));
957 EXPECT_STREQ("opus", gcodec.plname);
958 EXPECT_EQ(1, gcodec.channels);
959 EXPECT_EQ(510000, gcodec.rate);
960 }
961
962 // Test that with bitrate=0 and stereo=1,
963 // channels and bitrate are 2 and 64000.
964 TEST_F(WebRtcVoiceEngineTestFake, SetSendCodecOpusGood0Bitrate1Stereo) {
965 EXPECT_TRUE(SetupEngineWithSendStream());
966 int channel_num = voe_.GetLastChannel();
967 cricket::AudioSendParameters parameters;
968 parameters.codecs.push_back(kOpusCodec);
969 parameters.codecs[0].bitrate = 0;
970 parameters.codecs[0].params["stereo"] = "1";
971 EXPECT_TRUE(channel_->SetSendParameters(parameters));
972 webrtc::CodecInst gcodec;
973 EXPECT_EQ(0, voe_.GetSendCodec(channel_num, gcodec));
974 EXPECT_STREQ("opus", gcodec.plname);
975 EXPECT_EQ(2, gcodec.channels);
976 EXPECT_EQ(64000, gcodec.rate);
977 }
978
979 // Test that with bitrate=invalid and stereo=1,
980 // channels and bitrate are 2 and 64000.
981 TEST_F(WebRtcVoiceEngineTestFake, SetSendCodecOpusGoodXBitrate1Stereo) {
982 EXPECT_TRUE(SetupEngineWithSendStream());
983 int channel_num = voe_.GetLastChannel();
984 cricket::AudioSendParameters parameters;
985 parameters.codecs.push_back(kOpusCodec);
986 parameters.codecs[0].params["stereo"] = "1";
987 webrtc::CodecInst gcodec;
988
989 // bitrate that's out of the range between 6000 and 510000 will be clamped.
990 parameters.codecs[0].bitrate = 5999;
991 EXPECT_TRUE(channel_->SetSendParameters(parameters));
992 EXPECT_EQ(0, voe_.GetSendCodec(channel_num, gcodec));
993 EXPECT_STREQ("opus", gcodec.plname);
994 EXPECT_EQ(2, gcodec.channels);
995 EXPECT_EQ(6000, gcodec.rate);
996
997 parameters.codecs[0].bitrate = 510001;
998 EXPECT_TRUE(channel_->SetSendParameters(parameters));
999 EXPECT_EQ(0, voe_.GetSendCodec(channel_num, gcodec));
1000 EXPECT_STREQ("opus", gcodec.plname);
1001 EXPECT_EQ(2, gcodec.channels);
1002 EXPECT_EQ(510000, gcodec.rate);
1003 }
1004
1005 // Test that with bitrate=N and stereo unset,
1006 // channels and bitrate are 1 and N.
1007 TEST_F(WebRtcVoiceEngineTestFake, SetSendCodecOpusGoodNBitrateNoStereo) {
1008 EXPECT_TRUE(SetupEngineWithSendStream());
1009 int channel_num = voe_.GetLastChannel();
1010 cricket::AudioSendParameters parameters;
1011 parameters.codecs.push_back(kOpusCodec);
1012 parameters.codecs[0].bitrate = 96000;
1013 EXPECT_TRUE(channel_->SetSendParameters(parameters));
1014 webrtc::CodecInst gcodec;
1015 EXPECT_EQ(0, voe_.GetSendCodec(channel_num, gcodec));
1016 EXPECT_EQ(111, gcodec.pltype);
1017 EXPECT_EQ(96000, gcodec.rate);
1018 EXPECT_STREQ("opus", gcodec.plname);
1019 EXPECT_EQ(1, gcodec.channels);
1020 EXPECT_EQ(48000, gcodec.plfreq);
1021 }
1022
1023 // Test that with bitrate=N and stereo=0,
1024 // channels and bitrate are 1 and N.
1025 TEST_F(WebRtcVoiceEngineTestFake, SetSendCodecOpusGoodNBitrate0Stereo) {
1026 EXPECT_TRUE(SetupEngineWithSendStream());
1027 int channel_num = voe_.GetLastChannel();
1028 cricket::AudioSendParameters parameters;
1029 parameters.codecs.push_back(kOpusCodec);
1030 parameters.codecs[0].bitrate = 30000;
1031 parameters.codecs[0].params["stereo"] = "0";
1032 EXPECT_TRUE(channel_->SetSendParameters(parameters));
1033 webrtc::CodecInst gcodec;
1034 EXPECT_EQ(0, voe_.GetSendCodec(channel_num, gcodec));
1035 EXPECT_EQ(1, gcodec.channels);
1036 EXPECT_EQ(30000, gcodec.rate);
1037 EXPECT_STREQ("opus", gcodec.plname);
1038 }
1039
1040 // Test that with bitrate=N and without any parameters,
1041 // channels and bitrate are 1 and N.
1042 TEST_F(WebRtcVoiceEngineTestFake, SetSendCodecOpusGoodNBitrateNoParameters) {
1043 EXPECT_TRUE(SetupEngineWithSendStream());
1044 int channel_num = voe_.GetLastChannel();
1045 cricket::AudioSendParameters parameters;
1046 parameters.codecs.push_back(kOpusCodec);
1047 parameters.codecs[0].bitrate = 30000;
1048 EXPECT_TRUE(channel_->SetSendParameters(parameters));
1049 webrtc::CodecInst gcodec;
1050 EXPECT_EQ(0, voe_.GetSendCodec(channel_num, gcodec));
1051 EXPECT_EQ(1, gcodec.channels);
1052 EXPECT_EQ(30000, gcodec.rate);
1053 EXPECT_STREQ("opus", gcodec.plname);
1054 }
1055
1056 // Test that with bitrate=N and stereo=1,
1057 // channels and bitrate are 2 and N.
1058 TEST_F(WebRtcVoiceEngineTestFake, SetSendCodecOpusGoodNBitrate1Stereo) {
1059 EXPECT_TRUE(SetupEngineWithSendStream());
1060 int channel_num = voe_.GetLastChannel();
1061 cricket::AudioSendParameters parameters;
1062 parameters.codecs.push_back(kOpusCodec);
1063 parameters.codecs[0].bitrate = 30000;
1064 parameters.codecs[0].params["stereo"] = "1";
1065 EXPECT_TRUE(channel_->SetSendParameters(parameters));
1066 webrtc::CodecInst gcodec;
1067 EXPECT_EQ(0, voe_.GetSendCodec(channel_num, gcodec));
1068 EXPECT_EQ(2, gcodec.channels);
1069 EXPECT_EQ(30000, gcodec.rate);
1070 EXPECT_STREQ("opus", gcodec.plname);
1071 }
1072
1073 // Test that bitrate will be overridden by the "maxaveragebitrate" parameter.
1074 // Also test that the "maxaveragebitrate" can't be set to values outside the
1075 // range of 6000 and 510000
1076 TEST_F(WebRtcVoiceEngineTestFake, SetSendCodecOpusMaxAverageBitrate) {
1077 EXPECT_TRUE(SetupEngineWithSendStream());
1078 int channel_num = voe_.GetLastChannel();
1079 cricket::AudioSendParameters parameters;
1080 parameters.codecs.push_back(kOpusCodec);
1081 parameters.codecs[0].bitrate = 30000;
1082 webrtc::CodecInst gcodec;
1083
1084 // Ignore if less than 6000.
1085 parameters.codecs[0].params["maxaveragebitrate"] = "5999";
1086 EXPECT_TRUE(channel_->SetSendParameters(parameters));
1087 EXPECT_EQ(0, voe_.GetSendCodec(channel_num, gcodec));
1088 EXPECT_EQ(6000, gcodec.rate);
1089
1090 // Ignore if larger than 510000.
1091 parameters.codecs[0].params["maxaveragebitrate"] = "510001";
1092 EXPECT_TRUE(channel_->SetSendParameters(parameters));
1093 EXPECT_EQ(0, voe_.GetSendCodec(channel_num, gcodec));
1094 EXPECT_EQ(510000, gcodec.rate);
1095
1096 parameters.codecs[0].params["maxaveragebitrate"] = "200000";
1097 EXPECT_TRUE(channel_->SetSendParameters(parameters));
1098 EXPECT_EQ(0, voe_.GetSendCodec(channel_num, gcodec));
1099 EXPECT_EQ(200000, gcodec.rate);
1100 }
1101
1102 // Test that we can enable NACK with opus as caller.
1103 TEST_F(WebRtcVoiceEngineTestFake, SetSendCodecEnableNackAsCaller) {
1104 EXPECT_TRUE(SetupEngineWithSendStream());
1105 int channel_num = voe_.GetLastChannel();
1106 cricket::AudioSendParameters parameters;
1107 parameters.codecs.push_back(kOpusCodec);
1108 parameters.codecs[0].AddFeedbackParam(
1109 cricket::FeedbackParam(cricket::kRtcpFbParamNack,
1110 cricket::kParamValueEmpty));
1111 EXPECT_FALSE(voe_.GetNACK(channel_num));
1112 EXPECT_TRUE(channel_->SetSendParameters(parameters));
1113 EXPECT_TRUE(voe_.GetNACK(channel_num));
1114 }
1115
1116 // Test that we can enable NACK with opus as callee.
1117 TEST_F(WebRtcVoiceEngineTestFake, SetSendCodecEnableNackAsCallee) {
1118 EXPECT_TRUE(SetupEngineWithRecvStream());
1119 int channel_num = voe_.GetLastChannel();
1120 cricket::AudioSendParameters parameters;
1121 parameters.codecs.push_back(kOpusCodec);
1122 parameters.codecs[0].AddFeedbackParam(
1123 cricket::FeedbackParam(cricket::kRtcpFbParamNack,
1124 cricket::kParamValueEmpty));
1125 EXPECT_FALSE(voe_.GetNACK(channel_num));
1126 EXPECT_TRUE(channel_->SetSendParameters(parameters));
1127 EXPECT_FALSE(voe_.GetNACK(channel_num));
1128
1129 EXPECT_TRUE(channel_->AddSendStream(
1130 cricket::StreamParams::CreateLegacy(kSsrc1)));
1131 EXPECT_TRUE(voe_.GetNACK(voe_.GetLastChannel()));
1132 }
1133
1134 // Test that we can enable NACK on receive streams.
1135 TEST_F(WebRtcVoiceEngineTestFake, SetSendCodecEnableNackRecvStreams) {
1136 EXPECT_TRUE(SetupEngineWithSendStream());
1137 int channel_num1 = voe_.GetLastChannel();
1138 EXPECT_TRUE(channel_->AddRecvStream(cricket::StreamParams::CreateLegacy(2)));
1139 int channel_num2 = voe_.GetLastChannel();
1140 cricket::AudioSendParameters parameters;
1141 parameters.codecs.push_back(kOpusCodec);
1142 parameters.codecs[0].AddFeedbackParam(
1143 cricket::FeedbackParam(cricket::kRtcpFbParamNack,
1144 cricket::kParamValueEmpty));
1145 EXPECT_FALSE(voe_.GetNACK(channel_num1));
1146 EXPECT_FALSE(voe_.GetNACK(channel_num2));
1147 EXPECT_TRUE(channel_->SetSendParameters(parameters));
1148 EXPECT_TRUE(voe_.GetNACK(channel_num1));
1149 EXPECT_TRUE(voe_.GetNACK(channel_num2));
1150 }
1151
1152 // Test that we can disable NACK.
1153 TEST_F(WebRtcVoiceEngineTestFake, SetSendCodecDisableNack) {
1154 EXPECT_TRUE(SetupEngineWithSendStream());
1155 int channel_num = voe_.GetLastChannel();
1156 cricket::AudioSendParameters parameters;
1157 parameters.codecs.push_back(kOpusCodec);
1158 parameters.codecs[0].AddFeedbackParam(
1159 cricket::FeedbackParam(cricket::kRtcpFbParamNack,
1160 cricket::kParamValueEmpty));
1161 EXPECT_TRUE(channel_->SetSendParameters(parameters));
1162 EXPECT_TRUE(voe_.GetNACK(channel_num));
1163
1164 parameters.codecs.clear();
1165 parameters.codecs.push_back(kOpusCodec);
1166 EXPECT_TRUE(channel_->SetSendParameters(parameters));
1167 EXPECT_FALSE(voe_.GetNACK(channel_num));
1168 }
1169
1170 // Test that we can disable NACK on receive streams.
1171 TEST_F(WebRtcVoiceEngineTestFake, SetSendCodecDisableNackRecvStreams) {
1172 EXPECT_TRUE(SetupEngineWithSendStream());
1173 int channel_num1 = voe_.GetLastChannel();
1174 EXPECT_TRUE(channel_->AddRecvStream(cricket::StreamParams::CreateLegacy(2)));
1175 int channel_num2 = voe_.GetLastChannel();
1176 cricket::AudioSendParameters parameters;
1177 parameters.codecs.push_back(kOpusCodec);
1178 parameters.codecs[0].AddFeedbackParam(
1179 cricket::FeedbackParam(cricket::kRtcpFbParamNack,
1180 cricket::kParamValueEmpty));
1181 EXPECT_TRUE(channel_->SetSendParameters(parameters));
1182 EXPECT_TRUE(voe_.GetNACK(channel_num1));
1183 EXPECT_TRUE(voe_.GetNACK(channel_num2));
1184
1185 parameters.codecs.clear();
1186 parameters.codecs.push_back(kOpusCodec);
1187 EXPECT_TRUE(channel_->SetSendParameters(parameters));
1188 EXPECT_FALSE(voe_.GetNACK(channel_num1));
1189 EXPECT_FALSE(voe_.GetNACK(channel_num2));
1190 }
1191
1192 // Test that NACK is enabled on a new receive stream.
1193 TEST_F(WebRtcVoiceEngineTestFake, AddRecvStreamEnableNack) {
1194 EXPECT_TRUE(SetupEngineWithSendStream());
1195 int channel_num = voe_.GetLastChannel();
1196 cricket::AudioSendParameters parameters;
1197 parameters.codecs.push_back(kIsacCodec);
1198 parameters.codecs.push_back(kCn16000Codec);
1199 parameters.codecs[0].AddFeedbackParam(
1200 cricket::FeedbackParam(cricket::kRtcpFbParamNack,
1201 cricket::kParamValueEmpty));
1202 EXPECT_TRUE(channel_->SetSendParameters(parameters));
1203 EXPECT_TRUE(voe_.GetNACK(channel_num));
1204
1205 EXPECT_TRUE(channel_->AddRecvStream(cricket::StreamParams::CreateLegacy(2)));
1206 channel_num = voe_.GetLastChannel();
1207 EXPECT_TRUE(voe_.GetNACK(channel_num));
1208 EXPECT_TRUE(channel_->AddRecvStream(cricket::StreamParams::CreateLegacy(3)));
1209 channel_num = voe_.GetLastChannel();
1210 EXPECT_TRUE(voe_.GetNACK(channel_num));
1211 }
1212
1213 // Test that without useinbandfec, Opus FEC is off.
1214 TEST_F(WebRtcVoiceEngineTestFake, SetSendCodecNoOpusFec) {
1215 EXPECT_TRUE(SetupEngineWithSendStream());
1216 int channel_num = voe_.GetLastChannel();
1217 cricket::AudioSendParameters parameters;
1218 parameters.codecs.push_back(kOpusCodec);
1219 EXPECT_TRUE(channel_->SetSendParameters(parameters));
1220 EXPECT_FALSE(voe_.GetCodecFEC(channel_num));
1221 }
1222
1223 // Test that with useinbandfec=0, Opus FEC is off.
1224 TEST_F(WebRtcVoiceEngineTestFake, SetSendCodecOpusDisableFec) {
1225 EXPECT_TRUE(SetupEngineWithSendStream());
1226 int channel_num = voe_.GetLastChannel();
1227 cricket::AudioSendParameters parameters;
1228 parameters.codecs.push_back(kOpusCodec);
1229 parameters.codecs[0].bitrate = 0;
1230 parameters.codecs[0].params["useinbandfec"] = "0";
1231 EXPECT_TRUE(channel_->SetSendParameters(parameters));
1232 EXPECT_FALSE(voe_.GetCodecFEC(channel_num));
1233 webrtc::CodecInst gcodec;
1234 EXPECT_EQ(0, voe_.GetSendCodec(channel_num, gcodec));
1235 EXPECT_STREQ("opus", gcodec.plname);
1236 EXPECT_EQ(1, gcodec.channels);
1237 EXPECT_EQ(32000, gcodec.rate);
1238 }
1239
1240 // Test that with useinbandfec=1, Opus FEC is on.
1241 TEST_F(WebRtcVoiceEngineTestFake, SetSendCodecOpusEnableFec) {
1242 EXPECT_TRUE(SetupEngineWithSendStream());
1243 int channel_num = voe_.GetLastChannel();
1244 cricket::AudioSendParameters parameters;
1245 parameters.codecs.push_back(kOpusCodec);
1246 parameters.codecs[0].bitrate = 0;
1247 parameters.codecs[0].params["useinbandfec"] = "1";
1248 EXPECT_TRUE(channel_->SetSendParameters(parameters));
1249 EXPECT_TRUE(voe_.GetCodecFEC(channel_num));
1250 webrtc::CodecInst gcodec;
1251 EXPECT_EQ(0, voe_.GetSendCodec(channel_num, gcodec));
1252 EXPECT_STREQ("opus", gcodec.plname);
1253 EXPECT_EQ(1, gcodec.channels);
1254 EXPECT_EQ(32000, gcodec.rate);
1255 }
1256
1257 // Test that with useinbandfec=1, stereo=1, Opus FEC is on.
1258 TEST_F(WebRtcVoiceEngineTestFake, SetSendCodecOpusEnableFecStereo) {
1259 EXPECT_TRUE(SetupEngineWithSendStream());
1260 int channel_num = voe_.GetLastChannel();
1261 cricket::AudioSendParameters parameters;
1262 parameters.codecs.push_back(kOpusCodec);
1263 parameters.codecs[0].bitrate = 0;
1264 parameters.codecs[0].params["stereo"] = "1";
1265 parameters.codecs[0].params["useinbandfec"] = "1";
1266 EXPECT_TRUE(channel_->SetSendParameters(parameters));
1267 EXPECT_TRUE(voe_.GetCodecFEC(channel_num));
1268 webrtc::CodecInst gcodec;
1269 EXPECT_EQ(0, voe_.GetSendCodec(channel_num, gcodec));
1270 EXPECT_STREQ("opus", gcodec.plname);
1271 EXPECT_EQ(2, gcodec.channels);
1272 EXPECT_EQ(64000, gcodec.rate);
1273 }
1274
1275 // Test that with non-Opus, codec FEC is off.
1276 TEST_F(WebRtcVoiceEngineTestFake, SetSendCodecIsacNoFec) {
1277 EXPECT_TRUE(SetupEngineWithSendStream());
1278 int channel_num = voe_.GetLastChannel();
1279 cricket::AudioSendParameters parameters;
1280 parameters.codecs.push_back(kIsacCodec);
1281 EXPECT_TRUE(channel_->SetSendParameters(parameters));
1282 EXPECT_FALSE(voe_.GetCodecFEC(channel_num));
1283 }
1284
1285 // Test the with non-Opus, even if useinbandfec=1, FEC is off.
1286 TEST_F(WebRtcVoiceEngineTestFake, SetSendCodecIsacWithParamNoFec) {
1287 EXPECT_TRUE(SetupEngineWithSendStream());
1288 int channel_num = voe_.GetLastChannel();
1289 cricket::AudioSendParameters parameters;
1290 parameters.codecs.push_back(kIsacCodec);
1291 parameters.codecs[0].params["useinbandfec"] = "1";
1292 EXPECT_TRUE(channel_->SetSendParameters(parameters));
1293 EXPECT_FALSE(voe_.GetCodecFEC(channel_num));
1294 }
1295
1296 // Test that Opus FEC status can be changed.
1297 TEST_F(WebRtcVoiceEngineTestFake, ChangeOpusFecStatus) {
1298 EXPECT_TRUE(SetupEngineWithSendStream());
1299 int channel_num = voe_.GetLastChannel();
1300 cricket::AudioSendParameters parameters;
1301 parameters.codecs.push_back(kOpusCodec);
1302 EXPECT_TRUE(channel_->SetSendParameters(parameters));
1303 EXPECT_FALSE(voe_.GetCodecFEC(channel_num));
1304 parameters.codecs[0].params["useinbandfec"] = "1";
1305 EXPECT_TRUE(channel_->SetSendParameters(parameters));
1306 EXPECT_TRUE(voe_.GetCodecFEC(channel_num));
1307 }
1308
1309 TEST_F(WebRtcVoiceEngineTestFake, TransportCcCanBeEnabledAndDisabled) {
1310 EXPECT_TRUE(SetupEngine());
1311 cricket::AudioSendParameters send_parameters;
1312 send_parameters.codecs.push_back(kOpusCodec);
1313 EXPECT_TRUE(send_parameters.codecs[0].feedback_params.params().empty());
1314 EXPECT_TRUE(channel_->SetSendParameters(send_parameters));
1315
1316 cricket::AudioRecvParameters recv_parameters;
1317 recv_parameters.codecs.push_back(kIsacCodec);
1318 EXPECT_TRUE(channel_->SetRecvParameters(recv_parameters));
1319 EXPECT_TRUE(
1320 channel_->AddRecvStream(cricket::StreamParams::CreateLegacy(kSsrc1)));
1321 ASSERT_TRUE(call_.GetAudioReceiveStream(kSsrc1) != nullptr);
1322 EXPECT_FALSE(
1323 call_.GetAudioReceiveStream(kSsrc1)->GetConfig().rtp.transport_cc);
1324
1325 send_parameters.codecs = engine_.codecs();
1326 EXPECT_TRUE(channel_->SetSendParameters(send_parameters));
1327 ASSERT_TRUE(call_.GetAudioReceiveStream(kSsrc1) != nullptr);
1328 EXPECT_TRUE(
1329 call_.GetAudioReceiveStream(kSsrc1)->GetConfig().rtp.transport_cc);
1330 }
1331
1332 // Test maxplaybackrate <= 8000 triggers Opus narrow band mode.
1333 TEST_F(WebRtcVoiceEngineTestFake, SetOpusMaxPlaybackRateNb) {
1334 EXPECT_TRUE(SetupEngineWithSendStream());
1335 int channel_num = voe_.GetLastChannel();
1336 cricket::AudioSendParameters parameters;
1337 parameters.codecs.push_back(kOpusCodec);
1338 parameters.codecs[0].bitrate = 0;
1339 parameters.codecs[0].SetParam(cricket::kCodecParamMaxPlaybackRate, 8000);
1340 EXPECT_TRUE(channel_->SetSendParameters(parameters));
1341 EXPECT_EQ(cricket::kOpusBandwidthNb,
1342 voe_.GetMaxEncodingBandwidth(channel_num));
1343 webrtc::CodecInst gcodec;
1344 EXPECT_EQ(0, voe_.GetSendCodec(channel_num, gcodec));
1345 EXPECT_STREQ("opus", gcodec.plname);
1346
1347 EXPECT_EQ(12000, gcodec.rate);
1348 parameters.codecs[0].SetParam(cricket::kCodecParamStereo, "1");
1349 EXPECT_TRUE(channel_->SetSendParameters(parameters));
1350 EXPECT_EQ(0, voe_.GetSendCodec(channel_num, gcodec));
1351 EXPECT_EQ(24000, gcodec.rate);
1352 }
1353
1354 // Test 8000 < maxplaybackrate <= 12000 triggers Opus medium band mode.
1355 TEST_F(WebRtcVoiceEngineTestFake, SetOpusMaxPlaybackRateMb) {
1356 EXPECT_TRUE(SetupEngineWithSendStream());
1357 int channel_num = voe_.GetLastChannel();
1358 cricket::AudioSendParameters parameters;
1359 parameters.codecs.push_back(kOpusCodec);
1360 parameters.codecs[0].bitrate = 0;
1361 parameters.codecs[0].SetParam(cricket::kCodecParamMaxPlaybackRate, 8001);
1362 EXPECT_TRUE(channel_->SetSendParameters(parameters));
1363 EXPECT_EQ(cricket::kOpusBandwidthMb,
1364 voe_.GetMaxEncodingBandwidth(channel_num));
1365 webrtc::CodecInst gcodec;
1366 EXPECT_EQ(0, voe_.GetSendCodec(channel_num, gcodec));
1367 EXPECT_STREQ("opus", gcodec.plname);
1368
1369 EXPECT_EQ(20000, gcodec.rate);
1370 parameters.codecs[0].SetParam(cricket::kCodecParamStereo, "1");
1371 EXPECT_TRUE(channel_->SetSendParameters(parameters));
1372 EXPECT_EQ(0, voe_.GetSendCodec(channel_num, gcodec));
1373 EXPECT_EQ(40000, gcodec.rate);
1374 }
1375
1376 // Test 12000 < maxplaybackrate <= 16000 triggers Opus wide band mode.
1377 TEST_F(WebRtcVoiceEngineTestFake, SetOpusMaxPlaybackRateWb) {
1378 EXPECT_TRUE(SetupEngineWithSendStream());
1379 int channel_num = voe_.GetLastChannel();
1380 cricket::AudioSendParameters parameters;
1381 parameters.codecs.push_back(kOpusCodec);
1382 parameters.codecs[0].bitrate = 0;
1383 parameters.codecs[0].SetParam(cricket::kCodecParamMaxPlaybackRate, 12001);
1384 EXPECT_TRUE(channel_->SetSendParameters(parameters));
1385 EXPECT_EQ(cricket::kOpusBandwidthWb,
1386 voe_.GetMaxEncodingBandwidth(channel_num));
1387 webrtc::CodecInst gcodec;
1388 EXPECT_EQ(0, voe_.GetSendCodec(channel_num, gcodec));
1389 EXPECT_STREQ("opus", gcodec.plname);
1390
1391 EXPECT_EQ(20000, gcodec.rate);
1392 parameters.codecs[0].SetParam(cricket::kCodecParamStereo, "1");
1393 EXPECT_TRUE(channel_->SetSendParameters(parameters));
1394 EXPECT_EQ(0, voe_.GetSendCodec(channel_num, gcodec));
1395 EXPECT_EQ(40000, gcodec.rate);
1396 }
1397
1398 // Test 16000 < maxplaybackrate <= 24000 triggers Opus super wide band mode.
1399 TEST_F(WebRtcVoiceEngineTestFake, SetOpusMaxPlaybackRateSwb) {
1400 EXPECT_TRUE(SetupEngineWithSendStream());
1401 int channel_num = voe_.GetLastChannel();
1402 cricket::AudioSendParameters parameters;
1403 parameters.codecs.push_back(kOpusCodec);
1404 parameters.codecs[0].bitrate = 0;
1405 parameters.codecs[0].SetParam(cricket::kCodecParamMaxPlaybackRate, 16001);
1406 EXPECT_TRUE(channel_->SetSendParameters(parameters));
1407 EXPECT_EQ(cricket::kOpusBandwidthSwb,
1408 voe_.GetMaxEncodingBandwidth(channel_num));
1409 webrtc::CodecInst gcodec;
1410 EXPECT_EQ(0, voe_.GetSendCodec(channel_num, gcodec));
1411 EXPECT_STREQ("opus", gcodec.plname);
1412
1413 EXPECT_EQ(32000, gcodec.rate);
1414 parameters.codecs[0].SetParam(cricket::kCodecParamStereo, "1");
1415 EXPECT_TRUE(channel_->SetSendParameters(parameters));
1416 EXPECT_EQ(0, voe_.GetSendCodec(channel_num, gcodec));
1417 EXPECT_EQ(64000, gcodec.rate);
1418 }
1419
1420 // Test 24000 < maxplaybackrate triggers Opus full band mode.
1421 TEST_F(WebRtcVoiceEngineTestFake, SetOpusMaxPlaybackRateFb) {
1422 EXPECT_TRUE(SetupEngineWithSendStream());
1423 int channel_num = voe_.GetLastChannel();
1424 cricket::AudioSendParameters parameters;
1425 parameters.codecs.push_back(kOpusCodec);
1426 parameters.codecs[0].bitrate = 0;
1427 parameters.codecs[0].SetParam(cricket::kCodecParamMaxPlaybackRate, 24001);
1428 EXPECT_TRUE(channel_->SetSendParameters(parameters));
1429 EXPECT_EQ(cricket::kOpusBandwidthFb,
1430 voe_.GetMaxEncodingBandwidth(channel_num));
1431 webrtc::CodecInst gcodec;
1432 EXPECT_EQ(0, voe_.GetSendCodec(channel_num, gcodec));
1433 EXPECT_STREQ("opus", gcodec.plname);
1434
1435 EXPECT_EQ(32000, gcodec.rate);
1436 parameters.codecs[0].SetParam(cricket::kCodecParamStereo, "1");
1437 EXPECT_TRUE(channel_->SetSendParameters(parameters));
1438 EXPECT_EQ(0, voe_.GetSendCodec(channel_num, gcodec));
1439 EXPECT_EQ(64000, gcodec.rate);
1440 }
1441
1442 // Test Opus that without maxplaybackrate, default playback rate is used.
1443 TEST_F(WebRtcVoiceEngineTestFake, DefaultOpusMaxPlaybackRate) {
1444 EXPECT_TRUE(SetupEngineWithSendStream());
1445 int channel_num = voe_.GetLastChannel();
1446 cricket::AudioSendParameters parameters;
1447 parameters.codecs.push_back(kOpusCodec);
1448 EXPECT_TRUE(channel_->SetSendParameters(parameters));
1449 EXPECT_EQ(cricket::kOpusBandwidthFb,
1450 voe_.GetMaxEncodingBandwidth(channel_num));
1451 }
1452
1453 // Test the with non-Opus, maxplaybackrate has no effect.
1454 TEST_F(WebRtcVoiceEngineTestFake, SetNonOpusMaxPlaybackRate) {
1455 EXPECT_TRUE(SetupEngineWithSendStream());
1456 int channel_num = voe_.GetLastChannel();
1457 cricket::AudioSendParameters parameters;
1458 parameters.codecs.push_back(kIsacCodec);
1459 parameters.codecs[0].SetParam(cricket::kCodecParamMaxPlaybackRate, 32000);
1460 EXPECT_TRUE(channel_->SetSendParameters(parameters));
1461 EXPECT_EQ(0, voe_.GetMaxEncodingBandwidth(channel_num));
1462 }
1463
1464 // Test maxplaybackrate can be set on two streams.
1465 TEST_F(WebRtcVoiceEngineTestFake, SetOpusMaxPlaybackRateOnTwoStreams) {
1466 EXPECT_TRUE(SetupEngineWithSendStream());
1467 int channel_num = voe_.GetLastChannel();
1468 cricket::AudioSendParameters parameters;
1469 parameters.codecs.push_back(kOpusCodec);
1470 EXPECT_TRUE(channel_->SetSendParameters(parameters));
1471 // Default bandwidth is 24000.
1472 EXPECT_EQ(cricket::kOpusBandwidthFb,
1473 voe_.GetMaxEncodingBandwidth(channel_num));
1474
1475 parameters.codecs[0].SetParam(cricket::kCodecParamMaxPlaybackRate, 8000);
1476
1477 EXPECT_TRUE(channel_->SetSendParameters(parameters));
1478 EXPECT_EQ(cricket::kOpusBandwidthNb,
1479 voe_.GetMaxEncodingBandwidth(channel_num));
1480
1481 channel_->AddSendStream(cricket::StreamParams::CreateLegacy(kSsrc2));
1482 channel_num = voe_.GetLastChannel();
1483 EXPECT_EQ(cricket::kOpusBandwidthNb,
1484 voe_.GetMaxEncodingBandwidth(channel_num));
1485 }
1486
1487 // Test that with usedtx=0, Opus DTX is off.
1488 TEST_F(WebRtcVoiceEngineTestFake, DisableOpusDtxOnOpus) {
1489 EXPECT_TRUE(SetupEngineWithSendStream());
1490 int channel_num = voe_.GetLastChannel();
1491 cricket::AudioSendParameters parameters;
1492 parameters.codecs.push_back(kOpusCodec);
1493 parameters.codecs[0].params["usedtx"] = "0";
1494 EXPECT_TRUE(channel_->SetSendParameters(parameters));
1495 EXPECT_FALSE(voe_.GetOpusDtx(channel_num));
1496 }
1497
1498 // Test that with usedtx=1, Opus DTX is on.
1499 TEST_F(WebRtcVoiceEngineTestFake, EnableOpusDtxOnOpus) {
1500 EXPECT_TRUE(SetupEngineWithSendStream());
1501 int channel_num = voe_.GetLastChannel();
1502 cricket::AudioSendParameters parameters;
1503 parameters.codecs.push_back(kOpusCodec);
1504 parameters.codecs[0].params["usedtx"] = "1";
1505 EXPECT_TRUE(channel_->SetSendParameters(parameters));
1506 EXPECT_TRUE(voe_.GetOpusDtx(channel_num));
1507 EXPECT_FALSE(voe_.GetVAD(channel_num)); // Opus DTX should not affect VAD.
1508 }
1509
1510 // Test that usedtx=1 works with stereo Opus.
1511 TEST_F(WebRtcVoiceEngineTestFake, EnableOpusDtxOnOpusStereo) {
1512 EXPECT_TRUE(SetupEngineWithSendStream());
1513 int channel_num = voe_.GetLastChannel();
1514 cricket::AudioSendParameters parameters;
1515 parameters.codecs.push_back(kOpusCodec);
1516 parameters.codecs[0].params["usedtx"] = "1";
1517 parameters.codecs[0].params["stereo"] = "1";
1518 EXPECT_TRUE(channel_->SetSendParameters(parameters));
1519 EXPECT_TRUE(voe_.GetOpusDtx(channel_num));
1520 EXPECT_FALSE(voe_.GetVAD(channel_num)); // Opus DTX should not affect VAD.
1521 }
1522
1523 // Test that usedtx=1 does not work with non Opus.
1524 TEST_F(WebRtcVoiceEngineTestFake, CannotEnableOpusDtxOnNonOpus) {
1525 EXPECT_TRUE(SetupEngineWithSendStream());
1526 int channel_num = voe_.GetLastChannel();
1527 cricket::AudioSendParameters parameters;
1528 parameters.codecs.push_back(kIsacCodec);
1529 parameters.codecs[0].params["usedtx"] = "1";
1530 EXPECT_TRUE(channel_->SetSendParameters(parameters));
1531 EXPECT_FALSE(voe_.GetOpusDtx(channel_num));
1532 }
1533
1534 // Test that we can switch back and forth between Opus and ISAC with CN.
1535 TEST_F(WebRtcVoiceEngineTestFake, SetSendCodecsIsacOpusSwitching) {
1536 EXPECT_TRUE(SetupEngineWithSendStream());
1537 int channel_num = voe_.GetLastChannel();
1538 cricket::AudioSendParameters opus_parameters;
1539 opus_parameters.codecs.push_back(kOpusCodec);
1540 EXPECT_TRUE(channel_->SetSendParameters(opus_parameters));
1541 webrtc::CodecInst gcodec;
1542 EXPECT_EQ(0, voe_.GetSendCodec(channel_num, gcodec));
1543 EXPECT_EQ(111, gcodec.pltype);
1544 EXPECT_STREQ("opus", gcodec.plname);
1545
1546 cricket::AudioSendParameters isac_parameters;
1547 isac_parameters.codecs.push_back(kIsacCodec);
1548 isac_parameters.codecs.push_back(kCn16000Codec);
1549 isac_parameters.codecs.push_back(kOpusCodec);
1550 EXPECT_TRUE(channel_->SetSendParameters(isac_parameters));
1551 EXPECT_EQ(0, voe_.GetSendCodec(channel_num, gcodec));
1552 EXPECT_EQ(103, gcodec.pltype);
1553 EXPECT_STREQ("ISAC", gcodec.plname);
1554
1555 EXPECT_TRUE(channel_->SetSendParameters(opus_parameters));
1556 EXPECT_EQ(0, voe_.GetSendCodec(channel_num, gcodec));
1557 EXPECT_EQ(111, gcodec.pltype);
1558 EXPECT_STREQ("opus", gcodec.plname);
1559 }
1560
1561 // Test that we handle various ways of specifying bitrate.
1562 TEST_F(WebRtcVoiceEngineTestFake, SetSendCodecsBitrate) {
1563 EXPECT_TRUE(SetupEngineWithSendStream());
1564 int channel_num = voe_.GetLastChannel();
1565 cricket::AudioSendParameters parameters;
1566 parameters.codecs.push_back(kIsacCodec); // bitrate == 32000
1567 EXPECT_TRUE(channel_->SetSendParameters(parameters));
1568 webrtc::CodecInst gcodec;
1569 EXPECT_EQ(0, voe_.GetSendCodec(channel_num, gcodec));
1570 EXPECT_EQ(103, gcodec.pltype);
1571 EXPECT_STREQ("ISAC", gcodec.plname);
1572 EXPECT_EQ(32000, gcodec.rate);
1573
1574 parameters.codecs[0].bitrate = 0; // bitrate == default
1575 EXPECT_TRUE(channel_->SetSendParameters(parameters));
1576 EXPECT_EQ(0, voe_.GetSendCodec(channel_num, gcodec));
1577 EXPECT_EQ(103, gcodec.pltype);
1578 EXPECT_STREQ("ISAC", gcodec.plname);
1579 EXPECT_EQ(-1, gcodec.rate);
1580
1581 parameters.codecs[0].bitrate = 28000; // bitrate == 28000
1582 EXPECT_TRUE(channel_->SetSendParameters(parameters));
1583 EXPECT_EQ(0, voe_.GetSendCodec(channel_num, gcodec));
1584 EXPECT_EQ(103, gcodec.pltype);
1585 EXPECT_STREQ("ISAC", gcodec.plname);
1586 EXPECT_EQ(28000, gcodec.rate);
1587
1588 parameters.codecs[0] = kPcmuCodec; // bitrate == 64000
1589 EXPECT_TRUE(channel_->SetSendParameters(parameters));
1590 EXPECT_EQ(0, voe_.GetSendCodec(channel_num, gcodec));
1591 EXPECT_EQ(0, gcodec.pltype);
1592 EXPECT_STREQ("PCMU", gcodec.plname);
1593 EXPECT_EQ(64000, gcodec.rate);
1594
1595 parameters.codecs[0].bitrate = 0; // bitrate == default
1596 EXPECT_TRUE(channel_->SetSendParameters(parameters));
1597 EXPECT_EQ(0, voe_.GetSendCodec(channel_num, gcodec));
1598 EXPECT_EQ(0, gcodec.pltype);
1599 EXPECT_STREQ("PCMU", gcodec.plname);
1600 EXPECT_EQ(64000, gcodec.rate);
1601
1602 parameters.codecs[0] = kOpusCodec;
1603 parameters.codecs[0].bitrate = 0; // bitrate == default
1604 EXPECT_TRUE(channel_->SetSendParameters(parameters));
1605 EXPECT_EQ(0, voe_.GetSendCodec(channel_num, gcodec));
1606 EXPECT_EQ(111, gcodec.pltype);
1607 EXPECT_STREQ("opus", gcodec.plname);
1608 EXPECT_EQ(32000, gcodec.rate);
1609 }
1610
1611 // Test that we could set packet size specified in kCodecParamPTime.
1612 TEST_F(WebRtcVoiceEngineTestFake, SetSendCodecsPTimeAsPacketSize) {
1613 EXPECT_TRUE(SetupEngineWithSendStream());
1614 int channel_num = voe_.GetLastChannel();
1615 cricket::AudioSendParameters parameters;
1616 parameters.codecs.push_back(kOpusCodec);
1617 parameters.codecs[0].SetParam(cricket::kCodecParamPTime, 40); // Within range.
1618 EXPECT_TRUE(channel_->SetSendParameters(parameters));
1619 webrtc::CodecInst gcodec;
1620 EXPECT_EQ(0, voe_.GetSendCodec(channel_num, gcodec));
1621 EXPECT_EQ(1920, gcodec.pacsize); // Opus gets 40ms.
1622
1623 parameters.codecs[0].SetParam(cricket::kCodecParamPTime, 5); // Below range.
1624 EXPECT_TRUE(channel_->SetSendParameters(parameters));
1625 EXPECT_EQ(0, voe_.GetSendCodec(channel_num, gcodec));
1626 EXPECT_EQ(480, gcodec.pacsize); // Opus gets 10ms.
1627
1628 parameters.codecs[0].SetParam(cricket::kCodecParamPTime, 80); // Beyond range.
1629 EXPECT_TRUE(channel_->SetSendParameters(parameters));
1630 EXPECT_EQ(0, voe_.GetSendCodec(channel_num, gcodec));
1631 EXPECT_EQ(2880, gcodec.pacsize); // Opus gets 60ms.
1632
1633 parameters.codecs[0] = kIsacCodec; // Also try Isac, with unsupported size.
1634 parameters.codecs[0].SetParam(cricket::kCodecParamPTime, 40); // Within range.
1635 EXPECT_TRUE(channel_->SetSendParameters(parameters));
1636 EXPECT_EQ(0, voe_.GetSendCodec(channel_num, gcodec));
1637 EXPECT_EQ(480, gcodec.pacsize); // Isac gets 30ms as the next smallest value.
1638
1639 parameters.codecs[0] = kG722CodecSdp; // Try G722 @8kHz as negotiated in SDP.
1640 parameters.codecs[0].SetParam(cricket::kCodecParamPTime, 40);
1641 EXPECT_TRUE(channel_->SetSendParameters(parameters));
1642 EXPECT_EQ(0, voe_.GetSendCodec(channel_num, gcodec));
1643 EXPECT_EQ(640, gcodec.pacsize); // G722 gets 40ms @16kHz as defined in VoE.
1644 }
1645
1646 // Test that we fail if no codecs are specified.
1647 TEST_F(WebRtcVoiceEngineTestFake, SetSendCodecsNoCodecs) {
1648 EXPECT_TRUE(SetupEngineWithSendStream());
1649 cricket::AudioSendParameters parameters;
1650 EXPECT_FALSE(channel_->SetSendParameters(parameters));
1651 }
1652
1653 // Test that we can set send codecs even with telephone-event codec as the first
1654 // one on the list.
1655 TEST_F(WebRtcVoiceEngineTestFake, SetSendCodecsDTMFOnTop) {
1656 EXPECT_TRUE(SetupEngineWithSendStream());
1657 int channel_num = voe_.GetLastChannel();
1658 cricket::AudioSendParameters parameters;
1659 parameters.codecs.push_back(kTelephoneEventCodec);
1660 parameters.codecs.push_back(kIsacCodec);
1661 parameters.codecs.push_back(kPcmuCodec);
1662 parameters.codecs[0].id = 98; // DTMF
1663 parameters.codecs[1].id = 96;
1664 EXPECT_TRUE(channel_->SetSendParameters(parameters));
1665 webrtc::CodecInst gcodec;
1666 EXPECT_EQ(0, voe_.GetSendCodec(channel_num, gcodec));
1667 EXPECT_EQ(96, gcodec.pltype);
1668 EXPECT_STREQ("ISAC", gcodec.plname);
1669 EXPECT_TRUE(channel_->CanInsertDtmf());
1670 }
1671
1672 // Test that we can set send codecs even with CN codec as the first
1673 // one on the list.
1674 TEST_F(WebRtcVoiceEngineTestFake, SetSendCodecsCNOnTop) {
1675 EXPECT_TRUE(SetupEngineWithSendStream());
1676 int channel_num = voe_.GetLastChannel();
1677 cricket::AudioSendParameters parameters;
1678 parameters.codecs.push_back(kCn16000Codec);
1679 parameters.codecs.push_back(kIsacCodec);
1680 parameters.codecs.push_back(kPcmuCodec);
1681 parameters.codecs[0].id = 98; // wideband CN
1682 parameters.codecs[1].id = 96;
1683 EXPECT_TRUE(channel_->SetSendParameters(parameters));
1684 webrtc::CodecInst gcodec;
1685 EXPECT_EQ(0, voe_.GetSendCodec(channel_num, gcodec));
1686 EXPECT_EQ(96, gcodec.pltype);
1687 EXPECT_STREQ("ISAC", gcodec.plname);
1688 EXPECT_EQ(98, voe_.GetSendCNPayloadType(channel_num, true));
1689 }
1690
1691 // Test that we set VAD and DTMF types correctly as caller.
1692 TEST_F(WebRtcVoiceEngineTestFake, SetSendCodecsCNandDTMFAsCaller) {
1693 EXPECT_TRUE(SetupEngineWithSendStream());
1694 int channel_num = voe_.GetLastChannel();
1695 cricket::AudioSendParameters parameters;
1696 parameters.codecs.push_back(kIsacCodec);
1697 parameters.codecs.push_back(kPcmuCodec);
1698 // TODO(juberti): cn 32000
1699 parameters.codecs.push_back(kCn16000Codec);
1700 parameters.codecs.push_back(kCn8000Codec);
1701 parameters.codecs.push_back(kTelephoneEventCodec);
1702 parameters.codecs.push_back(kRedCodec);
1703 parameters.codecs[0].id = 96;
1704 parameters.codecs[2].id = 97; // wideband CN
1705 parameters.codecs[4].id = 98; // DTMF
1706 EXPECT_TRUE(channel_->SetSendParameters(parameters));
1707 webrtc::CodecInst gcodec;
1708 EXPECT_EQ(0, voe_.GetSendCodec(channel_num, gcodec));
1709 EXPECT_EQ(96, gcodec.pltype);
1710 EXPECT_STREQ("ISAC", gcodec.plname);
1711 EXPECT_TRUE(voe_.GetVAD(channel_num));
1712 EXPECT_FALSE(voe_.GetRED(channel_num));
1713 EXPECT_EQ(13, voe_.GetSendCNPayloadType(channel_num, false));
1714 EXPECT_EQ(97, voe_.GetSendCNPayloadType(channel_num, true));
1715 EXPECT_TRUE(channel_->CanInsertDtmf());
1716 }
1717
1718 // Test that we set VAD and DTMF types correctly as callee.
1719 TEST_F(WebRtcVoiceEngineTestFake, SetSendCodecsCNandDTMFAsCallee) {
1720 EXPECT_TRUE(engine_.Init(rtc::Thread::Current()));
1721 channel_ = engine_.CreateChannel(&call_, cricket::AudioOptions());
1722 EXPECT_TRUE(channel_ != nullptr);
1723
1724 cricket::AudioSendParameters parameters;
1725 parameters.codecs.push_back(kIsacCodec);
1726 parameters.codecs.push_back(kPcmuCodec);
1727 // TODO(juberti): cn 32000
1728 parameters.codecs.push_back(kCn16000Codec);
1729 parameters.codecs.push_back(kCn8000Codec);
1730 parameters.codecs.push_back(kTelephoneEventCodec);
1731 parameters.codecs.push_back(kRedCodec);
1732 parameters.codecs[0].id = 96;
1733 parameters.codecs[2].id = 97; // wideband CN
1734 parameters.codecs[4].id = 98; // DTMF
1735 EXPECT_TRUE(channel_->SetSendParameters(parameters));
1736 EXPECT_TRUE(channel_->AddSendStream(
1737 cricket::StreamParams::CreateLegacy(kSsrc1)));
1738 int channel_num = voe_.GetLastChannel();
1739
1740 webrtc::CodecInst gcodec;
1741 EXPECT_EQ(0, voe_.GetSendCodec(channel_num, gcodec));
1742 EXPECT_EQ(96, gcodec.pltype);
1743 EXPECT_STREQ("ISAC", gcodec.plname);
1744 EXPECT_TRUE(voe_.GetVAD(channel_num));
1745 EXPECT_FALSE(voe_.GetRED(channel_num));
1746 EXPECT_EQ(13, voe_.GetSendCNPayloadType(channel_num, false));
1747 EXPECT_EQ(97, voe_.GetSendCNPayloadType(channel_num, true));
1748 EXPECT_TRUE(channel_->CanInsertDtmf());
1749 }
1750
1751 // Test that we only apply VAD if we have a CN codec that matches the
1752 // send codec clockrate.
1753 TEST_F(WebRtcVoiceEngineTestFake, SetSendCodecsCNNoMatch) {
1754 EXPECT_TRUE(SetupEngineWithSendStream());
1755 int channel_num = voe_.GetLastChannel();
1756 cricket::AudioSendParameters parameters;
1757 // Set ISAC(16K) and CN(16K). VAD should be activated.
1758 parameters.codecs.push_back(kIsacCodec);
1759 parameters.codecs.push_back(kCn16000Codec);
1760 parameters.codecs[1].id = 97;
1761 EXPECT_TRUE(channel_->SetSendParameters(parameters));
1762 webrtc::CodecInst gcodec;
1763 EXPECT_EQ(0, voe_.GetSendCodec(channel_num, gcodec));
1764 EXPECT_STREQ("ISAC", gcodec.plname);
1765 EXPECT_TRUE(voe_.GetVAD(channel_num));
1766 EXPECT_EQ(97, voe_.GetSendCNPayloadType(channel_num, true));
1767 // Set PCMU(8K) and CN(16K). VAD should not be activated.
1768 parameters.codecs[0] = kPcmuCodec;
1769 EXPECT_TRUE(channel_->SetSendParameters(parameters));
1770 EXPECT_EQ(0, voe_.GetSendCodec(channel_num, gcodec));
1771 EXPECT_STREQ("PCMU", gcodec.plname);
1772 EXPECT_FALSE(voe_.GetVAD(channel_num));
1773 // Set PCMU(8K) and CN(8K). VAD should be activated.
1774 parameters.codecs[1] = kCn8000Codec;
1775 EXPECT_TRUE(channel_->SetSendParameters(parameters));
1776 EXPECT_EQ(0, voe_.GetSendCodec(channel_num, gcodec));
1777 EXPECT_STREQ("PCMU", gcodec.plname);
1778 EXPECT_TRUE(voe_.GetVAD(channel_num));
1779 EXPECT_EQ(13, voe_.GetSendCNPayloadType(channel_num, false));
1780 // Set ISAC(16K) and CN(8K). VAD should not be activated.
1781 parameters.codecs[0] = kIsacCodec;
1782 EXPECT_TRUE(channel_->SetSendParameters(parameters));
1783 EXPECT_EQ(0, voe_.GetSendCodec(channel_num, gcodec));
1784 EXPECT_STREQ("ISAC", gcodec.plname);
1785 EXPECT_FALSE(voe_.GetVAD(channel_num));
1786 }
1787
1788 // Test that we perform case-insensitive matching of codec names.
1789 TEST_F(WebRtcVoiceEngineTestFake, SetSendCodecsCaseInsensitive) {
1790 EXPECT_TRUE(SetupEngineWithSendStream());
1791 int channel_num = voe_.GetLastChannel();
1792 cricket::AudioSendParameters parameters;
1793 parameters.codecs.push_back(kIsacCodec);
1794 parameters.codecs.push_back(kPcmuCodec);
1795 parameters.codecs.push_back(kCn16000Codec);
1796 parameters.codecs.push_back(kCn8000Codec);
1797 parameters.codecs.push_back(kTelephoneEventCodec);
1798 parameters.codecs.push_back(kRedCodec);
1799 parameters.codecs[0].name = "iSaC";
1800 parameters.codecs[0].id = 96;
1801 parameters.codecs[2].id = 97; // wideband CN
1802 parameters.codecs[4].id = 98; // DTMF
1803 EXPECT_TRUE(channel_->SetSendParameters(parameters));
1804 webrtc::CodecInst gcodec;
1805 EXPECT_EQ(0, voe_.GetSendCodec(channel_num, gcodec));
1806 EXPECT_EQ(96, gcodec.pltype);
1807 EXPECT_STREQ("ISAC", gcodec.plname);
1808 EXPECT_TRUE(voe_.GetVAD(channel_num));
1809 EXPECT_FALSE(voe_.GetRED(channel_num));
1810 EXPECT_EQ(13, voe_.GetSendCNPayloadType(channel_num, false));
1811 EXPECT_EQ(97, voe_.GetSendCNPayloadType(channel_num, true));
1812 EXPECT_TRUE(channel_->CanInsertDtmf());
1813 }
1814
1815 // Test that we set up RED correctly as caller.
1816 TEST_F(WebRtcVoiceEngineTestFake, SetSendCodecsREDAsCaller) {
1817 EXPECT_TRUE(SetupEngineWithSendStream());
1818 int channel_num = voe_.GetLastChannel();
1819 cricket::AudioSendParameters parameters;
1820 parameters.codecs.push_back(kRedCodec);
1821 parameters.codecs.push_back(kIsacCodec);
1822 parameters.codecs.push_back(kPcmuCodec);
1823 parameters.codecs[0].id = 127;
1824 parameters.codecs[0].params[""] = "96/96";
1825 parameters.codecs[1].id = 96;
1826 EXPECT_TRUE(channel_->SetSendParameters(parameters));
1827 webrtc::CodecInst gcodec;
1828 EXPECT_EQ(0, voe_.GetSendCodec(channel_num, gcodec));
1829 EXPECT_EQ(96, gcodec.pltype);
1830 EXPECT_STREQ("ISAC", gcodec.plname);
1831 EXPECT_TRUE(voe_.GetRED(channel_num));
1832 EXPECT_EQ(127, voe_.GetSendREDPayloadType(channel_num));
1833 }
1834
1835 // Test that we set up RED correctly as callee.
1836 TEST_F(WebRtcVoiceEngineTestFake, SetSendCodecsREDAsCallee) {
1837 EXPECT_TRUE(engine_.Init(rtc::Thread::Current()));
1838 channel_ = engine_.CreateChannel(&call_, cricket::AudioOptions());
1839 EXPECT_TRUE(channel_ != nullptr);
1840
1841 cricket::AudioSendParameters parameters;
1842 parameters.codecs.push_back(kRedCodec);
1843 parameters.codecs.push_back(kIsacCodec);
1844 parameters.codecs.push_back(kPcmuCodec);
1845 parameters.codecs[0].id = 127;
1846 parameters.codecs[0].params[""] = "96/96";
1847 parameters.codecs[1].id = 96;
1848 EXPECT_TRUE(channel_->SetSendParameters(parameters));
1849 EXPECT_TRUE(channel_->AddSendStream(
1850 cricket::StreamParams::CreateLegacy(kSsrc1)));
1851 int channel_num = voe_.GetLastChannel();
1852 webrtc::CodecInst gcodec;
1853 EXPECT_EQ(0, voe_.GetSendCodec(channel_num, gcodec));
1854 EXPECT_EQ(96, gcodec.pltype);
1855 EXPECT_STREQ("ISAC", gcodec.plname);
1856 EXPECT_TRUE(voe_.GetRED(channel_num));
1857 EXPECT_EQ(127, voe_.GetSendREDPayloadType(channel_num));
1858 }
1859
1860 // Test that we set up RED correctly if params are omitted.
1861 TEST_F(WebRtcVoiceEngineTestFake, SetSendCodecsREDNoParams) {
1862 EXPECT_TRUE(SetupEngineWithSendStream());
1863 int channel_num = voe_.GetLastChannel();
1864 cricket::AudioSendParameters parameters;
1865 parameters.codecs.push_back(kRedCodec);
1866 parameters.codecs.push_back(kIsacCodec);
1867 parameters.codecs.push_back(kPcmuCodec);
1868 parameters.codecs[0].id = 127;
1869 parameters.codecs[1].id = 96;
1870 EXPECT_TRUE(channel_->SetSendParameters(parameters));
1871 webrtc::CodecInst gcodec;
1872 EXPECT_EQ(0, voe_.GetSendCodec(channel_num, gcodec));
1873 EXPECT_EQ(96, gcodec.pltype);
1874 EXPECT_STREQ("ISAC", gcodec.plname);
1875 EXPECT_TRUE(voe_.GetRED(channel_num));
1876 EXPECT_EQ(127, voe_.GetSendREDPayloadType(channel_num));
1877 }
1878
1879 // Test that we ignore RED if the parameters aren't named the way we expect.
1880 TEST_F(WebRtcVoiceEngineTestFake, SetSendCodecsBadRED1) {
1881 EXPECT_TRUE(SetupEngineWithSendStream());
1882 int channel_num = voe_.GetLastChannel();
1883 cricket::AudioSendParameters parameters;
1884 parameters.codecs.push_back(kRedCodec);
1885 parameters.codecs.push_back(kIsacCodec);
1886 parameters.codecs.push_back(kPcmuCodec);
1887 parameters.codecs[0].id = 127;
1888 parameters.codecs[0].params["ABC"] = "96/96";
1889 parameters.codecs[1].id = 96;
1890 EXPECT_TRUE(channel_->SetSendParameters(parameters));
1891 webrtc::CodecInst gcodec;
1892 EXPECT_EQ(0, voe_.GetSendCodec(channel_num, gcodec));
1893 EXPECT_EQ(96, gcodec.pltype);
1894 EXPECT_STREQ("ISAC", gcodec.plname);
1895 EXPECT_FALSE(voe_.GetRED(channel_num));
1896 }
1897
1898 // Test that we ignore RED if it uses different primary/secondary encoding.
1899 TEST_F(WebRtcVoiceEngineTestFake, SetSendCodecsBadRED2) {
1900 EXPECT_TRUE(SetupEngineWithSendStream());
1901 int channel_num = voe_.GetLastChannel();
1902 cricket::AudioSendParameters parameters;
1903 parameters.codecs.push_back(kRedCodec);
1904 parameters.codecs.push_back(kIsacCodec);
1905 parameters.codecs.push_back(kPcmuCodec);
1906 parameters.codecs[0].id = 127;
1907 parameters.codecs[0].params[""] = "96/0";
1908 parameters.codecs[1].id = 96;
1909 EXPECT_TRUE(channel_->SetSendParameters(parameters));
1910 webrtc::CodecInst gcodec;
1911 EXPECT_EQ(0, voe_.GetSendCodec(channel_num, gcodec));
1912 EXPECT_EQ(96, gcodec.pltype);
1913 EXPECT_STREQ("ISAC", gcodec.plname);
1914 EXPECT_FALSE(voe_.GetRED(channel_num));
1915 }
1916
1917 // Test that we ignore RED if it uses more than 2 encodings.
1918 TEST_F(WebRtcVoiceEngineTestFake, SetSendCodecsBadRED3) {
1919 EXPECT_TRUE(SetupEngineWithSendStream());
1920 int channel_num = voe_.GetLastChannel();
1921 cricket::AudioSendParameters parameters;
1922 parameters.codecs.push_back(kRedCodec);
1923 parameters.codecs.push_back(kIsacCodec);
1924 parameters.codecs.push_back(kPcmuCodec);
1925 parameters.codecs[0].id = 127;
1926 parameters.codecs[0].params[""] = "96/96/96";
1927 parameters.codecs[1].id = 96;
1928 EXPECT_TRUE(channel_->SetSendParameters(parameters));
1929 webrtc::CodecInst gcodec;
1930 EXPECT_EQ(0, voe_.GetSendCodec(channel_num, gcodec));
1931 EXPECT_EQ(96, gcodec.pltype);
1932 EXPECT_STREQ("ISAC", gcodec.plname);
1933 EXPECT_FALSE(voe_.GetRED(channel_num));
1934 }
1935
1936 // Test that we ignore RED if it has bogus codec ids.
1937 TEST_F(WebRtcVoiceEngineTestFake, SetSendCodecsBadRED4) {
1938 EXPECT_TRUE(SetupEngineWithSendStream());
1939 int channel_num = voe_.GetLastChannel();
1940 cricket::AudioSendParameters parameters;
1941 parameters.codecs.push_back(kRedCodec);
1942 parameters.codecs.push_back(kIsacCodec);
1943 parameters.codecs.push_back(kPcmuCodec);
1944 parameters.codecs[0].id = 127;
1945 parameters.codecs[0].params[""] = "ABC/ABC";
1946 parameters.codecs[1].id = 96;
1947 EXPECT_TRUE(channel_->SetSendParameters(parameters));
1948 webrtc::CodecInst gcodec;
1949 EXPECT_EQ(0, voe_.GetSendCodec(channel_num, gcodec));
1950 EXPECT_EQ(96, gcodec.pltype);
1951 EXPECT_STREQ("ISAC", gcodec.plname);
1952 EXPECT_FALSE(voe_.GetRED(channel_num));
1953 }
1954
1955 // Test that we ignore RED if it refers to a codec that is not present.
1956 TEST_F(WebRtcVoiceEngineTestFake, SetSendCodecsBadRED5) {
1957 EXPECT_TRUE(SetupEngineWithSendStream());
1958 int channel_num = voe_.GetLastChannel();
1959 cricket::AudioSendParameters parameters;
1960 parameters.codecs.push_back(kRedCodec);
1961 parameters.codecs.push_back(kIsacCodec);
1962 parameters.codecs.push_back(kPcmuCodec);
1963 parameters.codecs[0].id = 127;
1964 parameters.codecs[0].params[""] = "97/97";
1965 parameters.codecs[1].id = 96;
1966 EXPECT_TRUE(channel_->SetSendParameters(parameters));
1967 webrtc::CodecInst gcodec;
1968 EXPECT_EQ(0, voe_.GetSendCodec(channel_num, gcodec));
1969 EXPECT_EQ(96, gcodec.pltype);
1970 EXPECT_STREQ("ISAC", gcodec.plname);
1971 EXPECT_FALSE(voe_.GetRED(channel_num));
1972 }
1973
1974 class WebRtcVoiceEngineWithSendSideBweTest : public WebRtcVoiceEngineTestFake {
1975 public:
1976 WebRtcVoiceEngineWithSendSideBweTest()
1977 : WebRtcVoiceEngineTestFake("WebRTC-Audio-SendSideBwe/Enabled/") {}
1978 };
1979
1980 TEST_F(WebRtcVoiceEngineWithSendSideBweTest,
1981 SupportsTransportSequenceNumberHeaderExtension) {
1982 cricket::RtpCapabilities capabilities = engine_.GetCapabilities();
1983 ASSERT_FALSE(capabilities.header_extensions.empty());
1984 for (const cricket::RtpHeaderExtension& extension :
1985 capabilities.header_extensions) {
1986 if (extension.uri == cricket::kRtpTransportSequenceNumberHeaderExtension) {
1987 EXPECT_EQ(cricket::kRtpTransportSequenceNumberHeaderExtensionDefaultId,
1988 extension.id);
1989 return;
1990 }
1991 }
1992 FAIL() << "Transport sequence number extension not in header-extension list.";
1993 }
1994
1995 // Test support for audio level header extension.
1996 TEST_F(WebRtcVoiceEngineTestFake, SendAudioLevelHeaderExtensions) {
1997 TestSetSendRtpHeaderExtensions(kRtpAudioLevelHeaderExtension);
1998 }
1999 TEST_F(WebRtcVoiceEngineTestFake, RecvAudioLevelHeaderExtensions) {
2000 TestSetRecvRtpHeaderExtensions(kRtpAudioLevelHeaderExtension);
2001 }
2002
2003 // Test support for absolute send time header extension.
2004 TEST_F(WebRtcVoiceEngineTestFake, SendAbsoluteSendTimeHeaderExtensions) {
2005 TestSetSendRtpHeaderExtensions(kRtpAbsoluteSenderTimeHeaderExtension);
2006 }
2007 TEST_F(WebRtcVoiceEngineTestFake, RecvAbsoluteSendTimeHeaderExtensions) {
2008 TestSetRecvRtpHeaderExtensions(kRtpAbsoluteSenderTimeHeaderExtension);
2009 }
2010
2011 // Test that we can create a channel and start sending on it.
2012 TEST_F(WebRtcVoiceEngineTestFake, Send) {
2013 EXPECT_TRUE(SetupEngineWithSendStream());
2014 int channel_num = voe_.GetLastChannel();
2015 EXPECT_TRUE(channel_->SetSendParameters(send_parameters_));
2016 EXPECT_TRUE(channel_->SetSend(cricket::SEND_MICROPHONE));
2017 EXPECT_TRUE(voe_.GetSend(channel_num));
2018 EXPECT_TRUE(channel_->SetSend(cricket::SEND_NOTHING));
2019 EXPECT_FALSE(voe_.GetSend(channel_num));
2020 }
2021
2022 // Test that we can create a channel and start playing out on it.
2023 TEST_F(WebRtcVoiceEngineTestFake, Playout) {
2024 EXPECT_TRUE(SetupEngineWithRecvStream());
2025 int channel_num = voe_.GetLastChannel();
2026 EXPECT_TRUE(channel_->SetRecvParameters(recv_parameters_));
2027 EXPECT_TRUE(channel_->SetPlayout(true));
2028 EXPECT_TRUE(voe_.GetPlayout(channel_num));
2029 EXPECT_TRUE(channel_->SetPlayout(false));
2030 EXPECT_FALSE(voe_.GetPlayout(channel_num));
2031 }
2032
2033 // Test that we can add and remove send streams.
2034 TEST_F(WebRtcVoiceEngineTestFake, CreateAndDeleteMultipleSendStreams) {
2035 SetupForMultiSendStream();
2036
2037 // Set the global state for sending.
2038 EXPECT_TRUE(channel_->SetSend(cricket::SEND_MICROPHONE));
2039
2040 for (uint32_t ssrc : kSsrcs4) {
2041 EXPECT_TRUE(channel_->AddSendStream(
2042 cricket::StreamParams::CreateLegacy(ssrc)));
2043 // Verify that we are in a sending state for all the created streams.
2044 EXPECT_TRUE(voe_.GetSend(GetSendStreamConfig(ssrc).voe_channel_id));
2045 }
2046 EXPECT_EQ(arraysize(kSsrcs4), call_.GetAudioSendStreams().size());
2047
2048 // Delete the send streams.
2049 for (uint32_t ssrc : kSsrcs4) {
2050 EXPECT_TRUE(channel_->RemoveSendStream(ssrc));
2051 EXPECT_FALSE(call_.GetAudioSendStream(ssrc));
2052 EXPECT_FALSE(channel_->RemoveSendStream(ssrc));
2053 }
2054 EXPECT_EQ(0u, call_.GetAudioSendStreams().size());
2055 }
2056
2057 // Test SetSendCodecs correctly configure the codecs in all send streams.
2058 TEST_F(WebRtcVoiceEngineTestFake, SetSendCodecsWithMultipleSendStreams) {
2059 SetupForMultiSendStream();
2060
2061 // Create send streams.
2062 for (uint32_t ssrc : kSsrcs4) {
2063 EXPECT_TRUE(channel_->AddSendStream(
2064 cricket::StreamParams::CreateLegacy(ssrc)));
2065 }
2066
2067 cricket::AudioSendParameters parameters;
2068 // Set ISAC(16K) and CN(16K). VAD should be activated.
2069 parameters.codecs.push_back(kIsacCodec);
2070 parameters.codecs.push_back(kCn16000Codec);
2071 parameters.codecs[1].id = 97;
2072 EXPECT_TRUE(channel_->SetSendParameters(parameters));
2073
2074 // Verify ISAC and VAD are corrected configured on all send channels.
2075 webrtc::CodecInst gcodec;
2076 for (uint32_t ssrc : kSsrcs4) {
2077 int channel_num = GetSendStreamConfig(ssrc).voe_channel_id;
2078 EXPECT_EQ(0, voe_.GetSendCodec(channel_num, gcodec));
2079 EXPECT_STREQ("ISAC", gcodec.plname);
2080 EXPECT_TRUE(voe_.GetVAD(channel_num));
2081 EXPECT_EQ(97, voe_.GetSendCNPayloadType(channel_num, true));
2082 }
2083
2084 // Change to PCMU(8K) and CN(16K). VAD should not be activated.
2085 parameters.codecs[0] = kPcmuCodec;
2086 EXPECT_TRUE(channel_->SetSendParameters(parameters));
2087 for (uint32_t ssrc : kSsrcs4) {
2088 int channel_num = GetSendStreamConfig(ssrc).voe_channel_id;
2089 EXPECT_EQ(0, voe_.GetSendCodec(channel_num, gcodec));
2090 EXPECT_STREQ("PCMU", gcodec.plname);
2091 EXPECT_FALSE(voe_.GetVAD(channel_num));
2092 }
2093 }
2094
2095 // Test we can SetSend on all send streams correctly.
2096 TEST_F(WebRtcVoiceEngineTestFake, SetSendWithMultipleSendStreams) {
2097 SetupForMultiSendStream();
2098
2099 // Create the send channels and they should be a SEND_NOTHING date.
2100 for (uint32_t ssrc : kSsrcs4) {
2101 EXPECT_TRUE(channel_->AddSendStream(
2102 cricket::StreamParams::CreateLegacy(ssrc)));
2103 int channel_num = voe_.GetLastChannel();
2104 EXPECT_FALSE(voe_.GetSend(channel_num));
2105 }
2106
2107 // Set the global state for starting sending.
2108 EXPECT_TRUE(channel_->SetSend(cricket::SEND_MICROPHONE));
2109 for (uint32_t ssrc : kSsrcs4) {
2110 // Verify that we are in a sending state for all the send streams.
2111 int channel_num = GetSendStreamConfig(ssrc).voe_channel_id;
2112 EXPECT_TRUE(voe_.GetSend(channel_num));
2113 }
2114
2115 // Set the global state for stopping sending.
2116 EXPECT_TRUE(channel_->SetSend(cricket::SEND_NOTHING));
2117 for (uint32_t ssrc : kSsrcs4) {
2118 // Verify that we are in a stop state for all the send streams.
2119 int channel_num = GetSendStreamConfig(ssrc).voe_channel_id;
2120 EXPECT_FALSE(voe_.GetSend(channel_num));
2121 }
2122 }
2123
2124 // Test we can set the correct statistics on all send streams.
2125 TEST_F(WebRtcVoiceEngineTestFake, GetStatsWithMultipleSendStreams) {
2126 SetupForMultiSendStream();
2127
2128 // Create send streams.
2129 for (uint32_t ssrc : kSsrcs4) {
2130 EXPECT_TRUE(channel_->AddSendStream(
2131 cricket::StreamParams::CreateLegacy(ssrc)));
2132 }
2133 SetAudioSendStreamStats();
2134
2135 // Create a receive stream to check that none of the send streams end up in
2136 // the receive stream stats.
2137 EXPECT_TRUE(channel_->AddRecvStream(
2138 cricket::StreamParams::CreateLegacy(kSsrc2)));
2139 // We need send codec to be set to get all stats.
2140 EXPECT_TRUE(channel_->SetSendParameters(send_parameters_));
2141 EXPECT_TRUE(channel_->SetRecvParameters(recv_parameters_));
2142
2143 // Check stats for the added streams.
2144 {
2145 cricket::VoiceMediaInfo info;
2146 EXPECT_EQ(true, channel_->GetStats(&info));
2147
2148 // We have added 4 send streams. We should see empty stats for all.
2149 EXPECT_EQ(static_cast<size_t>(arraysize(kSsrcs4)), info.senders.size());
2150 for (const auto& sender : info.senders) {
2151 VerifyVoiceSenderInfo(sender, false);
2152 }
2153
2154 // We have added one receive stream. We should see empty stats.
2155 EXPECT_EQ(info.receivers.size(), 1u);
2156 EXPECT_EQ(info.receivers[0].ssrc(), 0);
2157 }
2158
2159 // Remove the kSsrc2 stream. No receiver stats.
2160 {
2161 cricket::VoiceMediaInfo info;
2162 EXPECT_TRUE(channel_->RemoveRecvStream(kSsrc2));
2163 EXPECT_EQ(true, channel_->GetStats(&info));
2164 EXPECT_EQ(static_cast<size_t>(arraysize(kSsrcs4)), info.senders.size());
2165 EXPECT_EQ(0u, info.receivers.size());
2166 }
2167
2168 // Deliver a new packet - a default receive stream should be created and we
2169 // should see stats again.
2170 {
2171 cricket::VoiceMediaInfo info;
2172 DeliverPacket(kPcmuFrame, sizeof(kPcmuFrame));
2173 SetAudioReceiveStreamStats();
2174 EXPECT_EQ(true, channel_->GetStats(&info));
2175 EXPECT_EQ(static_cast<size_t>(arraysize(kSsrcs4)), info.senders.size());
2176 EXPECT_EQ(1u, info.receivers.size());
2177 VerifyVoiceReceiverInfo(info.receivers[0]);
2178 }
2179 }
2180
2181 // Test that we can add and remove receive streams, and do proper send/playout.
2182 // We can receive on multiple streams while sending one stream.
2183 TEST_F(WebRtcVoiceEngineTestFake, PlayoutWithMultipleStreams) {
2184 EXPECT_TRUE(SetupEngineWithSendStream());
2185 int channel_num1 = voe_.GetLastChannel();
2186
2187 // Start playout without a receive stream.
2188 EXPECT_TRUE(channel_->SetSendParameters(send_parameters_));
2189 EXPECT_TRUE(channel_->SetPlayout(true));
2190 EXPECT_FALSE(voe_.GetPlayout(channel_num1));
2191
2192 // Adding another stream should enable playout on the new stream only.
2193 EXPECT_TRUE(channel_->AddRecvStream(cricket::StreamParams::CreateLegacy(2)));
2194 int channel_num2 = voe_.GetLastChannel();
2195 EXPECT_TRUE(channel_->SetSend(cricket::SEND_MICROPHONE));
2196 EXPECT_TRUE(voe_.GetSend(channel_num1));
2197 EXPECT_FALSE(voe_.GetSend(channel_num2));
2198
2199 // Make sure only the new stream is played out.
2200 EXPECT_FALSE(voe_.GetPlayout(channel_num1));
2201 EXPECT_TRUE(voe_.GetPlayout(channel_num2));
2202
2203 // Adding yet another stream should have stream 2 and 3 enabled for playout.
2204 EXPECT_TRUE(channel_->AddRecvStream(cricket::StreamParams::CreateLegacy(3)));
2205 int channel_num3 = voe_.GetLastChannel();
2206 EXPECT_FALSE(voe_.GetPlayout(channel_num1));
2207 EXPECT_TRUE(voe_.GetPlayout(channel_num2));
2208 EXPECT_TRUE(voe_.GetPlayout(channel_num3));
2209 EXPECT_FALSE(voe_.GetSend(channel_num3));
2210
2211 // Stop sending.
2212 EXPECT_TRUE(channel_->SetSend(cricket::SEND_NOTHING));
2213 EXPECT_FALSE(voe_.GetSend(channel_num1));
2214 EXPECT_FALSE(voe_.GetSend(channel_num2));
2215 EXPECT_FALSE(voe_.GetSend(channel_num3));
2216
2217 // Stop playout.
2218 EXPECT_TRUE(channel_->SetPlayout(false));
2219 EXPECT_FALSE(voe_.GetPlayout(channel_num1));
2220 EXPECT_FALSE(voe_.GetPlayout(channel_num2));
2221 EXPECT_FALSE(voe_.GetPlayout(channel_num3));
2222
2223 // Restart playout and make sure only recv streams are played out.
2224 EXPECT_TRUE(channel_->SetPlayout(true));
2225 EXPECT_FALSE(voe_.GetPlayout(channel_num1));
2226 EXPECT_TRUE(voe_.GetPlayout(channel_num2));
2227 EXPECT_TRUE(voe_.GetPlayout(channel_num3));
2228
2229 // Now remove the recv streams and verify that the send stream doesn't play.
2230 EXPECT_TRUE(channel_->RemoveRecvStream(3));
2231 EXPECT_TRUE(channel_->RemoveRecvStream(2));
2232 EXPECT_FALSE(voe_.GetPlayout(channel_num1));
2233 }
2234
2235 // Test that we can create a channel configured for Codian bridges,
2236 // and start sending on it.
2237 TEST_F(WebRtcVoiceEngineTestFake, CodianSend) {
2238 EXPECT_TRUE(SetupEngineWithSendStream());
2239 cricket::AudioOptions options_adjust_agc;
2240 options_adjust_agc.adjust_agc_delta = rtc::Optional<int>(-10);
2241 int channel_num = voe_.GetLastChannel();
2242 webrtc::AgcConfig agc_config;
2243 EXPECT_EQ(0, voe_.GetAgcConfig(agc_config));
2244 EXPECT_EQ(0, agc_config.targetLeveldBOv);
2245 send_parameters_.options = options_adjust_agc;
2246 EXPECT_TRUE(channel_->SetSendParameters(send_parameters_));
2247 EXPECT_TRUE(channel_->SetSend(cricket::SEND_MICROPHONE));
2248 EXPECT_TRUE(voe_.GetSend(channel_num));
2249 EXPECT_EQ(0, voe_.GetAgcConfig(agc_config));
2250 EXPECT_EQ(agc_config.targetLeveldBOv, 10); // level was attenuated
2251 EXPECT_TRUE(channel_->SetSend(cricket::SEND_NOTHING));
2252 EXPECT_FALSE(voe_.GetSend(channel_num));
2253 EXPECT_EQ(0, voe_.GetAgcConfig(agc_config));
2254 }
2255
2256 TEST_F(WebRtcVoiceEngineTestFake, TxAgcConfigViaOptions) {
2257 EXPECT_TRUE(SetupEngineWithSendStream());
2258 webrtc::AgcConfig agc_config;
2259 EXPECT_EQ(0, voe_.GetAgcConfig(agc_config));
2260 EXPECT_EQ(0, agc_config.targetLeveldBOv);
2261 send_parameters_.options.tx_agc_target_dbov = rtc::Optional<uint16_t>(3);
2262 send_parameters_.options.tx_agc_digital_compression_gain =
2263 rtc::Optional<uint16_t>(9);
2264 send_parameters_.options.tx_agc_limiter = rtc::Optional<bool>(true);
2265 send_parameters_.options.auto_gain_control = rtc::Optional<bool>(true);
2266 EXPECT_TRUE(channel_->SetSendParameters(send_parameters_));
2267 EXPECT_EQ(0, voe_.GetAgcConfig(agc_config));
2268 EXPECT_EQ(3, agc_config.targetLeveldBOv);
2269 EXPECT_EQ(9, agc_config.digitalCompressionGaindB);
2270 EXPECT_TRUE(agc_config.limiterEnable);
2271
2272 // Check interaction with adjust_agc_delta. Both should be respected, for
2273 // backwards compatibility.
2274 send_parameters_.options.adjust_agc_delta = rtc::Optional<int>(-10);
2275 EXPECT_TRUE(channel_->SetSendParameters(send_parameters_));
2276 EXPECT_EQ(0, voe_.GetAgcConfig(agc_config));
2277 EXPECT_EQ(13, agc_config.targetLeveldBOv);
2278 }
2279
2280 TEST_F(WebRtcVoiceEngineTestFake, SampleRatesViaOptions) {
2281 EXPECT_TRUE(SetupEngineWithSendStream());
2282 send_parameters_.options.recording_sample_rate =
2283 rtc::Optional<uint32_t>(48000);
2284 send_parameters_.options.playout_sample_rate = rtc::Optional<uint32_t>(44100);
2285 EXPECT_TRUE(channel_->SetSendParameters(send_parameters_));
2286
2287 unsigned int recording_sample_rate, playout_sample_rate;
2288 EXPECT_EQ(0, voe_.RecordingSampleRate(&recording_sample_rate));
2289 EXPECT_EQ(0, voe_.PlayoutSampleRate(&playout_sample_rate));
2290 EXPECT_EQ(48000u, recording_sample_rate);
2291 EXPECT_EQ(44100u, playout_sample_rate);
2292 }
2293
2294 // Test that we can set the outgoing SSRC properly.
2295 // SSRC is set in SetupEngine by calling AddSendStream.
2296 TEST_F(WebRtcVoiceEngineTestFake, SetSendSsrc) {
2297 EXPECT_TRUE(SetupEngineWithSendStream());
2298 EXPECT_TRUE(call_.GetAudioSendStream(kSsrc1));
2299 }
2300
2301 TEST_F(WebRtcVoiceEngineTestFake, GetStats) {
2302 // Setup. We need send codec to be set to get all stats.
2303 EXPECT_TRUE(SetupEngineWithSendStream());
2304 SetAudioSendStreamStats();
2305 // SetupEngineWithSendStream adds a send stream with kSsrc1, so the receive
2306 // stream has to use a different SSRC.
2307 EXPECT_TRUE(channel_->AddRecvStream(
2308 cricket::StreamParams::CreateLegacy(kSsrc2)));
2309 EXPECT_TRUE(channel_->SetSendParameters(send_parameters_));
2310 EXPECT_TRUE(channel_->SetRecvParameters(recv_parameters_));
2311
2312 // Check stats for the added streams.
2313 {
2314 cricket::VoiceMediaInfo info;
2315 EXPECT_EQ(true, channel_->GetStats(&info));
2316
2317 // We have added one send stream. We should see the stats we've set.
2318 EXPECT_EQ(1u, info.senders.size());
2319 VerifyVoiceSenderInfo(info.senders[0], false);
2320 // We have added one receive stream. We should see empty stats.
2321 EXPECT_EQ(info.receivers.size(), 1u);
2322 EXPECT_EQ(info.receivers[0].ssrc(), 0);
2323 }
2324
2325 // Start sending - this affects some reported stats.
2326 {
2327 cricket::VoiceMediaInfo info;
2328 EXPECT_TRUE(channel_->SetSend(cricket::SEND_MICROPHONE));
2329 EXPECT_EQ(true, channel_->GetStats(&info));
2330 VerifyVoiceSenderInfo(info.senders[0], true);
2331 }
2332
2333 // Remove the kSsrc2 stream. No receiver stats.
2334 {
2335 cricket::VoiceMediaInfo info;
2336 EXPECT_TRUE(channel_->RemoveRecvStream(kSsrc2));
2337 EXPECT_EQ(true, channel_->GetStats(&info));
2338 EXPECT_EQ(1u, info.senders.size());
2339 EXPECT_EQ(0u, info.receivers.size());
2340 }
2341
2342 // Deliver a new packet - a default receive stream should be created and we
2343 // should see stats again.
2344 {
2345 cricket::VoiceMediaInfo info;
2346 DeliverPacket(kPcmuFrame, sizeof(kPcmuFrame));
2347 SetAudioReceiveStreamStats();
2348 EXPECT_EQ(true, channel_->GetStats(&info));
2349 EXPECT_EQ(1u, info.senders.size());
2350 EXPECT_EQ(1u, info.receivers.size());
2351 VerifyVoiceReceiverInfo(info.receivers[0]);
2352 }
2353 }
2354
2355 // Test that we can set the outgoing SSRC properly with multiple streams.
2356 // SSRC is set in SetupEngine by calling AddSendStream.
2357 TEST_F(WebRtcVoiceEngineTestFake, SetSendSsrcWithMultipleStreams) {
2358 EXPECT_TRUE(SetupEngineWithSendStream());
2359 EXPECT_TRUE(call_.GetAudioSendStream(kSsrc1));
2360 EXPECT_TRUE(channel_->AddRecvStream(
2361 cricket::StreamParams::CreateLegacy(kSsrc2)));
2362 EXPECT_EQ(kSsrc1, GetRecvStreamConfig(kSsrc2).rtp.local_ssrc);
2363 }
2364
2365 // Test that the local SSRC is the same on sending and receiving channels if the
2366 // receive channel is created before the send channel.
2367 TEST_F(WebRtcVoiceEngineTestFake, SetSendSsrcAfterCreatingReceiveChannel) {
2368 EXPECT_TRUE(engine_.Init(rtc::Thread::Current()));
2369 channel_ = engine_.CreateChannel(&call_, cricket::AudioOptions());
2370
2371 EXPECT_TRUE(channel_->AddRecvStream(cricket::StreamParams::CreateLegacy(1)));
2372 int receive_channel_num = voe_.GetLastChannel();
2373 EXPECT_TRUE(channel_->AddSendStream(
2374 cricket::StreamParams::CreateLegacy(1234)));
2375
2376 EXPECT_TRUE(call_.GetAudioSendStream(1234));
2377 EXPECT_EQ(1234U, voe_.GetLocalSSRC(receive_channel_num));
2378 }
2379
2380 // Test that we can properly receive packets.
2381 TEST_F(WebRtcVoiceEngineTestFake, Recv) {
2382 EXPECT_TRUE(SetupEngine());
2383 EXPECT_TRUE(channel_->AddRecvStream(cricket::StreamParams::CreateLegacy(1)));
2384 DeliverPacket(kPcmuFrame, sizeof(kPcmuFrame));
2385 int channel_num = voe_.GetLastChannel();
2386 EXPECT_TRUE(voe_.CheckPacket(channel_num, kPcmuFrame, sizeof(kPcmuFrame)));
2387 }
2388
2389 // Test that we can properly receive packets on multiple streams.
2390 TEST_F(WebRtcVoiceEngineTestFake, RecvWithMultipleStreams) {
2391 EXPECT_TRUE(SetupEngine());
2392 EXPECT_TRUE(channel_->AddRecvStream(cricket::StreamParams::CreateLegacy(1)));
2393 int channel_num1 = voe_.GetLastChannel();
2394 EXPECT_TRUE(channel_->AddRecvStream(cricket::StreamParams::CreateLegacy(2)));
2395 int channel_num2 = voe_.GetLastChannel();
2396 EXPECT_TRUE(channel_->AddRecvStream(cricket::StreamParams::CreateLegacy(3)));
2397 int channel_num3 = voe_.GetLastChannel();
2398 // Create packets with the right SSRCs.
2399 char packets[4][sizeof(kPcmuFrame)];
2400 for (size_t i = 0; i < arraysize(packets); ++i) {
2401 memcpy(packets[i], kPcmuFrame, sizeof(kPcmuFrame));
2402 rtc::SetBE32(packets[i] + 8, static_cast<uint32_t>(i));
2403 }
2404 EXPECT_TRUE(voe_.CheckNoPacket(channel_num1));
2405 EXPECT_TRUE(voe_.CheckNoPacket(channel_num2));
2406 EXPECT_TRUE(voe_.CheckNoPacket(channel_num3));
2407
2408 DeliverPacket(packets[0], sizeof(packets[0]));
2409 EXPECT_TRUE(voe_.CheckNoPacket(channel_num1));
2410 EXPECT_TRUE(voe_.CheckNoPacket(channel_num2));
2411 EXPECT_TRUE(voe_.CheckNoPacket(channel_num3));
2412
2413 DeliverPacket(packets[1], sizeof(packets[1]));
2414 EXPECT_TRUE(voe_.CheckPacket(channel_num1, packets[1], sizeof(packets[1])));
2415 EXPECT_TRUE(voe_.CheckNoPacket(channel_num2));
2416 EXPECT_TRUE(voe_.CheckNoPacket(channel_num3));
2417
2418 DeliverPacket(packets[2], sizeof(packets[2]));
2419 EXPECT_TRUE(voe_.CheckNoPacket(channel_num1));
2420 EXPECT_TRUE(voe_.CheckPacket(channel_num2, packets[2], sizeof(packets[2])));
2421 EXPECT_TRUE(voe_.CheckNoPacket(channel_num3));
2422
2423 DeliverPacket(packets[3], sizeof(packets[3]));
2424 EXPECT_TRUE(voe_.CheckNoPacket(channel_num1));
2425 EXPECT_TRUE(voe_.CheckNoPacket(channel_num2));
2426 EXPECT_TRUE(voe_.CheckPacket(channel_num3, packets[3], sizeof(packets[3])));
2427
2428 EXPECT_TRUE(channel_->RemoveRecvStream(3));
2429 EXPECT_TRUE(channel_->RemoveRecvStream(2));
2430 EXPECT_TRUE(channel_->RemoveRecvStream(1));
2431 }
2432
2433 // Test that receiving on an unsignalled stream works (default channel will be
2434 // created).
2435 TEST_F(WebRtcVoiceEngineTestFake, RecvUnsignalled) {
2436 EXPECT_TRUE(SetupEngine());
2437 DeliverPacket(kPcmuFrame, sizeof(kPcmuFrame));
2438 int channel_num = voe_.GetLastChannel();
2439 EXPECT_TRUE(voe_.CheckPacket(channel_num, kPcmuFrame, sizeof(kPcmuFrame)));
2440 }
2441
2442 // Test that receiving on an unsignalled stream works (default channel will be
2443 // created), and that packets will be forwarded to the default channel
2444 // regardless of their SSRCs.
2445 TEST_F(WebRtcVoiceEngineTestFake, RecvUnsignalledWithSsrcSwitch) {
2446 EXPECT_TRUE(SetupEngine());
2447 char packet[sizeof(kPcmuFrame)];
2448 memcpy(packet, kPcmuFrame, sizeof(kPcmuFrame));
2449
2450 // Note that the first unknown SSRC cannot be 0, because we only support
2451 // creating receive streams for SSRC!=0.
2452 DeliverPacket(packet, sizeof(packet));
2453 int channel_num = voe_.GetLastChannel();
2454 EXPECT_TRUE(voe_.CheckPacket(channel_num, packet, sizeof(packet)));
2455 // Once we have the default channel, SSRC==0 will be ok.
2456 for (uint32_t ssrc = 0; ssrc < 10; ++ssrc) {
2457 rtc::SetBE32(&packet[8], ssrc);
2458 DeliverPacket(packet, sizeof(packet));
2459 EXPECT_TRUE(voe_.CheckPacket(channel_num, packet, sizeof(packet)));
2460 }
2461 }
2462
2463 // Test that a default channel is created even after a signalled stream has been
2464 // added, and that this stream will get any packets for unknown SSRCs.
2465 TEST_F(WebRtcVoiceEngineTestFake, RecvUnsignalledAfterSignalled) {
2466 EXPECT_TRUE(SetupEngine());
2467 char packet[sizeof(kPcmuFrame)];
2468 memcpy(packet, kPcmuFrame, sizeof(kPcmuFrame));
2469
2470 // Add a known stream, send packet and verify we got it.
2471 EXPECT_TRUE(channel_->AddRecvStream(cricket::StreamParams::CreateLegacy(1)));
2472 int signalled_channel_num = voe_.GetLastChannel();
2473 DeliverPacket(packet, sizeof(packet));
2474 EXPECT_TRUE(voe_.CheckPacket(signalled_channel_num, packet, sizeof(packet)));
2475
2476 // Note that the first unknown SSRC cannot be 0, because we only support
2477 // creating receive streams for SSRC!=0.
2478 rtc::SetBE32(&packet[8], 7011);
2479 DeliverPacket(packet, sizeof(packet));
2480 int channel_num = voe_.GetLastChannel();
2481 EXPECT_NE(channel_num, signalled_channel_num);
2482 EXPECT_TRUE(voe_.CheckPacket(channel_num, packet, sizeof(packet)));
2483 // Once we have the default channel, SSRC==0 will be ok.
2484 for (uint32_t ssrc = 0; ssrc < 20; ssrc += 2) {
2485 rtc::SetBE32(&packet[8], ssrc);
2486 DeliverPacket(packet, sizeof(packet));
2487 EXPECT_TRUE(voe_.CheckPacket(channel_num, packet, sizeof(packet)));
2488 }
2489 }
2490
2491 // Test that we properly handle failures to add a receive stream.
2492 TEST_F(WebRtcVoiceEngineTestFake, AddRecvStreamFail) {
2493 EXPECT_TRUE(SetupEngine());
2494 voe_.set_fail_create_channel(true);
2495 EXPECT_FALSE(channel_->AddRecvStream(cricket::StreamParams::CreateLegacy(2)));
2496 }
2497
2498 // Test that we properly handle failures to add a send stream.
2499 TEST_F(WebRtcVoiceEngineTestFake, AddSendStreamFail) {
2500 EXPECT_TRUE(SetupEngine());
2501 voe_.set_fail_create_channel(true);
2502 EXPECT_FALSE(channel_->AddSendStream(cricket::StreamParams::CreateLegacy(2)));
2503 }
2504
2505 // Test that AddRecvStream creates new stream.
2506 TEST_F(WebRtcVoiceEngineTestFake, AddRecvStream) {
2507 EXPECT_TRUE(SetupEngineWithRecvStream());
2508 int channel_num = voe_.GetLastChannel();
2509 EXPECT_TRUE(channel_->AddRecvStream(cricket::StreamParams::CreateLegacy(1)));
2510 EXPECT_NE(channel_num, voe_.GetLastChannel());
2511 }
2512
2513 // Test that after adding a recv stream, we do not decode more codecs than
2514 // those previously passed into SetRecvCodecs.
2515 TEST_F(WebRtcVoiceEngineTestFake, AddRecvStreamUnsupportedCodec) {
2516 EXPECT_TRUE(SetupEngineWithSendStream());
2517 cricket::AudioRecvParameters parameters;
2518 parameters.codecs.push_back(kIsacCodec);
2519 parameters.codecs.push_back(kPcmuCodec);
2520 EXPECT_TRUE(channel_->SetRecvParameters(parameters));
2521 EXPECT_TRUE(channel_->AddRecvStream(
2522 cricket::StreamParams::CreateLegacy(kSsrc1)));
2523 int channel_num2 = voe_.GetLastChannel();
2524 webrtc::CodecInst gcodec;
2525 rtc::strcpyn(gcodec.plname, arraysize(gcodec.plname), "opus");
2526 gcodec.plfreq = 48000;
2527 gcodec.channels = 2;
2528 EXPECT_EQ(-1, voe_.GetRecPayloadType(channel_num2, gcodec));
2529 }
2530
2531 // Test that we properly clean up any streams that were added, even if
2532 // not explicitly removed.
2533 TEST_F(WebRtcVoiceEngineTestFake, StreamCleanup) {
2534 EXPECT_TRUE(SetupEngineWithSendStream());
2535 EXPECT_TRUE(channel_->SetSendParameters(send_parameters_));
2536 EXPECT_TRUE(channel_->AddRecvStream(cricket::StreamParams::CreateLegacy(1)));
2537 EXPECT_TRUE(channel_->AddRecvStream(cricket::StreamParams::CreateLegacy(2)));
2538 EXPECT_EQ(3, voe_.GetNumChannels()); // default channel + 2 added
2539 delete channel_;
2540 channel_ = NULL;
2541 EXPECT_EQ(0, voe_.GetNumChannels());
2542 }
2543
2544 TEST_F(WebRtcVoiceEngineTestFake, TestAddRecvStreamFailWithZeroSsrc) {
2545 EXPECT_TRUE(SetupEngineWithSendStream());
2546 EXPECT_FALSE(channel_->AddRecvStream(cricket::StreamParams::CreateLegacy(0)));
2547 }
2548
2549 TEST_F(WebRtcVoiceEngineTestFake, TestNoLeakingWhenAddRecvStreamFail) {
2550 EXPECT_TRUE(SetupEngine());
2551 EXPECT_TRUE(channel_->AddRecvStream(cricket::StreamParams::CreateLegacy(1)));
2552 // Manually delete channel to simulate a failure.
2553 int channel = voe_.GetLastChannel();
2554 EXPECT_EQ(0, voe_.DeleteChannel(channel));
2555 // Add recv stream 2 should work.
2556 EXPECT_TRUE(channel_->AddRecvStream(cricket::StreamParams::CreateLegacy(2)));
2557 int new_channel = voe_.GetLastChannel();
2558 EXPECT_NE(channel, new_channel);
2559 // The last created channel is deleted too.
2560 EXPECT_EQ(0, voe_.DeleteChannel(new_channel));
2561 }
2562
2563 // Test the InsertDtmf on default send stream as caller.
2564 TEST_F(WebRtcVoiceEngineTestFake, InsertDtmfOnDefaultSendStreamAsCaller) {
2565 TestInsertDtmf(0, true);
2566 }
2567
2568 // Test the InsertDtmf on default send stream as callee
2569 TEST_F(WebRtcVoiceEngineTestFake, InsertDtmfOnDefaultSendStreamAsCallee) {
2570 TestInsertDtmf(0, false);
2571 }
2572
2573 // Test the InsertDtmf on specified send stream as caller.
2574 TEST_F(WebRtcVoiceEngineTestFake, InsertDtmfOnSendStreamAsCaller) {
2575 TestInsertDtmf(kSsrc1, true);
2576 }
2577
2578 // Test the InsertDtmf on specified send stream as callee.
2579 TEST_F(WebRtcVoiceEngineTestFake, InsertDtmfOnSendStreamAsCallee) {
2580 TestInsertDtmf(kSsrc1, false);
2581 }
2582
2583 TEST_F(WebRtcVoiceEngineTestFake, TestSetPlayoutError) {
2584 EXPECT_TRUE(SetupEngineWithSendStream());
2585 EXPECT_TRUE(channel_->SetSendParameters(send_parameters_));
2586 EXPECT_TRUE(channel_->SetSend(cricket::SEND_MICROPHONE));
2587 EXPECT_TRUE(channel_->AddRecvStream(cricket::StreamParams::CreateLegacy(2)));
2588 EXPECT_TRUE(channel_->AddRecvStream(cricket::StreamParams::CreateLegacy(3)));
2589 EXPECT_TRUE(channel_->SetPlayout(true));
2590 voe_.set_playout_fail_channel(voe_.GetLastChannel() - 1);
2591 EXPECT_TRUE(channel_->SetPlayout(false));
2592 EXPECT_FALSE(channel_->SetPlayout(true));
2593 }
2594
2595 TEST_F(WebRtcVoiceEngineTestFake, SetAudioOptions) {
2596 EXPECT_TRUE(SetupEngineWithSendStream());
2597
2598 bool ec_enabled;
2599 webrtc::EcModes ec_mode;
2600 webrtc::AecmModes aecm_mode;
2601 bool cng_enabled;
2602 bool agc_enabled;
2603 webrtc::AgcModes agc_mode;
2604 webrtc::AgcConfig agc_config;
2605 bool ns_enabled;
2606 webrtc::NsModes ns_mode;
2607 bool highpass_filter_enabled;
2608 bool stereo_swapping_enabled;
2609 bool typing_detection_enabled;
2610 voe_.GetEcStatus(ec_enabled, ec_mode);
2611 voe_.GetAecmMode(aecm_mode, cng_enabled);
2612 voe_.GetAgcStatus(agc_enabled, agc_mode);
2613 voe_.GetAgcConfig(agc_config);
2614 voe_.GetNsStatus(ns_enabled, ns_mode);
2615 highpass_filter_enabled = voe_.IsHighPassFilterEnabled();
2616 stereo_swapping_enabled = voe_.IsStereoChannelSwappingEnabled();
2617 voe_.GetTypingDetectionStatus(typing_detection_enabled);
2618 EXPECT_TRUE(ec_enabled);
2619 EXPECT_TRUE(voe_.ec_metrics_enabled());
2620 EXPECT_FALSE(cng_enabled);
2621 EXPECT_TRUE(agc_enabled);
2622 EXPECT_EQ(0, agc_config.targetLeveldBOv);
2623 EXPECT_TRUE(ns_enabled);
2624 EXPECT_TRUE(highpass_filter_enabled);
2625 EXPECT_FALSE(stereo_swapping_enabled);
2626 EXPECT_TRUE(typing_detection_enabled);
2627 EXPECT_EQ(ec_mode, webrtc::kEcConference);
2628 EXPECT_EQ(ns_mode, webrtc::kNsHighSuppression);
2629 EXPECT_EQ(50, voe_.GetNetEqCapacity());
2630 EXPECT_FALSE(voe_.GetNetEqFastAccelerate());
2631
2632 // Nothing set in AudioOptions, so everything should be as default.
2633 send_parameters_.options = cricket::AudioOptions();
2634 EXPECT_TRUE(channel_->SetSendParameters(send_parameters_));
2635 voe_.GetEcStatus(ec_enabled, ec_mode);
2636 voe_.GetAecmMode(aecm_mode, cng_enabled);
2637 voe_.GetAgcStatus(agc_enabled, agc_mode);
2638 voe_.GetAgcConfig(agc_config);
2639 voe_.GetNsStatus(ns_enabled, ns_mode);
2640 highpass_filter_enabled = voe_.IsHighPassFilterEnabled();
2641 stereo_swapping_enabled = voe_.IsStereoChannelSwappingEnabled();
2642 voe_.GetTypingDetectionStatus(typing_detection_enabled);
2643 EXPECT_TRUE(ec_enabled);
2644 EXPECT_TRUE(voe_.ec_metrics_enabled());
2645 EXPECT_FALSE(cng_enabled);
2646 EXPECT_TRUE(agc_enabled);
2647 EXPECT_EQ(0, agc_config.targetLeveldBOv);
2648 EXPECT_TRUE(ns_enabled);
2649 EXPECT_TRUE(highpass_filter_enabled);
2650 EXPECT_FALSE(stereo_swapping_enabled);
2651 EXPECT_TRUE(typing_detection_enabled);
2652 EXPECT_EQ(ec_mode, webrtc::kEcConference);
2653 EXPECT_EQ(ns_mode, webrtc::kNsHighSuppression);
2654 EXPECT_EQ(50, voe_.GetNetEqCapacity());
2655 EXPECT_FALSE(voe_.GetNetEqFastAccelerate());
2656
2657 // Turn echo cancellation off
2658 send_parameters_.options.echo_cancellation = rtc::Optional<bool>(false);
2659 EXPECT_TRUE(channel_->SetSendParameters(send_parameters_));
2660 voe_.GetEcStatus(ec_enabled, ec_mode);
2661 EXPECT_FALSE(ec_enabled);
2662
2663 // Turn echo cancellation back on, with settings, and make sure
2664 // nothing else changed.
2665 send_parameters_.options.echo_cancellation = rtc::Optional<bool>(true);
2666 EXPECT_TRUE(channel_->SetSendParameters(send_parameters_));
2667 voe_.GetEcStatus(ec_enabled, ec_mode);
2668 voe_.GetAecmMode(aecm_mode, cng_enabled);
2669 voe_.GetAgcStatus(agc_enabled, agc_mode);
2670 voe_.GetAgcConfig(agc_config);
2671 voe_.GetNsStatus(ns_enabled, ns_mode);
2672 highpass_filter_enabled = voe_.IsHighPassFilterEnabled();
2673 stereo_swapping_enabled = voe_.IsStereoChannelSwappingEnabled();
2674 voe_.GetTypingDetectionStatus(typing_detection_enabled);
2675 EXPECT_TRUE(ec_enabled);
2676 EXPECT_TRUE(voe_.ec_metrics_enabled());
2677 EXPECT_TRUE(agc_enabled);
2678 EXPECT_EQ(0, agc_config.targetLeveldBOv);
2679 EXPECT_TRUE(ns_enabled);
2680 EXPECT_TRUE(highpass_filter_enabled);
2681 EXPECT_FALSE(stereo_swapping_enabled);
2682 EXPECT_TRUE(typing_detection_enabled);
2683 EXPECT_EQ(ec_mode, webrtc::kEcConference);
2684 EXPECT_EQ(ns_mode, webrtc::kNsHighSuppression);
2685
2686 // Turn on delay agnostic aec and make sure nothing change w.r.t. echo
2687 // control.
2688 send_parameters_.options.delay_agnostic_aec = rtc::Optional<bool>(true);
2689 EXPECT_TRUE(channel_->SetSendParameters(send_parameters_));
2690 voe_.GetEcStatus(ec_enabled, ec_mode);
2691 voe_.GetAecmMode(aecm_mode, cng_enabled);
2692 EXPECT_TRUE(ec_enabled);
2693 EXPECT_TRUE(voe_.ec_metrics_enabled());
2694 EXPECT_EQ(ec_mode, webrtc::kEcConference);
2695
2696 // Turn off echo cancellation and delay agnostic aec.
2697 send_parameters_.options.delay_agnostic_aec = rtc::Optional<bool>(false);
2698 send_parameters_.options.extended_filter_aec = rtc::Optional<bool>(false);
2699 send_parameters_.options.echo_cancellation = rtc::Optional<bool>(false);
2700 EXPECT_TRUE(channel_->SetSendParameters(send_parameters_));
2701 voe_.GetEcStatus(ec_enabled, ec_mode);
2702 EXPECT_FALSE(ec_enabled);
2703 // Turning delay agnostic aec back on should also turn on echo cancellation.
2704 send_parameters_.options.delay_agnostic_aec = rtc::Optional<bool>(true);
2705 EXPECT_TRUE(channel_->SetSendParameters(send_parameters_));
2706 voe_.GetEcStatus(ec_enabled, ec_mode);
2707 EXPECT_TRUE(ec_enabled);
2708 EXPECT_TRUE(voe_.ec_metrics_enabled());
2709 EXPECT_EQ(ec_mode, webrtc::kEcConference);
2710
2711 // Turn off AGC
2712 send_parameters_.options.auto_gain_control = rtc::Optional<bool>(false);
2713 EXPECT_TRUE(channel_->SetSendParameters(send_parameters_));
2714 voe_.GetAgcStatus(agc_enabled, agc_mode);
2715 EXPECT_FALSE(agc_enabled);
2716
2717 // Turn AGC back on
2718 send_parameters_.options.auto_gain_control = rtc::Optional<bool>(true);
2719 send_parameters_.options.adjust_agc_delta = rtc::Optional<int>();
2720 EXPECT_TRUE(channel_->SetSendParameters(send_parameters_));
2721 voe_.GetAgcStatus(agc_enabled, agc_mode);
2722 EXPECT_TRUE(agc_enabled);
2723 voe_.GetAgcConfig(agc_config);
2724 EXPECT_EQ(0, agc_config.targetLeveldBOv);
2725
2726 // Turn off other options (and stereo swapping on).
2727 send_parameters_.options.noise_suppression = rtc::Optional<bool>(false);
2728 send_parameters_.options.highpass_filter = rtc::Optional<bool>(false);
2729 send_parameters_.options.typing_detection = rtc::Optional<bool>(false);
2730 send_parameters_.options.stereo_swapping = rtc::Optional<bool>(true);
2731 EXPECT_TRUE(channel_->SetSendParameters(send_parameters_));
2732 voe_.GetNsStatus(ns_enabled, ns_mode);
2733 highpass_filter_enabled = voe_.IsHighPassFilterEnabled();
2734 stereo_swapping_enabled = voe_.IsStereoChannelSwappingEnabled();
2735 voe_.GetTypingDetectionStatus(typing_detection_enabled);
2736 EXPECT_FALSE(ns_enabled);
2737 EXPECT_FALSE(highpass_filter_enabled);
2738 EXPECT_FALSE(typing_detection_enabled);
2739 EXPECT_TRUE(stereo_swapping_enabled);
2740
2741 // Set options again to ensure it has no impact.
2742 EXPECT_TRUE(channel_->SetSendParameters(send_parameters_));
2743 voe_.GetEcStatus(ec_enabled, ec_mode);
2744 voe_.GetNsStatus(ns_enabled, ns_mode);
2745 EXPECT_TRUE(ec_enabled);
2746 EXPECT_EQ(webrtc::kEcConference, ec_mode);
2747 EXPECT_FALSE(ns_enabled);
2748 EXPECT_EQ(webrtc::kNsHighSuppression, ns_mode);
2749 }
2750
2751 TEST_F(WebRtcVoiceEngineTestFake, DefaultOptions) {
2752 EXPECT_TRUE(SetupEngineWithSendStream());
2753
2754 bool ec_enabled;
2755 webrtc::EcModes ec_mode;
2756 bool agc_enabled;
2757 webrtc::AgcModes agc_mode;
2758 bool ns_enabled;
2759 webrtc::NsModes ns_mode;
2760 bool highpass_filter_enabled;
2761 bool stereo_swapping_enabled;
2762 bool typing_detection_enabled;
2763
2764 voe_.GetEcStatus(ec_enabled, ec_mode);
2765 voe_.GetAgcStatus(agc_enabled, agc_mode);
2766 voe_.GetNsStatus(ns_enabled, ns_mode);
2767 highpass_filter_enabled = voe_.IsHighPassFilterEnabled();
2768 stereo_swapping_enabled = voe_.IsStereoChannelSwappingEnabled();
2769 voe_.GetTypingDetectionStatus(typing_detection_enabled);
2770 EXPECT_TRUE(ec_enabled);
2771 EXPECT_TRUE(agc_enabled);
2772 EXPECT_TRUE(ns_enabled);
2773 EXPECT_TRUE(highpass_filter_enabled);
2774 EXPECT_TRUE(typing_detection_enabled);
2775 EXPECT_FALSE(stereo_swapping_enabled);
2776 }
2777
2778 TEST_F(WebRtcVoiceEngineTestFake, InitDoesNotOverwriteDefaultAgcConfig) {
2779 webrtc::AgcConfig set_config = {0};
2780 set_config.targetLeveldBOv = 3;
2781 set_config.digitalCompressionGaindB = 9;
2782 set_config.limiterEnable = true;
2783 EXPECT_EQ(0, voe_.SetAgcConfig(set_config));
2784 EXPECT_TRUE(engine_.Init(rtc::Thread::Current()));
2785
2786 webrtc::AgcConfig config = {0};
2787 EXPECT_EQ(0, voe_.GetAgcConfig(config));
2788 EXPECT_EQ(set_config.targetLeveldBOv, config.targetLeveldBOv);
2789 EXPECT_EQ(set_config.digitalCompressionGaindB,
2790 config.digitalCompressionGaindB);
2791 EXPECT_EQ(set_config.limiterEnable, config.limiterEnable);
2792 }
2793
2794 TEST_F(WebRtcVoiceEngineTestFake, SetOptionOverridesViaChannels) {
2795 EXPECT_TRUE(SetupEngineWithSendStream());
2796 rtc::scoped_ptr<cricket::WebRtcVoiceMediaChannel> channel1(
2797 static_cast<cricket::WebRtcVoiceMediaChannel*>(
2798 engine_.CreateChannel(&call_, cricket::AudioOptions())));
2799 rtc::scoped_ptr<cricket::WebRtcVoiceMediaChannel> channel2(
2800 static_cast<cricket::WebRtcVoiceMediaChannel*>(
2801 engine_.CreateChannel(&call_, cricket::AudioOptions())));
2802
2803 // Have to add a stream to make SetSend work.
2804 cricket::StreamParams stream1;
2805 stream1.ssrcs.push_back(1);
2806 channel1->AddSendStream(stream1);
2807 cricket::StreamParams stream2;
2808 stream2.ssrcs.push_back(2);
2809 channel2->AddSendStream(stream2);
2810
2811 // AEC and AGC and NS
2812 cricket::AudioSendParameters parameters_options_all = send_parameters_;
2813 parameters_options_all.options.echo_cancellation = rtc::Optional<bool>(true);
2814 parameters_options_all.options.auto_gain_control = rtc::Optional<bool>(true);
2815 parameters_options_all.options.noise_suppression = rtc::Optional<bool>(true);
2816 ASSERT_TRUE(channel1->SetSendParameters(parameters_options_all));
2817 EXPECT_EQ(parameters_options_all.options, channel1->options());
2818 ASSERT_TRUE(channel2->SetSendParameters(parameters_options_all));
2819 EXPECT_EQ(parameters_options_all.options, channel2->options());
2820
2821 // unset NS
2822 cricket::AudioSendParameters parameters_options_no_ns = send_parameters_;
2823 parameters_options_no_ns.options.noise_suppression =
2824 rtc::Optional<bool>(false);
2825 ASSERT_TRUE(channel1->SetSendParameters(parameters_options_no_ns));
2826 cricket::AudioOptions expected_options = parameters_options_all.options;
2827 expected_options.echo_cancellation = rtc::Optional<bool>(true);
2828 expected_options.auto_gain_control = rtc::Optional<bool>(true);
2829 expected_options.noise_suppression = rtc::Optional<bool>(false);
2830 EXPECT_EQ(expected_options, channel1->options());
2831
2832 // unset AGC
2833 cricket::AudioSendParameters parameters_options_no_agc = send_parameters_;
2834 parameters_options_no_agc.options.auto_gain_control =
2835 rtc::Optional<bool>(false);
2836 ASSERT_TRUE(channel2->SetSendParameters(parameters_options_no_agc));
2837 expected_options.echo_cancellation = rtc::Optional<bool>(true);
2838 expected_options.auto_gain_control = rtc::Optional<bool>(false);
2839 expected_options.noise_suppression = rtc::Optional<bool>(true);
2840 EXPECT_EQ(expected_options, channel2->options());
2841
2842 ASSERT_TRUE(channel_->SetSendParameters(parameters_options_all));
2843 bool ec_enabled;
2844 webrtc::EcModes ec_mode;
2845 bool agc_enabled;
2846 webrtc::AgcModes agc_mode;
2847 bool ns_enabled;
2848 webrtc::NsModes ns_mode;
2849 voe_.GetEcStatus(ec_enabled, ec_mode);
2850 voe_.GetAgcStatus(agc_enabled, agc_mode);
2851 voe_.GetNsStatus(ns_enabled, ns_mode);
2852 EXPECT_TRUE(ec_enabled);
2853 EXPECT_TRUE(agc_enabled);
2854 EXPECT_TRUE(ns_enabled);
2855
2856 channel1->SetSend(cricket::SEND_MICROPHONE);
2857 voe_.GetEcStatus(ec_enabled, ec_mode);
2858 voe_.GetAgcStatus(agc_enabled, agc_mode);
2859 voe_.GetNsStatus(ns_enabled, ns_mode);
2860 EXPECT_TRUE(ec_enabled);
2861 EXPECT_TRUE(agc_enabled);
2862 EXPECT_FALSE(ns_enabled);
2863
2864 channel2->SetSend(cricket::SEND_MICROPHONE);
2865 voe_.GetEcStatus(ec_enabled, ec_mode);
2866 voe_.GetAgcStatus(agc_enabled, agc_mode);
2867 voe_.GetNsStatus(ns_enabled, ns_mode);
2868 EXPECT_TRUE(ec_enabled);
2869 EXPECT_FALSE(agc_enabled);
2870 EXPECT_TRUE(ns_enabled);
2871
2872 // Make sure settings take effect while we are sending.
2873 ASSERT_TRUE(channel_->SetSendParameters(parameters_options_all));
2874 cricket::AudioSendParameters parameters_options_no_agc_nor_ns =
2875 send_parameters_;
2876 parameters_options_no_agc_nor_ns.options.auto_gain_control =
2877 rtc::Optional<bool>(false);
2878 parameters_options_no_agc_nor_ns.options.noise_suppression =
2879 rtc::Optional<bool>(false);
2880 channel2->SetSend(cricket::SEND_MICROPHONE);
2881 channel2->SetSendParameters(parameters_options_no_agc_nor_ns);
2882 expected_options.echo_cancellation = rtc::Optional<bool>(true);
2883 expected_options.auto_gain_control = rtc::Optional<bool>(false);
2884 expected_options.noise_suppression = rtc::Optional<bool>(false);
2885 EXPECT_EQ(expected_options, channel2->options());
2886 voe_.GetEcStatus(ec_enabled, ec_mode);
2887 voe_.GetAgcStatus(agc_enabled, agc_mode);
2888 voe_.GetNsStatus(ns_enabled, ns_mode);
2889 EXPECT_TRUE(ec_enabled);
2890 EXPECT_FALSE(agc_enabled);
2891 EXPECT_FALSE(ns_enabled);
2892 }
2893
2894 // This test verifies DSCP settings are properly applied on voice media channel.
2895 TEST_F(WebRtcVoiceEngineTestFake, TestSetDscpOptions) {
2896 EXPECT_TRUE(SetupEngineWithSendStream());
2897 rtc::scoped_ptr<cricket::VoiceMediaChannel> channel(
2898 engine_.CreateChannel(&call_, cricket::AudioOptions()));
2899 rtc::scoped_ptr<cricket::FakeNetworkInterface> network_interface(
2900 new cricket::FakeNetworkInterface);
2901 channel->SetInterface(network_interface.get());
2902 cricket::AudioSendParameters parameters = send_parameters_;
2903 parameters.options.dscp = rtc::Optional<bool>(true);
2904 EXPECT_TRUE(channel->SetSendParameters(parameters));
2905 EXPECT_EQ(rtc::DSCP_EF, network_interface->dscp());
2906 // Verify previous value is not modified if dscp option is not set.
2907 EXPECT_TRUE(channel->SetSendParameters(send_parameters_));
2908 EXPECT_EQ(rtc::DSCP_EF, network_interface->dscp());
2909 parameters.options.dscp = rtc::Optional<bool>(false);
2910 EXPECT_TRUE(channel->SetSendParameters(parameters));
2911 EXPECT_EQ(rtc::DSCP_DEFAULT, network_interface->dscp());
2912 }
2913
2914 TEST_F(WebRtcVoiceEngineTestFake, TestGetReceiveChannelId) {
2915 EXPECT_TRUE(SetupEngine());
2916 cricket::WebRtcVoiceMediaChannel* media_channel =
2917 static_cast<cricket::WebRtcVoiceMediaChannel*>(channel_);
2918 EXPECT_EQ(-1, media_channel->GetReceiveChannelId(0));
2919 EXPECT_TRUE(channel_->AddRecvStream(
2920 cricket::StreamParams::CreateLegacy(kSsrc1)));
2921 int channel_id = voe_.GetLastChannel();
2922 EXPECT_EQ(channel_id, media_channel->GetReceiveChannelId(kSsrc1));
2923 EXPECT_EQ(-1, media_channel->GetReceiveChannelId(kSsrc2));
2924 EXPECT_TRUE(channel_->AddRecvStream(
2925 cricket::StreamParams::CreateLegacy(kSsrc2)));
2926 int channel_id2 = voe_.GetLastChannel();
2927 EXPECT_EQ(channel_id2, media_channel->GetReceiveChannelId(kSsrc2));
2928 }
2929
2930 TEST_F(WebRtcVoiceEngineTestFake, TestGetSendChannelId) {
2931 EXPECT_TRUE(SetupEngine());
2932 cricket::WebRtcVoiceMediaChannel* media_channel =
2933 static_cast<cricket::WebRtcVoiceMediaChannel*>(channel_);
2934 EXPECT_EQ(-1, media_channel->GetSendChannelId(0));
2935 EXPECT_TRUE(channel_->AddSendStream(
2936 cricket::StreamParams::CreateLegacy(kSsrc1)));
2937 int channel_id = voe_.GetLastChannel();
2938 EXPECT_EQ(channel_id, media_channel->GetSendChannelId(kSsrc1));
2939 EXPECT_EQ(-1, media_channel->GetSendChannelId(kSsrc2));
2940 EXPECT_TRUE(channel_->AddSendStream(
2941 cricket::StreamParams::CreateLegacy(kSsrc2)));
2942 int channel_id2 = voe_.GetLastChannel();
2943 EXPECT_EQ(channel_id2, media_channel->GetSendChannelId(kSsrc2));
2944 }
2945
2946 TEST_F(WebRtcVoiceEngineTestFake, SetOutputVolume) {
2947 EXPECT_TRUE(SetupEngine());
2948 EXPECT_FALSE(channel_->SetOutputVolume(kSsrc2, 0.5));
2949 cricket::StreamParams stream;
2950 stream.ssrcs.push_back(kSsrc2);
2951 EXPECT_TRUE(channel_->AddRecvStream(stream));
2952 int channel_id = voe_.GetLastChannel();
2953 EXPECT_TRUE(channel_->SetOutputVolume(kSsrc2, 3));
2954 float scale = 0;
2955 EXPECT_EQ(0, voe_.GetChannelOutputVolumeScaling(channel_id, scale));
2956 EXPECT_DOUBLE_EQ(3, scale);
2957 }
2958
2959 TEST_F(WebRtcVoiceEngineTestFake, SetOutputVolumeDefaultRecvStream) {
2960 EXPECT_TRUE(SetupEngine());
2961 EXPECT_TRUE(channel_->SetOutputVolume(0, 2));
2962 DeliverPacket(kPcmuFrame, sizeof(kPcmuFrame));
2963 int channel_id = voe_.GetLastChannel();
2964 float scale = 0;
2965 EXPECT_EQ(0, voe_.GetChannelOutputVolumeScaling(channel_id, scale));
2966 EXPECT_DOUBLE_EQ(2, scale);
2967 EXPECT_TRUE(channel_->SetOutputVolume(0, 3));
2968 EXPECT_EQ(0, voe_.GetChannelOutputVolumeScaling(channel_id, scale));
2969 EXPECT_DOUBLE_EQ(3, scale);
2970 }
2971
2972 TEST_F(WebRtcVoiceEngineTestFake, SetsSyncGroupFromSyncLabel) {
2973 const uint32_t kAudioSsrc = 123;
2974 const std::string kSyncLabel = "AvSyncLabel";
2975
2976 EXPECT_TRUE(SetupEngineWithSendStream());
2977 cricket::StreamParams sp = cricket::StreamParams::CreateLegacy(kAudioSsrc);
2978 sp.sync_label = kSyncLabel;
2979 // Creating two channels to make sure that sync label is set properly for both
2980 // the default voice channel and following ones.
2981 EXPECT_TRUE(channel_->AddRecvStream(sp));
2982 sp.ssrcs[0] += 1;
2983 EXPECT_TRUE(channel_->AddRecvStream(sp));
2984
2985 ASSERT_EQ(2, call_.GetAudioReceiveStreams().size());
2986 EXPECT_EQ(kSyncLabel,
2987 call_.GetAudioReceiveStream(kAudioSsrc)->GetConfig().sync_group)
2988 << "SyncGroup should be set based on sync_label";
2989 EXPECT_EQ(kSyncLabel,
2990 call_.GetAudioReceiveStream(kAudioSsrc + 1)->GetConfig().sync_group)
2991 << "SyncGroup should be set based on sync_label";
2992 }
2993
2994 // TODO(solenberg): Remove, once recv streams are configured through Call.
2995 // (This is then covered by TestSetRecvRtpHeaderExtensions.)
2996 TEST_F(WebRtcVoiceEngineTestFake, ConfiguresAudioReceiveStreamRtpExtensions) {
2997 // Test that setting the header extensions results in the expected state
2998 // changes on an associated Call.
2999 std::vector<uint32_t> ssrcs;
3000 ssrcs.push_back(223);
3001 ssrcs.push_back(224);
3002
3003 EXPECT_TRUE(SetupEngineWithSendStream());
3004 cricket::WebRtcVoiceMediaChannel* media_channel =
3005 static_cast<cricket::WebRtcVoiceMediaChannel*>(channel_);
3006 EXPECT_TRUE(media_channel->SetSendParameters(send_parameters_));
3007 for (uint32_t ssrc : ssrcs) {
3008 EXPECT_TRUE(media_channel->AddRecvStream(
3009 cricket::StreamParams::CreateLegacy(ssrc)));
3010 }
3011
3012 EXPECT_EQ(2, call_.GetAudioReceiveStreams().size());
3013 for (uint32_t ssrc : ssrcs) {
3014 const auto* s = call_.GetAudioReceiveStream(ssrc);
3015 EXPECT_NE(nullptr, s);
3016 EXPECT_EQ(0, s->GetConfig().rtp.extensions.size());
3017 }
3018
3019 // Set up receive extensions.
3020 cricket::RtpCapabilities capabilities = engine_.GetCapabilities();
3021 cricket::AudioRecvParameters recv_parameters;
3022 recv_parameters.extensions = capabilities.header_extensions;
3023 channel_->SetRecvParameters(recv_parameters);
3024 EXPECT_EQ(2, call_.GetAudioReceiveStreams().size());
3025 for (uint32_t ssrc : ssrcs) {
3026 const auto* s = call_.GetAudioReceiveStream(ssrc);
3027 EXPECT_NE(nullptr, s);
3028 const auto& s_exts = s->GetConfig().rtp.extensions;
3029 EXPECT_EQ(capabilities.header_extensions.size(), s_exts.size());
3030 for (const auto& e_ext : capabilities.header_extensions) {
3031 for (const auto& s_ext : s_exts) {
3032 if (e_ext.id == s_ext.id) {
3033 EXPECT_EQ(e_ext.uri, s_ext.name);
3034 }
3035 }
3036 }
3037 }
3038
3039 // Disable receive extensions.
3040 channel_->SetRecvParameters(cricket::AudioRecvParameters());
3041 for (uint32_t ssrc : ssrcs) {
3042 const auto* s = call_.GetAudioReceiveStream(ssrc);
3043 EXPECT_NE(nullptr, s);
3044 EXPECT_EQ(0, s->GetConfig().rtp.extensions.size());
3045 }
3046 }
3047
3048 TEST_F(WebRtcVoiceEngineTestFake, DeliverAudioPacket_Call) {
3049 // Test that packets are forwarded to the Call when configured accordingly.
3050 const uint32_t kAudioSsrc = 1;
3051 rtc::Buffer kPcmuPacket(kPcmuFrame, sizeof(kPcmuFrame));
3052 static const unsigned char kRtcp[] = {
3053 0x80, 0xc9, 0x00, 0x01, 0x00, 0x00, 0x00, 0x02,
3054 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, 0x00,
3055 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
3056 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00
3057 };
3058 rtc::Buffer kRtcpPacket(kRtcp, sizeof(kRtcp));
3059
3060 EXPECT_TRUE(SetupEngineWithSendStream());
3061 cricket::WebRtcVoiceMediaChannel* media_channel =
3062 static_cast<cricket::WebRtcVoiceMediaChannel*>(channel_);
3063 EXPECT_TRUE(channel_->SetSendParameters(send_parameters_));
3064 EXPECT_TRUE(media_channel->AddRecvStream(
3065 cricket::StreamParams::CreateLegacy(kAudioSsrc)));
3066
3067 EXPECT_EQ(1, call_.GetAudioReceiveStreams().size());
3068 const cricket::FakeAudioReceiveStream* s =
3069 call_.GetAudioReceiveStream(kAudioSsrc);
3070 EXPECT_EQ(0, s->received_packets());
3071 channel_->OnPacketReceived(&kPcmuPacket, rtc::PacketTime());
3072 EXPECT_EQ(1, s->received_packets());
3073 channel_->OnRtcpReceived(&kRtcpPacket, rtc::PacketTime());
3074 EXPECT_EQ(2, s->received_packets());
3075 }
3076
3077 // All receive channels should be associated with the first send channel,
3078 // since they do not send RTCP SR.
3079 TEST_F(WebRtcVoiceEngineTestFake, AssociateFirstSendChannel) {
3080 EXPECT_TRUE(SetupEngineWithSendStream());
3081 EXPECT_TRUE(channel_->SetSendParameters(send_parameters_));
3082 int default_channel = voe_.GetLastChannel();
3083 EXPECT_TRUE(channel_->AddRecvStream(cricket::StreamParams::CreateLegacy(1)));
3084 int recv_ch = voe_.GetLastChannel();
3085 EXPECT_NE(recv_ch, default_channel);
3086 EXPECT_EQ(voe_.GetAssociateSendChannel(recv_ch), default_channel);
3087 EXPECT_TRUE(channel_->AddSendStream(cricket::StreamParams::CreateLegacy(2)));
3088 EXPECT_EQ(voe_.GetAssociateSendChannel(recv_ch), default_channel);
3089 EXPECT_TRUE(channel_->AddRecvStream(cricket::StreamParams::CreateLegacy(3)));
3090 recv_ch = voe_.GetLastChannel();
3091 EXPECT_NE(recv_ch, default_channel);
3092 EXPECT_EQ(voe_.GetAssociateSendChannel(recv_ch), default_channel);
3093 }
3094
3095 TEST_F(WebRtcVoiceEngineTestFake, AssociateChannelResetUponDeleteChannnel) {
3096 EXPECT_TRUE(SetupEngineWithSendStream());
3097 EXPECT_TRUE(channel_->SetSendParameters(send_parameters_));
3098
3099 EXPECT_TRUE(channel_->AddRecvStream(cricket::StreamParams::CreateLegacy(1)));
3100 int recv_ch = voe_.GetLastChannel();
3101
3102 EXPECT_TRUE(channel_->AddSendStream(cricket::StreamParams::CreateLegacy(2)));
3103 int send_ch = voe_.GetLastChannel();
3104
3105 // Manually associate |recv_ch| to |send_ch|. This test is to verify a
3106 // deleting logic, i.e., deleting |send_ch| will reset the associate send
3107 // channel of |recv_ch|.This is not a common case, sinceļ¼Œ normally, only the
3108 // default channel can be associated. However, the default is not deletable.
3109 // So we force the |recv_ch| to associate with a non-default channel.
3110 EXPECT_EQ(0, voe_.AssociateSendChannel(recv_ch, send_ch));
3111 EXPECT_EQ(voe_.GetAssociateSendChannel(recv_ch), send_ch);
3112
3113 EXPECT_TRUE(channel_->RemoveSendStream(2));
3114 EXPECT_EQ(voe_.GetAssociateSendChannel(recv_ch), -1);
3115 }
3116
3117 TEST_F(WebRtcVoiceEngineTestFake, SetRawAudioSink) {
3118 EXPECT_TRUE(SetupEngine());
3119 rtc::scoped_ptr<FakeAudioSink> fake_sink_1(new FakeAudioSink());
3120 rtc::scoped_ptr<FakeAudioSink> fake_sink_2(new FakeAudioSink());
3121
3122 // Setting the sink before a recv stream exists should do nothing.
3123 channel_->SetRawAudioSink(kSsrc1, std::move(fake_sink_1));
3124 EXPECT_TRUE(
3125 channel_->AddRecvStream(cricket::StreamParams::CreateLegacy(kSsrc1)));
3126 EXPECT_EQ(nullptr, GetRecvStream(kSsrc1).sink());
3127
3128 // Now try actually setting the sink.
3129 channel_->SetRawAudioSink(kSsrc1, std::move(fake_sink_2));
3130 EXPECT_NE(nullptr, GetRecvStream(kSsrc1).sink());
3131
3132 // Now try resetting it.
3133 channel_->SetRawAudioSink(kSsrc1, nullptr);
3134 EXPECT_EQ(nullptr, GetRecvStream(kSsrc1).sink());
3135 }
3136
3137 TEST_F(WebRtcVoiceEngineTestFake, SetRawAudioSinkDefaultRecvStream) {
3138 EXPECT_TRUE(SetupEngine());
3139 rtc::scoped_ptr<FakeAudioSink> fake_sink_1(new FakeAudioSink());
3140 rtc::scoped_ptr<FakeAudioSink> fake_sink_2(new FakeAudioSink());
3141
3142 // Should be able to set a default sink even when no stream exists.
3143 channel_->SetRawAudioSink(0, std::move(fake_sink_1));
3144
3145 // Create default channel and ensure it's assigned the default sink.
3146 DeliverPacket(kPcmuFrame, sizeof(kPcmuFrame));
3147 EXPECT_NE(nullptr, GetRecvStream(0x01).sink());
3148
3149 // Try resetting the default sink.
3150 channel_->SetRawAudioSink(0, nullptr);
3151 EXPECT_EQ(nullptr, GetRecvStream(0x01).sink());
3152
3153 // Try setting the default sink while the default stream exists.
3154 channel_->SetRawAudioSink(0, std::move(fake_sink_2));
3155 EXPECT_NE(nullptr, GetRecvStream(0x01).sink());
3156
3157 // If we remove and add a default stream, it should get the same sink.
3158 EXPECT_TRUE(channel_->RemoveRecvStream(0x01));
3159 DeliverPacket(kPcmuFrame, sizeof(kPcmuFrame));
3160 EXPECT_NE(nullptr, GetRecvStream(0x01).sink());
3161 }
3162
3163 // Tests that the library initializes and shuts down properly.
3164 TEST(WebRtcVoiceEngineTest, StartupShutdown) {
3165 cricket::WebRtcVoiceEngine engine;
3166 EXPECT_TRUE(engine.Init(rtc::Thread::Current()));
3167 rtc::scoped_ptr<webrtc::Call> call(
3168 webrtc::Call::Create(webrtc::Call::Config()));
3169 cricket::VoiceMediaChannel* channel =
3170 engine.CreateChannel(call.get(), cricket::AudioOptions());
3171 EXPECT_TRUE(channel != nullptr);
3172 delete channel;
3173 engine.Terminate();
3174
3175 // Reinit to catch regression where VoiceEngineObserver reference is lost
3176 EXPECT_TRUE(engine.Init(rtc::Thread::Current()));
3177 engine.Terminate();
3178 }
3179
3180 // Tests that the library is configured with the codecs we want.
3181 TEST(WebRtcVoiceEngineTest, HasCorrectCodecs) {
3182 // Check codecs by name.
3183 EXPECT_TRUE(cricket::WebRtcVoiceEngine::ToCodecInst(
3184 cricket::AudioCodec(96, "OPUS", 48000, 0, 2, 0), nullptr));
3185 EXPECT_TRUE(cricket::WebRtcVoiceEngine::ToCodecInst(
3186 cricket::AudioCodec(96, "ISAC", 16000, 0, 1, 0), nullptr));
3187 EXPECT_TRUE(cricket::WebRtcVoiceEngine::ToCodecInst(
3188 cricket::AudioCodec(96, "ISAC", 32000, 0, 1, 0), nullptr));
3189 // Check that name matching is case-insensitive.
3190 EXPECT_TRUE(cricket::WebRtcVoiceEngine::ToCodecInst(
3191 cricket::AudioCodec(96, "ILBC", 8000, 0, 1, 0), nullptr));
3192 EXPECT_TRUE(cricket::WebRtcVoiceEngine::ToCodecInst(
3193 cricket::AudioCodec(96, "iLBC", 8000, 0, 1, 0), nullptr));
3194 EXPECT_TRUE(cricket::WebRtcVoiceEngine::ToCodecInst(
3195 cricket::AudioCodec(96, "PCMU", 8000, 0, 1, 0), nullptr));
3196 EXPECT_TRUE(cricket::WebRtcVoiceEngine::ToCodecInst(
3197 cricket::AudioCodec(96, "PCMA", 8000, 0, 1, 0), nullptr));
3198 EXPECT_TRUE(cricket::WebRtcVoiceEngine::ToCodecInst(
3199 cricket::AudioCodec(96, "G722", 8000, 0, 1, 0), nullptr));
3200 EXPECT_TRUE(cricket::WebRtcVoiceEngine::ToCodecInst(
3201 cricket::AudioCodec(96, "red", 8000, 0, 1, 0), nullptr));
3202 EXPECT_TRUE(cricket::WebRtcVoiceEngine::ToCodecInst(
3203 cricket::AudioCodec(96, "CN", 32000, 0, 1, 0), nullptr));
3204 EXPECT_TRUE(cricket::WebRtcVoiceEngine::ToCodecInst(
3205 cricket::AudioCodec(96, "CN", 16000, 0, 1, 0), nullptr));
3206 EXPECT_TRUE(cricket::WebRtcVoiceEngine::ToCodecInst(
3207 cricket::AudioCodec(96, "CN", 8000, 0, 1, 0), nullptr));
3208 EXPECT_TRUE(cricket::WebRtcVoiceEngine::ToCodecInst(
3209 cricket::AudioCodec(96, "telephone-event", 8000, 0, 1, 0), nullptr));
3210 // Check codecs with an id by id.
3211 EXPECT_TRUE(cricket::WebRtcVoiceEngine::ToCodecInst(
3212 cricket::AudioCodec(0, "", 8000, 0, 1, 0), nullptr)); // PCMU
3213 EXPECT_TRUE(cricket::WebRtcVoiceEngine::ToCodecInst(
3214 cricket::AudioCodec(8, "", 8000, 0, 1, 0), nullptr)); // PCMA
3215 EXPECT_TRUE(cricket::WebRtcVoiceEngine::ToCodecInst(
3216 cricket::AudioCodec(9, "", 8000, 0, 1, 0), nullptr)); // G722
3217 EXPECT_TRUE(cricket::WebRtcVoiceEngine::ToCodecInst(
3218 cricket::AudioCodec(13, "", 8000, 0, 1, 0), nullptr)); // CN
3219 // Check sample/bitrate matching.
3220 EXPECT_TRUE(cricket::WebRtcVoiceEngine::ToCodecInst(
3221 cricket::AudioCodec(0, "PCMU", 8000, 64000, 1, 0), nullptr));
3222 // Check that bad codecs fail.
3223 EXPECT_FALSE(cricket::WebRtcVoiceEngine::ToCodecInst(
3224 cricket::AudioCodec(99, "ABCD", 0, 0, 1, 0), nullptr));
3225 EXPECT_FALSE(cricket::WebRtcVoiceEngine::ToCodecInst(
3226 cricket::AudioCodec(88, "", 0, 0, 1, 0), nullptr));
3227 EXPECT_FALSE(cricket::WebRtcVoiceEngine::ToCodecInst(
3228 cricket::AudioCodec(0, "", 0, 0, 2, 0), nullptr));
3229 EXPECT_FALSE(cricket::WebRtcVoiceEngine::ToCodecInst(
3230 cricket::AudioCodec(0, "", 5000, 0, 1, 0), nullptr));
3231 EXPECT_FALSE(cricket::WebRtcVoiceEngine::ToCodecInst(
3232 cricket::AudioCodec(0, "", 0, 5000, 1, 0), nullptr));
3233
3234 // Verify the payload id of common audio codecs, including CN, ISAC, and G722.
3235 cricket::WebRtcVoiceEngine engine;
3236 for (std::vector<cricket::AudioCodec>::const_iterator it =
3237 engine.codecs().begin(); it != engine.codecs().end(); ++it) {
3238 if (it->name == "CN" && it->clockrate == 16000) {
3239 EXPECT_EQ(105, it->id);
3240 } else if (it->name == "CN" && it->clockrate == 32000) {
3241 EXPECT_EQ(106, it->id);
3242 } else if (it->name == "ISAC" && it->clockrate == 16000) {
3243 EXPECT_EQ(103, it->id);
3244 } else if (it->name == "ISAC" && it->clockrate == 32000) {
3245 EXPECT_EQ(104, it->id);
3246 } else if (it->name == "G722" && it->clockrate == 8000) {
3247 EXPECT_EQ(9, it->id);
3248 } else if (it->name == "telephone-event") {
3249 EXPECT_EQ(126, it->id);
3250 } else if (it->name == "red") {
3251 EXPECT_EQ(127, it->id);
3252 } else if (it->name == "opus") {
3253 EXPECT_EQ(111, it->id);
3254 ASSERT_TRUE(it->params.find("minptime") != it->params.end());
3255 EXPECT_EQ("10", it->params.find("minptime")->second);
3256 ASSERT_TRUE(it->params.find("maxptime") != it->params.end());
3257 EXPECT_EQ("60", it->params.find("maxptime")->second);
3258 ASSERT_TRUE(it->params.find("useinbandfec") != it->params.end());
3259 EXPECT_EQ("1", it->params.find("useinbandfec")->second);
3260 }
3261 }
3262 engine.Terminate();
3263 }
3264
3265 // Tests that VoE supports at least 32 channels
3266 TEST(WebRtcVoiceEngineTest, Has32Channels) {
3267 cricket::WebRtcVoiceEngine engine;
3268 EXPECT_TRUE(engine.Init(rtc::Thread::Current()));
3269 rtc::scoped_ptr<webrtc::Call> call(
3270 webrtc::Call::Create(webrtc::Call::Config()));
3271
3272 cricket::VoiceMediaChannel* channels[32];
3273 int num_channels = 0;
3274 while (num_channels < arraysize(channels)) {
3275 cricket::VoiceMediaChannel* channel =
3276 engine.CreateChannel(call.get(), cricket::AudioOptions());
3277 if (!channel)
3278 break;
3279 channels[num_channels++] = channel;
3280 }
3281
3282 int expected = arraysize(channels);
3283 EXPECT_EQ(expected, num_channels);
3284
3285 while (num_channels > 0) {
3286 delete channels[--num_channels];
3287 }
3288 engine.Terminate();
3289 }
3290
3291 // Test that we set our preferred codecs properly.
3292 TEST(WebRtcVoiceEngineTest, SetRecvCodecs) {
3293 cricket::WebRtcVoiceEngine engine;
3294 EXPECT_TRUE(engine.Init(rtc::Thread::Current()));
3295 rtc::scoped_ptr<webrtc::Call> call(
3296 webrtc::Call::Create(webrtc::Call::Config()));
3297 cricket::WebRtcVoiceMediaChannel channel(&engine, cricket::AudioOptions(),
3298 call.get());
3299 cricket::AudioRecvParameters parameters;
3300 parameters.codecs = engine.codecs();
3301 EXPECT_TRUE(channel.SetRecvParameters(parameters));
3302 }
OLDNEW
« no previous file with comments | « talk/media/webrtc/webrtcvoiceengine.cc ('k') | talk/session/media/audiomonitor.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698