blob: b78d73ad0b0ffa10f4f03a1b6a3bd6813dce02ad [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,
solenberg72e29d22016-03-08 06:35:16 -0800387 webrtc::CodecInst* out,
stefanba4c0e42016-02-04 04:12:24 -0800388 int* red_payload_type) {
solenberg72e29d22016-03-08 06:35:16 -0800389 RTC_DCHECK(out);
stefanba4c0e42016-02-04 04:12:24 -0800390 RTC_DCHECK(red_payload_type);
391 // Select the preferred send codec (the first non-telephone-event/CN codec).
392 for (const AudioCodec& codec : codecs) {
393 *red_payload_type = -1;
394 if (IsCodec(codec, kDtmfCodecName) || IsCodec(codec, kCnCodecName)) {
395 // Skip telephone-event/CN codec, which will be handled later.
396 continue;
397 }
398
399 // We'll use the first codec in the list to actually send audio data.
400 // Be sure to use the payload type requested by the remote side.
401 // "red", for RED audio, is a special case where the actual codec to be
402 // used is specified in params.
403 const AudioCodec* found_codec = &codec;
404 if (IsCodec(*found_codec, kRedCodecName)) {
405 // Parse out the RED parameters. If we fail, just ignore RED;
406 // we don't support all possible params/usage scenarios.
407 *red_payload_type = codec.id;
408 found_codec = GetRedSendCodec(*found_codec, codecs);
409 if (!found_codec) {
410 continue;
411 }
412 }
413 // Ignore codecs we don't know about. The negotiation step should prevent
414 // this, but double-check to be sure.
solenberg72e29d22016-03-08 06:35:16 -0800415 webrtc::CodecInst voe_codec = {0};
416 if (!ToCodecInst(*found_codec, &voe_codec)) {
stefanba4c0e42016-02-04 04:12:24 -0800417 LOG(LS_WARNING) << "Unknown codec " << ToString(*found_codec);
418 continue;
419 }
solenberg72e29d22016-03-08 06:35:16 -0800420 *out = voe_codec;
stefanba4c0e42016-02-04 04:12:24 -0800421 return found_codec;
422 }
423 return nullptr;
424 }
425
solenberg26c8c912015-11-27 04:00:25 -0800426 private:
427 static const int kMaxNumPacketSize = 6;
428 struct CodecPref {
429 const char* name;
430 int clockrate;
Peter Kasting69558702016-01-12 16:26:35 -0800431 size_t channels;
solenberg26c8c912015-11-27 04:00:25 -0800432 int payload_type;
433 bool is_multi_rate;
434 int packet_sizes_ms[kMaxNumPacketSize];
deadbeef80346142016-04-27 14:17:10 -0700435 int max_bitrate_bps;
solenberg26c8c912015-11-27 04:00:25 -0800436 };
437 // Note: keep the supported packet sizes in ascending order.
438 static const CodecPref kCodecPrefs[12];
439
440 static int SelectPacketSize(const CodecPref& codec_pref, int ptime_ms) {
441 int selected_packet_size_ms = codec_pref.packet_sizes_ms[0];
442 for (int packet_size_ms : codec_pref.packet_sizes_ms) {
443 if (packet_size_ms && packet_size_ms <= ptime_ms) {
444 selected_packet_size_ms = packet_size_ms;
445 }
446 }
447 return selected_packet_size_ms;
448 }
449
450 // Changes RTP timestamp rate of G722. This is due to the "bug" in the RFC
451 // which says that G722 should be advertised as 8 kHz although it is a 16 kHz
452 // codec.
453 static void MaybeFixupG722(webrtc::CodecInst* voe_codec, int new_plfreq) {
454 if (IsCodec(*voe_codec, kG722CodecName)) {
455 // If the ASSERT triggers, the codec definition in WebRTC VoiceEngine
456 // has changed, and this special case is no longer needed.
457 RTC_DCHECK(voe_codec->plfreq != new_plfreq);
458 voe_codec->plfreq = new_plfreq;
459 }
460 }
stefanba4c0e42016-02-04 04:12:24 -0800461
462 static const AudioCodec* GetRedSendCodec(
463 const AudioCodec& red_codec,
464 const std::vector<AudioCodec>& all_codecs) {
465 // Get the RED encodings from the parameter with no name. This may
466 // change based on what is discussed on the Jingle list.
467 // The encoding parameter is of the form "a/b"; we only support where
468 // a == b. Verify this and parse out the value into red_pt.
469 // If the parameter value is absent (as it will be until we wire up the
470 // signaling of this message), use the second codec specified (i.e. the
471 // one after "red") as the encoding parameter.
472 int red_pt = -1;
473 std::string red_params;
474 CodecParameterMap::const_iterator it = red_codec.params.find("");
475 if (it != red_codec.params.end()) {
476 red_params = it->second;
477 std::vector<std::string> red_pts;
478 if (rtc::split(red_params, '/', &red_pts) != 2 ||
479 red_pts[0] != red_pts[1] || !rtc::FromString(red_pts[0], &red_pt)) {
480 LOG(LS_WARNING) << "RED params " << red_params << " not supported.";
481 return nullptr;
482 }
483 } else if (red_codec.params.empty()) {
484 LOG(LS_WARNING) << "RED params not present, using defaults";
485 if (all_codecs.size() > 1) {
486 red_pt = all_codecs[1].id;
487 }
488 }
489
490 // Try to find red_pt in |codecs|.
491 for (const AudioCodec& codec : all_codecs) {
492 if (codec.id == red_pt) {
493 return &codec;
494 }
495 }
496 LOG(LS_WARNING) << "RED params " << red_params << " are invalid.";
497 return nullptr;
498 }
solenberg26c8c912015-11-27 04:00:25 -0800499};
500
501const WebRtcVoiceCodecs::CodecPref WebRtcVoiceCodecs::kCodecPrefs[12] = {
deadbeef80346142016-04-27 14:17:10 -0700502 {kOpusCodecName, 48000, 2, 111, true, {10, 20, 40, 60}, kOpusMaxBitrate},
503 {kIsacCodecName, 16000, 1, 103, true, {30, 60}, kIsacMaxBitrate},
504 {kIsacCodecName, 32000, 1, 104, true, {30}, kIsacMaxBitrate},
505 // G722 should be advertised as 8000 Hz because of the RFC "bug".
506 {kG722CodecName, 8000, 1, 9, false, {10, 20, 30, 40, 50, 60}},
507 {kIlbcCodecName, 8000, 1, 102, false, {20, 30, 40, 60}},
508 {kPcmuCodecName, 8000, 1, 0, false, {10, 20, 30, 40, 50, 60}},
509 {kPcmaCodecName, 8000, 1, 8, false, {10, 20, 30, 40, 50, 60}},
510 {kCnCodecName, 32000, 1, 106, false, {}},
511 {kCnCodecName, 16000, 1, 105, false, {}},
512 {kCnCodecName, 8000, 1, 13, false, {}},
513 {kRedCodecName, 8000, 1, 127, false, {}},
514 {kDtmfCodecName, 8000, 1, 126, false, {}},
solenberg26c8c912015-11-27 04:00:25 -0800515};
516} // namespace {
517
518bool WebRtcVoiceEngine::ToCodecInst(const AudioCodec& in,
519 webrtc::CodecInst* out) {
520 return WebRtcVoiceCodecs::ToCodecInst(in, out);
521}
522
ossu29b1a8d2016-06-13 07:34:51 -0700523WebRtcVoiceEngine::WebRtcVoiceEngine(
524 webrtc::AudioDeviceModule* adm,
525 const rtc::scoped_refptr<webrtc::AudioDecoderFactory>& decoder_factory)
526 : WebRtcVoiceEngine(adm, decoder_factory, new VoEWrapper()) {
solenbergff976312016-03-30 23:28:51 -0700527 audio_state_ = webrtc::AudioState::Create(MakeAudioStateConfig(voe()));
solenberg26c8c912015-11-27 04:00:25 -0800528}
529
ossu29b1a8d2016-06-13 07:34:51 -0700530WebRtcVoiceEngine::WebRtcVoiceEngine(
531 webrtc::AudioDeviceModule* adm,
532 const rtc::scoped_refptr<webrtc::AudioDecoderFactory>& decoder_factory,
533 VoEWrapper* voe_wrapper)
534 : adm_(adm), decoder_factory_(decoder_factory), voe_wrapper_(voe_wrapper) {
solenberg26c8c912015-11-27 04:00:25 -0800535 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
solenbergff976312016-03-30 23:28:51 -0700536 LOG(LS_INFO) << "WebRtcVoiceEngine::WebRtcVoiceEngine";
537 RTC_DCHECK(voe_wrapper);
solenberg26c8c912015-11-27 04:00:25 -0800538
539 signal_thread_checker_.DetachFromThread();
solenberg26c8c912015-11-27 04:00:25 -0800540
541 // Load our audio codec list.
solenbergff976312016-03-30 23:28:51 -0700542 LOG(LS_INFO) << "Supported codecs in order of preference:";
solenberg26c8c912015-11-27 04:00:25 -0800543 codecs_ = WebRtcVoiceCodecs::SupportedCodecs();
solenbergff976312016-03-30 23:28:51 -0700544 for (const AudioCodec& codec : codecs_) {
545 LOG(LS_INFO) << ToString(codec);
buildbot@webrtc.org13d67762014-05-02 17:33:29 +0000546 }
buildbot@webrtc.org13d67762014-05-02 17:33:29 +0000547
solenbergff976312016-03-30 23:28:51 -0700548 voe_config_.Set<webrtc::VoicePacing>(new webrtc::VoicePacing(true));
buildbot@webrtc.org13d67762014-05-02 17:33:29 +0000549
solenbergff976312016-03-30 23:28:51 -0700550 // Temporarily turn logging level up for the Init() call.
551 webrtc::Trace::SetTraceCallback(this);
solenbergbd138382015-11-20 16:08:07 -0800552 webrtc::Trace::set_level_filter(kElevatedTraceFilter);
solenberg2515af22015-12-02 06:19:36 -0800553 LOG(LS_INFO) << webrtc::VoiceEngine::GetVersionString();
ossu29b1a8d2016-06-13 07:34:51 -0700554 RTC_CHECK_EQ(0, voe_wrapper_->base()->Init(adm_.get(), nullptr,
555 decoder_factory_));
solenbergbd138382015-11-20 16:08:07 -0800556 webrtc::Trace::set_level_filter(kDefaultTraceFilter);
buildbot@webrtc.org13d67762014-05-02 17:33:29 +0000557
solenbergff976312016-03-30 23:28:51 -0700558 // No ADM supplied? Get the default one from VoE.
559 if (!adm_) {
560 adm_ = voe_wrapper_->base()->audio_device_module();
561 }
562 RTC_DCHECK(adm_);
563
buildbot@webrtc.org13d67762014-05-02 17:33:29 +0000564 // Save the default AGC configuration settings. This must happen before
solenberg246b8172015-12-08 09:50:23 -0800565 // calling ApplyOptions or the default will be overwritten.
solenbergff976312016-03-30 23:28:51 -0700566 int error = voe_wrapper_->processing()->GetAgcConfig(default_agc_config_);
567 RTC_DCHECK_EQ(0, error);
buildbot@webrtc.org13d67762014-05-02 17:33:29 +0000568
solenberg0f7d2932016-01-15 01:40:39 -0800569 // Set default engine options.
570 {
571 AudioOptions options;
572 options.echo_cancellation = rtc::Optional<bool>(true);
573 options.auto_gain_control = rtc::Optional<bool>(true);
574 options.noise_suppression = rtc::Optional<bool>(true);
575 options.highpass_filter = rtc::Optional<bool>(true);
576 options.stereo_swapping = rtc::Optional<bool>(false);
577 options.audio_jitter_buffer_max_packets = rtc::Optional<int>(50);
578 options.audio_jitter_buffer_fast_accelerate = rtc::Optional<bool>(false);
579 options.typing_detection = rtc::Optional<bool>(true);
580 options.adjust_agc_delta = rtc::Optional<int>(0);
581 options.experimental_agc = rtc::Optional<bool>(false);
582 options.extended_filter_aec = rtc::Optional<bool>(false);
583 options.delay_agnostic_aec = rtc::Optional<bool>(false);
584 options.experimental_ns = rtc::Optional<bool>(false);
Alejandro Luebsc9b0c262016-05-16 15:32:38 -0700585 options.intelligibility_enhancer = rtc::Optional<bool>(false);
solenbergff976312016-03-30 23:28:51 -0700586 bool error = ApplyOptions(options);
587 RTC_DCHECK(error);
buildbot@webrtc.org13d67762014-05-02 17:33:29 +0000588 }
589
solenberg246b8172015-12-08 09:50:23 -0800590 SetDefaultDevices();
buildbot@webrtc.org13d67762014-05-02 17:33:29 +0000591}
592
solenbergff976312016-03-30 23:28:51 -0700593WebRtcVoiceEngine::~WebRtcVoiceEngine() {
solenberg566ef242015-11-06 15:34:49 -0800594 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
solenbergff976312016-03-30 23:28:51 -0700595 LOG(LS_INFO) << "WebRtcVoiceEngine::~WebRtcVoiceEngine";
buildbot@webrtc.org13d67762014-05-02 17:33:29 +0000596 StopAecDump();
buildbot@webrtc.org13d67762014-05-02 17:33:29 +0000597 voe_wrapper_->base()->Terminate();
solenbergff976312016-03-30 23:28:51 -0700598 webrtc::Trace::SetTraceCallback(nullptr);
buildbot@webrtc.org13d67762014-05-02 17:33:29 +0000599}
600
solenberg566ef242015-11-06 15:34:49 -0800601rtc::scoped_refptr<webrtc::AudioState>
602 WebRtcVoiceEngine::GetAudioState() const {
603 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
604 return audio_state_;
605}
606
nisse51542be2016-02-12 02:27:06 -0800607VoiceMediaChannel* WebRtcVoiceEngine::CreateChannel(
608 webrtc::Call* call,
609 const MediaConfig& config,
Jelena Marusicc28a8962015-05-29 15:05:44 +0200610 const AudioOptions& options) {
solenberg566ef242015-11-06 15:34:49 -0800611 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
nisse51542be2016-02-12 02:27:06 -0800612 return new WebRtcVoiceMediaChannel(this, config, options, call);
buildbot@webrtc.org13d67762014-05-02 17:33:29 +0000613}
614
buildbot@webrtc.org13d67762014-05-02 17:33:29 +0000615bool WebRtcVoiceEngine::ApplyOptions(const AudioOptions& options_in) {
solenberg566ef242015-11-06 15:34:49 -0800616 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
solenbergff976312016-03-30 23:28:51 -0700617 LOG(LS_INFO) << "WebRtcVoiceEngine::ApplyOptions: " << options_in.ToString();
solenberg0f7d2932016-01-15 01:40:39 -0800618 AudioOptions options = options_in; // The options are modified below.
solenberg246b8172015-12-08 09:50:23 -0800619
buildbot@webrtc.org13d67762014-05-02 17:33:29 +0000620 // kEcConference is AEC with high suppression.
621 webrtc::EcModes ec_mode = webrtc::kEcConference;
622 webrtc::AecmModes aecm_mode = webrtc::kAecmSpeakerphone;
623 webrtc::AgcModes agc_mode = webrtc::kAgcAdaptiveAnalog;
624 webrtc::NsModes ns_mode = webrtc::kNsHighSuppression;
kwiberg102c6a62015-10-30 02:47:38 -0700625 if (options.aecm_generate_comfort_noise) {
buildbot@webrtc.org13d67762014-05-02 17:33:29 +0000626 LOG(LS_VERBOSE) << "Comfort noise explicitly set to "
kwiberg102c6a62015-10-30 02:47:38 -0700627 << *options.aecm_generate_comfort_noise
628 << " (default is false).";
buildbot@webrtc.org13d67762014-05-02 17:33:29 +0000629 }
630
kjellanderfcfc8042016-01-14 11:01:09 -0800631#if defined(WEBRTC_IOS)
buildbot@webrtc.org13d67762014-05-02 17:33:29 +0000632 // On iOS, VPIO provides built-in EC and AGC.
Karl Wibergbe579832015-11-10 22:34:18 +0100633 options.echo_cancellation = rtc::Optional<bool>(false);
634 options.auto_gain_control = rtc::Optional<bool>(false);
henrika86d907c2015-09-07 16:09:50 +0200635 LOG(LS_INFO) << "Always disable AEC and AGC on iOS. Use built-in instead.";
buildbot@webrtc.org13d67762014-05-02 17:33:29 +0000636#elif defined(ANDROID)
637 ec_mode = webrtc::kEcAecm;
638#endif
639
kjellanderfcfc8042016-01-14 11:01:09 -0800640#if defined(WEBRTC_IOS) || defined(ANDROID)
buildbot@webrtc.org13d67762014-05-02 17:33:29 +0000641 // Set the AGC mode for iOS as well despite disabling it above, to avoid
642 // unsupported configuration errors from webrtc.
643 agc_mode = webrtc::kAgcFixedDigital;
Karl Wibergbe579832015-11-10 22:34:18 +0100644 options.typing_detection = rtc::Optional<bool>(false);
645 options.experimental_agc = rtc::Optional<bool>(false);
646 options.extended_filter_aec = rtc::Optional<bool>(false);
647 options.experimental_ns = rtc::Optional<bool>(false);
buildbot@webrtc.org13d67762014-05-02 17:33:29 +0000648#endif
649
Bjorn Volckerbf395c12015-03-25 22:45:56 +0100650 // Delay Agnostic AEC automatically turns on EC if not set except on iOS
651 // where the feature is not supported.
652 bool use_delay_agnostic_aec = false;
kjellanderfcfc8042016-01-14 11:01:09 -0800653#if !defined(WEBRTC_IOS)
kwiberg102c6a62015-10-30 02:47:38 -0700654 if (options.delay_agnostic_aec) {
655 use_delay_agnostic_aec = *options.delay_agnostic_aec;
Bjorn Volckerbf395c12015-03-25 22:45:56 +0100656 if (use_delay_agnostic_aec) {
Karl Wibergbe579832015-11-10 22:34:18 +0100657 options.echo_cancellation = rtc::Optional<bool>(true);
658 options.extended_filter_aec = rtc::Optional<bool>(true);
Bjorn Volckerbf395c12015-03-25 22:45:56 +0100659 ec_mode = webrtc::kEcConference;
660 }
661 }
662#endif
663
buildbot@webrtc.org13d67762014-05-02 17:33:29 +0000664 webrtc::VoEAudioProcessing* voep = voe_wrapper_->processing();
665
kwiberg102c6a62015-10-30 02:47:38 -0700666 if (options.echo_cancellation) {
henrika@webrtc.orga954c072014-12-09 16:22:09 +0000667 // Check if platform supports built-in EC. Currently only supported on
668 // Android and in combination with Java based audio layer.
669 // TODO(henrika): investigate possibility to support built-in EC also
670 // in combination with Open SL ES audio.
solenberg5b5129a2016-04-08 05:35:48 -0700671 const bool built_in_aec = adm()->BuiltInAECIsAvailable();
Bjorn Volcker73f72102015-06-03 14:50:15 +0200672 if (built_in_aec) {
Bjorn Volckerccfc9392015-05-07 07:43:17 +0200673 // Built-in EC exists on this device and use_delay_agnostic_aec is not
674 // overriding it. Enable/Disable it according to the echo_cancellation
675 // audio option.
Bjorn Volcker73f72102015-06-03 14:50:15 +0200676 const bool enable_built_in_aec =
kwiberg102c6a62015-10-30 02:47:38 -0700677 *options.echo_cancellation && !use_delay_agnostic_aec;
solenberg5b5129a2016-04-08 05:35:48 -0700678 if (adm()->EnableBuiltInAEC(enable_built_in_aec) == 0 &&
Bjorn Volcker73f72102015-06-03 14:50:15 +0200679 enable_built_in_aec) {
Bjorn Volckerbf395c12015-03-25 22:45:56 +0100680 // Disable internal software EC if built-in EC is enabled,
henrika@webrtc.orga954c072014-12-09 16:22:09 +0000681 // i.e., replace the software EC with the built-in EC.
Karl Wibergbe579832015-11-10 22:34:18 +0100682 options.echo_cancellation = rtc::Optional<bool>(false);
henrika@webrtc.orga954c072014-12-09 16:22:09 +0000683 LOG(LS_INFO) << "Disabling EC since built-in EC will be used instead";
684 }
685 }
kwiberg102c6a62015-10-30 02:47:38 -0700686 if (voep->SetEcStatus(*options.echo_cancellation, ec_mode) == -1) {
687 LOG_RTCERR2(SetEcStatus, *options.echo_cancellation, ec_mode);
buildbot@webrtc.org13d67762014-05-02 17:33:29 +0000688 return false;
689 } else {
kwiberg102c6a62015-10-30 02:47:38 -0700690 LOG(LS_INFO) << "Echo control set to " << *options.echo_cancellation
henrika86d907c2015-09-07 16:09:50 +0200691 << " with mode " << ec_mode;
buildbot@webrtc.org13d67762014-05-02 17:33:29 +0000692 }
693#if !defined(ANDROID)
694 // TODO(ajm): Remove the error return on Android from webrtc.
kwiberg102c6a62015-10-30 02:47:38 -0700695 if (voep->SetEcMetricsStatus(*options.echo_cancellation) == -1) {
696 LOG_RTCERR1(SetEcMetricsStatus, *options.echo_cancellation);
buildbot@webrtc.org13d67762014-05-02 17:33:29 +0000697 return false;
698 }
699#endif
700 if (ec_mode == webrtc::kEcAecm) {
kwiberg102c6a62015-10-30 02:47:38 -0700701 bool cn = options.aecm_generate_comfort_noise.value_or(false);
702 if (voep->SetAecmMode(aecm_mode, cn) != 0) {
703 LOG_RTCERR2(SetAecmMode, aecm_mode, cn);
buildbot@webrtc.org13d67762014-05-02 17:33:29 +0000704 return false;
705 }
706 }
707 }
708
kwiberg102c6a62015-10-30 02:47:38 -0700709 if (options.auto_gain_control) {
solenberg5b5129a2016-04-08 05:35:48 -0700710 const bool built_in_agc = adm()->BuiltInAGCIsAvailable();
henrikac14f5ff2015-09-23 14:08:33 +0200711 if (built_in_agc) {
solenberg5b5129a2016-04-08 05:35:48 -0700712 if (adm()->EnableBuiltInAGC(*options.auto_gain_control) == 0 &&
kwiberg102c6a62015-10-30 02:47:38 -0700713 *options.auto_gain_control) {
henrikac14f5ff2015-09-23 14:08:33 +0200714 // Disable internal software AGC if built-in AGC is enabled,
715 // i.e., replace the software AGC with the built-in AGC.
Karl Wibergbe579832015-11-10 22:34:18 +0100716 options.auto_gain_control = rtc::Optional<bool>(false);
henrikac14f5ff2015-09-23 14:08:33 +0200717 LOG(LS_INFO) << "Disabling AGC since built-in AGC will be used instead";
718 }
719 }
kwiberg102c6a62015-10-30 02:47:38 -0700720 if (voep->SetAgcStatus(*options.auto_gain_control, agc_mode) == -1) {
721 LOG_RTCERR2(SetAgcStatus, *options.auto_gain_control, agc_mode);
buildbot@webrtc.org13d67762014-05-02 17:33:29 +0000722 return false;
723 } else {
kwiberg102c6a62015-10-30 02:47:38 -0700724 LOG(LS_INFO) << "Auto gain set to " << *options.auto_gain_control
725 << " with mode " << agc_mode;
buildbot@webrtc.org13d67762014-05-02 17:33:29 +0000726 }
727 }
728
kwiberg102c6a62015-10-30 02:47:38 -0700729 if (options.tx_agc_target_dbov || options.tx_agc_digital_compression_gain ||
730 options.tx_agc_limiter) {
buildbot@webrtc.org13d67762014-05-02 17:33:29 +0000731 // Override default_agc_config_. Generally, an unset option means "leave
732 // the VoE bits alone" in this function, so we want whatever is set to be
733 // stored as the new "default". If we didn't, then setting e.g.
734 // tx_agc_target_dbov would reset digital compression gain and limiter
735 // settings.
736 // Also, if we don't update default_agc_config_, then adjust_agc_delta
737 // would be an offset from the original values, and not whatever was set
738 // explicitly.
kwiberg102c6a62015-10-30 02:47:38 -0700739 default_agc_config_.targetLeveldBOv = options.tx_agc_target_dbov.value_or(
740 default_agc_config_.targetLeveldBOv);
buildbot@webrtc.org13d67762014-05-02 17:33:29 +0000741 default_agc_config_.digitalCompressionGaindB =
kwiberg102c6a62015-10-30 02:47:38 -0700742 options.tx_agc_digital_compression_gain.value_or(
buildbot@webrtc.org13d67762014-05-02 17:33:29 +0000743 default_agc_config_.digitalCompressionGaindB);
744 default_agc_config_.limiterEnable =
kwiberg102c6a62015-10-30 02:47:38 -0700745 options.tx_agc_limiter.value_or(default_agc_config_.limiterEnable);
buildbot@webrtc.org13d67762014-05-02 17:33:29 +0000746 if (voe_wrapper_->processing()->SetAgcConfig(default_agc_config_) == -1) {
747 LOG_RTCERR3(SetAgcConfig,
748 default_agc_config_.targetLeveldBOv,
749 default_agc_config_.digitalCompressionGaindB,
750 default_agc_config_.limiterEnable);
751 return false;
752 }
753 }
754
Alejandro Luebsc9b0c262016-05-16 15:32:38 -0700755 if (options.intelligibility_enhancer) {
756 intelligibility_enhancer_ = options.intelligibility_enhancer;
757 }
758 if (intelligibility_enhancer_ && *intelligibility_enhancer_) {
759 LOG(LS_INFO) << "Enabling NS when Intelligibility Enhancer is active.";
760 options.noise_suppression = intelligibility_enhancer_;
761 }
762
kwiberg102c6a62015-10-30 02:47:38 -0700763 if (options.noise_suppression) {
Alejandro Luebsc9b0c262016-05-16 15:32:38 -0700764 if (adm()->BuiltInNSIsAvailable()) {
765 bool builtin_ns =
766 *options.noise_suppression &&
767 !(intelligibility_enhancer_ && *intelligibility_enhancer_);
768 if (adm()->EnableBuiltInNS(builtin_ns) == 0 && builtin_ns) {
henrikac14f5ff2015-09-23 14:08:33 +0200769 // Disable internal software NS if built-in NS is enabled,
770 // i.e., replace the software NS with the built-in NS.
Karl Wibergbe579832015-11-10 22:34:18 +0100771 options.noise_suppression = rtc::Optional<bool>(false);
henrikac14f5ff2015-09-23 14:08:33 +0200772 LOG(LS_INFO) << "Disabling NS since built-in NS will be used instead";
773 }
774 }
kwiberg102c6a62015-10-30 02:47:38 -0700775 if (voep->SetNsStatus(*options.noise_suppression, ns_mode) == -1) {
776 LOG_RTCERR2(SetNsStatus, *options.noise_suppression, ns_mode);
buildbot@webrtc.org13d67762014-05-02 17:33:29 +0000777 return false;
778 } else {
kwiberg102c6a62015-10-30 02:47:38 -0700779 LOG(LS_INFO) << "Noise suppression set to " << *options.noise_suppression
henrikac14f5ff2015-09-23 14:08:33 +0200780 << " with mode " << ns_mode;
buildbot@webrtc.org13d67762014-05-02 17:33:29 +0000781 }
782 }
783
kwiberg102c6a62015-10-30 02:47:38 -0700784 if (options.highpass_filter) {
785 LOG(LS_INFO) << "High pass filter enabled? " << *options.highpass_filter;
786 if (voep->EnableHighPassFilter(*options.highpass_filter) == -1) {
787 LOG_RTCERR1(SetHighpassFilterStatus, *options.highpass_filter);
buildbot@webrtc.org13d67762014-05-02 17:33:29 +0000788 return false;
789 }
790 }
791
kwiberg102c6a62015-10-30 02:47:38 -0700792 if (options.stereo_swapping) {
793 LOG(LS_INFO) << "Stereo swapping enabled? " << *options.stereo_swapping;
794 voep->EnableStereoChannelSwapping(*options.stereo_swapping);
795 if (voep->IsStereoChannelSwappingEnabled() != *options.stereo_swapping) {
796 LOG_RTCERR1(EnableStereoChannelSwapping, *options.stereo_swapping);
buildbot@webrtc.org13d67762014-05-02 17:33:29 +0000797 return false;
798 }
799 }
800
kwiberg102c6a62015-10-30 02:47:38 -0700801 if (options.audio_jitter_buffer_max_packets) {
802 LOG(LS_INFO) << "NetEq capacity is "
803 << *options.audio_jitter_buffer_max_packets;
Henrik Lundin64dad832015-05-11 12:44:23 +0200804 voe_config_.Set<webrtc::NetEqCapacityConfig>(
kwiberg102c6a62015-10-30 02:47:38 -0700805 new webrtc::NetEqCapacityConfig(
806 *options.audio_jitter_buffer_max_packets));
Henrik Lundin64dad832015-05-11 12:44:23 +0200807 }
808
kwiberg102c6a62015-10-30 02:47:38 -0700809 if (options.audio_jitter_buffer_fast_accelerate) {
810 LOG(LS_INFO) << "NetEq fast mode? "
811 << *options.audio_jitter_buffer_fast_accelerate;
Henrik Lundin5263b3c2015-06-01 10:29:41 +0200812 voe_config_.Set<webrtc::NetEqFastAccelerate>(
kwiberg102c6a62015-10-30 02:47:38 -0700813 new webrtc::NetEqFastAccelerate(
814 *options.audio_jitter_buffer_fast_accelerate));
Henrik Lundin5263b3c2015-06-01 10:29:41 +0200815 }
816
kwiberg102c6a62015-10-30 02:47:38 -0700817 if (options.typing_detection) {
818 LOG(LS_INFO) << "Typing detection is enabled? "
819 << *options.typing_detection;
820 if (voep->SetTypingDetectionStatus(*options.typing_detection) == -1) {
buildbot@webrtc.org13d67762014-05-02 17:33:29 +0000821 // In case of error, log the info and continue
kwiberg102c6a62015-10-30 02:47:38 -0700822 LOG_RTCERR1(SetTypingDetectionStatus, *options.typing_detection);
buildbot@webrtc.org13d67762014-05-02 17:33:29 +0000823 }
824 }
825
kwiberg102c6a62015-10-30 02:47:38 -0700826 if (options.adjust_agc_delta) {
827 LOG(LS_INFO) << "Adjust agc delta is " << *options.adjust_agc_delta;
828 if (!AdjustAgcLevel(*options.adjust_agc_delta)) {
buildbot@webrtc.org13d67762014-05-02 17:33:29 +0000829 return false;
830 }
831 }
832
buildbot@webrtc.org1f8a2372014-08-28 10:52:44 +0000833 webrtc::Config config;
834
kwiberg102c6a62015-10-30 02:47:38 -0700835 if (options.delay_agnostic_aec)
836 delay_agnostic_aec_ = options.delay_agnostic_aec;
837 if (delay_agnostic_aec_) {
838 LOG(LS_INFO) << "Delay agnostic aec is enabled? " << *delay_agnostic_aec_;
henrik.lundin0f133b92015-07-02 00:17:55 -0700839 config.Set<webrtc::DelayAgnostic>(
kwiberg102c6a62015-10-30 02:47:38 -0700840 new webrtc::DelayAgnostic(*delay_agnostic_aec_));
Bjorn Volckerbf395c12015-03-25 22:45:56 +0100841 }
842
kwiberg102c6a62015-10-30 02:47:38 -0700843 if (options.extended_filter_aec) {
844 extended_filter_aec_ = options.extended_filter_aec;
845 }
846 if (extended_filter_aec_) {
847 LOG(LS_INFO) << "Extended filter aec is enabled? " << *extended_filter_aec_;
Henrik Lundin441f6342015-06-09 16:03:13 +0200848 config.Set<webrtc::ExtendedFilter>(
kwiberg102c6a62015-10-30 02:47:38 -0700849 new webrtc::ExtendedFilter(*extended_filter_aec_));
buildbot@webrtc.org1f8a2372014-08-28 10:52:44 +0000850 }
851
kwiberg102c6a62015-10-30 02:47:38 -0700852 if (options.experimental_ns) {
853 experimental_ns_ = options.experimental_ns;
854 }
855 if (experimental_ns_) {
856 LOG(LS_INFO) << "Experimental ns is enabled? " << *experimental_ns_;
buildbot@webrtc.org1f8a2372014-08-28 10:52:44 +0000857 config.Set<webrtc::ExperimentalNs>(
kwiberg102c6a62015-10-30 02:47:38 -0700858 new webrtc::ExperimentalNs(*experimental_ns_));
buildbot@webrtc.org1f8a2372014-08-28 10:52:44 +0000859 }
buildbot@webrtc.org1f8a2372014-08-28 10:52:44 +0000860
Alejandro Luebsc9b0c262016-05-16 15:32:38 -0700861 if (intelligibility_enhancer_) {
862 LOG(LS_INFO) << "Intelligibility Enhancer is enabled? "
863 << *intelligibility_enhancer_;
864 config.Set<webrtc::Intelligibility>(
865 new webrtc::Intelligibility(*intelligibility_enhancer_));
866 }
867
buildbot@webrtc.org1f8a2372014-08-28 10:52:44 +0000868 // We check audioproc for the benefit of tests, since FakeWebRtcVoiceEngine
869 // returns NULL on audio_processing().
870 webrtc::AudioProcessing* audioproc = voe_wrapper_->base()->audio_processing();
871 if (audioproc) {
872 audioproc->SetExtraOptions(config);
873 }
874
kwiberg102c6a62015-10-30 02:47:38 -0700875 if (options.recording_sample_rate) {
876 LOG(LS_INFO) << "Recording sample rate is "
877 << *options.recording_sample_rate;
solenberg5b5129a2016-04-08 05:35:48 -0700878 if (adm()->SetRecordingSampleRate(*options.recording_sample_rate)) {
kwiberg102c6a62015-10-30 02:47:38 -0700879 LOG_RTCERR1(SetRecordingSampleRate, *options.recording_sample_rate);
buildbot@webrtc.org13d67762014-05-02 17:33:29 +0000880 }
881 }
882
kwiberg102c6a62015-10-30 02:47:38 -0700883 if (options.playout_sample_rate) {
884 LOG(LS_INFO) << "Playout sample rate is " << *options.playout_sample_rate;
solenberg5b5129a2016-04-08 05:35:48 -0700885 if (adm()->SetPlayoutSampleRate(*options.playout_sample_rate)) {
kwiberg102c6a62015-10-30 02:47:38 -0700886 LOG_RTCERR1(SetPlayoutSampleRate, *options.playout_sample_rate);
buildbot@webrtc.org13d67762014-05-02 17:33:29 +0000887 }
888 }
889
890 return true;
891}
892
solenberg246b8172015-12-08 09:50:23 -0800893void WebRtcVoiceEngine::SetDefaultDevices() {
solenberg566ef242015-11-06 15:34:49 -0800894 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
kjellanderfcfc8042016-01-14 11:01:09 -0800895#if !defined(WEBRTC_IOS)
solenberg246b8172015-12-08 09:50:23 -0800896 int in_id = kDefaultAudioDeviceId;
897 int out_id = kDefaultAudioDeviceId;
898 LOG(LS_INFO) << "Setting microphone to (id=" << in_id
899 << ") and speaker to (id=" << out_id << ")";
buildbot@webrtc.org13d67762014-05-02 17:33:29 +0000900
solenbergc1a1b352015-09-22 13:31:20 -0700901 bool ret = true;
solenberg246b8172015-12-08 09:50:23 -0800902 if (voe_wrapper_->hw()->SetRecordingDevice(in_id) == -1) {
903 LOG_RTCERR1(SetRecordingDevice, in_id);
buildbot@webrtc.org13d67762014-05-02 17:33:29 +0000904 ret = false;
905 }
solenberg246b8172015-12-08 09:50:23 -0800906 webrtc::AudioProcessing* ap = voe()->base()->audio_processing();
907 if (ap) {
908 ap->Initialize();
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000909 }
910
solenberg246b8172015-12-08 09:50:23 -0800911 if (voe_wrapper_->hw()->SetPlayoutDevice(out_id) == -1) {
912 LOG_RTCERR1(SetPlayoutDevice, out_id);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000913 ret = false;
914 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000915
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000916 if (ret) {
solenberg246b8172015-12-08 09:50:23 -0800917 LOG(LS_INFO) << "Set microphone to (id=" << in_id
918 << ") and speaker to (id=" << out_id << ")";
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000919 }
kjellanderfcfc8042016-01-14 11:01:09 -0800920#endif // !WEBRTC_IOS
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000921}
922
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000923bool WebRtcVoiceEngine::GetOutputVolume(int* level) {
solenberg566ef242015-11-06 15:34:49 -0800924 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000925 unsigned int ulevel;
926 if (voe_wrapper_->volume()->GetSpeakerVolume(ulevel) == -1) {
927 LOG_RTCERR1(GetSpeakerVolume, level);
928 return false;
929 }
930 *level = ulevel;
931 return true;
932}
933
934bool WebRtcVoiceEngine::SetOutputVolume(int level) {
solenberg566ef242015-11-06 15:34:49 -0800935 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
henrikg91d6ede2015-09-17 00:24:34 -0700936 RTC_DCHECK(level >= 0 && level <= 255);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000937 if (voe_wrapper_->volume()->SetSpeakerVolume(level) == -1) {
938 LOG_RTCERR1(SetSpeakerVolume, level);
939 return false;
940 }
941 return true;
942}
943
944int WebRtcVoiceEngine::GetInputLevel() {
solenberg566ef242015-11-06 15:34:49 -0800945 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000946 unsigned int ulevel;
947 return (voe_wrapper_->volume()->GetSpeechInputLevel(ulevel) != -1) ?
948 static_cast<int>(ulevel) : -1;
949}
950
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000951const std::vector<AudioCodec>& WebRtcVoiceEngine::codecs() {
solenberg566ef242015-11-06 15:34:49 -0800952 RTC_DCHECK(signal_thread_checker_.CalledOnValidThread());
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000953 return codecs_;
954}
955
Stefan Holmer9d69c3f2015-12-07 10:45:43 +0100956RtpCapabilities WebRtcVoiceEngine::GetCapabilities() const {
solenberg566ef242015-11-06 15:34:49 -0800957 RTC_DCHECK(signal_thread_checker_.CalledOnValidThread());
Stefan Holmer9d69c3f2015-12-07 10:45:43 +0100958 RtpCapabilities capabilities;
Stefan Holmer9d69c3f2015-12-07 10:45:43 +0100959 capabilities.header_extensions.push_back(
isheriff6f8d6862016-05-26 11:24:55 -0700960 webrtc::RtpExtension(webrtc::RtpExtension::kAudioLevelUri,
961 webrtc::RtpExtension::kAudioLevelDefaultId));
962 capabilities.header_extensions.push_back(
963 webrtc::RtpExtension(webrtc::RtpExtension::kAbsSendTimeUri,
964 webrtc::RtpExtension::kAbsSendTimeDefaultId));
stefanba4c0e42016-02-04 04:12:24 -0800965 if (webrtc::field_trial::FindFullName("WebRTC-Audio-SendSideBwe") ==
966 "Enabled") {
isheriff6f8d6862016-05-26 11:24:55 -0700967 capabilities.header_extensions.push_back(webrtc::RtpExtension(
968 webrtc::RtpExtension::kTransportSequenceNumberUri,
969 webrtc::RtpExtension::kTransportSequenceNumberDefaultId));
stefanba4c0e42016-02-04 04:12:24 -0800970 }
Stefan Holmer9d69c3f2015-12-07 10:45:43 +0100971 return capabilities;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000972}
973
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000974int WebRtcVoiceEngine::GetLastEngineError() {
solenberg566ef242015-11-06 15:34:49 -0800975 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000976 return voe_wrapper_->error();
977}
978
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000979void WebRtcVoiceEngine::Print(webrtc::TraceLevel level, const char* trace,
980 int length) {
solenberg566ef242015-11-06 15:34:49 -0800981 // Note: This callback can happen on any thread!
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000982 rtc::LoggingSeverity sev = rtc::LS_VERBOSE;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000983 if (level == webrtc::kTraceError || level == webrtc::kTraceCritical)
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000984 sev = rtc::LS_ERROR;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000985 else if (level == webrtc::kTraceWarning)
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000986 sev = rtc::LS_WARNING;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000987 else if (level == webrtc::kTraceStateInfo || level == webrtc::kTraceInfo)
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000988 sev = rtc::LS_INFO;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000989 else if (level == webrtc::kTraceTerseInfo)
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000990 sev = rtc::LS_INFO;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000991
solenberg72e29d22016-03-08 06:35:16 -0800992 // Skip past boilerplate prefix text.
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000993 if (length < 72) {
994 std::string msg(trace, length);
995 LOG(LS_ERROR) << "Malformed webrtc log message: ";
996 LOG_V(sev) << msg;
997 } else {
998 std::string msg(trace + 71, length - 72);
Peter Boströmd5c75b12015-09-23 13:24:32 +0200999 LOG_V(sev) << "webrtc: " << msg;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001000 }
1001}
1002
solenberg63b34542015-09-29 06:06:31 -07001003void WebRtcVoiceEngine::RegisterChannel(WebRtcVoiceMediaChannel* channel) {
solenberg566ef242015-11-06 15:34:49 -08001004 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
1005 RTC_DCHECK(channel);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001006 channels_.push_back(channel);
1007}
1008
solenberg63b34542015-09-29 06:06:31 -07001009void WebRtcVoiceEngine::UnregisterChannel(WebRtcVoiceMediaChannel* channel) {
solenberg566ef242015-11-06 15:34:49 -08001010 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
solenberg63b34542015-09-29 06:06:31 -07001011 auto it = std::find(channels_.begin(), channels_.end(), channel);
solenberg566ef242015-11-06 15:34:49 -08001012 RTC_DCHECK(it != channels_.end());
1013 channels_.erase(it);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001014}
1015
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001016// Adjusts the default AGC target level by the specified delta.
1017// NB: If we start messing with other config fields, we'll want
1018// to save the current webrtc::AgcConfig as well.
1019bool WebRtcVoiceEngine::AdjustAgcLevel(int delta) {
solenberg566ef242015-11-06 15:34:49 -08001020 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001021 webrtc::AgcConfig config = default_agc_config_;
1022 config.targetLeveldBOv -= delta;
1023
1024 LOG(LS_INFO) << "Adjusting AGC level from default -"
1025 << default_agc_config_.targetLeveldBOv << "dB to -"
1026 << config.targetLeveldBOv << "dB";
1027
1028 if (voe_wrapper_->processing()->SetAgcConfig(config) == -1) {
1029 LOG_RTCERR1(SetAgcConfig, config.targetLeveldBOv);
1030 return false;
1031 }
1032 return true;
1033}
1034
ivocd66b44d2016-01-15 03:06:36 -08001035bool WebRtcVoiceEngine::StartAecDump(rtc::PlatformFile file,
1036 int64_t max_size_bytes) {
solenberg566ef242015-11-06 15:34:49 -08001037 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00001038 FILE* aec_dump_file_stream = rtc::FdopenPlatformFileForWriting(file);
wu@webrtc.orga8910d22014-01-23 22:12:45 +00001039 if (!aec_dump_file_stream) {
1040 LOG(LS_ERROR) << "Could not open AEC dump file stream.";
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00001041 if (!rtc::ClosePlatformFile(file))
wu@webrtc.orga8910d22014-01-23 22:12:45 +00001042 LOG(LS_WARNING) << "Could not close file.";
1043 return false;
1044 }
wu@webrtc.orga9890802013-12-13 00:21:03 +00001045 StopAecDump();
ivocd66b44d2016-01-15 03:06:36 -08001046 if (voe_wrapper_->base()->audio_processing()->StartDebugRecording(
1047 aec_dump_file_stream, max_size_bytes) !=
wu@webrtc.orga9890802013-12-13 00:21:03 +00001048 webrtc::AudioProcessing::kNoError) {
wu@webrtc.orga8910d22014-01-23 22:12:45 +00001049 LOG_RTCERR0(StartDebugRecording);
1050 fclose(aec_dump_file_stream);
wu@webrtc.orga9890802013-12-13 00:21:03 +00001051 return false;
1052 }
1053 is_dumping_aec_ = true;
1054 return true;
wu@webrtc.orga9890802013-12-13 00:21:03 +00001055}
1056
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001057void WebRtcVoiceEngine::StartAecDump(const std::string& filename) {
solenberg566ef242015-11-06 15:34:49 -08001058 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001059 if (!is_dumping_aec_) {
1060 // Start dumping AEC when we are not dumping.
ivocd66b44d2016-01-15 03:06:36 -08001061 if (voe_wrapper_->base()->audio_processing()->StartDebugRecording(
1062 filename.c_str(), -1) != webrtc::AudioProcessing::kNoError) {
wu@webrtc.orga9890802013-12-13 00:21:03 +00001063 LOG_RTCERR1(StartDebugRecording, filename.c_str());
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001064 } else {
1065 is_dumping_aec_ = true;
1066 }
1067 }
1068}
1069
1070void WebRtcVoiceEngine::StopAecDump() {
solenberg566ef242015-11-06 15:34:49 -08001071 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001072 if (is_dumping_aec_) {
1073 // Stop dumping AEC when we are dumping.
ivocd66b44d2016-01-15 03:06:36 -08001074 if (voe_wrapper_->base()->audio_processing()->StopDebugRecording() !=
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001075 webrtc::AudioProcessing::kNoError) {
1076 LOG_RTCERR0(StopDebugRecording);
1077 }
1078 is_dumping_aec_ = false;
1079 }
1080}
1081
ivocc1513ee2016-05-13 08:30:39 -07001082bool WebRtcVoiceEngine::StartRtcEventLog(rtc::PlatformFile file,
1083 int64_t max_size_bytes) {
solenberg566ef242015-11-06 15:34:49 -08001084 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
ivoc20834ca2016-02-04 06:33:37 -08001085 webrtc::RtcEventLog* event_log = voe_wrapper_->codec()->GetEventLog();
1086 if (event_log) {
ivocc1513ee2016-05-13 08:30:39 -07001087 return event_log->StartLogging(file, max_size_bytes);
ivoc20834ca2016-02-04 06:33:37 -08001088 }
1089 LOG_RTCERR0(StartRtcEventLog);
1090 return false;
ivoc112a3d82015-10-16 02:22:18 -07001091}
1092
1093void WebRtcVoiceEngine::StopRtcEventLog() {
solenberg566ef242015-11-06 15:34:49 -08001094 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
ivoc20834ca2016-02-04 06:33:37 -08001095 webrtc::RtcEventLog* event_log = voe_wrapper_->codec()->GetEventLog();
1096 if (event_log) {
1097 event_log->StopLogging();
1098 return;
1099 }
1100 LOG_RTCERR0(StopRtcEventLog);
ivoc112a3d82015-10-16 02:22:18 -07001101}
1102
solenberg0a617e22015-10-20 15:49:38 -07001103int WebRtcVoiceEngine::CreateVoEChannel() {
solenberg566ef242015-11-06 15:34:49 -08001104 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
solenberg0a617e22015-10-20 15:49:38 -07001105 return voe_wrapper_->base()->CreateChannel(voe_config_);
sergeyu@chromium.org5bc25c42013-12-05 00:24:06 +00001106}
1107
solenberg5b5129a2016-04-08 05:35:48 -07001108webrtc::AudioDeviceModule* WebRtcVoiceEngine::adm() {
1109 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
1110 RTC_DCHECK(adm_);
1111 return adm_;
1112}
1113
solenbergc96df772015-10-21 13:01:53 -07001114class WebRtcVoiceMediaChannel::WebRtcAudioSendStream
Taylor Brandstetter1a018dc2016-03-08 12:37:39 -08001115 : public AudioSource::Sink {
mallinath@webrtc.org67ee6b92014-02-03 16:57:16 +00001116 public:
skvlade0d46372016-04-07 22:59:22 -07001117 WebRtcAudioSendStream(int ch,
1118 webrtc::AudioTransport* voe_audio_transport,
1119 uint32_t ssrc,
1120 const std::string& c_name,
solenberg3a941542015-11-16 07:34:50 -08001121 const std::vector<webrtc::RtpExtension>& extensions,
mflodman3d7db262016-04-29 00:57:13 -07001122 webrtc::Call* call,
1123 webrtc::Transport* send_transport)
solenberg7add0582015-11-20 09:59:34 -08001124 : voe_audio_transport_(voe_audio_transport),
solenberg3a941542015-11-16 07:34:50 -08001125 call_(call),
mflodman3d7db262016-04-29 00:57:13 -07001126 config_(send_transport),
skvlade0d46372016-04-07 22:59:22 -07001127 rtp_parameters_(CreateRtpParametersWithOneEncoding()) {
solenberg85a04962015-10-27 03:35:21 -07001128 RTC_DCHECK_GE(ch, 0);
1129 // TODO(solenberg): Once we're not using FakeWebRtcVoiceEngine anymore:
1130 // RTC_DCHECK(voe_audio_transport);
solenbergc96df772015-10-21 13:01:53 -07001131 RTC_DCHECK(call);
solenberg85a04962015-10-27 03:35:21 -07001132 audio_capture_thread_checker_.DetachFromThread();
solenberg3a941542015-11-16 07:34:50 -08001133 config_.rtp.ssrc = ssrc;
1134 config_.rtp.c_name = c_name;
1135 config_.voe_channel_id = ch;
1136 RecreateAudioSendStream(extensions);
solenbergc96df772015-10-21 13:01:53 -07001137 }
solenberg3a941542015-11-16 07:34:50 -08001138
solenbergc96df772015-10-21 13:01:53 -07001139 ~WebRtcAudioSendStream() override {
solenberg566ef242015-11-06 15:34:49 -08001140 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
Taylor Brandstetter1a018dc2016-03-08 12:37:39 -08001141 ClearSource();
solenbergc96df772015-10-21 13:01:53 -07001142 call_->DestroyAudioSendStream(stream_);
1143 }
henrike@webrtc.org1e09a712013-07-26 19:17:59 +00001144
solenberg3a941542015-11-16 07:34:50 -08001145 void RecreateAudioSendStream(
1146 const std::vector<webrtc::RtpExtension>& extensions) {
1147 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
1148 if (stream_) {
1149 call_->DestroyAudioSendStream(stream_);
1150 stream_ = nullptr;
1151 }
1152 config_.rtp.extensions = extensions;
1153 RTC_DCHECK(!stream_);
1154 stream_ = call_->CreateAudioSendStream(config_);
1155 RTC_CHECK(stream_);
solenberg6d6e7c52016-04-13 09:07:30 -07001156 UpdateSendState();
solenberg3a941542015-11-16 07:34:50 -08001157 }
1158
solenberg8842c3e2016-03-11 03:06:41 -08001159 bool SendTelephoneEvent(int payload_type, int event, int duration_ms) {
Fredrik Solenbergb5727682015-12-04 15:22:19 +01001160 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
1161 RTC_DCHECK(stream_);
1162 return stream_->SendTelephoneEvent(payload_type, event, duration_ms);
1163 }
1164
Taylor Brandstetter1a018dc2016-03-08 12:37:39 -08001165 void SetSend(bool send) {
1166 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
1167 send_ = send;
1168 UpdateSendState();
1169 }
1170
solenberg3a941542015-11-16 07:34:50 -08001171 webrtc::AudioSendStream::Stats GetStats() const {
1172 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
1173 RTC_DCHECK(stream_);
1174 return stream_->GetStats();
1175 }
1176
Taylor Brandstetter1a018dc2016-03-08 12:37:39 -08001177 // Starts the sending by setting ourselves as a sink to the AudioSource to
1178 // get data callbacks.
henrike@webrtc.orga7b98182014-02-21 15:51:43 +00001179 // This method is called on the libjingle worker thread.
mallinath@webrtc.org67ee6b92014-02-03 16:57:16 +00001180 // TODO(xians): Make sure Start() is called only once.
Taylor Brandstetter1a018dc2016-03-08 12:37:39 -08001181 void SetSource(AudioSource* source) {
solenberg566ef242015-11-06 15:34:49 -08001182 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
Taylor Brandstetter1a018dc2016-03-08 12:37:39 -08001183 RTC_DCHECK(source);
1184 if (source_) {
1185 RTC_DCHECK(source_ == source);
mallinath@webrtc.org67ee6b92014-02-03 16:57:16 +00001186 return;
1187 }
Taylor Brandstetter1a018dc2016-03-08 12:37:39 -08001188 source->SetSink(this);
1189 source_ = source;
1190 UpdateSendState();
mallinath@webrtc.org67ee6b92014-02-03 16:57:16 +00001191 }
1192
Taylor Brandstetter1a018dc2016-03-08 12:37:39 -08001193 // Stops sending by setting the sink of the AudioSource to nullptr. No data
mallinath@webrtc.org67ee6b92014-02-03 16:57:16 +00001194 // callback will be received after this method.
henrike@webrtc.orga7b98182014-02-21 15:51:43 +00001195 // This method is called on the libjingle worker thread.
Taylor Brandstetter1a018dc2016-03-08 12:37:39 -08001196 void ClearSource() {
solenberg566ef242015-11-06 15:34:49 -08001197 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
Taylor Brandstetter1a018dc2016-03-08 12:37:39 -08001198 if (source_) {
1199 source_->SetSink(nullptr);
1200 source_ = nullptr;
solenberg98c68862015-10-09 03:27:14 -07001201 }
Taylor Brandstetter1a018dc2016-03-08 12:37:39 -08001202 UpdateSendState();
mallinath@webrtc.org67ee6b92014-02-03 16:57:16 +00001203 }
1204
Taylor Brandstetter1a018dc2016-03-08 12:37:39 -08001205 // AudioSource::Sink implementation.
henrike@webrtc.orga7b98182014-02-21 15:51:43 +00001206 // This method is called on the audio thread.
kjellander@webrtc.org14665ff2015-03-04 12:58:35 +00001207 void OnData(const void* audio_data,
1208 int bits_per_sample,
1209 int sample_rate,
Peter Kasting69558702016-01-12 16:26:35 -08001210 size_t number_of_channels,
Peter Kastingdce40cf2015-08-24 14:52:23 -07001211 size_t number_of_frames) override {
solenberg566ef242015-11-06 15:34:49 -08001212 RTC_DCHECK(!worker_thread_checker_.CalledOnValidThread());
solenberg85a04962015-10-27 03:35:21 -07001213 RTC_DCHECK(audio_capture_thread_checker_.CalledOnValidThread());
solenbergc96df772015-10-21 13:01:53 -07001214 RTC_DCHECK(voe_audio_transport_);
solenberg7add0582015-11-20 09:59:34 -08001215 voe_audio_transport_->OnData(config_.voe_channel_id,
henrike@webrtc.orga7b98182014-02-21 15:51:43 +00001216 audio_data,
1217 bits_per_sample,
1218 sample_rate,
1219 number_of_channels,
1220 number_of_frames);
henrike@webrtc.orga7b98182014-02-21 15:51:43 +00001221 }
1222
Taylor Brandstetter1a018dc2016-03-08 12:37:39 -08001223 // Callback from the |source_| when it is going away. In case Start() has
henrike@webrtc.orga7b98182014-02-21 15:51:43 +00001224 // never been called, this callback won't be triggered.
kjellander@webrtc.org14665ff2015-03-04 12:58:35 +00001225 void OnClose() override {
solenberg566ef242015-11-06 15:34:49 -08001226 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
Taylor Brandstetter1a018dc2016-03-08 12:37:39 -08001227 // Set |source_| to nullptr to make sure no more callback will get into
1228 // the source.
1229 source_ = nullptr;
1230 UpdateSendState();
mallinath@webrtc.org67ee6b92014-02-03 16:57:16 +00001231 }
1232
1233 // Accessor to the VoE channel ID.
solenberg85a04962015-10-27 03:35:21 -07001234 int channel() const {
solenberg566ef242015-11-06 15:34:49 -08001235 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
solenberg7add0582015-11-20 09:59:34 -08001236 return config_.voe_channel_id;
solenberg85a04962015-10-27 03:35:21 -07001237 }
mallinath@webrtc.org67ee6b92014-02-03 16:57:16 +00001238
skvlade0d46372016-04-07 22:59:22 -07001239 const webrtc::RtpParameters& rtp_parameters() const {
1240 return rtp_parameters_;
1241 }
1242
Taylor Brandstetter55dd7082016-05-03 13:50:11 -07001243 void SetRtpParameters(const webrtc::RtpParameters& parameters) {
skvlade0d46372016-04-07 22:59:22 -07001244 RTC_CHECK_EQ(1UL, parameters.encodings.size());
1245 rtp_parameters_ = parameters;
Taylor Brandstetter55dd7082016-05-03 13:50:11 -07001246 // parameters.encodings[0].active could have changed.
1247 UpdateSendState();
skvlade0d46372016-04-07 22:59:22 -07001248 }
1249
mallinath@webrtc.org67ee6b92014-02-03 16:57:16 +00001250 private:
Taylor Brandstetter1a018dc2016-03-08 12:37:39 -08001251 void UpdateSendState() {
1252 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
1253 RTC_DCHECK(stream_);
Taylor Brandstetter55dd7082016-05-03 13:50:11 -07001254 RTC_DCHECK_EQ(1UL, rtp_parameters_.encodings.size());
1255 if (send_ && source_ != nullptr && rtp_parameters_.encodings[0].active) {
Taylor Brandstetter1a018dc2016-03-08 12:37:39 -08001256 stream_->Start();
1257 } else { // !send || source_ = nullptr
1258 stream_->Stop();
1259 }
1260 }
1261
solenberg566ef242015-11-06 15:34:49 -08001262 rtc::ThreadChecker worker_thread_checker_;
solenberg85a04962015-10-27 03:35:21 -07001263 rtc::ThreadChecker audio_capture_thread_checker_;
solenbergc96df772015-10-21 13:01:53 -07001264 webrtc::AudioTransport* const voe_audio_transport_ = nullptr;
1265 webrtc::Call* call_ = nullptr;
solenberg3a941542015-11-16 07:34:50 -08001266 webrtc::AudioSendStream::Config config_;
1267 // The stream is owned by WebRtcAudioSendStream and may be reallocated if
1268 // configuration changes.
solenbergc96df772015-10-21 13:01:53 -07001269 webrtc::AudioSendStream* stream_ = nullptr;
mallinath@webrtc.org67ee6b92014-02-03 16:57:16 +00001270
Taylor Brandstetter1a018dc2016-03-08 12:37:39 -08001271 // Raw pointer to AudioSource owned by LocalAudioTrackHandler.
mallinath@webrtc.org67ee6b92014-02-03 16:57:16 +00001272 // PeerConnection will make sure invalidating the pointer before the object
1273 // goes away.
Taylor Brandstetter1a018dc2016-03-08 12:37:39 -08001274 AudioSource* source_ = nullptr;
1275 bool send_ = false;
skvlade0d46372016-04-07 22:59:22 -07001276 webrtc::RtpParameters rtp_parameters_;
henrike@webrtc.orga7b98182014-02-21 15:51:43 +00001277
solenbergc96df772015-10-21 13:01:53 -07001278 RTC_DISALLOW_IMPLICIT_CONSTRUCTORS(WebRtcAudioSendStream);
1279};
1280
1281class WebRtcVoiceMediaChannel::WebRtcAudioReceiveStream {
1282 public:
ossu29b1a8d2016-06-13 07:34:51 -07001283 WebRtcAudioReceiveStream(
1284 int ch,
1285 uint32_t remote_ssrc,
1286 uint32_t local_ssrc,
1287 bool use_transport_cc,
1288 const std::string& sync_group,
1289 const std::vector<webrtc::RtpExtension>& extensions,
1290 webrtc::Call* call,
1291 webrtc::Transport* rtcp_send_transport,
1292 const rtc::scoped_refptr<webrtc::AudioDecoderFactory>& decoder_factory)
stefanba4c0e42016-02-04 04:12:24 -08001293 : call_(call), config_() {
solenberg7add0582015-11-20 09:59:34 -08001294 RTC_DCHECK_GE(ch, 0);
1295 RTC_DCHECK(call);
1296 config_.rtp.remote_ssrc = remote_ssrc;
1297 config_.rtp.local_ssrc = local_ssrc;
solenberg31fec402016-05-06 02:13:12 -07001298 config_.rtcp_send_transport = rtcp_send_transport;
solenberg7add0582015-11-20 09:59:34 -08001299 config_.voe_channel_id = ch;
1300 config_.sync_group = sync_group;
ossu29b1a8d2016-06-13 07:34:51 -07001301 config_.decoder_factory = decoder_factory;
stefanba4c0e42016-02-04 04:12:24 -08001302 RecreateAudioReceiveStream(use_transport_cc, extensions);
solenberg7add0582015-11-20 09:59:34 -08001303 }
solenbergc96df772015-10-21 13:01:53 -07001304
solenberg7add0582015-11-20 09:59:34 -08001305 ~WebRtcAudioReceiveStream() {
1306 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
1307 call_->DestroyAudioReceiveStream(stream_);
1308 }
1309
1310 void RecreateAudioReceiveStream(
1311 const std::vector<webrtc::RtpExtension>& extensions) {
1312 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
stefanba4c0e42016-02-04 04:12:24 -08001313 RecreateAudioReceiveStream(config_.rtp.transport_cc, extensions);
solenberg7add0582015-11-20 09:59:34 -08001314 }
stefanba4c0e42016-02-04 04:12:24 -08001315 void RecreateAudioReceiveStream(bool use_transport_cc) {
solenberg7add0582015-11-20 09:59:34 -08001316 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
stefanba4c0e42016-02-04 04:12:24 -08001317 RecreateAudioReceiveStream(use_transport_cc, config_.rtp.extensions);
solenberg7add0582015-11-20 09:59:34 -08001318 }
1319
1320 webrtc::AudioReceiveStream::Stats GetStats() const {
1321 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
1322 RTC_DCHECK(stream_);
1323 return stream_->GetStats();
1324 }
1325
1326 int channel() const {
1327 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
1328 return config_.voe_channel_id;
1329 }
solenbergc96df772015-10-21 13:01:53 -07001330
kwiberg686a8ef2016-02-26 03:00:35 -08001331 void SetRawAudioSink(std::unique_ptr<webrtc::AudioSinkInterface> sink) {
Tommif888bb52015-12-12 01:37:01 +01001332 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
kwiberg686a8ef2016-02-26 03:00:35 -08001333 stream_->SetSink(std::move(sink));
Tommif888bb52015-12-12 01:37:01 +01001334 }
1335
solenbergc96df772015-10-21 13:01:53 -07001336 private:
stefanba4c0e42016-02-04 04:12:24 -08001337 void RecreateAudioReceiveStream(
1338 bool use_transport_cc,
solenberg7add0582015-11-20 09:59:34 -08001339 const std::vector<webrtc::RtpExtension>& extensions) {
1340 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
1341 if (stream_) {
1342 call_->DestroyAudioReceiveStream(stream_);
1343 stream_ = nullptr;
1344 }
1345 config_.rtp.extensions = extensions;
stefanba4c0e42016-02-04 04:12:24 -08001346 config_.rtp.transport_cc = use_transport_cc;
solenberg7add0582015-11-20 09:59:34 -08001347 RTC_DCHECK(!stream_);
1348 stream_ = call_->CreateAudioReceiveStream(config_);
1349 RTC_CHECK(stream_);
1350 }
1351
1352 rtc::ThreadChecker worker_thread_checker_;
1353 webrtc::Call* call_ = nullptr;
1354 webrtc::AudioReceiveStream::Config config_;
1355 // The stream is owned by WebRtcAudioReceiveStream and may be reallocated if
1356 // configuration changes.
1357 webrtc::AudioReceiveStream* stream_ = nullptr;
solenbergc96df772015-10-21 13:01:53 -07001358
1359 RTC_DISALLOW_IMPLICIT_CONSTRUCTORS(WebRtcAudioReceiveStream);
henrike@webrtc.org1e09a712013-07-26 19:17:59 +00001360};
1361
Fredrik Solenberg709ed672015-09-15 12:26:33 +02001362WebRtcVoiceMediaChannel::WebRtcVoiceMediaChannel(WebRtcVoiceEngine* engine,
nisse51542be2016-02-12 02:27:06 -08001363 const MediaConfig& config,
Fredrik Solenbergb071a192015-09-17 16:42:56 +02001364 const AudioOptions& options,
Fredrik Solenberg709ed672015-09-15 12:26:33 +02001365 webrtc::Call* call)
nisse51542be2016-02-12 02:27:06 -08001366 : VoiceMediaChannel(config), engine_(engine), call_(call) {
solenberg0a617e22015-10-20 15:49:38 -07001367 LOG(LS_VERBOSE) << "WebRtcVoiceMediaChannel::WebRtcVoiceMediaChannel";
solenberg566ef242015-11-06 15:34:49 -08001368 RTC_DCHECK(call);
solenberg0a617e22015-10-20 15:49:38 -07001369 engine->RegisterChannel(this);
Fredrik Solenbergb071a192015-09-17 16:42:56 +02001370 SetOptions(options);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001371}
1372
1373WebRtcVoiceMediaChannel::~WebRtcVoiceMediaChannel() {
solenberg566ef242015-11-06 15:34:49 -08001374 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
solenberg0a617e22015-10-20 15:49:38 -07001375 LOG(LS_VERBOSE) << "WebRtcVoiceMediaChannel::~WebRtcVoiceMediaChannel";
solenberg7add0582015-11-20 09:59:34 -08001376 // TODO(solenberg): Should be able to delete the streams directly, without
1377 // going through RemoveNnStream(), once stream objects handle
1378 // all (de)configuration.
solenbergc96df772015-10-21 13:01:53 -07001379 while (!send_streams_.empty()) {
1380 RemoveSendStream(send_streams_.begin()->first);
solenbergd97ec302015-10-07 01:40:33 -07001381 }
solenberg7add0582015-11-20 09:59:34 -08001382 while (!recv_streams_.empty()) {
1383 RemoveRecvStream(recv_streams_.begin()->first);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001384 }
solenberg0a617e22015-10-20 15:49:38 -07001385 engine()->UnregisterChannel(this);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001386}
1387
nisse51542be2016-02-12 02:27:06 -08001388rtc::DiffServCodePoint WebRtcVoiceMediaChannel::PreferredDscp() const {
1389 return kAudioDscpValue;
1390}
1391
Peter Thatcherc2ee2c82015-08-07 16:05:34 -07001392bool WebRtcVoiceMediaChannel::SetSendParameters(
1393 const AudioSendParameters& params) {
Peter Boströmca8b4042016-03-08 14:24:13 -08001394 TRACE_EVENT0("webrtc", "WebRtcVoiceMediaChannel::SetSendParameters");
solenberg566ef242015-11-06 15:34:49 -08001395 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
solenberg7e4e01a2015-12-02 08:05:01 -08001396 LOG(LS_INFO) << "WebRtcVoiceMediaChannel::SetSendParameters: "
1397 << params.ToString();
Peter Thatcherc2ee2c82015-08-07 16:05:34 -07001398 // TODO(pthatcher): Refactor this to be more clean now that we have
1399 // all the information at once.
solenberg3a941542015-11-16 07:34:50 -08001400
1401 if (!SetSendCodecs(params.codecs)) {
1402 return false;
1403 }
1404
solenberg7e4e01a2015-12-02 08:05:01 -08001405 if (!ValidateRtpExtensions(params.extensions)) {
1406 return false;
1407 }
1408 std::vector<webrtc::RtpExtension> filtered_extensions =
1409 FilterRtpExtensions(params.extensions,
1410 webrtc::RtpExtension::IsSupportedForAudio, true);
1411 if (send_rtp_extensions_ != filtered_extensions) {
1412 send_rtp_extensions_.swap(filtered_extensions);
solenberg3a941542015-11-16 07:34:50 -08001413 for (auto& it : send_streams_) {
1414 it.second->RecreateAudioSendStream(send_rtp_extensions_);
1415 }
1416 }
1417
deadbeef80346142016-04-27 14:17:10 -07001418 if (!SetMaxSendBitrate(params.max_bandwidth_bps)) {
solenberg3a941542015-11-16 07:34:50 -08001419 return false;
1420 }
1421 return SetOptions(params.options);
Peter Thatcherc2ee2c82015-08-07 16:05:34 -07001422}
1423
1424bool WebRtcVoiceMediaChannel::SetRecvParameters(
1425 const AudioRecvParameters& params) {
Peter Boströmca8b4042016-03-08 14:24:13 -08001426 TRACE_EVENT0("webrtc", "WebRtcVoiceMediaChannel::SetRecvParameters");
solenberg566ef242015-11-06 15:34:49 -08001427 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
solenberg7e4e01a2015-12-02 08:05:01 -08001428 LOG(LS_INFO) << "WebRtcVoiceMediaChannel::SetRecvParameters: "
1429 << params.ToString();
Peter Thatcherc2ee2c82015-08-07 16:05:34 -07001430 // TODO(pthatcher): Refactor this to be more clean now that we have
1431 // all the information at once.
solenberg7add0582015-11-20 09:59:34 -08001432
1433 if (!SetRecvCodecs(params.codecs)) {
1434 return false;
1435 }
1436
solenberg7e4e01a2015-12-02 08:05:01 -08001437 if (!ValidateRtpExtensions(params.extensions)) {
1438 return false;
1439 }
1440 std::vector<webrtc::RtpExtension> filtered_extensions =
1441 FilterRtpExtensions(params.extensions,
1442 webrtc::RtpExtension::IsSupportedForAudio, false);
1443 if (recv_rtp_extensions_ != filtered_extensions) {
1444 recv_rtp_extensions_.swap(filtered_extensions);
solenberg7add0582015-11-20 09:59:34 -08001445 for (auto& it : recv_streams_) {
1446 it.second->RecreateAudioReceiveStream(recv_rtp_extensions_);
1447 }
1448 }
solenberg7add0582015-11-20 09:59:34 -08001449 return true;
Peter Thatcherc2ee2c82015-08-07 16:05:34 -07001450}
1451
Taylor Brandstetterdb0cd9e2016-05-16 11:40:30 -07001452webrtc::RtpParameters WebRtcVoiceMediaChannel::GetRtpSendParameters(
skvlade0d46372016-04-07 22:59:22 -07001453 uint32_t ssrc) const {
1454 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
1455 auto it = send_streams_.find(ssrc);
1456 if (it == send_streams_.end()) {
Taylor Brandstetterdb0cd9e2016-05-16 11:40:30 -07001457 LOG(LS_WARNING) << "Attempting to get RTP send parameters for stream "
1458 << "with ssrc " << ssrc << " which doesn't exist.";
skvlade0d46372016-04-07 22:59:22 -07001459 return webrtc::RtpParameters();
1460 }
1461
Taylor Brandstetter0cd086b2016-04-20 16:23:10 -07001462 webrtc::RtpParameters rtp_params = it->second->rtp_parameters();
1463 // Need to add the common list of codecs to the send stream-specific
1464 // RTP parameters.
1465 for (const AudioCodec& codec : send_codecs_) {
1466 rtp_params.codecs.push_back(codec.ToCodecParameters());
1467 }
1468 return rtp_params;
skvlade0d46372016-04-07 22:59:22 -07001469}
1470
Taylor Brandstetterdb0cd9e2016-05-16 11:40:30 -07001471bool WebRtcVoiceMediaChannel::SetRtpSendParameters(
skvlade0d46372016-04-07 22:59:22 -07001472 uint32_t ssrc,
1473 const webrtc::RtpParameters& parameters) {
1474 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
1475 if (!ValidateRtpParameters(parameters)) {
1476 return false;
1477 }
1478 auto it = send_streams_.find(ssrc);
1479 if (it == send_streams_.end()) {
Taylor Brandstetterdb0cd9e2016-05-16 11:40:30 -07001480 LOG(LS_WARNING) << "Attempting to set RTP send parameters for stream "
1481 << "with ssrc " << ssrc << " which doesn't exist.";
skvlade0d46372016-04-07 22:59:22 -07001482 return false;
1483 }
1484
Taylor Brandstetterdb0cd9e2016-05-16 11:40:30 -07001485 // TODO(deadbeef): Handle setting parameters with a list of codecs in a
1486 // different order (which should change the send codec).
1487 webrtc::RtpParameters current_parameters = GetRtpSendParameters(ssrc);
1488 if (current_parameters.codecs != parameters.codecs) {
1489 LOG(LS_ERROR) << "Using SetParameters to change the set of codecs "
1490 << "is not currently supported.";
1491 return false;
1492 }
1493
1494 if (!SetChannelSendParameters(it->second->channel(), parameters)) {
1495 LOG(LS_WARNING) << "Failed to set send RtpParameters.";
skvlade0d46372016-04-07 22:59:22 -07001496 return false;
1497 }
Taylor Brandstetter0cd086b2016-04-20 16:23:10 -07001498 // Codecs are handled at the WebRtcVoiceMediaChannel level.
1499 webrtc::RtpParameters reduced_params = parameters;
1500 reduced_params.codecs.clear();
Taylor Brandstetter55dd7082016-05-03 13:50:11 -07001501 it->second->SetRtpParameters(reduced_params);
skvlade0d46372016-04-07 22:59:22 -07001502 return true;
1503}
1504
Taylor Brandstetterdb0cd9e2016-05-16 11:40:30 -07001505webrtc::RtpParameters WebRtcVoiceMediaChannel::GetRtpReceiveParameters(
1506 uint32_t ssrc) const {
1507 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
1508 auto it = recv_streams_.find(ssrc);
1509 if (it == recv_streams_.end()) {
1510 LOG(LS_WARNING) << "Attempting to get RTP receive parameters for stream "
1511 << "with ssrc " << ssrc << " which doesn't exist.";
1512 return webrtc::RtpParameters();
1513 }
1514
1515 // TODO(deadbeef): Return stream-specific parameters.
1516 webrtc::RtpParameters rtp_params = CreateRtpParametersWithOneEncoding();
1517 for (const AudioCodec& codec : recv_codecs_) {
1518 rtp_params.codecs.push_back(codec.ToCodecParameters());
1519 }
1520 return rtp_params;
1521}
1522
1523bool WebRtcVoiceMediaChannel::SetRtpReceiveParameters(
1524 uint32_t ssrc,
1525 const webrtc::RtpParameters& parameters) {
1526 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
1527 if (!ValidateRtpParameters(parameters)) {
1528 return false;
1529 }
1530 auto it = recv_streams_.find(ssrc);
1531 if (it == recv_streams_.end()) {
1532 LOG(LS_WARNING) << "Attempting to set RTP receive parameters for stream "
1533 << "with ssrc " << ssrc << " which doesn't exist.";
1534 return false;
1535 }
1536
1537 webrtc::RtpParameters current_parameters = GetRtpReceiveParameters(ssrc);
1538 if (current_parameters != parameters) {
1539 LOG(LS_ERROR) << "Changing the RTP receive parameters is currently "
1540 << "unsupported.";
1541 return false;
1542 }
1543 return true;
1544}
1545
skvlade0d46372016-04-07 22:59:22 -07001546bool WebRtcVoiceMediaChannel::ValidateRtpParameters(
1547 const webrtc::RtpParameters& rtp_parameters) {
1548 if (rtp_parameters.encodings.size() != 1) {
1549 LOG(LS_ERROR)
1550 << "Attempted to set RtpParameters without exactly one encoding";
1551 return false;
1552 }
1553 return true;
1554}
1555
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001556bool WebRtcVoiceMediaChannel::SetOptions(const AudioOptions& options) {
solenberg566ef242015-11-06 15:34:49 -08001557 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001558 LOG(LS_INFO) << "Setting voice channel options: "
1559 << options.ToString();
1560
1561 // We retain all of the existing options, and apply the given ones
1562 // on top. This means there is no way to "clear" options such that
1563 // they go back to the engine default.
1564 options_.SetAll(options);
solenberg246b8172015-12-08 09:50:23 -08001565 if (!engine()->ApplyOptions(options_)) {
1566 LOG(LS_WARNING) <<
1567 "Failed to apply engine options during channel SetOptions.";
1568 return false;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001569 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001570 LOG(LS_INFO) << "Set voice channel options. Current options: "
1571 << options_.ToString();
1572 return true;
1573}
1574
1575bool WebRtcVoiceMediaChannel::SetRecvCodecs(
1576 const std::vector<AudioCodec>& codecs) {
solenberg566ef242015-11-06 15:34:49 -08001577 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
solenberg8fb30c32015-10-13 03:06:58 -07001578
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001579 // Set the payload types to be used for incoming media.
solenberg0b675462015-10-09 01:37:09 -07001580 LOG(LS_INFO) << "Setting receive voice codecs.";
solenberg0b675462015-10-09 01:37:09 -07001581
1582 if (!VerifyUniquePayloadTypes(codecs)) {
1583 LOG(LS_ERROR) << "Codec payload types overlap.";
1584 return false;
1585 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001586
1587 std::vector<AudioCodec> new_codecs;
1588 // Find all new codecs. We allow adding new codecs but don't allow changing
1589 // the payload type of codecs that is already configured since we might
1590 // already be receiving packets with that payload type.
Fredrik Solenbergaf9fb212015-08-26 10:45:53 +02001591 for (const AudioCodec& codec : codecs) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001592 AudioCodec old_codec;
Fredrik Solenbergaf9fb212015-08-26 10:45:53 +02001593 if (FindCodec(recv_codecs_, codec, &old_codec)) {
1594 if (old_codec.id != codec.id) {
1595 LOG(LS_ERROR) << codec.name << " payload type changed.";
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001596 return false;
1597 }
1598 } else {
Fredrik Solenbergaf9fb212015-08-26 10:45:53 +02001599 new_codecs.push_back(codec);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001600 }
1601 }
1602 if (new_codecs.empty()) {
1603 // There are no new codecs to configure. Already configured codecs are
1604 // never removed.
1605 return true;
1606 }
1607
1608 if (playout_) {
1609 // Receive codecs can not be changed while playing. So we temporarily
1610 // pause playout.
1611 PausePlayout();
1612 }
1613
solenberg26c8c912015-11-27 04:00:25 -08001614 bool result = true;
1615 for (const AudioCodec& codec : new_codecs) {
solenberg72e29d22016-03-08 06:35:16 -08001616 webrtc::CodecInst voe_codec = {0};
solenberg26c8c912015-11-27 04:00:25 -08001617 if (WebRtcVoiceEngine::ToCodecInst(codec, &voe_codec)) {
1618 LOG(LS_INFO) << ToString(codec);
1619 voe_codec.pltype = codec.id;
1620 for (const auto& ch : recv_streams_) {
1621 if (engine()->voe()->codec()->SetRecPayloadType(
1622 ch.second->channel(), voe_codec) == -1) {
1623 LOG_RTCERR2(SetRecPayloadType, ch.second->channel(),
1624 ToString(voe_codec));
1625 result = false;
1626 }
1627 }
1628 } else {
1629 LOG(LS_WARNING) << "Unknown codec " << ToString(codec);
1630 result = false;
1631 break;
1632 }
1633 }
Fredrik Solenbergaf9fb212015-08-26 10:45:53 +02001634 if (result) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001635 recv_codecs_ = codecs;
1636 }
1637
1638 if (desired_playout_ && !playout_) {
1639 ResumePlayout();
1640 }
Fredrik Solenbergaf9fb212015-08-26 10:45:53 +02001641 return result;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001642}
1643
solenberg72e29d22016-03-08 06:35:16 -08001644// Utility function called from SetSendParameters() to extract current send
1645// codec settings from the given list of codecs (originally from SDP). Both send
1646// and receive streams may be reconfigured based on the new settings.
wu@webrtc.orgcadf9042013-08-30 21:24:16 +00001647bool WebRtcVoiceMediaChannel::SetSendCodecs(
1648 const std::vector<AudioCodec>& codecs) {
solenberg566ef242015-11-06 15:34:49 -08001649 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
Fredrik Solenbergb5727682015-12-04 15:22:19 +01001650 // TODO(solenberg): Validate input - that payload types don't overlap, are
1651 // within range, filter out codecs we don't support,
solenberg31642aa2016-03-14 08:00:37 -07001652 // redundant codecs etc - the same way it is done for
1653 // RtpHeaderExtensions.
solenbergd97ec302015-10-07 01:40:33 -07001654
Fredrik Solenbergb5727682015-12-04 15:22:19 +01001655 // Find the DTMF telephone event "codec" payload type.
1656 dtmf_payload_type_ = rtc::Optional<int>();
Fredrik Solenbergaf9fb212015-08-26 10:45:53 +02001657 for (const AudioCodec& codec : codecs) {
Fredrik Solenbergaf9fb212015-08-26 10:45:53 +02001658 if (IsCodec(codec, kDtmfCodecName)) {
solenberg31642aa2016-03-14 08:00:37 -07001659 if (codec.id < kMinPayloadType || codec.id > kMaxPayloadType) {
1660 return false;
1661 }
Fredrik Solenbergb5727682015-12-04 15:22:19 +01001662 dtmf_payload_type_ = rtc::Optional<int>(codec.id);
1663 break;
wu@webrtc.orgcadf9042013-08-30 21:24:16 +00001664 }
1665 }
1666
solenberg72e29d22016-03-08 06:35:16 -08001667 // Scan through the list to figure out the codec to use for sending, along
1668 // with the proper configuration for VAD, CNG, RED, NACK and Opus-specific
1669 // parameters.
1670 {
1671 SendCodecSpec send_codec_spec;
1672 send_codec_spec.nack_enabled = send_codec_spec_.nack_enabled;
1673
1674 // Find send codec (the first non-telephone-event/CN codec).
1675 const AudioCodec* codec = WebRtcVoiceCodecs::GetPreferredCodec(
1676 codecs, &send_codec_spec.codec_inst, &send_codec_spec.red_payload_type);
1677 if (!codec) {
1678 LOG(LS_WARNING) << "Received empty list of codecs.";
1679 return false;
1680 }
1681
1682 send_codec_spec.transport_cc_enabled = HasTransportCc(*codec);
1683
1684 // This condition is apparently here because Opus does not support RED and
1685 // FEC simultaneously. However, DTX and max playback rate shouldn't have
1686 // such limitations.
1687 // TODO(solenberg): Refactor this logic once we create AudioEncoders here.
1688 if (send_codec_spec.red_payload_type == -1) {
1689 send_codec_spec.nack_enabled = HasNack(*codec);
1690 // For Opus as the send codec, we are to determine inband FEC, maximum
1691 // playback rate, and opus internal dtx.
1692 if (IsCodec(*codec, kOpusCodecName)) {
1693 GetOpusConfig(*codec, &send_codec_spec.codec_inst,
1694 &send_codec_spec.enable_codec_fec,
1695 &send_codec_spec.opus_max_playback_rate,
1696 &send_codec_spec.enable_opus_dtx);
1697 }
1698
1699 // Set packet size if the AudioCodec param kCodecParamPTime is set.
1700 int ptime_ms = 0;
1701 if (codec->GetParam(kCodecParamPTime, &ptime_ms)) {
1702 if (!WebRtcVoiceCodecs::SetPTimeAsPacketSize(
1703 &send_codec_spec.codec_inst, ptime_ms)) {
1704 LOG(LS_WARNING) << "Failed to set packet size for codec "
1705 << send_codec_spec.codec_inst.plname;
1706 return false;
1707 }
1708 }
1709 }
1710
1711 // Loop through the codecs list again to find the CN codec.
1712 // TODO(solenberg): Break out into a separate function?
1713 for (const AudioCodec& codec : codecs) {
1714 // Ignore codecs we don't know about. The negotiation step should prevent
1715 // this, but double-check to be sure.
1716 webrtc::CodecInst voe_codec = {0};
1717 if (!WebRtcVoiceEngine::ToCodecInst(codec, &voe_codec)) {
1718 LOG(LS_WARNING) << "Unknown codec " << ToString(codec);
1719 continue;
1720 }
1721
1722 if (IsCodec(codec, kCnCodecName)) {
1723 // Turn voice activity detection/comfort noise on if supported.
1724 // Set the wideband CN payload type appropriately.
1725 // (narrowband always uses the static payload type 13).
1726 int cng_plfreq = -1;
1727 switch (codec.clockrate) {
1728 case 8000:
1729 case 16000:
1730 case 32000:
1731 cng_plfreq = codec.clockrate;
1732 break;
1733 default:
1734 LOG(LS_WARNING) << "CN frequency " << codec.clockrate
1735 << " not supported.";
1736 continue;
1737 }
1738 send_codec_spec.cng_payload_type = codec.id;
1739 send_codec_spec.cng_plfreq = cng_plfreq;
1740 break;
1741 }
1742 }
1743
1744 // Latch in the new state.
1745 send_codec_spec_ = std::move(send_codec_spec);
1746 }
1747
wu@webrtc.orgcadf9042013-08-30 21:24:16 +00001748 // Cache the codecs in order to configure the channel created later.
solenbergc96df772015-10-21 13:01:53 -07001749 for (const auto& ch : send_streams_) {
skvlade0d46372016-04-07 22:59:22 -07001750 if (!SetSendCodecs(ch.second->channel(), ch.second->rtp_parameters())) {
wu@webrtc.orgcadf9042013-08-30 21:24:16 +00001751 return false;
1752 }
1753 }
1754
solenberg72e29d22016-03-08 06:35:16 -08001755 // Set nack status on receive channels.
deadbeefb56069e2016-05-06 04:57:03 -07001756 for (const auto& kv : recv_streams_) {
1757 SetNack(kv.second->channel(), send_codec_spec_.nack_enabled);
wu@webrtc.org9dba5252013-08-05 20:36:57 +00001758 }
solenberg0a617e22015-10-20 15:49:38 -07001759
stefanba4c0e42016-02-04 04:12:24 -08001760 // Check if the transport cc feedback has changed on the preferred send codec,
1761 // and in that case reconfigure all receive streams.
solenberg72e29d22016-03-08 06:35:16 -08001762 if (recv_transport_cc_enabled_ != send_codec_spec_.transport_cc_enabled) {
1763 LOG(LS_INFO) << "Recreate all the receive streams because the send "
1764 "codec has changed.";
1765 recv_transport_cc_enabled_ = send_codec_spec_.transport_cc_enabled;
1766 for (auto& kv : recv_streams_) {
1767 kv.second->RecreateAudioReceiveStream(recv_transport_cc_enabled_);
1768 }
1769 }
1770
Taylor Brandstetter0cd086b2016-04-20 16:23:10 -07001771 send_codecs_ = codecs;
solenberg72e29d22016-03-08 06:35:16 -08001772 return true;
1773}
1774
1775// Apply current codec settings to a single voe::Channel used for sending.
skvlade0d46372016-04-07 22:59:22 -07001776bool WebRtcVoiceMediaChannel::SetSendCodecs(
1777 int channel,
1778 const webrtc::RtpParameters& rtp_parameters) {
solenberg72e29d22016-03-08 06:35:16 -08001779 // Disable VAD, FEC, and RED unless we know the other side wants them.
1780 engine()->voe()->codec()->SetVADStatus(channel, false);
1781 engine()->voe()->rtp()->SetNACKStatus(channel, false, 0);
1782 engine()->voe()->rtp()->SetREDStatus(channel, false);
1783 engine()->voe()->codec()->SetFECStatus(channel, false);
1784
1785 if (send_codec_spec_.red_payload_type != -1) {
1786 // Enable redundant encoding of the specified codec. Treat any
1787 // failure as a fatal internal error.
1788 LOG(LS_INFO) << "Enabling RED on channel " << channel;
1789 if (engine()->voe()->rtp()->SetREDStatus(channel, true,
1790 send_codec_spec_.red_payload_type) == -1) {
1791 LOG_RTCERR3(SetREDStatus, channel, true,
1792 send_codec_spec_.red_payload_type);
1793 return false;
1794 }
1795 }
1796
1797 SetNack(channel, send_codec_spec_.nack_enabled);
1798
1799 // Set the codec immediately, since SetVADStatus() depends on whether
1800 // the current codec is mono or stereo.
1801 if (!SetSendCodec(channel, send_codec_spec_.codec_inst)) {
1802 return false;
1803 }
1804
1805 // FEC should be enabled after SetSendCodec.
1806 if (send_codec_spec_.enable_codec_fec) {
1807 LOG(LS_INFO) << "Attempt to enable codec internal FEC on channel "
1808 << channel;
1809 if (engine()->voe()->codec()->SetFECStatus(channel, true) == -1) {
1810 // Enable codec internal FEC. Treat any failure as fatal internal error.
1811 LOG_RTCERR2(SetFECStatus, channel, true);
1812 return false;
1813 }
1814 }
1815
1816 if (IsCodec(send_codec_spec_.codec_inst, kOpusCodecName)) {
1817 // DTX and maxplaybackrate should be set after SetSendCodec. Because current
1818 // send codec has to be Opus.
1819
1820 // Set Opus internal DTX.
1821 LOG(LS_INFO) << "Attempt to "
1822 << (send_codec_spec_.enable_opus_dtx ? "enable" : "disable")
1823 << " Opus DTX on channel "
1824 << channel;
1825 if (engine()->voe()->codec()->SetOpusDtx(channel,
1826 send_codec_spec_.enable_opus_dtx)) {
1827 LOG_RTCERR2(SetOpusDtx, channel, send_codec_spec_.enable_opus_dtx);
1828 return false;
1829 }
1830
1831 // If opus_max_playback_rate <= 0, the default maximum playback rate
1832 // (48 kHz) will be used.
1833 if (send_codec_spec_.opus_max_playback_rate > 0) {
1834 LOG(LS_INFO) << "Attempt to set maximum playback rate to "
1835 << send_codec_spec_.opus_max_playback_rate
1836 << " Hz on channel "
1837 << channel;
1838 if (engine()->voe()->codec()->SetOpusMaxPlaybackRate(
1839 channel, send_codec_spec_.opus_max_playback_rate) == -1) {
1840 LOG_RTCERR2(SetOpusMaxPlaybackRate, channel,
1841 send_codec_spec_.opus_max_playback_rate);
1842 return false;
stefanba4c0e42016-02-04 04:12:24 -08001843 }
1844 }
1845 }
deadbeef80346142016-04-27 14:17:10 -07001846 // TODO(solenberg): SetMaxSendBitrate() yields another call to SetSendCodec().
skvlade0d46372016-04-07 22:59:22 -07001847 // Check if it is possible to fuse with the previous call in this function.
Taylor Brandstetterdb0cd9e2016-05-16 11:40:30 -07001848 SetChannelSendParameters(channel, rtp_parameters);
solenberg72e29d22016-03-08 06:35:16 -08001849
1850 // Set the CN payloadtype and the VAD status.
1851 if (send_codec_spec_.cng_payload_type != -1) {
1852 // The CN payload type for 8000 Hz clockrate is fixed at 13.
1853 if (send_codec_spec_.cng_plfreq != 8000) {
1854 webrtc::PayloadFrequencies cn_freq;
1855 switch (send_codec_spec_.cng_plfreq) {
1856 case 16000:
1857 cn_freq = webrtc::kFreq16000Hz;
1858 break;
1859 case 32000:
1860 cn_freq = webrtc::kFreq32000Hz;
1861 break;
1862 default:
1863 RTC_NOTREACHED();
1864 return false;
1865 }
1866 if (engine()->voe()->codec()->SetSendCNPayloadType(
1867 channel, send_codec_spec_.cng_payload_type, cn_freq) == -1) {
1868 LOG_RTCERR3(SetSendCNPayloadType, channel,
1869 send_codec_spec_.cng_payload_type, cn_freq);
1870 // TODO(ajm): This failure condition will be removed from VoE.
1871 // Restore the return here when we update to a new enough webrtc.
1872 //
1873 // Not returning false because the SetSendCNPayloadType will fail if
1874 // the channel is already sending.
1875 // This can happen if the remote description is applied twice, for
1876 // example in the case of ROAP on top of JSEP, where both side will
1877 // send the offer.
1878 }
1879 }
1880
1881 // Only turn on VAD if we have a CN payload type that matches the
1882 // clockrate for the codec we are going to use.
1883 if (send_codec_spec_.cng_plfreq == send_codec_spec_.codec_inst.plfreq &&
1884 send_codec_spec_.codec_inst.channels == 1) {
1885 // TODO(minyue): If CN frequency == 48000 Hz is allowed, consider the
1886 // interaction between VAD and Opus FEC.
1887 LOG(LS_INFO) << "Enabling VAD";
1888 if (engine()->voe()->codec()->SetVADStatus(channel, true) == -1) {
1889 LOG_RTCERR2(SetVADStatus, channel, true);
1890 return false;
1891 }
1892 }
1893 }
solenberg0a617e22015-10-20 15:49:38 -07001894 return true;
wu@webrtc.org9dba5252013-08-05 20:36:57 +00001895}
1896
wu@webrtc.orgcadf9042013-08-30 21:24:16 +00001897void WebRtcVoiceMediaChannel::SetNack(int channel, bool nack_enabled) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001898 if (nack_enabled) {
wu@webrtc.orgcadf9042013-08-30 21:24:16 +00001899 LOG(LS_INFO) << "Enabling NACK for channel " << channel;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001900 engine()->voe()->rtp()->SetNACKStatus(channel, true, kNackMaxPackets);
1901 } else {
wu@webrtc.orgcadf9042013-08-30 21:24:16 +00001902 LOG(LS_INFO) << "Disabling NACK for channel " << channel;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001903 engine()->voe()->rtp()->SetNACKStatus(channel, false, 0);
1904 }
1905}
1906
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001907bool WebRtcVoiceMediaChannel::SetSendCodec(
wu@webrtc.org9dba5252013-08-05 20:36:57 +00001908 int channel, const webrtc::CodecInst& send_codec) {
1909 LOG(LS_INFO) << "Send channel " << channel << " selected voice codec "
1910 << ToString(send_codec) << ", bitrate=" << send_codec.rate;
1911
solenberg72e29d22016-03-08 06:35:16 -08001912 webrtc::CodecInst current_codec = {0};
wu@webrtc.org05e7b442014-04-01 17:44:24 +00001913 if (engine()->voe()->codec()->GetSendCodec(channel, current_codec) == 0 &&
1914 (send_codec == current_codec)) {
1915 // Codec is already configured, we can return without setting it again.
1916 return true;
1917 }
1918
wu@webrtc.org9dba5252013-08-05 20:36:57 +00001919 if (engine()->voe()->codec()->SetSendCodec(channel, send_codec) == -1) {
1920 LOG_RTCERR2(SetSendCodec, channel, ToString(send_codec));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001921 return false;
1922 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001923 return true;
1924}
1925
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001926bool WebRtcVoiceMediaChannel::SetPlayout(bool playout) {
1927 desired_playout_ = playout;
1928 return ChangePlayout(desired_playout_);
1929}
1930
1931bool WebRtcVoiceMediaChannel::PausePlayout() {
1932 return ChangePlayout(false);
1933}
1934
1935bool WebRtcVoiceMediaChannel::ResumePlayout() {
1936 return ChangePlayout(desired_playout_);
1937}
1938
1939bool WebRtcVoiceMediaChannel::ChangePlayout(bool playout) {
Peter Boströmca8b4042016-03-08 14:24:13 -08001940 TRACE_EVENT0("webrtc", "WebRtcVoiceMediaChannel::ChangePlayout");
solenberg566ef242015-11-06 15:34:49 -08001941 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001942 if (playout_ == playout) {
1943 return true;
1944 }
1945
solenberg7add0582015-11-20 09:59:34 -08001946 for (const auto& ch : recv_streams_) {
Fredrik Solenbergaf9fb212015-08-26 10:45:53 +02001947 if (!SetPlayout(ch.second->channel(), playout)) {
henrike@webrtc.org1e09a712013-07-26 19:17:59 +00001948 LOG(LS_ERROR) << "SetPlayout " << playout << " on channel "
Fredrik Solenbergaf9fb212015-08-26 10:45:53 +02001949 << ch.second->channel() << " failed";
solenberg1ac56142015-10-13 03:58:19 -07001950 return false;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001951 }
1952 }
solenberg1ac56142015-10-13 03:58:19 -07001953 playout_ = playout;
1954 return true;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001955}
1956
Taylor Brandstetter1a018dc2016-03-08 12:37:39 -08001957void WebRtcVoiceMediaChannel::SetSend(bool send) {
Peter Boströmca8b4042016-03-08 14:24:13 -08001958 TRACE_EVENT0("webrtc", "WebRtcVoiceMediaChannel::SetSend");
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001959 if (send_ == send) {
Taylor Brandstetter1a018dc2016-03-08 12:37:39 -08001960 return;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001961 }
1962
solenbergd53a3f92016-04-14 13:56:37 -07001963 // Apply channel specific options, and initialize the ADM for recording (this
1964 // may take time on some platforms, e.g. Android).
Taylor Brandstetter1a018dc2016-03-08 12:37:39 -08001965 if (send) {
solenberg63b34542015-09-29 06:06:31 -07001966 engine()->ApplyOptions(options_);
solenbergd53a3f92016-04-14 13:56:37 -07001967
1968 // InitRecording() may return an error if the ADM is already recording.
1969 if (!engine()->adm()->RecordingIsInitialized() &&
1970 !engine()->adm()->Recording()) {
1971 if (engine()->adm()->InitRecording() != 0) {
1972 LOG(LS_WARNING) << "Failed to initialize recording";
1973 }
1974 }
solenberg63b34542015-09-29 06:06:31 -07001975 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001976
wu@webrtc.org9dba5252013-08-05 20:36:57 +00001977 // Change the settings on each send channel.
Taylor Brandstetter1a018dc2016-03-08 12:37:39 -08001978 for (auto& kv : send_streams_) {
1979 kv.second->SetSend(send);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001980 }
wu@webrtc.org9dba5252013-08-05 20:36:57 +00001981
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001982 send_ = send;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001983}
1984
Peter Boström0c4e06b2015-10-07 12:23:21 +02001985bool WebRtcVoiceMediaChannel::SetAudioSend(uint32_t ssrc,
1986 bool enable,
solenberg1dd98f32015-09-10 01:57:14 -07001987 const AudioOptions* options,
Taylor Brandstetter1a018dc2016-03-08 12:37:39 -08001988 AudioSource* source) {
solenberg566ef242015-11-06 15:34:49 -08001989 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
solenberg1dd98f32015-09-10 01:57:14 -07001990 // TODO(solenberg): The state change should be fully rolled back if any one of
1991 // these calls fail.
Taylor Brandstetter1a018dc2016-03-08 12:37:39 -08001992 if (!SetLocalSource(ssrc, source)) {
solenberg1dd98f32015-09-10 01:57:14 -07001993 return false;
1994 }
solenbergdfc8f4f2015-10-01 02:31:10 -07001995 if (!MuteStream(ssrc, !enable)) {
solenberg1dd98f32015-09-10 01:57:14 -07001996 return false;
1997 }
solenbergdfc8f4f2015-10-01 02:31:10 -07001998 if (enable && options) {
solenberg1dd98f32015-09-10 01:57:14 -07001999 return SetOptions(*options);
2000 }
2001 return true;
2002}
2003
solenberg0a617e22015-10-20 15:49:38 -07002004int WebRtcVoiceMediaChannel::CreateVoEChannel() {
2005 int id = engine()->CreateVoEChannel();
2006 if (id == -1) {
2007 LOG_RTCERR0(CreateVoEChannel);
2008 return -1;
wu@webrtc.org9dba5252013-08-05 20:36:57 +00002009 }
mflodman3d7db262016-04-29 00:57:13 -07002010
solenberg0a617e22015-10-20 15:49:38 -07002011 return id;
wu@webrtc.org9dba5252013-08-05 20:36:57 +00002012}
2013
solenberg7add0582015-11-20 09:59:34 -08002014bool WebRtcVoiceMediaChannel::DeleteVoEChannel(int channel) {
wu@webrtc.org9dba5252013-08-05 20:36:57 +00002015 if (engine()->voe()->base()->DeleteChannel(channel) == -1) {
2016 LOG_RTCERR1(DeleteChannel, channel);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002017 return false;
2018 }
wu@webrtc.org9dba5252013-08-05 20:36:57 +00002019 return true;
2020}
henrike@webrtc.org1e09a712013-07-26 19:17:59 +00002021
wu@webrtc.org9dba5252013-08-05 20:36:57 +00002022bool WebRtcVoiceMediaChannel::AddSendStream(const StreamParams& sp) {
Peter Boströmca8b4042016-03-08 14:24:13 -08002023 TRACE_EVENT0("webrtc", "WebRtcVoiceMediaChannel::AddSendStream");
solenberg566ef242015-11-06 15:34:49 -08002024 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
solenberg0a617e22015-10-20 15:49:38 -07002025 LOG(LS_INFO) << "AddSendStream: " << sp.ToString();
2026
2027 uint32_t ssrc = sp.first_ssrc();
2028 RTC_DCHECK(0 != ssrc);
2029
2030 if (GetSendChannelId(ssrc) != -1) {
2031 LOG(LS_ERROR) << "Stream already exists with ssrc " << ssrc;
wu@webrtc.org9dba5252013-08-05 20:36:57 +00002032 return false;
2033 }
2034
solenberg0a617e22015-10-20 15:49:38 -07002035 // Create a new channel for sending audio data.
2036 int channel = CreateVoEChannel();
2037 if (channel == -1) {
2038 return false;
wu@webrtc.org9dba5252013-08-05 20:36:57 +00002039 }
wu@webrtc.org9dba5252013-08-05 20:36:57 +00002040
solenbergc96df772015-10-21 13:01:53 -07002041 // Save the channel to send_streams_, so that RemoveSendStream() can still
wu@webrtc.org9dba5252013-08-05 20:36:57 +00002042 // delete the channel in case failure happens below.
mallinath@webrtc.org67ee6b92014-02-03 16:57:16 +00002043 webrtc::AudioTransport* audio_transport =
2044 engine()->voe()->base()->audio_transport();
mflodman3d7db262016-04-29 00:57:13 -07002045
skvlade0d46372016-04-07 22:59:22 -07002046 WebRtcAudioSendStream* stream = new WebRtcAudioSendStream(
mflodman3d7db262016-04-29 00:57:13 -07002047 channel, audio_transport, ssrc, sp.cname, send_rtp_extensions_, call_,
2048 this);
skvlade0d46372016-04-07 22:59:22 -07002049 send_streams_.insert(std::make_pair(ssrc, stream));
wu@webrtc.org9dba5252013-08-05 20:36:57 +00002050
solenberg0a617e22015-10-20 15:49:38 -07002051 // Set the current codecs to be used for the new channel. We need to do this
2052 // after adding the channel to send_channels_, because of how max bitrate is
2053 // currently being configured by SetSendCodec().
skvlade0d46372016-04-07 22:59:22 -07002054 if (HasSendCodec() && !SetSendCodecs(channel, stream->rtp_parameters())) {
solenberg0a617e22015-10-20 15:49:38 -07002055 RemoveSendStream(ssrc);
wu@webrtc.org9dba5252013-08-05 20:36:57 +00002056 return false;
2057 }
2058
2059 // At this point the channel's local SSRC has been updated. If the channel is
solenberg0a617e22015-10-20 15:49:38 -07002060 // the first send channel make sure that all the receive channels are updated
2061 // with the same SSRC in order to send receiver reports.
solenbergc96df772015-10-21 13:01:53 -07002062 if (send_streams_.size() == 1) {
solenberg0a617e22015-10-20 15:49:38 -07002063 receiver_reports_ssrc_ = ssrc;
solenberg7add0582015-11-20 09:59:34 -08002064 for (const auto& stream : recv_streams_) {
2065 int recv_channel = stream.second->channel();
solenberg0a617e22015-10-20 15:49:38 -07002066 if (engine()->voe()->rtp()->SetLocalSSRC(recv_channel, ssrc) != 0) {
solenberg7add0582015-11-20 09:59:34 -08002067 LOG_RTCERR2(SetLocalSSRC, recv_channel, ssrc);
solenberg1ac56142015-10-13 03:58:19 -07002068 return false;
wu@webrtc.org9dba5252013-08-05 20:36:57 +00002069 }
solenberg0a617e22015-10-20 15:49:38 -07002070 engine()->voe()->base()->AssociateSendChannel(recv_channel, channel);
2071 LOG(LS_INFO) << "VoiceEngine channel #" << recv_channel
2072 << " is associated with channel #" << channel << ".";
wu@webrtc.org9dba5252013-08-05 20:36:57 +00002073 }
2074 }
2075
Taylor Brandstetter1a018dc2016-03-08 12:37:39 -08002076 send_streams_[ssrc]->SetSend(send_);
2077 return true;
wu@webrtc.org9dba5252013-08-05 20:36:57 +00002078}
2079
Peter Boström0c4e06b2015-10-07 12:23:21 +02002080bool WebRtcVoiceMediaChannel::RemoveSendStream(uint32_t ssrc) {
Peter Boströmca8b4042016-03-08 14:24:13 -08002081 TRACE_EVENT0("webrtc", "WebRtcVoiceMediaChannel::RemoveSendStream");
solenberg566ef242015-11-06 15:34:49 -08002082 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
solenberg3a941542015-11-16 07:34:50 -08002083 LOG(LS_INFO) << "RemoveSendStream: " << ssrc;
2084
solenbergc96df772015-10-21 13:01:53 -07002085 auto it = send_streams_.find(ssrc);
2086 if (it == send_streams_.end()) {
wu@webrtc.org9dba5252013-08-05 20:36:57 +00002087 LOG(LS_WARNING) << "Try to remove stream with ssrc " << ssrc
2088 << " which doesn't exist.";
2089 return false;
2090 }
2091
Taylor Brandstetter1a018dc2016-03-08 12:37:39 -08002092 it->second->SetSend(false);
wu@webrtc.org9dba5252013-08-05 20:36:57 +00002093
solenberg7add0582015-11-20 09:59:34 -08002094 // Clean up and delete the send stream+channel.
Taylor Brandstetter1a018dc2016-03-08 12:37:39 -08002095 int channel = it->second->channel();
solenberg0a617e22015-10-20 15:49:38 -07002096 LOG(LS_INFO) << "Removing audio send stream " << ssrc
2097 << " with VoiceEngine channel #" << channel << ".";
solenberg7add0582015-11-20 09:59:34 -08002098 delete it->second;
2099 send_streams_.erase(it);
2100 if (!DeleteVoEChannel(channel)) {
solenberg0a617e22015-10-20 15:49:38 -07002101 return false;
wu@webrtc.org9dba5252013-08-05 20:36:57 +00002102 }
solenbergc96df772015-10-21 13:01:53 -07002103 if (send_streams_.empty()) {
Taylor Brandstetter1a018dc2016-03-08 12:37:39 -08002104 SetSend(false);
solenberg0a617e22015-10-20 15:49:38 -07002105 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002106 return true;
2107}
2108
2109bool WebRtcVoiceMediaChannel::AddRecvStream(const StreamParams& sp) {
Peter Boströmca8b4042016-03-08 14:24:13 -08002110 TRACE_EVENT0("webrtc", "WebRtcVoiceMediaChannel::AddRecvStream");
solenberg566ef242015-11-06 15:34:49 -08002111 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
solenbergd97ec302015-10-07 01:40:33 -07002112 LOG(LS_INFO) << "AddRecvStream: " << sp.ToString();
2113
solenberg0b675462015-10-09 01:37:09 -07002114 if (!ValidateStreamParams(sp)) {
wu@webrtc.org78187522013-10-07 23:32:02 +00002115 return false;
2116 }
2117
solenberg7add0582015-11-20 09:59:34 -08002118 const uint32_t ssrc = sp.first_ssrc();
solenberg0b675462015-10-09 01:37:09 -07002119 if (ssrc == 0) {
2120 LOG(LS_WARNING) << "AddRecvStream with ssrc==0 is not supported.";
2121 return false;
2122 }
2123
solenberg1ac56142015-10-13 03:58:19 -07002124 // Remove the default receive stream if one had been created with this ssrc;
2125 // we'll recreate it then.
2126 if (IsDefaultRecvStream(ssrc)) {
2127 RemoveRecvStream(ssrc);
2128 }
solenberg0b675462015-10-09 01:37:09 -07002129
solenberg7add0582015-11-20 09:59:34 -08002130 if (GetReceiveChannelId(ssrc) != -1) {
henrike@webrtc.org1e09a712013-07-26 19:17:59 +00002131 LOG(LS_ERROR) << "Stream already exists with ssrc " << ssrc;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002132 return false;
2133 }
Fredrik Solenberg4b60c732015-05-07 14:07:48 +02002134
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002135 // Create a new channel for receiving audio data.
solenberg7add0582015-11-20 09:59:34 -08002136 const int channel = CreateVoEChannel();
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002137 if (channel == -1) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002138 return false;
2139 }
Minyue2013aec2015-05-13 14:14:42 +02002140
solenberg1ac56142015-10-13 03:58:19 -07002141 // Turn off all supported codecs.
solenberg26c8c912015-11-27 04:00:25 -08002142 // TODO(solenberg): Remove once "no codecs" is the default state of a stream.
2143 for (webrtc::CodecInst voe_codec : webrtc::acm2::RentACodec::Database()) {
2144 voe_codec.pltype = -1;
2145 if (engine()->voe()->codec()->SetRecPayloadType(channel, voe_codec) == -1) {
2146 LOG_RTCERR2(SetRecPayloadType, channel, ToString(voe_codec));
2147 DeleteVoEChannel(channel);
2148 return false;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002149 }
2150 }
2151
solenberg1ac56142015-10-13 03:58:19 -07002152 // Only enable those configured for this channel.
2153 for (const auto& codec : recv_codecs_) {
solenberg72e29d22016-03-08 06:35:16 -08002154 webrtc::CodecInst voe_codec = {0};
solenberg26c8c912015-11-27 04:00:25 -08002155 if (WebRtcVoiceEngine::ToCodecInst(codec, &voe_codec)) {
solenberg1ac56142015-10-13 03:58:19 -07002156 voe_codec.pltype = codec.id;
2157 if (engine()->voe()->codec()->SetRecPayloadType(
2158 channel, voe_codec) == -1) {
2159 LOG_RTCERR2(SetRecPayloadType, channel, ToString(voe_codec));
solenberg7add0582015-11-20 09:59:34 -08002160 DeleteVoEChannel(channel);
solenberg1ac56142015-10-13 03:58:19 -07002161 return false;
2162 }
henrike@webrtc.org1e09a712013-07-26 19:17:59 +00002163 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002164 }
solenberg8fb30c32015-10-13 03:06:58 -07002165
solenberg7add0582015-11-20 09:59:34 -08002166 const int send_channel = GetSendChannelId(receiver_reports_ssrc_);
2167 if (send_channel != -1) {
2168 // Associate receive channel with first send channel (so the receive channel
2169 // can obtain RTT from the send channel)
2170 engine()->voe()->base()->AssociateSendChannel(channel, send_channel);
2171 LOG(LS_INFO) << "VoiceEngine channel #" << channel
2172 << " is associated with channel #" << send_channel << ".";
buildbot@webrtc.org150835e2014-05-06 15:54:38 +00002173 }
2174
stefanba4c0e42016-02-04 04:12:24 -08002175 recv_streams_.insert(std::make_pair(
2176 ssrc, new WebRtcAudioReceiveStream(channel, ssrc, receiver_reports_ssrc_,
solenberg72e29d22016-03-08 06:35:16 -08002177 recv_transport_cc_enabled_,
2178 sp.sync_label, recv_rtp_extensions_,
ossu29b1a8d2016-06-13 07:34:51 -07002179 call_, this,
2180 engine()->decoder_factory_)));
solenberg7add0582015-11-20 09:59:34 -08002181
solenberg72e29d22016-03-08 06:35:16 -08002182 SetNack(channel, send_codec_spec_.nack_enabled);
solenberg1ac56142015-10-13 03:58:19 -07002183 SetPlayout(channel, playout_);
solenberg7add0582015-11-20 09:59:34 -08002184
solenberg1ac56142015-10-13 03:58:19 -07002185 return true;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002186}
2187
Peter Boström0c4e06b2015-10-07 12:23:21 +02002188bool WebRtcVoiceMediaChannel::RemoveRecvStream(uint32_t ssrc) {
Peter Boströmca8b4042016-03-08 14:24:13 -08002189 TRACE_EVENT0("webrtc", "WebRtcVoiceMediaChannel::RemoveRecvStream");
solenberg566ef242015-11-06 15:34:49 -08002190 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
solenbergd97ec302015-10-07 01:40:33 -07002191 LOG(LS_INFO) << "RemoveRecvStream: " << ssrc;
2192
solenberg7add0582015-11-20 09:59:34 -08002193 const auto it = recv_streams_.find(ssrc);
2194 if (it == recv_streams_.end()) {
wu@webrtc.org9dba5252013-08-05 20:36:57 +00002195 LOG(LS_WARNING) << "Try to remove stream with ssrc " << ssrc
2196 << " which doesn't exist.";
henrike@webrtc.org1e09a712013-07-26 19:17:59 +00002197 return false;
wu@webrtc.org9dba5252013-08-05 20:36:57 +00002198 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002199
solenberg1ac56142015-10-13 03:58:19 -07002200 // Deregister default channel, if that's the one being destroyed.
2201 if (IsDefaultRecvStream(ssrc)) {
2202 default_recv_ssrc_ = -1;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002203 }
henrike@webrtc.org1e09a712013-07-26 19:17:59 +00002204
solenberg7add0582015-11-20 09:59:34 -08002205 const int channel = it->second->channel();
2206
2207 // Clean up and delete the receive stream+channel.
2208 LOG(LS_INFO) << "Removing audio receive stream " << ssrc
mallinath@webrtc.org67ee6b92014-02-03 16:57:16 +00002209 << " with VoiceEngine channel #" << channel << ".";
Tommif888bb52015-12-12 01:37:01 +01002210 it->second->SetRawAudioSink(nullptr);
solenberg7add0582015-11-20 09:59:34 -08002211 delete it->second;
2212 recv_streams_.erase(it);
2213 return DeleteVoEChannel(channel);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002214}
2215
Taylor Brandstetter1a018dc2016-03-08 12:37:39 -08002216bool WebRtcVoiceMediaChannel::SetLocalSource(uint32_t ssrc,
2217 AudioSource* source) {
solenbergc96df772015-10-21 13:01:53 -07002218 auto it = send_streams_.find(ssrc);
2219 if (it == send_streams_.end()) {
Taylor Brandstetter1a018dc2016-03-08 12:37:39 -08002220 if (source) {
2221 // Return an error if trying to set a valid source with an invalid ssrc.
2222 LOG(LS_ERROR) << "SetLocalSource failed with ssrc " << ssrc;
wu@webrtc.org9dba5252013-08-05 20:36:57 +00002223 return false;
2224 }
2225
2226 // The channel likely has gone away, do nothing.
henrike@webrtc.org1e09a712013-07-26 19:17:59 +00002227 return true;
henrike@webrtc.org1e09a712013-07-26 19:17:59 +00002228 }
2229
Taylor Brandstetter1a018dc2016-03-08 12:37:39 -08002230 if (source) {
2231 it->second->SetSource(source);
solenberg1ac56142015-10-13 03:58:19 -07002232 } else {
Taylor Brandstetter1a018dc2016-03-08 12:37:39 -08002233 it->second->ClearSource();
solenberg1ac56142015-10-13 03:58:19 -07002234 }
henrike@webrtc.org1e09a712013-07-26 19:17:59 +00002235
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002236 return true;
2237}
2238
2239bool WebRtcVoiceMediaChannel::GetActiveStreams(
2240 AudioInfo::StreamList* actives) {
solenberg566ef242015-11-06 15:34:49 -08002241 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002242 actives->clear();
solenberg7add0582015-11-20 09:59:34 -08002243 for (const auto& ch : recv_streams_) {
Fredrik Solenbergaf9fb212015-08-26 10:45:53 +02002244 int level = GetOutputLevel(ch.second->channel());
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002245 if (level > 0) {
Fredrik Solenbergaf9fb212015-08-26 10:45:53 +02002246 actives->push_back(std::make_pair(ch.first, level));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002247 }
2248 }
2249 return true;
2250}
2251
2252int WebRtcVoiceMediaChannel::GetOutputLevel() {
solenberg566ef242015-11-06 15:34:49 -08002253 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
solenberg1ac56142015-10-13 03:58:19 -07002254 int highest = 0;
solenberg7add0582015-11-20 09:59:34 -08002255 for (const auto& ch : recv_streams_) {
solenberg8fb30c32015-10-13 03:06:58 -07002256 highest = std::max(GetOutputLevel(ch.second->channel()), highest);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002257 }
2258 return highest;
2259}
2260
2261int WebRtcVoiceMediaChannel::GetTimeSinceLastTyping() {
2262 int ret;
2263 if (engine()->voe()->processing()->TimeSinceLastTyping(ret) == -1) {
2264 // In case of error, log the info and continue
2265 LOG_RTCERR0(TimeSinceLastTyping);
2266 ret = -1;
2267 } else {
2268 ret *= 1000; // We return ms, webrtc returns seconds.
2269 }
2270 return ret;
2271}
2272
2273void WebRtcVoiceMediaChannel::SetTypingDetectionParameters(int time_window,
2274 int cost_per_typing, int reporting_threshold, int penalty_decay,
2275 int type_event_delay) {
2276 if (engine()->voe()->processing()->SetTypingDetectionParameters(
2277 time_window, cost_per_typing,
2278 reporting_threshold, penalty_decay, type_event_delay) == -1) {
2279 // In case of error, log the info and continue
2280 LOG_RTCERR5(SetTypingDetectionParameters, time_window,
2281 cost_per_typing, reporting_threshold, penalty_decay,
2282 type_event_delay);
2283 }
2284}
2285
solenberg4bac9c52015-10-09 02:32:53 -07002286bool WebRtcVoiceMediaChannel::SetOutputVolume(uint32_t ssrc, double volume) {
solenberg566ef242015-11-06 15:34:49 -08002287 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
solenberg1ac56142015-10-13 03:58:19 -07002288 if (ssrc == 0) {
2289 default_recv_volume_ = volume;
2290 if (default_recv_ssrc_ == -1) {
2291 return true;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002292 }
solenberg1ac56142015-10-13 03:58:19 -07002293 ssrc = static_cast<uint32_t>(default_recv_ssrc_);
2294 }
2295 int ch_id = GetReceiveChannelId(ssrc);
2296 if (ch_id < 0) {
2297 LOG(LS_WARNING) << "Cannot find channel for ssrc:" << ssrc;
2298 return false;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002299 }
2300
solenberg1ac56142015-10-13 03:58:19 -07002301 if (-1 == engine()->voe()->volume()->SetChannelOutputVolumeScaling(ch_id,
2302 volume)) {
2303 LOG_RTCERR2(SetChannelOutputVolumeScaling, ch_id, volume);
2304 return false;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002305 }
solenberg1ac56142015-10-13 03:58:19 -07002306 LOG(LS_INFO) << "SetOutputVolume to " << volume
2307 << " for channel " << ch_id << " and ssrc " << ssrc;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002308 return true;
2309}
2310
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002311bool WebRtcVoiceMediaChannel::CanInsertDtmf() {
Fredrik Solenbergb5727682015-12-04 15:22:19 +01002312 return dtmf_payload_type_ ? true : false;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002313}
2314
solenberg1d63dd02015-12-02 12:35:09 -08002315bool WebRtcVoiceMediaChannel::InsertDtmf(uint32_t ssrc, int event,
2316 int duration) {
solenberg566ef242015-11-06 15:34:49 -08002317 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
Fredrik Solenbergb5727682015-12-04 15:22:19 +01002318 LOG(LS_INFO) << "WebRtcVoiceMediaChannel::InsertDtmf";
2319 if (!dtmf_payload_type_) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002320 return false;
2321 }
2322
Fredrik Solenbergb5727682015-12-04 15:22:19 +01002323 // Figure out which WebRtcAudioSendStream to send the event on.
2324 auto it = ssrc != 0 ? send_streams_.find(ssrc) : send_streams_.begin();
2325 if (it == send_streams_.end()) {
2326 LOG(LS_WARNING) << "The specified ssrc " << ssrc << " is not in use.";
solenberg1d63dd02015-12-02 12:35:09 -08002327 return false;
2328 }
Fredrik Solenbergb5727682015-12-04 15:22:19 +01002329 if (event < kMinTelephoneEventCode ||
2330 event > kMaxTelephoneEventCode) {
2331 LOG(LS_WARNING) << "DTMF event code " << event << " out of range.";
solenberg1d63dd02015-12-02 12:35:09 -08002332 return false;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002333 }
Fredrik Solenbergb5727682015-12-04 15:22:19 +01002334 if (duration < kMinTelephoneEventDuration ||
2335 duration > kMaxTelephoneEventDuration) {
2336 LOG(LS_WARNING) << "DTMF event duration " << duration << " out of range.";
2337 return false;
2338 }
2339 return it->second->SendTelephoneEvent(*dtmf_payload_type_, event, duration);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002340}
2341
wu@webrtc.orga9890802013-12-13 00:21:03 +00002342void WebRtcVoiceMediaChannel::OnPacketReceived(
jbaucheec21bd2016-03-20 06:15:43 -07002343 rtc::CopyOnWriteBuffer* packet, const rtc::PacketTime& packet_time) {
solenberg566ef242015-11-06 15:34:49 -08002344 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
Fredrik Solenberg4b60c732015-05-07 14:07:48 +02002345
mflodman3d7db262016-04-29 00:57:13 -07002346 const webrtc::PacketTime webrtc_packet_time(packet_time.timestamp,
2347 packet_time.not_before);
2348 webrtc::PacketReceiver::DeliveryStatus delivery_result =
2349 call_->Receiver()->DeliverPacket(webrtc::MediaType::AUDIO,
2350 packet->cdata(), packet->size(),
2351 webrtc_packet_time);
mflodman3d7db262016-04-29 00:57:13 -07002352 if (delivery_result != webrtc::PacketReceiver::DELIVERY_UNKNOWN_SSRC) {
2353 return;
2354 }
2355
2356 // Create a default receive stream for this unsignalled and previously not
2357 // received ssrc. If there already is a default receive stream, delete it.
2358 // See: https://bugs.chromium.org/p/webrtc/issues/detail?id=5208
solenberg1ac56142015-10-13 03:58:19 -07002359 uint32_t ssrc = 0;
jbaucheec21bd2016-03-20 06:15:43 -07002360 if (!GetRtpSsrc(packet->cdata(), packet->size(), &ssrc)) {
solenberg1ac56142015-10-13 03:58:19 -07002361 return;
2362 }
2363
mflodman3d7db262016-04-29 00:57:13 -07002364 if (default_recv_ssrc_ != -1) {
2365 LOG(LS_INFO) << "Removing default receive stream with ssrc "
2366 << default_recv_ssrc_;
2367 RTC_DCHECK_NE(ssrc, default_recv_ssrc_);
2368 RemoveRecvStream(default_recv_ssrc_);
2369 default_recv_ssrc_ = -1;
solenberg1ac56142015-10-13 03:58:19 -07002370 }
2371
mflodman3d7db262016-04-29 00:57:13 -07002372 StreamParams sp;
2373 sp.ssrcs.push_back(ssrc);
2374 LOG(LS_INFO) << "Creating default receive stream for SSRC=" << ssrc << ".";
2375 if (!AddRecvStream(sp)) {
2376 LOG(LS_WARNING) << "Could not create default receive stream.";
2377 return;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002378 }
mflodman3d7db262016-04-29 00:57:13 -07002379 default_recv_ssrc_ = ssrc;
2380 SetOutputVolume(default_recv_ssrc_, default_recv_volume_);
2381 if (default_sink_) {
2382 std::unique_ptr<webrtc::AudioSinkInterface> proxy_sink(
2383 new ProxySink(default_sink_.get()));
2384 SetRawAudioSink(default_recv_ssrc_, std::move(proxy_sink));
2385 }
2386 delivery_result = call_->Receiver()->DeliverPacket(webrtc::MediaType::AUDIO,
2387 packet->cdata(),
2388 packet->size(),
2389 webrtc_packet_time);
2390 RTC_DCHECK_NE(webrtc::PacketReceiver::DELIVERY_UNKNOWN_SSRC, delivery_result);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002391}
2392
wu@webrtc.orga9890802013-12-13 00:21:03 +00002393void WebRtcVoiceMediaChannel::OnRtcpReceived(
jbaucheec21bd2016-03-20 06:15:43 -07002394 rtc::CopyOnWriteBuffer* packet, const rtc::PacketTime& packet_time) {
solenberg566ef242015-11-06 15:34:49 -08002395 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
Fredrik Solenberg4b60c732015-05-07 14:07:48 +02002396
Fredrik Solenberg709ed672015-09-15 12:26:33 +02002397 // Forward packet to Call as well.
2398 const webrtc::PacketTime webrtc_packet_time(packet_time.timestamp,
2399 packet_time.not_before);
2400 call_->Receiver()->DeliverPacket(webrtc::MediaType::AUDIO,
jbaucheec21bd2016-03-20 06:15:43 -07002401 packet->cdata(), packet->size(), webrtc_packet_time);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002402}
2403
Honghai Zhangcc411c02016-03-29 17:27:21 -07002404void WebRtcVoiceMediaChannel::OnNetworkRouteChanged(
2405 const std::string& transport_name,
Honghai Zhang0e533ef2016-04-19 15:41:36 -07002406 const rtc::NetworkRoute& network_route) {
2407 call_->OnNetworkRouteChanged(transport_name, network_route);
Honghai Zhangcc411c02016-03-29 17:27:21 -07002408}
2409
Peter Boström0c4e06b2015-10-07 12:23:21 +02002410bool WebRtcVoiceMediaChannel::MuteStream(uint32_t ssrc, bool muted) {
solenberg566ef242015-11-06 15:34:49 -08002411 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
solenberg0a617e22015-10-20 15:49:38 -07002412 int channel = GetSendChannelId(ssrc);
wu@webrtc.org9dba5252013-08-05 20:36:57 +00002413 if (channel == -1) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002414 LOG(LS_WARNING) << "The specified ssrc " << ssrc << " is not in use.";
2415 return false;
2416 }
wu@webrtc.org9dba5252013-08-05 20:36:57 +00002417 if (engine()->voe()->volume()->SetInputMute(channel, muted) == -1) {
2418 LOG_RTCERR2(SetInputMute, channel, muted);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002419 return false;
2420 }
buildbot@webrtc.org6b21b712014-07-31 15:08:53 +00002421 // We set the AGC to mute state only when all the channels are muted.
2422 // This implementation is not ideal, instead we should signal the AGC when
2423 // the mic channel is muted/unmuted. We can't do it today because there
2424 // is no good way to know which stream is mapping to the mic channel.
2425 bool all_muted = muted;
solenbergc96df772015-10-21 13:01:53 -07002426 for (const auto& ch : send_streams_) {
Fredrik Solenbergaf9fb212015-08-26 10:45:53 +02002427 if (!all_muted) {
2428 break;
2429 }
2430 if (engine()->voe()->volume()->GetInputMute(ch.second->channel(),
buildbot@webrtc.org6b21b712014-07-31 15:08:53 +00002431 all_muted)) {
Fredrik Solenbergaf9fb212015-08-26 10:45:53 +02002432 LOG_RTCERR1(GetInputMute, ch.second->channel());
buildbot@webrtc.org6b21b712014-07-31 15:08:53 +00002433 return false;
2434 }
2435 }
2436
2437 webrtc::AudioProcessing* ap = engine()->voe()->base()->audio_processing();
solenberg0a617e22015-10-20 15:49:38 -07002438 if (ap) {
buildbot@webrtc.org6b21b712014-07-31 15:08:53 +00002439 ap->set_output_will_be_muted(all_muted);
solenberg0a617e22015-10-20 15:49:38 -07002440 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002441 return true;
2442}
2443
deadbeef80346142016-04-27 14:17:10 -07002444bool WebRtcVoiceMediaChannel::SetMaxSendBitrate(int bps) {
2445 LOG(LS_INFO) << "WebRtcVoiceMediaChannel::SetMaxSendBitrate.";
2446 max_send_bitrate_bps_ = bps;
skvlade0d46372016-04-07 22:59:22 -07002447
2448 for (const auto& kv : send_streams_) {
Taylor Brandstetterdb0cd9e2016-05-16 11:40:30 -07002449 if (!SetChannelSendParameters(kv.second->channel(),
2450 kv.second->rtp_parameters())) {
skvlade0d46372016-04-07 22:59:22 -07002451 return false;
2452 }
2453 }
2454 return true;
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00002455}
2456
Taylor Brandstetterdb0cd9e2016-05-16 11:40:30 -07002457bool WebRtcVoiceMediaChannel::SetChannelSendParameters(
skvlade0d46372016-04-07 22:59:22 -07002458 int channel,
2459 const webrtc::RtpParameters& parameters) {
2460 RTC_CHECK_EQ(1UL, parameters.encodings.size());
Taylor Brandstetter0cd086b2016-04-20 16:23:10 -07002461 // TODO(deadbeef): Handle setting parameters with a list of codecs in a
2462 // different order (which should change the send codec).
deadbeef80346142016-04-27 14:17:10 -07002463 return SetMaxSendBitrate(
2464 channel, MinPositive(max_send_bitrate_bps_,
2465 parameters.encodings[0].max_bitrate_bps));
skvlade0d46372016-04-07 22:59:22 -07002466}
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00002467
deadbeef80346142016-04-27 14:17:10 -07002468bool WebRtcVoiceMediaChannel::SetMaxSendBitrate(int channel, int bps) {
skvlade0d46372016-04-07 22:59:22 -07002469 // Bitrate is auto by default.
2470 // TODO(bemasc): Fix this so that if SetMaxSendBandwidth(50) is followed by
2471 // SetMaxSendBandwith(0), the second call removes the previous limit.
deadbeef80346142016-04-27 14:17:10 -07002472 if (bps <= 0) {
skvlade0d46372016-04-07 22:59:22 -07002473 return true;
deadbeef80346142016-04-27 14:17:10 -07002474 }
wu@webrtc.org1d1ffc92013-10-16 18:12:02 +00002475
solenberg72e29d22016-03-08 06:35:16 -08002476 if (!HasSendCodec()) {
wu@webrtc.org1d1ffc92013-10-16 18:12:02 +00002477 LOG(LS_INFO) << "The send codec has not been set up yet. "
minyue@webrtc.org26236952014-10-29 02:27:08 +00002478 << "The send bitrate setting will be applied later.";
wu@webrtc.org1d1ffc92013-10-16 18:12:02 +00002479 return true;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002480 }
2481
solenberg72e29d22016-03-08 06:35:16 -08002482 webrtc::CodecInst codec = send_codec_spec_.codec_inst;
solenberg26c8c912015-11-27 04:00:25 -08002483 bool is_multi_rate = WebRtcVoiceCodecs::IsCodecMultiRate(codec);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002484
2485 if (is_multi_rate) {
2486 // If codec is multi-rate then just set the bitrate.
deadbeef80346142016-04-27 14:17:10 -07002487 int max_bitrate_bps = WebRtcVoiceCodecs::MaxBitrateBps(codec);
2488 codec.rate = std::min(bps, max_bitrate_bps);
2489 LOG(LS_INFO) << "Setting codec " << codec.plname << " to bitrate " << bps
2490 << " bps.";
skvlade0d46372016-04-07 22:59:22 -07002491 if (!SetSendCodec(channel, codec)) {
deadbeef80346142016-04-27 14:17:10 -07002492 LOG(LS_ERROR) << "Failed to set codec " << codec.plname << " to bitrate "
2493 << bps << " bps.";
skvlade0d46372016-04-07 22:59:22 -07002494 return false;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002495 }
2496 return true;
2497 } else {
2498 // If codec is not multi-rate and |bps| is less than the fixed bitrate
2499 // then fail. If codec is not multi-rate and |bps| exceeds or equal the
2500 // fixed bitrate then ignore.
2501 if (bps < codec.rate) {
deadbeef80346142016-04-27 14:17:10 -07002502 LOG(LS_ERROR) << "Failed to set codec " << codec.plname << " to bitrate "
2503 << bps << " bps"
2504 << ", requires at least " << codec.rate << " bps.";
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002505 return false;
2506 }
2507 return true;
2508 }
2509}
2510
skvlad7a43d252016-03-22 15:32:27 -07002511void WebRtcVoiceMediaChannel::OnReadyToSend(bool ready) {
2512 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
2513 LOG(LS_VERBOSE) << "OnReadyToSend: " << (ready ? "Ready." : "Not ready.");
2514 call_->SignalChannelNetworkState(
2515 webrtc::MediaType::AUDIO,
2516 ready ? webrtc::kNetworkUp : webrtc::kNetworkDown);
2517}
2518
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002519bool WebRtcVoiceMediaChannel::GetStats(VoiceMediaInfo* info) {
Peter Boströmca8b4042016-03-08 14:24:13 -08002520 TRACE_EVENT0("webrtc", "WebRtcVoiceMediaChannel::GetStats");
solenberg566ef242015-11-06 15:34:49 -08002521 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
solenberg85a04962015-10-27 03:35:21 -07002522 RTC_DCHECK(info);
solenbergd97ec302015-10-07 01:40:33 -07002523
solenberg85a04962015-10-27 03:35:21 -07002524 // Get SSRC and stats for each sender.
2525 RTC_DCHECK(info->senders.size() == 0);
2526 for (const auto& stream : send_streams_) {
2527 webrtc::AudioSendStream::Stats stats = stream.second->GetStats();
wu@webrtc.org9dba5252013-08-05 20:36:57 +00002528 VoiceSenderInfo sinfo;
solenberg85a04962015-10-27 03:35:21 -07002529 sinfo.add_ssrc(stats.local_ssrc);
2530 sinfo.bytes_sent = stats.bytes_sent;
2531 sinfo.packets_sent = stats.packets_sent;
2532 sinfo.packets_lost = stats.packets_lost;
2533 sinfo.fraction_lost = stats.fraction_lost;
2534 sinfo.codec_name = stats.codec_name;
2535 sinfo.ext_seqnum = stats.ext_seqnum;
2536 sinfo.jitter_ms = stats.jitter_ms;
2537 sinfo.rtt_ms = stats.rtt_ms;
2538 sinfo.audio_level = stats.audio_level;
2539 sinfo.aec_quality_min = stats.aec_quality_min;
2540 sinfo.echo_delay_median_ms = stats.echo_delay_median_ms;
2541 sinfo.echo_delay_std_ms = stats.echo_delay_std_ms;
2542 sinfo.echo_return_loss = stats.echo_return_loss;
2543 sinfo.echo_return_loss_enhancement = stats.echo_return_loss_enhancement;
Taylor Brandstetter1a018dc2016-03-08 12:37:39 -08002544 sinfo.typing_noise_detected = (send_ ? stats.typing_noise_detected : false);
wu@webrtc.org9dba5252013-08-05 20:36:57 +00002545 info->senders.push_back(sinfo);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002546 }
2547
solenberg85a04962015-10-27 03:35:21 -07002548 // Get SSRC and stats for each receiver.
2549 RTC_DCHECK(info->receivers.size() == 0);
solenberg7add0582015-11-20 09:59:34 -08002550 for (const auto& stream : recv_streams_) {
Fredrik Solenberg4f4ec0a2015-10-22 10:49:27 +02002551 webrtc::AudioReceiveStream::Stats stats = stream.second->GetStats();
2552 VoiceReceiverInfo rinfo;
2553 rinfo.add_ssrc(stats.remote_ssrc);
2554 rinfo.bytes_rcvd = stats.bytes_rcvd;
2555 rinfo.packets_rcvd = stats.packets_rcvd;
2556 rinfo.packets_lost = stats.packets_lost;
2557 rinfo.fraction_lost = stats.fraction_lost;
2558 rinfo.codec_name = stats.codec_name;
2559 rinfo.ext_seqnum = stats.ext_seqnum;
2560 rinfo.jitter_ms = stats.jitter_ms;
2561 rinfo.jitter_buffer_ms = stats.jitter_buffer_ms;
2562 rinfo.jitter_buffer_preferred_ms = stats.jitter_buffer_preferred_ms;
2563 rinfo.delay_estimate_ms = stats.delay_estimate_ms;
2564 rinfo.audio_level = stats.audio_level;
2565 rinfo.expand_rate = stats.expand_rate;
2566 rinfo.speech_expand_rate = stats.speech_expand_rate;
2567 rinfo.secondary_decoded_rate = stats.secondary_decoded_rate;
2568 rinfo.accelerate_rate = stats.accelerate_rate;
2569 rinfo.preemptive_expand_rate = stats.preemptive_expand_rate;
2570 rinfo.decoding_calls_to_silence_generator =
2571 stats.decoding_calls_to_silence_generator;
2572 rinfo.decoding_calls_to_neteq = stats.decoding_calls_to_neteq;
2573 rinfo.decoding_normal = stats.decoding_normal;
2574 rinfo.decoding_plc = stats.decoding_plc;
2575 rinfo.decoding_cng = stats.decoding_cng;
2576 rinfo.decoding_plc_cng = stats.decoding_plc_cng;
2577 rinfo.capture_start_ntp_time_ms = stats.capture_start_ntp_time_ms;
2578 info->receivers.push_back(rinfo);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002579 }
2580
2581 return true;
2582}
2583
Tommif888bb52015-12-12 01:37:01 +01002584void WebRtcVoiceMediaChannel::SetRawAudioSink(
2585 uint32_t ssrc,
kwiberg686a8ef2016-02-26 03:00:35 -08002586 std::unique_ptr<webrtc::AudioSinkInterface> sink) {
Tommif888bb52015-12-12 01:37:01 +01002587 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
deadbeef884f5852016-01-15 09:20:04 -08002588 LOG(LS_VERBOSE) << "WebRtcVoiceMediaChannel::SetRawAudioSink: ssrc:" << ssrc
2589 << " " << (sink ? "(ptr)" : "NULL");
2590 if (ssrc == 0) {
2591 if (default_recv_ssrc_ != -1) {
kwiberg686a8ef2016-02-26 03:00:35 -08002592 std::unique_ptr<webrtc::AudioSinkInterface> proxy_sink(
deadbeef884f5852016-01-15 09:20:04 -08002593 sink ? new ProxySink(sink.get()) : nullptr);
2594 SetRawAudioSink(default_recv_ssrc_, std::move(proxy_sink));
2595 }
2596 default_sink_ = std::move(sink);
2597 return;
2598 }
Tommif888bb52015-12-12 01:37:01 +01002599 const auto it = recv_streams_.find(ssrc);
2600 if (it == recv_streams_.end()) {
2601 LOG(LS_WARNING) << "SetRawAudioSink: no recv stream" << ssrc;
2602 return;
2603 }
deadbeef2d110be2016-01-13 12:00:26 -08002604 it->second->SetRawAudioSink(std::move(sink));
Tommif888bb52015-12-12 01:37:01 +01002605}
2606
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002607int WebRtcVoiceMediaChannel::GetOutputLevel(int channel) {
solenbergd97ec302015-10-07 01:40:33 -07002608 unsigned int ulevel = 0;
2609 int ret = engine()->voe()->volume()->GetSpeechOutputLevel(channel, ulevel);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002610 return (ret == 0) ? static_cast<int>(ulevel) : -1;
2611}
2612
Peter Boström0c4e06b2015-10-07 12:23:21 +02002613int WebRtcVoiceMediaChannel::GetReceiveChannelId(uint32_t ssrc) const {
solenberg566ef242015-11-06 15:34:49 -08002614 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
solenberg7add0582015-11-20 09:59:34 -08002615 const auto it = recv_streams_.find(ssrc);
2616 if (it != recv_streams_.end()) {
mallinath@webrtc.org67ee6b92014-02-03 16:57:16 +00002617 return it->second->channel();
solenberg8fb30c32015-10-13 03:06:58 -07002618 }
solenberg1ac56142015-10-13 03:58:19 -07002619 return -1;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002620}
2621
Peter Boström0c4e06b2015-10-07 12:23:21 +02002622int WebRtcVoiceMediaChannel::GetSendChannelId(uint32_t ssrc) const {
solenberg566ef242015-11-06 15:34:49 -08002623 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
solenbergc96df772015-10-21 13:01:53 -07002624 const auto it = send_streams_.find(ssrc);
2625 if (it != send_streams_.end()) {
mallinath@webrtc.org67ee6b92014-02-03 16:57:16 +00002626 return it->second->channel();
solenberg8fb30c32015-10-13 03:06:58 -07002627 }
wu@webrtc.org9dba5252013-08-05 20:36:57 +00002628 return -1;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002629}
2630
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002631bool WebRtcVoiceMediaChannel::SetPlayout(int channel, bool playout) {
2632 if (playout) {
2633 LOG(LS_INFO) << "Starting playout for channel #" << channel;
2634 if (engine()->voe()->base()->StartPlayout(channel) == -1) {
2635 LOG_RTCERR1(StartPlayout, channel);
2636 return false;
2637 }
2638 } else {
2639 LOG(LS_INFO) << "Stopping playout for channel #" << channel;
2640 engine()->voe()->base()->StopPlayout(channel);
2641 }
2642 return true;
2643}
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002644} // namespace cricket
2645
2646#endif // HAVE_WEBRTC_VOICE