blob: 948fcfed72c4f751e8952b2af13f621654c2c505 [file] [log] [blame]
solenbergc7a8b082015-10-16 21:35:071/*
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
Jonas Olssona4d87372019-07-05 17:08:3311#include "audio/audio_send_stream.h"
12
Mirko Bonadei317a1f02019-09-17 15:06:1813#include <memory>
Fredrik Solenbergea073732015-12-01 10:26:3414#include <string>
Yves Gerey17048012019-07-26 15:49:5215#include <thread>
ossu20a4b3f2017-04-27 09:08:5216#include <utility>
Fredrik Solenbergea073732015-12-01 10:26:3417#include <vector>
18
Danil Chapovalov31660fd2019-03-22 11:59:4819#include "api/task_queue/default_task_queue_factory.h"
Benjamin Wright78410ad2018-10-25 16:52:5720#include "api/test/mock_frame_encryptor.h"
Mirko Bonadei92ea95e2017-09-15 04:47:3121#include "audio/audio_state.h"
22#include "audio/conversion.h"
Fredrik Solenberga8b7c7f2018-01-17 10:18:3123#include "audio/mock_voe_channel_proxy.h"
Sebastian Janssonef9daee2018-02-22 13:49:0224#include "call/test/mock_rtp_transport_controller_send.h"
Mirko Bonadei92ea95e2017-09-15 04:47:3125#include "logging/rtc_event_log/mock/mock_rtc_event_log.h"
Fredrik Solenberg2a877972017-12-15 15:42:1526#include "modules/audio_device/include/mock_audio_device.h"
Mirko Bonadei92ea95e2017-09-15 04:47:3127#include "modules/audio_mixer/audio_mixer_impl.h"
Henrik Boströmd2c336f2019-07-03 15:11:1028#include "modules/audio_mixer/sine_wave_generator.h"
Ivo Creusen56d460902017-11-24 16:29:5929#include "modules/audio_processing/include/audio_processing_statistics.h"
Mirko Bonadei92ea95e2017-09-15 04:47:3130#include "modules/audio_processing/include/mock_audio_processing.h"
Sebastian Janssonef9daee2018-02-22 13:49:0231#include "modules/rtp_rtcp/mocks/mock_rtcp_bandwidth_observer.h"
Mirko Bonadei92ea95e2017-09-15 04:47:3132#include "modules/rtp_rtcp/mocks/mock_rtcp_rtt_stats.h"
33#include "modules/rtp_rtcp/mocks/mock_rtp_rtcp.h"
Danil Chapovalov31660fd2019-03-22 11:59:4834#include "rtc_base/task_queue_for_test.h"
Sebastian Janssonda6806c2019-03-04 16:05:1235#include "system_wrappers/include/clock.h"
Per Kjellander914351d2019-02-15 09:54:5536#include "test/field_trial.h"
Mirko Bonadei92ea95e2017-09-15 04:47:3137#include "test/gtest.h"
38#include "test/mock_audio_encoder.h"
39#include "test/mock_audio_encoder_factory.h"
solenbergc7a8b082015-10-16 21:35:0740
41namespace webrtc {
solenberg85a04962015-10-27 10:35:2142namespace test {
Fredrik Solenberg0ccae132015-11-03 09:15:4943namespace {
44
Mirko Bonadei6a489f22019-04-09 13:11:1245using ::testing::_;
Henrik Boströmd2c336f2019-07-03 15:11:1046using ::testing::AnyNumber;
Mirko Bonadei6a489f22019-04-09 13:11:1247using ::testing::Eq;
48using ::testing::Field;
49using ::testing::Invoke;
50using ::testing::Ne;
51using ::testing::Return;
52using ::testing::StrEq;
solenberg3a941542015-11-16 15:34:5053
Henrik Boströmd2c336f2019-07-03 15:11:1054static const float kTolerance = 0.0001f;
55
Fredrik Solenberg0ccae132015-11-03 09:15:4956const uint32_t kSsrc = 1234;
solenberg3a941542015-11-16 15:34:5057const char* kCName = "foo_name";
58const int kAudioLevelId = 2;
Stefan Holmerb86d4e42015-12-07 09:26:1859const int kTransportSequenceNumberId = 4;
Ivo Creusen56d460902017-11-24 16:29:5960const int32_t kEchoDelayMedian = 254;
61const int32_t kEchoDelayStdDev = -3;
62const double kDivergentFilterFraction = 0.2f;
63const double kEchoReturnLoss = -65;
64const double kEchoReturnLossEnhancement = 101;
65const double kResidualEchoLikelihood = -1.0f;
66const double kResidualEchoLikelihoodMax = 23.0f;
Niels Möller530ead42018-10-04 12:28:3967const CallSendStatistics kCallStats = {112, 13456, 17890};
solenberg566ef242015-11-06 23:34:4968const ReportBlock kReportBlock = {456, 780, 123, 567, 890, 132, 143, 13354};
Fredrik Solenbergb5727682015-12-04 14:22:1969const int kTelephoneEventPayloadType = 123;
solenbergffbbcac2016-11-17 13:25:3770const int kTelephoneEventPayloadFrequency = 65432;
solenberg8842c3e2016-03-11 11:06:4171const int kTelephoneEventCode = 45;
72const int kTelephoneEventDuration = 6789;
ossu20a4b3f2017-04-27 09:08:5273constexpr int kIsacPayloadType = 103;
74const SdpAudioFormat kIsacFormat = {"isac", 16000, 1};
75const SdpAudioFormat kOpusFormat = {"opus", 48000, 2};
76const SdpAudioFormat kG722Format = {"g722", 8000, 1};
77const AudioCodecSpec kCodecSpecs[] = {
78 {kIsacFormat, {16000, 1, 32000, 10000, 32000}},
79 {kOpusFormat, {48000, 1, 32000, 6000, 510000}},
80 {kG722Format, {16000, 1, 64000}}};
solenberg566ef242015-11-06 23:34:4981
Daniel Lee93562522019-05-03 12:40:1382// TODO(dklee): This mirrors calculation in audio_send_stream.cc, which
83// should be made more precise in the future. This can be changed when that
84// logic is more accurate.
85const DataSize kOverheadPerPacket = DataSize::bytes(20 + 8 + 10 + 12);
86const TimeDelta kMaxFrameLength = TimeDelta::ms(60);
87const DataRate kOverheadRate = kOverheadPerPacket / kMaxFrameLength;
88
mflodman86cc6ff2016-07-26 11:44:0689class MockLimitObserver : public BitrateAllocator::LimitObserver {
90 public:
Sebastian Jansson93b1ea22019-09-18 16:31:5291 MOCK_METHOD1(OnAllocationLimitsChanged, void(BitrateAllocationLimits));
mflodman86cc6ff2016-07-26 11:44:0692};
93
ossu20a4b3f2017-04-27 09:08:5294std::unique_ptr<MockAudioEncoder> SetupAudioEncoderMock(
95 int payload_type,
96 const SdpAudioFormat& format) {
97 for (const auto& spec : kCodecSpecs) {
98 if (format == spec.format) {
Sebastian Jansson41f16be2018-02-22 10:09:5699 std::unique_ptr<MockAudioEncoder> encoder(
Mirko Bonadei6a489f22019-04-09 13:11:12100 new ::testing::NiceMock<MockAudioEncoder>());
ossu20a4b3f2017-04-27 09:08:52101 ON_CALL(*encoder.get(), SampleRateHz())
102 .WillByDefault(Return(spec.info.sample_rate_hz));
103 ON_CALL(*encoder.get(), NumChannels())
104 .WillByDefault(Return(spec.info.num_channels));
105 ON_CALL(*encoder.get(), RtpTimestampRateHz())
106 .WillByDefault(Return(spec.format.clockrate_hz));
107 return encoder;
108 }
109 }
110 return nullptr;
111}
112
113rtc::scoped_refptr<MockAudioEncoderFactory> SetupEncoderFactoryMock() {
114 rtc::scoped_refptr<MockAudioEncoderFactory> factory =
115 new rtc::RefCountedObject<MockAudioEncoderFactory>();
116 ON_CALL(*factory.get(), GetSupportedEncoders())
117 .WillByDefault(Return(std::vector<AudioCodecSpec>(
118 std::begin(kCodecSpecs), std::end(kCodecSpecs))));
119 ON_CALL(*factory.get(), QueryAudioEncoder(_))
Oskar Sundbom2707fb22017-11-16 09:57:35120 .WillByDefault(Invoke(
Danil Chapovalovb9b146c2018-06-15 10:28:07121 [](const SdpAudioFormat& format) -> absl::optional<AudioCodecInfo> {
Oskar Sundbom2707fb22017-11-16 09:57:35122 for (const auto& spec : kCodecSpecs) {
123 if (format == spec.format) {
124 return spec.info;
125 }
126 }
Danil Chapovalovb9b146c2018-06-15 10:28:07127 return absl::nullopt;
Oskar Sundbom2707fb22017-11-16 09:57:35128 }));
Karl Wibergd6fbf2a2018-02-27 12:37:31129 ON_CALL(*factory.get(), MakeAudioEncoderMock(_, _, _, _))
ossu20a4b3f2017-04-27 09:08:52130 .WillByDefault(Invoke([](int payload_type, const SdpAudioFormat& format,
Danil Chapovalovb9b146c2018-06-15 10:28:07131 absl::optional<AudioCodecPairId> codec_pair_id,
ossu20a4b3f2017-04-27 09:08:52132 std::unique_ptr<AudioEncoder>* return_value) {
133 *return_value = SetupAudioEncoderMock(payload_type, format);
134 }));
135 return factory;
136}
137
solenberg566ef242015-11-06 23:34:49138struct ConfigHelper {
ossu20a4b3f2017-04-27 09:08:52139 ConfigHelper(bool audio_bwe_enabled, bool expect_set_encoder_call)
Sebastian Janssonda6806c2019-03-04 16:05:12140 : clock_(1000000),
Danil Chapovalov31660fd2019-03-22 11:59:48141 task_queue_factory_(CreateDefaultTaskQueueFactory()),
Anton Sukhanov4f08faa2019-05-21 18:12:57142 stream_config_(/*send_transport=*/nullptr, MediaTransportConfig()),
peaha9cc40b2017-06-29 15:32:09143 audio_processing_(new rtc::RefCountedObject<MockAudioProcessing>()),
Sebastian Jansson40de3cc2019-09-19 12:54:43144 bitrate_allocator_(&limit_observer_),
Danil Chapovalov31660fd2019-03-22 11:59:48145 worker_queue_(task_queue_factory_->CreateTaskQueue(
146 "ConfigHelper_worker_queue",
147 TaskQueueFactory::Priority::NORMAL)),
minyue-webrtc8de18262017-07-26 12:18:40148 audio_encoder_(nullptr) {
Mirko Bonadei6a489f22019-04-09 13:11:12149 using ::testing::Invoke;
solenberg3a941542015-11-16 15:34:50150
solenberg566ef242015-11-06 23:34:49151 AudioState::Config config;
aleloi10111bc2016-11-17 14:48:48152 config.audio_mixer = AudioMixerImpl::Create();
peaha9cc40b2017-06-29 15:32:09153 config.audio_processing = audio_processing_;
Fredrik Solenberg2a877972017-12-15 15:42:15154 config.audio_device_module =
155 new rtc::RefCountedObject<MockAudioDeviceModule>();
solenberg566ef242015-11-06 23:34:49156 audio_state_ = AudioState::Create(config);
solenberg3a941542015-11-16 15:34:50157
Niels Möllerdced9f62018-11-19 09:27:07158 SetupDefaultChannelSend(audio_bwe_enabled);
ossu20a4b3f2017-04-27 09:08:52159 SetupMockForSetupSendCodec(expect_set_encoder_call);
minyue6b825df2016-10-31 11:08:32160
Fredrik Solenberg8f5787a2018-01-11 12:52:30161 // Use ISAC as default codec so as to prevent unnecessary |channel_proxy_|
ossu20a4b3f2017-04-27 09:08:52162 // calls from the default ctor behavior.
163 stream_config_.send_codec_spec =
Oskar Sundbom2707fb22017-11-16 09:57:35164 AudioSendStream::Config::SendCodecSpec(kIsacPayloadType, kIsacFormat);
solenberg566ef242015-11-06 23:34:49165 stream_config_.rtp.ssrc = kSsrc;
solenberg3a941542015-11-16 15:34:50166 stream_config_.rtp.c_name = kCName;
167 stream_config_.rtp.extensions.push_back(
isheriff6f8d6862016-05-26 18:24:55168 RtpExtension(RtpExtension::kAudioLevelUri, kAudioLevelId));
stefan7de8d642017-02-07 15:14:08169 if (audio_bwe_enabled) {
ossu1129df22017-06-30 08:38:56170 AddBweToConfig(&stream_config_);
stefan7de8d642017-02-07 15:14:08171 }
ossu20a4b3f2017-04-27 09:08:52172 stream_config_.encoder_factory = SetupEncoderFactoryMock();
minyue78b4d562016-11-30 12:47:39173 stream_config_.min_bitrate_bps = 10000;
174 stream_config_.max_bitrate_bps = 65000;
solenberg566ef242015-11-06 23:34:49175 }
176
Fredrik Solenberg8f5787a2018-01-11 12:52:30177 std::unique_ptr<internal::AudioSendStream> CreateAudioSendStream() {
Sebastian Jansson0b698262019-03-07 08:17:19178 EXPECT_CALL(rtp_transport_, GetWorkerQueue())
179 .WillRepeatedly(Return(&worker_queue_));
Fredrik Solenberg8f5787a2018-01-11 12:52:30180 return std::unique_ptr<internal::AudioSendStream>(
181 new internal::AudioSendStream(
Sebastian Jansson977b3352019-03-04 16:43:34182 Clock::GetRealTimeClock(), stream_config_, audio_state_,
Danil Chapovalov31660fd2019-03-22 11:59:48183 task_queue_factory_.get(), &rtp_transport_, &bitrate_allocator_,
Sebastian Jansson44dd9f22019-03-08 13:50:30184 &event_log_, &rtcp_rtt_stats_, absl::nullopt,
Niels Möllerdced9f62018-11-19 09:27:07185 std::unique_ptr<voe::ChannelSendInterface>(channel_send_)));
Fredrik Solenberg8f5787a2018-01-11 12:52:30186 }
187
solenberg566ef242015-11-06 23:34:49188 AudioSendStream::Config& config() { return stream_config_; }
ossu20a4b3f2017-04-27 09:08:52189 MockAudioEncoderFactory& mock_encoder_factory() {
190 return *static_cast<MockAudioEncoderFactory*>(
191 stream_config_.encoder_factory.get());
192 }
Niels Möllerdced9f62018-11-19 09:27:07193 MockChannelSend* channel_send() { return channel_send_; }
Sebastian Jansson1896cec2018-02-20 08:06:11194 RtpTransportControllerSendInterface* transport() { return &rtp_transport_; }
minyue7a973442016-10-20 10:27:12195
ossu1129df22017-06-30 08:38:56196 static void AddBweToConfig(AudioSendStream::Config* config) {
Yves Gerey665174f2018-06-19 13:03:05197 config->rtp.extensions.push_back(RtpExtension(
198 RtpExtension::kTransportSequenceNumberUri, kTransportSequenceNumberId));
ossu1129df22017-06-30 08:38:56199 config->send_codec_spec->transport_cc_enabled = true;
200 }
201
Niels Möllerdced9f62018-11-19 09:27:07202 void SetupDefaultChannelSend(bool audio_bwe_enabled) {
203 EXPECT_TRUE(channel_send_ == nullptr);
Mirko Bonadei6a489f22019-04-09 13:11:12204 channel_send_ = new ::testing::StrictMock<MockChannelSend>();
Niels Möllerdced9f62018-11-19 09:27:07205 EXPECT_CALL(*channel_send_, GetRtpRtcp()).WillRepeatedly(Invoke([this]() {
Niels Möller848d6d32018-08-08 08:49:16206 return &this->rtp_rtcp_;
207 }));
Erik Språng70efdde2019-08-21 11:36:20208 EXPECT_CALL(rtp_rtcp_, SSRC).WillRepeatedly(Return(kSsrc));
Niels Möllerdced9f62018-11-19 09:27:07209 EXPECT_CALL(*channel_send_, SetRTCP_CNAME(StrEq(kCName))).Times(1);
Niels Möllerdced9f62018-11-19 09:27:07210 EXPECT_CALL(*channel_send_, SetFrameEncryptor(_)).Times(1);
211 EXPECT_CALL(*channel_send_, SetExtmapAllowMixed(false)).Times(1);
212 EXPECT_CALL(*channel_send_,
minyue6b825df2016-10-31 11:08:32213 SetSendAudioLevelIndicationStatus(true, kAudioLevelId))
214 .Times(1);
Sebastian Janssonef9daee2018-02-22 13:49:02215 EXPECT_CALL(rtp_transport_, GetBandwidthObserver())
216 .WillRepeatedly(Return(&bandwidth_observer_));
stefan7de8d642017-02-07 15:14:08217 if (audio_bwe_enabled) {
Niels Möllerdced9f62018-11-19 09:27:07218 EXPECT_CALL(*channel_send_,
stefan7de8d642017-02-07 15:14:08219 EnableSendTransportSequenceNumber(kTransportSequenceNumberId))
220 .Times(1);
Niels Möllerdced9f62018-11-19 09:27:07221 EXPECT_CALL(*channel_send_,
Sebastian Janssonef9daee2018-02-22 13:49:02222 RegisterSenderCongestionControlObjects(
223 &rtp_transport_, Eq(&bandwidth_observer_)))
stefan7de8d642017-02-07 15:14:08224 .Times(1);
225 } else {
Niels Möllerdced9f62018-11-19 09:27:07226 EXPECT_CALL(*channel_send_, RegisterSenderCongestionControlObjects(
227 &rtp_transport_, Eq(nullptr)))
stefan7de8d642017-02-07 15:14:08228 .Times(1);
229 }
Niels Möllerdced9f62018-11-19 09:27:07230 EXPECT_CALL(*channel_send_, ResetSenderCongestionControlObjects()).Times(1);
Amit Hilbuch77938e62018-12-21 17:23:38231 EXPECT_CALL(*channel_send_, SetRid(std::string(), 0, 0)).Times(1);
minyue6b825df2016-10-31 11:08:32232 }
233
ossu20a4b3f2017-04-27 09:08:52234 void SetupMockForSetupSendCodec(bool expect_set_encoder_call) {
235 if (expect_set_encoder_call) {
Niels Möllerdced9f62018-11-19 09:27:07236 EXPECT_CALL(*channel_send_, SetEncoderForMock(_, _))
minyue-webrtc8de18262017-07-26 12:18:40237 .WillOnce(Invoke(
238 [this](int payload_type, std::unique_ptr<AudioEncoder>* encoder) {
239 this->audio_encoder_ = std::move(*encoder);
240 return true;
241 }));
ossu20a4b3f2017-04-27 09:08:52242 }
minyue7a973442016-10-20 10:27:12243 }
ossu20a4b3f2017-04-27 09:08:52244
Sebastian Jansson14a7cf92019-02-13 14:11:42245 void SetupMockForCallEncoder() {
minyue-webrtc8de18262017-07-26 12:18:40246 // Let ModifyEncoder to invoke mock audio encoder.
Sebastian Jansson14a7cf92019-02-13 14:11:42247 EXPECT_CALL(*channel_send_, CallEncoder(_))
Artem Titove7d08df2019-01-16 13:49:44248 .WillRepeatedly(
Sebastian Jansson14a7cf92019-02-13 14:11:42249 [this](rtc::FunctionView<void(AudioEncoder*)> modifier) {
minyue-webrtc8de18262017-07-26 12:18:40250 if (this->audio_encoder_)
Sebastian Jansson14a7cf92019-02-13 14:11:42251 modifier(this->audio_encoder_.get());
Artem Titove7d08df2019-01-16 13:49:44252 });
minyue-webrtc8de18262017-07-26 12:18:40253 }
254
Fredrik Solenbergb5727682015-12-04 14:22:19255 void SetupMockForSendTelephoneEvent() {
Niels Möllerdced9f62018-11-19 09:27:07256 EXPECT_TRUE(channel_send_);
257 EXPECT_CALL(*channel_send_, SetSendTelephoneEventPayloadType(
258 kTelephoneEventPayloadType,
Niels Möller8fb1a6a2019-03-05 13:29:42259 kTelephoneEventPayloadFrequency));
Yves Gerey665174f2018-06-19 13:03:05260 EXPECT_CALL(
Niels Möllerdced9f62018-11-19 09:27:07261 *channel_send_,
Fredrik Solenbergb5727682015-12-04 14:22:19262 SendTelephoneEventOutband(kTelephoneEventCode, kTelephoneEventDuration))
Yves Gerey665174f2018-06-19 13:03:05263 .WillOnce(Return(true));
Fredrik Solenbergb5727682015-12-04 14:22:19264 }
265
solenberg566ef242015-11-06 23:34:49266 void SetupMockForGetStats() {
Mirko Bonadei6a489f22019-04-09 13:11:12267 using ::testing::DoAll;
268 using ::testing::SetArgPointee;
269 using ::testing::SetArgReferee;
solenberg3a941542015-11-16 15:34:50270
solenberg566ef242015-11-06 23:34:49271 std::vector<ReportBlock> report_blocks;
272 webrtc::ReportBlock block = kReportBlock;
273 report_blocks.push_back(block); // Has wrong SSRC.
274 block.source_SSRC = kSsrc;
275 report_blocks.push_back(block); // Correct block.
276 block.fraction_lost = 0;
277 report_blocks.push_back(block); // Duplicate SSRC, bad fraction_lost.
278
Niels Möllerdced9f62018-11-19 09:27:07279 EXPECT_TRUE(channel_send_);
280 EXPECT_CALL(*channel_send_, GetRTCPStatistics())
solenberg358057b2015-11-27 18:46:42281 .WillRepeatedly(Return(kCallStats));
Niels Möllerdced9f62018-11-19 09:27:07282 EXPECT_CALL(*channel_send_, GetRemoteRTCPReportBlocks())
solenberg358057b2015-11-27 18:46:42283 .WillRepeatedly(Return(report_blocks));
Niels Möllerdced9f62018-11-19 09:27:07284 EXPECT_CALL(*channel_send_, GetANAStatistics())
ivoce1198e02017-09-08 15:13:19285 .WillRepeatedly(Return(ANAStats()));
Niels Möllerdced9f62018-11-19 09:27:07286 EXPECT_CALL(*channel_send_, GetBitrate()).WillRepeatedly(Return(0));
solenberg796b8f92017-03-02 01:02:23287
Ivo Creusen56d460902017-11-24 16:29:59288 audio_processing_stats_.echo_return_loss = kEchoReturnLoss;
289 audio_processing_stats_.echo_return_loss_enhancement =
290 kEchoReturnLossEnhancement;
291 audio_processing_stats_.delay_median_ms = kEchoDelayMedian;
292 audio_processing_stats_.delay_standard_deviation_ms = kEchoDelayStdDev;
293 audio_processing_stats_.divergent_filter_fraction =
294 kDivergentFilterFraction;
295 audio_processing_stats_.residual_echo_likelihood = kResidualEchoLikelihood;
296 audio_processing_stats_.residual_echo_likelihood_recent_max =
297 kResidualEchoLikelihoodMax;
ivoc7aba0292016-11-14 12:52:06298
Ivo Creusen56d460902017-11-24 16:29:59299 EXPECT_CALL(*audio_processing_, GetStatistics(true))
ivoc7aba0292016-11-14 12:52:06300 .WillRepeatedly(Return(audio_processing_stats_));
solenberg566ef242015-11-06 23:34:49301 }
302
303 private:
Sebastian Janssonda6806c2019-03-04 16:05:12304 SimulatedClock clock_;
Danil Chapovalov31660fd2019-03-22 11:59:48305 std::unique_ptr<TaskQueueFactory> task_queue_factory_;
solenberg566ef242015-11-06 23:34:49306 rtc::scoped_refptr<AudioState> audio_state_;
307 AudioSendStream::Config stream_config_;
Mirko Bonadei6a489f22019-04-09 13:11:12308 ::testing::StrictMock<MockChannelSend>* channel_send_ = nullptr;
peaha9cc40b2017-06-29 15:32:09309 rtc::scoped_refptr<MockAudioProcessing> audio_processing_;
Ivo Creusen56d460902017-11-24 16:29:59310 AudioProcessingStats audio_processing_stats_;
Mirko Bonadei6a489f22019-04-09 13:11:12311 ::testing::StrictMock<MockRtcpBandwidthObserver> bandwidth_observer_;
312 ::testing::NiceMock<MockRtcEventLog> event_log_;
313 ::testing::NiceMock<MockRtpTransportControllerSend> rtp_transport_;
314 ::testing::NiceMock<MockRtpRtcp> rtp_rtcp_;
michaelt9332b7d2016-11-30 15:51:13315 MockRtcpRttStats rtcp_rtt_stats_;
Mirko Bonadei6a489f22019-04-09 13:11:12316 ::testing::NiceMock<MockLimitObserver> limit_observer_;
mflodman86cc6ff2016-07-26 11:44:06317 BitrateAllocator bitrate_allocator_;
perkj26091b12016-09-01 08:17:40318 // |worker_queue| is defined last to ensure all pending tasks are cancelled
319 // and deleted before any other members.
Danil Chapovalov31660fd2019-03-22 11:59:48320 TaskQueueForTest worker_queue_;
minyue-webrtc8de18262017-07-26 12:18:40321 std::unique_ptr<AudioEncoder> audio_encoder_;
solenberg566ef242015-11-06 23:34:49322};
Henrik Boströmd2c336f2019-07-03 15:11:10323
324// The audio level ranges linearly [0,32767].
325std::unique_ptr<AudioFrame> CreateAudioFrame1kHzSineWave(int16_t audio_level,
326 int duration_ms,
327 int sample_rate_hz,
328 size_t num_channels) {
329 size_t samples_per_channel = sample_rate_hz / (1000 / duration_ms);
330 std::vector<int16_t> audio_data(samples_per_channel * num_channels, 0);
Mirko Bonadei317a1f02019-09-17 15:06:18331 std::unique_ptr<AudioFrame> audio_frame = std::make_unique<AudioFrame>();
Henrik Boströmd2c336f2019-07-03 15:11:10332 audio_frame->UpdateFrame(0 /* RTP timestamp */, &audio_data[0],
333 samples_per_channel, sample_rate_hz,
334 AudioFrame::SpeechType::kNormalSpeech,
335 AudioFrame::VADActivity::kVadUnknown, num_channels);
336 SineWaveGenerator wave_generator(1000.0, audio_level);
337 wave_generator.GenerateNextFrame(audio_frame.get());
338 return audio_frame;
339}
340
Fredrik Solenberg0ccae132015-11-03 09:15:49341} // namespace
solenbergc7a8b082015-10-16 21:35:07342
343TEST(AudioSendStreamTest, ConfigToString) {
Niels Möller7d76a312018-10-26 10:57:07344 AudioSendStream::Config config(/*send_transport=*/nullptr,
Anton Sukhanov4f08faa2019-05-21 18:12:57345 MediaTransportConfig());
Fredrik Solenberg0ccae132015-11-03 09:15:49346 config.rtp.ssrc = kSsrc;
solenberg3a941542015-11-16 15:34:50347 config.rtp.c_name = kCName;
minyue10cbb462016-11-07 17:29:22348 config.min_bitrate_bps = 12000;
349 config.max_bitrate_bps = 34000;
ossu20a4b3f2017-04-27 09:08:52350 config.send_codec_spec =
Oskar Sundbom2707fb22017-11-16 09:57:35351 AudioSendStream::Config::SendCodecSpec(kIsacPayloadType, kIsacFormat);
ossu20a4b3f2017-04-27 09:08:52352 config.send_codec_spec->nack_enabled = true;
353 config.send_codec_spec->transport_cc_enabled = false;
Oskar Sundbom2707fb22017-11-16 09:57:35354 config.send_codec_spec->cng_payload_type = 42;
ossu20a4b3f2017-04-27 09:08:52355 config.encoder_factory = MockAudioEncoderFactory::CreateUnusedFactory();
Johannes Kron9190b822018-10-29 10:22:05356 config.rtp.extmap_allow_mixed = true;
stefanb521aa72016-11-01 10:17:16357 config.rtp.extensions.push_back(
358 RtpExtension(RtpExtension::kAudioLevelUri, kAudioLevelId));
Jiawei Ou55718122018-11-09 21:17:39359 config.rtcp_report_interval_ms = 2500;
Fredrik Solenberg0ccae132015-11-03 09:15:49360 EXPECT_EQ(
Johannes Kron9190b822018-10-29 10:22:05361 "{rtp: {ssrc: 1234, extmap-allow-mixed: true, extensions: [{uri: "
Fredrik Solenbergc69a56e2018-11-21 08:21:23362 "urn:ietf:params:rtp-hdrext:ssrc-audio-level, id: 2}], "
363 "c_name: foo_name}, rtcp_report_interval_ms: 2500, "
Anton Sukhanov4f08faa2019-05-21 18:12:57364 "send_transport: null, media_transport_config: {media_transport: null}, "
Fredrik Solenberg8f5787a2018-01-11 12:52:30365 "min_bitrate_bps: 12000, max_bitrate_bps: 34000, "
solenberg940b6d62016-10-25 18:19:07366 "send_codec_spec: {nack_enabled: true, transport_cc_enabled: false, "
ossu20a4b3f2017-04-27 09:08:52367 "cng_payload_type: 42, payload_type: 103, "
368 "format: {name: isac, clockrate_hz: 16000, num_channels: 1, "
369 "parameters: {}}}}",
solenberg85a04962015-10-27 10:35:21370 config.ToString());
solenbergc7a8b082015-10-16 21:35:07371}
372
373TEST(AudioSendStreamTest, ConstructDestruct) {
ossu20a4b3f2017-04-27 09:08:52374 ConfigHelper helper(false, true);
Fredrik Solenberg8f5787a2018-01-11 12:52:30375 auto send_stream = helper.CreateAudioSendStream();
solenbergc7a8b082015-10-16 21:35:07376}
solenberg85a04962015-10-27 10:35:21377
Fredrik Solenbergb5727682015-12-04 14:22:19378TEST(AudioSendStreamTest, SendTelephoneEvent) {
ossu20a4b3f2017-04-27 09:08:52379 ConfigHelper helper(false, true);
Fredrik Solenberg8f5787a2018-01-11 12:52:30380 auto send_stream = helper.CreateAudioSendStream();
Fredrik Solenbergb5727682015-12-04 14:22:19381 helper.SetupMockForSendTelephoneEvent();
Yves Gerey665174f2018-06-19 13:03:05382 EXPECT_TRUE(send_stream->SendTelephoneEvent(
383 kTelephoneEventPayloadType, kTelephoneEventPayloadFrequency,
384 kTelephoneEventCode, kTelephoneEventDuration));
Fredrik Solenbergb5727682015-12-04 14:22:19385}
386
solenberg94218532016-06-16 17:53:22387TEST(AudioSendStreamTest, SetMuted) {
ossu20a4b3f2017-04-27 09:08:52388 ConfigHelper helper(false, true);
Fredrik Solenberg8f5787a2018-01-11 12:52:30389 auto send_stream = helper.CreateAudioSendStream();
Niels Möllerdced9f62018-11-19 09:27:07390 EXPECT_CALL(*helper.channel_send(), SetInputMute(true));
Fredrik Solenberg8f5787a2018-01-11 12:52:30391 send_stream->SetMuted(true);
solenberg94218532016-06-16 17:53:22392}
393
stefan7de8d642017-02-07 15:14:08394TEST(AudioSendStreamTest, AudioBweCorrectObjectsOnChannelProxy) {
Per Kjellander914351d2019-02-15 09:54:55395 ScopedFieldTrials field_trials("WebRTC-Audio-SendSideBwe/Enabled/");
ossu20a4b3f2017-04-27 09:08:52396 ConfigHelper helper(true, true);
Fredrik Solenberg8f5787a2018-01-11 12:52:30397 auto send_stream = helper.CreateAudioSendStream();
stefan7de8d642017-02-07 15:14:08398}
399
400TEST(AudioSendStreamTest, NoAudioBweCorrectObjectsOnChannelProxy) {
ossu20a4b3f2017-04-27 09:08:52401 ConfigHelper helper(false, true);
Fredrik Solenberg8f5787a2018-01-11 12:52:30402 auto send_stream = helper.CreateAudioSendStream();
stefan7de8d642017-02-07 15:14:08403}
404
solenberg85a04962015-10-27 10:35:21405TEST(AudioSendStreamTest, GetStats) {
ossu20a4b3f2017-04-27 09:08:52406 ConfigHelper helper(false, true);
Fredrik Solenberg8f5787a2018-01-11 12:52:30407 auto send_stream = helper.CreateAudioSendStream();
solenberg566ef242015-11-06 23:34:49408 helper.SetupMockForGetStats();
Fredrik Solenberg8f5787a2018-01-11 12:52:30409 AudioSendStream::Stats stats = send_stream->GetStats(true);
Fredrik Solenberg0ccae132015-11-03 09:15:49410 EXPECT_EQ(kSsrc, stats.local_ssrc);
411 EXPECT_EQ(static_cast<int64_t>(kCallStats.bytesSent), stats.bytes_sent);
412 EXPECT_EQ(kCallStats.packetsSent, stats.packets_sent);
Sebastian Jansson9701e0c2018-08-09 09:21:11413 EXPECT_EQ(kReportBlock.cumulative_num_packets_lost, stats.packets_lost);
Fredrik Solenberg0ccae132015-11-03 09:15:49414 EXPECT_EQ(Q8ToFloat(kReportBlock.fraction_lost), stats.fraction_lost);
Fredrik Solenbergf693bfa2018-12-11 11:22:10415 EXPECT_EQ(kIsacFormat.name, stats.codec_name);
Fredrik Solenberg0ccae132015-11-03 09:15:49416 EXPECT_EQ(static_cast<int32_t>(kReportBlock.interarrival_jitter /
Fredrik Solenbergf693bfa2018-12-11 11:22:10417 (kIsacFormat.clockrate_hz / 1000)),
Fredrik Solenberg0ccae132015-11-03 09:15:49418 stats.jitter_ms);
419 EXPECT_EQ(kCallStats.rttMs, stats.rtt_ms);
Fredrik Solenberg2a877972017-12-15 15:42:15420 EXPECT_EQ(0, stats.audio_level);
421 EXPECT_EQ(0, stats.total_input_energy);
422 EXPECT_EQ(0, stats.total_input_duration);
Ivo Creusen56d460902017-11-24 16:29:59423 EXPECT_EQ(kEchoDelayMedian, stats.apm_statistics.delay_median_ms);
424 EXPECT_EQ(kEchoDelayStdDev, stats.apm_statistics.delay_standard_deviation_ms);
425 EXPECT_EQ(kEchoReturnLoss, stats.apm_statistics.echo_return_loss);
426 EXPECT_EQ(kEchoReturnLossEnhancement,
427 stats.apm_statistics.echo_return_loss_enhancement);
428 EXPECT_EQ(kDivergentFilterFraction,
429 stats.apm_statistics.divergent_filter_fraction);
430 EXPECT_EQ(kResidualEchoLikelihood,
431 stats.apm_statistics.residual_echo_likelihood);
432 EXPECT_EQ(kResidualEchoLikelihoodMax,
433 stats.apm_statistics.residual_echo_likelihood_recent_max);
Fredrik Solenberg2a877972017-12-15 15:42:15434 EXPECT_FALSE(stats.typing_noise_detected);
solenberg566ef242015-11-06 23:34:49435}
minyue7a973442016-10-20 10:27:12436
Henrik Boströmd2c336f2019-07-03 15:11:10437TEST(AudioSendStreamTest, GetStatsAudioLevel) {
438 ConfigHelper helper(false, true);
439 auto send_stream = helper.CreateAudioSendStream();
440 helper.SetupMockForGetStats();
441 EXPECT_CALL(*helper.channel_send(), ProcessAndEncodeAudioForMock(_))
442 .Times(AnyNumber());
443
444 constexpr int kSampleRateHz = 48000;
445 constexpr size_t kNumChannels = 1;
446
447 constexpr int16_t kSilentAudioLevel = 0;
448 constexpr int16_t kMaxAudioLevel = 32767; // Audio level is [0,32767].
449 constexpr int kAudioFrameDurationMs = 10;
450
451 // Process 10 audio frames (100 ms) of silence. After this, on the next
452 // (11-th) frame, the audio level will be updated with the maximum audio level
453 // of the first 11 frames. See AudioLevel.
454 for (size_t i = 0; i < 10; ++i) {
455 send_stream->SendAudioData(CreateAudioFrame1kHzSineWave(
456 kSilentAudioLevel, kAudioFrameDurationMs, kSampleRateHz, kNumChannels));
457 }
458 AudioSendStream::Stats stats = send_stream->GetStats();
459 EXPECT_EQ(kSilentAudioLevel, stats.audio_level);
460 EXPECT_NEAR(0.0f, stats.total_input_energy, kTolerance);
461 EXPECT_NEAR(0.1f, stats.total_input_duration, kTolerance); // 100 ms = 0.1 s
462
463 // Process 10 audio frames (100 ms) of maximum audio level.
464 // Note that AudioLevel updates the audio level every 11th frame, processing
465 // 10 frames above was needed to see a non-zero audio level here.
466 for (size_t i = 0; i < 10; ++i) {
467 send_stream->SendAudioData(CreateAudioFrame1kHzSineWave(
468 kMaxAudioLevel, kAudioFrameDurationMs, kSampleRateHz, kNumChannels));
469 }
470 stats = send_stream->GetStats();
471 EXPECT_EQ(kMaxAudioLevel, stats.audio_level);
472 // Energy increases by energy*duration, where energy is audio level in [0,1].
473 EXPECT_NEAR(0.1f, stats.total_input_energy, kTolerance); // 0.1 s of max
474 EXPECT_NEAR(0.2f, stats.total_input_duration, kTolerance); // 200 ms = 0.2 s
475}
476
minyue-webrtc8de18262017-07-26 12:18:40477TEST(AudioSendStreamTest, SendCodecAppliesAudioNetworkAdaptor) {
ossu20a4b3f2017-04-27 09:08:52478 ConfigHelper helper(false, true);
Fredrik Solenberg8f5787a2018-01-11 12:52:30479 helper.config().send_codec_spec =
Oskar Sundbom2707fb22017-11-16 09:57:35480 AudioSendStream::Config::SendCodecSpec(0, kOpusFormat);
minyue-webrtc8de18262017-07-26 12:18:40481 const std::string kAnaConfigString = "abcde";
482 const std::string kAnaReconfigString = "12345";
483
Fredrik Solenberg8f5787a2018-01-11 12:52:30484 helper.config().audio_network_adaptor_config = kAnaConfigString;
ossu20a4b3f2017-04-27 09:08:52485
Karl Wibergd6fbf2a2018-02-27 12:37:31486 EXPECT_CALL(helper.mock_encoder_factory(), MakeAudioEncoderMock(_, _, _, _))
minyue-webrtc8de18262017-07-26 12:18:40487 .WillOnce(Invoke([&kAnaConfigString, &kAnaReconfigString](
488 int payload_type, const SdpAudioFormat& format,
Danil Chapovalovb9b146c2018-06-15 10:28:07489 absl::optional<AudioCodecPairId> codec_pair_id,
minyue-webrtc8de18262017-07-26 12:18:40490 std::unique_ptr<AudioEncoder>* return_value) {
ossu20a4b3f2017-04-27 09:08:52491 auto mock_encoder = SetupAudioEncoderMock(payload_type, format);
minyue-webrtc8de18262017-07-26 12:18:40492 EXPECT_CALL(*mock_encoder,
493 EnableAudioNetworkAdaptor(StrEq(kAnaConfigString), _))
494 .WillOnce(Return(true));
495 EXPECT_CALL(*mock_encoder,
496 EnableAudioNetworkAdaptor(StrEq(kAnaReconfigString), _))
ossu20a4b3f2017-04-27 09:08:52497 .WillOnce(Return(true));
498 *return_value = std::move(mock_encoder);
499 }));
500
Fredrik Solenberg8f5787a2018-01-11 12:52:30501 auto send_stream = helper.CreateAudioSendStream();
minyue-webrtc8de18262017-07-26 12:18:40502
Fredrik Solenberg8f5787a2018-01-11 12:52:30503 auto stream_config = helper.config();
Oskar Sundbom2707fb22017-11-16 09:57:35504 stream_config.audio_network_adaptor_config = kAnaReconfigString;
minyue-webrtc8de18262017-07-26 12:18:40505
Sebastian Jansson14a7cf92019-02-13 14:11:42506 helper.SetupMockForCallEncoder();
Fredrik Solenberg8f5787a2018-01-11 12:52:30507 send_stream->Reconfigure(stream_config);
minyue7a973442016-10-20 10:27:12508}
509
510// VAD is applied when codec is mono and the CNG frequency matches the codec
ossu20a4b3f2017-04-27 09:08:52511// clock rate.
minyue7a973442016-10-20 10:27:12512TEST(AudioSendStreamTest, SendCodecCanApplyVad) {
ossu20a4b3f2017-04-27 09:08:52513 ConfigHelper helper(false, false);
Fredrik Solenberg8f5787a2018-01-11 12:52:30514 helper.config().send_codec_spec =
Oskar Sundbom2707fb22017-11-16 09:57:35515 AudioSendStream::Config::SendCodecSpec(9, kG722Format);
Fredrik Solenberg8f5787a2018-01-11 12:52:30516 helper.config().send_codec_spec->cng_payload_type = 105;
ossu20a4b3f2017-04-27 09:08:52517 using ::testing::Invoke;
518 std::unique_ptr<AudioEncoder> stolen_encoder;
Niels Möllerdced9f62018-11-19 09:27:07519 EXPECT_CALL(*helper.channel_send(), SetEncoderForMock(_, _))
ossu20a4b3f2017-04-27 09:08:52520 .WillOnce(
521 Invoke([&stolen_encoder](int payload_type,
522 std::unique_ptr<AudioEncoder>* encoder) {
523 stolen_encoder = std::move(*encoder);
524 return true;
525 }));
Niels Mölleree5ccbc2019-03-06 15:47:29526 EXPECT_CALL(*helper.channel_send(), RegisterCngPayloadType(105, 8000));
ossu20a4b3f2017-04-27 09:08:52527
Fredrik Solenberg8f5787a2018-01-11 12:52:30528 auto send_stream = helper.CreateAudioSendStream();
ossu20a4b3f2017-04-27 09:08:52529
530 // We cannot truly determine if the encoder created is an AudioEncoderCng. It
531 // is the only reasonable implementation that will return something from
532 // ReclaimContainedEncoders, though.
533 ASSERT_TRUE(stolen_encoder);
534 EXPECT_FALSE(stolen_encoder->ReclaimContainedEncoders().empty());
minyue7a973442016-10-20 10:27:12535}
536
minyue78b4d562016-11-30 12:47:39537TEST(AudioSendStreamTest, DoesNotPassHigherBitrateThanMaxBitrate) {
ossu20a4b3f2017-04-27 09:08:52538 ConfigHelper helper(false, true);
Fredrik Solenberg8f5787a2018-01-11 12:52:30539 auto send_stream = helper.CreateAudioSendStream();
Niels Möllerdced9f62018-11-19 09:27:07540 EXPECT_CALL(*helper.channel_send(),
Sebastian Jansson254d8692018-11-21 18:19:00541 OnBitrateAllocation(
542 Field(&BitrateAllocationUpdate::target_bitrate,
543 Eq(DataRate::bps(helper.config().max_bitrate_bps)))));
Sebastian Janssonc0e4d452018-10-25 13:08:32544 BitrateAllocationUpdate update;
Sebastian Jansson13e59032018-11-21 18:13:07545 update.target_bitrate = DataRate::bps(helper.config().max_bitrate_bps + 5000);
546 update.packet_loss_ratio = 0;
547 update.round_trip_time = TimeDelta::ms(50);
548 update.bwe_period = TimeDelta::ms(6000);
Sebastian Janssonc0e4d452018-10-25 13:08:32549 send_stream->OnBitrateUpdated(update);
minyue78b4d562016-11-30 12:47:39550}
551
Daniel Lee93562522019-05-03 12:40:13552TEST(AudioSendStreamTest, SSBweTargetInRangeRespected) {
553 ScopedFieldTrials field_trials("WebRTC-Audio-SendSideBwe/Enabled/");
554 ConfigHelper helper(true, true);
555 auto send_stream = helper.CreateAudioSendStream();
556 EXPECT_CALL(*helper.channel_send(),
557 OnBitrateAllocation(Field(
558 &BitrateAllocationUpdate::target_bitrate,
559 Eq(DataRate::bps(helper.config().max_bitrate_bps - 5000)))));
560 BitrateAllocationUpdate update;
561 update.target_bitrate = DataRate::bps(helper.config().max_bitrate_bps - 5000);
562 send_stream->OnBitrateUpdated(update);
563}
564
565TEST(AudioSendStreamTest, SSBweFieldTrialMinRespected) {
566 ScopedFieldTrials field_trials(
567 "WebRTC-Audio-SendSideBwe/Enabled/"
568 "WebRTC-Audio-Allocation/min:6kbps,max:64kbps/");
569 ConfigHelper helper(true, true);
570 auto send_stream = helper.CreateAudioSendStream();
571 EXPECT_CALL(
572 *helper.channel_send(),
573 OnBitrateAllocation(Field(&BitrateAllocationUpdate::target_bitrate,
574 Eq(DataRate::kbps(6)))));
575 BitrateAllocationUpdate update;
576 update.target_bitrate = DataRate::kbps(1);
577 send_stream->OnBitrateUpdated(update);
578}
579
580TEST(AudioSendStreamTest, SSBweFieldTrialMaxRespected) {
581 ScopedFieldTrials field_trials(
582 "WebRTC-Audio-SendSideBwe/Enabled/"
583 "WebRTC-Audio-Allocation/min:6kbps,max:64kbps/");
584 ConfigHelper helper(true, true);
585 auto send_stream = helper.CreateAudioSendStream();
586 EXPECT_CALL(
587 *helper.channel_send(),
588 OnBitrateAllocation(Field(&BitrateAllocationUpdate::target_bitrate,
589 Eq(DataRate::kbps(64)))));
590 BitrateAllocationUpdate update;
591 update.target_bitrate = DataRate::kbps(128);
592 send_stream->OnBitrateUpdated(update);
593}
594
595TEST(AudioSendStreamTest, SSBweWithOverhead) {
596 ScopedFieldTrials field_trials(
597 "WebRTC-Audio-SendSideBwe/Enabled/"
598 "WebRTC-SendSideBwe-WithOverhead/Enabled/");
599 ConfigHelper helper(true, true);
600 auto send_stream = helper.CreateAudioSendStream();
601 const DataRate bitrate =
602 DataRate::bps(helper.config().max_bitrate_bps) + kOverheadRate;
603 EXPECT_CALL(*helper.channel_send(),
604 OnBitrateAllocation(Field(
605 &BitrateAllocationUpdate::target_bitrate, Eq(bitrate))));
606 BitrateAllocationUpdate update;
607 update.target_bitrate = bitrate;
608 send_stream->OnBitrateUpdated(update);
609}
610
611TEST(AudioSendStreamTest, SSBweWithOverheadMinRespected) {
612 ScopedFieldTrials field_trials(
613 "WebRTC-Audio-SendSideBwe/Enabled/"
614 "WebRTC-SendSideBwe-WithOverhead/Enabled/"
615 "WebRTC-Audio-Allocation/min:6kbps,max:64kbps/");
616 ConfigHelper helper(true, true);
617 auto send_stream = helper.CreateAudioSendStream();
618 const DataRate bitrate = DataRate::kbps(6) + kOverheadRate;
619 EXPECT_CALL(*helper.channel_send(),
620 OnBitrateAllocation(Field(
621 &BitrateAllocationUpdate::target_bitrate, Eq(bitrate))));
622 BitrateAllocationUpdate update;
623 update.target_bitrate = DataRate::kbps(1);
624 send_stream->OnBitrateUpdated(update);
625}
626
627TEST(AudioSendStreamTest, SSBweWithOverheadMaxRespected) {
628 ScopedFieldTrials field_trials(
629 "WebRTC-Audio-SendSideBwe/Enabled/"
630 "WebRTC-SendSideBwe-WithOverhead/Enabled/"
631 "WebRTC-Audio-Allocation/min:6kbps,max:64kbps/");
632 ConfigHelper helper(true, true);
633 auto send_stream = helper.CreateAudioSendStream();
634 const DataRate bitrate = DataRate::kbps(64) + kOverheadRate;
635 EXPECT_CALL(*helper.channel_send(),
636 OnBitrateAllocation(Field(
637 &BitrateAllocationUpdate::target_bitrate, Eq(bitrate))));
638 BitrateAllocationUpdate update;
639 update.target_bitrate = DataRate::kbps(128);
640 send_stream->OnBitrateUpdated(update);
641}
642
minyue78b4d562016-11-30 12:47:39643TEST(AudioSendStreamTest, ProbingIntervalOnBitrateUpdated) {
ossu20a4b3f2017-04-27 09:08:52644 ConfigHelper helper(false, true);
Fredrik Solenberg8f5787a2018-01-11 12:52:30645 auto send_stream = helper.CreateAudioSendStream();
Sebastian Jansson254d8692018-11-21 18:19:00646
647 EXPECT_CALL(*helper.channel_send(),
648 OnBitrateAllocation(Field(&BitrateAllocationUpdate::bwe_period,
649 Eq(TimeDelta::ms(5000)))));
Sebastian Janssonc0e4d452018-10-25 13:08:32650 BitrateAllocationUpdate update;
Sebastian Jansson13e59032018-11-21 18:13:07651 update.target_bitrate = DataRate::bps(helper.config().max_bitrate_bps + 5000);
652 update.packet_loss_ratio = 0;
653 update.round_trip_time = TimeDelta::ms(50);
654 update.bwe_period = TimeDelta::ms(5000);
Sebastian Janssonc0e4d452018-10-25 13:08:32655 send_stream->OnBitrateUpdated(update);
minyue78b4d562016-11-30 12:47:39656}
657
ossu20a4b3f2017-04-27 09:08:52658// Test that AudioSendStream doesn't recreate the encoder unnecessarily.
659TEST(AudioSendStreamTest, DontRecreateEncoder) {
660 ConfigHelper helper(false, false);
661 // WillOnce is (currently) the default used by ConfigHelper if asked to set an
662 // expectation for SetEncoder. Since this behavior is essential for this test
663 // to be correct, it's instead set-up manually here. Otherwise a simple change
664 // to ConfigHelper (say to WillRepeatedly) would silently make this test
665 // useless.
Niels Möllerdced9f62018-11-19 09:27:07666 EXPECT_CALL(*helper.channel_send(), SetEncoderForMock(_, _))
Niels Möller8fb1a6a2019-03-05 13:29:42667 .WillOnce(Return());
ossu20a4b3f2017-04-27 09:08:52668
Niels Mölleree5ccbc2019-03-06 15:47:29669 EXPECT_CALL(*helper.channel_send(), RegisterCngPayloadType(105, 8000));
670
Fredrik Solenberg8f5787a2018-01-11 12:52:30671 helper.config().send_codec_spec =
Oskar Sundbom2707fb22017-11-16 09:57:35672 AudioSendStream::Config::SendCodecSpec(9, kG722Format);
Fredrik Solenberg8f5787a2018-01-11 12:52:30673 helper.config().send_codec_spec->cng_payload_type = 105;
674 auto send_stream = helper.CreateAudioSendStream();
675 send_stream->Reconfigure(helper.config());
ossu20a4b3f2017-04-27 09:08:52676}
677
Yves Gerey17048012019-07-26 15:49:52678// Allow to check for race conditions under tsan.
679// This mimicks the situation where 'ModuleProcessThread' (pacer thread) is
680// launched by webrtc::RtpTransportControllerSend::RtpTransportControllerSend().
681TEST(AudioSendStreamTest, RaceFree) {
682 ConfigHelper helper(false, false);
683 // Sanity checks: copy-pasted from DontRecreateEncoder test.
684 EXPECT_CALL(*helper.channel_send(), SetEncoderForMock(_, _))
685 .WillOnce(Return());
686
687 EXPECT_CALL(*helper.channel_send(), RegisterCngPayloadType(105, 8000));
688
689 helper.config().send_codec_spec =
690 AudioSendStream::Config::SendCodecSpec(9, kG722Format);
691 helper.config().send_codec_spec->cng_payload_type = 105;
692 auto send_stream = helper.CreateAudioSendStream();
693 std::thread pacer([&]() {
694 send_stream->OnPacketAdded(/*ssrc*/ 0xcafe,
695 /*seq_num*/ 0xf00d);
696 });
697 send_stream->Reconfigure(helper.config());
698 pacer.join();
699}
700
ossu1129df22017-06-30 08:38:56701TEST(AudioSendStreamTest, ReconfigureTransportCcResetsFirst) {
Per Kjellander914351d2019-02-15 09:54:55702 ScopedFieldTrials field_trials("WebRTC-Audio-SendSideBwe/Enabled/");
ossu1129df22017-06-30 08:38:56703 ConfigHelper helper(false, true);
Fredrik Solenberg8f5787a2018-01-11 12:52:30704 auto send_stream = helper.CreateAudioSendStream();
ossu1129df22017-06-30 08:38:56705 auto new_config = helper.config();
706 ConfigHelper::AddBweToConfig(&new_config);
Niels Möllerdced9f62018-11-19 09:27:07707 EXPECT_CALL(*helper.channel_send(),
ossu1129df22017-06-30 08:38:56708 EnableSendTransportSequenceNumber(kTransportSequenceNumberId))
709 .Times(1);
710 {
711 ::testing::InSequence seq;
Niels Möllerdced9f62018-11-19 09:27:07712 EXPECT_CALL(*helper.channel_send(), ResetSenderCongestionControlObjects())
ossu1129df22017-06-30 08:38:56713 .Times(1);
Niels Möllerdced9f62018-11-19 09:27:07714 EXPECT_CALL(*helper.channel_send(), RegisterSenderCongestionControlObjects(
715 helper.transport(), Ne(nullptr)))
ossu1129df22017-06-30 08:38:56716 .Times(1);
717 }
Anton Sukhanov626015d2019-02-04 23:16:06718
Fredrik Solenberg8f5787a2018-01-11 12:52:30719 send_stream->Reconfigure(new_config);
ossu1129df22017-06-30 08:38:56720}
Sam Zackrisson06953ba2018-02-01 15:53:16721
Anton Sukhanov626015d2019-02-04 23:16:06722TEST(AudioSendStreamTest, OnTransportOverheadChanged) {
723 ConfigHelper helper(false, true);
724 auto send_stream = helper.CreateAudioSendStream();
725 auto new_config = helper.config();
726
Sebastian Jansson14a7cf92019-02-13 14:11:42727 // CallEncoder will be called on overhead change.
Mirko Bonadei6a489f22019-04-09 13:11:12728 EXPECT_CALL(*helper.channel_send(), CallEncoder(::testing::_)).Times(1);
Anton Sukhanov626015d2019-02-04 23:16:06729
730 const size_t transport_overhead_per_packet_bytes = 333;
731 send_stream->SetTransportOverhead(transport_overhead_per_packet_bytes);
732
733 EXPECT_EQ(transport_overhead_per_packet_bytes,
734 send_stream->TestOnlyGetPerPacketOverheadBytes());
735}
736
737TEST(AudioSendStreamTest, OnAudioOverheadChanged) {
738 ConfigHelper helper(false, true);
739 auto send_stream = helper.CreateAudioSendStream();
740 auto new_config = helper.config();
741
Sebastian Jansson14a7cf92019-02-13 14:11:42742 // CallEncoder will be called on overhead change.
Mirko Bonadei6a489f22019-04-09 13:11:12743 EXPECT_CALL(*helper.channel_send(), CallEncoder(::testing::_)).Times(1);
Anton Sukhanov626015d2019-02-04 23:16:06744
745 const size_t audio_overhead_per_packet_bytes = 555;
746 send_stream->OnOverheadChanged(audio_overhead_per_packet_bytes);
747 EXPECT_EQ(audio_overhead_per_packet_bytes,
748 send_stream->TestOnlyGetPerPacketOverheadBytes());
749}
750
751TEST(AudioSendStreamTest, OnAudioAndTransportOverheadChanged) {
752 ConfigHelper helper(false, true);
753 auto send_stream = helper.CreateAudioSendStream();
754 auto new_config = helper.config();
755
Sebastian Jansson14a7cf92019-02-13 14:11:42756 // CallEncoder will be called when each of overhead changes.
Mirko Bonadei6a489f22019-04-09 13:11:12757 EXPECT_CALL(*helper.channel_send(), CallEncoder(::testing::_)).Times(2);
Anton Sukhanov626015d2019-02-04 23:16:06758
759 const size_t transport_overhead_per_packet_bytes = 333;
760 send_stream->SetTransportOverhead(transport_overhead_per_packet_bytes);
761
762 const size_t audio_overhead_per_packet_bytes = 555;
763 send_stream->OnOverheadChanged(audio_overhead_per_packet_bytes);
764
765 EXPECT_EQ(
766 transport_overhead_per_packet_bytes + audio_overhead_per_packet_bytes,
767 send_stream->TestOnlyGetPerPacketOverheadBytes());
768}
769
Benjamin Wright78410ad2018-10-25 16:52:57770// Validates that reconfiguring the AudioSendStream with a Frame encryptor
771// correctly reconfigures on the object without crashing.
772TEST(AudioSendStreamTest, ReconfigureWithFrameEncryptor) {
773 ConfigHelper helper(false, true);
774 auto send_stream = helper.CreateAudioSendStream();
775 auto new_config = helper.config();
776
777 rtc::scoped_refptr<FrameEncryptorInterface> mock_frame_encryptor_0(
778 new rtc::RefCountedObject<MockFrameEncryptor>());
779 new_config.frame_encryptor = mock_frame_encryptor_0;
Niels Möllerdced9f62018-11-19 09:27:07780 EXPECT_CALL(*helper.channel_send(), SetFrameEncryptor(Ne(nullptr))).Times(1);
Benjamin Wright78410ad2018-10-25 16:52:57781 send_stream->Reconfigure(new_config);
782
783 // Not updating the frame encryptor shouldn't force it to reconfigure.
Niels Möllerdced9f62018-11-19 09:27:07784 EXPECT_CALL(*helper.channel_send(), SetFrameEncryptor(_)).Times(0);
Benjamin Wright78410ad2018-10-25 16:52:57785 send_stream->Reconfigure(new_config);
786
787 // Updating frame encryptor to a new object should force a call to the proxy.
788 rtc::scoped_refptr<FrameEncryptorInterface> mock_frame_encryptor_1(
789 new rtc::RefCountedObject<MockFrameEncryptor>());
790 new_config.frame_encryptor = mock_frame_encryptor_1;
791 new_config.crypto_options.sframe.require_frame_encryption = true;
Niels Möllerdced9f62018-11-19 09:27:07792 EXPECT_CALL(*helper.channel_send(), SetFrameEncryptor(Ne(nullptr))).Times(1);
Benjamin Wright78410ad2018-10-25 16:52:57793 send_stream->Reconfigure(new_config);
794}
solenberg85a04962015-10-27 10:35:21795} // namespace test
solenbergc7a8b082015-10-16 21:35:07796} // namespace webrtc