blob: 19619a3a0f93ff10bc08bba57279a009025c13bf [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 }
Oskar Sundbom78807582017-11-16 11:09:55 +0100158 return rtc::nullopt;
minyue6b825df2016-10-31 04:08:32 -0700159}
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) {
Oskar Sundbom78807582017-11-16 11:09:55 +0100188 return 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.";
Oskar Sundbom78807582017-11-16 11:09:55 +0100199 return rtc::nullopt;
solenberg971cab02016-06-14 10:02:41 -0700200 }
ossu20a4b3f2017-04-27 02:08:52 -0700201
202 if (spec.info.HasFixedBitrate()) {
Oskar Sundbom78807582017-11-16 11:09:55 +0100203 return spec.info.default_bitrate_bps;
ossu20a4b3f2017-04-27 02:08:52 -0700204 } else {
205 // If codec is multi-rate then just set the bitrate.
Oskar Sundbom78807582017-11-16 11:09:55 +0100206 return std::min(bps, spec.info.max_bitrate_bps);
ossu20a4b3f2017-04-27 02:08:52 -0700207 }
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;
Oskar Sundbom78807582017-11-16 11:09:55 +0100305 options.echo_cancellation = true;
306 options.auto_gain_control = true;
307 options.noise_suppression = true;
308 options.highpass_filter = true;
309 options.stereo_swapping = false;
310 options.audio_jitter_buffer_max_packets = 50;
311 options.audio_jitter_buffer_fast_accelerate = false;
312 options.typing_detection = true;
313 options.adjust_agc_delta = 0;
314 options.experimental_agc = false;
315 options.extended_filter_aec = false;
316 options.delay_agnostic_aec = false;
317 options.experimental_ns = false;
318 options.intelligibility_enhancer = false;
319 options.level_control = false;
320 options.residual_echo_detector = 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.
Oskar Sundbom78807582017-11-16 11:09:55 +0100372 options.echo_cancellation = false;
373 options.extended_filter_aec = 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;
Oskar Sundbom78807582017-11-16 11:09:55 +0100377 options.extended_filter_aec = 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) {
Oskar Sundbom78807582017-11-16 11:09:55 +0100387 options.echo_cancellation = true;
388 options.extended_filter_aec = 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.
Oskar Sundbom78807582017-11-16 11:09:55 +0100397 options.noise_suppression = false;
398 options.typing_detection = false;
399 options.experimental_ns = 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)
Oskar Sundbom78807582017-11-16 11:09:55 +0100402 options.typing_detection = false;
403 options.experimental_ns = false;
peah8a8ebd92017-05-22 15:48:47 -0700404#endif
405
406// Set and adjust gain control options.
407#if defined(WEBRTC_IOS)
408 // On iOS, VPIO provides built-in AGC.
Oskar Sundbom78807582017-11-16 11:09:55 +0100409 options.auto_gain_control = false;
410 options.experimental_agc = 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)
Oskar Sundbom78807582017-11-16 11:09:55 +0100413 options.experimental_agc = false;
peah8a8ebd92017-05-22 15:48:47 -0700414#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")) {
Oskar Sundbom78807582017-11-16 11:09:55 +0100424 options.auto_gain_control = 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.";
Oskar Sundbom78807582017-11-16 11:09:55 +0100431 options.highpass_filter = false;
peah8a8ebd92017-05-22 15:48:47 -0700432 }
433 }
434#endif
435
peah1bcfce52016-08-26 07:16:04 -0700436#if (WEBRTC_INTELLIGIBILITY_ENHANCER == 0)
437 // Hardcode the intelligibility enhancer to be off.
Oskar Sundbom78807582017-11-16 11:09:55 +0100438 options.intelligibility_enhancer = false;
peah1bcfce52016-08-26 07:16:04 -0700439#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.
Oskar Sundbom78807582017-11-16 11:09:55 +0100457 options.echo_cancellation = 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.
Oskar Sundbom78807582017-11-16 11:09:55 +0100480 options.auto_gain_control = 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.
Oskar Sundbom78807582017-11-16 11:09:55 +0100532 options.noise_suppression = false;
Mirko Bonadei675513b2017-11-09 11:09:25 +0100533 RTC_LOG(LS_INFO)
534 << "Disabling NS since built-in NS will be used instead";
henrikac14f5ff2015-09-23 14:08:33 +0200535 }
536 }
solenberg76377c52017-02-21 00:54:31 -0800537 webrtc::apm_helpers::SetNsStatus(apm(), *options.noise_suppression);
buildbot@webrtc.org13d67762014-05-02 17:33:29 +0000538 }
539
kwiberg102c6a62015-10-30 02:47:38 -0700540 if (options.stereo_swapping) {
Mirko Bonadei675513b2017-11-09 11:09:25 +0100541 RTC_LOG(LS_INFO) << "Stereo swapping enabled? " << *options.stereo_swapping;
solenberg76377c52017-02-21 00:54:31 -0800542 transmit_mixer()->EnableStereoChannelSwapping(*options.stereo_swapping);
buildbot@webrtc.org13d67762014-05-02 17:33:29 +0000543 }
544
kwiberg102c6a62015-10-30 02:47:38 -0700545 if (options.audio_jitter_buffer_max_packets) {
Mirko Bonadei675513b2017-11-09 11:09:25 +0100546 RTC_LOG(LS_INFO) << "NetEq capacity is "
547 << *options.audio_jitter_buffer_max_packets;
solenberg88499ec2016-09-07 07:34:41 -0700548 channel_config_.acm_config.neteq_config.max_packets_in_buffer =
549 std::max(20, *options.audio_jitter_buffer_max_packets);
Henrik Lundin64dad832015-05-11 12:44:23 +0200550 }
kwiberg102c6a62015-10-30 02:47:38 -0700551 if (options.audio_jitter_buffer_fast_accelerate) {
Mirko Bonadei675513b2017-11-09 11:09:25 +0100552 RTC_LOG(LS_INFO) << "NetEq fast mode? "
553 << *options.audio_jitter_buffer_fast_accelerate;
solenberg88499ec2016-09-07 07:34:41 -0700554 channel_config_.acm_config.neteq_config.enable_fast_accelerate =
555 *options.audio_jitter_buffer_fast_accelerate;
Henrik Lundin5263b3c2015-06-01 10:29:41 +0200556 }
557
kwiberg102c6a62015-10-30 02:47:38 -0700558 if (options.typing_detection) {
Mirko Bonadei675513b2017-11-09 11:09:25 +0100559 RTC_LOG(LS_INFO) << "Typing detection is enabled? "
560 << *options.typing_detection;
solenberg76377c52017-02-21 00:54:31 -0800561 webrtc::apm_helpers::SetTypingDetectionStatus(
562 apm(), *options.typing_detection);
buildbot@webrtc.org13d67762014-05-02 17:33:29 +0000563 }
564
buildbot@webrtc.org1f8a2372014-08-28 10:52:44 +0000565 webrtc::Config config;
566
kwiberg102c6a62015-10-30 02:47:38 -0700567 if (options.delay_agnostic_aec)
568 delay_agnostic_aec_ = options.delay_agnostic_aec;
569 if (delay_agnostic_aec_) {
Mirko Bonadei675513b2017-11-09 11:09:25 +0100570 RTC_LOG(LS_INFO) << "Delay agnostic aec is enabled? "
571 << *delay_agnostic_aec_;
henrik.lundin0f133b92015-07-02 00:17:55 -0700572 config.Set<webrtc::DelayAgnostic>(
kwiberg102c6a62015-10-30 02:47:38 -0700573 new webrtc::DelayAgnostic(*delay_agnostic_aec_));
Bjorn Volckerbf395c12015-03-25 22:45:56 +0100574 }
575
kwiberg102c6a62015-10-30 02:47:38 -0700576 if (options.extended_filter_aec) {
577 extended_filter_aec_ = options.extended_filter_aec;
578 }
579 if (extended_filter_aec_) {
Mirko Bonadei675513b2017-11-09 11:09:25 +0100580 RTC_LOG(LS_INFO) << "Extended filter aec is enabled? "
581 << *extended_filter_aec_;
Henrik Lundin441f6342015-06-09 16:03:13 +0200582 config.Set<webrtc::ExtendedFilter>(
kwiberg102c6a62015-10-30 02:47:38 -0700583 new webrtc::ExtendedFilter(*extended_filter_aec_));
buildbot@webrtc.org1f8a2372014-08-28 10:52:44 +0000584 }
585
kwiberg102c6a62015-10-30 02:47:38 -0700586 if (options.experimental_ns) {
587 experimental_ns_ = options.experimental_ns;
588 }
589 if (experimental_ns_) {
Mirko Bonadei675513b2017-11-09 11:09:25 +0100590 RTC_LOG(LS_INFO) << "Experimental ns is enabled? " << *experimental_ns_;
buildbot@webrtc.org1f8a2372014-08-28 10:52:44 +0000591 config.Set<webrtc::ExperimentalNs>(
kwiberg102c6a62015-10-30 02:47:38 -0700592 new webrtc::ExperimentalNs(*experimental_ns_));
buildbot@webrtc.org1f8a2372014-08-28 10:52:44 +0000593 }
buildbot@webrtc.org1f8a2372014-08-28 10:52:44 +0000594
Alejandro Luebsc9b0c262016-05-16 15:32:38 -0700595 if (intelligibility_enhancer_) {
Mirko Bonadei675513b2017-11-09 11:09:25 +0100596 RTC_LOG(LS_INFO) << "Intelligibility Enhancer is enabled? "
597 << *intelligibility_enhancer_;
Alejandro Luebsc9b0c262016-05-16 15:32:38 -0700598 config.Set<webrtc::Intelligibility>(
599 new webrtc::Intelligibility(*intelligibility_enhancer_));
600 }
601
peaha3333bf2016-06-30 00:02:34 -0700602 if (options.level_control) {
603 level_control_ = options.level_control;
604 }
605
peahb1c9d1d2017-07-25 15:45:24 -0700606 webrtc::AudioProcessing::Config apm_config = apm()->GetConfig();
607
Mirko Bonadei675513b2017-11-09 11:09:25 +0100608 RTC_LOG(LS_INFO) << "Level control: "
609 << (!!level_control_ ? *level_control_ : -1);
peaha3333bf2016-06-30 00:02:34 -0700610 if (level_control_) {
peahb1c9d1d2017-07-25 15:45:24 -0700611 apm_config.level_controller.enabled = *level_control_;
aleloie33c5d92016-10-20 01:53:27 -0700612 if (options.level_control_initial_peak_level_dbfs) {
peahb1c9d1d2017-07-25 15:45:24 -0700613 apm_config.level_controller.initial_peak_level_dbfs =
aleloie33c5d92016-10-20 01:53:27 -0700614 *options.level_control_initial_peak_level_dbfs;
615 }
peaha3333bf2016-06-30 00:02:34 -0700616 }
617
peah8271d042016-11-22 07:24:52 -0800618 if (options.highpass_filter) {
peahb1c9d1d2017-07-25 15:45:24 -0700619 apm_config.high_pass_filter.enabled = *options.highpass_filter;
peah8271d042016-11-22 07:24:52 -0800620 }
621
ivoc4ca18692017-02-10 05:11:09 -0800622 if (options.residual_echo_detector) {
peahb1c9d1d2017-07-25 15:45:24 -0700623 apm_config.residual_echo_detector.enabled = *options.residual_echo_detector;
ivoc4ca18692017-02-10 05:11:09 -0800624 }
625
solenberg059fb442016-10-26 05:12:24 -0700626 apm()->SetExtraOptions(config);
peahb1c9d1d2017-07-25 15:45:24 -0700627 apm()->ApplyConfig(apm_config);
buildbot@webrtc.org13d67762014-05-02 17:33:29 +0000628 return true;
629}
630
solenberg796b8f92017-03-01 17:02:23 -0800631// TODO(solenberg): Remove, once AudioMonitor is gone.
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000632int WebRtcVoiceEngine::GetInputLevel() {
solenberg566ef242015-11-06 15:34:49 -0800633 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
solenberg796b8f92017-03-01 17:02:23 -0800634 int8_t level = transmit_mixer()->AudioLevel();
635 RTC_DCHECK_LE(0, level);
636 return level;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000637}
638
ossudedfd282016-06-14 07:12:39 -0700639const std::vector<AudioCodec>& WebRtcVoiceEngine::send_codecs() const {
640 RTC_DCHECK(signal_thread_checker_.CalledOnValidThread());
ossuc54071d2016-08-17 02:45:41 -0700641 return send_codecs_;
ossudedfd282016-06-14 07:12:39 -0700642}
643
644const std::vector<AudioCodec>& WebRtcVoiceEngine::recv_codecs() const {
solenberg566ef242015-11-06 15:34:49 -0800645 RTC_DCHECK(signal_thread_checker_.CalledOnValidThread());
ossuc54071d2016-08-17 02:45:41 -0700646 return recv_codecs_;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000647}
648
Stefan Holmer9d69c3f2015-12-07 10:45:43 +0100649RtpCapabilities WebRtcVoiceEngine::GetCapabilities() const {
solenberg566ef242015-11-06 15:34:49 -0800650 RTC_DCHECK(signal_thread_checker_.CalledOnValidThread());
Stefan Holmer9d69c3f2015-12-07 10:45:43 +0100651 RtpCapabilities capabilities;
Stefan Holmer9d69c3f2015-12-07 10:45:43 +0100652 capabilities.header_extensions.push_back(
isheriff6f8d6862016-05-26 11:24:55 -0700653 webrtc::RtpExtension(webrtc::RtpExtension::kAudioLevelUri,
654 webrtc::RtpExtension::kAudioLevelDefaultId));
sprangc1b57a12017-02-28 08:50:47 -0800655 if (webrtc::field_trial::IsEnabled("WebRTC-Audio-SendSideBwe")) {
isheriff6f8d6862016-05-26 11:24:55 -0700656 capabilities.header_extensions.push_back(webrtc::RtpExtension(
657 webrtc::RtpExtension::kTransportSequenceNumberUri,
658 webrtc::RtpExtension::kTransportSequenceNumberDefaultId));
stefanba4c0e42016-02-04 04:12:24 -0800659 }
Stefan Holmer9d69c3f2015-12-07 10:45:43 +0100660 return capabilities;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000661}
662
solenberg63b34542015-09-29 06:06:31 -0700663void WebRtcVoiceEngine::RegisterChannel(WebRtcVoiceMediaChannel* channel) {
solenberg566ef242015-11-06 15:34:49 -0800664 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
665 RTC_DCHECK(channel);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000666 channels_.push_back(channel);
667}
668
solenberg63b34542015-09-29 06:06:31 -0700669void WebRtcVoiceEngine::UnregisterChannel(WebRtcVoiceMediaChannel* channel) {
solenberg566ef242015-11-06 15:34:49 -0800670 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
solenberg63b34542015-09-29 06:06:31 -0700671 auto it = std::find(channels_.begin(), channels_.end(), channel);
solenberg566ef242015-11-06 15:34:49 -0800672 RTC_DCHECK(it != channels_.end());
673 channels_.erase(it);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000674}
675
ivocd66b44d2016-01-15 03:06:36 -0800676bool WebRtcVoiceEngine::StartAecDump(rtc::PlatformFile file,
677 int64_t max_size_bytes) {
solenberg566ef242015-11-06 15:34:49 -0800678 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
deadbeefeb02c032017-06-15 08:29:25 -0700679 auto aec_dump = webrtc::AecDumpFactory::Create(
680 file, max_size_bytes, low_priority_worker_queue_.get());
aleloi048cbdd2017-05-29 02:56:27 -0700681 if (!aec_dump) {
wu@webrtc.orga8910d22014-01-23 22:12:45 +0000682 return false;
683 }
aleloi048cbdd2017-05-29 02:56:27 -0700684 apm()->AttachAecDump(std::move(aec_dump));
wu@webrtc.orga9890802013-12-13 00:21:03 +0000685 return true;
wu@webrtc.orga9890802013-12-13 00:21:03 +0000686}
687
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000688void WebRtcVoiceEngine::StartAecDump(const std::string& filename) {
solenberg566ef242015-11-06 15:34:49 -0800689 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
aleloi048cbdd2017-05-29 02:56:27 -0700690
deadbeefeb02c032017-06-15 08:29:25 -0700691 auto aec_dump = webrtc::AecDumpFactory::Create(
692 filename, -1, low_priority_worker_queue_.get());
aleloi048cbdd2017-05-29 02:56:27 -0700693 if (aec_dump) {
694 apm()->AttachAecDump(std::move(aec_dump));
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000695 }
696}
697
698void WebRtcVoiceEngine::StopAecDump() {
solenberg566ef242015-11-06 15:34:49 -0800699 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
aleloi048cbdd2017-05-29 02:56:27 -0700700 apm()->DetachAecDump();
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000701}
702
solenberg0a617e22015-10-20 15:49:38 -0700703int WebRtcVoiceEngine::CreateVoEChannel() {
solenberg566ef242015-11-06 15:34:49 -0800704 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
solenberg88499ec2016-09-07 07:34:41 -0700705 return voe_wrapper_->base()->CreateChannel(channel_config_);
sergeyu@chromium.org5bc25c42013-12-05 00:24:06 +0000706}
707
solenberg5b5129a2016-04-08 05:35:48 -0700708webrtc::AudioDeviceModule* WebRtcVoiceEngine::adm() {
709 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
710 RTC_DCHECK(adm_);
711 return adm_;
712}
713
peahb1c9d1d2017-07-25 15:45:24 -0700714webrtc::AudioProcessing* WebRtcVoiceEngine::apm() const {
solenberg059fb442016-10-26 05:12:24 -0700715 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
peaha9cc40b2017-06-29 08:32:09 -0700716 return apm_.get();
solenberg059fb442016-10-26 05:12:24 -0700717}
718
solenberg76377c52017-02-21 00:54:31 -0800719webrtc::voe::TransmitMixer* WebRtcVoiceEngine::transmit_mixer() {
720 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
721 RTC_DCHECK(transmit_mixer_);
722 return transmit_mixer_;
723}
724
ossu20a4b3f2017-04-27 02:08:52 -0700725AudioCodecs WebRtcVoiceEngine::CollectCodecs(
726 const std::vector<webrtc::AudioCodecSpec>& specs) const {
ossuc54071d2016-08-17 02:45:41 -0700727 PayloadTypeMapper mapper;
728 AudioCodecs out;
ossuc54071d2016-08-17 02:45:41 -0700729
solenberg2779bab2016-11-17 04:45:19 -0800730 // Only generate CN payload types for these clockrates:
ossuc54071d2016-08-17 02:45:41 -0700731 std::map<int, bool, std::greater<int>> generate_cn = {{ 8000, false },
732 { 16000, false },
733 { 32000, false }};
solenberg2779bab2016-11-17 04:45:19 -0800734 // Only generate telephone-event payload types for these clockrates:
735 std::map<int, bool, std::greater<int>> generate_dtmf = {{ 8000, false },
736 { 16000, false },
737 { 32000, false },
738 { 48000, false }};
ossuc54071d2016-08-17 02:45:41 -0700739
ossu9def8002017-02-09 05:14:32 -0800740 auto map_format = [&mapper](const webrtc::SdpAudioFormat& format,
741 AudioCodecs* out) {
ossuc54071d2016-08-17 02:45:41 -0700742 rtc::Optional<AudioCodec> opt_codec = mapper.ToAudioCodec(format);
ossu9def8002017-02-09 05:14:32 -0800743 if (opt_codec) {
744 if (out) {
745 out->push_back(*opt_codec);
746 }
747 } else {
Mirko Bonadei675513b2017-11-09 11:09:25 +0100748 RTC_LOG(LS_ERROR) << "Unable to assign payload type to format: "
749 << format;
ossuc54071d2016-08-17 02:45:41 -0700750 }
751
ossu9def8002017-02-09 05:14:32 -0800752 return opt_codec;
ossuc54071d2016-08-17 02:45:41 -0700753 };
754
ossud4e9f622016-08-18 02:01:17 -0700755 for (const auto& spec : specs) {
ossu9def8002017-02-09 05:14:32 -0800756 // We need to do some extra stuff before adding the main codecs to out.
757 rtc::Optional<AudioCodec> opt_codec = map_format(spec.format, nullptr);
758 if (opt_codec) {
759 AudioCodec& codec = *opt_codec;
ossua1a040a2017-04-06 10:03:21 -0700760 if (spec.info.supports_network_adaption) {
ossu9def8002017-02-09 05:14:32 -0800761 codec.AddFeedbackParam(
762 FeedbackParam(kRtcpFbParamTransportCc, kParamValueEmpty));
763 }
764
ossua1a040a2017-04-06 10:03:21 -0700765 if (spec.info.allow_comfort_noise) {
solenberg2779bab2016-11-17 04:45:19 -0800766 // Generate a CN entry if the decoder allows it and we support the
767 // clockrate.
768 auto cn = generate_cn.find(spec.format.clockrate_hz);
769 if (cn != generate_cn.end()) {
770 cn->second = true;
771 }
772 }
773
774 // Generate a telephone-event entry if we support the clockrate.
775 auto dtmf = generate_dtmf.find(spec.format.clockrate_hz);
776 if (dtmf != generate_dtmf.end()) {
777 dtmf->second = true;
ossuc54071d2016-08-17 02:45:41 -0700778 }
ossu9def8002017-02-09 05:14:32 -0800779
780 out.push_back(codec);
ossuc54071d2016-08-17 02:45:41 -0700781 }
782 }
783
solenberg2779bab2016-11-17 04:45:19 -0800784 // Add CN codecs after "proper" audio codecs.
ossuc54071d2016-08-17 02:45:41 -0700785 for (const auto& cn : generate_cn) {
786 if (cn.second) {
ossu9def8002017-02-09 05:14:32 -0800787 map_format({kCnCodecName, cn.first, 1}, &out);
ossuc54071d2016-08-17 02:45:41 -0700788 }
789 }
790
solenberg2779bab2016-11-17 04:45:19 -0800791 // Add telephone-event codecs last.
792 for (const auto& dtmf : generate_dtmf) {
793 if (dtmf.second) {
ossu9def8002017-02-09 05:14:32 -0800794 map_format({kDtmfCodecName, dtmf.first, 1}, &out);
solenberg2779bab2016-11-17 04:45:19 -0800795 }
796 }
ossuc54071d2016-08-17 02:45:41 -0700797
798 return out;
799}
800
solenbergc96df772015-10-21 13:01:53 -0700801class WebRtcVoiceMediaChannel::WebRtcAudioSendStream
Taylor Brandstetter1a018dc2016-03-08 12:37:39 -0800802 : public AudioSource::Sink {
mallinath@webrtc.org67ee6b92014-02-03 16:57:16 +0000803 public:
minyue7a973442016-10-20 03:27:12 -0700804 WebRtcAudioSendStream(
805 int ch,
806 webrtc::AudioTransport* voe_audio_transport,
807 uint32_t ssrc,
808 const std::string& c_name,
Alex Narestb3944f02017-10-13 14:56:18 +0200809 const std::string track_id,
ossu20a4b3f2017-04-27 02:08:52 -0700810 const rtc::Optional<webrtc::AudioSendStream::Config::SendCodecSpec>&
811 send_codec_spec,
minyue7a973442016-10-20 03:27:12 -0700812 const std::vector<webrtc::RtpExtension>& extensions,
813 int max_send_bitrate_bps,
minyue6b825df2016-10-31 04:08:32 -0700814 const rtc::Optional<std::string>& audio_network_adaptor_config,
minyue7a973442016-10-20 03:27:12 -0700815 webrtc::Call* call,
ossu20a4b3f2017-04-27 02:08:52 -0700816 webrtc::Transport* send_transport,
817 const rtc::scoped_refptr<webrtc::AudioEncoderFactory>& encoder_factory)
solenberg7add0582015-11-20 09:59:34 -0800818 : voe_audio_transport_(voe_audio_transport),
solenberg3a941542015-11-16 07:34:50 -0800819 call_(call),
mflodman3d7db262016-04-29 00:57:13 -0700820 config_(send_transport),
sprangc1b57a12017-02-28 08:50:47 -0800821 send_side_bwe_with_overhead_(
822 webrtc::field_trial::IsEnabled("WebRTC-SendSideBwe-WithOverhead")),
minyue7a973442016-10-20 03:27:12 -0700823 max_send_bitrate_bps_(max_send_bitrate_bps),
skvlade0d46372016-04-07 22:59:22 -0700824 rtp_parameters_(CreateRtpParametersWithOneEncoding()) {
solenberg85a04962015-10-27 03:35:21 -0700825 RTC_DCHECK_GE(ch, 0);
826 // TODO(solenberg): Once we're not using FakeWebRtcVoiceEngine anymore:
827 // RTC_DCHECK(voe_audio_transport);
solenbergc96df772015-10-21 13:01:53 -0700828 RTC_DCHECK(call);
ossu20a4b3f2017-04-27 02:08:52 -0700829 RTC_DCHECK(encoder_factory);
solenberg3a941542015-11-16 07:34:50 -0800830 config_.rtp.ssrc = ssrc;
831 config_.rtp.c_name = c_name;
832 config_.voe_channel_id = ch;
solenberg971cab02016-06-14 10:02:41 -0700833 config_.rtp.extensions = extensions;
minyue6b825df2016-10-31 04:08:32 -0700834 config_.audio_network_adaptor_config = audio_network_adaptor_config;
ossu20a4b3f2017-04-27 02:08:52 -0700835 config_.encoder_factory = encoder_factory;
Alex Narestb3944f02017-10-13 14:56:18 +0200836 config_.track_id = track_id;
Oskar Sundbom78807582017-11-16 11:09:55 +0100837 rtp_parameters_.encodings[0].ssrc = ssrc;
ossu20a4b3f2017-04-27 02:08:52 -0700838
839 if (send_codec_spec) {
840 UpdateSendCodecSpec(*send_codec_spec);
841 }
842
843 stream_ = call_->CreateAudioSendStream(config_);
solenbergc96df772015-10-21 13:01:53 -0700844 }
solenberg3a941542015-11-16 07:34:50 -0800845
solenbergc96df772015-10-21 13:01:53 -0700846 ~WebRtcAudioSendStream() override {
solenberg566ef242015-11-06 15:34:49 -0800847 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
Taylor Brandstetter1a018dc2016-03-08 12:37:39 -0800848 ClearSource();
solenbergc96df772015-10-21 13:01:53 -0700849 call_->DestroyAudioSendStream(stream_);
850 }
henrike@webrtc.org1e09a712013-07-26 19:17:59 +0000851
ossu20a4b3f2017-04-27 02:08:52 -0700852 void SetSendCodecSpec(
minyue7a973442016-10-20 03:27:12 -0700853 const webrtc::AudioSendStream::Config::SendCodecSpec& send_codec_spec) {
ossu20a4b3f2017-04-27 02:08:52 -0700854 UpdateSendCodecSpec(send_codec_spec);
855 ReconfigureAudioSendStream();
solenberg971cab02016-06-14 10:02:41 -0700856 }
857
ossu20a4b3f2017-04-27 02:08:52 -0700858 void SetRtpExtensions(const std::vector<webrtc::RtpExtension>& extensions) {
solenberg3a941542015-11-16 07:34:50 -0800859 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
solenberg3a941542015-11-16 07:34:50 -0800860 config_.rtp.extensions = extensions;
ossu20a4b3f2017-04-27 02:08:52 -0700861 ReconfigureAudioSendStream();
solenberg3a941542015-11-16 07:34:50 -0800862 }
863
ossu20a4b3f2017-04-27 02:08:52 -0700864 void SetAudioNetworkAdaptorConfig(
minyue6b825df2016-10-31 04:08:32 -0700865 const rtc::Optional<std::string>& audio_network_adaptor_config) {
866 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
867 if (config_.audio_network_adaptor_config == audio_network_adaptor_config) {
868 return;
869 }
870 config_.audio_network_adaptor_config = audio_network_adaptor_config;
ossu20a4b3f2017-04-27 02:08:52 -0700871 UpdateAllowedBitrateRange();
872 ReconfigureAudioSendStream();
minyue6b825df2016-10-31 04:08:32 -0700873 }
874
minyue7a973442016-10-20 03:27:12 -0700875 bool SetMaxSendBitrate(int bps) {
876 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
ossu20a4b3f2017-04-27 02:08:52 -0700877 RTC_DCHECK(config_.send_codec_spec);
878 RTC_DCHECK(audio_codec_spec_);
879 auto send_rate = ComputeSendBitrate(
880 bps, rtp_parameters_.encodings[0].max_bitrate_bps, *audio_codec_spec_);
881
minyue7a973442016-10-20 03:27:12 -0700882 if (!send_rate) {
883 return false;
884 }
885
886 max_send_bitrate_bps_ = bps;
887
ossu20a4b3f2017-04-27 02:08:52 -0700888 if (send_rate != config_.send_codec_spec->target_bitrate_bps) {
889 config_.send_codec_spec->target_bitrate_bps = send_rate;
890 ReconfigureAudioSendStream();
minyue7a973442016-10-20 03:27:12 -0700891 }
892 return true;
893 }
894
solenbergffbbcac2016-11-17 05:25:37 -0800895 bool SendTelephoneEvent(int payload_type, int payload_freq, int event,
896 int duration_ms) {
Fredrik Solenbergb5727682015-12-04 15:22:19 +0100897 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
898 RTC_DCHECK(stream_);
solenbergffbbcac2016-11-17 05:25:37 -0800899 return stream_->SendTelephoneEvent(payload_type, payload_freq, event,
900 duration_ms);
Fredrik Solenbergb5727682015-12-04 15:22:19 +0100901 }
902
Taylor Brandstetter1a018dc2016-03-08 12:37:39 -0800903 void SetSend(bool send) {
904 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
905 send_ = send;
906 UpdateSendState();
907 }
908
solenberg94218532016-06-16 10:53:22 -0700909 void SetMuted(bool muted) {
910 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
911 RTC_DCHECK(stream_);
912 stream_->SetMuted(muted);
913 muted_ = muted;
914 }
915
916 bool muted() const {
917 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
918 return muted_;
919 }
920
solenberg3a941542015-11-16 07:34:50 -0800921 webrtc::AudioSendStream::Stats GetStats() const {
922 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
923 RTC_DCHECK(stream_);
924 return stream_->GetStats();
925 }
926
Taylor Brandstetter1a018dc2016-03-08 12:37:39 -0800927 // Starts the sending by setting ourselves as a sink to the AudioSource to
928 // get data callbacks.
henrike@webrtc.orga7b98182014-02-21 15:51:43 +0000929 // This method is called on the libjingle worker thread.
mallinath@webrtc.org67ee6b92014-02-03 16:57:16 +0000930 // TODO(xians): Make sure Start() is called only once.
Taylor Brandstetter1a018dc2016-03-08 12:37:39 -0800931 void SetSource(AudioSource* source) {
solenberg566ef242015-11-06 15:34:49 -0800932 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
Taylor Brandstetter1a018dc2016-03-08 12:37:39 -0800933 RTC_DCHECK(source);
934 if (source_) {
935 RTC_DCHECK(source_ == source);
mallinath@webrtc.org67ee6b92014-02-03 16:57:16 +0000936 return;
937 }
Taylor Brandstetter1a018dc2016-03-08 12:37:39 -0800938 source->SetSink(this);
939 source_ = source;
940 UpdateSendState();
mallinath@webrtc.org67ee6b92014-02-03 16:57:16 +0000941 }
942
Taylor Brandstetter1a018dc2016-03-08 12:37:39 -0800943 // Stops sending by setting the sink of the AudioSource to nullptr. No data
mallinath@webrtc.org67ee6b92014-02-03 16:57:16 +0000944 // callback will be received after this method.
henrike@webrtc.orga7b98182014-02-21 15:51:43 +0000945 // This method is called on the libjingle worker thread.
Taylor Brandstetter1a018dc2016-03-08 12:37:39 -0800946 void ClearSource() {
solenberg566ef242015-11-06 15:34:49 -0800947 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
Taylor Brandstetter1a018dc2016-03-08 12:37:39 -0800948 if (source_) {
949 source_->SetSink(nullptr);
950 source_ = nullptr;
solenberg98c68862015-10-09 03:27:14 -0700951 }
Taylor Brandstetter1a018dc2016-03-08 12:37:39 -0800952 UpdateSendState();
mallinath@webrtc.org67ee6b92014-02-03 16:57:16 +0000953 }
954
Taylor Brandstetter1a018dc2016-03-08 12:37:39 -0800955 // AudioSource::Sink implementation.
henrike@webrtc.orga7b98182014-02-21 15:51:43 +0000956 // This method is called on the audio thread.
kjellander@webrtc.org14665ff2015-03-04 12:58:35 +0000957 void OnData(const void* audio_data,
958 int bits_per_sample,
959 int sample_rate,
Peter Kasting69558702016-01-12 16:26:35 -0800960 size_t number_of_channels,
Peter Kastingdce40cf2015-08-24 14:52:23 -0700961 size_t number_of_frames) override {
solenberg347ec5c2016-09-23 04:21:47 -0700962 RTC_CHECK_RUNS_SERIALIZED(&audio_capture_race_checker_);
solenbergc96df772015-10-21 13:01:53 -0700963 RTC_DCHECK(voe_audio_transport_);
maxmorin1aee0b52016-08-15 11:46:19 -0700964 voe_audio_transport_->PushCaptureData(config_.voe_channel_id, audio_data,
965 bits_per_sample, sample_rate,
966 number_of_channels, number_of_frames);
henrike@webrtc.orga7b98182014-02-21 15:51:43 +0000967 }
968
Taylor Brandstetter1a018dc2016-03-08 12:37:39 -0800969 // Callback from the |source_| when it is going away. In case Start() has
henrike@webrtc.orga7b98182014-02-21 15:51:43 +0000970 // never been called, this callback won't be triggered.
kjellander@webrtc.org14665ff2015-03-04 12:58:35 +0000971 void OnClose() override {
solenberg566ef242015-11-06 15:34:49 -0800972 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
Taylor Brandstetter1a018dc2016-03-08 12:37:39 -0800973 // Set |source_| to nullptr to make sure no more callback will get into
974 // the source.
975 source_ = nullptr;
976 UpdateSendState();
mallinath@webrtc.org67ee6b92014-02-03 16:57:16 +0000977 }
978
979 // Accessor to the VoE channel ID.
solenberg85a04962015-10-27 03:35:21 -0700980 int channel() const {
solenberg566ef242015-11-06 15:34:49 -0800981 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
solenberg7add0582015-11-20 09:59:34 -0800982 return config_.voe_channel_id;
solenberg85a04962015-10-27 03:35:21 -0700983 }
mallinath@webrtc.org67ee6b92014-02-03 16:57:16 +0000984
skvlade0d46372016-04-07 22:59:22 -0700985 const webrtc::RtpParameters& rtp_parameters() const {
986 return rtp_parameters_;
987 }
988
deadbeeffb2aced2017-01-06 23:05:37 -0800989 bool ValidateRtpParameters(const webrtc::RtpParameters& rtp_parameters) {
990 if (rtp_parameters.encodings.size() != 1) {
Mirko Bonadei675513b2017-11-09 11:09:25 +0100991 RTC_LOG(LS_ERROR)
deadbeeffb2aced2017-01-06 23:05:37 -0800992 << "Attempted to set RtpParameters without exactly one encoding";
993 return false;
994 }
995 if (rtp_parameters.encodings[0].ssrc != rtp_parameters_.encodings[0].ssrc) {
Mirko Bonadei675513b2017-11-09 11:09:25 +0100996 RTC_LOG(LS_ERROR) << "Attempted to set RtpParameters with modified SSRC";
deadbeeffb2aced2017-01-06 23:05:37 -0800997 return false;
998 }
999 return true;
1000 }
1001
minyue7a973442016-10-20 03:27:12 -07001002 bool SetRtpParameters(const webrtc::RtpParameters& parameters) {
deadbeeffb2aced2017-01-06 23:05:37 -08001003 if (!ValidateRtpParameters(parameters)) {
1004 return false;
1005 }
ossu20a4b3f2017-04-27 02:08:52 -07001006
1007 rtc::Optional<int> send_rate;
1008 if (audio_codec_spec_) {
1009 send_rate = ComputeSendBitrate(max_send_bitrate_bps_,
1010 parameters.encodings[0].max_bitrate_bps,
1011 *audio_codec_spec_);
1012 if (!send_rate) {
1013 return false;
1014 }
minyue7a973442016-10-20 03:27:12 -07001015 }
1016
minyuececec102017-03-27 13:04:25 -07001017 const rtc::Optional<int> old_rtp_max_bitrate =
1018 rtp_parameters_.encodings[0].max_bitrate_bps;
1019
skvlade0d46372016-04-07 22:59:22 -07001020 rtp_parameters_ = parameters;
minyue7a973442016-10-20 03:27:12 -07001021
minyuececec102017-03-27 13:04:25 -07001022 if (rtp_parameters_.encodings[0].max_bitrate_bps != old_rtp_max_bitrate) {
ossu20a4b3f2017-04-27 02:08:52 -07001023 // Reconfigure AudioSendStream with new bit rate.
1024 if (send_rate) {
1025 config_.send_codec_spec->target_bitrate_bps = send_rate;
1026 }
1027 UpdateAllowedBitrateRange();
1028 ReconfigureAudioSendStream();
minyue7a973442016-10-20 03:27:12 -07001029 } else {
1030 // parameters.encodings[0].active could have changed.
1031 UpdateSendState();
1032 }
1033 return true;
skvlade0d46372016-04-07 22:59:22 -07001034 }
1035
mallinath@webrtc.org67ee6b92014-02-03 16:57:16 +00001036 private:
Taylor Brandstetter1a018dc2016-03-08 12:37:39 -08001037 void UpdateSendState() {
1038 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
1039 RTC_DCHECK(stream_);
Taylor Brandstetter55dd7082016-05-03 13:50:11 -07001040 RTC_DCHECK_EQ(1UL, rtp_parameters_.encodings.size());
1041 if (send_ && source_ != nullptr && rtp_parameters_.encodings[0].active) {
Taylor Brandstetter1a018dc2016-03-08 12:37:39 -08001042 stream_->Start();
1043 } else { // !send || source_ = nullptr
1044 stream_->Stop();
1045 }
1046 }
1047
ossu20a4b3f2017-04-27 02:08:52 -07001048 void UpdateAllowedBitrateRange() {
michaelt53fe19d2016-10-18 09:39:22 -07001049 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
ossu20a4b3f2017-04-27 02:08:52 -07001050 const bool is_opus =
1051 config_.send_codec_spec &&
1052 !STR_CASE_CMP(config_.send_codec_spec->format.name.c_str(),
1053 kOpusCodecName);
1054 if (is_opus && webrtc::field_trial::IsEnabled("WebRTC-Audio-SendSideBwe")) {
stefane9f36d52017-01-24 08:18:45 -08001055 config_.min_bitrate_bps = kOpusMinBitrateBps;
minyuececec102017-03-27 13:04:25 -07001056
1057 // This means that when RtpParameters is reset, we may change the
ossu20a4b3f2017-04-27 02:08:52 -07001058 // encoder's bit rate immediately (through ReconfigureAudioSendStream()),
minyuececec102017-03-27 13:04:25 -07001059 // meanwhile change the cap to the output of BWE.
1060 config_.max_bitrate_bps =
1061 rtp_parameters_.encodings[0].max_bitrate_bps
1062 ? *rtp_parameters_.encodings[0].max_bitrate_bps
1063 : kOpusBitrateFbBps;
1064
michaelt53fe19d2016-10-18 09:39:22 -07001065 // TODO(mflodman): Keep testing this and set proper values.
1066 // Note: This is an early experiment currently only supported by Opus.
elad.alon0fe12162017-01-31 05:48:37 -08001067 if (send_side_bwe_with_overhead_) {
ossu20a4b3f2017-04-27 02:08:52 -07001068 const int max_packet_size_ms =
1069 WEBRTC_OPUS_SUPPORT_120MS_PTIME ? 120 : 60;
michaelt6672b262017-01-11 10:17:59 -08001070
ossu20a4b3f2017-04-27 02:08:52 -07001071 // OverheadPerPacket = Ipv4(20B) + UDP(8B) + SRTP(10B) + RTP(12)
1072 constexpr int kOverheadPerPacket = 20 + 8 + 10 + 12;
michaelt6672b262017-01-11 10:17:59 -08001073
ossu20a4b3f2017-04-27 02:08:52 -07001074 int min_overhead_bps =
1075 kOverheadPerPacket * 8 * 1000 / max_packet_size_ms;
michaelt6672b262017-01-11 10:17:59 -08001076
ossu20a4b3f2017-04-27 02:08:52 -07001077 // We assume that |config_.max_bitrate_bps| before the next line is
1078 // a hard limit on the payload bitrate, so we add min_overhead_bps to
1079 // it to ensure that, when overhead is deducted, the payload rate
1080 // never goes beyond the limit.
1081 // Note: this also means that if a higher overhead is forced, we
1082 // cannot reach the limit.
1083 // TODO(minyue): Reconsider this when the signaling to BWE is done
1084 // through a dedicated API.
1085 config_.max_bitrate_bps += min_overhead_bps;
michaelt6672b262017-01-11 10:17:59 -08001086
ossu20a4b3f2017-04-27 02:08:52 -07001087 // In contrast to max_bitrate_bps, we let min_bitrate_bps always be
1088 // reachable.
1089 config_.min_bitrate_bps += min_overhead_bps;
michaelt6672b262017-01-11 10:17:59 -08001090 }
michaelt53fe19d2016-10-18 09:39:22 -07001091 }
ossu20a4b3f2017-04-27 02:08:52 -07001092 }
1093
1094 void UpdateSendCodecSpec(
1095 const webrtc::AudioSendStream::Config::SendCodecSpec& send_codec_spec) {
1096 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
1097 config_.rtp.nack.rtp_history_ms =
1098 send_codec_spec.nack_enabled ? kNackRtpHistoryMs : 0;
Oskar Sundbom78807582017-11-16 11:09:55 +01001099 config_.send_codec_spec = send_codec_spec;
ossu20a4b3f2017-04-27 02:08:52 -07001100 auto info =
1101 config_.encoder_factory->QueryAudioEncoder(send_codec_spec.format);
1102 RTC_DCHECK(info);
1103 // If a specific target bitrate has been set for the stream, use that as
1104 // the new default bitrate when computing send bitrate.
1105 if (send_codec_spec.target_bitrate_bps) {
1106 info->default_bitrate_bps = std::max(
1107 info->min_bitrate_bps,
1108 std::min(info->max_bitrate_bps, *send_codec_spec.target_bitrate_bps));
1109 }
1110
1111 audio_codec_spec_.emplace(
1112 webrtc::AudioCodecSpec{send_codec_spec.format, *info});
1113
1114 config_.send_codec_spec->target_bitrate_bps = ComputeSendBitrate(
1115 max_send_bitrate_bps_, rtp_parameters_.encodings[0].max_bitrate_bps,
1116 *audio_codec_spec_);
1117
1118 UpdateAllowedBitrateRange();
1119 }
1120
1121 void ReconfigureAudioSendStream() {
1122 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
1123 RTC_DCHECK(stream_);
1124 stream_->Reconfigure(config_);
michaelt53fe19d2016-10-18 09:39:22 -07001125 }
1126
solenberg566ef242015-11-06 15:34:49 -08001127 rtc::ThreadChecker worker_thread_checker_;
solenberg347ec5c2016-09-23 04:21:47 -07001128 rtc::RaceChecker audio_capture_race_checker_;
solenbergc96df772015-10-21 13:01:53 -07001129 webrtc::AudioTransport* const voe_audio_transport_ = nullptr;
1130 webrtc::Call* call_ = nullptr;
solenberg3a941542015-11-16 07:34:50 -08001131 webrtc::AudioSendStream::Config config_;
elad.alon0fe12162017-01-31 05:48:37 -08001132 const bool send_side_bwe_with_overhead_;
solenberg3a941542015-11-16 07:34:50 -08001133 // The stream is owned by WebRtcAudioSendStream and may be reallocated if
1134 // configuration changes.
solenbergc96df772015-10-21 13:01:53 -07001135 webrtc::AudioSendStream* stream_ = nullptr;
mallinath@webrtc.org67ee6b92014-02-03 16:57:16 +00001136
Taylor Brandstetter1a018dc2016-03-08 12:37:39 -08001137 // Raw pointer to AudioSource owned by LocalAudioTrackHandler.
mallinath@webrtc.org67ee6b92014-02-03 16:57:16 +00001138 // PeerConnection will make sure invalidating the pointer before the object
1139 // goes away.
Taylor Brandstetter1a018dc2016-03-08 12:37:39 -08001140 AudioSource* source_ = nullptr;
1141 bool send_ = false;
solenberg94218532016-06-16 10:53:22 -07001142 bool muted_ = false;
minyue7a973442016-10-20 03:27:12 -07001143 int max_send_bitrate_bps_;
skvlade0d46372016-04-07 22:59:22 -07001144 webrtc::RtpParameters rtp_parameters_;
ossu20a4b3f2017-04-27 02:08:52 -07001145 rtc::Optional<webrtc::AudioCodecSpec> audio_codec_spec_;
henrike@webrtc.orga7b98182014-02-21 15:51:43 +00001146
solenbergc96df772015-10-21 13:01:53 -07001147 RTC_DISALLOW_IMPLICIT_CONSTRUCTORS(WebRtcAudioSendStream);
1148};
1149
1150class WebRtcVoiceMediaChannel::WebRtcAudioReceiveStream {
1151 public:
ossu29b1a8d2016-06-13 07:34:51 -07001152 WebRtcAudioReceiveStream(
1153 int ch,
1154 uint32_t remote_ssrc,
1155 uint32_t local_ssrc,
1156 bool use_transport_cc,
solenberg8189b022016-06-14 12:13:00 -07001157 bool use_nack,
ossu29b1a8d2016-06-13 07:34:51 -07001158 const std::string& sync_group,
1159 const std::vector<webrtc::RtpExtension>& extensions,
1160 webrtc::Call* call,
1161 webrtc::Transport* rtcp_send_transport,
kwiberg1c07c702017-03-27 07:15:49 -07001162 const rtc::scoped_refptr<webrtc::AudioDecoderFactory>& decoder_factory,
1163 const std::map<int, webrtc::SdpAudioFormat>& decoder_map)
stefanba4c0e42016-02-04 04:12:24 -08001164 : call_(call), config_() {
solenberg7add0582015-11-20 09:59:34 -08001165 RTC_DCHECK_GE(ch, 0);
1166 RTC_DCHECK(call);
1167 config_.rtp.remote_ssrc = remote_ssrc;
kwibergd32bf752017-01-19 07:03:59 -08001168 config_.rtp.local_ssrc = local_ssrc;
1169 config_.rtp.transport_cc = use_transport_cc;
1170 config_.rtp.nack.rtp_history_ms = use_nack ? kNackRtpHistoryMs : 0;
1171 config_.rtp.extensions = extensions;
solenberg31fec402016-05-06 02:13:12 -07001172 config_.rtcp_send_transport = rtcp_send_transport;
solenberg7add0582015-11-20 09:59:34 -08001173 config_.voe_channel_id = ch;
1174 config_.sync_group = sync_group;
ossu29b1a8d2016-06-13 07:34:51 -07001175 config_.decoder_factory = decoder_factory;
kwiberg1c07c702017-03-27 07:15:49 -07001176 config_.decoder_map = decoder_map;
kwibergd32bf752017-01-19 07:03:59 -08001177 RecreateAudioReceiveStream();
solenberg7add0582015-11-20 09:59:34 -08001178 }
solenbergc96df772015-10-21 13:01:53 -07001179
solenberg7add0582015-11-20 09:59:34 -08001180 ~WebRtcAudioReceiveStream() {
1181 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
1182 call_->DestroyAudioReceiveStream(stream_);
1183 }
1184
solenberg4a0f7b52016-06-16 13:07:33 -07001185 void RecreateAudioReceiveStream(uint32_t local_ssrc) {
solenberg7add0582015-11-20 09:59:34 -08001186 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
kwibergd32bf752017-01-19 07:03:59 -08001187 config_.rtp.local_ssrc = local_ssrc;
1188 RecreateAudioReceiveStream();
solenberg7add0582015-11-20 09:59:34 -08001189 }
solenberg8189b022016-06-14 12:13:00 -07001190
1191 void RecreateAudioReceiveStream(bool use_transport_cc, bool use_nack) {
solenberg7add0582015-11-20 09:59:34 -08001192 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
kwibergd32bf752017-01-19 07:03:59 -08001193 config_.rtp.transport_cc = use_transport_cc;
1194 config_.rtp.nack.rtp_history_ms = use_nack ? kNackRtpHistoryMs : 0;
1195 RecreateAudioReceiveStream();
solenberg7add0582015-11-20 09:59:34 -08001196 }
1197
solenberg4a0f7b52016-06-16 13:07:33 -07001198 void RecreateAudioReceiveStream(
1199 const std::vector<webrtc::RtpExtension>& extensions) {
1200 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
kwibergd32bf752017-01-19 07:03:59 -08001201 config_.rtp.extensions = extensions;
1202 RecreateAudioReceiveStream();
1203 }
1204
deadbeefcb383672017-04-26 16:28:42 -07001205 // Set a new payload type -> decoder map.
kwibergd32bf752017-01-19 07:03:59 -08001206 void RecreateAudioReceiveStream(
1207 const std::map<int, webrtc::SdpAudioFormat>& decoder_map) {
1208 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
kwibergd32bf752017-01-19 07:03:59 -08001209 config_.decoder_map = decoder_map;
1210 RecreateAudioReceiveStream();
solenberg4a0f7b52016-06-16 13:07:33 -07001211 }
1212
solenberg4904fb62017-02-17 12:01:14 -08001213 void MaybeRecreateAudioReceiveStream(const std::string& sync_group) {
1214 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
1215 if (config_.sync_group != sync_group) {
1216 config_.sync_group = sync_group;
1217 RecreateAudioReceiveStream();
1218 }
1219 }
1220
solenberg7add0582015-11-20 09:59:34 -08001221 webrtc::AudioReceiveStream::Stats GetStats() const {
1222 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
1223 RTC_DCHECK(stream_);
1224 return stream_->GetStats();
1225 }
1226
solenberg796b8f92017-03-01 17:02:23 -08001227 int GetOutputLevel() const {
1228 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
1229 RTC_DCHECK(stream_);
1230 return stream_->GetOutputLevel();
1231 }
1232
solenberg7add0582015-11-20 09:59:34 -08001233 int channel() const {
1234 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
1235 return config_.voe_channel_id;
1236 }
solenbergc96df772015-10-21 13:01:53 -07001237
kwiberg686a8ef2016-02-26 03:00:35 -08001238 void SetRawAudioSink(std::unique_ptr<webrtc::AudioSinkInterface> sink) {
Tommif888bb52015-12-12 01:37:01 +01001239 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
kwiberg686a8ef2016-02-26 03:00:35 -08001240 stream_->SetSink(std::move(sink));
Tommif888bb52015-12-12 01:37:01 +01001241 }
1242
solenberg217fb662016-06-17 08:30:54 -07001243 void SetOutputVolume(double volume) {
1244 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
1245 stream_->SetGain(volume);
1246 }
1247
aleloi84ef6152016-08-04 05:28:21 -07001248 void SetPlayout(bool playout) {
1249 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
1250 RTC_DCHECK(stream_);
1251 if (playout) {
Mirko Bonadei675513b2017-11-09 11:09:25 +01001252 RTC_LOG(LS_INFO) << "Starting playout for channel #" << channel();
aleloi84ef6152016-08-04 05:28:21 -07001253 stream_->Start();
1254 } else {
Mirko Bonadei675513b2017-11-09 11:09:25 +01001255 RTC_LOG(LS_INFO) << "Stopping playout for channel #" << channel();
aleloi84ef6152016-08-04 05:28:21 -07001256 stream_->Stop();
1257 }
aleloi18e0b672016-10-04 02:45:47 -07001258 playout_ = playout;
aleloi84ef6152016-08-04 05:28:21 -07001259 }
1260
hbos8d609f62017-04-10 07:39:05 -07001261 std::vector<webrtc::RtpSource> GetSources() {
1262 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
1263 RTC_DCHECK(stream_);
1264 return stream_->GetSources();
1265 }
1266
solenbergc96df772015-10-21 13:01:53 -07001267 private:
kwibergd32bf752017-01-19 07:03:59 -08001268 void RecreateAudioReceiveStream() {
solenberg7add0582015-11-20 09:59:34 -08001269 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
1270 if (stream_) {
1271 call_->DestroyAudioReceiveStream(stream_);
solenberg7add0582015-11-20 09:59:34 -08001272 }
solenberg7add0582015-11-20 09:59:34 -08001273 stream_ = call_->CreateAudioReceiveStream(config_);
1274 RTC_CHECK(stream_);
aleloi18e0b672016-10-04 02:45:47 -07001275 SetPlayout(playout_);
solenberg7add0582015-11-20 09:59:34 -08001276 }
1277
1278 rtc::ThreadChecker worker_thread_checker_;
1279 webrtc::Call* call_ = nullptr;
1280 webrtc::AudioReceiveStream::Config config_;
1281 // The stream is owned by WebRtcAudioReceiveStream and may be reallocated if
1282 // configuration changes.
1283 webrtc::AudioReceiveStream* stream_ = nullptr;
aleloi18e0b672016-10-04 02:45:47 -07001284 bool playout_ = false;
solenbergc96df772015-10-21 13:01:53 -07001285
1286 RTC_DISALLOW_IMPLICIT_CONSTRUCTORS(WebRtcAudioReceiveStream);
henrike@webrtc.org1e09a712013-07-26 19:17:59 +00001287};
1288
Fredrik Solenberg709ed672015-09-15 12:26:33 +02001289WebRtcVoiceMediaChannel::WebRtcVoiceMediaChannel(WebRtcVoiceEngine* engine,
nisse51542be2016-02-12 02:27:06 -08001290 const MediaConfig& config,
Fredrik Solenbergb071a192015-09-17 16:42:56 +02001291 const AudioOptions& options,
Fredrik Solenberg709ed672015-09-15 12:26:33 +02001292 webrtc::Call* call)
nisse51542be2016-02-12 02:27:06 -08001293 : VoiceMediaChannel(config), engine_(engine), call_(call) {
Mirko Bonadei675513b2017-11-09 11:09:25 +01001294 RTC_LOG(LS_VERBOSE) << "WebRtcVoiceMediaChannel::WebRtcVoiceMediaChannel";
solenberg566ef242015-11-06 15:34:49 -08001295 RTC_DCHECK(call);
solenberg0a617e22015-10-20 15:49:38 -07001296 engine->RegisterChannel(this);
Fredrik Solenbergb071a192015-09-17 16:42:56 +02001297 SetOptions(options);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001298}
1299
1300WebRtcVoiceMediaChannel::~WebRtcVoiceMediaChannel() {
solenberg566ef242015-11-06 15:34:49 -08001301 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
Mirko Bonadei675513b2017-11-09 11:09:25 +01001302 RTC_LOG(LS_VERBOSE) << "WebRtcVoiceMediaChannel::~WebRtcVoiceMediaChannel";
solenberg7add0582015-11-20 09:59:34 -08001303 // TODO(solenberg): Should be able to delete the streams directly, without
1304 // going through RemoveNnStream(), once stream objects handle
1305 // all (de)configuration.
solenbergc96df772015-10-21 13:01:53 -07001306 while (!send_streams_.empty()) {
1307 RemoveSendStream(send_streams_.begin()->first);
solenbergd97ec302015-10-07 01:40:33 -07001308 }
solenberg7add0582015-11-20 09:59:34 -08001309 while (!recv_streams_.empty()) {
1310 RemoveRecvStream(recv_streams_.begin()->first);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001311 }
solenberg0a617e22015-10-20 15:49:38 -07001312 engine()->UnregisterChannel(this);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001313}
1314
nisse51542be2016-02-12 02:27:06 -08001315rtc::DiffServCodePoint WebRtcVoiceMediaChannel::PreferredDscp() const {
1316 return kAudioDscpValue;
1317}
1318
Peter Thatcherc2ee2c82015-08-07 16:05:34 -07001319bool WebRtcVoiceMediaChannel::SetSendParameters(
1320 const AudioSendParameters& params) {
Peter Boströmca8b4042016-03-08 14:24:13 -08001321 TRACE_EVENT0("webrtc", "WebRtcVoiceMediaChannel::SetSendParameters");
solenberg566ef242015-11-06 15:34:49 -08001322 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
Mirko Bonadei675513b2017-11-09 11:09:25 +01001323 RTC_LOG(LS_INFO) << "WebRtcVoiceMediaChannel::SetSendParameters: "
1324 << params.ToString();
Peter Thatcherc2ee2c82015-08-07 16:05:34 -07001325 // TODO(pthatcher): Refactor this to be more clean now that we have
1326 // all the information at once.
solenberg3a941542015-11-16 07:34:50 -08001327
1328 if (!SetSendCodecs(params.codecs)) {
1329 return false;
1330 }
1331
solenberg7e4e01a2015-12-02 08:05:01 -08001332 if (!ValidateRtpExtensions(params.extensions)) {
1333 return false;
1334 }
1335 std::vector<webrtc::RtpExtension> filtered_extensions =
1336 FilterRtpExtensions(params.extensions,
1337 webrtc::RtpExtension::IsSupportedForAudio, true);
1338 if (send_rtp_extensions_ != filtered_extensions) {
1339 send_rtp_extensions_.swap(filtered_extensions);
solenberg3a941542015-11-16 07:34:50 -08001340 for (auto& it : send_streams_) {
ossu20a4b3f2017-04-27 02:08:52 -07001341 it.second->SetRtpExtensions(send_rtp_extensions_);
solenberg3a941542015-11-16 07:34:50 -08001342 }
1343 }
1344
deadbeef80346142016-04-27 14:17:10 -07001345 if (!SetMaxSendBitrate(params.max_bandwidth_bps)) {
solenberg3a941542015-11-16 07:34:50 -08001346 return false;
1347 }
1348 return SetOptions(params.options);
Peter Thatcherc2ee2c82015-08-07 16:05:34 -07001349}
1350
1351bool WebRtcVoiceMediaChannel::SetRecvParameters(
1352 const AudioRecvParameters& params) {
Peter Boströmca8b4042016-03-08 14:24:13 -08001353 TRACE_EVENT0("webrtc", "WebRtcVoiceMediaChannel::SetRecvParameters");
solenberg566ef242015-11-06 15:34:49 -08001354 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
Mirko Bonadei675513b2017-11-09 11:09:25 +01001355 RTC_LOG(LS_INFO) << "WebRtcVoiceMediaChannel::SetRecvParameters: "
1356 << params.ToString();
Peter Thatcherc2ee2c82015-08-07 16:05:34 -07001357 // TODO(pthatcher): Refactor this to be more clean now that we have
1358 // all the information at once.
solenberg7add0582015-11-20 09:59:34 -08001359
1360 if (!SetRecvCodecs(params.codecs)) {
1361 return false;
1362 }
1363
solenberg7e4e01a2015-12-02 08:05:01 -08001364 if (!ValidateRtpExtensions(params.extensions)) {
1365 return false;
1366 }
1367 std::vector<webrtc::RtpExtension> filtered_extensions =
1368 FilterRtpExtensions(params.extensions,
1369 webrtc::RtpExtension::IsSupportedForAudio, false);
1370 if (recv_rtp_extensions_ != filtered_extensions) {
1371 recv_rtp_extensions_.swap(filtered_extensions);
solenberg7add0582015-11-20 09:59:34 -08001372 for (auto& it : recv_streams_) {
1373 it.second->RecreateAudioReceiveStream(recv_rtp_extensions_);
1374 }
1375 }
solenberg7add0582015-11-20 09:59:34 -08001376 return true;
Peter Thatcherc2ee2c82015-08-07 16:05:34 -07001377}
1378
Taylor Brandstetterdb0cd9e2016-05-16 11:40:30 -07001379webrtc::RtpParameters WebRtcVoiceMediaChannel::GetRtpSendParameters(
skvlade0d46372016-04-07 22:59:22 -07001380 uint32_t ssrc) const {
1381 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
1382 auto it = send_streams_.find(ssrc);
1383 if (it == send_streams_.end()) {
Mirko Bonadei675513b2017-11-09 11:09:25 +01001384 RTC_LOG(LS_WARNING) << "Attempting to get RTP send parameters for stream "
1385 << "with ssrc " << ssrc << " which doesn't exist.";
skvlade0d46372016-04-07 22:59:22 -07001386 return webrtc::RtpParameters();
1387 }
1388
Taylor Brandstetter0cd086b2016-04-20 16:23:10 -07001389 webrtc::RtpParameters rtp_params = it->second->rtp_parameters();
1390 // Need to add the common list of codecs to the send stream-specific
1391 // RTP parameters.
1392 for (const AudioCodec& codec : send_codecs_) {
1393 rtp_params.codecs.push_back(codec.ToCodecParameters());
1394 }
1395 return rtp_params;
skvlade0d46372016-04-07 22:59:22 -07001396}
1397
Taylor Brandstetterdb0cd9e2016-05-16 11:40:30 -07001398bool WebRtcVoiceMediaChannel::SetRtpSendParameters(
skvlade0d46372016-04-07 22:59:22 -07001399 uint32_t ssrc,
1400 const webrtc::RtpParameters& parameters) {
1401 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
skvlade0d46372016-04-07 22:59:22 -07001402 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 set RTP send parameters for stream "
1405 << "with ssrc " << ssrc << " which doesn't exist.";
skvlade0d46372016-04-07 22:59:22 -07001406 return false;
1407 }
1408
Taylor Brandstetterdb0cd9e2016-05-16 11:40:30 -07001409 // TODO(deadbeef): Handle setting parameters with a list of codecs in a
1410 // different order (which should change the send codec).
1411 webrtc::RtpParameters current_parameters = GetRtpSendParameters(ssrc);
1412 if (current_parameters.codecs != parameters.codecs) {
Mirko Bonadei675513b2017-11-09 11:09:25 +01001413 RTC_LOG(LS_ERROR) << "Using SetParameters to change the set of codecs "
1414 << "is not currently supported.";
Taylor Brandstetterdb0cd9e2016-05-16 11:40:30 -07001415 return false;
1416 }
1417
minyue7a973442016-10-20 03:27:12 -07001418 // TODO(minyue): The following legacy actions go into
1419 // |WebRtcAudioSendStream::SetRtpParameters()| which is called at the end,
1420 // though there are two difference:
1421 // 1. |WebRtcVoiceMediaChannel::SetChannelSendParameters()| only calls
1422 // |SetSendCodec| while |WebRtcAudioSendStream::SetRtpParameters()| calls
1423 // |SetSendCodecs|. The outcome should be the same.
1424 // 2. AudioSendStream can be recreated.
1425
Taylor Brandstetter0cd086b2016-04-20 16:23:10 -07001426 // Codecs are handled at the WebRtcVoiceMediaChannel level.
1427 webrtc::RtpParameters reduced_params = parameters;
1428 reduced_params.codecs.clear();
minyue7a973442016-10-20 03:27:12 -07001429 return it->second->SetRtpParameters(reduced_params);
skvlade0d46372016-04-07 22:59:22 -07001430}
1431
Taylor Brandstetterdb0cd9e2016-05-16 11:40:30 -07001432webrtc::RtpParameters WebRtcVoiceMediaChannel::GetRtpReceiveParameters(
1433 uint32_t ssrc) const {
1434 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
deadbeef3bc15102017-04-20 19:25:07 -07001435 webrtc::RtpParameters rtp_params;
1436 // SSRC of 0 represents the default receive stream.
1437 if (ssrc == 0) {
1438 if (!default_sink_) {
Mirko Bonadei675513b2017-11-09 11:09:25 +01001439 RTC_LOG(LS_WARNING)
1440 << "Attempting to get RTP parameters for the default, "
1441 "unsignaled audio receive stream, but not yet "
1442 "configured to receive such a stream.";
deadbeef3bc15102017-04-20 19:25:07 -07001443 return rtp_params;
1444 }
1445 rtp_params.encodings.emplace_back();
1446 } else {
1447 auto it = recv_streams_.find(ssrc);
1448 if (it == recv_streams_.end()) {
Mirko Bonadei675513b2017-11-09 11:09:25 +01001449 RTC_LOG(LS_WARNING)
1450 << "Attempting to get RTP receive parameters for stream "
1451 << "with ssrc " << ssrc << " which doesn't exist.";
deadbeef3bc15102017-04-20 19:25:07 -07001452 return webrtc::RtpParameters();
1453 }
1454 rtp_params.encodings.emplace_back();
1455 // TODO(deadbeef): Return stream-specific parameters.
Oskar Sundbom78807582017-11-16 11:09:55 +01001456 rtp_params.encodings[0].ssrc = ssrc;
Taylor Brandstetterdb0cd9e2016-05-16 11:40:30 -07001457 }
1458
Taylor Brandstetterdb0cd9e2016-05-16 11:40:30 -07001459 for (const AudioCodec& codec : recv_codecs_) {
1460 rtp_params.codecs.push_back(codec.ToCodecParameters());
1461 }
1462 return rtp_params;
1463}
1464
1465bool WebRtcVoiceMediaChannel::SetRtpReceiveParameters(
1466 uint32_t ssrc,
1467 const webrtc::RtpParameters& parameters) {
1468 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
deadbeef3bc15102017-04-20 19:25:07 -07001469 // SSRC of 0 represents the default receive stream.
1470 if (ssrc == 0) {
1471 if (!default_sink_) {
Mirko Bonadei675513b2017-11-09 11:09:25 +01001472 RTC_LOG(LS_WARNING)
1473 << "Attempting to set RTP parameters for the default, "
1474 "unsignaled audio receive stream, but not yet "
1475 "configured to receive such a stream.";
deadbeef3bc15102017-04-20 19:25:07 -07001476 return false;
1477 }
1478 } else {
1479 auto it = recv_streams_.find(ssrc);
1480 if (it == recv_streams_.end()) {
Mirko Bonadei675513b2017-11-09 11:09:25 +01001481 RTC_LOG(LS_WARNING)
1482 << "Attempting to set RTP receive parameters for stream "
1483 << "with ssrc " << ssrc << " which doesn't exist.";
deadbeef3bc15102017-04-20 19:25:07 -07001484 return false;
1485 }
Taylor Brandstetterdb0cd9e2016-05-16 11:40:30 -07001486 }
1487
1488 webrtc::RtpParameters current_parameters = GetRtpReceiveParameters(ssrc);
1489 if (current_parameters != parameters) {
Mirko Bonadei675513b2017-11-09 11:09:25 +01001490 RTC_LOG(LS_ERROR) << "Changing the RTP receive parameters is currently "
1491 << "unsupported.";
Taylor Brandstetterdb0cd9e2016-05-16 11:40:30 -07001492 return false;
1493 }
1494 return true;
1495}
1496
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001497bool WebRtcVoiceMediaChannel::SetOptions(const AudioOptions& options) {
solenberg566ef242015-11-06 15:34:49 -08001498 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
Mirko Bonadei675513b2017-11-09 11:09:25 +01001499 RTC_LOG(LS_INFO) << "Setting voice channel options: " << options.ToString();
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001500
1501 // We retain all of the existing options, and apply the given ones
1502 // on top. This means there is no way to "clear" options such that
1503 // they go back to the engine default.
1504 options_.SetAll(options);
solenberg246b8172015-12-08 09:50:23 -08001505 if (!engine()->ApplyOptions(options_)) {
Mirko Bonadei675513b2017-11-09 11:09:25 +01001506 RTC_LOG(LS_WARNING)
1507 << "Failed to apply engine options during channel SetOptions.";
solenberg246b8172015-12-08 09:50:23 -08001508 return false;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001509 }
minyue6b825df2016-10-31 04:08:32 -07001510
ossu20a4b3f2017-04-27 02:08:52 -07001511 rtc::Optional<std::string> audio_network_adaptor_config =
minyue6b825df2016-10-31 04:08:32 -07001512 GetAudioNetworkAdaptorConfig(options_);
1513 for (auto& it : send_streams_) {
ossu20a4b3f2017-04-27 02:08:52 -07001514 it.second->SetAudioNetworkAdaptorConfig(audio_network_adaptor_config);
minyue6b825df2016-10-31 04:08:32 -07001515 }
1516
Mirko Bonadei675513b2017-11-09 11:09:25 +01001517 RTC_LOG(LS_INFO) << "Set voice channel options. Current options: "
1518 << options_.ToString();
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001519 return true;
1520}
1521
1522bool WebRtcVoiceMediaChannel::SetRecvCodecs(
1523 const std::vector<AudioCodec>& codecs) {
solenberg566ef242015-11-06 15:34:49 -08001524 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
solenberg8fb30c32015-10-13 03:06:58 -07001525
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001526 // Set the payload types to be used for incoming media.
Mirko Bonadei675513b2017-11-09 11:09:25 +01001527 RTC_LOG(LS_INFO) << "Setting receive voice codecs.";
solenberg0b675462015-10-09 01:37:09 -07001528
1529 if (!VerifyUniquePayloadTypes(codecs)) {
Mirko Bonadei675513b2017-11-09 11:09:25 +01001530 RTC_LOG(LS_ERROR) << "Codec payload types overlap.";
solenberg0b675462015-10-09 01:37:09 -07001531 return false;
1532 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001533
kwibergd32bf752017-01-19 07:03:59 -08001534 // Create a payload type -> SdpAudioFormat map with all the decoders. Fail
1535 // unless the factory claims to support all decoders.
1536 std::map<int, webrtc::SdpAudioFormat> decoder_map;
1537 for (const AudioCodec& codec : codecs) {
deadbeefcb383672017-04-26 16:28:42 -07001538 // Log a warning if a codec's payload type is changing. This used to be
1539 // treated as an error. It's abnormal, but not really illegal.
1540 AudioCodec old_codec;
1541 if (FindCodec(recv_codecs_, codec, &old_codec) &&
1542 old_codec.id != codec.id) {
Mirko Bonadei675513b2017-11-09 11:09:25 +01001543 RTC_LOG(LS_WARNING) << codec.name << " mapped to a second payload type ("
1544 << codec.id << ", was already mapped to "
1545 << old_codec.id << ")";
deadbeefcb383672017-04-26 16:28:42 -07001546 }
kwibergd32bf752017-01-19 07:03:59 -08001547 auto format = AudioCodecToSdpAudioFormat(codec);
1548 if (!IsCodec(codec, "cn") && !IsCodec(codec, "telephone-event") &&
1549 !engine()->decoder_factory_->IsSupportedDecoder(format)) {
Mirko Bonadei675513b2017-11-09 11:09:25 +01001550 RTC_LOG(LS_ERROR) << "Unsupported codec: " << format;
kwibergd32bf752017-01-19 07:03:59 -08001551 return false;
1552 }
deadbeefcb383672017-04-26 16:28:42 -07001553 // We allow adding new codecs but don't allow changing the payload type of
1554 // codecs that are already configured since we might already be receiving
1555 // packets with that payload type. See RFC3264, Section 8.3.2.
1556 // TODO(deadbeef): Also need to check for clashes with previously mapped
1557 // payload types, and not just currently mapped ones. For example, this
1558 // should be illegal:
1559 // 1. {100: opus/48000/2, 101: ISAC/16000}
1560 // 2. {100: opus/48000/2}
1561 // 3. {100: opus/48000/2, 101: ISAC/32000}
1562 // Though this check really should happen at a higher level, since this
1563 // conflict could happen between audio and video codecs.
1564 auto existing = decoder_map_.find(codec.id);
1565 if (existing != decoder_map_.end() && !existing->second.Matches(format)) {
Mirko Bonadei675513b2017-11-09 11:09:25 +01001566 RTC_LOG(LS_ERROR) << "Attempting to use payload type " << codec.id
1567 << " for " << codec.name
1568 << ", but it is already used for "
1569 << existing->second.name;
deadbeefcb383672017-04-26 16:28:42 -07001570 return false;
1571 }
kwibergd32bf752017-01-19 07:03:59 -08001572 decoder_map.insert({codec.id, std::move(format)});
1573 }
1574
deadbeefcb383672017-04-26 16:28:42 -07001575 if (decoder_map == decoder_map_) {
1576 // There's nothing new to configure.
1577 return true;
1578 }
1579
kwiberg37b8b112016-11-03 02:46:53 -07001580 if (playout_) {
1581 // Receive codecs can not be changed while playing. So we temporarily
1582 // pause playout.
1583 ChangePlayout(false);
1584 }
1585
kwiberg1c07c702017-03-27 07:15:49 -07001586 decoder_map_ = std::move(decoder_map);
kwibergd32bf752017-01-19 07:03:59 -08001587 for (auto& kv : recv_streams_) {
kwiberg1c07c702017-03-27 07:15:49 -07001588 kv.second->RecreateAudioReceiveStream(decoder_map_);
solenberg26c8c912015-11-27 04:00:25 -08001589 }
kwibergd32bf752017-01-19 07:03:59 -08001590 recv_codecs_ = codecs;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001591
kwiberg37b8b112016-11-03 02:46:53 -07001592 if (desired_playout_ && !playout_) {
1593 ChangePlayout(desired_playout_);
1594 }
kwibergd32bf752017-01-19 07:03:59 -08001595 return true;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001596}
1597
solenberg72e29d22016-03-08 06:35:16 -08001598// Utility function called from SetSendParameters() to extract current send
1599// codec settings from the given list of codecs (originally from SDP). Both send
1600// and receive streams may be reconfigured based on the new settings.
wu@webrtc.orgcadf9042013-08-30 21:24:16 +00001601bool WebRtcVoiceMediaChannel::SetSendCodecs(
1602 const std::vector<AudioCodec>& codecs) {
solenberg566ef242015-11-06 15:34:49 -08001603 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
Oskar Sundbom78807582017-11-16 11:09:55 +01001604 dtmf_payload_type_ = rtc::nullopt;
solenbergffbbcac2016-11-17 05:25:37 -08001605 dtmf_payload_freq_ = -1;
1606
1607 // Validate supplied codecs list.
1608 for (const AudioCodec& codec : codecs) {
1609 // TODO(solenberg): Validate more aspects of input - that payload types
1610 // don't overlap, remove redundant/unsupported codecs etc -
1611 // the same way it is done for RtpHeaderExtensions.
1612 if (codec.id < kMinPayloadType || codec.id > kMaxPayloadType) {
Mirko Bonadei675513b2017-11-09 11:09:25 +01001613 RTC_LOG(LS_WARNING) << "Codec payload type out of range: "
1614 << ToString(codec);
solenbergffbbcac2016-11-17 05:25:37 -08001615 return false;
1616 }
1617 }
1618
1619 // Find PT of telephone-event codec with lowest clockrate, as a fallback, in
1620 // case we don't have a DTMF codec with a rate matching the send codec's, or
1621 // if this function returns early.
1622 std::vector<AudioCodec> dtmf_codecs;
Fredrik Solenbergaf9fb212015-08-26 10:45:53 +02001623 for (const AudioCodec& codec : codecs) {
Fredrik Solenbergaf9fb212015-08-26 10:45:53 +02001624 if (IsCodec(codec, kDtmfCodecName)) {
solenbergffbbcac2016-11-17 05:25:37 -08001625 dtmf_codecs.push_back(codec);
1626 if (!dtmf_payload_type_ || codec.clockrate < dtmf_payload_freq_) {
Oskar Sundbom78807582017-11-16 11:09:55 +01001627 dtmf_payload_type_ = codec.id;
solenbergffbbcac2016-11-17 05:25:37 -08001628 dtmf_payload_freq_ = codec.clockrate;
solenberg31642aa2016-03-14 08:00:37 -07001629 }
wu@webrtc.orgcadf9042013-08-30 21:24:16 +00001630 }
1631 }
1632
ossu20a4b3f2017-04-27 02:08:52 -07001633 // Scan through the list to figure out the codec to use for sending.
1634 rtc::Optional<webrtc::AudioSendStream::Config::SendCodecSpec> send_codec_spec;
stefan1ccf73f2017-03-27 03:51:18 -07001635 webrtc::Call::Config::BitrateConfig bitrate_config;
ossu20a4b3f2017-04-27 02:08:52 -07001636 rtc::Optional<webrtc::AudioCodecInfo> voice_codec_info;
1637 for (const AudioCodec& voice_codec : codecs) {
1638 if (!(IsCodec(voice_codec, kCnCodecName) ||
1639 IsCodec(voice_codec, kDtmfCodecName) ||
1640 IsCodec(voice_codec, kRedCodecName))) {
1641 webrtc::SdpAudioFormat format(voice_codec.name, voice_codec.clockrate,
1642 voice_codec.channels, voice_codec.params);
solenberg72e29d22016-03-08 06:35:16 -08001643
ossu20a4b3f2017-04-27 02:08:52 -07001644 voice_codec_info = engine()->encoder_factory_->QueryAudioEncoder(format);
1645 if (!voice_codec_info) {
Mirko Bonadei675513b2017-11-09 11:09:25 +01001646 RTC_LOG(LS_WARNING) << "Unknown codec " << ToString(voice_codec);
solenberg72e29d22016-03-08 06:35:16 -08001647 continue;
1648 }
1649
Oskar Sundbom78807582017-11-16 11:09:55 +01001650 send_codec_spec = webrtc::AudioSendStream::Config::SendCodecSpec(
1651 voice_codec.id, format);
ossu20a4b3f2017-04-27 02:08:52 -07001652 if (voice_codec.bitrate > 0) {
Oskar Sundbom78807582017-11-16 11:09:55 +01001653 send_codec_spec->target_bitrate_bps = voice_codec.bitrate;
ossu20a4b3f2017-04-27 02:08:52 -07001654 }
1655 send_codec_spec->transport_cc_enabled = HasTransportCc(voice_codec);
1656 send_codec_spec->nack_enabled = HasNack(voice_codec);
1657 bitrate_config = GetBitrateConfigForCodec(voice_codec);
1658 break;
1659 }
1660 }
1661
1662 if (!send_codec_spec) {
1663 return false;
1664 }
1665
1666 RTC_DCHECK(voice_codec_info);
1667 if (voice_codec_info->allow_comfort_noise) {
1668 // Loop through the codecs list again to find the CN codec.
1669 // TODO(solenberg): Break out into a separate function?
1670 for (const AudioCodec& cn_codec : codecs) {
ossu0c4b8492017-03-02 11:03:25 -08001671 if (IsCodec(cn_codec, kCnCodecName) &&
ossu20a4b3f2017-04-27 02:08:52 -07001672 cn_codec.clockrate == send_codec_spec->format.clockrate_hz) {
ossu0c4b8492017-03-02 11:03:25 -08001673 switch (cn_codec.clockrate) {
solenberg72e29d22016-03-08 06:35:16 -08001674 case 8000:
1675 case 16000:
1676 case 32000:
Oskar Sundbom78807582017-11-16 11:09:55 +01001677 send_codec_spec->cng_payload_type = cn_codec.id;
solenberg72e29d22016-03-08 06:35:16 -08001678 break;
1679 default:
Mirko Bonadei675513b2017-11-09 11:09:25 +01001680 RTC_LOG(LS_WARNING)
1681 << "CN frequency " << cn_codec.clockrate << " not supported.";
ossu20a4b3f2017-04-27 02:08:52 -07001682 break;
solenberg72e29d22016-03-08 06:35:16 -08001683 }
solenberg72e29d22016-03-08 06:35:16 -08001684 break;
1685 }
1686 }
solenbergffbbcac2016-11-17 05:25:37 -08001687
1688 // Find the telephone-event PT exactly matching the preferred send codec.
1689 for (const AudioCodec& dtmf_codec : dtmf_codecs) {
ossu20a4b3f2017-04-27 02:08:52 -07001690 if (dtmf_codec.clockrate == send_codec_spec->format.clockrate_hz) {
Oskar Sundbom78807582017-11-16 11:09:55 +01001691 dtmf_payload_type_ = dtmf_codec.id;
solenbergffbbcac2016-11-17 05:25:37 -08001692 dtmf_payload_freq_ = dtmf_codec.clockrate;
1693 break;
1694 }
1695 }
solenberg72e29d22016-03-08 06:35:16 -08001696 }
1697
solenberg971cab02016-06-14 10:02:41 -07001698 if (send_codec_spec_ != send_codec_spec) {
1699 send_codec_spec_ = std::move(send_codec_spec);
stefan13f1a0a2016-11-30 07:22:58 -08001700 // Apply new settings to all streams.
solenberg971cab02016-06-14 10:02:41 -07001701 for (const auto& kv : send_streams_) {
ossu20a4b3f2017-04-27 02:08:52 -07001702 kv.second->SetSendCodecSpec(*send_codec_spec_);
wu@webrtc.orgcadf9042013-08-30 21:24:16 +00001703 }
stefan13f1a0a2016-11-30 07:22:58 -08001704 } else {
1705 // If the codec isn't changing, set the start bitrate to -1 which means
1706 // "unchanged" so that BWE isn't affected.
stefan1ccf73f2017-03-27 03:51:18 -07001707 bitrate_config.start_bitrate_bps = -1;
wu@webrtc.orgcadf9042013-08-30 21:24:16 +00001708 }
stefan1ccf73f2017-03-27 03:51:18 -07001709 call_->SetBitrateConfig(bitrate_config);
wu@webrtc.orgcadf9042013-08-30 21:24:16 +00001710
solenberg8189b022016-06-14 12:13:00 -07001711 // Check if the transport cc feedback or NACK status has changed on the
1712 // preferred send codec, and in that case reconfigure all receive streams.
ossu20a4b3f2017-04-27 02:08:52 -07001713 if (recv_transport_cc_enabled_ != send_codec_spec_->transport_cc_enabled ||
1714 recv_nack_enabled_ != send_codec_spec_->nack_enabled) {
Mirko Bonadei675513b2017-11-09 11:09:25 +01001715 RTC_LOG(LS_INFO) << "Recreate all the receive streams because the send "
1716 "codec has changed.";
ossu20a4b3f2017-04-27 02:08:52 -07001717 recv_transport_cc_enabled_ = send_codec_spec_->transport_cc_enabled;
1718 recv_nack_enabled_ = send_codec_spec_->nack_enabled;
solenberg72e29d22016-03-08 06:35:16 -08001719 for (auto& kv : recv_streams_) {
solenberg8189b022016-06-14 12:13:00 -07001720 kv.second->RecreateAudioReceiveStream(recv_transport_cc_enabled_,
1721 recv_nack_enabled_);
solenberg72e29d22016-03-08 06:35:16 -08001722 }
1723 }
1724
Taylor Brandstetter0cd086b2016-04-20 16:23:10 -07001725 send_codecs_ = codecs;
solenberg72e29d22016-03-08 06:35:16 -08001726 return true;
1727}
1728
aleloi84ef6152016-08-04 05:28:21 -07001729void WebRtcVoiceMediaChannel::SetPlayout(bool playout) {
kwiberg37b8b112016-11-03 02:46:53 -07001730 desired_playout_ = playout;
1731 return ChangePlayout(desired_playout_);
1732}
1733
1734void WebRtcVoiceMediaChannel::ChangePlayout(bool playout) {
1735 TRACE_EVENT0("webrtc", "WebRtcVoiceMediaChannel::ChangePlayout");
solenberg566ef242015-11-06 15:34:49 -08001736 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001737 if (playout_ == playout) {
aleloi84ef6152016-08-04 05:28:21 -07001738 return;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001739 }
1740
aleloi84ef6152016-08-04 05:28:21 -07001741 for (const auto& kv : recv_streams_) {
1742 kv.second->SetPlayout(playout);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001743 }
solenberg1ac56142015-10-13 03:58:19 -07001744 playout_ = playout;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001745}
1746
Taylor Brandstetter1a018dc2016-03-08 12:37:39 -08001747void WebRtcVoiceMediaChannel::SetSend(bool send) {
Peter Boströmca8b4042016-03-08 14:24:13 -08001748 TRACE_EVENT0("webrtc", "WebRtcVoiceMediaChannel::SetSend");
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001749 if (send_ == send) {
Taylor Brandstetter1a018dc2016-03-08 12:37:39 -08001750 return;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001751 }
1752
solenbergd53a3f92016-04-14 13:56:37 -07001753 // Apply channel specific options, and initialize the ADM for recording (this
1754 // may take time on some platforms, e.g. Android).
Taylor Brandstetter1a018dc2016-03-08 12:37:39 -08001755 if (send) {
solenberg63b34542015-09-29 06:06:31 -07001756 engine()->ApplyOptions(options_);
solenbergd53a3f92016-04-14 13:56:37 -07001757
1758 // InitRecording() may return an error if the ADM is already recording.
1759 if (!engine()->adm()->RecordingIsInitialized() &&
1760 !engine()->adm()->Recording()) {
1761 if (engine()->adm()->InitRecording() != 0) {
Mirko Bonadei675513b2017-11-09 11:09:25 +01001762 RTC_LOG(LS_WARNING) << "Failed to initialize recording";
solenbergd53a3f92016-04-14 13:56:37 -07001763 }
1764 }
solenberg63b34542015-09-29 06:06:31 -07001765 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001766
wu@webrtc.org9dba5252013-08-05 20:36:57 +00001767 // Change the settings on each send channel.
Taylor Brandstetter1a018dc2016-03-08 12:37:39 -08001768 for (auto& kv : send_streams_) {
1769 kv.second->SetSend(send);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001770 }
wu@webrtc.org9dba5252013-08-05 20:36:57 +00001771
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001772 send_ = send;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001773}
1774
Peter Boström0c4e06b2015-10-07 12:23:21 +02001775bool WebRtcVoiceMediaChannel::SetAudioSend(uint32_t ssrc,
1776 bool enable,
solenberg1dd98f32015-09-10 01:57:14 -07001777 const AudioOptions* options,
Taylor Brandstetter1a018dc2016-03-08 12:37:39 -08001778 AudioSource* source) {
solenberg566ef242015-11-06 15:34:49 -08001779 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
solenberg1dd98f32015-09-10 01:57:14 -07001780 // TODO(solenberg): The state change should be fully rolled back if any one of
1781 // these calls fail.
Taylor Brandstetter1a018dc2016-03-08 12:37:39 -08001782 if (!SetLocalSource(ssrc, source)) {
solenberg1dd98f32015-09-10 01:57:14 -07001783 return false;
1784 }
solenbergdfc8f4f2015-10-01 02:31:10 -07001785 if (!MuteStream(ssrc, !enable)) {
solenberg1dd98f32015-09-10 01:57:14 -07001786 return false;
1787 }
solenbergdfc8f4f2015-10-01 02:31:10 -07001788 if (enable && options) {
solenberg1dd98f32015-09-10 01:57:14 -07001789 return SetOptions(*options);
1790 }
1791 return true;
1792}
1793
solenberg0a617e22015-10-20 15:49:38 -07001794int WebRtcVoiceMediaChannel::CreateVoEChannel() {
1795 int id = engine()->CreateVoEChannel();
1796 if (id == -1) {
Mirko Bonadei675513b2017-11-09 11:09:25 +01001797 RTC_LOG(LS_WARNING) << "CreateVoEChannel() failed.";
solenberg0a617e22015-10-20 15:49:38 -07001798 return -1;
wu@webrtc.org9dba5252013-08-05 20:36:57 +00001799 }
mflodman3d7db262016-04-29 00:57:13 -07001800
solenberg0a617e22015-10-20 15:49:38 -07001801 return id;
wu@webrtc.org9dba5252013-08-05 20:36:57 +00001802}
1803
solenberg7add0582015-11-20 09:59:34 -08001804bool WebRtcVoiceMediaChannel::DeleteVoEChannel(int channel) {
wu@webrtc.org9dba5252013-08-05 20:36:57 +00001805 if (engine()->voe()->base()->DeleteChannel(channel) == -1) {
Mirko Bonadei675513b2017-11-09 11:09:25 +01001806 RTC_LOG(LS_WARNING) << "DeleteChannel(" << channel << ") failed.";
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001807 return false;
1808 }
wu@webrtc.org9dba5252013-08-05 20:36:57 +00001809 return true;
1810}
henrike@webrtc.org1e09a712013-07-26 19:17:59 +00001811
wu@webrtc.org9dba5252013-08-05 20:36:57 +00001812bool WebRtcVoiceMediaChannel::AddSendStream(const StreamParams& sp) {
Peter Boströmca8b4042016-03-08 14:24:13 -08001813 TRACE_EVENT0("webrtc", "WebRtcVoiceMediaChannel::AddSendStream");
solenberg566ef242015-11-06 15:34:49 -08001814 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
Mirko Bonadei675513b2017-11-09 11:09:25 +01001815 RTC_LOG(LS_INFO) << "AddSendStream: " << sp.ToString();
solenberg0a617e22015-10-20 15:49:38 -07001816
1817 uint32_t ssrc = sp.first_ssrc();
1818 RTC_DCHECK(0 != ssrc);
1819
1820 if (GetSendChannelId(ssrc) != -1) {
Mirko Bonadei675513b2017-11-09 11:09:25 +01001821 RTC_LOG(LS_ERROR) << "Stream already exists with ssrc " << ssrc;
wu@webrtc.org9dba5252013-08-05 20:36:57 +00001822 return false;
1823 }
1824
solenberg0a617e22015-10-20 15:49:38 -07001825 // Create a new channel for sending audio data.
1826 int channel = CreateVoEChannel();
1827 if (channel == -1) {
1828 return false;
wu@webrtc.org9dba5252013-08-05 20:36:57 +00001829 }
wu@webrtc.org9dba5252013-08-05 20:36:57 +00001830
solenbergc96df772015-10-21 13:01:53 -07001831 // Save the channel to send_streams_, so that RemoveSendStream() can still
wu@webrtc.org9dba5252013-08-05 20:36:57 +00001832 // delete the channel in case failure happens below.
mallinath@webrtc.org67ee6b92014-02-03 16:57:16 +00001833 webrtc::AudioTransport* audio_transport =
1834 engine()->voe()->base()->audio_transport();
mflodman3d7db262016-04-29 00:57:13 -07001835
minyue6b825df2016-10-31 04:08:32 -07001836 rtc::Optional<std::string> audio_network_adaptor_config =
1837 GetAudioNetworkAdaptorConfig(options_);
skvlade0d46372016-04-07 22:59:22 -07001838 WebRtcAudioSendStream* stream = new WebRtcAudioSendStream(
Alex Narestb3944f02017-10-13 14:56:18 +02001839 channel, audio_transport, ssrc, sp.cname, sp.id, send_codec_spec_,
minyue6b825df2016-10-31 04:08:32 -07001840 send_rtp_extensions_, max_send_bitrate_bps_, audio_network_adaptor_config,
ossu20a4b3f2017-04-27 02:08:52 -07001841 call_, this, engine()->encoder_factory_);
skvlade0d46372016-04-07 22:59:22 -07001842 send_streams_.insert(std::make_pair(ssrc, stream));
wu@webrtc.org9dba5252013-08-05 20:36:57 +00001843
solenberg4a0f7b52016-06-16 13:07:33 -07001844 // At this point the stream's local SSRC has been updated. If it is the first
1845 // send stream, make sure that all the receive streams are updated with the
1846 // same SSRC in order to send receiver reports.
solenbergc96df772015-10-21 13:01:53 -07001847 if (send_streams_.size() == 1) {
solenberg0a617e22015-10-20 15:49:38 -07001848 receiver_reports_ssrc_ = ssrc;
solenberg4a0f7b52016-06-16 13:07:33 -07001849 for (const auto& kv : recv_streams_) {
1850 // TODO(solenberg): Allow applications to set the RTCP SSRC of receive
1851 // streams instead, so we can avoid recreating the streams here.
1852 kv.second->RecreateAudioReceiveStream(ssrc);
wu@webrtc.org9dba5252013-08-05 20:36:57 +00001853 }
1854 }
1855
Taylor Brandstetter1a018dc2016-03-08 12:37:39 -08001856 send_streams_[ssrc]->SetSend(send_);
1857 return true;
wu@webrtc.org9dba5252013-08-05 20:36:57 +00001858}
1859
Peter Boström0c4e06b2015-10-07 12:23:21 +02001860bool WebRtcVoiceMediaChannel::RemoveSendStream(uint32_t ssrc) {
Peter Boströmca8b4042016-03-08 14:24:13 -08001861 TRACE_EVENT0("webrtc", "WebRtcVoiceMediaChannel::RemoveSendStream");
solenberg566ef242015-11-06 15:34:49 -08001862 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
Mirko Bonadei675513b2017-11-09 11:09:25 +01001863 RTC_LOG(LS_INFO) << "RemoveSendStream: " << ssrc;
solenberg3a941542015-11-16 07:34:50 -08001864
solenbergc96df772015-10-21 13:01:53 -07001865 auto it = send_streams_.find(ssrc);
1866 if (it == send_streams_.end()) {
Mirko Bonadei675513b2017-11-09 11:09:25 +01001867 RTC_LOG(LS_WARNING) << "Try to remove stream with ssrc " << ssrc
1868 << " which doesn't exist.";
wu@webrtc.org9dba5252013-08-05 20:36:57 +00001869 return false;
1870 }
1871
Taylor Brandstetter1a018dc2016-03-08 12:37:39 -08001872 it->second->SetSend(false);
wu@webrtc.org9dba5252013-08-05 20:36:57 +00001873
solenberg7602aab2016-11-14 11:30:07 -08001874 // TODO(solenberg): If we're removing the receiver_reports_ssrc_ stream, find
1875 // the first active send stream and use that instead, reassociating receive
1876 // streams.
1877
solenberg7add0582015-11-20 09:59:34 -08001878 // Clean up and delete the send stream+channel.
Taylor Brandstetter1a018dc2016-03-08 12:37:39 -08001879 int channel = it->second->channel();
Mirko Bonadei675513b2017-11-09 11:09:25 +01001880 RTC_LOG(LS_INFO) << "Removing audio send stream " << ssrc
1881 << " with VoiceEngine channel #" << channel << ".";
solenberg7add0582015-11-20 09:59:34 -08001882 delete it->second;
1883 send_streams_.erase(it);
1884 if (!DeleteVoEChannel(channel)) {
solenberg0a617e22015-10-20 15:49:38 -07001885 return false;
wu@webrtc.org9dba5252013-08-05 20:36:57 +00001886 }
solenbergc96df772015-10-21 13:01:53 -07001887 if (send_streams_.empty()) {
Taylor Brandstetter1a018dc2016-03-08 12:37:39 -08001888 SetSend(false);
solenberg0a617e22015-10-20 15:49:38 -07001889 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001890 return true;
1891}
1892
1893bool WebRtcVoiceMediaChannel::AddRecvStream(const StreamParams& sp) {
Peter Boströmca8b4042016-03-08 14:24:13 -08001894 TRACE_EVENT0("webrtc", "WebRtcVoiceMediaChannel::AddRecvStream");
solenberg566ef242015-11-06 15:34:49 -08001895 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
Mirko Bonadei675513b2017-11-09 11:09:25 +01001896 RTC_LOG(LS_INFO) << "AddRecvStream: " << sp.ToString();
solenbergd97ec302015-10-07 01:40:33 -07001897
solenberg0b675462015-10-09 01:37:09 -07001898 if (!ValidateStreamParams(sp)) {
wu@webrtc.org78187522013-10-07 23:32:02 +00001899 return false;
1900 }
1901
solenberg7add0582015-11-20 09:59:34 -08001902 const uint32_t ssrc = sp.first_ssrc();
solenberg0b675462015-10-09 01:37:09 -07001903 if (ssrc == 0) {
Mirko Bonadei675513b2017-11-09 11:09:25 +01001904 RTC_LOG(LS_WARNING) << "AddRecvStream with ssrc==0 is not supported.";
solenberg0b675462015-10-09 01:37:09 -07001905 return false;
1906 }
1907
solenberg2100c0b2017-03-01 11:29:29 -08001908 // If this stream was previously received unsignaled, we promote it, possibly
1909 // recreating the AudioReceiveStream, if sync_label has changed.
1910 if (MaybeDeregisterUnsignaledRecvStream(ssrc)) {
solenberg4904fb62017-02-17 12:01:14 -08001911 recv_streams_[ssrc]->MaybeRecreateAudioReceiveStream(sp.sync_label);
solenberg4904fb62017-02-17 12:01:14 -08001912 return true;
solenberg1ac56142015-10-13 03:58:19 -07001913 }
solenberg0b675462015-10-09 01:37:09 -07001914
solenberg7add0582015-11-20 09:59:34 -08001915 if (GetReceiveChannelId(ssrc) != -1) {
Mirko Bonadei675513b2017-11-09 11:09:25 +01001916 RTC_LOG(LS_ERROR) << "Stream already exists with ssrc " << ssrc;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001917 return false;
1918 }
Fredrik Solenberg4b60c732015-05-07 14:07:48 +02001919
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001920 // Create a new channel for receiving audio data.
solenberg7add0582015-11-20 09:59:34 -08001921 const int channel = CreateVoEChannel();
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001922 if (channel == -1) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001923 return false;
1924 }
Minyue2013aec2015-05-13 14:14:42 +02001925
stefanba4c0e42016-02-04 04:12:24 -08001926 recv_streams_.insert(std::make_pair(
kwiberg1c07c702017-03-27 07:15:49 -07001927 ssrc,
1928 new WebRtcAudioReceiveStream(
1929 channel, ssrc, receiver_reports_ssrc_, recv_transport_cc_enabled_,
1930 recv_nack_enabled_, sp.sync_label, recv_rtp_extensions_, call_, this,
1931 engine()->decoder_factory_, decoder_map_)));
aleloi84ef6152016-08-04 05:28:21 -07001932 recv_streams_[ssrc]->SetPlayout(playout_);
solenberg7add0582015-11-20 09:59:34 -08001933
solenberg1ac56142015-10-13 03:58:19 -07001934 return true;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001935}
1936
Peter Boström0c4e06b2015-10-07 12:23:21 +02001937bool WebRtcVoiceMediaChannel::RemoveRecvStream(uint32_t ssrc) {
Peter Boströmca8b4042016-03-08 14:24:13 -08001938 TRACE_EVENT0("webrtc", "WebRtcVoiceMediaChannel::RemoveRecvStream");
solenberg566ef242015-11-06 15:34:49 -08001939 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
Mirko Bonadei675513b2017-11-09 11:09:25 +01001940 RTC_LOG(LS_INFO) << "RemoveRecvStream: " << ssrc;
solenbergd97ec302015-10-07 01:40:33 -07001941
solenberg7add0582015-11-20 09:59:34 -08001942 const auto it = recv_streams_.find(ssrc);
1943 if (it == recv_streams_.end()) {
Mirko Bonadei675513b2017-11-09 11:09:25 +01001944 RTC_LOG(LS_WARNING) << "Try to remove stream with ssrc " << ssrc
1945 << " which doesn't exist.";
henrike@webrtc.org1e09a712013-07-26 19:17:59 +00001946 return false;
wu@webrtc.org9dba5252013-08-05 20:36:57 +00001947 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001948
solenberg2100c0b2017-03-01 11:29:29 -08001949 MaybeDeregisterUnsignaledRecvStream(ssrc);
henrike@webrtc.org1e09a712013-07-26 19:17:59 +00001950
solenberg7add0582015-11-20 09:59:34 -08001951 const int channel = it->second->channel();
1952
1953 // Clean up and delete the receive stream+channel.
Mirko Bonadei675513b2017-11-09 11:09:25 +01001954 RTC_LOG(LS_INFO) << "Removing audio receive stream " << ssrc
1955 << " with VoiceEngine channel #" << channel << ".";
Tommif888bb52015-12-12 01:37:01 +01001956 it->second->SetRawAudioSink(nullptr);
solenberg7add0582015-11-20 09:59:34 -08001957 delete it->second;
1958 recv_streams_.erase(it);
1959 return DeleteVoEChannel(channel);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001960}
1961
Taylor Brandstetter1a018dc2016-03-08 12:37:39 -08001962bool WebRtcVoiceMediaChannel::SetLocalSource(uint32_t ssrc,
1963 AudioSource* source) {
solenbergc96df772015-10-21 13:01:53 -07001964 auto it = send_streams_.find(ssrc);
1965 if (it == send_streams_.end()) {
Taylor Brandstetter1a018dc2016-03-08 12:37:39 -08001966 if (source) {
1967 // Return an error if trying to set a valid source with an invalid ssrc.
Mirko Bonadei675513b2017-11-09 11:09:25 +01001968 RTC_LOG(LS_ERROR) << "SetLocalSource failed with ssrc " << ssrc;
wu@webrtc.org9dba5252013-08-05 20:36:57 +00001969 return false;
1970 }
1971
1972 // The channel likely has gone away, do nothing.
henrike@webrtc.org1e09a712013-07-26 19:17:59 +00001973 return true;
henrike@webrtc.org1e09a712013-07-26 19:17:59 +00001974 }
1975
Taylor Brandstetter1a018dc2016-03-08 12:37:39 -08001976 if (source) {
1977 it->second->SetSource(source);
solenberg1ac56142015-10-13 03:58:19 -07001978 } else {
Taylor Brandstetter1a018dc2016-03-08 12:37:39 -08001979 it->second->ClearSource();
solenberg1ac56142015-10-13 03:58:19 -07001980 }
henrike@webrtc.org1e09a712013-07-26 19:17:59 +00001981
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001982 return true;
1983}
1984
solenberg796b8f92017-03-01 17:02:23 -08001985// TODO(solenberg): Remove, once AudioMonitor is gone.
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001986bool WebRtcVoiceMediaChannel::GetActiveStreams(
1987 AudioInfo::StreamList* actives) {
solenberg566ef242015-11-06 15:34:49 -08001988 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001989 actives->clear();
solenberg7add0582015-11-20 09:59:34 -08001990 for (const auto& ch : recv_streams_) {
solenberg796b8f92017-03-01 17:02:23 -08001991 int level = ch.second->GetOutputLevel();
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001992 if (level > 0) {
Fredrik Solenbergaf9fb212015-08-26 10:45:53 +02001993 actives->push_back(std::make_pair(ch.first, level));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001994 }
1995 }
1996 return true;
1997}
1998
solenberg796b8f92017-03-01 17:02:23 -08001999// TODO(solenberg): Remove, once AudioMonitor is gone.
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002000int WebRtcVoiceMediaChannel::GetOutputLevel() {
solenberg566ef242015-11-06 15:34:49 -08002001 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
solenberg1ac56142015-10-13 03:58:19 -07002002 int highest = 0;
solenberg7add0582015-11-20 09:59:34 -08002003 for (const auto& ch : recv_streams_) {
solenberg796b8f92017-03-01 17:02:23 -08002004 highest = std::max(ch.second->GetOutputLevel(), highest);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002005 }
2006 return highest;
2007}
2008
solenberg4bac9c52015-10-09 02:32:53 -07002009bool WebRtcVoiceMediaChannel::SetOutputVolume(uint32_t ssrc, double volume) {
solenberg566ef242015-11-06 15:34:49 -08002010 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
solenberg2100c0b2017-03-01 11:29:29 -08002011 std::vector<uint32_t> ssrcs(1, ssrc);
deadbeef3bc15102017-04-20 19:25:07 -07002012 // SSRC of 0 represents the default receive stream.
solenberg1ac56142015-10-13 03:58:19 -07002013 if (ssrc == 0) {
2014 default_recv_volume_ = volume;
solenberg2100c0b2017-03-01 11:29:29 -08002015 ssrcs = unsignaled_recv_ssrcs_;
2016 }
2017 for (uint32_t ssrc : ssrcs) {
2018 const auto it = recv_streams_.find(ssrc);
2019 if (it == recv_streams_.end()) {
Mirko Bonadei675513b2017-11-09 11:09:25 +01002020 RTC_LOG(LS_WARNING) << "SetOutputVolume: no recv stream " << ssrc;
solenberg2100c0b2017-03-01 11:29:29 -08002021 return false;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002022 }
solenberg2100c0b2017-03-01 11:29:29 -08002023 it->second->SetOutputVolume(volume);
Mirko Bonadei675513b2017-11-09 11:09:25 +01002024 RTC_LOG(LS_INFO) << "SetOutputVolume() to " << volume
2025 << " for recv stream with ssrc " << ssrc;
solenberg1ac56142015-10-13 03:58:19 -07002026 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002027 return true;
2028}
2029
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002030bool WebRtcVoiceMediaChannel::CanInsertDtmf() {
Fredrik Solenbergb5727682015-12-04 15:22:19 +01002031 return dtmf_payload_type_ ? true : false;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002032}
2033
solenberg1d63dd02015-12-02 12:35:09 -08002034bool WebRtcVoiceMediaChannel::InsertDtmf(uint32_t ssrc, int event,
2035 int duration) {
solenberg566ef242015-11-06 15:34:49 -08002036 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
Mirko Bonadei675513b2017-11-09 11:09:25 +01002037 RTC_LOG(LS_INFO) << "WebRtcVoiceMediaChannel::InsertDtmf";
Fredrik Solenbergb5727682015-12-04 15:22:19 +01002038 if (!dtmf_payload_type_) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002039 return false;
2040 }
2041
Fredrik Solenbergb5727682015-12-04 15:22:19 +01002042 // Figure out which WebRtcAudioSendStream to send the event on.
2043 auto it = ssrc != 0 ? send_streams_.find(ssrc) : send_streams_.begin();
2044 if (it == send_streams_.end()) {
Mirko Bonadei675513b2017-11-09 11:09:25 +01002045 RTC_LOG(LS_WARNING) << "The specified ssrc " << ssrc << " is not in use.";
solenberg1d63dd02015-12-02 12:35:09 -08002046 return false;
2047 }
Fredrik Solenbergb5727682015-12-04 15:22:19 +01002048 if (event < kMinTelephoneEventCode ||
2049 event > kMaxTelephoneEventCode) {
Mirko Bonadei675513b2017-11-09 11:09:25 +01002050 RTC_LOG(LS_WARNING) << "DTMF event code " << event << " out of range.";
solenberg1d63dd02015-12-02 12:35:09 -08002051 return false;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002052 }
solenbergffbbcac2016-11-17 05:25:37 -08002053 RTC_DCHECK_NE(-1, dtmf_payload_freq_);
2054 return it->second->SendTelephoneEvent(*dtmf_payload_type_, dtmf_payload_freq_,
2055 event, duration);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002056}
2057
wu@webrtc.orga9890802013-12-13 00:21:03 +00002058void WebRtcVoiceMediaChannel::OnPacketReceived(
jbaucheec21bd2016-03-20 06:15:43 -07002059 rtc::CopyOnWriteBuffer* packet, const rtc::PacketTime& packet_time) {
solenberg566ef242015-11-06 15:34:49 -08002060 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
Fredrik Solenberg4b60c732015-05-07 14:07:48 +02002061
mflodman3d7db262016-04-29 00:57:13 -07002062 const webrtc::PacketTime webrtc_packet_time(packet_time.timestamp,
2063 packet_time.not_before);
2064 webrtc::PacketReceiver::DeliveryStatus delivery_result =
2065 call_->Receiver()->DeliverPacket(webrtc::MediaType::AUDIO,
2066 packet->cdata(), packet->size(),
2067 webrtc_packet_time);
mflodman3d7db262016-04-29 00:57:13 -07002068 if (delivery_result != webrtc::PacketReceiver::DELIVERY_UNKNOWN_SSRC) {
2069 return;
2070 }
2071
solenberg2100c0b2017-03-01 11:29:29 -08002072 // Create an unsignaled receive stream for this previously not received ssrc.
2073 // If there already is N unsignaled receive streams, delete the oldest.
mflodman3d7db262016-04-29 00:57:13 -07002074 // See: https://bugs.chromium.org/p/webrtc/issues/detail?id=5208
solenberg1ac56142015-10-13 03:58:19 -07002075 uint32_t ssrc = 0;
jbaucheec21bd2016-03-20 06:15:43 -07002076 if (!GetRtpSsrc(packet->cdata(), packet->size(), &ssrc)) {
solenberg1ac56142015-10-13 03:58:19 -07002077 return;
2078 }
solenberg2100c0b2017-03-01 11:29:29 -08002079 RTC_DCHECK(std::find(unsignaled_recv_ssrcs_.begin(),
2080 unsignaled_recv_ssrcs_.end(), ssrc) == unsignaled_recv_ssrcs_.end());
solenberg1ac56142015-10-13 03:58:19 -07002081
solenberg2100c0b2017-03-01 11:29:29 -08002082 // Add new stream.
mflodman3d7db262016-04-29 00:57:13 -07002083 StreamParams sp;
2084 sp.ssrcs.push_back(ssrc);
Mirko Bonadei675513b2017-11-09 11:09:25 +01002085 RTC_LOG(LS_INFO) << "Creating unsignaled receive stream for SSRC=" << ssrc;
mflodman3d7db262016-04-29 00:57:13 -07002086 if (!AddRecvStream(sp)) {
Mirko Bonadei675513b2017-11-09 11:09:25 +01002087 RTC_LOG(LS_WARNING) << "Could not create unsignaled receive stream.";
mflodman3d7db262016-04-29 00:57:13 -07002088 return;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002089 }
solenberg2100c0b2017-03-01 11:29:29 -08002090 unsignaled_recv_ssrcs_.push_back(ssrc);
2091 RTC_HISTOGRAM_COUNTS_LINEAR(
2092 "WebRTC.Audio.NumOfUnsignaledStreams", unsignaled_recv_ssrcs_.size(), 1,
2093 100, 101);
solenbergf748ca42017-02-06 13:03:19 -08002094
solenberg2100c0b2017-03-01 11:29:29 -08002095 // Remove oldest unsignaled stream, if we have too many.
2096 if (unsignaled_recv_ssrcs_.size() > kMaxUnsignaledRecvStreams) {
2097 uint32_t remove_ssrc = unsignaled_recv_ssrcs_.front();
Mirko Bonadei675513b2017-11-09 11:09:25 +01002098 RTC_LOG(LS_INFO) << "Removing unsignaled receive stream with SSRC="
2099 << remove_ssrc;
solenberg2100c0b2017-03-01 11:29:29 -08002100 RemoveRecvStream(remove_ssrc);
2101 }
2102 RTC_DCHECK_GE(kMaxUnsignaledRecvStreams, unsignaled_recv_ssrcs_.size());
2103
2104 SetOutputVolume(ssrc, default_recv_volume_);
2105
2106 // The default sink can only be attached to one stream at a time, so we hook
2107 // it up to the *latest* unsignaled stream we've seen, in order to support the
2108 // case where the SSRC of one unsignaled stream changes.
mflodman3d7db262016-04-29 00:57:13 -07002109 if (default_sink_) {
solenberg2100c0b2017-03-01 11:29:29 -08002110 for (uint32_t drop_ssrc : unsignaled_recv_ssrcs_) {
2111 auto it = recv_streams_.find(drop_ssrc);
2112 it->second->SetRawAudioSink(nullptr);
2113 }
mflodman3d7db262016-04-29 00:57:13 -07002114 std::unique_ptr<webrtc::AudioSinkInterface> proxy_sink(
2115 new ProxySink(default_sink_.get()));
solenberg2100c0b2017-03-01 11:29:29 -08002116 SetRawAudioSink(ssrc, std::move(proxy_sink));
mflodman3d7db262016-04-29 00:57:13 -07002117 }
solenberg2100c0b2017-03-01 11:29:29 -08002118
mflodman3d7db262016-04-29 00:57:13 -07002119 delivery_result = call_->Receiver()->DeliverPacket(webrtc::MediaType::AUDIO,
2120 packet->cdata(),
2121 packet->size(),
2122 webrtc_packet_time);
2123 RTC_DCHECK_NE(webrtc::PacketReceiver::DELIVERY_UNKNOWN_SSRC, delivery_result);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002124}
2125
wu@webrtc.orga9890802013-12-13 00:21:03 +00002126void WebRtcVoiceMediaChannel::OnRtcpReceived(
jbaucheec21bd2016-03-20 06:15:43 -07002127 rtc::CopyOnWriteBuffer* packet, const rtc::PacketTime& packet_time) {
solenberg566ef242015-11-06 15:34:49 -08002128 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
Fredrik Solenberg4b60c732015-05-07 14:07:48 +02002129
Fredrik Solenberg709ed672015-09-15 12:26:33 +02002130 // Forward packet to Call as well.
2131 const webrtc::PacketTime webrtc_packet_time(packet_time.timestamp,
2132 packet_time.not_before);
2133 call_->Receiver()->DeliverPacket(webrtc::MediaType::AUDIO,
jbaucheec21bd2016-03-20 06:15:43 -07002134 packet->cdata(), packet->size(), webrtc_packet_time);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002135}
2136
Honghai Zhangcc411c02016-03-29 17:27:21 -07002137void WebRtcVoiceMediaChannel::OnNetworkRouteChanged(
2138 const std::string& transport_name,
Honghai Zhang0e533ef2016-04-19 15:41:36 -07002139 const rtc::NetworkRoute& network_route) {
Zhi Huang5f5918f2017-11-12 17:26:23 -08002140 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
2141 // TODO(zhihaung): Merge these two callbacks.
Honghai Zhang0e533ef2016-04-19 15:41:36 -07002142 call_->OnNetworkRouteChanged(transport_name, network_route);
Zhi Huang5f5918f2017-11-12 17:26:23 -08002143 call_->OnTransportOverheadChanged(webrtc::MediaType::AUDIO,
2144 network_route.packet_overhead);
Honghai Zhangcc411c02016-03-29 17:27:21 -07002145}
2146
Peter Boström0c4e06b2015-10-07 12:23:21 +02002147bool WebRtcVoiceMediaChannel::MuteStream(uint32_t ssrc, bool muted) {
solenberg566ef242015-11-06 15:34:49 -08002148 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
solenberg94218532016-06-16 10:53:22 -07002149 const auto it = send_streams_.find(ssrc);
2150 if (it == send_streams_.end()) {
Mirko Bonadei675513b2017-11-09 11:09:25 +01002151 RTC_LOG(LS_WARNING) << "The specified ssrc " << ssrc << " is not in use.";
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002152 return false;
2153 }
solenberg94218532016-06-16 10:53:22 -07002154 it->second->SetMuted(muted);
2155
2156 // TODO(solenberg):
buildbot@webrtc.org6b21b712014-07-31 15:08:53 +00002157 // We set the AGC to mute state only when all the channels are muted.
2158 // This implementation is not ideal, instead we should signal the AGC when
2159 // the mic channel is muted/unmuted. We can't do it today because there
2160 // is no good way to know which stream is mapping to the mic channel.
2161 bool all_muted = muted;
solenberg94218532016-06-16 10:53:22 -07002162 for (const auto& kv : send_streams_) {
2163 all_muted = all_muted && kv.second->muted();
buildbot@webrtc.org6b21b712014-07-31 15:08:53 +00002164 }
solenberg059fb442016-10-26 05:12:24 -07002165 engine()->apm()->set_output_will_be_muted(all_muted);
buildbot@webrtc.org6b21b712014-07-31 15:08:53 +00002166
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002167 return true;
2168}
2169
deadbeef80346142016-04-27 14:17:10 -07002170bool WebRtcVoiceMediaChannel::SetMaxSendBitrate(int bps) {
Mirko Bonadei675513b2017-11-09 11:09:25 +01002171 RTC_LOG(LS_INFO) << "WebRtcVoiceMediaChannel::SetMaxSendBitrate.";
deadbeef80346142016-04-27 14:17:10 -07002172 max_send_bitrate_bps_ = bps;
minyue7a973442016-10-20 03:27:12 -07002173 bool success = true;
skvlade0d46372016-04-07 22:59:22 -07002174 for (const auto& kv : send_streams_) {
minyue7a973442016-10-20 03:27:12 -07002175 if (!kv.second->SetMaxSendBitrate(max_send_bitrate_bps_)) {
2176 success = false;
skvlade0d46372016-04-07 22:59:22 -07002177 }
2178 }
minyue7a973442016-10-20 03:27:12 -07002179 return success;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002180}
2181
skvlad7a43d252016-03-22 15:32:27 -07002182void WebRtcVoiceMediaChannel::OnReadyToSend(bool ready) {
2183 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
Mirko Bonadei675513b2017-11-09 11:09:25 +01002184 RTC_LOG(LS_VERBOSE) << "OnReadyToSend: " << (ready ? "Ready." : "Not ready.");
skvlad7a43d252016-03-22 15:32:27 -07002185 call_->SignalChannelNetworkState(
2186 webrtc::MediaType::AUDIO,
2187 ready ? webrtc::kNetworkUp : webrtc::kNetworkDown);
2188}
2189
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002190bool WebRtcVoiceMediaChannel::GetStats(VoiceMediaInfo* info) {
Peter Boströmca8b4042016-03-08 14:24:13 -08002191 TRACE_EVENT0("webrtc", "WebRtcVoiceMediaChannel::GetStats");
solenberg566ef242015-11-06 15:34:49 -08002192 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
solenberg85a04962015-10-27 03:35:21 -07002193 RTC_DCHECK(info);
solenbergd97ec302015-10-07 01:40:33 -07002194
solenberg85a04962015-10-27 03:35:21 -07002195 // Get SSRC and stats for each sender.
hbos1acfbd22016-11-17 23:43:29 -08002196 RTC_DCHECK_EQ(info->senders.size(), 0U);
solenberg85a04962015-10-27 03:35:21 -07002197 for (const auto& stream : send_streams_) {
2198 webrtc::AudioSendStream::Stats stats = stream.second->GetStats();
wu@webrtc.org9dba5252013-08-05 20:36:57 +00002199 VoiceSenderInfo sinfo;
solenberg85a04962015-10-27 03:35:21 -07002200 sinfo.add_ssrc(stats.local_ssrc);
2201 sinfo.bytes_sent = stats.bytes_sent;
2202 sinfo.packets_sent = stats.packets_sent;
2203 sinfo.packets_lost = stats.packets_lost;
2204 sinfo.fraction_lost = stats.fraction_lost;
2205 sinfo.codec_name = stats.codec_name;
hbos1acfbd22016-11-17 23:43:29 -08002206 sinfo.codec_payload_type = stats.codec_payload_type;
solenberg85a04962015-10-27 03:35:21 -07002207 sinfo.ext_seqnum = stats.ext_seqnum;
2208 sinfo.jitter_ms = stats.jitter_ms;
2209 sinfo.rtt_ms = stats.rtt_ms;
2210 sinfo.audio_level = stats.audio_level;
zsteine76bd3a2017-07-14 12:17:49 -07002211 sinfo.total_input_energy = stats.total_input_energy;
2212 sinfo.total_input_duration = stats.total_input_duration;
solenberg85a04962015-10-27 03:35:21 -07002213 sinfo.aec_quality_min = stats.aec_quality_min;
2214 sinfo.echo_delay_median_ms = stats.echo_delay_median_ms;
2215 sinfo.echo_delay_std_ms = stats.echo_delay_std_ms;
2216 sinfo.echo_return_loss = stats.echo_return_loss;
2217 sinfo.echo_return_loss_enhancement = stats.echo_return_loss_enhancement;
ivoc8c63a822016-10-21 04:10:03 -07002218 sinfo.residual_echo_likelihood = stats.residual_echo_likelihood;
ivoc4e477a12017-01-15 08:29:46 -08002219 sinfo.residual_echo_likelihood_recent_max =
2220 stats.residual_echo_likelihood_recent_max;
Taylor Brandstetter1a018dc2016-03-08 12:37:39 -08002221 sinfo.typing_noise_detected = (send_ ? stats.typing_noise_detected : false);
ivoce1198e02017-09-08 08:13:19 -07002222 sinfo.ana_statistics = stats.ana_statistics;
wu@webrtc.org9dba5252013-08-05 20:36:57 +00002223 info->senders.push_back(sinfo);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002224 }
2225
solenberg85a04962015-10-27 03:35:21 -07002226 // Get SSRC and stats for each receiver.
hbos1acfbd22016-11-17 23:43:29 -08002227 RTC_DCHECK_EQ(info->receivers.size(), 0U);
solenberg7add0582015-11-20 09:59:34 -08002228 for (const auto& stream : recv_streams_) {
deadbeef4e2deab2017-09-20 13:56:21 -07002229 uint32_t ssrc = stream.first;
2230 // When SSRCs are unsignaled, there's only one audio MediaStreamTrack, but
2231 // multiple RTP streams can be received over time (if the SSRC changes for
2232 // whatever reason). We only want the RTCMediaStreamTrackStats to represent
2233 // the stats for the most recent stream (the one whose audio is actually
2234 // routed to the MediaStreamTrack), so here we ignore any unsignaled SSRCs
2235 // except for the most recent one (last in the vector). This is somewhat of
2236 // a hack, and means you don't get *any* stats for these inactive streams,
2237 // but it's slightly better than the previous behavior, which was "highest
2238 // SSRC wins".
2239 // See: https://bugs.chromium.org/p/webrtc/issues/detail?id=8158
2240 if (!unsignaled_recv_ssrcs_.empty()) {
2241 auto end_it = --unsignaled_recv_ssrcs_.end();
2242 if (std::find(unsignaled_recv_ssrcs_.begin(), end_it, ssrc) != end_it) {
2243 continue;
2244 }
2245 }
Fredrik Solenberg4f4ec0a2015-10-22 10:49:27 +02002246 webrtc::AudioReceiveStream::Stats stats = stream.second->GetStats();
2247 VoiceReceiverInfo rinfo;
2248 rinfo.add_ssrc(stats.remote_ssrc);
2249 rinfo.bytes_rcvd = stats.bytes_rcvd;
2250 rinfo.packets_rcvd = stats.packets_rcvd;
2251 rinfo.packets_lost = stats.packets_lost;
2252 rinfo.fraction_lost = stats.fraction_lost;
2253 rinfo.codec_name = stats.codec_name;
hbos1acfbd22016-11-17 23:43:29 -08002254 rinfo.codec_payload_type = stats.codec_payload_type;
Fredrik Solenberg4f4ec0a2015-10-22 10:49:27 +02002255 rinfo.ext_seqnum = stats.ext_seqnum;
2256 rinfo.jitter_ms = stats.jitter_ms;
2257 rinfo.jitter_buffer_ms = stats.jitter_buffer_ms;
2258 rinfo.jitter_buffer_preferred_ms = stats.jitter_buffer_preferred_ms;
2259 rinfo.delay_estimate_ms = stats.delay_estimate_ms;
2260 rinfo.audio_level = stats.audio_level;
zsteine76bd3a2017-07-14 12:17:49 -07002261 rinfo.total_output_energy = stats.total_output_energy;
Steve Anton2dbc69f2017-08-24 17:15:13 -07002262 rinfo.total_samples_received = stats.total_samples_received;
zsteine76bd3a2017-07-14 12:17:49 -07002263 rinfo.total_output_duration = stats.total_output_duration;
Steve Anton2dbc69f2017-08-24 17:15:13 -07002264 rinfo.concealed_samples = stats.concealed_samples;
Gustaf Ullberg9a2e9062017-09-18 09:28:20 +02002265 rinfo.concealment_events = stats.concealment_events;
Gustaf Ullbergb0a02072017-10-02 12:00:34 +02002266 rinfo.jitter_buffer_delay_seconds = stats.jitter_buffer_delay_seconds;
Fredrik Solenberg4f4ec0a2015-10-22 10:49:27 +02002267 rinfo.expand_rate = stats.expand_rate;
2268 rinfo.speech_expand_rate = stats.speech_expand_rate;
2269 rinfo.secondary_decoded_rate = stats.secondary_decoded_rate;
minyue-webrtc0e320ec2017-08-28 13:51:27 +02002270 rinfo.secondary_discarded_rate = stats.secondary_discarded_rate;
Fredrik Solenberg4f4ec0a2015-10-22 10:49:27 +02002271 rinfo.accelerate_rate = stats.accelerate_rate;
2272 rinfo.preemptive_expand_rate = stats.preemptive_expand_rate;
2273 rinfo.decoding_calls_to_silence_generator =
2274 stats.decoding_calls_to_silence_generator;
2275 rinfo.decoding_calls_to_neteq = stats.decoding_calls_to_neteq;
2276 rinfo.decoding_normal = stats.decoding_normal;
2277 rinfo.decoding_plc = stats.decoding_plc;
2278 rinfo.decoding_cng = stats.decoding_cng;
2279 rinfo.decoding_plc_cng = stats.decoding_plc_cng;
henrik.lundin63489782016-09-20 01:47:12 -07002280 rinfo.decoding_muted_output = stats.decoding_muted_output;
Fredrik Solenberg4f4ec0a2015-10-22 10:49:27 +02002281 rinfo.capture_start_ntp_time_ms = stats.capture_start_ntp_time_ms;
2282 info->receivers.push_back(rinfo);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002283 }
2284
hbos1acfbd22016-11-17 23:43:29 -08002285 // Get codec info
2286 for (const AudioCodec& codec : send_codecs_) {
2287 webrtc::RtpCodecParameters codec_params = codec.ToCodecParameters();
2288 info->send_codecs.insert(
2289 std::make_pair(codec_params.payload_type, std::move(codec_params)));
2290 }
2291 for (const AudioCodec& codec : recv_codecs_) {
2292 webrtc::RtpCodecParameters codec_params = codec.ToCodecParameters();
2293 info->receive_codecs.insert(
2294 std::make_pair(codec_params.payload_type, std::move(codec_params)));
2295 }
2296
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002297 return true;
2298}
2299
Tommif888bb52015-12-12 01:37:01 +01002300void WebRtcVoiceMediaChannel::SetRawAudioSink(
2301 uint32_t ssrc,
kwiberg686a8ef2016-02-26 03:00:35 -08002302 std::unique_ptr<webrtc::AudioSinkInterface> sink) {
Tommif888bb52015-12-12 01:37:01 +01002303 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
Mirko Bonadei675513b2017-11-09 11:09:25 +01002304 RTC_LOG(LS_VERBOSE) << "WebRtcVoiceMediaChannel::SetRawAudioSink: ssrc:"
2305 << ssrc << " " << (sink ? "(ptr)" : "NULL");
deadbeef884f5852016-01-15 09:20:04 -08002306 if (ssrc == 0) {
solenberg2100c0b2017-03-01 11:29:29 -08002307 if (!unsignaled_recv_ssrcs_.empty()) {
kwiberg686a8ef2016-02-26 03:00:35 -08002308 std::unique_ptr<webrtc::AudioSinkInterface> proxy_sink(
deadbeef884f5852016-01-15 09:20:04 -08002309 sink ? new ProxySink(sink.get()) : nullptr);
solenberg2100c0b2017-03-01 11:29:29 -08002310 SetRawAudioSink(unsignaled_recv_ssrcs_.back(), std::move(proxy_sink));
deadbeef884f5852016-01-15 09:20:04 -08002311 }
2312 default_sink_ = std::move(sink);
2313 return;
2314 }
Tommif888bb52015-12-12 01:37:01 +01002315 const auto it = recv_streams_.find(ssrc);
2316 if (it == recv_streams_.end()) {
Mirko Bonadei675513b2017-11-09 11:09:25 +01002317 RTC_LOG(LS_WARNING) << "SetRawAudioSink: no recv stream " << ssrc;
Tommif888bb52015-12-12 01:37:01 +01002318 return;
2319 }
deadbeef2d110be2016-01-13 12:00:26 -08002320 it->second->SetRawAudioSink(std::move(sink));
Tommif888bb52015-12-12 01:37:01 +01002321}
2322
hbos8d609f62017-04-10 07:39:05 -07002323std::vector<webrtc::RtpSource> WebRtcVoiceMediaChannel::GetSources(
2324 uint32_t ssrc) const {
2325 auto it = recv_streams_.find(ssrc);
2326 RTC_DCHECK(it != recv_streams_.end())
2327 << "Attempting to get contributing sources for SSRC:" << ssrc
2328 << " which doesn't exist.";
2329 return it->second->GetSources();
2330}
2331
Peter Boström0c4e06b2015-10-07 12:23:21 +02002332int WebRtcVoiceMediaChannel::GetReceiveChannelId(uint32_t ssrc) const {
solenberg566ef242015-11-06 15:34:49 -08002333 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
solenberg7add0582015-11-20 09:59:34 -08002334 const auto it = recv_streams_.find(ssrc);
2335 if (it != recv_streams_.end()) {
mallinath@webrtc.org67ee6b92014-02-03 16:57:16 +00002336 return it->second->channel();
solenberg8fb30c32015-10-13 03:06:58 -07002337 }
solenberg1ac56142015-10-13 03:58:19 -07002338 return -1;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002339}
2340
Peter Boström0c4e06b2015-10-07 12:23:21 +02002341int WebRtcVoiceMediaChannel::GetSendChannelId(uint32_t ssrc) const {
solenberg566ef242015-11-06 15:34:49 -08002342 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
solenbergc96df772015-10-21 13:01:53 -07002343 const auto it = send_streams_.find(ssrc);
2344 if (it != send_streams_.end()) {
mallinath@webrtc.org67ee6b92014-02-03 16:57:16 +00002345 return it->second->channel();
solenberg8fb30c32015-10-13 03:06:58 -07002346 }
wu@webrtc.org9dba5252013-08-05 20:36:57 +00002347 return -1;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002348}
solenberg2100c0b2017-03-01 11:29:29 -08002349
2350bool WebRtcVoiceMediaChannel::
2351 MaybeDeregisterUnsignaledRecvStream(uint32_t ssrc) {
2352 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
2353 auto it = std::find(unsignaled_recv_ssrcs_.begin(),
2354 unsignaled_recv_ssrcs_.end(),
2355 ssrc);
2356 if (it != unsignaled_recv_ssrcs_.end()) {
2357 unsignaled_recv_ssrcs_.erase(it);
2358 return true;
2359 }
2360 return false;
2361}
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002362} // namespace cricket
2363
2364#endif // HAVE_WEBRTC_VOICE