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 | 2b101d2 | 2018-11-12 16:33:39 +0100 | [diff] [blame] | 13 | #include "rtc_base/bitrateallocationstrategy.h" |
Sebastian Jansson | 98b07e9 | 2018-09-27 13:47:01 +0200 | [diff] [blame] | 14 | #include "test/call_test.h" |
| 15 | |
Sebastian Jansson | b9972fa | 2018-10-17 16:27:55 +0200 | [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 | 98b07e9 | 2018-09-27 13:47:01 +0200 | [diff] [blame] | 26 | namespace webrtc { |
| 27 | namespace test { |
Sebastian Jansson | b9972fa | 2018-10-17 16:27:55 +0200 | [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 | 98b07e9 | 2018-09-27 13:47:01 +0200 | [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 12:57:07 +0200 | [diff] [blame] | 72 | AudioSendStream::Config send_config(send_transport, |
| 73 | /*media_transport=*/nullptr); |
Sebastian Jansson | 98b07e9 | 2018-09-27 13:47:01 +0200 | [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()); |
| 82 | |
| 83 | // SdpAudioFormat::num_channels indicates that the encoder is capable of |
| 84 | // stereo, but the actual channel count used is based on the "stereo" |
| 85 | // parameter. |
| 86 | send_config.send_codec_spec = AudioSendStream::Config::SendCodecSpec( |
| 87 | CallTest::kAudioSendPayloadType, {"opus", 48000, 2, sdp_params}); |
| 88 | RTC_DCHECK_LE(config.source.channels, 2); |
| 89 | send_config.encoder_factory = encoder_factory; |
| 90 | |
| 91 | if (config.encoder.fixed_rate) |
| 92 | send_config.send_codec_spec->target_bitrate_bps = |
| 93 | config.encoder.fixed_rate->bps(); |
| 94 | |
Sebastian Jansson | b9972fa | 2018-10-17 16:27:55 +0200 | [diff] [blame] | 95 | if (config.network_adaptation) { |
| 96 | send_config.audio_network_adaptor_config = |
| 97 | CreateAdaptationString(config.adapt); |
| 98 | } |
Sebastian Jansson | 98b07e9 | 2018-09-27 13:47:01 +0200 | [diff] [blame] | 99 | if (config.encoder.allocate_bitrate || |
| 100 | config.stream.in_bandwidth_estimation) { |
| 101 | DataRate min_rate = DataRate::Infinity(); |
| 102 | DataRate max_rate = DataRate::Infinity(); |
| 103 | if (config.encoder.fixed_rate) { |
| 104 | min_rate = *config.encoder.fixed_rate; |
| 105 | max_rate = *config.encoder.fixed_rate; |
| 106 | } else { |
| 107 | min_rate = *config.encoder.min_rate; |
| 108 | max_rate = *config.encoder.max_rate; |
| 109 | } |
| 110 | if (field_trial::IsEnabled("WebRTC-SendSideBwe-WithOverhead")) { |
Sebastian Jansson | ed45c57 | 2018-10-24 11:18:07 +0200 | [diff] [blame] | 111 | TimeDelta min_frame_length = TimeDelta::ms(20); |
| 112 | // Note, depends on WEBRTC_OPUS_SUPPORT_120MS_PTIME being set, which is |
| 113 | // the default. |
| 114 | TimeDelta max_frame_length = TimeDelta::ms(120); |
Sebastian Jansson | 98b07e9 | 2018-09-27 13:47:01 +0200 | [diff] [blame] | 115 | DataSize rtp_overhead = DataSize::bytes(12); |
Sebastian Jansson | ed45c57 | 2018-10-24 11:18:07 +0200 | [diff] [blame] | 116 | // Note that this does not include rtp extension overhead and will not |
| 117 | // follow updates in the transport overhead over time. |
Sebastian Jansson | 800e121 | 2018-10-22 11:49:03 +0200 | [diff] [blame] | 118 | DataSize total_overhead = |
| 119 | sender_->transport_.packet_overhead() + rtp_overhead; |
Sebastian Jansson | ed45c57 | 2018-10-24 11:18:07 +0200 | [diff] [blame] | 120 | |
Sebastian Jansson | b9972fa | 2018-10-17 16:27:55 +0200 | [diff] [blame] | 121 | min_rate += total_overhead / max_frame_length; |
Sebastian Jansson | ed45c57 | 2018-10-24 11:18:07 +0200 | [diff] [blame] | 122 | // In WebRTCVoiceEngine the max rate is also based on the max frame |
| 123 | // length. |
Sebastian Jansson | b9972fa | 2018-10-17 16:27:55 +0200 | [diff] [blame] | 124 | max_rate += total_overhead / min_frame_length; |
Sebastian Jansson | 98b07e9 | 2018-09-27 13:47:01 +0200 | [diff] [blame] | 125 | } |
| 126 | send_config.min_bitrate_bps = min_rate.bps(); |
| 127 | send_config.max_bitrate_bps = max_rate.bps(); |
| 128 | } |
| 129 | |
| 130 | if (config.stream.in_bandwidth_estimation) { |
| 131 | send_config.send_codec_spec->transport_cc_enabled = true; |
| 132 | send_config.rtp.extensions = { |
| 133 | {RtpExtension::kTransportSequenceNumberUri, 8}}; |
| 134 | } |
| 135 | |
Sebastian Jansson | 2b101d2 | 2018-11-12 16:33:39 +0100 | [diff] [blame] | 136 | if (config.encoder.priority_rate) { |
Sebastian Jansson | 98b07e9 | 2018-09-27 13:47:01 +0200 | [diff] [blame] | 137 | send_config.track_id = sender->GetNextPriorityId(); |
Sebastian Jansson | 2b101d2 | 2018-11-12 16:33:39 +0100 | [diff] [blame] | 138 | sender_->call_->SetBitrateAllocationStrategy( |
| 139 | absl::make_unique<rtc::AudioPriorityBitrateAllocationStrategy>( |
| 140 | send_config.track_id, |
| 141 | config.encoder.priority_rate->bps<uint32_t>())); |
Sebastian Jansson | 98b07e9 | 2018-09-27 13:47:01 +0200 | [diff] [blame] | 142 | } |
| 143 | send_stream_ = sender_->call_->CreateAudioSendStream(send_config); |
| 144 | if (field_trial::IsEnabled("WebRTC-SendSideBwe-WithOverhead")) { |
Stefan Holmer | 64be7fa | 2018-10-04 15:21:55 +0200 | [diff] [blame] | 145 | sender->call_->OnAudioTransportOverheadChanged( |
Sebastian Jansson | 800e121 | 2018-10-22 11:49:03 +0200 | [diff] [blame] | 146 | sender_->transport_.packet_overhead().bytes()); |
Sebastian Jansson | 98b07e9 | 2018-09-27 13:47:01 +0200 | [diff] [blame] | 147 | } |
| 148 | } |
| 149 | |
| 150 | SendAudioStream::~SendAudioStream() { |
| 151 | sender_->call_->DestroyAudioSendStream(send_stream_); |
| 152 | } |
| 153 | |
| 154 | void SendAudioStream::Start() { |
| 155 | send_stream_->Start(); |
Sebastian Jansson | 49a7843 | 2018-11-20 16:15:29 +0100 | [diff] [blame] | 156 | sender_->call_->SignalChannelNetworkState(MediaType::AUDIO, kNetworkUp); |
Sebastian Jansson | 98b07e9 | 2018-09-27 13:47:01 +0200 | [diff] [blame] | 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) { |
| 163 | AudioSendStream::Stats stats = send_stream_->GetStats(); |
| 164 | sb.AppendFormat("%.0lf", stats.target_bitrate_bps / 8.0); |
| 165 | }, |
| 166 | 64); |
| 167 | } |
| 168 | |
Sebastian Jansson | 98b07e9 | 2018-09-27 13:47:01 +0200 | [diff] [blame] | 169 | ReceiveAudioStream::ReceiveAudioStream( |
| 170 | CallClient* receiver, |
| 171 | AudioStreamConfig config, |
| 172 | SendAudioStream* send_stream, |
| 173 | rtc::scoped_refptr<AudioDecoderFactory> decoder_factory, |
| 174 | Transport* feedback_transport) |
| 175 | : receiver_(receiver), config_(config) { |
| 176 | AudioReceiveStream::Config recv_config; |
| 177 | recv_config.rtp.local_ssrc = CallTest::kReceiverLocalAudioSsrc; |
| 178 | recv_config.rtcp_send_transport = feedback_transport; |
| 179 | recv_config.rtp.remote_ssrc = send_stream->ssrc_; |
Sebastian Jansson | 800e121 | 2018-10-22 11:49:03 +0200 | [diff] [blame] | 180 | receiver->ssrc_media_types_[recv_config.rtp.remote_ssrc] = MediaType::AUDIO; |
Sebastian Jansson | 98b07e9 | 2018-09-27 13:47:01 +0200 | [diff] [blame] | 181 | if (config.stream.in_bandwidth_estimation) { |
| 182 | recv_config.rtp.transport_cc = true; |
| 183 | recv_config.rtp.extensions = { |
| 184 | {RtpExtension::kTransportSequenceNumberUri, 8}}; |
| 185 | } |
Sebastian Jansson | fd20171 | 2018-11-12 16:44:16 +0100 | [diff] [blame] | 186 | receiver_->AddExtensions(recv_config.rtp.extensions); |
Sebastian Jansson | 98b07e9 | 2018-09-27 13:47:01 +0200 | [diff] [blame] | 187 | recv_config.decoder_factory = decoder_factory; |
| 188 | recv_config.decoder_map = { |
| 189 | {CallTest::kAudioSendPayloadType, {"opus", 48000, 2}}}; |
| 190 | recv_config.sync_group = config.render.sync_group; |
| 191 | receive_stream_ = receiver_->call_->CreateAudioReceiveStream(recv_config); |
| 192 | } |
| 193 | ReceiveAudioStream::~ReceiveAudioStream() { |
| 194 | receiver_->call_->DestroyAudioReceiveStream(receive_stream_); |
| 195 | } |
| 196 | |
Sebastian Jansson | 49a7843 | 2018-11-20 16:15:29 +0100 | [diff] [blame] | 197 | void ReceiveAudioStream::Start() { |
| 198 | receive_stream_->Start(); |
| 199 | receiver_->call_->SignalChannelNetworkState(MediaType::AUDIO, kNetworkUp); |
| 200 | } |
| 201 | |
Sebastian Jansson | 98b07e9 | 2018-09-27 13:47:01 +0200 | [diff] [blame] | 202 | AudioStreamPair::~AudioStreamPair() = default; |
| 203 | |
| 204 | AudioStreamPair::AudioStreamPair( |
| 205 | CallClient* sender, |
Sebastian Jansson | 98b07e9 | 2018-09-27 13:47:01 +0200 | [diff] [blame] | 206 | rtc::scoped_refptr<AudioEncoderFactory> encoder_factory, |
| 207 | CallClient* receiver, |
Sebastian Jansson | 98b07e9 | 2018-09-27 13:47:01 +0200 | [diff] [blame] | 208 | rtc::scoped_refptr<AudioDecoderFactory> decoder_factory, |
| 209 | AudioStreamConfig config) |
| 210 | : config_(config), |
Sebastian Jansson | 800e121 | 2018-10-22 11:49:03 +0200 | [diff] [blame] | 211 | send_stream_(sender, config, encoder_factory, &sender->transport_), |
Sebastian Jansson | 98b07e9 | 2018-09-27 13:47:01 +0200 | [diff] [blame] | 212 | receive_stream_(receiver, |
| 213 | config, |
| 214 | &send_stream_, |
| 215 | decoder_factory, |
Sebastian Jansson | 800e121 | 2018-10-22 11:49:03 +0200 | [diff] [blame] | 216 | &receiver->transport_) {} |
Sebastian Jansson | 98b07e9 | 2018-09-27 13:47:01 +0200 | [diff] [blame] | 217 | |
| 218 | } // namespace test |
| 219 | } // namespace webrtc |