blob: 21f12105f1db6745f8c70c6167e8210e023e305e [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_);
1124 }
1125
solenberg8842c3e2016-03-11 03:06:41 -08001126 bool SendTelephoneEvent(int payload_type, int event, int duration_ms) {
Fredrik Solenbergb5727682015-12-04 15:22:19 +01001127 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
1128 RTC_DCHECK(stream_);
1129 return stream_->SendTelephoneEvent(payload_type, event, duration_ms);
1130 }
1131
Taylor Brandstetter1a018dc2016-03-08 12:37:39 -08001132 void SetSend(bool send) {
1133 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
1134 send_ = send;
1135 UpdateSendState();
1136 }
1137
solenberg3a941542015-11-16 07:34:50 -08001138 webrtc::AudioSendStream::Stats GetStats() const {
1139 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
1140 RTC_DCHECK(stream_);
1141 return stream_->GetStats();
1142 }
1143
Taylor Brandstetter1a018dc2016-03-08 12:37:39 -08001144 // Starts the sending by setting ourselves as a sink to the AudioSource to
1145 // get data callbacks.
henrike@webrtc.orga7b98182014-02-21 15:51:43 +00001146 // This method is called on the libjingle worker thread.
mallinath@webrtc.org67ee6b92014-02-03 16:57:16 +00001147 // TODO(xians): Make sure Start() is called only once.
Taylor Brandstetter1a018dc2016-03-08 12:37:39 -08001148 void SetSource(AudioSource* source) {
solenberg566ef242015-11-06 15:34:49 -08001149 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
Taylor Brandstetter1a018dc2016-03-08 12:37:39 -08001150 RTC_DCHECK(source);
1151 if (source_) {
1152 RTC_DCHECK(source_ == source);
mallinath@webrtc.org67ee6b92014-02-03 16:57:16 +00001153 return;
1154 }
Taylor Brandstetter1a018dc2016-03-08 12:37:39 -08001155 source->SetSink(this);
1156 source_ = source;
1157 UpdateSendState();
mallinath@webrtc.org67ee6b92014-02-03 16:57:16 +00001158 }
1159
Taylor Brandstetter1a018dc2016-03-08 12:37:39 -08001160 // Stops sending by setting the sink of the AudioSource to nullptr. No data
mallinath@webrtc.org67ee6b92014-02-03 16:57:16 +00001161 // callback will be received after this method.
henrike@webrtc.orga7b98182014-02-21 15:51:43 +00001162 // This method is called on the libjingle worker thread.
Taylor Brandstetter1a018dc2016-03-08 12:37:39 -08001163 void ClearSource() {
solenberg566ef242015-11-06 15:34:49 -08001164 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
Taylor Brandstetter1a018dc2016-03-08 12:37:39 -08001165 if (source_) {
1166 source_->SetSink(nullptr);
1167 source_ = nullptr;
solenberg98c68862015-10-09 03:27:14 -07001168 }
Taylor Brandstetter1a018dc2016-03-08 12:37:39 -08001169 UpdateSendState();
mallinath@webrtc.org67ee6b92014-02-03 16:57:16 +00001170 }
1171
Taylor Brandstetter1a018dc2016-03-08 12:37:39 -08001172 // AudioSource::Sink implementation.
henrike@webrtc.orga7b98182014-02-21 15:51:43 +00001173 // This method is called on the audio thread.
kjellander@webrtc.org14665ff2015-03-04 12:58:35 +00001174 void OnData(const void* audio_data,
1175 int bits_per_sample,
1176 int sample_rate,
Peter Kasting69558702016-01-12 16:26:35 -08001177 size_t number_of_channels,
Peter Kastingdce40cf2015-08-24 14:52:23 -07001178 size_t number_of_frames) override {
solenberg566ef242015-11-06 15:34:49 -08001179 RTC_DCHECK(!worker_thread_checker_.CalledOnValidThread());
solenberg85a04962015-10-27 03:35:21 -07001180 RTC_DCHECK(audio_capture_thread_checker_.CalledOnValidThread());
solenbergc96df772015-10-21 13:01:53 -07001181 RTC_DCHECK(voe_audio_transport_);
solenberg7add0582015-11-20 09:59:34 -08001182 voe_audio_transport_->OnData(config_.voe_channel_id,
henrike@webrtc.orga7b98182014-02-21 15:51:43 +00001183 audio_data,
1184 bits_per_sample,
1185 sample_rate,
1186 number_of_channels,
1187 number_of_frames);
henrike@webrtc.orga7b98182014-02-21 15:51:43 +00001188 }
1189
Taylor Brandstetter1a018dc2016-03-08 12:37:39 -08001190 // Callback from the |source_| when it is going away. In case Start() has
henrike@webrtc.orga7b98182014-02-21 15:51:43 +00001191 // never been called, this callback won't be triggered.
kjellander@webrtc.org14665ff2015-03-04 12:58:35 +00001192 void OnClose() override {
solenberg566ef242015-11-06 15:34:49 -08001193 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
Taylor Brandstetter1a018dc2016-03-08 12:37:39 -08001194 // Set |source_| to nullptr to make sure no more callback will get into
1195 // the source.
1196 source_ = nullptr;
1197 UpdateSendState();
mallinath@webrtc.org67ee6b92014-02-03 16:57:16 +00001198 }
1199
1200 // Accessor to the VoE channel ID.
solenberg85a04962015-10-27 03:35:21 -07001201 int channel() const {
solenberg566ef242015-11-06 15:34:49 -08001202 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
solenberg7add0582015-11-20 09:59:34 -08001203 return config_.voe_channel_id;
solenberg85a04962015-10-27 03:35:21 -07001204 }
mallinath@webrtc.org67ee6b92014-02-03 16:57:16 +00001205
skvlade0d46372016-04-07 22:59:22 -07001206 const webrtc::RtpParameters& rtp_parameters() const {
1207 return rtp_parameters_;
1208 }
1209
1210 void set_rtp_parameters(const webrtc::RtpParameters& parameters) {
1211 RTC_CHECK_EQ(1UL, parameters.encodings.size());
1212 rtp_parameters_ = parameters;
1213 }
1214
mallinath@webrtc.org67ee6b92014-02-03 16:57:16 +00001215 private:
Taylor Brandstetter1a018dc2016-03-08 12:37:39 -08001216 void UpdateSendState() {
1217 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
1218 RTC_DCHECK(stream_);
1219 if (send_ && source_ != nullptr) {
1220 stream_->Start();
1221 } else { // !send || source_ = nullptr
1222 stream_->Stop();
1223 }
1224 }
1225
solenberg566ef242015-11-06 15:34:49 -08001226 rtc::ThreadChecker worker_thread_checker_;
solenberg85a04962015-10-27 03:35:21 -07001227 rtc::ThreadChecker audio_capture_thread_checker_;
solenbergc96df772015-10-21 13:01:53 -07001228 webrtc::AudioTransport* const voe_audio_transport_ = nullptr;
1229 webrtc::Call* call_ = nullptr;
solenberg3a941542015-11-16 07:34:50 -08001230 webrtc::AudioSendStream::Config config_;
1231 // The stream is owned by WebRtcAudioSendStream and may be reallocated if
1232 // configuration changes.
solenbergc96df772015-10-21 13:01:53 -07001233 webrtc::AudioSendStream* stream_ = nullptr;
mallinath@webrtc.org67ee6b92014-02-03 16:57:16 +00001234
Taylor Brandstetter1a018dc2016-03-08 12:37:39 -08001235 // Raw pointer to AudioSource owned by LocalAudioTrackHandler.
mallinath@webrtc.org67ee6b92014-02-03 16:57:16 +00001236 // PeerConnection will make sure invalidating the pointer before the object
1237 // goes away.
Taylor Brandstetter1a018dc2016-03-08 12:37:39 -08001238 AudioSource* source_ = nullptr;
1239 bool send_ = false;
skvlade0d46372016-04-07 22:59:22 -07001240 webrtc::RtpParameters rtp_parameters_;
henrike@webrtc.orga7b98182014-02-21 15:51:43 +00001241
solenbergc96df772015-10-21 13:01:53 -07001242 RTC_DISALLOW_IMPLICIT_CONSTRUCTORS(WebRtcAudioSendStream);
1243};
1244
1245class WebRtcVoiceMediaChannel::WebRtcAudioReceiveStream {
1246 public:
stefanba4c0e42016-02-04 04:12:24 -08001247 WebRtcAudioReceiveStream(int ch,
1248 uint32_t remote_ssrc,
1249 uint32_t local_ssrc,
1250 bool use_transport_cc,
1251 const std::string& sync_group,
solenberg7add0582015-11-20 09:59:34 -08001252 const std::vector<webrtc::RtpExtension>& extensions,
1253 webrtc::Call* call)
stefanba4c0e42016-02-04 04:12:24 -08001254 : call_(call), config_() {
solenberg7add0582015-11-20 09:59:34 -08001255 RTC_DCHECK_GE(ch, 0);
1256 RTC_DCHECK(call);
1257 config_.rtp.remote_ssrc = remote_ssrc;
1258 config_.rtp.local_ssrc = local_ssrc;
1259 config_.voe_channel_id = ch;
1260 config_.sync_group = sync_group;
stefanba4c0e42016-02-04 04:12:24 -08001261 RecreateAudioReceiveStream(use_transport_cc, extensions);
solenberg7add0582015-11-20 09:59:34 -08001262 }
solenbergc96df772015-10-21 13:01:53 -07001263
solenberg7add0582015-11-20 09:59:34 -08001264 ~WebRtcAudioReceiveStream() {
1265 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
1266 call_->DestroyAudioReceiveStream(stream_);
1267 }
1268
1269 void RecreateAudioReceiveStream(
1270 const std::vector<webrtc::RtpExtension>& extensions) {
1271 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
stefanba4c0e42016-02-04 04:12:24 -08001272 RecreateAudioReceiveStream(config_.rtp.transport_cc, extensions);
solenberg7add0582015-11-20 09:59:34 -08001273 }
stefanba4c0e42016-02-04 04:12:24 -08001274 void RecreateAudioReceiveStream(bool use_transport_cc) {
solenberg7add0582015-11-20 09:59:34 -08001275 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
stefanba4c0e42016-02-04 04:12:24 -08001276 RecreateAudioReceiveStream(use_transport_cc, config_.rtp.extensions);
solenberg7add0582015-11-20 09:59:34 -08001277 }
1278
1279 webrtc::AudioReceiveStream::Stats GetStats() const {
1280 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
1281 RTC_DCHECK(stream_);
1282 return stream_->GetStats();
1283 }
1284
1285 int channel() const {
1286 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
1287 return config_.voe_channel_id;
1288 }
solenbergc96df772015-10-21 13:01:53 -07001289
kwiberg686a8ef2016-02-26 03:00:35 -08001290 void SetRawAudioSink(std::unique_ptr<webrtc::AudioSinkInterface> sink) {
Tommif888bb52015-12-12 01:37:01 +01001291 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
kwiberg686a8ef2016-02-26 03:00:35 -08001292 stream_->SetSink(std::move(sink));
Tommif888bb52015-12-12 01:37:01 +01001293 }
1294
solenbergc96df772015-10-21 13:01:53 -07001295 private:
stefanba4c0e42016-02-04 04:12:24 -08001296 void RecreateAudioReceiveStream(
1297 bool use_transport_cc,
solenberg7add0582015-11-20 09:59:34 -08001298 const std::vector<webrtc::RtpExtension>& extensions) {
1299 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
1300 if (stream_) {
1301 call_->DestroyAudioReceiveStream(stream_);
1302 stream_ = nullptr;
1303 }
1304 config_.rtp.extensions = extensions;
stefanba4c0e42016-02-04 04:12:24 -08001305 config_.rtp.transport_cc = use_transport_cc;
solenberg7add0582015-11-20 09:59:34 -08001306 RTC_DCHECK(!stream_);
1307 stream_ = call_->CreateAudioReceiveStream(config_);
1308 RTC_CHECK(stream_);
1309 }
1310
1311 rtc::ThreadChecker worker_thread_checker_;
1312 webrtc::Call* call_ = nullptr;
1313 webrtc::AudioReceiveStream::Config config_;
1314 // The stream is owned by WebRtcAudioReceiveStream and may be reallocated if
1315 // configuration changes.
1316 webrtc::AudioReceiveStream* stream_ = nullptr;
solenbergc96df772015-10-21 13:01:53 -07001317
1318 RTC_DISALLOW_IMPLICIT_CONSTRUCTORS(WebRtcAudioReceiveStream);
henrike@webrtc.org1e09a712013-07-26 19:17:59 +00001319};
1320
Fredrik Solenberg709ed672015-09-15 12:26:33 +02001321WebRtcVoiceMediaChannel::WebRtcVoiceMediaChannel(WebRtcVoiceEngine* engine,
nisse51542be2016-02-12 02:27:06 -08001322 const MediaConfig& config,
Fredrik Solenbergb071a192015-09-17 16:42:56 +02001323 const AudioOptions& options,
Fredrik Solenberg709ed672015-09-15 12:26:33 +02001324 webrtc::Call* call)
nisse51542be2016-02-12 02:27:06 -08001325 : VoiceMediaChannel(config), engine_(engine), call_(call) {
solenberg0a617e22015-10-20 15:49:38 -07001326 LOG(LS_VERBOSE) << "WebRtcVoiceMediaChannel::WebRtcVoiceMediaChannel";
solenberg566ef242015-11-06 15:34:49 -08001327 RTC_DCHECK(call);
solenberg0a617e22015-10-20 15:49:38 -07001328 engine->RegisterChannel(this);
Fredrik Solenbergb071a192015-09-17 16:42:56 +02001329 SetOptions(options);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001330}
1331
1332WebRtcVoiceMediaChannel::~WebRtcVoiceMediaChannel() {
solenberg566ef242015-11-06 15:34:49 -08001333 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
solenberg0a617e22015-10-20 15:49:38 -07001334 LOG(LS_VERBOSE) << "WebRtcVoiceMediaChannel::~WebRtcVoiceMediaChannel";
solenberg7add0582015-11-20 09:59:34 -08001335 // TODO(solenberg): Should be able to delete the streams directly, without
1336 // going through RemoveNnStream(), once stream objects handle
1337 // all (de)configuration.
solenbergc96df772015-10-21 13:01:53 -07001338 while (!send_streams_.empty()) {
1339 RemoveSendStream(send_streams_.begin()->first);
solenbergd97ec302015-10-07 01:40:33 -07001340 }
solenberg7add0582015-11-20 09:59:34 -08001341 while (!recv_streams_.empty()) {
1342 RemoveRecvStream(recv_streams_.begin()->first);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001343 }
solenberg0a617e22015-10-20 15:49:38 -07001344 engine()->UnregisterChannel(this);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001345}
1346
nisse51542be2016-02-12 02:27:06 -08001347rtc::DiffServCodePoint WebRtcVoiceMediaChannel::PreferredDscp() const {
1348 return kAudioDscpValue;
1349}
1350
Peter Thatcherc2ee2c82015-08-07 16:05:34 -07001351bool WebRtcVoiceMediaChannel::SetSendParameters(
1352 const AudioSendParameters& params) {
Peter Boströmca8b4042016-03-08 14:24:13 -08001353 TRACE_EVENT0("webrtc", "WebRtcVoiceMediaChannel::SetSendParameters");
solenberg566ef242015-11-06 15:34:49 -08001354 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
solenberg7e4e01a2015-12-02 08:05:01 -08001355 LOG(LS_INFO) << "WebRtcVoiceMediaChannel::SetSendParameters: "
1356 << params.ToString();
Peter Thatcherc2ee2c82015-08-07 16:05:34 -07001357 // TODO(pthatcher): Refactor this to be more clean now that we have
1358 // all the information at once.
solenberg3a941542015-11-16 07:34:50 -08001359
1360 if (!SetSendCodecs(params.codecs)) {
1361 return false;
1362 }
1363
solenberg7e4e01a2015-12-02 08:05:01 -08001364 if (!ValidateRtpExtensions(params.extensions)) {
1365 return false;
1366 }
1367 std::vector<webrtc::RtpExtension> filtered_extensions =
1368 FilterRtpExtensions(params.extensions,
1369 webrtc::RtpExtension::IsSupportedForAudio, true);
1370 if (send_rtp_extensions_ != filtered_extensions) {
1371 send_rtp_extensions_.swap(filtered_extensions);
solenberg3a941542015-11-16 07:34:50 -08001372 for (auto& it : send_streams_) {
1373 it.second->RecreateAudioSendStream(send_rtp_extensions_);
1374 }
1375 }
1376
skvlade0d46372016-04-07 22:59:22 -07001377 if (!SetSendBitrate(params.max_bandwidth_bps)) {
solenberg3a941542015-11-16 07:34:50 -08001378 return false;
1379 }
1380 return SetOptions(params.options);
Peter Thatcherc2ee2c82015-08-07 16:05:34 -07001381}
1382
1383bool WebRtcVoiceMediaChannel::SetRecvParameters(
1384 const AudioRecvParameters& params) {
Peter Boströmca8b4042016-03-08 14:24:13 -08001385 TRACE_EVENT0("webrtc", "WebRtcVoiceMediaChannel::SetRecvParameters");
solenberg566ef242015-11-06 15:34:49 -08001386 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
solenberg7e4e01a2015-12-02 08:05:01 -08001387 LOG(LS_INFO) << "WebRtcVoiceMediaChannel::SetRecvParameters: "
1388 << params.ToString();
Peter Thatcherc2ee2c82015-08-07 16:05:34 -07001389 // TODO(pthatcher): Refactor this to be more clean now that we have
1390 // all the information at once.
solenberg7add0582015-11-20 09:59:34 -08001391
1392 if (!SetRecvCodecs(params.codecs)) {
1393 return false;
1394 }
1395
solenberg7e4e01a2015-12-02 08:05:01 -08001396 if (!ValidateRtpExtensions(params.extensions)) {
1397 return false;
1398 }
1399 std::vector<webrtc::RtpExtension> filtered_extensions =
1400 FilterRtpExtensions(params.extensions,
1401 webrtc::RtpExtension::IsSupportedForAudio, false);
1402 if (recv_rtp_extensions_ != filtered_extensions) {
1403 recv_rtp_extensions_.swap(filtered_extensions);
solenberg7add0582015-11-20 09:59:34 -08001404 for (auto& it : recv_streams_) {
1405 it.second->RecreateAudioReceiveStream(recv_rtp_extensions_);
1406 }
1407 }
solenberg7add0582015-11-20 09:59:34 -08001408 return true;
Peter Thatcherc2ee2c82015-08-07 16:05:34 -07001409}
1410
skvlade0d46372016-04-07 22:59:22 -07001411webrtc::RtpParameters WebRtcVoiceMediaChannel::GetRtpParameters(
1412 uint32_t ssrc) const {
1413 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
1414 auto it = send_streams_.find(ssrc);
1415 if (it == send_streams_.end()) {
1416 LOG(LS_WARNING) << "Attempting to get RTP parameters for stream with ssrc "
1417 << ssrc << " which doesn't exist.";
1418 return webrtc::RtpParameters();
1419 }
1420
1421 return it->second->rtp_parameters();
1422}
1423
1424bool WebRtcVoiceMediaChannel::SetRtpParameters(
1425 uint32_t ssrc,
1426 const webrtc::RtpParameters& parameters) {
1427 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
1428 if (!ValidateRtpParameters(parameters)) {
1429 return false;
1430 }
1431 auto it = send_streams_.find(ssrc);
1432 if (it == send_streams_.end()) {
1433 LOG(LS_WARNING) << "Attempting to set RTP parameters for stream with ssrc "
1434 << ssrc << " which doesn't exist.";
1435 return false;
1436 }
1437
1438 if (!SetChannelParameters(it->second->channel(), parameters)) {
1439 LOG(LS_WARNING) << "Failed to set RtpParameters.";
1440 return false;
1441 }
1442 it->second->set_rtp_parameters(parameters);
1443 return true;
1444}
1445
1446bool WebRtcVoiceMediaChannel::ValidateRtpParameters(
1447 const webrtc::RtpParameters& rtp_parameters) {
1448 if (rtp_parameters.encodings.size() != 1) {
1449 LOG(LS_ERROR)
1450 << "Attempted to set RtpParameters without exactly one encoding";
1451 return false;
1452 }
1453 return true;
1454}
1455
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001456bool WebRtcVoiceMediaChannel::SetOptions(const AudioOptions& options) {
solenberg566ef242015-11-06 15:34:49 -08001457 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001458 LOG(LS_INFO) << "Setting voice channel options: "
1459 << options.ToString();
1460
1461 // We retain all of the existing options, and apply the given ones
1462 // on top. This means there is no way to "clear" options such that
1463 // they go back to the engine default.
1464 options_.SetAll(options);
solenberg246b8172015-12-08 09:50:23 -08001465 if (!engine()->ApplyOptions(options_)) {
1466 LOG(LS_WARNING) <<
1467 "Failed to apply engine options during channel SetOptions.";
1468 return false;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001469 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001470 LOG(LS_INFO) << "Set voice channel options. Current options: "
1471 << options_.ToString();
1472 return true;
1473}
1474
1475bool WebRtcVoiceMediaChannel::SetRecvCodecs(
1476 const std::vector<AudioCodec>& codecs) {
solenberg566ef242015-11-06 15:34:49 -08001477 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
solenberg8fb30c32015-10-13 03:06:58 -07001478
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001479 // Set the payload types to be used for incoming media.
solenberg0b675462015-10-09 01:37:09 -07001480 LOG(LS_INFO) << "Setting receive voice codecs.";
solenberg0b675462015-10-09 01:37:09 -07001481
1482 if (!VerifyUniquePayloadTypes(codecs)) {
1483 LOG(LS_ERROR) << "Codec payload types overlap.";
1484 return false;
1485 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001486
1487 std::vector<AudioCodec> new_codecs;
1488 // Find all new codecs. We allow adding new codecs but don't allow changing
1489 // the payload type of codecs that is already configured since we might
1490 // already be receiving packets with that payload type.
Fredrik Solenbergaf9fb212015-08-26 10:45:53 +02001491 for (const AudioCodec& codec : codecs) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001492 AudioCodec old_codec;
Fredrik Solenbergaf9fb212015-08-26 10:45:53 +02001493 if (FindCodec(recv_codecs_, codec, &old_codec)) {
1494 if (old_codec.id != codec.id) {
1495 LOG(LS_ERROR) << codec.name << " payload type changed.";
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001496 return false;
1497 }
1498 } else {
Fredrik Solenbergaf9fb212015-08-26 10:45:53 +02001499 new_codecs.push_back(codec);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001500 }
1501 }
1502 if (new_codecs.empty()) {
1503 // There are no new codecs to configure. Already configured codecs are
1504 // never removed.
1505 return true;
1506 }
1507
1508 if (playout_) {
1509 // Receive codecs can not be changed while playing. So we temporarily
1510 // pause playout.
1511 PausePlayout();
1512 }
1513
solenberg26c8c912015-11-27 04:00:25 -08001514 bool result = true;
1515 for (const AudioCodec& codec : new_codecs) {
solenberg72e29d22016-03-08 06:35:16 -08001516 webrtc::CodecInst voe_codec = {0};
solenberg26c8c912015-11-27 04:00:25 -08001517 if (WebRtcVoiceEngine::ToCodecInst(codec, &voe_codec)) {
1518 LOG(LS_INFO) << ToString(codec);
1519 voe_codec.pltype = codec.id;
1520 for (const auto& ch : recv_streams_) {
1521 if (engine()->voe()->codec()->SetRecPayloadType(
1522 ch.second->channel(), voe_codec) == -1) {
1523 LOG_RTCERR2(SetRecPayloadType, ch.second->channel(),
1524 ToString(voe_codec));
1525 result = false;
1526 }
1527 }
1528 } else {
1529 LOG(LS_WARNING) << "Unknown codec " << ToString(codec);
1530 result = false;
1531 break;
1532 }
1533 }
Fredrik Solenbergaf9fb212015-08-26 10:45:53 +02001534 if (result) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001535 recv_codecs_ = codecs;
1536 }
1537
1538 if (desired_playout_ && !playout_) {
1539 ResumePlayout();
1540 }
Fredrik Solenbergaf9fb212015-08-26 10:45:53 +02001541 return result;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001542}
1543
solenberg72e29d22016-03-08 06:35:16 -08001544// Utility function called from SetSendParameters() to extract current send
1545// codec settings from the given list of codecs (originally from SDP). Both send
1546// and receive streams may be reconfigured based on the new settings.
wu@webrtc.orgcadf9042013-08-30 21:24:16 +00001547bool WebRtcVoiceMediaChannel::SetSendCodecs(
1548 const std::vector<AudioCodec>& codecs) {
solenberg566ef242015-11-06 15:34:49 -08001549 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
Fredrik Solenbergb5727682015-12-04 15:22:19 +01001550 // TODO(solenberg): Validate input - that payload types don't overlap, are
1551 // within range, filter out codecs we don't support,
solenberg31642aa2016-03-14 08:00:37 -07001552 // redundant codecs etc - the same way it is done for
1553 // RtpHeaderExtensions.
solenbergd97ec302015-10-07 01:40:33 -07001554
Fredrik Solenbergb5727682015-12-04 15:22:19 +01001555 // Find the DTMF telephone event "codec" payload type.
1556 dtmf_payload_type_ = rtc::Optional<int>();
Fredrik Solenbergaf9fb212015-08-26 10:45:53 +02001557 for (const AudioCodec& codec : codecs) {
Fredrik Solenbergaf9fb212015-08-26 10:45:53 +02001558 if (IsCodec(codec, kDtmfCodecName)) {
solenberg31642aa2016-03-14 08:00:37 -07001559 if (codec.id < kMinPayloadType || codec.id > kMaxPayloadType) {
1560 return false;
1561 }
Fredrik Solenbergb5727682015-12-04 15:22:19 +01001562 dtmf_payload_type_ = rtc::Optional<int>(codec.id);
1563 break;
wu@webrtc.orgcadf9042013-08-30 21:24:16 +00001564 }
1565 }
1566
solenberg72e29d22016-03-08 06:35:16 -08001567 // Scan through the list to figure out the codec to use for sending, along
1568 // with the proper configuration for VAD, CNG, RED, NACK and Opus-specific
1569 // parameters.
1570 {
1571 SendCodecSpec send_codec_spec;
1572 send_codec_spec.nack_enabled = send_codec_spec_.nack_enabled;
1573
1574 // Find send codec (the first non-telephone-event/CN codec).
1575 const AudioCodec* codec = WebRtcVoiceCodecs::GetPreferredCodec(
1576 codecs, &send_codec_spec.codec_inst, &send_codec_spec.red_payload_type);
1577 if (!codec) {
1578 LOG(LS_WARNING) << "Received empty list of codecs.";
1579 return false;
1580 }
1581
1582 send_codec_spec.transport_cc_enabled = HasTransportCc(*codec);
1583
1584 // This condition is apparently here because Opus does not support RED and
1585 // FEC simultaneously. However, DTX and max playback rate shouldn't have
1586 // such limitations.
1587 // TODO(solenberg): Refactor this logic once we create AudioEncoders here.
1588 if (send_codec_spec.red_payload_type == -1) {
1589 send_codec_spec.nack_enabled = HasNack(*codec);
1590 // For Opus as the send codec, we are to determine inband FEC, maximum
1591 // playback rate, and opus internal dtx.
1592 if (IsCodec(*codec, kOpusCodecName)) {
1593 GetOpusConfig(*codec, &send_codec_spec.codec_inst,
1594 &send_codec_spec.enable_codec_fec,
1595 &send_codec_spec.opus_max_playback_rate,
1596 &send_codec_spec.enable_opus_dtx);
1597 }
1598
1599 // Set packet size if the AudioCodec param kCodecParamPTime is set.
1600 int ptime_ms = 0;
1601 if (codec->GetParam(kCodecParamPTime, &ptime_ms)) {
1602 if (!WebRtcVoiceCodecs::SetPTimeAsPacketSize(
1603 &send_codec_spec.codec_inst, ptime_ms)) {
1604 LOG(LS_WARNING) << "Failed to set packet size for codec "
1605 << send_codec_spec.codec_inst.plname;
1606 return false;
1607 }
1608 }
1609 }
1610
1611 // Loop through the codecs list again to find the CN codec.
1612 // TODO(solenberg): Break out into a separate function?
1613 for (const AudioCodec& codec : codecs) {
1614 // Ignore codecs we don't know about. The negotiation step should prevent
1615 // this, but double-check to be sure.
1616 webrtc::CodecInst voe_codec = {0};
1617 if (!WebRtcVoiceEngine::ToCodecInst(codec, &voe_codec)) {
1618 LOG(LS_WARNING) << "Unknown codec " << ToString(codec);
1619 continue;
1620 }
1621
1622 if (IsCodec(codec, kCnCodecName)) {
1623 // Turn voice activity detection/comfort noise on if supported.
1624 // Set the wideband CN payload type appropriately.
1625 // (narrowband always uses the static payload type 13).
1626 int cng_plfreq = -1;
1627 switch (codec.clockrate) {
1628 case 8000:
1629 case 16000:
1630 case 32000:
1631 cng_plfreq = codec.clockrate;
1632 break;
1633 default:
1634 LOG(LS_WARNING) << "CN frequency " << codec.clockrate
1635 << " not supported.";
1636 continue;
1637 }
1638 send_codec_spec.cng_payload_type = codec.id;
1639 send_codec_spec.cng_plfreq = cng_plfreq;
1640 break;
1641 }
1642 }
1643
1644 // Latch in the new state.
1645 send_codec_spec_ = std::move(send_codec_spec);
1646 }
1647
wu@webrtc.orgcadf9042013-08-30 21:24:16 +00001648 // Cache the codecs in order to configure the channel created later.
solenbergc96df772015-10-21 13:01:53 -07001649 for (const auto& ch : send_streams_) {
skvlade0d46372016-04-07 22:59:22 -07001650 if (!SetSendCodecs(ch.second->channel(), ch.second->rtp_parameters())) {
wu@webrtc.orgcadf9042013-08-30 21:24:16 +00001651 return false;
1652 }
1653 }
1654
solenberg72e29d22016-03-08 06:35:16 -08001655 // Set nack status on receive channels.
1656 if (!send_streams_.empty()) {
1657 for (const auto& kv : recv_streams_) {
1658 SetNack(kv.second->channel(), send_codec_spec_.nack_enabled);
1659 }
wu@webrtc.org9dba5252013-08-05 20:36:57 +00001660 }
solenberg0a617e22015-10-20 15:49:38 -07001661
stefanba4c0e42016-02-04 04:12:24 -08001662 // Check if the transport cc feedback has changed on the preferred send codec,
1663 // and in that case reconfigure all receive streams.
solenberg72e29d22016-03-08 06:35:16 -08001664 if (recv_transport_cc_enabled_ != send_codec_spec_.transport_cc_enabled) {
1665 LOG(LS_INFO) << "Recreate all the receive streams because the send "
1666 "codec has changed.";
1667 recv_transport_cc_enabled_ = send_codec_spec_.transport_cc_enabled;
1668 for (auto& kv : recv_streams_) {
1669 kv.second->RecreateAudioReceiveStream(recv_transport_cc_enabled_);
1670 }
1671 }
1672
1673 return true;
1674}
1675
1676// Apply current codec settings to a single voe::Channel used for sending.
skvlade0d46372016-04-07 22:59:22 -07001677bool WebRtcVoiceMediaChannel::SetSendCodecs(
1678 int channel,
1679 const webrtc::RtpParameters& rtp_parameters) {
solenberg72e29d22016-03-08 06:35:16 -08001680 // Disable VAD, FEC, and RED unless we know the other side wants them.
1681 engine()->voe()->codec()->SetVADStatus(channel, false);
1682 engine()->voe()->rtp()->SetNACKStatus(channel, false, 0);
1683 engine()->voe()->rtp()->SetREDStatus(channel, false);
1684 engine()->voe()->codec()->SetFECStatus(channel, false);
1685
1686 if (send_codec_spec_.red_payload_type != -1) {
1687 // Enable redundant encoding of the specified codec. Treat any
1688 // failure as a fatal internal error.
1689 LOG(LS_INFO) << "Enabling RED on channel " << channel;
1690 if (engine()->voe()->rtp()->SetREDStatus(channel, true,
1691 send_codec_spec_.red_payload_type) == -1) {
1692 LOG_RTCERR3(SetREDStatus, channel, true,
1693 send_codec_spec_.red_payload_type);
1694 return false;
1695 }
1696 }
1697
1698 SetNack(channel, send_codec_spec_.nack_enabled);
1699
1700 // Set the codec immediately, since SetVADStatus() depends on whether
1701 // the current codec is mono or stereo.
1702 if (!SetSendCodec(channel, send_codec_spec_.codec_inst)) {
1703 return false;
1704 }
1705
1706 // FEC should be enabled after SetSendCodec.
1707 if (send_codec_spec_.enable_codec_fec) {
1708 LOG(LS_INFO) << "Attempt to enable codec internal FEC on channel "
1709 << channel;
1710 if (engine()->voe()->codec()->SetFECStatus(channel, true) == -1) {
1711 // Enable codec internal FEC. Treat any failure as fatal internal error.
1712 LOG_RTCERR2(SetFECStatus, channel, true);
1713 return false;
1714 }
1715 }
1716
1717 if (IsCodec(send_codec_spec_.codec_inst, kOpusCodecName)) {
1718 // DTX and maxplaybackrate should be set after SetSendCodec. Because current
1719 // send codec has to be Opus.
1720
1721 // Set Opus internal DTX.
1722 LOG(LS_INFO) << "Attempt to "
1723 << (send_codec_spec_.enable_opus_dtx ? "enable" : "disable")
1724 << " Opus DTX on channel "
1725 << channel;
1726 if (engine()->voe()->codec()->SetOpusDtx(channel,
1727 send_codec_spec_.enable_opus_dtx)) {
1728 LOG_RTCERR2(SetOpusDtx, channel, send_codec_spec_.enable_opus_dtx);
1729 return false;
1730 }
1731
1732 // If opus_max_playback_rate <= 0, the default maximum playback rate
1733 // (48 kHz) will be used.
1734 if (send_codec_spec_.opus_max_playback_rate > 0) {
1735 LOG(LS_INFO) << "Attempt to set maximum playback rate to "
1736 << send_codec_spec_.opus_max_playback_rate
1737 << " Hz on channel "
1738 << channel;
1739 if (engine()->voe()->codec()->SetOpusMaxPlaybackRate(
1740 channel, send_codec_spec_.opus_max_playback_rate) == -1) {
1741 LOG_RTCERR2(SetOpusMaxPlaybackRate, channel,
1742 send_codec_spec_.opus_max_playback_rate);
1743 return false;
stefanba4c0e42016-02-04 04:12:24 -08001744 }
1745 }
1746 }
skvlade0d46372016-04-07 22:59:22 -07001747 // TODO(solenberg): SetSendBitrate() yields another call to SetSendCodec().
1748 // Check if it is possible to fuse with the previous call in this function.
1749 SetChannelParameters(channel, rtp_parameters);
solenberg72e29d22016-03-08 06:35:16 -08001750
1751 // Set the CN payloadtype and the VAD status.
1752 if (send_codec_spec_.cng_payload_type != -1) {
1753 // The CN payload type for 8000 Hz clockrate is fixed at 13.
1754 if (send_codec_spec_.cng_plfreq != 8000) {
1755 webrtc::PayloadFrequencies cn_freq;
1756 switch (send_codec_spec_.cng_plfreq) {
1757 case 16000:
1758 cn_freq = webrtc::kFreq16000Hz;
1759 break;
1760 case 32000:
1761 cn_freq = webrtc::kFreq32000Hz;
1762 break;
1763 default:
1764 RTC_NOTREACHED();
1765 return false;
1766 }
1767 if (engine()->voe()->codec()->SetSendCNPayloadType(
1768 channel, send_codec_spec_.cng_payload_type, cn_freq) == -1) {
1769 LOG_RTCERR3(SetSendCNPayloadType, channel,
1770 send_codec_spec_.cng_payload_type, cn_freq);
1771 // TODO(ajm): This failure condition will be removed from VoE.
1772 // Restore the return here when we update to a new enough webrtc.
1773 //
1774 // Not returning false because the SetSendCNPayloadType will fail if
1775 // the channel is already sending.
1776 // This can happen if the remote description is applied twice, for
1777 // example in the case of ROAP on top of JSEP, where both side will
1778 // send the offer.
1779 }
1780 }
1781
1782 // Only turn on VAD if we have a CN payload type that matches the
1783 // clockrate for the codec we are going to use.
1784 if (send_codec_spec_.cng_plfreq == send_codec_spec_.codec_inst.plfreq &&
1785 send_codec_spec_.codec_inst.channels == 1) {
1786 // TODO(minyue): If CN frequency == 48000 Hz is allowed, consider the
1787 // interaction between VAD and Opus FEC.
1788 LOG(LS_INFO) << "Enabling VAD";
1789 if (engine()->voe()->codec()->SetVADStatus(channel, true) == -1) {
1790 LOG_RTCERR2(SetVADStatus, channel, true);
1791 return false;
1792 }
1793 }
1794 }
solenberg0a617e22015-10-20 15:49:38 -07001795 return true;
wu@webrtc.org9dba5252013-08-05 20:36:57 +00001796}
1797
wu@webrtc.orgcadf9042013-08-30 21:24:16 +00001798void WebRtcVoiceMediaChannel::SetNack(int channel, bool nack_enabled) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001799 if (nack_enabled) {
wu@webrtc.orgcadf9042013-08-30 21:24:16 +00001800 LOG(LS_INFO) << "Enabling NACK for channel " << channel;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001801 engine()->voe()->rtp()->SetNACKStatus(channel, true, kNackMaxPackets);
1802 } else {
wu@webrtc.orgcadf9042013-08-30 21:24:16 +00001803 LOG(LS_INFO) << "Disabling NACK for channel " << channel;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001804 engine()->voe()->rtp()->SetNACKStatus(channel, false, 0);
1805 }
1806}
1807
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001808bool WebRtcVoiceMediaChannel::SetSendCodec(
wu@webrtc.org9dba5252013-08-05 20:36:57 +00001809 int channel, const webrtc::CodecInst& send_codec) {
1810 LOG(LS_INFO) << "Send channel " << channel << " selected voice codec "
1811 << ToString(send_codec) << ", bitrate=" << send_codec.rate;
1812
solenberg72e29d22016-03-08 06:35:16 -08001813 webrtc::CodecInst current_codec = {0};
wu@webrtc.org05e7b442014-04-01 17:44:24 +00001814 if (engine()->voe()->codec()->GetSendCodec(channel, current_codec) == 0 &&
1815 (send_codec == current_codec)) {
1816 // Codec is already configured, we can return without setting it again.
1817 return true;
1818 }
1819
wu@webrtc.org9dba5252013-08-05 20:36:57 +00001820 if (engine()->voe()->codec()->SetSendCodec(channel, send_codec) == -1) {
1821 LOG_RTCERR2(SetSendCodec, channel, ToString(send_codec));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001822 return false;
1823 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001824 return true;
1825}
1826
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001827bool WebRtcVoiceMediaChannel::SetPlayout(bool playout) {
1828 desired_playout_ = playout;
1829 return ChangePlayout(desired_playout_);
1830}
1831
1832bool WebRtcVoiceMediaChannel::PausePlayout() {
1833 return ChangePlayout(false);
1834}
1835
1836bool WebRtcVoiceMediaChannel::ResumePlayout() {
1837 return ChangePlayout(desired_playout_);
1838}
1839
1840bool WebRtcVoiceMediaChannel::ChangePlayout(bool playout) {
Peter Boströmca8b4042016-03-08 14:24:13 -08001841 TRACE_EVENT0("webrtc", "WebRtcVoiceMediaChannel::ChangePlayout");
solenberg566ef242015-11-06 15:34:49 -08001842 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001843 if (playout_ == playout) {
1844 return true;
1845 }
1846
solenberg7add0582015-11-20 09:59:34 -08001847 for (const auto& ch : recv_streams_) {
Fredrik Solenbergaf9fb212015-08-26 10:45:53 +02001848 if (!SetPlayout(ch.second->channel(), playout)) {
henrike@webrtc.org1e09a712013-07-26 19:17:59 +00001849 LOG(LS_ERROR) << "SetPlayout " << playout << " on channel "
Fredrik Solenbergaf9fb212015-08-26 10:45:53 +02001850 << ch.second->channel() << " failed";
solenberg1ac56142015-10-13 03:58:19 -07001851 return false;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001852 }
1853 }
solenberg1ac56142015-10-13 03:58:19 -07001854 playout_ = playout;
1855 return true;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001856}
1857
Taylor Brandstetter1a018dc2016-03-08 12:37:39 -08001858void WebRtcVoiceMediaChannel::SetSend(bool send) {
Peter Boströmca8b4042016-03-08 14:24:13 -08001859 TRACE_EVENT0("webrtc", "WebRtcVoiceMediaChannel::SetSend");
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001860 if (send_ == send) {
Taylor Brandstetter1a018dc2016-03-08 12:37:39 -08001861 return;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001862 }
1863
solenberg246b8172015-12-08 09:50:23 -08001864 // Apply channel specific options when channel is enabled for sending.
Taylor Brandstetter1a018dc2016-03-08 12:37:39 -08001865 if (send) {
solenberg63b34542015-09-29 06:06:31 -07001866 engine()->ApplyOptions(options_);
1867 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001868
wu@webrtc.org9dba5252013-08-05 20:36:57 +00001869 // Change the settings on each send channel.
Taylor Brandstetter1a018dc2016-03-08 12:37:39 -08001870 for (auto& kv : send_streams_) {
1871 kv.second->SetSend(send);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001872 }
wu@webrtc.org9dba5252013-08-05 20:36:57 +00001873
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001874 send_ = send;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001875}
1876
Peter Boström0c4e06b2015-10-07 12:23:21 +02001877bool WebRtcVoiceMediaChannel::SetAudioSend(uint32_t ssrc,
1878 bool enable,
solenberg1dd98f32015-09-10 01:57:14 -07001879 const AudioOptions* options,
Taylor Brandstetter1a018dc2016-03-08 12:37:39 -08001880 AudioSource* source) {
solenberg566ef242015-11-06 15:34:49 -08001881 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
solenberg1dd98f32015-09-10 01:57:14 -07001882 // TODO(solenberg): The state change should be fully rolled back if any one of
1883 // these calls fail.
Taylor Brandstetter1a018dc2016-03-08 12:37:39 -08001884 if (!SetLocalSource(ssrc, source)) {
solenberg1dd98f32015-09-10 01:57:14 -07001885 return false;
1886 }
solenbergdfc8f4f2015-10-01 02:31:10 -07001887 if (!MuteStream(ssrc, !enable)) {
solenberg1dd98f32015-09-10 01:57:14 -07001888 return false;
1889 }
solenbergdfc8f4f2015-10-01 02:31:10 -07001890 if (enable && options) {
solenberg1dd98f32015-09-10 01:57:14 -07001891 return SetOptions(*options);
1892 }
1893 return true;
1894}
1895
solenberg0a617e22015-10-20 15:49:38 -07001896int WebRtcVoiceMediaChannel::CreateVoEChannel() {
1897 int id = engine()->CreateVoEChannel();
1898 if (id == -1) {
1899 LOG_RTCERR0(CreateVoEChannel);
1900 return -1;
wu@webrtc.org9dba5252013-08-05 20:36:57 +00001901 }
solenberg0a617e22015-10-20 15:49:38 -07001902 if (engine()->voe()->network()->RegisterExternalTransport(id, *this) == -1) {
1903 LOG_RTCERR2(RegisterExternalTransport, id, this);
1904 engine()->voe()->base()->DeleteChannel(id);
1905 return -1;
1906 }
1907 return id;
wu@webrtc.org9dba5252013-08-05 20:36:57 +00001908}
1909
solenberg7add0582015-11-20 09:59:34 -08001910bool WebRtcVoiceMediaChannel::DeleteVoEChannel(int channel) {
wu@webrtc.org9dba5252013-08-05 20:36:57 +00001911 if (engine()->voe()->network()->DeRegisterExternalTransport(channel) == -1) {
1912 LOG_RTCERR1(DeRegisterExternalTransport, channel);
1913 }
wu@webrtc.org9dba5252013-08-05 20:36:57 +00001914 if (engine()->voe()->base()->DeleteChannel(channel) == -1) {
1915 LOG_RTCERR1(DeleteChannel, channel);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001916 return false;
1917 }
wu@webrtc.org9dba5252013-08-05 20:36:57 +00001918 return true;
1919}
henrike@webrtc.org1e09a712013-07-26 19:17:59 +00001920
wu@webrtc.org9dba5252013-08-05 20:36:57 +00001921bool WebRtcVoiceMediaChannel::AddSendStream(const StreamParams& sp) {
Peter Boströmca8b4042016-03-08 14:24:13 -08001922 TRACE_EVENT0("webrtc", "WebRtcVoiceMediaChannel::AddSendStream");
solenberg566ef242015-11-06 15:34:49 -08001923 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
solenberg0a617e22015-10-20 15:49:38 -07001924 LOG(LS_INFO) << "AddSendStream: " << sp.ToString();
1925
1926 uint32_t ssrc = sp.first_ssrc();
1927 RTC_DCHECK(0 != ssrc);
1928
1929 if (GetSendChannelId(ssrc) != -1) {
1930 LOG(LS_ERROR) << "Stream already exists with ssrc " << ssrc;
wu@webrtc.org9dba5252013-08-05 20:36:57 +00001931 return false;
1932 }
1933
solenberg0a617e22015-10-20 15:49:38 -07001934 // Create a new channel for sending audio data.
1935 int channel = CreateVoEChannel();
1936 if (channel == -1) {
1937 return false;
wu@webrtc.org9dba5252013-08-05 20:36:57 +00001938 }
wu@webrtc.org9dba5252013-08-05 20:36:57 +00001939
solenbergc96df772015-10-21 13:01:53 -07001940 // Save the channel to send_streams_, so that RemoveSendStream() can still
wu@webrtc.org9dba5252013-08-05 20:36:57 +00001941 // delete the channel in case failure happens below.
mallinath@webrtc.org67ee6b92014-02-03 16:57:16 +00001942 webrtc::AudioTransport* audio_transport =
1943 engine()->voe()->base()->audio_transport();
skvlade0d46372016-04-07 22:59:22 -07001944 WebRtcAudioSendStream* stream = new WebRtcAudioSendStream(
1945 channel, audio_transport, ssrc, sp.cname, send_rtp_extensions_, call_);
1946 send_streams_.insert(std::make_pair(ssrc, stream));
wu@webrtc.org9dba5252013-08-05 20:36:57 +00001947
solenberg0a617e22015-10-20 15:49:38 -07001948 // Set the current codecs to be used for the new channel. We need to do this
1949 // after adding the channel to send_channels_, because of how max bitrate is
1950 // currently being configured by SetSendCodec().
skvlade0d46372016-04-07 22:59:22 -07001951 if (HasSendCodec() && !SetSendCodecs(channel, stream->rtp_parameters())) {
solenberg0a617e22015-10-20 15:49:38 -07001952 RemoveSendStream(ssrc);
wu@webrtc.org9dba5252013-08-05 20:36:57 +00001953 return false;
1954 }
1955
1956 // At this point the channel's local SSRC has been updated. If the channel is
solenberg0a617e22015-10-20 15:49:38 -07001957 // the first send channel make sure that all the receive channels are updated
1958 // with the same SSRC in order to send receiver reports.
solenbergc96df772015-10-21 13:01:53 -07001959 if (send_streams_.size() == 1) {
solenberg0a617e22015-10-20 15:49:38 -07001960 receiver_reports_ssrc_ = ssrc;
solenberg7add0582015-11-20 09:59:34 -08001961 for (const auto& stream : recv_streams_) {
1962 int recv_channel = stream.second->channel();
solenberg0a617e22015-10-20 15:49:38 -07001963 if (engine()->voe()->rtp()->SetLocalSSRC(recv_channel, ssrc) != 0) {
solenberg7add0582015-11-20 09:59:34 -08001964 LOG_RTCERR2(SetLocalSSRC, recv_channel, ssrc);
solenberg1ac56142015-10-13 03:58:19 -07001965 return false;
wu@webrtc.org9dba5252013-08-05 20:36:57 +00001966 }
solenberg0a617e22015-10-20 15:49:38 -07001967 engine()->voe()->base()->AssociateSendChannel(recv_channel, channel);
1968 LOG(LS_INFO) << "VoiceEngine channel #" << recv_channel
1969 << " is associated with channel #" << channel << ".";
wu@webrtc.org9dba5252013-08-05 20:36:57 +00001970 }
1971 }
1972
Taylor Brandstetter1a018dc2016-03-08 12:37:39 -08001973 send_streams_[ssrc]->SetSend(send_);
1974 return true;
wu@webrtc.org9dba5252013-08-05 20:36:57 +00001975}
1976
Peter Boström0c4e06b2015-10-07 12:23:21 +02001977bool WebRtcVoiceMediaChannel::RemoveSendStream(uint32_t ssrc) {
Peter Boströmca8b4042016-03-08 14:24:13 -08001978 TRACE_EVENT0("webrtc", "WebRtcVoiceMediaChannel::RemoveSendStream");
solenberg566ef242015-11-06 15:34:49 -08001979 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
solenberg3a941542015-11-16 07:34:50 -08001980 LOG(LS_INFO) << "RemoveSendStream: " << ssrc;
1981
solenbergc96df772015-10-21 13:01:53 -07001982 auto it = send_streams_.find(ssrc);
1983 if (it == send_streams_.end()) {
wu@webrtc.org9dba5252013-08-05 20:36:57 +00001984 LOG(LS_WARNING) << "Try to remove stream with ssrc " << ssrc
1985 << " which doesn't exist.";
1986 return false;
1987 }
1988
Taylor Brandstetter1a018dc2016-03-08 12:37:39 -08001989 it->second->SetSend(false);
wu@webrtc.org9dba5252013-08-05 20:36:57 +00001990
solenberg7add0582015-11-20 09:59:34 -08001991 // Clean up and delete the send stream+channel.
Taylor Brandstetter1a018dc2016-03-08 12:37:39 -08001992 int channel = it->second->channel();
solenberg0a617e22015-10-20 15:49:38 -07001993 LOG(LS_INFO) << "Removing audio send stream " << ssrc
1994 << " with VoiceEngine channel #" << channel << ".";
solenberg7add0582015-11-20 09:59:34 -08001995 delete it->second;
1996 send_streams_.erase(it);
1997 if (!DeleteVoEChannel(channel)) {
solenberg0a617e22015-10-20 15:49:38 -07001998 return false;
wu@webrtc.org9dba5252013-08-05 20:36:57 +00001999 }
solenbergc96df772015-10-21 13:01:53 -07002000 if (send_streams_.empty()) {
Taylor Brandstetter1a018dc2016-03-08 12:37:39 -08002001 SetSend(false);
solenberg0a617e22015-10-20 15:49:38 -07002002 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002003 return true;
2004}
2005
2006bool WebRtcVoiceMediaChannel::AddRecvStream(const StreamParams& sp) {
Peter Boströmca8b4042016-03-08 14:24:13 -08002007 TRACE_EVENT0("webrtc", "WebRtcVoiceMediaChannel::AddRecvStream");
solenberg566ef242015-11-06 15:34:49 -08002008 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
solenbergd97ec302015-10-07 01:40:33 -07002009 LOG(LS_INFO) << "AddRecvStream: " << sp.ToString();
2010
solenberg0b675462015-10-09 01:37:09 -07002011 if (!ValidateStreamParams(sp)) {
wu@webrtc.org78187522013-10-07 23:32:02 +00002012 return false;
2013 }
2014
solenberg7add0582015-11-20 09:59:34 -08002015 const uint32_t ssrc = sp.first_ssrc();
solenberg0b675462015-10-09 01:37:09 -07002016 if (ssrc == 0) {
2017 LOG(LS_WARNING) << "AddRecvStream with ssrc==0 is not supported.";
2018 return false;
2019 }
2020
solenberg1ac56142015-10-13 03:58:19 -07002021 // Remove the default receive stream if one had been created with this ssrc;
2022 // we'll recreate it then.
2023 if (IsDefaultRecvStream(ssrc)) {
2024 RemoveRecvStream(ssrc);
2025 }
solenberg0b675462015-10-09 01:37:09 -07002026
solenberg7add0582015-11-20 09:59:34 -08002027 if (GetReceiveChannelId(ssrc) != -1) {
henrike@webrtc.org1e09a712013-07-26 19:17:59 +00002028 LOG(LS_ERROR) << "Stream already exists with ssrc " << ssrc;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002029 return false;
2030 }
Fredrik Solenberg4b60c732015-05-07 14:07:48 +02002031
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002032 // Create a new channel for receiving audio data.
solenberg7add0582015-11-20 09:59:34 -08002033 const int channel = CreateVoEChannel();
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002034 if (channel == -1) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002035 return false;
2036 }
Minyue2013aec2015-05-13 14:14:42 +02002037
solenberg1ac56142015-10-13 03:58:19 -07002038 // Turn off all supported codecs.
solenberg26c8c912015-11-27 04:00:25 -08002039 // TODO(solenberg): Remove once "no codecs" is the default state of a stream.
2040 for (webrtc::CodecInst voe_codec : webrtc::acm2::RentACodec::Database()) {
2041 voe_codec.pltype = -1;
2042 if (engine()->voe()->codec()->SetRecPayloadType(channel, voe_codec) == -1) {
2043 LOG_RTCERR2(SetRecPayloadType, channel, ToString(voe_codec));
2044 DeleteVoEChannel(channel);
2045 return false;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002046 }
2047 }
2048
solenberg1ac56142015-10-13 03:58:19 -07002049 // Only enable those configured for this channel.
2050 for (const auto& codec : recv_codecs_) {
solenberg72e29d22016-03-08 06:35:16 -08002051 webrtc::CodecInst voe_codec = {0};
solenberg26c8c912015-11-27 04:00:25 -08002052 if (WebRtcVoiceEngine::ToCodecInst(codec, &voe_codec)) {
solenberg1ac56142015-10-13 03:58:19 -07002053 voe_codec.pltype = codec.id;
2054 if (engine()->voe()->codec()->SetRecPayloadType(
2055 channel, voe_codec) == -1) {
2056 LOG_RTCERR2(SetRecPayloadType, channel, ToString(voe_codec));
solenberg7add0582015-11-20 09:59:34 -08002057 DeleteVoEChannel(channel);
solenberg1ac56142015-10-13 03:58:19 -07002058 return false;
2059 }
henrike@webrtc.org1e09a712013-07-26 19:17:59 +00002060 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002061 }
solenberg8fb30c32015-10-13 03:06:58 -07002062
solenberg7add0582015-11-20 09:59:34 -08002063 const int send_channel = GetSendChannelId(receiver_reports_ssrc_);
2064 if (send_channel != -1) {
2065 // Associate receive channel with first send channel (so the receive channel
2066 // can obtain RTT from the send channel)
2067 engine()->voe()->base()->AssociateSendChannel(channel, send_channel);
2068 LOG(LS_INFO) << "VoiceEngine channel #" << channel
2069 << " is associated with channel #" << send_channel << ".";
buildbot@webrtc.org150835e2014-05-06 15:54:38 +00002070 }
2071
stefanba4c0e42016-02-04 04:12:24 -08002072 recv_streams_.insert(std::make_pair(
2073 ssrc, new WebRtcAudioReceiveStream(channel, ssrc, receiver_reports_ssrc_,
solenberg72e29d22016-03-08 06:35:16 -08002074 recv_transport_cc_enabled_,
2075 sp.sync_label, recv_rtp_extensions_,
2076 call_)));
solenberg7add0582015-11-20 09:59:34 -08002077
solenberg72e29d22016-03-08 06:35:16 -08002078 SetNack(channel, send_codec_spec_.nack_enabled);
solenberg1ac56142015-10-13 03:58:19 -07002079 SetPlayout(channel, playout_);
solenberg7add0582015-11-20 09:59:34 -08002080
solenberg1ac56142015-10-13 03:58:19 -07002081 return true;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002082}
2083
Peter Boström0c4e06b2015-10-07 12:23:21 +02002084bool WebRtcVoiceMediaChannel::RemoveRecvStream(uint32_t ssrc) {
Peter Boströmca8b4042016-03-08 14:24:13 -08002085 TRACE_EVENT0("webrtc", "WebRtcVoiceMediaChannel::RemoveRecvStream");
solenberg566ef242015-11-06 15:34:49 -08002086 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
solenbergd97ec302015-10-07 01:40:33 -07002087 LOG(LS_INFO) << "RemoveRecvStream: " << ssrc;
2088
solenberg7add0582015-11-20 09:59:34 -08002089 const auto it = recv_streams_.find(ssrc);
2090 if (it == recv_streams_.end()) {
wu@webrtc.org9dba5252013-08-05 20:36:57 +00002091 LOG(LS_WARNING) << "Try to remove stream with ssrc " << ssrc
2092 << " which doesn't exist.";
henrike@webrtc.org1e09a712013-07-26 19:17:59 +00002093 return false;
wu@webrtc.org9dba5252013-08-05 20:36:57 +00002094 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002095
solenberg1ac56142015-10-13 03:58:19 -07002096 // Deregister default channel, if that's the one being destroyed.
2097 if (IsDefaultRecvStream(ssrc)) {
2098 default_recv_ssrc_ = -1;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002099 }
henrike@webrtc.org1e09a712013-07-26 19:17:59 +00002100
solenberg7add0582015-11-20 09:59:34 -08002101 const int channel = it->second->channel();
2102
2103 // Clean up and delete the receive stream+channel.
2104 LOG(LS_INFO) << "Removing audio receive stream " << ssrc
mallinath@webrtc.org67ee6b92014-02-03 16:57:16 +00002105 << " with VoiceEngine channel #" << channel << ".";
Tommif888bb52015-12-12 01:37:01 +01002106 it->second->SetRawAudioSink(nullptr);
solenberg7add0582015-11-20 09:59:34 -08002107 delete it->second;
2108 recv_streams_.erase(it);
2109 return DeleteVoEChannel(channel);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002110}
2111
Taylor Brandstetter1a018dc2016-03-08 12:37:39 -08002112bool WebRtcVoiceMediaChannel::SetLocalSource(uint32_t ssrc,
2113 AudioSource* source) {
solenbergc96df772015-10-21 13:01:53 -07002114 auto it = send_streams_.find(ssrc);
2115 if (it == send_streams_.end()) {
Taylor Brandstetter1a018dc2016-03-08 12:37:39 -08002116 if (source) {
2117 // Return an error if trying to set a valid source with an invalid ssrc.
2118 LOG(LS_ERROR) << "SetLocalSource failed with ssrc " << ssrc;
wu@webrtc.org9dba5252013-08-05 20:36:57 +00002119 return false;
2120 }
2121
2122 // The channel likely has gone away, do nothing.
henrike@webrtc.org1e09a712013-07-26 19:17:59 +00002123 return true;
henrike@webrtc.org1e09a712013-07-26 19:17:59 +00002124 }
2125
Taylor Brandstetter1a018dc2016-03-08 12:37:39 -08002126 if (source) {
2127 it->second->SetSource(source);
solenberg1ac56142015-10-13 03:58:19 -07002128 } else {
Taylor Brandstetter1a018dc2016-03-08 12:37:39 -08002129 it->second->ClearSource();
solenberg1ac56142015-10-13 03:58:19 -07002130 }
henrike@webrtc.org1e09a712013-07-26 19:17:59 +00002131
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002132 return true;
2133}
2134
2135bool WebRtcVoiceMediaChannel::GetActiveStreams(
2136 AudioInfo::StreamList* actives) {
solenberg566ef242015-11-06 15:34:49 -08002137 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002138 actives->clear();
solenberg7add0582015-11-20 09:59:34 -08002139 for (const auto& ch : recv_streams_) {
Fredrik Solenbergaf9fb212015-08-26 10:45:53 +02002140 int level = GetOutputLevel(ch.second->channel());
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002141 if (level > 0) {
Fredrik Solenbergaf9fb212015-08-26 10:45:53 +02002142 actives->push_back(std::make_pair(ch.first, level));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002143 }
2144 }
2145 return true;
2146}
2147
2148int WebRtcVoiceMediaChannel::GetOutputLevel() {
solenberg566ef242015-11-06 15:34:49 -08002149 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
solenberg1ac56142015-10-13 03:58:19 -07002150 int highest = 0;
solenberg7add0582015-11-20 09:59:34 -08002151 for (const auto& ch : recv_streams_) {
solenberg8fb30c32015-10-13 03:06:58 -07002152 highest = std::max(GetOutputLevel(ch.second->channel()), highest);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002153 }
2154 return highest;
2155}
2156
2157int WebRtcVoiceMediaChannel::GetTimeSinceLastTyping() {
2158 int ret;
2159 if (engine()->voe()->processing()->TimeSinceLastTyping(ret) == -1) {
2160 // In case of error, log the info and continue
2161 LOG_RTCERR0(TimeSinceLastTyping);
2162 ret = -1;
2163 } else {
2164 ret *= 1000; // We return ms, webrtc returns seconds.
2165 }
2166 return ret;
2167}
2168
2169void WebRtcVoiceMediaChannel::SetTypingDetectionParameters(int time_window,
2170 int cost_per_typing, int reporting_threshold, int penalty_decay,
2171 int type_event_delay) {
2172 if (engine()->voe()->processing()->SetTypingDetectionParameters(
2173 time_window, cost_per_typing,
2174 reporting_threshold, penalty_decay, type_event_delay) == -1) {
2175 // In case of error, log the info and continue
2176 LOG_RTCERR5(SetTypingDetectionParameters, time_window,
2177 cost_per_typing, reporting_threshold, penalty_decay,
2178 type_event_delay);
2179 }
2180}
2181
solenberg4bac9c52015-10-09 02:32:53 -07002182bool WebRtcVoiceMediaChannel::SetOutputVolume(uint32_t ssrc, double volume) {
solenberg566ef242015-11-06 15:34:49 -08002183 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
solenberg1ac56142015-10-13 03:58:19 -07002184 if (ssrc == 0) {
2185 default_recv_volume_ = volume;
2186 if (default_recv_ssrc_ == -1) {
2187 return true;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002188 }
solenberg1ac56142015-10-13 03:58:19 -07002189 ssrc = static_cast<uint32_t>(default_recv_ssrc_);
2190 }
2191 int ch_id = GetReceiveChannelId(ssrc);
2192 if (ch_id < 0) {
2193 LOG(LS_WARNING) << "Cannot find channel for ssrc:" << ssrc;
2194 return false;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002195 }
2196
solenberg1ac56142015-10-13 03:58:19 -07002197 if (-1 == engine()->voe()->volume()->SetChannelOutputVolumeScaling(ch_id,
2198 volume)) {
2199 LOG_RTCERR2(SetChannelOutputVolumeScaling, ch_id, volume);
2200 return false;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002201 }
solenberg1ac56142015-10-13 03:58:19 -07002202 LOG(LS_INFO) << "SetOutputVolume to " << volume
2203 << " for channel " << ch_id << " and ssrc " << ssrc;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002204 return true;
2205}
2206
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002207bool WebRtcVoiceMediaChannel::CanInsertDtmf() {
Fredrik Solenbergb5727682015-12-04 15:22:19 +01002208 return dtmf_payload_type_ ? true : false;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002209}
2210
solenberg1d63dd02015-12-02 12:35:09 -08002211bool WebRtcVoiceMediaChannel::InsertDtmf(uint32_t ssrc, int event,
2212 int duration) {
solenberg566ef242015-11-06 15:34:49 -08002213 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
Fredrik Solenbergb5727682015-12-04 15:22:19 +01002214 LOG(LS_INFO) << "WebRtcVoiceMediaChannel::InsertDtmf";
2215 if (!dtmf_payload_type_) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002216 return false;
2217 }
2218
Fredrik Solenbergb5727682015-12-04 15:22:19 +01002219 // Figure out which WebRtcAudioSendStream to send the event on.
2220 auto it = ssrc != 0 ? send_streams_.find(ssrc) : send_streams_.begin();
2221 if (it == send_streams_.end()) {
2222 LOG(LS_WARNING) << "The specified ssrc " << ssrc << " is not in use.";
solenberg1d63dd02015-12-02 12:35:09 -08002223 return false;
2224 }
Fredrik Solenbergb5727682015-12-04 15:22:19 +01002225 if (event < kMinTelephoneEventCode ||
2226 event > kMaxTelephoneEventCode) {
2227 LOG(LS_WARNING) << "DTMF event code " << event << " out of range.";
solenberg1d63dd02015-12-02 12:35:09 -08002228 return false;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002229 }
Fredrik Solenbergb5727682015-12-04 15:22:19 +01002230 if (duration < kMinTelephoneEventDuration ||
2231 duration > kMaxTelephoneEventDuration) {
2232 LOG(LS_WARNING) << "DTMF event duration " << duration << " out of range.";
2233 return false;
2234 }
2235 return it->second->SendTelephoneEvent(*dtmf_payload_type_, event, duration);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002236}
2237
wu@webrtc.orga9890802013-12-13 00:21:03 +00002238void WebRtcVoiceMediaChannel::OnPacketReceived(
jbaucheec21bd2016-03-20 06:15:43 -07002239 rtc::CopyOnWriteBuffer* packet, const rtc::PacketTime& packet_time) {
solenberg566ef242015-11-06 15:34:49 -08002240 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
Fredrik Solenberg4b60c732015-05-07 14:07:48 +02002241
solenberg1ac56142015-10-13 03:58:19 -07002242 uint32_t ssrc = 0;
jbaucheec21bd2016-03-20 06:15:43 -07002243 if (!GetRtpSsrc(packet->cdata(), packet->size(), &ssrc)) {
solenberg1ac56142015-10-13 03:58:19 -07002244 return;
2245 }
2246
solenberg7e63ef02015-11-20 00:19:43 -08002247 // If we don't have a default channel, and the SSRC is unknown, create a
2248 // default channel.
2249 if (default_recv_ssrc_ == -1 && GetReceiveChannelId(ssrc) == -1) {
solenberg1ac56142015-10-13 03:58:19 -07002250 StreamParams sp;
2251 sp.ssrcs.push_back(ssrc);
2252 LOG(LS_INFO) << "Creating default receive stream for SSRC=" << ssrc << ".";
2253 if (!AddRecvStream(sp)) {
2254 LOG(LS_WARNING) << "Could not create default receive stream.";
2255 return;
2256 }
2257 default_recv_ssrc_ = ssrc;
2258 SetOutputVolume(default_recv_ssrc_, default_recv_volume_);
deadbeef884f5852016-01-15 09:20:04 -08002259 if (default_sink_) {
kwiberg686a8ef2016-02-26 03:00:35 -08002260 std::unique_ptr<webrtc::AudioSinkInterface> proxy_sink(
deadbeef884f5852016-01-15 09:20:04 -08002261 new ProxySink(default_sink_.get()));
2262 SetRawAudioSink(default_recv_ssrc_, std::move(proxy_sink));
2263 }
solenberg1ac56142015-10-13 03:58:19 -07002264 }
2265
2266 // Forward packet to Call. If the SSRC is unknown we'll return after this.
Fredrik Solenberg709ed672015-09-15 12:26:33 +02002267 const webrtc::PacketTime webrtc_packet_time(packet_time.timestamp,
2268 packet_time.not_before);
solenberg1ac56142015-10-13 03:58:19 -07002269 webrtc::PacketReceiver::DeliveryStatus delivery_result =
2270 call_->Receiver()->DeliverPacket(webrtc::MediaType::AUDIO,
jbaucheec21bd2016-03-20 06:15:43 -07002271 packet->cdata(), packet->size(), webrtc_packet_time);
solenberg1ac56142015-10-13 03:58:19 -07002272 if (webrtc::PacketReceiver::DELIVERY_OK != delivery_result) {
solenberg7e63ef02015-11-20 00:19:43 -08002273 // If the SSRC is unknown here, route it to the default channel, if we have
2274 // one. See: https://bugs.chromium.org/p/webrtc/issues/detail?id=5208
2275 if (default_recv_ssrc_ == -1) {
2276 return;
2277 } else {
2278 ssrc = default_recv_ssrc_;
2279 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002280 }
2281
solenberg1ac56142015-10-13 03:58:19 -07002282 // Find the channel to send this packet to. It must exist since webrtc::Call
2283 // was able to demux the packet.
2284 int channel = GetReceiveChannelId(ssrc);
2285 RTC_DCHECK(channel != -1);
2286
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002287 // Pass it off to the decoder.
henrike@webrtc.org28654cb2013-07-22 21:07:49 +00002288 engine()->voe()->network()->ReceivedRTPPacket(
jbaucheec21bd2016-03-20 06:15:43 -07002289 channel, packet->cdata(), packet->size(), webrtc_packet_time);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002290}
2291
wu@webrtc.orga9890802013-12-13 00:21:03 +00002292void WebRtcVoiceMediaChannel::OnRtcpReceived(
jbaucheec21bd2016-03-20 06:15:43 -07002293 rtc::CopyOnWriteBuffer* packet, const rtc::PacketTime& packet_time) {
solenberg566ef242015-11-06 15:34:49 -08002294 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
Fredrik Solenberg4b60c732015-05-07 14:07:48 +02002295
Fredrik Solenberg709ed672015-09-15 12:26:33 +02002296 // Forward packet to Call as well.
2297 const webrtc::PacketTime webrtc_packet_time(packet_time.timestamp,
2298 packet_time.not_before);
2299 call_->Receiver()->DeliverPacket(webrtc::MediaType::AUDIO,
jbaucheec21bd2016-03-20 06:15:43 -07002300 packet->cdata(), packet->size(), webrtc_packet_time);
Fredrik Solenberg4b60c732015-05-07 14:07:48 +02002301
wu@webrtc.org9dba5252013-08-05 20:36:57 +00002302 // Sending channels need all RTCP packets with feedback information.
2303 // Even sender reports can contain attached report blocks.
2304 // Receiving channels need sender reports in order to create
2305 // correct receiver reports.
2306 int type = 0;
jbaucheec21bd2016-03-20 06:15:43 -07002307 if (!GetRtcpType(packet->cdata(), packet->size(), &type)) {
wu@webrtc.org9dba5252013-08-05 20:36:57 +00002308 LOG(LS_WARNING) << "Failed to parse type from received RTCP packet";
2309 return;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002310 }
2311
solenberg0b675462015-10-09 01:37:09 -07002312 // If it is a sender report, find the receive channel that is listening.
wu@webrtc.org9dba5252013-08-05 20:36:57 +00002313 if (type == kRtcpTypeSR) {
solenberg0b675462015-10-09 01:37:09 -07002314 uint32_t ssrc = 0;
jbaucheec21bd2016-03-20 06:15:43 -07002315 if (!GetRtcpSsrc(packet->cdata(), packet->size(), &ssrc)) {
solenberg0b675462015-10-09 01:37:09 -07002316 return;
2317 }
2318 int recv_channel_id = GetReceiveChannelId(ssrc);
2319 if (recv_channel_id != -1) {
wu@webrtc.org9dba5252013-08-05 20:36:57 +00002320 engine()->voe()->network()->ReceivedRTCPPacket(
jbaucheec21bd2016-03-20 06:15:43 -07002321 recv_channel_id, packet->cdata(), packet->size());
wu@webrtc.org9dba5252013-08-05 20:36:57 +00002322 }
2323 }
2324
2325 // SR may continue RR and any RR entry may correspond to any one of the send
2326 // channels. So all RTCP packets must be forwarded all send channels. VoE
2327 // will filter out RR internally.
solenbergc96df772015-10-21 13:01:53 -07002328 for (const auto& ch : send_streams_) {
wu@webrtc.org9dba5252013-08-05 20:36:57 +00002329 engine()->voe()->network()->ReceivedRTCPPacket(
jbaucheec21bd2016-03-20 06:15:43 -07002330 ch.second->channel(), packet->cdata(), packet->size());
wu@webrtc.org9dba5252013-08-05 20:36:57 +00002331 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002332}
2333
Honghai Zhangcc411c02016-03-29 17:27:21 -07002334void WebRtcVoiceMediaChannel::OnNetworkRouteChanged(
2335 const std::string& transport_name,
2336 const NetworkRoute& network_route) {
2337 // TODO(honghaiz): uncomment this once the function in call is implemented.
2338 // call_->OnNetworkRouteChanged(transport_name, network_route);
2339}
2340
Peter Boström0c4e06b2015-10-07 12:23:21 +02002341bool WebRtcVoiceMediaChannel::MuteStream(uint32_t ssrc, bool muted) {
solenberg566ef242015-11-06 15:34:49 -08002342 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
solenberg0a617e22015-10-20 15:49:38 -07002343 int channel = GetSendChannelId(ssrc);
wu@webrtc.org9dba5252013-08-05 20:36:57 +00002344 if (channel == -1) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002345 LOG(LS_WARNING) << "The specified ssrc " << ssrc << " is not in use.";
2346 return false;
2347 }
wu@webrtc.org9dba5252013-08-05 20:36:57 +00002348 if (engine()->voe()->volume()->SetInputMute(channel, muted) == -1) {
2349 LOG_RTCERR2(SetInputMute, channel, muted);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002350 return false;
2351 }
buildbot@webrtc.org6b21b712014-07-31 15:08:53 +00002352 // We set the AGC to mute state only when all the channels are muted.
2353 // This implementation is not ideal, instead we should signal the AGC when
2354 // the mic channel is muted/unmuted. We can't do it today because there
2355 // is no good way to know which stream is mapping to the mic channel.
2356 bool all_muted = muted;
solenbergc96df772015-10-21 13:01:53 -07002357 for (const auto& ch : send_streams_) {
Fredrik Solenbergaf9fb212015-08-26 10:45:53 +02002358 if (!all_muted) {
2359 break;
2360 }
2361 if (engine()->voe()->volume()->GetInputMute(ch.second->channel(),
buildbot@webrtc.org6b21b712014-07-31 15:08:53 +00002362 all_muted)) {
Fredrik Solenbergaf9fb212015-08-26 10:45:53 +02002363 LOG_RTCERR1(GetInputMute, ch.second->channel());
buildbot@webrtc.org6b21b712014-07-31 15:08:53 +00002364 return false;
2365 }
2366 }
2367
2368 webrtc::AudioProcessing* ap = engine()->voe()->base()->audio_processing();
solenberg0a617e22015-10-20 15:49:38 -07002369 if (ap) {
buildbot@webrtc.org6b21b712014-07-31 15:08:53 +00002370 ap->set_output_will_be_muted(all_muted);
solenberg0a617e22015-10-20 15:49:38 -07002371 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002372 return true;
2373}
2374
skvlade0d46372016-04-07 22:59:22 -07002375bool WebRtcVoiceMediaChannel::SetSendBitrate(int bps) {
2376 LOG(LS_INFO) << "WebRtcVoiceMediaChannel::SetSendBitrate.";
2377 send_bitrate_bps_ = bps;
2378
2379 for (const auto& kv : send_streams_) {
2380 if (!SetChannelParameters(kv.second->channel(),
2381 kv.second->rtp_parameters())) {
2382 return false;
2383 }
2384 }
2385 return true;
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00002386}
2387
skvlade0d46372016-04-07 22:59:22 -07002388bool WebRtcVoiceMediaChannel::SetChannelParameters(
2389 int channel,
2390 const webrtc::RtpParameters& parameters) {
2391 RTC_CHECK_EQ(1UL, parameters.encodings.size());
2392 return SetSendBitrate(
2393 channel,
2394 MinPositive(send_bitrate_bps_, parameters.encodings[0].max_bitrate_bps));
2395}
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00002396
skvlade0d46372016-04-07 22:59:22 -07002397bool WebRtcVoiceMediaChannel::SetSendBitrate(int channel, int bps) {
2398 // Bitrate is auto by default.
2399 // TODO(bemasc): Fix this so that if SetMaxSendBandwidth(50) is followed by
2400 // SetMaxSendBandwith(0), the second call removes the previous limit.
2401 if (bps <= 0)
2402 return true;
wu@webrtc.org1d1ffc92013-10-16 18:12:02 +00002403
solenberg72e29d22016-03-08 06:35:16 -08002404 if (!HasSendCodec()) {
wu@webrtc.org1d1ffc92013-10-16 18:12:02 +00002405 LOG(LS_INFO) << "The send codec has not been set up yet. "
minyue@webrtc.org26236952014-10-29 02:27:08 +00002406 << "The send bitrate setting will be applied later.";
wu@webrtc.org1d1ffc92013-10-16 18:12:02 +00002407 return true;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002408 }
2409
solenberg72e29d22016-03-08 06:35:16 -08002410 webrtc::CodecInst codec = send_codec_spec_.codec_inst;
solenberg26c8c912015-11-27 04:00:25 -08002411 bool is_multi_rate = WebRtcVoiceCodecs::IsCodecMultiRate(codec);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002412
2413 if (is_multi_rate) {
2414 // If codec is multi-rate then just set the bitrate.
2415 codec.rate = bps;
skvlade0d46372016-04-07 22:59:22 -07002416 if (!SetSendCodec(channel, codec)) {
2417 LOG(LS_INFO) << "Failed to set codec " << codec.plname << " to bitrate "
2418 << bps << " bps.";
2419 return false;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002420 }
2421 return true;
2422 } else {
2423 // If codec is not multi-rate and |bps| is less than the fixed bitrate
2424 // then fail. If codec is not multi-rate and |bps| exceeds or equal the
2425 // fixed bitrate then ignore.
2426 if (bps < codec.rate) {
2427 LOG(LS_INFO) << "Failed to set codec " << codec.plname
2428 << " to bitrate " << bps << " bps"
2429 << ", requires at least " << codec.rate << " bps.";
2430 return false;
2431 }
2432 return true;
2433 }
2434}
2435
skvlad7a43d252016-03-22 15:32:27 -07002436void WebRtcVoiceMediaChannel::OnReadyToSend(bool ready) {
2437 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
2438 LOG(LS_VERBOSE) << "OnReadyToSend: " << (ready ? "Ready." : "Not ready.");
2439 call_->SignalChannelNetworkState(
2440 webrtc::MediaType::AUDIO,
2441 ready ? webrtc::kNetworkUp : webrtc::kNetworkDown);
2442}
2443
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002444bool WebRtcVoiceMediaChannel::GetStats(VoiceMediaInfo* info) {
Peter Boströmca8b4042016-03-08 14:24:13 -08002445 TRACE_EVENT0("webrtc", "WebRtcVoiceMediaChannel::GetStats");
solenberg566ef242015-11-06 15:34:49 -08002446 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
solenberg85a04962015-10-27 03:35:21 -07002447 RTC_DCHECK(info);
solenbergd97ec302015-10-07 01:40:33 -07002448
solenberg85a04962015-10-27 03:35:21 -07002449 // Get SSRC and stats for each sender.
2450 RTC_DCHECK(info->senders.size() == 0);
2451 for (const auto& stream : send_streams_) {
2452 webrtc::AudioSendStream::Stats stats = stream.second->GetStats();
wu@webrtc.org9dba5252013-08-05 20:36:57 +00002453 VoiceSenderInfo sinfo;
solenberg85a04962015-10-27 03:35:21 -07002454 sinfo.add_ssrc(stats.local_ssrc);
2455 sinfo.bytes_sent = stats.bytes_sent;
2456 sinfo.packets_sent = stats.packets_sent;
2457 sinfo.packets_lost = stats.packets_lost;
2458 sinfo.fraction_lost = stats.fraction_lost;
2459 sinfo.codec_name = stats.codec_name;
2460 sinfo.ext_seqnum = stats.ext_seqnum;
2461 sinfo.jitter_ms = stats.jitter_ms;
2462 sinfo.rtt_ms = stats.rtt_ms;
2463 sinfo.audio_level = stats.audio_level;
2464 sinfo.aec_quality_min = stats.aec_quality_min;
2465 sinfo.echo_delay_median_ms = stats.echo_delay_median_ms;
2466 sinfo.echo_delay_std_ms = stats.echo_delay_std_ms;
2467 sinfo.echo_return_loss = stats.echo_return_loss;
2468 sinfo.echo_return_loss_enhancement = stats.echo_return_loss_enhancement;
Taylor Brandstetter1a018dc2016-03-08 12:37:39 -08002469 sinfo.typing_noise_detected = (send_ ? stats.typing_noise_detected : false);
wu@webrtc.org9dba5252013-08-05 20:36:57 +00002470 info->senders.push_back(sinfo);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002471 }
2472
solenberg85a04962015-10-27 03:35:21 -07002473 // Get SSRC and stats for each receiver.
2474 RTC_DCHECK(info->receivers.size() == 0);
solenberg7add0582015-11-20 09:59:34 -08002475 for (const auto& stream : recv_streams_) {
Fredrik Solenberg4f4ec0a2015-10-22 10:49:27 +02002476 webrtc::AudioReceiveStream::Stats stats = stream.second->GetStats();
2477 VoiceReceiverInfo rinfo;
2478 rinfo.add_ssrc(stats.remote_ssrc);
2479 rinfo.bytes_rcvd = stats.bytes_rcvd;
2480 rinfo.packets_rcvd = stats.packets_rcvd;
2481 rinfo.packets_lost = stats.packets_lost;
2482 rinfo.fraction_lost = stats.fraction_lost;
2483 rinfo.codec_name = stats.codec_name;
2484 rinfo.ext_seqnum = stats.ext_seqnum;
2485 rinfo.jitter_ms = stats.jitter_ms;
2486 rinfo.jitter_buffer_ms = stats.jitter_buffer_ms;
2487 rinfo.jitter_buffer_preferred_ms = stats.jitter_buffer_preferred_ms;
2488 rinfo.delay_estimate_ms = stats.delay_estimate_ms;
2489 rinfo.audio_level = stats.audio_level;
2490 rinfo.expand_rate = stats.expand_rate;
2491 rinfo.speech_expand_rate = stats.speech_expand_rate;
2492 rinfo.secondary_decoded_rate = stats.secondary_decoded_rate;
2493 rinfo.accelerate_rate = stats.accelerate_rate;
2494 rinfo.preemptive_expand_rate = stats.preemptive_expand_rate;
2495 rinfo.decoding_calls_to_silence_generator =
2496 stats.decoding_calls_to_silence_generator;
2497 rinfo.decoding_calls_to_neteq = stats.decoding_calls_to_neteq;
2498 rinfo.decoding_normal = stats.decoding_normal;
2499 rinfo.decoding_plc = stats.decoding_plc;
2500 rinfo.decoding_cng = stats.decoding_cng;
2501 rinfo.decoding_plc_cng = stats.decoding_plc_cng;
2502 rinfo.capture_start_ntp_time_ms = stats.capture_start_ntp_time_ms;
2503 info->receivers.push_back(rinfo);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002504 }
2505
2506 return true;
2507}
2508
Tommif888bb52015-12-12 01:37:01 +01002509void WebRtcVoiceMediaChannel::SetRawAudioSink(
2510 uint32_t ssrc,
kwiberg686a8ef2016-02-26 03:00:35 -08002511 std::unique_ptr<webrtc::AudioSinkInterface> sink) {
Tommif888bb52015-12-12 01:37:01 +01002512 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
deadbeef884f5852016-01-15 09:20:04 -08002513 LOG(LS_VERBOSE) << "WebRtcVoiceMediaChannel::SetRawAudioSink: ssrc:" << ssrc
2514 << " " << (sink ? "(ptr)" : "NULL");
2515 if (ssrc == 0) {
2516 if (default_recv_ssrc_ != -1) {
kwiberg686a8ef2016-02-26 03:00:35 -08002517 std::unique_ptr<webrtc::AudioSinkInterface> proxy_sink(
deadbeef884f5852016-01-15 09:20:04 -08002518 sink ? new ProxySink(sink.get()) : nullptr);
2519 SetRawAudioSink(default_recv_ssrc_, std::move(proxy_sink));
2520 }
2521 default_sink_ = std::move(sink);
2522 return;
2523 }
Tommif888bb52015-12-12 01:37:01 +01002524 const auto it = recv_streams_.find(ssrc);
2525 if (it == recv_streams_.end()) {
2526 LOG(LS_WARNING) << "SetRawAudioSink: no recv stream" << ssrc;
2527 return;
2528 }
deadbeef2d110be2016-01-13 12:00:26 -08002529 it->second->SetRawAudioSink(std::move(sink));
Tommif888bb52015-12-12 01:37:01 +01002530}
2531
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002532int WebRtcVoiceMediaChannel::GetOutputLevel(int channel) {
solenbergd97ec302015-10-07 01:40:33 -07002533 unsigned int ulevel = 0;
2534 int ret = engine()->voe()->volume()->GetSpeechOutputLevel(channel, ulevel);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002535 return (ret == 0) ? static_cast<int>(ulevel) : -1;
2536}
2537
Peter Boström0c4e06b2015-10-07 12:23:21 +02002538int WebRtcVoiceMediaChannel::GetReceiveChannelId(uint32_t ssrc) const {
solenberg566ef242015-11-06 15:34:49 -08002539 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
solenberg7add0582015-11-20 09:59:34 -08002540 const auto it = recv_streams_.find(ssrc);
2541 if (it != recv_streams_.end()) {
mallinath@webrtc.org67ee6b92014-02-03 16:57:16 +00002542 return it->second->channel();
solenberg8fb30c32015-10-13 03:06:58 -07002543 }
solenberg1ac56142015-10-13 03:58:19 -07002544 return -1;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002545}
2546
Peter Boström0c4e06b2015-10-07 12:23:21 +02002547int WebRtcVoiceMediaChannel::GetSendChannelId(uint32_t ssrc) const {
solenberg566ef242015-11-06 15:34:49 -08002548 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
solenbergc96df772015-10-21 13:01:53 -07002549 const auto it = send_streams_.find(ssrc);
2550 if (it != send_streams_.end()) {
mallinath@webrtc.org67ee6b92014-02-03 16:57:16 +00002551 return it->second->channel();
solenberg8fb30c32015-10-13 03:06:58 -07002552 }
wu@webrtc.org9dba5252013-08-05 20:36:57 +00002553 return -1;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002554}
2555
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002556bool WebRtcVoiceMediaChannel::SetPlayout(int channel, bool playout) {
2557 if (playout) {
2558 LOG(LS_INFO) << "Starting playout for channel #" << channel;
2559 if (engine()->voe()->base()->StartPlayout(channel) == -1) {
2560 LOG_RTCERR1(StartPlayout, channel);
2561 return false;
2562 }
2563 } else {
2564 LOG(LS_INFO) << "Stopping playout for channel #" << channel;
2565 engine()->voe()->base()->StopPlayout(channel);
2566 }
2567 return true;
2568}
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002569} // namespace cricket
2570
2571#endif // HAVE_WEBRTC_VOICE