solenberg | c7a8b08 | 2015-10-16 14:35:07 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (c) 2015 The WebRTC project authors. All Rights Reserved. |
| 3 | * |
| 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 |
| 6 | * tree. An additional intellectual property rights grant can be found |
| 7 | * in the file PATENTS. All contributing project authors may |
| 8 | * be found in the AUTHORS file in the root of the source tree. |
| 9 | */ |
| 10 | |
Fredrik Solenberg | ea07373 | 2015-12-01 11:26:34 +0100 | [diff] [blame] | 11 | #include <string> |
ossu | 20a4b3f | 2017-04-27 02:08:52 -0700 | [diff] [blame] | 12 | #include <utility> |
Fredrik Solenberg | ea07373 | 2015-12-01 11:26:34 +0100 | [diff] [blame] | 13 | #include <vector> |
| 14 | |
Karl Wiberg | 918f50c | 2018-07-05 11:40:33 +0200 | [diff] [blame] | 15 | #include "absl/memory/memory.h" |
Benjamin Wright | 78410ad | 2018-10-25 09:52:57 -0700 | [diff] [blame] | 16 | #include "api/test/mock_frame_encryptor.h" |
Mirko Bonadei | 92ea95e | 2017-09-15 06:47:31 +0200 | [diff] [blame] | 17 | #include "audio/audio_send_stream.h" |
| 18 | #include "audio/audio_state.h" |
| 19 | #include "audio/conversion.h" |
Fredrik Solenberg | a8b7c7f | 2018-01-17 11:18:31 +0100 | [diff] [blame] | 20 | #include "audio/mock_voe_channel_proxy.h" |
Sebastian Jansson | ef9daee | 2018-02-22 14:49:02 +0100 | [diff] [blame] | 21 | #include "call/test/mock_rtp_transport_controller_send.h" |
Mirko Bonadei | 92ea95e | 2017-09-15 06:47:31 +0200 | [diff] [blame] | 22 | #include "logging/rtc_event_log/mock/mock_rtc_event_log.h" |
Fredrik Solenberg | 2a87797 | 2017-12-15 16:42:15 +0100 | [diff] [blame] | 23 | #include "modules/audio_device/include/mock_audio_device.h" |
Mirko Bonadei | 92ea95e | 2017-09-15 06:47:31 +0200 | [diff] [blame] | 24 | #include "modules/audio_mixer/audio_mixer_impl.h" |
Ivo Creusen | 56d4609 | 2017-11-24 17:29:59 +0100 | [diff] [blame] | 25 | #include "modules/audio_processing/include/audio_processing_statistics.h" |
Mirko Bonadei | 92ea95e | 2017-09-15 06:47:31 +0200 | [diff] [blame] | 26 | #include "modules/audio_processing/include/mock_audio_processing.h" |
Sebastian Jansson | ef9daee | 2018-02-22 14:49:02 +0100 | [diff] [blame] | 27 | #include "modules/rtp_rtcp/mocks/mock_rtcp_bandwidth_observer.h" |
Mirko Bonadei | 92ea95e | 2017-09-15 06:47:31 +0200 | [diff] [blame] | 28 | #include "modules/rtp_rtcp/mocks/mock_rtcp_rtt_stats.h" |
| 29 | #include "modules/rtp_rtcp/mocks/mock_rtp_rtcp.h" |
Mirko Bonadei | 92ea95e | 2017-09-15 06:47:31 +0200 | [diff] [blame] | 30 | #include "rtc_base/task_queue.h" |
| 31 | #include "test/gtest.h" |
| 32 | #include "test/mock_audio_encoder.h" |
| 33 | #include "test/mock_audio_encoder_factory.h" |
solenberg | c7a8b08 | 2015-10-16 14:35:07 -0700 | [diff] [blame] | 34 | |
| 35 | namespace webrtc { |
solenberg | 85a0496 | 2015-10-27 03:35:21 -0700 | [diff] [blame] | 36 | namespace test { |
Fredrik Solenberg | 0ccae13 | 2015-11-03 10:15:49 +0100 | [diff] [blame] | 37 | namespace { |
| 38 | |
solenberg | 3a94154 | 2015-11-16 07:34:50 -0800 | [diff] [blame] | 39 | using testing::_; |
stefan | 7de8d64 | 2017-02-07 07:14:08 -0800 | [diff] [blame] | 40 | using testing::Eq; |
| 41 | using testing::Ne; |
Sebastian Jansson | 254d869 | 2018-11-21 19:19:00 +0100 | [diff] [blame] | 42 | using testing::Field; |
ossu | 20a4b3f | 2017-04-27 02:08:52 -0700 | [diff] [blame] | 43 | using testing::Invoke; |
solenberg | 3a94154 | 2015-11-16 07:34:50 -0800 | [diff] [blame] | 44 | using testing::Return; |
minyue-webrtc | 8de1826 | 2017-07-26 14:18:40 +0200 | [diff] [blame] | 45 | using testing::StrEq; |
solenberg | 3a94154 | 2015-11-16 07:34:50 -0800 | [diff] [blame] | 46 | |
Fredrik Solenberg | 0ccae13 | 2015-11-03 10:15:49 +0100 | [diff] [blame] | 47 | const uint32_t kSsrc = 1234; |
solenberg | 3a94154 | 2015-11-16 07:34:50 -0800 | [diff] [blame] | 48 | const char* kCName = "foo_name"; |
| 49 | const int kAudioLevelId = 2; |
Stefan Holmer | b86d4e4 | 2015-12-07 10:26:18 +0100 | [diff] [blame] | 50 | const int kTransportSequenceNumberId = 4; |
Ivo Creusen | 56d4609 | 2017-11-24 17:29:59 +0100 | [diff] [blame] | 51 | const int32_t kEchoDelayMedian = 254; |
| 52 | const int32_t kEchoDelayStdDev = -3; |
| 53 | const double kDivergentFilterFraction = 0.2f; |
| 54 | const double kEchoReturnLoss = -65; |
| 55 | const double kEchoReturnLossEnhancement = 101; |
| 56 | const double kResidualEchoLikelihood = -1.0f; |
| 57 | const double kResidualEchoLikelihoodMax = 23.0f; |
Niels Möller | 530ead4 | 2018-10-04 14:28:39 +0200 | [diff] [blame] | 58 | const CallSendStatistics kCallStats = {112, 13456, 17890}; |
solenberg | 566ef24 | 2015-11-06 15:34:49 -0800 | [diff] [blame] | 59 | const ReportBlock kReportBlock = {456, 780, 123, 567, 890, 132, 143, 13354}; |
Fredrik Solenberg | b572768 | 2015-12-04 15:22:19 +0100 | [diff] [blame] | 60 | const int kTelephoneEventPayloadType = 123; |
solenberg | ffbbcac | 2016-11-17 05:25:37 -0800 | [diff] [blame] | 61 | const int kTelephoneEventPayloadFrequency = 65432; |
solenberg | 8842c3e | 2016-03-11 03:06:41 -0800 | [diff] [blame] | 62 | const int kTelephoneEventCode = 45; |
| 63 | const int kTelephoneEventDuration = 6789; |
ossu | 20a4b3f | 2017-04-27 02:08:52 -0700 | [diff] [blame] | 64 | constexpr int kIsacPayloadType = 103; |
| 65 | const SdpAudioFormat kIsacFormat = {"isac", 16000, 1}; |
| 66 | const SdpAudioFormat kOpusFormat = {"opus", 48000, 2}; |
| 67 | const SdpAudioFormat kG722Format = {"g722", 8000, 1}; |
| 68 | const AudioCodecSpec kCodecSpecs[] = { |
| 69 | {kIsacFormat, {16000, 1, 32000, 10000, 32000}}, |
| 70 | {kOpusFormat, {48000, 1, 32000, 6000, 510000}}, |
| 71 | {kG722Format, {16000, 1, 64000}}}; |
solenberg | 566ef24 | 2015-11-06 15:34:49 -0800 | [diff] [blame] | 72 | |
mflodman | 86cc6ff | 2016-07-26 04:44:06 -0700 | [diff] [blame] | 73 | class MockLimitObserver : public BitrateAllocator::LimitObserver { |
| 74 | public: |
Sebastian Jansson | 35fa280 | 2018-10-01 09:16:12 +0200 | [diff] [blame] | 75 | MOCK_METHOD5(OnAllocationLimitsChanged, |
mflodman | 86cc6ff | 2016-07-26 04:44:06 -0700 | [diff] [blame] | 76 | void(uint32_t min_send_bitrate_bps, |
philipel | f69e768 | 2018-02-28 13:06:28 +0100 | [diff] [blame] | 77 | uint32_t max_padding_bitrate_bps, |
Sebastian Jansson | fe617a3 | 2018-03-21 12:45:20 +0100 | [diff] [blame] | 78 | uint32_t total_bitrate_bps, |
Sebastian Jansson | 35fa280 | 2018-10-01 09:16:12 +0200 | [diff] [blame] | 79 | uint32_t allocated_without_feedback_bps, |
Sebastian Jansson | fe617a3 | 2018-03-21 12:45:20 +0100 | [diff] [blame] | 80 | bool has_packet_feedback)); |
mflodman | 86cc6ff | 2016-07-26 04:44:06 -0700 | [diff] [blame] | 81 | }; |
| 82 | |
ossu | 20a4b3f | 2017-04-27 02:08:52 -0700 | [diff] [blame] | 83 | std::unique_ptr<MockAudioEncoder> SetupAudioEncoderMock( |
| 84 | int payload_type, |
| 85 | const SdpAudioFormat& format) { |
| 86 | for (const auto& spec : kCodecSpecs) { |
| 87 | if (format == spec.format) { |
Sebastian Jansson | 41f16be | 2018-02-22 11:09:56 +0100 | [diff] [blame] | 88 | std::unique_ptr<MockAudioEncoder> encoder( |
| 89 | new testing::NiceMock<MockAudioEncoder>()); |
ossu | 20a4b3f | 2017-04-27 02:08:52 -0700 | [diff] [blame] | 90 | ON_CALL(*encoder.get(), SampleRateHz()) |
| 91 | .WillByDefault(Return(spec.info.sample_rate_hz)); |
| 92 | ON_CALL(*encoder.get(), NumChannels()) |
| 93 | .WillByDefault(Return(spec.info.num_channels)); |
| 94 | ON_CALL(*encoder.get(), RtpTimestampRateHz()) |
| 95 | .WillByDefault(Return(spec.format.clockrate_hz)); |
| 96 | return encoder; |
| 97 | } |
| 98 | } |
| 99 | return nullptr; |
| 100 | } |
| 101 | |
| 102 | rtc::scoped_refptr<MockAudioEncoderFactory> SetupEncoderFactoryMock() { |
| 103 | rtc::scoped_refptr<MockAudioEncoderFactory> factory = |
| 104 | new rtc::RefCountedObject<MockAudioEncoderFactory>(); |
| 105 | ON_CALL(*factory.get(), GetSupportedEncoders()) |
| 106 | .WillByDefault(Return(std::vector<AudioCodecSpec>( |
| 107 | std::begin(kCodecSpecs), std::end(kCodecSpecs)))); |
| 108 | ON_CALL(*factory.get(), QueryAudioEncoder(_)) |
Oskar Sundbom | 2707fb2 | 2017-11-16 10:57:35 +0100 | [diff] [blame] | 109 | .WillByDefault(Invoke( |
Danil Chapovalov | b9b146c | 2018-06-15 12:28:07 +0200 | [diff] [blame] | 110 | [](const SdpAudioFormat& format) -> absl::optional<AudioCodecInfo> { |
Oskar Sundbom | 2707fb2 | 2017-11-16 10:57:35 +0100 | [diff] [blame] | 111 | for (const auto& spec : kCodecSpecs) { |
| 112 | if (format == spec.format) { |
| 113 | return spec.info; |
| 114 | } |
| 115 | } |
Danil Chapovalov | b9b146c | 2018-06-15 12:28:07 +0200 | [diff] [blame] | 116 | return absl::nullopt; |
Oskar Sundbom | 2707fb2 | 2017-11-16 10:57:35 +0100 | [diff] [blame] | 117 | })); |
Karl Wiberg | d6fbf2a | 2018-02-27 13:37:31 +0100 | [diff] [blame] | 118 | ON_CALL(*factory.get(), MakeAudioEncoderMock(_, _, _, _)) |
ossu | 20a4b3f | 2017-04-27 02:08:52 -0700 | [diff] [blame] | 119 | .WillByDefault(Invoke([](int payload_type, const SdpAudioFormat& format, |
Danil Chapovalov | b9b146c | 2018-06-15 12:28:07 +0200 | [diff] [blame] | 120 | absl::optional<AudioCodecPairId> codec_pair_id, |
ossu | 20a4b3f | 2017-04-27 02:08:52 -0700 | [diff] [blame] | 121 | std::unique_ptr<AudioEncoder>* return_value) { |
| 122 | *return_value = SetupAudioEncoderMock(payload_type, format); |
| 123 | })); |
| 124 | return factory; |
| 125 | } |
| 126 | |
solenberg | 566ef24 | 2015-11-06 15:34:49 -0800 | [diff] [blame] | 127 | struct ConfigHelper { |
ossu | 20a4b3f | 2017-04-27 02:08:52 -0700 | [diff] [blame] | 128 | ConfigHelper(bool audio_bwe_enabled, bool expect_set_encoder_call) |
Niels Möller | 7d76a31 | 2018-10-26 12:57:07 +0200 | [diff] [blame] | 129 | : stream_config_(/*send_transport=*/nullptr, /*media_transport=*/nullptr), |
peah | a9cc40b | 2017-06-29 08:32:09 -0700 | [diff] [blame] | 130 | audio_processing_(new rtc::RefCountedObject<MockAudioProcessing>()), |
perkj | 26091b1 | 2016-09-01 01:17:40 -0700 | [diff] [blame] | 131 | bitrate_allocator_(&limit_observer_), |
minyue-webrtc | 8de1826 | 2017-07-26 14:18:40 +0200 | [diff] [blame] | 132 | worker_queue_("ConfigHelper_worker_queue"), |
| 133 | audio_encoder_(nullptr) { |
solenberg | 1372508 | 2015-11-25 08:16:52 -0800 | [diff] [blame] | 134 | using testing::Invoke; |
solenberg | 3a94154 | 2015-11-16 07:34:50 -0800 | [diff] [blame] | 135 | |
solenberg | 566ef24 | 2015-11-06 15:34:49 -0800 | [diff] [blame] | 136 | AudioState::Config config; |
aleloi | 10111bc | 2016-11-17 06:48:48 -0800 | [diff] [blame] | 137 | config.audio_mixer = AudioMixerImpl::Create(); |
peah | a9cc40b | 2017-06-29 08:32:09 -0700 | [diff] [blame] | 138 | config.audio_processing = audio_processing_; |
Fredrik Solenberg | 2a87797 | 2017-12-15 16:42:15 +0100 | [diff] [blame] | 139 | config.audio_device_module = |
| 140 | new rtc::RefCountedObject<MockAudioDeviceModule>(); |
solenberg | 566ef24 | 2015-11-06 15:34:49 -0800 | [diff] [blame] | 141 | audio_state_ = AudioState::Create(config); |
solenberg | 3a94154 | 2015-11-16 07:34:50 -0800 | [diff] [blame] | 142 | |
Niels Möller | dced9f6 | 2018-11-19 10:27:07 +0100 | [diff] [blame] | 143 | SetupDefaultChannelSend(audio_bwe_enabled); |
ossu | 20a4b3f | 2017-04-27 02:08:52 -0700 | [diff] [blame] | 144 | SetupMockForSetupSendCodec(expect_set_encoder_call); |
minyue | 6b825df | 2016-10-31 04:08:32 -0700 | [diff] [blame] | 145 | |
Fredrik Solenberg | 8f5787a | 2018-01-11 13:52:30 +0100 | [diff] [blame] | 146 | // Use ISAC as default codec so as to prevent unnecessary |channel_proxy_| |
ossu | 20a4b3f | 2017-04-27 02:08:52 -0700 | [diff] [blame] | 147 | // calls from the default ctor behavior. |
| 148 | stream_config_.send_codec_spec = |
Oskar Sundbom | 2707fb2 | 2017-11-16 10:57:35 +0100 | [diff] [blame] | 149 | AudioSendStream::Config::SendCodecSpec(kIsacPayloadType, kIsacFormat); |
solenberg | 566ef24 | 2015-11-06 15:34:49 -0800 | [diff] [blame] | 150 | stream_config_.rtp.ssrc = kSsrc; |
solenberg | 3a94154 | 2015-11-16 07:34:50 -0800 | [diff] [blame] | 151 | stream_config_.rtp.c_name = kCName; |
| 152 | stream_config_.rtp.extensions.push_back( |
isheriff | 6f8d686 | 2016-05-26 11:24:55 -0700 | [diff] [blame] | 153 | RtpExtension(RtpExtension::kAudioLevelUri, kAudioLevelId)); |
stefan | 7de8d64 | 2017-02-07 07:14:08 -0800 | [diff] [blame] | 154 | if (audio_bwe_enabled) { |
ossu | 1129df2 | 2017-06-30 01:38:56 -0700 | [diff] [blame] | 155 | AddBweToConfig(&stream_config_); |
stefan | 7de8d64 | 2017-02-07 07:14:08 -0800 | [diff] [blame] | 156 | } |
ossu | 20a4b3f | 2017-04-27 02:08:52 -0700 | [diff] [blame] | 157 | stream_config_.encoder_factory = SetupEncoderFactoryMock(); |
minyue | 78b4d56 | 2016-11-30 04:47:39 -0800 | [diff] [blame] | 158 | stream_config_.min_bitrate_bps = 10000; |
| 159 | stream_config_.max_bitrate_bps = 65000; |
solenberg | 566ef24 | 2015-11-06 15:34:49 -0800 | [diff] [blame] | 160 | } |
| 161 | |
Fredrik Solenberg | 8f5787a | 2018-01-11 13:52:30 +0100 | [diff] [blame] | 162 | std::unique_ptr<internal::AudioSendStream> CreateAudioSendStream() { |
| 163 | return std::unique_ptr<internal::AudioSendStream>( |
| 164 | new internal::AudioSendStream( |
Sebastian Jansson | 1896cec | 2018-02-20 09:06:11 +0100 | [diff] [blame] | 165 | stream_config_, audio_state_, &worker_queue_, &rtp_transport_, |
Danil Chapovalov | b9b146c | 2018-06-15 12:28:07 +0200 | [diff] [blame] | 166 | &bitrate_allocator_, &event_log_, &rtcp_rtt_stats_, absl::nullopt, |
Niels Möller | dced9f6 | 2018-11-19 10:27:07 +0100 | [diff] [blame] | 167 | std::unique_ptr<voe::ChannelSendInterface>(channel_send_))); |
Fredrik Solenberg | 8f5787a | 2018-01-11 13:52:30 +0100 | [diff] [blame] | 168 | } |
| 169 | |
solenberg | 566ef24 | 2015-11-06 15:34:49 -0800 | [diff] [blame] | 170 | AudioSendStream::Config& config() { return stream_config_; } |
ossu | 20a4b3f | 2017-04-27 02:08:52 -0700 | [diff] [blame] | 171 | MockAudioEncoderFactory& mock_encoder_factory() { |
| 172 | return *static_cast<MockAudioEncoderFactory*>( |
| 173 | stream_config_.encoder_factory.get()); |
| 174 | } |
Niels Möller | dced9f6 | 2018-11-19 10:27:07 +0100 | [diff] [blame] | 175 | MockChannelSend* channel_send() { return channel_send_; } |
Sebastian Jansson | 1896cec | 2018-02-20 09:06:11 +0100 | [diff] [blame] | 176 | RtpTransportControllerSendInterface* transport() { return &rtp_transport_; } |
minyue | 7a97344 | 2016-10-20 03:27:12 -0700 | [diff] [blame] | 177 | |
ossu | 1129df2 | 2017-06-30 01:38:56 -0700 | [diff] [blame] | 178 | static void AddBweToConfig(AudioSendStream::Config* config) { |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame] | 179 | config->rtp.extensions.push_back(RtpExtension( |
| 180 | RtpExtension::kTransportSequenceNumberUri, kTransportSequenceNumberId)); |
ossu | 1129df2 | 2017-06-30 01:38:56 -0700 | [diff] [blame] | 181 | config->send_codec_spec->transport_cc_enabled = true; |
| 182 | } |
| 183 | |
Niels Möller | dced9f6 | 2018-11-19 10:27:07 +0100 | [diff] [blame] | 184 | void SetupDefaultChannelSend(bool audio_bwe_enabled) { |
| 185 | EXPECT_TRUE(channel_send_ == nullptr); |
| 186 | channel_send_ = new testing::StrictMock<MockChannelSend>(); |
| 187 | EXPECT_CALL(*channel_send_, GetRtpRtcp()).WillRepeatedly(Invoke([this]() { |
Niels Möller | 848d6d3 | 2018-08-08 10:49:16 +0200 | [diff] [blame] | 188 | return &this->rtp_rtcp_; |
| 189 | })); |
Niels Möller | dced9f6 | 2018-11-19 10:27:07 +0100 | [diff] [blame] | 190 | EXPECT_CALL(*channel_send_, SetLocalSSRC(kSsrc)).Times(1); |
| 191 | EXPECT_CALL(*channel_send_, SetRTCP_CNAME(StrEq(kCName))).Times(1); |
Niels Möller | dced9f6 | 2018-11-19 10:27:07 +0100 | [diff] [blame] | 192 | EXPECT_CALL(*channel_send_, SetFrameEncryptor(_)).Times(1); |
| 193 | EXPECT_CALL(*channel_send_, SetExtmapAllowMixed(false)).Times(1); |
| 194 | EXPECT_CALL(*channel_send_, |
minyue | 6b825df | 2016-10-31 04:08:32 -0700 | [diff] [blame] | 195 | SetSendAudioLevelIndicationStatus(true, kAudioLevelId)) |
| 196 | .Times(1); |
Sebastian Jansson | ef9daee | 2018-02-22 14:49:02 +0100 | [diff] [blame] | 197 | EXPECT_CALL(rtp_transport_, GetBandwidthObserver()) |
| 198 | .WillRepeatedly(Return(&bandwidth_observer_)); |
stefan | 7de8d64 | 2017-02-07 07:14:08 -0800 | [diff] [blame] | 199 | if (audio_bwe_enabled) { |
Niels Möller | dced9f6 | 2018-11-19 10:27:07 +0100 | [diff] [blame] | 200 | EXPECT_CALL(*channel_send_, |
stefan | 7de8d64 | 2017-02-07 07:14:08 -0800 | [diff] [blame] | 201 | EnableSendTransportSequenceNumber(kTransportSequenceNumberId)) |
| 202 | .Times(1); |
Niels Möller | dced9f6 | 2018-11-19 10:27:07 +0100 | [diff] [blame] | 203 | EXPECT_CALL(*channel_send_, |
Sebastian Jansson | ef9daee | 2018-02-22 14:49:02 +0100 | [diff] [blame] | 204 | RegisterSenderCongestionControlObjects( |
| 205 | &rtp_transport_, Eq(&bandwidth_observer_))) |
stefan | 7de8d64 | 2017-02-07 07:14:08 -0800 | [diff] [blame] | 206 | .Times(1); |
| 207 | } else { |
Niels Möller | dced9f6 | 2018-11-19 10:27:07 +0100 | [diff] [blame] | 208 | EXPECT_CALL(*channel_send_, RegisterSenderCongestionControlObjects( |
| 209 | &rtp_transport_, Eq(nullptr))) |
stefan | 7de8d64 | 2017-02-07 07:14:08 -0800 | [diff] [blame] | 210 | .Times(1); |
| 211 | } |
Niels Möller | dced9f6 | 2018-11-19 10:27:07 +0100 | [diff] [blame] | 212 | EXPECT_CALL(*channel_send_, ResetSenderCongestionControlObjects()).Times(1); |
Amit Hilbuch | 77938e6 | 2018-12-21 09:23:38 -0800 | [diff] [blame] | 213 | EXPECT_CALL(*channel_send_, SetRid(std::string(), 0, 0)).Times(1); |
minyue | 6b825df | 2016-10-31 04:08:32 -0700 | [diff] [blame] | 214 | } |
| 215 | |
ossu | 20a4b3f | 2017-04-27 02:08:52 -0700 | [diff] [blame] | 216 | void SetupMockForSetupSendCodec(bool expect_set_encoder_call) { |
| 217 | if (expect_set_encoder_call) { |
Niels Möller | dced9f6 | 2018-11-19 10:27:07 +0100 | [diff] [blame] | 218 | EXPECT_CALL(*channel_send_, SetEncoderForMock(_, _)) |
minyue-webrtc | 8de1826 | 2017-07-26 14:18:40 +0200 | [diff] [blame] | 219 | .WillOnce(Invoke( |
| 220 | [this](int payload_type, std::unique_ptr<AudioEncoder>* encoder) { |
| 221 | this->audio_encoder_ = std::move(*encoder); |
| 222 | return true; |
| 223 | })); |
ossu | 20a4b3f | 2017-04-27 02:08:52 -0700 | [diff] [blame] | 224 | } |
minyue | 7a97344 | 2016-10-20 03:27:12 -0700 | [diff] [blame] | 225 | } |
ossu | 20a4b3f | 2017-04-27 02:08:52 -0700 | [diff] [blame] | 226 | |
minyue-webrtc | 8de1826 | 2017-07-26 14:18:40 +0200 | [diff] [blame] | 227 | void SetupMockForModifyEncoder() { |
| 228 | // Let ModifyEncoder to invoke mock audio encoder. |
Niels Möller | dced9f6 | 2018-11-19 10:27:07 +0100 | [diff] [blame] | 229 | EXPECT_CALL(*channel_send_, ModifyEncoder(_)) |
Artem Titov | e7d08df | 2019-01-16 14:49:44 +0100 | [diff] [blame] | 230 | .WillRepeatedly( |
minyue-webrtc | 8de1826 | 2017-07-26 14:18:40 +0200 | [diff] [blame] | 231 | [this](rtc::FunctionView<void(std::unique_ptr<AudioEncoder>*)> |
| 232 | modifier) { |
| 233 | if (this->audio_encoder_) |
| 234 | modifier(&this->audio_encoder_); |
Artem Titov | e7d08df | 2019-01-16 14:49:44 +0100 | [diff] [blame] | 235 | }); |
minyue-webrtc | 8de1826 | 2017-07-26 14:18:40 +0200 | [diff] [blame] | 236 | } |
| 237 | |
Fredrik Solenberg | b572768 | 2015-12-04 15:22:19 +0100 | [diff] [blame] | 238 | void SetupMockForSendTelephoneEvent() { |
Niels Möller | dced9f6 | 2018-11-19 10:27:07 +0100 | [diff] [blame] | 239 | EXPECT_TRUE(channel_send_); |
| 240 | EXPECT_CALL(*channel_send_, SetSendTelephoneEventPayloadType( |
| 241 | kTelephoneEventPayloadType, |
| 242 | kTelephoneEventPayloadFrequency)) |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame] | 243 | .WillOnce(Return(true)); |
| 244 | EXPECT_CALL( |
Niels Möller | dced9f6 | 2018-11-19 10:27:07 +0100 | [diff] [blame] | 245 | *channel_send_, |
Fredrik Solenberg | b572768 | 2015-12-04 15:22:19 +0100 | [diff] [blame] | 246 | SendTelephoneEventOutband(kTelephoneEventCode, kTelephoneEventDuration)) |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame] | 247 | .WillOnce(Return(true)); |
Fredrik Solenberg | b572768 | 2015-12-04 15:22:19 +0100 | [diff] [blame] | 248 | } |
| 249 | |
solenberg | 566ef24 | 2015-11-06 15:34:49 -0800 | [diff] [blame] | 250 | void SetupMockForGetStats() { |
solenberg | 3a94154 | 2015-11-16 07:34:50 -0800 | [diff] [blame] | 251 | using testing::DoAll; |
solenberg | bd9a77f | 2017-02-06 12:53:57 -0800 | [diff] [blame] | 252 | using testing::SetArgPointee; |
solenberg | 3a94154 | 2015-11-16 07:34:50 -0800 | [diff] [blame] | 253 | using testing::SetArgReferee; |
| 254 | |
solenberg | 566ef24 | 2015-11-06 15:34:49 -0800 | [diff] [blame] | 255 | std::vector<ReportBlock> report_blocks; |
| 256 | webrtc::ReportBlock block = kReportBlock; |
| 257 | report_blocks.push_back(block); // Has wrong SSRC. |
| 258 | block.source_SSRC = kSsrc; |
| 259 | report_blocks.push_back(block); // Correct block. |
| 260 | block.fraction_lost = 0; |
| 261 | report_blocks.push_back(block); // Duplicate SSRC, bad fraction_lost. |
| 262 | |
Niels Möller | dced9f6 | 2018-11-19 10:27:07 +0100 | [diff] [blame] | 263 | EXPECT_TRUE(channel_send_); |
| 264 | EXPECT_CALL(*channel_send_, GetRTCPStatistics()) |
solenberg | 358057b | 2015-11-27 10:46:42 -0800 | [diff] [blame] | 265 | .WillRepeatedly(Return(kCallStats)); |
Niels Möller | dced9f6 | 2018-11-19 10:27:07 +0100 | [diff] [blame] | 266 | EXPECT_CALL(*channel_send_, GetRemoteRTCPReportBlocks()) |
solenberg | 358057b | 2015-11-27 10:46:42 -0800 | [diff] [blame] | 267 | .WillRepeatedly(Return(report_blocks)); |
Niels Möller | dced9f6 | 2018-11-19 10:27:07 +0100 | [diff] [blame] | 268 | EXPECT_CALL(*channel_send_, GetANAStatistics()) |
ivoc | e1198e0 | 2017-09-08 08:13:19 -0700 | [diff] [blame] | 269 | .WillRepeatedly(Return(ANAStats())); |
Niels Möller | dced9f6 | 2018-11-19 10:27:07 +0100 | [diff] [blame] | 270 | EXPECT_CALL(*channel_send_, GetBitrate()).WillRepeatedly(Return(0)); |
solenberg | 796b8f9 | 2017-03-01 17:02:23 -0800 | [diff] [blame] | 271 | |
Ivo Creusen | 56d4609 | 2017-11-24 17:29:59 +0100 | [diff] [blame] | 272 | audio_processing_stats_.echo_return_loss = kEchoReturnLoss; |
| 273 | audio_processing_stats_.echo_return_loss_enhancement = |
| 274 | kEchoReturnLossEnhancement; |
| 275 | audio_processing_stats_.delay_median_ms = kEchoDelayMedian; |
| 276 | audio_processing_stats_.delay_standard_deviation_ms = kEchoDelayStdDev; |
| 277 | audio_processing_stats_.divergent_filter_fraction = |
| 278 | kDivergentFilterFraction; |
| 279 | audio_processing_stats_.residual_echo_likelihood = kResidualEchoLikelihood; |
| 280 | audio_processing_stats_.residual_echo_likelihood_recent_max = |
| 281 | kResidualEchoLikelihoodMax; |
ivoc | 7aba029 | 2016-11-14 04:52:06 -0800 | [diff] [blame] | 282 | |
Ivo Creusen | 56d4609 | 2017-11-24 17:29:59 +0100 | [diff] [blame] | 283 | EXPECT_CALL(*audio_processing_, GetStatistics(true)) |
ivoc | 7aba029 | 2016-11-14 04:52:06 -0800 | [diff] [blame] | 284 | .WillRepeatedly(Return(audio_processing_stats_)); |
solenberg | 566ef24 | 2015-11-06 15:34:49 -0800 | [diff] [blame] | 285 | } |
| 286 | |
| 287 | private: |
solenberg | 566ef24 | 2015-11-06 15:34:49 -0800 | [diff] [blame] | 288 | rtc::scoped_refptr<AudioState> audio_state_; |
| 289 | AudioSendStream::Config stream_config_; |
Niels Möller | dced9f6 | 2018-11-19 10:27:07 +0100 | [diff] [blame] | 290 | testing::StrictMock<MockChannelSend>* channel_send_ = nullptr; |
peah | a9cc40b | 2017-06-29 08:32:09 -0700 | [diff] [blame] | 291 | rtc::scoped_refptr<MockAudioProcessing> audio_processing_; |
Ivo Creusen | 56d4609 | 2017-11-24 17:29:59 +0100 | [diff] [blame] | 292 | AudioProcessingStats audio_processing_stats_; |
Sebastian Jansson | ef9daee | 2018-02-22 14:49:02 +0100 | [diff] [blame] | 293 | testing::StrictMock<MockRtcpBandwidthObserver> bandwidth_observer_; |
Sebastian Jansson | 41f16be | 2018-02-22 11:09:56 +0100 | [diff] [blame] | 294 | testing::NiceMock<MockRtcEventLog> event_log_; |
Sebastian Jansson | ef9daee | 2018-02-22 14:49:02 +0100 | [diff] [blame] | 295 | testing::NiceMock<MockRtpTransportControllerSend> rtp_transport_; |
Sebastian Jansson | 41f16be | 2018-02-22 11:09:56 +0100 | [diff] [blame] | 296 | testing::NiceMock<MockRtpRtcp> rtp_rtcp_; |
michaelt | 9332b7d | 2016-11-30 07:51:13 -0800 | [diff] [blame] | 297 | MockRtcpRttStats rtcp_rtt_stats_; |
mflodman | 86cc6ff | 2016-07-26 04:44:06 -0700 | [diff] [blame] | 298 | testing::NiceMock<MockLimitObserver> limit_observer_; |
| 299 | BitrateAllocator bitrate_allocator_; |
perkj | 26091b1 | 2016-09-01 01:17:40 -0700 | [diff] [blame] | 300 | // |worker_queue| is defined last to ensure all pending tasks are cancelled |
| 301 | // and deleted before any other members. |
| 302 | rtc::TaskQueue worker_queue_; |
minyue-webrtc | 8de1826 | 2017-07-26 14:18:40 +0200 | [diff] [blame] | 303 | std::unique_ptr<AudioEncoder> audio_encoder_; |
solenberg | 566ef24 | 2015-11-06 15:34:49 -0800 | [diff] [blame] | 304 | }; |
Fredrik Solenberg | 0ccae13 | 2015-11-03 10:15:49 +0100 | [diff] [blame] | 305 | } // namespace |
solenberg | c7a8b08 | 2015-10-16 14:35:07 -0700 | [diff] [blame] | 306 | |
| 307 | TEST(AudioSendStreamTest, ConfigToString) { |
Niels Möller | 7d76a31 | 2018-10-26 12:57:07 +0200 | [diff] [blame] | 308 | AudioSendStream::Config config(/*send_transport=*/nullptr, |
| 309 | /*media_transport=*/nullptr); |
Fredrik Solenberg | 0ccae13 | 2015-11-03 10:15:49 +0100 | [diff] [blame] | 310 | config.rtp.ssrc = kSsrc; |
solenberg | 3a94154 | 2015-11-16 07:34:50 -0800 | [diff] [blame] | 311 | config.rtp.c_name = kCName; |
minyue | 10cbb46 | 2016-11-07 09:29:22 -0800 | [diff] [blame] | 312 | config.min_bitrate_bps = 12000; |
| 313 | config.max_bitrate_bps = 34000; |
ossu | 20a4b3f | 2017-04-27 02:08:52 -0700 | [diff] [blame] | 314 | config.send_codec_spec = |
Oskar Sundbom | 2707fb2 | 2017-11-16 10:57:35 +0100 | [diff] [blame] | 315 | AudioSendStream::Config::SendCodecSpec(kIsacPayloadType, kIsacFormat); |
ossu | 20a4b3f | 2017-04-27 02:08:52 -0700 | [diff] [blame] | 316 | config.send_codec_spec->nack_enabled = true; |
| 317 | config.send_codec_spec->transport_cc_enabled = false; |
Oskar Sundbom | 2707fb2 | 2017-11-16 10:57:35 +0100 | [diff] [blame] | 318 | config.send_codec_spec->cng_payload_type = 42; |
ossu | 20a4b3f | 2017-04-27 02:08:52 -0700 | [diff] [blame] | 319 | config.encoder_factory = MockAudioEncoderFactory::CreateUnusedFactory(); |
Johannes Kron | 9190b82 | 2018-10-29 11:22:05 +0100 | [diff] [blame] | 320 | config.rtp.extmap_allow_mixed = true; |
stefan | b521aa7 | 2016-11-01 03:17:16 -0700 | [diff] [blame] | 321 | config.rtp.extensions.push_back( |
| 322 | RtpExtension(RtpExtension::kAudioLevelUri, kAudioLevelId)); |
Jiawei Ou | 5571812 | 2018-11-09 13:17:39 -0800 | [diff] [blame] | 323 | config.rtcp_report_interval_ms = 2500; |
Fredrik Solenberg | 0ccae13 | 2015-11-03 10:15:49 +0100 | [diff] [blame] | 324 | EXPECT_EQ( |
Johannes Kron | 9190b82 | 2018-10-29 11:22:05 +0100 | [diff] [blame] | 325 | "{rtp: {ssrc: 1234, extmap-allow-mixed: true, extensions: [{uri: " |
Fredrik Solenberg | c69a56e | 2018-11-21 09:21:23 +0100 | [diff] [blame] | 326 | "urn:ietf:params:rtp-hdrext:ssrc-audio-level, id: 2}], " |
| 327 | "c_name: foo_name}, rtcp_report_interval_ms: 2500, " |
Jiawei Ou | 5571812 | 2018-11-09 13:17:39 -0800 | [diff] [blame] | 328 | "send_transport: null, media_transport: null, " |
Fredrik Solenberg | 8f5787a | 2018-01-11 13:52:30 +0100 | [diff] [blame] | 329 | "min_bitrate_bps: 12000, max_bitrate_bps: 34000, " |
solenberg | 940b6d6 | 2016-10-25 11:19:07 -0700 | [diff] [blame] | 330 | "send_codec_spec: {nack_enabled: true, transport_cc_enabled: false, " |
ossu | 20a4b3f | 2017-04-27 02:08:52 -0700 | [diff] [blame] | 331 | "cng_payload_type: 42, payload_type: 103, " |
| 332 | "format: {name: isac, clockrate_hz: 16000, num_channels: 1, " |
| 333 | "parameters: {}}}}", |
solenberg | 85a0496 | 2015-10-27 03:35:21 -0700 | [diff] [blame] | 334 | config.ToString()); |
solenberg | c7a8b08 | 2015-10-16 14:35:07 -0700 | [diff] [blame] | 335 | } |
| 336 | |
| 337 | TEST(AudioSendStreamTest, ConstructDestruct) { |
ossu | 20a4b3f | 2017-04-27 02:08:52 -0700 | [diff] [blame] | 338 | ConfigHelper helper(false, true); |
Fredrik Solenberg | 8f5787a | 2018-01-11 13:52:30 +0100 | [diff] [blame] | 339 | auto send_stream = helper.CreateAudioSendStream(); |
solenberg | c7a8b08 | 2015-10-16 14:35:07 -0700 | [diff] [blame] | 340 | } |
solenberg | 85a0496 | 2015-10-27 03:35:21 -0700 | [diff] [blame] | 341 | |
Fredrik Solenberg | b572768 | 2015-12-04 15:22:19 +0100 | [diff] [blame] | 342 | TEST(AudioSendStreamTest, SendTelephoneEvent) { |
ossu | 20a4b3f | 2017-04-27 02:08:52 -0700 | [diff] [blame] | 343 | ConfigHelper helper(false, true); |
Fredrik Solenberg | 8f5787a | 2018-01-11 13:52:30 +0100 | [diff] [blame] | 344 | auto send_stream = helper.CreateAudioSendStream(); |
Fredrik Solenberg | b572768 | 2015-12-04 15:22:19 +0100 | [diff] [blame] | 345 | helper.SetupMockForSendTelephoneEvent(); |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame] | 346 | EXPECT_TRUE(send_stream->SendTelephoneEvent( |
| 347 | kTelephoneEventPayloadType, kTelephoneEventPayloadFrequency, |
| 348 | kTelephoneEventCode, kTelephoneEventDuration)); |
Fredrik Solenberg | b572768 | 2015-12-04 15:22:19 +0100 | [diff] [blame] | 349 | } |
| 350 | |
solenberg | 9421853 | 2016-06-16 10:53:22 -0700 | [diff] [blame] | 351 | TEST(AudioSendStreamTest, SetMuted) { |
ossu | 20a4b3f | 2017-04-27 02:08:52 -0700 | [diff] [blame] | 352 | ConfigHelper helper(false, true); |
Fredrik Solenberg | 8f5787a | 2018-01-11 13:52:30 +0100 | [diff] [blame] | 353 | auto send_stream = helper.CreateAudioSendStream(); |
Niels Möller | dced9f6 | 2018-11-19 10:27:07 +0100 | [diff] [blame] | 354 | EXPECT_CALL(*helper.channel_send(), SetInputMute(true)); |
Fredrik Solenberg | 8f5787a | 2018-01-11 13:52:30 +0100 | [diff] [blame] | 355 | send_stream->SetMuted(true); |
solenberg | 9421853 | 2016-06-16 10:53:22 -0700 | [diff] [blame] | 356 | } |
| 357 | |
stefan | 7de8d64 | 2017-02-07 07:14:08 -0800 | [diff] [blame] | 358 | TEST(AudioSendStreamTest, AudioBweCorrectObjectsOnChannelProxy) { |
ossu | 20a4b3f | 2017-04-27 02:08:52 -0700 | [diff] [blame] | 359 | ConfigHelper helper(true, true); |
Fredrik Solenberg | 8f5787a | 2018-01-11 13:52:30 +0100 | [diff] [blame] | 360 | auto send_stream = helper.CreateAudioSendStream(); |
stefan | 7de8d64 | 2017-02-07 07:14:08 -0800 | [diff] [blame] | 361 | } |
| 362 | |
| 363 | TEST(AudioSendStreamTest, NoAudioBweCorrectObjectsOnChannelProxy) { |
ossu | 20a4b3f | 2017-04-27 02:08:52 -0700 | [diff] [blame] | 364 | ConfigHelper helper(false, true); |
Fredrik Solenberg | 8f5787a | 2018-01-11 13:52:30 +0100 | [diff] [blame] | 365 | auto send_stream = helper.CreateAudioSendStream(); |
stefan | 7de8d64 | 2017-02-07 07:14:08 -0800 | [diff] [blame] | 366 | } |
| 367 | |
solenberg | 85a0496 | 2015-10-27 03:35:21 -0700 | [diff] [blame] | 368 | TEST(AudioSendStreamTest, GetStats) { |
ossu | 20a4b3f | 2017-04-27 02:08:52 -0700 | [diff] [blame] | 369 | ConfigHelper helper(false, true); |
Fredrik Solenberg | 8f5787a | 2018-01-11 13:52:30 +0100 | [diff] [blame] | 370 | auto send_stream = helper.CreateAudioSendStream(); |
solenberg | 566ef24 | 2015-11-06 15:34:49 -0800 | [diff] [blame] | 371 | helper.SetupMockForGetStats(); |
Fredrik Solenberg | 8f5787a | 2018-01-11 13:52:30 +0100 | [diff] [blame] | 372 | AudioSendStream::Stats stats = send_stream->GetStats(true); |
Fredrik Solenberg | 0ccae13 | 2015-11-03 10:15:49 +0100 | [diff] [blame] | 373 | EXPECT_EQ(kSsrc, stats.local_ssrc); |
| 374 | EXPECT_EQ(static_cast<int64_t>(kCallStats.bytesSent), stats.bytes_sent); |
| 375 | EXPECT_EQ(kCallStats.packetsSent, stats.packets_sent); |
Sebastian Jansson | 9701e0c | 2018-08-09 11:21:11 +0200 | [diff] [blame] | 376 | EXPECT_EQ(kReportBlock.cumulative_num_packets_lost, stats.packets_lost); |
Fredrik Solenberg | 0ccae13 | 2015-11-03 10:15:49 +0100 | [diff] [blame] | 377 | EXPECT_EQ(Q8ToFloat(kReportBlock.fraction_lost), stats.fraction_lost); |
Fredrik Solenberg | f693bfa | 2018-12-11 12:22:10 +0100 | [diff] [blame] | 378 | EXPECT_EQ(kIsacFormat.name, stats.codec_name); |
Fredrik Solenberg | 0ccae13 | 2015-11-03 10:15:49 +0100 | [diff] [blame] | 379 | EXPECT_EQ(static_cast<int32_t>(kReportBlock.extended_highest_sequence_number), |
solenberg | 85a0496 | 2015-10-27 03:35:21 -0700 | [diff] [blame] | 380 | stats.ext_seqnum); |
Fredrik Solenberg | 0ccae13 | 2015-11-03 10:15:49 +0100 | [diff] [blame] | 381 | EXPECT_EQ(static_cast<int32_t>(kReportBlock.interarrival_jitter / |
Fredrik Solenberg | f693bfa | 2018-12-11 12:22:10 +0100 | [diff] [blame] | 382 | (kIsacFormat.clockrate_hz / 1000)), |
Fredrik Solenberg | 0ccae13 | 2015-11-03 10:15:49 +0100 | [diff] [blame] | 383 | stats.jitter_ms); |
| 384 | EXPECT_EQ(kCallStats.rttMs, stats.rtt_ms); |
Fredrik Solenberg | 2a87797 | 2017-12-15 16:42:15 +0100 | [diff] [blame] | 385 | EXPECT_EQ(0, stats.audio_level); |
| 386 | EXPECT_EQ(0, stats.total_input_energy); |
| 387 | EXPECT_EQ(0, stats.total_input_duration); |
Ivo Creusen | 56d4609 | 2017-11-24 17:29:59 +0100 | [diff] [blame] | 388 | EXPECT_EQ(kEchoDelayMedian, stats.apm_statistics.delay_median_ms); |
| 389 | EXPECT_EQ(kEchoDelayStdDev, stats.apm_statistics.delay_standard_deviation_ms); |
| 390 | EXPECT_EQ(kEchoReturnLoss, stats.apm_statistics.echo_return_loss); |
| 391 | EXPECT_EQ(kEchoReturnLossEnhancement, |
| 392 | stats.apm_statistics.echo_return_loss_enhancement); |
| 393 | EXPECT_EQ(kDivergentFilterFraction, |
| 394 | stats.apm_statistics.divergent_filter_fraction); |
| 395 | EXPECT_EQ(kResidualEchoLikelihood, |
| 396 | stats.apm_statistics.residual_echo_likelihood); |
| 397 | EXPECT_EQ(kResidualEchoLikelihoodMax, |
| 398 | stats.apm_statistics.residual_echo_likelihood_recent_max); |
Fredrik Solenberg | 2a87797 | 2017-12-15 16:42:15 +0100 | [diff] [blame] | 399 | EXPECT_FALSE(stats.typing_noise_detected); |
solenberg | 566ef24 | 2015-11-06 15:34:49 -0800 | [diff] [blame] | 400 | } |
minyue | 7a97344 | 2016-10-20 03:27:12 -0700 | [diff] [blame] | 401 | |
minyue-webrtc | 8de1826 | 2017-07-26 14:18:40 +0200 | [diff] [blame] | 402 | TEST(AudioSendStreamTest, SendCodecAppliesAudioNetworkAdaptor) { |
ossu | 20a4b3f | 2017-04-27 02:08:52 -0700 | [diff] [blame] | 403 | ConfigHelper helper(false, true); |
Fredrik Solenberg | 8f5787a | 2018-01-11 13:52:30 +0100 | [diff] [blame] | 404 | helper.config().send_codec_spec = |
Oskar Sundbom | 2707fb2 | 2017-11-16 10:57:35 +0100 | [diff] [blame] | 405 | AudioSendStream::Config::SendCodecSpec(0, kOpusFormat); |
minyue-webrtc | 8de1826 | 2017-07-26 14:18:40 +0200 | [diff] [blame] | 406 | const std::string kAnaConfigString = "abcde"; |
| 407 | const std::string kAnaReconfigString = "12345"; |
| 408 | |
Fredrik Solenberg | 8f5787a | 2018-01-11 13:52:30 +0100 | [diff] [blame] | 409 | helper.config().audio_network_adaptor_config = kAnaConfigString; |
ossu | 20a4b3f | 2017-04-27 02:08:52 -0700 | [diff] [blame] | 410 | |
Karl Wiberg | d6fbf2a | 2018-02-27 13:37:31 +0100 | [diff] [blame] | 411 | EXPECT_CALL(helper.mock_encoder_factory(), MakeAudioEncoderMock(_, _, _, _)) |
minyue-webrtc | 8de1826 | 2017-07-26 14:18:40 +0200 | [diff] [blame] | 412 | .WillOnce(Invoke([&kAnaConfigString, &kAnaReconfigString]( |
| 413 | int payload_type, const SdpAudioFormat& format, |
Danil Chapovalov | b9b146c | 2018-06-15 12:28:07 +0200 | [diff] [blame] | 414 | absl::optional<AudioCodecPairId> codec_pair_id, |
minyue-webrtc | 8de1826 | 2017-07-26 14:18:40 +0200 | [diff] [blame] | 415 | std::unique_ptr<AudioEncoder>* return_value) { |
ossu | 20a4b3f | 2017-04-27 02:08:52 -0700 | [diff] [blame] | 416 | auto mock_encoder = SetupAudioEncoderMock(payload_type, format); |
minyue-webrtc | 8de1826 | 2017-07-26 14:18:40 +0200 | [diff] [blame] | 417 | EXPECT_CALL(*mock_encoder, |
| 418 | EnableAudioNetworkAdaptor(StrEq(kAnaConfigString), _)) |
| 419 | .WillOnce(Return(true)); |
| 420 | EXPECT_CALL(*mock_encoder, |
| 421 | EnableAudioNetworkAdaptor(StrEq(kAnaReconfigString), _)) |
ossu | 20a4b3f | 2017-04-27 02:08:52 -0700 | [diff] [blame] | 422 | .WillOnce(Return(true)); |
| 423 | *return_value = std::move(mock_encoder); |
| 424 | })); |
| 425 | |
Fredrik Solenberg | 8f5787a | 2018-01-11 13:52:30 +0100 | [diff] [blame] | 426 | auto send_stream = helper.CreateAudioSendStream(); |
minyue-webrtc | 8de1826 | 2017-07-26 14:18:40 +0200 | [diff] [blame] | 427 | |
Fredrik Solenberg | 8f5787a | 2018-01-11 13:52:30 +0100 | [diff] [blame] | 428 | auto stream_config = helper.config(); |
Oskar Sundbom | 2707fb2 | 2017-11-16 10:57:35 +0100 | [diff] [blame] | 429 | stream_config.audio_network_adaptor_config = kAnaReconfigString; |
minyue-webrtc | 8de1826 | 2017-07-26 14:18:40 +0200 | [diff] [blame] | 430 | |
| 431 | helper.SetupMockForModifyEncoder(); |
Fredrik Solenberg | 8f5787a | 2018-01-11 13:52:30 +0100 | [diff] [blame] | 432 | send_stream->Reconfigure(stream_config); |
minyue | 7a97344 | 2016-10-20 03:27:12 -0700 | [diff] [blame] | 433 | } |
| 434 | |
| 435 | // VAD is applied when codec is mono and the CNG frequency matches the codec |
ossu | 20a4b3f | 2017-04-27 02:08:52 -0700 | [diff] [blame] | 436 | // clock rate. |
minyue | 7a97344 | 2016-10-20 03:27:12 -0700 | [diff] [blame] | 437 | TEST(AudioSendStreamTest, SendCodecCanApplyVad) { |
ossu | 20a4b3f | 2017-04-27 02:08:52 -0700 | [diff] [blame] | 438 | ConfigHelper helper(false, false); |
Fredrik Solenberg | 8f5787a | 2018-01-11 13:52:30 +0100 | [diff] [blame] | 439 | helper.config().send_codec_spec = |
Oskar Sundbom | 2707fb2 | 2017-11-16 10:57:35 +0100 | [diff] [blame] | 440 | AudioSendStream::Config::SendCodecSpec(9, kG722Format); |
Fredrik Solenberg | 8f5787a | 2018-01-11 13:52:30 +0100 | [diff] [blame] | 441 | helper.config().send_codec_spec->cng_payload_type = 105; |
ossu | 20a4b3f | 2017-04-27 02:08:52 -0700 | [diff] [blame] | 442 | using ::testing::Invoke; |
| 443 | std::unique_ptr<AudioEncoder> stolen_encoder; |
Niels Möller | dced9f6 | 2018-11-19 10:27:07 +0100 | [diff] [blame] | 444 | EXPECT_CALL(*helper.channel_send(), SetEncoderForMock(_, _)) |
ossu | 20a4b3f | 2017-04-27 02:08:52 -0700 | [diff] [blame] | 445 | .WillOnce( |
| 446 | Invoke([&stolen_encoder](int payload_type, |
| 447 | std::unique_ptr<AudioEncoder>* encoder) { |
| 448 | stolen_encoder = std::move(*encoder); |
| 449 | return true; |
| 450 | })); |
| 451 | |
Fredrik Solenberg | 8f5787a | 2018-01-11 13:52:30 +0100 | [diff] [blame] | 452 | auto send_stream = helper.CreateAudioSendStream(); |
ossu | 20a4b3f | 2017-04-27 02:08:52 -0700 | [diff] [blame] | 453 | |
| 454 | // We cannot truly determine if the encoder created is an AudioEncoderCng. It |
| 455 | // is the only reasonable implementation that will return something from |
| 456 | // ReclaimContainedEncoders, though. |
| 457 | ASSERT_TRUE(stolen_encoder); |
| 458 | EXPECT_FALSE(stolen_encoder->ReclaimContainedEncoders().empty()); |
minyue | 7a97344 | 2016-10-20 03:27:12 -0700 | [diff] [blame] | 459 | } |
| 460 | |
minyue | 78b4d56 | 2016-11-30 04:47:39 -0800 | [diff] [blame] | 461 | TEST(AudioSendStreamTest, DoesNotPassHigherBitrateThanMaxBitrate) { |
ossu | 20a4b3f | 2017-04-27 02:08:52 -0700 | [diff] [blame] | 462 | ConfigHelper helper(false, true); |
Fredrik Solenberg | 8f5787a | 2018-01-11 13:52:30 +0100 | [diff] [blame] | 463 | auto send_stream = helper.CreateAudioSendStream(); |
Niels Möller | dced9f6 | 2018-11-19 10:27:07 +0100 | [diff] [blame] | 464 | EXPECT_CALL(*helper.channel_send(), |
Sebastian Jansson | 254d869 | 2018-11-21 19:19:00 +0100 | [diff] [blame] | 465 | OnBitrateAllocation( |
| 466 | Field(&BitrateAllocationUpdate::target_bitrate, |
| 467 | Eq(DataRate::bps(helper.config().max_bitrate_bps))))); |
Sebastian Jansson | c0e4d45 | 2018-10-25 15:08:32 +0200 | [diff] [blame] | 468 | BitrateAllocationUpdate update; |
Sebastian Jansson | 13e5903 | 2018-11-21 19:13:07 +0100 | [diff] [blame] | 469 | update.target_bitrate = DataRate::bps(helper.config().max_bitrate_bps + 5000); |
| 470 | update.packet_loss_ratio = 0; |
| 471 | update.round_trip_time = TimeDelta::ms(50); |
| 472 | update.bwe_period = TimeDelta::ms(6000); |
Sebastian Jansson | c0e4d45 | 2018-10-25 15:08:32 +0200 | [diff] [blame] | 473 | send_stream->OnBitrateUpdated(update); |
minyue | 78b4d56 | 2016-11-30 04:47:39 -0800 | [diff] [blame] | 474 | } |
| 475 | |
| 476 | TEST(AudioSendStreamTest, ProbingIntervalOnBitrateUpdated) { |
ossu | 20a4b3f | 2017-04-27 02:08:52 -0700 | [diff] [blame] | 477 | ConfigHelper helper(false, true); |
Fredrik Solenberg | 8f5787a | 2018-01-11 13:52:30 +0100 | [diff] [blame] | 478 | auto send_stream = helper.CreateAudioSendStream(); |
Sebastian Jansson | 254d869 | 2018-11-21 19:19:00 +0100 | [diff] [blame] | 479 | |
| 480 | EXPECT_CALL(*helper.channel_send(), |
| 481 | OnBitrateAllocation(Field(&BitrateAllocationUpdate::bwe_period, |
| 482 | Eq(TimeDelta::ms(5000))))); |
Sebastian Jansson | c0e4d45 | 2018-10-25 15:08:32 +0200 | [diff] [blame] | 483 | BitrateAllocationUpdate update; |
Sebastian Jansson | 13e5903 | 2018-11-21 19:13:07 +0100 | [diff] [blame] | 484 | update.target_bitrate = DataRate::bps(helper.config().max_bitrate_bps + 5000); |
| 485 | update.packet_loss_ratio = 0; |
| 486 | update.round_trip_time = TimeDelta::ms(50); |
| 487 | update.bwe_period = TimeDelta::ms(5000); |
Sebastian Jansson | c0e4d45 | 2018-10-25 15:08:32 +0200 | [diff] [blame] | 488 | send_stream->OnBitrateUpdated(update); |
minyue | 78b4d56 | 2016-11-30 04:47:39 -0800 | [diff] [blame] | 489 | } |
| 490 | |
ossu | 20a4b3f | 2017-04-27 02:08:52 -0700 | [diff] [blame] | 491 | // Test that AudioSendStream doesn't recreate the encoder unnecessarily. |
| 492 | TEST(AudioSendStreamTest, DontRecreateEncoder) { |
| 493 | ConfigHelper helper(false, false); |
| 494 | // WillOnce is (currently) the default used by ConfigHelper if asked to set an |
| 495 | // expectation for SetEncoder. Since this behavior is essential for this test |
| 496 | // to be correct, it's instead set-up manually here. Otherwise a simple change |
| 497 | // to ConfigHelper (say to WillRepeatedly) would silently make this test |
| 498 | // useless. |
Niels Möller | dced9f6 | 2018-11-19 10:27:07 +0100 | [diff] [blame] | 499 | EXPECT_CALL(*helper.channel_send(), SetEncoderForMock(_, _)) |
ossu | 20a4b3f | 2017-04-27 02:08:52 -0700 | [diff] [blame] | 500 | .WillOnce(Return(true)); |
| 501 | |
Fredrik Solenberg | 8f5787a | 2018-01-11 13:52:30 +0100 | [diff] [blame] | 502 | helper.config().send_codec_spec = |
Oskar Sundbom | 2707fb2 | 2017-11-16 10:57:35 +0100 | [diff] [blame] | 503 | AudioSendStream::Config::SendCodecSpec(9, kG722Format); |
Fredrik Solenberg | 8f5787a | 2018-01-11 13:52:30 +0100 | [diff] [blame] | 504 | helper.config().send_codec_spec->cng_payload_type = 105; |
| 505 | auto send_stream = helper.CreateAudioSendStream(); |
| 506 | send_stream->Reconfigure(helper.config()); |
ossu | 20a4b3f | 2017-04-27 02:08:52 -0700 | [diff] [blame] | 507 | } |
| 508 | |
ossu | 1129df2 | 2017-06-30 01:38:56 -0700 | [diff] [blame] | 509 | TEST(AudioSendStreamTest, ReconfigureTransportCcResetsFirst) { |
| 510 | ConfigHelper helper(false, true); |
Fredrik Solenberg | 8f5787a | 2018-01-11 13:52:30 +0100 | [diff] [blame] | 511 | auto send_stream = helper.CreateAudioSendStream(); |
ossu | 1129df2 | 2017-06-30 01:38:56 -0700 | [diff] [blame] | 512 | auto new_config = helper.config(); |
| 513 | ConfigHelper::AddBweToConfig(&new_config); |
Niels Möller | dced9f6 | 2018-11-19 10:27:07 +0100 | [diff] [blame] | 514 | EXPECT_CALL(*helper.channel_send(), |
ossu | 1129df2 | 2017-06-30 01:38:56 -0700 | [diff] [blame] | 515 | EnableSendTransportSequenceNumber(kTransportSequenceNumberId)) |
| 516 | .Times(1); |
| 517 | { |
| 518 | ::testing::InSequence seq; |
Niels Möller | dced9f6 | 2018-11-19 10:27:07 +0100 | [diff] [blame] | 519 | EXPECT_CALL(*helper.channel_send(), ResetSenderCongestionControlObjects()) |
ossu | 1129df2 | 2017-06-30 01:38:56 -0700 | [diff] [blame] | 520 | .Times(1); |
Niels Möller | dced9f6 | 2018-11-19 10:27:07 +0100 | [diff] [blame] | 521 | EXPECT_CALL(*helper.channel_send(), RegisterSenderCongestionControlObjects( |
| 522 | helper.transport(), Ne(nullptr))) |
ossu | 1129df2 | 2017-06-30 01:38:56 -0700 | [diff] [blame] | 523 | .Times(1); |
| 524 | } |
Fredrik Solenberg | 8f5787a | 2018-01-11 13:52:30 +0100 | [diff] [blame] | 525 | send_stream->Reconfigure(new_config); |
ossu | 1129df2 | 2017-06-30 01:38:56 -0700 | [diff] [blame] | 526 | } |
Sam Zackrisson | 06953ba | 2018-02-01 16:53:16 +0100 | [diff] [blame] | 527 | |
Benjamin Wright | 78410ad | 2018-10-25 09:52:57 -0700 | [diff] [blame] | 528 | // Validates that reconfiguring the AudioSendStream with a Frame encryptor |
| 529 | // correctly reconfigures on the object without crashing. |
| 530 | TEST(AudioSendStreamTest, ReconfigureWithFrameEncryptor) { |
| 531 | ConfigHelper helper(false, true); |
| 532 | auto send_stream = helper.CreateAudioSendStream(); |
| 533 | auto new_config = helper.config(); |
| 534 | |
| 535 | rtc::scoped_refptr<FrameEncryptorInterface> mock_frame_encryptor_0( |
| 536 | new rtc::RefCountedObject<MockFrameEncryptor>()); |
| 537 | new_config.frame_encryptor = mock_frame_encryptor_0; |
Niels Möller | dced9f6 | 2018-11-19 10:27:07 +0100 | [diff] [blame] | 538 | EXPECT_CALL(*helper.channel_send(), SetFrameEncryptor(Ne(nullptr))).Times(1); |
Benjamin Wright | 78410ad | 2018-10-25 09:52:57 -0700 | [diff] [blame] | 539 | send_stream->Reconfigure(new_config); |
| 540 | |
| 541 | // Not updating the frame encryptor shouldn't force it to reconfigure. |
Niels Möller | dced9f6 | 2018-11-19 10:27:07 +0100 | [diff] [blame] | 542 | EXPECT_CALL(*helper.channel_send(), SetFrameEncryptor(_)).Times(0); |
Benjamin Wright | 78410ad | 2018-10-25 09:52:57 -0700 | [diff] [blame] | 543 | send_stream->Reconfigure(new_config); |
| 544 | |
| 545 | // Updating frame encryptor to a new object should force a call to the proxy. |
| 546 | rtc::scoped_refptr<FrameEncryptorInterface> mock_frame_encryptor_1( |
| 547 | new rtc::RefCountedObject<MockFrameEncryptor>()); |
| 548 | new_config.frame_encryptor = mock_frame_encryptor_1; |
| 549 | new_config.crypto_options.sframe.require_frame_encryption = true; |
Niels Möller | dced9f6 | 2018-11-19 10:27:07 +0100 | [diff] [blame] | 550 | EXPECT_CALL(*helper.channel_send(), SetFrameEncryptor(Ne(nullptr))).Times(1); |
Benjamin Wright | 78410ad | 2018-10-25 09:52:57 -0700 | [diff] [blame] | 551 | send_stream->Reconfigure(new_config); |
| 552 | } |
solenberg | 85a0496 | 2015-10-27 03:35:21 -0700 | [diff] [blame] | 553 | } // namespace test |
solenberg | c7a8b08 | 2015-10-16 14:35:07 -0700 | [diff] [blame] | 554 | } // namespace webrtc |