blob: 97e955f51edcbb61f1d2acb2d38569cbe3abfd53 [file] [log] [blame]
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001/*
kjellander1afca732016-02-07 20:46:45 -08002 * Copyright (c) 2004 The WebRTC project authors. All Rights Reserved.
henrike@webrtc.org28e20752013-07-10 00:45:36 +00003 *
kjellander1afca732016-02-07 20:46:45 -08004 * Use of this source code is governed by a BSD-style license
5 * that can be found in the LICENSE file in the root of the source
6 * tree. An additional intellectual property rights grant can be found
7 * in the file PATENTS. All contributing project authors may
8 * be found in the AUTHORS file in the root of the source tree.
henrike@webrtc.org28e20752013-07-10 00:45:36 +00009 */
10
henrike@webrtc.org28e20752013-07-10 00:45:36 +000011#ifdef HAVE_WEBRTC_VOICE
12
kjellander@webrtc.org5ad12972016-02-12 06:39:40 +010013#include "webrtc/media/engine/webrtcvoiceengine.h"
henrike@webrtc.org28e20752013-07-10 00:45:36 +000014
15#include <algorithm>
16#include <cstdio>
17#include <string>
18#include <vector>
19
kjellander@webrtc.org7ffeab52016-02-26 22:46:09 +010020#include "webrtc/audio_sink.h"
tfarina5237aaf2015-11-10 23:44:30 -080021#include "webrtc/base/arraysize.h"
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +000022#include "webrtc/base/base64.h"
23#include "webrtc/base/byteorder.h"
24#include "webrtc/base/common.h"
25#include "webrtc/base/helpers.h"
26#include "webrtc/base/logging.h"
27#include "webrtc/base/stringencode.h"
28#include "webrtc/base/stringutils.h"
Peter Boströmca8b4042016-03-08 14:24:13 -080029#include "webrtc/base/trace_event.h"
ivoc112a3d82015-10-16 02:22:18 -070030#include "webrtc/call/rtc_event_log.h"
mallinath@webrtc.orga27be8e2013-09-27 23:04:10 +000031#include "webrtc/common.h"
kjellandera96e2d72016-02-04 23:52:28 -080032#include "webrtc/media/base/audioframe.h"
Taylor Brandstetter1a018dc2016-03-08 12:37:39 -080033#include "webrtc/media/base/audiosource.h"
kjellanderf4752772016-03-02 05:42:30 -080034#include "webrtc/media/base/mediaconstants.h"
kjellandera96e2d72016-02-04 23:52:28 -080035#include "webrtc/media/base/streamparams.h"
kjellander@webrtc.org5ad12972016-02-12 06:39:40 +010036#include "webrtc/media/engine/webrtcmediaengine.h"
37#include "webrtc/media/engine/webrtcvoe.h"
solenberg26c8c912015-11-27 04:00:25 -080038#include "webrtc/modules/audio_coding/acm2/rent_a_codec.h"
henrike@webrtc.org28e20752013-07-10 00:45:36 +000039#include "webrtc/modules/audio_processing/include/audio_processing.h"
Henrik Kjellander98f53512015-10-28 18:17:40 +010040#include "webrtc/system_wrappers/include/field_trial.h"
solenbergbd138382015-11-20 16:08:07 -080041#include "webrtc/system_wrappers/include/trace.h"
henrike@webrtc.org28e20752013-07-10 00:45:36 +000042
henrike@webrtc.org28e20752013-07-10 00:45:36 +000043namespace cricket {
solenbergd97ec302015-10-07 01:40:33 -070044namespace {
henrike@webrtc.org28e20752013-07-10 00:45:36 +000045
solenbergbd138382015-11-20 16:08:07 -080046const int kDefaultTraceFilter = webrtc::kTraceNone | webrtc::kTraceTerseInfo |
47 webrtc::kTraceWarning | webrtc::kTraceError |
48 webrtc::kTraceCritical;
49const int kElevatedTraceFilter = kDefaultTraceFilter | webrtc::kTraceStateInfo |
50 webrtc::kTraceInfo;
51
henrike@webrtc.org28e20752013-07-10 00:45:36 +000052// On Windows Vista and newer, Microsoft introduced the concept of "Default
53// Communications Device". This means that there are two types of default
54// devices (old Wave Audio style default and Default Communications Device).
55//
56// On Windows systems which only support Wave Audio style default, uses either
57// -1 or 0 to select the default device.
henrike@webrtc.org28e20752013-07-10 00:45:36 +000058#ifdef WIN32
solenbergd97ec302015-10-07 01:40:33 -070059const int kDefaultAudioDeviceId = -1;
solenberg8ad582d2016-03-16 09:34:56 -070060#elif !defined(WEBRTC_IOS)
solenbergd97ec302015-10-07 01:40:33 -070061const int kDefaultAudioDeviceId = 0;
henrike@webrtc.org28e20752013-07-10 00:45:36 +000062#endif
63
henrike@webrtc.org28e20752013-07-10 00:45:36 +000064// Parameter used for NACK.
65// This value is equivalent to 5 seconds of audio data at 20 ms per packet.
solenbergd97ec302015-10-07 01:40:33 -070066const int kNackMaxPackets = 250;
minyue@webrtc.org2dc6f312014-10-31 05:33:10 +000067
68// Codec parameters for Opus.
henrike@webrtc.org1e09a712013-07-26 19:17:59 +000069// draft-spittka-payload-rtp-opus-03
minyue@webrtc.org2dc6f312014-10-31 05:33:10 +000070
71// Recommended bitrates:
72// 8-12 kb/s for NB speech,
73// 16-20 kb/s for WB speech,
74// 28-40 kb/s for FB speech,
75// 48-64 kb/s for FB mono music, and
76// 64-128 kb/s for FB stereo music.
77// The current implementation applies the following values to mono signals,
78// and multiplies them by 2 for stereo.
solenbergd97ec302015-10-07 01:40:33 -070079const int kOpusBitrateNb = 12000;
80const int kOpusBitrateWb = 20000;
81const int kOpusBitrateFb = 32000;
minyue@webrtc.org2dc6f312014-10-31 05:33:10 +000082
henrike@webrtc.org1e09a712013-07-26 19:17:59 +000083// Opus bitrate should be in the range between 6000 and 510000.
solenbergd97ec302015-10-07 01:40:33 -070084const int kOpusMinBitrate = 6000;
85const int kOpusMaxBitrate = 510000;
buildbot@webrtc.org5d639b32014-09-10 07:57:12 +000086
wu@webrtc.orgde305012013-10-31 15:40:38 +000087// Default audio dscp value.
88// See http://tools.ietf.org/html/rfc2474 for details.
89// See also http://tools.ietf.org/html/draft-jennings-rtcweb-qos-00
solenbergd97ec302015-10-07 01:40:33 -070090const rtc::DiffServCodePoint kAudioDscpValue = rtc::DSCP_EF;
henrike@webrtc.org1e09a712013-07-26 19:17:59 +000091
Fredrik Solenbergb5727682015-12-04 15:22:19 +010092// Constants from voice_engine_defines.h.
93const int kMinTelephoneEventCode = 0; // RFC4733 (Section 2.3.1)
94const int kMaxTelephoneEventCode = 255;
95const int kMinTelephoneEventDuration = 100;
96const int kMaxTelephoneEventDuration = 60000; // Actual limit is 2^16
97
solenberg31642aa2016-03-14 08:00:37 -070098const int kMinPayloadType = 0;
99const int kMaxPayloadType = 127;
100
deadbeef884f5852016-01-15 09:20:04 -0800101class ProxySink : public webrtc::AudioSinkInterface {
102 public:
103 ProxySink(AudioSinkInterface* sink) : sink_(sink) { RTC_DCHECK(sink); }
104
105 void OnData(const Data& audio) override { sink_->OnData(audio); }
106
107 private:
108 webrtc::AudioSinkInterface* sink_;
109};
110
solenberg0b675462015-10-09 01:37:09 -0700111bool ValidateStreamParams(const StreamParams& sp) {
112 if (sp.ssrcs.empty()) {
113 LOG(LS_ERROR) << "No SSRCs in stream parameters: " << sp.ToString();
114 return false;
115 }
116 if (sp.ssrcs.size() > 1) {
117 LOG(LS_ERROR) << "Multiple SSRCs in stream parameters: " << sp.ToString();
118 return false;
119 }
120 return true;
121}
122
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000123// Dumps an AudioCodec in RFC 2327-ish format.
solenbergd97ec302015-10-07 01:40:33 -0700124std::string ToString(const AudioCodec& codec) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000125 std::stringstream ss;
126 ss << codec.name << "/" << codec.clockrate << "/" << codec.channels
127 << " (" << codec.id << ")";
128 return ss.str();
129}
Minyue Li7100dcd2015-03-27 05:05:59 +0100130
solenbergd97ec302015-10-07 01:40:33 -0700131std::string ToString(const webrtc::CodecInst& codec) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000132 std::stringstream ss;
133 ss << codec.plname << "/" << codec.plfreq << "/" << codec.channels
134 << " (" << codec.pltype << ")";
135 return ss.str();
136}
137
solenbergd97ec302015-10-07 01:40:33 -0700138bool IsCodec(const AudioCodec& codec, const char* ref_name) {
Minyue Li7100dcd2015-03-27 05:05:59 +0100139 return (_stricmp(codec.name.c_str(), ref_name) == 0);
140}
141
solenbergd97ec302015-10-07 01:40:33 -0700142bool IsCodec(const webrtc::CodecInst& codec, const char* ref_name) {
Minyue Li7100dcd2015-03-27 05:05:59 +0100143 return (_stricmp(codec.plname, ref_name) == 0);
144}
145
solenbergd97ec302015-10-07 01:40:33 -0700146bool FindCodec(const std::vector<AudioCodec>& codecs,
solenberg26c8c912015-11-27 04:00:25 -0800147 const AudioCodec& codec,
148 AudioCodec* found_codec) {
Fredrik Solenbergaf9fb212015-08-26 10:45:53 +0200149 for (const AudioCodec& c : codecs) {
150 if (c.Matches(codec)) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000151 if (found_codec != NULL) {
Fredrik Solenbergaf9fb212015-08-26 10:45:53 +0200152 *found_codec = c;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000153 }
154 return true;
155 }
156 }
157 return false;
158}
wu@webrtc.org1d1ffc92013-10-16 18:12:02 +0000159
solenberg0b675462015-10-09 01:37:09 -0700160bool VerifyUniquePayloadTypes(const std::vector<AudioCodec>& codecs) {
161 if (codecs.empty()) {
162 return true;
163 }
164 std::vector<int> payload_types;
165 for (const AudioCodec& codec : codecs) {
166 payload_types.push_back(codec.id);
167 }
168 std::sort(payload_types.begin(), payload_types.end());
169 auto it = std::unique(payload_types.begin(), payload_types.end());
170 return it == payload_types.end();
171}
172
Minyue Li7100dcd2015-03-27 05:05:59 +0100173// Return true if codec.params[feature] == "1", false otherwise.
solenberg26c8c912015-11-27 04:00:25 -0800174bool IsCodecFeatureEnabled(const AudioCodec& codec, const char* feature) {
Minyue Li7100dcd2015-03-27 05:05:59 +0100175 int value;
176 return codec.GetParam(feature, &value) && value == 1;
177}
178
179// Use params[kCodecParamMaxAverageBitrate] if it is defined, use codec.bitrate
180// otherwise. If the value (either from params or codec.bitrate) <=0, use the
181// default configuration. If the value is beyond feasible bit rate of Opus,
182// clamp it. Returns the Opus bit rate for operation.
solenbergd97ec302015-10-07 01:40:33 -0700183int GetOpusBitrate(const AudioCodec& codec, int max_playback_rate) {
Minyue Li7100dcd2015-03-27 05:05:59 +0100184 int bitrate = 0;
185 bool use_param = true;
186 if (!codec.GetParam(kCodecParamMaxAverageBitrate, &bitrate)) {
187 bitrate = codec.bitrate;
188 use_param = false;
189 }
190 if (bitrate <= 0) {
191 if (max_playback_rate <= 8000) {
192 bitrate = kOpusBitrateNb;
193 } else if (max_playback_rate <= 16000) {
194 bitrate = kOpusBitrateWb;
195 } else {
196 bitrate = kOpusBitrateFb;
197 }
198
199 if (IsCodecFeatureEnabled(codec, kCodecParamStereo)) {
200 bitrate *= 2;
201 }
202 } else if (bitrate < kOpusMinBitrate || bitrate > kOpusMaxBitrate) {
203 bitrate = (bitrate < kOpusMinBitrate) ? kOpusMinBitrate : kOpusMaxBitrate;
204 std::string rate_source =
205 use_param ? "Codec parameter \"maxaveragebitrate\"" :
206 "Supplied Opus bitrate";
207 LOG(LS_WARNING) << rate_source
208 << " is invalid and is replaced by: "
209 << bitrate;
210 }
211 return bitrate;
212}
213
214// Returns kOpusDefaultPlaybackRate if params[kCodecParamMaxPlaybackRate] is not
215// defined. Returns the value of params[kCodecParamMaxPlaybackRate] otherwise.
solenbergd97ec302015-10-07 01:40:33 -0700216int GetOpusMaxPlaybackRate(const AudioCodec& codec) {
Minyue Li7100dcd2015-03-27 05:05:59 +0100217 int value;
218 if (codec.GetParam(kCodecParamMaxPlaybackRate, &value)) {
219 return value;
220 }
221 return kOpusDefaultMaxPlaybackRate;
222}
223
solenbergd97ec302015-10-07 01:40:33 -0700224void GetOpusConfig(const AudioCodec& codec, webrtc::CodecInst* voe_codec,
Minyue Li7100dcd2015-03-27 05:05:59 +0100225 bool* enable_codec_fec, int* max_playback_rate,
226 bool* enable_codec_dtx) {
227 *enable_codec_fec = IsCodecFeatureEnabled(codec, kCodecParamUseInbandFec);
228 *enable_codec_dtx = IsCodecFeatureEnabled(codec, kCodecParamUseDtx);
229 *max_playback_rate = GetOpusMaxPlaybackRate(codec);
230
231 // If OPUS, change what we send according to the "stereo" codec
232 // parameter, and not the "channels" parameter. We set
233 // voe_codec.channels to 2 if "stereo=1" and 1 otherwise. If
234 // the bitrate is not specified, i.e. is <= zero, we set it to the
235 // appropriate default value for mono or stereo Opus.
236
237 voe_codec->channels = IsCodecFeatureEnabled(codec, kCodecParamStereo) ? 2 : 1;
238 voe_codec->rate = GetOpusBitrate(codec, *max_playback_rate);
239}
240
solenberg566ef242015-11-06 15:34:49 -0800241webrtc::AudioState::Config MakeAudioStateConfig(VoEWrapper* voe_wrapper) {
242 webrtc::AudioState::Config config;
243 config.voice_engine = voe_wrapper->engine();
244 return config;
245}
246
solenberg26c8c912015-11-27 04:00:25 -0800247class WebRtcVoiceCodecs final {
248 public:
249 // TODO(solenberg): Do this filtering once off-line, add a simple AudioCodec
250 // list and add a test which verifies VoE supports the listed codecs.
251 static std::vector<AudioCodec> SupportedCodecs() {
solenberg26c8c912015-11-27 04:00:25 -0800252 std::vector<AudioCodec> result;
253 for (webrtc::CodecInst voe_codec : webrtc::acm2::RentACodec::Database()) {
254 // Change the sample rate of G722 to 8000 to match SDP.
255 MaybeFixupG722(&voe_codec, 8000);
buildbot@webrtc.org13d67762014-05-02 17:33:29 +0000256 // Skip uncompressed formats.
Minyue Li7100dcd2015-03-27 05:05:59 +0100257 if (IsCodec(voe_codec, kL16CodecName)) {
buildbot@webrtc.org13d67762014-05-02 17:33:29 +0000258 continue;
259 }
260
261 const CodecPref* pref = NULL;
tfarina5237aaf2015-11-10 23:44:30 -0800262 for (size_t j = 0; j < arraysize(kCodecPrefs); ++j) {
Minyue Li7100dcd2015-03-27 05:05:59 +0100263 if (IsCodec(voe_codec, kCodecPrefs[j].name) &&
buildbot@webrtc.org13d67762014-05-02 17:33:29 +0000264 kCodecPrefs[j].clockrate == voe_codec.plfreq &&
265 kCodecPrefs[j].channels == voe_codec.channels) {
266 pref = &kCodecPrefs[j];
267 break;
268 }
269 }
270
271 if (pref) {
272 // Use the payload type that we've configured in our pref table;
273 // use the offset in our pref table to determine the sort order.
tfarina5237aaf2015-11-10 23:44:30 -0800274 AudioCodec codec(
275 pref->payload_type, voe_codec.plname, voe_codec.plfreq,
276 voe_codec.rate, voe_codec.channels,
277 static_cast<int>(arraysize(kCodecPrefs)) - (pref - kCodecPrefs));
Minyue Li7100dcd2015-03-27 05:05:59 +0100278 if (IsCodec(codec, kIsacCodecName)) {
minyue@webrtc.org26236952014-10-29 02:27:08 +0000279 // Indicate auto-bitrate in signaling.
buildbot@webrtc.org13d67762014-05-02 17:33:29 +0000280 codec.bitrate = 0;
281 }
Minyue Li7100dcd2015-03-27 05:05:59 +0100282 if (IsCodec(codec, kOpusCodecName)) {
buildbot@webrtc.org13d67762014-05-02 17:33:29 +0000283 // Only add fmtp parameters that differ from the spec.
284 if (kPreferredMinPTime != kOpusDefaultMinPTime) {
285 codec.params[kCodecParamMinPTime] =
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000286 rtc::ToString(kPreferredMinPTime);
buildbot@webrtc.org13d67762014-05-02 17:33:29 +0000287 }
288 if (kPreferredMaxPTime != kOpusDefaultMaxPTime) {
289 codec.params[kCodecParamMaxPTime] =
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000290 rtc::ToString(kPreferredMaxPTime);
buildbot@webrtc.org13d67762014-05-02 17:33:29 +0000291 }
pkasting@chromium.orgd3245462015-02-23 21:28:22 +0000292 codec.SetParam(kCodecParamUseInbandFec, 1);
stefanba4c0e42016-02-04 04:12:24 -0800293 codec.AddFeedbackParam(
294 FeedbackParam(kRtcpFbParamTransportCc, kParamValueEmpty));
minyue@webrtc.org4ef22d12014-11-17 09:26:39 +0000295
296 // TODO(hellner): Add ptime, sprop-stereo, and stereo
buildbot@webrtc.org13d67762014-05-02 17:33:29 +0000297 // when they can be set to values other than the default.
298 }
solenberg26c8c912015-11-27 04:00:25 -0800299 result.push_back(codec);
buildbot@webrtc.org13d67762014-05-02 17:33:29 +0000300 } else {
solenbergff976312016-03-30 23:28:51 -0700301 LOG(LS_INFO) << "[Unused] " << ToString(voe_codec);
buildbot@webrtc.org13d67762014-05-02 17:33:29 +0000302 }
303 }
solenberg26c8c912015-11-27 04:00:25 -0800304 // Make sure they are in local preference order.
305 std::sort(result.begin(), result.end(), &AudioCodec::Preferable);
306 return result;
buildbot@webrtc.org13d67762014-05-02 17:33:29 +0000307 }
buildbot@webrtc.org13d67762014-05-02 17:33:29 +0000308
solenberg26c8c912015-11-27 04:00:25 -0800309 static bool ToCodecInst(const AudioCodec& in,
310 webrtc::CodecInst* out) {
311 for (webrtc::CodecInst voe_codec : webrtc::acm2::RentACodec::Database()) {
312 // Change the sample rate of G722 to 8000 to match SDP.
313 MaybeFixupG722(&voe_codec, 8000);
314 AudioCodec codec(voe_codec.pltype, voe_codec.plname, voe_codec.plfreq,
315 voe_codec.rate, voe_codec.channels, 0);
316 bool multi_rate = IsCodecMultiRate(voe_codec);
317 // Allow arbitrary rates for ISAC to be specified.
318 if (multi_rate) {
319 // Set codec.bitrate to 0 so the check for codec.Matches() passes.
320 codec.bitrate = 0;
321 }
322 if (codec.Matches(in)) {
323 if (out) {
324 // Fixup the payload type.
325 voe_codec.pltype = in.id;
326
327 // Set bitrate if specified.
328 if (multi_rate && in.bitrate != 0) {
329 voe_codec.rate = in.bitrate;
330 }
331
332 // Reset G722 sample rate to 16000 to match WebRTC.
333 MaybeFixupG722(&voe_codec, 16000);
334
335 // Apply codec-specific settings.
336 if (IsCodec(codec, kIsacCodecName)) {
337 // If ISAC and an explicit bitrate is not specified,
338 // enable auto bitrate adjustment.
339 voe_codec.rate = (in.bitrate > 0) ? in.bitrate : -1;
340 }
341 *out = voe_codec;
342 }
343 return true;
344 }
345 }
henrik.lundin@webrtc.org8038d422014-11-11 08:38:24 +0000346 return false;
henrik.lundin@webrtc.orgf85dbce2014-11-07 12:25:00 +0000347 }
solenberg26c8c912015-11-27 04:00:25 -0800348
349 static bool IsCodecMultiRate(const webrtc::CodecInst& codec) {
350 for (size_t i = 0; i < arraysize(kCodecPrefs); ++i) {
351 if (IsCodec(codec, kCodecPrefs[i].name) &&
352 kCodecPrefs[i].clockrate == codec.plfreq) {
353 return kCodecPrefs[i].is_multi_rate;
354 }
355 }
356 return false;
357 }
358
359 // If the AudioCodec param kCodecParamPTime is set, then we will set it to
360 // codec pacsize if it's valid, or we will pick the next smallest value we
361 // support.
362 // TODO(Brave): Query supported packet sizes from ACM when the API is ready.
363 static bool SetPTimeAsPacketSize(webrtc::CodecInst* codec, int ptime_ms) {
364 for (const CodecPref& codec_pref : kCodecPrefs) {
365 if ((IsCodec(*codec, codec_pref.name) &&
366 codec_pref.clockrate == codec->plfreq) ||
367 IsCodec(*codec, kG722CodecName)) {
368 int packet_size_ms = SelectPacketSize(codec_pref, ptime_ms);
369 if (packet_size_ms) {
370 // Convert unit from milli-seconds to samples.
371 codec->pacsize = (codec->plfreq / 1000) * packet_size_ms;
372 return true;
373 }
374 }
375 }
376 return false;
377 }
378
stefanba4c0e42016-02-04 04:12:24 -0800379 static const AudioCodec* GetPreferredCodec(
380 const std::vector<AudioCodec>& codecs,
solenberg72e29d22016-03-08 06:35:16 -0800381 webrtc::CodecInst* out,
stefanba4c0e42016-02-04 04:12:24 -0800382 int* red_payload_type) {
solenberg72e29d22016-03-08 06:35:16 -0800383 RTC_DCHECK(out);
stefanba4c0e42016-02-04 04:12:24 -0800384 RTC_DCHECK(red_payload_type);
385 // Select the preferred send codec (the first non-telephone-event/CN codec).
386 for (const AudioCodec& codec : codecs) {
387 *red_payload_type = -1;
388 if (IsCodec(codec, kDtmfCodecName) || IsCodec(codec, kCnCodecName)) {
389 // Skip telephone-event/CN codec, which will be handled later.
390 continue;
391 }
392
393 // We'll use the first codec in the list to actually send audio data.
394 // Be sure to use the payload type requested by the remote side.
395 // "red", for RED audio, is a special case where the actual codec to be
396 // used is specified in params.
397 const AudioCodec* found_codec = &codec;
398 if (IsCodec(*found_codec, kRedCodecName)) {
399 // Parse out the RED parameters. If we fail, just ignore RED;
400 // we don't support all possible params/usage scenarios.
401 *red_payload_type = codec.id;
402 found_codec = GetRedSendCodec(*found_codec, codecs);
403 if (!found_codec) {
404 continue;
405 }
406 }
407 // Ignore codecs we don't know about. The negotiation step should prevent
408 // this, but double-check to be sure.
solenberg72e29d22016-03-08 06:35:16 -0800409 webrtc::CodecInst voe_codec = {0};
410 if (!ToCodecInst(*found_codec, &voe_codec)) {
stefanba4c0e42016-02-04 04:12:24 -0800411 LOG(LS_WARNING) << "Unknown codec " << ToString(*found_codec);
412 continue;
413 }
solenberg72e29d22016-03-08 06:35:16 -0800414 *out = voe_codec;
stefanba4c0e42016-02-04 04:12:24 -0800415 return found_codec;
416 }
417 return nullptr;
418 }
419
solenberg26c8c912015-11-27 04:00:25 -0800420 private:
421 static const int kMaxNumPacketSize = 6;
422 struct CodecPref {
423 const char* name;
424 int clockrate;
Peter Kasting69558702016-01-12 16:26:35 -0800425 size_t channels;
solenberg26c8c912015-11-27 04:00:25 -0800426 int payload_type;
427 bool is_multi_rate;
428 int packet_sizes_ms[kMaxNumPacketSize];
429 };
430 // Note: keep the supported packet sizes in ascending order.
431 static const CodecPref kCodecPrefs[12];
432
433 static int SelectPacketSize(const CodecPref& codec_pref, int ptime_ms) {
434 int selected_packet_size_ms = codec_pref.packet_sizes_ms[0];
435 for (int packet_size_ms : codec_pref.packet_sizes_ms) {
436 if (packet_size_ms && packet_size_ms <= ptime_ms) {
437 selected_packet_size_ms = packet_size_ms;
438 }
439 }
440 return selected_packet_size_ms;
441 }
442
443 // Changes RTP timestamp rate of G722. This is due to the "bug" in the RFC
444 // which says that G722 should be advertised as 8 kHz although it is a 16 kHz
445 // codec.
446 static void MaybeFixupG722(webrtc::CodecInst* voe_codec, int new_plfreq) {
447 if (IsCodec(*voe_codec, kG722CodecName)) {
448 // If the ASSERT triggers, the codec definition in WebRTC VoiceEngine
449 // has changed, and this special case is no longer needed.
450 RTC_DCHECK(voe_codec->plfreq != new_plfreq);
451 voe_codec->plfreq = new_plfreq;
452 }
453 }
stefanba4c0e42016-02-04 04:12:24 -0800454
455 static const AudioCodec* GetRedSendCodec(
456 const AudioCodec& red_codec,
457 const std::vector<AudioCodec>& all_codecs) {
458 // Get the RED encodings from the parameter with no name. This may
459 // change based on what is discussed on the Jingle list.
460 // The encoding parameter is of the form "a/b"; we only support where
461 // a == b. Verify this and parse out the value into red_pt.
462 // If the parameter value is absent (as it will be until we wire up the
463 // signaling of this message), use the second codec specified (i.e. the
464 // one after "red") as the encoding parameter.
465 int red_pt = -1;
466 std::string red_params;
467 CodecParameterMap::const_iterator it = red_codec.params.find("");
468 if (it != red_codec.params.end()) {
469 red_params = it->second;
470 std::vector<std::string> red_pts;
471 if (rtc::split(red_params, '/', &red_pts) != 2 ||
472 red_pts[0] != red_pts[1] || !rtc::FromString(red_pts[0], &red_pt)) {
473 LOG(LS_WARNING) << "RED params " << red_params << " not supported.";
474 return nullptr;
475 }
476 } else if (red_codec.params.empty()) {
477 LOG(LS_WARNING) << "RED params not present, using defaults";
478 if (all_codecs.size() > 1) {
479 red_pt = all_codecs[1].id;
480 }
481 }
482
483 // Try to find red_pt in |codecs|.
484 for (const AudioCodec& codec : all_codecs) {
485 if (codec.id == red_pt) {
486 return &codec;
487 }
488 }
489 LOG(LS_WARNING) << "RED params " << red_params << " are invalid.";
490 return nullptr;
491 }
solenberg26c8c912015-11-27 04:00:25 -0800492};
493
494const WebRtcVoiceCodecs::CodecPref WebRtcVoiceCodecs::kCodecPrefs[12] = {
495 { kOpusCodecName, 48000, 2, 111, true, { 10, 20, 40, 60 } },
496 { kIsacCodecName, 16000, 1, 103, true, { 30, 60 } },
497 { kIsacCodecName, 32000, 1, 104, true, { 30 } },
498 // G722 should be advertised as 8000 Hz because of the RFC "bug".
499 { kG722CodecName, 8000, 1, 9, false, { 10, 20, 30, 40, 50, 60 } },
500 { kIlbcCodecName, 8000, 1, 102, false, { 20, 30, 40, 60 } },
501 { kPcmuCodecName, 8000, 1, 0, false, { 10, 20, 30, 40, 50, 60 } },
502 { kPcmaCodecName, 8000, 1, 8, false, { 10, 20, 30, 40, 50, 60 } },
503 { kCnCodecName, 32000, 1, 106, false, { } },
504 { kCnCodecName, 16000, 1, 105, false, { } },
505 { kCnCodecName, 8000, 1, 13, false, { } },
506 { kRedCodecName, 8000, 1, 127, false, { } },
507 { kDtmfCodecName, 8000, 1, 126, false, { } },
508};
509} // namespace {
510
511bool WebRtcVoiceEngine::ToCodecInst(const AudioCodec& in,
512 webrtc::CodecInst* out) {
513 return WebRtcVoiceCodecs::ToCodecInst(in, out);
514}
515
solenbergff976312016-03-30 23:28:51 -0700516WebRtcVoiceEngine::WebRtcVoiceEngine(webrtc::AudioDeviceModule* adm)
517 : WebRtcVoiceEngine(adm, new VoEWrapper()) {
518 audio_state_ = webrtc::AudioState::Create(MakeAudioStateConfig(voe()));
solenberg26c8c912015-11-27 04:00:25 -0800519}
520
solenbergff976312016-03-30 23:28:51 -0700521WebRtcVoiceEngine::WebRtcVoiceEngine(webrtc::AudioDeviceModule* adm,
522 VoEWrapper* voe_wrapper)
523 : adm_(adm), voe_wrapper_(voe_wrapper) {
solenberg26c8c912015-11-27 04:00:25 -0800524 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
solenbergff976312016-03-30 23:28:51 -0700525 LOG(LS_INFO) << "WebRtcVoiceEngine::WebRtcVoiceEngine";
526 RTC_DCHECK(voe_wrapper);
solenberg26c8c912015-11-27 04:00:25 -0800527
528 signal_thread_checker_.DetachFromThread();
solenberg26c8c912015-11-27 04:00:25 -0800529
530 // Load our audio codec list.
solenbergff976312016-03-30 23:28:51 -0700531 LOG(LS_INFO) << "Supported codecs in order of preference:";
solenberg26c8c912015-11-27 04:00:25 -0800532 codecs_ = WebRtcVoiceCodecs::SupportedCodecs();
solenbergff976312016-03-30 23:28:51 -0700533 for (const AudioCodec& codec : codecs_) {
534 LOG(LS_INFO) << ToString(codec);
buildbot@webrtc.org13d67762014-05-02 17:33:29 +0000535 }
buildbot@webrtc.org13d67762014-05-02 17:33:29 +0000536
solenbergff976312016-03-30 23:28:51 -0700537 voe_config_.Set<webrtc::VoicePacing>(new webrtc::VoicePacing(true));
buildbot@webrtc.org13d67762014-05-02 17:33:29 +0000538
solenbergff976312016-03-30 23:28:51 -0700539 // Temporarily turn logging level up for the Init() call.
540 webrtc::Trace::SetTraceCallback(this);
solenbergbd138382015-11-20 16:08:07 -0800541 webrtc::Trace::set_level_filter(kElevatedTraceFilter);
solenberg2515af22015-12-02 06:19:36 -0800542 LOG(LS_INFO) << webrtc::VoiceEngine::GetVersionString();
solenbergff976312016-03-30 23:28:51 -0700543 RTC_CHECK_EQ(0, voe_wrapper_->base()->Init(adm_.get()));
solenbergbd138382015-11-20 16:08:07 -0800544 webrtc::Trace::set_level_filter(kDefaultTraceFilter);
buildbot@webrtc.org13d67762014-05-02 17:33:29 +0000545
solenbergff976312016-03-30 23:28:51 -0700546 // No ADM supplied? Get the default one from VoE.
547 if (!adm_) {
548 adm_ = voe_wrapper_->base()->audio_device_module();
549 }
550 RTC_DCHECK(adm_);
551
buildbot@webrtc.org13d67762014-05-02 17:33:29 +0000552 // Save the default AGC configuration settings. This must happen before
solenberg246b8172015-12-08 09:50:23 -0800553 // calling ApplyOptions or the default will be overwritten.
solenbergff976312016-03-30 23:28:51 -0700554 int error = voe_wrapper_->processing()->GetAgcConfig(default_agc_config_);
555 RTC_DCHECK_EQ(0, error);
buildbot@webrtc.org13d67762014-05-02 17:33:29 +0000556
solenberg0f7d2932016-01-15 01:40:39 -0800557 // Set default engine options.
558 {
559 AudioOptions options;
560 options.echo_cancellation = rtc::Optional<bool>(true);
561 options.auto_gain_control = rtc::Optional<bool>(true);
562 options.noise_suppression = rtc::Optional<bool>(true);
563 options.highpass_filter = rtc::Optional<bool>(true);
564 options.stereo_swapping = rtc::Optional<bool>(false);
565 options.audio_jitter_buffer_max_packets = rtc::Optional<int>(50);
566 options.audio_jitter_buffer_fast_accelerate = rtc::Optional<bool>(false);
567 options.typing_detection = rtc::Optional<bool>(true);
568 options.adjust_agc_delta = rtc::Optional<int>(0);
569 options.experimental_agc = rtc::Optional<bool>(false);
570 options.extended_filter_aec = rtc::Optional<bool>(false);
571 options.delay_agnostic_aec = rtc::Optional<bool>(false);
572 options.experimental_ns = rtc::Optional<bool>(false);
solenbergff976312016-03-30 23:28:51 -0700573 bool error = ApplyOptions(options);
574 RTC_DCHECK(error);
buildbot@webrtc.org13d67762014-05-02 17:33:29 +0000575 }
576
solenberg246b8172015-12-08 09:50:23 -0800577 SetDefaultDevices();
buildbot@webrtc.org13d67762014-05-02 17:33:29 +0000578}
579
solenbergff976312016-03-30 23:28:51 -0700580WebRtcVoiceEngine::~WebRtcVoiceEngine() {
solenberg566ef242015-11-06 15:34:49 -0800581 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
solenbergff976312016-03-30 23:28:51 -0700582 LOG(LS_INFO) << "WebRtcVoiceEngine::~WebRtcVoiceEngine";
buildbot@webrtc.org13d67762014-05-02 17:33:29 +0000583 StopAecDump();
buildbot@webrtc.org13d67762014-05-02 17:33:29 +0000584 voe_wrapper_->base()->Terminate();
solenbergff976312016-03-30 23:28:51 -0700585 webrtc::Trace::SetTraceCallback(nullptr);
buildbot@webrtc.org13d67762014-05-02 17:33:29 +0000586}
587
solenberg566ef242015-11-06 15:34:49 -0800588rtc::scoped_refptr<webrtc::AudioState>
589 WebRtcVoiceEngine::GetAudioState() const {
590 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
591 return audio_state_;
592}
593
nisse51542be2016-02-12 02:27:06 -0800594VoiceMediaChannel* WebRtcVoiceEngine::CreateChannel(
595 webrtc::Call* call,
596 const MediaConfig& config,
Jelena Marusicc28a8962015-05-29 15:05:44 +0200597 const AudioOptions& options) {
solenberg566ef242015-11-06 15:34:49 -0800598 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
nisse51542be2016-02-12 02:27:06 -0800599 return new WebRtcVoiceMediaChannel(this, config, options, call);
buildbot@webrtc.org13d67762014-05-02 17:33:29 +0000600}
601
buildbot@webrtc.org13d67762014-05-02 17:33:29 +0000602bool WebRtcVoiceEngine::ApplyOptions(const AudioOptions& options_in) {
solenberg566ef242015-11-06 15:34:49 -0800603 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
solenbergff976312016-03-30 23:28:51 -0700604 LOG(LS_INFO) << "WebRtcVoiceEngine::ApplyOptions: " << options_in.ToString();
solenberg0f7d2932016-01-15 01:40:39 -0800605 AudioOptions options = options_in; // The options are modified below.
solenberg246b8172015-12-08 09:50:23 -0800606
buildbot@webrtc.org13d67762014-05-02 17:33:29 +0000607 // kEcConference is AEC with high suppression.
608 webrtc::EcModes ec_mode = webrtc::kEcConference;
609 webrtc::AecmModes aecm_mode = webrtc::kAecmSpeakerphone;
610 webrtc::AgcModes agc_mode = webrtc::kAgcAdaptiveAnalog;
611 webrtc::NsModes ns_mode = webrtc::kNsHighSuppression;
kwiberg102c6a62015-10-30 02:47:38 -0700612 if (options.aecm_generate_comfort_noise) {
buildbot@webrtc.org13d67762014-05-02 17:33:29 +0000613 LOG(LS_VERBOSE) << "Comfort noise explicitly set to "
kwiberg102c6a62015-10-30 02:47:38 -0700614 << *options.aecm_generate_comfort_noise
615 << " (default is false).";
buildbot@webrtc.org13d67762014-05-02 17:33:29 +0000616 }
617
kjellanderfcfc8042016-01-14 11:01:09 -0800618#if defined(WEBRTC_IOS)
buildbot@webrtc.org13d67762014-05-02 17:33:29 +0000619 // On iOS, VPIO provides built-in EC and AGC.
Karl Wibergbe579832015-11-10 22:34:18 +0100620 options.echo_cancellation = rtc::Optional<bool>(false);
621 options.auto_gain_control = rtc::Optional<bool>(false);
henrika86d907c2015-09-07 16:09:50 +0200622 LOG(LS_INFO) << "Always disable AEC and AGC on iOS. Use built-in instead.";
buildbot@webrtc.org13d67762014-05-02 17:33:29 +0000623#elif defined(ANDROID)
624 ec_mode = webrtc::kEcAecm;
625#endif
626
kjellanderfcfc8042016-01-14 11:01:09 -0800627#if defined(WEBRTC_IOS) || defined(ANDROID)
buildbot@webrtc.org13d67762014-05-02 17:33:29 +0000628 // Set the AGC mode for iOS as well despite disabling it above, to avoid
629 // unsupported configuration errors from webrtc.
630 agc_mode = webrtc::kAgcFixedDigital;
Karl Wibergbe579832015-11-10 22:34:18 +0100631 options.typing_detection = rtc::Optional<bool>(false);
632 options.experimental_agc = rtc::Optional<bool>(false);
633 options.extended_filter_aec = rtc::Optional<bool>(false);
634 options.experimental_ns = rtc::Optional<bool>(false);
buildbot@webrtc.org13d67762014-05-02 17:33:29 +0000635#endif
636
Bjorn Volckerbf395c12015-03-25 22:45:56 +0100637 // Delay Agnostic AEC automatically turns on EC if not set except on iOS
638 // where the feature is not supported.
639 bool use_delay_agnostic_aec = false;
kjellanderfcfc8042016-01-14 11:01:09 -0800640#if !defined(WEBRTC_IOS)
kwiberg102c6a62015-10-30 02:47:38 -0700641 if (options.delay_agnostic_aec) {
642 use_delay_agnostic_aec = *options.delay_agnostic_aec;
Bjorn Volckerbf395c12015-03-25 22:45:56 +0100643 if (use_delay_agnostic_aec) {
Karl Wibergbe579832015-11-10 22:34:18 +0100644 options.echo_cancellation = rtc::Optional<bool>(true);
645 options.extended_filter_aec = rtc::Optional<bool>(true);
Bjorn Volckerbf395c12015-03-25 22:45:56 +0100646 ec_mode = webrtc::kEcConference;
647 }
648 }
649#endif
650
buildbot@webrtc.org13d67762014-05-02 17:33:29 +0000651 webrtc::VoEAudioProcessing* voep = voe_wrapper_->processing();
652
kwiberg102c6a62015-10-30 02:47:38 -0700653 if (options.echo_cancellation) {
henrika@webrtc.orga954c072014-12-09 16:22:09 +0000654 // Check if platform supports built-in EC. Currently only supported on
655 // Android and in combination with Java based audio layer.
656 // TODO(henrika): investigate possibility to support built-in EC also
657 // in combination with Open SL ES audio.
658 const bool built_in_aec = voe_wrapper_->hw()->BuiltInAECIsAvailable();
Bjorn Volcker73f72102015-06-03 14:50:15 +0200659 if (built_in_aec) {
Bjorn Volckerccfc9392015-05-07 07:43:17 +0200660 // Built-in EC exists on this device and use_delay_agnostic_aec is not
661 // overriding it. Enable/Disable it according to the echo_cancellation
662 // audio option.
Bjorn Volcker73f72102015-06-03 14:50:15 +0200663 const bool enable_built_in_aec =
kwiberg102c6a62015-10-30 02:47:38 -0700664 *options.echo_cancellation && !use_delay_agnostic_aec;
Bjorn Volcker73f72102015-06-03 14:50:15 +0200665 if (voe_wrapper_->hw()->EnableBuiltInAEC(enable_built_in_aec) == 0 &&
666 enable_built_in_aec) {
Bjorn Volckerbf395c12015-03-25 22:45:56 +0100667 // Disable internal software EC if built-in EC is enabled,
henrika@webrtc.orga954c072014-12-09 16:22:09 +0000668 // i.e., replace the software EC with the built-in EC.
Karl Wibergbe579832015-11-10 22:34:18 +0100669 options.echo_cancellation = rtc::Optional<bool>(false);
henrika@webrtc.orga954c072014-12-09 16:22:09 +0000670 LOG(LS_INFO) << "Disabling EC since built-in EC will be used instead";
671 }
672 }
kwiberg102c6a62015-10-30 02:47:38 -0700673 if (voep->SetEcStatus(*options.echo_cancellation, ec_mode) == -1) {
674 LOG_RTCERR2(SetEcStatus, *options.echo_cancellation, ec_mode);
buildbot@webrtc.org13d67762014-05-02 17:33:29 +0000675 return false;
676 } else {
kwiberg102c6a62015-10-30 02:47:38 -0700677 LOG(LS_INFO) << "Echo control set to " << *options.echo_cancellation
henrika86d907c2015-09-07 16:09:50 +0200678 << " with mode " << ec_mode;
buildbot@webrtc.org13d67762014-05-02 17:33:29 +0000679 }
680#if !defined(ANDROID)
681 // TODO(ajm): Remove the error return on Android from webrtc.
kwiberg102c6a62015-10-30 02:47:38 -0700682 if (voep->SetEcMetricsStatus(*options.echo_cancellation) == -1) {
683 LOG_RTCERR1(SetEcMetricsStatus, *options.echo_cancellation);
buildbot@webrtc.org13d67762014-05-02 17:33:29 +0000684 return false;
685 }
686#endif
687 if (ec_mode == webrtc::kEcAecm) {
kwiberg102c6a62015-10-30 02:47:38 -0700688 bool cn = options.aecm_generate_comfort_noise.value_or(false);
689 if (voep->SetAecmMode(aecm_mode, cn) != 0) {
690 LOG_RTCERR2(SetAecmMode, aecm_mode, cn);
buildbot@webrtc.org13d67762014-05-02 17:33:29 +0000691 return false;
692 }
693 }
694 }
695
kwiberg102c6a62015-10-30 02:47:38 -0700696 if (options.auto_gain_control) {
henrikac14f5ff2015-09-23 14:08:33 +0200697 const bool built_in_agc = voe_wrapper_->hw()->BuiltInAGCIsAvailable();
698 if (built_in_agc) {
kwiberg102c6a62015-10-30 02:47:38 -0700699 if (voe_wrapper_->hw()->EnableBuiltInAGC(*options.auto_gain_control) ==
700 0 &&
701 *options.auto_gain_control) {
henrikac14f5ff2015-09-23 14:08:33 +0200702 // Disable internal software AGC if built-in AGC is enabled,
703 // i.e., replace the software AGC with the built-in AGC.
Karl Wibergbe579832015-11-10 22:34:18 +0100704 options.auto_gain_control = rtc::Optional<bool>(false);
henrikac14f5ff2015-09-23 14:08:33 +0200705 LOG(LS_INFO) << "Disabling AGC since built-in AGC will be used instead";
706 }
707 }
kwiberg102c6a62015-10-30 02:47:38 -0700708 if (voep->SetAgcStatus(*options.auto_gain_control, agc_mode) == -1) {
709 LOG_RTCERR2(SetAgcStatus, *options.auto_gain_control, agc_mode);
buildbot@webrtc.org13d67762014-05-02 17:33:29 +0000710 return false;
711 } else {
kwiberg102c6a62015-10-30 02:47:38 -0700712 LOG(LS_INFO) << "Auto gain set to " << *options.auto_gain_control
713 << " with mode " << agc_mode;
buildbot@webrtc.org13d67762014-05-02 17:33:29 +0000714 }
715 }
716
kwiberg102c6a62015-10-30 02:47:38 -0700717 if (options.tx_agc_target_dbov || options.tx_agc_digital_compression_gain ||
718 options.tx_agc_limiter) {
buildbot@webrtc.org13d67762014-05-02 17:33:29 +0000719 // Override default_agc_config_. Generally, an unset option means "leave
720 // the VoE bits alone" in this function, so we want whatever is set to be
721 // stored as the new "default". If we didn't, then setting e.g.
722 // tx_agc_target_dbov would reset digital compression gain and limiter
723 // settings.
724 // Also, if we don't update default_agc_config_, then adjust_agc_delta
725 // would be an offset from the original values, and not whatever was set
726 // explicitly.
kwiberg102c6a62015-10-30 02:47:38 -0700727 default_agc_config_.targetLeveldBOv = options.tx_agc_target_dbov.value_or(
728 default_agc_config_.targetLeveldBOv);
buildbot@webrtc.org13d67762014-05-02 17:33:29 +0000729 default_agc_config_.digitalCompressionGaindB =
kwiberg102c6a62015-10-30 02:47:38 -0700730 options.tx_agc_digital_compression_gain.value_or(
buildbot@webrtc.org13d67762014-05-02 17:33:29 +0000731 default_agc_config_.digitalCompressionGaindB);
732 default_agc_config_.limiterEnable =
kwiberg102c6a62015-10-30 02:47:38 -0700733 options.tx_agc_limiter.value_or(default_agc_config_.limiterEnable);
buildbot@webrtc.org13d67762014-05-02 17:33:29 +0000734 if (voe_wrapper_->processing()->SetAgcConfig(default_agc_config_) == -1) {
735 LOG_RTCERR3(SetAgcConfig,
736 default_agc_config_.targetLeveldBOv,
737 default_agc_config_.digitalCompressionGaindB,
738 default_agc_config_.limiterEnable);
739 return false;
740 }
741 }
742
kwiberg102c6a62015-10-30 02:47:38 -0700743 if (options.noise_suppression) {
henrikac14f5ff2015-09-23 14:08:33 +0200744 const bool built_in_ns = voe_wrapper_->hw()->BuiltInNSIsAvailable();
745 if (built_in_ns) {
kwiberg102c6a62015-10-30 02:47:38 -0700746 if (voe_wrapper_->hw()->EnableBuiltInNS(*options.noise_suppression) ==
747 0 &&
748 *options.noise_suppression) {
henrikac14f5ff2015-09-23 14:08:33 +0200749 // Disable internal software NS if built-in NS is enabled,
750 // i.e., replace the software NS with the built-in NS.
Karl Wibergbe579832015-11-10 22:34:18 +0100751 options.noise_suppression = rtc::Optional<bool>(false);
henrikac14f5ff2015-09-23 14:08:33 +0200752 LOG(LS_INFO) << "Disabling NS since built-in NS will be used instead";
753 }
754 }
kwiberg102c6a62015-10-30 02:47:38 -0700755 if (voep->SetNsStatus(*options.noise_suppression, ns_mode) == -1) {
756 LOG_RTCERR2(SetNsStatus, *options.noise_suppression, ns_mode);
buildbot@webrtc.org13d67762014-05-02 17:33:29 +0000757 return false;
758 } else {
kwiberg102c6a62015-10-30 02:47:38 -0700759 LOG(LS_INFO) << "Noise suppression set to " << *options.noise_suppression
henrikac14f5ff2015-09-23 14:08:33 +0200760 << " with mode " << ns_mode;
buildbot@webrtc.org13d67762014-05-02 17:33:29 +0000761 }
762 }
763
kwiberg102c6a62015-10-30 02:47:38 -0700764 if (options.highpass_filter) {
765 LOG(LS_INFO) << "High pass filter enabled? " << *options.highpass_filter;
766 if (voep->EnableHighPassFilter(*options.highpass_filter) == -1) {
767 LOG_RTCERR1(SetHighpassFilterStatus, *options.highpass_filter);
buildbot@webrtc.org13d67762014-05-02 17:33:29 +0000768 return false;
769 }
770 }
771
kwiberg102c6a62015-10-30 02:47:38 -0700772 if (options.stereo_swapping) {
773 LOG(LS_INFO) << "Stereo swapping enabled? " << *options.stereo_swapping;
774 voep->EnableStereoChannelSwapping(*options.stereo_swapping);
775 if (voep->IsStereoChannelSwappingEnabled() != *options.stereo_swapping) {
776 LOG_RTCERR1(EnableStereoChannelSwapping, *options.stereo_swapping);
buildbot@webrtc.org13d67762014-05-02 17:33:29 +0000777 return false;
778 }
779 }
780
kwiberg102c6a62015-10-30 02:47:38 -0700781 if (options.audio_jitter_buffer_max_packets) {
782 LOG(LS_INFO) << "NetEq capacity is "
783 << *options.audio_jitter_buffer_max_packets;
Henrik Lundin64dad832015-05-11 12:44:23 +0200784 voe_config_.Set<webrtc::NetEqCapacityConfig>(
kwiberg102c6a62015-10-30 02:47:38 -0700785 new webrtc::NetEqCapacityConfig(
786 *options.audio_jitter_buffer_max_packets));
Henrik Lundin64dad832015-05-11 12:44:23 +0200787 }
788
kwiberg102c6a62015-10-30 02:47:38 -0700789 if (options.audio_jitter_buffer_fast_accelerate) {
790 LOG(LS_INFO) << "NetEq fast mode? "
791 << *options.audio_jitter_buffer_fast_accelerate;
Henrik Lundin5263b3c2015-06-01 10:29:41 +0200792 voe_config_.Set<webrtc::NetEqFastAccelerate>(
kwiberg102c6a62015-10-30 02:47:38 -0700793 new webrtc::NetEqFastAccelerate(
794 *options.audio_jitter_buffer_fast_accelerate));
Henrik Lundin5263b3c2015-06-01 10:29:41 +0200795 }
796
kwiberg102c6a62015-10-30 02:47:38 -0700797 if (options.typing_detection) {
798 LOG(LS_INFO) << "Typing detection is enabled? "
799 << *options.typing_detection;
800 if (voep->SetTypingDetectionStatus(*options.typing_detection) == -1) {
buildbot@webrtc.org13d67762014-05-02 17:33:29 +0000801 // In case of error, log the info and continue
kwiberg102c6a62015-10-30 02:47:38 -0700802 LOG_RTCERR1(SetTypingDetectionStatus, *options.typing_detection);
buildbot@webrtc.org13d67762014-05-02 17:33:29 +0000803 }
804 }
805
kwiberg102c6a62015-10-30 02:47:38 -0700806 if (options.adjust_agc_delta) {
807 LOG(LS_INFO) << "Adjust agc delta is " << *options.adjust_agc_delta;
808 if (!AdjustAgcLevel(*options.adjust_agc_delta)) {
buildbot@webrtc.org13d67762014-05-02 17:33:29 +0000809 return false;
810 }
811 }
812
buildbot@webrtc.org1f8a2372014-08-28 10:52:44 +0000813 webrtc::Config config;
814
kwiberg102c6a62015-10-30 02:47:38 -0700815 if (options.delay_agnostic_aec)
816 delay_agnostic_aec_ = options.delay_agnostic_aec;
817 if (delay_agnostic_aec_) {
818 LOG(LS_INFO) << "Delay agnostic aec is enabled? " << *delay_agnostic_aec_;
henrik.lundin0f133b92015-07-02 00:17:55 -0700819 config.Set<webrtc::DelayAgnostic>(
kwiberg102c6a62015-10-30 02:47:38 -0700820 new webrtc::DelayAgnostic(*delay_agnostic_aec_));
Bjorn Volckerbf395c12015-03-25 22:45:56 +0100821 }
822
kwiberg102c6a62015-10-30 02:47:38 -0700823 if (options.extended_filter_aec) {
824 extended_filter_aec_ = options.extended_filter_aec;
825 }
826 if (extended_filter_aec_) {
827 LOG(LS_INFO) << "Extended filter aec is enabled? " << *extended_filter_aec_;
Henrik Lundin441f6342015-06-09 16:03:13 +0200828 config.Set<webrtc::ExtendedFilter>(
kwiberg102c6a62015-10-30 02:47:38 -0700829 new webrtc::ExtendedFilter(*extended_filter_aec_));
buildbot@webrtc.org1f8a2372014-08-28 10:52:44 +0000830 }
831
kwiberg102c6a62015-10-30 02:47:38 -0700832 if (options.experimental_ns) {
833 experimental_ns_ = options.experimental_ns;
834 }
835 if (experimental_ns_) {
836 LOG(LS_INFO) << "Experimental ns is enabled? " << *experimental_ns_;
buildbot@webrtc.org1f8a2372014-08-28 10:52:44 +0000837 config.Set<webrtc::ExperimentalNs>(
kwiberg102c6a62015-10-30 02:47:38 -0700838 new webrtc::ExperimentalNs(*experimental_ns_));
buildbot@webrtc.org1f8a2372014-08-28 10:52:44 +0000839 }
buildbot@webrtc.org1f8a2372014-08-28 10:52:44 +0000840
841 // We check audioproc for the benefit of tests, since FakeWebRtcVoiceEngine
842 // returns NULL on audio_processing().
843 webrtc::AudioProcessing* audioproc = voe_wrapper_->base()->audio_processing();
844 if (audioproc) {
845 audioproc->SetExtraOptions(config);
846 }
847
kwiberg102c6a62015-10-30 02:47:38 -0700848 if (options.recording_sample_rate) {
849 LOG(LS_INFO) << "Recording sample rate is "
850 << *options.recording_sample_rate;
851 if (voe_wrapper_->hw()->SetRecordingSampleRate(
852 *options.recording_sample_rate)) {
853 LOG_RTCERR1(SetRecordingSampleRate, *options.recording_sample_rate);
buildbot@webrtc.org13d67762014-05-02 17:33:29 +0000854 }
855 }
856
kwiberg102c6a62015-10-30 02:47:38 -0700857 if (options.playout_sample_rate) {
858 LOG(LS_INFO) << "Playout sample rate is " << *options.playout_sample_rate;
859 if (voe_wrapper_->hw()->SetPlayoutSampleRate(
860 *options.playout_sample_rate)) {
861 LOG_RTCERR1(SetPlayoutSampleRate, *options.playout_sample_rate);
buildbot@webrtc.org13d67762014-05-02 17:33:29 +0000862 }
863 }
864
865 return true;
866}
867
solenberg246b8172015-12-08 09:50:23 -0800868void WebRtcVoiceEngine::SetDefaultDevices() {
solenberg566ef242015-11-06 15:34:49 -0800869 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
kjellanderfcfc8042016-01-14 11:01:09 -0800870#if !defined(WEBRTC_IOS)
solenberg246b8172015-12-08 09:50:23 -0800871 int in_id = kDefaultAudioDeviceId;
872 int out_id = kDefaultAudioDeviceId;
873 LOG(LS_INFO) << "Setting microphone to (id=" << in_id
874 << ") and speaker to (id=" << out_id << ")";
buildbot@webrtc.org13d67762014-05-02 17:33:29 +0000875
solenbergc1a1b352015-09-22 13:31:20 -0700876 bool ret = true;
solenberg246b8172015-12-08 09:50:23 -0800877 if (voe_wrapper_->hw()->SetRecordingDevice(in_id) == -1) {
878 LOG_RTCERR1(SetRecordingDevice, in_id);
buildbot@webrtc.org13d67762014-05-02 17:33:29 +0000879 ret = false;
880 }
solenberg246b8172015-12-08 09:50:23 -0800881 webrtc::AudioProcessing* ap = voe()->base()->audio_processing();
882 if (ap) {
883 ap->Initialize();
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000884 }
885
solenberg246b8172015-12-08 09:50:23 -0800886 if (voe_wrapper_->hw()->SetPlayoutDevice(out_id) == -1) {
887 LOG_RTCERR1(SetPlayoutDevice, out_id);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000888 ret = false;
889 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000890
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000891 if (ret) {
solenberg246b8172015-12-08 09:50:23 -0800892 LOG(LS_INFO) << "Set microphone to (id=" << in_id
893 << ") and speaker to (id=" << out_id << ")";
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000894 }
kjellanderfcfc8042016-01-14 11:01:09 -0800895#endif // !WEBRTC_IOS
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000896}
897
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000898bool WebRtcVoiceEngine::GetOutputVolume(int* level) {
solenberg566ef242015-11-06 15:34:49 -0800899 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000900 unsigned int ulevel;
901 if (voe_wrapper_->volume()->GetSpeakerVolume(ulevel) == -1) {
902 LOG_RTCERR1(GetSpeakerVolume, level);
903 return false;
904 }
905 *level = ulevel;
906 return true;
907}
908
909bool WebRtcVoiceEngine::SetOutputVolume(int level) {
solenberg566ef242015-11-06 15:34:49 -0800910 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
henrikg91d6ede2015-09-17 00:24:34 -0700911 RTC_DCHECK(level >= 0 && level <= 255);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000912 if (voe_wrapper_->volume()->SetSpeakerVolume(level) == -1) {
913 LOG_RTCERR1(SetSpeakerVolume, level);
914 return false;
915 }
916 return true;
917}
918
919int WebRtcVoiceEngine::GetInputLevel() {
solenberg566ef242015-11-06 15:34:49 -0800920 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000921 unsigned int ulevel;
922 return (voe_wrapper_->volume()->GetSpeechInputLevel(ulevel) != -1) ?
923 static_cast<int>(ulevel) : -1;
924}
925
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000926const std::vector<AudioCodec>& WebRtcVoiceEngine::codecs() {
solenberg566ef242015-11-06 15:34:49 -0800927 RTC_DCHECK(signal_thread_checker_.CalledOnValidThread());
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000928 return codecs_;
929}
930
Stefan Holmer9d69c3f2015-12-07 10:45:43 +0100931RtpCapabilities WebRtcVoiceEngine::GetCapabilities() const {
solenberg566ef242015-11-06 15:34:49 -0800932 RTC_DCHECK(signal_thread_checker_.CalledOnValidThread());
Stefan Holmer9d69c3f2015-12-07 10:45:43 +0100933 RtpCapabilities capabilities;
934 capabilities.header_extensions.push_back(RtpHeaderExtension(
935 kRtpAudioLevelHeaderExtension, kRtpAudioLevelHeaderExtensionDefaultId));
936 capabilities.header_extensions.push_back(
937 RtpHeaderExtension(kRtpAbsoluteSenderTimeHeaderExtension,
938 kRtpAbsoluteSenderTimeHeaderExtensionDefaultId));
stefanba4c0e42016-02-04 04:12:24 -0800939 if (webrtc::field_trial::FindFullName("WebRTC-Audio-SendSideBwe") ==
940 "Enabled") {
941 capabilities.header_extensions.push_back(RtpHeaderExtension(
942 kRtpTransportSequenceNumberHeaderExtension,
943 kRtpTransportSequenceNumberHeaderExtensionDefaultId));
944 }
Stefan Holmer9d69c3f2015-12-07 10:45:43 +0100945 return capabilities;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000946}
947
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000948int WebRtcVoiceEngine::GetLastEngineError() {
solenberg566ef242015-11-06 15:34:49 -0800949 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000950 return voe_wrapper_->error();
951}
952
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000953void WebRtcVoiceEngine::Print(webrtc::TraceLevel level, const char* trace,
954 int length) {
solenberg566ef242015-11-06 15:34:49 -0800955 // Note: This callback can happen on any thread!
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000956 rtc::LoggingSeverity sev = rtc::LS_VERBOSE;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000957 if (level == webrtc::kTraceError || level == webrtc::kTraceCritical)
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000958 sev = rtc::LS_ERROR;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000959 else if (level == webrtc::kTraceWarning)
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000960 sev = rtc::LS_WARNING;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000961 else if (level == webrtc::kTraceStateInfo || level == webrtc::kTraceInfo)
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000962 sev = rtc::LS_INFO;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000963 else if (level == webrtc::kTraceTerseInfo)
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000964 sev = rtc::LS_INFO;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000965
solenberg72e29d22016-03-08 06:35:16 -0800966 // Skip past boilerplate prefix text.
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000967 if (length < 72) {
968 std::string msg(trace, length);
969 LOG(LS_ERROR) << "Malformed webrtc log message: ";
970 LOG_V(sev) << msg;
971 } else {
972 std::string msg(trace + 71, length - 72);
Peter Boströmd5c75b12015-09-23 13:24:32 +0200973 LOG_V(sev) << "webrtc: " << msg;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000974 }
975}
976
solenberg63b34542015-09-29 06:06:31 -0700977void WebRtcVoiceEngine::RegisterChannel(WebRtcVoiceMediaChannel* channel) {
solenberg566ef242015-11-06 15:34:49 -0800978 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
979 RTC_DCHECK(channel);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000980 channels_.push_back(channel);
981}
982
solenberg63b34542015-09-29 06:06:31 -0700983void WebRtcVoiceEngine::UnregisterChannel(WebRtcVoiceMediaChannel* channel) {
solenberg566ef242015-11-06 15:34:49 -0800984 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
solenberg63b34542015-09-29 06:06:31 -0700985 auto it = std::find(channels_.begin(), channels_.end(), channel);
solenberg566ef242015-11-06 15:34:49 -0800986 RTC_DCHECK(it != channels_.end());
987 channels_.erase(it);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000988}
989
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000990// Adjusts the default AGC target level by the specified delta.
991// NB: If we start messing with other config fields, we'll want
992// to save the current webrtc::AgcConfig as well.
993bool WebRtcVoiceEngine::AdjustAgcLevel(int delta) {
solenberg566ef242015-11-06 15:34:49 -0800994 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000995 webrtc::AgcConfig config = default_agc_config_;
996 config.targetLeveldBOv -= delta;
997
998 LOG(LS_INFO) << "Adjusting AGC level from default -"
999 << default_agc_config_.targetLeveldBOv << "dB to -"
1000 << config.targetLeveldBOv << "dB";
1001
1002 if (voe_wrapper_->processing()->SetAgcConfig(config) == -1) {
1003 LOG_RTCERR1(SetAgcConfig, config.targetLeveldBOv);
1004 return false;
1005 }
1006 return true;
1007}
1008
ivocd66b44d2016-01-15 03:06:36 -08001009bool WebRtcVoiceEngine::StartAecDump(rtc::PlatformFile file,
1010 int64_t max_size_bytes) {
solenberg566ef242015-11-06 15:34:49 -08001011 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00001012 FILE* aec_dump_file_stream = rtc::FdopenPlatformFileForWriting(file);
wu@webrtc.orga8910d22014-01-23 22:12:45 +00001013 if (!aec_dump_file_stream) {
1014 LOG(LS_ERROR) << "Could not open AEC dump file stream.";
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00001015 if (!rtc::ClosePlatformFile(file))
wu@webrtc.orga8910d22014-01-23 22:12:45 +00001016 LOG(LS_WARNING) << "Could not close file.";
1017 return false;
1018 }
wu@webrtc.orga9890802013-12-13 00:21:03 +00001019 StopAecDump();
ivocd66b44d2016-01-15 03:06:36 -08001020 if (voe_wrapper_->base()->audio_processing()->StartDebugRecording(
1021 aec_dump_file_stream, max_size_bytes) !=
wu@webrtc.orga9890802013-12-13 00:21:03 +00001022 webrtc::AudioProcessing::kNoError) {
wu@webrtc.orga8910d22014-01-23 22:12:45 +00001023 LOG_RTCERR0(StartDebugRecording);
1024 fclose(aec_dump_file_stream);
wu@webrtc.orga9890802013-12-13 00:21:03 +00001025 return false;
1026 }
1027 is_dumping_aec_ = true;
1028 return true;
wu@webrtc.orga9890802013-12-13 00:21:03 +00001029}
1030
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001031void WebRtcVoiceEngine::StartAecDump(const std::string& filename) {
solenberg566ef242015-11-06 15:34:49 -08001032 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001033 if (!is_dumping_aec_) {
1034 // Start dumping AEC when we are not dumping.
ivocd66b44d2016-01-15 03:06:36 -08001035 if (voe_wrapper_->base()->audio_processing()->StartDebugRecording(
1036 filename.c_str(), -1) != webrtc::AudioProcessing::kNoError) {
wu@webrtc.orga9890802013-12-13 00:21:03 +00001037 LOG_RTCERR1(StartDebugRecording, filename.c_str());
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001038 } else {
1039 is_dumping_aec_ = true;
1040 }
1041 }
1042}
1043
1044void WebRtcVoiceEngine::StopAecDump() {
solenberg566ef242015-11-06 15:34:49 -08001045 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001046 if (is_dumping_aec_) {
1047 // Stop dumping AEC when we are dumping.
ivocd66b44d2016-01-15 03:06:36 -08001048 if (voe_wrapper_->base()->audio_processing()->StopDebugRecording() !=
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001049 webrtc::AudioProcessing::kNoError) {
1050 LOG_RTCERR0(StopDebugRecording);
1051 }
1052 is_dumping_aec_ = false;
1053 }
1054}
1055
ivoc112a3d82015-10-16 02:22:18 -07001056bool WebRtcVoiceEngine::StartRtcEventLog(rtc::PlatformFile file) {
solenberg566ef242015-11-06 15:34:49 -08001057 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
ivoc20834ca2016-02-04 06:33:37 -08001058 webrtc::RtcEventLog* event_log = voe_wrapper_->codec()->GetEventLog();
1059 if (event_log) {
1060 return event_log->StartLogging(file);
1061 }
1062 LOG_RTCERR0(StartRtcEventLog);
1063 return false;
ivoc112a3d82015-10-16 02:22:18 -07001064}
1065
1066void WebRtcVoiceEngine::StopRtcEventLog() {
solenberg566ef242015-11-06 15:34:49 -08001067 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
ivoc20834ca2016-02-04 06:33:37 -08001068 webrtc::RtcEventLog* event_log = voe_wrapper_->codec()->GetEventLog();
1069 if (event_log) {
1070 event_log->StopLogging();
1071 return;
1072 }
1073 LOG_RTCERR0(StopRtcEventLog);
ivoc112a3d82015-10-16 02:22:18 -07001074}
1075
solenberg0a617e22015-10-20 15:49:38 -07001076int WebRtcVoiceEngine::CreateVoEChannel() {
solenberg566ef242015-11-06 15:34:49 -08001077 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
solenberg0a617e22015-10-20 15:49:38 -07001078 return voe_wrapper_->base()->CreateChannel(voe_config_);
sergeyu@chromium.org5bc25c42013-12-05 00:24:06 +00001079}
1080
solenbergc96df772015-10-21 13:01:53 -07001081class WebRtcVoiceMediaChannel::WebRtcAudioSendStream
Taylor Brandstetter1a018dc2016-03-08 12:37:39 -08001082 : public AudioSource::Sink {
mallinath@webrtc.org67ee6b92014-02-03 16:57:16 +00001083 public:
skvlade0d46372016-04-07 22:59:22 -07001084 WebRtcAudioSendStream(int ch,
1085 webrtc::AudioTransport* voe_audio_transport,
1086 uint32_t ssrc,
1087 const std::string& c_name,
solenberg3a941542015-11-16 07:34:50 -08001088 const std::vector<webrtc::RtpExtension>& extensions,
1089 webrtc::Call* call)
solenberg7add0582015-11-20 09:59:34 -08001090 : voe_audio_transport_(voe_audio_transport),
solenberg3a941542015-11-16 07:34:50 -08001091 call_(call),
skvlade0d46372016-04-07 22:59:22 -07001092 config_(nullptr),
1093 rtp_parameters_(CreateRtpParametersWithOneEncoding()) {
solenberg85a04962015-10-27 03:35:21 -07001094 RTC_DCHECK_GE(ch, 0);
1095 // TODO(solenberg): Once we're not using FakeWebRtcVoiceEngine anymore:
1096 // RTC_DCHECK(voe_audio_transport);
solenbergc96df772015-10-21 13:01:53 -07001097 RTC_DCHECK(call);
solenberg85a04962015-10-27 03:35:21 -07001098 audio_capture_thread_checker_.DetachFromThread();
solenberg3a941542015-11-16 07:34:50 -08001099 config_.rtp.ssrc = ssrc;
1100 config_.rtp.c_name = c_name;
1101 config_.voe_channel_id = ch;
1102 RecreateAudioSendStream(extensions);
solenbergc96df772015-10-21 13:01:53 -07001103 }
solenberg3a941542015-11-16 07:34:50 -08001104
solenbergc96df772015-10-21 13:01:53 -07001105 ~WebRtcAudioSendStream() override {
solenberg566ef242015-11-06 15:34:49 -08001106 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
Taylor Brandstetter1a018dc2016-03-08 12:37:39 -08001107 ClearSource();
solenbergc96df772015-10-21 13:01:53 -07001108 call_->DestroyAudioSendStream(stream_);
1109 }
henrike@webrtc.org1e09a712013-07-26 19:17:59 +00001110
solenberg3a941542015-11-16 07:34:50 -08001111 void RecreateAudioSendStream(
1112 const std::vector<webrtc::RtpExtension>& extensions) {
1113 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
1114 if (stream_) {
1115 call_->DestroyAudioSendStream(stream_);
1116 stream_ = nullptr;
1117 }
1118 config_.rtp.extensions = extensions;
1119 RTC_DCHECK(!stream_);
1120 stream_ = call_->CreateAudioSendStream(config_);
1121 RTC_CHECK(stream_);
1122 }
1123
solenberg8842c3e2016-03-11 03:06:41 -08001124 bool SendTelephoneEvent(int payload_type, int event, int duration_ms) {
Fredrik Solenbergb5727682015-12-04 15:22:19 +01001125 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
1126 RTC_DCHECK(stream_);
1127 return stream_->SendTelephoneEvent(payload_type, event, duration_ms);
1128 }
1129
Taylor Brandstetter1a018dc2016-03-08 12:37:39 -08001130 void SetSend(bool send) {
1131 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
1132 send_ = send;
1133 UpdateSendState();
1134 }
1135
solenberg3a941542015-11-16 07:34:50 -08001136 webrtc::AudioSendStream::Stats GetStats() const {
1137 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
1138 RTC_DCHECK(stream_);
1139 return stream_->GetStats();
1140 }
1141
Taylor Brandstetter1a018dc2016-03-08 12:37:39 -08001142 // Starts the sending by setting ourselves as a sink to the AudioSource to
1143 // get data callbacks.
henrike@webrtc.orga7b98182014-02-21 15:51:43 +00001144 // This method is called on the libjingle worker thread.
mallinath@webrtc.org67ee6b92014-02-03 16:57:16 +00001145 // TODO(xians): Make sure Start() is called only once.
Taylor Brandstetter1a018dc2016-03-08 12:37:39 -08001146 void SetSource(AudioSource* source) {
solenberg566ef242015-11-06 15:34:49 -08001147 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
Taylor Brandstetter1a018dc2016-03-08 12:37:39 -08001148 RTC_DCHECK(source);
1149 if (source_) {
1150 RTC_DCHECK(source_ == source);
mallinath@webrtc.org67ee6b92014-02-03 16:57:16 +00001151 return;
1152 }
Taylor Brandstetter1a018dc2016-03-08 12:37:39 -08001153 source->SetSink(this);
1154 source_ = source;
1155 UpdateSendState();
mallinath@webrtc.org67ee6b92014-02-03 16:57:16 +00001156 }
1157
Taylor Brandstetter1a018dc2016-03-08 12:37:39 -08001158 // Stops sending by setting the sink of the AudioSource to nullptr. No data
mallinath@webrtc.org67ee6b92014-02-03 16:57:16 +00001159 // callback will be received after this method.
henrike@webrtc.orga7b98182014-02-21 15:51:43 +00001160 // This method is called on the libjingle worker thread.
Taylor Brandstetter1a018dc2016-03-08 12:37:39 -08001161 void ClearSource() {
solenberg566ef242015-11-06 15:34:49 -08001162 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
Taylor Brandstetter1a018dc2016-03-08 12:37:39 -08001163 if (source_) {
1164 source_->SetSink(nullptr);
1165 source_ = nullptr;
solenberg98c68862015-10-09 03:27:14 -07001166 }
Taylor Brandstetter1a018dc2016-03-08 12:37:39 -08001167 UpdateSendState();
mallinath@webrtc.org67ee6b92014-02-03 16:57:16 +00001168 }
1169
Taylor Brandstetter1a018dc2016-03-08 12:37:39 -08001170 // AudioSource::Sink implementation.
henrike@webrtc.orga7b98182014-02-21 15:51:43 +00001171 // This method is called on the audio thread.
kjellander@webrtc.org14665ff2015-03-04 12:58:35 +00001172 void OnData(const void* audio_data,
1173 int bits_per_sample,
1174 int sample_rate,
Peter Kasting69558702016-01-12 16:26:35 -08001175 size_t number_of_channels,
Peter Kastingdce40cf2015-08-24 14:52:23 -07001176 size_t number_of_frames) override {
solenberg566ef242015-11-06 15:34:49 -08001177 RTC_DCHECK(!worker_thread_checker_.CalledOnValidThread());
solenberg85a04962015-10-27 03:35:21 -07001178 RTC_DCHECK(audio_capture_thread_checker_.CalledOnValidThread());
solenbergc96df772015-10-21 13:01:53 -07001179 RTC_DCHECK(voe_audio_transport_);
solenberg7add0582015-11-20 09:59:34 -08001180 voe_audio_transport_->OnData(config_.voe_channel_id,
henrike@webrtc.orga7b98182014-02-21 15:51:43 +00001181 audio_data,
1182 bits_per_sample,
1183 sample_rate,
1184 number_of_channels,
1185 number_of_frames);
henrike@webrtc.orga7b98182014-02-21 15:51:43 +00001186 }
1187
Taylor Brandstetter1a018dc2016-03-08 12:37:39 -08001188 // Callback from the |source_| when it is going away. In case Start() has
henrike@webrtc.orga7b98182014-02-21 15:51:43 +00001189 // never been called, this callback won't be triggered.
kjellander@webrtc.org14665ff2015-03-04 12:58:35 +00001190 void OnClose() override {
solenberg566ef242015-11-06 15:34:49 -08001191 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
Taylor Brandstetter1a018dc2016-03-08 12:37:39 -08001192 // Set |source_| to nullptr to make sure no more callback will get into
1193 // the source.
1194 source_ = nullptr;
1195 UpdateSendState();
mallinath@webrtc.org67ee6b92014-02-03 16:57:16 +00001196 }
1197
1198 // Accessor to the VoE channel ID.
solenberg85a04962015-10-27 03:35:21 -07001199 int channel() const {
solenberg566ef242015-11-06 15:34:49 -08001200 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
solenberg7add0582015-11-20 09:59:34 -08001201 return config_.voe_channel_id;
solenberg85a04962015-10-27 03:35:21 -07001202 }
mallinath@webrtc.org67ee6b92014-02-03 16:57:16 +00001203
skvlade0d46372016-04-07 22:59:22 -07001204 const webrtc::RtpParameters& rtp_parameters() const {
1205 return rtp_parameters_;
1206 }
1207
1208 void set_rtp_parameters(const webrtc::RtpParameters& parameters) {
1209 RTC_CHECK_EQ(1UL, parameters.encodings.size());
1210 rtp_parameters_ = parameters;
1211 }
1212
mallinath@webrtc.org67ee6b92014-02-03 16:57:16 +00001213 private:
Taylor Brandstetter1a018dc2016-03-08 12:37:39 -08001214 void UpdateSendState() {
1215 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
1216 RTC_DCHECK(stream_);
1217 if (send_ && source_ != nullptr) {
1218 stream_->Start();
1219 } else { // !send || source_ = nullptr
1220 stream_->Stop();
1221 }
1222 }
1223
solenberg566ef242015-11-06 15:34:49 -08001224 rtc::ThreadChecker worker_thread_checker_;
solenberg85a04962015-10-27 03:35:21 -07001225 rtc::ThreadChecker audio_capture_thread_checker_;
solenbergc96df772015-10-21 13:01:53 -07001226 webrtc::AudioTransport* const voe_audio_transport_ = nullptr;
1227 webrtc::Call* call_ = nullptr;
solenberg3a941542015-11-16 07:34:50 -08001228 webrtc::AudioSendStream::Config config_;
1229 // The stream is owned by WebRtcAudioSendStream and may be reallocated if
1230 // configuration changes.
solenbergc96df772015-10-21 13:01:53 -07001231 webrtc::AudioSendStream* stream_ = nullptr;
mallinath@webrtc.org67ee6b92014-02-03 16:57:16 +00001232
Taylor Brandstetter1a018dc2016-03-08 12:37:39 -08001233 // Raw pointer to AudioSource owned by LocalAudioTrackHandler.
mallinath@webrtc.org67ee6b92014-02-03 16:57:16 +00001234 // PeerConnection will make sure invalidating the pointer before the object
1235 // goes away.
Taylor Brandstetter1a018dc2016-03-08 12:37:39 -08001236 AudioSource* source_ = nullptr;
1237 bool send_ = false;
skvlade0d46372016-04-07 22:59:22 -07001238 webrtc::RtpParameters rtp_parameters_;
henrike@webrtc.orga7b98182014-02-21 15:51:43 +00001239
solenbergc96df772015-10-21 13:01:53 -07001240 RTC_DISALLOW_IMPLICIT_CONSTRUCTORS(WebRtcAudioSendStream);
1241};
1242
1243class WebRtcVoiceMediaChannel::WebRtcAudioReceiveStream {
1244 public:
stefanba4c0e42016-02-04 04:12:24 -08001245 WebRtcAudioReceiveStream(int ch,
1246 uint32_t remote_ssrc,
1247 uint32_t local_ssrc,
1248 bool use_transport_cc,
1249 const std::string& sync_group,
solenberg7add0582015-11-20 09:59:34 -08001250 const std::vector<webrtc::RtpExtension>& extensions,
1251 webrtc::Call* call)
stefanba4c0e42016-02-04 04:12:24 -08001252 : call_(call), config_() {
solenberg7add0582015-11-20 09:59:34 -08001253 RTC_DCHECK_GE(ch, 0);
1254 RTC_DCHECK(call);
1255 config_.rtp.remote_ssrc = remote_ssrc;
1256 config_.rtp.local_ssrc = local_ssrc;
1257 config_.voe_channel_id = ch;
1258 config_.sync_group = sync_group;
stefanba4c0e42016-02-04 04:12:24 -08001259 RecreateAudioReceiveStream(use_transport_cc, extensions);
solenberg7add0582015-11-20 09:59:34 -08001260 }
solenbergc96df772015-10-21 13:01:53 -07001261
solenberg7add0582015-11-20 09:59:34 -08001262 ~WebRtcAudioReceiveStream() {
1263 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
1264 call_->DestroyAudioReceiveStream(stream_);
1265 }
1266
1267 void RecreateAudioReceiveStream(
1268 const std::vector<webrtc::RtpExtension>& extensions) {
1269 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
stefanba4c0e42016-02-04 04:12:24 -08001270 RecreateAudioReceiveStream(config_.rtp.transport_cc, extensions);
solenberg7add0582015-11-20 09:59:34 -08001271 }
stefanba4c0e42016-02-04 04:12:24 -08001272 void RecreateAudioReceiveStream(bool use_transport_cc) {
solenberg7add0582015-11-20 09:59:34 -08001273 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
stefanba4c0e42016-02-04 04:12:24 -08001274 RecreateAudioReceiveStream(use_transport_cc, config_.rtp.extensions);
solenberg7add0582015-11-20 09:59:34 -08001275 }
1276
1277 webrtc::AudioReceiveStream::Stats GetStats() const {
1278 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
1279 RTC_DCHECK(stream_);
1280 return stream_->GetStats();
1281 }
1282
1283 int channel() const {
1284 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
1285 return config_.voe_channel_id;
1286 }
solenbergc96df772015-10-21 13:01:53 -07001287
kwiberg686a8ef2016-02-26 03:00:35 -08001288 void SetRawAudioSink(std::unique_ptr<webrtc::AudioSinkInterface> sink) {
Tommif888bb52015-12-12 01:37:01 +01001289 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
kwiberg686a8ef2016-02-26 03:00:35 -08001290 stream_->SetSink(std::move(sink));
Tommif888bb52015-12-12 01:37:01 +01001291 }
1292
solenbergc96df772015-10-21 13:01:53 -07001293 private:
stefanba4c0e42016-02-04 04:12:24 -08001294 void RecreateAudioReceiveStream(
1295 bool use_transport_cc,
solenberg7add0582015-11-20 09:59:34 -08001296 const std::vector<webrtc::RtpExtension>& extensions) {
1297 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
1298 if (stream_) {
1299 call_->DestroyAudioReceiveStream(stream_);
1300 stream_ = nullptr;
1301 }
1302 config_.rtp.extensions = extensions;
stefanba4c0e42016-02-04 04:12:24 -08001303 config_.rtp.transport_cc = use_transport_cc;
solenberg7add0582015-11-20 09:59:34 -08001304 RTC_DCHECK(!stream_);
1305 stream_ = call_->CreateAudioReceiveStream(config_);
1306 RTC_CHECK(stream_);
1307 }
1308
1309 rtc::ThreadChecker worker_thread_checker_;
1310 webrtc::Call* call_ = nullptr;
1311 webrtc::AudioReceiveStream::Config config_;
1312 // The stream is owned by WebRtcAudioReceiveStream and may be reallocated if
1313 // configuration changes.
1314 webrtc::AudioReceiveStream* stream_ = nullptr;
solenbergc96df772015-10-21 13:01:53 -07001315
1316 RTC_DISALLOW_IMPLICIT_CONSTRUCTORS(WebRtcAudioReceiveStream);
henrike@webrtc.org1e09a712013-07-26 19:17:59 +00001317};
1318
Fredrik Solenberg709ed672015-09-15 12:26:33 +02001319WebRtcVoiceMediaChannel::WebRtcVoiceMediaChannel(WebRtcVoiceEngine* engine,
nisse51542be2016-02-12 02:27:06 -08001320 const MediaConfig& config,
Fredrik Solenbergb071a192015-09-17 16:42:56 +02001321 const AudioOptions& options,
Fredrik Solenberg709ed672015-09-15 12:26:33 +02001322 webrtc::Call* call)
nisse51542be2016-02-12 02:27:06 -08001323 : VoiceMediaChannel(config), engine_(engine), call_(call) {
solenberg0a617e22015-10-20 15:49:38 -07001324 LOG(LS_VERBOSE) << "WebRtcVoiceMediaChannel::WebRtcVoiceMediaChannel";
solenberg566ef242015-11-06 15:34:49 -08001325 RTC_DCHECK(call);
solenberg0a617e22015-10-20 15:49:38 -07001326 engine->RegisterChannel(this);
Fredrik Solenbergb071a192015-09-17 16:42:56 +02001327 SetOptions(options);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001328}
1329
1330WebRtcVoiceMediaChannel::~WebRtcVoiceMediaChannel() {
solenberg566ef242015-11-06 15:34:49 -08001331 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
solenberg0a617e22015-10-20 15:49:38 -07001332 LOG(LS_VERBOSE) << "WebRtcVoiceMediaChannel::~WebRtcVoiceMediaChannel";
solenberg7add0582015-11-20 09:59:34 -08001333 // TODO(solenberg): Should be able to delete the streams directly, without
1334 // going through RemoveNnStream(), once stream objects handle
1335 // all (de)configuration.
solenbergc96df772015-10-21 13:01:53 -07001336 while (!send_streams_.empty()) {
1337 RemoveSendStream(send_streams_.begin()->first);
solenbergd97ec302015-10-07 01:40:33 -07001338 }
solenberg7add0582015-11-20 09:59:34 -08001339 while (!recv_streams_.empty()) {
1340 RemoveRecvStream(recv_streams_.begin()->first);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001341 }
solenberg0a617e22015-10-20 15:49:38 -07001342 engine()->UnregisterChannel(this);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001343}
1344
nisse51542be2016-02-12 02:27:06 -08001345rtc::DiffServCodePoint WebRtcVoiceMediaChannel::PreferredDscp() const {
1346 return kAudioDscpValue;
1347}
1348
Peter Thatcherc2ee2c82015-08-07 16:05:34 -07001349bool WebRtcVoiceMediaChannel::SetSendParameters(
1350 const AudioSendParameters& params) {
Peter Boströmca8b4042016-03-08 14:24:13 -08001351 TRACE_EVENT0("webrtc", "WebRtcVoiceMediaChannel::SetSendParameters");
solenberg566ef242015-11-06 15:34:49 -08001352 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
solenberg7e4e01a2015-12-02 08:05:01 -08001353 LOG(LS_INFO) << "WebRtcVoiceMediaChannel::SetSendParameters: "
1354 << params.ToString();
Peter Thatcherc2ee2c82015-08-07 16:05:34 -07001355 // TODO(pthatcher): Refactor this to be more clean now that we have
1356 // all the information at once.
solenberg3a941542015-11-16 07:34:50 -08001357
1358 if (!SetSendCodecs(params.codecs)) {
1359 return false;
1360 }
1361
solenberg7e4e01a2015-12-02 08:05:01 -08001362 if (!ValidateRtpExtensions(params.extensions)) {
1363 return false;
1364 }
1365 std::vector<webrtc::RtpExtension> filtered_extensions =
1366 FilterRtpExtensions(params.extensions,
1367 webrtc::RtpExtension::IsSupportedForAudio, true);
1368 if (send_rtp_extensions_ != filtered_extensions) {
1369 send_rtp_extensions_.swap(filtered_extensions);
solenberg3a941542015-11-16 07:34:50 -08001370 for (auto& it : send_streams_) {
1371 it.second->RecreateAudioSendStream(send_rtp_extensions_);
1372 }
1373 }
1374
skvlade0d46372016-04-07 22:59:22 -07001375 if (!SetSendBitrate(params.max_bandwidth_bps)) {
solenberg3a941542015-11-16 07:34:50 -08001376 return false;
1377 }
1378 return SetOptions(params.options);
Peter Thatcherc2ee2c82015-08-07 16:05:34 -07001379}
1380
1381bool WebRtcVoiceMediaChannel::SetRecvParameters(
1382 const AudioRecvParameters& params) {
Peter Boströmca8b4042016-03-08 14:24:13 -08001383 TRACE_EVENT0("webrtc", "WebRtcVoiceMediaChannel::SetRecvParameters");
solenberg566ef242015-11-06 15:34:49 -08001384 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
solenberg7e4e01a2015-12-02 08:05:01 -08001385 LOG(LS_INFO) << "WebRtcVoiceMediaChannel::SetRecvParameters: "
1386 << params.ToString();
Peter Thatcherc2ee2c82015-08-07 16:05:34 -07001387 // TODO(pthatcher): Refactor this to be more clean now that we have
1388 // all the information at once.
solenberg7add0582015-11-20 09:59:34 -08001389
1390 if (!SetRecvCodecs(params.codecs)) {
1391 return false;
1392 }
1393
solenberg7e4e01a2015-12-02 08:05:01 -08001394 if (!ValidateRtpExtensions(params.extensions)) {
1395 return false;
1396 }
1397 std::vector<webrtc::RtpExtension> filtered_extensions =
1398 FilterRtpExtensions(params.extensions,
1399 webrtc::RtpExtension::IsSupportedForAudio, false);
1400 if (recv_rtp_extensions_ != filtered_extensions) {
1401 recv_rtp_extensions_.swap(filtered_extensions);
solenberg7add0582015-11-20 09:59:34 -08001402 for (auto& it : recv_streams_) {
1403 it.second->RecreateAudioReceiveStream(recv_rtp_extensions_);
1404 }
1405 }
solenberg7add0582015-11-20 09:59:34 -08001406 return true;
Peter Thatcherc2ee2c82015-08-07 16:05:34 -07001407}
1408
skvlade0d46372016-04-07 22:59:22 -07001409webrtc::RtpParameters WebRtcVoiceMediaChannel::GetRtpParameters(
1410 uint32_t ssrc) const {
1411 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
1412 auto it = send_streams_.find(ssrc);
1413 if (it == send_streams_.end()) {
1414 LOG(LS_WARNING) << "Attempting to get RTP parameters for stream with ssrc "
1415 << ssrc << " which doesn't exist.";
1416 return webrtc::RtpParameters();
1417 }
1418
1419 return it->second->rtp_parameters();
1420}
1421
1422bool WebRtcVoiceMediaChannel::SetRtpParameters(
1423 uint32_t ssrc,
1424 const webrtc::RtpParameters& parameters) {
1425 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
1426 if (!ValidateRtpParameters(parameters)) {
1427 return false;
1428 }
1429 auto it = send_streams_.find(ssrc);
1430 if (it == send_streams_.end()) {
1431 LOG(LS_WARNING) << "Attempting to set RTP parameters for stream with ssrc "
1432 << ssrc << " which doesn't exist.";
1433 return false;
1434 }
1435
1436 if (!SetChannelParameters(it->second->channel(), parameters)) {
1437 LOG(LS_WARNING) << "Failed to set RtpParameters.";
1438 return false;
1439 }
1440 it->second->set_rtp_parameters(parameters);
1441 return true;
1442}
1443
1444bool WebRtcVoiceMediaChannel::ValidateRtpParameters(
1445 const webrtc::RtpParameters& rtp_parameters) {
1446 if (rtp_parameters.encodings.size() != 1) {
1447 LOG(LS_ERROR)
1448 << "Attempted to set RtpParameters without exactly one encoding";
1449 return false;
1450 }
1451 return true;
1452}
1453
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001454bool WebRtcVoiceMediaChannel::SetOptions(const AudioOptions& options) {
solenberg566ef242015-11-06 15:34:49 -08001455 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001456 LOG(LS_INFO) << "Setting voice channel options: "
1457 << options.ToString();
1458
1459 // We retain all of the existing options, and apply the given ones
1460 // on top. This means there is no way to "clear" options such that
1461 // they go back to the engine default.
1462 options_.SetAll(options);
solenberg246b8172015-12-08 09:50:23 -08001463 if (!engine()->ApplyOptions(options_)) {
1464 LOG(LS_WARNING) <<
1465 "Failed to apply engine options during channel SetOptions.";
1466 return false;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001467 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001468 LOG(LS_INFO) << "Set voice channel options. Current options: "
1469 << options_.ToString();
1470 return true;
1471}
1472
1473bool WebRtcVoiceMediaChannel::SetRecvCodecs(
1474 const std::vector<AudioCodec>& codecs) {
solenberg566ef242015-11-06 15:34:49 -08001475 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
solenberg8fb30c32015-10-13 03:06:58 -07001476
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001477 // Set the payload types to be used for incoming media.
solenberg0b675462015-10-09 01:37:09 -07001478 LOG(LS_INFO) << "Setting receive voice codecs.";
solenberg0b675462015-10-09 01:37:09 -07001479
1480 if (!VerifyUniquePayloadTypes(codecs)) {
1481 LOG(LS_ERROR) << "Codec payload types overlap.";
1482 return false;
1483 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001484
1485 std::vector<AudioCodec> new_codecs;
1486 // Find all new codecs. We allow adding new codecs but don't allow changing
1487 // the payload type of codecs that is already configured since we might
1488 // already be receiving packets with that payload type.
Fredrik Solenbergaf9fb212015-08-26 10:45:53 +02001489 for (const AudioCodec& codec : codecs) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001490 AudioCodec old_codec;
Fredrik Solenbergaf9fb212015-08-26 10:45:53 +02001491 if (FindCodec(recv_codecs_, codec, &old_codec)) {
1492 if (old_codec.id != codec.id) {
1493 LOG(LS_ERROR) << codec.name << " payload type changed.";
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001494 return false;
1495 }
1496 } else {
Fredrik Solenbergaf9fb212015-08-26 10:45:53 +02001497 new_codecs.push_back(codec);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001498 }
1499 }
1500 if (new_codecs.empty()) {
1501 // There are no new codecs to configure. Already configured codecs are
1502 // never removed.
1503 return true;
1504 }
1505
1506 if (playout_) {
1507 // Receive codecs can not be changed while playing. So we temporarily
1508 // pause playout.
1509 PausePlayout();
1510 }
1511
solenberg26c8c912015-11-27 04:00:25 -08001512 bool result = true;
1513 for (const AudioCodec& codec : new_codecs) {
solenberg72e29d22016-03-08 06:35:16 -08001514 webrtc::CodecInst voe_codec = {0};
solenberg26c8c912015-11-27 04:00:25 -08001515 if (WebRtcVoiceEngine::ToCodecInst(codec, &voe_codec)) {
1516 LOG(LS_INFO) << ToString(codec);
1517 voe_codec.pltype = codec.id;
1518 for (const auto& ch : recv_streams_) {
1519 if (engine()->voe()->codec()->SetRecPayloadType(
1520 ch.second->channel(), voe_codec) == -1) {
1521 LOG_RTCERR2(SetRecPayloadType, ch.second->channel(),
1522 ToString(voe_codec));
1523 result = false;
1524 }
1525 }
1526 } else {
1527 LOG(LS_WARNING) << "Unknown codec " << ToString(codec);
1528 result = false;
1529 break;
1530 }
1531 }
Fredrik Solenbergaf9fb212015-08-26 10:45:53 +02001532 if (result) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001533 recv_codecs_ = codecs;
1534 }
1535
1536 if (desired_playout_ && !playout_) {
1537 ResumePlayout();
1538 }
Fredrik Solenbergaf9fb212015-08-26 10:45:53 +02001539 return result;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001540}
1541
solenberg72e29d22016-03-08 06:35:16 -08001542// Utility function called from SetSendParameters() to extract current send
1543// codec settings from the given list of codecs (originally from SDP). Both send
1544// and receive streams may be reconfigured based on the new settings.
wu@webrtc.orgcadf9042013-08-30 21:24:16 +00001545bool WebRtcVoiceMediaChannel::SetSendCodecs(
1546 const std::vector<AudioCodec>& codecs) {
solenberg566ef242015-11-06 15:34:49 -08001547 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
Fredrik Solenbergb5727682015-12-04 15:22:19 +01001548 // TODO(solenberg): Validate input - that payload types don't overlap, are
1549 // within range, filter out codecs we don't support,
solenberg31642aa2016-03-14 08:00:37 -07001550 // redundant codecs etc - the same way it is done for
1551 // RtpHeaderExtensions.
solenbergd97ec302015-10-07 01:40:33 -07001552
Fredrik Solenbergb5727682015-12-04 15:22:19 +01001553 // Find the DTMF telephone event "codec" payload type.
1554 dtmf_payload_type_ = rtc::Optional<int>();
Fredrik Solenbergaf9fb212015-08-26 10:45:53 +02001555 for (const AudioCodec& codec : codecs) {
Fredrik Solenbergaf9fb212015-08-26 10:45:53 +02001556 if (IsCodec(codec, kDtmfCodecName)) {
solenberg31642aa2016-03-14 08:00:37 -07001557 if (codec.id < kMinPayloadType || codec.id > kMaxPayloadType) {
1558 return false;
1559 }
Fredrik Solenbergb5727682015-12-04 15:22:19 +01001560 dtmf_payload_type_ = rtc::Optional<int>(codec.id);
1561 break;
wu@webrtc.orgcadf9042013-08-30 21:24:16 +00001562 }
1563 }
1564
solenberg72e29d22016-03-08 06:35:16 -08001565 // Scan through the list to figure out the codec to use for sending, along
1566 // with the proper configuration for VAD, CNG, RED, NACK and Opus-specific
1567 // parameters.
1568 {
1569 SendCodecSpec send_codec_spec;
1570 send_codec_spec.nack_enabled = send_codec_spec_.nack_enabled;
1571
1572 // Find send codec (the first non-telephone-event/CN codec).
1573 const AudioCodec* codec = WebRtcVoiceCodecs::GetPreferredCodec(
1574 codecs, &send_codec_spec.codec_inst, &send_codec_spec.red_payload_type);
1575 if (!codec) {
1576 LOG(LS_WARNING) << "Received empty list of codecs.";
1577 return false;
1578 }
1579
1580 send_codec_spec.transport_cc_enabled = HasTransportCc(*codec);
1581
1582 // This condition is apparently here because Opus does not support RED and
1583 // FEC simultaneously. However, DTX and max playback rate shouldn't have
1584 // such limitations.
1585 // TODO(solenberg): Refactor this logic once we create AudioEncoders here.
1586 if (send_codec_spec.red_payload_type == -1) {
1587 send_codec_spec.nack_enabled = HasNack(*codec);
1588 // For Opus as the send codec, we are to determine inband FEC, maximum
1589 // playback rate, and opus internal dtx.
1590 if (IsCodec(*codec, kOpusCodecName)) {
1591 GetOpusConfig(*codec, &send_codec_spec.codec_inst,
1592 &send_codec_spec.enable_codec_fec,
1593 &send_codec_spec.opus_max_playback_rate,
1594 &send_codec_spec.enable_opus_dtx);
1595 }
1596
1597 // Set packet size if the AudioCodec param kCodecParamPTime is set.
1598 int ptime_ms = 0;
1599 if (codec->GetParam(kCodecParamPTime, &ptime_ms)) {
1600 if (!WebRtcVoiceCodecs::SetPTimeAsPacketSize(
1601 &send_codec_spec.codec_inst, ptime_ms)) {
1602 LOG(LS_WARNING) << "Failed to set packet size for codec "
1603 << send_codec_spec.codec_inst.plname;
1604 return false;
1605 }
1606 }
1607 }
1608
1609 // Loop through the codecs list again to find the CN codec.
1610 // TODO(solenberg): Break out into a separate function?
1611 for (const AudioCodec& codec : codecs) {
1612 // Ignore codecs we don't know about. The negotiation step should prevent
1613 // this, but double-check to be sure.
1614 webrtc::CodecInst voe_codec = {0};
1615 if (!WebRtcVoiceEngine::ToCodecInst(codec, &voe_codec)) {
1616 LOG(LS_WARNING) << "Unknown codec " << ToString(codec);
1617 continue;
1618 }
1619
1620 if (IsCodec(codec, kCnCodecName)) {
1621 // Turn voice activity detection/comfort noise on if supported.
1622 // Set the wideband CN payload type appropriately.
1623 // (narrowband always uses the static payload type 13).
1624 int cng_plfreq = -1;
1625 switch (codec.clockrate) {
1626 case 8000:
1627 case 16000:
1628 case 32000:
1629 cng_plfreq = codec.clockrate;
1630 break;
1631 default:
1632 LOG(LS_WARNING) << "CN frequency " << codec.clockrate
1633 << " not supported.";
1634 continue;
1635 }
1636 send_codec_spec.cng_payload_type = codec.id;
1637 send_codec_spec.cng_plfreq = cng_plfreq;
1638 break;
1639 }
1640 }
1641
1642 // Latch in the new state.
1643 send_codec_spec_ = std::move(send_codec_spec);
1644 }
1645
wu@webrtc.orgcadf9042013-08-30 21:24:16 +00001646 // Cache the codecs in order to configure the channel created later.
solenbergc96df772015-10-21 13:01:53 -07001647 for (const auto& ch : send_streams_) {
skvlade0d46372016-04-07 22:59:22 -07001648 if (!SetSendCodecs(ch.second->channel(), ch.second->rtp_parameters())) {
wu@webrtc.orgcadf9042013-08-30 21:24:16 +00001649 return false;
1650 }
1651 }
1652
solenberg72e29d22016-03-08 06:35:16 -08001653 // Set nack status on receive channels.
1654 if (!send_streams_.empty()) {
1655 for (const auto& kv : recv_streams_) {
1656 SetNack(kv.second->channel(), send_codec_spec_.nack_enabled);
1657 }
wu@webrtc.org9dba5252013-08-05 20:36:57 +00001658 }
solenberg0a617e22015-10-20 15:49:38 -07001659
stefanba4c0e42016-02-04 04:12:24 -08001660 // Check if the transport cc feedback has changed on the preferred send codec,
1661 // and in that case reconfigure all receive streams.
solenberg72e29d22016-03-08 06:35:16 -08001662 if (recv_transport_cc_enabled_ != send_codec_spec_.transport_cc_enabled) {
1663 LOG(LS_INFO) << "Recreate all the receive streams because the send "
1664 "codec has changed.";
1665 recv_transport_cc_enabled_ = send_codec_spec_.transport_cc_enabled;
1666 for (auto& kv : recv_streams_) {
1667 kv.second->RecreateAudioReceiveStream(recv_transport_cc_enabled_);
1668 }
1669 }
1670
1671 return true;
1672}
1673
1674// Apply current codec settings to a single voe::Channel used for sending.
skvlade0d46372016-04-07 22:59:22 -07001675bool WebRtcVoiceMediaChannel::SetSendCodecs(
1676 int channel,
1677 const webrtc::RtpParameters& rtp_parameters) {
solenberg72e29d22016-03-08 06:35:16 -08001678 // Disable VAD, FEC, and RED unless we know the other side wants them.
1679 engine()->voe()->codec()->SetVADStatus(channel, false);
1680 engine()->voe()->rtp()->SetNACKStatus(channel, false, 0);
1681 engine()->voe()->rtp()->SetREDStatus(channel, false);
1682 engine()->voe()->codec()->SetFECStatus(channel, false);
1683
1684 if (send_codec_spec_.red_payload_type != -1) {
1685 // Enable redundant encoding of the specified codec. Treat any
1686 // failure as a fatal internal error.
1687 LOG(LS_INFO) << "Enabling RED on channel " << channel;
1688 if (engine()->voe()->rtp()->SetREDStatus(channel, true,
1689 send_codec_spec_.red_payload_type) == -1) {
1690 LOG_RTCERR3(SetREDStatus, channel, true,
1691 send_codec_spec_.red_payload_type);
1692 return false;
1693 }
1694 }
1695
1696 SetNack(channel, send_codec_spec_.nack_enabled);
1697
1698 // Set the codec immediately, since SetVADStatus() depends on whether
1699 // the current codec is mono or stereo.
1700 if (!SetSendCodec(channel, send_codec_spec_.codec_inst)) {
1701 return false;
1702 }
1703
1704 // FEC should be enabled after SetSendCodec.
1705 if (send_codec_spec_.enable_codec_fec) {
1706 LOG(LS_INFO) << "Attempt to enable codec internal FEC on channel "
1707 << channel;
1708 if (engine()->voe()->codec()->SetFECStatus(channel, true) == -1) {
1709 // Enable codec internal FEC. Treat any failure as fatal internal error.
1710 LOG_RTCERR2(SetFECStatus, channel, true);
1711 return false;
1712 }
1713 }
1714
1715 if (IsCodec(send_codec_spec_.codec_inst, kOpusCodecName)) {
1716 // DTX and maxplaybackrate should be set after SetSendCodec. Because current
1717 // send codec has to be Opus.
1718
1719 // Set Opus internal DTX.
1720 LOG(LS_INFO) << "Attempt to "
1721 << (send_codec_spec_.enable_opus_dtx ? "enable" : "disable")
1722 << " Opus DTX on channel "
1723 << channel;
1724 if (engine()->voe()->codec()->SetOpusDtx(channel,
1725 send_codec_spec_.enable_opus_dtx)) {
1726 LOG_RTCERR2(SetOpusDtx, channel, send_codec_spec_.enable_opus_dtx);
1727 return false;
1728 }
1729
1730 // If opus_max_playback_rate <= 0, the default maximum playback rate
1731 // (48 kHz) will be used.
1732 if (send_codec_spec_.opus_max_playback_rate > 0) {
1733 LOG(LS_INFO) << "Attempt to set maximum playback rate to "
1734 << send_codec_spec_.opus_max_playback_rate
1735 << " Hz on channel "
1736 << channel;
1737 if (engine()->voe()->codec()->SetOpusMaxPlaybackRate(
1738 channel, send_codec_spec_.opus_max_playback_rate) == -1) {
1739 LOG_RTCERR2(SetOpusMaxPlaybackRate, channel,
1740 send_codec_spec_.opus_max_playback_rate);
1741 return false;
stefanba4c0e42016-02-04 04:12:24 -08001742 }
1743 }
1744 }
skvlade0d46372016-04-07 22:59:22 -07001745 // TODO(solenberg): SetSendBitrate() yields another call to SetSendCodec().
1746 // Check if it is possible to fuse with the previous call in this function.
1747 SetChannelParameters(channel, rtp_parameters);
solenberg72e29d22016-03-08 06:35:16 -08001748
1749 // Set the CN payloadtype and the VAD status.
1750 if (send_codec_spec_.cng_payload_type != -1) {
1751 // The CN payload type for 8000 Hz clockrate is fixed at 13.
1752 if (send_codec_spec_.cng_plfreq != 8000) {
1753 webrtc::PayloadFrequencies cn_freq;
1754 switch (send_codec_spec_.cng_plfreq) {
1755 case 16000:
1756 cn_freq = webrtc::kFreq16000Hz;
1757 break;
1758 case 32000:
1759 cn_freq = webrtc::kFreq32000Hz;
1760 break;
1761 default:
1762 RTC_NOTREACHED();
1763 return false;
1764 }
1765 if (engine()->voe()->codec()->SetSendCNPayloadType(
1766 channel, send_codec_spec_.cng_payload_type, cn_freq) == -1) {
1767 LOG_RTCERR3(SetSendCNPayloadType, channel,
1768 send_codec_spec_.cng_payload_type, cn_freq);
1769 // TODO(ajm): This failure condition will be removed from VoE.
1770 // Restore the return here when we update to a new enough webrtc.
1771 //
1772 // Not returning false because the SetSendCNPayloadType will fail if
1773 // the channel is already sending.
1774 // This can happen if the remote description is applied twice, for
1775 // example in the case of ROAP on top of JSEP, where both side will
1776 // send the offer.
1777 }
1778 }
1779
1780 // Only turn on VAD if we have a CN payload type that matches the
1781 // clockrate for the codec we are going to use.
1782 if (send_codec_spec_.cng_plfreq == send_codec_spec_.codec_inst.plfreq &&
1783 send_codec_spec_.codec_inst.channels == 1) {
1784 // TODO(minyue): If CN frequency == 48000 Hz is allowed, consider the
1785 // interaction between VAD and Opus FEC.
1786 LOG(LS_INFO) << "Enabling VAD";
1787 if (engine()->voe()->codec()->SetVADStatus(channel, true) == -1) {
1788 LOG_RTCERR2(SetVADStatus, channel, true);
1789 return false;
1790 }
1791 }
1792 }
solenberg0a617e22015-10-20 15:49:38 -07001793 return true;
wu@webrtc.org9dba5252013-08-05 20:36:57 +00001794}
1795
wu@webrtc.orgcadf9042013-08-30 21:24:16 +00001796void WebRtcVoiceMediaChannel::SetNack(int channel, bool nack_enabled) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001797 if (nack_enabled) {
wu@webrtc.orgcadf9042013-08-30 21:24:16 +00001798 LOG(LS_INFO) << "Enabling NACK for channel " << channel;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001799 engine()->voe()->rtp()->SetNACKStatus(channel, true, kNackMaxPackets);
1800 } else {
wu@webrtc.orgcadf9042013-08-30 21:24:16 +00001801 LOG(LS_INFO) << "Disabling NACK for channel " << channel;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001802 engine()->voe()->rtp()->SetNACKStatus(channel, false, 0);
1803 }
1804}
1805
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001806bool WebRtcVoiceMediaChannel::SetSendCodec(
wu@webrtc.org9dba5252013-08-05 20:36:57 +00001807 int channel, const webrtc::CodecInst& send_codec) {
1808 LOG(LS_INFO) << "Send channel " << channel << " selected voice codec "
1809 << ToString(send_codec) << ", bitrate=" << send_codec.rate;
1810
solenberg72e29d22016-03-08 06:35:16 -08001811 webrtc::CodecInst current_codec = {0};
wu@webrtc.org05e7b442014-04-01 17:44:24 +00001812 if (engine()->voe()->codec()->GetSendCodec(channel, current_codec) == 0 &&
1813 (send_codec == current_codec)) {
1814 // Codec is already configured, we can return without setting it again.
1815 return true;
1816 }
1817
wu@webrtc.org9dba5252013-08-05 20:36:57 +00001818 if (engine()->voe()->codec()->SetSendCodec(channel, send_codec) == -1) {
1819 LOG_RTCERR2(SetSendCodec, channel, ToString(send_codec));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001820 return false;
1821 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001822 return true;
1823}
1824
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001825bool WebRtcVoiceMediaChannel::SetPlayout(bool playout) {
1826 desired_playout_ = playout;
1827 return ChangePlayout(desired_playout_);
1828}
1829
1830bool WebRtcVoiceMediaChannel::PausePlayout() {
1831 return ChangePlayout(false);
1832}
1833
1834bool WebRtcVoiceMediaChannel::ResumePlayout() {
1835 return ChangePlayout(desired_playout_);
1836}
1837
1838bool WebRtcVoiceMediaChannel::ChangePlayout(bool playout) {
Peter Boströmca8b4042016-03-08 14:24:13 -08001839 TRACE_EVENT0("webrtc", "WebRtcVoiceMediaChannel::ChangePlayout");
solenberg566ef242015-11-06 15:34:49 -08001840 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001841 if (playout_ == playout) {
1842 return true;
1843 }
1844
solenberg7add0582015-11-20 09:59:34 -08001845 for (const auto& ch : recv_streams_) {
Fredrik Solenbergaf9fb212015-08-26 10:45:53 +02001846 if (!SetPlayout(ch.second->channel(), playout)) {
henrike@webrtc.org1e09a712013-07-26 19:17:59 +00001847 LOG(LS_ERROR) << "SetPlayout " << playout << " on channel "
Fredrik Solenbergaf9fb212015-08-26 10:45:53 +02001848 << ch.second->channel() << " failed";
solenberg1ac56142015-10-13 03:58:19 -07001849 return false;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001850 }
1851 }
solenberg1ac56142015-10-13 03:58:19 -07001852 playout_ = playout;
1853 return true;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001854}
1855
Taylor Brandstetter1a018dc2016-03-08 12:37:39 -08001856void WebRtcVoiceMediaChannel::SetSend(bool send) {
Peter Boströmca8b4042016-03-08 14:24:13 -08001857 TRACE_EVENT0("webrtc", "WebRtcVoiceMediaChannel::SetSend");
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001858 if (send_ == send) {
Taylor Brandstetter1a018dc2016-03-08 12:37:39 -08001859 return;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001860 }
1861
solenberg246b8172015-12-08 09:50:23 -08001862 // Apply channel specific options when channel is enabled for sending.
Taylor Brandstetter1a018dc2016-03-08 12:37:39 -08001863 if (send) {
solenberg63b34542015-09-29 06:06:31 -07001864 engine()->ApplyOptions(options_);
1865 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001866
wu@webrtc.org9dba5252013-08-05 20:36:57 +00001867 // Change the settings on each send channel.
Taylor Brandstetter1a018dc2016-03-08 12:37:39 -08001868 for (auto& kv : send_streams_) {
1869 kv.second->SetSend(send);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001870 }
wu@webrtc.org9dba5252013-08-05 20:36:57 +00001871
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001872 send_ = send;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001873}
1874
Peter Boström0c4e06b2015-10-07 12:23:21 +02001875bool WebRtcVoiceMediaChannel::SetAudioSend(uint32_t ssrc,
1876 bool enable,
solenberg1dd98f32015-09-10 01:57:14 -07001877 const AudioOptions* options,
Taylor Brandstetter1a018dc2016-03-08 12:37:39 -08001878 AudioSource* source) {
solenberg566ef242015-11-06 15:34:49 -08001879 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
solenberg1dd98f32015-09-10 01:57:14 -07001880 // TODO(solenberg): The state change should be fully rolled back if any one of
1881 // these calls fail.
Taylor Brandstetter1a018dc2016-03-08 12:37:39 -08001882 if (!SetLocalSource(ssrc, source)) {
solenberg1dd98f32015-09-10 01:57:14 -07001883 return false;
1884 }
solenbergdfc8f4f2015-10-01 02:31:10 -07001885 if (!MuteStream(ssrc, !enable)) {
solenberg1dd98f32015-09-10 01:57:14 -07001886 return false;
1887 }
solenbergdfc8f4f2015-10-01 02:31:10 -07001888 if (enable && options) {
solenberg1dd98f32015-09-10 01:57:14 -07001889 return SetOptions(*options);
1890 }
1891 return true;
1892}
1893
solenberg0a617e22015-10-20 15:49:38 -07001894int WebRtcVoiceMediaChannel::CreateVoEChannel() {
1895 int id = engine()->CreateVoEChannel();
1896 if (id == -1) {
1897 LOG_RTCERR0(CreateVoEChannel);
1898 return -1;
wu@webrtc.org9dba5252013-08-05 20:36:57 +00001899 }
solenberg0a617e22015-10-20 15:49:38 -07001900 if (engine()->voe()->network()->RegisterExternalTransport(id, *this) == -1) {
1901 LOG_RTCERR2(RegisterExternalTransport, id, this);
1902 engine()->voe()->base()->DeleteChannel(id);
1903 return -1;
1904 }
1905 return id;
wu@webrtc.org9dba5252013-08-05 20:36:57 +00001906}
1907
solenberg7add0582015-11-20 09:59:34 -08001908bool WebRtcVoiceMediaChannel::DeleteVoEChannel(int channel) {
wu@webrtc.org9dba5252013-08-05 20:36:57 +00001909 if (engine()->voe()->network()->DeRegisterExternalTransport(channel) == -1) {
1910 LOG_RTCERR1(DeRegisterExternalTransport, channel);
1911 }
wu@webrtc.org9dba5252013-08-05 20:36:57 +00001912 if (engine()->voe()->base()->DeleteChannel(channel) == -1) {
1913 LOG_RTCERR1(DeleteChannel, channel);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001914 return false;
1915 }
wu@webrtc.org9dba5252013-08-05 20:36:57 +00001916 return true;
1917}
henrike@webrtc.org1e09a712013-07-26 19:17:59 +00001918
wu@webrtc.org9dba5252013-08-05 20:36:57 +00001919bool WebRtcVoiceMediaChannel::AddSendStream(const StreamParams& sp) {
Peter Boströmca8b4042016-03-08 14:24:13 -08001920 TRACE_EVENT0("webrtc", "WebRtcVoiceMediaChannel::AddSendStream");
solenberg566ef242015-11-06 15:34:49 -08001921 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
solenberg0a617e22015-10-20 15:49:38 -07001922 LOG(LS_INFO) << "AddSendStream: " << sp.ToString();
1923
1924 uint32_t ssrc = sp.first_ssrc();
1925 RTC_DCHECK(0 != ssrc);
1926
1927 if (GetSendChannelId(ssrc) != -1) {
1928 LOG(LS_ERROR) << "Stream already exists with ssrc " << ssrc;
wu@webrtc.org9dba5252013-08-05 20:36:57 +00001929 return false;
1930 }
1931
solenberg0a617e22015-10-20 15:49:38 -07001932 // Create a new channel for sending audio data.
1933 int channel = CreateVoEChannel();
1934 if (channel == -1) {
1935 return false;
wu@webrtc.org9dba5252013-08-05 20:36:57 +00001936 }
wu@webrtc.org9dba5252013-08-05 20:36:57 +00001937
solenbergc96df772015-10-21 13:01:53 -07001938 // Save the channel to send_streams_, so that RemoveSendStream() can still
wu@webrtc.org9dba5252013-08-05 20:36:57 +00001939 // delete the channel in case failure happens below.
mallinath@webrtc.org67ee6b92014-02-03 16:57:16 +00001940 webrtc::AudioTransport* audio_transport =
1941 engine()->voe()->base()->audio_transport();
skvlade0d46372016-04-07 22:59:22 -07001942 WebRtcAudioSendStream* stream = new WebRtcAudioSendStream(
1943 channel, audio_transport, ssrc, sp.cname, send_rtp_extensions_, call_);
1944 send_streams_.insert(std::make_pair(ssrc, stream));
wu@webrtc.org9dba5252013-08-05 20:36:57 +00001945
solenberg0a617e22015-10-20 15:49:38 -07001946 // Set the current codecs to be used for the new channel. We need to do this
1947 // after adding the channel to send_channels_, because of how max bitrate is
1948 // currently being configured by SetSendCodec().
skvlade0d46372016-04-07 22:59:22 -07001949 if (HasSendCodec() && !SetSendCodecs(channel, stream->rtp_parameters())) {
solenberg0a617e22015-10-20 15:49:38 -07001950 RemoveSendStream(ssrc);
wu@webrtc.org9dba5252013-08-05 20:36:57 +00001951 return false;
1952 }
1953
1954 // At this point the channel's local SSRC has been updated. If the channel is
solenberg0a617e22015-10-20 15:49:38 -07001955 // the first send channel make sure that all the receive channels are updated
1956 // with the same SSRC in order to send receiver reports.
solenbergc96df772015-10-21 13:01:53 -07001957 if (send_streams_.size() == 1) {
solenberg0a617e22015-10-20 15:49:38 -07001958 receiver_reports_ssrc_ = ssrc;
solenberg7add0582015-11-20 09:59:34 -08001959 for (const auto& stream : recv_streams_) {
1960 int recv_channel = stream.second->channel();
solenberg0a617e22015-10-20 15:49:38 -07001961 if (engine()->voe()->rtp()->SetLocalSSRC(recv_channel, ssrc) != 0) {
solenberg7add0582015-11-20 09:59:34 -08001962 LOG_RTCERR2(SetLocalSSRC, recv_channel, ssrc);
solenberg1ac56142015-10-13 03:58:19 -07001963 return false;
wu@webrtc.org9dba5252013-08-05 20:36:57 +00001964 }
solenberg0a617e22015-10-20 15:49:38 -07001965 engine()->voe()->base()->AssociateSendChannel(recv_channel, channel);
1966 LOG(LS_INFO) << "VoiceEngine channel #" << recv_channel
1967 << " is associated with channel #" << channel << ".";
wu@webrtc.org9dba5252013-08-05 20:36:57 +00001968 }
1969 }
1970
Taylor Brandstetter1a018dc2016-03-08 12:37:39 -08001971 send_streams_[ssrc]->SetSend(send_);
1972 return true;
wu@webrtc.org9dba5252013-08-05 20:36:57 +00001973}
1974
Peter Boström0c4e06b2015-10-07 12:23:21 +02001975bool WebRtcVoiceMediaChannel::RemoveSendStream(uint32_t ssrc) {
Peter Boströmca8b4042016-03-08 14:24:13 -08001976 TRACE_EVENT0("webrtc", "WebRtcVoiceMediaChannel::RemoveSendStream");
solenberg566ef242015-11-06 15:34:49 -08001977 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
solenberg3a941542015-11-16 07:34:50 -08001978 LOG(LS_INFO) << "RemoveSendStream: " << ssrc;
1979
solenbergc96df772015-10-21 13:01:53 -07001980 auto it = send_streams_.find(ssrc);
1981 if (it == send_streams_.end()) {
wu@webrtc.org9dba5252013-08-05 20:36:57 +00001982 LOG(LS_WARNING) << "Try to remove stream with ssrc " << ssrc
1983 << " which doesn't exist.";
1984 return false;
1985 }
1986
Taylor Brandstetter1a018dc2016-03-08 12:37:39 -08001987 it->second->SetSend(false);
wu@webrtc.org9dba5252013-08-05 20:36:57 +00001988
solenberg7add0582015-11-20 09:59:34 -08001989 // Clean up and delete the send stream+channel.
Taylor Brandstetter1a018dc2016-03-08 12:37:39 -08001990 int channel = it->second->channel();
solenberg0a617e22015-10-20 15:49:38 -07001991 LOG(LS_INFO) << "Removing audio send stream " << ssrc
1992 << " with VoiceEngine channel #" << channel << ".";
solenberg7add0582015-11-20 09:59:34 -08001993 delete it->second;
1994 send_streams_.erase(it);
1995 if (!DeleteVoEChannel(channel)) {
solenberg0a617e22015-10-20 15:49:38 -07001996 return false;
wu@webrtc.org9dba5252013-08-05 20:36:57 +00001997 }
solenbergc96df772015-10-21 13:01:53 -07001998 if (send_streams_.empty()) {
Taylor Brandstetter1a018dc2016-03-08 12:37:39 -08001999 SetSend(false);
solenberg0a617e22015-10-20 15:49:38 -07002000 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002001 return true;
2002}
2003
2004bool WebRtcVoiceMediaChannel::AddRecvStream(const StreamParams& sp) {
Peter Boströmca8b4042016-03-08 14:24:13 -08002005 TRACE_EVENT0("webrtc", "WebRtcVoiceMediaChannel::AddRecvStream");
solenberg566ef242015-11-06 15:34:49 -08002006 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
solenbergd97ec302015-10-07 01:40:33 -07002007 LOG(LS_INFO) << "AddRecvStream: " << sp.ToString();
2008
solenberg0b675462015-10-09 01:37:09 -07002009 if (!ValidateStreamParams(sp)) {
wu@webrtc.org78187522013-10-07 23:32:02 +00002010 return false;
2011 }
2012
solenberg7add0582015-11-20 09:59:34 -08002013 const uint32_t ssrc = sp.first_ssrc();
solenberg0b675462015-10-09 01:37:09 -07002014 if (ssrc == 0) {
2015 LOG(LS_WARNING) << "AddRecvStream with ssrc==0 is not supported.";
2016 return false;
2017 }
2018
solenberg1ac56142015-10-13 03:58:19 -07002019 // Remove the default receive stream if one had been created with this ssrc;
2020 // we'll recreate it then.
2021 if (IsDefaultRecvStream(ssrc)) {
2022 RemoveRecvStream(ssrc);
2023 }
solenberg0b675462015-10-09 01:37:09 -07002024
solenberg7add0582015-11-20 09:59:34 -08002025 if (GetReceiveChannelId(ssrc) != -1) {
henrike@webrtc.org1e09a712013-07-26 19:17:59 +00002026 LOG(LS_ERROR) << "Stream already exists with ssrc " << ssrc;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002027 return false;
2028 }
Fredrik Solenberg4b60c732015-05-07 14:07:48 +02002029
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002030 // Create a new channel for receiving audio data.
solenberg7add0582015-11-20 09:59:34 -08002031 const int channel = CreateVoEChannel();
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002032 if (channel == -1) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002033 return false;
2034 }
Minyue2013aec2015-05-13 14:14:42 +02002035
solenberg1ac56142015-10-13 03:58:19 -07002036 // Turn off all supported codecs.
solenberg26c8c912015-11-27 04:00:25 -08002037 // TODO(solenberg): Remove once "no codecs" is the default state of a stream.
2038 for (webrtc::CodecInst voe_codec : webrtc::acm2::RentACodec::Database()) {
2039 voe_codec.pltype = -1;
2040 if (engine()->voe()->codec()->SetRecPayloadType(channel, voe_codec) == -1) {
2041 LOG_RTCERR2(SetRecPayloadType, channel, ToString(voe_codec));
2042 DeleteVoEChannel(channel);
2043 return false;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002044 }
2045 }
2046
solenberg1ac56142015-10-13 03:58:19 -07002047 // Only enable those configured for this channel.
2048 for (const auto& codec : recv_codecs_) {
solenberg72e29d22016-03-08 06:35:16 -08002049 webrtc::CodecInst voe_codec = {0};
solenberg26c8c912015-11-27 04:00:25 -08002050 if (WebRtcVoiceEngine::ToCodecInst(codec, &voe_codec)) {
solenberg1ac56142015-10-13 03:58:19 -07002051 voe_codec.pltype = codec.id;
2052 if (engine()->voe()->codec()->SetRecPayloadType(
2053 channel, voe_codec) == -1) {
2054 LOG_RTCERR2(SetRecPayloadType, channel, ToString(voe_codec));
solenberg7add0582015-11-20 09:59:34 -08002055 DeleteVoEChannel(channel);
solenberg1ac56142015-10-13 03:58:19 -07002056 return false;
2057 }
henrike@webrtc.org1e09a712013-07-26 19:17:59 +00002058 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002059 }
solenberg8fb30c32015-10-13 03:06:58 -07002060
solenberg7add0582015-11-20 09:59:34 -08002061 const int send_channel = GetSendChannelId(receiver_reports_ssrc_);
2062 if (send_channel != -1) {
2063 // Associate receive channel with first send channel (so the receive channel
2064 // can obtain RTT from the send channel)
2065 engine()->voe()->base()->AssociateSendChannel(channel, send_channel);
2066 LOG(LS_INFO) << "VoiceEngine channel #" << channel
2067 << " is associated with channel #" << send_channel << ".";
buildbot@webrtc.org150835e2014-05-06 15:54:38 +00002068 }
2069
stefanba4c0e42016-02-04 04:12:24 -08002070 recv_streams_.insert(std::make_pair(
2071 ssrc, new WebRtcAudioReceiveStream(channel, ssrc, receiver_reports_ssrc_,
solenberg72e29d22016-03-08 06:35:16 -08002072 recv_transport_cc_enabled_,
2073 sp.sync_label, recv_rtp_extensions_,
2074 call_)));
solenberg7add0582015-11-20 09:59:34 -08002075
solenberg72e29d22016-03-08 06:35:16 -08002076 SetNack(channel, send_codec_spec_.nack_enabled);
solenberg1ac56142015-10-13 03:58:19 -07002077 SetPlayout(channel, playout_);
solenberg7add0582015-11-20 09:59:34 -08002078
solenberg1ac56142015-10-13 03:58:19 -07002079 return true;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002080}
2081
Peter Boström0c4e06b2015-10-07 12:23:21 +02002082bool WebRtcVoiceMediaChannel::RemoveRecvStream(uint32_t ssrc) {
Peter Boströmca8b4042016-03-08 14:24:13 -08002083 TRACE_EVENT0("webrtc", "WebRtcVoiceMediaChannel::RemoveRecvStream");
solenberg566ef242015-11-06 15:34:49 -08002084 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
solenbergd97ec302015-10-07 01:40:33 -07002085 LOG(LS_INFO) << "RemoveRecvStream: " << ssrc;
2086
solenberg7add0582015-11-20 09:59:34 -08002087 const auto it = recv_streams_.find(ssrc);
2088 if (it == recv_streams_.end()) {
wu@webrtc.org9dba5252013-08-05 20:36:57 +00002089 LOG(LS_WARNING) << "Try to remove stream with ssrc " << ssrc
2090 << " which doesn't exist.";
henrike@webrtc.org1e09a712013-07-26 19:17:59 +00002091 return false;
wu@webrtc.org9dba5252013-08-05 20:36:57 +00002092 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002093
solenberg1ac56142015-10-13 03:58:19 -07002094 // Deregister default channel, if that's the one being destroyed.
2095 if (IsDefaultRecvStream(ssrc)) {
2096 default_recv_ssrc_ = -1;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002097 }
henrike@webrtc.org1e09a712013-07-26 19:17:59 +00002098
solenberg7add0582015-11-20 09:59:34 -08002099 const int channel = it->second->channel();
2100
2101 // Clean up and delete the receive stream+channel.
2102 LOG(LS_INFO) << "Removing audio receive stream " << ssrc
mallinath@webrtc.org67ee6b92014-02-03 16:57:16 +00002103 << " with VoiceEngine channel #" << channel << ".";
Tommif888bb52015-12-12 01:37:01 +01002104 it->second->SetRawAudioSink(nullptr);
solenberg7add0582015-11-20 09:59:34 -08002105 delete it->second;
2106 recv_streams_.erase(it);
2107 return DeleteVoEChannel(channel);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002108}
2109
Taylor Brandstetter1a018dc2016-03-08 12:37:39 -08002110bool WebRtcVoiceMediaChannel::SetLocalSource(uint32_t ssrc,
2111 AudioSource* source) {
solenbergc96df772015-10-21 13:01:53 -07002112 auto it = send_streams_.find(ssrc);
2113 if (it == send_streams_.end()) {
Taylor Brandstetter1a018dc2016-03-08 12:37:39 -08002114 if (source) {
2115 // Return an error if trying to set a valid source with an invalid ssrc.
2116 LOG(LS_ERROR) << "SetLocalSource failed with ssrc " << ssrc;
wu@webrtc.org9dba5252013-08-05 20:36:57 +00002117 return false;
2118 }
2119
2120 // The channel likely has gone away, do nothing.
henrike@webrtc.org1e09a712013-07-26 19:17:59 +00002121 return true;
henrike@webrtc.org1e09a712013-07-26 19:17:59 +00002122 }
2123
Taylor Brandstetter1a018dc2016-03-08 12:37:39 -08002124 if (source) {
2125 it->second->SetSource(source);
solenberg1ac56142015-10-13 03:58:19 -07002126 } else {
Taylor Brandstetter1a018dc2016-03-08 12:37:39 -08002127 it->second->ClearSource();
solenberg1ac56142015-10-13 03:58:19 -07002128 }
henrike@webrtc.org1e09a712013-07-26 19:17:59 +00002129
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002130 return true;
2131}
2132
2133bool WebRtcVoiceMediaChannel::GetActiveStreams(
2134 AudioInfo::StreamList* actives) {
solenberg566ef242015-11-06 15:34:49 -08002135 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002136 actives->clear();
solenberg7add0582015-11-20 09:59:34 -08002137 for (const auto& ch : recv_streams_) {
Fredrik Solenbergaf9fb212015-08-26 10:45:53 +02002138 int level = GetOutputLevel(ch.second->channel());
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002139 if (level > 0) {
Fredrik Solenbergaf9fb212015-08-26 10:45:53 +02002140 actives->push_back(std::make_pair(ch.first, level));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002141 }
2142 }
2143 return true;
2144}
2145
2146int WebRtcVoiceMediaChannel::GetOutputLevel() {
solenberg566ef242015-11-06 15:34:49 -08002147 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
solenberg1ac56142015-10-13 03:58:19 -07002148 int highest = 0;
solenberg7add0582015-11-20 09:59:34 -08002149 for (const auto& ch : recv_streams_) {
solenberg8fb30c32015-10-13 03:06:58 -07002150 highest = std::max(GetOutputLevel(ch.second->channel()), highest);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002151 }
2152 return highest;
2153}
2154
2155int WebRtcVoiceMediaChannel::GetTimeSinceLastTyping() {
2156 int ret;
2157 if (engine()->voe()->processing()->TimeSinceLastTyping(ret) == -1) {
2158 // In case of error, log the info and continue
2159 LOG_RTCERR0(TimeSinceLastTyping);
2160 ret = -1;
2161 } else {
2162 ret *= 1000; // We return ms, webrtc returns seconds.
2163 }
2164 return ret;
2165}
2166
2167void WebRtcVoiceMediaChannel::SetTypingDetectionParameters(int time_window,
2168 int cost_per_typing, int reporting_threshold, int penalty_decay,
2169 int type_event_delay) {
2170 if (engine()->voe()->processing()->SetTypingDetectionParameters(
2171 time_window, cost_per_typing,
2172 reporting_threshold, penalty_decay, type_event_delay) == -1) {
2173 // In case of error, log the info and continue
2174 LOG_RTCERR5(SetTypingDetectionParameters, time_window,
2175 cost_per_typing, reporting_threshold, penalty_decay,
2176 type_event_delay);
2177 }
2178}
2179
solenberg4bac9c52015-10-09 02:32:53 -07002180bool WebRtcVoiceMediaChannel::SetOutputVolume(uint32_t ssrc, double volume) {
solenberg566ef242015-11-06 15:34:49 -08002181 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
solenberg1ac56142015-10-13 03:58:19 -07002182 if (ssrc == 0) {
2183 default_recv_volume_ = volume;
2184 if (default_recv_ssrc_ == -1) {
2185 return true;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002186 }
solenberg1ac56142015-10-13 03:58:19 -07002187 ssrc = static_cast<uint32_t>(default_recv_ssrc_);
2188 }
2189 int ch_id = GetReceiveChannelId(ssrc);
2190 if (ch_id < 0) {
2191 LOG(LS_WARNING) << "Cannot find channel for ssrc:" << ssrc;
2192 return false;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002193 }
2194
solenberg1ac56142015-10-13 03:58:19 -07002195 if (-1 == engine()->voe()->volume()->SetChannelOutputVolumeScaling(ch_id,
2196 volume)) {
2197 LOG_RTCERR2(SetChannelOutputVolumeScaling, ch_id, volume);
2198 return false;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002199 }
solenberg1ac56142015-10-13 03:58:19 -07002200 LOG(LS_INFO) << "SetOutputVolume to " << volume
2201 << " for channel " << ch_id << " and ssrc " << ssrc;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002202 return true;
2203}
2204
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002205bool WebRtcVoiceMediaChannel::CanInsertDtmf() {
Fredrik Solenbergb5727682015-12-04 15:22:19 +01002206 return dtmf_payload_type_ ? true : false;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002207}
2208
solenberg1d63dd02015-12-02 12:35:09 -08002209bool WebRtcVoiceMediaChannel::InsertDtmf(uint32_t ssrc, int event,
2210 int duration) {
solenberg566ef242015-11-06 15:34:49 -08002211 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
Fredrik Solenbergb5727682015-12-04 15:22:19 +01002212 LOG(LS_INFO) << "WebRtcVoiceMediaChannel::InsertDtmf";
2213 if (!dtmf_payload_type_) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002214 return false;
2215 }
2216
Fredrik Solenbergb5727682015-12-04 15:22:19 +01002217 // Figure out which WebRtcAudioSendStream to send the event on.
2218 auto it = ssrc != 0 ? send_streams_.find(ssrc) : send_streams_.begin();
2219 if (it == send_streams_.end()) {
2220 LOG(LS_WARNING) << "The specified ssrc " << ssrc << " is not in use.";
solenberg1d63dd02015-12-02 12:35:09 -08002221 return false;
2222 }
Fredrik Solenbergb5727682015-12-04 15:22:19 +01002223 if (event < kMinTelephoneEventCode ||
2224 event > kMaxTelephoneEventCode) {
2225 LOG(LS_WARNING) << "DTMF event code " << event << " out of range.";
solenberg1d63dd02015-12-02 12:35:09 -08002226 return false;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002227 }
Fredrik Solenbergb5727682015-12-04 15:22:19 +01002228 if (duration < kMinTelephoneEventDuration ||
2229 duration > kMaxTelephoneEventDuration) {
2230 LOG(LS_WARNING) << "DTMF event duration " << duration << " out of range.";
2231 return false;
2232 }
2233 return it->second->SendTelephoneEvent(*dtmf_payload_type_, event, duration);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002234}
2235
wu@webrtc.orga9890802013-12-13 00:21:03 +00002236void WebRtcVoiceMediaChannel::OnPacketReceived(
jbaucheec21bd2016-03-20 06:15:43 -07002237 rtc::CopyOnWriteBuffer* packet, const rtc::PacketTime& packet_time) {
solenberg566ef242015-11-06 15:34:49 -08002238 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
Fredrik Solenberg4b60c732015-05-07 14:07:48 +02002239
solenberg1ac56142015-10-13 03:58:19 -07002240 uint32_t ssrc = 0;
jbaucheec21bd2016-03-20 06:15:43 -07002241 if (!GetRtpSsrc(packet->cdata(), packet->size(), &ssrc)) {
solenberg1ac56142015-10-13 03:58:19 -07002242 return;
2243 }
2244
solenberg7e63ef02015-11-20 00:19:43 -08002245 // If we don't have a default channel, and the SSRC is unknown, create a
2246 // default channel.
2247 if (default_recv_ssrc_ == -1 && GetReceiveChannelId(ssrc) == -1) {
solenberg1ac56142015-10-13 03:58:19 -07002248 StreamParams sp;
2249 sp.ssrcs.push_back(ssrc);
2250 LOG(LS_INFO) << "Creating default receive stream for SSRC=" << ssrc << ".";
2251 if (!AddRecvStream(sp)) {
2252 LOG(LS_WARNING) << "Could not create default receive stream.";
2253 return;
2254 }
2255 default_recv_ssrc_ = ssrc;
2256 SetOutputVolume(default_recv_ssrc_, default_recv_volume_);
deadbeef884f5852016-01-15 09:20:04 -08002257 if (default_sink_) {
kwiberg686a8ef2016-02-26 03:00:35 -08002258 std::unique_ptr<webrtc::AudioSinkInterface> proxy_sink(
deadbeef884f5852016-01-15 09:20:04 -08002259 new ProxySink(default_sink_.get()));
2260 SetRawAudioSink(default_recv_ssrc_, std::move(proxy_sink));
2261 }
solenberg1ac56142015-10-13 03:58:19 -07002262 }
2263
2264 // Forward packet to Call. If the SSRC is unknown we'll return after this.
Fredrik Solenberg709ed672015-09-15 12:26:33 +02002265 const webrtc::PacketTime webrtc_packet_time(packet_time.timestamp,
2266 packet_time.not_before);
solenberg1ac56142015-10-13 03:58:19 -07002267 webrtc::PacketReceiver::DeliveryStatus delivery_result =
2268 call_->Receiver()->DeliverPacket(webrtc::MediaType::AUDIO,
jbaucheec21bd2016-03-20 06:15:43 -07002269 packet->cdata(), packet->size(), webrtc_packet_time);
solenberg1ac56142015-10-13 03:58:19 -07002270 if (webrtc::PacketReceiver::DELIVERY_OK != delivery_result) {
solenberg7e63ef02015-11-20 00:19:43 -08002271 // If the SSRC is unknown here, route it to the default channel, if we have
2272 // one. See: https://bugs.chromium.org/p/webrtc/issues/detail?id=5208
2273 if (default_recv_ssrc_ == -1) {
2274 return;
2275 } else {
2276 ssrc = default_recv_ssrc_;
2277 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002278 }
2279
solenberg1ac56142015-10-13 03:58:19 -07002280 // Find the channel to send this packet to. It must exist since webrtc::Call
2281 // was able to demux the packet.
2282 int channel = GetReceiveChannelId(ssrc);
2283 RTC_DCHECK(channel != -1);
2284
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002285 // Pass it off to the decoder.
henrike@webrtc.org28654cb2013-07-22 21:07:49 +00002286 engine()->voe()->network()->ReceivedRTPPacket(
jbaucheec21bd2016-03-20 06:15:43 -07002287 channel, packet->cdata(), packet->size(), webrtc_packet_time);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002288}
2289
wu@webrtc.orga9890802013-12-13 00:21:03 +00002290void WebRtcVoiceMediaChannel::OnRtcpReceived(
jbaucheec21bd2016-03-20 06:15:43 -07002291 rtc::CopyOnWriteBuffer* packet, const rtc::PacketTime& packet_time) {
solenberg566ef242015-11-06 15:34:49 -08002292 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
Fredrik Solenberg4b60c732015-05-07 14:07:48 +02002293
Fredrik Solenberg709ed672015-09-15 12:26:33 +02002294 // Forward packet to Call as well.
2295 const webrtc::PacketTime webrtc_packet_time(packet_time.timestamp,
2296 packet_time.not_before);
2297 call_->Receiver()->DeliverPacket(webrtc::MediaType::AUDIO,
jbaucheec21bd2016-03-20 06:15:43 -07002298 packet->cdata(), packet->size(), webrtc_packet_time);
Fredrik Solenberg4b60c732015-05-07 14:07:48 +02002299
wu@webrtc.org9dba5252013-08-05 20:36:57 +00002300 // Sending channels need all RTCP packets with feedback information.
2301 // Even sender reports can contain attached report blocks.
2302 // Receiving channels need sender reports in order to create
2303 // correct receiver reports.
2304 int type = 0;
jbaucheec21bd2016-03-20 06:15:43 -07002305 if (!GetRtcpType(packet->cdata(), packet->size(), &type)) {
wu@webrtc.org9dba5252013-08-05 20:36:57 +00002306 LOG(LS_WARNING) << "Failed to parse type from received RTCP packet";
2307 return;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002308 }
2309
solenberg0b675462015-10-09 01:37:09 -07002310 // If it is a sender report, find the receive channel that is listening.
wu@webrtc.org9dba5252013-08-05 20:36:57 +00002311 if (type == kRtcpTypeSR) {
solenberg0b675462015-10-09 01:37:09 -07002312 uint32_t ssrc = 0;
jbaucheec21bd2016-03-20 06:15:43 -07002313 if (!GetRtcpSsrc(packet->cdata(), packet->size(), &ssrc)) {
solenberg0b675462015-10-09 01:37:09 -07002314 return;
2315 }
2316 int recv_channel_id = GetReceiveChannelId(ssrc);
2317 if (recv_channel_id != -1) {
wu@webrtc.org9dba5252013-08-05 20:36:57 +00002318 engine()->voe()->network()->ReceivedRTCPPacket(
jbaucheec21bd2016-03-20 06:15:43 -07002319 recv_channel_id, packet->cdata(), packet->size());
wu@webrtc.org9dba5252013-08-05 20:36:57 +00002320 }
2321 }
2322
2323 // SR may continue RR and any RR entry may correspond to any one of the send
2324 // channels. So all RTCP packets must be forwarded all send channels. VoE
2325 // will filter out RR internally.
solenbergc96df772015-10-21 13:01:53 -07002326 for (const auto& ch : send_streams_) {
wu@webrtc.org9dba5252013-08-05 20:36:57 +00002327 engine()->voe()->network()->ReceivedRTCPPacket(
jbaucheec21bd2016-03-20 06:15:43 -07002328 ch.second->channel(), packet->cdata(), packet->size());
wu@webrtc.org9dba5252013-08-05 20:36:57 +00002329 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002330}
2331
Honghai Zhangcc411c02016-03-29 17:27:21 -07002332void WebRtcVoiceMediaChannel::OnNetworkRouteChanged(
2333 const std::string& transport_name,
2334 const NetworkRoute& network_route) {
2335 // TODO(honghaiz): uncomment this once the function in call is implemented.
2336 // call_->OnNetworkRouteChanged(transport_name, network_route);
2337}
2338
Peter Boström0c4e06b2015-10-07 12:23:21 +02002339bool WebRtcVoiceMediaChannel::MuteStream(uint32_t ssrc, bool muted) {
solenberg566ef242015-11-06 15:34:49 -08002340 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
solenberg0a617e22015-10-20 15:49:38 -07002341 int channel = GetSendChannelId(ssrc);
wu@webrtc.org9dba5252013-08-05 20:36:57 +00002342 if (channel == -1) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002343 LOG(LS_WARNING) << "The specified ssrc " << ssrc << " is not in use.";
2344 return false;
2345 }
wu@webrtc.org9dba5252013-08-05 20:36:57 +00002346 if (engine()->voe()->volume()->SetInputMute(channel, muted) == -1) {
2347 LOG_RTCERR2(SetInputMute, channel, muted);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002348 return false;
2349 }
buildbot@webrtc.org6b21b712014-07-31 15:08:53 +00002350 // We set the AGC to mute state only when all the channels are muted.
2351 // This implementation is not ideal, instead we should signal the AGC when
2352 // the mic channel is muted/unmuted. We can't do it today because there
2353 // is no good way to know which stream is mapping to the mic channel.
2354 bool all_muted = muted;
solenbergc96df772015-10-21 13:01:53 -07002355 for (const auto& ch : send_streams_) {
Fredrik Solenbergaf9fb212015-08-26 10:45:53 +02002356 if (!all_muted) {
2357 break;
2358 }
2359 if (engine()->voe()->volume()->GetInputMute(ch.second->channel(),
buildbot@webrtc.org6b21b712014-07-31 15:08:53 +00002360 all_muted)) {
Fredrik Solenbergaf9fb212015-08-26 10:45:53 +02002361 LOG_RTCERR1(GetInputMute, ch.second->channel());
buildbot@webrtc.org6b21b712014-07-31 15:08:53 +00002362 return false;
2363 }
2364 }
2365
2366 webrtc::AudioProcessing* ap = engine()->voe()->base()->audio_processing();
solenberg0a617e22015-10-20 15:49:38 -07002367 if (ap) {
buildbot@webrtc.org6b21b712014-07-31 15:08:53 +00002368 ap->set_output_will_be_muted(all_muted);
solenberg0a617e22015-10-20 15:49:38 -07002369 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002370 return true;
2371}
2372
skvlade0d46372016-04-07 22:59:22 -07002373bool WebRtcVoiceMediaChannel::SetSendBitrate(int bps) {
2374 LOG(LS_INFO) << "WebRtcVoiceMediaChannel::SetSendBitrate.";
2375 send_bitrate_bps_ = bps;
2376
2377 for (const auto& kv : send_streams_) {
2378 if (!SetChannelParameters(kv.second->channel(),
2379 kv.second->rtp_parameters())) {
2380 return false;
2381 }
2382 }
2383 return true;
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00002384}
2385
skvlade0d46372016-04-07 22:59:22 -07002386bool WebRtcVoiceMediaChannel::SetChannelParameters(
2387 int channel,
2388 const webrtc::RtpParameters& parameters) {
2389 RTC_CHECK_EQ(1UL, parameters.encodings.size());
2390 return SetSendBitrate(
2391 channel,
2392 MinPositive(send_bitrate_bps_, parameters.encodings[0].max_bitrate_bps));
2393}
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00002394
skvlade0d46372016-04-07 22:59:22 -07002395bool WebRtcVoiceMediaChannel::SetSendBitrate(int channel, int bps) {
2396 // Bitrate is auto by default.
2397 // TODO(bemasc): Fix this so that if SetMaxSendBandwidth(50) is followed by
2398 // SetMaxSendBandwith(0), the second call removes the previous limit.
2399 if (bps <= 0)
2400 return true;
wu@webrtc.org1d1ffc92013-10-16 18:12:02 +00002401
solenberg72e29d22016-03-08 06:35:16 -08002402 if (!HasSendCodec()) {
wu@webrtc.org1d1ffc92013-10-16 18:12:02 +00002403 LOG(LS_INFO) << "The send codec has not been set up yet. "
minyue@webrtc.org26236952014-10-29 02:27:08 +00002404 << "The send bitrate setting will be applied later.";
wu@webrtc.org1d1ffc92013-10-16 18:12:02 +00002405 return true;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002406 }
2407
solenberg72e29d22016-03-08 06:35:16 -08002408 webrtc::CodecInst codec = send_codec_spec_.codec_inst;
solenberg26c8c912015-11-27 04:00:25 -08002409 bool is_multi_rate = WebRtcVoiceCodecs::IsCodecMultiRate(codec);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002410
2411 if (is_multi_rate) {
2412 // If codec is multi-rate then just set the bitrate.
2413 codec.rate = bps;
skvlade0d46372016-04-07 22:59:22 -07002414 if (!SetSendCodec(channel, codec)) {
2415 LOG(LS_INFO) << "Failed to set codec " << codec.plname << " to bitrate "
2416 << bps << " bps.";
2417 return false;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002418 }
2419 return true;
2420 } else {
2421 // If codec is not multi-rate and |bps| is less than the fixed bitrate
2422 // then fail. If codec is not multi-rate and |bps| exceeds or equal the
2423 // fixed bitrate then ignore.
2424 if (bps < codec.rate) {
2425 LOG(LS_INFO) << "Failed to set codec " << codec.plname
2426 << " to bitrate " << bps << " bps"
2427 << ", requires at least " << codec.rate << " bps.";
2428 return false;
2429 }
2430 return true;
2431 }
2432}
2433
skvlad7a43d252016-03-22 15:32:27 -07002434void WebRtcVoiceMediaChannel::OnReadyToSend(bool ready) {
2435 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
2436 LOG(LS_VERBOSE) << "OnReadyToSend: " << (ready ? "Ready." : "Not ready.");
2437 call_->SignalChannelNetworkState(
2438 webrtc::MediaType::AUDIO,
2439 ready ? webrtc::kNetworkUp : webrtc::kNetworkDown);
2440}
2441
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002442bool WebRtcVoiceMediaChannel::GetStats(VoiceMediaInfo* info) {
Peter Boströmca8b4042016-03-08 14:24:13 -08002443 TRACE_EVENT0("webrtc", "WebRtcVoiceMediaChannel::GetStats");
solenberg566ef242015-11-06 15:34:49 -08002444 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
solenberg85a04962015-10-27 03:35:21 -07002445 RTC_DCHECK(info);
solenbergd97ec302015-10-07 01:40:33 -07002446
solenberg85a04962015-10-27 03:35:21 -07002447 // Get SSRC and stats for each sender.
2448 RTC_DCHECK(info->senders.size() == 0);
2449 for (const auto& stream : send_streams_) {
2450 webrtc::AudioSendStream::Stats stats = stream.second->GetStats();
wu@webrtc.org9dba5252013-08-05 20:36:57 +00002451 VoiceSenderInfo sinfo;
solenberg85a04962015-10-27 03:35:21 -07002452 sinfo.add_ssrc(stats.local_ssrc);
2453 sinfo.bytes_sent = stats.bytes_sent;
2454 sinfo.packets_sent = stats.packets_sent;
2455 sinfo.packets_lost = stats.packets_lost;
2456 sinfo.fraction_lost = stats.fraction_lost;
2457 sinfo.codec_name = stats.codec_name;
2458 sinfo.ext_seqnum = stats.ext_seqnum;
2459 sinfo.jitter_ms = stats.jitter_ms;
2460 sinfo.rtt_ms = stats.rtt_ms;
2461 sinfo.audio_level = stats.audio_level;
2462 sinfo.aec_quality_min = stats.aec_quality_min;
2463 sinfo.echo_delay_median_ms = stats.echo_delay_median_ms;
2464 sinfo.echo_delay_std_ms = stats.echo_delay_std_ms;
2465 sinfo.echo_return_loss = stats.echo_return_loss;
2466 sinfo.echo_return_loss_enhancement = stats.echo_return_loss_enhancement;
Taylor Brandstetter1a018dc2016-03-08 12:37:39 -08002467 sinfo.typing_noise_detected = (send_ ? stats.typing_noise_detected : false);
wu@webrtc.org9dba5252013-08-05 20:36:57 +00002468 info->senders.push_back(sinfo);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002469 }
2470
solenberg85a04962015-10-27 03:35:21 -07002471 // Get SSRC and stats for each receiver.
2472 RTC_DCHECK(info->receivers.size() == 0);
solenberg7add0582015-11-20 09:59:34 -08002473 for (const auto& stream : recv_streams_) {
Fredrik Solenberg4f4ec0a2015-10-22 10:49:27 +02002474 webrtc::AudioReceiveStream::Stats stats = stream.second->GetStats();
2475 VoiceReceiverInfo rinfo;
2476 rinfo.add_ssrc(stats.remote_ssrc);
2477 rinfo.bytes_rcvd = stats.bytes_rcvd;
2478 rinfo.packets_rcvd = stats.packets_rcvd;
2479 rinfo.packets_lost = stats.packets_lost;
2480 rinfo.fraction_lost = stats.fraction_lost;
2481 rinfo.codec_name = stats.codec_name;
2482 rinfo.ext_seqnum = stats.ext_seqnum;
2483 rinfo.jitter_ms = stats.jitter_ms;
2484 rinfo.jitter_buffer_ms = stats.jitter_buffer_ms;
2485 rinfo.jitter_buffer_preferred_ms = stats.jitter_buffer_preferred_ms;
2486 rinfo.delay_estimate_ms = stats.delay_estimate_ms;
2487 rinfo.audio_level = stats.audio_level;
2488 rinfo.expand_rate = stats.expand_rate;
2489 rinfo.speech_expand_rate = stats.speech_expand_rate;
2490 rinfo.secondary_decoded_rate = stats.secondary_decoded_rate;
2491 rinfo.accelerate_rate = stats.accelerate_rate;
2492 rinfo.preemptive_expand_rate = stats.preemptive_expand_rate;
2493 rinfo.decoding_calls_to_silence_generator =
2494 stats.decoding_calls_to_silence_generator;
2495 rinfo.decoding_calls_to_neteq = stats.decoding_calls_to_neteq;
2496 rinfo.decoding_normal = stats.decoding_normal;
2497 rinfo.decoding_plc = stats.decoding_plc;
2498 rinfo.decoding_cng = stats.decoding_cng;
2499 rinfo.decoding_plc_cng = stats.decoding_plc_cng;
2500 rinfo.capture_start_ntp_time_ms = stats.capture_start_ntp_time_ms;
2501 info->receivers.push_back(rinfo);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002502 }
2503
2504 return true;
2505}
2506
Tommif888bb52015-12-12 01:37:01 +01002507void WebRtcVoiceMediaChannel::SetRawAudioSink(
2508 uint32_t ssrc,
kwiberg686a8ef2016-02-26 03:00:35 -08002509 std::unique_ptr<webrtc::AudioSinkInterface> sink) {
Tommif888bb52015-12-12 01:37:01 +01002510 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
deadbeef884f5852016-01-15 09:20:04 -08002511 LOG(LS_VERBOSE) << "WebRtcVoiceMediaChannel::SetRawAudioSink: ssrc:" << ssrc
2512 << " " << (sink ? "(ptr)" : "NULL");
2513 if (ssrc == 0) {
2514 if (default_recv_ssrc_ != -1) {
kwiberg686a8ef2016-02-26 03:00:35 -08002515 std::unique_ptr<webrtc::AudioSinkInterface> proxy_sink(
deadbeef884f5852016-01-15 09:20:04 -08002516 sink ? new ProxySink(sink.get()) : nullptr);
2517 SetRawAudioSink(default_recv_ssrc_, std::move(proxy_sink));
2518 }
2519 default_sink_ = std::move(sink);
2520 return;
2521 }
Tommif888bb52015-12-12 01:37:01 +01002522 const auto it = recv_streams_.find(ssrc);
2523 if (it == recv_streams_.end()) {
2524 LOG(LS_WARNING) << "SetRawAudioSink: no recv stream" << ssrc;
2525 return;
2526 }
deadbeef2d110be2016-01-13 12:00:26 -08002527 it->second->SetRawAudioSink(std::move(sink));
Tommif888bb52015-12-12 01:37:01 +01002528}
2529
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002530int WebRtcVoiceMediaChannel::GetOutputLevel(int channel) {
solenbergd97ec302015-10-07 01:40:33 -07002531 unsigned int ulevel = 0;
2532 int ret = engine()->voe()->volume()->GetSpeechOutputLevel(channel, ulevel);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002533 return (ret == 0) ? static_cast<int>(ulevel) : -1;
2534}
2535
Peter Boström0c4e06b2015-10-07 12:23:21 +02002536int WebRtcVoiceMediaChannel::GetReceiveChannelId(uint32_t ssrc) const {
solenberg566ef242015-11-06 15:34:49 -08002537 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
solenberg7add0582015-11-20 09:59:34 -08002538 const auto it = recv_streams_.find(ssrc);
2539 if (it != recv_streams_.end()) {
mallinath@webrtc.org67ee6b92014-02-03 16:57:16 +00002540 return it->second->channel();
solenberg8fb30c32015-10-13 03:06:58 -07002541 }
solenberg1ac56142015-10-13 03:58:19 -07002542 return -1;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002543}
2544
Peter Boström0c4e06b2015-10-07 12:23:21 +02002545int WebRtcVoiceMediaChannel::GetSendChannelId(uint32_t ssrc) const {
solenberg566ef242015-11-06 15:34:49 -08002546 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
solenbergc96df772015-10-21 13:01:53 -07002547 const auto it = send_streams_.find(ssrc);
2548 if (it != send_streams_.end()) {
mallinath@webrtc.org67ee6b92014-02-03 16:57:16 +00002549 return it->second->channel();
solenberg8fb30c32015-10-13 03:06:58 -07002550 }
wu@webrtc.org9dba5252013-08-05 20:36:57 +00002551 return -1;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002552}
2553
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002554bool WebRtcVoiceMediaChannel::SetPlayout(int channel, bool playout) {
2555 if (playout) {
2556 LOG(LS_INFO) << "Starting playout for channel #" << channel;
2557 if (engine()->voe()->base()->StartPlayout(channel) == -1) {
2558 LOG_RTCERR1(StartPlayout, channel);
2559 return false;
2560 }
2561 } else {
2562 LOG(LS_INFO) << "Stopping playout for channel #" << channel;
2563 engine()->voe()->base()->StopPlayout(channel);
2564 }
2565 return true;
2566}
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002567} // namespace cricket
2568
2569#endif // HAVE_WEBRTC_VOICE