blob: 6df653add7a29cb19b6d2a2b63664e3223926904 [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);
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();
tereliuse035e2d2016-09-21 06:51:47 -0700161 channel_proxy_->SetRtcEventLog(nullptr);
Sam Zackrisson06953ba2018-02-01 16:53:16 +0100162 // Lifetime can only be updated after deregistering
163 // |timed_send_transport_adapter_| in the underlying channel object to avoid
164 // data races in |active_lifetime_|.
165 overall_call_lifetime_->Extend(active_lifetime_);
solenbergc7a8b082015-10-16 14:35:07 -0700166}
167
eladalonabbc4302017-07-26 02:09:44 -0700168const webrtc::AudioSendStream::Config& AudioSendStream::GetConfig() const {
169 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
170 return config_;
171}
172
ossu20a4b3f2017-04-27 02:08:52 -0700173void AudioSendStream::Reconfigure(
174 const webrtc::AudioSendStream::Config& new_config) {
Fredrik Solenberg2a877972017-12-15 16:42:15 +0100175 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
ossu20a4b3f2017-04-27 02:08:52 -0700176 ConfigureStream(this, new_config, false);
177}
178
Alex Narestcedd3512017-12-07 20:54:55 +0100179AudioSendStream::ExtensionIds AudioSendStream::FindExtensionIds(
180 const std::vector<RtpExtension>& extensions) {
181 ExtensionIds ids;
182 for (const auto& extension : extensions) {
183 if (extension.uri == RtpExtension::kAudioLevelUri) {
184 ids.audio_level = extension.id;
185 } else if (extension.uri == RtpExtension::kTransportSequenceNumberUri) {
186 ids.transport_sequence_number = extension.id;
Steve Antonbb50ce52018-03-26 10:24:32 -0700187 } else if (extension.uri == RtpExtension::kMidUri) {
188 ids.mid = extension.id;
Alex Narestcedd3512017-12-07 20:54:55 +0100189 }
190 }
191 return ids;
192}
193
ossu20a4b3f2017-04-27 02:08:52 -0700194void AudioSendStream::ConfigureStream(
195 webrtc::internal::AudioSendStream* stream,
196 const webrtc::AudioSendStream::Config& new_config,
197 bool first_time) {
Jonas Olsson24ea8222018-01-25 10:14:29 +0100198 RTC_LOG(LS_INFO) << "AudioSendStream::ConfigureStream: "
199 << new_config.ToString();
ossu20a4b3f2017-04-27 02:08:52 -0700200 const auto& channel_proxy = stream->channel_proxy_;
201 const auto& old_config = stream->config_;
202
203 if (first_time || old_config.rtp.ssrc != new_config.rtp.ssrc) {
204 channel_proxy->SetLocalSSRC(new_config.rtp.ssrc);
ossuc3d4b482017-05-23 06:07:11 -0700205 if (stream->suspended_rtp_state_) {
206 stream->rtp_rtcp_module_->SetRtpState(*stream->suspended_rtp_state_);
207 }
ossu20a4b3f2017-04-27 02:08:52 -0700208 }
209 if (first_time || old_config.rtp.c_name != new_config.rtp.c_name) {
210 channel_proxy->SetRTCP_CNAME(new_config.rtp.c_name);
211 }
212 // TODO(solenberg): Config NACK history window (which is a packet count),
213 // using the actual packet size for the configured codec.
214 if (first_time || old_config.rtp.nack.rtp_history_ms !=
215 new_config.rtp.nack.rtp_history_ms) {
216 channel_proxy->SetNACKStatus(new_config.rtp.nack.rtp_history_ms != 0,
217 new_config.rtp.nack.rtp_history_ms / 20);
218 }
219
Yves Gerey665174f2018-06-19 15:03:05 +0200220 if (first_time || new_config.send_transport != old_config.send_transport) {
ossu20a4b3f2017-04-27 02:08:52 -0700221 if (old_config.send_transport) {
solenberg1c239d42017-09-29 06:00:28 -0700222 channel_proxy->RegisterTransport(nullptr);
ossu20a4b3f2017-04-27 02:08:52 -0700223 }
sazac58f8c02017-07-19 00:39:19 -0700224 if (new_config.send_transport) {
225 stream->timed_send_transport_adapter_.reset(new TimedTransport(
226 new_config.send_transport, &stream->active_lifetime_));
227 } else {
228 stream->timed_send_transport_adapter_.reset(nullptr);
229 }
solenberg1c239d42017-09-29 06:00:28 -0700230 channel_proxy->RegisterTransport(
sazac58f8c02017-07-19 00:39:19 -0700231 stream->timed_send_transport_adapter_.get());
ossu20a4b3f2017-04-27 02:08:52 -0700232 }
233
Alex Narestcedd3512017-12-07 20:54:55 +0100234 const ExtensionIds old_ids = FindExtensionIds(old_config.rtp.extensions);
235 const ExtensionIds new_ids = FindExtensionIds(new_config.rtp.extensions);
ossu20a4b3f2017-04-27 02:08:52 -0700236 // Audio level indication
237 if (first_time || new_ids.audio_level != old_ids.audio_level) {
238 channel_proxy->SetSendAudioLevelIndicationStatus(new_ids.audio_level != 0,
239 new_ids.audio_level);
240 }
Sebastian Jansson8d9c5402017-11-15 17:22:16 +0100241 bool transport_seq_num_id_changed =
242 new_ids.transport_sequence_number != old_ids.transport_sequence_number;
Alex Narest867e5102018-06-12 13:40:18 +0200243 if (first_time ||
244 (transport_seq_num_id_changed &&
245 !webrtc::field_trial::IsEnabled("WebRTC-Audio-ForceNoTWCC"))) {
ossu1129df22017-06-30 01:38:56 -0700246 if (!first_time) {
ossu20a4b3f2017-04-27 02:08:52 -0700247 channel_proxy->ResetSenderCongestionControlObjects();
ossu20a4b3f2017-04-27 02:08:52 -0700248 }
249
Sebastian Jansson8d9c5402017-11-15 17:22:16 +0100250 RtcpBandwidthObserver* bandwidth_observer = nullptr;
Alex Narest867e5102018-06-12 13:40:18 +0200251 bool has_transport_sequence_number =
252 new_ids.transport_sequence_number != 0 &&
253 !webrtc::field_trial::IsEnabled("WebRTC-Audio-ForceNoTWCC");
Sebastian Jansson8d9c5402017-11-15 17:22:16 +0100254 if (has_transport_sequence_number) {
ossu20a4b3f2017-04-27 02:08:52 -0700255 channel_proxy->EnableSendTransportSequenceNumber(
256 new_ids.transport_sequence_number);
Sebastian Jansson8d9c5402017-11-15 17:22:16 +0100257 // Probing in application limited region is only used in combination with
258 // send side congestion control, wich depends on feedback packets which
259 // requires transport sequence numbers to be enabled.
Sebastian Janssone4be6da2018-02-15 16:51:41 +0100260 stream->transport_->EnablePeriodicAlrProbing(true);
261 bandwidth_observer = stream->transport_->GetBandwidthObserver();
ossu20a4b3f2017-04-27 02:08:52 -0700262 }
263
Sebastian Jansson8d9c5402017-11-15 17:22:16 +0100264 channel_proxy->RegisterSenderCongestionControlObjects(stream->transport_,
265 bandwidth_observer);
ossu20a4b3f2017-04-27 02:08:52 -0700266 }
267
Steve Antonbb50ce52018-03-26 10:24:32 -0700268 // MID RTP header extension.
Steve Anton003930a2018-03-29 12:37:21 -0700269 if ((first_time || new_ids.mid != old_ids.mid ||
270 new_config.rtp.mid != old_config.rtp.mid) &&
271 new_ids.mid != 0 && !new_config.rtp.mid.empty()) {
Steve Antonbb50ce52018-03-26 10:24:32 -0700272 channel_proxy->SetMid(new_config.rtp.mid, new_ids.mid);
273 }
274
ossu20a4b3f2017-04-27 02:08:52 -0700275 if (!ReconfigureSendCodec(stream, new_config)) {
Mirko Bonadei675513b2017-11-09 11:09:25 +0100276 RTC_LOG(LS_ERROR) << "Failed to set up send codec state.";
ossu20a4b3f2017-04-27 02:08:52 -0700277 }
278
Oskar Sundbomf85e31b2017-12-20 16:38:09 +0100279 if (stream->sending_) {
280 ReconfigureBitrateObserver(stream, new_config);
281 }
ossu20a4b3f2017-04-27 02:08:52 -0700282 stream->config_ = new_config;
283}
284
solenberg3a941542015-11-16 07:34:50 -0800285void AudioSendStream::Start() {
elad.alond12a8e12017-03-23 11:04:48 -0700286 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
Fredrik Solenberg2a877972017-12-15 16:42:15 +0100287 if (sending_) {
288 return;
289 }
290
Sebastian Jansson763e9472018-03-21 12:46:56 +0100291 bool has_transport_sequence_number =
Alex Narest867e5102018-06-12 13:40:18 +0200292 FindExtensionIds(config_.rtp.extensions).transport_sequence_number != 0 &&
293 !webrtc::field_trial::IsEnabled("WebRTC-Audio-ForceNoTWCC");
Alex Narestcedd3512017-12-07 20:54:55 +0100294 if (config_.min_bitrate_bps != -1 && config_.max_bitrate_bps != -1 &&
Sebastian Jansson763e9472018-03-21 12:46:56 +0100295 (has_transport_sequence_number ||
Alex Narestbcf91802018-06-25 16:08:36 +0200296 !webrtc::field_trial::IsEnabled("WebRTC-Audio-SendSideBwe") ||
297 webrtc::field_trial::IsEnabled("WebRTC-Audio-ABWENoTWCC"))) {
Alex Narest78609d52017-10-20 10:37:47 +0200298 // Audio BWE is enabled.
299 transport_->packet_sender()->SetAccountForAudioPackets(true);
Seth Hampson24722b32017-12-22 09:36:42 -0800300 ConfigureBitrateObserver(config_.min_bitrate_bps, config_.max_bitrate_bps,
Sebastian Jansson763e9472018-03-21 12:46:56 +0100301 config_.bitrate_priority,
302 has_transport_sequence_number);
mflodman86cc6ff2016-07-26 04:44:06 -0700303 }
Fredrik Solenbergaaedf752017-12-18 13:09:12 +0100304 channel_proxy_->StartSend();
Fredrik Solenberg2a877972017-12-15 16:42:15 +0100305 sending_ = true;
306 audio_state()->AddSendingStream(this, encoder_sample_rate_hz_,
307 encoder_num_channels_);
solenberg3a941542015-11-16 07:34:50 -0800308}
309
310void AudioSendStream::Stop() {
elad.alond12a8e12017-03-23 11:04:48 -0700311 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
Fredrik Solenberg2a877972017-12-15 16:42:15 +0100312 if (!sending_) {
313 return;
314 }
315
ossu20a4b3f2017-04-27 02:08:52 -0700316 RemoveBitrateObserver();
Fredrik Solenbergaaedf752017-12-18 13:09:12 +0100317 channel_proxy_->StopSend();
Fredrik Solenberg2a877972017-12-15 16:42:15 +0100318 sending_ = false;
319 audio_state()->RemoveSendingStream(this);
320}
321
322void AudioSendStream::SendAudioData(std::unique_ptr<AudioFrame> audio_frame) {
323 RTC_CHECK_RUNS_SERIALIZED(&audio_capture_race_checker_);
324 channel_proxy_->ProcessAndEncodeAudio(std::move(audio_frame));
solenberg3a941542015-11-16 07:34:50 -0800325}
326
solenbergffbbcac2016-11-17 05:25:37 -0800327bool AudioSendStream::SendTelephoneEvent(int payload_type,
Yves Gerey665174f2018-06-19 15:03:05 +0200328 int payload_frequency,
329 int event,
solenberg8842c3e2016-03-11 03:06:41 -0800330 int duration_ms) {
elad.alond12a8e12017-03-23 11:04:48 -0700331 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
solenbergffbbcac2016-11-17 05:25:37 -0800332 return channel_proxy_->SetSendTelephoneEventPayloadType(payload_type,
333 payload_frequency) &&
Fredrik Solenbergb5727682015-12-04 15:22:19 +0100334 channel_proxy_->SendTelephoneEventOutband(event, duration_ms);
335}
336
solenberg94218532016-06-16 10:53:22 -0700337void AudioSendStream::SetMuted(bool muted) {
elad.alond12a8e12017-03-23 11:04:48 -0700338 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
solenberg94218532016-06-16 10:53:22 -0700339 channel_proxy_->SetInputMute(muted);
340}
341
solenbergc7a8b082015-10-16 14:35:07 -0700342webrtc::AudioSendStream::Stats AudioSendStream::GetStats() const {
Ivo Creusen56d46092017-11-24 17:29:59 +0100343 return GetStats(true);
344}
345
346webrtc::AudioSendStream::Stats AudioSendStream::GetStats(
347 bool has_remote_tracks) const {
elad.alond12a8e12017-03-23 11:04:48 -0700348 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
solenberg85a04962015-10-27 03:35:21 -0700349 webrtc::AudioSendStream::Stats stats;
350 stats.local_ssrc = config_.rtp.ssrc;
solenberg85a04962015-10-27 03:35:21 -0700351
solenberg358057b2015-11-27 10:46:42 -0800352 webrtc::CallStatistics call_stats = channel_proxy_->GetRTCPStatistics();
solenberg85a04962015-10-27 03:35:21 -0700353 stats.bytes_sent = call_stats.bytesSent;
354 stats.packets_sent = call_stats.packetsSent;
solenberg8b85de22015-11-16 09:48:04 -0800355 // RTT isn't known until a RTCP report is received. Until then, VoiceEngine
356 // returns 0 to indicate an error value.
357 if (call_stats.rttMs > 0) {
358 stats.rtt_ms = call_stats.rttMs;
359 }
ossu20a4b3f2017-04-27 02:08:52 -0700360 if (config_.send_codec_spec) {
361 const auto& spec = *config_.send_codec_spec;
362 stats.codec_name = spec.format.name;
Oskar Sundbom2707fb22017-11-16 10:57:35 +0100363 stats.codec_payload_type = spec.payload_type;
solenberg85a04962015-10-27 03:35:21 -0700364
365 // Get data from the last remote RTCP report.
solenberg358057b2015-11-27 10:46:42 -0800366 for (const auto& block : channel_proxy_->GetRemoteRTCPReportBlocks()) {
solenberg8b85de22015-11-16 09:48:04 -0800367 // Lookup report for send ssrc only.
368 if (block.source_SSRC == stats.local_ssrc) {
369 stats.packets_lost = block.cumulative_num_packets_lost;
370 stats.fraction_lost = Q8ToFloat(block.fraction_lost);
371 stats.ext_seqnum = block.extended_highest_sequence_number;
ossu20a4b3f2017-04-27 02:08:52 -0700372 // Convert timestamps to milliseconds.
373 if (spec.format.clockrate_hz / 1000 > 0) {
solenberg8b85de22015-11-16 09:48:04 -0800374 stats.jitter_ms =
ossu20a4b3f2017-04-27 02:08:52 -0700375 block.interarrival_jitter / (spec.format.clockrate_hz / 1000);
solenberg85a04962015-10-27 03:35:21 -0700376 }
solenberg8b85de22015-11-16 09:48:04 -0800377 break;
solenberg85a04962015-10-27 03:35:21 -0700378 }
379 }
380 }
381
Fredrik Solenberg2a877972017-12-15 16:42:15 +0100382 AudioState::Stats input_stats = audio_state()->GetAudioInputStats();
383 stats.audio_level = input_stats.audio_level;
384 stats.total_input_energy = input_stats.total_energy;
385 stats.total_input_duration = input_stats.total_duration;
solenberg796b8f92017-03-01 17:02:23 -0800386
Fredrik Solenberg2a877972017-12-15 16:42:15 +0100387 stats.typing_noise_detected = audio_state()->typing_noise_detected();
ivoce1198e02017-09-08 08:13:19 -0700388 stats.ana_statistics = channel_proxy_->GetANAStatistics();
Ivo Creusen56d46092017-11-24 17:29:59 +0100389 RTC_DCHECK(audio_state_->audio_processing());
390 stats.apm_statistics =
391 audio_state_->audio_processing()->GetStatistics(has_remote_tracks);
solenberg85a04962015-10-27 03:35:21 -0700392
393 return stats;
394}
395
pbos1ba8d392016-05-01 20:18:34 -0700396void AudioSendStream::SignalNetworkState(NetworkState state) {
elad.alond12a8e12017-03-23 11:04:48 -0700397 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
pbos1ba8d392016-05-01 20:18:34 -0700398}
399
400bool AudioSendStream::DeliverRtcp(const uint8_t* packet, size_t length) {
401 // TODO(solenberg): Tests call this function on a network thread, libjingle
402 // calls on the worker thread. We should move towards always using a network
403 // thread. Then this check can be enabled.
elad.alond12a8e12017-03-23 11:04:48 -0700404 // RTC_DCHECK(!worker_thread_checker_.CalledOnValidThread());
pbos1ba8d392016-05-01 20:18:34 -0700405 return channel_proxy_->ReceivedRTCPPacket(packet, length);
406}
407
mflodman86cc6ff2016-07-26 04:44:06 -0700408uint32_t AudioSendStream::OnBitrateUpdated(uint32_t bitrate_bps,
409 uint8_t fraction_loss,
minyue78b4d562016-11-30 04:47:39 -0800410 int64_t rtt,
minyue93e45222017-05-18 14:32:41 -0700411 int64_t bwe_period_ms) {
Alex Narestbcf91802018-06-25 16:08:36 +0200412 // Audio transport feedback will not be reported in this mode, instead update
413 // acknowledged bitrate estimator with the bitrate allocated for audio.
414 if (webrtc::field_trial::IsEnabled("WebRTC-Audio-ABWENoTWCC")) {
415 transport_->SetAllocatedBitrateWithoutFeedback(bitrate_bps);
416 }
417
stefanfca900a2017-04-10 03:53:00 -0700418 // A send stream may be allocated a bitrate of zero if the allocator decides
419 // to disable it. For now we ignore this decision and keep sending on min
420 // bitrate.
421 if (bitrate_bps == 0) {
422 bitrate_bps = config_.min_bitrate_bps;
423 }
Yves Gerey665174f2018-06-19 15:03:05 +0200424 RTC_DCHECK_GE(bitrate_bps, static_cast<uint32_t>(config_.min_bitrate_bps));
mflodman86cc6ff2016-07-26 04:44:06 -0700425 // The bitrate allocator might allocate an higher than max configured bitrate
426 // if there is room, to allow for, as example, extra FEC. Ignore that for now.
minyue10cbb462016-11-07 09:29:22 -0800427 const uint32_t max_bitrate_bps = config_.max_bitrate_bps;
mflodman86cc6ff2016-07-26 04:44:06 -0700428 if (bitrate_bps > max_bitrate_bps)
429 bitrate_bps = max_bitrate_bps;
430
minyue93e45222017-05-18 14:32:41 -0700431 channel_proxy_->SetBitrate(bitrate_bps, bwe_period_ms);
mflodman86cc6ff2016-07-26 04:44:06 -0700432
433 // The amount of audio protection is not exposed by the encoder, hence
434 // always returning 0.
435 return 0;
436}
437
elad.alond12a8e12017-03-23 11:04:48 -0700438void AudioSendStream::OnPacketAdded(uint32_t ssrc, uint16_t seq_num) {
439 RTC_DCHECK(pacer_thread_checker_.CalledOnValidThread());
440 // Only packets that belong to this stream are of interest.
441 if (ssrc == config_.rtp.ssrc) {
442 rtc::CritScope lock(&packet_loss_tracker_cs_);
eladalonedd6eea2017-05-25 00:15:35 -0700443 // TODO(eladalon): This function call could potentially reset the window,
elad.alond12a8e12017-03-23 11:04:48 -0700444 // setting both PLR and RPLR to unknown. Consider (during upcoming
445 // refactoring) passing an indication of such an event.
446 packet_loss_tracker_.OnPacketAdded(seq_num, rtc::TimeMillis());
447 }
448}
449
450void AudioSendStream::OnPacketFeedbackVector(
451 const std::vector<PacketFeedback>& packet_feedback_vector) {
eladalon3651fdd2017-08-24 07:26:25 -0700452 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
Danil Chapovalovb9b146c2018-06-15 12:28:07 +0200453 absl::optional<float> plr;
454 absl::optional<float> rplr;
elad.alond12a8e12017-03-23 11:04:48 -0700455 {
456 rtc::CritScope lock(&packet_loss_tracker_cs_);
457 packet_loss_tracker_.OnPacketFeedbackVector(packet_feedback_vector);
458 plr = packet_loss_tracker_.GetPacketLossRate();
elad.alondadb4dc2017-03-23 15:29:50 -0700459 rplr = packet_loss_tracker_.GetRecoverablePacketLossRate();
elad.alond12a8e12017-03-23 11:04:48 -0700460 }
eladalonedd6eea2017-05-25 00:15:35 -0700461 // TODO(eladalon): If R/PLR go back to unknown, no indication is given that
elad.alond12a8e12017-03-23 11:04:48 -0700462 // the previously sent value is no longer relevant. This will be taken care
463 // of with some refactoring which is now being done.
464 if (plr) {
465 channel_proxy_->OnTwccBasedUplinkPacketLossRate(*plr);
466 }
elad.alondadb4dc2017-03-23 15:29:50 -0700467 if (rplr) {
468 channel_proxy_->OnRecoverableUplinkPacketLossRate(*rplr);
469 }
elad.alond12a8e12017-03-23 11:04:48 -0700470}
471
michaelt79e05882016-11-08 02:50:09 -0800472void AudioSendStream::SetTransportOverhead(int transport_overhead_per_packet) {
elad.alond12a8e12017-03-23 11:04:48 -0700473 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
michaelt79e05882016-11-08 02:50:09 -0800474 channel_proxy_->SetTransportOverhead(transport_overhead_per_packet);
475}
476
ossuc3d4b482017-05-23 06:07:11 -0700477RtpState AudioSendStream::GetRtpState() const {
478 return rtp_rtcp_module_->GetRtpState();
479}
480
Fredrik Solenberg8f5787a2018-01-11 13:52:30 +0100481const voe::ChannelProxy& AudioSendStream::GetChannelProxy() const {
482 RTC_DCHECK(channel_proxy_.get());
483 return *channel_proxy_.get();
484}
485
Fredrik Solenberg2a877972017-12-15 16:42:15 +0100486internal::AudioState* AudioSendStream::audio_state() {
487 internal::AudioState* audio_state =
488 static_cast<internal::AudioState*>(audio_state_.get());
489 RTC_DCHECK(audio_state);
490 return audio_state;
491}
492
493const internal::AudioState* AudioSendStream::audio_state() const {
494 internal::AudioState* audio_state =
495 static_cast<internal::AudioState*>(audio_state_.get());
496 RTC_DCHECK(audio_state);
497 return audio_state;
498}
499
Fredrik Solenberg2a877972017-12-15 16:42:15 +0100500void AudioSendStream::StoreEncoderProperties(int sample_rate_hz,
501 size_t num_channels) {
502 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
503 encoder_sample_rate_hz_ = sample_rate_hz;
504 encoder_num_channels_ = num_channels;
505 if (sending_) {
506 // Update AudioState's information about the stream.
507 audio_state()->AddSendingStream(this, sample_rate_hz, num_channels);
508 }
509}
510
minyue7a973442016-10-20 03:27:12 -0700511// Apply current codec settings to a single voe::Channel used for sending.
ossu20a4b3f2017-04-27 02:08:52 -0700512bool AudioSendStream::SetupSendCodec(AudioSendStream* stream,
513 const Config& new_config) {
514 RTC_DCHECK(new_config.send_codec_spec);
515 const auto& spec = *new_config.send_codec_spec;
minyue48368ad2017-05-10 04:06:11 -0700516
517 RTC_DCHECK(new_config.encoder_factory);
ossu20a4b3f2017-04-27 02:08:52 -0700518 std::unique_ptr<AudioEncoder> encoder =
Karl Wiberg77490b92018-03-21 15:18:42 +0100519 new_config.encoder_factory->MakeAudioEncoder(
520 spec.payload_type, spec.format, new_config.codec_pair_id);
minyue7a973442016-10-20 03:27:12 -0700521
ossu20a4b3f2017-04-27 02:08:52 -0700522 if (!encoder) {
Jonas Olssonabbe8412018-04-03 13:40:05 +0200523 RTC_DLOG(LS_ERROR) << "Unable to create encoder for "
524 << rtc::ToString(spec.format);
ossu20a4b3f2017-04-27 02:08:52 -0700525 return false;
526 }
Alex Narestbbbe4e12018-07-13 10:32:58 +0200527
528 // If other side does not support audio TWCC and WebRTC-Audio-ABWENoTWCC is
529 // not enabled, do not update target audio bitrate if we are in
530 // WebRTC-Audio-SendSideBwe-For-Video experiment
531 const bool do_not_update_target_bitrate =
532 !webrtc::field_trial::IsEnabled("WebRTC-Audio-ABWENoTWCC") &&
533 webrtc::field_trial::IsEnabled("WebRTC-Audio-SendSideBwe-For-Video") &&
534 !FindExtensionIds(new_config.rtp.extensions).transport_sequence_number;
ossu20a4b3f2017-04-27 02:08:52 -0700535 // If a bitrate has been specified for the codec, use it over the
536 // codec's default.
Alex Narestbbbe4e12018-07-13 10:32:58 +0200537 if (!do_not_update_target_bitrate && spec.target_bitrate_bps) {
ossu20a4b3f2017-04-27 02:08:52 -0700538 encoder->OnReceivedTargetAudioBitrate(*spec.target_bitrate_bps);
minyue7a973442016-10-20 03:27:12 -0700539 }
540
ossu20a4b3f2017-04-27 02:08:52 -0700541 // Enable ANA if configured (currently only used by Opus).
542 if (new_config.audio_network_adaptor_config) {
543 if (encoder->EnableAudioNetworkAdaptor(
544 *new_config.audio_network_adaptor_config, stream->event_log_)) {
Jonas Olsson24ea8222018-01-25 10:14:29 +0100545 RTC_DLOG(LS_INFO) << "Audio network adaptor enabled on SSRC "
546 << new_config.rtp.ssrc;
ossu20a4b3f2017-04-27 02:08:52 -0700547 } else {
548 RTC_NOTREACHED();
minyue6b825df2016-10-31 04:08:32 -0700549 }
minyue7a973442016-10-20 03:27:12 -0700550 }
551
ossu20a4b3f2017-04-27 02:08:52 -0700552 // Wrap the encoder in a an AudioEncoderCNG, if VAD is enabled.
553 if (spec.cng_payload_type) {
554 AudioEncoderCng::Config cng_config;
555 cng_config.num_channels = encoder->NumChannels();
556 cng_config.payload_type = *spec.cng_payload_type;
557 cng_config.speech_encoder = std::move(encoder);
558 cng_config.vad_mode = Vad::kVadNormal;
559 encoder.reset(new AudioEncoderCng(std::move(cng_config)));
ossu3b9ff382017-04-27 08:03:42 -0700560
561 stream->RegisterCngPayloadType(
562 *spec.cng_payload_type,
563 new_config.send_codec_spec->format.clockrate_hz);
minyue7a973442016-10-20 03:27:12 -0700564 }
ossu20a4b3f2017-04-27 02:08:52 -0700565
Fredrik Solenberg2a877972017-12-15 16:42:15 +0100566 stream->StoreEncoderProperties(encoder->SampleRateHz(),
567 encoder->NumChannels());
ossu20a4b3f2017-04-27 02:08:52 -0700568 stream->channel_proxy_->SetEncoder(new_config.send_codec_spec->payload_type,
569 std::move(encoder));
minyue7a973442016-10-20 03:27:12 -0700570 return true;
571}
572
ossu20a4b3f2017-04-27 02:08:52 -0700573bool AudioSendStream::ReconfigureSendCodec(AudioSendStream* stream,
574 const Config& new_config) {
575 const auto& old_config = stream->config_;
minyue-webrtc8de18262017-07-26 14:18:40 +0200576
577 if (!new_config.send_codec_spec) {
578 // We cannot de-configure a send codec. So we will do nothing.
579 // By design, the send codec should have not been configured.
580 RTC_DCHECK(!old_config.send_codec_spec);
581 return true;
582 }
583
584 if (new_config.send_codec_spec == old_config.send_codec_spec &&
585 new_config.audio_network_adaptor_config ==
586 old_config.audio_network_adaptor_config) {
ossu20a4b3f2017-04-27 02:08:52 -0700587 return true;
588 }
589
590 // If we have no encoder, or the format or payload type's changed, create a
591 // new encoder.
592 if (!old_config.send_codec_spec ||
593 new_config.send_codec_spec->format !=
594 old_config.send_codec_spec->format ||
595 new_config.send_codec_spec->payload_type !=
596 old_config.send_codec_spec->payload_type) {
597 return SetupSendCodec(stream, new_config);
598 }
599
Alex Narestbbbe4e12018-07-13 10:32:58 +0200600 // If other side does not support audio TWCC and WebRTC-Audio-ABWENoTWCC is
601 // not enabled, do not update target audio bitrate if we are in
602 // WebRTC-Audio-SendSideBwe-For-Video experiment
603 const bool do_not_update_target_bitrate =
604 !webrtc::field_trial::IsEnabled("WebRTC-Audio-ABWENoTWCC") &&
605 webrtc::field_trial::IsEnabled("WebRTC-Audio-SendSideBwe-For-Video") &&
606 !FindExtensionIds(new_config.rtp.extensions).transport_sequence_number;
607
Danil Chapovalovb9b146c2018-06-15 12:28:07 +0200608 const absl::optional<int>& new_target_bitrate_bps =
ossu20a4b3f2017-04-27 02:08:52 -0700609 new_config.send_codec_spec->target_bitrate_bps;
610 // If a bitrate has been specified for the codec, use it over the
611 // codec's default.
Alex Narestbbbe4e12018-07-13 10:32:58 +0200612 if (!do_not_update_target_bitrate && new_target_bitrate_bps &&
ossu20a4b3f2017-04-27 02:08:52 -0700613 new_target_bitrate_bps !=
614 old_config.send_codec_spec->target_bitrate_bps) {
615 CallEncoder(stream->channel_proxy_, [&](AudioEncoder* encoder) {
616 encoder->OnReceivedTargetAudioBitrate(*new_target_bitrate_bps);
617 });
618 }
619
620 ReconfigureANA(stream, new_config);
621 ReconfigureCNG(stream, new_config);
622
623 return true;
624}
625
626void AudioSendStream::ReconfigureANA(AudioSendStream* stream,
627 const Config& new_config) {
628 if (new_config.audio_network_adaptor_config ==
629 stream->config_.audio_network_adaptor_config) {
630 return;
631 }
632 if (new_config.audio_network_adaptor_config) {
633 CallEncoder(stream->channel_proxy_, [&](AudioEncoder* encoder) {
634 if (encoder->EnableAudioNetworkAdaptor(
635 *new_config.audio_network_adaptor_config, stream->event_log_)) {
Jonas Olsson24ea8222018-01-25 10:14:29 +0100636 RTC_DLOG(LS_INFO) << "Audio network adaptor enabled on SSRC "
637 << new_config.rtp.ssrc;
ossu20a4b3f2017-04-27 02:08:52 -0700638 } else {
639 RTC_NOTREACHED();
640 }
641 });
642 } else {
643 CallEncoder(stream->channel_proxy_, [&](AudioEncoder* encoder) {
644 encoder->DisableAudioNetworkAdaptor();
645 });
Jonas Olsson24ea8222018-01-25 10:14:29 +0100646 RTC_DLOG(LS_INFO) << "Audio network adaptor disabled on SSRC "
647 << new_config.rtp.ssrc;
ossu20a4b3f2017-04-27 02:08:52 -0700648 }
649}
650
651void AudioSendStream::ReconfigureCNG(AudioSendStream* stream,
652 const Config& new_config) {
653 if (new_config.send_codec_spec->cng_payload_type ==
654 stream->config_.send_codec_spec->cng_payload_type) {
655 return;
656 }
657
ossu3b9ff382017-04-27 08:03:42 -0700658 // Register the CNG payload type if it's been added, don't do anything if CNG
659 // is removed. Payload types must not be redefined.
660 if (new_config.send_codec_spec->cng_payload_type) {
661 stream->RegisterCngPayloadType(
662 *new_config.send_codec_spec->cng_payload_type,
663 new_config.send_codec_spec->format.clockrate_hz);
664 }
665
ossu20a4b3f2017-04-27 02:08:52 -0700666 // Wrap or unwrap the encoder in an AudioEncoderCNG.
667 stream->channel_proxy_->ModifyEncoder(
668 [&](std::unique_ptr<AudioEncoder>* encoder_ptr) {
669 std::unique_ptr<AudioEncoder> old_encoder(std::move(*encoder_ptr));
670 auto sub_encoders = old_encoder->ReclaimContainedEncoders();
671 if (!sub_encoders.empty()) {
672 // Replace enc with its sub encoder. We need to put the sub
673 // encoder in a temporary first, since otherwise the old value
674 // of enc would be destroyed before the new value got assigned,
675 // which would be bad since the new value is a part of the old
676 // value.
677 auto tmp = std::move(sub_encoders[0]);
678 old_encoder = std::move(tmp);
679 }
680 if (new_config.send_codec_spec->cng_payload_type) {
681 AudioEncoderCng::Config config;
682 config.speech_encoder = std::move(old_encoder);
683 config.num_channels = config.speech_encoder->NumChannels();
684 config.payload_type = *new_config.send_codec_spec->cng_payload_type;
685 config.vad_mode = Vad::kVadNormal;
686 encoder_ptr->reset(new AudioEncoderCng(std::move(config)));
687 } else {
688 *encoder_ptr = std::move(old_encoder);
689 }
690 });
691}
692
693void AudioSendStream::ReconfigureBitrateObserver(
694 AudioSendStream* stream,
695 const webrtc::AudioSendStream::Config& new_config) {
696 // Since the Config's default is for both of these to be -1, this test will
697 // allow us to configure the bitrate observer if the new config has bitrate
698 // limits set, but would only have us call RemoveBitrateObserver if we were
699 // previously configured with bitrate limits.
Alex Narestcedd3512017-12-07 20:54:55 +0100700 int new_transport_seq_num_id =
701 FindExtensionIds(new_config.rtp.extensions).transport_sequence_number;
ossu20a4b3f2017-04-27 02:08:52 -0700702 if (stream->config_.min_bitrate_bps == new_config.min_bitrate_bps &&
Alex Narestcedd3512017-12-07 20:54:55 +0100703 stream->config_.max_bitrate_bps == new_config.max_bitrate_bps &&
Seth Hampson24722b32017-12-22 09:36:42 -0800704 stream->config_.bitrate_priority == new_config.bitrate_priority &&
Alex Narestcedd3512017-12-07 20:54:55 +0100705 (FindExtensionIds(stream->config_.rtp.extensions)
706 .transport_sequence_number == new_transport_seq_num_id ||
707 !webrtc::field_trial::IsEnabled("WebRTC-Audio-SendSideBwe"))) {
ossu20a4b3f2017-04-27 02:08:52 -0700708 return;
709 }
710
Sebastian Jansson763e9472018-03-21 12:46:56 +0100711 bool has_transport_sequence_number = new_transport_seq_num_id != 0;
Alex Narestcedd3512017-12-07 20:54:55 +0100712 if (new_config.min_bitrate_bps != -1 && new_config.max_bitrate_bps != -1 &&
Sebastian Jansson763e9472018-03-21 12:46:56 +0100713 (has_transport_sequence_number ||
Alex Narestcedd3512017-12-07 20:54:55 +0100714 !webrtc::field_trial::IsEnabled("WebRTC-Audio-SendSideBwe"))) {
Sebastian Jansson763e9472018-03-21 12:46:56 +0100715 stream->ConfigureBitrateObserver(
716 new_config.min_bitrate_bps, new_config.max_bitrate_bps,
717 new_config.bitrate_priority, has_transport_sequence_number);
ossu20a4b3f2017-04-27 02:08:52 -0700718 } else {
719 stream->RemoveBitrateObserver();
720 }
721}
722
723void AudioSendStream::ConfigureBitrateObserver(int min_bitrate_bps,
Seth Hampson24722b32017-12-22 09:36:42 -0800724 int max_bitrate_bps,
Sebastian Jansson763e9472018-03-21 12:46:56 +0100725 double bitrate_priority,
726 bool has_packet_feedback) {
ossu20a4b3f2017-04-27 02:08:52 -0700727 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
728 RTC_DCHECK_GE(max_bitrate_bps, min_bitrate_bps);
729 rtc::Event thread_sync_event(false /* manual_reset */, false);
730 worker_queue_->PostTask([&] {
731 // We may get a callback immediately as the observer is registered, so make
732 // sure the bitrate limits in config_ are up-to-date.
733 config_.min_bitrate_bps = min_bitrate_bps;
734 config_.max_bitrate_bps = max_bitrate_bps;
Seth Hampson24722b32017-12-22 09:36:42 -0800735 config_.bitrate_priority = bitrate_priority;
736 // This either updates the current observer or adds a new observer.
Sebastian Jansson24ad7202018-04-19 08:25:12 +0200737 bitrate_allocator_->AddObserver(
738 this, MediaStreamAllocationConfig{
739 static_cast<uint32_t>(min_bitrate_bps),
740 static_cast<uint32_t>(max_bitrate_bps), 0, true,
741 config_.track_id, bitrate_priority, has_packet_feedback});
ossu20a4b3f2017-04-27 02:08:52 -0700742 thread_sync_event.Set();
743 });
744 thread_sync_event.Wait(rtc::Event::kForever);
745}
746
747void AudioSendStream::RemoveBitrateObserver() {
748 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
749 rtc::Event thread_sync_event(false /* manual_reset */, false);
750 worker_queue_->PostTask([this, &thread_sync_event] {
751 bitrate_allocator_->RemoveObserver(this);
752 thread_sync_event.Set();
753 });
754 thread_sync_event.Wait(rtc::Event::kForever);
755}
756
ossu3b9ff382017-04-27 08:03:42 -0700757void AudioSendStream::RegisterCngPayloadType(int payload_type,
758 int clockrate_hz) {
ossu3b9ff382017-04-27 08:03:42 -0700759 const CodecInst codec = {payload_type, "CN", clockrate_hz, 0, 1, 0};
ossuc3d4b482017-05-23 06:07:11 -0700760 if (rtp_rtcp_module_->RegisterSendPayload(codec) != 0) {
761 rtp_rtcp_module_->DeRegisterSendPayload(codec.pltype);
762 if (rtp_rtcp_module_->RegisterSendPayload(codec) != 0) {
Jonas Olsson24ea8222018-01-25 10:14:29 +0100763 RTC_DLOG(LS_ERROR) << "RegisterCngPayloadType() failed to register CN to "
764 "RTP/RTCP module";
ossu3b9ff382017-04-27 08:03:42 -0700765 }
766 }
767}
solenbergc7a8b082015-10-16 14:35:07 -0700768} // namespace internal
769} // namespace webrtc