blob: 95b08e71587e2aa970834b9c60b9fe41d9907d66 [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
solenbergff976312016-03-30 23:28:51 -0700523WebRtcVoiceEngine::WebRtcVoiceEngine(webrtc::AudioDeviceModule* adm)
524 : WebRtcVoiceEngine(adm, new VoEWrapper()) {
525 audio_state_ = webrtc::AudioState::Create(MakeAudioStateConfig(voe()));
solenberg26c8c912015-11-27 04:00:25 -0800526}
527
solenbergff976312016-03-30 23:28:51 -0700528WebRtcVoiceEngine::WebRtcVoiceEngine(webrtc::AudioDeviceModule* adm,
529 VoEWrapper* voe_wrapper)
530 : adm_(adm), voe_wrapper_(voe_wrapper) {
solenberg26c8c912015-11-27 04:00:25 -0800531 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
solenbergff976312016-03-30 23:28:51 -0700532 LOG(LS_INFO) << "WebRtcVoiceEngine::WebRtcVoiceEngine";
533 RTC_DCHECK(voe_wrapper);
solenberg26c8c912015-11-27 04:00:25 -0800534
535 signal_thread_checker_.DetachFromThread();
solenberg26c8c912015-11-27 04:00:25 -0800536
537 // Load our audio codec list.
solenbergff976312016-03-30 23:28:51 -0700538 LOG(LS_INFO) << "Supported codecs in order of preference:";
solenberg26c8c912015-11-27 04:00:25 -0800539 codecs_ = WebRtcVoiceCodecs::SupportedCodecs();
solenbergff976312016-03-30 23:28:51 -0700540 for (const AudioCodec& codec : codecs_) {
541 LOG(LS_INFO) << ToString(codec);
buildbot@webrtc.org13d67762014-05-02 17:33:29 +0000542 }
buildbot@webrtc.org13d67762014-05-02 17:33:29 +0000543
solenbergff976312016-03-30 23:28:51 -0700544 voe_config_.Set<webrtc::VoicePacing>(new webrtc::VoicePacing(true));
buildbot@webrtc.org13d67762014-05-02 17:33:29 +0000545
solenbergff976312016-03-30 23:28:51 -0700546 // Temporarily turn logging level up for the Init() call.
547 webrtc::Trace::SetTraceCallback(this);
solenbergbd138382015-11-20 16:08:07 -0800548 webrtc::Trace::set_level_filter(kElevatedTraceFilter);
solenberg2515af22015-12-02 06:19:36 -0800549 LOG(LS_INFO) << webrtc::VoiceEngine::GetVersionString();
solenbergff976312016-03-30 23:28:51 -0700550 RTC_CHECK_EQ(0, voe_wrapper_->base()->Init(adm_.get()));
solenbergbd138382015-11-20 16:08:07 -0800551 webrtc::Trace::set_level_filter(kDefaultTraceFilter);
buildbot@webrtc.org13d67762014-05-02 17:33:29 +0000552
solenbergff976312016-03-30 23:28:51 -0700553 // No ADM supplied? Get the default one from VoE.
554 if (!adm_) {
555 adm_ = voe_wrapper_->base()->audio_device_module();
556 }
557 RTC_DCHECK(adm_);
558
buildbot@webrtc.org13d67762014-05-02 17:33:29 +0000559 // Save the default AGC configuration settings. This must happen before
solenberg246b8172015-12-08 09:50:23 -0800560 // calling ApplyOptions or the default will be overwritten.
solenbergff976312016-03-30 23:28:51 -0700561 int error = voe_wrapper_->processing()->GetAgcConfig(default_agc_config_);
562 RTC_DCHECK_EQ(0, error);
buildbot@webrtc.org13d67762014-05-02 17:33:29 +0000563
solenberg0f7d2932016-01-15 01:40:39 -0800564 // Set default engine options.
565 {
566 AudioOptions options;
567 options.echo_cancellation = rtc::Optional<bool>(true);
568 options.auto_gain_control = rtc::Optional<bool>(true);
569 options.noise_suppression = rtc::Optional<bool>(true);
570 options.highpass_filter = rtc::Optional<bool>(true);
571 options.stereo_swapping = rtc::Optional<bool>(false);
572 options.audio_jitter_buffer_max_packets = rtc::Optional<int>(50);
573 options.audio_jitter_buffer_fast_accelerate = rtc::Optional<bool>(false);
574 options.typing_detection = rtc::Optional<bool>(true);
575 options.adjust_agc_delta = rtc::Optional<int>(0);
576 options.experimental_agc = rtc::Optional<bool>(false);
577 options.extended_filter_aec = rtc::Optional<bool>(false);
578 options.delay_agnostic_aec = rtc::Optional<bool>(false);
579 options.experimental_ns = rtc::Optional<bool>(false);
Alejandro Luebsc9b0c262016-05-16 15:32:38 -0700580 options.intelligibility_enhancer = rtc::Optional<bool>(false);
solenbergff976312016-03-30 23:28:51 -0700581 bool error = ApplyOptions(options);
582 RTC_DCHECK(error);
buildbot@webrtc.org13d67762014-05-02 17:33:29 +0000583 }
584
solenberg246b8172015-12-08 09:50:23 -0800585 SetDefaultDevices();
buildbot@webrtc.org13d67762014-05-02 17:33:29 +0000586}
587
solenbergff976312016-03-30 23:28:51 -0700588WebRtcVoiceEngine::~WebRtcVoiceEngine() {
solenberg566ef242015-11-06 15:34:49 -0800589 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
solenbergff976312016-03-30 23:28:51 -0700590 LOG(LS_INFO) << "WebRtcVoiceEngine::~WebRtcVoiceEngine";
buildbot@webrtc.org13d67762014-05-02 17:33:29 +0000591 StopAecDump();
buildbot@webrtc.org13d67762014-05-02 17:33:29 +0000592 voe_wrapper_->base()->Terminate();
solenbergff976312016-03-30 23:28:51 -0700593 webrtc::Trace::SetTraceCallback(nullptr);
buildbot@webrtc.org13d67762014-05-02 17:33:29 +0000594}
595
solenberg566ef242015-11-06 15:34:49 -0800596rtc::scoped_refptr<webrtc::AudioState>
597 WebRtcVoiceEngine::GetAudioState() const {
598 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
599 return audio_state_;
600}
601
nisse51542be2016-02-12 02:27:06 -0800602VoiceMediaChannel* WebRtcVoiceEngine::CreateChannel(
603 webrtc::Call* call,
604 const MediaConfig& config,
Jelena Marusicc28a8962015-05-29 15:05:44 +0200605 const AudioOptions& options) {
solenberg566ef242015-11-06 15:34:49 -0800606 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
nisse51542be2016-02-12 02:27:06 -0800607 return new WebRtcVoiceMediaChannel(this, config, options, call);
buildbot@webrtc.org13d67762014-05-02 17:33:29 +0000608}
609
buildbot@webrtc.org13d67762014-05-02 17:33:29 +0000610bool WebRtcVoiceEngine::ApplyOptions(const AudioOptions& options_in) {
solenberg566ef242015-11-06 15:34:49 -0800611 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
solenbergff976312016-03-30 23:28:51 -0700612 LOG(LS_INFO) << "WebRtcVoiceEngine::ApplyOptions: " << options_in.ToString();
solenberg0f7d2932016-01-15 01:40:39 -0800613 AudioOptions options = options_in; // The options are modified below.
solenberg246b8172015-12-08 09:50:23 -0800614
buildbot@webrtc.org13d67762014-05-02 17:33:29 +0000615 // kEcConference is AEC with high suppression.
616 webrtc::EcModes ec_mode = webrtc::kEcConference;
617 webrtc::AecmModes aecm_mode = webrtc::kAecmSpeakerphone;
618 webrtc::AgcModes agc_mode = webrtc::kAgcAdaptiveAnalog;
619 webrtc::NsModes ns_mode = webrtc::kNsHighSuppression;
kwiberg102c6a62015-10-30 02:47:38 -0700620 if (options.aecm_generate_comfort_noise) {
buildbot@webrtc.org13d67762014-05-02 17:33:29 +0000621 LOG(LS_VERBOSE) << "Comfort noise explicitly set to "
kwiberg102c6a62015-10-30 02:47:38 -0700622 << *options.aecm_generate_comfort_noise
623 << " (default is false).";
buildbot@webrtc.org13d67762014-05-02 17:33:29 +0000624 }
625
kjellanderfcfc8042016-01-14 11:01:09 -0800626#if defined(WEBRTC_IOS)
buildbot@webrtc.org13d67762014-05-02 17:33:29 +0000627 // On iOS, VPIO provides built-in EC and AGC.
Karl Wibergbe579832015-11-10 22:34:18 +0100628 options.echo_cancellation = rtc::Optional<bool>(false);
629 options.auto_gain_control = rtc::Optional<bool>(false);
henrika86d907c2015-09-07 16:09:50 +0200630 LOG(LS_INFO) << "Always disable AEC and AGC on iOS. Use built-in instead.";
buildbot@webrtc.org13d67762014-05-02 17:33:29 +0000631#elif defined(ANDROID)
632 ec_mode = webrtc::kEcAecm;
633#endif
634
kjellanderfcfc8042016-01-14 11:01:09 -0800635#if defined(WEBRTC_IOS) || defined(ANDROID)
buildbot@webrtc.org13d67762014-05-02 17:33:29 +0000636 // Set the AGC mode for iOS as well despite disabling it above, to avoid
637 // unsupported configuration errors from webrtc.
638 agc_mode = webrtc::kAgcFixedDigital;
Karl Wibergbe579832015-11-10 22:34:18 +0100639 options.typing_detection = rtc::Optional<bool>(false);
640 options.experimental_agc = rtc::Optional<bool>(false);
641 options.extended_filter_aec = rtc::Optional<bool>(false);
642 options.experimental_ns = rtc::Optional<bool>(false);
buildbot@webrtc.org13d67762014-05-02 17:33:29 +0000643#endif
644
Bjorn Volckerbf395c12015-03-25 22:45:56 +0100645 // Delay Agnostic AEC automatically turns on EC if not set except on iOS
646 // where the feature is not supported.
647 bool use_delay_agnostic_aec = false;
kjellanderfcfc8042016-01-14 11:01:09 -0800648#if !defined(WEBRTC_IOS)
kwiberg102c6a62015-10-30 02:47:38 -0700649 if (options.delay_agnostic_aec) {
650 use_delay_agnostic_aec = *options.delay_agnostic_aec;
Bjorn Volckerbf395c12015-03-25 22:45:56 +0100651 if (use_delay_agnostic_aec) {
Karl Wibergbe579832015-11-10 22:34:18 +0100652 options.echo_cancellation = rtc::Optional<bool>(true);
653 options.extended_filter_aec = rtc::Optional<bool>(true);
Bjorn Volckerbf395c12015-03-25 22:45:56 +0100654 ec_mode = webrtc::kEcConference;
655 }
656 }
657#endif
658
buildbot@webrtc.org13d67762014-05-02 17:33:29 +0000659 webrtc::VoEAudioProcessing* voep = voe_wrapper_->processing();
660
kwiberg102c6a62015-10-30 02:47:38 -0700661 if (options.echo_cancellation) {
henrika@webrtc.orga954c072014-12-09 16:22:09 +0000662 // Check if platform supports built-in EC. Currently only supported on
663 // Android and in combination with Java based audio layer.
664 // TODO(henrika): investigate possibility to support built-in EC also
665 // in combination with Open SL ES audio.
solenberg5b5129a2016-04-08 05:35:48 -0700666 const bool built_in_aec = adm()->BuiltInAECIsAvailable();
Bjorn Volcker73f72102015-06-03 14:50:15 +0200667 if (built_in_aec) {
Bjorn Volckerccfc9392015-05-07 07:43:17 +0200668 // Built-in EC exists on this device and use_delay_agnostic_aec is not
669 // overriding it. Enable/Disable it according to the echo_cancellation
670 // audio option.
Bjorn Volcker73f72102015-06-03 14:50:15 +0200671 const bool enable_built_in_aec =
kwiberg102c6a62015-10-30 02:47:38 -0700672 *options.echo_cancellation && !use_delay_agnostic_aec;
solenberg5b5129a2016-04-08 05:35:48 -0700673 if (adm()->EnableBuiltInAEC(enable_built_in_aec) == 0 &&
Bjorn Volcker73f72102015-06-03 14:50:15 +0200674 enable_built_in_aec) {
Bjorn Volckerbf395c12015-03-25 22:45:56 +0100675 // Disable internal software EC if built-in EC is enabled,
henrika@webrtc.orga954c072014-12-09 16:22:09 +0000676 // i.e., replace the software EC with the built-in EC.
Karl Wibergbe579832015-11-10 22:34:18 +0100677 options.echo_cancellation = rtc::Optional<bool>(false);
henrika@webrtc.orga954c072014-12-09 16:22:09 +0000678 LOG(LS_INFO) << "Disabling EC since built-in EC will be used instead";
679 }
680 }
kwiberg102c6a62015-10-30 02:47:38 -0700681 if (voep->SetEcStatus(*options.echo_cancellation, ec_mode) == -1) {
682 LOG_RTCERR2(SetEcStatus, *options.echo_cancellation, ec_mode);
buildbot@webrtc.org13d67762014-05-02 17:33:29 +0000683 return false;
684 } else {
kwiberg102c6a62015-10-30 02:47:38 -0700685 LOG(LS_INFO) << "Echo control set to " << *options.echo_cancellation
henrika86d907c2015-09-07 16:09:50 +0200686 << " with mode " << ec_mode;
buildbot@webrtc.org13d67762014-05-02 17:33:29 +0000687 }
688#if !defined(ANDROID)
689 // TODO(ajm): Remove the error return on Android from webrtc.
kwiberg102c6a62015-10-30 02:47:38 -0700690 if (voep->SetEcMetricsStatus(*options.echo_cancellation) == -1) {
691 LOG_RTCERR1(SetEcMetricsStatus, *options.echo_cancellation);
buildbot@webrtc.org13d67762014-05-02 17:33:29 +0000692 return false;
693 }
694#endif
695 if (ec_mode == webrtc::kEcAecm) {
kwiberg102c6a62015-10-30 02:47:38 -0700696 bool cn = options.aecm_generate_comfort_noise.value_or(false);
697 if (voep->SetAecmMode(aecm_mode, cn) != 0) {
698 LOG_RTCERR2(SetAecmMode, aecm_mode, cn);
buildbot@webrtc.org13d67762014-05-02 17:33:29 +0000699 return false;
700 }
701 }
702 }
703
kwiberg102c6a62015-10-30 02:47:38 -0700704 if (options.auto_gain_control) {
solenberg5b5129a2016-04-08 05:35:48 -0700705 const bool built_in_agc = adm()->BuiltInAGCIsAvailable();
henrikac14f5ff2015-09-23 14:08:33 +0200706 if (built_in_agc) {
solenberg5b5129a2016-04-08 05:35:48 -0700707 if (adm()->EnableBuiltInAGC(*options.auto_gain_control) == 0 &&
kwiberg102c6a62015-10-30 02:47:38 -0700708 *options.auto_gain_control) {
henrikac14f5ff2015-09-23 14:08:33 +0200709 // Disable internal software AGC if built-in AGC is enabled,
710 // i.e., replace the software AGC with the built-in AGC.
Karl Wibergbe579832015-11-10 22:34:18 +0100711 options.auto_gain_control = rtc::Optional<bool>(false);
henrikac14f5ff2015-09-23 14:08:33 +0200712 LOG(LS_INFO) << "Disabling AGC since built-in AGC will be used instead";
713 }
714 }
kwiberg102c6a62015-10-30 02:47:38 -0700715 if (voep->SetAgcStatus(*options.auto_gain_control, agc_mode) == -1) {
716 LOG_RTCERR2(SetAgcStatus, *options.auto_gain_control, agc_mode);
buildbot@webrtc.org13d67762014-05-02 17:33:29 +0000717 return false;
718 } else {
kwiberg102c6a62015-10-30 02:47:38 -0700719 LOG(LS_INFO) << "Auto gain set to " << *options.auto_gain_control
720 << " with mode " << agc_mode;
buildbot@webrtc.org13d67762014-05-02 17:33:29 +0000721 }
722 }
723
kwiberg102c6a62015-10-30 02:47:38 -0700724 if (options.tx_agc_target_dbov || options.tx_agc_digital_compression_gain ||
725 options.tx_agc_limiter) {
buildbot@webrtc.org13d67762014-05-02 17:33:29 +0000726 // Override default_agc_config_. Generally, an unset option means "leave
727 // the VoE bits alone" in this function, so we want whatever is set to be
728 // stored as the new "default". If we didn't, then setting e.g.
729 // tx_agc_target_dbov would reset digital compression gain and limiter
730 // settings.
731 // Also, if we don't update default_agc_config_, then adjust_agc_delta
732 // would be an offset from the original values, and not whatever was set
733 // explicitly.
kwiberg102c6a62015-10-30 02:47:38 -0700734 default_agc_config_.targetLeveldBOv = options.tx_agc_target_dbov.value_or(
735 default_agc_config_.targetLeveldBOv);
buildbot@webrtc.org13d67762014-05-02 17:33:29 +0000736 default_agc_config_.digitalCompressionGaindB =
kwiberg102c6a62015-10-30 02:47:38 -0700737 options.tx_agc_digital_compression_gain.value_or(
buildbot@webrtc.org13d67762014-05-02 17:33:29 +0000738 default_agc_config_.digitalCompressionGaindB);
739 default_agc_config_.limiterEnable =
kwiberg102c6a62015-10-30 02:47:38 -0700740 options.tx_agc_limiter.value_or(default_agc_config_.limiterEnable);
buildbot@webrtc.org13d67762014-05-02 17:33:29 +0000741 if (voe_wrapper_->processing()->SetAgcConfig(default_agc_config_) == -1) {
742 LOG_RTCERR3(SetAgcConfig,
743 default_agc_config_.targetLeveldBOv,
744 default_agc_config_.digitalCompressionGaindB,
745 default_agc_config_.limiterEnable);
746 return false;
747 }
748 }
749
Alejandro Luebsc9b0c262016-05-16 15:32:38 -0700750 if (options.intelligibility_enhancer) {
751 intelligibility_enhancer_ = options.intelligibility_enhancer;
752 }
753 if (intelligibility_enhancer_ && *intelligibility_enhancer_) {
754 LOG(LS_INFO) << "Enabling NS when Intelligibility Enhancer is active.";
755 options.noise_suppression = intelligibility_enhancer_;
756 }
757
kwiberg102c6a62015-10-30 02:47:38 -0700758 if (options.noise_suppression) {
Alejandro Luebsc9b0c262016-05-16 15:32:38 -0700759 if (adm()->BuiltInNSIsAvailable()) {
760 bool builtin_ns =
761 *options.noise_suppression &&
762 !(intelligibility_enhancer_ && *intelligibility_enhancer_);
763 if (adm()->EnableBuiltInNS(builtin_ns) == 0 && builtin_ns) {
henrikac14f5ff2015-09-23 14:08:33 +0200764 // Disable internal software NS if built-in NS is enabled,
765 // i.e., replace the software NS with the built-in NS.
Karl Wibergbe579832015-11-10 22:34:18 +0100766 options.noise_suppression = rtc::Optional<bool>(false);
henrikac14f5ff2015-09-23 14:08:33 +0200767 LOG(LS_INFO) << "Disabling NS since built-in NS will be used instead";
768 }
769 }
kwiberg102c6a62015-10-30 02:47:38 -0700770 if (voep->SetNsStatus(*options.noise_suppression, ns_mode) == -1) {
771 LOG_RTCERR2(SetNsStatus, *options.noise_suppression, ns_mode);
buildbot@webrtc.org13d67762014-05-02 17:33:29 +0000772 return false;
773 } else {
kwiberg102c6a62015-10-30 02:47:38 -0700774 LOG(LS_INFO) << "Noise suppression set to " << *options.noise_suppression
henrikac14f5ff2015-09-23 14:08:33 +0200775 << " with mode " << ns_mode;
buildbot@webrtc.org13d67762014-05-02 17:33:29 +0000776 }
777 }
778
kwiberg102c6a62015-10-30 02:47:38 -0700779 if (options.highpass_filter) {
780 LOG(LS_INFO) << "High pass filter enabled? " << *options.highpass_filter;
781 if (voep->EnableHighPassFilter(*options.highpass_filter) == -1) {
782 LOG_RTCERR1(SetHighpassFilterStatus, *options.highpass_filter);
buildbot@webrtc.org13d67762014-05-02 17:33:29 +0000783 return false;
784 }
785 }
786
kwiberg102c6a62015-10-30 02:47:38 -0700787 if (options.stereo_swapping) {
788 LOG(LS_INFO) << "Stereo swapping enabled? " << *options.stereo_swapping;
789 voep->EnableStereoChannelSwapping(*options.stereo_swapping);
790 if (voep->IsStereoChannelSwappingEnabled() != *options.stereo_swapping) {
791 LOG_RTCERR1(EnableStereoChannelSwapping, *options.stereo_swapping);
buildbot@webrtc.org13d67762014-05-02 17:33:29 +0000792 return false;
793 }
794 }
795
kwiberg102c6a62015-10-30 02:47:38 -0700796 if (options.audio_jitter_buffer_max_packets) {
797 LOG(LS_INFO) << "NetEq capacity is "
798 << *options.audio_jitter_buffer_max_packets;
Henrik Lundin64dad832015-05-11 12:44:23 +0200799 voe_config_.Set<webrtc::NetEqCapacityConfig>(
kwiberg102c6a62015-10-30 02:47:38 -0700800 new webrtc::NetEqCapacityConfig(
801 *options.audio_jitter_buffer_max_packets));
Henrik Lundin64dad832015-05-11 12:44:23 +0200802 }
803
kwiberg102c6a62015-10-30 02:47:38 -0700804 if (options.audio_jitter_buffer_fast_accelerate) {
805 LOG(LS_INFO) << "NetEq fast mode? "
806 << *options.audio_jitter_buffer_fast_accelerate;
Henrik Lundin5263b3c2015-06-01 10:29:41 +0200807 voe_config_.Set<webrtc::NetEqFastAccelerate>(
kwiberg102c6a62015-10-30 02:47:38 -0700808 new webrtc::NetEqFastAccelerate(
809 *options.audio_jitter_buffer_fast_accelerate));
Henrik Lundin5263b3c2015-06-01 10:29:41 +0200810 }
811
kwiberg102c6a62015-10-30 02:47:38 -0700812 if (options.typing_detection) {
813 LOG(LS_INFO) << "Typing detection is enabled? "
814 << *options.typing_detection;
815 if (voep->SetTypingDetectionStatus(*options.typing_detection) == -1) {
buildbot@webrtc.org13d67762014-05-02 17:33:29 +0000816 // In case of error, log the info and continue
kwiberg102c6a62015-10-30 02:47:38 -0700817 LOG_RTCERR1(SetTypingDetectionStatus, *options.typing_detection);
buildbot@webrtc.org13d67762014-05-02 17:33:29 +0000818 }
819 }
820
kwiberg102c6a62015-10-30 02:47:38 -0700821 if (options.adjust_agc_delta) {
822 LOG(LS_INFO) << "Adjust agc delta is " << *options.adjust_agc_delta;
823 if (!AdjustAgcLevel(*options.adjust_agc_delta)) {
buildbot@webrtc.org13d67762014-05-02 17:33:29 +0000824 return false;
825 }
826 }
827
buildbot@webrtc.org1f8a2372014-08-28 10:52:44 +0000828 webrtc::Config config;
829
kwiberg102c6a62015-10-30 02:47:38 -0700830 if (options.delay_agnostic_aec)
831 delay_agnostic_aec_ = options.delay_agnostic_aec;
832 if (delay_agnostic_aec_) {
833 LOG(LS_INFO) << "Delay agnostic aec is enabled? " << *delay_agnostic_aec_;
henrik.lundin0f133b92015-07-02 00:17:55 -0700834 config.Set<webrtc::DelayAgnostic>(
kwiberg102c6a62015-10-30 02:47:38 -0700835 new webrtc::DelayAgnostic(*delay_agnostic_aec_));
Bjorn Volckerbf395c12015-03-25 22:45:56 +0100836 }
837
kwiberg102c6a62015-10-30 02:47:38 -0700838 if (options.extended_filter_aec) {
839 extended_filter_aec_ = options.extended_filter_aec;
840 }
841 if (extended_filter_aec_) {
842 LOG(LS_INFO) << "Extended filter aec is enabled? " << *extended_filter_aec_;
Henrik Lundin441f6342015-06-09 16:03:13 +0200843 config.Set<webrtc::ExtendedFilter>(
kwiberg102c6a62015-10-30 02:47:38 -0700844 new webrtc::ExtendedFilter(*extended_filter_aec_));
buildbot@webrtc.org1f8a2372014-08-28 10:52:44 +0000845 }
846
kwiberg102c6a62015-10-30 02:47:38 -0700847 if (options.experimental_ns) {
848 experimental_ns_ = options.experimental_ns;
849 }
850 if (experimental_ns_) {
851 LOG(LS_INFO) << "Experimental ns is enabled? " << *experimental_ns_;
buildbot@webrtc.org1f8a2372014-08-28 10:52:44 +0000852 config.Set<webrtc::ExperimentalNs>(
kwiberg102c6a62015-10-30 02:47:38 -0700853 new webrtc::ExperimentalNs(*experimental_ns_));
buildbot@webrtc.org1f8a2372014-08-28 10:52:44 +0000854 }
buildbot@webrtc.org1f8a2372014-08-28 10:52:44 +0000855
Alejandro Luebsc9b0c262016-05-16 15:32:38 -0700856 if (intelligibility_enhancer_) {
857 LOG(LS_INFO) << "Intelligibility Enhancer is enabled? "
858 << *intelligibility_enhancer_;
859 config.Set<webrtc::Intelligibility>(
860 new webrtc::Intelligibility(*intelligibility_enhancer_));
861 }
862
buildbot@webrtc.org1f8a2372014-08-28 10:52:44 +0000863 // We check audioproc for the benefit of tests, since FakeWebRtcVoiceEngine
864 // returns NULL on audio_processing().
865 webrtc::AudioProcessing* audioproc = voe_wrapper_->base()->audio_processing();
866 if (audioproc) {
867 audioproc->SetExtraOptions(config);
868 }
869
kwiberg102c6a62015-10-30 02:47:38 -0700870 if (options.recording_sample_rate) {
871 LOG(LS_INFO) << "Recording sample rate is "
872 << *options.recording_sample_rate;
solenberg5b5129a2016-04-08 05:35:48 -0700873 if (adm()->SetRecordingSampleRate(*options.recording_sample_rate)) {
kwiberg102c6a62015-10-30 02:47:38 -0700874 LOG_RTCERR1(SetRecordingSampleRate, *options.recording_sample_rate);
buildbot@webrtc.org13d67762014-05-02 17:33:29 +0000875 }
876 }
877
kwiberg102c6a62015-10-30 02:47:38 -0700878 if (options.playout_sample_rate) {
879 LOG(LS_INFO) << "Playout sample rate is " << *options.playout_sample_rate;
solenberg5b5129a2016-04-08 05:35:48 -0700880 if (adm()->SetPlayoutSampleRate(*options.playout_sample_rate)) {
kwiberg102c6a62015-10-30 02:47:38 -0700881 LOG_RTCERR1(SetPlayoutSampleRate, *options.playout_sample_rate);
buildbot@webrtc.org13d67762014-05-02 17:33:29 +0000882 }
883 }
884
885 return true;
886}
887
solenberg246b8172015-12-08 09:50:23 -0800888void WebRtcVoiceEngine::SetDefaultDevices() {
solenberg566ef242015-11-06 15:34:49 -0800889 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
kjellanderfcfc8042016-01-14 11:01:09 -0800890#if !defined(WEBRTC_IOS)
solenberg246b8172015-12-08 09:50:23 -0800891 int in_id = kDefaultAudioDeviceId;
892 int out_id = kDefaultAudioDeviceId;
893 LOG(LS_INFO) << "Setting microphone to (id=" << in_id
894 << ") and speaker to (id=" << out_id << ")";
buildbot@webrtc.org13d67762014-05-02 17:33:29 +0000895
solenbergc1a1b352015-09-22 13:31:20 -0700896 bool ret = true;
solenberg246b8172015-12-08 09:50:23 -0800897 if (voe_wrapper_->hw()->SetRecordingDevice(in_id) == -1) {
898 LOG_RTCERR1(SetRecordingDevice, in_id);
buildbot@webrtc.org13d67762014-05-02 17:33:29 +0000899 ret = false;
900 }
solenberg246b8172015-12-08 09:50:23 -0800901 webrtc::AudioProcessing* ap = voe()->base()->audio_processing();
902 if (ap) {
903 ap->Initialize();
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000904 }
905
solenberg246b8172015-12-08 09:50:23 -0800906 if (voe_wrapper_->hw()->SetPlayoutDevice(out_id) == -1) {
907 LOG_RTCERR1(SetPlayoutDevice, out_id);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000908 ret = false;
909 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000910
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000911 if (ret) {
solenberg246b8172015-12-08 09:50:23 -0800912 LOG(LS_INFO) << "Set microphone to (id=" << in_id
913 << ") and speaker to (id=" << out_id << ")";
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000914 }
kjellanderfcfc8042016-01-14 11:01:09 -0800915#endif // !WEBRTC_IOS
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000916}
917
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000918bool WebRtcVoiceEngine::GetOutputVolume(int* level) {
solenberg566ef242015-11-06 15:34:49 -0800919 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000920 unsigned int ulevel;
921 if (voe_wrapper_->volume()->GetSpeakerVolume(ulevel) == -1) {
922 LOG_RTCERR1(GetSpeakerVolume, level);
923 return false;
924 }
925 *level = ulevel;
926 return true;
927}
928
929bool WebRtcVoiceEngine::SetOutputVolume(int level) {
solenberg566ef242015-11-06 15:34:49 -0800930 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
henrikg91d6ede2015-09-17 00:24:34 -0700931 RTC_DCHECK(level >= 0 && level <= 255);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000932 if (voe_wrapper_->volume()->SetSpeakerVolume(level) == -1) {
933 LOG_RTCERR1(SetSpeakerVolume, level);
934 return false;
935 }
936 return true;
937}
938
939int WebRtcVoiceEngine::GetInputLevel() {
solenberg566ef242015-11-06 15:34:49 -0800940 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000941 unsigned int ulevel;
942 return (voe_wrapper_->volume()->GetSpeechInputLevel(ulevel) != -1) ?
943 static_cast<int>(ulevel) : -1;
944}
945
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000946const std::vector<AudioCodec>& WebRtcVoiceEngine::codecs() {
solenberg566ef242015-11-06 15:34:49 -0800947 RTC_DCHECK(signal_thread_checker_.CalledOnValidThread());
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000948 return codecs_;
949}
950
Stefan Holmer9d69c3f2015-12-07 10:45:43 +0100951RtpCapabilities WebRtcVoiceEngine::GetCapabilities() const {
solenberg566ef242015-11-06 15:34:49 -0800952 RTC_DCHECK(signal_thread_checker_.CalledOnValidThread());
Stefan Holmer9d69c3f2015-12-07 10:45:43 +0100953 RtpCapabilities capabilities;
Stefan Holmer9d69c3f2015-12-07 10:45:43 +0100954 capabilities.header_extensions.push_back(
isheriff6f8d6862016-05-26 11:24:55 -0700955 webrtc::RtpExtension(webrtc::RtpExtension::kAudioLevelUri,
956 webrtc::RtpExtension::kAudioLevelDefaultId));
957 capabilities.header_extensions.push_back(
958 webrtc::RtpExtension(webrtc::RtpExtension::kAbsSendTimeUri,
959 webrtc::RtpExtension::kAbsSendTimeDefaultId));
stefanba4c0e42016-02-04 04:12:24 -0800960 if (webrtc::field_trial::FindFullName("WebRTC-Audio-SendSideBwe") ==
961 "Enabled") {
isheriff6f8d6862016-05-26 11:24:55 -0700962 capabilities.header_extensions.push_back(webrtc::RtpExtension(
963 webrtc::RtpExtension::kTransportSequenceNumberUri,
964 webrtc::RtpExtension::kTransportSequenceNumberDefaultId));
stefanba4c0e42016-02-04 04:12:24 -0800965 }
Stefan Holmer9d69c3f2015-12-07 10:45:43 +0100966 return capabilities;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000967}
968
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000969int WebRtcVoiceEngine::GetLastEngineError() {
solenberg566ef242015-11-06 15:34:49 -0800970 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000971 return voe_wrapper_->error();
972}
973
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000974void WebRtcVoiceEngine::Print(webrtc::TraceLevel level, const char* trace,
975 int length) {
solenberg566ef242015-11-06 15:34:49 -0800976 // Note: This callback can happen on any thread!
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000977 rtc::LoggingSeverity sev = rtc::LS_VERBOSE;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000978 if (level == webrtc::kTraceError || level == webrtc::kTraceCritical)
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000979 sev = rtc::LS_ERROR;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000980 else if (level == webrtc::kTraceWarning)
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000981 sev = rtc::LS_WARNING;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000982 else if (level == webrtc::kTraceStateInfo || level == webrtc::kTraceInfo)
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000983 sev = rtc::LS_INFO;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000984 else if (level == webrtc::kTraceTerseInfo)
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000985 sev = rtc::LS_INFO;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000986
solenberg72e29d22016-03-08 06:35:16 -0800987 // Skip past boilerplate prefix text.
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000988 if (length < 72) {
989 std::string msg(trace, length);
990 LOG(LS_ERROR) << "Malformed webrtc log message: ";
991 LOG_V(sev) << msg;
992 } else {
993 std::string msg(trace + 71, length - 72);
Peter Boströmd5c75b12015-09-23 13:24:32 +0200994 LOG_V(sev) << "webrtc: " << msg;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000995 }
996}
997
solenberg63b34542015-09-29 06:06:31 -0700998void WebRtcVoiceEngine::RegisterChannel(WebRtcVoiceMediaChannel* channel) {
solenberg566ef242015-11-06 15:34:49 -0800999 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
1000 RTC_DCHECK(channel);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001001 channels_.push_back(channel);
1002}
1003
solenberg63b34542015-09-29 06:06:31 -07001004void WebRtcVoiceEngine::UnregisterChannel(WebRtcVoiceMediaChannel* channel) {
solenberg566ef242015-11-06 15:34:49 -08001005 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
solenberg63b34542015-09-29 06:06:31 -07001006 auto it = std::find(channels_.begin(), channels_.end(), channel);
solenberg566ef242015-11-06 15:34:49 -08001007 RTC_DCHECK(it != channels_.end());
1008 channels_.erase(it);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001009}
1010
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001011// Adjusts the default AGC target level by the specified delta.
1012// NB: If we start messing with other config fields, we'll want
1013// to save the current webrtc::AgcConfig as well.
1014bool WebRtcVoiceEngine::AdjustAgcLevel(int delta) {
solenberg566ef242015-11-06 15:34:49 -08001015 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001016 webrtc::AgcConfig config = default_agc_config_;
1017 config.targetLeveldBOv -= delta;
1018
1019 LOG(LS_INFO) << "Adjusting AGC level from default -"
1020 << default_agc_config_.targetLeveldBOv << "dB to -"
1021 << config.targetLeveldBOv << "dB";
1022
1023 if (voe_wrapper_->processing()->SetAgcConfig(config) == -1) {
1024 LOG_RTCERR1(SetAgcConfig, config.targetLeveldBOv);
1025 return false;
1026 }
1027 return true;
1028}
1029
ivocd66b44d2016-01-15 03:06:36 -08001030bool WebRtcVoiceEngine::StartAecDump(rtc::PlatformFile file,
1031 int64_t max_size_bytes) {
solenberg566ef242015-11-06 15:34:49 -08001032 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00001033 FILE* aec_dump_file_stream = rtc::FdopenPlatformFileForWriting(file);
wu@webrtc.orga8910d22014-01-23 22:12:45 +00001034 if (!aec_dump_file_stream) {
1035 LOG(LS_ERROR) << "Could not open AEC dump file stream.";
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00001036 if (!rtc::ClosePlatformFile(file))
wu@webrtc.orga8910d22014-01-23 22:12:45 +00001037 LOG(LS_WARNING) << "Could not close file.";
1038 return false;
1039 }
wu@webrtc.orga9890802013-12-13 00:21:03 +00001040 StopAecDump();
ivocd66b44d2016-01-15 03:06:36 -08001041 if (voe_wrapper_->base()->audio_processing()->StartDebugRecording(
1042 aec_dump_file_stream, max_size_bytes) !=
wu@webrtc.orga9890802013-12-13 00:21:03 +00001043 webrtc::AudioProcessing::kNoError) {
wu@webrtc.orga8910d22014-01-23 22:12:45 +00001044 LOG_RTCERR0(StartDebugRecording);
1045 fclose(aec_dump_file_stream);
wu@webrtc.orga9890802013-12-13 00:21:03 +00001046 return false;
1047 }
1048 is_dumping_aec_ = true;
1049 return true;
wu@webrtc.orga9890802013-12-13 00:21:03 +00001050}
1051
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001052void WebRtcVoiceEngine::StartAecDump(const std::string& filename) {
solenberg566ef242015-11-06 15:34:49 -08001053 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001054 if (!is_dumping_aec_) {
1055 // Start dumping AEC when we are not dumping.
ivocd66b44d2016-01-15 03:06:36 -08001056 if (voe_wrapper_->base()->audio_processing()->StartDebugRecording(
1057 filename.c_str(), -1) != webrtc::AudioProcessing::kNoError) {
wu@webrtc.orga9890802013-12-13 00:21:03 +00001058 LOG_RTCERR1(StartDebugRecording, filename.c_str());
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001059 } else {
1060 is_dumping_aec_ = true;
1061 }
1062 }
1063}
1064
1065void WebRtcVoiceEngine::StopAecDump() {
solenberg566ef242015-11-06 15:34:49 -08001066 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001067 if (is_dumping_aec_) {
1068 // Stop dumping AEC when we are dumping.
ivocd66b44d2016-01-15 03:06:36 -08001069 if (voe_wrapper_->base()->audio_processing()->StopDebugRecording() !=
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001070 webrtc::AudioProcessing::kNoError) {
1071 LOG_RTCERR0(StopDebugRecording);
1072 }
1073 is_dumping_aec_ = false;
1074 }
1075}
1076
ivocc1513ee2016-05-13 08:30:39 -07001077bool WebRtcVoiceEngine::StartRtcEventLog(rtc::PlatformFile file,
1078 int64_t max_size_bytes) {
solenberg566ef242015-11-06 15:34:49 -08001079 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
ivoc20834ca2016-02-04 06:33:37 -08001080 webrtc::RtcEventLog* event_log = voe_wrapper_->codec()->GetEventLog();
1081 if (event_log) {
ivocc1513ee2016-05-13 08:30:39 -07001082 return event_log->StartLogging(file, max_size_bytes);
ivoc20834ca2016-02-04 06:33:37 -08001083 }
1084 LOG_RTCERR0(StartRtcEventLog);
1085 return false;
ivoc112a3d82015-10-16 02:22:18 -07001086}
1087
1088void WebRtcVoiceEngine::StopRtcEventLog() {
solenberg566ef242015-11-06 15:34:49 -08001089 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
ivoc20834ca2016-02-04 06:33:37 -08001090 webrtc::RtcEventLog* event_log = voe_wrapper_->codec()->GetEventLog();
1091 if (event_log) {
1092 event_log->StopLogging();
1093 return;
1094 }
1095 LOG_RTCERR0(StopRtcEventLog);
ivoc112a3d82015-10-16 02:22:18 -07001096}
1097
solenberg0a617e22015-10-20 15:49:38 -07001098int WebRtcVoiceEngine::CreateVoEChannel() {
solenberg566ef242015-11-06 15:34:49 -08001099 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
solenberg0a617e22015-10-20 15:49:38 -07001100 return voe_wrapper_->base()->CreateChannel(voe_config_);
sergeyu@chromium.org5bc25c42013-12-05 00:24:06 +00001101}
1102
solenberg5b5129a2016-04-08 05:35:48 -07001103webrtc::AudioDeviceModule* WebRtcVoiceEngine::adm() {
1104 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
1105 RTC_DCHECK(adm_);
1106 return adm_;
1107}
1108
solenbergc96df772015-10-21 13:01:53 -07001109class WebRtcVoiceMediaChannel::WebRtcAudioSendStream
Taylor Brandstetter1a018dc2016-03-08 12:37:39 -08001110 : public AudioSource::Sink {
mallinath@webrtc.org67ee6b92014-02-03 16:57:16 +00001111 public:
skvlade0d46372016-04-07 22:59:22 -07001112 WebRtcAudioSendStream(int ch,
1113 webrtc::AudioTransport* voe_audio_transport,
1114 uint32_t ssrc,
1115 const std::string& c_name,
solenberg3a941542015-11-16 07:34:50 -08001116 const std::vector<webrtc::RtpExtension>& extensions,
mflodman3d7db262016-04-29 00:57:13 -07001117 webrtc::Call* call,
1118 webrtc::Transport* send_transport)
solenberg7add0582015-11-20 09:59:34 -08001119 : voe_audio_transport_(voe_audio_transport),
solenberg3a941542015-11-16 07:34:50 -08001120 call_(call),
mflodman3d7db262016-04-29 00:57:13 -07001121 config_(send_transport),
skvlade0d46372016-04-07 22:59:22 -07001122 rtp_parameters_(CreateRtpParametersWithOneEncoding()) {
solenberg85a04962015-10-27 03:35:21 -07001123 RTC_DCHECK_GE(ch, 0);
1124 // TODO(solenberg): Once we're not using FakeWebRtcVoiceEngine anymore:
1125 // RTC_DCHECK(voe_audio_transport);
solenbergc96df772015-10-21 13:01:53 -07001126 RTC_DCHECK(call);
solenberg85a04962015-10-27 03:35:21 -07001127 audio_capture_thread_checker_.DetachFromThread();
solenberg3a941542015-11-16 07:34:50 -08001128 config_.rtp.ssrc = ssrc;
1129 config_.rtp.c_name = c_name;
1130 config_.voe_channel_id = ch;
1131 RecreateAudioSendStream(extensions);
solenbergc96df772015-10-21 13:01:53 -07001132 }
solenberg3a941542015-11-16 07:34:50 -08001133
solenbergc96df772015-10-21 13:01:53 -07001134 ~WebRtcAudioSendStream() override {
solenberg566ef242015-11-06 15:34:49 -08001135 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
Taylor Brandstetter1a018dc2016-03-08 12:37:39 -08001136 ClearSource();
solenbergc96df772015-10-21 13:01:53 -07001137 call_->DestroyAudioSendStream(stream_);
1138 }
henrike@webrtc.org1e09a712013-07-26 19:17:59 +00001139
solenberg3a941542015-11-16 07:34:50 -08001140 void RecreateAudioSendStream(
1141 const std::vector<webrtc::RtpExtension>& extensions) {
1142 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
1143 if (stream_) {
1144 call_->DestroyAudioSendStream(stream_);
1145 stream_ = nullptr;
1146 }
1147 config_.rtp.extensions = extensions;
1148 RTC_DCHECK(!stream_);
1149 stream_ = call_->CreateAudioSendStream(config_);
1150 RTC_CHECK(stream_);
solenberg6d6e7c52016-04-13 09:07:30 -07001151 UpdateSendState();
solenberg3a941542015-11-16 07:34:50 -08001152 }
1153
solenberg8842c3e2016-03-11 03:06:41 -08001154 bool SendTelephoneEvent(int payload_type, int event, int duration_ms) {
Fredrik Solenbergb5727682015-12-04 15:22:19 +01001155 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
1156 RTC_DCHECK(stream_);
1157 return stream_->SendTelephoneEvent(payload_type, event, duration_ms);
1158 }
1159
Taylor Brandstetter1a018dc2016-03-08 12:37:39 -08001160 void SetSend(bool send) {
1161 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
1162 send_ = send;
1163 UpdateSendState();
1164 }
1165
solenberg3a941542015-11-16 07:34:50 -08001166 webrtc::AudioSendStream::Stats GetStats() const {
1167 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
1168 RTC_DCHECK(stream_);
1169 return stream_->GetStats();
1170 }
1171
Taylor Brandstetter1a018dc2016-03-08 12:37:39 -08001172 // Starts the sending by setting ourselves as a sink to the AudioSource to
1173 // get data callbacks.
henrike@webrtc.orga7b98182014-02-21 15:51:43 +00001174 // This method is called on the libjingle worker thread.
mallinath@webrtc.org67ee6b92014-02-03 16:57:16 +00001175 // TODO(xians): Make sure Start() is called only once.
Taylor Brandstetter1a018dc2016-03-08 12:37:39 -08001176 void SetSource(AudioSource* source) {
solenberg566ef242015-11-06 15:34:49 -08001177 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
Taylor Brandstetter1a018dc2016-03-08 12:37:39 -08001178 RTC_DCHECK(source);
1179 if (source_) {
1180 RTC_DCHECK(source_ == source);
mallinath@webrtc.org67ee6b92014-02-03 16:57:16 +00001181 return;
1182 }
Taylor Brandstetter1a018dc2016-03-08 12:37:39 -08001183 source->SetSink(this);
1184 source_ = source;
1185 UpdateSendState();
mallinath@webrtc.org67ee6b92014-02-03 16:57:16 +00001186 }
1187
Taylor Brandstetter1a018dc2016-03-08 12:37:39 -08001188 // Stops sending by setting the sink of the AudioSource to nullptr. No data
mallinath@webrtc.org67ee6b92014-02-03 16:57:16 +00001189 // callback will be received after this method.
henrike@webrtc.orga7b98182014-02-21 15:51:43 +00001190 // This method is called on the libjingle worker thread.
Taylor Brandstetter1a018dc2016-03-08 12:37:39 -08001191 void ClearSource() {
solenberg566ef242015-11-06 15:34:49 -08001192 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
Taylor Brandstetter1a018dc2016-03-08 12:37:39 -08001193 if (source_) {
1194 source_->SetSink(nullptr);
1195 source_ = nullptr;
solenberg98c68862015-10-09 03:27:14 -07001196 }
Taylor Brandstetter1a018dc2016-03-08 12:37:39 -08001197 UpdateSendState();
mallinath@webrtc.org67ee6b92014-02-03 16:57:16 +00001198 }
1199
Taylor Brandstetter1a018dc2016-03-08 12:37:39 -08001200 // AudioSource::Sink implementation.
henrike@webrtc.orga7b98182014-02-21 15:51:43 +00001201 // This method is called on the audio thread.
kjellander@webrtc.org14665ff2015-03-04 12:58:35 +00001202 void OnData(const void* audio_data,
1203 int bits_per_sample,
1204 int sample_rate,
Peter Kasting69558702016-01-12 16:26:35 -08001205 size_t number_of_channels,
Peter Kastingdce40cf2015-08-24 14:52:23 -07001206 size_t number_of_frames) override {
solenberg566ef242015-11-06 15:34:49 -08001207 RTC_DCHECK(!worker_thread_checker_.CalledOnValidThread());
solenberg85a04962015-10-27 03:35:21 -07001208 RTC_DCHECK(audio_capture_thread_checker_.CalledOnValidThread());
solenbergc96df772015-10-21 13:01:53 -07001209 RTC_DCHECK(voe_audio_transport_);
solenberg7add0582015-11-20 09:59:34 -08001210 voe_audio_transport_->OnData(config_.voe_channel_id,
henrike@webrtc.orga7b98182014-02-21 15:51:43 +00001211 audio_data,
1212 bits_per_sample,
1213 sample_rate,
1214 number_of_channels,
1215 number_of_frames);
henrike@webrtc.orga7b98182014-02-21 15:51:43 +00001216 }
1217
Taylor Brandstetter1a018dc2016-03-08 12:37:39 -08001218 // Callback from the |source_| when it is going away. In case Start() has
henrike@webrtc.orga7b98182014-02-21 15:51:43 +00001219 // never been called, this callback won't be triggered.
kjellander@webrtc.org14665ff2015-03-04 12:58:35 +00001220 void OnClose() override {
solenberg566ef242015-11-06 15:34:49 -08001221 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
Taylor Brandstetter1a018dc2016-03-08 12:37:39 -08001222 // Set |source_| to nullptr to make sure no more callback will get into
1223 // the source.
1224 source_ = nullptr;
1225 UpdateSendState();
mallinath@webrtc.org67ee6b92014-02-03 16:57:16 +00001226 }
1227
1228 // Accessor to the VoE channel ID.
solenberg85a04962015-10-27 03:35:21 -07001229 int channel() const {
solenberg566ef242015-11-06 15:34:49 -08001230 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
solenberg7add0582015-11-20 09:59:34 -08001231 return config_.voe_channel_id;
solenberg85a04962015-10-27 03:35:21 -07001232 }
mallinath@webrtc.org67ee6b92014-02-03 16:57:16 +00001233
skvlade0d46372016-04-07 22:59:22 -07001234 const webrtc::RtpParameters& rtp_parameters() const {
1235 return rtp_parameters_;
1236 }
1237
Taylor Brandstetter55dd7082016-05-03 13:50:11 -07001238 void SetRtpParameters(const webrtc::RtpParameters& parameters) {
skvlade0d46372016-04-07 22:59:22 -07001239 RTC_CHECK_EQ(1UL, parameters.encodings.size());
1240 rtp_parameters_ = parameters;
Taylor Brandstetter55dd7082016-05-03 13:50:11 -07001241 // parameters.encodings[0].active could have changed.
1242 UpdateSendState();
skvlade0d46372016-04-07 22:59:22 -07001243 }
1244
mallinath@webrtc.org67ee6b92014-02-03 16:57:16 +00001245 private:
Taylor Brandstetter1a018dc2016-03-08 12:37:39 -08001246 void UpdateSendState() {
1247 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
1248 RTC_DCHECK(stream_);
Taylor Brandstetter55dd7082016-05-03 13:50:11 -07001249 RTC_DCHECK_EQ(1UL, rtp_parameters_.encodings.size());
1250 if (send_ && source_ != nullptr && rtp_parameters_.encodings[0].active) {
Taylor Brandstetter1a018dc2016-03-08 12:37:39 -08001251 stream_->Start();
1252 } else { // !send || source_ = nullptr
1253 stream_->Stop();
1254 }
1255 }
1256
solenberg566ef242015-11-06 15:34:49 -08001257 rtc::ThreadChecker worker_thread_checker_;
solenberg85a04962015-10-27 03:35:21 -07001258 rtc::ThreadChecker audio_capture_thread_checker_;
solenbergc96df772015-10-21 13:01:53 -07001259 webrtc::AudioTransport* const voe_audio_transport_ = nullptr;
1260 webrtc::Call* call_ = nullptr;
solenberg3a941542015-11-16 07:34:50 -08001261 webrtc::AudioSendStream::Config config_;
1262 // The stream is owned by WebRtcAudioSendStream and may be reallocated if
1263 // configuration changes.
solenbergc96df772015-10-21 13:01:53 -07001264 webrtc::AudioSendStream* stream_ = nullptr;
mallinath@webrtc.org67ee6b92014-02-03 16:57:16 +00001265
Taylor Brandstetter1a018dc2016-03-08 12:37:39 -08001266 // Raw pointer to AudioSource owned by LocalAudioTrackHandler.
mallinath@webrtc.org67ee6b92014-02-03 16:57:16 +00001267 // PeerConnection will make sure invalidating the pointer before the object
1268 // goes away.
Taylor Brandstetter1a018dc2016-03-08 12:37:39 -08001269 AudioSource* source_ = nullptr;
1270 bool send_ = false;
skvlade0d46372016-04-07 22:59:22 -07001271 webrtc::RtpParameters rtp_parameters_;
henrike@webrtc.orga7b98182014-02-21 15:51:43 +00001272
solenbergc96df772015-10-21 13:01:53 -07001273 RTC_DISALLOW_IMPLICIT_CONSTRUCTORS(WebRtcAudioSendStream);
1274};
1275
1276class WebRtcVoiceMediaChannel::WebRtcAudioReceiveStream {
1277 public:
stefanba4c0e42016-02-04 04:12:24 -08001278 WebRtcAudioReceiveStream(int ch,
1279 uint32_t remote_ssrc,
1280 uint32_t local_ssrc,
1281 bool use_transport_cc,
1282 const std::string& sync_group,
solenberg7add0582015-11-20 09:59:34 -08001283 const std::vector<webrtc::RtpExtension>& extensions,
solenberg31fec402016-05-06 02:13:12 -07001284 webrtc::Call* call,
1285 webrtc::Transport* rtcp_send_transport)
stefanba4c0e42016-02-04 04:12:24 -08001286 : call_(call), config_() {
solenberg7add0582015-11-20 09:59:34 -08001287 RTC_DCHECK_GE(ch, 0);
1288 RTC_DCHECK(call);
1289 config_.rtp.remote_ssrc = remote_ssrc;
1290 config_.rtp.local_ssrc = local_ssrc;
solenberg31fec402016-05-06 02:13:12 -07001291 config_.rtcp_send_transport = rtcp_send_transport;
solenberg7add0582015-11-20 09:59:34 -08001292 config_.voe_channel_id = ch;
1293 config_.sync_group = sync_group;
stefanba4c0e42016-02-04 04:12:24 -08001294 RecreateAudioReceiveStream(use_transport_cc, extensions);
solenberg7add0582015-11-20 09:59:34 -08001295 }
solenbergc96df772015-10-21 13:01:53 -07001296
solenberg7add0582015-11-20 09:59:34 -08001297 ~WebRtcAudioReceiveStream() {
1298 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
1299 call_->DestroyAudioReceiveStream(stream_);
1300 }
1301
1302 void RecreateAudioReceiveStream(
1303 const std::vector<webrtc::RtpExtension>& extensions) {
1304 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
stefanba4c0e42016-02-04 04:12:24 -08001305 RecreateAudioReceiveStream(config_.rtp.transport_cc, extensions);
solenberg7add0582015-11-20 09:59:34 -08001306 }
stefanba4c0e42016-02-04 04:12:24 -08001307 void RecreateAudioReceiveStream(bool use_transport_cc) {
solenberg7add0582015-11-20 09:59:34 -08001308 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
stefanba4c0e42016-02-04 04:12:24 -08001309 RecreateAudioReceiveStream(use_transport_cc, config_.rtp.extensions);
solenberg7add0582015-11-20 09:59:34 -08001310 }
1311
1312 webrtc::AudioReceiveStream::Stats GetStats() const {
1313 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
1314 RTC_DCHECK(stream_);
1315 return stream_->GetStats();
1316 }
1317
1318 int channel() const {
1319 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
1320 return config_.voe_channel_id;
1321 }
solenbergc96df772015-10-21 13:01:53 -07001322
kwiberg686a8ef2016-02-26 03:00:35 -08001323 void SetRawAudioSink(std::unique_ptr<webrtc::AudioSinkInterface> sink) {
Tommif888bb52015-12-12 01:37:01 +01001324 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
kwiberg686a8ef2016-02-26 03:00:35 -08001325 stream_->SetSink(std::move(sink));
Tommif888bb52015-12-12 01:37:01 +01001326 }
1327
solenbergc96df772015-10-21 13:01:53 -07001328 private:
stefanba4c0e42016-02-04 04:12:24 -08001329 void RecreateAudioReceiveStream(
1330 bool use_transport_cc,
solenberg7add0582015-11-20 09:59:34 -08001331 const std::vector<webrtc::RtpExtension>& extensions) {
1332 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
1333 if (stream_) {
1334 call_->DestroyAudioReceiveStream(stream_);
1335 stream_ = nullptr;
1336 }
1337 config_.rtp.extensions = extensions;
stefanba4c0e42016-02-04 04:12:24 -08001338 config_.rtp.transport_cc = use_transport_cc;
solenberg7add0582015-11-20 09:59:34 -08001339 RTC_DCHECK(!stream_);
1340 stream_ = call_->CreateAudioReceiveStream(config_);
1341 RTC_CHECK(stream_);
1342 }
1343
1344 rtc::ThreadChecker worker_thread_checker_;
1345 webrtc::Call* call_ = nullptr;
1346 webrtc::AudioReceiveStream::Config config_;
1347 // The stream is owned by WebRtcAudioReceiveStream and may be reallocated if
1348 // configuration changes.
1349 webrtc::AudioReceiveStream* stream_ = nullptr;
solenbergc96df772015-10-21 13:01:53 -07001350
1351 RTC_DISALLOW_IMPLICIT_CONSTRUCTORS(WebRtcAudioReceiveStream);
henrike@webrtc.org1e09a712013-07-26 19:17:59 +00001352};
1353
Fredrik Solenberg709ed672015-09-15 12:26:33 +02001354WebRtcVoiceMediaChannel::WebRtcVoiceMediaChannel(WebRtcVoiceEngine* engine,
nisse51542be2016-02-12 02:27:06 -08001355 const MediaConfig& config,
Fredrik Solenbergb071a192015-09-17 16:42:56 +02001356 const AudioOptions& options,
Fredrik Solenberg709ed672015-09-15 12:26:33 +02001357 webrtc::Call* call)
nisse51542be2016-02-12 02:27:06 -08001358 : VoiceMediaChannel(config), engine_(engine), call_(call) {
solenberg0a617e22015-10-20 15:49:38 -07001359 LOG(LS_VERBOSE) << "WebRtcVoiceMediaChannel::WebRtcVoiceMediaChannel";
solenberg566ef242015-11-06 15:34:49 -08001360 RTC_DCHECK(call);
solenberg0a617e22015-10-20 15:49:38 -07001361 engine->RegisterChannel(this);
Fredrik Solenbergb071a192015-09-17 16:42:56 +02001362 SetOptions(options);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001363}
1364
1365WebRtcVoiceMediaChannel::~WebRtcVoiceMediaChannel() {
solenberg566ef242015-11-06 15:34:49 -08001366 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
solenberg0a617e22015-10-20 15:49:38 -07001367 LOG(LS_VERBOSE) << "WebRtcVoiceMediaChannel::~WebRtcVoiceMediaChannel";
solenberg7add0582015-11-20 09:59:34 -08001368 // TODO(solenberg): Should be able to delete the streams directly, without
1369 // going through RemoveNnStream(), once stream objects handle
1370 // all (de)configuration.
solenbergc96df772015-10-21 13:01:53 -07001371 while (!send_streams_.empty()) {
1372 RemoveSendStream(send_streams_.begin()->first);
solenbergd97ec302015-10-07 01:40:33 -07001373 }
solenberg7add0582015-11-20 09:59:34 -08001374 while (!recv_streams_.empty()) {
1375 RemoveRecvStream(recv_streams_.begin()->first);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001376 }
solenberg0a617e22015-10-20 15:49:38 -07001377 engine()->UnregisterChannel(this);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001378}
1379
nisse51542be2016-02-12 02:27:06 -08001380rtc::DiffServCodePoint WebRtcVoiceMediaChannel::PreferredDscp() const {
1381 return kAudioDscpValue;
1382}
1383
Peter Thatcherc2ee2c82015-08-07 16:05:34 -07001384bool WebRtcVoiceMediaChannel::SetSendParameters(
1385 const AudioSendParameters& params) {
Peter Boströmca8b4042016-03-08 14:24:13 -08001386 TRACE_EVENT0("webrtc", "WebRtcVoiceMediaChannel::SetSendParameters");
solenberg566ef242015-11-06 15:34:49 -08001387 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
solenberg7e4e01a2015-12-02 08:05:01 -08001388 LOG(LS_INFO) << "WebRtcVoiceMediaChannel::SetSendParameters: "
1389 << params.ToString();
Peter Thatcherc2ee2c82015-08-07 16:05:34 -07001390 // TODO(pthatcher): Refactor this to be more clean now that we have
1391 // all the information at once.
solenberg3a941542015-11-16 07:34:50 -08001392
1393 if (!SetSendCodecs(params.codecs)) {
1394 return false;
1395 }
1396
solenberg7e4e01a2015-12-02 08:05:01 -08001397 if (!ValidateRtpExtensions(params.extensions)) {
1398 return false;
1399 }
1400 std::vector<webrtc::RtpExtension> filtered_extensions =
1401 FilterRtpExtensions(params.extensions,
1402 webrtc::RtpExtension::IsSupportedForAudio, true);
1403 if (send_rtp_extensions_ != filtered_extensions) {
1404 send_rtp_extensions_.swap(filtered_extensions);
solenberg3a941542015-11-16 07:34:50 -08001405 for (auto& it : send_streams_) {
1406 it.second->RecreateAudioSendStream(send_rtp_extensions_);
1407 }
1408 }
1409
deadbeef80346142016-04-27 14:17:10 -07001410 if (!SetMaxSendBitrate(params.max_bandwidth_bps)) {
solenberg3a941542015-11-16 07:34:50 -08001411 return false;
1412 }
1413 return SetOptions(params.options);
Peter Thatcherc2ee2c82015-08-07 16:05:34 -07001414}
1415
1416bool WebRtcVoiceMediaChannel::SetRecvParameters(
1417 const AudioRecvParameters& params) {
Peter Boströmca8b4042016-03-08 14:24:13 -08001418 TRACE_EVENT0("webrtc", "WebRtcVoiceMediaChannel::SetRecvParameters");
solenberg566ef242015-11-06 15:34:49 -08001419 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
solenberg7e4e01a2015-12-02 08:05:01 -08001420 LOG(LS_INFO) << "WebRtcVoiceMediaChannel::SetRecvParameters: "
1421 << params.ToString();
Peter Thatcherc2ee2c82015-08-07 16:05:34 -07001422 // TODO(pthatcher): Refactor this to be more clean now that we have
1423 // all the information at once.
solenberg7add0582015-11-20 09:59:34 -08001424
1425 if (!SetRecvCodecs(params.codecs)) {
1426 return false;
1427 }
1428
solenberg7e4e01a2015-12-02 08:05:01 -08001429 if (!ValidateRtpExtensions(params.extensions)) {
1430 return false;
1431 }
1432 std::vector<webrtc::RtpExtension> filtered_extensions =
1433 FilterRtpExtensions(params.extensions,
1434 webrtc::RtpExtension::IsSupportedForAudio, false);
1435 if (recv_rtp_extensions_ != filtered_extensions) {
1436 recv_rtp_extensions_.swap(filtered_extensions);
solenberg7add0582015-11-20 09:59:34 -08001437 for (auto& it : recv_streams_) {
1438 it.second->RecreateAudioReceiveStream(recv_rtp_extensions_);
1439 }
1440 }
solenberg7add0582015-11-20 09:59:34 -08001441 return true;
Peter Thatcherc2ee2c82015-08-07 16:05:34 -07001442}
1443
Taylor Brandstetterdb0cd9e2016-05-16 11:40:30 -07001444webrtc::RtpParameters WebRtcVoiceMediaChannel::GetRtpSendParameters(
skvlade0d46372016-04-07 22:59:22 -07001445 uint32_t ssrc) const {
1446 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
1447 auto it = send_streams_.find(ssrc);
1448 if (it == send_streams_.end()) {
Taylor Brandstetterdb0cd9e2016-05-16 11:40:30 -07001449 LOG(LS_WARNING) << "Attempting to get RTP send parameters for stream "
1450 << "with ssrc " << ssrc << " which doesn't exist.";
skvlade0d46372016-04-07 22:59:22 -07001451 return webrtc::RtpParameters();
1452 }
1453
Taylor Brandstetter0cd086b2016-04-20 16:23:10 -07001454 webrtc::RtpParameters rtp_params = it->second->rtp_parameters();
1455 // Need to add the common list of codecs to the send stream-specific
1456 // RTP parameters.
1457 for (const AudioCodec& codec : send_codecs_) {
1458 rtp_params.codecs.push_back(codec.ToCodecParameters());
1459 }
1460 return rtp_params;
skvlade0d46372016-04-07 22:59:22 -07001461}
1462
Taylor Brandstetterdb0cd9e2016-05-16 11:40:30 -07001463bool WebRtcVoiceMediaChannel::SetRtpSendParameters(
skvlade0d46372016-04-07 22:59:22 -07001464 uint32_t ssrc,
1465 const webrtc::RtpParameters& parameters) {
1466 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
1467 if (!ValidateRtpParameters(parameters)) {
1468 return false;
1469 }
1470 auto it = send_streams_.find(ssrc);
1471 if (it == send_streams_.end()) {
Taylor Brandstetterdb0cd9e2016-05-16 11:40:30 -07001472 LOG(LS_WARNING) << "Attempting to set RTP send parameters for stream "
1473 << "with ssrc " << ssrc << " which doesn't exist.";
skvlade0d46372016-04-07 22:59:22 -07001474 return false;
1475 }
1476
Taylor Brandstetterdb0cd9e2016-05-16 11:40:30 -07001477 // TODO(deadbeef): Handle setting parameters with a list of codecs in a
1478 // different order (which should change the send codec).
1479 webrtc::RtpParameters current_parameters = GetRtpSendParameters(ssrc);
1480 if (current_parameters.codecs != parameters.codecs) {
1481 LOG(LS_ERROR) << "Using SetParameters to change the set of codecs "
1482 << "is not currently supported.";
1483 return false;
1484 }
1485
1486 if (!SetChannelSendParameters(it->second->channel(), parameters)) {
1487 LOG(LS_WARNING) << "Failed to set send RtpParameters.";
skvlade0d46372016-04-07 22:59:22 -07001488 return false;
1489 }
Taylor Brandstetter0cd086b2016-04-20 16:23:10 -07001490 // Codecs are handled at the WebRtcVoiceMediaChannel level.
1491 webrtc::RtpParameters reduced_params = parameters;
1492 reduced_params.codecs.clear();
Taylor Brandstetter55dd7082016-05-03 13:50:11 -07001493 it->second->SetRtpParameters(reduced_params);
skvlade0d46372016-04-07 22:59:22 -07001494 return true;
1495}
1496
Taylor Brandstetterdb0cd9e2016-05-16 11:40:30 -07001497webrtc::RtpParameters WebRtcVoiceMediaChannel::GetRtpReceiveParameters(
1498 uint32_t ssrc) const {
1499 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
1500 auto it = recv_streams_.find(ssrc);
1501 if (it == recv_streams_.end()) {
1502 LOG(LS_WARNING) << "Attempting to get RTP receive parameters for stream "
1503 << "with ssrc " << ssrc << " which doesn't exist.";
1504 return webrtc::RtpParameters();
1505 }
1506
1507 // TODO(deadbeef): Return stream-specific parameters.
1508 webrtc::RtpParameters rtp_params = CreateRtpParametersWithOneEncoding();
1509 for (const AudioCodec& codec : recv_codecs_) {
1510 rtp_params.codecs.push_back(codec.ToCodecParameters());
1511 }
1512 return rtp_params;
1513}
1514
1515bool WebRtcVoiceMediaChannel::SetRtpReceiveParameters(
1516 uint32_t ssrc,
1517 const webrtc::RtpParameters& parameters) {
1518 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
1519 if (!ValidateRtpParameters(parameters)) {
1520 return false;
1521 }
1522 auto it = recv_streams_.find(ssrc);
1523 if (it == recv_streams_.end()) {
1524 LOG(LS_WARNING) << "Attempting to set RTP receive parameters for stream "
1525 << "with ssrc " << ssrc << " which doesn't exist.";
1526 return false;
1527 }
1528
1529 webrtc::RtpParameters current_parameters = GetRtpReceiveParameters(ssrc);
1530 if (current_parameters != parameters) {
1531 LOG(LS_ERROR) << "Changing the RTP receive parameters is currently "
1532 << "unsupported.";
1533 return false;
1534 }
1535 return true;
1536}
1537
skvlade0d46372016-04-07 22:59:22 -07001538bool WebRtcVoiceMediaChannel::ValidateRtpParameters(
1539 const webrtc::RtpParameters& rtp_parameters) {
1540 if (rtp_parameters.encodings.size() != 1) {
1541 LOG(LS_ERROR)
1542 << "Attempted to set RtpParameters without exactly one encoding";
1543 return false;
1544 }
1545 return true;
1546}
1547
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001548bool WebRtcVoiceMediaChannel::SetOptions(const AudioOptions& options) {
solenberg566ef242015-11-06 15:34:49 -08001549 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001550 LOG(LS_INFO) << "Setting voice channel options: "
1551 << options.ToString();
1552
1553 // We retain all of the existing options, and apply the given ones
1554 // on top. This means there is no way to "clear" options such that
1555 // they go back to the engine default.
1556 options_.SetAll(options);
solenberg246b8172015-12-08 09:50:23 -08001557 if (!engine()->ApplyOptions(options_)) {
1558 LOG(LS_WARNING) <<
1559 "Failed to apply engine options during channel SetOptions.";
1560 return false;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001561 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001562 LOG(LS_INFO) << "Set voice channel options. Current options: "
1563 << options_.ToString();
1564 return true;
1565}
1566
1567bool WebRtcVoiceMediaChannel::SetRecvCodecs(
1568 const std::vector<AudioCodec>& codecs) {
solenberg566ef242015-11-06 15:34:49 -08001569 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
solenberg8fb30c32015-10-13 03:06:58 -07001570
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001571 // Set the payload types to be used for incoming media.
solenberg0b675462015-10-09 01:37:09 -07001572 LOG(LS_INFO) << "Setting receive voice codecs.";
solenberg0b675462015-10-09 01:37:09 -07001573
1574 if (!VerifyUniquePayloadTypes(codecs)) {
1575 LOG(LS_ERROR) << "Codec payload types overlap.";
1576 return false;
1577 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001578
1579 std::vector<AudioCodec> new_codecs;
1580 // Find all new codecs. We allow adding new codecs but don't allow changing
1581 // the payload type of codecs that is already configured since we might
1582 // already be receiving packets with that payload type.
Fredrik Solenbergaf9fb212015-08-26 10:45:53 +02001583 for (const AudioCodec& codec : codecs) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001584 AudioCodec old_codec;
Fredrik Solenbergaf9fb212015-08-26 10:45:53 +02001585 if (FindCodec(recv_codecs_, codec, &old_codec)) {
1586 if (old_codec.id != codec.id) {
1587 LOG(LS_ERROR) << codec.name << " payload type changed.";
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001588 return false;
1589 }
1590 } else {
Fredrik Solenbergaf9fb212015-08-26 10:45:53 +02001591 new_codecs.push_back(codec);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001592 }
1593 }
1594 if (new_codecs.empty()) {
1595 // There are no new codecs to configure. Already configured codecs are
1596 // never removed.
1597 return true;
1598 }
1599
1600 if (playout_) {
1601 // Receive codecs can not be changed while playing. So we temporarily
1602 // pause playout.
1603 PausePlayout();
1604 }
1605
solenberg26c8c912015-11-27 04:00:25 -08001606 bool result = true;
1607 for (const AudioCodec& codec : new_codecs) {
solenberg72e29d22016-03-08 06:35:16 -08001608 webrtc::CodecInst voe_codec = {0};
solenberg26c8c912015-11-27 04:00:25 -08001609 if (WebRtcVoiceEngine::ToCodecInst(codec, &voe_codec)) {
1610 LOG(LS_INFO) << ToString(codec);
1611 voe_codec.pltype = codec.id;
1612 for (const auto& ch : recv_streams_) {
1613 if (engine()->voe()->codec()->SetRecPayloadType(
1614 ch.second->channel(), voe_codec) == -1) {
1615 LOG_RTCERR2(SetRecPayloadType, ch.second->channel(),
1616 ToString(voe_codec));
1617 result = false;
1618 }
1619 }
1620 } else {
1621 LOG(LS_WARNING) << "Unknown codec " << ToString(codec);
1622 result = false;
1623 break;
1624 }
1625 }
Fredrik Solenbergaf9fb212015-08-26 10:45:53 +02001626 if (result) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001627 recv_codecs_ = codecs;
1628 }
1629
1630 if (desired_playout_ && !playout_) {
1631 ResumePlayout();
1632 }
Fredrik Solenbergaf9fb212015-08-26 10:45:53 +02001633 return result;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001634}
1635
solenberg72e29d22016-03-08 06:35:16 -08001636// Utility function called from SetSendParameters() to extract current send
1637// codec settings from the given list of codecs (originally from SDP). Both send
1638// and receive streams may be reconfigured based on the new settings.
wu@webrtc.orgcadf9042013-08-30 21:24:16 +00001639bool WebRtcVoiceMediaChannel::SetSendCodecs(
1640 const std::vector<AudioCodec>& codecs) {
solenberg566ef242015-11-06 15:34:49 -08001641 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
Fredrik Solenbergb5727682015-12-04 15:22:19 +01001642 // TODO(solenberg): Validate input - that payload types don't overlap, are
1643 // within range, filter out codecs we don't support,
solenberg31642aa2016-03-14 08:00:37 -07001644 // redundant codecs etc - the same way it is done for
1645 // RtpHeaderExtensions.
solenbergd97ec302015-10-07 01:40:33 -07001646
Fredrik Solenbergb5727682015-12-04 15:22:19 +01001647 // Find the DTMF telephone event "codec" payload type.
1648 dtmf_payload_type_ = rtc::Optional<int>();
Fredrik Solenbergaf9fb212015-08-26 10:45:53 +02001649 for (const AudioCodec& codec : codecs) {
Fredrik Solenbergaf9fb212015-08-26 10:45:53 +02001650 if (IsCodec(codec, kDtmfCodecName)) {
solenberg31642aa2016-03-14 08:00:37 -07001651 if (codec.id < kMinPayloadType || codec.id > kMaxPayloadType) {
1652 return false;
1653 }
Fredrik Solenbergb5727682015-12-04 15:22:19 +01001654 dtmf_payload_type_ = rtc::Optional<int>(codec.id);
1655 break;
wu@webrtc.orgcadf9042013-08-30 21:24:16 +00001656 }
1657 }
1658
solenberg72e29d22016-03-08 06:35:16 -08001659 // Scan through the list to figure out the codec to use for sending, along
1660 // with the proper configuration for VAD, CNG, RED, NACK and Opus-specific
1661 // parameters.
1662 {
1663 SendCodecSpec send_codec_spec;
1664 send_codec_spec.nack_enabled = send_codec_spec_.nack_enabled;
1665
1666 // Find send codec (the first non-telephone-event/CN codec).
1667 const AudioCodec* codec = WebRtcVoiceCodecs::GetPreferredCodec(
1668 codecs, &send_codec_spec.codec_inst, &send_codec_spec.red_payload_type);
1669 if (!codec) {
1670 LOG(LS_WARNING) << "Received empty list of codecs.";
1671 return false;
1672 }
1673
1674 send_codec_spec.transport_cc_enabled = HasTransportCc(*codec);
1675
1676 // This condition is apparently here because Opus does not support RED and
1677 // FEC simultaneously. However, DTX and max playback rate shouldn't have
1678 // such limitations.
1679 // TODO(solenberg): Refactor this logic once we create AudioEncoders here.
1680 if (send_codec_spec.red_payload_type == -1) {
1681 send_codec_spec.nack_enabled = HasNack(*codec);
1682 // For Opus as the send codec, we are to determine inband FEC, maximum
1683 // playback rate, and opus internal dtx.
1684 if (IsCodec(*codec, kOpusCodecName)) {
1685 GetOpusConfig(*codec, &send_codec_spec.codec_inst,
1686 &send_codec_spec.enable_codec_fec,
1687 &send_codec_spec.opus_max_playback_rate,
1688 &send_codec_spec.enable_opus_dtx);
1689 }
1690
1691 // Set packet size if the AudioCodec param kCodecParamPTime is set.
1692 int ptime_ms = 0;
1693 if (codec->GetParam(kCodecParamPTime, &ptime_ms)) {
1694 if (!WebRtcVoiceCodecs::SetPTimeAsPacketSize(
1695 &send_codec_spec.codec_inst, ptime_ms)) {
1696 LOG(LS_WARNING) << "Failed to set packet size for codec "
1697 << send_codec_spec.codec_inst.plname;
1698 return false;
1699 }
1700 }
1701 }
1702
1703 // Loop through the codecs list again to find the CN codec.
1704 // TODO(solenberg): Break out into a separate function?
1705 for (const AudioCodec& codec : codecs) {
1706 // Ignore codecs we don't know about. The negotiation step should prevent
1707 // this, but double-check to be sure.
1708 webrtc::CodecInst voe_codec = {0};
1709 if (!WebRtcVoiceEngine::ToCodecInst(codec, &voe_codec)) {
1710 LOG(LS_WARNING) << "Unknown codec " << ToString(codec);
1711 continue;
1712 }
1713
1714 if (IsCodec(codec, kCnCodecName)) {
1715 // Turn voice activity detection/comfort noise on if supported.
1716 // Set the wideband CN payload type appropriately.
1717 // (narrowband always uses the static payload type 13).
1718 int cng_plfreq = -1;
1719 switch (codec.clockrate) {
1720 case 8000:
1721 case 16000:
1722 case 32000:
1723 cng_plfreq = codec.clockrate;
1724 break;
1725 default:
1726 LOG(LS_WARNING) << "CN frequency " << codec.clockrate
1727 << " not supported.";
1728 continue;
1729 }
1730 send_codec_spec.cng_payload_type = codec.id;
1731 send_codec_spec.cng_plfreq = cng_plfreq;
1732 break;
1733 }
1734 }
1735
1736 // Latch in the new state.
1737 send_codec_spec_ = std::move(send_codec_spec);
1738 }
1739
wu@webrtc.orgcadf9042013-08-30 21:24:16 +00001740 // Cache the codecs in order to configure the channel created later.
solenbergc96df772015-10-21 13:01:53 -07001741 for (const auto& ch : send_streams_) {
skvlade0d46372016-04-07 22:59:22 -07001742 if (!SetSendCodecs(ch.second->channel(), ch.second->rtp_parameters())) {
wu@webrtc.orgcadf9042013-08-30 21:24:16 +00001743 return false;
1744 }
1745 }
1746
solenberg72e29d22016-03-08 06:35:16 -08001747 // Set nack status on receive channels.
deadbeefb56069e2016-05-06 04:57:03 -07001748 for (const auto& kv : recv_streams_) {
1749 SetNack(kv.second->channel(), send_codec_spec_.nack_enabled);
wu@webrtc.org9dba5252013-08-05 20:36:57 +00001750 }
solenberg0a617e22015-10-20 15:49:38 -07001751
stefanba4c0e42016-02-04 04:12:24 -08001752 // Check if the transport cc feedback has changed on the preferred send codec,
1753 // and in that case reconfigure all receive streams.
solenberg72e29d22016-03-08 06:35:16 -08001754 if (recv_transport_cc_enabled_ != send_codec_spec_.transport_cc_enabled) {
1755 LOG(LS_INFO) << "Recreate all the receive streams because the send "
1756 "codec has changed.";
1757 recv_transport_cc_enabled_ = send_codec_spec_.transport_cc_enabled;
1758 for (auto& kv : recv_streams_) {
1759 kv.second->RecreateAudioReceiveStream(recv_transport_cc_enabled_);
1760 }
1761 }
1762
Taylor Brandstetter0cd086b2016-04-20 16:23:10 -07001763 send_codecs_ = codecs;
solenberg72e29d22016-03-08 06:35:16 -08001764 return true;
1765}
1766
1767// Apply current codec settings to a single voe::Channel used for sending.
skvlade0d46372016-04-07 22:59:22 -07001768bool WebRtcVoiceMediaChannel::SetSendCodecs(
1769 int channel,
1770 const webrtc::RtpParameters& rtp_parameters) {
solenberg72e29d22016-03-08 06:35:16 -08001771 // Disable VAD, FEC, and RED unless we know the other side wants them.
1772 engine()->voe()->codec()->SetVADStatus(channel, false);
1773 engine()->voe()->rtp()->SetNACKStatus(channel, false, 0);
1774 engine()->voe()->rtp()->SetREDStatus(channel, false);
1775 engine()->voe()->codec()->SetFECStatus(channel, false);
1776
1777 if (send_codec_spec_.red_payload_type != -1) {
1778 // Enable redundant encoding of the specified codec. Treat any
1779 // failure as a fatal internal error.
1780 LOG(LS_INFO) << "Enabling RED on channel " << channel;
1781 if (engine()->voe()->rtp()->SetREDStatus(channel, true,
1782 send_codec_spec_.red_payload_type) == -1) {
1783 LOG_RTCERR3(SetREDStatus, channel, true,
1784 send_codec_spec_.red_payload_type);
1785 return false;
1786 }
1787 }
1788
1789 SetNack(channel, send_codec_spec_.nack_enabled);
1790
1791 // Set the codec immediately, since SetVADStatus() depends on whether
1792 // the current codec is mono or stereo.
1793 if (!SetSendCodec(channel, send_codec_spec_.codec_inst)) {
1794 return false;
1795 }
1796
1797 // FEC should be enabled after SetSendCodec.
1798 if (send_codec_spec_.enable_codec_fec) {
1799 LOG(LS_INFO) << "Attempt to enable codec internal FEC on channel "
1800 << channel;
1801 if (engine()->voe()->codec()->SetFECStatus(channel, true) == -1) {
1802 // Enable codec internal FEC. Treat any failure as fatal internal error.
1803 LOG_RTCERR2(SetFECStatus, channel, true);
1804 return false;
1805 }
1806 }
1807
1808 if (IsCodec(send_codec_spec_.codec_inst, kOpusCodecName)) {
1809 // DTX and maxplaybackrate should be set after SetSendCodec. Because current
1810 // send codec has to be Opus.
1811
1812 // Set Opus internal DTX.
1813 LOG(LS_INFO) << "Attempt to "
1814 << (send_codec_spec_.enable_opus_dtx ? "enable" : "disable")
1815 << " Opus DTX on channel "
1816 << channel;
1817 if (engine()->voe()->codec()->SetOpusDtx(channel,
1818 send_codec_spec_.enable_opus_dtx)) {
1819 LOG_RTCERR2(SetOpusDtx, channel, send_codec_spec_.enable_opus_dtx);
1820 return false;
1821 }
1822
1823 // If opus_max_playback_rate <= 0, the default maximum playback rate
1824 // (48 kHz) will be used.
1825 if (send_codec_spec_.opus_max_playback_rate > 0) {
1826 LOG(LS_INFO) << "Attempt to set maximum playback rate to "
1827 << send_codec_spec_.opus_max_playback_rate
1828 << " Hz on channel "
1829 << channel;
1830 if (engine()->voe()->codec()->SetOpusMaxPlaybackRate(
1831 channel, send_codec_spec_.opus_max_playback_rate) == -1) {
1832 LOG_RTCERR2(SetOpusMaxPlaybackRate, channel,
1833 send_codec_spec_.opus_max_playback_rate);
1834 return false;
stefanba4c0e42016-02-04 04:12:24 -08001835 }
1836 }
1837 }
deadbeef80346142016-04-27 14:17:10 -07001838 // TODO(solenberg): SetMaxSendBitrate() yields another call to SetSendCodec().
skvlade0d46372016-04-07 22:59:22 -07001839 // Check if it is possible to fuse with the previous call in this function.
Taylor Brandstetterdb0cd9e2016-05-16 11:40:30 -07001840 SetChannelSendParameters(channel, rtp_parameters);
solenberg72e29d22016-03-08 06:35:16 -08001841
1842 // Set the CN payloadtype and the VAD status.
1843 if (send_codec_spec_.cng_payload_type != -1) {
1844 // The CN payload type for 8000 Hz clockrate is fixed at 13.
1845 if (send_codec_spec_.cng_plfreq != 8000) {
1846 webrtc::PayloadFrequencies cn_freq;
1847 switch (send_codec_spec_.cng_plfreq) {
1848 case 16000:
1849 cn_freq = webrtc::kFreq16000Hz;
1850 break;
1851 case 32000:
1852 cn_freq = webrtc::kFreq32000Hz;
1853 break;
1854 default:
1855 RTC_NOTREACHED();
1856 return false;
1857 }
1858 if (engine()->voe()->codec()->SetSendCNPayloadType(
1859 channel, send_codec_spec_.cng_payload_type, cn_freq) == -1) {
1860 LOG_RTCERR3(SetSendCNPayloadType, channel,
1861 send_codec_spec_.cng_payload_type, cn_freq);
1862 // TODO(ajm): This failure condition will be removed from VoE.
1863 // Restore the return here when we update to a new enough webrtc.
1864 //
1865 // Not returning false because the SetSendCNPayloadType will fail if
1866 // the channel is already sending.
1867 // This can happen if the remote description is applied twice, for
1868 // example in the case of ROAP on top of JSEP, where both side will
1869 // send the offer.
1870 }
1871 }
1872
1873 // Only turn on VAD if we have a CN payload type that matches the
1874 // clockrate for the codec we are going to use.
1875 if (send_codec_spec_.cng_plfreq == send_codec_spec_.codec_inst.plfreq &&
1876 send_codec_spec_.codec_inst.channels == 1) {
1877 // TODO(minyue): If CN frequency == 48000 Hz is allowed, consider the
1878 // interaction between VAD and Opus FEC.
1879 LOG(LS_INFO) << "Enabling VAD";
1880 if (engine()->voe()->codec()->SetVADStatus(channel, true) == -1) {
1881 LOG_RTCERR2(SetVADStatus, channel, true);
1882 return false;
1883 }
1884 }
1885 }
solenberg0a617e22015-10-20 15:49:38 -07001886 return true;
wu@webrtc.org9dba5252013-08-05 20:36:57 +00001887}
1888
wu@webrtc.orgcadf9042013-08-30 21:24:16 +00001889void WebRtcVoiceMediaChannel::SetNack(int channel, bool nack_enabled) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001890 if (nack_enabled) {
wu@webrtc.orgcadf9042013-08-30 21:24:16 +00001891 LOG(LS_INFO) << "Enabling NACK for channel " << channel;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001892 engine()->voe()->rtp()->SetNACKStatus(channel, true, kNackMaxPackets);
1893 } else {
wu@webrtc.orgcadf9042013-08-30 21:24:16 +00001894 LOG(LS_INFO) << "Disabling NACK for channel " << channel;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001895 engine()->voe()->rtp()->SetNACKStatus(channel, false, 0);
1896 }
1897}
1898
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001899bool WebRtcVoiceMediaChannel::SetSendCodec(
wu@webrtc.org9dba5252013-08-05 20:36:57 +00001900 int channel, const webrtc::CodecInst& send_codec) {
1901 LOG(LS_INFO) << "Send channel " << channel << " selected voice codec "
1902 << ToString(send_codec) << ", bitrate=" << send_codec.rate;
1903
solenberg72e29d22016-03-08 06:35:16 -08001904 webrtc::CodecInst current_codec = {0};
wu@webrtc.org05e7b442014-04-01 17:44:24 +00001905 if (engine()->voe()->codec()->GetSendCodec(channel, current_codec) == 0 &&
1906 (send_codec == current_codec)) {
1907 // Codec is already configured, we can return without setting it again.
1908 return true;
1909 }
1910
wu@webrtc.org9dba5252013-08-05 20:36:57 +00001911 if (engine()->voe()->codec()->SetSendCodec(channel, send_codec) == -1) {
1912 LOG_RTCERR2(SetSendCodec, channel, ToString(send_codec));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001913 return false;
1914 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001915 return true;
1916}
1917
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001918bool WebRtcVoiceMediaChannel::SetPlayout(bool playout) {
1919 desired_playout_ = playout;
1920 return ChangePlayout(desired_playout_);
1921}
1922
1923bool WebRtcVoiceMediaChannel::PausePlayout() {
1924 return ChangePlayout(false);
1925}
1926
1927bool WebRtcVoiceMediaChannel::ResumePlayout() {
1928 return ChangePlayout(desired_playout_);
1929}
1930
1931bool WebRtcVoiceMediaChannel::ChangePlayout(bool playout) {
Peter Boströmca8b4042016-03-08 14:24:13 -08001932 TRACE_EVENT0("webrtc", "WebRtcVoiceMediaChannel::ChangePlayout");
solenberg566ef242015-11-06 15:34:49 -08001933 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001934 if (playout_ == playout) {
1935 return true;
1936 }
1937
solenberg7add0582015-11-20 09:59:34 -08001938 for (const auto& ch : recv_streams_) {
Fredrik Solenbergaf9fb212015-08-26 10:45:53 +02001939 if (!SetPlayout(ch.second->channel(), playout)) {
henrike@webrtc.org1e09a712013-07-26 19:17:59 +00001940 LOG(LS_ERROR) << "SetPlayout " << playout << " on channel "
Fredrik Solenbergaf9fb212015-08-26 10:45:53 +02001941 << ch.second->channel() << " failed";
solenberg1ac56142015-10-13 03:58:19 -07001942 return false;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001943 }
1944 }
solenberg1ac56142015-10-13 03:58:19 -07001945 playout_ = playout;
1946 return true;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001947}
1948
Taylor Brandstetter1a018dc2016-03-08 12:37:39 -08001949void WebRtcVoiceMediaChannel::SetSend(bool send) {
Peter Boströmca8b4042016-03-08 14:24:13 -08001950 TRACE_EVENT0("webrtc", "WebRtcVoiceMediaChannel::SetSend");
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001951 if (send_ == send) {
Taylor Brandstetter1a018dc2016-03-08 12:37:39 -08001952 return;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001953 }
1954
solenbergd53a3f92016-04-14 13:56:37 -07001955 // Apply channel specific options, and initialize the ADM for recording (this
1956 // may take time on some platforms, e.g. Android).
Taylor Brandstetter1a018dc2016-03-08 12:37:39 -08001957 if (send) {
solenberg63b34542015-09-29 06:06:31 -07001958 engine()->ApplyOptions(options_);
solenbergd53a3f92016-04-14 13:56:37 -07001959
1960 // InitRecording() may return an error if the ADM is already recording.
1961 if (!engine()->adm()->RecordingIsInitialized() &&
1962 !engine()->adm()->Recording()) {
1963 if (engine()->adm()->InitRecording() != 0) {
1964 LOG(LS_WARNING) << "Failed to initialize recording";
1965 }
1966 }
solenberg63b34542015-09-29 06:06:31 -07001967 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001968
wu@webrtc.org9dba5252013-08-05 20:36:57 +00001969 // Change the settings on each send channel.
Taylor Brandstetter1a018dc2016-03-08 12:37:39 -08001970 for (auto& kv : send_streams_) {
1971 kv.second->SetSend(send);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001972 }
wu@webrtc.org9dba5252013-08-05 20:36:57 +00001973
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001974 send_ = send;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001975}
1976
Peter Boström0c4e06b2015-10-07 12:23:21 +02001977bool WebRtcVoiceMediaChannel::SetAudioSend(uint32_t ssrc,
1978 bool enable,
solenberg1dd98f32015-09-10 01:57:14 -07001979 const AudioOptions* options,
Taylor Brandstetter1a018dc2016-03-08 12:37:39 -08001980 AudioSource* source) {
solenberg566ef242015-11-06 15:34:49 -08001981 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
solenberg1dd98f32015-09-10 01:57:14 -07001982 // TODO(solenberg): The state change should be fully rolled back if any one of
1983 // these calls fail.
Taylor Brandstetter1a018dc2016-03-08 12:37:39 -08001984 if (!SetLocalSource(ssrc, source)) {
solenberg1dd98f32015-09-10 01:57:14 -07001985 return false;
1986 }
solenbergdfc8f4f2015-10-01 02:31:10 -07001987 if (!MuteStream(ssrc, !enable)) {
solenberg1dd98f32015-09-10 01:57:14 -07001988 return false;
1989 }
solenbergdfc8f4f2015-10-01 02:31:10 -07001990 if (enable && options) {
solenberg1dd98f32015-09-10 01:57:14 -07001991 return SetOptions(*options);
1992 }
1993 return true;
1994}
1995
solenberg0a617e22015-10-20 15:49:38 -07001996int WebRtcVoiceMediaChannel::CreateVoEChannel() {
1997 int id = engine()->CreateVoEChannel();
1998 if (id == -1) {
1999 LOG_RTCERR0(CreateVoEChannel);
2000 return -1;
wu@webrtc.org9dba5252013-08-05 20:36:57 +00002001 }
mflodman3d7db262016-04-29 00:57:13 -07002002
solenberg0a617e22015-10-20 15:49:38 -07002003 return id;
wu@webrtc.org9dba5252013-08-05 20:36:57 +00002004}
2005
solenberg7add0582015-11-20 09:59:34 -08002006bool WebRtcVoiceMediaChannel::DeleteVoEChannel(int channel) {
wu@webrtc.org9dba5252013-08-05 20:36:57 +00002007 if (engine()->voe()->base()->DeleteChannel(channel) == -1) {
2008 LOG_RTCERR1(DeleteChannel, channel);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002009 return false;
2010 }
wu@webrtc.org9dba5252013-08-05 20:36:57 +00002011 return true;
2012}
henrike@webrtc.org1e09a712013-07-26 19:17:59 +00002013
wu@webrtc.org9dba5252013-08-05 20:36:57 +00002014bool WebRtcVoiceMediaChannel::AddSendStream(const StreamParams& sp) {
Peter Boströmca8b4042016-03-08 14:24:13 -08002015 TRACE_EVENT0("webrtc", "WebRtcVoiceMediaChannel::AddSendStream");
solenberg566ef242015-11-06 15:34:49 -08002016 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
solenberg0a617e22015-10-20 15:49:38 -07002017 LOG(LS_INFO) << "AddSendStream: " << sp.ToString();
2018
2019 uint32_t ssrc = sp.first_ssrc();
2020 RTC_DCHECK(0 != ssrc);
2021
2022 if (GetSendChannelId(ssrc) != -1) {
2023 LOG(LS_ERROR) << "Stream already exists with ssrc " << ssrc;
wu@webrtc.org9dba5252013-08-05 20:36:57 +00002024 return false;
2025 }
2026
solenberg0a617e22015-10-20 15:49:38 -07002027 // Create a new channel for sending audio data.
2028 int channel = CreateVoEChannel();
2029 if (channel == -1) {
2030 return false;
wu@webrtc.org9dba5252013-08-05 20:36:57 +00002031 }
wu@webrtc.org9dba5252013-08-05 20:36:57 +00002032
solenbergc96df772015-10-21 13:01:53 -07002033 // Save the channel to send_streams_, so that RemoveSendStream() can still
wu@webrtc.org9dba5252013-08-05 20:36:57 +00002034 // delete the channel in case failure happens below.
mallinath@webrtc.org67ee6b92014-02-03 16:57:16 +00002035 webrtc::AudioTransport* audio_transport =
2036 engine()->voe()->base()->audio_transport();
mflodman3d7db262016-04-29 00:57:13 -07002037
skvlade0d46372016-04-07 22:59:22 -07002038 WebRtcAudioSendStream* stream = new WebRtcAudioSendStream(
mflodman3d7db262016-04-29 00:57:13 -07002039 channel, audio_transport, ssrc, sp.cname, send_rtp_extensions_, call_,
2040 this);
skvlade0d46372016-04-07 22:59:22 -07002041 send_streams_.insert(std::make_pair(ssrc, stream));
wu@webrtc.org9dba5252013-08-05 20:36:57 +00002042
solenberg0a617e22015-10-20 15:49:38 -07002043 // Set the current codecs to be used for the new channel. We need to do this
2044 // after adding the channel to send_channels_, because of how max bitrate is
2045 // currently being configured by SetSendCodec().
skvlade0d46372016-04-07 22:59:22 -07002046 if (HasSendCodec() && !SetSendCodecs(channel, stream->rtp_parameters())) {
solenberg0a617e22015-10-20 15:49:38 -07002047 RemoveSendStream(ssrc);
wu@webrtc.org9dba5252013-08-05 20:36:57 +00002048 return false;
2049 }
2050
2051 // At this point the channel's local SSRC has been updated. If the channel is
solenberg0a617e22015-10-20 15:49:38 -07002052 // the first send channel make sure that all the receive channels are updated
2053 // with the same SSRC in order to send receiver reports.
solenbergc96df772015-10-21 13:01:53 -07002054 if (send_streams_.size() == 1) {
solenberg0a617e22015-10-20 15:49:38 -07002055 receiver_reports_ssrc_ = ssrc;
solenberg7add0582015-11-20 09:59:34 -08002056 for (const auto& stream : recv_streams_) {
2057 int recv_channel = stream.second->channel();
solenberg0a617e22015-10-20 15:49:38 -07002058 if (engine()->voe()->rtp()->SetLocalSSRC(recv_channel, ssrc) != 0) {
solenberg7add0582015-11-20 09:59:34 -08002059 LOG_RTCERR2(SetLocalSSRC, recv_channel, ssrc);
solenberg1ac56142015-10-13 03:58:19 -07002060 return false;
wu@webrtc.org9dba5252013-08-05 20:36:57 +00002061 }
solenberg0a617e22015-10-20 15:49:38 -07002062 engine()->voe()->base()->AssociateSendChannel(recv_channel, channel);
2063 LOG(LS_INFO) << "VoiceEngine channel #" << recv_channel
2064 << " is associated with channel #" << channel << ".";
wu@webrtc.org9dba5252013-08-05 20:36:57 +00002065 }
2066 }
2067
Taylor Brandstetter1a018dc2016-03-08 12:37:39 -08002068 send_streams_[ssrc]->SetSend(send_);
2069 return true;
wu@webrtc.org9dba5252013-08-05 20:36:57 +00002070}
2071
Peter Boström0c4e06b2015-10-07 12:23:21 +02002072bool WebRtcVoiceMediaChannel::RemoveSendStream(uint32_t ssrc) {
Peter Boströmca8b4042016-03-08 14:24:13 -08002073 TRACE_EVENT0("webrtc", "WebRtcVoiceMediaChannel::RemoveSendStream");
solenberg566ef242015-11-06 15:34:49 -08002074 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
solenberg3a941542015-11-16 07:34:50 -08002075 LOG(LS_INFO) << "RemoveSendStream: " << ssrc;
2076
solenbergc96df772015-10-21 13:01:53 -07002077 auto it = send_streams_.find(ssrc);
2078 if (it == send_streams_.end()) {
wu@webrtc.org9dba5252013-08-05 20:36:57 +00002079 LOG(LS_WARNING) << "Try to remove stream with ssrc " << ssrc
2080 << " which doesn't exist.";
2081 return false;
2082 }
2083
Taylor Brandstetter1a018dc2016-03-08 12:37:39 -08002084 it->second->SetSend(false);
wu@webrtc.org9dba5252013-08-05 20:36:57 +00002085
solenberg7add0582015-11-20 09:59:34 -08002086 // Clean up and delete the send stream+channel.
Taylor Brandstetter1a018dc2016-03-08 12:37:39 -08002087 int channel = it->second->channel();
solenberg0a617e22015-10-20 15:49:38 -07002088 LOG(LS_INFO) << "Removing audio send stream " << ssrc
2089 << " with VoiceEngine channel #" << channel << ".";
solenberg7add0582015-11-20 09:59:34 -08002090 delete it->second;
2091 send_streams_.erase(it);
2092 if (!DeleteVoEChannel(channel)) {
solenberg0a617e22015-10-20 15:49:38 -07002093 return false;
wu@webrtc.org9dba5252013-08-05 20:36:57 +00002094 }
solenbergc96df772015-10-21 13:01:53 -07002095 if (send_streams_.empty()) {
Taylor Brandstetter1a018dc2016-03-08 12:37:39 -08002096 SetSend(false);
solenberg0a617e22015-10-20 15:49:38 -07002097 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002098 return true;
2099}
2100
2101bool WebRtcVoiceMediaChannel::AddRecvStream(const StreamParams& sp) {
Peter Boströmca8b4042016-03-08 14:24:13 -08002102 TRACE_EVENT0("webrtc", "WebRtcVoiceMediaChannel::AddRecvStream");
solenberg566ef242015-11-06 15:34:49 -08002103 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
solenbergd97ec302015-10-07 01:40:33 -07002104 LOG(LS_INFO) << "AddRecvStream: " << sp.ToString();
2105
solenberg0b675462015-10-09 01:37:09 -07002106 if (!ValidateStreamParams(sp)) {
wu@webrtc.org78187522013-10-07 23:32:02 +00002107 return false;
2108 }
2109
solenberg7add0582015-11-20 09:59:34 -08002110 const uint32_t ssrc = sp.first_ssrc();
solenberg0b675462015-10-09 01:37:09 -07002111 if (ssrc == 0) {
2112 LOG(LS_WARNING) << "AddRecvStream with ssrc==0 is not supported.";
2113 return false;
2114 }
2115
solenberg1ac56142015-10-13 03:58:19 -07002116 // Remove the default receive stream if one had been created with this ssrc;
2117 // we'll recreate it then.
2118 if (IsDefaultRecvStream(ssrc)) {
2119 RemoveRecvStream(ssrc);
2120 }
solenberg0b675462015-10-09 01:37:09 -07002121
solenberg7add0582015-11-20 09:59:34 -08002122 if (GetReceiveChannelId(ssrc) != -1) {
henrike@webrtc.org1e09a712013-07-26 19:17:59 +00002123 LOG(LS_ERROR) << "Stream already exists with ssrc " << ssrc;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002124 return false;
2125 }
Fredrik Solenberg4b60c732015-05-07 14:07:48 +02002126
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002127 // Create a new channel for receiving audio data.
solenberg7add0582015-11-20 09:59:34 -08002128 const int channel = CreateVoEChannel();
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002129 if (channel == -1) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002130 return false;
2131 }
Minyue2013aec2015-05-13 14:14:42 +02002132
solenberg1ac56142015-10-13 03:58:19 -07002133 // Turn off all supported codecs.
solenberg26c8c912015-11-27 04:00:25 -08002134 // TODO(solenberg): Remove once "no codecs" is the default state of a stream.
2135 for (webrtc::CodecInst voe_codec : webrtc::acm2::RentACodec::Database()) {
2136 voe_codec.pltype = -1;
2137 if (engine()->voe()->codec()->SetRecPayloadType(channel, voe_codec) == -1) {
2138 LOG_RTCERR2(SetRecPayloadType, channel, ToString(voe_codec));
2139 DeleteVoEChannel(channel);
2140 return false;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002141 }
2142 }
2143
solenberg1ac56142015-10-13 03:58:19 -07002144 // Only enable those configured for this channel.
2145 for (const auto& codec : recv_codecs_) {
solenberg72e29d22016-03-08 06:35:16 -08002146 webrtc::CodecInst voe_codec = {0};
solenberg26c8c912015-11-27 04:00:25 -08002147 if (WebRtcVoiceEngine::ToCodecInst(codec, &voe_codec)) {
solenberg1ac56142015-10-13 03:58:19 -07002148 voe_codec.pltype = codec.id;
2149 if (engine()->voe()->codec()->SetRecPayloadType(
2150 channel, voe_codec) == -1) {
2151 LOG_RTCERR2(SetRecPayloadType, channel, ToString(voe_codec));
solenberg7add0582015-11-20 09:59:34 -08002152 DeleteVoEChannel(channel);
solenberg1ac56142015-10-13 03:58:19 -07002153 return false;
2154 }
henrike@webrtc.org1e09a712013-07-26 19:17:59 +00002155 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002156 }
solenberg8fb30c32015-10-13 03:06:58 -07002157
solenberg7add0582015-11-20 09:59:34 -08002158 const int send_channel = GetSendChannelId(receiver_reports_ssrc_);
2159 if (send_channel != -1) {
2160 // Associate receive channel with first send channel (so the receive channel
2161 // can obtain RTT from the send channel)
2162 engine()->voe()->base()->AssociateSendChannel(channel, send_channel);
2163 LOG(LS_INFO) << "VoiceEngine channel #" << channel
2164 << " is associated with channel #" << send_channel << ".";
buildbot@webrtc.org150835e2014-05-06 15:54:38 +00002165 }
2166
stefanba4c0e42016-02-04 04:12:24 -08002167 recv_streams_.insert(std::make_pair(
2168 ssrc, new WebRtcAudioReceiveStream(channel, ssrc, receiver_reports_ssrc_,
solenberg72e29d22016-03-08 06:35:16 -08002169 recv_transport_cc_enabled_,
2170 sp.sync_label, recv_rtp_extensions_,
solenberg31fec402016-05-06 02:13:12 -07002171 call_, this)));
solenberg7add0582015-11-20 09:59:34 -08002172
solenberg72e29d22016-03-08 06:35:16 -08002173 SetNack(channel, send_codec_spec_.nack_enabled);
solenberg1ac56142015-10-13 03:58:19 -07002174 SetPlayout(channel, playout_);
solenberg7add0582015-11-20 09:59:34 -08002175
solenberg1ac56142015-10-13 03:58:19 -07002176 return true;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002177}
2178
Peter Boström0c4e06b2015-10-07 12:23:21 +02002179bool WebRtcVoiceMediaChannel::RemoveRecvStream(uint32_t ssrc) {
Peter Boströmca8b4042016-03-08 14:24:13 -08002180 TRACE_EVENT0("webrtc", "WebRtcVoiceMediaChannel::RemoveRecvStream");
solenberg566ef242015-11-06 15:34:49 -08002181 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
solenbergd97ec302015-10-07 01:40:33 -07002182 LOG(LS_INFO) << "RemoveRecvStream: " << ssrc;
2183
solenberg7add0582015-11-20 09:59:34 -08002184 const auto it = recv_streams_.find(ssrc);
2185 if (it == recv_streams_.end()) {
wu@webrtc.org9dba5252013-08-05 20:36:57 +00002186 LOG(LS_WARNING) << "Try to remove stream with ssrc " << ssrc
2187 << " which doesn't exist.";
henrike@webrtc.org1e09a712013-07-26 19:17:59 +00002188 return false;
wu@webrtc.org9dba5252013-08-05 20:36:57 +00002189 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002190
solenberg1ac56142015-10-13 03:58:19 -07002191 // Deregister default channel, if that's the one being destroyed.
2192 if (IsDefaultRecvStream(ssrc)) {
2193 default_recv_ssrc_ = -1;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002194 }
henrike@webrtc.org1e09a712013-07-26 19:17:59 +00002195
solenberg7add0582015-11-20 09:59:34 -08002196 const int channel = it->second->channel();
2197
2198 // Clean up and delete the receive stream+channel.
2199 LOG(LS_INFO) << "Removing audio receive stream " << ssrc
mallinath@webrtc.org67ee6b92014-02-03 16:57:16 +00002200 << " with VoiceEngine channel #" << channel << ".";
Tommif888bb52015-12-12 01:37:01 +01002201 it->second->SetRawAudioSink(nullptr);
solenberg7add0582015-11-20 09:59:34 -08002202 delete it->second;
2203 recv_streams_.erase(it);
2204 return DeleteVoEChannel(channel);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002205}
2206
Taylor Brandstetter1a018dc2016-03-08 12:37:39 -08002207bool WebRtcVoiceMediaChannel::SetLocalSource(uint32_t ssrc,
2208 AudioSource* source) {
solenbergc96df772015-10-21 13:01:53 -07002209 auto it = send_streams_.find(ssrc);
2210 if (it == send_streams_.end()) {
Taylor Brandstetter1a018dc2016-03-08 12:37:39 -08002211 if (source) {
2212 // Return an error if trying to set a valid source with an invalid ssrc.
2213 LOG(LS_ERROR) << "SetLocalSource failed with ssrc " << ssrc;
wu@webrtc.org9dba5252013-08-05 20:36:57 +00002214 return false;
2215 }
2216
2217 // The channel likely has gone away, do nothing.
henrike@webrtc.org1e09a712013-07-26 19:17:59 +00002218 return true;
henrike@webrtc.org1e09a712013-07-26 19:17:59 +00002219 }
2220
Taylor Brandstetter1a018dc2016-03-08 12:37:39 -08002221 if (source) {
2222 it->second->SetSource(source);
solenberg1ac56142015-10-13 03:58:19 -07002223 } else {
Taylor Brandstetter1a018dc2016-03-08 12:37:39 -08002224 it->second->ClearSource();
solenberg1ac56142015-10-13 03:58:19 -07002225 }
henrike@webrtc.org1e09a712013-07-26 19:17:59 +00002226
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002227 return true;
2228}
2229
2230bool WebRtcVoiceMediaChannel::GetActiveStreams(
2231 AudioInfo::StreamList* actives) {
solenberg566ef242015-11-06 15:34:49 -08002232 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002233 actives->clear();
solenberg7add0582015-11-20 09:59:34 -08002234 for (const auto& ch : recv_streams_) {
Fredrik Solenbergaf9fb212015-08-26 10:45:53 +02002235 int level = GetOutputLevel(ch.second->channel());
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002236 if (level > 0) {
Fredrik Solenbergaf9fb212015-08-26 10:45:53 +02002237 actives->push_back(std::make_pair(ch.first, level));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002238 }
2239 }
2240 return true;
2241}
2242
2243int WebRtcVoiceMediaChannel::GetOutputLevel() {
solenberg566ef242015-11-06 15:34:49 -08002244 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
solenberg1ac56142015-10-13 03:58:19 -07002245 int highest = 0;
solenberg7add0582015-11-20 09:59:34 -08002246 for (const auto& ch : recv_streams_) {
solenberg8fb30c32015-10-13 03:06:58 -07002247 highest = std::max(GetOutputLevel(ch.second->channel()), highest);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002248 }
2249 return highest;
2250}
2251
2252int WebRtcVoiceMediaChannel::GetTimeSinceLastTyping() {
2253 int ret;
2254 if (engine()->voe()->processing()->TimeSinceLastTyping(ret) == -1) {
2255 // In case of error, log the info and continue
2256 LOG_RTCERR0(TimeSinceLastTyping);
2257 ret = -1;
2258 } else {
2259 ret *= 1000; // We return ms, webrtc returns seconds.
2260 }
2261 return ret;
2262}
2263
2264void WebRtcVoiceMediaChannel::SetTypingDetectionParameters(int time_window,
2265 int cost_per_typing, int reporting_threshold, int penalty_decay,
2266 int type_event_delay) {
2267 if (engine()->voe()->processing()->SetTypingDetectionParameters(
2268 time_window, cost_per_typing,
2269 reporting_threshold, penalty_decay, type_event_delay) == -1) {
2270 // In case of error, log the info and continue
2271 LOG_RTCERR5(SetTypingDetectionParameters, time_window,
2272 cost_per_typing, reporting_threshold, penalty_decay,
2273 type_event_delay);
2274 }
2275}
2276
solenberg4bac9c52015-10-09 02:32:53 -07002277bool WebRtcVoiceMediaChannel::SetOutputVolume(uint32_t ssrc, double volume) {
solenberg566ef242015-11-06 15:34:49 -08002278 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
solenberg1ac56142015-10-13 03:58:19 -07002279 if (ssrc == 0) {
2280 default_recv_volume_ = volume;
2281 if (default_recv_ssrc_ == -1) {
2282 return true;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002283 }
solenberg1ac56142015-10-13 03:58:19 -07002284 ssrc = static_cast<uint32_t>(default_recv_ssrc_);
2285 }
2286 int ch_id = GetReceiveChannelId(ssrc);
2287 if (ch_id < 0) {
2288 LOG(LS_WARNING) << "Cannot find channel for ssrc:" << ssrc;
2289 return false;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002290 }
2291
solenberg1ac56142015-10-13 03:58:19 -07002292 if (-1 == engine()->voe()->volume()->SetChannelOutputVolumeScaling(ch_id,
2293 volume)) {
2294 LOG_RTCERR2(SetChannelOutputVolumeScaling, ch_id, volume);
2295 return false;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002296 }
solenberg1ac56142015-10-13 03:58:19 -07002297 LOG(LS_INFO) << "SetOutputVolume to " << volume
2298 << " for channel " << ch_id << " and ssrc " << ssrc;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002299 return true;
2300}
2301
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002302bool WebRtcVoiceMediaChannel::CanInsertDtmf() {
Fredrik Solenbergb5727682015-12-04 15:22:19 +01002303 return dtmf_payload_type_ ? true : false;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002304}
2305
solenberg1d63dd02015-12-02 12:35:09 -08002306bool WebRtcVoiceMediaChannel::InsertDtmf(uint32_t ssrc, int event,
2307 int duration) {
solenberg566ef242015-11-06 15:34:49 -08002308 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
Fredrik Solenbergb5727682015-12-04 15:22:19 +01002309 LOG(LS_INFO) << "WebRtcVoiceMediaChannel::InsertDtmf";
2310 if (!dtmf_payload_type_) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002311 return false;
2312 }
2313
Fredrik Solenbergb5727682015-12-04 15:22:19 +01002314 // Figure out which WebRtcAudioSendStream to send the event on.
2315 auto it = ssrc != 0 ? send_streams_.find(ssrc) : send_streams_.begin();
2316 if (it == send_streams_.end()) {
2317 LOG(LS_WARNING) << "The specified ssrc " << ssrc << " is not in use.";
solenberg1d63dd02015-12-02 12:35:09 -08002318 return false;
2319 }
Fredrik Solenbergb5727682015-12-04 15:22:19 +01002320 if (event < kMinTelephoneEventCode ||
2321 event > kMaxTelephoneEventCode) {
2322 LOG(LS_WARNING) << "DTMF event code " << event << " out of range.";
solenberg1d63dd02015-12-02 12:35:09 -08002323 return false;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002324 }
Fredrik Solenbergb5727682015-12-04 15:22:19 +01002325 if (duration < kMinTelephoneEventDuration ||
2326 duration > kMaxTelephoneEventDuration) {
2327 LOG(LS_WARNING) << "DTMF event duration " << duration << " out of range.";
2328 return false;
2329 }
2330 return it->second->SendTelephoneEvent(*dtmf_payload_type_, event, duration);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002331}
2332
wu@webrtc.orga9890802013-12-13 00:21:03 +00002333void WebRtcVoiceMediaChannel::OnPacketReceived(
jbaucheec21bd2016-03-20 06:15:43 -07002334 rtc::CopyOnWriteBuffer* packet, const rtc::PacketTime& packet_time) {
solenberg566ef242015-11-06 15:34:49 -08002335 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
Fredrik Solenberg4b60c732015-05-07 14:07:48 +02002336
mflodman3d7db262016-04-29 00:57:13 -07002337 const webrtc::PacketTime webrtc_packet_time(packet_time.timestamp,
2338 packet_time.not_before);
2339 webrtc::PacketReceiver::DeliveryStatus delivery_result =
2340 call_->Receiver()->DeliverPacket(webrtc::MediaType::AUDIO,
2341 packet->cdata(), packet->size(),
2342 webrtc_packet_time);
mflodman3d7db262016-04-29 00:57:13 -07002343 if (delivery_result != webrtc::PacketReceiver::DELIVERY_UNKNOWN_SSRC) {
2344 return;
2345 }
2346
2347 // Create a default receive stream for this unsignalled and previously not
2348 // received ssrc. If there already is a default receive stream, delete it.
2349 // See: https://bugs.chromium.org/p/webrtc/issues/detail?id=5208
solenberg1ac56142015-10-13 03:58:19 -07002350 uint32_t ssrc = 0;
jbaucheec21bd2016-03-20 06:15:43 -07002351 if (!GetRtpSsrc(packet->cdata(), packet->size(), &ssrc)) {
solenberg1ac56142015-10-13 03:58:19 -07002352 return;
2353 }
2354
mflodman3d7db262016-04-29 00:57:13 -07002355 if (default_recv_ssrc_ != -1) {
2356 LOG(LS_INFO) << "Removing default receive stream with ssrc "
2357 << default_recv_ssrc_;
2358 RTC_DCHECK_NE(ssrc, default_recv_ssrc_);
2359 RemoveRecvStream(default_recv_ssrc_);
2360 default_recv_ssrc_ = -1;
solenberg1ac56142015-10-13 03:58:19 -07002361 }
2362
mflodman3d7db262016-04-29 00:57:13 -07002363 StreamParams sp;
2364 sp.ssrcs.push_back(ssrc);
2365 LOG(LS_INFO) << "Creating default receive stream for SSRC=" << ssrc << ".";
2366 if (!AddRecvStream(sp)) {
2367 LOG(LS_WARNING) << "Could not create default receive stream.";
2368 return;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002369 }
mflodman3d7db262016-04-29 00:57:13 -07002370 default_recv_ssrc_ = ssrc;
2371 SetOutputVolume(default_recv_ssrc_, default_recv_volume_);
2372 if (default_sink_) {
2373 std::unique_ptr<webrtc::AudioSinkInterface> proxy_sink(
2374 new ProxySink(default_sink_.get()));
2375 SetRawAudioSink(default_recv_ssrc_, std::move(proxy_sink));
2376 }
2377 delivery_result = call_->Receiver()->DeliverPacket(webrtc::MediaType::AUDIO,
2378 packet->cdata(),
2379 packet->size(),
2380 webrtc_packet_time);
2381 RTC_DCHECK_NE(webrtc::PacketReceiver::DELIVERY_UNKNOWN_SSRC, delivery_result);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002382}
2383
wu@webrtc.orga9890802013-12-13 00:21:03 +00002384void WebRtcVoiceMediaChannel::OnRtcpReceived(
jbaucheec21bd2016-03-20 06:15:43 -07002385 rtc::CopyOnWriteBuffer* packet, const rtc::PacketTime& packet_time) {
solenberg566ef242015-11-06 15:34:49 -08002386 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
Fredrik Solenberg4b60c732015-05-07 14:07:48 +02002387
Fredrik Solenberg709ed672015-09-15 12:26:33 +02002388 // Forward packet to Call as well.
2389 const webrtc::PacketTime webrtc_packet_time(packet_time.timestamp,
2390 packet_time.not_before);
2391 call_->Receiver()->DeliverPacket(webrtc::MediaType::AUDIO,
jbaucheec21bd2016-03-20 06:15:43 -07002392 packet->cdata(), packet->size(), webrtc_packet_time);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002393}
2394
Honghai Zhangcc411c02016-03-29 17:27:21 -07002395void WebRtcVoiceMediaChannel::OnNetworkRouteChanged(
2396 const std::string& transport_name,
Honghai Zhang0e533ef2016-04-19 15:41:36 -07002397 const rtc::NetworkRoute& network_route) {
2398 call_->OnNetworkRouteChanged(transport_name, network_route);
Honghai Zhangcc411c02016-03-29 17:27:21 -07002399}
2400
Peter Boström0c4e06b2015-10-07 12:23:21 +02002401bool WebRtcVoiceMediaChannel::MuteStream(uint32_t ssrc, bool muted) {
solenberg566ef242015-11-06 15:34:49 -08002402 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
solenberg0a617e22015-10-20 15:49:38 -07002403 int channel = GetSendChannelId(ssrc);
wu@webrtc.org9dba5252013-08-05 20:36:57 +00002404 if (channel == -1) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002405 LOG(LS_WARNING) << "The specified ssrc " << ssrc << " is not in use.";
2406 return false;
2407 }
wu@webrtc.org9dba5252013-08-05 20:36:57 +00002408 if (engine()->voe()->volume()->SetInputMute(channel, muted) == -1) {
2409 LOG_RTCERR2(SetInputMute, channel, muted);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002410 return false;
2411 }
buildbot@webrtc.org6b21b712014-07-31 15:08:53 +00002412 // We set the AGC to mute state only when all the channels are muted.
2413 // This implementation is not ideal, instead we should signal the AGC when
2414 // the mic channel is muted/unmuted. We can't do it today because there
2415 // is no good way to know which stream is mapping to the mic channel.
2416 bool all_muted = muted;
solenbergc96df772015-10-21 13:01:53 -07002417 for (const auto& ch : send_streams_) {
Fredrik Solenbergaf9fb212015-08-26 10:45:53 +02002418 if (!all_muted) {
2419 break;
2420 }
2421 if (engine()->voe()->volume()->GetInputMute(ch.second->channel(),
buildbot@webrtc.org6b21b712014-07-31 15:08:53 +00002422 all_muted)) {
Fredrik Solenbergaf9fb212015-08-26 10:45:53 +02002423 LOG_RTCERR1(GetInputMute, ch.second->channel());
buildbot@webrtc.org6b21b712014-07-31 15:08:53 +00002424 return false;
2425 }
2426 }
2427
2428 webrtc::AudioProcessing* ap = engine()->voe()->base()->audio_processing();
solenberg0a617e22015-10-20 15:49:38 -07002429 if (ap) {
buildbot@webrtc.org6b21b712014-07-31 15:08:53 +00002430 ap->set_output_will_be_muted(all_muted);
solenberg0a617e22015-10-20 15:49:38 -07002431 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002432 return true;
2433}
2434
deadbeef80346142016-04-27 14:17:10 -07002435bool WebRtcVoiceMediaChannel::SetMaxSendBitrate(int bps) {
2436 LOG(LS_INFO) << "WebRtcVoiceMediaChannel::SetMaxSendBitrate.";
2437 max_send_bitrate_bps_ = bps;
skvlade0d46372016-04-07 22:59:22 -07002438
2439 for (const auto& kv : send_streams_) {
Taylor Brandstetterdb0cd9e2016-05-16 11:40:30 -07002440 if (!SetChannelSendParameters(kv.second->channel(),
2441 kv.second->rtp_parameters())) {
skvlade0d46372016-04-07 22:59:22 -07002442 return false;
2443 }
2444 }
2445 return true;
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00002446}
2447
Taylor Brandstetterdb0cd9e2016-05-16 11:40:30 -07002448bool WebRtcVoiceMediaChannel::SetChannelSendParameters(
skvlade0d46372016-04-07 22:59:22 -07002449 int channel,
2450 const webrtc::RtpParameters& parameters) {
2451 RTC_CHECK_EQ(1UL, parameters.encodings.size());
Taylor Brandstetter0cd086b2016-04-20 16:23:10 -07002452 // TODO(deadbeef): Handle setting parameters with a list of codecs in a
2453 // different order (which should change the send codec).
deadbeef80346142016-04-27 14:17:10 -07002454 return SetMaxSendBitrate(
2455 channel, MinPositive(max_send_bitrate_bps_,
2456 parameters.encodings[0].max_bitrate_bps));
skvlade0d46372016-04-07 22:59:22 -07002457}
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00002458
deadbeef80346142016-04-27 14:17:10 -07002459bool WebRtcVoiceMediaChannel::SetMaxSendBitrate(int channel, int bps) {
skvlade0d46372016-04-07 22:59:22 -07002460 // Bitrate is auto by default.
2461 // TODO(bemasc): Fix this so that if SetMaxSendBandwidth(50) is followed by
2462 // SetMaxSendBandwith(0), the second call removes the previous limit.
deadbeef80346142016-04-27 14:17:10 -07002463 if (bps <= 0) {
skvlade0d46372016-04-07 22:59:22 -07002464 return true;
deadbeef80346142016-04-27 14:17:10 -07002465 }
wu@webrtc.org1d1ffc92013-10-16 18:12:02 +00002466
solenberg72e29d22016-03-08 06:35:16 -08002467 if (!HasSendCodec()) {
wu@webrtc.org1d1ffc92013-10-16 18:12:02 +00002468 LOG(LS_INFO) << "The send codec has not been set up yet. "
minyue@webrtc.org26236952014-10-29 02:27:08 +00002469 << "The send bitrate setting will be applied later.";
wu@webrtc.org1d1ffc92013-10-16 18:12:02 +00002470 return true;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002471 }
2472
solenberg72e29d22016-03-08 06:35:16 -08002473 webrtc::CodecInst codec = send_codec_spec_.codec_inst;
solenberg26c8c912015-11-27 04:00:25 -08002474 bool is_multi_rate = WebRtcVoiceCodecs::IsCodecMultiRate(codec);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002475
2476 if (is_multi_rate) {
2477 // If codec is multi-rate then just set the bitrate.
deadbeef80346142016-04-27 14:17:10 -07002478 int max_bitrate_bps = WebRtcVoiceCodecs::MaxBitrateBps(codec);
2479 codec.rate = std::min(bps, max_bitrate_bps);
2480 LOG(LS_INFO) << "Setting codec " << codec.plname << " to bitrate " << bps
2481 << " bps.";
skvlade0d46372016-04-07 22:59:22 -07002482 if (!SetSendCodec(channel, codec)) {
deadbeef80346142016-04-27 14:17:10 -07002483 LOG(LS_ERROR) << "Failed to set codec " << codec.plname << " to bitrate "
2484 << bps << " bps.";
skvlade0d46372016-04-07 22:59:22 -07002485 return false;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002486 }
2487 return true;
2488 } else {
2489 // If codec is not multi-rate and |bps| is less than the fixed bitrate
2490 // then fail. If codec is not multi-rate and |bps| exceeds or equal the
2491 // fixed bitrate then ignore.
2492 if (bps < codec.rate) {
deadbeef80346142016-04-27 14:17:10 -07002493 LOG(LS_ERROR) << "Failed to set codec " << codec.plname << " to bitrate "
2494 << bps << " bps"
2495 << ", requires at least " << codec.rate << " bps.";
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002496 return false;
2497 }
2498 return true;
2499 }
2500}
2501
skvlad7a43d252016-03-22 15:32:27 -07002502void WebRtcVoiceMediaChannel::OnReadyToSend(bool ready) {
2503 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
2504 LOG(LS_VERBOSE) << "OnReadyToSend: " << (ready ? "Ready." : "Not ready.");
2505 call_->SignalChannelNetworkState(
2506 webrtc::MediaType::AUDIO,
2507 ready ? webrtc::kNetworkUp : webrtc::kNetworkDown);
2508}
2509
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002510bool WebRtcVoiceMediaChannel::GetStats(VoiceMediaInfo* info) {
Peter Boströmca8b4042016-03-08 14:24:13 -08002511 TRACE_EVENT0("webrtc", "WebRtcVoiceMediaChannel::GetStats");
solenberg566ef242015-11-06 15:34:49 -08002512 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
solenberg85a04962015-10-27 03:35:21 -07002513 RTC_DCHECK(info);
solenbergd97ec302015-10-07 01:40:33 -07002514
solenberg85a04962015-10-27 03:35:21 -07002515 // Get SSRC and stats for each sender.
2516 RTC_DCHECK(info->senders.size() == 0);
2517 for (const auto& stream : send_streams_) {
2518 webrtc::AudioSendStream::Stats stats = stream.second->GetStats();
wu@webrtc.org9dba5252013-08-05 20:36:57 +00002519 VoiceSenderInfo sinfo;
solenberg85a04962015-10-27 03:35:21 -07002520 sinfo.add_ssrc(stats.local_ssrc);
2521 sinfo.bytes_sent = stats.bytes_sent;
2522 sinfo.packets_sent = stats.packets_sent;
2523 sinfo.packets_lost = stats.packets_lost;
2524 sinfo.fraction_lost = stats.fraction_lost;
2525 sinfo.codec_name = stats.codec_name;
2526 sinfo.ext_seqnum = stats.ext_seqnum;
2527 sinfo.jitter_ms = stats.jitter_ms;
2528 sinfo.rtt_ms = stats.rtt_ms;
2529 sinfo.audio_level = stats.audio_level;
2530 sinfo.aec_quality_min = stats.aec_quality_min;
2531 sinfo.echo_delay_median_ms = stats.echo_delay_median_ms;
2532 sinfo.echo_delay_std_ms = stats.echo_delay_std_ms;
2533 sinfo.echo_return_loss = stats.echo_return_loss;
2534 sinfo.echo_return_loss_enhancement = stats.echo_return_loss_enhancement;
Taylor Brandstetter1a018dc2016-03-08 12:37:39 -08002535 sinfo.typing_noise_detected = (send_ ? stats.typing_noise_detected : false);
wu@webrtc.org9dba5252013-08-05 20:36:57 +00002536 info->senders.push_back(sinfo);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002537 }
2538
solenberg85a04962015-10-27 03:35:21 -07002539 // Get SSRC and stats for each receiver.
2540 RTC_DCHECK(info->receivers.size() == 0);
solenberg7add0582015-11-20 09:59:34 -08002541 for (const auto& stream : recv_streams_) {
Fredrik Solenberg4f4ec0a2015-10-22 10:49:27 +02002542 webrtc::AudioReceiveStream::Stats stats = stream.second->GetStats();
2543 VoiceReceiverInfo rinfo;
2544 rinfo.add_ssrc(stats.remote_ssrc);
2545 rinfo.bytes_rcvd = stats.bytes_rcvd;
2546 rinfo.packets_rcvd = stats.packets_rcvd;
2547 rinfo.packets_lost = stats.packets_lost;
2548 rinfo.fraction_lost = stats.fraction_lost;
2549 rinfo.codec_name = stats.codec_name;
2550 rinfo.ext_seqnum = stats.ext_seqnum;
2551 rinfo.jitter_ms = stats.jitter_ms;
2552 rinfo.jitter_buffer_ms = stats.jitter_buffer_ms;
2553 rinfo.jitter_buffer_preferred_ms = stats.jitter_buffer_preferred_ms;
2554 rinfo.delay_estimate_ms = stats.delay_estimate_ms;
2555 rinfo.audio_level = stats.audio_level;
2556 rinfo.expand_rate = stats.expand_rate;
2557 rinfo.speech_expand_rate = stats.speech_expand_rate;
2558 rinfo.secondary_decoded_rate = stats.secondary_decoded_rate;
2559 rinfo.accelerate_rate = stats.accelerate_rate;
2560 rinfo.preemptive_expand_rate = stats.preemptive_expand_rate;
2561 rinfo.decoding_calls_to_silence_generator =
2562 stats.decoding_calls_to_silence_generator;
2563 rinfo.decoding_calls_to_neteq = stats.decoding_calls_to_neteq;
2564 rinfo.decoding_normal = stats.decoding_normal;
2565 rinfo.decoding_plc = stats.decoding_plc;
2566 rinfo.decoding_cng = stats.decoding_cng;
2567 rinfo.decoding_plc_cng = stats.decoding_plc_cng;
2568 rinfo.capture_start_ntp_time_ms = stats.capture_start_ntp_time_ms;
2569 info->receivers.push_back(rinfo);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002570 }
2571
2572 return true;
2573}
2574
Tommif888bb52015-12-12 01:37:01 +01002575void WebRtcVoiceMediaChannel::SetRawAudioSink(
2576 uint32_t ssrc,
kwiberg686a8ef2016-02-26 03:00:35 -08002577 std::unique_ptr<webrtc::AudioSinkInterface> sink) {
Tommif888bb52015-12-12 01:37:01 +01002578 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
deadbeef884f5852016-01-15 09:20:04 -08002579 LOG(LS_VERBOSE) << "WebRtcVoiceMediaChannel::SetRawAudioSink: ssrc:" << ssrc
2580 << " " << (sink ? "(ptr)" : "NULL");
2581 if (ssrc == 0) {
2582 if (default_recv_ssrc_ != -1) {
kwiberg686a8ef2016-02-26 03:00:35 -08002583 std::unique_ptr<webrtc::AudioSinkInterface> proxy_sink(
deadbeef884f5852016-01-15 09:20:04 -08002584 sink ? new ProxySink(sink.get()) : nullptr);
2585 SetRawAudioSink(default_recv_ssrc_, std::move(proxy_sink));
2586 }
2587 default_sink_ = std::move(sink);
2588 return;
2589 }
Tommif888bb52015-12-12 01:37:01 +01002590 const auto it = recv_streams_.find(ssrc);
2591 if (it == recv_streams_.end()) {
2592 LOG(LS_WARNING) << "SetRawAudioSink: no recv stream" << ssrc;
2593 return;
2594 }
deadbeef2d110be2016-01-13 12:00:26 -08002595 it->second->SetRawAudioSink(std::move(sink));
Tommif888bb52015-12-12 01:37:01 +01002596}
2597
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002598int WebRtcVoiceMediaChannel::GetOutputLevel(int channel) {
solenbergd97ec302015-10-07 01:40:33 -07002599 unsigned int ulevel = 0;
2600 int ret = engine()->voe()->volume()->GetSpeechOutputLevel(channel, ulevel);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002601 return (ret == 0) ? static_cast<int>(ulevel) : -1;
2602}
2603
Peter Boström0c4e06b2015-10-07 12:23:21 +02002604int WebRtcVoiceMediaChannel::GetReceiveChannelId(uint32_t ssrc) const {
solenberg566ef242015-11-06 15:34:49 -08002605 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
solenberg7add0582015-11-20 09:59:34 -08002606 const auto it = recv_streams_.find(ssrc);
2607 if (it != recv_streams_.end()) {
mallinath@webrtc.org67ee6b92014-02-03 16:57:16 +00002608 return it->second->channel();
solenberg8fb30c32015-10-13 03:06:58 -07002609 }
solenberg1ac56142015-10-13 03:58:19 -07002610 return -1;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002611}
2612
Peter Boström0c4e06b2015-10-07 12:23:21 +02002613int WebRtcVoiceMediaChannel::GetSendChannelId(uint32_t ssrc) const {
solenberg566ef242015-11-06 15:34:49 -08002614 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
solenbergc96df772015-10-21 13:01:53 -07002615 const auto it = send_streams_.find(ssrc);
2616 if (it != send_streams_.end()) {
mallinath@webrtc.org67ee6b92014-02-03 16:57:16 +00002617 return it->second->channel();
solenberg8fb30c32015-10-13 03:06:58 -07002618 }
wu@webrtc.org9dba5252013-08-05 20:36:57 +00002619 return -1;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002620}
2621
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002622bool WebRtcVoiceMediaChannel::SetPlayout(int channel, bool playout) {
2623 if (playout) {
2624 LOG(LS_INFO) << "Starting playout for channel #" << channel;
2625 if (engine()->voe()->base()->StartPlayout(channel) == -1) {
2626 LOG_RTCERR1(StartPlayout, channel);
2627 return false;
2628 }
2629 } else {
2630 LOG(LS_INFO) << "Stopping playout for channel #" << channel;
2631 engine()->voe()->base()->StopPlayout(channel);
2632 }
2633 return true;
2634}
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002635} // namespace cricket
2636
2637#endif // HAVE_WEBRTC_VOICE