blob: 59a2870cf8d7e48737c73593a5fcdb8693afaf15 [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>
ossu95eb1ba2016-07-13 06:05:27 -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"
ossu95eb1ba2016-07-13 06:05:27 -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.
ossu95eb1ba2016-07-13 06:05:27 -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);
ossu95eb1ba2016-07-13 06:05:27 -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.
ossu95eb1ba2016-07-13 06:05:27 -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)
buildbot@webrtc.org13d67762014-05-02 17:33:29 +0000618 // On iOS, VPIO provides built-in EC 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);
henrika86d907c2015-09-07 16:09:50 +0200621 LOG(LS_INFO) << "Always disable AEC and AGC on iOS. Use built-in instead.";
buildbot@webrtc.org13d67762014-05-02 17:33:29 +0000622#elif defined(ANDROID)
623 ec_mode = webrtc::kEcAecm;
624#endif
625
kjellanderfcfc8042016-01-14 11:01:09 -0800626#if defined(WEBRTC_IOS) || defined(ANDROID)
buildbot@webrtc.org13d67762014-05-02 17:33:29 +0000627 // Set the AGC mode for iOS as well despite disabling it above, to avoid
628 // unsupported configuration errors from webrtc.
629 agc_mode = webrtc::kAgcFixedDigital;
Karl Wibergbe579832015-11-10 22:34:18 +0100630 options.typing_detection = rtc::Optional<bool>(false);
631 options.experimental_agc = rtc::Optional<bool>(false);
632 options.extended_filter_aec = rtc::Optional<bool>(false);
633 options.experimental_ns = rtc::Optional<bool>(false);
buildbot@webrtc.org13d67762014-05-02 17:33:29 +0000634#endif
635
Bjorn Volckerbf395c12015-03-25 22:45:56 +0100636 // Delay Agnostic AEC automatically turns on EC if not set except on iOS
637 // where the feature is not supported.
638 bool use_delay_agnostic_aec = false;
kjellanderfcfc8042016-01-14 11:01:09 -0800639#if !defined(WEBRTC_IOS)
kwiberg102c6a62015-10-30 02:47:38 -0700640 if (options.delay_agnostic_aec) {
641 use_delay_agnostic_aec = *options.delay_agnostic_aec;
Bjorn Volckerbf395c12015-03-25 22:45:56 +0100642 if (use_delay_agnostic_aec) {
Karl Wibergbe579832015-11-10 22:34:18 +0100643 options.echo_cancellation = rtc::Optional<bool>(true);
644 options.extended_filter_aec = rtc::Optional<bool>(true);
Bjorn Volckerbf395c12015-03-25 22:45:56 +0100645 ec_mode = webrtc::kEcConference;
646 }
647 }
648#endif
649
buildbot@webrtc.org13d67762014-05-02 17:33:29 +0000650 webrtc::VoEAudioProcessing* voep = voe_wrapper_->processing();
651
kwiberg102c6a62015-10-30 02:47:38 -0700652 if (options.echo_cancellation) {
henrika@webrtc.orga954c072014-12-09 16:22:09 +0000653 // Check if platform supports built-in EC. Currently only supported on
654 // Android and in combination with Java based audio layer.
655 // TODO(henrika): investigate possibility to support built-in EC also
656 // in combination with Open SL ES audio.
solenberg5b5129a2016-04-08 05:35:48 -0700657 const bool built_in_aec = adm()->BuiltInAECIsAvailable();
Bjorn Volcker73f72102015-06-03 14:50:15 +0200658 if (built_in_aec) {
Bjorn Volckerccfc9392015-05-07 07:43:17 +0200659 // Built-in EC exists on this device and use_delay_agnostic_aec is not
660 // overriding it. Enable/Disable it according to the echo_cancellation
661 // audio option.
Bjorn Volcker73f72102015-06-03 14:50:15 +0200662 const bool enable_built_in_aec =
kwiberg102c6a62015-10-30 02:47:38 -0700663 *options.echo_cancellation && !use_delay_agnostic_aec;
solenberg5b5129a2016-04-08 05:35:48 -0700664 if (adm()->EnableBuiltInAEC(enable_built_in_aec) == 0 &&
Bjorn Volcker73f72102015-06-03 14:50:15 +0200665 enable_built_in_aec) {
Bjorn Volckerbf395c12015-03-25 22:45:56 +0100666 // Disable internal software EC if built-in EC is enabled,
henrika@webrtc.orga954c072014-12-09 16:22:09 +0000667 // i.e., replace the software EC with the built-in EC.
Karl Wibergbe579832015-11-10 22:34:18 +0100668 options.echo_cancellation = rtc::Optional<bool>(false);
henrika@webrtc.orga954c072014-12-09 16:22:09 +0000669 LOG(LS_INFO) << "Disabling EC since built-in EC will be used instead";
670 }
671 }
kwiberg102c6a62015-10-30 02:47:38 -0700672 if (voep->SetEcStatus(*options.echo_cancellation, ec_mode) == -1) {
673 LOG_RTCERR2(SetEcStatus, *options.echo_cancellation, ec_mode);
buildbot@webrtc.org13d67762014-05-02 17:33:29 +0000674 return false;
675 } else {
kwiberg102c6a62015-10-30 02:47:38 -0700676 LOG(LS_INFO) << "Echo control set to " << *options.echo_cancellation
henrika86d907c2015-09-07 16:09:50 +0200677 << " with mode " << ec_mode;
buildbot@webrtc.org13d67762014-05-02 17:33:29 +0000678 }
679#if !defined(ANDROID)
680 // TODO(ajm): Remove the error return on Android from webrtc.
kwiberg102c6a62015-10-30 02:47:38 -0700681 if (voep->SetEcMetricsStatus(*options.echo_cancellation) == -1) {
682 LOG_RTCERR1(SetEcMetricsStatus, *options.echo_cancellation);
buildbot@webrtc.org13d67762014-05-02 17:33:29 +0000683 return false;
684 }
685#endif
686 if (ec_mode == webrtc::kEcAecm) {
kwiberg102c6a62015-10-30 02:47:38 -0700687 bool cn = options.aecm_generate_comfort_noise.value_or(false);
688 if (voep->SetAecmMode(aecm_mode, cn) != 0) {
689 LOG_RTCERR2(SetAecmMode, aecm_mode, cn);
buildbot@webrtc.org13d67762014-05-02 17:33:29 +0000690 return false;
691 }
692 }
693 }
694
peaha3333bf2016-06-30 00:02:34 -0700695 // Use optional to avoid uneccessary calls to BuiltInAGCIsAvailable while
696 // complying with the unittest requirements of only 1 call per test.
697 rtc::Optional<bool> built_in_agc_avaliable;
698 if (options.level_control) {
699 if (!built_in_agc_avaliable) {
700 built_in_agc_avaliable =
701 rtc::Optional<bool>(adm()->BuiltInAGCIsAvailable());
702 }
703 RTC_DCHECK(built_in_agc_avaliable);
704 if (*built_in_agc_avaliable) {
705 // Disable internal software level control if built-in AGC is enabled,
706 // i.e., replace the software AGC with the built-in AGC.
707 options.level_control = rtc::Optional<bool>(false);
708 }
709 }
710
kwiberg102c6a62015-10-30 02:47:38 -0700711 if (options.auto_gain_control) {
peaha3333bf2016-06-30 00:02:34 -0700712 if (!built_in_agc_avaliable) {
713 built_in_agc_avaliable =
714 rtc::Optional<bool>(adm()->BuiltInAGCIsAvailable());
715 }
716 RTC_DCHECK(built_in_agc_avaliable);
717 if (*built_in_agc_avaliable) {
solenberg5b5129a2016-04-08 05:35:48 -0700718 if (adm()->EnableBuiltInAGC(*options.auto_gain_control) == 0 &&
kwiberg102c6a62015-10-30 02:47:38 -0700719 *options.auto_gain_control) {
henrikac14f5ff2015-09-23 14:08:33 +0200720 // Disable internal software AGC if built-in AGC is enabled,
721 // i.e., replace the software AGC with the built-in AGC.
Karl Wibergbe579832015-11-10 22:34:18 +0100722 options.auto_gain_control = rtc::Optional<bool>(false);
henrikac14f5ff2015-09-23 14:08:33 +0200723 LOG(LS_INFO) << "Disabling AGC since built-in AGC will be used instead";
724 }
725 }
kwiberg102c6a62015-10-30 02:47:38 -0700726 if (voep->SetAgcStatus(*options.auto_gain_control, agc_mode) == -1) {
727 LOG_RTCERR2(SetAgcStatus, *options.auto_gain_control, agc_mode);
buildbot@webrtc.org13d67762014-05-02 17:33:29 +0000728 return false;
729 } else {
kwiberg102c6a62015-10-30 02:47:38 -0700730 LOG(LS_INFO) << "Auto gain set to " << *options.auto_gain_control
731 << " with mode " << agc_mode;
buildbot@webrtc.org13d67762014-05-02 17:33:29 +0000732 }
733 }
734
kwiberg102c6a62015-10-30 02:47:38 -0700735 if (options.tx_agc_target_dbov || options.tx_agc_digital_compression_gain ||
736 options.tx_agc_limiter) {
buildbot@webrtc.org13d67762014-05-02 17:33:29 +0000737 // Override default_agc_config_. Generally, an unset option means "leave
738 // the VoE bits alone" in this function, so we want whatever is set to be
739 // stored as the new "default". If we didn't, then setting e.g.
740 // tx_agc_target_dbov would reset digital compression gain and limiter
741 // settings.
742 // Also, if we don't update default_agc_config_, then adjust_agc_delta
743 // would be an offset from the original values, and not whatever was set
744 // explicitly.
kwiberg102c6a62015-10-30 02:47:38 -0700745 default_agc_config_.targetLeveldBOv = options.tx_agc_target_dbov.value_or(
746 default_agc_config_.targetLeveldBOv);
buildbot@webrtc.org13d67762014-05-02 17:33:29 +0000747 default_agc_config_.digitalCompressionGaindB =
kwiberg102c6a62015-10-30 02:47:38 -0700748 options.tx_agc_digital_compression_gain.value_or(
buildbot@webrtc.org13d67762014-05-02 17:33:29 +0000749 default_agc_config_.digitalCompressionGaindB);
750 default_agc_config_.limiterEnable =
kwiberg102c6a62015-10-30 02:47:38 -0700751 options.tx_agc_limiter.value_or(default_agc_config_.limiterEnable);
buildbot@webrtc.org13d67762014-05-02 17:33:29 +0000752 if (voe_wrapper_->processing()->SetAgcConfig(default_agc_config_) == -1) {
753 LOG_RTCERR3(SetAgcConfig,
754 default_agc_config_.targetLeveldBOv,
755 default_agc_config_.digitalCompressionGaindB,
756 default_agc_config_.limiterEnable);
757 return false;
758 }
759 }
760
Alejandro Luebsc9b0c262016-05-16 15:32:38 -0700761 if (options.intelligibility_enhancer) {
762 intelligibility_enhancer_ = options.intelligibility_enhancer;
763 }
764 if (intelligibility_enhancer_ && *intelligibility_enhancer_) {
765 LOG(LS_INFO) << "Enabling NS when Intelligibility Enhancer is active.";
766 options.noise_suppression = intelligibility_enhancer_;
767 }
768
kwiberg102c6a62015-10-30 02:47:38 -0700769 if (options.noise_suppression) {
Alejandro Luebsc9b0c262016-05-16 15:32:38 -0700770 if (adm()->BuiltInNSIsAvailable()) {
771 bool builtin_ns =
772 *options.noise_suppression &&
773 !(intelligibility_enhancer_ && *intelligibility_enhancer_);
774 if (adm()->EnableBuiltInNS(builtin_ns) == 0 && builtin_ns) {
henrikac14f5ff2015-09-23 14:08:33 +0200775 // Disable internal software NS if built-in NS is enabled,
776 // i.e., replace the software NS with the built-in NS.
Karl Wibergbe579832015-11-10 22:34:18 +0100777 options.noise_suppression = rtc::Optional<bool>(false);
henrikac14f5ff2015-09-23 14:08:33 +0200778 LOG(LS_INFO) << "Disabling NS since built-in NS will be used instead";
779 }
780 }
kwiberg102c6a62015-10-30 02:47:38 -0700781 if (voep->SetNsStatus(*options.noise_suppression, ns_mode) == -1) {
782 LOG_RTCERR2(SetNsStatus, *options.noise_suppression, ns_mode);
buildbot@webrtc.org13d67762014-05-02 17:33:29 +0000783 return false;
784 } else {
kwiberg102c6a62015-10-30 02:47:38 -0700785 LOG(LS_INFO) << "Noise suppression set to " << *options.noise_suppression
henrikac14f5ff2015-09-23 14:08:33 +0200786 << " with mode " << ns_mode;
buildbot@webrtc.org13d67762014-05-02 17:33:29 +0000787 }
788 }
789
kwiberg102c6a62015-10-30 02:47:38 -0700790 if (options.highpass_filter) {
791 LOG(LS_INFO) << "High pass filter enabled? " << *options.highpass_filter;
792 if (voep->EnableHighPassFilter(*options.highpass_filter) == -1) {
793 LOG_RTCERR1(SetHighpassFilterStatus, *options.highpass_filter);
buildbot@webrtc.org13d67762014-05-02 17:33:29 +0000794 return false;
795 }
796 }
797
kwiberg102c6a62015-10-30 02:47:38 -0700798 if (options.stereo_swapping) {
799 LOG(LS_INFO) << "Stereo swapping enabled? " << *options.stereo_swapping;
800 voep->EnableStereoChannelSwapping(*options.stereo_swapping);
801 if (voep->IsStereoChannelSwappingEnabled() != *options.stereo_swapping) {
802 LOG_RTCERR1(EnableStereoChannelSwapping, *options.stereo_swapping);
buildbot@webrtc.org13d67762014-05-02 17:33:29 +0000803 return false;
804 }
805 }
806
kwiberg102c6a62015-10-30 02:47:38 -0700807 if (options.audio_jitter_buffer_max_packets) {
808 LOG(LS_INFO) << "NetEq capacity is "
809 << *options.audio_jitter_buffer_max_packets;
Henrik Lundin64dad832015-05-11 12:44:23 +0200810 voe_config_.Set<webrtc::NetEqCapacityConfig>(
kwiberg102c6a62015-10-30 02:47:38 -0700811 new webrtc::NetEqCapacityConfig(
812 *options.audio_jitter_buffer_max_packets));
Henrik Lundin64dad832015-05-11 12:44:23 +0200813 }
814
kwiberg102c6a62015-10-30 02:47:38 -0700815 if (options.audio_jitter_buffer_fast_accelerate) {
816 LOG(LS_INFO) << "NetEq fast mode? "
817 << *options.audio_jitter_buffer_fast_accelerate;
Henrik Lundin5263b3c2015-06-01 10:29:41 +0200818 voe_config_.Set<webrtc::NetEqFastAccelerate>(
kwiberg102c6a62015-10-30 02:47:38 -0700819 new webrtc::NetEqFastAccelerate(
820 *options.audio_jitter_buffer_fast_accelerate));
Henrik Lundin5263b3c2015-06-01 10:29:41 +0200821 }
822
kwiberg102c6a62015-10-30 02:47:38 -0700823 if (options.typing_detection) {
824 LOG(LS_INFO) << "Typing detection is enabled? "
825 << *options.typing_detection;
826 if (voep->SetTypingDetectionStatus(*options.typing_detection) == -1) {
buildbot@webrtc.org13d67762014-05-02 17:33:29 +0000827 // In case of error, log the info and continue
kwiberg102c6a62015-10-30 02:47:38 -0700828 LOG_RTCERR1(SetTypingDetectionStatus, *options.typing_detection);
buildbot@webrtc.org13d67762014-05-02 17:33:29 +0000829 }
830 }
831
kwiberg102c6a62015-10-30 02:47:38 -0700832 if (options.adjust_agc_delta) {
833 LOG(LS_INFO) << "Adjust agc delta is " << *options.adjust_agc_delta;
834 if (!AdjustAgcLevel(*options.adjust_agc_delta)) {
buildbot@webrtc.org13d67762014-05-02 17:33:29 +0000835 return false;
836 }
837 }
838
buildbot@webrtc.org1f8a2372014-08-28 10:52:44 +0000839 webrtc::Config config;
840
kwiberg102c6a62015-10-30 02:47:38 -0700841 if (options.delay_agnostic_aec)
842 delay_agnostic_aec_ = options.delay_agnostic_aec;
843 if (delay_agnostic_aec_) {
844 LOG(LS_INFO) << "Delay agnostic aec is enabled? " << *delay_agnostic_aec_;
henrik.lundin0f133b92015-07-02 00:17:55 -0700845 config.Set<webrtc::DelayAgnostic>(
kwiberg102c6a62015-10-30 02:47:38 -0700846 new webrtc::DelayAgnostic(*delay_agnostic_aec_));
Bjorn Volckerbf395c12015-03-25 22:45:56 +0100847 }
848
kwiberg102c6a62015-10-30 02:47:38 -0700849 if (options.extended_filter_aec) {
850 extended_filter_aec_ = options.extended_filter_aec;
851 }
852 if (extended_filter_aec_) {
853 LOG(LS_INFO) << "Extended filter aec is enabled? " << *extended_filter_aec_;
Henrik Lundin441f6342015-06-09 16:03:13 +0200854 config.Set<webrtc::ExtendedFilter>(
kwiberg102c6a62015-10-30 02:47:38 -0700855 new webrtc::ExtendedFilter(*extended_filter_aec_));
buildbot@webrtc.org1f8a2372014-08-28 10:52:44 +0000856 }
857
kwiberg102c6a62015-10-30 02:47:38 -0700858 if (options.experimental_ns) {
859 experimental_ns_ = options.experimental_ns;
860 }
861 if (experimental_ns_) {
862 LOG(LS_INFO) << "Experimental ns is enabled? " << *experimental_ns_;
buildbot@webrtc.org1f8a2372014-08-28 10:52:44 +0000863 config.Set<webrtc::ExperimentalNs>(
kwiberg102c6a62015-10-30 02:47:38 -0700864 new webrtc::ExperimentalNs(*experimental_ns_));
buildbot@webrtc.org1f8a2372014-08-28 10:52:44 +0000865 }
buildbot@webrtc.org1f8a2372014-08-28 10:52:44 +0000866
Alejandro Luebsc9b0c262016-05-16 15:32:38 -0700867 if (intelligibility_enhancer_) {
868 LOG(LS_INFO) << "Intelligibility Enhancer is enabled? "
869 << *intelligibility_enhancer_;
870 config.Set<webrtc::Intelligibility>(
871 new webrtc::Intelligibility(*intelligibility_enhancer_));
872 }
873
peaha3333bf2016-06-30 00:02:34 -0700874 if (options.level_control) {
875 level_control_ = options.level_control;
876 }
877
878 LOG(LS_INFO) << "Level control: "
879 << (!!level_control_ ? *level_control_ : -1);
880 if (level_control_) {
881 config.Set<webrtc::LevelControl>(new webrtc::LevelControl(*level_control_));
882 }
883
buildbot@webrtc.org1f8a2372014-08-28 10:52:44 +0000884 // We check audioproc for the benefit of tests, since FakeWebRtcVoiceEngine
885 // returns NULL on audio_processing().
886 webrtc::AudioProcessing* audioproc = voe_wrapper_->base()->audio_processing();
887 if (audioproc) {
888 audioproc->SetExtraOptions(config);
889 }
890
kwiberg102c6a62015-10-30 02:47:38 -0700891 if (options.recording_sample_rate) {
892 LOG(LS_INFO) << "Recording sample rate is "
893 << *options.recording_sample_rate;
solenberg5b5129a2016-04-08 05:35:48 -0700894 if (adm()->SetRecordingSampleRate(*options.recording_sample_rate)) {
kwiberg102c6a62015-10-30 02:47:38 -0700895 LOG_RTCERR1(SetRecordingSampleRate, *options.recording_sample_rate);
buildbot@webrtc.org13d67762014-05-02 17:33:29 +0000896 }
897 }
898
kwiberg102c6a62015-10-30 02:47:38 -0700899 if (options.playout_sample_rate) {
900 LOG(LS_INFO) << "Playout sample rate is " << *options.playout_sample_rate;
solenberg5b5129a2016-04-08 05:35:48 -0700901 if (adm()->SetPlayoutSampleRate(*options.playout_sample_rate)) {
kwiberg102c6a62015-10-30 02:47:38 -0700902 LOG_RTCERR1(SetPlayoutSampleRate, *options.playout_sample_rate);
buildbot@webrtc.org13d67762014-05-02 17:33:29 +0000903 }
904 }
905
906 return true;
907}
908
solenberg246b8172015-12-08 09:50:23 -0800909void WebRtcVoiceEngine::SetDefaultDevices() {
solenberg566ef242015-11-06 15:34:49 -0800910 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
kjellanderfcfc8042016-01-14 11:01:09 -0800911#if !defined(WEBRTC_IOS)
solenberg246b8172015-12-08 09:50:23 -0800912 int in_id = kDefaultAudioDeviceId;
913 int out_id = kDefaultAudioDeviceId;
914 LOG(LS_INFO) << "Setting microphone to (id=" << in_id
915 << ") and speaker to (id=" << out_id << ")";
buildbot@webrtc.org13d67762014-05-02 17:33:29 +0000916
solenbergc1a1b352015-09-22 13:31:20 -0700917 bool ret = true;
solenberg246b8172015-12-08 09:50:23 -0800918 if (voe_wrapper_->hw()->SetRecordingDevice(in_id) == -1) {
919 LOG_RTCERR1(SetRecordingDevice, in_id);
buildbot@webrtc.org13d67762014-05-02 17:33:29 +0000920 ret = false;
921 }
solenberg246b8172015-12-08 09:50:23 -0800922 webrtc::AudioProcessing* ap = voe()->base()->audio_processing();
923 if (ap) {
924 ap->Initialize();
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000925 }
926
solenberg246b8172015-12-08 09:50:23 -0800927 if (voe_wrapper_->hw()->SetPlayoutDevice(out_id) == -1) {
928 LOG_RTCERR1(SetPlayoutDevice, out_id);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000929 ret = false;
930 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000931
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000932 if (ret) {
solenberg246b8172015-12-08 09:50:23 -0800933 LOG(LS_INFO) << "Set microphone to (id=" << in_id
934 << ") and speaker to (id=" << out_id << ")";
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000935 }
kjellanderfcfc8042016-01-14 11:01:09 -0800936#endif // !WEBRTC_IOS
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000937}
938
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000939int WebRtcVoiceEngine::GetInputLevel() {
solenberg566ef242015-11-06 15:34:49 -0800940 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000941 unsigned int ulevel;
942 return (voe_wrapper_->volume()->GetSpeechInputLevel(ulevel) != -1) ?
943 static_cast<int>(ulevel) : -1;
944}
945
ossudedfd282016-06-14 07:12:39 -0700946const std::vector<AudioCodec>& WebRtcVoiceEngine::send_codecs() const {
947 RTC_DCHECK(signal_thread_checker_.CalledOnValidThread());
ossu95eb1ba2016-07-13 06:05:27 -0700948 return send_codecs_;
ossudedfd282016-06-14 07:12:39 -0700949}
950
951const std::vector<AudioCodec>& WebRtcVoiceEngine::recv_codecs() const {
solenberg566ef242015-11-06 15:34:49 -0800952 RTC_DCHECK(signal_thread_checker_.CalledOnValidThread());
ossu95eb1ba2016-07-13 06:05:27 -0700953 return recv_codecs_;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000954}
955
Stefan Holmer9d69c3f2015-12-07 10:45:43 +0100956RtpCapabilities WebRtcVoiceEngine::GetCapabilities() const {
solenberg566ef242015-11-06 15:34:49 -0800957 RTC_DCHECK(signal_thread_checker_.CalledOnValidThread());
Stefan Holmer9d69c3f2015-12-07 10:45:43 +0100958 RtpCapabilities capabilities;
Stefan Holmer9d69c3f2015-12-07 10:45:43 +0100959 capabilities.header_extensions.push_back(
isheriff6f8d6862016-05-26 11:24:55 -0700960 webrtc::RtpExtension(webrtc::RtpExtension::kAudioLevelUri,
961 webrtc::RtpExtension::kAudioLevelDefaultId));
962 capabilities.header_extensions.push_back(
963 webrtc::RtpExtension(webrtc::RtpExtension::kAbsSendTimeUri,
964 webrtc::RtpExtension::kAbsSendTimeDefaultId));
stefanba4c0e42016-02-04 04:12:24 -0800965 if (webrtc::field_trial::FindFullName("WebRTC-Audio-SendSideBwe") ==
966 "Enabled") {
isheriff6f8d6862016-05-26 11:24:55 -0700967 capabilities.header_extensions.push_back(webrtc::RtpExtension(
968 webrtc::RtpExtension::kTransportSequenceNumberUri,
969 webrtc::RtpExtension::kTransportSequenceNumberDefaultId));
stefanba4c0e42016-02-04 04:12:24 -0800970 }
Stefan Holmer9d69c3f2015-12-07 10:45:43 +0100971 return capabilities;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000972}
973
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000974int WebRtcVoiceEngine::GetLastEngineError() {
solenberg566ef242015-11-06 15:34:49 -0800975 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000976 return voe_wrapper_->error();
977}
978
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000979void WebRtcVoiceEngine::Print(webrtc::TraceLevel level, const char* trace,
980 int length) {
solenberg566ef242015-11-06 15:34:49 -0800981 // Note: This callback can happen on any thread!
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000982 rtc::LoggingSeverity sev = rtc::LS_VERBOSE;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000983 if (level == webrtc::kTraceError || level == webrtc::kTraceCritical)
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000984 sev = rtc::LS_ERROR;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000985 else if (level == webrtc::kTraceWarning)
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000986 sev = rtc::LS_WARNING;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000987 else if (level == webrtc::kTraceStateInfo || level == webrtc::kTraceInfo)
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000988 sev = rtc::LS_INFO;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000989 else if (level == webrtc::kTraceTerseInfo)
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000990 sev = rtc::LS_INFO;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000991
solenberg72e29d22016-03-08 06:35:16 -0800992 // Skip past boilerplate prefix text.
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000993 if (length < 72) {
994 std::string msg(trace, length);
995 LOG(LS_ERROR) << "Malformed webrtc log message: ";
996 LOG_V(sev) << msg;
997 } else {
998 std::string msg(trace + 71, length - 72);
Peter Boströmd5c75b12015-09-23 13:24:32 +0200999 LOG_V(sev) << "webrtc: " << msg;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001000 }
1001}
1002
solenberg63b34542015-09-29 06:06:31 -07001003void WebRtcVoiceEngine::RegisterChannel(WebRtcVoiceMediaChannel* channel) {
solenberg566ef242015-11-06 15:34:49 -08001004 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
1005 RTC_DCHECK(channel);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001006 channels_.push_back(channel);
1007}
1008
solenberg63b34542015-09-29 06:06:31 -07001009void WebRtcVoiceEngine::UnregisterChannel(WebRtcVoiceMediaChannel* channel) {
solenberg566ef242015-11-06 15:34:49 -08001010 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
solenberg63b34542015-09-29 06:06:31 -07001011 auto it = std::find(channels_.begin(), channels_.end(), channel);
solenberg566ef242015-11-06 15:34:49 -08001012 RTC_DCHECK(it != channels_.end());
1013 channels_.erase(it);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001014}
1015
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001016// Adjusts the default AGC target level by the specified delta.
1017// NB: If we start messing with other config fields, we'll want
1018// to save the current webrtc::AgcConfig as well.
1019bool WebRtcVoiceEngine::AdjustAgcLevel(int delta) {
solenberg566ef242015-11-06 15:34:49 -08001020 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001021 webrtc::AgcConfig config = default_agc_config_;
1022 config.targetLeveldBOv -= delta;
1023
1024 LOG(LS_INFO) << "Adjusting AGC level from default -"
1025 << default_agc_config_.targetLeveldBOv << "dB to -"
1026 << config.targetLeveldBOv << "dB";
1027
1028 if (voe_wrapper_->processing()->SetAgcConfig(config) == -1) {
1029 LOG_RTCERR1(SetAgcConfig, config.targetLeveldBOv);
1030 return false;
1031 }
1032 return true;
1033}
1034
ivocd66b44d2016-01-15 03:06:36 -08001035bool WebRtcVoiceEngine::StartAecDump(rtc::PlatformFile file,
1036 int64_t max_size_bytes) {
solenberg566ef242015-11-06 15:34:49 -08001037 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00001038 FILE* aec_dump_file_stream = rtc::FdopenPlatformFileForWriting(file);
wu@webrtc.orga8910d22014-01-23 22:12:45 +00001039 if (!aec_dump_file_stream) {
1040 LOG(LS_ERROR) << "Could not open AEC dump file stream.";
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00001041 if (!rtc::ClosePlatformFile(file))
wu@webrtc.orga8910d22014-01-23 22:12:45 +00001042 LOG(LS_WARNING) << "Could not close file.";
1043 return false;
1044 }
wu@webrtc.orga9890802013-12-13 00:21:03 +00001045 StopAecDump();
ivocd66b44d2016-01-15 03:06:36 -08001046 if (voe_wrapper_->base()->audio_processing()->StartDebugRecording(
1047 aec_dump_file_stream, max_size_bytes) !=
wu@webrtc.orga9890802013-12-13 00:21:03 +00001048 webrtc::AudioProcessing::kNoError) {
wu@webrtc.orga8910d22014-01-23 22:12:45 +00001049 LOG_RTCERR0(StartDebugRecording);
1050 fclose(aec_dump_file_stream);
wu@webrtc.orga9890802013-12-13 00:21:03 +00001051 return false;
1052 }
1053 is_dumping_aec_ = true;
1054 return true;
wu@webrtc.orga9890802013-12-13 00:21:03 +00001055}
1056
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001057void WebRtcVoiceEngine::StartAecDump(const std::string& filename) {
solenberg566ef242015-11-06 15:34:49 -08001058 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001059 if (!is_dumping_aec_) {
1060 // Start dumping AEC when we are not dumping.
ivocd66b44d2016-01-15 03:06:36 -08001061 if (voe_wrapper_->base()->audio_processing()->StartDebugRecording(
1062 filename.c_str(), -1) != webrtc::AudioProcessing::kNoError) {
wu@webrtc.orga9890802013-12-13 00:21:03 +00001063 LOG_RTCERR1(StartDebugRecording, filename.c_str());
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001064 } else {
1065 is_dumping_aec_ = true;
1066 }
1067 }
1068}
1069
1070void WebRtcVoiceEngine::StopAecDump() {
solenberg566ef242015-11-06 15:34:49 -08001071 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001072 if (is_dumping_aec_) {
1073 // Stop dumping AEC when we are dumping.
ivocd66b44d2016-01-15 03:06:36 -08001074 if (voe_wrapper_->base()->audio_processing()->StopDebugRecording() !=
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001075 webrtc::AudioProcessing::kNoError) {
1076 LOG_RTCERR0(StopDebugRecording);
1077 }
1078 is_dumping_aec_ = false;
1079 }
1080}
1081
solenberg0a617e22015-10-20 15:49:38 -07001082int WebRtcVoiceEngine::CreateVoEChannel() {
solenberg566ef242015-11-06 15:34:49 -08001083 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
solenberg0a617e22015-10-20 15:49:38 -07001084 return voe_wrapper_->base()->CreateChannel(voe_config_);
sergeyu@chromium.org5bc25c42013-12-05 00:24:06 +00001085}
1086
solenberg5b5129a2016-04-08 05:35:48 -07001087webrtc::AudioDeviceModule* WebRtcVoiceEngine::adm() {
1088 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
1089 RTC_DCHECK(adm_);
1090 return adm_;
1091}
1092
ossu95eb1ba2016-07-13 06:05:27 -07001093AudioCodecs WebRtcVoiceEngine::CollectRecvCodecs() const {
1094 PayloadTypeMapper mapper;
1095 AudioCodecs out;
1096 const std::vector<webrtc::SdpAudioFormat>& formats =
1097 decoder_factory_->GetSupportedFormats();
1098
1099 // Only generate CN payload types for these clockrates
1100 std::map<int, bool, std::greater<int>> generate_cn = {{ 8000, false },
1101 { 16000, false },
1102 { 32000, false }};
1103
1104 auto map_format = [&mapper, &out] (const webrtc::SdpAudioFormat& format) {
1105 rtc::Optional<AudioCodec> opt_codec = mapper.ToAudioCodec(format);
1106 if (!opt_codec) {
1107 LOG(LS_ERROR) << "Unable to assign payload type to format: " << format;
1108 return false;
1109 }
1110
1111 auto& codec = *opt_codec;
1112 if (IsCodec(codec, kOpusCodecName)) {
1113 // TODO(ossu): Set this specifically for Opus for now, until we have a
1114 // better way of dealing with rtcp-fb parameters.
1115 codec.AddFeedbackParam(
1116 FeedbackParam(kRtcpFbParamTransportCc, kParamValueEmpty));
1117 }
1118 out.push_back(codec);
1119 return true;
1120 };
1121
1122 for (const auto& format : formats) {
1123 if (map_format(format)) {
1124 // TODO(ossu): We should get more than just a format from the factory, so
1125 // we can determine if a format should be used with CN or not. For now,
1126 // generate a CN entry for each supported clock rate also used by a format
1127 // supported by the factory.
1128 auto cn = generate_cn.find(format.clockrate_hz);
1129 if (cn != generate_cn.end() /* && format.allow_comfort_noise */) {
1130 cn->second = true;
1131 }
1132 }
1133 }
1134
1135 // Add CN codecs after "proper" audio codecs
1136 for (const auto& cn : generate_cn) {
1137 if (cn.second) {
1138 map_format({kCnCodecName, cn.first, 1});
1139 }
1140 }
1141
1142 // Add telephone-event codec last
1143 map_format({kDtmfCodecName, 8000, 1});
1144
1145 return out;
1146}
1147
solenbergc96df772015-10-21 13:01:53 -07001148class WebRtcVoiceMediaChannel::WebRtcAudioSendStream
Taylor Brandstetter1a018dc2016-03-08 12:37:39 -08001149 : public AudioSource::Sink {
mallinath@webrtc.org67ee6b92014-02-03 16:57:16 +00001150 public:
skvlade0d46372016-04-07 22:59:22 -07001151 WebRtcAudioSendStream(int ch,
1152 webrtc::AudioTransport* voe_audio_transport,
1153 uint32_t ssrc,
1154 const std::string& c_name,
solenberg971cab02016-06-14 10:02:41 -07001155 const SendCodecSpec& send_codec_spec,
solenberg3a941542015-11-16 07:34:50 -08001156 const std::vector<webrtc::RtpExtension>& extensions,
mflodman3d7db262016-04-29 00:57:13 -07001157 webrtc::Call* call,
1158 webrtc::Transport* send_transport)
solenberg7add0582015-11-20 09:59:34 -08001159 : voe_audio_transport_(voe_audio_transport),
solenberg3a941542015-11-16 07:34:50 -08001160 call_(call),
mflodman3d7db262016-04-29 00:57:13 -07001161 config_(send_transport),
skvlade0d46372016-04-07 22:59:22 -07001162 rtp_parameters_(CreateRtpParametersWithOneEncoding()) {
solenberg85a04962015-10-27 03:35:21 -07001163 RTC_DCHECK_GE(ch, 0);
1164 // TODO(solenberg): Once we're not using FakeWebRtcVoiceEngine anymore:
1165 // RTC_DCHECK(voe_audio_transport);
solenbergc96df772015-10-21 13:01:53 -07001166 RTC_DCHECK(call);
solenberg85a04962015-10-27 03:35:21 -07001167 audio_capture_thread_checker_.DetachFromThread();
solenberg3a941542015-11-16 07:34:50 -08001168 config_.rtp.ssrc = ssrc;
1169 config_.rtp.c_name = c_name;
1170 config_.voe_channel_id = ch;
solenberg971cab02016-06-14 10:02:41 -07001171 config_.rtp.extensions = extensions;
1172 RecreateAudioSendStream(send_codec_spec);
solenbergc96df772015-10-21 13:01:53 -07001173 }
solenberg3a941542015-11-16 07:34:50 -08001174
solenbergc96df772015-10-21 13:01:53 -07001175 ~WebRtcAudioSendStream() override {
solenberg566ef242015-11-06 15:34:49 -08001176 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
Taylor Brandstetter1a018dc2016-03-08 12:37:39 -08001177 ClearSource();
solenbergc96df772015-10-21 13:01:53 -07001178 call_->DestroyAudioSendStream(stream_);
1179 }
henrike@webrtc.org1e09a712013-07-26 19:17:59 +00001180
solenberg971cab02016-06-14 10:02:41 -07001181 void RecreateAudioSendStream(const SendCodecSpec& send_codec_spec) {
1182 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
1183 if (stream_) {
1184 call_->DestroyAudioSendStream(stream_);
1185 stream_ = nullptr;
1186 }
1187 config_.rtp.nack.rtp_history_ms =
1188 send_codec_spec.nack_enabled ? kNackRtpHistoryMs : 0;
1189 RTC_DCHECK(!stream_);
1190 stream_ = call_->CreateAudioSendStream(config_);
1191 RTC_CHECK(stream_);
1192 UpdateSendState();
1193 }
1194
solenberg3a941542015-11-16 07:34:50 -08001195 void RecreateAudioSendStream(
1196 const std::vector<webrtc::RtpExtension>& extensions) {
1197 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
1198 if (stream_) {
1199 call_->DestroyAudioSendStream(stream_);
1200 stream_ = nullptr;
1201 }
1202 config_.rtp.extensions = extensions;
1203 RTC_DCHECK(!stream_);
1204 stream_ = call_->CreateAudioSendStream(config_);
1205 RTC_CHECK(stream_);
solenberg6d6e7c52016-04-13 09:07:30 -07001206 UpdateSendState();
solenberg3a941542015-11-16 07:34:50 -08001207 }
1208
solenberg8842c3e2016-03-11 03:06:41 -08001209 bool SendTelephoneEvent(int payload_type, int event, int duration_ms) {
Fredrik Solenbergb5727682015-12-04 15:22:19 +01001210 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
1211 RTC_DCHECK(stream_);
1212 return stream_->SendTelephoneEvent(payload_type, event, duration_ms);
1213 }
1214
Taylor Brandstetter1a018dc2016-03-08 12:37:39 -08001215 void SetSend(bool send) {
1216 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
1217 send_ = send;
1218 UpdateSendState();
1219 }
1220
solenberg94218532016-06-16 10:53:22 -07001221 void SetMuted(bool muted) {
1222 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
1223 RTC_DCHECK(stream_);
1224 stream_->SetMuted(muted);
1225 muted_ = muted;
1226 }
1227
1228 bool muted() const {
1229 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
1230 return muted_;
1231 }
1232
solenberg3a941542015-11-16 07:34:50 -08001233 webrtc::AudioSendStream::Stats GetStats() const {
1234 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
1235 RTC_DCHECK(stream_);
1236 return stream_->GetStats();
1237 }
1238
Taylor Brandstetter1a018dc2016-03-08 12:37:39 -08001239 // Starts the sending by setting ourselves as a sink to the AudioSource to
1240 // get data callbacks.
henrike@webrtc.orga7b98182014-02-21 15:51:43 +00001241 // This method is called on the libjingle worker thread.
mallinath@webrtc.org67ee6b92014-02-03 16:57:16 +00001242 // TODO(xians): Make sure Start() is called only once.
Taylor Brandstetter1a018dc2016-03-08 12:37:39 -08001243 void SetSource(AudioSource* source) {
solenberg566ef242015-11-06 15:34:49 -08001244 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
Taylor Brandstetter1a018dc2016-03-08 12:37:39 -08001245 RTC_DCHECK(source);
1246 if (source_) {
1247 RTC_DCHECK(source_ == source);
mallinath@webrtc.org67ee6b92014-02-03 16:57:16 +00001248 return;
1249 }
Taylor Brandstetter1a018dc2016-03-08 12:37:39 -08001250 source->SetSink(this);
1251 source_ = source;
1252 UpdateSendState();
mallinath@webrtc.org67ee6b92014-02-03 16:57:16 +00001253 }
1254
Taylor Brandstetter1a018dc2016-03-08 12:37:39 -08001255 // Stops sending by setting the sink of the AudioSource to nullptr. No data
mallinath@webrtc.org67ee6b92014-02-03 16:57:16 +00001256 // callback will be received after this method.
henrike@webrtc.orga7b98182014-02-21 15:51:43 +00001257 // This method is called on the libjingle worker thread.
Taylor Brandstetter1a018dc2016-03-08 12:37:39 -08001258 void ClearSource() {
solenberg566ef242015-11-06 15:34:49 -08001259 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
Taylor Brandstetter1a018dc2016-03-08 12:37:39 -08001260 if (source_) {
1261 source_->SetSink(nullptr);
1262 source_ = nullptr;
solenberg98c68862015-10-09 03:27:14 -07001263 }
Taylor Brandstetter1a018dc2016-03-08 12:37:39 -08001264 UpdateSendState();
mallinath@webrtc.org67ee6b92014-02-03 16:57:16 +00001265 }
1266
Taylor Brandstetter1a018dc2016-03-08 12:37:39 -08001267 // AudioSource::Sink implementation.
henrike@webrtc.orga7b98182014-02-21 15:51:43 +00001268 // This method is called on the audio thread.
kjellander@webrtc.org14665ff2015-03-04 12:58:35 +00001269 void OnData(const void* audio_data,
1270 int bits_per_sample,
1271 int sample_rate,
Peter Kasting69558702016-01-12 16:26:35 -08001272 size_t number_of_channels,
Peter Kastingdce40cf2015-08-24 14:52:23 -07001273 size_t number_of_frames) override {
solenberg566ef242015-11-06 15:34:49 -08001274 RTC_DCHECK(!worker_thread_checker_.CalledOnValidThread());
solenberg85a04962015-10-27 03:35:21 -07001275 RTC_DCHECK(audio_capture_thread_checker_.CalledOnValidThread());
solenbergc96df772015-10-21 13:01:53 -07001276 RTC_DCHECK(voe_audio_transport_);
solenberg7add0582015-11-20 09:59:34 -08001277 voe_audio_transport_->OnData(config_.voe_channel_id,
henrike@webrtc.orga7b98182014-02-21 15:51:43 +00001278 audio_data,
1279 bits_per_sample,
1280 sample_rate,
1281 number_of_channels,
1282 number_of_frames);
henrike@webrtc.orga7b98182014-02-21 15:51:43 +00001283 }
1284
Taylor Brandstetter1a018dc2016-03-08 12:37:39 -08001285 // Callback from the |source_| when it is going away. In case Start() has
henrike@webrtc.orga7b98182014-02-21 15:51:43 +00001286 // never been called, this callback won't be triggered.
kjellander@webrtc.org14665ff2015-03-04 12:58:35 +00001287 void OnClose() override {
solenberg566ef242015-11-06 15:34:49 -08001288 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
Taylor Brandstetter1a018dc2016-03-08 12:37:39 -08001289 // Set |source_| to nullptr to make sure no more callback will get into
1290 // the source.
1291 source_ = nullptr;
1292 UpdateSendState();
mallinath@webrtc.org67ee6b92014-02-03 16:57:16 +00001293 }
1294
1295 // Accessor to the VoE channel ID.
solenberg85a04962015-10-27 03:35:21 -07001296 int channel() const {
solenberg566ef242015-11-06 15:34:49 -08001297 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
solenberg7add0582015-11-20 09:59:34 -08001298 return config_.voe_channel_id;
solenberg85a04962015-10-27 03:35:21 -07001299 }
mallinath@webrtc.org67ee6b92014-02-03 16:57:16 +00001300
skvlade0d46372016-04-07 22:59:22 -07001301 const webrtc::RtpParameters& rtp_parameters() const {
1302 return rtp_parameters_;
1303 }
1304
Taylor Brandstetter55dd7082016-05-03 13:50:11 -07001305 void SetRtpParameters(const webrtc::RtpParameters& parameters) {
skvlade0d46372016-04-07 22:59:22 -07001306 RTC_CHECK_EQ(1UL, parameters.encodings.size());
1307 rtp_parameters_ = parameters;
Taylor Brandstetter55dd7082016-05-03 13:50:11 -07001308 // parameters.encodings[0].active could have changed.
1309 UpdateSendState();
skvlade0d46372016-04-07 22:59:22 -07001310 }
1311
mallinath@webrtc.org67ee6b92014-02-03 16:57:16 +00001312 private:
Taylor Brandstetter1a018dc2016-03-08 12:37:39 -08001313 void UpdateSendState() {
1314 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
1315 RTC_DCHECK(stream_);
Taylor Brandstetter55dd7082016-05-03 13:50:11 -07001316 RTC_DCHECK_EQ(1UL, rtp_parameters_.encodings.size());
1317 if (send_ && source_ != nullptr && rtp_parameters_.encodings[0].active) {
Taylor Brandstetter1a018dc2016-03-08 12:37:39 -08001318 stream_->Start();
1319 } else { // !send || source_ = nullptr
1320 stream_->Stop();
1321 }
1322 }
1323
solenberg566ef242015-11-06 15:34:49 -08001324 rtc::ThreadChecker worker_thread_checker_;
solenberg85a04962015-10-27 03:35:21 -07001325 rtc::ThreadChecker audio_capture_thread_checker_;
solenbergc96df772015-10-21 13:01:53 -07001326 webrtc::AudioTransport* const voe_audio_transport_ = nullptr;
1327 webrtc::Call* call_ = nullptr;
solenberg3a941542015-11-16 07:34:50 -08001328 webrtc::AudioSendStream::Config config_;
1329 // The stream is owned by WebRtcAudioSendStream and may be reallocated if
1330 // configuration changes.
solenbergc96df772015-10-21 13:01:53 -07001331 webrtc::AudioSendStream* stream_ = nullptr;
mallinath@webrtc.org67ee6b92014-02-03 16:57:16 +00001332
Taylor Brandstetter1a018dc2016-03-08 12:37:39 -08001333 // Raw pointer to AudioSource owned by LocalAudioTrackHandler.
mallinath@webrtc.org67ee6b92014-02-03 16:57:16 +00001334 // PeerConnection will make sure invalidating the pointer before the object
1335 // goes away.
Taylor Brandstetter1a018dc2016-03-08 12:37:39 -08001336 AudioSource* source_ = nullptr;
1337 bool send_ = false;
solenberg94218532016-06-16 10:53:22 -07001338 bool muted_ = false;
skvlade0d46372016-04-07 22:59:22 -07001339 webrtc::RtpParameters rtp_parameters_;
henrike@webrtc.orga7b98182014-02-21 15:51:43 +00001340
solenbergc96df772015-10-21 13:01:53 -07001341 RTC_DISALLOW_IMPLICIT_CONSTRUCTORS(WebRtcAudioSendStream);
1342};
1343
1344class WebRtcVoiceMediaChannel::WebRtcAudioReceiveStream {
1345 public:
ossu29b1a8d2016-06-13 07:34:51 -07001346 WebRtcAudioReceiveStream(
1347 int ch,
1348 uint32_t remote_ssrc,
1349 uint32_t local_ssrc,
1350 bool use_transport_cc,
solenberg8189b022016-06-14 12:13:00 -07001351 bool use_nack,
ossu29b1a8d2016-06-13 07:34:51 -07001352 const std::string& sync_group,
1353 const std::vector<webrtc::RtpExtension>& extensions,
1354 webrtc::Call* call,
1355 webrtc::Transport* rtcp_send_transport,
1356 const rtc::scoped_refptr<webrtc::AudioDecoderFactory>& decoder_factory)
stefanba4c0e42016-02-04 04:12:24 -08001357 : call_(call), config_() {
solenberg7add0582015-11-20 09:59:34 -08001358 RTC_DCHECK_GE(ch, 0);
1359 RTC_DCHECK(call);
1360 config_.rtp.remote_ssrc = remote_ssrc;
solenberg31fec402016-05-06 02:13:12 -07001361 config_.rtcp_send_transport = rtcp_send_transport;
solenberg7add0582015-11-20 09:59:34 -08001362 config_.voe_channel_id = ch;
1363 config_.sync_group = sync_group;
ossu29b1a8d2016-06-13 07:34:51 -07001364 config_.decoder_factory = decoder_factory;
solenberg4a0f7b52016-06-16 13:07:33 -07001365 RecreateAudioReceiveStream(local_ssrc,
1366 use_transport_cc,
1367 use_nack,
1368 extensions);
solenberg7add0582015-11-20 09:59:34 -08001369 }
solenbergc96df772015-10-21 13:01:53 -07001370
solenberg7add0582015-11-20 09:59:34 -08001371 ~WebRtcAudioReceiveStream() {
1372 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
1373 call_->DestroyAudioReceiveStream(stream_);
1374 }
1375
solenberg4a0f7b52016-06-16 13:07:33 -07001376 void RecreateAudioReceiveStream(uint32_t local_ssrc) {
solenberg7add0582015-11-20 09:59:34 -08001377 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
solenberg4a0f7b52016-06-16 13:07:33 -07001378 RecreateAudioReceiveStream(local_ssrc,
1379 config_.rtp.transport_cc,
solenberg8189b022016-06-14 12:13:00 -07001380 config_.rtp.nack.rtp_history_ms != 0,
solenberg4a0f7b52016-06-16 13:07:33 -07001381 config_.rtp.extensions);
solenberg7add0582015-11-20 09:59:34 -08001382 }
solenberg8189b022016-06-14 12:13:00 -07001383
1384 void RecreateAudioReceiveStream(bool use_transport_cc, bool use_nack) {
solenberg7add0582015-11-20 09:59:34 -08001385 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
solenberg4a0f7b52016-06-16 13:07:33 -07001386 RecreateAudioReceiveStream(config_.rtp.local_ssrc,
1387 use_transport_cc,
solenberg8189b022016-06-14 12:13:00 -07001388 use_nack,
1389 config_.rtp.extensions);
solenberg7add0582015-11-20 09:59:34 -08001390 }
1391
solenberg4a0f7b52016-06-16 13:07:33 -07001392 void RecreateAudioReceiveStream(
1393 const std::vector<webrtc::RtpExtension>& extensions) {
1394 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
1395 RecreateAudioReceiveStream(config_.rtp.local_ssrc,
1396 config_.rtp.transport_cc,
1397 config_.rtp.nack.rtp_history_ms != 0,
1398 extensions);
1399 }
1400
solenberg7add0582015-11-20 09:59:34 -08001401 webrtc::AudioReceiveStream::Stats GetStats() const {
1402 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
1403 RTC_DCHECK(stream_);
1404 return stream_->GetStats();
1405 }
1406
1407 int channel() const {
1408 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
1409 return config_.voe_channel_id;
1410 }
solenbergc96df772015-10-21 13:01:53 -07001411
kwiberg686a8ef2016-02-26 03:00:35 -08001412 void SetRawAudioSink(std::unique_ptr<webrtc::AudioSinkInterface> sink) {
Tommif888bb52015-12-12 01:37:01 +01001413 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
kwiberg686a8ef2016-02-26 03:00:35 -08001414 stream_->SetSink(std::move(sink));
Tommif888bb52015-12-12 01:37:01 +01001415 }
1416
solenberg217fb662016-06-17 08:30:54 -07001417 void SetOutputVolume(double volume) {
1418 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
1419 stream_->SetGain(volume);
1420 }
1421
solenbergc96df772015-10-21 13:01:53 -07001422 private:
stefanba4c0e42016-02-04 04:12:24 -08001423 void RecreateAudioReceiveStream(
solenberg4a0f7b52016-06-16 13:07:33 -07001424 uint32_t local_ssrc,
stefanba4c0e42016-02-04 04:12:24 -08001425 bool use_transport_cc,
solenberg8189b022016-06-14 12:13:00 -07001426 bool use_nack,
solenberg7add0582015-11-20 09:59:34 -08001427 const std::vector<webrtc::RtpExtension>& extensions) {
1428 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
1429 if (stream_) {
1430 call_->DestroyAudioReceiveStream(stream_);
1431 stream_ = nullptr;
1432 }
solenberg4a0f7b52016-06-16 13:07:33 -07001433 config_.rtp.local_ssrc = local_ssrc;
stefanba4c0e42016-02-04 04:12:24 -08001434 config_.rtp.transport_cc = use_transport_cc;
solenberg8189b022016-06-14 12:13:00 -07001435 config_.rtp.nack.rtp_history_ms = use_nack ? kNackRtpHistoryMs : 0;
1436 config_.rtp.extensions = extensions;
solenberg7add0582015-11-20 09:59:34 -08001437 RTC_DCHECK(!stream_);
1438 stream_ = call_->CreateAudioReceiveStream(config_);
1439 RTC_CHECK(stream_);
1440 }
1441
1442 rtc::ThreadChecker worker_thread_checker_;
1443 webrtc::Call* call_ = nullptr;
1444 webrtc::AudioReceiveStream::Config config_;
1445 // The stream is owned by WebRtcAudioReceiveStream and may be reallocated if
1446 // configuration changes.
1447 webrtc::AudioReceiveStream* stream_ = nullptr;
solenbergc96df772015-10-21 13:01:53 -07001448
1449 RTC_DISALLOW_IMPLICIT_CONSTRUCTORS(WebRtcAudioReceiveStream);
henrike@webrtc.org1e09a712013-07-26 19:17:59 +00001450};
1451
Fredrik Solenberg709ed672015-09-15 12:26:33 +02001452WebRtcVoiceMediaChannel::WebRtcVoiceMediaChannel(WebRtcVoiceEngine* engine,
nisse51542be2016-02-12 02:27:06 -08001453 const MediaConfig& config,
Fredrik Solenbergb071a192015-09-17 16:42:56 +02001454 const AudioOptions& options,
Fredrik Solenberg709ed672015-09-15 12:26:33 +02001455 webrtc::Call* call)
nisse51542be2016-02-12 02:27:06 -08001456 : VoiceMediaChannel(config), engine_(engine), call_(call) {
solenberg0a617e22015-10-20 15:49:38 -07001457 LOG(LS_VERBOSE) << "WebRtcVoiceMediaChannel::WebRtcVoiceMediaChannel";
solenberg566ef242015-11-06 15:34:49 -08001458 RTC_DCHECK(call);
solenberg0a617e22015-10-20 15:49:38 -07001459 engine->RegisterChannel(this);
Fredrik Solenbergb071a192015-09-17 16:42:56 +02001460 SetOptions(options);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001461}
1462
1463WebRtcVoiceMediaChannel::~WebRtcVoiceMediaChannel() {
solenberg566ef242015-11-06 15:34:49 -08001464 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
solenberg0a617e22015-10-20 15:49:38 -07001465 LOG(LS_VERBOSE) << "WebRtcVoiceMediaChannel::~WebRtcVoiceMediaChannel";
solenberg7add0582015-11-20 09:59:34 -08001466 // TODO(solenberg): Should be able to delete the streams directly, without
1467 // going through RemoveNnStream(), once stream objects handle
1468 // all (de)configuration.
solenbergc96df772015-10-21 13:01:53 -07001469 while (!send_streams_.empty()) {
1470 RemoveSendStream(send_streams_.begin()->first);
solenbergd97ec302015-10-07 01:40:33 -07001471 }
solenberg7add0582015-11-20 09:59:34 -08001472 while (!recv_streams_.empty()) {
1473 RemoveRecvStream(recv_streams_.begin()->first);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001474 }
solenberg0a617e22015-10-20 15:49:38 -07001475 engine()->UnregisterChannel(this);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001476}
1477
nisse51542be2016-02-12 02:27:06 -08001478rtc::DiffServCodePoint WebRtcVoiceMediaChannel::PreferredDscp() const {
1479 return kAudioDscpValue;
1480}
1481
Peter Thatcherc2ee2c82015-08-07 16:05:34 -07001482bool WebRtcVoiceMediaChannel::SetSendParameters(
1483 const AudioSendParameters& params) {
Peter Boströmca8b4042016-03-08 14:24:13 -08001484 TRACE_EVENT0("webrtc", "WebRtcVoiceMediaChannel::SetSendParameters");
solenberg566ef242015-11-06 15:34:49 -08001485 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
solenberg7e4e01a2015-12-02 08:05:01 -08001486 LOG(LS_INFO) << "WebRtcVoiceMediaChannel::SetSendParameters: "
1487 << params.ToString();
Peter Thatcherc2ee2c82015-08-07 16:05:34 -07001488 // TODO(pthatcher): Refactor this to be more clean now that we have
1489 // all the information at once.
solenberg3a941542015-11-16 07:34:50 -08001490
1491 if (!SetSendCodecs(params.codecs)) {
1492 return false;
1493 }
1494
solenberg7e4e01a2015-12-02 08:05:01 -08001495 if (!ValidateRtpExtensions(params.extensions)) {
1496 return false;
1497 }
1498 std::vector<webrtc::RtpExtension> filtered_extensions =
1499 FilterRtpExtensions(params.extensions,
1500 webrtc::RtpExtension::IsSupportedForAudio, true);
1501 if (send_rtp_extensions_ != filtered_extensions) {
1502 send_rtp_extensions_.swap(filtered_extensions);
solenberg3a941542015-11-16 07:34:50 -08001503 for (auto& it : send_streams_) {
1504 it.second->RecreateAudioSendStream(send_rtp_extensions_);
1505 }
1506 }
1507
deadbeef80346142016-04-27 14:17:10 -07001508 if (!SetMaxSendBitrate(params.max_bandwidth_bps)) {
solenberg3a941542015-11-16 07:34:50 -08001509 return false;
1510 }
1511 return SetOptions(params.options);
Peter Thatcherc2ee2c82015-08-07 16:05:34 -07001512}
1513
1514bool WebRtcVoiceMediaChannel::SetRecvParameters(
1515 const AudioRecvParameters& params) {
Peter Boströmca8b4042016-03-08 14:24:13 -08001516 TRACE_EVENT0("webrtc", "WebRtcVoiceMediaChannel::SetRecvParameters");
solenberg566ef242015-11-06 15:34:49 -08001517 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
solenberg7e4e01a2015-12-02 08:05:01 -08001518 LOG(LS_INFO) << "WebRtcVoiceMediaChannel::SetRecvParameters: "
1519 << params.ToString();
Peter Thatcherc2ee2c82015-08-07 16:05:34 -07001520 // TODO(pthatcher): Refactor this to be more clean now that we have
1521 // all the information at once.
solenberg7add0582015-11-20 09:59:34 -08001522
1523 if (!SetRecvCodecs(params.codecs)) {
1524 return false;
1525 }
1526
solenberg7e4e01a2015-12-02 08:05:01 -08001527 if (!ValidateRtpExtensions(params.extensions)) {
1528 return false;
1529 }
1530 std::vector<webrtc::RtpExtension> filtered_extensions =
1531 FilterRtpExtensions(params.extensions,
1532 webrtc::RtpExtension::IsSupportedForAudio, false);
1533 if (recv_rtp_extensions_ != filtered_extensions) {
1534 recv_rtp_extensions_.swap(filtered_extensions);
solenberg7add0582015-11-20 09:59:34 -08001535 for (auto& it : recv_streams_) {
1536 it.second->RecreateAudioReceiveStream(recv_rtp_extensions_);
1537 }
1538 }
solenberg7add0582015-11-20 09:59:34 -08001539 return true;
Peter Thatcherc2ee2c82015-08-07 16:05:34 -07001540}
1541
Taylor Brandstetterdb0cd9e2016-05-16 11:40:30 -07001542webrtc::RtpParameters WebRtcVoiceMediaChannel::GetRtpSendParameters(
skvlade0d46372016-04-07 22:59:22 -07001543 uint32_t ssrc) const {
1544 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
1545 auto it = send_streams_.find(ssrc);
1546 if (it == send_streams_.end()) {
Taylor Brandstetterdb0cd9e2016-05-16 11:40:30 -07001547 LOG(LS_WARNING) << "Attempting to get RTP send parameters for stream "
1548 << "with ssrc " << ssrc << " which doesn't exist.";
skvlade0d46372016-04-07 22:59:22 -07001549 return webrtc::RtpParameters();
1550 }
1551
Taylor Brandstetter0cd086b2016-04-20 16:23:10 -07001552 webrtc::RtpParameters rtp_params = it->second->rtp_parameters();
1553 // Need to add the common list of codecs to the send stream-specific
1554 // RTP parameters.
1555 for (const AudioCodec& codec : send_codecs_) {
1556 rtp_params.codecs.push_back(codec.ToCodecParameters());
1557 }
1558 return rtp_params;
skvlade0d46372016-04-07 22:59:22 -07001559}
1560
Taylor Brandstetterdb0cd9e2016-05-16 11:40:30 -07001561bool WebRtcVoiceMediaChannel::SetRtpSendParameters(
skvlade0d46372016-04-07 22:59:22 -07001562 uint32_t ssrc,
1563 const webrtc::RtpParameters& parameters) {
1564 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
1565 if (!ValidateRtpParameters(parameters)) {
1566 return false;
1567 }
1568 auto it = send_streams_.find(ssrc);
1569 if (it == send_streams_.end()) {
Taylor Brandstetterdb0cd9e2016-05-16 11:40:30 -07001570 LOG(LS_WARNING) << "Attempting to set RTP send parameters for stream "
1571 << "with ssrc " << ssrc << " which doesn't exist.";
skvlade0d46372016-04-07 22:59:22 -07001572 return false;
1573 }
1574
Taylor Brandstetterdb0cd9e2016-05-16 11:40:30 -07001575 // TODO(deadbeef): Handle setting parameters with a list of codecs in a
1576 // different order (which should change the send codec).
1577 webrtc::RtpParameters current_parameters = GetRtpSendParameters(ssrc);
1578 if (current_parameters.codecs != parameters.codecs) {
1579 LOG(LS_ERROR) << "Using SetParameters to change the set of codecs "
1580 << "is not currently supported.";
1581 return false;
1582 }
1583
1584 if (!SetChannelSendParameters(it->second->channel(), parameters)) {
1585 LOG(LS_WARNING) << "Failed to set send RtpParameters.";
skvlade0d46372016-04-07 22:59:22 -07001586 return false;
1587 }
Taylor Brandstetter0cd086b2016-04-20 16:23:10 -07001588 // Codecs are handled at the WebRtcVoiceMediaChannel level.
1589 webrtc::RtpParameters reduced_params = parameters;
1590 reduced_params.codecs.clear();
Taylor Brandstetter55dd7082016-05-03 13:50:11 -07001591 it->second->SetRtpParameters(reduced_params);
skvlade0d46372016-04-07 22:59:22 -07001592 return true;
1593}
1594
Taylor Brandstetterdb0cd9e2016-05-16 11:40:30 -07001595webrtc::RtpParameters WebRtcVoiceMediaChannel::GetRtpReceiveParameters(
1596 uint32_t ssrc) const {
1597 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
1598 auto it = recv_streams_.find(ssrc);
1599 if (it == recv_streams_.end()) {
1600 LOG(LS_WARNING) << "Attempting to get RTP receive parameters for stream "
1601 << "with ssrc " << ssrc << " which doesn't exist.";
1602 return webrtc::RtpParameters();
1603 }
1604
1605 // TODO(deadbeef): Return stream-specific parameters.
1606 webrtc::RtpParameters rtp_params = CreateRtpParametersWithOneEncoding();
1607 for (const AudioCodec& codec : recv_codecs_) {
1608 rtp_params.codecs.push_back(codec.ToCodecParameters());
1609 }
1610 return rtp_params;
1611}
1612
1613bool WebRtcVoiceMediaChannel::SetRtpReceiveParameters(
1614 uint32_t ssrc,
1615 const webrtc::RtpParameters& parameters) {
1616 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
1617 if (!ValidateRtpParameters(parameters)) {
1618 return false;
1619 }
1620 auto it = recv_streams_.find(ssrc);
1621 if (it == recv_streams_.end()) {
1622 LOG(LS_WARNING) << "Attempting to set RTP receive parameters for stream "
1623 << "with ssrc " << ssrc << " which doesn't exist.";
1624 return false;
1625 }
1626
1627 webrtc::RtpParameters current_parameters = GetRtpReceiveParameters(ssrc);
1628 if (current_parameters != parameters) {
1629 LOG(LS_ERROR) << "Changing the RTP receive parameters is currently "
1630 << "unsupported.";
1631 return false;
1632 }
1633 return true;
1634}
1635
skvlade0d46372016-04-07 22:59:22 -07001636bool WebRtcVoiceMediaChannel::ValidateRtpParameters(
1637 const webrtc::RtpParameters& rtp_parameters) {
1638 if (rtp_parameters.encodings.size() != 1) {
1639 LOG(LS_ERROR)
1640 << "Attempted to set RtpParameters without exactly one encoding";
1641 return false;
1642 }
1643 return true;
1644}
1645
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001646bool WebRtcVoiceMediaChannel::SetOptions(const AudioOptions& options) {
solenberg566ef242015-11-06 15:34:49 -08001647 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001648 LOG(LS_INFO) << "Setting voice channel options: "
1649 << options.ToString();
1650
1651 // We retain all of the existing options, and apply the given ones
1652 // on top. This means there is no way to "clear" options such that
1653 // they go back to the engine default.
1654 options_.SetAll(options);
solenberg246b8172015-12-08 09:50:23 -08001655 if (!engine()->ApplyOptions(options_)) {
1656 LOG(LS_WARNING) <<
1657 "Failed to apply engine options during channel SetOptions.";
1658 return false;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001659 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001660 LOG(LS_INFO) << "Set voice channel options. Current options: "
1661 << options_.ToString();
1662 return true;
1663}
1664
1665bool WebRtcVoiceMediaChannel::SetRecvCodecs(
1666 const std::vector<AudioCodec>& codecs) {
solenberg566ef242015-11-06 15:34:49 -08001667 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
solenberg8fb30c32015-10-13 03:06:58 -07001668
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001669 // Set the payload types to be used for incoming media.
solenberg0b675462015-10-09 01:37:09 -07001670 LOG(LS_INFO) << "Setting receive voice codecs.";
solenberg0b675462015-10-09 01:37:09 -07001671
1672 if (!VerifyUniquePayloadTypes(codecs)) {
1673 LOG(LS_ERROR) << "Codec payload types overlap.";
1674 return false;
1675 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001676
1677 std::vector<AudioCodec> new_codecs;
1678 // Find all new codecs. We allow adding new codecs but don't allow changing
1679 // the payload type of codecs that is already configured since we might
1680 // already be receiving packets with that payload type.
Fredrik Solenbergaf9fb212015-08-26 10:45:53 +02001681 for (const AudioCodec& codec : codecs) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001682 AudioCodec old_codec;
Fredrik Solenbergaf9fb212015-08-26 10:45:53 +02001683 if (FindCodec(recv_codecs_, codec, &old_codec)) {
1684 if (old_codec.id != codec.id) {
1685 LOG(LS_ERROR) << codec.name << " payload type changed.";
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001686 return false;
1687 }
1688 } else {
Fredrik Solenbergaf9fb212015-08-26 10:45:53 +02001689 new_codecs.push_back(codec);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001690 }
1691 }
1692 if (new_codecs.empty()) {
1693 // There are no new codecs to configure. Already configured codecs are
1694 // never removed.
1695 return true;
1696 }
1697
1698 if (playout_) {
1699 // Receive codecs can not be changed while playing. So we temporarily
1700 // pause playout.
1701 PausePlayout();
1702 }
1703
solenberg26c8c912015-11-27 04:00:25 -08001704 bool result = true;
1705 for (const AudioCodec& codec : new_codecs) {
solenberg72e29d22016-03-08 06:35:16 -08001706 webrtc::CodecInst voe_codec = {0};
solenberg26c8c912015-11-27 04:00:25 -08001707 if (WebRtcVoiceEngine::ToCodecInst(codec, &voe_codec)) {
1708 LOG(LS_INFO) << ToString(codec);
1709 voe_codec.pltype = codec.id;
1710 for (const auto& ch : recv_streams_) {
1711 if (engine()->voe()->codec()->SetRecPayloadType(
1712 ch.second->channel(), voe_codec) == -1) {
1713 LOG_RTCERR2(SetRecPayloadType, ch.second->channel(),
1714 ToString(voe_codec));
1715 result = false;
1716 }
1717 }
1718 } else {
1719 LOG(LS_WARNING) << "Unknown codec " << ToString(codec);
1720 result = false;
1721 break;
1722 }
1723 }
Fredrik Solenbergaf9fb212015-08-26 10:45:53 +02001724 if (result) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001725 recv_codecs_ = codecs;
1726 }
1727
1728 if (desired_playout_ && !playout_) {
1729 ResumePlayout();
1730 }
Fredrik Solenbergaf9fb212015-08-26 10:45:53 +02001731 return result;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001732}
1733
solenberg72e29d22016-03-08 06:35:16 -08001734// Utility function called from SetSendParameters() to extract current send
1735// codec settings from the given list of codecs (originally from SDP). Both send
1736// and receive streams may be reconfigured based on the new settings.
wu@webrtc.orgcadf9042013-08-30 21:24:16 +00001737bool WebRtcVoiceMediaChannel::SetSendCodecs(
1738 const std::vector<AudioCodec>& codecs) {
solenberg566ef242015-11-06 15:34:49 -08001739 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
Fredrik Solenbergb5727682015-12-04 15:22:19 +01001740 // TODO(solenberg): Validate input - that payload types don't overlap, are
1741 // within range, filter out codecs we don't support,
solenberg31642aa2016-03-14 08:00:37 -07001742 // redundant codecs etc - the same way it is done for
1743 // RtpHeaderExtensions.
solenbergd97ec302015-10-07 01:40:33 -07001744
Fredrik Solenbergb5727682015-12-04 15:22:19 +01001745 // Find the DTMF telephone event "codec" payload type.
1746 dtmf_payload_type_ = rtc::Optional<int>();
Fredrik Solenbergaf9fb212015-08-26 10:45:53 +02001747 for (const AudioCodec& codec : codecs) {
Fredrik Solenbergaf9fb212015-08-26 10:45:53 +02001748 if (IsCodec(codec, kDtmfCodecName)) {
solenberg31642aa2016-03-14 08:00:37 -07001749 if (codec.id < kMinPayloadType || codec.id > kMaxPayloadType) {
1750 return false;
1751 }
Fredrik Solenbergb5727682015-12-04 15:22:19 +01001752 dtmf_payload_type_ = rtc::Optional<int>(codec.id);
1753 break;
wu@webrtc.orgcadf9042013-08-30 21:24:16 +00001754 }
1755 }
1756
solenberg72e29d22016-03-08 06:35:16 -08001757 // Scan through the list to figure out the codec to use for sending, along
kwiberg68061362016-06-14 08:04:47 -07001758 // with the proper configuration for VAD, CNG, NACK and Opus-specific
solenberg72e29d22016-03-08 06:35:16 -08001759 // parameters.
kwiberg68061362016-06-14 08:04:47 -07001760 // TODO(solenberg): Refactor this logic once we create AudioEncoders here.
solenberg971cab02016-06-14 10:02:41 -07001761 SendCodecSpec send_codec_spec;
solenberg72e29d22016-03-08 06:35:16 -08001762 {
solenberg72e29d22016-03-08 06:35:16 -08001763 send_codec_spec.nack_enabled = send_codec_spec_.nack_enabled;
1764
1765 // Find send codec (the first non-telephone-event/CN codec).
1766 const AudioCodec* codec = WebRtcVoiceCodecs::GetPreferredCodec(
kwiberg68061362016-06-14 08:04:47 -07001767 codecs, &send_codec_spec.codec_inst);
solenberg72e29d22016-03-08 06:35:16 -08001768 if (!codec) {
1769 LOG(LS_WARNING) << "Received empty list of codecs.";
1770 return false;
1771 }
1772
1773 send_codec_spec.transport_cc_enabled = HasTransportCc(*codec);
kwiberg68061362016-06-14 08:04:47 -07001774 send_codec_spec.nack_enabled = HasNack(*codec);
solenberg72e29d22016-03-08 06:35:16 -08001775
kwiberg68061362016-06-14 08:04:47 -07001776 // For Opus as the send codec, we are to determine inband FEC, maximum
1777 // playback rate, and opus internal dtx.
1778 if (IsCodec(*codec, kOpusCodecName)) {
1779 GetOpusConfig(*codec, &send_codec_spec.codec_inst,
1780 &send_codec_spec.enable_codec_fec,
1781 &send_codec_spec.opus_max_playback_rate,
1782 &send_codec_spec.enable_opus_dtx);
1783 }
solenberg72e29d22016-03-08 06:35:16 -08001784
kwiberg68061362016-06-14 08:04:47 -07001785 // Set packet size if the AudioCodec param kCodecParamPTime is set.
1786 int ptime_ms = 0;
1787 if (codec->GetParam(kCodecParamPTime, &ptime_ms)) {
1788 if (!WebRtcVoiceCodecs::SetPTimeAsPacketSize(
1789 &send_codec_spec.codec_inst, ptime_ms)) {
1790 LOG(LS_WARNING) << "Failed to set packet size for codec "
1791 << send_codec_spec.codec_inst.plname;
1792 return false;
solenberg72e29d22016-03-08 06:35:16 -08001793 }
1794 }
1795
1796 // Loop through the codecs list again to find the CN codec.
1797 // TODO(solenberg): Break out into a separate function?
1798 for (const AudioCodec& codec : codecs) {
1799 // Ignore codecs we don't know about. The negotiation step should prevent
1800 // this, but double-check to be sure.
1801 webrtc::CodecInst voe_codec = {0};
1802 if (!WebRtcVoiceEngine::ToCodecInst(codec, &voe_codec)) {
1803 LOG(LS_WARNING) << "Unknown codec " << ToString(codec);
1804 continue;
1805 }
1806
1807 if (IsCodec(codec, kCnCodecName)) {
1808 // Turn voice activity detection/comfort noise on if supported.
1809 // Set the wideband CN payload type appropriately.
1810 // (narrowband always uses the static payload type 13).
1811 int cng_plfreq = -1;
1812 switch (codec.clockrate) {
1813 case 8000:
1814 case 16000:
1815 case 32000:
1816 cng_plfreq = codec.clockrate;
1817 break;
1818 default:
1819 LOG(LS_WARNING) << "CN frequency " << codec.clockrate
1820 << " not supported.";
1821 continue;
1822 }
1823 send_codec_spec.cng_payload_type = codec.id;
1824 send_codec_spec.cng_plfreq = cng_plfreq;
1825 break;
1826 }
1827 }
solenberg72e29d22016-03-08 06:35:16 -08001828 }
1829
solenberg971cab02016-06-14 10:02:41 -07001830 // Apply new settings to all streams.
1831 if (send_codec_spec_ != send_codec_spec) {
1832 send_codec_spec_ = std::move(send_codec_spec);
1833 for (const auto& kv : send_streams_) {
1834 kv.second->RecreateAudioSendStream(send_codec_spec_);
1835 if (!SetSendCodecs(kv.second->channel(), kv.second->rtp_parameters())) {
1836 return false;
1837 }
wu@webrtc.orgcadf9042013-08-30 21:24:16 +00001838 }
1839 }
1840
solenberg8189b022016-06-14 12:13:00 -07001841 // Check if the transport cc feedback or NACK status has changed on the
1842 // preferred send codec, and in that case reconfigure all receive streams.
1843 if (recv_transport_cc_enabled_ != send_codec_spec_.transport_cc_enabled ||
1844 recv_nack_enabled_ != send_codec_spec_.nack_enabled) {
solenberg72e29d22016-03-08 06:35:16 -08001845 LOG(LS_INFO) << "Recreate all the receive streams because the send "
1846 "codec has changed.";
1847 recv_transport_cc_enabled_ = send_codec_spec_.transport_cc_enabled;
solenberg8189b022016-06-14 12:13:00 -07001848 recv_nack_enabled_ = send_codec_spec_.nack_enabled;
solenberg72e29d22016-03-08 06:35:16 -08001849 for (auto& kv : recv_streams_) {
solenberg8189b022016-06-14 12:13:00 -07001850 kv.second->RecreateAudioReceiveStream(recv_transport_cc_enabled_,
1851 recv_nack_enabled_);
solenberg72e29d22016-03-08 06:35:16 -08001852 }
1853 }
1854
Taylor Brandstetter0cd086b2016-04-20 16:23:10 -07001855 send_codecs_ = codecs;
solenberg72e29d22016-03-08 06:35:16 -08001856 return true;
1857}
1858
1859// Apply current codec settings to a single voe::Channel used for sending.
skvlade0d46372016-04-07 22:59:22 -07001860bool WebRtcVoiceMediaChannel::SetSendCodecs(
1861 int channel,
1862 const webrtc::RtpParameters& rtp_parameters) {
solenberg971cab02016-06-14 10:02:41 -07001863 // Disable VAD and FEC unless we know the other side wants them.
solenberg72e29d22016-03-08 06:35:16 -08001864 engine()->voe()->codec()->SetVADStatus(channel, false);
solenberg72e29d22016-03-08 06:35:16 -08001865 engine()->voe()->codec()->SetFECStatus(channel, false);
1866
solenberg72e29d22016-03-08 06:35:16 -08001867 // Set the codec immediately, since SetVADStatus() depends on whether
1868 // the current codec is mono or stereo.
1869 if (!SetSendCodec(channel, send_codec_spec_.codec_inst)) {
1870 return false;
1871 }
1872
1873 // FEC should be enabled after SetSendCodec.
1874 if (send_codec_spec_.enable_codec_fec) {
1875 LOG(LS_INFO) << "Attempt to enable codec internal FEC on channel "
1876 << channel;
1877 if (engine()->voe()->codec()->SetFECStatus(channel, true) == -1) {
1878 // Enable codec internal FEC. Treat any failure as fatal internal error.
1879 LOG_RTCERR2(SetFECStatus, channel, true);
1880 return false;
1881 }
1882 }
1883
1884 if (IsCodec(send_codec_spec_.codec_inst, kOpusCodecName)) {
1885 // DTX and maxplaybackrate should be set after SetSendCodec. Because current
1886 // send codec has to be Opus.
1887
1888 // Set Opus internal DTX.
1889 LOG(LS_INFO) << "Attempt to "
1890 << (send_codec_spec_.enable_opus_dtx ? "enable" : "disable")
1891 << " Opus DTX on channel "
1892 << channel;
1893 if (engine()->voe()->codec()->SetOpusDtx(channel,
1894 send_codec_spec_.enable_opus_dtx)) {
1895 LOG_RTCERR2(SetOpusDtx, channel, send_codec_spec_.enable_opus_dtx);
1896 return false;
1897 }
1898
1899 // If opus_max_playback_rate <= 0, the default maximum playback rate
1900 // (48 kHz) will be used.
1901 if (send_codec_spec_.opus_max_playback_rate > 0) {
1902 LOG(LS_INFO) << "Attempt to set maximum playback rate to "
1903 << send_codec_spec_.opus_max_playback_rate
1904 << " Hz on channel "
1905 << channel;
1906 if (engine()->voe()->codec()->SetOpusMaxPlaybackRate(
1907 channel, send_codec_spec_.opus_max_playback_rate) == -1) {
1908 LOG_RTCERR2(SetOpusMaxPlaybackRate, channel,
1909 send_codec_spec_.opus_max_playback_rate);
1910 return false;
stefanba4c0e42016-02-04 04:12:24 -08001911 }
1912 }
1913 }
deadbeef80346142016-04-27 14:17:10 -07001914 // TODO(solenberg): SetMaxSendBitrate() yields another call to SetSendCodec().
skvlade0d46372016-04-07 22:59:22 -07001915 // Check if it is possible to fuse with the previous call in this function.
Taylor Brandstetterdb0cd9e2016-05-16 11:40:30 -07001916 SetChannelSendParameters(channel, rtp_parameters);
solenberg72e29d22016-03-08 06:35:16 -08001917
1918 // Set the CN payloadtype and the VAD status.
1919 if (send_codec_spec_.cng_payload_type != -1) {
1920 // The CN payload type for 8000 Hz clockrate is fixed at 13.
1921 if (send_codec_spec_.cng_plfreq != 8000) {
1922 webrtc::PayloadFrequencies cn_freq;
1923 switch (send_codec_spec_.cng_plfreq) {
1924 case 16000:
1925 cn_freq = webrtc::kFreq16000Hz;
1926 break;
1927 case 32000:
1928 cn_freq = webrtc::kFreq32000Hz;
1929 break;
1930 default:
1931 RTC_NOTREACHED();
1932 return false;
1933 }
1934 if (engine()->voe()->codec()->SetSendCNPayloadType(
1935 channel, send_codec_spec_.cng_payload_type, cn_freq) == -1) {
1936 LOG_RTCERR3(SetSendCNPayloadType, channel,
1937 send_codec_spec_.cng_payload_type, cn_freq);
1938 // TODO(ajm): This failure condition will be removed from VoE.
1939 // Restore the return here when we update to a new enough webrtc.
1940 //
1941 // Not returning false because the SetSendCNPayloadType will fail if
1942 // the channel is already sending.
1943 // This can happen if the remote description is applied twice, for
1944 // example in the case of ROAP on top of JSEP, where both side will
1945 // send the offer.
1946 }
1947 }
1948
1949 // Only turn on VAD if we have a CN payload type that matches the
1950 // clockrate for the codec we are going to use.
1951 if (send_codec_spec_.cng_plfreq == send_codec_spec_.codec_inst.plfreq &&
1952 send_codec_spec_.codec_inst.channels == 1) {
1953 // TODO(minyue): If CN frequency == 48000 Hz is allowed, consider the
1954 // interaction between VAD and Opus FEC.
1955 LOG(LS_INFO) << "Enabling VAD";
1956 if (engine()->voe()->codec()->SetVADStatus(channel, true) == -1) {
1957 LOG_RTCERR2(SetVADStatus, channel, true);
1958 return false;
1959 }
1960 }
1961 }
solenberg0a617e22015-10-20 15:49:38 -07001962 return true;
wu@webrtc.org9dba5252013-08-05 20:36:57 +00001963}
1964
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001965bool WebRtcVoiceMediaChannel::SetSendCodec(
wu@webrtc.org9dba5252013-08-05 20:36:57 +00001966 int channel, const webrtc::CodecInst& send_codec) {
1967 LOG(LS_INFO) << "Send channel " << channel << " selected voice codec "
1968 << ToString(send_codec) << ", bitrate=" << send_codec.rate;
1969
solenberg72e29d22016-03-08 06:35:16 -08001970 webrtc::CodecInst current_codec = {0};
wu@webrtc.org05e7b442014-04-01 17:44:24 +00001971 if (engine()->voe()->codec()->GetSendCodec(channel, current_codec) == 0 &&
1972 (send_codec == current_codec)) {
1973 // Codec is already configured, we can return without setting it again.
1974 return true;
1975 }
1976
wu@webrtc.org9dba5252013-08-05 20:36:57 +00001977 if (engine()->voe()->codec()->SetSendCodec(channel, send_codec) == -1) {
1978 LOG_RTCERR2(SetSendCodec, channel, ToString(send_codec));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001979 return false;
1980 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001981 return true;
1982}
1983
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001984bool WebRtcVoiceMediaChannel::SetPlayout(bool playout) {
1985 desired_playout_ = playout;
1986 return ChangePlayout(desired_playout_);
1987}
1988
1989bool WebRtcVoiceMediaChannel::PausePlayout() {
1990 return ChangePlayout(false);
1991}
1992
1993bool WebRtcVoiceMediaChannel::ResumePlayout() {
1994 return ChangePlayout(desired_playout_);
1995}
1996
1997bool WebRtcVoiceMediaChannel::ChangePlayout(bool playout) {
Peter Boströmca8b4042016-03-08 14:24:13 -08001998 TRACE_EVENT0("webrtc", "WebRtcVoiceMediaChannel::ChangePlayout");
solenberg566ef242015-11-06 15:34:49 -08001999 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002000 if (playout_ == playout) {
2001 return true;
2002 }
2003
solenberg7add0582015-11-20 09:59:34 -08002004 for (const auto& ch : recv_streams_) {
Fredrik Solenbergaf9fb212015-08-26 10:45:53 +02002005 if (!SetPlayout(ch.second->channel(), playout)) {
henrike@webrtc.org1e09a712013-07-26 19:17:59 +00002006 LOG(LS_ERROR) << "SetPlayout " << playout << " on channel "
Fredrik Solenbergaf9fb212015-08-26 10:45:53 +02002007 << ch.second->channel() << " failed";
solenberg1ac56142015-10-13 03:58:19 -07002008 return false;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002009 }
2010 }
solenberg1ac56142015-10-13 03:58:19 -07002011 playout_ = playout;
2012 return true;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002013}
2014
Taylor Brandstetter1a018dc2016-03-08 12:37:39 -08002015void WebRtcVoiceMediaChannel::SetSend(bool send) {
Peter Boströmca8b4042016-03-08 14:24:13 -08002016 TRACE_EVENT0("webrtc", "WebRtcVoiceMediaChannel::SetSend");
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002017 if (send_ == send) {
Taylor Brandstetter1a018dc2016-03-08 12:37:39 -08002018 return;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002019 }
2020
solenbergd53a3f92016-04-14 13:56:37 -07002021 // Apply channel specific options, and initialize the ADM for recording (this
2022 // may take time on some platforms, e.g. Android).
Taylor Brandstetter1a018dc2016-03-08 12:37:39 -08002023 if (send) {
solenberg63b34542015-09-29 06:06:31 -07002024 engine()->ApplyOptions(options_);
solenbergd53a3f92016-04-14 13:56:37 -07002025
2026 // InitRecording() may return an error if the ADM is already recording.
2027 if (!engine()->adm()->RecordingIsInitialized() &&
2028 !engine()->adm()->Recording()) {
2029 if (engine()->adm()->InitRecording() != 0) {
2030 LOG(LS_WARNING) << "Failed to initialize recording";
2031 }
2032 }
solenberg63b34542015-09-29 06:06:31 -07002033 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002034
wu@webrtc.org9dba5252013-08-05 20:36:57 +00002035 // Change the settings on each send channel.
Taylor Brandstetter1a018dc2016-03-08 12:37:39 -08002036 for (auto& kv : send_streams_) {
2037 kv.second->SetSend(send);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002038 }
wu@webrtc.org9dba5252013-08-05 20:36:57 +00002039
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002040 send_ = send;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002041}
2042
Peter Boström0c4e06b2015-10-07 12:23:21 +02002043bool WebRtcVoiceMediaChannel::SetAudioSend(uint32_t ssrc,
2044 bool enable,
solenberg1dd98f32015-09-10 01:57:14 -07002045 const AudioOptions* options,
Taylor Brandstetter1a018dc2016-03-08 12:37:39 -08002046 AudioSource* source) {
solenberg566ef242015-11-06 15:34:49 -08002047 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
solenberg1dd98f32015-09-10 01:57:14 -07002048 // TODO(solenberg): The state change should be fully rolled back if any one of
2049 // these calls fail.
Taylor Brandstetter1a018dc2016-03-08 12:37:39 -08002050 if (!SetLocalSource(ssrc, source)) {
solenberg1dd98f32015-09-10 01:57:14 -07002051 return false;
2052 }
solenbergdfc8f4f2015-10-01 02:31:10 -07002053 if (!MuteStream(ssrc, !enable)) {
solenberg1dd98f32015-09-10 01:57:14 -07002054 return false;
2055 }
solenbergdfc8f4f2015-10-01 02:31:10 -07002056 if (enable && options) {
solenberg1dd98f32015-09-10 01:57:14 -07002057 return SetOptions(*options);
2058 }
2059 return true;
2060}
2061
solenberg0a617e22015-10-20 15:49:38 -07002062int WebRtcVoiceMediaChannel::CreateVoEChannel() {
2063 int id = engine()->CreateVoEChannel();
2064 if (id == -1) {
2065 LOG_RTCERR0(CreateVoEChannel);
2066 return -1;
wu@webrtc.org9dba5252013-08-05 20:36:57 +00002067 }
mflodman3d7db262016-04-29 00:57:13 -07002068
solenberg0a617e22015-10-20 15:49:38 -07002069 return id;
wu@webrtc.org9dba5252013-08-05 20:36:57 +00002070}
2071
solenberg7add0582015-11-20 09:59:34 -08002072bool WebRtcVoiceMediaChannel::DeleteVoEChannel(int channel) {
wu@webrtc.org9dba5252013-08-05 20:36:57 +00002073 if (engine()->voe()->base()->DeleteChannel(channel) == -1) {
2074 LOG_RTCERR1(DeleteChannel, channel);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002075 return false;
2076 }
wu@webrtc.org9dba5252013-08-05 20:36:57 +00002077 return true;
2078}
henrike@webrtc.org1e09a712013-07-26 19:17:59 +00002079
wu@webrtc.org9dba5252013-08-05 20:36:57 +00002080bool WebRtcVoiceMediaChannel::AddSendStream(const StreamParams& sp) {
Peter Boströmca8b4042016-03-08 14:24:13 -08002081 TRACE_EVENT0("webrtc", "WebRtcVoiceMediaChannel::AddSendStream");
solenberg566ef242015-11-06 15:34:49 -08002082 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
solenberg0a617e22015-10-20 15:49:38 -07002083 LOG(LS_INFO) << "AddSendStream: " << sp.ToString();
2084
2085 uint32_t ssrc = sp.first_ssrc();
2086 RTC_DCHECK(0 != ssrc);
2087
2088 if (GetSendChannelId(ssrc) != -1) {
2089 LOG(LS_ERROR) << "Stream already exists with ssrc " << ssrc;
wu@webrtc.org9dba5252013-08-05 20:36:57 +00002090 return false;
2091 }
2092
solenberg0a617e22015-10-20 15:49:38 -07002093 // Create a new channel for sending audio data.
2094 int channel = CreateVoEChannel();
2095 if (channel == -1) {
2096 return false;
wu@webrtc.org9dba5252013-08-05 20:36:57 +00002097 }
wu@webrtc.org9dba5252013-08-05 20:36:57 +00002098
solenbergc96df772015-10-21 13:01:53 -07002099 // Save the channel to send_streams_, so that RemoveSendStream() can still
wu@webrtc.org9dba5252013-08-05 20:36:57 +00002100 // delete the channel in case failure happens below.
mallinath@webrtc.org67ee6b92014-02-03 16:57:16 +00002101 webrtc::AudioTransport* audio_transport =
2102 engine()->voe()->base()->audio_transport();
mflodman3d7db262016-04-29 00:57:13 -07002103
skvlade0d46372016-04-07 22:59:22 -07002104 WebRtcAudioSendStream* stream = new WebRtcAudioSendStream(
solenberg971cab02016-06-14 10:02:41 -07002105 channel, audio_transport, ssrc, sp.cname, send_codec_spec_,
2106 send_rtp_extensions_, call_, this);
skvlade0d46372016-04-07 22:59:22 -07002107 send_streams_.insert(std::make_pair(ssrc, stream));
wu@webrtc.org9dba5252013-08-05 20:36:57 +00002108
solenberg0a617e22015-10-20 15:49:38 -07002109 // Set the current codecs to be used for the new channel. We need to do this
2110 // after adding the channel to send_channels_, because of how max bitrate is
2111 // currently being configured by SetSendCodec().
skvlade0d46372016-04-07 22:59:22 -07002112 if (HasSendCodec() && !SetSendCodecs(channel, stream->rtp_parameters())) {
solenberg0a617e22015-10-20 15:49:38 -07002113 RemoveSendStream(ssrc);
wu@webrtc.org9dba5252013-08-05 20:36:57 +00002114 return false;
2115 }
2116
solenberg4a0f7b52016-06-16 13:07:33 -07002117 // At this point the stream's local SSRC has been updated. If it is the first
2118 // send stream, make sure that all the receive streams are updated with the
2119 // same SSRC in order to send receiver reports.
solenbergc96df772015-10-21 13:01:53 -07002120 if (send_streams_.size() == 1) {
solenberg0a617e22015-10-20 15:49:38 -07002121 receiver_reports_ssrc_ = ssrc;
solenberg4a0f7b52016-06-16 13:07:33 -07002122 for (const auto& kv : recv_streams_) {
2123 // TODO(solenberg): Allow applications to set the RTCP SSRC of receive
2124 // streams instead, so we can avoid recreating the streams here.
2125 kv.second->RecreateAudioReceiveStream(ssrc);
2126 int recv_channel = kv.second->channel();
solenberg0a617e22015-10-20 15:49:38 -07002127 engine()->voe()->base()->AssociateSendChannel(recv_channel, channel);
2128 LOG(LS_INFO) << "VoiceEngine channel #" << recv_channel
2129 << " is associated with channel #" << channel << ".";
wu@webrtc.org9dba5252013-08-05 20:36:57 +00002130 }
2131 }
2132
Taylor Brandstetter1a018dc2016-03-08 12:37:39 -08002133 send_streams_[ssrc]->SetSend(send_);
2134 return true;
wu@webrtc.org9dba5252013-08-05 20:36:57 +00002135}
2136
Peter Boström0c4e06b2015-10-07 12:23:21 +02002137bool WebRtcVoiceMediaChannel::RemoveSendStream(uint32_t ssrc) {
Peter Boströmca8b4042016-03-08 14:24:13 -08002138 TRACE_EVENT0("webrtc", "WebRtcVoiceMediaChannel::RemoveSendStream");
solenberg566ef242015-11-06 15:34:49 -08002139 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
solenberg3a941542015-11-16 07:34:50 -08002140 LOG(LS_INFO) << "RemoveSendStream: " << ssrc;
2141
solenbergc96df772015-10-21 13:01:53 -07002142 auto it = send_streams_.find(ssrc);
2143 if (it == send_streams_.end()) {
wu@webrtc.org9dba5252013-08-05 20:36:57 +00002144 LOG(LS_WARNING) << "Try to remove stream with ssrc " << ssrc
2145 << " which doesn't exist.";
2146 return false;
2147 }
2148
Taylor Brandstetter1a018dc2016-03-08 12:37:39 -08002149 it->second->SetSend(false);
wu@webrtc.org9dba5252013-08-05 20:36:57 +00002150
solenberg7add0582015-11-20 09:59:34 -08002151 // Clean up and delete the send stream+channel.
Taylor Brandstetter1a018dc2016-03-08 12:37:39 -08002152 int channel = it->second->channel();
solenberg0a617e22015-10-20 15:49:38 -07002153 LOG(LS_INFO) << "Removing audio send stream " << ssrc
2154 << " with VoiceEngine channel #" << channel << ".";
solenberg7add0582015-11-20 09:59:34 -08002155 delete it->second;
2156 send_streams_.erase(it);
2157 if (!DeleteVoEChannel(channel)) {
solenberg0a617e22015-10-20 15:49:38 -07002158 return false;
wu@webrtc.org9dba5252013-08-05 20:36:57 +00002159 }
solenbergc96df772015-10-21 13:01:53 -07002160 if (send_streams_.empty()) {
Taylor Brandstetter1a018dc2016-03-08 12:37:39 -08002161 SetSend(false);
solenberg0a617e22015-10-20 15:49:38 -07002162 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002163 return true;
2164}
2165
2166bool WebRtcVoiceMediaChannel::AddRecvStream(const StreamParams& sp) {
Peter Boströmca8b4042016-03-08 14:24:13 -08002167 TRACE_EVENT0("webrtc", "WebRtcVoiceMediaChannel::AddRecvStream");
solenberg566ef242015-11-06 15:34:49 -08002168 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
solenbergd97ec302015-10-07 01:40:33 -07002169 LOG(LS_INFO) << "AddRecvStream: " << sp.ToString();
2170
solenberg0b675462015-10-09 01:37:09 -07002171 if (!ValidateStreamParams(sp)) {
wu@webrtc.org78187522013-10-07 23:32:02 +00002172 return false;
2173 }
2174
solenberg7add0582015-11-20 09:59:34 -08002175 const uint32_t ssrc = sp.first_ssrc();
solenberg0b675462015-10-09 01:37:09 -07002176 if (ssrc == 0) {
2177 LOG(LS_WARNING) << "AddRecvStream with ssrc==0 is not supported.";
2178 return false;
2179 }
2180
solenberg1ac56142015-10-13 03:58:19 -07002181 // Remove the default receive stream if one had been created with this ssrc;
2182 // we'll recreate it then.
2183 if (IsDefaultRecvStream(ssrc)) {
2184 RemoveRecvStream(ssrc);
2185 }
solenberg0b675462015-10-09 01:37:09 -07002186
solenberg7add0582015-11-20 09:59:34 -08002187 if (GetReceiveChannelId(ssrc) != -1) {
henrike@webrtc.org1e09a712013-07-26 19:17:59 +00002188 LOG(LS_ERROR) << "Stream already exists with ssrc " << ssrc;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002189 return false;
2190 }
Fredrik Solenberg4b60c732015-05-07 14:07:48 +02002191
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002192 // Create a new channel for receiving audio data.
solenberg7add0582015-11-20 09:59:34 -08002193 const int channel = CreateVoEChannel();
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002194 if (channel == -1) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002195 return false;
2196 }
Minyue2013aec2015-05-13 14:14:42 +02002197
solenberg1ac56142015-10-13 03:58:19 -07002198 // Turn off all supported codecs.
solenberg26c8c912015-11-27 04:00:25 -08002199 // TODO(solenberg): Remove once "no codecs" is the default state of a stream.
2200 for (webrtc::CodecInst voe_codec : webrtc::acm2::RentACodec::Database()) {
2201 voe_codec.pltype = -1;
2202 if (engine()->voe()->codec()->SetRecPayloadType(channel, voe_codec) == -1) {
2203 LOG_RTCERR2(SetRecPayloadType, channel, ToString(voe_codec));
2204 DeleteVoEChannel(channel);
2205 return false;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002206 }
2207 }
2208
solenberg1ac56142015-10-13 03:58:19 -07002209 // Only enable those configured for this channel.
2210 for (const auto& codec : recv_codecs_) {
solenberg72e29d22016-03-08 06:35:16 -08002211 webrtc::CodecInst voe_codec = {0};
solenberg26c8c912015-11-27 04:00:25 -08002212 if (WebRtcVoiceEngine::ToCodecInst(codec, &voe_codec)) {
solenberg1ac56142015-10-13 03:58:19 -07002213 voe_codec.pltype = codec.id;
2214 if (engine()->voe()->codec()->SetRecPayloadType(
2215 channel, voe_codec) == -1) {
2216 LOG_RTCERR2(SetRecPayloadType, channel, ToString(voe_codec));
solenberg7add0582015-11-20 09:59:34 -08002217 DeleteVoEChannel(channel);
solenberg1ac56142015-10-13 03:58:19 -07002218 return false;
2219 }
henrike@webrtc.org1e09a712013-07-26 19:17:59 +00002220 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002221 }
solenberg8fb30c32015-10-13 03:06:58 -07002222
solenberg7add0582015-11-20 09:59:34 -08002223 const int send_channel = GetSendChannelId(receiver_reports_ssrc_);
2224 if (send_channel != -1) {
2225 // Associate receive channel with first send channel (so the receive channel
2226 // can obtain RTT from the send channel)
2227 engine()->voe()->base()->AssociateSendChannel(channel, send_channel);
2228 LOG(LS_INFO) << "VoiceEngine channel #" << channel
2229 << " is associated with channel #" << send_channel << ".";
buildbot@webrtc.org150835e2014-05-06 15:54:38 +00002230 }
2231
stefanba4c0e42016-02-04 04:12:24 -08002232 recv_streams_.insert(std::make_pair(
2233 ssrc, new WebRtcAudioReceiveStream(channel, ssrc, receiver_reports_ssrc_,
solenberg72e29d22016-03-08 06:35:16 -08002234 recv_transport_cc_enabled_,
solenberg8189b022016-06-14 12:13:00 -07002235 recv_nack_enabled_,
solenberg72e29d22016-03-08 06:35:16 -08002236 sp.sync_label, recv_rtp_extensions_,
ossu29b1a8d2016-06-13 07:34:51 -07002237 call_, this,
2238 engine()->decoder_factory_)));
solenberg1ac56142015-10-13 03:58:19 -07002239 SetPlayout(channel, playout_);
solenberg7add0582015-11-20 09:59:34 -08002240
solenberg1ac56142015-10-13 03:58:19 -07002241 return true;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002242}
2243
Peter Boström0c4e06b2015-10-07 12:23:21 +02002244bool WebRtcVoiceMediaChannel::RemoveRecvStream(uint32_t ssrc) {
Peter Boströmca8b4042016-03-08 14:24:13 -08002245 TRACE_EVENT0("webrtc", "WebRtcVoiceMediaChannel::RemoveRecvStream");
solenberg566ef242015-11-06 15:34:49 -08002246 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
solenbergd97ec302015-10-07 01:40:33 -07002247 LOG(LS_INFO) << "RemoveRecvStream: " << ssrc;
2248
solenberg7add0582015-11-20 09:59:34 -08002249 const auto it = recv_streams_.find(ssrc);
2250 if (it == recv_streams_.end()) {
wu@webrtc.org9dba5252013-08-05 20:36:57 +00002251 LOG(LS_WARNING) << "Try to remove stream with ssrc " << ssrc
2252 << " which doesn't exist.";
henrike@webrtc.org1e09a712013-07-26 19:17:59 +00002253 return false;
wu@webrtc.org9dba5252013-08-05 20:36:57 +00002254 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002255
solenberg1ac56142015-10-13 03:58:19 -07002256 // Deregister default channel, if that's the one being destroyed.
2257 if (IsDefaultRecvStream(ssrc)) {
2258 default_recv_ssrc_ = -1;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002259 }
henrike@webrtc.org1e09a712013-07-26 19:17:59 +00002260
solenberg7add0582015-11-20 09:59:34 -08002261 const int channel = it->second->channel();
2262
2263 // Clean up and delete the receive stream+channel.
2264 LOG(LS_INFO) << "Removing audio receive stream " << ssrc
mallinath@webrtc.org67ee6b92014-02-03 16:57:16 +00002265 << " with VoiceEngine channel #" << channel << ".";
Tommif888bb52015-12-12 01:37:01 +01002266 it->second->SetRawAudioSink(nullptr);
solenberg7add0582015-11-20 09:59:34 -08002267 delete it->second;
2268 recv_streams_.erase(it);
2269 return DeleteVoEChannel(channel);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002270}
2271
Taylor Brandstetter1a018dc2016-03-08 12:37:39 -08002272bool WebRtcVoiceMediaChannel::SetLocalSource(uint32_t ssrc,
2273 AudioSource* source) {
solenbergc96df772015-10-21 13:01:53 -07002274 auto it = send_streams_.find(ssrc);
2275 if (it == send_streams_.end()) {
Taylor Brandstetter1a018dc2016-03-08 12:37:39 -08002276 if (source) {
2277 // Return an error if trying to set a valid source with an invalid ssrc.
2278 LOG(LS_ERROR) << "SetLocalSource failed with ssrc " << ssrc;
wu@webrtc.org9dba5252013-08-05 20:36:57 +00002279 return false;
2280 }
2281
2282 // The channel likely has gone away, do nothing.
henrike@webrtc.org1e09a712013-07-26 19:17:59 +00002283 return true;
henrike@webrtc.org1e09a712013-07-26 19:17:59 +00002284 }
2285
Taylor Brandstetter1a018dc2016-03-08 12:37:39 -08002286 if (source) {
2287 it->second->SetSource(source);
solenberg1ac56142015-10-13 03:58:19 -07002288 } else {
Taylor Brandstetter1a018dc2016-03-08 12:37:39 -08002289 it->second->ClearSource();
solenberg1ac56142015-10-13 03:58:19 -07002290 }
henrike@webrtc.org1e09a712013-07-26 19:17:59 +00002291
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002292 return true;
2293}
2294
2295bool WebRtcVoiceMediaChannel::GetActiveStreams(
2296 AudioInfo::StreamList* actives) {
solenberg566ef242015-11-06 15:34:49 -08002297 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002298 actives->clear();
solenberg7add0582015-11-20 09:59:34 -08002299 for (const auto& ch : recv_streams_) {
Fredrik Solenbergaf9fb212015-08-26 10:45:53 +02002300 int level = GetOutputLevel(ch.second->channel());
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002301 if (level > 0) {
Fredrik Solenbergaf9fb212015-08-26 10:45:53 +02002302 actives->push_back(std::make_pair(ch.first, level));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002303 }
2304 }
2305 return true;
2306}
2307
2308int WebRtcVoiceMediaChannel::GetOutputLevel() {
solenberg566ef242015-11-06 15:34:49 -08002309 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
solenberg1ac56142015-10-13 03:58:19 -07002310 int highest = 0;
solenberg7add0582015-11-20 09:59:34 -08002311 for (const auto& ch : recv_streams_) {
solenberg8fb30c32015-10-13 03:06:58 -07002312 highest = std::max(GetOutputLevel(ch.second->channel()), highest);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002313 }
2314 return highest;
2315}
2316
2317int WebRtcVoiceMediaChannel::GetTimeSinceLastTyping() {
2318 int ret;
2319 if (engine()->voe()->processing()->TimeSinceLastTyping(ret) == -1) {
2320 // In case of error, log the info and continue
2321 LOG_RTCERR0(TimeSinceLastTyping);
2322 ret = -1;
2323 } else {
2324 ret *= 1000; // We return ms, webrtc returns seconds.
2325 }
2326 return ret;
2327}
2328
2329void WebRtcVoiceMediaChannel::SetTypingDetectionParameters(int time_window,
2330 int cost_per_typing, int reporting_threshold, int penalty_decay,
2331 int type_event_delay) {
2332 if (engine()->voe()->processing()->SetTypingDetectionParameters(
2333 time_window, cost_per_typing,
2334 reporting_threshold, penalty_decay, type_event_delay) == -1) {
2335 // In case of error, log the info and continue
2336 LOG_RTCERR5(SetTypingDetectionParameters, time_window,
2337 cost_per_typing, reporting_threshold, penalty_decay,
2338 type_event_delay);
2339 }
2340}
2341
solenberg4bac9c52015-10-09 02:32:53 -07002342bool WebRtcVoiceMediaChannel::SetOutputVolume(uint32_t ssrc, double volume) {
solenberg566ef242015-11-06 15:34:49 -08002343 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
solenberg1ac56142015-10-13 03:58:19 -07002344 if (ssrc == 0) {
2345 default_recv_volume_ = volume;
2346 if (default_recv_ssrc_ == -1) {
2347 return true;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002348 }
solenberg1ac56142015-10-13 03:58:19 -07002349 ssrc = static_cast<uint32_t>(default_recv_ssrc_);
2350 }
solenberg217fb662016-06-17 08:30:54 -07002351 const auto it = recv_streams_.find(ssrc);
2352 if (it == recv_streams_.end()) {
2353 LOG(LS_WARNING) << "SetOutputVolume: no recv stream" << ssrc;
solenberg1ac56142015-10-13 03:58:19 -07002354 return false;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002355 }
solenberg217fb662016-06-17 08:30:54 -07002356 it->second->SetOutputVolume(volume);
2357 LOG(LS_INFO) << "SetOutputVolume() to " << volume
2358 << " for recv stream with ssrc " << ssrc;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002359 return true;
2360}
2361
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002362bool WebRtcVoiceMediaChannel::CanInsertDtmf() {
Fredrik Solenbergb5727682015-12-04 15:22:19 +01002363 return dtmf_payload_type_ ? true : false;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002364}
2365
solenberg1d63dd02015-12-02 12:35:09 -08002366bool WebRtcVoiceMediaChannel::InsertDtmf(uint32_t ssrc, int event,
2367 int duration) {
solenberg566ef242015-11-06 15:34:49 -08002368 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
Fredrik Solenbergb5727682015-12-04 15:22:19 +01002369 LOG(LS_INFO) << "WebRtcVoiceMediaChannel::InsertDtmf";
2370 if (!dtmf_payload_type_) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002371 return false;
2372 }
2373
Fredrik Solenbergb5727682015-12-04 15:22:19 +01002374 // Figure out which WebRtcAudioSendStream to send the event on.
2375 auto it = ssrc != 0 ? send_streams_.find(ssrc) : send_streams_.begin();
2376 if (it == send_streams_.end()) {
2377 LOG(LS_WARNING) << "The specified ssrc " << ssrc << " is not in use.";
solenberg1d63dd02015-12-02 12:35:09 -08002378 return false;
2379 }
Fredrik Solenbergb5727682015-12-04 15:22:19 +01002380 if (event < kMinTelephoneEventCode ||
2381 event > kMaxTelephoneEventCode) {
2382 LOG(LS_WARNING) << "DTMF event code " << event << " out of range.";
solenberg1d63dd02015-12-02 12:35:09 -08002383 return false;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002384 }
Fredrik Solenbergb5727682015-12-04 15:22:19 +01002385 if (duration < kMinTelephoneEventDuration ||
2386 duration > kMaxTelephoneEventDuration) {
2387 LOG(LS_WARNING) << "DTMF event duration " << duration << " out of range.";
2388 return false;
2389 }
2390 return it->second->SendTelephoneEvent(*dtmf_payload_type_, event, duration);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002391}
2392
wu@webrtc.orga9890802013-12-13 00:21:03 +00002393void WebRtcVoiceMediaChannel::OnPacketReceived(
jbaucheec21bd2016-03-20 06:15:43 -07002394 rtc::CopyOnWriteBuffer* packet, const rtc::PacketTime& packet_time) {
solenberg566ef242015-11-06 15:34:49 -08002395 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
Fredrik Solenberg4b60c732015-05-07 14:07:48 +02002396
mflodman3d7db262016-04-29 00:57:13 -07002397 const webrtc::PacketTime webrtc_packet_time(packet_time.timestamp,
2398 packet_time.not_before);
2399 webrtc::PacketReceiver::DeliveryStatus delivery_result =
2400 call_->Receiver()->DeliverPacket(webrtc::MediaType::AUDIO,
2401 packet->cdata(), packet->size(),
2402 webrtc_packet_time);
mflodman3d7db262016-04-29 00:57:13 -07002403 if (delivery_result != webrtc::PacketReceiver::DELIVERY_UNKNOWN_SSRC) {
2404 return;
2405 }
2406
2407 // Create a default receive stream for this unsignalled and previously not
2408 // received ssrc. If there already is a default receive stream, delete it.
2409 // See: https://bugs.chromium.org/p/webrtc/issues/detail?id=5208
solenberg1ac56142015-10-13 03:58:19 -07002410 uint32_t ssrc = 0;
jbaucheec21bd2016-03-20 06:15:43 -07002411 if (!GetRtpSsrc(packet->cdata(), packet->size(), &ssrc)) {
solenberg1ac56142015-10-13 03:58:19 -07002412 return;
2413 }
2414
mflodman3d7db262016-04-29 00:57:13 -07002415 if (default_recv_ssrc_ != -1) {
2416 LOG(LS_INFO) << "Removing default receive stream with ssrc "
2417 << default_recv_ssrc_;
2418 RTC_DCHECK_NE(ssrc, default_recv_ssrc_);
2419 RemoveRecvStream(default_recv_ssrc_);
2420 default_recv_ssrc_ = -1;
solenberg1ac56142015-10-13 03:58:19 -07002421 }
2422
mflodman3d7db262016-04-29 00:57:13 -07002423 StreamParams sp;
2424 sp.ssrcs.push_back(ssrc);
2425 LOG(LS_INFO) << "Creating default receive stream for SSRC=" << ssrc << ".";
2426 if (!AddRecvStream(sp)) {
2427 LOG(LS_WARNING) << "Could not create default receive stream.";
2428 return;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002429 }
mflodman3d7db262016-04-29 00:57:13 -07002430 default_recv_ssrc_ = ssrc;
2431 SetOutputVolume(default_recv_ssrc_, default_recv_volume_);
2432 if (default_sink_) {
2433 std::unique_ptr<webrtc::AudioSinkInterface> proxy_sink(
2434 new ProxySink(default_sink_.get()));
2435 SetRawAudioSink(default_recv_ssrc_, std::move(proxy_sink));
2436 }
2437 delivery_result = call_->Receiver()->DeliverPacket(webrtc::MediaType::AUDIO,
2438 packet->cdata(),
2439 packet->size(),
2440 webrtc_packet_time);
2441 RTC_DCHECK_NE(webrtc::PacketReceiver::DELIVERY_UNKNOWN_SSRC, delivery_result);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002442}
2443
wu@webrtc.orga9890802013-12-13 00:21:03 +00002444void WebRtcVoiceMediaChannel::OnRtcpReceived(
jbaucheec21bd2016-03-20 06:15:43 -07002445 rtc::CopyOnWriteBuffer* packet, const rtc::PacketTime& packet_time) {
solenberg566ef242015-11-06 15:34:49 -08002446 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
Fredrik Solenberg4b60c732015-05-07 14:07:48 +02002447
Fredrik Solenberg709ed672015-09-15 12:26:33 +02002448 // Forward packet to Call as well.
2449 const webrtc::PacketTime webrtc_packet_time(packet_time.timestamp,
2450 packet_time.not_before);
2451 call_->Receiver()->DeliverPacket(webrtc::MediaType::AUDIO,
jbaucheec21bd2016-03-20 06:15:43 -07002452 packet->cdata(), packet->size(), webrtc_packet_time);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002453}
2454
Honghai Zhangcc411c02016-03-29 17:27:21 -07002455void WebRtcVoiceMediaChannel::OnNetworkRouteChanged(
2456 const std::string& transport_name,
Honghai Zhang0e533ef2016-04-19 15:41:36 -07002457 const rtc::NetworkRoute& network_route) {
2458 call_->OnNetworkRouteChanged(transport_name, network_route);
Honghai Zhangcc411c02016-03-29 17:27:21 -07002459}
2460
Peter Boström0c4e06b2015-10-07 12:23:21 +02002461bool WebRtcVoiceMediaChannel::MuteStream(uint32_t ssrc, bool muted) {
solenberg566ef242015-11-06 15:34:49 -08002462 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
solenberg94218532016-06-16 10:53:22 -07002463 const auto it = send_streams_.find(ssrc);
2464 if (it == send_streams_.end()) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002465 LOG(LS_WARNING) << "The specified ssrc " << ssrc << " is not in use.";
2466 return false;
2467 }
solenberg94218532016-06-16 10:53:22 -07002468 it->second->SetMuted(muted);
2469
2470 // TODO(solenberg):
buildbot@webrtc.org6b21b712014-07-31 15:08:53 +00002471 // We set the AGC to mute state only when all the channels are muted.
2472 // This implementation is not ideal, instead we should signal the AGC when
2473 // the mic channel is muted/unmuted. We can't do it today because there
2474 // is no good way to know which stream is mapping to the mic channel.
2475 bool all_muted = muted;
solenberg94218532016-06-16 10:53:22 -07002476 for (const auto& kv : send_streams_) {
2477 all_muted = all_muted && kv.second->muted();
buildbot@webrtc.org6b21b712014-07-31 15:08:53 +00002478 }
2479
2480 webrtc::AudioProcessing* ap = engine()->voe()->base()->audio_processing();
solenberg0a617e22015-10-20 15:49:38 -07002481 if (ap) {
buildbot@webrtc.org6b21b712014-07-31 15:08:53 +00002482 ap->set_output_will_be_muted(all_muted);
solenberg0a617e22015-10-20 15:49:38 -07002483 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002484 return true;
2485}
2486
deadbeef80346142016-04-27 14:17:10 -07002487bool WebRtcVoiceMediaChannel::SetMaxSendBitrate(int bps) {
2488 LOG(LS_INFO) << "WebRtcVoiceMediaChannel::SetMaxSendBitrate.";
2489 max_send_bitrate_bps_ = bps;
skvlade0d46372016-04-07 22:59:22 -07002490
2491 for (const auto& kv : send_streams_) {
Taylor Brandstetterdb0cd9e2016-05-16 11:40:30 -07002492 if (!SetChannelSendParameters(kv.second->channel(),
2493 kv.second->rtp_parameters())) {
skvlade0d46372016-04-07 22:59:22 -07002494 return false;
2495 }
2496 }
2497 return true;
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00002498}
2499
Taylor Brandstetterdb0cd9e2016-05-16 11:40:30 -07002500bool WebRtcVoiceMediaChannel::SetChannelSendParameters(
skvlade0d46372016-04-07 22:59:22 -07002501 int channel,
2502 const webrtc::RtpParameters& parameters) {
2503 RTC_CHECK_EQ(1UL, parameters.encodings.size());
Taylor Brandstetter0cd086b2016-04-20 16:23:10 -07002504 // TODO(deadbeef): Handle setting parameters with a list of codecs in a
2505 // different order (which should change the send codec).
deadbeef80346142016-04-27 14:17:10 -07002506 return SetMaxSendBitrate(
2507 channel, MinPositive(max_send_bitrate_bps_,
2508 parameters.encodings[0].max_bitrate_bps));
skvlade0d46372016-04-07 22:59:22 -07002509}
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00002510
deadbeef80346142016-04-27 14:17:10 -07002511bool WebRtcVoiceMediaChannel::SetMaxSendBitrate(int channel, int bps) {
skvlade0d46372016-04-07 22:59:22 -07002512 // Bitrate is auto by default.
2513 // TODO(bemasc): Fix this so that if SetMaxSendBandwidth(50) is followed by
2514 // SetMaxSendBandwith(0), the second call removes the previous limit.
deadbeef80346142016-04-27 14:17:10 -07002515 if (bps <= 0) {
skvlade0d46372016-04-07 22:59:22 -07002516 return true;
deadbeef80346142016-04-27 14:17:10 -07002517 }
wu@webrtc.org1d1ffc92013-10-16 18:12:02 +00002518
solenberg72e29d22016-03-08 06:35:16 -08002519 if (!HasSendCodec()) {
wu@webrtc.org1d1ffc92013-10-16 18:12:02 +00002520 LOG(LS_INFO) << "The send codec has not been set up yet. "
minyue@webrtc.org26236952014-10-29 02:27:08 +00002521 << "The send bitrate setting will be applied later.";
wu@webrtc.org1d1ffc92013-10-16 18:12:02 +00002522 return true;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002523 }
2524
solenberg72e29d22016-03-08 06:35:16 -08002525 webrtc::CodecInst codec = send_codec_spec_.codec_inst;
solenberg26c8c912015-11-27 04:00:25 -08002526 bool is_multi_rate = WebRtcVoiceCodecs::IsCodecMultiRate(codec);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002527
2528 if (is_multi_rate) {
2529 // If codec is multi-rate then just set the bitrate.
deadbeef80346142016-04-27 14:17:10 -07002530 int max_bitrate_bps = WebRtcVoiceCodecs::MaxBitrateBps(codec);
2531 codec.rate = std::min(bps, max_bitrate_bps);
2532 LOG(LS_INFO) << "Setting codec " << codec.plname << " to bitrate " << bps
2533 << " bps.";
skvlade0d46372016-04-07 22:59:22 -07002534 if (!SetSendCodec(channel, codec)) {
deadbeef80346142016-04-27 14:17:10 -07002535 LOG(LS_ERROR) << "Failed to set codec " << codec.plname << " to bitrate "
2536 << bps << " bps.";
skvlade0d46372016-04-07 22:59:22 -07002537 return false;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002538 }
2539 return true;
2540 } else {
2541 // If codec is not multi-rate and |bps| is less than the fixed bitrate
2542 // then fail. If codec is not multi-rate and |bps| exceeds or equal the
2543 // fixed bitrate then ignore.
2544 if (bps < codec.rate) {
deadbeef80346142016-04-27 14:17:10 -07002545 LOG(LS_ERROR) << "Failed to set codec " << codec.plname << " to bitrate "
2546 << bps << " bps"
2547 << ", requires at least " << codec.rate << " bps.";
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002548 return false;
2549 }
2550 return true;
2551 }
2552}
2553
skvlad7a43d252016-03-22 15:32:27 -07002554void WebRtcVoiceMediaChannel::OnReadyToSend(bool ready) {
2555 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
2556 LOG(LS_VERBOSE) << "OnReadyToSend: " << (ready ? "Ready." : "Not ready.");
2557 call_->SignalChannelNetworkState(
2558 webrtc::MediaType::AUDIO,
2559 ready ? webrtc::kNetworkUp : webrtc::kNetworkDown);
2560}
2561
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002562bool WebRtcVoiceMediaChannel::GetStats(VoiceMediaInfo* info) {
Peter Boströmca8b4042016-03-08 14:24:13 -08002563 TRACE_EVENT0("webrtc", "WebRtcVoiceMediaChannel::GetStats");
solenberg566ef242015-11-06 15:34:49 -08002564 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
solenberg85a04962015-10-27 03:35:21 -07002565 RTC_DCHECK(info);
solenbergd97ec302015-10-07 01:40:33 -07002566
solenberg85a04962015-10-27 03:35:21 -07002567 // Get SSRC and stats for each sender.
2568 RTC_DCHECK(info->senders.size() == 0);
2569 for (const auto& stream : send_streams_) {
2570 webrtc::AudioSendStream::Stats stats = stream.second->GetStats();
wu@webrtc.org9dba5252013-08-05 20:36:57 +00002571 VoiceSenderInfo sinfo;
solenberg85a04962015-10-27 03:35:21 -07002572 sinfo.add_ssrc(stats.local_ssrc);
2573 sinfo.bytes_sent = stats.bytes_sent;
2574 sinfo.packets_sent = stats.packets_sent;
2575 sinfo.packets_lost = stats.packets_lost;
2576 sinfo.fraction_lost = stats.fraction_lost;
2577 sinfo.codec_name = stats.codec_name;
2578 sinfo.ext_seqnum = stats.ext_seqnum;
2579 sinfo.jitter_ms = stats.jitter_ms;
2580 sinfo.rtt_ms = stats.rtt_ms;
2581 sinfo.audio_level = stats.audio_level;
2582 sinfo.aec_quality_min = stats.aec_quality_min;
2583 sinfo.echo_delay_median_ms = stats.echo_delay_median_ms;
2584 sinfo.echo_delay_std_ms = stats.echo_delay_std_ms;
2585 sinfo.echo_return_loss = stats.echo_return_loss;
2586 sinfo.echo_return_loss_enhancement = stats.echo_return_loss_enhancement;
Taylor Brandstetter1a018dc2016-03-08 12:37:39 -08002587 sinfo.typing_noise_detected = (send_ ? stats.typing_noise_detected : false);
wu@webrtc.org9dba5252013-08-05 20:36:57 +00002588 info->senders.push_back(sinfo);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002589 }
2590
solenberg85a04962015-10-27 03:35:21 -07002591 // Get SSRC and stats for each receiver.
2592 RTC_DCHECK(info->receivers.size() == 0);
solenberg7add0582015-11-20 09:59:34 -08002593 for (const auto& stream : recv_streams_) {
Fredrik Solenberg4f4ec0a2015-10-22 10:49:27 +02002594 webrtc::AudioReceiveStream::Stats stats = stream.second->GetStats();
2595 VoiceReceiverInfo rinfo;
2596 rinfo.add_ssrc(stats.remote_ssrc);
2597 rinfo.bytes_rcvd = stats.bytes_rcvd;
2598 rinfo.packets_rcvd = stats.packets_rcvd;
2599 rinfo.packets_lost = stats.packets_lost;
2600 rinfo.fraction_lost = stats.fraction_lost;
2601 rinfo.codec_name = stats.codec_name;
2602 rinfo.ext_seqnum = stats.ext_seqnum;
2603 rinfo.jitter_ms = stats.jitter_ms;
2604 rinfo.jitter_buffer_ms = stats.jitter_buffer_ms;
2605 rinfo.jitter_buffer_preferred_ms = stats.jitter_buffer_preferred_ms;
2606 rinfo.delay_estimate_ms = stats.delay_estimate_ms;
2607 rinfo.audio_level = stats.audio_level;
2608 rinfo.expand_rate = stats.expand_rate;
2609 rinfo.speech_expand_rate = stats.speech_expand_rate;
2610 rinfo.secondary_decoded_rate = stats.secondary_decoded_rate;
2611 rinfo.accelerate_rate = stats.accelerate_rate;
2612 rinfo.preemptive_expand_rate = stats.preemptive_expand_rate;
2613 rinfo.decoding_calls_to_silence_generator =
2614 stats.decoding_calls_to_silence_generator;
2615 rinfo.decoding_calls_to_neteq = stats.decoding_calls_to_neteq;
2616 rinfo.decoding_normal = stats.decoding_normal;
2617 rinfo.decoding_plc = stats.decoding_plc;
2618 rinfo.decoding_cng = stats.decoding_cng;
2619 rinfo.decoding_plc_cng = stats.decoding_plc_cng;
2620 rinfo.capture_start_ntp_time_ms = stats.capture_start_ntp_time_ms;
2621 info->receivers.push_back(rinfo);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002622 }
2623
2624 return true;
2625}
2626
Tommif888bb52015-12-12 01:37:01 +01002627void WebRtcVoiceMediaChannel::SetRawAudioSink(
2628 uint32_t ssrc,
kwiberg686a8ef2016-02-26 03:00:35 -08002629 std::unique_ptr<webrtc::AudioSinkInterface> sink) {
Tommif888bb52015-12-12 01:37:01 +01002630 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
deadbeef884f5852016-01-15 09:20:04 -08002631 LOG(LS_VERBOSE) << "WebRtcVoiceMediaChannel::SetRawAudioSink: ssrc:" << ssrc
2632 << " " << (sink ? "(ptr)" : "NULL");
2633 if (ssrc == 0) {
2634 if (default_recv_ssrc_ != -1) {
kwiberg686a8ef2016-02-26 03:00:35 -08002635 std::unique_ptr<webrtc::AudioSinkInterface> proxy_sink(
deadbeef884f5852016-01-15 09:20:04 -08002636 sink ? new ProxySink(sink.get()) : nullptr);
2637 SetRawAudioSink(default_recv_ssrc_, std::move(proxy_sink));
2638 }
2639 default_sink_ = std::move(sink);
2640 return;
2641 }
Tommif888bb52015-12-12 01:37:01 +01002642 const auto it = recv_streams_.find(ssrc);
2643 if (it == recv_streams_.end()) {
2644 LOG(LS_WARNING) << "SetRawAudioSink: no recv stream" << ssrc;
2645 return;
2646 }
deadbeef2d110be2016-01-13 12:00:26 -08002647 it->second->SetRawAudioSink(std::move(sink));
Tommif888bb52015-12-12 01:37:01 +01002648}
2649
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002650int WebRtcVoiceMediaChannel::GetOutputLevel(int channel) {
solenbergd97ec302015-10-07 01:40:33 -07002651 unsigned int ulevel = 0;
2652 int ret = engine()->voe()->volume()->GetSpeechOutputLevel(channel, ulevel);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002653 return (ret == 0) ? static_cast<int>(ulevel) : -1;
2654}
2655
Peter Boström0c4e06b2015-10-07 12:23:21 +02002656int WebRtcVoiceMediaChannel::GetReceiveChannelId(uint32_t ssrc) const {
solenberg566ef242015-11-06 15:34:49 -08002657 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
solenberg7add0582015-11-20 09:59:34 -08002658 const auto it = recv_streams_.find(ssrc);
2659 if (it != recv_streams_.end()) {
mallinath@webrtc.org67ee6b92014-02-03 16:57:16 +00002660 return it->second->channel();
solenberg8fb30c32015-10-13 03:06:58 -07002661 }
solenberg1ac56142015-10-13 03:58:19 -07002662 return -1;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002663}
2664
Peter Boström0c4e06b2015-10-07 12:23:21 +02002665int WebRtcVoiceMediaChannel::GetSendChannelId(uint32_t ssrc) const {
solenberg566ef242015-11-06 15:34:49 -08002666 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
solenbergc96df772015-10-21 13:01:53 -07002667 const auto it = send_streams_.find(ssrc);
2668 if (it != send_streams_.end()) {
mallinath@webrtc.org67ee6b92014-02-03 16:57:16 +00002669 return it->second->channel();
solenberg8fb30c32015-10-13 03:06:58 -07002670 }
wu@webrtc.org9dba5252013-08-05 20:36:57 +00002671 return -1;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002672}
2673
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002674bool WebRtcVoiceMediaChannel::SetPlayout(int channel, bool playout) {
2675 if (playout) {
2676 LOG(LS_INFO) << "Starting playout for channel #" << channel;
2677 if (engine()->voe()->base()->StartPlayout(channel) == -1) {
2678 LOG_RTCERR1(StartPlayout, channel);
2679 return false;
2680 }
2681 } else {
2682 LOG(LS_INFO) << "Stopping playout for channel #" << channel;
2683 engine()->voe()->base()->StopPlayout(channel);
2684 }
2685 return true;
2686}
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002687} // namespace cricket
2688
2689#endif // HAVE_WEBRTC_VOICE