blob: 9630bc4caa5eb124586927533d2d6adc5c35e28d [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"
Fredrik Solenbergd3195342017-11-21 20:33:05 +010031#include "modules/audio_device/audio_device_impl.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020032#include "modules/audio_mixer/audio_mixer_impl.h"
33#include "modules/audio_processing/aec_dump/aec_dump_factory.h"
34#include "modules/audio_processing/include/audio_processing.h"
35#include "rtc_base/arraysize.h"
36#include "rtc_base/base64.h"
37#include "rtc_base/byteorder.h"
38#include "rtc_base/constructormagic.h"
39#include "rtc_base/helpers.h"
40#include "rtc_base/logging.h"
41#include "rtc_base/race_checker.h"
42#include "rtc_base/stringencode.h"
43#include "rtc_base/stringutils.h"
44#include "rtc_base/trace_event.h"
45#include "system_wrappers/include/field_trial.h"
46#include "system_wrappers/include/metrics.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020047#include "voice_engine/transmit_mixer.h"
henrike@webrtc.org28e20752013-07-10 00:45:36 +000048
henrike@webrtc.org28e20752013-07-10 00:45:36 +000049namespace cricket {
solenbergd97ec302015-10-07 01:40:33 -070050namespace {
henrike@webrtc.org28e20752013-07-10 00:45:36 +000051
solenberg418b7d32017-06-13 00:38:27 -070052constexpr size_t kMaxUnsignaledRecvStreams = 4;
solenberg2100c0b2017-03-01 11:29:29 -080053
solenberg971cab02016-06-14 10:02:41 -070054constexpr int kNackRtpHistoryMs = 5000;
minyue@webrtc.org2dc6f312014-10-31 05:33:10 +000055
peah1bcfce52016-08-26 07:16:04 -070056// Check to verify that the define for the intelligibility enhancer is properly
57// set.
58#if !defined(WEBRTC_INTELLIGIBILITY_ENHANCER) || \
59 (WEBRTC_INTELLIGIBILITY_ENHANCER != 0 && \
60 WEBRTC_INTELLIGIBILITY_ENHANCER != 1)
61#error "Set WEBRTC_INTELLIGIBILITY_ENHANCER to either 0 or 1"
62#endif
63
ossu20a4b3f2017-04-27 02:08:52 -070064// For SendSideBwe, Opus bitrate should be in the range between 6000 and 32000.
minyue10cbb462016-11-07 09:29:22 -080065const int kOpusMinBitrateBps = 6000;
ossu20a4b3f2017-04-27 02:08:52 -070066const int kOpusBitrateFbBps = 32000;
deadbeef80346142016-04-27 14:17:10 -070067
wu@webrtc.orgde305012013-10-31 15:40:38 +000068// Default audio dscp value.
69// See http://tools.ietf.org/html/rfc2474 for details.
70// See also http://tools.ietf.org/html/draft-jennings-rtcweb-qos-00
solenbergd97ec302015-10-07 01:40:33 -070071const rtc::DiffServCodePoint kAudioDscpValue = rtc::DSCP_EF;
henrike@webrtc.org1e09a712013-07-26 19:17:59 +000072
Fredrik Solenbergb5727682015-12-04 15:22:19 +010073// Constants from voice_engine_defines.h.
74const int kMinTelephoneEventCode = 0; // RFC4733 (Section 2.3.1)
75const int kMaxTelephoneEventCode = 255;
Fredrik Solenbergb5727682015-12-04 15:22:19 +010076
solenberg31642aa2016-03-14 08:00:37 -070077const int kMinPayloadType = 0;
78const int kMaxPayloadType = 127;
79
deadbeef884f5852016-01-15 09:20:04 -080080class ProxySink : public webrtc::AudioSinkInterface {
81 public:
Steve Antone78bcb92017-10-31 09:53:08 -070082 explicit ProxySink(AudioSinkInterface* sink) : sink_(sink) {
83 RTC_DCHECK(sink);
84 }
deadbeef884f5852016-01-15 09:20:04 -080085
86 void OnData(const Data& audio) override { sink_->OnData(audio); }
87
88 private:
89 webrtc::AudioSinkInterface* sink_;
90};
91
solenberg0b675462015-10-09 01:37:09 -070092bool ValidateStreamParams(const StreamParams& sp) {
93 if (sp.ssrcs.empty()) {
Mirko Bonadei675513b2017-11-09 11:09:25 +010094 RTC_LOG(LS_ERROR) << "No SSRCs in stream parameters: " << sp.ToString();
solenberg0b675462015-10-09 01:37:09 -070095 return false;
96 }
97 if (sp.ssrcs.size() > 1) {
Mirko Bonadei675513b2017-11-09 11:09:25 +010098 RTC_LOG(LS_ERROR) << "Multiple SSRCs in stream parameters: "
99 << sp.ToString();
solenberg0b675462015-10-09 01:37:09 -0700100 return false;
101 }
102 return true;
103}
104
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000105// Dumps an AudioCodec in RFC 2327-ish format.
solenbergd97ec302015-10-07 01:40:33 -0700106std::string ToString(const AudioCodec& codec) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000107 std::stringstream ss;
ossu20a4b3f2017-04-27 02:08:52 -0700108 ss << codec.name << "/" << codec.clockrate << "/" << codec.channels;
109 if (!codec.params.empty()) {
110 ss << " {";
111 for (const auto& param : codec.params) {
112 ss << " " << param.first << "=" << param.second;
113 }
114 ss << " }";
115 }
116 ss << " (" << codec.id << ")";
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000117 return ss.str();
118}
Minyue Li7100dcd2015-03-27 05:05:59 +0100119
solenbergd97ec302015-10-07 01:40:33 -0700120bool IsCodec(const AudioCodec& codec, const char* ref_name) {
Minyue Li7100dcd2015-03-27 05:05:59 +0100121 return (_stricmp(codec.name.c_str(), ref_name) == 0);
122}
123
solenbergd97ec302015-10-07 01:40:33 -0700124bool FindCodec(const std::vector<AudioCodec>& codecs,
solenberg26c8c912015-11-27 04:00:25 -0800125 const AudioCodec& codec,
126 AudioCodec* found_codec) {
Fredrik Solenbergaf9fb212015-08-26 10:45:53 +0200127 for (const AudioCodec& c : codecs) {
128 if (c.Matches(codec)) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000129 if (found_codec != NULL) {
Fredrik Solenbergaf9fb212015-08-26 10:45:53 +0200130 *found_codec = c;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000131 }
132 return true;
133 }
134 }
135 return false;
136}
wu@webrtc.org1d1ffc92013-10-16 18:12:02 +0000137
solenberg0b675462015-10-09 01:37:09 -0700138bool VerifyUniquePayloadTypes(const std::vector<AudioCodec>& codecs) {
139 if (codecs.empty()) {
140 return true;
141 }
142 std::vector<int> payload_types;
143 for (const AudioCodec& codec : codecs) {
144 payload_types.push_back(codec.id);
145 }
146 std::sort(payload_types.begin(), payload_types.end());
147 auto it = std::unique(payload_types.begin(), payload_types.end());
148 return it == payload_types.end();
149}
150
minyue6b825df2016-10-31 04:08:32 -0700151rtc::Optional<std::string> GetAudioNetworkAdaptorConfig(
152 const AudioOptions& options) {
153 if (options.audio_network_adaptor && *options.audio_network_adaptor &&
154 options.audio_network_adaptor_config) {
155 // Turn on audio network adaptor only when |options_.audio_network_adaptor|
156 // equals true and |options_.audio_network_adaptor_config| has a value.
157 return options.audio_network_adaptor_config;
158 }
Oskar Sundbom78807582017-11-16 11:09:55 +0100159 return rtc::nullopt;
minyue6b825df2016-10-31 04:08:32 -0700160}
161
gyzhou95aa9642016-12-13 14:06:26 -0800162webrtc::AudioState::Config MakeAudioStateConfig(
163 VoEWrapper* voe_wrapper,
peaha9cc40b2017-06-29 08:32:09 -0700164 rtc::scoped_refptr<webrtc::AudioMixer> audio_mixer,
165 rtc::scoped_refptr<webrtc::AudioProcessing> audio_processing) {
solenberg566ef242015-11-06 15:34:49 -0800166 webrtc::AudioState::Config config;
167 config.voice_engine = voe_wrapper->engine();
gyzhou95aa9642016-12-13 14:06:26 -0800168 if (audio_mixer) {
169 config.audio_mixer = audio_mixer;
170 } else {
171 config.audio_mixer = webrtc::AudioMixerImpl::Create();
172 }
peaha9cc40b2017-06-29 08:32:09 -0700173 config.audio_processing = audio_processing;
solenberg566ef242015-11-06 15:34:49 -0800174 return config;
175}
176
deadbeefe702b302017-02-04 12:09:01 -0800177// |max_send_bitrate_bps| is the bitrate from "b=" in SDP.
178// |rtp_max_bitrate_bps| is the bitrate from RtpSender::SetParameters.
minyue7a973442016-10-20 03:27:12 -0700179rtc::Optional<int> ComputeSendBitrate(int max_send_bitrate_bps,
deadbeefe702b302017-02-04 12:09:01 -0800180 rtc::Optional<int> rtp_max_bitrate_bps,
ossu20a4b3f2017-04-27 02:08:52 -0700181 const webrtc::AudioCodecSpec& spec) {
deadbeefe702b302017-02-04 12:09:01 -0800182 // If application-configured bitrate is set, take minimum of that and SDP
183 // bitrate.
zsteina5e0df62017-06-14 11:41:48 -0700184 const int bps =
185 rtp_max_bitrate_bps
186 ? webrtc::MinPositive(max_send_bitrate_bps, *rtp_max_bitrate_bps)
187 : max_send_bitrate_bps;
minyue7a973442016-10-20 03:27:12 -0700188 if (bps <= 0) {
Oskar Sundbom78807582017-11-16 11:09:55 +0100189 return spec.info.default_bitrate_bps;
solenberg971cab02016-06-14 10:02:41 -0700190 }
minyue7a973442016-10-20 03:27:12 -0700191
ossu20a4b3f2017-04-27 02:08:52 -0700192 if (bps < spec.info.min_bitrate_bps) {
minyue7a973442016-10-20 03:27:12 -0700193 // If codec is not multi-rate and |bps| is less than the fixed bitrate then
194 // fail. If codec is not multi-rate and |bps| exceeds or equal the fixed
195 // bitrate then ignore.
Mirko Bonadei675513b2017-11-09 11:09:25 +0100196 RTC_LOG(LS_ERROR) << "Failed to set codec " << spec.format.name
197 << " to bitrate " << bps << " bps"
198 << ", requires at least " << spec.info.min_bitrate_bps
199 << " bps.";
Oskar Sundbom78807582017-11-16 11:09:55 +0100200 return rtc::nullopt;
solenberg971cab02016-06-14 10:02:41 -0700201 }
ossu20a4b3f2017-04-27 02:08:52 -0700202
203 if (spec.info.HasFixedBitrate()) {
Oskar Sundbom78807582017-11-16 11:09:55 +0100204 return spec.info.default_bitrate_bps;
ossu20a4b3f2017-04-27 02:08:52 -0700205 } else {
206 // If codec is multi-rate then just set the bitrate.
Oskar Sundbom78807582017-11-16 11:09:55 +0100207 return std::min(bps, spec.info.max_bitrate_bps);
ossu20a4b3f2017-04-27 02:08:52 -0700208 }
solenberg971cab02016-06-14 10:02:41 -0700209}
210
solenberg76377c52017-02-21 00:54:31 -0800211} // namespace
solenberg971cab02016-06-14 10:02:41 -0700212
ossu29b1a8d2016-06-13 07:34:51 -0700213WebRtcVoiceEngine::WebRtcVoiceEngine(
214 webrtc::AudioDeviceModule* adm,
ossueb1fde42017-05-02 06:46:30 -0700215 const rtc::scoped_refptr<webrtc::AudioEncoderFactory>& encoder_factory,
gyzhou95aa9642016-12-13 14:06:26 -0800216 const rtc::scoped_refptr<webrtc::AudioDecoderFactory>& decoder_factory,
peaha9cc40b2017-06-29 08:32:09 -0700217 rtc::scoped_refptr<webrtc::AudioMixer> audio_mixer,
218 rtc::scoped_refptr<webrtc::AudioProcessing> audio_processing)
ossueb1fde42017-05-02 06:46:30 -0700219 : WebRtcVoiceEngine(adm,
220 encoder_factory,
221 decoder_factory,
222 audio_mixer,
peaha9cc40b2017-06-29 08:32:09 -0700223 audio_processing,
deadbeefeb02c032017-06-15 08:29:25 -0700224 nullptr) {}
solenberg26c8c912015-11-27 04:00:25 -0800225
ossu29b1a8d2016-06-13 07:34:51 -0700226WebRtcVoiceEngine::WebRtcVoiceEngine(
227 webrtc::AudioDeviceModule* adm,
ossueb1fde42017-05-02 06:46:30 -0700228 const rtc::scoped_refptr<webrtc::AudioEncoderFactory>& encoder_factory,
ossu29b1a8d2016-06-13 07:34:51 -0700229 const rtc::scoped_refptr<webrtc::AudioDecoderFactory>& decoder_factory,
gyzhou95aa9642016-12-13 14:06:26 -0800230 rtc::scoped_refptr<webrtc::AudioMixer> audio_mixer,
peaha9cc40b2017-06-29 08:32:09 -0700231 rtc::scoped_refptr<webrtc::AudioProcessing> audio_processing,
ossu29b1a8d2016-06-13 07:34:51 -0700232 VoEWrapper* voe_wrapper)
deadbeefeb02c032017-06-15 08:29:25 -0700233 : adm_(adm),
ossueb1fde42017-05-02 06:46:30 -0700234 encoder_factory_(encoder_factory),
ossu20a4b3f2017-04-27 02:08:52 -0700235 decoder_factory_(decoder_factory),
deadbeefeb02c032017-06-15 08:29:25 -0700236 audio_mixer_(audio_mixer),
peaha9cc40b2017-06-29 08:32:09 -0700237 apm_(audio_processing),
ossu20a4b3f2017-04-27 02:08:52 -0700238 voe_wrapper_(voe_wrapper) {
deadbeefeb02c032017-06-15 08:29:25 -0700239 // This may be called from any thread, so detach thread checkers.
240 worker_thread_checker_.DetachFromThread();
solenberg26c8c912015-11-27 04:00:25 -0800241 signal_thread_checker_.DetachFromThread();
Mirko Bonadei675513b2017-11-09 11:09:25 +0100242 RTC_LOG(LS_INFO) << "WebRtcVoiceEngine::WebRtcVoiceEngine";
deadbeefeb02c032017-06-15 08:29:25 -0700243 RTC_DCHECK(decoder_factory);
244 RTC_DCHECK(encoder_factory);
peaha9cc40b2017-06-29 08:32:09 -0700245 RTC_DCHECK(audio_processing);
deadbeefeb02c032017-06-15 08:29:25 -0700246 // The rest of our initialization will happen in Init.
247}
248
249WebRtcVoiceEngine::~WebRtcVoiceEngine() {
250 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
Mirko Bonadei675513b2017-11-09 11:09:25 +0100251 RTC_LOG(LS_INFO) << "WebRtcVoiceEngine::~WebRtcVoiceEngine";
deadbeefeb02c032017-06-15 08:29:25 -0700252 if (initialized_) {
253 StopAecDump();
254 voe_wrapper_->base()->Terminate();
Fredrik Solenbergd3195342017-11-21 20:33:05 +0100255
256 // Stop AudioDevice.
257 adm()->StopPlayout();
258 adm()->StopRecording();
259 adm()->RegisterAudioCallback(nullptr);
260 adm()->Terminate();
deadbeefeb02c032017-06-15 08:29:25 -0700261 }
262}
263
264void WebRtcVoiceEngine::Init() {
265 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
Mirko Bonadei675513b2017-11-09 11:09:25 +0100266 RTC_LOG(LS_INFO) << "WebRtcVoiceEngine::Init";
deadbeefeb02c032017-06-15 08:29:25 -0700267
268 // TaskQueue expects to be created/destroyed on the same thread.
269 low_priority_worker_queue_.reset(
270 new rtc::TaskQueue("rtc-low-prio", rtc::TaskQueue::Priority::LOW));
271
272 // VoEWrapper needs to be created on the worker thread. It's expected to be
273 // null here unless it's being injected for testing.
274 if (!voe_wrapper_) {
275 voe_wrapper_.reset(new VoEWrapper());
276 }
solenberg26c8c912015-11-27 04:00:25 -0800277
ossueb1fde42017-05-02 06:46:30 -0700278 // Load our audio codec lists.
Mirko Bonadei675513b2017-11-09 11:09:25 +0100279 RTC_LOG(LS_INFO) << "Supported send codecs in order of preference:";
ossu20a4b3f2017-04-27 02:08:52 -0700280 send_codecs_ = CollectCodecs(encoder_factory_->GetSupportedEncoders());
ossuc54071d2016-08-17 02:45:41 -0700281 for (const AudioCodec& codec : send_codecs_) {
Mirko Bonadei675513b2017-11-09 11:09:25 +0100282 RTC_LOG(LS_INFO) << ToString(codec);
ossuc54071d2016-08-17 02:45:41 -0700283 }
284
Mirko Bonadei675513b2017-11-09 11:09:25 +0100285 RTC_LOG(LS_INFO) << "Supported recv codecs in order of preference:";
ossu20a4b3f2017-04-27 02:08:52 -0700286 recv_codecs_ = CollectCodecs(decoder_factory_->GetSupportedDecoders());
ossuc54071d2016-08-17 02:45:41 -0700287 for (const AudioCodec& codec : recv_codecs_) {
Mirko Bonadei675513b2017-11-09 11:09:25 +0100288 RTC_LOG(LS_INFO) << ToString(codec);
buildbot@webrtc.org13d67762014-05-02 17:33:29 +0000289 }
buildbot@webrtc.org13d67762014-05-02 17:33:29 +0000290
solenberg88499ec2016-09-07 07:34:41 -0700291 channel_config_.enable_voice_pacing = true;
buildbot@webrtc.org13d67762014-05-02 17:33:29 +0000292
Fredrik Solenbergd3195342017-11-21 20:33:05 +0100293#if defined(WEBRTC_INCLUDE_INTERNAL_AUDIO_DEVICE)
294 // No ADM supplied? Create a default one.
solenbergff976312016-03-30 23:28:51 -0700295 if (!adm_) {
Fredrik Solenbergd3195342017-11-21 20:33:05 +0100296 adm_ = webrtc::AudioDeviceModule::Create(
297 webrtc::AudioDeviceModule::kPlatformDefaultAudio);
solenbergff976312016-03-30 23:28:51 -0700298 }
Fredrik Solenbergd3195342017-11-21 20:33:05 +0100299#endif // WEBRTC_INCLUDE_INTERNAL_AUDIO_DEVICE
300 RTC_CHECK(adm());
301 webrtc::adm_helpers::Init(adm());
solenbergff976312016-03-30 23:28:51 -0700302
Fredrik Solenbergd3195342017-11-21 20:33:05 +0100303 RTC_CHECK_EQ(0, voe_wrapper_->base()->Init(adm(), apm(), decoder_factory_));
solenberg76377c52017-02-21 00:54:31 -0800304 transmit_mixer_ = voe_wrapper_->base()->transmit_mixer();
305 RTC_DCHECK(transmit_mixer_);
306
buildbot@webrtc.org13d67762014-05-02 17:33:29 +0000307 // Save the default AGC configuration settings. This must happen before
solenberg246b8172015-12-08 09:50:23 -0800308 // calling ApplyOptions or the default will be overwritten.
peaha9cc40b2017-06-29 08:32:09 -0700309 default_agc_config_ = webrtc::apm_helpers::GetAgcConfig(apm());
buildbot@webrtc.org13d67762014-05-02 17:33:29 +0000310
solenberg0f7d2932016-01-15 01:40:39 -0800311 // Set default engine options.
312 {
313 AudioOptions options;
Oskar Sundbom78807582017-11-16 11:09:55 +0100314 options.echo_cancellation = true;
315 options.auto_gain_control = true;
316 options.noise_suppression = true;
317 options.highpass_filter = true;
318 options.stereo_swapping = false;
319 options.audio_jitter_buffer_max_packets = 50;
320 options.audio_jitter_buffer_fast_accelerate = false;
321 options.typing_detection = true;
322 options.adjust_agc_delta = 0;
323 options.experimental_agc = false;
324 options.extended_filter_aec = false;
325 options.delay_agnostic_aec = false;
326 options.experimental_ns = false;
327 options.intelligibility_enhancer = false;
328 options.level_control = false;
329 options.residual_echo_detector = true;
solenbergff976312016-03-30 23:28:51 -0700330 bool error = ApplyOptions(options);
331 RTC_DCHECK(error);
buildbot@webrtc.org13d67762014-05-02 17:33:29 +0000332 }
333
solenberg9a5f032222017-03-15 06:14:12 -0700334 // Set default audio devices.
335#if !defined(WEBRTC_IOS)
solenberg9a5f032222017-03-15 06:14:12 -0700336 apm()->Initialize();
solenberg9a5f032222017-03-15 06:14:12 -0700337#endif // !WEBRTC_IOS
buildbot@webrtc.org13d67762014-05-02 17:33:29 +0000338
deadbeefeb02c032017-06-15 08:29:25 -0700339 // May be null for VoE injected for testing.
340 if (voe()->engine()) {
peaha9cc40b2017-06-29 08:32:09 -0700341 audio_state_ = webrtc::AudioState::Create(
342 MakeAudioStateConfig(voe(), audio_mixer_, apm_));
Fredrik Solenbergd3195342017-11-21 20:33:05 +0100343
344 // Connect the ADM to our audio path.
345 adm()->RegisterAudioCallback(audio_state_->audio_transport());
deadbeefeb02c032017-06-15 08:29:25 -0700346 }
347
348 initialized_ = true;
buildbot@webrtc.org13d67762014-05-02 17:33:29 +0000349}
350
solenberg566ef242015-11-06 15:34:49 -0800351rtc::scoped_refptr<webrtc::AudioState>
352 WebRtcVoiceEngine::GetAudioState() const {
353 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
354 return audio_state_;
355}
356
nisse51542be2016-02-12 02:27:06 -0800357VoiceMediaChannel* WebRtcVoiceEngine::CreateChannel(
358 webrtc::Call* call,
359 const MediaConfig& config,
Jelena Marusicc28a8962015-05-29 15:05:44 +0200360 const AudioOptions& options) {
solenberg566ef242015-11-06 15:34:49 -0800361 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
nisse51542be2016-02-12 02:27:06 -0800362 return new WebRtcVoiceMediaChannel(this, config, options, call);
buildbot@webrtc.org13d67762014-05-02 17:33:29 +0000363}
364
buildbot@webrtc.org13d67762014-05-02 17:33:29 +0000365bool WebRtcVoiceEngine::ApplyOptions(const AudioOptions& options_in) {
solenberg566ef242015-11-06 15:34:49 -0800366 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
Mirko Bonadei675513b2017-11-09 11:09:25 +0100367 RTC_LOG(LS_INFO) << "WebRtcVoiceEngine::ApplyOptions: "
368 << options_in.ToString();
solenberg0f7d2932016-01-15 01:40:39 -0800369 AudioOptions options = options_in; // The options are modified below.
solenberg246b8172015-12-08 09:50:23 -0800370
peah8a8ebd92017-05-22 15:48:47 -0700371 // Set and adjust echo canceller options.
buildbot@webrtc.org13d67762014-05-02 17:33:29 +0000372 // kEcConference is AEC with high suppression.
373 webrtc::EcModes ec_mode = webrtc::kEcConference;
kwiberg102c6a62015-10-30 02:47:38 -0700374 if (options.aecm_generate_comfort_noise) {
Mirko Bonadei675513b2017-11-09 11:09:25 +0100375 RTC_LOG(LS_VERBOSE) << "Comfort noise explicitly set to "
376 << *options.aecm_generate_comfort_noise
377 << " (default is false).";
buildbot@webrtc.org13d67762014-05-02 17:33:29 +0000378 }
379
kjellanderfcfc8042016-01-14 11:01:09 -0800380#if defined(WEBRTC_IOS)
peah8a8ebd92017-05-22 15:48:47 -0700381 // On iOS, VPIO provides built-in EC.
Oskar Sundbom78807582017-11-16 11:09:55 +0100382 options.echo_cancellation = false;
383 options.extended_filter_aec = false;
Mirko Bonadei675513b2017-11-09 11:09:25 +0100384 RTC_LOG(LS_INFO) << "Always disable AEC on iOS. Use built-in instead.";
Mirko Bonadeic8c71b92017-10-16 11:08:54 +0200385#elif defined(WEBRTC_ANDROID)
buildbot@webrtc.org13d67762014-05-02 17:33:29 +0000386 ec_mode = webrtc::kEcAecm;
Oskar Sundbom78807582017-11-16 11:09:55 +0100387 options.extended_filter_aec = false;
buildbot@webrtc.org13d67762014-05-02 17:33:29 +0000388#endif
389
Bjorn Volckerbf395c12015-03-25 22:45:56 +0100390 // Delay Agnostic AEC automatically turns on EC if not set except on iOS
391 // where the feature is not supported.
392 bool use_delay_agnostic_aec = false;
kjellanderfcfc8042016-01-14 11:01:09 -0800393#if !defined(WEBRTC_IOS)
kwiberg102c6a62015-10-30 02:47:38 -0700394 if (options.delay_agnostic_aec) {
395 use_delay_agnostic_aec = *options.delay_agnostic_aec;
Bjorn Volckerbf395c12015-03-25 22:45:56 +0100396 if (use_delay_agnostic_aec) {
Oskar Sundbom78807582017-11-16 11:09:55 +0100397 options.echo_cancellation = true;
398 options.extended_filter_aec = true;
Bjorn Volckerbf395c12015-03-25 22:45:56 +0100399 ec_mode = webrtc::kEcConference;
400 }
401 }
402#endif
403
peah8a8ebd92017-05-22 15:48:47 -0700404// Set and adjust noise suppressor options.
405#if defined(WEBRTC_IOS)
406 // On iOS, VPIO provides built-in NS.
Oskar Sundbom78807582017-11-16 11:09:55 +0100407 options.noise_suppression = false;
408 options.typing_detection = false;
409 options.experimental_ns = false;
Mirko Bonadei675513b2017-11-09 11:09:25 +0100410 RTC_LOG(LS_INFO) << "Always disable NS on iOS. Use built-in instead.";
Mirko Bonadeic8c71b92017-10-16 11:08:54 +0200411#elif defined(WEBRTC_ANDROID)
Oskar Sundbom78807582017-11-16 11:09:55 +0100412 options.typing_detection = false;
413 options.experimental_ns = false;
peah8a8ebd92017-05-22 15:48:47 -0700414#endif
415
416// Set and adjust gain control options.
417#if defined(WEBRTC_IOS)
418 // On iOS, VPIO provides built-in AGC.
Oskar Sundbom78807582017-11-16 11:09:55 +0100419 options.auto_gain_control = false;
420 options.experimental_agc = false;
Mirko Bonadei675513b2017-11-09 11:09:25 +0100421 RTC_LOG(LS_INFO) << "Always disable AGC on iOS. Use built-in instead.";
Mirko Bonadeic8c71b92017-10-16 11:08:54 +0200422#elif defined(WEBRTC_ANDROID)
Oskar Sundbom78807582017-11-16 11:09:55 +0100423 options.experimental_agc = false;
peah8a8ebd92017-05-22 15:48:47 -0700424#endif
425
426#if defined(WEBRTC_IOS) || defined(WEBRTC_ANDROID)
Mirko Bonadeic8c71b92017-10-16 11:08:54 +0200427 // Turn off the gain control if specified by the field trial.
428 // The purpose of the field trial is to reduce the amount of resampling
429 // performed inside the audio processing module on mobile platforms by
430 // whenever possible turning off the fixed AGC mode and the high-pass filter.
431 // (https://bugs.chromium.org/p/webrtc/issues/detail?id=6181).
peah8a8ebd92017-05-22 15:48:47 -0700432 if (webrtc::field_trial::IsEnabled(
433 "WebRTC-Audio-MinimizeResamplingOnMobile")) {
Oskar Sundbom78807582017-11-16 11:09:55 +0100434 options.auto_gain_control = false;
Mirko Bonadei675513b2017-11-09 11:09:25 +0100435 RTC_LOG(LS_INFO) << "Disable AGC according to field trial.";
Steve Antone78bcb92017-10-31 09:53:08 -0700436 if (!(options.noise_suppression.value_or(false) ||
peah8a8ebd92017-05-22 15:48:47 -0700437 options.echo_cancellation.value_or(false))) {
438 // If possible, turn off the high-pass filter.
Mirko Bonadei675513b2017-11-09 11:09:25 +0100439 RTC_LOG(LS_INFO)
440 << "Disable high-pass filter in response to field trial.";
Oskar Sundbom78807582017-11-16 11:09:55 +0100441 options.highpass_filter = false;
peah8a8ebd92017-05-22 15:48:47 -0700442 }
443 }
444#endif
445
peah1bcfce52016-08-26 07:16:04 -0700446#if (WEBRTC_INTELLIGIBILITY_ENHANCER == 0)
447 // Hardcode the intelligibility enhancer to be off.
Oskar Sundbom78807582017-11-16 11:09:55 +0100448 options.intelligibility_enhancer = false;
peah1bcfce52016-08-26 07:16:04 -0700449#endif
450
kwiberg102c6a62015-10-30 02:47:38 -0700451 if (options.echo_cancellation) {
henrika@webrtc.orga954c072014-12-09 16:22:09 +0000452 // Check if platform supports built-in EC. Currently only supported on
453 // Android and in combination with Java based audio layer.
454 // TODO(henrika): investigate possibility to support built-in EC also
455 // in combination with Open SL ES audio.
solenberg5b5129a2016-04-08 05:35:48 -0700456 const bool built_in_aec = adm()->BuiltInAECIsAvailable();
Bjorn Volcker73f72102015-06-03 14:50:15 +0200457 if (built_in_aec) {
Bjorn Volckerccfc9392015-05-07 07:43:17 +0200458 // Built-in EC exists on this device and use_delay_agnostic_aec is not
459 // overriding it. Enable/Disable it according to the echo_cancellation
460 // audio option.
Bjorn Volcker73f72102015-06-03 14:50:15 +0200461 const bool enable_built_in_aec =
kwiberg102c6a62015-10-30 02:47:38 -0700462 *options.echo_cancellation && !use_delay_agnostic_aec;
solenberg5b5129a2016-04-08 05:35:48 -0700463 if (adm()->EnableBuiltInAEC(enable_built_in_aec) == 0 &&
Bjorn Volcker73f72102015-06-03 14:50:15 +0200464 enable_built_in_aec) {
Bjorn Volckerbf395c12015-03-25 22:45:56 +0100465 // Disable internal software EC if built-in EC is enabled,
henrika@webrtc.orga954c072014-12-09 16:22:09 +0000466 // i.e., replace the software EC with the built-in EC.
Oskar Sundbom78807582017-11-16 11:09:55 +0100467 options.echo_cancellation = false;
Mirko Bonadei675513b2017-11-09 11:09:25 +0100468 RTC_LOG(LS_INFO)
469 << "Disabling EC since built-in EC will be used instead";
henrika@webrtc.orga954c072014-12-09 16:22:09 +0000470 }
471 }
solenberg76377c52017-02-21 00:54:31 -0800472 webrtc::apm_helpers::SetEcStatus(
473 apm(), *options.echo_cancellation, ec_mode);
Mirko Bonadeic8c71b92017-10-16 11:08:54 +0200474#if !defined(WEBRTC_ANDROID)
solenberg76377c52017-02-21 00:54:31 -0800475 webrtc::apm_helpers::SetEcMetricsStatus(apm(), *options.echo_cancellation);
buildbot@webrtc.org13d67762014-05-02 17:33:29 +0000476#endif
477 if (ec_mode == webrtc::kEcAecm) {
kwiberg102c6a62015-10-30 02:47:38 -0700478 bool cn = options.aecm_generate_comfort_noise.value_or(false);
solenberg76377c52017-02-21 00:54:31 -0800479 webrtc::apm_helpers::SetAecmMode(apm(), cn);
buildbot@webrtc.org13d67762014-05-02 17:33:29 +0000480 }
481 }
482
kwiberg102c6a62015-10-30 02:47:38 -0700483 if (options.auto_gain_control) {
peah72a56452016-08-22 12:08:55 -0700484 bool built_in_agc_avaliable = adm()->BuiltInAGCIsAvailable();
485 if (built_in_agc_avaliable) {
solenberg5b5129a2016-04-08 05:35:48 -0700486 if (adm()->EnableBuiltInAGC(*options.auto_gain_control) == 0 &&
kwiberg102c6a62015-10-30 02:47:38 -0700487 *options.auto_gain_control) {
henrikac14f5ff2015-09-23 14:08:33 +0200488 // Disable internal software AGC if built-in AGC is enabled,
489 // i.e., replace the software AGC with the built-in AGC.
Oskar Sundbom78807582017-11-16 11:09:55 +0100490 options.auto_gain_control = false;
Mirko Bonadei675513b2017-11-09 11:09:25 +0100491 RTC_LOG(LS_INFO)
492 << "Disabling AGC since built-in AGC will be used instead";
henrikac14f5ff2015-09-23 14:08:33 +0200493 }
494 }
solenberg22818a52017-03-16 01:20:23 -0700495 webrtc::apm_helpers::SetAgcStatus(apm(), adm(), *options.auto_gain_control);
buildbot@webrtc.org13d67762014-05-02 17:33:29 +0000496 }
497
kwiberg102c6a62015-10-30 02:47:38 -0700498 if (options.tx_agc_target_dbov || options.tx_agc_digital_compression_gain ||
solenberg76377c52017-02-21 00:54:31 -0800499 options.tx_agc_limiter || options.adjust_agc_delta) {
buildbot@webrtc.org13d67762014-05-02 17:33:29 +0000500 // Override default_agc_config_. Generally, an unset option means "leave
501 // the VoE bits alone" in this function, so we want whatever is set to be
502 // stored as the new "default". If we didn't, then setting e.g.
503 // tx_agc_target_dbov would reset digital compression gain and limiter
504 // settings.
505 // Also, if we don't update default_agc_config_, then adjust_agc_delta
506 // would be an offset from the original values, and not whatever was set
507 // explicitly.
kwiberg102c6a62015-10-30 02:47:38 -0700508 default_agc_config_.targetLeveldBOv = options.tx_agc_target_dbov.value_or(
509 default_agc_config_.targetLeveldBOv);
buildbot@webrtc.org13d67762014-05-02 17:33:29 +0000510 default_agc_config_.digitalCompressionGaindB =
kwiberg102c6a62015-10-30 02:47:38 -0700511 options.tx_agc_digital_compression_gain.value_or(
buildbot@webrtc.org13d67762014-05-02 17:33:29 +0000512 default_agc_config_.digitalCompressionGaindB);
513 default_agc_config_.limiterEnable =
kwiberg102c6a62015-10-30 02:47:38 -0700514 options.tx_agc_limiter.value_or(default_agc_config_.limiterEnable);
solenberg76377c52017-02-21 00:54:31 -0800515
516 webrtc::AgcConfig config = default_agc_config_;
517 if (options.adjust_agc_delta) {
518 config.targetLeveldBOv -= *options.adjust_agc_delta;
Mirko Bonadei675513b2017-11-09 11:09:25 +0100519 RTC_LOG(LS_INFO) << "Adjusting AGC level from default -"
520 << default_agc_config_.targetLeveldBOv << "dB to -"
521 << config.targetLeveldBOv << "dB";
buildbot@webrtc.org13d67762014-05-02 17:33:29 +0000522 }
peaha9cc40b2017-06-29 08:32:09 -0700523 webrtc::apm_helpers::SetAgcConfig(apm(), config);
buildbot@webrtc.org13d67762014-05-02 17:33:29 +0000524 }
525
Alejandro Luebsc9b0c262016-05-16 15:32:38 -0700526 if (options.intelligibility_enhancer) {
527 intelligibility_enhancer_ = options.intelligibility_enhancer;
528 }
529 if (intelligibility_enhancer_ && *intelligibility_enhancer_) {
Mirko Bonadei675513b2017-11-09 11:09:25 +0100530 RTC_LOG(LS_INFO) << "Enabling NS when Intelligibility Enhancer is active.";
Alejandro Luebsc9b0c262016-05-16 15:32:38 -0700531 options.noise_suppression = intelligibility_enhancer_;
532 }
533
kwiberg102c6a62015-10-30 02:47:38 -0700534 if (options.noise_suppression) {
Alejandro Luebsc9b0c262016-05-16 15:32:38 -0700535 if (adm()->BuiltInNSIsAvailable()) {
536 bool builtin_ns =
537 *options.noise_suppression &&
538 !(intelligibility_enhancer_ && *intelligibility_enhancer_);
539 if (adm()->EnableBuiltInNS(builtin_ns) == 0 && builtin_ns) {
henrikac14f5ff2015-09-23 14:08:33 +0200540 // Disable internal software NS if built-in NS is enabled,
541 // i.e., replace the software NS with the built-in NS.
Oskar Sundbom78807582017-11-16 11:09:55 +0100542 options.noise_suppression = false;
Mirko Bonadei675513b2017-11-09 11:09:25 +0100543 RTC_LOG(LS_INFO)
544 << "Disabling NS since built-in NS will be used instead";
henrikac14f5ff2015-09-23 14:08:33 +0200545 }
546 }
solenberg76377c52017-02-21 00:54:31 -0800547 webrtc::apm_helpers::SetNsStatus(apm(), *options.noise_suppression);
buildbot@webrtc.org13d67762014-05-02 17:33:29 +0000548 }
549
kwiberg102c6a62015-10-30 02:47:38 -0700550 if (options.stereo_swapping) {
Mirko Bonadei675513b2017-11-09 11:09:25 +0100551 RTC_LOG(LS_INFO) << "Stereo swapping enabled? " << *options.stereo_swapping;
solenberg76377c52017-02-21 00:54:31 -0800552 transmit_mixer()->EnableStereoChannelSwapping(*options.stereo_swapping);
buildbot@webrtc.org13d67762014-05-02 17:33:29 +0000553 }
554
kwiberg102c6a62015-10-30 02:47:38 -0700555 if (options.audio_jitter_buffer_max_packets) {
Mirko Bonadei675513b2017-11-09 11:09:25 +0100556 RTC_LOG(LS_INFO) << "NetEq capacity is "
557 << *options.audio_jitter_buffer_max_packets;
solenberg88499ec2016-09-07 07:34:41 -0700558 channel_config_.acm_config.neteq_config.max_packets_in_buffer =
559 std::max(20, *options.audio_jitter_buffer_max_packets);
Henrik Lundin64dad832015-05-11 12:44:23 +0200560 }
kwiberg102c6a62015-10-30 02:47:38 -0700561 if (options.audio_jitter_buffer_fast_accelerate) {
Mirko Bonadei675513b2017-11-09 11:09:25 +0100562 RTC_LOG(LS_INFO) << "NetEq fast mode? "
563 << *options.audio_jitter_buffer_fast_accelerate;
solenberg88499ec2016-09-07 07:34:41 -0700564 channel_config_.acm_config.neteq_config.enable_fast_accelerate =
565 *options.audio_jitter_buffer_fast_accelerate;
Henrik Lundin5263b3c2015-06-01 10:29:41 +0200566 }
567
kwiberg102c6a62015-10-30 02:47:38 -0700568 if (options.typing_detection) {
Mirko Bonadei675513b2017-11-09 11:09:25 +0100569 RTC_LOG(LS_INFO) << "Typing detection is enabled? "
570 << *options.typing_detection;
solenberg76377c52017-02-21 00:54:31 -0800571 webrtc::apm_helpers::SetTypingDetectionStatus(
572 apm(), *options.typing_detection);
buildbot@webrtc.org13d67762014-05-02 17:33:29 +0000573 }
574
buildbot@webrtc.org1f8a2372014-08-28 10:52:44 +0000575 webrtc::Config config;
576
kwiberg102c6a62015-10-30 02:47:38 -0700577 if (options.delay_agnostic_aec)
578 delay_agnostic_aec_ = options.delay_agnostic_aec;
579 if (delay_agnostic_aec_) {
Mirko Bonadei675513b2017-11-09 11:09:25 +0100580 RTC_LOG(LS_INFO) << "Delay agnostic aec is enabled? "
581 << *delay_agnostic_aec_;
henrik.lundin0f133b92015-07-02 00:17:55 -0700582 config.Set<webrtc::DelayAgnostic>(
kwiberg102c6a62015-10-30 02:47:38 -0700583 new webrtc::DelayAgnostic(*delay_agnostic_aec_));
Bjorn Volckerbf395c12015-03-25 22:45:56 +0100584 }
585
kwiberg102c6a62015-10-30 02:47:38 -0700586 if (options.extended_filter_aec) {
587 extended_filter_aec_ = options.extended_filter_aec;
588 }
589 if (extended_filter_aec_) {
Mirko Bonadei675513b2017-11-09 11:09:25 +0100590 RTC_LOG(LS_INFO) << "Extended filter aec is enabled? "
591 << *extended_filter_aec_;
Henrik Lundin441f6342015-06-09 16:03:13 +0200592 config.Set<webrtc::ExtendedFilter>(
kwiberg102c6a62015-10-30 02:47:38 -0700593 new webrtc::ExtendedFilter(*extended_filter_aec_));
buildbot@webrtc.org1f8a2372014-08-28 10:52:44 +0000594 }
595
kwiberg102c6a62015-10-30 02:47:38 -0700596 if (options.experimental_ns) {
597 experimental_ns_ = options.experimental_ns;
598 }
599 if (experimental_ns_) {
Mirko Bonadei675513b2017-11-09 11:09:25 +0100600 RTC_LOG(LS_INFO) << "Experimental ns is enabled? " << *experimental_ns_;
buildbot@webrtc.org1f8a2372014-08-28 10:52:44 +0000601 config.Set<webrtc::ExperimentalNs>(
kwiberg102c6a62015-10-30 02:47:38 -0700602 new webrtc::ExperimentalNs(*experimental_ns_));
buildbot@webrtc.org1f8a2372014-08-28 10:52:44 +0000603 }
buildbot@webrtc.org1f8a2372014-08-28 10:52:44 +0000604
Alejandro Luebsc9b0c262016-05-16 15:32:38 -0700605 if (intelligibility_enhancer_) {
Mirko Bonadei675513b2017-11-09 11:09:25 +0100606 RTC_LOG(LS_INFO) << "Intelligibility Enhancer is enabled? "
607 << *intelligibility_enhancer_;
Alejandro Luebsc9b0c262016-05-16 15:32:38 -0700608 config.Set<webrtc::Intelligibility>(
609 new webrtc::Intelligibility(*intelligibility_enhancer_));
610 }
611
peaha3333bf2016-06-30 00:02:34 -0700612 if (options.level_control) {
613 level_control_ = options.level_control;
614 }
615
peahb1c9d1d2017-07-25 15:45:24 -0700616 webrtc::AudioProcessing::Config apm_config = apm()->GetConfig();
617
Mirko Bonadei675513b2017-11-09 11:09:25 +0100618 RTC_LOG(LS_INFO) << "Level control: "
619 << (!!level_control_ ? *level_control_ : -1);
peaha3333bf2016-06-30 00:02:34 -0700620 if (level_control_) {
peahb1c9d1d2017-07-25 15:45:24 -0700621 apm_config.level_controller.enabled = *level_control_;
aleloie33c5d92016-10-20 01:53:27 -0700622 if (options.level_control_initial_peak_level_dbfs) {
peahb1c9d1d2017-07-25 15:45:24 -0700623 apm_config.level_controller.initial_peak_level_dbfs =
aleloie33c5d92016-10-20 01:53:27 -0700624 *options.level_control_initial_peak_level_dbfs;
625 }
peaha3333bf2016-06-30 00:02:34 -0700626 }
627
peah8271d042016-11-22 07:24:52 -0800628 if (options.highpass_filter) {
peahb1c9d1d2017-07-25 15:45:24 -0700629 apm_config.high_pass_filter.enabled = *options.highpass_filter;
peah8271d042016-11-22 07:24:52 -0800630 }
631
ivoc4ca18692017-02-10 05:11:09 -0800632 if (options.residual_echo_detector) {
peahb1c9d1d2017-07-25 15:45:24 -0700633 apm_config.residual_echo_detector.enabled = *options.residual_echo_detector;
ivoc4ca18692017-02-10 05:11:09 -0800634 }
635
solenberg059fb442016-10-26 05:12:24 -0700636 apm()->SetExtraOptions(config);
peahb1c9d1d2017-07-25 15:45:24 -0700637 apm()->ApplyConfig(apm_config);
buildbot@webrtc.org13d67762014-05-02 17:33:29 +0000638 return true;
639}
640
solenberg796b8f92017-03-01 17:02:23 -0800641// TODO(solenberg): Remove, once AudioMonitor is gone.
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000642int WebRtcVoiceEngine::GetInputLevel() {
solenberg566ef242015-11-06 15:34:49 -0800643 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
solenberg796b8f92017-03-01 17:02:23 -0800644 int8_t level = transmit_mixer()->AudioLevel();
645 RTC_DCHECK_LE(0, level);
646 return level;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000647}
648
ossudedfd282016-06-14 07:12:39 -0700649const std::vector<AudioCodec>& WebRtcVoiceEngine::send_codecs() const {
650 RTC_DCHECK(signal_thread_checker_.CalledOnValidThread());
ossuc54071d2016-08-17 02:45:41 -0700651 return send_codecs_;
ossudedfd282016-06-14 07:12:39 -0700652}
653
654const std::vector<AudioCodec>& WebRtcVoiceEngine::recv_codecs() const {
solenberg566ef242015-11-06 15:34:49 -0800655 RTC_DCHECK(signal_thread_checker_.CalledOnValidThread());
ossuc54071d2016-08-17 02:45:41 -0700656 return recv_codecs_;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000657}
658
Stefan Holmer9d69c3f2015-12-07 10:45:43 +0100659RtpCapabilities WebRtcVoiceEngine::GetCapabilities() const {
solenberg566ef242015-11-06 15:34:49 -0800660 RTC_DCHECK(signal_thread_checker_.CalledOnValidThread());
Stefan Holmer9d69c3f2015-12-07 10:45:43 +0100661 RtpCapabilities capabilities;
Stefan Holmer9d69c3f2015-12-07 10:45:43 +0100662 capabilities.header_extensions.push_back(
isheriff6f8d6862016-05-26 11:24:55 -0700663 webrtc::RtpExtension(webrtc::RtpExtension::kAudioLevelUri,
664 webrtc::RtpExtension::kAudioLevelDefaultId));
sprangc1b57a12017-02-28 08:50:47 -0800665 if (webrtc::field_trial::IsEnabled("WebRTC-Audio-SendSideBwe")) {
isheriff6f8d6862016-05-26 11:24:55 -0700666 capabilities.header_extensions.push_back(webrtc::RtpExtension(
667 webrtc::RtpExtension::kTransportSequenceNumberUri,
668 webrtc::RtpExtension::kTransportSequenceNumberDefaultId));
stefanba4c0e42016-02-04 04:12:24 -0800669 }
Stefan Holmer9d69c3f2015-12-07 10:45:43 +0100670 return capabilities;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000671}
672
solenberg63b34542015-09-29 06:06:31 -0700673void WebRtcVoiceEngine::RegisterChannel(WebRtcVoiceMediaChannel* channel) {
solenberg566ef242015-11-06 15:34:49 -0800674 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
675 RTC_DCHECK(channel);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000676 channels_.push_back(channel);
677}
678
solenberg63b34542015-09-29 06:06:31 -0700679void WebRtcVoiceEngine::UnregisterChannel(WebRtcVoiceMediaChannel* channel) {
solenberg566ef242015-11-06 15:34:49 -0800680 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
solenberg63b34542015-09-29 06:06:31 -0700681 auto it = std::find(channels_.begin(), channels_.end(), channel);
solenberg566ef242015-11-06 15:34:49 -0800682 RTC_DCHECK(it != channels_.end());
683 channels_.erase(it);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000684}
685
ivocd66b44d2016-01-15 03:06:36 -0800686bool WebRtcVoiceEngine::StartAecDump(rtc::PlatformFile file,
687 int64_t max_size_bytes) {
solenberg566ef242015-11-06 15:34:49 -0800688 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
deadbeefeb02c032017-06-15 08:29:25 -0700689 auto aec_dump = webrtc::AecDumpFactory::Create(
690 file, max_size_bytes, low_priority_worker_queue_.get());
aleloi048cbdd2017-05-29 02:56:27 -0700691 if (!aec_dump) {
wu@webrtc.orga8910d22014-01-23 22:12:45 +0000692 return false;
693 }
aleloi048cbdd2017-05-29 02:56:27 -0700694 apm()->AttachAecDump(std::move(aec_dump));
wu@webrtc.orga9890802013-12-13 00:21:03 +0000695 return true;
wu@webrtc.orga9890802013-12-13 00:21:03 +0000696}
697
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000698void WebRtcVoiceEngine::StartAecDump(const std::string& filename) {
solenberg566ef242015-11-06 15:34:49 -0800699 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
aleloi048cbdd2017-05-29 02:56:27 -0700700
deadbeefeb02c032017-06-15 08:29:25 -0700701 auto aec_dump = webrtc::AecDumpFactory::Create(
702 filename, -1, low_priority_worker_queue_.get());
aleloi048cbdd2017-05-29 02:56:27 -0700703 if (aec_dump) {
704 apm()->AttachAecDump(std::move(aec_dump));
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000705 }
706}
707
708void WebRtcVoiceEngine::StopAecDump() {
solenberg566ef242015-11-06 15:34:49 -0800709 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
aleloi048cbdd2017-05-29 02:56:27 -0700710 apm()->DetachAecDump();
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000711}
712
solenberg0a617e22015-10-20 15:49:38 -0700713int WebRtcVoiceEngine::CreateVoEChannel() {
solenberg566ef242015-11-06 15:34:49 -0800714 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
solenberg88499ec2016-09-07 07:34:41 -0700715 return voe_wrapper_->base()->CreateChannel(channel_config_);
sergeyu@chromium.org5bc25c42013-12-05 00:24:06 +0000716}
717
solenberg5b5129a2016-04-08 05:35:48 -0700718webrtc::AudioDeviceModule* WebRtcVoiceEngine::adm() {
719 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
720 RTC_DCHECK(adm_);
Fredrik Solenbergd3195342017-11-21 20:33:05 +0100721 return adm_.get();
solenberg5b5129a2016-04-08 05:35:48 -0700722}
723
peahb1c9d1d2017-07-25 15:45:24 -0700724webrtc::AudioProcessing* WebRtcVoiceEngine::apm() const {
solenberg059fb442016-10-26 05:12:24 -0700725 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
peaha9cc40b2017-06-29 08:32:09 -0700726 return apm_.get();
solenberg059fb442016-10-26 05:12:24 -0700727}
728
solenberg76377c52017-02-21 00:54:31 -0800729webrtc::voe::TransmitMixer* WebRtcVoiceEngine::transmit_mixer() {
730 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
731 RTC_DCHECK(transmit_mixer_);
732 return transmit_mixer_;
733}
734
ossu20a4b3f2017-04-27 02:08:52 -0700735AudioCodecs WebRtcVoiceEngine::CollectCodecs(
736 const std::vector<webrtc::AudioCodecSpec>& specs) const {
ossuc54071d2016-08-17 02:45:41 -0700737 PayloadTypeMapper mapper;
738 AudioCodecs out;
ossuc54071d2016-08-17 02:45:41 -0700739
solenberg2779bab2016-11-17 04:45:19 -0800740 // Only generate CN payload types for these clockrates:
ossuc54071d2016-08-17 02:45:41 -0700741 std::map<int, bool, std::greater<int>> generate_cn = {{ 8000, false },
742 { 16000, false },
743 { 32000, false }};
solenberg2779bab2016-11-17 04:45:19 -0800744 // Only generate telephone-event payload types for these clockrates:
745 std::map<int, bool, std::greater<int>> generate_dtmf = {{ 8000, false },
746 { 16000, false },
747 { 32000, false },
748 { 48000, false }};
ossuc54071d2016-08-17 02:45:41 -0700749
ossu9def8002017-02-09 05:14:32 -0800750 auto map_format = [&mapper](const webrtc::SdpAudioFormat& format,
751 AudioCodecs* out) {
ossuc54071d2016-08-17 02:45:41 -0700752 rtc::Optional<AudioCodec> opt_codec = mapper.ToAudioCodec(format);
ossu9def8002017-02-09 05:14:32 -0800753 if (opt_codec) {
754 if (out) {
755 out->push_back(*opt_codec);
756 }
757 } else {
Mirko Bonadei675513b2017-11-09 11:09:25 +0100758 RTC_LOG(LS_ERROR) << "Unable to assign payload type to format: "
759 << format;
ossuc54071d2016-08-17 02:45:41 -0700760 }
761
ossu9def8002017-02-09 05:14:32 -0800762 return opt_codec;
ossuc54071d2016-08-17 02:45:41 -0700763 };
764
ossud4e9f622016-08-18 02:01:17 -0700765 for (const auto& spec : specs) {
ossu9def8002017-02-09 05:14:32 -0800766 // We need to do some extra stuff before adding the main codecs to out.
767 rtc::Optional<AudioCodec> opt_codec = map_format(spec.format, nullptr);
768 if (opt_codec) {
769 AudioCodec& codec = *opt_codec;
ossua1a040a2017-04-06 10:03:21 -0700770 if (spec.info.supports_network_adaption) {
ossu9def8002017-02-09 05:14:32 -0800771 codec.AddFeedbackParam(
772 FeedbackParam(kRtcpFbParamTransportCc, kParamValueEmpty));
773 }
774
ossua1a040a2017-04-06 10:03:21 -0700775 if (spec.info.allow_comfort_noise) {
solenberg2779bab2016-11-17 04:45:19 -0800776 // Generate a CN entry if the decoder allows it and we support the
777 // clockrate.
778 auto cn = generate_cn.find(spec.format.clockrate_hz);
779 if (cn != generate_cn.end()) {
780 cn->second = true;
781 }
782 }
783
784 // Generate a telephone-event entry if we support the clockrate.
785 auto dtmf = generate_dtmf.find(spec.format.clockrate_hz);
786 if (dtmf != generate_dtmf.end()) {
787 dtmf->second = true;
ossuc54071d2016-08-17 02:45:41 -0700788 }
ossu9def8002017-02-09 05:14:32 -0800789
790 out.push_back(codec);
ossuc54071d2016-08-17 02:45:41 -0700791 }
792 }
793
solenberg2779bab2016-11-17 04:45:19 -0800794 // Add CN codecs after "proper" audio codecs.
ossuc54071d2016-08-17 02:45:41 -0700795 for (const auto& cn : generate_cn) {
796 if (cn.second) {
ossu9def8002017-02-09 05:14:32 -0800797 map_format({kCnCodecName, cn.first, 1}, &out);
ossuc54071d2016-08-17 02:45:41 -0700798 }
799 }
800
solenberg2779bab2016-11-17 04:45:19 -0800801 // Add telephone-event codecs last.
802 for (const auto& dtmf : generate_dtmf) {
803 if (dtmf.second) {
ossu9def8002017-02-09 05:14:32 -0800804 map_format({kDtmfCodecName, dtmf.first, 1}, &out);
solenberg2779bab2016-11-17 04:45:19 -0800805 }
806 }
ossuc54071d2016-08-17 02:45:41 -0700807
808 return out;
809}
810
solenbergc96df772015-10-21 13:01:53 -0700811class WebRtcVoiceMediaChannel::WebRtcAudioSendStream
Taylor Brandstetter1a018dc2016-03-08 12:37:39 -0800812 : public AudioSource::Sink {
mallinath@webrtc.org67ee6b92014-02-03 16:57:16 +0000813 public:
minyue7a973442016-10-20 03:27:12 -0700814 WebRtcAudioSendStream(
815 int ch,
816 webrtc::AudioTransport* voe_audio_transport,
817 uint32_t ssrc,
818 const std::string& c_name,
Alex Narestb3944f02017-10-13 14:56:18 +0200819 const std::string track_id,
ossu20a4b3f2017-04-27 02:08:52 -0700820 const rtc::Optional<webrtc::AudioSendStream::Config::SendCodecSpec>&
821 send_codec_spec,
minyue7a973442016-10-20 03:27:12 -0700822 const std::vector<webrtc::RtpExtension>& extensions,
823 int max_send_bitrate_bps,
minyue6b825df2016-10-31 04:08:32 -0700824 const rtc::Optional<std::string>& audio_network_adaptor_config,
minyue7a973442016-10-20 03:27:12 -0700825 webrtc::Call* call,
ossu20a4b3f2017-04-27 02:08:52 -0700826 webrtc::Transport* send_transport,
827 const rtc::scoped_refptr<webrtc::AudioEncoderFactory>& encoder_factory)
solenberg7add0582015-11-20 09:59:34 -0800828 : voe_audio_transport_(voe_audio_transport),
solenberg3a941542015-11-16 07:34:50 -0800829 call_(call),
mflodman3d7db262016-04-29 00:57:13 -0700830 config_(send_transport),
sprangc1b57a12017-02-28 08:50:47 -0800831 send_side_bwe_with_overhead_(
832 webrtc::field_trial::IsEnabled("WebRTC-SendSideBwe-WithOverhead")),
minyue7a973442016-10-20 03:27:12 -0700833 max_send_bitrate_bps_(max_send_bitrate_bps),
skvlade0d46372016-04-07 22:59:22 -0700834 rtp_parameters_(CreateRtpParametersWithOneEncoding()) {
solenberg85a04962015-10-27 03:35:21 -0700835 RTC_DCHECK_GE(ch, 0);
836 // TODO(solenberg): Once we're not using FakeWebRtcVoiceEngine anymore:
837 // RTC_DCHECK(voe_audio_transport);
solenbergc96df772015-10-21 13:01:53 -0700838 RTC_DCHECK(call);
ossu20a4b3f2017-04-27 02:08:52 -0700839 RTC_DCHECK(encoder_factory);
solenberg3a941542015-11-16 07:34:50 -0800840 config_.rtp.ssrc = ssrc;
841 config_.rtp.c_name = c_name;
842 config_.voe_channel_id = ch;
solenberg971cab02016-06-14 10:02:41 -0700843 config_.rtp.extensions = extensions;
minyue6b825df2016-10-31 04:08:32 -0700844 config_.audio_network_adaptor_config = audio_network_adaptor_config;
ossu20a4b3f2017-04-27 02:08:52 -0700845 config_.encoder_factory = encoder_factory;
Alex Narestb3944f02017-10-13 14:56:18 +0200846 config_.track_id = track_id;
Oskar Sundbom78807582017-11-16 11:09:55 +0100847 rtp_parameters_.encodings[0].ssrc = ssrc;
ossu20a4b3f2017-04-27 02:08:52 -0700848
849 if (send_codec_spec) {
850 UpdateSendCodecSpec(*send_codec_spec);
851 }
852
853 stream_ = call_->CreateAudioSendStream(config_);
solenbergc96df772015-10-21 13:01:53 -0700854 }
solenberg3a941542015-11-16 07:34:50 -0800855
solenbergc96df772015-10-21 13:01:53 -0700856 ~WebRtcAudioSendStream() override {
solenberg566ef242015-11-06 15:34:49 -0800857 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
Taylor Brandstetter1a018dc2016-03-08 12:37:39 -0800858 ClearSource();
solenbergc96df772015-10-21 13:01:53 -0700859 call_->DestroyAudioSendStream(stream_);
860 }
henrike@webrtc.org1e09a712013-07-26 19:17:59 +0000861
ossu20a4b3f2017-04-27 02:08:52 -0700862 void SetSendCodecSpec(
minyue7a973442016-10-20 03:27:12 -0700863 const webrtc::AudioSendStream::Config::SendCodecSpec& send_codec_spec) {
ossu20a4b3f2017-04-27 02:08:52 -0700864 UpdateSendCodecSpec(send_codec_spec);
865 ReconfigureAudioSendStream();
solenberg971cab02016-06-14 10:02:41 -0700866 }
867
ossu20a4b3f2017-04-27 02:08:52 -0700868 void SetRtpExtensions(const std::vector<webrtc::RtpExtension>& extensions) {
solenberg3a941542015-11-16 07:34:50 -0800869 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
solenberg3a941542015-11-16 07:34:50 -0800870 config_.rtp.extensions = extensions;
ossu20a4b3f2017-04-27 02:08:52 -0700871 ReconfigureAudioSendStream();
solenberg3a941542015-11-16 07:34:50 -0800872 }
873
ossu20a4b3f2017-04-27 02:08:52 -0700874 void SetAudioNetworkAdaptorConfig(
minyue6b825df2016-10-31 04:08:32 -0700875 const rtc::Optional<std::string>& audio_network_adaptor_config) {
876 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
877 if (config_.audio_network_adaptor_config == audio_network_adaptor_config) {
878 return;
879 }
880 config_.audio_network_adaptor_config = audio_network_adaptor_config;
ossu20a4b3f2017-04-27 02:08:52 -0700881 UpdateAllowedBitrateRange();
882 ReconfigureAudioSendStream();
minyue6b825df2016-10-31 04:08:32 -0700883 }
884
minyue7a973442016-10-20 03:27:12 -0700885 bool SetMaxSendBitrate(int bps) {
886 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
ossu20a4b3f2017-04-27 02:08:52 -0700887 RTC_DCHECK(config_.send_codec_spec);
888 RTC_DCHECK(audio_codec_spec_);
889 auto send_rate = ComputeSendBitrate(
890 bps, rtp_parameters_.encodings[0].max_bitrate_bps, *audio_codec_spec_);
891
minyue7a973442016-10-20 03:27:12 -0700892 if (!send_rate) {
893 return false;
894 }
895
896 max_send_bitrate_bps_ = bps;
897
ossu20a4b3f2017-04-27 02:08:52 -0700898 if (send_rate != config_.send_codec_spec->target_bitrate_bps) {
899 config_.send_codec_spec->target_bitrate_bps = send_rate;
900 ReconfigureAudioSendStream();
minyue7a973442016-10-20 03:27:12 -0700901 }
902 return true;
903 }
904
solenbergffbbcac2016-11-17 05:25:37 -0800905 bool SendTelephoneEvent(int payload_type, int payload_freq, int event,
906 int duration_ms) {
Fredrik Solenbergb5727682015-12-04 15:22:19 +0100907 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
908 RTC_DCHECK(stream_);
solenbergffbbcac2016-11-17 05:25:37 -0800909 return stream_->SendTelephoneEvent(payload_type, payload_freq, event,
910 duration_ms);
Fredrik Solenbergb5727682015-12-04 15:22:19 +0100911 }
912
Taylor Brandstetter1a018dc2016-03-08 12:37:39 -0800913 void SetSend(bool send) {
914 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
915 send_ = send;
916 UpdateSendState();
917 }
918
solenberg94218532016-06-16 10:53:22 -0700919 void SetMuted(bool muted) {
920 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
921 RTC_DCHECK(stream_);
922 stream_->SetMuted(muted);
923 muted_ = muted;
924 }
925
926 bool muted() const {
927 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
928 return muted_;
929 }
930
solenberg3a941542015-11-16 07:34:50 -0800931 webrtc::AudioSendStream::Stats GetStats() const {
932 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
933 RTC_DCHECK(stream_);
934 return stream_->GetStats();
935 }
936
Taylor Brandstetter1a018dc2016-03-08 12:37:39 -0800937 // Starts the sending by setting ourselves as a sink to the AudioSource to
938 // get data callbacks.
henrike@webrtc.orga7b98182014-02-21 15:51:43 +0000939 // This method is called on the libjingle worker thread.
mallinath@webrtc.org67ee6b92014-02-03 16:57:16 +0000940 // TODO(xians): Make sure Start() is called only once.
Taylor Brandstetter1a018dc2016-03-08 12:37:39 -0800941 void SetSource(AudioSource* source) {
solenberg566ef242015-11-06 15:34:49 -0800942 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
Taylor Brandstetter1a018dc2016-03-08 12:37:39 -0800943 RTC_DCHECK(source);
944 if (source_) {
945 RTC_DCHECK(source_ == source);
mallinath@webrtc.org67ee6b92014-02-03 16:57:16 +0000946 return;
947 }
Taylor Brandstetter1a018dc2016-03-08 12:37:39 -0800948 source->SetSink(this);
949 source_ = source;
950 UpdateSendState();
mallinath@webrtc.org67ee6b92014-02-03 16:57:16 +0000951 }
952
Taylor Brandstetter1a018dc2016-03-08 12:37:39 -0800953 // Stops sending by setting the sink of the AudioSource to nullptr. No data
mallinath@webrtc.org67ee6b92014-02-03 16:57:16 +0000954 // callback will be received after this method.
henrike@webrtc.orga7b98182014-02-21 15:51:43 +0000955 // This method is called on the libjingle worker thread.
Taylor Brandstetter1a018dc2016-03-08 12:37:39 -0800956 void ClearSource() {
solenberg566ef242015-11-06 15:34:49 -0800957 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
Taylor Brandstetter1a018dc2016-03-08 12:37:39 -0800958 if (source_) {
959 source_->SetSink(nullptr);
960 source_ = nullptr;
solenberg98c68862015-10-09 03:27:14 -0700961 }
Taylor Brandstetter1a018dc2016-03-08 12:37:39 -0800962 UpdateSendState();
mallinath@webrtc.org67ee6b92014-02-03 16:57:16 +0000963 }
964
Taylor Brandstetter1a018dc2016-03-08 12:37:39 -0800965 // AudioSource::Sink implementation.
henrike@webrtc.orga7b98182014-02-21 15:51:43 +0000966 // This method is called on the audio thread.
kjellander@webrtc.org14665ff2015-03-04 12:58:35 +0000967 void OnData(const void* audio_data,
968 int bits_per_sample,
969 int sample_rate,
Peter Kasting69558702016-01-12 16:26:35 -0800970 size_t number_of_channels,
Peter Kastingdce40cf2015-08-24 14:52:23 -0700971 size_t number_of_frames) override {
solenberg347ec5c2016-09-23 04:21:47 -0700972 RTC_CHECK_RUNS_SERIALIZED(&audio_capture_race_checker_);
solenbergc96df772015-10-21 13:01:53 -0700973 RTC_DCHECK(voe_audio_transport_);
maxmorin1aee0b52016-08-15 11:46:19 -0700974 voe_audio_transport_->PushCaptureData(config_.voe_channel_id, audio_data,
975 bits_per_sample, sample_rate,
976 number_of_channels, number_of_frames);
henrike@webrtc.orga7b98182014-02-21 15:51:43 +0000977 }
978
Taylor Brandstetter1a018dc2016-03-08 12:37:39 -0800979 // Callback from the |source_| when it is going away. In case Start() has
henrike@webrtc.orga7b98182014-02-21 15:51:43 +0000980 // never been called, this callback won't be triggered.
kjellander@webrtc.org14665ff2015-03-04 12:58:35 +0000981 void OnClose() override {
solenberg566ef242015-11-06 15:34:49 -0800982 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
Taylor Brandstetter1a018dc2016-03-08 12:37:39 -0800983 // Set |source_| to nullptr to make sure no more callback will get into
984 // the source.
985 source_ = nullptr;
986 UpdateSendState();
mallinath@webrtc.org67ee6b92014-02-03 16:57:16 +0000987 }
988
989 // Accessor to the VoE channel ID.
solenberg85a04962015-10-27 03:35:21 -0700990 int channel() const {
solenberg566ef242015-11-06 15:34:49 -0800991 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
solenberg7add0582015-11-20 09:59:34 -0800992 return config_.voe_channel_id;
solenberg85a04962015-10-27 03:35:21 -0700993 }
mallinath@webrtc.org67ee6b92014-02-03 16:57:16 +0000994
skvlade0d46372016-04-07 22:59:22 -0700995 const webrtc::RtpParameters& rtp_parameters() const {
996 return rtp_parameters_;
997 }
998
deadbeeffb2aced2017-01-06 23:05:37 -0800999 bool ValidateRtpParameters(const webrtc::RtpParameters& rtp_parameters) {
1000 if (rtp_parameters.encodings.size() != 1) {
Mirko Bonadei675513b2017-11-09 11:09:25 +01001001 RTC_LOG(LS_ERROR)
deadbeeffb2aced2017-01-06 23:05:37 -08001002 << "Attempted to set RtpParameters without exactly one encoding";
1003 return false;
1004 }
1005 if (rtp_parameters.encodings[0].ssrc != rtp_parameters_.encodings[0].ssrc) {
Mirko Bonadei675513b2017-11-09 11:09:25 +01001006 RTC_LOG(LS_ERROR) << "Attempted to set RtpParameters with modified SSRC";
deadbeeffb2aced2017-01-06 23:05:37 -08001007 return false;
1008 }
1009 return true;
1010 }
1011
minyue7a973442016-10-20 03:27:12 -07001012 bool SetRtpParameters(const webrtc::RtpParameters& parameters) {
deadbeeffb2aced2017-01-06 23:05:37 -08001013 if (!ValidateRtpParameters(parameters)) {
1014 return false;
1015 }
ossu20a4b3f2017-04-27 02:08:52 -07001016
1017 rtc::Optional<int> send_rate;
1018 if (audio_codec_spec_) {
1019 send_rate = ComputeSendBitrate(max_send_bitrate_bps_,
1020 parameters.encodings[0].max_bitrate_bps,
1021 *audio_codec_spec_);
1022 if (!send_rate) {
1023 return false;
1024 }
minyue7a973442016-10-20 03:27:12 -07001025 }
1026
minyuececec102017-03-27 13:04:25 -07001027 const rtc::Optional<int> old_rtp_max_bitrate =
1028 rtp_parameters_.encodings[0].max_bitrate_bps;
1029
skvlade0d46372016-04-07 22:59:22 -07001030 rtp_parameters_ = parameters;
minyue7a973442016-10-20 03:27:12 -07001031
minyuececec102017-03-27 13:04:25 -07001032 if (rtp_parameters_.encodings[0].max_bitrate_bps != old_rtp_max_bitrate) {
ossu20a4b3f2017-04-27 02:08:52 -07001033 // Reconfigure AudioSendStream with new bit rate.
1034 if (send_rate) {
1035 config_.send_codec_spec->target_bitrate_bps = send_rate;
1036 }
1037 UpdateAllowedBitrateRange();
1038 ReconfigureAudioSendStream();
minyue7a973442016-10-20 03:27:12 -07001039 } else {
1040 // parameters.encodings[0].active could have changed.
1041 UpdateSendState();
1042 }
1043 return true;
skvlade0d46372016-04-07 22:59:22 -07001044 }
1045
mallinath@webrtc.org67ee6b92014-02-03 16:57:16 +00001046 private:
Taylor Brandstetter1a018dc2016-03-08 12:37:39 -08001047 void UpdateSendState() {
1048 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
1049 RTC_DCHECK(stream_);
Taylor Brandstetter55dd7082016-05-03 13:50:11 -07001050 RTC_DCHECK_EQ(1UL, rtp_parameters_.encodings.size());
1051 if (send_ && source_ != nullptr && rtp_parameters_.encodings[0].active) {
Taylor Brandstetter1a018dc2016-03-08 12:37:39 -08001052 stream_->Start();
1053 } else { // !send || source_ = nullptr
1054 stream_->Stop();
1055 }
1056 }
1057
ossu20a4b3f2017-04-27 02:08:52 -07001058 void UpdateAllowedBitrateRange() {
michaelt53fe19d2016-10-18 09:39:22 -07001059 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
ossu20a4b3f2017-04-27 02:08:52 -07001060 const bool is_opus =
1061 config_.send_codec_spec &&
1062 !STR_CASE_CMP(config_.send_codec_spec->format.name.c_str(),
1063 kOpusCodecName);
1064 if (is_opus && webrtc::field_trial::IsEnabled("WebRTC-Audio-SendSideBwe")) {
stefane9f36d52017-01-24 08:18:45 -08001065 config_.min_bitrate_bps = kOpusMinBitrateBps;
minyuececec102017-03-27 13:04:25 -07001066
1067 // This means that when RtpParameters is reset, we may change the
ossu20a4b3f2017-04-27 02:08:52 -07001068 // encoder's bit rate immediately (through ReconfigureAudioSendStream()),
minyuececec102017-03-27 13:04:25 -07001069 // meanwhile change the cap to the output of BWE.
1070 config_.max_bitrate_bps =
1071 rtp_parameters_.encodings[0].max_bitrate_bps
1072 ? *rtp_parameters_.encodings[0].max_bitrate_bps
1073 : kOpusBitrateFbBps;
1074
michaelt53fe19d2016-10-18 09:39:22 -07001075 // TODO(mflodman): Keep testing this and set proper values.
1076 // Note: This is an early experiment currently only supported by Opus.
elad.alon0fe12162017-01-31 05:48:37 -08001077 if (send_side_bwe_with_overhead_) {
ossu20a4b3f2017-04-27 02:08:52 -07001078 const int max_packet_size_ms =
1079 WEBRTC_OPUS_SUPPORT_120MS_PTIME ? 120 : 60;
michaelt6672b262017-01-11 10:17:59 -08001080
ossu20a4b3f2017-04-27 02:08:52 -07001081 // OverheadPerPacket = Ipv4(20B) + UDP(8B) + SRTP(10B) + RTP(12)
1082 constexpr int kOverheadPerPacket = 20 + 8 + 10 + 12;
michaelt6672b262017-01-11 10:17:59 -08001083
ossu20a4b3f2017-04-27 02:08:52 -07001084 int min_overhead_bps =
1085 kOverheadPerPacket * 8 * 1000 / max_packet_size_ms;
michaelt6672b262017-01-11 10:17:59 -08001086
ossu20a4b3f2017-04-27 02:08:52 -07001087 // We assume that |config_.max_bitrate_bps| before the next line is
1088 // a hard limit on the payload bitrate, so we add min_overhead_bps to
1089 // it to ensure that, when overhead is deducted, the payload rate
1090 // never goes beyond the limit.
1091 // Note: this also means that if a higher overhead is forced, we
1092 // cannot reach the limit.
1093 // TODO(minyue): Reconsider this when the signaling to BWE is done
1094 // through a dedicated API.
1095 config_.max_bitrate_bps += min_overhead_bps;
michaelt6672b262017-01-11 10:17:59 -08001096
ossu20a4b3f2017-04-27 02:08:52 -07001097 // In contrast to max_bitrate_bps, we let min_bitrate_bps always be
1098 // reachable.
1099 config_.min_bitrate_bps += min_overhead_bps;
michaelt6672b262017-01-11 10:17:59 -08001100 }
michaelt53fe19d2016-10-18 09:39:22 -07001101 }
ossu20a4b3f2017-04-27 02:08:52 -07001102 }
1103
1104 void UpdateSendCodecSpec(
1105 const webrtc::AudioSendStream::Config::SendCodecSpec& send_codec_spec) {
1106 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
1107 config_.rtp.nack.rtp_history_ms =
1108 send_codec_spec.nack_enabled ? kNackRtpHistoryMs : 0;
Oskar Sundbom78807582017-11-16 11:09:55 +01001109 config_.send_codec_spec = send_codec_spec;
ossu20a4b3f2017-04-27 02:08:52 -07001110 auto info =
1111 config_.encoder_factory->QueryAudioEncoder(send_codec_spec.format);
1112 RTC_DCHECK(info);
1113 // If a specific target bitrate has been set for the stream, use that as
1114 // the new default bitrate when computing send bitrate.
1115 if (send_codec_spec.target_bitrate_bps) {
1116 info->default_bitrate_bps = std::max(
1117 info->min_bitrate_bps,
1118 std::min(info->max_bitrate_bps, *send_codec_spec.target_bitrate_bps));
1119 }
1120
1121 audio_codec_spec_.emplace(
1122 webrtc::AudioCodecSpec{send_codec_spec.format, *info});
1123
1124 config_.send_codec_spec->target_bitrate_bps = ComputeSendBitrate(
1125 max_send_bitrate_bps_, rtp_parameters_.encodings[0].max_bitrate_bps,
1126 *audio_codec_spec_);
1127
1128 UpdateAllowedBitrateRange();
1129 }
1130
1131 void ReconfigureAudioSendStream() {
1132 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
1133 RTC_DCHECK(stream_);
1134 stream_->Reconfigure(config_);
michaelt53fe19d2016-10-18 09:39:22 -07001135 }
1136
solenberg566ef242015-11-06 15:34:49 -08001137 rtc::ThreadChecker worker_thread_checker_;
solenberg347ec5c2016-09-23 04:21:47 -07001138 rtc::RaceChecker audio_capture_race_checker_;
solenbergc96df772015-10-21 13:01:53 -07001139 webrtc::AudioTransport* const voe_audio_transport_ = nullptr;
1140 webrtc::Call* call_ = nullptr;
solenberg3a941542015-11-16 07:34:50 -08001141 webrtc::AudioSendStream::Config config_;
elad.alon0fe12162017-01-31 05:48:37 -08001142 const bool send_side_bwe_with_overhead_;
solenberg3a941542015-11-16 07:34:50 -08001143 // The stream is owned by WebRtcAudioSendStream and may be reallocated if
1144 // configuration changes.
solenbergc96df772015-10-21 13:01:53 -07001145 webrtc::AudioSendStream* stream_ = nullptr;
mallinath@webrtc.org67ee6b92014-02-03 16:57:16 +00001146
Taylor Brandstetter1a018dc2016-03-08 12:37:39 -08001147 // Raw pointer to AudioSource owned by LocalAudioTrackHandler.
mallinath@webrtc.org67ee6b92014-02-03 16:57:16 +00001148 // PeerConnection will make sure invalidating the pointer before the object
1149 // goes away.
Taylor Brandstetter1a018dc2016-03-08 12:37:39 -08001150 AudioSource* source_ = nullptr;
1151 bool send_ = false;
solenberg94218532016-06-16 10:53:22 -07001152 bool muted_ = false;
minyue7a973442016-10-20 03:27:12 -07001153 int max_send_bitrate_bps_;
skvlade0d46372016-04-07 22:59:22 -07001154 webrtc::RtpParameters rtp_parameters_;
ossu20a4b3f2017-04-27 02:08:52 -07001155 rtc::Optional<webrtc::AudioCodecSpec> audio_codec_spec_;
henrike@webrtc.orga7b98182014-02-21 15:51:43 +00001156
solenbergc96df772015-10-21 13:01:53 -07001157 RTC_DISALLOW_IMPLICIT_CONSTRUCTORS(WebRtcAudioSendStream);
1158};
1159
1160class WebRtcVoiceMediaChannel::WebRtcAudioReceiveStream {
1161 public:
ossu29b1a8d2016-06-13 07:34:51 -07001162 WebRtcAudioReceiveStream(
1163 int ch,
1164 uint32_t remote_ssrc,
1165 uint32_t local_ssrc,
1166 bool use_transport_cc,
solenberg8189b022016-06-14 12:13:00 -07001167 bool use_nack,
ossu29b1a8d2016-06-13 07:34:51 -07001168 const std::string& sync_group,
1169 const std::vector<webrtc::RtpExtension>& extensions,
1170 webrtc::Call* call,
1171 webrtc::Transport* rtcp_send_transport,
kwiberg1c07c702017-03-27 07:15:49 -07001172 const rtc::scoped_refptr<webrtc::AudioDecoderFactory>& decoder_factory,
1173 const std::map<int, webrtc::SdpAudioFormat>& decoder_map)
stefanba4c0e42016-02-04 04:12:24 -08001174 : call_(call), config_() {
solenberg7add0582015-11-20 09:59:34 -08001175 RTC_DCHECK_GE(ch, 0);
1176 RTC_DCHECK(call);
1177 config_.rtp.remote_ssrc = remote_ssrc;
kwibergd32bf752017-01-19 07:03:59 -08001178 config_.rtp.local_ssrc = local_ssrc;
1179 config_.rtp.transport_cc = use_transport_cc;
1180 config_.rtp.nack.rtp_history_ms = use_nack ? kNackRtpHistoryMs : 0;
1181 config_.rtp.extensions = extensions;
solenberg31fec402016-05-06 02:13:12 -07001182 config_.rtcp_send_transport = rtcp_send_transport;
solenberg7add0582015-11-20 09:59:34 -08001183 config_.voe_channel_id = ch;
1184 config_.sync_group = sync_group;
ossu29b1a8d2016-06-13 07:34:51 -07001185 config_.decoder_factory = decoder_factory;
kwiberg1c07c702017-03-27 07:15:49 -07001186 config_.decoder_map = decoder_map;
kwibergd32bf752017-01-19 07:03:59 -08001187 RecreateAudioReceiveStream();
solenberg7add0582015-11-20 09:59:34 -08001188 }
solenbergc96df772015-10-21 13:01:53 -07001189
solenberg7add0582015-11-20 09:59:34 -08001190 ~WebRtcAudioReceiveStream() {
1191 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
1192 call_->DestroyAudioReceiveStream(stream_);
1193 }
1194
solenberg4a0f7b52016-06-16 13:07:33 -07001195 void RecreateAudioReceiveStream(uint32_t local_ssrc) {
solenberg7add0582015-11-20 09:59:34 -08001196 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
kwibergd32bf752017-01-19 07:03:59 -08001197 config_.rtp.local_ssrc = local_ssrc;
1198 RecreateAudioReceiveStream();
solenberg7add0582015-11-20 09:59:34 -08001199 }
solenberg8189b022016-06-14 12:13:00 -07001200
1201 void RecreateAudioReceiveStream(bool use_transport_cc, bool use_nack) {
solenberg7add0582015-11-20 09:59:34 -08001202 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
kwibergd32bf752017-01-19 07:03:59 -08001203 config_.rtp.transport_cc = use_transport_cc;
1204 config_.rtp.nack.rtp_history_ms = use_nack ? kNackRtpHistoryMs : 0;
1205 RecreateAudioReceiveStream();
solenberg7add0582015-11-20 09:59:34 -08001206 }
1207
solenberg4a0f7b52016-06-16 13:07:33 -07001208 void RecreateAudioReceiveStream(
1209 const std::vector<webrtc::RtpExtension>& extensions) {
1210 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
kwibergd32bf752017-01-19 07:03:59 -08001211 config_.rtp.extensions = extensions;
1212 RecreateAudioReceiveStream();
1213 }
1214
deadbeefcb383672017-04-26 16:28:42 -07001215 // Set a new payload type -> decoder map.
kwibergd32bf752017-01-19 07:03:59 -08001216 void RecreateAudioReceiveStream(
1217 const std::map<int, webrtc::SdpAudioFormat>& decoder_map) {
1218 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
kwibergd32bf752017-01-19 07:03:59 -08001219 config_.decoder_map = decoder_map;
1220 RecreateAudioReceiveStream();
solenberg4a0f7b52016-06-16 13:07:33 -07001221 }
1222
solenberg4904fb62017-02-17 12:01:14 -08001223 void MaybeRecreateAudioReceiveStream(const std::string& sync_group) {
1224 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
1225 if (config_.sync_group != sync_group) {
1226 config_.sync_group = sync_group;
1227 RecreateAudioReceiveStream();
1228 }
1229 }
1230
solenberg7add0582015-11-20 09:59:34 -08001231 webrtc::AudioReceiveStream::Stats GetStats() const {
1232 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
1233 RTC_DCHECK(stream_);
1234 return stream_->GetStats();
1235 }
1236
solenberg796b8f92017-03-01 17:02:23 -08001237 int GetOutputLevel() const {
1238 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
1239 RTC_DCHECK(stream_);
1240 return stream_->GetOutputLevel();
1241 }
1242
solenberg7add0582015-11-20 09:59:34 -08001243 int channel() const {
1244 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
1245 return config_.voe_channel_id;
1246 }
solenbergc96df772015-10-21 13:01:53 -07001247
kwiberg686a8ef2016-02-26 03:00:35 -08001248 void SetRawAudioSink(std::unique_ptr<webrtc::AudioSinkInterface> sink) {
Tommif888bb52015-12-12 01:37:01 +01001249 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
kwiberg686a8ef2016-02-26 03:00:35 -08001250 stream_->SetSink(std::move(sink));
Tommif888bb52015-12-12 01:37:01 +01001251 }
1252
solenberg217fb662016-06-17 08:30:54 -07001253 void SetOutputVolume(double volume) {
1254 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
1255 stream_->SetGain(volume);
1256 }
1257
aleloi84ef6152016-08-04 05:28:21 -07001258 void SetPlayout(bool playout) {
1259 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
1260 RTC_DCHECK(stream_);
1261 if (playout) {
Mirko Bonadei675513b2017-11-09 11:09:25 +01001262 RTC_LOG(LS_INFO) << "Starting playout for channel #" << channel();
aleloi84ef6152016-08-04 05:28:21 -07001263 stream_->Start();
1264 } else {
Mirko Bonadei675513b2017-11-09 11:09:25 +01001265 RTC_LOG(LS_INFO) << "Stopping playout for channel #" << channel();
aleloi84ef6152016-08-04 05:28:21 -07001266 stream_->Stop();
1267 }
aleloi18e0b672016-10-04 02:45:47 -07001268 playout_ = playout;
aleloi84ef6152016-08-04 05:28:21 -07001269 }
1270
hbos8d609f62017-04-10 07:39:05 -07001271 std::vector<webrtc::RtpSource> GetSources() {
1272 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
1273 RTC_DCHECK(stream_);
1274 return stream_->GetSources();
1275 }
1276
solenbergc96df772015-10-21 13:01:53 -07001277 private:
kwibergd32bf752017-01-19 07:03:59 -08001278 void RecreateAudioReceiveStream() {
solenberg7add0582015-11-20 09:59:34 -08001279 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
1280 if (stream_) {
1281 call_->DestroyAudioReceiveStream(stream_);
solenberg7add0582015-11-20 09:59:34 -08001282 }
solenberg7add0582015-11-20 09:59:34 -08001283 stream_ = call_->CreateAudioReceiveStream(config_);
1284 RTC_CHECK(stream_);
aleloi18e0b672016-10-04 02:45:47 -07001285 SetPlayout(playout_);
solenberg7add0582015-11-20 09:59:34 -08001286 }
1287
1288 rtc::ThreadChecker worker_thread_checker_;
1289 webrtc::Call* call_ = nullptr;
1290 webrtc::AudioReceiveStream::Config config_;
1291 // The stream is owned by WebRtcAudioReceiveStream and may be reallocated if
1292 // configuration changes.
1293 webrtc::AudioReceiveStream* stream_ = nullptr;
aleloi18e0b672016-10-04 02:45:47 -07001294 bool playout_ = false;
solenbergc96df772015-10-21 13:01:53 -07001295
1296 RTC_DISALLOW_IMPLICIT_CONSTRUCTORS(WebRtcAudioReceiveStream);
henrike@webrtc.org1e09a712013-07-26 19:17:59 +00001297};
1298
Fredrik Solenberg709ed672015-09-15 12:26:33 +02001299WebRtcVoiceMediaChannel::WebRtcVoiceMediaChannel(WebRtcVoiceEngine* engine,
nisse51542be2016-02-12 02:27:06 -08001300 const MediaConfig& config,
Fredrik Solenbergb071a192015-09-17 16:42:56 +02001301 const AudioOptions& options,
Fredrik Solenberg709ed672015-09-15 12:26:33 +02001302 webrtc::Call* call)
nisse51542be2016-02-12 02:27:06 -08001303 : VoiceMediaChannel(config), engine_(engine), call_(call) {
Mirko Bonadei675513b2017-11-09 11:09:25 +01001304 RTC_LOG(LS_VERBOSE) << "WebRtcVoiceMediaChannel::WebRtcVoiceMediaChannel";
solenberg566ef242015-11-06 15:34:49 -08001305 RTC_DCHECK(call);
solenberg0a617e22015-10-20 15:49:38 -07001306 engine->RegisterChannel(this);
Fredrik Solenbergb071a192015-09-17 16:42:56 +02001307 SetOptions(options);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001308}
1309
1310WebRtcVoiceMediaChannel::~WebRtcVoiceMediaChannel() {
solenberg566ef242015-11-06 15:34:49 -08001311 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
Mirko Bonadei675513b2017-11-09 11:09:25 +01001312 RTC_LOG(LS_VERBOSE) << "WebRtcVoiceMediaChannel::~WebRtcVoiceMediaChannel";
solenberg7add0582015-11-20 09:59:34 -08001313 // TODO(solenberg): Should be able to delete the streams directly, without
1314 // going through RemoveNnStream(), once stream objects handle
1315 // all (de)configuration.
solenbergc96df772015-10-21 13:01:53 -07001316 while (!send_streams_.empty()) {
1317 RemoveSendStream(send_streams_.begin()->first);
solenbergd97ec302015-10-07 01:40:33 -07001318 }
solenberg7add0582015-11-20 09:59:34 -08001319 while (!recv_streams_.empty()) {
1320 RemoveRecvStream(recv_streams_.begin()->first);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001321 }
solenberg0a617e22015-10-20 15:49:38 -07001322 engine()->UnregisterChannel(this);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001323}
1324
nisse51542be2016-02-12 02:27:06 -08001325rtc::DiffServCodePoint WebRtcVoiceMediaChannel::PreferredDscp() const {
1326 return kAudioDscpValue;
1327}
1328
Peter Thatcherc2ee2c82015-08-07 16:05:34 -07001329bool WebRtcVoiceMediaChannel::SetSendParameters(
1330 const AudioSendParameters& params) {
Peter Boströmca8b4042016-03-08 14:24:13 -08001331 TRACE_EVENT0("webrtc", "WebRtcVoiceMediaChannel::SetSendParameters");
solenberg566ef242015-11-06 15:34:49 -08001332 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
Mirko Bonadei675513b2017-11-09 11:09:25 +01001333 RTC_LOG(LS_INFO) << "WebRtcVoiceMediaChannel::SetSendParameters: "
1334 << params.ToString();
Peter Thatcherc2ee2c82015-08-07 16:05:34 -07001335 // TODO(pthatcher): Refactor this to be more clean now that we have
1336 // all the information at once.
solenberg3a941542015-11-16 07:34:50 -08001337
1338 if (!SetSendCodecs(params.codecs)) {
1339 return false;
1340 }
1341
solenberg7e4e01a2015-12-02 08:05:01 -08001342 if (!ValidateRtpExtensions(params.extensions)) {
1343 return false;
1344 }
1345 std::vector<webrtc::RtpExtension> filtered_extensions =
1346 FilterRtpExtensions(params.extensions,
1347 webrtc::RtpExtension::IsSupportedForAudio, true);
1348 if (send_rtp_extensions_ != filtered_extensions) {
1349 send_rtp_extensions_.swap(filtered_extensions);
solenberg3a941542015-11-16 07:34:50 -08001350 for (auto& it : send_streams_) {
ossu20a4b3f2017-04-27 02:08:52 -07001351 it.second->SetRtpExtensions(send_rtp_extensions_);
solenberg3a941542015-11-16 07:34:50 -08001352 }
1353 }
1354
deadbeef80346142016-04-27 14:17:10 -07001355 if (!SetMaxSendBitrate(params.max_bandwidth_bps)) {
solenberg3a941542015-11-16 07:34:50 -08001356 return false;
1357 }
1358 return SetOptions(params.options);
Peter Thatcherc2ee2c82015-08-07 16:05:34 -07001359}
1360
1361bool WebRtcVoiceMediaChannel::SetRecvParameters(
1362 const AudioRecvParameters& params) {
Peter Boströmca8b4042016-03-08 14:24:13 -08001363 TRACE_EVENT0("webrtc", "WebRtcVoiceMediaChannel::SetRecvParameters");
solenberg566ef242015-11-06 15:34:49 -08001364 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
Mirko Bonadei675513b2017-11-09 11:09:25 +01001365 RTC_LOG(LS_INFO) << "WebRtcVoiceMediaChannel::SetRecvParameters: "
1366 << params.ToString();
Peter Thatcherc2ee2c82015-08-07 16:05:34 -07001367 // TODO(pthatcher): Refactor this to be more clean now that we have
1368 // all the information at once.
solenberg7add0582015-11-20 09:59:34 -08001369
1370 if (!SetRecvCodecs(params.codecs)) {
1371 return false;
1372 }
1373
solenberg7e4e01a2015-12-02 08:05:01 -08001374 if (!ValidateRtpExtensions(params.extensions)) {
1375 return false;
1376 }
1377 std::vector<webrtc::RtpExtension> filtered_extensions =
1378 FilterRtpExtensions(params.extensions,
1379 webrtc::RtpExtension::IsSupportedForAudio, false);
1380 if (recv_rtp_extensions_ != filtered_extensions) {
1381 recv_rtp_extensions_.swap(filtered_extensions);
solenberg7add0582015-11-20 09:59:34 -08001382 for (auto& it : recv_streams_) {
1383 it.second->RecreateAudioReceiveStream(recv_rtp_extensions_);
1384 }
1385 }
solenberg7add0582015-11-20 09:59:34 -08001386 return true;
Peter Thatcherc2ee2c82015-08-07 16:05:34 -07001387}
1388
Taylor Brandstetterdb0cd9e2016-05-16 11:40:30 -07001389webrtc::RtpParameters WebRtcVoiceMediaChannel::GetRtpSendParameters(
skvlade0d46372016-04-07 22:59:22 -07001390 uint32_t ssrc) const {
1391 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
1392 auto it = send_streams_.find(ssrc);
1393 if (it == send_streams_.end()) {
Mirko Bonadei675513b2017-11-09 11:09:25 +01001394 RTC_LOG(LS_WARNING) << "Attempting to get RTP send parameters for stream "
1395 << "with ssrc " << ssrc << " which doesn't exist.";
skvlade0d46372016-04-07 22:59:22 -07001396 return webrtc::RtpParameters();
1397 }
1398
Taylor Brandstetter0cd086b2016-04-20 16:23:10 -07001399 webrtc::RtpParameters rtp_params = it->second->rtp_parameters();
1400 // Need to add the common list of codecs to the send stream-specific
1401 // RTP parameters.
1402 for (const AudioCodec& codec : send_codecs_) {
1403 rtp_params.codecs.push_back(codec.ToCodecParameters());
1404 }
1405 return rtp_params;
skvlade0d46372016-04-07 22:59:22 -07001406}
1407
Taylor Brandstetterdb0cd9e2016-05-16 11:40:30 -07001408bool WebRtcVoiceMediaChannel::SetRtpSendParameters(
skvlade0d46372016-04-07 22:59:22 -07001409 uint32_t ssrc,
1410 const webrtc::RtpParameters& parameters) {
1411 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
skvlade0d46372016-04-07 22:59:22 -07001412 auto it = send_streams_.find(ssrc);
1413 if (it == send_streams_.end()) {
Mirko Bonadei675513b2017-11-09 11:09:25 +01001414 RTC_LOG(LS_WARNING) << "Attempting to set RTP send parameters for stream "
1415 << "with ssrc " << ssrc << " which doesn't exist.";
skvlade0d46372016-04-07 22:59:22 -07001416 return false;
1417 }
1418
Taylor Brandstetterdb0cd9e2016-05-16 11:40:30 -07001419 // TODO(deadbeef): Handle setting parameters with a list of codecs in a
1420 // different order (which should change the send codec).
1421 webrtc::RtpParameters current_parameters = GetRtpSendParameters(ssrc);
1422 if (current_parameters.codecs != parameters.codecs) {
Mirko Bonadei675513b2017-11-09 11:09:25 +01001423 RTC_LOG(LS_ERROR) << "Using SetParameters to change the set of codecs "
1424 << "is not currently supported.";
Taylor Brandstetterdb0cd9e2016-05-16 11:40:30 -07001425 return false;
1426 }
1427
minyue7a973442016-10-20 03:27:12 -07001428 // TODO(minyue): The following legacy actions go into
1429 // |WebRtcAudioSendStream::SetRtpParameters()| which is called at the end,
1430 // though there are two difference:
1431 // 1. |WebRtcVoiceMediaChannel::SetChannelSendParameters()| only calls
1432 // |SetSendCodec| while |WebRtcAudioSendStream::SetRtpParameters()| calls
1433 // |SetSendCodecs|. The outcome should be the same.
1434 // 2. AudioSendStream can be recreated.
1435
Taylor Brandstetter0cd086b2016-04-20 16:23:10 -07001436 // Codecs are handled at the WebRtcVoiceMediaChannel level.
1437 webrtc::RtpParameters reduced_params = parameters;
1438 reduced_params.codecs.clear();
minyue7a973442016-10-20 03:27:12 -07001439 return it->second->SetRtpParameters(reduced_params);
skvlade0d46372016-04-07 22:59:22 -07001440}
1441
Taylor Brandstetterdb0cd9e2016-05-16 11:40:30 -07001442webrtc::RtpParameters WebRtcVoiceMediaChannel::GetRtpReceiveParameters(
1443 uint32_t ssrc) const {
1444 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
deadbeef3bc15102017-04-20 19:25:07 -07001445 webrtc::RtpParameters rtp_params;
1446 // SSRC of 0 represents the default receive stream.
1447 if (ssrc == 0) {
1448 if (!default_sink_) {
Mirko Bonadei675513b2017-11-09 11:09:25 +01001449 RTC_LOG(LS_WARNING)
1450 << "Attempting to get RTP parameters for the default, "
1451 "unsignaled audio receive stream, but not yet "
1452 "configured to receive such a stream.";
deadbeef3bc15102017-04-20 19:25:07 -07001453 return rtp_params;
1454 }
1455 rtp_params.encodings.emplace_back();
1456 } else {
1457 auto it = recv_streams_.find(ssrc);
1458 if (it == recv_streams_.end()) {
Mirko Bonadei675513b2017-11-09 11:09:25 +01001459 RTC_LOG(LS_WARNING)
1460 << "Attempting to get RTP receive parameters for stream "
1461 << "with ssrc " << ssrc << " which doesn't exist.";
deadbeef3bc15102017-04-20 19:25:07 -07001462 return webrtc::RtpParameters();
1463 }
1464 rtp_params.encodings.emplace_back();
1465 // TODO(deadbeef): Return stream-specific parameters.
Oskar Sundbom78807582017-11-16 11:09:55 +01001466 rtp_params.encodings[0].ssrc = ssrc;
Taylor Brandstetterdb0cd9e2016-05-16 11:40:30 -07001467 }
1468
Taylor Brandstetterdb0cd9e2016-05-16 11:40:30 -07001469 for (const AudioCodec& codec : recv_codecs_) {
1470 rtp_params.codecs.push_back(codec.ToCodecParameters());
1471 }
1472 return rtp_params;
1473}
1474
1475bool WebRtcVoiceMediaChannel::SetRtpReceiveParameters(
1476 uint32_t ssrc,
1477 const webrtc::RtpParameters& parameters) {
1478 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
deadbeef3bc15102017-04-20 19:25:07 -07001479 // SSRC of 0 represents the default receive stream.
1480 if (ssrc == 0) {
1481 if (!default_sink_) {
Mirko Bonadei675513b2017-11-09 11:09:25 +01001482 RTC_LOG(LS_WARNING)
1483 << "Attempting to set RTP parameters for the default, "
1484 "unsignaled audio receive stream, but not yet "
1485 "configured to receive such a stream.";
deadbeef3bc15102017-04-20 19:25:07 -07001486 return false;
1487 }
1488 } else {
1489 auto it = recv_streams_.find(ssrc);
1490 if (it == recv_streams_.end()) {
Mirko Bonadei675513b2017-11-09 11:09:25 +01001491 RTC_LOG(LS_WARNING)
1492 << "Attempting to set RTP receive parameters for stream "
1493 << "with ssrc " << ssrc << " which doesn't exist.";
deadbeef3bc15102017-04-20 19:25:07 -07001494 return false;
1495 }
Taylor Brandstetterdb0cd9e2016-05-16 11:40:30 -07001496 }
1497
1498 webrtc::RtpParameters current_parameters = GetRtpReceiveParameters(ssrc);
1499 if (current_parameters != parameters) {
Mirko Bonadei675513b2017-11-09 11:09:25 +01001500 RTC_LOG(LS_ERROR) << "Changing the RTP receive parameters is currently "
1501 << "unsupported.";
Taylor Brandstetterdb0cd9e2016-05-16 11:40:30 -07001502 return false;
1503 }
1504 return true;
1505}
1506
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001507bool WebRtcVoiceMediaChannel::SetOptions(const AudioOptions& options) {
solenberg566ef242015-11-06 15:34:49 -08001508 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
Mirko Bonadei675513b2017-11-09 11:09:25 +01001509 RTC_LOG(LS_INFO) << "Setting voice channel options: " << options.ToString();
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001510
1511 // We retain all of the existing options, and apply the given ones
1512 // on top. This means there is no way to "clear" options such that
1513 // they go back to the engine default.
1514 options_.SetAll(options);
solenberg246b8172015-12-08 09:50:23 -08001515 if (!engine()->ApplyOptions(options_)) {
Mirko Bonadei675513b2017-11-09 11:09:25 +01001516 RTC_LOG(LS_WARNING)
1517 << "Failed to apply engine options during channel SetOptions.";
solenberg246b8172015-12-08 09:50:23 -08001518 return false;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001519 }
minyue6b825df2016-10-31 04:08:32 -07001520
ossu20a4b3f2017-04-27 02:08:52 -07001521 rtc::Optional<std::string> audio_network_adaptor_config =
minyue6b825df2016-10-31 04:08:32 -07001522 GetAudioNetworkAdaptorConfig(options_);
1523 for (auto& it : send_streams_) {
ossu20a4b3f2017-04-27 02:08:52 -07001524 it.second->SetAudioNetworkAdaptorConfig(audio_network_adaptor_config);
minyue6b825df2016-10-31 04:08:32 -07001525 }
1526
Mirko Bonadei675513b2017-11-09 11:09:25 +01001527 RTC_LOG(LS_INFO) << "Set voice channel options. Current options: "
1528 << options_.ToString();
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001529 return true;
1530}
1531
1532bool WebRtcVoiceMediaChannel::SetRecvCodecs(
1533 const std::vector<AudioCodec>& codecs) {
solenberg566ef242015-11-06 15:34:49 -08001534 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
solenberg8fb30c32015-10-13 03:06:58 -07001535
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001536 // Set the payload types to be used for incoming media.
Mirko Bonadei675513b2017-11-09 11:09:25 +01001537 RTC_LOG(LS_INFO) << "Setting receive voice codecs.";
solenberg0b675462015-10-09 01:37:09 -07001538
1539 if (!VerifyUniquePayloadTypes(codecs)) {
Mirko Bonadei675513b2017-11-09 11:09:25 +01001540 RTC_LOG(LS_ERROR) << "Codec payload types overlap.";
solenberg0b675462015-10-09 01:37:09 -07001541 return false;
1542 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001543
kwibergd32bf752017-01-19 07:03:59 -08001544 // Create a payload type -> SdpAudioFormat map with all the decoders. Fail
1545 // unless the factory claims to support all decoders.
1546 std::map<int, webrtc::SdpAudioFormat> decoder_map;
1547 for (const AudioCodec& codec : codecs) {
deadbeefcb383672017-04-26 16:28:42 -07001548 // Log a warning if a codec's payload type is changing. This used to be
1549 // treated as an error. It's abnormal, but not really illegal.
1550 AudioCodec old_codec;
1551 if (FindCodec(recv_codecs_, codec, &old_codec) &&
1552 old_codec.id != codec.id) {
Mirko Bonadei675513b2017-11-09 11:09:25 +01001553 RTC_LOG(LS_WARNING) << codec.name << " mapped to a second payload type ("
1554 << codec.id << ", was already mapped to "
1555 << old_codec.id << ")";
deadbeefcb383672017-04-26 16:28:42 -07001556 }
kwibergd32bf752017-01-19 07:03:59 -08001557 auto format = AudioCodecToSdpAudioFormat(codec);
1558 if (!IsCodec(codec, "cn") && !IsCodec(codec, "telephone-event") &&
1559 !engine()->decoder_factory_->IsSupportedDecoder(format)) {
Mirko Bonadei675513b2017-11-09 11:09:25 +01001560 RTC_LOG(LS_ERROR) << "Unsupported codec: " << format;
kwibergd32bf752017-01-19 07:03:59 -08001561 return false;
1562 }
deadbeefcb383672017-04-26 16:28:42 -07001563 // We allow adding new codecs but don't allow changing the payload type of
1564 // codecs that are already configured since we might already be receiving
1565 // packets with that payload type. See RFC3264, Section 8.3.2.
1566 // TODO(deadbeef): Also need to check for clashes with previously mapped
1567 // payload types, and not just currently mapped ones. For example, this
1568 // should be illegal:
1569 // 1. {100: opus/48000/2, 101: ISAC/16000}
1570 // 2. {100: opus/48000/2}
1571 // 3. {100: opus/48000/2, 101: ISAC/32000}
1572 // Though this check really should happen at a higher level, since this
1573 // conflict could happen between audio and video codecs.
1574 auto existing = decoder_map_.find(codec.id);
1575 if (existing != decoder_map_.end() && !existing->second.Matches(format)) {
Mirko Bonadei675513b2017-11-09 11:09:25 +01001576 RTC_LOG(LS_ERROR) << "Attempting to use payload type " << codec.id
1577 << " for " << codec.name
1578 << ", but it is already used for "
1579 << existing->second.name;
deadbeefcb383672017-04-26 16:28:42 -07001580 return false;
1581 }
kwibergd32bf752017-01-19 07:03:59 -08001582 decoder_map.insert({codec.id, std::move(format)});
1583 }
1584
deadbeefcb383672017-04-26 16:28:42 -07001585 if (decoder_map == decoder_map_) {
1586 // There's nothing new to configure.
1587 return true;
1588 }
1589
kwiberg37b8b112016-11-03 02:46:53 -07001590 if (playout_) {
1591 // Receive codecs can not be changed while playing. So we temporarily
1592 // pause playout.
1593 ChangePlayout(false);
1594 }
1595
kwiberg1c07c702017-03-27 07:15:49 -07001596 decoder_map_ = std::move(decoder_map);
kwibergd32bf752017-01-19 07:03:59 -08001597 for (auto& kv : recv_streams_) {
kwiberg1c07c702017-03-27 07:15:49 -07001598 kv.second->RecreateAudioReceiveStream(decoder_map_);
solenberg26c8c912015-11-27 04:00:25 -08001599 }
kwibergd32bf752017-01-19 07:03:59 -08001600 recv_codecs_ = codecs;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001601
kwiberg37b8b112016-11-03 02:46:53 -07001602 if (desired_playout_ && !playout_) {
1603 ChangePlayout(desired_playout_);
1604 }
kwibergd32bf752017-01-19 07:03:59 -08001605 return true;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001606}
1607
solenberg72e29d22016-03-08 06:35:16 -08001608// Utility function called from SetSendParameters() to extract current send
1609// codec settings from the given list of codecs (originally from SDP). Both send
1610// and receive streams may be reconfigured based on the new settings.
wu@webrtc.orgcadf9042013-08-30 21:24:16 +00001611bool WebRtcVoiceMediaChannel::SetSendCodecs(
1612 const std::vector<AudioCodec>& codecs) {
solenberg566ef242015-11-06 15:34:49 -08001613 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
Oskar Sundbom78807582017-11-16 11:09:55 +01001614 dtmf_payload_type_ = rtc::nullopt;
solenbergffbbcac2016-11-17 05:25:37 -08001615 dtmf_payload_freq_ = -1;
1616
1617 // Validate supplied codecs list.
1618 for (const AudioCodec& codec : codecs) {
1619 // TODO(solenberg): Validate more aspects of input - that payload types
1620 // don't overlap, remove redundant/unsupported codecs etc -
1621 // the same way it is done for RtpHeaderExtensions.
1622 if (codec.id < kMinPayloadType || codec.id > kMaxPayloadType) {
Mirko Bonadei675513b2017-11-09 11:09:25 +01001623 RTC_LOG(LS_WARNING) << "Codec payload type out of range: "
1624 << ToString(codec);
solenbergffbbcac2016-11-17 05:25:37 -08001625 return false;
1626 }
1627 }
1628
1629 // Find PT of telephone-event codec with lowest clockrate, as a fallback, in
1630 // case we don't have a DTMF codec with a rate matching the send codec's, or
1631 // if this function returns early.
1632 std::vector<AudioCodec> dtmf_codecs;
Fredrik Solenbergaf9fb212015-08-26 10:45:53 +02001633 for (const AudioCodec& codec : codecs) {
Fredrik Solenbergaf9fb212015-08-26 10:45:53 +02001634 if (IsCodec(codec, kDtmfCodecName)) {
solenbergffbbcac2016-11-17 05:25:37 -08001635 dtmf_codecs.push_back(codec);
1636 if (!dtmf_payload_type_ || codec.clockrate < dtmf_payload_freq_) {
Oskar Sundbom78807582017-11-16 11:09:55 +01001637 dtmf_payload_type_ = codec.id;
solenbergffbbcac2016-11-17 05:25:37 -08001638 dtmf_payload_freq_ = codec.clockrate;
solenberg31642aa2016-03-14 08:00:37 -07001639 }
wu@webrtc.orgcadf9042013-08-30 21:24:16 +00001640 }
1641 }
1642
ossu20a4b3f2017-04-27 02:08:52 -07001643 // Scan through the list to figure out the codec to use for sending.
1644 rtc::Optional<webrtc::AudioSendStream::Config::SendCodecSpec> send_codec_spec;
stefan1ccf73f2017-03-27 03:51:18 -07001645 webrtc::Call::Config::BitrateConfig bitrate_config;
ossu20a4b3f2017-04-27 02:08:52 -07001646 rtc::Optional<webrtc::AudioCodecInfo> voice_codec_info;
1647 for (const AudioCodec& voice_codec : codecs) {
1648 if (!(IsCodec(voice_codec, kCnCodecName) ||
1649 IsCodec(voice_codec, kDtmfCodecName) ||
1650 IsCodec(voice_codec, kRedCodecName))) {
1651 webrtc::SdpAudioFormat format(voice_codec.name, voice_codec.clockrate,
1652 voice_codec.channels, voice_codec.params);
solenberg72e29d22016-03-08 06:35:16 -08001653
ossu20a4b3f2017-04-27 02:08:52 -07001654 voice_codec_info = engine()->encoder_factory_->QueryAudioEncoder(format);
1655 if (!voice_codec_info) {
Mirko Bonadei675513b2017-11-09 11:09:25 +01001656 RTC_LOG(LS_WARNING) << "Unknown codec " << ToString(voice_codec);
solenberg72e29d22016-03-08 06:35:16 -08001657 continue;
1658 }
1659
Oskar Sundbom78807582017-11-16 11:09:55 +01001660 send_codec_spec = webrtc::AudioSendStream::Config::SendCodecSpec(
1661 voice_codec.id, format);
ossu20a4b3f2017-04-27 02:08:52 -07001662 if (voice_codec.bitrate > 0) {
Oskar Sundbom78807582017-11-16 11:09:55 +01001663 send_codec_spec->target_bitrate_bps = voice_codec.bitrate;
ossu20a4b3f2017-04-27 02:08:52 -07001664 }
1665 send_codec_spec->transport_cc_enabled = HasTransportCc(voice_codec);
1666 send_codec_spec->nack_enabled = HasNack(voice_codec);
1667 bitrate_config = GetBitrateConfigForCodec(voice_codec);
1668 break;
1669 }
1670 }
1671
1672 if (!send_codec_spec) {
1673 return false;
1674 }
1675
1676 RTC_DCHECK(voice_codec_info);
1677 if (voice_codec_info->allow_comfort_noise) {
1678 // Loop through the codecs list again to find the CN codec.
1679 // TODO(solenberg): Break out into a separate function?
1680 for (const AudioCodec& cn_codec : codecs) {
ossu0c4b8492017-03-02 11:03:25 -08001681 if (IsCodec(cn_codec, kCnCodecName) &&
ossu20a4b3f2017-04-27 02:08:52 -07001682 cn_codec.clockrate == send_codec_spec->format.clockrate_hz) {
ossu0c4b8492017-03-02 11:03:25 -08001683 switch (cn_codec.clockrate) {
solenberg72e29d22016-03-08 06:35:16 -08001684 case 8000:
1685 case 16000:
1686 case 32000:
Oskar Sundbom78807582017-11-16 11:09:55 +01001687 send_codec_spec->cng_payload_type = cn_codec.id;
solenberg72e29d22016-03-08 06:35:16 -08001688 break;
1689 default:
Mirko Bonadei675513b2017-11-09 11:09:25 +01001690 RTC_LOG(LS_WARNING)
1691 << "CN frequency " << cn_codec.clockrate << " not supported.";
ossu20a4b3f2017-04-27 02:08:52 -07001692 break;
solenberg72e29d22016-03-08 06:35:16 -08001693 }
solenberg72e29d22016-03-08 06:35:16 -08001694 break;
1695 }
1696 }
solenbergffbbcac2016-11-17 05:25:37 -08001697
1698 // Find the telephone-event PT exactly matching the preferred send codec.
1699 for (const AudioCodec& dtmf_codec : dtmf_codecs) {
ossu20a4b3f2017-04-27 02:08:52 -07001700 if (dtmf_codec.clockrate == send_codec_spec->format.clockrate_hz) {
Oskar Sundbom78807582017-11-16 11:09:55 +01001701 dtmf_payload_type_ = dtmf_codec.id;
solenbergffbbcac2016-11-17 05:25:37 -08001702 dtmf_payload_freq_ = dtmf_codec.clockrate;
1703 break;
1704 }
1705 }
solenberg72e29d22016-03-08 06:35:16 -08001706 }
1707
solenberg971cab02016-06-14 10:02:41 -07001708 if (send_codec_spec_ != send_codec_spec) {
1709 send_codec_spec_ = std::move(send_codec_spec);
stefan13f1a0a2016-11-30 07:22:58 -08001710 // Apply new settings to all streams.
solenberg971cab02016-06-14 10:02:41 -07001711 for (const auto& kv : send_streams_) {
ossu20a4b3f2017-04-27 02:08:52 -07001712 kv.second->SetSendCodecSpec(*send_codec_spec_);
wu@webrtc.orgcadf9042013-08-30 21:24:16 +00001713 }
stefan13f1a0a2016-11-30 07:22:58 -08001714 } else {
1715 // If the codec isn't changing, set the start bitrate to -1 which means
1716 // "unchanged" so that BWE isn't affected.
stefan1ccf73f2017-03-27 03:51:18 -07001717 bitrate_config.start_bitrate_bps = -1;
wu@webrtc.orgcadf9042013-08-30 21:24:16 +00001718 }
stefan1ccf73f2017-03-27 03:51:18 -07001719 call_->SetBitrateConfig(bitrate_config);
wu@webrtc.orgcadf9042013-08-30 21:24:16 +00001720
solenberg8189b022016-06-14 12:13:00 -07001721 // Check if the transport cc feedback or NACK status has changed on the
1722 // preferred send codec, and in that case reconfigure all receive streams.
ossu20a4b3f2017-04-27 02:08:52 -07001723 if (recv_transport_cc_enabled_ != send_codec_spec_->transport_cc_enabled ||
1724 recv_nack_enabled_ != send_codec_spec_->nack_enabled) {
Mirko Bonadei675513b2017-11-09 11:09:25 +01001725 RTC_LOG(LS_INFO) << "Recreate all the receive streams because the send "
1726 "codec has changed.";
ossu20a4b3f2017-04-27 02:08:52 -07001727 recv_transport_cc_enabled_ = send_codec_spec_->transport_cc_enabled;
1728 recv_nack_enabled_ = send_codec_spec_->nack_enabled;
solenberg72e29d22016-03-08 06:35:16 -08001729 for (auto& kv : recv_streams_) {
solenberg8189b022016-06-14 12:13:00 -07001730 kv.second->RecreateAudioReceiveStream(recv_transport_cc_enabled_,
1731 recv_nack_enabled_);
solenberg72e29d22016-03-08 06:35:16 -08001732 }
1733 }
1734
Taylor Brandstetter0cd086b2016-04-20 16:23:10 -07001735 send_codecs_ = codecs;
solenberg72e29d22016-03-08 06:35:16 -08001736 return true;
1737}
1738
aleloi84ef6152016-08-04 05:28:21 -07001739void WebRtcVoiceMediaChannel::SetPlayout(bool playout) {
kwiberg37b8b112016-11-03 02:46:53 -07001740 desired_playout_ = playout;
1741 return ChangePlayout(desired_playout_);
1742}
1743
1744void WebRtcVoiceMediaChannel::ChangePlayout(bool playout) {
1745 TRACE_EVENT0("webrtc", "WebRtcVoiceMediaChannel::ChangePlayout");
solenberg566ef242015-11-06 15:34:49 -08001746 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001747 if (playout_ == playout) {
aleloi84ef6152016-08-04 05:28:21 -07001748 return;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001749 }
1750
aleloi84ef6152016-08-04 05:28:21 -07001751 for (const auto& kv : recv_streams_) {
1752 kv.second->SetPlayout(playout);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001753 }
solenberg1ac56142015-10-13 03:58:19 -07001754 playout_ = playout;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001755}
1756
Taylor Brandstetter1a018dc2016-03-08 12:37:39 -08001757void WebRtcVoiceMediaChannel::SetSend(bool send) {
Peter Boströmca8b4042016-03-08 14:24:13 -08001758 TRACE_EVENT0("webrtc", "WebRtcVoiceMediaChannel::SetSend");
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001759 if (send_ == send) {
Taylor Brandstetter1a018dc2016-03-08 12:37:39 -08001760 return;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001761 }
1762
solenbergd53a3f92016-04-14 13:56:37 -07001763 // Apply channel specific options, and initialize the ADM for recording (this
1764 // may take time on some platforms, e.g. Android).
Taylor Brandstetter1a018dc2016-03-08 12:37:39 -08001765 if (send) {
solenberg63b34542015-09-29 06:06:31 -07001766 engine()->ApplyOptions(options_);
solenbergd53a3f92016-04-14 13:56:37 -07001767
1768 // InitRecording() may return an error if the ADM is already recording.
1769 if (!engine()->adm()->RecordingIsInitialized() &&
1770 !engine()->adm()->Recording()) {
1771 if (engine()->adm()->InitRecording() != 0) {
Mirko Bonadei675513b2017-11-09 11:09:25 +01001772 RTC_LOG(LS_WARNING) << "Failed to initialize recording";
solenbergd53a3f92016-04-14 13:56:37 -07001773 }
1774 }
solenberg63b34542015-09-29 06:06:31 -07001775 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001776
wu@webrtc.org9dba5252013-08-05 20:36:57 +00001777 // Change the settings on each send channel.
Taylor Brandstetter1a018dc2016-03-08 12:37:39 -08001778 for (auto& kv : send_streams_) {
1779 kv.second->SetSend(send);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001780 }
wu@webrtc.org9dba5252013-08-05 20:36:57 +00001781
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001782 send_ = send;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001783}
1784
Peter Boström0c4e06b2015-10-07 12:23:21 +02001785bool WebRtcVoiceMediaChannel::SetAudioSend(uint32_t ssrc,
1786 bool enable,
solenberg1dd98f32015-09-10 01:57:14 -07001787 const AudioOptions* options,
Taylor Brandstetter1a018dc2016-03-08 12:37:39 -08001788 AudioSource* source) {
solenberg566ef242015-11-06 15:34:49 -08001789 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
solenberg1dd98f32015-09-10 01:57:14 -07001790 // TODO(solenberg): The state change should be fully rolled back if any one of
1791 // these calls fail.
Taylor Brandstetter1a018dc2016-03-08 12:37:39 -08001792 if (!SetLocalSource(ssrc, source)) {
solenberg1dd98f32015-09-10 01:57:14 -07001793 return false;
1794 }
solenbergdfc8f4f2015-10-01 02:31:10 -07001795 if (!MuteStream(ssrc, !enable)) {
solenberg1dd98f32015-09-10 01:57:14 -07001796 return false;
1797 }
solenbergdfc8f4f2015-10-01 02:31:10 -07001798 if (enable && options) {
solenberg1dd98f32015-09-10 01:57:14 -07001799 return SetOptions(*options);
1800 }
1801 return true;
1802}
1803
solenberg0a617e22015-10-20 15:49:38 -07001804int WebRtcVoiceMediaChannel::CreateVoEChannel() {
1805 int id = engine()->CreateVoEChannel();
1806 if (id == -1) {
Mirko Bonadei675513b2017-11-09 11:09:25 +01001807 RTC_LOG(LS_WARNING) << "CreateVoEChannel() failed.";
solenberg0a617e22015-10-20 15:49:38 -07001808 return -1;
wu@webrtc.org9dba5252013-08-05 20:36:57 +00001809 }
mflodman3d7db262016-04-29 00:57:13 -07001810
solenberg0a617e22015-10-20 15:49:38 -07001811 return id;
wu@webrtc.org9dba5252013-08-05 20:36:57 +00001812}
1813
solenberg7add0582015-11-20 09:59:34 -08001814bool WebRtcVoiceMediaChannel::DeleteVoEChannel(int channel) {
wu@webrtc.org9dba5252013-08-05 20:36:57 +00001815 if (engine()->voe()->base()->DeleteChannel(channel) == -1) {
Mirko Bonadei675513b2017-11-09 11:09:25 +01001816 RTC_LOG(LS_WARNING) << "DeleteChannel(" << channel << ") failed.";
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001817 return false;
1818 }
wu@webrtc.org9dba5252013-08-05 20:36:57 +00001819 return true;
1820}
henrike@webrtc.org1e09a712013-07-26 19:17:59 +00001821
wu@webrtc.org9dba5252013-08-05 20:36:57 +00001822bool WebRtcVoiceMediaChannel::AddSendStream(const StreamParams& sp) {
Peter Boströmca8b4042016-03-08 14:24:13 -08001823 TRACE_EVENT0("webrtc", "WebRtcVoiceMediaChannel::AddSendStream");
solenberg566ef242015-11-06 15:34:49 -08001824 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
Mirko Bonadei675513b2017-11-09 11:09:25 +01001825 RTC_LOG(LS_INFO) << "AddSendStream: " << sp.ToString();
solenberg0a617e22015-10-20 15:49:38 -07001826
1827 uint32_t ssrc = sp.first_ssrc();
1828 RTC_DCHECK(0 != ssrc);
1829
1830 if (GetSendChannelId(ssrc) != -1) {
Mirko Bonadei675513b2017-11-09 11:09:25 +01001831 RTC_LOG(LS_ERROR) << "Stream already exists with ssrc " << ssrc;
wu@webrtc.org9dba5252013-08-05 20:36:57 +00001832 return false;
1833 }
1834
solenberg0a617e22015-10-20 15:49:38 -07001835 // Create a new channel for sending audio data.
1836 int channel = CreateVoEChannel();
1837 if (channel == -1) {
1838 return false;
wu@webrtc.org9dba5252013-08-05 20:36:57 +00001839 }
wu@webrtc.org9dba5252013-08-05 20:36:57 +00001840
solenbergc96df772015-10-21 13:01:53 -07001841 // Save the channel to send_streams_, so that RemoveSendStream() can still
wu@webrtc.org9dba5252013-08-05 20:36:57 +00001842 // delete the channel in case failure happens below.
mallinath@webrtc.org67ee6b92014-02-03 16:57:16 +00001843 webrtc::AudioTransport* audio_transport =
1844 engine()->voe()->base()->audio_transport();
mflodman3d7db262016-04-29 00:57:13 -07001845
minyue6b825df2016-10-31 04:08:32 -07001846 rtc::Optional<std::string> audio_network_adaptor_config =
1847 GetAudioNetworkAdaptorConfig(options_);
skvlade0d46372016-04-07 22:59:22 -07001848 WebRtcAudioSendStream* stream = new WebRtcAudioSendStream(
Alex Narestb3944f02017-10-13 14:56:18 +02001849 channel, audio_transport, ssrc, sp.cname, sp.id, send_codec_spec_,
minyue6b825df2016-10-31 04:08:32 -07001850 send_rtp_extensions_, max_send_bitrate_bps_, audio_network_adaptor_config,
ossu20a4b3f2017-04-27 02:08:52 -07001851 call_, this, engine()->encoder_factory_);
skvlade0d46372016-04-07 22:59:22 -07001852 send_streams_.insert(std::make_pair(ssrc, stream));
wu@webrtc.org9dba5252013-08-05 20:36:57 +00001853
solenberg4a0f7b52016-06-16 13:07:33 -07001854 // At this point the stream's local SSRC has been updated. If it is the first
1855 // send stream, make sure that all the receive streams are updated with the
1856 // same SSRC in order to send receiver reports.
solenbergc96df772015-10-21 13:01:53 -07001857 if (send_streams_.size() == 1) {
solenberg0a617e22015-10-20 15:49:38 -07001858 receiver_reports_ssrc_ = ssrc;
solenberg4a0f7b52016-06-16 13:07:33 -07001859 for (const auto& kv : recv_streams_) {
1860 // TODO(solenberg): Allow applications to set the RTCP SSRC of receive
1861 // streams instead, so we can avoid recreating the streams here.
1862 kv.second->RecreateAudioReceiveStream(ssrc);
wu@webrtc.org9dba5252013-08-05 20:36:57 +00001863 }
1864 }
1865
Taylor Brandstetter1a018dc2016-03-08 12:37:39 -08001866 send_streams_[ssrc]->SetSend(send_);
1867 return true;
wu@webrtc.org9dba5252013-08-05 20:36:57 +00001868}
1869
Peter Boström0c4e06b2015-10-07 12:23:21 +02001870bool WebRtcVoiceMediaChannel::RemoveSendStream(uint32_t ssrc) {
Peter Boströmca8b4042016-03-08 14:24:13 -08001871 TRACE_EVENT0("webrtc", "WebRtcVoiceMediaChannel::RemoveSendStream");
solenberg566ef242015-11-06 15:34:49 -08001872 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
Mirko Bonadei675513b2017-11-09 11:09:25 +01001873 RTC_LOG(LS_INFO) << "RemoveSendStream: " << ssrc;
solenberg3a941542015-11-16 07:34:50 -08001874
solenbergc96df772015-10-21 13:01:53 -07001875 auto it = send_streams_.find(ssrc);
1876 if (it == send_streams_.end()) {
Mirko Bonadei675513b2017-11-09 11:09:25 +01001877 RTC_LOG(LS_WARNING) << "Try to remove stream with ssrc " << ssrc
1878 << " which doesn't exist.";
wu@webrtc.org9dba5252013-08-05 20:36:57 +00001879 return false;
1880 }
1881
Taylor Brandstetter1a018dc2016-03-08 12:37:39 -08001882 it->second->SetSend(false);
wu@webrtc.org9dba5252013-08-05 20:36:57 +00001883
solenberg7602aab2016-11-14 11:30:07 -08001884 // TODO(solenberg): If we're removing the receiver_reports_ssrc_ stream, find
1885 // the first active send stream and use that instead, reassociating receive
1886 // streams.
1887
solenberg7add0582015-11-20 09:59:34 -08001888 // Clean up and delete the send stream+channel.
Taylor Brandstetter1a018dc2016-03-08 12:37:39 -08001889 int channel = it->second->channel();
Mirko Bonadei675513b2017-11-09 11:09:25 +01001890 RTC_LOG(LS_INFO) << "Removing audio send stream " << ssrc
1891 << " with VoiceEngine channel #" << channel << ".";
solenberg7add0582015-11-20 09:59:34 -08001892 delete it->second;
1893 send_streams_.erase(it);
1894 if (!DeleteVoEChannel(channel)) {
solenberg0a617e22015-10-20 15:49:38 -07001895 return false;
wu@webrtc.org9dba5252013-08-05 20:36:57 +00001896 }
solenbergc96df772015-10-21 13:01:53 -07001897 if (send_streams_.empty()) {
Taylor Brandstetter1a018dc2016-03-08 12:37:39 -08001898 SetSend(false);
solenberg0a617e22015-10-20 15:49:38 -07001899 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001900 return true;
1901}
1902
1903bool WebRtcVoiceMediaChannel::AddRecvStream(const StreamParams& sp) {
Peter Boströmca8b4042016-03-08 14:24:13 -08001904 TRACE_EVENT0("webrtc", "WebRtcVoiceMediaChannel::AddRecvStream");
solenberg566ef242015-11-06 15:34:49 -08001905 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
Mirko Bonadei675513b2017-11-09 11:09:25 +01001906 RTC_LOG(LS_INFO) << "AddRecvStream: " << sp.ToString();
solenbergd97ec302015-10-07 01:40:33 -07001907
solenberg0b675462015-10-09 01:37:09 -07001908 if (!ValidateStreamParams(sp)) {
wu@webrtc.org78187522013-10-07 23:32:02 +00001909 return false;
1910 }
1911
solenberg7add0582015-11-20 09:59:34 -08001912 const uint32_t ssrc = sp.first_ssrc();
solenberg0b675462015-10-09 01:37:09 -07001913 if (ssrc == 0) {
Mirko Bonadei675513b2017-11-09 11:09:25 +01001914 RTC_LOG(LS_WARNING) << "AddRecvStream with ssrc==0 is not supported.";
solenberg0b675462015-10-09 01:37:09 -07001915 return false;
1916 }
1917
solenberg2100c0b2017-03-01 11:29:29 -08001918 // If this stream was previously received unsignaled, we promote it, possibly
1919 // recreating the AudioReceiveStream, if sync_label has changed.
1920 if (MaybeDeregisterUnsignaledRecvStream(ssrc)) {
solenberg4904fb62017-02-17 12:01:14 -08001921 recv_streams_[ssrc]->MaybeRecreateAudioReceiveStream(sp.sync_label);
solenberg4904fb62017-02-17 12:01:14 -08001922 return true;
solenberg1ac56142015-10-13 03:58:19 -07001923 }
solenberg0b675462015-10-09 01:37:09 -07001924
solenberg7add0582015-11-20 09:59:34 -08001925 if (GetReceiveChannelId(ssrc) != -1) {
Mirko Bonadei675513b2017-11-09 11:09:25 +01001926 RTC_LOG(LS_ERROR) << "Stream already exists with ssrc " << ssrc;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001927 return false;
1928 }
Fredrik Solenberg4b60c732015-05-07 14:07:48 +02001929
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001930 // Create a new channel for receiving audio data.
solenberg7add0582015-11-20 09:59:34 -08001931 const int channel = CreateVoEChannel();
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001932 if (channel == -1) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001933 return false;
1934 }
Minyue2013aec2015-05-13 14:14:42 +02001935
stefanba4c0e42016-02-04 04:12:24 -08001936 recv_streams_.insert(std::make_pair(
kwiberg1c07c702017-03-27 07:15:49 -07001937 ssrc,
1938 new WebRtcAudioReceiveStream(
1939 channel, ssrc, receiver_reports_ssrc_, recv_transport_cc_enabled_,
1940 recv_nack_enabled_, sp.sync_label, recv_rtp_extensions_, call_, this,
1941 engine()->decoder_factory_, decoder_map_)));
aleloi84ef6152016-08-04 05:28:21 -07001942 recv_streams_[ssrc]->SetPlayout(playout_);
solenberg7add0582015-11-20 09:59:34 -08001943
solenberg1ac56142015-10-13 03:58:19 -07001944 return true;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001945}
1946
Peter Boström0c4e06b2015-10-07 12:23:21 +02001947bool WebRtcVoiceMediaChannel::RemoveRecvStream(uint32_t ssrc) {
Peter Boströmca8b4042016-03-08 14:24:13 -08001948 TRACE_EVENT0("webrtc", "WebRtcVoiceMediaChannel::RemoveRecvStream");
solenberg566ef242015-11-06 15:34:49 -08001949 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
Mirko Bonadei675513b2017-11-09 11:09:25 +01001950 RTC_LOG(LS_INFO) << "RemoveRecvStream: " << ssrc;
solenbergd97ec302015-10-07 01:40:33 -07001951
solenberg7add0582015-11-20 09:59:34 -08001952 const auto it = recv_streams_.find(ssrc);
1953 if (it == recv_streams_.end()) {
Mirko Bonadei675513b2017-11-09 11:09:25 +01001954 RTC_LOG(LS_WARNING) << "Try to remove stream with ssrc " << ssrc
1955 << " which doesn't exist.";
henrike@webrtc.org1e09a712013-07-26 19:17:59 +00001956 return false;
wu@webrtc.org9dba5252013-08-05 20:36:57 +00001957 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001958
solenberg2100c0b2017-03-01 11:29:29 -08001959 MaybeDeregisterUnsignaledRecvStream(ssrc);
henrike@webrtc.org1e09a712013-07-26 19:17:59 +00001960
solenberg7add0582015-11-20 09:59:34 -08001961 const int channel = it->second->channel();
1962
1963 // Clean up and delete the receive stream+channel.
Mirko Bonadei675513b2017-11-09 11:09:25 +01001964 RTC_LOG(LS_INFO) << "Removing audio receive stream " << ssrc
1965 << " with VoiceEngine channel #" << channel << ".";
Tommif888bb52015-12-12 01:37:01 +01001966 it->second->SetRawAudioSink(nullptr);
solenberg7add0582015-11-20 09:59:34 -08001967 delete it->second;
1968 recv_streams_.erase(it);
1969 return DeleteVoEChannel(channel);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001970}
1971
Taylor Brandstetter1a018dc2016-03-08 12:37:39 -08001972bool WebRtcVoiceMediaChannel::SetLocalSource(uint32_t ssrc,
1973 AudioSource* source) {
solenbergc96df772015-10-21 13:01:53 -07001974 auto it = send_streams_.find(ssrc);
1975 if (it == send_streams_.end()) {
Taylor Brandstetter1a018dc2016-03-08 12:37:39 -08001976 if (source) {
1977 // Return an error if trying to set a valid source with an invalid ssrc.
Mirko Bonadei675513b2017-11-09 11:09:25 +01001978 RTC_LOG(LS_ERROR) << "SetLocalSource failed with ssrc " << ssrc;
wu@webrtc.org9dba5252013-08-05 20:36:57 +00001979 return false;
1980 }
1981
1982 // The channel likely has gone away, do nothing.
henrike@webrtc.org1e09a712013-07-26 19:17:59 +00001983 return true;
henrike@webrtc.org1e09a712013-07-26 19:17:59 +00001984 }
1985
Taylor Brandstetter1a018dc2016-03-08 12:37:39 -08001986 if (source) {
1987 it->second->SetSource(source);
solenberg1ac56142015-10-13 03:58:19 -07001988 } else {
Taylor Brandstetter1a018dc2016-03-08 12:37:39 -08001989 it->second->ClearSource();
solenberg1ac56142015-10-13 03:58:19 -07001990 }
henrike@webrtc.org1e09a712013-07-26 19:17:59 +00001991
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001992 return true;
1993}
1994
solenberg796b8f92017-03-01 17:02:23 -08001995// TODO(solenberg): Remove, once AudioMonitor is gone.
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001996bool WebRtcVoiceMediaChannel::GetActiveStreams(
1997 AudioInfo::StreamList* actives) {
solenberg566ef242015-11-06 15:34:49 -08001998 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001999 actives->clear();
solenberg7add0582015-11-20 09:59:34 -08002000 for (const auto& ch : recv_streams_) {
solenberg796b8f92017-03-01 17:02:23 -08002001 int level = ch.second->GetOutputLevel();
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002002 if (level > 0) {
Fredrik Solenbergaf9fb212015-08-26 10:45:53 +02002003 actives->push_back(std::make_pair(ch.first, level));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002004 }
2005 }
2006 return true;
2007}
2008
solenberg796b8f92017-03-01 17:02:23 -08002009// TODO(solenberg): Remove, once AudioMonitor is gone.
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002010int WebRtcVoiceMediaChannel::GetOutputLevel() {
solenberg566ef242015-11-06 15:34:49 -08002011 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
solenberg1ac56142015-10-13 03:58:19 -07002012 int highest = 0;
solenberg7add0582015-11-20 09:59:34 -08002013 for (const auto& ch : recv_streams_) {
solenberg796b8f92017-03-01 17:02:23 -08002014 highest = std::max(ch.second->GetOutputLevel(), highest);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002015 }
2016 return highest;
2017}
2018
solenberg4bac9c52015-10-09 02:32:53 -07002019bool WebRtcVoiceMediaChannel::SetOutputVolume(uint32_t ssrc, double volume) {
solenberg566ef242015-11-06 15:34:49 -08002020 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
solenberg2100c0b2017-03-01 11:29:29 -08002021 std::vector<uint32_t> ssrcs(1, ssrc);
deadbeef3bc15102017-04-20 19:25:07 -07002022 // SSRC of 0 represents the default receive stream.
solenberg1ac56142015-10-13 03:58:19 -07002023 if (ssrc == 0) {
2024 default_recv_volume_ = volume;
solenberg2100c0b2017-03-01 11:29:29 -08002025 ssrcs = unsignaled_recv_ssrcs_;
2026 }
2027 for (uint32_t ssrc : ssrcs) {
2028 const auto it = recv_streams_.find(ssrc);
2029 if (it == recv_streams_.end()) {
Mirko Bonadei675513b2017-11-09 11:09:25 +01002030 RTC_LOG(LS_WARNING) << "SetOutputVolume: no recv stream " << ssrc;
solenberg2100c0b2017-03-01 11:29:29 -08002031 return false;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002032 }
solenberg2100c0b2017-03-01 11:29:29 -08002033 it->second->SetOutputVolume(volume);
Mirko Bonadei675513b2017-11-09 11:09:25 +01002034 RTC_LOG(LS_INFO) << "SetOutputVolume() to " << volume
2035 << " for recv stream with ssrc " << ssrc;
solenberg1ac56142015-10-13 03:58:19 -07002036 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002037 return true;
2038}
2039
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002040bool WebRtcVoiceMediaChannel::CanInsertDtmf() {
Fredrik Solenbergb5727682015-12-04 15:22:19 +01002041 return dtmf_payload_type_ ? true : false;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002042}
2043
solenberg1d63dd02015-12-02 12:35:09 -08002044bool WebRtcVoiceMediaChannel::InsertDtmf(uint32_t ssrc, int event,
2045 int duration) {
solenberg566ef242015-11-06 15:34:49 -08002046 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
Mirko Bonadei675513b2017-11-09 11:09:25 +01002047 RTC_LOG(LS_INFO) << "WebRtcVoiceMediaChannel::InsertDtmf";
Fredrik Solenbergb5727682015-12-04 15:22:19 +01002048 if (!dtmf_payload_type_) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002049 return false;
2050 }
2051
Fredrik Solenbergb5727682015-12-04 15:22:19 +01002052 // Figure out which WebRtcAudioSendStream to send the event on.
2053 auto it = ssrc != 0 ? send_streams_.find(ssrc) : send_streams_.begin();
2054 if (it == send_streams_.end()) {
Mirko Bonadei675513b2017-11-09 11:09:25 +01002055 RTC_LOG(LS_WARNING) << "The specified ssrc " << ssrc << " is not in use.";
solenberg1d63dd02015-12-02 12:35:09 -08002056 return false;
2057 }
Fredrik Solenbergb5727682015-12-04 15:22:19 +01002058 if (event < kMinTelephoneEventCode ||
2059 event > kMaxTelephoneEventCode) {
Mirko Bonadei675513b2017-11-09 11:09:25 +01002060 RTC_LOG(LS_WARNING) << "DTMF event code " << event << " out of range.";
solenberg1d63dd02015-12-02 12:35:09 -08002061 return false;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002062 }
solenbergffbbcac2016-11-17 05:25:37 -08002063 RTC_DCHECK_NE(-1, dtmf_payload_freq_);
2064 return it->second->SendTelephoneEvent(*dtmf_payload_type_, dtmf_payload_freq_,
2065 event, duration);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002066}
2067
wu@webrtc.orga9890802013-12-13 00:21:03 +00002068void WebRtcVoiceMediaChannel::OnPacketReceived(
jbaucheec21bd2016-03-20 06:15:43 -07002069 rtc::CopyOnWriteBuffer* packet, const rtc::PacketTime& packet_time) {
solenberg566ef242015-11-06 15:34:49 -08002070 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
Fredrik Solenberg4b60c732015-05-07 14:07:48 +02002071
mflodman3d7db262016-04-29 00:57:13 -07002072 const webrtc::PacketTime webrtc_packet_time(packet_time.timestamp,
2073 packet_time.not_before);
2074 webrtc::PacketReceiver::DeliveryStatus delivery_result =
2075 call_->Receiver()->DeliverPacket(webrtc::MediaType::AUDIO,
2076 packet->cdata(), packet->size(),
2077 webrtc_packet_time);
mflodman3d7db262016-04-29 00:57:13 -07002078 if (delivery_result != webrtc::PacketReceiver::DELIVERY_UNKNOWN_SSRC) {
2079 return;
2080 }
2081
solenberg2100c0b2017-03-01 11:29:29 -08002082 // Create an unsignaled receive stream for this previously not received ssrc.
2083 // If there already is N unsignaled receive streams, delete the oldest.
mflodman3d7db262016-04-29 00:57:13 -07002084 // See: https://bugs.chromium.org/p/webrtc/issues/detail?id=5208
solenberg1ac56142015-10-13 03:58:19 -07002085 uint32_t ssrc = 0;
jbaucheec21bd2016-03-20 06:15:43 -07002086 if (!GetRtpSsrc(packet->cdata(), packet->size(), &ssrc)) {
solenberg1ac56142015-10-13 03:58:19 -07002087 return;
2088 }
solenberg2100c0b2017-03-01 11:29:29 -08002089 RTC_DCHECK(std::find(unsignaled_recv_ssrcs_.begin(),
2090 unsignaled_recv_ssrcs_.end(), ssrc) == unsignaled_recv_ssrcs_.end());
solenberg1ac56142015-10-13 03:58:19 -07002091
solenberg2100c0b2017-03-01 11:29:29 -08002092 // Add new stream.
mflodman3d7db262016-04-29 00:57:13 -07002093 StreamParams sp;
2094 sp.ssrcs.push_back(ssrc);
Mirko Bonadei675513b2017-11-09 11:09:25 +01002095 RTC_LOG(LS_INFO) << "Creating unsignaled receive stream for SSRC=" << ssrc;
mflodman3d7db262016-04-29 00:57:13 -07002096 if (!AddRecvStream(sp)) {
Mirko Bonadei675513b2017-11-09 11:09:25 +01002097 RTC_LOG(LS_WARNING) << "Could not create unsignaled receive stream.";
mflodman3d7db262016-04-29 00:57:13 -07002098 return;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002099 }
solenberg2100c0b2017-03-01 11:29:29 -08002100 unsignaled_recv_ssrcs_.push_back(ssrc);
2101 RTC_HISTOGRAM_COUNTS_LINEAR(
2102 "WebRTC.Audio.NumOfUnsignaledStreams", unsignaled_recv_ssrcs_.size(), 1,
2103 100, 101);
solenbergf748ca42017-02-06 13:03:19 -08002104
solenberg2100c0b2017-03-01 11:29:29 -08002105 // Remove oldest unsignaled stream, if we have too many.
2106 if (unsignaled_recv_ssrcs_.size() > kMaxUnsignaledRecvStreams) {
2107 uint32_t remove_ssrc = unsignaled_recv_ssrcs_.front();
Mirko Bonadei675513b2017-11-09 11:09:25 +01002108 RTC_LOG(LS_INFO) << "Removing unsignaled receive stream with SSRC="
2109 << remove_ssrc;
solenberg2100c0b2017-03-01 11:29:29 -08002110 RemoveRecvStream(remove_ssrc);
2111 }
2112 RTC_DCHECK_GE(kMaxUnsignaledRecvStreams, unsignaled_recv_ssrcs_.size());
2113
2114 SetOutputVolume(ssrc, default_recv_volume_);
2115
2116 // The default sink can only be attached to one stream at a time, so we hook
2117 // it up to the *latest* unsignaled stream we've seen, in order to support the
2118 // case where the SSRC of one unsignaled stream changes.
mflodman3d7db262016-04-29 00:57:13 -07002119 if (default_sink_) {
solenberg2100c0b2017-03-01 11:29:29 -08002120 for (uint32_t drop_ssrc : unsignaled_recv_ssrcs_) {
2121 auto it = recv_streams_.find(drop_ssrc);
2122 it->second->SetRawAudioSink(nullptr);
2123 }
mflodman3d7db262016-04-29 00:57:13 -07002124 std::unique_ptr<webrtc::AudioSinkInterface> proxy_sink(
2125 new ProxySink(default_sink_.get()));
solenberg2100c0b2017-03-01 11:29:29 -08002126 SetRawAudioSink(ssrc, std::move(proxy_sink));
mflodman3d7db262016-04-29 00:57:13 -07002127 }
solenberg2100c0b2017-03-01 11:29:29 -08002128
mflodman3d7db262016-04-29 00:57:13 -07002129 delivery_result = call_->Receiver()->DeliverPacket(webrtc::MediaType::AUDIO,
2130 packet->cdata(),
2131 packet->size(),
2132 webrtc_packet_time);
2133 RTC_DCHECK_NE(webrtc::PacketReceiver::DELIVERY_UNKNOWN_SSRC, delivery_result);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002134}
2135
wu@webrtc.orga9890802013-12-13 00:21:03 +00002136void WebRtcVoiceMediaChannel::OnRtcpReceived(
jbaucheec21bd2016-03-20 06:15:43 -07002137 rtc::CopyOnWriteBuffer* packet, const rtc::PacketTime& packet_time) {
solenberg566ef242015-11-06 15:34:49 -08002138 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
Fredrik Solenberg4b60c732015-05-07 14:07:48 +02002139
Fredrik Solenberg709ed672015-09-15 12:26:33 +02002140 // Forward packet to Call as well.
2141 const webrtc::PacketTime webrtc_packet_time(packet_time.timestamp,
2142 packet_time.not_before);
2143 call_->Receiver()->DeliverPacket(webrtc::MediaType::AUDIO,
jbaucheec21bd2016-03-20 06:15:43 -07002144 packet->cdata(), packet->size(), webrtc_packet_time);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002145}
2146
Honghai Zhangcc411c02016-03-29 17:27:21 -07002147void WebRtcVoiceMediaChannel::OnNetworkRouteChanged(
2148 const std::string& transport_name,
Honghai Zhang0e533ef2016-04-19 15:41:36 -07002149 const rtc::NetworkRoute& network_route) {
Zhi Huang5f5918f2017-11-12 17:26:23 -08002150 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
2151 // TODO(zhihaung): Merge these two callbacks.
Honghai Zhang0e533ef2016-04-19 15:41:36 -07002152 call_->OnNetworkRouteChanged(transport_name, network_route);
Zhi Huang5f5918f2017-11-12 17:26:23 -08002153 call_->OnTransportOverheadChanged(webrtc::MediaType::AUDIO,
2154 network_route.packet_overhead);
Honghai Zhangcc411c02016-03-29 17:27:21 -07002155}
2156
Peter Boström0c4e06b2015-10-07 12:23:21 +02002157bool WebRtcVoiceMediaChannel::MuteStream(uint32_t ssrc, bool muted) {
solenberg566ef242015-11-06 15:34:49 -08002158 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
solenberg94218532016-06-16 10:53:22 -07002159 const auto it = send_streams_.find(ssrc);
2160 if (it == send_streams_.end()) {
Mirko Bonadei675513b2017-11-09 11:09:25 +01002161 RTC_LOG(LS_WARNING) << "The specified ssrc " << ssrc << " is not in use.";
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002162 return false;
2163 }
solenberg94218532016-06-16 10:53:22 -07002164 it->second->SetMuted(muted);
2165
2166 // TODO(solenberg):
buildbot@webrtc.org6b21b712014-07-31 15:08:53 +00002167 // We set the AGC to mute state only when all the channels are muted.
2168 // This implementation is not ideal, instead we should signal the AGC when
2169 // the mic channel is muted/unmuted. We can't do it today because there
2170 // is no good way to know which stream is mapping to the mic channel.
2171 bool all_muted = muted;
solenberg94218532016-06-16 10:53:22 -07002172 for (const auto& kv : send_streams_) {
2173 all_muted = all_muted && kv.second->muted();
buildbot@webrtc.org6b21b712014-07-31 15:08:53 +00002174 }
solenberg059fb442016-10-26 05:12:24 -07002175 engine()->apm()->set_output_will_be_muted(all_muted);
buildbot@webrtc.org6b21b712014-07-31 15:08:53 +00002176
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002177 return true;
2178}
2179
deadbeef80346142016-04-27 14:17:10 -07002180bool WebRtcVoiceMediaChannel::SetMaxSendBitrate(int bps) {
Mirko Bonadei675513b2017-11-09 11:09:25 +01002181 RTC_LOG(LS_INFO) << "WebRtcVoiceMediaChannel::SetMaxSendBitrate.";
deadbeef80346142016-04-27 14:17:10 -07002182 max_send_bitrate_bps_ = bps;
minyue7a973442016-10-20 03:27:12 -07002183 bool success = true;
skvlade0d46372016-04-07 22:59:22 -07002184 for (const auto& kv : send_streams_) {
minyue7a973442016-10-20 03:27:12 -07002185 if (!kv.second->SetMaxSendBitrate(max_send_bitrate_bps_)) {
2186 success = false;
skvlade0d46372016-04-07 22:59:22 -07002187 }
2188 }
minyue7a973442016-10-20 03:27:12 -07002189 return success;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002190}
2191
skvlad7a43d252016-03-22 15:32:27 -07002192void WebRtcVoiceMediaChannel::OnReadyToSend(bool ready) {
2193 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
Mirko Bonadei675513b2017-11-09 11:09:25 +01002194 RTC_LOG(LS_VERBOSE) << "OnReadyToSend: " << (ready ? "Ready." : "Not ready.");
skvlad7a43d252016-03-22 15:32:27 -07002195 call_->SignalChannelNetworkState(
2196 webrtc::MediaType::AUDIO,
2197 ready ? webrtc::kNetworkUp : webrtc::kNetworkDown);
2198}
2199
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002200bool WebRtcVoiceMediaChannel::GetStats(VoiceMediaInfo* info) {
Peter Boströmca8b4042016-03-08 14:24:13 -08002201 TRACE_EVENT0("webrtc", "WebRtcVoiceMediaChannel::GetStats");
solenberg566ef242015-11-06 15:34:49 -08002202 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
solenberg85a04962015-10-27 03:35:21 -07002203 RTC_DCHECK(info);
solenbergd97ec302015-10-07 01:40:33 -07002204
solenberg85a04962015-10-27 03:35:21 -07002205 // Get SSRC and stats for each sender.
hbos1acfbd22016-11-17 23:43:29 -08002206 RTC_DCHECK_EQ(info->senders.size(), 0U);
solenberg85a04962015-10-27 03:35:21 -07002207 for (const auto& stream : send_streams_) {
2208 webrtc::AudioSendStream::Stats stats = stream.second->GetStats();
wu@webrtc.org9dba5252013-08-05 20:36:57 +00002209 VoiceSenderInfo sinfo;
solenberg85a04962015-10-27 03:35:21 -07002210 sinfo.add_ssrc(stats.local_ssrc);
2211 sinfo.bytes_sent = stats.bytes_sent;
2212 sinfo.packets_sent = stats.packets_sent;
2213 sinfo.packets_lost = stats.packets_lost;
2214 sinfo.fraction_lost = stats.fraction_lost;
2215 sinfo.codec_name = stats.codec_name;
hbos1acfbd22016-11-17 23:43:29 -08002216 sinfo.codec_payload_type = stats.codec_payload_type;
solenberg85a04962015-10-27 03:35:21 -07002217 sinfo.ext_seqnum = stats.ext_seqnum;
2218 sinfo.jitter_ms = stats.jitter_ms;
2219 sinfo.rtt_ms = stats.rtt_ms;
2220 sinfo.audio_level = stats.audio_level;
zsteine76bd3a2017-07-14 12:17:49 -07002221 sinfo.total_input_energy = stats.total_input_energy;
2222 sinfo.total_input_duration = stats.total_input_duration;
solenberg85a04962015-10-27 03:35:21 -07002223 sinfo.aec_quality_min = stats.aec_quality_min;
2224 sinfo.echo_delay_median_ms = stats.echo_delay_median_ms;
2225 sinfo.echo_delay_std_ms = stats.echo_delay_std_ms;
2226 sinfo.echo_return_loss = stats.echo_return_loss;
2227 sinfo.echo_return_loss_enhancement = stats.echo_return_loss_enhancement;
ivoc8c63a822016-10-21 04:10:03 -07002228 sinfo.residual_echo_likelihood = stats.residual_echo_likelihood;
ivoc4e477a12017-01-15 08:29:46 -08002229 sinfo.residual_echo_likelihood_recent_max =
2230 stats.residual_echo_likelihood_recent_max;
Taylor Brandstetter1a018dc2016-03-08 12:37:39 -08002231 sinfo.typing_noise_detected = (send_ ? stats.typing_noise_detected : false);
ivoce1198e02017-09-08 08:13:19 -07002232 sinfo.ana_statistics = stats.ana_statistics;
wu@webrtc.org9dba5252013-08-05 20:36:57 +00002233 info->senders.push_back(sinfo);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002234 }
2235
solenberg85a04962015-10-27 03:35:21 -07002236 // Get SSRC and stats for each receiver.
hbos1acfbd22016-11-17 23:43:29 -08002237 RTC_DCHECK_EQ(info->receivers.size(), 0U);
solenberg7add0582015-11-20 09:59:34 -08002238 for (const auto& stream : recv_streams_) {
deadbeef4e2deab2017-09-20 13:56:21 -07002239 uint32_t ssrc = stream.first;
2240 // When SSRCs are unsignaled, there's only one audio MediaStreamTrack, but
2241 // multiple RTP streams can be received over time (if the SSRC changes for
2242 // whatever reason). We only want the RTCMediaStreamTrackStats to represent
2243 // the stats for the most recent stream (the one whose audio is actually
2244 // routed to the MediaStreamTrack), so here we ignore any unsignaled SSRCs
2245 // except for the most recent one (last in the vector). This is somewhat of
2246 // a hack, and means you don't get *any* stats for these inactive streams,
2247 // but it's slightly better than the previous behavior, which was "highest
2248 // SSRC wins".
2249 // See: https://bugs.chromium.org/p/webrtc/issues/detail?id=8158
2250 if (!unsignaled_recv_ssrcs_.empty()) {
2251 auto end_it = --unsignaled_recv_ssrcs_.end();
2252 if (std::find(unsignaled_recv_ssrcs_.begin(), end_it, ssrc) != end_it) {
2253 continue;
2254 }
2255 }
Fredrik Solenberg4f4ec0a2015-10-22 10:49:27 +02002256 webrtc::AudioReceiveStream::Stats stats = stream.second->GetStats();
2257 VoiceReceiverInfo rinfo;
2258 rinfo.add_ssrc(stats.remote_ssrc);
2259 rinfo.bytes_rcvd = stats.bytes_rcvd;
2260 rinfo.packets_rcvd = stats.packets_rcvd;
2261 rinfo.packets_lost = stats.packets_lost;
2262 rinfo.fraction_lost = stats.fraction_lost;
2263 rinfo.codec_name = stats.codec_name;
hbos1acfbd22016-11-17 23:43:29 -08002264 rinfo.codec_payload_type = stats.codec_payload_type;
Fredrik Solenberg4f4ec0a2015-10-22 10:49:27 +02002265 rinfo.ext_seqnum = stats.ext_seqnum;
2266 rinfo.jitter_ms = stats.jitter_ms;
2267 rinfo.jitter_buffer_ms = stats.jitter_buffer_ms;
2268 rinfo.jitter_buffer_preferred_ms = stats.jitter_buffer_preferred_ms;
2269 rinfo.delay_estimate_ms = stats.delay_estimate_ms;
2270 rinfo.audio_level = stats.audio_level;
zsteine76bd3a2017-07-14 12:17:49 -07002271 rinfo.total_output_energy = stats.total_output_energy;
Steve Anton2dbc69f2017-08-24 17:15:13 -07002272 rinfo.total_samples_received = stats.total_samples_received;
zsteine76bd3a2017-07-14 12:17:49 -07002273 rinfo.total_output_duration = stats.total_output_duration;
Steve Anton2dbc69f2017-08-24 17:15:13 -07002274 rinfo.concealed_samples = stats.concealed_samples;
Gustaf Ullberg9a2e9062017-09-18 09:28:20 +02002275 rinfo.concealment_events = stats.concealment_events;
Gustaf Ullbergb0a02072017-10-02 12:00:34 +02002276 rinfo.jitter_buffer_delay_seconds = stats.jitter_buffer_delay_seconds;
Fredrik Solenberg4f4ec0a2015-10-22 10:49:27 +02002277 rinfo.expand_rate = stats.expand_rate;
2278 rinfo.speech_expand_rate = stats.speech_expand_rate;
2279 rinfo.secondary_decoded_rate = stats.secondary_decoded_rate;
minyue-webrtc0e320ec2017-08-28 13:51:27 +02002280 rinfo.secondary_discarded_rate = stats.secondary_discarded_rate;
Fredrik Solenberg4f4ec0a2015-10-22 10:49:27 +02002281 rinfo.accelerate_rate = stats.accelerate_rate;
2282 rinfo.preemptive_expand_rate = stats.preemptive_expand_rate;
2283 rinfo.decoding_calls_to_silence_generator =
2284 stats.decoding_calls_to_silence_generator;
2285 rinfo.decoding_calls_to_neteq = stats.decoding_calls_to_neteq;
2286 rinfo.decoding_normal = stats.decoding_normal;
2287 rinfo.decoding_plc = stats.decoding_plc;
2288 rinfo.decoding_cng = stats.decoding_cng;
2289 rinfo.decoding_plc_cng = stats.decoding_plc_cng;
henrik.lundin63489782016-09-20 01:47:12 -07002290 rinfo.decoding_muted_output = stats.decoding_muted_output;
Fredrik Solenberg4f4ec0a2015-10-22 10:49:27 +02002291 rinfo.capture_start_ntp_time_ms = stats.capture_start_ntp_time_ms;
2292 info->receivers.push_back(rinfo);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002293 }
2294
hbos1acfbd22016-11-17 23:43:29 -08002295 // Get codec info
2296 for (const AudioCodec& codec : send_codecs_) {
2297 webrtc::RtpCodecParameters codec_params = codec.ToCodecParameters();
2298 info->send_codecs.insert(
2299 std::make_pair(codec_params.payload_type, std::move(codec_params)));
2300 }
2301 for (const AudioCodec& codec : recv_codecs_) {
2302 webrtc::RtpCodecParameters codec_params = codec.ToCodecParameters();
2303 info->receive_codecs.insert(
2304 std::make_pair(codec_params.payload_type, std::move(codec_params)));
2305 }
2306
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002307 return true;
2308}
2309
Tommif888bb52015-12-12 01:37:01 +01002310void WebRtcVoiceMediaChannel::SetRawAudioSink(
2311 uint32_t ssrc,
kwiberg686a8ef2016-02-26 03:00:35 -08002312 std::unique_ptr<webrtc::AudioSinkInterface> sink) {
Tommif888bb52015-12-12 01:37:01 +01002313 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
Mirko Bonadei675513b2017-11-09 11:09:25 +01002314 RTC_LOG(LS_VERBOSE) << "WebRtcVoiceMediaChannel::SetRawAudioSink: ssrc:"
2315 << ssrc << " " << (sink ? "(ptr)" : "NULL");
deadbeef884f5852016-01-15 09:20:04 -08002316 if (ssrc == 0) {
solenberg2100c0b2017-03-01 11:29:29 -08002317 if (!unsignaled_recv_ssrcs_.empty()) {
kwiberg686a8ef2016-02-26 03:00:35 -08002318 std::unique_ptr<webrtc::AudioSinkInterface> proxy_sink(
deadbeef884f5852016-01-15 09:20:04 -08002319 sink ? new ProxySink(sink.get()) : nullptr);
solenberg2100c0b2017-03-01 11:29:29 -08002320 SetRawAudioSink(unsignaled_recv_ssrcs_.back(), std::move(proxy_sink));
deadbeef884f5852016-01-15 09:20:04 -08002321 }
2322 default_sink_ = std::move(sink);
2323 return;
2324 }
Tommif888bb52015-12-12 01:37:01 +01002325 const auto it = recv_streams_.find(ssrc);
2326 if (it == recv_streams_.end()) {
Mirko Bonadei675513b2017-11-09 11:09:25 +01002327 RTC_LOG(LS_WARNING) << "SetRawAudioSink: no recv stream " << ssrc;
Tommif888bb52015-12-12 01:37:01 +01002328 return;
2329 }
deadbeef2d110be2016-01-13 12:00:26 -08002330 it->second->SetRawAudioSink(std::move(sink));
Tommif888bb52015-12-12 01:37:01 +01002331}
2332
hbos8d609f62017-04-10 07:39:05 -07002333std::vector<webrtc::RtpSource> WebRtcVoiceMediaChannel::GetSources(
2334 uint32_t ssrc) const {
2335 auto it = recv_streams_.find(ssrc);
2336 RTC_DCHECK(it != recv_streams_.end())
2337 << "Attempting to get contributing sources for SSRC:" << ssrc
2338 << " which doesn't exist.";
2339 return it->second->GetSources();
2340}
2341
Peter Boström0c4e06b2015-10-07 12:23:21 +02002342int WebRtcVoiceMediaChannel::GetReceiveChannelId(uint32_t ssrc) const {
solenberg566ef242015-11-06 15:34:49 -08002343 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
solenberg7add0582015-11-20 09:59:34 -08002344 const auto it = recv_streams_.find(ssrc);
2345 if (it != recv_streams_.end()) {
mallinath@webrtc.org67ee6b92014-02-03 16:57:16 +00002346 return it->second->channel();
solenberg8fb30c32015-10-13 03:06:58 -07002347 }
solenberg1ac56142015-10-13 03:58:19 -07002348 return -1;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002349}
2350
Peter Boström0c4e06b2015-10-07 12:23:21 +02002351int WebRtcVoiceMediaChannel::GetSendChannelId(uint32_t ssrc) const {
solenberg566ef242015-11-06 15:34:49 -08002352 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
solenbergc96df772015-10-21 13:01:53 -07002353 const auto it = send_streams_.find(ssrc);
2354 if (it != send_streams_.end()) {
mallinath@webrtc.org67ee6b92014-02-03 16:57:16 +00002355 return it->second->channel();
solenberg8fb30c32015-10-13 03:06:58 -07002356 }
wu@webrtc.org9dba5252013-08-05 20:36:57 +00002357 return -1;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002358}
solenberg2100c0b2017-03-01 11:29:29 -08002359
2360bool WebRtcVoiceMediaChannel::
2361 MaybeDeregisterUnsignaledRecvStream(uint32_t ssrc) {
2362 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
2363 auto it = std::find(unsignaled_recv_ssrcs_.begin(),
2364 unsignaled_recv_ssrcs_.end(),
2365 ssrc);
2366 if (it != unsignaled_recv_ssrcs_.end()) {
2367 unsignaled_recv_ssrcs_.erase(it);
2368 return true;
2369 }
2370 return false;
2371}
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002372} // namespace cricket
2373
2374#endif // HAVE_WEBRTC_VOICE