blob: cef7cdf2becc2cfa786ee5e9e1414f9b752d148c [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.
solenberg5b5129a2016-04-08 05:35:48 -0700658 const bool built_in_aec = adm()->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;
solenberg5b5129a2016-04-08 05:35:48 -0700665 if (adm()->EnableBuiltInAEC(enable_built_in_aec) == 0 &&
Bjorn Volcker73f72102015-06-03 14:50:15 +0200666 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) {
solenberg5b5129a2016-04-08 05:35:48 -0700697 const bool built_in_agc = adm()->BuiltInAGCIsAvailable();
henrikac14f5ff2015-09-23 14:08:33 +0200698 if (built_in_agc) {
solenberg5b5129a2016-04-08 05:35:48 -0700699 if (adm()->EnableBuiltInAGC(*options.auto_gain_control) == 0 &&
kwiberg102c6a62015-10-30 02:47:38 -0700700 *options.auto_gain_control) {
henrikac14f5ff2015-09-23 14:08:33 +0200701 // Disable internal software AGC if built-in AGC is enabled,
702 // i.e., replace the software AGC with the built-in AGC.
Karl Wibergbe579832015-11-10 22:34:18 +0100703 options.auto_gain_control = rtc::Optional<bool>(false);
henrikac14f5ff2015-09-23 14:08:33 +0200704 LOG(LS_INFO) << "Disabling AGC since built-in AGC will be used instead";
705 }
706 }
kwiberg102c6a62015-10-30 02:47:38 -0700707 if (voep->SetAgcStatus(*options.auto_gain_control, agc_mode) == -1) {
708 LOG_RTCERR2(SetAgcStatus, *options.auto_gain_control, agc_mode);
buildbot@webrtc.org13d67762014-05-02 17:33:29 +0000709 return false;
710 } else {
kwiberg102c6a62015-10-30 02:47:38 -0700711 LOG(LS_INFO) << "Auto gain set to " << *options.auto_gain_control
712 << " with mode " << agc_mode;
buildbot@webrtc.org13d67762014-05-02 17:33:29 +0000713 }
714 }
715
kwiberg102c6a62015-10-30 02:47:38 -0700716 if (options.tx_agc_target_dbov || options.tx_agc_digital_compression_gain ||
717 options.tx_agc_limiter) {
buildbot@webrtc.org13d67762014-05-02 17:33:29 +0000718 // Override default_agc_config_. Generally, an unset option means "leave
719 // the VoE bits alone" in this function, so we want whatever is set to be
720 // stored as the new "default". If we didn't, then setting e.g.
721 // tx_agc_target_dbov would reset digital compression gain and limiter
722 // settings.
723 // Also, if we don't update default_agc_config_, then adjust_agc_delta
724 // would be an offset from the original values, and not whatever was set
725 // explicitly.
kwiberg102c6a62015-10-30 02:47:38 -0700726 default_agc_config_.targetLeveldBOv = options.tx_agc_target_dbov.value_or(
727 default_agc_config_.targetLeveldBOv);
buildbot@webrtc.org13d67762014-05-02 17:33:29 +0000728 default_agc_config_.digitalCompressionGaindB =
kwiberg102c6a62015-10-30 02:47:38 -0700729 options.tx_agc_digital_compression_gain.value_or(
buildbot@webrtc.org13d67762014-05-02 17:33:29 +0000730 default_agc_config_.digitalCompressionGaindB);
731 default_agc_config_.limiterEnable =
kwiberg102c6a62015-10-30 02:47:38 -0700732 options.tx_agc_limiter.value_or(default_agc_config_.limiterEnable);
buildbot@webrtc.org13d67762014-05-02 17:33:29 +0000733 if (voe_wrapper_->processing()->SetAgcConfig(default_agc_config_) == -1) {
734 LOG_RTCERR3(SetAgcConfig,
735 default_agc_config_.targetLeveldBOv,
736 default_agc_config_.digitalCompressionGaindB,
737 default_agc_config_.limiterEnable);
738 return false;
739 }
740 }
741
kwiberg102c6a62015-10-30 02:47:38 -0700742 if (options.noise_suppression) {
solenberg5b5129a2016-04-08 05:35:48 -0700743 const bool built_in_ns = adm()->BuiltInNSIsAvailable();
henrikac14f5ff2015-09-23 14:08:33 +0200744 if (built_in_ns) {
solenberg5b5129a2016-04-08 05:35:48 -0700745 if (adm()->EnableBuiltInNS(*options.noise_suppression) == 0 &&
kwiberg102c6a62015-10-30 02:47:38 -0700746 *options.noise_suppression) {
henrikac14f5ff2015-09-23 14:08:33 +0200747 // Disable internal software NS if built-in NS is enabled,
748 // i.e., replace the software NS with the built-in NS.
Karl Wibergbe579832015-11-10 22:34:18 +0100749 options.noise_suppression = rtc::Optional<bool>(false);
henrikac14f5ff2015-09-23 14:08:33 +0200750 LOG(LS_INFO) << "Disabling NS since built-in NS will be used instead";
751 }
752 }
kwiberg102c6a62015-10-30 02:47:38 -0700753 if (voep->SetNsStatus(*options.noise_suppression, ns_mode) == -1) {
754 LOG_RTCERR2(SetNsStatus, *options.noise_suppression, ns_mode);
buildbot@webrtc.org13d67762014-05-02 17:33:29 +0000755 return false;
756 } else {
kwiberg102c6a62015-10-30 02:47:38 -0700757 LOG(LS_INFO) << "Noise suppression set to " << *options.noise_suppression
henrikac14f5ff2015-09-23 14:08:33 +0200758 << " with mode " << ns_mode;
buildbot@webrtc.org13d67762014-05-02 17:33:29 +0000759 }
760 }
761
kwiberg102c6a62015-10-30 02:47:38 -0700762 if (options.highpass_filter) {
763 LOG(LS_INFO) << "High pass filter enabled? " << *options.highpass_filter;
764 if (voep->EnableHighPassFilter(*options.highpass_filter) == -1) {
765 LOG_RTCERR1(SetHighpassFilterStatus, *options.highpass_filter);
buildbot@webrtc.org13d67762014-05-02 17:33:29 +0000766 return false;
767 }
768 }
769
kwiberg102c6a62015-10-30 02:47:38 -0700770 if (options.stereo_swapping) {
771 LOG(LS_INFO) << "Stereo swapping enabled? " << *options.stereo_swapping;
772 voep->EnableStereoChannelSwapping(*options.stereo_swapping);
773 if (voep->IsStereoChannelSwappingEnabled() != *options.stereo_swapping) {
774 LOG_RTCERR1(EnableStereoChannelSwapping, *options.stereo_swapping);
buildbot@webrtc.org13d67762014-05-02 17:33:29 +0000775 return false;
776 }
777 }
778
kwiberg102c6a62015-10-30 02:47:38 -0700779 if (options.audio_jitter_buffer_max_packets) {
780 LOG(LS_INFO) << "NetEq capacity is "
781 << *options.audio_jitter_buffer_max_packets;
Henrik Lundin64dad832015-05-11 12:44:23 +0200782 voe_config_.Set<webrtc::NetEqCapacityConfig>(
kwiberg102c6a62015-10-30 02:47:38 -0700783 new webrtc::NetEqCapacityConfig(
784 *options.audio_jitter_buffer_max_packets));
Henrik Lundin64dad832015-05-11 12:44:23 +0200785 }
786
kwiberg102c6a62015-10-30 02:47:38 -0700787 if (options.audio_jitter_buffer_fast_accelerate) {
788 LOG(LS_INFO) << "NetEq fast mode? "
789 << *options.audio_jitter_buffer_fast_accelerate;
Henrik Lundin5263b3c2015-06-01 10:29:41 +0200790 voe_config_.Set<webrtc::NetEqFastAccelerate>(
kwiberg102c6a62015-10-30 02:47:38 -0700791 new webrtc::NetEqFastAccelerate(
792 *options.audio_jitter_buffer_fast_accelerate));
Henrik Lundin5263b3c2015-06-01 10:29:41 +0200793 }
794
kwiberg102c6a62015-10-30 02:47:38 -0700795 if (options.typing_detection) {
796 LOG(LS_INFO) << "Typing detection is enabled? "
797 << *options.typing_detection;
798 if (voep->SetTypingDetectionStatus(*options.typing_detection) == -1) {
buildbot@webrtc.org13d67762014-05-02 17:33:29 +0000799 // In case of error, log the info and continue
kwiberg102c6a62015-10-30 02:47:38 -0700800 LOG_RTCERR1(SetTypingDetectionStatus, *options.typing_detection);
buildbot@webrtc.org13d67762014-05-02 17:33:29 +0000801 }
802 }
803
kwiberg102c6a62015-10-30 02:47:38 -0700804 if (options.adjust_agc_delta) {
805 LOG(LS_INFO) << "Adjust agc delta is " << *options.adjust_agc_delta;
806 if (!AdjustAgcLevel(*options.adjust_agc_delta)) {
buildbot@webrtc.org13d67762014-05-02 17:33:29 +0000807 return false;
808 }
809 }
810
buildbot@webrtc.org1f8a2372014-08-28 10:52:44 +0000811 webrtc::Config config;
812
kwiberg102c6a62015-10-30 02:47:38 -0700813 if (options.delay_agnostic_aec)
814 delay_agnostic_aec_ = options.delay_agnostic_aec;
815 if (delay_agnostic_aec_) {
816 LOG(LS_INFO) << "Delay agnostic aec is enabled? " << *delay_agnostic_aec_;
henrik.lundin0f133b92015-07-02 00:17:55 -0700817 config.Set<webrtc::DelayAgnostic>(
kwiberg102c6a62015-10-30 02:47:38 -0700818 new webrtc::DelayAgnostic(*delay_agnostic_aec_));
Bjorn Volckerbf395c12015-03-25 22:45:56 +0100819 }
820
kwiberg102c6a62015-10-30 02:47:38 -0700821 if (options.extended_filter_aec) {
822 extended_filter_aec_ = options.extended_filter_aec;
823 }
824 if (extended_filter_aec_) {
825 LOG(LS_INFO) << "Extended filter aec is enabled? " << *extended_filter_aec_;
Henrik Lundin441f6342015-06-09 16:03:13 +0200826 config.Set<webrtc::ExtendedFilter>(
kwiberg102c6a62015-10-30 02:47:38 -0700827 new webrtc::ExtendedFilter(*extended_filter_aec_));
buildbot@webrtc.org1f8a2372014-08-28 10:52:44 +0000828 }
829
kwiberg102c6a62015-10-30 02:47:38 -0700830 if (options.experimental_ns) {
831 experimental_ns_ = options.experimental_ns;
832 }
833 if (experimental_ns_) {
834 LOG(LS_INFO) << "Experimental ns is enabled? " << *experimental_ns_;
buildbot@webrtc.org1f8a2372014-08-28 10:52:44 +0000835 config.Set<webrtc::ExperimentalNs>(
kwiberg102c6a62015-10-30 02:47:38 -0700836 new webrtc::ExperimentalNs(*experimental_ns_));
buildbot@webrtc.org1f8a2372014-08-28 10:52:44 +0000837 }
buildbot@webrtc.org1f8a2372014-08-28 10:52:44 +0000838
839 // We check audioproc for the benefit of tests, since FakeWebRtcVoiceEngine
840 // returns NULL on audio_processing().
841 webrtc::AudioProcessing* audioproc = voe_wrapper_->base()->audio_processing();
842 if (audioproc) {
843 audioproc->SetExtraOptions(config);
844 }
845
kwiberg102c6a62015-10-30 02:47:38 -0700846 if (options.recording_sample_rate) {
847 LOG(LS_INFO) << "Recording sample rate is "
848 << *options.recording_sample_rate;
solenberg5b5129a2016-04-08 05:35:48 -0700849 if (adm()->SetRecordingSampleRate(*options.recording_sample_rate)) {
kwiberg102c6a62015-10-30 02:47:38 -0700850 LOG_RTCERR1(SetRecordingSampleRate, *options.recording_sample_rate);
buildbot@webrtc.org13d67762014-05-02 17:33:29 +0000851 }
852 }
853
kwiberg102c6a62015-10-30 02:47:38 -0700854 if (options.playout_sample_rate) {
855 LOG(LS_INFO) << "Playout sample rate is " << *options.playout_sample_rate;
solenberg5b5129a2016-04-08 05:35:48 -0700856 if (adm()->SetPlayoutSampleRate(*options.playout_sample_rate)) {
kwiberg102c6a62015-10-30 02:47:38 -0700857 LOG_RTCERR1(SetPlayoutSampleRate, *options.playout_sample_rate);
buildbot@webrtc.org13d67762014-05-02 17:33:29 +0000858 }
859 }
860
861 return true;
862}
863
solenberg246b8172015-12-08 09:50:23 -0800864void WebRtcVoiceEngine::SetDefaultDevices() {
solenberg566ef242015-11-06 15:34:49 -0800865 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
kjellanderfcfc8042016-01-14 11:01:09 -0800866#if !defined(WEBRTC_IOS)
solenberg246b8172015-12-08 09:50:23 -0800867 int in_id = kDefaultAudioDeviceId;
868 int out_id = kDefaultAudioDeviceId;
869 LOG(LS_INFO) << "Setting microphone to (id=" << in_id
870 << ") and speaker to (id=" << out_id << ")";
buildbot@webrtc.org13d67762014-05-02 17:33:29 +0000871
solenbergc1a1b352015-09-22 13:31:20 -0700872 bool ret = true;
solenberg246b8172015-12-08 09:50:23 -0800873 if (voe_wrapper_->hw()->SetRecordingDevice(in_id) == -1) {
874 LOG_RTCERR1(SetRecordingDevice, in_id);
buildbot@webrtc.org13d67762014-05-02 17:33:29 +0000875 ret = false;
876 }
solenberg246b8172015-12-08 09:50:23 -0800877 webrtc::AudioProcessing* ap = voe()->base()->audio_processing();
878 if (ap) {
879 ap->Initialize();
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000880 }
881
solenberg246b8172015-12-08 09:50:23 -0800882 if (voe_wrapper_->hw()->SetPlayoutDevice(out_id) == -1) {
883 LOG_RTCERR1(SetPlayoutDevice, out_id);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000884 ret = false;
885 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000886
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000887 if (ret) {
solenberg246b8172015-12-08 09:50:23 -0800888 LOG(LS_INFO) << "Set microphone to (id=" << in_id
889 << ") and speaker to (id=" << out_id << ")";
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000890 }
kjellanderfcfc8042016-01-14 11:01:09 -0800891#endif // !WEBRTC_IOS
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000892}
893
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000894bool WebRtcVoiceEngine::GetOutputVolume(int* level) {
solenberg566ef242015-11-06 15:34:49 -0800895 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000896 unsigned int ulevel;
897 if (voe_wrapper_->volume()->GetSpeakerVolume(ulevel) == -1) {
898 LOG_RTCERR1(GetSpeakerVolume, level);
899 return false;
900 }
901 *level = ulevel;
902 return true;
903}
904
905bool WebRtcVoiceEngine::SetOutputVolume(int level) {
solenberg566ef242015-11-06 15:34:49 -0800906 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
henrikg91d6ede2015-09-17 00:24:34 -0700907 RTC_DCHECK(level >= 0 && level <= 255);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000908 if (voe_wrapper_->volume()->SetSpeakerVolume(level) == -1) {
909 LOG_RTCERR1(SetSpeakerVolume, level);
910 return false;
911 }
912 return true;
913}
914
915int WebRtcVoiceEngine::GetInputLevel() {
solenberg566ef242015-11-06 15:34:49 -0800916 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000917 unsigned int ulevel;
918 return (voe_wrapper_->volume()->GetSpeechInputLevel(ulevel) != -1) ?
919 static_cast<int>(ulevel) : -1;
920}
921
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000922const std::vector<AudioCodec>& WebRtcVoiceEngine::codecs() {
solenberg566ef242015-11-06 15:34:49 -0800923 RTC_DCHECK(signal_thread_checker_.CalledOnValidThread());
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000924 return codecs_;
925}
926
Stefan Holmer9d69c3f2015-12-07 10:45:43 +0100927RtpCapabilities WebRtcVoiceEngine::GetCapabilities() const {
solenberg566ef242015-11-06 15:34:49 -0800928 RTC_DCHECK(signal_thread_checker_.CalledOnValidThread());
Stefan Holmer9d69c3f2015-12-07 10:45:43 +0100929 RtpCapabilities capabilities;
930 capabilities.header_extensions.push_back(RtpHeaderExtension(
931 kRtpAudioLevelHeaderExtension, kRtpAudioLevelHeaderExtensionDefaultId));
932 capabilities.header_extensions.push_back(
933 RtpHeaderExtension(kRtpAbsoluteSenderTimeHeaderExtension,
934 kRtpAbsoluteSenderTimeHeaderExtensionDefaultId));
stefanba4c0e42016-02-04 04:12:24 -0800935 if (webrtc::field_trial::FindFullName("WebRTC-Audio-SendSideBwe") ==
936 "Enabled") {
937 capabilities.header_extensions.push_back(RtpHeaderExtension(
938 kRtpTransportSequenceNumberHeaderExtension,
939 kRtpTransportSequenceNumberHeaderExtensionDefaultId));
940 }
Stefan Holmer9d69c3f2015-12-07 10:45:43 +0100941 return capabilities;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000942}
943
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000944int WebRtcVoiceEngine::GetLastEngineError() {
solenberg566ef242015-11-06 15:34:49 -0800945 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000946 return voe_wrapper_->error();
947}
948
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000949void WebRtcVoiceEngine::Print(webrtc::TraceLevel level, const char* trace,
950 int length) {
solenberg566ef242015-11-06 15:34:49 -0800951 // Note: This callback can happen on any thread!
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000952 rtc::LoggingSeverity sev = rtc::LS_VERBOSE;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000953 if (level == webrtc::kTraceError || level == webrtc::kTraceCritical)
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000954 sev = rtc::LS_ERROR;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000955 else if (level == webrtc::kTraceWarning)
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000956 sev = rtc::LS_WARNING;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000957 else if (level == webrtc::kTraceStateInfo || level == webrtc::kTraceInfo)
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000958 sev = rtc::LS_INFO;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000959 else if (level == webrtc::kTraceTerseInfo)
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000960 sev = rtc::LS_INFO;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000961
solenberg72e29d22016-03-08 06:35:16 -0800962 // Skip past boilerplate prefix text.
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000963 if (length < 72) {
964 std::string msg(trace, length);
965 LOG(LS_ERROR) << "Malformed webrtc log message: ";
966 LOG_V(sev) << msg;
967 } else {
968 std::string msg(trace + 71, length - 72);
Peter Boströmd5c75b12015-09-23 13:24:32 +0200969 LOG_V(sev) << "webrtc: " << msg;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000970 }
971}
972
solenberg63b34542015-09-29 06:06:31 -0700973void WebRtcVoiceEngine::RegisterChannel(WebRtcVoiceMediaChannel* channel) {
solenberg566ef242015-11-06 15:34:49 -0800974 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
975 RTC_DCHECK(channel);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000976 channels_.push_back(channel);
977}
978
solenberg63b34542015-09-29 06:06:31 -0700979void WebRtcVoiceEngine::UnregisterChannel(WebRtcVoiceMediaChannel* channel) {
solenberg566ef242015-11-06 15:34:49 -0800980 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
solenberg63b34542015-09-29 06:06:31 -0700981 auto it = std::find(channels_.begin(), channels_.end(), channel);
solenberg566ef242015-11-06 15:34:49 -0800982 RTC_DCHECK(it != channels_.end());
983 channels_.erase(it);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000984}
985
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000986// Adjusts the default AGC target level by the specified delta.
987// NB: If we start messing with other config fields, we'll want
988// to save the current webrtc::AgcConfig as well.
989bool WebRtcVoiceEngine::AdjustAgcLevel(int delta) {
solenberg566ef242015-11-06 15:34:49 -0800990 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000991 webrtc::AgcConfig config = default_agc_config_;
992 config.targetLeveldBOv -= delta;
993
994 LOG(LS_INFO) << "Adjusting AGC level from default -"
995 << default_agc_config_.targetLeveldBOv << "dB to -"
996 << config.targetLeveldBOv << "dB";
997
998 if (voe_wrapper_->processing()->SetAgcConfig(config) == -1) {
999 LOG_RTCERR1(SetAgcConfig, config.targetLeveldBOv);
1000 return false;
1001 }
1002 return true;
1003}
1004
ivocd66b44d2016-01-15 03:06:36 -08001005bool WebRtcVoiceEngine::StartAecDump(rtc::PlatformFile file,
1006 int64_t max_size_bytes) {
solenberg566ef242015-11-06 15:34:49 -08001007 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00001008 FILE* aec_dump_file_stream = rtc::FdopenPlatformFileForWriting(file);
wu@webrtc.orga8910d22014-01-23 22:12:45 +00001009 if (!aec_dump_file_stream) {
1010 LOG(LS_ERROR) << "Could not open AEC dump file stream.";
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00001011 if (!rtc::ClosePlatformFile(file))
wu@webrtc.orga8910d22014-01-23 22:12:45 +00001012 LOG(LS_WARNING) << "Could not close file.";
1013 return false;
1014 }
wu@webrtc.orga9890802013-12-13 00:21:03 +00001015 StopAecDump();
ivocd66b44d2016-01-15 03:06:36 -08001016 if (voe_wrapper_->base()->audio_processing()->StartDebugRecording(
1017 aec_dump_file_stream, max_size_bytes) !=
wu@webrtc.orga9890802013-12-13 00:21:03 +00001018 webrtc::AudioProcessing::kNoError) {
wu@webrtc.orga8910d22014-01-23 22:12:45 +00001019 LOG_RTCERR0(StartDebugRecording);
1020 fclose(aec_dump_file_stream);
wu@webrtc.orga9890802013-12-13 00:21:03 +00001021 return false;
1022 }
1023 is_dumping_aec_ = true;
1024 return true;
wu@webrtc.orga9890802013-12-13 00:21:03 +00001025}
1026
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001027void WebRtcVoiceEngine::StartAecDump(const std::string& filename) {
solenberg566ef242015-11-06 15:34:49 -08001028 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001029 if (!is_dumping_aec_) {
1030 // Start dumping AEC when we are not dumping.
ivocd66b44d2016-01-15 03:06:36 -08001031 if (voe_wrapper_->base()->audio_processing()->StartDebugRecording(
1032 filename.c_str(), -1) != webrtc::AudioProcessing::kNoError) {
wu@webrtc.orga9890802013-12-13 00:21:03 +00001033 LOG_RTCERR1(StartDebugRecording, filename.c_str());
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001034 } else {
1035 is_dumping_aec_ = true;
1036 }
1037 }
1038}
1039
1040void WebRtcVoiceEngine::StopAecDump() {
solenberg566ef242015-11-06 15:34:49 -08001041 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001042 if (is_dumping_aec_) {
1043 // Stop dumping AEC when we are dumping.
ivocd66b44d2016-01-15 03:06:36 -08001044 if (voe_wrapper_->base()->audio_processing()->StopDebugRecording() !=
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001045 webrtc::AudioProcessing::kNoError) {
1046 LOG_RTCERR0(StopDebugRecording);
1047 }
1048 is_dumping_aec_ = false;
1049 }
1050}
1051
ivoc112a3d82015-10-16 02:22:18 -07001052bool WebRtcVoiceEngine::StartRtcEventLog(rtc::PlatformFile file) {
solenberg566ef242015-11-06 15:34:49 -08001053 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
ivoc20834ca2016-02-04 06:33:37 -08001054 webrtc::RtcEventLog* event_log = voe_wrapper_->codec()->GetEventLog();
1055 if (event_log) {
1056 return event_log->StartLogging(file);
1057 }
1058 LOG_RTCERR0(StartRtcEventLog);
1059 return false;
ivoc112a3d82015-10-16 02:22:18 -07001060}
1061
1062void WebRtcVoiceEngine::StopRtcEventLog() {
solenberg566ef242015-11-06 15:34:49 -08001063 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
ivoc20834ca2016-02-04 06:33:37 -08001064 webrtc::RtcEventLog* event_log = voe_wrapper_->codec()->GetEventLog();
1065 if (event_log) {
1066 event_log->StopLogging();
1067 return;
1068 }
1069 LOG_RTCERR0(StopRtcEventLog);
ivoc112a3d82015-10-16 02:22:18 -07001070}
1071
solenberg0a617e22015-10-20 15:49:38 -07001072int WebRtcVoiceEngine::CreateVoEChannel() {
solenberg566ef242015-11-06 15:34:49 -08001073 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
solenberg0a617e22015-10-20 15:49:38 -07001074 return voe_wrapper_->base()->CreateChannel(voe_config_);
sergeyu@chromium.org5bc25c42013-12-05 00:24:06 +00001075}
1076
solenberg5b5129a2016-04-08 05:35:48 -07001077webrtc::AudioDeviceModule* WebRtcVoiceEngine::adm() {
1078 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
1079 RTC_DCHECK(adm_);
1080 return adm_;
1081}
1082
solenbergc96df772015-10-21 13:01:53 -07001083class WebRtcVoiceMediaChannel::WebRtcAudioSendStream
Taylor Brandstetter1a018dc2016-03-08 12:37:39 -08001084 : public AudioSource::Sink {
mallinath@webrtc.org67ee6b92014-02-03 16:57:16 +00001085 public:
skvlade0d46372016-04-07 22:59:22 -07001086 WebRtcAudioSendStream(int ch,
1087 webrtc::AudioTransport* voe_audio_transport,
1088 uint32_t ssrc,
1089 const std::string& c_name,
solenberg3a941542015-11-16 07:34:50 -08001090 const std::vector<webrtc::RtpExtension>& extensions,
1091 webrtc::Call* call)
solenberg7add0582015-11-20 09:59:34 -08001092 : voe_audio_transport_(voe_audio_transport),
solenberg3a941542015-11-16 07:34:50 -08001093 call_(call),
skvlade0d46372016-04-07 22:59:22 -07001094 config_(nullptr),
1095 rtp_parameters_(CreateRtpParametersWithOneEncoding()) {
solenberg85a04962015-10-27 03:35:21 -07001096 RTC_DCHECK_GE(ch, 0);
1097 // TODO(solenberg): Once we're not using FakeWebRtcVoiceEngine anymore:
1098 // RTC_DCHECK(voe_audio_transport);
solenbergc96df772015-10-21 13:01:53 -07001099 RTC_DCHECK(call);
solenberg85a04962015-10-27 03:35:21 -07001100 audio_capture_thread_checker_.DetachFromThread();
solenberg3a941542015-11-16 07:34:50 -08001101 config_.rtp.ssrc = ssrc;
1102 config_.rtp.c_name = c_name;
1103 config_.voe_channel_id = ch;
1104 RecreateAudioSendStream(extensions);
solenbergc96df772015-10-21 13:01:53 -07001105 }
solenberg3a941542015-11-16 07:34:50 -08001106
solenbergc96df772015-10-21 13:01:53 -07001107 ~WebRtcAudioSendStream() override {
solenberg566ef242015-11-06 15:34:49 -08001108 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
Taylor Brandstetter1a018dc2016-03-08 12:37:39 -08001109 ClearSource();
solenbergc96df772015-10-21 13:01:53 -07001110 call_->DestroyAudioSendStream(stream_);
1111 }
henrike@webrtc.org1e09a712013-07-26 19:17:59 +00001112
solenberg3a941542015-11-16 07:34:50 -08001113 void RecreateAudioSendStream(
1114 const std::vector<webrtc::RtpExtension>& extensions) {
1115 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
1116 if (stream_) {
1117 call_->DestroyAudioSendStream(stream_);
1118 stream_ = nullptr;
1119 }
1120 config_.rtp.extensions = extensions;
1121 RTC_DCHECK(!stream_);
1122 stream_ = call_->CreateAudioSendStream(config_);
1123 RTC_CHECK(stream_);
solenberg6d6e7c52016-04-13 09:07:30 -07001124 UpdateSendState();
solenberg3a941542015-11-16 07:34:50 -08001125 }
1126
solenberg8842c3e2016-03-11 03:06:41 -08001127 bool SendTelephoneEvent(int payload_type, int event, int duration_ms) {
Fredrik Solenbergb5727682015-12-04 15:22:19 +01001128 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
1129 RTC_DCHECK(stream_);
1130 return stream_->SendTelephoneEvent(payload_type, event, duration_ms);
1131 }
1132
Taylor Brandstetter1a018dc2016-03-08 12:37:39 -08001133 void SetSend(bool send) {
1134 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
1135 send_ = send;
1136 UpdateSendState();
1137 }
1138
solenberg3a941542015-11-16 07:34:50 -08001139 webrtc::AudioSendStream::Stats GetStats() const {
1140 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
1141 RTC_DCHECK(stream_);
1142 return stream_->GetStats();
1143 }
1144
Taylor Brandstetter1a018dc2016-03-08 12:37:39 -08001145 // Starts the sending by setting ourselves as a sink to the AudioSource to
1146 // get data callbacks.
henrike@webrtc.orga7b98182014-02-21 15:51:43 +00001147 // This method is called on the libjingle worker thread.
mallinath@webrtc.org67ee6b92014-02-03 16:57:16 +00001148 // TODO(xians): Make sure Start() is called only once.
Taylor Brandstetter1a018dc2016-03-08 12:37:39 -08001149 void SetSource(AudioSource* source) {
solenberg566ef242015-11-06 15:34:49 -08001150 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
Taylor Brandstetter1a018dc2016-03-08 12:37:39 -08001151 RTC_DCHECK(source);
1152 if (source_) {
1153 RTC_DCHECK(source_ == source);
mallinath@webrtc.org67ee6b92014-02-03 16:57:16 +00001154 return;
1155 }
Taylor Brandstetter1a018dc2016-03-08 12:37:39 -08001156 source->SetSink(this);
1157 source_ = source;
1158 UpdateSendState();
mallinath@webrtc.org67ee6b92014-02-03 16:57:16 +00001159 }
1160
Taylor Brandstetter1a018dc2016-03-08 12:37:39 -08001161 // Stops sending by setting the sink of the AudioSource to nullptr. No data
mallinath@webrtc.org67ee6b92014-02-03 16:57:16 +00001162 // callback will be received after this method.
henrike@webrtc.orga7b98182014-02-21 15:51:43 +00001163 // This method is called on the libjingle worker thread.
Taylor Brandstetter1a018dc2016-03-08 12:37:39 -08001164 void ClearSource() {
solenberg566ef242015-11-06 15:34:49 -08001165 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
Taylor Brandstetter1a018dc2016-03-08 12:37:39 -08001166 if (source_) {
1167 source_->SetSink(nullptr);
1168 source_ = nullptr;
solenberg98c68862015-10-09 03:27:14 -07001169 }
Taylor Brandstetter1a018dc2016-03-08 12:37:39 -08001170 UpdateSendState();
mallinath@webrtc.org67ee6b92014-02-03 16:57:16 +00001171 }
1172
Taylor Brandstetter1a018dc2016-03-08 12:37:39 -08001173 // AudioSource::Sink implementation.
henrike@webrtc.orga7b98182014-02-21 15:51:43 +00001174 // This method is called on the audio thread.
kjellander@webrtc.org14665ff2015-03-04 12:58:35 +00001175 void OnData(const void* audio_data,
1176 int bits_per_sample,
1177 int sample_rate,
Peter Kasting69558702016-01-12 16:26:35 -08001178 size_t number_of_channels,
Peter Kastingdce40cf2015-08-24 14:52:23 -07001179 size_t number_of_frames) override {
solenberg566ef242015-11-06 15:34:49 -08001180 RTC_DCHECK(!worker_thread_checker_.CalledOnValidThread());
solenberg85a04962015-10-27 03:35:21 -07001181 RTC_DCHECK(audio_capture_thread_checker_.CalledOnValidThread());
solenbergc96df772015-10-21 13:01:53 -07001182 RTC_DCHECK(voe_audio_transport_);
solenberg7add0582015-11-20 09:59:34 -08001183 voe_audio_transport_->OnData(config_.voe_channel_id,
henrike@webrtc.orga7b98182014-02-21 15:51:43 +00001184 audio_data,
1185 bits_per_sample,
1186 sample_rate,
1187 number_of_channels,
1188 number_of_frames);
henrike@webrtc.orga7b98182014-02-21 15:51:43 +00001189 }
1190
Taylor Brandstetter1a018dc2016-03-08 12:37:39 -08001191 // Callback from the |source_| when it is going away. In case Start() has
henrike@webrtc.orga7b98182014-02-21 15:51:43 +00001192 // never been called, this callback won't be triggered.
kjellander@webrtc.org14665ff2015-03-04 12:58:35 +00001193 void OnClose() override {
solenberg566ef242015-11-06 15:34:49 -08001194 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
Taylor Brandstetter1a018dc2016-03-08 12:37:39 -08001195 // Set |source_| to nullptr to make sure no more callback will get into
1196 // the source.
1197 source_ = nullptr;
1198 UpdateSendState();
mallinath@webrtc.org67ee6b92014-02-03 16:57:16 +00001199 }
1200
1201 // Accessor to the VoE channel ID.
solenberg85a04962015-10-27 03:35:21 -07001202 int channel() const {
solenberg566ef242015-11-06 15:34:49 -08001203 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
solenberg7add0582015-11-20 09:59:34 -08001204 return config_.voe_channel_id;
solenberg85a04962015-10-27 03:35:21 -07001205 }
mallinath@webrtc.org67ee6b92014-02-03 16:57:16 +00001206
skvlade0d46372016-04-07 22:59:22 -07001207 const webrtc::RtpParameters& rtp_parameters() const {
1208 return rtp_parameters_;
1209 }
1210
1211 void set_rtp_parameters(const webrtc::RtpParameters& parameters) {
1212 RTC_CHECK_EQ(1UL, parameters.encodings.size());
1213 rtp_parameters_ = parameters;
1214 }
1215
mallinath@webrtc.org67ee6b92014-02-03 16:57:16 +00001216 private:
Taylor Brandstetter1a018dc2016-03-08 12:37:39 -08001217 void UpdateSendState() {
1218 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
1219 RTC_DCHECK(stream_);
1220 if (send_ && source_ != nullptr) {
1221 stream_->Start();
1222 } else { // !send || source_ = nullptr
1223 stream_->Stop();
1224 }
1225 }
1226
solenberg566ef242015-11-06 15:34:49 -08001227 rtc::ThreadChecker worker_thread_checker_;
solenberg85a04962015-10-27 03:35:21 -07001228 rtc::ThreadChecker audio_capture_thread_checker_;
solenbergc96df772015-10-21 13:01:53 -07001229 webrtc::AudioTransport* const voe_audio_transport_ = nullptr;
1230 webrtc::Call* call_ = nullptr;
solenberg3a941542015-11-16 07:34:50 -08001231 webrtc::AudioSendStream::Config config_;
1232 // The stream is owned by WebRtcAudioSendStream and may be reallocated if
1233 // configuration changes.
solenbergc96df772015-10-21 13:01:53 -07001234 webrtc::AudioSendStream* stream_ = nullptr;
mallinath@webrtc.org67ee6b92014-02-03 16:57:16 +00001235
Taylor Brandstetter1a018dc2016-03-08 12:37:39 -08001236 // Raw pointer to AudioSource owned by LocalAudioTrackHandler.
mallinath@webrtc.org67ee6b92014-02-03 16:57:16 +00001237 // PeerConnection will make sure invalidating the pointer before the object
1238 // goes away.
Taylor Brandstetter1a018dc2016-03-08 12:37:39 -08001239 AudioSource* source_ = nullptr;
1240 bool send_ = false;
skvlade0d46372016-04-07 22:59:22 -07001241 webrtc::RtpParameters rtp_parameters_;
henrike@webrtc.orga7b98182014-02-21 15:51:43 +00001242
solenbergc96df772015-10-21 13:01:53 -07001243 RTC_DISALLOW_IMPLICIT_CONSTRUCTORS(WebRtcAudioSendStream);
1244};
1245
1246class WebRtcVoiceMediaChannel::WebRtcAudioReceiveStream {
1247 public:
stefanba4c0e42016-02-04 04:12:24 -08001248 WebRtcAudioReceiveStream(int ch,
1249 uint32_t remote_ssrc,
1250 uint32_t local_ssrc,
1251 bool use_transport_cc,
1252 const std::string& sync_group,
solenberg7add0582015-11-20 09:59:34 -08001253 const std::vector<webrtc::RtpExtension>& extensions,
1254 webrtc::Call* call)
stefanba4c0e42016-02-04 04:12:24 -08001255 : call_(call), config_() {
solenberg7add0582015-11-20 09:59:34 -08001256 RTC_DCHECK_GE(ch, 0);
1257 RTC_DCHECK(call);
1258 config_.rtp.remote_ssrc = remote_ssrc;
1259 config_.rtp.local_ssrc = local_ssrc;
1260 config_.voe_channel_id = ch;
1261 config_.sync_group = sync_group;
stefanba4c0e42016-02-04 04:12:24 -08001262 RecreateAudioReceiveStream(use_transport_cc, extensions);
solenberg7add0582015-11-20 09:59:34 -08001263 }
solenbergc96df772015-10-21 13:01:53 -07001264
solenberg7add0582015-11-20 09:59:34 -08001265 ~WebRtcAudioReceiveStream() {
1266 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
1267 call_->DestroyAudioReceiveStream(stream_);
1268 }
1269
1270 void RecreateAudioReceiveStream(
1271 const std::vector<webrtc::RtpExtension>& extensions) {
1272 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
stefanba4c0e42016-02-04 04:12:24 -08001273 RecreateAudioReceiveStream(config_.rtp.transport_cc, extensions);
solenberg7add0582015-11-20 09:59:34 -08001274 }
stefanba4c0e42016-02-04 04:12:24 -08001275 void RecreateAudioReceiveStream(bool use_transport_cc) {
solenberg7add0582015-11-20 09:59:34 -08001276 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
stefanba4c0e42016-02-04 04:12:24 -08001277 RecreateAudioReceiveStream(use_transport_cc, config_.rtp.extensions);
solenberg7add0582015-11-20 09:59:34 -08001278 }
1279
1280 webrtc::AudioReceiveStream::Stats GetStats() const {
1281 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
1282 RTC_DCHECK(stream_);
1283 return stream_->GetStats();
1284 }
1285
1286 int channel() const {
1287 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
1288 return config_.voe_channel_id;
1289 }
solenbergc96df772015-10-21 13:01:53 -07001290
kwiberg686a8ef2016-02-26 03:00:35 -08001291 void SetRawAudioSink(std::unique_ptr<webrtc::AudioSinkInterface> sink) {
Tommif888bb52015-12-12 01:37:01 +01001292 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
kwiberg686a8ef2016-02-26 03:00:35 -08001293 stream_->SetSink(std::move(sink));
Tommif888bb52015-12-12 01:37:01 +01001294 }
1295
solenbergc96df772015-10-21 13:01:53 -07001296 private:
stefanba4c0e42016-02-04 04:12:24 -08001297 void RecreateAudioReceiveStream(
1298 bool use_transport_cc,
solenberg7add0582015-11-20 09:59:34 -08001299 const std::vector<webrtc::RtpExtension>& extensions) {
1300 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
1301 if (stream_) {
1302 call_->DestroyAudioReceiveStream(stream_);
1303 stream_ = nullptr;
1304 }
1305 config_.rtp.extensions = extensions;
stefanba4c0e42016-02-04 04:12:24 -08001306 config_.rtp.transport_cc = use_transport_cc;
solenberg7add0582015-11-20 09:59:34 -08001307 RTC_DCHECK(!stream_);
1308 stream_ = call_->CreateAudioReceiveStream(config_);
1309 RTC_CHECK(stream_);
1310 }
1311
1312 rtc::ThreadChecker worker_thread_checker_;
1313 webrtc::Call* call_ = nullptr;
1314 webrtc::AudioReceiveStream::Config config_;
1315 // The stream is owned by WebRtcAudioReceiveStream and may be reallocated if
1316 // configuration changes.
1317 webrtc::AudioReceiveStream* stream_ = nullptr;
solenbergc96df772015-10-21 13:01:53 -07001318
1319 RTC_DISALLOW_IMPLICIT_CONSTRUCTORS(WebRtcAudioReceiveStream);
henrike@webrtc.org1e09a712013-07-26 19:17:59 +00001320};
1321
Fredrik Solenberg709ed672015-09-15 12:26:33 +02001322WebRtcVoiceMediaChannel::WebRtcVoiceMediaChannel(WebRtcVoiceEngine* engine,
nisse51542be2016-02-12 02:27:06 -08001323 const MediaConfig& config,
Fredrik Solenbergb071a192015-09-17 16:42:56 +02001324 const AudioOptions& options,
Fredrik Solenberg709ed672015-09-15 12:26:33 +02001325 webrtc::Call* call)
nisse51542be2016-02-12 02:27:06 -08001326 : VoiceMediaChannel(config), engine_(engine), call_(call) {
solenberg0a617e22015-10-20 15:49:38 -07001327 LOG(LS_VERBOSE) << "WebRtcVoiceMediaChannel::WebRtcVoiceMediaChannel";
solenberg566ef242015-11-06 15:34:49 -08001328 RTC_DCHECK(call);
solenberg0a617e22015-10-20 15:49:38 -07001329 engine->RegisterChannel(this);
Fredrik Solenbergb071a192015-09-17 16:42:56 +02001330 SetOptions(options);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001331}
1332
1333WebRtcVoiceMediaChannel::~WebRtcVoiceMediaChannel() {
solenberg566ef242015-11-06 15:34:49 -08001334 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
solenberg0a617e22015-10-20 15:49:38 -07001335 LOG(LS_VERBOSE) << "WebRtcVoiceMediaChannel::~WebRtcVoiceMediaChannel";
solenberg7add0582015-11-20 09:59:34 -08001336 // TODO(solenberg): Should be able to delete the streams directly, without
1337 // going through RemoveNnStream(), once stream objects handle
1338 // all (de)configuration.
solenbergc96df772015-10-21 13:01:53 -07001339 while (!send_streams_.empty()) {
1340 RemoveSendStream(send_streams_.begin()->first);
solenbergd97ec302015-10-07 01:40:33 -07001341 }
solenberg7add0582015-11-20 09:59:34 -08001342 while (!recv_streams_.empty()) {
1343 RemoveRecvStream(recv_streams_.begin()->first);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001344 }
solenberg0a617e22015-10-20 15:49:38 -07001345 engine()->UnregisterChannel(this);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001346}
1347
nisse51542be2016-02-12 02:27:06 -08001348rtc::DiffServCodePoint WebRtcVoiceMediaChannel::PreferredDscp() const {
1349 return kAudioDscpValue;
1350}
1351
Peter Thatcherc2ee2c82015-08-07 16:05:34 -07001352bool WebRtcVoiceMediaChannel::SetSendParameters(
1353 const AudioSendParameters& params) {
Peter Boströmca8b4042016-03-08 14:24:13 -08001354 TRACE_EVENT0("webrtc", "WebRtcVoiceMediaChannel::SetSendParameters");
solenberg566ef242015-11-06 15:34:49 -08001355 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
solenberg7e4e01a2015-12-02 08:05:01 -08001356 LOG(LS_INFO) << "WebRtcVoiceMediaChannel::SetSendParameters: "
1357 << params.ToString();
Peter Thatcherc2ee2c82015-08-07 16:05:34 -07001358 // TODO(pthatcher): Refactor this to be more clean now that we have
1359 // all the information at once.
solenberg3a941542015-11-16 07:34:50 -08001360
1361 if (!SetSendCodecs(params.codecs)) {
1362 return false;
1363 }
1364
solenberg7e4e01a2015-12-02 08:05:01 -08001365 if (!ValidateRtpExtensions(params.extensions)) {
1366 return false;
1367 }
1368 std::vector<webrtc::RtpExtension> filtered_extensions =
1369 FilterRtpExtensions(params.extensions,
1370 webrtc::RtpExtension::IsSupportedForAudio, true);
1371 if (send_rtp_extensions_ != filtered_extensions) {
1372 send_rtp_extensions_.swap(filtered_extensions);
solenberg3a941542015-11-16 07:34:50 -08001373 for (auto& it : send_streams_) {
1374 it.second->RecreateAudioSendStream(send_rtp_extensions_);
1375 }
1376 }
1377
skvlade0d46372016-04-07 22:59:22 -07001378 if (!SetSendBitrate(params.max_bandwidth_bps)) {
solenberg3a941542015-11-16 07:34:50 -08001379 return false;
1380 }
1381 return SetOptions(params.options);
Peter Thatcherc2ee2c82015-08-07 16:05:34 -07001382}
1383
1384bool WebRtcVoiceMediaChannel::SetRecvParameters(
1385 const AudioRecvParameters& params) {
Peter Boströmca8b4042016-03-08 14:24:13 -08001386 TRACE_EVENT0("webrtc", "WebRtcVoiceMediaChannel::SetRecvParameters");
solenberg566ef242015-11-06 15:34:49 -08001387 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
solenberg7e4e01a2015-12-02 08:05:01 -08001388 LOG(LS_INFO) << "WebRtcVoiceMediaChannel::SetRecvParameters: "
1389 << params.ToString();
Peter Thatcherc2ee2c82015-08-07 16:05:34 -07001390 // TODO(pthatcher): Refactor this to be more clean now that we have
1391 // all the information at once.
solenberg7add0582015-11-20 09:59:34 -08001392
1393 if (!SetRecvCodecs(params.codecs)) {
1394 return false;
1395 }
1396
solenberg7e4e01a2015-12-02 08:05:01 -08001397 if (!ValidateRtpExtensions(params.extensions)) {
1398 return false;
1399 }
1400 std::vector<webrtc::RtpExtension> filtered_extensions =
1401 FilterRtpExtensions(params.extensions,
1402 webrtc::RtpExtension::IsSupportedForAudio, false);
1403 if (recv_rtp_extensions_ != filtered_extensions) {
1404 recv_rtp_extensions_.swap(filtered_extensions);
solenberg7add0582015-11-20 09:59:34 -08001405 for (auto& it : recv_streams_) {
1406 it.second->RecreateAudioReceiveStream(recv_rtp_extensions_);
1407 }
1408 }
solenberg7add0582015-11-20 09:59:34 -08001409 return true;
Peter Thatcherc2ee2c82015-08-07 16:05:34 -07001410}
1411
skvlade0d46372016-04-07 22:59:22 -07001412webrtc::RtpParameters WebRtcVoiceMediaChannel::GetRtpParameters(
1413 uint32_t ssrc) const {
1414 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
1415 auto it = send_streams_.find(ssrc);
1416 if (it == send_streams_.end()) {
1417 LOG(LS_WARNING) << "Attempting to get RTP parameters for stream with ssrc "
1418 << ssrc << " which doesn't exist.";
1419 return webrtc::RtpParameters();
1420 }
1421
1422 return it->second->rtp_parameters();
1423}
1424
1425bool WebRtcVoiceMediaChannel::SetRtpParameters(
1426 uint32_t ssrc,
1427 const webrtc::RtpParameters& parameters) {
1428 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
1429 if (!ValidateRtpParameters(parameters)) {
1430 return false;
1431 }
1432 auto it = send_streams_.find(ssrc);
1433 if (it == send_streams_.end()) {
1434 LOG(LS_WARNING) << "Attempting to set RTP parameters for stream with ssrc "
1435 << ssrc << " which doesn't exist.";
1436 return false;
1437 }
1438
1439 if (!SetChannelParameters(it->second->channel(), parameters)) {
1440 LOG(LS_WARNING) << "Failed to set RtpParameters.";
1441 return false;
1442 }
1443 it->second->set_rtp_parameters(parameters);
1444 return true;
1445}
1446
1447bool WebRtcVoiceMediaChannel::ValidateRtpParameters(
1448 const webrtc::RtpParameters& rtp_parameters) {
1449 if (rtp_parameters.encodings.size() != 1) {
1450 LOG(LS_ERROR)
1451 << "Attempted to set RtpParameters without exactly one encoding";
1452 return false;
1453 }
1454 return true;
1455}
1456
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001457bool WebRtcVoiceMediaChannel::SetOptions(const AudioOptions& options) {
solenberg566ef242015-11-06 15:34:49 -08001458 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001459 LOG(LS_INFO) << "Setting voice channel options: "
1460 << options.ToString();
1461
1462 // We retain all of the existing options, and apply the given ones
1463 // on top. This means there is no way to "clear" options such that
1464 // they go back to the engine default.
1465 options_.SetAll(options);
solenberg246b8172015-12-08 09:50:23 -08001466 if (!engine()->ApplyOptions(options_)) {
1467 LOG(LS_WARNING) <<
1468 "Failed to apply engine options during channel SetOptions.";
1469 return false;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001470 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001471 LOG(LS_INFO) << "Set voice channel options. Current options: "
1472 << options_.ToString();
1473 return true;
1474}
1475
1476bool WebRtcVoiceMediaChannel::SetRecvCodecs(
1477 const std::vector<AudioCodec>& codecs) {
solenberg566ef242015-11-06 15:34:49 -08001478 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
solenberg8fb30c32015-10-13 03:06:58 -07001479
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001480 // Set the payload types to be used for incoming media.
solenberg0b675462015-10-09 01:37:09 -07001481 LOG(LS_INFO) << "Setting receive voice codecs.";
solenberg0b675462015-10-09 01:37:09 -07001482
1483 if (!VerifyUniquePayloadTypes(codecs)) {
1484 LOG(LS_ERROR) << "Codec payload types overlap.";
1485 return false;
1486 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001487
1488 std::vector<AudioCodec> new_codecs;
1489 // Find all new codecs. We allow adding new codecs but don't allow changing
1490 // the payload type of codecs that is already configured since we might
1491 // already be receiving packets with that payload type.
Fredrik Solenbergaf9fb212015-08-26 10:45:53 +02001492 for (const AudioCodec& codec : codecs) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001493 AudioCodec old_codec;
Fredrik Solenbergaf9fb212015-08-26 10:45:53 +02001494 if (FindCodec(recv_codecs_, codec, &old_codec)) {
1495 if (old_codec.id != codec.id) {
1496 LOG(LS_ERROR) << codec.name << " payload type changed.";
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001497 return false;
1498 }
1499 } else {
Fredrik Solenbergaf9fb212015-08-26 10:45:53 +02001500 new_codecs.push_back(codec);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001501 }
1502 }
1503 if (new_codecs.empty()) {
1504 // There are no new codecs to configure. Already configured codecs are
1505 // never removed.
1506 return true;
1507 }
1508
1509 if (playout_) {
1510 // Receive codecs can not be changed while playing. So we temporarily
1511 // pause playout.
1512 PausePlayout();
1513 }
1514
solenberg26c8c912015-11-27 04:00:25 -08001515 bool result = true;
1516 for (const AudioCodec& codec : new_codecs) {
solenberg72e29d22016-03-08 06:35:16 -08001517 webrtc::CodecInst voe_codec = {0};
solenberg26c8c912015-11-27 04:00:25 -08001518 if (WebRtcVoiceEngine::ToCodecInst(codec, &voe_codec)) {
1519 LOG(LS_INFO) << ToString(codec);
1520 voe_codec.pltype = codec.id;
1521 for (const auto& ch : recv_streams_) {
1522 if (engine()->voe()->codec()->SetRecPayloadType(
1523 ch.second->channel(), voe_codec) == -1) {
1524 LOG_RTCERR2(SetRecPayloadType, ch.second->channel(),
1525 ToString(voe_codec));
1526 result = false;
1527 }
1528 }
1529 } else {
1530 LOG(LS_WARNING) << "Unknown codec " << ToString(codec);
1531 result = false;
1532 break;
1533 }
1534 }
Fredrik Solenbergaf9fb212015-08-26 10:45:53 +02001535 if (result) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001536 recv_codecs_ = codecs;
1537 }
1538
1539 if (desired_playout_ && !playout_) {
1540 ResumePlayout();
1541 }
Fredrik Solenbergaf9fb212015-08-26 10:45:53 +02001542 return result;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001543}
1544
solenberg72e29d22016-03-08 06:35:16 -08001545// Utility function called from SetSendParameters() to extract current send
1546// codec settings from the given list of codecs (originally from SDP). Both send
1547// and receive streams may be reconfigured based on the new settings.
wu@webrtc.orgcadf9042013-08-30 21:24:16 +00001548bool WebRtcVoiceMediaChannel::SetSendCodecs(
1549 const std::vector<AudioCodec>& codecs) {
solenberg566ef242015-11-06 15:34:49 -08001550 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
Fredrik Solenbergb5727682015-12-04 15:22:19 +01001551 // TODO(solenberg): Validate input - that payload types don't overlap, are
1552 // within range, filter out codecs we don't support,
solenberg31642aa2016-03-14 08:00:37 -07001553 // redundant codecs etc - the same way it is done for
1554 // RtpHeaderExtensions.
solenbergd97ec302015-10-07 01:40:33 -07001555
Fredrik Solenbergb5727682015-12-04 15:22:19 +01001556 // Find the DTMF telephone event "codec" payload type.
1557 dtmf_payload_type_ = rtc::Optional<int>();
Fredrik Solenbergaf9fb212015-08-26 10:45:53 +02001558 for (const AudioCodec& codec : codecs) {
Fredrik Solenbergaf9fb212015-08-26 10:45:53 +02001559 if (IsCodec(codec, kDtmfCodecName)) {
solenberg31642aa2016-03-14 08:00:37 -07001560 if (codec.id < kMinPayloadType || codec.id > kMaxPayloadType) {
1561 return false;
1562 }
Fredrik Solenbergb5727682015-12-04 15:22:19 +01001563 dtmf_payload_type_ = rtc::Optional<int>(codec.id);
1564 break;
wu@webrtc.orgcadf9042013-08-30 21:24:16 +00001565 }
1566 }
1567
solenberg72e29d22016-03-08 06:35:16 -08001568 // Scan through the list to figure out the codec to use for sending, along
1569 // with the proper configuration for VAD, CNG, RED, NACK and Opus-specific
1570 // parameters.
1571 {
1572 SendCodecSpec send_codec_spec;
1573 send_codec_spec.nack_enabled = send_codec_spec_.nack_enabled;
1574
1575 // Find send codec (the first non-telephone-event/CN codec).
1576 const AudioCodec* codec = WebRtcVoiceCodecs::GetPreferredCodec(
1577 codecs, &send_codec_spec.codec_inst, &send_codec_spec.red_payload_type);
1578 if (!codec) {
1579 LOG(LS_WARNING) << "Received empty list of codecs.";
1580 return false;
1581 }
1582
1583 send_codec_spec.transport_cc_enabled = HasTransportCc(*codec);
1584
1585 // This condition is apparently here because Opus does not support RED and
1586 // FEC simultaneously. However, DTX and max playback rate shouldn't have
1587 // such limitations.
1588 // TODO(solenberg): Refactor this logic once we create AudioEncoders here.
1589 if (send_codec_spec.red_payload_type == -1) {
1590 send_codec_spec.nack_enabled = HasNack(*codec);
1591 // For Opus as the send codec, we are to determine inband FEC, maximum
1592 // playback rate, and opus internal dtx.
1593 if (IsCodec(*codec, kOpusCodecName)) {
1594 GetOpusConfig(*codec, &send_codec_spec.codec_inst,
1595 &send_codec_spec.enable_codec_fec,
1596 &send_codec_spec.opus_max_playback_rate,
1597 &send_codec_spec.enable_opus_dtx);
1598 }
1599
1600 // Set packet size if the AudioCodec param kCodecParamPTime is set.
1601 int ptime_ms = 0;
1602 if (codec->GetParam(kCodecParamPTime, &ptime_ms)) {
1603 if (!WebRtcVoiceCodecs::SetPTimeAsPacketSize(
1604 &send_codec_spec.codec_inst, ptime_ms)) {
1605 LOG(LS_WARNING) << "Failed to set packet size for codec "
1606 << send_codec_spec.codec_inst.plname;
1607 return false;
1608 }
1609 }
1610 }
1611
1612 // Loop through the codecs list again to find the CN codec.
1613 // TODO(solenberg): Break out into a separate function?
1614 for (const AudioCodec& codec : codecs) {
1615 // Ignore codecs we don't know about. The negotiation step should prevent
1616 // this, but double-check to be sure.
1617 webrtc::CodecInst voe_codec = {0};
1618 if (!WebRtcVoiceEngine::ToCodecInst(codec, &voe_codec)) {
1619 LOG(LS_WARNING) << "Unknown codec " << ToString(codec);
1620 continue;
1621 }
1622
1623 if (IsCodec(codec, kCnCodecName)) {
1624 // Turn voice activity detection/comfort noise on if supported.
1625 // Set the wideband CN payload type appropriately.
1626 // (narrowband always uses the static payload type 13).
1627 int cng_plfreq = -1;
1628 switch (codec.clockrate) {
1629 case 8000:
1630 case 16000:
1631 case 32000:
1632 cng_plfreq = codec.clockrate;
1633 break;
1634 default:
1635 LOG(LS_WARNING) << "CN frequency " << codec.clockrate
1636 << " not supported.";
1637 continue;
1638 }
1639 send_codec_spec.cng_payload_type = codec.id;
1640 send_codec_spec.cng_plfreq = cng_plfreq;
1641 break;
1642 }
1643 }
1644
1645 // Latch in the new state.
1646 send_codec_spec_ = std::move(send_codec_spec);
1647 }
1648
wu@webrtc.orgcadf9042013-08-30 21:24:16 +00001649 // Cache the codecs in order to configure the channel created later.
solenbergc96df772015-10-21 13:01:53 -07001650 for (const auto& ch : send_streams_) {
skvlade0d46372016-04-07 22:59:22 -07001651 if (!SetSendCodecs(ch.second->channel(), ch.second->rtp_parameters())) {
wu@webrtc.orgcadf9042013-08-30 21:24:16 +00001652 return false;
1653 }
1654 }
1655
solenberg72e29d22016-03-08 06:35:16 -08001656 // Set nack status on receive channels.
1657 if (!send_streams_.empty()) {
1658 for (const auto& kv : recv_streams_) {
1659 SetNack(kv.second->channel(), send_codec_spec_.nack_enabled);
1660 }
wu@webrtc.org9dba5252013-08-05 20:36:57 +00001661 }
solenberg0a617e22015-10-20 15:49:38 -07001662
stefanba4c0e42016-02-04 04:12:24 -08001663 // Check if the transport cc feedback has changed on the preferred send codec,
1664 // and in that case reconfigure all receive streams.
solenberg72e29d22016-03-08 06:35:16 -08001665 if (recv_transport_cc_enabled_ != send_codec_spec_.transport_cc_enabled) {
1666 LOG(LS_INFO) << "Recreate all the receive streams because the send "
1667 "codec has changed.";
1668 recv_transport_cc_enabled_ = send_codec_spec_.transport_cc_enabled;
1669 for (auto& kv : recv_streams_) {
1670 kv.second->RecreateAudioReceiveStream(recv_transport_cc_enabled_);
1671 }
1672 }
1673
1674 return true;
1675}
1676
1677// Apply current codec settings to a single voe::Channel used for sending.
skvlade0d46372016-04-07 22:59:22 -07001678bool WebRtcVoiceMediaChannel::SetSendCodecs(
1679 int channel,
1680 const webrtc::RtpParameters& rtp_parameters) {
solenberg72e29d22016-03-08 06:35:16 -08001681 // Disable VAD, FEC, and RED unless we know the other side wants them.
1682 engine()->voe()->codec()->SetVADStatus(channel, false);
1683 engine()->voe()->rtp()->SetNACKStatus(channel, false, 0);
1684 engine()->voe()->rtp()->SetREDStatus(channel, false);
1685 engine()->voe()->codec()->SetFECStatus(channel, false);
1686
1687 if (send_codec_spec_.red_payload_type != -1) {
1688 // Enable redundant encoding of the specified codec. Treat any
1689 // failure as a fatal internal error.
1690 LOG(LS_INFO) << "Enabling RED on channel " << channel;
1691 if (engine()->voe()->rtp()->SetREDStatus(channel, true,
1692 send_codec_spec_.red_payload_type) == -1) {
1693 LOG_RTCERR3(SetREDStatus, channel, true,
1694 send_codec_spec_.red_payload_type);
1695 return false;
1696 }
1697 }
1698
1699 SetNack(channel, send_codec_spec_.nack_enabled);
1700
1701 // Set the codec immediately, since SetVADStatus() depends on whether
1702 // the current codec is mono or stereo.
1703 if (!SetSendCodec(channel, send_codec_spec_.codec_inst)) {
1704 return false;
1705 }
1706
1707 // FEC should be enabled after SetSendCodec.
1708 if (send_codec_spec_.enable_codec_fec) {
1709 LOG(LS_INFO) << "Attempt to enable codec internal FEC on channel "
1710 << channel;
1711 if (engine()->voe()->codec()->SetFECStatus(channel, true) == -1) {
1712 // Enable codec internal FEC. Treat any failure as fatal internal error.
1713 LOG_RTCERR2(SetFECStatus, channel, true);
1714 return false;
1715 }
1716 }
1717
1718 if (IsCodec(send_codec_spec_.codec_inst, kOpusCodecName)) {
1719 // DTX and maxplaybackrate should be set after SetSendCodec. Because current
1720 // send codec has to be Opus.
1721
1722 // Set Opus internal DTX.
1723 LOG(LS_INFO) << "Attempt to "
1724 << (send_codec_spec_.enable_opus_dtx ? "enable" : "disable")
1725 << " Opus DTX on channel "
1726 << channel;
1727 if (engine()->voe()->codec()->SetOpusDtx(channel,
1728 send_codec_spec_.enable_opus_dtx)) {
1729 LOG_RTCERR2(SetOpusDtx, channel, send_codec_spec_.enable_opus_dtx);
1730 return false;
1731 }
1732
1733 // If opus_max_playback_rate <= 0, the default maximum playback rate
1734 // (48 kHz) will be used.
1735 if (send_codec_spec_.opus_max_playback_rate > 0) {
1736 LOG(LS_INFO) << "Attempt to set maximum playback rate to "
1737 << send_codec_spec_.opus_max_playback_rate
1738 << " Hz on channel "
1739 << channel;
1740 if (engine()->voe()->codec()->SetOpusMaxPlaybackRate(
1741 channel, send_codec_spec_.opus_max_playback_rate) == -1) {
1742 LOG_RTCERR2(SetOpusMaxPlaybackRate, channel,
1743 send_codec_spec_.opus_max_playback_rate);
1744 return false;
stefanba4c0e42016-02-04 04:12:24 -08001745 }
1746 }
1747 }
skvlade0d46372016-04-07 22:59:22 -07001748 // TODO(solenberg): SetSendBitrate() yields another call to SetSendCodec().
1749 // Check if it is possible to fuse with the previous call in this function.
1750 SetChannelParameters(channel, rtp_parameters);
solenberg72e29d22016-03-08 06:35:16 -08001751
1752 // Set the CN payloadtype and the VAD status.
1753 if (send_codec_spec_.cng_payload_type != -1) {
1754 // The CN payload type for 8000 Hz clockrate is fixed at 13.
1755 if (send_codec_spec_.cng_plfreq != 8000) {
1756 webrtc::PayloadFrequencies cn_freq;
1757 switch (send_codec_spec_.cng_plfreq) {
1758 case 16000:
1759 cn_freq = webrtc::kFreq16000Hz;
1760 break;
1761 case 32000:
1762 cn_freq = webrtc::kFreq32000Hz;
1763 break;
1764 default:
1765 RTC_NOTREACHED();
1766 return false;
1767 }
1768 if (engine()->voe()->codec()->SetSendCNPayloadType(
1769 channel, send_codec_spec_.cng_payload_type, cn_freq) == -1) {
1770 LOG_RTCERR3(SetSendCNPayloadType, channel,
1771 send_codec_spec_.cng_payload_type, cn_freq);
1772 // TODO(ajm): This failure condition will be removed from VoE.
1773 // Restore the return here when we update to a new enough webrtc.
1774 //
1775 // Not returning false because the SetSendCNPayloadType will fail if
1776 // the channel is already sending.
1777 // This can happen if the remote description is applied twice, for
1778 // example in the case of ROAP on top of JSEP, where both side will
1779 // send the offer.
1780 }
1781 }
1782
1783 // Only turn on VAD if we have a CN payload type that matches the
1784 // clockrate for the codec we are going to use.
1785 if (send_codec_spec_.cng_plfreq == send_codec_spec_.codec_inst.plfreq &&
1786 send_codec_spec_.codec_inst.channels == 1) {
1787 // TODO(minyue): If CN frequency == 48000 Hz is allowed, consider the
1788 // interaction between VAD and Opus FEC.
1789 LOG(LS_INFO) << "Enabling VAD";
1790 if (engine()->voe()->codec()->SetVADStatus(channel, true) == -1) {
1791 LOG_RTCERR2(SetVADStatus, channel, true);
1792 return false;
1793 }
1794 }
1795 }
solenberg0a617e22015-10-20 15:49:38 -07001796 return true;
wu@webrtc.org9dba5252013-08-05 20:36:57 +00001797}
1798
wu@webrtc.orgcadf9042013-08-30 21:24:16 +00001799void WebRtcVoiceMediaChannel::SetNack(int channel, bool nack_enabled) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001800 if (nack_enabled) {
wu@webrtc.orgcadf9042013-08-30 21:24:16 +00001801 LOG(LS_INFO) << "Enabling NACK for channel " << channel;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001802 engine()->voe()->rtp()->SetNACKStatus(channel, true, kNackMaxPackets);
1803 } else {
wu@webrtc.orgcadf9042013-08-30 21:24:16 +00001804 LOG(LS_INFO) << "Disabling NACK for channel " << channel;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001805 engine()->voe()->rtp()->SetNACKStatus(channel, false, 0);
1806 }
1807}
1808
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001809bool WebRtcVoiceMediaChannel::SetSendCodec(
wu@webrtc.org9dba5252013-08-05 20:36:57 +00001810 int channel, const webrtc::CodecInst& send_codec) {
1811 LOG(LS_INFO) << "Send channel " << channel << " selected voice codec "
1812 << ToString(send_codec) << ", bitrate=" << send_codec.rate;
1813
solenberg72e29d22016-03-08 06:35:16 -08001814 webrtc::CodecInst current_codec = {0};
wu@webrtc.org05e7b442014-04-01 17:44:24 +00001815 if (engine()->voe()->codec()->GetSendCodec(channel, current_codec) == 0 &&
1816 (send_codec == current_codec)) {
1817 // Codec is already configured, we can return without setting it again.
1818 return true;
1819 }
1820
wu@webrtc.org9dba5252013-08-05 20:36:57 +00001821 if (engine()->voe()->codec()->SetSendCodec(channel, send_codec) == -1) {
1822 LOG_RTCERR2(SetSendCodec, channel, ToString(send_codec));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001823 return false;
1824 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001825 return true;
1826}
1827
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001828bool WebRtcVoiceMediaChannel::SetPlayout(bool playout) {
1829 desired_playout_ = playout;
1830 return ChangePlayout(desired_playout_);
1831}
1832
1833bool WebRtcVoiceMediaChannel::PausePlayout() {
1834 return ChangePlayout(false);
1835}
1836
1837bool WebRtcVoiceMediaChannel::ResumePlayout() {
1838 return ChangePlayout(desired_playout_);
1839}
1840
1841bool WebRtcVoiceMediaChannel::ChangePlayout(bool playout) {
Peter Boströmca8b4042016-03-08 14:24:13 -08001842 TRACE_EVENT0("webrtc", "WebRtcVoiceMediaChannel::ChangePlayout");
solenberg566ef242015-11-06 15:34:49 -08001843 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001844 if (playout_ == playout) {
1845 return true;
1846 }
1847
solenberg7add0582015-11-20 09:59:34 -08001848 for (const auto& ch : recv_streams_) {
Fredrik Solenbergaf9fb212015-08-26 10:45:53 +02001849 if (!SetPlayout(ch.second->channel(), playout)) {
henrike@webrtc.org1e09a712013-07-26 19:17:59 +00001850 LOG(LS_ERROR) << "SetPlayout " << playout << " on channel "
Fredrik Solenbergaf9fb212015-08-26 10:45:53 +02001851 << ch.second->channel() << " failed";
solenberg1ac56142015-10-13 03:58:19 -07001852 return false;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001853 }
1854 }
solenberg1ac56142015-10-13 03:58:19 -07001855 playout_ = playout;
1856 return true;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001857}
1858
Taylor Brandstetter1a018dc2016-03-08 12:37:39 -08001859void WebRtcVoiceMediaChannel::SetSend(bool send) {
Peter Boströmca8b4042016-03-08 14:24:13 -08001860 TRACE_EVENT0("webrtc", "WebRtcVoiceMediaChannel::SetSend");
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001861 if (send_ == send) {
Taylor Brandstetter1a018dc2016-03-08 12:37:39 -08001862 return;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001863 }
1864
solenberg246b8172015-12-08 09:50:23 -08001865 // Apply channel specific options when channel is enabled for sending.
Taylor Brandstetter1a018dc2016-03-08 12:37:39 -08001866 if (send) {
solenberg63b34542015-09-29 06:06:31 -07001867 engine()->ApplyOptions(options_);
1868 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001869
wu@webrtc.org9dba5252013-08-05 20:36:57 +00001870 // Change the settings on each send channel.
Taylor Brandstetter1a018dc2016-03-08 12:37:39 -08001871 for (auto& kv : send_streams_) {
1872 kv.second->SetSend(send);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001873 }
wu@webrtc.org9dba5252013-08-05 20:36:57 +00001874
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001875 send_ = send;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001876}
1877
Peter Boström0c4e06b2015-10-07 12:23:21 +02001878bool WebRtcVoiceMediaChannel::SetAudioSend(uint32_t ssrc,
1879 bool enable,
solenberg1dd98f32015-09-10 01:57:14 -07001880 const AudioOptions* options,
Taylor Brandstetter1a018dc2016-03-08 12:37:39 -08001881 AudioSource* source) {
solenberg566ef242015-11-06 15:34:49 -08001882 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
solenberg1dd98f32015-09-10 01:57:14 -07001883 // TODO(solenberg): The state change should be fully rolled back if any one of
1884 // these calls fail.
Taylor Brandstetter1a018dc2016-03-08 12:37:39 -08001885 if (!SetLocalSource(ssrc, source)) {
solenberg1dd98f32015-09-10 01:57:14 -07001886 return false;
1887 }
solenbergdfc8f4f2015-10-01 02:31:10 -07001888 if (!MuteStream(ssrc, !enable)) {
solenberg1dd98f32015-09-10 01:57:14 -07001889 return false;
1890 }
solenbergdfc8f4f2015-10-01 02:31:10 -07001891 if (enable && options) {
solenberg1dd98f32015-09-10 01:57:14 -07001892 return SetOptions(*options);
1893 }
1894 return true;
1895}
1896
solenberg0a617e22015-10-20 15:49:38 -07001897int WebRtcVoiceMediaChannel::CreateVoEChannel() {
1898 int id = engine()->CreateVoEChannel();
1899 if (id == -1) {
1900 LOG_RTCERR0(CreateVoEChannel);
1901 return -1;
wu@webrtc.org9dba5252013-08-05 20:36:57 +00001902 }
solenberg0a617e22015-10-20 15:49:38 -07001903 if (engine()->voe()->network()->RegisterExternalTransport(id, *this) == -1) {
1904 LOG_RTCERR2(RegisterExternalTransport, id, this);
1905 engine()->voe()->base()->DeleteChannel(id);
1906 return -1;
1907 }
1908 return id;
wu@webrtc.org9dba5252013-08-05 20:36:57 +00001909}
1910
solenberg7add0582015-11-20 09:59:34 -08001911bool WebRtcVoiceMediaChannel::DeleteVoEChannel(int channel) {
wu@webrtc.org9dba5252013-08-05 20:36:57 +00001912 if (engine()->voe()->network()->DeRegisterExternalTransport(channel) == -1) {
1913 LOG_RTCERR1(DeRegisterExternalTransport, channel);
1914 }
wu@webrtc.org9dba5252013-08-05 20:36:57 +00001915 if (engine()->voe()->base()->DeleteChannel(channel) == -1) {
1916 LOG_RTCERR1(DeleteChannel, channel);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001917 return false;
1918 }
wu@webrtc.org9dba5252013-08-05 20:36:57 +00001919 return true;
1920}
henrike@webrtc.org1e09a712013-07-26 19:17:59 +00001921
wu@webrtc.org9dba5252013-08-05 20:36:57 +00001922bool WebRtcVoiceMediaChannel::AddSendStream(const StreamParams& sp) {
Peter Boströmca8b4042016-03-08 14:24:13 -08001923 TRACE_EVENT0("webrtc", "WebRtcVoiceMediaChannel::AddSendStream");
solenberg566ef242015-11-06 15:34:49 -08001924 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
solenberg0a617e22015-10-20 15:49:38 -07001925 LOG(LS_INFO) << "AddSendStream: " << sp.ToString();
1926
1927 uint32_t ssrc = sp.first_ssrc();
1928 RTC_DCHECK(0 != ssrc);
1929
1930 if (GetSendChannelId(ssrc) != -1) {
1931 LOG(LS_ERROR) << "Stream already exists with ssrc " << ssrc;
wu@webrtc.org9dba5252013-08-05 20:36:57 +00001932 return false;
1933 }
1934
solenberg0a617e22015-10-20 15:49:38 -07001935 // Create a new channel for sending audio data.
1936 int channel = CreateVoEChannel();
1937 if (channel == -1) {
1938 return false;
wu@webrtc.org9dba5252013-08-05 20:36:57 +00001939 }
wu@webrtc.org9dba5252013-08-05 20:36:57 +00001940
solenbergc96df772015-10-21 13:01:53 -07001941 // Save the channel to send_streams_, so that RemoveSendStream() can still
wu@webrtc.org9dba5252013-08-05 20:36:57 +00001942 // delete the channel in case failure happens below.
mallinath@webrtc.org67ee6b92014-02-03 16:57:16 +00001943 webrtc::AudioTransport* audio_transport =
1944 engine()->voe()->base()->audio_transport();
skvlade0d46372016-04-07 22:59:22 -07001945 WebRtcAudioSendStream* stream = new WebRtcAudioSendStream(
1946 channel, audio_transport, ssrc, sp.cname, send_rtp_extensions_, call_);
1947 send_streams_.insert(std::make_pair(ssrc, stream));
wu@webrtc.org9dba5252013-08-05 20:36:57 +00001948
solenberg0a617e22015-10-20 15:49:38 -07001949 // Set the current codecs to be used for the new channel. We need to do this
1950 // after adding the channel to send_channels_, because of how max bitrate is
1951 // currently being configured by SetSendCodec().
skvlade0d46372016-04-07 22:59:22 -07001952 if (HasSendCodec() && !SetSendCodecs(channel, stream->rtp_parameters())) {
solenberg0a617e22015-10-20 15:49:38 -07001953 RemoveSendStream(ssrc);
wu@webrtc.org9dba5252013-08-05 20:36:57 +00001954 return false;
1955 }
1956
1957 // At this point the channel's local SSRC has been updated. If the channel is
solenberg0a617e22015-10-20 15:49:38 -07001958 // the first send channel make sure that all the receive channels are updated
1959 // with the same SSRC in order to send receiver reports.
solenbergc96df772015-10-21 13:01:53 -07001960 if (send_streams_.size() == 1) {
solenberg0a617e22015-10-20 15:49:38 -07001961 receiver_reports_ssrc_ = ssrc;
solenberg7add0582015-11-20 09:59:34 -08001962 for (const auto& stream : recv_streams_) {
1963 int recv_channel = stream.second->channel();
solenberg0a617e22015-10-20 15:49:38 -07001964 if (engine()->voe()->rtp()->SetLocalSSRC(recv_channel, ssrc) != 0) {
solenberg7add0582015-11-20 09:59:34 -08001965 LOG_RTCERR2(SetLocalSSRC, recv_channel, ssrc);
solenberg1ac56142015-10-13 03:58:19 -07001966 return false;
wu@webrtc.org9dba5252013-08-05 20:36:57 +00001967 }
solenberg0a617e22015-10-20 15:49:38 -07001968 engine()->voe()->base()->AssociateSendChannel(recv_channel, channel);
1969 LOG(LS_INFO) << "VoiceEngine channel #" << recv_channel
1970 << " is associated with channel #" << channel << ".";
wu@webrtc.org9dba5252013-08-05 20:36:57 +00001971 }
1972 }
1973
Taylor Brandstetter1a018dc2016-03-08 12:37:39 -08001974 send_streams_[ssrc]->SetSend(send_);
1975 return true;
wu@webrtc.org9dba5252013-08-05 20:36:57 +00001976}
1977
Peter Boström0c4e06b2015-10-07 12:23:21 +02001978bool WebRtcVoiceMediaChannel::RemoveSendStream(uint32_t ssrc) {
Peter Boströmca8b4042016-03-08 14:24:13 -08001979 TRACE_EVENT0("webrtc", "WebRtcVoiceMediaChannel::RemoveSendStream");
solenberg566ef242015-11-06 15:34:49 -08001980 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
solenberg3a941542015-11-16 07:34:50 -08001981 LOG(LS_INFO) << "RemoveSendStream: " << ssrc;
1982
solenbergc96df772015-10-21 13:01:53 -07001983 auto it = send_streams_.find(ssrc);
1984 if (it == send_streams_.end()) {
wu@webrtc.org9dba5252013-08-05 20:36:57 +00001985 LOG(LS_WARNING) << "Try to remove stream with ssrc " << ssrc
1986 << " which doesn't exist.";
1987 return false;
1988 }
1989
Taylor Brandstetter1a018dc2016-03-08 12:37:39 -08001990 it->second->SetSend(false);
wu@webrtc.org9dba5252013-08-05 20:36:57 +00001991
solenberg7add0582015-11-20 09:59:34 -08001992 // Clean up and delete the send stream+channel.
Taylor Brandstetter1a018dc2016-03-08 12:37:39 -08001993 int channel = it->second->channel();
solenberg0a617e22015-10-20 15:49:38 -07001994 LOG(LS_INFO) << "Removing audio send stream " << ssrc
1995 << " with VoiceEngine channel #" << channel << ".";
solenberg7add0582015-11-20 09:59:34 -08001996 delete it->second;
1997 send_streams_.erase(it);
1998 if (!DeleteVoEChannel(channel)) {
solenberg0a617e22015-10-20 15:49:38 -07001999 return false;
wu@webrtc.org9dba5252013-08-05 20:36:57 +00002000 }
solenbergc96df772015-10-21 13:01:53 -07002001 if (send_streams_.empty()) {
Taylor Brandstetter1a018dc2016-03-08 12:37:39 -08002002 SetSend(false);
solenberg0a617e22015-10-20 15:49:38 -07002003 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002004 return true;
2005}
2006
2007bool WebRtcVoiceMediaChannel::AddRecvStream(const StreamParams& sp) {
Peter Boströmca8b4042016-03-08 14:24:13 -08002008 TRACE_EVENT0("webrtc", "WebRtcVoiceMediaChannel::AddRecvStream");
solenberg566ef242015-11-06 15:34:49 -08002009 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
solenbergd97ec302015-10-07 01:40:33 -07002010 LOG(LS_INFO) << "AddRecvStream: " << sp.ToString();
2011
solenberg0b675462015-10-09 01:37:09 -07002012 if (!ValidateStreamParams(sp)) {
wu@webrtc.org78187522013-10-07 23:32:02 +00002013 return false;
2014 }
2015
solenberg7add0582015-11-20 09:59:34 -08002016 const uint32_t ssrc = sp.first_ssrc();
solenberg0b675462015-10-09 01:37:09 -07002017 if (ssrc == 0) {
2018 LOG(LS_WARNING) << "AddRecvStream with ssrc==0 is not supported.";
2019 return false;
2020 }
2021
solenberg1ac56142015-10-13 03:58:19 -07002022 // Remove the default receive stream if one had been created with this ssrc;
2023 // we'll recreate it then.
2024 if (IsDefaultRecvStream(ssrc)) {
2025 RemoveRecvStream(ssrc);
2026 }
solenberg0b675462015-10-09 01:37:09 -07002027
solenberg7add0582015-11-20 09:59:34 -08002028 if (GetReceiveChannelId(ssrc) != -1) {
henrike@webrtc.org1e09a712013-07-26 19:17:59 +00002029 LOG(LS_ERROR) << "Stream already exists with ssrc " << ssrc;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002030 return false;
2031 }
Fredrik Solenberg4b60c732015-05-07 14:07:48 +02002032
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002033 // Create a new channel for receiving audio data.
solenberg7add0582015-11-20 09:59:34 -08002034 const int channel = CreateVoEChannel();
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002035 if (channel == -1) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002036 return false;
2037 }
Minyue2013aec2015-05-13 14:14:42 +02002038
solenberg1ac56142015-10-13 03:58:19 -07002039 // Turn off all supported codecs.
solenberg26c8c912015-11-27 04:00:25 -08002040 // TODO(solenberg): Remove once "no codecs" is the default state of a stream.
2041 for (webrtc::CodecInst voe_codec : webrtc::acm2::RentACodec::Database()) {
2042 voe_codec.pltype = -1;
2043 if (engine()->voe()->codec()->SetRecPayloadType(channel, voe_codec) == -1) {
2044 LOG_RTCERR2(SetRecPayloadType, channel, ToString(voe_codec));
2045 DeleteVoEChannel(channel);
2046 return false;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002047 }
2048 }
2049
solenberg1ac56142015-10-13 03:58:19 -07002050 // Only enable those configured for this channel.
2051 for (const auto& codec : recv_codecs_) {
solenberg72e29d22016-03-08 06:35:16 -08002052 webrtc::CodecInst voe_codec = {0};
solenberg26c8c912015-11-27 04:00:25 -08002053 if (WebRtcVoiceEngine::ToCodecInst(codec, &voe_codec)) {
solenberg1ac56142015-10-13 03:58:19 -07002054 voe_codec.pltype = codec.id;
2055 if (engine()->voe()->codec()->SetRecPayloadType(
2056 channel, voe_codec) == -1) {
2057 LOG_RTCERR2(SetRecPayloadType, channel, ToString(voe_codec));
solenberg7add0582015-11-20 09:59:34 -08002058 DeleteVoEChannel(channel);
solenberg1ac56142015-10-13 03:58:19 -07002059 return false;
2060 }
henrike@webrtc.org1e09a712013-07-26 19:17:59 +00002061 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002062 }
solenberg8fb30c32015-10-13 03:06:58 -07002063
solenberg7add0582015-11-20 09:59:34 -08002064 const int send_channel = GetSendChannelId(receiver_reports_ssrc_);
2065 if (send_channel != -1) {
2066 // Associate receive channel with first send channel (so the receive channel
2067 // can obtain RTT from the send channel)
2068 engine()->voe()->base()->AssociateSendChannel(channel, send_channel);
2069 LOG(LS_INFO) << "VoiceEngine channel #" << channel
2070 << " is associated with channel #" << send_channel << ".";
buildbot@webrtc.org150835e2014-05-06 15:54:38 +00002071 }
2072
stefanba4c0e42016-02-04 04:12:24 -08002073 recv_streams_.insert(std::make_pair(
2074 ssrc, new WebRtcAudioReceiveStream(channel, ssrc, receiver_reports_ssrc_,
solenberg72e29d22016-03-08 06:35:16 -08002075 recv_transport_cc_enabled_,
2076 sp.sync_label, recv_rtp_extensions_,
2077 call_)));
solenberg7add0582015-11-20 09:59:34 -08002078
solenberg72e29d22016-03-08 06:35:16 -08002079 SetNack(channel, send_codec_spec_.nack_enabled);
solenberg1ac56142015-10-13 03:58:19 -07002080 SetPlayout(channel, playout_);
solenberg7add0582015-11-20 09:59:34 -08002081
solenberg1ac56142015-10-13 03:58:19 -07002082 return true;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002083}
2084
Peter Boström0c4e06b2015-10-07 12:23:21 +02002085bool WebRtcVoiceMediaChannel::RemoveRecvStream(uint32_t ssrc) {
Peter Boströmca8b4042016-03-08 14:24:13 -08002086 TRACE_EVENT0("webrtc", "WebRtcVoiceMediaChannel::RemoveRecvStream");
solenberg566ef242015-11-06 15:34:49 -08002087 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
solenbergd97ec302015-10-07 01:40:33 -07002088 LOG(LS_INFO) << "RemoveRecvStream: " << ssrc;
2089
solenberg7add0582015-11-20 09:59:34 -08002090 const auto it = recv_streams_.find(ssrc);
2091 if (it == recv_streams_.end()) {
wu@webrtc.org9dba5252013-08-05 20:36:57 +00002092 LOG(LS_WARNING) << "Try to remove stream with ssrc " << ssrc
2093 << " which doesn't exist.";
henrike@webrtc.org1e09a712013-07-26 19:17:59 +00002094 return false;
wu@webrtc.org9dba5252013-08-05 20:36:57 +00002095 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002096
solenberg1ac56142015-10-13 03:58:19 -07002097 // Deregister default channel, if that's the one being destroyed.
2098 if (IsDefaultRecvStream(ssrc)) {
2099 default_recv_ssrc_ = -1;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002100 }
henrike@webrtc.org1e09a712013-07-26 19:17:59 +00002101
solenberg7add0582015-11-20 09:59:34 -08002102 const int channel = it->second->channel();
2103
2104 // Clean up and delete the receive stream+channel.
2105 LOG(LS_INFO) << "Removing audio receive stream " << ssrc
mallinath@webrtc.org67ee6b92014-02-03 16:57:16 +00002106 << " with VoiceEngine channel #" << channel << ".";
Tommif888bb52015-12-12 01:37:01 +01002107 it->second->SetRawAudioSink(nullptr);
solenberg7add0582015-11-20 09:59:34 -08002108 delete it->second;
2109 recv_streams_.erase(it);
2110 return DeleteVoEChannel(channel);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002111}
2112
Taylor Brandstetter1a018dc2016-03-08 12:37:39 -08002113bool WebRtcVoiceMediaChannel::SetLocalSource(uint32_t ssrc,
2114 AudioSource* source) {
solenbergc96df772015-10-21 13:01:53 -07002115 auto it = send_streams_.find(ssrc);
2116 if (it == send_streams_.end()) {
Taylor Brandstetter1a018dc2016-03-08 12:37:39 -08002117 if (source) {
2118 // Return an error if trying to set a valid source with an invalid ssrc.
2119 LOG(LS_ERROR) << "SetLocalSource failed with ssrc " << ssrc;
wu@webrtc.org9dba5252013-08-05 20:36:57 +00002120 return false;
2121 }
2122
2123 // The channel likely has gone away, do nothing.
henrike@webrtc.org1e09a712013-07-26 19:17:59 +00002124 return true;
henrike@webrtc.org1e09a712013-07-26 19:17:59 +00002125 }
2126
Taylor Brandstetter1a018dc2016-03-08 12:37:39 -08002127 if (source) {
2128 it->second->SetSource(source);
solenberg1ac56142015-10-13 03:58:19 -07002129 } else {
Taylor Brandstetter1a018dc2016-03-08 12:37:39 -08002130 it->second->ClearSource();
solenberg1ac56142015-10-13 03:58:19 -07002131 }
henrike@webrtc.org1e09a712013-07-26 19:17:59 +00002132
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002133 return true;
2134}
2135
2136bool WebRtcVoiceMediaChannel::GetActiveStreams(
2137 AudioInfo::StreamList* actives) {
solenberg566ef242015-11-06 15:34:49 -08002138 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002139 actives->clear();
solenberg7add0582015-11-20 09:59:34 -08002140 for (const auto& ch : recv_streams_) {
Fredrik Solenbergaf9fb212015-08-26 10:45:53 +02002141 int level = GetOutputLevel(ch.second->channel());
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002142 if (level > 0) {
Fredrik Solenbergaf9fb212015-08-26 10:45:53 +02002143 actives->push_back(std::make_pair(ch.first, level));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002144 }
2145 }
2146 return true;
2147}
2148
2149int WebRtcVoiceMediaChannel::GetOutputLevel() {
solenberg566ef242015-11-06 15:34:49 -08002150 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
solenberg1ac56142015-10-13 03:58:19 -07002151 int highest = 0;
solenberg7add0582015-11-20 09:59:34 -08002152 for (const auto& ch : recv_streams_) {
solenberg8fb30c32015-10-13 03:06:58 -07002153 highest = std::max(GetOutputLevel(ch.second->channel()), highest);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002154 }
2155 return highest;
2156}
2157
2158int WebRtcVoiceMediaChannel::GetTimeSinceLastTyping() {
2159 int ret;
2160 if (engine()->voe()->processing()->TimeSinceLastTyping(ret) == -1) {
2161 // In case of error, log the info and continue
2162 LOG_RTCERR0(TimeSinceLastTyping);
2163 ret = -1;
2164 } else {
2165 ret *= 1000; // We return ms, webrtc returns seconds.
2166 }
2167 return ret;
2168}
2169
2170void WebRtcVoiceMediaChannel::SetTypingDetectionParameters(int time_window,
2171 int cost_per_typing, int reporting_threshold, int penalty_decay,
2172 int type_event_delay) {
2173 if (engine()->voe()->processing()->SetTypingDetectionParameters(
2174 time_window, cost_per_typing,
2175 reporting_threshold, penalty_decay, type_event_delay) == -1) {
2176 // In case of error, log the info and continue
2177 LOG_RTCERR5(SetTypingDetectionParameters, time_window,
2178 cost_per_typing, reporting_threshold, penalty_decay,
2179 type_event_delay);
2180 }
2181}
2182
solenberg4bac9c52015-10-09 02:32:53 -07002183bool WebRtcVoiceMediaChannel::SetOutputVolume(uint32_t ssrc, double volume) {
solenberg566ef242015-11-06 15:34:49 -08002184 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
solenberg1ac56142015-10-13 03:58:19 -07002185 if (ssrc == 0) {
2186 default_recv_volume_ = volume;
2187 if (default_recv_ssrc_ == -1) {
2188 return true;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002189 }
solenberg1ac56142015-10-13 03:58:19 -07002190 ssrc = static_cast<uint32_t>(default_recv_ssrc_);
2191 }
2192 int ch_id = GetReceiveChannelId(ssrc);
2193 if (ch_id < 0) {
2194 LOG(LS_WARNING) << "Cannot find channel for ssrc:" << ssrc;
2195 return false;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002196 }
2197
solenberg1ac56142015-10-13 03:58:19 -07002198 if (-1 == engine()->voe()->volume()->SetChannelOutputVolumeScaling(ch_id,
2199 volume)) {
2200 LOG_RTCERR2(SetChannelOutputVolumeScaling, ch_id, volume);
2201 return false;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002202 }
solenberg1ac56142015-10-13 03:58:19 -07002203 LOG(LS_INFO) << "SetOutputVolume to " << volume
2204 << " for channel " << ch_id << " and ssrc " << ssrc;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002205 return true;
2206}
2207
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002208bool WebRtcVoiceMediaChannel::CanInsertDtmf() {
Fredrik Solenbergb5727682015-12-04 15:22:19 +01002209 return dtmf_payload_type_ ? true : false;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002210}
2211
solenberg1d63dd02015-12-02 12:35:09 -08002212bool WebRtcVoiceMediaChannel::InsertDtmf(uint32_t ssrc, int event,
2213 int duration) {
solenberg566ef242015-11-06 15:34:49 -08002214 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
Fredrik Solenbergb5727682015-12-04 15:22:19 +01002215 LOG(LS_INFO) << "WebRtcVoiceMediaChannel::InsertDtmf";
2216 if (!dtmf_payload_type_) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002217 return false;
2218 }
2219
Fredrik Solenbergb5727682015-12-04 15:22:19 +01002220 // Figure out which WebRtcAudioSendStream to send the event on.
2221 auto it = ssrc != 0 ? send_streams_.find(ssrc) : send_streams_.begin();
2222 if (it == send_streams_.end()) {
2223 LOG(LS_WARNING) << "The specified ssrc " << ssrc << " is not in use.";
solenberg1d63dd02015-12-02 12:35:09 -08002224 return false;
2225 }
Fredrik Solenbergb5727682015-12-04 15:22:19 +01002226 if (event < kMinTelephoneEventCode ||
2227 event > kMaxTelephoneEventCode) {
2228 LOG(LS_WARNING) << "DTMF event code " << event << " out of range.";
solenberg1d63dd02015-12-02 12:35:09 -08002229 return false;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002230 }
Fredrik Solenbergb5727682015-12-04 15:22:19 +01002231 if (duration < kMinTelephoneEventDuration ||
2232 duration > kMaxTelephoneEventDuration) {
2233 LOG(LS_WARNING) << "DTMF event duration " << duration << " out of range.";
2234 return false;
2235 }
2236 return it->second->SendTelephoneEvent(*dtmf_payload_type_, event, duration);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002237}
2238
wu@webrtc.orga9890802013-12-13 00:21:03 +00002239void WebRtcVoiceMediaChannel::OnPacketReceived(
jbaucheec21bd2016-03-20 06:15:43 -07002240 rtc::CopyOnWriteBuffer* packet, const rtc::PacketTime& packet_time) {
solenberg566ef242015-11-06 15:34:49 -08002241 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
Fredrik Solenberg4b60c732015-05-07 14:07:48 +02002242
solenberg1ac56142015-10-13 03:58:19 -07002243 uint32_t ssrc = 0;
jbaucheec21bd2016-03-20 06:15:43 -07002244 if (!GetRtpSsrc(packet->cdata(), packet->size(), &ssrc)) {
solenberg1ac56142015-10-13 03:58:19 -07002245 return;
2246 }
2247
solenberg7e63ef02015-11-20 00:19:43 -08002248 // If we don't have a default channel, and the SSRC is unknown, create a
2249 // default channel.
2250 if (default_recv_ssrc_ == -1 && GetReceiveChannelId(ssrc) == -1) {
solenberg1ac56142015-10-13 03:58:19 -07002251 StreamParams sp;
2252 sp.ssrcs.push_back(ssrc);
2253 LOG(LS_INFO) << "Creating default receive stream for SSRC=" << ssrc << ".";
2254 if (!AddRecvStream(sp)) {
2255 LOG(LS_WARNING) << "Could not create default receive stream.";
2256 return;
2257 }
2258 default_recv_ssrc_ = ssrc;
2259 SetOutputVolume(default_recv_ssrc_, default_recv_volume_);
deadbeef884f5852016-01-15 09:20:04 -08002260 if (default_sink_) {
kwiberg686a8ef2016-02-26 03:00:35 -08002261 std::unique_ptr<webrtc::AudioSinkInterface> proxy_sink(
deadbeef884f5852016-01-15 09:20:04 -08002262 new ProxySink(default_sink_.get()));
2263 SetRawAudioSink(default_recv_ssrc_, std::move(proxy_sink));
2264 }
solenberg1ac56142015-10-13 03:58:19 -07002265 }
2266
2267 // Forward packet to Call. If the SSRC is unknown we'll return after this.
Fredrik Solenberg709ed672015-09-15 12:26:33 +02002268 const webrtc::PacketTime webrtc_packet_time(packet_time.timestamp,
2269 packet_time.not_before);
solenberg1ac56142015-10-13 03:58:19 -07002270 webrtc::PacketReceiver::DeliveryStatus delivery_result =
2271 call_->Receiver()->DeliverPacket(webrtc::MediaType::AUDIO,
jbaucheec21bd2016-03-20 06:15:43 -07002272 packet->cdata(), packet->size(), webrtc_packet_time);
solenberg1ac56142015-10-13 03:58:19 -07002273 if (webrtc::PacketReceiver::DELIVERY_OK != delivery_result) {
solenberg7e63ef02015-11-20 00:19:43 -08002274 // If the SSRC is unknown here, route it to the default channel, if we have
2275 // one. See: https://bugs.chromium.org/p/webrtc/issues/detail?id=5208
2276 if (default_recv_ssrc_ == -1) {
2277 return;
2278 } else {
2279 ssrc = default_recv_ssrc_;
2280 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002281 }
2282
solenberg1ac56142015-10-13 03:58:19 -07002283 // Find the channel to send this packet to. It must exist since webrtc::Call
2284 // was able to demux the packet.
2285 int channel = GetReceiveChannelId(ssrc);
2286 RTC_DCHECK(channel != -1);
2287
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002288 // Pass it off to the decoder.
henrike@webrtc.org28654cb2013-07-22 21:07:49 +00002289 engine()->voe()->network()->ReceivedRTPPacket(
jbaucheec21bd2016-03-20 06:15:43 -07002290 channel, packet->cdata(), packet->size(), webrtc_packet_time);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002291}
2292
wu@webrtc.orga9890802013-12-13 00:21:03 +00002293void WebRtcVoiceMediaChannel::OnRtcpReceived(
jbaucheec21bd2016-03-20 06:15:43 -07002294 rtc::CopyOnWriteBuffer* packet, const rtc::PacketTime& packet_time) {
solenberg566ef242015-11-06 15:34:49 -08002295 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
Fredrik Solenberg4b60c732015-05-07 14:07:48 +02002296
Fredrik Solenberg709ed672015-09-15 12:26:33 +02002297 // Forward packet to Call as well.
2298 const webrtc::PacketTime webrtc_packet_time(packet_time.timestamp,
2299 packet_time.not_before);
2300 call_->Receiver()->DeliverPacket(webrtc::MediaType::AUDIO,
jbaucheec21bd2016-03-20 06:15:43 -07002301 packet->cdata(), packet->size(), webrtc_packet_time);
Fredrik Solenberg4b60c732015-05-07 14:07:48 +02002302
wu@webrtc.org9dba5252013-08-05 20:36:57 +00002303 // Sending channels need all RTCP packets with feedback information.
2304 // Even sender reports can contain attached report blocks.
2305 // Receiving channels need sender reports in order to create
2306 // correct receiver reports.
2307 int type = 0;
jbaucheec21bd2016-03-20 06:15:43 -07002308 if (!GetRtcpType(packet->cdata(), packet->size(), &type)) {
wu@webrtc.org9dba5252013-08-05 20:36:57 +00002309 LOG(LS_WARNING) << "Failed to parse type from received RTCP packet";
2310 return;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002311 }
2312
solenberg0b675462015-10-09 01:37:09 -07002313 // If it is a sender report, find the receive channel that is listening.
wu@webrtc.org9dba5252013-08-05 20:36:57 +00002314 if (type == kRtcpTypeSR) {
solenberg0b675462015-10-09 01:37:09 -07002315 uint32_t ssrc = 0;
jbaucheec21bd2016-03-20 06:15:43 -07002316 if (!GetRtcpSsrc(packet->cdata(), packet->size(), &ssrc)) {
solenberg0b675462015-10-09 01:37:09 -07002317 return;
2318 }
2319 int recv_channel_id = GetReceiveChannelId(ssrc);
2320 if (recv_channel_id != -1) {
wu@webrtc.org9dba5252013-08-05 20:36:57 +00002321 engine()->voe()->network()->ReceivedRTCPPacket(
jbaucheec21bd2016-03-20 06:15:43 -07002322 recv_channel_id, packet->cdata(), packet->size());
wu@webrtc.org9dba5252013-08-05 20:36:57 +00002323 }
2324 }
2325
2326 // SR may continue RR and any RR entry may correspond to any one of the send
2327 // channels. So all RTCP packets must be forwarded all send channels. VoE
2328 // will filter out RR internally.
solenbergc96df772015-10-21 13:01:53 -07002329 for (const auto& ch : send_streams_) {
wu@webrtc.org9dba5252013-08-05 20:36:57 +00002330 engine()->voe()->network()->ReceivedRTCPPacket(
jbaucheec21bd2016-03-20 06:15:43 -07002331 ch.second->channel(), packet->cdata(), packet->size());
wu@webrtc.org9dba5252013-08-05 20:36:57 +00002332 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002333}
2334
Honghai Zhangcc411c02016-03-29 17:27:21 -07002335void WebRtcVoiceMediaChannel::OnNetworkRouteChanged(
2336 const std::string& transport_name,
2337 const NetworkRoute& network_route) {
2338 // TODO(honghaiz): uncomment this once the function in call is implemented.
2339 // call_->OnNetworkRouteChanged(transport_name, network_route);
2340}
2341
Peter Boström0c4e06b2015-10-07 12:23:21 +02002342bool WebRtcVoiceMediaChannel::MuteStream(uint32_t ssrc, bool muted) {
solenberg566ef242015-11-06 15:34:49 -08002343 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
solenberg0a617e22015-10-20 15:49:38 -07002344 int channel = GetSendChannelId(ssrc);
wu@webrtc.org9dba5252013-08-05 20:36:57 +00002345 if (channel == -1) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002346 LOG(LS_WARNING) << "The specified ssrc " << ssrc << " is not in use.";
2347 return false;
2348 }
wu@webrtc.org9dba5252013-08-05 20:36:57 +00002349 if (engine()->voe()->volume()->SetInputMute(channel, muted) == -1) {
2350 LOG_RTCERR2(SetInputMute, channel, muted);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002351 return false;
2352 }
buildbot@webrtc.org6b21b712014-07-31 15:08:53 +00002353 // We set the AGC to mute state only when all the channels are muted.
2354 // This implementation is not ideal, instead we should signal the AGC when
2355 // the mic channel is muted/unmuted. We can't do it today because there
2356 // is no good way to know which stream is mapping to the mic channel.
2357 bool all_muted = muted;
solenbergc96df772015-10-21 13:01:53 -07002358 for (const auto& ch : send_streams_) {
Fredrik Solenbergaf9fb212015-08-26 10:45:53 +02002359 if (!all_muted) {
2360 break;
2361 }
2362 if (engine()->voe()->volume()->GetInputMute(ch.second->channel(),
buildbot@webrtc.org6b21b712014-07-31 15:08:53 +00002363 all_muted)) {
Fredrik Solenbergaf9fb212015-08-26 10:45:53 +02002364 LOG_RTCERR1(GetInputMute, ch.second->channel());
buildbot@webrtc.org6b21b712014-07-31 15:08:53 +00002365 return false;
2366 }
2367 }
2368
2369 webrtc::AudioProcessing* ap = engine()->voe()->base()->audio_processing();
solenberg0a617e22015-10-20 15:49:38 -07002370 if (ap) {
buildbot@webrtc.org6b21b712014-07-31 15:08:53 +00002371 ap->set_output_will_be_muted(all_muted);
solenberg0a617e22015-10-20 15:49:38 -07002372 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002373 return true;
2374}
2375
skvlade0d46372016-04-07 22:59:22 -07002376bool WebRtcVoiceMediaChannel::SetSendBitrate(int bps) {
2377 LOG(LS_INFO) << "WebRtcVoiceMediaChannel::SetSendBitrate.";
2378 send_bitrate_bps_ = bps;
2379
2380 for (const auto& kv : send_streams_) {
2381 if (!SetChannelParameters(kv.second->channel(),
2382 kv.second->rtp_parameters())) {
2383 return false;
2384 }
2385 }
2386 return true;
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00002387}
2388
skvlade0d46372016-04-07 22:59:22 -07002389bool WebRtcVoiceMediaChannel::SetChannelParameters(
2390 int channel,
2391 const webrtc::RtpParameters& parameters) {
2392 RTC_CHECK_EQ(1UL, parameters.encodings.size());
2393 return SetSendBitrate(
2394 channel,
2395 MinPositive(send_bitrate_bps_, parameters.encodings[0].max_bitrate_bps));
2396}
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00002397
skvlade0d46372016-04-07 22:59:22 -07002398bool WebRtcVoiceMediaChannel::SetSendBitrate(int channel, int bps) {
2399 // Bitrate is auto by default.
2400 // TODO(bemasc): Fix this so that if SetMaxSendBandwidth(50) is followed by
2401 // SetMaxSendBandwith(0), the second call removes the previous limit.
2402 if (bps <= 0)
2403 return true;
wu@webrtc.org1d1ffc92013-10-16 18:12:02 +00002404
solenberg72e29d22016-03-08 06:35:16 -08002405 if (!HasSendCodec()) {
wu@webrtc.org1d1ffc92013-10-16 18:12:02 +00002406 LOG(LS_INFO) << "The send codec has not been set up yet. "
minyue@webrtc.org26236952014-10-29 02:27:08 +00002407 << "The send bitrate setting will be applied later.";
wu@webrtc.org1d1ffc92013-10-16 18:12:02 +00002408 return true;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002409 }
2410
solenberg72e29d22016-03-08 06:35:16 -08002411 webrtc::CodecInst codec = send_codec_spec_.codec_inst;
solenberg26c8c912015-11-27 04:00:25 -08002412 bool is_multi_rate = WebRtcVoiceCodecs::IsCodecMultiRate(codec);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002413
2414 if (is_multi_rate) {
2415 // If codec is multi-rate then just set the bitrate.
2416 codec.rate = bps;
skvlade0d46372016-04-07 22:59:22 -07002417 if (!SetSendCodec(channel, codec)) {
2418 LOG(LS_INFO) << "Failed to set codec " << codec.plname << " to bitrate "
2419 << bps << " bps.";
2420 return false;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002421 }
2422 return true;
2423 } else {
2424 // If codec is not multi-rate and |bps| is less than the fixed bitrate
2425 // then fail. If codec is not multi-rate and |bps| exceeds or equal the
2426 // fixed bitrate then ignore.
2427 if (bps < codec.rate) {
2428 LOG(LS_INFO) << "Failed to set codec " << codec.plname
2429 << " to bitrate " << bps << " bps"
2430 << ", requires at least " << codec.rate << " bps.";
2431 return false;
2432 }
2433 return true;
2434 }
2435}
2436
skvlad7a43d252016-03-22 15:32:27 -07002437void WebRtcVoiceMediaChannel::OnReadyToSend(bool ready) {
2438 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
2439 LOG(LS_VERBOSE) << "OnReadyToSend: " << (ready ? "Ready." : "Not ready.");
2440 call_->SignalChannelNetworkState(
2441 webrtc::MediaType::AUDIO,
2442 ready ? webrtc::kNetworkUp : webrtc::kNetworkDown);
2443}
2444
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002445bool WebRtcVoiceMediaChannel::GetStats(VoiceMediaInfo* info) {
Peter Boströmca8b4042016-03-08 14:24:13 -08002446 TRACE_EVENT0("webrtc", "WebRtcVoiceMediaChannel::GetStats");
solenberg566ef242015-11-06 15:34:49 -08002447 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
solenberg85a04962015-10-27 03:35:21 -07002448 RTC_DCHECK(info);
solenbergd97ec302015-10-07 01:40:33 -07002449
solenberg85a04962015-10-27 03:35:21 -07002450 // Get SSRC and stats for each sender.
2451 RTC_DCHECK(info->senders.size() == 0);
2452 for (const auto& stream : send_streams_) {
2453 webrtc::AudioSendStream::Stats stats = stream.second->GetStats();
wu@webrtc.org9dba5252013-08-05 20:36:57 +00002454 VoiceSenderInfo sinfo;
solenberg85a04962015-10-27 03:35:21 -07002455 sinfo.add_ssrc(stats.local_ssrc);
2456 sinfo.bytes_sent = stats.bytes_sent;
2457 sinfo.packets_sent = stats.packets_sent;
2458 sinfo.packets_lost = stats.packets_lost;
2459 sinfo.fraction_lost = stats.fraction_lost;
2460 sinfo.codec_name = stats.codec_name;
2461 sinfo.ext_seqnum = stats.ext_seqnum;
2462 sinfo.jitter_ms = stats.jitter_ms;
2463 sinfo.rtt_ms = stats.rtt_ms;
2464 sinfo.audio_level = stats.audio_level;
2465 sinfo.aec_quality_min = stats.aec_quality_min;
2466 sinfo.echo_delay_median_ms = stats.echo_delay_median_ms;
2467 sinfo.echo_delay_std_ms = stats.echo_delay_std_ms;
2468 sinfo.echo_return_loss = stats.echo_return_loss;
2469 sinfo.echo_return_loss_enhancement = stats.echo_return_loss_enhancement;
Taylor Brandstetter1a018dc2016-03-08 12:37:39 -08002470 sinfo.typing_noise_detected = (send_ ? stats.typing_noise_detected : false);
wu@webrtc.org9dba5252013-08-05 20:36:57 +00002471 info->senders.push_back(sinfo);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002472 }
2473
solenberg85a04962015-10-27 03:35:21 -07002474 // Get SSRC and stats for each receiver.
2475 RTC_DCHECK(info->receivers.size() == 0);
solenberg7add0582015-11-20 09:59:34 -08002476 for (const auto& stream : recv_streams_) {
Fredrik Solenberg4f4ec0a2015-10-22 10:49:27 +02002477 webrtc::AudioReceiveStream::Stats stats = stream.second->GetStats();
2478 VoiceReceiverInfo rinfo;
2479 rinfo.add_ssrc(stats.remote_ssrc);
2480 rinfo.bytes_rcvd = stats.bytes_rcvd;
2481 rinfo.packets_rcvd = stats.packets_rcvd;
2482 rinfo.packets_lost = stats.packets_lost;
2483 rinfo.fraction_lost = stats.fraction_lost;
2484 rinfo.codec_name = stats.codec_name;
2485 rinfo.ext_seqnum = stats.ext_seqnum;
2486 rinfo.jitter_ms = stats.jitter_ms;
2487 rinfo.jitter_buffer_ms = stats.jitter_buffer_ms;
2488 rinfo.jitter_buffer_preferred_ms = stats.jitter_buffer_preferred_ms;
2489 rinfo.delay_estimate_ms = stats.delay_estimate_ms;
2490 rinfo.audio_level = stats.audio_level;
2491 rinfo.expand_rate = stats.expand_rate;
2492 rinfo.speech_expand_rate = stats.speech_expand_rate;
2493 rinfo.secondary_decoded_rate = stats.secondary_decoded_rate;
2494 rinfo.accelerate_rate = stats.accelerate_rate;
2495 rinfo.preemptive_expand_rate = stats.preemptive_expand_rate;
2496 rinfo.decoding_calls_to_silence_generator =
2497 stats.decoding_calls_to_silence_generator;
2498 rinfo.decoding_calls_to_neteq = stats.decoding_calls_to_neteq;
2499 rinfo.decoding_normal = stats.decoding_normal;
2500 rinfo.decoding_plc = stats.decoding_plc;
2501 rinfo.decoding_cng = stats.decoding_cng;
2502 rinfo.decoding_plc_cng = stats.decoding_plc_cng;
2503 rinfo.capture_start_ntp_time_ms = stats.capture_start_ntp_time_ms;
2504 info->receivers.push_back(rinfo);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002505 }
2506
2507 return true;
2508}
2509
Tommif888bb52015-12-12 01:37:01 +01002510void WebRtcVoiceMediaChannel::SetRawAudioSink(
2511 uint32_t ssrc,
kwiberg686a8ef2016-02-26 03:00:35 -08002512 std::unique_ptr<webrtc::AudioSinkInterface> sink) {
Tommif888bb52015-12-12 01:37:01 +01002513 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
deadbeef884f5852016-01-15 09:20:04 -08002514 LOG(LS_VERBOSE) << "WebRtcVoiceMediaChannel::SetRawAudioSink: ssrc:" << ssrc
2515 << " " << (sink ? "(ptr)" : "NULL");
2516 if (ssrc == 0) {
2517 if (default_recv_ssrc_ != -1) {
kwiberg686a8ef2016-02-26 03:00:35 -08002518 std::unique_ptr<webrtc::AudioSinkInterface> proxy_sink(
deadbeef884f5852016-01-15 09:20:04 -08002519 sink ? new ProxySink(sink.get()) : nullptr);
2520 SetRawAudioSink(default_recv_ssrc_, std::move(proxy_sink));
2521 }
2522 default_sink_ = std::move(sink);
2523 return;
2524 }
Tommif888bb52015-12-12 01:37:01 +01002525 const auto it = recv_streams_.find(ssrc);
2526 if (it == recv_streams_.end()) {
2527 LOG(LS_WARNING) << "SetRawAudioSink: no recv stream" << ssrc;
2528 return;
2529 }
deadbeef2d110be2016-01-13 12:00:26 -08002530 it->second->SetRawAudioSink(std::move(sink));
Tommif888bb52015-12-12 01:37:01 +01002531}
2532
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002533int WebRtcVoiceMediaChannel::GetOutputLevel(int channel) {
solenbergd97ec302015-10-07 01:40:33 -07002534 unsigned int ulevel = 0;
2535 int ret = engine()->voe()->volume()->GetSpeechOutputLevel(channel, ulevel);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002536 return (ret == 0) ? static_cast<int>(ulevel) : -1;
2537}
2538
Peter Boström0c4e06b2015-10-07 12:23:21 +02002539int WebRtcVoiceMediaChannel::GetReceiveChannelId(uint32_t ssrc) const {
solenberg566ef242015-11-06 15:34:49 -08002540 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
solenberg7add0582015-11-20 09:59:34 -08002541 const auto it = recv_streams_.find(ssrc);
2542 if (it != recv_streams_.end()) {
mallinath@webrtc.org67ee6b92014-02-03 16:57:16 +00002543 return it->second->channel();
solenberg8fb30c32015-10-13 03:06:58 -07002544 }
solenberg1ac56142015-10-13 03:58:19 -07002545 return -1;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002546}
2547
Peter Boström0c4e06b2015-10-07 12:23:21 +02002548int WebRtcVoiceMediaChannel::GetSendChannelId(uint32_t ssrc) const {
solenberg566ef242015-11-06 15:34:49 -08002549 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
solenbergc96df772015-10-21 13:01:53 -07002550 const auto it = send_streams_.find(ssrc);
2551 if (it != send_streams_.end()) {
mallinath@webrtc.org67ee6b92014-02-03 16:57:16 +00002552 return it->second->channel();
solenberg8fb30c32015-10-13 03:06:58 -07002553 }
wu@webrtc.org9dba5252013-08-05 20:36:57 +00002554 return -1;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002555}
2556
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002557bool WebRtcVoiceMediaChannel::SetPlayout(int channel, bool playout) {
2558 if (playout) {
2559 LOG(LS_INFO) << "Starting playout for channel #" << channel;
2560 if (engine()->voe()->base()->StartPlayout(channel) == -1) {
2561 LOG_RTCERR1(StartPlayout, channel);
2562 return false;
2563 }
2564 } else {
2565 LOG(LS_INFO) << "Stopping playout for channel #" << channel;
2566 engine()->voe()->base()->StopPlayout(channel);
2567 }
2568 return true;
2569}
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002570} // namespace cricket
2571
2572#endif // HAVE_WEBRTC_VOICE