blob: df50642ea6b8a76bff1cea34796522eb4bedc6a1 [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
Karl Wiberg08126342018-03-20 19:18:55 +010022#include "api/audio_codecs/audio_codec_pair_id.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020023#include "api/call/audio_sink.h"
24#include "media/base/audiosource.h"
25#include "media/base/mediaconstants.h"
26#include "media/base/streamparams.h"
27#include "media/engine/adm_helpers.h"
28#include "media/engine/apm_helpers.h"
29#include "media/engine/payload_type_mapper.h"
30#include "media/engine/webrtcmediaengine.h"
Fredrik Solenbergd3195342017-11-21 20:33:05 +010031#include "modules/audio_device/audio_device_impl.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020032#include "modules/audio_mixer/audio_mixer_impl.h"
33#include "modules/audio_processing/aec_dump/aec_dump_factory.h"
34#include "modules/audio_processing/include/audio_processing.h"
35#include "rtc_base/arraysize.h"
36#include "rtc_base/base64.h"
37#include "rtc_base/byteorder.h"
38#include "rtc_base/constructormagic.h"
39#include "rtc_base/helpers.h"
40#include "rtc_base/logging.h"
41#include "rtc_base/race_checker.h"
42#include "rtc_base/stringencode.h"
Jonas Olssonabbe8412018-04-03 13:40:05 +020043#include "rtc_base/strings/audio_format_to_string.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020044#include "rtc_base/stringutils.h"
45#include "rtc_base/trace_event.h"
46#include "system_wrappers/include/field_trial.h"
47#include "system_wrappers/include/metrics.h"
henrike@webrtc.org28e20752013-07-10 00:45:36 +000048
henrike@webrtc.org28e20752013-07-10 00:45:36 +000049namespace cricket {
solenbergd97ec302015-10-07 01:40:33 -070050namespace {
henrike@webrtc.org28e20752013-07-10 00:45:36 +000051
solenberg418b7d32017-06-13 00:38:27 -070052constexpr size_t kMaxUnsignaledRecvStreams = 4;
solenberg2100c0b2017-03-01 11:29:29 -080053
solenberg971cab02016-06-14 10:02:41 -070054constexpr int kNackRtpHistoryMs = 5000;
minyue@webrtc.org2dc6f312014-10-31 05:33:10 +000055
peah1bcfce52016-08-26 07:16:04 -070056// Check to verify that the define for the intelligibility enhancer is properly
57// set.
58#if !defined(WEBRTC_INTELLIGIBILITY_ENHANCER) || \
59 (WEBRTC_INTELLIGIBILITY_ENHANCER != 0 && \
60 WEBRTC_INTELLIGIBILITY_ENHANCER != 1)
61#error "Set WEBRTC_INTELLIGIBILITY_ENHANCER to either 0 or 1"
62#endif
63
ossu20a4b3f2017-04-27 02:08:52 -070064// For SendSideBwe, Opus bitrate should be in the range between 6000 and 32000.
minyue10cbb462016-11-07 09:29:22 -080065const int kOpusMinBitrateBps = 6000;
ossu20a4b3f2017-04-27 02:08:52 -070066const int kOpusBitrateFbBps = 32000;
deadbeef80346142016-04-27 14:17:10 -070067
wu@webrtc.orgde305012013-10-31 15:40:38 +000068// Default audio dscp value.
69// See http://tools.ietf.org/html/rfc2474 for details.
70// See also http://tools.ietf.org/html/draft-jennings-rtcweb-qos-00
solenbergd97ec302015-10-07 01:40:33 -070071const rtc::DiffServCodePoint kAudioDscpValue = rtc::DSCP_EF;
henrike@webrtc.org1e09a712013-07-26 19:17:59 +000072
Fredrik Solenbergb5727682015-12-04 15:22:19 +010073const int kMinTelephoneEventCode = 0; // RFC4733 (Section 2.3.1)
74const int kMaxTelephoneEventCode = 255;
Fredrik Solenbergb5727682015-12-04 15:22:19 +010075
solenberg31642aa2016-03-14 08:00:37 -070076const int kMinPayloadType = 0;
77const int kMaxPayloadType = 127;
78
deadbeef884f5852016-01-15 09:20:04 -080079class ProxySink : public webrtc::AudioSinkInterface {
80 public:
Steve Antone78bcb92017-10-31 09:53:08 -070081 explicit ProxySink(AudioSinkInterface* sink) : sink_(sink) {
82 RTC_DCHECK(sink);
83 }
deadbeef884f5852016-01-15 09:20:04 -080084
85 void OnData(const Data& audio) override { sink_->OnData(audio); }
86
87 private:
88 webrtc::AudioSinkInterface* sink_;
89};
90
solenberg0b675462015-10-09 01:37:09 -070091bool ValidateStreamParams(const StreamParams& sp) {
92 if (sp.ssrcs.empty()) {
Mirko Bonadei675513b2017-11-09 11:09:25 +010093 RTC_LOG(LS_ERROR) << "No SSRCs in stream parameters: " << sp.ToString();
solenberg0b675462015-10-09 01:37:09 -070094 return false;
95 }
96 if (sp.ssrcs.size() > 1) {
Mirko Bonadei675513b2017-11-09 11:09:25 +010097 RTC_LOG(LS_ERROR) << "Multiple SSRCs in stream parameters: "
98 << sp.ToString();
solenberg0b675462015-10-09 01:37:09 -070099 return false;
100 }
101 return true;
102}
103
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000104// Dumps an AudioCodec in RFC 2327-ish format.
solenbergd97ec302015-10-07 01:40:33 -0700105std::string ToString(const AudioCodec& codec) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000106 std::stringstream ss;
ossu20a4b3f2017-04-27 02:08:52 -0700107 ss << codec.name << "/" << codec.clockrate << "/" << codec.channels;
108 if (!codec.params.empty()) {
109 ss << " {";
110 for (const auto& param : codec.params) {
111 ss << " " << param.first << "=" << param.second;
112 }
113 ss << " }";
114 }
115 ss << " (" << codec.id << ")";
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000116 return ss.str();
117}
Minyue Li7100dcd2015-03-27 05:05:59 +0100118
solenbergd97ec302015-10-07 01:40:33 -0700119bool IsCodec(const AudioCodec& codec, const char* ref_name) {
Minyue Li7100dcd2015-03-27 05:05:59 +0100120 return (_stricmp(codec.name.c_str(), ref_name) == 0);
121}
122
solenbergd97ec302015-10-07 01:40:33 -0700123bool FindCodec(const std::vector<AudioCodec>& codecs,
solenberg26c8c912015-11-27 04:00:25 -0800124 const AudioCodec& codec,
125 AudioCodec* found_codec) {
Fredrik Solenbergaf9fb212015-08-26 10:45:53 +0200126 for (const AudioCodec& c : codecs) {
127 if (c.Matches(codec)) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000128 if (found_codec != NULL) {
Fredrik Solenbergaf9fb212015-08-26 10:45:53 +0200129 *found_codec = c;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000130 }
131 return true;
132 }
133 }
134 return false;
135}
wu@webrtc.org1d1ffc92013-10-16 18:12:02 +0000136
solenberg0b675462015-10-09 01:37:09 -0700137bool VerifyUniquePayloadTypes(const std::vector<AudioCodec>& codecs) {
138 if (codecs.empty()) {
139 return true;
140 }
141 std::vector<int> payload_types;
142 for (const AudioCodec& codec : codecs) {
143 payload_types.push_back(codec.id);
144 }
145 std::sort(payload_types.begin(), payload_types.end());
146 auto it = std::unique(payload_types.begin(), payload_types.end());
147 return it == payload_types.end();
148}
149
minyue6b825df2016-10-31 04:08:32 -0700150rtc::Optional<std::string> GetAudioNetworkAdaptorConfig(
151 const AudioOptions& options) {
152 if (options.audio_network_adaptor && *options.audio_network_adaptor &&
153 options.audio_network_adaptor_config) {
154 // Turn on audio network adaptor only when |options_.audio_network_adaptor|
155 // equals true and |options_.audio_network_adaptor_config| has a value.
156 return options.audio_network_adaptor_config;
157 }
Oskar Sundbom78807582017-11-16 11:09:55 +0100158 return rtc::nullopt;
minyue6b825df2016-10-31 04:08:32 -0700159}
160
deadbeefe702b302017-02-04 12:09:01 -0800161// |max_send_bitrate_bps| is the bitrate from "b=" in SDP.
162// |rtp_max_bitrate_bps| is the bitrate from RtpSender::SetParameters.
minyue7a973442016-10-20 03:27:12 -0700163rtc::Optional<int> ComputeSendBitrate(int max_send_bitrate_bps,
deadbeefe702b302017-02-04 12:09:01 -0800164 rtc::Optional<int> rtp_max_bitrate_bps,
ossu20a4b3f2017-04-27 02:08:52 -0700165 const webrtc::AudioCodecSpec& spec) {
deadbeefe702b302017-02-04 12:09:01 -0800166 // If application-configured bitrate is set, take minimum of that and SDP
167 // bitrate.
zsteina5e0df62017-06-14 11:41:48 -0700168 const int bps =
169 rtp_max_bitrate_bps
170 ? webrtc::MinPositive(max_send_bitrate_bps, *rtp_max_bitrate_bps)
171 : max_send_bitrate_bps;
minyue7a973442016-10-20 03:27:12 -0700172 if (bps <= 0) {
Oskar Sundbom78807582017-11-16 11:09:55 +0100173 return spec.info.default_bitrate_bps;
solenberg971cab02016-06-14 10:02:41 -0700174 }
minyue7a973442016-10-20 03:27:12 -0700175
ossu20a4b3f2017-04-27 02:08:52 -0700176 if (bps < spec.info.min_bitrate_bps) {
minyue7a973442016-10-20 03:27:12 -0700177 // If codec is not multi-rate and |bps| is less than the fixed bitrate then
178 // fail. If codec is not multi-rate and |bps| exceeds or equal the fixed
179 // bitrate then ignore.
Mirko Bonadei675513b2017-11-09 11:09:25 +0100180 RTC_LOG(LS_ERROR) << "Failed to set codec " << spec.format.name
181 << " to bitrate " << bps << " bps"
182 << ", requires at least " << spec.info.min_bitrate_bps
183 << " bps.";
Oskar Sundbom78807582017-11-16 11:09:55 +0100184 return rtc::nullopt;
solenberg971cab02016-06-14 10:02:41 -0700185 }
ossu20a4b3f2017-04-27 02:08:52 -0700186
187 if (spec.info.HasFixedBitrate()) {
Oskar Sundbom78807582017-11-16 11:09:55 +0100188 return spec.info.default_bitrate_bps;
ossu20a4b3f2017-04-27 02:08:52 -0700189 } else {
190 // If codec is multi-rate then just set the bitrate.
Oskar Sundbom78807582017-11-16 11:09:55 +0100191 return std::min(bps, spec.info.max_bitrate_bps);
ossu20a4b3f2017-04-27 02:08:52 -0700192 }
solenberg971cab02016-06-14 10:02:41 -0700193}
194
solenberg76377c52017-02-21 00:54:31 -0800195} // namespace
solenberg971cab02016-06-14 10:02:41 -0700196
ossu29b1a8d2016-06-13 07:34:51 -0700197WebRtcVoiceEngine::WebRtcVoiceEngine(
198 webrtc::AudioDeviceModule* adm,
ossueb1fde42017-05-02 06:46:30 -0700199 const rtc::scoped_refptr<webrtc::AudioEncoderFactory>& encoder_factory,
gyzhou95aa9642016-12-13 14:06:26 -0800200 const rtc::scoped_refptr<webrtc::AudioDecoderFactory>& decoder_factory,
peaha9cc40b2017-06-29 08:32:09 -0700201 rtc::scoped_refptr<webrtc::AudioMixer> audio_mixer,
202 rtc::scoped_refptr<webrtc::AudioProcessing> audio_processing)
deadbeefeb02c032017-06-15 08:29:25 -0700203 : adm_(adm),
ossueb1fde42017-05-02 06:46:30 -0700204 encoder_factory_(encoder_factory),
ossu20a4b3f2017-04-27 02:08:52 -0700205 decoder_factory_(decoder_factory),
deadbeefeb02c032017-06-15 08:29:25 -0700206 audio_mixer_(audio_mixer),
Fredrik Solenberg8f5787a2018-01-11 13:52:30 +0100207 apm_(audio_processing) {
deadbeefeb02c032017-06-15 08:29:25 -0700208 // This may be called from any thread, so detach thread checkers.
209 worker_thread_checker_.DetachFromThread();
solenberg26c8c912015-11-27 04:00:25 -0800210 signal_thread_checker_.DetachFromThread();
Mirko Bonadei675513b2017-11-09 11:09:25 +0100211 RTC_LOG(LS_INFO) << "WebRtcVoiceEngine::WebRtcVoiceEngine";
deadbeefeb02c032017-06-15 08:29:25 -0700212 RTC_DCHECK(decoder_factory);
213 RTC_DCHECK(encoder_factory);
peaha9cc40b2017-06-29 08:32:09 -0700214 RTC_DCHECK(audio_processing);
deadbeefeb02c032017-06-15 08:29:25 -0700215 // The rest of our initialization will happen in Init.
216}
217
218WebRtcVoiceEngine::~WebRtcVoiceEngine() {
219 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
Mirko Bonadei675513b2017-11-09 11:09:25 +0100220 RTC_LOG(LS_INFO) << "WebRtcVoiceEngine::~WebRtcVoiceEngine";
deadbeefeb02c032017-06-15 08:29:25 -0700221 if (initialized_) {
222 StopAecDump();
Fredrik Solenbergd3195342017-11-21 20:33:05 +0100223
224 // Stop AudioDevice.
225 adm()->StopPlayout();
226 adm()->StopRecording();
227 adm()->RegisterAudioCallback(nullptr);
228 adm()->Terminate();
deadbeefeb02c032017-06-15 08:29:25 -0700229 }
230}
231
232void WebRtcVoiceEngine::Init() {
233 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
Mirko Bonadei675513b2017-11-09 11:09:25 +0100234 RTC_LOG(LS_INFO) << "WebRtcVoiceEngine::Init";
deadbeefeb02c032017-06-15 08:29:25 -0700235
236 // TaskQueue expects to be created/destroyed on the same thread.
237 low_priority_worker_queue_.reset(
238 new rtc::TaskQueue("rtc-low-prio", rtc::TaskQueue::Priority::LOW));
239
ossueb1fde42017-05-02 06:46:30 -0700240 // Load our audio codec lists.
Mirko Bonadei675513b2017-11-09 11:09:25 +0100241 RTC_LOG(LS_INFO) << "Supported send codecs in order of preference:";
ossu20a4b3f2017-04-27 02:08:52 -0700242 send_codecs_ = CollectCodecs(encoder_factory_->GetSupportedEncoders());
ossuc54071d2016-08-17 02:45:41 -0700243 for (const AudioCodec& codec : send_codecs_) {
Mirko Bonadei675513b2017-11-09 11:09:25 +0100244 RTC_LOG(LS_INFO) << ToString(codec);
ossuc54071d2016-08-17 02:45:41 -0700245 }
246
Mirko Bonadei675513b2017-11-09 11:09:25 +0100247 RTC_LOG(LS_INFO) << "Supported recv codecs in order of preference:";
ossu20a4b3f2017-04-27 02:08:52 -0700248 recv_codecs_ = CollectCodecs(decoder_factory_->GetSupportedDecoders());
ossuc54071d2016-08-17 02:45:41 -0700249 for (const AudioCodec& codec : recv_codecs_) {
Mirko Bonadei675513b2017-11-09 11:09:25 +0100250 RTC_LOG(LS_INFO) << ToString(codec);
buildbot@webrtc.org13d67762014-05-02 17:33:29 +0000251 }
buildbot@webrtc.org13d67762014-05-02 17:33:29 +0000252
Fredrik Solenbergd3195342017-11-21 20:33:05 +0100253#if defined(WEBRTC_INCLUDE_INTERNAL_AUDIO_DEVICE)
254 // No ADM supplied? Create a default one.
solenbergff976312016-03-30 23:28:51 -0700255 if (!adm_) {
Fredrik Solenbergd3195342017-11-21 20:33:05 +0100256 adm_ = webrtc::AudioDeviceModule::Create(
257 webrtc::AudioDeviceModule::kPlatformDefaultAudio);
solenbergff976312016-03-30 23:28:51 -0700258 }
Fredrik Solenbergd3195342017-11-21 20:33:05 +0100259#endif // WEBRTC_INCLUDE_INTERNAL_AUDIO_DEVICE
260 RTC_CHECK(adm());
261 webrtc::adm_helpers::Init(adm());
Fredrik Solenberg55900fd2017-11-23 20:22:55 +0100262 webrtc::apm_helpers::Init(apm());
Fredrik Solenberg2a877972017-12-15 16:42:15 +0100263
264 // Set up AudioState.
265 {
266 webrtc::AudioState::Config config;
Fredrik Solenberg2a877972017-12-15 16:42:15 +0100267 if (audio_mixer_) {
268 config.audio_mixer = audio_mixer_;
269 } else {
270 config.audio_mixer = webrtc::AudioMixerImpl::Create();
271 }
272 config.audio_processing = apm_;
273 config.audio_device_module = adm_;
274 audio_state_ = webrtc::AudioState::Create(config);
275 }
276
277 // Connect the ADM to our audio path.
278 adm()->RegisterAudioCallback(audio_state()->audio_transport());
solenberg76377c52017-02-21 00:54:31 -0800279
buildbot@webrtc.org13d67762014-05-02 17:33:29 +0000280 // Save the default AGC configuration settings. This must happen before
solenberg246b8172015-12-08 09:50:23 -0800281 // calling ApplyOptions or the default will be overwritten.
peaha9cc40b2017-06-29 08:32:09 -0700282 default_agc_config_ = webrtc::apm_helpers::GetAgcConfig(apm());
buildbot@webrtc.org13d67762014-05-02 17:33:29 +0000283
solenberg0f7d2932016-01-15 01:40:39 -0800284 // Set default engine options.
285 {
286 AudioOptions options;
Oskar Sundbom78807582017-11-16 11:09:55 +0100287 options.echo_cancellation = true;
288 options.auto_gain_control = true;
289 options.noise_suppression = true;
290 options.highpass_filter = true;
291 options.stereo_swapping = false;
292 options.audio_jitter_buffer_max_packets = 50;
293 options.audio_jitter_buffer_fast_accelerate = false;
294 options.typing_detection = true;
Oskar Sundbom78807582017-11-16 11:09:55 +0100295 options.experimental_agc = false;
296 options.extended_filter_aec = false;
297 options.delay_agnostic_aec = false;
298 options.experimental_ns = false;
299 options.intelligibility_enhancer = false;
Oskar Sundbom78807582017-11-16 11:09:55 +0100300 options.residual_echo_detector = true;
solenbergff976312016-03-30 23:28:51 -0700301 bool error = ApplyOptions(options);
302 RTC_DCHECK(error);
buildbot@webrtc.org13d67762014-05-02 17:33:29 +0000303 }
304
deadbeefeb02c032017-06-15 08:29:25 -0700305 initialized_ = true;
buildbot@webrtc.org13d67762014-05-02 17:33:29 +0000306}
307
solenberg566ef242015-11-06 15:34:49 -0800308rtc::scoped_refptr<webrtc::AudioState>
309 WebRtcVoiceEngine::GetAudioState() const {
310 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
311 return audio_state_;
312}
313
nisse51542be2016-02-12 02:27:06 -0800314VoiceMediaChannel* WebRtcVoiceEngine::CreateChannel(
315 webrtc::Call* call,
316 const MediaConfig& config,
Jelena Marusicc28a8962015-05-29 15:05:44 +0200317 const AudioOptions& options) {
solenberg566ef242015-11-06 15:34:49 -0800318 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
nisse51542be2016-02-12 02:27:06 -0800319 return new WebRtcVoiceMediaChannel(this, config, options, call);
buildbot@webrtc.org13d67762014-05-02 17:33:29 +0000320}
321
buildbot@webrtc.org13d67762014-05-02 17:33:29 +0000322bool WebRtcVoiceEngine::ApplyOptions(const AudioOptions& options_in) {
solenberg566ef242015-11-06 15:34:49 -0800323 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
Mirko Bonadei675513b2017-11-09 11:09:25 +0100324 RTC_LOG(LS_INFO) << "WebRtcVoiceEngine::ApplyOptions: "
325 << options_in.ToString();
solenberg0f7d2932016-01-15 01:40:39 -0800326 AudioOptions options = options_in; // The options are modified below.
solenberg246b8172015-12-08 09:50:23 -0800327
peah8a8ebd92017-05-22 15:48:47 -0700328 // Set and adjust echo canceller options.
buildbot@webrtc.org13d67762014-05-02 17:33:29 +0000329 // kEcConference is AEC with high suppression.
330 webrtc::EcModes ec_mode = webrtc::kEcConference;
kwiberg102c6a62015-10-30 02:47:38 -0700331 if (options.aecm_generate_comfort_noise) {
Mirko Bonadei675513b2017-11-09 11:09:25 +0100332 RTC_LOG(LS_VERBOSE) << "Comfort noise explicitly set to "
333 << *options.aecm_generate_comfort_noise
334 << " (default is false).";
buildbot@webrtc.org13d67762014-05-02 17:33:29 +0000335 }
336
kjellanderfcfc8042016-01-14 11:01:09 -0800337#if defined(WEBRTC_IOS)
Jonathan Yu76220482017-12-21 04:18:07 -0800338 if (options.ios_force_software_aec_HACK &&
339 *options.ios_force_software_aec_HACK) {
340 // EC may be forced on for a device known to have non-functioning platform
341 // AEC.
342 options.echo_cancellation = true;
343 options.extended_filter_aec = true;
344 RTC_LOG(LS_WARNING)
345 << "Force software AEC on iOS. May conflict with platform AEC.";
346 } else {
347 // On iOS, VPIO provides built-in EC.
348 options.echo_cancellation = false;
349 options.extended_filter_aec = false;
350 RTC_LOG(LS_INFO) << "Always disable AEC on iOS. Use built-in instead.";
351 }
Mirko Bonadeic8c71b92017-10-16 11:08:54 +0200352#elif defined(WEBRTC_ANDROID)
buildbot@webrtc.org13d67762014-05-02 17:33:29 +0000353 ec_mode = webrtc::kEcAecm;
Oskar Sundbom78807582017-11-16 11:09:55 +0100354 options.extended_filter_aec = false;
buildbot@webrtc.org13d67762014-05-02 17:33:29 +0000355#endif
356
Bjorn Volckerbf395c12015-03-25 22:45:56 +0100357 // Delay Agnostic AEC automatically turns on EC if not set except on iOS
358 // where the feature is not supported.
359 bool use_delay_agnostic_aec = false;
kjellanderfcfc8042016-01-14 11:01:09 -0800360#if !defined(WEBRTC_IOS)
kwiberg102c6a62015-10-30 02:47:38 -0700361 if (options.delay_agnostic_aec) {
362 use_delay_agnostic_aec = *options.delay_agnostic_aec;
Bjorn Volckerbf395c12015-03-25 22:45:56 +0100363 if (use_delay_agnostic_aec) {
Oskar Sundbom78807582017-11-16 11:09:55 +0100364 options.echo_cancellation = true;
365 options.extended_filter_aec = true;
Bjorn Volckerbf395c12015-03-25 22:45:56 +0100366 ec_mode = webrtc::kEcConference;
367 }
368 }
369#endif
370
peah8a8ebd92017-05-22 15:48:47 -0700371// Set and adjust noise suppressor options.
372#if defined(WEBRTC_IOS)
373 // On iOS, VPIO provides built-in NS.
Oskar Sundbom78807582017-11-16 11:09:55 +0100374 options.noise_suppression = false;
375 options.typing_detection = false;
376 options.experimental_ns = false;
Mirko Bonadei675513b2017-11-09 11:09:25 +0100377 RTC_LOG(LS_INFO) << "Always disable NS on iOS. Use built-in instead.";
Mirko Bonadeic8c71b92017-10-16 11:08:54 +0200378#elif defined(WEBRTC_ANDROID)
Oskar Sundbom78807582017-11-16 11:09:55 +0100379 options.typing_detection = false;
380 options.experimental_ns = false;
peah8a8ebd92017-05-22 15:48:47 -0700381#endif
382
383// Set and adjust gain control options.
384#if defined(WEBRTC_IOS)
385 // On iOS, VPIO provides built-in AGC.
Oskar Sundbom78807582017-11-16 11:09:55 +0100386 options.auto_gain_control = false;
387 options.experimental_agc = false;
Mirko Bonadei675513b2017-11-09 11:09:25 +0100388 RTC_LOG(LS_INFO) << "Always disable AGC on iOS. Use built-in instead.";
Mirko Bonadeic8c71b92017-10-16 11:08:54 +0200389#elif defined(WEBRTC_ANDROID)
Oskar Sundbom78807582017-11-16 11:09:55 +0100390 options.experimental_agc = false;
peah8a8ebd92017-05-22 15:48:47 -0700391#endif
392
393#if defined(WEBRTC_IOS) || defined(WEBRTC_ANDROID)
Mirko Bonadeic8c71b92017-10-16 11:08:54 +0200394 // Turn off the gain control if specified by the field trial.
395 // The purpose of the field trial is to reduce the amount of resampling
396 // performed inside the audio processing module on mobile platforms by
397 // whenever possible turning off the fixed AGC mode and the high-pass filter.
398 // (https://bugs.chromium.org/p/webrtc/issues/detail?id=6181).
peah8a8ebd92017-05-22 15:48:47 -0700399 if (webrtc::field_trial::IsEnabled(
400 "WebRTC-Audio-MinimizeResamplingOnMobile")) {
Oskar Sundbom78807582017-11-16 11:09:55 +0100401 options.auto_gain_control = false;
Mirko Bonadei675513b2017-11-09 11:09:25 +0100402 RTC_LOG(LS_INFO) << "Disable AGC according to field trial.";
Steve Antone78bcb92017-10-31 09:53:08 -0700403 if (!(options.noise_suppression.value_or(false) ||
peah8a8ebd92017-05-22 15:48:47 -0700404 options.echo_cancellation.value_or(false))) {
405 // If possible, turn off the high-pass filter.
Mirko Bonadei675513b2017-11-09 11:09:25 +0100406 RTC_LOG(LS_INFO)
407 << "Disable high-pass filter in response to field trial.";
Oskar Sundbom78807582017-11-16 11:09:55 +0100408 options.highpass_filter = false;
peah8a8ebd92017-05-22 15:48:47 -0700409 }
410 }
411#endif
412
peah1bcfce52016-08-26 07:16:04 -0700413#if (WEBRTC_INTELLIGIBILITY_ENHANCER == 0)
414 // Hardcode the intelligibility enhancer to be off.
Oskar Sundbom78807582017-11-16 11:09:55 +0100415 options.intelligibility_enhancer = false;
peah1bcfce52016-08-26 07:16:04 -0700416#endif
417
kwiberg102c6a62015-10-30 02:47:38 -0700418 if (options.echo_cancellation) {
henrika@webrtc.orga954c072014-12-09 16:22:09 +0000419 // Check if platform supports built-in EC. Currently only supported on
420 // Android and in combination with Java based audio layer.
421 // TODO(henrika): investigate possibility to support built-in EC also
422 // in combination with Open SL ES audio.
solenberg5b5129a2016-04-08 05:35:48 -0700423 const bool built_in_aec = adm()->BuiltInAECIsAvailable();
Bjorn Volcker73f72102015-06-03 14:50:15 +0200424 if (built_in_aec) {
Bjorn Volckerccfc9392015-05-07 07:43:17 +0200425 // Built-in EC exists on this device and use_delay_agnostic_aec is not
426 // overriding it. Enable/Disable it according to the echo_cancellation
427 // audio option.
Bjorn Volcker73f72102015-06-03 14:50:15 +0200428 const bool enable_built_in_aec =
kwiberg102c6a62015-10-30 02:47:38 -0700429 *options.echo_cancellation && !use_delay_agnostic_aec;
solenberg5b5129a2016-04-08 05:35:48 -0700430 if (adm()->EnableBuiltInAEC(enable_built_in_aec) == 0 &&
Bjorn Volcker73f72102015-06-03 14:50:15 +0200431 enable_built_in_aec) {
Bjorn Volckerbf395c12015-03-25 22:45:56 +0100432 // Disable internal software EC if built-in EC is enabled,
henrika@webrtc.orga954c072014-12-09 16:22:09 +0000433 // i.e., replace the software EC with the built-in EC.
Oskar Sundbom78807582017-11-16 11:09:55 +0100434 options.echo_cancellation = false;
Mirko Bonadei675513b2017-11-09 11:09:25 +0100435 RTC_LOG(LS_INFO)
436 << "Disabling EC since built-in EC will be used instead";
henrika@webrtc.orga954c072014-12-09 16:22:09 +0000437 }
438 }
solenberg76377c52017-02-21 00:54:31 -0800439 webrtc::apm_helpers::SetEcStatus(
440 apm(), *options.echo_cancellation, ec_mode);
Mirko Bonadeic8c71b92017-10-16 11:08:54 +0200441#if !defined(WEBRTC_ANDROID)
solenberg76377c52017-02-21 00:54:31 -0800442 webrtc::apm_helpers::SetEcMetricsStatus(apm(), *options.echo_cancellation);
buildbot@webrtc.org13d67762014-05-02 17:33:29 +0000443#endif
444 if (ec_mode == webrtc::kEcAecm) {
kwiberg102c6a62015-10-30 02:47:38 -0700445 bool cn = options.aecm_generate_comfort_noise.value_or(false);
solenberg76377c52017-02-21 00:54:31 -0800446 webrtc::apm_helpers::SetAecmMode(apm(), cn);
buildbot@webrtc.org13d67762014-05-02 17:33:29 +0000447 }
448 }
449
kwiberg102c6a62015-10-30 02:47:38 -0700450 if (options.auto_gain_control) {
peah72a56452016-08-22 12:08:55 -0700451 bool built_in_agc_avaliable = adm()->BuiltInAGCIsAvailable();
452 if (built_in_agc_avaliable) {
solenberg5b5129a2016-04-08 05:35:48 -0700453 if (adm()->EnableBuiltInAGC(*options.auto_gain_control) == 0 &&
kwiberg102c6a62015-10-30 02:47:38 -0700454 *options.auto_gain_control) {
henrikac14f5ff2015-09-23 14:08:33 +0200455 // Disable internal software AGC if built-in AGC is enabled,
456 // i.e., replace the software AGC with the built-in AGC.
Oskar Sundbom78807582017-11-16 11:09:55 +0100457 options.auto_gain_control = false;
Mirko Bonadei675513b2017-11-09 11:09:25 +0100458 RTC_LOG(LS_INFO)
459 << "Disabling AGC since built-in AGC will be used instead";
henrikac14f5ff2015-09-23 14:08:33 +0200460 }
461 }
henrikae26456a2017-12-13 14:08:48 +0100462 webrtc::apm_helpers::SetAgcStatus(apm(), *options.auto_gain_control);
buildbot@webrtc.org13d67762014-05-02 17:33:29 +0000463 }
464
kwiberg102c6a62015-10-30 02:47:38 -0700465 if (options.tx_agc_target_dbov || options.tx_agc_digital_compression_gain ||
Steve Anton606a5972017-12-07 14:31:01 -0800466 options.tx_agc_limiter) {
buildbot@webrtc.org13d67762014-05-02 17:33:29 +0000467 // Override default_agc_config_. Generally, an unset option means "leave
468 // the VoE bits alone" in this function, so we want whatever is set to be
469 // stored as the new "default". If we didn't, then setting e.g.
470 // tx_agc_target_dbov would reset digital compression gain and limiter
471 // settings.
kwiberg102c6a62015-10-30 02:47:38 -0700472 default_agc_config_.targetLeveldBOv = options.tx_agc_target_dbov.value_or(
473 default_agc_config_.targetLeveldBOv);
buildbot@webrtc.org13d67762014-05-02 17:33:29 +0000474 default_agc_config_.digitalCompressionGaindB =
kwiberg102c6a62015-10-30 02:47:38 -0700475 options.tx_agc_digital_compression_gain.value_or(
buildbot@webrtc.org13d67762014-05-02 17:33:29 +0000476 default_agc_config_.digitalCompressionGaindB);
477 default_agc_config_.limiterEnable =
kwiberg102c6a62015-10-30 02:47:38 -0700478 options.tx_agc_limiter.value_or(default_agc_config_.limiterEnable);
Steve Anton606a5972017-12-07 14:31:01 -0800479 webrtc::apm_helpers::SetAgcConfig(apm(), default_agc_config_);
buildbot@webrtc.org13d67762014-05-02 17:33:29 +0000480 }
481
Alejandro Luebsc9b0c262016-05-16 15:32:38 -0700482 if (options.intelligibility_enhancer) {
483 intelligibility_enhancer_ = options.intelligibility_enhancer;
484 }
485 if (intelligibility_enhancer_ && *intelligibility_enhancer_) {
Mirko Bonadei675513b2017-11-09 11:09:25 +0100486 RTC_LOG(LS_INFO) << "Enabling NS when Intelligibility Enhancer is active.";
Alejandro Luebsc9b0c262016-05-16 15:32:38 -0700487 options.noise_suppression = intelligibility_enhancer_;
488 }
489
kwiberg102c6a62015-10-30 02:47:38 -0700490 if (options.noise_suppression) {
Alejandro Luebsc9b0c262016-05-16 15:32:38 -0700491 if (adm()->BuiltInNSIsAvailable()) {
492 bool builtin_ns =
493 *options.noise_suppression &&
494 !(intelligibility_enhancer_ && *intelligibility_enhancer_);
495 if (adm()->EnableBuiltInNS(builtin_ns) == 0 && builtin_ns) {
henrikac14f5ff2015-09-23 14:08:33 +0200496 // Disable internal software NS if built-in NS is enabled,
497 // i.e., replace the software NS with the built-in NS.
Oskar Sundbom78807582017-11-16 11:09:55 +0100498 options.noise_suppression = false;
Mirko Bonadei675513b2017-11-09 11:09:25 +0100499 RTC_LOG(LS_INFO)
500 << "Disabling NS since built-in NS will be used instead";
henrikac14f5ff2015-09-23 14:08:33 +0200501 }
502 }
solenberg76377c52017-02-21 00:54:31 -0800503 webrtc::apm_helpers::SetNsStatus(apm(), *options.noise_suppression);
buildbot@webrtc.org13d67762014-05-02 17:33:29 +0000504 }
505
kwiberg102c6a62015-10-30 02:47:38 -0700506 if (options.stereo_swapping) {
Mirko Bonadei675513b2017-11-09 11:09:25 +0100507 RTC_LOG(LS_INFO) << "Stereo swapping enabled? " << *options.stereo_swapping;
Fredrik Solenberg2a877972017-12-15 16:42:15 +0100508 audio_state()->SetStereoChannelSwapping(*options.stereo_swapping);
buildbot@webrtc.org13d67762014-05-02 17:33:29 +0000509 }
510
kwiberg102c6a62015-10-30 02:47:38 -0700511 if (options.audio_jitter_buffer_max_packets) {
Mirko Bonadei675513b2017-11-09 11:09:25 +0100512 RTC_LOG(LS_INFO) << "NetEq capacity is "
513 << *options.audio_jitter_buffer_max_packets;
Fredrik Solenberg8f5787a2018-01-11 13:52:30 +0100514 audio_jitter_buffer_max_packets_ =
solenberg88499ec2016-09-07 07:34:41 -0700515 std::max(20, *options.audio_jitter_buffer_max_packets);
Henrik Lundin64dad832015-05-11 12:44:23 +0200516 }
kwiberg102c6a62015-10-30 02:47:38 -0700517 if (options.audio_jitter_buffer_fast_accelerate) {
Mirko Bonadei675513b2017-11-09 11:09:25 +0100518 RTC_LOG(LS_INFO) << "NetEq fast mode? "
519 << *options.audio_jitter_buffer_fast_accelerate;
Fredrik Solenberg8f5787a2018-01-11 13:52:30 +0100520 audio_jitter_buffer_fast_accelerate_ =
solenberg88499ec2016-09-07 07:34:41 -0700521 *options.audio_jitter_buffer_fast_accelerate;
Henrik Lundin5263b3c2015-06-01 10:29:41 +0200522 }
523
kwiberg102c6a62015-10-30 02:47:38 -0700524 if (options.typing_detection) {
Mirko Bonadei675513b2017-11-09 11:09:25 +0100525 RTC_LOG(LS_INFO) << "Typing detection is enabled? "
526 << *options.typing_detection;
solenberg76377c52017-02-21 00:54:31 -0800527 webrtc::apm_helpers::SetTypingDetectionStatus(
528 apm(), *options.typing_detection);
buildbot@webrtc.org13d67762014-05-02 17:33:29 +0000529 }
530
buildbot@webrtc.org1f8a2372014-08-28 10:52:44 +0000531 webrtc::Config config;
532
kwiberg102c6a62015-10-30 02:47:38 -0700533 if (options.delay_agnostic_aec)
534 delay_agnostic_aec_ = options.delay_agnostic_aec;
535 if (delay_agnostic_aec_) {
Mirko Bonadei675513b2017-11-09 11:09:25 +0100536 RTC_LOG(LS_INFO) << "Delay agnostic aec is enabled? "
537 << *delay_agnostic_aec_;
henrik.lundin0f133b92015-07-02 00:17:55 -0700538 config.Set<webrtc::DelayAgnostic>(
kwiberg102c6a62015-10-30 02:47:38 -0700539 new webrtc::DelayAgnostic(*delay_agnostic_aec_));
Bjorn Volckerbf395c12015-03-25 22:45:56 +0100540 }
541
kwiberg102c6a62015-10-30 02:47:38 -0700542 if (options.extended_filter_aec) {
543 extended_filter_aec_ = options.extended_filter_aec;
544 }
545 if (extended_filter_aec_) {
Mirko Bonadei675513b2017-11-09 11:09:25 +0100546 RTC_LOG(LS_INFO) << "Extended filter aec is enabled? "
547 << *extended_filter_aec_;
Henrik Lundin441f6342015-06-09 16:03:13 +0200548 config.Set<webrtc::ExtendedFilter>(
kwiberg102c6a62015-10-30 02:47:38 -0700549 new webrtc::ExtendedFilter(*extended_filter_aec_));
buildbot@webrtc.org1f8a2372014-08-28 10:52:44 +0000550 }
551
kwiberg102c6a62015-10-30 02:47:38 -0700552 if (options.experimental_ns) {
553 experimental_ns_ = options.experimental_ns;
554 }
555 if (experimental_ns_) {
Mirko Bonadei675513b2017-11-09 11:09:25 +0100556 RTC_LOG(LS_INFO) << "Experimental ns is enabled? " << *experimental_ns_;
buildbot@webrtc.org1f8a2372014-08-28 10:52:44 +0000557 config.Set<webrtc::ExperimentalNs>(
kwiberg102c6a62015-10-30 02:47:38 -0700558 new webrtc::ExperimentalNs(*experimental_ns_));
buildbot@webrtc.org1f8a2372014-08-28 10:52:44 +0000559 }
buildbot@webrtc.org1f8a2372014-08-28 10:52:44 +0000560
Alejandro Luebsc9b0c262016-05-16 15:32:38 -0700561 if (intelligibility_enhancer_) {
Mirko Bonadei675513b2017-11-09 11:09:25 +0100562 RTC_LOG(LS_INFO) << "Intelligibility Enhancer is enabled? "
563 << *intelligibility_enhancer_;
Alejandro Luebsc9b0c262016-05-16 15:32:38 -0700564 config.Set<webrtc::Intelligibility>(
565 new webrtc::Intelligibility(*intelligibility_enhancer_));
566 }
567
peahb1c9d1d2017-07-25 15:45:24 -0700568 webrtc::AudioProcessing::Config apm_config = apm()->GetConfig();
569
peah8271d042016-11-22 07:24:52 -0800570 if (options.highpass_filter) {
peahb1c9d1d2017-07-25 15:45:24 -0700571 apm_config.high_pass_filter.enabled = *options.highpass_filter;
peah8271d042016-11-22 07:24:52 -0800572 }
573
ivoc4ca18692017-02-10 05:11:09 -0800574 if (options.residual_echo_detector) {
peahb1c9d1d2017-07-25 15:45:24 -0700575 apm_config.residual_echo_detector.enabled = *options.residual_echo_detector;
ivoc4ca18692017-02-10 05:11:09 -0800576 }
577
solenberg059fb442016-10-26 05:12:24 -0700578 apm()->SetExtraOptions(config);
peahb1c9d1d2017-07-25 15:45:24 -0700579 apm()->ApplyConfig(apm_config);
buildbot@webrtc.org13d67762014-05-02 17:33:29 +0000580 return true;
581}
582
ossudedfd282016-06-14 07:12:39 -0700583const std::vector<AudioCodec>& WebRtcVoiceEngine::send_codecs() const {
584 RTC_DCHECK(signal_thread_checker_.CalledOnValidThread());
ossuc54071d2016-08-17 02:45:41 -0700585 return send_codecs_;
ossudedfd282016-06-14 07:12:39 -0700586}
587
588const std::vector<AudioCodec>& WebRtcVoiceEngine::recv_codecs() const {
solenberg566ef242015-11-06 15:34:49 -0800589 RTC_DCHECK(signal_thread_checker_.CalledOnValidThread());
ossuc54071d2016-08-17 02:45:41 -0700590 return recv_codecs_;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000591}
592
Stefan Holmer9d69c3f2015-12-07 10:45:43 +0100593RtpCapabilities WebRtcVoiceEngine::GetCapabilities() const {
solenberg566ef242015-11-06 15:34:49 -0800594 RTC_DCHECK(signal_thread_checker_.CalledOnValidThread());
Stefan Holmer9d69c3f2015-12-07 10:45:43 +0100595 RtpCapabilities capabilities;
Stefan Holmer9d69c3f2015-12-07 10:45:43 +0100596 capabilities.header_extensions.push_back(
isheriff6f8d6862016-05-26 11:24:55 -0700597 webrtc::RtpExtension(webrtc::RtpExtension::kAudioLevelUri,
598 webrtc::RtpExtension::kAudioLevelDefaultId));
sprangc1b57a12017-02-28 08:50:47 -0800599 if (webrtc::field_trial::IsEnabled("WebRTC-Audio-SendSideBwe")) {
isheriff6f8d6862016-05-26 11:24:55 -0700600 capabilities.header_extensions.push_back(webrtc::RtpExtension(
601 webrtc::RtpExtension::kTransportSequenceNumberUri,
602 webrtc::RtpExtension::kTransportSequenceNumberDefaultId));
stefanba4c0e42016-02-04 04:12:24 -0800603 }
Steve Antonbb50ce52018-03-26 10:24:32 -0700604 // TODO(bugs.webrtc.org/4050): Add MID header extension as capability once MID
605 // demuxing is completed.
606 // capabilities.header_extensions.push_back(webrtc::RtpExtension(
607 // webrtc::RtpExtension::kMidUri, webrtc::RtpExtension::kMidDefaultId));
Stefan Holmer9d69c3f2015-12-07 10:45:43 +0100608 return capabilities;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000609}
610
solenberg63b34542015-09-29 06:06:31 -0700611void WebRtcVoiceEngine::RegisterChannel(WebRtcVoiceMediaChannel* channel) {
solenberg566ef242015-11-06 15:34:49 -0800612 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
613 RTC_DCHECK(channel);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000614 channels_.push_back(channel);
615}
616
solenberg63b34542015-09-29 06:06:31 -0700617void WebRtcVoiceEngine::UnregisterChannel(WebRtcVoiceMediaChannel* channel) {
solenberg566ef242015-11-06 15:34:49 -0800618 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
solenberg63b34542015-09-29 06:06:31 -0700619 auto it = std::find(channels_.begin(), channels_.end(), channel);
solenberg566ef242015-11-06 15:34:49 -0800620 RTC_DCHECK(it != channels_.end());
621 channels_.erase(it);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000622}
623
ivocd66b44d2016-01-15 03:06:36 -0800624bool WebRtcVoiceEngine::StartAecDump(rtc::PlatformFile file,
625 int64_t max_size_bytes) {
solenberg566ef242015-11-06 15:34:49 -0800626 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
deadbeefeb02c032017-06-15 08:29:25 -0700627 auto aec_dump = webrtc::AecDumpFactory::Create(
628 file, max_size_bytes, low_priority_worker_queue_.get());
aleloi048cbdd2017-05-29 02:56:27 -0700629 if (!aec_dump) {
wu@webrtc.orga8910d22014-01-23 22:12:45 +0000630 return false;
631 }
aleloi048cbdd2017-05-29 02:56:27 -0700632 apm()->AttachAecDump(std::move(aec_dump));
wu@webrtc.orga9890802013-12-13 00:21:03 +0000633 return true;
wu@webrtc.orga9890802013-12-13 00:21:03 +0000634}
635
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000636void WebRtcVoiceEngine::StartAecDump(const std::string& filename) {
solenberg566ef242015-11-06 15:34:49 -0800637 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
aleloi048cbdd2017-05-29 02:56:27 -0700638
deadbeefeb02c032017-06-15 08:29:25 -0700639 auto aec_dump = webrtc::AecDumpFactory::Create(
640 filename, -1, low_priority_worker_queue_.get());
aleloi048cbdd2017-05-29 02:56:27 -0700641 if (aec_dump) {
642 apm()->AttachAecDump(std::move(aec_dump));
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000643 }
644}
645
646void WebRtcVoiceEngine::StopAecDump() {
solenberg566ef242015-11-06 15:34:49 -0800647 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
aleloi048cbdd2017-05-29 02:56:27 -0700648 apm()->DetachAecDump();
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000649}
650
solenberg5b5129a2016-04-08 05:35:48 -0700651webrtc::AudioDeviceModule* WebRtcVoiceEngine::adm() {
652 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
653 RTC_DCHECK(adm_);
Fredrik Solenbergd3195342017-11-21 20:33:05 +0100654 return adm_.get();
solenberg5b5129a2016-04-08 05:35:48 -0700655}
656
peahb1c9d1d2017-07-25 15:45:24 -0700657webrtc::AudioProcessing* WebRtcVoiceEngine::apm() const {
solenberg059fb442016-10-26 05:12:24 -0700658 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
Fredrik Solenberg55900fd2017-11-23 20:22:55 +0100659 RTC_DCHECK(apm_);
peaha9cc40b2017-06-29 08:32:09 -0700660 return apm_.get();
solenberg059fb442016-10-26 05:12:24 -0700661}
662
Fredrik Solenberg2a877972017-12-15 16:42:15 +0100663webrtc::AudioState* WebRtcVoiceEngine::audio_state() {
solenberg76377c52017-02-21 00:54:31 -0800664 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
Fredrik Solenberg2a877972017-12-15 16:42:15 +0100665 RTC_DCHECK(audio_state_);
666 return audio_state_.get();
solenberg76377c52017-02-21 00:54:31 -0800667}
668
ossu20a4b3f2017-04-27 02:08:52 -0700669AudioCodecs WebRtcVoiceEngine::CollectCodecs(
670 const std::vector<webrtc::AudioCodecSpec>& specs) const {
ossuc54071d2016-08-17 02:45:41 -0700671 PayloadTypeMapper mapper;
672 AudioCodecs out;
ossuc54071d2016-08-17 02:45:41 -0700673
solenberg2779bab2016-11-17 04:45:19 -0800674 // Only generate CN payload types for these clockrates:
ossuc54071d2016-08-17 02:45:41 -0700675 std::map<int, bool, std::greater<int>> generate_cn = {{ 8000, false },
676 { 16000, false },
677 { 32000, false }};
solenberg2779bab2016-11-17 04:45:19 -0800678 // Only generate telephone-event payload types for these clockrates:
679 std::map<int, bool, std::greater<int>> generate_dtmf = {{ 8000, false },
680 { 16000, false },
681 { 32000, false },
682 { 48000, false }};
ossuc54071d2016-08-17 02:45:41 -0700683
ossu9def8002017-02-09 05:14:32 -0800684 auto map_format = [&mapper](const webrtc::SdpAudioFormat& format,
685 AudioCodecs* out) {
ossuc54071d2016-08-17 02:45:41 -0700686 rtc::Optional<AudioCodec> opt_codec = mapper.ToAudioCodec(format);
ossu9def8002017-02-09 05:14:32 -0800687 if (opt_codec) {
688 if (out) {
689 out->push_back(*opt_codec);
690 }
691 } else {
Mirko Bonadei675513b2017-11-09 11:09:25 +0100692 RTC_LOG(LS_ERROR) << "Unable to assign payload type to format: "
Jonas Olssonabbe8412018-04-03 13:40:05 +0200693 << rtc::ToString(format);
ossuc54071d2016-08-17 02:45:41 -0700694 }
695
ossu9def8002017-02-09 05:14:32 -0800696 return opt_codec;
ossuc54071d2016-08-17 02:45:41 -0700697 };
698
ossud4e9f622016-08-18 02:01:17 -0700699 for (const auto& spec : specs) {
ossu9def8002017-02-09 05:14:32 -0800700 // We need to do some extra stuff before adding the main codecs to out.
701 rtc::Optional<AudioCodec> opt_codec = map_format(spec.format, nullptr);
702 if (opt_codec) {
703 AudioCodec& codec = *opt_codec;
ossua1a040a2017-04-06 10:03:21 -0700704 if (spec.info.supports_network_adaption) {
ossu9def8002017-02-09 05:14:32 -0800705 codec.AddFeedbackParam(
706 FeedbackParam(kRtcpFbParamTransportCc, kParamValueEmpty));
707 }
708
ossua1a040a2017-04-06 10:03:21 -0700709 if (spec.info.allow_comfort_noise) {
solenberg2779bab2016-11-17 04:45:19 -0800710 // Generate a CN entry if the decoder allows it and we support the
711 // clockrate.
712 auto cn = generate_cn.find(spec.format.clockrate_hz);
713 if (cn != generate_cn.end()) {
714 cn->second = true;
715 }
716 }
717
718 // Generate a telephone-event entry if we support the clockrate.
719 auto dtmf = generate_dtmf.find(spec.format.clockrate_hz);
720 if (dtmf != generate_dtmf.end()) {
721 dtmf->second = true;
ossuc54071d2016-08-17 02:45:41 -0700722 }
ossu9def8002017-02-09 05:14:32 -0800723
724 out.push_back(codec);
ossuc54071d2016-08-17 02:45:41 -0700725 }
726 }
727
solenberg2779bab2016-11-17 04:45:19 -0800728 // Add CN codecs after "proper" audio codecs.
ossuc54071d2016-08-17 02:45:41 -0700729 for (const auto& cn : generate_cn) {
730 if (cn.second) {
ossu9def8002017-02-09 05:14:32 -0800731 map_format({kCnCodecName, cn.first, 1}, &out);
ossuc54071d2016-08-17 02:45:41 -0700732 }
733 }
734
solenberg2779bab2016-11-17 04:45:19 -0800735 // Add telephone-event codecs last.
736 for (const auto& dtmf : generate_dtmf) {
737 if (dtmf.second) {
ossu9def8002017-02-09 05:14:32 -0800738 map_format({kDtmfCodecName, dtmf.first, 1}, &out);
solenberg2779bab2016-11-17 04:45:19 -0800739 }
740 }
ossuc54071d2016-08-17 02:45:41 -0700741
742 return out;
743}
744
solenbergc96df772015-10-21 13:01:53 -0700745class WebRtcVoiceMediaChannel::WebRtcAudioSendStream
Taylor Brandstetter1a018dc2016-03-08 12:37:39 -0800746 : public AudioSource::Sink {
mallinath@webrtc.org67ee6b92014-02-03 16:57:16 +0000747 public:
minyue7a973442016-10-20 03:27:12 -0700748 WebRtcAudioSendStream(
minyue7a973442016-10-20 03:27:12 -0700749 uint32_t ssrc,
Steve Antonbb50ce52018-03-26 10:24:32 -0700750 const std::string& mid,
minyue7a973442016-10-20 03:27:12 -0700751 const std::string& c_name,
Alex Narestb3944f02017-10-13 14:56:18 +0200752 const std::string track_id,
ossu20a4b3f2017-04-27 02:08:52 -0700753 const rtc::Optional<webrtc::AudioSendStream::Config::SendCodecSpec>&
754 send_codec_spec,
minyue7a973442016-10-20 03:27:12 -0700755 const std::vector<webrtc::RtpExtension>& extensions,
756 int max_send_bitrate_bps,
minyue6b825df2016-10-31 04:08:32 -0700757 const rtc::Optional<std::string>& audio_network_adaptor_config,
minyue7a973442016-10-20 03:27:12 -0700758 webrtc::Call* call,
ossu20a4b3f2017-04-27 02:08:52 -0700759 webrtc::Transport* send_transport,
Karl Wiberg77490b92018-03-21 15:18:42 +0100760 const rtc::scoped_refptr<webrtc::AudioEncoderFactory>& encoder_factory,
761 const rtc::Optional<webrtc::AudioCodecPairId> codec_pair_id)
Fredrik Solenberg2a877972017-12-15 16:42:15 +0100762 : call_(call),
mflodman3d7db262016-04-29 00:57:13 -0700763 config_(send_transport),
sprangc1b57a12017-02-28 08:50:47 -0800764 send_side_bwe_with_overhead_(
765 webrtc::field_trial::IsEnabled("WebRTC-SendSideBwe-WithOverhead")),
minyue7a973442016-10-20 03:27:12 -0700766 max_send_bitrate_bps_(max_send_bitrate_bps),
skvlade0d46372016-04-07 22:59:22 -0700767 rtp_parameters_(CreateRtpParametersWithOneEncoding()) {
solenbergc96df772015-10-21 13:01:53 -0700768 RTC_DCHECK(call);
ossu20a4b3f2017-04-27 02:08:52 -0700769 RTC_DCHECK(encoder_factory);
solenberg3a941542015-11-16 07:34:50 -0800770 config_.rtp.ssrc = ssrc;
Steve Antonbb50ce52018-03-26 10:24:32 -0700771 config_.rtp.mid = mid;
solenberg3a941542015-11-16 07:34:50 -0800772 config_.rtp.c_name = c_name;
solenberg971cab02016-06-14 10:02:41 -0700773 config_.rtp.extensions = extensions;
minyue6b825df2016-10-31 04:08:32 -0700774 config_.audio_network_adaptor_config = audio_network_adaptor_config;
ossu20a4b3f2017-04-27 02:08:52 -0700775 config_.encoder_factory = encoder_factory;
Karl Wiberg77490b92018-03-21 15:18:42 +0100776 config_.codec_pair_id = codec_pair_id;
Alex Narestb3944f02017-10-13 14:56:18 +0200777 config_.track_id = track_id;
Oskar Sundbom78807582017-11-16 11:09:55 +0100778 rtp_parameters_.encodings[0].ssrc = ssrc;
ossu20a4b3f2017-04-27 02:08:52 -0700779
780 if (send_codec_spec) {
781 UpdateSendCodecSpec(*send_codec_spec);
782 }
783
784 stream_ = call_->CreateAudioSendStream(config_);
solenbergc96df772015-10-21 13:01:53 -0700785 }
solenberg3a941542015-11-16 07:34:50 -0800786
solenbergc96df772015-10-21 13:01:53 -0700787 ~WebRtcAudioSendStream() override {
solenberg566ef242015-11-06 15:34:49 -0800788 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
Taylor Brandstetter1a018dc2016-03-08 12:37:39 -0800789 ClearSource();
solenbergc96df772015-10-21 13:01:53 -0700790 call_->DestroyAudioSendStream(stream_);
791 }
henrike@webrtc.org1e09a712013-07-26 19:17:59 +0000792
ossu20a4b3f2017-04-27 02:08:52 -0700793 void SetSendCodecSpec(
minyue7a973442016-10-20 03:27:12 -0700794 const webrtc::AudioSendStream::Config::SendCodecSpec& send_codec_spec) {
ossu20a4b3f2017-04-27 02:08:52 -0700795 UpdateSendCodecSpec(send_codec_spec);
796 ReconfigureAudioSendStream();
solenberg971cab02016-06-14 10:02:41 -0700797 }
798
ossu20a4b3f2017-04-27 02:08:52 -0700799 void SetRtpExtensions(const std::vector<webrtc::RtpExtension>& extensions) {
solenberg3a941542015-11-16 07:34:50 -0800800 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
solenberg3a941542015-11-16 07:34:50 -0800801 config_.rtp.extensions = extensions;
ossu20a4b3f2017-04-27 02:08:52 -0700802 ReconfigureAudioSendStream();
solenberg3a941542015-11-16 07:34:50 -0800803 }
804
Steve Antonbb50ce52018-03-26 10:24:32 -0700805 void SetMid(const std::string& mid) {
806 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
807 if (config_.rtp.mid == mid) {
808 return;
809 }
810 config_.rtp.mid = mid;
811 ReconfigureAudioSendStream();
812 }
813
ossu20a4b3f2017-04-27 02:08:52 -0700814 void SetAudioNetworkAdaptorConfig(
minyue6b825df2016-10-31 04:08:32 -0700815 const rtc::Optional<std::string>& audio_network_adaptor_config) {
816 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
817 if (config_.audio_network_adaptor_config == audio_network_adaptor_config) {
818 return;
819 }
820 config_.audio_network_adaptor_config = audio_network_adaptor_config;
ossu20a4b3f2017-04-27 02:08:52 -0700821 UpdateAllowedBitrateRange();
822 ReconfigureAudioSendStream();
minyue6b825df2016-10-31 04:08:32 -0700823 }
824
minyue7a973442016-10-20 03:27:12 -0700825 bool SetMaxSendBitrate(int bps) {
826 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
ossu20a4b3f2017-04-27 02:08:52 -0700827 RTC_DCHECK(config_.send_codec_spec);
828 RTC_DCHECK(audio_codec_spec_);
829 auto send_rate = ComputeSendBitrate(
830 bps, rtp_parameters_.encodings[0].max_bitrate_bps, *audio_codec_spec_);
831
minyue7a973442016-10-20 03:27:12 -0700832 if (!send_rate) {
833 return false;
834 }
835
836 max_send_bitrate_bps_ = bps;
837
ossu20a4b3f2017-04-27 02:08:52 -0700838 if (send_rate != config_.send_codec_spec->target_bitrate_bps) {
839 config_.send_codec_spec->target_bitrate_bps = send_rate;
840 ReconfigureAudioSendStream();
minyue7a973442016-10-20 03:27:12 -0700841 }
842 return true;
843 }
844
solenbergffbbcac2016-11-17 05:25:37 -0800845 bool SendTelephoneEvent(int payload_type, int payload_freq, int event,
846 int duration_ms) {
Fredrik Solenbergb5727682015-12-04 15:22:19 +0100847 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
848 RTC_DCHECK(stream_);
solenbergffbbcac2016-11-17 05:25:37 -0800849 return stream_->SendTelephoneEvent(payload_type, payload_freq, event,
850 duration_ms);
Fredrik Solenbergb5727682015-12-04 15:22:19 +0100851 }
852
Taylor Brandstetter1a018dc2016-03-08 12:37:39 -0800853 void SetSend(bool send) {
854 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
855 send_ = send;
856 UpdateSendState();
857 }
858
solenberg94218532016-06-16 10:53:22 -0700859 void SetMuted(bool muted) {
860 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
861 RTC_DCHECK(stream_);
862 stream_->SetMuted(muted);
863 muted_ = muted;
864 }
865
866 bool muted() const {
867 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
868 return muted_;
869 }
870
Ivo Creusen56d46092017-11-24 17:29:59 +0100871 webrtc::AudioSendStream::Stats GetStats(bool has_remote_tracks) const {
solenberg3a941542015-11-16 07:34:50 -0800872 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
873 RTC_DCHECK(stream_);
Ivo Creusen56d46092017-11-24 17:29:59 +0100874 return stream_->GetStats(has_remote_tracks);
solenberg3a941542015-11-16 07:34:50 -0800875 }
876
Taylor Brandstetter1a018dc2016-03-08 12:37:39 -0800877 // Starts the sending by setting ourselves as a sink to the AudioSource to
878 // get data callbacks.
henrike@webrtc.orga7b98182014-02-21 15:51:43 +0000879 // This method is called on the libjingle worker thread.
mallinath@webrtc.org67ee6b92014-02-03 16:57:16 +0000880 // TODO(xians): Make sure Start() is called only once.
Taylor Brandstetter1a018dc2016-03-08 12:37:39 -0800881 void SetSource(AudioSource* source) {
solenberg566ef242015-11-06 15:34:49 -0800882 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
Taylor Brandstetter1a018dc2016-03-08 12:37:39 -0800883 RTC_DCHECK(source);
884 if (source_) {
885 RTC_DCHECK(source_ == source);
mallinath@webrtc.org67ee6b92014-02-03 16:57:16 +0000886 return;
887 }
Taylor Brandstetter1a018dc2016-03-08 12:37:39 -0800888 source->SetSink(this);
889 source_ = source;
890 UpdateSendState();
mallinath@webrtc.org67ee6b92014-02-03 16:57:16 +0000891 }
892
Taylor Brandstetter1a018dc2016-03-08 12:37:39 -0800893 // Stops sending by setting the sink of the AudioSource to nullptr. No data
mallinath@webrtc.org67ee6b92014-02-03 16:57:16 +0000894 // callback will be received after this method.
henrike@webrtc.orga7b98182014-02-21 15:51:43 +0000895 // This method is called on the libjingle worker thread.
Taylor Brandstetter1a018dc2016-03-08 12:37:39 -0800896 void ClearSource() {
solenberg566ef242015-11-06 15:34:49 -0800897 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
Taylor Brandstetter1a018dc2016-03-08 12:37:39 -0800898 if (source_) {
899 source_->SetSink(nullptr);
900 source_ = nullptr;
solenberg98c68862015-10-09 03:27:14 -0700901 }
Taylor Brandstetter1a018dc2016-03-08 12:37:39 -0800902 UpdateSendState();
mallinath@webrtc.org67ee6b92014-02-03 16:57:16 +0000903 }
904
Taylor Brandstetter1a018dc2016-03-08 12:37:39 -0800905 // AudioSource::Sink implementation.
henrike@webrtc.orga7b98182014-02-21 15:51:43 +0000906 // This method is called on the audio thread.
kjellander@webrtc.org14665ff2015-03-04 12:58:35 +0000907 void OnData(const void* audio_data,
908 int bits_per_sample,
909 int sample_rate,
Peter Kasting69558702016-01-12 16:26:35 -0800910 size_t number_of_channels,
Peter Kastingdce40cf2015-08-24 14:52:23 -0700911 size_t number_of_frames) override {
Fredrik Solenberg2a877972017-12-15 16:42:15 +0100912 RTC_DCHECK_EQ(16, bits_per_sample);
solenberg347ec5c2016-09-23 04:21:47 -0700913 RTC_CHECK_RUNS_SERIALIZED(&audio_capture_race_checker_);
Fredrik Solenberg2a877972017-12-15 16:42:15 +0100914 RTC_DCHECK(stream_);
915 std::unique_ptr<webrtc::AudioFrame> audio_frame(new webrtc::AudioFrame());
916 audio_frame->UpdateFrame(audio_frame->timestamp_,
917 static_cast<const int16_t*>(audio_data),
918 number_of_frames,
919 sample_rate,
920 audio_frame->speech_type_,
921 audio_frame->vad_activity_,
922 number_of_channels);
923 stream_->SendAudioData(std::move(audio_frame));
henrike@webrtc.orga7b98182014-02-21 15:51:43 +0000924 }
925
Taylor Brandstetter1a018dc2016-03-08 12:37:39 -0800926 // Callback from the |source_| when it is going away. In case Start() has
henrike@webrtc.orga7b98182014-02-21 15:51:43 +0000927 // never been called, this callback won't be triggered.
kjellander@webrtc.org14665ff2015-03-04 12:58:35 +0000928 void OnClose() override {
solenberg566ef242015-11-06 15:34:49 -0800929 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
Taylor Brandstetter1a018dc2016-03-08 12:37:39 -0800930 // Set |source_| to nullptr to make sure no more callback will get into
931 // the source.
932 source_ = nullptr;
933 UpdateSendState();
mallinath@webrtc.org67ee6b92014-02-03 16:57:16 +0000934 }
935
skvlade0d46372016-04-07 22:59:22 -0700936 const webrtc::RtpParameters& rtp_parameters() const {
937 return rtp_parameters_;
938 }
939
Zach Steinba37b4b2018-01-23 15:02:36 -0800940 webrtc::RTCError ValidateRtpParameters(
941 const webrtc::RtpParameters& rtp_parameters) {
942 using webrtc::RTCErrorType;
943 if (rtp_parameters.encodings.size() != rtp_parameters_.encodings.size()) {
944 LOG_AND_RETURN_ERROR(
945 RTCErrorType::INVALID_MODIFICATION,
946 "Attempted to set RtpParameters with different encoding count");
deadbeeffb2aced2017-01-06 23:05:37 -0800947 }
948 if (rtp_parameters.encodings[0].ssrc != rtp_parameters_.encodings[0].ssrc) {
Zach Steinba37b4b2018-01-23 15:02:36 -0800949 LOG_AND_RETURN_ERROR(RTCErrorType::INVALID_MODIFICATION,
950 "Attempted to set RtpParameters with modified SSRC");
deadbeeffb2aced2017-01-06 23:05:37 -0800951 }
Seth Hampson24722b32017-12-22 09:36:42 -0800952 if (rtp_parameters.encodings[0].bitrate_priority <= 0) {
Zach Steinba37b4b2018-01-23 15:02:36 -0800953 LOG_AND_RETURN_ERROR(RTCErrorType::INVALID_RANGE,
954 "Attempted to set RtpParameters bitrate_priority to "
955 "an invalid number.");
Seth Hampson24722b32017-12-22 09:36:42 -0800956 }
Zach Steinba37b4b2018-01-23 15:02:36 -0800957 return webrtc::RTCError::OK();
deadbeeffb2aced2017-01-06 23:05:37 -0800958 }
959
Zach Steinba37b4b2018-01-23 15:02:36 -0800960 webrtc::RTCError SetRtpParameters(const webrtc::RtpParameters& parameters) {
961 webrtc::RTCError error = ValidateRtpParameters(parameters);
962 if (!error.ok()) {
963 return error;
deadbeeffb2aced2017-01-06 23:05:37 -0800964 }
ossu20a4b3f2017-04-27 02:08:52 -0700965
966 rtc::Optional<int> send_rate;
967 if (audio_codec_spec_) {
968 send_rate = ComputeSendBitrate(max_send_bitrate_bps_,
969 parameters.encodings[0].max_bitrate_bps,
970 *audio_codec_spec_);
971 if (!send_rate) {
Zach Steinba37b4b2018-01-23 15:02:36 -0800972 return webrtc::RTCError(webrtc::RTCErrorType::INTERNAL_ERROR);
ossu20a4b3f2017-04-27 02:08:52 -0700973 }
minyue7a973442016-10-20 03:27:12 -0700974 }
975
minyuececec102017-03-27 13:04:25 -0700976 const rtc::Optional<int> old_rtp_max_bitrate =
977 rtp_parameters_.encodings[0].max_bitrate_bps;
Seth Hampson24722b32017-12-22 09:36:42 -0800978 double old_priority = rtp_parameters_.encodings[0].bitrate_priority;
Lu Liu8b77aea2017-12-20 23:48:03 +0000979 rtp_parameters_ = parameters;
Seth Hampson24722b32017-12-22 09:36:42 -0800980 config_.bitrate_priority = rtp_parameters_.encodings[0].bitrate_priority;
Lu Liu8b77aea2017-12-20 23:48:03 +0000981
Seth Hampson24722b32017-12-22 09:36:42 -0800982 bool reconfigure_send_stream =
983 (rtp_parameters_.encodings[0].max_bitrate_bps != old_rtp_max_bitrate) ||
984 (rtp_parameters_.encodings[0].bitrate_priority != old_priority);
minyuececec102017-03-27 13:04:25 -0700985 if (rtp_parameters_.encodings[0].max_bitrate_bps != old_rtp_max_bitrate) {
Seth Hampson24722b32017-12-22 09:36:42 -0800986 // Update the bitrate range.
ossu20a4b3f2017-04-27 02:08:52 -0700987 if (send_rate) {
988 config_.send_codec_spec->target_bitrate_bps = send_rate;
989 }
990 UpdateAllowedBitrateRange();
Seth Hampsond2b912a2017-12-20 11:56:37 -0800991 }
Seth Hampson24722b32017-12-22 09:36:42 -0800992 if (reconfigure_send_stream) {
993 ReconfigureAudioSendStream();
994 }
995 // parameters.encodings[0].active could have changed.
996 UpdateSendState();
Zach Steinba37b4b2018-01-23 15:02:36 -0800997 return webrtc::RTCError::OK();
skvlade0d46372016-04-07 22:59:22 -0700998 }
999
mallinath@webrtc.org67ee6b92014-02-03 16:57:16 +00001000 private:
Taylor Brandstetter1a018dc2016-03-08 12:37:39 -08001001 void UpdateSendState() {
1002 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
1003 RTC_DCHECK(stream_);
Taylor Brandstetter55dd7082016-05-03 13:50:11 -07001004 RTC_DCHECK_EQ(1UL, rtp_parameters_.encodings.size());
1005 if (send_ && source_ != nullptr && rtp_parameters_.encodings[0].active) {
Taylor Brandstetter1a018dc2016-03-08 12:37:39 -08001006 stream_->Start();
1007 } else { // !send || source_ = nullptr
1008 stream_->Stop();
1009 }
1010 }
1011
ossu20a4b3f2017-04-27 02:08:52 -07001012 void UpdateAllowedBitrateRange() {
michaelt53fe19d2016-10-18 09:39:22 -07001013 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
ossu20a4b3f2017-04-27 02:08:52 -07001014 const bool is_opus =
1015 config_.send_codec_spec &&
1016 !STR_CASE_CMP(config_.send_codec_spec->format.name.c_str(),
1017 kOpusCodecName);
1018 if (is_opus && webrtc::field_trial::IsEnabled("WebRTC-Audio-SendSideBwe")) {
stefane9f36d52017-01-24 08:18:45 -08001019 config_.min_bitrate_bps = kOpusMinBitrateBps;
minyuececec102017-03-27 13:04:25 -07001020
1021 // This means that when RtpParameters is reset, we may change the
ossu20a4b3f2017-04-27 02:08:52 -07001022 // encoder's bit rate immediately (through ReconfigureAudioSendStream()),
minyuececec102017-03-27 13:04:25 -07001023 // meanwhile change the cap to the output of BWE.
1024 config_.max_bitrate_bps =
1025 rtp_parameters_.encodings[0].max_bitrate_bps
1026 ? *rtp_parameters_.encodings[0].max_bitrate_bps
1027 : kOpusBitrateFbBps;
1028
michaelt53fe19d2016-10-18 09:39:22 -07001029 // TODO(mflodman): Keep testing this and set proper values.
1030 // Note: This is an early experiment currently only supported by Opus.
elad.alon0fe12162017-01-31 05:48:37 -08001031 if (send_side_bwe_with_overhead_) {
ossu20a4b3f2017-04-27 02:08:52 -07001032 const int max_packet_size_ms =
1033 WEBRTC_OPUS_SUPPORT_120MS_PTIME ? 120 : 60;
michaelt6672b262017-01-11 10:17:59 -08001034
ossu20a4b3f2017-04-27 02:08:52 -07001035 // OverheadPerPacket = Ipv4(20B) + UDP(8B) + SRTP(10B) + RTP(12)
1036 constexpr int kOverheadPerPacket = 20 + 8 + 10 + 12;
michaelt6672b262017-01-11 10:17:59 -08001037
ossu20a4b3f2017-04-27 02:08:52 -07001038 int min_overhead_bps =
1039 kOverheadPerPacket * 8 * 1000 / max_packet_size_ms;
michaelt6672b262017-01-11 10:17:59 -08001040
ossu20a4b3f2017-04-27 02:08:52 -07001041 // We assume that |config_.max_bitrate_bps| before the next line is
1042 // a hard limit on the payload bitrate, so we add min_overhead_bps to
1043 // it to ensure that, when overhead is deducted, the payload rate
1044 // never goes beyond the limit.
1045 // Note: this also means that if a higher overhead is forced, we
1046 // cannot reach the limit.
1047 // TODO(minyue): Reconsider this when the signaling to BWE is done
1048 // through a dedicated API.
1049 config_.max_bitrate_bps += min_overhead_bps;
michaelt6672b262017-01-11 10:17:59 -08001050
ossu20a4b3f2017-04-27 02:08:52 -07001051 // In contrast to max_bitrate_bps, we let min_bitrate_bps always be
1052 // reachable.
1053 config_.min_bitrate_bps += min_overhead_bps;
michaelt6672b262017-01-11 10:17:59 -08001054 }
michaelt53fe19d2016-10-18 09:39:22 -07001055 }
ossu20a4b3f2017-04-27 02:08:52 -07001056 }
1057
1058 void UpdateSendCodecSpec(
1059 const webrtc::AudioSendStream::Config::SendCodecSpec& send_codec_spec) {
1060 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
1061 config_.rtp.nack.rtp_history_ms =
1062 send_codec_spec.nack_enabled ? kNackRtpHistoryMs : 0;
Oskar Sundbom78807582017-11-16 11:09:55 +01001063 config_.send_codec_spec = send_codec_spec;
ossu20a4b3f2017-04-27 02:08:52 -07001064 auto info =
1065 config_.encoder_factory->QueryAudioEncoder(send_codec_spec.format);
1066 RTC_DCHECK(info);
1067 // If a specific target bitrate has been set for the stream, use that as
1068 // the new default bitrate when computing send bitrate.
1069 if (send_codec_spec.target_bitrate_bps) {
1070 info->default_bitrate_bps = std::max(
1071 info->min_bitrate_bps,
1072 std::min(info->max_bitrate_bps, *send_codec_spec.target_bitrate_bps));
1073 }
1074
1075 audio_codec_spec_.emplace(
1076 webrtc::AudioCodecSpec{send_codec_spec.format, *info});
1077
1078 config_.send_codec_spec->target_bitrate_bps = ComputeSendBitrate(
1079 max_send_bitrate_bps_, rtp_parameters_.encodings[0].max_bitrate_bps,
1080 *audio_codec_spec_);
1081
1082 UpdateAllowedBitrateRange();
1083 }
1084
1085 void ReconfigureAudioSendStream() {
1086 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
1087 RTC_DCHECK(stream_);
1088 stream_->Reconfigure(config_);
michaelt53fe19d2016-10-18 09:39:22 -07001089 }
1090
solenberg566ef242015-11-06 15:34:49 -08001091 rtc::ThreadChecker worker_thread_checker_;
solenberg347ec5c2016-09-23 04:21:47 -07001092 rtc::RaceChecker audio_capture_race_checker_;
solenbergc96df772015-10-21 13:01:53 -07001093 webrtc::Call* call_ = nullptr;
solenberg3a941542015-11-16 07:34:50 -08001094 webrtc::AudioSendStream::Config config_;
elad.alon0fe12162017-01-31 05:48:37 -08001095 const bool send_side_bwe_with_overhead_;
solenberg3a941542015-11-16 07:34:50 -08001096 // The stream is owned by WebRtcAudioSendStream and may be reallocated if
1097 // configuration changes.
solenbergc96df772015-10-21 13:01:53 -07001098 webrtc::AudioSendStream* stream_ = nullptr;
mallinath@webrtc.org67ee6b92014-02-03 16:57:16 +00001099
Taylor Brandstetter1a018dc2016-03-08 12:37:39 -08001100 // Raw pointer to AudioSource owned by LocalAudioTrackHandler.
mallinath@webrtc.org67ee6b92014-02-03 16:57:16 +00001101 // PeerConnection will make sure invalidating the pointer before the object
1102 // goes away.
Taylor Brandstetter1a018dc2016-03-08 12:37:39 -08001103 AudioSource* source_ = nullptr;
1104 bool send_ = false;
solenberg94218532016-06-16 10:53:22 -07001105 bool muted_ = false;
minyue7a973442016-10-20 03:27:12 -07001106 int max_send_bitrate_bps_;
skvlade0d46372016-04-07 22:59:22 -07001107 webrtc::RtpParameters rtp_parameters_;
ossu20a4b3f2017-04-27 02:08:52 -07001108 rtc::Optional<webrtc::AudioCodecSpec> audio_codec_spec_;
henrike@webrtc.orga7b98182014-02-21 15:51:43 +00001109
solenbergc96df772015-10-21 13:01:53 -07001110 RTC_DISALLOW_IMPLICIT_CONSTRUCTORS(WebRtcAudioSendStream);
1111};
1112
1113class WebRtcVoiceMediaChannel::WebRtcAudioReceiveStream {
1114 public:
ossu29b1a8d2016-06-13 07:34:51 -07001115 WebRtcAudioReceiveStream(
ossu29b1a8d2016-06-13 07:34:51 -07001116 uint32_t remote_ssrc,
1117 uint32_t local_ssrc,
1118 bool use_transport_cc,
solenberg8189b022016-06-14 12:13:00 -07001119 bool use_nack,
Seth Hampson845e8782018-03-02 11:34:10 -08001120 const std::vector<std::string>& stream_ids,
ossu29b1a8d2016-06-13 07:34:51 -07001121 const std::vector<webrtc::RtpExtension>& extensions,
1122 webrtc::Call* call,
1123 webrtc::Transport* rtcp_send_transport,
kwiberg1c07c702017-03-27 07:15:49 -07001124 const rtc::scoped_refptr<webrtc::AudioDecoderFactory>& decoder_factory,
Fredrik Solenberg8f5787a2018-01-11 13:52:30 +01001125 const std::map<int, webrtc::SdpAudioFormat>& decoder_map,
Karl Wiberg08126342018-03-20 19:18:55 +01001126 rtc::Optional<webrtc::AudioCodecPairId> codec_pair_id,
Fredrik Solenberg8f5787a2018-01-11 13:52:30 +01001127 size_t jitter_buffer_max_packets,
1128 bool jitter_buffer_fast_accelerate)
stefanba4c0e42016-02-04 04:12:24 -08001129 : call_(call), config_() {
solenberg7add0582015-11-20 09:59:34 -08001130 RTC_DCHECK(call);
1131 config_.rtp.remote_ssrc = remote_ssrc;
kwibergd32bf752017-01-19 07:03:59 -08001132 config_.rtp.local_ssrc = local_ssrc;
1133 config_.rtp.transport_cc = use_transport_cc;
1134 config_.rtp.nack.rtp_history_ms = use_nack ? kNackRtpHistoryMs : 0;
1135 config_.rtp.extensions = extensions;
solenberg31fec402016-05-06 02:13:12 -07001136 config_.rtcp_send_transport = rtcp_send_transport;
Fredrik Solenberg8f5787a2018-01-11 13:52:30 +01001137 config_.jitter_buffer_max_packets = jitter_buffer_max_packets;
1138 config_.jitter_buffer_fast_accelerate = jitter_buffer_fast_accelerate;
Seth Hampson845e8782018-03-02 11:34:10 -08001139 if (!stream_ids.empty()) {
1140 config_.sync_group = stream_ids[0];
Steve Anton5a26a3a2018-02-28 11:38:47 -08001141 }
ossu29b1a8d2016-06-13 07:34:51 -07001142 config_.decoder_factory = decoder_factory;
kwiberg1c07c702017-03-27 07:15:49 -07001143 config_.decoder_map = decoder_map;
Karl Wiberg08126342018-03-20 19:18:55 +01001144 config_.codec_pair_id = codec_pair_id;
kwibergd32bf752017-01-19 07:03:59 -08001145 RecreateAudioReceiveStream();
solenberg7add0582015-11-20 09:59:34 -08001146 }
solenbergc96df772015-10-21 13:01:53 -07001147
solenberg7add0582015-11-20 09:59:34 -08001148 ~WebRtcAudioReceiveStream() {
1149 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
1150 call_->DestroyAudioReceiveStream(stream_);
1151 }
1152
Fredrik Solenberg3b903d02018-01-10 15:17:10 +01001153 void SetLocalSsrc(uint32_t local_ssrc) {
solenberg7add0582015-11-20 09:59:34 -08001154 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
kwibergd32bf752017-01-19 07:03:59 -08001155 config_.rtp.local_ssrc = local_ssrc;
Fredrik Solenberg3b903d02018-01-10 15:17:10 +01001156 ReconfigureAudioReceiveStream();
solenberg7add0582015-11-20 09:59:34 -08001157 }
solenberg8189b022016-06-14 12:13:00 -07001158
Fredrik Solenberg4613bdf2018-01-16 13:32:31 +01001159 void SetUseTransportCcAndRecreateStream(bool use_transport_cc,
1160 bool use_nack) {
solenberg7add0582015-11-20 09:59:34 -08001161 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
kwibergd32bf752017-01-19 07:03:59 -08001162 config_.rtp.transport_cc = use_transport_cc;
1163 config_.rtp.nack.rtp_history_ms = use_nack ? kNackRtpHistoryMs : 0;
Fredrik Solenberg3b903d02018-01-10 15:17:10 +01001164 ReconfigureAudioReceiveStream();
solenberg7add0582015-11-20 09:59:34 -08001165 }
1166
Fredrik Solenberg4613bdf2018-01-16 13:32:31 +01001167 void SetRtpExtensionsAndRecreateStream(
1168 const std::vector<webrtc::RtpExtension>& extensions) {
solenberg4a0f7b52016-06-16 13:07:33 -07001169 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
kwibergd32bf752017-01-19 07:03:59 -08001170 config_.rtp.extensions = extensions;
Fredrik Solenberg4613bdf2018-01-16 13:32:31 +01001171 RecreateAudioReceiveStream();
kwibergd32bf752017-01-19 07:03:59 -08001172 }
1173
deadbeefcb383672017-04-26 16:28:42 -07001174 // Set a new payload type -> decoder map.
Fredrik Solenberg3b903d02018-01-10 15:17:10 +01001175 void SetDecoderMap(const std::map<int, webrtc::SdpAudioFormat>& decoder_map) {
kwibergd32bf752017-01-19 07:03:59 -08001176 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
kwibergd32bf752017-01-19 07:03:59 -08001177 config_.decoder_map = decoder_map;
Fredrik Solenberg3b903d02018-01-10 15:17:10 +01001178 ReconfigureAudioReceiveStream();
solenberg4a0f7b52016-06-16 13:07:33 -07001179 }
1180
Steve Anton5a26a3a2018-02-28 11:38:47 -08001181 void MaybeRecreateAudioReceiveStream(
Seth Hampson845e8782018-03-02 11:34:10 -08001182 const std::vector<std::string>& stream_ids) {
solenberg4904fb62017-02-17 12:01:14 -08001183 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
Steve Anton5a26a3a2018-02-28 11:38:47 -08001184 std::string sync_group;
Seth Hampson845e8782018-03-02 11:34:10 -08001185 if (!stream_ids.empty()) {
1186 sync_group = stream_ids[0];
Steve Anton5a26a3a2018-02-28 11:38:47 -08001187 }
solenberg4904fb62017-02-17 12:01:14 -08001188 if (config_.sync_group != sync_group) {
Fredrik Solenberg3b903d02018-01-10 15:17:10 +01001189 RTC_LOG(LS_INFO) << "Recreating AudioReceiveStream for SSRC="
1190 << config_.rtp.remote_ssrc
1191 << " because of sync group change.";
solenberg4904fb62017-02-17 12:01:14 -08001192 config_.sync_group = sync_group;
1193 RecreateAudioReceiveStream();
1194 }
1195 }
1196
solenberg7add0582015-11-20 09:59:34 -08001197 webrtc::AudioReceiveStream::Stats GetStats() const {
1198 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
1199 RTC_DCHECK(stream_);
1200 return stream_->GetStats();
1201 }
1202
kwiberg686a8ef2016-02-26 03:00:35 -08001203 void SetRawAudioSink(std::unique_ptr<webrtc::AudioSinkInterface> sink) {
Tommif888bb52015-12-12 01:37:01 +01001204 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
Oskar Sundbom4ccc1c42018-03-16 13:56:27 +01001205 // Need to update the stream's sink first; once raw_audio_sink_ is
1206 // reassigned, whatever was in there before is destroyed.
1207 stream_->SetSink(sink.get());
Fredrik Solenberg8f5787a2018-01-11 13:52:30 +01001208 raw_audio_sink_ = std::move(sink);
Tommif888bb52015-12-12 01:37:01 +01001209 }
1210
solenberg217fb662016-06-17 08:30:54 -07001211 void SetOutputVolume(double volume) {
1212 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
Oskar Sundbomc6681082018-02-19 14:09:21 +01001213 output_volume_ = volume;
solenberg217fb662016-06-17 08:30:54 -07001214 stream_->SetGain(volume);
1215 }
1216
aleloi84ef6152016-08-04 05:28:21 -07001217 void SetPlayout(bool playout) {
1218 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
1219 RTC_DCHECK(stream_);
1220 if (playout) {
aleloi84ef6152016-08-04 05:28:21 -07001221 stream_->Start();
1222 } else {
aleloi84ef6152016-08-04 05:28:21 -07001223 stream_->Stop();
1224 }
aleloi18e0b672016-10-04 02:45:47 -07001225 playout_ = playout;
aleloi84ef6152016-08-04 05:28:21 -07001226 }
1227
hbos8d609f62017-04-10 07:39:05 -07001228 std::vector<webrtc::RtpSource> GetSources() {
1229 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
1230 RTC_DCHECK(stream_);
1231 return stream_->GetSources();
1232 }
1233
solenbergc96df772015-10-21 13:01:53 -07001234 private:
kwibergd32bf752017-01-19 07:03:59 -08001235 void RecreateAudioReceiveStream() {
solenberg7add0582015-11-20 09:59:34 -08001236 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
1237 if (stream_) {
1238 call_->DestroyAudioReceiveStream(stream_);
solenberg7add0582015-11-20 09:59:34 -08001239 }
solenberg7add0582015-11-20 09:59:34 -08001240 stream_ = call_->CreateAudioReceiveStream(config_);
1241 RTC_CHECK(stream_);
Oskar Sundbomc6681082018-02-19 14:09:21 +01001242 stream_->SetGain(output_volume_);
aleloi18e0b672016-10-04 02:45:47 -07001243 SetPlayout(playout_);
Fredrik Solenberg8f5787a2018-01-11 13:52:30 +01001244 stream_->SetSink(raw_audio_sink_.get());
solenberg7add0582015-11-20 09:59:34 -08001245 }
1246
Fredrik Solenberg3b903d02018-01-10 15:17:10 +01001247 void ReconfigureAudioReceiveStream() {
1248 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
1249 RTC_DCHECK(stream_);
1250 stream_->Reconfigure(config_);
1251 }
1252
solenberg7add0582015-11-20 09:59:34 -08001253 rtc::ThreadChecker worker_thread_checker_;
1254 webrtc::Call* call_ = nullptr;
1255 webrtc::AudioReceiveStream::Config config_;
1256 // The stream is owned by WebRtcAudioReceiveStream and may be reallocated if
1257 // configuration changes.
1258 webrtc::AudioReceiveStream* stream_ = nullptr;
aleloi18e0b672016-10-04 02:45:47 -07001259 bool playout_ = false;
Oskar Sundbomc6681082018-02-19 14:09:21 +01001260 float output_volume_ = 1.0;
Fredrik Solenberg8f5787a2018-01-11 13:52:30 +01001261 std::unique_ptr<webrtc::AudioSinkInterface> raw_audio_sink_;
solenbergc96df772015-10-21 13:01:53 -07001262
1263 RTC_DISALLOW_IMPLICIT_CONSTRUCTORS(WebRtcAudioReceiveStream);
henrike@webrtc.org1e09a712013-07-26 19:17:59 +00001264};
1265
Fredrik Solenberg709ed672015-09-15 12:26:33 +02001266WebRtcVoiceMediaChannel::WebRtcVoiceMediaChannel(WebRtcVoiceEngine* engine,
nisse51542be2016-02-12 02:27:06 -08001267 const MediaConfig& config,
Fredrik Solenbergb071a192015-09-17 16:42:56 +02001268 const AudioOptions& options,
Fredrik Solenberg709ed672015-09-15 12:26:33 +02001269 webrtc::Call* call)
nisse51542be2016-02-12 02:27:06 -08001270 : VoiceMediaChannel(config), engine_(engine), call_(call) {
Mirko Bonadei675513b2017-11-09 11:09:25 +01001271 RTC_LOG(LS_VERBOSE) << "WebRtcVoiceMediaChannel::WebRtcVoiceMediaChannel";
solenberg566ef242015-11-06 15:34:49 -08001272 RTC_DCHECK(call);
solenberg0a617e22015-10-20 15:49:38 -07001273 engine->RegisterChannel(this);
Fredrik Solenbergb071a192015-09-17 16:42:56 +02001274 SetOptions(options);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001275}
1276
1277WebRtcVoiceMediaChannel::~WebRtcVoiceMediaChannel() {
solenberg566ef242015-11-06 15:34:49 -08001278 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
Mirko Bonadei675513b2017-11-09 11:09:25 +01001279 RTC_LOG(LS_VERBOSE) << "WebRtcVoiceMediaChannel::~WebRtcVoiceMediaChannel";
solenberg7add0582015-11-20 09:59:34 -08001280 // TODO(solenberg): Should be able to delete the streams directly, without
1281 // going through RemoveNnStream(), once stream objects handle
1282 // all (de)configuration.
solenbergc96df772015-10-21 13:01:53 -07001283 while (!send_streams_.empty()) {
1284 RemoveSendStream(send_streams_.begin()->first);
solenbergd97ec302015-10-07 01:40:33 -07001285 }
solenberg7add0582015-11-20 09:59:34 -08001286 while (!recv_streams_.empty()) {
1287 RemoveRecvStream(recv_streams_.begin()->first);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001288 }
solenberg0a617e22015-10-20 15:49:38 -07001289 engine()->UnregisterChannel(this);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001290}
1291
nisse51542be2016-02-12 02:27:06 -08001292rtc::DiffServCodePoint WebRtcVoiceMediaChannel::PreferredDscp() const {
1293 return kAudioDscpValue;
1294}
1295
Peter Thatcherc2ee2c82015-08-07 16:05:34 -07001296bool WebRtcVoiceMediaChannel::SetSendParameters(
1297 const AudioSendParameters& params) {
Peter Boströmca8b4042016-03-08 14:24:13 -08001298 TRACE_EVENT0("webrtc", "WebRtcVoiceMediaChannel::SetSendParameters");
solenberg566ef242015-11-06 15:34:49 -08001299 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
Mirko Bonadei675513b2017-11-09 11:09:25 +01001300 RTC_LOG(LS_INFO) << "WebRtcVoiceMediaChannel::SetSendParameters: "
1301 << params.ToString();
Peter Thatcherc2ee2c82015-08-07 16:05:34 -07001302 // TODO(pthatcher): Refactor this to be more clean now that we have
1303 // all the information at once.
solenberg3a941542015-11-16 07:34:50 -08001304
1305 if (!SetSendCodecs(params.codecs)) {
1306 return false;
1307 }
1308
solenberg7e4e01a2015-12-02 08:05:01 -08001309 if (!ValidateRtpExtensions(params.extensions)) {
1310 return false;
1311 }
1312 std::vector<webrtc::RtpExtension> filtered_extensions =
1313 FilterRtpExtensions(params.extensions,
1314 webrtc::RtpExtension::IsSupportedForAudio, true);
1315 if (send_rtp_extensions_ != filtered_extensions) {
1316 send_rtp_extensions_.swap(filtered_extensions);
solenberg3a941542015-11-16 07:34:50 -08001317 for (auto& it : send_streams_) {
ossu20a4b3f2017-04-27 02:08:52 -07001318 it.second->SetRtpExtensions(send_rtp_extensions_);
solenberg3a941542015-11-16 07:34:50 -08001319 }
1320 }
Steve Antonbb50ce52018-03-26 10:24:32 -07001321 if (!params.mid.empty()) {
1322 mid_ = params.mid;
1323 for (auto& it : send_streams_) {
1324 it.second->SetMid(params.mid);
1325 }
1326 }
solenberg3a941542015-11-16 07:34:50 -08001327
deadbeef80346142016-04-27 14:17:10 -07001328 if (!SetMaxSendBitrate(params.max_bandwidth_bps)) {
solenberg3a941542015-11-16 07:34:50 -08001329 return false;
1330 }
1331 return SetOptions(params.options);
Peter Thatcherc2ee2c82015-08-07 16:05:34 -07001332}
1333
1334bool WebRtcVoiceMediaChannel::SetRecvParameters(
1335 const AudioRecvParameters& params) {
Peter Boströmca8b4042016-03-08 14:24:13 -08001336 TRACE_EVENT0("webrtc", "WebRtcVoiceMediaChannel::SetRecvParameters");
solenberg566ef242015-11-06 15:34:49 -08001337 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
Mirko Bonadei675513b2017-11-09 11:09:25 +01001338 RTC_LOG(LS_INFO) << "WebRtcVoiceMediaChannel::SetRecvParameters: "
1339 << params.ToString();
Peter Thatcherc2ee2c82015-08-07 16:05:34 -07001340 // TODO(pthatcher): Refactor this to be more clean now that we have
1341 // all the information at once.
solenberg7add0582015-11-20 09:59:34 -08001342
1343 if (!SetRecvCodecs(params.codecs)) {
1344 return false;
1345 }
1346
solenberg7e4e01a2015-12-02 08:05:01 -08001347 if (!ValidateRtpExtensions(params.extensions)) {
1348 return false;
1349 }
1350 std::vector<webrtc::RtpExtension> filtered_extensions =
1351 FilterRtpExtensions(params.extensions,
1352 webrtc::RtpExtension::IsSupportedForAudio, false);
1353 if (recv_rtp_extensions_ != filtered_extensions) {
1354 recv_rtp_extensions_.swap(filtered_extensions);
solenberg7add0582015-11-20 09:59:34 -08001355 for (auto& it : recv_streams_) {
Fredrik Solenberg4613bdf2018-01-16 13:32:31 +01001356 it.second->SetRtpExtensionsAndRecreateStream(recv_rtp_extensions_);
solenberg7add0582015-11-20 09:59:34 -08001357 }
1358 }
solenberg7add0582015-11-20 09:59:34 -08001359 return true;
Peter Thatcherc2ee2c82015-08-07 16:05:34 -07001360}
1361
Taylor Brandstetterdb0cd9e2016-05-16 11:40:30 -07001362webrtc::RtpParameters WebRtcVoiceMediaChannel::GetRtpSendParameters(
skvlade0d46372016-04-07 22:59:22 -07001363 uint32_t ssrc) const {
1364 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
1365 auto it = send_streams_.find(ssrc);
1366 if (it == send_streams_.end()) {
Mirko Bonadei675513b2017-11-09 11:09:25 +01001367 RTC_LOG(LS_WARNING) << "Attempting to get RTP send parameters for stream "
1368 << "with ssrc " << ssrc << " which doesn't exist.";
skvlade0d46372016-04-07 22:59:22 -07001369 return webrtc::RtpParameters();
1370 }
1371
Taylor Brandstetter0cd086b2016-04-20 16:23:10 -07001372 webrtc::RtpParameters rtp_params = it->second->rtp_parameters();
1373 // Need to add the common list of codecs to the send stream-specific
1374 // RTP parameters.
1375 for (const AudioCodec& codec : send_codecs_) {
1376 rtp_params.codecs.push_back(codec.ToCodecParameters());
1377 }
1378 return rtp_params;
skvlade0d46372016-04-07 22:59:22 -07001379}
1380
Zach Steinba37b4b2018-01-23 15:02:36 -08001381webrtc::RTCError WebRtcVoiceMediaChannel::SetRtpSendParameters(
skvlade0d46372016-04-07 22:59:22 -07001382 uint32_t ssrc,
1383 const webrtc::RtpParameters& parameters) {
1384 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
skvlade0d46372016-04-07 22:59:22 -07001385 auto it = send_streams_.find(ssrc);
1386 if (it == send_streams_.end()) {
Mirko Bonadei675513b2017-11-09 11:09:25 +01001387 RTC_LOG(LS_WARNING) << "Attempting to set RTP send parameters for stream "
1388 << "with ssrc " << ssrc << " which doesn't exist.";
Zach Steinba37b4b2018-01-23 15:02:36 -08001389 return webrtc::RTCError(webrtc::RTCErrorType::INTERNAL_ERROR);
skvlade0d46372016-04-07 22:59:22 -07001390 }
1391
Taylor Brandstetterdb0cd9e2016-05-16 11:40:30 -07001392 // TODO(deadbeef): Handle setting parameters with a list of codecs in a
1393 // different order (which should change the send codec).
1394 webrtc::RtpParameters current_parameters = GetRtpSendParameters(ssrc);
1395 if (current_parameters.codecs != parameters.codecs) {
Mirko Bonadei675513b2017-11-09 11:09:25 +01001396 RTC_LOG(LS_ERROR) << "Using SetParameters to change the set of codecs "
1397 << "is not currently supported.";
Zach Steinba37b4b2018-01-23 15:02:36 -08001398 return webrtc::RTCError(webrtc::RTCErrorType::UNSUPPORTED_PARAMETER);
Taylor Brandstetterdb0cd9e2016-05-16 11:40:30 -07001399 }
1400
minyue7a973442016-10-20 03:27:12 -07001401 // TODO(minyue): The following legacy actions go into
1402 // |WebRtcAudioSendStream::SetRtpParameters()| which is called at the end,
1403 // though there are two difference:
1404 // 1. |WebRtcVoiceMediaChannel::SetChannelSendParameters()| only calls
1405 // |SetSendCodec| while |WebRtcAudioSendStream::SetRtpParameters()| calls
1406 // |SetSendCodecs|. The outcome should be the same.
1407 // 2. AudioSendStream can be recreated.
1408
Taylor Brandstetter0cd086b2016-04-20 16:23:10 -07001409 // Codecs are handled at the WebRtcVoiceMediaChannel level.
1410 webrtc::RtpParameters reduced_params = parameters;
1411 reduced_params.codecs.clear();
minyue7a973442016-10-20 03:27:12 -07001412 return it->second->SetRtpParameters(reduced_params);
skvlade0d46372016-04-07 22:59:22 -07001413}
1414
Taylor Brandstetterdb0cd9e2016-05-16 11:40:30 -07001415webrtc::RtpParameters WebRtcVoiceMediaChannel::GetRtpReceiveParameters(
1416 uint32_t ssrc) const {
1417 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
deadbeef3bc15102017-04-20 19:25:07 -07001418 webrtc::RtpParameters rtp_params;
1419 // SSRC of 0 represents the default receive stream.
1420 if (ssrc == 0) {
1421 if (!default_sink_) {
Mirko Bonadei675513b2017-11-09 11:09:25 +01001422 RTC_LOG(LS_WARNING)
1423 << "Attempting to get RTP parameters for the default, "
1424 "unsignaled audio receive stream, but not yet "
1425 "configured to receive such a stream.";
deadbeef3bc15102017-04-20 19:25:07 -07001426 return rtp_params;
1427 }
1428 rtp_params.encodings.emplace_back();
1429 } else {
1430 auto it = recv_streams_.find(ssrc);
1431 if (it == recv_streams_.end()) {
Mirko Bonadei675513b2017-11-09 11:09:25 +01001432 RTC_LOG(LS_WARNING)
1433 << "Attempting to get RTP receive parameters for stream "
1434 << "with ssrc " << ssrc << " which doesn't exist.";
deadbeef3bc15102017-04-20 19:25:07 -07001435 return webrtc::RtpParameters();
1436 }
1437 rtp_params.encodings.emplace_back();
1438 // TODO(deadbeef): Return stream-specific parameters.
Oskar Sundbom78807582017-11-16 11:09:55 +01001439 rtp_params.encodings[0].ssrc = ssrc;
Taylor Brandstetterdb0cd9e2016-05-16 11:40:30 -07001440 }
1441
Taylor Brandstetterdb0cd9e2016-05-16 11:40:30 -07001442 for (const AudioCodec& codec : recv_codecs_) {
1443 rtp_params.codecs.push_back(codec.ToCodecParameters());
1444 }
1445 return rtp_params;
1446}
1447
1448bool WebRtcVoiceMediaChannel::SetRtpReceiveParameters(
1449 uint32_t ssrc,
1450 const webrtc::RtpParameters& parameters) {
1451 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
deadbeef3bc15102017-04-20 19:25:07 -07001452 // SSRC of 0 represents the default receive stream.
1453 if (ssrc == 0) {
1454 if (!default_sink_) {
Mirko Bonadei675513b2017-11-09 11:09:25 +01001455 RTC_LOG(LS_WARNING)
1456 << "Attempting to set RTP parameters for the default, "
1457 "unsignaled audio receive stream, but not yet "
1458 "configured to receive such a stream.";
deadbeef3bc15102017-04-20 19:25:07 -07001459 return false;
1460 }
1461 } else {
1462 auto it = recv_streams_.find(ssrc);
1463 if (it == recv_streams_.end()) {
Mirko Bonadei675513b2017-11-09 11:09:25 +01001464 RTC_LOG(LS_WARNING)
1465 << "Attempting to set RTP receive parameters for stream "
1466 << "with ssrc " << ssrc << " which doesn't exist.";
deadbeef3bc15102017-04-20 19:25:07 -07001467 return false;
1468 }
Taylor Brandstetterdb0cd9e2016-05-16 11:40:30 -07001469 }
1470
1471 webrtc::RtpParameters current_parameters = GetRtpReceiveParameters(ssrc);
1472 if (current_parameters != parameters) {
Mirko Bonadei675513b2017-11-09 11:09:25 +01001473 RTC_LOG(LS_ERROR) << "Changing the RTP receive parameters is currently "
1474 << "unsupported.";
Taylor Brandstetterdb0cd9e2016-05-16 11:40:30 -07001475 return false;
1476 }
1477 return true;
1478}
1479
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001480bool WebRtcVoiceMediaChannel::SetOptions(const AudioOptions& options) {
solenberg566ef242015-11-06 15:34:49 -08001481 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
Mirko Bonadei675513b2017-11-09 11:09:25 +01001482 RTC_LOG(LS_INFO) << "Setting voice channel options: " << options.ToString();
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001483
1484 // We retain all of the existing options, and apply the given ones
1485 // on top. This means there is no way to "clear" options such that
1486 // they go back to the engine default.
1487 options_.SetAll(options);
solenberg246b8172015-12-08 09:50:23 -08001488 if (!engine()->ApplyOptions(options_)) {
Mirko Bonadei675513b2017-11-09 11:09:25 +01001489 RTC_LOG(LS_WARNING)
1490 << "Failed to apply engine options during channel SetOptions.";
solenberg246b8172015-12-08 09:50:23 -08001491 return false;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001492 }
minyue6b825df2016-10-31 04:08:32 -07001493
ossu20a4b3f2017-04-27 02:08:52 -07001494 rtc::Optional<std::string> audio_network_adaptor_config =
minyue6b825df2016-10-31 04:08:32 -07001495 GetAudioNetworkAdaptorConfig(options_);
1496 for (auto& it : send_streams_) {
ossu20a4b3f2017-04-27 02:08:52 -07001497 it.second->SetAudioNetworkAdaptorConfig(audio_network_adaptor_config);
minyue6b825df2016-10-31 04:08:32 -07001498 }
1499
Mirko Bonadei675513b2017-11-09 11:09:25 +01001500 RTC_LOG(LS_INFO) << "Set voice channel options. Current options: "
1501 << options_.ToString();
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001502 return true;
1503}
1504
1505bool WebRtcVoiceMediaChannel::SetRecvCodecs(
1506 const std::vector<AudioCodec>& codecs) {
solenberg566ef242015-11-06 15:34:49 -08001507 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
solenberg8fb30c32015-10-13 03:06:58 -07001508
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001509 // Set the payload types to be used for incoming media.
Mirko Bonadei675513b2017-11-09 11:09:25 +01001510 RTC_LOG(LS_INFO) << "Setting receive voice codecs.";
solenberg0b675462015-10-09 01:37:09 -07001511
1512 if (!VerifyUniquePayloadTypes(codecs)) {
Mirko Bonadei675513b2017-11-09 11:09:25 +01001513 RTC_LOG(LS_ERROR) << "Codec payload types overlap.";
solenberg0b675462015-10-09 01:37:09 -07001514 return false;
1515 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001516
kwibergd32bf752017-01-19 07:03:59 -08001517 // Create a payload type -> SdpAudioFormat map with all the decoders. Fail
1518 // unless the factory claims to support all decoders.
1519 std::map<int, webrtc::SdpAudioFormat> decoder_map;
1520 for (const AudioCodec& codec : codecs) {
deadbeefcb383672017-04-26 16:28:42 -07001521 // Log a warning if a codec's payload type is changing. This used to be
1522 // treated as an error. It's abnormal, but not really illegal.
1523 AudioCodec old_codec;
1524 if (FindCodec(recv_codecs_, codec, &old_codec) &&
1525 old_codec.id != codec.id) {
Mirko Bonadei675513b2017-11-09 11:09:25 +01001526 RTC_LOG(LS_WARNING) << codec.name << " mapped to a second payload type ("
1527 << codec.id << ", was already mapped to "
1528 << old_codec.id << ")";
deadbeefcb383672017-04-26 16:28:42 -07001529 }
kwibergd32bf752017-01-19 07:03:59 -08001530 auto format = AudioCodecToSdpAudioFormat(codec);
1531 if (!IsCodec(codec, "cn") && !IsCodec(codec, "telephone-event") &&
1532 !engine()->decoder_factory_->IsSupportedDecoder(format)) {
Jonas Olssonabbe8412018-04-03 13:40:05 +02001533 RTC_LOG(LS_ERROR) << "Unsupported codec: " << rtc::ToString(format);
kwibergd32bf752017-01-19 07:03:59 -08001534 return false;
1535 }
deadbeefcb383672017-04-26 16:28:42 -07001536 // We allow adding new codecs but don't allow changing the payload type of
1537 // codecs that are already configured since we might already be receiving
1538 // packets with that payload type. See RFC3264, Section 8.3.2.
1539 // TODO(deadbeef): Also need to check for clashes with previously mapped
1540 // payload types, and not just currently mapped ones. For example, this
1541 // should be illegal:
1542 // 1. {100: opus/48000/2, 101: ISAC/16000}
1543 // 2. {100: opus/48000/2}
1544 // 3. {100: opus/48000/2, 101: ISAC/32000}
1545 // Though this check really should happen at a higher level, since this
1546 // conflict could happen between audio and video codecs.
1547 auto existing = decoder_map_.find(codec.id);
1548 if (existing != decoder_map_.end() && !existing->second.Matches(format)) {
Mirko Bonadei675513b2017-11-09 11:09:25 +01001549 RTC_LOG(LS_ERROR) << "Attempting to use payload type " << codec.id
1550 << " for " << codec.name
1551 << ", but it is already used for "
1552 << existing->second.name;
deadbeefcb383672017-04-26 16:28:42 -07001553 return false;
1554 }
kwibergd32bf752017-01-19 07:03:59 -08001555 decoder_map.insert({codec.id, std::move(format)});
1556 }
1557
deadbeefcb383672017-04-26 16:28:42 -07001558 if (decoder_map == decoder_map_) {
1559 // There's nothing new to configure.
1560 return true;
1561 }
1562
kwiberg37b8b112016-11-03 02:46:53 -07001563 if (playout_) {
1564 // Receive codecs can not be changed while playing. So we temporarily
1565 // pause playout.
1566 ChangePlayout(false);
1567 }
1568
kwiberg1c07c702017-03-27 07:15:49 -07001569 decoder_map_ = std::move(decoder_map);
kwibergd32bf752017-01-19 07:03:59 -08001570 for (auto& kv : recv_streams_) {
Fredrik Solenberg3b903d02018-01-10 15:17:10 +01001571 kv.second->SetDecoderMap(decoder_map_);
solenberg26c8c912015-11-27 04:00:25 -08001572 }
kwibergd32bf752017-01-19 07:03:59 -08001573 recv_codecs_ = codecs;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001574
kwiberg37b8b112016-11-03 02:46:53 -07001575 if (desired_playout_ && !playout_) {
1576 ChangePlayout(desired_playout_);
1577 }
kwibergd32bf752017-01-19 07:03:59 -08001578 return true;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001579}
1580
solenberg72e29d22016-03-08 06:35:16 -08001581// Utility function called from SetSendParameters() to extract current send
1582// codec settings from the given list of codecs (originally from SDP). Both send
1583// and receive streams may be reconfigured based on the new settings.
wu@webrtc.orgcadf9042013-08-30 21:24:16 +00001584bool WebRtcVoiceMediaChannel::SetSendCodecs(
1585 const std::vector<AudioCodec>& codecs) {
solenberg566ef242015-11-06 15:34:49 -08001586 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
Oskar Sundbom78807582017-11-16 11:09:55 +01001587 dtmf_payload_type_ = rtc::nullopt;
solenbergffbbcac2016-11-17 05:25:37 -08001588 dtmf_payload_freq_ = -1;
1589
1590 // Validate supplied codecs list.
1591 for (const AudioCodec& codec : codecs) {
1592 // TODO(solenberg): Validate more aspects of input - that payload types
1593 // don't overlap, remove redundant/unsupported codecs etc -
1594 // the same way it is done for RtpHeaderExtensions.
1595 if (codec.id < kMinPayloadType || codec.id > kMaxPayloadType) {
Mirko Bonadei675513b2017-11-09 11:09:25 +01001596 RTC_LOG(LS_WARNING) << "Codec payload type out of range: "
1597 << ToString(codec);
solenbergffbbcac2016-11-17 05:25:37 -08001598 return false;
1599 }
1600 }
1601
1602 // Find PT of telephone-event codec with lowest clockrate, as a fallback, in
1603 // case we don't have a DTMF codec with a rate matching the send codec's, or
1604 // if this function returns early.
1605 std::vector<AudioCodec> dtmf_codecs;
Fredrik Solenbergaf9fb212015-08-26 10:45:53 +02001606 for (const AudioCodec& codec : codecs) {
Fredrik Solenbergaf9fb212015-08-26 10:45:53 +02001607 if (IsCodec(codec, kDtmfCodecName)) {
solenbergffbbcac2016-11-17 05:25:37 -08001608 dtmf_codecs.push_back(codec);
1609 if (!dtmf_payload_type_ || codec.clockrate < dtmf_payload_freq_) {
Oskar Sundbom78807582017-11-16 11:09:55 +01001610 dtmf_payload_type_ = codec.id;
solenbergffbbcac2016-11-17 05:25:37 -08001611 dtmf_payload_freq_ = codec.clockrate;
solenberg31642aa2016-03-14 08:00:37 -07001612 }
wu@webrtc.orgcadf9042013-08-30 21:24:16 +00001613 }
1614 }
1615
ossu20a4b3f2017-04-27 02:08:52 -07001616 // Scan through the list to figure out the codec to use for sending.
1617 rtc::Optional<webrtc::AudioSendStream::Config::SendCodecSpec> send_codec_spec;
Sebastian Janssonfc8d26b2018-02-21 09:52:06 +01001618 webrtc::BitrateConstraints bitrate_config;
ossu20a4b3f2017-04-27 02:08:52 -07001619 rtc::Optional<webrtc::AudioCodecInfo> voice_codec_info;
1620 for (const AudioCodec& voice_codec : codecs) {
1621 if (!(IsCodec(voice_codec, kCnCodecName) ||
1622 IsCodec(voice_codec, kDtmfCodecName) ||
1623 IsCodec(voice_codec, kRedCodecName))) {
1624 webrtc::SdpAudioFormat format(voice_codec.name, voice_codec.clockrate,
1625 voice_codec.channels, voice_codec.params);
solenberg72e29d22016-03-08 06:35:16 -08001626
ossu20a4b3f2017-04-27 02:08:52 -07001627 voice_codec_info = engine()->encoder_factory_->QueryAudioEncoder(format);
1628 if (!voice_codec_info) {
Mirko Bonadei675513b2017-11-09 11:09:25 +01001629 RTC_LOG(LS_WARNING) << "Unknown codec " << ToString(voice_codec);
solenberg72e29d22016-03-08 06:35:16 -08001630 continue;
1631 }
1632
Oskar Sundbom78807582017-11-16 11:09:55 +01001633 send_codec_spec = webrtc::AudioSendStream::Config::SendCodecSpec(
1634 voice_codec.id, format);
ossu20a4b3f2017-04-27 02:08:52 -07001635 if (voice_codec.bitrate > 0) {
Oskar Sundbom78807582017-11-16 11:09:55 +01001636 send_codec_spec->target_bitrate_bps = voice_codec.bitrate;
ossu20a4b3f2017-04-27 02:08:52 -07001637 }
1638 send_codec_spec->transport_cc_enabled = HasTransportCc(voice_codec);
1639 send_codec_spec->nack_enabled = HasNack(voice_codec);
1640 bitrate_config = GetBitrateConfigForCodec(voice_codec);
1641 break;
1642 }
1643 }
1644
1645 if (!send_codec_spec) {
1646 return false;
1647 }
1648
1649 RTC_DCHECK(voice_codec_info);
1650 if (voice_codec_info->allow_comfort_noise) {
1651 // Loop through the codecs list again to find the CN codec.
1652 // TODO(solenberg): Break out into a separate function?
1653 for (const AudioCodec& cn_codec : codecs) {
ossu0c4b8492017-03-02 11:03:25 -08001654 if (IsCodec(cn_codec, kCnCodecName) &&
ossu20a4b3f2017-04-27 02:08:52 -07001655 cn_codec.clockrate == send_codec_spec->format.clockrate_hz) {
ossu0c4b8492017-03-02 11:03:25 -08001656 switch (cn_codec.clockrate) {
solenberg72e29d22016-03-08 06:35:16 -08001657 case 8000:
1658 case 16000:
1659 case 32000:
Oskar Sundbom78807582017-11-16 11:09:55 +01001660 send_codec_spec->cng_payload_type = cn_codec.id;
solenberg72e29d22016-03-08 06:35:16 -08001661 break;
1662 default:
Mirko Bonadei675513b2017-11-09 11:09:25 +01001663 RTC_LOG(LS_WARNING)
1664 << "CN frequency " << cn_codec.clockrate << " not supported.";
ossu20a4b3f2017-04-27 02:08:52 -07001665 break;
solenberg72e29d22016-03-08 06:35:16 -08001666 }
solenberg72e29d22016-03-08 06:35:16 -08001667 break;
1668 }
1669 }
solenbergffbbcac2016-11-17 05:25:37 -08001670
1671 // Find the telephone-event PT exactly matching the preferred send codec.
1672 for (const AudioCodec& dtmf_codec : dtmf_codecs) {
ossu20a4b3f2017-04-27 02:08:52 -07001673 if (dtmf_codec.clockrate == send_codec_spec->format.clockrate_hz) {
Oskar Sundbom78807582017-11-16 11:09:55 +01001674 dtmf_payload_type_ = dtmf_codec.id;
solenbergffbbcac2016-11-17 05:25:37 -08001675 dtmf_payload_freq_ = dtmf_codec.clockrate;
1676 break;
1677 }
1678 }
solenberg72e29d22016-03-08 06:35:16 -08001679 }
1680
solenberg971cab02016-06-14 10:02:41 -07001681 if (send_codec_spec_ != send_codec_spec) {
1682 send_codec_spec_ = std::move(send_codec_spec);
stefan13f1a0a2016-11-30 07:22:58 -08001683 // Apply new settings to all streams.
solenberg971cab02016-06-14 10:02:41 -07001684 for (const auto& kv : send_streams_) {
ossu20a4b3f2017-04-27 02:08:52 -07001685 kv.second->SetSendCodecSpec(*send_codec_spec_);
wu@webrtc.orgcadf9042013-08-30 21:24:16 +00001686 }
stefan13f1a0a2016-11-30 07:22:58 -08001687 } else {
1688 // If the codec isn't changing, set the start bitrate to -1 which means
1689 // "unchanged" so that BWE isn't affected.
stefan1ccf73f2017-03-27 03:51:18 -07001690 bitrate_config.start_bitrate_bps = -1;
wu@webrtc.orgcadf9042013-08-30 21:24:16 +00001691 }
Sebastian Jansson8f83b422018-02-21 13:07:13 +01001692 call_->GetTransportControllerSend()->SetSdpBitrateParameters(bitrate_config);
wu@webrtc.orgcadf9042013-08-30 21:24:16 +00001693
solenberg8189b022016-06-14 12:13:00 -07001694 // Check if the transport cc feedback or NACK status has changed on the
1695 // preferred send codec, and in that case reconfigure all receive streams.
ossu20a4b3f2017-04-27 02:08:52 -07001696 if (recv_transport_cc_enabled_ != send_codec_spec_->transport_cc_enabled ||
1697 recv_nack_enabled_ != send_codec_spec_->nack_enabled) {
Mirko Bonadei675513b2017-11-09 11:09:25 +01001698 RTC_LOG(LS_INFO) << "Recreate all the receive streams because the send "
1699 "codec has changed.";
ossu20a4b3f2017-04-27 02:08:52 -07001700 recv_transport_cc_enabled_ = send_codec_spec_->transport_cc_enabled;
1701 recv_nack_enabled_ = send_codec_spec_->nack_enabled;
solenberg72e29d22016-03-08 06:35:16 -08001702 for (auto& kv : recv_streams_) {
Fredrik Solenberg4613bdf2018-01-16 13:32:31 +01001703 kv.second->SetUseTransportCcAndRecreateStream(recv_transport_cc_enabled_,
1704 recv_nack_enabled_);
solenberg72e29d22016-03-08 06:35:16 -08001705 }
1706 }
1707
Taylor Brandstetter0cd086b2016-04-20 16:23:10 -07001708 send_codecs_ = codecs;
solenberg72e29d22016-03-08 06:35:16 -08001709 return true;
1710}
1711
aleloi84ef6152016-08-04 05:28:21 -07001712void WebRtcVoiceMediaChannel::SetPlayout(bool playout) {
kwiberg37b8b112016-11-03 02:46:53 -07001713 desired_playout_ = playout;
1714 return ChangePlayout(desired_playout_);
1715}
1716
1717void WebRtcVoiceMediaChannel::ChangePlayout(bool playout) {
1718 TRACE_EVENT0("webrtc", "WebRtcVoiceMediaChannel::ChangePlayout");
solenberg566ef242015-11-06 15:34:49 -08001719 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001720 if (playout_ == playout) {
aleloi84ef6152016-08-04 05:28:21 -07001721 return;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001722 }
1723
aleloi84ef6152016-08-04 05:28:21 -07001724 for (const auto& kv : recv_streams_) {
1725 kv.second->SetPlayout(playout);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001726 }
solenberg1ac56142015-10-13 03:58:19 -07001727 playout_ = playout;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001728}
1729
Taylor Brandstetter1a018dc2016-03-08 12:37:39 -08001730void WebRtcVoiceMediaChannel::SetSend(bool send) {
Peter Boströmca8b4042016-03-08 14:24:13 -08001731 TRACE_EVENT0("webrtc", "WebRtcVoiceMediaChannel::SetSend");
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001732 if (send_ == send) {
Taylor Brandstetter1a018dc2016-03-08 12:37:39 -08001733 return;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001734 }
1735
solenbergd53a3f92016-04-14 13:56:37 -07001736 // Apply channel specific options, and initialize the ADM for recording (this
1737 // may take time on some platforms, e.g. Android).
Taylor Brandstetter1a018dc2016-03-08 12:37:39 -08001738 if (send) {
solenberg63b34542015-09-29 06:06:31 -07001739 engine()->ApplyOptions(options_);
solenbergd53a3f92016-04-14 13:56:37 -07001740
1741 // InitRecording() may return an error if the ADM is already recording.
1742 if (!engine()->adm()->RecordingIsInitialized() &&
1743 !engine()->adm()->Recording()) {
1744 if (engine()->adm()->InitRecording() != 0) {
Mirko Bonadei675513b2017-11-09 11:09:25 +01001745 RTC_LOG(LS_WARNING) << "Failed to initialize recording";
solenbergd53a3f92016-04-14 13:56:37 -07001746 }
1747 }
solenberg63b34542015-09-29 06:06:31 -07001748 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001749
wu@webrtc.org9dba5252013-08-05 20:36:57 +00001750 // Change the settings on each send channel.
Taylor Brandstetter1a018dc2016-03-08 12:37:39 -08001751 for (auto& kv : send_streams_) {
1752 kv.second->SetSend(send);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001753 }
wu@webrtc.org9dba5252013-08-05 20:36:57 +00001754
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001755 send_ = send;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001756}
1757
Peter Boström0c4e06b2015-10-07 12:23:21 +02001758bool WebRtcVoiceMediaChannel::SetAudioSend(uint32_t ssrc,
1759 bool enable,
solenberg1dd98f32015-09-10 01:57:14 -07001760 const AudioOptions* options,
Taylor Brandstetter1a018dc2016-03-08 12:37:39 -08001761 AudioSource* source) {
solenberg566ef242015-11-06 15:34:49 -08001762 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
solenberg1dd98f32015-09-10 01:57:14 -07001763 // TODO(solenberg): The state change should be fully rolled back if any one of
1764 // these calls fail.
Taylor Brandstetter1a018dc2016-03-08 12:37:39 -08001765 if (!SetLocalSource(ssrc, source)) {
solenberg1dd98f32015-09-10 01:57:14 -07001766 return false;
1767 }
solenbergdfc8f4f2015-10-01 02:31:10 -07001768 if (!MuteStream(ssrc, !enable)) {
solenberg1dd98f32015-09-10 01:57:14 -07001769 return false;
1770 }
solenbergdfc8f4f2015-10-01 02:31:10 -07001771 if (enable && options) {
solenberg1dd98f32015-09-10 01:57:14 -07001772 return SetOptions(*options);
1773 }
1774 return true;
1775}
1776
wu@webrtc.org9dba5252013-08-05 20:36:57 +00001777bool WebRtcVoiceMediaChannel::AddSendStream(const StreamParams& sp) {
Peter Boströmca8b4042016-03-08 14:24:13 -08001778 TRACE_EVENT0("webrtc", "WebRtcVoiceMediaChannel::AddSendStream");
solenberg566ef242015-11-06 15:34:49 -08001779 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
Mirko Bonadei675513b2017-11-09 11:09:25 +01001780 RTC_LOG(LS_INFO) << "AddSendStream: " << sp.ToString();
solenberg0a617e22015-10-20 15:49:38 -07001781
1782 uint32_t ssrc = sp.first_ssrc();
1783 RTC_DCHECK(0 != ssrc);
1784
Fredrik Solenberg8f5787a2018-01-11 13:52:30 +01001785 if (send_streams_.find(ssrc) != send_streams_.end()) {
Mirko Bonadei675513b2017-11-09 11:09:25 +01001786 RTC_LOG(LS_ERROR) << "Stream already exists with ssrc " << ssrc;
wu@webrtc.org9dba5252013-08-05 20:36:57 +00001787 return false;
1788 }
1789
minyue6b825df2016-10-31 04:08:32 -07001790 rtc::Optional<std::string> audio_network_adaptor_config =
1791 GetAudioNetworkAdaptorConfig(options_);
skvlade0d46372016-04-07 22:59:22 -07001792 WebRtcAudioSendStream* stream = new WebRtcAudioSendStream(
Steve Antonbb50ce52018-03-26 10:24:32 -07001793 ssrc, mid_, sp.cname, sp.id, send_codec_spec_, send_rtp_extensions_,
Karl Wiberg77490b92018-03-21 15:18:42 +01001794 max_send_bitrate_bps_, audio_network_adaptor_config, call_, this,
1795 engine()->encoder_factory_, codec_pair_id_);
skvlade0d46372016-04-07 22:59:22 -07001796 send_streams_.insert(std::make_pair(ssrc, stream));
wu@webrtc.org9dba5252013-08-05 20:36:57 +00001797
solenberg4a0f7b52016-06-16 13:07:33 -07001798 // At this point the stream's local SSRC has been updated. If it is the first
1799 // send stream, make sure that all the receive streams are updated with the
1800 // same SSRC in order to send receiver reports.
solenbergc96df772015-10-21 13:01:53 -07001801 if (send_streams_.size() == 1) {
solenberg0a617e22015-10-20 15:49:38 -07001802 receiver_reports_ssrc_ = ssrc;
solenberg4a0f7b52016-06-16 13:07:33 -07001803 for (const auto& kv : recv_streams_) {
1804 // TODO(solenberg): Allow applications to set the RTCP SSRC of receive
Fredrik Solenberg3b903d02018-01-10 15:17:10 +01001805 // streams instead, so we can avoid reconfiguring the streams here.
1806 kv.second->SetLocalSsrc(ssrc);
wu@webrtc.org9dba5252013-08-05 20:36:57 +00001807 }
1808 }
1809
Taylor Brandstetter1a018dc2016-03-08 12:37:39 -08001810 send_streams_[ssrc]->SetSend(send_);
1811 return true;
wu@webrtc.org9dba5252013-08-05 20:36:57 +00001812}
1813
Peter Boström0c4e06b2015-10-07 12:23:21 +02001814bool WebRtcVoiceMediaChannel::RemoveSendStream(uint32_t ssrc) {
Peter Boströmca8b4042016-03-08 14:24:13 -08001815 TRACE_EVENT0("webrtc", "WebRtcVoiceMediaChannel::RemoveSendStream");
solenberg566ef242015-11-06 15:34:49 -08001816 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
Mirko Bonadei675513b2017-11-09 11:09:25 +01001817 RTC_LOG(LS_INFO) << "RemoveSendStream: " << ssrc;
solenberg3a941542015-11-16 07:34:50 -08001818
solenbergc96df772015-10-21 13:01:53 -07001819 auto it = send_streams_.find(ssrc);
1820 if (it == send_streams_.end()) {
Mirko Bonadei675513b2017-11-09 11:09:25 +01001821 RTC_LOG(LS_WARNING) << "Try to remove stream with ssrc " << ssrc
1822 << " which doesn't exist.";
wu@webrtc.org9dba5252013-08-05 20:36:57 +00001823 return false;
1824 }
1825
Taylor Brandstetter1a018dc2016-03-08 12:37:39 -08001826 it->second->SetSend(false);
wu@webrtc.org9dba5252013-08-05 20:36:57 +00001827
solenberg7602aab2016-11-14 11:30:07 -08001828 // TODO(solenberg): If we're removing the receiver_reports_ssrc_ stream, find
1829 // the first active send stream and use that instead, reassociating receive
1830 // streams.
1831
solenberg7add0582015-11-20 09:59:34 -08001832 delete it->second;
1833 send_streams_.erase(it);
solenbergc96df772015-10-21 13:01:53 -07001834 if (send_streams_.empty()) {
Taylor Brandstetter1a018dc2016-03-08 12:37:39 -08001835 SetSend(false);
solenberg0a617e22015-10-20 15:49:38 -07001836 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001837 return true;
1838}
1839
1840bool WebRtcVoiceMediaChannel::AddRecvStream(const StreamParams& sp) {
Peter Boströmca8b4042016-03-08 14:24:13 -08001841 TRACE_EVENT0("webrtc", "WebRtcVoiceMediaChannel::AddRecvStream");
solenberg566ef242015-11-06 15:34:49 -08001842 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
Mirko Bonadei675513b2017-11-09 11:09:25 +01001843 RTC_LOG(LS_INFO) << "AddRecvStream: " << sp.ToString();
solenbergd97ec302015-10-07 01:40:33 -07001844
solenberg0b675462015-10-09 01:37:09 -07001845 if (!ValidateStreamParams(sp)) {
wu@webrtc.org78187522013-10-07 23:32:02 +00001846 return false;
1847 }
1848
solenberg7add0582015-11-20 09:59:34 -08001849 const uint32_t ssrc = sp.first_ssrc();
solenberg0b675462015-10-09 01:37:09 -07001850 if (ssrc == 0) {
Mirko Bonadei675513b2017-11-09 11:09:25 +01001851 RTC_LOG(LS_WARNING) << "AddRecvStream with ssrc==0 is not supported.";
solenberg0b675462015-10-09 01:37:09 -07001852 return false;
1853 }
1854
solenberg2100c0b2017-03-01 11:29:29 -08001855 // If this stream was previously received unsignaled, we promote it, possibly
Seth Hampson845e8782018-03-02 11:34:10 -08001856 // recreating the AudioReceiveStream, if stream ids have changed.
solenberg2100c0b2017-03-01 11:29:29 -08001857 if (MaybeDeregisterUnsignaledRecvStream(ssrc)) {
Seth Hampson845e8782018-03-02 11:34:10 -08001858 recv_streams_[ssrc]->MaybeRecreateAudioReceiveStream(sp.stream_ids());
solenberg4904fb62017-02-17 12:01:14 -08001859 return true;
solenberg1ac56142015-10-13 03:58:19 -07001860 }
solenberg0b675462015-10-09 01:37:09 -07001861
Fredrik Solenberg8f5787a2018-01-11 13:52:30 +01001862 if (recv_streams_.find(ssrc) != recv_streams_.end()) {
Mirko Bonadei675513b2017-11-09 11:09:25 +01001863 RTC_LOG(LS_ERROR) << "Stream already exists with ssrc " << ssrc;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001864 return false;
1865 }
Fredrik Solenberg4b60c732015-05-07 14:07:48 +02001866
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001867 // Create a new channel for receiving audio data.
stefanba4c0e42016-02-04 04:12:24 -08001868 recv_streams_.insert(std::make_pair(
Steve Anton5a26a3a2018-02-28 11:38:47 -08001869 ssrc, new WebRtcAudioReceiveStream(
1870 ssrc, receiver_reports_ssrc_, recv_transport_cc_enabled_,
Seth Hampson845e8782018-03-02 11:34:10 -08001871 recv_nack_enabled_, sp.stream_ids(), recv_rtp_extensions_,
Steve Anton5a26a3a2018-02-28 11:38:47 -08001872 call_, this, engine()->decoder_factory_, decoder_map_,
Karl Wiberg08126342018-03-20 19:18:55 +01001873 codec_pair_id_, engine()->audio_jitter_buffer_max_packets_,
Steve Anton5a26a3a2018-02-28 11:38:47 -08001874 engine()->audio_jitter_buffer_fast_accelerate_)));
aleloi84ef6152016-08-04 05:28:21 -07001875 recv_streams_[ssrc]->SetPlayout(playout_);
solenberg7add0582015-11-20 09:59:34 -08001876
solenberg1ac56142015-10-13 03:58:19 -07001877 return true;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001878}
1879
Peter Boström0c4e06b2015-10-07 12:23:21 +02001880bool WebRtcVoiceMediaChannel::RemoveRecvStream(uint32_t ssrc) {
Peter Boströmca8b4042016-03-08 14:24:13 -08001881 TRACE_EVENT0("webrtc", "WebRtcVoiceMediaChannel::RemoveRecvStream");
solenberg566ef242015-11-06 15:34:49 -08001882 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
Mirko Bonadei675513b2017-11-09 11:09:25 +01001883 RTC_LOG(LS_INFO) << "RemoveRecvStream: " << ssrc;
solenbergd97ec302015-10-07 01:40:33 -07001884
solenberg7add0582015-11-20 09:59:34 -08001885 const auto it = recv_streams_.find(ssrc);
1886 if (it == recv_streams_.end()) {
Mirko Bonadei675513b2017-11-09 11:09:25 +01001887 RTC_LOG(LS_WARNING) << "Try to remove stream with ssrc " << ssrc
1888 << " which doesn't exist.";
henrike@webrtc.org1e09a712013-07-26 19:17:59 +00001889 return false;
wu@webrtc.org9dba5252013-08-05 20:36:57 +00001890 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001891
solenberg2100c0b2017-03-01 11:29:29 -08001892 MaybeDeregisterUnsignaledRecvStream(ssrc);
henrike@webrtc.org1e09a712013-07-26 19:17:59 +00001893
Tommif888bb52015-12-12 01:37:01 +01001894 it->second->SetRawAudioSink(nullptr);
solenberg7add0582015-11-20 09:59:34 -08001895 delete it->second;
1896 recv_streams_.erase(it);
Fredrik Solenberg8f5787a2018-01-11 13:52:30 +01001897 return true;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001898}
1899
Taylor Brandstetter1a018dc2016-03-08 12:37:39 -08001900bool WebRtcVoiceMediaChannel::SetLocalSource(uint32_t ssrc,
1901 AudioSource* source) {
solenbergc96df772015-10-21 13:01:53 -07001902 auto it = send_streams_.find(ssrc);
1903 if (it == send_streams_.end()) {
Taylor Brandstetter1a018dc2016-03-08 12:37:39 -08001904 if (source) {
1905 // Return an error if trying to set a valid source with an invalid ssrc.
Mirko Bonadei675513b2017-11-09 11:09:25 +01001906 RTC_LOG(LS_ERROR) << "SetLocalSource failed with ssrc " << ssrc;
wu@webrtc.org9dba5252013-08-05 20:36:57 +00001907 return false;
1908 }
1909
1910 // The channel likely has gone away, do nothing.
henrike@webrtc.org1e09a712013-07-26 19:17:59 +00001911 return true;
henrike@webrtc.org1e09a712013-07-26 19:17:59 +00001912 }
1913
Taylor Brandstetter1a018dc2016-03-08 12:37:39 -08001914 if (source) {
1915 it->second->SetSource(source);
solenberg1ac56142015-10-13 03:58:19 -07001916 } else {
Taylor Brandstetter1a018dc2016-03-08 12:37:39 -08001917 it->second->ClearSource();
solenberg1ac56142015-10-13 03:58:19 -07001918 }
henrike@webrtc.org1e09a712013-07-26 19:17:59 +00001919
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001920 return true;
1921}
1922
solenberg4bac9c52015-10-09 02:32:53 -07001923bool WebRtcVoiceMediaChannel::SetOutputVolume(uint32_t ssrc, double volume) {
solenberg566ef242015-11-06 15:34:49 -08001924 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
solenberg2100c0b2017-03-01 11:29:29 -08001925 std::vector<uint32_t> ssrcs(1, ssrc);
deadbeef3bc15102017-04-20 19:25:07 -07001926 // SSRC of 0 represents the default receive stream.
solenberg1ac56142015-10-13 03:58:19 -07001927 if (ssrc == 0) {
1928 default_recv_volume_ = volume;
solenberg2100c0b2017-03-01 11:29:29 -08001929 ssrcs = unsignaled_recv_ssrcs_;
1930 }
1931 for (uint32_t ssrc : ssrcs) {
1932 const auto it = recv_streams_.find(ssrc);
1933 if (it == recv_streams_.end()) {
Mirko Bonadei675513b2017-11-09 11:09:25 +01001934 RTC_LOG(LS_WARNING) << "SetOutputVolume: no recv stream " << ssrc;
solenberg2100c0b2017-03-01 11:29:29 -08001935 return false;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001936 }
solenberg2100c0b2017-03-01 11:29:29 -08001937 it->second->SetOutputVolume(volume);
Mirko Bonadei675513b2017-11-09 11:09:25 +01001938 RTC_LOG(LS_INFO) << "SetOutputVolume() to " << volume
1939 << " for recv stream with ssrc " << ssrc;
solenberg1ac56142015-10-13 03:58:19 -07001940 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001941 return true;
1942}
1943
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001944bool WebRtcVoiceMediaChannel::CanInsertDtmf() {
Harald Alvestranda1f66612018-02-21 11:24:23 +01001945 return dtmf_payload_type_.has_value() && send_;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001946}
1947
solenberg1d63dd02015-12-02 12:35:09 -08001948bool WebRtcVoiceMediaChannel::InsertDtmf(uint32_t ssrc, int event,
1949 int duration) {
solenberg566ef242015-11-06 15:34:49 -08001950 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
Mirko Bonadei675513b2017-11-09 11:09:25 +01001951 RTC_LOG(LS_INFO) << "WebRtcVoiceMediaChannel::InsertDtmf";
Harald Alvestranda1f66612018-02-21 11:24:23 +01001952 if (!CanInsertDtmf()) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001953 return false;
1954 }
1955
Fredrik Solenbergb5727682015-12-04 15:22:19 +01001956 // Figure out which WebRtcAudioSendStream to send the event on.
1957 auto it = ssrc != 0 ? send_streams_.find(ssrc) : send_streams_.begin();
1958 if (it == send_streams_.end()) {
Mirko Bonadei675513b2017-11-09 11:09:25 +01001959 RTC_LOG(LS_WARNING) << "The specified ssrc " << ssrc << " is not in use.";
solenberg1d63dd02015-12-02 12:35:09 -08001960 return false;
1961 }
Fredrik Solenbergb5727682015-12-04 15:22:19 +01001962 if (event < kMinTelephoneEventCode ||
1963 event > kMaxTelephoneEventCode) {
Mirko Bonadei675513b2017-11-09 11:09:25 +01001964 RTC_LOG(LS_WARNING) << "DTMF event code " << event << " out of range.";
solenberg1d63dd02015-12-02 12:35:09 -08001965 return false;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001966 }
solenbergffbbcac2016-11-17 05:25:37 -08001967 RTC_DCHECK_NE(-1, dtmf_payload_freq_);
1968 return it->second->SendTelephoneEvent(*dtmf_payload_type_, dtmf_payload_freq_,
1969 event, duration);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001970}
1971
wu@webrtc.orga9890802013-12-13 00:21:03 +00001972void WebRtcVoiceMediaChannel::OnPacketReceived(
jbaucheec21bd2016-03-20 06:15:43 -07001973 rtc::CopyOnWriteBuffer* packet, const rtc::PacketTime& packet_time) {
solenberg566ef242015-11-06 15:34:49 -08001974 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
Fredrik Solenberg4b60c732015-05-07 14:07:48 +02001975
mflodman3d7db262016-04-29 00:57:13 -07001976 const webrtc::PacketTime webrtc_packet_time(packet_time.timestamp,
1977 packet_time.not_before);
1978 webrtc::PacketReceiver::DeliveryStatus delivery_result =
Danil Chapovalov292a73e2017-12-07 17:00:40 +01001979 call_->Receiver()->DeliverPacket(webrtc::MediaType::AUDIO, *packet,
mflodman3d7db262016-04-29 00:57:13 -07001980 webrtc_packet_time);
mflodman3d7db262016-04-29 00:57:13 -07001981 if (delivery_result != webrtc::PacketReceiver::DELIVERY_UNKNOWN_SSRC) {
1982 return;
1983 }
1984
solenberg2100c0b2017-03-01 11:29:29 -08001985 // Create an unsignaled receive stream for this previously not received ssrc.
1986 // If there already is N unsignaled receive streams, delete the oldest.
mflodman3d7db262016-04-29 00:57:13 -07001987 // See: https://bugs.chromium.org/p/webrtc/issues/detail?id=5208
solenberg1ac56142015-10-13 03:58:19 -07001988 uint32_t ssrc = 0;
jbaucheec21bd2016-03-20 06:15:43 -07001989 if (!GetRtpSsrc(packet->cdata(), packet->size(), &ssrc)) {
solenberg1ac56142015-10-13 03:58:19 -07001990 return;
1991 }
solenberg2100c0b2017-03-01 11:29:29 -08001992 RTC_DCHECK(std::find(unsignaled_recv_ssrcs_.begin(),
1993 unsignaled_recv_ssrcs_.end(), ssrc) == unsignaled_recv_ssrcs_.end());
solenberg1ac56142015-10-13 03:58:19 -07001994
solenberg2100c0b2017-03-01 11:29:29 -08001995 // Add new stream.
mflodman3d7db262016-04-29 00:57:13 -07001996 StreamParams sp;
1997 sp.ssrcs.push_back(ssrc);
Mirko Bonadei675513b2017-11-09 11:09:25 +01001998 RTC_LOG(LS_INFO) << "Creating unsignaled receive stream for SSRC=" << ssrc;
mflodman3d7db262016-04-29 00:57:13 -07001999 if (!AddRecvStream(sp)) {
Mirko Bonadei675513b2017-11-09 11:09:25 +01002000 RTC_LOG(LS_WARNING) << "Could not create unsignaled receive stream.";
mflodman3d7db262016-04-29 00:57:13 -07002001 return;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002002 }
solenberg2100c0b2017-03-01 11:29:29 -08002003 unsignaled_recv_ssrcs_.push_back(ssrc);
2004 RTC_HISTOGRAM_COUNTS_LINEAR(
2005 "WebRTC.Audio.NumOfUnsignaledStreams", unsignaled_recv_ssrcs_.size(), 1,
2006 100, 101);
solenbergf748ca42017-02-06 13:03:19 -08002007
solenberg2100c0b2017-03-01 11:29:29 -08002008 // Remove oldest unsignaled stream, if we have too many.
2009 if (unsignaled_recv_ssrcs_.size() > kMaxUnsignaledRecvStreams) {
2010 uint32_t remove_ssrc = unsignaled_recv_ssrcs_.front();
Mirko Bonadei675513b2017-11-09 11:09:25 +01002011 RTC_LOG(LS_INFO) << "Removing unsignaled receive stream with SSRC="
2012 << remove_ssrc;
solenberg2100c0b2017-03-01 11:29:29 -08002013 RemoveRecvStream(remove_ssrc);
2014 }
2015 RTC_DCHECK_GE(kMaxUnsignaledRecvStreams, unsignaled_recv_ssrcs_.size());
2016
2017 SetOutputVolume(ssrc, default_recv_volume_);
2018
2019 // The default sink can only be attached to one stream at a time, so we hook
2020 // it up to the *latest* unsignaled stream we've seen, in order to support the
2021 // case where the SSRC of one unsignaled stream changes.
mflodman3d7db262016-04-29 00:57:13 -07002022 if (default_sink_) {
solenberg2100c0b2017-03-01 11:29:29 -08002023 for (uint32_t drop_ssrc : unsignaled_recv_ssrcs_) {
2024 auto it = recv_streams_.find(drop_ssrc);
2025 it->second->SetRawAudioSink(nullptr);
2026 }
mflodman3d7db262016-04-29 00:57:13 -07002027 std::unique_ptr<webrtc::AudioSinkInterface> proxy_sink(
2028 new ProxySink(default_sink_.get()));
solenberg2100c0b2017-03-01 11:29:29 -08002029 SetRawAudioSink(ssrc, std::move(proxy_sink));
mflodman3d7db262016-04-29 00:57:13 -07002030 }
solenberg2100c0b2017-03-01 11:29:29 -08002031
Danil Chapovalov292a73e2017-12-07 17:00:40 +01002032 delivery_result = call_->Receiver()->DeliverPacket(
2033 webrtc::MediaType::AUDIO, *packet, webrtc_packet_time);
mflodman3d7db262016-04-29 00:57:13 -07002034 RTC_DCHECK_NE(webrtc::PacketReceiver::DELIVERY_UNKNOWN_SSRC, delivery_result);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002035}
2036
wu@webrtc.orga9890802013-12-13 00:21:03 +00002037void WebRtcVoiceMediaChannel::OnRtcpReceived(
jbaucheec21bd2016-03-20 06:15:43 -07002038 rtc::CopyOnWriteBuffer* packet, const rtc::PacketTime& packet_time) {
solenberg566ef242015-11-06 15:34:49 -08002039 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
Fredrik Solenberg4b60c732015-05-07 14:07:48 +02002040
Fredrik Solenberg709ed672015-09-15 12:26:33 +02002041 // Forward packet to Call as well.
2042 const webrtc::PacketTime webrtc_packet_time(packet_time.timestamp,
2043 packet_time.not_before);
Danil Chapovalov292a73e2017-12-07 17:00:40 +01002044 call_->Receiver()->DeliverPacket(webrtc::MediaType::AUDIO, *packet,
2045 webrtc_packet_time);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002046}
2047
Honghai Zhangcc411c02016-03-29 17:27:21 -07002048void WebRtcVoiceMediaChannel::OnNetworkRouteChanged(
2049 const std::string& transport_name,
Honghai Zhang0e533ef2016-04-19 15:41:36 -07002050 const rtc::NetworkRoute& network_route) {
Zhi Huang5f5918f2017-11-12 17:26:23 -08002051 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
Sebastian Jansson8f83b422018-02-21 13:07:13 +01002052 call_->GetTransportControllerSend()->OnNetworkRouteChanged(transport_name,
2053 network_route);
Zhi Huang5f5918f2017-11-12 17:26:23 -08002054 call_->OnTransportOverheadChanged(webrtc::MediaType::AUDIO,
2055 network_route.packet_overhead);
Honghai Zhangcc411c02016-03-29 17:27:21 -07002056}
2057
Peter Boström0c4e06b2015-10-07 12:23:21 +02002058bool WebRtcVoiceMediaChannel::MuteStream(uint32_t ssrc, bool muted) {
solenberg566ef242015-11-06 15:34:49 -08002059 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
solenberg94218532016-06-16 10:53:22 -07002060 const auto it = send_streams_.find(ssrc);
2061 if (it == send_streams_.end()) {
Mirko Bonadei675513b2017-11-09 11:09:25 +01002062 RTC_LOG(LS_WARNING) << "The specified ssrc " << ssrc << " is not in use.";
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002063 return false;
2064 }
solenberg94218532016-06-16 10:53:22 -07002065 it->second->SetMuted(muted);
2066
2067 // TODO(solenberg):
buildbot@webrtc.org6b21b712014-07-31 15:08:53 +00002068 // We set the AGC to mute state only when all the channels are muted.
2069 // This implementation is not ideal, instead we should signal the AGC when
2070 // the mic channel is muted/unmuted. We can't do it today because there
2071 // is no good way to know which stream is mapping to the mic channel.
2072 bool all_muted = muted;
solenberg94218532016-06-16 10:53:22 -07002073 for (const auto& kv : send_streams_) {
2074 all_muted = all_muted && kv.second->muted();
buildbot@webrtc.org6b21b712014-07-31 15:08:53 +00002075 }
solenberg059fb442016-10-26 05:12:24 -07002076 engine()->apm()->set_output_will_be_muted(all_muted);
buildbot@webrtc.org6b21b712014-07-31 15:08:53 +00002077
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002078 return true;
2079}
2080
deadbeef80346142016-04-27 14:17:10 -07002081bool WebRtcVoiceMediaChannel::SetMaxSendBitrate(int bps) {
Mirko Bonadei675513b2017-11-09 11:09:25 +01002082 RTC_LOG(LS_INFO) << "WebRtcVoiceMediaChannel::SetMaxSendBitrate.";
deadbeef80346142016-04-27 14:17:10 -07002083 max_send_bitrate_bps_ = bps;
minyue7a973442016-10-20 03:27:12 -07002084 bool success = true;
skvlade0d46372016-04-07 22:59:22 -07002085 for (const auto& kv : send_streams_) {
minyue7a973442016-10-20 03:27:12 -07002086 if (!kv.second->SetMaxSendBitrate(max_send_bitrate_bps_)) {
2087 success = false;
skvlade0d46372016-04-07 22:59:22 -07002088 }
2089 }
minyue7a973442016-10-20 03:27:12 -07002090 return success;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002091}
2092
skvlad7a43d252016-03-22 15:32:27 -07002093void WebRtcVoiceMediaChannel::OnReadyToSend(bool ready) {
2094 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
Mirko Bonadei675513b2017-11-09 11:09:25 +01002095 RTC_LOG(LS_VERBOSE) << "OnReadyToSend: " << (ready ? "Ready." : "Not ready.");
skvlad7a43d252016-03-22 15:32:27 -07002096 call_->SignalChannelNetworkState(
2097 webrtc::MediaType::AUDIO,
2098 ready ? webrtc::kNetworkUp : webrtc::kNetworkDown);
2099}
2100
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002101bool WebRtcVoiceMediaChannel::GetStats(VoiceMediaInfo* info) {
Peter Boströmca8b4042016-03-08 14:24:13 -08002102 TRACE_EVENT0("webrtc", "WebRtcVoiceMediaChannel::GetStats");
solenberg566ef242015-11-06 15:34:49 -08002103 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
solenberg85a04962015-10-27 03:35:21 -07002104 RTC_DCHECK(info);
solenbergd97ec302015-10-07 01:40:33 -07002105
solenberg85a04962015-10-27 03:35:21 -07002106 // Get SSRC and stats for each sender.
hbos1acfbd22016-11-17 23:43:29 -08002107 RTC_DCHECK_EQ(info->senders.size(), 0U);
solenberg85a04962015-10-27 03:35:21 -07002108 for (const auto& stream : send_streams_) {
Ivo Creusen56d46092017-11-24 17:29:59 +01002109 webrtc::AudioSendStream::Stats stats =
2110 stream.second->GetStats(recv_streams_.size() > 0);
wu@webrtc.org9dba5252013-08-05 20:36:57 +00002111 VoiceSenderInfo sinfo;
solenberg85a04962015-10-27 03:35:21 -07002112 sinfo.add_ssrc(stats.local_ssrc);
2113 sinfo.bytes_sent = stats.bytes_sent;
2114 sinfo.packets_sent = stats.packets_sent;
2115 sinfo.packets_lost = stats.packets_lost;
2116 sinfo.fraction_lost = stats.fraction_lost;
2117 sinfo.codec_name = stats.codec_name;
hbos1acfbd22016-11-17 23:43:29 -08002118 sinfo.codec_payload_type = stats.codec_payload_type;
solenberg85a04962015-10-27 03:35:21 -07002119 sinfo.ext_seqnum = stats.ext_seqnum;
2120 sinfo.jitter_ms = stats.jitter_ms;
2121 sinfo.rtt_ms = stats.rtt_ms;
2122 sinfo.audio_level = stats.audio_level;
zsteine76bd3a2017-07-14 12:17:49 -07002123 sinfo.total_input_energy = stats.total_input_energy;
2124 sinfo.total_input_duration = stats.total_input_duration;
Taylor Brandstetter1a018dc2016-03-08 12:37:39 -08002125 sinfo.typing_noise_detected = (send_ ? stats.typing_noise_detected : false);
ivoce1198e02017-09-08 08:13:19 -07002126 sinfo.ana_statistics = stats.ana_statistics;
Ivo Creusen56d46092017-11-24 17:29:59 +01002127 sinfo.apm_statistics = stats.apm_statistics;
wu@webrtc.org9dba5252013-08-05 20:36:57 +00002128 info->senders.push_back(sinfo);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002129 }
2130
solenberg85a04962015-10-27 03:35:21 -07002131 // Get SSRC and stats for each receiver.
hbos1acfbd22016-11-17 23:43:29 -08002132 RTC_DCHECK_EQ(info->receivers.size(), 0U);
solenberg7add0582015-11-20 09:59:34 -08002133 for (const auto& stream : recv_streams_) {
deadbeef4e2deab2017-09-20 13:56:21 -07002134 uint32_t ssrc = stream.first;
2135 // When SSRCs are unsignaled, there's only one audio MediaStreamTrack, but
2136 // multiple RTP streams can be received over time (if the SSRC changes for
2137 // whatever reason). We only want the RTCMediaStreamTrackStats to represent
2138 // the stats for the most recent stream (the one whose audio is actually
2139 // routed to the MediaStreamTrack), so here we ignore any unsignaled SSRCs
2140 // except for the most recent one (last in the vector). This is somewhat of
2141 // a hack, and means you don't get *any* stats for these inactive streams,
2142 // but it's slightly better than the previous behavior, which was "highest
2143 // SSRC wins".
2144 // See: https://bugs.chromium.org/p/webrtc/issues/detail?id=8158
2145 if (!unsignaled_recv_ssrcs_.empty()) {
2146 auto end_it = --unsignaled_recv_ssrcs_.end();
2147 if (std::find(unsignaled_recv_ssrcs_.begin(), end_it, ssrc) != end_it) {
2148 continue;
2149 }
2150 }
Fredrik Solenberg4f4ec0a2015-10-22 10:49:27 +02002151 webrtc::AudioReceiveStream::Stats stats = stream.second->GetStats();
2152 VoiceReceiverInfo rinfo;
2153 rinfo.add_ssrc(stats.remote_ssrc);
2154 rinfo.bytes_rcvd = stats.bytes_rcvd;
2155 rinfo.packets_rcvd = stats.packets_rcvd;
2156 rinfo.packets_lost = stats.packets_lost;
2157 rinfo.fraction_lost = stats.fraction_lost;
2158 rinfo.codec_name = stats.codec_name;
hbos1acfbd22016-11-17 23:43:29 -08002159 rinfo.codec_payload_type = stats.codec_payload_type;
Fredrik Solenberg4f4ec0a2015-10-22 10:49:27 +02002160 rinfo.ext_seqnum = stats.ext_seqnum;
2161 rinfo.jitter_ms = stats.jitter_ms;
2162 rinfo.jitter_buffer_ms = stats.jitter_buffer_ms;
2163 rinfo.jitter_buffer_preferred_ms = stats.jitter_buffer_preferred_ms;
2164 rinfo.delay_estimate_ms = stats.delay_estimate_ms;
2165 rinfo.audio_level = stats.audio_level;
zsteine76bd3a2017-07-14 12:17:49 -07002166 rinfo.total_output_energy = stats.total_output_energy;
Steve Anton2dbc69f2017-08-24 17:15:13 -07002167 rinfo.total_samples_received = stats.total_samples_received;
zsteine76bd3a2017-07-14 12:17:49 -07002168 rinfo.total_output_duration = stats.total_output_duration;
Steve Anton2dbc69f2017-08-24 17:15:13 -07002169 rinfo.concealed_samples = stats.concealed_samples;
Gustaf Ullberg9a2e9062017-09-18 09:28:20 +02002170 rinfo.concealment_events = stats.concealment_events;
Gustaf Ullbergb0a02072017-10-02 12:00:34 +02002171 rinfo.jitter_buffer_delay_seconds = stats.jitter_buffer_delay_seconds;
Fredrik Solenberg4f4ec0a2015-10-22 10:49:27 +02002172 rinfo.expand_rate = stats.expand_rate;
2173 rinfo.speech_expand_rate = stats.speech_expand_rate;
2174 rinfo.secondary_decoded_rate = stats.secondary_decoded_rate;
minyue-webrtc0e320ec2017-08-28 13:51:27 +02002175 rinfo.secondary_discarded_rate = stats.secondary_discarded_rate;
Fredrik Solenberg4f4ec0a2015-10-22 10:49:27 +02002176 rinfo.accelerate_rate = stats.accelerate_rate;
2177 rinfo.preemptive_expand_rate = stats.preemptive_expand_rate;
2178 rinfo.decoding_calls_to_silence_generator =
2179 stats.decoding_calls_to_silence_generator;
2180 rinfo.decoding_calls_to_neteq = stats.decoding_calls_to_neteq;
2181 rinfo.decoding_normal = stats.decoding_normal;
2182 rinfo.decoding_plc = stats.decoding_plc;
2183 rinfo.decoding_cng = stats.decoding_cng;
2184 rinfo.decoding_plc_cng = stats.decoding_plc_cng;
henrik.lundin63489782016-09-20 01:47:12 -07002185 rinfo.decoding_muted_output = stats.decoding_muted_output;
Fredrik Solenberg4f4ec0a2015-10-22 10:49:27 +02002186 rinfo.capture_start_ntp_time_ms = stats.capture_start_ntp_time_ms;
2187 info->receivers.push_back(rinfo);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002188 }
2189
hbos1acfbd22016-11-17 23:43:29 -08002190 // Get codec info
2191 for (const AudioCodec& codec : send_codecs_) {
2192 webrtc::RtpCodecParameters codec_params = codec.ToCodecParameters();
2193 info->send_codecs.insert(
2194 std::make_pair(codec_params.payload_type, std::move(codec_params)));
2195 }
2196 for (const AudioCodec& codec : recv_codecs_) {
2197 webrtc::RtpCodecParameters codec_params = codec.ToCodecParameters();
2198 info->receive_codecs.insert(
2199 std::make_pair(codec_params.payload_type, std::move(codec_params)));
2200 }
2201
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002202 return true;
2203}
2204
Tommif888bb52015-12-12 01:37:01 +01002205void WebRtcVoiceMediaChannel::SetRawAudioSink(
2206 uint32_t ssrc,
kwiberg686a8ef2016-02-26 03:00:35 -08002207 std::unique_ptr<webrtc::AudioSinkInterface> sink) {
Tommif888bb52015-12-12 01:37:01 +01002208 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
Mirko Bonadei675513b2017-11-09 11:09:25 +01002209 RTC_LOG(LS_VERBOSE) << "WebRtcVoiceMediaChannel::SetRawAudioSink: ssrc:"
2210 << ssrc << " " << (sink ? "(ptr)" : "NULL");
deadbeef884f5852016-01-15 09:20:04 -08002211 if (ssrc == 0) {
solenberg2100c0b2017-03-01 11:29:29 -08002212 if (!unsignaled_recv_ssrcs_.empty()) {
kwiberg686a8ef2016-02-26 03:00:35 -08002213 std::unique_ptr<webrtc::AudioSinkInterface> proxy_sink(
deadbeef884f5852016-01-15 09:20:04 -08002214 sink ? new ProxySink(sink.get()) : nullptr);
solenberg2100c0b2017-03-01 11:29:29 -08002215 SetRawAudioSink(unsignaled_recv_ssrcs_.back(), std::move(proxy_sink));
deadbeef884f5852016-01-15 09:20:04 -08002216 }
2217 default_sink_ = std::move(sink);
2218 return;
2219 }
Tommif888bb52015-12-12 01:37:01 +01002220 const auto it = recv_streams_.find(ssrc);
2221 if (it == recv_streams_.end()) {
Mirko Bonadei675513b2017-11-09 11:09:25 +01002222 RTC_LOG(LS_WARNING) << "SetRawAudioSink: no recv stream " << ssrc;
Tommif888bb52015-12-12 01:37:01 +01002223 return;
2224 }
deadbeef2d110be2016-01-13 12:00:26 -08002225 it->second->SetRawAudioSink(std::move(sink));
Tommif888bb52015-12-12 01:37:01 +01002226}
2227
hbos8d609f62017-04-10 07:39:05 -07002228std::vector<webrtc::RtpSource> WebRtcVoiceMediaChannel::GetSources(
2229 uint32_t ssrc) const {
2230 auto it = recv_streams_.find(ssrc);
Zhi Huangfa266ef2017-12-13 10:27:46 -08002231 if (it == recv_streams_.end()) {
2232 RTC_LOG(LS_ERROR) << "Attempting to get contributing sources for SSRC:"
2233 << ssrc << " which doesn't exist.";
2234 return std::vector<webrtc::RtpSource>();
2235 }
hbos8d609f62017-04-10 07:39:05 -07002236 return it->second->GetSources();
2237}
2238
solenberg2100c0b2017-03-01 11:29:29 -08002239bool WebRtcVoiceMediaChannel::
2240 MaybeDeregisterUnsignaledRecvStream(uint32_t ssrc) {
2241 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
2242 auto it = std::find(unsignaled_recv_ssrcs_.begin(),
2243 unsignaled_recv_ssrcs_.end(),
2244 ssrc);
2245 if (it != unsignaled_recv_ssrcs_.end()) {
2246 unsignaled_recv_ssrcs_.erase(it);
2247 return true;
2248 }
2249 return false;
2250}
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002251} // namespace cricket
2252
2253#endif // HAVE_WEBRTC_VOICE