blob: de1be2c36c1e1ab9b677ef702a5a1ab157738a68 [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
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020017#include "audio/audio_state.h"
Fredrik Solenberga8b7c7f2018-01-17 11:18:31 +010018#include "audio/channel_proxy.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020019#include "audio/conversion.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020020#include "call/rtp_transport_controller_send_interface.h"
21#include "modules/audio_coding/codecs/cng/audio_encoder_cng.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020022#include "rtc_base/checks.h"
23#include "rtc_base/event.h"
24#include "rtc_base/function_view.h"
25#include "rtc_base/logging.h"
Jonas Olssonabbe8412018-04-03 13:40:05 +020026#include "rtc_base/strings/audio_format_to_string.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020027#include "rtc_base/task_queue.h"
28#include "rtc_base/timeutils.h"
Alex Narestcedd3512017-12-07 20:54:55 +010029#include "system_wrappers/include/field_trial.h"
solenbergc7a8b082015-10-16 14:35:07 -070030
31namespace webrtc {
solenbergc7a8b082015-10-16 14:35:07 -070032namespace internal {
Fredrik Solenberg8f5787a2018-01-11 13:52:30 +010033namespace {
eladalonedd6eea2017-05-25 00:15:35 -070034// TODO(eladalon): Subsequent CL will make these values experiment-dependent.
elad.alond12a8e12017-03-23 11:04:48 -070035constexpr size_t kPacketLossTrackerMaxWindowSizeMs = 15000;
36constexpr size_t kPacketLossRateMinNumAckedPackets = 50;
37constexpr size_t kRecoverablePacketLossRateMinNumAckedPairs = 40;
38
ossu20a4b3f2017-04-27 02:08:52 -070039void CallEncoder(const std::unique_ptr<voe::ChannelProxy>& channel_proxy,
40 rtc::FunctionView<void(AudioEncoder*)> lambda) {
41 channel_proxy->ModifyEncoder([&](std::unique_ptr<AudioEncoder>* encoder_ptr) {
42 RTC_DCHECK(encoder_ptr);
43 lambda(encoder_ptr->get());
44 });
45}
Fredrik Solenberg8f5787a2018-01-11 13:52:30 +010046
47std::unique_ptr<voe::ChannelProxy> CreateChannelAndProxy(
48 webrtc::AudioState* audio_state,
49 rtc::TaskQueue* worker_queue,
Tommi5f223652018-03-26 13:28:26 +020050 ProcessThread* module_process_thread,
51 RtcpRttStats* rtcp_rtt_stats) {
Fredrik Solenberg8f5787a2018-01-11 13:52:30 +010052 RTC_DCHECK(audio_state);
53 internal::AudioState* internal_audio_state =
54 static_cast<internal::AudioState*>(audio_state);
Tommi5f223652018-03-26 13:28:26 +020055 return std::unique_ptr<voe::ChannelProxy>(
56 new voe::ChannelProxy(std::unique_ptr<voe::Channel>(new voe::Channel(
57 worker_queue, module_process_thread,
58 internal_audio_state->audio_device_module(), rtcp_rtt_stats))));
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,
Fredrik Solenberg8f5787a2018-01-11 13:52:30 +0100105 CreateChannelAndProxy(audio_state.get(),
106 worker_queue,
Tommi5f223652018-03-26 13:28:26 +0200107 module_process_thread,
108 rtcp_rtt_stats)) {}
Fredrik Solenberg8f5787a2018-01-11 13:52:30 +0100109
110AudioSendStream::AudioSendStream(
111 const webrtc::AudioSendStream::Config& config,
112 const rtc::scoped_refptr<webrtc::AudioState>& audio_state,
113 rtc::TaskQueue* worker_queue,
114 RtpTransportControllerSendInterface* transport,
115 BitrateAllocator* bitrate_allocator,
116 RtcEventLog* event_log,
117 RtcpRttStats* rtcp_rtt_stats,
Danil Chapovalovb9b146c2018-06-15 12:28:07 +0200118 const absl::optional<RtpState>& suspended_rtp_state,
Sam Zackrisson06953ba2018-02-01 16:53:16 +0100119 TimeInterval* overall_call_lifetime,
Fredrik Solenberg8f5787a2018-01-11 13:52:30 +0100120 std::unique_ptr<voe::ChannelProxy> channel_proxy)
perkj26091b12016-09-01 01:17:40 -0700121 : worker_queue_(worker_queue),
ossu20a4b3f2017-04-27 02:08:52 -0700122 config_(Config(nullptr)),
mflodman86cc6ff2016-07-26 04:44:06 -0700123 audio_state_(audio_state),
Fredrik Solenberg8f5787a2018-01-11 13:52:30 +0100124 channel_proxy_(std::move(channel_proxy)),
ossu20a4b3f2017-04-27 02:08:52 -0700125 event_log_(event_log),
michaeltf4caaab2017-01-16 23:55:07 -0800126 bitrate_allocator_(bitrate_allocator),
nisseb8f9a322017-03-27 05:36:15 -0700127 transport_(transport),
elad.alond12a8e12017-03-23 11:04:48 -0700128 packet_loss_tracker_(kPacketLossTrackerMaxWindowSizeMs,
129 kPacketLossRateMinNumAckedPackets,
ossuc3d4b482017-05-23 06:07:11 -0700130 kRecoverablePacketLossRateMinNumAckedPairs),
131 rtp_rtcp_module_(nullptr),
Sam Zackrisson06953ba2018-02-01 16:53:16 +0100132 suspended_rtp_state_(suspended_rtp_state),
133 overall_call_lifetime_(overall_call_lifetime) {
Jonas Olsson24ea8222018-01-25 10:14:29 +0100134 RTC_LOG(LS_INFO) << "AudioSendStream: " << config.rtp.ssrc;
Fredrik Solenberg8f5787a2018-01-11 13:52:30 +0100135 RTC_DCHECK(worker_queue_);
136 RTC_DCHECK(audio_state_);
137 RTC_DCHECK(channel_proxy_);
138 RTC_DCHECK(bitrate_allocator_);
nisseb8f9a322017-03-27 05:36:15 -0700139 RTC_DCHECK(transport);
Sam Zackrisson06953ba2018-02-01 16:53:16 +0100140 RTC_DCHECK(overall_call_lifetime_);
solenberg3a941542015-11-16 07:34:50 -0800141
ossu20a4b3f2017-04-27 02:08:52 -0700142 channel_proxy_->SetRtcEventLog(event_log_);
solenberg13725082015-11-25 08:16:52 -0800143 channel_proxy_->SetRTCPStatus(true);
ossuc3d4b482017-05-23 06:07:11 -0700144 RtpReceiver* rtpReceiver = nullptr; // Unused, but required for call.
145 channel_proxy_->GetRtpRtcp(&rtp_rtcp_module_, &rtpReceiver);
146 RTC_DCHECK(rtp_rtcp_module_);
mflodman3d7db262016-04-29 00:57:13 -0700147
ossu20a4b3f2017-04-27 02:08:52 -0700148 ConfigureStream(this, config, true);
elad.alond12a8e12017-03-23 11:04:48 -0700149
150 pacer_thread_checker_.DetachFromThread();
Danil Chapovalov90e1f532017-10-03 14:59:27 +0200151 // Signal congestion controller this object is ready for OnPacket* callbacks.
Sebastian Janssone4be6da2018-02-15 16:51:41 +0100152 transport_->RegisterPacketFeedbackObserver(this);
solenbergc7a8b082015-10-16 14:35:07 -0700153}
154
155AudioSendStream::~AudioSendStream() {
elad.alond12a8e12017-03-23 11:04:48 -0700156 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
Jonas Olsson24ea8222018-01-25 10:14:29 +0100157 RTC_LOG(LS_INFO) << "~AudioSendStream: " << config_.rtp.ssrc;
Fredrik Solenberg2a877972017-12-15 16:42:15 +0100158 RTC_DCHECK(!sending_);
Sebastian Janssone4be6da2018-02-15 16:51:41 +0100159 transport_->DeRegisterPacketFeedbackObserver(this);
solenberg1c239d42017-09-29 06:00:28 -0700160 channel_proxy_->RegisterTransport(nullptr);
nissefdbfdc92017-03-31 05:44:52 -0700161 channel_proxy_->ResetSenderCongestionControlObjects();
tereliuse035e2d2016-09-21 06:51:47 -0700162 channel_proxy_->SetRtcEventLog(nullptr);
Sam Zackrisson06953ba2018-02-01 16:53:16 +0100163 // Lifetime can only be updated after deregistering
164 // |timed_send_transport_adapter_| in the underlying channel object to avoid
165 // data races in |active_lifetime_|.
166 overall_call_lifetime_->Extend(active_lifetime_);
solenbergc7a8b082015-10-16 14:35:07 -0700167}
168
eladalonabbc4302017-07-26 02:09:44 -0700169const webrtc::AudioSendStream::Config& AudioSendStream::GetConfig() const {
170 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
171 return config_;
172}
173
ossu20a4b3f2017-04-27 02:08:52 -0700174void AudioSendStream::Reconfigure(
175 const webrtc::AudioSendStream::Config& new_config) {
Fredrik Solenberg2a877972017-12-15 16:42:15 +0100176 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
ossu20a4b3f2017-04-27 02:08:52 -0700177 ConfigureStream(this, new_config, false);
178}
179
Alex Narestcedd3512017-12-07 20:54:55 +0100180AudioSendStream::ExtensionIds AudioSendStream::FindExtensionIds(
181 const std::vector<RtpExtension>& extensions) {
182 ExtensionIds ids;
183 for (const auto& extension : extensions) {
184 if (extension.uri == RtpExtension::kAudioLevelUri) {
185 ids.audio_level = extension.id;
186 } else if (extension.uri == RtpExtension::kTransportSequenceNumberUri) {
187 ids.transport_sequence_number = extension.id;
Steve Antonbb50ce52018-03-26 10:24:32 -0700188 } else if (extension.uri == RtpExtension::kMidUri) {
189 ids.mid = extension.id;
Alex Narestcedd3512017-12-07 20:54:55 +0100190 }
191 }
192 return ids;
193}
194
ossu20a4b3f2017-04-27 02:08:52 -0700195void AudioSendStream::ConfigureStream(
196 webrtc::internal::AudioSendStream* stream,
197 const webrtc::AudioSendStream::Config& new_config,
198 bool first_time) {
Jonas Olsson24ea8222018-01-25 10:14:29 +0100199 RTC_LOG(LS_INFO) << "AudioSendStream::ConfigureStream: "
200 << new_config.ToString();
ossu20a4b3f2017-04-27 02:08:52 -0700201 const auto& channel_proxy = stream->channel_proxy_;
202 const auto& old_config = stream->config_;
203
204 if (first_time || old_config.rtp.ssrc != new_config.rtp.ssrc) {
205 channel_proxy->SetLocalSSRC(new_config.rtp.ssrc);
ossuc3d4b482017-05-23 06:07:11 -0700206 if (stream->suspended_rtp_state_) {
207 stream->rtp_rtcp_module_->SetRtpState(*stream->suspended_rtp_state_);
208 }
ossu20a4b3f2017-04-27 02:08:52 -0700209 }
210 if (first_time || old_config.rtp.c_name != new_config.rtp.c_name) {
211 channel_proxy->SetRTCP_CNAME(new_config.rtp.c_name);
212 }
213 // TODO(solenberg): Config NACK history window (which is a packet count),
214 // using the actual packet size for the configured codec.
215 if (first_time || old_config.rtp.nack.rtp_history_ms !=
216 new_config.rtp.nack.rtp_history_ms) {
217 channel_proxy->SetNACKStatus(new_config.rtp.nack.rtp_history_ms != 0,
218 new_config.rtp.nack.rtp_history_ms / 20);
219 }
220
Yves Gerey665174f2018-06-19 15:03:05 +0200221 if (first_time || new_config.send_transport != old_config.send_transport) {
ossu20a4b3f2017-04-27 02:08:52 -0700222 if (old_config.send_transport) {
solenberg1c239d42017-09-29 06:00:28 -0700223 channel_proxy->RegisterTransport(nullptr);
ossu20a4b3f2017-04-27 02:08:52 -0700224 }
sazac58f8c02017-07-19 00:39:19 -0700225 if (new_config.send_transport) {
226 stream->timed_send_transport_adapter_.reset(new TimedTransport(
227 new_config.send_transport, &stream->active_lifetime_));
228 } else {
229 stream->timed_send_transport_adapter_.reset(nullptr);
230 }
solenberg1c239d42017-09-29 06:00:28 -0700231 channel_proxy->RegisterTransport(
sazac58f8c02017-07-19 00:39:19 -0700232 stream->timed_send_transport_adapter_.get());
ossu20a4b3f2017-04-27 02:08:52 -0700233 }
234
Alex Narestcedd3512017-12-07 20:54:55 +0100235 const ExtensionIds old_ids = FindExtensionIds(old_config.rtp.extensions);
236 const ExtensionIds new_ids = FindExtensionIds(new_config.rtp.extensions);
ossu20a4b3f2017-04-27 02:08:52 -0700237 // Audio level indication
238 if (first_time || new_ids.audio_level != old_ids.audio_level) {
239 channel_proxy->SetSendAudioLevelIndicationStatus(new_ids.audio_level != 0,
240 new_ids.audio_level);
241 }
Sebastian Jansson8d9c5402017-11-15 17:22:16 +0100242 bool transport_seq_num_id_changed =
243 new_ids.transport_sequence_number != old_ids.transport_sequence_number;
Alex Narest867e5102018-06-12 13:40:18 +0200244 if (first_time ||
245 (transport_seq_num_id_changed &&
246 !webrtc::field_trial::IsEnabled("WebRTC-Audio-ForceNoTWCC"))) {
ossu1129df22017-06-30 01:38:56 -0700247 if (!first_time) {
ossu20a4b3f2017-04-27 02:08:52 -0700248 channel_proxy->ResetSenderCongestionControlObjects();
ossu20a4b3f2017-04-27 02:08:52 -0700249 }
250
Sebastian Jansson8d9c5402017-11-15 17:22:16 +0100251 RtcpBandwidthObserver* bandwidth_observer = nullptr;
Alex Narest867e5102018-06-12 13:40:18 +0200252 bool has_transport_sequence_number =
253 new_ids.transport_sequence_number != 0 &&
254 !webrtc::field_trial::IsEnabled("WebRTC-Audio-ForceNoTWCC");
Sebastian Jansson8d9c5402017-11-15 17:22:16 +0100255 if (has_transport_sequence_number) {
ossu20a4b3f2017-04-27 02:08:52 -0700256 channel_proxy->EnableSendTransportSequenceNumber(
257 new_ids.transport_sequence_number);
Sebastian Jansson8d9c5402017-11-15 17:22:16 +0100258 // Probing in application limited region is only used in combination with
259 // send side congestion control, wich depends on feedback packets which
260 // requires transport sequence numbers to be enabled.
Sebastian Janssone4be6da2018-02-15 16:51:41 +0100261 stream->transport_->EnablePeriodicAlrProbing(true);
262 bandwidth_observer = stream->transport_->GetBandwidthObserver();
ossu20a4b3f2017-04-27 02:08:52 -0700263 }
264
Sebastian Jansson8d9c5402017-11-15 17:22:16 +0100265 channel_proxy->RegisterSenderCongestionControlObjects(stream->transport_,
266 bandwidth_observer);
ossu20a4b3f2017-04-27 02:08:52 -0700267 }
268
Steve Antonbb50ce52018-03-26 10:24:32 -0700269 // MID RTP header extension.
Steve Anton003930a2018-03-29 12:37:21 -0700270 if ((first_time || new_ids.mid != old_ids.mid ||
271 new_config.rtp.mid != old_config.rtp.mid) &&
272 new_ids.mid != 0 && !new_config.rtp.mid.empty()) {
Steve Antonbb50ce52018-03-26 10:24:32 -0700273 channel_proxy->SetMid(new_config.rtp.mid, new_ids.mid);
274 }
275
ossu20a4b3f2017-04-27 02:08:52 -0700276 if (!ReconfigureSendCodec(stream, new_config)) {
Mirko Bonadei675513b2017-11-09 11:09:25 +0100277 RTC_LOG(LS_ERROR) << "Failed to set up send codec state.";
ossu20a4b3f2017-04-27 02:08:52 -0700278 }
279
Oskar Sundbomf85e31b2017-12-20 16:38:09 +0100280 if (stream->sending_) {
281 ReconfigureBitrateObserver(stream, new_config);
282 }
ossu20a4b3f2017-04-27 02:08:52 -0700283 stream->config_ = new_config;
284}
285
solenberg3a941542015-11-16 07:34:50 -0800286void AudioSendStream::Start() {
elad.alond12a8e12017-03-23 11:04:48 -0700287 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
Fredrik Solenberg2a877972017-12-15 16:42:15 +0100288 if (sending_) {
289 return;
290 }
291
Sebastian Jansson763e9472018-03-21 12:46:56 +0100292 bool has_transport_sequence_number =
Alex Narest867e5102018-06-12 13:40:18 +0200293 FindExtensionIds(config_.rtp.extensions).transport_sequence_number != 0 &&
294 !webrtc::field_trial::IsEnabled("WebRTC-Audio-ForceNoTWCC");
Alex Narestcedd3512017-12-07 20:54:55 +0100295 if (config_.min_bitrate_bps != -1 && config_.max_bitrate_bps != -1 &&
Sebastian Jansson763e9472018-03-21 12:46:56 +0100296 (has_transport_sequence_number ||
Alex Narestbcf91802018-06-25 16:08:36 +0200297 !webrtc::field_trial::IsEnabled("WebRTC-Audio-SendSideBwe") ||
298 webrtc::field_trial::IsEnabled("WebRTC-Audio-ABWENoTWCC"))) {
Alex Narest78609d52017-10-20 10:37:47 +0200299 // Audio BWE is enabled.
300 transport_->packet_sender()->SetAccountForAudioPackets(true);
Seth Hampson24722b32017-12-22 09:36:42 -0800301 ConfigureBitrateObserver(config_.min_bitrate_bps, config_.max_bitrate_bps,
Sebastian Jansson763e9472018-03-21 12:46:56 +0100302 config_.bitrate_priority,
303 has_transport_sequence_number);
mflodman86cc6ff2016-07-26 04:44:06 -0700304 }
Fredrik Solenbergaaedf752017-12-18 13:09:12 +0100305 channel_proxy_->StartSend();
Fredrik Solenberg2a877972017-12-15 16:42:15 +0100306 sending_ = true;
307 audio_state()->AddSendingStream(this, encoder_sample_rate_hz_,
308 encoder_num_channels_);
solenberg3a941542015-11-16 07:34:50 -0800309}
310
311void AudioSendStream::Stop() {
elad.alond12a8e12017-03-23 11:04:48 -0700312 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
Fredrik Solenberg2a877972017-12-15 16:42:15 +0100313 if (!sending_) {
314 return;
315 }
316
ossu20a4b3f2017-04-27 02:08:52 -0700317 RemoveBitrateObserver();
Fredrik Solenbergaaedf752017-12-18 13:09:12 +0100318 channel_proxy_->StopSend();
Fredrik Solenberg2a877972017-12-15 16:42:15 +0100319 sending_ = false;
320 audio_state()->RemoveSendingStream(this);
321}
322
323void AudioSendStream::SendAudioData(std::unique_ptr<AudioFrame> audio_frame) {
324 RTC_CHECK_RUNS_SERIALIZED(&audio_capture_race_checker_);
325 channel_proxy_->ProcessAndEncodeAudio(std::move(audio_frame));
solenberg3a941542015-11-16 07:34:50 -0800326}
327
solenbergffbbcac2016-11-17 05:25:37 -0800328bool AudioSendStream::SendTelephoneEvent(int payload_type,
Yves Gerey665174f2018-06-19 15:03:05 +0200329 int payload_frequency,
330 int event,
solenberg8842c3e2016-03-11 03:06:41 -0800331 int duration_ms) {
elad.alond12a8e12017-03-23 11:04:48 -0700332 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
solenbergffbbcac2016-11-17 05:25:37 -0800333 return channel_proxy_->SetSendTelephoneEventPayloadType(payload_type,
334 payload_frequency) &&
Fredrik Solenbergb5727682015-12-04 15:22:19 +0100335 channel_proxy_->SendTelephoneEventOutband(event, duration_ms);
336}
337
solenberg94218532016-06-16 10:53:22 -0700338void AudioSendStream::SetMuted(bool muted) {
elad.alond12a8e12017-03-23 11:04:48 -0700339 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
solenberg94218532016-06-16 10:53:22 -0700340 channel_proxy_->SetInputMute(muted);
341}
342
solenbergc7a8b082015-10-16 14:35:07 -0700343webrtc::AudioSendStream::Stats AudioSendStream::GetStats() const {
Ivo Creusen56d46092017-11-24 17:29:59 +0100344 return GetStats(true);
345}
346
347webrtc::AudioSendStream::Stats AudioSendStream::GetStats(
348 bool has_remote_tracks) const {
elad.alond12a8e12017-03-23 11:04:48 -0700349 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
solenberg85a04962015-10-27 03:35:21 -0700350 webrtc::AudioSendStream::Stats stats;
351 stats.local_ssrc = config_.rtp.ssrc;
solenberg85a04962015-10-27 03:35:21 -0700352
solenberg358057b2015-11-27 10:46:42 -0800353 webrtc::CallStatistics call_stats = channel_proxy_->GetRTCPStatistics();
solenberg85a04962015-10-27 03:35:21 -0700354 stats.bytes_sent = call_stats.bytesSent;
355 stats.packets_sent = call_stats.packetsSent;
solenberg8b85de22015-11-16 09:48:04 -0800356 // RTT isn't known until a RTCP report is received. Until then, VoiceEngine
357 // returns 0 to indicate an error value.
358 if (call_stats.rttMs > 0) {
359 stats.rtt_ms = call_stats.rttMs;
360 }
ossu20a4b3f2017-04-27 02:08:52 -0700361 if (config_.send_codec_spec) {
362 const auto& spec = *config_.send_codec_spec;
363 stats.codec_name = spec.format.name;
Oskar Sundbom2707fb22017-11-16 10:57:35 +0100364 stats.codec_payload_type = spec.payload_type;
solenberg85a04962015-10-27 03:35:21 -0700365
366 // Get data from the last remote RTCP report.
solenberg358057b2015-11-27 10:46:42 -0800367 for (const auto& block : channel_proxy_->GetRemoteRTCPReportBlocks()) {
solenberg8b85de22015-11-16 09:48:04 -0800368 // Lookup report for send ssrc only.
369 if (block.source_SSRC == stats.local_ssrc) {
370 stats.packets_lost = block.cumulative_num_packets_lost;
371 stats.fraction_lost = Q8ToFloat(block.fraction_lost);
372 stats.ext_seqnum = block.extended_highest_sequence_number;
ossu20a4b3f2017-04-27 02:08:52 -0700373 // Convert timestamps to milliseconds.
374 if (spec.format.clockrate_hz / 1000 > 0) {
solenberg8b85de22015-11-16 09:48:04 -0800375 stats.jitter_ms =
ossu20a4b3f2017-04-27 02:08:52 -0700376 block.interarrival_jitter / (spec.format.clockrate_hz / 1000);
solenberg85a04962015-10-27 03:35:21 -0700377 }
solenberg8b85de22015-11-16 09:48:04 -0800378 break;
solenberg85a04962015-10-27 03:35:21 -0700379 }
380 }
381 }
382
Fredrik Solenberg2a877972017-12-15 16:42:15 +0100383 AudioState::Stats input_stats = audio_state()->GetAudioInputStats();
384 stats.audio_level = input_stats.audio_level;
385 stats.total_input_energy = input_stats.total_energy;
386 stats.total_input_duration = input_stats.total_duration;
solenberg796b8f92017-03-01 17:02:23 -0800387
Fredrik Solenberg2a877972017-12-15 16:42:15 +0100388 stats.typing_noise_detected = audio_state()->typing_noise_detected();
ivoce1198e02017-09-08 08:13:19 -0700389 stats.ana_statistics = channel_proxy_->GetANAStatistics();
Ivo Creusen56d46092017-11-24 17:29:59 +0100390 RTC_DCHECK(audio_state_->audio_processing());
391 stats.apm_statistics =
392 audio_state_->audio_processing()->GetStatistics(has_remote_tracks);
solenberg85a04962015-10-27 03:35:21 -0700393
394 return stats;
395}
396
pbos1ba8d392016-05-01 20:18:34 -0700397void AudioSendStream::SignalNetworkState(NetworkState state) {
elad.alond12a8e12017-03-23 11:04:48 -0700398 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
pbos1ba8d392016-05-01 20:18:34 -0700399}
400
401bool AudioSendStream::DeliverRtcp(const uint8_t* packet, size_t length) {
402 // TODO(solenberg): Tests call this function on a network thread, libjingle
403 // calls on the worker thread. We should move towards always using a network
404 // thread. Then this check can be enabled.
elad.alond12a8e12017-03-23 11:04:48 -0700405 // RTC_DCHECK(!worker_thread_checker_.CalledOnValidThread());
pbos1ba8d392016-05-01 20:18:34 -0700406 return channel_proxy_->ReceivedRTCPPacket(packet, length);
407}
408
mflodman86cc6ff2016-07-26 04:44:06 -0700409uint32_t AudioSendStream::OnBitrateUpdated(uint32_t bitrate_bps,
410 uint8_t fraction_loss,
minyue78b4d562016-11-30 04:47:39 -0800411 int64_t rtt,
minyue93e45222017-05-18 14:32:41 -0700412 int64_t bwe_period_ms) {
Alex Narestbcf91802018-06-25 16:08:36 +0200413 // Audio transport feedback will not be reported in this mode, instead update
414 // acknowledged bitrate estimator with the bitrate allocated for audio.
415 if (webrtc::field_trial::IsEnabled("WebRTC-Audio-ABWENoTWCC")) {
416 transport_->SetAllocatedBitrateWithoutFeedback(bitrate_bps);
417 }
418
stefanfca900a2017-04-10 03:53:00 -0700419 // A send stream may be allocated a bitrate of zero if the allocator decides
420 // to disable it. For now we ignore this decision and keep sending on min
421 // bitrate.
422 if (bitrate_bps == 0) {
423 bitrate_bps = config_.min_bitrate_bps;
424 }
Yves Gerey665174f2018-06-19 15:03:05 +0200425 RTC_DCHECK_GE(bitrate_bps, static_cast<uint32_t>(config_.min_bitrate_bps));
mflodman86cc6ff2016-07-26 04:44:06 -0700426 // The bitrate allocator might allocate an higher than max configured bitrate
427 // if there is room, to allow for, as example, extra FEC. Ignore that for now.
minyue10cbb462016-11-07 09:29:22 -0800428 const uint32_t max_bitrate_bps = config_.max_bitrate_bps;
mflodman86cc6ff2016-07-26 04:44:06 -0700429 if (bitrate_bps > max_bitrate_bps)
430 bitrate_bps = max_bitrate_bps;
431
minyue93e45222017-05-18 14:32:41 -0700432 channel_proxy_->SetBitrate(bitrate_bps, bwe_period_ms);
mflodman86cc6ff2016-07-26 04:44:06 -0700433
434 // The amount of audio protection is not exposed by the encoder, hence
435 // always returning 0.
436 return 0;
437}
438
elad.alond12a8e12017-03-23 11:04:48 -0700439void AudioSendStream::OnPacketAdded(uint32_t ssrc, uint16_t seq_num) {
440 RTC_DCHECK(pacer_thread_checker_.CalledOnValidThread());
441 // Only packets that belong to this stream are of interest.
442 if (ssrc == config_.rtp.ssrc) {
443 rtc::CritScope lock(&packet_loss_tracker_cs_);
eladalonedd6eea2017-05-25 00:15:35 -0700444 // TODO(eladalon): This function call could potentially reset the window,
elad.alond12a8e12017-03-23 11:04:48 -0700445 // setting both PLR and RPLR to unknown. Consider (during upcoming
446 // refactoring) passing an indication of such an event.
447 packet_loss_tracker_.OnPacketAdded(seq_num, rtc::TimeMillis());
448 }
449}
450
451void AudioSendStream::OnPacketFeedbackVector(
452 const std::vector<PacketFeedback>& packet_feedback_vector) {
eladalon3651fdd2017-08-24 07:26:25 -0700453 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
Danil Chapovalovb9b146c2018-06-15 12:28:07 +0200454 absl::optional<float> plr;
455 absl::optional<float> rplr;
elad.alond12a8e12017-03-23 11:04:48 -0700456 {
457 rtc::CritScope lock(&packet_loss_tracker_cs_);
458 packet_loss_tracker_.OnPacketFeedbackVector(packet_feedback_vector);
459 plr = packet_loss_tracker_.GetPacketLossRate();
elad.alondadb4dc2017-03-23 15:29:50 -0700460 rplr = packet_loss_tracker_.GetRecoverablePacketLossRate();
elad.alond12a8e12017-03-23 11:04:48 -0700461 }
eladalonedd6eea2017-05-25 00:15:35 -0700462 // TODO(eladalon): If R/PLR go back to unknown, no indication is given that
elad.alond12a8e12017-03-23 11:04:48 -0700463 // the previously sent value is no longer relevant. This will be taken care
464 // of with some refactoring which is now being done.
465 if (plr) {
466 channel_proxy_->OnTwccBasedUplinkPacketLossRate(*plr);
467 }
elad.alondadb4dc2017-03-23 15:29:50 -0700468 if (rplr) {
469 channel_proxy_->OnRecoverableUplinkPacketLossRate(*rplr);
470 }
elad.alond12a8e12017-03-23 11:04:48 -0700471}
472
michaelt79e05882016-11-08 02:50:09 -0800473void AudioSendStream::SetTransportOverhead(int transport_overhead_per_packet) {
elad.alond12a8e12017-03-23 11:04:48 -0700474 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
michaelt79e05882016-11-08 02:50:09 -0800475 channel_proxy_->SetTransportOverhead(transport_overhead_per_packet);
476}
477
ossuc3d4b482017-05-23 06:07:11 -0700478RtpState AudioSendStream::GetRtpState() const {
479 return rtp_rtcp_module_->GetRtpState();
480}
481
Fredrik Solenberg8f5787a2018-01-11 13:52:30 +0100482const voe::ChannelProxy& AudioSendStream::GetChannelProxy() const {
483 RTC_DCHECK(channel_proxy_.get());
484 return *channel_proxy_.get();
485}
486
Fredrik Solenberg2a877972017-12-15 16:42:15 +0100487internal::AudioState* AudioSendStream::audio_state() {
488 internal::AudioState* audio_state =
489 static_cast<internal::AudioState*>(audio_state_.get());
490 RTC_DCHECK(audio_state);
491 return audio_state;
492}
493
494const internal::AudioState* AudioSendStream::audio_state() const {
495 internal::AudioState* audio_state =
496 static_cast<internal::AudioState*>(audio_state_.get());
497 RTC_DCHECK(audio_state);
498 return audio_state;
499}
500
Fredrik Solenberg2a877972017-12-15 16:42:15 +0100501void AudioSendStream::StoreEncoderProperties(int sample_rate_hz,
502 size_t num_channels) {
503 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
504 encoder_sample_rate_hz_ = sample_rate_hz;
505 encoder_num_channels_ = num_channels;
506 if (sending_) {
507 // Update AudioState's information about the stream.
508 audio_state()->AddSendingStream(this, sample_rate_hz, num_channels);
509 }
510}
511
minyue7a973442016-10-20 03:27:12 -0700512// Apply current codec settings to a single voe::Channel used for sending.
ossu20a4b3f2017-04-27 02:08:52 -0700513bool AudioSendStream::SetupSendCodec(AudioSendStream* stream,
514 const Config& new_config) {
515 RTC_DCHECK(new_config.send_codec_spec);
516 const auto& spec = *new_config.send_codec_spec;
minyue48368ad2017-05-10 04:06:11 -0700517
518 RTC_DCHECK(new_config.encoder_factory);
ossu20a4b3f2017-04-27 02:08:52 -0700519 std::unique_ptr<AudioEncoder> encoder =
Karl Wiberg77490b92018-03-21 15:18:42 +0100520 new_config.encoder_factory->MakeAudioEncoder(
521 spec.payload_type, spec.format, new_config.codec_pair_id);
minyue7a973442016-10-20 03:27:12 -0700522
ossu20a4b3f2017-04-27 02:08:52 -0700523 if (!encoder) {
Jonas Olssonabbe8412018-04-03 13:40:05 +0200524 RTC_DLOG(LS_ERROR) << "Unable to create encoder for "
525 << rtc::ToString(spec.format);
ossu20a4b3f2017-04-27 02:08:52 -0700526 return false;
527 }
Alex Narestbbbe4e12018-07-13 10:32:58 +0200528
529 // If other side does not support audio TWCC and WebRTC-Audio-ABWENoTWCC is
530 // not enabled, do not update target audio bitrate if we are in
531 // WebRTC-Audio-SendSideBwe-For-Video experiment
532 const bool do_not_update_target_bitrate =
533 !webrtc::field_trial::IsEnabled("WebRTC-Audio-ABWENoTWCC") &&
534 webrtc::field_trial::IsEnabled("WebRTC-Audio-SendSideBwe-For-Video") &&
535 !FindExtensionIds(new_config.rtp.extensions).transport_sequence_number;
ossu20a4b3f2017-04-27 02:08:52 -0700536 // If a bitrate has been specified for the codec, use it over the
537 // codec's default.
Alex Narestbbbe4e12018-07-13 10:32:58 +0200538 if (!do_not_update_target_bitrate && spec.target_bitrate_bps) {
ossu20a4b3f2017-04-27 02:08:52 -0700539 encoder->OnReceivedTargetAudioBitrate(*spec.target_bitrate_bps);
minyue7a973442016-10-20 03:27:12 -0700540 }
541
ossu20a4b3f2017-04-27 02:08:52 -0700542 // Enable ANA if configured (currently only used by Opus).
543 if (new_config.audio_network_adaptor_config) {
544 if (encoder->EnableAudioNetworkAdaptor(
545 *new_config.audio_network_adaptor_config, stream->event_log_)) {
Jonas Olsson24ea8222018-01-25 10:14:29 +0100546 RTC_DLOG(LS_INFO) << "Audio network adaptor enabled on SSRC "
547 << new_config.rtp.ssrc;
ossu20a4b3f2017-04-27 02:08:52 -0700548 } else {
549 RTC_NOTREACHED();
minyue6b825df2016-10-31 04:08:32 -0700550 }
minyue7a973442016-10-20 03:27:12 -0700551 }
552
ossu20a4b3f2017-04-27 02:08:52 -0700553 // Wrap the encoder in a an AudioEncoderCNG, if VAD is enabled.
554 if (spec.cng_payload_type) {
555 AudioEncoderCng::Config cng_config;
556 cng_config.num_channels = encoder->NumChannels();
557 cng_config.payload_type = *spec.cng_payload_type;
558 cng_config.speech_encoder = std::move(encoder);
559 cng_config.vad_mode = Vad::kVadNormal;
560 encoder.reset(new AudioEncoderCng(std::move(cng_config)));
ossu3b9ff382017-04-27 08:03:42 -0700561
562 stream->RegisterCngPayloadType(
563 *spec.cng_payload_type,
564 new_config.send_codec_spec->format.clockrate_hz);
minyue7a973442016-10-20 03:27:12 -0700565 }
ossu20a4b3f2017-04-27 02:08:52 -0700566
Fredrik Solenberg2a877972017-12-15 16:42:15 +0100567 stream->StoreEncoderProperties(encoder->SampleRateHz(),
568 encoder->NumChannels());
ossu20a4b3f2017-04-27 02:08:52 -0700569 stream->channel_proxy_->SetEncoder(new_config.send_codec_spec->payload_type,
570 std::move(encoder));
minyue7a973442016-10-20 03:27:12 -0700571 return true;
572}
573
ossu20a4b3f2017-04-27 02:08:52 -0700574bool AudioSendStream::ReconfigureSendCodec(AudioSendStream* stream,
575 const Config& new_config) {
576 const auto& old_config = stream->config_;
minyue-webrtc8de18262017-07-26 14:18:40 +0200577
578 if (!new_config.send_codec_spec) {
579 // We cannot de-configure a send codec. So we will do nothing.
580 // By design, the send codec should have not been configured.
581 RTC_DCHECK(!old_config.send_codec_spec);
582 return true;
583 }
584
585 if (new_config.send_codec_spec == old_config.send_codec_spec &&
586 new_config.audio_network_adaptor_config ==
587 old_config.audio_network_adaptor_config) {
ossu20a4b3f2017-04-27 02:08:52 -0700588 return true;
589 }
590
591 // If we have no encoder, or the format or payload type's changed, create a
592 // new encoder.
593 if (!old_config.send_codec_spec ||
594 new_config.send_codec_spec->format !=
595 old_config.send_codec_spec->format ||
596 new_config.send_codec_spec->payload_type !=
597 old_config.send_codec_spec->payload_type) {
598 return SetupSendCodec(stream, new_config);
599 }
600
Alex Narestbbbe4e12018-07-13 10:32:58 +0200601 // If other side does not support audio TWCC and WebRTC-Audio-ABWENoTWCC is
602 // not enabled, do not update target audio bitrate if we are in
603 // WebRTC-Audio-SendSideBwe-For-Video experiment
604 const bool do_not_update_target_bitrate =
605 !webrtc::field_trial::IsEnabled("WebRTC-Audio-ABWENoTWCC") &&
606 webrtc::field_trial::IsEnabled("WebRTC-Audio-SendSideBwe-For-Video") &&
607 !FindExtensionIds(new_config.rtp.extensions).transport_sequence_number;
608
Danil Chapovalovb9b146c2018-06-15 12:28:07 +0200609 const absl::optional<int>& new_target_bitrate_bps =
ossu20a4b3f2017-04-27 02:08:52 -0700610 new_config.send_codec_spec->target_bitrate_bps;
611 // If a bitrate has been specified for the codec, use it over the
612 // codec's default.
Alex Narestbbbe4e12018-07-13 10:32:58 +0200613 if (!do_not_update_target_bitrate && new_target_bitrate_bps &&
ossu20a4b3f2017-04-27 02:08:52 -0700614 new_target_bitrate_bps !=
615 old_config.send_codec_spec->target_bitrate_bps) {
616 CallEncoder(stream->channel_proxy_, [&](AudioEncoder* encoder) {
617 encoder->OnReceivedTargetAudioBitrate(*new_target_bitrate_bps);
618 });
619 }
620
621 ReconfigureANA(stream, new_config);
622 ReconfigureCNG(stream, new_config);
623
624 return true;
625}
626
627void AudioSendStream::ReconfigureANA(AudioSendStream* stream,
628 const Config& new_config) {
629 if (new_config.audio_network_adaptor_config ==
630 stream->config_.audio_network_adaptor_config) {
631 return;
632 }
633 if (new_config.audio_network_adaptor_config) {
634 CallEncoder(stream->channel_proxy_, [&](AudioEncoder* encoder) {
635 if (encoder->EnableAudioNetworkAdaptor(
636 *new_config.audio_network_adaptor_config, stream->event_log_)) {
Jonas Olsson24ea8222018-01-25 10:14:29 +0100637 RTC_DLOG(LS_INFO) << "Audio network adaptor enabled on SSRC "
638 << new_config.rtp.ssrc;
ossu20a4b3f2017-04-27 02:08:52 -0700639 } else {
640 RTC_NOTREACHED();
641 }
642 });
643 } else {
644 CallEncoder(stream->channel_proxy_, [&](AudioEncoder* encoder) {
645 encoder->DisableAudioNetworkAdaptor();
646 });
Jonas Olsson24ea8222018-01-25 10:14:29 +0100647 RTC_DLOG(LS_INFO) << "Audio network adaptor disabled on SSRC "
648 << new_config.rtp.ssrc;
ossu20a4b3f2017-04-27 02:08:52 -0700649 }
650}
651
652void AudioSendStream::ReconfigureCNG(AudioSendStream* stream,
653 const Config& new_config) {
654 if (new_config.send_codec_spec->cng_payload_type ==
655 stream->config_.send_codec_spec->cng_payload_type) {
656 return;
657 }
658
ossu3b9ff382017-04-27 08:03:42 -0700659 // Register the CNG payload type if it's been added, don't do anything if CNG
660 // is removed. Payload types must not be redefined.
661 if (new_config.send_codec_spec->cng_payload_type) {
662 stream->RegisterCngPayloadType(
663 *new_config.send_codec_spec->cng_payload_type,
664 new_config.send_codec_spec->format.clockrate_hz);
665 }
666
ossu20a4b3f2017-04-27 02:08:52 -0700667 // Wrap or unwrap the encoder in an AudioEncoderCNG.
668 stream->channel_proxy_->ModifyEncoder(
669 [&](std::unique_ptr<AudioEncoder>* encoder_ptr) {
670 std::unique_ptr<AudioEncoder> old_encoder(std::move(*encoder_ptr));
671 auto sub_encoders = old_encoder->ReclaimContainedEncoders();
672 if (!sub_encoders.empty()) {
673 // Replace enc with its sub encoder. We need to put the sub
674 // encoder in a temporary first, since otherwise the old value
675 // of enc would be destroyed before the new value got assigned,
676 // which would be bad since the new value is a part of the old
677 // value.
678 auto tmp = std::move(sub_encoders[0]);
679 old_encoder = std::move(tmp);
680 }
681 if (new_config.send_codec_spec->cng_payload_type) {
682 AudioEncoderCng::Config config;
683 config.speech_encoder = std::move(old_encoder);
684 config.num_channels = config.speech_encoder->NumChannels();
685 config.payload_type = *new_config.send_codec_spec->cng_payload_type;
686 config.vad_mode = Vad::kVadNormal;
687 encoder_ptr->reset(new AudioEncoderCng(std::move(config)));
688 } else {
689 *encoder_ptr = std::move(old_encoder);
690 }
691 });
692}
693
694void AudioSendStream::ReconfigureBitrateObserver(
695 AudioSendStream* stream,
696 const webrtc::AudioSendStream::Config& new_config) {
697 // Since the Config's default is for both of these to be -1, this test will
698 // allow us to configure the bitrate observer if the new config has bitrate
699 // limits set, but would only have us call RemoveBitrateObserver if we were
700 // previously configured with bitrate limits.
Alex Narestcedd3512017-12-07 20:54:55 +0100701 int new_transport_seq_num_id =
702 FindExtensionIds(new_config.rtp.extensions).transport_sequence_number;
ossu20a4b3f2017-04-27 02:08:52 -0700703 if (stream->config_.min_bitrate_bps == new_config.min_bitrate_bps &&
Alex Narestcedd3512017-12-07 20:54:55 +0100704 stream->config_.max_bitrate_bps == new_config.max_bitrate_bps &&
Seth Hampson24722b32017-12-22 09:36:42 -0800705 stream->config_.bitrate_priority == new_config.bitrate_priority &&
Alex Narestcedd3512017-12-07 20:54:55 +0100706 (FindExtensionIds(stream->config_.rtp.extensions)
707 .transport_sequence_number == new_transport_seq_num_id ||
708 !webrtc::field_trial::IsEnabled("WebRTC-Audio-SendSideBwe"))) {
ossu20a4b3f2017-04-27 02:08:52 -0700709 return;
710 }
711
Sebastian Jansson763e9472018-03-21 12:46:56 +0100712 bool has_transport_sequence_number = new_transport_seq_num_id != 0;
Alex Narestcedd3512017-12-07 20:54:55 +0100713 if (new_config.min_bitrate_bps != -1 && new_config.max_bitrate_bps != -1 &&
Sebastian Jansson763e9472018-03-21 12:46:56 +0100714 (has_transport_sequence_number ||
Alex Narestcedd3512017-12-07 20:54:55 +0100715 !webrtc::field_trial::IsEnabled("WebRTC-Audio-SendSideBwe"))) {
Sebastian Jansson763e9472018-03-21 12:46:56 +0100716 stream->ConfigureBitrateObserver(
717 new_config.min_bitrate_bps, new_config.max_bitrate_bps,
718 new_config.bitrate_priority, has_transport_sequence_number);
ossu20a4b3f2017-04-27 02:08:52 -0700719 } else {
720 stream->RemoveBitrateObserver();
721 }
722}
723
724void AudioSendStream::ConfigureBitrateObserver(int min_bitrate_bps,
Seth Hampson24722b32017-12-22 09:36:42 -0800725 int max_bitrate_bps,
Sebastian Jansson763e9472018-03-21 12:46:56 +0100726 double bitrate_priority,
727 bool has_packet_feedback) {
ossu20a4b3f2017-04-27 02:08:52 -0700728 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
729 RTC_DCHECK_GE(max_bitrate_bps, min_bitrate_bps);
730 rtc::Event thread_sync_event(false /* manual_reset */, false);
731 worker_queue_->PostTask([&] {
732 // We may get a callback immediately as the observer is registered, so make
733 // sure the bitrate limits in config_ are up-to-date.
734 config_.min_bitrate_bps = min_bitrate_bps;
735 config_.max_bitrate_bps = max_bitrate_bps;
Seth Hampson24722b32017-12-22 09:36:42 -0800736 config_.bitrate_priority = bitrate_priority;
737 // This either updates the current observer or adds a new observer.
Sebastian Jansson24ad7202018-04-19 08:25:12 +0200738 bitrate_allocator_->AddObserver(
739 this, MediaStreamAllocationConfig{
740 static_cast<uint32_t>(min_bitrate_bps),
741 static_cast<uint32_t>(max_bitrate_bps), 0, true,
742 config_.track_id, bitrate_priority, has_packet_feedback});
ossu20a4b3f2017-04-27 02:08:52 -0700743 thread_sync_event.Set();
744 });
745 thread_sync_event.Wait(rtc::Event::kForever);
746}
747
748void AudioSendStream::RemoveBitrateObserver() {
749 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
750 rtc::Event thread_sync_event(false /* manual_reset */, false);
751 worker_queue_->PostTask([this, &thread_sync_event] {
752 bitrate_allocator_->RemoveObserver(this);
753 thread_sync_event.Set();
754 });
755 thread_sync_event.Wait(rtc::Event::kForever);
756}
757
ossu3b9ff382017-04-27 08:03:42 -0700758void AudioSendStream::RegisterCngPayloadType(int payload_type,
759 int clockrate_hz) {
ossu3b9ff382017-04-27 08:03:42 -0700760 const CodecInst codec = {payload_type, "CN", clockrate_hz, 0, 1, 0};
ossuc3d4b482017-05-23 06:07:11 -0700761 if (rtp_rtcp_module_->RegisterSendPayload(codec) != 0) {
762 rtp_rtcp_module_->DeRegisterSendPayload(codec.pltype);
763 if (rtp_rtcp_module_->RegisterSendPayload(codec) != 0) {
Jonas Olsson24ea8222018-01-25 10:14:29 +0100764 RTC_DLOG(LS_ERROR) << "RegisterCngPayloadType() failed to register CN to "
765 "RTP/RTCP module";
ossu3b9ff382017-04-27 08:03:42 -0700766 }
767 }
768}
solenbergc7a8b082015-10-16 14:35:07 -0700769} // namespace internal
770} // namespace webrtc