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