blob: 8730c452582180ff355934aad554735d3d6bbb8c [file] [log] [blame]
solenbergc7a8b082015-10-16 21:35:071/*
2 * Copyright (c) 2015 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
Mirko Bonadei92ea95e2017-09-15 04:47:3111#include "audio/audio_send_stream.h"
solenbergc7a8b082015-10-16 21:35:0712
Mirko Bonadei317a1f02019-09-17 15:06:1813#include <memory>
solenbergc7a8b082015-10-16 21:35:0714#include <string>
ossu20a4b3f2017-04-27 09:08:5215#include <utility>
16#include <vector>
solenbergc7a8b082015-10-16 21:35:0717
Yves Gerey988cc082018-10-23 10:03:0118#include "api/audio_codecs/audio_encoder.h"
19#include "api/audio_codecs/audio_encoder_factory.h"
20#include "api/audio_codecs/audio_format.h"
21#include "api/call/transport.h"
Steve Anton10542f22019-01-11 17:11:0022#include "api/crypto/frame_encryptor_interface.h"
Artem Titov741daaf2019-03-21 13:37:3623#include "api/function_view.h"
Danil Chapovalov83bbe912019-08-07 10:24:5324#include "api/rtc_event_log/rtc_event_log.h"
Mirko Bonadei92ea95e2017-09-15 04:47:3125#include "audio/audio_state.h"
Yves Gerey988cc082018-10-23 10:03:0126#include "audio/channel_send.h"
Mirko Bonadei92ea95e2017-09-15 04:47:3127#include "audio/conversion.h"
Yves Gerey988cc082018-10-23 10:03:0128#include "call/rtp_config.h"
Mirko Bonadei92ea95e2017-09-15 04:47:3129#include "call/rtp_transport_controller_send_interface.h"
Yves Gerey988cc082018-10-23 10:03:0130#include "common_audio/vad/include/vad.h"
Oskar Sundbom56ef3052018-10-30 15:11:0231#include "logging/rtc_event_log/events/rtc_event_audio_send_stream_config.h"
Oskar Sundbom56ef3052018-10-30 15:11:0232#include "logging/rtc_event_log/rtc_stream_config.h"
Mirko Bonadei92ea95e2017-09-15 04:47:3133#include "modules/audio_coding/codecs/cng/audio_encoder_cng.h"
Yves Gerey988cc082018-10-23 10:03:0134#include "modules/audio_processing/include/audio_processing.h"
Sebastian Jansson6298b562020-01-14 16:55:1935#include "modules/rtp_rtcp/source/rtp_header_extensions.h"
Mirko Bonadei92ea95e2017-09-15 04:47:3136#include "rtc_base/checks.h"
37#include "rtc_base/event.h"
Mirko Bonadei92ea95e2017-09-15 04:47:3138#include "rtc_base/logging.h"
Jonas Olssonabbe8412018-04-03 11:40:0539#include "rtc_base/strings/audio_format_to_string.h"
Mirko Bonadei92ea95e2017-09-15 04:47:3140#include "rtc_base/task_queue.h"
Alex Narestcedd3512017-12-07 19:54:5541#include "system_wrappers/include/field_trial.h"
solenbergc7a8b082015-10-16 21:35:0742
43namespace webrtc {
Fredrik Solenberg8f5787a2018-01-11 12:52:3044namespace {
elad.alond12a8e12017-03-23 18:04:4845
Oskar Sundbom56ef3052018-10-30 15:11:0246void UpdateEventLogStreamConfig(RtcEventLog* event_log,
47 const AudioSendStream::Config& config,
48 const AudioSendStream::Config* old_config) {
49 using SendCodecSpec = AudioSendStream::Config::SendCodecSpec;
50 // Only update if any of the things we log have changed.
51 auto payload_types_equal = [](const absl::optional<SendCodecSpec>& a,
52 const absl::optional<SendCodecSpec>& b) {
53 if (a.has_value() && b.has_value()) {
54 return a->format.name == b->format.name &&
55 a->payload_type == b->payload_type;
56 }
57 return !a.has_value() && !b.has_value();
58 };
59
60 if (old_config && config.rtp.ssrc == old_config->rtp.ssrc &&
61 config.rtp.extensions == old_config->rtp.extensions &&
62 payload_types_equal(config.send_codec_spec,
63 old_config->send_codec_spec)) {
64 return;
65 }
66
Mirko Bonadei317a1f02019-09-17 15:06:1867 auto rtclog_config = std::make_unique<rtclog::StreamConfig>();
Oskar Sundbom56ef3052018-10-30 15:11:0268 rtclog_config->local_ssrc = config.rtp.ssrc;
69 rtclog_config->rtp_extensions = config.rtp.extensions;
70 if (config.send_codec_spec) {
71 rtclog_config->codecs.emplace_back(config.send_codec_spec->format.name,
72 config.send_codec_spec->payload_type, 0);
73 }
Mirko Bonadei317a1f02019-09-17 15:06:1874 event_log->Log(std::make_unique<RtcEventAudioSendStreamConfig>(
Oskar Sundbom56ef3052018-10-30 15:11:0275 std::move(rtclog_config)));
76}
ossu20a4b3f2017-04-27 09:08:5277} // namespace
78
Sebastian Janssonf23131f2019-10-03 08:03:5579constexpr char AudioAllocationConfig::kKey[];
80
81std::unique_ptr<StructParametersParser> AudioAllocationConfig::Parser() {
82 return StructParametersParser::Create( //
83 "min", &min_bitrate, //
84 "max", &max_bitrate, //
85 "prio_rate", &priority_bitrate, //
86 "prio_rate_raw", &priority_bitrate_raw, //
87 "rate_prio", &bitrate_priority);
88}
89
90AudioAllocationConfig::AudioAllocationConfig() {
91 Parser()->Parse(field_trial::FindFullName(kKey));
92 if (priority_bitrate_raw && !priority_bitrate.IsZero()) {
93 RTC_LOG(LS_WARNING) << "'priority_bitrate' and '_raw' are mutually "
94 "exclusive but both were configured.";
95 }
96}
97
98namespace internal {
solenberg566ef242015-11-06 23:34:4999AudioSendStream::AudioSendStream(
Sebastian Jansson977b3352019-03-04 16:43:34100 Clock* clock,
solenberg566ef242015-11-06 23:34:49101 const webrtc::AudioSendStream::Config& config,
Stefan Holmerb86d4e42015-12-07 09:26:18102 const rtc::scoped_refptr<webrtc::AudioState>& audio_state,
Sebastian Jansson44dd9f22019-03-08 13:50:30103 TaskQueueFactory* task_queue_factory,
Fredrik Solenberg8f5787a2018-01-11 12:52:30104 ProcessThread* module_process_thread,
Niels Möller7d76a312018-10-26 10:57:07105 RtpTransportControllerSendInterface* rtp_transport,
Niels Möller67b011d2018-10-22 11:00:40106 BitrateAllocatorInterface* bitrate_allocator,
michaelt9332b7d2016-11-30 15:51:13107 RtcEventLog* event_log,
ossuc3d4b482017-05-23 13:07:11108 RtcpRttStats* rtcp_rtt_stats,
Sam Zackrissonff058162018-11-20 16:15:13109 const absl::optional<RtpState>& suspended_rtp_state)
Sebastian Jansson977b3352019-03-04 16:43:34110 : AudioSendStream(clock,
111 config,
Fredrik Solenberg8f5787a2018-01-11 12:52:30112 audio_state,
Sebastian Jansson44dd9f22019-03-08 13:50:30113 task_queue_factory,
Niels Möller7d76a312018-10-26 10:57:07114 rtp_transport,
Fredrik Solenberg8f5787a2018-01-11 12:52:30115 bitrate_allocator,
116 event_log,
Fredrik Solenberg8f5787a2018-01-11 12:52:30117 suspended_rtp_state,
Sebastian Jansson977b3352019-03-04 16:43:34118 voe::CreateChannelSend(clock,
Sebastian Jansson44dd9f22019-03-08 13:50:30119 task_queue_factory,
Niels Möllerdced9f62018-11-19 09:27:07120 module_process_thread,
Niels Möllere9771992018-11-26 09:55:07121 config.send_transport,
Niels Möllerdced9f62018-11-19 09:27:07122 rtcp_rtt_stats,
123 event_log,
124 config.frame_encryptor,
125 config.crypto_options,
126 config.rtp.extmap_allow_mixed,
Erik Språng4c2c4122019-07-11 13:20:15127 config.rtcp_report_interval_ms,
Marina Ciocead2aa8f92020-03-31 09:29:56128 config.rtp.ssrc,
129 config.frame_transformer)) {}
Fredrik Solenberg8f5787a2018-01-11 12:52:30130
131AudioSendStream::AudioSendStream(
Sebastian Jansson977b3352019-03-04 16:43:34132 Clock* clock,
Fredrik Solenberg8f5787a2018-01-11 12:52:30133 const webrtc::AudioSendStream::Config& config,
134 const rtc::scoped_refptr<webrtc::AudioState>& audio_state,
Sebastian Jansson44dd9f22019-03-08 13:50:30135 TaskQueueFactory* task_queue_factory,
Niels Möller7d76a312018-10-26 10:57:07136 RtpTransportControllerSendInterface* rtp_transport,
Niels Möller67b011d2018-10-22 11:00:40137 BitrateAllocatorInterface* bitrate_allocator,
Fredrik Solenberg8f5787a2018-01-11 12:52:30138 RtcEventLog* event_log,
Danil Chapovalovb9b146c2018-06-15 10:28:07139 const absl::optional<RtpState>& suspended_rtp_state,
Niels Möllerdced9f62018-11-19 09:27:07140 std::unique_ptr<voe::ChannelSendInterface> channel_send)
Sebastian Jansson977b3352019-03-04 16:43:34141 : clock_(clock),
Sebastian Jansson0b698262019-03-07 08:17:19142 worker_queue_(rtp_transport->GetWorkerQueue()),
Sebastian Janssonf23131f2019-10-03 08:03:55143 audio_send_side_bwe_(field_trial::IsEnabled("WebRTC-Audio-SendSideBwe")),
144 allocate_audio_without_feedback_(
145 field_trial::IsEnabled("WebRTC-Audio-ABWENoTWCC")),
146 enable_audio_alr_probing_(
147 !field_trial::IsDisabled("WebRTC-Audio-AlrProbing")),
148 send_side_bwe_with_overhead_(
149 field_trial::IsEnabled("WebRTC-SendSideBwe-WithOverhead")),
Bjorn A Mellem7a9a0922019-11-26 17:19:40150 config_(Config(/*send_transport=*/nullptr)),
mflodman86cc6ff2016-07-26 11:44:06151 audio_state_(audio_state),
Niels Möllerdced9f62018-11-19 09:27:07152 channel_send_(std::move(channel_send)),
ossu20a4b3f2017-04-27 09:08:52153 event_log_(event_log),
Sebastian Jansson62aee932019-10-02 10:27:06154 use_legacy_overhead_calculation_(
Sebastian Janssonbef818d2020-01-30 13:09:48155 field_trial::IsEnabled("WebRTC-Audio-LegacyOverhead")),
michaeltf4caaab2017-01-17 07:55:07156 bitrate_allocator_(bitrate_allocator),
Niels Möller7d76a312018-10-26 10:57:07157 rtp_transport_(rtp_transport),
Sebastian Jansson6298b562020-01-14 16:55:19158 rtp_rtcp_module_(channel_send_->GetRtpRtcp()),
Sam Zackrissonff058162018-11-20 16:15:13159 suspended_rtp_state_(suspended_rtp_state) {
Jonas Olsson24ea8222018-01-25 09:14:29160 RTC_LOG(LS_INFO) << "AudioSendStream: " << config.rtp.ssrc;
Fredrik Solenberg8f5787a2018-01-11 12:52:30161 RTC_DCHECK(worker_queue_);
162 RTC_DCHECK(audio_state_);
Niels Möllerdced9f62018-11-19 09:27:07163 RTC_DCHECK(channel_send_);
Fredrik Solenberg8f5787a2018-01-11 12:52:30164 RTC_DCHECK(bitrate_allocator_);
Sebastian Jansson0b698262019-03-07 08:17:19165 RTC_DCHECK(rtp_transport);
166
ossuc3d4b482017-05-23 13:07:11167 RTC_DCHECK(rtp_rtcp_module_);
mflodman3d7db262016-04-29 07:57:13168
Sebastian Jansson35cf9e72019-10-04 07:30:32169 ConfigureStream(config, true);
elad.alond12a8e12017-03-23 18:04:48170
Sebastian Janssonc01367d2019-04-08 13:20:44171 pacer_thread_checker_.Detach();
solenbergc7a8b082015-10-16 21:35:07172}
173
174AudioSendStream::~AudioSendStream() {
Sebastian Janssonc01367d2019-04-08 13:20:44175 RTC_DCHECK(worker_thread_checker_.IsCurrent());
Jonas Olsson24ea8222018-01-25 09:14:29176 RTC_LOG(LS_INFO) << "~AudioSendStream: " << config_.rtp.ssrc;
Fredrik Solenberg2a877972017-12-15 15:42:15177 RTC_DCHECK(!sending_);
Sebastian Jansson0a6510d2019-10-04 07:31:08178 channel_send_->ResetSenderCongestionControlObjects();
Sebastian Jansson8672cac2019-03-01 14:57:55179 // Blocking call to synchronize state with worker queue to ensure that there
180 // are no pending tasks left that keeps references to audio.
181 rtc::Event thread_sync_event;
182 worker_queue_->PostTask([&] { thread_sync_event.Set(); });
183 thread_sync_event.Wait(rtc::Event::kForever);
solenbergc7a8b082015-10-16 21:35:07184}
185
eladalonabbc4302017-07-26 09:09:44186const webrtc::AudioSendStream::Config& AudioSendStream::GetConfig() const {
Sebastian Janssonc01367d2019-04-08 13:20:44187 RTC_DCHECK(worker_thread_checker_.IsCurrent());
eladalonabbc4302017-07-26 09:09:44188 return config_;
189}
190
ossu20a4b3f2017-04-27 09:08:52191void AudioSendStream::Reconfigure(
192 const webrtc::AudioSendStream::Config& new_config) {
Sebastian Janssonc01367d2019-04-08 13:20:44193 RTC_DCHECK(worker_thread_checker_.IsCurrent());
Sebastian Jansson35cf9e72019-10-04 07:30:32194 ConfigureStream(new_config, false);
ossu20a4b3f2017-04-27 09:08:52195}
196
Alex Narestcedd3512017-12-07 19:54:55197AudioSendStream::ExtensionIds AudioSendStream::FindExtensionIds(
198 const std::vector<RtpExtension>& extensions) {
199 ExtensionIds ids;
200 for (const auto& extension : extensions) {
201 if (extension.uri == RtpExtension::kAudioLevelUri) {
202 ids.audio_level = extension.id;
Sebastian Jansson71c6b562019-08-14 09:31:02203 } else if (extension.uri == RtpExtension::kAbsSendTimeUri) {
204 ids.abs_send_time = extension.id;
Alex Narestcedd3512017-12-07 19:54:55205 } else if (extension.uri == RtpExtension::kTransportSequenceNumberUri) {
206 ids.transport_sequence_number = extension.id;
Steve Antonbb50ce52018-03-26 17:24:32207 } else if (extension.uri == RtpExtension::kMidUri) {
208 ids.mid = extension.id;
Amit Hilbuch77938e62018-12-21 17:23:38209 } else if (extension.uri == RtpExtension::kRidUri) {
210 ids.rid = extension.id;
211 } else if (extension.uri == RtpExtension::kRepairedRidUri) {
212 ids.repaired_rid = extension.id;
Minyue Li74dadc12020-03-05 10:33:13213 } else if (extension.uri == RtpExtension::kAbsoluteCaptureTimeUri) {
214 ids.abs_capture_time = extension.id;
Alex Narestcedd3512017-12-07 19:54:55215 }
216 }
217 return ids;
218}
219
Sebastian Jansson470a5ea2019-01-23 11:37:49220int AudioSendStream::TransportSeqNumId(const AudioSendStream::Config& config) {
221 return FindExtensionIds(config.rtp.extensions).transport_sequence_number;
222}
223
ossu20a4b3f2017-04-27 09:08:52224void AudioSendStream::ConfigureStream(
ossu20a4b3f2017-04-27 09:08:52225 const webrtc::AudioSendStream::Config& new_config,
226 bool first_time) {
Jonas Olsson24ea8222018-01-25 09:14:29227 RTC_LOG(LS_INFO) << "AudioSendStream::ConfigureStream: "
228 << new_config.ToString();
Sebastian Jansson35cf9e72019-10-04 07:30:32229 UpdateEventLogStreamConfig(event_log_, new_config,
230 first_time ? nullptr : &config_);
Oskar Sundbom56ef3052018-10-30 15:11:02231
Sebastian Jansson35cf9e72019-10-04 07:30:32232 const auto& old_config = config_;
ossu20a4b3f2017-04-27 09:08:52233
Niels Möllere9771992018-11-26 09:55:07234 // Configuration parameters which cannot be changed.
235 RTC_DCHECK(first_time ||
236 old_config.send_transport == new_config.send_transport);
Erik Språng70efdde2019-08-21 11:36:20237 RTC_DCHECK(first_time || old_config.rtp.ssrc == new_config.rtp.ssrc);
Sebastian Jansson35cf9e72019-10-04 07:30:32238 if (suspended_rtp_state_ && first_time) {
239 rtp_rtcp_module_->SetRtpState(*suspended_rtp_state_);
ossu20a4b3f2017-04-27 09:08:52240 }
241 if (first_time || old_config.rtp.c_name != new_config.rtp.c_name) {
Sebastian Jansson35cf9e72019-10-04 07:30:32242 channel_send_->SetRTCP_CNAME(new_config.rtp.c_name);
ossu20a4b3f2017-04-27 09:08:52243 }
ossu20a4b3f2017-04-27 09:08:52244
Benjamin Wright84583f62018-10-04 21:22:34245 // Enable the frame encryptor if a new frame encryptor has been provided.
246 if (first_time || new_config.frame_encryptor != old_config.frame_encryptor) {
Sebastian Jansson35cf9e72019-10-04 07:30:32247 channel_send_->SetFrameEncryptor(new_config.frame_encryptor);
Benjamin Wright84583f62018-10-04 21:22:34248 }
249
Johannes Kron9190b822018-10-29 10:22:05250 if (first_time ||
Marina Ciocead2aa8f92020-03-31 09:29:56251 new_config.frame_transformer != old_config.frame_transformer) {
252 channel_send_->SetEncoderToPacketizerFrameTransformer(
253 new_config.frame_transformer);
254 }
255
256 if (first_time ||
Johannes Kron9190b822018-10-29 10:22:05257 new_config.rtp.extmap_allow_mixed != old_config.rtp.extmap_allow_mixed) {
Sebastian Jansson6298b562020-01-14 16:55:19258 rtp_rtcp_module_->SetExtmapAllowMixed(new_config.rtp.extmap_allow_mixed);
Johannes Kron9190b822018-10-29 10:22:05259 }
260
Alex Narestcedd3512017-12-07 19:54:55261 const ExtensionIds old_ids = FindExtensionIds(old_config.rtp.extensions);
262 const ExtensionIds new_ids = FindExtensionIds(new_config.rtp.extensions);
Yves Gerey17048012019-07-26 15:49:52263
ossu20a4b3f2017-04-27 09:08:52264 // Audio level indication
265 if (first_time || new_ids.audio_level != old_ids.audio_level) {
Sebastian Jansson35cf9e72019-10-04 07:30:32266 channel_send_->SetSendAudioLevelIndicationStatus(new_ids.audio_level != 0,
267 new_ids.audio_level);
ossu20a4b3f2017-04-27 09:08:52268 }
Sebastian Jansson71c6b562019-08-14 09:31:02269
270 if (first_time || new_ids.abs_send_time != old_ids.abs_send_time) {
Sebastian Jansson6298b562020-01-14 16:55:19271 rtp_rtcp_module_->DeregisterSendRtpHeaderExtension(
Sebastian Jansson71c6b562019-08-14 09:31:02272 kRtpExtensionAbsoluteSendTime);
273 if (new_ids.abs_send_time) {
Sebastian Janssonf39c8152019-10-14 15:32:21274 rtp_rtcp_module_->RegisterRtpHeaderExtension(AbsoluteSendTime::kUri,
275 new_ids.abs_send_time);
Sebastian Jansson71c6b562019-08-14 09:31:02276 }
277 }
278
Sebastian Jansson8d9c5402017-11-15 16:22:16279 bool transport_seq_num_id_changed =
280 new_ids.transport_sequence_number != old_ids.transport_sequence_number;
Sebastian Jansson35cf9e72019-10-04 07:30:32281 if (first_time ||
282 (transport_seq_num_id_changed && !allocate_audio_without_feedback_)) {
ossu1129df22017-06-30 08:38:56283 if (!first_time) {
Sebastian Jansson35cf9e72019-10-04 07:30:32284 channel_send_->ResetSenderCongestionControlObjects();
ossu20a4b3f2017-04-27 09:08:52285 }
286
Sebastian Jansson8d9c5402017-11-15 16:22:16287 RtcpBandwidthObserver* bandwidth_observer = nullptr;
Sebastian Jansson470a5ea2019-01-23 11:37:49288
Sebastian Jansson35cf9e72019-10-04 07:30:32289 if (audio_send_side_bwe_ && !allocate_audio_without_feedback_ &&
Sebastian Janssonf23131f2019-10-03 08:03:55290 new_ids.transport_sequence_number != 0) {
Sebastian Jansson6298b562020-01-14 16:55:19291 rtp_rtcp_module_->RegisterRtpHeaderExtension(
292 TransportSequenceNumber::kUri, new_ids.transport_sequence_number);
Sebastian Jansson8d9c5402017-11-15 16:22:16293 // Probing in application limited region is only used in combination with
294 // send side congestion control, wich depends on feedback packets which
295 // requires transport sequence numbers to be enabled.
Sebastian Jansson0a6510d2019-10-04 07:31:08296 // Optionally request ALR probing but do not override any existing
297 // request from other streams.
298 if (enable_audio_alr_probing_) {
299 rtp_transport_->EnablePeriodicAlrProbing(true);
Niels Möller7d76a312018-10-26 10:57:07300 }
Sebastian Jansson0a6510d2019-10-04 07:31:08301 bandwidth_observer = rtp_transport_->GetBandwidthObserver();
ossu20a4b3f2017-04-27 09:08:52302 }
Sebastian Jansson0a6510d2019-10-04 07:31:08303 channel_send_->RegisterSenderCongestionControlObjects(rtp_transport_,
304 bandwidth_observer);
ossu20a4b3f2017-04-27 09:08:52305 }
Steve Antonbb50ce52018-03-26 17:24:32306 // MID RTP header extension.
Steve Anton003930a2018-03-29 19:37:21307 if ((first_time || new_ids.mid != old_ids.mid ||
308 new_config.rtp.mid != old_config.rtp.mid) &&
309 new_ids.mid != 0 && !new_config.rtp.mid.empty()) {
Sebastian Jansson6298b562020-01-14 16:55:19310 rtp_rtcp_module_->RegisterRtpHeaderExtension(RtpMid::kUri, new_ids.mid);
311 rtp_rtcp_module_->SetMid(new_config.rtp.mid);
Steve Antonbb50ce52018-03-26 17:24:32312 }
313
Amit Hilbuch77938e62018-12-21 17:23:38314 // RID RTP header extension
315 if ((first_time || new_ids.rid != old_ids.rid ||
316 new_ids.repaired_rid != old_ids.repaired_rid ||
317 new_config.rtp.rid != old_config.rtp.rid)) {
Sebastian Jansson6298b562020-01-14 16:55:19318 if (new_ids.rid != 0 || new_ids.repaired_rid != 0) {
319 if (new_config.rtp.rid.empty()) {
320 rtp_rtcp_module_->DeregisterSendRtpHeaderExtension(RtpStreamId::kUri);
321 } else if (new_ids.repaired_rid != 0) {
322 rtp_rtcp_module_->RegisterRtpHeaderExtension(RtpStreamId::kUri,
323 new_ids.repaired_rid);
324 } else {
325 rtp_rtcp_module_->RegisterRtpHeaderExtension(RtpStreamId::kUri,
326 new_ids.rid);
327 }
328 }
329 rtp_rtcp_module_->SetRid(new_config.rtp.rid);
Amit Hilbuch77938e62018-12-21 17:23:38330 }
331
Minyue Li74dadc12020-03-05 10:33:13332 if (first_time || new_ids.abs_capture_time != old_ids.abs_capture_time) {
333 rtp_rtcp_module_->DeregisterSendRtpHeaderExtension(
334 kRtpExtensionAbsoluteCaptureTime);
335 if (new_ids.abs_capture_time) {
336 rtp_rtcp_module_->RegisterRtpHeaderExtension(
337 AbsoluteCaptureTimeExtension::kUri, new_ids.abs_capture_time);
338 }
339 }
340
Sebastian Jansson35cf9e72019-10-04 07:30:32341 if (!ReconfigureSendCodec(new_config)) {
Mirko Bonadei675513b2017-11-09 10:09:25342 RTC_LOG(LS_ERROR) << "Failed to set up send codec state.";
ossu20a4b3f2017-04-27 09:08:52343 }
344
Erik Språng04e1bab2020-05-07 16:18:32345 // Set currently known overhead (used in ANA, opus only).
346 {
347 rtc::CritScope cs(&overhead_per_packet_lock_);
348 UpdateOverheadForEncoder();
349 }
350
Jakob Ivarssond14525e2020-03-06 08:49:29351 channel_send_->CallEncoder([this](AudioEncoder* encoder) {
352 if (!encoder) {
353 return;
354 }
355 worker_queue_->PostTask(
356 [this, length_range = encoder->GetFrameLengthRange()] {
357 RTC_DCHECK_RUN_ON(worker_queue_);
358 frame_length_range_ = length_range;
359 });
360 });
361
Sebastian Jansson35cf9e72019-10-04 07:30:32362 if (sending_) {
363 ReconfigureBitrateObserver(new_config);
Oskar Sundbomf85e31b2017-12-20 15:38:09364 }
Sebastian Jansson35cf9e72019-10-04 07:30:32365 config_ = new_config;
ossu20a4b3f2017-04-27 09:08:52366}
367
solenberg3a941542015-11-16 15:34:50368void AudioSendStream::Start() {
Sebastian Jansson8672cac2019-03-01 14:57:55369 RTC_DCHECK_RUN_ON(&worker_thread_checker_);
Fredrik Solenberg2a877972017-12-15 15:42:15370 if (sending_) {
371 return;
372 }
Sebastian Janssonf23131f2019-10-03 08:03:55373 if (!config_.has_dscp && config_.min_bitrate_bps != -1 &&
374 config_.max_bitrate_bps != -1 &&
Sebastian Janssoncd0eedb2019-10-10 11:52:26375 (allocate_audio_without_feedback_ || TransportSeqNumId(config_) != 0)) {
Erik Språngaa59eca2019-07-24 12:52:55376 rtp_transport_->AccountForAudioPacketsInPacedSender(true);
Sebastian Janssonc3eb9fd2020-01-29 16:42:52377 if (send_side_bwe_with_overhead_)
378 rtp_transport_->IncludeOverheadInPacedSender();
Sebastian Janssonb6863962018-10-10 08:23:13379 rtp_rtcp_module_->SetAsPartOfAllocation(true);
Sebastian Jansson8672cac2019-03-01 14:57:55380 rtc::Event thread_sync_event;
381 worker_queue_->PostTask([&] {
382 RTC_DCHECK_RUN_ON(worker_queue_);
383 ConfigureBitrateObserver();
384 thread_sync_event.Set();
385 });
386 thread_sync_event.Wait(rtc::Event::kForever);
Sebastian Janssonb6863962018-10-10 08:23:13387 } else {
388 rtp_rtcp_module_->SetAsPartOfAllocation(false);
mflodman86cc6ff2016-07-26 11:44:06389 }
Niels Möllerdced9f62018-11-19 09:27:07390 channel_send_->StartSend();
Fredrik Solenberg2a877972017-12-15 15:42:15391 sending_ = true;
392 audio_state()->AddSendingStream(this, encoder_sample_rate_hz_,
393 encoder_num_channels_);
solenberg3a941542015-11-16 15:34:50394}
395
396void AudioSendStream::Stop() {
Sebastian Janssonc01367d2019-04-08 13:20:44397 RTC_DCHECK(worker_thread_checker_.IsCurrent());
Fredrik Solenberg2a877972017-12-15 15:42:15398 if (!sending_) {
399 return;
400 }
401
ossu20a4b3f2017-04-27 09:08:52402 RemoveBitrateObserver();
Niels Möllerdced9f62018-11-19 09:27:07403 channel_send_->StopSend();
Fredrik Solenberg2a877972017-12-15 15:42:15404 sending_ = false;
405 audio_state()->RemoveSendingStream(this);
406}
407
408void AudioSendStream::SendAudioData(std::unique_ptr<AudioFrame> audio_frame) {
409 RTC_CHECK_RUNS_SERIALIZED(&audio_capture_race_checker_);
Henrik Boströmd2c336f2019-07-03 15:11:10410 RTC_DCHECK_GT(audio_frame->sample_rate_hz_, 0);
411 double duration = static_cast<double>(audio_frame->samples_per_channel_) /
412 audio_frame->sample_rate_hz_;
413 {
414 // Note: SendAudioData() passes the frame further down the pipeline and it
415 // may eventually get sent. But this method is invoked even if we are not
416 // connected, as long as we have an AudioSendStream (created as a result of
417 // an O/A exchange). This means that we are calculating audio levels whether
418 // or not we are sending samples.
419 // TODO(https://crbug.com/webrtc/10771): All "media-source" related stats
420 // should move from send-streams to the local audio sources or tracks; a
421 // send-stream should not be required to read the microphone audio levels.
422 rtc::CritScope cs(&audio_level_lock_);
423 audio_level_.ComputeLevel(*audio_frame, duration);
424 }
Niels Möllerdced9f62018-11-19 09:27:07425 channel_send_->ProcessAndEncodeAudio(std::move(audio_frame));
solenberg3a941542015-11-16 15:34:50426}
427
solenbergffbbcac2016-11-17 13:25:37428bool AudioSendStream::SendTelephoneEvent(int payload_type,
Yves Gerey665174f2018-06-19 13:03:05429 int payload_frequency,
430 int event,
solenberg8842c3e2016-03-11 11:06:41431 int duration_ms) {
Sebastian Janssonc01367d2019-04-08 13:20:44432 RTC_DCHECK(worker_thread_checker_.IsCurrent());
Niels Möller8fb1a6a2019-03-05 13:29:42433 channel_send_->SetSendTelephoneEventPayloadType(payload_type,
434 payload_frequency);
435 return channel_send_->SendTelephoneEventOutband(event, duration_ms);
Fredrik Solenbergb5727682015-12-04 14:22:19436}
437
solenberg94218532016-06-16 17:53:22438void AudioSendStream::SetMuted(bool muted) {
Sebastian Janssonc01367d2019-04-08 13:20:44439 RTC_DCHECK(worker_thread_checker_.IsCurrent());
Niels Möllerdced9f62018-11-19 09:27:07440 channel_send_->SetInputMute(muted);
solenberg94218532016-06-16 17:53:22441}
442
solenbergc7a8b082015-10-16 21:35:07443webrtc::AudioSendStream::Stats AudioSendStream::GetStats() const {
Ivo Creusen56d460902017-11-24 16:29:59444 return GetStats(true);
445}
446
447webrtc::AudioSendStream::Stats AudioSendStream::GetStats(
448 bool has_remote_tracks) const {
Sebastian Janssonc01367d2019-04-08 13:20:44449 RTC_DCHECK(worker_thread_checker_.IsCurrent());
solenberg85a04962015-10-27 10:35:21450 webrtc::AudioSendStream::Stats stats;
451 stats.local_ssrc = config_.rtp.ssrc;
Niels Möllerdced9f62018-11-19 09:27:07452 stats.target_bitrate_bps = channel_send_->GetBitrate();
solenberg85a04962015-10-27 10:35:21453
Niels Möllerdced9f62018-11-19 09:27:07454 webrtc::CallSendStatistics call_stats = channel_send_->GetRTCPStatistics();
Niels Möllerac0a4cb2019-10-09 13:01:33455 stats.payload_bytes_sent = call_stats.payload_bytes_sent;
456 stats.header_and_padding_bytes_sent =
457 call_stats.header_and_padding_bytes_sent;
Henrik Boströmcf96e0f2019-04-17 11:51:53458 stats.retransmitted_bytes_sent = call_stats.retransmitted_bytes_sent;
solenberg85a04962015-10-27 10:35:21459 stats.packets_sent = call_stats.packetsSent;
Henrik Boströmcf96e0f2019-04-17 11:51:53460 stats.retransmitted_packets_sent = call_stats.retransmitted_packets_sent;
solenberg8b85de22015-11-16 17:48:04461 // RTT isn't known until a RTCP report is received. Until then, VoiceEngine
462 // returns 0 to indicate an error value.
463 if (call_stats.rttMs > 0) {
464 stats.rtt_ms = call_stats.rttMs;
465 }
ossu20a4b3f2017-04-27 09:08:52466 if (config_.send_codec_spec) {
467 const auto& spec = *config_.send_codec_spec;
468 stats.codec_name = spec.format.name;
Oskar Sundbom2707fb22017-11-16 09:57:35469 stats.codec_payload_type = spec.payload_type;
solenberg85a04962015-10-27 10:35:21470
471 // Get data from the last remote RTCP report.
Niels Möllerdced9f62018-11-19 09:27:07472 for (const auto& block : channel_send_->GetRemoteRTCPReportBlocks()) {
solenberg8b85de22015-11-16 17:48:04473 // Lookup report for send ssrc only.
474 if (block.source_SSRC == stats.local_ssrc) {
475 stats.packets_lost = block.cumulative_num_packets_lost;
476 stats.fraction_lost = Q8ToFloat(block.fraction_lost);
ossu20a4b3f2017-04-27 09:08:52477 // Convert timestamps to milliseconds.
478 if (spec.format.clockrate_hz / 1000 > 0) {
solenberg8b85de22015-11-16 17:48:04479 stats.jitter_ms =
ossu20a4b3f2017-04-27 09:08:52480 block.interarrival_jitter / (spec.format.clockrate_hz / 1000);
solenberg85a04962015-10-27 10:35:21481 }
solenberg8b85de22015-11-16 17:48:04482 break;
solenberg85a04962015-10-27 10:35:21483 }
484 }
485 }
486
Henrik Boströmd2c336f2019-07-03 15:11:10487 {
488 rtc::CritScope cs(&audio_level_lock_);
489 stats.audio_level = audio_level_.LevelFullRange();
490 stats.total_input_energy = audio_level_.TotalEnergy();
491 stats.total_input_duration = audio_level_.TotalDuration();
492 }
solenberg796b8f92017-03-02 01:02:23493
Fredrik Solenberg2a877972017-12-15 15:42:15494 stats.typing_noise_detected = audio_state()->typing_noise_detected();
Niels Möllerdced9f62018-11-19 09:27:07495 stats.ana_statistics = channel_send_->GetANAStatistics();
Per Åhgrencc73ed32020-04-26 21:56:17496
497 AudioProcessing* ap = audio_state_->audio_processing();
498 if (ap) {
499 stats.apm_statistics = ap->GetStatistics(has_remote_tracks);
500 }
solenberg85a04962015-10-27 10:35:21501
Henrik Boström6e436d12019-05-27 10:19:33502 stats.report_block_datas = std::move(call_stats.report_block_datas);
503
solenberg85a04962015-10-27 10:35:21504 return stats;
505}
506
Niels Möller8fb1a6a2019-03-05 13:29:42507void AudioSendStream::DeliverRtcp(const uint8_t* packet, size_t length) {
pbos1ba8d392016-05-02 03:18:34508 // TODO(solenberg): Tests call this function on a network thread, libjingle
509 // calls on the worker thread. We should move towards always using a network
510 // thread. Then this check can be enabled.
Sebastian Janssonc01367d2019-04-08 13:20:44511 // RTC_DCHECK(!worker_thread_checker_.IsCurrent());
Niels Möller8fb1a6a2019-03-05 13:29:42512 channel_send_->ReceivedRTCPPacket(packet, length);
Erik Språng04e1bab2020-05-07 16:18:32513 worker_queue_->PostTask([&]() {
514 // Poll if overhead has changed, which it can do if ack triggers us to stop
515 // sending mid/rid.
516 rtc::CritScope cs(&overhead_per_packet_lock_);
517 UpdateOverheadForEncoder();
518 });
pbos1ba8d392016-05-02 03:18:34519}
520
Sebastian Janssonc0e4d452018-10-25 13:08:32521uint32_t AudioSendStream::OnBitrateUpdated(BitrateAllocationUpdate update) {
Sebastian Jansson62aee932019-10-02 10:27:06522 RTC_DCHECK_RUN_ON(worker_queue_);
Erik Språng04e1bab2020-05-07 16:18:32523
Daniel Lee93562522019-05-03 12:40:13524 // Pick a target bitrate between the constraints. Overrules the allocator if
525 // it 1) allocated a bitrate of zero to disable the stream or 2) allocated a
526 // higher than max to allow for e.g. extra FEC.
527 auto constraints = GetMinMaxBitrateConstraints();
528 update.target_bitrate.Clamp(constraints.min, constraints.max);
Jakob Ivarsson0c964492020-03-11 08:18:59529 update.stable_target_bitrate.Clamp(constraints.min, constraints.max);
mflodman86cc6ff2016-07-26 11:44:06530
Sebastian Jansson254d8692018-11-21 18:19:00531 channel_send_->OnBitrateAllocation(update);
mflodman86cc6ff2016-07-26 11:44:06532
533 // The amount of audio protection is not exposed by the encoder, hence
534 // always returning 0.
535 return 0;
536}
537
Anton Sukhanov626015d2019-02-04 23:16:06538void AudioSendStream::SetTransportOverhead(
539 int transport_overhead_per_packet_bytes) {
Sebastian Janssonc01367d2019-04-08 13:20:44540 RTC_DCHECK(worker_thread_checker_.IsCurrent());
Anton Sukhanov626015d2019-02-04 23:16:06541 rtc::CritScope cs(&overhead_per_packet_lock_);
542 transport_overhead_per_packet_bytes_ = transport_overhead_per_packet_bytes;
543 UpdateOverheadForEncoder();
544}
545
Anton Sukhanov626015d2019-02-04 23:16:06546void AudioSendStream::UpdateOverheadForEncoder() {
547 const size_t overhead_per_packet_bytes = GetPerPacketOverheadBytes();
Bjorn A Mellem413ccc42019-04-26 22:41:05548 if (overhead_per_packet_bytes == 0) {
549 return; // Overhead is not known yet, do not tell the encoder.
550 }
Sebastian Jansson14a7cf92019-02-13 14:11:42551 channel_send_->CallEncoder([&](AudioEncoder* encoder) {
552 encoder->OnReceivedOverhead(overhead_per_packet_bytes);
Anton Sukhanov626015d2019-02-04 23:16:06553 });
Erik Språng04e1bab2020-05-07 16:18:32554 auto update_task = [this, overhead_per_packet_bytes] {
Sebastian Jansson8672cac2019-03-01 14:57:55555 RTC_DCHECK_RUN_ON(worker_queue_);
556 if (total_packet_overhead_bytes_ != overhead_per_packet_bytes) {
557 total_packet_overhead_bytes_ = overhead_per_packet_bytes;
558 if (registered_with_allocator_) {
559 ConfigureBitrateObserver();
560 }
561 }
Erik Språng04e1bab2020-05-07 16:18:32562 };
563 if (worker_queue_->IsCurrent()) {
564 update_task();
565 } else {
566 worker_queue_->PostTask(update_task);
567 }
Anton Sukhanov626015d2019-02-04 23:16:06568}
569
570size_t AudioSendStream::TestOnlyGetPerPacketOverheadBytes() const {
571 rtc::CritScope cs(&overhead_per_packet_lock_);
572 return GetPerPacketOverheadBytes();
573}
574
575size_t AudioSendStream::GetPerPacketOverheadBytes() const {
576 return transport_overhead_per_packet_bytes_ +
Erik Språng04e1bab2020-05-07 16:18:32577 rtp_rtcp_module_->ExpectedPerPacketOverhead();
michaelt79e05882016-11-08 10:50:09578}
579
ossuc3d4b482017-05-23 13:07:11580RtpState AudioSendStream::GetRtpState() const {
581 return rtp_rtcp_module_->GetRtpState();
582}
583
Niels Möllerdced9f62018-11-19 09:27:07584const voe::ChannelSendInterface* AudioSendStream::GetChannel() const {
585 return channel_send_.get();
Fredrik Solenberg8f5787a2018-01-11 12:52:30586}
587
Fredrik Solenberg2a877972017-12-15 15:42:15588internal::AudioState* AudioSendStream::audio_state() {
589 internal::AudioState* audio_state =
590 static_cast<internal::AudioState*>(audio_state_.get());
591 RTC_DCHECK(audio_state);
592 return audio_state;
593}
594
595const internal::AudioState* AudioSendStream::audio_state() const {
596 internal::AudioState* audio_state =
597 static_cast<internal::AudioState*>(audio_state_.get());
598 RTC_DCHECK(audio_state);
599 return audio_state;
600}
601
Fredrik Solenberg2a877972017-12-15 15:42:15602void AudioSendStream::StoreEncoderProperties(int sample_rate_hz,
603 size_t num_channels) {
Sebastian Janssonc01367d2019-04-08 13:20:44604 RTC_DCHECK(worker_thread_checker_.IsCurrent());
Fredrik Solenberg2a877972017-12-15 15:42:15605 encoder_sample_rate_hz_ = sample_rate_hz;
606 encoder_num_channels_ = num_channels;
607 if (sending_) {
608 // Update AudioState's information about the stream.
609 audio_state()->AddSendingStream(this, sample_rate_hz, num_channels);
610 }
611}
612
minyue7a973442016-10-20 10:27:12613// Apply current codec settings to a single voe::Channel used for sending.
Sebastian Jansson35cf9e72019-10-04 07:30:32614bool AudioSendStream::SetupSendCodec(const Config& new_config) {
ossu20a4b3f2017-04-27 09:08:52615 RTC_DCHECK(new_config.send_codec_spec);
616 const auto& spec = *new_config.send_codec_spec;
minyue48368ad2017-05-10 11:06:11617
618 RTC_DCHECK(new_config.encoder_factory);
ossu20a4b3f2017-04-27 09:08:52619 std::unique_ptr<AudioEncoder> encoder =
Karl Wiberg77490b92018-03-21 14:18:42620 new_config.encoder_factory->MakeAudioEncoder(
621 spec.payload_type, spec.format, new_config.codec_pair_id);
minyue7a973442016-10-20 10:27:12622
ossu20a4b3f2017-04-27 09:08:52623 if (!encoder) {
Jonas Olssonabbe8412018-04-03 11:40:05624 RTC_DLOG(LS_ERROR) << "Unable to create encoder for "
625 << rtc::ToString(spec.format);
ossu20a4b3f2017-04-27 09:08:52626 return false;
627 }
Alex Narestbbbe4e12018-07-13 08:32:58628
ossu20a4b3f2017-04-27 09:08:52629 // If a bitrate has been specified for the codec, use it over the
630 // codec's default.
Christoffer Rodbro110c64b2019-03-06 08:51:08631 if (spec.target_bitrate_bps) {
ossu20a4b3f2017-04-27 09:08:52632 encoder->OnReceivedTargetAudioBitrate(*spec.target_bitrate_bps);
minyue7a973442016-10-20 10:27:12633 }
634
ossu20a4b3f2017-04-27 09:08:52635 // Enable ANA if configured (currently only used by Opus).
Mirko Bonadei43564902020-01-29 15:29:36636 if (new_config.audio_network_adaptor_config) {
ossu20a4b3f2017-04-27 09:08:52637 if (encoder->EnableAudioNetworkAdaptor(
Sebastian Jansson35cf9e72019-10-04 07:30:32638 *new_config.audio_network_adaptor_config, event_log_)) {
Jonas Olsson24ea8222018-01-25 09:14:29639 RTC_DLOG(LS_INFO) << "Audio network adaptor enabled on SSRC "
640 << new_config.rtp.ssrc;
ossu20a4b3f2017-04-27 09:08:52641 } else {
Alejandro Luebsc7710842020-03-05 20:05:50642 RTC_DLOG(LS_INFO) << "Failed to enable Audio network adaptor on SSRC "
643 << new_config.rtp.ssrc;
minyue6b825df2016-10-31 11:08:32644 }
minyue7a973442016-10-20 10:27:12645 }
646
ossu20a4b3f2017-04-27 09:08:52647 // Wrap the encoder in a an AudioEncoderCNG, if VAD is enabled.
648 if (spec.cng_payload_type) {
Karl Wiberg23659362018-11-01 10:13:44649 AudioEncoderCngConfig cng_config;
ossu20a4b3f2017-04-27 09:08:52650 cng_config.num_channels = encoder->NumChannels();
651 cng_config.payload_type = *spec.cng_payload_type;
652 cng_config.speech_encoder = std::move(encoder);
653 cng_config.vad_mode = Vad::kVadNormal;
Karl Wiberg23659362018-11-01 10:13:44654 encoder = CreateComfortNoiseEncoder(std::move(cng_config));
ossu3b9ff382017-04-27 15:03:42655
Sebastian Jansson35cf9e72019-10-04 07:30:32656 RegisterCngPayloadType(*spec.cng_payload_type,
657 new_config.send_codec_spec->format.clockrate_hz);
minyue7a973442016-10-20 10:27:12658 }
ossu20a4b3f2017-04-27 09:08:52659
Anton Sukhanov626015d2019-02-04 23:16:06660 // Set currently known overhead (used in ANA, opus only).
661 // If overhead changes later, it will be updated in UpdateOverheadForEncoder.
662 {
Sebastian Jansson35cf9e72019-10-04 07:30:32663 rtc::CritScope cs(&overhead_per_packet_lock_);
Erik Språng04e1bab2020-05-07 16:18:32664 size_t overhead = GetPerPacketOverheadBytes();
665 if (overhead > 0) {
666 encoder->OnReceivedOverhead(overhead);
Bjorn A Mellem413ccc42019-04-26 22:41:05667 }
Anton Sukhanov626015d2019-02-04 23:16:06668 }
669
Sebastian Jansson35cf9e72019-10-04 07:30:32670 StoreEncoderProperties(encoder->SampleRateHz(), encoder->NumChannels());
671 channel_send_->SetEncoder(new_config.send_codec_spec->payload_type,
672 std::move(encoder));
Anton Sukhanov626015d2019-02-04 23:16:06673
minyue7a973442016-10-20 10:27:12674 return true;
675}
676
Sebastian Jansson35cf9e72019-10-04 07:30:32677bool AudioSendStream::ReconfigureSendCodec(const Config& new_config) {
678 const auto& old_config = config_;
minyue-webrtc8de18262017-07-26 12:18:40679
680 if (!new_config.send_codec_spec) {
681 // We cannot de-configure a send codec. So we will do nothing.
682 // By design, the send codec should have not been configured.
683 RTC_DCHECK(!old_config.send_codec_spec);
684 return true;
685 }
686
687 if (new_config.send_codec_spec == old_config.send_codec_spec &&
688 new_config.audio_network_adaptor_config ==
689 old_config.audio_network_adaptor_config) {
ossu20a4b3f2017-04-27 09:08:52690 return true;
691 }
692
693 // If we have no encoder, or the format or payload type's changed, create a
694 // new encoder.
695 if (!old_config.send_codec_spec ||
696 new_config.send_codec_spec->format !=
697 old_config.send_codec_spec->format ||
698 new_config.send_codec_spec->payload_type !=
699 old_config.send_codec_spec->payload_type) {
Sebastian Jansson35cf9e72019-10-04 07:30:32700 return SetupSendCodec(new_config);
ossu20a4b3f2017-04-27 09:08:52701 }
702
Danil Chapovalovb9b146c2018-06-15 10:28:07703 const absl::optional<int>& new_target_bitrate_bps =
ossu20a4b3f2017-04-27 09:08:52704 new_config.send_codec_spec->target_bitrate_bps;
705 // If a bitrate has been specified for the codec, use it over the
706 // codec's default.
Christoffer Rodbro110c64b2019-03-06 08:51:08707 if (new_target_bitrate_bps &&
ossu20a4b3f2017-04-27 09:08:52708 new_target_bitrate_bps !=
709 old_config.send_codec_spec->target_bitrate_bps) {
Sebastian Jansson35cf9e72019-10-04 07:30:32710 channel_send_->CallEncoder([&](AudioEncoder* encoder) {
ossu20a4b3f2017-04-27 09:08:52711 encoder->OnReceivedTargetAudioBitrate(*new_target_bitrate_bps);
712 });
713 }
714
Sebastian Jansson35cf9e72019-10-04 07:30:32715 ReconfigureANA(new_config);
716 ReconfigureCNG(new_config);
ossu20a4b3f2017-04-27 09:08:52717
718 return true;
719}
720
Sebastian Jansson35cf9e72019-10-04 07:30:32721void AudioSendStream::ReconfigureANA(const Config& new_config) {
ossu20a4b3f2017-04-27 09:08:52722 if (new_config.audio_network_adaptor_config ==
Sebastian Jansson35cf9e72019-10-04 07:30:32723 config_.audio_network_adaptor_config) {
ossu20a4b3f2017-04-27 09:08:52724 return;
725 }
Mirko Bonadei43564902020-01-29 15:29:36726 if (new_config.audio_network_adaptor_config) {
Sebastian Jansson35cf9e72019-10-04 07:30:32727 channel_send_->CallEncoder([&](AudioEncoder* encoder) {
ossu20a4b3f2017-04-27 09:08:52728 if (encoder->EnableAudioNetworkAdaptor(
Sebastian Jansson35cf9e72019-10-04 07:30:32729 *new_config.audio_network_adaptor_config, event_log_)) {
Jonas Olsson24ea8222018-01-25 09:14:29730 RTC_DLOG(LS_INFO) << "Audio network adaptor enabled on SSRC "
731 << new_config.rtp.ssrc;
ossu20a4b3f2017-04-27 09:08:52732 } else {
Alejandro Luebsc7710842020-03-05 20:05:50733 RTC_DLOG(LS_INFO) << "Failed to enable Audio network adaptor on SSRC "
734 << new_config.rtp.ssrc;
ossu20a4b3f2017-04-27 09:08:52735 }
736 });
737 } else {
Sebastian Jansson35cf9e72019-10-04 07:30:32738 channel_send_->CallEncoder(
Sebastian Jansson14a7cf92019-02-13 14:11:42739 [&](AudioEncoder* encoder) { encoder->DisableAudioNetworkAdaptor(); });
Jonas Olsson24ea8222018-01-25 09:14:29740 RTC_DLOG(LS_INFO) << "Audio network adaptor disabled on SSRC "
741 << new_config.rtp.ssrc;
ossu20a4b3f2017-04-27 09:08:52742 }
743}
744
Sebastian Jansson35cf9e72019-10-04 07:30:32745void AudioSendStream::ReconfigureCNG(const Config& new_config) {
ossu20a4b3f2017-04-27 09:08:52746 if (new_config.send_codec_spec->cng_payload_type ==
Sebastian Jansson35cf9e72019-10-04 07:30:32747 config_.send_codec_spec->cng_payload_type) {
ossu20a4b3f2017-04-27 09:08:52748 return;
749 }
750
ossu3b9ff382017-04-27 15:03:42751 // Register the CNG payload type if it's been added, don't do anything if CNG
752 // is removed. Payload types must not be redefined.
753 if (new_config.send_codec_spec->cng_payload_type) {
Sebastian Jansson35cf9e72019-10-04 07:30:32754 RegisterCngPayloadType(*new_config.send_codec_spec->cng_payload_type,
755 new_config.send_codec_spec->format.clockrate_hz);
ossu3b9ff382017-04-27 15:03:42756 }
757
ossu20a4b3f2017-04-27 09:08:52758 // Wrap or unwrap the encoder in an AudioEncoderCNG.
Sebastian Jansson35cf9e72019-10-04 07:30:32759 channel_send_->ModifyEncoder([&](std::unique_ptr<AudioEncoder>* encoder_ptr) {
760 std::unique_ptr<AudioEncoder> old_encoder(std::move(*encoder_ptr));
761 auto sub_encoders = old_encoder->ReclaimContainedEncoders();
762 if (!sub_encoders.empty()) {
763 // Replace enc with its sub encoder. We need to put the sub
764 // encoder in a temporary first, since otherwise the old value
765 // of enc would be destroyed before the new value got assigned,
766 // which would be bad since the new value is a part of the old
767 // value.
768 auto tmp = std::move(sub_encoders[0]);
769 old_encoder = std::move(tmp);
770 }
771 if (new_config.send_codec_spec->cng_payload_type) {
772 AudioEncoderCngConfig config;
773 config.speech_encoder = std::move(old_encoder);
774 config.num_channels = config.speech_encoder->NumChannels();
775 config.payload_type = *new_config.send_codec_spec->cng_payload_type;
776 config.vad_mode = Vad::kVadNormal;
777 *encoder_ptr = CreateComfortNoiseEncoder(std::move(config));
778 } else {
779 *encoder_ptr = std::move(old_encoder);
780 }
781 });
ossu20a4b3f2017-04-27 09:08:52782}
783
784void AudioSendStream::ReconfigureBitrateObserver(
ossu20a4b3f2017-04-27 09:08:52785 const webrtc::AudioSendStream::Config& new_config) {
Sebastian Jansson35cf9e72019-10-04 07:30:32786 RTC_DCHECK_RUN_ON(&worker_thread_checker_);
ossu20a4b3f2017-04-27 09:08:52787 // Since the Config's default is for both of these to be -1, this test will
788 // allow us to configure the bitrate observer if the new config has bitrate
789 // limits set, but would only have us call RemoveBitrateObserver if we were
790 // previously configured with bitrate limits.
Sebastian Jansson35cf9e72019-10-04 07:30:32791 if (config_.min_bitrate_bps == new_config.min_bitrate_bps &&
792 config_.max_bitrate_bps == new_config.max_bitrate_bps &&
793 config_.bitrate_priority == new_config.bitrate_priority &&
794 (TransportSeqNumId(config_) == TransportSeqNumId(new_config) ||
Jakob Ivarssond14525e2020-03-06 08:49:29795 !audio_send_side_bwe_) &&
796 config_.audio_network_adaptor_config ==
797 new_config.audio_network_adaptor_config) {
ossu20a4b3f2017-04-27 09:08:52798 return;
799 }
800
Sebastian Janssonf23131f2019-10-03 08:03:55801 if (!new_config.has_dscp && new_config.min_bitrate_bps != -1 &&
Sebastian Janssoncd0eedb2019-10-10 11:52:26802 new_config.max_bitrate_bps != -1 && TransportSeqNumId(new_config) != 0) {
Sebastian Jansson35cf9e72019-10-04 07:30:32803 rtp_transport_->AccountForAudioPacketsInPacedSender(true);
Sebastian Janssonc3eb9fd2020-01-29 16:42:52804 if (send_side_bwe_with_overhead_)
805 rtp_transport_->IncludeOverheadInPacedSender();
Sebastian Jansson8672cac2019-03-01 14:57:55806 rtc::Event thread_sync_event;
Sebastian Jansson35cf9e72019-10-04 07:30:32807 worker_queue_->PostTask([&] {
808 RTC_DCHECK_RUN_ON(worker_queue_);
Sebastian Jansson8672cac2019-03-01 14:57:55809 // We may get a callback immediately as the observer is registered, so
810 // make
811 // sure the bitrate limits in config_ are up-to-date.
Sebastian Jansson35cf9e72019-10-04 07:30:32812 config_.min_bitrate_bps = new_config.min_bitrate_bps;
813 config_.max_bitrate_bps = new_config.max_bitrate_bps;
814
815 config_.bitrate_priority = new_config.bitrate_priority;
816 ConfigureBitrateObserver();
Sebastian Jansson8672cac2019-03-01 14:57:55817 thread_sync_event.Set();
818 });
819 thread_sync_event.Wait(rtc::Event::kForever);
Sebastian Jansson35cf9e72019-10-04 07:30:32820 rtp_rtcp_module_->SetAsPartOfAllocation(true);
ossu20a4b3f2017-04-27 09:08:52821 } else {
Sebastian Jansson35cf9e72019-10-04 07:30:32822 rtp_transport_->AccountForAudioPacketsInPacedSender(false);
823 RemoveBitrateObserver();
824 rtp_rtcp_module_->SetAsPartOfAllocation(false);
ossu20a4b3f2017-04-27 09:08:52825 }
826}
827
Sebastian Jansson8672cac2019-03-01 14:57:55828void AudioSendStream::ConfigureBitrateObserver() {
829 // This either updates the current observer or adds a new observer.
830 // TODO(srte): Add overhead compensation here.
Daniel Lee93562522019-05-03 12:40:13831 auto constraints = GetMinMaxBitrateConstraints();
832
Sebastian Jansson0429f782019-10-03 16:32:45833 DataRate priority_bitrate = allocation_settings_.priority_bitrate;
Sebastian Janssonf23131f2019-10-03 08:03:55834 if (send_side_bwe_with_overhead_) {
Sebastian Jansson0429f782019-10-03 16:32:45835 if (use_legacy_overhead_calculation_) {
836 // OverheadPerPacket = Ipv4(20B) + UDP(8B) + SRTP(10B) + RTP(12)
837 constexpr int kOverheadPerPacket = 20 + 8 + 10 + 12;
Danil Chapovalov0c626af2020-02-10 10:16:00838 const TimeDelta kMinPacketDuration = TimeDelta::Millis(20);
Sebastian Jansson0429f782019-10-03 16:32:45839 DataRate max_overhead =
Danil Chapovalovcad3e0e2020-02-17 17:46:07840 DataSize::Bytes(kOverheadPerPacket) / kMinPacketDuration;
Sebastian Jansson0429f782019-10-03 16:32:45841 priority_bitrate += max_overhead;
842 } else {
843 RTC_DCHECK(frame_length_range_);
Erik Språng04e1bab2020-05-07 16:18:32844 const DataSize overhead_per_packet =
Danil Chapovalovcad3e0e2020-02-17 17:46:07845 DataSize::Bytes(total_packet_overhead_bytes_);
Erik Språng04e1bab2020-05-07 16:18:32846 DataRate min_overhead = overhead_per_packet / frame_length_range_->second;
Jakob Ivarsson01ab0842020-03-06 08:59:56847 priority_bitrate += min_overhead;
Sebastian Jansson0429f782019-10-03 16:32:45848 }
Sebastian Janssonf23131f2019-10-03 08:03:55849 }
Sebastian Janssonf23131f2019-10-03 08:03:55850 if (allocation_settings_.priority_bitrate_raw)
851 priority_bitrate = *allocation_settings_.priority_bitrate_raw;
852
Sebastian Jansson8672cac2019-03-01 14:57:55853 bitrate_allocator_->AddObserver(
Daniel Lee93562522019-05-03 12:40:13854 this,
855 MediaStreamAllocationConfig{
856 constraints.min.bps<uint32_t>(), constraints.max.bps<uint32_t>(), 0,
Sebastian Janssonf23131f2019-10-03 08:03:55857 priority_bitrate.bps(), true,
858 allocation_settings_.bitrate_priority.value_or(
Jonas Olsson8f119ca2019-05-08 08:56:23859 config_.bitrate_priority)});
Jakob Ivarssond14525e2020-03-06 08:49:29860 registered_with_allocator_ = true;
ossu20a4b3f2017-04-27 09:08:52861}
862
863void AudioSendStream::RemoveBitrateObserver() {
Sebastian Janssonc01367d2019-04-08 13:20:44864 RTC_DCHECK(worker_thread_checker_.IsCurrent());
Niels Möllerc572ff32018-11-07 07:43:50865 rtc::Event thread_sync_event;
ossu20a4b3f2017-04-27 09:08:52866 worker_queue_->PostTask([this, &thread_sync_event] {
Sebastian Jansson8672cac2019-03-01 14:57:55867 RTC_DCHECK_RUN_ON(worker_queue_);
868 registered_with_allocator_ = false;
ossu20a4b3f2017-04-27 09:08:52869 bitrate_allocator_->RemoveObserver(this);
870 thread_sync_event.Set();
871 });
872 thread_sync_event.Wait(rtc::Event::kForever);
873}
874
Daniel Lee93562522019-05-03 12:40:13875AudioSendStream::TargetAudioBitrateConstraints
876AudioSendStream::GetMinMaxBitrateConstraints() const {
877 TargetAudioBitrateConstraints constraints{
Danil Chapovalovcad3e0e2020-02-17 17:46:07878 DataRate::BitsPerSec(config_.min_bitrate_bps),
879 DataRate::BitsPerSec(config_.max_bitrate_bps)};
Daniel Lee93562522019-05-03 12:40:13880
881 // If bitrates were explicitly overriden via field trial, use those values.
Sebastian Janssonf23131f2019-10-03 08:03:55882 if (allocation_settings_.min_bitrate)
883 constraints.min = *allocation_settings_.min_bitrate;
884 if (allocation_settings_.max_bitrate)
885 constraints.max = *allocation_settings_.max_bitrate;
Daniel Lee93562522019-05-03 12:40:13886
Sebastian Jansson62aee932019-10-02 10:27:06887 RTC_DCHECK_GE(constraints.min, DataRate::Zero());
888 RTC_DCHECK_GE(constraints.max, DataRate::Zero());
889 RTC_DCHECK_GE(constraints.max, constraints.min);
Sebastian Janssonf23131f2019-10-03 08:03:55890 if (send_side_bwe_with_overhead_) {
Sebastian Jansson62aee932019-10-02 10:27:06891 if (use_legacy_overhead_calculation_) {
892 // OverheadPerPacket = Ipv4(20B) + UDP(8B) + SRTP(10B) + RTP(12)
Danil Chapovalovcad3e0e2020-02-17 17:46:07893 const DataSize kOverheadPerPacket = DataSize::Bytes(20 + 8 + 10 + 12);
Sebastian Jansson62aee932019-10-02 10:27:06894 const TimeDelta kMaxFrameLength =
Danil Chapovalov0c626af2020-02-10 10:16:00895 TimeDelta::Millis(60); // Based on Opus spec
Sebastian Jansson62aee932019-10-02 10:27:06896 const DataRate kMinOverhead = kOverheadPerPacket / kMaxFrameLength;
897 constraints.min += kMinOverhead;
898 constraints.max += kMinOverhead;
899 } else {
900 RTC_DCHECK(frame_length_range_);
901 const DataSize kOverheadPerPacket =
Danil Chapovalovcad3e0e2020-02-17 17:46:07902 DataSize::Bytes(total_packet_overhead_bytes_);
Sebastian Jansson62aee932019-10-02 10:27:06903 constraints.min += kOverheadPerPacket / frame_length_range_->second;
904 constraints.max += kOverheadPerPacket / frame_length_range_->first;
905 }
Daniel Lee93562522019-05-03 12:40:13906 }
907 return constraints;
908}
909
ossu3b9ff382017-04-27 15:03:42910void AudioSendStream::RegisterCngPayloadType(int payload_type,
911 int clockrate_hz) {
Niels Mölleree5ccbc2019-03-06 15:47:29912 channel_send_->RegisterCngPayloadType(payload_type, clockrate_hz);
ossu3b9ff382017-04-27 15:03:42913}
solenbergc7a8b082015-10-16 21:35:07914} // namespace internal
915} // namespace webrtc