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 "absl/memory/memory.h"
13 #include "test/call_test.h"
14 
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 
25 namespace webrtc {
26 namespace test {
27 namespace {
28 enum : int {  // The first valid value is 1.
29   kTransportSequenceNumberExtensionId = 1,
30   kAbsSendTimeExtensionId
31 };
32 
CreateAdaptationString(AudioStreamConfig::NetworkAdaptation config)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
69 
SendAudioStream(CallClient * sender,AudioStreamConfig config,rtc::scoped_refptr<AudioEncoderFactory> encoder_factory,Transport * send_transport)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) {
76   AudioSendStream::Config send_config(send_transport);
77   ssrc_ = sender->GetNextAudioSsrc();
78   send_config.rtp.ssrc = ssrc_;
79   SdpAudioFormat::Parameters sdp_params;
80   if (config.source.channels == 2)
81     sdp_params["stereo"] = "1";
82   if (config.encoder.initial_frame_length != TimeDelta::Millis(20))
83     sdp_params["ptime"] =
84         std::to_string(config.encoder.initial_frame_length.ms());
85   if (config.encoder.enable_dtx)
86     sdp_params["usedtx"] = "1";
87 
88   // SdpAudioFormat::num_channels indicates that the encoder is capable of
89   // stereo, but the actual channel count used is based on the "stereo"
90   // parameter.
91   send_config.send_codec_spec = AudioSendStream::Config::SendCodecSpec(
92       CallTest::kAudioSendPayloadType, {"opus", 48000, 2, sdp_params});
93   RTC_DCHECK_LE(config.source.channels, 2);
94   send_config.encoder_factory = encoder_factory;
95 
96   if (config.encoder.fixed_rate)
97     send_config.send_codec_spec->target_bitrate_bps =
98         config.encoder.fixed_rate->bps();
99   if (!config.adapt.binary_proto.empty()) {
100     send_config.audio_network_adaptor_config = config.adapt.binary_proto;
101   } else if (config.network_adaptation) {
102     send_config.audio_network_adaptor_config =
103         CreateAdaptationString(config.adapt);
104   }
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     }
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;
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});
128   }
129 
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   });
137 }
138 
~SendAudioStream()139 SendAudioStream::~SendAudioStream() {
140   sender_->SendTask(
141       [this] { sender_->call_->DestroyAudioSendStream(send_stream_); });
142 }
143 
Start()144 void SendAudioStream::Start() {
145   sender_->SendTask([this] {
146     send_stream_->Start();
147     sender_->call_->SignalChannelNetworkState(MediaType::AUDIO, kNetworkUp);
148   });
149 }
150 
Stop()151 void SendAudioStream::Stop() {
152   sender_->SendTask([this] { send_stream_->Stop(); });
153 }
154 
SetMuted(bool mute)155 void SendAudioStream::SetMuted(bool mute) {
156   sender_->SendTask([this, mute] { send_stream_->SetMuted(mute); });
157 }
158 
StatsPrinter()159 ColumnPrinter SendAudioStream::StatsPrinter() {
160   return ColumnPrinter::Lambda(
161       "audio_target_rate",
162       [this](rtc::SimpleStringBuilder& sb) {
163         sender_->SendTask([this, &sb] {
164           AudioSendStream::Stats stats = send_stream_->GetStats();
165           sb.AppendFormat("%.0lf", stats.target_bitrate_bps / 8.0);
166         });
167       },
168       64);
169 }
170 
ReceiveAudioStream(CallClient * receiver,AudioStreamConfig config,SendAudioStream * send_stream,rtc::scoped_refptr<AudioDecoderFactory> decoder_factory,Transport * feedback_transport)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;
179   recv_config.rtp.local_ssrc = receiver_->GetNextAudioLocalSsrc();
180   recv_config.rtcp_send_transport = feedback_transport;
181   recv_config.rtp.remote_ssrc = send_stream->ssrc_;
182   receiver->ssrc_media_types_[recv_config.rtp.remote_ssrc] = MediaType::AUDIO;
183   if (config.stream.in_bandwidth_estimation) {
184     recv_config.rtp.transport_cc = true;
185     recv_config.rtp.extensions = {{RtpExtension::kTransportSequenceNumberUri,
186                                    kTransportSequenceNumberExtensionId}};
187   }
188   receiver_->AddExtensions(recv_config.rtp.extensions);
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;
193   receiver_->SendTask([&] {
194     receive_stream_ = receiver_->call_->CreateAudioReceiveStream(recv_config);
195   });
196 }
~ReceiveAudioStream()197 ReceiveAudioStream::~ReceiveAudioStream() {
198   receiver_->SendTask(
199       [&] { receiver_->call_->DestroyAudioReceiveStream(receive_stream_); });
200 }
201 
Start()202 void ReceiveAudioStream::Start() {
203   receiver_->SendTask([&] {
204     receive_stream_->Start();
205     receiver_->call_->SignalChannelNetworkState(MediaType::AUDIO, kNetworkUp);
206   });
207 }
208 
Stop()209 void ReceiveAudioStream::Stop() {
210   receiver_->SendTask([&] { receive_stream_->Stop(); });
211 }
212 
GetStats() const213 AudioReceiveStream::Stats ReceiveAudioStream::GetStats() const {
214   AudioReceiveStream::Stats result;
215   receiver_->SendTask([&] { result = receive_stream_->GetStats(); });
216   return result;
217 }
218 
219 AudioStreamPair::~AudioStreamPair() = default;
220 
AudioStreamPair(CallClient * sender,rtc::scoped_refptr<AudioEncoderFactory> encoder_factory,CallClient * receiver,rtc::scoped_refptr<AudioDecoderFactory> decoder_factory,AudioStreamConfig config)221 AudioStreamPair::AudioStreamPair(
222     CallClient* sender,
223     rtc::scoped_refptr<AudioEncoderFactory> encoder_factory,
224     CallClient* receiver,
225     rtc::scoped_refptr<AudioDecoderFactory> decoder_factory,
226     AudioStreamConfig config)
227     : config_(config),
228       send_stream_(sender, config, encoder_factory, sender->transport_.get()),
229       receive_stream_(receiver,
230                       config,
231                       &send_stream_,
232                       decoder_factory,
233                       receiver->transport_.get()) {}
234 
235 }  // namespace test
236 }  // namespace webrtc
237