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