blob: 0ae975dce07d4ea41d2e84df619fadce6ce99e93 [file] [log] [blame]
henrike@webrtc.org28e20752013-07-10 00:45:361/*
kjellander1afca732016-02-08 04:46:452 * Copyright (c) 2004 The WebRTC project authors. All Rights Reserved.
henrike@webrtc.org28e20752013-07-10 00:45:363 *
kjellander1afca732016-02-08 04:46:454 * 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:369 */
10
henrike@webrtc.org28e20752013-07-10 00:45:3611#ifdef HAVE_WEBRTC_VOICE
12
Mirko Bonadei92ea95e2017-09-15 04:47:3113#include "media/engine/webrtcvoiceengine.h"
henrike@webrtc.org28e20752013-07-10 00:45:3614
15#include <algorithm>
16#include <cstdio>
ossuc54071d2016-08-17 09:45:4117#include <functional>
henrike@webrtc.org28e20752013-07-10 00:45:3618#include <string>
Steve Antone78bcb92017-10-31 16:53:0819#include <utility>
henrike@webrtc.org28e20752013-07-10 00:45:3620#include <vector>
21
Mirko Bonadei92ea95e2017-09-15 04:47:3122#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 19:33:0531#include "modules/audio_device/audio_device_impl.h"
Mirko Bonadei92ea95e2017-09-15 04:47:3132#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 04:47:3147#include "voice_engine/transmit_mixer.h"
henrike@webrtc.org28e20752013-07-10 00:45:3648
henrike@webrtc.org28e20752013-07-10 00:45:3649namespace cricket {
solenbergd97ec302015-10-07 08:40:3350namespace {
henrike@webrtc.org28e20752013-07-10 00:45:3651
solenberg418b7d32017-06-13 07:38:2752constexpr size_t kMaxUnsignaledRecvStreams = 4;
solenberg2100c0b2017-03-01 19:29:2953
solenberg971cab02016-06-14 17:02:4154constexpr int kNackRtpHistoryMs = 5000;
minyue@webrtc.org2dc6f312014-10-31 05:33:1055
peah1bcfce52016-08-26 14:16:0456// 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 09:08:5264// For SendSideBwe, Opus bitrate should be in the range between 6000 and 32000.
minyue10cbb462016-11-07 17:29:2265const int kOpusMinBitrateBps = 6000;
ossu20a4b3f2017-04-27 09:08:5266const int kOpusBitrateFbBps = 32000;
deadbeef80346142016-04-27 21:17:1067
wu@webrtc.orgde305012013-10-31 15:40:3868// 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 08:40:3371const rtc::DiffServCodePoint kAudioDscpValue = rtc::DSCP_EF;
henrike@webrtc.org1e09a712013-07-26 19:17:5972
Fredrik Solenbergb5727682015-12-04 14:22:1973const int kMinTelephoneEventCode = 0; // RFC4733 (Section 2.3.1)
74const int kMaxTelephoneEventCode = 255;
Fredrik Solenbergb5727682015-12-04 14:22:1975
solenberg31642aa2016-03-14 15:00:3776const int kMinPayloadType = 0;
77const int kMaxPayloadType = 127;
78
deadbeef884f5852016-01-15 17:20:0479class ProxySink : public webrtc::AudioSinkInterface {
80 public:
Steve Antone78bcb92017-10-31 16:53:0881 explicit ProxySink(AudioSinkInterface* sink) : sink_(sink) {
82 RTC_DCHECK(sink);
83 }
deadbeef884f5852016-01-15 17:20:0484
85 void OnData(const Data& audio) override { sink_->OnData(audio); }
86
87 private:
88 webrtc::AudioSinkInterface* sink_;
89};
90
solenberg0b675462015-10-09 08:37:0991bool ValidateStreamParams(const StreamParams& sp) {
92 if (sp.ssrcs.empty()) {
Mirko Bonadei675513b2017-11-09 10:09:2593 RTC_LOG(LS_ERROR) << "No SSRCs in stream parameters: " << sp.ToString();
solenberg0b675462015-10-09 08:37:0994 return false;
95 }
96 if (sp.ssrcs.size() > 1) {
Mirko Bonadei675513b2017-11-09 10:09:2597 RTC_LOG(LS_ERROR) << "Multiple SSRCs in stream parameters: "
98 << sp.ToString();
solenberg0b675462015-10-09 08:37:0999 return false;
100 }
101 return true;
102}
103
henrike@webrtc.org28e20752013-07-10 00:45:36104// Dumps an AudioCodec in RFC 2327-ish format.
solenbergd97ec302015-10-07 08:40:33105std::string ToString(const AudioCodec& codec) {
henrike@webrtc.org28e20752013-07-10 00:45:36106 std::stringstream ss;
ossu20a4b3f2017-04-27 09:08:52107 ss << codec.name << "/" << codec.clockrate << "/" << codec.channels;
108 if (!codec.params.empty()) {
109 ss << " {";
110 for (const auto& param : codec.params) {
111 ss << " " << param.first << "=" << param.second;
112 }
113 ss << " }";
114 }
115 ss << " (" << codec.id << ")";
henrike@webrtc.org28e20752013-07-10 00:45:36116 return ss.str();
117}
Minyue Li7100dcd2015-03-27 04:05:59118
solenbergd97ec302015-10-07 08:40:33119bool IsCodec(const AudioCodec& codec, const char* ref_name) {
Minyue Li7100dcd2015-03-27 04:05:59120 return (_stricmp(codec.name.c_str(), ref_name) == 0);
121}
122
solenbergd97ec302015-10-07 08:40:33123bool FindCodec(const std::vector<AudioCodec>& codecs,
solenberg26c8c912015-11-27 12:00:25124 const AudioCodec& codec,
125 AudioCodec* found_codec) {
Fredrik Solenbergaf9fb212015-08-26 08:45:53126 for (const AudioCodec& c : codecs) {
127 if (c.Matches(codec)) {
henrike@webrtc.org28e20752013-07-10 00:45:36128 if (found_codec != NULL) {
Fredrik Solenbergaf9fb212015-08-26 08:45:53129 *found_codec = c;
henrike@webrtc.org28e20752013-07-10 00:45:36130 }
131 return true;
132 }
133 }
134 return false;
135}
wu@webrtc.org1d1ffc92013-10-16 18:12:02136
solenberg0b675462015-10-09 08:37:09137bool VerifyUniquePayloadTypes(const std::vector<AudioCodec>& codecs) {
138 if (codecs.empty()) {
139 return true;
140 }
141 std::vector<int> payload_types;
142 for (const AudioCodec& codec : codecs) {
143 payload_types.push_back(codec.id);
144 }
145 std::sort(payload_types.begin(), payload_types.end());
146 auto it = std::unique(payload_types.begin(), payload_types.end());
147 return it == payload_types.end();
148}
149
minyue6b825df2016-10-31 11:08:32150rtc::Optional<std::string> GetAudioNetworkAdaptorConfig(
151 const AudioOptions& options) {
152 if (options.audio_network_adaptor && *options.audio_network_adaptor &&
153 options.audio_network_adaptor_config) {
154 // Turn on audio network adaptor only when |options_.audio_network_adaptor|
155 // equals true and |options_.audio_network_adaptor_config| has a value.
156 return options.audio_network_adaptor_config;
157 }
Oskar Sundbom78807582017-11-16 10:09:55158 return rtc::nullopt;
minyue6b825df2016-10-31 11:08:32159}
160
gyzhou95aa9642016-12-13 22:06:26161webrtc::AudioState::Config MakeAudioStateConfig(
162 VoEWrapper* voe_wrapper,
peaha9cc40b2017-06-29 15:32:09163 rtc::scoped_refptr<webrtc::AudioMixer> audio_mixer,
164 rtc::scoped_refptr<webrtc::AudioProcessing> audio_processing) {
solenberg566ef242015-11-06 23:34:49165 webrtc::AudioState::Config config;
166 config.voice_engine = voe_wrapper->engine();
gyzhou95aa9642016-12-13 22:06:26167 if (audio_mixer) {
168 config.audio_mixer = audio_mixer;
169 } else {
170 config.audio_mixer = webrtc::AudioMixerImpl::Create();
171 }
peaha9cc40b2017-06-29 15:32:09172 config.audio_processing = audio_processing;
solenberg566ef242015-11-06 23:34:49173 return config;
174}
175
deadbeefe702b302017-02-04 20:09:01176// |max_send_bitrate_bps| is the bitrate from "b=" in SDP.
177// |rtp_max_bitrate_bps| is the bitrate from RtpSender::SetParameters.
minyue7a973442016-10-20 10:27:12178rtc::Optional<int> ComputeSendBitrate(int max_send_bitrate_bps,
deadbeefe702b302017-02-04 20:09:01179 rtc::Optional<int> rtp_max_bitrate_bps,
ossu20a4b3f2017-04-27 09:08:52180 const webrtc::AudioCodecSpec& spec) {
deadbeefe702b302017-02-04 20:09:01181 // If application-configured bitrate is set, take minimum of that and SDP
182 // bitrate.
zsteina5e0df62017-06-14 18:41:48183 const int bps =
184 rtp_max_bitrate_bps
185 ? webrtc::MinPositive(max_send_bitrate_bps, *rtp_max_bitrate_bps)
186 : max_send_bitrate_bps;
minyue7a973442016-10-20 10:27:12187 if (bps <= 0) {
Oskar Sundbom78807582017-11-16 10:09:55188 return spec.info.default_bitrate_bps;
solenberg971cab02016-06-14 17:02:41189 }
minyue7a973442016-10-20 10:27:12190
ossu20a4b3f2017-04-27 09:08:52191 if (bps < spec.info.min_bitrate_bps) {
minyue7a973442016-10-20 10:27:12192 // If codec is not multi-rate and |bps| is less than the fixed bitrate then
193 // fail. If codec is not multi-rate and |bps| exceeds or equal the fixed
194 // bitrate then ignore.
Mirko Bonadei675513b2017-11-09 10:09:25195 RTC_LOG(LS_ERROR) << "Failed to set codec " << spec.format.name
196 << " to bitrate " << bps << " bps"
197 << ", requires at least " << spec.info.min_bitrate_bps
198 << " bps.";
Oskar Sundbom78807582017-11-16 10:09:55199 return rtc::nullopt;
solenberg971cab02016-06-14 17:02:41200 }
ossu20a4b3f2017-04-27 09:08:52201
202 if (spec.info.HasFixedBitrate()) {
Oskar Sundbom78807582017-11-16 10:09:55203 return spec.info.default_bitrate_bps;
ossu20a4b3f2017-04-27 09:08:52204 } else {
205 // If codec is multi-rate then just set the bitrate.
Oskar Sundbom78807582017-11-16 10:09:55206 return std::min(bps, spec.info.max_bitrate_bps);
ossu20a4b3f2017-04-27 09:08:52207 }
solenberg971cab02016-06-14 17:02:41208}
209
solenberg76377c52017-02-21 08:54:31210} // namespace
solenberg971cab02016-06-14 17:02:41211
ossu29b1a8d2016-06-13 14:34:51212WebRtcVoiceEngine::WebRtcVoiceEngine(
213 webrtc::AudioDeviceModule* adm,
ossueb1fde42017-05-02 13:46:30214 const rtc::scoped_refptr<webrtc::AudioEncoderFactory>& encoder_factory,
gyzhou95aa9642016-12-13 22:06:26215 const rtc::scoped_refptr<webrtc::AudioDecoderFactory>& decoder_factory,
peaha9cc40b2017-06-29 15:32:09216 rtc::scoped_refptr<webrtc::AudioMixer> audio_mixer,
217 rtc::scoped_refptr<webrtc::AudioProcessing> audio_processing)
ossueb1fde42017-05-02 13:46:30218 : WebRtcVoiceEngine(adm,
219 encoder_factory,
220 decoder_factory,
221 audio_mixer,
peaha9cc40b2017-06-29 15:32:09222 audio_processing,
deadbeefeb02c032017-06-15 15:29:25223 nullptr) {}
solenberg26c8c912015-11-27 12:00:25224
ossu29b1a8d2016-06-13 14:34:51225WebRtcVoiceEngine::WebRtcVoiceEngine(
226 webrtc::AudioDeviceModule* adm,
ossueb1fde42017-05-02 13:46:30227 const rtc::scoped_refptr<webrtc::AudioEncoderFactory>& encoder_factory,
ossu29b1a8d2016-06-13 14:34:51228 const rtc::scoped_refptr<webrtc::AudioDecoderFactory>& decoder_factory,
gyzhou95aa9642016-12-13 22:06:26229 rtc::scoped_refptr<webrtc::AudioMixer> audio_mixer,
peaha9cc40b2017-06-29 15:32:09230 rtc::scoped_refptr<webrtc::AudioProcessing> audio_processing,
ossu29b1a8d2016-06-13 14:34:51231 VoEWrapper* voe_wrapper)
deadbeefeb02c032017-06-15 15:29:25232 : adm_(adm),
ossueb1fde42017-05-02 13:46:30233 encoder_factory_(encoder_factory),
ossu20a4b3f2017-04-27 09:08:52234 decoder_factory_(decoder_factory),
deadbeefeb02c032017-06-15 15:29:25235 audio_mixer_(audio_mixer),
peaha9cc40b2017-06-29 15:32:09236 apm_(audio_processing),
ossu20a4b3f2017-04-27 09:08:52237 voe_wrapper_(voe_wrapper) {
deadbeefeb02c032017-06-15 15:29:25238 // This may be called from any thread, so detach thread checkers.
239 worker_thread_checker_.DetachFromThread();
solenberg26c8c912015-11-27 12:00:25240 signal_thread_checker_.DetachFromThread();
Mirko Bonadei675513b2017-11-09 10:09:25241 RTC_LOG(LS_INFO) << "WebRtcVoiceEngine::WebRtcVoiceEngine";
deadbeefeb02c032017-06-15 15:29:25242 RTC_DCHECK(decoder_factory);
243 RTC_DCHECK(encoder_factory);
peaha9cc40b2017-06-29 15:32:09244 RTC_DCHECK(audio_processing);
deadbeefeb02c032017-06-15 15:29:25245 // The rest of our initialization will happen in Init.
246}
247
248WebRtcVoiceEngine::~WebRtcVoiceEngine() {
249 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
Mirko Bonadei675513b2017-11-09 10:09:25250 RTC_LOG(LS_INFO) << "WebRtcVoiceEngine::~WebRtcVoiceEngine";
deadbeefeb02c032017-06-15 15:29:25251 if (initialized_) {
252 StopAecDump();
253 voe_wrapper_->base()->Terminate();
Fredrik Solenbergd3195342017-11-21 19:33:05254
255 // Stop AudioDevice.
256 adm()->StopPlayout();
257 adm()->StopRecording();
258 adm()->RegisterAudioCallback(nullptr);
259 adm()->Terminate();
deadbeefeb02c032017-06-15 15:29:25260 }
261}
262
263void WebRtcVoiceEngine::Init() {
264 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
Mirko Bonadei675513b2017-11-09 10:09:25265 RTC_LOG(LS_INFO) << "WebRtcVoiceEngine::Init";
deadbeefeb02c032017-06-15 15:29:25266
267 // TaskQueue expects to be created/destroyed on the same thread.
268 low_priority_worker_queue_.reset(
269 new rtc::TaskQueue("rtc-low-prio", rtc::TaskQueue::Priority::LOW));
270
271 // VoEWrapper needs to be created on the worker thread. It's expected to be
272 // null here unless it's being injected for testing.
273 if (!voe_wrapper_) {
274 voe_wrapper_.reset(new VoEWrapper());
275 }
solenberg26c8c912015-11-27 12:00:25276
ossueb1fde42017-05-02 13:46:30277 // Load our audio codec lists.
Mirko Bonadei675513b2017-11-09 10:09:25278 RTC_LOG(LS_INFO) << "Supported send codecs in order of preference:";
ossu20a4b3f2017-04-27 09:08:52279 send_codecs_ = CollectCodecs(encoder_factory_->GetSupportedEncoders());
ossuc54071d2016-08-17 09:45:41280 for (const AudioCodec& codec : send_codecs_) {
Mirko Bonadei675513b2017-11-09 10:09:25281 RTC_LOG(LS_INFO) << ToString(codec);
ossuc54071d2016-08-17 09:45:41282 }
283
Mirko Bonadei675513b2017-11-09 10:09:25284 RTC_LOG(LS_INFO) << "Supported recv codecs in order of preference:";
ossu20a4b3f2017-04-27 09:08:52285 recv_codecs_ = CollectCodecs(decoder_factory_->GetSupportedDecoders());
ossuc54071d2016-08-17 09:45:41286 for (const AudioCodec& codec : recv_codecs_) {
Mirko Bonadei675513b2017-11-09 10:09:25287 RTC_LOG(LS_INFO) << ToString(codec);
buildbot@webrtc.org13d67762014-05-02 17:33:29288 }
buildbot@webrtc.org13d67762014-05-02 17:33:29289
solenberg88499ec2016-09-07 14:34:41290 channel_config_.enable_voice_pacing = true;
buildbot@webrtc.org13d67762014-05-02 17:33:29291
Fredrik Solenbergd3195342017-11-21 19:33:05292#if defined(WEBRTC_INCLUDE_INTERNAL_AUDIO_DEVICE)
293 // No ADM supplied? Create a default one.
solenbergff976312016-03-31 06:28:51294 if (!adm_) {
Fredrik Solenbergd3195342017-11-21 19:33:05295 adm_ = webrtc::AudioDeviceModule::Create(
296 webrtc::AudioDeviceModule::kPlatformDefaultAudio);
solenbergff976312016-03-31 06:28:51297 }
Fredrik Solenbergd3195342017-11-21 19:33:05298#endif // WEBRTC_INCLUDE_INTERNAL_AUDIO_DEVICE
299 RTC_CHECK(adm());
300 webrtc::adm_helpers::Init(adm());
Fredrik Solenberg55900fd2017-11-23 19:22:55301 webrtc::apm_helpers::Init(apm());
Fredrik Solenbergd3195342017-11-21 19:33:05302 RTC_CHECK_EQ(0, voe_wrapper_->base()->Init(adm(), apm(), decoder_factory_));
solenberg76377c52017-02-21 08:54:31303 transmit_mixer_ = voe_wrapper_->base()->transmit_mixer();
304 RTC_DCHECK(transmit_mixer_);
305
buildbot@webrtc.org13d67762014-05-02 17:33:29306 // Save the default AGC configuration settings. This must happen before
solenberg246b8172015-12-08 17:50:23307 // calling ApplyOptions or the default will be overwritten.
peaha9cc40b2017-06-29 15:32:09308 default_agc_config_ = webrtc::apm_helpers::GetAgcConfig(apm());
buildbot@webrtc.org13d67762014-05-02 17:33:29309
solenberg0f7d2932016-01-15 09:40:39310 // Set default engine options.
311 {
312 AudioOptions options;
Oskar Sundbom78807582017-11-16 10:09:55313 options.echo_cancellation = true;
314 options.auto_gain_control = true;
315 options.noise_suppression = true;
316 options.highpass_filter = true;
317 options.stereo_swapping = false;
318 options.audio_jitter_buffer_max_packets = 50;
319 options.audio_jitter_buffer_fast_accelerate = false;
320 options.typing_detection = true;
321 options.adjust_agc_delta = 0;
322 options.experimental_agc = false;
323 options.extended_filter_aec = false;
324 options.delay_agnostic_aec = false;
325 options.experimental_ns = false;
326 options.intelligibility_enhancer = false;
327 options.level_control = false;
328 options.residual_echo_detector = true;
solenbergff976312016-03-31 06:28:51329 bool error = ApplyOptions(options);
330 RTC_DCHECK(error);
buildbot@webrtc.org13d67762014-05-02 17:33:29331 }
332
deadbeefeb02c032017-06-15 15:29:25333 // May be null for VoE injected for testing.
334 if (voe()->engine()) {
peaha9cc40b2017-06-29 15:32:09335 audio_state_ = webrtc::AudioState::Create(
336 MakeAudioStateConfig(voe(), audio_mixer_, apm_));
Fredrik Solenbergd3195342017-11-21 19:33:05337
338 // Connect the ADM to our audio path.
339 adm()->RegisterAudioCallback(audio_state_->audio_transport());
deadbeefeb02c032017-06-15 15:29:25340 }
341
342 initialized_ = true;
buildbot@webrtc.org13d67762014-05-02 17:33:29343}
344
solenberg566ef242015-11-06 23:34:49345rtc::scoped_refptr<webrtc::AudioState>
346 WebRtcVoiceEngine::GetAudioState() const {
347 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
348 return audio_state_;
349}
350
nisse51542be2016-02-12 10:27:06351VoiceMediaChannel* WebRtcVoiceEngine::CreateChannel(
352 webrtc::Call* call,
353 const MediaConfig& config,
Jelena Marusicc28a8962015-05-29 13:05:44354 const AudioOptions& options) {
solenberg566ef242015-11-06 23:34:49355 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
nisse51542be2016-02-12 10:27:06356 return new WebRtcVoiceMediaChannel(this, config, options, call);
buildbot@webrtc.org13d67762014-05-02 17:33:29357}
358
buildbot@webrtc.org13d67762014-05-02 17:33:29359bool WebRtcVoiceEngine::ApplyOptions(const AudioOptions& options_in) {
solenberg566ef242015-11-06 23:34:49360 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
Mirko Bonadei675513b2017-11-09 10:09:25361 RTC_LOG(LS_INFO) << "WebRtcVoiceEngine::ApplyOptions: "
362 << options_in.ToString();
solenberg0f7d2932016-01-15 09:40:39363 AudioOptions options = options_in; // The options are modified below.
solenberg246b8172015-12-08 17:50:23364
peah8a8ebd92017-05-22 22:48:47365 // Set and adjust echo canceller options.
buildbot@webrtc.org13d67762014-05-02 17:33:29366 // kEcConference is AEC with high suppression.
367 webrtc::EcModes ec_mode = webrtc::kEcConference;
kwiberg102c6a62015-10-30 09:47:38368 if (options.aecm_generate_comfort_noise) {
Mirko Bonadei675513b2017-11-09 10:09:25369 RTC_LOG(LS_VERBOSE) << "Comfort noise explicitly set to "
370 << *options.aecm_generate_comfort_noise
371 << " (default is false).";
buildbot@webrtc.org13d67762014-05-02 17:33:29372 }
373
kjellanderfcfc8042016-01-14 19:01:09374#if defined(WEBRTC_IOS)
peah8a8ebd92017-05-22 22:48:47375 // On iOS, VPIO provides built-in EC.
Oskar Sundbom78807582017-11-16 10:09:55376 options.echo_cancellation = false;
377 options.extended_filter_aec = false;
Mirko Bonadei675513b2017-11-09 10:09:25378 RTC_LOG(LS_INFO) << "Always disable AEC on iOS. Use built-in instead.";
Mirko Bonadeic8c71b92017-10-16 09:08:54379#elif defined(WEBRTC_ANDROID)
buildbot@webrtc.org13d67762014-05-02 17:33:29380 ec_mode = webrtc::kEcAecm;
Oskar Sundbom78807582017-11-16 10:09:55381 options.extended_filter_aec = false;
buildbot@webrtc.org13d67762014-05-02 17:33:29382#endif
383
Bjorn Volckerbf395c12015-03-25 21:45:56384 // Delay Agnostic AEC automatically turns on EC if not set except on iOS
385 // where the feature is not supported.
386 bool use_delay_agnostic_aec = false;
kjellanderfcfc8042016-01-14 19:01:09387#if !defined(WEBRTC_IOS)
kwiberg102c6a62015-10-30 09:47:38388 if (options.delay_agnostic_aec) {
389 use_delay_agnostic_aec = *options.delay_agnostic_aec;
Bjorn Volckerbf395c12015-03-25 21:45:56390 if (use_delay_agnostic_aec) {
Oskar Sundbom78807582017-11-16 10:09:55391 options.echo_cancellation = true;
392 options.extended_filter_aec = true;
Bjorn Volckerbf395c12015-03-25 21:45:56393 ec_mode = webrtc::kEcConference;
394 }
395 }
396#endif
397
peah8a8ebd92017-05-22 22:48:47398// Set and adjust noise suppressor options.
399#if defined(WEBRTC_IOS)
400 // On iOS, VPIO provides built-in NS.
Oskar Sundbom78807582017-11-16 10:09:55401 options.noise_suppression = false;
402 options.typing_detection = false;
403 options.experimental_ns = false;
Mirko Bonadei675513b2017-11-09 10:09:25404 RTC_LOG(LS_INFO) << "Always disable NS on iOS. Use built-in instead.";
Mirko Bonadeic8c71b92017-10-16 09:08:54405#elif defined(WEBRTC_ANDROID)
Oskar Sundbom78807582017-11-16 10:09:55406 options.typing_detection = false;
407 options.experimental_ns = false;
peah8a8ebd92017-05-22 22:48:47408#endif
409
410// Set and adjust gain control options.
411#if defined(WEBRTC_IOS)
412 // On iOS, VPIO provides built-in AGC.
Oskar Sundbom78807582017-11-16 10:09:55413 options.auto_gain_control = false;
414 options.experimental_agc = false;
Mirko Bonadei675513b2017-11-09 10:09:25415 RTC_LOG(LS_INFO) << "Always disable AGC on iOS. Use built-in instead.";
Mirko Bonadeic8c71b92017-10-16 09:08:54416#elif defined(WEBRTC_ANDROID)
Oskar Sundbom78807582017-11-16 10:09:55417 options.experimental_agc = false;
peah8a8ebd92017-05-22 22:48:47418#endif
419
420#if defined(WEBRTC_IOS) || defined(WEBRTC_ANDROID)
Mirko Bonadeic8c71b92017-10-16 09:08:54421 // Turn off the gain control if specified by the field trial.
422 // The purpose of the field trial is to reduce the amount of resampling
423 // performed inside the audio processing module on mobile platforms by
424 // whenever possible turning off the fixed AGC mode and the high-pass filter.
425 // (https://bugs.chromium.org/p/webrtc/issues/detail?id=6181).
peah8a8ebd92017-05-22 22:48:47426 if (webrtc::field_trial::IsEnabled(
427 "WebRTC-Audio-MinimizeResamplingOnMobile")) {
Oskar Sundbom78807582017-11-16 10:09:55428 options.auto_gain_control = false;
Mirko Bonadei675513b2017-11-09 10:09:25429 RTC_LOG(LS_INFO) << "Disable AGC according to field trial.";
Steve Antone78bcb92017-10-31 16:53:08430 if (!(options.noise_suppression.value_or(false) ||
peah8a8ebd92017-05-22 22:48:47431 options.echo_cancellation.value_or(false))) {
432 // If possible, turn off the high-pass filter.
Mirko Bonadei675513b2017-11-09 10:09:25433 RTC_LOG(LS_INFO)
434 << "Disable high-pass filter in response to field trial.";
Oskar Sundbom78807582017-11-16 10:09:55435 options.highpass_filter = false;
peah8a8ebd92017-05-22 22:48:47436 }
437 }
438#endif
439
peah1bcfce52016-08-26 14:16:04440#if (WEBRTC_INTELLIGIBILITY_ENHANCER == 0)
441 // Hardcode the intelligibility enhancer to be off.
Oskar Sundbom78807582017-11-16 10:09:55442 options.intelligibility_enhancer = false;
peah1bcfce52016-08-26 14:16:04443#endif
444
kwiberg102c6a62015-10-30 09:47:38445 if (options.echo_cancellation) {
henrika@webrtc.orga954c072014-12-09 16:22:09446 // Check if platform supports built-in EC. Currently only supported on
447 // Android and in combination with Java based audio layer.
448 // TODO(henrika): investigate possibility to support built-in EC also
449 // in combination with Open SL ES audio.
solenberg5b5129a2016-04-08 12:35:48450 const bool built_in_aec = adm()->BuiltInAECIsAvailable();
Bjorn Volcker73f72102015-06-03 12:50:15451 if (built_in_aec) {
Bjorn Volckerccfc9392015-05-07 05:43:17452 // Built-in EC exists on this device and use_delay_agnostic_aec is not
453 // overriding it. Enable/Disable it according to the echo_cancellation
454 // audio option.
Bjorn Volcker73f72102015-06-03 12:50:15455 const bool enable_built_in_aec =
kwiberg102c6a62015-10-30 09:47:38456 *options.echo_cancellation && !use_delay_agnostic_aec;
solenberg5b5129a2016-04-08 12:35:48457 if (adm()->EnableBuiltInAEC(enable_built_in_aec) == 0 &&
Bjorn Volcker73f72102015-06-03 12:50:15458 enable_built_in_aec) {
Bjorn Volckerbf395c12015-03-25 21:45:56459 // Disable internal software EC if built-in EC is enabled,
henrika@webrtc.orga954c072014-12-09 16:22:09460 // i.e., replace the software EC with the built-in EC.
Oskar Sundbom78807582017-11-16 10:09:55461 options.echo_cancellation = false;
Mirko Bonadei675513b2017-11-09 10:09:25462 RTC_LOG(LS_INFO)
463 << "Disabling EC since built-in EC will be used instead";
henrika@webrtc.orga954c072014-12-09 16:22:09464 }
465 }
solenberg76377c52017-02-21 08:54:31466 webrtc::apm_helpers::SetEcStatus(
467 apm(), *options.echo_cancellation, ec_mode);
Mirko Bonadeic8c71b92017-10-16 09:08:54468#if !defined(WEBRTC_ANDROID)
solenberg76377c52017-02-21 08:54:31469 webrtc::apm_helpers::SetEcMetricsStatus(apm(), *options.echo_cancellation);
buildbot@webrtc.org13d67762014-05-02 17:33:29470#endif
471 if (ec_mode == webrtc::kEcAecm) {
kwiberg102c6a62015-10-30 09:47:38472 bool cn = options.aecm_generate_comfort_noise.value_or(false);
solenberg76377c52017-02-21 08:54:31473 webrtc::apm_helpers::SetAecmMode(apm(), cn);
buildbot@webrtc.org13d67762014-05-02 17:33:29474 }
475 }
476
kwiberg102c6a62015-10-30 09:47:38477 if (options.auto_gain_control) {
peah72a56452016-08-22 19:08:55478 bool built_in_agc_avaliable = adm()->BuiltInAGCIsAvailable();
479 if (built_in_agc_avaliable) {
solenberg5b5129a2016-04-08 12:35:48480 if (adm()->EnableBuiltInAGC(*options.auto_gain_control) == 0 &&
kwiberg102c6a62015-10-30 09:47:38481 *options.auto_gain_control) {
henrikac14f5ff2015-09-23 12:08:33482 // Disable internal software AGC if built-in AGC is enabled,
483 // i.e., replace the software AGC with the built-in AGC.
Oskar Sundbom78807582017-11-16 10:09:55484 options.auto_gain_control = false;
Mirko Bonadei675513b2017-11-09 10:09:25485 RTC_LOG(LS_INFO)
486 << "Disabling AGC since built-in AGC will be used instead";
henrikac14f5ff2015-09-23 12:08:33487 }
488 }
solenberg22818a52017-03-16 08:20:23489 webrtc::apm_helpers::SetAgcStatus(apm(), adm(), *options.auto_gain_control);
buildbot@webrtc.org13d67762014-05-02 17:33:29490 }
491
kwiberg102c6a62015-10-30 09:47:38492 if (options.tx_agc_target_dbov || options.tx_agc_digital_compression_gain ||
solenberg76377c52017-02-21 08:54:31493 options.tx_agc_limiter || options.adjust_agc_delta) {
buildbot@webrtc.org13d67762014-05-02 17:33:29494 // Override default_agc_config_. Generally, an unset option means "leave
495 // the VoE bits alone" in this function, so we want whatever is set to be
496 // stored as the new "default". If we didn't, then setting e.g.
497 // tx_agc_target_dbov would reset digital compression gain and limiter
498 // settings.
499 // Also, if we don't update default_agc_config_, then adjust_agc_delta
500 // would be an offset from the original values, and not whatever was set
501 // explicitly.
kwiberg102c6a62015-10-30 09:47:38502 default_agc_config_.targetLeveldBOv = options.tx_agc_target_dbov.value_or(
503 default_agc_config_.targetLeveldBOv);
buildbot@webrtc.org13d67762014-05-02 17:33:29504 default_agc_config_.digitalCompressionGaindB =
kwiberg102c6a62015-10-30 09:47:38505 options.tx_agc_digital_compression_gain.value_or(
buildbot@webrtc.org13d67762014-05-02 17:33:29506 default_agc_config_.digitalCompressionGaindB);
507 default_agc_config_.limiterEnable =
kwiberg102c6a62015-10-30 09:47:38508 options.tx_agc_limiter.value_or(default_agc_config_.limiterEnable);
solenberg76377c52017-02-21 08:54:31509
510 webrtc::AgcConfig config = default_agc_config_;
511 if (options.adjust_agc_delta) {
512 config.targetLeveldBOv -= *options.adjust_agc_delta;
Mirko Bonadei675513b2017-11-09 10:09:25513 RTC_LOG(LS_INFO) << "Adjusting AGC level from default -"
514 << default_agc_config_.targetLeveldBOv << "dB to -"
515 << config.targetLeveldBOv << "dB";
buildbot@webrtc.org13d67762014-05-02 17:33:29516 }
peaha9cc40b2017-06-29 15:32:09517 webrtc::apm_helpers::SetAgcConfig(apm(), config);
buildbot@webrtc.org13d67762014-05-02 17:33:29518 }
519
Alejandro Luebsc9b0c262016-05-16 22:32:38520 if (options.intelligibility_enhancer) {
521 intelligibility_enhancer_ = options.intelligibility_enhancer;
522 }
523 if (intelligibility_enhancer_ && *intelligibility_enhancer_) {
Mirko Bonadei675513b2017-11-09 10:09:25524 RTC_LOG(LS_INFO) << "Enabling NS when Intelligibility Enhancer is active.";
Alejandro Luebsc9b0c262016-05-16 22:32:38525 options.noise_suppression = intelligibility_enhancer_;
526 }
527
kwiberg102c6a62015-10-30 09:47:38528 if (options.noise_suppression) {
Alejandro Luebsc9b0c262016-05-16 22:32:38529 if (adm()->BuiltInNSIsAvailable()) {
530 bool builtin_ns =
531 *options.noise_suppression &&
532 !(intelligibility_enhancer_ && *intelligibility_enhancer_);
533 if (adm()->EnableBuiltInNS(builtin_ns) == 0 && builtin_ns) {
henrikac14f5ff2015-09-23 12:08:33534 // Disable internal software NS if built-in NS is enabled,
535 // i.e., replace the software NS with the built-in NS.
Oskar Sundbom78807582017-11-16 10:09:55536 options.noise_suppression = false;
Mirko Bonadei675513b2017-11-09 10:09:25537 RTC_LOG(LS_INFO)
538 << "Disabling NS since built-in NS will be used instead";
henrikac14f5ff2015-09-23 12:08:33539 }
540 }
solenberg76377c52017-02-21 08:54:31541 webrtc::apm_helpers::SetNsStatus(apm(), *options.noise_suppression);
buildbot@webrtc.org13d67762014-05-02 17:33:29542 }
543
kwiberg102c6a62015-10-30 09:47:38544 if (options.stereo_swapping) {
Mirko Bonadei675513b2017-11-09 10:09:25545 RTC_LOG(LS_INFO) << "Stereo swapping enabled? " << *options.stereo_swapping;
solenberg76377c52017-02-21 08:54:31546 transmit_mixer()->EnableStereoChannelSwapping(*options.stereo_swapping);
buildbot@webrtc.org13d67762014-05-02 17:33:29547 }
548
kwiberg102c6a62015-10-30 09:47:38549 if (options.audio_jitter_buffer_max_packets) {
Mirko Bonadei675513b2017-11-09 10:09:25550 RTC_LOG(LS_INFO) << "NetEq capacity is "
551 << *options.audio_jitter_buffer_max_packets;
solenberg88499ec2016-09-07 14:34:41552 channel_config_.acm_config.neteq_config.max_packets_in_buffer =
553 std::max(20, *options.audio_jitter_buffer_max_packets);
Henrik Lundin64dad832015-05-11 10:44:23554 }
kwiberg102c6a62015-10-30 09:47:38555 if (options.audio_jitter_buffer_fast_accelerate) {
Mirko Bonadei675513b2017-11-09 10:09:25556 RTC_LOG(LS_INFO) << "NetEq fast mode? "
557 << *options.audio_jitter_buffer_fast_accelerate;
solenberg88499ec2016-09-07 14:34:41558 channel_config_.acm_config.neteq_config.enable_fast_accelerate =
559 *options.audio_jitter_buffer_fast_accelerate;
Henrik Lundin5263b3c2015-06-01 08:29:41560 }
561
kwiberg102c6a62015-10-30 09:47:38562 if (options.typing_detection) {
Mirko Bonadei675513b2017-11-09 10:09:25563 RTC_LOG(LS_INFO) << "Typing detection is enabled? "
564 << *options.typing_detection;
solenberg76377c52017-02-21 08:54:31565 webrtc::apm_helpers::SetTypingDetectionStatus(
566 apm(), *options.typing_detection);
buildbot@webrtc.org13d67762014-05-02 17:33:29567 }
568
buildbot@webrtc.org1f8a2372014-08-28 10:52:44569 webrtc::Config config;
570
kwiberg102c6a62015-10-30 09:47:38571 if (options.delay_agnostic_aec)
572 delay_agnostic_aec_ = options.delay_agnostic_aec;
573 if (delay_agnostic_aec_) {
Mirko Bonadei675513b2017-11-09 10:09:25574 RTC_LOG(LS_INFO) << "Delay agnostic aec is enabled? "
575 << *delay_agnostic_aec_;
henrik.lundin0f133b92015-07-02 07:17:55576 config.Set<webrtc::DelayAgnostic>(
kwiberg102c6a62015-10-30 09:47:38577 new webrtc::DelayAgnostic(*delay_agnostic_aec_));
Bjorn Volckerbf395c12015-03-25 21:45:56578 }
579
kwiberg102c6a62015-10-30 09:47:38580 if (options.extended_filter_aec) {
581 extended_filter_aec_ = options.extended_filter_aec;
582 }
583 if (extended_filter_aec_) {
Mirko Bonadei675513b2017-11-09 10:09:25584 RTC_LOG(LS_INFO) << "Extended filter aec is enabled? "
585 << *extended_filter_aec_;
Henrik Lundin441f6342015-06-09 14:03:13586 config.Set<webrtc::ExtendedFilter>(
kwiberg102c6a62015-10-30 09:47:38587 new webrtc::ExtendedFilter(*extended_filter_aec_));
buildbot@webrtc.org1f8a2372014-08-28 10:52:44588 }
589
kwiberg102c6a62015-10-30 09:47:38590 if (options.experimental_ns) {
591 experimental_ns_ = options.experimental_ns;
592 }
593 if (experimental_ns_) {
Mirko Bonadei675513b2017-11-09 10:09:25594 RTC_LOG(LS_INFO) << "Experimental ns is enabled? " << *experimental_ns_;
buildbot@webrtc.org1f8a2372014-08-28 10:52:44595 config.Set<webrtc::ExperimentalNs>(
kwiberg102c6a62015-10-30 09:47:38596 new webrtc::ExperimentalNs(*experimental_ns_));
buildbot@webrtc.org1f8a2372014-08-28 10:52:44597 }
buildbot@webrtc.org1f8a2372014-08-28 10:52:44598
Alejandro Luebsc9b0c262016-05-16 22:32:38599 if (intelligibility_enhancer_) {
Mirko Bonadei675513b2017-11-09 10:09:25600 RTC_LOG(LS_INFO) << "Intelligibility Enhancer is enabled? "
601 << *intelligibility_enhancer_;
Alejandro Luebsc9b0c262016-05-16 22:32:38602 config.Set<webrtc::Intelligibility>(
603 new webrtc::Intelligibility(*intelligibility_enhancer_));
604 }
605
peaha3333bf2016-06-30 07:02:34606 if (options.level_control) {
607 level_control_ = options.level_control;
608 }
609
peahb1c9d1d2017-07-25 22:45:24610 webrtc::AudioProcessing::Config apm_config = apm()->GetConfig();
611
Mirko Bonadei675513b2017-11-09 10:09:25612 RTC_LOG(LS_INFO) << "Level control: "
613 << (!!level_control_ ? *level_control_ : -1);
peaha3333bf2016-06-30 07:02:34614 if (level_control_) {
peahb1c9d1d2017-07-25 22:45:24615 apm_config.level_controller.enabled = *level_control_;
aleloie33c5d92016-10-20 08:53:27616 if (options.level_control_initial_peak_level_dbfs) {
peahb1c9d1d2017-07-25 22:45:24617 apm_config.level_controller.initial_peak_level_dbfs =
aleloie33c5d92016-10-20 08:53:27618 *options.level_control_initial_peak_level_dbfs;
619 }
peaha3333bf2016-06-30 07:02:34620 }
621
peah8271d042016-11-22 15:24:52622 if (options.highpass_filter) {
peahb1c9d1d2017-07-25 22:45:24623 apm_config.high_pass_filter.enabled = *options.highpass_filter;
peah8271d042016-11-22 15:24:52624 }
625
ivoc4ca18692017-02-10 13:11:09626 if (options.residual_echo_detector) {
peahb1c9d1d2017-07-25 22:45:24627 apm_config.residual_echo_detector.enabled = *options.residual_echo_detector;
ivoc4ca18692017-02-10 13:11:09628 }
629
solenberg059fb442016-10-26 12:12:24630 apm()->SetExtraOptions(config);
peahb1c9d1d2017-07-25 22:45:24631 apm()->ApplyConfig(apm_config);
buildbot@webrtc.org13d67762014-05-02 17:33:29632 return true;
633}
634
solenberg796b8f92017-03-02 01:02:23635// TODO(solenberg): Remove, once AudioMonitor is gone.
henrike@webrtc.org28e20752013-07-10 00:45:36636int WebRtcVoiceEngine::GetInputLevel() {
solenberg566ef242015-11-06 23:34:49637 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
solenberg796b8f92017-03-02 01:02:23638 int8_t level = transmit_mixer()->AudioLevel();
639 RTC_DCHECK_LE(0, level);
640 return level;
henrike@webrtc.org28e20752013-07-10 00:45:36641}
642
ossudedfd282016-06-14 14:12:39643const std::vector<AudioCodec>& WebRtcVoiceEngine::send_codecs() const {
644 RTC_DCHECK(signal_thread_checker_.CalledOnValidThread());
ossuc54071d2016-08-17 09:45:41645 return send_codecs_;
ossudedfd282016-06-14 14:12:39646}
647
648const std::vector<AudioCodec>& WebRtcVoiceEngine::recv_codecs() const {
solenberg566ef242015-11-06 23:34:49649 RTC_DCHECK(signal_thread_checker_.CalledOnValidThread());
ossuc54071d2016-08-17 09:45:41650 return recv_codecs_;
henrike@webrtc.org28e20752013-07-10 00:45:36651}
652
Stefan Holmer9d69c3f2015-12-07 09:45:43653RtpCapabilities WebRtcVoiceEngine::GetCapabilities() const {
solenberg566ef242015-11-06 23:34:49654 RTC_DCHECK(signal_thread_checker_.CalledOnValidThread());
Stefan Holmer9d69c3f2015-12-07 09:45:43655 RtpCapabilities capabilities;
Stefan Holmer9d69c3f2015-12-07 09:45:43656 capabilities.header_extensions.push_back(
isheriff6f8d6862016-05-26 18:24:55657 webrtc::RtpExtension(webrtc::RtpExtension::kAudioLevelUri,
658 webrtc::RtpExtension::kAudioLevelDefaultId));
sprangc1b57a12017-02-28 16:50:47659 if (webrtc::field_trial::IsEnabled("WebRTC-Audio-SendSideBwe")) {
isheriff6f8d6862016-05-26 18:24:55660 capabilities.header_extensions.push_back(webrtc::RtpExtension(
661 webrtc::RtpExtension::kTransportSequenceNumberUri,
662 webrtc::RtpExtension::kTransportSequenceNumberDefaultId));
stefanba4c0e42016-02-04 12:12:24663 }
Stefan Holmer9d69c3f2015-12-07 09:45:43664 return capabilities;
henrike@webrtc.org28e20752013-07-10 00:45:36665}
666
solenberg63b34542015-09-29 13:06:31667void WebRtcVoiceEngine::RegisterChannel(WebRtcVoiceMediaChannel* channel) {
solenberg566ef242015-11-06 23:34:49668 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
669 RTC_DCHECK(channel);
henrike@webrtc.org28e20752013-07-10 00:45:36670 channels_.push_back(channel);
671}
672
solenberg63b34542015-09-29 13:06:31673void WebRtcVoiceEngine::UnregisterChannel(WebRtcVoiceMediaChannel* channel) {
solenberg566ef242015-11-06 23:34:49674 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
solenberg63b34542015-09-29 13:06:31675 auto it = std::find(channels_.begin(), channels_.end(), channel);
solenberg566ef242015-11-06 23:34:49676 RTC_DCHECK(it != channels_.end());
677 channels_.erase(it);
henrike@webrtc.org28e20752013-07-10 00:45:36678}
679
ivocd66b44d2016-01-15 11:06:36680bool WebRtcVoiceEngine::StartAecDump(rtc::PlatformFile file,
681 int64_t max_size_bytes) {
solenberg566ef242015-11-06 23:34:49682 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
deadbeefeb02c032017-06-15 15:29:25683 auto aec_dump = webrtc::AecDumpFactory::Create(
684 file, max_size_bytes, low_priority_worker_queue_.get());
aleloi048cbdd2017-05-29 09:56:27685 if (!aec_dump) {
wu@webrtc.orga8910d22014-01-23 22:12:45686 return false;
687 }
aleloi048cbdd2017-05-29 09:56:27688 apm()->AttachAecDump(std::move(aec_dump));
wu@webrtc.orga9890802013-12-13 00:21:03689 return true;
wu@webrtc.orga9890802013-12-13 00:21:03690}
691
henrike@webrtc.org28e20752013-07-10 00:45:36692void WebRtcVoiceEngine::StartAecDump(const std::string& filename) {
solenberg566ef242015-11-06 23:34:49693 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
aleloi048cbdd2017-05-29 09:56:27694
deadbeefeb02c032017-06-15 15:29:25695 auto aec_dump = webrtc::AecDumpFactory::Create(
696 filename, -1, low_priority_worker_queue_.get());
aleloi048cbdd2017-05-29 09:56:27697 if (aec_dump) {
698 apm()->AttachAecDump(std::move(aec_dump));
henrike@webrtc.org28e20752013-07-10 00:45:36699 }
700}
701
702void WebRtcVoiceEngine::StopAecDump() {
solenberg566ef242015-11-06 23:34:49703 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
aleloi048cbdd2017-05-29 09:56:27704 apm()->DetachAecDump();
henrike@webrtc.org28e20752013-07-10 00:45:36705}
706
solenberg0a617e22015-10-20 22:49:38707int WebRtcVoiceEngine::CreateVoEChannel() {
solenberg566ef242015-11-06 23:34:49708 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
solenberg88499ec2016-09-07 14:34:41709 return voe_wrapper_->base()->CreateChannel(channel_config_);
sergeyu@chromium.org5bc25c42013-12-05 00:24:06710}
711
solenberg5b5129a2016-04-08 12:35:48712webrtc::AudioDeviceModule* WebRtcVoiceEngine::adm() {
713 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
714 RTC_DCHECK(adm_);
Fredrik Solenbergd3195342017-11-21 19:33:05715 return adm_.get();
solenberg5b5129a2016-04-08 12:35:48716}
717
peahb1c9d1d2017-07-25 22:45:24718webrtc::AudioProcessing* WebRtcVoiceEngine::apm() const {
solenberg059fb442016-10-26 12:12:24719 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
Fredrik Solenberg55900fd2017-11-23 19:22:55720 RTC_DCHECK(apm_);
peaha9cc40b2017-06-29 15:32:09721 return apm_.get();
solenberg059fb442016-10-26 12:12:24722}
723
solenberg76377c52017-02-21 08:54:31724webrtc::voe::TransmitMixer* WebRtcVoiceEngine::transmit_mixer() {
725 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
726 RTC_DCHECK(transmit_mixer_);
727 return transmit_mixer_;
728}
729
ossu20a4b3f2017-04-27 09:08:52730AudioCodecs WebRtcVoiceEngine::CollectCodecs(
731 const std::vector<webrtc::AudioCodecSpec>& specs) const {
ossuc54071d2016-08-17 09:45:41732 PayloadTypeMapper mapper;
733 AudioCodecs out;
ossuc54071d2016-08-17 09:45:41734
solenberg2779bab2016-11-17 12:45:19735 // Only generate CN payload types for these clockrates:
ossuc54071d2016-08-17 09:45:41736 std::map<int, bool, std::greater<int>> generate_cn = {{ 8000, false },
737 { 16000, false },
738 { 32000, false }};
solenberg2779bab2016-11-17 12:45:19739 // Only generate telephone-event payload types for these clockrates:
740 std::map<int, bool, std::greater<int>> generate_dtmf = {{ 8000, false },
741 { 16000, false },
742 { 32000, false },
743 { 48000, false }};
ossuc54071d2016-08-17 09:45:41744
ossu9def8002017-02-09 13:14:32745 auto map_format = [&mapper](const webrtc::SdpAudioFormat& format,
746 AudioCodecs* out) {
ossuc54071d2016-08-17 09:45:41747 rtc::Optional<AudioCodec> opt_codec = mapper.ToAudioCodec(format);
ossu9def8002017-02-09 13:14:32748 if (opt_codec) {
749 if (out) {
750 out->push_back(*opt_codec);
751 }
752 } else {
Mirko Bonadei675513b2017-11-09 10:09:25753 RTC_LOG(LS_ERROR) << "Unable to assign payload type to format: "
754 << format;
ossuc54071d2016-08-17 09:45:41755 }
756
ossu9def8002017-02-09 13:14:32757 return opt_codec;
ossuc54071d2016-08-17 09:45:41758 };
759
ossud4e9f622016-08-18 09:01:17760 for (const auto& spec : specs) {
ossu9def8002017-02-09 13:14:32761 // We need to do some extra stuff before adding the main codecs to out.
762 rtc::Optional<AudioCodec> opt_codec = map_format(spec.format, nullptr);
763 if (opt_codec) {
764 AudioCodec& codec = *opt_codec;
ossua1a040a2017-04-06 17:03:21765 if (spec.info.supports_network_adaption) {
ossu9def8002017-02-09 13:14:32766 codec.AddFeedbackParam(
767 FeedbackParam(kRtcpFbParamTransportCc, kParamValueEmpty));
768 }
769
ossua1a040a2017-04-06 17:03:21770 if (spec.info.allow_comfort_noise) {
solenberg2779bab2016-11-17 12:45:19771 // Generate a CN entry if the decoder allows it and we support the
772 // clockrate.
773 auto cn = generate_cn.find(spec.format.clockrate_hz);
774 if (cn != generate_cn.end()) {
775 cn->second = true;
776 }
777 }
778
779 // Generate a telephone-event entry if we support the clockrate.
780 auto dtmf = generate_dtmf.find(spec.format.clockrate_hz);
781 if (dtmf != generate_dtmf.end()) {
782 dtmf->second = true;
ossuc54071d2016-08-17 09:45:41783 }
ossu9def8002017-02-09 13:14:32784
785 out.push_back(codec);
ossuc54071d2016-08-17 09:45:41786 }
787 }
788
solenberg2779bab2016-11-17 12:45:19789 // Add CN codecs after "proper" audio codecs.
ossuc54071d2016-08-17 09:45:41790 for (const auto& cn : generate_cn) {
791 if (cn.second) {
ossu9def8002017-02-09 13:14:32792 map_format({kCnCodecName, cn.first, 1}, &out);
ossuc54071d2016-08-17 09:45:41793 }
794 }
795
solenberg2779bab2016-11-17 12:45:19796 // Add telephone-event codecs last.
797 for (const auto& dtmf : generate_dtmf) {
798 if (dtmf.second) {
ossu9def8002017-02-09 13:14:32799 map_format({kDtmfCodecName, dtmf.first, 1}, &out);
solenberg2779bab2016-11-17 12:45:19800 }
801 }
ossuc54071d2016-08-17 09:45:41802
803 return out;
804}
805
solenbergc96df772015-10-21 20:01:53806class WebRtcVoiceMediaChannel::WebRtcAudioSendStream
Taylor Brandstetter1a018dc2016-03-08 20:37:39807 : public AudioSource::Sink {
mallinath@webrtc.org67ee6b92014-02-03 16:57:16808 public:
minyue7a973442016-10-20 10:27:12809 WebRtcAudioSendStream(
810 int ch,
811 webrtc::AudioTransport* voe_audio_transport,
812 uint32_t ssrc,
813 const std::string& c_name,
Alex Narestb3944f02017-10-13 12:56:18814 const std::string track_id,
ossu20a4b3f2017-04-27 09:08:52815 const rtc::Optional<webrtc::AudioSendStream::Config::SendCodecSpec>&
816 send_codec_spec,
minyue7a973442016-10-20 10:27:12817 const std::vector<webrtc::RtpExtension>& extensions,
818 int max_send_bitrate_bps,
minyue6b825df2016-10-31 11:08:32819 const rtc::Optional<std::string>& audio_network_adaptor_config,
minyue7a973442016-10-20 10:27:12820 webrtc::Call* call,
ossu20a4b3f2017-04-27 09:08:52821 webrtc::Transport* send_transport,
822 const rtc::scoped_refptr<webrtc::AudioEncoderFactory>& encoder_factory)
solenberg7add0582015-11-20 17:59:34823 : voe_audio_transport_(voe_audio_transport),
solenberg3a941542015-11-16 15:34:50824 call_(call),
mflodman3d7db262016-04-29 07:57:13825 config_(send_transport),
sprangc1b57a12017-02-28 16:50:47826 send_side_bwe_with_overhead_(
827 webrtc::field_trial::IsEnabled("WebRTC-SendSideBwe-WithOverhead")),
minyue7a973442016-10-20 10:27:12828 max_send_bitrate_bps_(max_send_bitrate_bps),
skvlade0d46372016-04-08 05:59:22829 rtp_parameters_(CreateRtpParametersWithOneEncoding()) {
solenberg85a04962015-10-27 10:35:21830 RTC_DCHECK_GE(ch, 0);
831 // TODO(solenberg): Once we're not using FakeWebRtcVoiceEngine anymore:
832 // RTC_DCHECK(voe_audio_transport);
solenbergc96df772015-10-21 20:01:53833 RTC_DCHECK(call);
ossu20a4b3f2017-04-27 09:08:52834 RTC_DCHECK(encoder_factory);
solenberg3a941542015-11-16 15:34:50835 config_.rtp.ssrc = ssrc;
836 config_.rtp.c_name = c_name;
837 config_.voe_channel_id = ch;
solenberg971cab02016-06-14 17:02:41838 config_.rtp.extensions = extensions;
minyue6b825df2016-10-31 11:08:32839 config_.audio_network_adaptor_config = audio_network_adaptor_config;
ossu20a4b3f2017-04-27 09:08:52840 config_.encoder_factory = encoder_factory;
Alex Narestb3944f02017-10-13 12:56:18841 config_.track_id = track_id;
Oskar Sundbom78807582017-11-16 10:09:55842 rtp_parameters_.encodings[0].ssrc = ssrc;
ossu20a4b3f2017-04-27 09:08:52843
844 if (send_codec_spec) {
845 UpdateSendCodecSpec(*send_codec_spec);
846 }
847
848 stream_ = call_->CreateAudioSendStream(config_);
solenbergc96df772015-10-21 20:01:53849 }
solenberg3a941542015-11-16 15:34:50850
solenbergc96df772015-10-21 20:01:53851 ~WebRtcAudioSendStream() override {
solenberg566ef242015-11-06 23:34:49852 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
Taylor Brandstetter1a018dc2016-03-08 20:37:39853 ClearSource();
solenbergc96df772015-10-21 20:01:53854 call_->DestroyAudioSendStream(stream_);
855 }
henrike@webrtc.org1e09a712013-07-26 19:17:59856
ossu20a4b3f2017-04-27 09:08:52857 void SetSendCodecSpec(
minyue7a973442016-10-20 10:27:12858 const webrtc::AudioSendStream::Config::SendCodecSpec& send_codec_spec) {
ossu20a4b3f2017-04-27 09:08:52859 UpdateSendCodecSpec(send_codec_spec);
860 ReconfigureAudioSendStream();
solenberg971cab02016-06-14 17:02:41861 }
862
ossu20a4b3f2017-04-27 09:08:52863 void SetRtpExtensions(const std::vector<webrtc::RtpExtension>& extensions) {
solenberg3a941542015-11-16 15:34:50864 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
solenberg3a941542015-11-16 15:34:50865 config_.rtp.extensions = extensions;
ossu20a4b3f2017-04-27 09:08:52866 ReconfigureAudioSendStream();
solenberg3a941542015-11-16 15:34:50867 }
868
ossu20a4b3f2017-04-27 09:08:52869 void SetAudioNetworkAdaptorConfig(
minyue6b825df2016-10-31 11:08:32870 const rtc::Optional<std::string>& audio_network_adaptor_config) {
871 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
872 if (config_.audio_network_adaptor_config == audio_network_adaptor_config) {
873 return;
874 }
875 config_.audio_network_adaptor_config = audio_network_adaptor_config;
ossu20a4b3f2017-04-27 09:08:52876 UpdateAllowedBitrateRange();
877 ReconfigureAudioSendStream();
minyue6b825df2016-10-31 11:08:32878 }
879
minyue7a973442016-10-20 10:27:12880 bool SetMaxSendBitrate(int bps) {
881 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
ossu20a4b3f2017-04-27 09:08:52882 RTC_DCHECK(config_.send_codec_spec);
883 RTC_DCHECK(audio_codec_spec_);
884 auto send_rate = ComputeSendBitrate(
885 bps, rtp_parameters_.encodings[0].max_bitrate_bps, *audio_codec_spec_);
886
minyue7a973442016-10-20 10:27:12887 if (!send_rate) {
888 return false;
889 }
890
891 max_send_bitrate_bps_ = bps;
892
ossu20a4b3f2017-04-27 09:08:52893 if (send_rate != config_.send_codec_spec->target_bitrate_bps) {
894 config_.send_codec_spec->target_bitrate_bps = send_rate;
895 ReconfigureAudioSendStream();
minyue7a973442016-10-20 10:27:12896 }
897 return true;
898 }
899
solenbergffbbcac2016-11-17 13:25:37900 bool SendTelephoneEvent(int payload_type, int payload_freq, int event,
901 int duration_ms) {
Fredrik Solenbergb5727682015-12-04 14:22:19902 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
903 RTC_DCHECK(stream_);
solenbergffbbcac2016-11-17 13:25:37904 return stream_->SendTelephoneEvent(payload_type, payload_freq, event,
905 duration_ms);
Fredrik Solenbergb5727682015-12-04 14:22:19906 }
907
Taylor Brandstetter1a018dc2016-03-08 20:37:39908 void SetSend(bool send) {
909 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
910 send_ = send;
911 UpdateSendState();
912 }
913
solenberg94218532016-06-16 17:53:22914 void SetMuted(bool muted) {
915 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
916 RTC_DCHECK(stream_);
917 stream_->SetMuted(muted);
918 muted_ = muted;
919 }
920
921 bool muted() const {
922 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
923 return muted_;
924 }
925
Ivo Creusen56d460902017-11-24 16:29:59926 webrtc::AudioSendStream::Stats GetStats(bool has_remote_tracks) const {
solenberg3a941542015-11-16 15:34:50927 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
928 RTC_DCHECK(stream_);
Ivo Creusen56d460902017-11-24 16:29:59929 return stream_->GetStats(has_remote_tracks);
solenberg3a941542015-11-16 15:34:50930 }
931
Taylor Brandstetter1a018dc2016-03-08 20:37:39932 // Starts the sending by setting ourselves as a sink to the AudioSource to
933 // get data callbacks.
henrike@webrtc.orga7b98182014-02-21 15:51:43934 // This method is called on the libjingle worker thread.
mallinath@webrtc.org67ee6b92014-02-03 16:57:16935 // TODO(xians): Make sure Start() is called only once.
Taylor Brandstetter1a018dc2016-03-08 20:37:39936 void SetSource(AudioSource* source) {
solenberg566ef242015-11-06 23:34:49937 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
Taylor Brandstetter1a018dc2016-03-08 20:37:39938 RTC_DCHECK(source);
939 if (source_) {
940 RTC_DCHECK(source_ == source);
mallinath@webrtc.org67ee6b92014-02-03 16:57:16941 return;
942 }
Taylor Brandstetter1a018dc2016-03-08 20:37:39943 source->SetSink(this);
944 source_ = source;
945 UpdateSendState();
mallinath@webrtc.org67ee6b92014-02-03 16:57:16946 }
947
Taylor Brandstetter1a018dc2016-03-08 20:37:39948 // Stops sending by setting the sink of the AudioSource to nullptr. No data
mallinath@webrtc.org67ee6b92014-02-03 16:57:16949 // callback will be received after this method.
henrike@webrtc.orga7b98182014-02-21 15:51:43950 // This method is called on the libjingle worker thread.
Taylor Brandstetter1a018dc2016-03-08 20:37:39951 void ClearSource() {
solenberg566ef242015-11-06 23:34:49952 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
Taylor Brandstetter1a018dc2016-03-08 20:37:39953 if (source_) {
954 source_->SetSink(nullptr);
955 source_ = nullptr;
solenberg98c68862015-10-09 10:27:14956 }
Taylor Brandstetter1a018dc2016-03-08 20:37:39957 UpdateSendState();
mallinath@webrtc.org67ee6b92014-02-03 16:57:16958 }
959
Taylor Brandstetter1a018dc2016-03-08 20:37:39960 // AudioSource::Sink implementation.
henrike@webrtc.orga7b98182014-02-21 15:51:43961 // This method is called on the audio thread.
kjellander@webrtc.org14665ff2015-03-04 12:58:35962 void OnData(const void* audio_data,
963 int bits_per_sample,
964 int sample_rate,
Peter Kasting69558702016-01-13 00:26:35965 size_t number_of_channels,
Peter Kastingdce40cf2015-08-24 21:52:23966 size_t number_of_frames) override {
solenberg347ec5c2016-09-23 11:21:47967 RTC_CHECK_RUNS_SERIALIZED(&audio_capture_race_checker_);
solenbergc96df772015-10-21 20:01:53968 RTC_DCHECK(voe_audio_transport_);
maxmorin1aee0b52016-08-15 18:46:19969 voe_audio_transport_->PushCaptureData(config_.voe_channel_id, audio_data,
970 bits_per_sample, sample_rate,
971 number_of_channels, number_of_frames);
henrike@webrtc.orga7b98182014-02-21 15:51:43972 }
973
Taylor Brandstetter1a018dc2016-03-08 20:37:39974 // Callback from the |source_| when it is going away. In case Start() has
henrike@webrtc.orga7b98182014-02-21 15:51:43975 // never been called, this callback won't be triggered.
kjellander@webrtc.org14665ff2015-03-04 12:58:35976 void OnClose() override {
solenberg566ef242015-11-06 23:34:49977 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
Taylor Brandstetter1a018dc2016-03-08 20:37:39978 // Set |source_| to nullptr to make sure no more callback will get into
979 // the source.
980 source_ = nullptr;
981 UpdateSendState();
mallinath@webrtc.org67ee6b92014-02-03 16:57:16982 }
983
984 // Accessor to the VoE channel ID.
solenberg85a04962015-10-27 10:35:21985 int channel() const {
solenberg566ef242015-11-06 23:34:49986 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
solenberg7add0582015-11-20 17:59:34987 return config_.voe_channel_id;
solenberg85a04962015-10-27 10:35:21988 }
mallinath@webrtc.org67ee6b92014-02-03 16:57:16989
skvlade0d46372016-04-08 05:59:22990 const webrtc::RtpParameters& rtp_parameters() const {
991 return rtp_parameters_;
992 }
993
deadbeeffb2aced2017-01-07 07:05:37994 bool ValidateRtpParameters(const webrtc::RtpParameters& rtp_parameters) {
995 if (rtp_parameters.encodings.size() != 1) {
Mirko Bonadei675513b2017-11-09 10:09:25996 RTC_LOG(LS_ERROR)
deadbeeffb2aced2017-01-07 07:05:37997 << "Attempted to set RtpParameters without exactly one encoding";
998 return false;
999 }
1000 if (rtp_parameters.encodings[0].ssrc != rtp_parameters_.encodings[0].ssrc) {
Mirko Bonadei675513b2017-11-09 10:09:251001 RTC_LOG(LS_ERROR) << "Attempted to set RtpParameters with modified SSRC";
deadbeeffb2aced2017-01-07 07:05:371002 return false;
1003 }
1004 return true;
1005 }
1006
minyue7a973442016-10-20 10:27:121007 bool SetRtpParameters(const webrtc::RtpParameters& parameters) {
deadbeeffb2aced2017-01-07 07:05:371008 if (!ValidateRtpParameters(parameters)) {
1009 return false;
1010 }
ossu20a4b3f2017-04-27 09:08:521011
1012 rtc::Optional<int> send_rate;
1013 if (audio_codec_spec_) {
1014 send_rate = ComputeSendBitrate(max_send_bitrate_bps_,
1015 parameters.encodings[0].max_bitrate_bps,
1016 *audio_codec_spec_);
1017 if (!send_rate) {
1018 return false;
1019 }
minyue7a973442016-10-20 10:27:121020 }
1021
minyuececec102017-03-27 20:04:251022 const rtc::Optional<int> old_rtp_max_bitrate =
1023 rtp_parameters_.encodings[0].max_bitrate_bps;
1024
skvlade0d46372016-04-08 05:59:221025 rtp_parameters_ = parameters;
minyue7a973442016-10-20 10:27:121026
minyuececec102017-03-27 20:04:251027 if (rtp_parameters_.encodings[0].max_bitrate_bps != old_rtp_max_bitrate) {
ossu20a4b3f2017-04-27 09:08:521028 // Reconfigure AudioSendStream with new bit rate.
1029 if (send_rate) {
1030 config_.send_codec_spec->target_bitrate_bps = send_rate;
1031 }
1032 UpdateAllowedBitrateRange();
1033 ReconfigureAudioSendStream();
minyue7a973442016-10-20 10:27:121034 } else {
1035 // parameters.encodings[0].active could have changed.
1036 UpdateSendState();
1037 }
1038 return true;
skvlade0d46372016-04-08 05:59:221039 }
1040
mallinath@webrtc.org67ee6b92014-02-03 16:57:161041 private:
Taylor Brandstetter1a018dc2016-03-08 20:37:391042 void UpdateSendState() {
1043 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
1044 RTC_DCHECK(stream_);
Taylor Brandstetter55dd7082016-05-03 20:50:111045 RTC_DCHECK_EQ(1UL, rtp_parameters_.encodings.size());
1046 if (send_ && source_ != nullptr && rtp_parameters_.encodings[0].active) {
Taylor Brandstetter1a018dc2016-03-08 20:37:391047 stream_->Start();
1048 } else { // !send || source_ = nullptr
1049 stream_->Stop();
1050 }
1051 }
1052
ossu20a4b3f2017-04-27 09:08:521053 void UpdateAllowedBitrateRange() {
michaelt53fe19d2016-10-18 16:39:221054 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
ossu20a4b3f2017-04-27 09:08:521055 const bool is_opus =
1056 config_.send_codec_spec &&
1057 !STR_CASE_CMP(config_.send_codec_spec->format.name.c_str(),
1058 kOpusCodecName);
1059 if (is_opus && webrtc::field_trial::IsEnabled("WebRTC-Audio-SendSideBwe")) {
stefane9f36d52017-01-24 16:18:451060 config_.min_bitrate_bps = kOpusMinBitrateBps;
minyuececec102017-03-27 20:04:251061
1062 // This means that when RtpParameters is reset, we may change the
ossu20a4b3f2017-04-27 09:08:521063 // encoder's bit rate immediately (through ReconfigureAudioSendStream()),
minyuececec102017-03-27 20:04:251064 // meanwhile change the cap to the output of BWE.
1065 config_.max_bitrate_bps =
1066 rtp_parameters_.encodings[0].max_bitrate_bps
1067 ? *rtp_parameters_.encodings[0].max_bitrate_bps
1068 : kOpusBitrateFbBps;
1069
michaelt53fe19d2016-10-18 16:39:221070 // TODO(mflodman): Keep testing this and set proper values.
1071 // Note: This is an early experiment currently only supported by Opus.
elad.alon0fe12162017-01-31 13:48:371072 if (send_side_bwe_with_overhead_) {
ossu20a4b3f2017-04-27 09:08:521073 const int max_packet_size_ms =
1074 WEBRTC_OPUS_SUPPORT_120MS_PTIME ? 120 : 60;
michaelt6672b262017-01-11 18:17:591075
ossu20a4b3f2017-04-27 09:08:521076 // OverheadPerPacket = Ipv4(20B) + UDP(8B) + SRTP(10B) + RTP(12)
1077 constexpr int kOverheadPerPacket = 20 + 8 + 10 + 12;
michaelt6672b262017-01-11 18:17:591078
ossu20a4b3f2017-04-27 09:08:521079 int min_overhead_bps =
1080 kOverheadPerPacket * 8 * 1000 / max_packet_size_ms;
michaelt6672b262017-01-11 18:17:591081
ossu20a4b3f2017-04-27 09:08:521082 // We assume that |config_.max_bitrate_bps| before the next line is
1083 // a hard limit on the payload bitrate, so we add min_overhead_bps to
1084 // it to ensure that, when overhead is deducted, the payload rate
1085 // never goes beyond the limit.
1086 // Note: this also means that if a higher overhead is forced, we
1087 // cannot reach the limit.
1088 // TODO(minyue): Reconsider this when the signaling to BWE is done
1089 // through a dedicated API.
1090 config_.max_bitrate_bps += min_overhead_bps;
michaelt6672b262017-01-11 18:17:591091
ossu20a4b3f2017-04-27 09:08:521092 // In contrast to max_bitrate_bps, we let min_bitrate_bps always be
1093 // reachable.
1094 config_.min_bitrate_bps += min_overhead_bps;
michaelt6672b262017-01-11 18:17:591095 }
michaelt53fe19d2016-10-18 16:39:221096 }
ossu20a4b3f2017-04-27 09:08:521097 }
1098
1099 void UpdateSendCodecSpec(
1100 const webrtc::AudioSendStream::Config::SendCodecSpec& send_codec_spec) {
1101 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
1102 config_.rtp.nack.rtp_history_ms =
1103 send_codec_spec.nack_enabled ? kNackRtpHistoryMs : 0;
Oskar Sundbom78807582017-11-16 10:09:551104 config_.send_codec_spec = send_codec_spec;
ossu20a4b3f2017-04-27 09:08:521105 auto info =
1106 config_.encoder_factory->QueryAudioEncoder(send_codec_spec.format);
1107 RTC_DCHECK(info);
1108 // If a specific target bitrate has been set for the stream, use that as
1109 // the new default bitrate when computing send bitrate.
1110 if (send_codec_spec.target_bitrate_bps) {
1111 info->default_bitrate_bps = std::max(
1112 info->min_bitrate_bps,
1113 std::min(info->max_bitrate_bps, *send_codec_spec.target_bitrate_bps));
1114 }
1115
1116 audio_codec_spec_.emplace(
1117 webrtc::AudioCodecSpec{send_codec_spec.format, *info});
1118
1119 config_.send_codec_spec->target_bitrate_bps = ComputeSendBitrate(
1120 max_send_bitrate_bps_, rtp_parameters_.encodings[0].max_bitrate_bps,
1121 *audio_codec_spec_);
1122
1123 UpdateAllowedBitrateRange();
1124 }
1125
1126 void ReconfigureAudioSendStream() {
1127 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
1128 RTC_DCHECK(stream_);
1129 stream_->Reconfigure(config_);
michaelt53fe19d2016-10-18 16:39:221130 }
1131
solenberg566ef242015-11-06 23:34:491132 rtc::ThreadChecker worker_thread_checker_;
solenberg347ec5c2016-09-23 11:21:471133 rtc::RaceChecker audio_capture_race_checker_;
solenbergc96df772015-10-21 20:01:531134 webrtc::AudioTransport* const voe_audio_transport_ = nullptr;
1135 webrtc::Call* call_ = nullptr;
solenberg3a941542015-11-16 15:34:501136 webrtc::AudioSendStream::Config config_;
elad.alon0fe12162017-01-31 13:48:371137 const bool send_side_bwe_with_overhead_;
solenberg3a941542015-11-16 15:34:501138 // The stream is owned by WebRtcAudioSendStream and may be reallocated if
1139 // configuration changes.
solenbergc96df772015-10-21 20:01:531140 webrtc::AudioSendStream* stream_ = nullptr;
mallinath@webrtc.org67ee6b92014-02-03 16:57:161141
Taylor Brandstetter1a018dc2016-03-08 20:37:391142 // Raw pointer to AudioSource owned by LocalAudioTrackHandler.
mallinath@webrtc.org67ee6b92014-02-03 16:57:161143 // PeerConnection will make sure invalidating the pointer before the object
1144 // goes away.
Taylor Brandstetter1a018dc2016-03-08 20:37:391145 AudioSource* source_ = nullptr;
1146 bool send_ = false;
solenberg94218532016-06-16 17:53:221147 bool muted_ = false;
minyue7a973442016-10-20 10:27:121148 int max_send_bitrate_bps_;
skvlade0d46372016-04-08 05:59:221149 webrtc::RtpParameters rtp_parameters_;
ossu20a4b3f2017-04-27 09:08:521150 rtc::Optional<webrtc::AudioCodecSpec> audio_codec_spec_;
henrike@webrtc.orga7b98182014-02-21 15:51:431151
solenbergc96df772015-10-21 20:01:531152 RTC_DISALLOW_IMPLICIT_CONSTRUCTORS(WebRtcAudioSendStream);
1153};
1154
1155class WebRtcVoiceMediaChannel::WebRtcAudioReceiveStream {
1156 public:
ossu29b1a8d2016-06-13 14:34:511157 WebRtcAudioReceiveStream(
1158 int ch,
1159 uint32_t remote_ssrc,
1160 uint32_t local_ssrc,
1161 bool use_transport_cc,
solenberg8189b022016-06-14 19:13:001162 bool use_nack,
ossu29b1a8d2016-06-13 14:34:511163 const std::string& sync_group,
1164 const std::vector<webrtc::RtpExtension>& extensions,
1165 webrtc::Call* call,
1166 webrtc::Transport* rtcp_send_transport,
kwiberg1c07c702017-03-27 14:15:491167 const rtc::scoped_refptr<webrtc::AudioDecoderFactory>& decoder_factory,
1168 const std::map<int, webrtc::SdpAudioFormat>& decoder_map)
stefanba4c0e42016-02-04 12:12:241169 : call_(call), config_() {
solenberg7add0582015-11-20 17:59:341170 RTC_DCHECK_GE(ch, 0);
1171 RTC_DCHECK(call);
1172 config_.rtp.remote_ssrc = remote_ssrc;
kwibergd32bf752017-01-19 15:03:591173 config_.rtp.local_ssrc = local_ssrc;
1174 config_.rtp.transport_cc = use_transport_cc;
1175 config_.rtp.nack.rtp_history_ms = use_nack ? kNackRtpHistoryMs : 0;
1176 config_.rtp.extensions = extensions;
solenberg31fec402016-05-06 09:13:121177 config_.rtcp_send_transport = rtcp_send_transport;
solenberg7add0582015-11-20 17:59:341178 config_.voe_channel_id = ch;
1179 config_.sync_group = sync_group;
ossu29b1a8d2016-06-13 14:34:511180 config_.decoder_factory = decoder_factory;
kwiberg1c07c702017-03-27 14:15:491181 config_.decoder_map = decoder_map;
kwibergd32bf752017-01-19 15:03:591182 RecreateAudioReceiveStream();
solenberg7add0582015-11-20 17:59:341183 }
solenbergc96df772015-10-21 20:01:531184
solenberg7add0582015-11-20 17:59:341185 ~WebRtcAudioReceiveStream() {
1186 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
1187 call_->DestroyAudioReceiveStream(stream_);
1188 }
1189
solenberg4a0f7b52016-06-16 20:07:331190 void RecreateAudioReceiveStream(uint32_t local_ssrc) {
solenberg7add0582015-11-20 17:59:341191 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
kwibergd32bf752017-01-19 15:03:591192 config_.rtp.local_ssrc = local_ssrc;
1193 RecreateAudioReceiveStream();
solenberg7add0582015-11-20 17:59:341194 }
solenberg8189b022016-06-14 19:13:001195
1196 void RecreateAudioReceiveStream(bool use_transport_cc, bool use_nack) {
solenberg7add0582015-11-20 17:59:341197 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
kwibergd32bf752017-01-19 15:03:591198 config_.rtp.transport_cc = use_transport_cc;
1199 config_.rtp.nack.rtp_history_ms = use_nack ? kNackRtpHistoryMs : 0;
1200 RecreateAudioReceiveStream();
solenberg7add0582015-11-20 17:59:341201 }
1202
solenberg4a0f7b52016-06-16 20:07:331203 void RecreateAudioReceiveStream(
1204 const std::vector<webrtc::RtpExtension>& extensions) {
1205 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
kwibergd32bf752017-01-19 15:03:591206 config_.rtp.extensions = extensions;
1207 RecreateAudioReceiveStream();
1208 }
1209
deadbeefcb383672017-04-26 23:28:421210 // Set a new payload type -> decoder map.
kwibergd32bf752017-01-19 15:03:591211 void RecreateAudioReceiveStream(
1212 const std::map<int, webrtc::SdpAudioFormat>& decoder_map) {
1213 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
kwibergd32bf752017-01-19 15:03:591214 config_.decoder_map = decoder_map;
1215 RecreateAudioReceiveStream();
solenberg4a0f7b52016-06-16 20:07:331216 }
1217
solenberg4904fb62017-02-17 20:01:141218 void MaybeRecreateAudioReceiveStream(const std::string& sync_group) {
1219 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
1220 if (config_.sync_group != sync_group) {
1221 config_.sync_group = sync_group;
1222 RecreateAudioReceiveStream();
1223 }
1224 }
1225
solenberg7add0582015-11-20 17:59:341226 webrtc::AudioReceiveStream::Stats GetStats() const {
1227 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
1228 RTC_DCHECK(stream_);
1229 return stream_->GetStats();
1230 }
1231
solenberg796b8f92017-03-02 01:02:231232 int GetOutputLevel() const {
1233 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
1234 RTC_DCHECK(stream_);
1235 return stream_->GetOutputLevel();
1236 }
1237
solenberg7add0582015-11-20 17:59:341238 int channel() const {
1239 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
1240 return config_.voe_channel_id;
1241 }
solenbergc96df772015-10-21 20:01:531242
kwiberg686a8ef2016-02-26 11:00:351243 void SetRawAudioSink(std::unique_ptr<webrtc::AudioSinkInterface> sink) {
Tommif888bb52015-12-12 00:37:011244 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
kwiberg686a8ef2016-02-26 11:00:351245 stream_->SetSink(std::move(sink));
Tommif888bb52015-12-12 00:37:011246 }
1247
solenberg217fb662016-06-17 15:30:541248 void SetOutputVolume(double volume) {
1249 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
1250 stream_->SetGain(volume);
1251 }
1252
aleloi84ef6152016-08-04 12:28:211253 void SetPlayout(bool playout) {
1254 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
1255 RTC_DCHECK(stream_);
1256 if (playout) {
Mirko Bonadei675513b2017-11-09 10:09:251257 RTC_LOG(LS_INFO) << "Starting playout for channel #" << channel();
aleloi84ef6152016-08-04 12:28:211258 stream_->Start();
1259 } else {
Mirko Bonadei675513b2017-11-09 10:09:251260 RTC_LOG(LS_INFO) << "Stopping playout for channel #" << channel();
aleloi84ef6152016-08-04 12:28:211261 stream_->Stop();
1262 }
aleloi18e0b672016-10-04 09:45:471263 playout_ = playout;
aleloi84ef6152016-08-04 12:28:211264 }
1265
hbos8d609f62017-04-10 14:39:051266 std::vector<webrtc::RtpSource> GetSources() {
1267 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
1268 RTC_DCHECK(stream_);
1269 return stream_->GetSources();
1270 }
1271
solenbergc96df772015-10-21 20:01:531272 private:
kwibergd32bf752017-01-19 15:03:591273 void RecreateAudioReceiveStream() {
solenberg7add0582015-11-20 17:59:341274 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
1275 if (stream_) {
1276 call_->DestroyAudioReceiveStream(stream_);
solenberg7add0582015-11-20 17:59:341277 }
solenberg7add0582015-11-20 17:59:341278 stream_ = call_->CreateAudioReceiveStream(config_);
1279 RTC_CHECK(stream_);
aleloi18e0b672016-10-04 09:45:471280 SetPlayout(playout_);
solenberg7add0582015-11-20 17:59:341281 }
1282
1283 rtc::ThreadChecker worker_thread_checker_;
1284 webrtc::Call* call_ = nullptr;
1285 webrtc::AudioReceiveStream::Config config_;
1286 // The stream is owned by WebRtcAudioReceiveStream and may be reallocated if
1287 // configuration changes.
1288 webrtc::AudioReceiveStream* stream_ = nullptr;
aleloi18e0b672016-10-04 09:45:471289 bool playout_ = false;
solenbergc96df772015-10-21 20:01:531290
1291 RTC_DISALLOW_IMPLICIT_CONSTRUCTORS(WebRtcAudioReceiveStream);
henrike@webrtc.org1e09a712013-07-26 19:17:591292};
1293
Fredrik Solenberg709ed672015-09-15 10:26:331294WebRtcVoiceMediaChannel::WebRtcVoiceMediaChannel(WebRtcVoiceEngine* engine,
nisse51542be2016-02-12 10:27:061295 const MediaConfig& config,
Fredrik Solenbergb071a192015-09-17 14:42:561296 const AudioOptions& options,
Fredrik Solenberg709ed672015-09-15 10:26:331297 webrtc::Call* call)
nisse51542be2016-02-12 10:27:061298 : VoiceMediaChannel(config), engine_(engine), call_(call) {
Mirko Bonadei675513b2017-11-09 10:09:251299 RTC_LOG(LS_VERBOSE) << "WebRtcVoiceMediaChannel::WebRtcVoiceMediaChannel";
solenberg566ef242015-11-06 23:34:491300 RTC_DCHECK(call);
solenberg0a617e22015-10-20 22:49:381301 engine->RegisterChannel(this);
Fredrik Solenbergb071a192015-09-17 14:42:561302 SetOptions(options);
henrike@webrtc.org28e20752013-07-10 00:45:361303}
1304
1305WebRtcVoiceMediaChannel::~WebRtcVoiceMediaChannel() {
solenberg566ef242015-11-06 23:34:491306 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
Mirko Bonadei675513b2017-11-09 10:09:251307 RTC_LOG(LS_VERBOSE) << "WebRtcVoiceMediaChannel::~WebRtcVoiceMediaChannel";
solenberg7add0582015-11-20 17:59:341308 // TODO(solenberg): Should be able to delete the streams directly, without
1309 // going through RemoveNnStream(), once stream objects handle
1310 // all (de)configuration.
solenbergc96df772015-10-21 20:01:531311 while (!send_streams_.empty()) {
1312 RemoveSendStream(send_streams_.begin()->first);
solenbergd97ec302015-10-07 08:40:331313 }
solenberg7add0582015-11-20 17:59:341314 while (!recv_streams_.empty()) {
1315 RemoveRecvStream(recv_streams_.begin()->first);
henrike@webrtc.org28e20752013-07-10 00:45:361316 }
solenberg0a617e22015-10-20 22:49:381317 engine()->UnregisterChannel(this);
henrike@webrtc.org28e20752013-07-10 00:45:361318}
1319
nisse51542be2016-02-12 10:27:061320rtc::DiffServCodePoint WebRtcVoiceMediaChannel::PreferredDscp() const {
1321 return kAudioDscpValue;
1322}
1323
Peter Thatcherc2ee2c82015-08-07 23:05:341324bool WebRtcVoiceMediaChannel::SetSendParameters(
1325 const AudioSendParameters& params) {
Peter Boströmca8b4042016-03-08 22:24:131326 TRACE_EVENT0("webrtc", "WebRtcVoiceMediaChannel::SetSendParameters");
solenberg566ef242015-11-06 23:34:491327 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
Mirko Bonadei675513b2017-11-09 10:09:251328 RTC_LOG(LS_INFO) << "WebRtcVoiceMediaChannel::SetSendParameters: "
1329 << params.ToString();
Peter Thatcherc2ee2c82015-08-07 23:05:341330 // TODO(pthatcher): Refactor this to be more clean now that we have
1331 // all the information at once.
solenberg3a941542015-11-16 15:34:501332
1333 if (!SetSendCodecs(params.codecs)) {
1334 return false;
1335 }
1336
solenberg7e4e01a2015-12-02 16:05:011337 if (!ValidateRtpExtensions(params.extensions)) {
1338 return false;
1339 }
1340 std::vector<webrtc::RtpExtension> filtered_extensions =
1341 FilterRtpExtensions(params.extensions,
1342 webrtc::RtpExtension::IsSupportedForAudio, true);
1343 if (send_rtp_extensions_ != filtered_extensions) {
1344 send_rtp_extensions_.swap(filtered_extensions);
solenberg3a941542015-11-16 15:34:501345 for (auto& it : send_streams_) {
ossu20a4b3f2017-04-27 09:08:521346 it.second->SetRtpExtensions(send_rtp_extensions_);
solenberg3a941542015-11-16 15:34:501347 }
1348 }
1349
deadbeef80346142016-04-27 21:17:101350 if (!SetMaxSendBitrate(params.max_bandwidth_bps)) {
solenberg3a941542015-11-16 15:34:501351 return false;
1352 }
1353 return SetOptions(params.options);
Peter Thatcherc2ee2c82015-08-07 23:05:341354}
1355
1356bool WebRtcVoiceMediaChannel::SetRecvParameters(
1357 const AudioRecvParameters& params) {
Peter Boströmca8b4042016-03-08 22:24:131358 TRACE_EVENT0("webrtc", "WebRtcVoiceMediaChannel::SetRecvParameters");
solenberg566ef242015-11-06 23:34:491359 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
Mirko Bonadei675513b2017-11-09 10:09:251360 RTC_LOG(LS_INFO) << "WebRtcVoiceMediaChannel::SetRecvParameters: "
1361 << params.ToString();
Peter Thatcherc2ee2c82015-08-07 23:05:341362 // TODO(pthatcher): Refactor this to be more clean now that we have
1363 // all the information at once.
solenberg7add0582015-11-20 17:59:341364
1365 if (!SetRecvCodecs(params.codecs)) {
1366 return false;
1367 }
1368
solenberg7e4e01a2015-12-02 16:05:011369 if (!ValidateRtpExtensions(params.extensions)) {
1370 return false;
1371 }
1372 std::vector<webrtc::RtpExtension> filtered_extensions =
1373 FilterRtpExtensions(params.extensions,
1374 webrtc::RtpExtension::IsSupportedForAudio, false);
1375 if (recv_rtp_extensions_ != filtered_extensions) {
1376 recv_rtp_extensions_.swap(filtered_extensions);
solenberg7add0582015-11-20 17:59:341377 for (auto& it : recv_streams_) {
1378 it.second->RecreateAudioReceiveStream(recv_rtp_extensions_);
1379 }
1380 }
solenberg7add0582015-11-20 17:59:341381 return true;
Peter Thatcherc2ee2c82015-08-07 23:05:341382}
1383
Taylor Brandstetterdb0cd9e2016-05-16 18:40:301384webrtc::RtpParameters WebRtcVoiceMediaChannel::GetRtpSendParameters(
skvlade0d46372016-04-08 05:59:221385 uint32_t ssrc) const {
1386 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
1387 auto it = send_streams_.find(ssrc);
1388 if (it == send_streams_.end()) {
Mirko Bonadei675513b2017-11-09 10:09:251389 RTC_LOG(LS_WARNING) << "Attempting to get RTP send parameters for stream "
1390 << "with ssrc " << ssrc << " which doesn't exist.";
skvlade0d46372016-04-08 05:59:221391 return webrtc::RtpParameters();
1392 }
1393
Taylor Brandstetter0cd086b2016-04-20 23:23:101394 webrtc::RtpParameters rtp_params = it->second->rtp_parameters();
1395 // Need to add the common list of codecs to the send stream-specific
1396 // RTP parameters.
1397 for (const AudioCodec& codec : send_codecs_) {
1398 rtp_params.codecs.push_back(codec.ToCodecParameters());
1399 }
1400 return rtp_params;
skvlade0d46372016-04-08 05:59:221401}
1402
Taylor Brandstetterdb0cd9e2016-05-16 18:40:301403bool WebRtcVoiceMediaChannel::SetRtpSendParameters(
skvlade0d46372016-04-08 05:59:221404 uint32_t ssrc,
1405 const webrtc::RtpParameters& parameters) {
1406 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
skvlade0d46372016-04-08 05:59:221407 auto it = send_streams_.find(ssrc);
1408 if (it == send_streams_.end()) {
Mirko Bonadei675513b2017-11-09 10:09:251409 RTC_LOG(LS_WARNING) << "Attempting to set RTP send parameters for stream "
1410 << "with ssrc " << ssrc << " which doesn't exist.";
skvlade0d46372016-04-08 05:59:221411 return false;
1412 }
1413
Taylor Brandstetterdb0cd9e2016-05-16 18:40:301414 // TODO(deadbeef): Handle setting parameters with a list of codecs in a
1415 // different order (which should change the send codec).
1416 webrtc::RtpParameters current_parameters = GetRtpSendParameters(ssrc);
1417 if (current_parameters.codecs != parameters.codecs) {
Mirko Bonadei675513b2017-11-09 10:09:251418 RTC_LOG(LS_ERROR) << "Using SetParameters to change the set of codecs "
1419 << "is not currently supported.";
Taylor Brandstetterdb0cd9e2016-05-16 18:40:301420 return false;
1421 }
1422
minyue7a973442016-10-20 10:27:121423 // TODO(minyue): The following legacy actions go into
1424 // |WebRtcAudioSendStream::SetRtpParameters()| which is called at the end,
1425 // though there are two difference:
1426 // 1. |WebRtcVoiceMediaChannel::SetChannelSendParameters()| only calls
1427 // |SetSendCodec| while |WebRtcAudioSendStream::SetRtpParameters()| calls
1428 // |SetSendCodecs|. The outcome should be the same.
1429 // 2. AudioSendStream can be recreated.
1430
Taylor Brandstetter0cd086b2016-04-20 23:23:101431 // Codecs are handled at the WebRtcVoiceMediaChannel level.
1432 webrtc::RtpParameters reduced_params = parameters;
1433 reduced_params.codecs.clear();
minyue7a973442016-10-20 10:27:121434 return it->second->SetRtpParameters(reduced_params);
skvlade0d46372016-04-08 05:59:221435}
1436
Taylor Brandstetterdb0cd9e2016-05-16 18:40:301437webrtc::RtpParameters WebRtcVoiceMediaChannel::GetRtpReceiveParameters(
1438 uint32_t ssrc) const {
1439 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
deadbeef3bc15102017-04-21 02:25:071440 webrtc::RtpParameters rtp_params;
1441 // SSRC of 0 represents the default receive stream.
1442 if (ssrc == 0) {
1443 if (!default_sink_) {
Mirko Bonadei675513b2017-11-09 10:09:251444 RTC_LOG(LS_WARNING)
1445 << "Attempting to get RTP parameters for the default, "
1446 "unsignaled audio receive stream, but not yet "
1447 "configured to receive such a stream.";
deadbeef3bc15102017-04-21 02:25:071448 return rtp_params;
1449 }
1450 rtp_params.encodings.emplace_back();
1451 } else {
1452 auto it = recv_streams_.find(ssrc);
1453 if (it == recv_streams_.end()) {
Mirko Bonadei675513b2017-11-09 10:09:251454 RTC_LOG(LS_WARNING)
1455 << "Attempting to get RTP receive parameters for stream "
1456 << "with ssrc " << ssrc << " which doesn't exist.";
deadbeef3bc15102017-04-21 02:25:071457 return webrtc::RtpParameters();
1458 }
1459 rtp_params.encodings.emplace_back();
1460 // TODO(deadbeef): Return stream-specific parameters.
Oskar Sundbom78807582017-11-16 10:09:551461 rtp_params.encodings[0].ssrc = ssrc;
Taylor Brandstetterdb0cd9e2016-05-16 18:40:301462 }
1463
Taylor Brandstetterdb0cd9e2016-05-16 18:40:301464 for (const AudioCodec& codec : recv_codecs_) {
1465 rtp_params.codecs.push_back(codec.ToCodecParameters());
1466 }
1467 return rtp_params;
1468}
1469
1470bool WebRtcVoiceMediaChannel::SetRtpReceiveParameters(
1471 uint32_t ssrc,
1472 const webrtc::RtpParameters& parameters) {
1473 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
deadbeef3bc15102017-04-21 02:25:071474 // SSRC of 0 represents the default receive stream.
1475 if (ssrc == 0) {
1476 if (!default_sink_) {
Mirko Bonadei675513b2017-11-09 10:09:251477 RTC_LOG(LS_WARNING)
1478 << "Attempting to set RTP parameters for the default, "
1479 "unsignaled audio receive stream, but not yet "
1480 "configured to receive such a stream.";
deadbeef3bc15102017-04-21 02:25:071481 return false;
1482 }
1483 } else {
1484 auto it = recv_streams_.find(ssrc);
1485 if (it == recv_streams_.end()) {
Mirko Bonadei675513b2017-11-09 10:09:251486 RTC_LOG(LS_WARNING)
1487 << "Attempting to set RTP receive parameters for stream "
1488 << "with ssrc " << ssrc << " which doesn't exist.";
deadbeef3bc15102017-04-21 02:25:071489 return false;
1490 }
Taylor Brandstetterdb0cd9e2016-05-16 18:40:301491 }
1492
1493 webrtc::RtpParameters current_parameters = GetRtpReceiveParameters(ssrc);
1494 if (current_parameters != parameters) {
Mirko Bonadei675513b2017-11-09 10:09:251495 RTC_LOG(LS_ERROR) << "Changing the RTP receive parameters is currently "
1496 << "unsupported.";
Taylor Brandstetterdb0cd9e2016-05-16 18:40:301497 return false;
1498 }
1499 return true;
1500}
1501
henrike@webrtc.org28e20752013-07-10 00:45:361502bool WebRtcVoiceMediaChannel::SetOptions(const AudioOptions& options) {
solenberg566ef242015-11-06 23:34:491503 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
Mirko Bonadei675513b2017-11-09 10:09:251504 RTC_LOG(LS_INFO) << "Setting voice channel options: " << options.ToString();
henrike@webrtc.org28e20752013-07-10 00:45:361505
1506 // We retain all of the existing options, and apply the given ones
1507 // on top. This means there is no way to "clear" options such that
1508 // they go back to the engine default.
1509 options_.SetAll(options);
solenberg246b8172015-12-08 17:50:231510 if (!engine()->ApplyOptions(options_)) {
Mirko Bonadei675513b2017-11-09 10:09:251511 RTC_LOG(LS_WARNING)
1512 << "Failed to apply engine options during channel SetOptions.";
solenberg246b8172015-12-08 17:50:231513 return false;
henrike@webrtc.org28e20752013-07-10 00:45:361514 }
minyue6b825df2016-10-31 11:08:321515
ossu20a4b3f2017-04-27 09:08:521516 rtc::Optional<std::string> audio_network_adaptor_config =
minyue6b825df2016-10-31 11:08:321517 GetAudioNetworkAdaptorConfig(options_);
1518 for (auto& it : send_streams_) {
ossu20a4b3f2017-04-27 09:08:521519 it.second->SetAudioNetworkAdaptorConfig(audio_network_adaptor_config);
minyue6b825df2016-10-31 11:08:321520 }
1521
Mirko Bonadei675513b2017-11-09 10:09:251522 RTC_LOG(LS_INFO) << "Set voice channel options. Current options: "
1523 << options_.ToString();
henrike@webrtc.org28e20752013-07-10 00:45:361524 return true;
1525}
1526
1527bool WebRtcVoiceMediaChannel::SetRecvCodecs(
1528 const std::vector<AudioCodec>& codecs) {
solenberg566ef242015-11-06 23:34:491529 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
solenberg8fb30c32015-10-13 10:06:581530
henrike@webrtc.org28e20752013-07-10 00:45:361531 // Set the payload types to be used for incoming media.
Mirko Bonadei675513b2017-11-09 10:09:251532 RTC_LOG(LS_INFO) << "Setting receive voice codecs.";
solenberg0b675462015-10-09 08:37:091533
1534 if (!VerifyUniquePayloadTypes(codecs)) {
Mirko Bonadei675513b2017-11-09 10:09:251535 RTC_LOG(LS_ERROR) << "Codec payload types overlap.";
solenberg0b675462015-10-09 08:37:091536 return false;
1537 }
henrike@webrtc.org28e20752013-07-10 00:45:361538
kwibergd32bf752017-01-19 15:03:591539 // Create a payload type -> SdpAudioFormat map with all the decoders. Fail
1540 // unless the factory claims to support all decoders.
1541 std::map<int, webrtc::SdpAudioFormat> decoder_map;
1542 for (const AudioCodec& codec : codecs) {
deadbeefcb383672017-04-26 23:28:421543 // Log a warning if a codec's payload type is changing. This used to be
1544 // treated as an error. It's abnormal, but not really illegal.
1545 AudioCodec old_codec;
1546 if (FindCodec(recv_codecs_, codec, &old_codec) &&
1547 old_codec.id != codec.id) {
Mirko Bonadei675513b2017-11-09 10:09:251548 RTC_LOG(LS_WARNING) << codec.name << " mapped to a second payload type ("
1549 << codec.id << ", was already mapped to "
1550 << old_codec.id << ")";
deadbeefcb383672017-04-26 23:28:421551 }
kwibergd32bf752017-01-19 15:03:591552 auto format = AudioCodecToSdpAudioFormat(codec);
1553 if (!IsCodec(codec, "cn") && !IsCodec(codec, "telephone-event") &&
1554 !engine()->decoder_factory_->IsSupportedDecoder(format)) {
Mirko Bonadei675513b2017-11-09 10:09:251555 RTC_LOG(LS_ERROR) << "Unsupported codec: " << format;
kwibergd32bf752017-01-19 15:03:591556 return false;
1557 }
deadbeefcb383672017-04-26 23:28:421558 // We allow adding new codecs but don't allow changing the payload type of
1559 // codecs that are already configured since we might already be receiving
1560 // packets with that payload type. See RFC3264, Section 8.3.2.
1561 // TODO(deadbeef): Also need to check for clashes with previously mapped
1562 // payload types, and not just currently mapped ones. For example, this
1563 // should be illegal:
1564 // 1. {100: opus/48000/2, 101: ISAC/16000}
1565 // 2. {100: opus/48000/2}
1566 // 3. {100: opus/48000/2, 101: ISAC/32000}
1567 // Though this check really should happen at a higher level, since this
1568 // conflict could happen between audio and video codecs.
1569 auto existing = decoder_map_.find(codec.id);
1570 if (existing != decoder_map_.end() && !existing->second.Matches(format)) {
Mirko Bonadei675513b2017-11-09 10:09:251571 RTC_LOG(LS_ERROR) << "Attempting to use payload type " << codec.id
1572 << " for " << codec.name
1573 << ", but it is already used for "
1574 << existing->second.name;
deadbeefcb383672017-04-26 23:28:421575 return false;
1576 }
kwibergd32bf752017-01-19 15:03:591577 decoder_map.insert({codec.id, std::move(format)});
1578 }
1579
deadbeefcb383672017-04-26 23:28:421580 if (decoder_map == decoder_map_) {
1581 // There's nothing new to configure.
1582 return true;
1583 }
1584
kwiberg37b8b112016-11-03 09:46:531585 if (playout_) {
1586 // Receive codecs can not be changed while playing. So we temporarily
1587 // pause playout.
1588 ChangePlayout(false);
1589 }
1590
kwiberg1c07c702017-03-27 14:15:491591 decoder_map_ = std::move(decoder_map);
kwibergd32bf752017-01-19 15:03:591592 for (auto& kv : recv_streams_) {
kwiberg1c07c702017-03-27 14:15:491593 kv.second->RecreateAudioReceiveStream(decoder_map_);
solenberg26c8c912015-11-27 12:00:251594 }
kwibergd32bf752017-01-19 15:03:591595 recv_codecs_ = codecs;
henrike@webrtc.org28e20752013-07-10 00:45:361596
kwiberg37b8b112016-11-03 09:46:531597 if (desired_playout_ && !playout_) {
1598 ChangePlayout(desired_playout_);
1599 }
kwibergd32bf752017-01-19 15:03:591600 return true;
henrike@webrtc.org28e20752013-07-10 00:45:361601}
1602
solenberg72e29d22016-03-08 14:35:161603// Utility function called from SetSendParameters() to extract current send
1604// codec settings from the given list of codecs (originally from SDP). Both send
1605// and receive streams may be reconfigured based on the new settings.
wu@webrtc.orgcadf9042013-08-30 21:24:161606bool WebRtcVoiceMediaChannel::SetSendCodecs(
1607 const std::vector<AudioCodec>& codecs) {
solenberg566ef242015-11-06 23:34:491608 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
Oskar Sundbom78807582017-11-16 10:09:551609 dtmf_payload_type_ = rtc::nullopt;
solenbergffbbcac2016-11-17 13:25:371610 dtmf_payload_freq_ = -1;
1611
1612 // Validate supplied codecs list.
1613 for (const AudioCodec& codec : codecs) {
1614 // TODO(solenberg): Validate more aspects of input - that payload types
1615 // don't overlap, remove redundant/unsupported codecs etc -
1616 // the same way it is done for RtpHeaderExtensions.
1617 if (codec.id < kMinPayloadType || codec.id > kMaxPayloadType) {
Mirko Bonadei675513b2017-11-09 10:09:251618 RTC_LOG(LS_WARNING) << "Codec payload type out of range: "
1619 << ToString(codec);
solenbergffbbcac2016-11-17 13:25:371620 return false;
1621 }
1622 }
1623
1624 // Find PT of telephone-event codec with lowest clockrate, as a fallback, in
1625 // case we don't have a DTMF codec with a rate matching the send codec's, or
1626 // if this function returns early.
1627 std::vector<AudioCodec> dtmf_codecs;
Fredrik Solenbergaf9fb212015-08-26 08:45:531628 for (const AudioCodec& codec : codecs) {
Fredrik Solenbergaf9fb212015-08-26 08:45:531629 if (IsCodec(codec, kDtmfCodecName)) {
solenbergffbbcac2016-11-17 13:25:371630 dtmf_codecs.push_back(codec);
1631 if (!dtmf_payload_type_ || codec.clockrate < dtmf_payload_freq_) {
Oskar Sundbom78807582017-11-16 10:09:551632 dtmf_payload_type_ = codec.id;
solenbergffbbcac2016-11-17 13:25:371633 dtmf_payload_freq_ = codec.clockrate;
solenberg31642aa2016-03-14 15:00:371634 }
wu@webrtc.orgcadf9042013-08-30 21:24:161635 }
1636 }
1637
ossu20a4b3f2017-04-27 09:08:521638 // Scan through the list to figure out the codec to use for sending.
1639 rtc::Optional<webrtc::AudioSendStream::Config::SendCodecSpec> send_codec_spec;
stefan1ccf73f2017-03-27 10:51:181640 webrtc::Call::Config::BitrateConfig bitrate_config;
ossu20a4b3f2017-04-27 09:08:521641 rtc::Optional<webrtc::AudioCodecInfo> voice_codec_info;
1642 for (const AudioCodec& voice_codec : codecs) {
1643 if (!(IsCodec(voice_codec, kCnCodecName) ||
1644 IsCodec(voice_codec, kDtmfCodecName) ||
1645 IsCodec(voice_codec, kRedCodecName))) {
1646 webrtc::SdpAudioFormat format(voice_codec.name, voice_codec.clockrate,
1647 voice_codec.channels, voice_codec.params);
solenberg72e29d22016-03-08 14:35:161648
ossu20a4b3f2017-04-27 09:08:521649 voice_codec_info = engine()->encoder_factory_->QueryAudioEncoder(format);
1650 if (!voice_codec_info) {
Mirko Bonadei675513b2017-11-09 10:09:251651 RTC_LOG(LS_WARNING) << "Unknown codec " << ToString(voice_codec);
solenberg72e29d22016-03-08 14:35:161652 continue;
1653 }
1654
Oskar Sundbom78807582017-11-16 10:09:551655 send_codec_spec = webrtc::AudioSendStream::Config::SendCodecSpec(
1656 voice_codec.id, format);
ossu20a4b3f2017-04-27 09:08:521657 if (voice_codec.bitrate > 0) {
Oskar Sundbom78807582017-11-16 10:09:551658 send_codec_spec->target_bitrate_bps = voice_codec.bitrate;
ossu20a4b3f2017-04-27 09:08:521659 }
1660 send_codec_spec->transport_cc_enabled = HasTransportCc(voice_codec);
1661 send_codec_spec->nack_enabled = HasNack(voice_codec);
1662 bitrate_config = GetBitrateConfigForCodec(voice_codec);
1663 break;
1664 }
1665 }
1666
1667 if (!send_codec_spec) {
1668 return false;
1669 }
1670
1671 RTC_DCHECK(voice_codec_info);
1672 if (voice_codec_info->allow_comfort_noise) {
1673 // Loop through the codecs list again to find the CN codec.
1674 // TODO(solenberg): Break out into a separate function?
1675 for (const AudioCodec& cn_codec : codecs) {
ossu0c4b8492017-03-02 19:03:251676 if (IsCodec(cn_codec, kCnCodecName) &&
ossu20a4b3f2017-04-27 09:08:521677 cn_codec.clockrate == send_codec_spec->format.clockrate_hz) {
ossu0c4b8492017-03-02 19:03:251678 switch (cn_codec.clockrate) {
solenberg72e29d22016-03-08 14:35:161679 case 8000:
1680 case 16000:
1681 case 32000:
Oskar Sundbom78807582017-11-16 10:09:551682 send_codec_spec->cng_payload_type = cn_codec.id;
solenberg72e29d22016-03-08 14:35:161683 break;
1684 default:
Mirko Bonadei675513b2017-11-09 10:09:251685 RTC_LOG(LS_WARNING)
1686 << "CN frequency " << cn_codec.clockrate << " not supported.";
ossu20a4b3f2017-04-27 09:08:521687 break;
solenberg72e29d22016-03-08 14:35:161688 }
solenberg72e29d22016-03-08 14:35:161689 break;
1690 }
1691 }
solenbergffbbcac2016-11-17 13:25:371692
1693 // Find the telephone-event PT exactly matching the preferred send codec.
1694 for (const AudioCodec& dtmf_codec : dtmf_codecs) {
ossu20a4b3f2017-04-27 09:08:521695 if (dtmf_codec.clockrate == send_codec_spec->format.clockrate_hz) {
Oskar Sundbom78807582017-11-16 10:09:551696 dtmf_payload_type_ = dtmf_codec.id;
solenbergffbbcac2016-11-17 13:25:371697 dtmf_payload_freq_ = dtmf_codec.clockrate;
1698 break;
1699 }
1700 }
solenberg72e29d22016-03-08 14:35:161701 }
1702
solenberg971cab02016-06-14 17:02:411703 if (send_codec_spec_ != send_codec_spec) {
1704 send_codec_spec_ = std::move(send_codec_spec);
stefan13f1a0a2016-11-30 15:22:581705 // Apply new settings to all streams.
solenberg971cab02016-06-14 17:02:411706 for (const auto& kv : send_streams_) {
ossu20a4b3f2017-04-27 09:08:521707 kv.second->SetSendCodecSpec(*send_codec_spec_);
wu@webrtc.orgcadf9042013-08-30 21:24:161708 }
stefan13f1a0a2016-11-30 15:22:581709 } else {
1710 // If the codec isn't changing, set the start bitrate to -1 which means
1711 // "unchanged" so that BWE isn't affected.
stefan1ccf73f2017-03-27 10:51:181712 bitrate_config.start_bitrate_bps = -1;
wu@webrtc.orgcadf9042013-08-30 21:24:161713 }
stefan1ccf73f2017-03-27 10:51:181714 call_->SetBitrateConfig(bitrate_config);
wu@webrtc.orgcadf9042013-08-30 21:24:161715
solenberg8189b022016-06-14 19:13:001716 // Check if the transport cc feedback or NACK status has changed on the
1717 // preferred send codec, and in that case reconfigure all receive streams.
ossu20a4b3f2017-04-27 09:08:521718 if (recv_transport_cc_enabled_ != send_codec_spec_->transport_cc_enabled ||
1719 recv_nack_enabled_ != send_codec_spec_->nack_enabled) {
Mirko Bonadei675513b2017-11-09 10:09:251720 RTC_LOG(LS_INFO) << "Recreate all the receive streams because the send "
1721 "codec has changed.";
ossu20a4b3f2017-04-27 09:08:521722 recv_transport_cc_enabled_ = send_codec_spec_->transport_cc_enabled;
1723 recv_nack_enabled_ = send_codec_spec_->nack_enabled;
solenberg72e29d22016-03-08 14:35:161724 for (auto& kv : recv_streams_) {
solenberg8189b022016-06-14 19:13:001725 kv.second->RecreateAudioReceiveStream(recv_transport_cc_enabled_,
1726 recv_nack_enabled_);
solenberg72e29d22016-03-08 14:35:161727 }
1728 }
1729
Taylor Brandstetter0cd086b2016-04-20 23:23:101730 send_codecs_ = codecs;
solenberg72e29d22016-03-08 14:35:161731 return true;
1732}
1733
aleloi84ef6152016-08-04 12:28:211734void WebRtcVoiceMediaChannel::SetPlayout(bool playout) {
kwiberg37b8b112016-11-03 09:46:531735 desired_playout_ = playout;
1736 return ChangePlayout(desired_playout_);
1737}
1738
1739void WebRtcVoiceMediaChannel::ChangePlayout(bool playout) {
1740 TRACE_EVENT0("webrtc", "WebRtcVoiceMediaChannel::ChangePlayout");
solenberg566ef242015-11-06 23:34:491741 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
henrike@webrtc.org28e20752013-07-10 00:45:361742 if (playout_ == playout) {
aleloi84ef6152016-08-04 12:28:211743 return;
henrike@webrtc.org28e20752013-07-10 00:45:361744 }
1745
aleloi84ef6152016-08-04 12:28:211746 for (const auto& kv : recv_streams_) {
1747 kv.second->SetPlayout(playout);
henrike@webrtc.org28e20752013-07-10 00:45:361748 }
solenberg1ac56142015-10-13 10:58:191749 playout_ = playout;
henrike@webrtc.org28e20752013-07-10 00:45:361750}
1751
Taylor Brandstetter1a018dc2016-03-08 20:37:391752void WebRtcVoiceMediaChannel::SetSend(bool send) {
Peter Boströmca8b4042016-03-08 22:24:131753 TRACE_EVENT0("webrtc", "WebRtcVoiceMediaChannel::SetSend");
henrike@webrtc.org28e20752013-07-10 00:45:361754 if (send_ == send) {
Taylor Brandstetter1a018dc2016-03-08 20:37:391755 return;
henrike@webrtc.org28e20752013-07-10 00:45:361756 }
1757
solenbergd53a3f92016-04-14 20:56:371758 // Apply channel specific options, and initialize the ADM for recording (this
1759 // may take time on some platforms, e.g. Android).
Taylor Brandstetter1a018dc2016-03-08 20:37:391760 if (send) {
solenberg63b34542015-09-29 13:06:311761 engine()->ApplyOptions(options_);
solenbergd53a3f92016-04-14 20:56:371762
1763 // InitRecording() may return an error if the ADM is already recording.
1764 if (!engine()->adm()->RecordingIsInitialized() &&
1765 !engine()->adm()->Recording()) {
1766 if (engine()->adm()->InitRecording() != 0) {
Mirko Bonadei675513b2017-11-09 10:09:251767 RTC_LOG(LS_WARNING) << "Failed to initialize recording";
solenbergd53a3f92016-04-14 20:56:371768 }
1769 }
solenberg63b34542015-09-29 13:06:311770 }
henrike@webrtc.org28e20752013-07-10 00:45:361771
wu@webrtc.org9dba5252013-08-05 20:36:571772 // Change the settings on each send channel.
Taylor Brandstetter1a018dc2016-03-08 20:37:391773 for (auto& kv : send_streams_) {
1774 kv.second->SetSend(send);
henrike@webrtc.org28e20752013-07-10 00:45:361775 }
wu@webrtc.org9dba5252013-08-05 20:36:571776
henrike@webrtc.org28e20752013-07-10 00:45:361777 send_ = send;
henrike@webrtc.org28e20752013-07-10 00:45:361778}
1779
Peter Boström0c4e06b2015-10-07 10:23:211780bool WebRtcVoiceMediaChannel::SetAudioSend(uint32_t ssrc,
1781 bool enable,
solenberg1dd98f32015-09-10 08:57:141782 const AudioOptions* options,
Taylor Brandstetter1a018dc2016-03-08 20:37:391783 AudioSource* source) {
solenberg566ef242015-11-06 23:34:491784 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
solenberg1dd98f32015-09-10 08:57:141785 // TODO(solenberg): The state change should be fully rolled back if any one of
1786 // these calls fail.
Taylor Brandstetter1a018dc2016-03-08 20:37:391787 if (!SetLocalSource(ssrc, source)) {
solenberg1dd98f32015-09-10 08:57:141788 return false;
1789 }
solenbergdfc8f4f2015-10-01 09:31:101790 if (!MuteStream(ssrc, !enable)) {
solenberg1dd98f32015-09-10 08:57:141791 return false;
1792 }
solenbergdfc8f4f2015-10-01 09:31:101793 if (enable && options) {
solenberg1dd98f32015-09-10 08:57:141794 return SetOptions(*options);
1795 }
1796 return true;
1797}
1798
solenberg0a617e22015-10-20 22:49:381799int WebRtcVoiceMediaChannel::CreateVoEChannel() {
1800 int id = engine()->CreateVoEChannel();
1801 if (id == -1) {
Mirko Bonadei675513b2017-11-09 10:09:251802 RTC_LOG(LS_WARNING) << "CreateVoEChannel() failed.";
solenberg0a617e22015-10-20 22:49:381803 return -1;
wu@webrtc.org9dba5252013-08-05 20:36:571804 }
mflodman3d7db262016-04-29 07:57:131805
solenberg0a617e22015-10-20 22:49:381806 return id;
wu@webrtc.org9dba5252013-08-05 20:36:571807}
1808
solenberg7add0582015-11-20 17:59:341809bool WebRtcVoiceMediaChannel::DeleteVoEChannel(int channel) {
wu@webrtc.org9dba5252013-08-05 20:36:571810 if (engine()->voe()->base()->DeleteChannel(channel) == -1) {
Mirko Bonadei675513b2017-11-09 10:09:251811 RTC_LOG(LS_WARNING) << "DeleteChannel(" << channel << ") failed.";
henrike@webrtc.org28e20752013-07-10 00:45:361812 return false;
1813 }
wu@webrtc.org9dba5252013-08-05 20:36:571814 return true;
1815}
henrike@webrtc.org1e09a712013-07-26 19:17:591816
wu@webrtc.org9dba5252013-08-05 20:36:571817bool WebRtcVoiceMediaChannel::AddSendStream(const StreamParams& sp) {
Peter Boströmca8b4042016-03-08 22:24:131818 TRACE_EVENT0("webrtc", "WebRtcVoiceMediaChannel::AddSendStream");
solenberg566ef242015-11-06 23:34:491819 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
Mirko Bonadei675513b2017-11-09 10:09:251820 RTC_LOG(LS_INFO) << "AddSendStream: " << sp.ToString();
solenberg0a617e22015-10-20 22:49:381821
1822 uint32_t ssrc = sp.first_ssrc();
1823 RTC_DCHECK(0 != ssrc);
1824
1825 if (GetSendChannelId(ssrc) != -1) {
Mirko Bonadei675513b2017-11-09 10:09:251826 RTC_LOG(LS_ERROR) << "Stream already exists with ssrc " << ssrc;
wu@webrtc.org9dba5252013-08-05 20:36:571827 return false;
1828 }
1829
solenberg0a617e22015-10-20 22:49:381830 // Create a new channel for sending audio data.
1831 int channel = CreateVoEChannel();
1832 if (channel == -1) {
1833 return false;
wu@webrtc.org9dba5252013-08-05 20:36:571834 }
wu@webrtc.org9dba5252013-08-05 20:36:571835
solenbergc96df772015-10-21 20:01:531836 // Save the channel to send_streams_, so that RemoveSendStream() can still
wu@webrtc.org9dba5252013-08-05 20:36:571837 // delete the channel in case failure happens below.
mallinath@webrtc.org67ee6b92014-02-03 16:57:161838 webrtc::AudioTransport* audio_transport =
1839 engine()->voe()->base()->audio_transport();
mflodman3d7db262016-04-29 07:57:131840
minyue6b825df2016-10-31 11:08:321841 rtc::Optional<std::string> audio_network_adaptor_config =
1842 GetAudioNetworkAdaptorConfig(options_);
skvlade0d46372016-04-08 05:59:221843 WebRtcAudioSendStream* stream = new WebRtcAudioSendStream(
Alex Narestb3944f02017-10-13 12:56:181844 channel, audio_transport, ssrc, sp.cname, sp.id, send_codec_spec_,
minyue6b825df2016-10-31 11:08:321845 send_rtp_extensions_, max_send_bitrate_bps_, audio_network_adaptor_config,
ossu20a4b3f2017-04-27 09:08:521846 call_, this, engine()->encoder_factory_);
skvlade0d46372016-04-08 05:59:221847 send_streams_.insert(std::make_pair(ssrc, stream));
wu@webrtc.org9dba5252013-08-05 20:36:571848
solenberg4a0f7b52016-06-16 20:07:331849 // At this point the stream's local SSRC has been updated. If it is the first
1850 // send stream, make sure that all the receive streams are updated with the
1851 // same SSRC in order to send receiver reports.
solenbergc96df772015-10-21 20:01:531852 if (send_streams_.size() == 1) {
solenberg0a617e22015-10-20 22:49:381853 receiver_reports_ssrc_ = ssrc;
solenberg4a0f7b52016-06-16 20:07:331854 for (const auto& kv : recv_streams_) {
1855 // TODO(solenberg): Allow applications to set the RTCP SSRC of receive
1856 // streams instead, so we can avoid recreating the streams here.
1857 kv.second->RecreateAudioReceiveStream(ssrc);
wu@webrtc.org9dba5252013-08-05 20:36:571858 }
1859 }
1860
Taylor Brandstetter1a018dc2016-03-08 20:37:391861 send_streams_[ssrc]->SetSend(send_);
1862 return true;
wu@webrtc.org9dba5252013-08-05 20:36:571863}
1864
Peter Boström0c4e06b2015-10-07 10:23:211865bool WebRtcVoiceMediaChannel::RemoveSendStream(uint32_t ssrc) {
Peter Boströmca8b4042016-03-08 22:24:131866 TRACE_EVENT0("webrtc", "WebRtcVoiceMediaChannel::RemoveSendStream");
solenberg566ef242015-11-06 23:34:491867 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
Mirko Bonadei675513b2017-11-09 10:09:251868 RTC_LOG(LS_INFO) << "RemoveSendStream: " << ssrc;
solenberg3a941542015-11-16 15:34:501869
solenbergc96df772015-10-21 20:01:531870 auto it = send_streams_.find(ssrc);
1871 if (it == send_streams_.end()) {
Mirko Bonadei675513b2017-11-09 10:09:251872 RTC_LOG(LS_WARNING) << "Try to remove stream with ssrc " << ssrc
1873 << " which doesn't exist.";
wu@webrtc.org9dba5252013-08-05 20:36:571874 return false;
1875 }
1876
Taylor Brandstetter1a018dc2016-03-08 20:37:391877 it->second->SetSend(false);
wu@webrtc.org9dba5252013-08-05 20:36:571878
solenberg7602aab2016-11-14 19:30:071879 // TODO(solenberg): If we're removing the receiver_reports_ssrc_ stream, find
1880 // the first active send stream and use that instead, reassociating receive
1881 // streams.
1882
solenberg7add0582015-11-20 17:59:341883 // Clean up and delete the send stream+channel.
Taylor Brandstetter1a018dc2016-03-08 20:37:391884 int channel = it->second->channel();
Mirko Bonadei675513b2017-11-09 10:09:251885 RTC_LOG(LS_INFO) << "Removing audio send stream " << ssrc
1886 << " with VoiceEngine channel #" << channel << ".";
solenberg7add0582015-11-20 17:59:341887 delete it->second;
1888 send_streams_.erase(it);
1889 if (!DeleteVoEChannel(channel)) {
solenberg0a617e22015-10-20 22:49:381890 return false;
wu@webrtc.org9dba5252013-08-05 20:36:571891 }
solenbergc96df772015-10-21 20:01:531892 if (send_streams_.empty()) {
Taylor Brandstetter1a018dc2016-03-08 20:37:391893 SetSend(false);
solenberg0a617e22015-10-20 22:49:381894 }
henrike@webrtc.org28e20752013-07-10 00:45:361895 return true;
1896}
1897
1898bool WebRtcVoiceMediaChannel::AddRecvStream(const StreamParams& sp) {
Peter Boströmca8b4042016-03-08 22:24:131899 TRACE_EVENT0("webrtc", "WebRtcVoiceMediaChannel::AddRecvStream");
solenberg566ef242015-11-06 23:34:491900 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
Mirko Bonadei675513b2017-11-09 10:09:251901 RTC_LOG(LS_INFO) << "AddRecvStream: " << sp.ToString();
solenbergd97ec302015-10-07 08:40:331902
solenberg0b675462015-10-09 08:37:091903 if (!ValidateStreamParams(sp)) {
wu@webrtc.org78187522013-10-07 23:32:021904 return false;
1905 }
1906
solenberg7add0582015-11-20 17:59:341907 const uint32_t ssrc = sp.first_ssrc();
solenberg0b675462015-10-09 08:37:091908 if (ssrc == 0) {
Mirko Bonadei675513b2017-11-09 10:09:251909 RTC_LOG(LS_WARNING) << "AddRecvStream with ssrc==0 is not supported.";
solenberg0b675462015-10-09 08:37:091910 return false;
1911 }
1912
solenberg2100c0b2017-03-01 19:29:291913 // If this stream was previously received unsignaled, we promote it, possibly
1914 // recreating the AudioReceiveStream, if sync_label has changed.
1915 if (MaybeDeregisterUnsignaledRecvStream(ssrc)) {
solenberg4904fb62017-02-17 20:01:141916 recv_streams_[ssrc]->MaybeRecreateAudioReceiveStream(sp.sync_label);
solenberg4904fb62017-02-17 20:01:141917 return true;
solenberg1ac56142015-10-13 10:58:191918 }
solenberg0b675462015-10-09 08:37:091919
solenberg7add0582015-11-20 17:59:341920 if (GetReceiveChannelId(ssrc) != -1) {
Mirko Bonadei675513b2017-11-09 10:09:251921 RTC_LOG(LS_ERROR) << "Stream already exists with ssrc " << ssrc;
henrike@webrtc.org28e20752013-07-10 00:45:361922 return false;
1923 }
Fredrik Solenberg4b60c732015-05-07 12:07:481924
henrike@webrtc.org28e20752013-07-10 00:45:361925 // Create a new channel for receiving audio data.
solenberg7add0582015-11-20 17:59:341926 const int channel = CreateVoEChannel();
henrike@webrtc.org28e20752013-07-10 00:45:361927 if (channel == -1) {
henrike@webrtc.org28e20752013-07-10 00:45:361928 return false;
1929 }
Minyue2013aec2015-05-13 12:14:421930
stefanba4c0e42016-02-04 12:12:241931 recv_streams_.insert(std::make_pair(
kwiberg1c07c702017-03-27 14:15:491932 ssrc,
1933 new WebRtcAudioReceiveStream(
1934 channel, ssrc, receiver_reports_ssrc_, recv_transport_cc_enabled_,
1935 recv_nack_enabled_, sp.sync_label, recv_rtp_extensions_, call_, this,
1936 engine()->decoder_factory_, decoder_map_)));
aleloi84ef6152016-08-04 12:28:211937 recv_streams_[ssrc]->SetPlayout(playout_);
solenberg7add0582015-11-20 17:59:341938
solenberg1ac56142015-10-13 10:58:191939 return true;
henrike@webrtc.org28e20752013-07-10 00:45:361940}
1941
Peter Boström0c4e06b2015-10-07 10:23:211942bool WebRtcVoiceMediaChannel::RemoveRecvStream(uint32_t ssrc) {
Peter Boströmca8b4042016-03-08 22:24:131943 TRACE_EVENT0("webrtc", "WebRtcVoiceMediaChannel::RemoveRecvStream");
solenberg566ef242015-11-06 23:34:491944 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
Mirko Bonadei675513b2017-11-09 10:09:251945 RTC_LOG(LS_INFO) << "RemoveRecvStream: " << ssrc;
solenbergd97ec302015-10-07 08:40:331946
solenberg7add0582015-11-20 17:59:341947 const auto it = recv_streams_.find(ssrc);
1948 if (it == recv_streams_.end()) {
Mirko Bonadei675513b2017-11-09 10:09:251949 RTC_LOG(LS_WARNING) << "Try to remove stream with ssrc " << ssrc
1950 << " which doesn't exist.";
henrike@webrtc.org1e09a712013-07-26 19:17:591951 return false;
wu@webrtc.org9dba5252013-08-05 20:36:571952 }
henrike@webrtc.org28e20752013-07-10 00:45:361953
solenberg2100c0b2017-03-01 19:29:291954 MaybeDeregisterUnsignaledRecvStream(ssrc);
henrike@webrtc.org1e09a712013-07-26 19:17:591955
solenberg7add0582015-11-20 17:59:341956 const int channel = it->second->channel();
1957
1958 // Clean up and delete the receive stream+channel.
Mirko Bonadei675513b2017-11-09 10:09:251959 RTC_LOG(LS_INFO) << "Removing audio receive stream " << ssrc
1960 << " with VoiceEngine channel #" << channel << ".";
Tommif888bb52015-12-12 00:37:011961 it->second->SetRawAudioSink(nullptr);
solenberg7add0582015-11-20 17:59:341962 delete it->second;
1963 recv_streams_.erase(it);
1964 return DeleteVoEChannel(channel);
henrike@webrtc.org28e20752013-07-10 00:45:361965}
1966
Taylor Brandstetter1a018dc2016-03-08 20:37:391967bool WebRtcVoiceMediaChannel::SetLocalSource(uint32_t ssrc,
1968 AudioSource* source) {
solenbergc96df772015-10-21 20:01:531969 auto it = send_streams_.find(ssrc);
1970 if (it == send_streams_.end()) {
Taylor Brandstetter1a018dc2016-03-08 20:37:391971 if (source) {
1972 // Return an error if trying to set a valid source with an invalid ssrc.
Mirko Bonadei675513b2017-11-09 10:09:251973 RTC_LOG(LS_ERROR) << "SetLocalSource failed with ssrc " << ssrc;
wu@webrtc.org9dba5252013-08-05 20:36:571974 return false;
1975 }
1976
1977 // The channel likely has gone away, do nothing.
henrike@webrtc.org1e09a712013-07-26 19:17:591978 return true;
henrike@webrtc.org1e09a712013-07-26 19:17:591979 }
1980
Taylor Brandstetter1a018dc2016-03-08 20:37:391981 if (source) {
1982 it->second->SetSource(source);
solenberg1ac56142015-10-13 10:58:191983 } else {
Taylor Brandstetter1a018dc2016-03-08 20:37:391984 it->second->ClearSource();
solenberg1ac56142015-10-13 10:58:191985 }
henrike@webrtc.org1e09a712013-07-26 19:17:591986
henrike@webrtc.org28e20752013-07-10 00:45:361987 return true;
1988}
1989
solenberg796b8f92017-03-02 01:02:231990// TODO(solenberg): Remove, once AudioMonitor is gone.
henrike@webrtc.org28e20752013-07-10 00:45:361991bool WebRtcVoiceMediaChannel::GetActiveStreams(
1992 AudioInfo::StreamList* actives) {
solenberg566ef242015-11-06 23:34:491993 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
henrike@webrtc.org28e20752013-07-10 00:45:361994 actives->clear();
solenberg7add0582015-11-20 17:59:341995 for (const auto& ch : recv_streams_) {
solenberg796b8f92017-03-02 01:02:231996 int level = ch.second->GetOutputLevel();
henrike@webrtc.org28e20752013-07-10 00:45:361997 if (level > 0) {
Fredrik Solenbergaf9fb212015-08-26 08:45:531998 actives->push_back(std::make_pair(ch.first, level));
henrike@webrtc.org28e20752013-07-10 00:45:361999 }
2000 }
2001 return true;
2002}
2003
solenberg796b8f92017-03-02 01:02:232004// TODO(solenberg): Remove, once AudioMonitor is gone.
henrike@webrtc.org28e20752013-07-10 00:45:362005int WebRtcVoiceMediaChannel::GetOutputLevel() {
solenberg566ef242015-11-06 23:34:492006 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
solenberg1ac56142015-10-13 10:58:192007 int highest = 0;
solenberg7add0582015-11-20 17:59:342008 for (const auto& ch : recv_streams_) {
solenberg796b8f92017-03-02 01:02:232009 highest = std::max(ch.second->GetOutputLevel(), highest);
henrike@webrtc.org28e20752013-07-10 00:45:362010 }
2011 return highest;
2012}
2013
solenberg4bac9c52015-10-09 09:32:532014bool WebRtcVoiceMediaChannel::SetOutputVolume(uint32_t ssrc, double volume) {
solenberg566ef242015-11-06 23:34:492015 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
solenberg2100c0b2017-03-01 19:29:292016 std::vector<uint32_t> ssrcs(1, ssrc);
deadbeef3bc15102017-04-21 02:25:072017 // SSRC of 0 represents the default receive stream.
solenberg1ac56142015-10-13 10:58:192018 if (ssrc == 0) {
2019 default_recv_volume_ = volume;
solenberg2100c0b2017-03-01 19:29:292020 ssrcs = unsignaled_recv_ssrcs_;
2021 }
2022 for (uint32_t ssrc : ssrcs) {
2023 const auto it = recv_streams_.find(ssrc);
2024 if (it == recv_streams_.end()) {
Mirko Bonadei675513b2017-11-09 10:09:252025 RTC_LOG(LS_WARNING) << "SetOutputVolume: no recv stream " << ssrc;
solenberg2100c0b2017-03-01 19:29:292026 return false;
henrike@webrtc.org28e20752013-07-10 00:45:362027 }
solenberg2100c0b2017-03-01 19:29:292028 it->second->SetOutputVolume(volume);
Mirko Bonadei675513b2017-11-09 10:09:252029 RTC_LOG(LS_INFO) << "SetOutputVolume() to " << volume
2030 << " for recv stream with ssrc " << ssrc;
solenberg1ac56142015-10-13 10:58:192031 }
henrike@webrtc.org28e20752013-07-10 00:45:362032 return true;
2033}
2034
henrike@webrtc.org28e20752013-07-10 00:45:362035bool WebRtcVoiceMediaChannel::CanInsertDtmf() {
Fredrik Solenbergb5727682015-12-04 14:22:192036 return dtmf_payload_type_ ? true : false;
henrike@webrtc.org28e20752013-07-10 00:45:362037}
2038
solenberg1d63dd02015-12-02 20:35:092039bool WebRtcVoiceMediaChannel::InsertDtmf(uint32_t ssrc, int event,
2040 int duration) {
solenberg566ef242015-11-06 23:34:492041 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
Mirko Bonadei675513b2017-11-09 10:09:252042 RTC_LOG(LS_INFO) << "WebRtcVoiceMediaChannel::InsertDtmf";
Fredrik Solenbergb5727682015-12-04 14:22:192043 if (!dtmf_payload_type_) {
henrike@webrtc.org28e20752013-07-10 00:45:362044 return false;
2045 }
2046
Fredrik Solenbergb5727682015-12-04 14:22:192047 // Figure out which WebRtcAudioSendStream to send the event on.
2048 auto it = ssrc != 0 ? send_streams_.find(ssrc) : send_streams_.begin();
2049 if (it == send_streams_.end()) {
Mirko Bonadei675513b2017-11-09 10:09:252050 RTC_LOG(LS_WARNING) << "The specified ssrc " << ssrc << " is not in use.";
solenberg1d63dd02015-12-02 20:35:092051 return false;
2052 }
Fredrik Solenbergb5727682015-12-04 14:22:192053 if (event < kMinTelephoneEventCode ||
2054 event > kMaxTelephoneEventCode) {
Mirko Bonadei675513b2017-11-09 10:09:252055 RTC_LOG(LS_WARNING) << "DTMF event code " << event << " out of range.";
solenberg1d63dd02015-12-02 20:35:092056 return false;
henrike@webrtc.org28e20752013-07-10 00:45:362057 }
solenbergffbbcac2016-11-17 13:25:372058 RTC_DCHECK_NE(-1, dtmf_payload_freq_);
2059 return it->second->SendTelephoneEvent(*dtmf_payload_type_, dtmf_payload_freq_,
2060 event, duration);
henrike@webrtc.org28e20752013-07-10 00:45:362061}
2062
wu@webrtc.orga9890802013-12-13 00:21:032063void WebRtcVoiceMediaChannel::OnPacketReceived(
jbaucheec21bd2016-03-20 13:15:432064 rtc::CopyOnWriteBuffer* packet, const rtc::PacketTime& packet_time) {
solenberg566ef242015-11-06 23:34:492065 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
Fredrik Solenberg4b60c732015-05-07 12:07:482066
mflodman3d7db262016-04-29 07:57:132067 const webrtc::PacketTime webrtc_packet_time(packet_time.timestamp,
2068 packet_time.not_before);
2069 webrtc::PacketReceiver::DeliveryStatus delivery_result =
2070 call_->Receiver()->DeliverPacket(webrtc::MediaType::AUDIO,
2071 packet->cdata(), packet->size(),
2072 webrtc_packet_time);
mflodman3d7db262016-04-29 07:57:132073 if (delivery_result != webrtc::PacketReceiver::DELIVERY_UNKNOWN_SSRC) {
2074 return;
2075 }
2076
solenberg2100c0b2017-03-01 19:29:292077 // Create an unsignaled receive stream for this previously not received ssrc.
2078 // If there already is N unsignaled receive streams, delete the oldest.
mflodman3d7db262016-04-29 07:57:132079 // See: https://bugs.chromium.org/p/webrtc/issues/detail?id=5208
solenberg1ac56142015-10-13 10:58:192080 uint32_t ssrc = 0;
jbaucheec21bd2016-03-20 13:15:432081 if (!GetRtpSsrc(packet->cdata(), packet->size(), &ssrc)) {
solenberg1ac56142015-10-13 10:58:192082 return;
2083 }
solenberg2100c0b2017-03-01 19:29:292084 RTC_DCHECK(std::find(unsignaled_recv_ssrcs_.begin(),
2085 unsignaled_recv_ssrcs_.end(), ssrc) == unsignaled_recv_ssrcs_.end());
solenberg1ac56142015-10-13 10:58:192086
solenberg2100c0b2017-03-01 19:29:292087 // Add new stream.
mflodman3d7db262016-04-29 07:57:132088 StreamParams sp;
2089 sp.ssrcs.push_back(ssrc);
Mirko Bonadei675513b2017-11-09 10:09:252090 RTC_LOG(LS_INFO) << "Creating unsignaled receive stream for SSRC=" << ssrc;
mflodman3d7db262016-04-29 07:57:132091 if (!AddRecvStream(sp)) {
Mirko Bonadei675513b2017-11-09 10:09:252092 RTC_LOG(LS_WARNING) << "Could not create unsignaled receive stream.";
mflodman3d7db262016-04-29 07:57:132093 return;
henrike@webrtc.org28e20752013-07-10 00:45:362094 }
solenberg2100c0b2017-03-01 19:29:292095 unsignaled_recv_ssrcs_.push_back(ssrc);
2096 RTC_HISTOGRAM_COUNTS_LINEAR(
2097 "WebRTC.Audio.NumOfUnsignaledStreams", unsignaled_recv_ssrcs_.size(), 1,
2098 100, 101);
solenbergf748ca42017-02-06 21:03:192099
solenberg2100c0b2017-03-01 19:29:292100 // Remove oldest unsignaled stream, if we have too many.
2101 if (unsignaled_recv_ssrcs_.size() > kMaxUnsignaledRecvStreams) {
2102 uint32_t remove_ssrc = unsignaled_recv_ssrcs_.front();
Mirko Bonadei675513b2017-11-09 10:09:252103 RTC_LOG(LS_INFO) << "Removing unsignaled receive stream with SSRC="
2104 << remove_ssrc;
solenberg2100c0b2017-03-01 19:29:292105 RemoveRecvStream(remove_ssrc);
2106 }
2107 RTC_DCHECK_GE(kMaxUnsignaledRecvStreams, unsignaled_recv_ssrcs_.size());
2108
2109 SetOutputVolume(ssrc, default_recv_volume_);
2110
2111 // The default sink can only be attached to one stream at a time, so we hook
2112 // it up to the *latest* unsignaled stream we've seen, in order to support the
2113 // case where the SSRC of one unsignaled stream changes.
mflodman3d7db262016-04-29 07:57:132114 if (default_sink_) {
solenberg2100c0b2017-03-01 19:29:292115 for (uint32_t drop_ssrc : unsignaled_recv_ssrcs_) {
2116 auto it = recv_streams_.find(drop_ssrc);
2117 it->second->SetRawAudioSink(nullptr);
2118 }
mflodman3d7db262016-04-29 07:57:132119 std::unique_ptr<webrtc::AudioSinkInterface> proxy_sink(
2120 new ProxySink(default_sink_.get()));
solenberg2100c0b2017-03-01 19:29:292121 SetRawAudioSink(ssrc, std::move(proxy_sink));
mflodman3d7db262016-04-29 07:57:132122 }
solenberg2100c0b2017-03-01 19:29:292123
mflodman3d7db262016-04-29 07:57:132124 delivery_result = call_->Receiver()->DeliverPacket(webrtc::MediaType::AUDIO,
2125 packet->cdata(),
2126 packet->size(),
2127 webrtc_packet_time);
2128 RTC_DCHECK_NE(webrtc::PacketReceiver::DELIVERY_UNKNOWN_SSRC, delivery_result);
henrike@webrtc.org28e20752013-07-10 00:45:362129}
2130
wu@webrtc.orga9890802013-12-13 00:21:032131void WebRtcVoiceMediaChannel::OnRtcpReceived(
jbaucheec21bd2016-03-20 13:15:432132 rtc::CopyOnWriteBuffer* packet, const rtc::PacketTime& packet_time) {
solenberg566ef242015-11-06 23:34:492133 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
Fredrik Solenberg4b60c732015-05-07 12:07:482134
Fredrik Solenberg709ed672015-09-15 10:26:332135 // Forward packet to Call as well.
2136 const webrtc::PacketTime webrtc_packet_time(packet_time.timestamp,
2137 packet_time.not_before);
2138 call_->Receiver()->DeliverPacket(webrtc::MediaType::AUDIO,
jbaucheec21bd2016-03-20 13:15:432139 packet->cdata(), packet->size(), webrtc_packet_time);
henrike@webrtc.org28e20752013-07-10 00:45:362140}
2141
Honghai Zhangcc411c02016-03-30 00:27:212142void WebRtcVoiceMediaChannel::OnNetworkRouteChanged(
2143 const std::string& transport_name,
Honghai Zhang0e533ef2016-04-19 22:41:362144 const rtc::NetworkRoute& network_route) {
Zhi Huang5f5918f2017-11-13 01:26:232145 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
2146 // TODO(zhihaung): Merge these two callbacks.
Honghai Zhang0e533ef2016-04-19 22:41:362147 call_->OnNetworkRouteChanged(transport_name, network_route);
Zhi Huang5f5918f2017-11-13 01:26:232148 call_->OnTransportOverheadChanged(webrtc::MediaType::AUDIO,
2149 network_route.packet_overhead);
Honghai Zhangcc411c02016-03-30 00:27:212150}
2151
Peter Boström0c4e06b2015-10-07 10:23:212152bool WebRtcVoiceMediaChannel::MuteStream(uint32_t ssrc, bool muted) {
solenberg566ef242015-11-06 23:34:492153 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
solenberg94218532016-06-16 17:53:222154 const auto it = send_streams_.find(ssrc);
2155 if (it == send_streams_.end()) {
Mirko Bonadei675513b2017-11-09 10:09:252156 RTC_LOG(LS_WARNING) << "The specified ssrc " << ssrc << " is not in use.";
henrike@webrtc.org28e20752013-07-10 00:45:362157 return false;
2158 }
solenberg94218532016-06-16 17:53:222159 it->second->SetMuted(muted);
2160
2161 // TODO(solenberg):
buildbot@webrtc.org6b21b712014-07-31 15:08:532162 // We set the AGC to mute state only when all the channels are muted.
2163 // This implementation is not ideal, instead we should signal the AGC when
2164 // the mic channel is muted/unmuted. We can't do it today because there
2165 // is no good way to know which stream is mapping to the mic channel.
2166 bool all_muted = muted;
solenberg94218532016-06-16 17:53:222167 for (const auto& kv : send_streams_) {
2168 all_muted = all_muted && kv.second->muted();
buildbot@webrtc.org6b21b712014-07-31 15:08:532169 }
solenberg059fb442016-10-26 12:12:242170 engine()->apm()->set_output_will_be_muted(all_muted);
buildbot@webrtc.org6b21b712014-07-31 15:08:532171
henrike@webrtc.org28e20752013-07-10 00:45:362172 return true;
2173}
2174
deadbeef80346142016-04-27 21:17:102175bool WebRtcVoiceMediaChannel::SetMaxSendBitrate(int bps) {
Mirko Bonadei675513b2017-11-09 10:09:252176 RTC_LOG(LS_INFO) << "WebRtcVoiceMediaChannel::SetMaxSendBitrate.";
deadbeef80346142016-04-27 21:17:102177 max_send_bitrate_bps_ = bps;
minyue7a973442016-10-20 10:27:122178 bool success = true;
skvlade0d46372016-04-08 05:59:222179 for (const auto& kv : send_streams_) {
minyue7a973442016-10-20 10:27:122180 if (!kv.second->SetMaxSendBitrate(max_send_bitrate_bps_)) {
2181 success = false;
skvlade0d46372016-04-08 05:59:222182 }
2183 }
minyue7a973442016-10-20 10:27:122184 return success;
henrike@webrtc.org28e20752013-07-10 00:45:362185}
2186
skvlad7a43d252016-03-22 22:32:272187void WebRtcVoiceMediaChannel::OnReadyToSend(bool ready) {
2188 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
Mirko Bonadei675513b2017-11-09 10:09:252189 RTC_LOG(LS_VERBOSE) << "OnReadyToSend: " << (ready ? "Ready." : "Not ready.");
skvlad7a43d252016-03-22 22:32:272190 call_->SignalChannelNetworkState(
2191 webrtc::MediaType::AUDIO,
2192 ready ? webrtc::kNetworkUp : webrtc::kNetworkDown);
2193}
2194
henrike@webrtc.org28e20752013-07-10 00:45:362195bool WebRtcVoiceMediaChannel::GetStats(VoiceMediaInfo* info) {
Peter Boströmca8b4042016-03-08 22:24:132196 TRACE_EVENT0("webrtc", "WebRtcVoiceMediaChannel::GetStats");
solenberg566ef242015-11-06 23:34:492197 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
solenberg85a04962015-10-27 10:35:212198 RTC_DCHECK(info);
solenbergd97ec302015-10-07 08:40:332199
solenberg85a04962015-10-27 10:35:212200 // Get SSRC and stats for each sender.
hbos1acfbd22016-11-18 07:43:292201 RTC_DCHECK_EQ(info->senders.size(), 0U);
solenberg85a04962015-10-27 10:35:212202 for (const auto& stream : send_streams_) {
Ivo Creusen56d460902017-11-24 16:29:592203 webrtc::AudioSendStream::Stats stats =
2204 stream.second->GetStats(recv_streams_.size() > 0);
wu@webrtc.org9dba5252013-08-05 20:36:572205 VoiceSenderInfo sinfo;
solenberg85a04962015-10-27 10:35:212206 sinfo.add_ssrc(stats.local_ssrc);
2207 sinfo.bytes_sent = stats.bytes_sent;
2208 sinfo.packets_sent = stats.packets_sent;
2209 sinfo.packets_lost = stats.packets_lost;
2210 sinfo.fraction_lost = stats.fraction_lost;
2211 sinfo.codec_name = stats.codec_name;
hbos1acfbd22016-11-18 07:43:292212 sinfo.codec_payload_type = stats.codec_payload_type;
solenberg85a04962015-10-27 10:35:212213 sinfo.ext_seqnum = stats.ext_seqnum;
2214 sinfo.jitter_ms = stats.jitter_ms;
2215 sinfo.rtt_ms = stats.rtt_ms;
2216 sinfo.audio_level = stats.audio_level;
zsteine76bd3a2017-07-14 19:17:492217 sinfo.total_input_energy = stats.total_input_energy;
2218 sinfo.total_input_duration = stats.total_input_duration;
Taylor Brandstetter1a018dc2016-03-08 20:37:392219 sinfo.typing_noise_detected = (send_ ? stats.typing_noise_detected : false);
ivoce1198e02017-09-08 15:13:192220 sinfo.ana_statistics = stats.ana_statistics;
Ivo Creusen56d460902017-11-24 16:29:592221 sinfo.apm_statistics = stats.apm_statistics;
wu@webrtc.org9dba5252013-08-05 20:36:572222 info->senders.push_back(sinfo);
henrike@webrtc.org28e20752013-07-10 00:45:362223 }
2224
solenberg85a04962015-10-27 10:35:212225 // Get SSRC and stats for each receiver.
hbos1acfbd22016-11-18 07:43:292226 RTC_DCHECK_EQ(info->receivers.size(), 0U);
solenberg7add0582015-11-20 17:59:342227 for (const auto& stream : recv_streams_) {
deadbeef4e2deab2017-09-20 20:56:212228 uint32_t ssrc = stream.first;
2229 // When SSRCs are unsignaled, there's only one audio MediaStreamTrack, but
2230 // multiple RTP streams can be received over time (if the SSRC changes for
2231 // whatever reason). We only want the RTCMediaStreamTrackStats to represent
2232 // the stats for the most recent stream (the one whose audio is actually
2233 // routed to the MediaStreamTrack), so here we ignore any unsignaled SSRCs
2234 // except for the most recent one (last in the vector). This is somewhat of
2235 // a hack, and means you don't get *any* stats for these inactive streams,
2236 // but it's slightly better than the previous behavior, which was "highest
2237 // SSRC wins".
2238 // See: https://bugs.chromium.org/p/webrtc/issues/detail?id=8158
2239 if (!unsignaled_recv_ssrcs_.empty()) {
2240 auto end_it = --unsignaled_recv_ssrcs_.end();
2241 if (std::find(unsignaled_recv_ssrcs_.begin(), end_it, ssrc) != end_it) {
2242 continue;
2243 }
2244 }
Fredrik Solenberg4f4ec0a2015-10-22 08:49:272245 webrtc::AudioReceiveStream::Stats stats = stream.second->GetStats();
2246 VoiceReceiverInfo rinfo;
2247 rinfo.add_ssrc(stats.remote_ssrc);
2248 rinfo.bytes_rcvd = stats.bytes_rcvd;
2249 rinfo.packets_rcvd = stats.packets_rcvd;
2250 rinfo.packets_lost = stats.packets_lost;
2251 rinfo.fraction_lost = stats.fraction_lost;
2252 rinfo.codec_name = stats.codec_name;
hbos1acfbd22016-11-18 07:43:292253 rinfo.codec_payload_type = stats.codec_payload_type;
Fredrik Solenberg4f4ec0a2015-10-22 08:49:272254 rinfo.ext_seqnum = stats.ext_seqnum;
2255 rinfo.jitter_ms = stats.jitter_ms;
2256 rinfo.jitter_buffer_ms = stats.jitter_buffer_ms;
2257 rinfo.jitter_buffer_preferred_ms = stats.jitter_buffer_preferred_ms;
2258 rinfo.delay_estimate_ms = stats.delay_estimate_ms;
2259 rinfo.audio_level = stats.audio_level;
zsteine76bd3a2017-07-14 19:17:492260 rinfo.total_output_energy = stats.total_output_energy;
Steve Anton2dbc69f2017-08-25 00:15:132261 rinfo.total_samples_received = stats.total_samples_received;
zsteine76bd3a2017-07-14 19:17:492262 rinfo.total_output_duration = stats.total_output_duration;
Steve Anton2dbc69f2017-08-25 00:15:132263 rinfo.concealed_samples = stats.concealed_samples;
Gustaf Ullberg9a2e9062017-09-18 07:28:202264 rinfo.concealment_events = stats.concealment_events;
Gustaf Ullbergb0a02072017-10-02 10:00:342265 rinfo.jitter_buffer_delay_seconds = stats.jitter_buffer_delay_seconds;
Fredrik Solenberg4f4ec0a2015-10-22 08:49:272266 rinfo.expand_rate = stats.expand_rate;
2267 rinfo.speech_expand_rate = stats.speech_expand_rate;
2268 rinfo.secondary_decoded_rate = stats.secondary_decoded_rate;
minyue-webrtc0e320ec2017-08-28 11:51:272269 rinfo.secondary_discarded_rate = stats.secondary_discarded_rate;
Fredrik Solenberg4f4ec0a2015-10-22 08:49:272270 rinfo.accelerate_rate = stats.accelerate_rate;
2271 rinfo.preemptive_expand_rate = stats.preemptive_expand_rate;
2272 rinfo.decoding_calls_to_silence_generator =
2273 stats.decoding_calls_to_silence_generator;
2274 rinfo.decoding_calls_to_neteq = stats.decoding_calls_to_neteq;
2275 rinfo.decoding_normal = stats.decoding_normal;
2276 rinfo.decoding_plc = stats.decoding_plc;
2277 rinfo.decoding_cng = stats.decoding_cng;
2278 rinfo.decoding_plc_cng = stats.decoding_plc_cng;
henrik.lundin63489782016-09-20 08:47:122279 rinfo.decoding_muted_output = stats.decoding_muted_output;
Fredrik Solenberg4f4ec0a2015-10-22 08:49:272280 rinfo.capture_start_ntp_time_ms = stats.capture_start_ntp_time_ms;
2281 info->receivers.push_back(rinfo);
henrike@webrtc.org28e20752013-07-10 00:45:362282 }
2283
hbos1acfbd22016-11-18 07:43:292284 // Get codec info
2285 for (const AudioCodec& codec : send_codecs_) {
2286 webrtc::RtpCodecParameters codec_params = codec.ToCodecParameters();
2287 info->send_codecs.insert(
2288 std::make_pair(codec_params.payload_type, std::move(codec_params)));
2289 }
2290 for (const AudioCodec& codec : recv_codecs_) {
2291 webrtc::RtpCodecParameters codec_params = codec.ToCodecParameters();
2292 info->receive_codecs.insert(
2293 std::make_pair(codec_params.payload_type, std::move(codec_params)));
2294 }
2295
henrike@webrtc.org28e20752013-07-10 00:45:362296 return true;
2297}
2298
Tommif888bb52015-12-12 00:37:012299void WebRtcVoiceMediaChannel::SetRawAudioSink(
2300 uint32_t ssrc,
kwiberg686a8ef2016-02-26 11:00:352301 std::unique_ptr<webrtc::AudioSinkInterface> sink) {
Tommif888bb52015-12-12 00:37:012302 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
Mirko Bonadei675513b2017-11-09 10:09:252303 RTC_LOG(LS_VERBOSE) << "WebRtcVoiceMediaChannel::SetRawAudioSink: ssrc:"
2304 << ssrc << " " << (sink ? "(ptr)" : "NULL");
deadbeef884f5852016-01-15 17:20:042305 if (ssrc == 0) {
solenberg2100c0b2017-03-01 19:29:292306 if (!unsignaled_recv_ssrcs_.empty()) {
kwiberg686a8ef2016-02-26 11:00:352307 std::unique_ptr<webrtc::AudioSinkInterface> proxy_sink(
deadbeef884f5852016-01-15 17:20:042308 sink ? new ProxySink(sink.get()) : nullptr);
solenberg2100c0b2017-03-01 19:29:292309 SetRawAudioSink(unsignaled_recv_ssrcs_.back(), std::move(proxy_sink));
deadbeef884f5852016-01-15 17:20:042310 }
2311 default_sink_ = std::move(sink);
2312 return;
2313 }
Tommif888bb52015-12-12 00:37:012314 const auto it = recv_streams_.find(ssrc);
2315 if (it == recv_streams_.end()) {
Mirko Bonadei675513b2017-11-09 10:09:252316 RTC_LOG(LS_WARNING) << "SetRawAudioSink: no recv stream " << ssrc;
Tommif888bb52015-12-12 00:37:012317 return;
2318 }
deadbeef2d110be2016-01-13 20:00:262319 it->second->SetRawAudioSink(std::move(sink));
Tommif888bb52015-12-12 00:37:012320}
2321
hbos8d609f62017-04-10 14:39:052322std::vector<webrtc::RtpSource> WebRtcVoiceMediaChannel::GetSources(
2323 uint32_t ssrc) const {
2324 auto it = recv_streams_.find(ssrc);
2325 RTC_DCHECK(it != recv_streams_.end())
2326 << "Attempting to get contributing sources for SSRC:" << ssrc
2327 << " which doesn't exist.";
2328 return it->second->GetSources();
2329}
2330
Peter Boström0c4e06b2015-10-07 10:23:212331int WebRtcVoiceMediaChannel::GetReceiveChannelId(uint32_t ssrc) const {
solenberg566ef242015-11-06 23:34:492332 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
solenberg7add0582015-11-20 17:59:342333 const auto it = recv_streams_.find(ssrc);
2334 if (it != recv_streams_.end()) {
mallinath@webrtc.org67ee6b92014-02-03 16:57:162335 return it->second->channel();
solenberg8fb30c32015-10-13 10:06:582336 }
solenberg1ac56142015-10-13 10:58:192337 return -1;
henrike@webrtc.org28e20752013-07-10 00:45:362338}
2339
Peter Boström0c4e06b2015-10-07 10:23:212340int WebRtcVoiceMediaChannel::GetSendChannelId(uint32_t ssrc) const {
solenberg566ef242015-11-06 23:34:492341 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
solenbergc96df772015-10-21 20:01:532342 const auto it = send_streams_.find(ssrc);
2343 if (it != send_streams_.end()) {
mallinath@webrtc.org67ee6b92014-02-03 16:57:162344 return it->second->channel();
solenberg8fb30c32015-10-13 10:06:582345 }
wu@webrtc.org9dba5252013-08-05 20:36:572346 return -1;
henrike@webrtc.org28e20752013-07-10 00:45:362347}
solenberg2100c0b2017-03-01 19:29:292348
2349bool WebRtcVoiceMediaChannel::
2350 MaybeDeregisterUnsignaledRecvStream(uint32_t ssrc) {
2351 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
2352 auto it = std::find(unsignaled_recv_ssrcs_.begin(),
2353 unsignaled_recv_ssrcs_.end(),
2354 ssrc);
2355 if (it != unsignaled_recv_ssrcs_.end()) {
2356 unsignaled_recv_ssrcs_.erase(it);
2357 return true;
2358 }
2359 return false;
2360}
henrike@webrtc.org28e20752013-07-10 00:45:362361} // namespace cricket
2362
2363#endif // HAVE_WEBRTC_VOICE