blob: 28c50793aa3518cefa4fe35ca073e4f474022642 [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
11#ifdef HAVE_CONFIG_H
12#include <config.h>
13#endif
14
15#ifdef HAVE_WEBRTC_VOICE
16
kjellander@webrtc.org5ad12972016-02-12 06:39:40 +010017#include "webrtc/media/engine/webrtcvoiceengine.h"
henrike@webrtc.org28e20752013-07-10 00:45:36 +000018
19#include <algorithm>
20#include <cstdio>
21#include <string>
22#include <vector>
23
kjellander7324eb92016-02-25 08:36:42 -080024#include "webrtc/audio/audio_sink.h"
tfarina5237aaf2015-11-10 23:44:30 -080025#include "webrtc/base/arraysize.h"
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +000026#include "webrtc/base/base64.h"
27#include "webrtc/base/byteorder.h"
28#include "webrtc/base/common.h"
29#include "webrtc/base/helpers.h"
30#include "webrtc/base/logging.h"
31#include "webrtc/base/stringencode.h"
32#include "webrtc/base/stringutils.h"
ivoc112a3d82015-10-16 02:22:18 -070033#include "webrtc/call/rtc_event_log.h"
mallinath@webrtc.orga27be8e2013-09-27 23:04:10 +000034#include "webrtc/common.h"
kjellandera96e2d72016-02-04 23:52:28 -080035#include "webrtc/media/base/audioframe.h"
36#include "webrtc/media/base/audiorenderer.h"
37#include "webrtc/media/base/constants.h"
38#include "webrtc/media/base/streamparams.h"
kjellander@webrtc.org5ad12972016-02-12 06:39:40 +010039#include "webrtc/media/engine/webrtcmediaengine.h"
40#include "webrtc/media/engine/webrtcvoe.h"
solenberg26c8c912015-11-27 04:00:25 -080041#include "webrtc/modules/audio_coding/acm2/rent_a_codec.h"
henrike@webrtc.org28e20752013-07-10 00:45:36 +000042#include "webrtc/modules/audio_processing/include/audio_processing.h"
Henrik Kjellander98f53512015-10-28 18:17:40 +010043#include "webrtc/system_wrappers/include/field_trial.h"
solenbergbd138382015-11-20 16:08:07 -080044#include "webrtc/system_wrappers/include/trace.h"
henrike@webrtc.org28e20752013-07-10 00:45:36 +000045
henrike@webrtc.org28e20752013-07-10 00:45:36 +000046namespace cricket {
solenbergd97ec302015-10-07 01:40:33 -070047namespace {
henrike@webrtc.org28e20752013-07-10 00:45:36 +000048
solenbergbd138382015-11-20 16:08:07 -080049const int kDefaultTraceFilter = webrtc::kTraceNone | webrtc::kTraceTerseInfo |
50 webrtc::kTraceWarning | webrtc::kTraceError |
51 webrtc::kTraceCritical;
52const int kElevatedTraceFilter = kDefaultTraceFilter | webrtc::kTraceStateInfo |
53 webrtc::kTraceInfo;
54
henrike@webrtc.org28e20752013-07-10 00:45:36 +000055// On Windows Vista and newer, Microsoft introduced the concept of "Default
56// Communications Device". This means that there are two types of default
57// devices (old Wave Audio style default and Default Communications Device).
58//
59// On Windows systems which only support Wave Audio style default, uses either
60// -1 or 0 to select the default device.
henrike@webrtc.org28e20752013-07-10 00:45:36 +000061#ifdef WIN32
solenbergd97ec302015-10-07 01:40:33 -070062const int kDefaultAudioDeviceId = -1;
henrike@webrtc.org28e20752013-07-10 00:45:36 +000063#else
solenbergd97ec302015-10-07 01:40:33 -070064const int kDefaultAudioDeviceId = 0;
henrike@webrtc.org28e20752013-07-10 00:45:36 +000065#endif
66
henrike@webrtc.org28e20752013-07-10 00:45:36 +000067// Parameter used for NACK.
68// This value is equivalent to 5 seconds of audio data at 20 ms per packet.
solenbergd97ec302015-10-07 01:40:33 -070069const int kNackMaxPackets = 250;
minyue@webrtc.org2dc6f312014-10-31 05:33:10 +000070
71// Codec parameters for Opus.
henrike@webrtc.org1e09a712013-07-26 19:17:59 +000072// draft-spittka-payload-rtp-opus-03
minyue@webrtc.org2dc6f312014-10-31 05:33:10 +000073
74// Recommended bitrates:
75// 8-12 kb/s for NB speech,
76// 16-20 kb/s for WB speech,
77// 28-40 kb/s for FB speech,
78// 48-64 kb/s for FB mono music, and
79// 64-128 kb/s for FB stereo music.
80// The current implementation applies the following values to mono signals,
81// and multiplies them by 2 for stereo.
solenbergd97ec302015-10-07 01:40:33 -070082const int kOpusBitrateNb = 12000;
83const int kOpusBitrateWb = 20000;
84const int kOpusBitrateFb = 32000;
minyue@webrtc.org2dc6f312014-10-31 05:33:10 +000085
henrike@webrtc.org1e09a712013-07-26 19:17:59 +000086// Opus bitrate should be in the range between 6000 and 510000.
solenbergd97ec302015-10-07 01:40:33 -070087const int kOpusMinBitrate = 6000;
88const int kOpusMaxBitrate = 510000;
buildbot@webrtc.org5d639b32014-09-10 07:57:12 +000089
wu@webrtc.orgde305012013-10-31 15:40:38 +000090// Default audio dscp value.
91// See http://tools.ietf.org/html/rfc2474 for details.
92// See also http://tools.ietf.org/html/draft-jennings-rtcweb-qos-00
solenbergd97ec302015-10-07 01:40:33 -070093const rtc::DiffServCodePoint kAudioDscpValue = rtc::DSCP_EF;
henrike@webrtc.org1e09a712013-07-26 19:17:59 +000094
Fredrik Solenbergb5727682015-12-04 15:22:19 +010095// Constants from voice_engine_defines.h.
96const int kMinTelephoneEventCode = 0; // RFC4733 (Section 2.3.1)
97const int kMaxTelephoneEventCode = 255;
98const int kMinTelephoneEventDuration = 100;
99const int kMaxTelephoneEventDuration = 60000; // Actual limit is 2^16
100
deadbeef884f5852016-01-15 09:20:04 -0800101class ProxySink : public webrtc::AudioSinkInterface {
102 public:
103 ProxySink(AudioSinkInterface* sink) : sink_(sink) { RTC_DCHECK(sink); }
104
105 void OnData(const Data& audio) override { sink_->OnData(audio); }
106
107 private:
108 webrtc::AudioSinkInterface* sink_;
109};
110
solenberg0b675462015-10-09 01:37:09 -0700111bool ValidateStreamParams(const StreamParams& sp) {
112 if (sp.ssrcs.empty()) {
113 LOG(LS_ERROR) << "No SSRCs in stream parameters: " << sp.ToString();
114 return false;
115 }
116 if (sp.ssrcs.size() > 1) {
117 LOG(LS_ERROR) << "Multiple SSRCs in stream parameters: " << sp.ToString();
118 return false;
119 }
120 return true;
121}
122
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000123// Dumps an AudioCodec in RFC 2327-ish format.
solenbergd97ec302015-10-07 01:40:33 -0700124std::string ToString(const AudioCodec& codec) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000125 std::stringstream ss;
126 ss << codec.name << "/" << codec.clockrate << "/" << codec.channels
127 << " (" << codec.id << ")";
128 return ss.str();
129}
Minyue Li7100dcd2015-03-27 05:05:59 +0100130
solenbergd97ec302015-10-07 01:40:33 -0700131std::string ToString(const webrtc::CodecInst& codec) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000132 std::stringstream ss;
133 ss << codec.plname << "/" << codec.plfreq << "/" << codec.channels
134 << " (" << codec.pltype << ")";
135 return ss.str();
136}
137
solenbergd97ec302015-10-07 01:40:33 -0700138bool IsCodec(const AudioCodec& codec, const char* ref_name) {
Minyue Li7100dcd2015-03-27 05:05:59 +0100139 return (_stricmp(codec.name.c_str(), ref_name) == 0);
140}
141
solenbergd97ec302015-10-07 01:40:33 -0700142bool IsCodec(const webrtc::CodecInst& codec, const char* ref_name) {
Minyue Li7100dcd2015-03-27 05:05:59 +0100143 return (_stricmp(codec.plname, ref_name) == 0);
144}
145
solenbergd97ec302015-10-07 01:40:33 -0700146bool FindCodec(const std::vector<AudioCodec>& codecs,
solenberg26c8c912015-11-27 04:00:25 -0800147 const AudioCodec& codec,
148 AudioCodec* found_codec) {
Fredrik Solenbergaf9fb212015-08-26 10:45:53 +0200149 for (const AudioCodec& c : codecs) {
150 if (c.Matches(codec)) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000151 if (found_codec != NULL) {
Fredrik Solenbergaf9fb212015-08-26 10:45:53 +0200152 *found_codec = c;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000153 }
154 return true;
155 }
156 }
157 return false;
158}
wu@webrtc.org1d1ffc92013-10-16 18:12:02 +0000159
solenberg0b675462015-10-09 01:37:09 -0700160bool VerifyUniquePayloadTypes(const std::vector<AudioCodec>& codecs) {
161 if (codecs.empty()) {
162 return true;
163 }
164 std::vector<int> payload_types;
165 for (const AudioCodec& codec : codecs) {
166 payload_types.push_back(codec.id);
167 }
168 std::sort(payload_types.begin(), payload_types.end());
169 auto it = std::unique(payload_types.begin(), payload_types.end());
170 return it == payload_types.end();
171}
172
Minyue Li7100dcd2015-03-27 05:05:59 +0100173// Return true if codec.params[feature] == "1", false otherwise.
solenberg26c8c912015-11-27 04:00:25 -0800174bool IsCodecFeatureEnabled(const AudioCodec& codec, const char* feature) {
Minyue Li7100dcd2015-03-27 05:05:59 +0100175 int value;
176 return codec.GetParam(feature, &value) && value == 1;
177}
178
179// Use params[kCodecParamMaxAverageBitrate] if it is defined, use codec.bitrate
180// otherwise. If the value (either from params or codec.bitrate) <=0, use the
181// default configuration. If the value is beyond feasible bit rate of Opus,
182// clamp it. Returns the Opus bit rate for operation.
solenbergd97ec302015-10-07 01:40:33 -0700183int GetOpusBitrate(const AudioCodec& codec, int max_playback_rate) {
Minyue Li7100dcd2015-03-27 05:05:59 +0100184 int bitrate = 0;
185 bool use_param = true;
186 if (!codec.GetParam(kCodecParamMaxAverageBitrate, &bitrate)) {
187 bitrate = codec.bitrate;
188 use_param = false;
189 }
190 if (bitrate <= 0) {
191 if (max_playback_rate <= 8000) {
192 bitrate = kOpusBitrateNb;
193 } else if (max_playback_rate <= 16000) {
194 bitrate = kOpusBitrateWb;
195 } else {
196 bitrate = kOpusBitrateFb;
197 }
198
199 if (IsCodecFeatureEnabled(codec, kCodecParamStereo)) {
200 bitrate *= 2;
201 }
202 } else if (bitrate < kOpusMinBitrate || bitrate > kOpusMaxBitrate) {
203 bitrate = (bitrate < kOpusMinBitrate) ? kOpusMinBitrate : kOpusMaxBitrate;
204 std::string rate_source =
205 use_param ? "Codec parameter \"maxaveragebitrate\"" :
206 "Supplied Opus bitrate";
207 LOG(LS_WARNING) << rate_source
208 << " is invalid and is replaced by: "
209 << bitrate;
210 }
211 return bitrate;
212}
213
214// Returns kOpusDefaultPlaybackRate if params[kCodecParamMaxPlaybackRate] is not
215// defined. Returns the value of params[kCodecParamMaxPlaybackRate] otherwise.
solenbergd97ec302015-10-07 01:40:33 -0700216int GetOpusMaxPlaybackRate(const AudioCodec& codec) {
Minyue Li7100dcd2015-03-27 05:05:59 +0100217 int value;
218 if (codec.GetParam(kCodecParamMaxPlaybackRate, &value)) {
219 return value;
220 }
221 return kOpusDefaultMaxPlaybackRate;
222}
223
solenbergd97ec302015-10-07 01:40:33 -0700224void GetOpusConfig(const AudioCodec& codec, webrtc::CodecInst* voe_codec,
Minyue Li7100dcd2015-03-27 05:05:59 +0100225 bool* enable_codec_fec, int* max_playback_rate,
226 bool* enable_codec_dtx) {
227 *enable_codec_fec = IsCodecFeatureEnabled(codec, kCodecParamUseInbandFec);
228 *enable_codec_dtx = IsCodecFeatureEnabled(codec, kCodecParamUseDtx);
229 *max_playback_rate = GetOpusMaxPlaybackRate(codec);
230
231 // If OPUS, change what we send according to the "stereo" codec
232 // parameter, and not the "channels" parameter. We set
233 // voe_codec.channels to 2 if "stereo=1" and 1 otherwise. If
234 // the bitrate is not specified, i.e. is <= zero, we set it to the
235 // appropriate default value for mono or stereo Opus.
236
237 voe_codec->channels = IsCodecFeatureEnabled(codec, kCodecParamStereo) ? 2 : 1;
238 voe_codec->rate = GetOpusBitrate(codec, *max_playback_rate);
239}
240
solenberg566ef242015-11-06 15:34:49 -0800241webrtc::AudioState::Config MakeAudioStateConfig(VoEWrapper* voe_wrapper) {
242 webrtc::AudioState::Config config;
243 config.voice_engine = voe_wrapper->engine();
244 return config;
245}
246
solenberg26c8c912015-11-27 04:00:25 -0800247class WebRtcVoiceCodecs final {
248 public:
249 // TODO(solenberg): Do this filtering once off-line, add a simple AudioCodec
250 // list and add a test which verifies VoE supports the listed codecs.
251 static std::vector<AudioCodec> SupportedCodecs() {
252 LOG(LS_INFO) << "WebRtc VoiceEngine codecs:";
253 std::vector<AudioCodec> result;
254 for (webrtc::CodecInst voe_codec : webrtc::acm2::RentACodec::Database()) {
255 // Change the sample rate of G722 to 8000 to match SDP.
256 MaybeFixupG722(&voe_codec, 8000);
buildbot@webrtc.org13d67762014-05-02 17:33:29 +0000257 // Skip uncompressed formats.
Minyue Li7100dcd2015-03-27 05:05:59 +0100258 if (IsCodec(voe_codec, kL16CodecName)) {
buildbot@webrtc.org13d67762014-05-02 17:33:29 +0000259 continue;
260 }
261
262 const CodecPref* pref = NULL;
tfarina5237aaf2015-11-10 23:44:30 -0800263 for (size_t j = 0; j < arraysize(kCodecPrefs); ++j) {
Minyue Li7100dcd2015-03-27 05:05:59 +0100264 if (IsCodec(voe_codec, kCodecPrefs[j].name) &&
buildbot@webrtc.org13d67762014-05-02 17:33:29 +0000265 kCodecPrefs[j].clockrate == voe_codec.plfreq &&
266 kCodecPrefs[j].channels == voe_codec.channels) {
267 pref = &kCodecPrefs[j];
268 break;
269 }
270 }
271
272 if (pref) {
273 // Use the payload type that we've configured in our pref table;
274 // use the offset in our pref table to determine the sort order.
tfarina5237aaf2015-11-10 23:44:30 -0800275 AudioCodec codec(
276 pref->payload_type, voe_codec.plname, voe_codec.plfreq,
277 voe_codec.rate, voe_codec.channels,
278 static_cast<int>(arraysize(kCodecPrefs)) - (pref - kCodecPrefs));
buildbot@webrtc.org13d67762014-05-02 17:33:29 +0000279 LOG(LS_INFO) << ToString(codec);
Minyue Li7100dcd2015-03-27 05:05:59 +0100280 if (IsCodec(codec, kIsacCodecName)) {
minyue@webrtc.org26236952014-10-29 02:27:08 +0000281 // Indicate auto-bitrate in signaling.
buildbot@webrtc.org13d67762014-05-02 17:33:29 +0000282 codec.bitrate = 0;
283 }
Minyue Li7100dcd2015-03-27 05:05:59 +0100284 if (IsCodec(codec, kOpusCodecName)) {
buildbot@webrtc.org13d67762014-05-02 17:33:29 +0000285 // Only add fmtp parameters that differ from the spec.
286 if (kPreferredMinPTime != kOpusDefaultMinPTime) {
287 codec.params[kCodecParamMinPTime] =
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000288 rtc::ToString(kPreferredMinPTime);
buildbot@webrtc.org13d67762014-05-02 17:33:29 +0000289 }
290 if (kPreferredMaxPTime != kOpusDefaultMaxPTime) {
291 codec.params[kCodecParamMaxPTime] =
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000292 rtc::ToString(kPreferredMaxPTime);
buildbot@webrtc.org13d67762014-05-02 17:33:29 +0000293 }
pkasting@chromium.orgd3245462015-02-23 21:28:22 +0000294 codec.SetParam(kCodecParamUseInbandFec, 1);
stefanba4c0e42016-02-04 04:12:24 -0800295 codec.AddFeedbackParam(
296 FeedbackParam(kRtcpFbParamTransportCc, kParamValueEmpty));
minyue@webrtc.org4ef22d12014-11-17 09:26:39 +0000297
298 // TODO(hellner): Add ptime, sprop-stereo, and stereo
buildbot@webrtc.org13d67762014-05-02 17:33:29 +0000299 // when they can be set to values other than the default.
300 }
solenberg26c8c912015-11-27 04:00:25 -0800301 result.push_back(codec);
buildbot@webrtc.org13d67762014-05-02 17:33:29 +0000302 } else {
303 LOG(LS_WARNING) << "Unexpected codec: " << ToString(voe_codec);
304 }
305 }
solenberg26c8c912015-11-27 04:00:25 -0800306 // Make sure they are in local preference order.
307 std::sort(result.begin(), result.end(), &AudioCodec::Preferable);
308 return result;
buildbot@webrtc.org13d67762014-05-02 17:33:29 +0000309 }
buildbot@webrtc.org13d67762014-05-02 17:33:29 +0000310
solenberg26c8c912015-11-27 04:00:25 -0800311 static bool ToCodecInst(const AudioCodec& in,
312 webrtc::CodecInst* out) {
313 for (webrtc::CodecInst voe_codec : webrtc::acm2::RentACodec::Database()) {
314 // Change the sample rate of G722 to 8000 to match SDP.
315 MaybeFixupG722(&voe_codec, 8000);
316 AudioCodec codec(voe_codec.pltype, voe_codec.plname, voe_codec.plfreq,
317 voe_codec.rate, voe_codec.channels, 0);
318 bool multi_rate = IsCodecMultiRate(voe_codec);
319 // Allow arbitrary rates for ISAC to be specified.
320 if (multi_rate) {
321 // Set codec.bitrate to 0 so the check for codec.Matches() passes.
322 codec.bitrate = 0;
323 }
324 if (codec.Matches(in)) {
325 if (out) {
326 // Fixup the payload type.
327 voe_codec.pltype = in.id;
328
329 // Set bitrate if specified.
330 if (multi_rate && in.bitrate != 0) {
331 voe_codec.rate = in.bitrate;
332 }
333
334 // Reset G722 sample rate to 16000 to match WebRTC.
335 MaybeFixupG722(&voe_codec, 16000);
336
337 // Apply codec-specific settings.
338 if (IsCodec(codec, kIsacCodecName)) {
339 // If ISAC and an explicit bitrate is not specified,
340 // enable auto bitrate adjustment.
341 voe_codec.rate = (in.bitrate > 0) ? in.bitrate : -1;
342 }
343 *out = voe_codec;
344 }
345 return true;
346 }
347 }
henrik.lundin@webrtc.org8038d422014-11-11 08:38:24 +0000348 return false;
henrik.lundin@webrtc.orgf85dbce2014-11-07 12:25:00 +0000349 }
solenberg26c8c912015-11-27 04:00:25 -0800350
351 static bool IsCodecMultiRate(const webrtc::CodecInst& codec) {
352 for (size_t i = 0; i < arraysize(kCodecPrefs); ++i) {
353 if (IsCodec(codec, kCodecPrefs[i].name) &&
354 kCodecPrefs[i].clockrate == codec.plfreq) {
355 return kCodecPrefs[i].is_multi_rate;
356 }
357 }
358 return false;
359 }
360
361 // If the AudioCodec param kCodecParamPTime is set, then we will set it to
362 // codec pacsize if it's valid, or we will pick the next smallest value we
363 // support.
364 // TODO(Brave): Query supported packet sizes from ACM when the API is ready.
365 static bool SetPTimeAsPacketSize(webrtc::CodecInst* codec, int ptime_ms) {
366 for (const CodecPref& codec_pref : kCodecPrefs) {
367 if ((IsCodec(*codec, codec_pref.name) &&
368 codec_pref.clockrate == codec->plfreq) ||
369 IsCodec(*codec, kG722CodecName)) {
370 int packet_size_ms = SelectPacketSize(codec_pref, ptime_ms);
371 if (packet_size_ms) {
372 // Convert unit from milli-seconds to samples.
373 codec->pacsize = (codec->plfreq / 1000) * packet_size_ms;
374 return true;
375 }
376 }
377 }
378 return false;
379 }
380
stefanba4c0e42016-02-04 04:12:24 -0800381 static const AudioCodec* GetPreferredCodec(
382 const std::vector<AudioCodec>& codecs,
383 webrtc::CodecInst* voe_codec,
384 int* red_payload_type) {
385 RTC_DCHECK(voe_codec);
386 RTC_DCHECK(red_payload_type);
387 // Select the preferred send codec (the first non-telephone-event/CN codec).
388 for (const AudioCodec& codec : codecs) {
389 *red_payload_type = -1;
390 if (IsCodec(codec, kDtmfCodecName) || IsCodec(codec, kCnCodecName)) {
391 // Skip telephone-event/CN codec, which will be handled later.
392 continue;
393 }
394
395 // We'll use the first codec in the list to actually send audio data.
396 // Be sure to use the payload type requested by the remote side.
397 // "red", for RED audio, is a special case where the actual codec to be
398 // used is specified in params.
399 const AudioCodec* found_codec = &codec;
400 if (IsCodec(*found_codec, kRedCodecName)) {
401 // Parse out the RED parameters. If we fail, just ignore RED;
402 // we don't support all possible params/usage scenarios.
403 *red_payload_type = codec.id;
404 found_codec = GetRedSendCodec(*found_codec, codecs);
405 if (!found_codec) {
406 continue;
407 }
408 }
409 // Ignore codecs we don't know about. The negotiation step should prevent
410 // this, but double-check to be sure.
411 if (!ToCodecInst(*found_codec, voe_codec)) {
412 LOG(LS_WARNING) << "Unknown codec " << ToString(*found_codec);
413 continue;
414 }
415 return found_codec;
416 }
417 return nullptr;
418 }
419
solenberg26c8c912015-11-27 04:00:25 -0800420 private:
421 static const int kMaxNumPacketSize = 6;
422 struct CodecPref {
423 const char* name;
424 int clockrate;
Peter Kasting69558702016-01-12 16:26:35 -0800425 size_t channels;
solenberg26c8c912015-11-27 04:00:25 -0800426 int payload_type;
427 bool is_multi_rate;
428 int packet_sizes_ms[kMaxNumPacketSize];
429 };
430 // Note: keep the supported packet sizes in ascending order.
431 static const CodecPref kCodecPrefs[12];
432
433 static int SelectPacketSize(const CodecPref& codec_pref, int ptime_ms) {
434 int selected_packet_size_ms = codec_pref.packet_sizes_ms[0];
435 for (int packet_size_ms : codec_pref.packet_sizes_ms) {
436 if (packet_size_ms && packet_size_ms <= ptime_ms) {
437 selected_packet_size_ms = packet_size_ms;
438 }
439 }
440 return selected_packet_size_ms;
441 }
442
443 // Changes RTP timestamp rate of G722. This is due to the "bug" in the RFC
444 // which says that G722 should be advertised as 8 kHz although it is a 16 kHz
445 // codec.
446 static void MaybeFixupG722(webrtc::CodecInst* voe_codec, int new_plfreq) {
447 if (IsCodec(*voe_codec, kG722CodecName)) {
448 // If the ASSERT triggers, the codec definition in WebRTC VoiceEngine
449 // has changed, and this special case is no longer needed.
450 RTC_DCHECK(voe_codec->plfreq != new_plfreq);
451 voe_codec->plfreq = new_plfreq;
452 }
453 }
stefanba4c0e42016-02-04 04:12:24 -0800454
455 static const AudioCodec* GetRedSendCodec(
456 const AudioCodec& red_codec,
457 const std::vector<AudioCodec>& all_codecs) {
458 // Get the RED encodings from the parameter with no name. This may
459 // change based on what is discussed on the Jingle list.
460 // The encoding parameter is of the form "a/b"; we only support where
461 // a == b. Verify this and parse out the value into red_pt.
462 // If the parameter value is absent (as it will be until we wire up the
463 // signaling of this message), use the second codec specified (i.e. the
464 // one after "red") as the encoding parameter.
465 int red_pt = -1;
466 std::string red_params;
467 CodecParameterMap::const_iterator it = red_codec.params.find("");
468 if (it != red_codec.params.end()) {
469 red_params = it->second;
470 std::vector<std::string> red_pts;
471 if (rtc::split(red_params, '/', &red_pts) != 2 ||
472 red_pts[0] != red_pts[1] || !rtc::FromString(red_pts[0], &red_pt)) {
473 LOG(LS_WARNING) << "RED params " << red_params << " not supported.";
474 return nullptr;
475 }
476 } else if (red_codec.params.empty()) {
477 LOG(LS_WARNING) << "RED params not present, using defaults";
478 if (all_codecs.size() > 1) {
479 red_pt = all_codecs[1].id;
480 }
481 }
482
483 // Try to find red_pt in |codecs|.
484 for (const AudioCodec& codec : all_codecs) {
485 if (codec.id == red_pt) {
486 return &codec;
487 }
488 }
489 LOG(LS_WARNING) << "RED params " << red_params << " are invalid.";
490 return nullptr;
491 }
solenberg26c8c912015-11-27 04:00:25 -0800492};
493
494const WebRtcVoiceCodecs::CodecPref WebRtcVoiceCodecs::kCodecPrefs[12] = {
495 { kOpusCodecName, 48000, 2, 111, true, { 10, 20, 40, 60 } },
496 { kIsacCodecName, 16000, 1, 103, true, { 30, 60 } },
497 { kIsacCodecName, 32000, 1, 104, true, { 30 } },
498 // G722 should be advertised as 8000 Hz because of the RFC "bug".
499 { kG722CodecName, 8000, 1, 9, false, { 10, 20, 30, 40, 50, 60 } },
500 { kIlbcCodecName, 8000, 1, 102, false, { 20, 30, 40, 60 } },
501 { kPcmuCodecName, 8000, 1, 0, false, { 10, 20, 30, 40, 50, 60 } },
502 { kPcmaCodecName, 8000, 1, 8, false, { 10, 20, 30, 40, 50, 60 } },
503 { kCnCodecName, 32000, 1, 106, false, { } },
504 { kCnCodecName, 16000, 1, 105, false, { } },
505 { kCnCodecName, 8000, 1, 13, false, { } },
506 { kRedCodecName, 8000, 1, 127, false, { } },
507 { kDtmfCodecName, 8000, 1, 126, false, { } },
508};
509} // namespace {
510
511bool WebRtcVoiceEngine::ToCodecInst(const AudioCodec& in,
512 webrtc::CodecInst* out) {
513 return WebRtcVoiceCodecs::ToCodecInst(in, out);
514}
515
516WebRtcVoiceEngine::WebRtcVoiceEngine()
517 : voe_wrapper_(new VoEWrapper()),
518 audio_state_(webrtc::AudioState::Create(MakeAudioStateConfig(voe()))) {
519 Construct();
520}
521
522WebRtcVoiceEngine::WebRtcVoiceEngine(VoEWrapper* voe_wrapper)
523 : voe_wrapper_(voe_wrapper) {
524 Construct();
525}
526
527void WebRtcVoiceEngine::Construct() {
528 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
529 LOG(LS_VERBOSE) << "WebRtcVoiceEngine::WebRtcVoiceEngine";
530
531 signal_thread_checker_.DetachFromThread();
532 std::memset(&default_agc_config_, 0, sizeof(default_agc_config_));
solenberg246b8172015-12-08 09:50:23 -0800533 voe_config_.Set<webrtc::VoicePacing>(new webrtc::VoicePacing(true));
solenberg26c8c912015-11-27 04:00:25 -0800534
535 webrtc::Trace::set_level_filter(kDefaultTraceFilter);
536 webrtc::Trace::SetTraceCallback(this);
537
538 // Load our audio codec list.
539 codecs_ = WebRtcVoiceCodecs::SupportedCodecs();
henrik.lundin@webrtc.orgf85dbce2014-11-07 12:25:00 +0000540}
541
buildbot@webrtc.org13d67762014-05-02 17:33:29 +0000542WebRtcVoiceEngine::~WebRtcVoiceEngine() {
solenberg566ef242015-11-06 15:34:49 -0800543 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
buildbot@webrtc.org13d67762014-05-02 17:33:29 +0000544 LOG(LS_VERBOSE) << "WebRtcVoiceEngine::~WebRtcVoiceEngine";
buildbot@webrtc.org13d67762014-05-02 17:33:29 +0000545 if (adm_) {
546 voe_wrapper_.reset();
547 adm_->Release();
548 adm_ = NULL;
549 }
solenbergbd138382015-11-20 16:08:07 -0800550 webrtc::Trace::SetTraceCallback(nullptr);
buildbot@webrtc.org13d67762014-05-02 17:33:29 +0000551}
552
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000553bool WebRtcVoiceEngine::Init(rtc::Thread* worker_thread) {
solenberg566ef242015-11-06 15:34:49 -0800554 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
henrikg91d6ede2015-09-17 00:24:34 -0700555 RTC_DCHECK(worker_thread == rtc::Thread::Current());
buildbot@webrtc.org13d67762014-05-02 17:33:29 +0000556 LOG(LS_INFO) << "WebRtcVoiceEngine::Init";
557 bool res = InitInternal();
558 if (res) {
559 LOG(LS_INFO) << "WebRtcVoiceEngine::Init Done!";
560 } else {
561 LOG(LS_ERROR) << "WebRtcVoiceEngine::Init failed";
562 Terminate();
563 }
564 return res;
565}
566
567bool WebRtcVoiceEngine::InitInternal() {
solenberg566ef242015-11-06 15:34:49 -0800568 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
buildbot@webrtc.org13d67762014-05-02 17:33:29 +0000569 // Temporarily turn logging level up for the Init call
solenbergbd138382015-11-20 16:08:07 -0800570 webrtc::Trace::set_level_filter(kElevatedTraceFilter);
solenberg2515af22015-12-02 06:19:36 -0800571 LOG(LS_INFO) << webrtc::VoiceEngine::GetVersionString();
buildbot@webrtc.org13d67762014-05-02 17:33:29 +0000572 if (voe_wrapper_->base()->Init(adm_) == -1) {
573 LOG_RTCERR0_EX(Init, voe_wrapper_->error());
buildbot@webrtc.org13d67762014-05-02 17:33:29 +0000574 return false;
575 }
solenbergbd138382015-11-20 16:08:07 -0800576 webrtc::Trace::set_level_filter(kDefaultTraceFilter);
buildbot@webrtc.org13d67762014-05-02 17:33:29 +0000577
buildbot@webrtc.org13d67762014-05-02 17:33:29 +0000578 // Save the default AGC configuration settings. This must happen before
solenberg246b8172015-12-08 09:50:23 -0800579 // calling ApplyOptions or the default will be overwritten.
buildbot@webrtc.org13d67762014-05-02 17:33:29 +0000580 if (voe_wrapper_->processing()->GetAgcConfig(default_agc_config_) == -1) {
581 LOG_RTCERR0(GetAgcConfig);
582 return false;
583 }
584
solenberg0f7d2932016-01-15 01:40:39 -0800585 // Set default engine options.
586 {
587 AudioOptions options;
588 options.echo_cancellation = rtc::Optional<bool>(true);
589 options.auto_gain_control = rtc::Optional<bool>(true);
590 options.noise_suppression = rtc::Optional<bool>(true);
591 options.highpass_filter = rtc::Optional<bool>(true);
592 options.stereo_swapping = rtc::Optional<bool>(false);
593 options.audio_jitter_buffer_max_packets = rtc::Optional<int>(50);
594 options.audio_jitter_buffer_fast_accelerate = rtc::Optional<bool>(false);
595 options.typing_detection = rtc::Optional<bool>(true);
596 options.adjust_agc_delta = rtc::Optional<int>(0);
597 options.experimental_agc = rtc::Optional<bool>(false);
598 options.extended_filter_aec = rtc::Optional<bool>(false);
599 options.delay_agnostic_aec = rtc::Optional<bool>(false);
600 options.experimental_ns = rtc::Optional<bool>(false);
solenberg0f7d2932016-01-15 01:40:39 -0800601 if (!ApplyOptions(options)) {
602 return false;
603 }
604 }
605
buildbot@webrtc.org13d67762014-05-02 17:33:29 +0000606 // Print our codec list again for the call diagnostic log
607 LOG(LS_INFO) << "WebRtc VoiceEngine codecs:";
Fredrik Solenbergaf9fb212015-08-26 10:45:53 +0200608 for (const AudioCodec& codec : codecs_) {
609 LOG(LS_INFO) << ToString(codec);
buildbot@webrtc.org13d67762014-05-02 17:33:29 +0000610 }
611
solenberg246b8172015-12-08 09:50:23 -0800612 SetDefaultDevices();
613
buildbot@webrtc.org13d67762014-05-02 17:33:29 +0000614 initialized_ = true;
615 return true;
616}
617
buildbot@webrtc.org13d67762014-05-02 17:33:29 +0000618void WebRtcVoiceEngine::Terminate() {
solenberg566ef242015-11-06 15:34:49 -0800619 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
buildbot@webrtc.org13d67762014-05-02 17:33:29 +0000620 LOG(LS_INFO) << "WebRtcVoiceEngine::Terminate";
621 initialized_ = false;
622
623 StopAecDump();
624
buildbot@webrtc.org13d67762014-05-02 17:33:29 +0000625 voe_wrapper_->base()->Terminate();
buildbot@webrtc.org13d67762014-05-02 17:33:29 +0000626}
627
solenberg566ef242015-11-06 15:34:49 -0800628rtc::scoped_refptr<webrtc::AudioState>
629 WebRtcVoiceEngine::GetAudioState() const {
630 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
631 return audio_state_;
632}
633
nisse51542be2016-02-12 02:27:06 -0800634VoiceMediaChannel* WebRtcVoiceEngine::CreateChannel(
635 webrtc::Call* call,
636 const MediaConfig& config,
Jelena Marusicc28a8962015-05-29 15:05:44 +0200637 const AudioOptions& options) {
solenberg566ef242015-11-06 15:34:49 -0800638 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
nisse51542be2016-02-12 02:27:06 -0800639 return new WebRtcVoiceMediaChannel(this, config, options, call);
buildbot@webrtc.org13d67762014-05-02 17:33:29 +0000640}
641
buildbot@webrtc.org13d67762014-05-02 17:33:29 +0000642bool WebRtcVoiceEngine::ApplyOptions(const AudioOptions& options_in) {
solenberg566ef242015-11-06 15:34:49 -0800643 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
henrikac14f5ff2015-09-23 14:08:33 +0200644 LOG(LS_INFO) << "ApplyOptions: " << options_in.ToString();
solenberg0f7d2932016-01-15 01:40:39 -0800645 AudioOptions options = options_in; // The options are modified below.
solenberg246b8172015-12-08 09:50:23 -0800646
buildbot@webrtc.org13d67762014-05-02 17:33:29 +0000647 // kEcConference is AEC with high suppression.
648 webrtc::EcModes ec_mode = webrtc::kEcConference;
649 webrtc::AecmModes aecm_mode = webrtc::kAecmSpeakerphone;
650 webrtc::AgcModes agc_mode = webrtc::kAgcAdaptiveAnalog;
651 webrtc::NsModes ns_mode = webrtc::kNsHighSuppression;
kwiberg102c6a62015-10-30 02:47:38 -0700652 if (options.aecm_generate_comfort_noise) {
buildbot@webrtc.org13d67762014-05-02 17:33:29 +0000653 LOG(LS_VERBOSE) << "Comfort noise explicitly set to "
kwiberg102c6a62015-10-30 02:47:38 -0700654 << *options.aecm_generate_comfort_noise
655 << " (default is false).";
buildbot@webrtc.org13d67762014-05-02 17:33:29 +0000656 }
657
kjellanderfcfc8042016-01-14 11:01:09 -0800658#if defined(WEBRTC_IOS)
buildbot@webrtc.org13d67762014-05-02 17:33:29 +0000659 // On iOS, VPIO provides built-in EC and AGC.
Karl Wibergbe579832015-11-10 22:34:18 +0100660 options.echo_cancellation = rtc::Optional<bool>(false);
661 options.auto_gain_control = rtc::Optional<bool>(false);
henrika86d907c2015-09-07 16:09:50 +0200662 LOG(LS_INFO) << "Always disable AEC and AGC on iOS. Use built-in instead.";
buildbot@webrtc.org13d67762014-05-02 17:33:29 +0000663#elif defined(ANDROID)
664 ec_mode = webrtc::kEcAecm;
665#endif
666
kjellanderfcfc8042016-01-14 11:01:09 -0800667#if defined(WEBRTC_IOS) || defined(ANDROID)
buildbot@webrtc.org13d67762014-05-02 17:33:29 +0000668 // Set the AGC mode for iOS as well despite disabling it above, to avoid
669 // unsupported configuration errors from webrtc.
670 agc_mode = webrtc::kAgcFixedDigital;
Karl Wibergbe579832015-11-10 22:34:18 +0100671 options.typing_detection = rtc::Optional<bool>(false);
672 options.experimental_agc = rtc::Optional<bool>(false);
673 options.extended_filter_aec = rtc::Optional<bool>(false);
674 options.experimental_ns = rtc::Optional<bool>(false);
buildbot@webrtc.org13d67762014-05-02 17:33:29 +0000675#endif
676
Bjorn Volckerbf395c12015-03-25 22:45:56 +0100677 // Delay Agnostic AEC automatically turns on EC if not set except on iOS
678 // where the feature is not supported.
679 bool use_delay_agnostic_aec = false;
kjellanderfcfc8042016-01-14 11:01:09 -0800680#if !defined(WEBRTC_IOS)
kwiberg102c6a62015-10-30 02:47:38 -0700681 if (options.delay_agnostic_aec) {
682 use_delay_agnostic_aec = *options.delay_agnostic_aec;
Bjorn Volckerbf395c12015-03-25 22:45:56 +0100683 if (use_delay_agnostic_aec) {
Karl Wibergbe579832015-11-10 22:34:18 +0100684 options.echo_cancellation = rtc::Optional<bool>(true);
685 options.extended_filter_aec = rtc::Optional<bool>(true);
Bjorn Volckerbf395c12015-03-25 22:45:56 +0100686 ec_mode = webrtc::kEcConference;
687 }
688 }
689#endif
690
buildbot@webrtc.org13d67762014-05-02 17:33:29 +0000691 webrtc::VoEAudioProcessing* voep = voe_wrapper_->processing();
692
kwiberg102c6a62015-10-30 02:47:38 -0700693 if (options.echo_cancellation) {
henrika@webrtc.orga954c072014-12-09 16:22:09 +0000694 // Check if platform supports built-in EC. Currently only supported on
695 // Android and in combination with Java based audio layer.
696 // TODO(henrika): investigate possibility to support built-in EC also
697 // in combination with Open SL ES audio.
698 const bool built_in_aec = voe_wrapper_->hw()->BuiltInAECIsAvailable();
Bjorn Volcker73f72102015-06-03 14:50:15 +0200699 if (built_in_aec) {
Bjorn Volckerccfc9392015-05-07 07:43:17 +0200700 // Built-in EC exists on this device and use_delay_agnostic_aec is not
701 // overriding it. Enable/Disable it according to the echo_cancellation
702 // audio option.
Bjorn Volcker73f72102015-06-03 14:50:15 +0200703 const bool enable_built_in_aec =
kwiberg102c6a62015-10-30 02:47:38 -0700704 *options.echo_cancellation && !use_delay_agnostic_aec;
Bjorn Volcker73f72102015-06-03 14:50:15 +0200705 if (voe_wrapper_->hw()->EnableBuiltInAEC(enable_built_in_aec) == 0 &&
706 enable_built_in_aec) {
Bjorn Volckerbf395c12015-03-25 22:45:56 +0100707 // Disable internal software EC if built-in EC is enabled,
henrika@webrtc.orga954c072014-12-09 16:22:09 +0000708 // i.e., replace the software EC with the built-in EC.
Karl Wibergbe579832015-11-10 22:34:18 +0100709 options.echo_cancellation = rtc::Optional<bool>(false);
henrika@webrtc.orga954c072014-12-09 16:22:09 +0000710 LOG(LS_INFO) << "Disabling EC since built-in EC will be used instead";
711 }
712 }
kwiberg102c6a62015-10-30 02:47:38 -0700713 if (voep->SetEcStatus(*options.echo_cancellation, ec_mode) == -1) {
714 LOG_RTCERR2(SetEcStatus, *options.echo_cancellation, ec_mode);
buildbot@webrtc.org13d67762014-05-02 17:33:29 +0000715 return false;
716 } else {
kwiberg102c6a62015-10-30 02:47:38 -0700717 LOG(LS_INFO) << "Echo control set to " << *options.echo_cancellation
henrika86d907c2015-09-07 16:09:50 +0200718 << " with mode " << ec_mode;
buildbot@webrtc.org13d67762014-05-02 17:33:29 +0000719 }
720#if !defined(ANDROID)
721 // TODO(ajm): Remove the error return on Android from webrtc.
kwiberg102c6a62015-10-30 02:47:38 -0700722 if (voep->SetEcMetricsStatus(*options.echo_cancellation) == -1) {
723 LOG_RTCERR1(SetEcMetricsStatus, *options.echo_cancellation);
buildbot@webrtc.org13d67762014-05-02 17:33:29 +0000724 return false;
725 }
726#endif
727 if (ec_mode == webrtc::kEcAecm) {
kwiberg102c6a62015-10-30 02:47:38 -0700728 bool cn = options.aecm_generate_comfort_noise.value_or(false);
729 if (voep->SetAecmMode(aecm_mode, cn) != 0) {
730 LOG_RTCERR2(SetAecmMode, aecm_mode, cn);
buildbot@webrtc.org13d67762014-05-02 17:33:29 +0000731 return false;
732 }
733 }
734 }
735
kwiberg102c6a62015-10-30 02:47:38 -0700736 if (options.auto_gain_control) {
henrikac14f5ff2015-09-23 14:08:33 +0200737 const bool built_in_agc = voe_wrapper_->hw()->BuiltInAGCIsAvailable();
738 if (built_in_agc) {
kwiberg102c6a62015-10-30 02:47:38 -0700739 if (voe_wrapper_->hw()->EnableBuiltInAGC(*options.auto_gain_control) ==
740 0 &&
741 *options.auto_gain_control) {
henrikac14f5ff2015-09-23 14:08:33 +0200742 // Disable internal software AGC if built-in AGC is enabled,
743 // i.e., replace the software AGC with the built-in AGC.
Karl Wibergbe579832015-11-10 22:34:18 +0100744 options.auto_gain_control = rtc::Optional<bool>(false);
henrikac14f5ff2015-09-23 14:08:33 +0200745 LOG(LS_INFO) << "Disabling AGC since built-in AGC will be used instead";
746 }
747 }
kwiberg102c6a62015-10-30 02:47:38 -0700748 if (voep->SetAgcStatus(*options.auto_gain_control, agc_mode) == -1) {
749 LOG_RTCERR2(SetAgcStatus, *options.auto_gain_control, agc_mode);
buildbot@webrtc.org13d67762014-05-02 17:33:29 +0000750 return false;
751 } else {
kwiberg102c6a62015-10-30 02:47:38 -0700752 LOG(LS_INFO) << "Auto gain set to " << *options.auto_gain_control
753 << " with mode " << agc_mode;
buildbot@webrtc.org13d67762014-05-02 17:33:29 +0000754 }
755 }
756
kwiberg102c6a62015-10-30 02:47:38 -0700757 if (options.tx_agc_target_dbov || options.tx_agc_digital_compression_gain ||
758 options.tx_agc_limiter) {
buildbot@webrtc.org13d67762014-05-02 17:33:29 +0000759 // Override default_agc_config_. Generally, an unset option means "leave
760 // the VoE bits alone" in this function, so we want whatever is set to be
761 // stored as the new "default". If we didn't, then setting e.g.
762 // tx_agc_target_dbov would reset digital compression gain and limiter
763 // settings.
764 // Also, if we don't update default_agc_config_, then adjust_agc_delta
765 // would be an offset from the original values, and not whatever was set
766 // explicitly.
kwiberg102c6a62015-10-30 02:47:38 -0700767 default_agc_config_.targetLeveldBOv = options.tx_agc_target_dbov.value_or(
768 default_agc_config_.targetLeveldBOv);
buildbot@webrtc.org13d67762014-05-02 17:33:29 +0000769 default_agc_config_.digitalCompressionGaindB =
kwiberg102c6a62015-10-30 02:47:38 -0700770 options.tx_agc_digital_compression_gain.value_or(
buildbot@webrtc.org13d67762014-05-02 17:33:29 +0000771 default_agc_config_.digitalCompressionGaindB);
772 default_agc_config_.limiterEnable =
kwiberg102c6a62015-10-30 02:47:38 -0700773 options.tx_agc_limiter.value_or(default_agc_config_.limiterEnable);
buildbot@webrtc.org13d67762014-05-02 17:33:29 +0000774 if (voe_wrapper_->processing()->SetAgcConfig(default_agc_config_) == -1) {
775 LOG_RTCERR3(SetAgcConfig,
776 default_agc_config_.targetLeveldBOv,
777 default_agc_config_.digitalCompressionGaindB,
778 default_agc_config_.limiterEnable);
779 return false;
780 }
781 }
782
kwiberg102c6a62015-10-30 02:47:38 -0700783 if (options.noise_suppression) {
henrikac14f5ff2015-09-23 14:08:33 +0200784 const bool built_in_ns = voe_wrapper_->hw()->BuiltInNSIsAvailable();
785 if (built_in_ns) {
kwiberg102c6a62015-10-30 02:47:38 -0700786 if (voe_wrapper_->hw()->EnableBuiltInNS(*options.noise_suppression) ==
787 0 &&
788 *options.noise_suppression) {
henrikac14f5ff2015-09-23 14:08:33 +0200789 // Disable internal software NS if built-in NS is enabled,
790 // i.e., replace the software NS with the built-in NS.
Karl Wibergbe579832015-11-10 22:34:18 +0100791 options.noise_suppression = rtc::Optional<bool>(false);
henrikac14f5ff2015-09-23 14:08:33 +0200792 LOG(LS_INFO) << "Disabling NS since built-in NS will be used instead";
793 }
794 }
kwiberg102c6a62015-10-30 02:47:38 -0700795 if (voep->SetNsStatus(*options.noise_suppression, ns_mode) == -1) {
796 LOG_RTCERR2(SetNsStatus, *options.noise_suppression, ns_mode);
buildbot@webrtc.org13d67762014-05-02 17:33:29 +0000797 return false;
798 } else {
kwiberg102c6a62015-10-30 02:47:38 -0700799 LOG(LS_INFO) << "Noise suppression set to " << *options.noise_suppression
henrikac14f5ff2015-09-23 14:08:33 +0200800 << " with mode " << ns_mode;
buildbot@webrtc.org13d67762014-05-02 17:33:29 +0000801 }
802 }
803
kwiberg102c6a62015-10-30 02:47:38 -0700804 if (options.highpass_filter) {
805 LOG(LS_INFO) << "High pass filter enabled? " << *options.highpass_filter;
806 if (voep->EnableHighPassFilter(*options.highpass_filter) == -1) {
807 LOG_RTCERR1(SetHighpassFilterStatus, *options.highpass_filter);
buildbot@webrtc.org13d67762014-05-02 17:33:29 +0000808 return false;
809 }
810 }
811
kwiberg102c6a62015-10-30 02:47:38 -0700812 if (options.stereo_swapping) {
813 LOG(LS_INFO) << "Stereo swapping enabled? " << *options.stereo_swapping;
814 voep->EnableStereoChannelSwapping(*options.stereo_swapping);
815 if (voep->IsStereoChannelSwappingEnabled() != *options.stereo_swapping) {
816 LOG_RTCERR1(EnableStereoChannelSwapping, *options.stereo_swapping);
buildbot@webrtc.org13d67762014-05-02 17:33:29 +0000817 return false;
818 }
819 }
820
kwiberg102c6a62015-10-30 02:47:38 -0700821 if (options.audio_jitter_buffer_max_packets) {
822 LOG(LS_INFO) << "NetEq capacity is "
823 << *options.audio_jitter_buffer_max_packets;
Henrik Lundin64dad832015-05-11 12:44:23 +0200824 voe_config_.Set<webrtc::NetEqCapacityConfig>(
kwiberg102c6a62015-10-30 02:47:38 -0700825 new webrtc::NetEqCapacityConfig(
826 *options.audio_jitter_buffer_max_packets));
Henrik Lundin64dad832015-05-11 12:44:23 +0200827 }
828
kwiberg102c6a62015-10-30 02:47:38 -0700829 if (options.audio_jitter_buffer_fast_accelerate) {
830 LOG(LS_INFO) << "NetEq fast mode? "
831 << *options.audio_jitter_buffer_fast_accelerate;
Henrik Lundin5263b3c2015-06-01 10:29:41 +0200832 voe_config_.Set<webrtc::NetEqFastAccelerate>(
kwiberg102c6a62015-10-30 02:47:38 -0700833 new webrtc::NetEqFastAccelerate(
834 *options.audio_jitter_buffer_fast_accelerate));
Henrik Lundin5263b3c2015-06-01 10:29:41 +0200835 }
836
kwiberg102c6a62015-10-30 02:47:38 -0700837 if (options.typing_detection) {
838 LOG(LS_INFO) << "Typing detection is enabled? "
839 << *options.typing_detection;
840 if (voep->SetTypingDetectionStatus(*options.typing_detection) == -1) {
buildbot@webrtc.org13d67762014-05-02 17:33:29 +0000841 // In case of error, log the info and continue
kwiberg102c6a62015-10-30 02:47:38 -0700842 LOG_RTCERR1(SetTypingDetectionStatus, *options.typing_detection);
buildbot@webrtc.org13d67762014-05-02 17:33:29 +0000843 }
844 }
845
kwiberg102c6a62015-10-30 02:47:38 -0700846 if (options.adjust_agc_delta) {
847 LOG(LS_INFO) << "Adjust agc delta is " << *options.adjust_agc_delta;
848 if (!AdjustAgcLevel(*options.adjust_agc_delta)) {
buildbot@webrtc.org13d67762014-05-02 17:33:29 +0000849 return false;
850 }
851 }
852
buildbot@webrtc.org1f8a2372014-08-28 10:52:44 +0000853 webrtc::Config config;
854
kwiberg102c6a62015-10-30 02:47:38 -0700855 if (options.delay_agnostic_aec)
856 delay_agnostic_aec_ = options.delay_agnostic_aec;
857 if (delay_agnostic_aec_) {
858 LOG(LS_INFO) << "Delay agnostic aec is enabled? " << *delay_agnostic_aec_;
henrik.lundin0f133b92015-07-02 00:17:55 -0700859 config.Set<webrtc::DelayAgnostic>(
kwiberg102c6a62015-10-30 02:47:38 -0700860 new webrtc::DelayAgnostic(*delay_agnostic_aec_));
Bjorn Volckerbf395c12015-03-25 22:45:56 +0100861 }
862
kwiberg102c6a62015-10-30 02:47:38 -0700863 if (options.extended_filter_aec) {
864 extended_filter_aec_ = options.extended_filter_aec;
865 }
866 if (extended_filter_aec_) {
867 LOG(LS_INFO) << "Extended filter aec is enabled? " << *extended_filter_aec_;
Henrik Lundin441f6342015-06-09 16:03:13 +0200868 config.Set<webrtc::ExtendedFilter>(
kwiberg102c6a62015-10-30 02:47:38 -0700869 new webrtc::ExtendedFilter(*extended_filter_aec_));
buildbot@webrtc.org1f8a2372014-08-28 10:52:44 +0000870 }
871
kwiberg102c6a62015-10-30 02:47:38 -0700872 if (options.experimental_ns) {
873 experimental_ns_ = options.experimental_ns;
874 }
875 if (experimental_ns_) {
876 LOG(LS_INFO) << "Experimental ns is enabled? " << *experimental_ns_;
buildbot@webrtc.org1f8a2372014-08-28 10:52:44 +0000877 config.Set<webrtc::ExperimentalNs>(
kwiberg102c6a62015-10-30 02:47:38 -0700878 new webrtc::ExperimentalNs(*experimental_ns_));
buildbot@webrtc.org1f8a2372014-08-28 10:52:44 +0000879 }
buildbot@webrtc.org1f8a2372014-08-28 10:52:44 +0000880
881 // We check audioproc for the benefit of tests, since FakeWebRtcVoiceEngine
882 // returns NULL on audio_processing().
883 webrtc::AudioProcessing* audioproc = voe_wrapper_->base()->audio_processing();
884 if (audioproc) {
885 audioproc->SetExtraOptions(config);
886 }
887
kwiberg102c6a62015-10-30 02:47:38 -0700888 if (options.recording_sample_rate) {
889 LOG(LS_INFO) << "Recording sample rate is "
890 << *options.recording_sample_rate;
891 if (voe_wrapper_->hw()->SetRecordingSampleRate(
892 *options.recording_sample_rate)) {
893 LOG_RTCERR1(SetRecordingSampleRate, *options.recording_sample_rate);
buildbot@webrtc.org13d67762014-05-02 17:33:29 +0000894 }
895 }
896
kwiberg102c6a62015-10-30 02:47:38 -0700897 if (options.playout_sample_rate) {
898 LOG(LS_INFO) << "Playout sample rate is " << *options.playout_sample_rate;
899 if (voe_wrapper_->hw()->SetPlayoutSampleRate(
900 *options.playout_sample_rate)) {
901 LOG_RTCERR1(SetPlayoutSampleRate, *options.playout_sample_rate);
buildbot@webrtc.org13d67762014-05-02 17:33:29 +0000902 }
903 }
904
905 return true;
906}
907
solenberg246b8172015-12-08 09:50:23 -0800908void WebRtcVoiceEngine::SetDefaultDevices() {
solenberg566ef242015-11-06 15:34:49 -0800909 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
kjellanderfcfc8042016-01-14 11:01:09 -0800910#if !defined(WEBRTC_IOS)
solenberg246b8172015-12-08 09:50:23 -0800911 int in_id = kDefaultAudioDeviceId;
912 int out_id = kDefaultAudioDeviceId;
913 LOG(LS_INFO) << "Setting microphone to (id=" << in_id
914 << ") and speaker to (id=" << out_id << ")";
buildbot@webrtc.org13d67762014-05-02 17:33:29 +0000915
solenbergc1a1b352015-09-22 13:31:20 -0700916 bool ret = true;
solenberg246b8172015-12-08 09:50:23 -0800917 if (voe_wrapper_->hw()->SetRecordingDevice(in_id) == -1) {
918 LOG_RTCERR1(SetRecordingDevice, in_id);
buildbot@webrtc.org13d67762014-05-02 17:33:29 +0000919 ret = false;
920 }
solenberg246b8172015-12-08 09:50:23 -0800921 webrtc::AudioProcessing* ap = voe()->base()->audio_processing();
922 if (ap) {
923 ap->Initialize();
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000924 }
925
solenberg246b8172015-12-08 09:50:23 -0800926 if (voe_wrapper_->hw()->SetPlayoutDevice(out_id) == -1) {
927 LOG_RTCERR1(SetPlayoutDevice, out_id);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000928 ret = false;
929 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000930
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000931 if (ret) {
solenberg246b8172015-12-08 09:50:23 -0800932 LOG(LS_INFO) << "Set microphone to (id=" << in_id
933 << ") and speaker to (id=" << out_id << ")";
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000934 }
kjellanderfcfc8042016-01-14 11:01:09 -0800935#endif // !WEBRTC_IOS
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000936}
937
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000938bool WebRtcVoiceEngine::GetOutputVolume(int* level) {
solenberg566ef242015-11-06 15:34:49 -0800939 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000940 unsigned int ulevel;
941 if (voe_wrapper_->volume()->GetSpeakerVolume(ulevel) == -1) {
942 LOG_RTCERR1(GetSpeakerVolume, level);
943 return false;
944 }
945 *level = ulevel;
946 return true;
947}
948
949bool WebRtcVoiceEngine::SetOutputVolume(int level) {
solenberg566ef242015-11-06 15:34:49 -0800950 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
henrikg91d6ede2015-09-17 00:24:34 -0700951 RTC_DCHECK(level >= 0 && level <= 255);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000952 if (voe_wrapper_->volume()->SetSpeakerVolume(level) == -1) {
953 LOG_RTCERR1(SetSpeakerVolume, level);
954 return false;
955 }
956 return true;
957}
958
959int WebRtcVoiceEngine::GetInputLevel() {
solenberg566ef242015-11-06 15:34:49 -0800960 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000961 unsigned int ulevel;
962 return (voe_wrapper_->volume()->GetSpeechInputLevel(ulevel) != -1) ?
963 static_cast<int>(ulevel) : -1;
964}
965
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000966const std::vector<AudioCodec>& WebRtcVoiceEngine::codecs() {
solenberg566ef242015-11-06 15:34:49 -0800967 RTC_DCHECK(signal_thread_checker_.CalledOnValidThread());
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000968 return codecs_;
969}
970
Stefan Holmer9d69c3f2015-12-07 10:45:43 +0100971RtpCapabilities WebRtcVoiceEngine::GetCapabilities() const {
solenberg566ef242015-11-06 15:34:49 -0800972 RTC_DCHECK(signal_thread_checker_.CalledOnValidThread());
Stefan Holmer9d69c3f2015-12-07 10:45:43 +0100973 RtpCapabilities capabilities;
974 capabilities.header_extensions.push_back(RtpHeaderExtension(
975 kRtpAudioLevelHeaderExtension, kRtpAudioLevelHeaderExtensionDefaultId));
976 capabilities.header_extensions.push_back(
977 RtpHeaderExtension(kRtpAbsoluteSenderTimeHeaderExtension,
978 kRtpAbsoluteSenderTimeHeaderExtensionDefaultId));
stefanba4c0e42016-02-04 04:12:24 -0800979 if (webrtc::field_trial::FindFullName("WebRTC-Audio-SendSideBwe") ==
980 "Enabled") {
981 capabilities.header_extensions.push_back(RtpHeaderExtension(
982 kRtpTransportSequenceNumberHeaderExtension,
983 kRtpTransportSequenceNumberHeaderExtensionDefaultId));
984 }
Stefan Holmer9d69c3f2015-12-07 10:45:43 +0100985 return capabilities;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000986}
987
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000988int WebRtcVoiceEngine::GetLastEngineError() {
solenberg566ef242015-11-06 15:34:49 -0800989 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000990 return voe_wrapper_->error();
991}
992
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000993void WebRtcVoiceEngine::Print(webrtc::TraceLevel level, const char* trace,
994 int length) {
solenberg566ef242015-11-06 15:34:49 -0800995 // Note: This callback can happen on any thread!
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000996 rtc::LoggingSeverity sev = rtc::LS_VERBOSE;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000997 if (level == webrtc::kTraceError || level == webrtc::kTraceCritical)
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000998 sev = rtc::LS_ERROR;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000999 else if (level == webrtc::kTraceWarning)
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00001000 sev = rtc::LS_WARNING;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001001 else if (level == webrtc::kTraceStateInfo || level == webrtc::kTraceInfo)
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00001002 sev = rtc::LS_INFO;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001003 else if (level == webrtc::kTraceTerseInfo)
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00001004 sev = rtc::LS_INFO;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001005
1006 // Skip past boilerplate prefix text
1007 if (length < 72) {
1008 std::string msg(trace, length);
1009 LOG(LS_ERROR) << "Malformed webrtc log message: ";
1010 LOG_V(sev) << msg;
1011 } else {
1012 std::string msg(trace + 71, length - 72);
Peter Boströmd5c75b12015-09-23 13:24:32 +02001013 LOG_V(sev) << "webrtc: " << msg;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001014 }
1015}
1016
solenberg63b34542015-09-29 06:06:31 -07001017void WebRtcVoiceEngine::RegisterChannel(WebRtcVoiceMediaChannel* channel) {
solenberg566ef242015-11-06 15:34:49 -08001018 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
1019 RTC_DCHECK(channel);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001020 channels_.push_back(channel);
1021}
1022
solenberg63b34542015-09-29 06:06:31 -07001023void WebRtcVoiceEngine::UnregisterChannel(WebRtcVoiceMediaChannel* channel) {
solenberg566ef242015-11-06 15:34:49 -08001024 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
solenberg63b34542015-09-29 06:06:31 -07001025 auto it = std::find(channels_.begin(), channels_.end(), channel);
solenberg566ef242015-11-06 15:34:49 -08001026 RTC_DCHECK(it != channels_.end());
1027 channels_.erase(it);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001028}
1029
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001030// Adjusts the default AGC target level by the specified delta.
1031// NB: If we start messing with other config fields, we'll want
1032// to save the current webrtc::AgcConfig as well.
1033bool WebRtcVoiceEngine::AdjustAgcLevel(int delta) {
solenberg566ef242015-11-06 15:34:49 -08001034 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001035 webrtc::AgcConfig config = default_agc_config_;
1036 config.targetLeveldBOv -= delta;
1037
1038 LOG(LS_INFO) << "Adjusting AGC level from default -"
1039 << default_agc_config_.targetLeveldBOv << "dB to -"
1040 << config.targetLeveldBOv << "dB";
1041
1042 if (voe_wrapper_->processing()->SetAgcConfig(config) == -1) {
1043 LOG_RTCERR1(SetAgcConfig, config.targetLeveldBOv);
1044 return false;
1045 }
1046 return true;
1047}
1048
Fredrik Solenbergccb49e72015-05-19 11:37:56 +02001049bool WebRtcVoiceEngine::SetAudioDeviceModule(webrtc::AudioDeviceModule* adm) {
solenberg566ef242015-11-06 15:34:49 -08001050 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001051 if (initialized_) {
1052 LOG(LS_WARNING) << "SetAudioDeviceModule can not be called after Init.";
1053 return false;
1054 }
1055 if (adm_) {
1056 adm_->Release();
1057 adm_ = NULL;
1058 }
1059 if (adm) {
1060 adm_ = adm;
1061 adm_->AddRef();
1062 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001063 return true;
1064}
1065
ivocd66b44d2016-01-15 03:06:36 -08001066bool WebRtcVoiceEngine::StartAecDump(rtc::PlatformFile file,
1067 int64_t max_size_bytes) {
solenberg566ef242015-11-06 15:34:49 -08001068 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00001069 FILE* aec_dump_file_stream = rtc::FdopenPlatformFileForWriting(file);
wu@webrtc.orga8910d22014-01-23 22:12:45 +00001070 if (!aec_dump_file_stream) {
1071 LOG(LS_ERROR) << "Could not open AEC dump file stream.";
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00001072 if (!rtc::ClosePlatformFile(file))
wu@webrtc.orga8910d22014-01-23 22:12:45 +00001073 LOG(LS_WARNING) << "Could not close file.";
1074 return false;
1075 }
wu@webrtc.orga9890802013-12-13 00:21:03 +00001076 StopAecDump();
ivocd66b44d2016-01-15 03:06:36 -08001077 if (voe_wrapper_->base()->audio_processing()->StartDebugRecording(
1078 aec_dump_file_stream, max_size_bytes) !=
wu@webrtc.orga9890802013-12-13 00:21:03 +00001079 webrtc::AudioProcessing::kNoError) {
wu@webrtc.orga8910d22014-01-23 22:12:45 +00001080 LOG_RTCERR0(StartDebugRecording);
1081 fclose(aec_dump_file_stream);
wu@webrtc.orga9890802013-12-13 00:21:03 +00001082 return false;
1083 }
1084 is_dumping_aec_ = true;
1085 return true;
wu@webrtc.orga9890802013-12-13 00:21:03 +00001086}
1087
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001088void WebRtcVoiceEngine::StartAecDump(const std::string& filename) {
solenberg566ef242015-11-06 15:34:49 -08001089 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001090 if (!is_dumping_aec_) {
1091 // Start dumping AEC when we are not dumping.
ivocd66b44d2016-01-15 03:06:36 -08001092 if (voe_wrapper_->base()->audio_processing()->StartDebugRecording(
1093 filename.c_str(), -1) != webrtc::AudioProcessing::kNoError) {
wu@webrtc.orga9890802013-12-13 00:21:03 +00001094 LOG_RTCERR1(StartDebugRecording, filename.c_str());
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001095 } else {
1096 is_dumping_aec_ = true;
1097 }
1098 }
1099}
1100
1101void WebRtcVoiceEngine::StopAecDump() {
solenberg566ef242015-11-06 15:34:49 -08001102 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001103 if (is_dumping_aec_) {
1104 // Stop dumping AEC when we are dumping.
ivocd66b44d2016-01-15 03:06:36 -08001105 if (voe_wrapper_->base()->audio_processing()->StopDebugRecording() !=
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001106 webrtc::AudioProcessing::kNoError) {
1107 LOG_RTCERR0(StopDebugRecording);
1108 }
1109 is_dumping_aec_ = false;
1110 }
1111}
1112
ivoc112a3d82015-10-16 02:22:18 -07001113bool WebRtcVoiceEngine::StartRtcEventLog(rtc::PlatformFile file) {
solenberg566ef242015-11-06 15:34:49 -08001114 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
ivoc20834ca2016-02-04 06:33:37 -08001115 webrtc::RtcEventLog* event_log = voe_wrapper_->codec()->GetEventLog();
1116 if (event_log) {
1117 return event_log->StartLogging(file);
1118 }
1119 LOG_RTCERR0(StartRtcEventLog);
1120 return false;
ivoc112a3d82015-10-16 02:22:18 -07001121}
1122
1123void WebRtcVoiceEngine::StopRtcEventLog() {
solenberg566ef242015-11-06 15:34:49 -08001124 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
ivoc20834ca2016-02-04 06:33:37 -08001125 webrtc::RtcEventLog* event_log = voe_wrapper_->codec()->GetEventLog();
1126 if (event_log) {
1127 event_log->StopLogging();
1128 return;
1129 }
1130 LOG_RTCERR0(StopRtcEventLog);
ivoc112a3d82015-10-16 02:22:18 -07001131}
1132
solenberg0a617e22015-10-20 15:49:38 -07001133int WebRtcVoiceEngine::CreateVoEChannel() {
solenberg566ef242015-11-06 15:34:49 -08001134 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
solenberg0a617e22015-10-20 15:49:38 -07001135 return voe_wrapper_->base()->CreateChannel(voe_config_);
sergeyu@chromium.org5bc25c42013-12-05 00:24:06 +00001136}
1137
solenbergc96df772015-10-21 13:01:53 -07001138class WebRtcVoiceMediaChannel::WebRtcAudioSendStream
mallinath@webrtc.org67ee6b92014-02-03 16:57:16 +00001139 : public AudioRenderer::Sink {
1140 public:
solenbergc96df772015-10-21 13:01:53 -07001141 WebRtcAudioSendStream(int ch, webrtc::AudioTransport* voe_audio_transport,
solenberg3a941542015-11-16 07:34:50 -08001142 uint32_t ssrc, const std::string& c_name,
1143 const std::vector<webrtc::RtpExtension>& extensions,
1144 webrtc::Call* call)
solenberg7add0582015-11-20 09:59:34 -08001145 : voe_audio_transport_(voe_audio_transport),
solenberg3a941542015-11-16 07:34:50 -08001146 call_(call),
1147 config_(nullptr) {
solenberg85a04962015-10-27 03:35:21 -07001148 RTC_DCHECK_GE(ch, 0);
1149 // TODO(solenberg): Once we're not using FakeWebRtcVoiceEngine anymore:
1150 // RTC_DCHECK(voe_audio_transport);
solenbergc96df772015-10-21 13:01:53 -07001151 RTC_DCHECK(call);
solenberg85a04962015-10-27 03:35:21 -07001152 audio_capture_thread_checker_.DetachFromThread();
solenberg3a941542015-11-16 07:34:50 -08001153 config_.rtp.ssrc = ssrc;
1154 config_.rtp.c_name = c_name;
1155 config_.voe_channel_id = ch;
1156 RecreateAudioSendStream(extensions);
solenbergc96df772015-10-21 13:01:53 -07001157 }
solenberg3a941542015-11-16 07:34:50 -08001158
solenbergc96df772015-10-21 13:01:53 -07001159 ~WebRtcAudioSendStream() override {
solenberg566ef242015-11-06 15:34:49 -08001160 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
solenbergc96df772015-10-21 13:01:53 -07001161 Stop();
1162 call_->DestroyAudioSendStream(stream_);
1163 }
henrike@webrtc.org1e09a712013-07-26 19:17:59 +00001164
solenberg3a941542015-11-16 07:34:50 -08001165 void RecreateAudioSendStream(
1166 const std::vector<webrtc::RtpExtension>& extensions) {
1167 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
1168 if (stream_) {
1169 call_->DestroyAudioSendStream(stream_);
1170 stream_ = nullptr;
1171 }
1172 config_.rtp.extensions = extensions;
1173 RTC_DCHECK(!stream_);
1174 stream_ = call_->CreateAudioSendStream(config_);
1175 RTC_CHECK(stream_);
1176 }
1177
Fredrik Solenbergb5727682015-12-04 15:22:19 +01001178 bool SendTelephoneEvent(int payload_type, uint8_t event,
1179 uint32_t duration_ms) {
1180 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
1181 RTC_DCHECK(stream_);
1182 return stream_->SendTelephoneEvent(payload_type, event, duration_ms);
1183 }
1184
solenberg3a941542015-11-16 07:34:50 -08001185 webrtc::AudioSendStream::Stats GetStats() const {
1186 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
1187 RTC_DCHECK(stream_);
1188 return stream_->GetStats();
1189 }
1190
mallinath@webrtc.org67ee6b92014-02-03 16:57:16 +00001191 // Starts the rendering by setting a sink to the renderer to get data
1192 // callback.
henrike@webrtc.orga7b98182014-02-21 15:51:43 +00001193 // This method is called on the libjingle worker thread.
mallinath@webrtc.org67ee6b92014-02-03 16:57:16 +00001194 // TODO(xians): Make sure Start() is called only once.
1195 void Start(AudioRenderer* renderer) {
solenberg566ef242015-11-06 15:34:49 -08001196 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
solenbergc96df772015-10-21 13:01:53 -07001197 RTC_DCHECK(renderer);
1198 if (renderer_) {
henrikg91d6ede2015-09-17 00:24:34 -07001199 RTC_DCHECK(renderer_ == renderer);
mallinath@webrtc.org67ee6b92014-02-03 16:57:16 +00001200 return;
1201 }
mallinath@webrtc.org67ee6b92014-02-03 16:57:16 +00001202 renderer->SetSink(this);
1203 renderer_ = renderer;
1204 }
1205
solenbergc96df772015-10-21 13:01:53 -07001206 // Stops rendering by setting the sink of the renderer to nullptr. No data
mallinath@webrtc.org67ee6b92014-02-03 16:57:16 +00001207 // callback will be received after this method.
henrike@webrtc.orga7b98182014-02-21 15:51:43 +00001208 // This method is called on the libjingle worker thread.
mallinath@webrtc.org67ee6b92014-02-03 16:57:16 +00001209 void Stop() {
solenberg566ef242015-11-06 15:34:49 -08001210 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
solenbergc96df772015-10-21 13:01:53 -07001211 if (renderer_) {
1212 renderer_->SetSink(nullptr);
1213 renderer_ = nullptr;
solenberg98c68862015-10-09 03:27:14 -07001214 }
mallinath@webrtc.org67ee6b92014-02-03 16:57:16 +00001215 }
1216
1217 // AudioRenderer::Sink implementation.
henrike@webrtc.orga7b98182014-02-21 15:51:43 +00001218 // This method is called on the audio thread.
kjellander@webrtc.org14665ff2015-03-04 12:58:35 +00001219 void OnData(const void* audio_data,
1220 int bits_per_sample,
1221 int sample_rate,
Peter Kasting69558702016-01-12 16:26:35 -08001222 size_t number_of_channels,
Peter Kastingdce40cf2015-08-24 14:52:23 -07001223 size_t number_of_frames) override {
solenberg566ef242015-11-06 15:34:49 -08001224 RTC_DCHECK(!worker_thread_checker_.CalledOnValidThread());
solenberg85a04962015-10-27 03:35:21 -07001225 RTC_DCHECK(audio_capture_thread_checker_.CalledOnValidThread());
solenbergc96df772015-10-21 13:01:53 -07001226 RTC_DCHECK(voe_audio_transport_);
solenberg7add0582015-11-20 09:59:34 -08001227 voe_audio_transport_->OnData(config_.voe_channel_id,
henrike@webrtc.orga7b98182014-02-21 15:51:43 +00001228 audio_data,
1229 bits_per_sample,
1230 sample_rate,
1231 number_of_channels,
1232 number_of_frames);
henrike@webrtc.orga7b98182014-02-21 15:51:43 +00001233 }
1234
1235 // Callback from the |renderer_| when it is going away. In case Start() has
1236 // never been called, this callback won't be triggered.
kjellander@webrtc.org14665ff2015-03-04 12:58:35 +00001237 void OnClose() override {
solenberg566ef242015-11-06 15:34:49 -08001238 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
solenbergc96df772015-10-21 13:01:53 -07001239 // Set |renderer_| to nullptr to make sure no more callback will get into
henrike@webrtc.orga7b98182014-02-21 15:51:43 +00001240 // the renderer.
solenbergc96df772015-10-21 13:01:53 -07001241 renderer_ = nullptr;
mallinath@webrtc.org67ee6b92014-02-03 16:57:16 +00001242 }
1243
1244 // Accessor to the VoE channel ID.
solenberg85a04962015-10-27 03:35:21 -07001245 int channel() const {
solenberg566ef242015-11-06 15:34:49 -08001246 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
solenberg7add0582015-11-20 09:59:34 -08001247 return config_.voe_channel_id;
solenberg85a04962015-10-27 03:35:21 -07001248 }
mallinath@webrtc.org67ee6b92014-02-03 16:57:16 +00001249
1250 private:
solenberg566ef242015-11-06 15:34:49 -08001251 rtc::ThreadChecker worker_thread_checker_;
solenberg85a04962015-10-27 03:35:21 -07001252 rtc::ThreadChecker audio_capture_thread_checker_;
solenbergc96df772015-10-21 13:01:53 -07001253 webrtc::AudioTransport* const voe_audio_transport_ = nullptr;
1254 webrtc::Call* call_ = nullptr;
solenberg3a941542015-11-16 07:34:50 -08001255 webrtc::AudioSendStream::Config config_;
1256 // The stream is owned by WebRtcAudioSendStream and may be reallocated if
1257 // configuration changes.
solenbergc96df772015-10-21 13:01:53 -07001258 webrtc::AudioSendStream* stream_ = nullptr;
mallinath@webrtc.org67ee6b92014-02-03 16:57:16 +00001259
1260 // Raw pointer to AudioRenderer owned by LocalAudioTrackHandler.
1261 // PeerConnection will make sure invalidating the pointer before the object
1262 // goes away.
solenbergc96df772015-10-21 13:01:53 -07001263 AudioRenderer* renderer_ = nullptr;
henrike@webrtc.orga7b98182014-02-21 15:51:43 +00001264
solenbergc96df772015-10-21 13:01:53 -07001265 RTC_DISALLOW_IMPLICIT_CONSTRUCTORS(WebRtcAudioSendStream);
1266};
1267
1268class WebRtcVoiceMediaChannel::WebRtcAudioReceiveStream {
1269 public:
stefanba4c0e42016-02-04 04:12:24 -08001270 WebRtcAudioReceiveStream(int ch,
1271 uint32_t remote_ssrc,
1272 uint32_t local_ssrc,
1273 bool use_transport_cc,
1274 const std::string& sync_group,
solenberg7add0582015-11-20 09:59:34 -08001275 const std::vector<webrtc::RtpExtension>& extensions,
1276 webrtc::Call* call)
stefanba4c0e42016-02-04 04:12:24 -08001277 : call_(call), config_() {
solenberg7add0582015-11-20 09:59:34 -08001278 RTC_DCHECK_GE(ch, 0);
1279 RTC_DCHECK(call);
1280 config_.rtp.remote_ssrc = remote_ssrc;
1281 config_.rtp.local_ssrc = local_ssrc;
1282 config_.voe_channel_id = ch;
1283 config_.sync_group = sync_group;
stefanba4c0e42016-02-04 04:12:24 -08001284 RecreateAudioReceiveStream(use_transport_cc, extensions);
solenberg7add0582015-11-20 09:59:34 -08001285 }
solenbergc96df772015-10-21 13:01:53 -07001286
solenberg7add0582015-11-20 09:59:34 -08001287 ~WebRtcAudioReceiveStream() {
1288 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
1289 call_->DestroyAudioReceiveStream(stream_);
1290 }
1291
1292 void RecreateAudioReceiveStream(
1293 const std::vector<webrtc::RtpExtension>& extensions) {
1294 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
stefanba4c0e42016-02-04 04:12:24 -08001295 RecreateAudioReceiveStream(config_.rtp.transport_cc, extensions);
solenberg7add0582015-11-20 09:59:34 -08001296 }
stefanba4c0e42016-02-04 04:12:24 -08001297 void RecreateAudioReceiveStream(bool use_transport_cc) {
solenberg7add0582015-11-20 09:59:34 -08001298 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
stefanba4c0e42016-02-04 04:12:24 -08001299 RecreateAudioReceiveStream(use_transport_cc, config_.rtp.extensions);
solenberg7add0582015-11-20 09:59:34 -08001300 }
1301
1302 webrtc::AudioReceiveStream::Stats GetStats() const {
1303 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
1304 RTC_DCHECK(stream_);
1305 return stream_->GetStats();
1306 }
1307
1308 int channel() const {
1309 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
1310 return config_.voe_channel_id;
1311 }
solenbergc96df772015-10-21 13:01:53 -07001312
kwiberg686a8ef2016-02-26 03:00:35 -08001313 void SetRawAudioSink(std::unique_ptr<webrtc::AudioSinkInterface> sink) {
Tommif888bb52015-12-12 01:37:01 +01001314 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
kwiberg686a8ef2016-02-26 03:00:35 -08001315 stream_->SetSink(std::move(sink));
Tommif888bb52015-12-12 01:37:01 +01001316 }
1317
solenbergc96df772015-10-21 13:01:53 -07001318 private:
stefanba4c0e42016-02-04 04:12:24 -08001319 void RecreateAudioReceiveStream(
1320 bool use_transport_cc,
solenberg7add0582015-11-20 09:59:34 -08001321 const std::vector<webrtc::RtpExtension>& extensions) {
1322 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
1323 if (stream_) {
1324 call_->DestroyAudioReceiveStream(stream_);
1325 stream_ = nullptr;
1326 }
1327 config_.rtp.extensions = extensions;
stefanba4c0e42016-02-04 04:12:24 -08001328 config_.rtp.transport_cc = use_transport_cc;
solenberg7add0582015-11-20 09:59:34 -08001329 RTC_DCHECK(!stream_);
1330 stream_ = call_->CreateAudioReceiveStream(config_);
1331 RTC_CHECK(stream_);
1332 }
1333
1334 rtc::ThreadChecker worker_thread_checker_;
1335 webrtc::Call* call_ = nullptr;
1336 webrtc::AudioReceiveStream::Config config_;
1337 // The stream is owned by WebRtcAudioReceiveStream and may be reallocated if
1338 // configuration changes.
1339 webrtc::AudioReceiveStream* stream_ = nullptr;
solenbergc96df772015-10-21 13:01:53 -07001340
1341 RTC_DISALLOW_IMPLICIT_CONSTRUCTORS(WebRtcAudioReceiveStream);
henrike@webrtc.org1e09a712013-07-26 19:17:59 +00001342};
1343
Fredrik Solenberg709ed672015-09-15 12:26:33 +02001344WebRtcVoiceMediaChannel::WebRtcVoiceMediaChannel(WebRtcVoiceEngine* engine,
nisse51542be2016-02-12 02:27:06 -08001345 const MediaConfig& config,
Fredrik Solenbergb071a192015-09-17 16:42:56 +02001346 const AudioOptions& options,
Fredrik Solenberg709ed672015-09-15 12:26:33 +02001347 webrtc::Call* call)
nisse51542be2016-02-12 02:27:06 -08001348 : VoiceMediaChannel(config), engine_(engine), call_(call) {
solenberg0a617e22015-10-20 15:49:38 -07001349 LOG(LS_VERBOSE) << "WebRtcVoiceMediaChannel::WebRtcVoiceMediaChannel";
solenberg566ef242015-11-06 15:34:49 -08001350 RTC_DCHECK(call);
solenberg0a617e22015-10-20 15:49:38 -07001351 engine->RegisterChannel(this);
Fredrik Solenbergb071a192015-09-17 16:42:56 +02001352 SetOptions(options);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001353}
1354
1355WebRtcVoiceMediaChannel::~WebRtcVoiceMediaChannel() {
solenberg566ef242015-11-06 15:34:49 -08001356 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
solenberg0a617e22015-10-20 15:49:38 -07001357 LOG(LS_VERBOSE) << "WebRtcVoiceMediaChannel::~WebRtcVoiceMediaChannel";
solenberg7add0582015-11-20 09:59:34 -08001358 // TODO(solenberg): Should be able to delete the streams directly, without
1359 // going through RemoveNnStream(), once stream objects handle
1360 // all (de)configuration.
solenbergc96df772015-10-21 13:01:53 -07001361 while (!send_streams_.empty()) {
1362 RemoveSendStream(send_streams_.begin()->first);
solenbergd97ec302015-10-07 01:40:33 -07001363 }
solenberg7add0582015-11-20 09:59:34 -08001364 while (!recv_streams_.empty()) {
1365 RemoveRecvStream(recv_streams_.begin()->first);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001366 }
solenberg0a617e22015-10-20 15:49:38 -07001367 engine()->UnregisterChannel(this);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001368}
1369
nisse51542be2016-02-12 02:27:06 -08001370rtc::DiffServCodePoint WebRtcVoiceMediaChannel::PreferredDscp() const {
1371 return kAudioDscpValue;
1372}
1373
Peter Thatcherc2ee2c82015-08-07 16:05:34 -07001374bool WebRtcVoiceMediaChannel::SetSendParameters(
1375 const AudioSendParameters& params) {
solenberg566ef242015-11-06 15:34:49 -08001376 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
solenberg7e4e01a2015-12-02 08:05:01 -08001377 LOG(LS_INFO) << "WebRtcVoiceMediaChannel::SetSendParameters: "
1378 << params.ToString();
Peter Thatcherc2ee2c82015-08-07 16:05:34 -07001379 // TODO(pthatcher): Refactor this to be more clean now that we have
1380 // all the information at once.
solenberg3a941542015-11-16 07:34:50 -08001381
1382 if (!SetSendCodecs(params.codecs)) {
1383 return false;
1384 }
1385
solenberg7e4e01a2015-12-02 08:05:01 -08001386 if (!ValidateRtpExtensions(params.extensions)) {
1387 return false;
1388 }
1389 std::vector<webrtc::RtpExtension> filtered_extensions =
1390 FilterRtpExtensions(params.extensions,
1391 webrtc::RtpExtension::IsSupportedForAudio, true);
1392 if (send_rtp_extensions_ != filtered_extensions) {
1393 send_rtp_extensions_.swap(filtered_extensions);
solenberg3a941542015-11-16 07:34:50 -08001394 for (auto& it : send_streams_) {
1395 it.second->RecreateAudioSendStream(send_rtp_extensions_);
1396 }
1397 }
1398
1399 if (!SetMaxSendBandwidth(params.max_bandwidth_bps)) {
1400 return false;
1401 }
1402 return SetOptions(params.options);
Peter Thatcherc2ee2c82015-08-07 16:05:34 -07001403}
1404
1405bool WebRtcVoiceMediaChannel::SetRecvParameters(
1406 const AudioRecvParameters& params) {
solenberg566ef242015-11-06 15:34:49 -08001407 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
solenberg7e4e01a2015-12-02 08:05:01 -08001408 LOG(LS_INFO) << "WebRtcVoiceMediaChannel::SetRecvParameters: "
1409 << params.ToString();
Peter Thatcherc2ee2c82015-08-07 16:05:34 -07001410 // TODO(pthatcher): Refactor this to be more clean now that we have
1411 // all the information at once.
solenberg7add0582015-11-20 09:59:34 -08001412
1413 if (!SetRecvCodecs(params.codecs)) {
1414 return false;
1415 }
1416
solenberg7e4e01a2015-12-02 08:05:01 -08001417 if (!ValidateRtpExtensions(params.extensions)) {
1418 return false;
1419 }
1420 std::vector<webrtc::RtpExtension> filtered_extensions =
1421 FilterRtpExtensions(params.extensions,
1422 webrtc::RtpExtension::IsSupportedForAudio, false);
1423 if (recv_rtp_extensions_ != filtered_extensions) {
1424 recv_rtp_extensions_.swap(filtered_extensions);
solenberg7add0582015-11-20 09:59:34 -08001425 for (auto& it : recv_streams_) {
1426 it.second->RecreateAudioReceiveStream(recv_rtp_extensions_);
1427 }
1428 }
solenberg7add0582015-11-20 09:59:34 -08001429 return true;
Peter Thatcherc2ee2c82015-08-07 16:05:34 -07001430}
1431
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001432bool WebRtcVoiceMediaChannel::SetOptions(const AudioOptions& options) {
solenberg566ef242015-11-06 15:34:49 -08001433 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001434 LOG(LS_INFO) << "Setting voice channel options: "
1435 << options.ToString();
1436
1437 // We retain all of the existing options, and apply the given ones
1438 // on top. This means there is no way to "clear" options such that
1439 // they go back to the engine default.
1440 options_.SetAll(options);
solenberg246b8172015-12-08 09:50:23 -08001441 if (!engine()->ApplyOptions(options_)) {
1442 LOG(LS_WARNING) <<
1443 "Failed to apply engine options during channel SetOptions.";
1444 return false;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001445 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001446 LOG(LS_INFO) << "Set voice channel options. Current options: "
1447 << options_.ToString();
1448 return true;
1449}
1450
1451bool WebRtcVoiceMediaChannel::SetRecvCodecs(
1452 const std::vector<AudioCodec>& codecs) {
solenberg566ef242015-11-06 15:34:49 -08001453 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
solenberg8fb30c32015-10-13 03:06:58 -07001454
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001455 // Set the payload types to be used for incoming media.
solenberg0b675462015-10-09 01:37:09 -07001456 LOG(LS_INFO) << "Setting receive voice codecs.";
solenberg0b675462015-10-09 01:37:09 -07001457
1458 if (!VerifyUniquePayloadTypes(codecs)) {
1459 LOG(LS_ERROR) << "Codec payload types overlap.";
1460 return false;
1461 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001462
1463 std::vector<AudioCodec> new_codecs;
1464 // Find all new codecs. We allow adding new codecs but don't allow changing
1465 // the payload type of codecs that is already configured since we might
1466 // already be receiving packets with that payload type.
Fredrik Solenbergaf9fb212015-08-26 10:45:53 +02001467 for (const AudioCodec& codec : codecs) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001468 AudioCodec old_codec;
Fredrik Solenbergaf9fb212015-08-26 10:45:53 +02001469 if (FindCodec(recv_codecs_, codec, &old_codec)) {
1470 if (old_codec.id != codec.id) {
1471 LOG(LS_ERROR) << codec.name << " payload type changed.";
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001472 return false;
1473 }
1474 } else {
Fredrik Solenbergaf9fb212015-08-26 10:45:53 +02001475 new_codecs.push_back(codec);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001476 }
1477 }
1478 if (new_codecs.empty()) {
1479 // There are no new codecs to configure. Already configured codecs are
1480 // never removed.
1481 return true;
1482 }
1483
1484 if (playout_) {
1485 // Receive codecs can not be changed while playing. So we temporarily
1486 // pause playout.
1487 PausePlayout();
1488 }
1489
solenberg26c8c912015-11-27 04:00:25 -08001490 bool result = true;
1491 for (const AudioCodec& codec : new_codecs) {
1492 webrtc::CodecInst voe_codec;
1493 if (WebRtcVoiceEngine::ToCodecInst(codec, &voe_codec)) {
1494 LOG(LS_INFO) << ToString(codec);
1495 voe_codec.pltype = codec.id;
1496 for (const auto& ch : recv_streams_) {
1497 if (engine()->voe()->codec()->SetRecPayloadType(
1498 ch.second->channel(), voe_codec) == -1) {
1499 LOG_RTCERR2(SetRecPayloadType, ch.second->channel(),
1500 ToString(voe_codec));
1501 result = false;
1502 }
1503 }
1504 } else {
1505 LOG(LS_WARNING) << "Unknown codec " << ToString(codec);
1506 result = false;
1507 break;
1508 }
1509 }
Fredrik Solenbergaf9fb212015-08-26 10:45:53 +02001510 if (result) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001511 recv_codecs_ = codecs;
1512 }
1513
1514 if (desired_playout_ && !playout_) {
1515 ResumePlayout();
1516 }
Fredrik Solenbergaf9fb212015-08-26 10:45:53 +02001517 return result;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001518}
1519
1520bool WebRtcVoiceMediaChannel::SetSendCodecs(
wu@webrtc.orgcadf9042013-08-30 21:24:16 +00001521 int channel, const std::vector<AudioCodec>& codecs) {
buildbot@webrtc.orgae740dd2014-06-17 10:56:41 +00001522 // Disable VAD, FEC, and RED unless we know the other side wants them.
wu@webrtc.orgcadf9042013-08-30 21:24:16 +00001523 engine()->voe()->codec()->SetVADStatus(channel, false);
1524 engine()->voe()->rtp()->SetNACKStatus(channel, false, 0);
buildbot@webrtc.orgae740dd2014-06-17 10:56:41 +00001525 engine()->voe()->rtp()->SetREDStatus(channel, false);
1526 engine()->voe()->codec()->SetFECStatus(channel, false);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001527
1528 // Scan through the list to figure out the codec to use for sending, along
Fredrik Solenbergb5727682015-12-04 15:22:19 +01001529 // with the proper configuration for VAD.
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001530 webrtc::CodecInst send_codec;
1531 memset(&send_codec, 0, sizeof(send_codec));
1532
wu@webrtc.org05e7b442014-04-01 17:44:24 +00001533 bool nack_enabled = nack_enabled_;
buildbot@webrtc.org3ffa1f92014-07-02 19:51:26 +00001534 bool enable_codec_fec = false;
Minyue Li7100dcd2015-03-27 05:05:59 +01001535 bool enable_opus_dtx = false;
minyue@webrtc.org26236952014-10-29 02:27:08 +00001536 int opus_max_playback_rate = 0;
stefanba4c0e42016-02-04 04:12:24 -08001537 int red_payload_type = -1;
buildbot@webrtc.org5d639b32014-09-10 07:57:12 +00001538
henrike@webrtc.org704bf9e2014-02-27 17:52:04 +00001539 // Set send codec (the first non-telephone-event/CN codec)
stefanba4c0e42016-02-04 04:12:24 -08001540 const AudioCodec* codec = WebRtcVoiceCodecs::GetPreferredCodec(
1541 codecs, &send_codec, &red_payload_type);
1542 if (codec) {
1543 if (red_payload_type != -1) {
henrike@webrtc.org704bf9e2014-02-27 17:52:04 +00001544 // Enable redundant encoding of the specified codec. Treat any
1545 // failure as a fatal internal error.
buildbot@webrtc.orgae740dd2014-06-17 10:56:41 +00001546 LOG(LS_INFO) << "Enabling RED on channel " << channel;
stefanba4c0e42016-02-04 04:12:24 -08001547 if (engine()->voe()->rtp()->SetREDStatus(channel, true,
1548 red_payload_type) == -1) {
1549 LOG_RTCERR3(SetREDStatus, channel, true, red_payload_type);
henrike@webrtc.org704bf9e2014-02-27 17:52:04 +00001550 return false;
1551 }
1552 } else {
stefanba4c0e42016-02-04 04:12:24 -08001553 nack_enabled = HasNack(*codec);
Minyue Li7100dcd2015-03-27 05:05:59 +01001554 // For Opus as the send codec, we are to determine inband FEC, maximum
1555 // playback rate, and opus internal dtx.
stefanba4c0e42016-02-04 04:12:24 -08001556 if (IsCodec(*codec, kOpusCodecName)) {
1557 GetOpusConfig(*codec, &send_codec, &enable_codec_fec,
Minyue Li7100dcd2015-03-27 05:05:59 +01001558 &opus_max_playback_rate, &enable_opus_dtx);
buildbot@webrtc.org5d639b32014-09-10 07:57:12 +00001559 }
Brave Yao5225dd82015-03-26 07:39:19 +08001560
1561 // Set packet size if the AudioCodec param kCodecParamPTime is set.
1562 int ptime_ms = 0;
stefanba4c0e42016-02-04 04:12:24 -08001563 if (codec->GetParam(kCodecParamPTime, &ptime_ms)) {
solenberg26c8c912015-11-27 04:00:25 -08001564 if (!WebRtcVoiceCodecs::SetPTimeAsPacketSize(&send_codec, ptime_ms)) {
Brave Yao5225dd82015-03-26 07:39:19 +08001565 LOG(LS_WARNING) << "Failed to set packet size for codec "
1566 << send_codec.plname;
1567 return false;
1568 }
1569 }
henrike@webrtc.org704bf9e2014-02-27 17:52:04 +00001570 }
henrike@webrtc.org704bf9e2014-02-27 17:52:04 +00001571 }
1572
wu@webrtc.org05e7b442014-04-01 17:44:24 +00001573 if (nack_enabled_ != nack_enabled) {
1574 SetNack(channel, nack_enabled);
1575 nack_enabled_ = nack_enabled;
1576 }
stefanba4c0e42016-02-04 04:12:24 -08001577 if (!codec) {
henrike@webrtc.org704bf9e2014-02-27 17:52:04 +00001578 LOG(LS_WARNING) << "Received empty list of codecs.";
1579 return false;
1580 }
1581
1582 // Set the codec immediately, since SetVADStatus() depends on whether
1583 // the current codec is mono or stereo.
1584 if (!SetSendCodec(channel, send_codec))
1585 return false;
1586
buildbot@webrtc.org3ffa1f92014-07-02 19:51:26 +00001587 // FEC should be enabled after SetSendCodec.
1588 if (enable_codec_fec) {
1589 LOG(LS_INFO) << "Attempt to enable codec internal FEC on channel "
1590 << channel;
buildbot@webrtc.org3ffa1f92014-07-02 19:51:26 +00001591 if (engine()->voe()->codec()->SetFECStatus(channel, true) == -1) {
1592 // Enable codec internal FEC. Treat any failure as fatal internal error.
1593 LOG_RTCERR2(SetFECStatus, channel, true);
1594 return false;
1595 }
buildbot@webrtc.org3ffa1f92014-07-02 19:51:26 +00001596 }
1597
Minyue Li7100dcd2015-03-27 05:05:59 +01001598 if (IsCodec(send_codec, kOpusCodecName)) {
1599 // DTX and maxplaybackrate should be set after SetSendCodec. Because current
1600 // send codec has to be Opus.
1601
1602 // Set Opus internal DTX.
1603 LOG(LS_INFO) << "Attempt to "
solenbergbd138382015-11-20 16:08:07 -08001604 << (enable_opus_dtx ? "enable" : "disable")
Minyue Li7100dcd2015-03-27 05:05:59 +01001605 << " Opus DTX on channel "
buildbot@webrtc.org5d639b32014-09-10 07:57:12 +00001606 << channel;
Minyue Li7100dcd2015-03-27 05:05:59 +01001607 if (engine()->voe()->codec()->SetOpusDtx(channel, enable_opus_dtx)) {
1608 LOG_RTCERR2(SetOpusDtx, channel, enable_opus_dtx);
1609 return false;
1610 }
1611
1612 // If opus_max_playback_rate <= 0, the default maximum playback rate
1613 // (48 kHz) will be used.
1614 if (opus_max_playback_rate > 0) {
1615 LOG(LS_INFO) << "Attempt to set maximum playback rate to "
1616 << opus_max_playback_rate
1617 << " Hz on channel "
1618 << channel;
1619 if (engine()->voe()->codec()->SetOpusMaxPlaybackRate(
1620 channel, opus_max_playback_rate) == -1) {
1621 LOG_RTCERR2(SetOpusMaxPlaybackRate, channel, opus_max_playback_rate);
1622 return false;
1623 }
buildbot@webrtc.org5d639b32014-09-10 07:57:12 +00001624 }
buildbot@webrtc.org5d639b32014-09-10 07:57:12 +00001625 }
1626
henrike@webrtc.org704bf9e2014-02-27 17:52:04 +00001627 // Always update the |send_codec_| to the currently set send codec.
1628 send_codec_.reset(new webrtc::CodecInst(send_codec));
1629
minyue@webrtc.org26236952014-10-29 02:27:08 +00001630 if (send_bitrate_setting_) {
1631 SetSendBitrateInternal(send_bitrate_bps_);
henrike@webrtc.org704bf9e2014-02-27 17:52:04 +00001632 }
1633
Fredrik Solenbergb5727682015-12-04 15:22:19 +01001634 // Loop through the codecs list again to config the CN codec.
Fredrik Solenbergaf9fb212015-08-26 10:45:53 +02001635 for (const AudioCodec& codec : codecs) {
henrike@webrtc.org704bf9e2014-02-27 17:52:04 +00001636 // Ignore codecs we don't know about. The negotiation step should prevent
1637 // this, but double-check to be sure.
1638 webrtc::CodecInst voe_codec;
solenberg26c8c912015-11-27 04:00:25 -08001639 if (!WebRtcVoiceEngine::ToCodecInst(codec, &voe_codec)) {
Fredrik Solenbergaf9fb212015-08-26 10:45:53 +02001640 LOG(LS_WARNING) << "Unknown codec " << ToString(codec);
henrike@webrtc.org704bf9e2014-02-27 17:52:04 +00001641 continue;
1642 }
1643
Fredrik Solenbergb5727682015-12-04 15:22:19 +01001644 if (IsCodec(codec, kCnCodecName)) {
henrike@webrtc.org704bf9e2014-02-27 17:52:04 +00001645 // Turn voice activity detection/comfort noise on if supported.
1646 // Set the wideband CN payload type appropriately.
1647 // (narrowband always uses the static payload type 13).
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001648 webrtc::PayloadFrequencies cn_freq;
Fredrik Solenbergaf9fb212015-08-26 10:45:53 +02001649 switch (codec.clockrate) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001650 case 8000:
1651 cn_freq = webrtc::kFreq8000Hz;
1652 break;
1653 case 16000:
1654 cn_freq = webrtc::kFreq16000Hz;
1655 break;
1656 case 32000:
1657 cn_freq = webrtc::kFreq32000Hz;
1658 break;
1659 default:
Fredrik Solenbergaf9fb212015-08-26 10:45:53 +02001660 LOG(LS_WARNING) << "CN frequency " << codec.clockrate
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001661 << " not supported.";
1662 continue;
1663 }
wu@webrtc.orgcadf9042013-08-30 21:24:16 +00001664 // Set the CN payloadtype and the VAD status.
1665 // The CN payload type for 8000 Hz clockrate is fixed at 13.
1666 if (cn_freq != webrtc::kFreq8000Hz) {
1667 if (engine()->voe()->codec()->SetSendCNPayloadType(
Fredrik Solenbergaf9fb212015-08-26 10:45:53 +02001668 channel, codec.id, cn_freq) == -1) {
1669 LOG_RTCERR3(SetSendCNPayloadType, channel, codec.id, cn_freq);
wu@webrtc.orgcadf9042013-08-30 21:24:16 +00001670 // TODO(ajm): This failure condition will be removed from VoE.
1671 // Restore the return here when we update to a new enough webrtc.
1672 //
1673 // Not returning false because the SetSendCNPayloadType will fail if
1674 // the channel is already sending.
1675 // This can happen if the remote description is applied twice, for
1676 // example in the case of ROAP on top of JSEP, where both side will
1677 // send the offer.
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001678 }
wu@webrtc.orgcadf9042013-08-30 21:24:16 +00001679 }
wu@webrtc.orgcadf9042013-08-30 21:24:16 +00001680 // Only turn on VAD if we have a CN payload type that matches the
1681 // clockrate for the codec we are going to use.
Fredrik Solenbergaf9fb212015-08-26 10:45:53 +02001682 if (codec.clockrate == send_codec.plfreq && send_codec.channels != 2) {
Minyue Li7100dcd2015-03-27 05:05:59 +01001683 // TODO(minyue): If CN frequency == 48000 Hz is allowed, consider the
1684 // interaction between VAD and Opus FEC.
wu@webrtc.orgcadf9042013-08-30 21:24:16 +00001685 LOG(LS_INFO) << "Enabling VAD";
1686 if (engine()->voe()->codec()->SetVADStatus(channel, true) == -1) {
1687 LOG_RTCERR2(SetVADStatus, channel, true);
1688 return false;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001689 }
1690 }
1691 }
wu@webrtc.org1d1ffc92013-10-16 18:12:02 +00001692 }
wu@webrtc.orgcadf9042013-08-30 21:24:16 +00001693 return true;
1694}
1695
1696bool WebRtcVoiceMediaChannel::SetSendCodecs(
1697 const std::vector<AudioCodec>& codecs) {
solenberg566ef242015-11-06 15:34:49 -08001698 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
Fredrik Solenbergb5727682015-12-04 15:22:19 +01001699 // TODO(solenberg): Validate input - that payload types don't overlap, are
1700 // within range, filter out codecs we don't support,
1701 // redundant codecs etc.
solenbergd97ec302015-10-07 01:40:33 -07001702
Fredrik Solenbergb5727682015-12-04 15:22:19 +01001703 // Find the DTMF telephone event "codec" payload type.
1704 dtmf_payload_type_ = rtc::Optional<int>();
Fredrik Solenbergaf9fb212015-08-26 10:45:53 +02001705 for (const AudioCodec& codec : codecs) {
Fredrik Solenbergaf9fb212015-08-26 10:45:53 +02001706 if (IsCodec(codec, kDtmfCodecName)) {
Fredrik Solenbergb5727682015-12-04 15:22:19 +01001707 dtmf_payload_type_ = rtc::Optional<int>(codec.id);
1708 break;
wu@webrtc.orgcadf9042013-08-30 21:24:16 +00001709 }
1710 }
1711
1712 // Cache the codecs in order to configure the channel created later.
1713 send_codecs_ = codecs;
solenbergc96df772015-10-21 13:01:53 -07001714 for (const auto& ch : send_streams_) {
Fredrik Solenbergaf9fb212015-08-26 10:45:53 +02001715 if (!SetSendCodecs(ch.second->channel(), codecs)) {
wu@webrtc.orgcadf9042013-08-30 21:24:16 +00001716 return false;
1717 }
1718 }
1719
wu@webrtc.org05e7b442014-04-01 17:44:24 +00001720 // Set nack status on receive channels and update |nack_enabled_|.
solenberg7add0582015-11-20 09:59:34 -08001721 for (const auto& ch : recv_streams_) {
solenberg0a617e22015-10-20 15:49:38 -07001722 SetNack(ch.second->channel(), nack_enabled_);
wu@webrtc.org9dba5252013-08-05 20:36:57 +00001723 }
solenberg0a617e22015-10-20 15:49:38 -07001724
stefanba4c0e42016-02-04 04:12:24 -08001725 // Check if the transport cc feedback has changed on the preferred send codec,
1726 // and in that case reconfigure all receive streams.
1727 webrtc::CodecInst voe_codec;
1728 int red_payload_type;
1729 const AudioCodec* send_codec = WebRtcVoiceCodecs::GetPreferredCodec(
1730 send_codecs_, &voe_codec, &red_payload_type);
1731 if (send_codec) {
1732 bool transport_cc = HasTransportCc(*send_codec);
1733 if (transport_cc_enabled_ != transport_cc) {
1734 LOG(LS_INFO) << "Recreate all the receive streams because the send "
1735 "codec has changed.";
1736 transport_cc_enabled_ = transport_cc;
1737 for (auto& kv : recv_streams_) {
1738 RTC_DCHECK(kv.second != nullptr);
1739 kv.second->RecreateAudioReceiveStream(transport_cc_enabled_);
1740 }
1741 }
1742 }
1743
solenberg0a617e22015-10-20 15:49:38 -07001744 return true;
wu@webrtc.org9dba5252013-08-05 20:36:57 +00001745}
1746
wu@webrtc.orgcadf9042013-08-30 21:24:16 +00001747void WebRtcVoiceMediaChannel::SetNack(int channel, bool nack_enabled) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001748 if (nack_enabled) {
wu@webrtc.orgcadf9042013-08-30 21:24:16 +00001749 LOG(LS_INFO) << "Enabling NACK for channel " << channel;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001750 engine()->voe()->rtp()->SetNACKStatus(channel, true, kNackMaxPackets);
1751 } else {
wu@webrtc.orgcadf9042013-08-30 21:24:16 +00001752 LOG(LS_INFO) << "Disabling NACK for channel " << channel;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001753 engine()->voe()->rtp()->SetNACKStatus(channel, false, 0);
1754 }
1755}
1756
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001757bool WebRtcVoiceMediaChannel::SetSendCodec(
wu@webrtc.org9dba5252013-08-05 20:36:57 +00001758 int channel, const webrtc::CodecInst& send_codec) {
1759 LOG(LS_INFO) << "Send channel " << channel << " selected voice codec "
1760 << ToString(send_codec) << ", bitrate=" << send_codec.rate;
1761
wu@webrtc.org05e7b442014-04-01 17:44:24 +00001762 webrtc::CodecInst current_codec;
1763 if (engine()->voe()->codec()->GetSendCodec(channel, current_codec) == 0 &&
1764 (send_codec == current_codec)) {
1765 // Codec is already configured, we can return without setting it again.
1766 return true;
1767 }
1768
wu@webrtc.org9dba5252013-08-05 20:36:57 +00001769 if (engine()->voe()->codec()->SetSendCodec(channel, send_codec) == -1) {
1770 LOG_RTCERR2(SetSendCodec, channel, ToString(send_codec));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001771 return false;
1772 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001773 return true;
1774}
1775
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001776bool WebRtcVoiceMediaChannel::SetPlayout(bool playout) {
1777 desired_playout_ = playout;
1778 return ChangePlayout(desired_playout_);
1779}
1780
1781bool WebRtcVoiceMediaChannel::PausePlayout() {
1782 return ChangePlayout(false);
1783}
1784
1785bool WebRtcVoiceMediaChannel::ResumePlayout() {
1786 return ChangePlayout(desired_playout_);
1787}
1788
1789bool WebRtcVoiceMediaChannel::ChangePlayout(bool playout) {
solenberg566ef242015-11-06 15:34:49 -08001790 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001791 if (playout_ == playout) {
1792 return true;
1793 }
1794
solenberg7add0582015-11-20 09:59:34 -08001795 for (const auto& ch : recv_streams_) {
Fredrik Solenbergaf9fb212015-08-26 10:45:53 +02001796 if (!SetPlayout(ch.second->channel(), playout)) {
henrike@webrtc.org1e09a712013-07-26 19:17:59 +00001797 LOG(LS_ERROR) << "SetPlayout " << playout << " on channel "
Fredrik Solenbergaf9fb212015-08-26 10:45:53 +02001798 << ch.second->channel() << " failed";
solenberg1ac56142015-10-13 03:58:19 -07001799 return false;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001800 }
1801 }
solenberg1ac56142015-10-13 03:58:19 -07001802 playout_ = playout;
1803 return true;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001804}
1805
1806bool WebRtcVoiceMediaChannel::SetSend(SendFlags send) {
1807 desired_send_ = send;
solenbergc96df772015-10-21 13:01:53 -07001808 if (!send_streams_.empty()) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001809 return ChangeSend(desired_send_);
solenbergc96df772015-10-21 13:01:53 -07001810 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001811 return true;
1812}
1813
1814bool WebRtcVoiceMediaChannel::PauseSend() {
1815 return ChangeSend(SEND_NOTHING);
1816}
1817
1818bool WebRtcVoiceMediaChannel::ResumeSend() {
1819 return ChangeSend(desired_send_);
1820}
1821
1822bool WebRtcVoiceMediaChannel::ChangeSend(SendFlags send) {
1823 if (send_ == send) {
1824 return true;
1825 }
1826
solenberg246b8172015-12-08 09:50:23 -08001827 // Apply channel specific options when channel is enabled for sending.
solenberg63b34542015-09-29 06:06:31 -07001828 if (send == SEND_MICROPHONE) {
1829 engine()->ApplyOptions(options_);
1830 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001831
wu@webrtc.org9dba5252013-08-05 20:36:57 +00001832 // Change the settings on each send channel.
solenbergc96df772015-10-21 13:01:53 -07001833 for (const auto& ch : send_streams_) {
solenberg63b34542015-09-29 06:06:31 -07001834 if (!ChangeSend(ch.second->channel(), send)) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001835 return false;
solenberg63b34542015-09-29 06:06:31 -07001836 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001837 }
wu@webrtc.org9dba5252013-08-05 20:36:57 +00001838
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001839 send_ = send;
1840 return true;
1841}
1842
wu@webrtc.org9dba5252013-08-05 20:36:57 +00001843bool WebRtcVoiceMediaChannel::ChangeSend(int channel, SendFlags send) {
1844 if (send == SEND_MICROPHONE) {
1845 if (engine()->voe()->base()->StartSend(channel) == -1) {
1846 LOG_RTCERR1(StartSend, channel);
1847 return false;
1848 }
wu@webrtc.org9dba5252013-08-05 20:36:57 +00001849 } else { // SEND_NOTHING
henrikg91d6ede2015-09-17 00:24:34 -07001850 RTC_DCHECK(send == SEND_NOTHING);
wu@webrtc.org9dba5252013-08-05 20:36:57 +00001851 if (engine()->voe()->base()->StopSend(channel) == -1) {
1852 LOG_RTCERR1(StopSend, channel);
1853 return false;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001854 }
1855 }
1856
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001857 return true;
1858}
1859
Peter Boström0c4e06b2015-10-07 12:23:21 +02001860bool WebRtcVoiceMediaChannel::SetAudioSend(uint32_t ssrc,
1861 bool enable,
solenberg1dd98f32015-09-10 01:57:14 -07001862 const AudioOptions* options,
1863 AudioRenderer* renderer) {
solenberg566ef242015-11-06 15:34:49 -08001864 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
solenberg1dd98f32015-09-10 01:57:14 -07001865 // TODO(solenberg): The state change should be fully rolled back if any one of
1866 // these calls fail.
1867 if (!SetLocalRenderer(ssrc, renderer)) {
1868 return false;
1869 }
solenbergdfc8f4f2015-10-01 02:31:10 -07001870 if (!MuteStream(ssrc, !enable)) {
solenberg1dd98f32015-09-10 01:57:14 -07001871 return false;
1872 }
solenbergdfc8f4f2015-10-01 02:31:10 -07001873 if (enable && options) {
solenberg1dd98f32015-09-10 01:57:14 -07001874 return SetOptions(*options);
1875 }
1876 return true;
1877}
1878
solenberg0a617e22015-10-20 15:49:38 -07001879int WebRtcVoiceMediaChannel::CreateVoEChannel() {
1880 int id = engine()->CreateVoEChannel();
1881 if (id == -1) {
1882 LOG_RTCERR0(CreateVoEChannel);
1883 return -1;
wu@webrtc.org9dba5252013-08-05 20:36:57 +00001884 }
solenberg0a617e22015-10-20 15:49:38 -07001885 if (engine()->voe()->network()->RegisterExternalTransport(id, *this) == -1) {
1886 LOG_RTCERR2(RegisterExternalTransport, id, this);
1887 engine()->voe()->base()->DeleteChannel(id);
1888 return -1;
1889 }
1890 return id;
wu@webrtc.org9dba5252013-08-05 20:36:57 +00001891}
1892
solenberg7add0582015-11-20 09:59:34 -08001893bool WebRtcVoiceMediaChannel::DeleteVoEChannel(int channel) {
wu@webrtc.org9dba5252013-08-05 20:36:57 +00001894 if (engine()->voe()->network()->DeRegisterExternalTransport(channel) == -1) {
1895 LOG_RTCERR1(DeRegisterExternalTransport, channel);
1896 }
wu@webrtc.org9dba5252013-08-05 20:36:57 +00001897 if (engine()->voe()->base()->DeleteChannel(channel) == -1) {
1898 LOG_RTCERR1(DeleteChannel, channel);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001899 return false;
1900 }
wu@webrtc.org9dba5252013-08-05 20:36:57 +00001901 return true;
1902}
henrike@webrtc.org1e09a712013-07-26 19:17:59 +00001903
wu@webrtc.org9dba5252013-08-05 20:36:57 +00001904bool WebRtcVoiceMediaChannel::AddSendStream(const StreamParams& sp) {
solenberg566ef242015-11-06 15:34:49 -08001905 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
solenberg0a617e22015-10-20 15:49:38 -07001906 LOG(LS_INFO) << "AddSendStream: " << sp.ToString();
1907
1908 uint32_t ssrc = sp.first_ssrc();
1909 RTC_DCHECK(0 != ssrc);
1910
1911 if (GetSendChannelId(ssrc) != -1) {
1912 LOG(LS_ERROR) << "Stream already exists with ssrc " << ssrc;
wu@webrtc.org9dba5252013-08-05 20:36:57 +00001913 return false;
1914 }
1915
solenberg0a617e22015-10-20 15:49:38 -07001916 // Create a new channel for sending audio data.
1917 int channel = CreateVoEChannel();
1918 if (channel == -1) {
1919 return false;
wu@webrtc.org9dba5252013-08-05 20:36:57 +00001920 }
wu@webrtc.org9dba5252013-08-05 20:36:57 +00001921
solenbergc96df772015-10-21 13:01:53 -07001922 // Save the channel to send_streams_, so that RemoveSendStream() can still
wu@webrtc.org9dba5252013-08-05 20:36:57 +00001923 // delete the channel in case failure happens below.
mallinath@webrtc.org67ee6b92014-02-03 16:57:16 +00001924 webrtc::AudioTransport* audio_transport =
1925 engine()->voe()->base()->audio_transport();
solenberg3a941542015-11-16 07:34:50 -08001926 send_streams_.insert(std::make_pair(ssrc, new WebRtcAudioSendStream(
1927 channel, audio_transport, ssrc, sp.cname, send_rtp_extensions_, call_)));
wu@webrtc.org9dba5252013-08-05 20:36:57 +00001928
solenberg0a617e22015-10-20 15:49:38 -07001929 // Set the current codecs to be used for the new channel. We need to do this
1930 // after adding the channel to send_channels_, because of how max bitrate is
1931 // currently being configured by SetSendCodec().
1932 if (!send_codecs_.empty() && !SetSendCodecs(channel, send_codecs_)) {
1933 RemoveSendStream(ssrc);
wu@webrtc.org9dba5252013-08-05 20:36:57 +00001934 return false;
1935 }
1936
1937 // At this point the channel's local SSRC has been updated. If the channel is
solenberg0a617e22015-10-20 15:49:38 -07001938 // the first send channel make sure that all the receive channels are updated
1939 // with the same SSRC in order to send receiver reports.
solenbergc96df772015-10-21 13:01:53 -07001940 if (send_streams_.size() == 1) {
solenberg0a617e22015-10-20 15:49:38 -07001941 receiver_reports_ssrc_ = ssrc;
solenberg7add0582015-11-20 09:59:34 -08001942 for (const auto& stream : recv_streams_) {
1943 int recv_channel = stream.second->channel();
solenberg0a617e22015-10-20 15:49:38 -07001944 if (engine()->voe()->rtp()->SetLocalSSRC(recv_channel, ssrc) != 0) {
solenberg7add0582015-11-20 09:59:34 -08001945 LOG_RTCERR2(SetLocalSSRC, recv_channel, ssrc);
solenberg1ac56142015-10-13 03:58:19 -07001946 return false;
wu@webrtc.org9dba5252013-08-05 20:36:57 +00001947 }
solenberg0a617e22015-10-20 15:49:38 -07001948 engine()->voe()->base()->AssociateSendChannel(recv_channel, channel);
1949 LOG(LS_INFO) << "VoiceEngine channel #" << recv_channel
1950 << " is associated with channel #" << channel << ".";
wu@webrtc.org9dba5252013-08-05 20:36:57 +00001951 }
1952 }
1953
wu@webrtc.org9dba5252013-08-05 20:36:57 +00001954 return ChangeSend(channel, desired_send_);
1955}
1956
Peter Boström0c4e06b2015-10-07 12:23:21 +02001957bool WebRtcVoiceMediaChannel::RemoveSendStream(uint32_t ssrc) {
solenberg566ef242015-11-06 15:34:49 -08001958 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
solenberg3a941542015-11-16 07:34:50 -08001959 LOG(LS_INFO) << "RemoveSendStream: " << ssrc;
1960
solenbergc96df772015-10-21 13:01:53 -07001961 auto it = send_streams_.find(ssrc);
1962 if (it == send_streams_.end()) {
wu@webrtc.org9dba5252013-08-05 20:36:57 +00001963 LOG(LS_WARNING) << "Try to remove stream with ssrc " << ssrc
1964 << " which doesn't exist.";
1965 return false;
1966 }
1967
mallinath@webrtc.org67ee6b92014-02-03 16:57:16 +00001968 int channel = it->second->channel();
wu@webrtc.org9dba5252013-08-05 20:36:57 +00001969 ChangeSend(channel, SEND_NOTHING);
1970
solenberg7add0582015-11-20 09:59:34 -08001971 // Clean up and delete the send stream+channel.
solenberg0a617e22015-10-20 15:49:38 -07001972 LOG(LS_INFO) << "Removing audio send stream " << ssrc
1973 << " with VoiceEngine channel #" << channel << ".";
solenberg7add0582015-11-20 09:59:34 -08001974 delete it->second;
1975 send_streams_.erase(it);
1976 if (!DeleteVoEChannel(channel)) {
solenberg0a617e22015-10-20 15:49:38 -07001977 return false;
wu@webrtc.org9dba5252013-08-05 20:36:57 +00001978 }
solenbergc96df772015-10-21 13:01:53 -07001979 if (send_streams_.empty()) {
wu@webrtc.org9dba5252013-08-05 20:36:57 +00001980 ChangeSend(SEND_NOTHING);
solenberg0a617e22015-10-20 15:49:38 -07001981 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001982 return true;
1983}
1984
1985bool WebRtcVoiceMediaChannel::AddRecvStream(const StreamParams& sp) {
solenberg566ef242015-11-06 15:34:49 -08001986 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
solenbergd97ec302015-10-07 01:40:33 -07001987 LOG(LS_INFO) << "AddRecvStream: " << sp.ToString();
1988
solenberg0b675462015-10-09 01:37:09 -07001989 if (!ValidateStreamParams(sp)) {
wu@webrtc.org78187522013-10-07 23:32:02 +00001990 return false;
1991 }
1992
solenberg7add0582015-11-20 09:59:34 -08001993 const uint32_t ssrc = sp.first_ssrc();
solenberg0b675462015-10-09 01:37:09 -07001994 if (ssrc == 0) {
1995 LOG(LS_WARNING) << "AddRecvStream with ssrc==0 is not supported.";
1996 return false;
1997 }
1998
solenberg1ac56142015-10-13 03:58:19 -07001999 // Remove the default receive stream if one had been created with this ssrc;
2000 // we'll recreate it then.
2001 if (IsDefaultRecvStream(ssrc)) {
2002 RemoveRecvStream(ssrc);
2003 }
solenberg0b675462015-10-09 01:37:09 -07002004
solenberg7add0582015-11-20 09:59:34 -08002005 if (GetReceiveChannelId(ssrc) != -1) {
henrike@webrtc.org1e09a712013-07-26 19:17:59 +00002006 LOG(LS_ERROR) << "Stream already exists with ssrc " << ssrc;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002007 return false;
2008 }
Fredrik Solenberg4b60c732015-05-07 14:07:48 +02002009
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002010 // Create a new channel for receiving audio data.
solenberg7add0582015-11-20 09:59:34 -08002011 const int channel = CreateVoEChannel();
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002012 if (channel == -1) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002013 return false;
2014 }
Minyue2013aec2015-05-13 14:14:42 +02002015
solenberg1ac56142015-10-13 03:58:19 -07002016 // Turn off all supported codecs.
solenberg26c8c912015-11-27 04:00:25 -08002017 // TODO(solenberg): Remove once "no codecs" is the default state of a stream.
2018 for (webrtc::CodecInst voe_codec : webrtc::acm2::RentACodec::Database()) {
2019 voe_codec.pltype = -1;
2020 if (engine()->voe()->codec()->SetRecPayloadType(channel, voe_codec) == -1) {
2021 LOG_RTCERR2(SetRecPayloadType, channel, ToString(voe_codec));
2022 DeleteVoEChannel(channel);
2023 return false;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002024 }
2025 }
2026
solenberg1ac56142015-10-13 03:58:19 -07002027 // Only enable those configured for this channel.
2028 for (const auto& codec : recv_codecs_) {
2029 webrtc::CodecInst voe_codec;
solenberg26c8c912015-11-27 04:00:25 -08002030 if (WebRtcVoiceEngine::ToCodecInst(codec, &voe_codec)) {
solenberg1ac56142015-10-13 03:58:19 -07002031 voe_codec.pltype = codec.id;
2032 if (engine()->voe()->codec()->SetRecPayloadType(
2033 channel, voe_codec) == -1) {
2034 LOG_RTCERR2(SetRecPayloadType, channel, ToString(voe_codec));
solenberg7add0582015-11-20 09:59:34 -08002035 DeleteVoEChannel(channel);
solenberg1ac56142015-10-13 03:58:19 -07002036 return false;
2037 }
henrike@webrtc.org1e09a712013-07-26 19:17:59 +00002038 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002039 }
solenberg8fb30c32015-10-13 03:06:58 -07002040
solenberg7add0582015-11-20 09:59:34 -08002041 const int send_channel = GetSendChannelId(receiver_reports_ssrc_);
2042 if (send_channel != -1) {
2043 // Associate receive channel with first send channel (so the receive channel
2044 // can obtain RTT from the send channel)
2045 engine()->voe()->base()->AssociateSendChannel(channel, send_channel);
2046 LOG(LS_INFO) << "VoiceEngine channel #" << channel
2047 << " is associated with channel #" << send_channel << ".";
buildbot@webrtc.org150835e2014-05-06 15:54:38 +00002048 }
2049
stefanba4c0e42016-02-04 04:12:24 -08002050 transport_cc_enabled_ =
2051 !send_codecs_.empty() ? HasTransportCc(send_codecs_[0]) : false;
2052
2053 recv_streams_.insert(std::make_pair(
2054 ssrc, new WebRtcAudioReceiveStream(channel, ssrc, receiver_reports_ssrc_,
2055 transport_cc_enabled_, sp.sync_label,
2056 recv_rtp_extensions_, call_)));
solenberg7add0582015-11-20 09:59:34 -08002057
2058 SetNack(channel, nack_enabled_);
solenberg1ac56142015-10-13 03:58:19 -07002059 SetPlayout(channel, playout_);
solenberg7add0582015-11-20 09:59:34 -08002060
solenberg1ac56142015-10-13 03:58:19 -07002061 return true;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002062}
2063
Peter Boström0c4e06b2015-10-07 12:23:21 +02002064bool WebRtcVoiceMediaChannel::RemoveRecvStream(uint32_t ssrc) {
solenberg566ef242015-11-06 15:34:49 -08002065 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
solenbergd97ec302015-10-07 01:40:33 -07002066 LOG(LS_INFO) << "RemoveRecvStream: " << ssrc;
2067
solenberg7add0582015-11-20 09:59:34 -08002068 const auto it = recv_streams_.find(ssrc);
2069 if (it == recv_streams_.end()) {
wu@webrtc.org9dba5252013-08-05 20:36:57 +00002070 LOG(LS_WARNING) << "Try to remove stream with ssrc " << ssrc
2071 << " which doesn't exist.";
henrike@webrtc.org1e09a712013-07-26 19:17:59 +00002072 return false;
wu@webrtc.org9dba5252013-08-05 20:36:57 +00002073 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002074
solenberg1ac56142015-10-13 03:58:19 -07002075 // Deregister default channel, if that's the one being destroyed.
2076 if (IsDefaultRecvStream(ssrc)) {
2077 default_recv_ssrc_ = -1;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002078 }
henrike@webrtc.org1e09a712013-07-26 19:17:59 +00002079
solenberg7add0582015-11-20 09:59:34 -08002080 const int channel = it->second->channel();
2081
2082 // Clean up and delete the receive stream+channel.
2083 LOG(LS_INFO) << "Removing audio receive stream " << ssrc
mallinath@webrtc.org67ee6b92014-02-03 16:57:16 +00002084 << " with VoiceEngine channel #" << channel << ".";
Tommif888bb52015-12-12 01:37:01 +01002085 it->second->SetRawAudioSink(nullptr);
solenberg7add0582015-11-20 09:59:34 -08002086 delete it->second;
2087 recv_streams_.erase(it);
2088 return DeleteVoEChannel(channel);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002089}
2090
Peter Boström0c4e06b2015-10-07 12:23:21 +02002091bool WebRtcVoiceMediaChannel::SetLocalRenderer(uint32_t ssrc,
henrike@webrtc.org1e09a712013-07-26 19:17:59 +00002092 AudioRenderer* renderer) {
solenbergc96df772015-10-21 13:01:53 -07002093 auto it = send_streams_.find(ssrc);
2094 if (it == send_streams_.end()) {
wu@webrtc.org9dba5252013-08-05 20:36:57 +00002095 if (renderer) {
2096 // Return an error if trying to set a valid renderer with an invalid ssrc.
2097 LOG(LS_ERROR) << "SetLocalRenderer failed with ssrc "<< ssrc;
2098 return false;
2099 }
2100
2101 // The channel likely has gone away, do nothing.
henrike@webrtc.org1e09a712013-07-26 19:17:59 +00002102 return true;
henrike@webrtc.org1e09a712013-07-26 19:17:59 +00002103 }
2104
solenberg1ac56142015-10-13 03:58:19 -07002105 if (renderer) {
mallinath@webrtc.org67ee6b92014-02-03 16:57:16 +00002106 it->second->Start(renderer);
solenberg1ac56142015-10-13 03:58:19 -07002107 } else {
mallinath@webrtc.org67ee6b92014-02-03 16:57:16 +00002108 it->second->Stop();
solenberg1ac56142015-10-13 03:58:19 -07002109 }
henrike@webrtc.org1e09a712013-07-26 19:17:59 +00002110
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002111 return true;
2112}
2113
2114bool WebRtcVoiceMediaChannel::GetActiveStreams(
2115 AudioInfo::StreamList* actives) {
solenberg566ef242015-11-06 15:34:49 -08002116 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002117 actives->clear();
solenberg7add0582015-11-20 09:59:34 -08002118 for (const auto& ch : recv_streams_) {
Fredrik Solenbergaf9fb212015-08-26 10:45:53 +02002119 int level = GetOutputLevel(ch.second->channel());
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002120 if (level > 0) {
Fredrik Solenbergaf9fb212015-08-26 10:45:53 +02002121 actives->push_back(std::make_pair(ch.first, level));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002122 }
2123 }
2124 return true;
2125}
2126
2127int WebRtcVoiceMediaChannel::GetOutputLevel() {
solenberg566ef242015-11-06 15:34:49 -08002128 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
solenberg1ac56142015-10-13 03:58:19 -07002129 int highest = 0;
solenberg7add0582015-11-20 09:59:34 -08002130 for (const auto& ch : recv_streams_) {
solenberg8fb30c32015-10-13 03:06:58 -07002131 highest = std::max(GetOutputLevel(ch.second->channel()), highest);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002132 }
2133 return highest;
2134}
2135
2136int WebRtcVoiceMediaChannel::GetTimeSinceLastTyping() {
2137 int ret;
2138 if (engine()->voe()->processing()->TimeSinceLastTyping(ret) == -1) {
2139 // In case of error, log the info and continue
2140 LOG_RTCERR0(TimeSinceLastTyping);
2141 ret = -1;
2142 } else {
2143 ret *= 1000; // We return ms, webrtc returns seconds.
2144 }
2145 return ret;
2146}
2147
2148void WebRtcVoiceMediaChannel::SetTypingDetectionParameters(int time_window,
2149 int cost_per_typing, int reporting_threshold, int penalty_decay,
2150 int type_event_delay) {
2151 if (engine()->voe()->processing()->SetTypingDetectionParameters(
2152 time_window, cost_per_typing,
2153 reporting_threshold, penalty_decay, type_event_delay) == -1) {
2154 // In case of error, log the info and continue
2155 LOG_RTCERR5(SetTypingDetectionParameters, time_window,
2156 cost_per_typing, reporting_threshold, penalty_decay,
2157 type_event_delay);
2158 }
2159}
2160
solenberg4bac9c52015-10-09 02:32:53 -07002161bool WebRtcVoiceMediaChannel::SetOutputVolume(uint32_t ssrc, double volume) {
solenberg566ef242015-11-06 15:34:49 -08002162 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
solenberg1ac56142015-10-13 03:58:19 -07002163 if (ssrc == 0) {
2164 default_recv_volume_ = volume;
2165 if (default_recv_ssrc_ == -1) {
2166 return true;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002167 }
solenberg1ac56142015-10-13 03:58:19 -07002168 ssrc = static_cast<uint32_t>(default_recv_ssrc_);
2169 }
2170 int ch_id = GetReceiveChannelId(ssrc);
2171 if (ch_id < 0) {
2172 LOG(LS_WARNING) << "Cannot find channel for ssrc:" << ssrc;
2173 return false;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002174 }
2175
solenberg1ac56142015-10-13 03:58:19 -07002176 if (-1 == engine()->voe()->volume()->SetChannelOutputVolumeScaling(ch_id,
2177 volume)) {
2178 LOG_RTCERR2(SetChannelOutputVolumeScaling, ch_id, volume);
2179 return false;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002180 }
solenberg1ac56142015-10-13 03:58:19 -07002181 LOG(LS_INFO) << "SetOutputVolume to " << volume
2182 << " for channel " << ch_id << " and ssrc " << ssrc;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002183 return true;
2184}
2185
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002186bool WebRtcVoiceMediaChannel::CanInsertDtmf() {
Fredrik Solenbergb5727682015-12-04 15:22:19 +01002187 return dtmf_payload_type_ ? true : false;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002188}
2189
solenberg1d63dd02015-12-02 12:35:09 -08002190bool WebRtcVoiceMediaChannel::InsertDtmf(uint32_t ssrc, int event,
2191 int duration) {
solenberg566ef242015-11-06 15:34:49 -08002192 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
Fredrik Solenbergb5727682015-12-04 15:22:19 +01002193 LOG(LS_INFO) << "WebRtcVoiceMediaChannel::InsertDtmf";
2194 if (!dtmf_payload_type_) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002195 return false;
2196 }
2197
Fredrik Solenbergb5727682015-12-04 15:22:19 +01002198 // Figure out which WebRtcAudioSendStream to send the event on.
2199 auto it = ssrc != 0 ? send_streams_.find(ssrc) : send_streams_.begin();
2200 if (it == send_streams_.end()) {
2201 LOG(LS_WARNING) << "The specified ssrc " << ssrc << " is not in use.";
solenberg1d63dd02015-12-02 12:35:09 -08002202 return false;
2203 }
Fredrik Solenbergb5727682015-12-04 15:22:19 +01002204 if (event < kMinTelephoneEventCode ||
2205 event > kMaxTelephoneEventCode) {
2206 LOG(LS_WARNING) << "DTMF event code " << event << " out of range.";
solenberg1d63dd02015-12-02 12:35:09 -08002207 return false;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002208 }
Fredrik Solenbergb5727682015-12-04 15:22:19 +01002209 if (duration < kMinTelephoneEventDuration ||
2210 duration > kMaxTelephoneEventDuration) {
2211 LOG(LS_WARNING) << "DTMF event duration " << duration << " out of range.";
2212 return false;
2213 }
2214 return it->second->SendTelephoneEvent(*dtmf_payload_type_, event, duration);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002215}
2216
wu@webrtc.orga9890802013-12-13 00:21:03 +00002217void WebRtcVoiceMediaChannel::OnPacketReceived(
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00002218 rtc::Buffer* packet, const rtc::PacketTime& packet_time) {
solenberg566ef242015-11-06 15:34:49 -08002219 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
Fredrik Solenberg4b60c732015-05-07 14:07:48 +02002220
solenberg1ac56142015-10-13 03:58:19 -07002221 uint32_t ssrc = 0;
2222 if (!GetRtpSsrc(packet->data(), packet->size(), &ssrc)) {
2223 return;
2224 }
2225
solenberg7e63ef02015-11-20 00:19:43 -08002226 // If we don't have a default channel, and the SSRC is unknown, create a
2227 // default channel.
2228 if (default_recv_ssrc_ == -1 && GetReceiveChannelId(ssrc) == -1) {
solenberg1ac56142015-10-13 03:58:19 -07002229 StreamParams sp;
2230 sp.ssrcs.push_back(ssrc);
2231 LOG(LS_INFO) << "Creating default receive stream for SSRC=" << ssrc << ".";
2232 if (!AddRecvStream(sp)) {
2233 LOG(LS_WARNING) << "Could not create default receive stream.";
2234 return;
2235 }
2236 default_recv_ssrc_ = ssrc;
2237 SetOutputVolume(default_recv_ssrc_, default_recv_volume_);
deadbeef884f5852016-01-15 09:20:04 -08002238 if (default_sink_) {
kwiberg686a8ef2016-02-26 03:00:35 -08002239 std::unique_ptr<webrtc::AudioSinkInterface> proxy_sink(
deadbeef884f5852016-01-15 09:20:04 -08002240 new ProxySink(default_sink_.get()));
2241 SetRawAudioSink(default_recv_ssrc_, std::move(proxy_sink));
2242 }
solenberg1ac56142015-10-13 03:58:19 -07002243 }
2244
2245 // Forward packet to Call. If the SSRC is unknown we'll return after this.
Fredrik Solenberg709ed672015-09-15 12:26:33 +02002246 const webrtc::PacketTime webrtc_packet_time(packet_time.timestamp,
2247 packet_time.not_before);
solenberg1ac56142015-10-13 03:58:19 -07002248 webrtc::PacketReceiver::DeliveryStatus delivery_result =
2249 call_->Receiver()->DeliverPacket(webrtc::MediaType::AUDIO,
2250 reinterpret_cast<const uint8_t*>(packet->data()), packet->size(),
2251 webrtc_packet_time);
2252 if (webrtc::PacketReceiver::DELIVERY_OK != delivery_result) {
solenberg7e63ef02015-11-20 00:19:43 -08002253 // If the SSRC is unknown here, route it to the default channel, if we have
2254 // one. See: https://bugs.chromium.org/p/webrtc/issues/detail?id=5208
2255 if (default_recv_ssrc_ == -1) {
2256 return;
2257 } else {
2258 ssrc = default_recv_ssrc_;
2259 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002260 }
2261
solenberg1ac56142015-10-13 03:58:19 -07002262 // Find the channel to send this packet to. It must exist since webrtc::Call
2263 // was able to demux the packet.
2264 int channel = GetReceiveChannelId(ssrc);
2265 RTC_DCHECK(channel != -1);
2266
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002267 // Pass it off to the decoder.
henrike@webrtc.org28654cb2013-07-22 21:07:49 +00002268 engine()->voe()->network()->ReceivedRTPPacket(
solenberg1ac56142015-10-13 03:58:19 -07002269 channel, packet->data(), packet->size(), webrtc_packet_time);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002270}
2271
wu@webrtc.orga9890802013-12-13 00:21:03 +00002272void WebRtcVoiceMediaChannel::OnRtcpReceived(
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00002273 rtc::Buffer* packet, const rtc::PacketTime& packet_time) {
solenberg566ef242015-11-06 15:34:49 -08002274 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
Fredrik Solenberg4b60c732015-05-07 14:07:48 +02002275
Fredrik Solenberg709ed672015-09-15 12:26:33 +02002276 // Forward packet to Call as well.
2277 const webrtc::PacketTime webrtc_packet_time(packet_time.timestamp,
2278 packet_time.not_before);
2279 call_->Receiver()->DeliverPacket(webrtc::MediaType::AUDIO,
2280 reinterpret_cast<const uint8_t*>(packet->data()), packet->size(),
2281 webrtc_packet_time);
Fredrik Solenberg4b60c732015-05-07 14:07:48 +02002282
wu@webrtc.org9dba5252013-08-05 20:36:57 +00002283 // Sending channels need all RTCP packets with feedback information.
2284 // Even sender reports can contain attached report blocks.
2285 // Receiving channels need sender reports in order to create
2286 // correct receiver reports.
2287 int type = 0;
kwiberg@webrtc.orgeebcab52015-03-24 09:19:06 +00002288 if (!GetRtcpType(packet->data(), packet->size(), &type)) {
wu@webrtc.org9dba5252013-08-05 20:36:57 +00002289 LOG(LS_WARNING) << "Failed to parse type from received RTCP packet";
2290 return;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002291 }
2292
solenberg0b675462015-10-09 01:37:09 -07002293 // If it is a sender report, find the receive channel that is listening.
wu@webrtc.org9dba5252013-08-05 20:36:57 +00002294 if (type == kRtcpTypeSR) {
solenberg0b675462015-10-09 01:37:09 -07002295 uint32_t ssrc = 0;
2296 if (!GetRtcpSsrc(packet->data(), packet->size(), &ssrc)) {
2297 return;
2298 }
2299 int recv_channel_id = GetReceiveChannelId(ssrc);
2300 if (recv_channel_id != -1) {
wu@webrtc.org9dba5252013-08-05 20:36:57 +00002301 engine()->voe()->network()->ReceivedRTCPPacket(
solenberg0b675462015-10-09 01:37:09 -07002302 recv_channel_id, packet->data(), packet->size());
wu@webrtc.org9dba5252013-08-05 20:36:57 +00002303 }
2304 }
2305
2306 // SR may continue RR and any RR entry may correspond to any one of the send
2307 // channels. So all RTCP packets must be forwarded all send channels. VoE
2308 // will filter out RR internally.
solenbergc96df772015-10-21 13:01:53 -07002309 for (const auto& ch : send_streams_) {
wu@webrtc.org9dba5252013-08-05 20:36:57 +00002310 engine()->voe()->network()->ReceivedRTCPPacket(
Fredrik Solenbergaf9fb212015-08-26 10:45:53 +02002311 ch.second->channel(), packet->data(), packet->size());
wu@webrtc.org9dba5252013-08-05 20:36:57 +00002312 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002313}
2314
Peter Boström0c4e06b2015-10-07 12:23:21 +02002315bool WebRtcVoiceMediaChannel::MuteStream(uint32_t ssrc, bool muted) {
solenberg566ef242015-11-06 15:34:49 -08002316 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
solenberg0a617e22015-10-20 15:49:38 -07002317 int channel = GetSendChannelId(ssrc);
wu@webrtc.org9dba5252013-08-05 20:36:57 +00002318 if (channel == -1) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002319 LOG(LS_WARNING) << "The specified ssrc " << ssrc << " is not in use.";
2320 return false;
2321 }
wu@webrtc.org9dba5252013-08-05 20:36:57 +00002322 if (engine()->voe()->volume()->SetInputMute(channel, muted) == -1) {
2323 LOG_RTCERR2(SetInputMute, channel, muted);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002324 return false;
2325 }
buildbot@webrtc.org6b21b712014-07-31 15:08:53 +00002326 // We set the AGC to mute state only when all the channels are muted.
2327 // This implementation is not ideal, instead we should signal the AGC when
2328 // the mic channel is muted/unmuted. We can't do it today because there
2329 // is no good way to know which stream is mapping to the mic channel.
2330 bool all_muted = muted;
solenbergc96df772015-10-21 13:01:53 -07002331 for (const auto& ch : send_streams_) {
Fredrik Solenbergaf9fb212015-08-26 10:45:53 +02002332 if (!all_muted) {
2333 break;
2334 }
2335 if (engine()->voe()->volume()->GetInputMute(ch.second->channel(),
buildbot@webrtc.org6b21b712014-07-31 15:08:53 +00002336 all_muted)) {
Fredrik Solenbergaf9fb212015-08-26 10:45:53 +02002337 LOG_RTCERR1(GetInputMute, ch.second->channel());
buildbot@webrtc.org6b21b712014-07-31 15:08:53 +00002338 return false;
2339 }
2340 }
2341
2342 webrtc::AudioProcessing* ap = engine()->voe()->base()->audio_processing();
solenberg0a617e22015-10-20 15:49:38 -07002343 if (ap) {
buildbot@webrtc.org6b21b712014-07-31 15:08:53 +00002344 ap->set_output_will_be_muted(all_muted);
solenberg0a617e22015-10-20 15:49:38 -07002345 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002346 return true;
2347}
2348
minyue@webrtc.org26236952014-10-29 02:27:08 +00002349// TODO(minyue): SetMaxSendBandwidth() is subject to be renamed to
2350// SetMaxSendBitrate() in future.
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00002351bool WebRtcVoiceMediaChannel::SetMaxSendBandwidth(int bps) {
minyue@webrtc.org26236952014-10-29 02:27:08 +00002352 LOG(LS_INFO) << "WebRtcVoiceMediaChannel::SetMaxSendBandwidth.";
minyue@webrtc.org26236952014-10-29 02:27:08 +00002353 return SetSendBitrateInternal(bps);
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00002354}
2355
minyue@webrtc.org26236952014-10-29 02:27:08 +00002356bool WebRtcVoiceMediaChannel::SetSendBitrateInternal(int bps) {
2357 LOG(LS_INFO) << "WebRtcVoiceMediaChannel::SetSendBitrateInternal.";
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00002358
minyue@webrtc.org26236952014-10-29 02:27:08 +00002359 send_bitrate_setting_ = true;
2360 send_bitrate_bps_ = bps;
wu@webrtc.org1d1ffc92013-10-16 18:12:02 +00002361
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002362 if (!send_codec_) {
wu@webrtc.org1d1ffc92013-10-16 18:12:02 +00002363 LOG(LS_INFO) << "The send codec has not been set up yet. "
minyue@webrtc.org26236952014-10-29 02:27:08 +00002364 << "The send bitrate setting will be applied later.";
wu@webrtc.org1d1ffc92013-10-16 18:12:02 +00002365 return true;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002366 }
2367
minyue@webrtc.org26236952014-10-29 02:27:08 +00002368 // Bitrate is auto by default.
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00002369 // TODO(bemasc): Fix this so that if SetMaxSendBandwidth(50) is followed by
2370 // SetMaxSendBandwith(0), the second call removes the previous limit.
2371 if (bps <= 0)
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002372 return true;
2373
2374 webrtc::CodecInst codec = *send_codec_;
solenberg26c8c912015-11-27 04:00:25 -08002375 bool is_multi_rate = WebRtcVoiceCodecs::IsCodecMultiRate(codec);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002376
2377 if (is_multi_rate) {
2378 // If codec is multi-rate then just set the bitrate.
2379 codec.rate = bps;
solenbergc96df772015-10-21 13:01:53 -07002380 for (const auto& ch : send_streams_) {
solenberg0a617e22015-10-20 15:49:38 -07002381 if (!SetSendCodec(ch.second->channel(), codec)) {
2382 LOG(LS_INFO) << "Failed to set codec " << codec.plname
2383 << " to bitrate " << bps << " bps.";
2384 return false;
2385 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002386 }
2387 return true;
2388 } else {
2389 // If codec is not multi-rate and |bps| is less than the fixed bitrate
2390 // then fail. If codec is not multi-rate and |bps| exceeds or equal the
2391 // fixed bitrate then ignore.
2392 if (bps < codec.rate) {
2393 LOG(LS_INFO) << "Failed to set codec " << codec.plname
2394 << " to bitrate " << bps << " bps"
2395 << ", requires at least " << codec.rate << " bps.";
2396 return false;
2397 }
2398 return true;
2399 }
2400}
2401
2402bool WebRtcVoiceMediaChannel::GetStats(VoiceMediaInfo* info) {
solenberg566ef242015-11-06 15:34:49 -08002403 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
solenberg85a04962015-10-27 03:35:21 -07002404 RTC_DCHECK(info);
solenbergd97ec302015-10-07 01:40:33 -07002405
solenberg85a04962015-10-27 03:35:21 -07002406 // Get SSRC and stats for each sender.
2407 RTC_DCHECK(info->senders.size() == 0);
2408 for (const auto& stream : send_streams_) {
2409 webrtc::AudioSendStream::Stats stats = stream.second->GetStats();
wu@webrtc.org9dba5252013-08-05 20:36:57 +00002410 VoiceSenderInfo sinfo;
solenberg85a04962015-10-27 03:35:21 -07002411 sinfo.add_ssrc(stats.local_ssrc);
2412 sinfo.bytes_sent = stats.bytes_sent;
2413 sinfo.packets_sent = stats.packets_sent;
2414 sinfo.packets_lost = stats.packets_lost;
2415 sinfo.fraction_lost = stats.fraction_lost;
2416 sinfo.codec_name = stats.codec_name;
2417 sinfo.ext_seqnum = stats.ext_seqnum;
2418 sinfo.jitter_ms = stats.jitter_ms;
2419 sinfo.rtt_ms = stats.rtt_ms;
2420 sinfo.audio_level = stats.audio_level;
2421 sinfo.aec_quality_min = stats.aec_quality_min;
2422 sinfo.echo_delay_median_ms = stats.echo_delay_median_ms;
2423 sinfo.echo_delay_std_ms = stats.echo_delay_std_ms;
2424 sinfo.echo_return_loss = stats.echo_return_loss;
2425 sinfo.echo_return_loss_enhancement = stats.echo_return_loss_enhancement;
solenberg566ef242015-11-06 15:34:49 -08002426 sinfo.typing_noise_detected =
2427 (send_ == SEND_NOTHING ? false : stats.typing_noise_detected);
wu@webrtc.org9dba5252013-08-05 20:36:57 +00002428 info->senders.push_back(sinfo);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002429 }
2430
solenberg85a04962015-10-27 03:35:21 -07002431 // Get SSRC and stats for each receiver.
2432 RTC_DCHECK(info->receivers.size() == 0);
solenberg7add0582015-11-20 09:59:34 -08002433 for (const auto& stream : recv_streams_) {
Fredrik Solenberg4f4ec0a2015-10-22 10:49:27 +02002434 webrtc::AudioReceiveStream::Stats stats = stream.second->GetStats();
2435 VoiceReceiverInfo rinfo;
2436 rinfo.add_ssrc(stats.remote_ssrc);
2437 rinfo.bytes_rcvd = stats.bytes_rcvd;
2438 rinfo.packets_rcvd = stats.packets_rcvd;
2439 rinfo.packets_lost = stats.packets_lost;
2440 rinfo.fraction_lost = stats.fraction_lost;
2441 rinfo.codec_name = stats.codec_name;
2442 rinfo.ext_seqnum = stats.ext_seqnum;
2443 rinfo.jitter_ms = stats.jitter_ms;
2444 rinfo.jitter_buffer_ms = stats.jitter_buffer_ms;
2445 rinfo.jitter_buffer_preferred_ms = stats.jitter_buffer_preferred_ms;
2446 rinfo.delay_estimate_ms = stats.delay_estimate_ms;
2447 rinfo.audio_level = stats.audio_level;
2448 rinfo.expand_rate = stats.expand_rate;
2449 rinfo.speech_expand_rate = stats.speech_expand_rate;
2450 rinfo.secondary_decoded_rate = stats.secondary_decoded_rate;
2451 rinfo.accelerate_rate = stats.accelerate_rate;
2452 rinfo.preemptive_expand_rate = stats.preemptive_expand_rate;
2453 rinfo.decoding_calls_to_silence_generator =
2454 stats.decoding_calls_to_silence_generator;
2455 rinfo.decoding_calls_to_neteq = stats.decoding_calls_to_neteq;
2456 rinfo.decoding_normal = stats.decoding_normal;
2457 rinfo.decoding_plc = stats.decoding_plc;
2458 rinfo.decoding_cng = stats.decoding_cng;
2459 rinfo.decoding_plc_cng = stats.decoding_plc_cng;
2460 rinfo.capture_start_ntp_time_ms = stats.capture_start_ntp_time_ms;
2461 info->receivers.push_back(rinfo);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002462 }
2463
2464 return true;
2465}
2466
Tommif888bb52015-12-12 01:37:01 +01002467void WebRtcVoiceMediaChannel::SetRawAudioSink(
2468 uint32_t ssrc,
kwiberg686a8ef2016-02-26 03:00:35 -08002469 std::unique_ptr<webrtc::AudioSinkInterface> sink) {
Tommif888bb52015-12-12 01:37:01 +01002470 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
deadbeef884f5852016-01-15 09:20:04 -08002471 LOG(LS_VERBOSE) << "WebRtcVoiceMediaChannel::SetRawAudioSink: ssrc:" << ssrc
2472 << " " << (sink ? "(ptr)" : "NULL");
2473 if (ssrc == 0) {
2474 if (default_recv_ssrc_ != -1) {
kwiberg686a8ef2016-02-26 03:00:35 -08002475 std::unique_ptr<webrtc::AudioSinkInterface> proxy_sink(
deadbeef884f5852016-01-15 09:20:04 -08002476 sink ? new ProxySink(sink.get()) : nullptr);
2477 SetRawAudioSink(default_recv_ssrc_, std::move(proxy_sink));
2478 }
2479 default_sink_ = std::move(sink);
2480 return;
2481 }
Tommif888bb52015-12-12 01:37:01 +01002482 const auto it = recv_streams_.find(ssrc);
2483 if (it == recv_streams_.end()) {
2484 LOG(LS_WARNING) << "SetRawAudioSink: no recv stream" << ssrc;
2485 return;
2486 }
deadbeef2d110be2016-01-13 12:00:26 -08002487 it->second->SetRawAudioSink(std::move(sink));
Tommif888bb52015-12-12 01:37:01 +01002488}
2489
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002490int WebRtcVoiceMediaChannel::GetOutputLevel(int channel) {
solenbergd97ec302015-10-07 01:40:33 -07002491 unsigned int ulevel = 0;
2492 int ret = engine()->voe()->volume()->GetSpeechOutputLevel(channel, ulevel);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002493 return (ret == 0) ? static_cast<int>(ulevel) : -1;
2494}
2495
Peter Boström0c4e06b2015-10-07 12:23:21 +02002496int WebRtcVoiceMediaChannel::GetReceiveChannelId(uint32_t ssrc) const {
solenberg566ef242015-11-06 15:34:49 -08002497 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
solenberg7add0582015-11-20 09:59:34 -08002498 const auto it = recv_streams_.find(ssrc);
2499 if (it != recv_streams_.end()) {
mallinath@webrtc.org67ee6b92014-02-03 16:57:16 +00002500 return it->second->channel();
solenberg8fb30c32015-10-13 03:06:58 -07002501 }
solenberg1ac56142015-10-13 03:58:19 -07002502 return -1;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002503}
2504
Peter Boström0c4e06b2015-10-07 12:23:21 +02002505int WebRtcVoiceMediaChannel::GetSendChannelId(uint32_t ssrc) const {
solenberg566ef242015-11-06 15:34:49 -08002506 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
solenbergc96df772015-10-21 13:01:53 -07002507 const auto it = send_streams_.find(ssrc);
2508 if (it != send_streams_.end()) {
mallinath@webrtc.org67ee6b92014-02-03 16:57:16 +00002509 return it->second->channel();
solenberg8fb30c32015-10-13 03:06:58 -07002510 }
wu@webrtc.org9dba5252013-08-05 20:36:57 +00002511 return -1;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002512}
2513
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002514bool WebRtcVoiceMediaChannel::SetPlayout(int channel, bool playout) {
2515 if (playout) {
2516 LOG(LS_INFO) << "Starting playout for channel #" << channel;
2517 if (engine()->voe()->base()->StartPlayout(channel) == -1) {
2518 LOG_RTCERR1(StartPlayout, channel);
2519 return false;
2520 }
2521 } else {
2522 LOG(LS_INFO) << "Stopping playout for channel #" << channel;
2523 engine()->voe()->base()->StopPlayout(channel);
2524 }
2525 return true;
2526}
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002527} // namespace cricket
2528
2529#endif // HAVE_WEBRTC_VOICE