OLD | NEW |
---|---|
1 /* | 1 /* |
2 * Copyright (c) 2015 The WebRTC project authors. All Rights Reserved. | 2 * Copyright (c) 2015 The WebRTC project authors. All Rights Reserved. |
3 * | 3 * |
4 * Use of this source code is governed by a BSD-style license | 4 * Use of this source code is governed by a BSD-style license |
5 * that can be found in the LICENSE file in the root of the source | 5 * that can be found in the LICENSE file in the root of the source |
6 * tree. An additional intellectual property rights grant can be found | 6 * tree. An additional intellectual property rights grant can be found |
7 * in the file PATENTS. All contributing project authors may | 7 * in the file PATENTS. All contributing project authors may |
8 * be found in the AUTHORS file in the root of the source tree. | 8 * be found in the AUTHORS file in the root of the source tree. |
9 */ | 9 */ |
10 | 10 |
(...skipping 27 matching lines...) Expand all Loading... | |
38 config.frame_size_ms = rtc::CheckedDivExact(codec_inst.pacsize, 48); | 38 config.frame_size_ms = rtc::CheckedDivExact(codec_inst.pacsize, 48); |
39 config.num_channels = codec_inst.channels; | 39 config.num_channels = codec_inst.channels; |
40 config.bitrate_bps = rtc::Optional<int>(codec_inst.rate); | 40 config.bitrate_bps = rtc::Optional<int>(codec_inst.rate); |
41 config.payload_type = codec_inst.pltype; | 41 config.payload_type = codec_inst.pltype; |
42 config.application = config.num_channels == 1 ? AudioEncoderOpus::kVoip | 42 config.application = config.num_channels == 1 ? AudioEncoderOpus::kVoip |
43 : AudioEncoderOpus::kAudio; | 43 : AudioEncoderOpus::kAudio; |
44 config.supported_frame_lengths_ms.push_back(config.frame_size_ms); | 44 config.supported_frame_lengths_ms.push_back(config.frame_size_ms); |
45 return config; | 45 return config; |
46 } | 46 } |
47 | 47 |
48 AudioEncoderOpus::Config CreateConfigWithParameters( | |
49 const SdpAudioFormat::Parameters& params) { | |
50 webrtc::SdpAudioFormat format("opus", 48000, 2, params); | |
kwiberg-webrtc
2017/03/15 13:33:18
Is the webrtc:: prefix necessary?
ossu
2017/03/16 18:03:58
Nope.
| |
51 return AudioEncoderOpus::CreateConfig(0, format); | |
52 } | |
53 | |
48 struct AudioEncoderOpusStates { | 54 struct AudioEncoderOpusStates { |
49 std::shared_ptr<MockAudioNetworkAdaptor*> mock_audio_network_adaptor; | 55 std::shared_ptr<MockAudioNetworkAdaptor*> mock_audio_network_adaptor; |
50 MockSmoothingFilter* mock_bitrate_smoother; | 56 MockSmoothingFilter* mock_bitrate_smoother; |
51 std::unique_ptr<AudioEncoderOpus> encoder; | 57 std::unique_ptr<AudioEncoderOpus> encoder; |
52 std::unique_ptr<SimulatedClock> simulated_clock; | 58 std::unique_ptr<SimulatedClock> simulated_clock; |
53 AudioEncoderOpus::Config config; | 59 AudioEncoderOpus::Config config; |
54 }; | 60 }; |
55 | 61 |
56 AudioEncoderOpusStates CreateCodec(size_t num_channels) { | 62 AudioEncoderOpusStates CreateCodec(size_t num_channels) { |
57 AudioEncoderOpusStates states; | 63 AudioEncoderOpusStates states; |
(...skipping 43 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
101 config.enable_dtx = rtc::Optional<bool>(kEnableDtx); | 107 config.enable_dtx = rtc::Optional<bool>(kEnableDtx); |
102 config.num_channels = rtc::Optional<size_t>(kNumChannels); | 108 config.num_channels = rtc::Optional<size_t>(kNumChannels); |
103 config.uplink_packet_loss_fraction = | 109 config.uplink_packet_loss_fraction = |
104 rtc::Optional<float>(kPacketLossFraction); | 110 rtc::Optional<float>(kPacketLossFraction); |
105 return config; | 111 return config; |
106 } | 112 } |
107 | 113 |
108 void CheckEncoderRuntimeConfig( | 114 void CheckEncoderRuntimeConfig( |
109 const AudioEncoderOpus* encoder, | 115 const AudioEncoderOpus* encoder, |
110 const AudioNetworkAdaptor::EncoderRuntimeConfig& config) { | 116 const AudioNetworkAdaptor::EncoderRuntimeConfig& config) { |
111 EXPECT_EQ(*config.bitrate_bps, encoder->GetTargetBitrate()); | 117 EXPECT_EQ(config.bitrate_bps, encoder->GetTargetBitrate()); |
112 EXPECT_EQ(*config.frame_length_ms, encoder->next_frame_length_ms()); | 118 EXPECT_EQ(config.frame_length_ms, encoder->next_frame_length_ms()); |
113 EXPECT_EQ(*config.enable_fec, encoder->fec_enabled()); | 119 EXPECT_EQ(config.enable_fec, encoder->fec_enabled()); |
114 EXPECT_EQ(*config.enable_dtx, encoder->GetDtx()); | 120 EXPECT_EQ(config.enable_dtx, encoder->GetDtx()); |
115 EXPECT_EQ(*config.num_channels, encoder->num_channels_to_encode()); | 121 EXPECT_EQ(config.num_channels, encoder->num_channels_to_encode()); |
116 } | 122 } |
117 | 123 |
118 // Create 10ms audio data blocks for a total packet size of "packet_size_ms". | 124 // Create 10ms audio data blocks for a total packet size of "packet_size_ms". |
119 std::unique_ptr<test::AudioLoop> Create10msAudioBlocks( | 125 std::unique_ptr<test::AudioLoop> Create10msAudioBlocks( |
120 const std::unique_ptr<AudioEncoderOpus>& encoder, | 126 const std::unique_ptr<AudioEncoderOpus>& encoder, |
121 int packet_size_ms) { | 127 int packet_size_ms) { |
122 const std::string file_name = | 128 const std::string file_name = |
123 test::ResourcePath("audio_coding/testfile32kHz", "pcm"); | 129 test::ResourcePath("audio_coding/testfile32kHz", "pcm"); |
124 | 130 |
125 std::unique_ptr<test::AudioLoop> speech_data(new test::AudioLoop()); | 131 std::unique_ptr<test::AudioLoop> speech_data(new test::AudioLoop()); |
(...skipping 54 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
180 EXPECT_EQ(AudioEncoderOpus::kAudio, states.encoder->application()); | 186 EXPECT_EQ(AudioEncoderOpus::kAudio, states.encoder->application()); |
181 // Turn off DTX. | 187 // Turn off DTX. |
182 EXPECT_TRUE(states.encoder->SetDtx(false)); | 188 EXPECT_TRUE(states.encoder->SetDtx(false)); |
183 } | 189 } |
184 | 190 |
185 TEST(AudioEncoderOpusTest, | 191 TEST(AudioEncoderOpusTest, |
186 OnReceivedUplinkBandwidthWithoutAudioNetworkAdaptor) { | 192 OnReceivedUplinkBandwidthWithoutAudioNetworkAdaptor) { |
187 auto states = CreateCodec(1); | 193 auto states = CreateCodec(1); |
188 // Constants are replicated from audio_states.encoderopus.cc. | 194 // Constants are replicated from audio_states.encoderopus.cc. |
189 const int kMinBitrateBps = 6000; | 195 const int kMinBitrateBps = 6000; |
190 const int kMaxBitrateBps = 512000; | 196 const int kMaxBitrateBps = 510000; |
191 // Set a too low bitrate. | 197 // Set a too low bitrate. |
192 states.encoder->OnReceivedUplinkBandwidth(kMinBitrateBps - 1, | 198 states.encoder->OnReceivedUplinkBandwidth(kMinBitrateBps - 1, |
193 rtc::Optional<int64_t>()); | 199 rtc::Optional<int64_t>()); |
194 EXPECT_EQ(kMinBitrateBps, states.encoder->GetTargetBitrate()); | 200 EXPECT_EQ(kMinBitrateBps, states.encoder->GetTargetBitrate()); |
195 // Set a too high bitrate. | 201 // Set a too high bitrate. |
196 states.encoder->OnReceivedUplinkBandwidth(kMaxBitrateBps + 1, | 202 states.encoder->OnReceivedUplinkBandwidth(kMaxBitrateBps + 1, |
197 rtc::Optional<int64_t>()); | 203 rtc::Optional<int64_t>()); |
198 EXPECT_EQ(kMaxBitrateBps, states.encoder->GetTargetBitrate()); | 204 EXPECT_EQ(kMaxBitrateBps, states.encoder->GetTargetBitrate()); |
199 // Set the minimum rate. | 205 // Set the minimum rate. |
200 states.encoder->OnReceivedUplinkBandwidth(kMinBitrateBps, | 206 states.encoder->OnReceivedUplinkBandwidth(kMinBitrateBps, |
(...skipping 210 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
411 EXPECT_EQ(kTargetBitrateBps - | 417 EXPECT_EQ(kTargetBitrateBps - |
412 8 * static_cast<int>(kOverheadBytesPerPacket) * packet_rate, | 418 8 * static_cast<int>(kOverheadBytesPerPacket) * packet_rate, |
413 states.encoder->GetTargetBitrate()); | 419 states.encoder->GetTargetBitrate()); |
414 } | 420 } |
415 | 421 |
416 TEST(AudioEncoderOpusTest, BitrateBounded) { | 422 TEST(AudioEncoderOpusTest, BitrateBounded) { |
417 test::ScopedFieldTrials override_field_trials( | 423 test::ScopedFieldTrials override_field_trials( |
418 "WebRTC-SendSideBwe-WithOverhead/Enabled/"); | 424 "WebRTC-SendSideBwe-WithOverhead/Enabled/"); |
419 | 425 |
420 constexpr int kMinBitrateBps = 6000; | 426 constexpr int kMinBitrateBps = 6000; |
421 constexpr int kMaxBitrateBps = 512000; | 427 constexpr int kMaxBitrateBps = 510000; |
422 | 428 |
423 auto states = CreateCodec(2); | 429 auto states = CreateCodec(2); |
424 | 430 |
425 constexpr size_t kOverheadBytesPerPacket = 64; | 431 constexpr size_t kOverheadBytesPerPacket = 64; |
426 states.encoder->OnReceivedOverhead(kOverheadBytesPerPacket); | 432 states.encoder->OnReceivedOverhead(kOverheadBytesPerPacket); |
427 | 433 |
428 int packet_rate = rtc::CheckedDivExact(48000, kDefaultOpusSettings.pacsize); | 434 int packet_rate = rtc::CheckedDivExact(48000, kDefaultOpusSettings.pacsize); |
429 | 435 |
430 // Set a target rate that is smaller than |kMinBitrateBps| when overhead is | 436 // Set a target rate that is smaller than |kMinBitrateBps| when overhead is |
431 // subtracted. The eventual codec rate should be bounded by |kMinBitrateBps|. | 437 // subtracted. The eventual codec rate should be bounded by |kMinBitrateBps|. |
(...skipping 107 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
539 } | 545 } |
540 | 546 |
541 // Should encode now. | 547 // Should encode now. |
542 states.encoder->Encode(rtp_timestamp, audio_frames->GetNextBlock(), | 548 states.encoder->Encode(rtp_timestamp, audio_frames->GetNextBlock(), |
543 &encoded); | 549 &encoded); |
544 EXPECT_GT(encoded.size(), 0u); | 550 EXPECT_GT(encoded.size(), 0u); |
545 encoded.Clear(); | 551 encoded.Clear(); |
546 } | 552 } |
547 } | 553 } |
548 | 554 |
555 TEST(AudioEncoderOpusTest, TestConfigDefaults) { | |
556 const AudioEncoderOpus::Config config = | |
557 AudioEncoderOpus::CreateConfig(0, {"opus", 48000, 2}); | |
558 | |
559 EXPECT_EQ(48000, config.max_playback_rate_hz); | |
560 EXPECT_EQ(1U, config.num_channels); | |
kwiberg-webrtc
2017/03/15 13:33:18
Lower-case "u"? It's easier to read, because all d
ossu
2017/03/16 18:03:58
Acknowledged.
| |
561 EXPECT_FALSE(config.fec_enabled); | |
562 EXPECT_FALSE(config.dtx_enabled); | |
563 EXPECT_EQ(20, config.frame_size_ms); | |
564 } | |
565 | |
566 TEST(AudioEncoderOpusTest, TestConfigFromParams) { | |
567 AudioEncoderOpus::Config config; | |
568 | |
569 config = CreateConfigWithParameters({{"stereo", "0"}}); | |
570 EXPECT_EQ(1U, config.num_channels); | |
571 | |
572 config = CreateConfigWithParameters({{"stereo", "1"}}); | |
573 EXPECT_EQ(2U, config.num_channels); | |
574 | |
575 config = CreateConfigWithParameters({{"useinbandfec", "0"}}); | |
576 EXPECT_EQ(false, config.fec_enabled); | |
577 | |
578 config = CreateConfigWithParameters({{"useinbandfec", "1"}}); | |
579 EXPECT_EQ(true, config.fec_enabled); | |
580 | |
581 config = CreateConfigWithParameters({{"usedtx", "0"}}); | |
582 EXPECT_EQ(false, config.dtx_enabled); | |
583 | |
584 config = CreateConfigWithParameters({{"usedtx", "1"}}); | |
585 EXPECT_EQ(true, config.dtx_enabled); | |
586 | |
587 config = CreateConfigWithParameters({{"maxplaybackrate", "12345"}}); | |
588 EXPECT_EQ(12345, config.max_playback_rate_hz); | |
589 | |
590 config = CreateConfigWithParameters({{"maxaveragebitrate", "96000"}}); | |
591 EXPECT_EQ(96000, config.bitrate_bps); | |
592 | |
593 config = CreateConfigWithParameters({{"maxptime", "40"}}); | |
594 for (int frame_length : config.supported_frame_lengths_ms) { | |
595 EXPECT_LE(frame_length, 40); | |
596 } | |
kwiberg-webrtc
2017/03/15 13:33:18
EXPECT the full list, to make the test easier to r
ossu
2017/03/16 18:03:58
I don't want to make the test overly strict. If we
kwiberg-webrtc
2017/03/17 10:20:02
Acknowledged.
| |
597 | |
598 config = CreateConfigWithParameters({{"minptime", "40"}}); | |
599 for (int frame_length : config.supported_frame_lengths_ms) { | |
600 EXPECT_GE(frame_length, 40); | |
601 } | |
602 | |
603 config = CreateConfigWithParameters({{"ptime", "40"}}); | |
604 EXPECT_EQ(40, config.frame_size_ms); | |
605 | |
606 constexpr int kMinSupportedFrameLength = 10; | |
607 constexpr int kMaxSupportedFrameLength = | |
608 WEBRTC_OPUS_SUPPORT_120MS_PTIME ? 120 : 60; | |
609 | |
610 config = CreateConfigWithParameters({{"ptime", "1"}}); | |
611 EXPECT_EQ(kMinSupportedFrameLength, config.frame_size_ms); | |
612 | |
613 config = CreateConfigWithParameters({{"ptime", "2000"}}); | |
614 EXPECT_EQ(kMaxSupportedFrameLength, config.frame_size_ms); | |
615 } | |
616 | |
617 TEST(AudioEncoderOpusTest, TestConfigFromInvalidParams) { | |
618 const webrtc::SdpAudioFormat format("opus", 48000, 2); | |
619 const AudioEncoderOpus::Config default_config = | |
620 AudioEncoderOpus::CreateConfig(0, format); | |
621 const std::vector<int> default_supported_frame_lengths_ms({20, 60}); | |
622 | |
623 AudioEncoderOpus::Config config; | |
624 config = CreateConfigWithParameters({{"stereo", "invalid"}}); | |
625 EXPECT_EQ(default_config.num_channels, config.num_channels); | |
626 | |
627 config = CreateConfigWithParameters({{"useinbandfec", "invalid"}}); | |
628 EXPECT_EQ(default_config.fec_enabled, config.fec_enabled); | |
629 | |
630 config = CreateConfigWithParameters({{"usedtx", "invalid"}}); | |
631 EXPECT_EQ(default_config.dtx_enabled, config.dtx_enabled); | |
632 | |
633 config = CreateConfigWithParameters({{"maxplaybackrate", "0"}}); | |
634 EXPECT_EQ(default_config.max_playback_rate_hz, config.max_playback_rate_hz); | |
635 | |
636 config = CreateConfigWithParameters({{"maxplaybackrate", "-23"}}); | |
637 EXPECT_EQ(default_config.max_playback_rate_hz, config.max_playback_rate_hz); | |
638 | |
639 config = CreateConfigWithParameters({{"maxplaybackrate", "not a number!"}}); | |
640 EXPECT_EQ(default_config.max_playback_rate_hz, config.max_playback_rate_hz); | |
641 | |
642 config = CreateConfigWithParameters({{"maxaveragebitrate", "0"}}); | |
643 EXPECT_EQ(6000, config.bitrate_bps); | |
644 | |
645 config = CreateConfigWithParameters({{"maxaveragebitrate", "-1000"}}); | |
646 EXPECT_EQ(6000, config.bitrate_bps); | |
647 | |
648 config = CreateConfigWithParameters({{"maxaveragebitrate", "1024000"}}); | |
649 EXPECT_EQ(510000, config.bitrate_bps); | |
650 | |
651 config = CreateConfigWithParameters({{"maxaveragebitrate", "not a number!"}}); | |
652 EXPECT_EQ(default_config.bitrate_bps, config.bitrate_bps); | |
653 | |
654 config = CreateConfigWithParameters({{"maxptime", "invalid"}}); | |
655 EXPECT_EQ(default_supported_frame_lengths_ms, | |
656 config.supported_frame_lengths_ms); | |
657 | |
658 config = CreateConfigWithParameters({{"minptime", "invalid"}}); | |
659 EXPECT_EQ(default_supported_frame_lengths_ms, | |
660 config.supported_frame_lengths_ms); | |
661 | |
662 config = CreateConfigWithParameters({{"ptime", "invalid"}}); | |
663 EXPECT_EQ(default_supported_frame_lengths_ms, | |
664 config.supported_frame_lengths_ms); | |
665 } | |
666 | |
667 // Test that bitrate will be overridden by the "maxaveragebitrate" parameter. | |
668 // Also test that the "maxaveragebitrate" can't be set to values outside the | |
669 // range of 6000 and 510000 | |
670 TEST(AudioEncoderOpusTest, SetSendCodecOpusMaxAverageBitrate) { | |
671 // Ignore if less than 6000. | |
672 const AudioEncoderOpus::Config config1 = AudioEncoderOpus::CreateConfig( | |
673 0, {"opus", 48000, 2, {{"maxaveragebitrate", "5999"}}}); | |
674 EXPECT_EQ(6000, config1.bitrate_bps); | |
675 | |
676 // Ignore if larger than 510000. | |
677 const AudioEncoderOpus::Config config2 = AudioEncoderOpus::CreateConfig( | |
678 0, {"opus", 48000, 2, {{"maxaveragebitrate", "510001"}}}); | |
679 EXPECT_EQ(510000, config2.bitrate_bps); | |
680 | |
681 const AudioEncoderOpus::Config config3 = AudioEncoderOpus::CreateConfig( | |
682 0, {"opus", 48000, 2, {{"maxaveragebitrate", "200000"}}}); | |
683 EXPECT_EQ(200000, config3.bitrate_bps); | |
684 } | |
685 | |
686 // Test maxplaybackrate <= 8000 triggers Opus narrow band mode. | |
687 TEST(AudioEncoderOpusTest, SetMaxPlaybackRateNb) { | |
688 auto config = CreateConfigWithParameters({{"maxplaybackrate", "8000"}}); | |
689 EXPECT_EQ(8000, config.max_playback_rate_hz); | |
690 EXPECT_EQ(12000, config.bitrate_bps); | |
691 | |
692 config = CreateConfigWithParameters({{"maxplaybackrate", "8000"}, | |
693 {"stereo", "1"}}); | |
694 EXPECT_EQ(8000, config.max_playback_rate_hz); | |
695 EXPECT_EQ(24000, config.bitrate_bps); | |
696 } | |
697 | |
698 // Test 8000 < maxplaybackrate <= 12000 triggers Opus medium band mode. | |
699 TEST(AudioEncoderOpusTest, SetMaxPlaybackRateMb) { | |
700 auto config = CreateConfigWithParameters({{"maxplaybackrate", "8001"}}); | |
701 EXPECT_EQ(8001, config.max_playback_rate_hz); | |
702 EXPECT_EQ(20000, config.bitrate_bps); | |
703 | |
704 config = CreateConfigWithParameters({{"maxplaybackrate", "8001"}, | |
705 {"stereo", "1"}}); | |
706 EXPECT_EQ(8001, config.max_playback_rate_hz); | |
707 EXPECT_EQ(40000, config.bitrate_bps); | |
708 } | |
709 | |
710 // Test 12000 < maxplaybackrate <= 16000 triggers Opus wide band mode. | |
711 TEST(AudioEncoderOpusTest, SetMaxPlaybackRateWb) { | |
712 auto config = CreateConfigWithParameters({{"maxplaybackrate", "12001"}}); | |
713 EXPECT_EQ(12001, config.max_playback_rate_hz); | |
714 EXPECT_EQ(20000, config.bitrate_bps); | |
715 | |
716 config = CreateConfigWithParameters({{"maxplaybackrate", "12001"}, | |
717 {"stereo", "1"}}); | |
718 EXPECT_EQ(12001, config.max_playback_rate_hz); | |
719 EXPECT_EQ(40000, config.bitrate_bps); | |
720 } | |
721 | |
722 // Test 16000 < maxplaybackrate <= 24000 triggers Opus super wide band mode. | |
723 TEST(AudioEncoderOpusTest, SetMaxPlaybackRateSwb) { | |
724 auto config = CreateConfigWithParameters({{"maxplaybackrate", "16001"}}); | |
725 EXPECT_EQ(16001, config.max_playback_rate_hz); | |
726 EXPECT_EQ(32000, config.bitrate_bps); | |
727 | |
728 config = CreateConfigWithParameters({{"maxplaybackrate", "16001"}, | |
729 {"stereo", "1"}}); | |
730 EXPECT_EQ(16001, config.max_playback_rate_hz); | |
731 EXPECT_EQ(64000, config.bitrate_bps); | |
732 } | |
733 | |
734 // Test 24000 < maxplaybackrate triggers Opus full band mode. | |
735 TEST(AudioEncoderOpusTest, SetMaxPlaybackRateFb) { | |
736 auto config = CreateConfigWithParameters({{"maxplaybackrate", "24001"}}); | |
737 EXPECT_EQ(24001, config.max_playback_rate_hz); | |
738 EXPECT_EQ(32000, config.bitrate_bps); | |
739 | |
740 config = CreateConfigWithParameters({{"maxplaybackrate", "24001"}, | |
741 {"stereo", "1"}}); | |
742 EXPECT_EQ(24001, config.max_playback_rate_hz); | |
743 EXPECT_EQ(64000, config.bitrate_bps); | |
744 } | |
745 | |
549 } // namespace webrtc | 746 } // namespace webrtc |
OLD | NEW |