blob: f778745e319bc8771d6e4dd2c212b0329025a240 [file] [log] [blame]
solenbergc7a8b082015-10-16 14:35:07 -07001/*
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 06:47:31 +020011#include "audio/audio_send_stream.h"
solenbergc7a8b082015-10-16 14:35:07 -070012
13#include <string>
ossu20a4b3f2017-04-27 02:08:52 -070014#include <utility>
15#include <vector>
solenbergc7a8b082015-10-16 14:35:07 -070016
Niels Möllerfa4e1852018-08-14 09:43:34 +020017#include "absl/memory/memory.h"
Yves Gerey988cc082018-10-23 12:03:01 +020018#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"
22#include "api/crypto/frameencryptorinterface.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020023#include "audio/audio_state.h"
Yves Gerey988cc082018-10-23 12:03:01 +020024#include "audio/channel_send.h"
Niels Möllerb222f492018-10-03 16:50:08 +020025#include "audio/channel_send_proxy.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020026#include "audio/conversion.h"
Yves Gerey988cc082018-10-23 12:03:01 +020027#include "call/rtp_config.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020028#include "call/rtp_transport_controller_send_interface.h"
Yves Gerey988cc082018-10-23 12:03:01 +020029#include "common_audio/vad/include/vad.h"
30#include "common_types.h" // NOLINT(build/include)
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020031#include "modules/audio_coding/codecs/cng/audio_encoder_cng.h"
Yves Gerey988cc082018-10-23 12:03:01 +020032#include "modules/audio_processing/include/audio_processing.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020033#include "rtc_base/checks.h"
34#include "rtc_base/event.h"
35#include "rtc_base/function_view.h"
36#include "rtc_base/logging.h"
Jonas Olssonabbe8412018-04-03 13:40:05 +020037#include "rtc_base/strings/audio_format_to_string.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020038#include "rtc_base/task_queue.h"
39#include "rtc_base/timeutils.h"
Alex Narestcedd3512017-12-07 20:54:55 +010040#include "system_wrappers/include/field_trial.h"
solenbergc7a8b082015-10-16 14:35:07 -070041
42namespace webrtc {
solenbergc7a8b082015-10-16 14:35:07 -070043namespace internal {
Fredrik Solenberg8f5787a2018-01-11 13:52:30 +010044namespace {
eladalonedd6eea2017-05-25 00:15:35 -070045// TODO(eladalon): Subsequent CL will make these values experiment-dependent.
elad.alond12a8e12017-03-23 11:04:48 -070046constexpr size_t kPacketLossTrackerMaxWindowSizeMs = 15000;
47constexpr size_t kPacketLossRateMinNumAckedPackets = 50;
48constexpr size_t kRecoverablePacketLossRateMinNumAckedPairs = 40;
49
Niels Möllerb222f492018-10-03 16:50:08 +020050void CallEncoder(const std::unique_ptr<voe::ChannelSendProxy>& channel_proxy,
ossu20a4b3f2017-04-27 02:08:52 -070051 rtc::FunctionView<void(AudioEncoder*)> lambda) {
52 channel_proxy->ModifyEncoder([&](std::unique_ptr<AudioEncoder>* encoder_ptr) {
53 RTC_DCHECK(encoder_ptr);
54 lambda(encoder_ptr->get());
55 });
56}
Fredrik Solenberg8f5787a2018-01-11 13:52:30 +010057
Niels Möllerb222f492018-10-03 16:50:08 +020058std::unique_ptr<voe::ChannelSendProxy> CreateChannelAndProxy(
Fredrik Solenberg8f5787a2018-01-11 13:52:30 +010059 rtc::TaskQueue* worker_queue,
Tommi5f223652018-03-26 13:28:26 +020060 ProcessThread* module_process_thread,
Niels Möller7d76a312018-10-26 12:57:07 +020061 MediaTransportInterface* media_transport,
Niels Möllerfa4e1852018-08-14 09:43:34 +020062 RtcpRttStats* rtcp_rtt_stats,
Benjamin Wright84583f62018-10-04 14:22:34 -070063 RtcEventLog* event_log,
Benjamin Wrightbfb444c2018-10-15 10:20:24 -070064 FrameEncryptorInterface* frame_encryptor,
Johannes Kron9190b822018-10-29 11:22:05 +010065 const webrtc::CryptoOptions& crypto_options,
66 bool extmap_allow_mixed) {
Niels Möllerb222f492018-10-03 16:50:08 +020067 return absl::make_unique<voe::ChannelSendProxy>(
Niels Möller7d76a312018-10-26 12:57:07 +020068 absl::make_unique<voe::ChannelSend>(
69 worker_queue, module_process_thread, media_transport, rtcp_rtt_stats,
Johannes Kron9190b822018-10-29 11:22:05 +010070 event_log, frame_encryptor, crypto_options, extmap_allow_mixed));
Fredrik Solenberg8f5787a2018-01-11 13:52:30 +010071}
ossu20a4b3f2017-04-27 02:08:52 -070072} // namespace
73
Sam Zackrisson06953ba2018-02-01 16:53:16 +010074// Helper class to track the actively sending lifetime of this stream.
sazac58f8c02017-07-19 00:39:19 -070075class AudioSendStream::TimedTransport : public Transport {
76 public:
77 TimedTransport(Transport* transport, TimeInterval* time_interval)
78 : transport_(transport), lifetime_(time_interval) {}
79 bool SendRtp(const uint8_t* packet,
80 size_t length,
81 const PacketOptions& options) {
82 if (lifetime_) {
83 lifetime_->Extend();
84 }
85 return transport_->SendRtp(packet, length, options);
86 }
87 bool SendRtcp(const uint8_t* packet, size_t length) {
88 return transport_->SendRtcp(packet, length);
89 }
90 ~TimedTransport() {}
91
92 private:
93 Transport* transport_;
94 TimeInterval* lifetime_;
95};
96
solenberg566ef242015-11-06 15:34:49 -080097AudioSendStream::AudioSendStream(
98 const webrtc::AudioSendStream::Config& config,
Stefan Holmerb86d4e42015-12-07 10:26:18 +010099 const rtc::scoped_refptr<webrtc::AudioState>& audio_state,
perkj26091b12016-09-01 01:17:40 -0700100 rtc::TaskQueue* worker_queue,
Fredrik Solenberg8f5787a2018-01-11 13:52:30 +0100101 ProcessThread* module_process_thread,
Niels Möller7d76a312018-10-26 12:57:07 +0200102 RtpTransportControllerSendInterface* rtp_transport,
Niels Möller67b011d2018-10-22 13:00:40 +0200103 BitrateAllocatorInterface* bitrate_allocator,
michaelt9332b7d2016-11-30 07:51:13 -0800104 RtcEventLog* event_log,
ossuc3d4b482017-05-23 06:07:11 -0700105 RtcpRttStats* rtcp_rtt_stats,
Danil Chapovalovb9b146c2018-06-15 12:28:07 +0200106 const absl::optional<RtpState>& suspended_rtp_state,
Sam Zackrisson06953ba2018-02-01 16:53:16 +0100107 TimeInterval* overall_call_lifetime)
Fredrik Solenberg8f5787a2018-01-11 13:52:30 +0100108 : AudioSendStream(config,
109 audio_state,
110 worker_queue,
Niels Möller7d76a312018-10-26 12:57:07 +0200111 rtp_transport,
Fredrik Solenberg8f5787a2018-01-11 13:52:30 +0100112 bitrate_allocator,
113 event_log,
114 rtcp_rtt_stats,
115 suspended_rtp_state,
Sam Zackrisson06953ba2018-02-01 16:53:16 +0100116 overall_call_lifetime,
Niels Möller530ead42018-10-04 14:28:39 +0200117 CreateChannelAndProxy(worker_queue,
Tommi5f223652018-03-26 13:28:26 +0200118 module_process_thread,
Niels Möller7d76a312018-10-26 12:57:07 +0200119 config.media_transport,
Niels Möllerfa4e1852018-08-14 09:43:34 +0200120 rtcp_rtt_stats,
Benjamin Wright84583f62018-10-04 14:22:34 -0700121 event_log,
Benjamin Wrightbfb444c2018-10-15 10:20:24 -0700122 config.frame_encryptor,
Johannes Kron9190b822018-10-29 11:22:05 +0100123 config.crypto_options,
124 config.rtp.extmap_allow_mixed)) {}
Fredrik Solenberg8f5787a2018-01-11 13:52:30 +0100125
126AudioSendStream::AudioSendStream(
127 const webrtc::AudioSendStream::Config& config,
128 const rtc::scoped_refptr<webrtc::AudioState>& audio_state,
129 rtc::TaskQueue* worker_queue,
Niels Möller7d76a312018-10-26 12:57:07 +0200130 RtpTransportControllerSendInterface* rtp_transport,
Niels Möller67b011d2018-10-22 13:00:40 +0200131 BitrateAllocatorInterface* bitrate_allocator,
Fredrik Solenberg8f5787a2018-01-11 13:52:30 +0100132 RtcEventLog* event_log,
133 RtcpRttStats* rtcp_rtt_stats,
Danil Chapovalovb9b146c2018-06-15 12:28:07 +0200134 const absl::optional<RtpState>& suspended_rtp_state,
Sam Zackrisson06953ba2018-02-01 16:53:16 +0100135 TimeInterval* overall_call_lifetime,
Niels Möllerb222f492018-10-03 16:50:08 +0200136 std::unique_ptr<voe::ChannelSendProxy> channel_proxy)
perkj26091b12016-09-01 01:17:40 -0700137 : worker_queue_(worker_queue),
Niels Möller7d76a312018-10-26 12:57:07 +0200138 config_(Config(/*send_transport=*/nullptr,
139 /*media_transport=*/nullptr)),
mflodman86cc6ff2016-07-26 04:44:06 -0700140 audio_state_(audio_state),
Fredrik Solenberg8f5787a2018-01-11 13:52:30 +0100141 channel_proxy_(std::move(channel_proxy)),
ossu20a4b3f2017-04-27 02:08:52 -0700142 event_log_(event_log),
michaeltf4caaab2017-01-16 23:55:07 -0800143 bitrate_allocator_(bitrate_allocator),
Niels Möller7d76a312018-10-26 12:57:07 +0200144 rtp_transport_(rtp_transport),
elad.alond12a8e12017-03-23 11:04:48 -0700145 packet_loss_tracker_(kPacketLossTrackerMaxWindowSizeMs,
146 kPacketLossRateMinNumAckedPackets,
ossuc3d4b482017-05-23 06:07:11 -0700147 kRecoverablePacketLossRateMinNumAckedPairs),
148 rtp_rtcp_module_(nullptr),
Sam Zackrisson06953ba2018-02-01 16:53:16 +0100149 suspended_rtp_state_(suspended_rtp_state),
150 overall_call_lifetime_(overall_call_lifetime) {
Jonas Olsson24ea8222018-01-25 10:14:29 +0100151 RTC_LOG(LS_INFO) << "AudioSendStream: " << config.rtp.ssrc;
Fredrik Solenberg8f5787a2018-01-11 13:52:30 +0100152 RTC_DCHECK(worker_queue_);
153 RTC_DCHECK(audio_state_);
154 RTC_DCHECK(channel_proxy_);
155 RTC_DCHECK(bitrate_allocator_);
Niels Möller7d76a312018-10-26 12:57:07 +0200156 // TODO(nisse): Eventually, we should have only media_transport. But for the
157 // time being, we can have either. When media transport is injected, there
158 // should be no rtp_transport, and below check should be strengthened to XOR
159 // (either rtp_transport or media_transport but not both).
160 RTC_DCHECK(rtp_transport || config.media_transport);
Sam Zackrisson06953ba2018-02-01 16:53:16 +0100161 RTC_DCHECK(overall_call_lifetime_);
solenberg3a941542015-11-16 07:34:50 -0800162
solenberg13725082015-11-25 08:16:52 -0800163 channel_proxy_->SetRTCPStatus(true);
Niels Möller848d6d32018-08-08 10:49:16 +0200164 rtp_rtcp_module_ = channel_proxy_->GetRtpRtcp();
ossuc3d4b482017-05-23 06:07:11 -0700165 RTC_DCHECK(rtp_rtcp_module_);
mflodman3d7db262016-04-29 00:57:13 -0700166
ossu20a4b3f2017-04-27 02:08:52 -0700167 ConfigureStream(this, config, true);
elad.alond12a8e12017-03-23 11:04:48 -0700168
169 pacer_thread_checker_.DetachFromThread();
Niels Möller7d76a312018-10-26 12:57:07 +0200170 if (rtp_transport_) {
171 // Signal congestion controller this object is ready for OnPacket*
172 // callbacks.
173 rtp_transport_->RegisterPacketFeedbackObserver(this);
174 }
solenbergc7a8b082015-10-16 14:35:07 -0700175}
176
177AudioSendStream::~AudioSendStream() {
elad.alond12a8e12017-03-23 11:04:48 -0700178 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
Jonas Olsson24ea8222018-01-25 10:14:29 +0100179 RTC_LOG(LS_INFO) << "~AudioSendStream: " << config_.rtp.ssrc;
Fredrik Solenberg2a877972017-12-15 16:42:15 +0100180 RTC_DCHECK(!sending_);
Niels Möller7d76a312018-10-26 12:57:07 +0200181 if (rtp_transport_) {
182 rtp_transport_->DeRegisterPacketFeedbackObserver(this);
183 channel_proxy_->RegisterTransport(nullptr);
184 channel_proxy_->ResetSenderCongestionControlObjects();
185 }
Sam Zackrisson06953ba2018-02-01 16:53:16 +0100186 // Lifetime can only be updated after deregistering
187 // |timed_send_transport_adapter_| in the underlying channel object to avoid
188 // data races in |active_lifetime_|.
189 overall_call_lifetime_->Extend(active_lifetime_);
solenbergc7a8b082015-10-16 14:35:07 -0700190}
191
eladalonabbc4302017-07-26 02:09:44 -0700192const webrtc::AudioSendStream::Config& AudioSendStream::GetConfig() const {
193 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
194 return config_;
195}
196
ossu20a4b3f2017-04-27 02:08:52 -0700197void AudioSendStream::Reconfigure(
198 const webrtc::AudioSendStream::Config& new_config) {
Fredrik Solenberg2a877972017-12-15 16:42:15 +0100199 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
ossu20a4b3f2017-04-27 02:08:52 -0700200 ConfigureStream(this, new_config, false);
201}
202
Alex Narestcedd3512017-12-07 20:54:55 +0100203AudioSendStream::ExtensionIds AudioSendStream::FindExtensionIds(
204 const std::vector<RtpExtension>& extensions) {
205 ExtensionIds ids;
206 for (const auto& extension : extensions) {
207 if (extension.uri == RtpExtension::kAudioLevelUri) {
208 ids.audio_level = extension.id;
209 } else if (extension.uri == RtpExtension::kTransportSequenceNumberUri) {
210 ids.transport_sequence_number = extension.id;
Steve Antonbb50ce52018-03-26 10:24:32 -0700211 } else if (extension.uri == RtpExtension::kMidUri) {
212 ids.mid = extension.id;
Alex Narestcedd3512017-12-07 20:54:55 +0100213 }
214 }
215 return ids;
216}
217
ossu20a4b3f2017-04-27 02:08:52 -0700218void AudioSendStream::ConfigureStream(
219 webrtc::internal::AudioSendStream* stream,
220 const webrtc::AudioSendStream::Config& new_config,
221 bool first_time) {
Jonas Olsson24ea8222018-01-25 10:14:29 +0100222 RTC_LOG(LS_INFO) << "AudioSendStream::ConfigureStream: "
223 << new_config.ToString();
ossu20a4b3f2017-04-27 02:08:52 -0700224 const auto& channel_proxy = stream->channel_proxy_;
225 const auto& old_config = stream->config_;
226
227 if (first_time || old_config.rtp.ssrc != new_config.rtp.ssrc) {
228 channel_proxy->SetLocalSSRC(new_config.rtp.ssrc);
ossuc3d4b482017-05-23 06:07:11 -0700229 if (stream->suspended_rtp_state_) {
230 stream->rtp_rtcp_module_->SetRtpState(*stream->suspended_rtp_state_);
231 }
ossu20a4b3f2017-04-27 02:08:52 -0700232 }
233 if (first_time || old_config.rtp.c_name != new_config.rtp.c_name) {
234 channel_proxy->SetRTCP_CNAME(new_config.rtp.c_name);
235 }
236 // TODO(solenberg): Config NACK history window (which is a packet count),
237 // using the actual packet size for the configured codec.
238 if (first_time || old_config.rtp.nack.rtp_history_ms !=
239 new_config.rtp.nack.rtp_history_ms) {
240 channel_proxy->SetNACKStatus(new_config.rtp.nack.rtp_history_ms != 0,
241 new_config.rtp.nack.rtp_history_ms / 20);
242 }
243
Yves Gerey665174f2018-06-19 15:03:05 +0200244 if (first_time || new_config.send_transport != old_config.send_transport) {
ossu20a4b3f2017-04-27 02:08:52 -0700245 if (old_config.send_transport) {
solenberg1c239d42017-09-29 06:00:28 -0700246 channel_proxy->RegisterTransport(nullptr);
ossu20a4b3f2017-04-27 02:08:52 -0700247 }
sazac58f8c02017-07-19 00:39:19 -0700248 if (new_config.send_transport) {
249 stream->timed_send_transport_adapter_.reset(new TimedTransport(
250 new_config.send_transport, &stream->active_lifetime_));
251 } else {
252 stream->timed_send_transport_adapter_.reset(nullptr);
253 }
solenberg1c239d42017-09-29 06:00:28 -0700254 channel_proxy->RegisterTransport(
sazac58f8c02017-07-19 00:39:19 -0700255 stream->timed_send_transport_adapter_.get());
ossu20a4b3f2017-04-27 02:08:52 -0700256 }
257
Benjamin Wright84583f62018-10-04 14:22:34 -0700258 // Enable the frame encryptor if a new frame encryptor has been provided.
259 if (first_time || new_config.frame_encryptor != old_config.frame_encryptor) {
260 channel_proxy->SetFrameEncryptor(new_config.frame_encryptor);
261 }
262
Johannes Kron9190b822018-10-29 11:22:05 +0100263 if (first_time ||
264 new_config.rtp.extmap_allow_mixed != old_config.rtp.extmap_allow_mixed) {
265 channel_proxy->SetExtmapAllowMixed(new_config.rtp.extmap_allow_mixed);
266 }
267
Alex Narestcedd3512017-12-07 20:54:55 +0100268 const ExtensionIds old_ids = FindExtensionIds(old_config.rtp.extensions);
269 const ExtensionIds new_ids = FindExtensionIds(new_config.rtp.extensions);
ossu20a4b3f2017-04-27 02:08:52 -0700270 // Audio level indication
271 if (first_time || new_ids.audio_level != old_ids.audio_level) {
272 channel_proxy->SetSendAudioLevelIndicationStatus(new_ids.audio_level != 0,
273 new_ids.audio_level);
274 }
Sebastian Jansson8d9c5402017-11-15 17:22:16 +0100275 bool transport_seq_num_id_changed =
276 new_ids.transport_sequence_number != old_ids.transport_sequence_number;
Alex Narest867e5102018-06-12 13:40:18 +0200277 if (first_time ||
278 (transport_seq_num_id_changed &&
279 !webrtc::field_trial::IsEnabled("WebRTC-Audio-ForceNoTWCC"))) {
ossu1129df22017-06-30 01:38:56 -0700280 if (!first_time) {
ossu20a4b3f2017-04-27 02:08:52 -0700281 channel_proxy->ResetSenderCongestionControlObjects();
ossu20a4b3f2017-04-27 02:08:52 -0700282 }
283
Sebastian Jansson8d9c5402017-11-15 17:22:16 +0100284 RtcpBandwidthObserver* bandwidth_observer = nullptr;
Alex Narest867e5102018-06-12 13:40:18 +0200285 bool has_transport_sequence_number =
286 new_ids.transport_sequence_number != 0 &&
287 !webrtc::field_trial::IsEnabled("WebRTC-Audio-ForceNoTWCC");
Sebastian Jansson8d9c5402017-11-15 17:22:16 +0100288 if (has_transport_sequence_number) {
ossu20a4b3f2017-04-27 02:08:52 -0700289 channel_proxy->EnableSendTransportSequenceNumber(
290 new_ids.transport_sequence_number);
Sebastian Jansson8d9c5402017-11-15 17:22:16 +0100291 // Probing in application limited region is only used in combination with
292 // send side congestion control, wich depends on feedback packets which
293 // requires transport sequence numbers to be enabled.
Niels Möller7d76a312018-10-26 12:57:07 +0200294 if (stream->rtp_transport_) {
295 stream->rtp_transport_->EnablePeriodicAlrProbing(true);
296 bandwidth_observer = stream->rtp_transport_->GetBandwidthObserver();
297 }
ossu20a4b3f2017-04-27 02:08:52 -0700298 }
Niels Möller7d76a312018-10-26 12:57:07 +0200299 if (stream->rtp_transport_) {
300 channel_proxy->RegisterSenderCongestionControlObjects(
301 stream->rtp_transport_, bandwidth_observer);
302 }
ossu20a4b3f2017-04-27 02:08:52 -0700303 }
Steve Antonbb50ce52018-03-26 10:24:32 -0700304 // MID RTP header extension.
Steve Anton003930a2018-03-29 12:37:21 -0700305 if ((first_time || new_ids.mid != old_ids.mid ||
306 new_config.rtp.mid != old_config.rtp.mid) &&
307 new_ids.mid != 0 && !new_config.rtp.mid.empty()) {
Steve Antonbb50ce52018-03-26 10:24:32 -0700308 channel_proxy->SetMid(new_config.rtp.mid, new_ids.mid);
309 }
310
ossu20a4b3f2017-04-27 02:08:52 -0700311 if (!ReconfigureSendCodec(stream, new_config)) {
Mirko Bonadei675513b2017-11-09 11:09:25 +0100312 RTC_LOG(LS_ERROR) << "Failed to set up send codec state.";
ossu20a4b3f2017-04-27 02:08:52 -0700313 }
314
Oskar Sundbomf85e31b2017-12-20 16:38:09 +0100315 if (stream->sending_) {
316 ReconfigureBitrateObserver(stream, new_config);
317 }
ossu20a4b3f2017-04-27 02:08:52 -0700318 stream->config_ = new_config;
319}
320
solenberg3a941542015-11-16 07:34:50 -0800321void AudioSendStream::Start() {
elad.alond12a8e12017-03-23 11:04:48 -0700322 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
Fredrik Solenberg2a877972017-12-15 16:42:15 +0100323 if (sending_) {
324 return;
325 }
326
Sebastian Jansson763e9472018-03-21 12:46:56 +0100327 bool has_transport_sequence_number =
Alex Narest867e5102018-06-12 13:40:18 +0200328 FindExtensionIds(config_.rtp.extensions).transport_sequence_number != 0 &&
329 !webrtc::field_trial::IsEnabled("WebRTC-Audio-ForceNoTWCC");
Alex Narestcedd3512017-12-07 20:54:55 +0100330 if (config_.min_bitrate_bps != -1 && config_.max_bitrate_bps != -1 &&
Tim Haloun648d28a2018-10-18 16:52:22 -0700331 !config_.has_dscp &&
Sebastian Jansson763e9472018-03-21 12:46:56 +0100332 (has_transport_sequence_number ||
Alex Narestbcf91802018-06-25 16:08:36 +0200333 !webrtc::field_trial::IsEnabled("WebRTC-Audio-SendSideBwe") ||
334 webrtc::field_trial::IsEnabled("WebRTC-Audio-ABWENoTWCC"))) {
Alex Narest78609d52017-10-20 10:37:47 +0200335 // Audio BWE is enabled.
Niels Möller7d76a312018-10-26 12:57:07 +0200336 rtp_transport_->packet_sender()->SetAccountForAudioPackets(true);
Sebastian Janssonb6863962018-10-10 10:23:13 +0200337 rtp_rtcp_module_->SetAsPartOfAllocation(true);
Seth Hampson24722b32017-12-22 09:36:42 -0800338 ConfigureBitrateObserver(config_.min_bitrate_bps, config_.max_bitrate_bps,
Sebastian Jansson763e9472018-03-21 12:46:56 +0100339 config_.bitrate_priority,
340 has_transport_sequence_number);
Sebastian Janssonb6863962018-10-10 10:23:13 +0200341 } else {
342 rtp_rtcp_module_->SetAsPartOfAllocation(false);
mflodman86cc6ff2016-07-26 04:44:06 -0700343 }
Fredrik Solenbergaaedf752017-12-18 13:09:12 +0100344 channel_proxy_->StartSend();
Fredrik Solenberg2a877972017-12-15 16:42:15 +0100345 sending_ = true;
346 audio_state()->AddSendingStream(this, encoder_sample_rate_hz_,
347 encoder_num_channels_);
solenberg3a941542015-11-16 07:34:50 -0800348}
349
350void AudioSendStream::Stop() {
elad.alond12a8e12017-03-23 11:04:48 -0700351 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
Fredrik Solenberg2a877972017-12-15 16:42:15 +0100352 if (!sending_) {
353 return;
354 }
355
ossu20a4b3f2017-04-27 02:08:52 -0700356 RemoveBitrateObserver();
Fredrik Solenbergaaedf752017-12-18 13:09:12 +0100357 channel_proxy_->StopSend();
Fredrik Solenberg2a877972017-12-15 16:42:15 +0100358 sending_ = false;
359 audio_state()->RemoveSendingStream(this);
360}
361
362void AudioSendStream::SendAudioData(std::unique_ptr<AudioFrame> audio_frame) {
363 RTC_CHECK_RUNS_SERIALIZED(&audio_capture_race_checker_);
364 channel_proxy_->ProcessAndEncodeAudio(std::move(audio_frame));
solenberg3a941542015-11-16 07:34:50 -0800365}
366
solenbergffbbcac2016-11-17 05:25:37 -0800367bool AudioSendStream::SendTelephoneEvent(int payload_type,
Yves Gerey665174f2018-06-19 15:03:05 +0200368 int payload_frequency,
369 int event,
solenberg8842c3e2016-03-11 03:06:41 -0800370 int duration_ms) {
elad.alond12a8e12017-03-23 11:04:48 -0700371 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
solenbergffbbcac2016-11-17 05:25:37 -0800372 return channel_proxy_->SetSendTelephoneEventPayloadType(payload_type,
373 payload_frequency) &&
Fredrik Solenbergb5727682015-12-04 15:22:19 +0100374 channel_proxy_->SendTelephoneEventOutband(event, duration_ms);
375}
376
solenberg94218532016-06-16 10:53:22 -0700377void AudioSendStream::SetMuted(bool muted) {
elad.alond12a8e12017-03-23 11:04:48 -0700378 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
solenberg94218532016-06-16 10:53:22 -0700379 channel_proxy_->SetInputMute(muted);
380}
381
solenbergc7a8b082015-10-16 14:35:07 -0700382webrtc::AudioSendStream::Stats AudioSendStream::GetStats() const {
Ivo Creusen56d46092017-11-24 17:29:59 +0100383 return GetStats(true);
384}
385
386webrtc::AudioSendStream::Stats AudioSendStream::GetStats(
387 bool has_remote_tracks) const {
elad.alond12a8e12017-03-23 11:04:48 -0700388 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
solenberg85a04962015-10-27 03:35:21 -0700389 webrtc::AudioSendStream::Stats stats;
390 stats.local_ssrc = config_.rtp.ssrc;
Sebastian Jansson359d60a2018-10-25 16:22:02 +0200391 stats.target_bitrate_bps = channel_proxy_->GetBitrate();
solenberg85a04962015-10-27 03:35:21 -0700392
Niels Möller530ead42018-10-04 14:28:39 +0200393 webrtc::CallSendStatistics call_stats = channel_proxy_->GetRTCPStatistics();
solenberg85a04962015-10-27 03:35:21 -0700394 stats.bytes_sent = call_stats.bytesSent;
395 stats.packets_sent = call_stats.packetsSent;
solenberg8b85de22015-11-16 09:48:04 -0800396 // RTT isn't known until a RTCP report is received. Until then, VoiceEngine
397 // returns 0 to indicate an error value.
398 if (call_stats.rttMs > 0) {
399 stats.rtt_ms = call_stats.rttMs;
400 }
ossu20a4b3f2017-04-27 02:08:52 -0700401 if (config_.send_codec_spec) {
402 const auto& spec = *config_.send_codec_spec;
403 stats.codec_name = spec.format.name;
Oskar Sundbom2707fb22017-11-16 10:57:35 +0100404 stats.codec_payload_type = spec.payload_type;
solenberg85a04962015-10-27 03:35:21 -0700405
406 // Get data from the last remote RTCP report.
solenberg358057b2015-11-27 10:46:42 -0800407 for (const auto& block : channel_proxy_->GetRemoteRTCPReportBlocks()) {
solenberg8b85de22015-11-16 09:48:04 -0800408 // Lookup report for send ssrc only.
409 if (block.source_SSRC == stats.local_ssrc) {
410 stats.packets_lost = block.cumulative_num_packets_lost;
411 stats.fraction_lost = Q8ToFloat(block.fraction_lost);
412 stats.ext_seqnum = block.extended_highest_sequence_number;
ossu20a4b3f2017-04-27 02:08:52 -0700413 // Convert timestamps to milliseconds.
414 if (spec.format.clockrate_hz / 1000 > 0) {
solenberg8b85de22015-11-16 09:48:04 -0800415 stats.jitter_ms =
ossu20a4b3f2017-04-27 02:08:52 -0700416 block.interarrival_jitter / (spec.format.clockrate_hz / 1000);
solenberg85a04962015-10-27 03:35:21 -0700417 }
solenberg8b85de22015-11-16 09:48:04 -0800418 break;
solenberg85a04962015-10-27 03:35:21 -0700419 }
420 }
421 }
422
Fredrik Solenberg2a877972017-12-15 16:42:15 +0100423 AudioState::Stats input_stats = audio_state()->GetAudioInputStats();
424 stats.audio_level = input_stats.audio_level;
425 stats.total_input_energy = input_stats.total_energy;
426 stats.total_input_duration = input_stats.total_duration;
solenberg796b8f92017-03-01 17:02:23 -0800427
Fredrik Solenberg2a877972017-12-15 16:42:15 +0100428 stats.typing_noise_detected = audio_state()->typing_noise_detected();
ivoce1198e02017-09-08 08:13:19 -0700429 stats.ana_statistics = channel_proxy_->GetANAStatistics();
Ivo Creusen56d46092017-11-24 17:29:59 +0100430 RTC_DCHECK(audio_state_->audio_processing());
431 stats.apm_statistics =
432 audio_state_->audio_processing()->GetStatistics(has_remote_tracks);
solenberg85a04962015-10-27 03:35:21 -0700433
434 return stats;
435}
436
pbos1ba8d392016-05-01 20:18:34 -0700437void AudioSendStream::SignalNetworkState(NetworkState state) {
elad.alond12a8e12017-03-23 11:04:48 -0700438 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
pbos1ba8d392016-05-01 20:18:34 -0700439}
440
441bool AudioSendStream::DeliverRtcp(const uint8_t* packet, size_t length) {
442 // TODO(solenberg): Tests call this function on a network thread, libjingle
443 // calls on the worker thread. We should move towards always using a network
444 // thread. Then this check can be enabled.
elad.alond12a8e12017-03-23 11:04:48 -0700445 // RTC_DCHECK(!worker_thread_checker_.CalledOnValidThread());
pbos1ba8d392016-05-01 20:18:34 -0700446 return channel_proxy_->ReceivedRTCPPacket(packet, length);
447}
448
Sebastian Janssonc0e4d452018-10-25 15:08:32 +0200449uint32_t AudioSendStream::OnBitrateUpdated(BitrateAllocationUpdate update) {
stefanfca900a2017-04-10 03:53:00 -0700450 // A send stream may be allocated a bitrate of zero if the allocator decides
451 // to disable it. For now we ignore this decision and keep sending on min
452 // bitrate.
Sebastian Janssonc0e4d452018-10-25 15:08:32 +0200453 if (update.bitrate_bps == 0) {
454 update.bitrate_bps = config_.min_bitrate_bps;
stefanfca900a2017-04-10 03:53:00 -0700455 }
Sebastian Janssonc0e4d452018-10-25 15:08:32 +0200456 RTC_DCHECK_GE(update.bitrate_bps,
457 static_cast<uint32_t>(config_.min_bitrate_bps));
mflodman86cc6ff2016-07-26 04:44:06 -0700458 // The bitrate allocator might allocate an higher than max configured bitrate
459 // if there is room, to allow for, as example, extra FEC. Ignore that for now.
minyue10cbb462016-11-07 09:29:22 -0800460 const uint32_t max_bitrate_bps = config_.max_bitrate_bps;
Sebastian Janssonc0e4d452018-10-25 15:08:32 +0200461 if (update.bitrate_bps > max_bitrate_bps)
462 update.bitrate_bps = max_bitrate_bps;
mflodman86cc6ff2016-07-26 04:44:06 -0700463
Sebastian Janssonc0e4d452018-10-25 15:08:32 +0200464 channel_proxy_->SetBitrate(update.bitrate_bps, update.bwe_period_ms);
mflodman86cc6ff2016-07-26 04:44:06 -0700465
466 // The amount of audio protection is not exposed by the encoder, hence
467 // always returning 0.
468 return 0;
469}
470
elad.alond12a8e12017-03-23 11:04:48 -0700471void AudioSendStream::OnPacketAdded(uint32_t ssrc, uint16_t seq_num) {
472 RTC_DCHECK(pacer_thread_checker_.CalledOnValidThread());
473 // Only packets that belong to this stream are of interest.
474 if (ssrc == config_.rtp.ssrc) {
475 rtc::CritScope lock(&packet_loss_tracker_cs_);
eladalonedd6eea2017-05-25 00:15:35 -0700476 // TODO(eladalon): This function call could potentially reset the window,
elad.alond12a8e12017-03-23 11:04:48 -0700477 // setting both PLR and RPLR to unknown. Consider (during upcoming
478 // refactoring) passing an indication of such an event.
479 packet_loss_tracker_.OnPacketAdded(seq_num, rtc::TimeMillis());
480 }
481}
482
483void AudioSendStream::OnPacketFeedbackVector(
484 const std::vector<PacketFeedback>& packet_feedback_vector) {
eladalon3651fdd2017-08-24 07:26:25 -0700485 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
Danil Chapovalovb9b146c2018-06-15 12:28:07 +0200486 absl::optional<float> plr;
487 absl::optional<float> rplr;
elad.alond12a8e12017-03-23 11:04:48 -0700488 {
489 rtc::CritScope lock(&packet_loss_tracker_cs_);
490 packet_loss_tracker_.OnPacketFeedbackVector(packet_feedback_vector);
491 plr = packet_loss_tracker_.GetPacketLossRate();
elad.alondadb4dc2017-03-23 15:29:50 -0700492 rplr = packet_loss_tracker_.GetRecoverablePacketLossRate();
elad.alond12a8e12017-03-23 11:04:48 -0700493 }
eladalonedd6eea2017-05-25 00:15:35 -0700494 // TODO(eladalon): If R/PLR go back to unknown, no indication is given that
elad.alond12a8e12017-03-23 11:04:48 -0700495 // the previously sent value is no longer relevant. This will be taken care
496 // of with some refactoring which is now being done.
497 if (plr) {
498 channel_proxy_->OnTwccBasedUplinkPacketLossRate(*plr);
499 }
elad.alondadb4dc2017-03-23 15:29:50 -0700500 if (rplr) {
501 channel_proxy_->OnRecoverableUplinkPacketLossRate(*rplr);
502 }
elad.alond12a8e12017-03-23 11:04:48 -0700503}
504
michaelt79e05882016-11-08 02:50:09 -0800505void AudioSendStream::SetTransportOverhead(int transport_overhead_per_packet) {
elad.alond12a8e12017-03-23 11:04:48 -0700506 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
michaelt79e05882016-11-08 02:50:09 -0800507 channel_proxy_->SetTransportOverhead(transport_overhead_per_packet);
508}
509
ossuc3d4b482017-05-23 06:07:11 -0700510RtpState AudioSendStream::GetRtpState() const {
511 return rtp_rtcp_module_->GetRtpState();
512}
513
Niels Möllerb222f492018-10-03 16:50:08 +0200514const voe::ChannelSendProxy& AudioSendStream::GetChannelProxy() const {
Fredrik Solenberg8f5787a2018-01-11 13:52:30 +0100515 RTC_DCHECK(channel_proxy_.get());
516 return *channel_proxy_.get();
517}
518
Fredrik Solenberg2a877972017-12-15 16:42:15 +0100519internal::AudioState* AudioSendStream::audio_state() {
520 internal::AudioState* audio_state =
521 static_cast<internal::AudioState*>(audio_state_.get());
522 RTC_DCHECK(audio_state);
523 return audio_state;
524}
525
526const internal::AudioState* AudioSendStream::audio_state() const {
527 internal::AudioState* audio_state =
528 static_cast<internal::AudioState*>(audio_state_.get());
529 RTC_DCHECK(audio_state);
530 return audio_state;
531}
532
Fredrik Solenberg2a877972017-12-15 16:42:15 +0100533void AudioSendStream::StoreEncoderProperties(int sample_rate_hz,
534 size_t num_channels) {
535 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
536 encoder_sample_rate_hz_ = sample_rate_hz;
537 encoder_num_channels_ = num_channels;
538 if (sending_) {
539 // Update AudioState's information about the stream.
540 audio_state()->AddSendingStream(this, sample_rate_hz, num_channels);
541 }
542}
543
minyue7a973442016-10-20 03:27:12 -0700544// Apply current codec settings to a single voe::Channel used for sending.
ossu20a4b3f2017-04-27 02:08:52 -0700545bool AudioSendStream::SetupSendCodec(AudioSendStream* stream,
546 const Config& new_config) {
547 RTC_DCHECK(new_config.send_codec_spec);
548 const auto& spec = *new_config.send_codec_spec;
minyue48368ad2017-05-10 04:06:11 -0700549
550 RTC_DCHECK(new_config.encoder_factory);
ossu20a4b3f2017-04-27 02:08:52 -0700551 std::unique_ptr<AudioEncoder> encoder =
Karl Wiberg77490b92018-03-21 15:18:42 +0100552 new_config.encoder_factory->MakeAudioEncoder(
553 spec.payload_type, spec.format, new_config.codec_pair_id);
minyue7a973442016-10-20 03:27:12 -0700554
ossu20a4b3f2017-04-27 02:08:52 -0700555 if (!encoder) {
Jonas Olssonabbe8412018-04-03 13:40:05 +0200556 RTC_DLOG(LS_ERROR) << "Unable to create encoder for "
557 << rtc::ToString(spec.format);
ossu20a4b3f2017-04-27 02:08:52 -0700558 return false;
559 }
Alex Narestbbbe4e12018-07-13 10:32:58 +0200560
561 // If other side does not support audio TWCC and WebRTC-Audio-ABWENoTWCC is
562 // not enabled, do not update target audio bitrate if we are in
563 // WebRTC-Audio-SendSideBwe-For-Video experiment
564 const bool do_not_update_target_bitrate =
565 !webrtc::field_trial::IsEnabled("WebRTC-Audio-ABWENoTWCC") &&
566 webrtc::field_trial::IsEnabled("WebRTC-Audio-SendSideBwe-For-Video") &&
567 !FindExtensionIds(new_config.rtp.extensions).transport_sequence_number;
ossu20a4b3f2017-04-27 02:08:52 -0700568 // If a bitrate has been specified for the codec, use it over the
569 // codec's default.
Alex Narestbbbe4e12018-07-13 10:32:58 +0200570 if (!do_not_update_target_bitrate && spec.target_bitrate_bps) {
ossu20a4b3f2017-04-27 02:08:52 -0700571 encoder->OnReceivedTargetAudioBitrate(*spec.target_bitrate_bps);
minyue7a973442016-10-20 03:27:12 -0700572 }
573
ossu20a4b3f2017-04-27 02:08:52 -0700574 // Enable ANA if configured (currently only used by Opus).
575 if (new_config.audio_network_adaptor_config) {
576 if (encoder->EnableAudioNetworkAdaptor(
577 *new_config.audio_network_adaptor_config, stream->event_log_)) {
Jonas Olsson24ea8222018-01-25 10:14:29 +0100578 RTC_DLOG(LS_INFO) << "Audio network adaptor enabled on SSRC "
579 << new_config.rtp.ssrc;
ossu20a4b3f2017-04-27 02:08:52 -0700580 } else {
581 RTC_NOTREACHED();
minyue6b825df2016-10-31 04:08:32 -0700582 }
minyue7a973442016-10-20 03:27:12 -0700583 }
584
ossu20a4b3f2017-04-27 02:08:52 -0700585 // Wrap the encoder in a an AudioEncoderCNG, if VAD is enabled.
586 if (spec.cng_payload_type) {
587 AudioEncoderCng::Config cng_config;
588 cng_config.num_channels = encoder->NumChannels();
589 cng_config.payload_type = *spec.cng_payload_type;
590 cng_config.speech_encoder = std::move(encoder);
591 cng_config.vad_mode = Vad::kVadNormal;
592 encoder.reset(new AudioEncoderCng(std::move(cng_config)));
ossu3b9ff382017-04-27 08:03:42 -0700593
594 stream->RegisterCngPayloadType(
595 *spec.cng_payload_type,
596 new_config.send_codec_spec->format.clockrate_hz);
minyue7a973442016-10-20 03:27:12 -0700597 }
ossu20a4b3f2017-04-27 02:08:52 -0700598
Fredrik Solenberg2a877972017-12-15 16:42:15 +0100599 stream->StoreEncoderProperties(encoder->SampleRateHz(),
600 encoder->NumChannels());
ossu20a4b3f2017-04-27 02:08:52 -0700601 stream->channel_proxy_->SetEncoder(new_config.send_codec_spec->payload_type,
602 std::move(encoder));
minyue7a973442016-10-20 03:27:12 -0700603 return true;
604}
605
ossu20a4b3f2017-04-27 02:08:52 -0700606bool AudioSendStream::ReconfigureSendCodec(AudioSendStream* stream,
607 const Config& new_config) {
608 const auto& old_config = stream->config_;
minyue-webrtc8de18262017-07-26 14:18:40 +0200609
610 if (!new_config.send_codec_spec) {
611 // We cannot de-configure a send codec. So we will do nothing.
612 // By design, the send codec should have not been configured.
613 RTC_DCHECK(!old_config.send_codec_spec);
614 return true;
615 }
616
617 if (new_config.send_codec_spec == old_config.send_codec_spec &&
618 new_config.audio_network_adaptor_config ==
619 old_config.audio_network_adaptor_config) {
ossu20a4b3f2017-04-27 02:08:52 -0700620 return true;
621 }
622
623 // If we have no encoder, or the format or payload type's changed, create a
624 // new encoder.
625 if (!old_config.send_codec_spec ||
626 new_config.send_codec_spec->format !=
627 old_config.send_codec_spec->format ||
628 new_config.send_codec_spec->payload_type !=
629 old_config.send_codec_spec->payload_type) {
630 return SetupSendCodec(stream, new_config);
631 }
632
Alex Narestbbbe4e12018-07-13 10:32:58 +0200633 // If other side does not support audio TWCC and WebRTC-Audio-ABWENoTWCC is
634 // not enabled, do not update target audio bitrate if we are in
635 // WebRTC-Audio-SendSideBwe-For-Video experiment
636 const bool do_not_update_target_bitrate =
637 !webrtc::field_trial::IsEnabled("WebRTC-Audio-ABWENoTWCC") &&
638 webrtc::field_trial::IsEnabled("WebRTC-Audio-SendSideBwe-For-Video") &&
639 !FindExtensionIds(new_config.rtp.extensions).transport_sequence_number;
640
Danil Chapovalovb9b146c2018-06-15 12:28:07 +0200641 const absl::optional<int>& new_target_bitrate_bps =
ossu20a4b3f2017-04-27 02:08:52 -0700642 new_config.send_codec_spec->target_bitrate_bps;
643 // If a bitrate has been specified for the codec, use it over the
644 // codec's default.
Alex Narestbbbe4e12018-07-13 10:32:58 +0200645 if (!do_not_update_target_bitrate && new_target_bitrate_bps &&
ossu20a4b3f2017-04-27 02:08:52 -0700646 new_target_bitrate_bps !=
647 old_config.send_codec_spec->target_bitrate_bps) {
648 CallEncoder(stream->channel_proxy_, [&](AudioEncoder* encoder) {
649 encoder->OnReceivedTargetAudioBitrate(*new_target_bitrate_bps);
650 });
651 }
652
653 ReconfigureANA(stream, new_config);
654 ReconfigureCNG(stream, new_config);
655
656 return true;
657}
658
659void AudioSendStream::ReconfigureANA(AudioSendStream* stream,
660 const Config& new_config) {
661 if (new_config.audio_network_adaptor_config ==
662 stream->config_.audio_network_adaptor_config) {
663 return;
664 }
665 if (new_config.audio_network_adaptor_config) {
666 CallEncoder(stream->channel_proxy_, [&](AudioEncoder* encoder) {
667 if (encoder->EnableAudioNetworkAdaptor(
668 *new_config.audio_network_adaptor_config, stream->event_log_)) {
Jonas Olsson24ea8222018-01-25 10:14:29 +0100669 RTC_DLOG(LS_INFO) << "Audio network adaptor enabled on SSRC "
670 << new_config.rtp.ssrc;
ossu20a4b3f2017-04-27 02:08:52 -0700671 } else {
672 RTC_NOTREACHED();
673 }
674 });
675 } else {
676 CallEncoder(stream->channel_proxy_, [&](AudioEncoder* encoder) {
677 encoder->DisableAudioNetworkAdaptor();
678 });
Jonas Olsson24ea8222018-01-25 10:14:29 +0100679 RTC_DLOG(LS_INFO) << "Audio network adaptor disabled on SSRC "
680 << new_config.rtp.ssrc;
ossu20a4b3f2017-04-27 02:08:52 -0700681 }
682}
683
684void AudioSendStream::ReconfigureCNG(AudioSendStream* stream,
685 const Config& new_config) {
686 if (new_config.send_codec_spec->cng_payload_type ==
687 stream->config_.send_codec_spec->cng_payload_type) {
688 return;
689 }
690
ossu3b9ff382017-04-27 08:03:42 -0700691 // Register the CNG payload type if it's been added, don't do anything if CNG
692 // is removed. Payload types must not be redefined.
693 if (new_config.send_codec_spec->cng_payload_type) {
694 stream->RegisterCngPayloadType(
695 *new_config.send_codec_spec->cng_payload_type,
696 new_config.send_codec_spec->format.clockrate_hz);
697 }
698
ossu20a4b3f2017-04-27 02:08:52 -0700699 // Wrap or unwrap the encoder in an AudioEncoderCNG.
700 stream->channel_proxy_->ModifyEncoder(
701 [&](std::unique_ptr<AudioEncoder>* encoder_ptr) {
702 std::unique_ptr<AudioEncoder> old_encoder(std::move(*encoder_ptr));
703 auto sub_encoders = old_encoder->ReclaimContainedEncoders();
704 if (!sub_encoders.empty()) {
705 // Replace enc with its sub encoder. We need to put the sub
706 // encoder in a temporary first, since otherwise the old value
707 // of enc would be destroyed before the new value got assigned,
708 // which would be bad since the new value is a part of the old
709 // value.
710 auto tmp = std::move(sub_encoders[0]);
711 old_encoder = std::move(tmp);
712 }
713 if (new_config.send_codec_spec->cng_payload_type) {
714 AudioEncoderCng::Config config;
715 config.speech_encoder = std::move(old_encoder);
716 config.num_channels = config.speech_encoder->NumChannels();
717 config.payload_type = *new_config.send_codec_spec->cng_payload_type;
718 config.vad_mode = Vad::kVadNormal;
719 encoder_ptr->reset(new AudioEncoderCng(std::move(config)));
720 } else {
721 *encoder_ptr = std::move(old_encoder);
722 }
723 });
724}
725
726void AudioSendStream::ReconfigureBitrateObserver(
727 AudioSendStream* stream,
728 const webrtc::AudioSendStream::Config& new_config) {
729 // Since the Config's default is for both of these to be -1, this test will
730 // allow us to configure the bitrate observer if the new config has bitrate
731 // limits set, but would only have us call RemoveBitrateObserver if we were
732 // previously configured with bitrate limits.
Alex Narestcedd3512017-12-07 20:54:55 +0100733 int new_transport_seq_num_id =
734 FindExtensionIds(new_config.rtp.extensions).transport_sequence_number;
ossu20a4b3f2017-04-27 02:08:52 -0700735 if (stream->config_.min_bitrate_bps == new_config.min_bitrate_bps &&
Alex Narestcedd3512017-12-07 20:54:55 +0100736 stream->config_.max_bitrate_bps == new_config.max_bitrate_bps &&
Seth Hampson24722b32017-12-22 09:36:42 -0800737 stream->config_.bitrate_priority == new_config.bitrate_priority &&
Alex Narestcedd3512017-12-07 20:54:55 +0100738 (FindExtensionIds(stream->config_.rtp.extensions)
739 .transport_sequence_number == new_transport_seq_num_id ||
740 !webrtc::field_trial::IsEnabled("WebRTC-Audio-SendSideBwe"))) {
ossu20a4b3f2017-04-27 02:08:52 -0700741 return;
742 }
743
Sebastian Jansson763e9472018-03-21 12:46:56 +0100744 bool has_transport_sequence_number = new_transport_seq_num_id != 0;
Alex Narestcedd3512017-12-07 20:54:55 +0100745 if (new_config.min_bitrate_bps != -1 && new_config.max_bitrate_bps != -1 &&
Tim Haloun648d28a2018-10-18 16:52:22 -0700746 !new_config.has_dscp &&
Sebastian Jansson763e9472018-03-21 12:46:56 +0100747 (has_transport_sequence_number ||
Alex Narestcedd3512017-12-07 20:54:55 +0100748 !webrtc::field_trial::IsEnabled("WebRTC-Audio-SendSideBwe"))) {
Niels Möller7d76a312018-10-26 12:57:07 +0200749 stream->rtp_transport_->packet_sender()->SetAccountForAudioPackets(true);
Sebastian Jansson763e9472018-03-21 12:46:56 +0100750 stream->ConfigureBitrateObserver(
751 new_config.min_bitrate_bps, new_config.max_bitrate_bps,
752 new_config.bitrate_priority, has_transport_sequence_number);
Sebastian Janssonb6863962018-10-10 10:23:13 +0200753 stream->rtp_rtcp_module_->SetAsPartOfAllocation(true);
ossu20a4b3f2017-04-27 02:08:52 -0700754 } else {
Niels Möller7d76a312018-10-26 12:57:07 +0200755 stream->rtp_transport_->packet_sender()->SetAccountForAudioPackets(false);
ossu20a4b3f2017-04-27 02:08:52 -0700756 stream->RemoveBitrateObserver();
Sebastian Janssonb6863962018-10-10 10:23:13 +0200757 stream->rtp_rtcp_module_->SetAsPartOfAllocation(false);
ossu20a4b3f2017-04-27 02:08:52 -0700758 }
759}
760
761void AudioSendStream::ConfigureBitrateObserver(int min_bitrate_bps,
Seth Hampson24722b32017-12-22 09:36:42 -0800762 int max_bitrate_bps,
Sebastian Jansson763e9472018-03-21 12:46:56 +0100763 double bitrate_priority,
764 bool has_packet_feedback) {
ossu20a4b3f2017-04-27 02:08:52 -0700765 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
766 RTC_DCHECK_GE(max_bitrate_bps, min_bitrate_bps);
767 rtc::Event thread_sync_event(false /* manual_reset */, false);
768 worker_queue_->PostTask([&] {
769 // We may get a callback immediately as the observer is registered, so make
770 // sure the bitrate limits in config_ are up-to-date.
771 config_.min_bitrate_bps = min_bitrate_bps;
772 config_.max_bitrate_bps = max_bitrate_bps;
Seth Hampson24722b32017-12-22 09:36:42 -0800773 config_.bitrate_priority = bitrate_priority;
774 // This either updates the current observer or adds a new observer.
Sebastian Jansson24ad7202018-04-19 08:25:12 +0200775 bitrate_allocator_->AddObserver(
776 this, MediaStreamAllocationConfig{
777 static_cast<uint32_t>(min_bitrate_bps),
778 static_cast<uint32_t>(max_bitrate_bps), 0, true,
779 config_.track_id, bitrate_priority, has_packet_feedback});
ossu20a4b3f2017-04-27 02:08:52 -0700780 thread_sync_event.Set();
781 });
782 thread_sync_event.Wait(rtc::Event::kForever);
783}
784
785void AudioSendStream::RemoveBitrateObserver() {
786 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
787 rtc::Event thread_sync_event(false /* manual_reset */, false);
788 worker_queue_->PostTask([this, &thread_sync_event] {
789 bitrate_allocator_->RemoveObserver(this);
790 thread_sync_event.Set();
791 });
792 thread_sync_event.Wait(rtc::Event::kForever);
793}
794
ossu3b9ff382017-04-27 08:03:42 -0700795void AudioSendStream::RegisterCngPayloadType(int payload_type,
796 int clockrate_hz) {
ossu3b9ff382017-04-27 08:03:42 -0700797 const CodecInst codec = {payload_type, "CN", clockrate_hz, 0, 1, 0};
ossuc3d4b482017-05-23 06:07:11 -0700798 if (rtp_rtcp_module_->RegisterSendPayload(codec) != 0) {
799 rtp_rtcp_module_->DeRegisterSendPayload(codec.pltype);
800 if (rtp_rtcp_module_->RegisterSendPayload(codec) != 0) {
Jonas Olsson24ea8222018-01-25 10:14:29 +0100801 RTC_DLOG(LS_ERROR) << "RegisterCngPayloadType() failed to register CN to "
802 "RTP/RTCP module";
ossu3b9ff382017-04-27 08:03:42 -0700803 }
804 }
805}
solenbergc7a8b082015-10-16 14:35:07 -0700806} // namespace internal
807} // namespace webrtc