blob: e82264a83cb1cb285e83e83adc40955c605e6cb5 [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"
18
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020019#include "audio/audio_state.h"
Niels Möllerb222f492018-10-03 16:50:08 +020020#include "audio/channel_send_proxy.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020021#include "audio/conversion.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020022#include "call/rtp_transport_controller_send_interface.h"
23#include "modules/audio_coding/codecs/cng/audio_encoder_cng.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020024#include "rtc_base/checks.h"
25#include "rtc_base/event.h"
26#include "rtc_base/function_view.h"
27#include "rtc_base/logging.h"
Jonas Olssonabbe8412018-04-03 13:40:05 +020028#include "rtc_base/strings/audio_format_to_string.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020029#include "rtc_base/task_queue.h"
30#include "rtc_base/timeutils.h"
Alex Narestcedd3512017-12-07 20:54:55 +010031#include "system_wrappers/include/field_trial.h"
solenbergc7a8b082015-10-16 14:35:07 -070032
33namespace webrtc {
solenbergc7a8b082015-10-16 14:35:07 -070034namespace internal {
Fredrik Solenberg8f5787a2018-01-11 13:52:30 +010035namespace {
eladalonedd6eea2017-05-25 00:15:35 -070036// TODO(eladalon): Subsequent CL will make these values experiment-dependent.
elad.alond12a8e12017-03-23 11:04:48 -070037constexpr size_t kPacketLossTrackerMaxWindowSizeMs = 15000;
38constexpr size_t kPacketLossRateMinNumAckedPackets = 50;
39constexpr size_t kRecoverablePacketLossRateMinNumAckedPairs = 40;
40
Niels Möllerb222f492018-10-03 16:50:08 +020041void CallEncoder(const std::unique_ptr<voe::ChannelSendProxy>& channel_proxy,
ossu20a4b3f2017-04-27 02:08:52 -070042 rtc::FunctionView<void(AudioEncoder*)> lambda) {
43 channel_proxy->ModifyEncoder([&](std::unique_ptr<AudioEncoder>* encoder_ptr) {
44 RTC_DCHECK(encoder_ptr);
45 lambda(encoder_ptr->get());
46 });
47}
Fredrik Solenberg8f5787a2018-01-11 13:52:30 +010048
Niels Möllerb222f492018-10-03 16:50:08 +020049std::unique_ptr<voe::ChannelSendProxy> CreateChannelAndProxy(
Fredrik Solenberg8f5787a2018-01-11 13:52:30 +010050 rtc::TaskQueue* worker_queue,
Tommi5f223652018-03-26 13:28:26 +020051 ProcessThread* module_process_thread,
Niels Möllerfa4e1852018-08-14 09:43:34 +020052 RtcpRttStats* rtcp_rtt_stats,
Benjamin Wright84583f62018-10-04 14:22:34 -070053 RtcEventLog* event_log,
54 FrameEncryptorInterface* frame_encryptor) {
Niels Möllerb222f492018-10-03 16:50:08 +020055 return absl::make_unique<voe::ChannelSendProxy>(
Niels Möller530ead42018-10-04 14:28:39 +020056 absl::make_unique<voe::ChannelSend>(worker_queue, module_process_thread,
Benjamin Wright84583f62018-10-04 14:22:34 -070057 rtcp_rtt_stats, event_log,
58 frame_encryptor));
Fredrik Solenberg8f5787a2018-01-11 13:52:30 +010059}
ossu20a4b3f2017-04-27 02:08:52 -070060} // namespace
61
Sam Zackrisson06953ba2018-02-01 16:53:16 +010062// Helper class to track the actively sending lifetime of this stream.
sazac58f8c02017-07-19 00:39:19 -070063class AudioSendStream::TimedTransport : public Transport {
64 public:
65 TimedTransport(Transport* transport, TimeInterval* time_interval)
66 : transport_(transport), lifetime_(time_interval) {}
67 bool SendRtp(const uint8_t* packet,
68 size_t length,
69 const PacketOptions& options) {
70 if (lifetime_) {
71 lifetime_->Extend();
72 }
73 return transport_->SendRtp(packet, length, options);
74 }
75 bool SendRtcp(const uint8_t* packet, size_t length) {
76 return transport_->SendRtcp(packet, length);
77 }
78 ~TimedTransport() {}
79
80 private:
81 Transport* transport_;
82 TimeInterval* lifetime_;
83};
84
solenberg566ef242015-11-06 15:34:49 -080085AudioSendStream::AudioSendStream(
86 const webrtc::AudioSendStream::Config& config,
Stefan Holmerb86d4e42015-12-07 10:26:18 +010087 const rtc::scoped_refptr<webrtc::AudioState>& audio_state,
perkj26091b12016-09-01 01:17:40 -070088 rtc::TaskQueue* worker_queue,
Fredrik Solenberg8f5787a2018-01-11 13:52:30 +010089 ProcessThread* module_process_thread,
nisseb8f9a322017-03-27 05:36:15 -070090 RtpTransportControllerSendInterface* transport,
tereliuse035e2d2016-09-21 06:51:47 -070091 BitrateAllocator* bitrate_allocator,
michaelt9332b7d2016-11-30 07:51:13 -080092 RtcEventLog* event_log,
ossuc3d4b482017-05-23 06:07:11 -070093 RtcpRttStats* rtcp_rtt_stats,
Danil Chapovalovb9b146c2018-06-15 12:28:07 +020094 const absl::optional<RtpState>& suspended_rtp_state,
Sam Zackrisson06953ba2018-02-01 16:53:16 +010095 TimeInterval* overall_call_lifetime)
Fredrik Solenberg8f5787a2018-01-11 13:52:30 +010096 : AudioSendStream(config,
97 audio_state,
98 worker_queue,
99 transport,
100 bitrate_allocator,
101 event_log,
102 rtcp_rtt_stats,
103 suspended_rtp_state,
Sam Zackrisson06953ba2018-02-01 16:53:16 +0100104 overall_call_lifetime,
Niels Möller530ead42018-10-04 14:28:39 +0200105 CreateChannelAndProxy(worker_queue,
Tommi5f223652018-03-26 13:28:26 +0200106 module_process_thread,
Niels Möllerfa4e1852018-08-14 09:43:34 +0200107 rtcp_rtt_stats,
Benjamin Wright84583f62018-10-04 14:22:34 -0700108 event_log,
109 config.frame_encryptor)) {}
Fredrik Solenberg8f5787a2018-01-11 13:52:30 +0100110
111AudioSendStream::AudioSendStream(
112 const webrtc::AudioSendStream::Config& config,
113 const rtc::scoped_refptr<webrtc::AudioState>& audio_state,
114 rtc::TaskQueue* worker_queue,
115 RtpTransportControllerSendInterface* transport,
116 BitrateAllocator* bitrate_allocator,
117 RtcEventLog* event_log,
118 RtcpRttStats* rtcp_rtt_stats,
Danil Chapovalovb9b146c2018-06-15 12:28:07 +0200119 const absl::optional<RtpState>& suspended_rtp_state,
Sam Zackrisson06953ba2018-02-01 16:53:16 +0100120 TimeInterval* overall_call_lifetime,
Niels Möllerb222f492018-10-03 16:50:08 +0200121 std::unique_ptr<voe::ChannelSendProxy> channel_proxy)
perkj26091b12016-09-01 01:17:40 -0700122 : worker_queue_(worker_queue),
ossu20a4b3f2017-04-27 02:08:52 -0700123 config_(Config(nullptr)),
mflodman86cc6ff2016-07-26 04:44:06 -0700124 audio_state_(audio_state),
Fredrik Solenberg8f5787a2018-01-11 13:52:30 +0100125 channel_proxy_(std::move(channel_proxy)),
ossu20a4b3f2017-04-27 02:08:52 -0700126 event_log_(event_log),
michaeltf4caaab2017-01-16 23:55:07 -0800127 bitrate_allocator_(bitrate_allocator),
nisseb8f9a322017-03-27 05:36:15 -0700128 transport_(transport),
elad.alond12a8e12017-03-23 11:04:48 -0700129 packet_loss_tracker_(kPacketLossTrackerMaxWindowSizeMs,
130 kPacketLossRateMinNumAckedPackets,
ossuc3d4b482017-05-23 06:07:11 -0700131 kRecoverablePacketLossRateMinNumAckedPairs),
132 rtp_rtcp_module_(nullptr),
Sam Zackrisson06953ba2018-02-01 16:53:16 +0100133 suspended_rtp_state_(suspended_rtp_state),
134 overall_call_lifetime_(overall_call_lifetime) {
Jonas Olsson24ea8222018-01-25 10:14:29 +0100135 RTC_LOG(LS_INFO) << "AudioSendStream: " << config.rtp.ssrc;
Fredrik Solenberg8f5787a2018-01-11 13:52:30 +0100136 RTC_DCHECK(worker_queue_);
137 RTC_DCHECK(audio_state_);
138 RTC_DCHECK(channel_proxy_);
139 RTC_DCHECK(bitrate_allocator_);
nisseb8f9a322017-03-27 05:36:15 -0700140 RTC_DCHECK(transport);
Sam Zackrisson06953ba2018-02-01 16:53:16 +0100141 RTC_DCHECK(overall_call_lifetime_);
solenberg3a941542015-11-16 07:34:50 -0800142
solenberg13725082015-11-25 08:16:52 -0800143 channel_proxy_->SetRTCPStatus(true);
Niels Möller848d6d32018-08-08 10:49:16 +0200144 rtp_rtcp_module_ = channel_proxy_->GetRtpRtcp();
ossuc3d4b482017-05-23 06:07:11 -0700145 RTC_DCHECK(rtp_rtcp_module_);
mflodman3d7db262016-04-29 00:57:13 -0700146
ossu20a4b3f2017-04-27 02:08:52 -0700147 ConfigureStream(this, config, true);
elad.alond12a8e12017-03-23 11:04:48 -0700148
149 pacer_thread_checker_.DetachFromThread();
Danil Chapovalov90e1f532017-10-03 14:59:27 +0200150 // Signal congestion controller this object is ready for OnPacket* callbacks.
Sebastian Janssone4be6da2018-02-15 16:51:41 +0100151 transport_->RegisterPacketFeedbackObserver(this);
solenbergc7a8b082015-10-16 14:35:07 -0700152}
153
154AudioSendStream::~AudioSendStream() {
elad.alond12a8e12017-03-23 11:04:48 -0700155 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
Jonas Olsson24ea8222018-01-25 10:14:29 +0100156 RTC_LOG(LS_INFO) << "~AudioSendStream: " << config_.rtp.ssrc;
Fredrik Solenberg2a877972017-12-15 16:42:15 +0100157 RTC_DCHECK(!sending_);
Sebastian Janssone4be6da2018-02-15 16:51:41 +0100158 transport_->DeRegisterPacketFeedbackObserver(this);
solenberg1c239d42017-09-29 06:00:28 -0700159 channel_proxy_->RegisterTransport(nullptr);
nissefdbfdc92017-03-31 05:44:52 -0700160 channel_proxy_->ResetSenderCongestionControlObjects();
Sam Zackrisson06953ba2018-02-01 16:53:16 +0100161 // Lifetime can only be updated after deregistering
162 // |timed_send_transport_adapter_| in the underlying channel object to avoid
163 // data races in |active_lifetime_|.
164 overall_call_lifetime_->Extend(active_lifetime_);
solenbergc7a8b082015-10-16 14:35:07 -0700165}
166
eladalonabbc4302017-07-26 02:09:44 -0700167const webrtc::AudioSendStream::Config& AudioSendStream::GetConfig() const {
168 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
169 return config_;
170}
171
ossu20a4b3f2017-04-27 02:08:52 -0700172void AudioSendStream::Reconfigure(
173 const webrtc::AudioSendStream::Config& new_config) {
Fredrik Solenberg2a877972017-12-15 16:42:15 +0100174 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
ossu20a4b3f2017-04-27 02:08:52 -0700175 ConfigureStream(this, new_config, false);
176}
177
Alex Narestcedd3512017-12-07 20:54:55 +0100178AudioSendStream::ExtensionIds AudioSendStream::FindExtensionIds(
179 const std::vector<RtpExtension>& extensions) {
180 ExtensionIds ids;
181 for (const auto& extension : extensions) {
182 if (extension.uri == RtpExtension::kAudioLevelUri) {
183 ids.audio_level = extension.id;
184 } else if (extension.uri == RtpExtension::kTransportSequenceNumberUri) {
185 ids.transport_sequence_number = extension.id;
Steve Antonbb50ce52018-03-26 10:24:32 -0700186 } else if (extension.uri == RtpExtension::kMidUri) {
187 ids.mid = extension.id;
Alex Narestcedd3512017-12-07 20:54:55 +0100188 }
189 }
190 return ids;
191}
192
ossu20a4b3f2017-04-27 02:08:52 -0700193void AudioSendStream::ConfigureStream(
194 webrtc::internal::AudioSendStream* stream,
195 const webrtc::AudioSendStream::Config& new_config,
196 bool first_time) {
Jonas Olsson24ea8222018-01-25 10:14:29 +0100197 RTC_LOG(LS_INFO) << "AudioSendStream::ConfigureStream: "
198 << new_config.ToString();
ossu20a4b3f2017-04-27 02:08:52 -0700199 const auto& channel_proxy = stream->channel_proxy_;
200 const auto& old_config = stream->config_;
201
202 if (first_time || old_config.rtp.ssrc != new_config.rtp.ssrc) {
203 channel_proxy->SetLocalSSRC(new_config.rtp.ssrc);
ossuc3d4b482017-05-23 06:07:11 -0700204 if (stream->suspended_rtp_state_) {
205 stream->rtp_rtcp_module_->SetRtpState(*stream->suspended_rtp_state_);
206 }
ossu20a4b3f2017-04-27 02:08:52 -0700207 }
208 if (first_time || old_config.rtp.c_name != new_config.rtp.c_name) {
209 channel_proxy->SetRTCP_CNAME(new_config.rtp.c_name);
210 }
211 // TODO(solenberg): Config NACK history window (which is a packet count),
212 // using the actual packet size for the configured codec.
213 if (first_time || old_config.rtp.nack.rtp_history_ms !=
214 new_config.rtp.nack.rtp_history_ms) {
215 channel_proxy->SetNACKStatus(new_config.rtp.nack.rtp_history_ms != 0,
216 new_config.rtp.nack.rtp_history_ms / 20);
217 }
218
Yves Gerey665174f2018-06-19 15:03:05 +0200219 if (first_time || new_config.send_transport != old_config.send_transport) {
ossu20a4b3f2017-04-27 02:08:52 -0700220 if (old_config.send_transport) {
solenberg1c239d42017-09-29 06:00:28 -0700221 channel_proxy->RegisterTransport(nullptr);
ossu20a4b3f2017-04-27 02:08:52 -0700222 }
sazac58f8c02017-07-19 00:39:19 -0700223 if (new_config.send_transport) {
224 stream->timed_send_transport_adapter_.reset(new TimedTransport(
225 new_config.send_transport, &stream->active_lifetime_));
226 } else {
227 stream->timed_send_transport_adapter_.reset(nullptr);
228 }
solenberg1c239d42017-09-29 06:00:28 -0700229 channel_proxy->RegisterTransport(
sazac58f8c02017-07-19 00:39:19 -0700230 stream->timed_send_transport_adapter_.get());
ossu20a4b3f2017-04-27 02:08:52 -0700231 }
232
Benjamin Wright84583f62018-10-04 14:22:34 -0700233 // Enable the frame encryptor if a new frame encryptor has been provided.
234 if (first_time || new_config.frame_encryptor != old_config.frame_encryptor) {
235 channel_proxy->SetFrameEncryptor(new_config.frame_encryptor);
236 }
237
Alex Narestcedd3512017-12-07 20:54:55 +0100238 const ExtensionIds old_ids = FindExtensionIds(old_config.rtp.extensions);
239 const ExtensionIds new_ids = FindExtensionIds(new_config.rtp.extensions);
ossu20a4b3f2017-04-27 02:08:52 -0700240 // Audio level indication
241 if (first_time || new_ids.audio_level != old_ids.audio_level) {
242 channel_proxy->SetSendAudioLevelIndicationStatus(new_ids.audio_level != 0,
243 new_ids.audio_level);
244 }
Sebastian Jansson8d9c5402017-11-15 17:22:16 +0100245 bool transport_seq_num_id_changed =
246 new_ids.transport_sequence_number != old_ids.transport_sequence_number;
Alex Narest867e5102018-06-12 13:40:18 +0200247 if (first_time ||
248 (transport_seq_num_id_changed &&
249 !webrtc::field_trial::IsEnabled("WebRTC-Audio-ForceNoTWCC"))) {
ossu1129df22017-06-30 01:38:56 -0700250 if (!first_time) {
ossu20a4b3f2017-04-27 02:08:52 -0700251 channel_proxy->ResetSenderCongestionControlObjects();
ossu20a4b3f2017-04-27 02:08:52 -0700252 }
253
Sebastian Jansson8d9c5402017-11-15 17:22:16 +0100254 RtcpBandwidthObserver* bandwidth_observer = nullptr;
Alex Narest867e5102018-06-12 13:40:18 +0200255 bool has_transport_sequence_number =
256 new_ids.transport_sequence_number != 0 &&
257 !webrtc::field_trial::IsEnabled("WebRTC-Audio-ForceNoTWCC");
Sebastian Jansson8d9c5402017-11-15 17:22:16 +0100258 if (has_transport_sequence_number) {
ossu20a4b3f2017-04-27 02:08:52 -0700259 channel_proxy->EnableSendTransportSequenceNumber(
260 new_ids.transport_sequence_number);
Sebastian Jansson8d9c5402017-11-15 17:22:16 +0100261 // Probing in application limited region is only used in combination with
262 // send side congestion control, wich depends on feedback packets which
263 // requires transport sequence numbers to be enabled.
Sebastian Janssone4be6da2018-02-15 16:51:41 +0100264 stream->transport_->EnablePeriodicAlrProbing(true);
265 bandwidth_observer = stream->transport_->GetBandwidthObserver();
ossu20a4b3f2017-04-27 02:08:52 -0700266 }
267
Sebastian Jansson8d9c5402017-11-15 17:22:16 +0100268 channel_proxy->RegisterSenderCongestionControlObjects(stream->transport_,
269 bandwidth_observer);
ossu20a4b3f2017-04-27 02:08:52 -0700270 }
271
Steve Antonbb50ce52018-03-26 10:24:32 -0700272 // MID RTP header extension.
Steve Anton003930a2018-03-29 12:37:21 -0700273 if ((first_time || new_ids.mid != old_ids.mid ||
274 new_config.rtp.mid != old_config.rtp.mid) &&
275 new_ids.mid != 0 && !new_config.rtp.mid.empty()) {
Steve Antonbb50ce52018-03-26 10:24:32 -0700276 channel_proxy->SetMid(new_config.rtp.mid, new_ids.mid);
277 }
278
ossu20a4b3f2017-04-27 02:08:52 -0700279 if (!ReconfigureSendCodec(stream, new_config)) {
Mirko Bonadei675513b2017-11-09 11:09:25 +0100280 RTC_LOG(LS_ERROR) << "Failed to set up send codec state.";
ossu20a4b3f2017-04-27 02:08:52 -0700281 }
282
Oskar Sundbomf85e31b2017-12-20 16:38:09 +0100283 if (stream->sending_) {
284 ReconfigureBitrateObserver(stream, new_config);
285 }
ossu20a4b3f2017-04-27 02:08:52 -0700286 stream->config_ = new_config;
287}
288
solenberg3a941542015-11-16 07:34:50 -0800289void AudioSendStream::Start() {
elad.alond12a8e12017-03-23 11:04:48 -0700290 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
Fredrik Solenberg2a877972017-12-15 16:42:15 +0100291 if (sending_) {
292 return;
293 }
294
Sebastian Jansson763e9472018-03-21 12:46:56 +0100295 bool has_transport_sequence_number =
Alex Narest867e5102018-06-12 13:40:18 +0200296 FindExtensionIds(config_.rtp.extensions).transport_sequence_number != 0 &&
297 !webrtc::field_trial::IsEnabled("WebRTC-Audio-ForceNoTWCC");
Alex Narestcedd3512017-12-07 20:54:55 +0100298 if (config_.min_bitrate_bps != -1 && config_.max_bitrate_bps != -1 &&
Sebastian Jansson763e9472018-03-21 12:46:56 +0100299 (has_transport_sequence_number ||
Alex Narestbcf91802018-06-25 16:08:36 +0200300 !webrtc::field_trial::IsEnabled("WebRTC-Audio-SendSideBwe") ||
301 webrtc::field_trial::IsEnabled("WebRTC-Audio-ABWENoTWCC"))) {
Alex Narest78609d52017-10-20 10:37:47 +0200302 // Audio BWE is enabled.
303 transport_->packet_sender()->SetAccountForAudioPackets(true);
Seth Hampson24722b32017-12-22 09:36:42 -0800304 ConfigureBitrateObserver(config_.min_bitrate_bps, config_.max_bitrate_bps,
Sebastian Jansson763e9472018-03-21 12:46:56 +0100305 config_.bitrate_priority,
306 has_transport_sequence_number);
mflodman86cc6ff2016-07-26 04:44:06 -0700307 }
Fredrik Solenbergaaedf752017-12-18 13:09:12 +0100308 channel_proxy_->StartSend();
Fredrik Solenberg2a877972017-12-15 16:42:15 +0100309 sending_ = true;
310 audio_state()->AddSendingStream(this, encoder_sample_rate_hz_,
311 encoder_num_channels_);
solenberg3a941542015-11-16 07:34:50 -0800312}
313
314void AudioSendStream::Stop() {
elad.alond12a8e12017-03-23 11:04:48 -0700315 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
Fredrik Solenberg2a877972017-12-15 16:42:15 +0100316 if (!sending_) {
317 return;
318 }
319
ossu20a4b3f2017-04-27 02:08:52 -0700320 RemoveBitrateObserver();
Fredrik Solenbergaaedf752017-12-18 13:09:12 +0100321 channel_proxy_->StopSend();
Fredrik Solenberg2a877972017-12-15 16:42:15 +0100322 sending_ = false;
323 audio_state()->RemoveSendingStream(this);
324}
325
326void AudioSendStream::SendAudioData(std::unique_ptr<AudioFrame> audio_frame) {
327 RTC_CHECK_RUNS_SERIALIZED(&audio_capture_race_checker_);
328 channel_proxy_->ProcessAndEncodeAudio(std::move(audio_frame));
solenberg3a941542015-11-16 07:34:50 -0800329}
330
solenbergffbbcac2016-11-17 05:25:37 -0800331bool AudioSendStream::SendTelephoneEvent(int payload_type,
Yves Gerey665174f2018-06-19 15:03:05 +0200332 int payload_frequency,
333 int event,
solenberg8842c3e2016-03-11 03:06:41 -0800334 int duration_ms) {
elad.alond12a8e12017-03-23 11:04:48 -0700335 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
solenbergffbbcac2016-11-17 05:25:37 -0800336 return channel_proxy_->SetSendTelephoneEventPayloadType(payload_type,
337 payload_frequency) &&
Fredrik Solenbergb5727682015-12-04 15:22:19 +0100338 channel_proxy_->SendTelephoneEventOutband(event, duration_ms);
339}
340
solenberg94218532016-06-16 10:53:22 -0700341void AudioSendStream::SetMuted(bool muted) {
elad.alond12a8e12017-03-23 11:04:48 -0700342 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
solenberg94218532016-06-16 10:53:22 -0700343 channel_proxy_->SetInputMute(muted);
344}
345
solenbergc7a8b082015-10-16 14:35:07 -0700346webrtc::AudioSendStream::Stats AudioSendStream::GetStats() const {
Ivo Creusen56d46092017-11-24 17:29:59 +0100347 return GetStats(true);
348}
349
350webrtc::AudioSendStream::Stats AudioSendStream::GetStats(
351 bool has_remote_tracks) const {
elad.alond12a8e12017-03-23 11:04:48 -0700352 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
solenberg85a04962015-10-27 03:35:21 -0700353 webrtc::AudioSendStream::Stats stats;
354 stats.local_ssrc = config_.rtp.ssrc;
solenberg85a04962015-10-27 03:35:21 -0700355
Niels Möller530ead42018-10-04 14:28:39 +0200356 webrtc::CallSendStatistics call_stats = channel_proxy_->GetRTCPStatistics();
solenberg85a04962015-10-27 03:35:21 -0700357 stats.bytes_sent = call_stats.bytesSent;
358 stats.packets_sent = call_stats.packetsSent;
solenberg8b85de22015-11-16 09:48:04 -0800359 // RTT isn't known until a RTCP report is received. Until then, VoiceEngine
360 // returns 0 to indicate an error value.
361 if (call_stats.rttMs > 0) {
362 stats.rtt_ms = call_stats.rttMs;
363 }
ossu20a4b3f2017-04-27 02:08:52 -0700364 if (config_.send_codec_spec) {
365 const auto& spec = *config_.send_codec_spec;
366 stats.codec_name = spec.format.name;
Oskar Sundbom2707fb22017-11-16 10:57:35 +0100367 stats.codec_payload_type = spec.payload_type;
solenberg85a04962015-10-27 03:35:21 -0700368
369 // Get data from the last remote RTCP report.
solenberg358057b2015-11-27 10:46:42 -0800370 for (const auto& block : channel_proxy_->GetRemoteRTCPReportBlocks()) {
solenberg8b85de22015-11-16 09:48:04 -0800371 // Lookup report for send ssrc only.
372 if (block.source_SSRC == stats.local_ssrc) {
373 stats.packets_lost = block.cumulative_num_packets_lost;
374 stats.fraction_lost = Q8ToFloat(block.fraction_lost);
375 stats.ext_seqnum = block.extended_highest_sequence_number;
ossu20a4b3f2017-04-27 02:08:52 -0700376 // Convert timestamps to milliseconds.
377 if (spec.format.clockrate_hz / 1000 > 0) {
solenberg8b85de22015-11-16 09:48:04 -0800378 stats.jitter_ms =
ossu20a4b3f2017-04-27 02:08:52 -0700379 block.interarrival_jitter / (spec.format.clockrate_hz / 1000);
solenberg85a04962015-10-27 03:35:21 -0700380 }
solenberg8b85de22015-11-16 09:48:04 -0800381 break;
solenberg85a04962015-10-27 03:35:21 -0700382 }
383 }
384 }
385
Fredrik Solenberg2a877972017-12-15 16:42:15 +0100386 AudioState::Stats input_stats = audio_state()->GetAudioInputStats();
387 stats.audio_level = input_stats.audio_level;
388 stats.total_input_energy = input_stats.total_energy;
389 stats.total_input_duration = input_stats.total_duration;
solenberg796b8f92017-03-01 17:02:23 -0800390
Fredrik Solenberg2a877972017-12-15 16:42:15 +0100391 stats.typing_noise_detected = audio_state()->typing_noise_detected();
ivoce1198e02017-09-08 08:13:19 -0700392 stats.ana_statistics = channel_proxy_->GetANAStatistics();
Ivo Creusen56d46092017-11-24 17:29:59 +0100393 RTC_DCHECK(audio_state_->audio_processing());
394 stats.apm_statistics =
395 audio_state_->audio_processing()->GetStatistics(has_remote_tracks);
solenberg85a04962015-10-27 03:35:21 -0700396
397 return stats;
398}
399
pbos1ba8d392016-05-01 20:18:34 -0700400void AudioSendStream::SignalNetworkState(NetworkState state) {
elad.alond12a8e12017-03-23 11:04:48 -0700401 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
pbos1ba8d392016-05-01 20:18:34 -0700402}
403
404bool AudioSendStream::DeliverRtcp(const uint8_t* packet, size_t length) {
405 // TODO(solenberg): Tests call this function on a network thread, libjingle
406 // calls on the worker thread. We should move towards always using a network
407 // thread. Then this check can be enabled.
elad.alond12a8e12017-03-23 11:04:48 -0700408 // RTC_DCHECK(!worker_thread_checker_.CalledOnValidThread());
pbos1ba8d392016-05-01 20:18:34 -0700409 return channel_proxy_->ReceivedRTCPPacket(packet, length);
410}
411
mflodman86cc6ff2016-07-26 04:44:06 -0700412uint32_t AudioSendStream::OnBitrateUpdated(uint32_t bitrate_bps,
413 uint8_t fraction_loss,
minyue78b4d562016-11-30 04:47:39 -0800414 int64_t rtt,
minyue93e45222017-05-18 14:32:41 -0700415 int64_t bwe_period_ms) {
stefanfca900a2017-04-10 03:53:00 -0700416 // A send stream may be allocated a bitrate of zero if the allocator decides
417 // to disable it. For now we ignore this decision and keep sending on min
418 // bitrate.
419 if (bitrate_bps == 0) {
420 bitrate_bps = config_.min_bitrate_bps;
421 }
Yves Gerey665174f2018-06-19 15:03:05 +0200422 RTC_DCHECK_GE(bitrate_bps, static_cast<uint32_t>(config_.min_bitrate_bps));
mflodman86cc6ff2016-07-26 04:44:06 -0700423 // The bitrate allocator might allocate an higher than max configured bitrate
424 // if there is room, to allow for, as example, extra FEC. Ignore that for now.
minyue10cbb462016-11-07 09:29:22 -0800425 const uint32_t max_bitrate_bps = config_.max_bitrate_bps;
mflodman86cc6ff2016-07-26 04:44:06 -0700426 if (bitrate_bps > max_bitrate_bps)
427 bitrate_bps = max_bitrate_bps;
428
minyue93e45222017-05-18 14:32:41 -0700429 channel_proxy_->SetBitrate(bitrate_bps, bwe_period_ms);
mflodman86cc6ff2016-07-26 04:44:06 -0700430
431 // The amount of audio protection is not exposed by the encoder, hence
432 // always returning 0.
433 return 0;
434}
435
elad.alond12a8e12017-03-23 11:04:48 -0700436void AudioSendStream::OnPacketAdded(uint32_t ssrc, uint16_t seq_num) {
437 RTC_DCHECK(pacer_thread_checker_.CalledOnValidThread());
438 // Only packets that belong to this stream are of interest.
439 if (ssrc == config_.rtp.ssrc) {
440 rtc::CritScope lock(&packet_loss_tracker_cs_);
eladalonedd6eea2017-05-25 00:15:35 -0700441 // TODO(eladalon): This function call could potentially reset the window,
elad.alond12a8e12017-03-23 11:04:48 -0700442 // setting both PLR and RPLR to unknown. Consider (during upcoming
443 // refactoring) passing an indication of such an event.
444 packet_loss_tracker_.OnPacketAdded(seq_num, rtc::TimeMillis());
445 }
446}
447
448void AudioSendStream::OnPacketFeedbackVector(
449 const std::vector<PacketFeedback>& packet_feedback_vector) {
eladalon3651fdd2017-08-24 07:26:25 -0700450 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
Danil Chapovalovb9b146c2018-06-15 12:28:07 +0200451 absl::optional<float> plr;
452 absl::optional<float> rplr;
elad.alond12a8e12017-03-23 11:04:48 -0700453 {
454 rtc::CritScope lock(&packet_loss_tracker_cs_);
455 packet_loss_tracker_.OnPacketFeedbackVector(packet_feedback_vector);
456 plr = packet_loss_tracker_.GetPacketLossRate();
elad.alondadb4dc2017-03-23 15:29:50 -0700457 rplr = packet_loss_tracker_.GetRecoverablePacketLossRate();
elad.alond12a8e12017-03-23 11:04:48 -0700458 }
eladalonedd6eea2017-05-25 00:15:35 -0700459 // TODO(eladalon): If R/PLR go back to unknown, no indication is given that
elad.alond12a8e12017-03-23 11:04:48 -0700460 // the previously sent value is no longer relevant. This will be taken care
461 // of with some refactoring which is now being done.
462 if (plr) {
463 channel_proxy_->OnTwccBasedUplinkPacketLossRate(*plr);
464 }
elad.alondadb4dc2017-03-23 15:29:50 -0700465 if (rplr) {
466 channel_proxy_->OnRecoverableUplinkPacketLossRate(*rplr);
467 }
elad.alond12a8e12017-03-23 11:04:48 -0700468}
469
michaelt79e05882016-11-08 02:50:09 -0800470void AudioSendStream::SetTransportOverhead(int transport_overhead_per_packet) {
elad.alond12a8e12017-03-23 11:04:48 -0700471 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
michaelt79e05882016-11-08 02:50:09 -0800472 channel_proxy_->SetTransportOverhead(transport_overhead_per_packet);
473}
474
ossuc3d4b482017-05-23 06:07:11 -0700475RtpState AudioSendStream::GetRtpState() const {
476 return rtp_rtcp_module_->GetRtpState();
477}
478
Niels Möllerb222f492018-10-03 16:50:08 +0200479const voe::ChannelSendProxy& AudioSendStream::GetChannelProxy() const {
Fredrik Solenberg8f5787a2018-01-11 13:52:30 +0100480 RTC_DCHECK(channel_proxy_.get());
481 return *channel_proxy_.get();
482}
483
Fredrik Solenberg2a877972017-12-15 16:42:15 +0100484internal::AudioState* AudioSendStream::audio_state() {
485 internal::AudioState* audio_state =
486 static_cast<internal::AudioState*>(audio_state_.get());
487 RTC_DCHECK(audio_state);
488 return audio_state;
489}
490
491const internal::AudioState* AudioSendStream::audio_state() const {
492 internal::AudioState* audio_state =
493 static_cast<internal::AudioState*>(audio_state_.get());
494 RTC_DCHECK(audio_state);
495 return audio_state;
496}
497
Fredrik Solenberg2a877972017-12-15 16:42:15 +0100498void AudioSendStream::StoreEncoderProperties(int sample_rate_hz,
499 size_t num_channels) {
500 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
501 encoder_sample_rate_hz_ = sample_rate_hz;
502 encoder_num_channels_ = num_channels;
503 if (sending_) {
504 // Update AudioState's information about the stream.
505 audio_state()->AddSendingStream(this, sample_rate_hz, num_channels);
506 }
507}
508
minyue7a973442016-10-20 03:27:12 -0700509// Apply current codec settings to a single voe::Channel used for sending.
ossu20a4b3f2017-04-27 02:08:52 -0700510bool AudioSendStream::SetupSendCodec(AudioSendStream* stream,
511 const Config& new_config) {
512 RTC_DCHECK(new_config.send_codec_spec);
513 const auto& spec = *new_config.send_codec_spec;
minyue48368ad2017-05-10 04:06:11 -0700514
515 RTC_DCHECK(new_config.encoder_factory);
ossu20a4b3f2017-04-27 02:08:52 -0700516 std::unique_ptr<AudioEncoder> encoder =
Karl Wiberg77490b92018-03-21 15:18:42 +0100517 new_config.encoder_factory->MakeAudioEncoder(
518 spec.payload_type, spec.format, new_config.codec_pair_id);
minyue7a973442016-10-20 03:27:12 -0700519
ossu20a4b3f2017-04-27 02:08:52 -0700520 if (!encoder) {
Jonas Olssonabbe8412018-04-03 13:40:05 +0200521 RTC_DLOG(LS_ERROR) << "Unable to create encoder for "
522 << rtc::ToString(spec.format);
ossu20a4b3f2017-04-27 02:08:52 -0700523 return false;
524 }
Alex Narestbbbe4e12018-07-13 10:32:58 +0200525
526 // If other side does not support audio TWCC and WebRTC-Audio-ABWENoTWCC is
527 // not enabled, do not update target audio bitrate if we are in
528 // WebRTC-Audio-SendSideBwe-For-Video experiment
529 const bool do_not_update_target_bitrate =
530 !webrtc::field_trial::IsEnabled("WebRTC-Audio-ABWENoTWCC") &&
531 webrtc::field_trial::IsEnabled("WebRTC-Audio-SendSideBwe-For-Video") &&
532 !FindExtensionIds(new_config.rtp.extensions).transport_sequence_number;
ossu20a4b3f2017-04-27 02:08:52 -0700533 // If a bitrate has been specified for the codec, use it over the
534 // codec's default.
Alex Narestbbbe4e12018-07-13 10:32:58 +0200535 if (!do_not_update_target_bitrate && spec.target_bitrate_bps) {
ossu20a4b3f2017-04-27 02:08:52 -0700536 encoder->OnReceivedTargetAudioBitrate(*spec.target_bitrate_bps);
minyue7a973442016-10-20 03:27:12 -0700537 }
538
ossu20a4b3f2017-04-27 02:08:52 -0700539 // Enable ANA if configured (currently only used by Opus).
540 if (new_config.audio_network_adaptor_config) {
541 if (encoder->EnableAudioNetworkAdaptor(
542 *new_config.audio_network_adaptor_config, stream->event_log_)) {
Jonas Olsson24ea8222018-01-25 10:14:29 +0100543 RTC_DLOG(LS_INFO) << "Audio network adaptor enabled on SSRC "
544 << new_config.rtp.ssrc;
ossu20a4b3f2017-04-27 02:08:52 -0700545 } else {
546 RTC_NOTREACHED();
minyue6b825df2016-10-31 04:08:32 -0700547 }
minyue7a973442016-10-20 03:27:12 -0700548 }
549
ossu20a4b3f2017-04-27 02:08:52 -0700550 // Wrap the encoder in a an AudioEncoderCNG, if VAD is enabled.
551 if (spec.cng_payload_type) {
552 AudioEncoderCng::Config cng_config;
553 cng_config.num_channels = encoder->NumChannels();
554 cng_config.payload_type = *spec.cng_payload_type;
555 cng_config.speech_encoder = std::move(encoder);
556 cng_config.vad_mode = Vad::kVadNormal;
557 encoder.reset(new AudioEncoderCng(std::move(cng_config)));
ossu3b9ff382017-04-27 08:03:42 -0700558
559 stream->RegisterCngPayloadType(
560 *spec.cng_payload_type,
561 new_config.send_codec_spec->format.clockrate_hz);
minyue7a973442016-10-20 03:27:12 -0700562 }
ossu20a4b3f2017-04-27 02:08:52 -0700563
Fredrik Solenberg2a877972017-12-15 16:42:15 +0100564 stream->StoreEncoderProperties(encoder->SampleRateHz(),
565 encoder->NumChannels());
ossu20a4b3f2017-04-27 02:08:52 -0700566 stream->channel_proxy_->SetEncoder(new_config.send_codec_spec->payload_type,
567 std::move(encoder));
minyue7a973442016-10-20 03:27:12 -0700568 return true;
569}
570
ossu20a4b3f2017-04-27 02:08:52 -0700571bool AudioSendStream::ReconfigureSendCodec(AudioSendStream* stream,
572 const Config& new_config) {
573 const auto& old_config = stream->config_;
minyue-webrtc8de18262017-07-26 14:18:40 +0200574
575 if (!new_config.send_codec_spec) {
576 // We cannot de-configure a send codec. So we will do nothing.
577 // By design, the send codec should have not been configured.
578 RTC_DCHECK(!old_config.send_codec_spec);
579 return true;
580 }
581
582 if (new_config.send_codec_spec == old_config.send_codec_spec &&
583 new_config.audio_network_adaptor_config ==
584 old_config.audio_network_adaptor_config) {
ossu20a4b3f2017-04-27 02:08:52 -0700585 return true;
586 }
587
588 // If we have no encoder, or the format or payload type's changed, create a
589 // new encoder.
590 if (!old_config.send_codec_spec ||
591 new_config.send_codec_spec->format !=
592 old_config.send_codec_spec->format ||
593 new_config.send_codec_spec->payload_type !=
594 old_config.send_codec_spec->payload_type) {
595 return SetupSendCodec(stream, new_config);
596 }
597
Alex Narestbbbe4e12018-07-13 10:32:58 +0200598 // If other side does not support audio TWCC and WebRTC-Audio-ABWENoTWCC is
599 // not enabled, do not update target audio bitrate if we are in
600 // WebRTC-Audio-SendSideBwe-For-Video experiment
601 const bool do_not_update_target_bitrate =
602 !webrtc::field_trial::IsEnabled("WebRTC-Audio-ABWENoTWCC") &&
603 webrtc::field_trial::IsEnabled("WebRTC-Audio-SendSideBwe-For-Video") &&
604 !FindExtensionIds(new_config.rtp.extensions).transport_sequence_number;
605
Danil Chapovalovb9b146c2018-06-15 12:28:07 +0200606 const absl::optional<int>& new_target_bitrate_bps =
ossu20a4b3f2017-04-27 02:08:52 -0700607 new_config.send_codec_spec->target_bitrate_bps;
608 // If a bitrate has been specified for the codec, use it over the
609 // codec's default.
Alex Narestbbbe4e12018-07-13 10:32:58 +0200610 if (!do_not_update_target_bitrate && new_target_bitrate_bps &&
ossu20a4b3f2017-04-27 02:08:52 -0700611 new_target_bitrate_bps !=
612 old_config.send_codec_spec->target_bitrate_bps) {
613 CallEncoder(stream->channel_proxy_, [&](AudioEncoder* encoder) {
614 encoder->OnReceivedTargetAudioBitrate(*new_target_bitrate_bps);
615 });
616 }
617
618 ReconfigureANA(stream, new_config);
619 ReconfigureCNG(stream, new_config);
620
621 return true;
622}
623
624void AudioSendStream::ReconfigureANA(AudioSendStream* stream,
625 const Config& new_config) {
626 if (new_config.audio_network_adaptor_config ==
627 stream->config_.audio_network_adaptor_config) {
628 return;
629 }
630 if (new_config.audio_network_adaptor_config) {
631 CallEncoder(stream->channel_proxy_, [&](AudioEncoder* encoder) {
632 if (encoder->EnableAudioNetworkAdaptor(
633 *new_config.audio_network_adaptor_config, stream->event_log_)) {
Jonas Olsson24ea8222018-01-25 10:14:29 +0100634 RTC_DLOG(LS_INFO) << "Audio network adaptor enabled on SSRC "
635 << new_config.rtp.ssrc;
ossu20a4b3f2017-04-27 02:08:52 -0700636 } else {
637 RTC_NOTREACHED();
638 }
639 });
640 } else {
641 CallEncoder(stream->channel_proxy_, [&](AudioEncoder* encoder) {
642 encoder->DisableAudioNetworkAdaptor();
643 });
Jonas Olsson24ea8222018-01-25 10:14:29 +0100644 RTC_DLOG(LS_INFO) << "Audio network adaptor disabled on SSRC "
645 << new_config.rtp.ssrc;
ossu20a4b3f2017-04-27 02:08:52 -0700646 }
647}
648
649void AudioSendStream::ReconfigureCNG(AudioSendStream* stream,
650 const Config& new_config) {
651 if (new_config.send_codec_spec->cng_payload_type ==
652 stream->config_.send_codec_spec->cng_payload_type) {
653 return;
654 }
655
ossu3b9ff382017-04-27 08:03:42 -0700656 // Register the CNG payload type if it's been added, don't do anything if CNG
657 // is removed. Payload types must not be redefined.
658 if (new_config.send_codec_spec->cng_payload_type) {
659 stream->RegisterCngPayloadType(
660 *new_config.send_codec_spec->cng_payload_type,
661 new_config.send_codec_spec->format.clockrate_hz);
662 }
663
ossu20a4b3f2017-04-27 02:08:52 -0700664 // Wrap or unwrap the encoder in an AudioEncoderCNG.
665 stream->channel_proxy_->ModifyEncoder(
666 [&](std::unique_ptr<AudioEncoder>* encoder_ptr) {
667 std::unique_ptr<AudioEncoder> old_encoder(std::move(*encoder_ptr));
668 auto sub_encoders = old_encoder->ReclaimContainedEncoders();
669 if (!sub_encoders.empty()) {
670 // Replace enc with its sub encoder. We need to put the sub
671 // encoder in a temporary first, since otherwise the old value
672 // of enc would be destroyed before the new value got assigned,
673 // which would be bad since the new value is a part of the old
674 // value.
675 auto tmp = std::move(sub_encoders[0]);
676 old_encoder = std::move(tmp);
677 }
678 if (new_config.send_codec_spec->cng_payload_type) {
679 AudioEncoderCng::Config config;
680 config.speech_encoder = std::move(old_encoder);
681 config.num_channels = config.speech_encoder->NumChannels();
682 config.payload_type = *new_config.send_codec_spec->cng_payload_type;
683 config.vad_mode = Vad::kVadNormal;
684 encoder_ptr->reset(new AudioEncoderCng(std::move(config)));
685 } else {
686 *encoder_ptr = std::move(old_encoder);
687 }
688 });
689}
690
691void AudioSendStream::ReconfigureBitrateObserver(
692 AudioSendStream* stream,
693 const webrtc::AudioSendStream::Config& new_config) {
694 // Since the Config's default is for both of these to be -1, this test will
695 // allow us to configure the bitrate observer if the new config has bitrate
696 // limits set, but would only have us call RemoveBitrateObserver if we were
697 // previously configured with bitrate limits.
Alex Narestcedd3512017-12-07 20:54:55 +0100698 int new_transport_seq_num_id =
699 FindExtensionIds(new_config.rtp.extensions).transport_sequence_number;
ossu20a4b3f2017-04-27 02:08:52 -0700700 if (stream->config_.min_bitrate_bps == new_config.min_bitrate_bps &&
Alex Narestcedd3512017-12-07 20:54:55 +0100701 stream->config_.max_bitrate_bps == new_config.max_bitrate_bps &&
Seth Hampson24722b32017-12-22 09:36:42 -0800702 stream->config_.bitrate_priority == new_config.bitrate_priority &&
Alex Narestcedd3512017-12-07 20:54:55 +0100703 (FindExtensionIds(stream->config_.rtp.extensions)
704 .transport_sequence_number == new_transport_seq_num_id ||
705 !webrtc::field_trial::IsEnabled("WebRTC-Audio-SendSideBwe"))) {
ossu20a4b3f2017-04-27 02:08:52 -0700706 return;
707 }
708
Sebastian Jansson763e9472018-03-21 12:46:56 +0100709 bool has_transport_sequence_number = new_transport_seq_num_id != 0;
Alex Narestcedd3512017-12-07 20:54:55 +0100710 if (new_config.min_bitrate_bps != -1 && new_config.max_bitrate_bps != -1 &&
Sebastian Jansson763e9472018-03-21 12:46:56 +0100711 (has_transport_sequence_number ||
Alex Narestcedd3512017-12-07 20:54:55 +0100712 !webrtc::field_trial::IsEnabled("WebRTC-Audio-SendSideBwe"))) {
Sebastian Jansson763e9472018-03-21 12:46:56 +0100713 stream->ConfigureBitrateObserver(
714 new_config.min_bitrate_bps, new_config.max_bitrate_bps,
715 new_config.bitrate_priority, has_transport_sequence_number);
ossu20a4b3f2017-04-27 02:08:52 -0700716 } else {
717 stream->RemoveBitrateObserver();
718 }
719}
720
721void AudioSendStream::ConfigureBitrateObserver(int min_bitrate_bps,
Seth Hampson24722b32017-12-22 09:36:42 -0800722 int max_bitrate_bps,
Sebastian Jansson763e9472018-03-21 12:46:56 +0100723 double bitrate_priority,
724 bool has_packet_feedback) {
ossu20a4b3f2017-04-27 02:08:52 -0700725 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
726 RTC_DCHECK_GE(max_bitrate_bps, min_bitrate_bps);
727 rtc::Event thread_sync_event(false /* manual_reset */, false);
728 worker_queue_->PostTask([&] {
729 // We may get a callback immediately as the observer is registered, so make
730 // sure the bitrate limits in config_ are up-to-date.
731 config_.min_bitrate_bps = min_bitrate_bps;
732 config_.max_bitrate_bps = max_bitrate_bps;
Seth Hampson24722b32017-12-22 09:36:42 -0800733 config_.bitrate_priority = bitrate_priority;
734 // This either updates the current observer or adds a new observer.
Sebastian Jansson24ad7202018-04-19 08:25:12 +0200735 bitrate_allocator_->AddObserver(
736 this, MediaStreamAllocationConfig{
737 static_cast<uint32_t>(min_bitrate_bps),
738 static_cast<uint32_t>(max_bitrate_bps), 0, true,
739 config_.track_id, bitrate_priority, has_packet_feedback});
ossu20a4b3f2017-04-27 02:08:52 -0700740 thread_sync_event.Set();
741 });
742 thread_sync_event.Wait(rtc::Event::kForever);
743}
744
745void AudioSendStream::RemoveBitrateObserver() {
746 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
747 rtc::Event thread_sync_event(false /* manual_reset */, false);
748 worker_queue_->PostTask([this, &thread_sync_event] {
749 bitrate_allocator_->RemoveObserver(this);
750 thread_sync_event.Set();
751 });
752 thread_sync_event.Wait(rtc::Event::kForever);
753}
754
ossu3b9ff382017-04-27 08:03:42 -0700755void AudioSendStream::RegisterCngPayloadType(int payload_type,
756 int clockrate_hz) {
ossu3b9ff382017-04-27 08:03:42 -0700757 const CodecInst codec = {payload_type, "CN", clockrate_hz, 0, 1, 0};
ossuc3d4b482017-05-23 06:07:11 -0700758 if (rtp_rtcp_module_->RegisterSendPayload(codec) != 0) {
759 rtp_rtcp_module_->DeRegisterSendPayload(codec.pltype);
760 if (rtp_rtcp_module_->RegisterSendPayload(codec) != 0) {
Jonas Olsson24ea8222018-01-25 10:14:29 +0100761 RTC_DLOG(LS_ERROR) << "RegisterCngPayloadType() failed to register CN to "
762 "RTP/RTCP module";
ossu3b9ff382017-04-27 08:03:42 -0700763 }
764 }
765}
solenbergc7a8b082015-10-16 14:35:07 -0700766} // namespace internal
767} // namespace webrtc