blob: 1b3cde6a0fe0e6116f5916ad07163d0a47579bd1 [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.org1f8a2372014-08-28 10:52:44 +0000628
kwiberg102c6a62015-10-30 02:47:38 -0700629 if (options.recording_sample_rate) {
Mirko Bonadei675513b2017-11-09 11:09:25 +0100630 RTC_LOG(LS_INFO) << "Recording sample rate is "
631 << *options.recording_sample_rate;
solenberg5b5129a2016-04-08 05:35:48 -0700632 if (adm()->SetRecordingSampleRate(*options.recording_sample_rate)) {
Mirko Bonadei675513b2017-11-09 11:09:25 +0100633 RTC_LOG(LS_WARNING) << "SetRecordingSampleRate("
634 << *options.recording_sample_rate << ") failed.";
buildbot@webrtc.org13d67762014-05-02 17:33:29 +0000635 }
636 }
637
kwiberg102c6a62015-10-30 02:47:38 -0700638 if (options.playout_sample_rate) {
Mirko Bonadei675513b2017-11-09 11:09:25 +0100639 RTC_LOG(LS_INFO) << "Playout sample rate is "
640 << *options.playout_sample_rate;
solenberg5b5129a2016-04-08 05:35:48 -0700641 if (adm()->SetPlayoutSampleRate(*options.playout_sample_rate)) {
Mirko Bonadei675513b2017-11-09 11:09:25 +0100642 RTC_LOG(LS_WARNING) << "SetPlayoutSampleRate("
643 << *options.playout_sample_rate << ") failed.";
buildbot@webrtc.org13d67762014-05-02 17:33:29 +0000644 }
645 }
buildbot@webrtc.org13d67762014-05-02 17:33:29 +0000646 return true;
647}
648
solenberg796b8f92017-03-01 17:02:23 -0800649// TODO(solenberg): Remove, once AudioMonitor is gone.
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000650int WebRtcVoiceEngine::GetInputLevel() {
solenberg566ef242015-11-06 15:34:49 -0800651 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
solenberg796b8f92017-03-01 17:02:23 -0800652 int8_t level = transmit_mixer()->AudioLevel();
653 RTC_DCHECK_LE(0, level);
654 return level;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000655}
656
ossudedfd282016-06-14 07:12:39 -0700657const std::vector<AudioCodec>& WebRtcVoiceEngine::send_codecs() const {
658 RTC_DCHECK(signal_thread_checker_.CalledOnValidThread());
ossuc54071d2016-08-17 02:45:41 -0700659 return send_codecs_;
ossudedfd282016-06-14 07:12:39 -0700660}
661
662const std::vector<AudioCodec>& WebRtcVoiceEngine::recv_codecs() const {
solenberg566ef242015-11-06 15:34:49 -0800663 RTC_DCHECK(signal_thread_checker_.CalledOnValidThread());
ossuc54071d2016-08-17 02:45:41 -0700664 return recv_codecs_;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000665}
666
Stefan Holmer9d69c3f2015-12-07 10:45:43 +0100667RtpCapabilities WebRtcVoiceEngine::GetCapabilities() const {
solenberg566ef242015-11-06 15:34:49 -0800668 RTC_DCHECK(signal_thread_checker_.CalledOnValidThread());
Stefan Holmer9d69c3f2015-12-07 10:45:43 +0100669 RtpCapabilities capabilities;
Stefan Holmer9d69c3f2015-12-07 10:45:43 +0100670 capabilities.header_extensions.push_back(
isheriff6f8d6862016-05-26 11:24:55 -0700671 webrtc::RtpExtension(webrtc::RtpExtension::kAudioLevelUri,
672 webrtc::RtpExtension::kAudioLevelDefaultId));
sprangc1b57a12017-02-28 08:50:47 -0800673 if (webrtc::field_trial::IsEnabled("WebRTC-Audio-SendSideBwe")) {
isheriff6f8d6862016-05-26 11:24:55 -0700674 capabilities.header_extensions.push_back(webrtc::RtpExtension(
675 webrtc::RtpExtension::kTransportSequenceNumberUri,
676 webrtc::RtpExtension::kTransportSequenceNumberDefaultId));
stefanba4c0e42016-02-04 04:12:24 -0800677 }
Stefan Holmer9d69c3f2015-12-07 10:45:43 +0100678 return capabilities;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000679}
680
solenberg63b34542015-09-29 06:06:31 -0700681void WebRtcVoiceEngine::RegisterChannel(WebRtcVoiceMediaChannel* channel) {
solenberg566ef242015-11-06 15:34:49 -0800682 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
683 RTC_DCHECK(channel);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000684 channels_.push_back(channel);
685}
686
solenberg63b34542015-09-29 06:06:31 -0700687void WebRtcVoiceEngine::UnregisterChannel(WebRtcVoiceMediaChannel* channel) {
solenberg566ef242015-11-06 15:34:49 -0800688 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
solenberg63b34542015-09-29 06:06:31 -0700689 auto it = std::find(channels_.begin(), channels_.end(), channel);
solenberg566ef242015-11-06 15:34:49 -0800690 RTC_DCHECK(it != channels_.end());
691 channels_.erase(it);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000692}
693
ivocd66b44d2016-01-15 03:06:36 -0800694bool WebRtcVoiceEngine::StartAecDump(rtc::PlatformFile file,
695 int64_t max_size_bytes) {
solenberg566ef242015-11-06 15:34:49 -0800696 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
deadbeefeb02c032017-06-15 08:29:25 -0700697 auto aec_dump = webrtc::AecDumpFactory::Create(
698 file, max_size_bytes, low_priority_worker_queue_.get());
aleloi048cbdd2017-05-29 02:56:27 -0700699 if (!aec_dump) {
wu@webrtc.orga8910d22014-01-23 22:12:45 +0000700 return false;
701 }
aleloi048cbdd2017-05-29 02:56:27 -0700702 apm()->AttachAecDump(std::move(aec_dump));
wu@webrtc.orga9890802013-12-13 00:21:03 +0000703 return true;
wu@webrtc.orga9890802013-12-13 00:21:03 +0000704}
705
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000706void WebRtcVoiceEngine::StartAecDump(const std::string& filename) {
solenberg566ef242015-11-06 15:34:49 -0800707 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
aleloi048cbdd2017-05-29 02:56:27 -0700708
deadbeefeb02c032017-06-15 08:29:25 -0700709 auto aec_dump = webrtc::AecDumpFactory::Create(
710 filename, -1, low_priority_worker_queue_.get());
aleloi048cbdd2017-05-29 02:56:27 -0700711 if (aec_dump) {
712 apm()->AttachAecDump(std::move(aec_dump));
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000713 }
714}
715
716void WebRtcVoiceEngine::StopAecDump() {
solenberg566ef242015-11-06 15:34:49 -0800717 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
aleloi048cbdd2017-05-29 02:56:27 -0700718 apm()->DetachAecDump();
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000719}
720
solenberg0a617e22015-10-20 15:49:38 -0700721int WebRtcVoiceEngine::CreateVoEChannel() {
solenberg566ef242015-11-06 15:34:49 -0800722 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
solenberg88499ec2016-09-07 07:34:41 -0700723 return voe_wrapper_->base()->CreateChannel(channel_config_);
sergeyu@chromium.org5bc25c42013-12-05 00:24:06 +0000724}
725
solenberg5b5129a2016-04-08 05:35:48 -0700726webrtc::AudioDeviceModule* WebRtcVoiceEngine::adm() {
727 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
728 RTC_DCHECK(adm_);
729 return adm_;
730}
731
peahb1c9d1d2017-07-25 15:45:24 -0700732webrtc::AudioProcessing* WebRtcVoiceEngine::apm() const {
solenberg059fb442016-10-26 05:12:24 -0700733 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
peaha9cc40b2017-06-29 08:32:09 -0700734 return apm_.get();
solenberg059fb442016-10-26 05:12:24 -0700735}
736
solenberg76377c52017-02-21 00:54:31 -0800737webrtc::voe::TransmitMixer* WebRtcVoiceEngine::transmit_mixer() {
738 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
739 RTC_DCHECK(transmit_mixer_);
740 return transmit_mixer_;
741}
742
ossu20a4b3f2017-04-27 02:08:52 -0700743AudioCodecs WebRtcVoiceEngine::CollectCodecs(
744 const std::vector<webrtc::AudioCodecSpec>& specs) const {
ossuc54071d2016-08-17 02:45:41 -0700745 PayloadTypeMapper mapper;
746 AudioCodecs out;
ossuc54071d2016-08-17 02:45:41 -0700747
solenberg2779bab2016-11-17 04:45:19 -0800748 // Only generate CN payload types for these clockrates:
ossuc54071d2016-08-17 02:45:41 -0700749 std::map<int, bool, std::greater<int>> generate_cn = {{ 8000, false },
750 { 16000, false },
751 { 32000, false }};
solenberg2779bab2016-11-17 04:45:19 -0800752 // Only generate telephone-event payload types for these clockrates:
753 std::map<int, bool, std::greater<int>> generate_dtmf = {{ 8000, false },
754 { 16000, false },
755 { 32000, false },
756 { 48000, false }};
ossuc54071d2016-08-17 02:45:41 -0700757
ossu9def8002017-02-09 05:14:32 -0800758 auto map_format = [&mapper](const webrtc::SdpAudioFormat& format,
759 AudioCodecs* out) {
ossuc54071d2016-08-17 02:45:41 -0700760 rtc::Optional<AudioCodec> opt_codec = mapper.ToAudioCodec(format);
ossu9def8002017-02-09 05:14:32 -0800761 if (opt_codec) {
762 if (out) {
763 out->push_back(*opt_codec);
764 }
765 } else {
Mirko Bonadei675513b2017-11-09 11:09:25 +0100766 RTC_LOG(LS_ERROR) << "Unable to assign payload type to format: "
767 << format;
ossuc54071d2016-08-17 02:45:41 -0700768 }
769
ossu9def8002017-02-09 05:14:32 -0800770 return opt_codec;
ossuc54071d2016-08-17 02:45:41 -0700771 };
772
ossud4e9f622016-08-18 02:01:17 -0700773 for (const auto& spec : specs) {
ossu9def8002017-02-09 05:14:32 -0800774 // We need to do some extra stuff before adding the main codecs to out.
775 rtc::Optional<AudioCodec> opt_codec = map_format(spec.format, nullptr);
776 if (opt_codec) {
777 AudioCodec& codec = *opt_codec;
ossua1a040a2017-04-06 10:03:21 -0700778 if (spec.info.supports_network_adaption) {
ossu9def8002017-02-09 05:14:32 -0800779 codec.AddFeedbackParam(
780 FeedbackParam(kRtcpFbParamTransportCc, kParamValueEmpty));
781 }
782
ossua1a040a2017-04-06 10:03:21 -0700783 if (spec.info.allow_comfort_noise) {
solenberg2779bab2016-11-17 04:45:19 -0800784 // Generate a CN entry if the decoder allows it and we support the
785 // clockrate.
786 auto cn = generate_cn.find(spec.format.clockrate_hz);
787 if (cn != generate_cn.end()) {
788 cn->second = true;
789 }
790 }
791
792 // Generate a telephone-event entry if we support the clockrate.
793 auto dtmf = generate_dtmf.find(spec.format.clockrate_hz);
794 if (dtmf != generate_dtmf.end()) {
795 dtmf->second = true;
ossuc54071d2016-08-17 02:45:41 -0700796 }
ossu9def8002017-02-09 05:14:32 -0800797
798 out.push_back(codec);
ossuc54071d2016-08-17 02:45:41 -0700799 }
800 }
801
solenberg2779bab2016-11-17 04:45:19 -0800802 // Add CN codecs after "proper" audio codecs.
ossuc54071d2016-08-17 02:45:41 -0700803 for (const auto& cn : generate_cn) {
804 if (cn.second) {
ossu9def8002017-02-09 05:14:32 -0800805 map_format({kCnCodecName, cn.first, 1}, &out);
ossuc54071d2016-08-17 02:45:41 -0700806 }
807 }
808
solenberg2779bab2016-11-17 04:45:19 -0800809 // Add telephone-event codecs last.
810 for (const auto& dtmf : generate_dtmf) {
811 if (dtmf.second) {
ossu9def8002017-02-09 05:14:32 -0800812 map_format({kDtmfCodecName, dtmf.first, 1}, &out);
solenberg2779bab2016-11-17 04:45:19 -0800813 }
814 }
ossuc54071d2016-08-17 02:45:41 -0700815
816 return out;
817}
818
solenbergc96df772015-10-21 13:01:53 -0700819class WebRtcVoiceMediaChannel::WebRtcAudioSendStream
Taylor Brandstetter1a018dc2016-03-08 12:37:39 -0800820 : public AudioSource::Sink {
mallinath@webrtc.org67ee6b92014-02-03 16:57:16 +0000821 public:
minyue7a973442016-10-20 03:27:12 -0700822 WebRtcAudioSendStream(
823 int ch,
824 webrtc::AudioTransport* voe_audio_transport,
825 uint32_t ssrc,
826 const std::string& c_name,
Alex Narestb3944f02017-10-13 14:56:18 +0200827 const std::string track_id,
ossu20a4b3f2017-04-27 02:08:52 -0700828 const rtc::Optional<webrtc::AudioSendStream::Config::SendCodecSpec>&
829 send_codec_spec,
minyue7a973442016-10-20 03:27:12 -0700830 const std::vector<webrtc::RtpExtension>& extensions,
831 int max_send_bitrate_bps,
minyue6b825df2016-10-31 04:08:32 -0700832 const rtc::Optional<std::string>& audio_network_adaptor_config,
minyue7a973442016-10-20 03:27:12 -0700833 webrtc::Call* call,
ossu20a4b3f2017-04-27 02:08:52 -0700834 webrtc::Transport* send_transport,
835 const rtc::scoped_refptr<webrtc::AudioEncoderFactory>& encoder_factory)
solenberg7add0582015-11-20 09:59:34 -0800836 : voe_audio_transport_(voe_audio_transport),
solenberg3a941542015-11-16 07:34:50 -0800837 call_(call),
mflodman3d7db262016-04-29 00:57:13 -0700838 config_(send_transport),
sprangc1b57a12017-02-28 08:50:47 -0800839 send_side_bwe_with_overhead_(
840 webrtc::field_trial::IsEnabled("WebRTC-SendSideBwe-WithOverhead")),
minyue7a973442016-10-20 03:27:12 -0700841 max_send_bitrate_bps_(max_send_bitrate_bps),
skvlade0d46372016-04-07 22:59:22 -0700842 rtp_parameters_(CreateRtpParametersWithOneEncoding()) {
solenberg85a04962015-10-27 03:35:21 -0700843 RTC_DCHECK_GE(ch, 0);
844 // TODO(solenberg): Once we're not using FakeWebRtcVoiceEngine anymore:
845 // RTC_DCHECK(voe_audio_transport);
solenbergc96df772015-10-21 13:01:53 -0700846 RTC_DCHECK(call);
ossu20a4b3f2017-04-27 02:08:52 -0700847 RTC_DCHECK(encoder_factory);
solenberg3a941542015-11-16 07:34:50 -0800848 config_.rtp.ssrc = ssrc;
849 config_.rtp.c_name = c_name;
850 config_.voe_channel_id = ch;
solenberg971cab02016-06-14 10:02:41 -0700851 config_.rtp.extensions = extensions;
minyue6b825df2016-10-31 04:08:32 -0700852 config_.audio_network_adaptor_config = audio_network_adaptor_config;
ossu20a4b3f2017-04-27 02:08:52 -0700853 config_.encoder_factory = encoder_factory;
Alex Narestb3944f02017-10-13 14:56:18 +0200854 config_.track_id = track_id;
deadbeefcb443432016-12-12 11:12:36 -0800855 rtp_parameters_.encodings[0].ssrc = rtc::Optional<uint32_t>(ssrc);
ossu20a4b3f2017-04-27 02:08:52 -0700856
857 if (send_codec_spec) {
858 UpdateSendCodecSpec(*send_codec_spec);
859 }
860
861 stream_ = call_->CreateAudioSendStream(config_);
solenbergc96df772015-10-21 13:01:53 -0700862 }
solenberg3a941542015-11-16 07:34:50 -0800863
solenbergc96df772015-10-21 13:01:53 -0700864 ~WebRtcAudioSendStream() override {
solenberg566ef242015-11-06 15:34:49 -0800865 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
Taylor Brandstetter1a018dc2016-03-08 12:37:39 -0800866 ClearSource();
solenbergc96df772015-10-21 13:01:53 -0700867 call_->DestroyAudioSendStream(stream_);
868 }
henrike@webrtc.org1e09a712013-07-26 19:17:59 +0000869
ossu20a4b3f2017-04-27 02:08:52 -0700870 void SetSendCodecSpec(
minyue7a973442016-10-20 03:27:12 -0700871 const webrtc::AudioSendStream::Config::SendCodecSpec& send_codec_spec) {
ossu20a4b3f2017-04-27 02:08:52 -0700872 UpdateSendCodecSpec(send_codec_spec);
873 ReconfigureAudioSendStream();
solenberg971cab02016-06-14 10:02:41 -0700874 }
875
ossu20a4b3f2017-04-27 02:08:52 -0700876 void SetRtpExtensions(const std::vector<webrtc::RtpExtension>& extensions) {
solenberg3a941542015-11-16 07:34:50 -0800877 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
solenberg3a941542015-11-16 07:34:50 -0800878 config_.rtp.extensions = extensions;
ossu20a4b3f2017-04-27 02:08:52 -0700879 ReconfigureAudioSendStream();
solenberg3a941542015-11-16 07:34:50 -0800880 }
881
ossu20a4b3f2017-04-27 02:08:52 -0700882 void SetAudioNetworkAdaptorConfig(
minyue6b825df2016-10-31 04:08:32 -0700883 const rtc::Optional<std::string>& audio_network_adaptor_config) {
884 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
885 if (config_.audio_network_adaptor_config == audio_network_adaptor_config) {
886 return;
887 }
888 config_.audio_network_adaptor_config = audio_network_adaptor_config;
ossu20a4b3f2017-04-27 02:08:52 -0700889 UpdateAllowedBitrateRange();
890 ReconfigureAudioSendStream();
minyue6b825df2016-10-31 04:08:32 -0700891 }
892
minyue7a973442016-10-20 03:27:12 -0700893 bool SetMaxSendBitrate(int bps) {
894 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
ossu20a4b3f2017-04-27 02:08:52 -0700895 RTC_DCHECK(config_.send_codec_spec);
896 RTC_DCHECK(audio_codec_spec_);
897 auto send_rate = ComputeSendBitrate(
898 bps, rtp_parameters_.encodings[0].max_bitrate_bps, *audio_codec_spec_);
899
minyue7a973442016-10-20 03:27:12 -0700900 if (!send_rate) {
901 return false;
902 }
903
904 max_send_bitrate_bps_ = bps;
905
ossu20a4b3f2017-04-27 02:08:52 -0700906 if (send_rate != config_.send_codec_spec->target_bitrate_bps) {
907 config_.send_codec_spec->target_bitrate_bps = send_rate;
908 ReconfigureAudioSendStream();
minyue7a973442016-10-20 03:27:12 -0700909 }
910 return true;
911 }
912
solenbergffbbcac2016-11-17 05:25:37 -0800913 bool SendTelephoneEvent(int payload_type, int payload_freq, int event,
914 int duration_ms) {
Fredrik Solenbergb5727682015-12-04 15:22:19 +0100915 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
916 RTC_DCHECK(stream_);
solenbergffbbcac2016-11-17 05:25:37 -0800917 return stream_->SendTelephoneEvent(payload_type, payload_freq, event,
918 duration_ms);
Fredrik Solenbergb5727682015-12-04 15:22:19 +0100919 }
920
Taylor Brandstetter1a018dc2016-03-08 12:37:39 -0800921 void SetSend(bool send) {
922 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
923 send_ = send;
924 UpdateSendState();
925 }
926
solenberg94218532016-06-16 10:53:22 -0700927 void SetMuted(bool muted) {
928 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
929 RTC_DCHECK(stream_);
930 stream_->SetMuted(muted);
931 muted_ = muted;
932 }
933
934 bool muted() const {
935 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
936 return muted_;
937 }
938
solenberg3a941542015-11-16 07:34:50 -0800939 webrtc::AudioSendStream::Stats GetStats() const {
940 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
941 RTC_DCHECK(stream_);
942 return stream_->GetStats();
943 }
944
Taylor Brandstetter1a018dc2016-03-08 12:37:39 -0800945 // Starts the sending by setting ourselves as a sink to the AudioSource to
946 // get data callbacks.
henrike@webrtc.orga7b98182014-02-21 15:51:43 +0000947 // This method is called on the libjingle worker thread.
mallinath@webrtc.org67ee6b92014-02-03 16:57:16 +0000948 // TODO(xians): Make sure Start() is called only once.
Taylor Brandstetter1a018dc2016-03-08 12:37:39 -0800949 void SetSource(AudioSource* source) {
solenberg566ef242015-11-06 15:34:49 -0800950 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
Taylor Brandstetter1a018dc2016-03-08 12:37:39 -0800951 RTC_DCHECK(source);
952 if (source_) {
953 RTC_DCHECK(source_ == source);
mallinath@webrtc.org67ee6b92014-02-03 16:57:16 +0000954 return;
955 }
Taylor Brandstetter1a018dc2016-03-08 12:37:39 -0800956 source->SetSink(this);
957 source_ = source;
958 UpdateSendState();
mallinath@webrtc.org67ee6b92014-02-03 16:57:16 +0000959 }
960
Taylor Brandstetter1a018dc2016-03-08 12:37:39 -0800961 // Stops sending by setting the sink of the AudioSource to nullptr. No data
mallinath@webrtc.org67ee6b92014-02-03 16:57:16 +0000962 // callback will be received after this method.
henrike@webrtc.orga7b98182014-02-21 15:51:43 +0000963 // This method is called on the libjingle worker thread.
Taylor Brandstetter1a018dc2016-03-08 12:37:39 -0800964 void ClearSource() {
solenberg566ef242015-11-06 15:34:49 -0800965 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
Taylor Brandstetter1a018dc2016-03-08 12:37:39 -0800966 if (source_) {
967 source_->SetSink(nullptr);
968 source_ = nullptr;
solenberg98c68862015-10-09 03:27:14 -0700969 }
Taylor Brandstetter1a018dc2016-03-08 12:37:39 -0800970 UpdateSendState();
mallinath@webrtc.org67ee6b92014-02-03 16:57:16 +0000971 }
972
Taylor Brandstetter1a018dc2016-03-08 12:37:39 -0800973 // AudioSource::Sink implementation.
henrike@webrtc.orga7b98182014-02-21 15:51:43 +0000974 // This method is called on the audio thread.
kjellander@webrtc.org14665ff2015-03-04 12:58:35 +0000975 void OnData(const void* audio_data,
976 int bits_per_sample,
977 int sample_rate,
Peter Kasting69558702016-01-12 16:26:35 -0800978 size_t number_of_channels,
Peter Kastingdce40cf2015-08-24 14:52:23 -0700979 size_t number_of_frames) override {
solenberg347ec5c2016-09-23 04:21:47 -0700980 RTC_CHECK_RUNS_SERIALIZED(&audio_capture_race_checker_);
solenbergc96df772015-10-21 13:01:53 -0700981 RTC_DCHECK(voe_audio_transport_);
maxmorin1aee0b52016-08-15 11:46:19 -0700982 voe_audio_transport_->PushCaptureData(config_.voe_channel_id, audio_data,
983 bits_per_sample, sample_rate,
984 number_of_channels, number_of_frames);
henrike@webrtc.orga7b98182014-02-21 15:51:43 +0000985 }
986
Taylor Brandstetter1a018dc2016-03-08 12:37:39 -0800987 // Callback from the |source_| when it is going away. In case Start() has
henrike@webrtc.orga7b98182014-02-21 15:51:43 +0000988 // never been called, this callback won't be triggered.
kjellander@webrtc.org14665ff2015-03-04 12:58:35 +0000989 void OnClose() override {
solenberg566ef242015-11-06 15:34:49 -0800990 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
Taylor Brandstetter1a018dc2016-03-08 12:37:39 -0800991 // Set |source_| to nullptr to make sure no more callback will get into
992 // the source.
993 source_ = nullptr;
994 UpdateSendState();
mallinath@webrtc.org67ee6b92014-02-03 16:57:16 +0000995 }
996
997 // Accessor to the VoE channel ID.
solenberg85a04962015-10-27 03:35:21 -0700998 int channel() const {
solenberg566ef242015-11-06 15:34:49 -0800999 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
solenberg7add0582015-11-20 09:59:34 -08001000 return config_.voe_channel_id;
solenberg85a04962015-10-27 03:35:21 -07001001 }
mallinath@webrtc.org67ee6b92014-02-03 16:57:16 +00001002
skvlade0d46372016-04-07 22:59:22 -07001003 const webrtc::RtpParameters& rtp_parameters() const {
1004 return rtp_parameters_;
1005 }
1006
deadbeeffb2aced2017-01-06 23:05:37 -08001007 bool ValidateRtpParameters(const webrtc::RtpParameters& rtp_parameters) {
1008 if (rtp_parameters.encodings.size() != 1) {
Mirko Bonadei675513b2017-11-09 11:09:25 +01001009 RTC_LOG(LS_ERROR)
deadbeeffb2aced2017-01-06 23:05:37 -08001010 << "Attempted to set RtpParameters without exactly one encoding";
1011 return false;
1012 }
1013 if (rtp_parameters.encodings[0].ssrc != rtp_parameters_.encodings[0].ssrc) {
Mirko Bonadei675513b2017-11-09 11:09:25 +01001014 RTC_LOG(LS_ERROR) << "Attempted to set RtpParameters with modified SSRC";
deadbeeffb2aced2017-01-06 23:05:37 -08001015 return false;
1016 }
1017 return true;
1018 }
1019
minyue7a973442016-10-20 03:27:12 -07001020 bool SetRtpParameters(const webrtc::RtpParameters& parameters) {
deadbeeffb2aced2017-01-06 23:05:37 -08001021 if (!ValidateRtpParameters(parameters)) {
1022 return false;
1023 }
ossu20a4b3f2017-04-27 02:08:52 -07001024
1025 rtc::Optional<int> send_rate;
1026 if (audio_codec_spec_) {
1027 send_rate = ComputeSendBitrate(max_send_bitrate_bps_,
1028 parameters.encodings[0].max_bitrate_bps,
1029 *audio_codec_spec_);
1030 if (!send_rate) {
1031 return false;
1032 }
minyue7a973442016-10-20 03:27:12 -07001033 }
1034
minyuececec102017-03-27 13:04:25 -07001035 const rtc::Optional<int> old_rtp_max_bitrate =
1036 rtp_parameters_.encodings[0].max_bitrate_bps;
1037
skvlade0d46372016-04-07 22:59:22 -07001038 rtp_parameters_ = parameters;
minyue7a973442016-10-20 03:27:12 -07001039
minyuececec102017-03-27 13:04:25 -07001040 if (rtp_parameters_.encodings[0].max_bitrate_bps != old_rtp_max_bitrate) {
ossu20a4b3f2017-04-27 02:08:52 -07001041 // Reconfigure AudioSendStream with new bit rate.
1042 if (send_rate) {
1043 config_.send_codec_spec->target_bitrate_bps = send_rate;
1044 }
1045 UpdateAllowedBitrateRange();
1046 ReconfigureAudioSendStream();
minyue7a973442016-10-20 03:27:12 -07001047 } else {
1048 // parameters.encodings[0].active could have changed.
1049 UpdateSendState();
1050 }
1051 return true;
skvlade0d46372016-04-07 22:59:22 -07001052 }
1053
mallinath@webrtc.org67ee6b92014-02-03 16:57:16 +00001054 private:
Taylor Brandstetter1a018dc2016-03-08 12:37:39 -08001055 void UpdateSendState() {
1056 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
1057 RTC_DCHECK(stream_);
Taylor Brandstetter55dd7082016-05-03 13:50:11 -07001058 RTC_DCHECK_EQ(1UL, rtp_parameters_.encodings.size());
1059 if (send_ && source_ != nullptr && rtp_parameters_.encodings[0].active) {
Taylor Brandstetter1a018dc2016-03-08 12:37:39 -08001060 stream_->Start();
1061 } else { // !send || source_ = nullptr
1062 stream_->Stop();
1063 }
1064 }
1065
ossu20a4b3f2017-04-27 02:08:52 -07001066 void UpdateAllowedBitrateRange() {
michaelt53fe19d2016-10-18 09:39:22 -07001067 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
ossu20a4b3f2017-04-27 02:08:52 -07001068 const bool is_opus =
1069 config_.send_codec_spec &&
1070 !STR_CASE_CMP(config_.send_codec_spec->format.name.c_str(),
1071 kOpusCodecName);
1072 if (is_opus && webrtc::field_trial::IsEnabled("WebRTC-Audio-SendSideBwe")) {
stefane9f36d52017-01-24 08:18:45 -08001073 config_.min_bitrate_bps = kOpusMinBitrateBps;
minyuececec102017-03-27 13:04:25 -07001074
1075 // This means that when RtpParameters is reset, we may change the
ossu20a4b3f2017-04-27 02:08:52 -07001076 // encoder's bit rate immediately (through ReconfigureAudioSendStream()),
minyuececec102017-03-27 13:04:25 -07001077 // meanwhile change the cap to the output of BWE.
1078 config_.max_bitrate_bps =
1079 rtp_parameters_.encodings[0].max_bitrate_bps
1080 ? *rtp_parameters_.encodings[0].max_bitrate_bps
1081 : kOpusBitrateFbBps;
1082
michaelt53fe19d2016-10-18 09:39:22 -07001083 // TODO(mflodman): Keep testing this and set proper values.
1084 // Note: This is an early experiment currently only supported by Opus.
elad.alon0fe12162017-01-31 05:48:37 -08001085 if (send_side_bwe_with_overhead_) {
ossu20a4b3f2017-04-27 02:08:52 -07001086 const int max_packet_size_ms =
1087 WEBRTC_OPUS_SUPPORT_120MS_PTIME ? 120 : 60;
michaelt6672b262017-01-11 10:17:59 -08001088
ossu20a4b3f2017-04-27 02:08:52 -07001089 // OverheadPerPacket = Ipv4(20B) + UDP(8B) + SRTP(10B) + RTP(12)
1090 constexpr int kOverheadPerPacket = 20 + 8 + 10 + 12;
michaelt6672b262017-01-11 10:17:59 -08001091
ossu20a4b3f2017-04-27 02:08:52 -07001092 int min_overhead_bps =
1093 kOverheadPerPacket * 8 * 1000 / max_packet_size_ms;
michaelt6672b262017-01-11 10:17:59 -08001094
ossu20a4b3f2017-04-27 02:08:52 -07001095 // We assume that |config_.max_bitrate_bps| before the next line is
1096 // a hard limit on the payload bitrate, so we add min_overhead_bps to
1097 // it to ensure that, when overhead is deducted, the payload rate
1098 // never goes beyond the limit.
1099 // Note: this also means that if a higher overhead is forced, we
1100 // cannot reach the limit.
1101 // TODO(minyue): Reconsider this when the signaling to BWE is done
1102 // through a dedicated API.
1103 config_.max_bitrate_bps += min_overhead_bps;
michaelt6672b262017-01-11 10:17:59 -08001104
ossu20a4b3f2017-04-27 02:08:52 -07001105 // In contrast to max_bitrate_bps, we let min_bitrate_bps always be
1106 // reachable.
1107 config_.min_bitrate_bps += min_overhead_bps;
michaelt6672b262017-01-11 10:17:59 -08001108 }
michaelt53fe19d2016-10-18 09:39:22 -07001109 }
ossu20a4b3f2017-04-27 02:08:52 -07001110 }
1111
1112 void UpdateSendCodecSpec(
1113 const webrtc::AudioSendStream::Config::SendCodecSpec& send_codec_spec) {
1114 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
1115 config_.rtp.nack.rtp_history_ms =
1116 send_codec_spec.nack_enabled ? kNackRtpHistoryMs : 0;
1117 config_.send_codec_spec =
1118 rtc::Optional<webrtc::AudioSendStream::Config::SendCodecSpec>(
1119 send_codec_spec);
1120 auto info =
1121 config_.encoder_factory->QueryAudioEncoder(send_codec_spec.format);
1122 RTC_DCHECK(info);
1123 // If a specific target bitrate has been set for the stream, use that as
1124 // the new default bitrate when computing send bitrate.
1125 if (send_codec_spec.target_bitrate_bps) {
1126 info->default_bitrate_bps = std::max(
1127 info->min_bitrate_bps,
1128 std::min(info->max_bitrate_bps, *send_codec_spec.target_bitrate_bps));
1129 }
1130
1131 audio_codec_spec_.emplace(
1132 webrtc::AudioCodecSpec{send_codec_spec.format, *info});
1133
1134 config_.send_codec_spec->target_bitrate_bps = ComputeSendBitrate(
1135 max_send_bitrate_bps_, rtp_parameters_.encodings[0].max_bitrate_bps,
1136 *audio_codec_spec_);
1137
1138 UpdateAllowedBitrateRange();
1139 }
1140
1141 void ReconfigureAudioSendStream() {
1142 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
1143 RTC_DCHECK(stream_);
1144 stream_->Reconfigure(config_);
michaelt53fe19d2016-10-18 09:39:22 -07001145 }
1146
solenberg566ef242015-11-06 15:34:49 -08001147 rtc::ThreadChecker worker_thread_checker_;
solenberg347ec5c2016-09-23 04:21:47 -07001148 rtc::RaceChecker audio_capture_race_checker_;
solenbergc96df772015-10-21 13:01:53 -07001149 webrtc::AudioTransport* const voe_audio_transport_ = nullptr;
1150 webrtc::Call* call_ = nullptr;
solenberg3a941542015-11-16 07:34:50 -08001151 webrtc::AudioSendStream::Config config_;
elad.alon0fe12162017-01-31 05:48:37 -08001152 const bool send_side_bwe_with_overhead_;
solenberg3a941542015-11-16 07:34:50 -08001153 // The stream is owned by WebRtcAudioSendStream and may be reallocated if
1154 // configuration changes.
solenbergc96df772015-10-21 13:01:53 -07001155 webrtc::AudioSendStream* stream_ = nullptr;
mallinath@webrtc.org67ee6b92014-02-03 16:57:16 +00001156
Taylor Brandstetter1a018dc2016-03-08 12:37:39 -08001157 // Raw pointer to AudioSource owned by LocalAudioTrackHandler.
mallinath@webrtc.org67ee6b92014-02-03 16:57:16 +00001158 // PeerConnection will make sure invalidating the pointer before the object
1159 // goes away.
Taylor Brandstetter1a018dc2016-03-08 12:37:39 -08001160 AudioSource* source_ = nullptr;
1161 bool send_ = false;
solenberg94218532016-06-16 10:53:22 -07001162 bool muted_ = false;
minyue7a973442016-10-20 03:27:12 -07001163 int max_send_bitrate_bps_;
skvlade0d46372016-04-07 22:59:22 -07001164 webrtc::RtpParameters rtp_parameters_;
ossu20a4b3f2017-04-27 02:08:52 -07001165 rtc::Optional<webrtc::AudioCodecSpec> audio_codec_spec_;
henrike@webrtc.orga7b98182014-02-21 15:51:43 +00001166
solenbergc96df772015-10-21 13:01:53 -07001167 RTC_DISALLOW_IMPLICIT_CONSTRUCTORS(WebRtcAudioSendStream);
1168};
1169
1170class WebRtcVoiceMediaChannel::WebRtcAudioReceiveStream {
1171 public:
ossu29b1a8d2016-06-13 07:34:51 -07001172 WebRtcAudioReceiveStream(
1173 int ch,
1174 uint32_t remote_ssrc,
1175 uint32_t local_ssrc,
1176 bool use_transport_cc,
solenberg8189b022016-06-14 12:13:00 -07001177 bool use_nack,
ossu29b1a8d2016-06-13 07:34:51 -07001178 const std::string& sync_group,
1179 const std::vector<webrtc::RtpExtension>& extensions,
1180 webrtc::Call* call,
1181 webrtc::Transport* rtcp_send_transport,
kwiberg1c07c702017-03-27 07:15:49 -07001182 const rtc::scoped_refptr<webrtc::AudioDecoderFactory>& decoder_factory,
1183 const std::map<int, webrtc::SdpAudioFormat>& decoder_map)
stefanba4c0e42016-02-04 04:12:24 -08001184 : call_(call), config_() {
solenberg7add0582015-11-20 09:59:34 -08001185 RTC_DCHECK_GE(ch, 0);
1186 RTC_DCHECK(call);
1187 config_.rtp.remote_ssrc = remote_ssrc;
kwibergd32bf752017-01-19 07:03:59 -08001188 config_.rtp.local_ssrc = local_ssrc;
1189 config_.rtp.transport_cc = use_transport_cc;
1190 config_.rtp.nack.rtp_history_ms = use_nack ? kNackRtpHistoryMs : 0;
1191 config_.rtp.extensions = extensions;
solenberg31fec402016-05-06 02:13:12 -07001192 config_.rtcp_send_transport = rtcp_send_transport;
solenberg7add0582015-11-20 09:59:34 -08001193 config_.voe_channel_id = ch;
1194 config_.sync_group = sync_group;
ossu29b1a8d2016-06-13 07:34:51 -07001195 config_.decoder_factory = decoder_factory;
kwiberg1c07c702017-03-27 07:15:49 -07001196 config_.decoder_map = decoder_map;
kwibergd32bf752017-01-19 07:03:59 -08001197 RecreateAudioReceiveStream();
solenberg7add0582015-11-20 09:59:34 -08001198 }
solenbergc96df772015-10-21 13:01:53 -07001199
solenberg7add0582015-11-20 09:59:34 -08001200 ~WebRtcAudioReceiveStream() {
1201 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
1202 call_->DestroyAudioReceiveStream(stream_);
1203 }
1204
solenberg4a0f7b52016-06-16 13:07:33 -07001205 void RecreateAudioReceiveStream(uint32_t local_ssrc) {
solenberg7add0582015-11-20 09:59:34 -08001206 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
kwibergd32bf752017-01-19 07:03:59 -08001207 config_.rtp.local_ssrc = local_ssrc;
1208 RecreateAudioReceiveStream();
solenberg7add0582015-11-20 09:59:34 -08001209 }
solenberg8189b022016-06-14 12:13:00 -07001210
1211 void RecreateAudioReceiveStream(bool use_transport_cc, bool use_nack) {
solenberg7add0582015-11-20 09:59:34 -08001212 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
kwibergd32bf752017-01-19 07:03:59 -08001213 config_.rtp.transport_cc = use_transport_cc;
1214 config_.rtp.nack.rtp_history_ms = use_nack ? kNackRtpHistoryMs : 0;
1215 RecreateAudioReceiveStream();
solenberg7add0582015-11-20 09:59:34 -08001216 }
1217
solenberg4a0f7b52016-06-16 13:07:33 -07001218 void RecreateAudioReceiveStream(
1219 const std::vector<webrtc::RtpExtension>& extensions) {
1220 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
kwibergd32bf752017-01-19 07:03:59 -08001221 config_.rtp.extensions = extensions;
1222 RecreateAudioReceiveStream();
1223 }
1224
deadbeefcb383672017-04-26 16:28:42 -07001225 // Set a new payload type -> decoder map.
kwibergd32bf752017-01-19 07:03:59 -08001226 void RecreateAudioReceiveStream(
1227 const std::map<int, webrtc::SdpAudioFormat>& decoder_map) {
1228 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
kwibergd32bf752017-01-19 07:03:59 -08001229 config_.decoder_map = decoder_map;
1230 RecreateAudioReceiveStream();
solenberg4a0f7b52016-06-16 13:07:33 -07001231 }
1232
solenberg4904fb62017-02-17 12:01:14 -08001233 void MaybeRecreateAudioReceiveStream(const std::string& sync_group) {
1234 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
1235 if (config_.sync_group != sync_group) {
1236 config_.sync_group = sync_group;
1237 RecreateAudioReceiveStream();
1238 }
1239 }
1240
solenberg7add0582015-11-20 09:59:34 -08001241 webrtc::AudioReceiveStream::Stats GetStats() const {
1242 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
1243 RTC_DCHECK(stream_);
1244 return stream_->GetStats();
1245 }
1246
solenberg796b8f92017-03-01 17:02:23 -08001247 int GetOutputLevel() const {
1248 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
1249 RTC_DCHECK(stream_);
1250 return stream_->GetOutputLevel();
1251 }
1252
solenberg7add0582015-11-20 09:59:34 -08001253 int channel() const {
1254 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
1255 return config_.voe_channel_id;
1256 }
solenbergc96df772015-10-21 13:01:53 -07001257
kwiberg686a8ef2016-02-26 03:00:35 -08001258 void SetRawAudioSink(std::unique_ptr<webrtc::AudioSinkInterface> sink) {
Tommif888bb52015-12-12 01:37:01 +01001259 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
kwiberg686a8ef2016-02-26 03:00:35 -08001260 stream_->SetSink(std::move(sink));
Tommif888bb52015-12-12 01:37:01 +01001261 }
1262
solenberg217fb662016-06-17 08:30:54 -07001263 void SetOutputVolume(double volume) {
1264 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
1265 stream_->SetGain(volume);
1266 }
1267
aleloi84ef6152016-08-04 05:28:21 -07001268 void SetPlayout(bool playout) {
1269 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
1270 RTC_DCHECK(stream_);
1271 if (playout) {
Mirko Bonadei675513b2017-11-09 11:09:25 +01001272 RTC_LOG(LS_INFO) << "Starting playout for channel #" << channel();
aleloi84ef6152016-08-04 05:28:21 -07001273 stream_->Start();
1274 } else {
Mirko Bonadei675513b2017-11-09 11:09:25 +01001275 RTC_LOG(LS_INFO) << "Stopping playout for channel #" << channel();
aleloi84ef6152016-08-04 05:28:21 -07001276 stream_->Stop();
1277 }
aleloi18e0b672016-10-04 02:45:47 -07001278 playout_ = playout;
aleloi84ef6152016-08-04 05:28:21 -07001279 }
1280
hbos8d609f62017-04-10 07:39:05 -07001281 std::vector<webrtc::RtpSource> GetSources() {
1282 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
1283 RTC_DCHECK(stream_);
1284 return stream_->GetSources();
1285 }
1286
solenbergc96df772015-10-21 13:01:53 -07001287 private:
kwibergd32bf752017-01-19 07:03:59 -08001288 void RecreateAudioReceiveStream() {
solenberg7add0582015-11-20 09:59:34 -08001289 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
1290 if (stream_) {
1291 call_->DestroyAudioReceiveStream(stream_);
solenberg7add0582015-11-20 09:59:34 -08001292 }
solenberg7add0582015-11-20 09:59:34 -08001293 stream_ = call_->CreateAudioReceiveStream(config_);
1294 RTC_CHECK(stream_);
aleloi18e0b672016-10-04 02:45:47 -07001295 SetPlayout(playout_);
solenberg7add0582015-11-20 09:59:34 -08001296 }
1297
1298 rtc::ThreadChecker worker_thread_checker_;
1299 webrtc::Call* call_ = nullptr;
1300 webrtc::AudioReceiveStream::Config config_;
1301 // The stream is owned by WebRtcAudioReceiveStream and may be reallocated if
1302 // configuration changes.
1303 webrtc::AudioReceiveStream* stream_ = nullptr;
aleloi18e0b672016-10-04 02:45:47 -07001304 bool playout_ = false;
solenbergc96df772015-10-21 13:01:53 -07001305
1306 RTC_DISALLOW_IMPLICIT_CONSTRUCTORS(WebRtcAudioReceiveStream);
henrike@webrtc.org1e09a712013-07-26 19:17:59 +00001307};
1308
Fredrik Solenberg709ed672015-09-15 12:26:33 +02001309WebRtcVoiceMediaChannel::WebRtcVoiceMediaChannel(WebRtcVoiceEngine* engine,
nisse51542be2016-02-12 02:27:06 -08001310 const MediaConfig& config,
Fredrik Solenbergb071a192015-09-17 16:42:56 +02001311 const AudioOptions& options,
Fredrik Solenberg709ed672015-09-15 12:26:33 +02001312 webrtc::Call* call)
nisse51542be2016-02-12 02:27:06 -08001313 : VoiceMediaChannel(config), engine_(engine), call_(call) {
Mirko Bonadei675513b2017-11-09 11:09:25 +01001314 RTC_LOG(LS_VERBOSE) << "WebRtcVoiceMediaChannel::WebRtcVoiceMediaChannel";
solenberg566ef242015-11-06 15:34:49 -08001315 RTC_DCHECK(call);
solenberg0a617e22015-10-20 15:49:38 -07001316 engine->RegisterChannel(this);
Fredrik Solenbergb071a192015-09-17 16:42:56 +02001317 SetOptions(options);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001318}
1319
1320WebRtcVoiceMediaChannel::~WebRtcVoiceMediaChannel() {
solenberg566ef242015-11-06 15:34:49 -08001321 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
Mirko Bonadei675513b2017-11-09 11:09:25 +01001322 RTC_LOG(LS_VERBOSE) << "WebRtcVoiceMediaChannel::~WebRtcVoiceMediaChannel";
solenberg7add0582015-11-20 09:59:34 -08001323 // TODO(solenberg): Should be able to delete the streams directly, without
1324 // going through RemoveNnStream(), once stream objects handle
1325 // all (de)configuration.
solenbergc96df772015-10-21 13:01:53 -07001326 while (!send_streams_.empty()) {
1327 RemoveSendStream(send_streams_.begin()->first);
solenbergd97ec302015-10-07 01:40:33 -07001328 }
solenberg7add0582015-11-20 09:59:34 -08001329 while (!recv_streams_.empty()) {
1330 RemoveRecvStream(recv_streams_.begin()->first);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001331 }
solenberg0a617e22015-10-20 15:49:38 -07001332 engine()->UnregisterChannel(this);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001333}
1334
nisse51542be2016-02-12 02:27:06 -08001335rtc::DiffServCodePoint WebRtcVoiceMediaChannel::PreferredDscp() const {
1336 return kAudioDscpValue;
1337}
1338
Peter Thatcherc2ee2c82015-08-07 16:05:34 -07001339bool WebRtcVoiceMediaChannel::SetSendParameters(
1340 const AudioSendParameters& params) {
Peter Boströmca8b4042016-03-08 14:24:13 -08001341 TRACE_EVENT0("webrtc", "WebRtcVoiceMediaChannel::SetSendParameters");
solenberg566ef242015-11-06 15:34:49 -08001342 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
Mirko Bonadei675513b2017-11-09 11:09:25 +01001343 RTC_LOG(LS_INFO) << "WebRtcVoiceMediaChannel::SetSendParameters: "
1344 << params.ToString();
Peter Thatcherc2ee2c82015-08-07 16:05:34 -07001345 // TODO(pthatcher): Refactor this to be more clean now that we have
1346 // all the information at once.
solenberg3a941542015-11-16 07:34:50 -08001347
1348 if (!SetSendCodecs(params.codecs)) {
1349 return false;
1350 }
1351
solenberg7e4e01a2015-12-02 08:05:01 -08001352 if (!ValidateRtpExtensions(params.extensions)) {
1353 return false;
1354 }
1355 std::vector<webrtc::RtpExtension> filtered_extensions =
1356 FilterRtpExtensions(params.extensions,
1357 webrtc::RtpExtension::IsSupportedForAudio, true);
1358 if (send_rtp_extensions_ != filtered_extensions) {
1359 send_rtp_extensions_.swap(filtered_extensions);
solenberg3a941542015-11-16 07:34:50 -08001360 for (auto& it : send_streams_) {
ossu20a4b3f2017-04-27 02:08:52 -07001361 it.second->SetRtpExtensions(send_rtp_extensions_);
solenberg3a941542015-11-16 07:34:50 -08001362 }
1363 }
1364
deadbeef80346142016-04-27 14:17:10 -07001365 if (!SetMaxSendBitrate(params.max_bandwidth_bps)) {
solenberg3a941542015-11-16 07:34:50 -08001366 return false;
1367 }
1368 return SetOptions(params.options);
Peter Thatcherc2ee2c82015-08-07 16:05:34 -07001369}
1370
1371bool WebRtcVoiceMediaChannel::SetRecvParameters(
1372 const AudioRecvParameters& params) {
Peter Boströmca8b4042016-03-08 14:24:13 -08001373 TRACE_EVENT0("webrtc", "WebRtcVoiceMediaChannel::SetRecvParameters");
solenberg566ef242015-11-06 15:34:49 -08001374 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
Mirko Bonadei675513b2017-11-09 11:09:25 +01001375 RTC_LOG(LS_INFO) << "WebRtcVoiceMediaChannel::SetRecvParameters: "
1376 << params.ToString();
Peter Thatcherc2ee2c82015-08-07 16:05:34 -07001377 // TODO(pthatcher): Refactor this to be more clean now that we have
1378 // all the information at once.
solenberg7add0582015-11-20 09:59:34 -08001379
1380 if (!SetRecvCodecs(params.codecs)) {
1381 return false;
1382 }
1383
solenberg7e4e01a2015-12-02 08:05:01 -08001384 if (!ValidateRtpExtensions(params.extensions)) {
1385 return false;
1386 }
1387 std::vector<webrtc::RtpExtension> filtered_extensions =
1388 FilterRtpExtensions(params.extensions,
1389 webrtc::RtpExtension::IsSupportedForAudio, false);
1390 if (recv_rtp_extensions_ != filtered_extensions) {
1391 recv_rtp_extensions_.swap(filtered_extensions);
solenberg7add0582015-11-20 09:59:34 -08001392 for (auto& it : recv_streams_) {
1393 it.second->RecreateAudioReceiveStream(recv_rtp_extensions_);
1394 }
1395 }
solenberg7add0582015-11-20 09:59:34 -08001396 return true;
Peter Thatcherc2ee2c82015-08-07 16:05:34 -07001397}
1398
Taylor Brandstetterdb0cd9e2016-05-16 11:40:30 -07001399webrtc::RtpParameters WebRtcVoiceMediaChannel::GetRtpSendParameters(
skvlade0d46372016-04-07 22:59:22 -07001400 uint32_t ssrc) const {
1401 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
1402 auto it = send_streams_.find(ssrc);
1403 if (it == send_streams_.end()) {
Mirko Bonadei675513b2017-11-09 11:09:25 +01001404 RTC_LOG(LS_WARNING) << "Attempting to get RTP send parameters for stream "
1405 << "with ssrc " << ssrc << " which doesn't exist.";
skvlade0d46372016-04-07 22:59:22 -07001406 return webrtc::RtpParameters();
1407 }
1408
Taylor Brandstetter0cd086b2016-04-20 16:23:10 -07001409 webrtc::RtpParameters rtp_params = it->second->rtp_parameters();
1410 // Need to add the common list of codecs to the send stream-specific
1411 // RTP parameters.
1412 for (const AudioCodec& codec : send_codecs_) {
1413 rtp_params.codecs.push_back(codec.ToCodecParameters());
1414 }
1415 return rtp_params;
skvlade0d46372016-04-07 22:59:22 -07001416}
1417
Taylor Brandstetterdb0cd9e2016-05-16 11:40:30 -07001418bool WebRtcVoiceMediaChannel::SetRtpSendParameters(
skvlade0d46372016-04-07 22:59:22 -07001419 uint32_t ssrc,
1420 const webrtc::RtpParameters& parameters) {
1421 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
skvlade0d46372016-04-07 22:59:22 -07001422 auto it = send_streams_.find(ssrc);
1423 if (it == send_streams_.end()) {
Mirko Bonadei675513b2017-11-09 11:09:25 +01001424 RTC_LOG(LS_WARNING) << "Attempting to set RTP send parameters for stream "
1425 << "with ssrc " << ssrc << " which doesn't exist.";
skvlade0d46372016-04-07 22:59:22 -07001426 return false;
1427 }
1428
Taylor Brandstetterdb0cd9e2016-05-16 11:40:30 -07001429 // TODO(deadbeef): Handle setting parameters with a list of codecs in a
1430 // different order (which should change the send codec).
1431 webrtc::RtpParameters current_parameters = GetRtpSendParameters(ssrc);
1432 if (current_parameters.codecs != parameters.codecs) {
Mirko Bonadei675513b2017-11-09 11:09:25 +01001433 RTC_LOG(LS_ERROR) << "Using SetParameters to change the set of codecs "
1434 << "is not currently supported.";
Taylor Brandstetterdb0cd9e2016-05-16 11:40:30 -07001435 return false;
1436 }
1437
minyue7a973442016-10-20 03:27:12 -07001438 // TODO(minyue): The following legacy actions go into
1439 // |WebRtcAudioSendStream::SetRtpParameters()| which is called at the end,
1440 // though there are two difference:
1441 // 1. |WebRtcVoiceMediaChannel::SetChannelSendParameters()| only calls
1442 // |SetSendCodec| while |WebRtcAudioSendStream::SetRtpParameters()| calls
1443 // |SetSendCodecs|. The outcome should be the same.
1444 // 2. AudioSendStream can be recreated.
1445
Taylor Brandstetter0cd086b2016-04-20 16:23:10 -07001446 // Codecs are handled at the WebRtcVoiceMediaChannel level.
1447 webrtc::RtpParameters reduced_params = parameters;
1448 reduced_params.codecs.clear();
minyue7a973442016-10-20 03:27:12 -07001449 return it->second->SetRtpParameters(reduced_params);
skvlade0d46372016-04-07 22:59:22 -07001450}
1451
Taylor Brandstetterdb0cd9e2016-05-16 11:40:30 -07001452webrtc::RtpParameters WebRtcVoiceMediaChannel::GetRtpReceiveParameters(
1453 uint32_t ssrc) const {
1454 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
deadbeef3bc15102017-04-20 19:25:07 -07001455 webrtc::RtpParameters rtp_params;
1456 // SSRC of 0 represents the default receive stream.
1457 if (ssrc == 0) {
1458 if (!default_sink_) {
Mirko Bonadei675513b2017-11-09 11:09:25 +01001459 RTC_LOG(LS_WARNING)
1460 << "Attempting to get RTP parameters for the default, "
1461 "unsignaled audio receive stream, but not yet "
1462 "configured to receive such a stream.";
deadbeef3bc15102017-04-20 19:25:07 -07001463 return rtp_params;
1464 }
1465 rtp_params.encodings.emplace_back();
1466 } else {
1467 auto it = recv_streams_.find(ssrc);
1468 if (it == recv_streams_.end()) {
Mirko Bonadei675513b2017-11-09 11:09:25 +01001469 RTC_LOG(LS_WARNING)
1470 << "Attempting to get RTP receive parameters for stream "
1471 << "with ssrc " << ssrc << " which doesn't exist.";
deadbeef3bc15102017-04-20 19:25:07 -07001472 return webrtc::RtpParameters();
1473 }
1474 rtp_params.encodings.emplace_back();
1475 // TODO(deadbeef): Return stream-specific parameters.
1476 rtp_params.encodings[0].ssrc = rtc::Optional<uint32_t>(ssrc);
Taylor Brandstetterdb0cd9e2016-05-16 11:40:30 -07001477 }
1478
Taylor Brandstetterdb0cd9e2016-05-16 11:40:30 -07001479 for (const AudioCodec& codec : recv_codecs_) {
1480 rtp_params.codecs.push_back(codec.ToCodecParameters());
1481 }
1482 return rtp_params;
1483}
1484
1485bool WebRtcVoiceMediaChannel::SetRtpReceiveParameters(
1486 uint32_t ssrc,
1487 const webrtc::RtpParameters& parameters) {
1488 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
deadbeef3bc15102017-04-20 19:25:07 -07001489 // SSRC of 0 represents the default receive stream.
1490 if (ssrc == 0) {
1491 if (!default_sink_) {
Mirko Bonadei675513b2017-11-09 11:09:25 +01001492 RTC_LOG(LS_WARNING)
1493 << "Attempting to set RTP parameters for the default, "
1494 "unsignaled audio receive stream, but not yet "
1495 "configured to receive such a stream.";
deadbeef3bc15102017-04-20 19:25:07 -07001496 return false;
1497 }
1498 } else {
1499 auto it = recv_streams_.find(ssrc);
1500 if (it == recv_streams_.end()) {
Mirko Bonadei675513b2017-11-09 11:09:25 +01001501 RTC_LOG(LS_WARNING)
1502 << "Attempting to set RTP receive parameters for stream "
1503 << "with ssrc " << ssrc << " which doesn't exist.";
deadbeef3bc15102017-04-20 19:25:07 -07001504 return false;
1505 }
Taylor Brandstetterdb0cd9e2016-05-16 11:40:30 -07001506 }
1507
1508 webrtc::RtpParameters current_parameters = GetRtpReceiveParameters(ssrc);
1509 if (current_parameters != parameters) {
Mirko Bonadei675513b2017-11-09 11:09:25 +01001510 RTC_LOG(LS_ERROR) << "Changing the RTP receive parameters is currently "
1511 << "unsupported.";
Taylor Brandstetterdb0cd9e2016-05-16 11:40:30 -07001512 return false;
1513 }
1514 return true;
1515}
1516
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001517bool WebRtcVoiceMediaChannel::SetOptions(const AudioOptions& options) {
solenberg566ef242015-11-06 15:34:49 -08001518 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
Mirko Bonadei675513b2017-11-09 11:09:25 +01001519 RTC_LOG(LS_INFO) << "Setting voice channel options: " << options.ToString();
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001520
1521 // We retain all of the existing options, and apply the given ones
1522 // on top. This means there is no way to "clear" options such that
1523 // they go back to the engine default.
1524 options_.SetAll(options);
solenberg246b8172015-12-08 09:50:23 -08001525 if (!engine()->ApplyOptions(options_)) {
Mirko Bonadei675513b2017-11-09 11:09:25 +01001526 RTC_LOG(LS_WARNING)
1527 << "Failed to apply engine options during channel SetOptions.";
solenberg246b8172015-12-08 09:50:23 -08001528 return false;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001529 }
minyue6b825df2016-10-31 04:08:32 -07001530
ossu20a4b3f2017-04-27 02:08:52 -07001531 rtc::Optional<std::string> audio_network_adaptor_config =
minyue6b825df2016-10-31 04:08:32 -07001532 GetAudioNetworkAdaptorConfig(options_);
1533 for (auto& it : send_streams_) {
ossu20a4b3f2017-04-27 02:08:52 -07001534 it.second->SetAudioNetworkAdaptorConfig(audio_network_adaptor_config);
minyue6b825df2016-10-31 04:08:32 -07001535 }
1536
Mirko Bonadei675513b2017-11-09 11:09:25 +01001537 RTC_LOG(LS_INFO) << "Set voice channel options. Current options: "
1538 << options_.ToString();
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001539 return true;
1540}
1541
1542bool WebRtcVoiceMediaChannel::SetRecvCodecs(
1543 const std::vector<AudioCodec>& codecs) {
solenberg566ef242015-11-06 15:34:49 -08001544 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
solenberg8fb30c32015-10-13 03:06:58 -07001545
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001546 // Set the payload types to be used for incoming media.
Mirko Bonadei675513b2017-11-09 11:09:25 +01001547 RTC_LOG(LS_INFO) << "Setting receive voice codecs.";
solenberg0b675462015-10-09 01:37:09 -07001548
1549 if (!VerifyUniquePayloadTypes(codecs)) {
Mirko Bonadei675513b2017-11-09 11:09:25 +01001550 RTC_LOG(LS_ERROR) << "Codec payload types overlap.";
solenberg0b675462015-10-09 01:37:09 -07001551 return false;
1552 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001553
kwibergd32bf752017-01-19 07:03:59 -08001554 // Create a payload type -> SdpAudioFormat map with all the decoders. Fail
1555 // unless the factory claims to support all decoders.
1556 std::map<int, webrtc::SdpAudioFormat> decoder_map;
1557 for (const AudioCodec& codec : codecs) {
deadbeefcb383672017-04-26 16:28:42 -07001558 // Log a warning if a codec's payload type is changing. This used to be
1559 // treated as an error. It's abnormal, but not really illegal.
1560 AudioCodec old_codec;
1561 if (FindCodec(recv_codecs_, codec, &old_codec) &&
1562 old_codec.id != codec.id) {
Mirko Bonadei675513b2017-11-09 11:09:25 +01001563 RTC_LOG(LS_WARNING) << codec.name << " mapped to a second payload type ("
1564 << codec.id << ", was already mapped to "
1565 << old_codec.id << ")";
deadbeefcb383672017-04-26 16:28:42 -07001566 }
kwibergd32bf752017-01-19 07:03:59 -08001567 auto format = AudioCodecToSdpAudioFormat(codec);
1568 if (!IsCodec(codec, "cn") && !IsCodec(codec, "telephone-event") &&
1569 !engine()->decoder_factory_->IsSupportedDecoder(format)) {
Mirko Bonadei675513b2017-11-09 11:09:25 +01001570 RTC_LOG(LS_ERROR) << "Unsupported codec: " << format;
kwibergd32bf752017-01-19 07:03:59 -08001571 return false;
1572 }
deadbeefcb383672017-04-26 16:28:42 -07001573 // We allow adding new codecs but don't allow changing the payload type of
1574 // codecs that are already configured since we might already be receiving
1575 // packets with that payload type. See RFC3264, Section 8.3.2.
1576 // TODO(deadbeef): Also need to check for clashes with previously mapped
1577 // payload types, and not just currently mapped ones. For example, this
1578 // should be illegal:
1579 // 1. {100: opus/48000/2, 101: ISAC/16000}
1580 // 2. {100: opus/48000/2}
1581 // 3. {100: opus/48000/2, 101: ISAC/32000}
1582 // Though this check really should happen at a higher level, since this
1583 // conflict could happen between audio and video codecs.
1584 auto existing = decoder_map_.find(codec.id);
1585 if (existing != decoder_map_.end() && !existing->second.Matches(format)) {
Mirko Bonadei675513b2017-11-09 11:09:25 +01001586 RTC_LOG(LS_ERROR) << "Attempting to use payload type " << codec.id
1587 << " for " << codec.name
1588 << ", but it is already used for "
1589 << existing->second.name;
deadbeefcb383672017-04-26 16:28:42 -07001590 return false;
1591 }
kwibergd32bf752017-01-19 07:03:59 -08001592 decoder_map.insert({codec.id, std::move(format)});
1593 }
1594
deadbeefcb383672017-04-26 16:28:42 -07001595 if (decoder_map == decoder_map_) {
1596 // There's nothing new to configure.
1597 return true;
1598 }
1599
kwiberg37b8b112016-11-03 02:46:53 -07001600 if (playout_) {
1601 // Receive codecs can not be changed while playing. So we temporarily
1602 // pause playout.
1603 ChangePlayout(false);
1604 }
1605
kwiberg1c07c702017-03-27 07:15:49 -07001606 decoder_map_ = std::move(decoder_map);
kwibergd32bf752017-01-19 07:03:59 -08001607 for (auto& kv : recv_streams_) {
kwiberg1c07c702017-03-27 07:15:49 -07001608 kv.second->RecreateAudioReceiveStream(decoder_map_);
solenberg26c8c912015-11-27 04:00:25 -08001609 }
kwibergd32bf752017-01-19 07:03:59 -08001610 recv_codecs_ = codecs;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001611
kwiberg37b8b112016-11-03 02:46:53 -07001612 if (desired_playout_ && !playout_) {
1613 ChangePlayout(desired_playout_);
1614 }
kwibergd32bf752017-01-19 07:03:59 -08001615 return true;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001616}
1617
solenberg72e29d22016-03-08 06:35:16 -08001618// Utility function called from SetSendParameters() to extract current send
1619// codec settings from the given list of codecs (originally from SDP). Both send
1620// and receive streams may be reconfigured based on the new settings.
wu@webrtc.orgcadf9042013-08-30 21:24:16 +00001621bool WebRtcVoiceMediaChannel::SetSendCodecs(
1622 const std::vector<AudioCodec>& codecs) {
solenberg566ef242015-11-06 15:34:49 -08001623 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
Fredrik Solenbergb5727682015-12-04 15:22:19 +01001624 dtmf_payload_type_ = rtc::Optional<int>();
solenbergffbbcac2016-11-17 05:25:37 -08001625 dtmf_payload_freq_ = -1;
1626
1627 // Validate supplied codecs list.
1628 for (const AudioCodec& codec : codecs) {
1629 // TODO(solenberg): Validate more aspects of input - that payload types
1630 // don't overlap, remove redundant/unsupported codecs etc -
1631 // the same way it is done for RtpHeaderExtensions.
1632 if (codec.id < kMinPayloadType || codec.id > kMaxPayloadType) {
Mirko Bonadei675513b2017-11-09 11:09:25 +01001633 RTC_LOG(LS_WARNING) << "Codec payload type out of range: "
1634 << ToString(codec);
solenbergffbbcac2016-11-17 05:25:37 -08001635 return false;
1636 }
1637 }
1638
1639 // Find PT of telephone-event codec with lowest clockrate, as a fallback, in
1640 // case we don't have a DTMF codec with a rate matching the send codec's, or
1641 // if this function returns early.
1642 std::vector<AudioCodec> dtmf_codecs;
Fredrik Solenbergaf9fb212015-08-26 10:45:53 +02001643 for (const AudioCodec& codec : codecs) {
Fredrik Solenbergaf9fb212015-08-26 10:45:53 +02001644 if (IsCodec(codec, kDtmfCodecName)) {
solenbergffbbcac2016-11-17 05:25:37 -08001645 dtmf_codecs.push_back(codec);
1646 if (!dtmf_payload_type_ || codec.clockrate < dtmf_payload_freq_) {
1647 dtmf_payload_type_ = rtc::Optional<int>(codec.id);
1648 dtmf_payload_freq_ = codec.clockrate;
solenberg31642aa2016-03-14 08:00:37 -07001649 }
wu@webrtc.orgcadf9042013-08-30 21:24:16 +00001650 }
1651 }
1652
ossu20a4b3f2017-04-27 02:08:52 -07001653 // Scan through the list to figure out the codec to use for sending.
1654 rtc::Optional<webrtc::AudioSendStream::Config::SendCodecSpec> send_codec_spec;
stefan1ccf73f2017-03-27 03:51:18 -07001655 webrtc::Call::Config::BitrateConfig bitrate_config;
ossu20a4b3f2017-04-27 02:08:52 -07001656 rtc::Optional<webrtc::AudioCodecInfo> voice_codec_info;
1657 for (const AudioCodec& voice_codec : codecs) {
1658 if (!(IsCodec(voice_codec, kCnCodecName) ||
1659 IsCodec(voice_codec, kDtmfCodecName) ||
1660 IsCodec(voice_codec, kRedCodecName))) {
1661 webrtc::SdpAudioFormat format(voice_codec.name, voice_codec.clockrate,
1662 voice_codec.channels, voice_codec.params);
solenberg72e29d22016-03-08 06:35:16 -08001663
ossu20a4b3f2017-04-27 02:08:52 -07001664 voice_codec_info = engine()->encoder_factory_->QueryAudioEncoder(format);
1665 if (!voice_codec_info) {
Mirko Bonadei675513b2017-11-09 11:09:25 +01001666 RTC_LOG(LS_WARNING) << "Unknown codec " << ToString(voice_codec);
solenberg72e29d22016-03-08 06:35:16 -08001667 continue;
1668 }
1669
ossu20a4b3f2017-04-27 02:08:52 -07001670 send_codec_spec =
1671 rtc::Optional<webrtc::AudioSendStream::Config::SendCodecSpec>(
1672 {voice_codec.id, format});
1673 if (voice_codec.bitrate > 0) {
1674 send_codec_spec->target_bitrate_bps =
1675 rtc::Optional<int>(voice_codec.bitrate);
1676 }
1677 send_codec_spec->transport_cc_enabled = HasTransportCc(voice_codec);
1678 send_codec_spec->nack_enabled = HasNack(voice_codec);
1679 bitrate_config = GetBitrateConfigForCodec(voice_codec);
1680 break;
1681 }
1682 }
1683
1684 if (!send_codec_spec) {
1685 return false;
1686 }
1687
1688 RTC_DCHECK(voice_codec_info);
1689 if (voice_codec_info->allow_comfort_noise) {
1690 // Loop through the codecs list again to find the CN codec.
1691 // TODO(solenberg): Break out into a separate function?
1692 for (const AudioCodec& cn_codec : codecs) {
ossu0c4b8492017-03-02 11:03:25 -08001693 if (IsCodec(cn_codec, kCnCodecName) &&
ossu20a4b3f2017-04-27 02:08:52 -07001694 cn_codec.clockrate == send_codec_spec->format.clockrate_hz) {
ossu0c4b8492017-03-02 11:03:25 -08001695 switch (cn_codec.clockrate) {
solenberg72e29d22016-03-08 06:35:16 -08001696 case 8000:
1697 case 16000:
1698 case 32000:
ossu20a4b3f2017-04-27 02:08:52 -07001699 send_codec_spec->cng_payload_type = rtc::Optional<int>(cn_codec.id);
solenberg72e29d22016-03-08 06:35:16 -08001700 break;
1701 default:
Mirko Bonadei675513b2017-11-09 11:09:25 +01001702 RTC_LOG(LS_WARNING)
1703 << "CN frequency " << cn_codec.clockrate << " not supported.";
ossu20a4b3f2017-04-27 02:08:52 -07001704 break;
solenberg72e29d22016-03-08 06:35:16 -08001705 }
solenberg72e29d22016-03-08 06:35:16 -08001706 break;
1707 }
1708 }
solenbergffbbcac2016-11-17 05:25:37 -08001709
1710 // Find the telephone-event PT exactly matching the preferred send codec.
1711 for (const AudioCodec& dtmf_codec : dtmf_codecs) {
ossu20a4b3f2017-04-27 02:08:52 -07001712 if (dtmf_codec.clockrate == send_codec_spec->format.clockrate_hz) {
solenbergffbbcac2016-11-17 05:25:37 -08001713 dtmf_payload_type_ = rtc::Optional<int>(dtmf_codec.id);
1714 dtmf_payload_freq_ = dtmf_codec.clockrate;
1715 break;
1716 }
1717 }
solenberg72e29d22016-03-08 06:35:16 -08001718 }
1719
solenberg971cab02016-06-14 10:02:41 -07001720 if (send_codec_spec_ != send_codec_spec) {
1721 send_codec_spec_ = std::move(send_codec_spec);
stefan13f1a0a2016-11-30 07:22:58 -08001722 // Apply new settings to all streams.
solenberg971cab02016-06-14 10:02:41 -07001723 for (const auto& kv : send_streams_) {
ossu20a4b3f2017-04-27 02:08:52 -07001724 kv.second->SetSendCodecSpec(*send_codec_spec_);
wu@webrtc.orgcadf9042013-08-30 21:24:16 +00001725 }
stefan13f1a0a2016-11-30 07:22:58 -08001726 } else {
1727 // If the codec isn't changing, set the start bitrate to -1 which means
1728 // "unchanged" so that BWE isn't affected.
stefan1ccf73f2017-03-27 03:51:18 -07001729 bitrate_config.start_bitrate_bps = -1;
wu@webrtc.orgcadf9042013-08-30 21:24:16 +00001730 }
stefan1ccf73f2017-03-27 03:51:18 -07001731 call_->SetBitrateConfig(bitrate_config);
wu@webrtc.orgcadf9042013-08-30 21:24:16 +00001732
solenberg8189b022016-06-14 12:13:00 -07001733 // Check if the transport cc feedback or NACK status has changed on the
1734 // preferred send codec, and in that case reconfigure all receive streams.
ossu20a4b3f2017-04-27 02:08:52 -07001735 if (recv_transport_cc_enabled_ != send_codec_spec_->transport_cc_enabled ||
1736 recv_nack_enabled_ != send_codec_spec_->nack_enabled) {
Mirko Bonadei675513b2017-11-09 11:09:25 +01001737 RTC_LOG(LS_INFO) << "Recreate all the receive streams because the send "
1738 "codec has changed.";
ossu20a4b3f2017-04-27 02:08:52 -07001739 recv_transport_cc_enabled_ = send_codec_spec_->transport_cc_enabled;
1740 recv_nack_enabled_ = send_codec_spec_->nack_enabled;
solenberg72e29d22016-03-08 06:35:16 -08001741 for (auto& kv : recv_streams_) {
solenberg8189b022016-06-14 12:13:00 -07001742 kv.second->RecreateAudioReceiveStream(recv_transport_cc_enabled_,
1743 recv_nack_enabled_);
solenberg72e29d22016-03-08 06:35:16 -08001744 }
1745 }
1746
Taylor Brandstetter0cd086b2016-04-20 16:23:10 -07001747 send_codecs_ = codecs;
solenberg72e29d22016-03-08 06:35:16 -08001748 return true;
1749}
1750
aleloi84ef6152016-08-04 05:28:21 -07001751void WebRtcVoiceMediaChannel::SetPlayout(bool playout) {
kwiberg37b8b112016-11-03 02:46:53 -07001752 desired_playout_ = playout;
1753 return ChangePlayout(desired_playout_);
1754}
1755
1756void WebRtcVoiceMediaChannel::ChangePlayout(bool playout) {
1757 TRACE_EVENT0("webrtc", "WebRtcVoiceMediaChannel::ChangePlayout");
solenberg566ef242015-11-06 15:34:49 -08001758 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001759 if (playout_ == playout) {
aleloi84ef6152016-08-04 05:28:21 -07001760 return;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001761 }
1762
aleloi84ef6152016-08-04 05:28:21 -07001763 for (const auto& kv : recv_streams_) {
1764 kv.second->SetPlayout(playout);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001765 }
solenberg1ac56142015-10-13 03:58:19 -07001766 playout_ = playout;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001767}
1768
Taylor Brandstetter1a018dc2016-03-08 12:37:39 -08001769void WebRtcVoiceMediaChannel::SetSend(bool send) {
Peter Boströmca8b4042016-03-08 14:24:13 -08001770 TRACE_EVENT0("webrtc", "WebRtcVoiceMediaChannel::SetSend");
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001771 if (send_ == send) {
Taylor Brandstetter1a018dc2016-03-08 12:37:39 -08001772 return;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001773 }
1774
solenbergd53a3f92016-04-14 13:56:37 -07001775 // Apply channel specific options, and initialize the ADM for recording (this
1776 // may take time on some platforms, e.g. Android).
Taylor Brandstetter1a018dc2016-03-08 12:37:39 -08001777 if (send) {
solenberg63b34542015-09-29 06:06:31 -07001778 engine()->ApplyOptions(options_);
solenbergd53a3f92016-04-14 13:56:37 -07001779
1780 // InitRecording() may return an error if the ADM is already recording.
1781 if (!engine()->adm()->RecordingIsInitialized() &&
1782 !engine()->adm()->Recording()) {
1783 if (engine()->adm()->InitRecording() != 0) {
Mirko Bonadei675513b2017-11-09 11:09:25 +01001784 RTC_LOG(LS_WARNING) << "Failed to initialize recording";
solenbergd53a3f92016-04-14 13:56:37 -07001785 }
1786 }
solenberg63b34542015-09-29 06:06:31 -07001787 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001788
wu@webrtc.org9dba5252013-08-05 20:36:57 +00001789 // Change the settings on each send channel.
Taylor Brandstetter1a018dc2016-03-08 12:37:39 -08001790 for (auto& kv : send_streams_) {
1791 kv.second->SetSend(send);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001792 }
wu@webrtc.org9dba5252013-08-05 20:36:57 +00001793
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001794 send_ = send;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001795}
1796
Peter Boström0c4e06b2015-10-07 12:23:21 +02001797bool WebRtcVoiceMediaChannel::SetAudioSend(uint32_t ssrc,
1798 bool enable,
solenberg1dd98f32015-09-10 01:57:14 -07001799 const AudioOptions* options,
Taylor Brandstetter1a018dc2016-03-08 12:37:39 -08001800 AudioSource* source) {
solenberg566ef242015-11-06 15:34:49 -08001801 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
solenberg1dd98f32015-09-10 01:57:14 -07001802 // TODO(solenberg): The state change should be fully rolled back if any one of
1803 // these calls fail.
Taylor Brandstetter1a018dc2016-03-08 12:37:39 -08001804 if (!SetLocalSource(ssrc, source)) {
solenberg1dd98f32015-09-10 01:57:14 -07001805 return false;
1806 }
solenbergdfc8f4f2015-10-01 02:31:10 -07001807 if (!MuteStream(ssrc, !enable)) {
solenberg1dd98f32015-09-10 01:57:14 -07001808 return false;
1809 }
solenbergdfc8f4f2015-10-01 02:31:10 -07001810 if (enable && options) {
solenberg1dd98f32015-09-10 01:57:14 -07001811 return SetOptions(*options);
1812 }
1813 return true;
1814}
1815
solenberg0a617e22015-10-20 15:49:38 -07001816int WebRtcVoiceMediaChannel::CreateVoEChannel() {
1817 int id = engine()->CreateVoEChannel();
1818 if (id == -1) {
Mirko Bonadei675513b2017-11-09 11:09:25 +01001819 RTC_LOG(LS_WARNING) << "CreateVoEChannel() failed.";
solenberg0a617e22015-10-20 15:49:38 -07001820 return -1;
wu@webrtc.org9dba5252013-08-05 20:36:57 +00001821 }
mflodman3d7db262016-04-29 00:57:13 -07001822
solenberg0a617e22015-10-20 15:49:38 -07001823 return id;
wu@webrtc.org9dba5252013-08-05 20:36:57 +00001824}
1825
solenberg7add0582015-11-20 09:59:34 -08001826bool WebRtcVoiceMediaChannel::DeleteVoEChannel(int channel) {
wu@webrtc.org9dba5252013-08-05 20:36:57 +00001827 if (engine()->voe()->base()->DeleteChannel(channel) == -1) {
Mirko Bonadei675513b2017-11-09 11:09:25 +01001828 RTC_LOG(LS_WARNING) << "DeleteChannel(" << channel << ") failed.";
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001829 return false;
1830 }
wu@webrtc.org9dba5252013-08-05 20:36:57 +00001831 return true;
1832}
henrike@webrtc.org1e09a712013-07-26 19:17:59 +00001833
wu@webrtc.org9dba5252013-08-05 20:36:57 +00001834bool WebRtcVoiceMediaChannel::AddSendStream(const StreamParams& sp) {
Peter Boströmca8b4042016-03-08 14:24:13 -08001835 TRACE_EVENT0("webrtc", "WebRtcVoiceMediaChannel::AddSendStream");
solenberg566ef242015-11-06 15:34:49 -08001836 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
Mirko Bonadei675513b2017-11-09 11:09:25 +01001837 RTC_LOG(LS_INFO) << "AddSendStream: " << sp.ToString();
solenberg0a617e22015-10-20 15:49:38 -07001838
1839 uint32_t ssrc = sp.first_ssrc();
1840 RTC_DCHECK(0 != ssrc);
1841
1842 if (GetSendChannelId(ssrc) != -1) {
Mirko Bonadei675513b2017-11-09 11:09:25 +01001843 RTC_LOG(LS_ERROR) << "Stream already exists with ssrc " << ssrc;
wu@webrtc.org9dba5252013-08-05 20:36:57 +00001844 return false;
1845 }
1846
solenberg0a617e22015-10-20 15:49:38 -07001847 // Create a new channel for sending audio data.
1848 int channel = CreateVoEChannel();
1849 if (channel == -1) {
1850 return false;
wu@webrtc.org9dba5252013-08-05 20:36:57 +00001851 }
wu@webrtc.org9dba5252013-08-05 20:36:57 +00001852
solenbergc96df772015-10-21 13:01:53 -07001853 // Save the channel to send_streams_, so that RemoveSendStream() can still
wu@webrtc.org9dba5252013-08-05 20:36:57 +00001854 // delete the channel in case failure happens below.
mallinath@webrtc.org67ee6b92014-02-03 16:57:16 +00001855 webrtc::AudioTransport* audio_transport =
1856 engine()->voe()->base()->audio_transport();
mflodman3d7db262016-04-29 00:57:13 -07001857
minyue6b825df2016-10-31 04:08:32 -07001858 rtc::Optional<std::string> audio_network_adaptor_config =
1859 GetAudioNetworkAdaptorConfig(options_);
skvlade0d46372016-04-07 22:59:22 -07001860 WebRtcAudioSendStream* stream = new WebRtcAudioSendStream(
Alex Narestb3944f02017-10-13 14:56:18 +02001861 channel, audio_transport, ssrc, sp.cname, sp.id, send_codec_spec_,
minyue6b825df2016-10-31 04:08:32 -07001862 send_rtp_extensions_, max_send_bitrate_bps_, audio_network_adaptor_config,
ossu20a4b3f2017-04-27 02:08:52 -07001863 call_, this, engine()->encoder_factory_);
skvlade0d46372016-04-07 22:59:22 -07001864 send_streams_.insert(std::make_pair(ssrc, stream));
wu@webrtc.org9dba5252013-08-05 20:36:57 +00001865
solenberg4a0f7b52016-06-16 13:07:33 -07001866 // At this point the stream's local SSRC has been updated. If it is the first
1867 // send stream, make sure that all the receive streams are updated with the
1868 // same SSRC in order to send receiver reports.
solenbergc96df772015-10-21 13:01:53 -07001869 if (send_streams_.size() == 1) {
solenberg0a617e22015-10-20 15:49:38 -07001870 receiver_reports_ssrc_ = ssrc;
solenberg4a0f7b52016-06-16 13:07:33 -07001871 for (const auto& kv : recv_streams_) {
1872 // TODO(solenberg): Allow applications to set the RTCP SSRC of receive
1873 // streams instead, so we can avoid recreating the streams here.
1874 kv.second->RecreateAudioReceiveStream(ssrc);
wu@webrtc.org9dba5252013-08-05 20:36:57 +00001875 }
1876 }
1877
Taylor Brandstetter1a018dc2016-03-08 12:37:39 -08001878 send_streams_[ssrc]->SetSend(send_);
1879 return true;
wu@webrtc.org9dba5252013-08-05 20:36:57 +00001880}
1881
Peter Boström0c4e06b2015-10-07 12:23:21 +02001882bool WebRtcVoiceMediaChannel::RemoveSendStream(uint32_t ssrc) {
Peter Boströmca8b4042016-03-08 14:24:13 -08001883 TRACE_EVENT0("webrtc", "WebRtcVoiceMediaChannel::RemoveSendStream");
solenberg566ef242015-11-06 15:34:49 -08001884 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
Mirko Bonadei675513b2017-11-09 11:09:25 +01001885 RTC_LOG(LS_INFO) << "RemoveSendStream: " << ssrc;
solenberg3a941542015-11-16 07:34:50 -08001886
solenbergc96df772015-10-21 13:01:53 -07001887 auto it = send_streams_.find(ssrc);
1888 if (it == send_streams_.end()) {
Mirko Bonadei675513b2017-11-09 11:09:25 +01001889 RTC_LOG(LS_WARNING) << "Try to remove stream with ssrc " << ssrc
1890 << " which doesn't exist.";
wu@webrtc.org9dba5252013-08-05 20:36:57 +00001891 return false;
1892 }
1893
Taylor Brandstetter1a018dc2016-03-08 12:37:39 -08001894 it->second->SetSend(false);
wu@webrtc.org9dba5252013-08-05 20:36:57 +00001895
solenberg7602aab2016-11-14 11:30:07 -08001896 // TODO(solenberg): If we're removing the receiver_reports_ssrc_ stream, find
1897 // the first active send stream and use that instead, reassociating receive
1898 // streams.
1899
solenberg7add0582015-11-20 09:59:34 -08001900 // Clean up and delete the send stream+channel.
Taylor Brandstetter1a018dc2016-03-08 12:37:39 -08001901 int channel = it->second->channel();
Mirko Bonadei675513b2017-11-09 11:09:25 +01001902 RTC_LOG(LS_INFO) << "Removing audio send stream " << ssrc
1903 << " with VoiceEngine channel #" << channel << ".";
solenberg7add0582015-11-20 09:59:34 -08001904 delete it->second;
1905 send_streams_.erase(it);
1906 if (!DeleteVoEChannel(channel)) {
solenberg0a617e22015-10-20 15:49:38 -07001907 return false;
wu@webrtc.org9dba5252013-08-05 20:36:57 +00001908 }
solenbergc96df772015-10-21 13:01:53 -07001909 if (send_streams_.empty()) {
Taylor Brandstetter1a018dc2016-03-08 12:37:39 -08001910 SetSend(false);
solenberg0a617e22015-10-20 15:49:38 -07001911 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001912 return true;
1913}
1914
1915bool WebRtcVoiceMediaChannel::AddRecvStream(const StreamParams& sp) {
Peter Boströmca8b4042016-03-08 14:24:13 -08001916 TRACE_EVENT0("webrtc", "WebRtcVoiceMediaChannel::AddRecvStream");
solenberg566ef242015-11-06 15:34:49 -08001917 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
Mirko Bonadei675513b2017-11-09 11:09:25 +01001918 RTC_LOG(LS_INFO) << "AddRecvStream: " << sp.ToString();
solenbergd97ec302015-10-07 01:40:33 -07001919
solenberg0b675462015-10-09 01:37:09 -07001920 if (!ValidateStreamParams(sp)) {
wu@webrtc.org78187522013-10-07 23:32:02 +00001921 return false;
1922 }
1923
solenberg7add0582015-11-20 09:59:34 -08001924 const uint32_t ssrc = sp.first_ssrc();
solenberg0b675462015-10-09 01:37:09 -07001925 if (ssrc == 0) {
Mirko Bonadei675513b2017-11-09 11:09:25 +01001926 RTC_LOG(LS_WARNING) << "AddRecvStream with ssrc==0 is not supported.";
solenberg0b675462015-10-09 01:37:09 -07001927 return false;
1928 }
1929
solenberg2100c0b2017-03-01 11:29:29 -08001930 // If this stream was previously received unsignaled, we promote it, possibly
1931 // recreating the AudioReceiveStream, if sync_label has changed.
1932 if (MaybeDeregisterUnsignaledRecvStream(ssrc)) {
solenberg4904fb62017-02-17 12:01:14 -08001933 recv_streams_[ssrc]->MaybeRecreateAudioReceiveStream(sp.sync_label);
solenberg4904fb62017-02-17 12:01:14 -08001934 return true;
solenberg1ac56142015-10-13 03:58:19 -07001935 }
solenberg0b675462015-10-09 01:37:09 -07001936
solenberg7add0582015-11-20 09:59:34 -08001937 if (GetReceiveChannelId(ssrc) != -1) {
Mirko Bonadei675513b2017-11-09 11:09:25 +01001938 RTC_LOG(LS_ERROR) << "Stream already exists with ssrc " << ssrc;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001939 return false;
1940 }
Fredrik Solenberg4b60c732015-05-07 14:07:48 +02001941
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001942 // Create a new channel for receiving audio data.
solenberg7add0582015-11-20 09:59:34 -08001943 const int channel = CreateVoEChannel();
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001944 if (channel == -1) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001945 return false;
1946 }
Minyue2013aec2015-05-13 14:14:42 +02001947
stefanba4c0e42016-02-04 04:12:24 -08001948 recv_streams_.insert(std::make_pair(
kwiberg1c07c702017-03-27 07:15:49 -07001949 ssrc,
1950 new WebRtcAudioReceiveStream(
1951 channel, ssrc, receiver_reports_ssrc_, recv_transport_cc_enabled_,
1952 recv_nack_enabled_, sp.sync_label, recv_rtp_extensions_, call_, this,
1953 engine()->decoder_factory_, decoder_map_)));
aleloi84ef6152016-08-04 05:28:21 -07001954 recv_streams_[ssrc]->SetPlayout(playout_);
solenberg7add0582015-11-20 09:59:34 -08001955
solenberg1ac56142015-10-13 03:58:19 -07001956 return true;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001957}
1958
Peter Boström0c4e06b2015-10-07 12:23:21 +02001959bool WebRtcVoiceMediaChannel::RemoveRecvStream(uint32_t ssrc) {
Peter Boströmca8b4042016-03-08 14:24:13 -08001960 TRACE_EVENT0("webrtc", "WebRtcVoiceMediaChannel::RemoveRecvStream");
solenberg566ef242015-11-06 15:34:49 -08001961 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
Mirko Bonadei675513b2017-11-09 11:09:25 +01001962 RTC_LOG(LS_INFO) << "RemoveRecvStream: " << ssrc;
solenbergd97ec302015-10-07 01:40:33 -07001963
solenberg7add0582015-11-20 09:59:34 -08001964 const auto it = recv_streams_.find(ssrc);
1965 if (it == recv_streams_.end()) {
Mirko Bonadei675513b2017-11-09 11:09:25 +01001966 RTC_LOG(LS_WARNING) << "Try to remove stream with ssrc " << ssrc
1967 << " which doesn't exist.";
henrike@webrtc.org1e09a712013-07-26 19:17:59 +00001968 return false;
wu@webrtc.org9dba5252013-08-05 20:36:57 +00001969 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001970
solenberg2100c0b2017-03-01 11:29:29 -08001971 MaybeDeregisterUnsignaledRecvStream(ssrc);
henrike@webrtc.org1e09a712013-07-26 19:17:59 +00001972
solenberg7add0582015-11-20 09:59:34 -08001973 const int channel = it->second->channel();
1974
1975 // Clean up and delete the receive stream+channel.
Mirko Bonadei675513b2017-11-09 11:09:25 +01001976 RTC_LOG(LS_INFO) << "Removing audio receive stream " << ssrc
1977 << " with VoiceEngine channel #" << channel << ".";
Tommif888bb52015-12-12 01:37:01 +01001978 it->second->SetRawAudioSink(nullptr);
solenberg7add0582015-11-20 09:59:34 -08001979 delete it->second;
1980 recv_streams_.erase(it);
1981 return DeleteVoEChannel(channel);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001982}
1983
Taylor Brandstetter1a018dc2016-03-08 12:37:39 -08001984bool WebRtcVoiceMediaChannel::SetLocalSource(uint32_t ssrc,
1985 AudioSource* source) {
solenbergc96df772015-10-21 13:01:53 -07001986 auto it = send_streams_.find(ssrc);
1987 if (it == send_streams_.end()) {
Taylor Brandstetter1a018dc2016-03-08 12:37:39 -08001988 if (source) {
1989 // Return an error if trying to set a valid source with an invalid ssrc.
Mirko Bonadei675513b2017-11-09 11:09:25 +01001990 RTC_LOG(LS_ERROR) << "SetLocalSource failed with ssrc " << ssrc;
wu@webrtc.org9dba5252013-08-05 20:36:57 +00001991 return false;
1992 }
1993
1994 // The channel likely has gone away, do nothing.
henrike@webrtc.org1e09a712013-07-26 19:17:59 +00001995 return true;
henrike@webrtc.org1e09a712013-07-26 19:17:59 +00001996 }
1997
Taylor Brandstetter1a018dc2016-03-08 12:37:39 -08001998 if (source) {
1999 it->second->SetSource(source);
solenberg1ac56142015-10-13 03:58:19 -07002000 } else {
Taylor Brandstetter1a018dc2016-03-08 12:37:39 -08002001 it->second->ClearSource();
solenberg1ac56142015-10-13 03:58:19 -07002002 }
henrike@webrtc.org1e09a712013-07-26 19:17:59 +00002003
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002004 return true;
2005}
2006
solenberg796b8f92017-03-01 17:02:23 -08002007// TODO(solenberg): Remove, once AudioMonitor is gone.
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002008bool WebRtcVoiceMediaChannel::GetActiveStreams(
2009 AudioInfo::StreamList* actives) {
solenberg566ef242015-11-06 15:34:49 -08002010 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002011 actives->clear();
solenberg7add0582015-11-20 09:59:34 -08002012 for (const auto& ch : recv_streams_) {
solenberg796b8f92017-03-01 17:02:23 -08002013 int level = ch.second->GetOutputLevel();
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002014 if (level > 0) {
Fredrik Solenbergaf9fb212015-08-26 10:45:53 +02002015 actives->push_back(std::make_pair(ch.first, level));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002016 }
2017 }
2018 return true;
2019}
2020
solenberg796b8f92017-03-01 17:02:23 -08002021// TODO(solenberg): Remove, once AudioMonitor is gone.
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002022int WebRtcVoiceMediaChannel::GetOutputLevel() {
solenberg566ef242015-11-06 15:34:49 -08002023 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
solenberg1ac56142015-10-13 03:58:19 -07002024 int highest = 0;
solenberg7add0582015-11-20 09:59:34 -08002025 for (const auto& ch : recv_streams_) {
solenberg796b8f92017-03-01 17:02:23 -08002026 highest = std::max(ch.second->GetOutputLevel(), highest);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002027 }
2028 return highest;
2029}
2030
solenberg4bac9c52015-10-09 02:32:53 -07002031bool WebRtcVoiceMediaChannel::SetOutputVolume(uint32_t ssrc, double volume) {
solenberg566ef242015-11-06 15:34:49 -08002032 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
solenberg2100c0b2017-03-01 11:29:29 -08002033 std::vector<uint32_t> ssrcs(1, ssrc);
deadbeef3bc15102017-04-20 19:25:07 -07002034 // SSRC of 0 represents the default receive stream.
solenberg1ac56142015-10-13 03:58:19 -07002035 if (ssrc == 0) {
2036 default_recv_volume_ = volume;
solenberg2100c0b2017-03-01 11:29:29 -08002037 ssrcs = unsignaled_recv_ssrcs_;
2038 }
2039 for (uint32_t ssrc : ssrcs) {
2040 const auto it = recv_streams_.find(ssrc);
2041 if (it == recv_streams_.end()) {
Mirko Bonadei675513b2017-11-09 11:09:25 +01002042 RTC_LOG(LS_WARNING) << "SetOutputVolume: no recv stream " << ssrc;
solenberg2100c0b2017-03-01 11:29:29 -08002043 return false;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002044 }
solenberg2100c0b2017-03-01 11:29:29 -08002045 it->second->SetOutputVolume(volume);
Mirko Bonadei675513b2017-11-09 11:09:25 +01002046 RTC_LOG(LS_INFO) << "SetOutputVolume() to " << volume
2047 << " for recv stream with ssrc " << ssrc;
solenberg1ac56142015-10-13 03:58:19 -07002048 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002049 return true;
2050}
2051
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002052bool WebRtcVoiceMediaChannel::CanInsertDtmf() {
Fredrik Solenbergb5727682015-12-04 15:22:19 +01002053 return dtmf_payload_type_ ? true : false;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002054}
2055
solenberg1d63dd02015-12-02 12:35:09 -08002056bool WebRtcVoiceMediaChannel::InsertDtmf(uint32_t ssrc, int event,
2057 int duration) {
solenberg566ef242015-11-06 15:34:49 -08002058 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
Mirko Bonadei675513b2017-11-09 11:09:25 +01002059 RTC_LOG(LS_INFO) << "WebRtcVoiceMediaChannel::InsertDtmf";
Fredrik Solenbergb5727682015-12-04 15:22:19 +01002060 if (!dtmf_payload_type_) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002061 return false;
2062 }
2063
Fredrik Solenbergb5727682015-12-04 15:22:19 +01002064 // Figure out which WebRtcAudioSendStream to send the event on.
2065 auto it = ssrc != 0 ? send_streams_.find(ssrc) : send_streams_.begin();
2066 if (it == send_streams_.end()) {
Mirko Bonadei675513b2017-11-09 11:09:25 +01002067 RTC_LOG(LS_WARNING) << "The specified ssrc " << ssrc << " is not in use.";
solenberg1d63dd02015-12-02 12:35:09 -08002068 return false;
2069 }
Fredrik Solenbergb5727682015-12-04 15:22:19 +01002070 if (event < kMinTelephoneEventCode ||
2071 event > kMaxTelephoneEventCode) {
Mirko Bonadei675513b2017-11-09 11:09:25 +01002072 RTC_LOG(LS_WARNING) << "DTMF event code " << event << " out of range.";
solenberg1d63dd02015-12-02 12:35:09 -08002073 return false;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002074 }
solenbergffbbcac2016-11-17 05:25:37 -08002075 RTC_DCHECK_NE(-1, dtmf_payload_freq_);
2076 return it->second->SendTelephoneEvent(*dtmf_payload_type_, dtmf_payload_freq_,
2077 event, duration);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002078}
2079
wu@webrtc.orga9890802013-12-13 00:21:03 +00002080void WebRtcVoiceMediaChannel::OnPacketReceived(
jbaucheec21bd2016-03-20 06:15:43 -07002081 rtc::CopyOnWriteBuffer* packet, const rtc::PacketTime& packet_time) {
solenberg566ef242015-11-06 15:34:49 -08002082 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
Fredrik Solenberg4b60c732015-05-07 14:07:48 +02002083
mflodman3d7db262016-04-29 00:57:13 -07002084 const webrtc::PacketTime webrtc_packet_time(packet_time.timestamp,
2085 packet_time.not_before);
2086 webrtc::PacketReceiver::DeliveryStatus delivery_result =
2087 call_->Receiver()->DeliverPacket(webrtc::MediaType::AUDIO,
2088 packet->cdata(), packet->size(),
2089 webrtc_packet_time);
mflodman3d7db262016-04-29 00:57:13 -07002090 if (delivery_result != webrtc::PacketReceiver::DELIVERY_UNKNOWN_SSRC) {
2091 return;
2092 }
2093
solenberg2100c0b2017-03-01 11:29:29 -08002094 // Create an unsignaled receive stream for this previously not received ssrc.
2095 // If there already is N unsignaled receive streams, delete the oldest.
mflodman3d7db262016-04-29 00:57:13 -07002096 // See: https://bugs.chromium.org/p/webrtc/issues/detail?id=5208
solenberg1ac56142015-10-13 03:58:19 -07002097 uint32_t ssrc = 0;
jbaucheec21bd2016-03-20 06:15:43 -07002098 if (!GetRtpSsrc(packet->cdata(), packet->size(), &ssrc)) {
solenberg1ac56142015-10-13 03:58:19 -07002099 return;
2100 }
solenberg2100c0b2017-03-01 11:29:29 -08002101 RTC_DCHECK(std::find(unsignaled_recv_ssrcs_.begin(),
2102 unsignaled_recv_ssrcs_.end(), ssrc) == unsignaled_recv_ssrcs_.end());
solenberg1ac56142015-10-13 03:58:19 -07002103
solenberg2100c0b2017-03-01 11:29:29 -08002104 // Add new stream.
mflodman3d7db262016-04-29 00:57:13 -07002105 StreamParams sp;
2106 sp.ssrcs.push_back(ssrc);
Mirko Bonadei675513b2017-11-09 11:09:25 +01002107 RTC_LOG(LS_INFO) << "Creating unsignaled receive stream for SSRC=" << ssrc;
mflodman3d7db262016-04-29 00:57:13 -07002108 if (!AddRecvStream(sp)) {
Mirko Bonadei675513b2017-11-09 11:09:25 +01002109 RTC_LOG(LS_WARNING) << "Could not create unsignaled receive stream.";
mflodman3d7db262016-04-29 00:57:13 -07002110 return;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002111 }
solenberg2100c0b2017-03-01 11:29:29 -08002112 unsignaled_recv_ssrcs_.push_back(ssrc);
2113 RTC_HISTOGRAM_COUNTS_LINEAR(
2114 "WebRTC.Audio.NumOfUnsignaledStreams", unsignaled_recv_ssrcs_.size(), 1,
2115 100, 101);
solenbergf748ca42017-02-06 13:03:19 -08002116
solenberg2100c0b2017-03-01 11:29:29 -08002117 // Remove oldest unsignaled stream, if we have too many.
2118 if (unsignaled_recv_ssrcs_.size() > kMaxUnsignaledRecvStreams) {
2119 uint32_t remove_ssrc = unsignaled_recv_ssrcs_.front();
Mirko Bonadei675513b2017-11-09 11:09:25 +01002120 RTC_LOG(LS_INFO) << "Removing unsignaled receive stream with SSRC="
2121 << remove_ssrc;
solenberg2100c0b2017-03-01 11:29:29 -08002122 RemoveRecvStream(remove_ssrc);
2123 }
2124 RTC_DCHECK_GE(kMaxUnsignaledRecvStreams, unsignaled_recv_ssrcs_.size());
2125
2126 SetOutputVolume(ssrc, default_recv_volume_);
2127
2128 // The default sink can only be attached to one stream at a time, so we hook
2129 // it up to the *latest* unsignaled stream we've seen, in order to support the
2130 // case where the SSRC of one unsignaled stream changes.
mflodman3d7db262016-04-29 00:57:13 -07002131 if (default_sink_) {
solenberg2100c0b2017-03-01 11:29:29 -08002132 for (uint32_t drop_ssrc : unsignaled_recv_ssrcs_) {
2133 auto it = recv_streams_.find(drop_ssrc);
2134 it->second->SetRawAudioSink(nullptr);
2135 }
mflodman3d7db262016-04-29 00:57:13 -07002136 std::unique_ptr<webrtc::AudioSinkInterface> proxy_sink(
2137 new ProxySink(default_sink_.get()));
solenberg2100c0b2017-03-01 11:29:29 -08002138 SetRawAudioSink(ssrc, std::move(proxy_sink));
mflodman3d7db262016-04-29 00:57:13 -07002139 }
solenberg2100c0b2017-03-01 11:29:29 -08002140
mflodman3d7db262016-04-29 00:57:13 -07002141 delivery_result = call_->Receiver()->DeliverPacket(webrtc::MediaType::AUDIO,
2142 packet->cdata(),
2143 packet->size(),
2144 webrtc_packet_time);
2145 RTC_DCHECK_NE(webrtc::PacketReceiver::DELIVERY_UNKNOWN_SSRC, delivery_result);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002146}
2147
wu@webrtc.orga9890802013-12-13 00:21:03 +00002148void WebRtcVoiceMediaChannel::OnRtcpReceived(
jbaucheec21bd2016-03-20 06:15:43 -07002149 rtc::CopyOnWriteBuffer* packet, const rtc::PacketTime& packet_time) {
solenberg566ef242015-11-06 15:34:49 -08002150 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
Fredrik Solenberg4b60c732015-05-07 14:07:48 +02002151
Fredrik Solenberg709ed672015-09-15 12:26:33 +02002152 // Forward packet to Call as well.
2153 const webrtc::PacketTime webrtc_packet_time(packet_time.timestamp,
2154 packet_time.not_before);
2155 call_->Receiver()->DeliverPacket(webrtc::MediaType::AUDIO,
jbaucheec21bd2016-03-20 06:15:43 -07002156 packet->cdata(), packet->size(), webrtc_packet_time);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002157}
2158
Honghai Zhangcc411c02016-03-29 17:27:21 -07002159void WebRtcVoiceMediaChannel::OnNetworkRouteChanged(
2160 const std::string& transport_name,
Honghai Zhang0e533ef2016-04-19 15:41:36 -07002161 const rtc::NetworkRoute& network_route) {
2162 call_->OnNetworkRouteChanged(transport_name, network_route);
Honghai Zhangcc411c02016-03-29 17:27:21 -07002163}
2164
Peter Boström0c4e06b2015-10-07 12:23:21 +02002165bool WebRtcVoiceMediaChannel::MuteStream(uint32_t ssrc, bool muted) {
solenberg566ef242015-11-06 15:34:49 -08002166 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
solenberg94218532016-06-16 10:53:22 -07002167 const auto it = send_streams_.find(ssrc);
2168 if (it == send_streams_.end()) {
Mirko Bonadei675513b2017-11-09 11:09:25 +01002169 RTC_LOG(LS_WARNING) << "The specified ssrc " << ssrc << " is not in use.";
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002170 return false;
2171 }
solenberg94218532016-06-16 10:53:22 -07002172 it->second->SetMuted(muted);
2173
2174 // TODO(solenberg):
buildbot@webrtc.org6b21b712014-07-31 15:08:53 +00002175 // We set the AGC to mute state only when all the channels are muted.
2176 // This implementation is not ideal, instead we should signal the AGC when
2177 // the mic channel is muted/unmuted. We can't do it today because there
2178 // is no good way to know which stream is mapping to the mic channel.
2179 bool all_muted = muted;
solenberg94218532016-06-16 10:53:22 -07002180 for (const auto& kv : send_streams_) {
2181 all_muted = all_muted && kv.second->muted();
buildbot@webrtc.org6b21b712014-07-31 15:08:53 +00002182 }
solenberg059fb442016-10-26 05:12:24 -07002183 engine()->apm()->set_output_will_be_muted(all_muted);
buildbot@webrtc.org6b21b712014-07-31 15:08:53 +00002184
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002185 return true;
2186}
2187
deadbeef80346142016-04-27 14:17:10 -07002188bool WebRtcVoiceMediaChannel::SetMaxSendBitrate(int bps) {
Mirko Bonadei675513b2017-11-09 11:09:25 +01002189 RTC_LOG(LS_INFO) << "WebRtcVoiceMediaChannel::SetMaxSendBitrate.";
deadbeef80346142016-04-27 14:17:10 -07002190 max_send_bitrate_bps_ = bps;
minyue7a973442016-10-20 03:27:12 -07002191 bool success = true;
skvlade0d46372016-04-07 22:59:22 -07002192 for (const auto& kv : send_streams_) {
minyue7a973442016-10-20 03:27:12 -07002193 if (!kv.second->SetMaxSendBitrate(max_send_bitrate_bps_)) {
2194 success = false;
skvlade0d46372016-04-07 22:59:22 -07002195 }
2196 }
minyue7a973442016-10-20 03:27:12 -07002197 return success;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002198}
2199
skvlad7a43d252016-03-22 15:32:27 -07002200void WebRtcVoiceMediaChannel::OnReadyToSend(bool ready) {
2201 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
Mirko Bonadei675513b2017-11-09 11:09:25 +01002202 RTC_LOG(LS_VERBOSE) << "OnReadyToSend: " << (ready ? "Ready." : "Not ready.");
skvlad7a43d252016-03-22 15:32:27 -07002203 call_->SignalChannelNetworkState(
2204 webrtc::MediaType::AUDIO,
2205 ready ? webrtc::kNetworkUp : webrtc::kNetworkDown);
2206}
2207
michaelt79e05882016-11-08 02:50:09 -08002208void WebRtcVoiceMediaChannel::OnTransportOverheadChanged(
2209 int transport_overhead_per_packet) {
2210 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
2211 call_->OnTransportOverheadChanged(webrtc::MediaType::AUDIO,
2212 transport_overhead_per_packet);
2213}
2214
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002215bool WebRtcVoiceMediaChannel::GetStats(VoiceMediaInfo* info) {
Peter Boströmca8b4042016-03-08 14:24:13 -08002216 TRACE_EVENT0("webrtc", "WebRtcVoiceMediaChannel::GetStats");
solenberg566ef242015-11-06 15:34:49 -08002217 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
solenberg85a04962015-10-27 03:35:21 -07002218 RTC_DCHECK(info);
solenbergd97ec302015-10-07 01:40:33 -07002219
solenberg85a04962015-10-27 03:35:21 -07002220 // Get SSRC and stats for each sender.
hbos1acfbd22016-11-17 23:43:29 -08002221 RTC_DCHECK_EQ(info->senders.size(), 0U);
solenberg85a04962015-10-27 03:35:21 -07002222 for (const auto& stream : send_streams_) {
2223 webrtc::AudioSendStream::Stats stats = stream.second->GetStats();
wu@webrtc.org9dba5252013-08-05 20:36:57 +00002224 VoiceSenderInfo sinfo;
solenberg85a04962015-10-27 03:35:21 -07002225 sinfo.add_ssrc(stats.local_ssrc);
2226 sinfo.bytes_sent = stats.bytes_sent;
2227 sinfo.packets_sent = stats.packets_sent;
2228 sinfo.packets_lost = stats.packets_lost;
2229 sinfo.fraction_lost = stats.fraction_lost;
2230 sinfo.codec_name = stats.codec_name;
hbos1acfbd22016-11-17 23:43:29 -08002231 sinfo.codec_payload_type = stats.codec_payload_type;
solenberg85a04962015-10-27 03:35:21 -07002232 sinfo.ext_seqnum = stats.ext_seqnum;
2233 sinfo.jitter_ms = stats.jitter_ms;
2234 sinfo.rtt_ms = stats.rtt_ms;
2235 sinfo.audio_level = stats.audio_level;
zsteine76bd3a2017-07-14 12:17:49 -07002236 sinfo.total_input_energy = stats.total_input_energy;
2237 sinfo.total_input_duration = stats.total_input_duration;
solenberg85a04962015-10-27 03:35:21 -07002238 sinfo.aec_quality_min = stats.aec_quality_min;
2239 sinfo.echo_delay_median_ms = stats.echo_delay_median_ms;
2240 sinfo.echo_delay_std_ms = stats.echo_delay_std_ms;
2241 sinfo.echo_return_loss = stats.echo_return_loss;
2242 sinfo.echo_return_loss_enhancement = stats.echo_return_loss_enhancement;
ivoc8c63a822016-10-21 04:10:03 -07002243 sinfo.residual_echo_likelihood = stats.residual_echo_likelihood;
ivoc4e477a12017-01-15 08:29:46 -08002244 sinfo.residual_echo_likelihood_recent_max =
2245 stats.residual_echo_likelihood_recent_max;
Taylor Brandstetter1a018dc2016-03-08 12:37:39 -08002246 sinfo.typing_noise_detected = (send_ ? stats.typing_noise_detected : false);
ivoce1198e02017-09-08 08:13:19 -07002247 sinfo.ana_statistics = stats.ana_statistics;
wu@webrtc.org9dba5252013-08-05 20:36:57 +00002248 info->senders.push_back(sinfo);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002249 }
2250
solenberg85a04962015-10-27 03:35:21 -07002251 // Get SSRC and stats for each receiver.
hbos1acfbd22016-11-17 23:43:29 -08002252 RTC_DCHECK_EQ(info->receivers.size(), 0U);
solenberg7add0582015-11-20 09:59:34 -08002253 for (const auto& stream : recv_streams_) {
deadbeef4e2deab2017-09-20 13:56:21 -07002254 uint32_t ssrc = stream.first;
2255 // When SSRCs are unsignaled, there's only one audio MediaStreamTrack, but
2256 // multiple RTP streams can be received over time (if the SSRC changes for
2257 // whatever reason). We only want the RTCMediaStreamTrackStats to represent
2258 // the stats for the most recent stream (the one whose audio is actually
2259 // routed to the MediaStreamTrack), so here we ignore any unsignaled SSRCs
2260 // except for the most recent one (last in the vector). This is somewhat of
2261 // a hack, and means you don't get *any* stats for these inactive streams,
2262 // but it's slightly better than the previous behavior, which was "highest
2263 // SSRC wins".
2264 // See: https://bugs.chromium.org/p/webrtc/issues/detail?id=8158
2265 if (!unsignaled_recv_ssrcs_.empty()) {
2266 auto end_it = --unsignaled_recv_ssrcs_.end();
2267 if (std::find(unsignaled_recv_ssrcs_.begin(), end_it, ssrc) != end_it) {
2268 continue;
2269 }
2270 }
Fredrik Solenberg4f4ec0a2015-10-22 10:49:27 +02002271 webrtc::AudioReceiveStream::Stats stats = stream.second->GetStats();
2272 VoiceReceiverInfo rinfo;
2273 rinfo.add_ssrc(stats.remote_ssrc);
2274 rinfo.bytes_rcvd = stats.bytes_rcvd;
2275 rinfo.packets_rcvd = stats.packets_rcvd;
2276 rinfo.packets_lost = stats.packets_lost;
2277 rinfo.fraction_lost = stats.fraction_lost;
2278 rinfo.codec_name = stats.codec_name;
hbos1acfbd22016-11-17 23:43:29 -08002279 rinfo.codec_payload_type = stats.codec_payload_type;
Fredrik Solenberg4f4ec0a2015-10-22 10:49:27 +02002280 rinfo.ext_seqnum = stats.ext_seqnum;
2281 rinfo.jitter_ms = stats.jitter_ms;
2282 rinfo.jitter_buffer_ms = stats.jitter_buffer_ms;
2283 rinfo.jitter_buffer_preferred_ms = stats.jitter_buffer_preferred_ms;
2284 rinfo.delay_estimate_ms = stats.delay_estimate_ms;
2285 rinfo.audio_level = stats.audio_level;
zsteine76bd3a2017-07-14 12:17:49 -07002286 rinfo.total_output_energy = stats.total_output_energy;
Steve Anton2dbc69f2017-08-24 17:15:13 -07002287 rinfo.total_samples_received = stats.total_samples_received;
zsteine76bd3a2017-07-14 12:17:49 -07002288 rinfo.total_output_duration = stats.total_output_duration;
Steve Anton2dbc69f2017-08-24 17:15:13 -07002289 rinfo.concealed_samples = stats.concealed_samples;
Gustaf Ullberg9a2e9062017-09-18 09:28:20 +02002290 rinfo.concealment_events = stats.concealment_events;
Gustaf Ullbergb0a02072017-10-02 12:00:34 +02002291 rinfo.jitter_buffer_delay_seconds = stats.jitter_buffer_delay_seconds;
Fredrik Solenberg4f4ec0a2015-10-22 10:49:27 +02002292 rinfo.expand_rate = stats.expand_rate;
2293 rinfo.speech_expand_rate = stats.speech_expand_rate;
2294 rinfo.secondary_decoded_rate = stats.secondary_decoded_rate;
minyue-webrtc0e320ec2017-08-28 13:51:27 +02002295 rinfo.secondary_discarded_rate = stats.secondary_discarded_rate;
Fredrik Solenberg4f4ec0a2015-10-22 10:49:27 +02002296 rinfo.accelerate_rate = stats.accelerate_rate;
2297 rinfo.preemptive_expand_rate = stats.preemptive_expand_rate;
2298 rinfo.decoding_calls_to_silence_generator =
2299 stats.decoding_calls_to_silence_generator;
2300 rinfo.decoding_calls_to_neteq = stats.decoding_calls_to_neteq;
2301 rinfo.decoding_normal = stats.decoding_normal;
2302 rinfo.decoding_plc = stats.decoding_plc;
2303 rinfo.decoding_cng = stats.decoding_cng;
2304 rinfo.decoding_plc_cng = stats.decoding_plc_cng;
henrik.lundin63489782016-09-20 01:47:12 -07002305 rinfo.decoding_muted_output = stats.decoding_muted_output;
Fredrik Solenberg4f4ec0a2015-10-22 10:49:27 +02002306 rinfo.capture_start_ntp_time_ms = stats.capture_start_ntp_time_ms;
2307 info->receivers.push_back(rinfo);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002308 }
2309
hbos1acfbd22016-11-17 23:43:29 -08002310 // Get codec info
2311 for (const AudioCodec& codec : send_codecs_) {
2312 webrtc::RtpCodecParameters codec_params = codec.ToCodecParameters();
2313 info->send_codecs.insert(
2314 std::make_pair(codec_params.payload_type, std::move(codec_params)));
2315 }
2316 for (const AudioCodec& codec : recv_codecs_) {
2317 webrtc::RtpCodecParameters codec_params = codec.ToCodecParameters();
2318 info->receive_codecs.insert(
2319 std::make_pair(codec_params.payload_type, std::move(codec_params)));
2320 }
2321
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002322 return true;
2323}
2324
Tommif888bb52015-12-12 01:37:01 +01002325void WebRtcVoiceMediaChannel::SetRawAudioSink(
2326 uint32_t ssrc,
kwiberg686a8ef2016-02-26 03:00:35 -08002327 std::unique_ptr<webrtc::AudioSinkInterface> sink) {
Tommif888bb52015-12-12 01:37:01 +01002328 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
Mirko Bonadei675513b2017-11-09 11:09:25 +01002329 RTC_LOG(LS_VERBOSE) << "WebRtcVoiceMediaChannel::SetRawAudioSink: ssrc:"
2330 << ssrc << " " << (sink ? "(ptr)" : "NULL");
deadbeef884f5852016-01-15 09:20:04 -08002331 if (ssrc == 0) {
solenberg2100c0b2017-03-01 11:29:29 -08002332 if (!unsignaled_recv_ssrcs_.empty()) {
kwiberg686a8ef2016-02-26 03:00:35 -08002333 std::unique_ptr<webrtc::AudioSinkInterface> proxy_sink(
deadbeef884f5852016-01-15 09:20:04 -08002334 sink ? new ProxySink(sink.get()) : nullptr);
solenberg2100c0b2017-03-01 11:29:29 -08002335 SetRawAudioSink(unsignaled_recv_ssrcs_.back(), std::move(proxy_sink));
deadbeef884f5852016-01-15 09:20:04 -08002336 }
2337 default_sink_ = std::move(sink);
2338 return;
2339 }
Tommif888bb52015-12-12 01:37:01 +01002340 const auto it = recv_streams_.find(ssrc);
2341 if (it == recv_streams_.end()) {
Mirko Bonadei675513b2017-11-09 11:09:25 +01002342 RTC_LOG(LS_WARNING) << "SetRawAudioSink: no recv stream " << ssrc;
Tommif888bb52015-12-12 01:37:01 +01002343 return;
2344 }
deadbeef2d110be2016-01-13 12:00:26 -08002345 it->second->SetRawAudioSink(std::move(sink));
Tommif888bb52015-12-12 01:37:01 +01002346}
2347
hbos8d609f62017-04-10 07:39:05 -07002348std::vector<webrtc::RtpSource> WebRtcVoiceMediaChannel::GetSources(
2349 uint32_t ssrc) const {
2350 auto it = recv_streams_.find(ssrc);
2351 RTC_DCHECK(it != recv_streams_.end())
2352 << "Attempting to get contributing sources for SSRC:" << ssrc
2353 << " which doesn't exist.";
2354 return it->second->GetSources();
2355}
2356
Peter Boström0c4e06b2015-10-07 12:23:21 +02002357int WebRtcVoiceMediaChannel::GetReceiveChannelId(uint32_t ssrc) const {
solenberg566ef242015-11-06 15:34:49 -08002358 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
solenberg7add0582015-11-20 09:59:34 -08002359 const auto it = recv_streams_.find(ssrc);
2360 if (it != recv_streams_.end()) {
mallinath@webrtc.org67ee6b92014-02-03 16:57:16 +00002361 return it->second->channel();
solenberg8fb30c32015-10-13 03:06:58 -07002362 }
solenberg1ac56142015-10-13 03:58:19 -07002363 return -1;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002364}
2365
Peter Boström0c4e06b2015-10-07 12:23:21 +02002366int WebRtcVoiceMediaChannel::GetSendChannelId(uint32_t ssrc) const {
solenberg566ef242015-11-06 15:34:49 -08002367 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
solenbergc96df772015-10-21 13:01:53 -07002368 const auto it = send_streams_.find(ssrc);
2369 if (it != send_streams_.end()) {
mallinath@webrtc.org67ee6b92014-02-03 16:57:16 +00002370 return it->second->channel();
solenberg8fb30c32015-10-13 03:06:58 -07002371 }
wu@webrtc.org9dba5252013-08-05 20:36:57 +00002372 return -1;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002373}
solenberg2100c0b2017-03-01 11:29:29 -08002374
2375bool WebRtcVoiceMediaChannel::
2376 MaybeDeregisterUnsignaledRecvStream(uint32_t ssrc) {
2377 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
2378 auto it = std::find(unsignaled_recv_ssrcs_.begin(),
2379 unsignaled_recv_ssrcs_.end(),
2380 ssrc);
2381 if (it != unsignaled_recv_ssrcs_.end()) {
2382 unsignaled_recv_ssrcs_.erase(it);
2383 return true;
2384 }
2385 return false;
2386}
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002387} // namespace cricket
2388
2389#endif // HAVE_WEBRTC_VOICE