blob: 5e3b9ffc9ce26d353d1fd23420f94bd27ecadd99 [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,
117 rtcp_rtt_stats,
118 suspended_rtp_state,
Sebastian Jansson977b3352019-03-04 16:43:34119 voe::CreateChannelSend(clock,
Sebastian Jansson44dd9f22019-03-08 13:50:30120 task_queue_factory,
Niels Möllerdced9f62018-11-19 09:27:07121 module_process_thread,
Anton Sukhanov626015d2019-02-04 23:16:06122 /*overhead_observer=*/this,
Niels Möllere9771992018-11-26 09:55:07123 config.send_transport,
Niels Möllerdced9f62018-11-19 09:27:07124 rtcp_rtt_stats,
125 event_log,
126 config.frame_encryptor,
127 config.crypto_options,
128 config.rtp.extmap_allow_mixed,
Erik Språng4c2c4122019-07-11 13:20:15129 config.rtcp_report_interval_ms,
130 config.rtp.ssrc)) {}
Fredrik Solenberg8f5787a2018-01-11 12:52:30131
132AudioSendStream::AudioSendStream(
Sebastian Jansson977b3352019-03-04 16:43:34133 Clock* clock,
Fredrik Solenberg8f5787a2018-01-11 12:52:30134 const webrtc::AudioSendStream::Config& config,
135 const rtc::scoped_refptr<webrtc::AudioState>& audio_state,
Sebastian Jansson44dd9f22019-03-08 13:50:30136 TaskQueueFactory* task_queue_factory,
Niels Möller7d76a312018-10-26 10:57:07137 RtpTransportControllerSendInterface* rtp_transport,
Niels Möller67b011d2018-10-22 11:00:40138 BitrateAllocatorInterface* bitrate_allocator,
Fredrik Solenberg8f5787a2018-01-11 12:52:30139 RtcEventLog* event_log,
140 RtcpRttStats* rtcp_rtt_stats,
Danil Chapovalovb9b146c2018-06-15 10:28:07141 const absl::optional<RtpState>& suspended_rtp_state,
Niels Möllerdced9f62018-11-19 09:27:07142 std::unique_ptr<voe::ChannelSendInterface> channel_send)
Sebastian Jansson977b3352019-03-04 16:43:34143 : clock_(clock),
Sebastian Jansson0b698262019-03-07 08:17:19144 worker_queue_(rtp_transport->GetWorkerQueue()),
Sebastian Janssonf23131f2019-10-03 08:03:55145 audio_send_side_bwe_(field_trial::IsEnabled("WebRTC-Audio-SendSideBwe")),
146 allocate_audio_without_feedback_(
147 field_trial::IsEnabled("WebRTC-Audio-ABWENoTWCC")),
148 enable_audio_alr_probing_(
149 !field_trial::IsDisabled("WebRTC-Audio-AlrProbing")),
150 send_side_bwe_with_overhead_(
151 field_trial::IsEnabled("WebRTC-SendSideBwe-WithOverhead")),
Bjorn A Mellem7a9a0922019-11-26 17:19:40152 config_(Config(/*send_transport=*/nullptr)),
mflodman86cc6ff2016-07-26 11:44:06153 audio_state_(audio_state),
Niels Möllerdced9f62018-11-19 09:27:07154 channel_send_(std::move(channel_send)),
ossu20a4b3f2017-04-27 09:08:52155 event_log_(event_log),
Sebastian Jansson62aee932019-10-02 10:27:06156 use_legacy_overhead_calculation_(
157 !field_trial::IsDisabled("WebRTC-Audio-LegacyOverhead")),
michaeltf4caaab2017-01-17 07:55:07158 bitrate_allocator_(bitrate_allocator),
Niels Möller7d76a312018-10-26 10:57:07159 rtp_transport_(rtp_transport),
Sebastian Jansson6298b562020-01-14 16:55:19160 rtp_rtcp_module_(channel_send_->GetRtpRtcp()),
Sam Zackrissonff058162018-11-20 16:15:13161 suspended_rtp_state_(suspended_rtp_state) {
Jonas Olsson24ea8222018-01-25 09:14:29162 RTC_LOG(LS_INFO) << "AudioSendStream: " << config.rtp.ssrc;
Fredrik Solenberg8f5787a2018-01-11 12:52:30163 RTC_DCHECK(worker_queue_);
164 RTC_DCHECK(audio_state_);
Niels Möllerdced9f62018-11-19 09:27:07165 RTC_DCHECK(channel_send_);
Fredrik Solenberg8f5787a2018-01-11 12:52:30166 RTC_DCHECK(bitrate_allocator_);
Sebastian Jansson0b698262019-03-07 08:17:19167 RTC_DCHECK(rtp_transport);
168
ossuc3d4b482017-05-23 13:07:11169 RTC_DCHECK(rtp_rtcp_module_);
mflodman3d7db262016-04-29 07:57:13170
Sebastian Jansson35cf9e72019-10-04 07:30:32171 ConfigureStream(config, true);
elad.alond12a8e12017-03-23 18:04:48172
Sebastian Janssonc01367d2019-04-08 13:20:44173 pacer_thread_checker_.Detach();
solenbergc7a8b082015-10-16 21:35:07174}
175
176AudioSendStream::~AudioSendStream() {
Sebastian Janssonc01367d2019-04-08 13:20:44177 RTC_DCHECK(worker_thread_checker_.IsCurrent());
Jonas Olsson24ea8222018-01-25 09:14:29178 RTC_LOG(LS_INFO) << "~AudioSendStream: " << config_.rtp.ssrc;
Fredrik Solenberg2a877972017-12-15 15:42:15179 RTC_DCHECK(!sending_);
Sebastian Jansson0a6510d2019-10-04 07:31:08180 channel_send_->ResetSenderCongestionControlObjects();
Sebastian Jansson8672cac2019-03-01 14:57:55181 // Blocking call to synchronize state with worker queue to ensure that there
182 // are no pending tasks left that keeps references to audio.
183 rtc::Event thread_sync_event;
184 worker_queue_->PostTask([&] { thread_sync_event.Set(); });
185 thread_sync_event.Wait(rtc::Event::kForever);
solenbergc7a8b082015-10-16 21:35:07186}
187
eladalonabbc4302017-07-26 09:09:44188const webrtc::AudioSendStream::Config& AudioSendStream::GetConfig() const {
Sebastian Janssonc01367d2019-04-08 13:20:44189 RTC_DCHECK(worker_thread_checker_.IsCurrent());
eladalonabbc4302017-07-26 09:09:44190 return config_;
191}
192
ossu20a4b3f2017-04-27 09:08:52193void AudioSendStream::Reconfigure(
194 const webrtc::AudioSendStream::Config& new_config) {
Sebastian Janssonc01367d2019-04-08 13:20:44195 RTC_DCHECK(worker_thread_checker_.IsCurrent());
Sebastian Jansson35cf9e72019-10-04 07:30:32196 ConfigureStream(new_config, false);
ossu20a4b3f2017-04-27 09:08:52197}
198
Alex Narestcedd3512017-12-07 19:54:55199AudioSendStream::ExtensionIds AudioSendStream::FindExtensionIds(
200 const std::vector<RtpExtension>& extensions) {
201 ExtensionIds ids;
202 for (const auto& extension : extensions) {
203 if (extension.uri == RtpExtension::kAudioLevelUri) {
204 ids.audio_level = extension.id;
Sebastian Jansson71c6b562019-08-14 09:31:02205 } else if (extension.uri == RtpExtension::kAbsSendTimeUri) {
206 ids.abs_send_time = extension.id;
Alex Narestcedd3512017-12-07 19:54:55207 } else if (extension.uri == RtpExtension::kTransportSequenceNumberUri) {
208 ids.transport_sequence_number = extension.id;
Steve Antonbb50ce52018-03-26 17:24:32209 } else if (extension.uri == RtpExtension::kMidUri) {
210 ids.mid = extension.id;
Amit Hilbuch77938e62018-12-21 17:23:38211 } else if (extension.uri == RtpExtension::kRidUri) {
212 ids.rid = extension.id;
213 } else if (extension.uri == RtpExtension::kRepairedRidUri) {
214 ids.repaired_rid = 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 ||
251 new_config.rtp.extmap_allow_mixed != old_config.rtp.extmap_allow_mixed) {
Sebastian Jansson6298b562020-01-14 16:55:19252 rtp_rtcp_module_->SetExtmapAllowMixed(new_config.rtp.extmap_allow_mixed);
Johannes Kron9190b822018-10-29 10:22:05253 }
254
Alex Narestcedd3512017-12-07 19:54:55255 const ExtensionIds old_ids = FindExtensionIds(old_config.rtp.extensions);
256 const ExtensionIds new_ids = FindExtensionIds(new_config.rtp.extensions);
Yves Gerey17048012019-07-26 15:49:52257
ossu20a4b3f2017-04-27 09:08:52258 // Audio level indication
259 if (first_time || new_ids.audio_level != old_ids.audio_level) {
Sebastian Jansson35cf9e72019-10-04 07:30:32260 channel_send_->SetSendAudioLevelIndicationStatus(new_ids.audio_level != 0,
261 new_ids.audio_level);
ossu20a4b3f2017-04-27 09:08:52262 }
Sebastian Jansson71c6b562019-08-14 09:31:02263
264 if (first_time || new_ids.abs_send_time != old_ids.abs_send_time) {
Sebastian Jansson6298b562020-01-14 16:55:19265 rtp_rtcp_module_->DeregisterSendRtpHeaderExtension(
Sebastian Jansson71c6b562019-08-14 09:31:02266 kRtpExtensionAbsoluteSendTime);
267 if (new_ids.abs_send_time) {
Sebastian Janssonf39c8152019-10-14 15:32:21268 rtp_rtcp_module_->RegisterRtpHeaderExtension(AbsoluteSendTime::kUri,
269 new_ids.abs_send_time);
Sebastian Jansson71c6b562019-08-14 09:31:02270 }
271 }
272
Sebastian Jansson8d9c5402017-11-15 16:22:16273 bool transport_seq_num_id_changed =
274 new_ids.transport_sequence_number != old_ids.transport_sequence_number;
Sebastian Jansson35cf9e72019-10-04 07:30:32275 if (first_time ||
276 (transport_seq_num_id_changed && !allocate_audio_without_feedback_)) {
ossu1129df22017-06-30 08:38:56277 if (!first_time) {
Sebastian Jansson35cf9e72019-10-04 07:30:32278 channel_send_->ResetSenderCongestionControlObjects();
ossu20a4b3f2017-04-27 09:08:52279 }
280
Sebastian Jansson8d9c5402017-11-15 16:22:16281 RtcpBandwidthObserver* bandwidth_observer = nullptr;
Sebastian Jansson470a5ea2019-01-23 11:37:49282
Sebastian Jansson35cf9e72019-10-04 07:30:32283 if (audio_send_side_bwe_ && !allocate_audio_without_feedback_ &&
Sebastian Janssonf23131f2019-10-03 08:03:55284 new_ids.transport_sequence_number != 0) {
Sebastian Jansson6298b562020-01-14 16:55:19285 rtp_rtcp_module_->RegisterRtpHeaderExtension(
286 TransportSequenceNumber::kUri, new_ids.transport_sequence_number);
Sebastian Jansson8d9c5402017-11-15 16:22:16287 // Probing in application limited region is only used in combination with
288 // send side congestion control, wich depends on feedback packets which
289 // requires transport sequence numbers to be enabled.
Sebastian Jansson0a6510d2019-10-04 07:31:08290 // Optionally request ALR probing but do not override any existing
291 // request from other streams.
292 if (enable_audio_alr_probing_) {
293 rtp_transport_->EnablePeriodicAlrProbing(true);
Niels Möller7d76a312018-10-26 10:57:07294 }
Sebastian Jansson0a6510d2019-10-04 07:31:08295 bandwidth_observer = rtp_transport_->GetBandwidthObserver();
ossu20a4b3f2017-04-27 09:08:52296 }
Sebastian Jansson0a6510d2019-10-04 07:31:08297 channel_send_->RegisterSenderCongestionControlObjects(rtp_transport_,
298 bandwidth_observer);
ossu20a4b3f2017-04-27 09:08:52299 }
Steve Antonbb50ce52018-03-26 17:24:32300 // MID RTP header extension.
Steve Anton003930a2018-03-29 19:37:21301 if ((first_time || new_ids.mid != old_ids.mid ||
302 new_config.rtp.mid != old_config.rtp.mid) &&
303 new_ids.mid != 0 && !new_config.rtp.mid.empty()) {
Sebastian Jansson6298b562020-01-14 16:55:19304 rtp_rtcp_module_->RegisterRtpHeaderExtension(RtpMid::kUri, new_ids.mid);
305 rtp_rtcp_module_->SetMid(new_config.rtp.mid);
Steve Antonbb50ce52018-03-26 17:24:32306 }
307
Amit Hilbuch77938e62018-12-21 17:23:38308 // RID RTP header extension
309 if ((first_time || new_ids.rid != old_ids.rid ||
310 new_ids.repaired_rid != old_ids.repaired_rid ||
311 new_config.rtp.rid != old_config.rtp.rid)) {
Sebastian Jansson6298b562020-01-14 16:55:19312 if (new_ids.rid != 0 || new_ids.repaired_rid != 0) {
313 if (new_config.rtp.rid.empty()) {
314 rtp_rtcp_module_->DeregisterSendRtpHeaderExtension(RtpStreamId::kUri);
315 } else if (new_ids.repaired_rid != 0) {
316 rtp_rtcp_module_->RegisterRtpHeaderExtension(RtpStreamId::kUri,
317 new_ids.repaired_rid);
318 } else {
319 rtp_rtcp_module_->RegisterRtpHeaderExtension(RtpStreamId::kUri,
320 new_ids.rid);
321 }
322 }
323 rtp_rtcp_module_->SetRid(new_config.rtp.rid);
Amit Hilbuch77938e62018-12-21 17:23:38324 }
325
Sebastian Jansson35cf9e72019-10-04 07:30:32326 if (!ReconfigureSendCodec(new_config)) {
Mirko Bonadei675513b2017-11-09 10:09:25327 RTC_LOG(LS_ERROR) << "Failed to set up send codec state.";
ossu20a4b3f2017-04-27 09:08:52328 }
329
Sebastian Jansson35cf9e72019-10-04 07:30:32330 if (sending_) {
331 ReconfigureBitrateObserver(new_config);
Oskar Sundbomf85e31b2017-12-20 15:38:09332 }
Sebastian Jansson35cf9e72019-10-04 07:30:32333 config_ = new_config;
ossu20a4b3f2017-04-27 09:08:52334}
335
solenberg3a941542015-11-16 15:34:50336void AudioSendStream::Start() {
Sebastian Jansson8672cac2019-03-01 14:57:55337 RTC_DCHECK_RUN_ON(&worker_thread_checker_);
Fredrik Solenberg2a877972017-12-15 15:42:15338 if (sending_) {
339 return;
340 }
Sebastian Janssonf23131f2019-10-03 08:03:55341 if (!config_.has_dscp && config_.min_bitrate_bps != -1 &&
342 config_.max_bitrate_bps != -1 &&
Sebastian Janssoncd0eedb2019-10-10 11:52:26343 (allocate_audio_without_feedback_ || TransportSeqNumId(config_) != 0)) {
Erik Språngaa59eca2019-07-24 12:52:55344 rtp_transport_->AccountForAudioPacketsInPacedSender(true);
Sebastian Janssonb6863962018-10-10 08:23:13345 rtp_rtcp_module_->SetAsPartOfAllocation(true);
Sebastian Jansson8672cac2019-03-01 14:57:55346 rtc::Event thread_sync_event;
347 worker_queue_->PostTask([&] {
348 RTC_DCHECK_RUN_ON(worker_queue_);
349 ConfigureBitrateObserver();
350 thread_sync_event.Set();
351 });
352 thread_sync_event.Wait(rtc::Event::kForever);
Sebastian Janssonb6863962018-10-10 08:23:13353 } else {
354 rtp_rtcp_module_->SetAsPartOfAllocation(false);
mflodman86cc6ff2016-07-26 11:44:06355 }
Niels Möllerdced9f62018-11-19 09:27:07356 channel_send_->StartSend();
Fredrik Solenberg2a877972017-12-15 15:42:15357 sending_ = true;
358 audio_state()->AddSendingStream(this, encoder_sample_rate_hz_,
359 encoder_num_channels_);
solenberg3a941542015-11-16 15:34:50360}
361
362void AudioSendStream::Stop() {
Sebastian Janssonc01367d2019-04-08 13:20:44363 RTC_DCHECK(worker_thread_checker_.IsCurrent());
Fredrik Solenberg2a877972017-12-15 15:42:15364 if (!sending_) {
365 return;
366 }
367
ossu20a4b3f2017-04-27 09:08:52368 RemoveBitrateObserver();
Niels Möllerdced9f62018-11-19 09:27:07369 channel_send_->StopSend();
Fredrik Solenberg2a877972017-12-15 15:42:15370 sending_ = false;
371 audio_state()->RemoveSendingStream(this);
372}
373
374void AudioSendStream::SendAudioData(std::unique_ptr<AudioFrame> audio_frame) {
375 RTC_CHECK_RUNS_SERIALIZED(&audio_capture_race_checker_);
Henrik Boströmd2c336f2019-07-03 15:11:10376 RTC_DCHECK_GT(audio_frame->sample_rate_hz_, 0);
377 double duration = static_cast<double>(audio_frame->samples_per_channel_) /
378 audio_frame->sample_rate_hz_;
379 {
380 // Note: SendAudioData() passes the frame further down the pipeline and it
381 // may eventually get sent. But this method is invoked even if we are not
382 // connected, as long as we have an AudioSendStream (created as a result of
383 // an O/A exchange). This means that we are calculating audio levels whether
384 // or not we are sending samples.
385 // TODO(https://crbug.com/webrtc/10771): All "media-source" related stats
386 // should move from send-streams to the local audio sources or tracks; a
387 // send-stream should not be required to read the microphone audio levels.
388 rtc::CritScope cs(&audio_level_lock_);
389 audio_level_.ComputeLevel(*audio_frame, duration);
390 }
Niels Möllerdced9f62018-11-19 09:27:07391 channel_send_->ProcessAndEncodeAudio(std::move(audio_frame));
solenberg3a941542015-11-16 15:34:50392}
393
solenbergffbbcac2016-11-17 13:25:37394bool AudioSendStream::SendTelephoneEvent(int payload_type,
Yves Gerey665174f2018-06-19 13:03:05395 int payload_frequency,
396 int event,
solenberg8842c3e2016-03-11 11:06:41397 int duration_ms) {
Sebastian Janssonc01367d2019-04-08 13:20:44398 RTC_DCHECK(worker_thread_checker_.IsCurrent());
Niels Möller8fb1a6a2019-03-05 13:29:42399 channel_send_->SetSendTelephoneEventPayloadType(payload_type,
400 payload_frequency);
401 return channel_send_->SendTelephoneEventOutband(event, duration_ms);
Fredrik Solenbergb5727682015-12-04 14:22:19402}
403
solenberg94218532016-06-16 17:53:22404void AudioSendStream::SetMuted(bool muted) {
Sebastian Janssonc01367d2019-04-08 13:20:44405 RTC_DCHECK(worker_thread_checker_.IsCurrent());
Niels Möllerdced9f62018-11-19 09:27:07406 channel_send_->SetInputMute(muted);
solenberg94218532016-06-16 17:53:22407}
408
solenbergc7a8b082015-10-16 21:35:07409webrtc::AudioSendStream::Stats AudioSendStream::GetStats() const {
Ivo Creusen56d460902017-11-24 16:29:59410 return GetStats(true);
411}
412
413webrtc::AudioSendStream::Stats AudioSendStream::GetStats(
414 bool has_remote_tracks) const {
Sebastian Janssonc01367d2019-04-08 13:20:44415 RTC_DCHECK(worker_thread_checker_.IsCurrent());
solenberg85a04962015-10-27 10:35:21416 webrtc::AudioSendStream::Stats stats;
417 stats.local_ssrc = config_.rtp.ssrc;
Niels Möllerdced9f62018-11-19 09:27:07418 stats.target_bitrate_bps = channel_send_->GetBitrate();
solenberg85a04962015-10-27 10:35:21419
Niels Möllerdced9f62018-11-19 09:27:07420 webrtc::CallSendStatistics call_stats = channel_send_->GetRTCPStatistics();
Niels Möllerac0a4cb2019-10-09 13:01:33421 stats.payload_bytes_sent = call_stats.payload_bytes_sent;
422 stats.header_and_padding_bytes_sent =
423 call_stats.header_and_padding_bytes_sent;
Henrik Boströmcf96e0f2019-04-17 11:51:53424 stats.retransmitted_bytes_sent = call_stats.retransmitted_bytes_sent;
solenberg85a04962015-10-27 10:35:21425 stats.packets_sent = call_stats.packetsSent;
Henrik Boströmcf96e0f2019-04-17 11:51:53426 stats.retransmitted_packets_sent = call_stats.retransmitted_packets_sent;
solenberg8b85de22015-11-16 17:48:04427 // RTT isn't known until a RTCP report is received. Until then, VoiceEngine
428 // returns 0 to indicate an error value.
429 if (call_stats.rttMs > 0) {
430 stats.rtt_ms = call_stats.rttMs;
431 }
ossu20a4b3f2017-04-27 09:08:52432 if (config_.send_codec_spec) {
433 const auto& spec = *config_.send_codec_spec;
434 stats.codec_name = spec.format.name;
Oskar Sundbom2707fb22017-11-16 09:57:35435 stats.codec_payload_type = spec.payload_type;
solenberg85a04962015-10-27 10:35:21436
437 // Get data from the last remote RTCP report.
Niels Möllerdced9f62018-11-19 09:27:07438 for (const auto& block : channel_send_->GetRemoteRTCPReportBlocks()) {
solenberg8b85de22015-11-16 17:48:04439 // Lookup report for send ssrc only.
440 if (block.source_SSRC == stats.local_ssrc) {
441 stats.packets_lost = block.cumulative_num_packets_lost;
442 stats.fraction_lost = Q8ToFloat(block.fraction_lost);
ossu20a4b3f2017-04-27 09:08:52443 // Convert timestamps to milliseconds.
444 if (spec.format.clockrate_hz / 1000 > 0) {
solenberg8b85de22015-11-16 17:48:04445 stats.jitter_ms =
ossu20a4b3f2017-04-27 09:08:52446 block.interarrival_jitter / (spec.format.clockrate_hz / 1000);
solenberg85a04962015-10-27 10:35:21447 }
solenberg8b85de22015-11-16 17:48:04448 break;
solenberg85a04962015-10-27 10:35:21449 }
450 }
451 }
452
Henrik Boströmd2c336f2019-07-03 15:11:10453 {
454 rtc::CritScope cs(&audio_level_lock_);
455 stats.audio_level = audio_level_.LevelFullRange();
456 stats.total_input_energy = audio_level_.TotalEnergy();
457 stats.total_input_duration = audio_level_.TotalDuration();
458 }
solenberg796b8f92017-03-02 01:02:23459
Fredrik Solenberg2a877972017-12-15 15:42:15460 stats.typing_noise_detected = audio_state()->typing_noise_detected();
Niels Möllerdced9f62018-11-19 09:27:07461 stats.ana_statistics = channel_send_->GetANAStatistics();
Ivo Creusen56d460902017-11-24 16:29:59462 RTC_DCHECK(audio_state_->audio_processing());
463 stats.apm_statistics =
464 audio_state_->audio_processing()->GetStatistics(has_remote_tracks);
solenberg85a04962015-10-27 10:35:21465
Henrik Boström6e436d12019-05-27 10:19:33466 stats.report_block_datas = std::move(call_stats.report_block_datas);
467
solenberg85a04962015-10-27 10:35:21468 return stats;
469}
470
Niels Möller8fb1a6a2019-03-05 13:29:42471void AudioSendStream::DeliverRtcp(const uint8_t* packet, size_t length) {
pbos1ba8d392016-05-02 03:18:34472 // TODO(solenberg): Tests call this function on a network thread, libjingle
473 // calls on the worker thread. We should move towards always using a network
474 // thread. Then this check can be enabled.
Sebastian Janssonc01367d2019-04-08 13:20:44475 // RTC_DCHECK(!worker_thread_checker_.IsCurrent());
Niels Möller8fb1a6a2019-03-05 13:29:42476 channel_send_->ReceivedRTCPPacket(packet, length);
pbos1ba8d392016-05-02 03:18:34477}
478
Sebastian Janssonc0e4d452018-10-25 13:08:32479uint32_t AudioSendStream::OnBitrateUpdated(BitrateAllocationUpdate update) {
Sebastian Jansson62aee932019-10-02 10:27:06480 RTC_DCHECK_RUN_ON(worker_queue_);
Daniel Lee93562522019-05-03 12:40:13481 // Pick a target bitrate between the constraints. Overrules the allocator if
482 // it 1) allocated a bitrate of zero to disable the stream or 2) allocated a
483 // higher than max to allow for e.g. extra FEC.
484 auto constraints = GetMinMaxBitrateConstraints();
485 update.target_bitrate.Clamp(constraints.min, constraints.max);
mflodman86cc6ff2016-07-26 11:44:06486
Sebastian Jansson254d8692018-11-21 18:19:00487 channel_send_->OnBitrateAllocation(update);
mflodman86cc6ff2016-07-26 11:44:06488
489 // The amount of audio protection is not exposed by the encoder, hence
490 // always returning 0.
491 return 0;
492}
493
Anton Sukhanov626015d2019-02-04 23:16:06494void AudioSendStream::SetTransportOverhead(
495 int transport_overhead_per_packet_bytes) {
Sebastian Janssonc01367d2019-04-08 13:20:44496 RTC_DCHECK(worker_thread_checker_.IsCurrent());
Anton Sukhanov626015d2019-02-04 23:16:06497 rtc::CritScope cs(&overhead_per_packet_lock_);
498 transport_overhead_per_packet_bytes_ = transport_overhead_per_packet_bytes;
499 UpdateOverheadForEncoder();
500}
501
502void AudioSendStream::OnOverheadChanged(
503 size_t overhead_bytes_per_packet_bytes) {
504 rtc::CritScope cs(&overhead_per_packet_lock_);
505 audio_overhead_per_packet_bytes_ = overhead_bytes_per_packet_bytes;
506 UpdateOverheadForEncoder();
507}
508
509void AudioSendStream::UpdateOverheadForEncoder() {
510 const size_t overhead_per_packet_bytes = GetPerPacketOverheadBytes();
Bjorn A Mellem413ccc42019-04-26 22:41:05511 if (overhead_per_packet_bytes == 0) {
512 return; // Overhead is not known yet, do not tell the encoder.
513 }
Sebastian Jansson14a7cf92019-02-13 14:11:42514 channel_send_->CallEncoder([&](AudioEncoder* encoder) {
515 encoder->OnReceivedOverhead(overhead_per_packet_bytes);
Anton Sukhanov626015d2019-02-04 23:16:06516 });
Sebastian Jansson8672cac2019-03-01 14:57:55517 worker_queue_->PostTask([this, overhead_per_packet_bytes] {
518 RTC_DCHECK_RUN_ON(worker_queue_);
519 if (total_packet_overhead_bytes_ != overhead_per_packet_bytes) {
520 total_packet_overhead_bytes_ = overhead_per_packet_bytes;
521 if (registered_with_allocator_) {
522 ConfigureBitrateObserver();
523 }
524 }
525 });
Anton Sukhanov626015d2019-02-04 23:16:06526}
527
528size_t AudioSendStream::TestOnlyGetPerPacketOverheadBytes() const {
529 rtc::CritScope cs(&overhead_per_packet_lock_);
530 return GetPerPacketOverheadBytes();
531}
532
533size_t AudioSendStream::GetPerPacketOverheadBytes() const {
534 return transport_overhead_per_packet_bytes_ +
535 audio_overhead_per_packet_bytes_;
michaelt79e05882016-11-08 10:50:09536}
537
ossuc3d4b482017-05-23 13:07:11538RtpState AudioSendStream::GetRtpState() const {
539 return rtp_rtcp_module_->GetRtpState();
540}
541
Niels Möllerdced9f62018-11-19 09:27:07542const voe::ChannelSendInterface* AudioSendStream::GetChannel() const {
543 return channel_send_.get();
Fredrik Solenberg8f5787a2018-01-11 12:52:30544}
545
Fredrik Solenberg2a877972017-12-15 15:42:15546internal::AudioState* AudioSendStream::audio_state() {
547 internal::AudioState* audio_state =
548 static_cast<internal::AudioState*>(audio_state_.get());
549 RTC_DCHECK(audio_state);
550 return audio_state;
551}
552
553const internal::AudioState* AudioSendStream::audio_state() const {
554 internal::AudioState* audio_state =
555 static_cast<internal::AudioState*>(audio_state_.get());
556 RTC_DCHECK(audio_state);
557 return audio_state;
558}
559
Fredrik Solenberg2a877972017-12-15 15:42:15560void AudioSendStream::StoreEncoderProperties(int sample_rate_hz,
561 size_t num_channels) {
Sebastian Janssonc01367d2019-04-08 13:20:44562 RTC_DCHECK(worker_thread_checker_.IsCurrent());
Fredrik Solenberg2a877972017-12-15 15:42:15563 encoder_sample_rate_hz_ = sample_rate_hz;
564 encoder_num_channels_ = num_channels;
565 if (sending_) {
566 // Update AudioState's information about the stream.
567 audio_state()->AddSendingStream(this, sample_rate_hz, num_channels);
568 }
569}
570
minyue7a973442016-10-20 10:27:12571// Apply current codec settings to a single voe::Channel used for sending.
Sebastian Jansson35cf9e72019-10-04 07:30:32572bool AudioSendStream::SetupSendCodec(const Config& new_config) {
ossu20a4b3f2017-04-27 09:08:52573 RTC_DCHECK(new_config.send_codec_spec);
574 const auto& spec = *new_config.send_codec_spec;
minyue48368ad2017-05-10 11:06:11575
576 RTC_DCHECK(new_config.encoder_factory);
ossu20a4b3f2017-04-27 09:08:52577 std::unique_ptr<AudioEncoder> encoder =
Karl Wiberg77490b92018-03-21 14:18:42578 new_config.encoder_factory->MakeAudioEncoder(
579 spec.payload_type, spec.format, new_config.codec_pair_id);
minyue7a973442016-10-20 10:27:12580
ossu20a4b3f2017-04-27 09:08:52581 if (!encoder) {
Jonas Olssonabbe8412018-04-03 11:40:05582 RTC_DLOG(LS_ERROR) << "Unable to create encoder for "
583 << rtc::ToString(spec.format);
ossu20a4b3f2017-04-27 09:08:52584 return false;
585 }
Alex Narestbbbe4e12018-07-13 08:32:58586
ossu20a4b3f2017-04-27 09:08:52587 // If a bitrate has been specified for the codec, use it over the
588 // codec's default.
Christoffer Rodbro110c64b2019-03-06 08:51:08589 if (spec.target_bitrate_bps) {
ossu20a4b3f2017-04-27 09:08:52590 encoder->OnReceivedTargetAudioBitrate(*spec.target_bitrate_bps);
minyue7a973442016-10-20 10:27:12591 }
592
ossu20a4b3f2017-04-27 09:08:52593 // Enable ANA if configured (currently only used by Opus).
Mirko Bonadei43564902020-01-29 15:29:36594 if (new_config.audio_network_adaptor_config) {
ossu20a4b3f2017-04-27 09:08:52595 if (encoder->EnableAudioNetworkAdaptor(
Sebastian Jansson35cf9e72019-10-04 07:30:32596 *new_config.audio_network_adaptor_config, event_log_)) {
Jonas Olsson24ea8222018-01-25 09:14:29597 RTC_DLOG(LS_INFO) << "Audio network adaptor enabled on SSRC "
598 << new_config.rtp.ssrc;
ossu20a4b3f2017-04-27 09:08:52599 } else {
600 RTC_NOTREACHED();
minyue6b825df2016-10-31 11:08:32601 }
minyue7a973442016-10-20 10:27:12602 }
603
ossu20a4b3f2017-04-27 09:08:52604 // Wrap the encoder in a an AudioEncoderCNG, if VAD is enabled.
605 if (spec.cng_payload_type) {
Karl Wiberg23659362018-11-01 10:13:44606 AudioEncoderCngConfig cng_config;
ossu20a4b3f2017-04-27 09:08:52607 cng_config.num_channels = encoder->NumChannels();
608 cng_config.payload_type = *spec.cng_payload_type;
609 cng_config.speech_encoder = std::move(encoder);
610 cng_config.vad_mode = Vad::kVadNormal;
Karl Wiberg23659362018-11-01 10:13:44611 encoder = CreateComfortNoiseEncoder(std::move(cng_config));
ossu3b9ff382017-04-27 15:03:42612
Sebastian Jansson35cf9e72019-10-04 07:30:32613 RegisterCngPayloadType(*spec.cng_payload_type,
614 new_config.send_codec_spec->format.clockrate_hz);
minyue7a973442016-10-20 10:27:12615 }
ossu20a4b3f2017-04-27 09:08:52616
Anton Sukhanov626015d2019-02-04 23:16:06617 // Set currently known overhead (used in ANA, opus only).
618 // If overhead changes later, it will be updated in UpdateOverheadForEncoder.
619 {
Sebastian Jansson35cf9e72019-10-04 07:30:32620 rtc::CritScope cs(&overhead_per_packet_lock_);
621 if (GetPerPacketOverheadBytes() > 0) {
622 encoder->OnReceivedOverhead(GetPerPacketOverheadBytes());
Bjorn A Mellem413ccc42019-04-26 22:41:05623 }
Anton Sukhanov626015d2019-02-04 23:16:06624 }
Sebastian Jansson35cf9e72019-10-04 07:30:32625 worker_queue_->PostTask(
626 [this, length_range = encoder->GetFrameLengthRange()] {
627 RTC_DCHECK_RUN_ON(worker_queue_);
628 frame_length_range_ = length_range;
Sebastian Jansson62aee932019-10-02 10:27:06629 });
Anton Sukhanov626015d2019-02-04 23:16:06630
Sebastian Jansson35cf9e72019-10-04 07:30:32631 StoreEncoderProperties(encoder->SampleRateHz(), encoder->NumChannels());
632 channel_send_->SetEncoder(new_config.send_codec_spec->payload_type,
633 std::move(encoder));
Anton Sukhanov626015d2019-02-04 23:16:06634
minyue7a973442016-10-20 10:27:12635 return true;
636}
637
Sebastian Jansson35cf9e72019-10-04 07:30:32638bool AudioSendStream::ReconfigureSendCodec(const Config& new_config) {
639 const auto& old_config = config_;
minyue-webrtc8de18262017-07-26 12:18:40640
641 if (!new_config.send_codec_spec) {
642 // We cannot de-configure a send codec. So we will do nothing.
643 // By design, the send codec should have not been configured.
644 RTC_DCHECK(!old_config.send_codec_spec);
645 return true;
646 }
647
648 if (new_config.send_codec_spec == old_config.send_codec_spec &&
649 new_config.audio_network_adaptor_config ==
650 old_config.audio_network_adaptor_config) {
ossu20a4b3f2017-04-27 09:08:52651 return true;
652 }
653
654 // If we have no encoder, or the format or payload type's changed, create a
655 // new encoder.
656 if (!old_config.send_codec_spec ||
657 new_config.send_codec_spec->format !=
658 old_config.send_codec_spec->format ||
659 new_config.send_codec_spec->payload_type !=
660 old_config.send_codec_spec->payload_type) {
Sebastian Jansson35cf9e72019-10-04 07:30:32661 return SetupSendCodec(new_config);
ossu20a4b3f2017-04-27 09:08:52662 }
663
Danil Chapovalovb9b146c2018-06-15 10:28:07664 const absl::optional<int>& new_target_bitrate_bps =
ossu20a4b3f2017-04-27 09:08:52665 new_config.send_codec_spec->target_bitrate_bps;
666 // If a bitrate has been specified for the codec, use it over the
667 // codec's default.
Christoffer Rodbro110c64b2019-03-06 08:51:08668 if (new_target_bitrate_bps &&
ossu20a4b3f2017-04-27 09:08:52669 new_target_bitrate_bps !=
670 old_config.send_codec_spec->target_bitrate_bps) {
Sebastian Jansson35cf9e72019-10-04 07:30:32671 channel_send_->CallEncoder([&](AudioEncoder* encoder) {
ossu20a4b3f2017-04-27 09:08:52672 encoder->OnReceivedTargetAudioBitrate(*new_target_bitrate_bps);
673 });
674 }
675
Sebastian Jansson35cf9e72019-10-04 07:30:32676 ReconfigureANA(new_config);
677 ReconfigureCNG(new_config);
ossu20a4b3f2017-04-27 09:08:52678
Anton Sukhanov626015d2019-02-04 23:16:06679 // Set currently known overhead (used in ANA, opus only).
680 {
Sebastian Jansson35cf9e72019-10-04 07:30:32681 rtc::CritScope cs(&overhead_per_packet_lock_);
682 UpdateOverheadForEncoder();
Anton Sukhanov626015d2019-02-04 23:16:06683 }
684
ossu20a4b3f2017-04-27 09:08:52685 return true;
686}
687
Sebastian Jansson35cf9e72019-10-04 07:30:32688void AudioSendStream::ReconfigureANA(const Config& new_config) {
ossu20a4b3f2017-04-27 09:08:52689 if (new_config.audio_network_adaptor_config ==
Sebastian Jansson35cf9e72019-10-04 07:30:32690 config_.audio_network_adaptor_config) {
ossu20a4b3f2017-04-27 09:08:52691 return;
692 }
Mirko Bonadei43564902020-01-29 15:29:36693 if (new_config.audio_network_adaptor_config) {
Sebastian Jansson35cf9e72019-10-04 07:30:32694 channel_send_->CallEncoder([&](AudioEncoder* encoder) {
ossu20a4b3f2017-04-27 09:08:52695 if (encoder->EnableAudioNetworkAdaptor(
Sebastian Jansson35cf9e72019-10-04 07:30:32696 *new_config.audio_network_adaptor_config, event_log_)) {
Jonas Olsson24ea8222018-01-25 09:14:29697 RTC_DLOG(LS_INFO) << "Audio network adaptor enabled on SSRC "
698 << new_config.rtp.ssrc;
ossu20a4b3f2017-04-27 09:08:52699 } else {
700 RTC_NOTREACHED();
701 }
702 });
703 } else {
Sebastian Jansson35cf9e72019-10-04 07:30:32704 channel_send_->CallEncoder(
Sebastian Jansson14a7cf92019-02-13 14:11:42705 [&](AudioEncoder* encoder) { encoder->DisableAudioNetworkAdaptor(); });
Jonas Olsson24ea8222018-01-25 09:14:29706 RTC_DLOG(LS_INFO) << "Audio network adaptor disabled on SSRC "
707 << new_config.rtp.ssrc;
ossu20a4b3f2017-04-27 09:08:52708 }
709}
710
Sebastian Jansson35cf9e72019-10-04 07:30:32711void AudioSendStream::ReconfigureCNG(const Config& new_config) {
ossu20a4b3f2017-04-27 09:08:52712 if (new_config.send_codec_spec->cng_payload_type ==
Sebastian Jansson35cf9e72019-10-04 07:30:32713 config_.send_codec_spec->cng_payload_type) {
ossu20a4b3f2017-04-27 09:08:52714 return;
715 }
716
ossu3b9ff382017-04-27 15:03:42717 // Register the CNG payload type if it's been added, don't do anything if CNG
718 // is removed. Payload types must not be redefined.
719 if (new_config.send_codec_spec->cng_payload_type) {
Sebastian Jansson35cf9e72019-10-04 07:30:32720 RegisterCngPayloadType(*new_config.send_codec_spec->cng_payload_type,
721 new_config.send_codec_spec->format.clockrate_hz);
ossu3b9ff382017-04-27 15:03:42722 }
723
ossu20a4b3f2017-04-27 09:08:52724 // Wrap or unwrap the encoder in an AudioEncoderCNG.
Sebastian Jansson35cf9e72019-10-04 07:30:32725 channel_send_->ModifyEncoder([&](std::unique_ptr<AudioEncoder>* encoder_ptr) {
726 std::unique_ptr<AudioEncoder> old_encoder(std::move(*encoder_ptr));
727 auto sub_encoders = old_encoder->ReclaimContainedEncoders();
728 if (!sub_encoders.empty()) {
729 // Replace enc with its sub encoder. We need to put the sub
730 // encoder in a temporary first, since otherwise the old value
731 // of enc would be destroyed before the new value got assigned,
732 // which would be bad since the new value is a part of the old
733 // value.
734 auto tmp = std::move(sub_encoders[0]);
735 old_encoder = std::move(tmp);
736 }
737 if (new_config.send_codec_spec->cng_payload_type) {
738 AudioEncoderCngConfig config;
739 config.speech_encoder = std::move(old_encoder);
740 config.num_channels = config.speech_encoder->NumChannels();
741 config.payload_type = *new_config.send_codec_spec->cng_payload_type;
742 config.vad_mode = Vad::kVadNormal;
743 *encoder_ptr = CreateComfortNoiseEncoder(std::move(config));
744 } else {
745 *encoder_ptr = std::move(old_encoder);
746 }
747 });
ossu20a4b3f2017-04-27 09:08:52748}
749
750void AudioSendStream::ReconfigureBitrateObserver(
ossu20a4b3f2017-04-27 09:08:52751 const webrtc::AudioSendStream::Config& new_config) {
Sebastian Jansson35cf9e72019-10-04 07:30:32752 RTC_DCHECK_RUN_ON(&worker_thread_checker_);
ossu20a4b3f2017-04-27 09:08:52753 // Since the Config's default is for both of these to be -1, this test will
754 // allow us to configure the bitrate observer if the new config has bitrate
755 // limits set, but would only have us call RemoveBitrateObserver if we were
756 // previously configured with bitrate limits.
Sebastian Jansson35cf9e72019-10-04 07:30:32757 if (config_.min_bitrate_bps == new_config.min_bitrate_bps &&
758 config_.max_bitrate_bps == new_config.max_bitrate_bps &&
759 config_.bitrate_priority == new_config.bitrate_priority &&
760 (TransportSeqNumId(config_) == TransportSeqNumId(new_config) ||
761 !audio_send_side_bwe_)) {
ossu20a4b3f2017-04-27 09:08:52762 return;
763 }
764
Sebastian Janssonf23131f2019-10-03 08:03:55765 if (!new_config.has_dscp && new_config.min_bitrate_bps != -1 &&
Sebastian Janssoncd0eedb2019-10-10 11:52:26766 new_config.max_bitrate_bps != -1 && TransportSeqNumId(new_config) != 0) {
Sebastian Jansson35cf9e72019-10-04 07:30:32767 rtp_transport_->AccountForAudioPacketsInPacedSender(true);
Sebastian Jansson8672cac2019-03-01 14:57:55768 rtc::Event thread_sync_event;
Sebastian Jansson35cf9e72019-10-04 07:30:32769 worker_queue_->PostTask([&] {
770 RTC_DCHECK_RUN_ON(worker_queue_);
771 registered_with_allocator_ = true;
Sebastian Jansson8672cac2019-03-01 14:57:55772 // We may get a callback immediately as the observer is registered, so
773 // make
774 // sure the bitrate limits in config_ are up-to-date.
Sebastian Jansson35cf9e72019-10-04 07:30:32775 config_.min_bitrate_bps = new_config.min_bitrate_bps;
776 config_.max_bitrate_bps = new_config.max_bitrate_bps;
777
778 config_.bitrate_priority = new_config.bitrate_priority;
779 ConfigureBitrateObserver();
Sebastian Jansson8672cac2019-03-01 14:57:55780 thread_sync_event.Set();
781 });
782 thread_sync_event.Wait(rtc::Event::kForever);
Sebastian Jansson35cf9e72019-10-04 07:30:32783 rtp_rtcp_module_->SetAsPartOfAllocation(true);
ossu20a4b3f2017-04-27 09:08:52784 } else {
Sebastian Jansson35cf9e72019-10-04 07:30:32785 rtp_transport_->AccountForAudioPacketsInPacedSender(false);
786 RemoveBitrateObserver();
787 rtp_rtcp_module_->SetAsPartOfAllocation(false);
ossu20a4b3f2017-04-27 09:08:52788 }
789}
790
Sebastian Jansson8672cac2019-03-01 14:57:55791void AudioSendStream::ConfigureBitrateObserver() {
792 // This either updates the current observer or adds a new observer.
793 // TODO(srte): Add overhead compensation here.
Daniel Lee93562522019-05-03 12:40:13794 auto constraints = GetMinMaxBitrateConstraints();
795
Sebastian Jansson0429f782019-10-03 16:32:45796 DataRate priority_bitrate = allocation_settings_.priority_bitrate;
Sebastian Janssonf23131f2019-10-03 08:03:55797 if (send_side_bwe_with_overhead_) {
Sebastian Jansson0429f782019-10-03 16:32:45798 if (use_legacy_overhead_calculation_) {
799 // OverheadPerPacket = Ipv4(20B) + UDP(8B) + SRTP(10B) + RTP(12)
800 constexpr int kOverheadPerPacket = 20 + 8 + 10 + 12;
801 const TimeDelta kMinPacketDuration = TimeDelta::ms(20);
802 DataRate max_overhead =
803 DataSize::bytes(kOverheadPerPacket) / kMinPacketDuration;
804 priority_bitrate += max_overhead;
805 } else {
806 RTC_DCHECK(frame_length_range_);
807 const DataSize kOverheadPerPacket =
808 DataSize::bytes(total_packet_overhead_bytes_);
809 DataRate max_overhead = kOverheadPerPacket / frame_length_range_->first;
810 priority_bitrate += max_overhead;
811 }
Sebastian Janssonf23131f2019-10-03 08:03:55812 }
Sebastian Janssonf23131f2019-10-03 08:03:55813 if (allocation_settings_.priority_bitrate_raw)
814 priority_bitrate = *allocation_settings_.priority_bitrate_raw;
815
Sebastian Jansson8672cac2019-03-01 14:57:55816 bitrate_allocator_->AddObserver(
Daniel Lee93562522019-05-03 12:40:13817 this,
818 MediaStreamAllocationConfig{
819 constraints.min.bps<uint32_t>(), constraints.max.bps<uint32_t>(), 0,
Sebastian Janssonf23131f2019-10-03 08:03:55820 priority_bitrate.bps(), true,
821 allocation_settings_.bitrate_priority.value_or(
Jonas Olsson8f119ca2019-05-08 08:56:23822 config_.bitrate_priority)});
ossu20a4b3f2017-04-27 09:08:52823}
824
825void AudioSendStream::RemoveBitrateObserver() {
Sebastian Janssonc01367d2019-04-08 13:20:44826 RTC_DCHECK(worker_thread_checker_.IsCurrent());
Niels Möllerc572ff32018-11-07 07:43:50827 rtc::Event thread_sync_event;
ossu20a4b3f2017-04-27 09:08:52828 worker_queue_->PostTask([this, &thread_sync_event] {
Sebastian Jansson8672cac2019-03-01 14:57:55829 RTC_DCHECK_RUN_ON(worker_queue_);
830 registered_with_allocator_ = false;
ossu20a4b3f2017-04-27 09:08:52831 bitrate_allocator_->RemoveObserver(this);
832 thread_sync_event.Set();
833 });
834 thread_sync_event.Wait(rtc::Event::kForever);
835}
836
Daniel Lee93562522019-05-03 12:40:13837AudioSendStream::TargetAudioBitrateConstraints
838AudioSendStream::GetMinMaxBitrateConstraints() const {
839 TargetAudioBitrateConstraints constraints{
840 DataRate::bps(config_.min_bitrate_bps),
841 DataRate::bps(config_.max_bitrate_bps)};
842
843 // If bitrates were explicitly overriden via field trial, use those values.
Sebastian Janssonf23131f2019-10-03 08:03:55844 if (allocation_settings_.min_bitrate)
845 constraints.min = *allocation_settings_.min_bitrate;
846 if (allocation_settings_.max_bitrate)
847 constraints.max = *allocation_settings_.max_bitrate;
Daniel Lee93562522019-05-03 12:40:13848
Sebastian Jansson62aee932019-10-02 10:27:06849 RTC_DCHECK_GE(constraints.min, DataRate::Zero());
850 RTC_DCHECK_GE(constraints.max, DataRate::Zero());
851 RTC_DCHECK_GE(constraints.max, constraints.min);
Sebastian Janssonf23131f2019-10-03 08:03:55852 if (send_side_bwe_with_overhead_) {
Sebastian Jansson62aee932019-10-02 10:27:06853 if (use_legacy_overhead_calculation_) {
854 // OverheadPerPacket = Ipv4(20B) + UDP(8B) + SRTP(10B) + RTP(12)
855 const DataSize kOverheadPerPacket = DataSize::bytes(20 + 8 + 10 + 12);
856 const TimeDelta kMaxFrameLength =
857 TimeDelta::ms(60); // Based on Opus spec
858 const DataRate kMinOverhead = kOverheadPerPacket / kMaxFrameLength;
859 constraints.min += kMinOverhead;
860 constraints.max += kMinOverhead;
861 } else {
862 RTC_DCHECK(frame_length_range_);
863 const DataSize kOverheadPerPacket =
864 DataSize::bytes(total_packet_overhead_bytes_);
865 constraints.min += kOverheadPerPacket / frame_length_range_->second;
866 constraints.max += kOverheadPerPacket / frame_length_range_->first;
867 }
Daniel Lee93562522019-05-03 12:40:13868 }
869 return constraints;
870}
871
ossu3b9ff382017-04-27 15:03:42872void AudioSendStream::RegisterCngPayloadType(int payload_type,
873 int clockrate_hz) {
Niels Mölleree5ccbc2019-03-06 15:47:29874 channel_send_->RegisterCngPayloadType(payload_type, clockrate_hz);
ossu3b9ff382017-04-27 15:03:42875}
solenbergc7a8b082015-10-16 21:35:07876} // namespace internal
877} // namespace webrtc