blob: a61b8c42844efe67dc7109534080b9036a26a62b [file] [log] [blame]
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001/*
kjellander1afca732016-02-07 20:46:45 -08002 * Copyright (c) 2004 The WebRTC project authors. All Rights Reserved.
henrike@webrtc.org28e20752013-07-10 00:45:36 +00003 *
kjellander1afca732016-02-07 20:46:45 -08004 * Use of this source code is governed by a BSD-style license
5 * that can be found in the LICENSE file in the root of the source
6 * tree. An additional intellectual property rights grant can be found
7 * in the file PATENTS. All contributing project authors may
8 * be found in the AUTHORS file in the root of the source tree.
henrike@webrtc.org28e20752013-07-10 00:45:36 +00009 */
10
henrike@webrtc.org28e20752013-07-10 00:45:36 +000011#ifdef HAVE_WEBRTC_VOICE
12
kjellander@webrtc.org5ad12972016-02-12 06:39:40 +010013#include "webrtc/media/engine/webrtcvoiceengine.h"
henrike@webrtc.org28e20752013-07-10 00:45:36 +000014
15#include <algorithm>
16#include <cstdio>
ossuc54071d2016-08-17 02:45:41 -070017#include <functional>
henrike@webrtc.org28e20752013-07-10 00:45:36 +000018#include <string>
19#include <vector>
20
kjellander@webrtc.org7ffeab52016-02-26 22:46:09 +010021#include "webrtc/audio_sink.h"
tfarina5237aaf2015-11-10 23:44:30 -080022#include "webrtc/base/arraysize.h"
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +000023#include "webrtc/base/base64.h"
24#include "webrtc/base/byteorder.h"
25#include "webrtc/base/common.h"
kwiberg4485ffb2016-04-26 08:14:39 -070026#include "webrtc/base/constructormagic.h"
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +000027#include "webrtc/base/helpers.h"
28#include "webrtc/base/logging.h"
29#include "webrtc/base/stringencode.h"
30#include "webrtc/base/stringutils.h"
Peter Boströmca8b4042016-03-08 14:24:13 -080031#include "webrtc/base/trace_event.h"
mallinath@webrtc.orga27be8e2013-09-27 23:04:10 +000032#include "webrtc/common.h"
Taylor Brandstetter1a018dc2016-03-08 12:37:39 -080033#include "webrtc/media/base/audiosource.h"
kjellanderf4752772016-03-02 05:42:30 -080034#include "webrtc/media/base/mediaconstants.h"
kjellandera96e2d72016-02-04 23:52:28 -080035#include "webrtc/media/base/streamparams.h"
ossuc54071d2016-08-17 02:45:41 -070036#include "webrtc/media/engine/payload_type_mapper.h"
kjellander@webrtc.org5ad12972016-02-12 06:39:40 +010037#include "webrtc/media/engine/webrtcmediaengine.h"
38#include "webrtc/media/engine/webrtcvoe.h"
solenberg26c8c912015-11-27 04:00:25 -080039#include "webrtc/modules/audio_coding/acm2/rent_a_codec.h"
henrike@webrtc.org28e20752013-07-10 00:45:36 +000040#include "webrtc/modules/audio_processing/include/audio_processing.h"
Henrik Kjellander98f53512015-10-28 18:17:40 +010041#include "webrtc/system_wrappers/include/field_trial.h"
solenbergbd138382015-11-20 16:08:07 -080042#include "webrtc/system_wrappers/include/trace.h"
henrike@webrtc.org28e20752013-07-10 00:45:36 +000043
henrike@webrtc.org28e20752013-07-10 00:45:36 +000044namespace cricket {
solenbergd97ec302015-10-07 01:40:33 -070045namespace {
henrike@webrtc.org28e20752013-07-10 00:45:36 +000046
solenbergbd138382015-11-20 16:08:07 -080047const int kDefaultTraceFilter = webrtc::kTraceNone | webrtc::kTraceTerseInfo |
48 webrtc::kTraceWarning | webrtc::kTraceError |
49 webrtc::kTraceCritical;
50const int kElevatedTraceFilter = kDefaultTraceFilter | webrtc::kTraceStateInfo |
51 webrtc::kTraceInfo;
52
henrike@webrtc.org28e20752013-07-10 00:45:36 +000053// On Windows Vista and newer, Microsoft introduced the concept of "Default
54// Communications Device". This means that there are two types of default
55// devices (old Wave Audio style default and Default Communications Device).
56//
57// On Windows systems which only support Wave Audio style default, uses either
58// -1 or 0 to select the default device.
henrike@webrtc.org28e20752013-07-10 00:45:36 +000059#ifdef WIN32
solenbergd97ec302015-10-07 01:40:33 -070060const int kDefaultAudioDeviceId = -1;
solenberg8ad582d2016-03-16 09:34:56 -070061#elif !defined(WEBRTC_IOS)
solenbergd97ec302015-10-07 01:40:33 -070062const int kDefaultAudioDeviceId = 0;
henrike@webrtc.org28e20752013-07-10 00:45:36 +000063#endif
64
solenberg971cab02016-06-14 10:02:41 -070065constexpr int kNackRtpHistoryMs = 5000;
minyue@webrtc.org2dc6f312014-10-31 05:33:10 +000066
67// Codec parameters for Opus.
henrike@webrtc.org1e09a712013-07-26 19:17:59 +000068// draft-spittka-payload-rtp-opus-03
minyue@webrtc.org2dc6f312014-10-31 05:33:10 +000069
70// Recommended bitrates:
71// 8-12 kb/s for NB speech,
72// 16-20 kb/s for WB speech,
73// 28-40 kb/s for FB speech,
74// 48-64 kb/s for FB mono music, and
75// 64-128 kb/s for FB stereo music.
76// The current implementation applies the following values to mono signals,
77// and multiplies them by 2 for stereo.
solenbergd97ec302015-10-07 01:40:33 -070078const int kOpusBitrateNb = 12000;
79const int kOpusBitrateWb = 20000;
80const int kOpusBitrateFb = 32000;
minyue@webrtc.org2dc6f312014-10-31 05:33:10 +000081
henrike@webrtc.org1e09a712013-07-26 19:17:59 +000082// Opus bitrate should be in the range between 6000 and 510000.
solenbergd97ec302015-10-07 01:40:33 -070083const int kOpusMinBitrate = 6000;
84const int kOpusMaxBitrate = 510000;
buildbot@webrtc.org5d639b32014-09-10 07:57:12 +000085
deadbeef80346142016-04-27 14:17:10 -070086// iSAC bitrate should be <= 56000.
87const int kIsacMaxBitrate = 56000;
88
wu@webrtc.orgde305012013-10-31 15:40:38 +000089// Default audio dscp value.
90// See http://tools.ietf.org/html/rfc2474 for details.
91// See also http://tools.ietf.org/html/draft-jennings-rtcweb-qos-00
solenbergd97ec302015-10-07 01:40:33 -070092const rtc::DiffServCodePoint kAudioDscpValue = rtc::DSCP_EF;
henrike@webrtc.org1e09a712013-07-26 19:17:59 +000093
Fredrik Solenbergb5727682015-12-04 15:22:19 +010094// Constants from voice_engine_defines.h.
95const int kMinTelephoneEventCode = 0; // RFC4733 (Section 2.3.1)
96const int kMaxTelephoneEventCode = 255;
97const int kMinTelephoneEventDuration = 100;
98const int kMaxTelephoneEventDuration = 60000; // Actual limit is 2^16
99
solenberg31642aa2016-03-14 08:00:37 -0700100const int kMinPayloadType = 0;
101const int kMaxPayloadType = 127;
102
deadbeef884f5852016-01-15 09:20:04 -0800103class ProxySink : public webrtc::AudioSinkInterface {
104 public:
105 ProxySink(AudioSinkInterface* sink) : sink_(sink) { RTC_DCHECK(sink); }
106
107 void OnData(const Data& audio) override { sink_->OnData(audio); }
108
109 private:
110 webrtc::AudioSinkInterface* sink_;
111};
112
solenberg0b675462015-10-09 01:37:09 -0700113bool ValidateStreamParams(const StreamParams& sp) {
114 if (sp.ssrcs.empty()) {
115 LOG(LS_ERROR) << "No SSRCs in stream parameters: " << sp.ToString();
116 return false;
117 }
118 if (sp.ssrcs.size() > 1) {
119 LOG(LS_ERROR) << "Multiple SSRCs in stream parameters: " << sp.ToString();
120 return false;
121 }
122 return true;
123}
124
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000125// Dumps an AudioCodec in RFC 2327-ish format.
solenbergd97ec302015-10-07 01:40:33 -0700126std::string ToString(const AudioCodec& codec) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000127 std::stringstream ss;
128 ss << codec.name << "/" << codec.clockrate << "/" << codec.channels
129 << " (" << codec.id << ")";
130 return ss.str();
131}
Minyue Li7100dcd2015-03-27 05:05:59 +0100132
solenbergd97ec302015-10-07 01:40:33 -0700133std::string ToString(const webrtc::CodecInst& codec) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000134 std::stringstream ss;
135 ss << codec.plname << "/" << codec.plfreq << "/" << codec.channels
136 << " (" << codec.pltype << ")";
137 return ss.str();
138}
139
solenbergd97ec302015-10-07 01:40:33 -0700140bool IsCodec(const AudioCodec& codec, const char* ref_name) {
Minyue Li7100dcd2015-03-27 05:05:59 +0100141 return (_stricmp(codec.name.c_str(), ref_name) == 0);
142}
143
solenbergd97ec302015-10-07 01:40:33 -0700144bool IsCodec(const webrtc::CodecInst& codec, const char* ref_name) {
Minyue Li7100dcd2015-03-27 05:05:59 +0100145 return (_stricmp(codec.plname, ref_name) == 0);
146}
147
solenbergd97ec302015-10-07 01:40:33 -0700148bool FindCodec(const std::vector<AudioCodec>& codecs,
solenberg26c8c912015-11-27 04:00:25 -0800149 const AudioCodec& codec,
150 AudioCodec* found_codec) {
Fredrik Solenbergaf9fb212015-08-26 10:45:53 +0200151 for (const AudioCodec& c : codecs) {
152 if (c.Matches(codec)) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000153 if (found_codec != NULL) {
Fredrik Solenbergaf9fb212015-08-26 10:45:53 +0200154 *found_codec = c;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000155 }
156 return true;
157 }
158 }
159 return false;
160}
wu@webrtc.org1d1ffc92013-10-16 18:12:02 +0000161
solenberg0b675462015-10-09 01:37:09 -0700162bool VerifyUniquePayloadTypes(const std::vector<AudioCodec>& codecs) {
163 if (codecs.empty()) {
164 return true;
165 }
166 std::vector<int> payload_types;
167 for (const AudioCodec& codec : codecs) {
168 payload_types.push_back(codec.id);
169 }
170 std::sort(payload_types.begin(), payload_types.end());
171 auto it = std::unique(payload_types.begin(), payload_types.end());
172 return it == payload_types.end();
173}
174
Minyue Li7100dcd2015-03-27 05:05:59 +0100175// Return true if codec.params[feature] == "1", false otherwise.
solenberg26c8c912015-11-27 04:00:25 -0800176bool IsCodecFeatureEnabled(const AudioCodec& codec, const char* feature) {
Minyue Li7100dcd2015-03-27 05:05:59 +0100177 int value;
178 return codec.GetParam(feature, &value) && value == 1;
179}
180
181// Use params[kCodecParamMaxAverageBitrate] if it is defined, use codec.bitrate
182// otherwise. If the value (either from params or codec.bitrate) <=0, use the
183// default configuration. If the value is beyond feasible bit rate of Opus,
184// clamp it. Returns the Opus bit rate for operation.
solenbergd97ec302015-10-07 01:40:33 -0700185int GetOpusBitrate(const AudioCodec& codec, int max_playback_rate) {
Minyue Li7100dcd2015-03-27 05:05:59 +0100186 int bitrate = 0;
187 bool use_param = true;
188 if (!codec.GetParam(kCodecParamMaxAverageBitrate, &bitrate)) {
189 bitrate = codec.bitrate;
190 use_param = false;
191 }
192 if (bitrate <= 0) {
193 if (max_playback_rate <= 8000) {
194 bitrate = kOpusBitrateNb;
195 } else if (max_playback_rate <= 16000) {
196 bitrate = kOpusBitrateWb;
197 } else {
198 bitrate = kOpusBitrateFb;
199 }
200
201 if (IsCodecFeatureEnabled(codec, kCodecParamStereo)) {
202 bitrate *= 2;
203 }
204 } else if (bitrate < kOpusMinBitrate || bitrate > kOpusMaxBitrate) {
205 bitrate = (bitrate < kOpusMinBitrate) ? kOpusMinBitrate : kOpusMaxBitrate;
206 std::string rate_source =
207 use_param ? "Codec parameter \"maxaveragebitrate\"" :
208 "Supplied Opus bitrate";
209 LOG(LS_WARNING) << rate_source
210 << " is invalid and is replaced by: "
211 << bitrate;
212 }
213 return bitrate;
214}
215
216// Returns kOpusDefaultPlaybackRate if params[kCodecParamMaxPlaybackRate] is not
217// defined. Returns the value of params[kCodecParamMaxPlaybackRate] otherwise.
solenbergd97ec302015-10-07 01:40:33 -0700218int GetOpusMaxPlaybackRate(const AudioCodec& codec) {
Minyue Li7100dcd2015-03-27 05:05:59 +0100219 int value;
220 if (codec.GetParam(kCodecParamMaxPlaybackRate, &value)) {
221 return value;
222 }
223 return kOpusDefaultMaxPlaybackRate;
224}
225
solenbergd97ec302015-10-07 01:40:33 -0700226void GetOpusConfig(const AudioCodec& codec, webrtc::CodecInst* voe_codec,
Minyue Li7100dcd2015-03-27 05:05:59 +0100227 bool* enable_codec_fec, int* max_playback_rate,
228 bool* enable_codec_dtx) {
229 *enable_codec_fec = IsCodecFeatureEnabled(codec, kCodecParamUseInbandFec);
230 *enable_codec_dtx = IsCodecFeatureEnabled(codec, kCodecParamUseDtx);
231 *max_playback_rate = GetOpusMaxPlaybackRate(codec);
232
233 // If OPUS, change what we send according to the "stereo" codec
234 // parameter, and not the "channels" parameter. We set
235 // voe_codec.channels to 2 if "stereo=1" and 1 otherwise. If
236 // the bitrate is not specified, i.e. is <= zero, we set it to the
237 // appropriate default value for mono or stereo Opus.
238
239 voe_codec->channels = IsCodecFeatureEnabled(codec, kCodecParamStereo) ? 2 : 1;
240 voe_codec->rate = GetOpusBitrate(codec, *max_playback_rate);
241}
242
solenberg566ef242015-11-06 15:34:49 -0800243webrtc::AudioState::Config MakeAudioStateConfig(VoEWrapper* voe_wrapper) {
244 webrtc::AudioState::Config config;
245 config.voice_engine = voe_wrapper->engine();
246 return config;
247}
248
solenberg26c8c912015-11-27 04:00:25 -0800249class WebRtcVoiceCodecs final {
250 public:
251 // TODO(solenberg): Do this filtering once off-line, add a simple AudioCodec
252 // list and add a test which verifies VoE supports the listed codecs.
ossuc54071d2016-08-17 02:45:41 -0700253 static std::vector<AudioCodec> SupportedSendCodecs() {
solenberg26c8c912015-11-27 04:00:25 -0800254 std::vector<AudioCodec> result;
deadbeef67cf2c12016-04-13 10:07:16 -0700255 // Iterate first over our preferred codecs list, so that the results are
256 // added in order of preference.
257 for (size_t i = 0; i < arraysize(kCodecPrefs); ++i) {
258 const CodecPref* pref = &kCodecPrefs[i];
259 for (webrtc::CodecInst voe_codec : webrtc::acm2::RentACodec::Database()) {
260 // Change the sample rate of G722 to 8000 to match SDP.
261 MaybeFixupG722(&voe_codec, 8000);
262 // Skip uncompressed formats.
263 if (IsCodec(voe_codec, kL16CodecName)) {
264 continue;
buildbot@webrtc.org13d67762014-05-02 17:33:29 +0000265 }
buildbot@webrtc.org13d67762014-05-02 17:33:29 +0000266
deadbeef67cf2c12016-04-13 10:07:16 -0700267 if (!IsCodec(voe_codec, pref->name) ||
268 pref->clockrate != voe_codec.plfreq ||
269 pref->channels != voe_codec.channels) {
270 // Not a match.
271 continue;
272 }
273
274 AudioCodec codec(pref->payload_type, voe_codec.plname, voe_codec.plfreq,
275 voe_codec.rate, voe_codec.channels);
276 LOG(LS_INFO) << "Adding supported codec: " << ToString(codec);
Minyue Li7100dcd2015-03-27 05:05:59 +0100277 if (IsCodec(codec, kIsacCodecName)) {
minyue@webrtc.org26236952014-10-29 02:27:08 +0000278 // Indicate auto-bitrate in signaling.
buildbot@webrtc.org13d67762014-05-02 17:33:29 +0000279 codec.bitrate = 0;
280 }
Minyue Li7100dcd2015-03-27 05:05:59 +0100281 if (IsCodec(codec, kOpusCodecName)) {
buildbot@webrtc.org13d67762014-05-02 17:33:29 +0000282 // Only add fmtp parameters that differ from the spec.
283 if (kPreferredMinPTime != kOpusDefaultMinPTime) {
284 codec.params[kCodecParamMinPTime] =
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000285 rtc::ToString(kPreferredMinPTime);
buildbot@webrtc.org13d67762014-05-02 17:33:29 +0000286 }
287 if (kPreferredMaxPTime != kOpusDefaultMaxPTime) {
288 codec.params[kCodecParamMaxPTime] =
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000289 rtc::ToString(kPreferredMaxPTime);
buildbot@webrtc.org13d67762014-05-02 17:33:29 +0000290 }
pkasting@chromium.orgd3245462015-02-23 21:28:22 +0000291 codec.SetParam(kCodecParamUseInbandFec, 1);
stefanba4c0e42016-02-04 04:12:24 -0800292 codec.AddFeedbackParam(
293 FeedbackParam(kRtcpFbParamTransportCc, kParamValueEmpty));
minyue@webrtc.org4ef22d12014-11-17 09:26:39 +0000294
295 // TODO(hellner): Add ptime, sprop-stereo, and stereo
buildbot@webrtc.org13d67762014-05-02 17:33:29 +0000296 // when they can be set to values other than the default.
297 }
solenberg26c8c912015-11-27 04:00:25 -0800298 result.push_back(codec);
buildbot@webrtc.org13d67762014-05-02 17:33:29 +0000299 }
300 }
solenberg26c8c912015-11-27 04:00:25 -0800301 return result;
buildbot@webrtc.org13d67762014-05-02 17:33:29 +0000302 }
buildbot@webrtc.org13d67762014-05-02 17:33:29 +0000303
solenberg26c8c912015-11-27 04:00:25 -0800304 static bool ToCodecInst(const AudioCodec& in,
305 webrtc::CodecInst* out) {
306 for (webrtc::CodecInst voe_codec : webrtc::acm2::RentACodec::Database()) {
307 // Change the sample rate of G722 to 8000 to match SDP.
308 MaybeFixupG722(&voe_codec, 8000);
309 AudioCodec codec(voe_codec.pltype, voe_codec.plname, voe_codec.plfreq,
deadbeef67cf2c12016-04-13 10:07:16 -0700310 voe_codec.rate, voe_codec.channels);
solenberg26c8c912015-11-27 04:00:25 -0800311 bool multi_rate = IsCodecMultiRate(voe_codec);
312 // Allow arbitrary rates for ISAC to be specified.
313 if (multi_rate) {
314 // Set codec.bitrate to 0 so the check for codec.Matches() passes.
315 codec.bitrate = 0;
316 }
317 if (codec.Matches(in)) {
318 if (out) {
319 // Fixup the payload type.
320 voe_codec.pltype = in.id;
321
322 // Set bitrate if specified.
323 if (multi_rate && in.bitrate != 0) {
324 voe_codec.rate = in.bitrate;
325 }
326
327 // Reset G722 sample rate to 16000 to match WebRTC.
328 MaybeFixupG722(&voe_codec, 16000);
329
330 // Apply codec-specific settings.
331 if (IsCodec(codec, kIsacCodecName)) {
332 // If ISAC and an explicit bitrate is not specified,
333 // enable auto bitrate adjustment.
334 voe_codec.rate = (in.bitrate > 0) ? in.bitrate : -1;
335 }
336 *out = voe_codec;
337 }
338 return true;
339 }
340 }
henrik.lundin@webrtc.org8038d422014-11-11 08:38:24 +0000341 return false;
henrik.lundin@webrtc.orgf85dbce2014-11-07 12:25:00 +0000342 }
solenberg26c8c912015-11-27 04:00:25 -0800343
344 static bool IsCodecMultiRate(const webrtc::CodecInst& codec) {
345 for (size_t i = 0; i < arraysize(kCodecPrefs); ++i) {
346 if (IsCodec(codec, kCodecPrefs[i].name) &&
347 kCodecPrefs[i].clockrate == codec.plfreq) {
348 return kCodecPrefs[i].is_multi_rate;
349 }
350 }
351 return false;
352 }
353
deadbeef80346142016-04-27 14:17:10 -0700354 static int MaxBitrateBps(const webrtc::CodecInst& codec) {
355 for (size_t i = 0; i < arraysize(kCodecPrefs); ++i) {
356 if (IsCodec(codec, kCodecPrefs[i].name) &&
357 kCodecPrefs[i].clockrate == codec.plfreq) {
358 return kCodecPrefs[i].max_bitrate_bps;
359 }
360 }
361 return 0;
362 }
363
solenberg26c8c912015-11-27 04:00:25 -0800364 // If the AudioCodec param kCodecParamPTime is set, then we will set it to
365 // codec pacsize if it's valid, or we will pick the next smallest value we
366 // support.
367 // TODO(Brave): Query supported packet sizes from ACM when the API is ready.
368 static bool SetPTimeAsPacketSize(webrtc::CodecInst* codec, int ptime_ms) {
369 for (const CodecPref& codec_pref : kCodecPrefs) {
370 if ((IsCodec(*codec, codec_pref.name) &&
371 codec_pref.clockrate == codec->plfreq) ||
372 IsCodec(*codec, kG722CodecName)) {
373 int packet_size_ms = SelectPacketSize(codec_pref, ptime_ms);
374 if (packet_size_ms) {
375 // Convert unit from milli-seconds to samples.
376 codec->pacsize = (codec->plfreq / 1000) * packet_size_ms;
377 return true;
378 }
379 }
380 }
381 return false;
382 }
383
stefanba4c0e42016-02-04 04:12:24 -0800384 static const AudioCodec* GetPreferredCodec(
385 const std::vector<AudioCodec>& codecs,
kwiberg68061362016-06-14 08:04:47 -0700386 webrtc::CodecInst* out) {
solenberg72e29d22016-03-08 06:35:16 -0800387 RTC_DCHECK(out);
stefanba4c0e42016-02-04 04:12:24 -0800388 // Select the preferred send codec (the first non-telephone-event/CN codec).
389 for (const AudioCodec& codec : codecs) {
stefanba4c0e42016-02-04 04:12:24 -0800390 if (IsCodec(codec, kDtmfCodecName) || IsCodec(codec, kCnCodecName)) {
391 // Skip telephone-event/CN codec, which will be handled later.
392 continue;
393 }
394
395 // We'll use the first codec in the list to actually send audio data.
396 // Be sure to use the payload type requested by the remote side.
stefanba4c0e42016-02-04 04:12:24 -0800397 // Ignore codecs we don't know about. The negotiation step should prevent
398 // this, but double-check to be sure.
kwibergedaa8492016-06-15 04:34:47 -0700399 if (!ToCodecInst(codec, out)) {
kwiberg68061362016-06-14 08:04:47 -0700400 LOG(LS_WARNING) << "Unknown codec " << ToString(codec);
stefanba4c0e42016-02-04 04:12:24 -0800401 continue;
402 }
kwiberg68061362016-06-14 08:04:47 -0700403 return &codec;
stefanba4c0e42016-02-04 04:12:24 -0800404 }
405 return nullptr;
406 }
407
solenberg26c8c912015-11-27 04:00:25 -0800408 private:
409 static const int kMaxNumPacketSize = 6;
410 struct CodecPref {
411 const char* name;
412 int clockrate;
Peter Kasting69558702016-01-12 16:26:35 -0800413 size_t channels;
solenberg26c8c912015-11-27 04:00:25 -0800414 int payload_type;
415 bool is_multi_rate;
416 int packet_sizes_ms[kMaxNumPacketSize];
deadbeef80346142016-04-27 14:17:10 -0700417 int max_bitrate_bps;
solenberg26c8c912015-11-27 04:00:25 -0800418 };
419 // Note: keep the supported packet sizes in ascending order.
kwiberg68061362016-06-14 08:04:47 -0700420 static const CodecPref kCodecPrefs[11];
solenberg26c8c912015-11-27 04:00:25 -0800421
422 static int SelectPacketSize(const CodecPref& codec_pref, int ptime_ms) {
423 int selected_packet_size_ms = codec_pref.packet_sizes_ms[0];
424 for (int packet_size_ms : codec_pref.packet_sizes_ms) {
425 if (packet_size_ms && packet_size_ms <= ptime_ms) {
426 selected_packet_size_ms = packet_size_ms;
427 }
428 }
429 return selected_packet_size_ms;
430 }
431
432 // Changes RTP timestamp rate of G722. This is due to the "bug" in the RFC
433 // which says that G722 should be advertised as 8 kHz although it is a 16 kHz
434 // codec.
435 static void MaybeFixupG722(webrtc::CodecInst* voe_codec, int new_plfreq) {
436 if (IsCodec(*voe_codec, kG722CodecName)) {
437 // If the ASSERT triggers, the codec definition in WebRTC VoiceEngine
438 // has changed, and this special case is no longer needed.
439 RTC_DCHECK(voe_codec->plfreq != new_plfreq);
440 voe_codec->plfreq = new_plfreq;
441 }
442 }
443};
444
kwiberg68061362016-06-14 08:04:47 -0700445const WebRtcVoiceCodecs::CodecPref WebRtcVoiceCodecs::kCodecPrefs[11] = {
deadbeef80346142016-04-27 14:17:10 -0700446 {kOpusCodecName, 48000, 2, 111, true, {10, 20, 40, 60}, kOpusMaxBitrate},
447 {kIsacCodecName, 16000, 1, 103, true, {30, 60}, kIsacMaxBitrate},
448 {kIsacCodecName, 32000, 1, 104, true, {30}, kIsacMaxBitrate},
449 // G722 should be advertised as 8000 Hz because of the RFC "bug".
450 {kG722CodecName, 8000, 1, 9, false, {10, 20, 30, 40, 50, 60}},
451 {kIlbcCodecName, 8000, 1, 102, false, {20, 30, 40, 60}},
452 {kPcmuCodecName, 8000, 1, 0, false, {10, 20, 30, 40, 50, 60}},
453 {kPcmaCodecName, 8000, 1, 8, false, {10, 20, 30, 40, 50, 60}},
454 {kCnCodecName, 32000, 1, 106, false, {}},
455 {kCnCodecName, 16000, 1, 105, false, {}},
456 {kCnCodecName, 8000, 1, 13, false, {}},
kwiberg68061362016-06-14 08:04:47 -0700457 {kDtmfCodecName, 8000, 1, 126, false, {}}
solenberg26c8c912015-11-27 04:00:25 -0800458};
459} // namespace {
460
solenberg971cab02016-06-14 10:02:41 -0700461bool SendCodecSpec::operator==(const SendCodecSpec& rhs) const {
462 if (nack_enabled != rhs.nack_enabled) {
463 return false;
464 }
465 if (transport_cc_enabled != rhs.transport_cc_enabled) {
466 return false;
467 }
468 if (enable_codec_fec != rhs.enable_codec_fec) {
469 return false;
470 }
471 if (enable_opus_dtx != rhs.enable_opus_dtx) {
472 return false;
473 }
474 if (opus_max_playback_rate != rhs.opus_max_playback_rate) {
475 return false;
476 }
477 if (red_payload_type != rhs.red_payload_type) {
478 return false;
479 }
480 if (cng_payload_type != rhs.cng_payload_type) {
481 return false;
482 }
483 if (cng_plfreq != rhs.cng_plfreq) {
484 return false;
485 }
486 if (codec_inst != rhs.codec_inst) {
487 return false;
488 }
489 return true;
490}
491
492bool SendCodecSpec::operator!=(const SendCodecSpec& rhs) const {
493 return !(*this == rhs);
494}
495
solenberg26c8c912015-11-27 04:00:25 -0800496bool WebRtcVoiceEngine::ToCodecInst(const AudioCodec& in,
497 webrtc::CodecInst* out) {
498 return WebRtcVoiceCodecs::ToCodecInst(in, out);
499}
500
ossu29b1a8d2016-06-13 07:34:51 -0700501WebRtcVoiceEngine::WebRtcVoiceEngine(
502 webrtc::AudioDeviceModule* adm,
503 const rtc::scoped_refptr<webrtc::AudioDecoderFactory>& decoder_factory)
504 : WebRtcVoiceEngine(adm, decoder_factory, new VoEWrapper()) {
solenbergff976312016-03-30 23:28:51 -0700505 audio_state_ = webrtc::AudioState::Create(MakeAudioStateConfig(voe()));
solenberg26c8c912015-11-27 04:00:25 -0800506}
507
ossu29b1a8d2016-06-13 07:34:51 -0700508WebRtcVoiceEngine::WebRtcVoiceEngine(
509 webrtc::AudioDeviceModule* adm,
510 const rtc::scoped_refptr<webrtc::AudioDecoderFactory>& decoder_factory,
511 VoEWrapper* voe_wrapper)
512 : adm_(adm), decoder_factory_(decoder_factory), voe_wrapper_(voe_wrapper) {
solenberg26c8c912015-11-27 04:00:25 -0800513 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
solenbergff976312016-03-30 23:28:51 -0700514 LOG(LS_INFO) << "WebRtcVoiceEngine::WebRtcVoiceEngine";
515 RTC_DCHECK(voe_wrapper);
ossuc54071d2016-08-17 02:45:41 -0700516 RTC_DCHECK(decoder_factory);
solenberg26c8c912015-11-27 04:00:25 -0800517
518 signal_thread_checker_.DetachFromThread();
solenberg26c8c912015-11-27 04:00:25 -0800519
520 // Load our audio codec list.
ossuc54071d2016-08-17 02:45:41 -0700521 LOG(LS_INFO) << "Supported send codecs in order of preference:";
522 send_codecs_ = WebRtcVoiceCodecs::SupportedSendCodecs();
523 for (const AudioCodec& codec : send_codecs_) {
524 LOG(LS_INFO) << ToString(codec);
525 }
526
527 LOG(LS_INFO) << "Supported recv codecs in order of preference:";
528 recv_codecs_ = CollectRecvCodecs();
529 for (const AudioCodec& codec : recv_codecs_) {
solenbergff976312016-03-30 23:28:51 -0700530 LOG(LS_INFO) << ToString(codec);
buildbot@webrtc.org13d67762014-05-02 17:33:29 +0000531 }
buildbot@webrtc.org13d67762014-05-02 17:33:29 +0000532
solenbergff976312016-03-30 23:28:51 -0700533 voe_config_.Set<webrtc::VoicePacing>(new webrtc::VoicePacing(true));
buildbot@webrtc.org13d67762014-05-02 17:33:29 +0000534
solenbergff976312016-03-30 23:28:51 -0700535 // Temporarily turn logging level up for the Init() call.
536 webrtc::Trace::SetTraceCallback(this);
solenbergbd138382015-11-20 16:08:07 -0800537 webrtc::Trace::set_level_filter(kElevatedTraceFilter);
solenberg2515af22015-12-02 06:19:36 -0800538 LOG(LS_INFO) << webrtc::VoiceEngine::GetVersionString();
ossu29b1a8d2016-06-13 07:34:51 -0700539 RTC_CHECK_EQ(0, voe_wrapper_->base()->Init(adm_.get(), nullptr,
540 decoder_factory_));
solenbergbd138382015-11-20 16:08:07 -0800541 webrtc::Trace::set_level_filter(kDefaultTraceFilter);
buildbot@webrtc.org13d67762014-05-02 17:33:29 +0000542
solenbergff976312016-03-30 23:28:51 -0700543 // No ADM supplied? Get the default one from VoE.
544 if (!adm_) {
545 adm_ = voe_wrapper_->base()->audio_device_module();
546 }
547 RTC_DCHECK(adm_);
548
buildbot@webrtc.org13d67762014-05-02 17:33:29 +0000549 // Save the default AGC configuration settings. This must happen before
solenberg246b8172015-12-08 09:50:23 -0800550 // calling ApplyOptions or the default will be overwritten.
solenbergff976312016-03-30 23:28:51 -0700551 int error = voe_wrapper_->processing()->GetAgcConfig(default_agc_config_);
552 RTC_DCHECK_EQ(0, error);
buildbot@webrtc.org13d67762014-05-02 17:33:29 +0000553
solenberg0f7d2932016-01-15 01:40:39 -0800554 // Set default engine options.
555 {
556 AudioOptions options;
557 options.echo_cancellation = rtc::Optional<bool>(true);
558 options.auto_gain_control = rtc::Optional<bool>(true);
559 options.noise_suppression = rtc::Optional<bool>(true);
560 options.highpass_filter = rtc::Optional<bool>(true);
561 options.stereo_swapping = rtc::Optional<bool>(false);
562 options.audio_jitter_buffer_max_packets = rtc::Optional<int>(50);
563 options.audio_jitter_buffer_fast_accelerate = rtc::Optional<bool>(false);
564 options.typing_detection = rtc::Optional<bool>(true);
565 options.adjust_agc_delta = rtc::Optional<int>(0);
566 options.experimental_agc = rtc::Optional<bool>(false);
567 options.extended_filter_aec = rtc::Optional<bool>(false);
568 options.delay_agnostic_aec = rtc::Optional<bool>(false);
569 options.experimental_ns = rtc::Optional<bool>(false);
Alejandro Luebsc9b0c262016-05-16 15:32:38 -0700570 options.intelligibility_enhancer = rtc::Optional<bool>(false);
peaha3333bf2016-06-30 00:02:34 -0700571 options.level_control = rtc::Optional<bool>(false);
solenbergff976312016-03-30 23:28:51 -0700572 bool error = ApplyOptions(options);
573 RTC_DCHECK(error);
buildbot@webrtc.org13d67762014-05-02 17:33:29 +0000574 }
575
solenberg246b8172015-12-08 09:50:23 -0800576 SetDefaultDevices();
buildbot@webrtc.org13d67762014-05-02 17:33:29 +0000577}
578
solenbergff976312016-03-30 23:28:51 -0700579WebRtcVoiceEngine::~WebRtcVoiceEngine() {
solenberg566ef242015-11-06 15:34:49 -0800580 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
solenbergff976312016-03-30 23:28:51 -0700581 LOG(LS_INFO) << "WebRtcVoiceEngine::~WebRtcVoiceEngine";
buildbot@webrtc.org13d67762014-05-02 17:33:29 +0000582 StopAecDump();
buildbot@webrtc.org13d67762014-05-02 17:33:29 +0000583 voe_wrapper_->base()->Terminate();
solenbergff976312016-03-30 23:28:51 -0700584 webrtc::Trace::SetTraceCallback(nullptr);
buildbot@webrtc.org13d67762014-05-02 17:33:29 +0000585}
586
solenberg566ef242015-11-06 15:34:49 -0800587rtc::scoped_refptr<webrtc::AudioState>
588 WebRtcVoiceEngine::GetAudioState() const {
589 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
590 return audio_state_;
591}
592
nisse51542be2016-02-12 02:27:06 -0800593VoiceMediaChannel* WebRtcVoiceEngine::CreateChannel(
594 webrtc::Call* call,
595 const MediaConfig& config,
Jelena Marusicc28a8962015-05-29 15:05:44 +0200596 const AudioOptions& options) {
solenberg566ef242015-11-06 15:34:49 -0800597 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
nisse51542be2016-02-12 02:27:06 -0800598 return new WebRtcVoiceMediaChannel(this, config, options, call);
buildbot@webrtc.org13d67762014-05-02 17:33:29 +0000599}
600
buildbot@webrtc.org13d67762014-05-02 17:33:29 +0000601bool WebRtcVoiceEngine::ApplyOptions(const AudioOptions& options_in) {
solenberg566ef242015-11-06 15:34:49 -0800602 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
solenbergff976312016-03-30 23:28:51 -0700603 LOG(LS_INFO) << "WebRtcVoiceEngine::ApplyOptions: " << options_in.ToString();
solenberg0f7d2932016-01-15 01:40:39 -0800604 AudioOptions options = options_in; // The options are modified below.
solenberg246b8172015-12-08 09:50:23 -0800605
buildbot@webrtc.org13d67762014-05-02 17:33:29 +0000606 // kEcConference is AEC with high suppression.
607 webrtc::EcModes ec_mode = webrtc::kEcConference;
608 webrtc::AecmModes aecm_mode = webrtc::kAecmSpeakerphone;
609 webrtc::AgcModes agc_mode = webrtc::kAgcAdaptiveAnalog;
610 webrtc::NsModes ns_mode = webrtc::kNsHighSuppression;
kwiberg102c6a62015-10-30 02:47:38 -0700611 if (options.aecm_generate_comfort_noise) {
buildbot@webrtc.org13d67762014-05-02 17:33:29 +0000612 LOG(LS_VERBOSE) << "Comfort noise explicitly set to "
kwiberg102c6a62015-10-30 02:47:38 -0700613 << *options.aecm_generate_comfort_noise
614 << " (default is false).";
buildbot@webrtc.org13d67762014-05-02 17:33:29 +0000615 }
616
kjellanderfcfc8042016-01-14 11:01:09 -0800617#if defined(WEBRTC_IOS)
peah4905f062016-08-22 01:58:50 -0700618 // On iOS, VPIO provides built-in EC, NS and AGC.
Karl Wibergbe579832015-11-10 22:34:18 +0100619 options.echo_cancellation = rtc::Optional<bool>(false);
620 options.auto_gain_control = rtc::Optional<bool>(false);
peah4905f062016-08-22 01:58:50 -0700621 options.noise_suppression = rtc::Optional<bool>(false);
622 LOG(LS_INFO)
623 << "Always disable AEC, NS and AGC on iOS. Use built-in instead.";
buildbot@webrtc.org13d67762014-05-02 17:33:29 +0000624#elif defined(ANDROID)
625 ec_mode = webrtc::kEcAecm;
626#endif
627
kjellanderfcfc8042016-01-14 11:01:09 -0800628#if defined(WEBRTC_IOS) || defined(ANDROID)
buildbot@webrtc.org13d67762014-05-02 17:33:29 +0000629 // Set the AGC mode for iOS as well despite disabling it above, to avoid
630 // unsupported configuration errors from webrtc.
631 agc_mode = webrtc::kAgcFixedDigital;
Karl Wibergbe579832015-11-10 22:34:18 +0100632 options.typing_detection = rtc::Optional<bool>(false);
633 options.experimental_agc = rtc::Optional<bool>(false);
634 options.extended_filter_aec = rtc::Optional<bool>(false);
635 options.experimental_ns = rtc::Optional<bool>(false);
buildbot@webrtc.org13d67762014-05-02 17:33:29 +0000636#endif
637
Bjorn Volckerbf395c12015-03-25 22:45:56 +0100638 // Delay Agnostic AEC automatically turns on EC if not set except on iOS
639 // where the feature is not supported.
640 bool use_delay_agnostic_aec = false;
kjellanderfcfc8042016-01-14 11:01:09 -0800641#if !defined(WEBRTC_IOS)
kwiberg102c6a62015-10-30 02:47:38 -0700642 if (options.delay_agnostic_aec) {
643 use_delay_agnostic_aec = *options.delay_agnostic_aec;
Bjorn Volckerbf395c12015-03-25 22:45:56 +0100644 if (use_delay_agnostic_aec) {
Karl Wibergbe579832015-11-10 22:34:18 +0100645 options.echo_cancellation = rtc::Optional<bool>(true);
646 options.extended_filter_aec = rtc::Optional<bool>(true);
Bjorn Volckerbf395c12015-03-25 22:45:56 +0100647 ec_mode = webrtc::kEcConference;
648 }
649 }
650#endif
651
buildbot@webrtc.org13d67762014-05-02 17:33:29 +0000652 webrtc::VoEAudioProcessing* voep = voe_wrapper_->processing();
653
kwiberg102c6a62015-10-30 02:47:38 -0700654 if (options.echo_cancellation) {
henrika@webrtc.orga954c072014-12-09 16:22:09 +0000655 // Check if platform supports built-in EC. Currently only supported on
656 // Android and in combination with Java based audio layer.
657 // TODO(henrika): investigate possibility to support built-in EC also
658 // in combination with Open SL ES audio.
solenberg5b5129a2016-04-08 05:35:48 -0700659 const bool built_in_aec = adm()->BuiltInAECIsAvailable();
Bjorn Volcker73f72102015-06-03 14:50:15 +0200660 if (built_in_aec) {
Bjorn Volckerccfc9392015-05-07 07:43:17 +0200661 // Built-in EC exists on this device and use_delay_agnostic_aec is not
662 // overriding it. Enable/Disable it according to the echo_cancellation
663 // audio option.
Bjorn Volcker73f72102015-06-03 14:50:15 +0200664 const bool enable_built_in_aec =
kwiberg102c6a62015-10-30 02:47:38 -0700665 *options.echo_cancellation && !use_delay_agnostic_aec;
solenberg5b5129a2016-04-08 05:35:48 -0700666 if (adm()->EnableBuiltInAEC(enable_built_in_aec) == 0 &&
Bjorn Volcker73f72102015-06-03 14:50:15 +0200667 enable_built_in_aec) {
Bjorn Volckerbf395c12015-03-25 22:45:56 +0100668 // Disable internal software EC if built-in EC is enabled,
henrika@webrtc.orga954c072014-12-09 16:22:09 +0000669 // i.e., replace the software EC with the built-in EC.
Karl Wibergbe579832015-11-10 22:34:18 +0100670 options.echo_cancellation = rtc::Optional<bool>(false);
henrika@webrtc.orga954c072014-12-09 16:22:09 +0000671 LOG(LS_INFO) << "Disabling EC since built-in EC will be used instead";
672 }
673 }
kwiberg102c6a62015-10-30 02:47:38 -0700674 if (voep->SetEcStatus(*options.echo_cancellation, ec_mode) == -1) {
675 LOG_RTCERR2(SetEcStatus, *options.echo_cancellation, ec_mode);
buildbot@webrtc.org13d67762014-05-02 17:33:29 +0000676 return false;
677 } else {
kwiberg102c6a62015-10-30 02:47:38 -0700678 LOG(LS_INFO) << "Echo control set to " << *options.echo_cancellation
henrika86d907c2015-09-07 16:09:50 +0200679 << " with mode " << ec_mode;
buildbot@webrtc.org13d67762014-05-02 17:33:29 +0000680 }
681#if !defined(ANDROID)
682 // TODO(ajm): Remove the error return on Android from webrtc.
kwiberg102c6a62015-10-30 02:47:38 -0700683 if (voep->SetEcMetricsStatus(*options.echo_cancellation) == -1) {
684 LOG_RTCERR1(SetEcMetricsStatus, *options.echo_cancellation);
buildbot@webrtc.org13d67762014-05-02 17:33:29 +0000685 return false;
686 }
687#endif
688 if (ec_mode == webrtc::kEcAecm) {
kwiberg102c6a62015-10-30 02:47:38 -0700689 bool cn = options.aecm_generate_comfort_noise.value_or(false);
690 if (voep->SetAecmMode(aecm_mode, cn) != 0) {
691 LOG_RTCERR2(SetAecmMode, aecm_mode, cn);
buildbot@webrtc.org13d67762014-05-02 17:33:29 +0000692 return false;
693 }
694 }
695 }
696
kwiberg102c6a62015-10-30 02:47:38 -0700697 if (options.auto_gain_control) {
peah72a56452016-08-22 12:08:55 -0700698 bool built_in_agc_avaliable = adm()->BuiltInAGCIsAvailable();
699 if (built_in_agc_avaliable) {
solenberg5b5129a2016-04-08 05:35:48 -0700700 if (adm()->EnableBuiltInAGC(*options.auto_gain_control) == 0 &&
kwiberg102c6a62015-10-30 02:47:38 -0700701 *options.auto_gain_control) {
henrikac14f5ff2015-09-23 14:08:33 +0200702 // Disable internal software AGC if built-in AGC is enabled,
703 // i.e., replace the software AGC with the built-in AGC.
Karl Wibergbe579832015-11-10 22:34:18 +0100704 options.auto_gain_control = rtc::Optional<bool>(false);
henrikac14f5ff2015-09-23 14:08:33 +0200705 LOG(LS_INFO) << "Disabling AGC since built-in AGC will be used instead";
706 }
707 }
kwiberg102c6a62015-10-30 02:47:38 -0700708 if (voep->SetAgcStatus(*options.auto_gain_control, agc_mode) == -1) {
709 LOG_RTCERR2(SetAgcStatus, *options.auto_gain_control, agc_mode);
buildbot@webrtc.org13d67762014-05-02 17:33:29 +0000710 return false;
711 } else {
kwiberg102c6a62015-10-30 02:47:38 -0700712 LOG(LS_INFO) << "Auto gain set to " << *options.auto_gain_control
713 << " with mode " << agc_mode;
buildbot@webrtc.org13d67762014-05-02 17:33:29 +0000714 }
715 }
716
kwiberg102c6a62015-10-30 02:47:38 -0700717 if (options.tx_agc_target_dbov || options.tx_agc_digital_compression_gain ||
718 options.tx_agc_limiter) {
buildbot@webrtc.org13d67762014-05-02 17:33:29 +0000719 // Override default_agc_config_. Generally, an unset option means "leave
720 // the VoE bits alone" in this function, so we want whatever is set to be
721 // stored as the new "default". If we didn't, then setting e.g.
722 // tx_agc_target_dbov would reset digital compression gain and limiter
723 // settings.
724 // Also, if we don't update default_agc_config_, then adjust_agc_delta
725 // would be an offset from the original values, and not whatever was set
726 // explicitly.
kwiberg102c6a62015-10-30 02:47:38 -0700727 default_agc_config_.targetLeveldBOv = options.tx_agc_target_dbov.value_or(
728 default_agc_config_.targetLeveldBOv);
buildbot@webrtc.org13d67762014-05-02 17:33:29 +0000729 default_agc_config_.digitalCompressionGaindB =
kwiberg102c6a62015-10-30 02:47:38 -0700730 options.tx_agc_digital_compression_gain.value_or(
buildbot@webrtc.org13d67762014-05-02 17:33:29 +0000731 default_agc_config_.digitalCompressionGaindB);
732 default_agc_config_.limiterEnable =
kwiberg102c6a62015-10-30 02:47:38 -0700733 options.tx_agc_limiter.value_or(default_agc_config_.limiterEnable);
buildbot@webrtc.org13d67762014-05-02 17:33:29 +0000734 if (voe_wrapper_->processing()->SetAgcConfig(default_agc_config_) == -1) {
735 LOG_RTCERR3(SetAgcConfig,
736 default_agc_config_.targetLeveldBOv,
737 default_agc_config_.digitalCompressionGaindB,
738 default_agc_config_.limiterEnable);
739 return false;
740 }
741 }
742
Alejandro Luebsc9b0c262016-05-16 15:32:38 -0700743 if (options.intelligibility_enhancer) {
744 intelligibility_enhancer_ = options.intelligibility_enhancer;
745 }
746 if (intelligibility_enhancer_ && *intelligibility_enhancer_) {
747 LOG(LS_INFO) << "Enabling NS when Intelligibility Enhancer is active.";
748 options.noise_suppression = intelligibility_enhancer_;
749 }
750
kwiberg102c6a62015-10-30 02:47:38 -0700751 if (options.noise_suppression) {
Alejandro Luebsc9b0c262016-05-16 15:32:38 -0700752 if (adm()->BuiltInNSIsAvailable()) {
753 bool builtin_ns =
754 *options.noise_suppression &&
755 !(intelligibility_enhancer_ && *intelligibility_enhancer_);
756 if (adm()->EnableBuiltInNS(builtin_ns) == 0 && builtin_ns) {
henrikac14f5ff2015-09-23 14:08:33 +0200757 // Disable internal software NS if built-in NS is enabled,
758 // i.e., replace the software NS with the built-in NS.
Karl Wibergbe579832015-11-10 22:34:18 +0100759 options.noise_suppression = rtc::Optional<bool>(false);
henrikac14f5ff2015-09-23 14:08:33 +0200760 LOG(LS_INFO) << "Disabling NS since built-in NS will be used instead";
761 }
762 }
kwiberg102c6a62015-10-30 02:47:38 -0700763 if (voep->SetNsStatus(*options.noise_suppression, ns_mode) == -1) {
764 LOG_RTCERR2(SetNsStatus, *options.noise_suppression, ns_mode);
buildbot@webrtc.org13d67762014-05-02 17:33:29 +0000765 return false;
766 } else {
kwiberg102c6a62015-10-30 02:47:38 -0700767 LOG(LS_INFO) << "Noise suppression set to " << *options.noise_suppression
henrikac14f5ff2015-09-23 14:08:33 +0200768 << " with mode " << ns_mode;
buildbot@webrtc.org13d67762014-05-02 17:33:29 +0000769 }
770 }
771
kwiberg102c6a62015-10-30 02:47:38 -0700772 if (options.highpass_filter) {
773 LOG(LS_INFO) << "High pass filter enabled? " << *options.highpass_filter;
774 if (voep->EnableHighPassFilter(*options.highpass_filter) == -1) {
775 LOG_RTCERR1(SetHighpassFilterStatus, *options.highpass_filter);
buildbot@webrtc.org13d67762014-05-02 17:33:29 +0000776 return false;
777 }
778 }
779
kwiberg102c6a62015-10-30 02:47:38 -0700780 if (options.stereo_swapping) {
781 LOG(LS_INFO) << "Stereo swapping enabled? " << *options.stereo_swapping;
782 voep->EnableStereoChannelSwapping(*options.stereo_swapping);
783 if (voep->IsStereoChannelSwappingEnabled() != *options.stereo_swapping) {
784 LOG_RTCERR1(EnableStereoChannelSwapping, *options.stereo_swapping);
buildbot@webrtc.org13d67762014-05-02 17:33:29 +0000785 return false;
786 }
787 }
788
kwiberg102c6a62015-10-30 02:47:38 -0700789 if (options.audio_jitter_buffer_max_packets) {
790 LOG(LS_INFO) << "NetEq capacity is "
791 << *options.audio_jitter_buffer_max_packets;
Henrik Lundin64dad832015-05-11 12:44:23 +0200792 voe_config_.Set<webrtc::NetEqCapacityConfig>(
kwiberg102c6a62015-10-30 02:47:38 -0700793 new webrtc::NetEqCapacityConfig(
794 *options.audio_jitter_buffer_max_packets));
Henrik Lundin64dad832015-05-11 12:44:23 +0200795 }
796
kwiberg102c6a62015-10-30 02:47:38 -0700797 if (options.audio_jitter_buffer_fast_accelerate) {
798 LOG(LS_INFO) << "NetEq fast mode? "
799 << *options.audio_jitter_buffer_fast_accelerate;
Henrik Lundin5263b3c2015-06-01 10:29:41 +0200800 voe_config_.Set<webrtc::NetEqFastAccelerate>(
kwiberg102c6a62015-10-30 02:47:38 -0700801 new webrtc::NetEqFastAccelerate(
802 *options.audio_jitter_buffer_fast_accelerate));
Henrik Lundin5263b3c2015-06-01 10:29:41 +0200803 }
804
kwiberg102c6a62015-10-30 02:47:38 -0700805 if (options.typing_detection) {
806 LOG(LS_INFO) << "Typing detection is enabled? "
807 << *options.typing_detection;
808 if (voep->SetTypingDetectionStatus(*options.typing_detection) == -1) {
buildbot@webrtc.org13d67762014-05-02 17:33:29 +0000809 // In case of error, log the info and continue
kwiberg102c6a62015-10-30 02:47:38 -0700810 LOG_RTCERR1(SetTypingDetectionStatus, *options.typing_detection);
buildbot@webrtc.org13d67762014-05-02 17:33:29 +0000811 }
812 }
813
kwiberg102c6a62015-10-30 02:47:38 -0700814 if (options.adjust_agc_delta) {
815 LOG(LS_INFO) << "Adjust agc delta is " << *options.adjust_agc_delta;
816 if (!AdjustAgcLevel(*options.adjust_agc_delta)) {
buildbot@webrtc.org13d67762014-05-02 17:33:29 +0000817 return false;
818 }
819 }
820
buildbot@webrtc.org1f8a2372014-08-28 10:52:44 +0000821 webrtc::Config config;
822
kwiberg102c6a62015-10-30 02:47:38 -0700823 if (options.delay_agnostic_aec)
824 delay_agnostic_aec_ = options.delay_agnostic_aec;
825 if (delay_agnostic_aec_) {
826 LOG(LS_INFO) << "Delay agnostic aec is enabled? " << *delay_agnostic_aec_;
henrik.lundin0f133b92015-07-02 00:17:55 -0700827 config.Set<webrtc::DelayAgnostic>(
kwiberg102c6a62015-10-30 02:47:38 -0700828 new webrtc::DelayAgnostic(*delay_agnostic_aec_));
Bjorn Volckerbf395c12015-03-25 22:45:56 +0100829 }
830
kwiberg102c6a62015-10-30 02:47:38 -0700831 if (options.extended_filter_aec) {
832 extended_filter_aec_ = options.extended_filter_aec;
833 }
834 if (extended_filter_aec_) {
835 LOG(LS_INFO) << "Extended filter aec is enabled? " << *extended_filter_aec_;
Henrik Lundin441f6342015-06-09 16:03:13 +0200836 config.Set<webrtc::ExtendedFilter>(
kwiberg102c6a62015-10-30 02:47:38 -0700837 new webrtc::ExtendedFilter(*extended_filter_aec_));
buildbot@webrtc.org1f8a2372014-08-28 10:52:44 +0000838 }
839
kwiberg102c6a62015-10-30 02:47:38 -0700840 if (options.experimental_ns) {
841 experimental_ns_ = options.experimental_ns;
842 }
843 if (experimental_ns_) {
844 LOG(LS_INFO) << "Experimental ns is enabled? " << *experimental_ns_;
buildbot@webrtc.org1f8a2372014-08-28 10:52:44 +0000845 config.Set<webrtc::ExperimentalNs>(
kwiberg102c6a62015-10-30 02:47:38 -0700846 new webrtc::ExperimentalNs(*experimental_ns_));
buildbot@webrtc.org1f8a2372014-08-28 10:52:44 +0000847 }
buildbot@webrtc.org1f8a2372014-08-28 10:52:44 +0000848
Alejandro Luebsc9b0c262016-05-16 15:32:38 -0700849 if (intelligibility_enhancer_) {
850 LOG(LS_INFO) << "Intelligibility Enhancer is enabled? "
851 << *intelligibility_enhancer_;
852 config.Set<webrtc::Intelligibility>(
853 new webrtc::Intelligibility(*intelligibility_enhancer_));
854 }
855
peaha3333bf2016-06-30 00:02:34 -0700856 if (options.level_control) {
857 level_control_ = options.level_control;
858 }
859
860 LOG(LS_INFO) << "Level control: "
861 << (!!level_control_ ? *level_control_ : -1);
862 if (level_control_) {
863 config.Set<webrtc::LevelControl>(new webrtc::LevelControl(*level_control_));
864 }
865
buildbot@webrtc.org1f8a2372014-08-28 10:52:44 +0000866 // We check audioproc for the benefit of tests, since FakeWebRtcVoiceEngine
867 // returns NULL on audio_processing().
868 webrtc::AudioProcessing* audioproc = voe_wrapper_->base()->audio_processing();
869 if (audioproc) {
870 audioproc->SetExtraOptions(config);
871 }
872
kwiberg102c6a62015-10-30 02:47:38 -0700873 if (options.recording_sample_rate) {
874 LOG(LS_INFO) << "Recording sample rate is "
875 << *options.recording_sample_rate;
solenberg5b5129a2016-04-08 05:35:48 -0700876 if (adm()->SetRecordingSampleRate(*options.recording_sample_rate)) {
kwiberg102c6a62015-10-30 02:47:38 -0700877 LOG_RTCERR1(SetRecordingSampleRate, *options.recording_sample_rate);
buildbot@webrtc.org13d67762014-05-02 17:33:29 +0000878 }
879 }
880
kwiberg102c6a62015-10-30 02:47:38 -0700881 if (options.playout_sample_rate) {
882 LOG(LS_INFO) << "Playout sample rate is " << *options.playout_sample_rate;
solenberg5b5129a2016-04-08 05:35:48 -0700883 if (adm()->SetPlayoutSampleRate(*options.playout_sample_rate)) {
kwiberg102c6a62015-10-30 02:47:38 -0700884 LOG_RTCERR1(SetPlayoutSampleRate, *options.playout_sample_rate);
buildbot@webrtc.org13d67762014-05-02 17:33:29 +0000885 }
886 }
887
888 return true;
889}
890
solenberg246b8172015-12-08 09:50:23 -0800891void WebRtcVoiceEngine::SetDefaultDevices() {
solenberg566ef242015-11-06 15:34:49 -0800892 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
kjellanderfcfc8042016-01-14 11:01:09 -0800893#if !defined(WEBRTC_IOS)
solenberg246b8172015-12-08 09:50:23 -0800894 int in_id = kDefaultAudioDeviceId;
895 int out_id = kDefaultAudioDeviceId;
896 LOG(LS_INFO) << "Setting microphone to (id=" << in_id
897 << ") and speaker to (id=" << out_id << ")";
buildbot@webrtc.org13d67762014-05-02 17:33:29 +0000898
solenbergc1a1b352015-09-22 13:31:20 -0700899 bool ret = true;
solenberg246b8172015-12-08 09:50:23 -0800900 if (voe_wrapper_->hw()->SetRecordingDevice(in_id) == -1) {
901 LOG_RTCERR1(SetRecordingDevice, in_id);
buildbot@webrtc.org13d67762014-05-02 17:33:29 +0000902 ret = false;
903 }
solenberg246b8172015-12-08 09:50:23 -0800904 webrtc::AudioProcessing* ap = voe()->base()->audio_processing();
905 if (ap) {
906 ap->Initialize();
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000907 }
908
solenberg246b8172015-12-08 09:50:23 -0800909 if (voe_wrapper_->hw()->SetPlayoutDevice(out_id) == -1) {
910 LOG_RTCERR1(SetPlayoutDevice, out_id);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000911 ret = false;
912 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000913
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000914 if (ret) {
solenberg246b8172015-12-08 09:50:23 -0800915 LOG(LS_INFO) << "Set microphone to (id=" << in_id
916 << ") and speaker to (id=" << out_id << ")";
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000917 }
kjellanderfcfc8042016-01-14 11:01:09 -0800918#endif // !WEBRTC_IOS
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000919}
920
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000921int WebRtcVoiceEngine::GetInputLevel() {
solenberg566ef242015-11-06 15:34:49 -0800922 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000923 unsigned int ulevel;
924 return (voe_wrapper_->volume()->GetSpeechInputLevel(ulevel) != -1) ?
925 static_cast<int>(ulevel) : -1;
926}
927
ossudedfd282016-06-14 07:12:39 -0700928const std::vector<AudioCodec>& WebRtcVoiceEngine::send_codecs() const {
929 RTC_DCHECK(signal_thread_checker_.CalledOnValidThread());
ossuc54071d2016-08-17 02:45:41 -0700930 return send_codecs_;
ossudedfd282016-06-14 07:12:39 -0700931}
932
933const std::vector<AudioCodec>& WebRtcVoiceEngine::recv_codecs() const {
solenberg566ef242015-11-06 15:34:49 -0800934 RTC_DCHECK(signal_thread_checker_.CalledOnValidThread());
ossuc54071d2016-08-17 02:45:41 -0700935 return recv_codecs_;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000936}
937
Stefan Holmer9d69c3f2015-12-07 10:45:43 +0100938RtpCapabilities WebRtcVoiceEngine::GetCapabilities() const {
solenberg566ef242015-11-06 15:34:49 -0800939 RTC_DCHECK(signal_thread_checker_.CalledOnValidThread());
Stefan Holmer9d69c3f2015-12-07 10:45:43 +0100940 RtpCapabilities capabilities;
Stefan Holmer9d69c3f2015-12-07 10:45:43 +0100941 capabilities.header_extensions.push_back(
isheriff6f8d6862016-05-26 11:24:55 -0700942 webrtc::RtpExtension(webrtc::RtpExtension::kAudioLevelUri,
943 webrtc::RtpExtension::kAudioLevelDefaultId));
944 capabilities.header_extensions.push_back(
945 webrtc::RtpExtension(webrtc::RtpExtension::kAbsSendTimeUri,
946 webrtc::RtpExtension::kAbsSendTimeDefaultId));
stefanba4c0e42016-02-04 04:12:24 -0800947 if (webrtc::field_trial::FindFullName("WebRTC-Audio-SendSideBwe") ==
948 "Enabled") {
isheriff6f8d6862016-05-26 11:24:55 -0700949 capabilities.header_extensions.push_back(webrtc::RtpExtension(
950 webrtc::RtpExtension::kTransportSequenceNumberUri,
951 webrtc::RtpExtension::kTransportSequenceNumberDefaultId));
stefanba4c0e42016-02-04 04:12:24 -0800952 }
Stefan Holmer9d69c3f2015-12-07 10:45:43 +0100953 return capabilities;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000954}
955
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000956int WebRtcVoiceEngine::GetLastEngineError() {
solenberg566ef242015-11-06 15:34:49 -0800957 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000958 return voe_wrapper_->error();
959}
960
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000961void WebRtcVoiceEngine::Print(webrtc::TraceLevel level, const char* trace,
962 int length) {
solenberg566ef242015-11-06 15:34:49 -0800963 // Note: This callback can happen on any thread!
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000964 rtc::LoggingSeverity sev = rtc::LS_VERBOSE;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000965 if (level == webrtc::kTraceError || level == webrtc::kTraceCritical)
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000966 sev = rtc::LS_ERROR;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000967 else if (level == webrtc::kTraceWarning)
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000968 sev = rtc::LS_WARNING;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000969 else if (level == webrtc::kTraceStateInfo || level == webrtc::kTraceInfo)
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000970 sev = rtc::LS_INFO;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000971 else if (level == webrtc::kTraceTerseInfo)
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000972 sev = rtc::LS_INFO;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000973
solenberg72e29d22016-03-08 06:35:16 -0800974 // Skip past boilerplate prefix text.
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000975 if (length < 72) {
976 std::string msg(trace, length);
977 LOG(LS_ERROR) << "Malformed webrtc log message: ";
978 LOG_V(sev) << msg;
979 } else {
980 std::string msg(trace + 71, length - 72);
Peter Boströmd5c75b12015-09-23 13:24:32 +0200981 LOG_V(sev) << "webrtc: " << msg;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000982 }
983}
984
solenberg63b34542015-09-29 06:06:31 -0700985void WebRtcVoiceEngine::RegisterChannel(WebRtcVoiceMediaChannel* channel) {
solenberg566ef242015-11-06 15:34:49 -0800986 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
987 RTC_DCHECK(channel);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000988 channels_.push_back(channel);
989}
990
solenberg63b34542015-09-29 06:06:31 -0700991void WebRtcVoiceEngine::UnregisterChannel(WebRtcVoiceMediaChannel* channel) {
solenberg566ef242015-11-06 15:34:49 -0800992 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
solenberg63b34542015-09-29 06:06:31 -0700993 auto it = std::find(channels_.begin(), channels_.end(), channel);
solenberg566ef242015-11-06 15:34:49 -0800994 RTC_DCHECK(it != channels_.end());
995 channels_.erase(it);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000996}
997
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000998// Adjusts the default AGC target level by the specified delta.
999// NB: If we start messing with other config fields, we'll want
1000// to save the current webrtc::AgcConfig as well.
1001bool WebRtcVoiceEngine::AdjustAgcLevel(int delta) {
solenberg566ef242015-11-06 15:34:49 -08001002 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001003 webrtc::AgcConfig config = default_agc_config_;
1004 config.targetLeveldBOv -= delta;
1005
1006 LOG(LS_INFO) << "Adjusting AGC level from default -"
1007 << default_agc_config_.targetLeveldBOv << "dB to -"
1008 << config.targetLeveldBOv << "dB";
1009
1010 if (voe_wrapper_->processing()->SetAgcConfig(config) == -1) {
1011 LOG_RTCERR1(SetAgcConfig, config.targetLeveldBOv);
1012 return false;
1013 }
1014 return true;
1015}
1016
ivocd66b44d2016-01-15 03:06:36 -08001017bool WebRtcVoiceEngine::StartAecDump(rtc::PlatformFile file,
1018 int64_t max_size_bytes) {
solenberg566ef242015-11-06 15:34:49 -08001019 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00001020 FILE* aec_dump_file_stream = rtc::FdopenPlatformFileForWriting(file);
wu@webrtc.orga8910d22014-01-23 22:12:45 +00001021 if (!aec_dump_file_stream) {
1022 LOG(LS_ERROR) << "Could not open AEC dump file stream.";
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00001023 if (!rtc::ClosePlatformFile(file))
wu@webrtc.orga8910d22014-01-23 22:12:45 +00001024 LOG(LS_WARNING) << "Could not close file.";
1025 return false;
1026 }
wu@webrtc.orga9890802013-12-13 00:21:03 +00001027 StopAecDump();
ivocd66b44d2016-01-15 03:06:36 -08001028 if (voe_wrapper_->base()->audio_processing()->StartDebugRecording(
1029 aec_dump_file_stream, max_size_bytes) !=
wu@webrtc.orga9890802013-12-13 00:21:03 +00001030 webrtc::AudioProcessing::kNoError) {
wu@webrtc.orga8910d22014-01-23 22:12:45 +00001031 LOG_RTCERR0(StartDebugRecording);
1032 fclose(aec_dump_file_stream);
wu@webrtc.orga9890802013-12-13 00:21:03 +00001033 return false;
1034 }
1035 is_dumping_aec_ = true;
1036 return true;
wu@webrtc.orga9890802013-12-13 00:21:03 +00001037}
1038
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001039void WebRtcVoiceEngine::StartAecDump(const std::string& filename) {
solenberg566ef242015-11-06 15:34:49 -08001040 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001041 if (!is_dumping_aec_) {
1042 // Start dumping AEC when we are not dumping.
ivocd66b44d2016-01-15 03:06:36 -08001043 if (voe_wrapper_->base()->audio_processing()->StartDebugRecording(
1044 filename.c_str(), -1) != webrtc::AudioProcessing::kNoError) {
wu@webrtc.orga9890802013-12-13 00:21:03 +00001045 LOG_RTCERR1(StartDebugRecording, filename.c_str());
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001046 } else {
1047 is_dumping_aec_ = true;
1048 }
1049 }
1050}
1051
1052void WebRtcVoiceEngine::StopAecDump() {
solenberg566ef242015-11-06 15:34:49 -08001053 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001054 if (is_dumping_aec_) {
1055 // Stop dumping AEC when we are dumping.
ivocd66b44d2016-01-15 03:06:36 -08001056 if (voe_wrapper_->base()->audio_processing()->StopDebugRecording() !=
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001057 webrtc::AudioProcessing::kNoError) {
1058 LOG_RTCERR0(StopDebugRecording);
1059 }
1060 is_dumping_aec_ = false;
1061 }
1062}
1063
solenberg0a617e22015-10-20 15:49:38 -07001064int WebRtcVoiceEngine::CreateVoEChannel() {
solenberg566ef242015-11-06 15:34:49 -08001065 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
solenberg0a617e22015-10-20 15:49:38 -07001066 return voe_wrapper_->base()->CreateChannel(voe_config_);
sergeyu@chromium.org5bc25c42013-12-05 00:24:06 +00001067}
1068
solenberg5b5129a2016-04-08 05:35:48 -07001069webrtc::AudioDeviceModule* WebRtcVoiceEngine::adm() {
1070 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
1071 RTC_DCHECK(adm_);
1072 return adm_;
1073}
1074
ossuc54071d2016-08-17 02:45:41 -07001075AudioCodecs WebRtcVoiceEngine::CollectRecvCodecs() const {
1076 PayloadTypeMapper mapper;
1077 AudioCodecs out;
ossud4e9f622016-08-18 02:01:17 -07001078 const std::vector<webrtc::AudioCodecSpec>& specs =
1079 decoder_factory_->GetSupportedDecoders();
ossuc54071d2016-08-17 02:45:41 -07001080
1081 // Only generate CN payload types for these clockrates
1082 std::map<int, bool, std::greater<int>> generate_cn = {{ 8000, false },
1083 { 16000, false },
1084 { 32000, false }};
1085
1086 auto map_format = [&mapper, &out] (const webrtc::SdpAudioFormat& format) {
1087 rtc::Optional<AudioCodec> opt_codec = mapper.ToAudioCodec(format);
1088 if (!opt_codec) {
1089 LOG(LS_ERROR) << "Unable to assign payload type to format: " << format;
1090 return false;
1091 }
1092
1093 auto& codec = *opt_codec;
1094 if (IsCodec(codec, kOpusCodecName)) {
1095 // TODO(ossu): Set this specifically for Opus for now, until we have a
1096 // better way of dealing with rtcp-fb parameters.
1097 codec.AddFeedbackParam(
1098 FeedbackParam(kRtcpFbParamTransportCc, kParamValueEmpty));
1099 }
1100 out.push_back(codec);
1101 return true;
1102 };
1103
ossud4e9f622016-08-18 02:01:17 -07001104 for (const auto& spec : specs) {
1105 if (map_format(spec.format) && spec.allow_comfort_noise) {
1106 // Generate a CN entry if the decoder allows it and we support the
1107 // clockrate.
1108 auto cn = generate_cn.find(spec.format.clockrate_hz);
1109 if (cn != generate_cn.end()) {
ossuc54071d2016-08-17 02:45:41 -07001110 cn->second = true;
1111 }
1112 }
1113 }
1114
1115 // Add CN codecs after "proper" audio codecs
1116 for (const auto& cn : generate_cn) {
1117 if (cn.second) {
1118 map_format({kCnCodecName, cn.first, 1});
1119 }
1120 }
1121
1122 // Add telephone-event codec last
1123 map_format({kDtmfCodecName, 8000, 1});
1124
1125 return out;
1126}
1127
solenbergc96df772015-10-21 13:01:53 -07001128class WebRtcVoiceMediaChannel::WebRtcAudioSendStream
Taylor Brandstetter1a018dc2016-03-08 12:37:39 -08001129 : public AudioSource::Sink {
mallinath@webrtc.org67ee6b92014-02-03 16:57:16 +00001130 public:
skvlade0d46372016-04-07 22:59:22 -07001131 WebRtcAudioSendStream(int ch,
1132 webrtc::AudioTransport* voe_audio_transport,
1133 uint32_t ssrc,
1134 const std::string& c_name,
solenberg971cab02016-06-14 10:02:41 -07001135 const SendCodecSpec& send_codec_spec,
solenberg3a941542015-11-16 07:34:50 -08001136 const std::vector<webrtc::RtpExtension>& extensions,
mflodman3d7db262016-04-29 00:57:13 -07001137 webrtc::Call* call,
1138 webrtc::Transport* send_transport)
solenberg7add0582015-11-20 09:59:34 -08001139 : voe_audio_transport_(voe_audio_transport),
solenberg3a941542015-11-16 07:34:50 -08001140 call_(call),
mflodman3d7db262016-04-29 00:57:13 -07001141 config_(send_transport),
skvlade0d46372016-04-07 22:59:22 -07001142 rtp_parameters_(CreateRtpParametersWithOneEncoding()) {
solenberg85a04962015-10-27 03:35:21 -07001143 RTC_DCHECK_GE(ch, 0);
1144 // TODO(solenberg): Once we're not using FakeWebRtcVoiceEngine anymore:
1145 // RTC_DCHECK(voe_audio_transport);
solenbergc96df772015-10-21 13:01:53 -07001146 RTC_DCHECK(call);
solenberg85a04962015-10-27 03:35:21 -07001147 audio_capture_thread_checker_.DetachFromThread();
solenberg3a941542015-11-16 07:34:50 -08001148 config_.rtp.ssrc = ssrc;
1149 config_.rtp.c_name = c_name;
1150 config_.voe_channel_id = ch;
solenberg971cab02016-06-14 10:02:41 -07001151 config_.rtp.extensions = extensions;
1152 RecreateAudioSendStream(send_codec_spec);
solenbergc96df772015-10-21 13:01:53 -07001153 }
solenberg3a941542015-11-16 07:34:50 -08001154
solenbergc96df772015-10-21 13:01:53 -07001155 ~WebRtcAudioSendStream() override {
solenberg566ef242015-11-06 15:34:49 -08001156 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
Taylor Brandstetter1a018dc2016-03-08 12:37:39 -08001157 ClearSource();
solenbergc96df772015-10-21 13:01:53 -07001158 call_->DestroyAudioSendStream(stream_);
1159 }
henrike@webrtc.org1e09a712013-07-26 19:17:59 +00001160
solenberg971cab02016-06-14 10:02:41 -07001161 void RecreateAudioSendStream(const SendCodecSpec& send_codec_spec) {
1162 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
1163 if (stream_) {
1164 call_->DestroyAudioSendStream(stream_);
1165 stream_ = nullptr;
1166 }
1167 config_.rtp.nack.rtp_history_ms =
1168 send_codec_spec.nack_enabled ? kNackRtpHistoryMs : 0;
1169 RTC_DCHECK(!stream_);
1170 stream_ = call_->CreateAudioSendStream(config_);
1171 RTC_CHECK(stream_);
1172 UpdateSendState();
1173 }
1174
solenberg3a941542015-11-16 07:34:50 -08001175 void RecreateAudioSendStream(
1176 const std::vector<webrtc::RtpExtension>& extensions) {
1177 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
1178 if (stream_) {
1179 call_->DestroyAudioSendStream(stream_);
1180 stream_ = nullptr;
1181 }
1182 config_.rtp.extensions = extensions;
mflodman86cc6ff2016-07-26 04:44:06 -07001183 if (webrtc::field_trial::FindFullName("WebRTC-AdaptAudioBitrate") ==
1184 "Enabled") {
1185 // TODO(mflodman): Keep testing this and set proper values.
1186 // Note: This is an early experiment currently only supported by Opus.
1187 config_.min_bitrate_kbps = kOpusMinBitrate;
1188 config_.max_bitrate_kbps = kOpusBitrateFb;
1189 }
1190
solenberg3a941542015-11-16 07:34:50 -08001191 RTC_DCHECK(!stream_);
1192 stream_ = call_->CreateAudioSendStream(config_);
1193 RTC_CHECK(stream_);
solenberg6d6e7c52016-04-13 09:07:30 -07001194 UpdateSendState();
solenberg3a941542015-11-16 07:34:50 -08001195 }
1196
solenberg8842c3e2016-03-11 03:06:41 -08001197 bool SendTelephoneEvent(int payload_type, int event, int duration_ms) {
Fredrik Solenbergb5727682015-12-04 15:22:19 +01001198 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
1199 RTC_DCHECK(stream_);
1200 return stream_->SendTelephoneEvent(payload_type, event, duration_ms);
1201 }
1202
Taylor Brandstetter1a018dc2016-03-08 12:37:39 -08001203 void SetSend(bool send) {
1204 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
1205 send_ = send;
1206 UpdateSendState();
1207 }
1208
solenberg94218532016-06-16 10:53:22 -07001209 void SetMuted(bool muted) {
1210 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
1211 RTC_DCHECK(stream_);
1212 stream_->SetMuted(muted);
1213 muted_ = muted;
1214 }
1215
1216 bool muted() const {
1217 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
1218 return muted_;
1219 }
1220
solenberg3a941542015-11-16 07:34:50 -08001221 webrtc::AudioSendStream::Stats GetStats() const {
1222 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
1223 RTC_DCHECK(stream_);
1224 return stream_->GetStats();
1225 }
1226
Taylor Brandstetter1a018dc2016-03-08 12:37:39 -08001227 // Starts the sending by setting ourselves as a sink to the AudioSource to
1228 // get data callbacks.
henrike@webrtc.orga7b98182014-02-21 15:51:43 +00001229 // This method is called on the libjingle worker thread.
mallinath@webrtc.org67ee6b92014-02-03 16:57:16 +00001230 // TODO(xians): Make sure Start() is called only once.
Taylor Brandstetter1a018dc2016-03-08 12:37:39 -08001231 void SetSource(AudioSource* source) {
solenberg566ef242015-11-06 15:34:49 -08001232 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
Taylor Brandstetter1a018dc2016-03-08 12:37:39 -08001233 RTC_DCHECK(source);
1234 if (source_) {
1235 RTC_DCHECK(source_ == source);
mallinath@webrtc.org67ee6b92014-02-03 16:57:16 +00001236 return;
1237 }
Taylor Brandstetter1a018dc2016-03-08 12:37:39 -08001238 source->SetSink(this);
1239 source_ = source;
1240 UpdateSendState();
mallinath@webrtc.org67ee6b92014-02-03 16:57:16 +00001241 }
1242
Taylor Brandstetter1a018dc2016-03-08 12:37:39 -08001243 // Stops sending by setting the sink of the AudioSource to nullptr. No data
mallinath@webrtc.org67ee6b92014-02-03 16:57:16 +00001244 // callback will be received after this method.
henrike@webrtc.orga7b98182014-02-21 15:51:43 +00001245 // This method is called on the libjingle worker thread.
Taylor Brandstetter1a018dc2016-03-08 12:37:39 -08001246 void ClearSource() {
solenberg566ef242015-11-06 15:34:49 -08001247 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
Taylor Brandstetter1a018dc2016-03-08 12:37:39 -08001248 if (source_) {
1249 source_->SetSink(nullptr);
1250 source_ = nullptr;
solenberg98c68862015-10-09 03:27:14 -07001251 }
Taylor Brandstetter1a018dc2016-03-08 12:37:39 -08001252 UpdateSendState();
mallinath@webrtc.org67ee6b92014-02-03 16:57:16 +00001253 }
1254
Taylor Brandstetter1a018dc2016-03-08 12:37:39 -08001255 // AudioSource::Sink implementation.
henrike@webrtc.orga7b98182014-02-21 15:51:43 +00001256 // This method is called on the audio thread.
kjellander@webrtc.org14665ff2015-03-04 12:58:35 +00001257 void OnData(const void* audio_data,
1258 int bits_per_sample,
1259 int sample_rate,
Peter Kasting69558702016-01-12 16:26:35 -08001260 size_t number_of_channels,
Peter Kastingdce40cf2015-08-24 14:52:23 -07001261 size_t number_of_frames) override {
solenberg566ef242015-11-06 15:34:49 -08001262 RTC_DCHECK(!worker_thread_checker_.CalledOnValidThread());
solenberg85a04962015-10-27 03:35:21 -07001263 RTC_DCHECK(audio_capture_thread_checker_.CalledOnValidThread());
solenbergc96df772015-10-21 13:01:53 -07001264 RTC_DCHECK(voe_audio_transport_);
maxmorin1aee0b52016-08-15 11:46:19 -07001265 voe_audio_transport_->PushCaptureData(config_.voe_channel_id, audio_data,
1266 bits_per_sample, sample_rate,
1267 number_of_channels, number_of_frames);
henrike@webrtc.orga7b98182014-02-21 15:51:43 +00001268 }
1269
Taylor Brandstetter1a018dc2016-03-08 12:37:39 -08001270 // Callback from the |source_| when it is going away. In case Start() has
henrike@webrtc.orga7b98182014-02-21 15:51:43 +00001271 // never been called, this callback won't be triggered.
kjellander@webrtc.org14665ff2015-03-04 12:58:35 +00001272 void OnClose() override {
solenberg566ef242015-11-06 15:34:49 -08001273 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
Taylor Brandstetter1a018dc2016-03-08 12:37:39 -08001274 // Set |source_| to nullptr to make sure no more callback will get into
1275 // the source.
1276 source_ = nullptr;
1277 UpdateSendState();
mallinath@webrtc.org67ee6b92014-02-03 16:57:16 +00001278 }
1279
1280 // Accessor to the VoE channel ID.
solenberg85a04962015-10-27 03:35:21 -07001281 int channel() const {
solenberg566ef242015-11-06 15:34:49 -08001282 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
solenberg7add0582015-11-20 09:59:34 -08001283 return config_.voe_channel_id;
solenberg85a04962015-10-27 03:35:21 -07001284 }
mallinath@webrtc.org67ee6b92014-02-03 16:57:16 +00001285
skvlade0d46372016-04-07 22:59:22 -07001286 const webrtc::RtpParameters& rtp_parameters() const {
1287 return rtp_parameters_;
1288 }
1289
Taylor Brandstetter55dd7082016-05-03 13:50:11 -07001290 void SetRtpParameters(const webrtc::RtpParameters& parameters) {
skvlade0d46372016-04-07 22:59:22 -07001291 RTC_CHECK_EQ(1UL, parameters.encodings.size());
1292 rtp_parameters_ = parameters;
Taylor Brandstetter55dd7082016-05-03 13:50:11 -07001293 // parameters.encodings[0].active could have changed.
1294 UpdateSendState();
skvlade0d46372016-04-07 22:59:22 -07001295 }
1296
mallinath@webrtc.org67ee6b92014-02-03 16:57:16 +00001297 private:
Taylor Brandstetter1a018dc2016-03-08 12:37:39 -08001298 void UpdateSendState() {
1299 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
1300 RTC_DCHECK(stream_);
Taylor Brandstetter55dd7082016-05-03 13:50:11 -07001301 RTC_DCHECK_EQ(1UL, rtp_parameters_.encodings.size());
1302 if (send_ && source_ != nullptr && rtp_parameters_.encodings[0].active) {
Taylor Brandstetter1a018dc2016-03-08 12:37:39 -08001303 stream_->Start();
1304 } else { // !send || source_ = nullptr
1305 stream_->Stop();
1306 }
1307 }
1308
solenberg566ef242015-11-06 15:34:49 -08001309 rtc::ThreadChecker worker_thread_checker_;
solenberg85a04962015-10-27 03:35:21 -07001310 rtc::ThreadChecker audio_capture_thread_checker_;
solenbergc96df772015-10-21 13:01:53 -07001311 webrtc::AudioTransport* const voe_audio_transport_ = nullptr;
1312 webrtc::Call* call_ = nullptr;
solenberg3a941542015-11-16 07:34:50 -08001313 webrtc::AudioSendStream::Config config_;
1314 // The stream is owned by WebRtcAudioSendStream and may be reallocated if
1315 // configuration changes.
solenbergc96df772015-10-21 13:01:53 -07001316 webrtc::AudioSendStream* stream_ = nullptr;
mallinath@webrtc.org67ee6b92014-02-03 16:57:16 +00001317
Taylor Brandstetter1a018dc2016-03-08 12:37:39 -08001318 // Raw pointer to AudioSource owned by LocalAudioTrackHandler.
mallinath@webrtc.org67ee6b92014-02-03 16:57:16 +00001319 // PeerConnection will make sure invalidating the pointer before the object
1320 // goes away.
Taylor Brandstetter1a018dc2016-03-08 12:37:39 -08001321 AudioSource* source_ = nullptr;
1322 bool send_ = false;
solenberg94218532016-06-16 10:53:22 -07001323 bool muted_ = false;
skvlade0d46372016-04-07 22:59:22 -07001324 webrtc::RtpParameters rtp_parameters_;
henrike@webrtc.orga7b98182014-02-21 15:51:43 +00001325
solenbergc96df772015-10-21 13:01:53 -07001326 RTC_DISALLOW_IMPLICIT_CONSTRUCTORS(WebRtcAudioSendStream);
1327};
1328
1329class WebRtcVoiceMediaChannel::WebRtcAudioReceiveStream {
1330 public:
ossu29b1a8d2016-06-13 07:34:51 -07001331 WebRtcAudioReceiveStream(
1332 int ch,
1333 uint32_t remote_ssrc,
1334 uint32_t local_ssrc,
1335 bool use_transport_cc,
solenberg8189b022016-06-14 12:13:00 -07001336 bool use_nack,
ossu29b1a8d2016-06-13 07:34:51 -07001337 const std::string& sync_group,
1338 const std::vector<webrtc::RtpExtension>& extensions,
1339 webrtc::Call* call,
1340 webrtc::Transport* rtcp_send_transport,
1341 const rtc::scoped_refptr<webrtc::AudioDecoderFactory>& decoder_factory)
stefanba4c0e42016-02-04 04:12:24 -08001342 : call_(call), config_() {
solenberg7add0582015-11-20 09:59:34 -08001343 RTC_DCHECK_GE(ch, 0);
1344 RTC_DCHECK(call);
1345 config_.rtp.remote_ssrc = remote_ssrc;
solenberg31fec402016-05-06 02:13:12 -07001346 config_.rtcp_send_transport = rtcp_send_transport;
solenberg7add0582015-11-20 09:59:34 -08001347 config_.voe_channel_id = ch;
1348 config_.sync_group = sync_group;
ossu29b1a8d2016-06-13 07:34:51 -07001349 config_.decoder_factory = decoder_factory;
solenberg4a0f7b52016-06-16 13:07:33 -07001350 RecreateAudioReceiveStream(local_ssrc,
1351 use_transport_cc,
1352 use_nack,
1353 extensions);
solenberg7add0582015-11-20 09:59:34 -08001354 }
solenbergc96df772015-10-21 13:01:53 -07001355
solenberg7add0582015-11-20 09:59:34 -08001356 ~WebRtcAudioReceiveStream() {
1357 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
1358 call_->DestroyAudioReceiveStream(stream_);
1359 }
1360
solenberg4a0f7b52016-06-16 13:07:33 -07001361 void RecreateAudioReceiveStream(uint32_t local_ssrc) {
solenberg7add0582015-11-20 09:59:34 -08001362 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
solenberg4a0f7b52016-06-16 13:07:33 -07001363 RecreateAudioReceiveStream(local_ssrc,
1364 config_.rtp.transport_cc,
solenberg8189b022016-06-14 12:13:00 -07001365 config_.rtp.nack.rtp_history_ms != 0,
solenberg4a0f7b52016-06-16 13:07:33 -07001366 config_.rtp.extensions);
solenberg7add0582015-11-20 09:59:34 -08001367 }
solenberg8189b022016-06-14 12:13:00 -07001368
1369 void RecreateAudioReceiveStream(bool use_transport_cc, bool use_nack) {
solenberg7add0582015-11-20 09:59:34 -08001370 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
solenberg4a0f7b52016-06-16 13:07:33 -07001371 RecreateAudioReceiveStream(config_.rtp.local_ssrc,
1372 use_transport_cc,
solenberg8189b022016-06-14 12:13:00 -07001373 use_nack,
1374 config_.rtp.extensions);
solenberg7add0582015-11-20 09:59:34 -08001375 }
1376
solenberg4a0f7b52016-06-16 13:07:33 -07001377 void RecreateAudioReceiveStream(
1378 const std::vector<webrtc::RtpExtension>& extensions) {
1379 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
1380 RecreateAudioReceiveStream(config_.rtp.local_ssrc,
1381 config_.rtp.transport_cc,
1382 config_.rtp.nack.rtp_history_ms != 0,
1383 extensions);
1384 }
1385
solenberg7add0582015-11-20 09:59:34 -08001386 webrtc::AudioReceiveStream::Stats GetStats() const {
1387 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
1388 RTC_DCHECK(stream_);
1389 return stream_->GetStats();
1390 }
1391
1392 int channel() const {
1393 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
1394 return config_.voe_channel_id;
1395 }
solenbergc96df772015-10-21 13:01:53 -07001396
kwiberg686a8ef2016-02-26 03:00:35 -08001397 void SetRawAudioSink(std::unique_ptr<webrtc::AudioSinkInterface> sink) {
Tommif888bb52015-12-12 01:37:01 +01001398 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
kwiberg686a8ef2016-02-26 03:00:35 -08001399 stream_->SetSink(std::move(sink));
Tommif888bb52015-12-12 01:37:01 +01001400 }
1401
solenberg217fb662016-06-17 08:30:54 -07001402 void SetOutputVolume(double volume) {
1403 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
1404 stream_->SetGain(volume);
1405 }
1406
aleloi84ef6152016-08-04 05:28:21 -07001407 void SetPlayout(bool playout) {
1408 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
1409 RTC_DCHECK(stream_);
1410 if (playout) {
1411 LOG(LS_INFO) << "Starting playout for channel #" << channel();
1412 stream_->Start();
1413 } else {
1414 LOG(LS_INFO) << "Stopping playout for channel #" << channel();
1415 stream_->Stop();
1416 }
1417 }
1418
solenbergc96df772015-10-21 13:01:53 -07001419 private:
stefanba4c0e42016-02-04 04:12:24 -08001420 void RecreateAudioReceiveStream(
solenberg4a0f7b52016-06-16 13:07:33 -07001421 uint32_t local_ssrc,
stefanba4c0e42016-02-04 04:12:24 -08001422 bool use_transport_cc,
solenberg8189b022016-06-14 12:13:00 -07001423 bool use_nack,
solenberg7add0582015-11-20 09:59:34 -08001424 const std::vector<webrtc::RtpExtension>& extensions) {
1425 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
1426 if (stream_) {
1427 call_->DestroyAudioReceiveStream(stream_);
1428 stream_ = nullptr;
1429 }
solenberg4a0f7b52016-06-16 13:07:33 -07001430 config_.rtp.local_ssrc = local_ssrc;
stefanba4c0e42016-02-04 04:12:24 -08001431 config_.rtp.transport_cc = use_transport_cc;
solenberg8189b022016-06-14 12:13:00 -07001432 config_.rtp.nack.rtp_history_ms = use_nack ? kNackRtpHistoryMs : 0;
1433 config_.rtp.extensions = extensions;
solenberg7add0582015-11-20 09:59:34 -08001434 RTC_DCHECK(!stream_);
1435 stream_ = call_->CreateAudioReceiveStream(config_);
1436 RTC_CHECK(stream_);
1437 }
1438
1439 rtc::ThreadChecker worker_thread_checker_;
1440 webrtc::Call* call_ = nullptr;
1441 webrtc::AudioReceiveStream::Config config_;
1442 // The stream is owned by WebRtcAudioReceiveStream and may be reallocated if
1443 // configuration changes.
1444 webrtc::AudioReceiveStream* stream_ = nullptr;
solenbergc96df772015-10-21 13:01:53 -07001445
1446 RTC_DISALLOW_IMPLICIT_CONSTRUCTORS(WebRtcAudioReceiveStream);
henrike@webrtc.org1e09a712013-07-26 19:17:59 +00001447};
1448
Fredrik Solenberg709ed672015-09-15 12:26:33 +02001449WebRtcVoiceMediaChannel::WebRtcVoiceMediaChannel(WebRtcVoiceEngine* engine,
nisse51542be2016-02-12 02:27:06 -08001450 const MediaConfig& config,
Fredrik Solenbergb071a192015-09-17 16:42:56 +02001451 const AudioOptions& options,
Fredrik Solenberg709ed672015-09-15 12:26:33 +02001452 webrtc::Call* call)
nisse51542be2016-02-12 02:27:06 -08001453 : VoiceMediaChannel(config), engine_(engine), call_(call) {
solenberg0a617e22015-10-20 15:49:38 -07001454 LOG(LS_VERBOSE) << "WebRtcVoiceMediaChannel::WebRtcVoiceMediaChannel";
solenberg566ef242015-11-06 15:34:49 -08001455 RTC_DCHECK(call);
solenberg0a617e22015-10-20 15:49:38 -07001456 engine->RegisterChannel(this);
Fredrik Solenbergb071a192015-09-17 16:42:56 +02001457 SetOptions(options);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001458}
1459
1460WebRtcVoiceMediaChannel::~WebRtcVoiceMediaChannel() {
solenberg566ef242015-11-06 15:34:49 -08001461 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
solenberg0a617e22015-10-20 15:49:38 -07001462 LOG(LS_VERBOSE) << "WebRtcVoiceMediaChannel::~WebRtcVoiceMediaChannel";
solenberg7add0582015-11-20 09:59:34 -08001463 // TODO(solenberg): Should be able to delete the streams directly, without
1464 // going through RemoveNnStream(), once stream objects handle
1465 // all (de)configuration.
solenbergc96df772015-10-21 13:01:53 -07001466 while (!send_streams_.empty()) {
1467 RemoveSendStream(send_streams_.begin()->first);
solenbergd97ec302015-10-07 01:40:33 -07001468 }
solenberg7add0582015-11-20 09:59:34 -08001469 while (!recv_streams_.empty()) {
1470 RemoveRecvStream(recv_streams_.begin()->first);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001471 }
solenberg0a617e22015-10-20 15:49:38 -07001472 engine()->UnregisterChannel(this);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001473}
1474
nisse51542be2016-02-12 02:27:06 -08001475rtc::DiffServCodePoint WebRtcVoiceMediaChannel::PreferredDscp() const {
1476 return kAudioDscpValue;
1477}
1478
Peter Thatcherc2ee2c82015-08-07 16:05:34 -07001479bool WebRtcVoiceMediaChannel::SetSendParameters(
1480 const AudioSendParameters& params) {
Peter Boströmca8b4042016-03-08 14:24:13 -08001481 TRACE_EVENT0("webrtc", "WebRtcVoiceMediaChannel::SetSendParameters");
solenberg566ef242015-11-06 15:34:49 -08001482 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
solenberg7e4e01a2015-12-02 08:05:01 -08001483 LOG(LS_INFO) << "WebRtcVoiceMediaChannel::SetSendParameters: "
1484 << params.ToString();
Peter Thatcherc2ee2c82015-08-07 16:05:34 -07001485 // TODO(pthatcher): Refactor this to be more clean now that we have
1486 // all the information at once.
solenberg3a941542015-11-16 07:34:50 -08001487
1488 if (!SetSendCodecs(params.codecs)) {
1489 return false;
1490 }
1491
solenberg7e4e01a2015-12-02 08:05:01 -08001492 if (!ValidateRtpExtensions(params.extensions)) {
1493 return false;
1494 }
1495 std::vector<webrtc::RtpExtension> filtered_extensions =
1496 FilterRtpExtensions(params.extensions,
1497 webrtc::RtpExtension::IsSupportedForAudio, true);
1498 if (send_rtp_extensions_ != filtered_extensions) {
1499 send_rtp_extensions_.swap(filtered_extensions);
solenberg3a941542015-11-16 07:34:50 -08001500 for (auto& it : send_streams_) {
1501 it.second->RecreateAudioSendStream(send_rtp_extensions_);
1502 }
1503 }
1504
deadbeef80346142016-04-27 14:17:10 -07001505 if (!SetMaxSendBitrate(params.max_bandwidth_bps)) {
solenberg3a941542015-11-16 07:34:50 -08001506 return false;
1507 }
1508 return SetOptions(params.options);
Peter Thatcherc2ee2c82015-08-07 16:05:34 -07001509}
1510
1511bool WebRtcVoiceMediaChannel::SetRecvParameters(
1512 const AudioRecvParameters& params) {
Peter Boströmca8b4042016-03-08 14:24:13 -08001513 TRACE_EVENT0("webrtc", "WebRtcVoiceMediaChannel::SetRecvParameters");
solenberg566ef242015-11-06 15:34:49 -08001514 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
solenberg7e4e01a2015-12-02 08:05:01 -08001515 LOG(LS_INFO) << "WebRtcVoiceMediaChannel::SetRecvParameters: "
1516 << params.ToString();
Peter Thatcherc2ee2c82015-08-07 16:05:34 -07001517 // TODO(pthatcher): Refactor this to be more clean now that we have
1518 // all the information at once.
solenberg7add0582015-11-20 09:59:34 -08001519
1520 if (!SetRecvCodecs(params.codecs)) {
1521 return false;
1522 }
1523
solenberg7e4e01a2015-12-02 08:05:01 -08001524 if (!ValidateRtpExtensions(params.extensions)) {
1525 return false;
1526 }
1527 std::vector<webrtc::RtpExtension> filtered_extensions =
1528 FilterRtpExtensions(params.extensions,
1529 webrtc::RtpExtension::IsSupportedForAudio, false);
1530 if (recv_rtp_extensions_ != filtered_extensions) {
1531 recv_rtp_extensions_.swap(filtered_extensions);
solenberg7add0582015-11-20 09:59:34 -08001532 for (auto& it : recv_streams_) {
1533 it.second->RecreateAudioReceiveStream(recv_rtp_extensions_);
1534 }
1535 }
solenberg7add0582015-11-20 09:59:34 -08001536 return true;
Peter Thatcherc2ee2c82015-08-07 16:05:34 -07001537}
1538
Taylor Brandstetterdb0cd9e2016-05-16 11:40:30 -07001539webrtc::RtpParameters WebRtcVoiceMediaChannel::GetRtpSendParameters(
skvlade0d46372016-04-07 22:59:22 -07001540 uint32_t ssrc) const {
1541 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
1542 auto it = send_streams_.find(ssrc);
1543 if (it == send_streams_.end()) {
Taylor Brandstetterdb0cd9e2016-05-16 11:40:30 -07001544 LOG(LS_WARNING) << "Attempting to get RTP send parameters for stream "
1545 << "with ssrc " << ssrc << " which doesn't exist.";
skvlade0d46372016-04-07 22:59:22 -07001546 return webrtc::RtpParameters();
1547 }
1548
Taylor Brandstetter0cd086b2016-04-20 16:23:10 -07001549 webrtc::RtpParameters rtp_params = it->second->rtp_parameters();
1550 // Need to add the common list of codecs to the send stream-specific
1551 // RTP parameters.
1552 for (const AudioCodec& codec : send_codecs_) {
1553 rtp_params.codecs.push_back(codec.ToCodecParameters());
1554 }
1555 return rtp_params;
skvlade0d46372016-04-07 22:59:22 -07001556}
1557
Taylor Brandstetterdb0cd9e2016-05-16 11:40:30 -07001558bool WebRtcVoiceMediaChannel::SetRtpSendParameters(
skvlade0d46372016-04-07 22:59:22 -07001559 uint32_t ssrc,
1560 const webrtc::RtpParameters& parameters) {
1561 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
1562 if (!ValidateRtpParameters(parameters)) {
1563 return false;
1564 }
1565 auto it = send_streams_.find(ssrc);
1566 if (it == send_streams_.end()) {
Taylor Brandstetterdb0cd9e2016-05-16 11:40:30 -07001567 LOG(LS_WARNING) << "Attempting to set RTP send parameters for stream "
1568 << "with ssrc " << ssrc << " which doesn't exist.";
skvlade0d46372016-04-07 22:59:22 -07001569 return false;
1570 }
1571
Taylor Brandstetterdb0cd9e2016-05-16 11:40:30 -07001572 // TODO(deadbeef): Handle setting parameters with a list of codecs in a
1573 // different order (which should change the send codec).
1574 webrtc::RtpParameters current_parameters = GetRtpSendParameters(ssrc);
1575 if (current_parameters.codecs != parameters.codecs) {
1576 LOG(LS_ERROR) << "Using SetParameters to change the set of codecs "
1577 << "is not currently supported.";
1578 return false;
1579 }
1580
1581 if (!SetChannelSendParameters(it->second->channel(), parameters)) {
1582 LOG(LS_WARNING) << "Failed to set send RtpParameters.";
skvlade0d46372016-04-07 22:59:22 -07001583 return false;
1584 }
Taylor Brandstetter0cd086b2016-04-20 16:23:10 -07001585 // Codecs are handled at the WebRtcVoiceMediaChannel level.
1586 webrtc::RtpParameters reduced_params = parameters;
1587 reduced_params.codecs.clear();
Taylor Brandstetter55dd7082016-05-03 13:50:11 -07001588 it->second->SetRtpParameters(reduced_params);
skvlade0d46372016-04-07 22:59:22 -07001589 return true;
1590}
1591
Taylor Brandstetterdb0cd9e2016-05-16 11:40:30 -07001592webrtc::RtpParameters WebRtcVoiceMediaChannel::GetRtpReceiveParameters(
1593 uint32_t ssrc) const {
1594 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
1595 auto it = recv_streams_.find(ssrc);
1596 if (it == recv_streams_.end()) {
1597 LOG(LS_WARNING) << "Attempting to get RTP receive parameters for stream "
1598 << "with ssrc " << ssrc << " which doesn't exist.";
1599 return webrtc::RtpParameters();
1600 }
1601
1602 // TODO(deadbeef): Return stream-specific parameters.
1603 webrtc::RtpParameters rtp_params = CreateRtpParametersWithOneEncoding();
1604 for (const AudioCodec& codec : recv_codecs_) {
1605 rtp_params.codecs.push_back(codec.ToCodecParameters());
1606 }
1607 return rtp_params;
1608}
1609
1610bool WebRtcVoiceMediaChannel::SetRtpReceiveParameters(
1611 uint32_t ssrc,
1612 const webrtc::RtpParameters& parameters) {
1613 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
1614 if (!ValidateRtpParameters(parameters)) {
1615 return false;
1616 }
1617 auto it = recv_streams_.find(ssrc);
1618 if (it == recv_streams_.end()) {
1619 LOG(LS_WARNING) << "Attempting to set RTP receive parameters for stream "
1620 << "with ssrc " << ssrc << " which doesn't exist.";
1621 return false;
1622 }
1623
1624 webrtc::RtpParameters current_parameters = GetRtpReceiveParameters(ssrc);
1625 if (current_parameters != parameters) {
1626 LOG(LS_ERROR) << "Changing the RTP receive parameters is currently "
1627 << "unsupported.";
1628 return false;
1629 }
1630 return true;
1631}
1632
skvlade0d46372016-04-07 22:59:22 -07001633bool WebRtcVoiceMediaChannel::ValidateRtpParameters(
1634 const webrtc::RtpParameters& rtp_parameters) {
1635 if (rtp_parameters.encodings.size() != 1) {
1636 LOG(LS_ERROR)
1637 << "Attempted to set RtpParameters without exactly one encoding";
1638 return false;
1639 }
1640 return true;
1641}
1642
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001643bool WebRtcVoiceMediaChannel::SetOptions(const AudioOptions& options) {
solenberg566ef242015-11-06 15:34:49 -08001644 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001645 LOG(LS_INFO) << "Setting voice channel options: "
1646 << options.ToString();
1647
1648 // We retain all of the existing options, and apply the given ones
1649 // on top. This means there is no way to "clear" options such that
1650 // they go back to the engine default.
1651 options_.SetAll(options);
solenberg246b8172015-12-08 09:50:23 -08001652 if (!engine()->ApplyOptions(options_)) {
1653 LOG(LS_WARNING) <<
1654 "Failed to apply engine options during channel SetOptions.";
1655 return false;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001656 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001657 LOG(LS_INFO) << "Set voice channel options. Current options: "
1658 << options_.ToString();
1659 return true;
1660}
1661
1662bool WebRtcVoiceMediaChannel::SetRecvCodecs(
1663 const std::vector<AudioCodec>& codecs) {
solenberg566ef242015-11-06 15:34:49 -08001664 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
solenberg8fb30c32015-10-13 03:06:58 -07001665
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001666 // Set the payload types to be used for incoming media.
solenberg0b675462015-10-09 01:37:09 -07001667 LOG(LS_INFO) << "Setting receive voice codecs.";
solenberg0b675462015-10-09 01:37:09 -07001668
1669 if (!VerifyUniquePayloadTypes(codecs)) {
1670 LOG(LS_ERROR) << "Codec payload types overlap.";
1671 return false;
1672 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001673
1674 std::vector<AudioCodec> new_codecs;
1675 // Find all new codecs. We allow adding new codecs but don't allow changing
1676 // the payload type of codecs that is already configured since we might
1677 // already be receiving packets with that payload type.
Fredrik Solenbergaf9fb212015-08-26 10:45:53 +02001678 for (const AudioCodec& codec : codecs) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001679 AudioCodec old_codec;
Fredrik Solenbergaf9fb212015-08-26 10:45:53 +02001680 if (FindCodec(recv_codecs_, codec, &old_codec)) {
1681 if (old_codec.id != codec.id) {
1682 LOG(LS_ERROR) << codec.name << " payload type changed.";
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001683 return false;
1684 }
1685 } else {
Fredrik Solenbergaf9fb212015-08-26 10:45:53 +02001686 new_codecs.push_back(codec);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001687 }
1688 }
1689 if (new_codecs.empty()) {
1690 // There are no new codecs to configure. Already configured codecs are
1691 // never removed.
1692 return true;
1693 }
1694
1695 if (playout_) {
1696 // Receive codecs can not be changed while playing. So we temporarily
1697 // pause playout.
aleloi84ef6152016-08-04 05:28:21 -07001698 ChangePlayout(false);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001699 }
1700
solenberg26c8c912015-11-27 04:00:25 -08001701 bool result = true;
1702 for (const AudioCodec& codec : new_codecs) {
solenberg72e29d22016-03-08 06:35:16 -08001703 webrtc::CodecInst voe_codec = {0};
solenberg26c8c912015-11-27 04:00:25 -08001704 if (WebRtcVoiceEngine::ToCodecInst(codec, &voe_codec)) {
1705 LOG(LS_INFO) << ToString(codec);
1706 voe_codec.pltype = codec.id;
1707 for (const auto& ch : recv_streams_) {
1708 if (engine()->voe()->codec()->SetRecPayloadType(
1709 ch.second->channel(), voe_codec) == -1) {
1710 LOG_RTCERR2(SetRecPayloadType, ch.second->channel(),
1711 ToString(voe_codec));
1712 result = false;
1713 }
1714 }
1715 } else {
1716 LOG(LS_WARNING) << "Unknown codec " << ToString(codec);
1717 result = false;
1718 break;
1719 }
1720 }
Fredrik Solenbergaf9fb212015-08-26 10:45:53 +02001721 if (result) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001722 recv_codecs_ = codecs;
1723 }
1724
1725 if (desired_playout_ && !playout_) {
aleloi84ef6152016-08-04 05:28:21 -07001726 ChangePlayout(desired_playout_);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001727 }
Fredrik Solenbergaf9fb212015-08-26 10:45:53 +02001728 return result;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001729}
1730
solenberg72e29d22016-03-08 06:35:16 -08001731// Utility function called from SetSendParameters() to extract current send
1732// codec settings from the given list of codecs (originally from SDP). Both send
1733// and receive streams may be reconfigured based on the new settings.
wu@webrtc.orgcadf9042013-08-30 21:24:16 +00001734bool WebRtcVoiceMediaChannel::SetSendCodecs(
1735 const std::vector<AudioCodec>& codecs) {
solenberg566ef242015-11-06 15:34:49 -08001736 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
Fredrik Solenbergb5727682015-12-04 15:22:19 +01001737 // TODO(solenberg): Validate input - that payload types don't overlap, are
1738 // within range, filter out codecs we don't support,
solenberg31642aa2016-03-14 08:00:37 -07001739 // redundant codecs etc - the same way it is done for
1740 // RtpHeaderExtensions.
solenbergd97ec302015-10-07 01:40:33 -07001741
Fredrik Solenbergb5727682015-12-04 15:22:19 +01001742 // Find the DTMF telephone event "codec" payload type.
1743 dtmf_payload_type_ = rtc::Optional<int>();
Fredrik Solenbergaf9fb212015-08-26 10:45:53 +02001744 for (const AudioCodec& codec : codecs) {
Fredrik Solenbergaf9fb212015-08-26 10:45:53 +02001745 if (IsCodec(codec, kDtmfCodecName)) {
solenberg31642aa2016-03-14 08:00:37 -07001746 if (codec.id < kMinPayloadType || codec.id > kMaxPayloadType) {
1747 return false;
1748 }
Fredrik Solenbergb5727682015-12-04 15:22:19 +01001749 dtmf_payload_type_ = rtc::Optional<int>(codec.id);
1750 break;
wu@webrtc.orgcadf9042013-08-30 21:24:16 +00001751 }
1752 }
1753
solenberg72e29d22016-03-08 06:35:16 -08001754 // Scan through the list to figure out the codec to use for sending, along
kwiberg68061362016-06-14 08:04:47 -07001755 // with the proper configuration for VAD, CNG, NACK and Opus-specific
solenberg72e29d22016-03-08 06:35:16 -08001756 // parameters.
kwiberg68061362016-06-14 08:04:47 -07001757 // TODO(solenberg): Refactor this logic once we create AudioEncoders here.
solenberg971cab02016-06-14 10:02:41 -07001758 SendCodecSpec send_codec_spec;
solenberg72e29d22016-03-08 06:35:16 -08001759 {
solenberg72e29d22016-03-08 06:35:16 -08001760 send_codec_spec.nack_enabled = send_codec_spec_.nack_enabled;
1761
1762 // Find send codec (the first non-telephone-event/CN codec).
1763 const AudioCodec* codec = WebRtcVoiceCodecs::GetPreferredCodec(
kwiberg68061362016-06-14 08:04:47 -07001764 codecs, &send_codec_spec.codec_inst);
solenberg72e29d22016-03-08 06:35:16 -08001765 if (!codec) {
1766 LOG(LS_WARNING) << "Received empty list of codecs.";
1767 return false;
1768 }
1769
1770 send_codec_spec.transport_cc_enabled = HasTransportCc(*codec);
kwiberg68061362016-06-14 08:04:47 -07001771 send_codec_spec.nack_enabled = HasNack(*codec);
solenberg72e29d22016-03-08 06:35:16 -08001772
kwiberg68061362016-06-14 08:04:47 -07001773 // For Opus as the send codec, we are to determine inband FEC, maximum
1774 // playback rate, and opus internal dtx.
1775 if (IsCodec(*codec, kOpusCodecName)) {
1776 GetOpusConfig(*codec, &send_codec_spec.codec_inst,
1777 &send_codec_spec.enable_codec_fec,
1778 &send_codec_spec.opus_max_playback_rate,
1779 &send_codec_spec.enable_opus_dtx);
1780 }
solenberg72e29d22016-03-08 06:35:16 -08001781
kwiberg68061362016-06-14 08:04:47 -07001782 // Set packet size if the AudioCodec param kCodecParamPTime is set.
1783 int ptime_ms = 0;
1784 if (codec->GetParam(kCodecParamPTime, &ptime_ms)) {
1785 if (!WebRtcVoiceCodecs::SetPTimeAsPacketSize(
1786 &send_codec_spec.codec_inst, ptime_ms)) {
1787 LOG(LS_WARNING) << "Failed to set packet size for codec "
1788 << send_codec_spec.codec_inst.plname;
1789 return false;
solenberg72e29d22016-03-08 06:35:16 -08001790 }
1791 }
1792
1793 // Loop through the codecs list again to find the CN codec.
1794 // TODO(solenberg): Break out into a separate function?
1795 for (const AudioCodec& codec : codecs) {
1796 // Ignore codecs we don't know about. The negotiation step should prevent
1797 // this, but double-check to be sure.
1798 webrtc::CodecInst voe_codec = {0};
1799 if (!WebRtcVoiceEngine::ToCodecInst(codec, &voe_codec)) {
1800 LOG(LS_WARNING) << "Unknown codec " << ToString(codec);
1801 continue;
1802 }
1803
1804 if (IsCodec(codec, kCnCodecName)) {
1805 // Turn voice activity detection/comfort noise on if supported.
1806 // Set the wideband CN payload type appropriately.
1807 // (narrowband always uses the static payload type 13).
1808 int cng_plfreq = -1;
1809 switch (codec.clockrate) {
1810 case 8000:
1811 case 16000:
1812 case 32000:
1813 cng_plfreq = codec.clockrate;
1814 break;
1815 default:
1816 LOG(LS_WARNING) << "CN frequency " << codec.clockrate
1817 << " not supported.";
1818 continue;
1819 }
1820 send_codec_spec.cng_payload_type = codec.id;
1821 send_codec_spec.cng_plfreq = cng_plfreq;
1822 break;
1823 }
1824 }
solenberg72e29d22016-03-08 06:35:16 -08001825 }
1826
solenberg971cab02016-06-14 10:02:41 -07001827 // Apply new settings to all streams.
1828 if (send_codec_spec_ != send_codec_spec) {
1829 send_codec_spec_ = std::move(send_codec_spec);
1830 for (const auto& kv : send_streams_) {
1831 kv.second->RecreateAudioSendStream(send_codec_spec_);
1832 if (!SetSendCodecs(kv.second->channel(), kv.second->rtp_parameters())) {
1833 return false;
1834 }
wu@webrtc.orgcadf9042013-08-30 21:24:16 +00001835 }
1836 }
1837
solenberg8189b022016-06-14 12:13:00 -07001838 // Check if the transport cc feedback or NACK status has changed on the
1839 // preferred send codec, and in that case reconfigure all receive streams.
1840 if (recv_transport_cc_enabled_ != send_codec_spec_.transport_cc_enabled ||
1841 recv_nack_enabled_ != send_codec_spec_.nack_enabled) {
solenberg72e29d22016-03-08 06:35:16 -08001842 LOG(LS_INFO) << "Recreate all the receive streams because the send "
1843 "codec has changed.";
1844 recv_transport_cc_enabled_ = send_codec_spec_.transport_cc_enabled;
solenberg8189b022016-06-14 12:13:00 -07001845 recv_nack_enabled_ = send_codec_spec_.nack_enabled;
solenberg72e29d22016-03-08 06:35:16 -08001846 for (auto& kv : recv_streams_) {
solenberg8189b022016-06-14 12:13:00 -07001847 kv.second->RecreateAudioReceiveStream(recv_transport_cc_enabled_,
1848 recv_nack_enabled_);
solenberg72e29d22016-03-08 06:35:16 -08001849 }
1850 }
1851
Taylor Brandstetter0cd086b2016-04-20 16:23:10 -07001852 send_codecs_ = codecs;
solenberg72e29d22016-03-08 06:35:16 -08001853 return true;
1854}
1855
1856// Apply current codec settings to a single voe::Channel used for sending.
skvlade0d46372016-04-07 22:59:22 -07001857bool WebRtcVoiceMediaChannel::SetSendCodecs(
1858 int channel,
1859 const webrtc::RtpParameters& rtp_parameters) {
solenberg971cab02016-06-14 10:02:41 -07001860 // Disable VAD and FEC unless we know the other side wants them.
solenberg72e29d22016-03-08 06:35:16 -08001861 engine()->voe()->codec()->SetVADStatus(channel, false);
solenberg72e29d22016-03-08 06:35:16 -08001862 engine()->voe()->codec()->SetFECStatus(channel, false);
1863
solenberg72e29d22016-03-08 06:35:16 -08001864 // Set the codec immediately, since SetVADStatus() depends on whether
1865 // the current codec is mono or stereo.
1866 if (!SetSendCodec(channel, send_codec_spec_.codec_inst)) {
1867 return false;
1868 }
1869
1870 // FEC should be enabled after SetSendCodec.
1871 if (send_codec_spec_.enable_codec_fec) {
1872 LOG(LS_INFO) << "Attempt to enable codec internal FEC on channel "
1873 << channel;
1874 if (engine()->voe()->codec()->SetFECStatus(channel, true) == -1) {
1875 // Enable codec internal FEC. Treat any failure as fatal internal error.
1876 LOG_RTCERR2(SetFECStatus, channel, true);
1877 return false;
1878 }
1879 }
1880
1881 if (IsCodec(send_codec_spec_.codec_inst, kOpusCodecName)) {
1882 // DTX and maxplaybackrate should be set after SetSendCodec. Because current
1883 // send codec has to be Opus.
1884
1885 // Set Opus internal DTX.
1886 LOG(LS_INFO) << "Attempt to "
1887 << (send_codec_spec_.enable_opus_dtx ? "enable" : "disable")
1888 << " Opus DTX on channel "
1889 << channel;
1890 if (engine()->voe()->codec()->SetOpusDtx(channel,
1891 send_codec_spec_.enable_opus_dtx)) {
1892 LOG_RTCERR2(SetOpusDtx, channel, send_codec_spec_.enable_opus_dtx);
1893 return false;
1894 }
1895
1896 // If opus_max_playback_rate <= 0, the default maximum playback rate
1897 // (48 kHz) will be used.
1898 if (send_codec_spec_.opus_max_playback_rate > 0) {
1899 LOG(LS_INFO) << "Attempt to set maximum playback rate to "
1900 << send_codec_spec_.opus_max_playback_rate
1901 << " Hz on channel "
1902 << channel;
1903 if (engine()->voe()->codec()->SetOpusMaxPlaybackRate(
1904 channel, send_codec_spec_.opus_max_playback_rate) == -1) {
1905 LOG_RTCERR2(SetOpusMaxPlaybackRate, channel,
1906 send_codec_spec_.opus_max_playback_rate);
1907 return false;
stefanba4c0e42016-02-04 04:12:24 -08001908 }
1909 }
1910 }
deadbeef80346142016-04-27 14:17:10 -07001911 // TODO(solenberg): SetMaxSendBitrate() yields another call to SetSendCodec().
skvlade0d46372016-04-07 22:59:22 -07001912 // Check if it is possible to fuse with the previous call in this function.
Taylor Brandstetterdb0cd9e2016-05-16 11:40:30 -07001913 SetChannelSendParameters(channel, rtp_parameters);
solenberg72e29d22016-03-08 06:35:16 -08001914
1915 // Set the CN payloadtype and the VAD status.
1916 if (send_codec_spec_.cng_payload_type != -1) {
1917 // The CN payload type for 8000 Hz clockrate is fixed at 13.
1918 if (send_codec_spec_.cng_plfreq != 8000) {
1919 webrtc::PayloadFrequencies cn_freq;
1920 switch (send_codec_spec_.cng_plfreq) {
1921 case 16000:
1922 cn_freq = webrtc::kFreq16000Hz;
1923 break;
1924 case 32000:
1925 cn_freq = webrtc::kFreq32000Hz;
1926 break;
1927 default:
1928 RTC_NOTREACHED();
1929 return false;
1930 }
1931 if (engine()->voe()->codec()->SetSendCNPayloadType(
1932 channel, send_codec_spec_.cng_payload_type, cn_freq) == -1) {
1933 LOG_RTCERR3(SetSendCNPayloadType, channel,
1934 send_codec_spec_.cng_payload_type, cn_freq);
1935 // TODO(ajm): This failure condition will be removed from VoE.
1936 // Restore the return here when we update to a new enough webrtc.
1937 //
1938 // Not returning false because the SetSendCNPayloadType will fail if
1939 // the channel is already sending.
1940 // This can happen if the remote description is applied twice, for
1941 // example in the case of ROAP on top of JSEP, where both side will
1942 // send the offer.
1943 }
1944 }
1945
1946 // Only turn on VAD if we have a CN payload type that matches the
1947 // clockrate for the codec we are going to use.
1948 if (send_codec_spec_.cng_plfreq == send_codec_spec_.codec_inst.plfreq &&
1949 send_codec_spec_.codec_inst.channels == 1) {
1950 // TODO(minyue): If CN frequency == 48000 Hz is allowed, consider the
1951 // interaction between VAD and Opus FEC.
1952 LOG(LS_INFO) << "Enabling VAD";
1953 if (engine()->voe()->codec()->SetVADStatus(channel, true) == -1) {
1954 LOG_RTCERR2(SetVADStatus, channel, true);
1955 return false;
1956 }
1957 }
1958 }
solenberg0a617e22015-10-20 15:49:38 -07001959 return true;
wu@webrtc.org9dba5252013-08-05 20:36:57 +00001960}
1961
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001962bool WebRtcVoiceMediaChannel::SetSendCodec(
wu@webrtc.org9dba5252013-08-05 20:36:57 +00001963 int channel, const webrtc::CodecInst& send_codec) {
1964 LOG(LS_INFO) << "Send channel " << channel << " selected voice codec "
1965 << ToString(send_codec) << ", bitrate=" << send_codec.rate;
1966
solenberg72e29d22016-03-08 06:35:16 -08001967 webrtc::CodecInst current_codec = {0};
wu@webrtc.org05e7b442014-04-01 17:44:24 +00001968 if (engine()->voe()->codec()->GetSendCodec(channel, current_codec) == 0 &&
1969 (send_codec == current_codec)) {
1970 // Codec is already configured, we can return without setting it again.
1971 return true;
1972 }
1973
wu@webrtc.org9dba5252013-08-05 20:36:57 +00001974 if (engine()->voe()->codec()->SetSendCodec(channel, send_codec) == -1) {
1975 LOG_RTCERR2(SetSendCodec, channel, ToString(send_codec));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001976 return false;
1977 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001978 return true;
1979}
1980
aleloi84ef6152016-08-04 05:28:21 -07001981void WebRtcVoiceMediaChannel::SetPlayout(bool playout) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001982 desired_playout_ = playout;
1983 return ChangePlayout(desired_playout_);
1984}
1985
aleloi84ef6152016-08-04 05:28:21 -07001986void WebRtcVoiceMediaChannel::ChangePlayout(bool playout) {
Peter Boströmca8b4042016-03-08 14:24:13 -08001987 TRACE_EVENT0("webrtc", "WebRtcVoiceMediaChannel::ChangePlayout");
solenberg566ef242015-11-06 15:34:49 -08001988 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001989 if (playout_ == playout) {
aleloi84ef6152016-08-04 05:28:21 -07001990 return;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001991 }
1992
aleloi84ef6152016-08-04 05:28:21 -07001993 for (const auto& kv : recv_streams_) {
1994 kv.second->SetPlayout(playout);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001995 }
solenberg1ac56142015-10-13 03:58:19 -07001996 playout_ = playout;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001997}
1998
Taylor Brandstetter1a018dc2016-03-08 12:37:39 -08001999void WebRtcVoiceMediaChannel::SetSend(bool send) {
Peter Boströmca8b4042016-03-08 14:24:13 -08002000 TRACE_EVENT0("webrtc", "WebRtcVoiceMediaChannel::SetSend");
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002001 if (send_ == send) {
Taylor Brandstetter1a018dc2016-03-08 12:37:39 -08002002 return;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002003 }
2004
solenbergd53a3f92016-04-14 13:56:37 -07002005 // Apply channel specific options, and initialize the ADM for recording (this
2006 // may take time on some platforms, e.g. Android).
Taylor Brandstetter1a018dc2016-03-08 12:37:39 -08002007 if (send) {
solenberg63b34542015-09-29 06:06:31 -07002008 engine()->ApplyOptions(options_);
solenbergd53a3f92016-04-14 13:56:37 -07002009
2010 // InitRecording() may return an error if the ADM is already recording.
2011 if (!engine()->adm()->RecordingIsInitialized() &&
2012 !engine()->adm()->Recording()) {
2013 if (engine()->adm()->InitRecording() != 0) {
2014 LOG(LS_WARNING) << "Failed to initialize recording";
2015 }
2016 }
solenberg63b34542015-09-29 06:06:31 -07002017 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002018
wu@webrtc.org9dba5252013-08-05 20:36:57 +00002019 // Change the settings on each send channel.
Taylor Brandstetter1a018dc2016-03-08 12:37:39 -08002020 for (auto& kv : send_streams_) {
2021 kv.second->SetSend(send);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002022 }
wu@webrtc.org9dba5252013-08-05 20:36:57 +00002023
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002024 send_ = send;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002025}
2026
Peter Boström0c4e06b2015-10-07 12:23:21 +02002027bool WebRtcVoiceMediaChannel::SetAudioSend(uint32_t ssrc,
2028 bool enable,
solenberg1dd98f32015-09-10 01:57:14 -07002029 const AudioOptions* options,
Taylor Brandstetter1a018dc2016-03-08 12:37:39 -08002030 AudioSource* source) {
solenberg566ef242015-11-06 15:34:49 -08002031 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
solenberg1dd98f32015-09-10 01:57:14 -07002032 // TODO(solenberg): The state change should be fully rolled back if any one of
2033 // these calls fail.
Taylor Brandstetter1a018dc2016-03-08 12:37:39 -08002034 if (!SetLocalSource(ssrc, source)) {
solenberg1dd98f32015-09-10 01:57:14 -07002035 return false;
2036 }
solenbergdfc8f4f2015-10-01 02:31:10 -07002037 if (!MuteStream(ssrc, !enable)) {
solenberg1dd98f32015-09-10 01:57:14 -07002038 return false;
2039 }
solenbergdfc8f4f2015-10-01 02:31:10 -07002040 if (enable && options) {
solenberg1dd98f32015-09-10 01:57:14 -07002041 return SetOptions(*options);
2042 }
2043 return true;
2044}
2045
solenberg0a617e22015-10-20 15:49:38 -07002046int WebRtcVoiceMediaChannel::CreateVoEChannel() {
2047 int id = engine()->CreateVoEChannel();
2048 if (id == -1) {
2049 LOG_RTCERR0(CreateVoEChannel);
2050 return -1;
wu@webrtc.org9dba5252013-08-05 20:36:57 +00002051 }
mflodman3d7db262016-04-29 00:57:13 -07002052
solenberg0a617e22015-10-20 15:49:38 -07002053 return id;
wu@webrtc.org9dba5252013-08-05 20:36:57 +00002054}
2055
solenberg7add0582015-11-20 09:59:34 -08002056bool WebRtcVoiceMediaChannel::DeleteVoEChannel(int channel) {
wu@webrtc.org9dba5252013-08-05 20:36:57 +00002057 if (engine()->voe()->base()->DeleteChannel(channel) == -1) {
2058 LOG_RTCERR1(DeleteChannel, channel);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002059 return false;
2060 }
wu@webrtc.org9dba5252013-08-05 20:36:57 +00002061 return true;
2062}
henrike@webrtc.org1e09a712013-07-26 19:17:59 +00002063
wu@webrtc.org9dba5252013-08-05 20:36:57 +00002064bool WebRtcVoiceMediaChannel::AddSendStream(const StreamParams& sp) {
Peter Boströmca8b4042016-03-08 14:24:13 -08002065 TRACE_EVENT0("webrtc", "WebRtcVoiceMediaChannel::AddSendStream");
solenberg566ef242015-11-06 15:34:49 -08002066 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
solenberg0a617e22015-10-20 15:49:38 -07002067 LOG(LS_INFO) << "AddSendStream: " << sp.ToString();
2068
2069 uint32_t ssrc = sp.first_ssrc();
2070 RTC_DCHECK(0 != ssrc);
2071
2072 if (GetSendChannelId(ssrc) != -1) {
2073 LOG(LS_ERROR) << "Stream already exists with ssrc " << ssrc;
wu@webrtc.org9dba5252013-08-05 20:36:57 +00002074 return false;
2075 }
2076
solenberg0a617e22015-10-20 15:49:38 -07002077 // Create a new channel for sending audio data.
2078 int channel = CreateVoEChannel();
2079 if (channel == -1) {
2080 return false;
wu@webrtc.org9dba5252013-08-05 20:36:57 +00002081 }
wu@webrtc.org9dba5252013-08-05 20:36:57 +00002082
solenbergc96df772015-10-21 13:01:53 -07002083 // Save the channel to send_streams_, so that RemoveSendStream() can still
wu@webrtc.org9dba5252013-08-05 20:36:57 +00002084 // delete the channel in case failure happens below.
mallinath@webrtc.org67ee6b92014-02-03 16:57:16 +00002085 webrtc::AudioTransport* audio_transport =
2086 engine()->voe()->base()->audio_transport();
mflodman3d7db262016-04-29 00:57:13 -07002087
skvlade0d46372016-04-07 22:59:22 -07002088 WebRtcAudioSendStream* stream = new WebRtcAudioSendStream(
solenberg971cab02016-06-14 10:02:41 -07002089 channel, audio_transport, ssrc, sp.cname, send_codec_spec_,
2090 send_rtp_extensions_, call_, this);
skvlade0d46372016-04-07 22:59:22 -07002091 send_streams_.insert(std::make_pair(ssrc, stream));
wu@webrtc.org9dba5252013-08-05 20:36:57 +00002092
solenberg0a617e22015-10-20 15:49:38 -07002093 // Set the current codecs to be used for the new channel. We need to do this
2094 // after adding the channel to send_channels_, because of how max bitrate is
2095 // currently being configured by SetSendCodec().
skvlade0d46372016-04-07 22:59:22 -07002096 if (HasSendCodec() && !SetSendCodecs(channel, stream->rtp_parameters())) {
solenberg0a617e22015-10-20 15:49:38 -07002097 RemoveSendStream(ssrc);
wu@webrtc.org9dba5252013-08-05 20:36:57 +00002098 return false;
2099 }
2100
solenberg4a0f7b52016-06-16 13:07:33 -07002101 // At this point the stream's local SSRC has been updated. If it is the first
2102 // send stream, make sure that all the receive streams are updated with the
2103 // same SSRC in order to send receiver reports.
solenbergc96df772015-10-21 13:01:53 -07002104 if (send_streams_.size() == 1) {
solenberg0a617e22015-10-20 15:49:38 -07002105 receiver_reports_ssrc_ = ssrc;
solenberg4a0f7b52016-06-16 13:07:33 -07002106 for (const auto& kv : recv_streams_) {
2107 // TODO(solenberg): Allow applications to set the RTCP SSRC of receive
2108 // streams instead, so we can avoid recreating the streams here.
2109 kv.second->RecreateAudioReceiveStream(ssrc);
2110 int recv_channel = kv.second->channel();
solenberg0a617e22015-10-20 15:49:38 -07002111 engine()->voe()->base()->AssociateSendChannel(recv_channel, channel);
2112 LOG(LS_INFO) << "VoiceEngine channel #" << recv_channel
2113 << " is associated with channel #" << channel << ".";
wu@webrtc.org9dba5252013-08-05 20:36:57 +00002114 }
2115 }
2116
Taylor Brandstetter1a018dc2016-03-08 12:37:39 -08002117 send_streams_[ssrc]->SetSend(send_);
2118 return true;
wu@webrtc.org9dba5252013-08-05 20:36:57 +00002119}
2120
Peter Boström0c4e06b2015-10-07 12:23:21 +02002121bool WebRtcVoiceMediaChannel::RemoveSendStream(uint32_t ssrc) {
Peter Boströmca8b4042016-03-08 14:24:13 -08002122 TRACE_EVENT0("webrtc", "WebRtcVoiceMediaChannel::RemoveSendStream");
solenberg566ef242015-11-06 15:34:49 -08002123 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
solenberg3a941542015-11-16 07:34:50 -08002124 LOG(LS_INFO) << "RemoveSendStream: " << ssrc;
2125
solenbergc96df772015-10-21 13:01:53 -07002126 auto it = send_streams_.find(ssrc);
2127 if (it == send_streams_.end()) {
wu@webrtc.org9dba5252013-08-05 20:36:57 +00002128 LOG(LS_WARNING) << "Try to remove stream with ssrc " << ssrc
2129 << " which doesn't exist.";
2130 return false;
2131 }
2132
Taylor Brandstetter1a018dc2016-03-08 12:37:39 -08002133 it->second->SetSend(false);
wu@webrtc.org9dba5252013-08-05 20:36:57 +00002134
solenberg7add0582015-11-20 09:59:34 -08002135 // Clean up and delete the send stream+channel.
Taylor Brandstetter1a018dc2016-03-08 12:37:39 -08002136 int channel = it->second->channel();
solenberg0a617e22015-10-20 15:49:38 -07002137 LOG(LS_INFO) << "Removing audio send stream " << ssrc
2138 << " with VoiceEngine channel #" << channel << ".";
solenberg7add0582015-11-20 09:59:34 -08002139 delete it->second;
2140 send_streams_.erase(it);
2141 if (!DeleteVoEChannel(channel)) {
solenberg0a617e22015-10-20 15:49:38 -07002142 return false;
wu@webrtc.org9dba5252013-08-05 20:36:57 +00002143 }
solenbergc96df772015-10-21 13:01:53 -07002144 if (send_streams_.empty()) {
Taylor Brandstetter1a018dc2016-03-08 12:37:39 -08002145 SetSend(false);
solenberg0a617e22015-10-20 15:49:38 -07002146 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002147 return true;
2148}
2149
2150bool WebRtcVoiceMediaChannel::AddRecvStream(const StreamParams& sp) {
Peter Boströmca8b4042016-03-08 14:24:13 -08002151 TRACE_EVENT0("webrtc", "WebRtcVoiceMediaChannel::AddRecvStream");
solenberg566ef242015-11-06 15:34:49 -08002152 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
solenbergd97ec302015-10-07 01:40:33 -07002153 LOG(LS_INFO) << "AddRecvStream: " << sp.ToString();
2154
solenberg0b675462015-10-09 01:37:09 -07002155 if (!ValidateStreamParams(sp)) {
wu@webrtc.org78187522013-10-07 23:32:02 +00002156 return false;
2157 }
2158
solenberg7add0582015-11-20 09:59:34 -08002159 const uint32_t ssrc = sp.first_ssrc();
solenberg0b675462015-10-09 01:37:09 -07002160 if (ssrc == 0) {
2161 LOG(LS_WARNING) << "AddRecvStream with ssrc==0 is not supported.";
2162 return false;
2163 }
2164
solenberg1ac56142015-10-13 03:58:19 -07002165 // Remove the default receive stream if one had been created with this ssrc;
2166 // we'll recreate it then.
2167 if (IsDefaultRecvStream(ssrc)) {
2168 RemoveRecvStream(ssrc);
2169 }
solenberg0b675462015-10-09 01:37:09 -07002170
solenberg7add0582015-11-20 09:59:34 -08002171 if (GetReceiveChannelId(ssrc) != -1) {
henrike@webrtc.org1e09a712013-07-26 19:17:59 +00002172 LOG(LS_ERROR) << "Stream already exists with ssrc " << ssrc;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002173 return false;
2174 }
Fredrik Solenberg4b60c732015-05-07 14:07:48 +02002175
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002176 // Create a new channel for receiving audio data.
solenberg7add0582015-11-20 09:59:34 -08002177 const int channel = CreateVoEChannel();
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002178 if (channel == -1) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002179 return false;
2180 }
Minyue2013aec2015-05-13 14:14:42 +02002181
solenberg1ac56142015-10-13 03:58:19 -07002182 // Turn off all supported codecs.
solenberg26c8c912015-11-27 04:00:25 -08002183 // TODO(solenberg): Remove once "no codecs" is the default state of a stream.
2184 for (webrtc::CodecInst voe_codec : webrtc::acm2::RentACodec::Database()) {
2185 voe_codec.pltype = -1;
2186 if (engine()->voe()->codec()->SetRecPayloadType(channel, voe_codec) == -1) {
2187 LOG_RTCERR2(SetRecPayloadType, channel, ToString(voe_codec));
2188 DeleteVoEChannel(channel);
2189 return false;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002190 }
2191 }
2192
solenberg1ac56142015-10-13 03:58:19 -07002193 // Only enable those configured for this channel.
2194 for (const auto& codec : recv_codecs_) {
solenberg72e29d22016-03-08 06:35:16 -08002195 webrtc::CodecInst voe_codec = {0};
solenberg26c8c912015-11-27 04:00:25 -08002196 if (WebRtcVoiceEngine::ToCodecInst(codec, &voe_codec)) {
solenberg1ac56142015-10-13 03:58:19 -07002197 voe_codec.pltype = codec.id;
2198 if (engine()->voe()->codec()->SetRecPayloadType(
2199 channel, voe_codec) == -1) {
2200 LOG_RTCERR2(SetRecPayloadType, channel, ToString(voe_codec));
solenberg7add0582015-11-20 09:59:34 -08002201 DeleteVoEChannel(channel);
solenberg1ac56142015-10-13 03:58:19 -07002202 return false;
2203 }
henrike@webrtc.org1e09a712013-07-26 19:17:59 +00002204 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002205 }
solenberg8fb30c32015-10-13 03:06:58 -07002206
solenberg7add0582015-11-20 09:59:34 -08002207 const int send_channel = GetSendChannelId(receiver_reports_ssrc_);
2208 if (send_channel != -1) {
2209 // Associate receive channel with first send channel (so the receive channel
2210 // can obtain RTT from the send channel)
2211 engine()->voe()->base()->AssociateSendChannel(channel, send_channel);
2212 LOG(LS_INFO) << "VoiceEngine channel #" << channel
2213 << " is associated with channel #" << send_channel << ".";
buildbot@webrtc.org150835e2014-05-06 15:54:38 +00002214 }
2215
stefanba4c0e42016-02-04 04:12:24 -08002216 recv_streams_.insert(std::make_pair(
2217 ssrc, new WebRtcAudioReceiveStream(channel, ssrc, receiver_reports_ssrc_,
solenberg72e29d22016-03-08 06:35:16 -08002218 recv_transport_cc_enabled_,
solenberg8189b022016-06-14 12:13:00 -07002219 recv_nack_enabled_,
solenberg72e29d22016-03-08 06:35:16 -08002220 sp.sync_label, recv_rtp_extensions_,
ossu29b1a8d2016-06-13 07:34:51 -07002221 call_, this,
2222 engine()->decoder_factory_)));
aleloi84ef6152016-08-04 05:28:21 -07002223 recv_streams_[ssrc]->SetPlayout(playout_);
solenberg7add0582015-11-20 09:59:34 -08002224
solenberg1ac56142015-10-13 03:58:19 -07002225 return true;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002226}
2227
Peter Boström0c4e06b2015-10-07 12:23:21 +02002228bool WebRtcVoiceMediaChannel::RemoveRecvStream(uint32_t ssrc) {
Peter Boströmca8b4042016-03-08 14:24:13 -08002229 TRACE_EVENT0("webrtc", "WebRtcVoiceMediaChannel::RemoveRecvStream");
solenberg566ef242015-11-06 15:34:49 -08002230 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
solenbergd97ec302015-10-07 01:40:33 -07002231 LOG(LS_INFO) << "RemoveRecvStream: " << ssrc;
2232
solenberg7add0582015-11-20 09:59:34 -08002233 const auto it = recv_streams_.find(ssrc);
2234 if (it == recv_streams_.end()) {
wu@webrtc.org9dba5252013-08-05 20:36:57 +00002235 LOG(LS_WARNING) << "Try to remove stream with ssrc " << ssrc
2236 << " which doesn't exist.";
henrike@webrtc.org1e09a712013-07-26 19:17:59 +00002237 return false;
wu@webrtc.org9dba5252013-08-05 20:36:57 +00002238 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002239
solenberg1ac56142015-10-13 03:58:19 -07002240 // Deregister default channel, if that's the one being destroyed.
2241 if (IsDefaultRecvStream(ssrc)) {
2242 default_recv_ssrc_ = -1;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002243 }
henrike@webrtc.org1e09a712013-07-26 19:17:59 +00002244
solenberg7add0582015-11-20 09:59:34 -08002245 const int channel = it->second->channel();
2246
2247 // Clean up and delete the receive stream+channel.
2248 LOG(LS_INFO) << "Removing audio receive stream " << ssrc
mallinath@webrtc.org67ee6b92014-02-03 16:57:16 +00002249 << " with VoiceEngine channel #" << channel << ".";
Tommif888bb52015-12-12 01:37:01 +01002250 it->second->SetRawAudioSink(nullptr);
solenberg7add0582015-11-20 09:59:34 -08002251 delete it->second;
2252 recv_streams_.erase(it);
2253 return DeleteVoEChannel(channel);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002254}
2255
Taylor Brandstetter1a018dc2016-03-08 12:37:39 -08002256bool WebRtcVoiceMediaChannel::SetLocalSource(uint32_t ssrc,
2257 AudioSource* source) {
solenbergc96df772015-10-21 13:01:53 -07002258 auto it = send_streams_.find(ssrc);
2259 if (it == send_streams_.end()) {
Taylor Brandstetter1a018dc2016-03-08 12:37:39 -08002260 if (source) {
2261 // Return an error if trying to set a valid source with an invalid ssrc.
2262 LOG(LS_ERROR) << "SetLocalSource failed with ssrc " << ssrc;
wu@webrtc.org9dba5252013-08-05 20:36:57 +00002263 return false;
2264 }
2265
2266 // The channel likely has gone away, do nothing.
henrike@webrtc.org1e09a712013-07-26 19:17:59 +00002267 return true;
henrike@webrtc.org1e09a712013-07-26 19:17:59 +00002268 }
2269
Taylor Brandstetter1a018dc2016-03-08 12:37:39 -08002270 if (source) {
2271 it->second->SetSource(source);
solenberg1ac56142015-10-13 03:58:19 -07002272 } else {
Taylor Brandstetter1a018dc2016-03-08 12:37:39 -08002273 it->second->ClearSource();
solenberg1ac56142015-10-13 03:58:19 -07002274 }
henrike@webrtc.org1e09a712013-07-26 19:17:59 +00002275
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002276 return true;
2277}
2278
2279bool WebRtcVoiceMediaChannel::GetActiveStreams(
2280 AudioInfo::StreamList* actives) {
solenberg566ef242015-11-06 15:34:49 -08002281 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002282 actives->clear();
solenberg7add0582015-11-20 09:59:34 -08002283 for (const auto& ch : recv_streams_) {
Fredrik Solenbergaf9fb212015-08-26 10:45:53 +02002284 int level = GetOutputLevel(ch.second->channel());
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002285 if (level > 0) {
Fredrik Solenbergaf9fb212015-08-26 10:45:53 +02002286 actives->push_back(std::make_pair(ch.first, level));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002287 }
2288 }
2289 return true;
2290}
2291
2292int WebRtcVoiceMediaChannel::GetOutputLevel() {
solenberg566ef242015-11-06 15:34:49 -08002293 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
solenberg1ac56142015-10-13 03:58:19 -07002294 int highest = 0;
solenberg7add0582015-11-20 09:59:34 -08002295 for (const auto& ch : recv_streams_) {
solenberg8fb30c32015-10-13 03:06:58 -07002296 highest = std::max(GetOutputLevel(ch.second->channel()), highest);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002297 }
2298 return highest;
2299}
2300
2301int WebRtcVoiceMediaChannel::GetTimeSinceLastTyping() {
2302 int ret;
2303 if (engine()->voe()->processing()->TimeSinceLastTyping(ret) == -1) {
2304 // In case of error, log the info and continue
2305 LOG_RTCERR0(TimeSinceLastTyping);
2306 ret = -1;
2307 } else {
2308 ret *= 1000; // We return ms, webrtc returns seconds.
2309 }
2310 return ret;
2311}
2312
2313void WebRtcVoiceMediaChannel::SetTypingDetectionParameters(int time_window,
2314 int cost_per_typing, int reporting_threshold, int penalty_decay,
2315 int type_event_delay) {
2316 if (engine()->voe()->processing()->SetTypingDetectionParameters(
2317 time_window, cost_per_typing,
2318 reporting_threshold, penalty_decay, type_event_delay) == -1) {
2319 // In case of error, log the info and continue
2320 LOG_RTCERR5(SetTypingDetectionParameters, time_window,
2321 cost_per_typing, reporting_threshold, penalty_decay,
2322 type_event_delay);
2323 }
2324}
2325
solenberg4bac9c52015-10-09 02:32:53 -07002326bool WebRtcVoiceMediaChannel::SetOutputVolume(uint32_t ssrc, double volume) {
solenberg566ef242015-11-06 15:34:49 -08002327 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
solenberg1ac56142015-10-13 03:58:19 -07002328 if (ssrc == 0) {
2329 default_recv_volume_ = volume;
2330 if (default_recv_ssrc_ == -1) {
2331 return true;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002332 }
solenberg1ac56142015-10-13 03:58:19 -07002333 ssrc = static_cast<uint32_t>(default_recv_ssrc_);
2334 }
solenberg217fb662016-06-17 08:30:54 -07002335 const auto it = recv_streams_.find(ssrc);
2336 if (it == recv_streams_.end()) {
2337 LOG(LS_WARNING) << "SetOutputVolume: no recv stream" << ssrc;
solenberg1ac56142015-10-13 03:58:19 -07002338 return false;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002339 }
solenberg217fb662016-06-17 08:30:54 -07002340 it->second->SetOutputVolume(volume);
2341 LOG(LS_INFO) << "SetOutputVolume() to " << volume
2342 << " for recv stream with ssrc " << ssrc;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002343 return true;
2344}
2345
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002346bool WebRtcVoiceMediaChannel::CanInsertDtmf() {
Fredrik Solenbergb5727682015-12-04 15:22:19 +01002347 return dtmf_payload_type_ ? true : false;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002348}
2349
solenberg1d63dd02015-12-02 12:35:09 -08002350bool WebRtcVoiceMediaChannel::InsertDtmf(uint32_t ssrc, int event,
2351 int duration) {
solenberg566ef242015-11-06 15:34:49 -08002352 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
Fredrik Solenbergb5727682015-12-04 15:22:19 +01002353 LOG(LS_INFO) << "WebRtcVoiceMediaChannel::InsertDtmf";
2354 if (!dtmf_payload_type_) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002355 return false;
2356 }
2357
Fredrik Solenbergb5727682015-12-04 15:22:19 +01002358 // Figure out which WebRtcAudioSendStream to send the event on.
2359 auto it = ssrc != 0 ? send_streams_.find(ssrc) : send_streams_.begin();
2360 if (it == send_streams_.end()) {
2361 LOG(LS_WARNING) << "The specified ssrc " << ssrc << " is not in use.";
solenberg1d63dd02015-12-02 12:35:09 -08002362 return false;
2363 }
Fredrik Solenbergb5727682015-12-04 15:22:19 +01002364 if (event < kMinTelephoneEventCode ||
2365 event > kMaxTelephoneEventCode) {
2366 LOG(LS_WARNING) << "DTMF event code " << event << " out of range.";
solenberg1d63dd02015-12-02 12:35:09 -08002367 return false;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002368 }
Fredrik Solenbergb5727682015-12-04 15:22:19 +01002369 if (duration < kMinTelephoneEventDuration ||
2370 duration > kMaxTelephoneEventDuration) {
2371 LOG(LS_WARNING) << "DTMF event duration " << duration << " out of range.";
2372 return false;
2373 }
2374 return it->second->SendTelephoneEvent(*dtmf_payload_type_, event, duration);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002375}
2376
wu@webrtc.orga9890802013-12-13 00:21:03 +00002377void WebRtcVoiceMediaChannel::OnPacketReceived(
jbaucheec21bd2016-03-20 06:15:43 -07002378 rtc::CopyOnWriteBuffer* packet, const rtc::PacketTime& packet_time) {
solenberg566ef242015-11-06 15:34:49 -08002379 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
Fredrik Solenberg4b60c732015-05-07 14:07:48 +02002380
mflodman3d7db262016-04-29 00:57:13 -07002381 const webrtc::PacketTime webrtc_packet_time(packet_time.timestamp,
2382 packet_time.not_before);
2383 webrtc::PacketReceiver::DeliveryStatus delivery_result =
2384 call_->Receiver()->DeliverPacket(webrtc::MediaType::AUDIO,
2385 packet->cdata(), packet->size(),
2386 webrtc_packet_time);
mflodman3d7db262016-04-29 00:57:13 -07002387 if (delivery_result != webrtc::PacketReceiver::DELIVERY_UNKNOWN_SSRC) {
2388 return;
2389 }
2390
2391 // Create a default receive stream for this unsignalled and previously not
2392 // received ssrc. If there already is a default receive stream, delete it.
2393 // See: https://bugs.chromium.org/p/webrtc/issues/detail?id=5208
solenberg1ac56142015-10-13 03:58:19 -07002394 uint32_t ssrc = 0;
jbaucheec21bd2016-03-20 06:15:43 -07002395 if (!GetRtpSsrc(packet->cdata(), packet->size(), &ssrc)) {
solenberg1ac56142015-10-13 03:58:19 -07002396 return;
2397 }
2398
mflodman3d7db262016-04-29 00:57:13 -07002399 if (default_recv_ssrc_ != -1) {
2400 LOG(LS_INFO) << "Removing default receive stream with ssrc "
2401 << default_recv_ssrc_;
2402 RTC_DCHECK_NE(ssrc, default_recv_ssrc_);
2403 RemoveRecvStream(default_recv_ssrc_);
2404 default_recv_ssrc_ = -1;
solenberg1ac56142015-10-13 03:58:19 -07002405 }
2406
mflodman3d7db262016-04-29 00:57:13 -07002407 StreamParams sp;
2408 sp.ssrcs.push_back(ssrc);
2409 LOG(LS_INFO) << "Creating default receive stream for SSRC=" << ssrc << ".";
2410 if (!AddRecvStream(sp)) {
2411 LOG(LS_WARNING) << "Could not create default receive stream.";
2412 return;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002413 }
mflodman3d7db262016-04-29 00:57:13 -07002414 default_recv_ssrc_ = ssrc;
2415 SetOutputVolume(default_recv_ssrc_, default_recv_volume_);
2416 if (default_sink_) {
2417 std::unique_ptr<webrtc::AudioSinkInterface> proxy_sink(
2418 new ProxySink(default_sink_.get()));
2419 SetRawAudioSink(default_recv_ssrc_, std::move(proxy_sink));
2420 }
2421 delivery_result = call_->Receiver()->DeliverPacket(webrtc::MediaType::AUDIO,
2422 packet->cdata(),
2423 packet->size(),
2424 webrtc_packet_time);
2425 RTC_DCHECK_NE(webrtc::PacketReceiver::DELIVERY_UNKNOWN_SSRC, delivery_result);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002426}
2427
wu@webrtc.orga9890802013-12-13 00:21:03 +00002428void WebRtcVoiceMediaChannel::OnRtcpReceived(
jbaucheec21bd2016-03-20 06:15:43 -07002429 rtc::CopyOnWriteBuffer* packet, const rtc::PacketTime& packet_time) {
solenberg566ef242015-11-06 15:34:49 -08002430 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
Fredrik Solenberg4b60c732015-05-07 14:07:48 +02002431
Fredrik Solenberg709ed672015-09-15 12:26:33 +02002432 // Forward packet to Call as well.
2433 const webrtc::PacketTime webrtc_packet_time(packet_time.timestamp,
2434 packet_time.not_before);
2435 call_->Receiver()->DeliverPacket(webrtc::MediaType::AUDIO,
jbaucheec21bd2016-03-20 06:15:43 -07002436 packet->cdata(), packet->size(), webrtc_packet_time);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002437}
2438
Honghai Zhangcc411c02016-03-29 17:27:21 -07002439void WebRtcVoiceMediaChannel::OnNetworkRouteChanged(
2440 const std::string& transport_name,
Honghai Zhang0e533ef2016-04-19 15:41:36 -07002441 const rtc::NetworkRoute& network_route) {
2442 call_->OnNetworkRouteChanged(transport_name, network_route);
Honghai Zhangcc411c02016-03-29 17:27:21 -07002443}
2444
Peter Boström0c4e06b2015-10-07 12:23:21 +02002445bool WebRtcVoiceMediaChannel::MuteStream(uint32_t ssrc, bool muted) {
solenberg566ef242015-11-06 15:34:49 -08002446 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
solenberg94218532016-06-16 10:53:22 -07002447 const auto it = send_streams_.find(ssrc);
2448 if (it == send_streams_.end()) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002449 LOG(LS_WARNING) << "The specified ssrc " << ssrc << " is not in use.";
2450 return false;
2451 }
solenberg94218532016-06-16 10:53:22 -07002452 it->second->SetMuted(muted);
2453
2454 // TODO(solenberg):
buildbot@webrtc.org6b21b712014-07-31 15:08:53 +00002455 // We set the AGC to mute state only when all the channels are muted.
2456 // This implementation is not ideal, instead we should signal the AGC when
2457 // the mic channel is muted/unmuted. We can't do it today because there
2458 // is no good way to know which stream is mapping to the mic channel.
2459 bool all_muted = muted;
solenberg94218532016-06-16 10:53:22 -07002460 for (const auto& kv : send_streams_) {
2461 all_muted = all_muted && kv.second->muted();
buildbot@webrtc.org6b21b712014-07-31 15:08:53 +00002462 }
2463
2464 webrtc::AudioProcessing* ap = engine()->voe()->base()->audio_processing();
solenberg0a617e22015-10-20 15:49:38 -07002465 if (ap) {
buildbot@webrtc.org6b21b712014-07-31 15:08:53 +00002466 ap->set_output_will_be_muted(all_muted);
solenberg0a617e22015-10-20 15:49:38 -07002467 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002468 return true;
2469}
2470
deadbeef80346142016-04-27 14:17:10 -07002471bool WebRtcVoiceMediaChannel::SetMaxSendBitrate(int bps) {
2472 LOG(LS_INFO) << "WebRtcVoiceMediaChannel::SetMaxSendBitrate.";
2473 max_send_bitrate_bps_ = bps;
skvlade0d46372016-04-07 22:59:22 -07002474
2475 for (const auto& kv : send_streams_) {
Taylor Brandstetterdb0cd9e2016-05-16 11:40:30 -07002476 if (!SetChannelSendParameters(kv.second->channel(),
2477 kv.second->rtp_parameters())) {
skvlade0d46372016-04-07 22:59:22 -07002478 return false;
2479 }
2480 }
2481 return true;
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00002482}
2483
Taylor Brandstetterdb0cd9e2016-05-16 11:40:30 -07002484bool WebRtcVoiceMediaChannel::SetChannelSendParameters(
skvlade0d46372016-04-07 22:59:22 -07002485 int channel,
2486 const webrtc::RtpParameters& parameters) {
2487 RTC_CHECK_EQ(1UL, parameters.encodings.size());
Taylor Brandstetter0cd086b2016-04-20 16:23:10 -07002488 // TODO(deadbeef): Handle setting parameters with a list of codecs in a
2489 // different order (which should change the send codec).
deadbeef80346142016-04-27 14:17:10 -07002490 return SetMaxSendBitrate(
2491 channel, MinPositive(max_send_bitrate_bps_,
2492 parameters.encodings[0].max_bitrate_bps));
skvlade0d46372016-04-07 22:59:22 -07002493}
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00002494
deadbeef80346142016-04-27 14:17:10 -07002495bool WebRtcVoiceMediaChannel::SetMaxSendBitrate(int channel, int bps) {
skvlade0d46372016-04-07 22:59:22 -07002496 // Bitrate is auto by default.
2497 // TODO(bemasc): Fix this so that if SetMaxSendBandwidth(50) is followed by
2498 // SetMaxSendBandwith(0), the second call removes the previous limit.
deadbeef80346142016-04-27 14:17:10 -07002499 if (bps <= 0) {
skvlade0d46372016-04-07 22:59:22 -07002500 return true;
deadbeef80346142016-04-27 14:17:10 -07002501 }
wu@webrtc.org1d1ffc92013-10-16 18:12:02 +00002502
solenberg72e29d22016-03-08 06:35:16 -08002503 if (!HasSendCodec()) {
wu@webrtc.org1d1ffc92013-10-16 18:12:02 +00002504 LOG(LS_INFO) << "The send codec has not been set up yet. "
minyue@webrtc.org26236952014-10-29 02:27:08 +00002505 << "The send bitrate setting will be applied later.";
wu@webrtc.org1d1ffc92013-10-16 18:12:02 +00002506 return true;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002507 }
2508
solenberg72e29d22016-03-08 06:35:16 -08002509 webrtc::CodecInst codec = send_codec_spec_.codec_inst;
solenberg26c8c912015-11-27 04:00:25 -08002510 bool is_multi_rate = WebRtcVoiceCodecs::IsCodecMultiRate(codec);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002511
2512 if (is_multi_rate) {
2513 // If codec is multi-rate then just set the bitrate.
deadbeef80346142016-04-27 14:17:10 -07002514 int max_bitrate_bps = WebRtcVoiceCodecs::MaxBitrateBps(codec);
2515 codec.rate = std::min(bps, max_bitrate_bps);
2516 LOG(LS_INFO) << "Setting codec " << codec.plname << " to bitrate " << bps
2517 << " bps.";
skvlade0d46372016-04-07 22:59:22 -07002518 if (!SetSendCodec(channel, codec)) {
deadbeef80346142016-04-27 14:17:10 -07002519 LOG(LS_ERROR) << "Failed to set codec " << codec.plname << " to bitrate "
2520 << bps << " bps.";
skvlade0d46372016-04-07 22:59:22 -07002521 return false;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002522 }
2523 return true;
2524 } else {
2525 // If codec is not multi-rate and |bps| is less than the fixed bitrate
2526 // then fail. If codec is not multi-rate and |bps| exceeds or equal the
2527 // fixed bitrate then ignore.
2528 if (bps < codec.rate) {
deadbeef80346142016-04-27 14:17:10 -07002529 LOG(LS_ERROR) << "Failed to set codec " << codec.plname << " to bitrate "
2530 << bps << " bps"
2531 << ", requires at least " << codec.rate << " bps.";
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002532 return false;
2533 }
2534 return true;
2535 }
2536}
2537
skvlad7a43d252016-03-22 15:32:27 -07002538void WebRtcVoiceMediaChannel::OnReadyToSend(bool ready) {
2539 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
2540 LOG(LS_VERBOSE) << "OnReadyToSend: " << (ready ? "Ready." : "Not ready.");
2541 call_->SignalChannelNetworkState(
2542 webrtc::MediaType::AUDIO,
2543 ready ? webrtc::kNetworkUp : webrtc::kNetworkDown);
2544}
2545
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002546bool WebRtcVoiceMediaChannel::GetStats(VoiceMediaInfo* info) {
Peter Boströmca8b4042016-03-08 14:24:13 -08002547 TRACE_EVENT0("webrtc", "WebRtcVoiceMediaChannel::GetStats");
solenberg566ef242015-11-06 15:34:49 -08002548 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
solenberg85a04962015-10-27 03:35:21 -07002549 RTC_DCHECK(info);
solenbergd97ec302015-10-07 01:40:33 -07002550
solenberg85a04962015-10-27 03:35:21 -07002551 // Get SSRC and stats for each sender.
2552 RTC_DCHECK(info->senders.size() == 0);
2553 for (const auto& stream : send_streams_) {
2554 webrtc::AudioSendStream::Stats stats = stream.second->GetStats();
wu@webrtc.org9dba5252013-08-05 20:36:57 +00002555 VoiceSenderInfo sinfo;
solenberg85a04962015-10-27 03:35:21 -07002556 sinfo.add_ssrc(stats.local_ssrc);
2557 sinfo.bytes_sent = stats.bytes_sent;
2558 sinfo.packets_sent = stats.packets_sent;
2559 sinfo.packets_lost = stats.packets_lost;
2560 sinfo.fraction_lost = stats.fraction_lost;
2561 sinfo.codec_name = stats.codec_name;
2562 sinfo.ext_seqnum = stats.ext_seqnum;
2563 sinfo.jitter_ms = stats.jitter_ms;
2564 sinfo.rtt_ms = stats.rtt_ms;
2565 sinfo.audio_level = stats.audio_level;
2566 sinfo.aec_quality_min = stats.aec_quality_min;
2567 sinfo.echo_delay_median_ms = stats.echo_delay_median_ms;
2568 sinfo.echo_delay_std_ms = stats.echo_delay_std_ms;
2569 sinfo.echo_return_loss = stats.echo_return_loss;
2570 sinfo.echo_return_loss_enhancement = stats.echo_return_loss_enhancement;
Taylor Brandstetter1a018dc2016-03-08 12:37:39 -08002571 sinfo.typing_noise_detected = (send_ ? stats.typing_noise_detected : false);
wu@webrtc.org9dba5252013-08-05 20:36:57 +00002572 info->senders.push_back(sinfo);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002573 }
2574
solenberg85a04962015-10-27 03:35:21 -07002575 // Get SSRC and stats for each receiver.
2576 RTC_DCHECK(info->receivers.size() == 0);
solenberg7add0582015-11-20 09:59:34 -08002577 for (const auto& stream : recv_streams_) {
Fredrik Solenberg4f4ec0a2015-10-22 10:49:27 +02002578 webrtc::AudioReceiveStream::Stats stats = stream.second->GetStats();
2579 VoiceReceiverInfo rinfo;
2580 rinfo.add_ssrc(stats.remote_ssrc);
2581 rinfo.bytes_rcvd = stats.bytes_rcvd;
2582 rinfo.packets_rcvd = stats.packets_rcvd;
2583 rinfo.packets_lost = stats.packets_lost;
2584 rinfo.fraction_lost = stats.fraction_lost;
2585 rinfo.codec_name = stats.codec_name;
2586 rinfo.ext_seqnum = stats.ext_seqnum;
2587 rinfo.jitter_ms = stats.jitter_ms;
2588 rinfo.jitter_buffer_ms = stats.jitter_buffer_ms;
2589 rinfo.jitter_buffer_preferred_ms = stats.jitter_buffer_preferred_ms;
2590 rinfo.delay_estimate_ms = stats.delay_estimate_ms;
2591 rinfo.audio_level = stats.audio_level;
2592 rinfo.expand_rate = stats.expand_rate;
2593 rinfo.speech_expand_rate = stats.speech_expand_rate;
2594 rinfo.secondary_decoded_rate = stats.secondary_decoded_rate;
2595 rinfo.accelerate_rate = stats.accelerate_rate;
2596 rinfo.preemptive_expand_rate = stats.preemptive_expand_rate;
2597 rinfo.decoding_calls_to_silence_generator =
2598 stats.decoding_calls_to_silence_generator;
2599 rinfo.decoding_calls_to_neteq = stats.decoding_calls_to_neteq;
2600 rinfo.decoding_normal = stats.decoding_normal;
2601 rinfo.decoding_plc = stats.decoding_plc;
2602 rinfo.decoding_cng = stats.decoding_cng;
2603 rinfo.decoding_plc_cng = stats.decoding_plc_cng;
2604 rinfo.capture_start_ntp_time_ms = stats.capture_start_ntp_time_ms;
2605 info->receivers.push_back(rinfo);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002606 }
2607
2608 return true;
2609}
2610
Tommif888bb52015-12-12 01:37:01 +01002611void WebRtcVoiceMediaChannel::SetRawAudioSink(
2612 uint32_t ssrc,
kwiberg686a8ef2016-02-26 03:00:35 -08002613 std::unique_ptr<webrtc::AudioSinkInterface> sink) {
Tommif888bb52015-12-12 01:37:01 +01002614 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
deadbeef884f5852016-01-15 09:20:04 -08002615 LOG(LS_VERBOSE) << "WebRtcVoiceMediaChannel::SetRawAudioSink: ssrc:" << ssrc
2616 << " " << (sink ? "(ptr)" : "NULL");
2617 if (ssrc == 0) {
2618 if (default_recv_ssrc_ != -1) {
kwiberg686a8ef2016-02-26 03:00:35 -08002619 std::unique_ptr<webrtc::AudioSinkInterface> proxy_sink(
deadbeef884f5852016-01-15 09:20:04 -08002620 sink ? new ProxySink(sink.get()) : nullptr);
2621 SetRawAudioSink(default_recv_ssrc_, std::move(proxy_sink));
2622 }
2623 default_sink_ = std::move(sink);
2624 return;
2625 }
Tommif888bb52015-12-12 01:37:01 +01002626 const auto it = recv_streams_.find(ssrc);
2627 if (it == recv_streams_.end()) {
2628 LOG(LS_WARNING) << "SetRawAudioSink: no recv stream" << ssrc;
2629 return;
2630 }
deadbeef2d110be2016-01-13 12:00:26 -08002631 it->second->SetRawAudioSink(std::move(sink));
Tommif888bb52015-12-12 01:37:01 +01002632}
2633
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002634int WebRtcVoiceMediaChannel::GetOutputLevel(int channel) {
solenbergd97ec302015-10-07 01:40:33 -07002635 unsigned int ulevel = 0;
2636 int ret = engine()->voe()->volume()->GetSpeechOutputLevel(channel, ulevel);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002637 return (ret == 0) ? static_cast<int>(ulevel) : -1;
2638}
2639
Peter Boström0c4e06b2015-10-07 12:23:21 +02002640int WebRtcVoiceMediaChannel::GetReceiveChannelId(uint32_t ssrc) const {
solenberg566ef242015-11-06 15:34:49 -08002641 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
solenberg7add0582015-11-20 09:59:34 -08002642 const auto it = recv_streams_.find(ssrc);
2643 if (it != recv_streams_.end()) {
mallinath@webrtc.org67ee6b92014-02-03 16:57:16 +00002644 return it->second->channel();
solenberg8fb30c32015-10-13 03:06:58 -07002645 }
solenberg1ac56142015-10-13 03:58:19 -07002646 return -1;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002647}
2648
Peter Boström0c4e06b2015-10-07 12:23:21 +02002649int WebRtcVoiceMediaChannel::GetSendChannelId(uint32_t ssrc) const {
solenberg566ef242015-11-06 15:34:49 -08002650 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
solenbergc96df772015-10-21 13:01:53 -07002651 const auto it = send_streams_.find(ssrc);
2652 if (it != send_streams_.end()) {
mallinath@webrtc.org67ee6b92014-02-03 16:57:16 +00002653 return it->second->channel();
solenberg8fb30c32015-10-13 03:06:58 -07002654 }
wu@webrtc.org9dba5252013-08-05 20:36:57 +00002655 return -1;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002656}
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002657} // namespace cricket
2658
2659#endif // HAVE_WEBRTC_VOICE