blob: 358c142a1b01bea7ba3a977f55279a78cc45221f [file] [log] [blame]
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001/*
kjellander1afca732016-02-07 20:46:45 -08002 * Copyright (c) 2004 The WebRTC project authors. All Rights Reserved.
henrike@webrtc.org28e20752013-07-10 00:45:36 +00003 *
kjellander1afca732016-02-07 20:46:45 -08004 * Use of this source code is governed by a BSD-style license
5 * that can be found in the LICENSE file in the root of the source
6 * tree. An additional intellectual property rights grant can be found
7 * in the file PATENTS. All contributing project authors may
8 * be found in the AUTHORS file in the root of the source tree.
henrike@webrtc.org28e20752013-07-10 00:45:36 +00009 */
10
henrike@webrtc.org28e20752013-07-10 00:45:36 +000011#ifdef HAVE_WEBRTC_VOICE
12
kjellander@webrtc.org5ad12972016-02-12 06:39:40 +010013#include "webrtc/media/engine/webrtcvoiceengine.h"
henrike@webrtc.org28e20752013-07-10 00:45:36 +000014
15#include <algorithm>
16#include <cstdio>
ossuc54071d2016-08-17 02:45:41 -070017#include <functional>
henrike@webrtc.org28e20752013-07-10 00:45:36 +000018#include <string>
19#include <vector>
20
kjellandera69d9732016-08-31 07:33:05 -070021#include "webrtc/api/call/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"
solenberg347ec5c2016-09-23 04:21:47 -070029#include "webrtc/base/race_checker.h"
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +000030#include "webrtc/base/stringencode.h"
31#include "webrtc/base/stringutils.h"
Peter Boströmca8b4042016-03-08 14:24:13 -080032#include "webrtc/base/trace_event.h"
Taylor Brandstetter1a018dc2016-03-08 12:37:39 -080033#include "webrtc/media/base/audiosource.h"
kjellanderf4752772016-03-02 05:42:30 -080034#include "webrtc/media/base/mediaconstants.h"
kjellandera96e2d72016-02-04 23:52:28 -080035#include "webrtc/media/base/streamparams.h"
ossuc54071d2016-08-17 02:45:41 -070036#include "webrtc/media/engine/payload_type_mapper.h"
kjellander@webrtc.org5ad12972016-02-12 06:39:40 +010037#include "webrtc/media/engine/webrtcmediaengine.h"
38#include "webrtc/media/engine/webrtcvoe.h"
solenberg26c8c912015-11-27 04:00:25 -080039#include "webrtc/modules/audio_coding/acm2/rent_a_codec.h"
henrike@webrtc.org28e20752013-07-10 00:45:36 +000040#include "webrtc/modules/audio_processing/include/audio_processing.h"
Henrik Kjellander98f53512015-10-28 18:17:40 +010041#include "webrtc/system_wrappers/include/field_trial.h"
solenbergbd138382015-11-20 16:08:07 -080042#include "webrtc/system_wrappers/include/trace.h"
henrike@webrtc.org28e20752013-07-10 00:45:36 +000043
henrike@webrtc.org28e20752013-07-10 00:45:36 +000044namespace cricket {
solenbergd97ec302015-10-07 01:40:33 -070045namespace {
henrike@webrtc.org28e20752013-07-10 00:45:36 +000046
solenbergbd138382015-11-20 16:08:07 -080047const int kDefaultTraceFilter = webrtc::kTraceNone | webrtc::kTraceTerseInfo |
48 webrtc::kTraceWarning | webrtc::kTraceError |
49 webrtc::kTraceCritical;
50const int kElevatedTraceFilter = kDefaultTraceFilter | webrtc::kTraceStateInfo |
51 webrtc::kTraceInfo;
52
henrike@webrtc.org28e20752013-07-10 00:45:36 +000053// On Windows Vista and newer, Microsoft introduced the concept of "Default
54// Communications Device". This means that there are two types of default
55// devices (old Wave Audio style default and Default Communications Device).
56//
57// On Windows systems which only support Wave Audio style default, uses either
58// -1 or 0 to select the default device.
henrike@webrtc.org28e20752013-07-10 00:45:36 +000059#ifdef WIN32
solenbergd97ec302015-10-07 01:40:33 -070060const int kDefaultAudioDeviceId = -1;
solenberg8ad582d2016-03-16 09:34:56 -070061#elif !defined(WEBRTC_IOS)
solenbergd97ec302015-10-07 01:40:33 -070062const int kDefaultAudioDeviceId = 0;
henrike@webrtc.org28e20752013-07-10 00:45:36 +000063#endif
64
solenberg971cab02016-06-14 10:02:41 -070065constexpr int kNackRtpHistoryMs = 5000;
minyue@webrtc.org2dc6f312014-10-31 05:33:10 +000066
peah1bcfce52016-08-26 07:16:04 -070067// Check to verify that the define for the intelligibility enhancer is properly
68// set.
69#if !defined(WEBRTC_INTELLIGIBILITY_ENHANCER) || \
70 (WEBRTC_INTELLIGIBILITY_ENHANCER != 0 && \
71 WEBRTC_INTELLIGIBILITY_ENHANCER != 1)
72#error "Set WEBRTC_INTELLIGIBILITY_ENHANCER to either 0 or 1"
73#endif
74
minyue@webrtc.org2dc6f312014-10-31 05:33:10 +000075// Codec parameters for Opus.
henrike@webrtc.org1e09a712013-07-26 19:17:59 +000076// draft-spittka-payload-rtp-opus-03
minyue@webrtc.org2dc6f312014-10-31 05:33:10 +000077
78// Recommended bitrates:
79// 8-12 kb/s for NB speech,
80// 16-20 kb/s for WB speech,
81// 28-40 kb/s for FB speech,
82// 48-64 kb/s for FB mono music, and
83// 64-128 kb/s for FB stereo music.
84// The current implementation applies the following values to mono signals,
85// and multiplies them by 2 for stereo.
solenbergd97ec302015-10-07 01:40:33 -070086const int kOpusBitrateNb = 12000;
87const int kOpusBitrateWb = 20000;
88const int kOpusBitrateFb = 32000;
minyue@webrtc.org2dc6f312014-10-31 05:33:10 +000089
henrike@webrtc.org1e09a712013-07-26 19:17:59 +000090// Opus bitrate should be in the range between 6000 and 510000.
solenbergd97ec302015-10-07 01:40:33 -070091const int kOpusMinBitrate = 6000;
92const int kOpusMaxBitrate = 510000;
buildbot@webrtc.org5d639b32014-09-10 07:57:12 +000093
deadbeef80346142016-04-27 14:17:10 -070094// iSAC bitrate should be <= 56000.
95const int kIsacMaxBitrate = 56000;
96
wu@webrtc.orgde305012013-10-31 15:40:38 +000097// Default audio dscp value.
98// See http://tools.ietf.org/html/rfc2474 for details.
99// See also http://tools.ietf.org/html/draft-jennings-rtcweb-qos-00
solenbergd97ec302015-10-07 01:40:33 -0700100const rtc::DiffServCodePoint kAudioDscpValue = rtc::DSCP_EF;
henrike@webrtc.org1e09a712013-07-26 19:17:59 +0000101
Fredrik Solenbergb5727682015-12-04 15:22:19 +0100102// Constants from voice_engine_defines.h.
103const int kMinTelephoneEventCode = 0; // RFC4733 (Section 2.3.1)
104const int kMaxTelephoneEventCode = 255;
105const int kMinTelephoneEventDuration = 100;
106const int kMaxTelephoneEventDuration = 60000; // Actual limit is 2^16
107
solenberg31642aa2016-03-14 08:00:37 -0700108const int kMinPayloadType = 0;
109const int kMaxPayloadType = 127;
110
deadbeef884f5852016-01-15 09:20:04 -0800111class ProxySink : public webrtc::AudioSinkInterface {
112 public:
113 ProxySink(AudioSinkInterface* sink) : sink_(sink) { RTC_DCHECK(sink); }
114
115 void OnData(const Data& audio) override { sink_->OnData(audio); }
116
117 private:
118 webrtc::AudioSinkInterface* sink_;
119};
120
solenberg0b675462015-10-09 01:37:09 -0700121bool ValidateStreamParams(const StreamParams& sp) {
122 if (sp.ssrcs.empty()) {
123 LOG(LS_ERROR) << "No SSRCs in stream parameters: " << sp.ToString();
124 return false;
125 }
126 if (sp.ssrcs.size() > 1) {
127 LOG(LS_ERROR) << "Multiple SSRCs in stream parameters: " << sp.ToString();
128 return false;
129 }
130 return true;
131}
132
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000133// Dumps an AudioCodec in RFC 2327-ish format.
solenbergd97ec302015-10-07 01:40:33 -0700134std::string ToString(const AudioCodec& codec) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000135 std::stringstream ss;
136 ss << codec.name << "/" << codec.clockrate << "/" << codec.channels
137 << " (" << codec.id << ")";
138 return ss.str();
139}
Minyue Li7100dcd2015-03-27 05:05:59 +0100140
solenbergd97ec302015-10-07 01:40:33 -0700141std::string ToString(const webrtc::CodecInst& codec) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000142 std::stringstream ss;
143 ss << codec.plname << "/" << codec.plfreq << "/" << codec.channels
144 << " (" << codec.pltype << ")";
145 return ss.str();
146}
147
solenbergd97ec302015-10-07 01:40:33 -0700148bool IsCodec(const AudioCodec& codec, const char* ref_name) {
Minyue Li7100dcd2015-03-27 05:05:59 +0100149 return (_stricmp(codec.name.c_str(), ref_name) == 0);
150}
151
solenbergd97ec302015-10-07 01:40:33 -0700152bool IsCodec(const webrtc::CodecInst& codec, const char* ref_name) {
Minyue Li7100dcd2015-03-27 05:05:59 +0100153 return (_stricmp(codec.plname, ref_name) == 0);
154}
155
solenbergd97ec302015-10-07 01:40:33 -0700156bool FindCodec(const std::vector<AudioCodec>& codecs,
solenberg26c8c912015-11-27 04:00:25 -0800157 const AudioCodec& codec,
158 AudioCodec* found_codec) {
Fredrik Solenbergaf9fb212015-08-26 10:45:53 +0200159 for (const AudioCodec& c : codecs) {
160 if (c.Matches(codec)) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000161 if (found_codec != NULL) {
Fredrik Solenbergaf9fb212015-08-26 10:45:53 +0200162 *found_codec = c;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000163 }
164 return true;
165 }
166 }
167 return false;
168}
wu@webrtc.org1d1ffc92013-10-16 18:12:02 +0000169
solenberg0b675462015-10-09 01:37:09 -0700170bool VerifyUniquePayloadTypes(const std::vector<AudioCodec>& codecs) {
171 if (codecs.empty()) {
172 return true;
173 }
174 std::vector<int> payload_types;
175 for (const AudioCodec& codec : codecs) {
176 payload_types.push_back(codec.id);
177 }
178 std::sort(payload_types.begin(), payload_types.end());
179 auto it = std::unique(payload_types.begin(), payload_types.end());
180 return it == payload_types.end();
181}
182
Minyue Li7100dcd2015-03-27 05:05:59 +0100183// Return true if codec.params[feature] == "1", false otherwise.
solenberg26c8c912015-11-27 04:00:25 -0800184bool IsCodecFeatureEnabled(const AudioCodec& codec, const char* feature) {
Minyue Li7100dcd2015-03-27 05:05:59 +0100185 int value;
186 return codec.GetParam(feature, &value) && value == 1;
187}
188
189// Use params[kCodecParamMaxAverageBitrate] if it is defined, use codec.bitrate
190// otherwise. If the value (either from params or codec.bitrate) <=0, use the
191// default configuration. If the value is beyond feasible bit rate of Opus,
192// clamp it. Returns the Opus bit rate for operation.
solenbergd97ec302015-10-07 01:40:33 -0700193int GetOpusBitrate(const AudioCodec& codec, int max_playback_rate) {
Minyue Li7100dcd2015-03-27 05:05:59 +0100194 int bitrate = 0;
195 bool use_param = true;
196 if (!codec.GetParam(kCodecParamMaxAverageBitrate, &bitrate)) {
197 bitrate = codec.bitrate;
198 use_param = false;
199 }
200 if (bitrate <= 0) {
201 if (max_playback_rate <= 8000) {
202 bitrate = kOpusBitrateNb;
203 } else if (max_playback_rate <= 16000) {
204 bitrate = kOpusBitrateWb;
205 } else {
206 bitrate = kOpusBitrateFb;
207 }
208
209 if (IsCodecFeatureEnabled(codec, kCodecParamStereo)) {
210 bitrate *= 2;
211 }
212 } else if (bitrate < kOpusMinBitrate || bitrate > kOpusMaxBitrate) {
213 bitrate = (bitrate < kOpusMinBitrate) ? kOpusMinBitrate : kOpusMaxBitrate;
214 std::string rate_source =
215 use_param ? "Codec parameter \"maxaveragebitrate\"" :
216 "Supplied Opus bitrate";
217 LOG(LS_WARNING) << rate_source
218 << " is invalid and is replaced by: "
219 << bitrate;
220 }
221 return bitrate;
222}
223
224// Returns kOpusDefaultPlaybackRate if params[kCodecParamMaxPlaybackRate] is not
225// defined. Returns the value of params[kCodecParamMaxPlaybackRate] otherwise.
solenbergd97ec302015-10-07 01:40:33 -0700226int GetOpusMaxPlaybackRate(const AudioCodec& codec) {
Minyue Li7100dcd2015-03-27 05:05:59 +0100227 int value;
228 if (codec.GetParam(kCodecParamMaxPlaybackRate, &value)) {
229 return value;
230 }
231 return kOpusDefaultMaxPlaybackRate;
232}
233
solenbergd97ec302015-10-07 01:40:33 -0700234void GetOpusConfig(const AudioCodec& codec, webrtc::CodecInst* voe_codec,
Minyue Li7100dcd2015-03-27 05:05:59 +0100235 bool* enable_codec_fec, int* max_playback_rate,
236 bool* enable_codec_dtx) {
237 *enable_codec_fec = IsCodecFeatureEnabled(codec, kCodecParamUseInbandFec);
238 *enable_codec_dtx = IsCodecFeatureEnabled(codec, kCodecParamUseDtx);
239 *max_playback_rate = GetOpusMaxPlaybackRate(codec);
240
241 // If OPUS, change what we send according to the "stereo" codec
242 // parameter, and not the "channels" parameter. We set
243 // voe_codec.channels to 2 if "stereo=1" and 1 otherwise. If
244 // the bitrate is not specified, i.e. is <= zero, we set it to the
245 // appropriate default value for mono or stereo Opus.
246
247 voe_codec->channels = IsCodecFeatureEnabled(codec, kCodecParamStereo) ? 2 : 1;
248 voe_codec->rate = GetOpusBitrate(codec, *max_playback_rate);
249}
250
solenberg566ef242015-11-06 15:34:49 -0800251webrtc::AudioState::Config MakeAudioStateConfig(VoEWrapper* voe_wrapper) {
252 webrtc::AudioState::Config config;
253 config.voice_engine = voe_wrapper->engine();
254 return config;
255}
256
solenberg26c8c912015-11-27 04:00:25 -0800257class WebRtcVoiceCodecs final {
258 public:
259 // TODO(solenberg): Do this filtering once off-line, add a simple AudioCodec
260 // list and add a test which verifies VoE supports the listed codecs.
ossuc54071d2016-08-17 02:45:41 -0700261 static std::vector<AudioCodec> SupportedSendCodecs() {
solenberg26c8c912015-11-27 04:00:25 -0800262 std::vector<AudioCodec> result;
deadbeef67cf2c12016-04-13 10:07:16 -0700263 // Iterate first over our preferred codecs list, so that the results are
264 // added in order of preference.
265 for (size_t i = 0; i < arraysize(kCodecPrefs); ++i) {
266 const CodecPref* pref = &kCodecPrefs[i];
267 for (webrtc::CodecInst voe_codec : webrtc::acm2::RentACodec::Database()) {
268 // Change the sample rate of G722 to 8000 to match SDP.
269 MaybeFixupG722(&voe_codec, 8000);
270 // Skip uncompressed formats.
271 if (IsCodec(voe_codec, kL16CodecName)) {
272 continue;
buildbot@webrtc.org13d67762014-05-02 17:33:29 +0000273 }
buildbot@webrtc.org13d67762014-05-02 17:33:29 +0000274
deadbeef67cf2c12016-04-13 10:07:16 -0700275 if (!IsCodec(voe_codec, pref->name) ||
276 pref->clockrate != voe_codec.plfreq ||
277 pref->channels != voe_codec.channels) {
278 // Not a match.
279 continue;
280 }
281
282 AudioCodec codec(pref->payload_type, voe_codec.plname, voe_codec.plfreq,
283 voe_codec.rate, voe_codec.channels);
284 LOG(LS_INFO) << "Adding supported codec: " << ToString(codec);
Minyue Li7100dcd2015-03-27 05:05:59 +0100285 if (IsCodec(codec, kIsacCodecName)) {
minyue@webrtc.org26236952014-10-29 02:27:08 +0000286 // Indicate auto-bitrate in signaling.
buildbot@webrtc.org13d67762014-05-02 17:33:29 +0000287 codec.bitrate = 0;
288 }
Minyue Li7100dcd2015-03-27 05:05:59 +0100289 if (IsCodec(codec, kOpusCodecName)) {
buildbot@webrtc.org13d67762014-05-02 17:33:29 +0000290 // Only add fmtp parameters that differ from the spec.
291 if (kPreferredMinPTime != kOpusDefaultMinPTime) {
292 codec.params[kCodecParamMinPTime] =
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000293 rtc::ToString(kPreferredMinPTime);
buildbot@webrtc.org13d67762014-05-02 17:33:29 +0000294 }
295 if (kPreferredMaxPTime != kOpusDefaultMaxPTime) {
296 codec.params[kCodecParamMaxPTime] =
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000297 rtc::ToString(kPreferredMaxPTime);
buildbot@webrtc.org13d67762014-05-02 17:33:29 +0000298 }
pkasting@chromium.orgd3245462015-02-23 21:28:22 +0000299 codec.SetParam(kCodecParamUseInbandFec, 1);
stefanba4c0e42016-02-04 04:12:24 -0800300 codec.AddFeedbackParam(
301 FeedbackParam(kRtcpFbParamTransportCc, kParamValueEmpty));
minyue@webrtc.org4ef22d12014-11-17 09:26:39 +0000302
303 // TODO(hellner): Add ptime, sprop-stereo, and stereo
buildbot@webrtc.org13d67762014-05-02 17:33:29 +0000304 // when they can be set to values other than the default.
305 }
solenberg26c8c912015-11-27 04:00:25 -0800306 result.push_back(codec);
buildbot@webrtc.org13d67762014-05-02 17:33:29 +0000307 }
308 }
solenberg26c8c912015-11-27 04:00:25 -0800309 return result;
buildbot@webrtc.org13d67762014-05-02 17:33:29 +0000310 }
buildbot@webrtc.org13d67762014-05-02 17:33:29 +0000311
solenberg26c8c912015-11-27 04:00:25 -0800312 static bool ToCodecInst(const AudioCodec& in,
313 webrtc::CodecInst* out) {
314 for (webrtc::CodecInst voe_codec : webrtc::acm2::RentACodec::Database()) {
315 // Change the sample rate of G722 to 8000 to match SDP.
316 MaybeFixupG722(&voe_codec, 8000);
317 AudioCodec codec(voe_codec.pltype, voe_codec.plname, voe_codec.plfreq,
deadbeef67cf2c12016-04-13 10:07:16 -0700318 voe_codec.rate, voe_codec.channels);
solenberg26c8c912015-11-27 04:00:25 -0800319 bool multi_rate = IsCodecMultiRate(voe_codec);
320 // Allow arbitrary rates for ISAC to be specified.
321 if (multi_rate) {
322 // Set codec.bitrate to 0 so the check for codec.Matches() passes.
323 codec.bitrate = 0;
324 }
325 if (codec.Matches(in)) {
326 if (out) {
327 // Fixup the payload type.
328 voe_codec.pltype = in.id;
329
330 // Set bitrate if specified.
331 if (multi_rate && in.bitrate != 0) {
332 voe_codec.rate = in.bitrate;
333 }
334
335 // Reset G722 sample rate to 16000 to match WebRTC.
336 MaybeFixupG722(&voe_codec, 16000);
337
338 // Apply codec-specific settings.
339 if (IsCodec(codec, kIsacCodecName)) {
340 // If ISAC and an explicit bitrate is not specified,
341 // enable auto bitrate adjustment.
342 voe_codec.rate = (in.bitrate > 0) ? in.bitrate : -1;
343 }
344 *out = voe_codec;
345 }
346 return true;
347 }
348 }
henrik.lundin@webrtc.org8038d422014-11-11 08:38:24 +0000349 return false;
henrik.lundin@webrtc.orgf85dbce2014-11-07 12:25:00 +0000350 }
solenberg26c8c912015-11-27 04:00:25 -0800351
352 static bool IsCodecMultiRate(const webrtc::CodecInst& codec) {
353 for (size_t i = 0; i < arraysize(kCodecPrefs); ++i) {
354 if (IsCodec(codec, kCodecPrefs[i].name) &&
355 kCodecPrefs[i].clockrate == codec.plfreq) {
356 return kCodecPrefs[i].is_multi_rate;
357 }
358 }
359 return false;
360 }
361
deadbeef80346142016-04-27 14:17:10 -0700362 static int MaxBitrateBps(const webrtc::CodecInst& codec) {
363 for (size_t i = 0; i < arraysize(kCodecPrefs); ++i) {
364 if (IsCodec(codec, kCodecPrefs[i].name) &&
365 kCodecPrefs[i].clockrate == codec.plfreq) {
366 return kCodecPrefs[i].max_bitrate_bps;
367 }
368 }
369 return 0;
370 }
371
solenberg26c8c912015-11-27 04:00:25 -0800372 // If the AudioCodec param kCodecParamPTime is set, then we will set it to
373 // codec pacsize if it's valid, or we will pick the next smallest value we
374 // support.
375 // TODO(Brave): Query supported packet sizes from ACM when the API is ready.
376 static bool SetPTimeAsPacketSize(webrtc::CodecInst* codec, int ptime_ms) {
377 for (const CodecPref& codec_pref : kCodecPrefs) {
378 if ((IsCodec(*codec, codec_pref.name) &&
379 codec_pref.clockrate == codec->plfreq) ||
380 IsCodec(*codec, kG722CodecName)) {
381 int packet_size_ms = SelectPacketSize(codec_pref, ptime_ms);
382 if (packet_size_ms) {
383 // Convert unit from milli-seconds to samples.
384 codec->pacsize = (codec->plfreq / 1000) * packet_size_ms;
385 return true;
386 }
387 }
388 }
389 return false;
390 }
391
stefanba4c0e42016-02-04 04:12:24 -0800392 static const AudioCodec* GetPreferredCodec(
393 const std::vector<AudioCodec>& codecs,
kwiberg68061362016-06-14 08:04:47 -0700394 webrtc::CodecInst* out) {
solenberg72e29d22016-03-08 06:35:16 -0800395 RTC_DCHECK(out);
stefanba4c0e42016-02-04 04:12:24 -0800396 // Select the preferred send codec (the first non-telephone-event/CN codec).
397 for (const AudioCodec& codec : codecs) {
stefanba4c0e42016-02-04 04:12:24 -0800398 if (IsCodec(codec, kDtmfCodecName) || IsCodec(codec, kCnCodecName)) {
399 // Skip telephone-event/CN codec, which will be handled later.
400 continue;
401 }
402
403 // We'll use the first codec in the list to actually send audio data.
404 // Be sure to use the payload type requested by the remote side.
stefanba4c0e42016-02-04 04:12:24 -0800405 // Ignore codecs we don't know about. The negotiation step should prevent
406 // this, but double-check to be sure.
kwibergedaa8492016-06-15 04:34:47 -0700407 if (!ToCodecInst(codec, out)) {
kwiberg68061362016-06-14 08:04:47 -0700408 LOG(LS_WARNING) << "Unknown codec " << ToString(codec);
stefanba4c0e42016-02-04 04:12:24 -0800409 continue;
410 }
kwiberg68061362016-06-14 08:04:47 -0700411 return &codec;
stefanba4c0e42016-02-04 04:12:24 -0800412 }
413 return nullptr;
414 }
415
solenberg26c8c912015-11-27 04:00:25 -0800416 private:
417 static const int kMaxNumPacketSize = 6;
418 struct CodecPref {
419 const char* name;
420 int clockrate;
Peter Kasting69558702016-01-12 16:26:35 -0800421 size_t channels;
solenberg26c8c912015-11-27 04:00:25 -0800422 int payload_type;
423 bool is_multi_rate;
424 int packet_sizes_ms[kMaxNumPacketSize];
deadbeef80346142016-04-27 14:17:10 -0700425 int max_bitrate_bps;
solenberg26c8c912015-11-27 04:00:25 -0800426 };
427 // Note: keep the supported packet sizes in ascending order.
kwiberg68061362016-06-14 08:04:47 -0700428 static const CodecPref kCodecPrefs[11];
solenberg26c8c912015-11-27 04:00:25 -0800429
430 static int SelectPacketSize(const CodecPref& codec_pref, int ptime_ms) {
431 int selected_packet_size_ms = codec_pref.packet_sizes_ms[0];
432 for (int packet_size_ms : codec_pref.packet_sizes_ms) {
433 if (packet_size_ms && packet_size_ms <= ptime_ms) {
434 selected_packet_size_ms = packet_size_ms;
435 }
436 }
437 return selected_packet_size_ms;
438 }
439
440 // Changes RTP timestamp rate of G722. This is due to the "bug" in the RFC
441 // which says that G722 should be advertised as 8 kHz although it is a 16 kHz
442 // codec.
443 static void MaybeFixupG722(webrtc::CodecInst* voe_codec, int new_plfreq) {
444 if (IsCodec(*voe_codec, kG722CodecName)) {
445 // If the ASSERT triggers, the codec definition in WebRTC VoiceEngine
446 // has changed, and this special case is no longer needed.
447 RTC_DCHECK(voe_codec->plfreq != new_plfreq);
448 voe_codec->plfreq = new_plfreq;
449 }
450 }
451};
452
kwiberg68061362016-06-14 08:04:47 -0700453const WebRtcVoiceCodecs::CodecPref WebRtcVoiceCodecs::kCodecPrefs[11] = {
deadbeef80346142016-04-27 14:17:10 -0700454 {kOpusCodecName, 48000, 2, 111, true, {10, 20, 40, 60}, kOpusMaxBitrate},
455 {kIsacCodecName, 16000, 1, 103, true, {30, 60}, kIsacMaxBitrate},
456 {kIsacCodecName, 32000, 1, 104, true, {30}, kIsacMaxBitrate},
457 // G722 should be advertised as 8000 Hz because of the RFC "bug".
458 {kG722CodecName, 8000, 1, 9, false, {10, 20, 30, 40, 50, 60}},
459 {kIlbcCodecName, 8000, 1, 102, false, {20, 30, 40, 60}},
460 {kPcmuCodecName, 8000, 1, 0, false, {10, 20, 30, 40, 50, 60}},
461 {kPcmaCodecName, 8000, 1, 8, false, {10, 20, 30, 40, 50, 60}},
462 {kCnCodecName, 32000, 1, 106, false, {}},
463 {kCnCodecName, 16000, 1, 105, false, {}},
464 {kCnCodecName, 8000, 1, 13, false, {}},
kwiberg68061362016-06-14 08:04:47 -0700465 {kDtmfCodecName, 8000, 1, 126, false, {}}
solenberg26c8c912015-11-27 04:00:25 -0800466};
467} // namespace {
468
solenberg971cab02016-06-14 10:02:41 -0700469bool SendCodecSpec::operator==(const SendCodecSpec& rhs) const {
470 if (nack_enabled != rhs.nack_enabled) {
471 return false;
472 }
473 if (transport_cc_enabled != rhs.transport_cc_enabled) {
474 return false;
475 }
476 if (enable_codec_fec != rhs.enable_codec_fec) {
477 return false;
478 }
479 if (enable_opus_dtx != rhs.enable_opus_dtx) {
480 return false;
481 }
482 if (opus_max_playback_rate != rhs.opus_max_playback_rate) {
483 return false;
484 }
485 if (red_payload_type != rhs.red_payload_type) {
486 return false;
487 }
488 if (cng_payload_type != rhs.cng_payload_type) {
489 return false;
490 }
491 if (cng_plfreq != rhs.cng_plfreq) {
492 return false;
493 }
494 if (codec_inst != rhs.codec_inst) {
495 return false;
496 }
497 return true;
498}
499
500bool SendCodecSpec::operator!=(const SendCodecSpec& rhs) const {
501 return !(*this == rhs);
502}
503
solenberg26c8c912015-11-27 04:00:25 -0800504bool WebRtcVoiceEngine::ToCodecInst(const AudioCodec& in,
505 webrtc::CodecInst* out) {
506 return WebRtcVoiceCodecs::ToCodecInst(in, out);
507}
508
ossu29b1a8d2016-06-13 07:34:51 -0700509WebRtcVoiceEngine::WebRtcVoiceEngine(
510 webrtc::AudioDeviceModule* adm,
511 const rtc::scoped_refptr<webrtc::AudioDecoderFactory>& decoder_factory)
512 : WebRtcVoiceEngine(adm, decoder_factory, new VoEWrapper()) {
solenbergff976312016-03-30 23:28:51 -0700513 audio_state_ = webrtc::AudioState::Create(MakeAudioStateConfig(voe()));
solenberg26c8c912015-11-27 04:00:25 -0800514}
515
ossu29b1a8d2016-06-13 07:34:51 -0700516WebRtcVoiceEngine::WebRtcVoiceEngine(
517 webrtc::AudioDeviceModule* adm,
518 const rtc::scoped_refptr<webrtc::AudioDecoderFactory>& decoder_factory,
519 VoEWrapper* voe_wrapper)
520 : adm_(adm), decoder_factory_(decoder_factory), voe_wrapper_(voe_wrapper) {
solenberg26c8c912015-11-27 04:00:25 -0800521 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
solenbergff976312016-03-30 23:28:51 -0700522 LOG(LS_INFO) << "WebRtcVoiceEngine::WebRtcVoiceEngine";
523 RTC_DCHECK(voe_wrapper);
ossuc54071d2016-08-17 02:45:41 -0700524 RTC_DCHECK(decoder_factory);
solenberg26c8c912015-11-27 04:00:25 -0800525
526 signal_thread_checker_.DetachFromThread();
solenberg26c8c912015-11-27 04:00:25 -0800527
528 // Load our audio codec list.
ossuc54071d2016-08-17 02:45:41 -0700529 LOG(LS_INFO) << "Supported send codecs in order of preference:";
530 send_codecs_ = WebRtcVoiceCodecs::SupportedSendCodecs();
531 for (const AudioCodec& codec : send_codecs_) {
532 LOG(LS_INFO) << ToString(codec);
533 }
534
535 LOG(LS_INFO) << "Supported recv codecs in order of preference:";
536 recv_codecs_ = CollectRecvCodecs();
537 for (const AudioCodec& codec : recv_codecs_) {
solenbergff976312016-03-30 23:28:51 -0700538 LOG(LS_INFO) << ToString(codec);
buildbot@webrtc.org13d67762014-05-02 17:33:29 +0000539 }
buildbot@webrtc.org13d67762014-05-02 17:33:29 +0000540
solenberg88499ec2016-09-07 07:34:41 -0700541 channel_config_.enable_voice_pacing = true;
buildbot@webrtc.org13d67762014-05-02 17:33:29 +0000542
solenbergff976312016-03-30 23:28:51 -0700543 // Temporarily turn logging level up for the Init() call.
544 webrtc::Trace::SetTraceCallback(this);
solenbergbd138382015-11-20 16:08:07 -0800545 webrtc::Trace::set_level_filter(kElevatedTraceFilter);
solenberg2515af22015-12-02 06:19:36 -0800546 LOG(LS_INFO) << webrtc::VoiceEngine::GetVersionString();
ossu29b1a8d2016-06-13 07:34:51 -0700547 RTC_CHECK_EQ(0, voe_wrapper_->base()->Init(adm_.get(), nullptr,
548 decoder_factory_));
solenbergbd138382015-11-20 16:08:07 -0800549 webrtc::Trace::set_level_filter(kDefaultTraceFilter);
buildbot@webrtc.org13d67762014-05-02 17:33:29 +0000550
solenbergff976312016-03-30 23:28:51 -0700551 // No ADM supplied? Get the default one from VoE.
552 if (!adm_) {
553 adm_ = voe_wrapper_->base()->audio_device_module();
554 }
555 RTC_DCHECK(adm_);
556
buildbot@webrtc.org13d67762014-05-02 17:33:29 +0000557 // Save the default AGC configuration settings. This must happen before
solenberg246b8172015-12-08 09:50:23 -0800558 // calling ApplyOptions or the default will be overwritten.
solenbergff976312016-03-30 23:28:51 -0700559 int error = voe_wrapper_->processing()->GetAgcConfig(default_agc_config_);
560 RTC_DCHECK_EQ(0, error);
buildbot@webrtc.org13d67762014-05-02 17:33:29 +0000561
solenberg0f7d2932016-01-15 01:40:39 -0800562 // Set default engine options.
563 {
564 AudioOptions options;
565 options.echo_cancellation = rtc::Optional<bool>(true);
566 options.auto_gain_control = rtc::Optional<bool>(true);
567 options.noise_suppression = rtc::Optional<bool>(true);
568 options.highpass_filter = rtc::Optional<bool>(true);
569 options.stereo_swapping = rtc::Optional<bool>(false);
570 options.audio_jitter_buffer_max_packets = rtc::Optional<int>(50);
571 options.audio_jitter_buffer_fast_accelerate = rtc::Optional<bool>(false);
572 options.typing_detection = rtc::Optional<bool>(true);
573 options.adjust_agc_delta = rtc::Optional<int>(0);
574 options.experimental_agc = rtc::Optional<bool>(false);
575 options.extended_filter_aec = rtc::Optional<bool>(false);
576 options.delay_agnostic_aec = rtc::Optional<bool>(false);
577 options.experimental_ns = rtc::Optional<bool>(false);
Alejandro Luebsc9b0c262016-05-16 15:32:38 -0700578 options.intelligibility_enhancer = rtc::Optional<bool>(false);
peaha3333bf2016-06-30 00:02:34 -0700579 options.level_control = rtc::Optional<bool>(false);
solenbergff976312016-03-30 23:28:51 -0700580 bool error = ApplyOptions(options);
581 RTC_DCHECK(error);
buildbot@webrtc.org13d67762014-05-02 17:33:29 +0000582 }
583
solenberg246b8172015-12-08 09:50:23 -0800584 SetDefaultDevices();
buildbot@webrtc.org13d67762014-05-02 17:33:29 +0000585}
586
solenbergff976312016-03-30 23:28:51 -0700587WebRtcVoiceEngine::~WebRtcVoiceEngine() {
solenberg566ef242015-11-06 15:34:49 -0800588 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
solenbergff976312016-03-30 23:28:51 -0700589 LOG(LS_INFO) << "WebRtcVoiceEngine::~WebRtcVoiceEngine";
buildbot@webrtc.org13d67762014-05-02 17:33:29 +0000590 StopAecDump();
buildbot@webrtc.org13d67762014-05-02 17:33:29 +0000591 voe_wrapper_->base()->Terminate();
solenbergff976312016-03-30 23:28:51 -0700592 webrtc::Trace::SetTraceCallback(nullptr);
buildbot@webrtc.org13d67762014-05-02 17:33:29 +0000593}
594
solenberg566ef242015-11-06 15:34:49 -0800595rtc::scoped_refptr<webrtc::AudioState>
596 WebRtcVoiceEngine::GetAudioState() const {
597 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
598 return audio_state_;
599}
600
nisse51542be2016-02-12 02:27:06 -0800601VoiceMediaChannel* WebRtcVoiceEngine::CreateChannel(
602 webrtc::Call* call,
603 const MediaConfig& config,
Jelena Marusicc28a8962015-05-29 15:05:44 +0200604 const AudioOptions& options) {
solenberg566ef242015-11-06 15:34:49 -0800605 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
nisse51542be2016-02-12 02:27:06 -0800606 return new WebRtcVoiceMediaChannel(this, config, options, call);
buildbot@webrtc.org13d67762014-05-02 17:33:29 +0000607}
608
buildbot@webrtc.org13d67762014-05-02 17:33:29 +0000609bool WebRtcVoiceEngine::ApplyOptions(const AudioOptions& options_in) {
solenberg566ef242015-11-06 15:34:49 -0800610 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
solenbergff976312016-03-30 23:28:51 -0700611 LOG(LS_INFO) << "WebRtcVoiceEngine::ApplyOptions: " << options_in.ToString();
solenberg0f7d2932016-01-15 01:40:39 -0800612 AudioOptions options = options_in; // The options are modified below.
solenberg246b8172015-12-08 09:50:23 -0800613
buildbot@webrtc.org13d67762014-05-02 17:33:29 +0000614 // kEcConference is AEC with high suppression.
615 webrtc::EcModes ec_mode = webrtc::kEcConference;
616 webrtc::AecmModes aecm_mode = webrtc::kAecmSpeakerphone;
617 webrtc::AgcModes agc_mode = webrtc::kAgcAdaptiveAnalog;
618 webrtc::NsModes ns_mode = webrtc::kNsHighSuppression;
kwiberg102c6a62015-10-30 02:47:38 -0700619 if (options.aecm_generate_comfort_noise) {
buildbot@webrtc.org13d67762014-05-02 17:33:29 +0000620 LOG(LS_VERBOSE) << "Comfort noise explicitly set to "
kwiberg102c6a62015-10-30 02:47:38 -0700621 << *options.aecm_generate_comfort_noise
622 << " (default is false).";
buildbot@webrtc.org13d67762014-05-02 17:33:29 +0000623 }
624
kjellanderfcfc8042016-01-14 11:01:09 -0800625#if defined(WEBRTC_IOS)
peah4905f062016-08-22 01:58:50 -0700626 // On iOS, VPIO provides built-in EC, NS and AGC.
Karl Wibergbe579832015-11-10 22:34:18 +0100627 options.echo_cancellation = rtc::Optional<bool>(false);
628 options.auto_gain_control = rtc::Optional<bool>(false);
peah4905f062016-08-22 01:58:50 -0700629 options.noise_suppression = rtc::Optional<bool>(false);
630 LOG(LS_INFO)
631 << "Always disable AEC, NS and AGC on iOS. Use built-in instead.";
buildbot@webrtc.org13d67762014-05-02 17:33:29 +0000632#elif defined(ANDROID)
633 ec_mode = webrtc::kEcAecm;
634#endif
635
kjellanderfcfc8042016-01-14 11:01:09 -0800636#if defined(WEBRTC_IOS) || defined(ANDROID)
buildbot@webrtc.org13d67762014-05-02 17:33:29 +0000637 // Set the AGC mode for iOS as well despite disabling it above, to avoid
638 // unsupported configuration errors from webrtc.
639 agc_mode = webrtc::kAgcFixedDigital;
Karl Wibergbe579832015-11-10 22:34:18 +0100640 options.typing_detection = rtc::Optional<bool>(false);
641 options.experimental_agc = rtc::Optional<bool>(false);
642 options.extended_filter_aec = rtc::Optional<bool>(false);
643 options.experimental_ns = rtc::Optional<bool>(false);
buildbot@webrtc.org13d67762014-05-02 17:33:29 +0000644#endif
645
Bjorn Volckerbf395c12015-03-25 22:45:56 +0100646 // Delay Agnostic AEC automatically turns on EC if not set except on iOS
647 // where the feature is not supported.
648 bool use_delay_agnostic_aec = false;
kjellanderfcfc8042016-01-14 11:01:09 -0800649#if !defined(WEBRTC_IOS)
kwiberg102c6a62015-10-30 02:47:38 -0700650 if (options.delay_agnostic_aec) {
651 use_delay_agnostic_aec = *options.delay_agnostic_aec;
Bjorn Volckerbf395c12015-03-25 22:45:56 +0100652 if (use_delay_agnostic_aec) {
Karl Wibergbe579832015-11-10 22:34:18 +0100653 options.echo_cancellation = rtc::Optional<bool>(true);
654 options.extended_filter_aec = rtc::Optional<bool>(true);
Bjorn Volckerbf395c12015-03-25 22:45:56 +0100655 ec_mode = webrtc::kEcConference;
656 }
657 }
658#endif
659
peah1bcfce52016-08-26 07:16:04 -0700660#if (WEBRTC_INTELLIGIBILITY_ENHANCER == 0)
661 // Hardcode the intelligibility enhancer to be off.
662 options.intelligibility_enhancer = rtc::Optional<bool>(false);
663#endif
664
buildbot@webrtc.org13d67762014-05-02 17:33:29 +0000665 webrtc::VoEAudioProcessing* voep = voe_wrapper_->processing();
666
kwiberg102c6a62015-10-30 02:47:38 -0700667 if (options.echo_cancellation) {
henrika@webrtc.orga954c072014-12-09 16:22:09 +0000668 // Check if platform supports built-in EC. Currently only supported on
669 // Android and in combination with Java based audio layer.
670 // TODO(henrika): investigate possibility to support built-in EC also
671 // in combination with Open SL ES audio.
solenberg5b5129a2016-04-08 05:35:48 -0700672 const bool built_in_aec = adm()->BuiltInAECIsAvailable();
Bjorn Volcker73f72102015-06-03 14:50:15 +0200673 if (built_in_aec) {
Bjorn Volckerccfc9392015-05-07 07:43:17 +0200674 // Built-in EC exists on this device and use_delay_agnostic_aec is not
675 // overriding it. Enable/Disable it according to the echo_cancellation
676 // audio option.
Bjorn Volcker73f72102015-06-03 14:50:15 +0200677 const bool enable_built_in_aec =
kwiberg102c6a62015-10-30 02:47:38 -0700678 *options.echo_cancellation && !use_delay_agnostic_aec;
solenberg5b5129a2016-04-08 05:35:48 -0700679 if (adm()->EnableBuiltInAEC(enable_built_in_aec) == 0 &&
Bjorn Volcker73f72102015-06-03 14:50:15 +0200680 enable_built_in_aec) {
Bjorn Volckerbf395c12015-03-25 22:45:56 +0100681 // Disable internal software EC if built-in EC is enabled,
henrika@webrtc.orga954c072014-12-09 16:22:09 +0000682 // i.e., replace the software EC with the built-in EC.
Karl Wibergbe579832015-11-10 22:34:18 +0100683 options.echo_cancellation = rtc::Optional<bool>(false);
henrika@webrtc.orga954c072014-12-09 16:22:09 +0000684 LOG(LS_INFO) << "Disabling EC since built-in EC will be used instead";
685 }
686 }
kwiberg102c6a62015-10-30 02:47:38 -0700687 if (voep->SetEcStatus(*options.echo_cancellation, ec_mode) == -1) {
688 LOG_RTCERR2(SetEcStatus, *options.echo_cancellation, ec_mode);
buildbot@webrtc.org13d67762014-05-02 17:33:29 +0000689 return false;
690 } else {
kwiberg102c6a62015-10-30 02:47:38 -0700691 LOG(LS_INFO) << "Echo control set to " << *options.echo_cancellation
henrika86d907c2015-09-07 16:09:50 +0200692 << " with mode " << ec_mode;
buildbot@webrtc.org13d67762014-05-02 17:33:29 +0000693 }
694#if !defined(ANDROID)
695 // TODO(ajm): Remove the error return on Android from webrtc.
kwiberg102c6a62015-10-30 02:47:38 -0700696 if (voep->SetEcMetricsStatus(*options.echo_cancellation) == -1) {
697 LOG_RTCERR1(SetEcMetricsStatus, *options.echo_cancellation);
buildbot@webrtc.org13d67762014-05-02 17:33:29 +0000698 return false;
699 }
700#endif
701 if (ec_mode == webrtc::kEcAecm) {
kwiberg102c6a62015-10-30 02:47:38 -0700702 bool cn = options.aecm_generate_comfort_noise.value_or(false);
703 if (voep->SetAecmMode(aecm_mode, cn) != 0) {
704 LOG_RTCERR2(SetAecmMode, aecm_mode, cn);
buildbot@webrtc.org13d67762014-05-02 17:33:29 +0000705 return false;
706 }
707 }
708 }
709
kwiberg102c6a62015-10-30 02:47:38 -0700710 if (options.auto_gain_control) {
peah72a56452016-08-22 12:08:55 -0700711 bool built_in_agc_avaliable = adm()->BuiltInAGCIsAvailable();
712 if (built_in_agc_avaliable) {
solenberg5b5129a2016-04-08 05:35:48 -0700713 if (adm()->EnableBuiltInAGC(*options.auto_gain_control) == 0 &&
kwiberg102c6a62015-10-30 02:47:38 -0700714 *options.auto_gain_control) {
henrikac14f5ff2015-09-23 14:08:33 +0200715 // Disable internal software AGC if built-in AGC is enabled,
716 // i.e., replace the software AGC with the built-in AGC.
Karl Wibergbe579832015-11-10 22:34:18 +0100717 options.auto_gain_control = rtc::Optional<bool>(false);
henrikac14f5ff2015-09-23 14:08:33 +0200718 LOG(LS_INFO) << "Disabling AGC since built-in AGC will be used instead";
719 }
720 }
kwiberg102c6a62015-10-30 02:47:38 -0700721 if (voep->SetAgcStatus(*options.auto_gain_control, agc_mode) == -1) {
722 LOG_RTCERR2(SetAgcStatus, *options.auto_gain_control, agc_mode);
buildbot@webrtc.org13d67762014-05-02 17:33:29 +0000723 return false;
724 } else {
kwiberg102c6a62015-10-30 02:47:38 -0700725 LOG(LS_INFO) << "Auto gain set to " << *options.auto_gain_control
726 << " with mode " << agc_mode;
buildbot@webrtc.org13d67762014-05-02 17:33:29 +0000727 }
728 }
729
kwiberg102c6a62015-10-30 02:47:38 -0700730 if (options.tx_agc_target_dbov || options.tx_agc_digital_compression_gain ||
731 options.tx_agc_limiter) {
buildbot@webrtc.org13d67762014-05-02 17:33:29 +0000732 // Override default_agc_config_. Generally, an unset option means "leave
733 // the VoE bits alone" in this function, so we want whatever is set to be
734 // stored as the new "default". If we didn't, then setting e.g.
735 // tx_agc_target_dbov would reset digital compression gain and limiter
736 // settings.
737 // Also, if we don't update default_agc_config_, then adjust_agc_delta
738 // would be an offset from the original values, and not whatever was set
739 // explicitly.
kwiberg102c6a62015-10-30 02:47:38 -0700740 default_agc_config_.targetLeveldBOv = options.tx_agc_target_dbov.value_or(
741 default_agc_config_.targetLeveldBOv);
buildbot@webrtc.org13d67762014-05-02 17:33:29 +0000742 default_agc_config_.digitalCompressionGaindB =
kwiberg102c6a62015-10-30 02:47:38 -0700743 options.tx_agc_digital_compression_gain.value_or(
buildbot@webrtc.org13d67762014-05-02 17:33:29 +0000744 default_agc_config_.digitalCompressionGaindB);
745 default_agc_config_.limiterEnable =
kwiberg102c6a62015-10-30 02:47:38 -0700746 options.tx_agc_limiter.value_or(default_agc_config_.limiterEnable);
buildbot@webrtc.org13d67762014-05-02 17:33:29 +0000747 if (voe_wrapper_->processing()->SetAgcConfig(default_agc_config_) == -1) {
748 LOG_RTCERR3(SetAgcConfig,
749 default_agc_config_.targetLeveldBOv,
750 default_agc_config_.digitalCompressionGaindB,
751 default_agc_config_.limiterEnable);
752 return false;
753 }
754 }
755
Alejandro Luebsc9b0c262016-05-16 15:32:38 -0700756 if (options.intelligibility_enhancer) {
757 intelligibility_enhancer_ = options.intelligibility_enhancer;
758 }
759 if (intelligibility_enhancer_ && *intelligibility_enhancer_) {
760 LOG(LS_INFO) << "Enabling NS when Intelligibility Enhancer is active.";
761 options.noise_suppression = intelligibility_enhancer_;
762 }
763
kwiberg102c6a62015-10-30 02:47:38 -0700764 if (options.noise_suppression) {
Alejandro Luebsc9b0c262016-05-16 15:32:38 -0700765 if (adm()->BuiltInNSIsAvailable()) {
766 bool builtin_ns =
767 *options.noise_suppression &&
768 !(intelligibility_enhancer_ && *intelligibility_enhancer_);
769 if (adm()->EnableBuiltInNS(builtin_ns) == 0 && builtin_ns) {
henrikac14f5ff2015-09-23 14:08:33 +0200770 // Disable internal software NS if built-in NS is enabled,
771 // i.e., replace the software NS with the built-in NS.
Karl Wibergbe579832015-11-10 22:34:18 +0100772 options.noise_suppression = rtc::Optional<bool>(false);
henrikac14f5ff2015-09-23 14:08:33 +0200773 LOG(LS_INFO) << "Disabling NS since built-in NS will be used instead";
774 }
775 }
kwiberg102c6a62015-10-30 02:47:38 -0700776 if (voep->SetNsStatus(*options.noise_suppression, ns_mode) == -1) {
777 LOG_RTCERR2(SetNsStatus, *options.noise_suppression, ns_mode);
buildbot@webrtc.org13d67762014-05-02 17:33:29 +0000778 return false;
779 } else {
kwiberg102c6a62015-10-30 02:47:38 -0700780 LOG(LS_INFO) << "Noise suppression set to " << *options.noise_suppression
henrikac14f5ff2015-09-23 14:08:33 +0200781 << " with mode " << ns_mode;
buildbot@webrtc.org13d67762014-05-02 17:33:29 +0000782 }
783 }
784
kwiberg102c6a62015-10-30 02:47:38 -0700785 if (options.highpass_filter) {
786 LOG(LS_INFO) << "High pass filter enabled? " << *options.highpass_filter;
787 if (voep->EnableHighPassFilter(*options.highpass_filter) == -1) {
788 LOG_RTCERR1(SetHighpassFilterStatus, *options.highpass_filter);
buildbot@webrtc.org13d67762014-05-02 17:33:29 +0000789 return false;
790 }
791 }
792
kwiberg102c6a62015-10-30 02:47:38 -0700793 if (options.stereo_swapping) {
794 LOG(LS_INFO) << "Stereo swapping enabled? " << *options.stereo_swapping;
795 voep->EnableStereoChannelSwapping(*options.stereo_swapping);
796 if (voep->IsStereoChannelSwappingEnabled() != *options.stereo_swapping) {
797 LOG_RTCERR1(EnableStereoChannelSwapping, *options.stereo_swapping);
buildbot@webrtc.org13d67762014-05-02 17:33:29 +0000798 return false;
799 }
800 }
801
kwiberg102c6a62015-10-30 02:47:38 -0700802 if (options.audio_jitter_buffer_max_packets) {
803 LOG(LS_INFO) << "NetEq capacity is "
804 << *options.audio_jitter_buffer_max_packets;
solenberg88499ec2016-09-07 07:34:41 -0700805 channel_config_.acm_config.neteq_config.max_packets_in_buffer =
806 std::max(20, *options.audio_jitter_buffer_max_packets);
Henrik Lundin64dad832015-05-11 12:44:23 +0200807 }
kwiberg102c6a62015-10-30 02:47:38 -0700808 if (options.audio_jitter_buffer_fast_accelerate) {
809 LOG(LS_INFO) << "NetEq fast mode? "
810 << *options.audio_jitter_buffer_fast_accelerate;
solenberg88499ec2016-09-07 07:34:41 -0700811 channel_config_.acm_config.neteq_config.enable_fast_accelerate =
812 *options.audio_jitter_buffer_fast_accelerate;
Henrik Lundin5263b3c2015-06-01 10:29:41 +0200813 }
814
kwiberg102c6a62015-10-30 02:47:38 -0700815 if (options.typing_detection) {
816 LOG(LS_INFO) << "Typing detection is enabled? "
817 << *options.typing_detection;
818 if (voep->SetTypingDetectionStatus(*options.typing_detection) == -1) {
buildbot@webrtc.org13d67762014-05-02 17:33:29 +0000819 // In case of error, log the info and continue
kwiberg102c6a62015-10-30 02:47:38 -0700820 LOG_RTCERR1(SetTypingDetectionStatus, *options.typing_detection);
buildbot@webrtc.org13d67762014-05-02 17:33:29 +0000821 }
822 }
823
kwiberg102c6a62015-10-30 02:47:38 -0700824 if (options.adjust_agc_delta) {
825 LOG(LS_INFO) << "Adjust agc delta is " << *options.adjust_agc_delta;
826 if (!AdjustAgcLevel(*options.adjust_agc_delta)) {
buildbot@webrtc.org13d67762014-05-02 17:33:29 +0000827 return false;
828 }
829 }
830
buildbot@webrtc.org1f8a2372014-08-28 10:52:44 +0000831 webrtc::Config config;
832
kwiberg102c6a62015-10-30 02:47:38 -0700833 if (options.delay_agnostic_aec)
834 delay_agnostic_aec_ = options.delay_agnostic_aec;
835 if (delay_agnostic_aec_) {
836 LOG(LS_INFO) << "Delay agnostic aec is enabled? " << *delay_agnostic_aec_;
henrik.lundin0f133b92015-07-02 00:17:55 -0700837 config.Set<webrtc::DelayAgnostic>(
kwiberg102c6a62015-10-30 02:47:38 -0700838 new webrtc::DelayAgnostic(*delay_agnostic_aec_));
Bjorn Volckerbf395c12015-03-25 22:45:56 +0100839 }
840
kwiberg102c6a62015-10-30 02:47:38 -0700841 if (options.extended_filter_aec) {
842 extended_filter_aec_ = options.extended_filter_aec;
843 }
844 if (extended_filter_aec_) {
845 LOG(LS_INFO) << "Extended filter aec is enabled? " << *extended_filter_aec_;
Henrik Lundin441f6342015-06-09 16:03:13 +0200846 config.Set<webrtc::ExtendedFilter>(
kwiberg102c6a62015-10-30 02:47:38 -0700847 new webrtc::ExtendedFilter(*extended_filter_aec_));
buildbot@webrtc.org1f8a2372014-08-28 10:52:44 +0000848 }
849
kwiberg102c6a62015-10-30 02:47:38 -0700850 if (options.experimental_ns) {
851 experimental_ns_ = options.experimental_ns;
852 }
853 if (experimental_ns_) {
854 LOG(LS_INFO) << "Experimental ns is enabled? " << *experimental_ns_;
buildbot@webrtc.org1f8a2372014-08-28 10:52:44 +0000855 config.Set<webrtc::ExperimentalNs>(
kwiberg102c6a62015-10-30 02:47:38 -0700856 new webrtc::ExperimentalNs(*experimental_ns_));
buildbot@webrtc.org1f8a2372014-08-28 10:52:44 +0000857 }
buildbot@webrtc.org1f8a2372014-08-28 10:52:44 +0000858
Alejandro Luebsc9b0c262016-05-16 15:32:38 -0700859 if (intelligibility_enhancer_) {
860 LOG(LS_INFO) << "Intelligibility Enhancer is enabled? "
861 << *intelligibility_enhancer_;
862 config.Set<webrtc::Intelligibility>(
863 new webrtc::Intelligibility(*intelligibility_enhancer_));
864 }
865
peaha3333bf2016-06-30 00:02:34 -0700866 if (options.level_control) {
867 level_control_ = options.level_control;
868 }
869
870 LOG(LS_INFO) << "Level control: "
871 << (!!level_control_ ? *level_control_ : -1);
peah88ac8532016-09-12 16:47:25 -0700872 webrtc::AudioProcessing::Config apm_config;
peaha3333bf2016-06-30 00:02:34 -0700873 if (level_control_) {
peah88ac8532016-09-12 16:47:25 -0700874 apm_config.level_controller.enabled = *level_control_;
peaha3333bf2016-06-30 00:02:34 -0700875 }
876
buildbot@webrtc.org1f8a2372014-08-28 10:52:44 +0000877 // We check audioproc for the benefit of tests, since FakeWebRtcVoiceEngine
878 // returns NULL on audio_processing().
879 webrtc::AudioProcessing* audioproc = voe_wrapper_->base()->audio_processing();
880 if (audioproc) {
881 audioproc->SetExtraOptions(config);
peah88ac8532016-09-12 16:47:25 -0700882 audioproc->ApplyConfig(apm_config);
buildbot@webrtc.org1f8a2372014-08-28 10:52:44 +0000883 }
884
kwiberg102c6a62015-10-30 02:47:38 -0700885 if (options.recording_sample_rate) {
886 LOG(LS_INFO) << "Recording sample rate is "
887 << *options.recording_sample_rate;
solenberg5b5129a2016-04-08 05:35:48 -0700888 if (adm()->SetRecordingSampleRate(*options.recording_sample_rate)) {
kwiberg102c6a62015-10-30 02:47:38 -0700889 LOG_RTCERR1(SetRecordingSampleRate, *options.recording_sample_rate);
buildbot@webrtc.org13d67762014-05-02 17:33:29 +0000890 }
891 }
892
kwiberg102c6a62015-10-30 02:47:38 -0700893 if (options.playout_sample_rate) {
894 LOG(LS_INFO) << "Playout sample rate is " << *options.playout_sample_rate;
solenberg5b5129a2016-04-08 05:35:48 -0700895 if (adm()->SetPlayoutSampleRate(*options.playout_sample_rate)) {
kwiberg102c6a62015-10-30 02:47:38 -0700896 LOG_RTCERR1(SetPlayoutSampleRate, *options.playout_sample_rate);
buildbot@webrtc.org13d67762014-05-02 17:33:29 +0000897 }
898 }
899
900 return true;
901}
902
solenberg246b8172015-12-08 09:50:23 -0800903void WebRtcVoiceEngine::SetDefaultDevices() {
solenberg566ef242015-11-06 15:34:49 -0800904 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
kjellanderfcfc8042016-01-14 11:01:09 -0800905#if !defined(WEBRTC_IOS)
solenberg246b8172015-12-08 09:50:23 -0800906 int in_id = kDefaultAudioDeviceId;
907 int out_id = kDefaultAudioDeviceId;
908 LOG(LS_INFO) << "Setting microphone to (id=" << in_id
909 << ") and speaker to (id=" << out_id << ")";
buildbot@webrtc.org13d67762014-05-02 17:33:29 +0000910
solenbergc1a1b352015-09-22 13:31:20 -0700911 bool ret = true;
solenberg246b8172015-12-08 09:50:23 -0800912 if (voe_wrapper_->hw()->SetRecordingDevice(in_id) == -1) {
913 LOG_RTCERR1(SetRecordingDevice, in_id);
buildbot@webrtc.org13d67762014-05-02 17:33:29 +0000914 ret = false;
915 }
solenberg246b8172015-12-08 09:50:23 -0800916 webrtc::AudioProcessing* ap = voe()->base()->audio_processing();
917 if (ap) {
918 ap->Initialize();
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000919 }
920
solenberg246b8172015-12-08 09:50:23 -0800921 if (voe_wrapper_->hw()->SetPlayoutDevice(out_id) == -1) {
922 LOG_RTCERR1(SetPlayoutDevice, out_id);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000923 ret = false;
924 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000925
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000926 if (ret) {
solenberg246b8172015-12-08 09:50:23 -0800927 LOG(LS_INFO) << "Set microphone to (id=" << in_id
928 << ") and speaker to (id=" << out_id << ")";
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000929 }
kjellanderfcfc8042016-01-14 11:01:09 -0800930#endif // !WEBRTC_IOS
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000931}
932
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000933int WebRtcVoiceEngine::GetInputLevel() {
solenberg566ef242015-11-06 15:34:49 -0800934 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000935 unsigned int ulevel;
936 return (voe_wrapper_->volume()->GetSpeechInputLevel(ulevel) != -1) ?
937 static_cast<int>(ulevel) : -1;
938}
939
ossudedfd282016-06-14 07:12:39 -0700940const std::vector<AudioCodec>& WebRtcVoiceEngine::send_codecs() const {
941 RTC_DCHECK(signal_thread_checker_.CalledOnValidThread());
ossuc54071d2016-08-17 02:45:41 -0700942 return send_codecs_;
ossudedfd282016-06-14 07:12:39 -0700943}
944
945const std::vector<AudioCodec>& WebRtcVoiceEngine::recv_codecs() const {
solenberg566ef242015-11-06 15:34:49 -0800946 RTC_DCHECK(signal_thread_checker_.CalledOnValidThread());
ossuc54071d2016-08-17 02:45:41 -0700947 return recv_codecs_;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000948}
949
Stefan Holmer9d69c3f2015-12-07 10:45:43 +0100950RtpCapabilities WebRtcVoiceEngine::GetCapabilities() const {
solenberg566ef242015-11-06 15:34:49 -0800951 RTC_DCHECK(signal_thread_checker_.CalledOnValidThread());
Stefan Holmer9d69c3f2015-12-07 10:45:43 +0100952 RtpCapabilities capabilities;
Stefan Holmer9d69c3f2015-12-07 10:45:43 +0100953 capabilities.header_extensions.push_back(
isheriff6f8d6862016-05-26 11:24:55 -0700954 webrtc::RtpExtension(webrtc::RtpExtension::kAudioLevelUri,
955 webrtc::RtpExtension::kAudioLevelDefaultId));
956 capabilities.header_extensions.push_back(
957 webrtc::RtpExtension(webrtc::RtpExtension::kAbsSendTimeUri,
958 webrtc::RtpExtension::kAbsSendTimeDefaultId));
stefanba4c0e42016-02-04 04:12:24 -0800959 if (webrtc::field_trial::FindFullName("WebRTC-Audio-SendSideBwe") ==
960 "Enabled") {
isheriff6f8d6862016-05-26 11:24:55 -0700961 capabilities.header_extensions.push_back(webrtc::RtpExtension(
962 webrtc::RtpExtension::kTransportSequenceNumberUri,
963 webrtc::RtpExtension::kTransportSequenceNumberDefaultId));
stefanba4c0e42016-02-04 04:12:24 -0800964 }
Stefan Holmer9d69c3f2015-12-07 10:45:43 +0100965 return capabilities;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000966}
967
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000968int WebRtcVoiceEngine::GetLastEngineError() {
solenberg566ef242015-11-06 15:34:49 -0800969 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000970 return voe_wrapper_->error();
971}
972
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000973void WebRtcVoiceEngine::Print(webrtc::TraceLevel level, const char* trace,
974 int length) {
solenberg566ef242015-11-06 15:34:49 -0800975 // Note: This callback can happen on any thread!
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000976 rtc::LoggingSeverity sev = rtc::LS_VERBOSE;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000977 if (level == webrtc::kTraceError || level == webrtc::kTraceCritical)
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000978 sev = rtc::LS_ERROR;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000979 else if (level == webrtc::kTraceWarning)
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000980 sev = rtc::LS_WARNING;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000981 else if (level == webrtc::kTraceStateInfo || level == webrtc::kTraceInfo)
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000982 sev = rtc::LS_INFO;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000983 else if (level == webrtc::kTraceTerseInfo)
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000984 sev = rtc::LS_INFO;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000985
solenberg72e29d22016-03-08 06:35:16 -0800986 // Skip past boilerplate prefix text.
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000987 if (length < 72) {
988 std::string msg(trace, length);
989 LOG(LS_ERROR) << "Malformed webrtc log message: ";
990 LOG_V(sev) << msg;
991 } else {
992 std::string msg(trace + 71, length - 72);
Peter Boströmd5c75b12015-09-23 13:24:32 +0200993 LOG_V(sev) << "webrtc: " << msg;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000994 }
995}
996
solenberg63b34542015-09-29 06:06:31 -0700997void WebRtcVoiceEngine::RegisterChannel(WebRtcVoiceMediaChannel* channel) {
solenberg566ef242015-11-06 15:34:49 -0800998 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
999 RTC_DCHECK(channel);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001000 channels_.push_back(channel);
1001}
1002
solenberg63b34542015-09-29 06:06:31 -07001003void WebRtcVoiceEngine::UnregisterChannel(WebRtcVoiceMediaChannel* channel) {
solenberg566ef242015-11-06 15:34:49 -08001004 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
solenberg63b34542015-09-29 06:06:31 -07001005 auto it = std::find(channels_.begin(), channels_.end(), channel);
solenberg566ef242015-11-06 15:34:49 -08001006 RTC_DCHECK(it != channels_.end());
1007 channels_.erase(it);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001008}
1009
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001010// Adjusts the default AGC target level by the specified delta.
1011// NB: If we start messing with other config fields, we'll want
1012// to save the current webrtc::AgcConfig as well.
1013bool WebRtcVoiceEngine::AdjustAgcLevel(int delta) {
solenberg566ef242015-11-06 15:34:49 -08001014 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001015 webrtc::AgcConfig config = default_agc_config_;
1016 config.targetLeveldBOv -= delta;
1017
1018 LOG(LS_INFO) << "Adjusting AGC level from default -"
1019 << default_agc_config_.targetLeveldBOv << "dB to -"
1020 << config.targetLeveldBOv << "dB";
1021
1022 if (voe_wrapper_->processing()->SetAgcConfig(config) == -1) {
1023 LOG_RTCERR1(SetAgcConfig, config.targetLeveldBOv);
1024 return false;
1025 }
1026 return true;
1027}
1028
ivocd66b44d2016-01-15 03:06:36 -08001029bool WebRtcVoiceEngine::StartAecDump(rtc::PlatformFile file,
1030 int64_t max_size_bytes) {
solenberg566ef242015-11-06 15:34:49 -08001031 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00001032 FILE* aec_dump_file_stream = rtc::FdopenPlatformFileForWriting(file);
wu@webrtc.orga8910d22014-01-23 22:12:45 +00001033 if (!aec_dump_file_stream) {
1034 LOG(LS_ERROR) << "Could not open AEC dump file stream.";
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00001035 if (!rtc::ClosePlatformFile(file))
wu@webrtc.orga8910d22014-01-23 22:12:45 +00001036 LOG(LS_WARNING) << "Could not close file.";
1037 return false;
1038 }
wu@webrtc.orga9890802013-12-13 00:21:03 +00001039 StopAecDump();
ivocd66b44d2016-01-15 03:06:36 -08001040 if (voe_wrapper_->base()->audio_processing()->StartDebugRecording(
1041 aec_dump_file_stream, max_size_bytes) !=
wu@webrtc.orga9890802013-12-13 00:21:03 +00001042 webrtc::AudioProcessing::kNoError) {
wu@webrtc.orga8910d22014-01-23 22:12:45 +00001043 LOG_RTCERR0(StartDebugRecording);
1044 fclose(aec_dump_file_stream);
wu@webrtc.orga9890802013-12-13 00:21:03 +00001045 return false;
1046 }
1047 is_dumping_aec_ = true;
1048 return true;
wu@webrtc.orga9890802013-12-13 00:21:03 +00001049}
1050
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001051void WebRtcVoiceEngine::StartAecDump(const std::string& filename) {
solenberg566ef242015-11-06 15:34:49 -08001052 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001053 if (!is_dumping_aec_) {
1054 // Start dumping AEC when we are not dumping.
ivocd66b44d2016-01-15 03:06:36 -08001055 if (voe_wrapper_->base()->audio_processing()->StartDebugRecording(
1056 filename.c_str(), -1) != webrtc::AudioProcessing::kNoError) {
wu@webrtc.orga9890802013-12-13 00:21:03 +00001057 LOG_RTCERR1(StartDebugRecording, filename.c_str());
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001058 } else {
1059 is_dumping_aec_ = true;
1060 }
1061 }
1062}
1063
1064void WebRtcVoiceEngine::StopAecDump() {
solenberg566ef242015-11-06 15:34:49 -08001065 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001066 if (is_dumping_aec_) {
1067 // Stop dumping AEC when we are dumping.
ivocd66b44d2016-01-15 03:06:36 -08001068 if (voe_wrapper_->base()->audio_processing()->StopDebugRecording() !=
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001069 webrtc::AudioProcessing::kNoError) {
1070 LOG_RTCERR0(StopDebugRecording);
1071 }
1072 is_dumping_aec_ = false;
1073 }
1074}
1075
solenberg0a617e22015-10-20 15:49:38 -07001076int WebRtcVoiceEngine::CreateVoEChannel() {
solenberg566ef242015-11-06 15:34:49 -08001077 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
solenberg88499ec2016-09-07 07:34:41 -07001078 return voe_wrapper_->base()->CreateChannel(channel_config_);
sergeyu@chromium.org5bc25c42013-12-05 00:24:06 +00001079}
1080
solenberg5b5129a2016-04-08 05:35:48 -07001081webrtc::AudioDeviceModule* WebRtcVoiceEngine::adm() {
1082 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
1083 RTC_DCHECK(adm_);
1084 return adm_;
1085}
1086
ossuc54071d2016-08-17 02:45:41 -07001087AudioCodecs WebRtcVoiceEngine::CollectRecvCodecs() const {
1088 PayloadTypeMapper mapper;
1089 AudioCodecs out;
ossud4e9f622016-08-18 02:01:17 -07001090 const std::vector<webrtc::AudioCodecSpec>& specs =
1091 decoder_factory_->GetSupportedDecoders();
ossuc54071d2016-08-17 02:45:41 -07001092
1093 // Only generate CN payload types for these clockrates
1094 std::map<int, bool, std::greater<int>> generate_cn = {{ 8000, false },
1095 { 16000, false },
1096 { 32000, false }};
1097
1098 auto map_format = [&mapper, &out] (const webrtc::SdpAudioFormat& format) {
1099 rtc::Optional<AudioCodec> opt_codec = mapper.ToAudioCodec(format);
1100 if (!opt_codec) {
1101 LOG(LS_ERROR) << "Unable to assign payload type to format: " << format;
1102 return false;
1103 }
1104
1105 auto& codec = *opt_codec;
1106 if (IsCodec(codec, kOpusCodecName)) {
1107 // TODO(ossu): Set this specifically for Opus for now, until we have a
1108 // better way of dealing with rtcp-fb parameters.
1109 codec.AddFeedbackParam(
1110 FeedbackParam(kRtcpFbParamTransportCc, kParamValueEmpty));
1111 }
1112 out.push_back(codec);
1113 return true;
1114 };
1115
ossud4e9f622016-08-18 02:01:17 -07001116 for (const auto& spec : specs) {
1117 if (map_format(spec.format) && spec.allow_comfort_noise) {
1118 // Generate a CN entry if the decoder allows it and we support the
1119 // clockrate.
1120 auto cn = generate_cn.find(spec.format.clockrate_hz);
1121 if (cn != generate_cn.end()) {
ossuc54071d2016-08-17 02:45:41 -07001122 cn->second = true;
1123 }
1124 }
1125 }
1126
1127 // Add CN codecs after "proper" audio codecs
1128 for (const auto& cn : generate_cn) {
1129 if (cn.second) {
1130 map_format({kCnCodecName, cn.first, 1});
1131 }
1132 }
1133
1134 // Add telephone-event codec last
1135 map_format({kDtmfCodecName, 8000, 1});
1136
1137 return out;
1138}
1139
solenbergc96df772015-10-21 13:01:53 -07001140class WebRtcVoiceMediaChannel::WebRtcAudioSendStream
Taylor Brandstetter1a018dc2016-03-08 12:37:39 -08001141 : public AudioSource::Sink {
mallinath@webrtc.org67ee6b92014-02-03 16:57:16 +00001142 public:
skvlade0d46372016-04-07 22:59:22 -07001143 WebRtcAudioSendStream(int ch,
1144 webrtc::AudioTransport* voe_audio_transport,
1145 uint32_t ssrc,
1146 const std::string& c_name,
solenberg971cab02016-06-14 10:02:41 -07001147 const SendCodecSpec& send_codec_spec,
solenberg3a941542015-11-16 07:34:50 -08001148 const std::vector<webrtc::RtpExtension>& extensions,
mflodman3d7db262016-04-29 00:57:13 -07001149 webrtc::Call* call,
1150 webrtc::Transport* send_transport)
solenberg7add0582015-11-20 09:59:34 -08001151 : voe_audio_transport_(voe_audio_transport),
solenberg3a941542015-11-16 07:34:50 -08001152 call_(call),
mflodman3d7db262016-04-29 00:57:13 -07001153 config_(send_transport),
skvlade0d46372016-04-07 22:59:22 -07001154 rtp_parameters_(CreateRtpParametersWithOneEncoding()) {
solenberg85a04962015-10-27 03:35:21 -07001155 RTC_DCHECK_GE(ch, 0);
1156 // TODO(solenberg): Once we're not using FakeWebRtcVoiceEngine anymore:
1157 // RTC_DCHECK(voe_audio_transport);
solenbergc96df772015-10-21 13:01:53 -07001158 RTC_DCHECK(call);
solenberg3a941542015-11-16 07:34:50 -08001159 config_.rtp.ssrc = ssrc;
1160 config_.rtp.c_name = c_name;
1161 config_.voe_channel_id = ch;
solenberg971cab02016-06-14 10:02:41 -07001162 config_.rtp.extensions = extensions;
1163 RecreateAudioSendStream(send_codec_spec);
solenbergc96df772015-10-21 13:01:53 -07001164 }
solenberg3a941542015-11-16 07:34:50 -08001165
solenbergc96df772015-10-21 13:01:53 -07001166 ~WebRtcAudioSendStream() override {
solenberg566ef242015-11-06 15:34:49 -08001167 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
Taylor Brandstetter1a018dc2016-03-08 12:37:39 -08001168 ClearSource();
solenbergc96df772015-10-21 13:01:53 -07001169 call_->DestroyAudioSendStream(stream_);
1170 }
henrike@webrtc.org1e09a712013-07-26 19:17:59 +00001171
solenberg971cab02016-06-14 10:02:41 -07001172 void RecreateAudioSendStream(const SendCodecSpec& send_codec_spec) {
1173 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
1174 if (stream_) {
1175 call_->DestroyAudioSendStream(stream_);
1176 stream_ = nullptr;
1177 }
1178 config_.rtp.nack.rtp_history_ms =
1179 send_codec_spec.nack_enabled ? kNackRtpHistoryMs : 0;
1180 RTC_DCHECK(!stream_);
1181 stream_ = call_->CreateAudioSendStream(config_);
1182 RTC_CHECK(stream_);
1183 UpdateSendState();
1184 }
1185
solenberg3a941542015-11-16 07:34:50 -08001186 void RecreateAudioSendStream(
1187 const std::vector<webrtc::RtpExtension>& extensions) {
1188 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
1189 if (stream_) {
1190 call_->DestroyAudioSendStream(stream_);
1191 stream_ = nullptr;
1192 }
1193 config_.rtp.extensions = extensions;
mflodman86cc6ff2016-07-26 04:44:06 -07001194 if (webrtc::field_trial::FindFullName("WebRTC-AdaptAudioBitrate") ==
1195 "Enabled") {
1196 // TODO(mflodman): Keep testing this and set proper values.
1197 // Note: This is an early experiment currently only supported by Opus.
1198 config_.min_bitrate_kbps = kOpusMinBitrate;
1199 config_.max_bitrate_kbps = kOpusBitrateFb;
1200 }
1201
solenberg3a941542015-11-16 07:34:50 -08001202 RTC_DCHECK(!stream_);
1203 stream_ = call_->CreateAudioSendStream(config_);
1204 RTC_CHECK(stream_);
solenberg6d6e7c52016-04-13 09:07:30 -07001205 UpdateSendState();
solenberg3a941542015-11-16 07:34:50 -08001206 }
1207
solenberg8842c3e2016-03-11 03:06:41 -08001208 bool SendTelephoneEvent(int payload_type, int event, int duration_ms) {
Fredrik Solenbergb5727682015-12-04 15:22:19 +01001209 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
1210 RTC_DCHECK(stream_);
1211 return stream_->SendTelephoneEvent(payload_type, event, duration_ms);
1212 }
1213
Taylor Brandstetter1a018dc2016-03-08 12:37:39 -08001214 void SetSend(bool send) {
1215 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
1216 send_ = send;
1217 UpdateSendState();
1218 }
1219
solenberg94218532016-06-16 10:53:22 -07001220 void SetMuted(bool muted) {
1221 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
1222 RTC_DCHECK(stream_);
1223 stream_->SetMuted(muted);
1224 muted_ = muted;
1225 }
1226
1227 bool muted() const {
1228 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
1229 return muted_;
1230 }
1231
solenberg3a941542015-11-16 07:34:50 -08001232 webrtc::AudioSendStream::Stats GetStats() const {
1233 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
1234 RTC_DCHECK(stream_);
1235 return stream_->GetStats();
1236 }
1237
Taylor Brandstetter1a018dc2016-03-08 12:37:39 -08001238 // Starts the sending by setting ourselves as a sink to the AudioSource to
1239 // get data callbacks.
henrike@webrtc.orga7b98182014-02-21 15:51:43 +00001240 // This method is called on the libjingle worker thread.
mallinath@webrtc.org67ee6b92014-02-03 16:57:16 +00001241 // TODO(xians): Make sure Start() is called only once.
Taylor Brandstetter1a018dc2016-03-08 12:37:39 -08001242 void SetSource(AudioSource* source) {
solenberg566ef242015-11-06 15:34:49 -08001243 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
Taylor Brandstetter1a018dc2016-03-08 12:37:39 -08001244 RTC_DCHECK(source);
1245 if (source_) {
1246 RTC_DCHECK(source_ == source);
mallinath@webrtc.org67ee6b92014-02-03 16:57:16 +00001247 return;
1248 }
Taylor Brandstetter1a018dc2016-03-08 12:37:39 -08001249 source->SetSink(this);
1250 source_ = source;
1251 UpdateSendState();
mallinath@webrtc.org67ee6b92014-02-03 16:57:16 +00001252 }
1253
Taylor Brandstetter1a018dc2016-03-08 12:37:39 -08001254 // Stops sending by setting the sink of the AudioSource to nullptr. No data
mallinath@webrtc.org67ee6b92014-02-03 16:57:16 +00001255 // callback will be received after this method.
henrike@webrtc.orga7b98182014-02-21 15:51:43 +00001256 // This method is called on the libjingle worker thread.
Taylor Brandstetter1a018dc2016-03-08 12:37:39 -08001257 void ClearSource() {
solenberg566ef242015-11-06 15:34:49 -08001258 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
Taylor Brandstetter1a018dc2016-03-08 12:37:39 -08001259 if (source_) {
1260 source_->SetSink(nullptr);
1261 source_ = nullptr;
solenberg98c68862015-10-09 03:27:14 -07001262 }
Taylor Brandstetter1a018dc2016-03-08 12:37:39 -08001263 UpdateSendState();
mallinath@webrtc.org67ee6b92014-02-03 16:57:16 +00001264 }
1265
Taylor Brandstetter1a018dc2016-03-08 12:37:39 -08001266 // AudioSource::Sink implementation.
henrike@webrtc.orga7b98182014-02-21 15:51:43 +00001267 // This method is called on the audio thread.
kjellander@webrtc.org14665ff2015-03-04 12:58:35 +00001268 void OnData(const void* audio_data,
1269 int bits_per_sample,
1270 int sample_rate,
Peter Kasting69558702016-01-12 16:26:35 -08001271 size_t number_of_channels,
Peter Kastingdce40cf2015-08-24 14:52:23 -07001272 size_t number_of_frames) override {
solenberg347ec5c2016-09-23 04:21:47 -07001273 RTC_CHECK_RUNS_SERIALIZED(&audio_capture_race_checker_);
solenbergc96df772015-10-21 13:01:53 -07001274 RTC_DCHECK(voe_audio_transport_);
maxmorin1aee0b52016-08-15 11:46:19 -07001275 voe_audio_transport_->PushCaptureData(config_.voe_channel_id, audio_data,
1276 bits_per_sample, sample_rate,
1277 number_of_channels, number_of_frames);
henrike@webrtc.orga7b98182014-02-21 15:51:43 +00001278 }
1279
Taylor Brandstetter1a018dc2016-03-08 12:37:39 -08001280 // Callback from the |source_| when it is going away. In case Start() has
henrike@webrtc.orga7b98182014-02-21 15:51:43 +00001281 // never been called, this callback won't be triggered.
kjellander@webrtc.org14665ff2015-03-04 12:58:35 +00001282 void OnClose() override {
solenberg566ef242015-11-06 15:34:49 -08001283 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
Taylor Brandstetter1a018dc2016-03-08 12:37:39 -08001284 // Set |source_| to nullptr to make sure no more callback will get into
1285 // the source.
1286 source_ = nullptr;
1287 UpdateSendState();
mallinath@webrtc.org67ee6b92014-02-03 16:57:16 +00001288 }
1289
1290 // Accessor to the VoE channel ID.
solenberg85a04962015-10-27 03:35:21 -07001291 int channel() const {
solenberg566ef242015-11-06 15:34:49 -08001292 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
solenberg7add0582015-11-20 09:59:34 -08001293 return config_.voe_channel_id;
solenberg85a04962015-10-27 03:35:21 -07001294 }
mallinath@webrtc.org67ee6b92014-02-03 16:57:16 +00001295
skvlade0d46372016-04-07 22:59:22 -07001296 const webrtc::RtpParameters& rtp_parameters() const {
1297 return rtp_parameters_;
1298 }
1299
Taylor Brandstetter55dd7082016-05-03 13:50:11 -07001300 void SetRtpParameters(const webrtc::RtpParameters& parameters) {
skvlade0d46372016-04-07 22:59:22 -07001301 RTC_CHECK_EQ(1UL, parameters.encodings.size());
1302 rtp_parameters_ = parameters;
Taylor Brandstetter55dd7082016-05-03 13:50:11 -07001303 // parameters.encodings[0].active could have changed.
1304 UpdateSendState();
skvlade0d46372016-04-07 22:59:22 -07001305 }
1306
mallinath@webrtc.org67ee6b92014-02-03 16:57:16 +00001307 private:
Taylor Brandstetter1a018dc2016-03-08 12:37:39 -08001308 void UpdateSendState() {
1309 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
1310 RTC_DCHECK(stream_);
Taylor Brandstetter55dd7082016-05-03 13:50:11 -07001311 RTC_DCHECK_EQ(1UL, rtp_parameters_.encodings.size());
1312 if (send_ && source_ != nullptr && rtp_parameters_.encodings[0].active) {
Taylor Brandstetter1a018dc2016-03-08 12:37:39 -08001313 stream_->Start();
1314 } else { // !send || source_ = nullptr
1315 stream_->Stop();
1316 }
1317 }
1318
solenberg566ef242015-11-06 15:34:49 -08001319 rtc::ThreadChecker worker_thread_checker_;
solenberg347ec5c2016-09-23 04:21:47 -07001320 rtc::RaceChecker audio_capture_race_checker_;
solenbergc96df772015-10-21 13:01:53 -07001321 webrtc::AudioTransport* const voe_audio_transport_ = nullptr;
1322 webrtc::Call* call_ = nullptr;
solenberg3a941542015-11-16 07:34:50 -08001323 webrtc::AudioSendStream::Config config_;
1324 // The stream is owned by WebRtcAudioSendStream and may be reallocated if
1325 // configuration changes.
solenbergc96df772015-10-21 13:01:53 -07001326 webrtc::AudioSendStream* stream_ = nullptr;
mallinath@webrtc.org67ee6b92014-02-03 16:57:16 +00001327
Taylor Brandstetter1a018dc2016-03-08 12:37:39 -08001328 // Raw pointer to AudioSource owned by LocalAudioTrackHandler.
mallinath@webrtc.org67ee6b92014-02-03 16:57:16 +00001329 // PeerConnection will make sure invalidating the pointer before the object
1330 // goes away.
Taylor Brandstetter1a018dc2016-03-08 12:37:39 -08001331 AudioSource* source_ = nullptr;
1332 bool send_ = false;
solenberg94218532016-06-16 10:53:22 -07001333 bool muted_ = false;
skvlade0d46372016-04-07 22:59:22 -07001334 webrtc::RtpParameters rtp_parameters_;
henrike@webrtc.orga7b98182014-02-21 15:51:43 +00001335
solenbergc96df772015-10-21 13:01:53 -07001336 RTC_DISALLOW_IMPLICIT_CONSTRUCTORS(WebRtcAudioSendStream);
1337};
1338
1339class WebRtcVoiceMediaChannel::WebRtcAudioReceiveStream {
1340 public:
ossu29b1a8d2016-06-13 07:34:51 -07001341 WebRtcAudioReceiveStream(
1342 int ch,
1343 uint32_t remote_ssrc,
1344 uint32_t local_ssrc,
1345 bool use_transport_cc,
solenberg8189b022016-06-14 12:13:00 -07001346 bool use_nack,
ossu29b1a8d2016-06-13 07:34:51 -07001347 const std::string& sync_group,
1348 const std::vector<webrtc::RtpExtension>& extensions,
1349 webrtc::Call* call,
1350 webrtc::Transport* rtcp_send_transport,
1351 const rtc::scoped_refptr<webrtc::AudioDecoderFactory>& decoder_factory)
stefanba4c0e42016-02-04 04:12:24 -08001352 : call_(call), config_() {
solenberg7add0582015-11-20 09:59:34 -08001353 RTC_DCHECK_GE(ch, 0);
1354 RTC_DCHECK(call);
1355 config_.rtp.remote_ssrc = remote_ssrc;
solenberg31fec402016-05-06 02:13:12 -07001356 config_.rtcp_send_transport = rtcp_send_transport;
solenberg7add0582015-11-20 09:59:34 -08001357 config_.voe_channel_id = ch;
1358 config_.sync_group = sync_group;
ossu29b1a8d2016-06-13 07:34:51 -07001359 config_.decoder_factory = decoder_factory;
solenberg4a0f7b52016-06-16 13:07:33 -07001360 RecreateAudioReceiveStream(local_ssrc,
1361 use_transport_cc,
1362 use_nack,
1363 extensions);
solenberg7add0582015-11-20 09:59:34 -08001364 }
solenbergc96df772015-10-21 13:01:53 -07001365
solenberg7add0582015-11-20 09:59:34 -08001366 ~WebRtcAudioReceiveStream() {
1367 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
1368 call_->DestroyAudioReceiveStream(stream_);
1369 }
1370
solenberg4a0f7b52016-06-16 13:07:33 -07001371 void RecreateAudioReceiveStream(uint32_t local_ssrc) {
solenberg7add0582015-11-20 09:59:34 -08001372 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
solenberg4a0f7b52016-06-16 13:07:33 -07001373 RecreateAudioReceiveStream(local_ssrc,
1374 config_.rtp.transport_cc,
solenberg8189b022016-06-14 12:13:00 -07001375 config_.rtp.nack.rtp_history_ms != 0,
solenberg4a0f7b52016-06-16 13:07:33 -07001376 config_.rtp.extensions);
solenberg7add0582015-11-20 09:59:34 -08001377 }
solenberg8189b022016-06-14 12:13:00 -07001378
1379 void RecreateAudioReceiveStream(bool use_transport_cc, bool use_nack) {
solenberg7add0582015-11-20 09:59:34 -08001380 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
solenberg4a0f7b52016-06-16 13:07:33 -07001381 RecreateAudioReceiveStream(config_.rtp.local_ssrc,
1382 use_transport_cc,
solenberg8189b022016-06-14 12:13:00 -07001383 use_nack,
1384 config_.rtp.extensions);
solenberg7add0582015-11-20 09:59:34 -08001385 }
1386
solenberg4a0f7b52016-06-16 13:07:33 -07001387 void RecreateAudioReceiveStream(
1388 const std::vector<webrtc::RtpExtension>& extensions) {
1389 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
1390 RecreateAudioReceiveStream(config_.rtp.local_ssrc,
1391 config_.rtp.transport_cc,
1392 config_.rtp.nack.rtp_history_ms != 0,
1393 extensions);
1394 }
1395
solenberg7add0582015-11-20 09:59:34 -08001396 webrtc::AudioReceiveStream::Stats GetStats() const {
1397 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
1398 RTC_DCHECK(stream_);
1399 return stream_->GetStats();
1400 }
1401
1402 int channel() const {
1403 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
1404 return config_.voe_channel_id;
1405 }
solenbergc96df772015-10-21 13:01:53 -07001406
kwiberg686a8ef2016-02-26 03:00:35 -08001407 void SetRawAudioSink(std::unique_ptr<webrtc::AudioSinkInterface> sink) {
Tommif888bb52015-12-12 01:37:01 +01001408 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
kwiberg686a8ef2016-02-26 03:00:35 -08001409 stream_->SetSink(std::move(sink));
Tommif888bb52015-12-12 01:37:01 +01001410 }
1411
solenberg217fb662016-06-17 08:30:54 -07001412 void SetOutputVolume(double volume) {
1413 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
1414 stream_->SetGain(volume);
1415 }
1416
aleloi84ef6152016-08-04 05:28:21 -07001417 void SetPlayout(bool playout) {
1418 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
1419 RTC_DCHECK(stream_);
1420 if (playout) {
1421 LOG(LS_INFO) << "Starting playout for channel #" << channel();
1422 stream_->Start();
1423 } else {
1424 LOG(LS_INFO) << "Stopping playout for channel #" << channel();
1425 stream_->Stop();
1426 }
1427 }
1428
solenbergc96df772015-10-21 13:01:53 -07001429 private:
stefanba4c0e42016-02-04 04:12:24 -08001430 void RecreateAudioReceiveStream(
solenberg4a0f7b52016-06-16 13:07:33 -07001431 uint32_t local_ssrc,
stefanba4c0e42016-02-04 04:12:24 -08001432 bool use_transport_cc,
solenberg8189b022016-06-14 12:13:00 -07001433 bool use_nack,
solenberg7add0582015-11-20 09:59:34 -08001434 const std::vector<webrtc::RtpExtension>& extensions) {
1435 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
1436 if (stream_) {
1437 call_->DestroyAudioReceiveStream(stream_);
1438 stream_ = nullptr;
1439 }
solenberg4a0f7b52016-06-16 13:07:33 -07001440 config_.rtp.local_ssrc = local_ssrc;
stefanba4c0e42016-02-04 04:12:24 -08001441 config_.rtp.transport_cc = use_transport_cc;
solenberg8189b022016-06-14 12:13:00 -07001442 config_.rtp.nack.rtp_history_ms = use_nack ? kNackRtpHistoryMs : 0;
1443 config_.rtp.extensions = extensions;
solenberg7add0582015-11-20 09:59:34 -08001444 RTC_DCHECK(!stream_);
1445 stream_ = call_->CreateAudioReceiveStream(config_);
1446 RTC_CHECK(stream_);
1447 }
1448
1449 rtc::ThreadChecker worker_thread_checker_;
1450 webrtc::Call* call_ = nullptr;
1451 webrtc::AudioReceiveStream::Config config_;
1452 // The stream is owned by WebRtcAudioReceiveStream and may be reallocated if
1453 // configuration changes.
1454 webrtc::AudioReceiveStream* stream_ = nullptr;
solenbergc96df772015-10-21 13:01:53 -07001455
1456 RTC_DISALLOW_IMPLICIT_CONSTRUCTORS(WebRtcAudioReceiveStream);
henrike@webrtc.org1e09a712013-07-26 19:17:59 +00001457};
1458
Fredrik Solenberg709ed672015-09-15 12:26:33 +02001459WebRtcVoiceMediaChannel::WebRtcVoiceMediaChannel(WebRtcVoiceEngine* engine,
nisse51542be2016-02-12 02:27:06 -08001460 const MediaConfig& config,
Fredrik Solenbergb071a192015-09-17 16:42:56 +02001461 const AudioOptions& options,
Fredrik Solenberg709ed672015-09-15 12:26:33 +02001462 webrtc::Call* call)
nisse51542be2016-02-12 02:27:06 -08001463 : VoiceMediaChannel(config), engine_(engine), call_(call) {
solenberg0a617e22015-10-20 15:49:38 -07001464 LOG(LS_VERBOSE) << "WebRtcVoiceMediaChannel::WebRtcVoiceMediaChannel";
solenberg566ef242015-11-06 15:34:49 -08001465 RTC_DCHECK(call);
solenberg0a617e22015-10-20 15:49:38 -07001466 engine->RegisterChannel(this);
Fredrik Solenbergb071a192015-09-17 16:42:56 +02001467 SetOptions(options);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001468}
1469
1470WebRtcVoiceMediaChannel::~WebRtcVoiceMediaChannel() {
solenberg566ef242015-11-06 15:34:49 -08001471 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
solenberg0a617e22015-10-20 15:49:38 -07001472 LOG(LS_VERBOSE) << "WebRtcVoiceMediaChannel::~WebRtcVoiceMediaChannel";
solenberg7add0582015-11-20 09:59:34 -08001473 // TODO(solenberg): Should be able to delete the streams directly, without
1474 // going through RemoveNnStream(), once stream objects handle
1475 // all (de)configuration.
solenbergc96df772015-10-21 13:01:53 -07001476 while (!send_streams_.empty()) {
1477 RemoveSendStream(send_streams_.begin()->first);
solenbergd97ec302015-10-07 01:40:33 -07001478 }
solenberg7add0582015-11-20 09:59:34 -08001479 while (!recv_streams_.empty()) {
1480 RemoveRecvStream(recv_streams_.begin()->first);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001481 }
solenberg0a617e22015-10-20 15:49:38 -07001482 engine()->UnregisterChannel(this);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001483}
1484
nisse51542be2016-02-12 02:27:06 -08001485rtc::DiffServCodePoint WebRtcVoiceMediaChannel::PreferredDscp() const {
1486 return kAudioDscpValue;
1487}
1488
Peter Thatcherc2ee2c82015-08-07 16:05:34 -07001489bool WebRtcVoiceMediaChannel::SetSendParameters(
1490 const AudioSendParameters& params) {
Peter Boströmca8b4042016-03-08 14:24:13 -08001491 TRACE_EVENT0("webrtc", "WebRtcVoiceMediaChannel::SetSendParameters");
solenberg566ef242015-11-06 15:34:49 -08001492 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
solenberg7e4e01a2015-12-02 08:05:01 -08001493 LOG(LS_INFO) << "WebRtcVoiceMediaChannel::SetSendParameters: "
1494 << params.ToString();
Peter Thatcherc2ee2c82015-08-07 16:05:34 -07001495 // TODO(pthatcher): Refactor this to be more clean now that we have
1496 // all the information at once.
solenberg3a941542015-11-16 07:34:50 -08001497
1498 if (!SetSendCodecs(params.codecs)) {
1499 return false;
1500 }
1501
solenberg7e4e01a2015-12-02 08:05:01 -08001502 if (!ValidateRtpExtensions(params.extensions)) {
1503 return false;
1504 }
1505 std::vector<webrtc::RtpExtension> filtered_extensions =
1506 FilterRtpExtensions(params.extensions,
1507 webrtc::RtpExtension::IsSupportedForAudio, true);
1508 if (send_rtp_extensions_ != filtered_extensions) {
1509 send_rtp_extensions_.swap(filtered_extensions);
solenberg3a941542015-11-16 07:34:50 -08001510 for (auto& it : send_streams_) {
1511 it.second->RecreateAudioSendStream(send_rtp_extensions_);
1512 }
1513 }
1514
deadbeef80346142016-04-27 14:17:10 -07001515 if (!SetMaxSendBitrate(params.max_bandwidth_bps)) {
solenberg3a941542015-11-16 07:34:50 -08001516 return false;
1517 }
1518 return SetOptions(params.options);
Peter Thatcherc2ee2c82015-08-07 16:05:34 -07001519}
1520
1521bool WebRtcVoiceMediaChannel::SetRecvParameters(
1522 const AudioRecvParameters& params) {
Peter Boströmca8b4042016-03-08 14:24:13 -08001523 TRACE_EVENT0("webrtc", "WebRtcVoiceMediaChannel::SetRecvParameters");
solenberg566ef242015-11-06 15:34:49 -08001524 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
solenberg7e4e01a2015-12-02 08:05:01 -08001525 LOG(LS_INFO) << "WebRtcVoiceMediaChannel::SetRecvParameters: "
1526 << params.ToString();
Peter Thatcherc2ee2c82015-08-07 16:05:34 -07001527 // TODO(pthatcher): Refactor this to be more clean now that we have
1528 // all the information at once.
solenberg7add0582015-11-20 09:59:34 -08001529
1530 if (!SetRecvCodecs(params.codecs)) {
1531 return false;
1532 }
1533
solenberg7e4e01a2015-12-02 08:05:01 -08001534 if (!ValidateRtpExtensions(params.extensions)) {
1535 return false;
1536 }
1537 std::vector<webrtc::RtpExtension> filtered_extensions =
1538 FilterRtpExtensions(params.extensions,
1539 webrtc::RtpExtension::IsSupportedForAudio, false);
1540 if (recv_rtp_extensions_ != filtered_extensions) {
1541 recv_rtp_extensions_.swap(filtered_extensions);
solenberg7add0582015-11-20 09:59:34 -08001542 for (auto& it : recv_streams_) {
1543 it.second->RecreateAudioReceiveStream(recv_rtp_extensions_);
1544 }
1545 }
solenberg7add0582015-11-20 09:59:34 -08001546 return true;
Peter Thatcherc2ee2c82015-08-07 16:05:34 -07001547}
1548
Taylor Brandstetterdb0cd9e2016-05-16 11:40:30 -07001549webrtc::RtpParameters WebRtcVoiceMediaChannel::GetRtpSendParameters(
skvlade0d46372016-04-07 22:59:22 -07001550 uint32_t ssrc) const {
1551 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
1552 auto it = send_streams_.find(ssrc);
1553 if (it == send_streams_.end()) {
Taylor Brandstetterdb0cd9e2016-05-16 11:40:30 -07001554 LOG(LS_WARNING) << "Attempting to get RTP send parameters for stream "
1555 << "with ssrc " << ssrc << " which doesn't exist.";
skvlade0d46372016-04-07 22:59:22 -07001556 return webrtc::RtpParameters();
1557 }
1558
Taylor Brandstetter0cd086b2016-04-20 16:23:10 -07001559 webrtc::RtpParameters rtp_params = it->second->rtp_parameters();
1560 // Need to add the common list of codecs to the send stream-specific
1561 // RTP parameters.
1562 for (const AudioCodec& codec : send_codecs_) {
1563 rtp_params.codecs.push_back(codec.ToCodecParameters());
1564 }
1565 return rtp_params;
skvlade0d46372016-04-07 22:59:22 -07001566}
1567
Taylor Brandstetterdb0cd9e2016-05-16 11:40:30 -07001568bool WebRtcVoiceMediaChannel::SetRtpSendParameters(
skvlade0d46372016-04-07 22:59:22 -07001569 uint32_t ssrc,
1570 const webrtc::RtpParameters& parameters) {
1571 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
1572 if (!ValidateRtpParameters(parameters)) {
1573 return false;
1574 }
1575 auto it = send_streams_.find(ssrc);
1576 if (it == send_streams_.end()) {
Taylor Brandstetterdb0cd9e2016-05-16 11:40:30 -07001577 LOG(LS_WARNING) << "Attempting to set RTP send parameters for stream "
1578 << "with ssrc " << ssrc << " which doesn't exist.";
skvlade0d46372016-04-07 22:59:22 -07001579 return false;
1580 }
1581
Taylor Brandstetterdb0cd9e2016-05-16 11:40:30 -07001582 // TODO(deadbeef): Handle setting parameters with a list of codecs in a
1583 // different order (which should change the send codec).
1584 webrtc::RtpParameters current_parameters = GetRtpSendParameters(ssrc);
1585 if (current_parameters.codecs != parameters.codecs) {
1586 LOG(LS_ERROR) << "Using SetParameters to change the set of codecs "
1587 << "is not currently supported.";
1588 return false;
1589 }
1590
1591 if (!SetChannelSendParameters(it->second->channel(), parameters)) {
1592 LOG(LS_WARNING) << "Failed to set send RtpParameters.";
skvlade0d46372016-04-07 22:59:22 -07001593 return false;
1594 }
Taylor Brandstetter0cd086b2016-04-20 16:23:10 -07001595 // Codecs are handled at the WebRtcVoiceMediaChannel level.
1596 webrtc::RtpParameters reduced_params = parameters;
1597 reduced_params.codecs.clear();
Taylor Brandstetter55dd7082016-05-03 13:50:11 -07001598 it->second->SetRtpParameters(reduced_params);
skvlade0d46372016-04-07 22:59:22 -07001599 return true;
1600}
1601
Taylor Brandstetterdb0cd9e2016-05-16 11:40:30 -07001602webrtc::RtpParameters WebRtcVoiceMediaChannel::GetRtpReceiveParameters(
1603 uint32_t ssrc) const {
1604 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
1605 auto it = recv_streams_.find(ssrc);
1606 if (it == recv_streams_.end()) {
1607 LOG(LS_WARNING) << "Attempting to get RTP receive parameters for stream "
1608 << "with ssrc " << ssrc << " which doesn't exist.";
1609 return webrtc::RtpParameters();
1610 }
1611
1612 // TODO(deadbeef): Return stream-specific parameters.
1613 webrtc::RtpParameters rtp_params = CreateRtpParametersWithOneEncoding();
1614 for (const AudioCodec& codec : recv_codecs_) {
1615 rtp_params.codecs.push_back(codec.ToCodecParameters());
1616 }
1617 return rtp_params;
1618}
1619
1620bool WebRtcVoiceMediaChannel::SetRtpReceiveParameters(
1621 uint32_t ssrc,
1622 const webrtc::RtpParameters& parameters) {
1623 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
1624 if (!ValidateRtpParameters(parameters)) {
1625 return false;
1626 }
1627 auto it = recv_streams_.find(ssrc);
1628 if (it == recv_streams_.end()) {
1629 LOG(LS_WARNING) << "Attempting to set RTP receive parameters for stream "
1630 << "with ssrc " << ssrc << " which doesn't exist.";
1631 return false;
1632 }
1633
1634 webrtc::RtpParameters current_parameters = GetRtpReceiveParameters(ssrc);
1635 if (current_parameters != parameters) {
1636 LOG(LS_ERROR) << "Changing the RTP receive parameters is currently "
1637 << "unsupported.";
1638 return false;
1639 }
1640 return true;
1641}
1642
skvlade0d46372016-04-07 22:59:22 -07001643bool WebRtcVoiceMediaChannel::ValidateRtpParameters(
1644 const webrtc::RtpParameters& rtp_parameters) {
1645 if (rtp_parameters.encodings.size() != 1) {
1646 LOG(LS_ERROR)
1647 << "Attempted to set RtpParameters without exactly one encoding";
1648 return false;
1649 }
1650 return true;
1651}
1652
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001653bool WebRtcVoiceMediaChannel::SetOptions(const AudioOptions& options) {
solenberg566ef242015-11-06 15:34:49 -08001654 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001655 LOG(LS_INFO) << "Setting voice channel options: "
1656 << options.ToString();
1657
1658 // We retain all of the existing options, and apply the given ones
1659 // on top. This means there is no way to "clear" options such that
1660 // they go back to the engine default.
1661 options_.SetAll(options);
solenberg246b8172015-12-08 09:50:23 -08001662 if (!engine()->ApplyOptions(options_)) {
1663 LOG(LS_WARNING) <<
1664 "Failed to apply engine options during channel SetOptions.";
1665 return false;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001666 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001667 LOG(LS_INFO) << "Set voice channel options. Current options: "
1668 << options_.ToString();
1669 return true;
1670}
1671
1672bool WebRtcVoiceMediaChannel::SetRecvCodecs(
1673 const std::vector<AudioCodec>& codecs) {
solenberg566ef242015-11-06 15:34:49 -08001674 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
solenberg8fb30c32015-10-13 03:06:58 -07001675
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001676 // Set the payload types to be used for incoming media.
solenberg0b675462015-10-09 01:37:09 -07001677 LOG(LS_INFO) << "Setting receive voice codecs.";
solenberg0b675462015-10-09 01:37:09 -07001678
1679 if (!VerifyUniquePayloadTypes(codecs)) {
1680 LOG(LS_ERROR) << "Codec payload types overlap.";
1681 return false;
1682 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001683
1684 std::vector<AudioCodec> new_codecs;
1685 // Find all new codecs. We allow adding new codecs but don't allow changing
1686 // the payload type of codecs that is already configured since we might
1687 // already be receiving packets with that payload type.
Fredrik Solenbergaf9fb212015-08-26 10:45:53 +02001688 for (const AudioCodec& codec : codecs) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001689 AudioCodec old_codec;
Fredrik Solenbergaf9fb212015-08-26 10:45:53 +02001690 if (FindCodec(recv_codecs_, codec, &old_codec)) {
1691 if (old_codec.id != codec.id) {
1692 LOG(LS_ERROR) << codec.name << " payload type changed.";
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001693 return false;
1694 }
1695 } else {
Fredrik Solenbergaf9fb212015-08-26 10:45:53 +02001696 new_codecs.push_back(codec);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001697 }
1698 }
1699 if (new_codecs.empty()) {
1700 // There are no new codecs to configure. Already configured codecs are
1701 // never removed.
1702 return true;
1703 }
1704
1705 if (playout_) {
1706 // Receive codecs can not be changed while playing. So we temporarily
1707 // pause playout.
aleloi84ef6152016-08-04 05:28:21 -07001708 ChangePlayout(false);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001709 }
1710
solenberg26c8c912015-11-27 04:00:25 -08001711 bool result = true;
1712 for (const AudioCodec& codec : new_codecs) {
solenberg72e29d22016-03-08 06:35:16 -08001713 webrtc::CodecInst voe_codec = {0};
solenberg26c8c912015-11-27 04:00:25 -08001714 if (WebRtcVoiceEngine::ToCodecInst(codec, &voe_codec)) {
1715 LOG(LS_INFO) << ToString(codec);
1716 voe_codec.pltype = codec.id;
1717 for (const auto& ch : recv_streams_) {
1718 if (engine()->voe()->codec()->SetRecPayloadType(
1719 ch.second->channel(), voe_codec) == -1) {
1720 LOG_RTCERR2(SetRecPayloadType, ch.second->channel(),
1721 ToString(voe_codec));
1722 result = false;
1723 }
1724 }
1725 } else {
1726 LOG(LS_WARNING) << "Unknown codec " << ToString(codec);
1727 result = false;
1728 break;
1729 }
1730 }
Fredrik Solenbergaf9fb212015-08-26 10:45:53 +02001731 if (result) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001732 recv_codecs_ = codecs;
1733 }
1734
1735 if (desired_playout_ && !playout_) {
aleloi84ef6152016-08-04 05:28:21 -07001736 ChangePlayout(desired_playout_);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001737 }
Fredrik Solenbergaf9fb212015-08-26 10:45:53 +02001738 return result;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001739}
1740
solenberg72e29d22016-03-08 06:35:16 -08001741// Utility function called from SetSendParameters() to extract current send
1742// codec settings from the given list of codecs (originally from SDP). Both send
1743// and receive streams may be reconfigured based on the new settings.
wu@webrtc.orgcadf9042013-08-30 21:24:16 +00001744bool WebRtcVoiceMediaChannel::SetSendCodecs(
1745 const std::vector<AudioCodec>& codecs) {
solenberg566ef242015-11-06 15:34:49 -08001746 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
Fredrik Solenbergb5727682015-12-04 15:22:19 +01001747 // TODO(solenberg): Validate input - that payload types don't overlap, are
1748 // within range, filter out codecs we don't support,
solenberg31642aa2016-03-14 08:00:37 -07001749 // redundant codecs etc - the same way it is done for
1750 // RtpHeaderExtensions.
solenbergd97ec302015-10-07 01:40:33 -07001751
Fredrik Solenbergb5727682015-12-04 15:22:19 +01001752 // Find the DTMF telephone event "codec" payload type.
1753 dtmf_payload_type_ = rtc::Optional<int>();
Fredrik Solenbergaf9fb212015-08-26 10:45:53 +02001754 for (const AudioCodec& codec : codecs) {
Fredrik Solenbergaf9fb212015-08-26 10:45:53 +02001755 if (IsCodec(codec, kDtmfCodecName)) {
solenberg31642aa2016-03-14 08:00:37 -07001756 if (codec.id < kMinPayloadType || codec.id > kMaxPayloadType) {
1757 return false;
1758 }
Fredrik Solenbergb5727682015-12-04 15:22:19 +01001759 dtmf_payload_type_ = rtc::Optional<int>(codec.id);
1760 break;
wu@webrtc.orgcadf9042013-08-30 21:24:16 +00001761 }
1762 }
1763
solenberg72e29d22016-03-08 06:35:16 -08001764 // Scan through the list to figure out the codec to use for sending, along
kwiberg68061362016-06-14 08:04:47 -07001765 // with the proper configuration for VAD, CNG, NACK and Opus-specific
solenberg72e29d22016-03-08 06:35:16 -08001766 // parameters.
kwiberg68061362016-06-14 08:04:47 -07001767 // TODO(solenberg): Refactor this logic once we create AudioEncoders here.
solenberg971cab02016-06-14 10:02:41 -07001768 SendCodecSpec send_codec_spec;
solenberg72e29d22016-03-08 06:35:16 -08001769 {
solenberg72e29d22016-03-08 06:35:16 -08001770 send_codec_spec.nack_enabled = send_codec_spec_.nack_enabled;
1771
1772 // Find send codec (the first non-telephone-event/CN codec).
1773 const AudioCodec* codec = WebRtcVoiceCodecs::GetPreferredCodec(
kwiberg68061362016-06-14 08:04:47 -07001774 codecs, &send_codec_spec.codec_inst);
solenberg72e29d22016-03-08 06:35:16 -08001775 if (!codec) {
1776 LOG(LS_WARNING) << "Received empty list of codecs.";
1777 return false;
1778 }
1779
1780 send_codec_spec.transport_cc_enabled = HasTransportCc(*codec);
kwiberg68061362016-06-14 08:04:47 -07001781 send_codec_spec.nack_enabled = HasNack(*codec);
solenberg72e29d22016-03-08 06:35:16 -08001782
kwiberg68061362016-06-14 08:04:47 -07001783 // For Opus as the send codec, we are to determine inband FEC, maximum
1784 // playback rate, and opus internal dtx.
1785 if (IsCodec(*codec, kOpusCodecName)) {
1786 GetOpusConfig(*codec, &send_codec_spec.codec_inst,
1787 &send_codec_spec.enable_codec_fec,
1788 &send_codec_spec.opus_max_playback_rate,
1789 &send_codec_spec.enable_opus_dtx);
1790 }
solenberg72e29d22016-03-08 06:35:16 -08001791
kwiberg68061362016-06-14 08:04:47 -07001792 // Set packet size if the AudioCodec param kCodecParamPTime is set.
1793 int ptime_ms = 0;
1794 if (codec->GetParam(kCodecParamPTime, &ptime_ms)) {
1795 if (!WebRtcVoiceCodecs::SetPTimeAsPacketSize(
1796 &send_codec_spec.codec_inst, ptime_ms)) {
1797 LOG(LS_WARNING) << "Failed to set packet size for codec "
1798 << send_codec_spec.codec_inst.plname;
1799 return false;
solenberg72e29d22016-03-08 06:35:16 -08001800 }
1801 }
1802
1803 // Loop through the codecs list again to find the CN codec.
1804 // TODO(solenberg): Break out into a separate function?
1805 for (const AudioCodec& codec : codecs) {
1806 // Ignore codecs we don't know about. The negotiation step should prevent
1807 // this, but double-check to be sure.
1808 webrtc::CodecInst voe_codec = {0};
1809 if (!WebRtcVoiceEngine::ToCodecInst(codec, &voe_codec)) {
1810 LOG(LS_WARNING) << "Unknown codec " << ToString(codec);
1811 continue;
1812 }
1813
1814 if (IsCodec(codec, kCnCodecName)) {
1815 // Turn voice activity detection/comfort noise on if supported.
1816 // Set the wideband CN payload type appropriately.
1817 // (narrowband always uses the static payload type 13).
1818 int cng_plfreq = -1;
1819 switch (codec.clockrate) {
1820 case 8000:
1821 case 16000:
1822 case 32000:
1823 cng_plfreq = codec.clockrate;
1824 break;
1825 default:
1826 LOG(LS_WARNING) << "CN frequency " << codec.clockrate
1827 << " not supported.";
1828 continue;
1829 }
1830 send_codec_spec.cng_payload_type = codec.id;
1831 send_codec_spec.cng_plfreq = cng_plfreq;
1832 break;
1833 }
1834 }
solenberg72e29d22016-03-08 06:35:16 -08001835 }
1836
solenberg971cab02016-06-14 10:02:41 -07001837 // Apply new settings to all streams.
1838 if (send_codec_spec_ != send_codec_spec) {
1839 send_codec_spec_ = std::move(send_codec_spec);
1840 for (const auto& kv : send_streams_) {
1841 kv.second->RecreateAudioSendStream(send_codec_spec_);
1842 if (!SetSendCodecs(kv.second->channel(), kv.second->rtp_parameters())) {
1843 return false;
1844 }
wu@webrtc.orgcadf9042013-08-30 21:24:16 +00001845 }
1846 }
1847
solenberg8189b022016-06-14 12:13:00 -07001848 // Check if the transport cc feedback or NACK status has changed on the
1849 // preferred send codec, and in that case reconfigure all receive streams.
1850 if (recv_transport_cc_enabled_ != send_codec_spec_.transport_cc_enabled ||
1851 recv_nack_enabled_ != send_codec_spec_.nack_enabled) {
solenberg72e29d22016-03-08 06:35:16 -08001852 LOG(LS_INFO) << "Recreate all the receive streams because the send "
1853 "codec has changed.";
1854 recv_transport_cc_enabled_ = send_codec_spec_.transport_cc_enabled;
solenberg8189b022016-06-14 12:13:00 -07001855 recv_nack_enabled_ = send_codec_spec_.nack_enabled;
solenberg72e29d22016-03-08 06:35:16 -08001856 for (auto& kv : recv_streams_) {
solenberg8189b022016-06-14 12:13:00 -07001857 kv.second->RecreateAudioReceiveStream(recv_transport_cc_enabled_,
1858 recv_nack_enabled_);
solenberg72e29d22016-03-08 06:35:16 -08001859 }
1860 }
1861
Taylor Brandstetter0cd086b2016-04-20 16:23:10 -07001862 send_codecs_ = codecs;
solenberg72e29d22016-03-08 06:35:16 -08001863 return true;
1864}
1865
1866// Apply current codec settings to a single voe::Channel used for sending.
skvlade0d46372016-04-07 22:59:22 -07001867bool WebRtcVoiceMediaChannel::SetSendCodecs(
1868 int channel,
1869 const webrtc::RtpParameters& rtp_parameters) {
solenberg971cab02016-06-14 10:02:41 -07001870 // Disable VAD and FEC unless we know the other side wants them.
solenberg72e29d22016-03-08 06:35:16 -08001871 engine()->voe()->codec()->SetVADStatus(channel, false);
solenberg72e29d22016-03-08 06:35:16 -08001872 engine()->voe()->codec()->SetFECStatus(channel, false);
1873
solenberg72e29d22016-03-08 06:35:16 -08001874 // Set the codec immediately, since SetVADStatus() depends on whether
1875 // the current codec is mono or stereo.
1876 if (!SetSendCodec(channel, send_codec_spec_.codec_inst)) {
1877 return false;
1878 }
1879
1880 // FEC should be enabled after SetSendCodec.
1881 if (send_codec_spec_.enable_codec_fec) {
1882 LOG(LS_INFO) << "Attempt to enable codec internal FEC on channel "
1883 << channel;
1884 if (engine()->voe()->codec()->SetFECStatus(channel, true) == -1) {
1885 // Enable codec internal FEC. Treat any failure as fatal internal error.
1886 LOG_RTCERR2(SetFECStatus, channel, true);
1887 return false;
1888 }
1889 }
1890
1891 if (IsCodec(send_codec_spec_.codec_inst, kOpusCodecName)) {
1892 // DTX and maxplaybackrate should be set after SetSendCodec. Because current
1893 // send codec has to be Opus.
1894
1895 // Set Opus internal DTX.
1896 LOG(LS_INFO) << "Attempt to "
1897 << (send_codec_spec_.enable_opus_dtx ? "enable" : "disable")
1898 << " Opus DTX on channel "
1899 << channel;
1900 if (engine()->voe()->codec()->SetOpusDtx(channel,
1901 send_codec_spec_.enable_opus_dtx)) {
1902 LOG_RTCERR2(SetOpusDtx, channel, send_codec_spec_.enable_opus_dtx);
1903 return false;
1904 }
1905
1906 // If opus_max_playback_rate <= 0, the default maximum playback rate
1907 // (48 kHz) will be used.
1908 if (send_codec_spec_.opus_max_playback_rate > 0) {
1909 LOG(LS_INFO) << "Attempt to set maximum playback rate to "
1910 << send_codec_spec_.opus_max_playback_rate
1911 << " Hz on channel "
1912 << channel;
1913 if (engine()->voe()->codec()->SetOpusMaxPlaybackRate(
1914 channel, send_codec_spec_.opus_max_playback_rate) == -1) {
1915 LOG_RTCERR2(SetOpusMaxPlaybackRate, channel,
1916 send_codec_spec_.opus_max_playback_rate);
1917 return false;
stefanba4c0e42016-02-04 04:12:24 -08001918 }
1919 }
1920 }
deadbeef80346142016-04-27 14:17:10 -07001921 // TODO(solenberg): SetMaxSendBitrate() yields another call to SetSendCodec().
skvlade0d46372016-04-07 22:59:22 -07001922 // Check if it is possible to fuse with the previous call in this function.
Taylor Brandstetterdb0cd9e2016-05-16 11:40:30 -07001923 SetChannelSendParameters(channel, rtp_parameters);
solenberg72e29d22016-03-08 06:35:16 -08001924
1925 // Set the CN payloadtype and the VAD status.
1926 if (send_codec_spec_.cng_payload_type != -1) {
1927 // The CN payload type for 8000 Hz clockrate is fixed at 13.
1928 if (send_codec_spec_.cng_plfreq != 8000) {
1929 webrtc::PayloadFrequencies cn_freq;
1930 switch (send_codec_spec_.cng_plfreq) {
1931 case 16000:
1932 cn_freq = webrtc::kFreq16000Hz;
1933 break;
1934 case 32000:
1935 cn_freq = webrtc::kFreq32000Hz;
1936 break;
1937 default:
1938 RTC_NOTREACHED();
1939 return false;
1940 }
1941 if (engine()->voe()->codec()->SetSendCNPayloadType(
1942 channel, send_codec_spec_.cng_payload_type, cn_freq) == -1) {
1943 LOG_RTCERR3(SetSendCNPayloadType, channel,
1944 send_codec_spec_.cng_payload_type, cn_freq);
1945 // TODO(ajm): This failure condition will be removed from VoE.
1946 // Restore the return here when we update to a new enough webrtc.
1947 //
1948 // Not returning false because the SetSendCNPayloadType will fail if
1949 // the channel is already sending.
1950 // This can happen if the remote description is applied twice, for
1951 // example in the case of ROAP on top of JSEP, where both side will
1952 // send the offer.
1953 }
1954 }
1955
1956 // Only turn on VAD if we have a CN payload type that matches the
1957 // clockrate for the codec we are going to use.
1958 if (send_codec_spec_.cng_plfreq == send_codec_spec_.codec_inst.plfreq &&
1959 send_codec_spec_.codec_inst.channels == 1) {
1960 // TODO(minyue): If CN frequency == 48000 Hz is allowed, consider the
1961 // interaction between VAD and Opus FEC.
1962 LOG(LS_INFO) << "Enabling VAD";
1963 if (engine()->voe()->codec()->SetVADStatus(channel, true) == -1) {
1964 LOG_RTCERR2(SetVADStatus, channel, true);
1965 return false;
1966 }
1967 }
1968 }
solenberg0a617e22015-10-20 15:49:38 -07001969 return true;
wu@webrtc.org9dba5252013-08-05 20:36:57 +00001970}
1971
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001972bool WebRtcVoiceMediaChannel::SetSendCodec(
wu@webrtc.org9dba5252013-08-05 20:36:57 +00001973 int channel, const webrtc::CodecInst& send_codec) {
1974 LOG(LS_INFO) << "Send channel " << channel << " selected voice codec "
1975 << ToString(send_codec) << ", bitrate=" << send_codec.rate;
1976
solenberg72e29d22016-03-08 06:35:16 -08001977 webrtc::CodecInst current_codec = {0};
wu@webrtc.org05e7b442014-04-01 17:44:24 +00001978 if (engine()->voe()->codec()->GetSendCodec(channel, current_codec) == 0 &&
1979 (send_codec == current_codec)) {
1980 // Codec is already configured, we can return without setting it again.
1981 return true;
1982 }
1983
wu@webrtc.org9dba5252013-08-05 20:36:57 +00001984 if (engine()->voe()->codec()->SetSendCodec(channel, send_codec) == -1) {
1985 LOG_RTCERR2(SetSendCodec, channel, ToString(send_codec));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001986 return false;
1987 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001988 return true;
1989}
1990
aleloi84ef6152016-08-04 05:28:21 -07001991void WebRtcVoiceMediaChannel::SetPlayout(bool playout) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001992 desired_playout_ = playout;
1993 return ChangePlayout(desired_playout_);
1994}
1995
aleloi84ef6152016-08-04 05:28:21 -07001996void WebRtcVoiceMediaChannel::ChangePlayout(bool playout) {
Peter Boströmca8b4042016-03-08 14:24:13 -08001997 TRACE_EVENT0("webrtc", "WebRtcVoiceMediaChannel::ChangePlayout");
solenberg566ef242015-11-06 15:34:49 -08001998 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001999 if (playout_ == playout) {
aleloi84ef6152016-08-04 05:28:21 -07002000 return;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002001 }
2002
aleloi84ef6152016-08-04 05:28:21 -07002003 for (const auto& kv : recv_streams_) {
2004 kv.second->SetPlayout(playout);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002005 }
solenberg1ac56142015-10-13 03:58:19 -07002006 playout_ = playout;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002007}
2008
Taylor Brandstetter1a018dc2016-03-08 12:37:39 -08002009void WebRtcVoiceMediaChannel::SetSend(bool send) {
Peter Boströmca8b4042016-03-08 14:24:13 -08002010 TRACE_EVENT0("webrtc", "WebRtcVoiceMediaChannel::SetSend");
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002011 if (send_ == send) {
Taylor Brandstetter1a018dc2016-03-08 12:37:39 -08002012 return;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002013 }
2014
solenbergd53a3f92016-04-14 13:56:37 -07002015 // Apply channel specific options, and initialize the ADM for recording (this
2016 // may take time on some platforms, e.g. Android).
Taylor Brandstetter1a018dc2016-03-08 12:37:39 -08002017 if (send) {
solenberg63b34542015-09-29 06:06:31 -07002018 engine()->ApplyOptions(options_);
solenbergd53a3f92016-04-14 13:56:37 -07002019
2020 // InitRecording() may return an error if the ADM is already recording.
2021 if (!engine()->adm()->RecordingIsInitialized() &&
2022 !engine()->adm()->Recording()) {
2023 if (engine()->adm()->InitRecording() != 0) {
2024 LOG(LS_WARNING) << "Failed to initialize recording";
2025 }
2026 }
solenberg63b34542015-09-29 06:06:31 -07002027 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002028
wu@webrtc.org9dba5252013-08-05 20:36:57 +00002029 // Change the settings on each send channel.
Taylor Brandstetter1a018dc2016-03-08 12:37:39 -08002030 for (auto& kv : send_streams_) {
2031 kv.second->SetSend(send);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002032 }
wu@webrtc.org9dba5252013-08-05 20:36:57 +00002033
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002034 send_ = send;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002035}
2036
Peter Boström0c4e06b2015-10-07 12:23:21 +02002037bool WebRtcVoiceMediaChannel::SetAudioSend(uint32_t ssrc,
2038 bool enable,
solenberg1dd98f32015-09-10 01:57:14 -07002039 const AudioOptions* options,
Taylor Brandstetter1a018dc2016-03-08 12:37:39 -08002040 AudioSource* source) {
solenberg566ef242015-11-06 15:34:49 -08002041 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
solenberg1dd98f32015-09-10 01:57:14 -07002042 // TODO(solenberg): The state change should be fully rolled back if any one of
2043 // these calls fail.
Taylor Brandstetter1a018dc2016-03-08 12:37:39 -08002044 if (!SetLocalSource(ssrc, source)) {
solenberg1dd98f32015-09-10 01:57:14 -07002045 return false;
2046 }
solenbergdfc8f4f2015-10-01 02:31:10 -07002047 if (!MuteStream(ssrc, !enable)) {
solenberg1dd98f32015-09-10 01:57:14 -07002048 return false;
2049 }
solenbergdfc8f4f2015-10-01 02:31:10 -07002050 if (enable && options) {
solenberg1dd98f32015-09-10 01:57:14 -07002051 return SetOptions(*options);
2052 }
2053 return true;
2054}
2055
solenberg0a617e22015-10-20 15:49:38 -07002056int WebRtcVoiceMediaChannel::CreateVoEChannel() {
2057 int id = engine()->CreateVoEChannel();
2058 if (id == -1) {
2059 LOG_RTCERR0(CreateVoEChannel);
2060 return -1;
wu@webrtc.org9dba5252013-08-05 20:36:57 +00002061 }
mflodman3d7db262016-04-29 00:57:13 -07002062
solenberg0a617e22015-10-20 15:49:38 -07002063 return id;
wu@webrtc.org9dba5252013-08-05 20:36:57 +00002064}
2065
solenberg7add0582015-11-20 09:59:34 -08002066bool WebRtcVoiceMediaChannel::DeleteVoEChannel(int channel) {
wu@webrtc.org9dba5252013-08-05 20:36:57 +00002067 if (engine()->voe()->base()->DeleteChannel(channel) == -1) {
2068 LOG_RTCERR1(DeleteChannel, channel);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002069 return false;
2070 }
wu@webrtc.org9dba5252013-08-05 20:36:57 +00002071 return true;
2072}
henrike@webrtc.org1e09a712013-07-26 19:17:59 +00002073
wu@webrtc.org9dba5252013-08-05 20:36:57 +00002074bool WebRtcVoiceMediaChannel::AddSendStream(const StreamParams& sp) {
Peter Boströmca8b4042016-03-08 14:24:13 -08002075 TRACE_EVENT0("webrtc", "WebRtcVoiceMediaChannel::AddSendStream");
solenberg566ef242015-11-06 15:34:49 -08002076 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
solenberg0a617e22015-10-20 15:49:38 -07002077 LOG(LS_INFO) << "AddSendStream: " << sp.ToString();
2078
2079 uint32_t ssrc = sp.first_ssrc();
2080 RTC_DCHECK(0 != ssrc);
2081
2082 if (GetSendChannelId(ssrc) != -1) {
2083 LOG(LS_ERROR) << "Stream already exists with ssrc " << ssrc;
wu@webrtc.org9dba5252013-08-05 20:36:57 +00002084 return false;
2085 }
2086
solenberg0a617e22015-10-20 15:49:38 -07002087 // Create a new channel for sending audio data.
2088 int channel = CreateVoEChannel();
2089 if (channel == -1) {
2090 return false;
wu@webrtc.org9dba5252013-08-05 20:36:57 +00002091 }
wu@webrtc.org9dba5252013-08-05 20:36:57 +00002092
solenbergc96df772015-10-21 13:01:53 -07002093 // Save the channel to send_streams_, so that RemoveSendStream() can still
wu@webrtc.org9dba5252013-08-05 20:36:57 +00002094 // delete the channel in case failure happens below.
mallinath@webrtc.org67ee6b92014-02-03 16:57:16 +00002095 webrtc::AudioTransport* audio_transport =
2096 engine()->voe()->base()->audio_transport();
mflodman3d7db262016-04-29 00:57:13 -07002097
skvlade0d46372016-04-07 22:59:22 -07002098 WebRtcAudioSendStream* stream = new WebRtcAudioSendStream(
solenberg971cab02016-06-14 10:02:41 -07002099 channel, audio_transport, ssrc, sp.cname, send_codec_spec_,
2100 send_rtp_extensions_, call_, this);
skvlade0d46372016-04-07 22:59:22 -07002101 send_streams_.insert(std::make_pair(ssrc, stream));
wu@webrtc.org9dba5252013-08-05 20:36:57 +00002102
solenberg0a617e22015-10-20 15:49:38 -07002103 // Set the current codecs to be used for the new channel. We need to do this
2104 // after adding the channel to send_channels_, because of how max bitrate is
2105 // currently being configured by SetSendCodec().
skvlade0d46372016-04-07 22:59:22 -07002106 if (HasSendCodec() && !SetSendCodecs(channel, stream->rtp_parameters())) {
solenberg0a617e22015-10-20 15:49:38 -07002107 RemoveSendStream(ssrc);
wu@webrtc.org9dba5252013-08-05 20:36:57 +00002108 return false;
2109 }
2110
solenberg4a0f7b52016-06-16 13:07:33 -07002111 // At this point the stream's local SSRC has been updated. If it is the first
2112 // send stream, make sure that all the receive streams are updated with the
2113 // same SSRC in order to send receiver reports.
solenbergc96df772015-10-21 13:01:53 -07002114 if (send_streams_.size() == 1) {
solenberg0a617e22015-10-20 15:49:38 -07002115 receiver_reports_ssrc_ = ssrc;
solenberg4a0f7b52016-06-16 13:07:33 -07002116 for (const auto& kv : recv_streams_) {
2117 // TODO(solenberg): Allow applications to set the RTCP SSRC of receive
2118 // streams instead, so we can avoid recreating the streams here.
2119 kv.second->RecreateAudioReceiveStream(ssrc);
2120 int recv_channel = kv.second->channel();
solenberg0a617e22015-10-20 15:49:38 -07002121 engine()->voe()->base()->AssociateSendChannel(recv_channel, channel);
2122 LOG(LS_INFO) << "VoiceEngine channel #" << recv_channel
2123 << " is associated with channel #" << channel << ".";
wu@webrtc.org9dba5252013-08-05 20:36:57 +00002124 }
2125 }
2126
Taylor Brandstetter1a018dc2016-03-08 12:37:39 -08002127 send_streams_[ssrc]->SetSend(send_);
2128 return true;
wu@webrtc.org9dba5252013-08-05 20:36:57 +00002129}
2130
Peter Boström0c4e06b2015-10-07 12:23:21 +02002131bool WebRtcVoiceMediaChannel::RemoveSendStream(uint32_t ssrc) {
Peter Boströmca8b4042016-03-08 14:24:13 -08002132 TRACE_EVENT0("webrtc", "WebRtcVoiceMediaChannel::RemoveSendStream");
solenberg566ef242015-11-06 15:34:49 -08002133 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
solenberg3a941542015-11-16 07:34:50 -08002134 LOG(LS_INFO) << "RemoveSendStream: " << ssrc;
2135
solenbergc96df772015-10-21 13:01:53 -07002136 auto it = send_streams_.find(ssrc);
2137 if (it == send_streams_.end()) {
wu@webrtc.org9dba5252013-08-05 20:36:57 +00002138 LOG(LS_WARNING) << "Try to remove stream with ssrc " << ssrc
2139 << " which doesn't exist.";
2140 return false;
2141 }
2142
Taylor Brandstetter1a018dc2016-03-08 12:37:39 -08002143 it->second->SetSend(false);
wu@webrtc.org9dba5252013-08-05 20:36:57 +00002144
solenberg7add0582015-11-20 09:59:34 -08002145 // Clean up and delete the send stream+channel.
Taylor Brandstetter1a018dc2016-03-08 12:37:39 -08002146 int channel = it->second->channel();
solenberg0a617e22015-10-20 15:49:38 -07002147 LOG(LS_INFO) << "Removing audio send stream " << ssrc
2148 << " with VoiceEngine channel #" << channel << ".";
solenberg7add0582015-11-20 09:59:34 -08002149 delete it->second;
2150 send_streams_.erase(it);
2151 if (!DeleteVoEChannel(channel)) {
solenberg0a617e22015-10-20 15:49:38 -07002152 return false;
wu@webrtc.org9dba5252013-08-05 20:36:57 +00002153 }
solenbergc96df772015-10-21 13:01:53 -07002154 if (send_streams_.empty()) {
Taylor Brandstetter1a018dc2016-03-08 12:37:39 -08002155 SetSend(false);
solenberg0a617e22015-10-20 15:49:38 -07002156 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002157 return true;
2158}
2159
2160bool WebRtcVoiceMediaChannel::AddRecvStream(const StreamParams& sp) {
Peter Boströmca8b4042016-03-08 14:24:13 -08002161 TRACE_EVENT0("webrtc", "WebRtcVoiceMediaChannel::AddRecvStream");
solenberg566ef242015-11-06 15:34:49 -08002162 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
solenbergd97ec302015-10-07 01:40:33 -07002163 LOG(LS_INFO) << "AddRecvStream: " << sp.ToString();
2164
solenberg0b675462015-10-09 01:37:09 -07002165 if (!ValidateStreamParams(sp)) {
wu@webrtc.org78187522013-10-07 23:32:02 +00002166 return false;
2167 }
2168
solenberg7add0582015-11-20 09:59:34 -08002169 const uint32_t ssrc = sp.first_ssrc();
solenberg0b675462015-10-09 01:37:09 -07002170 if (ssrc == 0) {
2171 LOG(LS_WARNING) << "AddRecvStream with ssrc==0 is not supported.";
2172 return false;
2173 }
2174
solenberg1ac56142015-10-13 03:58:19 -07002175 // Remove the default receive stream if one had been created with this ssrc;
2176 // we'll recreate it then.
2177 if (IsDefaultRecvStream(ssrc)) {
2178 RemoveRecvStream(ssrc);
2179 }
solenberg0b675462015-10-09 01:37:09 -07002180
solenberg7add0582015-11-20 09:59:34 -08002181 if (GetReceiveChannelId(ssrc) != -1) {
henrike@webrtc.org1e09a712013-07-26 19:17:59 +00002182 LOG(LS_ERROR) << "Stream already exists with ssrc " << ssrc;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002183 return false;
2184 }
Fredrik Solenberg4b60c732015-05-07 14:07:48 +02002185
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002186 // Create a new channel for receiving audio data.
solenberg7add0582015-11-20 09:59:34 -08002187 const int channel = CreateVoEChannel();
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002188 if (channel == -1) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002189 return false;
2190 }
Minyue2013aec2015-05-13 14:14:42 +02002191
solenberg1ac56142015-10-13 03:58:19 -07002192 // Turn off all supported codecs.
solenberg26c8c912015-11-27 04:00:25 -08002193 // TODO(solenberg): Remove once "no codecs" is the default state of a stream.
2194 for (webrtc::CodecInst voe_codec : webrtc::acm2::RentACodec::Database()) {
2195 voe_codec.pltype = -1;
2196 if (engine()->voe()->codec()->SetRecPayloadType(channel, voe_codec) == -1) {
2197 LOG_RTCERR2(SetRecPayloadType, channel, ToString(voe_codec));
2198 DeleteVoEChannel(channel);
2199 return false;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002200 }
2201 }
2202
solenberg1ac56142015-10-13 03:58:19 -07002203 // Only enable those configured for this channel.
2204 for (const auto& codec : recv_codecs_) {
solenberg72e29d22016-03-08 06:35:16 -08002205 webrtc::CodecInst voe_codec = {0};
solenberg26c8c912015-11-27 04:00:25 -08002206 if (WebRtcVoiceEngine::ToCodecInst(codec, &voe_codec)) {
solenberg1ac56142015-10-13 03:58:19 -07002207 voe_codec.pltype = codec.id;
2208 if (engine()->voe()->codec()->SetRecPayloadType(
2209 channel, voe_codec) == -1) {
2210 LOG_RTCERR2(SetRecPayloadType, channel, ToString(voe_codec));
solenberg7add0582015-11-20 09:59:34 -08002211 DeleteVoEChannel(channel);
solenberg1ac56142015-10-13 03:58:19 -07002212 return false;
2213 }
henrike@webrtc.org1e09a712013-07-26 19:17:59 +00002214 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002215 }
solenberg8fb30c32015-10-13 03:06:58 -07002216
solenberg7add0582015-11-20 09:59:34 -08002217 const int send_channel = GetSendChannelId(receiver_reports_ssrc_);
2218 if (send_channel != -1) {
2219 // Associate receive channel with first send channel (so the receive channel
2220 // can obtain RTT from the send channel)
2221 engine()->voe()->base()->AssociateSendChannel(channel, send_channel);
2222 LOG(LS_INFO) << "VoiceEngine channel #" << channel
2223 << " is associated with channel #" << send_channel << ".";
buildbot@webrtc.org150835e2014-05-06 15:54:38 +00002224 }
2225
stefanba4c0e42016-02-04 04:12:24 -08002226 recv_streams_.insert(std::make_pair(
2227 ssrc, new WebRtcAudioReceiveStream(channel, ssrc, receiver_reports_ssrc_,
solenberg72e29d22016-03-08 06:35:16 -08002228 recv_transport_cc_enabled_,
solenberg8189b022016-06-14 12:13:00 -07002229 recv_nack_enabled_,
solenberg72e29d22016-03-08 06:35:16 -08002230 sp.sync_label, recv_rtp_extensions_,
ossu29b1a8d2016-06-13 07:34:51 -07002231 call_, this,
2232 engine()->decoder_factory_)));
aleloi84ef6152016-08-04 05:28:21 -07002233 recv_streams_[ssrc]->SetPlayout(playout_);
solenberg7add0582015-11-20 09:59:34 -08002234
solenberg1ac56142015-10-13 03:58:19 -07002235 return true;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002236}
2237
Peter Boström0c4e06b2015-10-07 12:23:21 +02002238bool WebRtcVoiceMediaChannel::RemoveRecvStream(uint32_t ssrc) {
Peter Boströmca8b4042016-03-08 14:24:13 -08002239 TRACE_EVENT0("webrtc", "WebRtcVoiceMediaChannel::RemoveRecvStream");
solenberg566ef242015-11-06 15:34:49 -08002240 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
solenbergd97ec302015-10-07 01:40:33 -07002241 LOG(LS_INFO) << "RemoveRecvStream: " << ssrc;
2242
solenberg7add0582015-11-20 09:59:34 -08002243 const auto it = recv_streams_.find(ssrc);
2244 if (it == recv_streams_.end()) {
wu@webrtc.org9dba5252013-08-05 20:36:57 +00002245 LOG(LS_WARNING) << "Try to remove stream with ssrc " << ssrc
2246 << " which doesn't exist.";
henrike@webrtc.org1e09a712013-07-26 19:17:59 +00002247 return false;
wu@webrtc.org9dba5252013-08-05 20:36:57 +00002248 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002249
solenberg1ac56142015-10-13 03:58:19 -07002250 // Deregister default channel, if that's the one being destroyed.
2251 if (IsDefaultRecvStream(ssrc)) {
2252 default_recv_ssrc_ = -1;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002253 }
henrike@webrtc.org1e09a712013-07-26 19:17:59 +00002254
solenberg7add0582015-11-20 09:59:34 -08002255 const int channel = it->second->channel();
2256
2257 // Clean up and delete the receive stream+channel.
2258 LOG(LS_INFO) << "Removing audio receive stream " << ssrc
mallinath@webrtc.org67ee6b92014-02-03 16:57:16 +00002259 << " with VoiceEngine channel #" << channel << ".";
Tommif888bb52015-12-12 01:37:01 +01002260 it->second->SetRawAudioSink(nullptr);
solenberg7add0582015-11-20 09:59:34 -08002261 delete it->second;
2262 recv_streams_.erase(it);
2263 return DeleteVoEChannel(channel);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002264}
2265
Taylor Brandstetter1a018dc2016-03-08 12:37:39 -08002266bool WebRtcVoiceMediaChannel::SetLocalSource(uint32_t ssrc,
2267 AudioSource* source) {
solenbergc96df772015-10-21 13:01:53 -07002268 auto it = send_streams_.find(ssrc);
2269 if (it == send_streams_.end()) {
Taylor Brandstetter1a018dc2016-03-08 12:37:39 -08002270 if (source) {
2271 // Return an error if trying to set a valid source with an invalid ssrc.
2272 LOG(LS_ERROR) << "SetLocalSource failed with ssrc " << ssrc;
wu@webrtc.org9dba5252013-08-05 20:36:57 +00002273 return false;
2274 }
2275
2276 // The channel likely has gone away, do nothing.
henrike@webrtc.org1e09a712013-07-26 19:17:59 +00002277 return true;
henrike@webrtc.org1e09a712013-07-26 19:17:59 +00002278 }
2279
Taylor Brandstetter1a018dc2016-03-08 12:37:39 -08002280 if (source) {
2281 it->second->SetSource(source);
solenberg1ac56142015-10-13 03:58:19 -07002282 } else {
Taylor Brandstetter1a018dc2016-03-08 12:37:39 -08002283 it->second->ClearSource();
solenberg1ac56142015-10-13 03:58:19 -07002284 }
henrike@webrtc.org1e09a712013-07-26 19:17:59 +00002285
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002286 return true;
2287}
2288
2289bool WebRtcVoiceMediaChannel::GetActiveStreams(
2290 AudioInfo::StreamList* actives) {
solenberg566ef242015-11-06 15:34:49 -08002291 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002292 actives->clear();
solenberg7add0582015-11-20 09:59:34 -08002293 for (const auto& ch : recv_streams_) {
Fredrik Solenbergaf9fb212015-08-26 10:45:53 +02002294 int level = GetOutputLevel(ch.second->channel());
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002295 if (level > 0) {
Fredrik Solenbergaf9fb212015-08-26 10:45:53 +02002296 actives->push_back(std::make_pair(ch.first, level));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002297 }
2298 }
2299 return true;
2300}
2301
2302int WebRtcVoiceMediaChannel::GetOutputLevel() {
solenberg566ef242015-11-06 15:34:49 -08002303 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
solenberg1ac56142015-10-13 03:58:19 -07002304 int highest = 0;
solenberg7add0582015-11-20 09:59:34 -08002305 for (const auto& ch : recv_streams_) {
solenberg8fb30c32015-10-13 03:06:58 -07002306 highest = std::max(GetOutputLevel(ch.second->channel()), highest);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002307 }
2308 return highest;
2309}
2310
2311int WebRtcVoiceMediaChannel::GetTimeSinceLastTyping() {
2312 int ret;
2313 if (engine()->voe()->processing()->TimeSinceLastTyping(ret) == -1) {
2314 // In case of error, log the info and continue
2315 LOG_RTCERR0(TimeSinceLastTyping);
2316 ret = -1;
2317 } else {
2318 ret *= 1000; // We return ms, webrtc returns seconds.
2319 }
2320 return ret;
2321}
2322
2323void WebRtcVoiceMediaChannel::SetTypingDetectionParameters(int time_window,
2324 int cost_per_typing, int reporting_threshold, int penalty_decay,
2325 int type_event_delay) {
2326 if (engine()->voe()->processing()->SetTypingDetectionParameters(
2327 time_window, cost_per_typing,
2328 reporting_threshold, penalty_decay, type_event_delay) == -1) {
2329 // In case of error, log the info and continue
2330 LOG_RTCERR5(SetTypingDetectionParameters, time_window,
2331 cost_per_typing, reporting_threshold, penalty_decay,
2332 type_event_delay);
2333 }
2334}
2335
solenberg4bac9c52015-10-09 02:32:53 -07002336bool WebRtcVoiceMediaChannel::SetOutputVolume(uint32_t ssrc, double volume) {
solenberg566ef242015-11-06 15:34:49 -08002337 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
solenberg1ac56142015-10-13 03:58:19 -07002338 if (ssrc == 0) {
2339 default_recv_volume_ = volume;
2340 if (default_recv_ssrc_ == -1) {
2341 return true;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002342 }
solenberg1ac56142015-10-13 03:58:19 -07002343 ssrc = static_cast<uint32_t>(default_recv_ssrc_);
2344 }
solenberg217fb662016-06-17 08:30:54 -07002345 const auto it = recv_streams_.find(ssrc);
2346 if (it == recv_streams_.end()) {
2347 LOG(LS_WARNING) << "SetOutputVolume: no recv stream" << ssrc;
solenberg1ac56142015-10-13 03:58:19 -07002348 return false;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002349 }
solenberg217fb662016-06-17 08:30:54 -07002350 it->second->SetOutputVolume(volume);
2351 LOG(LS_INFO) << "SetOutputVolume() to " << volume
2352 << " for recv stream with ssrc " << ssrc;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002353 return true;
2354}
2355
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002356bool WebRtcVoiceMediaChannel::CanInsertDtmf() {
Fredrik Solenbergb5727682015-12-04 15:22:19 +01002357 return dtmf_payload_type_ ? true : false;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002358}
2359
solenberg1d63dd02015-12-02 12:35:09 -08002360bool WebRtcVoiceMediaChannel::InsertDtmf(uint32_t ssrc, int event,
2361 int duration) {
solenberg566ef242015-11-06 15:34:49 -08002362 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
Fredrik Solenbergb5727682015-12-04 15:22:19 +01002363 LOG(LS_INFO) << "WebRtcVoiceMediaChannel::InsertDtmf";
2364 if (!dtmf_payload_type_) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002365 return false;
2366 }
2367
Fredrik Solenbergb5727682015-12-04 15:22:19 +01002368 // Figure out which WebRtcAudioSendStream to send the event on.
2369 auto it = ssrc != 0 ? send_streams_.find(ssrc) : send_streams_.begin();
2370 if (it == send_streams_.end()) {
2371 LOG(LS_WARNING) << "The specified ssrc " << ssrc << " is not in use.";
solenberg1d63dd02015-12-02 12:35:09 -08002372 return false;
2373 }
Fredrik Solenbergb5727682015-12-04 15:22:19 +01002374 if (event < kMinTelephoneEventCode ||
2375 event > kMaxTelephoneEventCode) {
2376 LOG(LS_WARNING) << "DTMF event code " << event << " out of range.";
solenberg1d63dd02015-12-02 12:35:09 -08002377 return false;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002378 }
Fredrik Solenbergb5727682015-12-04 15:22:19 +01002379 if (duration < kMinTelephoneEventDuration ||
2380 duration > kMaxTelephoneEventDuration) {
2381 LOG(LS_WARNING) << "DTMF event duration " << duration << " out of range.";
2382 return false;
2383 }
2384 return it->second->SendTelephoneEvent(*dtmf_payload_type_, event, duration);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002385}
2386
wu@webrtc.orga9890802013-12-13 00:21:03 +00002387void WebRtcVoiceMediaChannel::OnPacketReceived(
jbaucheec21bd2016-03-20 06:15:43 -07002388 rtc::CopyOnWriteBuffer* packet, const rtc::PacketTime& packet_time) {
solenberg566ef242015-11-06 15:34:49 -08002389 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
Fredrik Solenberg4b60c732015-05-07 14:07:48 +02002390
mflodman3d7db262016-04-29 00:57:13 -07002391 const webrtc::PacketTime webrtc_packet_time(packet_time.timestamp,
2392 packet_time.not_before);
2393 webrtc::PacketReceiver::DeliveryStatus delivery_result =
2394 call_->Receiver()->DeliverPacket(webrtc::MediaType::AUDIO,
2395 packet->cdata(), packet->size(),
2396 webrtc_packet_time);
mflodman3d7db262016-04-29 00:57:13 -07002397 if (delivery_result != webrtc::PacketReceiver::DELIVERY_UNKNOWN_SSRC) {
2398 return;
2399 }
2400
2401 // Create a default receive stream for this unsignalled and previously not
2402 // received ssrc. If there already is a default receive stream, delete it.
2403 // See: https://bugs.chromium.org/p/webrtc/issues/detail?id=5208
solenberg1ac56142015-10-13 03:58:19 -07002404 uint32_t ssrc = 0;
jbaucheec21bd2016-03-20 06:15:43 -07002405 if (!GetRtpSsrc(packet->cdata(), packet->size(), &ssrc)) {
solenberg1ac56142015-10-13 03:58:19 -07002406 return;
2407 }
2408
mflodman3d7db262016-04-29 00:57:13 -07002409 if (default_recv_ssrc_ != -1) {
2410 LOG(LS_INFO) << "Removing default receive stream with ssrc "
2411 << default_recv_ssrc_;
2412 RTC_DCHECK_NE(ssrc, default_recv_ssrc_);
2413 RemoveRecvStream(default_recv_ssrc_);
2414 default_recv_ssrc_ = -1;
solenberg1ac56142015-10-13 03:58:19 -07002415 }
2416
mflodman3d7db262016-04-29 00:57:13 -07002417 StreamParams sp;
2418 sp.ssrcs.push_back(ssrc);
2419 LOG(LS_INFO) << "Creating default receive stream for SSRC=" << ssrc << ".";
2420 if (!AddRecvStream(sp)) {
2421 LOG(LS_WARNING) << "Could not create default receive stream.";
2422 return;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002423 }
mflodman3d7db262016-04-29 00:57:13 -07002424 default_recv_ssrc_ = ssrc;
2425 SetOutputVolume(default_recv_ssrc_, default_recv_volume_);
2426 if (default_sink_) {
2427 std::unique_ptr<webrtc::AudioSinkInterface> proxy_sink(
2428 new ProxySink(default_sink_.get()));
2429 SetRawAudioSink(default_recv_ssrc_, std::move(proxy_sink));
2430 }
2431 delivery_result = call_->Receiver()->DeliverPacket(webrtc::MediaType::AUDIO,
2432 packet->cdata(),
2433 packet->size(),
2434 webrtc_packet_time);
2435 RTC_DCHECK_NE(webrtc::PacketReceiver::DELIVERY_UNKNOWN_SSRC, delivery_result);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002436}
2437
wu@webrtc.orga9890802013-12-13 00:21:03 +00002438void WebRtcVoiceMediaChannel::OnRtcpReceived(
jbaucheec21bd2016-03-20 06:15:43 -07002439 rtc::CopyOnWriteBuffer* packet, const rtc::PacketTime& packet_time) {
solenberg566ef242015-11-06 15:34:49 -08002440 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
Fredrik Solenberg4b60c732015-05-07 14:07:48 +02002441
Fredrik Solenberg709ed672015-09-15 12:26:33 +02002442 // Forward packet to Call as well.
2443 const webrtc::PacketTime webrtc_packet_time(packet_time.timestamp,
2444 packet_time.not_before);
2445 call_->Receiver()->DeliverPacket(webrtc::MediaType::AUDIO,
jbaucheec21bd2016-03-20 06:15:43 -07002446 packet->cdata(), packet->size(), webrtc_packet_time);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002447}
2448
Honghai Zhangcc411c02016-03-29 17:27:21 -07002449void WebRtcVoiceMediaChannel::OnNetworkRouteChanged(
2450 const std::string& transport_name,
Honghai Zhang0e533ef2016-04-19 15:41:36 -07002451 const rtc::NetworkRoute& network_route) {
2452 call_->OnNetworkRouteChanged(transport_name, network_route);
Honghai Zhangcc411c02016-03-29 17:27:21 -07002453}
2454
Peter Boström0c4e06b2015-10-07 12:23:21 +02002455bool WebRtcVoiceMediaChannel::MuteStream(uint32_t ssrc, bool muted) {
solenberg566ef242015-11-06 15:34:49 -08002456 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
solenberg94218532016-06-16 10:53:22 -07002457 const auto it = send_streams_.find(ssrc);
2458 if (it == send_streams_.end()) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002459 LOG(LS_WARNING) << "The specified ssrc " << ssrc << " is not in use.";
2460 return false;
2461 }
solenberg94218532016-06-16 10:53:22 -07002462 it->second->SetMuted(muted);
2463
2464 // TODO(solenberg):
buildbot@webrtc.org6b21b712014-07-31 15:08:53 +00002465 // We set the AGC to mute state only when all the channels are muted.
2466 // This implementation is not ideal, instead we should signal the AGC when
2467 // the mic channel is muted/unmuted. We can't do it today because there
2468 // is no good way to know which stream is mapping to the mic channel.
2469 bool all_muted = muted;
solenberg94218532016-06-16 10:53:22 -07002470 for (const auto& kv : send_streams_) {
2471 all_muted = all_muted && kv.second->muted();
buildbot@webrtc.org6b21b712014-07-31 15:08:53 +00002472 }
2473
2474 webrtc::AudioProcessing* ap = engine()->voe()->base()->audio_processing();
solenberg0a617e22015-10-20 15:49:38 -07002475 if (ap) {
buildbot@webrtc.org6b21b712014-07-31 15:08:53 +00002476 ap->set_output_will_be_muted(all_muted);
solenberg0a617e22015-10-20 15:49:38 -07002477 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002478 return true;
2479}
2480
deadbeef80346142016-04-27 14:17:10 -07002481bool WebRtcVoiceMediaChannel::SetMaxSendBitrate(int bps) {
2482 LOG(LS_INFO) << "WebRtcVoiceMediaChannel::SetMaxSendBitrate.";
2483 max_send_bitrate_bps_ = bps;
skvlade0d46372016-04-07 22:59:22 -07002484
2485 for (const auto& kv : send_streams_) {
Taylor Brandstetterdb0cd9e2016-05-16 11:40:30 -07002486 if (!SetChannelSendParameters(kv.second->channel(),
2487 kv.second->rtp_parameters())) {
skvlade0d46372016-04-07 22:59:22 -07002488 return false;
2489 }
2490 }
2491 return true;
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00002492}
2493
Taylor Brandstetterdb0cd9e2016-05-16 11:40:30 -07002494bool WebRtcVoiceMediaChannel::SetChannelSendParameters(
skvlade0d46372016-04-07 22:59:22 -07002495 int channel,
2496 const webrtc::RtpParameters& parameters) {
2497 RTC_CHECK_EQ(1UL, parameters.encodings.size());
Taylor Brandstetter0cd086b2016-04-20 16:23:10 -07002498 // TODO(deadbeef): Handle setting parameters with a list of codecs in a
2499 // different order (which should change the send codec).
deadbeef80346142016-04-27 14:17:10 -07002500 return SetMaxSendBitrate(
2501 channel, MinPositive(max_send_bitrate_bps_,
2502 parameters.encodings[0].max_bitrate_bps));
skvlade0d46372016-04-07 22:59:22 -07002503}
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00002504
deadbeef80346142016-04-27 14:17:10 -07002505bool WebRtcVoiceMediaChannel::SetMaxSendBitrate(int channel, int bps) {
skvlade0d46372016-04-07 22:59:22 -07002506 // Bitrate is auto by default.
2507 // TODO(bemasc): Fix this so that if SetMaxSendBandwidth(50) is followed by
2508 // SetMaxSendBandwith(0), the second call removes the previous limit.
deadbeef80346142016-04-27 14:17:10 -07002509 if (bps <= 0) {
skvlade0d46372016-04-07 22:59:22 -07002510 return true;
deadbeef80346142016-04-27 14:17:10 -07002511 }
wu@webrtc.org1d1ffc92013-10-16 18:12:02 +00002512
solenberg72e29d22016-03-08 06:35:16 -08002513 if (!HasSendCodec()) {
wu@webrtc.org1d1ffc92013-10-16 18:12:02 +00002514 LOG(LS_INFO) << "The send codec has not been set up yet. "
minyue@webrtc.org26236952014-10-29 02:27:08 +00002515 << "The send bitrate setting will be applied later.";
wu@webrtc.org1d1ffc92013-10-16 18:12:02 +00002516 return true;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002517 }
2518
solenberg72e29d22016-03-08 06:35:16 -08002519 webrtc::CodecInst codec = send_codec_spec_.codec_inst;
solenberg26c8c912015-11-27 04:00:25 -08002520 bool is_multi_rate = WebRtcVoiceCodecs::IsCodecMultiRate(codec);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002521
2522 if (is_multi_rate) {
2523 // If codec is multi-rate then just set the bitrate.
deadbeef80346142016-04-27 14:17:10 -07002524 int max_bitrate_bps = WebRtcVoiceCodecs::MaxBitrateBps(codec);
2525 codec.rate = std::min(bps, max_bitrate_bps);
2526 LOG(LS_INFO) << "Setting codec " << codec.plname << " to bitrate " << bps
2527 << " bps.";
skvlade0d46372016-04-07 22:59:22 -07002528 if (!SetSendCodec(channel, codec)) {
deadbeef80346142016-04-27 14:17:10 -07002529 LOG(LS_ERROR) << "Failed to set codec " << codec.plname << " to bitrate "
2530 << bps << " bps.";
skvlade0d46372016-04-07 22:59:22 -07002531 return false;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002532 }
2533 return true;
2534 } else {
2535 // If codec is not multi-rate and |bps| is less than the fixed bitrate
2536 // then fail. If codec is not multi-rate and |bps| exceeds or equal the
2537 // fixed bitrate then ignore.
2538 if (bps < codec.rate) {
deadbeef80346142016-04-27 14:17:10 -07002539 LOG(LS_ERROR) << "Failed to set codec " << codec.plname << " to bitrate "
2540 << bps << " bps"
2541 << ", requires at least " << codec.rate << " bps.";
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002542 return false;
2543 }
2544 return true;
2545 }
2546}
2547
skvlad7a43d252016-03-22 15:32:27 -07002548void WebRtcVoiceMediaChannel::OnReadyToSend(bool ready) {
2549 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
2550 LOG(LS_VERBOSE) << "OnReadyToSend: " << (ready ? "Ready." : "Not ready.");
2551 call_->SignalChannelNetworkState(
2552 webrtc::MediaType::AUDIO,
2553 ready ? webrtc::kNetworkUp : webrtc::kNetworkDown);
2554}
2555
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002556bool WebRtcVoiceMediaChannel::GetStats(VoiceMediaInfo* info) {
Peter Boströmca8b4042016-03-08 14:24:13 -08002557 TRACE_EVENT0("webrtc", "WebRtcVoiceMediaChannel::GetStats");
solenberg566ef242015-11-06 15:34:49 -08002558 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
solenberg85a04962015-10-27 03:35:21 -07002559 RTC_DCHECK(info);
solenbergd97ec302015-10-07 01:40:33 -07002560
solenberg85a04962015-10-27 03:35:21 -07002561 // Get SSRC and stats for each sender.
2562 RTC_DCHECK(info->senders.size() == 0);
2563 for (const auto& stream : send_streams_) {
2564 webrtc::AudioSendStream::Stats stats = stream.second->GetStats();
wu@webrtc.org9dba5252013-08-05 20:36:57 +00002565 VoiceSenderInfo sinfo;
solenberg85a04962015-10-27 03:35:21 -07002566 sinfo.add_ssrc(stats.local_ssrc);
2567 sinfo.bytes_sent = stats.bytes_sent;
2568 sinfo.packets_sent = stats.packets_sent;
2569 sinfo.packets_lost = stats.packets_lost;
2570 sinfo.fraction_lost = stats.fraction_lost;
2571 sinfo.codec_name = stats.codec_name;
2572 sinfo.ext_seqnum = stats.ext_seqnum;
2573 sinfo.jitter_ms = stats.jitter_ms;
2574 sinfo.rtt_ms = stats.rtt_ms;
2575 sinfo.audio_level = stats.audio_level;
2576 sinfo.aec_quality_min = stats.aec_quality_min;
2577 sinfo.echo_delay_median_ms = stats.echo_delay_median_ms;
2578 sinfo.echo_delay_std_ms = stats.echo_delay_std_ms;
2579 sinfo.echo_return_loss = stats.echo_return_loss;
2580 sinfo.echo_return_loss_enhancement = stats.echo_return_loss_enhancement;
Taylor Brandstetter1a018dc2016-03-08 12:37:39 -08002581 sinfo.typing_noise_detected = (send_ ? stats.typing_noise_detected : false);
wu@webrtc.org9dba5252013-08-05 20:36:57 +00002582 info->senders.push_back(sinfo);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002583 }
2584
solenberg85a04962015-10-27 03:35:21 -07002585 // Get SSRC and stats for each receiver.
2586 RTC_DCHECK(info->receivers.size() == 0);
solenberg7add0582015-11-20 09:59:34 -08002587 for (const auto& stream : recv_streams_) {
Fredrik Solenberg4f4ec0a2015-10-22 10:49:27 +02002588 webrtc::AudioReceiveStream::Stats stats = stream.second->GetStats();
2589 VoiceReceiverInfo rinfo;
2590 rinfo.add_ssrc(stats.remote_ssrc);
2591 rinfo.bytes_rcvd = stats.bytes_rcvd;
2592 rinfo.packets_rcvd = stats.packets_rcvd;
2593 rinfo.packets_lost = stats.packets_lost;
2594 rinfo.fraction_lost = stats.fraction_lost;
2595 rinfo.codec_name = stats.codec_name;
2596 rinfo.ext_seqnum = stats.ext_seqnum;
2597 rinfo.jitter_ms = stats.jitter_ms;
2598 rinfo.jitter_buffer_ms = stats.jitter_buffer_ms;
2599 rinfo.jitter_buffer_preferred_ms = stats.jitter_buffer_preferred_ms;
2600 rinfo.delay_estimate_ms = stats.delay_estimate_ms;
2601 rinfo.audio_level = stats.audio_level;
2602 rinfo.expand_rate = stats.expand_rate;
2603 rinfo.speech_expand_rate = stats.speech_expand_rate;
2604 rinfo.secondary_decoded_rate = stats.secondary_decoded_rate;
2605 rinfo.accelerate_rate = stats.accelerate_rate;
2606 rinfo.preemptive_expand_rate = stats.preemptive_expand_rate;
2607 rinfo.decoding_calls_to_silence_generator =
2608 stats.decoding_calls_to_silence_generator;
2609 rinfo.decoding_calls_to_neteq = stats.decoding_calls_to_neteq;
2610 rinfo.decoding_normal = stats.decoding_normal;
2611 rinfo.decoding_plc = stats.decoding_plc;
2612 rinfo.decoding_cng = stats.decoding_cng;
2613 rinfo.decoding_plc_cng = stats.decoding_plc_cng;
henrik.lundin63489782016-09-20 01:47:12 -07002614 rinfo.decoding_muted_output = stats.decoding_muted_output;
Fredrik Solenberg4f4ec0a2015-10-22 10:49:27 +02002615 rinfo.capture_start_ntp_time_ms = stats.capture_start_ntp_time_ms;
2616 info->receivers.push_back(rinfo);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002617 }
2618
2619 return true;
2620}
2621
Tommif888bb52015-12-12 01:37:01 +01002622void WebRtcVoiceMediaChannel::SetRawAudioSink(
2623 uint32_t ssrc,
kwiberg686a8ef2016-02-26 03:00:35 -08002624 std::unique_ptr<webrtc::AudioSinkInterface> sink) {
Tommif888bb52015-12-12 01:37:01 +01002625 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
deadbeef884f5852016-01-15 09:20:04 -08002626 LOG(LS_VERBOSE) << "WebRtcVoiceMediaChannel::SetRawAudioSink: ssrc:" << ssrc
2627 << " " << (sink ? "(ptr)" : "NULL");
2628 if (ssrc == 0) {
2629 if (default_recv_ssrc_ != -1) {
kwiberg686a8ef2016-02-26 03:00:35 -08002630 std::unique_ptr<webrtc::AudioSinkInterface> proxy_sink(
deadbeef884f5852016-01-15 09:20:04 -08002631 sink ? new ProxySink(sink.get()) : nullptr);
2632 SetRawAudioSink(default_recv_ssrc_, std::move(proxy_sink));
2633 }
2634 default_sink_ = std::move(sink);
2635 return;
2636 }
Tommif888bb52015-12-12 01:37:01 +01002637 const auto it = recv_streams_.find(ssrc);
2638 if (it == recv_streams_.end()) {
2639 LOG(LS_WARNING) << "SetRawAudioSink: no recv stream" << ssrc;
2640 return;
2641 }
deadbeef2d110be2016-01-13 12:00:26 -08002642 it->second->SetRawAudioSink(std::move(sink));
Tommif888bb52015-12-12 01:37:01 +01002643}
2644
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002645int WebRtcVoiceMediaChannel::GetOutputLevel(int channel) {
solenbergd97ec302015-10-07 01:40:33 -07002646 unsigned int ulevel = 0;
2647 int ret = engine()->voe()->volume()->GetSpeechOutputLevel(channel, ulevel);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002648 return (ret == 0) ? static_cast<int>(ulevel) : -1;
2649}
2650
Peter Boström0c4e06b2015-10-07 12:23:21 +02002651int WebRtcVoiceMediaChannel::GetReceiveChannelId(uint32_t ssrc) const {
solenberg566ef242015-11-06 15:34:49 -08002652 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
solenberg7add0582015-11-20 09:59:34 -08002653 const auto it = recv_streams_.find(ssrc);
2654 if (it != recv_streams_.end()) {
mallinath@webrtc.org67ee6b92014-02-03 16:57:16 +00002655 return it->second->channel();
solenberg8fb30c32015-10-13 03:06:58 -07002656 }
solenberg1ac56142015-10-13 03:58:19 -07002657 return -1;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002658}
2659
Peter Boström0c4e06b2015-10-07 12:23:21 +02002660int WebRtcVoiceMediaChannel::GetSendChannelId(uint32_t ssrc) const {
solenberg566ef242015-11-06 15:34:49 -08002661 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
solenbergc96df772015-10-21 13:01:53 -07002662 const auto it = send_streams_.find(ssrc);
2663 if (it != send_streams_.end()) {
mallinath@webrtc.org67ee6b92014-02-03 16:57:16 +00002664 return it->second->channel();
solenberg8fb30c32015-10-13 03:06:58 -07002665 }
wu@webrtc.org9dba5252013-08-05 20:36:57 +00002666 return -1;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002667}
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002668} // namespace cricket
2669
2670#endif // HAVE_WEBRTC_VOICE