blob: 9fbfd494ebcef26e8ae589e4ec7a6b1d74dc6b98 [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"
kwiberg4485ffb2016-04-26 08:14:39 -070025#include "webrtc/base/constructormagic.h"
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +000026#include "webrtc/base/helpers.h"
27#include "webrtc/base/logging.h"
28#include "webrtc/base/stringencode.h"
29#include "webrtc/base/stringutils.h"
Peter Boströmca8b4042016-03-08 14:24:13 -080030#include "webrtc/base/trace_event.h"
ivoc112a3d82015-10-16 02:22:18 -070031#include "webrtc/call/rtc_event_log.h"
mallinath@webrtc.orga27be8e2013-09-27 23:04:10 +000032#include "webrtc/common.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
deadbeef80346142016-04-27 14:17:10 -070087// iSAC bitrate should be <= 56000.
88const int kIsacMaxBitrate = 56000;
89
wu@webrtc.orgde305012013-10-31 15:40:38 +000090// Default audio dscp value.
91// See http://tools.ietf.org/html/rfc2474 for details.
92// See also http://tools.ietf.org/html/draft-jennings-rtcweb-qos-00
solenbergd97ec302015-10-07 01:40:33 -070093const rtc::DiffServCodePoint kAudioDscpValue = rtc::DSCP_EF;
henrike@webrtc.org1e09a712013-07-26 19:17:59 +000094
Fredrik Solenbergb5727682015-12-04 15:22:19 +010095// Constants from voice_engine_defines.h.
96const int kMinTelephoneEventCode = 0; // RFC4733 (Section 2.3.1)
97const int kMaxTelephoneEventCode = 255;
98const int kMinTelephoneEventDuration = 100;
99const int kMaxTelephoneEventDuration = 60000; // Actual limit is 2^16
100
solenberg31642aa2016-03-14 08:00:37 -0700101const int kMinPayloadType = 0;
102const int kMaxPayloadType = 127;
103
deadbeef884f5852016-01-15 09:20:04 -0800104class ProxySink : public webrtc::AudioSinkInterface {
105 public:
106 ProxySink(AudioSinkInterface* sink) : sink_(sink) { RTC_DCHECK(sink); }
107
108 void OnData(const Data& audio) override { sink_->OnData(audio); }
109
110 private:
111 webrtc::AudioSinkInterface* sink_;
112};
113
solenberg0b675462015-10-09 01:37:09 -0700114bool ValidateStreamParams(const StreamParams& sp) {
115 if (sp.ssrcs.empty()) {
116 LOG(LS_ERROR) << "No SSRCs in stream parameters: " << sp.ToString();
117 return false;
118 }
119 if (sp.ssrcs.size() > 1) {
120 LOG(LS_ERROR) << "Multiple SSRCs in stream parameters: " << sp.ToString();
121 return false;
122 }
123 return true;
124}
125
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000126// Dumps an AudioCodec in RFC 2327-ish format.
solenbergd97ec302015-10-07 01:40:33 -0700127std::string ToString(const AudioCodec& codec) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000128 std::stringstream ss;
129 ss << codec.name << "/" << codec.clockrate << "/" << codec.channels
130 << " (" << codec.id << ")";
131 return ss.str();
132}
Minyue Li7100dcd2015-03-27 05:05:59 +0100133
solenbergd97ec302015-10-07 01:40:33 -0700134std::string ToString(const webrtc::CodecInst& codec) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000135 std::stringstream ss;
136 ss << codec.plname << "/" << codec.plfreq << "/" << codec.channels
137 << " (" << codec.pltype << ")";
138 return ss.str();
139}
140
solenbergd97ec302015-10-07 01:40:33 -0700141bool IsCodec(const AudioCodec& codec, const char* ref_name) {
Minyue Li7100dcd2015-03-27 05:05:59 +0100142 return (_stricmp(codec.name.c_str(), ref_name) == 0);
143}
144
solenbergd97ec302015-10-07 01:40:33 -0700145bool IsCodec(const webrtc::CodecInst& codec, const char* ref_name) {
Minyue Li7100dcd2015-03-27 05:05:59 +0100146 return (_stricmp(codec.plname, ref_name) == 0);
147}
148
solenbergd97ec302015-10-07 01:40:33 -0700149bool FindCodec(const std::vector<AudioCodec>& codecs,
solenberg26c8c912015-11-27 04:00:25 -0800150 const AudioCodec& codec,
151 AudioCodec* found_codec) {
Fredrik Solenbergaf9fb212015-08-26 10:45:53 +0200152 for (const AudioCodec& c : codecs) {
153 if (c.Matches(codec)) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000154 if (found_codec != NULL) {
Fredrik Solenbergaf9fb212015-08-26 10:45:53 +0200155 *found_codec = c;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000156 }
157 return true;
158 }
159 }
160 return false;
161}
wu@webrtc.org1d1ffc92013-10-16 18:12:02 +0000162
solenberg0b675462015-10-09 01:37:09 -0700163bool VerifyUniquePayloadTypes(const std::vector<AudioCodec>& codecs) {
164 if (codecs.empty()) {
165 return true;
166 }
167 std::vector<int> payload_types;
168 for (const AudioCodec& codec : codecs) {
169 payload_types.push_back(codec.id);
170 }
171 std::sort(payload_types.begin(), payload_types.end());
172 auto it = std::unique(payload_types.begin(), payload_types.end());
173 return it == payload_types.end();
174}
175
Minyue Li7100dcd2015-03-27 05:05:59 +0100176// Return true if codec.params[feature] == "1", false otherwise.
solenberg26c8c912015-11-27 04:00:25 -0800177bool IsCodecFeatureEnabled(const AudioCodec& codec, const char* feature) {
Minyue Li7100dcd2015-03-27 05:05:59 +0100178 int value;
179 return codec.GetParam(feature, &value) && value == 1;
180}
181
182// Use params[kCodecParamMaxAverageBitrate] if it is defined, use codec.bitrate
183// otherwise. If the value (either from params or codec.bitrate) <=0, use the
184// default configuration. If the value is beyond feasible bit rate of Opus,
185// clamp it. Returns the Opus bit rate for operation.
solenbergd97ec302015-10-07 01:40:33 -0700186int GetOpusBitrate(const AudioCodec& codec, int max_playback_rate) {
Minyue Li7100dcd2015-03-27 05:05:59 +0100187 int bitrate = 0;
188 bool use_param = true;
189 if (!codec.GetParam(kCodecParamMaxAverageBitrate, &bitrate)) {
190 bitrate = codec.bitrate;
191 use_param = false;
192 }
193 if (bitrate <= 0) {
194 if (max_playback_rate <= 8000) {
195 bitrate = kOpusBitrateNb;
196 } else if (max_playback_rate <= 16000) {
197 bitrate = kOpusBitrateWb;
198 } else {
199 bitrate = kOpusBitrateFb;
200 }
201
202 if (IsCodecFeatureEnabled(codec, kCodecParamStereo)) {
203 bitrate *= 2;
204 }
205 } else if (bitrate < kOpusMinBitrate || bitrate > kOpusMaxBitrate) {
206 bitrate = (bitrate < kOpusMinBitrate) ? kOpusMinBitrate : kOpusMaxBitrate;
207 std::string rate_source =
208 use_param ? "Codec parameter \"maxaveragebitrate\"" :
209 "Supplied Opus bitrate";
210 LOG(LS_WARNING) << rate_source
211 << " is invalid and is replaced by: "
212 << bitrate;
213 }
214 return bitrate;
215}
216
217// Returns kOpusDefaultPlaybackRate if params[kCodecParamMaxPlaybackRate] is not
218// defined. Returns the value of params[kCodecParamMaxPlaybackRate] otherwise.
solenbergd97ec302015-10-07 01:40:33 -0700219int GetOpusMaxPlaybackRate(const AudioCodec& codec) {
Minyue Li7100dcd2015-03-27 05:05:59 +0100220 int value;
221 if (codec.GetParam(kCodecParamMaxPlaybackRate, &value)) {
222 return value;
223 }
224 return kOpusDefaultMaxPlaybackRate;
225}
226
solenbergd97ec302015-10-07 01:40:33 -0700227void GetOpusConfig(const AudioCodec& codec, webrtc::CodecInst* voe_codec,
Minyue Li7100dcd2015-03-27 05:05:59 +0100228 bool* enable_codec_fec, int* max_playback_rate,
229 bool* enable_codec_dtx) {
230 *enable_codec_fec = IsCodecFeatureEnabled(codec, kCodecParamUseInbandFec);
231 *enable_codec_dtx = IsCodecFeatureEnabled(codec, kCodecParamUseDtx);
232 *max_playback_rate = GetOpusMaxPlaybackRate(codec);
233
234 // If OPUS, change what we send according to the "stereo" codec
235 // parameter, and not the "channels" parameter. We set
236 // voe_codec.channels to 2 if "stereo=1" and 1 otherwise. If
237 // the bitrate is not specified, i.e. is <= zero, we set it to the
238 // appropriate default value for mono or stereo Opus.
239
240 voe_codec->channels = IsCodecFeatureEnabled(codec, kCodecParamStereo) ? 2 : 1;
241 voe_codec->rate = GetOpusBitrate(codec, *max_playback_rate);
242}
243
solenberg566ef242015-11-06 15:34:49 -0800244webrtc::AudioState::Config MakeAudioStateConfig(VoEWrapper* voe_wrapper) {
245 webrtc::AudioState::Config config;
246 config.voice_engine = voe_wrapper->engine();
247 return config;
248}
249
solenberg26c8c912015-11-27 04:00:25 -0800250class WebRtcVoiceCodecs final {
251 public:
252 // TODO(solenberg): Do this filtering once off-line, add a simple AudioCodec
253 // list and add a test which verifies VoE supports the listed codecs.
254 static std::vector<AudioCodec> SupportedCodecs() {
solenberg26c8c912015-11-27 04:00:25 -0800255 std::vector<AudioCodec> result;
deadbeef67cf2c12016-04-13 10:07:16 -0700256 // Iterate first over our preferred codecs list, so that the results are
257 // added in order of preference.
258 for (size_t i = 0; i < arraysize(kCodecPrefs); ++i) {
259 const CodecPref* pref = &kCodecPrefs[i];
260 for (webrtc::CodecInst voe_codec : webrtc::acm2::RentACodec::Database()) {
261 // Change the sample rate of G722 to 8000 to match SDP.
262 MaybeFixupG722(&voe_codec, 8000);
263 // Skip uncompressed formats.
264 if (IsCodec(voe_codec, kL16CodecName)) {
265 continue;
buildbot@webrtc.org13d67762014-05-02 17:33:29 +0000266 }
buildbot@webrtc.org13d67762014-05-02 17:33:29 +0000267
deadbeef67cf2c12016-04-13 10:07:16 -0700268 if (!IsCodec(voe_codec, pref->name) ||
269 pref->clockrate != voe_codec.plfreq ||
270 pref->channels != voe_codec.channels) {
271 // Not a match.
272 continue;
273 }
274
275 AudioCodec codec(pref->payload_type, voe_codec.plname, voe_codec.plfreq,
276 voe_codec.rate, voe_codec.channels);
277 LOG(LS_INFO) << "Adding supported codec: " << ToString(codec);
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 }
301 }
solenberg26c8c912015-11-27 04:00:25 -0800302 return result;
buildbot@webrtc.org13d67762014-05-02 17:33:29 +0000303 }
buildbot@webrtc.org13d67762014-05-02 17:33:29 +0000304
solenberg26c8c912015-11-27 04:00:25 -0800305 static bool ToCodecInst(const AudioCodec& in,
306 webrtc::CodecInst* out) {
307 for (webrtc::CodecInst voe_codec : webrtc::acm2::RentACodec::Database()) {
308 // Change the sample rate of G722 to 8000 to match SDP.
309 MaybeFixupG722(&voe_codec, 8000);
310 AudioCodec codec(voe_codec.pltype, voe_codec.plname, voe_codec.plfreq,
deadbeef67cf2c12016-04-13 10:07:16 -0700311 voe_codec.rate, voe_codec.channels);
solenberg26c8c912015-11-27 04:00:25 -0800312 bool multi_rate = IsCodecMultiRate(voe_codec);
313 // Allow arbitrary rates for ISAC to be specified.
314 if (multi_rate) {
315 // Set codec.bitrate to 0 so the check for codec.Matches() passes.
316 codec.bitrate = 0;
317 }
318 if (codec.Matches(in)) {
319 if (out) {
320 // Fixup the payload type.
321 voe_codec.pltype = in.id;
322
323 // Set bitrate if specified.
324 if (multi_rate && in.bitrate != 0) {
325 voe_codec.rate = in.bitrate;
326 }
327
328 // Reset G722 sample rate to 16000 to match WebRTC.
329 MaybeFixupG722(&voe_codec, 16000);
330
331 // Apply codec-specific settings.
332 if (IsCodec(codec, kIsacCodecName)) {
333 // If ISAC and an explicit bitrate is not specified,
334 // enable auto bitrate adjustment.
335 voe_codec.rate = (in.bitrate > 0) ? in.bitrate : -1;
336 }
337 *out = voe_codec;
338 }
339 return true;
340 }
341 }
henrik.lundin@webrtc.org8038d422014-11-11 08:38:24 +0000342 return false;
henrik.lundin@webrtc.orgf85dbce2014-11-07 12:25:00 +0000343 }
solenberg26c8c912015-11-27 04:00:25 -0800344
345 static bool IsCodecMultiRate(const webrtc::CodecInst& codec) {
346 for (size_t i = 0; i < arraysize(kCodecPrefs); ++i) {
347 if (IsCodec(codec, kCodecPrefs[i].name) &&
348 kCodecPrefs[i].clockrate == codec.plfreq) {
349 return kCodecPrefs[i].is_multi_rate;
350 }
351 }
352 return false;
353 }
354
deadbeef80346142016-04-27 14:17:10 -0700355 static int MaxBitrateBps(const webrtc::CodecInst& codec) {
356 for (size_t i = 0; i < arraysize(kCodecPrefs); ++i) {
357 if (IsCodec(codec, kCodecPrefs[i].name) &&
358 kCodecPrefs[i].clockrate == codec.plfreq) {
359 return kCodecPrefs[i].max_bitrate_bps;
360 }
361 }
362 return 0;
363 }
364
solenberg26c8c912015-11-27 04:00:25 -0800365 // If the AudioCodec param kCodecParamPTime is set, then we will set it to
366 // codec pacsize if it's valid, or we will pick the next smallest value we
367 // support.
368 // TODO(Brave): Query supported packet sizes from ACM when the API is ready.
369 static bool SetPTimeAsPacketSize(webrtc::CodecInst* codec, int ptime_ms) {
370 for (const CodecPref& codec_pref : kCodecPrefs) {
371 if ((IsCodec(*codec, codec_pref.name) &&
372 codec_pref.clockrate == codec->plfreq) ||
373 IsCodec(*codec, kG722CodecName)) {
374 int packet_size_ms = SelectPacketSize(codec_pref, ptime_ms);
375 if (packet_size_ms) {
376 // Convert unit from milli-seconds to samples.
377 codec->pacsize = (codec->plfreq / 1000) * packet_size_ms;
378 return true;
379 }
380 }
381 }
382 return false;
383 }
384
stefanba4c0e42016-02-04 04:12:24 -0800385 static const AudioCodec* GetPreferredCodec(
386 const std::vector<AudioCodec>& codecs,
kwiberg68061362016-06-14 08:04:47 -0700387 webrtc::CodecInst* out) {
solenberg72e29d22016-03-08 06:35:16 -0800388 RTC_DCHECK(out);
stefanba4c0e42016-02-04 04:12:24 -0800389 // Select the preferred send codec (the first non-telephone-event/CN codec).
390 for (const AudioCodec& codec : codecs) {
stefanba4c0e42016-02-04 04:12:24 -0800391 if (IsCodec(codec, kDtmfCodecName) || IsCodec(codec, kCnCodecName)) {
392 // Skip telephone-event/CN codec, which will be handled later.
393 continue;
394 }
395
396 // We'll use the first codec in the list to actually send audio data.
397 // Be sure to use the payload type requested by the remote side.
stefanba4c0e42016-02-04 04:12:24 -0800398 // Ignore codecs we don't know about. The negotiation step should prevent
399 // this, but double-check to be sure.
solenberg72e29d22016-03-08 06:35:16 -0800400 webrtc::CodecInst voe_codec = {0};
kwiberg68061362016-06-14 08:04:47 -0700401 if (!ToCodecInst(codec, &voe_codec)) {
402 LOG(LS_WARNING) << "Unknown codec " << ToString(codec);
stefanba4c0e42016-02-04 04:12:24 -0800403 continue;
404 }
solenberg72e29d22016-03-08 06:35:16 -0800405 *out = voe_codec;
kwiberg68061362016-06-14 08:04:47 -0700406 return &codec;
stefanba4c0e42016-02-04 04:12:24 -0800407 }
408 return nullptr;
409 }
410
solenberg26c8c912015-11-27 04:00:25 -0800411 private:
412 static const int kMaxNumPacketSize = 6;
413 struct CodecPref {
414 const char* name;
415 int clockrate;
Peter Kasting69558702016-01-12 16:26:35 -0800416 size_t channels;
solenberg26c8c912015-11-27 04:00:25 -0800417 int payload_type;
418 bool is_multi_rate;
419 int packet_sizes_ms[kMaxNumPacketSize];
deadbeef80346142016-04-27 14:17:10 -0700420 int max_bitrate_bps;
solenberg26c8c912015-11-27 04:00:25 -0800421 };
422 // Note: keep the supported packet sizes in ascending order.
kwiberg68061362016-06-14 08:04:47 -0700423 static const CodecPref kCodecPrefs[11];
solenberg26c8c912015-11-27 04:00:25 -0800424
425 static int SelectPacketSize(const CodecPref& codec_pref, int ptime_ms) {
426 int selected_packet_size_ms = codec_pref.packet_sizes_ms[0];
427 for (int packet_size_ms : codec_pref.packet_sizes_ms) {
428 if (packet_size_ms && packet_size_ms <= ptime_ms) {
429 selected_packet_size_ms = packet_size_ms;
430 }
431 }
432 return selected_packet_size_ms;
433 }
434
435 // Changes RTP timestamp rate of G722. This is due to the "bug" in the RFC
436 // which says that G722 should be advertised as 8 kHz although it is a 16 kHz
437 // codec.
438 static void MaybeFixupG722(webrtc::CodecInst* voe_codec, int new_plfreq) {
439 if (IsCodec(*voe_codec, kG722CodecName)) {
440 // If the ASSERT triggers, the codec definition in WebRTC VoiceEngine
441 // has changed, and this special case is no longer needed.
442 RTC_DCHECK(voe_codec->plfreq != new_plfreq);
443 voe_codec->plfreq = new_plfreq;
444 }
445 }
446};
447
kwiberg68061362016-06-14 08:04:47 -0700448const WebRtcVoiceCodecs::CodecPref WebRtcVoiceCodecs::kCodecPrefs[11] = {
deadbeef80346142016-04-27 14:17:10 -0700449 {kOpusCodecName, 48000, 2, 111, true, {10, 20, 40, 60}, kOpusMaxBitrate},
450 {kIsacCodecName, 16000, 1, 103, true, {30, 60}, kIsacMaxBitrate},
451 {kIsacCodecName, 32000, 1, 104, true, {30}, kIsacMaxBitrate},
452 // G722 should be advertised as 8000 Hz because of the RFC "bug".
453 {kG722CodecName, 8000, 1, 9, false, {10, 20, 30, 40, 50, 60}},
454 {kIlbcCodecName, 8000, 1, 102, false, {20, 30, 40, 60}},
455 {kPcmuCodecName, 8000, 1, 0, false, {10, 20, 30, 40, 50, 60}},
456 {kPcmaCodecName, 8000, 1, 8, false, {10, 20, 30, 40, 50, 60}},
457 {kCnCodecName, 32000, 1, 106, false, {}},
458 {kCnCodecName, 16000, 1, 105, false, {}},
459 {kCnCodecName, 8000, 1, 13, false, {}},
kwiberg68061362016-06-14 08:04:47 -0700460 {kDtmfCodecName, 8000, 1, 126, false, {}}
solenberg26c8c912015-11-27 04:00:25 -0800461};
462} // namespace {
463
464bool WebRtcVoiceEngine::ToCodecInst(const AudioCodec& in,
465 webrtc::CodecInst* out) {
466 return WebRtcVoiceCodecs::ToCodecInst(in, out);
467}
468
ossu29b1a8d2016-06-13 07:34:51 -0700469WebRtcVoiceEngine::WebRtcVoiceEngine(
470 webrtc::AudioDeviceModule* adm,
471 const rtc::scoped_refptr<webrtc::AudioDecoderFactory>& decoder_factory)
472 : WebRtcVoiceEngine(adm, decoder_factory, new VoEWrapper()) {
solenbergff976312016-03-30 23:28:51 -0700473 audio_state_ = webrtc::AudioState::Create(MakeAudioStateConfig(voe()));
solenberg26c8c912015-11-27 04:00:25 -0800474}
475
ossu29b1a8d2016-06-13 07:34:51 -0700476WebRtcVoiceEngine::WebRtcVoiceEngine(
477 webrtc::AudioDeviceModule* adm,
478 const rtc::scoped_refptr<webrtc::AudioDecoderFactory>& decoder_factory,
479 VoEWrapper* voe_wrapper)
480 : adm_(adm), decoder_factory_(decoder_factory), voe_wrapper_(voe_wrapper) {
solenberg26c8c912015-11-27 04:00:25 -0800481 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
solenbergff976312016-03-30 23:28:51 -0700482 LOG(LS_INFO) << "WebRtcVoiceEngine::WebRtcVoiceEngine";
483 RTC_DCHECK(voe_wrapper);
solenberg26c8c912015-11-27 04:00:25 -0800484
485 signal_thread_checker_.DetachFromThread();
solenberg26c8c912015-11-27 04:00:25 -0800486
487 // Load our audio codec list.
solenbergff976312016-03-30 23:28:51 -0700488 LOG(LS_INFO) << "Supported codecs in order of preference:";
solenberg26c8c912015-11-27 04:00:25 -0800489 codecs_ = WebRtcVoiceCodecs::SupportedCodecs();
solenbergff976312016-03-30 23:28:51 -0700490 for (const AudioCodec& codec : codecs_) {
491 LOG(LS_INFO) << ToString(codec);
buildbot@webrtc.org13d67762014-05-02 17:33:29 +0000492 }
buildbot@webrtc.org13d67762014-05-02 17:33:29 +0000493
solenbergff976312016-03-30 23:28:51 -0700494 voe_config_.Set<webrtc::VoicePacing>(new webrtc::VoicePacing(true));
buildbot@webrtc.org13d67762014-05-02 17:33:29 +0000495
solenbergff976312016-03-30 23:28:51 -0700496 // Temporarily turn logging level up for the Init() call.
497 webrtc::Trace::SetTraceCallback(this);
solenbergbd138382015-11-20 16:08:07 -0800498 webrtc::Trace::set_level_filter(kElevatedTraceFilter);
solenberg2515af22015-12-02 06:19:36 -0800499 LOG(LS_INFO) << webrtc::VoiceEngine::GetVersionString();
ossu29b1a8d2016-06-13 07:34:51 -0700500 RTC_CHECK_EQ(0, voe_wrapper_->base()->Init(adm_.get(), nullptr,
501 decoder_factory_));
solenbergbd138382015-11-20 16:08:07 -0800502 webrtc::Trace::set_level_filter(kDefaultTraceFilter);
buildbot@webrtc.org13d67762014-05-02 17:33:29 +0000503
solenbergff976312016-03-30 23:28:51 -0700504 // No ADM supplied? Get the default one from VoE.
505 if (!adm_) {
506 adm_ = voe_wrapper_->base()->audio_device_module();
507 }
508 RTC_DCHECK(adm_);
509
buildbot@webrtc.org13d67762014-05-02 17:33:29 +0000510 // Save the default AGC configuration settings. This must happen before
solenberg246b8172015-12-08 09:50:23 -0800511 // calling ApplyOptions or the default will be overwritten.
solenbergff976312016-03-30 23:28:51 -0700512 int error = voe_wrapper_->processing()->GetAgcConfig(default_agc_config_);
513 RTC_DCHECK_EQ(0, error);
buildbot@webrtc.org13d67762014-05-02 17:33:29 +0000514
solenberg0f7d2932016-01-15 01:40:39 -0800515 // Set default engine options.
516 {
517 AudioOptions options;
518 options.echo_cancellation = rtc::Optional<bool>(true);
519 options.auto_gain_control = rtc::Optional<bool>(true);
520 options.noise_suppression = rtc::Optional<bool>(true);
521 options.highpass_filter = rtc::Optional<bool>(true);
522 options.stereo_swapping = rtc::Optional<bool>(false);
523 options.audio_jitter_buffer_max_packets = rtc::Optional<int>(50);
524 options.audio_jitter_buffer_fast_accelerate = rtc::Optional<bool>(false);
525 options.typing_detection = rtc::Optional<bool>(true);
526 options.adjust_agc_delta = rtc::Optional<int>(0);
527 options.experimental_agc = rtc::Optional<bool>(false);
528 options.extended_filter_aec = rtc::Optional<bool>(false);
529 options.delay_agnostic_aec = rtc::Optional<bool>(false);
530 options.experimental_ns = rtc::Optional<bool>(false);
Alejandro Luebsc9b0c262016-05-16 15:32:38 -0700531 options.intelligibility_enhancer = rtc::Optional<bool>(false);
solenbergff976312016-03-30 23:28:51 -0700532 bool error = ApplyOptions(options);
533 RTC_DCHECK(error);
buildbot@webrtc.org13d67762014-05-02 17:33:29 +0000534 }
535
solenberg246b8172015-12-08 09:50:23 -0800536 SetDefaultDevices();
buildbot@webrtc.org13d67762014-05-02 17:33:29 +0000537}
538
solenbergff976312016-03-30 23:28:51 -0700539WebRtcVoiceEngine::~WebRtcVoiceEngine() {
solenberg566ef242015-11-06 15:34:49 -0800540 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
solenbergff976312016-03-30 23:28:51 -0700541 LOG(LS_INFO) << "WebRtcVoiceEngine::~WebRtcVoiceEngine";
buildbot@webrtc.org13d67762014-05-02 17:33:29 +0000542 StopAecDump();
buildbot@webrtc.org13d67762014-05-02 17:33:29 +0000543 voe_wrapper_->base()->Terminate();
solenbergff976312016-03-30 23:28:51 -0700544 webrtc::Trace::SetTraceCallback(nullptr);
buildbot@webrtc.org13d67762014-05-02 17:33:29 +0000545}
546
solenberg566ef242015-11-06 15:34:49 -0800547rtc::scoped_refptr<webrtc::AudioState>
548 WebRtcVoiceEngine::GetAudioState() const {
549 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
550 return audio_state_;
551}
552
nisse51542be2016-02-12 02:27:06 -0800553VoiceMediaChannel* WebRtcVoiceEngine::CreateChannel(
554 webrtc::Call* call,
555 const MediaConfig& config,
Jelena Marusicc28a8962015-05-29 15:05:44 +0200556 const AudioOptions& options) {
solenberg566ef242015-11-06 15:34:49 -0800557 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
nisse51542be2016-02-12 02:27:06 -0800558 return new WebRtcVoiceMediaChannel(this, config, options, call);
buildbot@webrtc.org13d67762014-05-02 17:33:29 +0000559}
560
buildbot@webrtc.org13d67762014-05-02 17:33:29 +0000561bool WebRtcVoiceEngine::ApplyOptions(const AudioOptions& options_in) {
solenberg566ef242015-11-06 15:34:49 -0800562 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
solenbergff976312016-03-30 23:28:51 -0700563 LOG(LS_INFO) << "WebRtcVoiceEngine::ApplyOptions: " << options_in.ToString();
solenberg0f7d2932016-01-15 01:40:39 -0800564 AudioOptions options = options_in; // The options are modified below.
solenberg246b8172015-12-08 09:50:23 -0800565
buildbot@webrtc.org13d67762014-05-02 17:33:29 +0000566 // kEcConference is AEC with high suppression.
567 webrtc::EcModes ec_mode = webrtc::kEcConference;
568 webrtc::AecmModes aecm_mode = webrtc::kAecmSpeakerphone;
569 webrtc::AgcModes agc_mode = webrtc::kAgcAdaptiveAnalog;
570 webrtc::NsModes ns_mode = webrtc::kNsHighSuppression;
kwiberg102c6a62015-10-30 02:47:38 -0700571 if (options.aecm_generate_comfort_noise) {
buildbot@webrtc.org13d67762014-05-02 17:33:29 +0000572 LOG(LS_VERBOSE) << "Comfort noise explicitly set to "
kwiberg102c6a62015-10-30 02:47:38 -0700573 << *options.aecm_generate_comfort_noise
574 << " (default is false).";
buildbot@webrtc.org13d67762014-05-02 17:33:29 +0000575 }
576
kjellanderfcfc8042016-01-14 11:01:09 -0800577#if defined(WEBRTC_IOS)
buildbot@webrtc.org13d67762014-05-02 17:33:29 +0000578 // On iOS, VPIO provides built-in EC and AGC.
Karl Wibergbe579832015-11-10 22:34:18 +0100579 options.echo_cancellation = rtc::Optional<bool>(false);
580 options.auto_gain_control = rtc::Optional<bool>(false);
henrika86d907c2015-09-07 16:09:50 +0200581 LOG(LS_INFO) << "Always disable AEC and AGC on iOS. Use built-in instead.";
buildbot@webrtc.org13d67762014-05-02 17:33:29 +0000582#elif defined(ANDROID)
583 ec_mode = webrtc::kEcAecm;
584#endif
585
kjellanderfcfc8042016-01-14 11:01:09 -0800586#if defined(WEBRTC_IOS) || defined(ANDROID)
buildbot@webrtc.org13d67762014-05-02 17:33:29 +0000587 // Set the AGC mode for iOS as well despite disabling it above, to avoid
588 // unsupported configuration errors from webrtc.
589 agc_mode = webrtc::kAgcFixedDigital;
Karl Wibergbe579832015-11-10 22:34:18 +0100590 options.typing_detection = rtc::Optional<bool>(false);
591 options.experimental_agc = rtc::Optional<bool>(false);
592 options.extended_filter_aec = rtc::Optional<bool>(false);
593 options.experimental_ns = rtc::Optional<bool>(false);
buildbot@webrtc.org13d67762014-05-02 17:33:29 +0000594#endif
595
Bjorn Volckerbf395c12015-03-25 22:45:56 +0100596 // Delay Agnostic AEC automatically turns on EC if not set except on iOS
597 // where the feature is not supported.
598 bool use_delay_agnostic_aec = false;
kjellanderfcfc8042016-01-14 11:01:09 -0800599#if !defined(WEBRTC_IOS)
kwiberg102c6a62015-10-30 02:47:38 -0700600 if (options.delay_agnostic_aec) {
601 use_delay_agnostic_aec = *options.delay_agnostic_aec;
Bjorn Volckerbf395c12015-03-25 22:45:56 +0100602 if (use_delay_agnostic_aec) {
Karl Wibergbe579832015-11-10 22:34:18 +0100603 options.echo_cancellation = rtc::Optional<bool>(true);
604 options.extended_filter_aec = rtc::Optional<bool>(true);
Bjorn Volckerbf395c12015-03-25 22:45:56 +0100605 ec_mode = webrtc::kEcConference;
606 }
607 }
608#endif
609
buildbot@webrtc.org13d67762014-05-02 17:33:29 +0000610 webrtc::VoEAudioProcessing* voep = voe_wrapper_->processing();
611
kwiberg102c6a62015-10-30 02:47:38 -0700612 if (options.echo_cancellation) {
henrika@webrtc.orga954c072014-12-09 16:22:09 +0000613 // Check if platform supports built-in EC. Currently only supported on
614 // Android and in combination with Java based audio layer.
615 // TODO(henrika): investigate possibility to support built-in EC also
616 // in combination with Open SL ES audio.
solenberg5b5129a2016-04-08 05:35:48 -0700617 const bool built_in_aec = adm()->BuiltInAECIsAvailable();
Bjorn Volcker73f72102015-06-03 14:50:15 +0200618 if (built_in_aec) {
Bjorn Volckerccfc9392015-05-07 07:43:17 +0200619 // Built-in EC exists on this device and use_delay_agnostic_aec is not
620 // overriding it. Enable/Disable it according to the echo_cancellation
621 // audio option.
Bjorn Volcker73f72102015-06-03 14:50:15 +0200622 const bool enable_built_in_aec =
kwiberg102c6a62015-10-30 02:47:38 -0700623 *options.echo_cancellation && !use_delay_agnostic_aec;
solenberg5b5129a2016-04-08 05:35:48 -0700624 if (adm()->EnableBuiltInAEC(enable_built_in_aec) == 0 &&
Bjorn Volcker73f72102015-06-03 14:50:15 +0200625 enable_built_in_aec) {
Bjorn Volckerbf395c12015-03-25 22:45:56 +0100626 // Disable internal software EC if built-in EC is enabled,
henrika@webrtc.orga954c072014-12-09 16:22:09 +0000627 // i.e., replace the software EC with the built-in EC.
Karl Wibergbe579832015-11-10 22:34:18 +0100628 options.echo_cancellation = rtc::Optional<bool>(false);
henrika@webrtc.orga954c072014-12-09 16:22:09 +0000629 LOG(LS_INFO) << "Disabling EC since built-in EC will be used instead";
630 }
631 }
kwiberg102c6a62015-10-30 02:47:38 -0700632 if (voep->SetEcStatus(*options.echo_cancellation, ec_mode) == -1) {
633 LOG_RTCERR2(SetEcStatus, *options.echo_cancellation, ec_mode);
buildbot@webrtc.org13d67762014-05-02 17:33:29 +0000634 return false;
635 } else {
kwiberg102c6a62015-10-30 02:47:38 -0700636 LOG(LS_INFO) << "Echo control set to " << *options.echo_cancellation
henrika86d907c2015-09-07 16:09:50 +0200637 << " with mode " << ec_mode;
buildbot@webrtc.org13d67762014-05-02 17:33:29 +0000638 }
639#if !defined(ANDROID)
640 // TODO(ajm): Remove the error return on Android from webrtc.
kwiberg102c6a62015-10-30 02:47:38 -0700641 if (voep->SetEcMetricsStatus(*options.echo_cancellation) == -1) {
642 LOG_RTCERR1(SetEcMetricsStatus, *options.echo_cancellation);
buildbot@webrtc.org13d67762014-05-02 17:33:29 +0000643 return false;
644 }
645#endif
646 if (ec_mode == webrtc::kEcAecm) {
kwiberg102c6a62015-10-30 02:47:38 -0700647 bool cn = options.aecm_generate_comfort_noise.value_or(false);
648 if (voep->SetAecmMode(aecm_mode, cn) != 0) {
649 LOG_RTCERR2(SetAecmMode, aecm_mode, cn);
buildbot@webrtc.org13d67762014-05-02 17:33:29 +0000650 return false;
651 }
652 }
653 }
654
kwiberg102c6a62015-10-30 02:47:38 -0700655 if (options.auto_gain_control) {
solenberg5b5129a2016-04-08 05:35:48 -0700656 const bool built_in_agc = adm()->BuiltInAGCIsAvailable();
henrikac14f5ff2015-09-23 14:08:33 +0200657 if (built_in_agc) {
solenberg5b5129a2016-04-08 05:35:48 -0700658 if (adm()->EnableBuiltInAGC(*options.auto_gain_control) == 0 &&
kwiberg102c6a62015-10-30 02:47:38 -0700659 *options.auto_gain_control) {
henrikac14f5ff2015-09-23 14:08:33 +0200660 // Disable internal software AGC if built-in AGC is enabled,
661 // i.e., replace the software AGC with the built-in AGC.
Karl Wibergbe579832015-11-10 22:34:18 +0100662 options.auto_gain_control = rtc::Optional<bool>(false);
henrikac14f5ff2015-09-23 14:08:33 +0200663 LOG(LS_INFO) << "Disabling AGC since built-in AGC will be used instead";
664 }
665 }
kwiberg102c6a62015-10-30 02:47:38 -0700666 if (voep->SetAgcStatus(*options.auto_gain_control, agc_mode) == -1) {
667 LOG_RTCERR2(SetAgcStatus, *options.auto_gain_control, agc_mode);
buildbot@webrtc.org13d67762014-05-02 17:33:29 +0000668 return false;
669 } else {
kwiberg102c6a62015-10-30 02:47:38 -0700670 LOG(LS_INFO) << "Auto gain set to " << *options.auto_gain_control
671 << " with mode " << agc_mode;
buildbot@webrtc.org13d67762014-05-02 17:33:29 +0000672 }
673 }
674
kwiberg102c6a62015-10-30 02:47:38 -0700675 if (options.tx_agc_target_dbov || options.tx_agc_digital_compression_gain ||
676 options.tx_agc_limiter) {
buildbot@webrtc.org13d67762014-05-02 17:33:29 +0000677 // Override default_agc_config_. Generally, an unset option means "leave
678 // the VoE bits alone" in this function, so we want whatever is set to be
679 // stored as the new "default". If we didn't, then setting e.g.
680 // tx_agc_target_dbov would reset digital compression gain and limiter
681 // settings.
682 // Also, if we don't update default_agc_config_, then adjust_agc_delta
683 // would be an offset from the original values, and not whatever was set
684 // explicitly.
kwiberg102c6a62015-10-30 02:47:38 -0700685 default_agc_config_.targetLeveldBOv = options.tx_agc_target_dbov.value_or(
686 default_agc_config_.targetLeveldBOv);
buildbot@webrtc.org13d67762014-05-02 17:33:29 +0000687 default_agc_config_.digitalCompressionGaindB =
kwiberg102c6a62015-10-30 02:47:38 -0700688 options.tx_agc_digital_compression_gain.value_or(
buildbot@webrtc.org13d67762014-05-02 17:33:29 +0000689 default_agc_config_.digitalCompressionGaindB);
690 default_agc_config_.limiterEnable =
kwiberg102c6a62015-10-30 02:47:38 -0700691 options.tx_agc_limiter.value_or(default_agc_config_.limiterEnable);
buildbot@webrtc.org13d67762014-05-02 17:33:29 +0000692 if (voe_wrapper_->processing()->SetAgcConfig(default_agc_config_) == -1) {
693 LOG_RTCERR3(SetAgcConfig,
694 default_agc_config_.targetLeveldBOv,
695 default_agc_config_.digitalCompressionGaindB,
696 default_agc_config_.limiterEnable);
697 return false;
698 }
699 }
700
Alejandro Luebsc9b0c262016-05-16 15:32:38 -0700701 if (options.intelligibility_enhancer) {
702 intelligibility_enhancer_ = options.intelligibility_enhancer;
703 }
704 if (intelligibility_enhancer_ && *intelligibility_enhancer_) {
705 LOG(LS_INFO) << "Enabling NS when Intelligibility Enhancer is active.";
706 options.noise_suppression = intelligibility_enhancer_;
707 }
708
kwiberg102c6a62015-10-30 02:47:38 -0700709 if (options.noise_suppression) {
Alejandro Luebsc9b0c262016-05-16 15:32:38 -0700710 if (adm()->BuiltInNSIsAvailable()) {
711 bool builtin_ns =
712 *options.noise_suppression &&
713 !(intelligibility_enhancer_ && *intelligibility_enhancer_);
714 if (adm()->EnableBuiltInNS(builtin_ns) == 0 && builtin_ns) {
henrikac14f5ff2015-09-23 14:08:33 +0200715 // Disable internal software NS if built-in NS is enabled,
716 // i.e., replace the software NS with the built-in NS.
Karl Wibergbe579832015-11-10 22:34:18 +0100717 options.noise_suppression = rtc::Optional<bool>(false);
henrikac14f5ff2015-09-23 14:08:33 +0200718 LOG(LS_INFO) << "Disabling NS since built-in NS will be used instead";
719 }
720 }
kwiberg102c6a62015-10-30 02:47:38 -0700721 if (voep->SetNsStatus(*options.noise_suppression, ns_mode) == -1) {
722 LOG_RTCERR2(SetNsStatus, *options.noise_suppression, ns_mode);
buildbot@webrtc.org13d67762014-05-02 17:33:29 +0000723 return false;
724 } else {
kwiberg102c6a62015-10-30 02:47:38 -0700725 LOG(LS_INFO) << "Noise suppression set to " << *options.noise_suppression
henrikac14f5ff2015-09-23 14:08:33 +0200726 << " with mode " << ns_mode;
buildbot@webrtc.org13d67762014-05-02 17:33:29 +0000727 }
728 }
729
kwiberg102c6a62015-10-30 02:47:38 -0700730 if (options.highpass_filter) {
731 LOG(LS_INFO) << "High pass filter enabled? " << *options.highpass_filter;
732 if (voep->EnableHighPassFilter(*options.highpass_filter) == -1) {
733 LOG_RTCERR1(SetHighpassFilterStatus, *options.highpass_filter);
buildbot@webrtc.org13d67762014-05-02 17:33:29 +0000734 return false;
735 }
736 }
737
kwiberg102c6a62015-10-30 02:47:38 -0700738 if (options.stereo_swapping) {
739 LOG(LS_INFO) << "Stereo swapping enabled? " << *options.stereo_swapping;
740 voep->EnableStereoChannelSwapping(*options.stereo_swapping);
741 if (voep->IsStereoChannelSwappingEnabled() != *options.stereo_swapping) {
742 LOG_RTCERR1(EnableStereoChannelSwapping, *options.stereo_swapping);
buildbot@webrtc.org13d67762014-05-02 17:33:29 +0000743 return false;
744 }
745 }
746
kwiberg102c6a62015-10-30 02:47:38 -0700747 if (options.audio_jitter_buffer_max_packets) {
748 LOG(LS_INFO) << "NetEq capacity is "
749 << *options.audio_jitter_buffer_max_packets;
Henrik Lundin64dad832015-05-11 12:44:23 +0200750 voe_config_.Set<webrtc::NetEqCapacityConfig>(
kwiberg102c6a62015-10-30 02:47:38 -0700751 new webrtc::NetEqCapacityConfig(
752 *options.audio_jitter_buffer_max_packets));
Henrik Lundin64dad832015-05-11 12:44:23 +0200753 }
754
kwiberg102c6a62015-10-30 02:47:38 -0700755 if (options.audio_jitter_buffer_fast_accelerate) {
756 LOG(LS_INFO) << "NetEq fast mode? "
757 << *options.audio_jitter_buffer_fast_accelerate;
Henrik Lundin5263b3c2015-06-01 10:29:41 +0200758 voe_config_.Set<webrtc::NetEqFastAccelerate>(
kwiberg102c6a62015-10-30 02:47:38 -0700759 new webrtc::NetEqFastAccelerate(
760 *options.audio_jitter_buffer_fast_accelerate));
Henrik Lundin5263b3c2015-06-01 10:29:41 +0200761 }
762
kwiberg102c6a62015-10-30 02:47:38 -0700763 if (options.typing_detection) {
764 LOG(LS_INFO) << "Typing detection is enabled? "
765 << *options.typing_detection;
766 if (voep->SetTypingDetectionStatus(*options.typing_detection) == -1) {
buildbot@webrtc.org13d67762014-05-02 17:33:29 +0000767 // In case of error, log the info and continue
kwiberg102c6a62015-10-30 02:47:38 -0700768 LOG_RTCERR1(SetTypingDetectionStatus, *options.typing_detection);
buildbot@webrtc.org13d67762014-05-02 17:33:29 +0000769 }
770 }
771
kwiberg102c6a62015-10-30 02:47:38 -0700772 if (options.adjust_agc_delta) {
773 LOG(LS_INFO) << "Adjust agc delta is " << *options.adjust_agc_delta;
774 if (!AdjustAgcLevel(*options.adjust_agc_delta)) {
buildbot@webrtc.org13d67762014-05-02 17:33:29 +0000775 return false;
776 }
777 }
778
buildbot@webrtc.org1f8a2372014-08-28 10:52:44 +0000779 webrtc::Config config;
780
kwiberg102c6a62015-10-30 02:47:38 -0700781 if (options.delay_agnostic_aec)
782 delay_agnostic_aec_ = options.delay_agnostic_aec;
783 if (delay_agnostic_aec_) {
784 LOG(LS_INFO) << "Delay agnostic aec is enabled? " << *delay_agnostic_aec_;
henrik.lundin0f133b92015-07-02 00:17:55 -0700785 config.Set<webrtc::DelayAgnostic>(
kwiberg102c6a62015-10-30 02:47:38 -0700786 new webrtc::DelayAgnostic(*delay_agnostic_aec_));
Bjorn Volckerbf395c12015-03-25 22:45:56 +0100787 }
788
kwiberg102c6a62015-10-30 02:47:38 -0700789 if (options.extended_filter_aec) {
790 extended_filter_aec_ = options.extended_filter_aec;
791 }
792 if (extended_filter_aec_) {
793 LOG(LS_INFO) << "Extended filter aec is enabled? " << *extended_filter_aec_;
Henrik Lundin441f6342015-06-09 16:03:13 +0200794 config.Set<webrtc::ExtendedFilter>(
kwiberg102c6a62015-10-30 02:47:38 -0700795 new webrtc::ExtendedFilter(*extended_filter_aec_));
buildbot@webrtc.org1f8a2372014-08-28 10:52:44 +0000796 }
797
kwiberg102c6a62015-10-30 02:47:38 -0700798 if (options.experimental_ns) {
799 experimental_ns_ = options.experimental_ns;
800 }
801 if (experimental_ns_) {
802 LOG(LS_INFO) << "Experimental ns is enabled? " << *experimental_ns_;
buildbot@webrtc.org1f8a2372014-08-28 10:52:44 +0000803 config.Set<webrtc::ExperimentalNs>(
kwiberg102c6a62015-10-30 02:47:38 -0700804 new webrtc::ExperimentalNs(*experimental_ns_));
buildbot@webrtc.org1f8a2372014-08-28 10:52:44 +0000805 }
buildbot@webrtc.org1f8a2372014-08-28 10:52:44 +0000806
Alejandro Luebsc9b0c262016-05-16 15:32:38 -0700807 if (intelligibility_enhancer_) {
808 LOG(LS_INFO) << "Intelligibility Enhancer is enabled? "
809 << *intelligibility_enhancer_;
810 config.Set<webrtc::Intelligibility>(
811 new webrtc::Intelligibility(*intelligibility_enhancer_));
812 }
813
buildbot@webrtc.org1f8a2372014-08-28 10:52:44 +0000814 // We check audioproc for the benefit of tests, since FakeWebRtcVoiceEngine
815 // returns NULL on audio_processing().
816 webrtc::AudioProcessing* audioproc = voe_wrapper_->base()->audio_processing();
817 if (audioproc) {
818 audioproc->SetExtraOptions(config);
819 }
820
kwiberg102c6a62015-10-30 02:47:38 -0700821 if (options.recording_sample_rate) {
822 LOG(LS_INFO) << "Recording sample rate is "
823 << *options.recording_sample_rate;
solenberg5b5129a2016-04-08 05:35:48 -0700824 if (adm()->SetRecordingSampleRate(*options.recording_sample_rate)) {
kwiberg102c6a62015-10-30 02:47:38 -0700825 LOG_RTCERR1(SetRecordingSampleRate, *options.recording_sample_rate);
buildbot@webrtc.org13d67762014-05-02 17:33:29 +0000826 }
827 }
828
kwiberg102c6a62015-10-30 02:47:38 -0700829 if (options.playout_sample_rate) {
830 LOG(LS_INFO) << "Playout sample rate is " << *options.playout_sample_rate;
solenberg5b5129a2016-04-08 05:35:48 -0700831 if (adm()->SetPlayoutSampleRate(*options.playout_sample_rate)) {
kwiberg102c6a62015-10-30 02:47:38 -0700832 LOG_RTCERR1(SetPlayoutSampleRate, *options.playout_sample_rate);
buildbot@webrtc.org13d67762014-05-02 17:33:29 +0000833 }
834 }
835
836 return true;
837}
838
solenberg246b8172015-12-08 09:50:23 -0800839void WebRtcVoiceEngine::SetDefaultDevices() {
solenberg566ef242015-11-06 15:34:49 -0800840 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
kjellanderfcfc8042016-01-14 11:01:09 -0800841#if !defined(WEBRTC_IOS)
solenberg246b8172015-12-08 09:50:23 -0800842 int in_id = kDefaultAudioDeviceId;
843 int out_id = kDefaultAudioDeviceId;
844 LOG(LS_INFO) << "Setting microphone to (id=" << in_id
845 << ") and speaker to (id=" << out_id << ")";
buildbot@webrtc.org13d67762014-05-02 17:33:29 +0000846
solenbergc1a1b352015-09-22 13:31:20 -0700847 bool ret = true;
solenberg246b8172015-12-08 09:50:23 -0800848 if (voe_wrapper_->hw()->SetRecordingDevice(in_id) == -1) {
849 LOG_RTCERR1(SetRecordingDevice, in_id);
buildbot@webrtc.org13d67762014-05-02 17:33:29 +0000850 ret = false;
851 }
solenberg246b8172015-12-08 09:50:23 -0800852 webrtc::AudioProcessing* ap = voe()->base()->audio_processing();
853 if (ap) {
854 ap->Initialize();
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000855 }
856
solenberg246b8172015-12-08 09:50:23 -0800857 if (voe_wrapper_->hw()->SetPlayoutDevice(out_id) == -1) {
858 LOG_RTCERR1(SetPlayoutDevice, out_id);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000859 ret = false;
860 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000861
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000862 if (ret) {
solenberg246b8172015-12-08 09:50:23 -0800863 LOG(LS_INFO) << "Set microphone to (id=" << in_id
864 << ") and speaker to (id=" << out_id << ")";
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000865 }
kjellanderfcfc8042016-01-14 11:01:09 -0800866#endif // !WEBRTC_IOS
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000867}
868
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000869int WebRtcVoiceEngine::GetInputLevel() {
solenberg566ef242015-11-06 15:34:49 -0800870 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000871 unsigned int ulevel;
872 return (voe_wrapper_->volume()->GetSpeechInputLevel(ulevel) != -1) ?
873 static_cast<int>(ulevel) : -1;
874}
875
ossudedfd282016-06-14 07:12:39 -0700876const std::vector<AudioCodec>& WebRtcVoiceEngine::send_codecs() const {
877 RTC_DCHECK(signal_thread_checker_.CalledOnValidThread());
878 return codecs_;
879}
880
881const std::vector<AudioCodec>& WebRtcVoiceEngine::recv_codecs() const {
solenberg566ef242015-11-06 15:34:49 -0800882 RTC_DCHECK(signal_thread_checker_.CalledOnValidThread());
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000883 return codecs_;
884}
885
Stefan Holmer9d69c3f2015-12-07 10:45:43 +0100886RtpCapabilities WebRtcVoiceEngine::GetCapabilities() const {
solenberg566ef242015-11-06 15:34:49 -0800887 RTC_DCHECK(signal_thread_checker_.CalledOnValidThread());
Stefan Holmer9d69c3f2015-12-07 10:45:43 +0100888 RtpCapabilities capabilities;
Stefan Holmer9d69c3f2015-12-07 10:45:43 +0100889 capabilities.header_extensions.push_back(
isheriff6f8d6862016-05-26 11:24:55 -0700890 webrtc::RtpExtension(webrtc::RtpExtension::kAudioLevelUri,
891 webrtc::RtpExtension::kAudioLevelDefaultId));
892 capabilities.header_extensions.push_back(
893 webrtc::RtpExtension(webrtc::RtpExtension::kAbsSendTimeUri,
894 webrtc::RtpExtension::kAbsSendTimeDefaultId));
stefanba4c0e42016-02-04 04:12:24 -0800895 if (webrtc::field_trial::FindFullName("WebRTC-Audio-SendSideBwe") ==
896 "Enabled") {
isheriff6f8d6862016-05-26 11:24:55 -0700897 capabilities.header_extensions.push_back(webrtc::RtpExtension(
898 webrtc::RtpExtension::kTransportSequenceNumberUri,
899 webrtc::RtpExtension::kTransportSequenceNumberDefaultId));
stefanba4c0e42016-02-04 04:12:24 -0800900 }
Stefan Holmer9d69c3f2015-12-07 10:45:43 +0100901 return capabilities;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000902}
903
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000904int WebRtcVoiceEngine::GetLastEngineError() {
solenberg566ef242015-11-06 15:34:49 -0800905 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000906 return voe_wrapper_->error();
907}
908
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000909void WebRtcVoiceEngine::Print(webrtc::TraceLevel level, const char* trace,
910 int length) {
solenberg566ef242015-11-06 15:34:49 -0800911 // Note: This callback can happen on any thread!
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000912 rtc::LoggingSeverity sev = rtc::LS_VERBOSE;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000913 if (level == webrtc::kTraceError || level == webrtc::kTraceCritical)
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000914 sev = rtc::LS_ERROR;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000915 else if (level == webrtc::kTraceWarning)
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000916 sev = rtc::LS_WARNING;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000917 else if (level == webrtc::kTraceStateInfo || level == webrtc::kTraceInfo)
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000918 sev = rtc::LS_INFO;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000919 else if (level == webrtc::kTraceTerseInfo)
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000920 sev = rtc::LS_INFO;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000921
solenberg72e29d22016-03-08 06:35:16 -0800922 // Skip past boilerplate prefix text.
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000923 if (length < 72) {
924 std::string msg(trace, length);
925 LOG(LS_ERROR) << "Malformed webrtc log message: ";
926 LOG_V(sev) << msg;
927 } else {
928 std::string msg(trace + 71, length - 72);
Peter Boströmd5c75b12015-09-23 13:24:32 +0200929 LOG_V(sev) << "webrtc: " << msg;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000930 }
931}
932
solenberg63b34542015-09-29 06:06:31 -0700933void WebRtcVoiceEngine::RegisterChannel(WebRtcVoiceMediaChannel* channel) {
solenberg566ef242015-11-06 15:34:49 -0800934 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
935 RTC_DCHECK(channel);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000936 channels_.push_back(channel);
937}
938
solenberg63b34542015-09-29 06:06:31 -0700939void WebRtcVoiceEngine::UnregisterChannel(WebRtcVoiceMediaChannel* channel) {
solenberg566ef242015-11-06 15:34:49 -0800940 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
solenberg63b34542015-09-29 06:06:31 -0700941 auto it = std::find(channels_.begin(), channels_.end(), channel);
solenberg566ef242015-11-06 15:34:49 -0800942 RTC_DCHECK(it != channels_.end());
943 channels_.erase(it);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000944}
945
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000946// Adjusts the default AGC target level by the specified delta.
947// NB: If we start messing with other config fields, we'll want
948// to save the current webrtc::AgcConfig as well.
949bool WebRtcVoiceEngine::AdjustAgcLevel(int delta) {
solenberg566ef242015-11-06 15:34:49 -0800950 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000951 webrtc::AgcConfig config = default_agc_config_;
952 config.targetLeveldBOv -= delta;
953
954 LOG(LS_INFO) << "Adjusting AGC level from default -"
955 << default_agc_config_.targetLeveldBOv << "dB to -"
956 << config.targetLeveldBOv << "dB";
957
958 if (voe_wrapper_->processing()->SetAgcConfig(config) == -1) {
959 LOG_RTCERR1(SetAgcConfig, config.targetLeveldBOv);
960 return false;
961 }
962 return true;
963}
964
ivocd66b44d2016-01-15 03:06:36 -0800965bool WebRtcVoiceEngine::StartAecDump(rtc::PlatformFile file,
966 int64_t max_size_bytes) {
solenberg566ef242015-11-06 15:34:49 -0800967 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000968 FILE* aec_dump_file_stream = rtc::FdopenPlatformFileForWriting(file);
wu@webrtc.orga8910d22014-01-23 22:12:45 +0000969 if (!aec_dump_file_stream) {
970 LOG(LS_ERROR) << "Could not open AEC dump file stream.";
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000971 if (!rtc::ClosePlatformFile(file))
wu@webrtc.orga8910d22014-01-23 22:12:45 +0000972 LOG(LS_WARNING) << "Could not close file.";
973 return false;
974 }
wu@webrtc.orga9890802013-12-13 00:21:03 +0000975 StopAecDump();
ivocd66b44d2016-01-15 03:06:36 -0800976 if (voe_wrapper_->base()->audio_processing()->StartDebugRecording(
977 aec_dump_file_stream, max_size_bytes) !=
wu@webrtc.orga9890802013-12-13 00:21:03 +0000978 webrtc::AudioProcessing::kNoError) {
wu@webrtc.orga8910d22014-01-23 22:12:45 +0000979 LOG_RTCERR0(StartDebugRecording);
980 fclose(aec_dump_file_stream);
wu@webrtc.orga9890802013-12-13 00:21:03 +0000981 return false;
982 }
983 is_dumping_aec_ = true;
984 return true;
wu@webrtc.orga9890802013-12-13 00:21:03 +0000985}
986
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000987void WebRtcVoiceEngine::StartAecDump(const std::string& filename) {
solenberg566ef242015-11-06 15:34:49 -0800988 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000989 if (!is_dumping_aec_) {
990 // Start dumping AEC when we are not dumping.
ivocd66b44d2016-01-15 03:06:36 -0800991 if (voe_wrapper_->base()->audio_processing()->StartDebugRecording(
992 filename.c_str(), -1) != webrtc::AudioProcessing::kNoError) {
wu@webrtc.orga9890802013-12-13 00:21:03 +0000993 LOG_RTCERR1(StartDebugRecording, filename.c_str());
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000994 } else {
995 is_dumping_aec_ = true;
996 }
997 }
998}
999
1000void WebRtcVoiceEngine::StopAecDump() {
solenberg566ef242015-11-06 15:34:49 -08001001 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001002 if (is_dumping_aec_) {
1003 // Stop dumping AEC when we are dumping.
ivocd66b44d2016-01-15 03:06:36 -08001004 if (voe_wrapper_->base()->audio_processing()->StopDebugRecording() !=
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001005 webrtc::AudioProcessing::kNoError) {
1006 LOG_RTCERR0(StopDebugRecording);
1007 }
1008 is_dumping_aec_ = false;
1009 }
1010}
1011
ivocc1513ee2016-05-13 08:30:39 -07001012bool WebRtcVoiceEngine::StartRtcEventLog(rtc::PlatformFile file,
1013 int64_t max_size_bytes) {
solenberg566ef242015-11-06 15:34:49 -08001014 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
ivoc20834ca2016-02-04 06:33:37 -08001015 webrtc::RtcEventLog* event_log = voe_wrapper_->codec()->GetEventLog();
1016 if (event_log) {
ivocc1513ee2016-05-13 08:30:39 -07001017 return event_log->StartLogging(file, max_size_bytes);
ivoc20834ca2016-02-04 06:33:37 -08001018 }
1019 LOG_RTCERR0(StartRtcEventLog);
1020 return false;
ivoc112a3d82015-10-16 02:22:18 -07001021}
1022
1023void WebRtcVoiceEngine::StopRtcEventLog() {
solenberg566ef242015-11-06 15:34:49 -08001024 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
ivoc20834ca2016-02-04 06:33:37 -08001025 webrtc::RtcEventLog* event_log = voe_wrapper_->codec()->GetEventLog();
1026 if (event_log) {
1027 event_log->StopLogging();
1028 return;
1029 }
1030 LOG_RTCERR0(StopRtcEventLog);
ivoc112a3d82015-10-16 02:22:18 -07001031}
1032
solenberg0a617e22015-10-20 15:49:38 -07001033int WebRtcVoiceEngine::CreateVoEChannel() {
solenberg566ef242015-11-06 15:34:49 -08001034 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
solenberg0a617e22015-10-20 15:49:38 -07001035 return voe_wrapper_->base()->CreateChannel(voe_config_);
sergeyu@chromium.org5bc25c42013-12-05 00:24:06 +00001036}
1037
solenberg5b5129a2016-04-08 05:35:48 -07001038webrtc::AudioDeviceModule* WebRtcVoiceEngine::adm() {
1039 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
1040 RTC_DCHECK(adm_);
1041 return adm_;
1042}
1043
solenbergc96df772015-10-21 13:01:53 -07001044class WebRtcVoiceMediaChannel::WebRtcAudioSendStream
Taylor Brandstetter1a018dc2016-03-08 12:37:39 -08001045 : public AudioSource::Sink {
mallinath@webrtc.org67ee6b92014-02-03 16:57:16 +00001046 public:
skvlade0d46372016-04-07 22:59:22 -07001047 WebRtcAudioSendStream(int ch,
1048 webrtc::AudioTransport* voe_audio_transport,
1049 uint32_t ssrc,
1050 const std::string& c_name,
solenberg3a941542015-11-16 07:34:50 -08001051 const std::vector<webrtc::RtpExtension>& extensions,
mflodman3d7db262016-04-29 00:57:13 -07001052 webrtc::Call* call,
1053 webrtc::Transport* send_transport)
solenberg7add0582015-11-20 09:59:34 -08001054 : voe_audio_transport_(voe_audio_transport),
solenberg3a941542015-11-16 07:34:50 -08001055 call_(call),
mflodman3d7db262016-04-29 00:57:13 -07001056 config_(send_transport),
skvlade0d46372016-04-07 22:59:22 -07001057 rtp_parameters_(CreateRtpParametersWithOneEncoding()) {
solenberg85a04962015-10-27 03:35:21 -07001058 RTC_DCHECK_GE(ch, 0);
1059 // TODO(solenberg): Once we're not using FakeWebRtcVoiceEngine anymore:
1060 // RTC_DCHECK(voe_audio_transport);
solenbergc96df772015-10-21 13:01:53 -07001061 RTC_DCHECK(call);
solenberg85a04962015-10-27 03:35:21 -07001062 audio_capture_thread_checker_.DetachFromThread();
solenberg3a941542015-11-16 07:34:50 -08001063 config_.rtp.ssrc = ssrc;
1064 config_.rtp.c_name = c_name;
1065 config_.voe_channel_id = ch;
1066 RecreateAudioSendStream(extensions);
solenbergc96df772015-10-21 13:01:53 -07001067 }
solenberg3a941542015-11-16 07:34:50 -08001068
solenbergc96df772015-10-21 13:01:53 -07001069 ~WebRtcAudioSendStream() override {
solenberg566ef242015-11-06 15:34:49 -08001070 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
Taylor Brandstetter1a018dc2016-03-08 12:37:39 -08001071 ClearSource();
solenbergc96df772015-10-21 13:01:53 -07001072 call_->DestroyAudioSendStream(stream_);
1073 }
henrike@webrtc.org1e09a712013-07-26 19:17:59 +00001074
solenberg3a941542015-11-16 07:34:50 -08001075 void RecreateAudioSendStream(
1076 const std::vector<webrtc::RtpExtension>& extensions) {
1077 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
1078 if (stream_) {
1079 call_->DestroyAudioSendStream(stream_);
1080 stream_ = nullptr;
1081 }
1082 config_.rtp.extensions = extensions;
1083 RTC_DCHECK(!stream_);
1084 stream_ = call_->CreateAudioSendStream(config_);
1085 RTC_CHECK(stream_);
solenberg6d6e7c52016-04-13 09:07:30 -07001086 UpdateSendState();
solenberg3a941542015-11-16 07:34:50 -08001087 }
1088
solenberg8842c3e2016-03-11 03:06:41 -08001089 bool SendTelephoneEvent(int payload_type, int event, int duration_ms) {
Fredrik Solenbergb5727682015-12-04 15:22:19 +01001090 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
1091 RTC_DCHECK(stream_);
1092 return stream_->SendTelephoneEvent(payload_type, event, duration_ms);
1093 }
1094
Taylor Brandstetter1a018dc2016-03-08 12:37:39 -08001095 void SetSend(bool send) {
1096 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
1097 send_ = send;
1098 UpdateSendState();
1099 }
1100
solenberg3a941542015-11-16 07:34:50 -08001101 webrtc::AudioSendStream::Stats GetStats() const {
1102 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
1103 RTC_DCHECK(stream_);
1104 return stream_->GetStats();
1105 }
1106
Taylor Brandstetter1a018dc2016-03-08 12:37:39 -08001107 // Starts the sending by setting ourselves as a sink to the AudioSource to
1108 // get data callbacks.
henrike@webrtc.orga7b98182014-02-21 15:51:43 +00001109 // This method is called on the libjingle worker thread.
mallinath@webrtc.org67ee6b92014-02-03 16:57:16 +00001110 // TODO(xians): Make sure Start() is called only once.
Taylor Brandstetter1a018dc2016-03-08 12:37:39 -08001111 void SetSource(AudioSource* source) {
solenberg566ef242015-11-06 15:34:49 -08001112 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
Taylor Brandstetter1a018dc2016-03-08 12:37:39 -08001113 RTC_DCHECK(source);
1114 if (source_) {
1115 RTC_DCHECK(source_ == source);
mallinath@webrtc.org67ee6b92014-02-03 16:57:16 +00001116 return;
1117 }
Taylor Brandstetter1a018dc2016-03-08 12:37:39 -08001118 source->SetSink(this);
1119 source_ = source;
1120 UpdateSendState();
mallinath@webrtc.org67ee6b92014-02-03 16:57:16 +00001121 }
1122
Taylor Brandstetter1a018dc2016-03-08 12:37:39 -08001123 // Stops sending by setting the sink of the AudioSource to nullptr. No data
mallinath@webrtc.org67ee6b92014-02-03 16:57:16 +00001124 // callback will be received after this method.
henrike@webrtc.orga7b98182014-02-21 15:51:43 +00001125 // This method is called on the libjingle worker thread.
Taylor Brandstetter1a018dc2016-03-08 12:37:39 -08001126 void ClearSource() {
solenberg566ef242015-11-06 15:34:49 -08001127 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
Taylor Brandstetter1a018dc2016-03-08 12:37:39 -08001128 if (source_) {
1129 source_->SetSink(nullptr);
1130 source_ = nullptr;
solenberg98c68862015-10-09 03:27:14 -07001131 }
Taylor Brandstetter1a018dc2016-03-08 12:37:39 -08001132 UpdateSendState();
mallinath@webrtc.org67ee6b92014-02-03 16:57:16 +00001133 }
1134
Taylor Brandstetter1a018dc2016-03-08 12:37:39 -08001135 // AudioSource::Sink implementation.
henrike@webrtc.orga7b98182014-02-21 15:51:43 +00001136 // This method is called on the audio thread.
kjellander@webrtc.org14665ff2015-03-04 12:58:35 +00001137 void OnData(const void* audio_data,
1138 int bits_per_sample,
1139 int sample_rate,
Peter Kasting69558702016-01-12 16:26:35 -08001140 size_t number_of_channels,
Peter Kastingdce40cf2015-08-24 14:52:23 -07001141 size_t number_of_frames) override {
solenberg566ef242015-11-06 15:34:49 -08001142 RTC_DCHECK(!worker_thread_checker_.CalledOnValidThread());
solenberg85a04962015-10-27 03:35:21 -07001143 RTC_DCHECK(audio_capture_thread_checker_.CalledOnValidThread());
solenbergc96df772015-10-21 13:01:53 -07001144 RTC_DCHECK(voe_audio_transport_);
solenberg7add0582015-11-20 09:59:34 -08001145 voe_audio_transport_->OnData(config_.voe_channel_id,
henrike@webrtc.orga7b98182014-02-21 15:51:43 +00001146 audio_data,
1147 bits_per_sample,
1148 sample_rate,
1149 number_of_channels,
1150 number_of_frames);
henrike@webrtc.orga7b98182014-02-21 15:51:43 +00001151 }
1152
Taylor Brandstetter1a018dc2016-03-08 12:37:39 -08001153 // Callback from the |source_| when it is going away. In case Start() has
henrike@webrtc.orga7b98182014-02-21 15:51:43 +00001154 // never been called, this callback won't be triggered.
kjellander@webrtc.org14665ff2015-03-04 12:58:35 +00001155 void OnClose() override {
solenberg566ef242015-11-06 15:34:49 -08001156 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
Taylor Brandstetter1a018dc2016-03-08 12:37:39 -08001157 // Set |source_| to nullptr to make sure no more callback will get into
1158 // the source.
1159 source_ = nullptr;
1160 UpdateSendState();
mallinath@webrtc.org67ee6b92014-02-03 16:57:16 +00001161 }
1162
1163 // Accessor to the VoE channel ID.
solenberg85a04962015-10-27 03:35:21 -07001164 int channel() const {
solenberg566ef242015-11-06 15:34:49 -08001165 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
solenberg7add0582015-11-20 09:59:34 -08001166 return config_.voe_channel_id;
solenberg85a04962015-10-27 03:35:21 -07001167 }
mallinath@webrtc.org67ee6b92014-02-03 16:57:16 +00001168
skvlade0d46372016-04-07 22:59:22 -07001169 const webrtc::RtpParameters& rtp_parameters() const {
1170 return rtp_parameters_;
1171 }
1172
Taylor Brandstetter55dd7082016-05-03 13:50:11 -07001173 void SetRtpParameters(const webrtc::RtpParameters& parameters) {
skvlade0d46372016-04-07 22:59:22 -07001174 RTC_CHECK_EQ(1UL, parameters.encodings.size());
1175 rtp_parameters_ = parameters;
Taylor Brandstetter55dd7082016-05-03 13:50:11 -07001176 // parameters.encodings[0].active could have changed.
1177 UpdateSendState();
skvlade0d46372016-04-07 22:59:22 -07001178 }
1179
mallinath@webrtc.org67ee6b92014-02-03 16:57:16 +00001180 private:
Taylor Brandstetter1a018dc2016-03-08 12:37:39 -08001181 void UpdateSendState() {
1182 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
1183 RTC_DCHECK(stream_);
Taylor Brandstetter55dd7082016-05-03 13:50:11 -07001184 RTC_DCHECK_EQ(1UL, rtp_parameters_.encodings.size());
1185 if (send_ && source_ != nullptr && rtp_parameters_.encodings[0].active) {
Taylor Brandstetter1a018dc2016-03-08 12:37:39 -08001186 stream_->Start();
1187 } else { // !send || source_ = nullptr
1188 stream_->Stop();
1189 }
1190 }
1191
solenberg566ef242015-11-06 15:34:49 -08001192 rtc::ThreadChecker worker_thread_checker_;
solenberg85a04962015-10-27 03:35:21 -07001193 rtc::ThreadChecker audio_capture_thread_checker_;
solenbergc96df772015-10-21 13:01:53 -07001194 webrtc::AudioTransport* const voe_audio_transport_ = nullptr;
1195 webrtc::Call* call_ = nullptr;
solenberg3a941542015-11-16 07:34:50 -08001196 webrtc::AudioSendStream::Config config_;
1197 // The stream is owned by WebRtcAudioSendStream and may be reallocated if
1198 // configuration changes.
solenbergc96df772015-10-21 13:01:53 -07001199 webrtc::AudioSendStream* stream_ = nullptr;
mallinath@webrtc.org67ee6b92014-02-03 16:57:16 +00001200
Taylor Brandstetter1a018dc2016-03-08 12:37:39 -08001201 // Raw pointer to AudioSource owned by LocalAudioTrackHandler.
mallinath@webrtc.org67ee6b92014-02-03 16:57:16 +00001202 // PeerConnection will make sure invalidating the pointer before the object
1203 // goes away.
Taylor Brandstetter1a018dc2016-03-08 12:37:39 -08001204 AudioSource* source_ = nullptr;
1205 bool send_ = false;
skvlade0d46372016-04-07 22:59:22 -07001206 webrtc::RtpParameters rtp_parameters_;
henrike@webrtc.orga7b98182014-02-21 15:51:43 +00001207
solenbergc96df772015-10-21 13:01:53 -07001208 RTC_DISALLOW_IMPLICIT_CONSTRUCTORS(WebRtcAudioSendStream);
1209};
1210
1211class WebRtcVoiceMediaChannel::WebRtcAudioReceiveStream {
1212 public:
ossu29b1a8d2016-06-13 07:34:51 -07001213 WebRtcAudioReceiveStream(
1214 int ch,
1215 uint32_t remote_ssrc,
1216 uint32_t local_ssrc,
1217 bool use_transport_cc,
1218 const std::string& sync_group,
1219 const std::vector<webrtc::RtpExtension>& extensions,
1220 webrtc::Call* call,
1221 webrtc::Transport* rtcp_send_transport,
1222 const rtc::scoped_refptr<webrtc::AudioDecoderFactory>& decoder_factory)
stefanba4c0e42016-02-04 04:12:24 -08001223 : call_(call), config_() {
solenberg7add0582015-11-20 09:59:34 -08001224 RTC_DCHECK_GE(ch, 0);
1225 RTC_DCHECK(call);
1226 config_.rtp.remote_ssrc = remote_ssrc;
1227 config_.rtp.local_ssrc = local_ssrc;
solenberg31fec402016-05-06 02:13:12 -07001228 config_.rtcp_send_transport = rtcp_send_transport;
solenberg7add0582015-11-20 09:59:34 -08001229 config_.voe_channel_id = ch;
1230 config_.sync_group = sync_group;
ossu29b1a8d2016-06-13 07:34:51 -07001231 config_.decoder_factory = decoder_factory;
stefanba4c0e42016-02-04 04:12:24 -08001232 RecreateAudioReceiveStream(use_transport_cc, extensions);
solenberg7add0582015-11-20 09:59:34 -08001233 }
solenbergc96df772015-10-21 13:01:53 -07001234
solenberg7add0582015-11-20 09:59:34 -08001235 ~WebRtcAudioReceiveStream() {
1236 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
1237 call_->DestroyAudioReceiveStream(stream_);
1238 }
1239
1240 void RecreateAudioReceiveStream(
1241 const std::vector<webrtc::RtpExtension>& extensions) {
1242 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
stefanba4c0e42016-02-04 04:12:24 -08001243 RecreateAudioReceiveStream(config_.rtp.transport_cc, extensions);
solenberg7add0582015-11-20 09:59:34 -08001244 }
stefanba4c0e42016-02-04 04:12:24 -08001245 void RecreateAudioReceiveStream(bool use_transport_cc) {
solenberg7add0582015-11-20 09:59:34 -08001246 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
stefanba4c0e42016-02-04 04:12:24 -08001247 RecreateAudioReceiveStream(use_transport_cc, config_.rtp.extensions);
solenberg7add0582015-11-20 09:59:34 -08001248 }
1249
1250 webrtc::AudioReceiveStream::Stats GetStats() const {
1251 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
1252 RTC_DCHECK(stream_);
1253 return stream_->GetStats();
1254 }
1255
1256 int channel() const {
1257 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
1258 return config_.voe_channel_id;
1259 }
solenbergc96df772015-10-21 13:01:53 -07001260
kwiberg686a8ef2016-02-26 03:00:35 -08001261 void SetRawAudioSink(std::unique_ptr<webrtc::AudioSinkInterface> sink) {
Tommif888bb52015-12-12 01:37:01 +01001262 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
kwiberg686a8ef2016-02-26 03:00:35 -08001263 stream_->SetSink(std::move(sink));
Tommif888bb52015-12-12 01:37:01 +01001264 }
1265
solenbergc96df772015-10-21 13:01:53 -07001266 private:
stefanba4c0e42016-02-04 04:12:24 -08001267 void RecreateAudioReceiveStream(
1268 bool use_transport_cc,
solenberg7add0582015-11-20 09:59:34 -08001269 const std::vector<webrtc::RtpExtension>& extensions) {
1270 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
1271 if (stream_) {
1272 call_->DestroyAudioReceiveStream(stream_);
1273 stream_ = nullptr;
1274 }
1275 config_.rtp.extensions = extensions;
stefanba4c0e42016-02-04 04:12:24 -08001276 config_.rtp.transport_cc = use_transport_cc;
solenberg7add0582015-11-20 09:59:34 -08001277 RTC_DCHECK(!stream_);
1278 stream_ = call_->CreateAudioReceiveStream(config_);
1279 RTC_CHECK(stream_);
1280 }
1281
1282 rtc::ThreadChecker worker_thread_checker_;
1283 webrtc::Call* call_ = nullptr;
1284 webrtc::AudioReceiveStream::Config config_;
1285 // The stream is owned by WebRtcAudioReceiveStream and may be reallocated if
1286 // configuration changes.
1287 webrtc::AudioReceiveStream* stream_ = nullptr;
solenbergc96df772015-10-21 13:01:53 -07001288
1289 RTC_DISALLOW_IMPLICIT_CONSTRUCTORS(WebRtcAudioReceiveStream);
henrike@webrtc.org1e09a712013-07-26 19:17:59 +00001290};
1291
Fredrik Solenberg709ed672015-09-15 12:26:33 +02001292WebRtcVoiceMediaChannel::WebRtcVoiceMediaChannel(WebRtcVoiceEngine* engine,
nisse51542be2016-02-12 02:27:06 -08001293 const MediaConfig& config,
Fredrik Solenbergb071a192015-09-17 16:42:56 +02001294 const AudioOptions& options,
Fredrik Solenberg709ed672015-09-15 12:26:33 +02001295 webrtc::Call* call)
nisse51542be2016-02-12 02:27:06 -08001296 : VoiceMediaChannel(config), engine_(engine), call_(call) {
solenberg0a617e22015-10-20 15:49:38 -07001297 LOG(LS_VERBOSE) << "WebRtcVoiceMediaChannel::WebRtcVoiceMediaChannel";
solenberg566ef242015-11-06 15:34:49 -08001298 RTC_DCHECK(call);
solenberg0a617e22015-10-20 15:49:38 -07001299 engine->RegisterChannel(this);
Fredrik Solenbergb071a192015-09-17 16:42:56 +02001300 SetOptions(options);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001301}
1302
1303WebRtcVoiceMediaChannel::~WebRtcVoiceMediaChannel() {
solenberg566ef242015-11-06 15:34:49 -08001304 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
solenberg0a617e22015-10-20 15:49:38 -07001305 LOG(LS_VERBOSE) << "WebRtcVoiceMediaChannel::~WebRtcVoiceMediaChannel";
solenberg7add0582015-11-20 09:59:34 -08001306 // TODO(solenberg): Should be able to delete the streams directly, without
1307 // going through RemoveNnStream(), once stream objects handle
1308 // all (de)configuration.
solenbergc96df772015-10-21 13:01:53 -07001309 while (!send_streams_.empty()) {
1310 RemoveSendStream(send_streams_.begin()->first);
solenbergd97ec302015-10-07 01:40:33 -07001311 }
solenberg7add0582015-11-20 09:59:34 -08001312 while (!recv_streams_.empty()) {
1313 RemoveRecvStream(recv_streams_.begin()->first);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001314 }
solenberg0a617e22015-10-20 15:49:38 -07001315 engine()->UnregisterChannel(this);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001316}
1317
nisse51542be2016-02-12 02:27:06 -08001318rtc::DiffServCodePoint WebRtcVoiceMediaChannel::PreferredDscp() const {
1319 return kAudioDscpValue;
1320}
1321
Peter Thatcherc2ee2c82015-08-07 16:05:34 -07001322bool WebRtcVoiceMediaChannel::SetSendParameters(
1323 const AudioSendParameters& params) {
Peter Boströmca8b4042016-03-08 14:24:13 -08001324 TRACE_EVENT0("webrtc", "WebRtcVoiceMediaChannel::SetSendParameters");
solenberg566ef242015-11-06 15:34:49 -08001325 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
solenberg7e4e01a2015-12-02 08:05:01 -08001326 LOG(LS_INFO) << "WebRtcVoiceMediaChannel::SetSendParameters: "
1327 << params.ToString();
Peter Thatcherc2ee2c82015-08-07 16:05:34 -07001328 // TODO(pthatcher): Refactor this to be more clean now that we have
1329 // all the information at once.
solenberg3a941542015-11-16 07:34:50 -08001330
1331 if (!SetSendCodecs(params.codecs)) {
1332 return false;
1333 }
1334
solenberg7e4e01a2015-12-02 08:05:01 -08001335 if (!ValidateRtpExtensions(params.extensions)) {
1336 return false;
1337 }
1338 std::vector<webrtc::RtpExtension> filtered_extensions =
1339 FilterRtpExtensions(params.extensions,
1340 webrtc::RtpExtension::IsSupportedForAudio, true);
1341 if (send_rtp_extensions_ != filtered_extensions) {
1342 send_rtp_extensions_.swap(filtered_extensions);
solenberg3a941542015-11-16 07:34:50 -08001343 for (auto& it : send_streams_) {
1344 it.second->RecreateAudioSendStream(send_rtp_extensions_);
1345 }
1346 }
1347
deadbeef80346142016-04-27 14:17:10 -07001348 if (!SetMaxSendBitrate(params.max_bandwidth_bps)) {
solenberg3a941542015-11-16 07:34:50 -08001349 return false;
1350 }
1351 return SetOptions(params.options);
Peter Thatcherc2ee2c82015-08-07 16:05:34 -07001352}
1353
1354bool WebRtcVoiceMediaChannel::SetRecvParameters(
1355 const AudioRecvParameters& params) {
Peter Boströmca8b4042016-03-08 14:24:13 -08001356 TRACE_EVENT0("webrtc", "WebRtcVoiceMediaChannel::SetRecvParameters");
solenberg566ef242015-11-06 15:34:49 -08001357 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
solenberg7e4e01a2015-12-02 08:05:01 -08001358 LOG(LS_INFO) << "WebRtcVoiceMediaChannel::SetRecvParameters: "
1359 << params.ToString();
Peter Thatcherc2ee2c82015-08-07 16:05:34 -07001360 // TODO(pthatcher): Refactor this to be more clean now that we have
1361 // all the information at once.
solenberg7add0582015-11-20 09:59:34 -08001362
1363 if (!SetRecvCodecs(params.codecs)) {
1364 return false;
1365 }
1366
solenberg7e4e01a2015-12-02 08:05:01 -08001367 if (!ValidateRtpExtensions(params.extensions)) {
1368 return false;
1369 }
1370 std::vector<webrtc::RtpExtension> filtered_extensions =
1371 FilterRtpExtensions(params.extensions,
1372 webrtc::RtpExtension::IsSupportedForAudio, false);
1373 if (recv_rtp_extensions_ != filtered_extensions) {
1374 recv_rtp_extensions_.swap(filtered_extensions);
solenberg7add0582015-11-20 09:59:34 -08001375 for (auto& it : recv_streams_) {
1376 it.second->RecreateAudioReceiveStream(recv_rtp_extensions_);
1377 }
1378 }
solenberg7add0582015-11-20 09:59:34 -08001379 return true;
Peter Thatcherc2ee2c82015-08-07 16:05:34 -07001380}
1381
Taylor Brandstetterdb0cd9e2016-05-16 11:40:30 -07001382webrtc::RtpParameters WebRtcVoiceMediaChannel::GetRtpSendParameters(
skvlade0d46372016-04-07 22:59:22 -07001383 uint32_t ssrc) const {
1384 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
1385 auto it = send_streams_.find(ssrc);
1386 if (it == send_streams_.end()) {
Taylor Brandstetterdb0cd9e2016-05-16 11:40:30 -07001387 LOG(LS_WARNING) << "Attempting to get RTP send parameters for stream "
1388 << "with ssrc " << ssrc << " which doesn't exist.";
skvlade0d46372016-04-07 22:59:22 -07001389 return webrtc::RtpParameters();
1390 }
1391
Taylor Brandstetter0cd086b2016-04-20 16:23:10 -07001392 webrtc::RtpParameters rtp_params = it->second->rtp_parameters();
1393 // Need to add the common list of codecs to the send stream-specific
1394 // RTP parameters.
1395 for (const AudioCodec& codec : send_codecs_) {
1396 rtp_params.codecs.push_back(codec.ToCodecParameters());
1397 }
1398 return rtp_params;
skvlade0d46372016-04-07 22:59:22 -07001399}
1400
Taylor Brandstetterdb0cd9e2016-05-16 11:40:30 -07001401bool WebRtcVoiceMediaChannel::SetRtpSendParameters(
skvlade0d46372016-04-07 22:59:22 -07001402 uint32_t ssrc,
1403 const webrtc::RtpParameters& parameters) {
1404 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
1405 if (!ValidateRtpParameters(parameters)) {
1406 return false;
1407 }
1408 auto it = send_streams_.find(ssrc);
1409 if (it == send_streams_.end()) {
Taylor Brandstetterdb0cd9e2016-05-16 11:40:30 -07001410 LOG(LS_WARNING) << "Attempting to set RTP send parameters for stream "
1411 << "with ssrc " << ssrc << " which doesn't exist.";
skvlade0d46372016-04-07 22:59:22 -07001412 return false;
1413 }
1414
Taylor Brandstetterdb0cd9e2016-05-16 11:40:30 -07001415 // TODO(deadbeef): Handle setting parameters with a list of codecs in a
1416 // different order (which should change the send codec).
1417 webrtc::RtpParameters current_parameters = GetRtpSendParameters(ssrc);
1418 if (current_parameters.codecs != parameters.codecs) {
1419 LOG(LS_ERROR) << "Using SetParameters to change the set of codecs "
1420 << "is not currently supported.";
1421 return false;
1422 }
1423
1424 if (!SetChannelSendParameters(it->second->channel(), parameters)) {
1425 LOG(LS_WARNING) << "Failed to set send RtpParameters.";
skvlade0d46372016-04-07 22:59:22 -07001426 return false;
1427 }
Taylor Brandstetter0cd086b2016-04-20 16:23:10 -07001428 // Codecs are handled at the WebRtcVoiceMediaChannel level.
1429 webrtc::RtpParameters reduced_params = parameters;
1430 reduced_params.codecs.clear();
Taylor Brandstetter55dd7082016-05-03 13:50:11 -07001431 it->second->SetRtpParameters(reduced_params);
skvlade0d46372016-04-07 22:59:22 -07001432 return true;
1433}
1434
Taylor Brandstetterdb0cd9e2016-05-16 11:40:30 -07001435webrtc::RtpParameters WebRtcVoiceMediaChannel::GetRtpReceiveParameters(
1436 uint32_t ssrc) const {
1437 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
1438 auto it = recv_streams_.find(ssrc);
1439 if (it == recv_streams_.end()) {
1440 LOG(LS_WARNING) << "Attempting to get RTP receive parameters for stream "
1441 << "with ssrc " << ssrc << " which doesn't exist.";
1442 return webrtc::RtpParameters();
1443 }
1444
1445 // TODO(deadbeef): Return stream-specific parameters.
1446 webrtc::RtpParameters rtp_params = CreateRtpParametersWithOneEncoding();
1447 for (const AudioCodec& codec : recv_codecs_) {
1448 rtp_params.codecs.push_back(codec.ToCodecParameters());
1449 }
1450 return rtp_params;
1451}
1452
1453bool WebRtcVoiceMediaChannel::SetRtpReceiveParameters(
1454 uint32_t ssrc,
1455 const webrtc::RtpParameters& parameters) {
1456 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
1457 if (!ValidateRtpParameters(parameters)) {
1458 return false;
1459 }
1460 auto it = recv_streams_.find(ssrc);
1461 if (it == recv_streams_.end()) {
1462 LOG(LS_WARNING) << "Attempting to set RTP receive parameters for stream "
1463 << "with ssrc " << ssrc << " which doesn't exist.";
1464 return false;
1465 }
1466
1467 webrtc::RtpParameters current_parameters = GetRtpReceiveParameters(ssrc);
1468 if (current_parameters != parameters) {
1469 LOG(LS_ERROR) << "Changing the RTP receive parameters is currently "
1470 << "unsupported.";
1471 return false;
1472 }
1473 return true;
1474}
1475
skvlade0d46372016-04-07 22:59:22 -07001476bool WebRtcVoiceMediaChannel::ValidateRtpParameters(
1477 const webrtc::RtpParameters& rtp_parameters) {
1478 if (rtp_parameters.encodings.size() != 1) {
1479 LOG(LS_ERROR)
1480 << "Attempted to set RtpParameters without exactly one encoding";
1481 return false;
1482 }
1483 return true;
1484}
1485
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001486bool WebRtcVoiceMediaChannel::SetOptions(const AudioOptions& options) {
solenberg566ef242015-11-06 15:34:49 -08001487 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001488 LOG(LS_INFO) << "Setting voice channel options: "
1489 << options.ToString();
1490
1491 // We retain all of the existing options, and apply the given ones
1492 // on top. This means there is no way to "clear" options such that
1493 // they go back to the engine default.
1494 options_.SetAll(options);
solenberg246b8172015-12-08 09:50:23 -08001495 if (!engine()->ApplyOptions(options_)) {
1496 LOG(LS_WARNING) <<
1497 "Failed to apply engine options during channel SetOptions.";
1498 return false;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001499 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001500 LOG(LS_INFO) << "Set voice channel options. Current options: "
1501 << options_.ToString();
1502 return true;
1503}
1504
1505bool WebRtcVoiceMediaChannel::SetRecvCodecs(
1506 const std::vector<AudioCodec>& codecs) {
solenberg566ef242015-11-06 15:34:49 -08001507 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
solenberg8fb30c32015-10-13 03:06:58 -07001508
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001509 // Set the payload types to be used for incoming media.
solenberg0b675462015-10-09 01:37:09 -07001510 LOG(LS_INFO) << "Setting receive voice codecs.";
solenberg0b675462015-10-09 01:37:09 -07001511
1512 if (!VerifyUniquePayloadTypes(codecs)) {
1513 LOG(LS_ERROR) << "Codec payload types overlap.";
1514 return false;
1515 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001516
1517 std::vector<AudioCodec> new_codecs;
1518 // Find all new codecs. We allow adding new codecs but don't allow changing
1519 // the payload type of codecs that is already configured since we might
1520 // already be receiving packets with that payload type.
Fredrik Solenbergaf9fb212015-08-26 10:45:53 +02001521 for (const AudioCodec& codec : codecs) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001522 AudioCodec old_codec;
Fredrik Solenbergaf9fb212015-08-26 10:45:53 +02001523 if (FindCodec(recv_codecs_, codec, &old_codec)) {
1524 if (old_codec.id != codec.id) {
1525 LOG(LS_ERROR) << codec.name << " payload type changed.";
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001526 return false;
1527 }
1528 } else {
Fredrik Solenbergaf9fb212015-08-26 10:45:53 +02001529 new_codecs.push_back(codec);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001530 }
1531 }
1532 if (new_codecs.empty()) {
1533 // There are no new codecs to configure. Already configured codecs are
1534 // never removed.
1535 return true;
1536 }
1537
1538 if (playout_) {
1539 // Receive codecs can not be changed while playing. So we temporarily
1540 // pause playout.
1541 PausePlayout();
1542 }
1543
solenberg26c8c912015-11-27 04:00:25 -08001544 bool result = true;
1545 for (const AudioCodec& codec : new_codecs) {
solenberg72e29d22016-03-08 06:35:16 -08001546 webrtc::CodecInst voe_codec = {0};
solenberg26c8c912015-11-27 04:00:25 -08001547 if (WebRtcVoiceEngine::ToCodecInst(codec, &voe_codec)) {
1548 LOG(LS_INFO) << ToString(codec);
1549 voe_codec.pltype = codec.id;
1550 for (const auto& ch : recv_streams_) {
1551 if (engine()->voe()->codec()->SetRecPayloadType(
1552 ch.second->channel(), voe_codec) == -1) {
1553 LOG_RTCERR2(SetRecPayloadType, ch.second->channel(),
1554 ToString(voe_codec));
1555 result = false;
1556 }
1557 }
1558 } else {
1559 LOG(LS_WARNING) << "Unknown codec " << ToString(codec);
1560 result = false;
1561 break;
1562 }
1563 }
Fredrik Solenbergaf9fb212015-08-26 10:45:53 +02001564 if (result) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001565 recv_codecs_ = codecs;
1566 }
1567
1568 if (desired_playout_ && !playout_) {
1569 ResumePlayout();
1570 }
Fredrik Solenbergaf9fb212015-08-26 10:45:53 +02001571 return result;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001572}
1573
solenberg72e29d22016-03-08 06:35:16 -08001574// Utility function called from SetSendParameters() to extract current send
1575// codec settings from the given list of codecs (originally from SDP). Both send
1576// and receive streams may be reconfigured based on the new settings.
wu@webrtc.orgcadf9042013-08-30 21:24:16 +00001577bool WebRtcVoiceMediaChannel::SetSendCodecs(
1578 const std::vector<AudioCodec>& codecs) {
solenberg566ef242015-11-06 15:34:49 -08001579 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
Fredrik Solenbergb5727682015-12-04 15:22:19 +01001580 // TODO(solenberg): Validate input - that payload types don't overlap, are
1581 // within range, filter out codecs we don't support,
solenberg31642aa2016-03-14 08:00:37 -07001582 // redundant codecs etc - the same way it is done for
1583 // RtpHeaderExtensions.
solenbergd97ec302015-10-07 01:40:33 -07001584
Fredrik Solenbergb5727682015-12-04 15:22:19 +01001585 // Find the DTMF telephone event "codec" payload type.
1586 dtmf_payload_type_ = rtc::Optional<int>();
Fredrik Solenbergaf9fb212015-08-26 10:45:53 +02001587 for (const AudioCodec& codec : codecs) {
Fredrik Solenbergaf9fb212015-08-26 10:45:53 +02001588 if (IsCodec(codec, kDtmfCodecName)) {
solenberg31642aa2016-03-14 08:00:37 -07001589 if (codec.id < kMinPayloadType || codec.id > kMaxPayloadType) {
1590 return false;
1591 }
Fredrik Solenbergb5727682015-12-04 15:22:19 +01001592 dtmf_payload_type_ = rtc::Optional<int>(codec.id);
1593 break;
wu@webrtc.orgcadf9042013-08-30 21:24:16 +00001594 }
1595 }
1596
solenberg72e29d22016-03-08 06:35:16 -08001597 // Scan through the list to figure out the codec to use for sending, along
kwiberg68061362016-06-14 08:04:47 -07001598 // with the proper configuration for VAD, CNG, NACK and Opus-specific
solenberg72e29d22016-03-08 06:35:16 -08001599 // parameters.
kwiberg68061362016-06-14 08:04:47 -07001600 // TODO(solenberg): Refactor this logic once we create AudioEncoders here.
solenberg72e29d22016-03-08 06:35:16 -08001601 {
1602 SendCodecSpec send_codec_spec;
1603 send_codec_spec.nack_enabled = send_codec_spec_.nack_enabled;
1604
1605 // Find send codec (the first non-telephone-event/CN codec).
1606 const AudioCodec* codec = WebRtcVoiceCodecs::GetPreferredCodec(
kwiberg68061362016-06-14 08:04:47 -07001607 codecs, &send_codec_spec.codec_inst);
solenberg72e29d22016-03-08 06:35:16 -08001608 if (!codec) {
1609 LOG(LS_WARNING) << "Received empty list of codecs.";
1610 return false;
1611 }
1612
1613 send_codec_spec.transport_cc_enabled = HasTransportCc(*codec);
kwiberg68061362016-06-14 08:04:47 -07001614 send_codec_spec.nack_enabled = HasNack(*codec);
solenberg72e29d22016-03-08 06:35:16 -08001615
kwiberg68061362016-06-14 08:04:47 -07001616 // For Opus as the send codec, we are to determine inband FEC, maximum
1617 // playback rate, and opus internal dtx.
1618 if (IsCodec(*codec, kOpusCodecName)) {
1619 GetOpusConfig(*codec, &send_codec_spec.codec_inst,
1620 &send_codec_spec.enable_codec_fec,
1621 &send_codec_spec.opus_max_playback_rate,
1622 &send_codec_spec.enable_opus_dtx);
1623 }
solenberg72e29d22016-03-08 06:35:16 -08001624
kwiberg68061362016-06-14 08:04:47 -07001625 // Set packet size if the AudioCodec param kCodecParamPTime is set.
1626 int ptime_ms = 0;
1627 if (codec->GetParam(kCodecParamPTime, &ptime_ms)) {
1628 if (!WebRtcVoiceCodecs::SetPTimeAsPacketSize(
1629 &send_codec_spec.codec_inst, ptime_ms)) {
1630 LOG(LS_WARNING) << "Failed to set packet size for codec "
1631 << send_codec_spec.codec_inst.plname;
1632 return false;
solenberg72e29d22016-03-08 06:35:16 -08001633 }
1634 }
1635
1636 // Loop through the codecs list again to find the CN codec.
1637 // TODO(solenberg): Break out into a separate function?
1638 for (const AudioCodec& codec : codecs) {
1639 // Ignore codecs we don't know about. The negotiation step should prevent
1640 // this, but double-check to be sure.
1641 webrtc::CodecInst voe_codec = {0};
1642 if (!WebRtcVoiceEngine::ToCodecInst(codec, &voe_codec)) {
1643 LOG(LS_WARNING) << "Unknown codec " << ToString(codec);
1644 continue;
1645 }
1646
1647 if (IsCodec(codec, kCnCodecName)) {
1648 // Turn voice activity detection/comfort noise on if supported.
1649 // Set the wideband CN payload type appropriately.
1650 // (narrowband always uses the static payload type 13).
1651 int cng_plfreq = -1;
1652 switch (codec.clockrate) {
1653 case 8000:
1654 case 16000:
1655 case 32000:
1656 cng_plfreq = codec.clockrate;
1657 break;
1658 default:
1659 LOG(LS_WARNING) << "CN frequency " << codec.clockrate
1660 << " not supported.";
1661 continue;
1662 }
1663 send_codec_spec.cng_payload_type = codec.id;
1664 send_codec_spec.cng_plfreq = cng_plfreq;
1665 break;
1666 }
1667 }
1668
1669 // Latch in the new state.
1670 send_codec_spec_ = std::move(send_codec_spec);
1671 }
1672
wu@webrtc.orgcadf9042013-08-30 21:24:16 +00001673 // Cache the codecs in order to configure the channel created later.
solenbergc96df772015-10-21 13:01:53 -07001674 for (const auto& ch : send_streams_) {
skvlade0d46372016-04-07 22:59:22 -07001675 if (!SetSendCodecs(ch.second->channel(), ch.second->rtp_parameters())) {
wu@webrtc.orgcadf9042013-08-30 21:24:16 +00001676 return false;
1677 }
1678 }
1679
solenberg72e29d22016-03-08 06:35:16 -08001680 // Set nack status on receive channels.
deadbeefb56069e2016-05-06 04:57:03 -07001681 for (const auto& kv : recv_streams_) {
1682 SetNack(kv.second->channel(), send_codec_spec_.nack_enabled);
wu@webrtc.org9dba5252013-08-05 20:36:57 +00001683 }
solenberg0a617e22015-10-20 15:49:38 -07001684
stefanba4c0e42016-02-04 04:12:24 -08001685 // Check if the transport cc feedback has changed on the preferred send codec,
1686 // and in that case reconfigure all receive streams.
solenberg72e29d22016-03-08 06:35:16 -08001687 if (recv_transport_cc_enabled_ != send_codec_spec_.transport_cc_enabled) {
1688 LOG(LS_INFO) << "Recreate all the receive streams because the send "
1689 "codec has changed.";
1690 recv_transport_cc_enabled_ = send_codec_spec_.transport_cc_enabled;
1691 for (auto& kv : recv_streams_) {
1692 kv.second->RecreateAudioReceiveStream(recv_transport_cc_enabled_);
1693 }
1694 }
1695
Taylor Brandstetter0cd086b2016-04-20 16:23:10 -07001696 send_codecs_ = codecs;
solenberg72e29d22016-03-08 06:35:16 -08001697 return true;
1698}
1699
1700// Apply current codec settings to a single voe::Channel used for sending.
skvlade0d46372016-04-07 22:59:22 -07001701bool WebRtcVoiceMediaChannel::SetSendCodecs(
1702 int channel,
1703 const webrtc::RtpParameters& rtp_parameters) {
kwiberg68061362016-06-14 08:04:47 -07001704 // Disable VAD, NACK and FEC unless we know the other side wants them.
solenberg72e29d22016-03-08 06:35:16 -08001705 engine()->voe()->codec()->SetVADStatus(channel, false);
1706 engine()->voe()->rtp()->SetNACKStatus(channel, false, 0);
solenberg72e29d22016-03-08 06:35:16 -08001707 engine()->voe()->codec()->SetFECStatus(channel, false);
1708
solenberg72e29d22016-03-08 06:35:16 -08001709 SetNack(channel, send_codec_spec_.nack_enabled);
1710
1711 // Set the codec immediately, since SetVADStatus() depends on whether
1712 // the current codec is mono or stereo.
1713 if (!SetSendCodec(channel, send_codec_spec_.codec_inst)) {
1714 return false;
1715 }
1716
1717 // FEC should be enabled after SetSendCodec.
1718 if (send_codec_spec_.enable_codec_fec) {
1719 LOG(LS_INFO) << "Attempt to enable codec internal FEC on channel "
1720 << channel;
1721 if (engine()->voe()->codec()->SetFECStatus(channel, true) == -1) {
1722 // Enable codec internal FEC. Treat any failure as fatal internal error.
1723 LOG_RTCERR2(SetFECStatus, channel, true);
1724 return false;
1725 }
1726 }
1727
1728 if (IsCodec(send_codec_spec_.codec_inst, kOpusCodecName)) {
1729 // DTX and maxplaybackrate should be set after SetSendCodec. Because current
1730 // send codec has to be Opus.
1731
1732 // Set Opus internal DTX.
1733 LOG(LS_INFO) << "Attempt to "
1734 << (send_codec_spec_.enable_opus_dtx ? "enable" : "disable")
1735 << " Opus DTX on channel "
1736 << channel;
1737 if (engine()->voe()->codec()->SetOpusDtx(channel,
1738 send_codec_spec_.enable_opus_dtx)) {
1739 LOG_RTCERR2(SetOpusDtx, channel, send_codec_spec_.enable_opus_dtx);
1740 return false;
1741 }
1742
1743 // If opus_max_playback_rate <= 0, the default maximum playback rate
1744 // (48 kHz) will be used.
1745 if (send_codec_spec_.opus_max_playback_rate > 0) {
1746 LOG(LS_INFO) << "Attempt to set maximum playback rate to "
1747 << send_codec_spec_.opus_max_playback_rate
1748 << " Hz on channel "
1749 << channel;
1750 if (engine()->voe()->codec()->SetOpusMaxPlaybackRate(
1751 channel, send_codec_spec_.opus_max_playback_rate) == -1) {
1752 LOG_RTCERR2(SetOpusMaxPlaybackRate, channel,
1753 send_codec_spec_.opus_max_playback_rate);
1754 return false;
stefanba4c0e42016-02-04 04:12:24 -08001755 }
1756 }
1757 }
deadbeef80346142016-04-27 14:17:10 -07001758 // TODO(solenberg): SetMaxSendBitrate() yields another call to SetSendCodec().
skvlade0d46372016-04-07 22:59:22 -07001759 // Check if it is possible to fuse with the previous call in this function.
Taylor Brandstetterdb0cd9e2016-05-16 11:40:30 -07001760 SetChannelSendParameters(channel, rtp_parameters);
solenberg72e29d22016-03-08 06:35:16 -08001761
1762 // Set the CN payloadtype and the VAD status.
1763 if (send_codec_spec_.cng_payload_type != -1) {
1764 // The CN payload type for 8000 Hz clockrate is fixed at 13.
1765 if (send_codec_spec_.cng_plfreq != 8000) {
1766 webrtc::PayloadFrequencies cn_freq;
1767 switch (send_codec_spec_.cng_plfreq) {
1768 case 16000:
1769 cn_freq = webrtc::kFreq16000Hz;
1770 break;
1771 case 32000:
1772 cn_freq = webrtc::kFreq32000Hz;
1773 break;
1774 default:
1775 RTC_NOTREACHED();
1776 return false;
1777 }
1778 if (engine()->voe()->codec()->SetSendCNPayloadType(
1779 channel, send_codec_spec_.cng_payload_type, cn_freq) == -1) {
1780 LOG_RTCERR3(SetSendCNPayloadType, channel,
1781 send_codec_spec_.cng_payload_type, cn_freq);
1782 // TODO(ajm): This failure condition will be removed from VoE.
1783 // Restore the return here when we update to a new enough webrtc.
1784 //
1785 // Not returning false because the SetSendCNPayloadType will fail if
1786 // the channel is already sending.
1787 // This can happen if the remote description is applied twice, for
1788 // example in the case of ROAP on top of JSEP, where both side will
1789 // send the offer.
1790 }
1791 }
1792
1793 // Only turn on VAD if we have a CN payload type that matches the
1794 // clockrate for the codec we are going to use.
1795 if (send_codec_spec_.cng_plfreq == send_codec_spec_.codec_inst.plfreq &&
1796 send_codec_spec_.codec_inst.channels == 1) {
1797 // TODO(minyue): If CN frequency == 48000 Hz is allowed, consider the
1798 // interaction between VAD and Opus FEC.
1799 LOG(LS_INFO) << "Enabling VAD";
1800 if (engine()->voe()->codec()->SetVADStatus(channel, true) == -1) {
1801 LOG_RTCERR2(SetVADStatus, channel, true);
1802 return false;
1803 }
1804 }
1805 }
solenberg0a617e22015-10-20 15:49:38 -07001806 return true;
wu@webrtc.org9dba5252013-08-05 20:36:57 +00001807}
1808
wu@webrtc.orgcadf9042013-08-30 21:24:16 +00001809void WebRtcVoiceMediaChannel::SetNack(int channel, bool nack_enabled) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001810 if (nack_enabled) {
wu@webrtc.orgcadf9042013-08-30 21:24:16 +00001811 LOG(LS_INFO) << "Enabling NACK for channel " << channel;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001812 engine()->voe()->rtp()->SetNACKStatus(channel, true, kNackMaxPackets);
1813 } else {
wu@webrtc.orgcadf9042013-08-30 21:24:16 +00001814 LOG(LS_INFO) << "Disabling NACK for channel " << channel;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001815 engine()->voe()->rtp()->SetNACKStatus(channel, false, 0);
1816 }
1817}
1818
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001819bool WebRtcVoiceMediaChannel::SetSendCodec(
wu@webrtc.org9dba5252013-08-05 20:36:57 +00001820 int channel, const webrtc::CodecInst& send_codec) {
1821 LOG(LS_INFO) << "Send channel " << channel << " selected voice codec "
1822 << ToString(send_codec) << ", bitrate=" << send_codec.rate;
1823
solenberg72e29d22016-03-08 06:35:16 -08001824 webrtc::CodecInst current_codec = {0};
wu@webrtc.org05e7b442014-04-01 17:44:24 +00001825 if (engine()->voe()->codec()->GetSendCodec(channel, current_codec) == 0 &&
1826 (send_codec == current_codec)) {
1827 // Codec is already configured, we can return without setting it again.
1828 return true;
1829 }
1830
wu@webrtc.org9dba5252013-08-05 20:36:57 +00001831 if (engine()->voe()->codec()->SetSendCodec(channel, send_codec) == -1) {
1832 LOG_RTCERR2(SetSendCodec, channel, ToString(send_codec));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001833 return false;
1834 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001835 return true;
1836}
1837
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001838bool WebRtcVoiceMediaChannel::SetPlayout(bool playout) {
1839 desired_playout_ = playout;
1840 return ChangePlayout(desired_playout_);
1841}
1842
1843bool WebRtcVoiceMediaChannel::PausePlayout() {
1844 return ChangePlayout(false);
1845}
1846
1847bool WebRtcVoiceMediaChannel::ResumePlayout() {
1848 return ChangePlayout(desired_playout_);
1849}
1850
1851bool WebRtcVoiceMediaChannel::ChangePlayout(bool playout) {
Peter Boströmca8b4042016-03-08 14:24:13 -08001852 TRACE_EVENT0("webrtc", "WebRtcVoiceMediaChannel::ChangePlayout");
solenberg566ef242015-11-06 15:34:49 -08001853 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001854 if (playout_ == playout) {
1855 return true;
1856 }
1857
solenberg7add0582015-11-20 09:59:34 -08001858 for (const auto& ch : recv_streams_) {
Fredrik Solenbergaf9fb212015-08-26 10:45:53 +02001859 if (!SetPlayout(ch.second->channel(), playout)) {
henrike@webrtc.org1e09a712013-07-26 19:17:59 +00001860 LOG(LS_ERROR) << "SetPlayout " << playout << " on channel "
Fredrik Solenbergaf9fb212015-08-26 10:45:53 +02001861 << ch.second->channel() << " failed";
solenberg1ac56142015-10-13 03:58:19 -07001862 return false;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001863 }
1864 }
solenberg1ac56142015-10-13 03:58:19 -07001865 playout_ = playout;
1866 return true;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001867}
1868
Taylor Brandstetter1a018dc2016-03-08 12:37:39 -08001869void WebRtcVoiceMediaChannel::SetSend(bool send) {
Peter Boströmca8b4042016-03-08 14:24:13 -08001870 TRACE_EVENT0("webrtc", "WebRtcVoiceMediaChannel::SetSend");
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001871 if (send_ == send) {
Taylor Brandstetter1a018dc2016-03-08 12:37:39 -08001872 return;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001873 }
1874
solenbergd53a3f92016-04-14 13:56:37 -07001875 // Apply channel specific options, and initialize the ADM for recording (this
1876 // may take time on some platforms, e.g. Android).
Taylor Brandstetter1a018dc2016-03-08 12:37:39 -08001877 if (send) {
solenberg63b34542015-09-29 06:06:31 -07001878 engine()->ApplyOptions(options_);
solenbergd53a3f92016-04-14 13:56:37 -07001879
1880 // InitRecording() may return an error if the ADM is already recording.
1881 if (!engine()->adm()->RecordingIsInitialized() &&
1882 !engine()->adm()->Recording()) {
1883 if (engine()->adm()->InitRecording() != 0) {
1884 LOG(LS_WARNING) << "Failed to initialize recording";
1885 }
1886 }
solenberg63b34542015-09-29 06:06:31 -07001887 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001888
wu@webrtc.org9dba5252013-08-05 20:36:57 +00001889 // Change the settings on each send channel.
Taylor Brandstetter1a018dc2016-03-08 12:37:39 -08001890 for (auto& kv : send_streams_) {
1891 kv.second->SetSend(send);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001892 }
wu@webrtc.org9dba5252013-08-05 20:36:57 +00001893
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001894 send_ = send;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001895}
1896
Peter Boström0c4e06b2015-10-07 12:23:21 +02001897bool WebRtcVoiceMediaChannel::SetAudioSend(uint32_t ssrc,
1898 bool enable,
solenberg1dd98f32015-09-10 01:57:14 -07001899 const AudioOptions* options,
Taylor Brandstetter1a018dc2016-03-08 12:37:39 -08001900 AudioSource* source) {
solenberg566ef242015-11-06 15:34:49 -08001901 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
solenberg1dd98f32015-09-10 01:57:14 -07001902 // TODO(solenberg): The state change should be fully rolled back if any one of
1903 // these calls fail.
Taylor Brandstetter1a018dc2016-03-08 12:37:39 -08001904 if (!SetLocalSource(ssrc, source)) {
solenberg1dd98f32015-09-10 01:57:14 -07001905 return false;
1906 }
solenbergdfc8f4f2015-10-01 02:31:10 -07001907 if (!MuteStream(ssrc, !enable)) {
solenberg1dd98f32015-09-10 01:57:14 -07001908 return false;
1909 }
solenbergdfc8f4f2015-10-01 02:31:10 -07001910 if (enable && options) {
solenberg1dd98f32015-09-10 01:57:14 -07001911 return SetOptions(*options);
1912 }
1913 return true;
1914}
1915
solenberg0a617e22015-10-20 15:49:38 -07001916int WebRtcVoiceMediaChannel::CreateVoEChannel() {
1917 int id = engine()->CreateVoEChannel();
1918 if (id == -1) {
1919 LOG_RTCERR0(CreateVoEChannel);
1920 return -1;
wu@webrtc.org9dba5252013-08-05 20:36:57 +00001921 }
mflodman3d7db262016-04-29 00:57:13 -07001922
solenberg0a617e22015-10-20 15:49:38 -07001923 return id;
wu@webrtc.org9dba5252013-08-05 20:36:57 +00001924}
1925
solenberg7add0582015-11-20 09:59:34 -08001926bool WebRtcVoiceMediaChannel::DeleteVoEChannel(int channel) {
wu@webrtc.org9dba5252013-08-05 20:36:57 +00001927 if (engine()->voe()->base()->DeleteChannel(channel) == -1) {
1928 LOG_RTCERR1(DeleteChannel, channel);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001929 return false;
1930 }
wu@webrtc.org9dba5252013-08-05 20:36:57 +00001931 return true;
1932}
henrike@webrtc.org1e09a712013-07-26 19:17:59 +00001933
wu@webrtc.org9dba5252013-08-05 20:36:57 +00001934bool WebRtcVoiceMediaChannel::AddSendStream(const StreamParams& sp) {
Peter Boströmca8b4042016-03-08 14:24:13 -08001935 TRACE_EVENT0("webrtc", "WebRtcVoiceMediaChannel::AddSendStream");
solenberg566ef242015-11-06 15:34:49 -08001936 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
solenberg0a617e22015-10-20 15:49:38 -07001937 LOG(LS_INFO) << "AddSendStream: " << sp.ToString();
1938
1939 uint32_t ssrc = sp.first_ssrc();
1940 RTC_DCHECK(0 != ssrc);
1941
1942 if (GetSendChannelId(ssrc) != -1) {
1943 LOG(LS_ERROR) << "Stream already exists with ssrc " << ssrc;
wu@webrtc.org9dba5252013-08-05 20:36:57 +00001944 return false;
1945 }
1946
solenberg0a617e22015-10-20 15:49:38 -07001947 // Create a new channel for sending audio data.
1948 int channel = CreateVoEChannel();
1949 if (channel == -1) {
1950 return false;
wu@webrtc.org9dba5252013-08-05 20:36:57 +00001951 }
wu@webrtc.org9dba5252013-08-05 20:36:57 +00001952
solenbergc96df772015-10-21 13:01:53 -07001953 // Save the channel to send_streams_, so that RemoveSendStream() can still
wu@webrtc.org9dba5252013-08-05 20:36:57 +00001954 // delete the channel in case failure happens below.
mallinath@webrtc.org67ee6b92014-02-03 16:57:16 +00001955 webrtc::AudioTransport* audio_transport =
1956 engine()->voe()->base()->audio_transport();
mflodman3d7db262016-04-29 00:57:13 -07001957
skvlade0d46372016-04-07 22:59:22 -07001958 WebRtcAudioSendStream* stream = new WebRtcAudioSendStream(
mflodman3d7db262016-04-29 00:57:13 -07001959 channel, audio_transport, ssrc, sp.cname, send_rtp_extensions_, call_,
1960 this);
skvlade0d46372016-04-07 22:59:22 -07001961 send_streams_.insert(std::make_pair(ssrc, stream));
wu@webrtc.org9dba5252013-08-05 20:36:57 +00001962
solenberg0a617e22015-10-20 15:49:38 -07001963 // Set the current codecs to be used for the new channel. We need to do this
1964 // after adding the channel to send_channels_, because of how max bitrate is
1965 // currently being configured by SetSendCodec().
skvlade0d46372016-04-07 22:59:22 -07001966 if (HasSendCodec() && !SetSendCodecs(channel, stream->rtp_parameters())) {
solenberg0a617e22015-10-20 15:49:38 -07001967 RemoveSendStream(ssrc);
wu@webrtc.org9dba5252013-08-05 20:36:57 +00001968 return false;
1969 }
1970
1971 // At this point the channel's local SSRC has been updated. If the channel is
solenberg0a617e22015-10-20 15:49:38 -07001972 // the first send channel make sure that all the receive channels are updated
1973 // with the same SSRC in order to send receiver reports.
solenbergc96df772015-10-21 13:01:53 -07001974 if (send_streams_.size() == 1) {
solenberg0a617e22015-10-20 15:49:38 -07001975 receiver_reports_ssrc_ = ssrc;
solenberg7add0582015-11-20 09:59:34 -08001976 for (const auto& stream : recv_streams_) {
1977 int recv_channel = stream.second->channel();
solenberg0a617e22015-10-20 15:49:38 -07001978 if (engine()->voe()->rtp()->SetLocalSSRC(recv_channel, ssrc) != 0) {
solenberg7add0582015-11-20 09:59:34 -08001979 LOG_RTCERR2(SetLocalSSRC, recv_channel, ssrc);
solenberg1ac56142015-10-13 03:58:19 -07001980 return false;
wu@webrtc.org9dba5252013-08-05 20:36:57 +00001981 }
solenberg0a617e22015-10-20 15:49:38 -07001982 engine()->voe()->base()->AssociateSendChannel(recv_channel, channel);
1983 LOG(LS_INFO) << "VoiceEngine channel #" << recv_channel
1984 << " is associated with channel #" << channel << ".";
wu@webrtc.org9dba5252013-08-05 20:36:57 +00001985 }
1986 }
1987
Taylor Brandstetter1a018dc2016-03-08 12:37:39 -08001988 send_streams_[ssrc]->SetSend(send_);
1989 return true;
wu@webrtc.org9dba5252013-08-05 20:36:57 +00001990}
1991
Peter Boström0c4e06b2015-10-07 12:23:21 +02001992bool WebRtcVoiceMediaChannel::RemoveSendStream(uint32_t ssrc) {
Peter Boströmca8b4042016-03-08 14:24:13 -08001993 TRACE_EVENT0("webrtc", "WebRtcVoiceMediaChannel::RemoveSendStream");
solenberg566ef242015-11-06 15:34:49 -08001994 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
solenberg3a941542015-11-16 07:34:50 -08001995 LOG(LS_INFO) << "RemoveSendStream: " << ssrc;
1996
solenbergc96df772015-10-21 13:01:53 -07001997 auto it = send_streams_.find(ssrc);
1998 if (it == send_streams_.end()) {
wu@webrtc.org9dba5252013-08-05 20:36:57 +00001999 LOG(LS_WARNING) << "Try to remove stream with ssrc " << ssrc
2000 << " which doesn't exist.";
2001 return false;
2002 }
2003
Taylor Brandstetter1a018dc2016-03-08 12:37:39 -08002004 it->second->SetSend(false);
wu@webrtc.org9dba5252013-08-05 20:36:57 +00002005
solenberg7add0582015-11-20 09:59:34 -08002006 // Clean up and delete the send stream+channel.
Taylor Brandstetter1a018dc2016-03-08 12:37:39 -08002007 int channel = it->second->channel();
solenberg0a617e22015-10-20 15:49:38 -07002008 LOG(LS_INFO) << "Removing audio send stream " << ssrc
2009 << " with VoiceEngine channel #" << channel << ".";
solenberg7add0582015-11-20 09:59:34 -08002010 delete it->second;
2011 send_streams_.erase(it);
2012 if (!DeleteVoEChannel(channel)) {
solenberg0a617e22015-10-20 15:49:38 -07002013 return false;
wu@webrtc.org9dba5252013-08-05 20:36:57 +00002014 }
solenbergc96df772015-10-21 13:01:53 -07002015 if (send_streams_.empty()) {
Taylor Brandstetter1a018dc2016-03-08 12:37:39 -08002016 SetSend(false);
solenberg0a617e22015-10-20 15:49:38 -07002017 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002018 return true;
2019}
2020
2021bool WebRtcVoiceMediaChannel::AddRecvStream(const StreamParams& sp) {
Peter Boströmca8b4042016-03-08 14:24:13 -08002022 TRACE_EVENT0("webrtc", "WebRtcVoiceMediaChannel::AddRecvStream");
solenberg566ef242015-11-06 15:34:49 -08002023 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
solenbergd97ec302015-10-07 01:40:33 -07002024 LOG(LS_INFO) << "AddRecvStream: " << sp.ToString();
2025
solenberg0b675462015-10-09 01:37:09 -07002026 if (!ValidateStreamParams(sp)) {
wu@webrtc.org78187522013-10-07 23:32:02 +00002027 return false;
2028 }
2029
solenberg7add0582015-11-20 09:59:34 -08002030 const uint32_t ssrc = sp.first_ssrc();
solenberg0b675462015-10-09 01:37:09 -07002031 if (ssrc == 0) {
2032 LOG(LS_WARNING) << "AddRecvStream with ssrc==0 is not supported.";
2033 return false;
2034 }
2035
solenberg1ac56142015-10-13 03:58:19 -07002036 // Remove the default receive stream if one had been created with this ssrc;
2037 // we'll recreate it then.
2038 if (IsDefaultRecvStream(ssrc)) {
2039 RemoveRecvStream(ssrc);
2040 }
solenberg0b675462015-10-09 01:37:09 -07002041
solenberg7add0582015-11-20 09:59:34 -08002042 if (GetReceiveChannelId(ssrc) != -1) {
henrike@webrtc.org1e09a712013-07-26 19:17:59 +00002043 LOG(LS_ERROR) << "Stream already exists with ssrc " << ssrc;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002044 return false;
2045 }
Fredrik Solenberg4b60c732015-05-07 14:07:48 +02002046
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002047 // Create a new channel for receiving audio data.
solenberg7add0582015-11-20 09:59:34 -08002048 const int channel = CreateVoEChannel();
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002049 if (channel == -1) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002050 return false;
2051 }
Minyue2013aec2015-05-13 14:14:42 +02002052
solenberg1ac56142015-10-13 03:58:19 -07002053 // Turn off all supported codecs.
solenberg26c8c912015-11-27 04:00:25 -08002054 // TODO(solenberg): Remove once "no codecs" is the default state of a stream.
2055 for (webrtc::CodecInst voe_codec : webrtc::acm2::RentACodec::Database()) {
2056 voe_codec.pltype = -1;
2057 if (engine()->voe()->codec()->SetRecPayloadType(channel, voe_codec) == -1) {
2058 LOG_RTCERR2(SetRecPayloadType, channel, ToString(voe_codec));
2059 DeleteVoEChannel(channel);
2060 return false;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002061 }
2062 }
2063
solenberg1ac56142015-10-13 03:58:19 -07002064 // Only enable those configured for this channel.
2065 for (const auto& codec : recv_codecs_) {
solenberg72e29d22016-03-08 06:35:16 -08002066 webrtc::CodecInst voe_codec = {0};
solenberg26c8c912015-11-27 04:00:25 -08002067 if (WebRtcVoiceEngine::ToCodecInst(codec, &voe_codec)) {
solenberg1ac56142015-10-13 03:58:19 -07002068 voe_codec.pltype = codec.id;
2069 if (engine()->voe()->codec()->SetRecPayloadType(
2070 channel, voe_codec) == -1) {
2071 LOG_RTCERR2(SetRecPayloadType, channel, ToString(voe_codec));
solenberg7add0582015-11-20 09:59:34 -08002072 DeleteVoEChannel(channel);
solenberg1ac56142015-10-13 03:58:19 -07002073 return false;
2074 }
henrike@webrtc.org1e09a712013-07-26 19:17:59 +00002075 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002076 }
solenberg8fb30c32015-10-13 03:06:58 -07002077
solenberg7add0582015-11-20 09:59:34 -08002078 const int send_channel = GetSendChannelId(receiver_reports_ssrc_);
2079 if (send_channel != -1) {
2080 // Associate receive channel with first send channel (so the receive channel
2081 // can obtain RTT from the send channel)
2082 engine()->voe()->base()->AssociateSendChannel(channel, send_channel);
2083 LOG(LS_INFO) << "VoiceEngine channel #" << channel
2084 << " is associated with channel #" << send_channel << ".";
buildbot@webrtc.org150835e2014-05-06 15:54:38 +00002085 }
2086
stefanba4c0e42016-02-04 04:12:24 -08002087 recv_streams_.insert(std::make_pair(
2088 ssrc, new WebRtcAudioReceiveStream(channel, ssrc, receiver_reports_ssrc_,
solenberg72e29d22016-03-08 06:35:16 -08002089 recv_transport_cc_enabled_,
2090 sp.sync_label, recv_rtp_extensions_,
ossu29b1a8d2016-06-13 07:34:51 -07002091 call_, this,
2092 engine()->decoder_factory_)));
solenberg7add0582015-11-20 09:59:34 -08002093
solenberg72e29d22016-03-08 06:35:16 -08002094 SetNack(channel, send_codec_spec_.nack_enabled);
solenberg1ac56142015-10-13 03:58:19 -07002095 SetPlayout(channel, playout_);
solenberg7add0582015-11-20 09:59:34 -08002096
solenberg1ac56142015-10-13 03:58:19 -07002097 return true;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002098}
2099
Peter Boström0c4e06b2015-10-07 12:23:21 +02002100bool WebRtcVoiceMediaChannel::RemoveRecvStream(uint32_t ssrc) {
Peter Boströmca8b4042016-03-08 14:24:13 -08002101 TRACE_EVENT0("webrtc", "WebRtcVoiceMediaChannel::RemoveRecvStream");
solenberg566ef242015-11-06 15:34:49 -08002102 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
solenbergd97ec302015-10-07 01:40:33 -07002103 LOG(LS_INFO) << "RemoveRecvStream: " << ssrc;
2104
solenberg7add0582015-11-20 09:59:34 -08002105 const auto it = recv_streams_.find(ssrc);
2106 if (it == recv_streams_.end()) {
wu@webrtc.org9dba5252013-08-05 20:36:57 +00002107 LOG(LS_WARNING) << "Try to remove stream with ssrc " << ssrc
2108 << " which doesn't exist.";
henrike@webrtc.org1e09a712013-07-26 19:17:59 +00002109 return false;
wu@webrtc.org9dba5252013-08-05 20:36:57 +00002110 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002111
solenberg1ac56142015-10-13 03:58:19 -07002112 // Deregister default channel, if that's the one being destroyed.
2113 if (IsDefaultRecvStream(ssrc)) {
2114 default_recv_ssrc_ = -1;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002115 }
henrike@webrtc.org1e09a712013-07-26 19:17:59 +00002116
solenberg7add0582015-11-20 09:59:34 -08002117 const int channel = it->second->channel();
2118
2119 // Clean up and delete the receive stream+channel.
2120 LOG(LS_INFO) << "Removing audio receive stream " << ssrc
mallinath@webrtc.org67ee6b92014-02-03 16:57:16 +00002121 << " with VoiceEngine channel #" << channel << ".";
Tommif888bb52015-12-12 01:37:01 +01002122 it->second->SetRawAudioSink(nullptr);
solenberg7add0582015-11-20 09:59:34 -08002123 delete it->second;
2124 recv_streams_.erase(it);
2125 return DeleteVoEChannel(channel);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002126}
2127
Taylor Brandstetter1a018dc2016-03-08 12:37:39 -08002128bool WebRtcVoiceMediaChannel::SetLocalSource(uint32_t ssrc,
2129 AudioSource* source) {
solenbergc96df772015-10-21 13:01:53 -07002130 auto it = send_streams_.find(ssrc);
2131 if (it == send_streams_.end()) {
Taylor Brandstetter1a018dc2016-03-08 12:37:39 -08002132 if (source) {
2133 // Return an error if trying to set a valid source with an invalid ssrc.
2134 LOG(LS_ERROR) << "SetLocalSource failed with ssrc " << ssrc;
wu@webrtc.org9dba5252013-08-05 20:36:57 +00002135 return false;
2136 }
2137
2138 // The channel likely has gone away, do nothing.
henrike@webrtc.org1e09a712013-07-26 19:17:59 +00002139 return true;
henrike@webrtc.org1e09a712013-07-26 19:17:59 +00002140 }
2141
Taylor Brandstetter1a018dc2016-03-08 12:37:39 -08002142 if (source) {
2143 it->second->SetSource(source);
solenberg1ac56142015-10-13 03:58:19 -07002144 } else {
Taylor Brandstetter1a018dc2016-03-08 12:37:39 -08002145 it->second->ClearSource();
solenberg1ac56142015-10-13 03:58:19 -07002146 }
henrike@webrtc.org1e09a712013-07-26 19:17:59 +00002147
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002148 return true;
2149}
2150
2151bool WebRtcVoiceMediaChannel::GetActiveStreams(
2152 AudioInfo::StreamList* actives) {
solenberg566ef242015-11-06 15:34:49 -08002153 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002154 actives->clear();
solenberg7add0582015-11-20 09:59:34 -08002155 for (const auto& ch : recv_streams_) {
Fredrik Solenbergaf9fb212015-08-26 10:45:53 +02002156 int level = GetOutputLevel(ch.second->channel());
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002157 if (level > 0) {
Fredrik Solenbergaf9fb212015-08-26 10:45:53 +02002158 actives->push_back(std::make_pair(ch.first, level));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002159 }
2160 }
2161 return true;
2162}
2163
2164int WebRtcVoiceMediaChannel::GetOutputLevel() {
solenberg566ef242015-11-06 15:34:49 -08002165 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
solenberg1ac56142015-10-13 03:58:19 -07002166 int highest = 0;
solenberg7add0582015-11-20 09:59:34 -08002167 for (const auto& ch : recv_streams_) {
solenberg8fb30c32015-10-13 03:06:58 -07002168 highest = std::max(GetOutputLevel(ch.second->channel()), highest);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002169 }
2170 return highest;
2171}
2172
2173int WebRtcVoiceMediaChannel::GetTimeSinceLastTyping() {
2174 int ret;
2175 if (engine()->voe()->processing()->TimeSinceLastTyping(ret) == -1) {
2176 // In case of error, log the info and continue
2177 LOG_RTCERR0(TimeSinceLastTyping);
2178 ret = -1;
2179 } else {
2180 ret *= 1000; // We return ms, webrtc returns seconds.
2181 }
2182 return ret;
2183}
2184
2185void WebRtcVoiceMediaChannel::SetTypingDetectionParameters(int time_window,
2186 int cost_per_typing, int reporting_threshold, int penalty_decay,
2187 int type_event_delay) {
2188 if (engine()->voe()->processing()->SetTypingDetectionParameters(
2189 time_window, cost_per_typing,
2190 reporting_threshold, penalty_decay, type_event_delay) == -1) {
2191 // In case of error, log the info and continue
2192 LOG_RTCERR5(SetTypingDetectionParameters, time_window,
2193 cost_per_typing, reporting_threshold, penalty_decay,
2194 type_event_delay);
2195 }
2196}
2197
solenberg4bac9c52015-10-09 02:32:53 -07002198bool WebRtcVoiceMediaChannel::SetOutputVolume(uint32_t ssrc, double volume) {
solenberg566ef242015-11-06 15:34:49 -08002199 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
solenberg1ac56142015-10-13 03:58:19 -07002200 if (ssrc == 0) {
2201 default_recv_volume_ = volume;
2202 if (default_recv_ssrc_ == -1) {
2203 return true;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002204 }
solenberg1ac56142015-10-13 03:58:19 -07002205 ssrc = static_cast<uint32_t>(default_recv_ssrc_);
2206 }
2207 int ch_id = GetReceiveChannelId(ssrc);
2208 if (ch_id < 0) {
2209 LOG(LS_WARNING) << "Cannot find channel for ssrc:" << ssrc;
2210 return false;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002211 }
2212
solenberg1ac56142015-10-13 03:58:19 -07002213 if (-1 == engine()->voe()->volume()->SetChannelOutputVolumeScaling(ch_id,
2214 volume)) {
2215 LOG_RTCERR2(SetChannelOutputVolumeScaling, ch_id, volume);
2216 return false;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002217 }
solenberg1ac56142015-10-13 03:58:19 -07002218 LOG(LS_INFO) << "SetOutputVolume to " << volume
2219 << " for channel " << ch_id << " and ssrc " << ssrc;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002220 return true;
2221}
2222
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002223bool WebRtcVoiceMediaChannel::CanInsertDtmf() {
Fredrik Solenbergb5727682015-12-04 15:22:19 +01002224 return dtmf_payload_type_ ? true : false;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002225}
2226
solenberg1d63dd02015-12-02 12:35:09 -08002227bool WebRtcVoiceMediaChannel::InsertDtmf(uint32_t ssrc, int event,
2228 int duration) {
solenberg566ef242015-11-06 15:34:49 -08002229 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
Fredrik Solenbergb5727682015-12-04 15:22:19 +01002230 LOG(LS_INFO) << "WebRtcVoiceMediaChannel::InsertDtmf";
2231 if (!dtmf_payload_type_) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002232 return false;
2233 }
2234
Fredrik Solenbergb5727682015-12-04 15:22:19 +01002235 // Figure out which WebRtcAudioSendStream to send the event on.
2236 auto it = ssrc != 0 ? send_streams_.find(ssrc) : send_streams_.begin();
2237 if (it == send_streams_.end()) {
2238 LOG(LS_WARNING) << "The specified ssrc " << ssrc << " is not in use.";
solenberg1d63dd02015-12-02 12:35:09 -08002239 return false;
2240 }
Fredrik Solenbergb5727682015-12-04 15:22:19 +01002241 if (event < kMinTelephoneEventCode ||
2242 event > kMaxTelephoneEventCode) {
2243 LOG(LS_WARNING) << "DTMF event code " << event << " out of range.";
solenberg1d63dd02015-12-02 12:35:09 -08002244 return false;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002245 }
Fredrik Solenbergb5727682015-12-04 15:22:19 +01002246 if (duration < kMinTelephoneEventDuration ||
2247 duration > kMaxTelephoneEventDuration) {
2248 LOG(LS_WARNING) << "DTMF event duration " << duration << " out of range.";
2249 return false;
2250 }
2251 return it->second->SendTelephoneEvent(*dtmf_payload_type_, event, duration);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002252}
2253
wu@webrtc.orga9890802013-12-13 00:21:03 +00002254void WebRtcVoiceMediaChannel::OnPacketReceived(
jbaucheec21bd2016-03-20 06:15:43 -07002255 rtc::CopyOnWriteBuffer* packet, const rtc::PacketTime& packet_time) {
solenberg566ef242015-11-06 15:34:49 -08002256 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
Fredrik Solenberg4b60c732015-05-07 14:07:48 +02002257
mflodman3d7db262016-04-29 00:57:13 -07002258 const webrtc::PacketTime webrtc_packet_time(packet_time.timestamp,
2259 packet_time.not_before);
2260 webrtc::PacketReceiver::DeliveryStatus delivery_result =
2261 call_->Receiver()->DeliverPacket(webrtc::MediaType::AUDIO,
2262 packet->cdata(), packet->size(),
2263 webrtc_packet_time);
mflodman3d7db262016-04-29 00:57:13 -07002264 if (delivery_result != webrtc::PacketReceiver::DELIVERY_UNKNOWN_SSRC) {
2265 return;
2266 }
2267
2268 // Create a default receive stream for this unsignalled and previously not
2269 // received ssrc. If there already is a default receive stream, delete it.
2270 // See: https://bugs.chromium.org/p/webrtc/issues/detail?id=5208
solenberg1ac56142015-10-13 03:58:19 -07002271 uint32_t ssrc = 0;
jbaucheec21bd2016-03-20 06:15:43 -07002272 if (!GetRtpSsrc(packet->cdata(), packet->size(), &ssrc)) {
solenberg1ac56142015-10-13 03:58:19 -07002273 return;
2274 }
2275
mflodman3d7db262016-04-29 00:57:13 -07002276 if (default_recv_ssrc_ != -1) {
2277 LOG(LS_INFO) << "Removing default receive stream with ssrc "
2278 << default_recv_ssrc_;
2279 RTC_DCHECK_NE(ssrc, default_recv_ssrc_);
2280 RemoveRecvStream(default_recv_ssrc_);
2281 default_recv_ssrc_ = -1;
solenberg1ac56142015-10-13 03:58:19 -07002282 }
2283
mflodman3d7db262016-04-29 00:57:13 -07002284 StreamParams sp;
2285 sp.ssrcs.push_back(ssrc);
2286 LOG(LS_INFO) << "Creating default receive stream for SSRC=" << ssrc << ".";
2287 if (!AddRecvStream(sp)) {
2288 LOG(LS_WARNING) << "Could not create default receive stream.";
2289 return;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002290 }
mflodman3d7db262016-04-29 00:57:13 -07002291 default_recv_ssrc_ = ssrc;
2292 SetOutputVolume(default_recv_ssrc_, default_recv_volume_);
2293 if (default_sink_) {
2294 std::unique_ptr<webrtc::AudioSinkInterface> proxy_sink(
2295 new ProxySink(default_sink_.get()));
2296 SetRawAudioSink(default_recv_ssrc_, std::move(proxy_sink));
2297 }
2298 delivery_result = call_->Receiver()->DeliverPacket(webrtc::MediaType::AUDIO,
2299 packet->cdata(),
2300 packet->size(),
2301 webrtc_packet_time);
2302 RTC_DCHECK_NE(webrtc::PacketReceiver::DELIVERY_UNKNOWN_SSRC, delivery_result);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002303}
2304
wu@webrtc.orga9890802013-12-13 00:21:03 +00002305void WebRtcVoiceMediaChannel::OnRtcpReceived(
jbaucheec21bd2016-03-20 06:15:43 -07002306 rtc::CopyOnWriteBuffer* packet, const rtc::PacketTime& packet_time) {
solenberg566ef242015-11-06 15:34:49 -08002307 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
Fredrik Solenberg4b60c732015-05-07 14:07:48 +02002308
Fredrik Solenberg709ed672015-09-15 12:26:33 +02002309 // Forward packet to Call as well.
2310 const webrtc::PacketTime webrtc_packet_time(packet_time.timestamp,
2311 packet_time.not_before);
2312 call_->Receiver()->DeliverPacket(webrtc::MediaType::AUDIO,
jbaucheec21bd2016-03-20 06:15:43 -07002313 packet->cdata(), packet->size(), webrtc_packet_time);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002314}
2315
Honghai Zhangcc411c02016-03-29 17:27:21 -07002316void WebRtcVoiceMediaChannel::OnNetworkRouteChanged(
2317 const std::string& transport_name,
Honghai Zhang0e533ef2016-04-19 15:41:36 -07002318 const rtc::NetworkRoute& network_route) {
2319 call_->OnNetworkRouteChanged(transport_name, network_route);
Honghai Zhangcc411c02016-03-29 17:27:21 -07002320}
2321
Peter Boström0c4e06b2015-10-07 12:23:21 +02002322bool WebRtcVoiceMediaChannel::MuteStream(uint32_t ssrc, bool muted) {
solenberg566ef242015-11-06 15:34:49 -08002323 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
solenberg0a617e22015-10-20 15:49:38 -07002324 int channel = GetSendChannelId(ssrc);
wu@webrtc.org9dba5252013-08-05 20:36:57 +00002325 if (channel == -1) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002326 LOG(LS_WARNING) << "The specified ssrc " << ssrc << " is not in use.";
2327 return false;
2328 }
wu@webrtc.org9dba5252013-08-05 20:36:57 +00002329 if (engine()->voe()->volume()->SetInputMute(channel, muted) == -1) {
2330 LOG_RTCERR2(SetInputMute, channel, muted);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002331 return false;
2332 }
buildbot@webrtc.org6b21b712014-07-31 15:08:53 +00002333 // We set the AGC to mute state only when all the channels are muted.
2334 // This implementation is not ideal, instead we should signal the AGC when
2335 // the mic channel is muted/unmuted. We can't do it today because there
2336 // is no good way to know which stream is mapping to the mic channel.
2337 bool all_muted = muted;
solenbergc96df772015-10-21 13:01:53 -07002338 for (const auto& ch : send_streams_) {
Fredrik Solenbergaf9fb212015-08-26 10:45:53 +02002339 if (!all_muted) {
2340 break;
2341 }
2342 if (engine()->voe()->volume()->GetInputMute(ch.second->channel(),
buildbot@webrtc.org6b21b712014-07-31 15:08:53 +00002343 all_muted)) {
Fredrik Solenbergaf9fb212015-08-26 10:45:53 +02002344 LOG_RTCERR1(GetInputMute, ch.second->channel());
buildbot@webrtc.org6b21b712014-07-31 15:08:53 +00002345 return false;
2346 }
2347 }
2348
2349 webrtc::AudioProcessing* ap = engine()->voe()->base()->audio_processing();
solenberg0a617e22015-10-20 15:49:38 -07002350 if (ap) {
buildbot@webrtc.org6b21b712014-07-31 15:08:53 +00002351 ap->set_output_will_be_muted(all_muted);
solenberg0a617e22015-10-20 15:49:38 -07002352 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002353 return true;
2354}
2355
deadbeef80346142016-04-27 14:17:10 -07002356bool WebRtcVoiceMediaChannel::SetMaxSendBitrate(int bps) {
2357 LOG(LS_INFO) << "WebRtcVoiceMediaChannel::SetMaxSendBitrate.";
2358 max_send_bitrate_bps_ = bps;
skvlade0d46372016-04-07 22:59:22 -07002359
2360 for (const auto& kv : send_streams_) {
Taylor Brandstetterdb0cd9e2016-05-16 11:40:30 -07002361 if (!SetChannelSendParameters(kv.second->channel(),
2362 kv.second->rtp_parameters())) {
skvlade0d46372016-04-07 22:59:22 -07002363 return false;
2364 }
2365 }
2366 return true;
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00002367}
2368
Taylor Brandstetterdb0cd9e2016-05-16 11:40:30 -07002369bool WebRtcVoiceMediaChannel::SetChannelSendParameters(
skvlade0d46372016-04-07 22:59:22 -07002370 int channel,
2371 const webrtc::RtpParameters& parameters) {
2372 RTC_CHECK_EQ(1UL, parameters.encodings.size());
Taylor Brandstetter0cd086b2016-04-20 16:23:10 -07002373 // TODO(deadbeef): Handle setting parameters with a list of codecs in a
2374 // different order (which should change the send codec).
deadbeef80346142016-04-27 14:17:10 -07002375 return SetMaxSendBitrate(
2376 channel, MinPositive(max_send_bitrate_bps_,
2377 parameters.encodings[0].max_bitrate_bps));
skvlade0d46372016-04-07 22:59:22 -07002378}
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00002379
deadbeef80346142016-04-27 14:17:10 -07002380bool WebRtcVoiceMediaChannel::SetMaxSendBitrate(int channel, int bps) {
skvlade0d46372016-04-07 22:59:22 -07002381 // Bitrate is auto by default.
2382 // TODO(bemasc): Fix this so that if SetMaxSendBandwidth(50) is followed by
2383 // SetMaxSendBandwith(0), the second call removes the previous limit.
deadbeef80346142016-04-27 14:17:10 -07002384 if (bps <= 0) {
skvlade0d46372016-04-07 22:59:22 -07002385 return true;
deadbeef80346142016-04-27 14:17:10 -07002386 }
wu@webrtc.org1d1ffc92013-10-16 18:12:02 +00002387
solenberg72e29d22016-03-08 06:35:16 -08002388 if (!HasSendCodec()) {
wu@webrtc.org1d1ffc92013-10-16 18:12:02 +00002389 LOG(LS_INFO) << "The send codec has not been set up yet. "
minyue@webrtc.org26236952014-10-29 02:27:08 +00002390 << "The send bitrate setting will be applied later.";
wu@webrtc.org1d1ffc92013-10-16 18:12:02 +00002391 return true;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002392 }
2393
solenberg72e29d22016-03-08 06:35:16 -08002394 webrtc::CodecInst codec = send_codec_spec_.codec_inst;
solenberg26c8c912015-11-27 04:00:25 -08002395 bool is_multi_rate = WebRtcVoiceCodecs::IsCodecMultiRate(codec);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002396
2397 if (is_multi_rate) {
2398 // If codec is multi-rate then just set the bitrate.
deadbeef80346142016-04-27 14:17:10 -07002399 int max_bitrate_bps = WebRtcVoiceCodecs::MaxBitrateBps(codec);
2400 codec.rate = std::min(bps, max_bitrate_bps);
2401 LOG(LS_INFO) << "Setting codec " << codec.plname << " to bitrate " << bps
2402 << " bps.";
skvlade0d46372016-04-07 22:59:22 -07002403 if (!SetSendCodec(channel, codec)) {
deadbeef80346142016-04-27 14:17:10 -07002404 LOG(LS_ERROR) << "Failed to set codec " << codec.plname << " to bitrate "
2405 << bps << " bps.";
skvlade0d46372016-04-07 22:59:22 -07002406 return false;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002407 }
2408 return true;
2409 } else {
2410 // If codec is not multi-rate and |bps| is less than the fixed bitrate
2411 // then fail. If codec is not multi-rate and |bps| exceeds or equal the
2412 // fixed bitrate then ignore.
2413 if (bps < codec.rate) {
deadbeef80346142016-04-27 14:17:10 -07002414 LOG(LS_ERROR) << "Failed to set codec " << codec.plname << " to bitrate "
2415 << bps << " bps"
2416 << ", requires at least " << codec.rate << " bps.";
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002417 return false;
2418 }
2419 return true;
2420 }
2421}
2422
skvlad7a43d252016-03-22 15:32:27 -07002423void WebRtcVoiceMediaChannel::OnReadyToSend(bool ready) {
2424 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
2425 LOG(LS_VERBOSE) << "OnReadyToSend: " << (ready ? "Ready." : "Not ready.");
2426 call_->SignalChannelNetworkState(
2427 webrtc::MediaType::AUDIO,
2428 ready ? webrtc::kNetworkUp : webrtc::kNetworkDown);
2429}
2430
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002431bool WebRtcVoiceMediaChannel::GetStats(VoiceMediaInfo* info) {
Peter Boströmca8b4042016-03-08 14:24:13 -08002432 TRACE_EVENT0("webrtc", "WebRtcVoiceMediaChannel::GetStats");
solenberg566ef242015-11-06 15:34:49 -08002433 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
solenberg85a04962015-10-27 03:35:21 -07002434 RTC_DCHECK(info);
solenbergd97ec302015-10-07 01:40:33 -07002435
solenberg85a04962015-10-27 03:35:21 -07002436 // Get SSRC and stats for each sender.
2437 RTC_DCHECK(info->senders.size() == 0);
2438 for (const auto& stream : send_streams_) {
2439 webrtc::AudioSendStream::Stats stats = stream.second->GetStats();
wu@webrtc.org9dba5252013-08-05 20:36:57 +00002440 VoiceSenderInfo sinfo;
solenberg85a04962015-10-27 03:35:21 -07002441 sinfo.add_ssrc(stats.local_ssrc);
2442 sinfo.bytes_sent = stats.bytes_sent;
2443 sinfo.packets_sent = stats.packets_sent;
2444 sinfo.packets_lost = stats.packets_lost;
2445 sinfo.fraction_lost = stats.fraction_lost;
2446 sinfo.codec_name = stats.codec_name;
2447 sinfo.ext_seqnum = stats.ext_seqnum;
2448 sinfo.jitter_ms = stats.jitter_ms;
2449 sinfo.rtt_ms = stats.rtt_ms;
2450 sinfo.audio_level = stats.audio_level;
2451 sinfo.aec_quality_min = stats.aec_quality_min;
2452 sinfo.echo_delay_median_ms = stats.echo_delay_median_ms;
2453 sinfo.echo_delay_std_ms = stats.echo_delay_std_ms;
2454 sinfo.echo_return_loss = stats.echo_return_loss;
2455 sinfo.echo_return_loss_enhancement = stats.echo_return_loss_enhancement;
Taylor Brandstetter1a018dc2016-03-08 12:37:39 -08002456 sinfo.typing_noise_detected = (send_ ? stats.typing_noise_detected : false);
wu@webrtc.org9dba5252013-08-05 20:36:57 +00002457 info->senders.push_back(sinfo);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002458 }
2459
solenberg85a04962015-10-27 03:35:21 -07002460 // Get SSRC and stats for each receiver.
2461 RTC_DCHECK(info->receivers.size() == 0);
solenberg7add0582015-11-20 09:59:34 -08002462 for (const auto& stream : recv_streams_) {
Fredrik Solenberg4f4ec0a2015-10-22 10:49:27 +02002463 webrtc::AudioReceiveStream::Stats stats = stream.second->GetStats();
2464 VoiceReceiverInfo rinfo;
2465 rinfo.add_ssrc(stats.remote_ssrc);
2466 rinfo.bytes_rcvd = stats.bytes_rcvd;
2467 rinfo.packets_rcvd = stats.packets_rcvd;
2468 rinfo.packets_lost = stats.packets_lost;
2469 rinfo.fraction_lost = stats.fraction_lost;
2470 rinfo.codec_name = stats.codec_name;
2471 rinfo.ext_seqnum = stats.ext_seqnum;
2472 rinfo.jitter_ms = stats.jitter_ms;
2473 rinfo.jitter_buffer_ms = stats.jitter_buffer_ms;
2474 rinfo.jitter_buffer_preferred_ms = stats.jitter_buffer_preferred_ms;
2475 rinfo.delay_estimate_ms = stats.delay_estimate_ms;
2476 rinfo.audio_level = stats.audio_level;
2477 rinfo.expand_rate = stats.expand_rate;
2478 rinfo.speech_expand_rate = stats.speech_expand_rate;
2479 rinfo.secondary_decoded_rate = stats.secondary_decoded_rate;
2480 rinfo.accelerate_rate = stats.accelerate_rate;
2481 rinfo.preemptive_expand_rate = stats.preemptive_expand_rate;
2482 rinfo.decoding_calls_to_silence_generator =
2483 stats.decoding_calls_to_silence_generator;
2484 rinfo.decoding_calls_to_neteq = stats.decoding_calls_to_neteq;
2485 rinfo.decoding_normal = stats.decoding_normal;
2486 rinfo.decoding_plc = stats.decoding_plc;
2487 rinfo.decoding_cng = stats.decoding_cng;
2488 rinfo.decoding_plc_cng = stats.decoding_plc_cng;
2489 rinfo.capture_start_ntp_time_ms = stats.capture_start_ntp_time_ms;
2490 info->receivers.push_back(rinfo);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002491 }
2492
2493 return true;
2494}
2495
Tommif888bb52015-12-12 01:37:01 +01002496void WebRtcVoiceMediaChannel::SetRawAudioSink(
2497 uint32_t ssrc,
kwiberg686a8ef2016-02-26 03:00:35 -08002498 std::unique_ptr<webrtc::AudioSinkInterface> sink) {
Tommif888bb52015-12-12 01:37:01 +01002499 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
deadbeef884f5852016-01-15 09:20:04 -08002500 LOG(LS_VERBOSE) << "WebRtcVoiceMediaChannel::SetRawAudioSink: ssrc:" << ssrc
2501 << " " << (sink ? "(ptr)" : "NULL");
2502 if (ssrc == 0) {
2503 if (default_recv_ssrc_ != -1) {
kwiberg686a8ef2016-02-26 03:00:35 -08002504 std::unique_ptr<webrtc::AudioSinkInterface> proxy_sink(
deadbeef884f5852016-01-15 09:20:04 -08002505 sink ? new ProxySink(sink.get()) : nullptr);
2506 SetRawAudioSink(default_recv_ssrc_, std::move(proxy_sink));
2507 }
2508 default_sink_ = std::move(sink);
2509 return;
2510 }
Tommif888bb52015-12-12 01:37:01 +01002511 const auto it = recv_streams_.find(ssrc);
2512 if (it == recv_streams_.end()) {
2513 LOG(LS_WARNING) << "SetRawAudioSink: no recv stream" << ssrc;
2514 return;
2515 }
deadbeef2d110be2016-01-13 12:00:26 -08002516 it->second->SetRawAudioSink(std::move(sink));
Tommif888bb52015-12-12 01:37:01 +01002517}
2518
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002519int WebRtcVoiceMediaChannel::GetOutputLevel(int channel) {
solenbergd97ec302015-10-07 01:40:33 -07002520 unsigned int ulevel = 0;
2521 int ret = engine()->voe()->volume()->GetSpeechOutputLevel(channel, ulevel);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002522 return (ret == 0) ? static_cast<int>(ulevel) : -1;
2523}
2524
Peter Boström0c4e06b2015-10-07 12:23:21 +02002525int WebRtcVoiceMediaChannel::GetReceiveChannelId(uint32_t ssrc) const {
solenberg566ef242015-11-06 15:34:49 -08002526 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
solenberg7add0582015-11-20 09:59:34 -08002527 const auto it = recv_streams_.find(ssrc);
2528 if (it != recv_streams_.end()) {
mallinath@webrtc.org67ee6b92014-02-03 16:57:16 +00002529 return it->second->channel();
solenberg8fb30c32015-10-13 03:06:58 -07002530 }
solenberg1ac56142015-10-13 03:58:19 -07002531 return -1;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002532}
2533
Peter Boström0c4e06b2015-10-07 12:23:21 +02002534int WebRtcVoiceMediaChannel::GetSendChannelId(uint32_t ssrc) const {
solenberg566ef242015-11-06 15:34:49 -08002535 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
solenbergc96df772015-10-21 13:01:53 -07002536 const auto it = send_streams_.find(ssrc);
2537 if (it != send_streams_.end()) {
mallinath@webrtc.org67ee6b92014-02-03 16:57:16 +00002538 return it->second->channel();
solenberg8fb30c32015-10-13 03:06:58 -07002539 }
wu@webrtc.org9dba5252013-08-05 20:36:57 +00002540 return -1;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002541}
2542
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002543bool WebRtcVoiceMediaChannel::SetPlayout(int channel, bool playout) {
2544 if (playout) {
2545 LOG(LS_INFO) << "Starting playout for channel #" << channel;
2546 if (engine()->voe()->base()->StartPlayout(channel) == -1) {
2547 LOG_RTCERR1(StartPlayout, channel);
2548 return false;
2549 }
2550 } else {
2551 LOG(LS_INFO) << "Stopping playout for channel #" << channel;
2552 engine()->voe()->base()->StopPlayout(channel);
2553 }
2554 return true;
2555}
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002556} // namespace cricket
2557
2558#endif // HAVE_WEBRTC_VOICE