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