blob: 21094fde4d45af8baed7c5228237a5caba363e04 [file] [log] [blame]
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001/*
kjellander1afca732016-02-07 20:46:45 -08002 * Copyright (c) 2004 The WebRTC project authors. All Rights Reserved.
henrike@webrtc.org28e20752013-07-10 00:45:36 +00003 *
kjellander1afca732016-02-07 20:46:45 -08004 * Use of this source code is governed by a BSD-style license
5 * that can be found in the LICENSE file in the root of the source
6 * tree. An additional intellectual property rights grant can be found
7 * in the file PATENTS. All contributing project authors may
8 * be found in the AUTHORS file in the root of the source tree.
henrike@webrtc.org28e20752013-07-10 00:45:36 +00009 */
10
henrike@webrtc.org28e20752013-07-10 00:45:36 +000011#ifdef HAVE_WEBRTC_VOICE
12
kjellander@webrtc.org5ad12972016-02-12 06:39:40 +010013#include "webrtc/media/engine/webrtcvoiceengine.h"
henrike@webrtc.org28e20752013-07-10 00:45:36 +000014
15#include <algorithm>
16#include <cstdio>
17#include <string>
18#include <vector>
19
kjellander@webrtc.org7ffeab52016-02-26 22:46:09 +010020#include "webrtc/audio_sink.h"
tfarina5237aaf2015-11-10 23:44:30 -080021#include "webrtc/base/arraysize.h"
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +000022#include "webrtc/base/base64.h"
23#include "webrtc/base/byteorder.h"
24#include "webrtc/base/common.h"
25#include "webrtc/base/helpers.h"
26#include "webrtc/base/logging.h"
27#include "webrtc/base/stringencode.h"
28#include "webrtc/base/stringutils.h"
Peter Boströmca8b4042016-03-08 14:24:13 -080029#include "webrtc/base/trace_event.h"
ivoc112a3d82015-10-16 02:22:18 -070030#include "webrtc/call/rtc_event_log.h"
mallinath@webrtc.orga27be8e2013-09-27 23:04:10 +000031#include "webrtc/common.h"
kjellandera96e2d72016-02-04 23:52:28 -080032#include "webrtc/media/base/audioframe.h"
Taylor Brandstetter1a018dc2016-03-08 12:37:39 -080033#include "webrtc/media/base/audiosource.h"
kjellanderf4752772016-03-02 05:42:30 -080034#include "webrtc/media/base/mediaconstants.h"
kjellandera96e2d72016-02-04 23:52:28 -080035#include "webrtc/media/base/streamparams.h"
kjellander@webrtc.org5ad12972016-02-12 06:39:40 +010036#include "webrtc/media/engine/webrtcmediaengine.h"
37#include "webrtc/media/engine/webrtcvoe.h"
solenberg26c8c912015-11-27 04:00:25 -080038#include "webrtc/modules/audio_coding/acm2/rent_a_codec.h"
henrike@webrtc.org28e20752013-07-10 00:45:36 +000039#include "webrtc/modules/audio_processing/include/audio_processing.h"
Henrik Kjellander98f53512015-10-28 18:17:40 +010040#include "webrtc/system_wrappers/include/field_trial.h"
solenbergbd138382015-11-20 16:08:07 -080041#include "webrtc/system_wrappers/include/trace.h"
henrike@webrtc.org28e20752013-07-10 00:45:36 +000042
henrike@webrtc.org28e20752013-07-10 00:45:36 +000043namespace cricket {
solenbergd97ec302015-10-07 01:40:33 -070044namespace {
henrike@webrtc.org28e20752013-07-10 00:45:36 +000045
solenbergbd138382015-11-20 16:08:07 -080046const int kDefaultTraceFilter = webrtc::kTraceNone | webrtc::kTraceTerseInfo |
47 webrtc::kTraceWarning | webrtc::kTraceError |
48 webrtc::kTraceCritical;
49const int kElevatedTraceFilter = kDefaultTraceFilter | webrtc::kTraceStateInfo |
50 webrtc::kTraceInfo;
51
henrike@webrtc.org28e20752013-07-10 00:45:36 +000052// On Windows Vista and newer, Microsoft introduced the concept of "Default
53// Communications Device". This means that there are two types of default
54// devices (old Wave Audio style default and Default Communications Device).
55//
56// On Windows systems which only support Wave Audio style default, uses either
57// -1 or 0 to select the default device.
henrike@webrtc.org28e20752013-07-10 00:45:36 +000058#ifdef WIN32
solenbergd97ec302015-10-07 01:40:33 -070059const int kDefaultAudioDeviceId = -1;
solenberg8ad582d2016-03-16 09:34:56 -070060#elif !defined(WEBRTC_IOS)
solenbergd97ec302015-10-07 01:40:33 -070061const int kDefaultAudioDeviceId = 0;
henrike@webrtc.org28e20752013-07-10 00:45:36 +000062#endif
63
henrike@webrtc.org28e20752013-07-10 00:45:36 +000064// Parameter used for NACK.
65// This value is equivalent to 5 seconds of audio data at 20 ms per packet.
solenbergd97ec302015-10-07 01:40:33 -070066const int kNackMaxPackets = 250;
minyue@webrtc.org2dc6f312014-10-31 05:33:10 +000067
68// Codec parameters for Opus.
henrike@webrtc.org1e09a712013-07-26 19:17:59 +000069// draft-spittka-payload-rtp-opus-03
minyue@webrtc.org2dc6f312014-10-31 05:33:10 +000070
71// Recommended bitrates:
72// 8-12 kb/s for NB speech,
73// 16-20 kb/s for WB speech,
74// 28-40 kb/s for FB speech,
75// 48-64 kb/s for FB mono music, and
76// 64-128 kb/s for FB stereo music.
77// The current implementation applies the following values to mono signals,
78// and multiplies them by 2 for stereo.
solenbergd97ec302015-10-07 01:40:33 -070079const int kOpusBitrateNb = 12000;
80const int kOpusBitrateWb = 20000;
81const int kOpusBitrateFb = 32000;
minyue@webrtc.org2dc6f312014-10-31 05:33:10 +000082
henrike@webrtc.org1e09a712013-07-26 19:17:59 +000083// Opus bitrate should be in the range between 6000 and 510000.
solenbergd97ec302015-10-07 01:40:33 -070084const int kOpusMinBitrate = 6000;
85const int kOpusMaxBitrate = 510000;
buildbot@webrtc.org5d639b32014-09-10 07:57:12 +000086
wu@webrtc.orgde305012013-10-31 15:40:38 +000087// Default audio dscp value.
88// See http://tools.ietf.org/html/rfc2474 for details.
89// See also http://tools.ietf.org/html/draft-jennings-rtcweb-qos-00
solenbergd97ec302015-10-07 01:40:33 -070090const rtc::DiffServCodePoint kAudioDscpValue = rtc::DSCP_EF;
henrike@webrtc.org1e09a712013-07-26 19:17:59 +000091
Fredrik Solenbergb5727682015-12-04 15:22:19 +010092// Constants from voice_engine_defines.h.
93const int kMinTelephoneEventCode = 0; // RFC4733 (Section 2.3.1)
94const int kMaxTelephoneEventCode = 255;
95const int kMinTelephoneEventDuration = 100;
96const int kMaxTelephoneEventDuration = 60000; // Actual limit is 2^16
97
solenberg31642aa2016-03-14 08:00:37 -070098const int kMinPayloadType = 0;
99const int kMaxPayloadType = 127;
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() {
solenberg26c8c912015-11-27 04:00:25 -0800252 std::vector<AudioCodec> result;
253 for (webrtc::CodecInst voe_codec : webrtc::acm2::RentACodec::Database()) {
254 // Change the sample rate of G722 to 8000 to match SDP.
255 MaybeFixupG722(&voe_codec, 8000);
buildbot@webrtc.org13d67762014-05-02 17:33:29 +0000256 // Skip uncompressed formats.
Minyue Li7100dcd2015-03-27 05:05:59 +0100257 if (IsCodec(voe_codec, kL16CodecName)) {
buildbot@webrtc.org13d67762014-05-02 17:33:29 +0000258 continue;
259 }
260
261 const CodecPref* pref = NULL;
tfarina5237aaf2015-11-10 23:44:30 -0800262 for (size_t j = 0; j < arraysize(kCodecPrefs); ++j) {
Minyue Li7100dcd2015-03-27 05:05:59 +0100263 if (IsCodec(voe_codec, kCodecPrefs[j].name) &&
buildbot@webrtc.org13d67762014-05-02 17:33:29 +0000264 kCodecPrefs[j].clockrate == voe_codec.plfreq &&
265 kCodecPrefs[j].channels == voe_codec.channels) {
266 pref = &kCodecPrefs[j];
267 break;
268 }
269 }
270
271 if (pref) {
272 // Use the payload type that we've configured in our pref table;
273 // use the offset in our pref table to determine the sort order.
tfarina5237aaf2015-11-10 23:44:30 -0800274 AudioCodec codec(
275 pref->payload_type, voe_codec.plname, voe_codec.plfreq,
276 voe_codec.rate, voe_codec.channels,
277 static_cast<int>(arraysize(kCodecPrefs)) - (pref - kCodecPrefs));
Minyue Li7100dcd2015-03-27 05:05:59 +0100278 if (IsCodec(codec, kIsacCodecName)) {
minyue@webrtc.org26236952014-10-29 02:27:08 +0000279 // Indicate auto-bitrate in signaling.
buildbot@webrtc.org13d67762014-05-02 17:33:29 +0000280 codec.bitrate = 0;
281 }
Minyue Li7100dcd2015-03-27 05:05:59 +0100282 if (IsCodec(codec, kOpusCodecName)) {
buildbot@webrtc.org13d67762014-05-02 17:33:29 +0000283 // Only add fmtp parameters that differ from the spec.
284 if (kPreferredMinPTime != kOpusDefaultMinPTime) {
285 codec.params[kCodecParamMinPTime] =
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000286 rtc::ToString(kPreferredMinPTime);
buildbot@webrtc.org13d67762014-05-02 17:33:29 +0000287 }
288 if (kPreferredMaxPTime != kOpusDefaultMaxPTime) {
289 codec.params[kCodecParamMaxPTime] =
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000290 rtc::ToString(kPreferredMaxPTime);
buildbot@webrtc.org13d67762014-05-02 17:33:29 +0000291 }
pkasting@chromium.orgd3245462015-02-23 21:28:22 +0000292 codec.SetParam(kCodecParamUseInbandFec, 1);
stefanba4c0e42016-02-04 04:12:24 -0800293 codec.AddFeedbackParam(
294 FeedbackParam(kRtcpFbParamTransportCc, kParamValueEmpty));
minyue@webrtc.org4ef22d12014-11-17 09:26:39 +0000295
296 // TODO(hellner): Add ptime, sprop-stereo, and stereo
buildbot@webrtc.org13d67762014-05-02 17:33:29 +0000297 // when they can be set to values other than the default.
298 }
solenberg26c8c912015-11-27 04:00:25 -0800299 result.push_back(codec);
buildbot@webrtc.org13d67762014-05-02 17:33:29 +0000300 } else {
solenbergff976312016-03-30 23:28:51 -0700301 LOG(LS_INFO) << "[Unused] " << ToString(voe_codec);
buildbot@webrtc.org13d67762014-05-02 17:33:29 +0000302 }
303 }
solenberg26c8c912015-11-27 04:00:25 -0800304 // Make sure they are in local preference order.
305 std::sort(result.begin(), result.end(), &AudioCodec::Preferable);
306 return result;
buildbot@webrtc.org13d67762014-05-02 17:33:29 +0000307 }
buildbot@webrtc.org13d67762014-05-02 17:33:29 +0000308
solenberg26c8c912015-11-27 04:00:25 -0800309 static bool ToCodecInst(const AudioCodec& in,
310 webrtc::CodecInst* out) {
311 for (webrtc::CodecInst voe_codec : webrtc::acm2::RentACodec::Database()) {
312 // Change the sample rate of G722 to 8000 to match SDP.
313 MaybeFixupG722(&voe_codec, 8000);
314 AudioCodec codec(voe_codec.pltype, voe_codec.plname, voe_codec.plfreq,
315 voe_codec.rate, voe_codec.channels, 0);
316 bool multi_rate = IsCodecMultiRate(voe_codec);
317 // Allow arbitrary rates for ISAC to be specified.
318 if (multi_rate) {
319 // Set codec.bitrate to 0 so the check for codec.Matches() passes.
320 codec.bitrate = 0;
321 }
322 if (codec.Matches(in)) {
323 if (out) {
324 // Fixup the payload type.
325 voe_codec.pltype = in.id;
326
327 // Set bitrate if specified.
328 if (multi_rate && in.bitrate != 0) {
329 voe_codec.rate = in.bitrate;
330 }
331
332 // Reset G722 sample rate to 16000 to match WebRTC.
333 MaybeFixupG722(&voe_codec, 16000);
334
335 // Apply codec-specific settings.
336 if (IsCodec(codec, kIsacCodecName)) {
337 // If ISAC and an explicit bitrate is not specified,
338 // enable auto bitrate adjustment.
339 voe_codec.rate = (in.bitrate > 0) ? in.bitrate : -1;
340 }
341 *out = voe_codec;
342 }
343 return true;
344 }
345 }
henrik.lundin@webrtc.org8038d422014-11-11 08:38:24 +0000346 return false;
henrik.lundin@webrtc.orgf85dbce2014-11-07 12:25:00 +0000347 }
solenberg26c8c912015-11-27 04:00:25 -0800348
349 static bool IsCodecMultiRate(const webrtc::CodecInst& codec) {
350 for (size_t i = 0; i < arraysize(kCodecPrefs); ++i) {
351 if (IsCodec(codec, kCodecPrefs[i].name) &&
352 kCodecPrefs[i].clockrate == codec.plfreq) {
353 return kCodecPrefs[i].is_multi_rate;
354 }
355 }
356 return false;
357 }
358
359 // If the AudioCodec param kCodecParamPTime is set, then we will set it to
360 // codec pacsize if it's valid, or we will pick the next smallest value we
361 // support.
362 // TODO(Brave): Query supported packet sizes from ACM when the API is ready.
363 static bool SetPTimeAsPacketSize(webrtc::CodecInst* codec, int ptime_ms) {
364 for (const CodecPref& codec_pref : kCodecPrefs) {
365 if ((IsCodec(*codec, codec_pref.name) &&
366 codec_pref.clockrate == codec->plfreq) ||
367 IsCodec(*codec, kG722CodecName)) {
368 int packet_size_ms = SelectPacketSize(codec_pref, ptime_ms);
369 if (packet_size_ms) {
370 // Convert unit from milli-seconds to samples.
371 codec->pacsize = (codec->plfreq / 1000) * packet_size_ms;
372 return true;
373 }
374 }
375 }
376 return false;
377 }
378
stefanba4c0e42016-02-04 04:12:24 -0800379 static const AudioCodec* GetPreferredCodec(
380 const std::vector<AudioCodec>& codecs,
solenberg72e29d22016-03-08 06:35:16 -0800381 webrtc::CodecInst* out,
stefanba4c0e42016-02-04 04:12:24 -0800382 int* red_payload_type) {
solenberg72e29d22016-03-08 06:35:16 -0800383 RTC_DCHECK(out);
stefanba4c0e42016-02-04 04:12:24 -0800384 RTC_DCHECK(red_payload_type);
385 // Select the preferred send codec (the first non-telephone-event/CN codec).
386 for (const AudioCodec& codec : codecs) {
387 *red_payload_type = -1;
388 if (IsCodec(codec, kDtmfCodecName) || IsCodec(codec, kCnCodecName)) {
389 // Skip telephone-event/CN codec, which will be handled later.
390 continue;
391 }
392
393 // We'll use the first codec in the list to actually send audio data.
394 // Be sure to use the payload type requested by the remote side.
395 // "red", for RED audio, is a special case where the actual codec to be
396 // used is specified in params.
397 const AudioCodec* found_codec = &codec;
398 if (IsCodec(*found_codec, kRedCodecName)) {
399 // Parse out the RED parameters. If we fail, just ignore RED;
400 // we don't support all possible params/usage scenarios.
401 *red_payload_type = codec.id;
402 found_codec = GetRedSendCodec(*found_codec, codecs);
403 if (!found_codec) {
404 continue;
405 }
406 }
407 // Ignore codecs we don't know about. The negotiation step should prevent
408 // this, but double-check to be sure.
solenberg72e29d22016-03-08 06:35:16 -0800409 webrtc::CodecInst voe_codec = {0};
410 if (!ToCodecInst(*found_codec, &voe_codec)) {
stefanba4c0e42016-02-04 04:12:24 -0800411 LOG(LS_WARNING) << "Unknown codec " << ToString(*found_codec);
412 continue;
413 }
solenberg72e29d22016-03-08 06:35:16 -0800414 *out = voe_codec;
stefanba4c0e42016-02-04 04:12:24 -0800415 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
solenbergff976312016-03-30 23:28:51 -0700516WebRtcVoiceEngine::WebRtcVoiceEngine(webrtc::AudioDeviceModule* adm)
517 : WebRtcVoiceEngine(adm, new VoEWrapper()) {
518 audio_state_ = webrtc::AudioState::Create(MakeAudioStateConfig(voe()));
solenberg26c8c912015-11-27 04:00:25 -0800519}
520
solenbergff976312016-03-30 23:28:51 -0700521WebRtcVoiceEngine::WebRtcVoiceEngine(webrtc::AudioDeviceModule* adm,
522 VoEWrapper* voe_wrapper)
523 : adm_(adm), voe_wrapper_(voe_wrapper) {
solenberg26c8c912015-11-27 04:00:25 -0800524 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
solenbergff976312016-03-30 23:28:51 -0700525 LOG(LS_INFO) << "WebRtcVoiceEngine::WebRtcVoiceEngine";
526 RTC_DCHECK(voe_wrapper);
solenberg26c8c912015-11-27 04:00:25 -0800527
528 signal_thread_checker_.DetachFromThread();
solenberg26c8c912015-11-27 04:00:25 -0800529
530 // Load our audio codec list.
solenbergff976312016-03-30 23:28:51 -0700531 LOG(LS_INFO) << "Supported codecs in order of preference:";
solenberg26c8c912015-11-27 04:00:25 -0800532 codecs_ = WebRtcVoiceCodecs::SupportedCodecs();
solenbergff976312016-03-30 23:28:51 -0700533 for (const AudioCodec& codec : codecs_) {
534 LOG(LS_INFO) << ToString(codec);
buildbot@webrtc.org13d67762014-05-02 17:33:29 +0000535 }
buildbot@webrtc.org13d67762014-05-02 17:33:29 +0000536
solenbergff976312016-03-30 23:28:51 -0700537 voe_config_.Set<webrtc::VoicePacing>(new webrtc::VoicePacing(true));
buildbot@webrtc.org13d67762014-05-02 17:33:29 +0000538
solenbergff976312016-03-30 23:28:51 -0700539 // Temporarily turn logging level up for the Init() call.
540 webrtc::Trace::SetTraceCallback(this);
solenbergbd138382015-11-20 16:08:07 -0800541 webrtc::Trace::set_level_filter(kElevatedTraceFilter);
solenberg2515af22015-12-02 06:19:36 -0800542 LOG(LS_INFO) << webrtc::VoiceEngine::GetVersionString();
solenbergff976312016-03-30 23:28:51 -0700543 RTC_CHECK_EQ(0, voe_wrapper_->base()->Init(adm_.get()));
solenbergbd138382015-11-20 16:08:07 -0800544 webrtc::Trace::set_level_filter(kDefaultTraceFilter);
buildbot@webrtc.org13d67762014-05-02 17:33:29 +0000545
solenbergff976312016-03-30 23:28:51 -0700546 // No ADM supplied? Get the default one from VoE.
547 if (!adm_) {
548 adm_ = voe_wrapper_->base()->audio_device_module();
549 }
550 RTC_DCHECK(adm_);
551
buildbot@webrtc.org13d67762014-05-02 17:33:29 +0000552 // Save the default AGC configuration settings. This must happen before
solenberg246b8172015-12-08 09:50:23 -0800553 // calling ApplyOptions or the default will be overwritten.
solenbergff976312016-03-30 23:28:51 -0700554 int error = voe_wrapper_->processing()->GetAgcConfig(default_agc_config_);
555 RTC_DCHECK_EQ(0, error);
buildbot@webrtc.org13d67762014-05-02 17:33:29 +0000556
solenberg0f7d2932016-01-15 01:40:39 -0800557 // Set default engine options.
558 {
559 AudioOptions options;
560 options.echo_cancellation = rtc::Optional<bool>(true);
561 options.auto_gain_control = rtc::Optional<bool>(true);
562 options.noise_suppression = rtc::Optional<bool>(true);
563 options.highpass_filter = rtc::Optional<bool>(true);
564 options.stereo_swapping = rtc::Optional<bool>(false);
565 options.audio_jitter_buffer_max_packets = rtc::Optional<int>(50);
566 options.audio_jitter_buffer_fast_accelerate = rtc::Optional<bool>(false);
567 options.typing_detection = rtc::Optional<bool>(true);
568 options.adjust_agc_delta = rtc::Optional<int>(0);
569 options.experimental_agc = rtc::Optional<bool>(false);
570 options.extended_filter_aec = rtc::Optional<bool>(false);
571 options.delay_agnostic_aec = rtc::Optional<bool>(false);
572 options.experimental_ns = rtc::Optional<bool>(false);
solenbergff976312016-03-30 23:28:51 -0700573 bool error = ApplyOptions(options);
574 RTC_DCHECK(error);
buildbot@webrtc.org13d67762014-05-02 17:33:29 +0000575 }
576
solenberg246b8172015-12-08 09:50:23 -0800577 SetDefaultDevices();
buildbot@webrtc.org13d67762014-05-02 17:33:29 +0000578}
579
solenbergff976312016-03-30 23:28:51 -0700580WebRtcVoiceEngine::~WebRtcVoiceEngine() {
solenberg566ef242015-11-06 15:34:49 -0800581 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
solenbergff976312016-03-30 23:28:51 -0700582 LOG(LS_INFO) << "WebRtcVoiceEngine::~WebRtcVoiceEngine";
buildbot@webrtc.org13d67762014-05-02 17:33:29 +0000583 StopAecDump();
buildbot@webrtc.org13d67762014-05-02 17:33:29 +0000584 voe_wrapper_->base()->Terminate();
solenbergff976312016-03-30 23:28:51 -0700585 webrtc::Trace::SetTraceCallback(nullptr);
buildbot@webrtc.org13d67762014-05-02 17:33:29 +0000586}
587
solenberg566ef242015-11-06 15:34:49 -0800588rtc::scoped_refptr<webrtc::AudioState>
589 WebRtcVoiceEngine::GetAudioState() const {
590 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
591 return audio_state_;
592}
593
nisse51542be2016-02-12 02:27:06 -0800594VoiceMediaChannel* WebRtcVoiceEngine::CreateChannel(
595 webrtc::Call* call,
596 const MediaConfig& config,
Jelena Marusicc28a8962015-05-29 15:05:44 +0200597 const AudioOptions& options) {
solenberg566ef242015-11-06 15:34:49 -0800598 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
nisse51542be2016-02-12 02:27:06 -0800599 return new WebRtcVoiceMediaChannel(this, config, options, call);
buildbot@webrtc.org13d67762014-05-02 17:33:29 +0000600}
601
buildbot@webrtc.org13d67762014-05-02 17:33:29 +0000602bool WebRtcVoiceEngine::ApplyOptions(const AudioOptions& options_in) {
solenberg566ef242015-11-06 15:34:49 -0800603 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
solenbergff976312016-03-30 23:28:51 -0700604 LOG(LS_INFO) << "WebRtcVoiceEngine::ApplyOptions: " << options_in.ToString();
solenberg0f7d2932016-01-15 01:40:39 -0800605 AudioOptions options = options_in; // The options are modified below.
solenberg246b8172015-12-08 09:50:23 -0800606
buildbot@webrtc.org13d67762014-05-02 17:33:29 +0000607 // kEcConference is AEC with high suppression.
608 webrtc::EcModes ec_mode = webrtc::kEcConference;
609 webrtc::AecmModes aecm_mode = webrtc::kAecmSpeakerphone;
610 webrtc::AgcModes agc_mode = webrtc::kAgcAdaptiveAnalog;
611 webrtc::NsModes ns_mode = webrtc::kNsHighSuppression;
kwiberg102c6a62015-10-30 02:47:38 -0700612 if (options.aecm_generate_comfort_noise) {
buildbot@webrtc.org13d67762014-05-02 17:33:29 +0000613 LOG(LS_VERBOSE) << "Comfort noise explicitly set to "
kwiberg102c6a62015-10-30 02:47:38 -0700614 << *options.aecm_generate_comfort_noise
615 << " (default is false).";
buildbot@webrtc.org13d67762014-05-02 17:33:29 +0000616 }
617
kjellanderfcfc8042016-01-14 11:01:09 -0800618#if defined(WEBRTC_IOS)
buildbot@webrtc.org13d67762014-05-02 17:33:29 +0000619 // On iOS, VPIO provides built-in EC and AGC.
Karl Wibergbe579832015-11-10 22:34:18 +0100620 options.echo_cancellation = rtc::Optional<bool>(false);
621 options.auto_gain_control = rtc::Optional<bool>(false);
henrika86d907c2015-09-07 16:09:50 +0200622 LOG(LS_INFO) << "Always disable AEC and AGC on iOS. Use built-in instead.";
buildbot@webrtc.org13d67762014-05-02 17:33:29 +0000623#elif defined(ANDROID)
624 ec_mode = webrtc::kEcAecm;
625#endif
626
kjellanderfcfc8042016-01-14 11:01:09 -0800627#if defined(WEBRTC_IOS) || defined(ANDROID)
buildbot@webrtc.org13d67762014-05-02 17:33:29 +0000628 // Set the AGC mode for iOS as well despite disabling it above, to avoid
629 // unsupported configuration errors from webrtc.
630 agc_mode = webrtc::kAgcFixedDigital;
Karl Wibergbe579832015-11-10 22:34:18 +0100631 options.typing_detection = rtc::Optional<bool>(false);
632 options.experimental_agc = rtc::Optional<bool>(false);
633 options.extended_filter_aec = rtc::Optional<bool>(false);
634 options.experimental_ns = rtc::Optional<bool>(false);
buildbot@webrtc.org13d67762014-05-02 17:33:29 +0000635#endif
636
Bjorn Volckerbf395c12015-03-25 22:45:56 +0100637 // Delay Agnostic AEC automatically turns on EC if not set except on iOS
638 // where the feature is not supported.
639 bool use_delay_agnostic_aec = false;
kjellanderfcfc8042016-01-14 11:01:09 -0800640#if !defined(WEBRTC_IOS)
kwiberg102c6a62015-10-30 02:47:38 -0700641 if (options.delay_agnostic_aec) {
642 use_delay_agnostic_aec = *options.delay_agnostic_aec;
Bjorn Volckerbf395c12015-03-25 22:45:56 +0100643 if (use_delay_agnostic_aec) {
Karl Wibergbe579832015-11-10 22:34:18 +0100644 options.echo_cancellation = rtc::Optional<bool>(true);
645 options.extended_filter_aec = rtc::Optional<bool>(true);
Bjorn Volckerbf395c12015-03-25 22:45:56 +0100646 ec_mode = webrtc::kEcConference;
647 }
648 }
649#endif
650
buildbot@webrtc.org13d67762014-05-02 17:33:29 +0000651 webrtc::VoEAudioProcessing* voep = voe_wrapper_->processing();
652
kwiberg102c6a62015-10-30 02:47:38 -0700653 if (options.echo_cancellation) {
henrika@webrtc.orga954c072014-12-09 16:22:09 +0000654 // Check if platform supports built-in EC. Currently only supported on
655 // Android and in combination with Java based audio layer.
656 // TODO(henrika): investigate possibility to support built-in EC also
657 // in combination with Open SL ES audio.
658 const bool built_in_aec = voe_wrapper_->hw()->BuiltInAECIsAvailable();
Bjorn Volcker73f72102015-06-03 14:50:15 +0200659 if (built_in_aec) {
Bjorn Volckerccfc9392015-05-07 07:43:17 +0200660 // Built-in EC exists on this device and use_delay_agnostic_aec is not
661 // overriding it. Enable/Disable it according to the echo_cancellation
662 // audio option.
Bjorn Volcker73f72102015-06-03 14:50:15 +0200663 const bool enable_built_in_aec =
kwiberg102c6a62015-10-30 02:47:38 -0700664 *options.echo_cancellation && !use_delay_agnostic_aec;
Bjorn Volcker73f72102015-06-03 14:50:15 +0200665 if (voe_wrapper_->hw()->EnableBuiltInAEC(enable_built_in_aec) == 0 &&
666 enable_built_in_aec) {
Bjorn Volckerbf395c12015-03-25 22:45:56 +0100667 // Disable internal software EC if built-in EC is enabled,
henrika@webrtc.orga954c072014-12-09 16:22:09 +0000668 // i.e., replace the software EC with the built-in EC.
Karl Wibergbe579832015-11-10 22:34:18 +0100669 options.echo_cancellation = rtc::Optional<bool>(false);
henrika@webrtc.orga954c072014-12-09 16:22:09 +0000670 LOG(LS_INFO) << "Disabling EC since built-in EC will be used instead";
671 }
672 }
kwiberg102c6a62015-10-30 02:47:38 -0700673 if (voep->SetEcStatus(*options.echo_cancellation, ec_mode) == -1) {
674 LOG_RTCERR2(SetEcStatus, *options.echo_cancellation, ec_mode);
buildbot@webrtc.org13d67762014-05-02 17:33:29 +0000675 return false;
676 } else {
kwiberg102c6a62015-10-30 02:47:38 -0700677 LOG(LS_INFO) << "Echo control set to " << *options.echo_cancellation
henrika86d907c2015-09-07 16:09:50 +0200678 << " with mode " << ec_mode;
buildbot@webrtc.org13d67762014-05-02 17:33:29 +0000679 }
680#if !defined(ANDROID)
681 // TODO(ajm): Remove the error return on Android from webrtc.
kwiberg102c6a62015-10-30 02:47:38 -0700682 if (voep->SetEcMetricsStatus(*options.echo_cancellation) == -1) {
683 LOG_RTCERR1(SetEcMetricsStatus, *options.echo_cancellation);
buildbot@webrtc.org13d67762014-05-02 17:33:29 +0000684 return false;
685 }
686#endif
687 if (ec_mode == webrtc::kEcAecm) {
kwiberg102c6a62015-10-30 02:47:38 -0700688 bool cn = options.aecm_generate_comfort_noise.value_or(false);
689 if (voep->SetAecmMode(aecm_mode, cn) != 0) {
690 LOG_RTCERR2(SetAecmMode, aecm_mode, cn);
buildbot@webrtc.org13d67762014-05-02 17:33:29 +0000691 return false;
692 }
693 }
694 }
695
kwiberg102c6a62015-10-30 02:47:38 -0700696 if (options.auto_gain_control) {
henrikac14f5ff2015-09-23 14:08:33 +0200697 const bool built_in_agc = voe_wrapper_->hw()->BuiltInAGCIsAvailable();
698 if (built_in_agc) {
kwiberg102c6a62015-10-30 02:47:38 -0700699 if (voe_wrapper_->hw()->EnableBuiltInAGC(*options.auto_gain_control) ==
700 0 &&
701 *options.auto_gain_control) {
henrikac14f5ff2015-09-23 14:08:33 +0200702 // Disable internal software AGC if built-in AGC is enabled,
703 // i.e., replace the software AGC with the built-in AGC.
Karl Wibergbe579832015-11-10 22:34:18 +0100704 options.auto_gain_control = rtc::Optional<bool>(false);
henrikac14f5ff2015-09-23 14:08:33 +0200705 LOG(LS_INFO) << "Disabling AGC since built-in AGC will be used instead";
706 }
707 }
kwiberg102c6a62015-10-30 02:47:38 -0700708 if (voep->SetAgcStatus(*options.auto_gain_control, agc_mode) == -1) {
709 LOG_RTCERR2(SetAgcStatus, *options.auto_gain_control, agc_mode);
buildbot@webrtc.org13d67762014-05-02 17:33:29 +0000710 return false;
711 } else {
kwiberg102c6a62015-10-30 02:47:38 -0700712 LOG(LS_INFO) << "Auto gain set to " << *options.auto_gain_control
713 << " with mode " << agc_mode;
buildbot@webrtc.org13d67762014-05-02 17:33:29 +0000714 }
715 }
716
kwiberg102c6a62015-10-30 02:47:38 -0700717 if (options.tx_agc_target_dbov || options.tx_agc_digital_compression_gain ||
718 options.tx_agc_limiter) {
buildbot@webrtc.org13d67762014-05-02 17:33:29 +0000719 // Override default_agc_config_. Generally, an unset option means "leave
720 // the VoE bits alone" in this function, so we want whatever is set to be
721 // stored as the new "default". If we didn't, then setting e.g.
722 // tx_agc_target_dbov would reset digital compression gain and limiter
723 // settings.
724 // Also, if we don't update default_agc_config_, then adjust_agc_delta
725 // would be an offset from the original values, and not whatever was set
726 // explicitly.
kwiberg102c6a62015-10-30 02:47:38 -0700727 default_agc_config_.targetLeveldBOv = options.tx_agc_target_dbov.value_or(
728 default_agc_config_.targetLeveldBOv);
buildbot@webrtc.org13d67762014-05-02 17:33:29 +0000729 default_agc_config_.digitalCompressionGaindB =
kwiberg102c6a62015-10-30 02:47:38 -0700730 options.tx_agc_digital_compression_gain.value_or(
buildbot@webrtc.org13d67762014-05-02 17:33:29 +0000731 default_agc_config_.digitalCompressionGaindB);
732 default_agc_config_.limiterEnable =
kwiberg102c6a62015-10-30 02:47:38 -0700733 options.tx_agc_limiter.value_or(default_agc_config_.limiterEnable);
buildbot@webrtc.org13d67762014-05-02 17:33:29 +0000734 if (voe_wrapper_->processing()->SetAgcConfig(default_agc_config_) == -1) {
735 LOG_RTCERR3(SetAgcConfig,
736 default_agc_config_.targetLeveldBOv,
737 default_agc_config_.digitalCompressionGaindB,
738 default_agc_config_.limiterEnable);
739 return false;
740 }
741 }
742
kwiberg102c6a62015-10-30 02:47:38 -0700743 if (options.noise_suppression) {
henrikac14f5ff2015-09-23 14:08:33 +0200744 const bool built_in_ns = voe_wrapper_->hw()->BuiltInNSIsAvailable();
745 if (built_in_ns) {
kwiberg102c6a62015-10-30 02:47:38 -0700746 if (voe_wrapper_->hw()->EnableBuiltInNS(*options.noise_suppression) ==
747 0 &&
748 *options.noise_suppression) {
henrikac14f5ff2015-09-23 14:08:33 +0200749 // Disable internal software NS if built-in NS is enabled,
750 // i.e., replace the software NS with the built-in NS.
Karl Wibergbe579832015-11-10 22:34:18 +0100751 options.noise_suppression = rtc::Optional<bool>(false);
henrikac14f5ff2015-09-23 14:08:33 +0200752 LOG(LS_INFO) << "Disabling NS since built-in NS will be used instead";
753 }
754 }
kwiberg102c6a62015-10-30 02:47:38 -0700755 if (voep->SetNsStatus(*options.noise_suppression, ns_mode) == -1) {
756 LOG_RTCERR2(SetNsStatus, *options.noise_suppression, ns_mode);
buildbot@webrtc.org13d67762014-05-02 17:33:29 +0000757 return false;
758 } else {
kwiberg102c6a62015-10-30 02:47:38 -0700759 LOG(LS_INFO) << "Noise suppression set to " << *options.noise_suppression
henrikac14f5ff2015-09-23 14:08:33 +0200760 << " with mode " << ns_mode;
buildbot@webrtc.org13d67762014-05-02 17:33:29 +0000761 }
762 }
763
kwiberg102c6a62015-10-30 02:47:38 -0700764 if (options.highpass_filter) {
765 LOG(LS_INFO) << "High pass filter enabled? " << *options.highpass_filter;
766 if (voep->EnableHighPassFilter(*options.highpass_filter) == -1) {
767 LOG_RTCERR1(SetHighpassFilterStatus, *options.highpass_filter);
buildbot@webrtc.org13d67762014-05-02 17:33:29 +0000768 return false;
769 }
770 }
771
kwiberg102c6a62015-10-30 02:47:38 -0700772 if (options.stereo_swapping) {
773 LOG(LS_INFO) << "Stereo swapping enabled? " << *options.stereo_swapping;
774 voep->EnableStereoChannelSwapping(*options.stereo_swapping);
775 if (voep->IsStereoChannelSwappingEnabled() != *options.stereo_swapping) {
776 LOG_RTCERR1(EnableStereoChannelSwapping, *options.stereo_swapping);
buildbot@webrtc.org13d67762014-05-02 17:33:29 +0000777 return false;
778 }
779 }
780
kwiberg102c6a62015-10-30 02:47:38 -0700781 if (options.audio_jitter_buffer_max_packets) {
782 LOG(LS_INFO) << "NetEq capacity is "
783 << *options.audio_jitter_buffer_max_packets;
Henrik Lundin64dad832015-05-11 12:44:23 +0200784 voe_config_.Set<webrtc::NetEqCapacityConfig>(
kwiberg102c6a62015-10-30 02:47:38 -0700785 new webrtc::NetEqCapacityConfig(
786 *options.audio_jitter_buffer_max_packets));
Henrik Lundin64dad832015-05-11 12:44:23 +0200787 }
788
kwiberg102c6a62015-10-30 02:47:38 -0700789 if (options.audio_jitter_buffer_fast_accelerate) {
790 LOG(LS_INFO) << "NetEq fast mode? "
791 << *options.audio_jitter_buffer_fast_accelerate;
Henrik Lundin5263b3c2015-06-01 10:29:41 +0200792 voe_config_.Set<webrtc::NetEqFastAccelerate>(
kwiberg102c6a62015-10-30 02:47:38 -0700793 new webrtc::NetEqFastAccelerate(
794 *options.audio_jitter_buffer_fast_accelerate));
Henrik Lundin5263b3c2015-06-01 10:29:41 +0200795 }
796
kwiberg102c6a62015-10-30 02:47:38 -0700797 if (options.typing_detection) {
798 LOG(LS_INFO) << "Typing detection is enabled? "
799 << *options.typing_detection;
800 if (voep->SetTypingDetectionStatus(*options.typing_detection) == -1) {
buildbot@webrtc.org13d67762014-05-02 17:33:29 +0000801 // In case of error, log the info and continue
kwiberg102c6a62015-10-30 02:47:38 -0700802 LOG_RTCERR1(SetTypingDetectionStatus, *options.typing_detection);
buildbot@webrtc.org13d67762014-05-02 17:33:29 +0000803 }
804 }
805
kwiberg102c6a62015-10-30 02:47:38 -0700806 if (options.adjust_agc_delta) {
807 LOG(LS_INFO) << "Adjust agc delta is " << *options.adjust_agc_delta;
808 if (!AdjustAgcLevel(*options.adjust_agc_delta)) {
buildbot@webrtc.org13d67762014-05-02 17:33:29 +0000809 return false;
810 }
811 }
812
buildbot@webrtc.org1f8a2372014-08-28 10:52:44 +0000813 webrtc::Config config;
814
kwiberg102c6a62015-10-30 02:47:38 -0700815 if (options.delay_agnostic_aec)
816 delay_agnostic_aec_ = options.delay_agnostic_aec;
817 if (delay_agnostic_aec_) {
818 LOG(LS_INFO) << "Delay agnostic aec is enabled? " << *delay_agnostic_aec_;
henrik.lundin0f133b92015-07-02 00:17:55 -0700819 config.Set<webrtc::DelayAgnostic>(
kwiberg102c6a62015-10-30 02:47:38 -0700820 new webrtc::DelayAgnostic(*delay_agnostic_aec_));
Bjorn Volckerbf395c12015-03-25 22:45:56 +0100821 }
822
kwiberg102c6a62015-10-30 02:47:38 -0700823 if (options.extended_filter_aec) {
824 extended_filter_aec_ = options.extended_filter_aec;
825 }
826 if (extended_filter_aec_) {
827 LOG(LS_INFO) << "Extended filter aec is enabled? " << *extended_filter_aec_;
Henrik Lundin441f6342015-06-09 16:03:13 +0200828 config.Set<webrtc::ExtendedFilter>(
kwiberg102c6a62015-10-30 02:47:38 -0700829 new webrtc::ExtendedFilter(*extended_filter_aec_));
buildbot@webrtc.org1f8a2372014-08-28 10:52:44 +0000830 }
831
kwiberg102c6a62015-10-30 02:47:38 -0700832 if (options.experimental_ns) {
833 experimental_ns_ = options.experimental_ns;
834 }
835 if (experimental_ns_) {
836 LOG(LS_INFO) << "Experimental ns is enabled? " << *experimental_ns_;
buildbot@webrtc.org1f8a2372014-08-28 10:52:44 +0000837 config.Set<webrtc::ExperimentalNs>(
kwiberg102c6a62015-10-30 02:47:38 -0700838 new webrtc::ExperimentalNs(*experimental_ns_));
buildbot@webrtc.org1f8a2372014-08-28 10:52:44 +0000839 }
buildbot@webrtc.org1f8a2372014-08-28 10:52:44 +0000840
841 // We check audioproc for the benefit of tests, since FakeWebRtcVoiceEngine
842 // returns NULL on audio_processing().
843 webrtc::AudioProcessing* audioproc = voe_wrapper_->base()->audio_processing();
844 if (audioproc) {
845 audioproc->SetExtraOptions(config);
846 }
847
kwiberg102c6a62015-10-30 02:47:38 -0700848 if (options.recording_sample_rate) {
849 LOG(LS_INFO) << "Recording sample rate is "
850 << *options.recording_sample_rate;
851 if (voe_wrapper_->hw()->SetRecordingSampleRate(
852 *options.recording_sample_rate)) {
853 LOG_RTCERR1(SetRecordingSampleRate, *options.recording_sample_rate);
buildbot@webrtc.org13d67762014-05-02 17:33:29 +0000854 }
855 }
856
kwiberg102c6a62015-10-30 02:47:38 -0700857 if (options.playout_sample_rate) {
858 LOG(LS_INFO) << "Playout sample rate is " << *options.playout_sample_rate;
859 if (voe_wrapper_->hw()->SetPlayoutSampleRate(
860 *options.playout_sample_rate)) {
861 LOG_RTCERR1(SetPlayoutSampleRate, *options.playout_sample_rate);
buildbot@webrtc.org13d67762014-05-02 17:33:29 +0000862 }
863 }
864
865 return true;
866}
867
solenberg246b8172015-12-08 09:50:23 -0800868void WebRtcVoiceEngine::SetDefaultDevices() {
solenberg566ef242015-11-06 15:34:49 -0800869 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
kjellanderfcfc8042016-01-14 11:01:09 -0800870#if !defined(WEBRTC_IOS)
solenberg246b8172015-12-08 09:50:23 -0800871 int in_id = kDefaultAudioDeviceId;
872 int out_id = kDefaultAudioDeviceId;
873 LOG(LS_INFO) << "Setting microphone to (id=" << in_id
874 << ") and speaker to (id=" << out_id << ")";
buildbot@webrtc.org13d67762014-05-02 17:33:29 +0000875
solenbergc1a1b352015-09-22 13:31:20 -0700876 bool ret = true;
solenberg246b8172015-12-08 09:50:23 -0800877 if (voe_wrapper_->hw()->SetRecordingDevice(in_id) == -1) {
878 LOG_RTCERR1(SetRecordingDevice, in_id);
buildbot@webrtc.org13d67762014-05-02 17:33:29 +0000879 ret = false;
880 }
solenberg246b8172015-12-08 09:50:23 -0800881 webrtc::AudioProcessing* ap = voe()->base()->audio_processing();
882 if (ap) {
883 ap->Initialize();
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000884 }
885
solenberg246b8172015-12-08 09:50:23 -0800886 if (voe_wrapper_->hw()->SetPlayoutDevice(out_id) == -1) {
887 LOG_RTCERR1(SetPlayoutDevice, out_id);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000888 ret = false;
889 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000890
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000891 if (ret) {
solenberg246b8172015-12-08 09:50:23 -0800892 LOG(LS_INFO) << "Set microphone to (id=" << in_id
893 << ") and speaker to (id=" << out_id << ")";
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000894 }
kjellanderfcfc8042016-01-14 11:01:09 -0800895#endif // !WEBRTC_IOS
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000896}
897
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000898bool WebRtcVoiceEngine::GetOutputVolume(int* level) {
solenberg566ef242015-11-06 15:34:49 -0800899 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000900 unsigned int ulevel;
901 if (voe_wrapper_->volume()->GetSpeakerVolume(ulevel) == -1) {
902 LOG_RTCERR1(GetSpeakerVolume, level);
903 return false;
904 }
905 *level = ulevel;
906 return true;
907}
908
909bool WebRtcVoiceEngine::SetOutputVolume(int level) {
solenberg566ef242015-11-06 15:34:49 -0800910 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
henrikg91d6ede2015-09-17 00:24:34 -0700911 RTC_DCHECK(level >= 0 && level <= 255);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000912 if (voe_wrapper_->volume()->SetSpeakerVolume(level) == -1) {
913 LOG_RTCERR1(SetSpeakerVolume, level);
914 return false;
915 }
916 return true;
917}
918
919int WebRtcVoiceEngine::GetInputLevel() {
solenberg566ef242015-11-06 15:34:49 -0800920 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000921 unsigned int ulevel;
922 return (voe_wrapper_->volume()->GetSpeechInputLevel(ulevel) != -1) ?
923 static_cast<int>(ulevel) : -1;
924}
925
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000926const std::vector<AudioCodec>& WebRtcVoiceEngine::codecs() {
solenberg566ef242015-11-06 15:34:49 -0800927 RTC_DCHECK(signal_thread_checker_.CalledOnValidThread());
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000928 return codecs_;
929}
930
Stefan Holmer9d69c3f2015-12-07 10:45:43 +0100931RtpCapabilities WebRtcVoiceEngine::GetCapabilities() const {
solenberg566ef242015-11-06 15:34:49 -0800932 RTC_DCHECK(signal_thread_checker_.CalledOnValidThread());
Stefan Holmer9d69c3f2015-12-07 10:45:43 +0100933 RtpCapabilities capabilities;
934 capabilities.header_extensions.push_back(RtpHeaderExtension(
935 kRtpAudioLevelHeaderExtension, kRtpAudioLevelHeaderExtensionDefaultId));
936 capabilities.header_extensions.push_back(
937 RtpHeaderExtension(kRtpAbsoluteSenderTimeHeaderExtension,
938 kRtpAbsoluteSenderTimeHeaderExtensionDefaultId));
stefanba4c0e42016-02-04 04:12:24 -0800939 if (webrtc::field_trial::FindFullName("WebRTC-Audio-SendSideBwe") ==
940 "Enabled") {
941 capabilities.header_extensions.push_back(RtpHeaderExtension(
942 kRtpTransportSequenceNumberHeaderExtension,
943 kRtpTransportSequenceNumberHeaderExtensionDefaultId));
944 }
Stefan Holmer9d69c3f2015-12-07 10:45:43 +0100945 return capabilities;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000946}
947
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000948int WebRtcVoiceEngine::GetLastEngineError() {
solenberg566ef242015-11-06 15:34:49 -0800949 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000950 return voe_wrapper_->error();
951}
952
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000953void WebRtcVoiceEngine::Print(webrtc::TraceLevel level, const char* trace,
954 int length) {
solenberg566ef242015-11-06 15:34:49 -0800955 // Note: This callback can happen on any thread!
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000956 rtc::LoggingSeverity sev = rtc::LS_VERBOSE;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000957 if (level == webrtc::kTraceError || level == webrtc::kTraceCritical)
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000958 sev = rtc::LS_ERROR;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000959 else if (level == webrtc::kTraceWarning)
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000960 sev = rtc::LS_WARNING;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000961 else if (level == webrtc::kTraceStateInfo || level == webrtc::kTraceInfo)
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000962 sev = rtc::LS_INFO;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000963 else if (level == webrtc::kTraceTerseInfo)
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000964 sev = rtc::LS_INFO;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000965
solenberg72e29d22016-03-08 06:35:16 -0800966 // Skip past boilerplate prefix text.
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000967 if (length < 72) {
968 std::string msg(trace, length);
969 LOG(LS_ERROR) << "Malformed webrtc log message: ";
970 LOG_V(sev) << msg;
971 } else {
972 std::string msg(trace + 71, length - 72);
Peter Boströmd5c75b12015-09-23 13:24:32 +0200973 LOG_V(sev) << "webrtc: " << msg;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000974 }
975}
976
solenberg63b34542015-09-29 06:06:31 -0700977void WebRtcVoiceEngine::RegisterChannel(WebRtcVoiceMediaChannel* channel) {
solenberg566ef242015-11-06 15:34:49 -0800978 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
979 RTC_DCHECK(channel);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000980 channels_.push_back(channel);
981}
982
solenberg63b34542015-09-29 06:06:31 -0700983void WebRtcVoiceEngine::UnregisterChannel(WebRtcVoiceMediaChannel* channel) {
solenberg566ef242015-11-06 15:34:49 -0800984 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
solenberg63b34542015-09-29 06:06:31 -0700985 auto it = std::find(channels_.begin(), channels_.end(), channel);
solenberg566ef242015-11-06 15:34:49 -0800986 RTC_DCHECK(it != channels_.end());
987 channels_.erase(it);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000988}
989
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000990// Adjusts the default AGC target level by the specified delta.
991// NB: If we start messing with other config fields, we'll want
992// to save the current webrtc::AgcConfig as well.
993bool WebRtcVoiceEngine::AdjustAgcLevel(int delta) {
solenberg566ef242015-11-06 15:34:49 -0800994 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000995 webrtc::AgcConfig config = default_agc_config_;
996 config.targetLeveldBOv -= delta;
997
998 LOG(LS_INFO) << "Adjusting AGC level from default -"
999 << default_agc_config_.targetLeveldBOv << "dB to -"
1000 << config.targetLeveldBOv << "dB";
1001
1002 if (voe_wrapper_->processing()->SetAgcConfig(config) == -1) {
1003 LOG_RTCERR1(SetAgcConfig, config.targetLeveldBOv);
1004 return false;
1005 }
1006 return true;
1007}
1008
ivocd66b44d2016-01-15 03:06:36 -08001009bool WebRtcVoiceEngine::StartAecDump(rtc::PlatformFile file,
1010 int64_t max_size_bytes) {
solenberg566ef242015-11-06 15:34:49 -08001011 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00001012 FILE* aec_dump_file_stream = rtc::FdopenPlatformFileForWriting(file);
wu@webrtc.orga8910d22014-01-23 22:12:45 +00001013 if (!aec_dump_file_stream) {
1014 LOG(LS_ERROR) << "Could not open AEC dump file stream.";
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00001015 if (!rtc::ClosePlatformFile(file))
wu@webrtc.orga8910d22014-01-23 22:12:45 +00001016 LOG(LS_WARNING) << "Could not close file.";
1017 return false;
1018 }
wu@webrtc.orga9890802013-12-13 00:21:03 +00001019 StopAecDump();
ivocd66b44d2016-01-15 03:06:36 -08001020 if (voe_wrapper_->base()->audio_processing()->StartDebugRecording(
1021 aec_dump_file_stream, max_size_bytes) !=
wu@webrtc.orga9890802013-12-13 00:21:03 +00001022 webrtc::AudioProcessing::kNoError) {
wu@webrtc.orga8910d22014-01-23 22:12:45 +00001023 LOG_RTCERR0(StartDebugRecording);
1024 fclose(aec_dump_file_stream);
wu@webrtc.orga9890802013-12-13 00:21:03 +00001025 return false;
1026 }
1027 is_dumping_aec_ = true;
1028 return true;
wu@webrtc.orga9890802013-12-13 00:21:03 +00001029}
1030
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001031void WebRtcVoiceEngine::StartAecDump(const std::string& filename) {
solenberg566ef242015-11-06 15:34:49 -08001032 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001033 if (!is_dumping_aec_) {
1034 // Start dumping AEC when we are not dumping.
ivocd66b44d2016-01-15 03:06:36 -08001035 if (voe_wrapper_->base()->audio_processing()->StartDebugRecording(
1036 filename.c_str(), -1) != webrtc::AudioProcessing::kNoError) {
wu@webrtc.orga9890802013-12-13 00:21:03 +00001037 LOG_RTCERR1(StartDebugRecording, filename.c_str());
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001038 } else {
1039 is_dumping_aec_ = true;
1040 }
1041 }
1042}
1043
1044void WebRtcVoiceEngine::StopAecDump() {
solenberg566ef242015-11-06 15:34:49 -08001045 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001046 if (is_dumping_aec_) {
1047 // Stop dumping AEC when we are dumping.
ivocd66b44d2016-01-15 03:06:36 -08001048 if (voe_wrapper_->base()->audio_processing()->StopDebugRecording() !=
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001049 webrtc::AudioProcessing::kNoError) {
1050 LOG_RTCERR0(StopDebugRecording);
1051 }
1052 is_dumping_aec_ = false;
1053 }
1054}
1055
ivoc112a3d82015-10-16 02:22:18 -07001056bool WebRtcVoiceEngine::StartRtcEventLog(rtc::PlatformFile file) {
solenberg566ef242015-11-06 15:34:49 -08001057 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
ivoc20834ca2016-02-04 06:33:37 -08001058 webrtc::RtcEventLog* event_log = voe_wrapper_->codec()->GetEventLog();
1059 if (event_log) {
1060 return event_log->StartLogging(file);
1061 }
1062 LOG_RTCERR0(StartRtcEventLog);
1063 return false;
ivoc112a3d82015-10-16 02:22:18 -07001064}
1065
1066void WebRtcVoiceEngine::StopRtcEventLog() {
solenberg566ef242015-11-06 15:34:49 -08001067 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
ivoc20834ca2016-02-04 06:33:37 -08001068 webrtc::RtcEventLog* event_log = voe_wrapper_->codec()->GetEventLog();
1069 if (event_log) {
1070 event_log->StopLogging();
1071 return;
1072 }
1073 LOG_RTCERR0(StopRtcEventLog);
ivoc112a3d82015-10-16 02:22:18 -07001074}
1075
solenberg0a617e22015-10-20 15:49:38 -07001076int WebRtcVoiceEngine::CreateVoEChannel() {
solenberg566ef242015-11-06 15:34:49 -08001077 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
solenberg0a617e22015-10-20 15:49:38 -07001078 return voe_wrapper_->base()->CreateChannel(voe_config_);
sergeyu@chromium.org5bc25c42013-12-05 00:24:06 +00001079}
1080
solenbergc96df772015-10-21 13:01:53 -07001081class WebRtcVoiceMediaChannel::WebRtcAudioSendStream
Taylor Brandstetter1a018dc2016-03-08 12:37:39 -08001082 : public AudioSource::Sink {
mallinath@webrtc.org67ee6b92014-02-03 16:57:16 +00001083 public:
solenbergc96df772015-10-21 13:01:53 -07001084 WebRtcAudioSendStream(int ch, webrtc::AudioTransport* voe_audio_transport,
solenberg3a941542015-11-16 07:34:50 -08001085 uint32_t ssrc, const std::string& c_name,
1086 const std::vector<webrtc::RtpExtension>& extensions,
1087 webrtc::Call* call)
solenberg7add0582015-11-20 09:59:34 -08001088 : voe_audio_transport_(voe_audio_transport),
solenberg3a941542015-11-16 07:34:50 -08001089 call_(call),
1090 config_(nullptr) {
solenberg85a04962015-10-27 03:35:21 -07001091 RTC_DCHECK_GE(ch, 0);
1092 // TODO(solenberg): Once we're not using FakeWebRtcVoiceEngine anymore:
1093 // RTC_DCHECK(voe_audio_transport);
solenbergc96df772015-10-21 13:01:53 -07001094 RTC_DCHECK(call);
solenberg85a04962015-10-27 03:35:21 -07001095 audio_capture_thread_checker_.DetachFromThread();
solenberg3a941542015-11-16 07:34:50 -08001096 config_.rtp.ssrc = ssrc;
1097 config_.rtp.c_name = c_name;
1098 config_.voe_channel_id = ch;
1099 RecreateAudioSendStream(extensions);
solenbergc96df772015-10-21 13:01:53 -07001100 }
solenberg3a941542015-11-16 07:34:50 -08001101
solenbergc96df772015-10-21 13:01:53 -07001102 ~WebRtcAudioSendStream() override {
solenberg566ef242015-11-06 15:34:49 -08001103 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
Taylor Brandstetter1a018dc2016-03-08 12:37:39 -08001104 ClearSource();
solenbergc96df772015-10-21 13:01:53 -07001105 call_->DestroyAudioSendStream(stream_);
1106 }
henrike@webrtc.org1e09a712013-07-26 19:17:59 +00001107
solenberg3a941542015-11-16 07:34:50 -08001108 void RecreateAudioSendStream(
1109 const std::vector<webrtc::RtpExtension>& extensions) {
1110 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
1111 if (stream_) {
1112 call_->DestroyAudioSendStream(stream_);
1113 stream_ = nullptr;
1114 }
1115 config_.rtp.extensions = extensions;
1116 RTC_DCHECK(!stream_);
1117 stream_ = call_->CreateAudioSendStream(config_);
1118 RTC_CHECK(stream_);
1119 }
1120
solenberg8842c3e2016-03-11 03:06:41 -08001121 bool SendTelephoneEvent(int payload_type, int event, int duration_ms) {
Fredrik Solenbergb5727682015-12-04 15:22:19 +01001122 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
1123 RTC_DCHECK(stream_);
1124 return stream_->SendTelephoneEvent(payload_type, event, duration_ms);
1125 }
1126
Taylor Brandstetter1a018dc2016-03-08 12:37:39 -08001127 void SetSend(bool send) {
1128 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
1129 send_ = send;
1130 UpdateSendState();
1131 }
1132
solenberg3a941542015-11-16 07:34:50 -08001133 webrtc::AudioSendStream::Stats GetStats() const {
1134 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
1135 RTC_DCHECK(stream_);
1136 return stream_->GetStats();
1137 }
1138
Taylor Brandstetter1a018dc2016-03-08 12:37:39 -08001139 // Starts the sending by setting ourselves as a sink to the AudioSource to
1140 // get data callbacks.
henrike@webrtc.orga7b98182014-02-21 15:51:43 +00001141 // This method is called on the libjingle worker thread.
mallinath@webrtc.org67ee6b92014-02-03 16:57:16 +00001142 // TODO(xians): Make sure Start() is called only once.
Taylor Brandstetter1a018dc2016-03-08 12:37:39 -08001143 void SetSource(AudioSource* source) {
solenberg566ef242015-11-06 15:34:49 -08001144 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
Taylor Brandstetter1a018dc2016-03-08 12:37:39 -08001145 RTC_DCHECK(source);
1146 if (source_) {
1147 RTC_DCHECK(source_ == source);
mallinath@webrtc.org67ee6b92014-02-03 16:57:16 +00001148 return;
1149 }
Taylor Brandstetter1a018dc2016-03-08 12:37:39 -08001150 source->SetSink(this);
1151 source_ = source;
1152 UpdateSendState();
mallinath@webrtc.org67ee6b92014-02-03 16:57:16 +00001153 }
1154
Taylor Brandstetter1a018dc2016-03-08 12:37:39 -08001155 // Stops sending by setting the sink of the AudioSource to nullptr. No data
mallinath@webrtc.org67ee6b92014-02-03 16:57:16 +00001156 // callback will be received after this method.
henrike@webrtc.orga7b98182014-02-21 15:51:43 +00001157 // This method is called on the libjingle worker thread.
Taylor Brandstetter1a018dc2016-03-08 12:37:39 -08001158 void ClearSource() {
solenberg566ef242015-11-06 15:34:49 -08001159 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
Taylor Brandstetter1a018dc2016-03-08 12:37:39 -08001160 if (source_) {
1161 source_->SetSink(nullptr);
1162 source_ = nullptr;
solenberg98c68862015-10-09 03:27:14 -07001163 }
Taylor Brandstetter1a018dc2016-03-08 12:37:39 -08001164 UpdateSendState();
mallinath@webrtc.org67ee6b92014-02-03 16:57:16 +00001165 }
1166
Taylor Brandstetter1a018dc2016-03-08 12:37:39 -08001167 // AudioSource::Sink implementation.
henrike@webrtc.orga7b98182014-02-21 15:51:43 +00001168 // This method is called on the audio thread.
kjellander@webrtc.org14665ff2015-03-04 12:58:35 +00001169 void OnData(const void* audio_data,
1170 int bits_per_sample,
1171 int sample_rate,
Peter Kasting69558702016-01-12 16:26:35 -08001172 size_t number_of_channels,
Peter Kastingdce40cf2015-08-24 14:52:23 -07001173 size_t number_of_frames) override {
solenberg566ef242015-11-06 15:34:49 -08001174 RTC_DCHECK(!worker_thread_checker_.CalledOnValidThread());
solenberg85a04962015-10-27 03:35:21 -07001175 RTC_DCHECK(audio_capture_thread_checker_.CalledOnValidThread());
solenbergc96df772015-10-21 13:01:53 -07001176 RTC_DCHECK(voe_audio_transport_);
solenberg7add0582015-11-20 09:59:34 -08001177 voe_audio_transport_->OnData(config_.voe_channel_id,
henrike@webrtc.orga7b98182014-02-21 15:51:43 +00001178 audio_data,
1179 bits_per_sample,
1180 sample_rate,
1181 number_of_channels,
1182 number_of_frames);
henrike@webrtc.orga7b98182014-02-21 15:51:43 +00001183 }
1184
Taylor Brandstetter1a018dc2016-03-08 12:37:39 -08001185 // Callback from the |source_| when it is going away. In case Start() has
henrike@webrtc.orga7b98182014-02-21 15:51:43 +00001186 // never been called, this callback won't be triggered.
kjellander@webrtc.org14665ff2015-03-04 12:58:35 +00001187 void OnClose() override {
solenberg566ef242015-11-06 15:34:49 -08001188 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
Taylor Brandstetter1a018dc2016-03-08 12:37:39 -08001189 // Set |source_| to nullptr to make sure no more callback will get into
1190 // the source.
1191 source_ = nullptr;
1192 UpdateSendState();
mallinath@webrtc.org67ee6b92014-02-03 16:57:16 +00001193 }
1194
1195 // Accessor to the VoE channel ID.
solenberg85a04962015-10-27 03:35:21 -07001196 int channel() const {
solenberg566ef242015-11-06 15:34:49 -08001197 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
solenberg7add0582015-11-20 09:59:34 -08001198 return config_.voe_channel_id;
solenberg85a04962015-10-27 03:35:21 -07001199 }
mallinath@webrtc.org67ee6b92014-02-03 16:57:16 +00001200
1201 private:
Taylor Brandstetter1a018dc2016-03-08 12:37:39 -08001202 void UpdateSendState() {
1203 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
1204 RTC_DCHECK(stream_);
1205 if (send_ && source_ != nullptr) {
1206 stream_->Start();
1207 } else { // !send || source_ = nullptr
1208 stream_->Stop();
1209 }
1210 }
1211
solenberg566ef242015-11-06 15:34:49 -08001212 rtc::ThreadChecker worker_thread_checker_;
solenberg85a04962015-10-27 03:35:21 -07001213 rtc::ThreadChecker audio_capture_thread_checker_;
solenbergc96df772015-10-21 13:01:53 -07001214 webrtc::AudioTransport* const voe_audio_transport_ = nullptr;
1215 webrtc::Call* call_ = nullptr;
solenberg3a941542015-11-16 07:34:50 -08001216 webrtc::AudioSendStream::Config config_;
1217 // The stream is owned by WebRtcAudioSendStream and may be reallocated if
1218 // configuration changes.
solenbergc96df772015-10-21 13:01:53 -07001219 webrtc::AudioSendStream* stream_ = nullptr;
mallinath@webrtc.org67ee6b92014-02-03 16:57:16 +00001220
Taylor Brandstetter1a018dc2016-03-08 12:37:39 -08001221 // Raw pointer to AudioSource owned by LocalAudioTrackHandler.
mallinath@webrtc.org67ee6b92014-02-03 16:57:16 +00001222 // PeerConnection will make sure invalidating the pointer before the object
1223 // goes away.
Taylor Brandstetter1a018dc2016-03-08 12:37:39 -08001224 AudioSource* source_ = nullptr;
1225 bool send_ = false;
henrike@webrtc.orga7b98182014-02-21 15:51:43 +00001226
solenbergc96df772015-10-21 13:01:53 -07001227 RTC_DISALLOW_IMPLICIT_CONSTRUCTORS(WebRtcAudioSendStream);
1228};
1229
1230class WebRtcVoiceMediaChannel::WebRtcAudioReceiveStream {
1231 public:
stefanba4c0e42016-02-04 04:12:24 -08001232 WebRtcAudioReceiveStream(int ch,
1233 uint32_t remote_ssrc,
1234 uint32_t local_ssrc,
1235 bool use_transport_cc,
1236 const std::string& sync_group,
solenberg7add0582015-11-20 09:59:34 -08001237 const std::vector<webrtc::RtpExtension>& extensions,
1238 webrtc::Call* call)
stefanba4c0e42016-02-04 04:12:24 -08001239 : call_(call), config_() {
solenberg7add0582015-11-20 09:59:34 -08001240 RTC_DCHECK_GE(ch, 0);
1241 RTC_DCHECK(call);
1242 config_.rtp.remote_ssrc = remote_ssrc;
1243 config_.rtp.local_ssrc = local_ssrc;
1244 config_.voe_channel_id = ch;
1245 config_.sync_group = sync_group;
stefanba4c0e42016-02-04 04:12:24 -08001246 RecreateAudioReceiveStream(use_transport_cc, extensions);
solenberg7add0582015-11-20 09:59:34 -08001247 }
solenbergc96df772015-10-21 13:01:53 -07001248
solenberg7add0582015-11-20 09:59:34 -08001249 ~WebRtcAudioReceiveStream() {
1250 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
1251 call_->DestroyAudioReceiveStream(stream_);
1252 }
1253
1254 void RecreateAudioReceiveStream(
1255 const std::vector<webrtc::RtpExtension>& extensions) {
1256 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
stefanba4c0e42016-02-04 04:12:24 -08001257 RecreateAudioReceiveStream(config_.rtp.transport_cc, extensions);
solenberg7add0582015-11-20 09:59:34 -08001258 }
stefanba4c0e42016-02-04 04:12:24 -08001259 void RecreateAudioReceiveStream(bool use_transport_cc) {
solenberg7add0582015-11-20 09:59:34 -08001260 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
stefanba4c0e42016-02-04 04:12:24 -08001261 RecreateAudioReceiveStream(use_transport_cc, config_.rtp.extensions);
solenberg7add0582015-11-20 09:59:34 -08001262 }
1263
1264 webrtc::AudioReceiveStream::Stats GetStats() const {
1265 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
1266 RTC_DCHECK(stream_);
1267 return stream_->GetStats();
1268 }
1269
1270 int channel() const {
1271 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
1272 return config_.voe_channel_id;
1273 }
solenbergc96df772015-10-21 13:01:53 -07001274
kwiberg686a8ef2016-02-26 03:00:35 -08001275 void SetRawAudioSink(std::unique_ptr<webrtc::AudioSinkInterface> sink) {
Tommif888bb52015-12-12 01:37:01 +01001276 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
kwiberg686a8ef2016-02-26 03:00:35 -08001277 stream_->SetSink(std::move(sink));
Tommif888bb52015-12-12 01:37:01 +01001278 }
1279
solenbergc96df772015-10-21 13:01:53 -07001280 private:
stefanba4c0e42016-02-04 04:12:24 -08001281 void RecreateAudioReceiveStream(
1282 bool use_transport_cc,
solenberg7add0582015-11-20 09:59:34 -08001283 const std::vector<webrtc::RtpExtension>& extensions) {
1284 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
1285 if (stream_) {
1286 call_->DestroyAudioReceiveStream(stream_);
1287 stream_ = nullptr;
1288 }
1289 config_.rtp.extensions = extensions;
stefanba4c0e42016-02-04 04:12:24 -08001290 config_.rtp.transport_cc = use_transport_cc;
solenberg7add0582015-11-20 09:59:34 -08001291 RTC_DCHECK(!stream_);
1292 stream_ = call_->CreateAudioReceiveStream(config_);
1293 RTC_CHECK(stream_);
1294 }
1295
1296 rtc::ThreadChecker worker_thread_checker_;
1297 webrtc::Call* call_ = nullptr;
1298 webrtc::AudioReceiveStream::Config config_;
1299 // The stream is owned by WebRtcAudioReceiveStream and may be reallocated if
1300 // configuration changes.
1301 webrtc::AudioReceiveStream* stream_ = nullptr;
solenbergc96df772015-10-21 13:01:53 -07001302
1303 RTC_DISALLOW_IMPLICIT_CONSTRUCTORS(WebRtcAudioReceiveStream);
henrike@webrtc.org1e09a712013-07-26 19:17:59 +00001304};
1305
Fredrik Solenberg709ed672015-09-15 12:26:33 +02001306WebRtcVoiceMediaChannel::WebRtcVoiceMediaChannel(WebRtcVoiceEngine* engine,
nisse51542be2016-02-12 02:27:06 -08001307 const MediaConfig& config,
Fredrik Solenbergb071a192015-09-17 16:42:56 +02001308 const AudioOptions& options,
Fredrik Solenberg709ed672015-09-15 12:26:33 +02001309 webrtc::Call* call)
nisse51542be2016-02-12 02:27:06 -08001310 : VoiceMediaChannel(config), engine_(engine), call_(call) {
solenberg0a617e22015-10-20 15:49:38 -07001311 LOG(LS_VERBOSE) << "WebRtcVoiceMediaChannel::WebRtcVoiceMediaChannel";
solenberg566ef242015-11-06 15:34:49 -08001312 RTC_DCHECK(call);
solenberg0a617e22015-10-20 15:49:38 -07001313 engine->RegisterChannel(this);
Fredrik Solenbergb071a192015-09-17 16:42:56 +02001314 SetOptions(options);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001315}
1316
1317WebRtcVoiceMediaChannel::~WebRtcVoiceMediaChannel() {
solenberg566ef242015-11-06 15:34:49 -08001318 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
solenberg0a617e22015-10-20 15:49:38 -07001319 LOG(LS_VERBOSE) << "WebRtcVoiceMediaChannel::~WebRtcVoiceMediaChannel";
solenberg7add0582015-11-20 09:59:34 -08001320 // TODO(solenberg): Should be able to delete the streams directly, without
1321 // going through RemoveNnStream(), once stream objects handle
1322 // all (de)configuration.
solenbergc96df772015-10-21 13:01:53 -07001323 while (!send_streams_.empty()) {
1324 RemoveSendStream(send_streams_.begin()->first);
solenbergd97ec302015-10-07 01:40:33 -07001325 }
solenberg7add0582015-11-20 09:59:34 -08001326 while (!recv_streams_.empty()) {
1327 RemoveRecvStream(recv_streams_.begin()->first);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001328 }
solenberg0a617e22015-10-20 15:49:38 -07001329 engine()->UnregisterChannel(this);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001330}
1331
nisse51542be2016-02-12 02:27:06 -08001332rtc::DiffServCodePoint WebRtcVoiceMediaChannel::PreferredDscp() const {
1333 return kAudioDscpValue;
1334}
1335
Peter Thatcherc2ee2c82015-08-07 16:05:34 -07001336bool WebRtcVoiceMediaChannel::SetSendParameters(
1337 const AudioSendParameters& params) {
Peter Boströmca8b4042016-03-08 14:24:13 -08001338 TRACE_EVENT0("webrtc", "WebRtcVoiceMediaChannel::SetSendParameters");
solenberg566ef242015-11-06 15:34:49 -08001339 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
solenberg7e4e01a2015-12-02 08:05:01 -08001340 LOG(LS_INFO) << "WebRtcVoiceMediaChannel::SetSendParameters: "
1341 << params.ToString();
Peter Thatcherc2ee2c82015-08-07 16:05:34 -07001342 // TODO(pthatcher): Refactor this to be more clean now that we have
1343 // all the information at once.
solenberg3a941542015-11-16 07:34:50 -08001344
1345 if (!SetSendCodecs(params.codecs)) {
1346 return false;
1347 }
1348
solenberg7e4e01a2015-12-02 08:05:01 -08001349 if (!ValidateRtpExtensions(params.extensions)) {
1350 return false;
1351 }
1352 std::vector<webrtc::RtpExtension> filtered_extensions =
1353 FilterRtpExtensions(params.extensions,
1354 webrtc::RtpExtension::IsSupportedForAudio, true);
1355 if (send_rtp_extensions_ != filtered_extensions) {
1356 send_rtp_extensions_.swap(filtered_extensions);
solenberg3a941542015-11-16 07:34:50 -08001357 for (auto& it : send_streams_) {
1358 it.second->RecreateAudioSendStream(send_rtp_extensions_);
1359 }
1360 }
1361
1362 if (!SetMaxSendBandwidth(params.max_bandwidth_bps)) {
1363 return false;
1364 }
1365 return SetOptions(params.options);
Peter Thatcherc2ee2c82015-08-07 16:05:34 -07001366}
1367
1368bool WebRtcVoiceMediaChannel::SetRecvParameters(
1369 const AudioRecvParameters& params) {
Peter Boströmca8b4042016-03-08 14:24:13 -08001370 TRACE_EVENT0("webrtc", "WebRtcVoiceMediaChannel::SetRecvParameters");
solenberg566ef242015-11-06 15:34:49 -08001371 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
solenberg7e4e01a2015-12-02 08:05:01 -08001372 LOG(LS_INFO) << "WebRtcVoiceMediaChannel::SetRecvParameters: "
1373 << params.ToString();
Peter Thatcherc2ee2c82015-08-07 16:05:34 -07001374 // TODO(pthatcher): Refactor this to be more clean now that we have
1375 // all the information at once.
solenberg7add0582015-11-20 09:59:34 -08001376
1377 if (!SetRecvCodecs(params.codecs)) {
1378 return false;
1379 }
1380
solenberg7e4e01a2015-12-02 08:05:01 -08001381 if (!ValidateRtpExtensions(params.extensions)) {
1382 return false;
1383 }
1384 std::vector<webrtc::RtpExtension> filtered_extensions =
1385 FilterRtpExtensions(params.extensions,
1386 webrtc::RtpExtension::IsSupportedForAudio, false);
1387 if (recv_rtp_extensions_ != filtered_extensions) {
1388 recv_rtp_extensions_.swap(filtered_extensions);
solenberg7add0582015-11-20 09:59:34 -08001389 for (auto& it : recv_streams_) {
1390 it.second->RecreateAudioReceiveStream(recv_rtp_extensions_);
1391 }
1392 }
solenberg7add0582015-11-20 09:59:34 -08001393 return true;
Peter Thatcherc2ee2c82015-08-07 16:05:34 -07001394}
1395
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001396bool WebRtcVoiceMediaChannel::SetOptions(const AudioOptions& options) {
solenberg566ef242015-11-06 15:34:49 -08001397 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001398 LOG(LS_INFO) << "Setting voice channel options: "
1399 << options.ToString();
1400
1401 // We retain all of the existing options, and apply the given ones
1402 // on top. This means there is no way to "clear" options such that
1403 // they go back to the engine default.
1404 options_.SetAll(options);
solenberg246b8172015-12-08 09:50:23 -08001405 if (!engine()->ApplyOptions(options_)) {
1406 LOG(LS_WARNING) <<
1407 "Failed to apply engine options during channel SetOptions.";
1408 return false;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001409 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001410 LOG(LS_INFO) << "Set voice channel options. Current options: "
1411 << options_.ToString();
1412 return true;
1413}
1414
1415bool WebRtcVoiceMediaChannel::SetRecvCodecs(
1416 const std::vector<AudioCodec>& codecs) {
solenberg566ef242015-11-06 15:34:49 -08001417 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
solenberg8fb30c32015-10-13 03:06:58 -07001418
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001419 // Set the payload types to be used for incoming media.
solenberg0b675462015-10-09 01:37:09 -07001420 LOG(LS_INFO) << "Setting receive voice codecs.";
solenberg0b675462015-10-09 01:37:09 -07001421
1422 if (!VerifyUniquePayloadTypes(codecs)) {
1423 LOG(LS_ERROR) << "Codec payload types overlap.";
1424 return false;
1425 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001426
1427 std::vector<AudioCodec> new_codecs;
1428 // Find all new codecs. We allow adding new codecs but don't allow changing
1429 // the payload type of codecs that is already configured since we might
1430 // already be receiving packets with that payload type.
Fredrik Solenbergaf9fb212015-08-26 10:45:53 +02001431 for (const AudioCodec& codec : codecs) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001432 AudioCodec old_codec;
Fredrik Solenbergaf9fb212015-08-26 10:45:53 +02001433 if (FindCodec(recv_codecs_, codec, &old_codec)) {
1434 if (old_codec.id != codec.id) {
1435 LOG(LS_ERROR) << codec.name << " payload type changed.";
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001436 return false;
1437 }
1438 } else {
Fredrik Solenbergaf9fb212015-08-26 10:45:53 +02001439 new_codecs.push_back(codec);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001440 }
1441 }
1442 if (new_codecs.empty()) {
1443 // There are no new codecs to configure. Already configured codecs are
1444 // never removed.
1445 return true;
1446 }
1447
1448 if (playout_) {
1449 // Receive codecs can not be changed while playing. So we temporarily
1450 // pause playout.
1451 PausePlayout();
1452 }
1453
solenberg26c8c912015-11-27 04:00:25 -08001454 bool result = true;
1455 for (const AudioCodec& codec : new_codecs) {
solenberg72e29d22016-03-08 06:35:16 -08001456 webrtc::CodecInst voe_codec = {0};
solenberg26c8c912015-11-27 04:00:25 -08001457 if (WebRtcVoiceEngine::ToCodecInst(codec, &voe_codec)) {
1458 LOG(LS_INFO) << ToString(codec);
1459 voe_codec.pltype = codec.id;
1460 for (const auto& ch : recv_streams_) {
1461 if (engine()->voe()->codec()->SetRecPayloadType(
1462 ch.second->channel(), voe_codec) == -1) {
1463 LOG_RTCERR2(SetRecPayloadType, ch.second->channel(),
1464 ToString(voe_codec));
1465 result = false;
1466 }
1467 }
1468 } else {
1469 LOG(LS_WARNING) << "Unknown codec " << ToString(codec);
1470 result = false;
1471 break;
1472 }
1473 }
Fredrik Solenbergaf9fb212015-08-26 10:45:53 +02001474 if (result) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001475 recv_codecs_ = codecs;
1476 }
1477
1478 if (desired_playout_ && !playout_) {
1479 ResumePlayout();
1480 }
Fredrik Solenbergaf9fb212015-08-26 10:45:53 +02001481 return result;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001482}
1483
solenberg72e29d22016-03-08 06:35:16 -08001484// Utility function called from SetSendParameters() to extract current send
1485// codec settings from the given list of codecs (originally from SDP). Both send
1486// and receive streams may be reconfigured based on the new settings.
wu@webrtc.orgcadf9042013-08-30 21:24:16 +00001487bool WebRtcVoiceMediaChannel::SetSendCodecs(
1488 const std::vector<AudioCodec>& codecs) {
solenberg566ef242015-11-06 15:34:49 -08001489 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
Fredrik Solenbergb5727682015-12-04 15:22:19 +01001490 // TODO(solenberg): Validate input - that payload types don't overlap, are
1491 // within range, filter out codecs we don't support,
solenberg31642aa2016-03-14 08:00:37 -07001492 // redundant codecs etc - the same way it is done for
1493 // RtpHeaderExtensions.
solenbergd97ec302015-10-07 01:40:33 -07001494
Fredrik Solenbergb5727682015-12-04 15:22:19 +01001495 // Find the DTMF telephone event "codec" payload type.
1496 dtmf_payload_type_ = rtc::Optional<int>();
Fredrik Solenbergaf9fb212015-08-26 10:45:53 +02001497 for (const AudioCodec& codec : codecs) {
Fredrik Solenbergaf9fb212015-08-26 10:45:53 +02001498 if (IsCodec(codec, kDtmfCodecName)) {
solenberg31642aa2016-03-14 08:00:37 -07001499 if (codec.id < kMinPayloadType || codec.id > kMaxPayloadType) {
1500 return false;
1501 }
Fredrik Solenbergb5727682015-12-04 15:22:19 +01001502 dtmf_payload_type_ = rtc::Optional<int>(codec.id);
1503 break;
wu@webrtc.orgcadf9042013-08-30 21:24:16 +00001504 }
1505 }
1506
solenberg72e29d22016-03-08 06:35:16 -08001507 // Scan through the list to figure out the codec to use for sending, along
1508 // with the proper configuration for VAD, CNG, RED, NACK and Opus-specific
1509 // parameters.
1510 {
1511 SendCodecSpec send_codec_spec;
1512 send_codec_spec.nack_enabled = send_codec_spec_.nack_enabled;
1513
1514 // Find send codec (the first non-telephone-event/CN codec).
1515 const AudioCodec* codec = WebRtcVoiceCodecs::GetPreferredCodec(
1516 codecs, &send_codec_spec.codec_inst, &send_codec_spec.red_payload_type);
1517 if (!codec) {
1518 LOG(LS_WARNING) << "Received empty list of codecs.";
1519 return false;
1520 }
1521
1522 send_codec_spec.transport_cc_enabled = HasTransportCc(*codec);
1523
1524 // This condition is apparently here because Opus does not support RED and
1525 // FEC simultaneously. However, DTX and max playback rate shouldn't have
1526 // such limitations.
1527 // TODO(solenberg): Refactor this logic once we create AudioEncoders here.
1528 if (send_codec_spec.red_payload_type == -1) {
1529 send_codec_spec.nack_enabled = HasNack(*codec);
1530 // For Opus as the send codec, we are to determine inband FEC, maximum
1531 // playback rate, and opus internal dtx.
1532 if (IsCodec(*codec, kOpusCodecName)) {
1533 GetOpusConfig(*codec, &send_codec_spec.codec_inst,
1534 &send_codec_spec.enable_codec_fec,
1535 &send_codec_spec.opus_max_playback_rate,
1536 &send_codec_spec.enable_opus_dtx);
1537 }
1538
1539 // Set packet size if the AudioCodec param kCodecParamPTime is set.
1540 int ptime_ms = 0;
1541 if (codec->GetParam(kCodecParamPTime, &ptime_ms)) {
1542 if (!WebRtcVoiceCodecs::SetPTimeAsPacketSize(
1543 &send_codec_spec.codec_inst, ptime_ms)) {
1544 LOG(LS_WARNING) << "Failed to set packet size for codec "
1545 << send_codec_spec.codec_inst.plname;
1546 return false;
1547 }
1548 }
1549 }
1550
1551 // Loop through the codecs list again to find the CN codec.
1552 // TODO(solenberg): Break out into a separate function?
1553 for (const AudioCodec& codec : codecs) {
1554 // Ignore codecs we don't know about. The negotiation step should prevent
1555 // this, but double-check to be sure.
1556 webrtc::CodecInst voe_codec = {0};
1557 if (!WebRtcVoiceEngine::ToCodecInst(codec, &voe_codec)) {
1558 LOG(LS_WARNING) << "Unknown codec " << ToString(codec);
1559 continue;
1560 }
1561
1562 if (IsCodec(codec, kCnCodecName)) {
1563 // Turn voice activity detection/comfort noise on if supported.
1564 // Set the wideband CN payload type appropriately.
1565 // (narrowband always uses the static payload type 13).
1566 int cng_plfreq = -1;
1567 switch (codec.clockrate) {
1568 case 8000:
1569 case 16000:
1570 case 32000:
1571 cng_plfreq = codec.clockrate;
1572 break;
1573 default:
1574 LOG(LS_WARNING) << "CN frequency " << codec.clockrate
1575 << " not supported.";
1576 continue;
1577 }
1578 send_codec_spec.cng_payload_type = codec.id;
1579 send_codec_spec.cng_plfreq = cng_plfreq;
1580 break;
1581 }
1582 }
1583
1584 // Latch in the new state.
1585 send_codec_spec_ = std::move(send_codec_spec);
1586 }
1587
wu@webrtc.orgcadf9042013-08-30 21:24:16 +00001588 // Cache the codecs in order to configure the channel created later.
solenbergc96df772015-10-21 13:01:53 -07001589 for (const auto& ch : send_streams_) {
solenberg72e29d22016-03-08 06:35:16 -08001590 if (!SetSendCodecs(ch.second->channel())) {
wu@webrtc.orgcadf9042013-08-30 21:24:16 +00001591 return false;
1592 }
1593 }
1594
solenberg72e29d22016-03-08 06:35:16 -08001595 // Set nack status on receive channels.
1596 if (!send_streams_.empty()) {
1597 for (const auto& kv : recv_streams_) {
1598 SetNack(kv.second->channel(), send_codec_spec_.nack_enabled);
1599 }
wu@webrtc.org9dba5252013-08-05 20:36:57 +00001600 }
solenberg0a617e22015-10-20 15:49:38 -07001601
stefanba4c0e42016-02-04 04:12:24 -08001602 // Check if the transport cc feedback has changed on the preferred send codec,
1603 // and in that case reconfigure all receive streams.
solenberg72e29d22016-03-08 06:35:16 -08001604 if (recv_transport_cc_enabled_ != send_codec_spec_.transport_cc_enabled) {
1605 LOG(LS_INFO) << "Recreate all the receive streams because the send "
1606 "codec has changed.";
1607 recv_transport_cc_enabled_ = send_codec_spec_.transport_cc_enabled;
1608 for (auto& kv : recv_streams_) {
1609 kv.second->RecreateAudioReceiveStream(recv_transport_cc_enabled_);
1610 }
1611 }
1612
1613 return true;
1614}
1615
1616// Apply current codec settings to a single voe::Channel used for sending.
1617bool WebRtcVoiceMediaChannel::SetSendCodecs(int channel) {
1618 // Disable VAD, FEC, and RED unless we know the other side wants them.
1619 engine()->voe()->codec()->SetVADStatus(channel, false);
1620 engine()->voe()->rtp()->SetNACKStatus(channel, false, 0);
1621 engine()->voe()->rtp()->SetREDStatus(channel, false);
1622 engine()->voe()->codec()->SetFECStatus(channel, false);
1623
1624 if (send_codec_spec_.red_payload_type != -1) {
1625 // Enable redundant encoding of the specified codec. Treat any
1626 // failure as a fatal internal error.
1627 LOG(LS_INFO) << "Enabling RED on channel " << channel;
1628 if (engine()->voe()->rtp()->SetREDStatus(channel, true,
1629 send_codec_spec_.red_payload_type) == -1) {
1630 LOG_RTCERR3(SetREDStatus, channel, true,
1631 send_codec_spec_.red_payload_type);
1632 return false;
1633 }
1634 }
1635
1636 SetNack(channel, send_codec_spec_.nack_enabled);
1637
1638 // Set the codec immediately, since SetVADStatus() depends on whether
1639 // the current codec is mono or stereo.
1640 if (!SetSendCodec(channel, send_codec_spec_.codec_inst)) {
1641 return false;
1642 }
1643
1644 // FEC should be enabled after SetSendCodec.
1645 if (send_codec_spec_.enable_codec_fec) {
1646 LOG(LS_INFO) << "Attempt to enable codec internal FEC on channel "
1647 << channel;
1648 if (engine()->voe()->codec()->SetFECStatus(channel, true) == -1) {
1649 // Enable codec internal FEC. Treat any failure as fatal internal error.
1650 LOG_RTCERR2(SetFECStatus, channel, true);
1651 return false;
1652 }
1653 }
1654
1655 if (IsCodec(send_codec_spec_.codec_inst, kOpusCodecName)) {
1656 // DTX and maxplaybackrate should be set after SetSendCodec. Because current
1657 // send codec has to be Opus.
1658
1659 // Set Opus internal DTX.
1660 LOG(LS_INFO) << "Attempt to "
1661 << (send_codec_spec_.enable_opus_dtx ? "enable" : "disable")
1662 << " Opus DTX on channel "
1663 << channel;
1664 if (engine()->voe()->codec()->SetOpusDtx(channel,
1665 send_codec_spec_.enable_opus_dtx)) {
1666 LOG_RTCERR2(SetOpusDtx, channel, send_codec_spec_.enable_opus_dtx);
1667 return false;
1668 }
1669
1670 // If opus_max_playback_rate <= 0, the default maximum playback rate
1671 // (48 kHz) will be used.
1672 if (send_codec_spec_.opus_max_playback_rate > 0) {
1673 LOG(LS_INFO) << "Attempt to set maximum playback rate to "
1674 << send_codec_spec_.opus_max_playback_rate
1675 << " Hz on channel "
1676 << channel;
1677 if (engine()->voe()->codec()->SetOpusMaxPlaybackRate(
1678 channel, send_codec_spec_.opus_max_playback_rate) == -1) {
1679 LOG_RTCERR2(SetOpusMaxPlaybackRate, channel,
1680 send_codec_spec_.opus_max_playback_rate);
1681 return false;
stefanba4c0e42016-02-04 04:12:24 -08001682 }
1683 }
1684 }
1685
solenberg72e29d22016-03-08 06:35:16 -08001686 if (send_bitrate_setting_) {
1687 SetSendBitrateInternal(send_bitrate_bps_);
1688 }
1689
1690 // Set the CN payloadtype and the VAD status.
1691 if (send_codec_spec_.cng_payload_type != -1) {
1692 // The CN payload type for 8000 Hz clockrate is fixed at 13.
1693 if (send_codec_spec_.cng_plfreq != 8000) {
1694 webrtc::PayloadFrequencies cn_freq;
1695 switch (send_codec_spec_.cng_plfreq) {
1696 case 16000:
1697 cn_freq = webrtc::kFreq16000Hz;
1698 break;
1699 case 32000:
1700 cn_freq = webrtc::kFreq32000Hz;
1701 break;
1702 default:
1703 RTC_NOTREACHED();
1704 return false;
1705 }
1706 if (engine()->voe()->codec()->SetSendCNPayloadType(
1707 channel, send_codec_spec_.cng_payload_type, cn_freq) == -1) {
1708 LOG_RTCERR3(SetSendCNPayloadType, channel,
1709 send_codec_spec_.cng_payload_type, cn_freq);
1710 // TODO(ajm): This failure condition will be removed from VoE.
1711 // Restore the return here when we update to a new enough webrtc.
1712 //
1713 // Not returning false because the SetSendCNPayloadType will fail if
1714 // the channel is already sending.
1715 // This can happen if the remote description is applied twice, for
1716 // example in the case of ROAP on top of JSEP, where both side will
1717 // send the offer.
1718 }
1719 }
1720
1721 // Only turn on VAD if we have a CN payload type that matches the
1722 // clockrate for the codec we are going to use.
1723 if (send_codec_spec_.cng_plfreq == send_codec_spec_.codec_inst.plfreq &&
1724 send_codec_spec_.codec_inst.channels == 1) {
1725 // TODO(minyue): If CN frequency == 48000 Hz is allowed, consider the
1726 // interaction between VAD and Opus FEC.
1727 LOG(LS_INFO) << "Enabling VAD";
1728 if (engine()->voe()->codec()->SetVADStatus(channel, true) == -1) {
1729 LOG_RTCERR2(SetVADStatus, channel, true);
1730 return false;
1731 }
1732 }
1733 }
solenberg0a617e22015-10-20 15:49:38 -07001734 return true;
wu@webrtc.org9dba5252013-08-05 20:36:57 +00001735}
1736
wu@webrtc.orgcadf9042013-08-30 21:24:16 +00001737void WebRtcVoiceMediaChannel::SetNack(int channel, bool nack_enabled) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001738 if (nack_enabled) {
wu@webrtc.orgcadf9042013-08-30 21:24:16 +00001739 LOG(LS_INFO) << "Enabling NACK for channel " << channel;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001740 engine()->voe()->rtp()->SetNACKStatus(channel, true, kNackMaxPackets);
1741 } else {
wu@webrtc.orgcadf9042013-08-30 21:24:16 +00001742 LOG(LS_INFO) << "Disabling NACK for channel " << channel;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001743 engine()->voe()->rtp()->SetNACKStatus(channel, false, 0);
1744 }
1745}
1746
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001747bool WebRtcVoiceMediaChannel::SetSendCodec(
wu@webrtc.org9dba5252013-08-05 20:36:57 +00001748 int channel, const webrtc::CodecInst& send_codec) {
1749 LOG(LS_INFO) << "Send channel " << channel << " selected voice codec "
1750 << ToString(send_codec) << ", bitrate=" << send_codec.rate;
1751
solenberg72e29d22016-03-08 06:35:16 -08001752 webrtc::CodecInst current_codec = {0};
wu@webrtc.org05e7b442014-04-01 17:44:24 +00001753 if (engine()->voe()->codec()->GetSendCodec(channel, current_codec) == 0 &&
1754 (send_codec == current_codec)) {
1755 // Codec is already configured, we can return without setting it again.
1756 return true;
1757 }
1758
wu@webrtc.org9dba5252013-08-05 20:36:57 +00001759 if (engine()->voe()->codec()->SetSendCodec(channel, send_codec) == -1) {
1760 LOG_RTCERR2(SetSendCodec, channel, ToString(send_codec));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001761 return false;
1762 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001763 return true;
1764}
1765
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001766bool WebRtcVoiceMediaChannel::SetPlayout(bool playout) {
1767 desired_playout_ = playout;
1768 return ChangePlayout(desired_playout_);
1769}
1770
1771bool WebRtcVoiceMediaChannel::PausePlayout() {
1772 return ChangePlayout(false);
1773}
1774
1775bool WebRtcVoiceMediaChannel::ResumePlayout() {
1776 return ChangePlayout(desired_playout_);
1777}
1778
1779bool WebRtcVoiceMediaChannel::ChangePlayout(bool playout) {
Peter Boströmca8b4042016-03-08 14:24:13 -08001780 TRACE_EVENT0("webrtc", "WebRtcVoiceMediaChannel::ChangePlayout");
solenberg566ef242015-11-06 15:34:49 -08001781 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001782 if (playout_ == playout) {
1783 return true;
1784 }
1785
solenberg7add0582015-11-20 09:59:34 -08001786 for (const auto& ch : recv_streams_) {
Fredrik Solenbergaf9fb212015-08-26 10:45:53 +02001787 if (!SetPlayout(ch.second->channel(), playout)) {
henrike@webrtc.org1e09a712013-07-26 19:17:59 +00001788 LOG(LS_ERROR) << "SetPlayout " << playout << " on channel "
Fredrik Solenbergaf9fb212015-08-26 10:45:53 +02001789 << ch.second->channel() << " failed";
solenberg1ac56142015-10-13 03:58:19 -07001790 return false;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001791 }
1792 }
solenberg1ac56142015-10-13 03:58:19 -07001793 playout_ = playout;
1794 return true;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001795}
1796
Taylor Brandstetter1a018dc2016-03-08 12:37:39 -08001797void WebRtcVoiceMediaChannel::SetSend(bool send) {
Peter Boströmca8b4042016-03-08 14:24:13 -08001798 TRACE_EVENT0("webrtc", "WebRtcVoiceMediaChannel::SetSend");
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001799 if (send_ == send) {
Taylor Brandstetter1a018dc2016-03-08 12:37:39 -08001800 return;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001801 }
1802
solenberg246b8172015-12-08 09:50:23 -08001803 // Apply channel specific options when channel is enabled for sending.
Taylor Brandstetter1a018dc2016-03-08 12:37:39 -08001804 if (send) {
solenberg63b34542015-09-29 06:06:31 -07001805 engine()->ApplyOptions(options_);
1806 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001807
wu@webrtc.org9dba5252013-08-05 20:36:57 +00001808 // Change the settings on each send channel.
Taylor Brandstetter1a018dc2016-03-08 12:37:39 -08001809 for (auto& kv : send_streams_) {
1810 kv.second->SetSend(send);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001811 }
wu@webrtc.org9dba5252013-08-05 20:36:57 +00001812
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001813 send_ = send;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001814}
1815
Peter Boström0c4e06b2015-10-07 12:23:21 +02001816bool WebRtcVoiceMediaChannel::SetAudioSend(uint32_t ssrc,
1817 bool enable,
solenberg1dd98f32015-09-10 01:57:14 -07001818 const AudioOptions* options,
Taylor Brandstetter1a018dc2016-03-08 12:37:39 -08001819 AudioSource* source) {
solenberg566ef242015-11-06 15:34:49 -08001820 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
solenberg1dd98f32015-09-10 01:57:14 -07001821 // TODO(solenberg): The state change should be fully rolled back if any one of
1822 // these calls fail.
Taylor Brandstetter1a018dc2016-03-08 12:37:39 -08001823 if (!SetLocalSource(ssrc, source)) {
solenberg1dd98f32015-09-10 01:57:14 -07001824 return false;
1825 }
solenbergdfc8f4f2015-10-01 02:31:10 -07001826 if (!MuteStream(ssrc, !enable)) {
solenberg1dd98f32015-09-10 01:57:14 -07001827 return false;
1828 }
solenbergdfc8f4f2015-10-01 02:31:10 -07001829 if (enable && options) {
solenberg1dd98f32015-09-10 01:57:14 -07001830 return SetOptions(*options);
1831 }
1832 return true;
1833}
1834
solenberg0a617e22015-10-20 15:49:38 -07001835int WebRtcVoiceMediaChannel::CreateVoEChannel() {
1836 int id = engine()->CreateVoEChannel();
1837 if (id == -1) {
1838 LOG_RTCERR0(CreateVoEChannel);
1839 return -1;
wu@webrtc.org9dba5252013-08-05 20:36:57 +00001840 }
solenberg0a617e22015-10-20 15:49:38 -07001841 if (engine()->voe()->network()->RegisterExternalTransport(id, *this) == -1) {
1842 LOG_RTCERR2(RegisterExternalTransport, id, this);
1843 engine()->voe()->base()->DeleteChannel(id);
1844 return -1;
1845 }
1846 return id;
wu@webrtc.org9dba5252013-08-05 20:36:57 +00001847}
1848
solenberg7add0582015-11-20 09:59:34 -08001849bool WebRtcVoiceMediaChannel::DeleteVoEChannel(int channel) {
wu@webrtc.org9dba5252013-08-05 20:36:57 +00001850 if (engine()->voe()->network()->DeRegisterExternalTransport(channel) == -1) {
1851 LOG_RTCERR1(DeRegisterExternalTransport, channel);
1852 }
wu@webrtc.org9dba5252013-08-05 20:36:57 +00001853 if (engine()->voe()->base()->DeleteChannel(channel) == -1) {
1854 LOG_RTCERR1(DeleteChannel, channel);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001855 return false;
1856 }
wu@webrtc.org9dba5252013-08-05 20:36:57 +00001857 return true;
1858}
henrike@webrtc.org1e09a712013-07-26 19:17:59 +00001859
wu@webrtc.org9dba5252013-08-05 20:36:57 +00001860bool WebRtcVoiceMediaChannel::AddSendStream(const StreamParams& sp) {
Peter Boströmca8b4042016-03-08 14:24:13 -08001861 TRACE_EVENT0("webrtc", "WebRtcVoiceMediaChannel::AddSendStream");
solenberg566ef242015-11-06 15:34:49 -08001862 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
solenberg0a617e22015-10-20 15:49:38 -07001863 LOG(LS_INFO) << "AddSendStream: " << sp.ToString();
1864
1865 uint32_t ssrc = sp.first_ssrc();
1866 RTC_DCHECK(0 != ssrc);
1867
1868 if (GetSendChannelId(ssrc) != -1) {
1869 LOG(LS_ERROR) << "Stream already exists with ssrc " << ssrc;
wu@webrtc.org9dba5252013-08-05 20:36:57 +00001870 return false;
1871 }
1872
solenberg0a617e22015-10-20 15:49:38 -07001873 // Create a new channel for sending audio data.
1874 int channel = CreateVoEChannel();
1875 if (channel == -1) {
1876 return false;
wu@webrtc.org9dba5252013-08-05 20:36:57 +00001877 }
wu@webrtc.org9dba5252013-08-05 20:36:57 +00001878
solenbergc96df772015-10-21 13:01:53 -07001879 // Save the channel to send_streams_, so that RemoveSendStream() can still
wu@webrtc.org9dba5252013-08-05 20:36:57 +00001880 // delete the channel in case failure happens below.
mallinath@webrtc.org67ee6b92014-02-03 16:57:16 +00001881 webrtc::AudioTransport* audio_transport =
1882 engine()->voe()->base()->audio_transport();
solenberg3a941542015-11-16 07:34:50 -08001883 send_streams_.insert(std::make_pair(ssrc, new WebRtcAudioSendStream(
1884 channel, audio_transport, ssrc, sp.cname, send_rtp_extensions_, call_)));
wu@webrtc.org9dba5252013-08-05 20:36:57 +00001885
solenberg0a617e22015-10-20 15:49:38 -07001886 // Set the current codecs to be used for the new channel. We need to do this
1887 // after adding the channel to send_channels_, because of how max bitrate is
1888 // currently being configured by SetSendCodec().
solenberg72e29d22016-03-08 06:35:16 -08001889 if (HasSendCodec() && !SetSendCodecs(channel)) {
solenberg0a617e22015-10-20 15:49:38 -07001890 RemoveSendStream(ssrc);
wu@webrtc.org9dba5252013-08-05 20:36:57 +00001891 return false;
1892 }
1893
1894 // At this point the channel's local SSRC has been updated. If the channel is
solenberg0a617e22015-10-20 15:49:38 -07001895 // the first send channel make sure that all the receive channels are updated
1896 // with the same SSRC in order to send receiver reports.
solenbergc96df772015-10-21 13:01:53 -07001897 if (send_streams_.size() == 1) {
solenberg0a617e22015-10-20 15:49:38 -07001898 receiver_reports_ssrc_ = ssrc;
solenberg7add0582015-11-20 09:59:34 -08001899 for (const auto& stream : recv_streams_) {
1900 int recv_channel = stream.second->channel();
solenberg0a617e22015-10-20 15:49:38 -07001901 if (engine()->voe()->rtp()->SetLocalSSRC(recv_channel, ssrc) != 0) {
solenberg7add0582015-11-20 09:59:34 -08001902 LOG_RTCERR2(SetLocalSSRC, recv_channel, ssrc);
solenberg1ac56142015-10-13 03:58:19 -07001903 return false;
wu@webrtc.org9dba5252013-08-05 20:36:57 +00001904 }
solenberg0a617e22015-10-20 15:49:38 -07001905 engine()->voe()->base()->AssociateSendChannel(recv_channel, channel);
1906 LOG(LS_INFO) << "VoiceEngine channel #" << recv_channel
1907 << " is associated with channel #" << channel << ".";
wu@webrtc.org9dba5252013-08-05 20:36:57 +00001908 }
1909 }
1910
Taylor Brandstetter1a018dc2016-03-08 12:37:39 -08001911 send_streams_[ssrc]->SetSend(send_);
1912 return true;
wu@webrtc.org9dba5252013-08-05 20:36:57 +00001913}
1914
Peter Boström0c4e06b2015-10-07 12:23:21 +02001915bool WebRtcVoiceMediaChannel::RemoveSendStream(uint32_t ssrc) {
Peter Boströmca8b4042016-03-08 14:24:13 -08001916 TRACE_EVENT0("webrtc", "WebRtcVoiceMediaChannel::RemoveSendStream");
solenberg566ef242015-11-06 15:34:49 -08001917 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
solenberg3a941542015-11-16 07:34:50 -08001918 LOG(LS_INFO) << "RemoveSendStream: " << ssrc;
1919
solenbergc96df772015-10-21 13:01:53 -07001920 auto it = send_streams_.find(ssrc);
1921 if (it == send_streams_.end()) {
wu@webrtc.org9dba5252013-08-05 20:36:57 +00001922 LOG(LS_WARNING) << "Try to remove stream with ssrc " << ssrc
1923 << " which doesn't exist.";
1924 return false;
1925 }
1926
Taylor Brandstetter1a018dc2016-03-08 12:37:39 -08001927 it->second->SetSend(false);
wu@webrtc.org9dba5252013-08-05 20:36:57 +00001928
solenberg7add0582015-11-20 09:59:34 -08001929 // Clean up and delete the send stream+channel.
Taylor Brandstetter1a018dc2016-03-08 12:37:39 -08001930 int channel = it->second->channel();
solenberg0a617e22015-10-20 15:49:38 -07001931 LOG(LS_INFO) << "Removing audio send stream " << ssrc
1932 << " with VoiceEngine channel #" << channel << ".";
solenberg7add0582015-11-20 09:59:34 -08001933 delete it->second;
1934 send_streams_.erase(it);
1935 if (!DeleteVoEChannel(channel)) {
solenberg0a617e22015-10-20 15:49:38 -07001936 return false;
wu@webrtc.org9dba5252013-08-05 20:36:57 +00001937 }
solenbergc96df772015-10-21 13:01:53 -07001938 if (send_streams_.empty()) {
Taylor Brandstetter1a018dc2016-03-08 12:37:39 -08001939 SetSend(false);
solenberg0a617e22015-10-20 15:49:38 -07001940 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001941 return true;
1942}
1943
1944bool WebRtcVoiceMediaChannel::AddRecvStream(const StreamParams& sp) {
Peter Boströmca8b4042016-03-08 14:24:13 -08001945 TRACE_EVENT0("webrtc", "WebRtcVoiceMediaChannel::AddRecvStream");
solenberg566ef242015-11-06 15:34:49 -08001946 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
solenbergd97ec302015-10-07 01:40:33 -07001947 LOG(LS_INFO) << "AddRecvStream: " << sp.ToString();
1948
solenberg0b675462015-10-09 01:37:09 -07001949 if (!ValidateStreamParams(sp)) {
wu@webrtc.org78187522013-10-07 23:32:02 +00001950 return false;
1951 }
1952
solenberg7add0582015-11-20 09:59:34 -08001953 const uint32_t ssrc = sp.first_ssrc();
solenberg0b675462015-10-09 01:37:09 -07001954 if (ssrc == 0) {
1955 LOG(LS_WARNING) << "AddRecvStream with ssrc==0 is not supported.";
1956 return false;
1957 }
1958
solenberg1ac56142015-10-13 03:58:19 -07001959 // Remove the default receive stream if one had been created with this ssrc;
1960 // we'll recreate it then.
1961 if (IsDefaultRecvStream(ssrc)) {
1962 RemoveRecvStream(ssrc);
1963 }
solenberg0b675462015-10-09 01:37:09 -07001964
solenberg7add0582015-11-20 09:59:34 -08001965 if (GetReceiveChannelId(ssrc) != -1) {
henrike@webrtc.org1e09a712013-07-26 19:17:59 +00001966 LOG(LS_ERROR) << "Stream already exists with ssrc " << ssrc;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001967 return false;
1968 }
Fredrik Solenberg4b60c732015-05-07 14:07:48 +02001969
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001970 // Create a new channel for receiving audio data.
solenberg7add0582015-11-20 09:59:34 -08001971 const int channel = CreateVoEChannel();
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001972 if (channel == -1) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001973 return false;
1974 }
Minyue2013aec2015-05-13 14:14:42 +02001975
solenberg1ac56142015-10-13 03:58:19 -07001976 // Turn off all supported codecs.
solenberg26c8c912015-11-27 04:00:25 -08001977 // TODO(solenberg): Remove once "no codecs" is the default state of a stream.
1978 for (webrtc::CodecInst voe_codec : webrtc::acm2::RentACodec::Database()) {
1979 voe_codec.pltype = -1;
1980 if (engine()->voe()->codec()->SetRecPayloadType(channel, voe_codec) == -1) {
1981 LOG_RTCERR2(SetRecPayloadType, channel, ToString(voe_codec));
1982 DeleteVoEChannel(channel);
1983 return false;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001984 }
1985 }
1986
solenberg1ac56142015-10-13 03:58:19 -07001987 // Only enable those configured for this channel.
1988 for (const auto& codec : recv_codecs_) {
solenberg72e29d22016-03-08 06:35:16 -08001989 webrtc::CodecInst voe_codec = {0};
solenberg26c8c912015-11-27 04:00:25 -08001990 if (WebRtcVoiceEngine::ToCodecInst(codec, &voe_codec)) {
solenberg1ac56142015-10-13 03:58:19 -07001991 voe_codec.pltype = codec.id;
1992 if (engine()->voe()->codec()->SetRecPayloadType(
1993 channel, voe_codec) == -1) {
1994 LOG_RTCERR2(SetRecPayloadType, channel, ToString(voe_codec));
solenberg7add0582015-11-20 09:59:34 -08001995 DeleteVoEChannel(channel);
solenberg1ac56142015-10-13 03:58:19 -07001996 return false;
1997 }
henrike@webrtc.org1e09a712013-07-26 19:17:59 +00001998 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001999 }
solenberg8fb30c32015-10-13 03:06:58 -07002000
solenberg7add0582015-11-20 09:59:34 -08002001 const int send_channel = GetSendChannelId(receiver_reports_ssrc_);
2002 if (send_channel != -1) {
2003 // Associate receive channel with first send channel (so the receive channel
2004 // can obtain RTT from the send channel)
2005 engine()->voe()->base()->AssociateSendChannel(channel, send_channel);
2006 LOG(LS_INFO) << "VoiceEngine channel #" << channel
2007 << " is associated with channel #" << send_channel << ".";
buildbot@webrtc.org150835e2014-05-06 15:54:38 +00002008 }
2009
stefanba4c0e42016-02-04 04:12:24 -08002010 recv_streams_.insert(std::make_pair(
2011 ssrc, new WebRtcAudioReceiveStream(channel, ssrc, receiver_reports_ssrc_,
solenberg72e29d22016-03-08 06:35:16 -08002012 recv_transport_cc_enabled_,
2013 sp.sync_label, recv_rtp_extensions_,
2014 call_)));
solenberg7add0582015-11-20 09:59:34 -08002015
solenberg72e29d22016-03-08 06:35:16 -08002016 SetNack(channel, send_codec_spec_.nack_enabled);
solenberg1ac56142015-10-13 03:58:19 -07002017 SetPlayout(channel, playout_);
solenberg7add0582015-11-20 09:59:34 -08002018
solenberg1ac56142015-10-13 03:58:19 -07002019 return true;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002020}
2021
Peter Boström0c4e06b2015-10-07 12:23:21 +02002022bool WebRtcVoiceMediaChannel::RemoveRecvStream(uint32_t ssrc) {
Peter Boströmca8b4042016-03-08 14:24:13 -08002023 TRACE_EVENT0("webrtc", "WebRtcVoiceMediaChannel::RemoveRecvStream");
solenberg566ef242015-11-06 15:34:49 -08002024 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
solenbergd97ec302015-10-07 01:40:33 -07002025 LOG(LS_INFO) << "RemoveRecvStream: " << ssrc;
2026
solenberg7add0582015-11-20 09:59:34 -08002027 const auto it = recv_streams_.find(ssrc);
2028 if (it == recv_streams_.end()) {
wu@webrtc.org9dba5252013-08-05 20:36:57 +00002029 LOG(LS_WARNING) << "Try to remove stream with ssrc " << ssrc
2030 << " which doesn't exist.";
henrike@webrtc.org1e09a712013-07-26 19:17:59 +00002031 return false;
wu@webrtc.org9dba5252013-08-05 20:36:57 +00002032 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002033
solenberg1ac56142015-10-13 03:58:19 -07002034 // Deregister default channel, if that's the one being destroyed.
2035 if (IsDefaultRecvStream(ssrc)) {
2036 default_recv_ssrc_ = -1;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002037 }
henrike@webrtc.org1e09a712013-07-26 19:17:59 +00002038
solenberg7add0582015-11-20 09:59:34 -08002039 const int channel = it->second->channel();
2040
2041 // Clean up and delete the receive stream+channel.
2042 LOG(LS_INFO) << "Removing audio receive stream " << ssrc
mallinath@webrtc.org67ee6b92014-02-03 16:57:16 +00002043 << " with VoiceEngine channel #" << channel << ".";
Tommif888bb52015-12-12 01:37:01 +01002044 it->second->SetRawAudioSink(nullptr);
solenberg7add0582015-11-20 09:59:34 -08002045 delete it->second;
2046 recv_streams_.erase(it);
2047 return DeleteVoEChannel(channel);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002048}
2049
Taylor Brandstetter1a018dc2016-03-08 12:37:39 -08002050bool WebRtcVoiceMediaChannel::SetLocalSource(uint32_t ssrc,
2051 AudioSource* source) {
solenbergc96df772015-10-21 13:01:53 -07002052 auto it = send_streams_.find(ssrc);
2053 if (it == send_streams_.end()) {
Taylor Brandstetter1a018dc2016-03-08 12:37:39 -08002054 if (source) {
2055 // Return an error if trying to set a valid source with an invalid ssrc.
2056 LOG(LS_ERROR) << "SetLocalSource failed with ssrc " << ssrc;
wu@webrtc.org9dba5252013-08-05 20:36:57 +00002057 return false;
2058 }
2059
2060 // The channel likely has gone away, do nothing.
henrike@webrtc.org1e09a712013-07-26 19:17:59 +00002061 return true;
henrike@webrtc.org1e09a712013-07-26 19:17:59 +00002062 }
2063
Taylor Brandstetter1a018dc2016-03-08 12:37:39 -08002064 if (source) {
2065 it->second->SetSource(source);
solenberg1ac56142015-10-13 03:58:19 -07002066 } else {
Taylor Brandstetter1a018dc2016-03-08 12:37:39 -08002067 it->second->ClearSource();
solenberg1ac56142015-10-13 03:58:19 -07002068 }
henrike@webrtc.org1e09a712013-07-26 19:17:59 +00002069
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002070 return true;
2071}
2072
2073bool WebRtcVoiceMediaChannel::GetActiveStreams(
2074 AudioInfo::StreamList* actives) {
solenberg566ef242015-11-06 15:34:49 -08002075 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002076 actives->clear();
solenberg7add0582015-11-20 09:59:34 -08002077 for (const auto& ch : recv_streams_) {
Fredrik Solenbergaf9fb212015-08-26 10:45:53 +02002078 int level = GetOutputLevel(ch.second->channel());
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002079 if (level > 0) {
Fredrik Solenbergaf9fb212015-08-26 10:45:53 +02002080 actives->push_back(std::make_pair(ch.first, level));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002081 }
2082 }
2083 return true;
2084}
2085
2086int WebRtcVoiceMediaChannel::GetOutputLevel() {
solenberg566ef242015-11-06 15:34:49 -08002087 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
solenberg1ac56142015-10-13 03:58:19 -07002088 int highest = 0;
solenberg7add0582015-11-20 09:59:34 -08002089 for (const auto& ch : recv_streams_) {
solenberg8fb30c32015-10-13 03:06:58 -07002090 highest = std::max(GetOutputLevel(ch.second->channel()), highest);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002091 }
2092 return highest;
2093}
2094
2095int WebRtcVoiceMediaChannel::GetTimeSinceLastTyping() {
2096 int ret;
2097 if (engine()->voe()->processing()->TimeSinceLastTyping(ret) == -1) {
2098 // In case of error, log the info and continue
2099 LOG_RTCERR0(TimeSinceLastTyping);
2100 ret = -1;
2101 } else {
2102 ret *= 1000; // We return ms, webrtc returns seconds.
2103 }
2104 return ret;
2105}
2106
2107void WebRtcVoiceMediaChannel::SetTypingDetectionParameters(int time_window,
2108 int cost_per_typing, int reporting_threshold, int penalty_decay,
2109 int type_event_delay) {
2110 if (engine()->voe()->processing()->SetTypingDetectionParameters(
2111 time_window, cost_per_typing,
2112 reporting_threshold, penalty_decay, type_event_delay) == -1) {
2113 // In case of error, log the info and continue
2114 LOG_RTCERR5(SetTypingDetectionParameters, time_window,
2115 cost_per_typing, reporting_threshold, penalty_decay,
2116 type_event_delay);
2117 }
2118}
2119
solenberg4bac9c52015-10-09 02:32:53 -07002120bool WebRtcVoiceMediaChannel::SetOutputVolume(uint32_t ssrc, double volume) {
solenberg566ef242015-11-06 15:34:49 -08002121 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
solenberg1ac56142015-10-13 03:58:19 -07002122 if (ssrc == 0) {
2123 default_recv_volume_ = volume;
2124 if (default_recv_ssrc_ == -1) {
2125 return true;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002126 }
solenberg1ac56142015-10-13 03:58:19 -07002127 ssrc = static_cast<uint32_t>(default_recv_ssrc_);
2128 }
2129 int ch_id = GetReceiveChannelId(ssrc);
2130 if (ch_id < 0) {
2131 LOG(LS_WARNING) << "Cannot find channel for ssrc:" << ssrc;
2132 return false;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002133 }
2134
solenberg1ac56142015-10-13 03:58:19 -07002135 if (-1 == engine()->voe()->volume()->SetChannelOutputVolumeScaling(ch_id,
2136 volume)) {
2137 LOG_RTCERR2(SetChannelOutputVolumeScaling, ch_id, volume);
2138 return false;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002139 }
solenberg1ac56142015-10-13 03:58:19 -07002140 LOG(LS_INFO) << "SetOutputVolume to " << volume
2141 << " for channel " << ch_id << " and ssrc " << ssrc;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002142 return true;
2143}
2144
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002145bool WebRtcVoiceMediaChannel::CanInsertDtmf() {
Fredrik Solenbergb5727682015-12-04 15:22:19 +01002146 return dtmf_payload_type_ ? true : false;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002147}
2148
solenberg1d63dd02015-12-02 12:35:09 -08002149bool WebRtcVoiceMediaChannel::InsertDtmf(uint32_t ssrc, int event,
2150 int duration) {
solenberg566ef242015-11-06 15:34:49 -08002151 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
Fredrik Solenbergb5727682015-12-04 15:22:19 +01002152 LOG(LS_INFO) << "WebRtcVoiceMediaChannel::InsertDtmf";
2153 if (!dtmf_payload_type_) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002154 return false;
2155 }
2156
Fredrik Solenbergb5727682015-12-04 15:22:19 +01002157 // Figure out which WebRtcAudioSendStream to send the event on.
2158 auto it = ssrc != 0 ? send_streams_.find(ssrc) : send_streams_.begin();
2159 if (it == send_streams_.end()) {
2160 LOG(LS_WARNING) << "The specified ssrc " << ssrc << " is not in use.";
solenberg1d63dd02015-12-02 12:35:09 -08002161 return false;
2162 }
Fredrik Solenbergb5727682015-12-04 15:22:19 +01002163 if (event < kMinTelephoneEventCode ||
2164 event > kMaxTelephoneEventCode) {
2165 LOG(LS_WARNING) << "DTMF event code " << event << " out of range.";
solenberg1d63dd02015-12-02 12:35:09 -08002166 return false;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002167 }
Fredrik Solenbergb5727682015-12-04 15:22:19 +01002168 if (duration < kMinTelephoneEventDuration ||
2169 duration > kMaxTelephoneEventDuration) {
2170 LOG(LS_WARNING) << "DTMF event duration " << duration << " out of range.";
2171 return false;
2172 }
2173 return it->second->SendTelephoneEvent(*dtmf_payload_type_, event, duration);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002174}
2175
wu@webrtc.orga9890802013-12-13 00:21:03 +00002176void WebRtcVoiceMediaChannel::OnPacketReceived(
jbaucheec21bd2016-03-20 06:15:43 -07002177 rtc::CopyOnWriteBuffer* packet, const rtc::PacketTime& packet_time) {
solenberg566ef242015-11-06 15:34:49 -08002178 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
Fredrik Solenberg4b60c732015-05-07 14:07:48 +02002179
solenberg1ac56142015-10-13 03:58:19 -07002180 uint32_t ssrc = 0;
jbaucheec21bd2016-03-20 06:15:43 -07002181 if (!GetRtpSsrc(packet->cdata(), packet->size(), &ssrc)) {
solenberg1ac56142015-10-13 03:58:19 -07002182 return;
2183 }
2184
solenberg7e63ef02015-11-20 00:19:43 -08002185 // If we don't have a default channel, and the SSRC is unknown, create a
2186 // default channel.
2187 if (default_recv_ssrc_ == -1 && GetReceiveChannelId(ssrc) == -1) {
solenberg1ac56142015-10-13 03:58:19 -07002188 StreamParams sp;
2189 sp.ssrcs.push_back(ssrc);
2190 LOG(LS_INFO) << "Creating default receive stream for SSRC=" << ssrc << ".";
2191 if (!AddRecvStream(sp)) {
2192 LOG(LS_WARNING) << "Could not create default receive stream.";
2193 return;
2194 }
2195 default_recv_ssrc_ = ssrc;
2196 SetOutputVolume(default_recv_ssrc_, default_recv_volume_);
deadbeef884f5852016-01-15 09:20:04 -08002197 if (default_sink_) {
kwiberg686a8ef2016-02-26 03:00:35 -08002198 std::unique_ptr<webrtc::AudioSinkInterface> proxy_sink(
deadbeef884f5852016-01-15 09:20:04 -08002199 new ProxySink(default_sink_.get()));
2200 SetRawAudioSink(default_recv_ssrc_, std::move(proxy_sink));
2201 }
solenberg1ac56142015-10-13 03:58:19 -07002202 }
2203
2204 // Forward packet to Call. If the SSRC is unknown we'll return after this.
Fredrik Solenberg709ed672015-09-15 12:26:33 +02002205 const webrtc::PacketTime webrtc_packet_time(packet_time.timestamp,
2206 packet_time.not_before);
solenberg1ac56142015-10-13 03:58:19 -07002207 webrtc::PacketReceiver::DeliveryStatus delivery_result =
2208 call_->Receiver()->DeliverPacket(webrtc::MediaType::AUDIO,
jbaucheec21bd2016-03-20 06:15:43 -07002209 packet->cdata(), packet->size(), webrtc_packet_time);
solenberg1ac56142015-10-13 03:58:19 -07002210 if (webrtc::PacketReceiver::DELIVERY_OK != delivery_result) {
solenberg7e63ef02015-11-20 00:19:43 -08002211 // If the SSRC is unknown here, route it to the default channel, if we have
2212 // one. See: https://bugs.chromium.org/p/webrtc/issues/detail?id=5208
2213 if (default_recv_ssrc_ == -1) {
2214 return;
2215 } else {
2216 ssrc = default_recv_ssrc_;
2217 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002218 }
2219
solenberg1ac56142015-10-13 03:58:19 -07002220 // Find the channel to send this packet to. It must exist since webrtc::Call
2221 // was able to demux the packet.
2222 int channel = GetReceiveChannelId(ssrc);
2223 RTC_DCHECK(channel != -1);
2224
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002225 // Pass it off to the decoder.
henrike@webrtc.org28654cb2013-07-22 21:07:49 +00002226 engine()->voe()->network()->ReceivedRTPPacket(
jbaucheec21bd2016-03-20 06:15:43 -07002227 channel, packet->cdata(), packet->size(), webrtc_packet_time);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002228}
2229
wu@webrtc.orga9890802013-12-13 00:21:03 +00002230void WebRtcVoiceMediaChannel::OnRtcpReceived(
jbaucheec21bd2016-03-20 06:15:43 -07002231 rtc::CopyOnWriteBuffer* packet, const rtc::PacketTime& packet_time) {
solenberg566ef242015-11-06 15:34:49 -08002232 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
Fredrik Solenberg4b60c732015-05-07 14:07:48 +02002233
Fredrik Solenberg709ed672015-09-15 12:26:33 +02002234 // Forward packet to Call as well.
2235 const webrtc::PacketTime webrtc_packet_time(packet_time.timestamp,
2236 packet_time.not_before);
2237 call_->Receiver()->DeliverPacket(webrtc::MediaType::AUDIO,
jbaucheec21bd2016-03-20 06:15:43 -07002238 packet->cdata(), packet->size(), webrtc_packet_time);
Fredrik Solenberg4b60c732015-05-07 14:07:48 +02002239
wu@webrtc.org9dba5252013-08-05 20:36:57 +00002240 // Sending channels need all RTCP packets with feedback information.
2241 // Even sender reports can contain attached report blocks.
2242 // Receiving channels need sender reports in order to create
2243 // correct receiver reports.
2244 int type = 0;
jbaucheec21bd2016-03-20 06:15:43 -07002245 if (!GetRtcpType(packet->cdata(), packet->size(), &type)) {
wu@webrtc.org9dba5252013-08-05 20:36:57 +00002246 LOG(LS_WARNING) << "Failed to parse type from received RTCP packet";
2247 return;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002248 }
2249
solenberg0b675462015-10-09 01:37:09 -07002250 // If it is a sender report, find the receive channel that is listening.
wu@webrtc.org9dba5252013-08-05 20:36:57 +00002251 if (type == kRtcpTypeSR) {
solenberg0b675462015-10-09 01:37:09 -07002252 uint32_t ssrc = 0;
jbaucheec21bd2016-03-20 06:15:43 -07002253 if (!GetRtcpSsrc(packet->cdata(), packet->size(), &ssrc)) {
solenberg0b675462015-10-09 01:37:09 -07002254 return;
2255 }
2256 int recv_channel_id = GetReceiveChannelId(ssrc);
2257 if (recv_channel_id != -1) {
wu@webrtc.org9dba5252013-08-05 20:36:57 +00002258 engine()->voe()->network()->ReceivedRTCPPacket(
jbaucheec21bd2016-03-20 06:15:43 -07002259 recv_channel_id, packet->cdata(), packet->size());
wu@webrtc.org9dba5252013-08-05 20:36:57 +00002260 }
2261 }
2262
2263 // SR may continue RR and any RR entry may correspond to any one of the send
2264 // channels. So all RTCP packets must be forwarded all send channels. VoE
2265 // will filter out RR internally.
solenbergc96df772015-10-21 13:01:53 -07002266 for (const auto& ch : send_streams_) {
wu@webrtc.org9dba5252013-08-05 20:36:57 +00002267 engine()->voe()->network()->ReceivedRTCPPacket(
jbaucheec21bd2016-03-20 06:15:43 -07002268 ch.second->channel(), packet->cdata(), packet->size());
wu@webrtc.org9dba5252013-08-05 20:36:57 +00002269 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002270}
2271
Honghai Zhangcc411c02016-03-29 17:27:21 -07002272void WebRtcVoiceMediaChannel::OnNetworkRouteChanged(
2273 const std::string& transport_name,
2274 const NetworkRoute& network_route) {
2275 // TODO(honghaiz): uncomment this once the function in call is implemented.
2276 // call_->OnNetworkRouteChanged(transport_name, network_route);
2277}
2278
Peter Boström0c4e06b2015-10-07 12:23:21 +02002279bool WebRtcVoiceMediaChannel::MuteStream(uint32_t ssrc, bool muted) {
solenberg566ef242015-11-06 15:34:49 -08002280 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
solenberg0a617e22015-10-20 15:49:38 -07002281 int channel = GetSendChannelId(ssrc);
wu@webrtc.org9dba5252013-08-05 20:36:57 +00002282 if (channel == -1) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002283 LOG(LS_WARNING) << "The specified ssrc " << ssrc << " is not in use.";
2284 return false;
2285 }
wu@webrtc.org9dba5252013-08-05 20:36:57 +00002286 if (engine()->voe()->volume()->SetInputMute(channel, muted) == -1) {
2287 LOG_RTCERR2(SetInputMute, channel, muted);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002288 return false;
2289 }
buildbot@webrtc.org6b21b712014-07-31 15:08:53 +00002290 // We set the AGC to mute state only when all the channels are muted.
2291 // This implementation is not ideal, instead we should signal the AGC when
2292 // the mic channel is muted/unmuted. We can't do it today because there
2293 // is no good way to know which stream is mapping to the mic channel.
2294 bool all_muted = muted;
solenbergc96df772015-10-21 13:01:53 -07002295 for (const auto& ch : send_streams_) {
Fredrik Solenbergaf9fb212015-08-26 10:45:53 +02002296 if (!all_muted) {
2297 break;
2298 }
2299 if (engine()->voe()->volume()->GetInputMute(ch.second->channel(),
buildbot@webrtc.org6b21b712014-07-31 15:08:53 +00002300 all_muted)) {
Fredrik Solenbergaf9fb212015-08-26 10:45:53 +02002301 LOG_RTCERR1(GetInputMute, ch.second->channel());
buildbot@webrtc.org6b21b712014-07-31 15:08:53 +00002302 return false;
2303 }
2304 }
2305
2306 webrtc::AudioProcessing* ap = engine()->voe()->base()->audio_processing();
solenberg0a617e22015-10-20 15:49:38 -07002307 if (ap) {
buildbot@webrtc.org6b21b712014-07-31 15:08:53 +00002308 ap->set_output_will_be_muted(all_muted);
solenberg0a617e22015-10-20 15:49:38 -07002309 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002310 return true;
2311}
2312
minyue@webrtc.org26236952014-10-29 02:27:08 +00002313// TODO(minyue): SetMaxSendBandwidth() is subject to be renamed to
2314// SetMaxSendBitrate() in future.
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00002315bool WebRtcVoiceMediaChannel::SetMaxSendBandwidth(int bps) {
minyue@webrtc.org26236952014-10-29 02:27:08 +00002316 LOG(LS_INFO) << "WebRtcVoiceMediaChannel::SetMaxSendBandwidth.";
minyue@webrtc.org26236952014-10-29 02:27:08 +00002317 return SetSendBitrateInternal(bps);
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00002318}
2319
minyue@webrtc.org26236952014-10-29 02:27:08 +00002320bool WebRtcVoiceMediaChannel::SetSendBitrateInternal(int bps) {
2321 LOG(LS_INFO) << "WebRtcVoiceMediaChannel::SetSendBitrateInternal.";
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00002322
minyue@webrtc.org26236952014-10-29 02:27:08 +00002323 send_bitrate_setting_ = true;
2324 send_bitrate_bps_ = bps;
wu@webrtc.org1d1ffc92013-10-16 18:12:02 +00002325
solenberg72e29d22016-03-08 06:35:16 -08002326 if (!HasSendCodec()) {
wu@webrtc.org1d1ffc92013-10-16 18:12:02 +00002327 LOG(LS_INFO) << "The send codec has not been set up yet. "
minyue@webrtc.org26236952014-10-29 02:27:08 +00002328 << "The send bitrate setting will be applied later.";
wu@webrtc.org1d1ffc92013-10-16 18:12:02 +00002329 return true;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002330 }
2331
minyue@webrtc.org26236952014-10-29 02:27:08 +00002332 // Bitrate is auto by default.
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00002333 // TODO(bemasc): Fix this so that if SetMaxSendBandwidth(50) is followed by
2334 // SetMaxSendBandwith(0), the second call removes the previous limit.
2335 if (bps <= 0)
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002336 return true;
2337
solenberg72e29d22016-03-08 06:35:16 -08002338 webrtc::CodecInst codec = send_codec_spec_.codec_inst;
solenberg26c8c912015-11-27 04:00:25 -08002339 bool is_multi_rate = WebRtcVoiceCodecs::IsCodecMultiRate(codec);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002340
2341 if (is_multi_rate) {
2342 // If codec is multi-rate then just set the bitrate.
2343 codec.rate = bps;
solenbergc96df772015-10-21 13:01:53 -07002344 for (const auto& ch : send_streams_) {
solenberg0a617e22015-10-20 15:49:38 -07002345 if (!SetSendCodec(ch.second->channel(), codec)) {
2346 LOG(LS_INFO) << "Failed to set codec " << codec.plname
2347 << " to bitrate " << bps << " bps.";
2348 return false;
2349 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002350 }
2351 return true;
2352 } else {
2353 // If codec is not multi-rate and |bps| is less than the fixed bitrate
2354 // then fail. If codec is not multi-rate and |bps| exceeds or equal the
2355 // fixed bitrate then ignore.
2356 if (bps < codec.rate) {
2357 LOG(LS_INFO) << "Failed to set codec " << codec.plname
2358 << " to bitrate " << bps << " bps"
2359 << ", requires at least " << codec.rate << " bps.";
2360 return false;
2361 }
2362 return true;
2363 }
2364}
2365
skvlad7a43d252016-03-22 15:32:27 -07002366void WebRtcVoiceMediaChannel::OnReadyToSend(bool ready) {
2367 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
2368 LOG(LS_VERBOSE) << "OnReadyToSend: " << (ready ? "Ready." : "Not ready.");
2369 call_->SignalChannelNetworkState(
2370 webrtc::MediaType::AUDIO,
2371 ready ? webrtc::kNetworkUp : webrtc::kNetworkDown);
2372}
2373
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002374bool WebRtcVoiceMediaChannel::GetStats(VoiceMediaInfo* info) {
Peter Boströmca8b4042016-03-08 14:24:13 -08002375 TRACE_EVENT0("webrtc", "WebRtcVoiceMediaChannel::GetStats");
solenberg566ef242015-11-06 15:34:49 -08002376 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
solenberg85a04962015-10-27 03:35:21 -07002377 RTC_DCHECK(info);
solenbergd97ec302015-10-07 01:40:33 -07002378
solenberg85a04962015-10-27 03:35:21 -07002379 // Get SSRC and stats for each sender.
2380 RTC_DCHECK(info->senders.size() == 0);
2381 for (const auto& stream : send_streams_) {
2382 webrtc::AudioSendStream::Stats stats = stream.second->GetStats();
wu@webrtc.org9dba5252013-08-05 20:36:57 +00002383 VoiceSenderInfo sinfo;
solenberg85a04962015-10-27 03:35:21 -07002384 sinfo.add_ssrc(stats.local_ssrc);
2385 sinfo.bytes_sent = stats.bytes_sent;
2386 sinfo.packets_sent = stats.packets_sent;
2387 sinfo.packets_lost = stats.packets_lost;
2388 sinfo.fraction_lost = stats.fraction_lost;
2389 sinfo.codec_name = stats.codec_name;
2390 sinfo.ext_seqnum = stats.ext_seqnum;
2391 sinfo.jitter_ms = stats.jitter_ms;
2392 sinfo.rtt_ms = stats.rtt_ms;
2393 sinfo.audio_level = stats.audio_level;
2394 sinfo.aec_quality_min = stats.aec_quality_min;
2395 sinfo.echo_delay_median_ms = stats.echo_delay_median_ms;
2396 sinfo.echo_delay_std_ms = stats.echo_delay_std_ms;
2397 sinfo.echo_return_loss = stats.echo_return_loss;
2398 sinfo.echo_return_loss_enhancement = stats.echo_return_loss_enhancement;
Taylor Brandstetter1a018dc2016-03-08 12:37:39 -08002399 sinfo.typing_noise_detected = (send_ ? stats.typing_noise_detected : false);
wu@webrtc.org9dba5252013-08-05 20:36:57 +00002400 info->senders.push_back(sinfo);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002401 }
2402
solenberg85a04962015-10-27 03:35:21 -07002403 // Get SSRC and stats for each receiver.
2404 RTC_DCHECK(info->receivers.size() == 0);
solenberg7add0582015-11-20 09:59:34 -08002405 for (const auto& stream : recv_streams_) {
Fredrik Solenberg4f4ec0a2015-10-22 10:49:27 +02002406 webrtc::AudioReceiveStream::Stats stats = stream.second->GetStats();
2407 VoiceReceiverInfo rinfo;
2408 rinfo.add_ssrc(stats.remote_ssrc);
2409 rinfo.bytes_rcvd = stats.bytes_rcvd;
2410 rinfo.packets_rcvd = stats.packets_rcvd;
2411 rinfo.packets_lost = stats.packets_lost;
2412 rinfo.fraction_lost = stats.fraction_lost;
2413 rinfo.codec_name = stats.codec_name;
2414 rinfo.ext_seqnum = stats.ext_seqnum;
2415 rinfo.jitter_ms = stats.jitter_ms;
2416 rinfo.jitter_buffer_ms = stats.jitter_buffer_ms;
2417 rinfo.jitter_buffer_preferred_ms = stats.jitter_buffer_preferred_ms;
2418 rinfo.delay_estimate_ms = stats.delay_estimate_ms;
2419 rinfo.audio_level = stats.audio_level;
2420 rinfo.expand_rate = stats.expand_rate;
2421 rinfo.speech_expand_rate = stats.speech_expand_rate;
2422 rinfo.secondary_decoded_rate = stats.secondary_decoded_rate;
2423 rinfo.accelerate_rate = stats.accelerate_rate;
2424 rinfo.preemptive_expand_rate = stats.preemptive_expand_rate;
2425 rinfo.decoding_calls_to_silence_generator =
2426 stats.decoding_calls_to_silence_generator;
2427 rinfo.decoding_calls_to_neteq = stats.decoding_calls_to_neteq;
2428 rinfo.decoding_normal = stats.decoding_normal;
2429 rinfo.decoding_plc = stats.decoding_plc;
2430 rinfo.decoding_cng = stats.decoding_cng;
2431 rinfo.decoding_plc_cng = stats.decoding_plc_cng;
2432 rinfo.capture_start_ntp_time_ms = stats.capture_start_ntp_time_ms;
2433 info->receivers.push_back(rinfo);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002434 }
2435
2436 return true;
2437}
2438
Tommif888bb52015-12-12 01:37:01 +01002439void WebRtcVoiceMediaChannel::SetRawAudioSink(
2440 uint32_t ssrc,
kwiberg686a8ef2016-02-26 03:00:35 -08002441 std::unique_ptr<webrtc::AudioSinkInterface> sink) {
Tommif888bb52015-12-12 01:37:01 +01002442 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
deadbeef884f5852016-01-15 09:20:04 -08002443 LOG(LS_VERBOSE) << "WebRtcVoiceMediaChannel::SetRawAudioSink: ssrc:" << ssrc
2444 << " " << (sink ? "(ptr)" : "NULL");
2445 if (ssrc == 0) {
2446 if (default_recv_ssrc_ != -1) {
kwiberg686a8ef2016-02-26 03:00:35 -08002447 std::unique_ptr<webrtc::AudioSinkInterface> proxy_sink(
deadbeef884f5852016-01-15 09:20:04 -08002448 sink ? new ProxySink(sink.get()) : nullptr);
2449 SetRawAudioSink(default_recv_ssrc_, std::move(proxy_sink));
2450 }
2451 default_sink_ = std::move(sink);
2452 return;
2453 }
Tommif888bb52015-12-12 01:37:01 +01002454 const auto it = recv_streams_.find(ssrc);
2455 if (it == recv_streams_.end()) {
2456 LOG(LS_WARNING) << "SetRawAudioSink: no recv stream" << ssrc;
2457 return;
2458 }
deadbeef2d110be2016-01-13 12:00:26 -08002459 it->second->SetRawAudioSink(std::move(sink));
Tommif888bb52015-12-12 01:37:01 +01002460}
2461
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002462int WebRtcVoiceMediaChannel::GetOutputLevel(int channel) {
solenbergd97ec302015-10-07 01:40:33 -07002463 unsigned int ulevel = 0;
2464 int ret = engine()->voe()->volume()->GetSpeechOutputLevel(channel, ulevel);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002465 return (ret == 0) ? static_cast<int>(ulevel) : -1;
2466}
2467
Peter Boström0c4e06b2015-10-07 12:23:21 +02002468int WebRtcVoiceMediaChannel::GetReceiveChannelId(uint32_t ssrc) const {
solenberg566ef242015-11-06 15:34:49 -08002469 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
solenberg7add0582015-11-20 09:59:34 -08002470 const auto it = recv_streams_.find(ssrc);
2471 if (it != recv_streams_.end()) {
mallinath@webrtc.org67ee6b92014-02-03 16:57:16 +00002472 return it->second->channel();
solenberg8fb30c32015-10-13 03:06:58 -07002473 }
solenberg1ac56142015-10-13 03:58:19 -07002474 return -1;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002475}
2476
Peter Boström0c4e06b2015-10-07 12:23:21 +02002477int WebRtcVoiceMediaChannel::GetSendChannelId(uint32_t ssrc) const {
solenberg566ef242015-11-06 15:34:49 -08002478 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
solenbergc96df772015-10-21 13:01:53 -07002479 const auto it = send_streams_.find(ssrc);
2480 if (it != send_streams_.end()) {
mallinath@webrtc.org67ee6b92014-02-03 16:57:16 +00002481 return it->second->channel();
solenberg8fb30c32015-10-13 03:06:58 -07002482 }
wu@webrtc.org9dba5252013-08-05 20:36:57 +00002483 return -1;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002484}
2485
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002486bool WebRtcVoiceMediaChannel::SetPlayout(int channel, bool playout) {
2487 if (playout) {
2488 LOG(LS_INFO) << "Starting playout for channel #" << channel;
2489 if (engine()->voe()->base()->StartPlayout(channel) == -1) {
2490 LOG_RTCERR1(StartPlayout, channel);
2491 return false;
2492 }
2493 } else {
2494 LOG(LS_INFO) << "Stopping playout for channel #" << channel;
2495 engine()->voe()->base()->StopPlayout(channel);
2496 }
2497 return true;
2498}
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002499} // namespace cricket
2500
2501#endif // HAVE_WEBRTC_VOICE