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 | |
| 12 | #include "test/call_test.h" |
| 13 | |
Sebastian Jansson | b9972fa | 2018-10-17 14:27:55 | [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 | 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 { |
| 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 | 98b07e91 | 2018-09-27 11:47:01 | [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) { |
| 70 | AudioSendStream::Config send_config(send_transport); |
| 71 | ssrc_ = sender->GetNextAudioSsrc(); |
| 72 | send_config.rtp.ssrc = ssrc_; |
| 73 | SdpAudioFormat::Parameters sdp_params; |
| 74 | if (config.source.channels == 2) |
| 75 | sdp_params["stereo"] = "1"; |
| 76 | if (config.encoder.initial_frame_length != TimeDelta::ms(20)) |
| 77 | sdp_params["ptime"] = |
| 78 | std::to_string(config.encoder.initial_frame_length.ms()); |
| 79 | |
| 80 | // SdpAudioFormat::num_channels indicates that the encoder is capable of |
| 81 | // stereo, but the actual channel count used is based on the "stereo" |
| 82 | // parameter. |
| 83 | send_config.send_codec_spec = AudioSendStream::Config::SendCodecSpec( |
| 84 | CallTest::kAudioSendPayloadType, {"opus", 48000, 2, sdp_params}); |
| 85 | RTC_DCHECK_LE(config.source.channels, 2); |
| 86 | send_config.encoder_factory = encoder_factory; |
| 87 | |
| 88 | if (config.encoder.fixed_rate) |
| 89 | send_config.send_codec_spec->target_bitrate_bps = |
| 90 | config.encoder.fixed_rate->bps(); |
| 91 | |
Sebastian Jansson | b9972fa | 2018-10-17 14:27:55 | [diff] [blame] | 92 | if (config.network_adaptation) { |
| 93 | send_config.audio_network_adaptor_config = |
| 94 | CreateAdaptationString(config.adapt); |
| 95 | } |
Sebastian Jansson | 98b07e91 | 2018-09-27 11:47:01 | [diff] [blame] | 96 | if (config.encoder.allocate_bitrate || |
| 97 | config.stream.in_bandwidth_estimation) { |
| 98 | DataRate min_rate = DataRate::Infinity(); |
| 99 | DataRate max_rate = DataRate::Infinity(); |
| 100 | if (config.encoder.fixed_rate) { |
| 101 | min_rate = *config.encoder.fixed_rate; |
| 102 | max_rate = *config.encoder.fixed_rate; |
| 103 | } else { |
| 104 | min_rate = *config.encoder.min_rate; |
| 105 | max_rate = *config.encoder.max_rate; |
| 106 | } |
| 107 | if (field_trial::IsEnabled("WebRTC-SendSideBwe-WithOverhead")) { |
Sebastian Jansson | b9972fa | 2018-10-17 14:27:55 | [diff] [blame] | 108 | TimeDelta min_frame_length = config.encoder.initial_frame_length; |
Sebastian Jansson | b9972fa | 2018-10-17 14:27:55 | [diff] [blame] | 109 | TimeDelta max_frame_length = config.encoder.initial_frame_length; |
Sebastian Jansson | b9972fa | 2018-10-17 14:27:55 | [diff] [blame] | 110 | if (field_trial::IsEnabled("WebRTC-Audio-FrameLengthAdaptation") && |
| 111 | !config.adapt.frame.min_rate_for_20_ms.IsZero()) { |
| 112 | if (!config.adapt.frame.min_rate_for_60_ms.IsZero()) { |
| 113 | max_frame_length = TimeDelta::ms(120); |
| 114 | } else { |
| 115 | max_frame_length = TimeDelta::ms(60); |
| 116 | } |
| 117 | } |
Sebastian Jansson | 98b07e91 | 2018-09-27 11:47:01 | [diff] [blame] | 118 | DataSize rtp_overhead = DataSize::bytes(12); |
Sebastian Jansson | 800e121 | 2018-10-22 09:49:03 | [diff] [blame^] | 119 | DataSize total_overhead = |
| 120 | sender_->transport_.packet_overhead() + rtp_overhead; |
Sebastian Jansson | b9972fa | 2018-10-17 14:27:55 | [diff] [blame] | 121 | min_rate += total_overhead / max_frame_length; |
| 122 | max_rate += total_overhead / min_frame_length; |
Sebastian Jansson | 98b07e91 | 2018-09-27 11:47:01 | [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 13:21:55 | [diff] [blame] | 139 | sender->call_->OnAudioTransportOverheadChanged( |
Sebastian Jansson | 800e121 | 2018-10-22 09:49:03 | [diff] [blame^] | 140 | sender_->transport_.packet_overhead().bytes()); |
Sebastian Jansson | 98b07e91 | 2018-09-27 11:47:01 | [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 | 98b07e91 | 2018-09-27 11:47:01 | [diff] [blame] | 152 | ReceiveAudioStream::ReceiveAudioStream( |
| 153 | CallClient* receiver, |
| 154 | AudioStreamConfig config, |
| 155 | SendAudioStream* send_stream, |
| 156 | rtc::scoped_refptr<AudioDecoderFactory> decoder_factory, |
| 157 | Transport* feedback_transport) |
| 158 | : receiver_(receiver), config_(config) { |
| 159 | AudioReceiveStream::Config recv_config; |
| 160 | recv_config.rtp.local_ssrc = CallTest::kReceiverLocalAudioSsrc; |
| 161 | recv_config.rtcp_send_transport = feedback_transport; |
| 162 | recv_config.rtp.remote_ssrc = send_stream->ssrc_; |
Sebastian Jansson | 800e121 | 2018-10-22 09:49:03 | [diff] [blame^] | 163 | receiver->ssrc_media_types_[recv_config.rtp.remote_ssrc] = MediaType::AUDIO; |
Sebastian Jansson | 98b07e91 | 2018-09-27 11:47:01 | [diff] [blame] | 164 | if (config.stream.in_bandwidth_estimation) { |
| 165 | recv_config.rtp.transport_cc = true; |
| 166 | recv_config.rtp.extensions = { |
| 167 | {RtpExtension::kTransportSequenceNumberUri, 8}}; |
| 168 | } |
| 169 | recv_config.decoder_factory = decoder_factory; |
| 170 | recv_config.decoder_map = { |
| 171 | {CallTest::kAudioSendPayloadType, {"opus", 48000, 2}}}; |
| 172 | recv_config.sync_group = config.render.sync_group; |
| 173 | receive_stream_ = receiver_->call_->CreateAudioReceiveStream(recv_config); |
| 174 | } |
| 175 | ReceiveAudioStream::~ReceiveAudioStream() { |
| 176 | receiver_->call_->DestroyAudioReceiveStream(receive_stream_); |
| 177 | } |
| 178 | |
Sebastian Jansson | 98b07e91 | 2018-09-27 11:47:01 | [diff] [blame] | 179 | AudioStreamPair::~AudioStreamPair() = default; |
| 180 | |
| 181 | AudioStreamPair::AudioStreamPair( |
| 182 | CallClient* sender, |
Sebastian Jansson | 98b07e91 | 2018-09-27 11:47:01 | [diff] [blame] | 183 | rtc::scoped_refptr<AudioEncoderFactory> encoder_factory, |
| 184 | CallClient* receiver, |
Sebastian Jansson | 98b07e91 | 2018-09-27 11:47:01 | [diff] [blame] | 185 | rtc::scoped_refptr<AudioDecoderFactory> decoder_factory, |
| 186 | AudioStreamConfig config) |
| 187 | : config_(config), |
Sebastian Jansson | 800e121 | 2018-10-22 09:49:03 | [diff] [blame^] | 188 | send_stream_(sender, config, encoder_factory, &sender->transport_), |
Sebastian Jansson | 98b07e91 | 2018-09-27 11:47:01 | [diff] [blame] | 189 | receive_stream_(receiver, |
| 190 | config, |
| 191 | &send_stream_, |
| 192 | decoder_factory, |
Sebastian Jansson | 800e121 | 2018-10-22 09:49:03 | [diff] [blame^] | 193 | &receiver->transport_) {} |
Sebastian Jansson | 98b07e91 | 2018-09-27 11:47:01 | [diff] [blame] | 194 | |
| 195 | } // namespace test |
| 196 | } // namespace webrtc |