blob: cab9f82f00edaba6a56c0257921028db4e6fdb33 [file] [log] [blame]
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001/*
kjellander1afca732016-02-07 20:46:45 -08002 * Copyright (c) 2004 The WebRTC project authors. All Rights Reserved.
henrike@webrtc.org28e20752013-07-10 00:45:36 +00003 *
kjellander1afca732016-02-07 20:46:45 -08004 * Use of this source code is governed by a BSD-style license
5 * that can be found in the LICENSE file in the root of the source
6 * tree. An additional intellectual property rights grant can be found
7 * in the file PATENTS. All contributing project authors may
8 * be found in the AUTHORS file in the root of the source tree.
henrike@webrtc.org28e20752013-07-10 00:45:36 +00009 */
10
henrike@webrtc.org28e20752013-07-10 00:45:36 +000011#ifdef HAVE_WEBRTC_VOICE
12
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020013#include "media/engine/webrtcvoiceengine.h"
henrike@webrtc.org28e20752013-07-10 00:45:36 +000014
15#include <algorithm>
16#include <cstdio>
ossuc54071d2016-08-17 02:45:41 -070017#include <functional>
henrike@webrtc.org28e20752013-07-10 00:45:36 +000018#include <string>
Steve Antone78bcb92017-10-31 09:53:08 -070019#include <utility>
henrike@webrtc.org28e20752013-07-10 00:45:36 +000020#include <vector>
21
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020022#include "api/call/audio_sink.h"
23#include "media/base/audiosource.h"
24#include "media/base/mediaconstants.h"
25#include "media/base/streamparams.h"
26#include "media/engine/adm_helpers.h"
27#include "media/engine/apm_helpers.h"
28#include "media/engine/payload_type_mapper.h"
29#include "media/engine/webrtcmediaengine.h"
30#include "media/engine/webrtcvoe.h"
31#include "modules/audio_mixer/audio_mixer_impl.h"
32#include "modules/audio_processing/aec_dump/aec_dump_factory.h"
33#include "modules/audio_processing/include/audio_processing.h"
34#include "rtc_base/arraysize.h"
35#include "rtc_base/base64.h"
36#include "rtc_base/byteorder.h"
37#include "rtc_base/constructormagic.h"
38#include "rtc_base/helpers.h"
39#include "rtc_base/logging.h"
40#include "rtc_base/race_checker.h"
41#include "rtc_base/stringencode.h"
42#include "rtc_base/stringutils.h"
43#include "rtc_base/trace_event.h"
44#include "system_wrappers/include/field_trial.h"
45#include "system_wrappers/include/metrics.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020046#include "voice_engine/transmit_mixer.h"
henrike@webrtc.org28e20752013-07-10 00:45:36 +000047
henrike@webrtc.org28e20752013-07-10 00:45:36 +000048namespace cricket {
solenbergd97ec302015-10-07 01:40:33 -070049namespace {
henrike@webrtc.org28e20752013-07-10 00:45:36 +000050
solenberg418b7d32017-06-13 00:38:27 -070051constexpr size_t kMaxUnsignaledRecvStreams = 4;
solenberg2100c0b2017-03-01 11:29:29 -080052
solenberg971cab02016-06-14 10:02:41 -070053constexpr int kNackRtpHistoryMs = 5000;
minyue@webrtc.org2dc6f312014-10-31 05:33:10 +000054
peah1bcfce52016-08-26 07:16:04 -070055// Check to verify that the define for the intelligibility enhancer is properly
56// set.
57#if !defined(WEBRTC_INTELLIGIBILITY_ENHANCER) || \
58 (WEBRTC_INTELLIGIBILITY_ENHANCER != 0 && \
59 WEBRTC_INTELLIGIBILITY_ENHANCER != 1)
60#error "Set WEBRTC_INTELLIGIBILITY_ENHANCER to either 0 or 1"
61#endif
62
ossu20a4b3f2017-04-27 02:08:52 -070063// For SendSideBwe, Opus bitrate should be in the range between 6000 and 32000.
minyue10cbb462016-11-07 09:29:22 -080064const int kOpusMinBitrateBps = 6000;
ossu20a4b3f2017-04-27 02:08:52 -070065const int kOpusBitrateFbBps = 32000;
deadbeef80346142016-04-27 14:17:10 -070066
wu@webrtc.orgde305012013-10-31 15:40:38 +000067// Default audio dscp value.
68// See http://tools.ietf.org/html/rfc2474 for details.
69// See also http://tools.ietf.org/html/draft-jennings-rtcweb-qos-00
solenbergd97ec302015-10-07 01:40:33 -070070const rtc::DiffServCodePoint kAudioDscpValue = rtc::DSCP_EF;
henrike@webrtc.org1e09a712013-07-26 19:17:59 +000071
Fredrik Solenbergb5727682015-12-04 15:22:19 +010072// Constants from voice_engine_defines.h.
73const 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 }
158 return rtc::Optional<std::string>();
159}
160
gyzhou95aa9642016-12-13 14:06:26 -0800161webrtc::AudioState::Config MakeAudioStateConfig(
162 VoEWrapper* voe_wrapper,
peaha9cc40b2017-06-29 08:32:09 -0700163 rtc::scoped_refptr<webrtc::AudioMixer> audio_mixer,
164 rtc::scoped_refptr<webrtc::AudioProcessing> audio_processing) {
solenberg566ef242015-11-06 15:34:49 -0800165 webrtc::AudioState::Config config;
166 config.voice_engine = voe_wrapper->engine();
gyzhou95aa9642016-12-13 14:06:26 -0800167 if (audio_mixer) {
168 config.audio_mixer = audio_mixer;
169 } else {
170 config.audio_mixer = webrtc::AudioMixerImpl::Create();
171 }
peaha9cc40b2017-06-29 08:32:09 -0700172 config.audio_processing = audio_processing;
solenberg566ef242015-11-06 15:34:49 -0800173 return config;
174}
175
deadbeefe702b302017-02-04 12:09:01 -0800176// |max_send_bitrate_bps| is the bitrate from "b=" in SDP.
177// |rtp_max_bitrate_bps| is the bitrate from RtpSender::SetParameters.
minyue7a973442016-10-20 03:27:12 -0700178rtc::Optional<int> ComputeSendBitrate(int max_send_bitrate_bps,
deadbeefe702b302017-02-04 12:09:01 -0800179 rtc::Optional<int> rtp_max_bitrate_bps,
ossu20a4b3f2017-04-27 02:08:52 -0700180 const webrtc::AudioCodecSpec& spec) {
deadbeefe702b302017-02-04 12:09:01 -0800181 // If application-configured bitrate is set, take minimum of that and SDP
182 // bitrate.
zsteina5e0df62017-06-14 11:41:48 -0700183 const int bps =
184 rtp_max_bitrate_bps
185 ? webrtc::MinPositive(max_send_bitrate_bps, *rtp_max_bitrate_bps)
186 : max_send_bitrate_bps;
minyue7a973442016-10-20 03:27:12 -0700187 if (bps <= 0) {
ossu20a4b3f2017-04-27 02:08:52 -0700188 return rtc::Optional<int>(spec.info.default_bitrate_bps);
solenberg971cab02016-06-14 10:02:41 -0700189 }
minyue7a973442016-10-20 03:27:12 -0700190
ossu20a4b3f2017-04-27 02:08:52 -0700191 if (bps < spec.info.min_bitrate_bps) {
minyue7a973442016-10-20 03:27:12 -0700192 // If codec is not multi-rate and |bps| is less than the fixed bitrate then
193 // fail. If codec is not multi-rate and |bps| exceeds or equal the fixed
194 // bitrate then ignore.
Mirko Bonadei675513b2017-11-09 11:09:25 +0100195 RTC_LOG(LS_ERROR) << "Failed to set codec " << spec.format.name
196 << " to bitrate " << bps << " bps"
197 << ", requires at least " << spec.info.min_bitrate_bps
198 << " bps.";
minyue7a973442016-10-20 03:27:12 -0700199 return rtc::Optional<int>();
solenberg971cab02016-06-14 10:02:41 -0700200 }
ossu20a4b3f2017-04-27 02:08:52 -0700201
202 if (spec.info.HasFixedBitrate()) {
203 return rtc::Optional<int>(spec.info.default_bitrate_bps);
204 } else {
205 // If codec is multi-rate then just set the bitrate.
206 return rtc::Optional<int>(std::min(bps, spec.info.max_bitrate_bps));
207 }
solenberg971cab02016-06-14 10:02:41 -0700208}
209
solenberg76377c52017-02-21 00:54:31 -0800210} // namespace
solenberg971cab02016-06-14 10:02:41 -0700211
ossu29b1a8d2016-06-13 07:34:51 -0700212WebRtcVoiceEngine::WebRtcVoiceEngine(
213 webrtc::AudioDeviceModule* adm,
ossueb1fde42017-05-02 06:46:30 -0700214 const rtc::scoped_refptr<webrtc::AudioEncoderFactory>& encoder_factory,
gyzhou95aa9642016-12-13 14:06:26 -0800215 const rtc::scoped_refptr<webrtc::AudioDecoderFactory>& decoder_factory,
peaha9cc40b2017-06-29 08:32:09 -0700216 rtc::scoped_refptr<webrtc::AudioMixer> audio_mixer,
217 rtc::scoped_refptr<webrtc::AudioProcessing> audio_processing)
ossueb1fde42017-05-02 06:46:30 -0700218 : WebRtcVoiceEngine(adm,
219 encoder_factory,
220 decoder_factory,
221 audio_mixer,
peaha9cc40b2017-06-29 08:32:09 -0700222 audio_processing,
deadbeefeb02c032017-06-15 08:29:25 -0700223 nullptr) {}
solenberg26c8c912015-11-27 04:00:25 -0800224
ossu29b1a8d2016-06-13 07:34:51 -0700225WebRtcVoiceEngine::WebRtcVoiceEngine(
226 webrtc::AudioDeviceModule* adm,
ossueb1fde42017-05-02 06:46:30 -0700227 const rtc::scoped_refptr<webrtc::AudioEncoderFactory>& encoder_factory,
ossu29b1a8d2016-06-13 07:34:51 -0700228 const rtc::scoped_refptr<webrtc::AudioDecoderFactory>& decoder_factory,
gyzhou95aa9642016-12-13 14:06:26 -0800229 rtc::scoped_refptr<webrtc::AudioMixer> audio_mixer,
peaha9cc40b2017-06-29 08:32:09 -0700230 rtc::scoped_refptr<webrtc::AudioProcessing> audio_processing,
ossu29b1a8d2016-06-13 07:34:51 -0700231 VoEWrapper* voe_wrapper)
deadbeefeb02c032017-06-15 08:29:25 -0700232 : adm_(adm),
ossueb1fde42017-05-02 06:46:30 -0700233 encoder_factory_(encoder_factory),
ossu20a4b3f2017-04-27 02:08:52 -0700234 decoder_factory_(decoder_factory),
deadbeefeb02c032017-06-15 08:29:25 -0700235 audio_mixer_(audio_mixer),
peaha9cc40b2017-06-29 08:32:09 -0700236 apm_(audio_processing),
ossu20a4b3f2017-04-27 02:08:52 -0700237 voe_wrapper_(voe_wrapper) {
deadbeefeb02c032017-06-15 08:29:25 -0700238 // This may be called from any thread, so detach thread checkers.
239 worker_thread_checker_.DetachFromThread();
solenberg26c8c912015-11-27 04:00:25 -0800240 signal_thread_checker_.DetachFromThread();
Mirko Bonadei675513b2017-11-09 11:09:25 +0100241 RTC_LOG(LS_INFO) << "WebRtcVoiceEngine::WebRtcVoiceEngine";
deadbeefeb02c032017-06-15 08:29:25 -0700242 RTC_DCHECK(decoder_factory);
243 RTC_DCHECK(encoder_factory);
peaha9cc40b2017-06-29 08:32:09 -0700244 RTC_DCHECK(audio_processing);
deadbeefeb02c032017-06-15 08:29:25 -0700245 // The rest of our initialization will happen in Init.
246}
247
248WebRtcVoiceEngine::~WebRtcVoiceEngine() {
249 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
Mirko Bonadei675513b2017-11-09 11:09:25 +0100250 RTC_LOG(LS_INFO) << "WebRtcVoiceEngine::~WebRtcVoiceEngine";
deadbeefeb02c032017-06-15 08:29:25 -0700251 if (initialized_) {
252 StopAecDump();
253 voe_wrapper_->base()->Terminate();
deadbeefeb02c032017-06-15 08:29:25 -0700254 }
255}
256
257void WebRtcVoiceEngine::Init() {
258 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
Mirko Bonadei675513b2017-11-09 11:09:25 +0100259 RTC_LOG(LS_INFO) << "WebRtcVoiceEngine::Init";
deadbeefeb02c032017-06-15 08:29:25 -0700260
261 // TaskQueue expects to be created/destroyed on the same thread.
262 low_priority_worker_queue_.reset(
263 new rtc::TaskQueue("rtc-low-prio", rtc::TaskQueue::Priority::LOW));
264
265 // VoEWrapper needs to be created on the worker thread. It's expected to be
266 // null here unless it's being injected for testing.
267 if (!voe_wrapper_) {
268 voe_wrapper_.reset(new VoEWrapper());
269 }
solenberg26c8c912015-11-27 04:00:25 -0800270
ossueb1fde42017-05-02 06:46:30 -0700271 // Load our audio codec lists.
Mirko Bonadei675513b2017-11-09 11:09:25 +0100272 RTC_LOG(LS_INFO) << "Supported send codecs in order of preference:";
ossu20a4b3f2017-04-27 02:08:52 -0700273 send_codecs_ = CollectCodecs(encoder_factory_->GetSupportedEncoders());
ossuc54071d2016-08-17 02:45:41 -0700274 for (const AudioCodec& codec : send_codecs_) {
Mirko Bonadei675513b2017-11-09 11:09:25 +0100275 RTC_LOG(LS_INFO) << ToString(codec);
ossuc54071d2016-08-17 02:45:41 -0700276 }
277
Mirko Bonadei675513b2017-11-09 11:09:25 +0100278 RTC_LOG(LS_INFO) << "Supported recv codecs in order of preference:";
ossu20a4b3f2017-04-27 02:08:52 -0700279 recv_codecs_ = CollectCodecs(decoder_factory_->GetSupportedDecoders());
ossuc54071d2016-08-17 02:45:41 -0700280 for (const AudioCodec& codec : recv_codecs_) {
Mirko Bonadei675513b2017-11-09 11:09:25 +0100281 RTC_LOG(LS_INFO) << ToString(codec);
buildbot@webrtc.org13d67762014-05-02 17:33:29 +0000282 }
buildbot@webrtc.org13d67762014-05-02 17:33:29 +0000283
solenberg88499ec2016-09-07 07:34:41 -0700284 channel_config_.enable_voice_pacing = true;
buildbot@webrtc.org13d67762014-05-02 17:33:29 +0000285
peaha9cc40b2017-06-29 08:32:09 -0700286 RTC_CHECK_EQ(0,
287 voe_wrapper_->base()->Init(adm_.get(), apm(), decoder_factory_));
buildbot@webrtc.org13d67762014-05-02 17:33:29 +0000288
solenbergff976312016-03-30 23:28:51 -0700289 // No ADM supplied? Get the default one from VoE.
290 if (!adm_) {
291 adm_ = voe_wrapper_->base()->audio_device_module();
292 }
293 RTC_DCHECK(adm_);
294
solenberg76377c52017-02-21 00:54:31 -0800295 transmit_mixer_ = voe_wrapper_->base()->transmit_mixer();
296 RTC_DCHECK(transmit_mixer_);
297
buildbot@webrtc.org13d67762014-05-02 17:33:29 +0000298 // Save the default AGC configuration settings. This must happen before
solenberg246b8172015-12-08 09:50:23 -0800299 // calling ApplyOptions or the default will be overwritten.
peaha9cc40b2017-06-29 08:32:09 -0700300 default_agc_config_ = webrtc::apm_helpers::GetAgcConfig(apm());
buildbot@webrtc.org13d67762014-05-02 17:33:29 +0000301
solenberg0f7d2932016-01-15 01:40:39 -0800302 // Set default engine options.
303 {
304 AudioOptions options;
305 options.echo_cancellation = rtc::Optional<bool>(true);
306 options.auto_gain_control = rtc::Optional<bool>(true);
307 options.noise_suppression = rtc::Optional<bool>(true);
308 options.highpass_filter = rtc::Optional<bool>(true);
309 options.stereo_swapping = rtc::Optional<bool>(false);
310 options.audio_jitter_buffer_max_packets = rtc::Optional<int>(50);
311 options.audio_jitter_buffer_fast_accelerate = rtc::Optional<bool>(false);
312 options.typing_detection = rtc::Optional<bool>(true);
313 options.adjust_agc_delta = rtc::Optional<int>(0);
314 options.experimental_agc = rtc::Optional<bool>(false);
315 options.extended_filter_aec = rtc::Optional<bool>(false);
316 options.delay_agnostic_aec = rtc::Optional<bool>(false);
317 options.experimental_ns = rtc::Optional<bool>(false);
Alejandro Luebsc9b0c262016-05-16 15:32:38 -0700318 options.intelligibility_enhancer = rtc::Optional<bool>(false);
peaha3333bf2016-06-30 00:02:34 -0700319 options.level_control = rtc::Optional<bool>(false);
ivocb829d9f2016-11-15 02:34:47 -0800320 options.residual_echo_detector = rtc::Optional<bool>(true);
solenbergff976312016-03-30 23:28:51 -0700321 bool error = ApplyOptions(options);
322 RTC_DCHECK(error);
buildbot@webrtc.org13d67762014-05-02 17:33:29 +0000323 }
324
solenberg9a5f032222017-03-15 06:14:12 -0700325 // Set default audio devices.
326#if !defined(WEBRTC_IOS)
327 webrtc::adm_helpers::SetRecordingDevice(adm_);
328 apm()->Initialize();
329 webrtc::adm_helpers::SetPlayoutDevice(adm_);
330#endif // !WEBRTC_IOS
buildbot@webrtc.org13d67762014-05-02 17:33:29 +0000331
deadbeefeb02c032017-06-15 08:29:25 -0700332 // May be null for VoE injected for testing.
333 if (voe()->engine()) {
peaha9cc40b2017-06-29 08:32:09 -0700334 audio_state_ = webrtc::AudioState::Create(
335 MakeAudioStateConfig(voe(), audio_mixer_, apm_));
deadbeefeb02c032017-06-15 08:29:25 -0700336 }
337
338 initialized_ = true;
buildbot@webrtc.org13d67762014-05-02 17:33:29 +0000339}
340
solenberg566ef242015-11-06 15:34:49 -0800341rtc::scoped_refptr<webrtc::AudioState>
342 WebRtcVoiceEngine::GetAudioState() const {
343 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
344 return audio_state_;
345}
346
nisse51542be2016-02-12 02:27:06 -0800347VoiceMediaChannel* WebRtcVoiceEngine::CreateChannel(
348 webrtc::Call* call,
349 const MediaConfig& config,
Jelena Marusicc28a8962015-05-29 15:05:44 +0200350 const AudioOptions& options) {
solenberg566ef242015-11-06 15:34:49 -0800351 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
nisse51542be2016-02-12 02:27:06 -0800352 return new WebRtcVoiceMediaChannel(this, config, options, call);
buildbot@webrtc.org13d67762014-05-02 17:33:29 +0000353}
354
buildbot@webrtc.org13d67762014-05-02 17:33:29 +0000355bool WebRtcVoiceEngine::ApplyOptions(const AudioOptions& options_in) {
solenberg566ef242015-11-06 15:34:49 -0800356 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
Mirko Bonadei675513b2017-11-09 11:09:25 +0100357 RTC_LOG(LS_INFO) << "WebRtcVoiceEngine::ApplyOptions: "
358 << options_in.ToString();
solenberg0f7d2932016-01-15 01:40:39 -0800359 AudioOptions options = options_in; // The options are modified below.
solenberg246b8172015-12-08 09:50:23 -0800360
peah8a8ebd92017-05-22 15:48:47 -0700361 // Set and adjust echo canceller options.
buildbot@webrtc.org13d67762014-05-02 17:33:29 +0000362 // kEcConference is AEC with high suppression.
363 webrtc::EcModes ec_mode = webrtc::kEcConference;
kwiberg102c6a62015-10-30 02:47:38 -0700364 if (options.aecm_generate_comfort_noise) {
Mirko Bonadei675513b2017-11-09 11:09:25 +0100365 RTC_LOG(LS_VERBOSE) << "Comfort noise explicitly set to "
366 << *options.aecm_generate_comfort_noise
367 << " (default is false).";
buildbot@webrtc.org13d67762014-05-02 17:33:29 +0000368 }
369
kjellanderfcfc8042016-01-14 11:01:09 -0800370#if defined(WEBRTC_IOS)
peah8a8ebd92017-05-22 15:48:47 -0700371 // On iOS, VPIO provides built-in EC.
Karl Wibergbe579832015-11-10 22:34:18 +0100372 options.echo_cancellation = rtc::Optional<bool>(false);
peah8a8ebd92017-05-22 15:48:47 -0700373 options.extended_filter_aec = rtc::Optional<bool>(false);
Mirko Bonadei675513b2017-11-09 11:09:25 +0100374 RTC_LOG(LS_INFO) << "Always disable AEC on iOS. Use built-in instead.";
Mirko Bonadeic8c71b92017-10-16 11:08:54 +0200375#elif defined(WEBRTC_ANDROID)
buildbot@webrtc.org13d67762014-05-02 17:33:29 +0000376 ec_mode = webrtc::kEcAecm;
Karl Wibergbe579832015-11-10 22:34:18 +0100377 options.extended_filter_aec = rtc::Optional<bool>(false);
buildbot@webrtc.org13d67762014-05-02 17:33:29 +0000378#endif
379
Bjorn Volckerbf395c12015-03-25 22:45:56 +0100380 // Delay Agnostic AEC automatically turns on EC if not set except on iOS
381 // where the feature is not supported.
382 bool use_delay_agnostic_aec = false;
kjellanderfcfc8042016-01-14 11:01:09 -0800383#if !defined(WEBRTC_IOS)
kwiberg102c6a62015-10-30 02:47:38 -0700384 if (options.delay_agnostic_aec) {
385 use_delay_agnostic_aec = *options.delay_agnostic_aec;
Bjorn Volckerbf395c12015-03-25 22:45:56 +0100386 if (use_delay_agnostic_aec) {
Karl Wibergbe579832015-11-10 22:34:18 +0100387 options.echo_cancellation = rtc::Optional<bool>(true);
388 options.extended_filter_aec = rtc::Optional<bool>(true);
Bjorn Volckerbf395c12015-03-25 22:45:56 +0100389 ec_mode = webrtc::kEcConference;
390 }
391 }
392#endif
393
peah8a8ebd92017-05-22 15:48:47 -0700394// Set and adjust noise suppressor options.
395#if defined(WEBRTC_IOS)
396 // On iOS, VPIO provides built-in NS.
397 options.noise_suppression = rtc::Optional<bool>(false);
398 options.typing_detection = rtc::Optional<bool>(false);
399 options.experimental_ns = rtc::Optional<bool>(false);
Mirko Bonadei675513b2017-11-09 11:09:25 +0100400 RTC_LOG(LS_INFO) << "Always disable NS on iOS. Use built-in instead.";
Mirko Bonadeic8c71b92017-10-16 11:08:54 +0200401#elif defined(WEBRTC_ANDROID)
peah8a8ebd92017-05-22 15:48:47 -0700402 options.typing_detection = rtc::Optional<bool>(false);
403 options.experimental_ns = rtc::Optional<bool>(false);
404#endif
405
406// Set and adjust gain control options.
407#if defined(WEBRTC_IOS)
408 // On iOS, VPIO provides built-in AGC.
409 options.auto_gain_control = rtc::Optional<bool>(false);
410 options.experimental_agc = rtc::Optional<bool>(false);
Mirko Bonadei675513b2017-11-09 11:09:25 +0100411 RTC_LOG(LS_INFO) << "Always disable AGC on iOS. Use built-in instead.";
Mirko Bonadeic8c71b92017-10-16 11:08:54 +0200412#elif defined(WEBRTC_ANDROID)
peah8a8ebd92017-05-22 15:48:47 -0700413 options.experimental_agc = rtc::Optional<bool>(false);
414#endif
415
416#if defined(WEBRTC_IOS) || defined(WEBRTC_ANDROID)
Mirko Bonadeic8c71b92017-10-16 11:08:54 +0200417 // Turn off the gain control if specified by the field trial.
418 // The purpose of the field trial is to reduce the amount of resampling
419 // performed inside the audio processing module on mobile platforms by
420 // whenever possible turning off the fixed AGC mode and the high-pass filter.
421 // (https://bugs.chromium.org/p/webrtc/issues/detail?id=6181).
peah8a8ebd92017-05-22 15:48:47 -0700422 if (webrtc::field_trial::IsEnabled(
423 "WebRTC-Audio-MinimizeResamplingOnMobile")) {
424 options.auto_gain_control = rtc::Optional<bool>(false);
Mirko Bonadei675513b2017-11-09 11:09:25 +0100425 RTC_LOG(LS_INFO) << "Disable AGC according to field trial.";
Steve Antone78bcb92017-10-31 09:53:08 -0700426 if (!(options.noise_suppression.value_or(false) ||
peah8a8ebd92017-05-22 15:48:47 -0700427 options.echo_cancellation.value_or(false))) {
428 // If possible, turn off the high-pass filter.
Mirko Bonadei675513b2017-11-09 11:09:25 +0100429 RTC_LOG(LS_INFO)
430 << "Disable high-pass filter in response to field trial.";
peah8a8ebd92017-05-22 15:48:47 -0700431 options.highpass_filter = rtc::Optional<bool>(false);
432 }
433 }
434#endif
435
peah1bcfce52016-08-26 07:16:04 -0700436#if (WEBRTC_INTELLIGIBILITY_ENHANCER == 0)
437 // Hardcode the intelligibility enhancer to be off.
438 options.intelligibility_enhancer = rtc::Optional<bool>(false);
439#endif
440
kwiberg102c6a62015-10-30 02:47:38 -0700441 if (options.echo_cancellation) {
henrika@webrtc.orga954c072014-12-09 16:22:09 +0000442 // Check if platform supports built-in EC. Currently only supported on
443 // Android and in combination with Java based audio layer.
444 // TODO(henrika): investigate possibility to support built-in EC also
445 // in combination with Open SL ES audio.
solenberg5b5129a2016-04-08 05:35:48 -0700446 const bool built_in_aec = adm()->BuiltInAECIsAvailable();
Bjorn Volcker73f72102015-06-03 14:50:15 +0200447 if (built_in_aec) {
Bjorn Volckerccfc9392015-05-07 07:43:17 +0200448 // Built-in EC exists on this device and use_delay_agnostic_aec is not
449 // overriding it. Enable/Disable it according to the echo_cancellation
450 // audio option.
Bjorn Volcker73f72102015-06-03 14:50:15 +0200451 const bool enable_built_in_aec =
kwiberg102c6a62015-10-30 02:47:38 -0700452 *options.echo_cancellation && !use_delay_agnostic_aec;
solenberg5b5129a2016-04-08 05:35:48 -0700453 if (adm()->EnableBuiltInAEC(enable_built_in_aec) == 0 &&
Bjorn Volcker73f72102015-06-03 14:50:15 +0200454 enable_built_in_aec) {
Bjorn Volckerbf395c12015-03-25 22:45:56 +0100455 // Disable internal software EC if built-in EC is enabled,
henrika@webrtc.orga954c072014-12-09 16:22:09 +0000456 // i.e., replace the software EC with the built-in EC.
Karl Wibergbe579832015-11-10 22:34:18 +0100457 options.echo_cancellation = rtc::Optional<bool>(false);
Mirko Bonadei675513b2017-11-09 11:09:25 +0100458 RTC_LOG(LS_INFO)
459 << "Disabling EC since built-in EC will be used instead";
henrika@webrtc.orga954c072014-12-09 16:22:09 +0000460 }
461 }
solenberg76377c52017-02-21 00:54:31 -0800462 webrtc::apm_helpers::SetEcStatus(
463 apm(), *options.echo_cancellation, ec_mode);
Mirko Bonadeic8c71b92017-10-16 11:08:54 +0200464#if !defined(WEBRTC_ANDROID)
solenberg76377c52017-02-21 00:54:31 -0800465 webrtc::apm_helpers::SetEcMetricsStatus(apm(), *options.echo_cancellation);
buildbot@webrtc.org13d67762014-05-02 17:33:29 +0000466#endif
467 if (ec_mode == webrtc::kEcAecm) {
kwiberg102c6a62015-10-30 02:47:38 -0700468 bool cn = options.aecm_generate_comfort_noise.value_or(false);
solenberg76377c52017-02-21 00:54:31 -0800469 webrtc::apm_helpers::SetAecmMode(apm(), cn);
buildbot@webrtc.org13d67762014-05-02 17:33:29 +0000470 }
471 }
472
kwiberg102c6a62015-10-30 02:47:38 -0700473 if (options.auto_gain_control) {
peah72a56452016-08-22 12:08:55 -0700474 bool built_in_agc_avaliable = adm()->BuiltInAGCIsAvailable();
475 if (built_in_agc_avaliable) {
solenberg5b5129a2016-04-08 05:35:48 -0700476 if (adm()->EnableBuiltInAGC(*options.auto_gain_control) == 0 &&
kwiberg102c6a62015-10-30 02:47:38 -0700477 *options.auto_gain_control) {
henrikac14f5ff2015-09-23 14:08:33 +0200478 // Disable internal software AGC if built-in AGC is enabled,
479 // i.e., replace the software AGC with the built-in AGC.
Karl Wibergbe579832015-11-10 22:34:18 +0100480 options.auto_gain_control = rtc::Optional<bool>(false);
Mirko Bonadei675513b2017-11-09 11:09:25 +0100481 RTC_LOG(LS_INFO)
482 << "Disabling AGC since built-in AGC will be used instead";
henrikac14f5ff2015-09-23 14:08:33 +0200483 }
484 }
solenberg22818a52017-03-16 01:20:23 -0700485 webrtc::apm_helpers::SetAgcStatus(apm(), adm(), *options.auto_gain_control);
buildbot@webrtc.org13d67762014-05-02 17:33:29 +0000486 }
487
kwiberg102c6a62015-10-30 02:47:38 -0700488 if (options.tx_agc_target_dbov || options.tx_agc_digital_compression_gain ||
solenberg76377c52017-02-21 00:54:31 -0800489 options.tx_agc_limiter || options.adjust_agc_delta) {
buildbot@webrtc.org13d67762014-05-02 17:33:29 +0000490 // Override default_agc_config_. Generally, an unset option means "leave
491 // the VoE bits alone" in this function, so we want whatever is set to be
492 // stored as the new "default". If we didn't, then setting e.g.
493 // tx_agc_target_dbov would reset digital compression gain and limiter
494 // settings.
495 // Also, if we don't update default_agc_config_, then adjust_agc_delta
496 // would be an offset from the original values, and not whatever was set
497 // explicitly.
kwiberg102c6a62015-10-30 02:47:38 -0700498 default_agc_config_.targetLeveldBOv = options.tx_agc_target_dbov.value_or(
499 default_agc_config_.targetLeveldBOv);
buildbot@webrtc.org13d67762014-05-02 17:33:29 +0000500 default_agc_config_.digitalCompressionGaindB =
kwiberg102c6a62015-10-30 02:47:38 -0700501 options.tx_agc_digital_compression_gain.value_or(
buildbot@webrtc.org13d67762014-05-02 17:33:29 +0000502 default_agc_config_.digitalCompressionGaindB);
503 default_agc_config_.limiterEnable =
kwiberg102c6a62015-10-30 02:47:38 -0700504 options.tx_agc_limiter.value_or(default_agc_config_.limiterEnable);
solenberg76377c52017-02-21 00:54:31 -0800505
506 webrtc::AgcConfig config = default_agc_config_;
507 if (options.adjust_agc_delta) {
508 config.targetLeveldBOv -= *options.adjust_agc_delta;
Mirko Bonadei675513b2017-11-09 11:09:25 +0100509 RTC_LOG(LS_INFO) << "Adjusting AGC level from default -"
510 << default_agc_config_.targetLeveldBOv << "dB to -"
511 << config.targetLeveldBOv << "dB";
buildbot@webrtc.org13d67762014-05-02 17:33:29 +0000512 }
peaha9cc40b2017-06-29 08:32:09 -0700513 webrtc::apm_helpers::SetAgcConfig(apm(), config);
buildbot@webrtc.org13d67762014-05-02 17:33:29 +0000514 }
515
Alejandro Luebsc9b0c262016-05-16 15:32:38 -0700516 if (options.intelligibility_enhancer) {
517 intelligibility_enhancer_ = options.intelligibility_enhancer;
518 }
519 if (intelligibility_enhancer_ && *intelligibility_enhancer_) {
Mirko Bonadei675513b2017-11-09 11:09:25 +0100520 RTC_LOG(LS_INFO) << "Enabling NS when Intelligibility Enhancer is active.";
Alejandro Luebsc9b0c262016-05-16 15:32:38 -0700521 options.noise_suppression = intelligibility_enhancer_;
522 }
523
kwiberg102c6a62015-10-30 02:47:38 -0700524 if (options.noise_suppression) {
Alejandro Luebsc9b0c262016-05-16 15:32:38 -0700525 if (adm()->BuiltInNSIsAvailable()) {
526 bool builtin_ns =
527 *options.noise_suppression &&
528 !(intelligibility_enhancer_ && *intelligibility_enhancer_);
529 if (adm()->EnableBuiltInNS(builtin_ns) == 0 && builtin_ns) {
henrikac14f5ff2015-09-23 14:08:33 +0200530 // Disable internal software NS if built-in NS is enabled,
531 // i.e., replace the software NS with the built-in NS.
Karl Wibergbe579832015-11-10 22:34:18 +0100532 options.noise_suppression = rtc::Optional<bool>(false);
Mirko Bonadei675513b2017-11-09 11:09:25 +0100533 RTC_LOG(LS_INFO)
534 << "Disabling NS since built-in NS will be used instead";
henrikac14f5ff2015-09-23 14:08:33 +0200535 }
536 }
solenberg76377c52017-02-21 00:54:31 -0800537 webrtc::apm_helpers::SetNsStatus(apm(), *options.noise_suppression);
buildbot@webrtc.org13d67762014-05-02 17:33:29 +0000538 }
539
kwiberg102c6a62015-10-30 02:47:38 -0700540 if (options.stereo_swapping) {
Mirko Bonadei675513b2017-11-09 11:09:25 +0100541 RTC_LOG(LS_INFO) << "Stereo swapping enabled? " << *options.stereo_swapping;
solenberg76377c52017-02-21 00:54:31 -0800542 transmit_mixer()->EnableStereoChannelSwapping(*options.stereo_swapping);
buildbot@webrtc.org13d67762014-05-02 17:33:29 +0000543 }
544
kwiberg102c6a62015-10-30 02:47:38 -0700545 if (options.audio_jitter_buffer_max_packets) {
Mirko Bonadei675513b2017-11-09 11:09:25 +0100546 RTC_LOG(LS_INFO) << "NetEq capacity is "
547 << *options.audio_jitter_buffer_max_packets;
solenberg88499ec2016-09-07 07:34:41 -0700548 channel_config_.acm_config.neteq_config.max_packets_in_buffer =
549 std::max(20, *options.audio_jitter_buffer_max_packets);
Henrik Lundin64dad832015-05-11 12:44:23 +0200550 }
kwiberg102c6a62015-10-30 02:47:38 -0700551 if (options.audio_jitter_buffer_fast_accelerate) {
Mirko Bonadei675513b2017-11-09 11:09:25 +0100552 RTC_LOG(LS_INFO) << "NetEq fast mode? "
553 << *options.audio_jitter_buffer_fast_accelerate;
solenberg88499ec2016-09-07 07:34:41 -0700554 channel_config_.acm_config.neteq_config.enable_fast_accelerate =
555 *options.audio_jitter_buffer_fast_accelerate;
Henrik Lundin5263b3c2015-06-01 10:29:41 +0200556 }
557
kwiberg102c6a62015-10-30 02:47:38 -0700558 if (options.typing_detection) {
Mirko Bonadei675513b2017-11-09 11:09:25 +0100559 RTC_LOG(LS_INFO) << "Typing detection is enabled? "
560 << *options.typing_detection;
solenberg76377c52017-02-21 00:54:31 -0800561 webrtc::apm_helpers::SetTypingDetectionStatus(
562 apm(), *options.typing_detection);
buildbot@webrtc.org13d67762014-05-02 17:33:29 +0000563 }
564
buildbot@webrtc.org1f8a2372014-08-28 10:52:44 +0000565 webrtc::Config config;
566
kwiberg102c6a62015-10-30 02:47:38 -0700567 if (options.delay_agnostic_aec)
568 delay_agnostic_aec_ = options.delay_agnostic_aec;
569 if (delay_agnostic_aec_) {
Mirko Bonadei675513b2017-11-09 11:09:25 +0100570 RTC_LOG(LS_INFO) << "Delay agnostic aec is enabled? "
571 << *delay_agnostic_aec_;
henrik.lundin0f133b92015-07-02 00:17:55 -0700572 config.Set<webrtc::DelayAgnostic>(
kwiberg102c6a62015-10-30 02:47:38 -0700573 new webrtc::DelayAgnostic(*delay_agnostic_aec_));
Bjorn Volckerbf395c12015-03-25 22:45:56 +0100574 }
575
kwiberg102c6a62015-10-30 02:47:38 -0700576 if (options.extended_filter_aec) {
577 extended_filter_aec_ = options.extended_filter_aec;
578 }
579 if (extended_filter_aec_) {
Mirko Bonadei675513b2017-11-09 11:09:25 +0100580 RTC_LOG(LS_INFO) << "Extended filter aec is enabled? "
581 << *extended_filter_aec_;
Henrik Lundin441f6342015-06-09 16:03:13 +0200582 config.Set<webrtc::ExtendedFilter>(
kwiberg102c6a62015-10-30 02:47:38 -0700583 new webrtc::ExtendedFilter(*extended_filter_aec_));
buildbot@webrtc.org1f8a2372014-08-28 10:52:44 +0000584 }
585
kwiberg102c6a62015-10-30 02:47:38 -0700586 if (options.experimental_ns) {
587 experimental_ns_ = options.experimental_ns;
588 }
589 if (experimental_ns_) {
Mirko Bonadei675513b2017-11-09 11:09:25 +0100590 RTC_LOG(LS_INFO) << "Experimental ns is enabled? " << *experimental_ns_;
buildbot@webrtc.org1f8a2372014-08-28 10:52:44 +0000591 config.Set<webrtc::ExperimentalNs>(
kwiberg102c6a62015-10-30 02:47:38 -0700592 new webrtc::ExperimentalNs(*experimental_ns_));
buildbot@webrtc.org1f8a2372014-08-28 10:52:44 +0000593 }
buildbot@webrtc.org1f8a2372014-08-28 10:52:44 +0000594
Alejandro Luebsc9b0c262016-05-16 15:32:38 -0700595 if (intelligibility_enhancer_) {
Mirko Bonadei675513b2017-11-09 11:09:25 +0100596 RTC_LOG(LS_INFO) << "Intelligibility Enhancer is enabled? "
597 << *intelligibility_enhancer_;
Alejandro Luebsc9b0c262016-05-16 15:32:38 -0700598 config.Set<webrtc::Intelligibility>(
599 new webrtc::Intelligibility(*intelligibility_enhancer_));
600 }
601
peaha3333bf2016-06-30 00:02:34 -0700602 if (options.level_control) {
603 level_control_ = options.level_control;
604 }
605
peahb1c9d1d2017-07-25 15:45:24 -0700606 webrtc::AudioProcessing::Config apm_config = apm()->GetConfig();
607
Mirko Bonadei675513b2017-11-09 11:09:25 +0100608 RTC_LOG(LS_INFO) << "Level control: "
609 << (!!level_control_ ? *level_control_ : -1);
peaha3333bf2016-06-30 00:02:34 -0700610 if (level_control_) {
peahb1c9d1d2017-07-25 15:45:24 -0700611 apm_config.level_controller.enabled = *level_control_;
aleloie33c5d92016-10-20 01:53:27 -0700612 if (options.level_control_initial_peak_level_dbfs) {
peahb1c9d1d2017-07-25 15:45:24 -0700613 apm_config.level_controller.initial_peak_level_dbfs =
aleloie33c5d92016-10-20 01:53:27 -0700614 *options.level_control_initial_peak_level_dbfs;
615 }
peaha3333bf2016-06-30 00:02:34 -0700616 }
617
peah8271d042016-11-22 07:24:52 -0800618 if (options.highpass_filter) {
peahb1c9d1d2017-07-25 15:45:24 -0700619 apm_config.high_pass_filter.enabled = *options.highpass_filter;
peah8271d042016-11-22 07:24:52 -0800620 }
621
ivoc4ca18692017-02-10 05:11:09 -0800622 if (options.residual_echo_detector) {
peahb1c9d1d2017-07-25 15:45:24 -0700623 apm_config.residual_echo_detector.enabled = *options.residual_echo_detector;
ivoc4ca18692017-02-10 05:11:09 -0800624 }
625
solenberg059fb442016-10-26 05:12:24 -0700626 apm()->SetExtraOptions(config);
peahb1c9d1d2017-07-25 15:45:24 -0700627 apm()->ApplyConfig(apm_config);
buildbot@webrtc.org13d67762014-05-02 17:33:29 +0000628 return true;
629}
630
solenberg796b8f92017-03-01 17:02:23 -0800631// TODO(solenberg): Remove, once AudioMonitor is gone.
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000632int WebRtcVoiceEngine::GetInputLevel() {
solenberg566ef242015-11-06 15:34:49 -0800633 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
solenberg796b8f92017-03-01 17:02:23 -0800634 int8_t level = transmit_mixer()->AudioLevel();
635 RTC_DCHECK_LE(0, level);
636 return level;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000637}
638
ossudedfd282016-06-14 07:12:39 -0700639const std::vector<AudioCodec>& WebRtcVoiceEngine::send_codecs() const {
640 RTC_DCHECK(signal_thread_checker_.CalledOnValidThread());
ossuc54071d2016-08-17 02:45:41 -0700641 return send_codecs_;
ossudedfd282016-06-14 07:12:39 -0700642}
643
644const std::vector<AudioCodec>& WebRtcVoiceEngine::recv_codecs() const {
solenberg566ef242015-11-06 15:34:49 -0800645 RTC_DCHECK(signal_thread_checker_.CalledOnValidThread());
ossuc54071d2016-08-17 02:45:41 -0700646 return recv_codecs_;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000647}
648
Stefan Holmer9d69c3f2015-12-07 10:45:43 +0100649RtpCapabilities WebRtcVoiceEngine::GetCapabilities() const {
solenberg566ef242015-11-06 15:34:49 -0800650 RTC_DCHECK(signal_thread_checker_.CalledOnValidThread());
Stefan Holmer9d69c3f2015-12-07 10:45:43 +0100651 RtpCapabilities capabilities;
Stefan Holmer9d69c3f2015-12-07 10:45:43 +0100652 capabilities.header_extensions.push_back(
isheriff6f8d6862016-05-26 11:24:55 -0700653 webrtc::RtpExtension(webrtc::RtpExtension::kAudioLevelUri,
654 webrtc::RtpExtension::kAudioLevelDefaultId));
sprangc1b57a12017-02-28 08:50:47 -0800655 if (webrtc::field_trial::IsEnabled("WebRTC-Audio-SendSideBwe")) {
isheriff6f8d6862016-05-26 11:24:55 -0700656 capabilities.header_extensions.push_back(webrtc::RtpExtension(
657 webrtc::RtpExtension::kTransportSequenceNumberUri,
658 webrtc::RtpExtension::kTransportSequenceNumberDefaultId));
stefanba4c0e42016-02-04 04:12:24 -0800659 }
Stefan Holmer9d69c3f2015-12-07 10:45:43 +0100660 return capabilities;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000661}
662
solenberg63b34542015-09-29 06:06:31 -0700663void WebRtcVoiceEngine::RegisterChannel(WebRtcVoiceMediaChannel* channel) {
solenberg566ef242015-11-06 15:34:49 -0800664 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
665 RTC_DCHECK(channel);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000666 channels_.push_back(channel);
667}
668
solenberg63b34542015-09-29 06:06:31 -0700669void WebRtcVoiceEngine::UnregisterChannel(WebRtcVoiceMediaChannel* channel) {
solenberg566ef242015-11-06 15:34:49 -0800670 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
solenberg63b34542015-09-29 06:06:31 -0700671 auto it = std::find(channels_.begin(), channels_.end(), channel);
solenberg566ef242015-11-06 15:34:49 -0800672 RTC_DCHECK(it != channels_.end());
673 channels_.erase(it);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000674}
675
ivocd66b44d2016-01-15 03:06:36 -0800676bool WebRtcVoiceEngine::StartAecDump(rtc::PlatformFile file,
677 int64_t max_size_bytes) {
solenberg566ef242015-11-06 15:34:49 -0800678 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
deadbeefeb02c032017-06-15 08:29:25 -0700679 auto aec_dump = webrtc::AecDumpFactory::Create(
680 file, max_size_bytes, low_priority_worker_queue_.get());
aleloi048cbdd2017-05-29 02:56:27 -0700681 if (!aec_dump) {
wu@webrtc.orga8910d22014-01-23 22:12:45 +0000682 return false;
683 }
aleloi048cbdd2017-05-29 02:56:27 -0700684 apm()->AttachAecDump(std::move(aec_dump));
wu@webrtc.orga9890802013-12-13 00:21:03 +0000685 return true;
wu@webrtc.orga9890802013-12-13 00:21:03 +0000686}
687
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000688void WebRtcVoiceEngine::StartAecDump(const std::string& filename) {
solenberg566ef242015-11-06 15:34:49 -0800689 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
aleloi048cbdd2017-05-29 02:56:27 -0700690
deadbeefeb02c032017-06-15 08:29:25 -0700691 auto aec_dump = webrtc::AecDumpFactory::Create(
692 filename, -1, low_priority_worker_queue_.get());
aleloi048cbdd2017-05-29 02:56:27 -0700693 if (aec_dump) {
694 apm()->AttachAecDump(std::move(aec_dump));
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000695 }
696}
697
698void WebRtcVoiceEngine::StopAecDump() {
solenberg566ef242015-11-06 15:34:49 -0800699 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
aleloi048cbdd2017-05-29 02:56:27 -0700700 apm()->DetachAecDump();
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000701}
702
solenberg0a617e22015-10-20 15:49:38 -0700703int WebRtcVoiceEngine::CreateVoEChannel() {
solenberg566ef242015-11-06 15:34:49 -0800704 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
solenberg88499ec2016-09-07 07:34:41 -0700705 return voe_wrapper_->base()->CreateChannel(channel_config_);
sergeyu@chromium.org5bc25c42013-12-05 00:24:06 +0000706}
707
solenberg5b5129a2016-04-08 05:35:48 -0700708webrtc::AudioDeviceModule* WebRtcVoiceEngine::adm() {
709 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
710 RTC_DCHECK(adm_);
711 return adm_;
712}
713
peahb1c9d1d2017-07-25 15:45:24 -0700714webrtc::AudioProcessing* WebRtcVoiceEngine::apm() const {
solenberg059fb442016-10-26 05:12:24 -0700715 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
peaha9cc40b2017-06-29 08:32:09 -0700716 return apm_.get();
solenberg059fb442016-10-26 05:12:24 -0700717}
718
solenberg76377c52017-02-21 00:54:31 -0800719webrtc::voe::TransmitMixer* WebRtcVoiceEngine::transmit_mixer() {
720 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
721 RTC_DCHECK(transmit_mixer_);
722 return transmit_mixer_;
723}
724
ossu20a4b3f2017-04-27 02:08:52 -0700725AudioCodecs WebRtcVoiceEngine::CollectCodecs(
726 const std::vector<webrtc::AudioCodecSpec>& specs) const {
ossuc54071d2016-08-17 02:45:41 -0700727 PayloadTypeMapper mapper;
728 AudioCodecs out;
ossuc54071d2016-08-17 02:45:41 -0700729
solenberg2779bab2016-11-17 04:45:19 -0800730 // Only generate CN payload types for these clockrates:
ossuc54071d2016-08-17 02:45:41 -0700731 std::map<int, bool, std::greater<int>> generate_cn = {{ 8000, false },
732 { 16000, false },
733 { 32000, false }};
solenberg2779bab2016-11-17 04:45:19 -0800734 // Only generate telephone-event payload types for these clockrates:
735 std::map<int, bool, std::greater<int>> generate_dtmf = {{ 8000, false },
736 { 16000, false },
737 { 32000, false },
738 { 48000, false }};
ossuc54071d2016-08-17 02:45:41 -0700739
ossu9def8002017-02-09 05:14:32 -0800740 auto map_format = [&mapper](const webrtc::SdpAudioFormat& format,
741 AudioCodecs* out) {
ossuc54071d2016-08-17 02:45:41 -0700742 rtc::Optional<AudioCodec> opt_codec = mapper.ToAudioCodec(format);
ossu9def8002017-02-09 05:14:32 -0800743 if (opt_codec) {
744 if (out) {
745 out->push_back(*opt_codec);
746 }
747 } else {
Mirko Bonadei675513b2017-11-09 11:09:25 +0100748 RTC_LOG(LS_ERROR) << "Unable to assign payload type to format: "
749 << format;
ossuc54071d2016-08-17 02:45:41 -0700750 }
751
ossu9def8002017-02-09 05:14:32 -0800752 return opt_codec;
ossuc54071d2016-08-17 02:45:41 -0700753 };
754
ossud4e9f622016-08-18 02:01:17 -0700755 for (const auto& spec : specs) {
ossu9def8002017-02-09 05:14:32 -0800756 // We need to do some extra stuff before adding the main codecs to out.
757 rtc::Optional<AudioCodec> opt_codec = map_format(spec.format, nullptr);
758 if (opt_codec) {
759 AudioCodec& codec = *opt_codec;
ossua1a040a2017-04-06 10:03:21 -0700760 if (spec.info.supports_network_adaption) {
ossu9def8002017-02-09 05:14:32 -0800761 codec.AddFeedbackParam(
762 FeedbackParam(kRtcpFbParamTransportCc, kParamValueEmpty));
763 }
764
ossua1a040a2017-04-06 10:03:21 -0700765 if (spec.info.allow_comfort_noise) {
solenberg2779bab2016-11-17 04:45:19 -0800766 // Generate a CN entry if the decoder allows it and we support the
767 // clockrate.
768 auto cn = generate_cn.find(spec.format.clockrate_hz);
769 if (cn != generate_cn.end()) {
770 cn->second = true;
771 }
772 }
773
774 // Generate a telephone-event entry if we support the clockrate.
775 auto dtmf = generate_dtmf.find(spec.format.clockrate_hz);
776 if (dtmf != generate_dtmf.end()) {
777 dtmf->second = true;
ossuc54071d2016-08-17 02:45:41 -0700778 }
ossu9def8002017-02-09 05:14:32 -0800779
780 out.push_back(codec);
ossuc54071d2016-08-17 02:45:41 -0700781 }
782 }
783
solenberg2779bab2016-11-17 04:45:19 -0800784 // Add CN codecs after "proper" audio codecs.
ossuc54071d2016-08-17 02:45:41 -0700785 for (const auto& cn : generate_cn) {
786 if (cn.second) {
ossu9def8002017-02-09 05:14:32 -0800787 map_format({kCnCodecName, cn.first, 1}, &out);
ossuc54071d2016-08-17 02:45:41 -0700788 }
789 }
790
solenberg2779bab2016-11-17 04:45:19 -0800791 // Add telephone-event codecs last.
792 for (const auto& dtmf : generate_dtmf) {
793 if (dtmf.second) {
ossu9def8002017-02-09 05:14:32 -0800794 map_format({kDtmfCodecName, dtmf.first, 1}, &out);
solenberg2779bab2016-11-17 04:45:19 -0800795 }
796 }
ossuc54071d2016-08-17 02:45:41 -0700797
798 return out;
799}
800
solenbergc96df772015-10-21 13:01:53 -0700801class WebRtcVoiceMediaChannel::WebRtcAudioSendStream
Taylor Brandstetter1a018dc2016-03-08 12:37:39 -0800802 : public AudioSource::Sink {
mallinath@webrtc.org67ee6b92014-02-03 16:57:16 +0000803 public:
minyue7a973442016-10-20 03:27:12 -0700804 WebRtcAudioSendStream(
805 int ch,
806 webrtc::AudioTransport* voe_audio_transport,
807 uint32_t ssrc,
808 const std::string& c_name,
Alex Narestb3944f02017-10-13 14:56:18 +0200809 const std::string track_id,
ossu20a4b3f2017-04-27 02:08:52 -0700810 const rtc::Optional<webrtc::AudioSendStream::Config::SendCodecSpec>&
811 send_codec_spec,
minyue7a973442016-10-20 03:27:12 -0700812 const std::vector<webrtc::RtpExtension>& extensions,
813 int max_send_bitrate_bps,
minyue6b825df2016-10-31 04:08:32 -0700814 const rtc::Optional<std::string>& audio_network_adaptor_config,
minyue7a973442016-10-20 03:27:12 -0700815 webrtc::Call* call,
ossu20a4b3f2017-04-27 02:08:52 -0700816 webrtc::Transport* send_transport,
817 const rtc::scoped_refptr<webrtc::AudioEncoderFactory>& encoder_factory)
solenberg7add0582015-11-20 09:59:34 -0800818 : voe_audio_transport_(voe_audio_transport),
solenberg3a941542015-11-16 07:34:50 -0800819 call_(call),
mflodman3d7db262016-04-29 00:57:13 -0700820 config_(send_transport),
sprangc1b57a12017-02-28 08:50:47 -0800821 send_side_bwe_with_overhead_(
822 webrtc::field_trial::IsEnabled("WebRTC-SendSideBwe-WithOverhead")),
minyue7a973442016-10-20 03:27:12 -0700823 max_send_bitrate_bps_(max_send_bitrate_bps),
skvlade0d46372016-04-07 22:59:22 -0700824 rtp_parameters_(CreateRtpParametersWithOneEncoding()) {
solenberg85a04962015-10-27 03:35:21 -0700825 RTC_DCHECK_GE(ch, 0);
826 // TODO(solenberg): Once we're not using FakeWebRtcVoiceEngine anymore:
827 // RTC_DCHECK(voe_audio_transport);
solenbergc96df772015-10-21 13:01:53 -0700828 RTC_DCHECK(call);
ossu20a4b3f2017-04-27 02:08:52 -0700829 RTC_DCHECK(encoder_factory);
solenberg3a941542015-11-16 07:34:50 -0800830 config_.rtp.ssrc = ssrc;
831 config_.rtp.c_name = c_name;
832 config_.voe_channel_id = ch;
solenberg971cab02016-06-14 10:02:41 -0700833 config_.rtp.extensions = extensions;
minyue6b825df2016-10-31 04:08:32 -0700834 config_.audio_network_adaptor_config = audio_network_adaptor_config;
ossu20a4b3f2017-04-27 02:08:52 -0700835 config_.encoder_factory = encoder_factory;
Alex Narestb3944f02017-10-13 14:56:18 +0200836 config_.track_id = track_id;
deadbeefcb443432016-12-12 11:12:36 -0800837 rtp_parameters_.encodings[0].ssrc = rtc::Optional<uint32_t>(ssrc);
ossu20a4b3f2017-04-27 02:08:52 -0700838
839 if (send_codec_spec) {
840 UpdateSendCodecSpec(*send_codec_spec);
841 }
842
843 stream_ = call_->CreateAudioSendStream(config_);
solenbergc96df772015-10-21 13:01:53 -0700844 }
solenberg3a941542015-11-16 07:34:50 -0800845
solenbergc96df772015-10-21 13:01:53 -0700846 ~WebRtcAudioSendStream() override {
solenberg566ef242015-11-06 15:34:49 -0800847 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
Taylor Brandstetter1a018dc2016-03-08 12:37:39 -0800848 ClearSource();
solenbergc96df772015-10-21 13:01:53 -0700849 call_->DestroyAudioSendStream(stream_);
850 }
henrike@webrtc.org1e09a712013-07-26 19:17:59 +0000851
ossu20a4b3f2017-04-27 02:08:52 -0700852 void SetSendCodecSpec(
minyue7a973442016-10-20 03:27:12 -0700853 const webrtc::AudioSendStream::Config::SendCodecSpec& send_codec_spec) {
ossu20a4b3f2017-04-27 02:08:52 -0700854 UpdateSendCodecSpec(send_codec_spec);
855 ReconfigureAudioSendStream();
solenberg971cab02016-06-14 10:02:41 -0700856 }
857
ossu20a4b3f2017-04-27 02:08:52 -0700858 void SetRtpExtensions(const std::vector<webrtc::RtpExtension>& extensions) {
solenberg3a941542015-11-16 07:34:50 -0800859 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
solenberg3a941542015-11-16 07:34:50 -0800860 config_.rtp.extensions = extensions;
ossu20a4b3f2017-04-27 02:08:52 -0700861 ReconfigureAudioSendStream();
solenberg3a941542015-11-16 07:34:50 -0800862 }
863
ossu20a4b3f2017-04-27 02:08:52 -0700864 void SetAudioNetworkAdaptorConfig(
minyue6b825df2016-10-31 04:08:32 -0700865 const rtc::Optional<std::string>& audio_network_adaptor_config) {
866 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
867 if (config_.audio_network_adaptor_config == audio_network_adaptor_config) {
868 return;
869 }
870 config_.audio_network_adaptor_config = audio_network_adaptor_config;
ossu20a4b3f2017-04-27 02:08:52 -0700871 UpdateAllowedBitrateRange();
872 ReconfigureAudioSendStream();
minyue6b825df2016-10-31 04:08:32 -0700873 }
874
minyue7a973442016-10-20 03:27:12 -0700875 bool SetMaxSendBitrate(int bps) {
876 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
ossu20a4b3f2017-04-27 02:08:52 -0700877 RTC_DCHECK(config_.send_codec_spec);
878 RTC_DCHECK(audio_codec_spec_);
879 auto send_rate = ComputeSendBitrate(
880 bps, rtp_parameters_.encodings[0].max_bitrate_bps, *audio_codec_spec_);
881
minyue7a973442016-10-20 03:27:12 -0700882 if (!send_rate) {
883 return false;
884 }
885
886 max_send_bitrate_bps_ = bps;
887
ossu20a4b3f2017-04-27 02:08:52 -0700888 if (send_rate != config_.send_codec_spec->target_bitrate_bps) {
889 config_.send_codec_spec->target_bitrate_bps = send_rate;
890 ReconfigureAudioSendStream();
minyue7a973442016-10-20 03:27:12 -0700891 }
892 return true;
893 }
894
solenbergffbbcac2016-11-17 05:25:37 -0800895 bool SendTelephoneEvent(int payload_type, int payload_freq, int event,
896 int duration_ms) {
Fredrik Solenbergb5727682015-12-04 15:22:19 +0100897 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
898 RTC_DCHECK(stream_);
solenbergffbbcac2016-11-17 05:25:37 -0800899 return stream_->SendTelephoneEvent(payload_type, payload_freq, event,
900 duration_ms);
Fredrik Solenbergb5727682015-12-04 15:22:19 +0100901 }
902
Taylor Brandstetter1a018dc2016-03-08 12:37:39 -0800903 void SetSend(bool send) {
904 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
905 send_ = send;
906 UpdateSendState();
907 }
908
solenberg94218532016-06-16 10:53:22 -0700909 void SetMuted(bool muted) {
910 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
911 RTC_DCHECK(stream_);
912 stream_->SetMuted(muted);
913 muted_ = muted;
914 }
915
916 bool muted() const {
917 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
918 return muted_;
919 }
920
solenberg3a941542015-11-16 07:34:50 -0800921 webrtc::AudioSendStream::Stats GetStats() const {
922 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
923 RTC_DCHECK(stream_);
924 return stream_->GetStats();
925 }
926
Taylor Brandstetter1a018dc2016-03-08 12:37:39 -0800927 // Starts the sending by setting ourselves as a sink to the AudioSource to
928 // get data callbacks.
henrike@webrtc.orga7b98182014-02-21 15:51:43 +0000929 // This method is called on the libjingle worker thread.
mallinath@webrtc.org67ee6b92014-02-03 16:57:16 +0000930 // TODO(xians): Make sure Start() is called only once.
Taylor Brandstetter1a018dc2016-03-08 12:37:39 -0800931 void SetSource(AudioSource* source) {
solenberg566ef242015-11-06 15:34:49 -0800932 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
Taylor Brandstetter1a018dc2016-03-08 12:37:39 -0800933 RTC_DCHECK(source);
934 if (source_) {
935 RTC_DCHECK(source_ == source);
mallinath@webrtc.org67ee6b92014-02-03 16:57:16 +0000936 return;
937 }
Taylor Brandstetter1a018dc2016-03-08 12:37:39 -0800938 source->SetSink(this);
939 source_ = source;
940 UpdateSendState();
mallinath@webrtc.org67ee6b92014-02-03 16:57:16 +0000941 }
942
Taylor Brandstetter1a018dc2016-03-08 12:37:39 -0800943 // Stops sending by setting the sink of the AudioSource to nullptr. No data
mallinath@webrtc.org67ee6b92014-02-03 16:57:16 +0000944 // callback will be received after this method.
henrike@webrtc.orga7b98182014-02-21 15:51:43 +0000945 // This method is called on the libjingle worker thread.
Taylor Brandstetter1a018dc2016-03-08 12:37:39 -0800946 void ClearSource() {
solenberg566ef242015-11-06 15:34:49 -0800947 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
Taylor Brandstetter1a018dc2016-03-08 12:37:39 -0800948 if (source_) {
949 source_->SetSink(nullptr);
950 source_ = nullptr;
solenberg98c68862015-10-09 03:27:14 -0700951 }
Taylor Brandstetter1a018dc2016-03-08 12:37:39 -0800952 UpdateSendState();
mallinath@webrtc.org67ee6b92014-02-03 16:57:16 +0000953 }
954
Taylor Brandstetter1a018dc2016-03-08 12:37:39 -0800955 // AudioSource::Sink implementation.
henrike@webrtc.orga7b98182014-02-21 15:51:43 +0000956 // This method is called on the audio thread.
kjellander@webrtc.org14665ff2015-03-04 12:58:35 +0000957 void OnData(const void* audio_data,
958 int bits_per_sample,
959 int sample_rate,
Peter Kasting69558702016-01-12 16:26:35 -0800960 size_t number_of_channels,
Peter Kastingdce40cf2015-08-24 14:52:23 -0700961 size_t number_of_frames) override {
solenberg347ec5c2016-09-23 04:21:47 -0700962 RTC_CHECK_RUNS_SERIALIZED(&audio_capture_race_checker_);
solenbergc96df772015-10-21 13:01:53 -0700963 RTC_DCHECK(voe_audio_transport_);
maxmorin1aee0b52016-08-15 11:46:19 -0700964 voe_audio_transport_->PushCaptureData(config_.voe_channel_id, audio_data,
965 bits_per_sample, sample_rate,
966 number_of_channels, number_of_frames);
henrike@webrtc.orga7b98182014-02-21 15:51:43 +0000967 }
968
Taylor Brandstetter1a018dc2016-03-08 12:37:39 -0800969 // Callback from the |source_| when it is going away. In case Start() has
henrike@webrtc.orga7b98182014-02-21 15:51:43 +0000970 // never been called, this callback won't be triggered.
kjellander@webrtc.org14665ff2015-03-04 12:58:35 +0000971 void OnClose() override {
solenberg566ef242015-11-06 15:34:49 -0800972 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
Taylor Brandstetter1a018dc2016-03-08 12:37:39 -0800973 // Set |source_| to nullptr to make sure no more callback will get into
974 // the source.
975 source_ = nullptr;
976 UpdateSendState();
mallinath@webrtc.org67ee6b92014-02-03 16:57:16 +0000977 }
978
979 // Accessor to the VoE channel ID.
solenberg85a04962015-10-27 03:35:21 -0700980 int channel() const {
solenberg566ef242015-11-06 15:34:49 -0800981 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
solenberg7add0582015-11-20 09:59:34 -0800982 return config_.voe_channel_id;
solenberg85a04962015-10-27 03:35:21 -0700983 }
mallinath@webrtc.org67ee6b92014-02-03 16:57:16 +0000984
skvlade0d46372016-04-07 22:59:22 -0700985 const webrtc::RtpParameters& rtp_parameters() const {
986 return rtp_parameters_;
987 }
988
deadbeeffb2aced2017-01-06 23:05:37 -0800989 bool ValidateRtpParameters(const webrtc::RtpParameters& rtp_parameters) {
990 if (rtp_parameters.encodings.size() != 1) {
Mirko Bonadei675513b2017-11-09 11:09:25 +0100991 RTC_LOG(LS_ERROR)
deadbeeffb2aced2017-01-06 23:05:37 -0800992 << "Attempted to set RtpParameters without exactly one encoding";
993 return false;
994 }
995 if (rtp_parameters.encodings[0].ssrc != rtp_parameters_.encodings[0].ssrc) {
Mirko Bonadei675513b2017-11-09 11:09:25 +0100996 RTC_LOG(LS_ERROR) << "Attempted to set RtpParameters with modified SSRC";
deadbeeffb2aced2017-01-06 23:05:37 -0800997 return false;
998 }
999 return true;
1000 }
1001
minyue7a973442016-10-20 03:27:12 -07001002 bool SetRtpParameters(const webrtc::RtpParameters& parameters) {
deadbeeffb2aced2017-01-06 23:05:37 -08001003 if (!ValidateRtpParameters(parameters)) {
1004 return false;
1005 }
ossu20a4b3f2017-04-27 02:08:52 -07001006
1007 rtc::Optional<int> send_rate;
1008 if (audio_codec_spec_) {
1009 send_rate = ComputeSendBitrate(max_send_bitrate_bps_,
1010 parameters.encodings[0].max_bitrate_bps,
1011 *audio_codec_spec_);
1012 if (!send_rate) {
1013 return false;
1014 }
minyue7a973442016-10-20 03:27:12 -07001015 }
1016
minyuececec102017-03-27 13:04:25 -07001017 const rtc::Optional<int> old_rtp_max_bitrate =
1018 rtp_parameters_.encodings[0].max_bitrate_bps;
1019
skvlade0d46372016-04-07 22:59:22 -07001020 rtp_parameters_ = parameters;
minyue7a973442016-10-20 03:27:12 -07001021
minyuececec102017-03-27 13:04:25 -07001022 if (rtp_parameters_.encodings[0].max_bitrate_bps != old_rtp_max_bitrate) {
ossu20a4b3f2017-04-27 02:08:52 -07001023 // Reconfigure AudioSendStream with new bit rate.
1024 if (send_rate) {
1025 config_.send_codec_spec->target_bitrate_bps = send_rate;
1026 }
1027 UpdateAllowedBitrateRange();
1028 ReconfigureAudioSendStream();
minyue7a973442016-10-20 03:27:12 -07001029 } else {
1030 // parameters.encodings[0].active could have changed.
1031 UpdateSendState();
1032 }
1033 return true;
skvlade0d46372016-04-07 22:59:22 -07001034 }
1035
mallinath@webrtc.org67ee6b92014-02-03 16:57:16 +00001036 private:
Taylor Brandstetter1a018dc2016-03-08 12:37:39 -08001037 void UpdateSendState() {
1038 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
1039 RTC_DCHECK(stream_);
Taylor Brandstetter55dd7082016-05-03 13:50:11 -07001040 RTC_DCHECK_EQ(1UL, rtp_parameters_.encodings.size());
1041 if (send_ && source_ != nullptr && rtp_parameters_.encodings[0].active) {
Taylor Brandstetter1a018dc2016-03-08 12:37:39 -08001042 stream_->Start();
1043 } else { // !send || source_ = nullptr
1044 stream_->Stop();
1045 }
1046 }
1047
ossu20a4b3f2017-04-27 02:08:52 -07001048 void UpdateAllowedBitrateRange() {
michaelt53fe19d2016-10-18 09:39:22 -07001049 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
ossu20a4b3f2017-04-27 02:08:52 -07001050 const bool is_opus =
1051 config_.send_codec_spec &&
1052 !STR_CASE_CMP(config_.send_codec_spec->format.name.c_str(),
1053 kOpusCodecName);
1054 if (is_opus && webrtc::field_trial::IsEnabled("WebRTC-Audio-SendSideBwe")) {
stefane9f36d52017-01-24 08:18:45 -08001055 config_.min_bitrate_bps = kOpusMinBitrateBps;
minyuececec102017-03-27 13:04:25 -07001056
1057 // This means that when RtpParameters is reset, we may change the
ossu20a4b3f2017-04-27 02:08:52 -07001058 // encoder's bit rate immediately (through ReconfigureAudioSendStream()),
minyuececec102017-03-27 13:04:25 -07001059 // meanwhile change the cap to the output of BWE.
1060 config_.max_bitrate_bps =
1061 rtp_parameters_.encodings[0].max_bitrate_bps
1062 ? *rtp_parameters_.encodings[0].max_bitrate_bps
1063 : kOpusBitrateFbBps;
1064
michaelt53fe19d2016-10-18 09:39:22 -07001065 // TODO(mflodman): Keep testing this and set proper values.
1066 // Note: This is an early experiment currently only supported by Opus.
elad.alon0fe12162017-01-31 05:48:37 -08001067 if (send_side_bwe_with_overhead_) {
ossu20a4b3f2017-04-27 02:08:52 -07001068 const int max_packet_size_ms =
1069 WEBRTC_OPUS_SUPPORT_120MS_PTIME ? 120 : 60;
michaelt6672b262017-01-11 10:17:59 -08001070
ossu20a4b3f2017-04-27 02:08:52 -07001071 // OverheadPerPacket = Ipv4(20B) + UDP(8B) + SRTP(10B) + RTP(12)
1072 constexpr int kOverheadPerPacket = 20 + 8 + 10 + 12;
michaelt6672b262017-01-11 10:17:59 -08001073
ossu20a4b3f2017-04-27 02:08:52 -07001074 int min_overhead_bps =
1075 kOverheadPerPacket * 8 * 1000 / max_packet_size_ms;
michaelt6672b262017-01-11 10:17:59 -08001076
ossu20a4b3f2017-04-27 02:08:52 -07001077 // We assume that |config_.max_bitrate_bps| before the next line is
1078 // a hard limit on the payload bitrate, so we add min_overhead_bps to
1079 // it to ensure that, when overhead is deducted, the payload rate
1080 // never goes beyond the limit.
1081 // Note: this also means that if a higher overhead is forced, we
1082 // cannot reach the limit.
1083 // TODO(minyue): Reconsider this when the signaling to BWE is done
1084 // through a dedicated API.
1085 config_.max_bitrate_bps += min_overhead_bps;
michaelt6672b262017-01-11 10:17:59 -08001086
ossu20a4b3f2017-04-27 02:08:52 -07001087 // In contrast to max_bitrate_bps, we let min_bitrate_bps always be
1088 // reachable.
1089 config_.min_bitrate_bps += min_overhead_bps;
michaelt6672b262017-01-11 10:17:59 -08001090 }
michaelt53fe19d2016-10-18 09:39:22 -07001091 }
ossu20a4b3f2017-04-27 02:08:52 -07001092 }
1093
1094 void UpdateSendCodecSpec(
1095 const webrtc::AudioSendStream::Config::SendCodecSpec& send_codec_spec) {
1096 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
1097 config_.rtp.nack.rtp_history_ms =
1098 send_codec_spec.nack_enabled ? kNackRtpHistoryMs : 0;
1099 config_.send_codec_spec =
1100 rtc::Optional<webrtc::AudioSendStream::Config::SendCodecSpec>(
1101 send_codec_spec);
1102 auto info =
1103 config_.encoder_factory->QueryAudioEncoder(send_codec_spec.format);
1104 RTC_DCHECK(info);
1105 // If a specific target bitrate has been set for the stream, use that as
1106 // the new default bitrate when computing send bitrate.
1107 if (send_codec_spec.target_bitrate_bps) {
1108 info->default_bitrate_bps = std::max(
1109 info->min_bitrate_bps,
1110 std::min(info->max_bitrate_bps, *send_codec_spec.target_bitrate_bps));
1111 }
1112
1113 audio_codec_spec_.emplace(
1114 webrtc::AudioCodecSpec{send_codec_spec.format, *info});
1115
1116 config_.send_codec_spec->target_bitrate_bps = ComputeSendBitrate(
1117 max_send_bitrate_bps_, rtp_parameters_.encodings[0].max_bitrate_bps,
1118 *audio_codec_spec_);
1119
1120 UpdateAllowedBitrateRange();
1121 }
1122
1123 void ReconfigureAudioSendStream() {
1124 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
1125 RTC_DCHECK(stream_);
1126 stream_->Reconfigure(config_);
michaelt53fe19d2016-10-18 09:39:22 -07001127 }
1128
solenberg566ef242015-11-06 15:34:49 -08001129 rtc::ThreadChecker worker_thread_checker_;
solenberg347ec5c2016-09-23 04:21:47 -07001130 rtc::RaceChecker audio_capture_race_checker_;
solenbergc96df772015-10-21 13:01:53 -07001131 webrtc::AudioTransport* const voe_audio_transport_ = nullptr;
1132 webrtc::Call* call_ = nullptr;
solenberg3a941542015-11-16 07:34:50 -08001133 webrtc::AudioSendStream::Config config_;
elad.alon0fe12162017-01-31 05:48:37 -08001134 const bool send_side_bwe_with_overhead_;
solenberg3a941542015-11-16 07:34:50 -08001135 // The stream is owned by WebRtcAudioSendStream and may be reallocated if
1136 // configuration changes.
solenbergc96df772015-10-21 13:01:53 -07001137 webrtc::AudioSendStream* stream_ = nullptr;
mallinath@webrtc.org67ee6b92014-02-03 16:57:16 +00001138
Taylor Brandstetter1a018dc2016-03-08 12:37:39 -08001139 // Raw pointer to AudioSource owned by LocalAudioTrackHandler.
mallinath@webrtc.org67ee6b92014-02-03 16:57:16 +00001140 // PeerConnection will make sure invalidating the pointer before the object
1141 // goes away.
Taylor Brandstetter1a018dc2016-03-08 12:37:39 -08001142 AudioSource* source_ = nullptr;
1143 bool send_ = false;
solenberg94218532016-06-16 10:53:22 -07001144 bool muted_ = false;
minyue7a973442016-10-20 03:27:12 -07001145 int max_send_bitrate_bps_;
skvlade0d46372016-04-07 22:59:22 -07001146 webrtc::RtpParameters rtp_parameters_;
ossu20a4b3f2017-04-27 02:08:52 -07001147 rtc::Optional<webrtc::AudioCodecSpec> audio_codec_spec_;
henrike@webrtc.orga7b98182014-02-21 15:51:43 +00001148
solenbergc96df772015-10-21 13:01:53 -07001149 RTC_DISALLOW_IMPLICIT_CONSTRUCTORS(WebRtcAudioSendStream);
1150};
1151
1152class WebRtcVoiceMediaChannel::WebRtcAudioReceiveStream {
1153 public:
ossu29b1a8d2016-06-13 07:34:51 -07001154 WebRtcAudioReceiveStream(
1155 int ch,
1156 uint32_t remote_ssrc,
1157 uint32_t local_ssrc,
1158 bool use_transport_cc,
solenberg8189b022016-06-14 12:13:00 -07001159 bool use_nack,
ossu29b1a8d2016-06-13 07:34:51 -07001160 const std::string& sync_group,
1161 const std::vector<webrtc::RtpExtension>& extensions,
1162 webrtc::Call* call,
1163 webrtc::Transport* rtcp_send_transport,
kwiberg1c07c702017-03-27 07:15:49 -07001164 const rtc::scoped_refptr<webrtc::AudioDecoderFactory>& decoder_factory,
1165 const std::map<int, webrtc::SdpAudioFormat>& decoder_map)
stefanba4c0e42016-02-04 04:12:24 -08001166 : call_(call), config_() {
solenberg7add0582015-11-20 09:59:34 -08001167 RTC_DCHECK_GE(ch, 0);
1168 RTC_DCHECK(call);
1169 config_.rtp.remote_ssrc = remote_ssrc;
kwibergd32bf752017-01-19 07:03:59 -08001170 config_.rtp.local_ssrc = local_ssrc;
1171 config_.rtp.transport_cc = use_transport_cc;
1172 config_.rtp.nack.rtp_history_ms = use_nack ? kNackRtpHistoryMs : 0;
1173 config_.rtp.extensions = extensions;
solenberg31fec402016-05-06 02:13:12 -07001174 config_.rtcp_send_transport = rtcp_send_transport;
solenberg7add0582015-11-20 09:59:34 -08001175 config_.voe_channel_id = ch;
1176 config_.sync_group = sync_group;
ossu29b1a8d2016-06-13 07:34:51 -07001177 config_.decoder_factory = decoder_factory;
kwiberg1c07c702017-03-27 07:15:49 -07001178 config_.decoder_map = decoder_map;
kwibergd32bf752017-01-19 07:03:59 -08001179 RecreateAudioReceiveStream();
solenberg7add0582015-11-20 09:59:34 -08001180 }
solenbergc96df772015-10-21 13:01:53 -07001181
solenberg7add0582015-11-20 09:59:34 -08001182 ~WebRtcAudioReceiveStream() {
1183 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
1184 call_->DestroyAudioReceiveStream(stream_);
1185 }
1186
solenberg4a0f7b52016-06-16 13:07:33 -07001187 void RecreateAudioReceiveStream(uint32_t local_ssrc) {
solenberg7add0582015-11-20 09:59:34 -08001188 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
kwibergd32bf752017-01-19 07:03:59 -08001189 config_.rtp.local_ssrc = local_ssrc;
1190 RecreateAudioReceiveStream();
solenberg7add0582015-11-20 09:59:34 -08001191 }
solenberg8189b022016-06-14 12:13:00 -07001192
1193 void RecreateAudioReceiveStream(bool use_transport_cc, bool use_nack) {
solenberg7add0582015-11-20 09:59:34 -08001194 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
kwibergd32bf752017-01-19 07:03:59 -08001195 config_.rtp.transport_cc = use_transport_cc;
1196 config_.rtp.nack.rtp_history_ms = use_nack ? kNackRtpHistoryMs : 0;
1197 RecreateAudioReceiveStream();
solenberg7add0582015-11-20 09:59:34 -08001198 }
1199
solenberg4a0f7b52016-06-16 13:07:33 -07001200 void RecreateAudioReceiveStream(
1201 const std::vector<webrtc::RtpExtension>& extensions) {
1202 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
kwibergd32bf752017-01-19 07:03:59 -08001203 config_.rtp.extensions = extensions;
1204 RecreateAudioReceiveStream();
1205 }
1206
deadbeefcb383672017-04-26 16:28:42 -07001207 // Set a new payload type -> decoder map.
kwibergd32bf752017-01-19 07:03:59 -08001208 void RecreateAudioReceiveStream(
1209 const std::map<int, webrtc::SdpAudioFormat>& decoder_map) {
1210 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
kwibergd32bf752017-01-19 07:03:59 -08001211 config_.decoder_map = decoder_map;
1212 RecreateAudioReceiveStream();
solenberg4a0f7b52016-06-16 13:07:33 -07001213 }
1214
solenberg4904fb62017-02-17 12:01:14 -08001215 void MaybeRecreateAudioReceiveStream(const std::string& sync_group) {
1216 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
1217 if (config_.sync_group != sync_group) {
1218 config_.sync_group = sync_group;
1219 RecreateAudioReceiveStream();
1220 }
1221 }
1222
solenberg7add0582015-11-20 09:59:34 -08001223 webrtc::AudioReceiveStream::Stats GetStats() const {
1224 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
1225 RTC_DCHECK(stream_);
1226 return stream_->GetStats();
1227 }
1228
solenberg796b8f92017-03-01 17:02:23 -08001229 int GetOutputLevel() const {
1230 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
1231 RTC_DCHECK(stream_);
1232 return stream_->GetOutputLevel();
1233 }
1234
solenberg7add0582015-11-20 09:59:34 -08001235 int channel() const {
1236 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
1237 return config_.voe_channel_id;
1238 }
solenbergc96df772015-10-21 13:01:53 -07001239
kwiberg686a8ef2016-02-26 03:00:35 -08001240 void SetRawAudioSink(std::unique_ptr<webrtc::AudioSinkInterface> sink) {
Tommif888bb52015-12-12 01:37:01 +01001241 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
kwiberg686a8ef2016-02-26 03:00:35 -08001242 stream_->SetSink(std::move(sink));
Tommif888bb52015-12-12 01:37:01 +01001243 }
1244
solenberg217fb662016-06-17 08:30:54 -07001245 void SetOutputVolume(double volume) {
1246 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
1247 stream_->SetGain(volume);
1248 }
1249
aleloi84ef6152016-08-04 05:28:21 -07001250 void SetPlayout(bool playout) {
1251 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
1252 RTC_DCHECK(stream_);
1253 if (playout) {
Mirko Bonadei675513b2017-11-09 11:09:25 +01001254 RTC_LOG(LS_INFO) << "Starting playout for channel #" << channel();
aleloi84ef6152016-08-04 05:28:21 -07001255 stream_->Start();
1256 } else {
Mirko Bonadei675513b2017-11-09 11:09:25 +01001257 RTC_LOG(LS_INFO) << "Stopping playout for channel #" << channel();
aleloi84ef6152016-08-04 05:28:21 -07001258 stream_->Stop();
1259 }
aleloi18e0b672016-10-04 02:45:47 -07001260 playout_ = playout;
aleloi84ef6152016-08-04 05:28:21 -07001261 }
1262
hbos8d609f62017-04-10 07:39:05 -07001263 std::vector<webrtc::RtpSource> GetSources() {
1264 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
1265 RTC_DCHECK(stream_);
1266 return stream_->GetSources();
1267 }
1268
solenbergc96df772015-10-21 13:01:53 -07001269 private:
kwibergd32bf752017-01-19 07:03:59 -08001270 void RecreateAudioReceiveStream() {
solenberg7add0582015-11-20 09:59:34 -08001271 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
1272 if (stream_) {
1273 call_->DestroyAudioReceiveStream(stream_);
solenberg7add0582015-11-20 09:59:34 -08001274 }
solenberg7add0582015-11-20 09:59:34 -08001275 stream_ = call_->CreateAudioReceiveStream(config_);
1276 RTC_CHECK(stream_);
aleloi18e0b672016-10-04 02:45:47 -07001277 SetPlayout(playout_);
solenberg7add0582015-11-20 09:59:34 -08001278 }
1279
1280 rtc::ThreadChecker worker_thread_checker_;
1281 webrtc::Call* call_ = nullptr;
1282 webrtc::AudioReceiveStream::Config config_;
1283 // The stream is owned by WebRtcAudioReceiveStream and may be reallocated if
1284 // configuration changes.
1285 webrtc::AudioReceiveStream* stream_ = nullptr;
aleloi18e0b672016-10-04 02:45:47 -07001286 bool playout_ = false;
solenbergc96df772015-10-21 13:01:53 -07001287
1288 RTC_DISALLOW_IMPLICIT_CONSTRUCTORS(WebRtcAudioReceiveStream);
henrike@webrtc.org1e09a712013-07-26 19:17:59 +00001289};
1290
Fredrik Solenberg709ed672015-09-15 12:26:33 +02001291WebRtcVoiceMediaChannel::WebRtcVoiceMediaChannel(WebRtcVoiceEngine* engine,
nisse51542be2016-02-12 02:27:06 -08001292 const MediaConfig& config,
Fredrik Solenbergb071a192015-09-17 16:42:56 +02001293 const AudioOptions& options,
Fredrik Solenberg709ed672015-09-15 12:26:33 +02001294 webrtc::Call* call)
nisse51542be2016-02-12 02:27:06 -08001295 : VoiceMediaChannel(config), engine_(engine), call_(call) {
Mirko Bonadei675513b2017-11-09 11:09:25 +01001296 RTC_LOG(LS_VERBOSE) << "WebRtcVoiceMediaChannel::WebRtcVoiceMediaChannel";
solenberg566ef242015-11-06 15:34:49 -08001297 RTC_DCHECK(call);
solenberg0a617e22015-10-20 15:49:38 -07001298 engine->RegisterChannel(this);
Fredrik Solenbergb071a192015-09-17 16:42:56 +02001299 SetOptions(options);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001300}
1301
1302WebRtcVoiceMediaChannel::~WebRtcVoiceMediaChannel() {
solenberg566ef242015-11-06 15:34:49 -08001303 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
Mirko Bonadei675513b2017-11-09 11:09:25 +01001304 RTC_LOG(LS_VERBOSE) << "WebRtcVoiceMediaChannel::~WebRtcVoiceMediaChannel";
solenberg7add0582015-11-20 09:59:34 -08001305 // TODO(solenberg): Should be able to delete the streams directly, without
1306 // going through RemoveNnStream(), once stream objects handle
1307 // all (de)configuration.
solenbergc96df772015-10-21 13:01:53 -07001308 while (!send_streams_.empty()) {
1309 RemoveSendStream(send_streams_.begin()->first);
solenbergd97ec302015-10-07 01:40:33 -07001310 }
solenberg7add0582015-11-20 09:59:34 -08001311 while (!recv_streams_.empty()) {
1312 RemoveRecvStream(recv_streams_.begin()->first);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001313 }
solenberg0a617e22015-10-20 15:49:38 -07001314 engine()->UnregisterChannel(this);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001315}
1316
nisse51542be2016-02-12 02:27:06 -08001317rtc::DiffServCodePoint WebRtcVoiceMediaChannel::PreferredDscp() const {
1318 return kAudioDscpValue;
1319}
1320
Peter Thatcherc2ee2c82015-08-07 16:05:34 -07001321bool WebRtcVoiceMediaChannel::SetSendParameters(
1322 const AudioSendParameters& params) {
Peter Boströmca8b4042016-03-08 14:24:13 -08001323 TRACE_EVENT0("webrtc", "WebRtcVoiceMediaChannel::SetSendParameters");
solenberg566ef242015-11-06 15:34:49 -08001324 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
Mirko Bonadei675513b2017-11-09 11:09:25 +01001325 RTC_LOG(LS_INFO) << "WebRtcVoiceMediaChannel::SetSendParameters: "
1326 << params.ToString();
Peter Thatcherc2ee2c82015-08-07 16:05:34 -07001327 // TODO(pthatcher): Refactor this to be more clean now that we have
1328 // all the information at once.
solenberg3a941542015-11-16 07:34:50 -08001329
1330 if (!SetSendCodecs(params.codecs)) {
1331 return false;
1332 }
1333
solenberg7e4e01a2015-12-02 08:05:01 -08001334 if (!ValidateRtpExtensions(params.extensions)) {
1335 return false;
1336 }
1337 std::vector<webrtc::RtpExtension> filtered_extensions =
1338 FilterRtpExtensions(params.extensions,
1339 webrtc::RtpExtension::IsSupportedForAudio, true);
1340 if (send_rtp_extensions_ != filtered_extensions) {
1341 send_rtp_extensions_.swap(filtered_extensions);
solenberg3a941542015-11-16 07:34:50 -08001342 for (auto& it : send_streams_) {
ossu20a4b3f2017-04-27 02:08:52 -07001343 it.second->SetRtpExtensions(send_rtp_extensions_);
solenberg3a941542015-11-16 07:34:50 -08001344 }
1345 }
1346
deadbeef80346142016-04-27 14:17:10 -07001347 if (!SetMaxSendBitrate(params.max_bandwidth_bps)) {
solenberg3a941542015-11-16 07:34:50 -08001348 return false;
1349 }
1350 return SetOptions(params.options);
Peter Thatcherc2ee2c82015-08-07 16:05:34 -07001351}
1352
1353bool WebRtcVoiceMediaChannel::SetRecvParameters(
1354 const AudioRecvParameters& params) {
Peter Boströmca8b4042016-03-08 14:24:13 -08001355 TRACE_EVENT0("webrtc", "WebRtcVoiceMediaChannel::SetRecvParameters");
solenberg566ef242015-11-06 15:34:49 -08001356 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
Mirko Bonadei675513b2017-11-09 11:09:25 +01001357 RTC_LOG(LS_INFO) << "WebRtcVoiceMediaChannel::SetRecvParameters: "
1358 << params.ToString();
Peter Thatcherc2ee2c82015-08-07 16:05:34 -07001359 // TODO(pthatcher): Refactor this to be more clean now that we have
1360 // all the information at once.
solenberg7add0582015-11-20 09:59:34 -08001361
1362 if (!SetRecvCodecs(params.codecs)) {
1363 return false;
1364 }
1365
solenberg7e4e01a2015-12-02 08:05:01 -08001366 if (!ValidateRtpExtensions(params.extensions)) {
1367 return false;
1368 }
1369 std::vector<webrtc::RtpExtension> filtered_extensions =
1370 FilterRtpExtensions(params.extensions,
1371 webrtc::RtpExtension::IsSupportedForAudio, false);
1372 if (recv_rtp_extensions_ != filtered_extensions) {
1373 recv_rtp_extensions_.swap(filtered_extensions);
solenberg7add0582015-11-20 09:59:34 -08001374 for (auto& it : recv_streams_) {
1375 it.second->RecreateAudioReceiveStream(recv_rtp_extensions_);
1376 }
1377 }
solenberg7add0582015-11-20 09:59:34 -08001378 return true;
Peter Thatcherc2ee2c82015-08-07 16:05:34 -07001379}
1380
Taylor Brandstetterdb0cd9e2016-05-16 11:40:30 -07001381webrtc::RtpParameters WebRtcVoiceMediaChannel::GetRtpSendParameters(
skvlade0d46372016-04-07 22:59:22 -07001382 uint32_t ssrc) const {
1383 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
1384 auto it = send_streams_.find(ssrc);
1385 if (it == send_streams_.end()) {
Mirko Bonadei675513b2017-11-09 11:09:25 +01001386 RTC_LOG(LS_WARNING) << "Attempting to get RTP send parameters for stream "
1387 << "with ssrc " << ssrc << " which doesn't exist.";
skvlade0d46372016-04-07 22:59:22 -07001388 return webrtc::RtpParameters();
1389 }
1390
Taylor Brandstetter0cd086b2016-04-20 16:23:10 -07001391 webrtc::RtpParameters rtp_params = it->second->rtp_parameters();
1392 // Need to add the common list of codecs to the send stream-specific
1393 // RTP parameters.
1394 for (const AudioCodec& codec : send_codecs_) {
1395 rtp_params.codecs.push_back(codec.ToCodecParameters());
1396 }
1397 return rtp_params;
skvlade0d46372016-04-07 22:59:22 -07001398}
1399
Taylor Brandstetterdb0cd9e2016-05-16 11:40:30 -07001400bool WebRtcVoiceMediaChannel::SetRtpSendParameters(
skvlade0d46372016-04-07 22:59:22 -07001401 uint32_t ssrc,
1402 const webrtc::RtpParameters& parameters) {
1403 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
skvlade0d46372016-04-07 22:59:22 -07001404 auto it = send_streams_.find(ssrc);
1405 if (it == send_streams_.end()) {
Mirko Bonadei675513b2017-11-09 11:09:25 +01001406 RTC_LOG(LS_WARNING) << "Attempting to set RTP send parameters for stream "
1407 << "with ssrc " << ssrc << " which doesn't exist.";
skvlade0d46372016-04-07 22:59:22 -07001408 return false;
1409 }
1410
Taylor Brandstetterdb0cd9e2016-05-16 11:40:30 -07001411 // TODO(deadbeef): Handle setting parameters with a list of codecs in a
1412 // different order (which should change the send codec).
1413 webrtc::RtpParameters current_parameters = GetRtpSendParameters(ssrc);
1414 if (current_parameters.codecs != parameters.codecs) {
Mirko Bonadei675513b2017-11-09 11:09:25 +01001415 RTC_LOG(LS_ERROR) << "Using SetParameters to change the set of codecs "
1416 << "is not currently supported.";
Taylor Brandstetterdb0cd9e2016-05-16 11:40:30 -07001417 return false;
1418 }
1419
minyue7a973442016-10-20 03:27:12 -07001420 // TODO(minyue): The following legacy actions go into
1421 // |WebRtcAudioSendStream::SetRtpParameters()| which is called at the end,
1422 // though there are two difference:
1423 // 1. |WebRtcVoiceMediaChannel::SetChannelSendParameters()| only calls
1424 // |SetSendCodec| while |WebRtcAudioSendStream::SetRtpParameters()| calls
1425 // |SetSendCodecs|. The outcome should be the same.
1426 // 2. AudioSendStream can be recreated.
1427
Taylor Brandstetter0cd086b2016-04-20 16:23:10 -07001428 // Codecs are handled at the WebRtcVoiceMediaChannel level.
1429 webrtc::RtpParameters reduced_params = parameters;
1430 reduced_params.codecs.clear();
minyue7a973442016-10-20 03:27:12 -07001431 return it->second->SetRtpParameters(reduced_params);
skvlade0d46372016-04-07 22:59:22 -07001432}
1433
Taylor Brandstetterdb0cd9e2016-05-16 11:40:30 -07001434webrtc::RtpParameters WebRtcVoiceMediaChannel::GetRtpReceiveParameters(
1435 uint32_t ssrc) const {
1436 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
deadbeef3bc15102017-04-20 19:25:07 -07001437 webrtc::RtpParameters rtp_params;
1438 // SSRC of 0 represents the default receive stream.
1439 if (ssrc == 0) {
1440 if (!default_sink_) {
Mirko Bonadei675513b2017-11-09 11:09:25 +01001441 RTC_LOG(LS_WARNING)
1442 << "Attempting to get RTP parameters for the default, "
1443 "unsignaled audio receive stream, but not yet "
1444 "configured to receive such a stream.";
deadbeef3bc15102017-04-20 19:25:07 -07001445 return rtp_params;
1446 }
1447 rtp_params.encodings.emplace_back();
1448 } else {
1449 auto it = recv_streams_.find(ssrc);
1450 if (it == recv_streams_.end()) {
Mirko Bonadei675513b2017-11-09 11:09:25 +01001451 RTC_LOG(LS_WARNING)
1452 << "Attempting to get RTP receive parameters for stream "
1453 << "with ssrc " << ssrc << " which doesn't exist.";
deadbeef3bc15102017-04-20 19:25:07 -07001454 return webrtc::RtpParameters();
1455 }
1456 rtp_params.encodings.emplace_back();
1457 // TODO(deadbeef): Return stream-specific parameters.
1458 rtp_params.encodings[0].ssrc = rtc::Optional<uint32_t>(ssrc);
Taylor Brandstetterdb0cd9e2016-05-16 11:40:30 -07001459 }
1460
Taylor Brandstetterdb0cd9e2016-05-16 11:40:30 -07001461 for (const AudioCodec& codec : recv_codecs_) {
1462 rtp_params.codecs.push_back(codec.ToCodecParameters());
1463 }
1464 return rtp_params;
1465}
1466
1467bool WebRtcVoiceMediaChannel::SetRtpReceiveParameters(
1468 uint32_t ssrc,
1469 const webrtc::RtpParameters& parameters) {
1470 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
deadbeef3bc15102017-04-20 19:25:07 -07001471 // SSRC of 0 represents the default receive stream.
1472 if (ssrc == 0) {
1473 if (!default_sink_) {
Mirko Bonadei675513b2017-11-09 11:09:25 +01001474 RTC_LOG(LS_WARNING)
1475 << "Attempting to set RTP parameters for the default, "
1476 "unsignaled audio receive stream, but not yet "
1477 "configured to receive such a stream.";
deadbeef3bc15102017-04-20 19:25:07 -07001478 return false;
1479 }
1480 } else {
1481 auto it = recv_streams_.find(ssrc);
1482 if (it == recv_streams_.end()) {
Mirko Bonadei675513b2017-11-09 11:09:25 +01001483 RTC_LOG(LS_WARNING)
1484 << "Attempting to set RTP receive parameters for stream "
1485 << "with ssrc " << ssrc << " which doesn't exist.";
deadbeef3bc15102017-04-20 19:25:07 -07001486 return false;
1487 }
Taylor Brandstetterdb0cd9e2016-05-16 11:40:30 -07001488 }
1489
1490 webrtc::RtpParameters current_parameters = GetRtpReceiveParameters(ssrc);
1491 if (current_parameters != parameters) {
Mirko Bonadei675513b2017-11-09 11:09:25 +01001492 RTC_LOG(LS_ERROR) << "Changing the RTP receive parameters is currently "
1493 << "unsupported.";
Taylor Brandstetterdb0cd9e2016-05-16 11:40:30 -07001494 return false;
1495 }
1496 return true;
1497}
1498
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001499bool WebRtcVoiceMediaChannel::SetOptions(const AudioOptions& options) {
solenberg566ef242015-11-06 15:34:49 -08001500 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
Mirko Bonadei675513b2017-11-09 11:09:25 +01001501 RTC_LOG(LS_INFO) << "Setting voice channel options: " << options.ToString();
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001502
1503 // We retain all of the existing options, and apply the given ones
1504 // on top. This means there is no way to "clear" options such that
1505 // they go back to the engine default.
1506 options_.SetAll(options);
solenberg246b8172015-12-08 09:50:23 -08001507 if (!engine()->ApplyOptions(options_)) {
Mirko Bonadei675513b2017-11-09 11:09:25 +01001508 RTC_LOG(LS_WARNING)
1509 << "Failed to apply engine options during channel SetOptions.";
solenberg246b8172015-12-08 09:50:23 -08001510 return false;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001511 }
minyue6b825df2016-10-31 04:08:32 -07001512
ossu20a4b3f2017-04-27 02:08:52 -07001513 rtc::Optional<std::string> audio_network_adaptor_config =
minyue6b825df2016-10-31 04:08:32 -07001514 GetAudioNetworkAdaptorConfig(options_);
1515 for (auto& it : send_streams_) {
ossu20a4b3f2017-04-27 02:08:52 -07001516 it.second->SetAudioNetworkAdaptorConfig(audio_network_adaptor_config);
minyue6b825df2016-10-31 04:08:32 -07001517 }
1518
Mirko Bonadei675513b2017-11-09 11:09:25 +01001519 RTC_LOG(LS_INFO) << "Set voice channel options. Current options: "
1520 << options_.ToString();
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001521 return true;
1522}
1523
1524bool WebRtcVoiceMediaChannel::SetRecvCodecs(
1525 const std::vector<AudioCodec>& codecs) {
solenberg566ef242015-11-06 15:34:49 -08001526 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
solenberg8fb30c32015-10-13 03:06:58 -07001527
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001528 // Set the payload types to be used for incoming media.
Mirko Bonadei675513b2017-11-09 11:09:25 +01001529 RTC_LOG(LS_INFO) << "Setting receive voice codecs.";
solenberg0b675462015-10-09 01:37:09 -07001530
1531 if (!VerifyUniquePayloadTypes(codecs)) {
Mirko Bonadei675513b2017-11-09 11:09:25 +01001532 RTC_LOG(LS_ERROR) << "Codec payload types overlap.";
solenberg0b675462015-10-09 01:37:09 -07001533 return false;
1534 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001535
kwibergd32bf752017-01-19 07:03:59 -08001536 // Create a payload type -> SdpAudioFormat map with all the decoders. Fail
1537 // unless the factory claims to support all decoders.
1538 std::map<int, webrtc::SdpAudioFormat> decoder_map;
1539 for (const AudioCodec& codec : codecs) {
deadbeefcb383672017-04-26 16:28:42 -07001540 // Log a warning if a codec's payload type is changing. This used to be
1541 // treated as an error. It's abnormal, but not really illegal.
1542 AudioCodec old_codec;
1543 if (FindCodec(recv_codecs_, codec, &old_codec) &&
1544 old_codec.id != codec.id) {
Mirko Bonadei675513b2017-11-09 11:09:25 +01001545 RTC_LOG(LS_WARNING) << codec.name << " mapped to a second payload type ("
1546 << codec.id << ", was already mapped to "
1547 << old_codec.id << ")";
deadbeefcb383672017-04-26 16:28:42 -07001548 }
kwibergd32bf752017-01-19 07:03:59 -08001549 auto format = AudioCodecToSdpAudioFormat(codec);
1550 if (!IsCodec(codec, "cn") && !IsCodec(codec, "telephone-event") &&
1551 !engine()->decoder_factory_->IsSupportedDecoder(format)) {
Mirko Bonadei675513b2017-11-09 11:09:25 +01001552 RTC_LOG(LS_ERROR) << "Unsupported codec: " << format;
kwibergd32bf752017-01-19 07:03:59 -08001553 return false;
1554 }
deadbeefcb383672017-04-26 16:28:42 -07001555 // We allow adding new codecs but don't allow changing the payload type of
1556 // codecs that are already configured since we might already be receiving
1557 // packets with that payload type. See RFC3264, Section 8.3.2.
1558 // TODO(deadbeef): Also need to check for clashes with previously mapped
1559 // payload types, and not just currently mapped ones. For example, this
1560 // should be illegal:
1561 // 1. {100: opus/48000/2, 101: ISAC/16000}
1562 // 2. {100: opus/48000/2}
1563 // 3. {100: opus/48000/2, 101: ISAC/32000}
1564 // Though this check really should happen at a higher level, since this
1565 // conflict could happen between audio and video codecs.
1566 auto existing = decoder_map_.find(codec.id);
1567 if (existing != decoder_map_.end() && !existing->second.Matches(format)) {
Mirko Bonadei675513b2017-11-09 11:09:25 +01001568 RTC_LOG(LS_ERROR) << "Attempting to use payload type " << codec.id
1569 << " for " << codec.name
1570 << ", but it is already used for "
1571 << existing->second.name;
deadbeefcb383672017-04-26 16:28:42 -07001572 return false;
1573 }
kwibergd32bf752017-01-19 07:03:59 -08001574 decoder_map.insert({codec.id, std::move(format)});
1575 }
1576
deadbeefcb383672017-04-26 16:28:42 -07001577 if (decoder_map == decoder_map_) {
1578 // There's nothing new to configure.
1579 return true;
1580 }
1581
kwiberg37b8b112016-11-03 02:46:53 -07001582 if (playout_) {
1583 // Receive codecs can not be changed while playing. So we temporarily
1584 // pause playout.
1585 ChangePlayout(false);
1586 }
1587
kwiberg1c07c702017-03-27 07:15:49 -07001588 decoder_map_ = std::move(decoder_map);
kwibergd32bf752017-01-19 07:03:59 -08001589 for (auto& kv : recv_streams_) {
kwiberg1c07c702017-03-27 07:15:49 -07001590 kv.second->RecreateAudioReceiveStream(decoder_map_);
solenberg26c8c912015-11-27 04:00:25 -08001591 }
kwibergd32bf752017-01-19 07:03:59 -08001592 recv_codecs_ = codecs;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001593
kwiberg37b8b112016-11-03 02:46:53 -07001594 if (desired_playout_ && !playout_) {
1595 ChangePlayout(desired_playout_);
1596 }
kwibergd32bf752017-01-19 07:03:59 -08001597 return true;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001598}
1599
solenberg72e29d22016-03-08 06:35:16 -08001600// Utility function called from SetSendParameters() to extract current send
1601// codec settings from the given list of codecs (originally from SDP). Both send
1602// and receive streams may be reconfigured based on the new settings.
wu@webrtc.orgcadf9042013-08-30 21:24:16 +00001603bool WebRtcVoiceMediaChannel::SetSendCodecs(
1604 const std::vector<AudioCodec>& codecs) {
solenberg566ef242015-11-06 15:34:49 -08001605 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
Fredrik Solenbergb5727682015-12-04 15:22:19 +01001606 dtmf_payload_type_ = rtc::Optional<int>();
solenbergffbbcac2016-11-17 05:25:37 -08001607 dtmf_payload_freq_ = -1;
1608
1609 // Validate supplied codecs list.
1610 for (const AudioCodec& codec : codecs) {
1611 // TODO(solenberg): Validate more aspects of input - that payload types
1612 // don't overlap, remove redundant/unsupported codecs etc -
1613 // the same way it is done for RtpHeaderExtensions.
1614 if (codec.id < kMinPayloadType || codec.id > kMaxPayloadType) {
Mirko Bonadei675513b2017-11-09 11:09:25 +01001615 RTC_LOG(LS_WARNING) << "Codec payload type out of range: "
1616 << ToString(codec);
solenbergffbbcac2016-11-17 05:25:37 -08001617 return false;
1618 }
1619 }
1620
1621 // Find PT of telephone-event codec with lowest clockrate, as a fallback, in
1622 // case we don't have a DTMF codec with a rate matching the send codec's, or
1623 // if this function returns early.
1624 std::vector<AudioCodec> dtmf_codecs;
Fredrik Solenbergaf9fb212015-08-26 10:45:53 +02001625 for (const AudioCodec& codec : codecs) {
Fredrik Solenbergaf9fb212015-08-26 10:45:53 +02001626 if (IsCodec(codec, kDtmfCodecName)) {
solenbergffbbcac2016-11-17 05:25:37 -08001627 dtmf_codecs.push_back(codec);
1628 if (!dtmf_payload_type_ || codec.clockrate < dtmf_payload_freq_) {
1629 dtmf_payload_type_ = rtc::Optional<int>(codec.id);
1630 dtmf_payload_freq_ = codec.clockrate;
solenberg31642aa2016-03-14 08:00:37 -07001631 }
wu@webrtc.orgcadf9042013-08-30 21:24:16 +00001632 }
1633 }
1634
ossu20a4b3f2017-04-27 02:08:52 -07001635 // Scan through the list to figure out the codec to use for sending.
1636 rtc::Optional<webrtc::AudioSendStream::Config::SendCodecSpec> send_codec_spec;
stefan1ccf73f2017-03-27 03:51:18 -07001637 webrtc::Call::Config::BitrateConfig bitrate_config;
ossu20a4b3f2017-04-27 02:08:52 -07001638 rtc::Optional<webrtc::AudioCodecInfo> voice_codec_info;
1639 for (const AudioCodec& voice_codec : codecs) {
1640 if (!(IsCodec(voice_codec, kCnCodecName) ||
1641 IsCodec(voice_codec, kDtmfCodecName) ||
1642 IsCodec(voice_codec, kRedCodecName))) {
1643 webrtc::SdpAudioFormat format(voice_codec.name, voice_codec.clockrate,
1644 voice_codec.channels, voice_codec.params);
solenberg72e29d22016-03-08 06:35:16 -08001645
ossu20a4b3f2017-04-27 02:08:52 -07001646 voice_codec_info = engine()->encoder_factory_->QueryAudioEncoder(format);
1647 if (!voice_codec_info) {
Mirko Bonadei675513b2017-11-09 11:09:25 +01001648 RTC_LOG(LS_WARNING) << "Unknown codec " << ToString(voice_codec);
solenberg72e29d22016-03-08 06:35:16 -08001649 continue;
1650 }
1651
ossu20a4b3f2017-04-27 02:08:52 -07001652 send_codec_spec =
1653 rtc::Optional<webrtc::AudioSendStream::Config::SendCodecSpec>(
1654 {voice_codec.id, format});
1655 if (voice_codec.bitrate > 0) {
1656 send_codec_spec->target_bitrate_bps =
1657 rtc::Optional<int>(voice_codec.bitrate);
1658 }
1659 send_codec_spec->transport_cc_enabled = HasTransportCc(voice_codec);
1660 send_codec_spec->nack_enabled = HasNack(voice_codec);
1661 bitrate_config = GetBitrateConfigForCodec(voice_codec);
1662 break;
1663 }
1664 }
1665
1666 if (!send_codec_spec) {
1667 return false;
1668 }
1669
1670 RTC_DCHECK(voice_codec_info);
1671 if (voice_codec_info->allow_comfort_noise) {
1672 // Loop through the codecs list again to find the CN codec.
1673 // TODO(solenberg): Break out into a separate function?
1674 for (const AudioCodec& cn_codec : codecs) {
ossu0c4b8492017-03-02 11:03:25 -08001675 if (IsCodec(cn_codec, kCnCodecName) &&
ossu20a4b3f2017-04-27 02:08:52 -07001676 cn_codec.clockrate == send_codec_spec->format.clockrate_hz) {
ossu0c4b8492017-03-02 11:03:25 -08001677 switch (cn_codec.clockrate) {
solenberg72e29d22016-03-08 06:35:16 -08001678 case 8000:
1679 case 16000:
1680 case 32000:
ossu20a4b3f2017-04-27 02:08:52 -07001681 send_codec_spec->cng_payload_type = rtc::Optional<int>(cn_codec.id);
solenberg72e29d22016-03-08 06:35:16 -08001682 break;
1683 default:
Mirko Bonadei675513b2017-11-09 11:09:25 +01001684 RTC_LOG(LS_WARNING)
1685 << "CN frequency " << cn_codec.clockrate << " not supported.";
ossu20a4b3f2017-04-27 02:08:52 -07001686 break;
solenberg72e29d22016-03-08 06:35:16 -08001687 }
solenberg72e29d22016-03-08 06:35:16 -08001688 break;
1689 }
1690 }
solenbergffbbcac2016-11-17 05:25:37 -08001691
1692 // Find the telephone-event PT exactly matching the preferred send codec.
1693 for (const AudioCodec& dtmf_codec : dtmf_codecs) {
ossu20a4b3f2017-04-27 02:08:52 -07001694 if (dtmf_codec.clockrate == send_codec_spec->format.clockrate_hz) {
solenbergffbbcac2016-11-17 05:25:37 -08001695 dtmf_payload_type_ = rtc::Optional<int>(dtmf_codec.id);
1696 dtmf_payload_freq_ = dtmf_codec.clockrate;
1697 break;
1698 }
1699 }
solenberg72e29d22016-03-08 06:35:16 -08001700 }
1701
solenberg971cab02016-06-14 10:02:41 -07001702 if (send_codec_spec_ != send_codec_spec) {
1703 send_codec_spec_ = std::move(send_codec_spec);
stefan13f1a0a2016-11-30 07:22:58 -08001704 // Apply new settings to all streams.
solenberg971cab02016-06-14 10:02:41 -07001705 for (const auto& kv : send_streams_) {
ossu20a4b3f2017-04-27 02:08:52 -07001706 kv.second->SetSendCodecSpec(*send_codec_spec_);
wu@webrtc.orgcadf9042013-08-30 21:24:16 +00001707 }
stefan13f1a0a2016-11-30 07:22:58 -08001708 } else {
1709 // If the codec isn't changing, set the start bitrate to -1 which means
1710 // "unchanged" so that BWE isn't affected.
stefan1ccf73f2017-03-27 03:51:18 -07001711 bitrate_config.start_bitrate_bps = -1;
wu@webrtc.orgcadf9042013-08-30 21:24:16 +00001712 }
stefan1ccf73f2017-03-27 03:51:18 -07001713 call_->SetBitrateConfig(bitrate_config);
wu@webrtc.orgcadf9042013-08-30 21:24:16 +00001714
solenberg8189b022016-06-14 12:13:00 -07001715 // Check if the transport cc feedback or NACK status has changed on the
1716 // preferred send codec, and in that case reconfigure all receive streams.
ossu20a4b3f2017-04-27 02:08:52 -07001717 if (recv_transport_cc_enabled_ != send_codec_spec_->transport_cc_enabled ||
1718 recv_nack_enabled_ != send_codec_spec_->nack_enabled) {
Mirko Bonadei675513b2017-11-09 11:09:25 +01001719 RTC_LOG(LS_INFO) << "Recreate all the receive streams because the send "
1720 "codec has changed.";
ossu20a4b3f2017-04-27 02:08:52 -07001721 recv_transport_cc_enabled_ = send_codec_spec_->transport_cc_enabled;
1722 recv_nack_enabled_ = send_codec_spec_->nack_enabled;
solenberg72e29d22016-03-08 06:35:16 -08001723 for (auto& kv : recv_streams_) {
solenberg8189b022016-06-14 12:13:00 -07001724 kv.second->RecreateAudioReceiveStream(recv_transport_cc_enabled_,
1725 recv_nack_enabled_);
solenberg72e29d22016-03-08 06:35:16 -08001726 }
1727 }
1728
Taylor Brandstetter0cd086b2016-04-20 16:23:10 -07001729 send_codecs_ = codecs;
solenberg72e29d22016-03-08 06:35:16 -08001730 return true;
1731}
1732
aleloi84ef6152016-08-04 05:28:21 -07001733void WebRtcVoiceMediaChannel::SetPlayout(bool playout) {
kwiberg37b8b112016-11-03 02:46:53 -07001734 desired_playout_ = playout;
1735 return ChangePlayout(desired_playout_);
1736}
1737
1738void WebRtcVoiceMediaChannel::ChangePlayout(bool playout) {
1739 TRACE_EVENT0("webrtc", "WebRtcVoiceMediaChannel::ChangePlayout");
solenberg566ef242015-11-06 15:34:49 -08001740 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001741 if (playout_ == playout) {
aleloi84ef6152016-08-04 05:28:21 -07001742 return;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001743 }
1744
aleloi84ef6152016-08-04 05:28:21 -07001745 for (const auto& kv : recv_streams_) {
1746 kv.second->SetPlayout(playout);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001747 }
solenberg1ac56142015-10-13 03:58:19 -07001748 playout_ = playout;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001749}
1750
Taylor Brandstetter1a018dc2016-03-08 12:37:39 -08001751void WebRtcVoiceMediaChannel::SetSend(bool send) {
Peter Boströmca8b4042016-03-08 14:24:13 -08001752 TRACE_EVENT0("webrtc", "WebRtcVoiceMediaChannel::SetSend");
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001753 if (send_ == send) {
Taylor Brandstetter1a018dc2016-03-08 12:37:39 -08001754 return;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001755 }
1756
solenbergd53a3f92016-04-14 13:56:37 -07001757 // Apply channel specific options, and initialize the ADM for recording (this
1758 // may take time on some platforms, e.g. Android).
Taylor Brandstetter1a018dc2016-03-08 12:37:39 -08001759 if (send) {
solenberg63b34542015-09-29 06:06:31 -07001760 engine()->ApplyOptions(options_);
solenbergd53a3f92016-04-14 13:56:37 -07001761
1762 // InitRecording() may return an error if the ADM is already recording.
1763 if (!engine()->adm()->RecordingIsInitialized() &&
1764 !engine()->adm()->Recording()) {
1765 if (engine()->adm()->InitRecording() != 0) {
Mirko Bonadei675513b2017-11-09 11:09:25 +01001766 RTC_LOG(LS_WARNING) << "Failed to initialize recording";
solenbergd53a3f92016-04-14 13:56:37 -07001767 }
1768 }
solenberg63b34542015-09-29 06:06:31 -07001769 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001770
wu@webrtc.org9dba5252013-08-05 20:36:57 +00001771 // Change the settings on each send channel.
Taylor Brandstetter1a018dc2016-03-08 12:37:39 -08001772 for (auto& kv : send_streams_) {
1773 kv.second->SetSend(send);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001774 }
wu@webrtc.org9dba5252013-08-05 20:36:57 +00001775
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001776 send_ = send;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001777}
1778
Peter Boström0c4e06b2015-10-07 12:23:21 +02001779bool WebRtcVoiceMediaChannel::SetAudioSend(uint32_t ssrc,
1780 bool enable,
solenberg1dd98f32015-09-10 01:57:14 -07001781 const AudioOptions* options,
Taylor Brandstetter1a018dc2016-03-08 12:37:39 -08001782 AudioSource* source) {
solenberg566ef242015-11-06 15:34:49 -08001783 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
solenberg1dd98f32015-09-10 01:57:14 -07001784 // TODO(solenberg): The state change should be fully rolled back if any one of
1785 // these calls fail.
Taylor Brandstetter1a018dc2016-03-08 12:37:39 -08001786 if (!SetLocalSource(ssrc, source)) {
solenberg1dd98f32015-09-10 01:57:14 -07001787 return false;
1788 }
solenbergdfc8f4f2015-10-01 02:31:10 -07001789 if (!MuteStream(ssrc, !enable)) {
solenberg1dd98f32015-09-10 01:57:14 -07001790 return false;
1791 }
solenbergdfc8f4f2015-10-01 02:31:10 -07001792 if (enable && options) {
solenberg1dd98f32015-09-10 01:57:14 -07001793 return SetOptions(*options);
1794 }
1795 return true;
1796}
1797
solenberg0a617e22015-10-20 15:49:38 -07001798int WebRtcVoiceMediaChannel::CreateVoEChannel() {
1799 int id = engine()->CreateVoEChannel();
1800 if (id == -1) {
Mirko Bonadei675513b2017-11-09 11:09:25 +01001801 RTC_LOG(LS_WARNING) << "CreateVoEChannel() failed.";
solenberg0a617e22015-10-20 15:49:38 -07001802 return -1;
wu@webrtc.org9dba5252013-08-05 20:36:57 +00001803 }
mflodman3d7db262016-04-29 00:57:13 -07001804
solenberg0a617e22015-10-20 15:49:38 -07001805 return id;
wu@webrtc.org9dba5252013-08-05 20:36:57 +00001806}
1807
solenberg7add0582015-11-20 09:59:34 -08001808bool WebRtcVoiceMediaChannel::DeleteVoEChannel(int channel) {
wu@webrtc.org9dba5252013-08-05 20:36:57 +00001809 if (engine()->voe()->base()->DeleteChannel(channel) == -1) {
Mirko Bonadei675513b2017-11-09 11:09:25 +01001810 RTC_LOG(LS_WARNING) << "DeleteChannel(" << channel << ") failed.";
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001811 return false;
1812 }
wu@webrtc.org9dba5252013-08-05 20:36:57 +00001813 return true;
1814}
henrike@webrtc.org1e09a712013-07-26 19:17:59 +00001815
wu@webrtc.org9dba5252013-08-05 20:36:57 +00001816bool WebRtcVoiceMediaChannel::AddSendStream(const StreamParams& sp) {
Peter Boströmca8b4042016-03-08 14:24:13 -08001817 TRACE_EVENT0("webrtc", "WebRtcVoiceMediaChannel::AddSendStream");
solenberg566ef242015-11-06 15:34:49 -08001818 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
Mirko Bonadei675513b2017-11-09 11:09:25 +01001819 RTC_LOG(LS_INFO) << "AddSendStream: " << sp.ToString();
solenberg0a617e22015-10-20 15:49:38 -07001820
1821 uint32_t ssrc = sp.first_ssrc();
1822 RTC_DCHECK(0 != ssrc);
1823
1824 if (GetSendChannelId(ssrc) != -1) {
Mirko Bonadei675513b2017-11-09 11:09:25 +01001825 RTC_LOG(LS_ERROR) << "Stream already exists with ssrc " << ssrc;
wu@webrtc.org9dba5252013-08-05 20:36:57 +00001826 return false;
1827 }
1828
solenberg0a617e22015-10-20 15:49:38 -07001829 // Create a new channel for sending audio data.
1830 int channel = CreateVoEChannel();
1831 if (channel == -1) {
1832 return false;
wu@webrtc.org9dba5252013-08-05 20:36:57 +00001833 }
wu@webrtc.org9dba5252013-08-05 20:36:57 +00001834
solenbergc96df772015-10-21 13:01:53 -07001835 // Save the channel to send_streams_, so that RemoveSendStream() can still
wu@webrtc.org9dba5252013-08-05 20:36:57 +00001836 // delete the channel in case failure happens below.
mallinath@webrtc.org67ee6b92014-02-03 16:57:16 +00001837 webrtc::AudioTransport* audio_transport =
1838 engine()->voe()->base()->audio_transport();
mflodman3d7db262016-04-29 00:57:13 -07001839
minyue6b825df2016-10-31 04:08:32 -07001840 rtc::Optional<std::string> audio_network_adaptor_config =
1841 GetAudioNetworkAdaptorConfig(options_);
skvlade0d46372016-04-07 22:59:22 -07001842 WebRtcAudioSendStream* stream = new WebRtcAudioSendStream(
Alex Narestb3944f02017-10-13 14:56:18 +02001843 channel, audio_transport, ssrc, sp.cname, sp.id, send_codec_spec_,
minyue6b825df2016-10-31 04:08:32 -07001844 send_rtp_extensions_, max_send_bitrate_bps_, audio_network_adaptor_config,
ossu20a4b3f2017-04-27 02:08:52 -07001845 call_, this, engine()->encoder_factory_);
skvlade0d46372016-04-07 22:59:22 -07001846 send_streams_.insert(std::make_pair(ssrc, stream));
wu@webrtc.org9dba5252013-08-05 20:36:57 +00001847
solenberg4a0f7b52016-06-16 13:07:33 -07001848 // At this point the stream's local SSRC has been updated. If it is the first
1849 // send stream, make sure that all the receive streams are updated with the
1850 // same SSRC in order to send receiver reports.
solenbergc96df772015-10-21 13:01:53 -07001851 if (send_streams_.size() == 1) {
solenberg0a617e22015-10-20 15:49:38 -07001852 receiver_reports_ssrc_ = ssrc;
solenberg4a0f7b52016-06-16 13:07:33 -07001853 for (const auto& kv : recv_streams_) {
1854 // TODO(solenberg): Allow applications to set the RTCP SSRC of receive
1855 // streams instead, so we can avoid recreating the streams here.
1856 kv.second->RecreateAudioReceiveStream(ssrc);
wu@webrtc.org9dba5252013-08-05 20:36:57 +00001857 }
1858 }
1859
Taylor Brandstetter1a018dc2016-03-08 12:37:39 -08001860 send_streams_[ssrc]->SetSend(send_);
1861 return true;
wu@webrtc.org9dba5252013-08-05 20:36:57 +00001862}
1863
Peter Boström0c4e06b2015-10-07 12:23:21 +02001864bool WebRtcVoiceMediaChannel::RemoveSendStream(uint32_t ssrc) {
Peter Boströmca8b4042016-03-08 14:24:13 -08001865 TRACE_EVENT0("webrtc", "WebRtcVoiceMediaChannel::RemoveSendStream");
solenberg566ef242015-11-06 15:34:49 -08001866 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
Mirko Bonadei675513b2017-11-09 11:09:25 +01001867 RTC_LOG(LS_INFO) << "RemoveSendStream: " << ssrc;
solenberg3a941542015-11-16 07:34:50 -08001868
solenbergc96df772015-10-21 13:01:53 -07001869 auto it = send_streams_.find(ssrc);
1870 if (it == send_streams_.end()) {
Mirko Bonadei675513b2017-11-09 11:09:25 +01001871 RTC_LOG(LS_WARNING) << "Try to remove stream with ssrc " << ssrc
1872 << " which doesn't exist.";
wu@webrtc.org9dba5252013-08-05 20:36:57 +00001873 return false;
1874 }
1875
Taylor Brandstetter1a018dc2016-03-08 12:37:39 -08001876 it->second->SetSend(false);
wu@webrtc.org9dba5252013-08-05 20:36:57 +00001877
solenberg7602aab2016-11-14 11:30:07 -08001878 // TODO(solenberg): If we're removing the receiver_reports_ssrc_ stream, find
1879 // the first active send stream and use that instead, reassociating receive
1880 // streams.
1881
solenberg7add0582015-11-20 09:59:34 -08001882 // Clean up and delete the send stream+channel.
Taylor Brandstetter1a018dc2016-03-08 12:37:39 -08001883 int channel = it->second->channel();
Mirko Bonadei675513b2017-11-09 11:09:25 +01001884 RTC_LOG(LS_INFO) << "Removing audio send stream " << ssrc
1885 << " with VoiceEngine channel #" << channel << ".";
solenberg7add0582015-11-20 09:59:34 -08001886 delete it->second;
1887 send_streams_.erase(it);
1888 if (!DeleteVoEChannel(channel)) {
solenberg0a617e22015-10-20 15:49:38 -07001889 return false;
wu@webrtc.org9dba5252013-08-05 20:36:57 +00001890 }
solenbergc96df772015-10-21 13:01:53 -07001891 if (send_streams_.empty()) {
Taylor Brandstetter1a018dc2016-03-08 12:37:39 -08001892 SetSend(false);
solenberg0a617e22015-10-20 15:49:38 -07001893 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001894 return true;
1895}
1896
1897bool WebRtcVoiceMediaChannel::AddRecvStream(const StreamParams& sp) {
Peter Boströmca8b4042016-03-08 14:24:13 -08001898 TRACE_EVENT0("webrtc", "WebRtcVoiceMediaChannel::AddRecvStream");
solenberg566ef242015-11-06 15:34:49 -08001899 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
Mirko Bonadei675513b2017-11-09 11:09:25 +01001900 RTC_LOG(LS_INFO) << "AddRecvStream: " << sp.ToString();
solenbergd97ec302015-10-07 01:40:33 -07001901
solenberg0b675462015-10-09 01:37:09 -07001902 if (!ValidateStreamParams(sp)) {
wu@webrtc.org78187522013-10-07 23:32:02 +00001903 return false;
1904 }
1905
solenberg7add0582015-11-20 09:59:34 -08001906 const uint32_t ssrc = sp.first_ssrc();
solenberg0b675462015-10-09 01:37:09 -07001907 if (ssrc == 0) {
Mirko Bonadei675513b2017-11-09 11:09:25 +01001908 RTC_LOG(LS_WARNING) << "AddRecvStream with ssrc==0 is not supported.";
solenberg0b675462015-10-09 01:37:09 -07001909 return false;
1910 }
1911
solenberg2100c0b2017-03-01 11:29:29 -08001912 // If this stream was previously received unsignaled, we promote it, possibly
1913 // recreating the AudioReceiveStream, if sync_label has changed.
1914 if (MaybeDeregisterUnsignaledRecvStream(ssrc)) {
solenberg4904fb62017-02-17 12:01:14 -08001915 recv_streams_[ssrc]->MaybeRecreateAudioReceiveStream(sp.sync_label);
solenberg4904fb62017-02-17 12:01:14 -08001916 return true;
solenberg1ac56142015-10-13 03:58:19 -07001917 }
solenberg0b675462015-10-09 01:37:09 -07001918
solenberg7add0582015-11-20 09:59:34 -08001919 if (GetReceiveChannelId(ssrc) != -1) {
Mirko Bonadei675513b2017-11-09 11:09:25 +01001920 RTC_LOG(LS_ERROR) << "Stream already exists with ssrc " << ssrc;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001921 return false;
1922 }
Fredrik Solenberg4b60c732015-05-07 14:07:48 +02001923
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001924 // Create a new channel for receiving audio data.
solenberg7add0582015-11-20 09:59:34 -08001925 const int channel = CreateVoEChannel();
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001926 if (channel == -1) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001927 return false;
1928 }
Minyue2013aec2015-05-13 14:14:42 +02001929
stefanba4c0e42016-02-04 04:12:24 -08001930 recv_streams_.insert(std::make_pair(
kwiberg1c07c702017-03-27 07:15:49 -07001931 ssrc,
1932 new WebRtcAudioReceiveStream(
1933 channel, ssrc, receiver_reports_ssrc_, recv_transport_cc_enabled_,
1934 recv_nack_enabled_, sp.sync_label, recv_rtp_extensions_, call_, this,
1935 engine()->decoder_factory_, decoder_map_)));
aleloi84ef6152016-08-04 05:28:21 -07001936 recv_streams_[ssrc]->SetPlayout(playout_);
solenberg7add0582015-11-20 09:59:34 -08001937
solenberg1ac56142015-10-13 03:58:19 -07001938 return true;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001939}
1940
Peter Boström0c4e06b2015-10-07 12:23:21 +02001941bool WebRtcVoiceMediaChannel::RemoveRecvStream(uint32_t ssrc) {
Peter Boströmca8b4042016-03-08 14:24:13 -08001942 TRACE_EVENT0("webrtc", "WebRtcVoiceMediaChannel::RemoveRecvStream");
solenberg566ef242015-11-06 15:34:49 -08001943 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
Mirko Bonadei675513b2017-11-09 11:09:25 +01001944 RTC_LOG(LS_INFO) << "RemoveRecvStream: " << ssrc;
solenbergd97ec302015-10-07 01:40:33 -07001945
solenberg7add0582015-11-20 09:59:34 -08001946 const auto it = recv_streams_.find(ssrc);
1947 if (it == recv_streams_.end()) {
Mirko Bonadei675513b2017-11-09 11:09:25 +01001948 RTC_LOG(LS_WARNING) << "Try to remove stream with ssrc " << ssrc
1949 << " which doesn't exist.";
henrike@webrtc.org1e09a712013-07-26 19:17:59 +00001950 return false;
wu@webrtc.org9dba5252013-08-05 20:36:57 +00001951 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001952
solenberg2100c0b2017-03-01 11:29:29 -08001953 MaybeDeregisterUnsignaledRecvStream(ssrc);
henrike@webrtc.org1e09a712013-07-26 19:17:59 +00001954
solenberg7add0582015-11-20 09:59:34 -08001955 const int channel = it->second->channel();
1956
1957 // Clean up and delete the receive stream+channel.
Mirko Bonadei675513b2017-11-09 11:09:25 +01001958 RTC_LOG(LS_INFO) << "Removing audio receive stream " << ssrc
1959 << " with VoiceEngine channel #" << channel << ".";
Tommif888bb52015-12-12 01:37:01 +01001960 it->second->SetRawAudioSink(nullptr);
solenberg7add0582015-11-20 09:59:34 -08001961 delete it->second;
1962 recv_streams_.erase(it);
1963 return DeleteVoEChannel(channel);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001964}
1965
Taylor Brandstetter1a018dc2016-03-08 12:37:39 -08001966bool WebRtcVoiceMediaChannel::SetLocalSource(uint32_t ssrc,
1967 AudioSource* source) {
solenbergc96df772015-10-21 13:01:53 -07001968 auto it = send_streams_.find(ssrc);
1969 if (it == send_streams_.end()) {
Taylor Brandstetter1a018dc2016-03-08 12:37:39 -08001970 if (source) {
1971 // Return an error if trying to set a valid source with an invalid ssrc.
Mirko Bonadei675513b2017-11-09 11:09:25 +01001972 RTC_LOG(LS_ERROR) << "SetLocalSource failed with ssrc " << ssrc;
wu@webrtc.org9dba5252013-08-05 20:36:57 +00001973 return false;
1974 }
1975
1976 // The channel likely has gone away, do nothing.
henrike@webrtc.org1e09a712013-07-26 19:17:59 +00001977 return true;
henrike@webrtc.org1e09a712013-07-26 19:17:59 +00001978 }
1979
Taylor Brandstetter1a018dc2016-03-08 12:37:39 -08001980 if (source) {
1981 it->second->SetSource(source);
solenberg1ac56142015-10-13 03:58:19 -07001982 } else {
Taylor Brandstetter1a018dc2016-03-08 12:37:39 -08001983 it->second->ClearSource();
solenberg1ac56142015-10-13 03:58:19 -07001984 }
henrike@webrtc.org1e09a712013-07-26 19:17:59 +00001985
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001986 return true;
1987}
1988
solenberg796b8f92017-03-01 17:02:23 -08001989// TODO(solenberg): Remove, once AudioMonitor is gone.
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001990bool WebRtcVoiceMediaChannel::GetActiveStreams(
1991 AudioInfo::StreamList* actives) {
solenberg566ef242015-11-06 15:34:49 -08001992 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001993 actives->clear();
solenberg7add0582015-11-20 09:59:34 -08001994 for (const auto& ch : recv_streams_) {
solenberg796b8f92017-03-01 17:02:23 -08001995 int level = ch.second->GetOutputLevel();
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001996 if (level > 0) {
Fredrik Solenbergaf9fb212015-08-26 10:45:53 +02001997 actives->push_back(std::make_pair(ch.first, level));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001998 }
1999 }
2000 return true;
2001}
2002
solenberg796b8f92017-03-01 17:02:23 -08002003// TODO(solenberg): Remove, once AudioMonitor is gone.
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002004int WebRtcVoiceMediaChannel::GetOutputLevel() {
solenberg566ef242015-11-06 15:34:49 -08002005 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
solenberg1ac56142015-10-13 03:58:19 -07002006 int highest = 0;
solenberg7add0582015-11-20 09:59:34 -08002007 for (const auto& ch : recv_streams_) {
solenberg796b8f92017-03-01 17:02:23 -08002008 highest = std::max(ch.second->GetOutputLevel(), highest);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002009 }
2010 return highest;
2011}
2012
solenberg4bac9c52015-10-09 02:32:53 -07002013bool WebRtcVoiceMediaChannel::SetOutputVolume(uint32_t ssrc, double volume) {
solenberg566ef242015-11-06 15:34:49 -08002014 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
solenberg2100c0b2017-03-01 11:29:29 -08002015 std::vector<uint32_t> ssrcs(1, ssrc);
deadbeef3bc15102017-04-20 19:25:07 -07002016 // SSRC of 0 represents the default receive stream.
solenberg1ac56142015-10-13 03:58:19 -07002017 if (ssrc == 0) {
2018 default_recv_volume_ = volume;
solenberg2100c0b2017-03-01 11:29:29 -08002019 ssrcs = unsignaled_recv_ssrcs_;
2020 }
2021 for (uint32_t ssrc : ssrcs) {
2022 const auto it = recv_streams_.find(ssrc);
2023 if (it == recv_streams_.end()) {
Mirko Bonadei675513b2017-11-09 11:09:25 +01002024 RTC_LOG(LS_WARNING) << "SetOutputVolume: no recv stream " << ssrc;
solenberg2100c0b2017-03-01 11:29:29 -08002025 return false;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002026 }
solenberg2100c0b2017-03-01 11:29:29 -08002027 it->second->SetOutputVolume(volume);
Mirko Bonadei675513b2017-11-09 11:09:25 +01002028 RTC_LOG(LS_INFO) << "SetOutputVolume() to " << volume
2029 << " for recv stream with ssrc " << ssrc;
solenberg1ac56142015-10-13 03:58:19 -07002030 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002031 return true;
2032}
2033
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002034bool WebRtcVoiceMediaChannel::CanInsertDtmf() {
Fredrik Solenbergb5727682015-12-04 15:22:19 +01002035 return dtmf_payload_type_ ? true : false;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002036}
2037
solenberg1d63dd02015-12-02 12:35:09 -08002038bool WebRtcVoiceMediaChannel::InsertDtmf(uint32_t ssrc, int event,
2039 int duration) {
solenberg566ef242015-11-06 15:34:49 -08002040 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
Mirko Bonadei675513b2017-11-09 11:09:25 +01002041 RTC_LOG(LS_INFO) << "WebRtcVoiceMediaChannel::InsertDtmf";
Fredrik Solenbergb5727682015-12-04 15:22:19 +01002042 if (!dtmf_payload_type_) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002043 return false;
2044 }
2045
Fredrik Solenbergb5727682015-12-04 15:22:19 +01002046 // Figure out which WebRtcAudioSendStream to send the event on.
2047 auto it = ssrc != 0 ? send_streams_.find(ssrc) : send_streams_.begin();
2048 if (it == send_streams_.end()) {
Mirko Bonadei675513b2017-11-09 11:09:25 +01002049 RTC_LOG(LS_WARNING) << "The specified ssrc " << ssrc << " is not in use.";
solenberg1d63dd02015-12-02 12:35:09 -08002050 return false;
2051 }
Fredrik Solenbergb5727682015-12-04 15:22:19 +01002052 if (event < kMinTelephoneEventCode ||
2053 event > kMaxTelephoneEventCode) {
Mirko Bonadei675513b2017-11-09 11:09:25 +01002054 RTC_LOG(LS_WARNING) << "DTMF event code " << event << " out of range.";
solenberg1d63dd02015-12-02 12:35:09 -08002055 return false;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002056 }
solenbergffbbcac2016-11-17 05:25:37 -08002057 RTC_DCHECK_NE(-1, dtmf_payload_freq_);
2058 return it->second->SendTelephoneEvent(*dtmf_payload_type_, dtmf_payload_freq_,
2059 event, duration);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002060}
2061
wu@webrtc.orga9890802013-12-13 00:21:03 +00002062void WebRtcVoiceMediaChannel::OnPacketReceived(
jbaucheec21bd2016-03-20 06:15:43 -07002063 rtc::CopyOnWriteBuffer* packet, const rtc::PacketTime& packet_time) {
solenberg566ef242015-11-06 15:34:49 -08002064 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
Fredrik Solenberg4b60c732015-05-07 14:07:48 +02002065
mflodman3d7db262016-04-29 00:57:13 -07002066 const webrtc::PacketTime webrtc_packet_time(packet_time.timestamp,
2067 packet_time.not_before);
2068 webrtc::PacketReceiver::DeliveryStatus delivery_result =
2069 call_->Receiver()->DeliverPacket(webrtc::MediaType::AUDIO,
2070 packet->cdata(), packet->size(),
2071 webrtc_packet_time);
mflodman3d7db262016-04-29 00:57:13 -07002072 if (delivery_result != webrtc::PacketReceiver::DELIVERY_UNKNOWN_SSRC) {
2073 return;
2074 }
2075
solenberg2100c0b2017-03-01 11:29:29 -08002076 // Create an unsignaled receive stream for this previously not received ssrc.
2077 // If there already is N unsignaled receive streams, delete the oldest.
mflodman3d7db262016-04-29 00:57:13 -07002078 // See: https://bugs.chromium.org/p/webrtc/issues/detail?id=5208
solenberg1ac56142015-10-13 03:58:19 -07002079 uint32_t ssrc = 0;
jbaucheec21bd2016-03-20 06:15:43 -07002080 if (!GetRtpSsrc(packet->cdata(), packet->size(), &ssrc)) {
solenberg1ac56142015-10-13 03:58:19 -07002081 return;
2082 }
solenberg2100c0b2017-03-01 11:29:29 -08002083 RTC_DCHECK(std::find(unsignaled_recv_ssrcs_.begin(),
2084 unsignaled_recv_ssrcs_.end(), ssrc) == unsignaled_recv_ssrcs_.end());
solenberg1ac56142015-10-13 03:58:19 -07002085
solenberg2100c0b2017-03-01 11:29:29 -08002086 // Add new stream.
mflodman3d7db262016-04-29 00:57:13 -07002087 StreamParams sp;
2088 sp.ssrcs.push_back(ssrc);
Mirko Bonadei675513b2017-11-09 11:09:25 +01002089 RTC_LOG(LS_INFO) << "Creating unsignaled receive stream for SSRC=" << ssrc;
mflodman3d7db262016-04-29 00:57:13 -07002090 if (!AddRecvStream(sp)) {
Mirko Bonadei675513b2017-11-09 11:09:25 +01002091 RTC_LOG(LS_WARNING) << "Could not create unsignaled receive stream.";
mflodman3d7db262016-04-29 00:57:13 -07002092 return;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002093 }
solenberg2100c0b2017-03-01 11:29:29 -08002094 unsignaled_recv_ssrcs_.push_back(ssrc);
2095 RTC_HISTOGRAM_COUNTS_LINEAR(
2096 "WebRTC.Audio.NumOfUnsignaledStreams", unsignaled_recv_ssrcs_.size(), 1,
2097 100, 101);
solenbergf748ca42017-02-06 13:03:19 -08002098
solenberg2100c0b2017-03-01 11:29:29 -08002099 // Remove oldest unsignaled stream, if we have too many.
2100 if (unsignaled_recv_ssrcs_.size() > kMaxUnsignaledRecvStreams) {
2101 uint32_t remove_ssrc = unsignaled_recv_ssrcs_.front();
Mirko Bonadei675513b2017-11-09 11:09:25 +01002102 RTC_LOG(LS_INFO) << "Removing unsignaled receive stream with SSRC="
2103 << remove_ssrc;
solenberg2100c0b2017-03-01 11:29:29 -08002104 RemoveRecvStream(remove_ssrc);
2105 }
2106 RTC_DCHECK_GE(kMaxUnsignaledRecvStreams, unsignaled_recv_ssrcs_.size());
2107
2108 SetOutputVolume(ssrc, default_recv_volume_);
2109
2110 // The default sink can only be attached to one stream at a time, so we hook
2111 // it up to the *latest* unsignaled stream we've seen, in order to support the
2112 // case where the SSRC of one unsignaled stream changes.
mflodman3d7db262016-04-29 00:57:13 -07002113 if (default_sink_) {
solenberg2100c0b2017-03-01 11:29:29 -08002114 for (uint32_t drop_ssrc : unsignaled_recv_ssrcs_) {
2115 auto it = recv_streams_.find(drop_ssrc);
2116 it->second->SetRawAudioSink(nullptr);
2117 }
mflodman3d7db262016-04-29 00:57:13 -07002118 std::unique_ptr<webrtc::AudioSinkInterface> proxy_sink(
2119 new ProxySink(default_sink_.get()));
solenberg2100c0b2017-03-01 11:29:29 -08002120 SetRawAudioSink(ssrc, std::move(proxy_sink));
mflodman3d7db262016-04-29 00:57:13 -07002121 }
solenberg2100c0b2017-03-01 11:29:29 -08002122
mflodman3d7db262016-04-29 00:57:13 -07002123 delivery_result = call_->Receiver()->DeliverPacket(webrtc::MediaType::AUDIO,
2124 packet->cdata(),
2125 packet->size(),
2126 webrtc_packet_time);
2127 RTC_DCHECK_NE(webrtc::PacketReceiver::DELIVERY_UNKNOWN_SSRC, delivery_result);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002128}
2129
wu@webrtc.orga9890802013-12-13 00:21:03 +00002130void WebRtcVoiceMediaChannel::OnRtcpReceived(
jbaucheec21bd2016-03-20 06:15:43 -07002131 rtc::CopyOnWriteBuffer* packet, const rtc::PacketTime& packet_time) {
solenberg566ef242015-11-06 15:34:49 -08002132 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
Fredrik Solenberg4b60c732015-05-07 14:07:48 +02002133
Fredrik Solenberg709ed672015-09-15 12:26:33 +02002134 // Forward packet to Call as well.
2135 const webrtc::PacketTime webrtc_packet_time(packet_time.timestamp,
2136 packet_time.not_before);
2137 call_->Receiver()->DeliverPacket(webrtc::MediaType::AUDIO,
jbaucheec21bd2016-03-20 06:15:43 -07002138 packet->cdata(), packet->size(), webrtc_packet_time);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002139}
2140
Honghai Zhangcc411c02016-03-29 17:27:21 -07002141void WebRtcVoiceMediaChannel::OnNetworkRouteChanged(
2142 const std::string& transport_name,
Honghai Zhang0e533ef2016-04-19 15:41:36 -07002143 const rtc::NetworkRoute& network_route) {
Zhi Huang5f5918f2017-11-12 17:26:23 -08002144 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
2145 // TODO(zhihaung): Merge these two callbacks.
Honghai Zhang0e533ef2016-04-19 15:41:36 -07002146 call_->OnNetworkRouteChanged(transport_name, network_route);
Zhi Huang5f5918f2017-11-12 17:26:23 -08002147 call_->OnTransportOverheadChanged(webrtc::MediaType::AUDIO,
2148 network_route.packet_overhead);
Honghai Zhangcc411c02016-03-29 17:27:21 -07002149}
2150
Peter Boström0c4e06b2015-10-07 12:23:21 +02002151bool WebRtcVoiceMediaChannel::MuteStream(uint32_t ssrc, bool muted) {
solenberg566ef242015-11-06 15:34:49 -08002152 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
solenberg94218532016-06-16 10:53:22 -07002153 const auto it = send_streams_.find(ssrc);
2154 if (it == send_streams_.end()) {
Mirko Bonadei675513b2017-11-09 11:09:25 +01002155 RTC_LOG(LS_WARNING) << "The specified ssrc " << ssrc << " is not in use.";
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002156 return false;
2157 }
solenberg94218532016-06-16 10:53:22 -07002158 it->second->SetMuted(muted);
2159
2160 // TODO(solenberg):
buildbot@webrtc.org6b21b712014-07-31 15:08:53 +00002161 // We set the AGC to mute state only when all the channels are muted.
2162 // This implementation is not ideal, instead we should signal the AGC when
2163 // the mic channel is muted/unmuted. We can't do it today because there
2164 // is no good way to know which stream is mapping to the mic channel.
2165 bool all_muted = muted;
solenberg94218532016-06-16 10:53:22 -07002166 for (const auto& kv : send_streams_) {
2167 all_muted = all_muted && kv.second->muted();
buildbot@webrtc.org6b21b712014-07-31 15:08:53 +00002168 }
solenberg059fb442016-10-26 05:12:24 -07002169 engine()->apm()->set_output_will_be_muted(all_muted);
buildbot@webrtc.org6b21b712014-07-31 15:08:53 +00002170
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002171 return true;
2172}
2173
deadbeef80346142016-04-27 14:17:10 -07002174bool WebRtcVoiceMediaChannel::SetMaxSendBitrate(int bps) {
Mirko Bonadei675513b2017-11-09 11:09:25 +01002175 RTC_LOG(LS_INFO) << "WebRtcVoiceMediaChannel::SetMaxSendBitrate.";
deadbeef80346142016-04-27 14:17:10 -07002176 max_send_bitrate_bps_ = bps;
minyue7a973442016-10-20 03:27:12 -07002177 bool success = true;
skvlade0d46372016-04-07 22:59:22 -07002178 for (const auto& kv : send_streams_) {
minyue7a973442016-10-20 03:27:12 -07002179 if (!kv.second->SetMaxSendBitrate(max_send_bitrate_bps_)) {
2180 success = false;
skvlade0d46372016-04-07 22:59:22 -07002181 }
2182 }
minyue7a973442016-10-20 03:27:12 -07002183 return success;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002184}
2185
skvlad7a43d252016-03-22 15:32:27 -07002186void WebRtcVoiceMediaChannel::OnReadyToSend(bool ready) {
2187 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
Mirko Bonadei675513b2017-11-09 11:09:25 +01002188 RTC_LOG(LS_VERBOSE) << "OnReadyToSend: " << (ready ? "Ready." : "Not ready.");
skvlad7a43d252016-03-22 15:32:27 -07002189 call_->SignalChannelNetworkState(
2190 webrtc::MediaType::AUDIO,
2191 ready ? webrtc::kNetworkUp : webrtc::kNetworkDown);
2192}
2193
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002194bool WebRtcVoiceMediaChannel::GetStats(VoiceMediaInfo* info) {
Peter Boströmca8b4042016-03-08 14:24:13 -08002195 TRACE_EVENT0("webrtc", "WebRtcVoiceMediaChannel::GetStats");
solenberg566ef242015-11-06 15:34:49 -08002196 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
solenberg85a04962015-10-27 03:35:21 -07002197 RTC_DCHECK(info);
solenbergd97ec302015-10-07 01:40:33 -07002198
solenberg85a04962015-10-27 03:35:21 -07002199 // Get SSRC and stats for each sender.
hbos1acfbd22016-11-17 23:43:29 -08002200 RTC_DCHECK_EQ(info->senders.size(), 0U);
solenberg85a04962015-10-27 03:35:21 -07002201 for (const auto& stream : send_streams_) {
2202 webrtc::AudioSendStream::Stats stats = stream.second->GetStats();
wu@webrtc.org9dba5252013-08-05 20:36:57 +00002203 VoiceSenderInfo sinfo;
solenberg85a04962015-10-27 03:35:21 -07002204 sinfo.add_ssrc(stats.local_ssrc);
2205 sinfo.bytes_sent = stats.bytes_sent;
2206 sinfo.packets_sent = stats.packets_sent;
2207 sinfo.packets_lost = stats.packets_lost;
2208 sinfo.fraction_lost = stats.fraction_lost;
2209 sinfo.codec_name = stats.codec_name;
hbos1acfbd22016-11-17 23:43:29 -08002210 sinfo.codec_payload_type = stats.codec_payload_type;
solenberg85a04962015-10-27 03:35:21 -07002211 sinfo.ext_seqnum = stats.ext_seqnum;
2212 sinfo.jitter_ms = stats.jitter_ms;
2213 sinfo.rtt_ms = stats.rtt_ms;
2214 sinfo.audio_level = stats.audio_level;
zsteine76bd3a2017-07-14 12:17:49 -07002215 sinfo.total_input_energy = stats.total_input_energy;
2216 sinfo.total_input_duration = stats.total_input_duration;
solenberg85a04962015-10-27 03:35:21 -07002217 sinfo.aec_quality_min = stats.aec_quality_min;
2218 sinfo.echo_delay_median_ms = stats.echo_delay_median_ms;
2219 sinfo.echo_delay_std_ms = stats.echo_delay_std_ms;
2220 sinfo.echo_return_loss = stats.echo_return_loss;
2221 sinfo.echo_return_loss_enhancement = stats.echo_return_loss_enhancement;
ivoc8c63a822016-10-21 04:10:03 -07002222 sinfo.residual_echo_likelihood = stats.residual_echo_likelihood;
ivoc4e477a12017-01-15 08:29:46 -08002223 sinfo.residual_echo_likelihood_recent_max =
2224 stats.residual_echo_likelihood_recent_max;
Taylor Brandstetter1a018dc2016-03-08 12:37:39 -08002225 sinfo.typing_noise_detected = (send_ ? stats.typing_noise_detected : false);
ivoce1198e02017-09-08 08:13:19 -07002226 sinfo.ana_statistics = stats.ana_statistics;
wu@webrtc.org9dba5252013-08-05 20:36:57 +00002227 info->senders.push_back(sinfo);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002228 }
2229
solenberg85a04962015-10-27 03:35:21 -07002230 // Get SSRC and stats for each receiver.
hbos1acfbd22016-11-17 23:43:29 -08002231 RTC_DCHECK_EQ(info->receivers.size(), 0U);
solenberg7add0582015-11-20 09:59:34 -08002232 for (const auto& stream : recv_streams_) {
deadbeef4e2deab2017-09-20 13:56:21 -07002233 uint32_t ssrc = stream.first;
2234 // When SSRCs are unsignaled, there's only one audio MediaStreamTrack, but
2235 // multiple RTP streams can be received over time (if the SSRC changes for
2236 // whatever reason). We only want the RTCMediaStreamTrackStats to represent
2237 // the stats for the most recent stream (the one whose audio is actually
2238 // routed to the MediaStreamTrack), so here we ignore any unsignaled SSRCs
2239 // except for the most recent one (last in the vector). This is somewhat of
2240 // a hack, and means you don't get *any* stats for these inactive streams,
2241 // but it's slightly better than the previous behavior, which was "highest
2242 // SSRC wins".
2243 // See: https://bugs.chromium.org/p/webrtc/issues/detail?id=8158
2244 if (!unsignaled_recv_ssrcs_.empty()) {
2245 auto end_it = --unsignaled_recv_ssrcs_.end();
2246 if (std::find(unsignaled_recv_ssrcs_.begin(), end_it, ssrc) != end_it) {
2247 continue;
2248 }
2249 }
Fredrik Solenberg4f4ec0a2015-10-22 10:49:27 +02002250 webrtc::AudioReceiveStream::Stats stats = stream.second->GetStats();
2251 VoiceReceiverInfo rinfo;
2252 rinfo.add_ssrc(stats.remote_ssrc);
2253 rinfo.bytes_rcvd = stats.bytes_rcvd;
2254 rinfo.packets_rcvd = stats.packets_rcvd;
2255 rinfo.packets_lost = stats.packets_lost;
2256 rinfo.fraction_lost = stats.fraction_lost;
2257 rinfo.codec_name = stats.codec_name;
hbos1acfbd22016-11-17 23:43:29 -08002258 rinfo.codec_payload_type = stats.codec_payload_type;
Fredrik Solenberg4f4ec0a2015-10-22 10:49:27 +02002259 rinfo.ext_seqnum = stats.ext_seqnum;
2260 rinfo.jitter_ms = stats.jitter_ms;
2261 rinfo.jitter_buffer_ms = stats.jitter_buffer_ms;
2262 rinfo.jitter_buffer_preferred_ms = stats.jitter_buffer_preferred_ms;
2263 rinfo.delay_estimate_ms = stats.delay_estimate_ms;
2264 rinfo.audio_level = stats.audio_level;
zsteine76bd3a2017-07-14 12:17:49 -07002265 rinfo.total_output_energy = stats.total_output_energy;
Steve Anton2dbc69f2017-08-24 17:15:13 -07002266 rinfo.total_samples_received = stats.total_samples_received;
zsteine76bd3a2017-07-14 12:17:49 -07002267 rinfo.total_output_duration = stats.total_output_duration;
Steve Anton2dbc69f2017-08-24 17:15:13 -07002268 rinfo.concealed_samples = stats.concealed_samples;
Gustaf Ullberg9a2e9062017-09-18 09:28:20 +02002269 rinfo.concealment_events = stats.concealment_events;
Gustaf Ullbergb0a02072017-10-02 12:00:34 +02002270 rinfo.jitter_buffer_delay_seconds = stats.jitter_buffer_delay_seconds;
Fredrik Solenberg4f4ec0a2015-10-22 10:49:27 +02002271 rinfo.expand_rate = stats.expand_rate;
2272 rinfo.speech_expand_rate = stats.speech_expand_rate;
2273 rinfo.secondary_decoded_rate = stats.secondary_decoded_rate;
minyue-webrtc0e320ec2017-08-28 13:51:27 +02002274 rinfo.secondary_discarded_rate = stats.secondary_discarded_rate;
Fredrik Solenberg4f4ec0a2015-10-22 10:49:27 +02002275 rinfo.accelerate_rate = stats.accelerate_rate;
2276 rinfo.preemptive_expand_rate = stats.preemptive_expand_rate;
2277 rinfo.decoding_calls_to_silence_generator =
2278 stats.decoding_calls_to_silence_generator;
2279 rinfo.decoding_calls_to_neteq = stats.decoding_calls_to_neteq;
2280 rinfo.decoding_normal = stats.decoding_normal;
2281 rinfo.decoding_plc = stats.decoding_plc;
2282 rinfo.decoding_cng = stats.decoding_cng;
2283 rinfo.decoding_plc_cng = stats.decoding_plc_cng;
henrik.lundin63489782016-09-20 01:47:12 -07002284 rinfo.decoding_muted_output = stats.decoding_muted_output;
Fredrik Solenberg4f4ec0a2015-10-22 10:49:27 +02002285 rinfo.capture_start_ntp_time_ms = stats.capture_start_ntp_time_ms;
2286 info->receivers.push_back(rinfo);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002287 }
2288
hbos1acfbd22016-11-17 23:43:29 -08002289 // Get codec info
2290 for (const AudioCodec& codec : send_codecs_) {
2291 webrtc::RtpCodecParameters codec_params = codec.ToCodecParameters();
2292 info->send_codecs.insert(
2293 std::make_pair(codec_params.payload_type, std::move(codec_params)));
2294 }
2295 for (const AudioCodec& codec : recv_codecs_) {
2296 webrtc::RtpCodecParameters codec_params = codec.ToCodecParameters();
2297 info->receive_codecs.insert(
2298 std::make_pair(codec_params.payload_type, std::move(codec_params)));
2299 }
2300
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002301 return true;
2302}
2303
Tommif888bb52015-12-12 01:37:01 +01002304void WebRtcVoiceMediaChannel::SetRawAudioSink(
2305 uint32_t ssrc,
kwiberg686a8ef2016-02-26 03:00:35 -08002306 std::unique_ptr<webrtc::AudioSinkInterface> sink) {
Tommif888bb52015-12-12 01:37:01 +01002307 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
Mirko Bonadei675513b2017-11-09 11:09:25 +01002308 RTC_LOG(LS_VERBOSE) << "WebRtcVoiceMediaChannel::SetRawAudioSink: ssrc:"
2309 << ssrc << " " << (sink ? "(ptr)" : "NULL");
deadbeef884f5852016-01-15 09:20:04 -08002310 if (ssrc == 0) {
solenberg2100c0b2017-03-01 11:29:29 -08002311 if (!unsignaled_recv_ssrcs_.empty()) {
kwiberg686a8ef2016-02-26 03:00:35 -08002312 std::unique_ptr<webrtc::AudioSinkInterface> proxy_sink(
deadbeef884f5852016-01-15 09:20:04 -08002313 sink ? new ProxySink(sink.get()) : nullptr);
solenberg2100c0b2017-03-01 11:29:29 -08002314 SetRawAudioSink(unsignaled_recv_ssrcs_.back(), std::move(proxy_sink));
deadbeef884f5852016-01-15 09:20:04 -08002315 }
2316 default_sink_ = std::move(sink);
2317 return;
2318 }
Tommif888bb52015-12-12 01:37:01 +01002319 const auto it = recv_streams_.find(ssrc);
2320 if (it == recv_streams_.end()) {
Mirko Bonadei675513b2017-11-09 11:09:25 +01002321 RTC_LOG(LS_WARNING) << "SetRawAudioSink: no recv stream " << ssrc;
Tommif888bb52015-12-12 01:37:01 +01002322 return;
2323 }
deadbeef2d110be2016-01-13 12:00:26 -08002324 it->second->SetRawAudioSink(std::move(sink));
Tommif888bb52015-12-12 01:37:01 +01002325}
2326
hbos8d609f62017-04-10 07:39:05 -07002327std::vector<webrtc::RtpSource> WebRtcVoiceMediaChannel::GetSources(
2328 uint32_t ssrc) const {
2329 auto it = recv_streams_.find(ssrc);
2330 RTC_DCHECK(it != recv_streams_.end())
2331 << "Attempting to get contributing sources for SSRC:" << ssrc
2332 << " which doesn't exist.";
2333 return it->second->GetSources();
2334}
2335
Peter Boström0c4e06b2015-10-07 12:23:21 +02002336int WebRtcVoiceMediaChannel::GetReceiveChannelId(uint32_t ssrc) const {
solenberg566ef242015-11-06 15:34:49 -08002337 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
solenberg7add0582015-11-20 09:59:34 -08002338 const auto it = recv_streams_.find(ssrc);
2339 if (it != recv_streams_.end()) {
mallinath@webrtc.org67ee6b92014-02-03 16:57:16 +00002340 return it->second->channel();
solenberg8fb30c32015-10-13 03:06:58 -07002341 }
solenberg1ac56142015-10-13 03:58:19 -07002342 return -1;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002343}
2344
Peter Boström0c4e06b2015-10-07 12:23:21 +02002345int WebRtcVoiceMediaChannel::GetSendChannelId(uint32_t ssrc) const {
solenberg566ef242015-11-06 15:34:49 -08002346 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
solenbergc96df772015-10-21 13:01:53 -07002347 const auto it = send_streams_.find(ssrc);
2348 if (it != send_streams_.end()) {
mallinath@webrtc.org67ee6b92014-02-03 16:57:16 +00002349 return it->second->channel();
solenberg8fb30c32015-10-13 03:06:58 -07002350 }
wu@webrtc.org9dba5252013-08-05 20:36:57 +00002351 return -1;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002352}
solenberg2100c0b2017-03-01 11:29:29 -08002353
2354bool WebRtcVoiceMediaChannel::
2355 MaybeDeregisterUnsignaledRecvStream(uint32_t ssrc) {
2356 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
2357 auto it = std::find(unsignaled_recv_ssrcs_.begin(),
2358 unsignaled_recv_ssrcs_.end(),
2359 ssrc);
2360 if (it != unsignaled_recv_ssrcs_.end()) {
2361 unsignaled_recv_ssrcs_.erase(it);
2362 return true;
2363 }
2364 return false;
2365}
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002366} // namespace cricket
2367
2368#endif // HAVE_WEBRTC_VOICE