Sebastian Jansson | 98b07e91 | 2018-09-27 11:47:01 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2018 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 | #include "test/scenario/audio_stream.h" |
| 11 | |
Steve Anton | 40d5533 | 2019-01-07 18:21:47 | [diff] [blame] | 12 | #include "absl/memory/memory.h" |
Steve Anton | 10542f2 | 2019-01-11 17:11:00 | [diff] [blame] | 13 | #include "rtc_base/bitrate_allocation_strategy.h" |
Sebastian Jansson | 98b07e91 | 2018-09-27 11:47:01 | [diff] [blame] | 14 | #include "test/call_test.h" |
| 15 | |
Sebastian Jansson | b9972fa | 2018-10-17 14:27:55 | [diff] [blame] | 16 | #if WEBRTC_ENABLE_PROTOBUF |
| 17 | RTC_PUSH_IGNORING_WUNDEF() |
| 18 | #ifdef WEBRTC_ANDROID_PLATFORM_BUILD |
| 19 | #include "external/webrtc/webrtc/modules/audio_coding/audio_network_adaptor/config.pb.h" |
| 20 | #else |
| 21 | #include "modules/audio_coding/audio_network_adaptor/config.pb.h" |
| 22 | #endif |
| 23 | RTC_POP_IGNORING_WUNDEF() |
| 24 | #endif |
| 25 | |
Sebastian Jansson | 98b07e91 | 2018-09-27 11:47:01 | [diff] [blame] | 26 | namespace webrtc { |
| 27 | namespace test { |
Sebastian Jansson | b9972fa | 2018-10-17 14:27:55 | [diff] [blame] | 28 | namespace { |
| 29 | absl::optional<std::string> CreateAdaptationString( |
| 30 | AudioStreamConfig::NetworkAdaptation config) { |
| 31 | #if WEBRTC_ENABLE_PROTOBUF |
| 32 | |
| 33 | audio_network_adaptor::config::ControllerManager cont_conf; |
| 34 | if (config.frame.max_rate_for_60_ms.IsFinite()) { |
| 35 | auto controller = |
| 36 | cont_conf.add_controllers()->mutable_frame_length_controller(); |
| 37 | controller->set_fl_decreasing_packet_loss_fraction( |
| 38 | config.frame.min_packet_loss_for_decrease); |
| 39 | controller->set_fl_increasing_packet_loss_fraction( |
| 40 | config.frame.max_packet_loss_for_increase); |
| 41 | |
| 42 | controller->set_fl_20ms_to_60ms_bandwidth_bps( |
| 43 | config.frame.min_rate_for_20_ms.bps<int32_t>()); |
| 44 | controller->set_fl_60ms_to_20ms_bandwidth_bps( |
| 45 | config.frame.max_rate_for_60_ms.bps<int32_t>()); |
| 46 | |
| 47 | if (config.frame.max_rate_for_120_ms.IsFinite()) { |
| 48 | controller->set_fl_60ms_to_120ms_bandwidth_bps( |
| 49 | config.frame.min_rate_for_60_ms.bps<int32_t>()); |
| 50 | controller->set_fl_120ms_to_60ms_bandwidth_bps( |
| 51 | config.frame.max_rate_for_120_ms.bps<int32_t>()); |
| 52 | } |
| 53 | } |
| 54 | cont_conf.add_controllers()->mutable_bitrate_controller(); |
| 55 | std::string config_string = cont_conf.SerializeAsString(); |
| 56 | return config_string; |
| 57 | #else |
| 58 | RTC_LOG(LS_ERROR) << "audio_network_adaptation is enabled" |
| 59 | " but WEBRTC_ENABLE_PROTOBUF is false.\n" |
| 60 | "Ignoring settings."; |
| 61 | return absl::nullopt; |
| 62 | #endif // WEBRTC_ENABLE_PROTOBUF |
| 63 | } |
| 64 | } // namespace |
Sebastian Jansson | 98b07e91 | 2018-09-27 11:47:01 | [diff] [blame] | 65 | |
| 66 | SendAudioStream::SendAudioStream( |
| 67 | CallClient* sender, |
| 68 | AudioStreamConfig config, |
| 69 | rtc::scoped_refptr<AudioEncoderFactory> encoder_factory, |
| 70 | Transport* send_transport) |
| 71 | : sender_(sender), config_(config) { |
Niels Möller | 7d76a31 | 2018-10-26 10:57:07 | [diff] [blame] | 72 | AudioSendStream::Config send_config(send_transport, |
| 73 | /*media_transport=*/nullptr); |
Sebastian Jansson | 98b07e91 | 2018-09-27 11:47:01 | [diff] [blame] | 74 | ssrc_ = sender->GetNextAudioSsrc(); |
| 75 | send_config.rtp.ssrc = ssrc_; |
| 76 | SdpAudioFormat::Parameters sdp_params; |
| 77 | if (config.source.channels == 2) |
| 78 | sdp_params["stereo"] = "1"; |
| 79 | if (config.encoder.initial_frame_length != TimeDelta::ms(20)) |
| 80 | sdp_params["ptime"] = |
| 81 | std::to_string(config.encoder.initial_frame_length.ms()); |
Sebastian Jansson | ad87194 | 2019-01-16 16:21:28 | [diff] [blame] | 82 | if (config.encoder.enable_dtx) |
| 83 | sdp_params["usedtx"] = "1"; |
Sebastian Jansson | 98b07e91 | 2018-09-27 11:47:01 | [diff] [blame] | 84 | |
| 85 | // SdpAudioFormat::num_channels indicates that the encoder is capable of |
| 86 | // stereo, but the actual channel count used is based on the "stereo" |
| 87 | // parameter. |
| 88 | send_config.send_codec_spec = AudioSendStream::Config::SendCodecSpec( |
| 89 | CallTest::kAudioSendPayloadType, {"opus", 48000, 2, sdp_params}); |
| 90 | RTC_DCHECK_LE(config.source.channels, 2); |
| 91 | send_config.encoder_factory = encoder_factory; |
| 92 | |
| 93 | if (config.encoder.fixed_rate) |
| 94 | send_config.send_codec_spec->target_bitrate_bps = |
| 95 | config.encoder.fixed_rate->bps(); |
| 96 | |
Sebastian Jansson | b9972fa | 2018-10-17 14:27:55 | [diff] [blame] | 97 | if (config.network_adaptation) { |
| 98 | send_config.audio_network_adaptor_config = |
| 99 | CreateAdaptationString(config.adapt); |
| 100 | } |
Sebastian Jansson | 98b07e91 | 2018-09-27 11:47:01 | [diff] [blame] | 101 | if (config.encoder.allocate_bitrate || |
| 102 | config.stream.in_bandwidth_estimation) { |
| 103 | DataRate min_rate = DataRate::Infinity(); |
| 104 | DataRate max_rate = DataRate::Infinity(); |
| 105 | if (config.encoder.fixed_rate) { |
| 106 | min_rate = *config.encoder.fixed_rate; |
| 107 | max_rate = *config.encoder.fixed_rate; |
| 108 | } else { |
| 109 | min_rate = *config.encoder.min_rate; |
| 110 | max_rate = *config.encoder.max_rate; |
| 111 | } |
Sebastian Jansson | 98b07e91 | 2018-09-27 11:47:01 | [diff] [blame] | 112 | send_config.min_bitrate_bps = min_rate.bps(); |
| 113 | send_config.max_bitrate_bps = max_rate.bps(); |
| 114 | } |
| 115 | |
| 116 | if (config.stream.in_bandwidth_estimation) { |
| 117 | send_config.send_codec_spec->transport_cc_enabled = true; |
| 118 | send_config.rtp.extensions = { |
| 119 | {RtpExtension::kTransportSequenceNumberUri, 8}}; |
| 120 | } |
| 121 | |
Sebastian Jansson | 2b101d2 | 2018-11-12 15:33:39 | [diff] [blame] | 122 | if (config.encoder.priority_rate) { |
Sebastian Jansson | 98b07e91 | 2018-09-27 11:47:01 | [diff] [blame] | 123 | send_config.track_id = sender->GetNextPriorityId(); |
Sebastian Jansson | 2b101d2 | 2018-11-12 15:33:39 | [diff] [blame] | 124 | sender_->call_->SetBitrateAllocationStrategy( |
| 125 | absl::make_unique<rtc::AudioPriorityBitrateAllocationStrategy>( |
| 126 | send_config.track_id, |
| 127 | config.encoder.priority_rate->bps<uint32_t>())); |
Sebastian Jansson | 98b07e91 | 2018-09-27 11:47:01 | [diff] [blame] | 128 | } |
Sebastian Jansson | 105a10a | 2019-04-01 07:18:14 | [diff] [blame^] | 129 | sender_->SendTask([&] { |
| 130 | send_stream_ = sender_->call_->CreateAudioSendStream(send_config); |
| 131 | if (field_trial::IsEnabled("WebRTC-SendSideBwe-WithOverhead")) { |
| 132 | sender->call_->OnAudioTransportOverheadChanged( |
| 133 | sender_->transport_->packet_overhead().bytes()); |
| 134 | } |
| 135 | }); |
Sebastian Jansson | 98b07e91 | 2018-09-27 11:47:01 | [diff] [blame] | 136 | } |
| 137 | |
| 138 | SendAudioStream::~SendAudioStream() { |
Sebastian Jansson | 105a10a | 2019-04-01 07:18:14 | [diff] [blame^] | 139 | sender_->SendTask( |
| 140 | [this] { sender_->call_->DestroyAudioSendStream(send_stream_); }); |
Sebastian Jansson | 98b07e91 | 2018-09-27 11:47:01 | [diff] [blame] | 141 | } |
| 142 | |
| 143 | void SendAudioStream::Start() { |
Sebastian Jansson | 105a10a | 2019-04-01 07:18:14 | [diff] [blame^] | 144 | sender_->SendTask([this] { |
| 145 | send_stream_->Start(); |
| 146 | sender_->call_->SignalChannelNetworkState(MediaType::AUDIO, kNetworkUp); |
| 147 | }); |
Sebastian Jansson | 98b07e91 | 2018-09-27 11:47:01 | [diff] [blame] | 148 | } |
| 149 | |
Sebastian Jansson | bdfadd6 | 2019-02-08 12:34:57 | [diff] [blame] | 150 | void SendAudioStream::Stop() { |
Sebastian Jansson | 105a10a | 2019-04-01 07:18:14 | [diff] [blame^] | 151 | sender_->SendTask([this] { send_stream_->Stop(); }); |
Sebastian Jansson | bdfadd6 | 2019-02-08 12:34:57 | [diff] [blame] | 152 | } |
| 153 | |
Sebastian Jansson | ad87194 | 2019-01-16 16:21:28 | [diff] [blame] | 154 | void SendAudioStream::SetMuted(bool mute) { |
| 155 | send_stream_->SetMuted(mute); |
| 156 | } |
| 157 | |
Sebastian Jansson | 359d60a | 2018-10-25 14:22:02 | [diff] [blame] | 158 | ColumnPrinter SendAudioStream::StatsPrinter() { |
| 159 | return ColumnPrinter::Lambda( |
| 160 | "audio_target_rate", |
| 161 | [this](rtc::SimpleStringBuilder& sb) { |
Sebastian Jansson | 105a10a | 2019-04-01 07:18:14 | [diff] [blame^] | 162 | sender_->SendTask([this, &sb] { |
| 163 | AudioSendStream::Stats stats = send_stream_->GetStats(); |
| 164 | sb.AppendFormat("%.0lf", stats.target_bitrate_bps / 8.0); |
| 165 | }); |
Sebastian Jansson | 359d60a | 2018-10-25 14:22:02 | [diff] [blame] | 166 | }, |
| 167 | 64); |
| 168 | } |
| 169 | |
Sebastian Jansson | 98b07e91 | 2018-09-27 11:47:01 | [diff] [blame] | 170 | ReceiveAudioStream::ReceiveAudioStream( |
| 171 | CallClient* receiver, |
| 172 | AudioStreamConfig config, |
| 173 | SendAudioStream* send_stream, |
| 174 | rtc::scoped_refptr<AudioDecoderFactory> decoder_factory, |
| 175 | Transport* feedback_transport) |
| 176 | : receiver_(receiver), config_(config) { |
| 177 | AudioReceiveStream::Config recv_config; |
Sebastian Jansson | 5fbebd5 | 2019-02-20 10:16:19 | [diff] [blame] | 178 | recv_config.rtp.local_ssrc = receiver_->GetNextAudioLocalSsrc(); |
Sebastian Jansson | 98b07e91 | 2018-09-27 11:47:01 | [diff] [blame] | 179 | recv_config.rtcp_send_transport = feedback_transport; |
| 180 | recv_config.rtp.remote_ssrc = send_stream->ssrc_; |
Sebastian Jansson | 800e121 | 2018-10-22 09:49:03 | [diff] [blame] | 181 | receiver->ssrc_media_types_[recv_config.rtp.remote_ssrc] = MediaType::AUDIO; |
Sebastian Jansson | 98b07e91 | 2018-09-27 11:47:01 | [diff] [blame] | 182 | if (config.stream.in_bandwidth_estimation) { |
| 183 | recv_config.rtp.transport_cc = true; |
| 184 | recv_config.rtp.extensions = { |
| 185 | {RtpExtension::kTransportSequenceNumberUri, 8}}; |
| 186 | } |
Sebastian Jansson | fd20171 | 2018-11-12 15:44:16 | [diff] [blame] | 187 | receiver_->AddExtensions(recv_config.rtp.extensions); |
Sebastian Jansson | 98b07e91 | 2018-09-27 11:47:01 | [diff] [blame] | 188 | recv_config.decoder_factory = decoder_factory; |
| 189 | recv_config.decoder_map = { |
| 190 | {CallTest::kAudioSendPayloadType, {"opus", 48000, 2}}}; |
| 191 | recv_config.sync_group = config.render.sync_group; |
Sebastian Jansson | 105a10a | 2019-04-01 07:18:14 | [diff] [blame^] | 192 | receiver_->SendTask([&] { |
| 193 | receive_stream_ = receiver_->call_->CreateAudioReceiveStream(recv_config); |
| 194 | }); |
Sebastian Jansson | 98b07e91 | 2018-09-27 11:47:01 | [diff] [blame] | 195 | } |
| 196 | ReceiveAudioStream::~ReceiveAudioStream() { |
Sebastian Jansson | 105a10a | 2019-04-01 07:18:14 | [diff] [blame^] | 197 | receiver_->SendTask( |
| 198 | [&] { receiver_->call_->DestroyAudioReceiveStream(receive_stream_); }); |
Sebastian Jansson | 98b07e91 | 2018-09-27 11:47:01 | [diff] [blame] | 199 | } |
| 200 | |
Sebastian Jansson | 49a7843 | 2018-11-20 15:15:29 | [diff] [blame] | 201 | void ReceiveAudioStream::Start() { |
Sebastian Jansson | 105a10a | 2019-04-01 07:18:14 | [diff] [blame^] | 202 | receiver_->SendTask([&] { |
| 203 | receive_stream_->Start(); |
| 204 | receiver_->call_->SignalChannelNetworkState(MediaType::AUDIO, kNetworkUp); |
| 205 | }); |
Sebastian Jansson | 49a7843 | 2018-11-20 15:15:29 | [diff] [blame] | 206 | } |
| 207 | |
Sebastian Jansson | bdfadd6 | 2019-02-08 12:34:57 | [diff] [blame] | 208 | void ReceiveAudioStream::Stop() { |
Sebastian Jansson | 105a10a | 2019-04-01 07:18:14 | [diff] [blame^] | 209 | receiver_->SendTask([&] { receive_stream_->Stop(); }); |
Sebastian Jansson | bdfadd6 | 2019-02-08 12:34:57 | [diff] [blame] | 210 | } |
| 211 | |
Sebastian Jansson | 98b07e91 | 2018-09-27 11:47:01 | [diff] [blame] | 212 | AudioStreamPair::~AudioStreamPair() = default; |
| 213 | |
| 214 | AudioStreamPair::AudioStreamPair( |
| 215 | CallClient* sender, |
Sebastian Jansson | 98b07e91 | 2018-09-27 11:47:01 | [diff] [blame] | 216 | rtc::scoped_refptr<AudioEncoderFactory> encoder_factory, |
| 217 | CallClient* receiver, |
Sebastian Jansson | 98b07e91 | 2018-09-27 11:47:01 | [diff] [blame] | 218 | rtc::scoped_refptr<AudioDecoderFactory> decoder_factory, |
| 219 | AudioStreamConfig config) |
| 220 | : config_(config), |
Sebastian Jansson | 105a10a | 2019-04-01 07:18:14 | [diff] [blame^] | 221 | send_stream_(sender, config, encoder_factory, sender->transport_.get()), |
Sebastian Jansson | 98b07e91 | 2018-09-27 11:47:01 | [diff] [blame] | 222 | receive_stream_(receiver, |
| 223 | config, |
| 224 | &send_stream_, |
| 225 | decoder_factory, |
Sebastian Jansson | 105a10a | 2019-04-01 07:18:14 | [diff] [blame^] | 226 | receiver->transport_.get()) {} |
Sebastian Jansson | 98b07e91 | 2018-09-27 11:47:01 | [diff] [blame] | 227 | |
| 228 | } // namespace test |
| 229 | } // namespace webrtc |