blob: edd91218ba6f7b0bbcf472781ce4017e19b0f7a9 [file] [log] [blame]
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001/*
kjellander1afca732016-02-07 20:46:45 -08002 * Copyright (c) 2004 The WebRTC project authors. All Rights Reserved.
henrike@webrtc.org28e20752013-07-10 00:45:36 +00003 *
kjellander1afca732016-02-07 20:46:45 -08004 * 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.
henrike@webrtc.org28e20752013-07-10 00:45:36 +00009 */
10
henrike@webrtc.org28e20752013-07-10 00:45:36 +000011#ifdef HAVE_WEBRTC_VOICE
12
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020013#include "media/engine/webrtcvoiceengine.h"
henrike@webrtc.org28e20752013-07-10 00:45:36 +000014
15#include <algorithm>
16#include <cstdio>
ossuc54071d2016-08-17 02:45:41 -070017#include <functional>
henrike@webrtc.org28e20752013-07-10 00:45:36 +000018#include <string>
Steve Antone78bcb92017-10-31 09:53:08 -070019#include <utility>
henrike@webrtc.org28e20752013-07-10 00:45:36 +000020#include <vector>
21
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020022#include "api/call/audio_sink.h"
23#include "media/base/audiosource.h"
24#include "media/base/mediaconstants.h"
25#include "media/base/streamparams.h"
26#include "media/engine/adm_helpers.h"
27#include "media/engine/apm_helpers.h"
28#include "media/engine/payload_type_mapper.h"
29#include "media/engine/webrtcmediaengine.h"
Fredrik Solenbergd3195342017-11-21 20:33:05 +010030#include "modules/audio_device/audio_device_impl.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020031#include "modules/audio_mixer/audio_mixer_impl.h"
32#include "modules/audio_processing/aec_dump/aec_dump_factory.h"
33#include "modules/audio_processing/include/audio_processing.h"
34#include "rtc_base/arraysize.h"
35#include "rtc_base/base64.h"
36#include "rtc_base/byteorder.h"
37#include "rtc_base/constructormagic.h"
38#include "rtc_base/helpers.h"
39#include "rtc_base/logging.h"
40#include "rtc_base/race_checker.h"
41#include "rtc_base/stringencode.h"
42#include "rtc_base/stringutils.h"
43#include "rtc_base/trace_event.h"
44#include "system_wrappers/include/field_trial.h"
45#include "system_wrappers/include/metrics.h"
henrike@webrtc.org28e20752013-07-10 00:45:36 +000046
henrike@webrtc.org28e20752013-07-10 00:45:36 +000047namespace cricket {
solenbergd97ec302015-10-07 01:40:33 -070048namespace {
henrike@webrtc.org28e20752013-07-10 00:45:36 +000049
solenberg418b7d32017-06-13 00:38:27 -070050constexpr size_t kMaxUnsignaledRecvStreams = 4;
solenberg2100c0b2017-03-01 11:29:29 -080051
solenberg971cab02016-06-14 10:02:41 -070052constexpr int kNackRtpHistoryMs = 5000;
minyue@webrtc.org2dc6f312014-10-31 05:33:10 +000053
peah1bcfce52016-08-26 07:16:04 -070054// Check to verify that the define for the intelligibility enhancer is properly
55// set.
56#if !defined(WEBRTC_INTELLIGIBILITY_ENHANCER) || \
57 (WEBRTC_INTELLIGIBILITY_ENHANCER != 0 && \
58 WEBRTC_INTELLIGIBILITY_ENHANCER != 1)
59#error "Set WEBRTC_INTELLIGIBILITY_ENHANCER to either 0 or 1"
60#endif
61
ossu20a4b3f2017-04-27 02:08:52 -070062// For SendSideBwe, Opus bitrate should be in the range between 6000 and 32000.
minyue10cbb462016-11-07 09:29:22 -080063const int kOpusMinBitrateBps = 6000;
ossu20a4b3f2017-04-27 02:08:52 -070064const int kOpusBitrateFbBps = 32000;
deadbeef80346142016-04-27 14:17:10 -070065
wu@webrtc.orgde305012013-10-31 15:40:38 +000066// Default audio dscp value.
67// See http://tools.ietf.org/html/rfc2474 for details.
68// See also http://tools.ietf.org/html/draft-jennings-rtcweb-qos-00
solenbergd97ec302015-10-07 01:40:33 -070069const rtc::DiffServCodePoint kAudioDscpValue = rtc::DSCP_EF;
henrike@webrtc.org1e09a712013-07-26 19:17:59 +000070
Fredrik Solenbergb5727682015-12-04 15:22:19 +010071const int kMinTelephoneEventCode = 0; // RFC4733 (Section 2.3.1)
72const int kMaxTelephoneEventCode = 255;
Fredrik Solenbergb5727682015-12-04 15:22:19 +010073
solenberg31642aa2016-03-14 08:00:37 -070074const int kMinPayloadType = 0;
75const int kMaxPayloadType = 127;
76
deadbeef884f5852016-01-15 09:20:04 -080077class ProxySink : public webrtc::AudioSinkInterface {
78 public:
Steve Antone78bcb92017-10-31 09:53:08 -070079 explicit ProxySink(AudioSinkInterface* sink) : sink_(sink) {
80 RTC_DCHECK(sink);
81 }
deadbeef884f5852016-01-15 09:20:04 -080082
83 void OnData(const Data& audio) override { sink_->OnData(audio); }
84
85 private:
86 webrtc::AudioSinkInterface* sink_;
87};
88
solenberg0b675462015-10-09 01:37:09 -070089bool ValidateStreamParams(const StreamParams& sp) {
90 if (sp.ssrcs.empty()) {
Mirko Bonadei675513b2017-11-09 11:09:25 +010091 RTC_LOG(LS_ERROR) << "No SSRCs in stream parameters: " << sp.ToString();
solenberg0b675462015-10-09 01:37:09 -070092 return false;
93 }
94 if (sp.ssrcs.size() > 1) {
Mirko Bonadei675513b2017-11-09 11:09:25 +010095 RTC_LOG(LS_ERROR) << "Multiple SSRCs in stream parameters: "
96 << sp.ToString();
solenberg0b675462015-10-09 01:37:09 -070097 return false;
98 }
99 return true;
100}
101
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000102// Dumps an AudioCodec in RFC 2327-ish format.
solenbergd97ec302015-10-07 01:40:33 -0700103std::string ToString(const AudioCodec& codec) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000104 std::stringstream ss;
ossu20a4b3f2017-04-27 02:08:52 -0700105 ss << codec.name << "/" << codec.clockrate << "/" << codec.channels;
106 if (!codec.params.empty()) {
107 ss << " {";
108 for (const auto& param : codec.params) {
109 ss << " " << param.first << "=" << param.second;
110 }
111 ss << " }";
112 }
113 ss << " (" << codec.id << ")";
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000114 return ss.str();
115}
Minyue Li7100dcd2015-03-27 05:05:59 +0100116
solenbergd97ec302015-10-07 01:40:33 -0700117bool IsCodec(const AudioCodec& codec, const char* ref_name) {
Minyue Li7100dcd2015-03-27 05:05:59 +0100118 return (_stricmp(codec.name.c_str(), ref_name) == 0);
119}
120
solenbergd97ec302015-10-07 01:40:33 -0700121bool FindCodec(const std::vector<AudioCodec>& codecs,
solenberg26c8c912015-11-27 04:00:25 -0800122 const AudioCodec& codec,
123 AudioCodec* found_codec) {
Fredrik Solenbergaf9fb212015-08-26 10:45:53 +0200124 for (const AudioCodec& c : codecs) {
125 if (c.Matches(codec)) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000126 if (found_codec != NULL) {
Fredrik Solenbergaf9fb212015-08-26 10:45:53 +0200127 *found_codec = c;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000128 }
129 return true;
130 }
131 }
132 return false;
133}
wu@webrtc.org1d1ffc92013-10-16 18:12:02 +0000134
solenberg0b675462015-10-09 01:37:09 -0700135bool VerifyUniquePayloadTypes(const std::vector<AudioCodec>& codecs) {
136 if (codecs.empty()) {
137 return true;
138 }
139 std::vector<int> payload_types;
140 for (const AudioCodec& codec : codecs) {
141 payload_types.push_back(codec.id);
142 }
143 std::sort(payload_types.begin(), payload_types.end());
144 auto it = std::unique(payload_types.begin(), payload_types.end());
145 return it == payload_types.end();
146}
147
minyue6b825df2016-10-31 04:08:32 -0700148rtc::Optional<std::string> GetAudioNetworkAdaptorConfig(
149 const AudioOptions& options) {
150 if (options.audio_network_adaptor && *options.audio_network_adaptor &&
151 options.audio_network_adaptor_config) {
152 // Turn on audio network adaptor only when |options_.audio_network_adaptor|
153 // equals true and |options_.audio_network_adaptor_config| has a value.
154 return options.audio_network_adaptor_config;
155 }
Oskar Sundbom78807582017-11-16 11:09:55 +0100156 return rtc::nullopt;
minyue6b825df2016-10-31 04:08:32 -0700157}
158
deadbeefe702b302017-02-04 12:09:01 -0800159// |max_send_bitrate_bps| is the bitrate from "b=" in SDP.
160// |rtp_max_bitrate_bps| is the bitrate from RtpSender::SetParameters.
minyue7a973442016-10-20 03:27:12 -0700161rtc::Optional<int> ComputeSendBitrate(int max_send_bitrate_bps,
deadbeefe702b302017-02-04 12:09:01 -0800162 rtc::Optional<int> rtp_max_bitrate_bps,
ossu20a4b3f2017-04-27 02:08:52 -0700163 const webrtc::AudioCodecSpec& spec) {
deadbeefe702b302017-02-04 12:09:01 -0800164 // If application-configured bitrate is set, take minimum of that and SDP
165 // bitrate.
zsteina5e0df62017-06-14 11:41:48 -0700166 const int bps =
167 rtp_max_bitrate_bps
168 ? webrtc::MinPositive(max_send_bitrate_bps, *rtp_max_bitrate_bps)
169 : max_send_bitrate_bps;
minyue7a973442016-10-20 03:27:12 -0700170 if (bps <= 0) {
Oskar Sundbom78807582017-11-16 11:09:55 +0100171 return spec.info.default_bitrate_bps;
solenberg971cab02016-06-14 10:02:41 -0700172 }
minyue7a973442016-10-20 03:27:12 -0700173
ossu20a4b3f2017-04-27 02:08:52 -0700174 if (bps < spec.info.min_bitrate_bps) {
minyue7a973442016-10-20 03:27:12 -0700175 // If codec is not multi-rate and |bps| is less than the fixed bitrate then
176 // fail. If codec is not multi-rate and |bps| exceeds or equal the fixed
177 // bitrate then ignore.
Mirko Bonadei675513b2017-11-09 11:09:25 +0100178 RTC_LOG(LS_ERROR) << "Failed to set codec " << spec.format.name
179 << " to bitrate " << bps << " bps"
180 << ", requires at least " << spec.info.min_bitrate_bps
181 << " bps.";
Oskar Sundbom78807582017-11-16 11:09:55 +0100182 return rtc::nullopt;
solenberg971cab02016-06-14 10:02:41 -0700183 }
ossu20a4b3f2017-04-27 02:08:52 -0700184
185 if (spec.info.HasFixedBitrate()) {
Oskar Sundbom78807582017-11-16 11:09:55 +0100186 return spec.info.default_bitrate_bps;
ossu20a4b3f2017-04-27 02:08:52 -0700187 } else {
188 // If codec is multi-rate then just set the bitrate.
Oskar Sundbom78807582017-11-16 11:09:55 +0100189 return std::min(bps, spec.info.max_bitrate_bps);
ossu20a4b3f2017-04-27 02:08:52 -0700190 }
solenberg971cab02016-06-14 10:02:41 -0700191}
192
solenberg76377c52017-02-21 00:54:31 -0800193} // namespace
solenberg971cab02016-06-14 10:02:41 -0700194
ossu29b1a8d2016-06-13 07:34:51 -0700195WebRtcVoiceEngine::WebRtcVoiceEngine(
196 webrtc::AudioDeviceModule* adm,
ossueb1fde42017-05-02 06:46:30 -0700197 const rtc::scoped_refptr<webrtc::AudioEncoderFactory>& encoder_factory,
gyzhou95aa9642016-12-13 14:06:26 -0800198 const rtc::scoped_refptr<webrtc::AudioDecoderFactory>& decoder_factory,
peaha9cc40b2017-06-29 08:32:09 -0700199 rtc::scoped_refptr<webrtc::AudioMixer> audio_mixer,
200 rtc::scoped_refptr<webrtc::AudioProcessing> audio_processing)
deadbeefeb02c032017-06-15 08:29:25 -0700201 : adm_(adm),
ossueb1fde42017-05-02 06:46:30 -0700202 encoder_factory_(encoder_factory),
ossu20a4b3f2017-04-27 02:08:52 -0700203 decoder_factory_(decoder_factory),
deadbeefeb02c032017-06-15 08:29:25 -0700204 audio_mixer_(audio_mixer),
Fredrik Solenberg8f5787a2018-01-11 13:52:30 +0100205 apm_(audio_processing) {
deadbeefeb02c032017-06-15 08:29:25 -0700206 // This may be called from any thread, so detach thread checkers.
207 worker_thread_checker_.DetachFromThread();
solenberg26c8c912015-11-27 04:00:25 -0800208 signal_thread_checker_.DetachFromThread();
Mirko Bonadei675513b2017-11-09 11:09:25 +0100209 RTC_LOG(LS_INFO) << "WebRtcVoiceEngine::WebRtcVoiceEngine";
deadbeefeb02c032017-06-15 08:29:25 -0700210 RTC_DCHECK(decoder_factory);
211 RTC_DCHECK(encoder_factory);
peaha9cc40b2017-06-29 08:32:09 -0700212 RTC_DCHECK(audio_processing);
deadbeefeb02c032017-06-15 08:29:25 -0700213 // The rest of our initialization will happen in Init.
214}
215
216WebRtcVoiceEngine::~WebRtcVoiceEngine() {
217 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
Mirko Bonadei675513b2017-11-09 11:09:25 +0100218 RTC_LOG(LS_INFO) << "WebRtcVoiceEngine::~WebRtcVoiceEngine";
deadbeefeb02c032017-06-15 08:29:25 -0700219 if (initialized_) {
220 StopAecDump();
Fredrik Solenbergd3195342017-11-21 20:33:05 +0100221
222 // Stop AudioDevice.
223 adm()->StopPlayout();
224 adm()->StopRecording();
225 adm()->RegisterAudioCallback(nullptr);
226 adm()->Terminate();
deadbeefeb02c032017-06-15 08:29:25 -0700227 }
228}
229
230void WebRtcVoiceEngine::Init() {
231 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
Mirko Bonadei675513b2017-11-09 11:09:25 +0100232 RTC_LOG(LS_INFO) << "WebRtcVoiceEngine::Init";
deadbeefeb02c032017-06-15 08:29:25 -0700233
234 // TaskQueue expects to be created/destroyed on the same thread.
235 low_priority_worker_queue_.reset(
236 new rtc::TaskQueue("rtc-low-prio", rtc::TaskQueue::Priority::LOW));
237
ossueb1fde42017-05-02 06:46:30 -0700238 // Load our audio codec lists.
Mirko Bonadei675513b2017-11-09 11:09:25 +0100239 RTC_LOG(LS_INFO) << "Supported send codecs in order of preference:";
ossu20a4b3f2017-04-27 02:08:52 -0700240 send_codecs_ = CollectCodecs(encoder_factory_->GetSupportedEncoders());
ossuc54071d2016-08-17 02:45:41 -0700241 for (const AudioCodec& codec : send_codecs_) {
Mirko Bonadei675513b2017-11-09 11:09:25 +0100242 RTC_LOG(LS_INFO) << ToString(codec);
ossuc54071d2016-08-17 02:45:41 -0700243 }
244
Mirko Bonadei675513b2017-11-09 11:09:25 +0100245 RTC_LOG(LS_INFO) << "Supported recv codecs in order of preference:";
ossu20a4b3f2017-04-27 02:08:52 -0700246 recv_codecs_ = CollectCodecs(decoder_factory_->GetSupportedDecoders());
ossuc54071d2016-08-17 02:45:41 -0700247 for (const AudioCodec& codec : recv_codecs_) {
Mirko Bonadei675513b2017-11-09 11:09:25 +0100248 RTC_LOG(LS_INFO) << ToString(codec);
buildbot@webrtc.org13d67762014-05-02 17:33:29 +0000249 }
buildbot@webrtc.org13d67762014-05-02 17:33:29 +0000250
Fredrik Solenbergd3195342017-11-21 20:33:05 +0100251#if defined(WEBRTC_INCLUDE_INTERNAL_AUDIO_DEVICE)
252 // No ADM supplied? Create a default one.
solenbergff976312016-03-30 23:28:51 -0700253 if (!adm_) {
Fredrik Solenbergd3195342017-11-21 20:33:05 +0100254 adm_ = webrtc::AudioDeviceModule::Create(
255 webrtc::AudioDeviceModule::kPlatformDefaultAudio);
solenbergff976312016-03-30 23:28:51 -0700256 }
Fredrik Solenbergd3195342017-11-21 20:33:05 +0100257#endif // WEBRTC_INCLUDE_INTERNAL_AUDIO_DEVICE
258 RTC_CHECK(adm());
259 webrtc::adm_helpers::Init(adm());
Fredrik Solenberg55900fd2017-11-23 20:22:55 +0100260 webrtc::apm_helpers::Init(apm());
Fredrik Solenberg2a877972017-12-15 16:42:15 +0100261
262 // Set up AudioState.
263 {
264 webrtc::AudioState::Config config;
Fredrik Solenberg2a877972017-12-15 16:42:15 +0100265 if (audio_mixer_) {
266 config.audio_mixer = audio_mixer_;
267 } else {
268 config.audio_mixer = webrtc::AudioMixerImpl::Create();
269 }
270 config.audio_processing = apm_;
271 config.audio_device_module = adm_;
272 audio_state_ = webrtc::AudioState::Create(config);
273 }
274
275 // Connect the ADM to our audio path.
276 adm()->RegisterAudioCallback(audio_state()->audio_transport());
solenberg76377c52017-02-21 00:54:31 -0800277
buildbot@webrtc.org13d67762014-05-02 17:33:29 +0000278 // Save the default AGC configuration settings. This must happen before
solenberg246b8172015-12-08 09:50:23 -0800279 // calling ApplyOptions or the default will be overwritten.
peaha9cc40b2017-06-29 08:32:09 -0700280 default_agc_config_ = webrtc::apm_helpers::GetAgcConfig(apm());
buildbot@webrtc.org13d67762014-05-02 17:33:29 +0000281
solenberg0f7d2932016-01-15 01:40:39 -0800282 // Set default engine options.
283 {
284 AudioOptions options;
Oskar Sundbom78807582017-11-16 11:09:55 +0100285 options.echo_cancellation = true;
286 options.auto_gain_control = true;
287 options.noise_suppression = true;
288 options.highpass_filter = true;
289 options.stereo_swapping = false;
290 options.audio_jitter_buffer_max_packets = 50;
291 options.audio_jitter_buffer_fast_accelerate = false;
292 options.typing_detection = true;
Oskar Sundbom78807582017-11-16 11:09:55 +0100293 options.experimental_agc = false;
294 options.extended_filter_aec = false;
295 options.delay_agnostic_aec = false;
296 options.experimental_ns = false;
297 options.intelligibility_enhancer = false;
298 options.level_control = false;
299 options.residual_echo_detector = true;
solenbergff976312016-03-30 23:28:51 -0700300 bool error = ApplyOptions(options);
301 RTC_DCHECK(error);
buildbot@webrtc.org13d67762014-05-02 17:33:29 +0000302 }
303
deadbeefeb02c032017-06-15 08:29:25 -0700304 initialized_ = true;
buildbot@webrtc.org13d67762014-05-02 17:33:29 +0000305}
306
solenberg566ef242015-11-06 15:34:49 -0800307rtc::scoped_refptr<webrtc::AudioState>
308 WebRtcVoiceEngine::GetAudioState() const {
309 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
310 return audio_state_;
311}
312
nisse51542be2016-02-12 02:27:06 -0800313VoiceMediaChannel* WebRtcVoiceEngine::CreateChannel(
314 webrtc::Call* call,
315 const MediaConfig& config,
Jelena Marusicc28a8962015-05-29 15:05:44 +0200316 const AudioOptions& options) {
solenberg566ef242015-11-06 15:34:49 -0800317 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
nisse51542be2016-02-12 02:27:06 -0800318 return new WebRtcVoiceMediaChannel(this, config, options, call);
buildbot@webrtc.org13d67762014-05-02 17:33:29 +0000319}
320
buildbot@webrtc.org13d67762014-05-02 17:33:29 +0000321bool WebRtcVoiceEngine::ApplyOptions(const AudioOptions& options_in) {
solenberg566ef242015-11-06 15:34:49 -0800322 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
Mirko Bonadei675513b2017-11-09 11:09:25 +0100323 RTC_LOG(LS_INFO) << "WebRtcVoiceEngine::ApplyOptions: "
324 << options_in.ToString();
solenberg0f7d2932016-01-15 01:40:39 -0800325 AudioOptions options = options_in; // The options are modified below.
solenberg246b8172015-12-08 09:50:23 -0800326
peah8a8ebd92017-05-22 15:48:47 -0700327 // Set and adjust echo canceller options.
buildbot@webrtc.org13d67762014-05-02 17:33:29 +0000328 // kEcConference is AEC with high suppression.
329 webrtc::EcModes ec_mode = webrtc::kEcConference;
kwiberg102c6a62015-10-30 02:47:38 -0700330 if (options.aecm_generate_comfort_noise) {
Mirko Bonadei675513b2017-11-09 11:09:25 +0100331 RTC_LOG(LS_VERBOSE) << "Comfort noise explicitly set to "
332 << *options.aecm_generate_comfort_noise
333 << " (default is false).";
buildbot@webrtc.org13d67762014-05-02 17:33:29 +0000334 }
335
kjellanderfcfc8042016-01-14 11:01:09 -0800336#if defined(WEBRTC_IOS)
Jonathan Yu76220482017-12-21 04:18:07 -0800337 if (options.ios_force_software_aec_HACK &&
338 *options.ios_force_software_aec_HACK) {
339 // EC may be forced on for a device known to have non-functioning platform
340 // AEC.
341 options.echo_cancellation = true;
342 options.extended_filter_aec = true;
343 RTC_LOG(LS_WARNING)
344 << "Force software AEC on iOS. May conflict with platform AEC.";
345 } else {
346 // On iOS, VPIO provides built-in EC.
347 options.echo_cancellation = false;
348 options.extended_filter_aec = false;
349 RTC_LOG(LS_INFO) << "Always disable AEC on iOS. Use built-in instead.";
350 }
Mirko Bonadeic8c71b92017-10-16 11:08:54 +0200351#elif defined(WEBRTC_ANDROID)
buildbot@webrtc.org13d67762014-05-02 17:33:29 +0000352 ec_mode = webrtc::kEcAecm;
Oskar Sundbom78807582017-11-16 11:09:55 +0100353 options.extended_filter_aec = false;
buildbot@webrtc.org13d67762014-05-02 17:33:29 +0000354#endif
355
Bjorn Volckerbf395c12015-03-25 22:45:56 +0100356 // Delay Agnostic AEC automatically turns on EC if not set except on iOS
357 // where the feature is not supported.
358 bool use_delay_agnostic_aec = false;
kjellanderfcfc8042016-01-14 11:01:09 -0800359#if !defined(WEBRTC_IOS)
kwiberg102c6a62015-10-30 02:47:38 -0700360 if (options.delay_agnostic_aec) {
361 use_delay_agnostic_aec = *options.delay_agnostic_aec;
Bjorn Volckerbf395c12015-03-25 22:45:56 +0100362 if (use_delay_agnostic_aec) {
Oskar Sundbom78807582017-11-16 11:09:55 +0100363 options.echo_cancellation = true;
364 options.extended_filter_aec = true;
Bjorn Volckerbf395c12015-03-25 22:45:56 +0100365 ec_mode = webrtc::kEcConference;
366 }
367 }
368#endif
369
peah8a8ebd92017-05-22 15:48:47 -0700370// Set and adjust noise suppressor options.
371#if defined(WEBRTC_IOS)
372 // On iOS, VPIO provides built-in NS.
Oskar Sundbom78807582017-11-16 11:09:55 +0100373 options.noise_suppression = false;
374 options.typing_detection = false;
375 options.experimental_ns = false;
Mirko Bonadei675513b2017-11-09 11:09:25 +0100376 RTC_LOG(LS_INFO) << "Always disable NS on iOS. Use built-in instead.";
Mirko Bonadeic8c71b92017-10-16 11:08:54 +0200377#elif defined(WEBRTC_ANDROID)
Oskar Sundbom78807582017-11-16 11:09:55 +0100378 options.typing_detection = false;
379 options.experimental_ns = false;
peah8a8ebd92017-05-22 15:48:47 -0700380#endif
381
382// Set and adjust gain control options.
383#if defined(WEBRTC_IOS)
384 // On iOS, VPIO provides built-in AGC.
Oskar Sundbom78807582017-11-16 11:09:55 +0100385 options.auto_gain_control = false;
386 options.experimental_agc = false;
Mirko Bonadei675513b2017-11-09 11:09:25 +0100387 RTC_LOG(LS_INFO) << "Always disable AGC on iOS. Use built-in instead.";
Mirko Bonadeic8c71b92017-10-16 11:08:54 +0200388#elif defined(WEBRTC_ANDROID)
Oskar Sundbom78807582017-11-16 11:09:55 +0100389 options.experimental_agc = false;
peah8a8ebd92017-05-22 15:48:47 -0700390#endif
391
392#if defined(WEBRTC_IOS) || defined(WEBRTC_ANDROID)
Mirko Bonadeic8c71b92017-10-16 11:08:54 +0200393 // Turn off the gain control if specified by the field trial.
394 // The purpose of the field trial is to reduce the amount of resampling
395 // performed inside the audio processing module on mobile platforms by
396 // whenever possible turning off the fixed AGC mode and the high-pass filter.
397 // (https://bugs.chromium.org/p/webrtc/issues/detail?id=6181).
peah8a8ebd92017-05-22 15:48:47 -0700398 if (webrtc::field_trial::IsEnabled(
399 "WebRTC-Audio-MinimizeResamplingOnMobile")) {
Oskar Sundbom78807582017-11-16 11:09:55 +0100400 options.auto_gain_control = false;
Mirko Bonadei675513b2017-11-09 11:09:25 +0100401 RTC_LOG(LS_INFO) << "Disable AGC according to field trial.";
Steve Antone78bcb92017-10-31 09:53:08 -0700402 if (!(options.noise_suppression.value_or(false) ||
peah8a8ebd92017-05-22 15:48:47 -0700403 options.echo_cancellation.value_or(false))) {
404 // If possible, turn off the high-pass filter.
Mirko Bonadei675513b2017-11-09 11:09:25 +0100405 RTC_LOG(LS_INFO)
406 << "Disable high-pass filter in response to field trial.";
Oskar Sundbom78807582017-11-16 11:09:55 +0100407 options.highpass_filter = false;
peah8a8ebd92017-05-22 15:48:47 -0700408 }
409 }
410#endif
411
peah1bcfce52016-08-26 07:16:04 -0700412#if (WEBRTC_INTELLIGIBILITY_ENHANCER == 0)
413 // Hardcode the intelligibility enhancer to be off.
Oskar Sundbom78807582017-11-16 11:09:55 +0100414 options.intelligibility_enhancer = false;
peah1bcfce52016-08-26 07:16:04 -0700415#endif
416
kwiberg102c6a62015-10-30 02:47:38 -0700417 if (options.echo_cancellation) {
henrika@webrtc.orga954c072014-12-09 16:22:09 +0000418 // Check if platform supports built-in EC. Currently only supported on
419 // Android and in combination with Java based audio layer.
420 // TODO(henrika): investigate possibility to support built-in EC also
421 // in combination with Open SL ES audio.
solenberg5b5129a2016-04-08 05:35:48 -0700422 const bool built_in_aec = adm()->BuiltInAECIsAvailable();
Bjorn Volcker73f72102015-06-03 14:50:15 +0200423 if (built_in_aec) {
Bjorn Volckerccfc9392015-05-07 07:43:17 +0200424 // Built-in EC exists on this device and use_delay_agnostic_aec is not
425 // overriding it. Enable/Disable it according to the echo_cancellation
426 // audio option.
Bjorn Volcker73f72102015-06-03 14:50:15 +0200427 const bool enable_built_in_aec =
kwiberg102c6a62015-10-30 02:47:38 -0700428 *options.echo_cancellation && !use_delay_agnostic_aec;
solenberg5b5129a2016-04-08 05:35:48 -0700429 if (adm()->EnableBuiltInAEC(enable_built_in_aec) == 0 &&
Bjorn Volcker73f72102015-06-03 14:50:15 +0200430 enable_built_in_aec) {
Bjorn Volckerbf395c12015-03-25 22:45:56 +0100431 // Disable internal software EC if built-in EC is enabled,
henrika@webrtc.orga954c072014-12-09 16:22:09 +0000432 // i.e., replace the software EC with the built-in EC.
Oskar Sundbom78807582017-11-16 11:09:55 +0100433 options.echo_cancellation = false;
Mirko Bonadei675513b2017-11-09 11:09:25 +0100434 RTC_LOG(LS_INFO)
435 << "Disabling EC since built-in EC will be used instead";
henrika@webrtc.orga954c072014-12-09 16:22:09 +0000436 }
437 }
solenberg76377c52017-02-21 00:54:31 -0800438 webrtc::apm_helpers::SetEcStatus(
439 apm(), *options.echo_cancellation, ec_mode);
Mirko Bonadeic8c71b92017-10-16 11:08:54 +0200440#if !defined(WEBRTC_ANDROID)
solenberg76377c52017-02-21 00:54:31 -0800441 webrtc::apm_helpers::SetEcMetricsStatus(apm(), *options.echo_cancellation);
buildbot@webrtc.org13d67762014-05-02 17:33:29 +0000442#endif
443 if (ec_mode == webrtc::kEcAecm) {
kwiberg102c6a62015-10-30 02:47:38 -0700444 bool cn = options.aecm_generate_comfort_noise.value_or(false);
solenberg76377c52017-02-21 00:54:31 -0800445 webrtc::apm_helpers::SetAecmMode(apm(), cn);
buildbot@webrtc.org13d67762014-05-02 17:33:29 +0000446 }
447 }
448
kwiberg102c6a62015-10-30 02:47:38 -0700449 if (options.auto_gain_control) {
peah72a56452016-08-22 12:08:55 -0700450 bool built_in_agc_avaliable = adm()->BuiltInAGCIsAvailable();
451 if (built_in_agc_avaliable) {
solenberg5b5129a2016-04-08 05:35:48 -0700452 if (adm()->EnableBuiltInAGC(*options.auto_gain_control) == 0 &&
kwiberg102c6a62015-10-30 02:47:38 -0700453 *options.auto_gain_control) {
henrikac14f5ff2015-09-23 14:08:33 +0200454 // Disable internal software AGC if built-in AGC is enabled,
455 // i.e., replace the software AGC with the built-in AGC.
Oskar Sundbom78807582017-11-16 11:09:55 +0100456 options.auto_gain_control = false;
Mirko Bonadei675513b2017-11-09 11:09:25 +0100457 RTC_LOG(LS_INFO)
458 << "Disabling AGC since built-in AGC will be used instead";
henrikac14f5ff2015-09-23 14:08:33 +0200459 }
460 }
henrikae26456a2017-12-13 14:08:48 +0100461 webrtc::apm_helpers::SetAgcStatus(apm(), *options.auto_gain_control);
buildbot@webrtc.org13d67762014-05-02 17:33:29 +0000462 }
463
kwiberg102c6a62015-10-30 02:47:38 -0700464 if (options.tx_agc_target_dbov || options.tx_agc_digital_compression_gain ||
Steve Anton606a5972017-12-07 14:31:01 -0800465 options.tx_agc_limiter) {
buildbot@webrtc.org13d67762014-05-02 17:33:29 +0000466 // Override default_agc_config_. Generally, an unset option means "leave
467 // the VoE bits alone" in this function, so we want whatever is set to be
468 // stored as the new "default". If we didn't, then setting e.g.
469 // tx_agc_target_dbov would reset digital compression gain and limiter
470 // settings.
kwiberg102c6a62015-10-30 02:47:38 -0700471 default_agc_config_.targetLeveldBOv = options.tx_agc_target_dbov.value_or(
472 default_agc_config_.targetLeveldBOv);
buildbot@webrtc.org13d67762014-05-02 17:33:29 +0000473 default_agc_config_.digitalCompressionGaindB =
kwiberg102c6a62015-10-30 02:47:38 -0700474 options.tx_agc_digital_compression_gain.value_or(
buildbot@webrtc.org13d67762014-05-02 17:33:29 +0000475 default_agc_config_.digitalCompressionGaindB);
476 default_agc_config_.limiterEnable =
kwiberg102c6a62015-10-30 02:47:38 -0700477 options.tx_agc_limiter.value_or(default_agc_config_.limiterEnable);
Steve Anton606a5972017-12-07 14:31:01 -0800478 webrtc::apm_helpers::SetAgcConfig(apm(), default_agc_config_);
buildbot@webrtc.org13d67762014-05-02 17:33:29 +0000479 }
480
Alejandro Luebsc9b0c262016-05-16 15:32:38 -0700481 if (options.intelligibility_enhancer) {
482 intelligibility_enhancer_ = options.intelligibility_enhancer;
483 }
484 if (intelligibility_enhancer_ && *intelligibility_enhancer_) {
Mirko Bonadei675513b2017-11-09 11:09:25 +0100485 RTC_LOG(LS_INFO) << "Enabling NS when Intelligibility Enhancer is active.";
Alejandro Luebsc9b0c262016-05-16 15:32:38 -0700486 options.noise_suppression = intelligibility_enhancer_;
487 }
488
kwiberg102c6a62015-10-30 02:47:38 -0700489 if (options.noise_suppression) {
Alejandro Luebsc9b0c262016-05-16 15:32:38 -0700490 if (adm()->BuiltInNSIsAvailable()) {
491 bool builtin_ns =
492 *options.noise_suppression &&
493 !(intelligibility_enhancer_ && *intelligibility_enhancer_);
494 if (adm()->EnableBuiltInNS(builtin_ns) == 0 && builtin_ns) {
henrikac14f5ff2015-09-23 14:08:33 +0200495 // Disable internal software NS if built-in NS is enabled,
496 // i.e., replace the software NS with the built-in NS.
Oskar Sundbom78807582017-11-16 11:09:55 +0100497 options.noise_suppression = false;
Mirko Bonadei675513b2017-11-09 11:09:25 +0100498 RTC_LOG(LS_INFO)
499 << "Disabling NS since built-in NS will be used instead";
henrikac14f5ff2015-09-23 14:08:33 +0200500 }
501 }
solenberg76377c52017-02-21 00:54:31 -0800502 webrtc::apm_helpers::SetNsStatus(apm(), *options.noise_suppression);
buildbot@webrtc.org13d67762014-05-02 17:33:29 +0000503 }
504
kwiberg102c6a62015-10-30 02:47:38 -0700505 if (options.stereo_swapping) {
Mirko Bonadei675513b2017-11-09 11:09:25 +0100506 RTC_LOG(LS_INFO) << "Stereo swapping enabled? " << *options.stereo_swapping;
Fredrik Solenberg2a877972017-12-15 16:42:15 +0100507 audio_state()->SetStereoChannelSwapping(*options.stereo_swapping);
buildbot@webrtc.org13d67762014-05-02 17:33:29 +0000508 }
509
kwiberg102c6a62015-10-30 02:47:38 -0700510 if (options.audio_jitter_buffer_max_packets) {
Mirko Bonadei675513b2017-11-09 11:09:25 +0100511 RTC_LOG(LS_INFO) << "NetEq capacity is "
512 << *options.audio_jitter_buffer_max_packets;
Fredrik Solenberg8f5787a2018-01-11 13:52:30 +0100513 audio_jitter_buffer_max_packets_ =
solenberg88499ec2016-09-07 07:34:41 -0700514 std::max(20, *options.audio_jitter_buffer_max_packets);
Henrik Lundin64dad832015-05-11 12:44:23 +0200515 }
kwiberg102c6a62015-10-30 02:47:38 -0700516 if (options.audio_jitter_buffer_fast_accelerate) {
Mirko Bonadei675513b2017-11-09 11:09:25 +0100517 RTC_LOG(LS_INFO) << "NetEq fast mode? "
518 << *options.audio_jitter_buffer_fast_accelerate;
Fredrik Solenberg8f5787a2018-01-11 13:52:30 +0100519 audio_jitter_buffer_fast_accelerate_ =
solenberg88499ec2016-09-07 07:34:41 -0700520 *options.audio_jitter_buffer_fast_accelerate;
Henrik Lundin5263b3c2015-06-01 10:29:41 +0200521 }
522
kwiberg102c6a62015-10-30 02:47:38 -0700523 if (options.typing_detection) {
Mirko Bonadei675513b2017-11-09 11:09:25 +0100524 RTC_LOG(LS_INFO) << "Typing detection is enabled? "
525 << *options.typing_detection;
solenberg76377c52017-02-21 00:54:31 -0800526 webrtc::apm_helpers::SetTypingDetectionStatus(
527 apm(), *options.typing_detection);
buildbot@webrtc.org13d67762014-05-02 17:33:29 +0000528 }
529
buildbot@webrtc.org1f8a2372014-08-28 10:52:44 +0000530 webrtc::Config config;
531
kwiberg102c6a62015-10-30 02:47:38 -0700532 if (options.delay_agnostic_aec)
533 delay_agnostic_aec_ = options.delay_agnostic_aec;
534 if (delay_agnostic_aec_) {
Mirko Bonadei675513b2017-11-09 11:09:25 +0100535 RTC_LOG(LS_INFO) << "Delay agnostic aec is enabled? "
536 << *delay_agnostic_aec_;
henrik.lundin0f133b92015-07-02 00:17:55 -0700537 config.Set<webrtc::DelayAgnostic>(
kwiberg102c6a62015-10-30 02:47:38 -0700538 new webrtc::DelayAgnostic(*delay_agnostic_aec_));
Bjorn Volckerbf395c12015-03-25 22:45:56 +0100539 }
540
kwiberg102c6a62015-10-30 02:47:38 -0700541 if (options.extended_filter_aec) {
542 extended_filter_aec_ = options.extended_filter_aec;
543 }
544 if (extended_filter_aec_) {
Mirko Bonadei675513b2017-11-09 11:09:25 +0100545 RTC_LOG(LS_INFO) << "Extended filter aec is enabled? "
546 << *extended_filter_aec_;
Henrik Lundin441f6342015-06-09 16:03:13 +0200547 config.Set<webrtc::ExtendedFilter>(
kwiberg102c6a62015-10-30 02:47:38 -0700548 new webrtc::ExtendedFilter(*extended_filter_aec_));
buildbot@webrtc.org1f8a2372014-08-28 10:52:44 +0000549 }
550
kwiberg102c6a62015-10-30 02:47:38 -0700551 if (options.experimental_ns) {
552 experimental_ns_ = options.experimental_ns;
553 }
554 if (experimental_ns_) {
Mirko Bonadei675513b2017-11-09 11:09:25 +0100555 RTC_LOG(LS_INFO) << "Experimental ns is enabled? " << *experimental_ns_;
buildbot@webrtc.org1f8a2372014-08-28 10:52:44 +0000556 config.Set<webrtc::ExperimentalNs>(
kwiberg102c6a62015-10-30 02:47:38 -0700557 new webrtc::ExperimentalNs(*experimental_ns_));
buildbot@webrtc.org1f8a2372014-08-28 10:52:44 +0000558 }
buildbot@webrtc.org1f8a2372014-08-28 10:52:44 +0000559
Alejandro Luebsc9b0c262016-05-16 15:32:38 -0700560 if (intelligibility_enhancer_) {
Mirko Bonadei675513b2017-11-09 11:09:25 +0100561 RTC_LOG(LS_INFO) << "Intelligibility Enhancer is enabled? "
562 << *intelligibility_enhancer_;
Alejandro Luebsc9b0c262016-05-16 15:32:38 -0700563 config.Set<webrtc::Intelligibility>(
564 new webrtc::Intelligibility(*intelligibility_enhancer_));
565 }
566
peaha3333bf2016-06-30 00:02:34 -0700567 if (options.level_control) {
568 level_control_ = options.level_control;
569 }
570
peahb1c9d1d2017-07-25 15:45:24 -0700571 webrtc::AudioProcessing::Config apm_config = apm()->GetConfig();
572
Mirko Bonadei675513b2017-11-09 11:09:25 +0100573 RTC_LOG(LS_INFO) << "Level control: "
574 << (!!level_control_ ? *level_control_ : -1);
peaha3333bf2016-06-30 00:02:34 -0700575 if (level_control_) {
peahb1c9d1d2017-07-25 15:45:24 -0700576 apm_config.level_controller.enabled = *level_control_;
aleloie33c5d92016-10-20 01:53:27 -0700577 if (options.level_control_initial_peak_level_dbfs) {
peahb1c9d1d2017-07-25 15:45:24 -0700578 apm_config.level_controller.initial_peak_level_dbfs =
aleloie33c5d92016-10-20 01:53:27 -0700579 *options.level_control_initial_peak_level_dbfs;
580 }
peaha3333bf2016-06-30 00:02:34 -0700581 }
582
peah8271d042016-11-22 07:24:52 -0800583 if (options.highpass_filter) {
peahb1c9d1d2017-07-25 15:45:24 -0700584 apm_config.high_pass_filter.enabled = *options.highpass_filter;
peah8271d042016-11-22 07:24:52 -0800585 }
586
ivoc4ca18692017-02-10 05:11:09 -0800587 if (options.residual_echo_detector) {
peahb1c9d1d2017-07-25 15:45:24 -0700588 apm_config.residual_echo_detector.enabled = *options.residual_echo_detector;
ivoc4ca18692017-02-10 05:11:09 -0800589 }
590
solenberg059fb442016-10-26 05:12:24 -0700591 apm()->SetExtraOptions(config);
peahb1c9d1d2017-07-25 15:45:24 -0700592 apm()->ApplyConfig(apm_config);
buildbot@webrtc.org13d67762014-05-02 17:33:29 +0000593 return true;
594}
595
solenberg796b8f92017-03-01 17:02:23 -0800596// TODO(solenberg): Remove, once AudioMonitor is gone.
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000597int WebRtcVoiceEngine::GetInputLevel() {
solenberg566ef242015-11-06 15:34:49 -0800598 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
Fredrik Solenberg2a877972017-12-15 16:42:15 +0100599 return audio_state()->GetAudioInputStats().quantized_audio_level;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000600}
601
ossudedfd282016-06-14 07:12:39 -0700602const std::vector<AudioCodec>& WebRtcVoiceEngine::send_codecs() const {
603 RTC_DCHECK(signal_thread_checker_.CalledOnValidThread());
ossuc54071d2016-08-17 02:45:41 -0700604 return send_codecs_;
ossudedfd282016-06-14 07:12:39 -0700605}
606
607const std::vector<AudioCodec>& WebRtcVoiceEngine::recv_codecs() const {
solenberg566ef242015-11-06 15:34:49 -0800608 RTC_DCHECK(signal_thread_checker_.CalledOnValidThread());
ossuc54071d2016-08-17 02:45:41 -0700609 return recv_codecs_;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000610}
611
Stefan Holmer9d69c3f2015-12-07 10:45:43 +0100612RtpCapabilities WebRtcVoiceEngine::GetCapabilities() const {
solenberg566ef242015-11-06 15:34:49 -0800613 RTC_DCHECK(signal_thread_checker_.CalledOnValidThread());
Stefan Holmer9d69c3f2015-12-07 10:45:43 +0100614 RtpCapabilities capabilities;
Stefan Holmer9d69c3f2015-12-07 10:45:43 +0100615 capabilities.header_extensions.push_back(
isheriff6f8d6862016-05-26 11:24:55 -0700616 webrtc::RtpExtension(webrtc::RtpExtension::kAudioLevelUri,
617 webrtc::RtpExtension::kAudioLevelDefaultId));
sprangc1b57a12017-02-28 08:50:47 -0800618 if (webrtc::field_trial::IsEnabled("WebRTC-Audio-SendSideBwe")) {
isheriff6f8d6862016-05-26 11:24:55 -0700619 capabilities.header_extensions.push_back(webrtc::RtpExtension(
620 webrtc::RtpExtension::kTransportSequenceNumberUri,
621 webrtc::RtpExtension::kTransportSequenceNumberDefaultId));
stefanba4c0e42016-02-04 04:12:24 -0800622 }
Stefan Holmer9d69c3f2015-12-07 10:45:43 +0100623 return capabilities;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000624}
625
solenberg63b34542015-09-29 06:06:31 -0700626void WebRtcVoiceEngine::RegisterChannel(WebRtcVoiceMediaChannel* channel) {
solenberg566ef242015-11-06 15:34:49 -0800627 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
628 RTC_DCHECK(channel);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000629 channels_.push_back(channel);
630}
631
solenberg63b34542015-09-29 06:06:31 -0700632void WebRtcVoiceEngine::UnregisterChannel(WebRtcVoiceMediaChannel* channel) {
solenberg566ef242015-11-06 15:34:49 -0800633 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
solenberg63b34542015-09-29 06:06:31 -0700634 auto it = std::find(channels_.begin(), channels_.end(), channel);
solenberg566ef242015-11-06 15:34:49 -0800635 RTC_DCHECK(it != channels_.end());
636 channels_.erase(it);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000637}
638
ivocd66b44d2016-01-15 03:06:36 -0800639bool WebRtcVoiceEngine::StartAecDump(rtc::PlatformFile file,
640 int64_t max_size_bytes) {
solenberg566ef242015-11-06 15:34:49 -0800641 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
deadbeefeb02c032017-06-15 08:29:25 -0700642 auto aec_dump = webrtc::AecDumpFactory::Create(
643 file, max_size_bytes, low_priority_worker_queue_.get());
aleloi048cbdd2017-05-29 02:56:27 -0700644 if (!aec_dump) {
wu@webrtc.orga8910d22014-01-23 22:12:45 +0000645 return false;
646 }
aleloi048cbdd2017-05-29 02:56:27 -0700647 apm()->AttachAecDump(std::move(aec_dump));
wu@webrtc.orga9890802013-12-13 00:21:03 +0000648 return true;
wu@webrtc.orga9890802013-12-13 00:21:03 +0000649}
650
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000651void WebRtcVoiceEngine::StartAecDump(const std::string& filename) {
solenberg566ef242015-11-06 15:34:49 -0800652 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
aleloi048cbdd2017-05-29 02:56:27 -0700653
deadbeefeb02c032017-06-15 08:29:25 -0700654 auto aec_dump = webrtc::AecDumpFactory::Create(
655 filename, -1, low_priority_worker_queue_.get());
aleloi048cbdd2017-05-29 02:56:27 -0700656 if (aec_dump) {
657 apm()->AttachAecDump(std::move(aec_dump));
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000658 }
659}
660
661void WebRtcVoiceEngine::StopAecDump() {
solenberg566ef242015-11-06 15:34:49 -0800662 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
aleloi048cbdd2017-05-29 02:56:27 -0700663 apm()->DetachAecDump();
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000664}
665
solenberg5b5129a2016-04-08 05:35:48 -0700666webrtc::AudioDeviceModule* WebRtcVoiceEngine::adm() {
667 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
668 RTC_DCHECK(adm_);
Fredrik Solenbergd3195342017-11-21 20:33:05 +0100669 return adm_.get();
solenberg5b5129a2016-04-08 05:35:48 -0700670}
671
peahb1c9d1d2017-07-25 15:45:24 -0700672webrtc::AudioProcessing* WebRtcVoiceEngine::apm() const {
solenberg059fb442016-10-26 05:12:24 -0700673 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
Fredrik Solenberg55900fd2017-11-23 20:22:55 +0100674 RTC_DCHECK(apm_);
peaha9cc40b2017-06-29 08:32:09 -0700675 return apm_.get();
solenberg059fb442016-10-26 05:12:24 -0700676}
677
Fredrik Solenberg2a877972017-12-15 16:42:15 +0100678webrtc::AudioState* WebRtcVoiceEngine::audio_state() {
solenberg76377c52017-02-21 00:54:31 -0800679 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
Fredrik Solenberg2a877972017-12-15 16:42:15 +0100680 RTC_DCHECK(audio_state_);
681 return audio_state_.get();
solenberg76377c52017-02-21 00:54:31 -0800682}
683
ossu20a4b3f2017-04-27 02:08:52 -0700684AudioCodecs WebRtcVoiceEngine::CollectCodecs(
685 const std::vector<webrtc::AudioCodecSpec>& specs) const {
ossuc54071d2016-08-17 02:45:41 -0700686 PayloadTypeMapper mapper;
687 AudioCodecs out;
ossuc54071d2016-08-17 02:45:41 -0700688
solenberg2779bab2016-11-17 04:45:19 -0800689 // Only generate CN payload types for these clockrates:
ossuc54071d2016-08-17 02:45:41 -0700690 std::map<int, bool, std::greater<int>> generate_cn = {{ 8000, false },
691 { 16000, false },
692 { 32000, false }};
solenberg2779bab2016-11-17 04:45:19 -0800693 // Only generate telephone-event payload types for these clockrates:
694 std::map<int, bool, std::greater<int>> generate_dtmf = {{ 8000, false },
695 { 16000, false },
696 { 32000, false },
697 { 48000, false }};
ossuc54071d2016-08-17 02:45:41 -0700698
ossu9def8002017-02-09 05:14:32 -0800699 auto map_format = [&mapper](const webrtc::SdpAudioFormat& format,
700 AudioCodecs* out) {
ossuc54071d2016-08-17 02:45:41 -0700701 rtc::Optional<AudioCodec> opt_codec = mapper.ToAudioCodec(format);
ossu9def8002017-02-09 05:14:32 -0800702 if (opt_codec) {
703 if (out) {
704 out->push_back(*opt_codec);
705 }
706 } else {
Mirko Bonadei675513b2017-11-09 11:09:25 +0100707 RTC_LOG(LS_ERROR) << "Unable to assign payload type to format: "
708 << format;
ossuc54071d2016-08-17 02:45:41 -0700709 }
710
ossu9def8002017-02-09 05:14:32 -0800711 return opt_codec;
ossuc54071d2016-08-17 02:45:41 -0700712 };
713
ossud4e9f622016-08-18 02:01:17 -0700714 for (const auto& spec : specs) {
ossu9def8002017-02-09 05:14:32 -0800715 // We need to do some extra stuff before adding the main codecs to out.
716 rtc::Optional<AudioCodec> opt_codec = map_format(spec.format, nullptr);
717 if (opt_codec) {
718 AudioCodec& codec = *opt_codec;
ossua1a040a2017-04-06 10:03:21 -0700719 if (spec.info.supports_network_adaption) {
ossu9def8002017-02-09 05:14:32 -0800720 codec.AddFeedbackParam(
721 FeedbackParam(kRtcpFbParamTransportCc, kParamValueEmpty));
722 }
723
ossua1a040a2017-04-06 10:03:21 -0700724 if (spec.info.allow_comfort_noise) {
solenberg2779bab2016-11-17 04:45:19 -0800725 // Generate a CN entry if the decoder allows it and we support the
726 // clockrate.
727 auto cn = generate_cn.find(spec.format.clockrate_hz);
728 if (cn != generate_cn.end()) {
729 cn->second = true;
730 }
731 }
732
733 // Generate a telephone-event entry if we support the clockrate.
734 auto dtmf = generate_dtmf.find(spec.format.clockrate_hz);
735 if (dtmf != generate_dtmf.end()) {
736 dtmf->second = true;
ossuc54071d2016-08-17 02:45:41 -0700737 }
ossu9def8002017-02-09 05:14:32 -0800738
739 out.push_back(codec);
ossuc54071d2016-08-17 02:45:41 -0700740 }
741 }
742
solenberg2779bab2016-11-17 04:45:19 -0800743 // Add CN codecs after "proper" audio codecs.
ossuc54071d2016-08-17 02:45:41 -0700744 for (const auto& cn : generate_cn) {
745 if (cn.second) {
ossu9def8002017-02-09 05:14:32 -0800746 map_format({kCnCodecName, cn.first, 1}, &out);
ossuc54071d2016-08-17 02:45:41 -0700747 }
748 }
749
solenberg2779bab2016-11-17 04:45:19 -0800750 // Add telephone-event codecs last.
751 for (const auto& dtmf : generate_dtmf) {
752 if (dtmf.second) {
ossu9def8002017-02-09 05:14:32 -0800753 map_format({kDtmfCodecName, dtmf.first, 1}, &out);
solenberg2779bab2016-11-17 04:45:19 -0800754 }
755 }
ossuc54071d2016-08-17 02:45:41 -0700756
757 return out;
758}
759
solenbergc96df772015-10-21 13:01:53 -0700760class WebRtcVoiceMediaChannel::WebRtcAudioSendStream
Taylor Brandstetter1a018dc2016-03-08 12:37:39 -0800761 : public AudioSource::Sink {
mallinath@webrtc.org67ee6b92014-02-03 16:57:16 +0000762 public:
minyue7a973442016-10-20 03:27:12 -0700763 WebRtcAudioSendStream(
minyue7a973442016-10-20 03:27:12 -0700764 uint32_t ssrc,
765 const std::string& c_name,
Alex Narestb3944f02017-10-13 14:56:18 +0200766 const std::string track_id,
ossu20a4b3f2017-04-27 02:08:52 -0700767 const rtc::Optional<webrtc::AudioSendStream::Config::SendCodecSpec>&
768 send_codec_spec,
minyue7a973442016-10-20 03:27:12 -0700769 const std::vector<webrtc::RtpExtension>& extensions,
770 int max_send_bitrate_bps,
minyue6b825df2016-10-31 04:08:32 -0700771 const rtc::Optional<std::string>& audio_network_adaptor_config,
minyue7a973442016-10-20 03:27:12 -0700772 webrtc::Call* call,
ossu20a4b3f2017-04-27 02:08:52 -0700773 webrtc::Transport* send_transport,
774 const rtc::scoped_refptr<webrtc::AudioEncoderFactory>& encoder_factory)
Fredrik Solenberg2a877972017-12-15 16:42:15 +0100775 : call_(call),
mflodman3d7db262016-04-29 00:57:13 -0700776 config_(send_transport),
sprangc1b57a12017-02-28 08:50:47 -0800777 send_side_bwe_with_overhead_(
778 webrtc::field_trial::IsEnabled("WebRTC-SendSideBwe-WithOverhead")),
minyue7a973442016-10-20 03:27:12 -0700779 max_send_bitrate_bps_(max_send_bitrate_bps),
skvlade0d46372016-04-07 22:59:22 -0700780 rtp_parameters_(CreateRtpParametersWithOneEncoding()) {
solenbergc96df772015-10-21 13:01:53 -0700781 RTC_DCHECK(call);
ossu20a4b3f2017-04-27 02:08:52 -0700782 RTC_DCHECK(encoder_factory);
solenberg3a941542015-11-16 07:34:50 -0800783 config_.rtp.ssrc = ssrc;
784 config_.rtp.c_name = c_name;
solenberg971cab02016-06-14 10:02:41 -0700785 config_.rtp.extensions = extensions;
minyue6b825df2016-10-31 04:08:32 -0700786 config_.audio_network_adaptor_config = audio_network_adaptor_config;
ossu20a4b3f2017-04-27 02:08:52 -0700787 config_.encoder_factory = encoder_factory;
Alex Narestb3944f02017-10-13 14:56:18 +0200788 config_.track_id = track_id;
Oskar Sundbom78807582017-11-16 11:09:55 +0100789 rtp_parameters_.encodings[0].ssrc = ssrc;
ossu20a4b3f2017-04-27 02:08:52 -0700790
791 if (send_codec_spec) {
792 UpdateSendCodecSpec(*send_codec_spec);
793 }
794
795 stream_ = call_->CreateAudioSendStream(config_);
solenbergc96df772015-10-21 13:01:53 -0700796 }
solenberg3a941542015-11-16 07:34:50 -0800797
solenbergc96df772015-10-21 13:01:53 -0700798 ~WebRtcAudioSendStream() override {
solenberg566ef242015-11-06 15:34:49 -0800799 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
Taylor Brandstetter1a018dc2016-03-08 12:37:39 -0800800 ClearSource();
solenbergc96df772015-10-21 13:01:53 -0700801 call_->DestroyAudioSendStream(stream_);
802 }
henrike@webrtc.org1e09a712013-07-26 19:17:59 +0000803
ossu20a4b3f2017-04-27 02:08:52 -0700804 void SetSendCodecSpec(
minyue7a973442016-10-20 03:27:12 -0700805 const webrtc::AudioSendStream::Config::SendCodecSpec& send_codec_spec) {
ossu20a4b3f2017-04-27 02:08:52 -0700806 UpdateSendCodecSpec(send_codec_spec);
807 ReconfigureAudioSendStream();
solenberg971cab02016-06-14 10:02:41 -0700808 }
809
ossu20a4b3f2017-04-27 02:08:52 -0700810 void SetRtpExtensions(const std::vector<webrtc::RtpExtension>& extensions) {
solenberg3a941542015-11-16 07:34:50 -0800811 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
solenberg3a941542015-11-16 07:34:50 -0800812 config_.rtp.extensions = extensions;
ossu20a4b3f2017-04-27 02:08:52 -0700813 ReconfigureAudioSendStream();
solenberg3a941542015-11-16 07:34:50 -0800814 }
815
ossu20a4b3f2017-04-27 02:08:52 -0700816 void SetAudioNetworkAdaptorConfig(
minyue6b825df2016-10-31 04:08:32 -0700817 const rtc::Optional<std::string>& audio_network_adaptor_config) {
818 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
819 if (config_.audio_network_adaptor_config == audio_network_adaptor_config) {
820 return;
821 }
822 config_.audio_network_adaptor_config = audio_network_adaptor_config;
ossu20a4b3f2017-04-27 02:08:52 -0700823 UpdateAllowedBitrateRange();
824 ReconfigureAudioSendStream();
minyue6b825df2016-10-31 04:08:32 -0700825 }
826
minyue7a973442016-10-20 03:27:12 -0700827 bool SetMaxSendBitrate(int bps) {
828 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
ossu20a4b3f2017-04-27 02:08:52 -0700829 RTC_DCHECK(config_.send_codec_spec);
830 RTC_DCHECK(audio_codec_spec_);
831 auto send_rate = ComputeSendBitrate(
832 bps, rtp_parameters_.encodings[0].max_bitrate_bps, *audio_codec_spec_);
833
minyue7a973442016-10-20 03:27:12 -0700834 if (!send_rate) {
835 return false;
836 }
837
838 max_send_bitrate_bps_ = bps;
839
ossu20a4b3f2017-04-27 02:08:52 -0700840 if (send_rate != config_.send_codec_spec->target_bitrate_bps) {
841 config_.send_codec_spec->target_bitrate_bps = send_rate;
842 ReconfigureAudioSendStream();
minyue7a973442016-10-20 03:27:12 -0700843 }
844 return true;
845 }
846
solenbergffbbcac2016-11-17 05:25:37 -0800847 bool SendTelephoneEvent(int payload_type, int payload_freq, int event,
848 int duration_ms) {
Fredrik Solenbergb5727682015-12-04 15:22:19 +0100849 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
850 RTC_DCHECK(stream_);
solenbergffbbcac2016-11-17 05:25:37 -0800851 return stream_->SendTelephoneEvent(payload_type, payload_freq, event,
852 duration_ms);
Fredrik Solenbergb5727682015-12-04 15:22:19 +0100853 }
854
Taylor Brandstetter1a018dc2016-03-08 12:37:39 -0800855 void SetSend(bool send) {
856 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
857 send_ = send;
858 UpdateSendState();
859 }
860
solenberg94218532016-06-16 10:53:22 -0700861 void SetMuted(bool muted) {
862 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
863 RTC_DCHECK(stream_);
864 stream_->SetMuted(muted);
865 muted_ = muted;
866 }
867
868 bool muted() const {
869 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
870 return muted_;
871 }
872
Ivo Creusen56d46092017-11-24 17:29:59 +0100873 webrtc::AudioSendStream::Stats GetStats(bool has_remote_tracks) const {
solenberg3a941542015-11-16 07:34:50 -0800874 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
875 RTC_DCHECK(stream_);
Ivo Creusen56d46092017-11-24 17:29:59 +0100876 return stream_->GetStats(has_remote_tracks);
solenberg3a941542015-11-16 07:34:50 -0800877 }
878
Taylor Brandstetter1a018dc2016-03-08 12:37:39 -0800879 // Starts the sending by setting ourselves as a sink to the AudioSource to
880 // get data callbacks.
henrike@webrtc.orga7b98182014-02-21 15:51:43 +0000881 // This method is called on the libjingle worker thread.
mallinath@webrtc.org67ee6b92014-02-03 16:57:16 +0000882 // TODO(xians): Make sure Start() is called only once.
Taylor Brandstetter1a018dc2016-03-08 12:37:39 -0800883 void SetSource(AudioSource* source) {
solenberg566ef242015-11-06 15:34:49 -0800884 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
Taylor Brandstetter1a018dc2016-03-08 12:37:39 -0800885 RTC_DCHECK(source);
886 if (source_) {
887 RTC_DCHECK(source_ == source);
mallinath@webrtc.org67ee6b92014-02-03 16:57:16 +0000888 return;
889 }
Taylor Brandstetter1a018dc2016-03-08 12:37:39 -0800890 source->SetSink(this);
891 source_ = source;
892 UpdateSendState();
mallinath@webrtc.org67ee6b92014-02-03 16:57:16 +0000893 }
894
Taylor Brandstetter1a018dc2016-03-08 12:37:39 -0800895 // Stops sending by setting the sink of the AudioSource to nullptr. No data
mallinath@webrtc.org67ee6b92014-02-03 16:57:16 +0000896 // callback will be received after this method.
henrike@webrtc.orga7b98182014-02-21 15:51:43 +0000897 // This method is called on the libjingle worker thread.
Taylor Brandstetter1a018dc2016-03-08 12:37:39 -0800898 void ClearSource() {
solenberg566ef242015-11-06 15:34:49 -0800899 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
Taylor Brandstetter1a018dc2016-03-08 12:37:39 -0800900 if (source_) {
901 source_->SetSink(nullptr);
902 source_ = nullptr;
solenberg98c68862015-10-09 03:27:14 -0700903 }
Taylor Brandstetter1a018dc2016-03-08 12:37:39 -0800904 UpdateSendState();
mallinath@webrtc.org67ee6b92014-02-03 16:57:16 +0000905 }
906
Taylor Brandstetter1a018dc2016-03-08 12:37:39 -0800907 // AudioSource::Sink implementation.
henrike@webrtc.orga7b98182014-02-21 15:51:43 +0000908 // This method is called on the audio thread.
kjellander@webrtc.org14665ff2015-03-04 12:58:35 +0000909 void OnData(const void* audio_data,
910 int bits_per_sample,
911 int sample_rate,
Peter Kasting69558702016-01-12 16:26:35 -0800912 size_t number_of_channels,
Peter Kastingdce40cf2015-08-24 14:52:23 -0700913 size_t number_of_frames) override {
Fredrik Solenberg2a877972017-12-15 16:42:15 +0100914 RTC_DCHECK_EQ(16, bits_per_sample);
solenberg347ec5c2016-09-23 04:21:47 -0700915 RTC_CHECK_RUNS_SERIALIZED(&audio_capture_race_checker_);
Fredrik Solenberg2a877972017-12-15 16:42:15 +0100916 RTC_DCHECK(stream_);
917 std::unique_ptr<webrtc::AudioFrame> audio_frame(new webrtc::AudioFrame());
918 audio_frame->UpdateFrame(audio_frame->timestamp_,
919 static_cast<const int16_t*>(audio_data),
920 number_of_frames,
921 sample_rate,
922 audio_frame->speech_type_,
923 audio_frame->vad_activity_,
924 number_of_channels);
925 stream_->SendAudioData(std::move(audio_frame));
henrike@webrtc.orga7b98182014-02-21 15:51:43 +0000926 }
927
Taylor Brandstetter1a018dc2016-03-08 12:37:39 -0800928 // Callback from the |source_| when it is going away. In case Start() has
henrike@webrtc.orga7b98182014-02-21 15:51:43 +0000929 // never been called, this callback won't be triggered.
kjellander@webrtc.org14665ff2015-03-04 12:58:35 +0000930 void OnClose() override {
solenberg566ef242015-11-06 15:34:49 -0800931 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
Taylor Brandstetter1a018dc2016-03-08 12:37:39 -0800932 // Set |source_| to nullptr to make sure no more callback will get into
933 // the source.
934 source_ = nullptr;
935 UpdateSendState();
mallinath@webrtc.org67ee6b92014-02-03 16:57:16 +0000936 }
937
skvlade0d46372016-04-07 22:59:22 -0700938 const webrtc::RtpParameters& rtp_parameters() const {
939 return rtp_parameters_;
940 }
941
deadbeeffb2aced2017-01-06 23:05:37 -0800942 bool ValidateRtpParameters(const webrtc::RtpParameters& rtp_parameters) {
943 if (rtp_parameters.encodings.size() != 1) {
Mirko Bonadei675513b2017-11-09 11:09:25 +0100944 RTC_LOG(LS_ERROR)
deadbeeffb2aced2017-01-06 23:05:37 -0800945 << "Attempted to set RtpParameters without exactly one encoding";
946 return false;
947 }
948 if (rtp_parameters.encodings[0].ssrc != rtp_parameters_.encodings[0].ssrc) {
Mirko Bonadei675513b2017-11-09 11:09:25 +0100949 RTC_LOG(LS_ERROR) << "Attempted to set RtpParameters with modified SSRC";
deadbeeffb2aced2017-01-06 23:05:37 -0800950 return false;
951 }
Seth Hampson24722b32017-12-22 09:36:42 -0800952 if (rtp_parameters.encodings[0].bitrate_priority <= 0) {
953 RTC_LOG(LS_ERROR) << "Attempted to set RtpParameters bitrate_priority to "
954 "an invalid number.";
955 return false;
956 }
deadbeeffb2aced2017-01-06 23:05:37 -0800957 return true;
958 }
959
minyue7a973442016-10-20 03:27:12 -0700960 bool SetRtpParameters(const webrtc::RtpParameters& parameters) {
deadbeeffb2aced2017-01-06 23:05:37 -0800961 if (!ValidateRtpParameters(parameters)) {
962 return false;
963 }
ossu20a4b3f2017-04-27 02:08:52 -0700964
965 rtc::Optional<int> send_rate;
966 if (audio_codec_spec_) {
967 send_rate = ComputeSendBitrate(max_send_bitrate_bps_,
968 parameters.encodings[0].max_bitrate_bps,
969 *audio_codec_spec_);
970 if (!send_rate) {
971 return false;
972 }
minyue7a973442016-10-20 03:27:12 -0700973 }
974
minyuececec102017-03-27 13:04:25 -0700975 const rtc::Optional<int> old_rtp_max_bitrate =
976 rtp_parameters_.encodings[0].max_bitrate_bps;
Seth Hampson24722b32017-12-22 09:36:42 -0800977 double old_priority = rtp_parameters_.encodings[0].bitrate_priority;
Lu Liu8b77aea2017-12-20 23:48:03 +0000978 rtp_parameters_ = parameters;
Seth Hampson24722b32017-12-22 09:36:42 -0800979 config_.bitrate_priority = rtp_parameters_.encodings[0].bitrate_priority;
Lu Liu8b77aea2017-12-20 23:48:03 +0000980
Seth Hampson24722b32017-12-22 09:36:42 -0800981 bool reconfigure_send_stream =
982 (rtp_parameters_.encodings[0].max_bitrate_bps != old_rtp_max_bitrate) ||
983 (rtp_parameters_.encodings[0].bitrate_priority != old_priority);
minyuececec102017-03-27 13:04:25 -0700984 if (rtp_parameters_.encodings[0].max_bitrate_bps != old_rtp_max_bitrate) {
Seth Hampson24722b32017-12-22 09:36:42 -0800985 // Update the bitrate range.
ossu20a4b3f2017-04-27 02:08:52 -0700986 if (send_rate) {
987 config_.send_codec_spec->target_bitrate_bps = send_rate;
988 }
989 UpdateAllowedBitrateRange();
Seth Hampsond2b912a2017-12-20 11:56:37 -0800990 }
Seth Hampson24722b32017-12-22 09:36:42 -0800991 if (reconfigure_send_stream) {
992 ReconfigureAudioSendStream();
993 }
994 // parameters.encodings[0].active could have changed.
995 UpdateSendState();
minyue7a973442016-10-20 03:27:12 -0700996 return true;
skvlade0d46372016-04-07 22:59:22 -0700997 }
998
mallinath@webrtc.org67ee6b92014-02-03 16:57:16 +0000999 private:
Taylor Brandstetter1a018dc2016-03-08 12:37:39 -08001000 void UpdateSendState() {
1001 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
1002 RTC_DCHECK(stream_);
Taylor Brandstetter55dd7082016-05-03 13:50:11 -07001003 RTC_DCHECK_EQ(1UL, rtp_parameters_.encodings.size());
1004 if (send_ && source_ != nullptr && rtp_parameters_.encodings[0].active) {
Taylor Brandstetter1a018dc2016-03-08 12:37:39 -08001005 stream_->Start();
1006 } else { // !send || source_ = nullptr
1007 stream_->Stop();
1008 }
1009 }
1010
ossu20a4b3f2017-04-27 02:08:52 -07001011 void UpdateAllowedBitrateRange() {
michaelt53fe19d2016-10-18 09:39:22 -07001012 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
ossu20a4b3f2017-04-27 02:08:52 -07001013 const bool is_opus =
1014 config_.send_codec_spec &&
1015 !STR_CASE_CMP(config_.send_codec_spec->format.name.c_str(),
1016 kOpusCodecName);
1017 if (is_opus && webrtc::field_trial::IsEnabled("WebRTC-Audio-SendSideBwe")) {
stefane9f36d52017-01-24 08:18:45 -08001018 config_.min_bitrate_bps = kOpusMinBitrateBps;
minyuececec102017-03-27 13:04:25 -07001019
1020 // This means that when RtpParameters is reset, we may change the
ossu20a4b3f2017-04-27 02:08:52 -07001021 // encoder's bit rate immediately (through ReconfigureAudioSendStream()),
minyuececec102017-03-27 13:04:25 -07001022 // meanwhile change the cap to the output of BWE.
1023 config_.max_bitrate_bps =
1024 rtp_parameters_.encodings[0].max_bitrate_bps
1025 ? *rtp_parameters_.encodings[0].max_bitrate_bps
1026 : kOpusBitrateFbBps;
1027
michaelt53fe19d2016-10-18 09:39:22 -07001028 // TODO(mflodman): Keep testing this and set proper values.
1029 // Note: This is an early experiment currently only supported by Opus.
elad.alon0fe12162017-01-31 05:48:37 -08001030 if (send_side_bwe_with_overhead_) {
ossu20a4b3f2017-04-27 02:08:52 -07001031 const int max_packet_size_ms =
1032 WEBRTC_OPUS_SUPPORT_120MS_PTIME ? 120 : 60;
michaelt6672b262017-01-11 10:17:59 -08001033
ossu20a4b3f2017-04-27 02:08:52 -07001034 // OverheadPerPacket = Ipv4(20B) + UDP(8B) + SRTP(10B) + RTP(12)
1035 constexpr int kOverheadPerPacket = 20 + 8 + 10 + 12;
michaelt6672b262017-01-11 10:17:59 -08001036
ossu20a4b3f2017-04-27 02:08:52 -07001037 int min_overhead_bps =
1038 kOverheadPerPacket * 8 * 1000 / max_packet_size_ms;
michaelt6672b262017-01-11 10:17:59 -08001039
ossu20a4b3f2017-04-27 02:08:52 -07001040 // We assume that |config_.max_bitrate_bps| before the next line is
1041 // a hard limit on the payload bitrate, so we add min_overhead_bps to
1042 // it to ensure that, when overhead is deducted, the payload rate
1043 // never goes beyond the limit.
1044 // Note: this also means that if a higher overhead is forced, we
1045 // cannot reach the limit.
1046 // TODO(minyue): Reconsider this when the signaling to BWE is done
1047 // through a dedicated API.
1048 config_.max_bitrate_bps += min_overhead_bps;
michaelt6672b262017-01-11 10:17:59 -08001049
ossu20a4b3f2017-04-27 02:08:52 -07001050 // In contrast to max_bitrate_bps, we let min_bitrate_bps always be
1051 // reachable.
1052 config_.min_bitrate_bps += min_overhead_bps;
michaelt6672b262017-01-11 10:17:59 -08001053 }
michaelt53fe19d2016-10-18 09:39:22 -07001054 }
ossu20a4b3f2017-04-27 02:08:52 -07001055 }
1056
1057 void UpdateSendCodecSpec(
1058 const webrtc::AudioSendStream::Config::SendCodecSpec& send_codec_spec) {
1059 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
1060 config_.rtp.nack.rtp_history_ms =
1061 send_codec_spec.nack_enabled ? kNackRtpHistoryMs : 0;
Oskar Sundbom78807582017-11-16 11:09:55 +01001062 config_.send_codec_spec = send_codec_spec;
ossu20a4b3f2017-04-27 02:08:52 -07001063 auto info =
1064 config_.encoder_factory->QueryAudioEncoder(send_codec_spec.format);
1065 RTC_DCHECK(info);
1066 // If a specific target bitrate has been set for the stream, use that as
1067 // the new default bitrate when computing send bitrate.
1068 if (send_codec_spec.target_bitrate_bps) {
1069 info->default_bitrate_bps = std::max(
1070 info->min_bitrate_bps,
1071 std::min(info->max_bitrate_bps, *send_codec_spec.target_bitrate_bps));
1072 }
1073
1074 audio_codec_spec_.emplace(
1075 webrtc::AudioCodecSpec{send_codec_spec.format, *info});
1076
1077 config_.send_codec_spec->target_bitrate_bps = ComputeSendBitrate(
1078 max_send_bitrate_bps_, rtp_parameters_.encodings[0].max_bitrate_bps,
1079 *audio_codec_spec_);
1080
1081 UpdateAllowedBitrateRange();
1082 }
1083
1084 void ReconfigureAudioSendStream() {
1085 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
1086 RTC_DCHECK(stream_);
1087 stream_->Reconfigure(config_);
michaelt53fe19d2016-10-18 09:39:22 -07001088 }
1089
solenberg566ef242015-11-06 15:34:49 -08001090 rtc::ThreadChecker worker_thread_checker_;
solenberg347ec5c2016-09-23 04:21:47 -07001091 rtc::RaceChecker audio_capture_race_checker_;
solenbergc96df772015-10-21 13:01:53 -07001092 webrtc::Call* call_ = nullptr;
solenberg3a941542015-11-16 07:34:50 -08001093 webrtc::AudioSendStream::Config config_;
elad.alon0fe12162017-01-31 05:48:37 -08001094 const bool send_side_bwe_with_overhead_;
solenberg3a941542015-11-16 07:34:50 -08001095 // The stream is owned by WebRtcAudioSendStream and may be reallocated if
1096 // configuration changes.
solenbergc96df772015-10-21 13:01:53 -07001097 webrtc::AudioSendStream* stream_ = nullptr;
mallinath@webrtc.org67ee6b92014-02-03 16:57:16 +00001098
Taylor Brandstetter1a018dc2016-03-08 12:37:39 -08001099 // Raw pointer to AudioSource owned by LocalAudioTrackHandler.
mallinath@webrtc.org67ee6b92014-02-03 16:57:16 +00001100 // PeerConnection will make sure invalidating the pointer before the object
1101 // goes away.
Taylor Brandstetter1a018dc2016-03-08 12:37:39 -08001102 AudioSource* source_ = nullptr;
1103 bool send_ = false;
solenberg94218532016-06-16 10:53:22 -07001104 bool muted_ = false;
minyue7a973442016-10-20 03:27:12 -07001105 int max_send_bitrate_bps_;
skvlade0d46372016-04-07 22:59:22 -07001106 webrtc::RtpParameters rtp_parameters_;
ossu20a4b3f2017-04-27 02:08:52 -07001107 rtc::Optional<webrtc::AudioCodecSpec> audio_codec_spec_;
henrike@webrtc.orga7b98182014-02-21 15:51:43 +00001108
solenbergc96df772015-10-21 13:01:53 -07001109 RTC_DISALLOW_IMPLICIT_CONSTRUCTORS(WebRtcAudioSendStream);
1110};
1111
1112class WebRtcVoiceMediaChannel::WebRtcAudioReceiveStream {
1113 public:
ossu29b1a8d2016-06-13 07:34:51 -07001114 WebRtcAudioReceiveStream(
ossu29b1a8d2016-06-13 07:34:51 -07001115 uint32_t remote_ssrc,
1116 uint32_t local_ssrc,
1117 bool use_transport_cc,
solenberg8189b022016-06-14 12:13:00 -07001118 bool use_nack,
ossu29b1a8d2016-06-13 07:34:51 -07001119 const std::string& sync_group,
1120 const std::vector<webrtc::RtpExtension>& extensions,
1121 webrtc::Call* call,
1122 webrtc::Transport* rtcp_send_transport,
kwiberg1c07c702017-03-27 07:15:49 -07001123 const rtc::scoped_refptr<webrtc::AudioDecoderFactory>& decoder_factory,
Fredrik Solenberg8f5787a2018-01-11 13:52:30 +01001124 const std::map<int, webrtc::SdpAudioFormat>& decoder_map,
1125 size_t jitter_buffer_max_packets,
1126 bool jitter_buffer_fast_accelerate)
stefanba4c0e42016-02-04 04:12:24 -08001127 : call_(call), config_() {
solenberg7add0582015-11-20 09:59:34 -08001128 RTC_DCHECK(call);
1129 config_.rtp.remote_ssrc = remote_ssrc;
kwibergd32bf752017-01-19 07:03:59 -08001130 config_.rtp.local_ssrc = local_ssrc;
1131 config_.rtp.transport_cc = use_transport_cc;
1132 config_.rtp.nack.rtp_history_ms = use_nack ? kNackRtpHistoryMs : 0;
1133 config_.rtp.extensions = extensions;
solenberg31fec402016-05-06 02:13:12 -07001134 config_.rtcp_send_transport = rtcp_send_transport;
Fredrik Solenberg8f5787a2018-01-11 13:52:30 +01001135 config_.jitter_buffer_max_packets = jitter_buffer_max_packets;
1136 config_.jitter_buffer_fast_accelerate = jitter_buffer_fast_accelerate;
solenberg7add0582015-11-20 09:59:34 -08001137 config_.sync_group = sync_group;
ossu29b1a8d2016-06-13 07:34:51 -07001138 config_.decoder_factory = decoder_factory;
kwiberg1c07c702017-03-27 07:15:49 -07001139 config_.decoder_map = decoder_map;
kwibergd32bf752017-01-19 07:03:59 -08001140 RecreateAudioReceiveStream();
solenberg7add0582015-11-20 09:59:34 -08001141 }
solenbergc96df772015-10-21 13:01:53 -07001142
solenberg7add0582015-11-20 09:59:34 -08001143 ~WebRtcAudioReceiveStream() {
1144 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
1145 call_->DestroyAudioReceiveStream(stream_);
1146 }
1147
Fredrik Solenberg3b903d02018-01-10 15:17:10 +01001148 void SetLocalSsrc(uint32_t local_ssrc) {
solenberg7add0582015-11-20 09:59:34 -08001149 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
kwibergd32bf752017-01-19 07:03:59 -08001150 config_.rtp.local_ssrc = local_ssrc;
Fredrik Solenberg3b903d02018-01-10 15:17:10 +01001151 ReconfigureAudioReceiveStream();
solenberg7add0582015-11-20 09:59:34 -08001152 }
solenberg8189b022016-06-14 12:13:00 -07001153
Fredrik Solenberg3b903d02018-01-10 15:17:10 +01001154 void SetUseTransportCc(bool use_transport_cc, bool use_nack) {
solenberg7add0582015-11-20 09:59:34 -08001155 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
kwibergd32bf752017-01-19 07:03:59 -08001156 config_.rtp.transport_cc = use_transport_cc;
1157 config_.rtp.nack.rtp_history_ms = use_nack ? kNackRtpHistoryMs : 0;
Fredrik Solenberg3b903d02018-01-10 15:17:10 +01001158 ReconfigureAudioReceiveStream();
solenberg7add0582015-11-20 09:59:34 -08001159 }
1160
Fredrik Solenberg3b903d02018-01-10 15:17:10 +01001161 void SetRtpExtensions(const std::vector<webrtc::RtpExtension>& extensions) {
solenberg4a0f7b52016-06-16 13:07:33 -07001162 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
kwibergd32bf752017-01-19 07:03:59 -08001163 config_.rtp.extensions = extensions;
Fredrik Solenberg3b903d02018-01-10 15:17:10 +01001164 ReconfigureAudioReceiveStream();
kwibergd32bf752017-01-19 07:03:59 -08001165 }
1166
deadbeefcb383672017-04-26 16:28:42 -07001167 // Set a new payload type -> decoder map.
Fredrik Solenberg3b903d02018-01-10 15:17:10 +01001168 void SetDecoderMap(const std::map<int, webrtc::SdpAudioFormat>& decoder_map) {
kwibergd32bf752017-01-19 07:03:59 -08001169 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
kwibergd32bf752017-01-19 07:03:59 -08001170 config_.decoder_map = decoder_map;
Fredrik Solenberg3b903d02018-01-10 15:17:10 +01001171 ReconfigureAudioReceiveStream();
solenberg4a0f7b52016-06-16 13:07:33 -07001172 }
1173
solenberg4904fb62017-02-17 12:01:14 -08001174 void MaybeRecreateAudioReceiveStream(const std::string& sync_group) {
1175 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
1176 if (config_.sync_group != sync_group) {
Fredrik Solenberg3b903d02018-01-10 15:17:10 +01001177 RTC_LOG(LS_INFO) << "Recreating AudioReceiveStream for SSRC="
1178 << config_.rtp.remote_ssrc
1179 << " because of sync group change.";
solenberg4904fb62017-02-17 12:01:14 -08001180 config_.sync_group = sync_group;
1181 RecreateAudioReceiveStream();
1182 }
1183 }
1184
solenberg7add0582015-11-20 09:59:34 -08001185 webrtc::AudioReceiveStream::Stats GetStats() const {
1186 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
1187 RTC_DCHECK(stream_);
1188 return stream_->GetStats();
1189 }
1190
solenberg796b8f92017-03-01 17:02:23 -08001191 int GetOutputLevel() const {
1192 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
1193 RTC_DCHECK(stream_);
1194 return stream_->GetOutputLevel();
1195 }
1196
kwiberg686a8ef2016-02-26 03:00:35 -08001197 void SetRawAudioSink(std::unique_ptr<webrtc::AudioSinkInterface> sink) {
Tommif888bb52015-12-12 01:37:01 +01001198 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
Fredrik Solenberg8f5787a2018-01-11 13:52:30 +01001199 raw_audio_sink_ = std::move(sink);
1200 stream_->SetSink(raw_audio_sink_.get());
Tommif888bb52015-12-12 01:37:01 +01001201 }
1202
solenberg217fb662016-06-17 08:30:54 -07001203 void SetOutputVolume(double volume) {
1204 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
1205 stream_->SetGain(volume);
1206 }
1207
aleloi84ef6152016-08-04 05:28:21 -07001208 void SetPlayout(bool playout) {
1209 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
1210 RTC_DCHECK(stream_);
1211 if (playout) {
aleloi84ef6152016-08-04 05:28:21 -07001212 stream_->Start();
1213 } else {
aleloi84ef6152016-08-04 05:28:21 -07001214 stream_->Stop();
1215 }
aleloi18e0b672016-10-04 02:45:47 -07001216 playout_ = playout;
aleloi84ef6152016-08-04 05:28:21 -07001217 }
1218
hbos8d609f62017-04-10 07:39:05 -07001219 std::vector<webrtc::RtpSource> GetSources() {
1220 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
1221 RTC_DCHECK(stream_);
1222 return stream_->GetSources();
1223 }
1224
solenbergc96df772015-10-21 13:01:53 -07001225 private:
kwibergd32bf752017-01-19 07:03:59 -08001226 void RecreateAudioReceiveStream() {
solenberg7add0582015-11-20 09:59:34 -08001227 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
1228 if (stream_) {
1229 call_->DestroyAudioReceiveStream(stream_);
solenberg7add0582015-11-20 09:59:34 -08001230 }
solenberg7add0582015-11-20 09:59:34 -08001231 stream_ = call_->CreateAudioReceiveStream(config_);
1232 RTC_CHECK(stream_);
aleloi18e0b672016-10-04 02:45:47 -07001233 SetPlayout(playout_);
Fredrik Solenberg8f5787a2018-01-11 13:52:30 +01001234 stream_->SetSink(raw_audio_sink_.get());
solenberg7add0582015-11-20 09:59:34 -08001235 }
1236
Fredrik Solenberg3b903d02018-01-10 15:17:10 +01001237 void ReconfigureAudioReceiveStream() {
1238 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
1239 RTC_DCHECK(stream_);
1240 stream_->Reconfigure(config_);
1241 }
1242
solenberg7add0582015-11-20 09:59:34 -08001243 rtc::ThreadChecker worker_thread_checker_;
1244 webrtc::Call* call_ = nullptr;
1245 webrtc::AudioReceiveStream::Config config_;
1246 // The stream is owned by WebRtcAudioReceiveStream and may be reallocated if
1247 // configuration changes.
1248 webrtc::AudioReceiveStream* stream_ = nullptr;
aleloi18e0b672016-10-04 02:45:47 -07001249 bool playout_ = false;
Fredrik Solenberg8f5787a2018-01-11 13:52:30 +01001250 std::unique_ptr<webrtc::AudioSinkInterface> raw_audio_sink_;
solenbergc96df772015-10-21 13:01:53 -07001251
1252 RTC_DISALLOW_IMPLICIT_CONSTRUCTORS(WebRtcAudioReceiveStream);
henrike@webrtc.org1e09a712013-07-26 19:17:59 +00001253};
1254
Fredrik Solenberg709ed672015-09-15 12:26:33 +02001255WebRtcVoiceMediaChannel::WebRtcVoiceMediaChannel(WebRtcVoiceEngine* engine,
nisse51542be2016-02-12 02:27:06 -08001256 const MediaConfig& config,
Fredrik Solenbergb071a192015-09-17 16:42:56 +02001257 const AudioOptions& options,
Fredrik Solenberg709ed672015-09-15 12:26:33 +02001258 webrtc::Call* call)
nisse51542be2016-02-12 02:27:06 -08001259 : VoiceMediaChannel(config), engine_(engine), call_(call) {
Mirko Bonadei675513b2017-11-09 11:09:25 +01001260 RTC_LOG(LS_VERBOSE) << "WebRtcVoiceMediaChannel::WebRtcVoiceMediaChannel";
solenberg566ef242015-11-06 15:34:49 -08001261 RTC_DCHECK(call);
solenberg0a617e22015-10-20 15:49:38 -07001262 engine->RegisterChannel(this);
Fredrik Solenbergb071a192015-09-17 16:42:56 +02001263 SetOptions(options);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001264}
1265
1266WebRtcVoiceMediaChannel::~WebRtcVoiceMediaChannel() {
solenberg566ef242015-11-06 15:34:49 -08001267 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
Mirko Bonadei675513b2017-11-09 11:09:25 +01001268 RTC_LOG(LS_VERBOSE) << "WebRtcVoiceMediaChannel::~WebRtcVoiceMediaChannel";
solenberg7add0582015-11-20 09:59:34 -08001269 // TODO(solenberg): Should be able to delete the streams directly, without
1270 // going through RemoveNnStream(), once stream objects handle
1271 // all (de)configuration.
solenbergc96df772015-10-21 13:01:53 -07001272 while (!send_streams_.empty()) {
1273 RemoveSendStream(send_streams_.begin()->first);
solenbergd97ec302015-10-07 01:40:33 -07001274 }
solenberg7add0582015-11-20 09:59:34 -08001275 while (!recv_streams_.empty()) {
1276 RemoveRecvStream(recv_streams_.begin()->first);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001277 }
solenberg0a617e22015-10-20 15:49:38 -07001278 engine()->UnregisterChannel(this);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001279}
1280
nisse51542be2016-02-12 02:27:06 -08001281rtc::DiffServCodePoint WebRtcVoiceMediaChannel::PreferredDscp() const {
1282 return kAudioDscpValue;
1283}
1284
Peter Thatcherc2ee2c82015-08-07 16:05:34 -07001285bool WebRtcVoiceMediaChannel::SetSendParameters(
1286 const AudioSendParameters& params) {
Peter Boströmca8b4042016-03-08 14:24:13 -08001287 TRACE_EVENT0("webrtc", "WebRtcVoiceMediaChannel::SetSendParameters");
solenberg566ef242015-11-06 15:34:49 -08001288 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
Mirko Bonadei675513b2017-11-09 11:09:25 +01001289 RTC_LOG(LS_INFO) << "WebRtcVoiceMediaChannel::SetSendParameters: "
1290 << params.ToString();
Peter Thatcherc2ee2c82015-08-07 16:05:34 -07001291 // TODO(pthatcher): Refactor this to be more clean now that we have
1292 // all the information at once.
solenberg3a941542015-11-16 07:34:50 -08001293
1294 if (!SetSendCodecs(params.codecs)) {
1295 return false;
1296 }
1297
solenberg7e4e01a2015-12-02 08:05:01 -08001298 if (!ValidateRtpExtensions(params.extensions)) {
1299 return false;
1300 }
1301 std::vector<webrtc::RtpExtension> filtered_extensions =
1302 FilterRtpExtensions(params.extensions,
1303 webrtc::RtpExtension::IsSupportedForAudio, true);
1304 if (send_rtp_extensions_ != filtered_extensions) {
1305 send_rtp_extensions_.swap(filtered_extensions);
solenberg3a941542015-11-16 07:34:50 -08001306 for (auto& it : send_streams_) {
ossu20a4b3f2017-04-27 02:08:52 -07001307 it.second->SetRtpExtensions(send_rtp_extensions_);
solenberg3a941542015-11-16 07:34:50 -08001308 }
1309 }
1310
deadbeef80346142016-04-27 14:17:10 -07001311 if (!SetMaxSendBitrate(params.max_bandwidth_bps)) {
solenberg3a941542015-11-16 07:34:50 -08001312 return false;
1313 }
1314 return SetOptions(params.options);
Peter Thatcherc2ee2c82015-08-07 16:05:34 -07001315}
1316
1317bool WebRtcVoiceMediaChannel::SetRecvParameters(
1318 const AudioRecvParameters& params) {
Peter Boströmca8b4042016-03-08 14:24:13 -08001319 TRACE_EVENT0("webrtc", "WebRtcVoiceMediaChannel::SetRecvParameters");
solenberg566ef242015-11-06 15:34:49 -08001320 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
Mirko Bonadei675513b2017-11-09 11:09:25 +01001321 RTC_LOG(LS_INFO) << "WebRtcVoiceMediaChannel::SetRecvParameters: "
1322 << params.ToString();
Peter Thatcherc2ee2c82015-08-07 16:05:34 -07001323 // TODO(pthatcher): Refactor this to be more clean now that we have
1324 // all the information at once.
solenberg7add0582015-11-20 09:59:34 -08001325
1326 if (!SetRecvCodecs(params.codecs)) {
1327 return false;
1328 }
1329
solenberg7e4e01a2015-12-02 08:05:01 -08001330 if (!ValidateRtpExtensions(params.extensions)) {
1331 return false;
1332 }
1333 std::vector<webrtc::RtpExtension> filtered_extensions =
1334 FilterRtpExtensions(params.extensions,
1335 webrtc::RtpExtension::IsSupportedForAudio, false);
1336 if (recv_rtp_extensions_ != filtered_extensions) {
1337 recv_rtp_extensions_.swap(filtered_extensions);
solenberg7add0582015-11-20 09:59:34 -08001338 for (auto& it : recv_streams_) {
Fredrik Solenberg3b903d02018-01-10 15:17:10 +01001339 it.second->SetRtpExtensions(recv_rtp_extensions_);
solenberg7add0582015-11-20 09:59:34 -08001340 }
1341 }
solenberg7add0582015-11-20 09:59:34 -08001342 return true;
Peter Thatcherc2ee2c82015-08-07 16:05:34 -07001343}
1344
Taylor Brandstetterdb0cd9e2016-05-16 11:40:30 -07001345webrtc::RtpParameters WebRtcVoiceMediaChannel::GetRtpSendParameters(
skvlade0d46372016-04-07 22:59:22 -07001346 uint32_t ssrc) const {
1347 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
1348 auto it = send_streams_.find(ssrc);
1349 if (it == send_streams_.end()) {
Mirko Bonadei675513b2017-11-09 11:09:25 +01001350 RTC_LOG(LS_WARNING) << "Attempting to get RTP send parameters for stream "
1351 << "with ssrc " << ssrc << " which doesn't exist.";
skvlade0d46372016-04-07 22:59:22 -07001352 return webrtc::RtpParameters();
1353 }
1354
Taylor Brandstetter0cd086b2016-04-20 16:23:10 -07001355 webrtc::RtpParameters rtp_params = it->second->rtp_parameters();
1356 // Need to add the common list of codecs to the send stream-specific
1357 // RTP parameters.
1358 for (const AudioCodec& codec : send_codecs_) {
1359 rtp_params.codecs.push_back(codec.ToCodecParameters());
1360 }
1361 return rtp_params;
skvlade0d46372016-04-07 22:59:22 -07001362}
1363
Taylor Brandstetterdb0cd9e2016-05-16 11:40:30 -07001364bool WebRtcVoiceMediaChannel::SetRtpSendParameters(
skvlade0d46372016-04-07 22:59:22 -07001365 uint32_t ssrc,
1366 const webrtc::RtpParameters& parameters) {
1367 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
skvlade0d46372016-04-07 22:59:22 -07001368 auto it = send_streams_.find(ssrc);
1369 if (it == send_streams_.end()) {
Mirko Bonadei675513b2017-11-09 11:09:25 +01001370 RTC_LOG(LS_WARNING) << "Attempting to set RTP send parameters for stream "
1371 << "with ssrc " << ssrc << " which doesn't exist.";
skvlade0d46372016-04-07 22:59:22 -07001372 return false;
1373 }
1374
Taylor Brandstetterdb0cd9e2016-05-16 11:40:30 -07001375 // TODO(deadbeef): Handle setting parameters with a list of codecs in a
1376 // different order (which should change the send codec).
1377 webrtc::RtpParameters current_parameters = GetRtpSendParameters(ssrc);
1378 if (current_parameters.codecs != parameters.codecs) {
Mirko Bonadei675513b2017-11-09 11:09:25 +01001379 RTC_LOG(LS_ERROR) << "Using SetParameters to change the set of codecs "
1380 << "is not currently supported.";
Taylor Brandstetterdb0cd9e2016-05-16 11:40:30 -07001381 return false;
1382 }
1383
minyue7a973442016-10-20 03:27:12 -07001384 // TODO(minyue): The following legacy actions go into
1385 // |WebRtcAudioSendStream::SetRtpParameters()| which is called at the end,
1386 // though there are two difference:
1387 // 1. |WebRtcVoiceMediaChannel::SetChannelSendParameters()| only calls
1388 // |SetSendCodec| while |WebRtcAudioSendStream::SetRtpParameters()| calls
1389 // |SetSendCodecs|. The outcome should be the same.
1390 // 2. AudioSendStream can be recreated.
1391
Taylor Brandstetter0cd086b2016-04-20 16:23:10 -07001392 // Codecs are handled at the WebRtcVoiceMediaChannel level.
1393 webrtc::RtpParameters reduced_params = parameters;
1394 reduced_params.codecs.clear();
minyue7a973442016-10-20 03:27:12 -07001395 return it->second->SetRtpParameters(reduced_params);
skvlade0d46372016-04-07 22:59:22 -07001396}
1397
Taylor Brandstetterdb0cd9e2016-05-16 11:40:30 -07001398webrtc::RtpParameters WebRtcVoiceMediaChannel::GetRtpReceiveParameters(
1399 uint32_t ssrc) const {
1400 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
deadbeef3bc15102017-04-20 19:25:07 -07001401 webrtc::RtpParameters rtp_params;
1402 // SSRC of 0 represents the default receive stream.
1403 if (ssrc == 0) {
1404 if (!default_sink_) {
Mirko Bonadei675513b2017-11-09 11:09:25 +01001405 RTC_LOG(LS_WARNING)
1406 << "Attempting to get RTP parameters for the default, "
1407 "unsignaled audio receive stream, but not yet "
1408 "configured to receive such a stream.";
deadbeef3bc15102017-04-20 19:25:07 -07001409 return rtp_params;
1410 }
1411 rtp_params.encodings.emplace_back();
1412 } else {
1413 auto it = recv_streams_.find(ssrc);
1414 if (it == recv_streams_.end()) {
Mirko Bonadei675513b2017-11-09 11:09:25 +01001415 RTC_LOG(LS_WARNING)
1416 << "Attempting to get RTP receive parameters for stream "
1417 << "with ssrc " << ssrc << " which doesn't exist.";
deadbeef3bc15102017-04-20 19:25:07 -07001418 return webrtc::RtpParameters();
1419 }
1420 rtp_params.encodings.emplace_back();
1421 // TODO(deadbeef): Return stream-specific parameters.
Oskar Sundbom78807582017-11-16 11:09:55 +01001422 rtp_params.encodings[0].ssrc = ssrc;
Taylor Brandstetterdb0cd9e2016-05-16 11:40:30 -07001423 }
1424
Taylor Brandstetterdb0cd9e2016-05-16 11:40:30 -07001425 for (const AudioCodec& codec : recv_codecs_) {
1426 rtp_params.codecs.push_back(codec.ToCodecParameters());
1427 }
1428 return rtp_params;
1429}
1430
1431bool WebRtcVoiceMediaChannel::SetRtpReceiveParameters(
1432 uint32_t ssrc,
1433 const webrtc::RtpParameters& parameters) {
1434 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
deadbeef3bc15102017-04-20 19:25:07 -07001435 // SSRC of 0 represents the default receive stream.
1436 if (ssrc == 0) {
1437 if (!default_sink_) {
Mirko Bonadei675513b2017-11-09 11:09:25 +01001438 RTC_LOG(LS_WARNING)
1439 << "Attempting to set RTP parameters for the default, "
1440 "unsignaled audio receive stream, but not yet "
1441 "configured to receive such a stream.";
deadbeef3bc15102017-04-20 19:25:07 -07001442 return false;
1443 }
1444 } else {
1445 auto it = recv_streams_.find(ssrc);
1446 if (it == recv_streams_.end()) {
Mirko Bonadei675513b2017-11-09 11:09:25 +01001447 RTC_LOG(LS_WARNING)
1448 << "Attempting to set RTP receive parameters for stream "
1449 << "with ssrc " << ssrc << " which doesn't exist.";
deadbeef3bc15102017-04-20 19:25:07 -07001450 return false;
1451 }
Taylor Brandstetterdb0cd9e2016-05-16 11:40:30 -07001452 }
1453
1454 webrtc::RtpParameters current_parameters = GetRtpReceiveParameters(ssrc);
1455 if (current_parameters != parameters) {
Mirko Bonadei675513b2017-11-09 11:09:25 +01001456 RTC_LOG(LS_ERROR) << "Changing the RTP receive parameters is currently "
1457 << "unsupported.";
Taylor Brandstetterdb0cd9e2016-05-16 11:40:30 -07001458 return false;
1459 }
1460 return true;
1461}
1462
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001463bool WebRtcVoiceMediaChannel::SetOptions(const AudioOptions& options) {
solenberg566ef242015-11-06 15:34:49 -08001464 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
Mirko Bonadei675513b2017-11-09 11:09:25 +01001465 RTC_LOG(LS_INFO) << "Setting voice channel options: " << options.ToString();
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001466
1467 // We retain all of the existing options, and apply the given ones
1468 // on top. This means there is no way to "clear" options such that
1469 // they go back to the engine default.
1470 options_.SetAll(options);
solenberg246b8172015-12-08 09:50:23 -08001471 if (!engine()->ApplyOptions(options_)) {
Mirko Bonadei675513b2017-11-09 11:09:25 +01001472 RTC_LOG(LS_WARNING)
1473 << "Failed to apply engine options during channel SetOptions.";
solenberg246b8172015-12-08 09:50:23 -08001474 return false;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001475 }
minyue6b825df2016-10-31 04:08:32 -07001476
ossu20a4b3f2017-04-27 02:08:52 -07001477 rtc::Optional<std::string> audio_network_adaptor_config =
minyue6b825df2016-10-31 04:08:32 -07001478 GetAudioNetworkAdaptorConfig(options_);
1479 for (auto& it : send_streams_) {
ossu20a4b3f2017-04-27 02:08:52 -07001480 it.second->SetAudioNetworkAdaptorConfig(audio_network_adaptor_config);
minyue6b825df2016-10-31 04:08:32 -07001481 }
1482
Mirko Bonadei675513b2017-11-09 11:09:25 +01001483 RTC_LOG(LS_INFO) << "Set voice channel options. Current options: "
1484 << options_.ToString();
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001485 return true;
1486}
1487
1488bool WebRtcVoiceMediaChannel::SetRecvCodecs(
1489 const std::vector<AudioCodec>& codecs) {
solenberg566ef242015-11-06 15:34:49 -08001490 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
solenberg8fb30c32015-10-13 03:06:58 -07001491
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001492 // Set the payload types to be used for incoming media.
Mirko Bonadei675513b2017-11-09 11:09:25 +01001493 RTC_LOG(LS_INFO) << "Setting receive voice codecs.";
solenberg0b675462015-10-09 01:37:09 -07001494
1495 if (!VerifyUniquePayloadTypes(codecs)) {
Mirko Bonadei675513b2017-11-09 11:09:25 +01001496 RTC_LOG(LS_ERROR) << "Codec payload types overlap.";
solenberg0b675462015-10-09 01:37:09 -07001497 return false;
1498 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001499
kwibergd32bf752017-01-19 07:03:59 -08001500 // Create a payload type -> SdpAudioFormat map with all the decoders. Fail
1501 // unless the factory claims to support all decoders.
1502 std::map<int, webrtc::SdpAudioFormat> decoder_map;
1503 for (const AudioCodec& codec : codecs) {
deadbeefcb383672017-04-26 16:28:42 -07001504 // Log a warning if a codec's payload type is changing. This used to be
1505 // treated as an error. It's abnormal, but not really illegal.
1506 AudioCodec old_codec;
1507 if (FindCodec(recv_codecs_, codec, &old_codec) &&
1508 old_codec.id != codec.id) {
Mirko Bonadei675513b2017-11-09 11:09:25 +01001509 RTC_LOG(LS_WARNING) << codec.name << " mapped to a second payload type ("
1510 << codec.id << ", was already mapped to "
1511 << old_codec.id << ")";
deadbeefcb383672017-04-26 16:28:42 -07001512 }
kwibergd32bf752017-01-19 07:03:59 -08001513 auto format = AudioCodecToSdpAudioFormat(codec);
1514 if (!IsCodec(codec, "cn") && !IsCodec(codec, "telephone-event") &&
1515 !engine()->decoder_factory_->IsSupportedDecoder(format)) {
Mirko Bonadei675513b2017-11-09 11:09:25 +01001516 RTC_LOG(LS_ERROR) << "Unsupported codec: " << format;
kwibergd32bf752017-01-19 07:03:59 -08001517 return false;
1518 }
deadbeefcb383672017-04-26 16:28:42 -07001519 // We allow adding new codecs but don't allow changing the payload type of
1520 // codecs that are already configured since we might already be receiving
1521 // packets with that payload type. See RFC3264, Section 8.3.2.
1522 // TODO(deadbeef): Also need to check for clashes with previously mapped
1523 // payload types, and not just currently mapped ones. For example, this
1524 // should be illegal:
1525 // 1. {100: opus/48000/2, 101: ISAC/16000}
1526 // 2. {100: opus/48000/2}
1527 // 3. {100: opus/48000/2, 101: ISAC/32000}
1528 // Though this check really should happen at a higher level, since this
1529 // conflict could happen between audio and video codecs.
1530 auto existing = decoder_map_.find(codec.id);
1531 if (existing != decoder_map_.end() && !existing->second.Matches(format)) {
Mirko Bonadei675513b2017-11-09 11:09:25 +01001532 RTC_LOG(LS_ERROR) << "Attempting to use payload type " << codec.id
1533 << " for " << codec.name
1534 << ", but it is already used for "
1535 << existing->second.name;
deadbeefcb383672017-04-26 16:28:42 -07001536 return false;
1537 }
kwibergd32bf752017-01-19 07:03:59 -08001538 decoder_map.insert({codec.id, std::move(format)});
1539 }
1540
deadbeefcb383672017-04-26 16:28:42 -07001541 if (decoder_map == decoder_map_) {
1542 // There's nothing new to configure.
1543 return true;
1544 }
1545
kwiberg37b8b112016-11-03 02:46:53 -07001546 if (playout_) {
1547 // Receive codecs can not be changed while playing. So we temporarily
1548 // pause playout.
1549 ChangePlayout(false);
1550 }
1551
kwiberg1c07c702017-03-27 07:15:49 -07001552 decoder_map_ = std::move(decoder_map);
kwibergd32bf752017-01-19 07:03:59 -08001553 for (auto& kv : recv_streams_) {
Fredrik Solenberg3b903d02018-01-10 15:17:10 +01001554 kv.second->SetDecoderMap(decoder_map_);
solenberg26c8c912015-11-27 04:00:25 -08001555 }
kwibergd32bf752017-01-19 07:03:59 -08001556 recv_codecs_ = codecs;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001557
kwiberg37b8b112016-11-03 02:46:53 -07001558 if (desired_playout_ && !playout_) {
1559 ChangePlayout(desired_playout_);
1560 }
kwibergd32bf752017-01-19 07:03:59 -08001561 return true;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001562}
1563
solenberg72e29d22016-03-08 06:35:16 -08001564// Utility function called from SetSendParameters() to extract current send
1565// codec settings from the given list of codecs (originally from SDP). Both send
1566// and receive streams may be reconfigured based on the new settings.
wu@webrtc.orgcadf9042013-08-30 21:24:16 +00001567bool WebRtcVoiceMediaChannel::SetSendCodecs(
1568 const std::vector<AudioCodec>& codecs) {
solenberg566ef242015-11-06 15:34:49 -08001569 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
Oskar Sundbom78807582017-11-16 11:09:55 +01001570 dtmf_payload_type_ = rtc::nullopt;
solenbergffbbcac2016-11-17 05:25:37 -08001571 dtmf_payload_freq_ = -1;
1572
1573 // Validate supplied codecs list.
1574 for (const AudioCodec& codec : codecs) {
1575 // TODO(solenberg): Validate more aspects of input - that payload types
1576 // don't overlap, remove redundant/unsupported codecs etc -
1577 // the same way it is done for RtpHeaderExtensions.
1578 if (codec.id < kMinPayloadType || codec.id > kMaxPayloadType) {
Mirko Bonadei675513b2017-11-09 11:09:25 +01001579 RTC_LOG(LS_WARNING) << "Codec payload type out of range: "
1580 << ToString(codec);
solenbergffbbcac2016-11-17 05:25:37 -08001581 return false;
1582 }
1583 }
1584
1585 // Find PT of telephone-event codec with lowest clockrate, as a fallback, in
1586 // case we don't have a DTMF codec with a rate matching the send codec's, or
1587 // if this function returns early.
1588 std::vector<AudioCodec> dtmf_codecs;
Fredrik Solenbergaf9fb212015-08-26 10:45:53 +02001589 for (const AudioCodec& codec : codecs) {
Fredrik Solenbergaf9fb212015-08-26 10:45:53 +02001590 if (IsCodec(codec, kDtmfCodecName)) {
solenbergffbbcac2016-11-17 05:25:37 -08001591 dtmf_codecs.push_back(codec);
1592 if (!dtmf_payload_type_ || codec.clockrate < dtmf_payload_freq_) {
Oskar Sundbom78807582017-11-16 11:09:55 +01001593 dtmf_payload_type_ = codec.id;
solenbergffbbcac2016-11-17 05:25:37 -08001594 dtmf_payload_freq_ = codec.clockrate;
solenberg31642aa2016-03-14 08:00:37 -07001595 }
wu@webrtc.orgcadf9042013-08-30 21:24:16 +00001596 }
1597 }
1598
ossu20a4b3f2017-04-27 02:08:52 -07001599 // Scan through the list to figure out the codec to use for sending.
1600 rtc::Optional<webrtc::AudioSendStream::Config::SendCodecSpec> send_codec_spec;
stefan1ccf73f2017-03-27 03:51:18 -07001601 webrtc::Call::Config::BitrateConfig bitrate_config;
ossu20a4b3f2017-04-27 02:08:52 -07001602 rtc::Optional<webrtc::AudioCodecInfo> voice_codec_info;
1603 for (const AudioCodec& voice_codec : codecs) {
1604 if (!(IsCodec(voice_codec, kCnCodecName) ||
1605 IsCodec(voice_codec, kDtmfCodecName) ||
1606 IsCodec(voice_codec, kRedCodecName))) {
1607 webrtc::SdpAudioFormat format(voice_codec.name, voice_codec.clockrate,
1608 voice_codec.channels, voice_codec.params);
solenberg72e29d22016-03-08 06:35:16 -08001609
ossu20a4b3f2017-04-27 02:08:52 -07001610 voice_codec_info = engine()->encoder_factory_->QueryAudioEncoder(format);
1611 if (!voice_codec_info) {
Mirko Bonadei675513b2017-11-09 11:09:25 +01001612 RTC_LOG(LS_WARNING) << "Unknown codec " << ToString(voice_codec);
solenberg72e29d22016-03-08 06:35:16 -08001613 continue;
1614 }
1615
Oskar Sundbom78807582017-11-16 11:09:55 +01001616 send_codec_spec = webrtc::AudioSendStream::Config::SendCodecSpec(
1617 voice_codec.id, format);
ossu20a4b3f2017-04-27 02:08:52 -07001618 if (voice_codec.bitrate > 0) {
Oskar Sundbom78807582017-11-16 11:09:55 +01001619 send_codec_spec->target_bitrate_bps = voice_codec.bitrate;
ossu20a4b3f2017-04-27 02:08:52 -07001620 }
1621 send_codec_spec->transport_cc_enabled = HasTransportCc(voice_codec);
1622 send_codec_spec->nack_enabled = HasNack(voice_codec);
1623 bitrate_config = GetBitrateConfigForCodec(voice_codec);
1624 break;
1625 }
1626 }
1627
1628 if (!send_codec_spec) {
1629 return false;
1630 }
1631
1632 RTC_DCHECK(voice_codec_info);
1633 if (voice_codec_info->allow_comfort_noise) {
1634 // Loop through the codecs list again to find the CN codec.
1635 // TODO(solenberg): Break out into a separate function?
1636 for (const AudioCodec& cn_codec : codecs) {
ossu0c4b8492017-03-02 11:03:25 -08001637 if (IsCodec(cn_codec, kCnCodecName) &&
ossu20a4b3f2017-04-27 02:08:52 -07001638 cn_codec.clockrate == send_codec_spec->format.clockrate_hz) {
ossu0c4b8492017-03-02 11:03:25 -08001639 switch (cn_codec.clockrate) {
solenberg72e29d22016-03-08 06:35:16 -08001640 case 8000:
1641 case 16000:
1642 case 32000:
Oskar Sundbom78807582017-11-16 11:09:55 +01001643 send_codec_spec->cng_payload_type = cn_codec.id;
solenberg72e29d22016-03-08 06:35:16 -08001644 break;
1645 default:
Mirko Bonadei675513b2017-11-09 11:09:25 +01001646 RTC_LOG(LS_WARNING)
1647 << "CN frequency " << cn_codec.clockrate << " not supported.";
ossu20a4b3f2017-04-27 02:08:52 -07001648 break;
solenberg72e29d22016-03-08 06:35:16 -08001649 }
solenberg72e29d22016-03-08 06:35:16 -08001650 break;
1651 }
1652 }
solenbergffbbcac2016-11-17 05:25:37 -08001653
1654 // Find the telephone-event PT exactly matching the preferred send codec.
1655 for (const AudioCodec& dtmf_codec : dtmf_codecs) {
ossu20a4b3f2017-04-27 02:08:52 -07001656 if (dtmf_codec.clockrate == send_codec_spec->format.clockrate_hz) {
Oskar Sundbom78807582017-11-16 11:09:55 +01001657 dtmf_payload_type_ = dtmf_codec.id;
solenbergffbbcac2016-11-17 05:25:37 -08001658 dtmf_payload_freq_ = dtmf_codec.clockrate;
1659 break;
1660 }
1661 }
solenberg72e29d22016-03-08 06:35:16 -08001662 }
1663
solenberg971cab02016-06-14 10:02:41 -07001664 if (send_codec_spec_ != send_codec_spec) {
1665 send_codec_spec_ = std::move(send_codec_spec);
stefan13f1a0a2016-11-30 07:22:58 -08001666 // Apply new settings to all streams.
solenberg971cab02016-06-14 10:02:41 -07001667 for (const auto& kv : send_streams_) {
ossu20a4b3f2017-04-27 02:08:52 -07001668 kv.second->SetSendCodecSpec(*send_codec_spec_);
wu@webrtc.orgcadf9042013-08-30 21:24:16 +00001669 }
stefan13f1a0a2016-11-30 07:22:58 -08001670 } else {
1671 // If the codec isn't changing, set the start bitrate to -1 which means
1672 // "unchanged" so that BWE isn't affected.
stefan1ccf73f2017-03-27 03:51:18 -07001673 bitrate_config.start_bitrate_bps = -1;
wu@webrtc.orgcadf9042013-08-30 21:24:16 +00001674 }
stefan1ccf73f2017-03-27 03:51:18 -07001675 call_->SetBitrateConfig(bitrate_config);
wu@webrtc.orgcadf9042013-08-30 21:24:16 +00001676
solenberg8189b022016-06-14 12:13:00 -07001677 // Check if the transport cc feedback or NACK status has changed on the
1678 // preferred send codec, and in that case reconfigure all receive streams.
ossu20a4b3f2017-04-27 02:08:52 -07001679 if (recv_transport_cc_enabled_ != send_codec_spec_->transport_cc_enabled ||
1680 recv_nack_enabled_ != send_codec_spec_->nack_enabled) {
Mirko Bonadei675513b2017-11-09 11:09:25 +01001681 RTC_LOG(LS_INFO) << "Recreate all the receive streams because the send "
1682 "codec has changed.";
ossu20a4b3f2017-04-27 02:08:52 -07001683 recv_transport_cc_enabled_ = send_codec_spec_->transport_cc_enabled;
1684 recv_nack_enabled_ = send_codec_spec_->nack_enabled;
solenberg72e29d22016-03-08 06:35:16 -08001685 for (auto& kv : recv_streams_) {
Fredrik Solenberg3b903d02018-01-10 15:17:10 +01001686 kv.second->SetUseTransportCc(recv_transport_cc_enabled_,
1687 recv_nack_enabled_);
solenberg72e29d22016-03-08 06:35:16 -08001688 }
1689 }
1690
Taylor Brandstetter0cd086b2016-04-20 16:23:10 -07001691 send_codecs_ = codecs;
solenberg72e29d22016-03-08 06:35:16 -08001692 return true;
1693}
1694
aleloi84ef6152016-08-04 05:28:21 -07001695void WebRtcVoiceMediaChannel::SetPlayout(bool playout) {
kwiberg37b8b112016-11-03 02:46:53 -07001696 desired_playout_ = playout;
1697 return ChangePlayout(desired_playout_);
1698}
1699
1700void WebRtcVoiceMediaChannel::ChangePlayout(bool playout) {
1701 TRACE_EVENT0("webrtc", "WebRtcVoiceMediaChannel::ChangePlayout");
solenberg566ef242015-11-06 15:34:49 -08001702 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001703 if (playout_ == playout) {
aleloi84ef6152016-08-04 05:28:21 -07001704 return;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001705 }
1706
aleloi84ef6152016-08-04 05:28:21 -07001707 for (const auto& kv : recv_streams_) {
1708 kv.second->SetPlayout(playout);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001709 }
solenberg1ac56142015-10-13 03:58:19 -07001710 playout_ = playout;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001711}
1712
Taylor Brandstetter1a018dc2016-03-08 12:37:39 -08001713void WebRtcVoiceMediaChannel::SetSend(bool send) {
Peter Boströmca8b4042016-03-08 14:24:13 -08001714 TRACE_EVENT0("webrtc", "WebRtcVoiceMediaChannel::SetSend");
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001715 if (send_ == send) {
Taylor Brandstetter1a018dc2016-03-08 12:37:39 -08001716 return;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001717 }
1718
solenbergd53a3f92016-04-14 13:56:37 -07001719 // Apply channel specific options, and initialize the ADM for recording (this
1720 // may take time on some platforms, e.g. Android).
Taylor Brandstetter1a018dc2016-03-08 12:37:39 -08001721 if (send) {
solenberg63b34542015-09-29 06:06:31 -07001722 engine()->ApplyOptions(options_);
solenbergd53a3f92016-04-14 13:56:37 -07001723
1724 // InitRecording() may return an error if the ADM is already recording.
1725 if (!engine()->adm()->RecordingIsInitialized() &&
1726 !engine()->adm()->Recording()) {
1727 if (engine()->adm()->InitRecording() != 0) {
Mirko Bonadei675513b2017-11-09 11:09:25 +01001728 RTC_LOG(LS_WARNING) << "Failed to initialize recording";
solenbergd53a3f92016-04-14 13:56:37 -07001729 }
1730 }
solenberg63b34542015-09-29 06:06:31 -07001731 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001732
wu@webrtc.org9dba5252013-08-05 20:36:57 +00001733 // Change the settings on each send channel.
Taylor Brandstetter1a018dc2016-03-08 12:37:39 -08001734 for (auto& kv : send_streams_) {
1735 kv.second->SetSend(send);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001736 }
wu@webrtc.org9dba5252013-08-05 20:36:57 +00001737
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001738 send_ = send;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001739}
1740
Peter Boström0c4e06b2015-10-07 12:23:21 +02001741bool WebRtcVoiceMediaChannel::SetAudioSend(uint32_t ssrc,
1742 bool enable,
solenberg1dd98f32015-09-10 01:57:14 -07001743 const AudioOptions* options,
Taylor Brandstetter1a018dc2016-03-08 12:37:39 -08001744 AudioSource* source) {
solenberg566ef242015-11-06 15:34:49 -08001745 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
solenberg1dd98f32015-09-10 01:57:14 -07001746 // TODO(solenberg): The state change should be fully rolled back if any one of
1747 // these calls fail.
Taylor Brandstetter1a018dc2016-03-08 12:37:39 -08001748 if (!SetLocalSource(ssrc, source)) {
solenberg1dd98f32015-09-10 01:57:14 -07001749 return false;
1750 }
solenbergdfc8f4f2015-10-01 02:31:10 -07001751 if (!MuteStream(ssrc, !enable)) {
solenberg1dd98f32015-09-10 01:57:14 -07001752 return false;
1753 }
solenbergdfc8f4f2015-10-01 02:31:10 -07001754 if (enable && options) {
solenberg1dd98f32015-09-10 01:57:14 -07001755 return SetOptions(*options);
1756 }
1757 return true;
1758}
1759
wu@webrtc.org9dba5252013-08-05 20:36:57 +00001760bool WebRtcVoiceMediaChannel::AddSendStream(const StreamParams& sp) {
Peter Boströmca8b4042016-03-08 14:24:13 -08001761 TRACE_EVENT0("webrtc", "WebRtcVoiceMediaChannel::AddSendStream");
solenberg566ef242015-11-06 15:34:49 -08001762 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
Mirko Bonadei675513b2017-11-09 11:09:25 +01001763 RTC_LOG(LS_INFO) << "AddSendStream: " << sp.ToString();
solenberg0a617e22015-10-20 15:49:38 -07001764
1765 uint32_t ssrc = sp.first_ssrc();
1766 RTC_DCHECK(0 != ssrc);
1767
Fredrik Solenberg8f5787a2018-01-11 13:52:30 +01001768 if (send_streams_.find(ssrc) != send_streams_.end()) {
Mirko Bonadei675513b2017-11-09 11:09:25 +01001769 RTC_LOG(LS_ERROR) << "Stream already exists with ssrc " << ssrc;
wu@webrtc.org9dba5252013-08-05 20:36:57 +00001770 return false;
1771 }
1772
minyue6b825df2016-10-31 04:08:32 -07001773 rtc::Optional<std::string> audio_network_adaptor_config =
1774 GetAudioNetworkAdaptorConfig(options_);
skvlade0d46372016-04-07 22:59:22 -07001775 WebRtcAudioSendStream* stream = new WebRtcAudioSendStream(
Fredrik Solenberg8f5787a2018-01-11 13:52:30 +01001776 ssrc,
1777 sp.cname,
1778 sp.id,
1779 send_codec_spec_,
1780 send_rtp_extensions_,
1781 max_send_bitrate_bps_,
1782 audio_network_adaptor_config,
1783 call_,
1784 this,
Fredrik Solenberg2a877972017-12-15 16:42:15 +01001785 engine()->encoder_factory_);
skvlade0d46372016-04-07 22:59:22 -07001786 send_streams_.insert(std::make_pair(ssrc, stream));
wu@webrtc.org9dba5252013-08-05 20:36:57 +00001787
solenberg4a0f7b52016-06-16 13:07:33 -07001788 // At this point the stream's local SSRC has been updated. If it is the first
1789 // send stream, make sure that all the receive streams are updated with the
1790 // same SSRC in order to send receiver reports.
solenbergc96df772015-10-21 13:01:53 -07001791 if (send_streams_.size() == 1) {
solenberg0a617e22015-10-20 15:49:38 -07001792 receiver_reports_ssrc_ = ssrc;
solenberg4a0f7b52016-06-16 13:07:33 -07001793 for (const auto& kv : recv_streams_) {
1794 // TODO(solenberg): Allow applications to set the RTCP SSRC of receive
Fredrik Solenberg3b903d02018-01-10 15:17:10 +01001795 // streams instead, so we can avoid reconfiguring the streams here.
1796 kv.second->SetLocalSsrc(ssrc);
wu@webrtc.org9dba5252013-08-05 20:36:57 +00001797 }
1798 }
1799
Taylor Brandstetter1a018dc2016-03-08 12:37:39 -08001800 send_streams_[ssrc]->SetSend(send_);
1801 return true;
wu@webrtc.org9dba5252013-08-05 20:36:57 +00001802}
1803
Peter Boström0c4e06b2015-10-07 12:23:21 +02001804bool WebRtcVoiceMediaChannel::RemoveSendStream(uint32_t ssrc) {
Peter Boströmca8b4042016-03-08 14:24:13 -08001805 TRACE_EVENT0("webrtc", "WebRtcVoiceMediaChannel::RemoveSendStream");
solenberg566ef242015-11-06 15:34:49 -08001806 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
Mirko Bonadei675513b2017-11-09 11:09:25 +01001807 RTC_LOG(LS_INFO) << "RemoveSendStream: " << ssrc;
solenberg3a941542015-11-16 07:34:50 -08001808
solenbergc96df772015-10-21 13:01:53 -07001809 auto it = send_streams_.find(ssrc);
1810 if (it == send_streams_.end()) {
Mirko Bonadei675513b2017-11-09 11:09:25 +01001811 RTC_LOG(LS_WARNING) << "Try to remove stream with ssrc " << ssrc
1812 << " which doesn't exist.";
wu@webrtc.org9dba5252013-08-05 20:36:57 +00001813 return false;
1814 }
1815
Taylor Brandstetter1a018dc2016-03-08 12:37:39 -08001816 it->second->SetSend(false);
wu@webrtc.org9dba5252013-08-05 20:36:57 +00001817
solenberg7602aab2016-11-14 11:30:07 -08001818 // TODO(solenberg): If we're removing the receiver_reports_ssrc_ stream, find
1819 // the first active send stream and use that instead, reassociating receive
1820 // streams.
1821
solenberg7add0582015-11-20 09:59:34 -08001822 delete it->second;
1823 send_streams_.erase(it);
solenbergc96df772015-10-21 13:01:53 -07001824 if (send_streams_.empty()) {
Taylor Brandstetter1a018dc2016-03-08 12:37:39 -08001825 SetSend(false);
solenberg0a617e22015-10-20 15:49:38 -07001826 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001827 return true;
1828}
1829
1830bool WebRtcVoiceMediaChannel::AddRecvStream(const StreamParams& sp) {
Peter Boströmca8b4042016-03-08 14:24:13 -08001831 TRACE_EVENT0("webrtc", "WebRtcVoiceMediaChannel::AddRecvStream");
solenberg566ef242015-11-06 15:34:49 -08001832 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
Mirko Bonadei675513b2017-11-09 11:09:25 +01001833 RTC_LOG(LS_INFO) << "AddRecvStream: " << sp.ToString();
solenbergd97ec302015-10-07 01:40:33 -07001834
solenberg0b675462015-10-09 01:37:09 -07001835 if (!ValidateStreamParams(sp)) {
wu@webrtc.org78187522013-10-07 23:32:02 +00001836 return false;
1837 }
1838
solenberg7add0582015-11-20 09:59:34 -08001839 const uint32_t ssrc = sp.first_ssrc();
solenberg0b675462015-10-09 01:37:09 -07001840 if (ssrc == 0) {
Mirko Bonadei675513b2017-11-09 11:09:25 +01001841 RTC_LOG(LS_WARNING) << "AddRecvStream with ssrc==0 is not supported.";
solenberg0b675462015-10-09 01:37:09 -07001842 return false;
1843 }
1844
solenberg2100c0b2017-03-01 11:29:29 -08001845 // If this stream was previously received unsignaled, we promote it, possibly
1846 // recreating the AudioReceiveStream, if sync_label has changed.
1847 if (MaybeDeregisterUnsignaledRecvStream(ssrc)) {
solenberg4904fb62017-02-17 12:01:14 -08001848 recv_streams_[ssrc]->MaybeRecreateAudioReceiveStream(sp.sync_label);
solenberg4904fb62017-02-17 12:01:14 -08001849 return true;
solenberg1ac56142015-10-13 03:58:19 -07001850 }
solenberg0b675462015-10-09 01:37:09 -07001851
Fredrik Solenberg8f5787a2018-01-11 13:52:30 +01001852 if (recv_streams_.find(ssrc) != recv_streams_.end()) {
Mirko Bonadei675513b2017-11-09 11:09:25 +01001853 RTC_LOG(LS_ERROR) << "Stream already exists with ssrc " << ssrc;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001854 return false;
1855 }
Fredrik Solenberg4b60c732015-05-07 14:07:48 +02001856
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001857 // Create a new channel for receiving audio data.
stefanba4c0e42016-02-04 04:12:24 -08001858 recv_streams_.insert(std::make_pair(
kwiberg1c07c702017-03-27 07:15:49 -07001859 ssrc,
1860 new WebRtcAudioReceiveStream(
Fredrik Solenberg8f5787a2018-01-11 13:52:30 +01001861 ssrc,
1862 receiver_reports_ssrc_,
1863 recv_transport_cc_enabled_,
1864 recv_nack_enabled_,
1865 sp.sync_label,
1866 recv_rtp_extensions_,
1867 call_,
1868 this,
1869 engine()->decoder_factory_,
1870 decoder_map_,
1871 engine()->audio_jitter_buffer_max_packets_,
1872 engine()->audio_jitter_buffer_fast_accelerate_)));
aleloi84ef6152016-08-04 05:28:21 -07001873 recv_streams_[ssrc]->SetPlayout(playout_);
solenberg7add0582015-11-20 09:59:34 -08001874
solenberg1ac56142015-10-13 03:58:19 -07001875 return true;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001876}
1877
Peter Boström0c4e06b2015-10-07 12:23:21 +02001878bool WebRtcVoiceMediaChannel::RemoveRecvStream(uint32_t ssrc) {
Peter Boströmca8b4042016-03-08 14:24:13 -08001879 TRACE_EVENT0("webrtc", "WebRtcVoiceMediaChannel::RemoveRecvStream");
solenberg566ef242015-11-06 15:34:49 -08001880 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
Mirko Bonadei675513b2017-11-09 11:09:25 +01001881 RTC_LOG(LS_INFO) << "RemoveRecvStream: " << ssrc;
solenbergd97ec302015-10-07 01:40:33 -07001882
solenberg7add0582015-11-20 09:59:34 -08001883 const auto it = recv_streams_.find(ssrc);
1884 if (it == recv_streams_.end()) {
Mirko Bonadei675513b2017-11-09 11:09:25 +01001885 RTC_LOG(LS_WARNING) << "Try to remove stream with ssrc " << ssrc
1886 << " which doesn't exist.";
henrike@webrtc.org1e09a712013-07-26 19:17:59 +00001887 return false;
wu@webrtc.org9dba5252013-08-05 20:36:57 +00001888 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001889
solenberg2100c0b2017-03-01 11:29:29 -08001890 MaybeDeregisterUnsignaledRecvStream(ssrc);
henrike@webrtc.org1e09a712013-07-26 19:17:59 +00001891
Tommif888bb52015-12-12 01:37:01 +01001892 it->second->SetRawAudioSink(nullptr);
solenberg7add0582015-11-20 09:59:34 -08001893 delete it->second;
1894 recv_streams_.erase(it);
Fredrik Solenberg8f5787a2018-01-11 13:52:30 +01001895 return true;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001896}
1897
Taylor Brandstetter1a018dc2016-03-08 12:37:39 -08001898bool WebRtcVoiceMediaChannel::SetLocalSource(uint32_t ssrc,
1899 AudioSource* source) {
solenbergc96df772015-10-21 13:01:53 -07001900 auto it = send_streams_.find(ssrc);
1901 if (it == send_streams_.end()) {
Taylor Brandstetter1a018dc2016-03-08 12:37:39 -08001902 if (source) {
1903 // Return an error if trying to set a valid source with an invalid ssrc.
Mirko Bonadei675513b2017-11-09 11:09:25 +01001904 RTC_LOG(LS_ERROR) << "SetLocalSource failed with ssrc " << ssrc;
wu@webrtc.org9dba5252013-08-05 20:36:57 +00001905 return false;
1906 }
1907
1908 // The channel likely has gone away, do nothing.
henrike@webrtc.org1e09a712013-07-26 19:17:59 +00001909 return true;
henrike@webrtc.org1e09a712013-07-26 19:17:59 +00001910 }
1911
Taylor Brandstetter1a018dc2016-03-08 12:37:39 -08001912 if (source) {
1913 it->second->SetSource(source);
solenberg1ac56142015-10-13 03:58:19 -07001914 } else {
Taylor Brandstetter1a018dc2016-03-08 12:37:39 -08001915 it->second->ClearSource();
solenberg1ac56142015-10-13 03:58:19 -07001916 }
henrike@webrtc.org1e09a712013-07-26 19:17:59 +00001917
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001918 return true;
1919}
1920
solenberg796b8f92017-03-01 17:02:23 -08001921// TODO(solenberg): Remove, once AudioMonitor is gone.
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001922bool WebRtcVoiceMediaChannel::GetActiveStreams(
1923 AudioInfo::StreamList* actives) {
solenberg566ef242015-11-06 15:34:49 -08001924 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001925 actives->clear();
solenberg7add0582015-11-20 09:59:34 -08001926 for (const auto& ch : recv_streams_) {
solenberg796b8f92017-03-01 17:02:23 -08001927 int level = ch.second->GetOutputLevel();
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001928 if (level > 0) {
Fredrik Solenbergaf9fb212015-08-26 10:45:53 +02001929 actives->push_back(std::make_pair(ch.first, level));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001930 }
1931 }
1932 return true;
1933}
1934
solenberg796b8f92017-03-01 17:02:23 -08001935// TODO(solenberg): Remove, once AudioMonitor is gone.
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001936int WebRtcVoiceMediaChannel::GetOutputLevel() {
solenberg566ef242015-11-06 15:34:49 -08001937 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
solenberg1ac56142015-10-13 03:58:19 -07001938 int highest = 0;
solenberg7add0582015-11-20 09:59:34 -08001939 for (const auto& ch : recv_streams_) {
solenberg796b8f92017-03-01 17:02:23 -08001940 highest = std::max(ch.second->GetOutputLevel(), highest);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001941 }
1942 return highest;
1943}
1944
solenberg4bac9c52015-10-09 02:32:53 -07001945bool WebRtcVoiceMediaChannel::SetOutputVolume(uint32_t ssrc, double volume) {
solenberg566ef242015-11-06 15:34:49 -08001946 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
solenberg2100c0b2017-03-01 11:29:29 -08001947 std::vector<uint32_t> ssrcs(1, ssrc);
deadbeef3bc15102017-04-20 19:25:07 -07001948 // SSRC of 0 represents the default receive stream.
solenberg1ac56142015-10-13 03:58:19 -07001949 if (ssrc == 0) {
1950 default_recv_volume_ = volume;
solenberg2100c0b2017-03-01 11:29:29 -08001951 ssrcs = unsignaled_recv_ssrcs_;
1952 }
1953 for (uint32_t ssrc : ssrcs) {
1954 const auto it = recv_streams_.find(ssrc);
1955 if (it == recv_streams_.end()) {
Mirko Bonadei675513b2017-11-09 11:09:25 +01001956 RTC_LOG(LS_WARNING) << "SetOutputVolume: no recv stream " << ssrc;
solenberg2100c0b2017-03-01 11:29:29 -08001957 return false;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001958 }
solenberg2100c0b2017-03-01 11:29:29 -08001959 it->second->SetOutputVolume(volume);
Mirko Bonadei675513b2017-11-09 11:09:25 +01001960 RTC_LOG(LS_INFO) << "SetOutputVolume() to " << volume
1961 << " for recv stream with ssrc " << ssrc;
solenberg1ac56142015-10-13 03:58:19 -07001962 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001963 return true;
1964}
1965
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001966bool WebRtcVoiceMediaChannel::CanInsertDtmf() {
Fredrik Solenbergb5727682015-12-04 15:22:19 +01001967 return dtmf_payload_type_ ? true : false;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001968}
1969
solenberg1d63dd02015-12-02 12:35:09 -08001970bool WebRtcVoiceMediaChannel::InsertDtmf(uint32_t ssrc, int event,
1971 int duration) {
solenberg566ef242015-11-06 15:34:49 -08001972 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
Mirko Bonadei675513b2017-11-09 11:09:25 +01001973 RTC_LOG(LS_INFO) << "WebRtcVoiceMediaChannel::InsertDtmf";
Fredrik Solenbergb5727682015-12-04 15:22:19 +01001974 if (!dtmf_payload_type_) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001975 return false;
1976 }
1977
Fredrik Solenbergb5727682015-12-04 15:22:19 +01001978 // Figure out which WebRtcAudioSendStream to send the event on.
1979 auto it = ssrc != 0 ? send_streams_.find(ssrc) : send_streams_.begin();
1980 if (it == send_streams_.end()) {
Mirko Bonadei675513b2017-11-09 11:09:25 +01001981 RTC_LOG(LS_WARNING) << "The specified ssrc " << ssrc << " is not in use.";
solenberg1d63dd02015-12-02 12:35:09 -08001982 return false;
1983 }
Fredrik Solenbergb5727682015-12-04 15:22:19 +01001984 if (event < kMinTelephoneEventCode ||
1985 event > kMaxTelephoneEventCode) {
Mirko Bonadei675513b2017-11-09 11:09:25 +01001986 RTC_LOG(LS_WARNING) << "DTMF event code " << event << " out of range.";
solenberg1d63dd02015-12-02 12:35:09 -08001987 return false;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001988 }
solenbergffbbcac2016-11-17 05:25:37 -08001989 RTC_DCHECK_NE(-1, dtmf_payload_freq_);
1990 return it->second->SendTelephoneEvent(*dtmf_payload_type_, dtmf_payload_freq_,
1991 event, duration);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001992}
1993
wu@webrtc.orga9890802013-12-13 00:21:03 +00001994void WebRtcVoiceMediaChannel::OnPacketReceived(
jbaucheec21bd2016-03-20 06:15:43 -07001995 rtc::CopyOnWriteBuffer* packet, const rtc::PacketTime& packet_time) {
solenberg566ef242015-11-06 15:34:49 -08001996 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
Fredrik Solenberg4b60c732015-05-07 14:07:48 +02001997
mflodman3d7db262016-04-29 00:57:13 -07001998 const webrtc::PacketTime webrtc_packet_time(packet_time.timestamp,
1999 packet_time.not_before);
2000 webrtc::PacketReceiver::DeliveryStatus delivery_result =
Danil Chapovalov292a73e2017-12-07 17:00:40 +01002001 call_->Receiver()->DeliverPacket(webrtc::MediaType::AUDIO, *packet,
mflodman3d7db262016-04-29 00:57:13 -07002002 webrtc_packet_time);
mflodman3d7db262016-04-29 00:57:13 -07002003 if (delivery_result != webrtc::PacketReceiver::DELIVERY_UNKNOWN_SSRC) {
2004 return;
2005 }
2006
solenberg2100c0b2017-03-01 11:29:29 -08002007 // Create an unsignaled receive stream for this previously not received ssrc.
2008 // If there already is N unsignaled receive streams, delete the oldest.
mflodman3d7db262016-04-29 00:57:13 -07002009 // See: https://bugs.chromium.org/p/webrtc/issues/detail?id=5208
solenberg1ac56142015-10-13 03:58:19 -07002010 uint32_t ssrc = 0;
jbaucheec21bd2016-03-20 06:15:43 -07002011 if (!GetRtpSsrc(packet->cdata(), packet->size(), &ssrc)) {
solenberg1ac56142015-10-13 03:58:19 -07002012 return;
2013 }
solenberg2100c0b2017-03-01 11:29:29 -08002014 RTC_DCHECK(std::find(unsignaled_recv_ssrcs_.begin(),
2015 unsignaled_recv_ssrcs_.end(), ssrc) == unsignaled_recv_ssrcs_.end());
solenberg1ac56142015-10-13 03:58:19 -07002016
solenberg2100c0b2017-03-01 11:29:29 -08002017 // Add new stream.
mflodman3d7db262016-04-29 00:57:13 -07002018 StreamParams sp;
2019 sp.ssrcs.push_back(ssrc);
Mirko Bonadei675513b2017-11-09 11:09:25 +01002020 RTC_LOG(LS_INFO) << "Creating unsignaled receive stream for SSRC=" << ssrc;
mflodman3d7db262016-04-29 00:57:13 -07002021 if (!AddRecvStream(sp)) {
Mirko Bonadei675513b2017-11-09 11:09:25 +01002022 RTC_LOG(LS_WARNING) << "Could not create unsignaled receive stream.";
mflodman3d7db262016-04-29 00:57:13 -07002023 return;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002024 }
solenberg2100c0b2017-03-01 11:29:29 -08002025 unsignaled_recv_ssrcs_.push_back(ssrc);
2026 RTC_HISTOGRAM_COUNTS_LINEAR(
2027 "WebRTC.Audio.NumOfUnsignaledStreams", unsignaled_recv_ssrcs_.size(), 1,
2028 100, 101);
solenbergf748ca42017-02-06 13:03:19 -08002029
solenberg2100c0b2017-03-01 11:29:29 -08002030 // Remove oldest unsignaled stream, if we have too many.
2031 if (unsignaled_recv_ssrcs_.size() > kMaxUnsignaledRecvStreams) {
2032 uint32_t remove_ssrc = unsignaled_recv_ssrcs_.front();
Mirko Bonadei675513b2017-11-09 11:09:25 +01002033 RTC_LOG(LS_INFO) << "Removing unsignaled receive stream with SSRC="
2034 << remove_ssrc;
solenberg2100c0b2017-03-01 11:29:29 -08002035 RemoveRecvStream(remove_ssrc);
2036 }
2037 RTC_DCHECK_GE(kMaxUnsignaledRecvStreams, unsignaled_recv_ssrcs_.size());
2038
2039 SetOutputVolume(ssrc, default_recv_volume_);
2040
2041 // The default sink can only be attached to one stream at a time, so we hook
2042 // it up to the *latest* unsignaled stream we've seen, in order to support the
2043 // case where the SSRC of one unsignaled stream changes.
mflodman3d7db262016-04-29 00:57:13 -07002044 if (default_sink_) {
solenberg2100c0b2017-03-01 11:29:29 -08002045 for (uint32_t drop_ssrc : unsignaled_recv_ssrcs_) {
2046 auto it = recv_streams_.find(drop_ssrc);
2047 it->second->SetRawAudioSink(nullptr);
2048 }
mflodman3d7db262016-04-29 00:57:13 -07002049 std::unique_ptr<webrtc::AudioSinkInterface> proxy_sink(
2050 new ProxySink(default_sink_.get()));
solenberg2100c0b2017-03-01 11:29:29 -08002051 SetRawAudioSink(ssrc, std::move(proxy_sink));
mflodman3d7db262016-04-29 00:57:13 -07002052 }
solenberg2100c0b2017-03-01 11:29:29 -08002053
Danil Chapovalov292a73e2017-12-07 17:00:40 +01002054 delivery_result = call_->Receiver()->DeliverPacket(
2055 webrtc::MediaType::AUDIO, *packet, webrtc_packet_time);
mflodman3d7db262016-04-29 00:57:13 -07002056 RTC_DCHECK_NE(webrtc::PacketReceiver::DELIVERY_UNKNOWN_SSRC, delivery_result);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002057}
2058
wu@webrtc.orga9890802013-12-13 00:21:03 +00002059void WebRtcVoiceMediaChannel::OnRtcpReceived(
jbaucheec21bd2016-03-20 06:15:43 -07002060 rtc::CopyOnWriteBuffer* packet, const rtc::PacketTime& packet_time) {
solenberg566ef242015-11-06 15:34:49 -08002061 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
Fredrik Solenberg4b60c732015-05-07 14:07:48 +02002062
Fredrik Solenberg709ed672015-09-15 12:26:33 +02002063 // Forward packet to Call as well.
2064 const webrtc::PacketTime webrtc_packet_time(packet_time.timestamp,
2065 packet_time.not_before);
Danil Chapovalov292a73e2017-12-07 17:00:40 +01002066 call_->Receiver()->DeliverPacket(webrtc::MediaType::AUDIO, *packet,
2067 webrtc_packet_time);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002068}
2069
Honghai Zhangcc411c02016-03-29 17:27:21 -07002070void WebRtcVoiceMediaChannel::OnNetworkRouteChanged(
2071 const std::string& transport_name,
Honghai Zhang0e533ef2016-04-19 15:41:36 -07002072 const rtc::NetworkRoute& network_route) {
Zhi Huang5f5918f2017-11-12 17:26:23 -08002073 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
2074 // TODO(zhihaung): Merge these two callbacks.
Honghai Zhang0e533ef2016-04-19 15:41:36 -07002075 call_->OnNetworkRouteChanged(transport_name, network_route);
Zhi Huang5f5918f2017-11-12 17:26:23 -08002076 call_->OnTransportOverheadChanged(webrtc::MediaType::AUDIO,
2077 network_route.packet_overhead);
Honghai Zhangcc411c02016-03-29 17:27:21 -07002078}
2079
Peter Boström0c4e06b2015-10-07 12:23:21 +02002080bool WebRtcVoiceMediaChannel::MuteStream(uint32_t ssrc, bool muted) {
solenberg566ef242015-11-06 15:34:49 -08002081 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
solenberg94218532016-06-16 10:53:22 -07002082 const auto it = send_streams_.find(ssrc);
2083 if (it == send_streams_.end()) {
Mirko Bonadei675513b2017-11-09 11:09:25 +01002084 RTC_LOG(LS_WARNING) << "The specified ssrc " << ssrc << " is not in use.";
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002085 return false;
2086 }
solenberg94218532016-06-16 10:53:22 -07002087 it->second->SetMuted(muted);
2088
2089 // TODO(solenberg):
buildbot@webrtc.org6b21b712014-07-31 15:08:53 +00002090 // We set the AGC to mute state only when all the channels are muted.
2091 // This implementation is not ideal, instead we should signal the AGC when
2092 // the mic channel is muted/unmuted. We can't do it today because there
2093 // is no good way to know which stream is mapping to the mic channel.
2094 bool all_muted = muted;
solenberg94218532016-06-16 10:53:22 -07002095 for (const auto& kv : send_streams_) {
2096 all_muted = all_muted && kv.second->muted();
buildbot@webrtc.org6b21b712014-07-31 15:08:53 +00002097 }
solenberg059fb442016-10-26 05:12:24 -07002098 engine()->apm()->set_output_will_be_muted(all_muted);
buildbot@webrtc.org6b21b712014-07-31 15:08:53 +00002099
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002100 return true;
2101}
2102
deadbeef80346142016-04-27 14:17:10 -07002103bool WebRtcVoiceMediaChannel::SetMaxSendBitrate(int bps) {
Mirko Bonadei675513b2017-11-09 11:09:25 +01002104 RTC_LOG(LS_INFO) << "WebRtcVoiceMediaChannel::SetMaxSendBitrate.";
deadbeef80346142016-04-27 14:17:10 -07002105 max_send_bitrate_bps_ = bps;
minyue7a973442016-10-20 03:27:12 -07002106 bool success = true;
skvlade0d46372016-04-07 22:59:22 -07002107 for (const auto& kv : send_streams_) {
minyue7a973442016-10-20 03:27:12 -07002108 if (!kv.second->SetMaxSendBitrate(max_send_bitrate_bps_)) {
2109 success = false;
skvlade0d46372016-04-07 22:59:22 -07002110 }
2111 }
minyue7a973442016-10-20 03:27:12 -07002112 return success;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002113}
2114
skvlad7a43d252016-03-22 15:32:27 -07002115void WebRtcVoiceMediaChannel::OnReadyToSend(bool ready) {
2116 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
Mirko Bonadei675513b2017-11-09 11:09:25 +01002117 RTC_LOG(LS_VERBOSE) << "OnReadyToSend: " << (ready ? "Ready." : "Not ready.");
skvlad7a43d252016-03-22 15:32:27 -07002118 call_->SignalChannelNetworkState(
2119 webrtc::MediaType::AUDIO,
2120 ready ? webrtc::kNetworkUp : webrtc::kNetworkDown);
2121}
2122
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002123bool WebRtcVoiceMediaChannel::GetStats(VoiceMediaInfo* info) {
Peter Boströmca8b4042016-03-08 14:24:13 -08002124 TRACE_EVENT0("webrtc", "WebRtcVoiceMediaChannel::GetStats");
solenberg566ef242015-11-06 15:34:49 -08002125 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
solenberg85a04962015-10-27 03:35:21 -07002126 RTC_DCHECK(info);
solenbergd97ec302015-10-07 01:40:33 -07002127
solenberg85a04962015-10-27 03:35:21 -07002128 // Get SSRC and stats for each sender.
hbos1acfbd22016-11-17 23:43:29 -08002129 RTC_DCHECK_EQ(info->senders.size(), 0U);
solenberg85a04962015-10-27 03:35:21 -07002130 for (const auto& stream : send_streams_) {
Ivo Creusen56d46092017-11-24 17:29:59 +01002131 webrtc::AudioSendStream::Stats stats =
2132 stream.second->GetStats(recv_streams_.size() > 0);
wu@webrtc.org9dba5252013-08-05 20:36:57 +00002133 VoiceSenderInfo sinfo;
solenberg85a04962015-10-27 03:35:21 -07002134 sinfo.add_ssrc(stats.local_ssrc);
2135 sinfo.bytes_sent = stats.bytes_sent;
2136 sinfo.packets_sent = stats.packets_sent;
2137 sinfo.packets_lost = stats.packets_lost;
2138 sinfo.fraction_lost = stats.fraction_lost;
2139 sinfo.codec_name = stats.codec_name;
hbos1acfbd22016-11-17 23:43:29 -08002140 sinfo.codec_payload_type = stats.codec_payload_type;
solenberg85a04962015-10-27 03:35:21 -07002141 sinfo.ext_seqnum = stats.ext_seqnum;
2142 sinfo.jitter_ms = stats.jitter_ms;
2143 sinfo.rtt_ms = stats.rtt_ms;
2144 sinfo.audio_level = stats.audio_level;
zsteine76bd3a2017-07-14 12:17:49 -07002145 sinfo.total_input_energy = stats.total_input_energy;
2146 sinfo.total_input_duration = stats.total_input_duration;
Taylor Brandstetter1a018dc2016-03-08 12:37:39 -08002147 sinfo.typing_noise_detected = (send_ ? stats.typing_noise_detected : false);
ivoce1198e02017-09-08 08:13:19 -07002148 sinfo.ana_statistics = stats.ana_statistics;
Ivo Creusen56d46092017-11-24 17:29:59 +01002149 sinfo.apm_statistics = stats.apm_statistics;
wu@webrtc.org9dba5252013-08-05 20:36:57 +00002150 info->senders.push_back(sinfo);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002151 }
2152
solenberg85a04962015-10-27 03:35:21 -07002153 // Get SSRC and stats for each receiver.
hbos1acfbd22016-11-17 23:43:29 -08002154 RTC_DCHECK_EQ(info->receivers.size(), 0U);
solenberg7add0582015-11-20 09:59:34 -08002155 for (const auto& stream : recv_streams_) {
deadbeef4e2deab2017-09-20 13:56:21 -07002156 uint32_t ssrc = stream.first;
2157 // When SSRCs are unsignaled, there's only one audio MediaStreamTrack, but
2158 // multiple RTP streams can be received over time (if the SSRC changes for
2159 // whatever reason). We only want the RTCMediaStreamTrackStats to represent
2160 // the stats for the most recent stream (the one whose audio is actually
2161 // routed to the MediaStreamTrack), so here we ignore any unsignaled SSRCs
2162 // except for the most recent one (last in the vector). This is somewhat of
2163 // a hack, and means you don't get *any* stats for these inactive streams,
2164 // but it's slightly better than the previous behavior, which was "highest
2165 // SSRC wins".
2166 // See: https://bugs.chromium.org/p/webrtc/issues/detail?id=8158
2167 if (!unsignaled_recv_ssrcs_.empty()) {
2168 auto end_it = --unsignaled_recv_ssrcs_.end();
2169 if (std::find(unsignaled_recv_ssrcs_.begin(), end_it, ssrc) != end_it) {
2170 continue;
2171 }
2172 }
Fredrik Solenberg4f4ec0a2015-10-22 10:49:27 +02002173 webrtc::AudioReceiveStream::Stats stats = stream.second->GetStats();
2174 VoiceReceiverInfo rinfo;
2175 rinfo.add_ssrc(stats.remote_ssrc);
2176 rinfo.bytes_rcvd = stats.bytes_rcvd;
2177 rinfo.packets_rcvd = stats.packets_rcvd;
2178 rinfo.packets_lost = stats.packets_lost;
2179 rinfo.fraction_lost = stats.fraction_lost;
2180 rinfo.codec_name = stats.codec_name;
hbos1acfbd22016-11-17 23:43:29 -08002181 rinfo.codec_payload_type = stats.codec_payload_type;
Fredrik Solenberg4f4ec0a2015-10-22 10:49:27 +02002182 rinfo.ext_seqnum = stats.ext_seqnum;
2183 rinfo.jitter_ms = stats.jitter_ms;
2184 rinfo.jitter_buffer_ms = stats.jitter_buffer_ms;
2185 rinfo.jitter_buffer_preferred_ms = stats.jitter_buffer_preferred_ms;
2186 rinfo.delay_estimate_ms = stats.delay_estimate_ms;
2187 rinfo.audio_level = stats.audio_level;
zsteine76bd3a2017-07-14 12:17:49 -07002188 rinfo.total_output_energy = stats.total_output_energy;
Steve Anton2dbc69f2017-08-24 17:15:13 -07002189 rinfo.total_samples_received = stats.total_samples_received;
zsteine76bd3a2017-07-14 12:17:49 -07002190 rinfo.total_output_duration = stats.total_output_duration;
Steve Anton2dbc69f2017-08-24 17:15:13 -07002191 rinfo.concealed_samples = stats.concealed_samples;
Gustaf Ullberg9a2e9062017-09-18 09:28:20 +02002192 rinfo.concealment_events = stats.concealment_events;
Gustaf Ullbergb0a02072017-10-02 12:00:34 +02002193 rinfo.jitter_buffer_delay_seconds = stats.jitter_buffer_delay_seconds;
Fredrik Solenberg4f4ec0a2015-10-22 10:49:27 +02002194 rinfo.expand_rate = stats.expand_rate;
2195 rinfo.speech_expand_rate = stats.speech_expand_rate;
2196 rinfo.secondary_decoded_rate = stats.secondary_decoded_rate;
minyue-webrtc0e320ec2017-08-28 13:51:27 +02002197 rinfo.secondary_discarded_rate = stats.secondary_discarded_rate;
Fredrik Solenberg4f4ec0a2015-10-22 10:49:27 +02002198 rinfo.accelerate_rate = stats.accelerate_rate;
2199 rinfo.preemptive_expand_rate = stats.preemptive_expand_rate;
2200 rinfo.decoding_calls_to_silence_generator =
2201 stats.decoding_calls_to_silence_generator;
2202 rinfo.decoding_calls_to_neteq = stats.decoding_calls_to_neteq;
2203 rinfo.decoding_normal = stats.decoding_normal;
2204 rinfo.decoding_plc = stats.decoding_plc;
2205 rinfo.decoding_cng = stats.decoding_cng;
2206 rinfo.decoding_plc_cng = stats.decoding_plc_cng;
henrik.lundin63489782016-09-20 01:47:12 -07002207 rinfo.decoding_muted_output = stats.decoding_muted_output;
Fredrik Solenberg4f4ec0a2015-10-22 10:49:27 +02002208 rinfo.capture_start_ntp_time_ms = stats.capture_start_ntp_time_ms;
2209 info->receivers.push_back(rinfo);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002210 }
2211
hbos1acfbd22016-11-17 23:43:29 -08002212 // Get codec info
2213 for (const AudioCodec& codec : send_codecs_) {
2214 webrtc::RtpCodecParameters codec_params = codec.ToCodecParameters();
2215 info->send_codecs.insert(
2216 std::make_pair(codec_params.payload_type, std::move(codec_params)));
2217 }
2218 for (const AudioCodec& codec : recv_codecs_) {
2219 webrtc::RtpCodecParameters codec_params = codec.ToCodecParameters();
2220 info->receive_codecs.insert(
2221 std::make_pair(codec_params.payload_type, std::move(codec_params)));
2222 }
2223
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002224 return true;
2225}
2226
Tommif888bb52015-12-12 01:37:01 +01002227void WebRtcVoiceMediaChannel::SetRawAudioSink(
2228 uint32_t ssrc,
kwiberg686a8ef2016-02-26 03:00:35 -08002229 std::unique_ptr<webrtc::AudioSinkInterface> sink) {
Tommif888bb52015-12-12 01:37:01 +01002230 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
Mirko Bonadei675513b2017-11-09 11:09:25 +01002231 RTC_LOG(LS_VERBOSE) << "WebRtcVoiceMediaChannel::SetRawAudioSink: ssrc:"
2232 << ssrc << " " << (sink ? "(ptr)" : "NULL");
deadbeef884f5852016-01-15 09:20:04 -08002233 if (ssrc == 0) {
solenberg2100c0b2017-03-01 11:29:29 -08002234 if (!unsignaled_recv_ssrcs_.empty()) {
kwiberg686a8ef2016-02-26 03:00:35 -08002235 std::unique_ptr<webrtc::AudioSinkInterface> proxy_sink(
deadbeef884f5852016-01-15 09:20:04 -08002236 sink ? new ProxySink(sink.get()) : nullptr);
solenberg2100c0b2017-03-01 11:29:29 -08002237 SetRawAudioSink(unsignaled_recv_ssrcs_.back(), std::move(proxy_sink));
deadbeef884f5852016-01-15 09:20:04 -08002238 }
2239 default_sink_ = std::move(sink);
2240 return;
2241 }
Tommif888bb52015-12-12 01:37:01 +01002242 const auto it = recv_streams_.find(ssrc);
2243 if (it == recv_streams_.end()) {
Mirko Bonadei675513b2017-11-09 11:09:25 +01002244 RTC_LOG(LS_WARNING) << "SetRawAudioSink: no recv stream " << ssrc;
Tommif888bb52015-12-12 01:37:01 +01002245 return;
2246 }
deadbeef2d110be2016-01-13 12:00:26 -08002247 it->second->SetRawAudioSink(std::move(sink));
Tommif888bb52015-12-12 01:37:01 +01002248}
2249
hbos8d609f62017-04-10 07:39:05 -07002250std::vector<webrtc::RtpSource> WebRtcVoiceMediaChannel::GetSources(
2251 uint32_t ssrc) const {
2252 auto it = recv_streams_.find(ssrc);
Zhi Huangfa266ef2017-12-13 10:27:46 -08002253 if (it == recv_streams_.end()) {
2254 RTC_LOG(LS_ERROR) << "Attempting to get contributing sources for SSRC:"
2255 << ssrc << " which doesn't exist.";
2256 return std::vector<webrtc::RtpSource>();
2257 }
hbos8d609f62017-04-10 07:39:05 -07002258 return it->second->GetSources();
2259}
2260
solenberg2100c0b2017-03-01 11:29:29 -08002261bool WebRtcVoiceMediaChannel::
2262 MaybeDeregisterUnsignaledRecvStream(uint32_t ssrc) {
2263 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
2264 auto it = std::find(unsignaled_recv_ssrcs_.begin(),
2265 unsignaled_recv_ssrcs_.end(),
2266 ssrc);
2267 if (it != unsignaled_recv_ssrcs_.end()) {
2268 unsignaled_recv_ssrcs_.erase(it);
2269 return true;
2270 }
2271 return false;
2272}
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002273} // namespace cricket
2274
2275#endif // HAVE_WEBRTC_VOICE