blob: 721990a036bdf95a30f3013bfc5872d5af2147eb [file] [log] [blame]
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001/*
2 * libjingle
3 * Copyright 2004 Google Inc.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions are met:
7 *
8 * 1. Redistributions of source code must retain the above copyright notice,
9 * this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright notice,
11 * this list of conditions and the following disclaimer in the documentation
12 * and/or other materials provided with the distribution.
13 * 3. The name of the author may not be used to endorse or promote products
14 * derived from this software without specific prior written permission.
15 *
16 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED
17 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
18 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO
19 * EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
20 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
21 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
22 * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
23 * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
24 * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
25 * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26 */
27
28#ifdef HAVE_CONFIG_H
29#include <config.h>
30#endif
31
32#ifdef HAVE_WEBRTC_VOICE
33
34#include "talk/media/webrtc/webrtcvoiceengine.h"
35
36#include <algorithm>
37#include <cstdio>
38#include <string>
39#include <vector>
40
Thiago Farinaef883092015-04-06 10:36:41 +000041#include "talk/media/base/audioframe.h"
buildbot@webrtc.orga09a9992014-08-13 17:26:08 +000042#include "talk/media/base/audiorenderer.h"
43#include "talk/media/base/constants.h"
44#include "talk/media/base/streamparams.h"
buildbot@webrtc.orga09a9992014-08-13 17:26:08 +000045#include "talk/media/webrtc/webrtcvoe.h"
tfarina5237aaf2015-11-10 23:44:30 -080046#include "webrtc/base/arraysize.h"
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +000047#include "webrtc/base/base64.h"
48#include "webrtc/base/byteorder.h"
49#include "webrtc/base/common.h"
50#include "webrtc/base/helpers.h"
51#include "webrtc/base/logging.h"
52#include "webrtc/base/stringencode.h"
53#include "webrtc/base/stringutils.h"
ivoc112a3d82015-10-16 02:22:18 -070054#include "webrtc/call/rtc_event_log.h"
mallinath@webrtc.orga27be8e2013-09-27 23:04:10 +000055#include "webrtc/common.h"
henrike@webrtc.org28e20752013-07-10 00:45:36 +000056#include "webrtc/modules/audio_processing/include/audio_processing.h"
Henrik Kjellander98f53512015-10-28 18:17:40 +010057#include "webrtc/system_wrappers/include/field_trial.h"
solenbergbd138382015-11-20 16:08:07 -080058#include "webrtc/system_wrappers/include/trace.h"
henrike@webrtc.org28e20752013-07-10 00:45:36 +000059
henrike@webrtc.org28e20752013-07-10 00:45:36 +000060namespace cricket {
solenbergd97ec302015-10-07 01:40:33 -070061namespace {
henrike@webrtc.org28e20752013-07-10 00:45:36 +000062
solenbergbd138382015-11-20 16:08:07 -080063const int kDefaultTraceFilter = webrtc::kTraceNone | webrtc::kTraceTerseInfo |
64 webrtc::kTraceWarning | webrtc::kTraceError |
65 webrtc::kTraceCritical;
66const int kElevatedTraceFilter = kDefaultTraceFilter | webrtc::kTraceStateInfo |
67 webrtc::kTraceInfo;
68
solenbergd97ec302015-10-07 01:40:33 -070069const int kMaxNumPacketSize = 6;
henrike@webrtc.org28e20752013-07-10 00:45:36 +000070struct CodecPref {
71 const char* name;
72 int clockrate;
73 int channels;
74 int payload_type;
75 bool is_multi_rate;
Brave Yao5225dd82015-03-26 07:39:19 +080076 int packet_sizes_ms[kMaxNumPacketSize];
henrike@webrtc.org28e20752013-07-10 00:45:36 +000077};
Brave Yao5225dd82015-03-26 07:39:19 +080078// Note: keep the supported packet sizes in ascending order.
solenbergd97ec302015-10-07 01:40:33 -070079const CodecPref kCodecPrefs[] = {
Brave Yao5225dd82015-03-26 07:39:19 +080080 { kOpusCodecName, 48000, 2, 111, true, { 10, 20, 40, 60 } },
81 { kIsacCodecName, 16000, 1, 103, true, { 30, 60 } },
82 { kIsacCodecName, 32000, 1, 104, true, { 30 } },
henrik.lundin@webrtc.org8038d422014-11-11 08:38:24 +000083 // G722 should be advertised as 8000 Hz because of the RFC "bug".
Brave Yao5225dd82015-03-26 07:39:19 +080084 { kG722CodecName, 8000, 1, 9, false, { 10, 20, 30, 40, 50, 60 } },
85 { kIlbcCodecName, 8000, 1, 102, false, { 20, 30, 40, 60 } },
86 { kPcmuCodecName, 8000, 1, 0, false, { 10, 20, 30, 40, 50, 60 } },
87 { kPcmaCodecName, 8000, 1, 8, false, { 10, 20, 30, 40, 50, 60 } },
Brave Yao5225dd82015-03-26 07:39:19 +080088 { kCnCodecName, 32000, 1, 106, false, { } },
89 { kCnCodecName, 16000, 1, 105, false, { } },
90 { kCnCodecName, 8000, 1, 13, false, { } },
91 { kRedCodecName, 8000, 1, 127, false, { } },
92 { kDtmfCodecName, 8000, 1, 126, false, { } },
henrike@webrtc.org28e20752013-07-10 00:45:36 +000093};
94
95// For Linux/Mac, using the default device is done by specifying index 0 for
96// VoE 4.0 and not -1 (which was the case for VoE 3.5).
97//
98// On Windows Vista and newer, Microsoft introduced the concept of "Default
99// Communications Device". This means that there are two types of default
100// devices (old Wave Audio style default and Default Communications Device).
101//
102// On Windows systems which only support Wave Audio style default, uses either
103// -1 or 0 to select the default device.
104//
105// On Windows systems which support both "Default Communication Device" and
106// old Wave Audio style default, use -1 for Default Communications Device and
107// -2 for Wave Audio style default, which is what we want to use for clips.
108// It's not clear yet whether the -2 index is handled properly on other OSes.
109
110#ifdef WIN32
solenbergd97ec302015-10-07 01:40:33 -0700111const int kDefaultAudioDeviceId = -1;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000112#else
solenbergd97ec302015-10-07 01:40:33 -0700113const int kDefaultAudioDeviceId = 0;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000114#endif
115
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000116// Parameter used for NACK.
117// This value is equivalent to 5 seconds of audio data at 20 ms per packet.
solenbergd97ec302015-10-07 01:40:33 -0700118const int kNackMaxPackets = 250;
minyue@webrtc.org2dc6f312014-10-31 05:33:10 +0000119
120// Codec parameters for Opus.
henrike@webrtc.org1e09a712013-07-26 19:17:59 +0000121// draft-spittka-payload-rtp-opus-03
minyue@webrtc.org2dc6f312014-10-31 05:33:10 +0000122
123// Recommended bitrates:
124// 8-12 kb/s for NB speech,
125// 16-20 kb/s for WB speech,
126// 28-40 kb/s for FB speech,
127// 48-64 kb/s for FB mono music, and
128// 64-128 kb/s for FB stereo music.
129// The current implementation applies the following values to mono signals,
130// and multiplies them by 2 for stereo.
solenbergd97ec302015-10-07 01:40:33 -0700131const int kOpusBitrateNb = 12000;
132const int kOpusBitrateWb = 20000;
133const int kOpusBitrateFb = 32000;
minyue@webrtc.org2dc6f312014-10-31 05:33:10 +0000134
henrike@webrtc.org1e09a712013-07-26 19:17:59 +0000135// Opus bitrate should be in the range between 6000 and 510000.
solenbergd97ec302015-10-07 01:40:33 -0700136const int kOpusMinBitrate = 6000;
137const int kOpusMaxBitrate = 510000;
buildbot@webrtc.org5d639b32014-09-10 07:57:12 +0000138
wu@webrtc.orgde305012013-10-31 15:40:38 +0000139// Default audio dscp value.
140// See http://tools.ietf.org/html/rfc2474 for details.
141// See also http://tools.ietf.org/html/draft-jennings-rtcweb-qos-00
solenbergd97ec302015-10-07 01:40:33 -0700142const rtc::DiffServCodePoint kAudioDscpValue = rtc::DSCP_EF;
henrike@webrtc.org1e09a712013-07-26 19:17:59 +0000143
sergeyu@chromium.orga59696b2013-09-13 23:48:58 +0000144// Ensure we open the file in a writeable path on ChromeOS and Android. This
145// workaround can be removed when it's possible to specify a filename for audio
146// option based AEC dumps.
henrike@webrtc.org1e09a712013-07-26 19:17:59 +0000147//
148// TODO(grunell): Use a string in the options instead of hardcoding it here
149// and let the embedder choose the filename (crbug.com/264223).
150//
sergeyu@chromium.orga59696b2013-09-13 23:48:58 +0000151// NOTE(ajm): Don't use hardcoded paths on platforms not explicitly specified
152// below.
153#if defined(CHROMEOS)
solenbergd97ec302015-10-07 01:40:33 -0700154const char kAecDumpByAudioOptionFilename[] = "/tmp/audio.aecdump";
sergeyu@chromium.orga59696b2013-09-13 23:48:58 +0000155#elif defined(ANDROID)
solenbergd97ec302015-10-07 01:40:33 -0700156const char kAecDumpByAudioOptionFilename[] = "/sdcard/audio.aecdump";
henrike@webrtc.org1e09a712013-07-26 19:17:59 +0000157#else
solenbergd97ec302015-10-07 01:40:33 -0700158const char kAecDumpByAudioOptionFilename[] = "audio.aecdump";
henrike@webrtc.org1e09a712013-07-26 19:17:59 +0000159#endif
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000160
solenberg0b675462015-10-09 01:37:09 -0700161bool ValidateStreamParams(const StreamParams& sp) {
162 if (sp.ssrcs.empty()) {
163 LOG(LS_ERROR) << "No SSRCs in stream parameters: " << sp.ToString();
164 return false;
165 }
166 if (sp.ssrcs.size() > 1) {
167 LOG(LS_ERROR) << "Multiple SSRCs in stream parameters: " << sp.ToString();
168 return false;
169 }
170 return true;
171}
172
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000173// Dumps an AudioCodec in RFC 2327-ish format.
solenbergd97ec302015-10-07 01:40:33 -0700174std::string ToString(const AudioCodec& codec) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000175 std::stringstream ss;
176 ss << codec.name << "/" << codec.clockrate << "/" << codec.channels
177 << " (" << codec.id << ")";
178 return ss.str();
179}
Minyue Li7100dcd2015-03-27 05:05:59 +0100180
solenbergd97ec302015-10-07 01:40:33 -0700181std::string ToString(const webrtc::CodecInst& codec) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000182 std::stringstream ss;
183 ss << codec.plname << "/" << codec.plfreq << "/" << codec.channels
184 << " (" << codec.pltype << ")";
185 return ss.str();
186}
187
solenbergd97ec302015-10-07 01:40:33 -0700188void LogMultiline(rtc::LoggingSeverity sev, char* text) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000189 const char* delim = "\r\n";
190 for (char* tok = strtok(text, delim); tok; tok = strtok(NULL, delim)) {
191 LOG_V(sev) << tok;
192 }
193}
194
solenbergd97ec302015-10-07 01:40:33 -0700195bool IsCodec(const AudioCodec& codec, const char* ref_name) {
Minyue Li7100dcd2015-03-27 05:05:59 +0100196 return (_stricmp(codec.name.c_str(), ref_name) == 0);
197}
198
solenbergd97ec302015-10-07 01:40:33 -0700199bool IsCodec(const webrtc::CodecInst& codec, const char* ref_name) {
Minyue Li7100dcd2015-03-27 05:05:59 +0100200 return (_stricmp(codec.plname, ref_name) == 0);
201}
202
solenbergd97ec302015-10-07 01:40:33 -0700203bool IsCodecMultiRate(const webrtc::CodecInst& codec) {
tfarina5237aaf2015-11-10 23:44:30 -0800204 for (size_t i = 0; i < arraysize(kCodecPrefs); ++i) {
Minyue Li7100dcd2015-03-27 05:05:59 +0100205 if (IsCodec(codec, kCodecPrefs[i].name) &&
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000206 kCodecPrefs[i].clockrate == codec.plfreq) {
207 return kCodecPrefs[i].is_multi_rate;
208 }
209 }
210 return false;
211}
212
solenbergd97ec302015-10-07 01:40:33 -0700213bool FindCodec(const std::vector<AudioCodec>& codecs,
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000214 const AudioCodec& codec,
215 AudioCodec* found_codec) {
Fredrik Solenbergaf9fb212015-08-26 10:45:53 +0200216 for (const AudioCodec& c : codecs) {
217 if (c.Matches(codec)) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000218 if (found_codec != NULL) {
Fredrik Solenbergaf9fb212015-08-26 10:45:53 +0200219 *found_codec = c;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000220 }
221 return true;
222 }
223 }
224 return false;
225}
wu@webrtc.org1d1ffc92013-10-16 18:12:02 +0000226
solenberg0b675462015-10-09 01:37:09 -0700227bool VerifyUniquePayloadTypes(const std::vector<AudioCodec>& codecs) {
228 if (codecs.empty()) {
229 return true;
230 }
231 std::vector<int> payload_types;
232 for (const AudioCodec& codec : codecs) {
233 payload_types.push_back(codec.id);
234 }
235 std::sort(payload_types.begin(), payload_types.end());
236 auto it = std::unique(payload_types.begin(), payload_types.end());
237 return it == payload_types.end();
238}
239
solenbergd97ec302015-10-07 01:40:33 -0700240bool IsNackEnabled(const AudioCodec& codec) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000241 return codec.HasFeedbackParam(FeedbackParam(kRtcpFbParamNack,
242 kParamValueEmpty));
243}
244
solenbergd97ec302015-10-07 01:40:33 -0700245int SelectPacketSize(const CodecPref& codec_pref, int ptime_ms) {
Brave Yao5225dd82015-03-26 07:39:19 +0800246 int selected_packet_size_ms = codec_pref.packet_sizes_ms[0];
247 for (int packet_size_ms : codec_pref.packet_sizes_ms) {
248 if (packet_size_ms && packet_size_ms <= ptime_ms) {
249 selected_packet_size_ms = packet_size_ms;
250 }
251 }
252 return selected_packet_size_ms;
253}
254
255// If the AudioCodec param kCodecParamPTime is set, then we will set it to codec
256// pacsize if it's valid, or we will pick the next smallest value we support.
257// TODO(Brave): Query supported packet sizes from ACM when the API is ready.
solenbergd97ec302015-10-07 01:40:33 -0700258bool SetPTimeAsPacketSize(webrtc::CodecInst* codec, int ptime_ms) {
Brave Yao5225dd82015-03-26 07:39:19 +0800259 for (const CodecPref& codec_pref : kCodecPrefs) {
Minyue Li7100dcd2015-03-27 05:05:59 +0100260 if ((IsCodec(*codec, codec_pref.name) &&
Brave Yao5225dd82015-03-26 07:39:19 +0800261 codec_pref.clockrate == codec->plfreq) ||
Minyue Li7100dcd2015-03-27 05:05:59 +0100262 IsCodec(*codec, kG722CodecName)) {
Brave Yao5225dd82015-03-26 07:39:19 +0800263 int packet_size_ms = SelectPacketSize(codec_pref, ptime_ms);
264 if (packet_size_ms) {
265 // Convert unit from milli-seconds to samples.
266 codec->pacsize = (codec->plfreq / 1000) * packet_size_ms;
267 return true;
268 }
269 }
270 }
271 return false;
272}
273
Minyue Li7100dcd2015-03-27 05:05:59 +0100274// Return true if codec.params[feature] == "1", false otherwise.
solenbergd97ec302015-10-07 01:40:33 -0700275bool IsCodecFeatureEnabled(const AudioCodec& codec,
Minyue Li7100dcd2015-03-27 05:05:59 +0100276 const char* feature) {
277 int value;
278 return codec.GetParam(feature, &value) && value == 1;
279}
280
281// Use params[kCodecParamMaxAverageBitrate] if it is defined, use codec.bitrate
282// otherwise. If the value (either from params or codec.bitrate) <=0, use the
283// default configuration. If the value is beyond feasible bit rate of Opus,
284// clamp it. Returns the Opus bit rate for operation.
solenbergd97ec302015-10-07 01:40:33 -0700285int GetOpusBitrate(const AudioCodec& codec, int max_playback_rate) {
Minyue Li7100dcd2015-03-27 05:05:59 +0100286 int bitrate = 0;
287 bool use_param = true;
288 if (!codec.GetParam(kCodecParamMaxAverageBitrate, &bitrate)) {
289 bitrate = codec.bitrate;
290 use_param = false;
291 }
292 if (bitrate <= 0) {
293 if (max_playback_rate <= 8000) {
294 bitrate = kOpusBitrateNb;
295 } else if (max_playback_rate <= 16000) {
296 bitrate = kOpusBitrateWb;
297 } else {
298 bitrate = kOpusBitrateFb;
299 }
300
301 if (IsCodecFeatureEnabled(codec, kCodecParamStereo)) {
302 bitrate *= 2;
303 }
304 } else if (bitrate < kOpusMinBitrate || bitrate > kOpusMaxBitrate) {
305 bitrate = (bitrate < kOpusMinBitrate) ? kOpusMinBitrate : kOpusMaxBitrate;
306 std::string rate_source =
307 use_param ? "Codec parameter \"maxaveragebitrate\"" :
308 "Supplied Opus bitrate";
309 LOG(LS_WARNING) << rate_source
310 << " is invalid and is replaced by: "
311 << bitrate;
312 }
313 return bitrate;
314}
315
316// Returns kOpusDefaultPlaybackRate if params[kCodecParamMaxPlaybackRate] is not
317// defined. Returns the value of params[kCodecParamMaxPlaybackRate] otherwise.
solenbergd97ec302015-10-07 01:40:33 -0700318int GetOpusMaxPlaybackRate(const AudioCodec& codec) {
Minyue Li7100dcd2015-03-27 05:05:59 +0100319 int value;
320 if (codec.GetParam(kCodecParamMaxPlaybackRate, &value)) {
321 return value;
322 }
323 return kOpusDefaultMaxPlaybackRate;
324}
325
solenbergd97ec302015-10-07 01:40:33 -0700326void GetOpusConfig(const AudioCodec& codec, webrtc::CodecInst* voe_codec,
Minyue Li7100dcd2015-03-27 05:05:59 +0100327 bool* enable_codec_fec, int* max_playback_rate,
328 bool* enable_codec_dtx) {
329 *enable_codec_fec = IsCodecFeatureEnabled(codec, kCodecParamUseInbandFec);
330 *enable_codec_dtx = IsCodecFeatureEnabled(codec, kCodecParamUseDtx);
331 *max_playback_rate = GetOpusMaxPlaybackRate(codec);
332
333 // If OPUS, change what we send according to the "stereo" codec
334 // parameter, and not the "channels" parameter. We set
335 // voe_codec.channels to 2 if "stereo=1" and 1 otherwise. If
336 // the bitrate is not specified, i.e. is <= zero, we set it to the
337 // appropriate default value for mono or stereo Opus.
338
339 voe_codec->channels = IsCodecFeatureEnabled(codec, kCodecParamStereo) ? 2 : 1;
340 voe_codec->rate = GetOpusBitrate(codec, *max_playback_rate);
341}
342
343// Changes RTP timestamp rate of G722. This is due to the "bug" in the RFC
344// which says that G722 should be advertised as 8 kHz although it is a 16 kHz
345// codec.
solenbergd97ec302015-10-07 01:40:33 -0700346void MaybeFixupG722(webrtc::CodecInst* voe_codec, int new_plfreq) {
Minyue Li7100dcd2015-03-27 05:05:59 +0100347 if (IsCodec(*voe_codec, kG722CodecName)) {
348 // If the ASSERT triggers, the codec definition in WebRTC VoiceEngine
349 // has changed, and this special case is no longer needed.
henrikg91d6ede2015-09-17 00:24:34 -0700350 RTC_DCHECK(voe_codec->plfreq != new_plfreq);
Minyue Li7100dcd2015-03-27 05:05:59 +0100351 voe_codec->plfreq = new_plfreq;
352 }
353}
354
wu@webrtc.org1d1ffc92013-10-16 18:12:02 +0000355// Gets the default set of options applied to the engine. Historically, these
356// were supplied as a combination of flags from the channel manager (ec, agc,
357// ns, and highpass) and the rest hardcoded in InitInternal.
solenbergd97ec302015-10-07 01:40:33 -0700358AudioOptions GetDefaultEngineOptions() {
wu@webrtc.org1d1ffc92013-10-16 18:12:02 +0000359 AudioOptions options;
Karl Wibergbe579832015-11-10 22:34:18 +0100360 options.echo_cancellation = rtc::Optional<bool>(true);
361 options.auto_gain_control = rtc::Optional<bool>(true);
362 options.noise_suppression = rtc::Optional<bool>(true);
363 options.highpass_filter = rtc::Optional<bool>(true);
364 options.stereo_swapping = rtc::Optional<bool>(false);
365 options.audio_jitter_buffer_max_packets = rtc::Optional<int>(50);
366 options.audio_jitter_buffer_fast_accelerate = rtc::Optional<bool>(false);
367 options.typing_detection = rtc::Optional<bool>(true);
368 options.adjust_agc_delta = rtc::Optional<int>(0);
369 options.experimental_agc = rtc::Optional<bool>(false);
370 options.extended_filter_aec = rtc::Optional<bool>(false);
371 options.delay_agnostic_aec = rtc::Optional<bool>(false);
372 options.experimental_ns = rtc::Optional<bool>(false);
373 options.aec_dump = rtc::Optional<bool>(false);
wu@webrtc.org1d1ffc92013-10-16 18:12:02 +0000374 return options;
375}
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000376
solenberg566ef242015-11-06 15:34:49 -0800377webrtc::AudioState::Config MakeAudioStateConfig(VoEWrapper* voe_wrapper) {
378 webrtc::AudioState::Config config;
379 config.voice_engine = voe_wrapper->engine();
380 return config;
381}
382
solenberg3a941542015-11-16 07:34:50 -0800383std::vector<webrtc::RtpExtension> FindAudioRtpHeaderExtensions(
384 const std::vector<RtpHeaderExtension>& extensions) {
385 std::vector<webrtc::RtpExtension> result;
386 for (const auto& extension : extensions) {
387 if (extension.uri == kRtpAbsoluteSenderTimeHeaderExtension ||
388 extension.uri == kRtpAudioLevelHeaderExtension) {
389 result.push_back({extension.uri, extension.id});
390 } else {
391 LOG(LS_WARNING) << "Unsupported RTP extension: " << extension.ToString();
392 }
393 }
394 return result;
395}
solenbergd97ec302015-10-07 01:40:33 -0700396} // namespace {
Brave Yao5225dd82015-03-26 07:39:19 +0800397
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000398WebRtcVoiceEngine::WebRtcVoiceEngine()
399 : voe_wrapper_(new VoEWrapper()),
solenbergbd138382015-11-20 16:08:07 -0800400 audio_state_(webrtc::AudioState::Create(MakeAudioStateConfig(voe()))) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000401 Construct();
402}
403
solenbergbd138382015-11-20 16:08:07 -0800404WebRtcVoiceEngine::WebRtcVoiceEngine(VoEWrapper* voe_wrapper)
405 : voe_wrapper_(voe_wrapper) {
buildbot@webrtc.org13d67762014-05-02 17:33:29 +0000406 Construct();
407}
408
409void WebRtcVoiceEngine::Construct() {
solenberg566ef242015-11-06 15:34:49 -0800410 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
solenbergbd138382015-11-20 16:08:07 -0800411 LOG(LS_VERBOSE) << "WebRtcVoiceEngine::WebRtcVoiceEngine";
412
solenberg566ef242015-11-06 15:34:49 -0800413 signal_thread_checker_.DetachFromThread();
414 std::memset(&default_agc_config_, 0, sizeof(default_agc_config_));
solenbergbd138382015-11-20 16:08:07 -0800415
416 webrtc::Trace::set_level_filter(kDefaultTraceFilter);
417 webrtc::Trace::SetTraceCallback(this);
buildbot@webrtc.org13d67762014-05-02 17:33:29 +0000418
419 // Load our audio codec list.
420 ConstructCodecs();
421
422 // Load our RTP Header extensions.
423 rtp_header_extensions_.push_back(
424 RtpHeaderExtension(kRtpAudioLevelHeaderExtension,
425 kRtpAudioLevelHeaderExtensionDefaultId));
426 rtp_header_extensions_.push_back(
427 RtpHeaderExtension(kRtpAbsoluteSenderTimeHeaderExtension,
428 kRtpAbsoluteSenderTimeHeaderExtensionDefaultId));
stefanc1aeaf02015-10-15 07:26:07 -0700429 if (webrtc::field_trial::FindFullName("WebRTC-SendSideBwe") == "Enabled") {
430 rtp_header_extensions_.push_back(RtpHeaderExtension(
431 kRtpTransportSequenceNumberHeaderExtension,
432 kRtpTransportSequenceNumberHeaderExtensionDefaultId));
433 }
buildbot@webrtc.org13d67762014-05-02 17:33:29 +0000434 options_ = GetDefaultEngineOptions();
435}
436
buildbot@webrtc.org13d67762014-05-02 17:33:29 +0000437void WebRtcVoiceEngine::ConstructCodecs() {
solenberg566ef242015-11-06 15:34:49 -0800438 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
buildbot@webrtc.org13d67762014-05-02 17:33:29 +0000439 LOG(LS_INFO) << "WebRtc VoiceEngine codecs:";
440 int ncodecs = voe_wrapper_->codec()->NumOfCodecs();
441 for (int i = 0; i < ncodecs; ++i) {
442 webrtc::CodecInst voe_codec;
henrik.lundin@webrtc.org8038d422014-11-11 08:38:24 +0000443 if (GetVoeCodec(i, &voe_codec)) {
buildbot@webrtc.org13d67762014-05-02 17:33:29 +0000444 // Skip uncompressed formats.
Minyue Li7100dcd2015-03-27 05:05:59 +0100445 if (IsCodec(voe_codec, kL16CodecName)) {
buildbot@webrtc.org13d67762014-05-02 17:33:29 +0000446 continue;
447 }
448
449 const CodecPref* pref = NULL;
tfarina5237aaf2015-11-10 23:44:30 -0800450 for (size_t j = 0; j < arraysize(kCodecPrefs); ++j) {
Minyue Li7100dcd2015-03-27 05:05:59 +0100451 if (IsCodec(voe_codec, kCodecPrefs[j].name) &&
buildbot@webrtc.org13d67762014-05-02 17:33:29 +0000452 kCodecPrefs[j].clockrate == voe_codec.plfreq &&
453 kCodecPrefs[j].channels == voe_codec.channels) {
454 pref = &kCodecPrefs[j];
455 break;
456 }
457 }
458
459 if (pref) {
460 // Use the payload type that we've configured in our pref table;
461 // use the offset in our pref table to determine the sort order.
tfarina5237aaf2015-11-10 23:44:30 -0800462 AudioCodec codec(
463 pref->payload_type, voe_codec.plname, voe_codec.plfreq,
464 voe_codec.rate, voe_codec.channels,
465 static_cast<int>(arraysize(kCodecPrefs)) - (pref - kCodecPrefs));
buildbot@webrtc.org13d67762014-05-02 17:33:29 +0000466 LOG(LS_INFO) << ToString(codec);
Minyue Li7100dcd2015-03-27 05:05:59 +0100467 if (IsCodec(codec, kIsacCodecName)) {
minyue@webrtc.org26236952014-10-29 02:27:08 +0000468 // Indicate auto-bitrate in signaling.
buildbot@webrtc.org13d67762014-05-02 17:33:29 +0000469 codec.bitrate = 0;
470 }
Minyue Li7100dcd2015-03-27 05:05:59 +0100471 if (IsCodec(codec, kOpusCodecName)) {
buildbot@webrtc.org13d67762014-05-02 17:33:29 +0000472 // Only add fmtp parameters that differ from the spec.
473 if (kPreferredMinPTime != kOpusDefaultMinPTime) {
474 codec.params[kCodecParamMinPTime] =
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000475 rtc::ToString(kPreferredMinPTime);
buildbot@webrtc.org13d67762014-05-02 17:33:29 +0000476 }
477 if (kPreferredMaxPTime != kOpusDefaultMaxPTime) {
478 codec.params[kCodecParamMaxPTime] =
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000479 rtc::ToString(kPreferredMaxPTime);
buildbot@webrtc.org13d67762014-05-02 17:33:29 +0000480 }
pkasting@chromium.orgd3245462015-02-23 21:28:22 +0000481 codec.SetParam(kCodecParamUseInbandFec, 1);
minyue@webrtc.org4ef22d12014-11-17 09:26:39 +0000482
483 // TODO(hellner): Add ptime, sprop-stereo, and stereo
buildbot@webrtc.org13d67762014-05-02 17:33:29 +0000484 // when they can be set to values other than the default.
485 }
486 codecs_.push_back(codec);
487 } else {
488 LOG(LS_WARNING) << "Unexpected codec: " << ToString(voe_codec);
489 }
490 }
491 }
492 // Make sure they are in local preference order.
493 std::sort(codecs_.begin(), codecs_.end(), &AudioCodec::Preferable);
494}
495
henrik.lundin@webrtc.org8038d422014-11-11 08:38:24 +0000496bool WebRtcVoiceEngine::GetVoeCodec(int index, webrtc::CodecInst* codec) {
solenberg566ef242015-11-06 15:34:49 -0800497 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
henrik.lundin@webrtc.org8038d422014-11-11 08:38:24 +0000498 if (voe_wrapper_->codec()->GetCodec(index, *codec) == -1) {
499 return false;
henrik.lundin@webrtc.orgf85dbce2014-11-07 12:25:00 +0000500 }
henrik.lundin@webrtc.org8038d422014-11-11 08:38:24 +0000501 // Change the sample rate of G722 to 8000 to match SDP.
502 MaybeFixupG722(codec, 8000);
503 return true;
henrik.lundin@webrtc.orgf85dbce2014-11-07 12:25:00 +0000504}
505
buildbot@webrtc.org13d67762014-05-02 17:33:29 +0000506WebRtcVoiceEngine::~WebRtcVoiceEngine() {
solenberg566ef242015-11-06 15:34:49 -0800507 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
buildbot@webrtc.org13d67762014-05-02 17:33:29 +0000508 LOG(LS_VERBOSE) << "WebRtcVoiceEngine::~WebRtcVoiceEngine";
buildbot@webrtc.org13d67762014-05-02 17:33:29 +0000509 if (adm_) {
510 voe_wrapper_.reset();
511 adm_->Release();
512 adm_ = NULL;
513 }
solenbergbd138382015-11-20 16:08:07 -0800514 webrtc::Trace::SetTraceCallback(nullptr);
buildbot@webrtc.org13d67762014-05-02 17:33:29 +0000515}
516
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000517bool WebRtcVoiceEngine::Init(rtc::Thread* worker_thread) {
solenberg566ef242015-11-06 15:34:49 -0800518 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
henrikg91d6ede2015-09-17 00:24:34 -0700519 RTC_DCHECK(worker_thread == rtc::Thread::Current());
buildbot@webrtc.org13d67762014-05-02 17:33:29 +0000520 LOG(LS_INFO) << "WebRtcVoiceEngine::Init";
521 bool res = InitInternal();
522 if (res) {
523 LOG(LS_INFO) << "WebRtcVoiceEngine::Init Done!";
524 } else {
525 LOG(LS_ERROR) << "WebRtcVoiceEngine::Init failed";
526 Terminate();
527 }
528 return res;
529}
530
531bool WebRtcVoiceEngine::InitInternal() {
solenberg566ef242015-11-06 15:34:49 -0800532 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
buildbot@webrtc.org13d67762014-05-02 17:33:29 +0000533 // Temporarily turn logging level up for the Init call
solenbergbd138382015-11-20 16:08:07 -0800534 webrtc::Trace::set_level_filter(kElevatedTraceFilter);
buildbot@webrtc.org13d67762014-05-02 17:33:29 +0000535 if (voe_wrapper_->base()->Init(adm_) == -1) {
536 LOG_RTCERR0_EX(Init, voe_wrapper_->error());
buildbot@webrtc.org13d67762014-05-02 17:33:29 +0000537 return false;
538 }
solenbergbd138382015-11-20 16:08:07 -0800539 webrtc::Trace::set_level_filter(kDefaultTraceFilter);
buildbot@webrtc.org13d67762014-05-02 17:33:29 +0000540
541 // Log the VoiceEngine version info
542 char buffer[1024] = "";
543 voe_wrapper_->base()->GetVersion(buffer);
544 LOG(LS_INFO) << "WebRtc VoiceEngine Version:";
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000545 LogMultiline(rtc::LS_INFO, buffer);
buildbot@webrtc.org13d67762014-05-02 17:33:29 +0000546
547 // Save the default AGC configuration settings. This must happen before
548 // calling SetOptions or the default will be overwritten.
549 if (voe_wrapper_->processing()->GetAgcConfig(default_agc_config_) == -1) {
550 LOG_RTCERR0(GetAgcConfig);
551 return false;
552 }
553
554 // Set defaults for options, so that ApplyOptions applies them explicitly
555 // when we clear option (channel) overrides. External clients can still
556 // modify the defaults via SetOptions (on the media engine).
557 if (!SetOptions(GetDefaultEngineOptions())) {
558 return false;
559 }
560
561 // Print our codec list again for the call diagnostic log
562 LOG(LS_INFO) << "WebRtc VoiceEngine codecs:";
Fredrik Solenbergaf9fb212015-08-26 10:45:53 +0200563 for (const AudioCodec& codec : codecs_) {
564 LOG(LS_INFO) << ToString(codec);
buildbot@webrtc.org13d67762014-05-02 17:33:29 +0000565 }
566
567 // Disable the DTMF playout when a tone is sent.
568 // PlayDtmfTone will be used if local playout is needed.
569 if (voe_wrapper_->dtmf()->SetDtmfFeedbackStatus(false) == -1) {
570 LOG_RTCERR1(SetDtmfFeedbackStatus, false);
571 }
572
573 initialized_ = true;
574 return true;
575}
576
buildbot@webrtc.org13d67762014-05-02 17:33:29 +0000577void WebRtcVoiceEngine::Terminate() {
solenberg566ef242015-11-06 15:34:49 -0800578 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
buildbot@webrtc.org13d67762014-05-02 17:33:29 +0000579 LOG(LS_INFO) << "WebRtcVoiceEngine::Terminate";
580 initialized_ = false;
581
582 StopAecDump();
583
buildbot@webrtc.org13d67762014-05-02 17:33:29 +0000584 voe_wrapper_->base()->Terminate();
buildbot@webrtc.org13d67762014-05-02 17:33:29 +0000585}
586
solenberg566ef242015-11-06 15:34:49 -0800587rtc::scoped_refptr<webrtc::AudioState>
588 WebRtcVoiceEngine::GetAudioState() const {
589 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
590 return audio_state_;
591}
592
Fredrik Solenberg709ed672015-09-15 12:26:33 +0200593VoiceMediaChannel* WebRtcVoiceEngine::CreateChannel(webrtc::Call* call,
Jelena Marusicc28a8962015-05-29 15:05:44 +0200594 const AudioOptions& options) {
solenberg566ef242015-11-06 15:34:49 -0800595 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
solenberg0a617e22015-10-20 15:49:38 -0700596 return new WebRtcVoiceMediaChannel(this, options, call);
buildbot@webrtc.org13d67762014-05-02 17:33:29 +0000597}
598
buildbot@webrtc.org13d67762014-05-02 17:33:29 +0000599bool WebRtcVoiceEngine::SetOptions(const AudioOptions& options) {
solenberg566ef242015-11-06 15:34:49 -0800600 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
buildbot@webrtc.org13d67762014-05-02 17:33:29 +0000601 if (!ApplyOptions(options)) {
602 return false;
603 }
604 options_ = options;
605 return true;
606}
607
buildbot@webrtc.org13d67762014-05-02 17:33:29 +0000608// AudioOptions defaults are set in InitInternal (for options with corresponding
609// MediaEngineInterface flags) and in SetOptions(int) for flagless options.
610bool WebRtcVoiceEngine::ApplyOptions(const AudioOptions& options_in) {
solenberg566ef242015-11-06 15:34:49 -0800611 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
henrikac14f5ff2015-09-23 14:08:33 +0200612 LOG(LS_INFO) << "ApplyOptions: " << options_in.ToString();
buildbot@webrtc.org13d67762014-05-02 17:33:29 +0000613 AudioOptions options = options_in; // The options are modified below.
614 // kEcConference is AEC with high suppression.
615 webrtc::EcModes ec_mode = webrtc::kEcConference;
616 webrtc::AecmModes aecm_mode = webrtc::kAecmSpeakerphone;
617 webrtc::AgcModes agc_mode = webrtc::kAgcAdaptiveAnalog;
618 webrtc::NsModes ns_mode = webrtc::kNsHighSuppression;
kwiberg102c6a62015-10-30 02:47:38 -0700619 if (options.aecm_generate_comfort_noise) {
buildbot@webrtc.org13d67762014-05-02 17:33:29 +0000620 LOG(LS_VERBOSE) << "Comfort noise explicitly set to "
kwiberg102c6a62015-10-30 02:47:38 -0700621 << *options.aecm_generate_comfort_noise
622 << " (default is false).";
buildbot@webrtc.org13d67762014-05-02 17:33:29 +0000623 }
624
625#if defined(IOS)
626 // On iOS, VPIO provides built-in EC and AGC.
Karl Wibergbe579832015-11-10 22:34:18 +0100627 options.echo_cancellation = rtc::Optional<bool>(false);
628 options.auto_gain_control = rtc::Optional<bool>(false);
henrika86d907c2015-09-07 16:09:50 +0200629 LOG(LS_INFO) << "Always disable AEC and AGC on iOS. Use built-in instead.";
buildbot@webrtc.org13d67762014-05-02 17:33:29 +0000630#elif defined(ANDROID)
631 ec_mode = webrtc::kEcAecm;
632#endif
633
634#if defined(IOS) || defined(ANDROID)
635 // Set the AGC mode for iOS as well despite disabling it above, to avoid
636 // unsupported configuration errors from webrtc.
637 agc_mode = webrtc::kAgcFixedDigital;
Karl Wibergbe579832015-11-10 22:34:18 +0100638 options.typing_detection = rtc::Optional<bool>(false);
639 options.experimental_agc = rtc::Optional<bool>(false);
640 options.extended_filter_aec = rtc::Optional<bool>(false);
641 options.experimental_ns = rtc::Optional<bool>(false);
buildbot@webrtc.org13d67762014-05-02 17:33:29 +0000642#endif
643
Bjorn Volckerbf395c12015-03-25 22:45:56 +0100644 // Delay Agnostic AEC automatically turns on EC if not set except on iOS
645 // where the feature is not supported.
646 bool use_delay_agnostic_aec = false;
647#if !defined(IOS)
kwiberg102c6a62015-10-30 02:47:38 -0700648 if (options.delay_agnostic_aec) {
649 use_delay_agnostic_aec = *options.delay_agnostic_aec;
Bjorn Volckerbf395c12015-03-25 22:45:56 +0100650 if (use_delay_agnostic_aec) {
Karl Wibergbe579832015-11-10 22:34:18 +0100651 options.echo_cancellation = rtc::Optional<bool>(true);
652 options.extended_filter_aec = rtc::Optional<bool>(true);
Bjorn Volckerbf395c12015-03-25 22:45:56 +0100653 ec_mode = webrtc::kEcConference;
654 }
655 }
656#endif
657
buildbot@webrtc.org13d67762014-05-02 17:33:29 +0000658 webrtc::VoEAudioProcessing* voep = voe_wrapper_->processing();
659
kwiberg102c6a62015-10-30 02:47:38 -0700660 if (options.echo_cancellation) {
henrika@webrtc.orga954c072014-12-09 16:22:09 +0000661 // Check if platform supports built-in EC. Currently only supported on
662 // Android and in combination with Java based audio layer.
663 // TODO(henrika): investigate possibility to support built-in EC also
664 // in combination with Open SL ES audio.
665 const bool built_in_aec = voe_wrapper_->hw()->BuiltInAECIsAvailable();
Bjorn Volcker73f72102015-06-03 14:50:15 +0200666 if (built_in_aec) {
Bjorn Volckerccfc9392015-05-07 07:43:17 +0200667 // Built-in EC exists on this device and use_delay_agnostic_aec is not
668 // overriding it. Enable/Disable it according to the echo_cancellation
669 // audio option.
Bjorn Volcker73f72102015-06-03 14:50:15 +0200670 const bool enable_built_in_aec =
kwiberg102c6a62015-10-30 02:47:38 -0700671 *options.echo_cancellation && !use_delay_agnostic_aec;
Bjorn Volcker73f72102015-06-03 14:50:15 +0200672 if (voe_wrapper_->hw()->EnableBuiltInAEC(enable_built_in_aec) == 0 &&
673 enable_built_in_aec) {
Bjorn Volckerbf395c12015-03-25 22:45:56 +0100674 // Disable internal software EC if built-in EC is enabled,
henrika@webrtc.orga954c072014-12-09 16:22:09 +0000675 // i.e., replace the software EC with the built-in EC.
Karl Wibergbe579832015-11-10 22:34:18 +0100676 options.echo_cancellation = rtc::Optional<bool>(false);
henrika@webrtc.orga954c072014-12-09 16:22:09 +0000677 LOG(LS_INFO) << "Disabling EC since built-in EC will be used instead";
678 }
679 }
kwiberg102c6a62015-10-30 02:47:38 -0700680 if (voep->SetEcStatus(*options.echo_cancellation, ec_mode) == -1) {
681 LOG_RTCERR2(SetEcStatus, *options.echo_cancellation, ec_mode);
buildbot@webrtc.org13d67762014-05-02 17:33:29 +0000682 return false;
683 } else {
kwiberg102c6a62015-10-30 02:47:38 -0700684 LOG(LS_INFO) << "Echo control set to " << *options.echo_cancellation
henrika86d907c2015-09-07 16:09:50 +0200685 << " with mode " << ec_mode;
buildbot@webrtc.org13d67762014-05-02 17:33:29 +0000686 }
687#if !defined(ANDROID)
688 // TODO(ajm): Remove the error return on Android from webrtc.
kwiberg102c6a62015-10-30 02:47:38 -0700689 if (voep->SetEcMetricsStatus(*options.echo_cancellation) == -1) {
690 LOG_RTCERR1(SetEcMetricsStatus, *options.echo_cancellation);
buildbot@webrtc.org13d67762014-05-02 17:33:29 +0000691 return false;
692 }
693#endif
694 if (ec_mode == webrtc::kEcAecm) {
kwiberg102c6a62015-10-30 02:47:38 -0700695 bool cn = options.aecm_generate_comfort_noise.value_or(false);
696 if (voep->SetAecmMode(aecm_mode, cn) != 0) {
697 LOG_RTCERR2(SetAecmMode, aecm_mode, cn);
buildbot@webrtc.org13d67762014-05-02 17:33:29 +0000698 return false;
699 }
700 }
701 }
702
kwiberg102c6a62015-10-30 02:47:38 -0700703 if (options.auto_gain_control) {
henrikac14f5ff2015-09-23 14:08:33 +0200704 const bool built_in_agc = voe_wrapper_->hw()->BuiltInAGCIsAvailable();
705 if (built_in_agc) {
kwiberg102c6a62015-10-30 02:47:38 -0700706 if (voe_wrapper_->hw()->EnableBuiltInAGC(*options.auto_gain_control) ==
707 0 &&
708 *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
kwiberg102c6a62015-10-30 02:47:38 -0700750 if (options.noise_suppression) {
henrikac14f5ff2015-09-23 14:08:33 +0200751 const bool built_in_ns = voe_wrapper_->hw()->BuiltInNSIsAvailable();
752 if (built_in_ns) {
kwiberg102c6a62015-10-30 02:47:38 -0700753 if (voe_wrapper_->hw()->EnableBuiltInNS(*options.noise_suppression) ==
754 0 &&
755 *options.noise_suppression) {
henrikac14f5ff2015-09-23 14:08:33 +0200756 // Disable internal software NS if built-in NS is enabled,
757 // i.e., replace the software NS with the built-in NS.
Karl Wibergbe579832015-11-10 22:34:18 +0100758 options.noise_suppression = rtc::Optional<bool>(false);
henrikac14f5ff2015-09-23 14:08:33 +0200759 LOG(LS_INFO) << "Disabling NS since built-in NS will be used instead";
760 }
761 }
kwiberg102c6a62015-10-30 02:47:38 -0700762 if (voep->SetNsStatus(*options.noise_suppression, ns_mode) == -1) {
763 LOG_RTCERR2(SetNsStatus, *options.noise_suppression, ns_mode);
buildbot@webrtc.org13d67762014-05-02 17:33:29 +0000764 return false;
765 } else {
kwiberg102c6a62015-10-30 02:47:38 -0700766 LOG(LS_INFO) << "Noise suppression set to " << *options.noise_suppression
henrikac14f5ff2015-09-23 14:08:33 +0200767 << " with mode " << ns_mode;
buildbot@webrtc.org13d67762014-05-02 17:33:29 +0000768 }
769 }
770
kwiberg102c6a62015-10-30 02:47:38 -0700771 if (options.highpass_filter) {
772 LOG(LS_INFO) << "High pass filter enabled? " << *options.highpass_filter;
773 if (voep->EnableHighPassFilter(*options.highpass_filter) == -1) {
774 LOG_RTCERR1(SetHighpassFilterStatus, *options.highpass_filter);
buildbot@webrtc.org13d67762014-05-02 17:33:29 +0000775 return false;
776 }
777 }
778
kwiberg102c6a62015-10-30 02:47:38 -0700779 if (options.stereo_swapping) {
780 LOG(LS_INFO) << "Stereo swapping enabled? " << *options.stereo_swapping;
781 voep->EnableStereoChannelSwapping(*options.stereo_swapping);
782 if (voep->IsStereoChannelSwappingEnabled() != *options.stereo_swapping) {
783 LOG_RTCERR1(EnableStereoChannelSwapping, *options.stereo_swapping);
buildbot@webrtc.org13d67762014-05-02 17:33:29 +0000784 return false;
785 }
786 }
787
kwiberg102c6a62015-10-30 02:47:38 -0700788 if (options.audio_jitter_buffer_max_packets) {
789 LOG(LS_INFO) << "NetEq capacity is "
790 << *options.audio_jitter_buffer_max_packets;
Henrik Lundin64dad832015-05-11 12:44:23 +0200791 voe_config_.Set<webrtc::NetEqCapacityConfig>(
kwiberg102c6a62015-10-30 02:47:38 -0700792 new webrtc::NetEqCapacityConfig(
793 *options.audio_jitter_buffer_max_packets));
Henrik Lundin64dad832015-05-11 12:44:23 +0200794 }
795
kwiberg102c6a62015-10-30 02:47:38 -0700796 if (options.audio_jitter_buffer_fast_accelerate) {
797 LOG(LS_INFO) << "NetEq fast mode? "
798 << *options.audio_jitter_buffer_fast_accelerate;
Henrik Lundin5263b3c2015-06-01 10:29:41 +0200799 voe_config_.Set<webrtc::NetEqFastAccelerate>(
kwiberg102c6a62015-10-30 02:47:38 -0700800 new webrtc::NetEqFastAccelerate(
801 *options.audio_jitter_buffer_fast_accelerate));
Henrik Lundin5263b3c2015-06-01 10:29:41 +0200802 }
803
kwiberg102c6a62015-10-30 02:47:38 -0700804 if (options.typing_detection) {
805 LOG(LS_INFO) << "Typing detection is enabled? "
806 << *options.typing_detection;
807 if (voep->SetTypingDetectionStatus(*options.typing_detection) == -1) {
buildbot@webrtc.org13d67762014-05-02 17:33:29 +0000808 // In case of error, log the info and continue
kwiberg102c6a62015-10-30 02:47:38 -0700809 LOG_RTCERR1(SetTypingDetectionStatus, *options.typing_detection);
buildbot@webrtc.org13d67762014-05-02 17:33:29 +0000810 }
811 }
812
kwiberg102c6a62015-10-30 02:47:38 -0700813 if (options.adjust_agc_delta) {
814 LOG(LS_INFO) << "Adjust agc delta is " << *options.adjust_agc_delta;
815 if (!AdjustAgcLevel(*options.adjust_agc_delta)) {
buildbot@webrtc.org13d67762014-05-02 17:33:29 +0000816 return false;
817 }
818 }
819
kwiberg102c6a62015-10-30 02:47:38 -0700820 if (options.aec_dump) {
821 LOG(LS_INFO) << "Aec dump is enabled? " << *options.aec_dump;
822 if (*options.aec_dump)
buildbot@webrtc.org13d67762014-05-02 17:33:29 +0000823 StartAecDump(kAecDumpByAudioOptionFilename);
824 else
825 StopAecDump();
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
856 // We check audioproc for the benefit of tests, since FakeWebRtcVoiceEngine
857 // returns NULL on audio_processing().
858 webrtc::AudioProcessing* audioproc = voe_wrapper_->base()->audio_processing();
859 if (audioproc) {
860 audioproc->SetExtraOptions(config);
861 }
862
kwiberg102c6a62015-10-30 02:47:38 -0700863 if (options.recording_sample_rate) {
864 LOG(LS_INFO) << "Recording sample rate is "
865 << *options.recording_sample_rate;
866 if (voe_wrapper_->hw()->SetRecordingSampleRate(
867 *options.recording_sample_rate)) {
868 LOG_RTCERR1(SetRecordingSampleRate, *options.recording_sample_rate);
buildbot@webrtc.org13d67762014-05-02 17:33:29 +0000869 }
870 }
871
kwiberg102c6a62015-10-30 02:47:38 -0700872 if (options.playout_sample_rate) {
873 LOG(LS_INFO) << "Playout sample rate is " << *options.playout_sample_rate;
874 if (voe_wrapper_->hw()->SetPlayoutSampleRate(
875 *options.playout_sample_rate)) {
876 LOG_RTCERR1(SetPlayoutSampleRate, *options.playout_sample_rate);
buildbot@webrtc.org13d67762014-05-02 17:33:29 +0000877 }
878 }
879
880 return true;
881}
882
buildbot@webrtc.org13d67762014-05-02 17:33:29 +0000883// TODO(juberti): Refactor this so that the core logic can be used to set the
884// soundclip device. At that time, reinstate the soundclip pause/resume code.
885bool WebRtcVoiceEngine::SetDevices(const Device* in_device,
886 const Device* out_device) {
solenberg566ef242015-11-06 15:34:49 -0800887 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
buildbot@webrtc.org13d67762014-05-02 17:33:29 +0000888#if !defined(IOS)
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000889 int in_id = in_device ? rtc::FromString<int>(in_device->id) :
buildbot@webrtc.org13d67762014-05-02 17:33:29 +0000890 kDefaultAudioDeviceId;
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000891 int out_id = out_device ? rtc::FromString<int>(out_device->id) :
buildbot@webrtc.org13d67762014-05-02 17:33:29 +0000892 kDefaultAudioDeviceId;
893 // The device manager uses -1 as the default device, which was the case for
894 // VoE 3.5. VoE 4.0, however, uses 0 as the default in Linux and Mac.
895#ifndef WIN32
896 if (-1 == in_id) {
897 in_id = kDefaultAudioDeviceId;
898 }
899 if (-1 == out_id) {
900 out_id = kDefaultAudioDeviceId;
901 }
902#endif
903
904 std::string in_name = (in_id != kDefaultAudioDeviceId) ?
905 in_device->name : "Default device";
906 std::string out_name = (out_id != kDefaultAudioDeviceId) ?
907 out_device->name : "Default device";
908 LOG(LS_INFO) << "Setting microphone to (id=" << in_id << ", name=" << in_name
909 << ") and speaker to (id=" << out_id << ", name=" << out_name
910 << ")";
911
buildbot@webrtc.org13d67762014-05-02 17:33:29 +0000912 // Must also pause all audio playback and capture.
solenbergc1a1b352015-09-22 13:31:20 -0700913 bool ret = true;
Fredrik Solenbergaf9fb212015-08-26 10:45:53 +0200914 for (WebRtcVoiceMediaChannel* channel : channels_) {
buildbot@webrtc.org13d67762014-05-02 17:33:29 +0000915 if (!channel->PausePlayout()) {
916 LOG(LS_WARNING) << "Failed to pause playout";
917 ret = false;
918 }
919 if (!channel->PauseSend()) {
920 LOG(LS_WARNING) << "Failed to pause send";
921 ret = false;
922 }
923 }
924
925 // Find the recording device id in VoiceEngine and set recording device.
926 if (!FindWebRtcAudioDeviceId(true, in_name, in_id, &in_id)) {
927 ret = false;
928 }
929 if (ret) {
930 if (voe_wrapper_->hw()->SetRecordingDevice(in_id) == -1) {
931 LOG_RTCERR2(SetRecordingDevice, in_name, in_id);
932 ret = false;
933 }
buildbot@webrtc.org6b21b712014-07-31 15:08:53 +0000934 webrtc::AudioProcessing* ap = voe()->base()->audio_processing();
935 if (ap)
936 ap->Initialize();
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000937 }
938
939 // Find the playout device id in VoiceEngine and set playout device.
940 if (!FindWebRtcAudioDeviceId(false, out_name, out_id, &out_id)) {
941 LOG(LS_WARNING) << "Failed to find VoiceEngine device id for " << out_name;
942 ret = false;
943 }
944 if (ret) {
945 if (voe_wrapper_->hw()->SetPlayoutDevice(out_id) == -1) {
sergeyu@chromium.org5bc25c42013-12-05 00:24:06 +0000946 LOG_RTCERR2(SetPlayoutDevice, out_name, out_id);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000947 ret = false;
948 }
949 }
950
951 // Resume all audio playback and capture.
Fredrik Solenbergaf9fb212015-08-26 10:45:53 +0200952 for (WebRtcVoiceMediaChannel* channel : channels_) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000953 if (!channel->ResumePlayout()) {
954 LOG(LS_WARNING) << "Failed to resume playout";
955 ret = false;
956 }
957 if (!channel->ResumeSend()) {
958 LOG(LS_WARNING) << "Failed to resume send";
959 ret = false;
960 }
961 }
962
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000963 if (ret) {
964 LOG(LS_INFO) << "Set microphone to (id=" << in_id <<" name=" << in_name
965 << ") and speaker to (id="<< out_id << " name=" << out_name
966 << ")";
967 }
968
969 return ret;
970#else
971 return true;
wu@webrtc.orgcecfd182013-10-30 05:18:12 +0000972#endif // !IOS
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000973}
974
975bool WebRtcVoiceEngine::FindWebRtcAudioDeviceId(
976 bool is_input, const std::string& dev_name, int dev_id, int* rtc_id) {
solenberg566ef242015-11-06 15:34:49 -0800977 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000978 // In Linux, VoiceEngine uses the same device dev_id as the device manager.
wu@webrtc.orgcecfd182013-10-30 05:18:12 +0000979#if defined(LINUX) || defined(ANDROID)
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000980 *rtc_id = dev_id;
981 return true;
982#else
983 // In Windows and Mac, we need to find the VoiceEngine device id by name
984 // unless the input dev_id is the default device id.
985 if (kDefaultAudioDeviceId == dev_id) {
986 *rtc_id = dev_id;
987 return true;
988 }
989
990 // Get the number of VoiceEngine audio devices.
991 int count = 0;
992 if (is_input) {
993 if (-1 == voe_wrapper_->hw()->GetNumOfRecordingDevices(count)) {
994 LOG_RTCERR0(GetNumOfRecordingDevices);
995 return false;
996 }
997 } else {
998 if (-1 == voe_wrapper_->hw()->GetNumOfPlayoutDevices(count)) {
999 LOG_RTCERR0(GetNumOfPlayoutDevices);
1000 return false;
1001 }
1002 }
1003
1004 for (int i = 0; i < count; ++i) {
1005 char name[128];
1006 char guid[128];
1007 if (is_input) {
1008 voe_wrapper_->hw()->GetRecordingDeviceName(i, name, guid);
1009 LOG(LS_VERBOSE) << "VoiceEngine microphone " << i << ": " << name;
1010 } else {
1011 voe_wrapper_->hw()->GetPlayoutDeviceName(i, name, guid);
1012 LOG(LS_VERBOSE) << "VoiceEngine speaker " << i << ": " << name;
1013 }
1014
1015 std::string webrtc_name(name);
1016 if (dev_name.compare(0, webrtc_name.size(), webrtc_name) == 0) {
1017 *rtc_id = i;
1018 return true;
1019 }
1020 }
1021 LOG(LS_WARNING) << "VoiceEngine cannot find device: " << dev_name;
1022 return false;
1023#endif
1024}
1025
1026bool WebRtcVoiceEngine::GetOutputVolume(int* level) {
solenberg566ef242015-11-06 15:34:49 -08001027 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001028 unsigned int ulevel;
1029 if (voe_wrapper_->volume()->GetSpeakerVolume(ulevel) == -1) {
1030 LOG_RTCERR1(GetSpeakerVolume, level);
1031 return false;
1032 }
1033 *level = ulevel;
1034 return true;
1035}
1036
1037bool WebRtcVoiceEngine::SetOutputVolume(int level) {
solenberg566ef242015-11-06 15:34:49 -08001038 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
henrikg91d6ede2015-09-17 00:24:34 -07001039 RTC_DCHECK(level >= 0 && level <= 255);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001040 if (voe_wrapper_->volume()->SetSpeakerVolume(level) == -1) {
1041 LOG_RTCERR1(SetSpeakerVolume, level);
1042 return false;
1043 }
1044 return true;
1045}
1046
1047int WebRtcVoiceEngine::GetInputLevel() {
solenberg566ef242015-11-06 15:34:49 -08001048 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001049 unsigned int ulevel;
1050 return (voe_wrapper_->volume()->GetSpeechInputLevel(ulevel) != -1) ?
1051 static_cast<int>(ulevel) : -1;
1052}
1053
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001054const std::vector<AudioCodec>& WebRtcVoiceEngine::codecs() {
solenberg566ef242015-11-06 15:34:49 -08001055 RTC_DCHECK(signal_thread_checker_.CalledOnValidThread());
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001056 return codecs_;
1057}
1058
1059bool WebRtcVoiceEngine::FindCodec(const AudioCodec& in) {
solenberg566ef242015-11-06 15:34:49 -08001060 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001061 return FindWebRtcCodec(in, NULL);
1062}
1063
1064// Get the VoiceEngine codec that matches |in|, with the supplied settings.
1065bool WebRtcVoiceEngine::FindWebRtcCodec(const AudioCodec& in,
1066 webrtc::CodecInst* out) {
solenberg566ef242015-11-06 15:34:49 -08001067 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001068 int ncodecs = voe_wrapper_->codec()->NumOfCodecs();
1069 for (int i = 0; i < ncodecs; ++i) {
1070 webrtc::CodecInst voe_codec;
henrik.lundin@webrtc.org8038d422014-11-11 08:38:24 +00001071 if (GetVoeCodec(i, &voe_codec)) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001072 AudioCodec codec(voe_codec.pltype, voe_codec.plname, voe_codec.plfreq,
1073 voe_codec.rate, voe_codec.channels, 0);
1074 bool multi_rate = IsCodecMultiRate(voe_codec);
1075 // Allow arbitrary rates for ISAC to be specified.
1076 if (multi_rate) {
1077 // Set codec.bitrate to 0 so the check for codec.Matches() passes.
1078 codec.bitrate = 0;
1079 }
1080 if (codec.Matches(in)) {
1081 if (out) {
1082 // Fixup the payload type.
1083 voe_codec.pltype = in.id;
1084
1085 // Set bitrate if specified.
1086 if (multi_rate && in.bitrate != 0) {
1087 voe_codec.rate = in.bitrate;
1088 }
1089
henrik.lundin@webrtc.orgf85dbce2014-11-07 12:25:00 +00001090 // Reset G722 sample rate to 16000 to match WebRTC.
1091 MaybeFixupG722(&voe_codec, 16000);
1092
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001093 // Apply codec-specific settings.
Minyue Li7100dcd2015-03-27 05:05:59 +01001094 if (IsCodec(codec, kIsacCodecName)) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001095 // If ISAC and an explicit bitrate is not specified,
minyue@webrtc.org26236952014-10-29 02:27:08 +00001096 // enable auto bitrate adjustment.
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001097 voe_codec.rate = (in.bitrate > 0) ? in.bitrate : -1;
1098 }
1099 *out = voe_codec;
1100 }
1101 return true;
1102 }
1103 }
1104 }
1105 return false;
1106}
solenberg7add0582015-11-20 09:59:34 -08001107
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001108const std::vector<RtpHeaderExtension>&
1109WebRtcVoiceEngine::rtp_header_extensions() const {
solenberg566ef242015-11-06 15:34:49 -08001110 RTC_DCHECK(signal_thread_checker_.CalledOnValidThread());
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001111 return rtp_header_extensions_;
1112}
1113
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001114int WebRtcVoiceEngine::GetLastEngineError() {
solenberg566ef242015-11-06 15:34:49 -08001115 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001116 return voe_wrapper_->error();
1117}
1118
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001119void WebRtcVoiceEngine::Print(webrtc::TraceLevel level, const char* trace,
1120 int length) {
solenberg566ef242015-11-06 15:34:49 -08001121 // Note: This callback can happen on any thread!
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00001122 rtc::LoggingSeverity sev = rtc::LS_VERBOSE;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001123 if (level == webrtc::kTraceError || level == webrtc::kTraceCritical)
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00001124 sev = rtc::LS_ERROR;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001125 else if (level == webrtc::kTraceWarning)
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00001126 sev = rtc::LS_WARNING;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001127 else if (level == webrtc::kTraceStateInfo || level == webrtc::kTraceInfo)
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00001128 sev = rtc::LS_INFO;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001129 else if (level == webrtc::kTraceTerseInfo)
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00001130 sev = rtc::LS_INFO;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001131
1132 // Skip past boilerplate prefix text
1133 if (length < 72) {
1134 std::string msg(trace, length);
1135 LOG(LS_ERROR) << "Malformed webrtc log message: ";
1136 LOG_V(sev) << msg;
1137 } else {
1138 std::string msg(trace + 71, length - 72);
Peter Boströmd5c75b12015-09-23 13:24:32 +02001139 LOG_V(sev) << "webrtc: " << msg;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001140 }
1141}
1142
solenberg63b34542015-09-29 06:06:31 -07001143void WebRtcVoiceEngine::RegisterChannel(WebRtcVoiceMediaChannel* channel) {
solenberg566ef242015-11-06 15:34:49 -08001144 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
1145 RTC_DCHECK(channel);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001146 channels_.push_back(channel);
1147}
1148
solenberg63b34542015-09-29 06:06:31 -07001149void WebRtcVoiceEngine::UnregisterChannel(WebRtcVoiceMediaChannel* channel) {
solenberg566ef242015-11-06 15:34:49 -08001150 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
solenberg63b34542015-09-29 06:06:31 -07001151 auto it = std::find(channels_.begin(), channels_.end(), channel);
solenberg566ef242015-11-06 15:34:49 -08001152 RTC_DCHECK(it != channels_.end());
1153 channels_.erase(it);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001154}
1155
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001156// Adjusts the default AGC target level by the specified delta.
1157// NB: If we start messing with other config fields, we'll want
1158// to save the current webrtc::AgcConfig as well.
1159bool WebRtcVoiceEngine::AdjustAgcLevel(int delta) {
solenberg566ef242015-11-06 15:34:49 -08001160 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001161 webrtc::AgcConfig config = default_agc_config_;
1162 config.targetLeveldBOv -= delta;
1163
1164 LOG(LS_INFO) << "Adjusting AGC level from default -"
1165 << default_agc_config_.targetLeveldBOv << "dB to -"
1166 << config.targetLeveldBOv << "dB";
1167
1168 if (voe_wrapper_->processing()->SetAgcConfig(config) == -1) {
1169 LOG_RTCERR1(SetAgcConfig, config.targetLeveldBOv);
1170 return false;
1171 }
1172 return true;
1173}
1174
Fredrik Solenbergccb49e72015-05-19 11:37:56 +02001175bool WebRtcVoiceEngine::SetAudioDeviceModule(webrtc::AudioDeviceModule* adm) {
solenberg566ef242015-11-06 15:34:49 -08001176 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001177 if (initialized_) {
1178 LOG(LS_WARNING) << "SetAudioDeviceModule can not be called after Init.";
1179 return false;
1180 }
1181 if (adm_) {
1182 adm_->Release();
1183 adm_ = NULL;
1184 }
1185 if (adm) {
1186 adm_ = adm;
1187 adm_->AddRef();
1188 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001189 return true;
1190}
1191
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00001192bool WebRtcVoiceEngine::StartAecDump(rtc::PlatformFile file) {
solenberg566ef242015-11-06 15:34:49 -08001193 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00001194 FILE* aec_dump_file_stream = rtc::FdopenPlatformFileForWriting(file);
wu@webrtc.orga8910d22014-01-23 22:12:45 +00001195 if (!aec_dump_file_stream) {
1196 LOG(LS_ERROR) << "Could not open AEC dump file stream.";
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00001197 if (!rtc::ClosePlatformFile(file))
wu@webrtc.orga8910d22014-01-23 22:12:45 +00001198 LOG(LS_WARNING) << "Could not close file.";
1199 return false;
1200 }
wu@webrtc.orga9890802013-12-13 00:21:03 +00001201 StopAecDump();
wu@webrtc.orga8910d22014-01-23 22:12:45 +00001202 if (voe_wrapper_->processing()->StartDebugRecording(aec_dump_file_stream) !=
wu@webrtc.orga9890802013-12-13 00:21:03 +00001203 webrtc::AudioProcessing::kNoError) {
wu@webrtc.orga8910d22014-01-23 22:12:45 +00001204 LOG_RTCERR0(StartDebugRecording);
1205 fclose(aec_dump_file_stream);
wu@webrtc.orga9890802013-12-13 00:21:03 +00001206 return false;
1207 }
1208 is_dumping_aec_ = true;
1209 return true;
wu@webrtc.orga9890802013-12-13 00:21:03 +00001210}
1211
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001212void WebRtcVoiceEngine::StartAecDump(const std::string& filename) {
solenberg566ef242015-11-06 15:34:49 -08001213 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001214 if (!is_dumping_aec_) {
1215 // Start dumping AEC when we are not dumping.
1216 if (voe_wrapper_->processing()->StartDebugRecording(
1217 filename.c_str()) != webrtc::AudioProcessing::kNoError) {
wu@webrtc.orga9890802013-12-13 00:21:03 +00001218 LOG_RTCERR1(StartDebugRecording, filename.c_str());
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001219 } else {
1220 is_dumping_aec_ = true;
1221 }
1222 }
1223}
1224
1225void WebRtcVoiceEngine::StopAecDump() {
solenberg566ef242015-11-06 15:34:49 -08001226 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001227 if (is_dumping_aec_) {
1228 // Stop dumping AEC when we are dumping.
1229 if (voe_wrapper_->processing()->StopDebugRecording() !=
1230 webrtc::AudioProcessing::kNoError) {
1231 LOG_RTCERR0(StopDebugRecording);
1232 }
1233 is_dumping_aec_ = false;
1234 }
1235}
1236
ivoc112a3d82015-10-16 02:22:18 -07001237bool WebRtcVoiceEngine::StartRtcEventLog(rtc::PlatformFile file) {
solenberg566ef242015-11-06 15:34:49 -08001238 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
ivoc112a3d82015-10-16 02:22:18 -07001239 return voe_wrapper_->codec()->GetEventLog()->StartLogging(file);
1240}
1241
1242void WebRtcVoiceEngine::StopRtcEventLog() {
solenberg566ef242015-11-06 15:34:49 -08001243 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
ivoc112a3d82015-10-16 02:22:18 -07001244 voe_wrapper_->codec()->GetEventLog()->StopLogging();
1245}
1246
solenberg0a617e22015-10-20 15:49:38 -07001247int WebRtcVoiceEngine::CreateVoEChannel() {
solenberg566ef242015-11-06 15:34:49 -08001248 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
solenberg0a617e22015-10-20 15:49:38 -07001249 return voe_wrapper_->base()->CreateChannel(voe_config_);
sergeyu@chromium.org5bc25c42013-12-05 00:24:06 +00001250}
1251
solenbergc96df772015-10-21 13:01:53 -07001252class WebRtcVoiceMediaChannel::WebRtcAudioSendStream
mallinath@webrtc.org67ee6b92014-02-03 16:57:16 +00001253 : public AudioRenderer::Sink {
1254 public:
solenbergc96df772015-10-21 13:01:53 -07001255 WebRtcAudioSendStream(int ch, webrtc::AudioTransport* voe_audio_transport,
solenberg3a941542015-11-16 07:34:50 -08001256 uint32_t ssrc, const std::string& c_name,
1257 const std::vector<webrtc::RtpExtension>& extensions,
1258 webrtc::Call* call)
solenberg7add0582015-11-20 09:59:34 -08001259 : voe_audio_transport_(voe_audio_transport),
solenberg3a941542015-11-16 07:34:50 -08001260 call_(call),
1261 config_(nullptr) {
solenberg85a04962015-10-27 03:35:21 -07001262 RTC_DCHECK_GE(ch, 0);
1263 // TODO(solenberg): Once we're not using FakeWebRtcVoiceEngine anymore:
1264 // RTC_DCHECK(voe_audio_transport);
solenbergc96df772015-10-21 13:01:53 -07001265 RTC_DCHECK(call);
solenberg85a04962015-10-27 03:35:21 -07001266 audio_capture_thread_checker_.DetachFromThread();
solenberg3a941542015-11-16 07:34:50 -08001267 config_.rtp.ssrc = ssrc;
1268 config_.rtp.c_name = c_name;
1269 config_.voe_channel_id = ch;
1270 RecreateAudioSendStream(extensions);
solenbergc96df772015-10-21 13:01:53 -07001271 }
solenberg3a941542015-11-16 07:34:50 -08001272
solenbergc96df772015-10-21 13:01:53 -07001273 ~WebRtcAudioSendStream() override {
solenberg566ef242015-11-06 15:34:49 -08001274 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
solenbergc96df772015-10-21 13:01:53 -07001275 Stop();
1276 call_->DestroyAudioSendStream(stream_);
1277 }
henrike@webrtc.org1e09a712013-07-26 19:17:59 +00001278
solenberg3a941542015-11-16 07:34:50 -08001279 void RecreateAudioSendStream(
1280 const std::vector<webrtc::RtpExtension>& extensions) {
1281 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
1282 if (stream_) {
1283 call_->DestroyAudioSendStream(stream_);
1284 stream_ = nullptr;
1285 }
1286 config_.rtp.extensions = extensions;
1287 RTC_DCHECK(!stream_);
1288 stream_ = call_->CreateAudioSendStream(config_);
1289 RTC_CHECK(stream_);
1290 }
1291
1292 webrtc::AudioSendStream::Stats GetStats() const {
1293 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
1294 RTC_DCHECK(stream_);
1295 return stream_->GetStats();
1296 }
1297
mallinath@webrtc.org67ee6b92014-02-03 16:57:16 +00001298 // Starts the rendering by setting a sink to the renderer to get data
1299 // callback.
henrike@webrtc.orga7b98182014-02-21 15:51:43 +00001300 // This method is called on the libjingle worker thread.
mallinath@webrtc.org67ee6b92014-02-03 16:57:16 +00001301 // TODO(xians): Make sure Start() is called only once.
1302 void Start(AudioRenderer* renderer) {
solenberg566ef242015-11-06 15:34:49 -08001303 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
solenbergc96df772015-10-21 13:01:53 -07001304 RTC_DCHECK(renderer);
1305 if (renderer_) {
henrikg91d6ede2015-09-17 00:24:34 -07001306 RTC_DCHECK(renderer_ == renderer);
mallinath@webrtc.org67ee6b92014-02-03 16:57:16 +00001307 return;
1308 }
mallinath@webrtc.org67ee6b92014-02-03 16:57:16 +00001309 renderer->SetSink(this);
1310 renderer_ = renderer;
1311 }
1312
solenbergc96df772015-10-21 13:01:53 -07001313 // Stops rendering by setting the sink of the renderer to nullptr. No data
mallinath@webrtc.org67ee6b92014-02-03 16:57:16 +00001314 // callback will be received after this method.
henrike@webrtc.orga7b98182014-02-21 15:51:43 +00001315 // This method is called on the libjingle worker thread.
mallinath@webrtc.org67ee6b92014-02-03 16:57:16 +00001316 void Stop() {
solenberg566ef242015-11-06 15:34:49 -08001317 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
solenbergc96df772015-10-21 13:01:53 -07001318 if (renderer_) {
1319 renderer_->SetSink(nullptr);
1320 renderer_ = nullptr;
solenberg98c68862015-10-09 03:27:14 -07001321 }
mallinath@webrtc.org67ee6b92014-02-03 16:57:16 +00001322 }
1323
1324 // AudioRenderer::Sink implementation.
henrike@webrtc.orga7b98182014-02-21 15:51:43 +00001325 // This method is called on the audio thread.
kjellander@webrtc.org14665ff2015-03-04 12:58:35 +00001326 void OnData(const void* audio_data,
1327 int bits_per_sample,
1328 int sample_rate,
1329 int number_of_channels,
Peter Kastingdce40cf2015-08-24 14:52:23 -07001330 size_t number_of_frames) override {
solenberg566ef242015-11-06 15:34:49 -08001331 RTC_DCHECK(!worker_thread_checker_.CalledOnValidThread());
solenberg85a04962015-10-27 03:35:21 -07001332 RTC_DCHECK(audio_capture_thread_checker_.CalledOnValidThread());
solenbergc96df772015-10-21 13:01:53 -07001333 RTC_DCHECK(voe_audio_transport_);
solenberg7add0582015-11-20 09:59:34 -08001334 voe_audio_transport_->OnData(config_.voe_channel_id,
henrike@webrtc.orga7b98182014-02-21 15:51:43 +00001335 audio_data,
1336 bits_per_sample,
1337 sample_rate,
1338 number_of_channels,
1339 number_of_frames);
henrike@webrtc.orga7b98182014-02-21 15:51:43 +00001340 }
1341
1342 // Callback from the |renderer_| when it is going away. In case Start() has
1343 // never been called, this callback won't be triggered.
kjellander@webrtc.org14665ff2015-03-04 12:58:35 +00001344 void OnClose() override {
solenberg566ef242015-11-06 15:34:49 -08001345 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
solenbergc96df772015-10-21 13:01:53 -07001346 // Set |renderer_| to nullptr to make sure no more callback will get into
henrike@webrtc.orga7b98182014-02-21 15:51:43 +00001347 // the renderer.
solenbergc96df772015-10-21 13:01:53 -07001348 renderer_ = nullptr;
mallinath@webrtc.org67ee6b92014-02-03 16:57:16 +00001349 }
1350
1351 // Accessor to the VoE channel ID.
solenberg85a04962015-10-27 03:35:21 -07001352 int channel() const {
solenberg566ef242015-11-06 15:34:49 -08001353 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
solenberg7add0582015-11-20 09:59:34 -08001354 return config_.voe_channel_id;
solenberg85a04962015-10-27 03:35:21 -07001355 }
mallinath@webrtc.org67ee6b92014-02-03 16:57:16 +00001356
1357 private:
solenberg566ef242015-11-06 15:34:49 -08001358 rtc::ThreadChecker worker_thread_checker_;
solenberg85a04962015-10-27 03:35:21 -07001359 rtc::ThreadChecker audio_capture_thread_checker_;
solenbergc96df772015-10-21 13:01:53 -07001360 webrtc::AudioTransport* const voe_audio_transport_ = nullptr;
1361 webrtc::Call* call_ = nullptr;
solenberg3a941542015-11-16 07:34:50 -08001362 webrtc::AudioSendStream::Config config_;
1363 // The stream is owned by WebRtcAudioSendStream and may be reallocated if
1364 // configuration changes.
solenbergc96df772015-10-21 13:01:53 -07001365 webrtc::AudioSendStream* stream_ = nullptr;
mallinath@webrtc.org67ee6b92014-02-03 16:57:16 +00001366
1367 // Raw pointer to AudioRenderer owned by LocalAudioTrackHandler.
1368 // PeerConnection will make sure invalidating the pointer before the object
1369 // goes away.
solenbergc96df772015-10-21 13:01:53 -07001370 AudioRenderer* renderer_ = nullptr;
henrike@webrtc.orga7b98182014-02-21 15:51:43 +00001371
solenbergc96df772015-10-21 13:01:53 -07001372 RTC_DISALLOW_IMPLICIT_CONSTRUCTORS(WebRtcAudioSendStream);
1373};
1374
1375class WebRtcVoiceMediaChannel::WebRtcAudioReceiveStream {
1376 public:
solenberg7add0582015-11-20 09:59:34 -08001377 WebRtcAudioReceiveStream(int ch, uint32_t remote_ssrc, uint32_t local_ssrc,
1378 bool use_combined_bwe, const std::string& sync_group,
1379 const std::vector<webrtc::RtpExtension>& extensions,
1380 webrtc::Call* call)
1381 : call_(call),
1382 config_() {
1383 RTC_DCHECK_GE(ch, 0);
1384 RTC_DCHECK(call);
1385 config_.rtp.remote_ssrc = remote_ssrc;
1386 config_.rtp.local_ssrc = local_ssrc;
1387 config_.voe_channel_id = ch;
1388 config_.sync_group = sync_group;
1389 RecreateAudioReceiveStream(use_combined_bwe, extensions);
1390 }
solenbergc96df772015-10-21 13:01:53 -07001391
solenberg7add0582015-11-20 09:59:34 -08001392 ~WebRtcAudioReceiveStream() {
1393 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
1394 call_->DestroyAudioReceiveStream(stream_);
1395 }
1396
1397 void RecreateAudioReceiveStream(
1398 const std::vector<webrtc::RtpExtension>& extensions) {
1399 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
1400 RecreateAudioReceiveStream(config_.combined_audio_video_bwe, extensions);
1401 }
1402 void RecreateAudioReceiveStream(bool use_combined_bwe) {
1403 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
1404 RecreateAudioReceiveStream(use_combined_bwe, config_.rtp.extensions);
1405 }
1406
1407 webrtc::AudioReceiveStream::Stats GetStats() const {
1408 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
1409 RTC_DCHECK(stream_);
1410 return stream_->GetStats();
1411 }
1412
1413 int channel() const {
1414 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
1415 return config_.voe_channel_id;
1416 }
solenbergc96df772015-10-21 13:01:53 -07001417
1418 private:
solenberg7add0582015-11-20 09:59:34 -08001419 void RecreateAudioReceiveStream(bool use_combined_bwe,
1420 const std::vector<webrtc::RtpExtension>& extensions) {
1421 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
1422 if (stream_) {
1423 call_->DestroyAudioReceiveStream(stream_);
1424 stream_ = nullptr;
1425 }
1426 config_.rtp.extensions = extensions;
1427 config_.combined_audio_video_bwe = use_combined_bwe;
1428 RTC_DCHECK(!stream_);
1429 stream_ = call_->CreateAudioReceiveStream(config_);
1430 RTC_CHECK(stream_);
1431 }
1432
1433 rtc::ThreadChecker worker_thread_checker_;
1434 webrtc::Call* call_ = nullptr;
1435 webrtc::AudioReceiveStream::Config config_;
1436 // The stream is owned by WebRtcAudioReceiveStream and may be reallocated if
1437 // configuration changes.
1438 webrtc::AudioReceiveStream* stream_ = nullptr;
solenbergc96df772015-10-21 13:01:53 -07001439
1440 RTC_DISALLOW_IMPLICIT_CONSTRUCTORS(WebRtcAudioReceiveStream);
henrike@webrtc.org1e09a712013-07-26 19:17:59 +00001441};
1442
Fredrik Solenberg709ed672015-09-15 12:26:33 +02001443WebRtcVoiceMediaChannel::WebRtcVoiceMediaChannel(WebRtcVoiceEngine* engine,
Fredrik Solenbergb071a192015-09-17 16:42:56 +02001444 const AudioOptions& options,
Fredrik Solenberg709ed672015-09-15 12:26:33 +02001445 webrtc::Call* call)
solenberg566ef242015-11-06 15:34:49 -08001446 : engine_(engine), call_(call) {
solenberg0a617e22015-10-20 15:49:38 -07001447 LOG(LS_VERBOSE) << "WebRtcVoiceMediaChannel::WebRtcVoiceMediaChannel";
solenberg566ef242015-11-06 15:34:49 -08001448 RTC_DCHECK(call);
solenberg0a617e22015-10-20 15:49:38 -07001449 engine->RegisterChannel(this);
Fredrik Solenbergb071a192015-09-17 16:42:56 +02001450 SetOptions(options);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001451}
1452
1453WebRtcVoiceMediaChannel::~WebRtcVoiceMediaChannel() {
solenberg566ef242015-11-06 15:34:49 -08001454 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
solenberg0a617e22015-10-20 15:49:38 -07001455 LOG(LS_VERBOSE) << "WebRtcVoiceMediaChannel::~WebRtcVoiceMediaChannel";
solenberg7add0582015-11-20 09:59:34 -08001456 // TODO(solenberg): Should be able to delete the streams directly, without
1457 // going through RemoveNnStream(), once stream objects handle
1458 // all (de)configuration.
solenbergc96df772015-10-21 13:01:53 -07001459 while (!send_streams_.empty()) {
1460 RemoveSendStream(send_streams_.begin()->first);
solenbergd97ec302015-10-07 01:40:33 -07001461 }
solenberg7add0582015-11-20 09:59:34 -08001462 while (!recv_streams_.empty()) {
1463 RemoveRecvStream(recv_streams_.begin()->first);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001464 }
solenberg0a617e22015-10-20 15:49:38 -07001465 engine()->UnregisterChannel(this);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001466}
1467
Peter Thatcherc2ee2c82015-08-07 16:05:34 -07001468bool WebRtcVoiceMediaChannel::SetSendParameters(
1469 const AudioSendParameters& params) {
solenberg566ef242015-11-06 15:34:49 -08001470 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
Peter Thatcherc2ee2c82015-08-07 16:05:34 -07001471 // TODO(pthatcher): Refactor this to be more clean now that we have
1472 // all the information at once.
solenberg3a941542015-11-16 07:34:50 -08001473
1474 if (!SetSendCodecs(params.codecs)) {
1475 return false;
1476 }
1477
1478 std::vector<webrtc::RtpExtension> send_rtp_extensions =
1479 FindAudioRtpHeaderExtensions(params.extensions);
1480 if (send_rtp_extensions_ != send_rtp_extensions) {
1481 send_rtp_extensions_.swap(send_rtp_extensions);
1482 for (auto& it : send_streams_) {
1483 it.second->RecreateAudioSendStream(send_rtp_extensions_);
1484 }
1485 }
1486
1487 if (!SetMaxSendBandwidth(params.max_bandwidth_bps)) {
1488 return false;
1489 }
1490 return SetOptions(params.options);
Peter Thatcherc2ee2c82015-08-07 16:05:34 -07001491}
1492
1493bool WebRtcVoiceMediaChannel::SetRecvParameters(
1494 const AudioRecvParameters& params) {
solenberg566ef242015-11-06 15:34:49 -08001495 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
Peter Thatcherc2ee2c82015-08-07 16:05:34 -07001496 // TODO(pthatcher): Refactor this to be more clean now that we have
1497 // all the information at once.
solenberg7add0582015-11-20 09:59:34 -08001498
1499 if (!SetRecvCodecs(params.codecs)) {
1500 return false;
1501 }
1502
1503 std::vector<webrtc::RtpExtension> recv_rtp_extensions =
1504 FindAudioRtpHeaderExtensions(params.extensions);
1505 if (recv_rtp_extensions_ != recv_rtp_extensions) {
1506 recv_rtp_extensions_.swap(recv_rtp_extensions);
1507 for (auto& it : recv_streams_) {
1508 it.second->RecreateAudioReceiveStream(recv_rtp_extensions_);
1509 }
1510 }
1511
1512 return true;
Peter Thatcherc2ee2c82015-08-07 16:05:34 -07001513}
1514
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001515bool WebRtcVoiceMediaChannel::SetOptions(const AudioOptions& options) {
solenberg566ef242015-11-06 15:34:49 -08001516 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001517 LOG(LS_INFO) << "Setting voice channel options: "
1518 << options.ToString();
1519
wu@webrtc.orgde305012013-10-31 15:40:38 +00001520 // Check if DSCP value is changed from previous.
1521 bool dscp_option_changed = (options_.dscp != options.dscp);
1522
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001523 // We retain all of the existing options, and apply the given ones
1524 // on top. This means there is no way to "clear" options such that
1525 // they go back to the engine default.
1526 options_.SetAll(options);
1527
1528 if (send_ != SEND_NOTHING) {
solenberg63b34542015-09-29 06:06:31 -07001529 if (!engine()->ApplyOptions(options_)) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001530 LOG(LS_WARNING) <<
solenberg63b34542015-09-29 06:06:31 -07001531 "Failed to apply engine options during channel SetOptions.";
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001532 return false;
1533 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001534 }
1535
wu@webrtc.orgde305012013-10-31 15:40:38 +00001536 if (dscp_option_changed) {
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00001537 rtc::DiffServCodePoint dscp = rtc::DSCP_DEFAULT;
kwiberg102c6a62015-10-30 02:47:38 -07001538 if (options_.dscp.value_or(false))
wu@webrtc.orgde305012013-10-31 15:40:38 +00001539 dscp = kAudioDscpValue;
1540 if (MediaChannel::SetDscp(dscp) != 0) {
1541 LOG(LS_WARNING) << "Failed to set DSCP settings for audio channel";
1542 }
1543 }
solenberg8fb30c32015-10-13 03:06:58 -07001544
solenbergc96df772015-10-21 13:01:53 -07001545 // TODO(solenberg): Don't recreate unless options changed.
solenberg7add0582015-11-20 09:59:34 -08001546 for (auto& it : recv_streams_) {
1547 it.second->RecreateAudioReceiveStream(
1548 options_.combined_audio_video_bwe.value_or(false));
1549 }
solenberg8fb30c32015-10-13 03:06:58 -07001550
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001551 LOG(LS_INFO) << "Set voice channel options. Current options: "
1552 << options_.ToString();
1553 return true;
1554}
1555
1556bool WebRtcVoiceMediaChannel::SetRecvCodecs(
1557 const std::vector<AudioCodec>& codecs) {
solenberg566ef242015-11-06 15:34:49 -08001558 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
solenberg8fb30c32015-10-13 03:06:58 -07001559
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001560 // Set the payload types to be used for incoming media.
solenberg0b675462015-10-09 01:37:09 -07001561 LOG(LS_INFO) << "Setting receive voice codecs.";
solenberg0b675462015-10-09 01:37:09 -07001562
1563 if (!VerifyUniquePayloadTypes(codecs)) {
1564 LOG(LS_ERROR) << "Codec payload types overlap.";
1565 return false;
1566 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001567
1568 std::vector<AudioCodec> new_codecs;
1569 // Find all new codecs. We allow adding new codecs but don't allow changing
1570 // the payload type of codecs that is already configured since we might
1571 // already be receiving packets with that payload type.
Fredrik Solenbergaf9fb212015-08-26 10:45:53 +02001572 for (const AudioCodec& codec : codecs) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001573 AudioCodec old_codec;
Fredrik Solenbergaf9fb212015-08-26 10:45:53 +02001574 if (FindCodec(recv_codecs_, codec, &old_codec)) {
1575 if (old_codec.id != codec.id) {
1576 LOG(LS_ERROR) << codec.name << " payload type changed.";
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001577 return false;
1578 }
1579 } else {
Fredrik Solenbergaf9fb212015-08-26 10:45:53 +02001580 new_codecs.push_back(codec);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001581 }
1582 }
1583 if (new_codecs.empty()) {
1584 // There are no new codecs to configure. Already configured codecs are
1585 // never removed.
1586 return true;
1587 }
1588
1589 if (playout_) {
1590 // Receive codecs can not be changed while playing. So we temporarily
1591 // pause playout.
1592 PausePlayout();
1593 }
1594
Fredrik Solenbergaf9fb212015-08-26 10:45:53 +02001595 bool result = SetRecvCodecsInternal(new_codecs);
1596 if (result) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001597 recv_codecs_ = codecs;
1598 }
1599
1600 if (desired_playout_ && !playout_) {
1601 ResumePlayout();
1602 }
Fredrik Solenbergaf9fb212015-08-26 10:45:53 +02001603 return result;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001604}
1605
1606bool WebRtcVoiceMediaChannel::SetSendCodecs(
wu@webrtc.orgcadf9042013-08-30 21:24:16 +00001607 int channel, const std::vector<AudioCodec>& codecs) {
buildbot@webrtc.orgae740dd2014-06-17 10:56:41 +00001608 // Disable VAD, FEC, and RED unless we know the other side wants them.
wu@webrtc.orgcadf9042013-08-30 21:24:16 +00001609 engine()->voe()->codec()->SetVADStatus(channel, false);
1610 engine()->voe()->rtp()->SetNACKStatus(channel, false, 0);
buildbot@webrtc.orgae740dd2014-06-17 10:56:41 +00001611 engine()->voe()->rtp()->SetREDStatus(channel, false);
1612 engine()->voe()->codec()->SetFECStatus(channel, false);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001613
1614 // Scan through the list to figure out the codec to use for sending, along
1615 // with the proper configuration for VAD and DTMF.
henrike@webrtc.org704bf9e2014-02-27 17:52:04 +00001616 bool found_send_codec = false;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001617 webrtc::CodecInst send_codec;
1618 memset(&send_codec, 0, sizeof(send_codec));
1619
wu@webrtc.org05e7b442014-04-01 17:44:24 +00001620 bool nack_enabled = nack_enabled_;
buildbot@webrtc.org3ffa1f92014-07-02 19:51:26 +00001621 bool enable_codec_fec = false;
Minyue Li7100dcd2015-03-27 05:05:59 +01001622 bool enable_opus_dtx = false;
minyue@webrtc.org26236952014-10-29 02:27:08 +00001623 int opus_max_playback_rate = 0;
buildbot@webrtc.org5d639b32014-09-10 07:57:12 +00001624
henrike@webrtc.org704bf9e2014-02-27 17:52:04 +00001625 // Set send codec (the first non-telephone-event/CN codec)
Fredrik Solenbergaf9fb212015-08-26 10:45:53 +02001626 for (const AudioCodec& codec : codecs) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001627 // Ignore codecs we don't know about. The negotiation step should prevent
1628 // this, but double-check to be sure.
1629 webrtc::CodecInst voe_codec;
Fredrik Solenbergaf9fb212015-08-26 10:45:53 +02001630 if (!engine()->FindWebRtcCodec(codec, &voe_codec)) {
1631 LOG(LS_WARNING) << "Unknown codec " << ToString(codec);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001632 continue;
1633 }
1634
Fredrik Solenbergaf9fb212015-08-26 10:45:53 +02001635 if (IsCodec(codec, kDtmfCodecName) || IsCodec(codec, kCnCodecName)) {
henrike@webrtc.org704bf9e2014-02-27 17:52:04 +00001636 // Skip telephone-event/CN codec, which will be handled later.
1637 continue;
1638 }
1639
henrike@webrtc.org704bf9e2014-02-27 17:52:04 +00001640 // We'll use the first codec in the list to actually send audio data.
1641 // Be sure to use the payload type requested by the remote side.
buildbot@webrtc.orgae740dd2014-06-17 10:56:41 +00001642 // "red", for RED audio, is a special case where the actual codec to be
henrike@webrtc.org704bf9e2014-02-27 17:52:04 +00001643 // used is specified in params.
Fredrik Solenbergaf9fb212015-08-26 10:45:53 +02001644 if (IsCodec(codec, kRedCodecName)) {
henrike@webrtc.org704bf9e2014-02-27 17:52:04 +00001645 // Parse out the RED parameters. If we fail, just ignore RED;
1646 // we don't support all possible params/usage scenarios.
Fredrik Solenbergaf9fb212015-08-26 10:45:53 +02001647 if (!GetRedSendCodec(codec, codecs, &send_codec)) {
henrike@webrtc.org704bf9e2014-02-27 17:52:04 +00001648 continue;
1649 }
1650
1651 // Enable redundant encoding of the specified codec. Treat any
1652 // failure as a fatal internal error.
buildbot@webrtc.orgae740dd2014-06-17 10:56:41 +00001653 LOG(LS_INFO) << "Enabling RED on channel " << channel;
Fredrik Solenbergaf9fb212015-08-26 10:45:53 +02001654 if (engine()->voe()->rtp()->SetREDStatus(channel, true, codec.id) == -1) {
1655 LOG_RTCERR3(SetREDStatus, channel, true, codec.id);
henrike@webrtc.org704bf9e2014-02-27 17:52:04 +00001656 return false;
1657 }
1658 } else {
1659 send_codec = voe_codec;
Fredrik Solenbergaf9fb212015-08-26 10:45:53 +02001660 nack_enabled = IsNackEnabled(codec);
Minyue Li7100dcd2015-03-27 05:05:59 +01001661 // For Opus as the send codec, we are to determine inband FEC, maximum
1662 // playback rate, and opus internal dtx.
Fredrik Solenbergaf9fb212015-08-26 10:45:53 +02001663 if (IsCodec(codec, kOpusCodecName)) {
1664 GetOpusConfig(codec, &send_codec, &enable_codec_fec,
Minyue Li7100dcd2015-03-27 05:05:59 +01001665 &opus_max_playback_rate, &enable_opus_dtx);
buildbot@webrtc.org5d639b32014-09-10 07:57:12 +00001666 }
Brave Yao5225dd82015-03-26 07:39:19 +08001667
1668 // Set packet size if the AudioCodec param kCodecParamPTime is set.
1669 int ptime_ms = 0;
Fredrik Solenbergaf9fb212015-08-26 10:45:53 +02001670 if (codec.GetParam(kCodecParamPTime, &ptime_ms)) {
Brave Yao5225dd82015-03-26 07:39:19 +08001671 if (!SetPTimeAsPacketSize(&send_codec, ptime_ms)) {
1672 LOG(LS_WARNING) << "Failed to set packet size for codec "
1673 << send_codec.plname;
1674 return false;
1675 }
1676 }
henrike@webrtc.org704bf9e2014-02-27 17:52:04 +00001677 }
1678 found_send_codec = true;
1679 break;
1680 }
1681
wu@webrtc.org05e7b442014-04-01 17:44:24 +00001682 if (nack_enabled_ != nack_enabled) {
1683 SetNack(channel, nack_enabled);
1684 nack_enabled_ = nack_enabled;
1685 }
1686
henrike@webrtc.org704bf9e2014-02-27 17:52:04 +00001687 if (!found_send_codec) {
1688 LOG(LS_WARNING) << "Received empty list of codecs.";
1689 return false;
1690 }
1691
1692 // Set the codec immediately, since SetVADStatus() depends on whether
1693 // the current codec is mono or stereo.
1694 if (!SetSendCodec(channel, send_codec))
1695 return false;
1696
buildbot@webrtc.org3ffa1f92014-07-02 19:51:26 +00001697 // FEC should be enabled after SetSendCodec.
1698 if (enable_codec_fec) {
1699 LOG(LS_INFO) << "Attempt to enable codec internal FEC on channel "
1700 << channel;
buildbot@webrtc.org3ffa1f92014-07-02 19:51:26 +00001701 if (engine()->voe()->codec()->SetFECStatus(channel, true) == -1) {
1702 // Enable codec internal FEC. Treat any failure as fatal internal error.
1703 LOG_RTCERR2(SetFECStatus, channel, true);
1704 return false;
1705 }
buildbot@webrtc.org3ffa1f92014-07-02 19:51:26 +00001706 }
1707
Minyue Li7100dcd2015-03-27 05:05:59 +01001708 if (IsCodec(send_codec, kOpusCodecName)) {
1709 // DTX and maxplaybackrate should be set after SetSendCodec. Because current
1710 // send codec has to be Opus.
1711
1712 // Set Opus internal DTX.
1713 LOG(LS_INFO) << "Attempt to "
solenbergbd138382015-11-20 16:08:07 -08001714 << (enable_opus_dtx ? "enable" : "disable")
Minyue Li7100dcd2015-03-27 05:05:59 +01001715 << " Opus DTX on channel "
buildbot@webrtc.org5d639b32014-09-10 07:57:12 +00001716 << channel;
Minyue Li7100dcd2015-03-27 05:05:59 +01001717 if (engine()->voe()->codec()->SetOpusDtx(channel, enable_opus_dtx)) {
1718 LOG_RTCERR2(SetOpusDtx, channel, enable_opus_dtx);
1719 return false;
1720 }
1721
1722 // If opus_max_playback_rate <= 0, the default maximum playback rate
1723 // (48 kHz) will be used.
1724 if (opus_max_playback_rate > 0) {
1725 LOG(LS_INFO) << "Attempt to set maximum playback rate to "
1726 << opus_max_playback_rate
1727 << " Hz on channel "
1728 << channel;
1729 if (engine()->voe()->codec()->SetOpusMaxPlaybackRate(
1730 channel, opus_max_playback_rate) == -1) {
1731 LOG_RTCERR2(SetOpusMaxPlaybackRate, channel, opus_max_playback_rate);
1732 return false;
1733 }
buildbot@webrtc.org5d639b32014-09-10 07:57:12 +00001734 }
buildbot@webrtc.org5d639b32014-09-10 07:57:12 +00001735 }
1736
henrike@webrtc.org704bf9e2014-02-27 17:52:04 +00001737 // Always update the |send_codec_| to the currently set send codec.
1738 send_codec_.reset(new webrtc::CodecInst(send_codec));
1739
minyue@webrtc.org26236952014-10-29 02:27:08 +00001740 if (send_bitrate_setting_) {
1741 SetSendBitrateInternal(send_bitrate_bps_);
henrike@webrtc.org704bf9e2014-02-27 17:52:04 +00001742 }
1743
1744 // Loop through the codecs list again to config the telephone-event/CN codec.
Fredrik Solenbergaf9fb212015-08-26 10:45:53 +02001745 for (const AudioCodec& codec : codecs) {
henrike@webrtc.org704bf9e2014-02-27 17:52:04 +00001746 // Ignore codecs we don't know about. The negotiation step should prevent
1747 // this, but double-check to be sure.
1748 webrtc::CodecInst voe_codec;
Fredrik Solenbergaf9fb212015-08-26 10:45:53 +02001749 if (!engine()->FindWebRtcCodec(codec, &voe_codec)) {
1750 LOG(LS_WARNING) << "Unknown codec " << ToString(codec);
henrike@webrtc.org704bf9e2014-02-27 17:52:04 +00001751 continue;
1752 }
1753
wu@webrtc.org9dba5252013-08-05 20:36:57 +00001754 // Find the DTMF telephone event "codec" and tell VoiceEngine channels
1755 // about it.
Fredrik Solenbergaf9fb212015-08-26 10:45:53 +02001756 if (IsCodec(codec, kDtmfCodecName)) {
wu@webrtc.orgcadf9042013-08-30 21:24:16 +00001757 if (engine()->voe()->dtmf()->SetSendTelephoneEventPayloadType(
Fredrik Solenbergaf9fb212015-08-26 10:45:53 +02001758 channel, codec.id) == -1) {
1759 LOG_RTCERR2(SetSendTelephoneEventPayloadType, channel, codec.id);
wu@webrtc.orgcadf9042013-08-30 21:24:16 +00001760 return false;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001761 }
Fredrik Solenbergaf9fb212015-08-26 10:45:53 +02001762 } else if (IsCodec(codec, kCnCodecName)) {
henrike@webrtc.org704bf9e2014-02-27 17:52:04 +00001763 // Turn voice activity detection/comfort noise on if supported.
1764 // Set the wideband CN payload type appropriately.
1765 // (narrowband always uses the static payload type 13).
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001766 webrtc::PayloadFrequencies cn_freq;
Fredrik Solenbergaf9fb212015-08-26 10:45:53 +02001767 switch (codec.clockrate) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001768 case 8000:
1769 cn_freq = webrtc::kFreq8000Hz;
1770 break;
1771 case 16000:
1772 cn_freq = webrtc::kFreq16000Hz;
1773 break;
1774 case 32000:
1775 cn_freq = webrtc::kFreq32000Hz;
1776 break;
1777 default:
Fredrik Solenbergaf9fb212015-08-26 10:45:53 +02001778 LOG(LS_WARNING) << "CN frequency " << codec.clockrate
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001779 << " not supported.";
1780 continue;
1781 }
wu@webrtc.orgcadf9042013-08-30 21:24:16 +00001782 // Set the CN payloadtype and the VAD status.
1783 // The CN payload type for 8000 Hz clockrate is fixed at 13.
1784 if (cn_freq != webrtc::kFreq8000Hz) {
1785 if (engine()->voe()->codec()->SetSendCNPayloadType(
Fredrik Solenbergaf9fb212015-08-26 10:45:53 +02001786 channel, codec.id, cn_freq) == -1) {
1787 LOG_RTCERR3(SetSendCNPayloadType, channel, codec.id, cn_freq);
wu@webrtc.orgcadf9042013-08-30 21:24:16 +00001788 // TODO(ajm): This failure condition will be removed from VoE.
1789 // Restore the return here when we update to a new enough webrtc.
1790 //
1791 // Not returning false because the SetSendCNPayloadType will fail if
1792 // the channel is already sending.
1793 // This can happen if the remote description is applied twice, for
1794 // example in the case of ROAP on top of JSEP, where both side will
1795 // send the offer.
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001796 }
wu@webrtc.orgcadf9042013-08-30 21:24:16 +00001797 }
wu@webrtc.orgcadf9042013-08-30 21:24:16 +00001798 // Only turn on VAD if we have a CN payload type that matches the
1799 // clockrate for the codec we are going to use.
Fredrik Solenbergaf9fb212015-08-26 10:45:53 +02001800 if (codec.clockrate == send_codec.plfreq && send_codec.channels != 2) {
Minyue Li7100dcd2015-03-27 05:05:59 +01001801 // TODO(minyue): If CN frequency == 48000 Hz is allowed, consider the
1802 // interaction between VAD and Opus FEC.
wu@webrtc.orgcadf9042013-08-30 21:24:16 +00001803 LOG(LS_INFO) << "Enabling VAD";
1804 if (engine()->voe()->codec()->SetVADStatus(channel, true) == -1) {
1805 LOG_RTCERR2(SetVADStatus, channel, true);
1806 return false;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001807 }
1808 }
1809 }
wu@webrtc.org1d1ffc92013-10-16 18:12:02 +00001810 }
wu@webrtc.orgcadf9042013-08-30 21:24:16 +00001811 return true;
1812}
1813
1814bool WebRtcVoiceMediaChannel::SetSendCodecs(
1815 const std::vector<AudioCodec>& codecs) {
solenberg566ef242015-11-06 15:34:49 -08001816 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
solenbergd97ec302015-10-07 01:40:33 -07001817
wu@webrtc.orgcadf9042013-08-30 21:24:16 +00001818 dtmf_allowed_ = false;
Fredrik Solenbergaf9fb212015-08-26 10:45:53 +02001819 for (const AudioCodec& codec : codecs) {
wu@webrtc.orgcadf9042013-08-30 21:24:16 +00001820 // Find the DTMF telephone event "codec".
Fredrik Solenbergaf9fb212015-08-26 10:45:53 +02001821 if (IsCodec(codec, kDtmfCodecName)) {
wu@webrtc.orgcadf9042013-08-30 21:24:16 +00001822 dtmf_allowed_ = true;
1823 }
1824 }
1825
1826 // Cache the codecs in order to configure the channel created later.
1827 send_codecs_ = codecs;
solenbergc96df772015-10-21 13:01:53 -07001828 for (const auto& ch : send_streams_) {
Fredrik Solenbergaf9fb212015-08-26 10:45:53 +02001829 if (!SetSendCodecs(ch.second->channel(), codecs)) {
wu@webrtc.orgcadf9042013-08-30 21:24:16 +00001830 return false;
1831 }
1832 }
1833
wu@webrtc.org05e7b442014-04-01 17:44:24 +00001834 // Set nack status on receive channels and update |nack_enabled_|.
solenberg7add0582015-11-20 09:59:34 -08001835 for (const auto& ch : recv_streams_) {
solenberg0a617e22015-10-20 15:49:38 -07001836 SetNack(ch.second->channel(), nack_enabled_);
wu@webrtc.org9dba5252013-08-05 20:36:57 +00001837 }
solenberg0a617e22015-10-20 15:49:38 -07001838
1839 return true;
wu@webrtc.org9dba5252013-08-05 20:36:57 +00001840}
1841
wu@webrtc.orgcadf9042013-08-30 21:24:16 +00001842void WebRtcVoiceMediaChannel::SetNack(int channel, bool nack_enabled) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001843 if (nack_enabled) {
wu@webrtc.orgcadf9042013-08-30 21:24:16 +00001844 LOG(LS_INFO) << "Enabling NACK for channel " << channel;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001845 engine()->voe()->rtp()->SetNACKStatus(channel, true, kNackMaxPackets);
1846 } else {
wu@webrtc.orgcadf9042013-08-30 21:24:16 +00001847 LOG(LS_INFO) << "Disabling NACK for channel " << channel;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001848 engine()->voe()->rtp()->SetNACKStatus(channel, false, 0);
1849 }
1850}
1851
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001852bool WebRtcVoiceMediaChannel::SetSendCodec(
wu@webrtc.org9dba5252013-08-05 20:36:57 +00001853 int channel, const webrtc::CodecInst& send_codec) {
1854 LOG(LS_INFO) << "Send channel " << channel << " selected voice codec "
1855 << ToString(send_codec) << ", bitrate=" << send_codec.rate;
1856
wu@webrtc.org05e7b442014-04-01 17:44:24 +00001857 webrtc::CodecInst current_codec;
1858 if (engine()->voe()->codec()->GetSendCodec(channel, current_codec) == 0 &&
1859 (send_codec == current_codec)) {
1860 // Codec is already configured, we can return without setting it again.
1861 return true;
1862 }
1863
wu@webrtc.org9dba5252013-08-05 20:36:57 +00001864 if (engine()->voe()->codec()->SetSendCodec(channel, send_codec) == -1) {
1865 LOG_RTCERR2(SetSendCodec, channel, ToString(send_codec));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001866 return false;
1867 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001868 return true;
1869}
1870
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001871bool WebRtcVoiceMediaChannel::SetPlayout(bool playout) {
1872 desired_playout_ = playout;
1873 return ChangePlayout(desired_playout_);
1874}
1875
1876bool WebRtcVoiceMediaChannel::PausePlayout() {
1877 return ChangePlayout(false);
1878}
1879
1880bool WebRtcVoiceMediaChannel::ResumePlayout() {
1881 return ChangePlayout(desired_playout_);
1882}
1883
1884bool WebRtcVoiceMediaChannel::ChangePlayout(bool playout) {
solenberg566ef242015-11-06 15:34:49 -08001885 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001886 if (playout_ == playout) {
1887 return true;
1888 }
1889
solenberg7add0582015-11-20 09:59:34 -08001890 for (const auto& ch : recv_streams_) {
Fredrik Solenbergaf9fb212015-08-26 10:45:53 +02001891 if (!SetPlayout(ch.second->channel(), playout)) {
henrike@webrtc.org1e09a712013-07-26 19:17:59 +00001892 LOG(LS_ERROR) << "SetPlayout " << playout << " on channel "
Fredrik Solenbergaf9fb212015-08-26 10:45:53 +02001893 << ch.second->channel() << " failed";
solenberg1ac56142015-10-13 03:58:19 -07001894 return false;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001895 }
1896 }
solenberg1ac56142015-10-13 03:58:19 -07001897 playout_ = playout;
1898 return true;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001899}
1900
1901bool WebRtcVoiceMediaChannel::SetSend(SendFlags send) {
1902 desired_send_ = send;
solenbergc96df772015-10-21 13:01:53 -07001903 if (!send_streams_.empty()) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001904 return ChangeSend(desired_send_);
solenbergc96df772015-10-21 13:01:53 -07001905 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001906 return true;
1907}
1908
1909bool WebRtcVoiceMediaChannel::PauseSend() {
1910 return ChangeSend(SEND_NOTHING);
1911}
1912
1913bool WebRtcVoiceMediaChannel::ResumeSend() {
1914 return ChangeSend(desired_send_);
1915}
1916
1917bool WebRtcVoiceMediaChannel::ChangeSend(SendFlags send) {
1918 if (send_ == send) {
1919 return true;
1920 }
1921
solenberg63b34542015-09-29 06:06:31 -07001922 // Apply channel specific options.
1923 if (send == SEND_MICROPHONE) {
1924 engine()->ApplyOptions(options_);
1925 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001926
wu@webrtc.org9dba5252013-08-05 20:36:57 +00001927 // Change the settings on each send channel.
solenbergc96df772015-10-21 13:01:53 -07001928 for (const auto& ch : send_streams_) {
solenberg63b34542015-09-29 06:06:31 -07001929 if (!ChangeSend(ch.second->channel(), send)) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001930 return false;
solenberg63b34542015-09-29 06:06:31 -07001931 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001932 }
wu@webrtc.org9dba5252013-08-05 20:36:57 +00001933
solenberg63b34542015-09-29 06:06:31 -07001934 // Clear up the options after stopping sending. Since we may previously have
1935 // applied the channel specific options, now apply the original options stored
1936 // in WebRtcVoiceEngine.
1937 if (send == SEND_NOTHING) {
1938 engine()->ApplyOptions(engine()->GetOptions());
1939 }
wu@webrtc.org9dba5252013-08-05 20:36:57 +00001940
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001941 send_ = send;
1942 return true;
1943}
1944
wu@webrtc.org9dba5252013-08-05 20:36:57 +00001945bool WebRtcVoiceMediaChannel::ChangeSend(int channel, SendFlags send) {
1946 if (send == SEND_MICROPHONE) {
1947 if (engine()->voe()->base()->StartSend(channel) == -1) {
1948 LOG_RTCERR1(StartSend, channel);
1949 return false;
1950 }
wu@webrtc.org9dba5252013-08-05 20:36:57 +00001951 } else { // SEND_NOTHING
henrikg91d6ede2015-09-17 00:24:34 -07001952 RTC_DCHECK(send == SEND_NOTHING);
wu@webrtc.org9dba5252013-08-05 20:36:57 +00001953 if (engine()->voe()->base()->StopSend(channel) == -1) {
1954 LOG_RTCERR1(StopSend, channel);
1955 return false;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001956 }
1957 }
1958
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001959 return true;
1960}
1961
Peter Boström0c4e06b2015-10-07 12:23:21 +02001962bool WebRtcVoiceMediaChannel::SetAudioSend(uint32_t ssrc,
1963 bool enable,
solenberg1dd98f32015-09-10 01:57:14 -07001964 const AudioOptions* options,
1965 AudioRenderer* renderer) {
solenberg566ef242015-11-06 15:34:49 -08001966 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
solenberg1dd98f32015-09-10 01:57:14 -07001967 // TODO(solenberg): The state change should be fully rolled back if any one of
1968 // these calls fail.
1969 if (!SetLocalRenderer(ssrc, renderer)) {
1970 return false;
1971 }
solenbergdfc8f4f2015-10-01 02:31:10 -07001972 if (!MuteStream(ssrc, !enable)) {
solenberg1dd98f32015-09-10 01:57:14 -07001973 return false;
1974 }
solenbergdfc8f4f2015-10-01 02:31:10 -07001975 if (enable && options) {
solenberg1dd98f32015-09-10 01:57:14 -07001976 return SetOptions(*options);
1977 }
1978 return true;
1979}
1980
solenberg0a617e22015-10-20 15:49:38 -07001981int WebRtcVoiceMediaChannel::CreateVoEChannel() {
1982 int id = engine()->CreateVoEChannel();
1983 if (id == -1) {
1984 LOG_RTCERR0(CreateVoEChannel);
1985 return -1;
wu@webrtc.org9dba5252013-08-05 20:36:57 +00001986 }
solenberg0a617e22015-10-20 15:49:38 -07001987 if (engine()->voe()->network()->RegisterExternalTransport(id, *this) == -1) {
1988 LOG_RTCERR2(RegisterExternalTransport, id, this);
1989 engine()->voe()->base()->DeleteChannel(id);
1990 return -1;
1991 }
1992 return id;
wu@webrtc.org9dba5252013-08-05 20:36:57 +00001993}
1994
solenberg7add0582015-11-20 09:59:34 -08001995bool WebRtcVoiceMediaChannel::DeleteVoEChannel(int channel) {
wu@webrtc.org9dba5252013-08-05 20:36:57 +00001996 if (engine()->voe()->network()->DeRegisterExternalTransport(channel) == -1) {
1997 LOG_RTCERR1(DeRegisterExternalTransport, channel);
1998 }
wu@webrtc.org9dba5252013-08-05 20:36:57 +00001999 if (engine()->voe()->base()->DeleteChannel(channel) == -1) {
2000 LOG_RTCERR1(DeleteChannel, channel);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002001 return false;
2002 }
wu@webrtc.org9dba5252013-08-05 20:36:57 +00002003 return true;
2004}
henrike@webrtc.org1e09a712013-07-26 19:17:59 +00002005
wu@webrtc.org9dba5252013-08-05 20:36:57 +00002006bool WebRtcVoiceMediaChannel::AddSendStream(const StreamParams& sp) {
solenberg566ef242015-11-06 15:34:49 -08002007 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
solenberg0a617e22015-10-20 15:49:38 -07002008 LOG(LS_INFO) << "AddSendStream: " << sp.ToString();
2009
2010 uint32_t ssrc = sp.first_ssrc();
2011 RTC_DCHECK(0 != ssrc);
2012
2013 if (GetSendChannelId(ssrc) != -1) {
2014 LOG(LS_ERROR) << "Stream already exists with ssrc " << ssrc;
wu@webrtc.org9dba5252013-08-05 20:36:57 +00002015 return false;
2016 }
2017
solenberg0a617e22015-10-20 15:49:38 -07002018 // Create a new channel for sending audio data.
2019 int channel = CreateVoEChannel();
2020 if (channel == -1) {
2021 return false;
wu@webrtc.org9dba5252013-08-05 20:36:57 +00002022 }
wu@webrtc.org9dba5252013-08-05 20:36:57 +00002023
solenbergc96df772015-10-21 13:01:53 -07002024 // Save the channel to send_streams_, so that RemoveSendStream() can still
wu@webrtc.org9dba5252013-08-05 20:36:57 +00002025 // delete the channel in case failure happens below.
mallinath@webrtc.org67ee6b92014-02-03 16:57:16 +00002026 webrtc::AudioTransport* audio_transport =
2027 engine()->voe()->base()->audio_transport();
solenberg3a941542015-11-16 07:34:50 -08002028 send_streams_.insert(std::make_pair(ssrc, new WebRtcAudioSendStream(
2029 channel, audio_transport, ssrc, sp.cname, send_rtp_extensions_, call_)));
wu@webrtc.org9dba5252013-08-05 20:36:57 +00002030
solenberg0a617e22015-10-20 15:49:38 -07002031 // Set the current codecs to be used for the new channel. We need to do this
2032 // after adding the channel to send_channels_, because of how max bitrate is
2033 // currently being configured by SetSendCodec().
2034 if (!send_codecs_.empty() && !SetSendCodecs(channel, send_codecs_)) {
2035 RemoveSendStream(ssrc);
wu@webrtc.org9dba5252013-08-05 20:36:57 +00002036 return false;
2037 }
2038
2039 // At this point the channel's local SSRC has been updated. If the channel is
solenberg0a617e22015-10-20 15:49:38 -07002040 // the first send channel make sure that all the receive channels are updated
2041 // with the same SSRC in order to send receiver reports.
solenbergc96df772015-10-21 13:01:53 -07002042 if (send_streams_.size() == 1) {
solenberg0a617e22015-10-20 15:49:38 -07002043 receiver_reports_ssrc_ = ssrc;
solenberg7add0582015-11-20 09:59:34 -08002044 for (const auto& stream : recv_streams_) {
2045 int recv_channel = stream.second->channel();
solenberg0a617e22015-10-20 15:49:38 -07002046 if (engine()->voe()->rtp()->SetLocalSSRC(recv_channel, ssrc) != 0) {
solenberg7add0582015-11-20 09:59:34 -08002047 LOG_RTCERR2(SetLocalSSRC, recv_channel, ssrc);
solenberg1ac56142015-10-13 03:58:19 -07002048 return false;
wu@webrtc.org9dba5252013-08-05 20:36:57 +00002049 }
solenberg0a617e22015-10-20 15:49:38 -07002050 engine()->voe()->base()->AssociateSendChannel(recv_channel, channel);
2051 LOG(LS_INFO) << "VoiceEngine channel #" << recv_channel
2052 << " is associated with channel #" << channel << ".";
wu@webrtc.org9dba5252013-08-05 20:36:57 +00002053 }
2054 }
2055
wu@webrtc.org9dba5252013-08-05 20:36:57 +00002056 return ChangeSend(channel, desired_send_);
2057}
2058
Peter Boström0c4e06b2015-10-07 12:23:21 +02002059bool WebRtcVoiceMediaChannel::RemoveSendStream(uint32_t ssrc) {
solenberg566ef242015-11-06 15:34:49 -08002060 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
solenberg3a941542015-11-16 07:34:50 -08002061 LOG(LS_INFO) << "RemoveSendStream: " << ssrc;
2062
solenbergc96df772015-10-21 13:01:53 -07002063 auto it = send_streams_.find(ssrc);
2064 if (it == send_streams_.end()) {
wu@webrtc.org9dba5252013-08-05 20:36:57 +00002065 LOG(LS_WARNING) << "Try to remove stream with ssrc " << ssrc
2066 << " which doesn't exist.";
2067 return false;
2068 }
2069
mallinath@webrtc.org67ee6b92014-02-03 16:57:16 +00002070 int channel = it->second->channel();
wu@webrtc.org9dba5252013-08-05 20:36:57 +00002071 ChangeSend(channel, SEND_NOTHING);
2072
solenberg7add0582015-11-20 09:59:34 -08002073 // Clean up and delete the send stream+channel.
solenberg0a617e22015-10-20 15:49:38 -07002074 LOG(LS_INFO) << "Removing audio send stream " << ssrc
2075 << " with VoiceEngine channel #" << channel << ".";
solenberg7add0582015-11-20 09:59:34 -08002076 delete it->second;
2077 send_streams_.erase(it);
2078 if (!DeleteVoEChannel(channel)) {
solenberg0a617e22015-10-20 15:49:38 -07002079 return false;
wu@webrtc.org9dba5252013-08-05 20:36:57 +00002080 }
solenbergc96df772015-10-21 13:01:53 -07002081 if (send_streams_.empty()) {
wu@webrtc.org9dba5252013-08-05 20:36:57 +00002082 ChangeSend(SEND_NOTHING);
solenberg0a617e22015-10-20 15:49:38 -07002083 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002084 return true;
2085}
2086
2087bool WebRtcVoiceMediaChannel::AddRecvStream(const StreamParams& sp) {
solenberg566ef242015-11-06 15:34:49 -08002088 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
solenbergd97ec302015-10-07 01:40:33 -07002089 LOG(LS_INFO) << "AddRecvStream: " << sp.ToString();
2090
solenberg0b675462015-10-09 01:37:09 -07002091 if (!ValidateStreamParams(sp)) {
wu@webrtc.org78187522013-10-07 23:32:02 +00002092 return false;
2093 }
2094
solenberg7add0582015-11-20 09:59:34 -08002095 const uint32_t ssrc = sp.first_ssrc();
solenberg0b675462015-10-09 01:37:09 -07002096 if (ssrc == 0) {
2097 LOG(LS_WARNING) << "AddRecvStream with ssrc==0 is not supported.";
2098 return false;
2099 }
2100
solenberg1ac56142015-10-13 03:58:19 -07002101 // Remove the default receive stream if one had been created with this ssrc;
2102 // we'll recreate it then.
2103 if (IsDefaultRecvStream(ssrc)) {
2104 RemoveRecvStream(ssrc);
2105 }
solenberg0b675462015-10-09 01:37:09 -07002106
solenberg7add0582015-11-20 09:59:34 -08002107 if (GetReceiveChannelId(ssrc) != -1) {
henrike@webrtc.org1e09a712013-07-26 19:17:59 +00002108 LOG(LS_ERROR) << "Stream already exists with ssrc " << ssrc;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002109 return false;
2110 }
Fredrik Solenberg4b60c732015-05-07 14:07:48 +02002111
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002112 // Create a new channel for receiving audio data.
solenberg7add0582015-11-20 09:59:34 -08002113 const int channel = CreateVoEChannel();
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002114 if (channel == -1) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002115 return false;
2116 }
Minyue2013aec2015-05-13 14:14:42 +02002117
solenberg1ac56142015-10-13 03:58:19 -07002118 // Turn off all supported codecs.
solenberg7add0582015-11-20 09:59:34 -08002119 const int ncodecs = engine()->voe()->codec()->NumOfCodecs();
solenberg1ac56142015-10-13 03:58:19 -07002120 for (int i = 0; i < ncodecs; ++i) {
2121 webrtc::CodecInst voe_codec;
2122 if (engine()->voe()->codec()->GetCodec(i, voe_codec) != -1) {
2123 voe_codec.pltype = -1;
2124 if (engine()->voe()->codec()->SetRecPayloadType(
2125 channel, voe_codec) == -1) {
2126 LOG_RTCERR2(SetRecPayloadType, channel, ToString(voe_codec));
solenberg7add0582015-11-20 09:59:34 -08002127 DeleteVoEChannel(channel);
solenberg1ac56142015-10-13 03:58:19 -07002128 return false;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002129 }
2130 }
2131 }
2132
solenberg1ac56142015-10-13 03:58:19 -07002133 // Only enable those configured for this channel.
2134 for (const auto& codec : recv_codecs_) {
2135 webrtc::CodecInst voe_codec;
2136 if (engine()->FindWebRtcCodec(codec, &voe_codec)) {
2137 voe_codec.pltype = codec.id;
2138 if (engine()->voe()->codec()->SetRecPayloadType(
2139 channel, voe_codec) == -1) {
2140 LOG_RTCERR2(SetRecPayloadType, channel, ToString(voe_codec));
solenberg7add0582015-11-20 09:59:34 -08002141 DeleteVoEChannel(channel);
solenberg1ac56142015-10-13 03:58:19 -07002142 return false;
2143 }
henrike@webrtc.org1e09a712013-07-26 19:17:59 +00002144 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002145 }
solenberg8fb30c32015-10-13 03:06:58 -07002146
solenberg7add0582015-11-20 09:59:34 -08002147 const int send_channel = GetSendChannelId(receiver_reports_ssrc_);
2148 if (send_channel != -1) {
2149 // Associate receive channel with first send channel (so the receive channel
2150 // can obtain RTT from the send channel)
2151 engine()->voe()->base()->AssociateSendChannel(channel, send_channel);
2152 LOG(LS_INFO) << "VoiceEngine channel #" << channel
2153 << " is associated with channel #" << send_channel << ".";
buildbot@webrtc.org150835e2014-05-06 15:54:38 +00002154 }
2155
solenberg7add0582015-11-20 09:59:34 -08002156 recv_streams_.insert(std::make_pair(ssrc, new WebRtcAudioReceiveStream(
2157 channel, ssrc, receiver_reports_ssrc_,
2158 options_.combined_audio_video_bwe.value_or(false), sp.sync_label,
2159 recv_rtp_extensions_, call_)));
2160
2161 SetNack(channel, nack_enabled_);
solenberg1ac56142015-10-13 03:58:19 -07002162 SetPlayout(channel, playout_);
solenberg7add0582015-11-20 09:59:34 -08002163
solenberg1ac56142015-10-13 03:58:19 -07002164 return true;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002165}
2166
Peter Boström0c4e06b2015-10-07 12:23:21 +02002167bool WebRtcVoiceMediaChannel::RemoveRecvStream(uint32_t ssrc) {
solenberg566ef242015-11-06 15:34:49 -08002168 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
solenbergd97ec302015-10-07 01:40:33 -07002169 LOG(LS_INFO) << "RemoveRecvStream: " << ssrc;
2170
solenberg7add0582015-11-20 09:59:34 -08002171 const auto it = recv_streams_.find(ssrc);
2172 if (it == recv_streams_.end()) {
wu@webrtc.org9dba5252013-08-05 20:36:57 +00002173 LOG(LS_WARNING) << "Try to remove stream with ssrc " << ssrc
2174 << " which doesn't exist.";
henrike@webrtc.org1e09a712013-07-26 19:17:59 +00002175 return false;
wu@webrtc.org9dba5252013-08-05 20:36:57 +00002176 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002177
solenberg1ac56142015-10-13 03:58:19 -07002178 // Deregister default channel, if that's the one being destroyed.
2179 if (IsDefaultRecvStream(ssrc)) {
2180 default_recv_ssrc_ = -1;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002181 }
henrike@webrtc.org1e09a712013-07-26 19:17:59 +00002182
solenberg7add0582015-11-20 09:59:34 -08002183 const int channel = it->second->channel();
2184
2185 // Clean up and delete the receive stream+channel.
2186 LOG(LS_INFO) << "Removing audio receive stream " << ssrc
mallinath@webrtc.org67ee6b92014-02-03 16:57:16 +00002187 << " with VoiceEngine channel #" << channel << ".";
solenberg7add0582015-11-20 09:59:34 -08002188 delete it->second;
2189 recv_streams_.erase(it);
2190 return DeleteVoEChannel(channel);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002191}
2192
Peter Boström0c4e06b2015-10-07 12:23:21 +02002193bool WebRtcVoiceMediaChannel::SetLocalRenderer(uint32_t ssrc,
henrike@webrtc.org1e09a712013-07-26 19:17:59 +00002194 AudioRenderer* renderer) {
solenbergc96df772015-10-21 13:01:53 -07002195 auto it = send_streams_.find(ssrc);
2196 if (it == send_streams_.end()) {
wu@webrtc.org9dba5252013-08-05 20:36:57 +00002197 if (renderer) {
2198 // Return an error if trying to set a valid renderer with an invalid ssrc.
2199 LOG(LS_ERROR) << "SetLocalRenderer failed with ssrc "<< ssrc;
2200 return false;
2201 }
2202
2203 // The channel likely has gone away, do nothing.
henrike@webrtc.org1e09a712013-07-26 19:17:59 +00002204 return true;
henrike@webrtc.org1e09a712013-07-26 19:17:59 +00002205 }
2206
solenberg1ac56142015-10-13 03:58:19 -07002207 if (renderer) {
mallinath@webrtc.org67ee6b92014-02-03 16:57:16 +00002208 it->second->Start(renderer);
solenberg1ac56142015-10-13 03:58:19 -07002209 } else {
mallinath@webrtc.org67ee6b92014-02-03 16:57:16 +00002210 it->second->Stop();
solenberg1ac56142015-10-13 03:58:19 -07002211 }
henrike@webrtc.org1e09a712013-07-26 19:17:59 +00002212
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002213 return true;
2214}
2215
2216bool WebRtcVoiceMediaChannel::GetActiveStreams(
2217 AudioInfo::StreamList* actives) {
solenberg566ef242015-11-06 15:34:49 -08002218 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002219 actives->clear();
solenberg7add0582015-11-20 09:59:34 -08002220 for (const auto& ch : recv_streams_) {
Fredrik Solenbergaf9fb212015-08-26 10:45:53 +02002221 int level = GetOutputLevel(ch.second->channel());
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002222 if (level > 0) {
Fredrik Solenbergaf9fb212015-08-26 10:45:53 +02002223 actives->push_back(std::make_pair(ch.first, level));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002224 }
2225 }
2226 return true;
2227}
2228
2229int WebRtcVoiceMediaChannel::GetOutputLevel() {
solenberg566ef242015-11-06 15:34:49 -08002230 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
solenberg1ac56142015-10-13 03:58:19 -07002231 int highest = 0;
solenberg7add0582015-11-20 09:59:34 -08002232 for (const auto& ch : recv_streams_) {
solenberg8fb30c32015-10-13 03:06:58 -07002233 highest = std::max(GetOutputLevel(ch.second->channel()), highest);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002234 }
2235 return highest;
2236}
2237
2238int WebRtcVoiceMediaChannel::GetTimeSinceLastTyping() {
2239 int ret;
2240 if (engine()->voe()->processing()->TimeSinceLastTyping(ret) == -1) {
2241 // In case of error, log the info and continue
2242 LOG_RTCERR0(TimeSinceLastTyping);
2243 ret = -1;
2244 } else {
2245 ret *= 1000; // We return ms, webrtc returns seconds.
2246 }
2247 return ret;
2248}
2249
2250void WebRtcVoiceMediaChannel::SetTypingDetectionParameters(int time_window,
2251 int cost_per_typing, int reporting_threshold, int penalty_decay,
2252 int type_event_delay) {
2253 if (engine()->voe()->processing()->SetTypingDetectionParameters(
2254 time_window, cost_per_typing,
2255 reporting_threshold, penalty_decay, type_event_delay) == -1) {
2256 // In case of error, log the info and continue
2257 LOG_RTCERR5(SetTypingDetectionParameters, time_window,
2258 cost_per_typing, reporting_threshold, penalty_decay,
2259 type_event_delay);
2260 }
2261}
2262
solenberg4bac9c52015-10-09 02:32:53 -07002263bool WebRtcVoiceMediaChannel::SetOutputVolume(uint32_t ssrc, double volume) {
solenberg566ef242015-11-06 15:34:49 -08002264 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
solenberg1ac56142015-10-13 03:58:19 -07002265 if (ssrc == 0) {
2266 default_recv_volume_ = volume;
2267 if (default_recv_ssrc_ == -1) {
2268 return true;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002269 }
solenberg1ac56142015-10-13 03:58:19 -07002270 ssrc = static_cast<uint32_t>(default_recv_ssrc_);
2271 }
2272 int ch_id = GetReceiveChannelId(ssrc);
2273 if (ch_id < 0) {
2274 LOG(LS_WARNING) << "Cannot find channel for ssrc:" << ssrc;
2275 return false;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002276 }
2277
solenberg1ac56142015-10-13 03:58:19 -07002278 if (-1 == engine()->voe()->volume()->SetChannelOutputVolumeScaling(ch_id,
2279 volume)) {
2280 LOG_RTCERR2(SetChannelOutputVolumeScaling, ch_id, volume);
2281 return false;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002282 }
solenberg1ac56142015-10-13 03:58:19 -07002283 LOG(LS_INFO) << "SetOutputVolume to " << volume
2284 << " for channel " << ch_id << " and ssrc " << ssrc;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002285 return true;
2286}
2287
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002288bool WebRtcVoiceMediaChannel::CanInsertDtmf() {
2289 return dtmf_allowed_;
2290}
2291
Peter Boström0c4e06b2015-10-07 12:23:21 +02002292bool WebRtcVoiceMediaChannel::InsertDtmf(uint32_t ssrc,
2293 int event,
2294 int duration,
2295 int flags) {
solenberg566ef242015-11-06 15:34:49 -08002296 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002297 if (!dtmf_allowed_) {
2298 return false;
2299 }
2300
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002301 // Send the event.
2302 if (flags & cricket::DF_SEND) {
wu@webrtc.orgcadf9042013-08-30 21:24:16 +00002303 int channel = -1;
2304 if (ssrc == 0) {
solenbergc96df772015-10-21 13:01:53 -07002305 if (send_streams_.size() > 0) {
2306 channel = send_streams_.begin()->second->channel();
wu@webrtc.orgcadf9042013-08-30 21:24:16 +00002307 }
2308 } else {
solenbergd97ec302015-10-07 01:40:33 -07002309 channel = GetSendChannelId(ssrc);
wu@webrtc.orgcadf9042013-08-30 21:24:16 +00002310 }
wu@webrtc.org9dba5252013-08-05 20:36:57 +00002311 if (channel == -1) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002312 LOG(LS_WARNING) << "InsertDtmf - The specified ssrc "
2313 << ssrc << " is not in use.";
2314 return false;
2315 }
2316 // Send DTMF using out-of-band DTMF. ("true", as 3rd arg)
wu@webrtc.org9dba5252013-08-05 20:36:57 +00002317 if (engine()->voe()->dtmf()->SendTelephoneEvent(
2318 channel, event, true, duration) == -1) {
2319 LOG_RTCERR4(SendTelephoneEvent, channel, event, true, duration);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002320 return false;
2321 }
2322 }
2323
2324 // Play the event.
2325 if (flags & cricket::DF_PLAY) {
2326 // Play DTMF tone locally.
2327 if (engine()->voe()->dtmf()->PlayDtmfTone(event, duration) == -1) {
2328 LOG_RTCERR2(PlayDtmfTone, event, duration);
2329 return false;
2330 }
2331 }
2332
2333 return true;
2334}
2335
wu@webrtc.orga9890802013-12-13 00:21:03 +00002336void WebRtcVoiceMediaChannel::OnPacketReceived(
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00002337 rtc::Buffer* packet, const rtc::PacketTime& packet_time) {
solenberg566ef242015-11-06 15:34:49 -08002338 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
Fredrik Solenberg4b60c732015-05-07 14:07:48 +02002339
solenberg1ac56142015-10-13 03:58:19 -07002340 uint32_t ssrc = 0;
2341 if (!GetRtpSsrc(packet->data(), packet->size(), &ssrc)) {
2342 return;
2343 }
2344
solenberg7e63ef02015-11-20 00:19:43 -08002345 // If we don't have a default channel, and the SSRC is unknown, create a
2346 // default channel.
2347 if (default_recv_ssrc_ == -1 && GetReceiveChannelId(ssrc) == -1) {
solenberg1ac56142015-10-13 03:58:19 -07002348 StreamParams sp;
2349 sp.ssrcs.push_back(ssrc);
2350 LOG(LS_INFO) << "Creating default receive stream for SSRC=" << ssrc << ".";
2351 if (!AddRecvStream(sp)) {
2352 LOG(LS_WARNING) << "Could not create default receive stream.";
2353 return;
2354 }
2355 default_recv_ssrc_ = ssrc;
2356 SetOutputVolume(default_recv_ssrc_, default_recv_volume_);
2357 }
2358
2359 // Forward packet to Call. If the SSRC is unknown we'll return after this.
Fredrik Solenberg709ed672015-09-15 12:26:33 +02002360 const webrtc::PacketTime webrtc_packet_time(packet_time.timestamp,
2361 packet_time.not_before);
solenberg1ac56142015-10-13 03:58:19 -07002362 webrtc::PacketReceiver::DeliveryStatus delivery_result =
2363 call_->Receiver()->DeliverPacket(webrtc::MediaType::AUDIO,
2364 reinterpret_cast<const uint8_t*>(packet->data()), packet->size(),
2365 webrtc_packet_time);
2366 if (webrtc::PacketReceiver::DELIVERY_OK != delivery_result) {
solenberg7e63ef02015-11-20 00:19:43 -08002367 // If the SSRC is unknown here, route it to the default channel, if we have
2368 // one. See: https://bugs.chromium.org/p/webrtc/issues/detail?id=5208
2369 if (default_recv_ssrc_ == -1) {
2370 return;
2371 } else {
2372 ssrc = default_recv_ssrc_;
2373 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002374 }
2375
solenberg1ac56142015-10-13 03:58:19 -07002376 // Find the channel to send this packet to. It must exist since webrtc::Call
2377 // was able to demux the packet.
2378 int channel = GetReceiveChannelId(ssrc);
2379 RTC_DCHECK(channel != -1);
2380
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002381 // Pass it off to the decoder.
henrike@webrtc.org28654cb2013-07-22 21:07:49 +00002382 engine()->voe()->network()->ReceivedRTPPacket(
solenberg1ac56142015-10-13 03:58:19 -07002383 channel, packet->data(), packet->size(), webrtc_packet_time);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002384}
2385
wu@webrtc.orga9890802013-12-13 00:21:03 +00002386void WebRtcVoiceMediaChannel::OnRtcpReceived(
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00002387 rtc::Buffer* packet, const rtc::PacketTime& packet_time) {
solenberg566ef242015-11-06 15:34:49 -08002388 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
Fredrik Solenberg4b60c732015-05-07 14:07:48 +02002389
Fredrik Solenberg709ed672015-09-15 12:26:33 +02002390 // Forward packet to Call as well.
2391 const webrtc::PacketTime webrtc_packet_time(packet_time.timestamp,
2392 packet_time.not_before);
2393 call_->Receiver()->DeliverPacket(webrtc::MediaType::AUDIO,
2394 reinterpret_cast<const uint8_t*>(packet->data()), packet->size(),
2395 webrtc_packet_time);
Fredrik Solenberg4b60c732015-05-07 14:07:48 +02002396
wu@webrtc.org9dba5252013-08-05 20:36:57 +00002397 // Sending channels need all RTCP packets with feedback information.
2398 // Even sender reports can contain attached report blocks.
2399 // Receiving channels need sender reports in order to create
2400 // correct receiver reports.
2401 int type = 0;
kwiberg@webrtc.orgeebcab52015-03-24 09:19:06 +00002402 if (!GetRtcpType(packet->data(), packet->size(), &type)) {
wu@webrtc.org9dba5252013-08-05 20:36:57 +00002403 LOG(LS_WARNING) << "Failed to parse type from received RTCP packet";
2404 return;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002405 }
2406
solenberg0b675462015-10-09 01:37:09 -07002407 // If it is a sender report, find the receive channel that is listening.
wu@webrtc.org9dba5252013-08-05 20:36:57 +00002408 if (type == kRtcpTypeSR) {
solenberg0b675462015-10-09 01:37:09 -07002409 uint32_t ssrc = 0;
2410 if (!GetRtcpSsrc(packet->data(), packet->size(), &ssrc)) {
2411 return;
2412 }
2413 int recv_channel_id = GetReceiveChannelId(ssrc);
2414 if (recv_channel_id != -1) {
wu@webrtc.org9dba5252013-08-05 20:36:57 +00002415 engine()->voe()->network()->ReceivedRTCPPacket(
solenberg0b675462015-10-09 01:37:09 -07002416 recv_channel_id, packet->data(), packet->size());
wu@webrtc.org9dba5252013-08-05 20:36:57 +00002417 }
2418 }
2419
2420 // SR may continue RR and any RR entry may correspond to any one of the send
2421 // channels. So all RTCP packets must be forwarded all send channels. VoE
2422 // will filter out RR internally.
solenbergc96df772015-10-21 13:01:53 -07002423 for (const auto& ch : send_streams_) {
wu@webrtc.org9dba5252013-08-05 20:36:57 +00002424 engine()->voe()->network()->ReceivedRTCPPacket(
Fredrik Solenbergaf9fb212015-08-26 10:45:53 +02002425 ch.second->channel(), packet->data(), packet->size());
wu@webrtc.org9dba5252013-08-05 20:36:57 +00002426 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002427}
2428
Peter Boström0c4e06b2015-10-07 12:23:21 +02002429bool WebRtcVoiceMediaChannel::MuteStream(uint32_t ssrc, bool muted) {
solenberg566ef242015-11-06 15:34:49 -08002430 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
solenberg0a617e22015-10-20 15:49:38 -07002431 int channel = GetSendChannelId(ssrc);
wu@webrtc.org9dba5252013-08-05 20:36:57 +00002432 if (channel == -1) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002433 LOG(LS_WARNING) << "The specified ssrc " << ssrc << " is not in use.";
2434 return false;
2435 }
wu@webrtc.org9dba5252013-08-05 20:36:57 +00002436 if (engine()->voe()->volume()->SetInputMute(channel, muted) == -1) {
2437 LOG_RTCERR2(SetInputMute, channel, muted);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002438 return false;
2439 }
buildbot@webrtc.org6b21b712014-07-31 15:08:53 +00002440 // We set the AGC to mute state only when all the channels are muted.
2441 // This implementation is not ideal, instead we should signal the AGC when
2442 // the mic channel is muted/unmuted. We can't do it today because there
2443 // is no good way to know which stream is mapping to the mic channel.
2444 bool all_muted = muted;
solenbergc96df772015-10-21 13:01:53 -07002445 for (const auto& ch : send_streams_) {
Fredrik Solenbergaf9fb212015-08-26 10:45:53 +02002446 if (!all_muted) {
2447 break;
2448 }
2449 if (engine()->voe()->volume()->GetInputMute(ch.second->channel(),
buildbot@webrtc.org6b21b712014-07-31 15:08:53 +00002450 all_muted)) {
Fredrik Solenbergaf9fb212015-08-26 10:45:53 +02002451 LOG_RTCERR1(GetInputMute, ch.second->channel());
buildbot@webrtc.org6b21b712014-07-31 15:08:53 +00002452 return false;
2453 }
2454 }
2455
2456 webrtc::AudioProcessing* ap = engine()->voe()->base()->audio_processing();
solenberg0a617e22015-10-20 15:49:38 -07002457 if (ap) {
buildbot@webrtc.org6b21b712014-07-31 15:08:53 +00002458 ap->set_output_will_be_muted(all_muted);
solenberg0a617e22015-10-20 15:49:38 -07002459 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002460 return true;
2461}
2462
minyue@webrtc.org26236952014-10-29 02:27:08 +00002463// TODO(minyue): SetMaxSendBandwidth() is subject to be renamed to
2464// SetMaxSendBitrate() in future.
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00002465bool WebRtcVoiceMediaChannel::SetMaxSendBandwidth(int bps) {
minyue@webrtc.org26236952014-10-29 02:27:08 +00002466 LOG(LS_INFO) << "WebRtcVoiceMediaChannel::SetMaxSendBandwidth.";
minyue@webrtc.org26236952014-10-29 02:27:08 +00002467 return SetSendBitrateInternal(bps);
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00002468}
2469
minyue@webrtc.org26236952014-10-29 02:27:08 +00002470bool WebRtcVoiceMediaChannel::SetSendBitrateInternal(int bps) {
2471 LOG(LS_INFO) << "WebRtcVoiceMediaChannel::SetSendBitrateInternal.";
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00002472
minyue@webrtc.org26236952014-10-29 02:27:08 +00002473 send_bitrate_setting_ = true;
2474 send_bitrate_bps_ = bps;
wu@webrtc.org1d1ffc92013-10-16 18:12:02 +00002475
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002476 if (!send_codec_) {
wu@webrtc.org1d1ffc92013-10-16 18:12:02 +00002477 LOG(LS_INFO) << "The send codec has not been set up yet. "
minyue@webrtc.org26236952014-10-29 02:27:08 +00002478 << "The send bitrate setting will be applied later.";
wu@webrtc.org1d1ffc92013-10-16 18:12:02 +00002479 return true;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002480 }
2481
minyue@webrtc.org26236952014-10-29 02:27:08 +00002482 // Bitrate is auto by default.
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00002483 // TODO(bemasc): Fix this so that if SetMaxSendBandwidth(50) is followed by
2484 // SetMaxSendBandwith(0), the second call removes the previous limit.
2485 if (bps <= 0)
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002486 return true;
2487
2488 webrtc::CodecInst codec = *send_codec_;
2489 bool is_multi_rate = IsCodecMultiRate(codec);
2490
2491 if (is_multi_rate) {
2492 // If codec is multi-rate then just set the bitrate.
2493 codec.rate = bps;
solenbergc96df772015-10-21 13:01:53 -07002494 for (const auto& ch : send_streams_) {
solenberg0a617e22015-10-20 15:49:38 -07002495 if (!SetSendCodec(ch.second->channel(), codec)) {
2496 LOG(LS_INFO) << "Failed to set codec " << codec.plname
2497 << " to bitrate " << bps << " bps.";
2498 return false;
2499 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002500 }
2501 return true;
2502 } else {
2503 // If codec is not multi-rate and |bps| is less than the fixed bitrate
2504 // then fail. If codec is not multi-rate and |bps| exceeds or equal the
2505 // fixed bitrate then ignore.
2506 if (bps < codec.rate) {
2507 LOG(LS_INFO) << "Failed to set codec " << codec.plname
2508 << " to bitrate " << bps << " bps"
2509 << ", requires at least " << codec.rate << " bps.";
2510 return false;
2511 }
2512 return true;
2513 }
2514}
2515
2516bool WebRtcVoiceMediaChannel::GetStats(VoiceMediaInfo* info) {
solenberg566ef242015-11-06 15:34:49 -08002517 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
solenberg85a04962015-10-27 03:35:21 -07002518 RTC_DCHECK(info);
solenbergd97ec302015-10-07 01:40:33 -07002519
solenberg85a04962015-10-27 03:35:21 -07002520 // Get SSRC and stats for each sender.
2521 RTC_DCHECK(info->senders.size() == 0);
2522 for (const auto& stream : send_streams_) {
2523 webrtc::AudioSendStream::Stats stats = stream.second->GetStats();
wu@webrtc.org9dba5252013-08-05 20:36:57 +00002524 VoiceSenderInfo sinfo;
solenberg85a04962015-10-27 03:35:21 -07002525 sinfo.add_ssrc(stats.local_ssrc);
2526 sinfo.bytes_sent = stats.bytes_sent;
2527 sinfo.packets_sent = stats.packets_sent;
2528 sinfo.packets_lost = stats.packets_lost;
2529 sinfo.fraction_lost = stats.fraction_lost;
2530 sinfo.codec_name = stats.codec_name;
2531 sinfo.ext_seqnum = stats.ext_seqnum;
2532 sinfo.jitter_ms = stats.jitter_ms;
2533 sinfo.rtt_ms = stats.rtt_ms;
2534 sinfo.audio_level = stats.audio_level;
2535 sinfo.aec_quality_min = stats.aec_quality_min;
2536 sinfo.echo_delay_median_ms = stats.echo_delay_median_ms;
2537 sinfo.echo_delay_std_ms = stats.echo_delay_std_ms;
2538 sinfo.echo_return_loss = stats.echo_return_loss;
2539 sinfo.echo_return_loss_enhancement = stats.echo_return_loss_enhancement;
solenberg566ef242015-11-06 15:34:49 -08002540 sinfo.typing_noise_detected =
2541 (send_ == SEND_NOTHING ? false : stats.typing_noise_detected);
wu@webrtc.org9dba5252013-08-05 20:36:57 +00002542 info->senders.push_back(sinfo);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002543 }
2544
solenberg85a04962015-10-27 03:35:21 -07002545 // Get SSRC and stats for each receiver.
2546 RTC_DCHECK(info->receivers.size() == 0);
solenberg7add0582015-11-20 09:59:34 -08002547 for (const auto& stream : recv_streams_) {
Fredrik Solenberg4f4ec0a2015-10-22 10:49:27 +02002548 webrtc::AudioReceiveStream::Stats stats = stream.second->GetStats();
2549 VoiceReceiverInfo rinfo;
2550 rinfo.add_ssrc(stats.remote_ssrc);
2551 rinfo.bytes_rcvd = stats.bytes_rcvd;
2552 rinfo.packets_rcvd = stats.packets_rcvd;
2553 rinfo.packets_lost = stats.packets_lost;
2554 rinfo.fraction_lost = stats.fraction_lost;
2555 rinfo.codec_name = stats.codec_name;
2556 rinfo.ext_seqnum = stats.ext_seqnum;
2557 rinfo.jitter_ms = stats.jitter_ms;
2558 rinfo.jitter_buffer_ms = stats.jitter_buffer_ms;
2559 rinfo.jitter_buffer_preferred_ms = stats.jitter_buffer_preferred_ms;
2560 rinfo.delay_estimate_ms = stats.delay_estimate_ms;
2561 rinfo.audio_level = stats.audio_level;
2562 rinfo.expand_rate = stats.expand_rate;
2563 rinfo.speech_expand_rate = stats.speech_expand_rate;
2564 rinfo.secondary_decoded_rate = stats.secondary_decoded_rate;
2565 rinfo.accelerate_rate = stats.accelerate_rate;
2566 rinfo.preemptive_expand_rate = stats.preemptive_expand_rate;
2567 rinfo.decoding_calls_to_silence_generator =
2568 stats.decoding_calls_to_silence_generator;
2569 rinfo.decoding_calls_to_neteq = stats.decoding_calls_to_neteq;
2570 rinfo.decoding_normal = stats.decoding_normal;
2571 rinfo.decoding_plc = stats.decoding_plc;
2572 rinfo.decoding_cng = stats.decoding_cng;
2573 rinfo.decoding_plc_cng = stats.decoding_plc_cng;
2574 rinfo.capture_start_ntp_time_ms = stats.capture_start_ntp_time_ms;
2575 info->receivers.push_back(rinfo);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002576 }
2577
2578 return true;
2579}
2580
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002581int WebRtcVoiceMediaChannel::GetOutputLevel(int channel) {
solenbergd97ec302015-10-07 01:40:33 -07002582 unsigned int ulevel = 0;
2583 int ret = engine()->voe()->volume()->GetSpeechOutputLevel(channel, ulevel);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002584 return (ret == 0) ? static_cast<int>(ulevel) : -1;
2585}
2586
Peter Boström0c4e06b2015-10-07 12:23:21 +02002587int WebRtcVoiceMediaChannel::GetReceiveChannelId(uint32_t ssrc) const {
solenberg566ef242015-11-06 15:34:49 -08002588 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
solenberg7add0582015-11-20 09:59:34 -08002589 const auto it = recv_streams_.find(ssrc);
2590 if (it != recv_streams_.end()) {
mallinath@webrtc.org67ee6b92014-02-03 16:57:16 +00002591 return it->second->channel();
solenberg8fb30c32015-10-13 03:06:58 -07002592 }
solenberg1ac56142015-10-13 03:58:19 -07002593 return -1;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002594}
2595
Peter Boström0c4e06b2015-10-07 12:23:21 +02002596int WebRtcVoiceMediaChannel::GetSendChannelId(uint32_t ssrc) const {
solenberg566ef242015-11-06 15:34:49 -08002597 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
solenbergc96df772015-10-21 13:01:53 -07002598 const auto it = send_streams_.find(ssrc);
2599 if (it != send_streams_.end()) {
mallinath@webrtc.org67ee6b92014-02-03 16:57:16 +00002600 return it->second->channel();
solenberg8fb30c32015-10-13 03:06:58 -07002601 }
wu@webrtc.org9dba5252013-08-05 20:36:57 +00002602 return -1;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002603}
2604
2605bool WebRtcVoiceMediaChannel::GetRedSendCodec(const AudioCodec& red_codec,
2606 const std::vector<AudioCodec>& all_codecs, webrtc::CodecInst* send_codec) {
2607 // Get the RED encodings from the parameter with no name. This may
2608 // change based on what is discussed on the Jingle list.
2609 // The encoding parameter is of the form "a/b"; we only support where
2610 // a == b. Verify this and parse out the value into red_pt.
2611 // If the parameter value is absent (as it will be until we wire up the
2612 // signaling of this message), use the second codec specified (i.e. the
2613 // one after "red") as the encoding parameter.
2614 int red_pt = -1;
2615 std::string red_params;
2616 CodecParameterMap::const_iterator it = red_codec.params.find("");
2617 if (it != red_codec.params.end()) {
2618 red_params = it->second;
2619 std::vector<std::string> red_pts;
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00002620 if (rtc::split(red_params, '/', &red_pts) != 2 ||
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002621 red_pts[0] != red_pts[1] ||
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00002622 !rtc::FromString(red_pts[0], &red_pt)) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002623 LOG(LS_WARNING) << "RED params " << red_params << " not supported.";
2624 return false;
2625 }
2626 } else if (red_codec.params.empty()) {
2627 LOG(LS_WARNING) << "RED params not present, using defaults";
2628 if (all_codecs.size() > 1) {
2629 red_pt = all_codecs[1].id;
2630 }
2631 }
2632
2633 // Try to find red_pt in |codecs|.
Fredrik Solenbergaf9fb212015-08-26 10:45:53 +02002634 for (const AudioCodec& codec : all_codecs) {
2635 if (codec.id == red_pt) {
2636 // If we find the right codec, that will be the codec we pass to
2637 // SetSendCodec, with the desired payload type.
2638 if (engine()->FindWebRtcCodec(codec, send_codec)) {
2639 return true;
2640 } else {
2641 break;
2642 }
2643 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002644 }
Fredrik Solenbergaf9fb212015-08-26 10:45:53 +02002645 LOG(LS_WARNING) << "RED params " << red_params << " are invalid.";
2646 return false;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002647}
2648
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002649bool WebRtcVoiceMediaChannel::SetPlayout(int channel, bool playout) {
2650 if (playout) {
2651 LOG(LS_INFO) << "Starting playout for channel #" << channel;
2652 if (engine()->voe()->base()->StartPlayout(channel) == -1) {
2653 LOG_RTCERR1(StartPlayout, channel);
2654 return false;
2655 }
2656 } else {
2657 LOG(LS_INFO) << "Stopping playout for channel #" << channel;
2658 engine()->voe()->base()->StopPlayout(channel);
2659 }
2660 return true;
2661}
2662
Fredrik Solenbergaf9fb212015-08-26 10:45:53 +02002663bool WebRtcVoiceMediaChannel::SetRecvCodecsInternal(
2664 const std::vector<AudioCodec>& new_codecs) {
solenberg566ef242015-11-06 15:34:49 -08002665 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
Fredrik Solenbergaf9fb212015-08-26 10:45:53 +02002666 for (const AudioCodec& codec : new_codecs) {
2667 webrtc::CodecInst voe_codec;
2668 if (engine()->FindWebRtcCodec(codec, &voe_codec)) {
2669 LOG(LS_INFO) << ToString(codec);
2670 voe_codec.pltype = codec.id;
solenberg7add0582015-11-20 09:59:34 -08002671 for (const auto& ch : recv_streams_) {
Fredrik Solenbergaf9fb212015-08-26 10:45:53 +02002672 if (engine()->voe()->codec()->SetRecPayloadType(
2673 ch.second->channel(), voe_codec) == -1) {
2674 LOG_RTCERR2(SetRecPayloadType, ch.second->channel(),
2675 ToString(voe_codec));
2676 return false;
2677 }
2678 }
2679 } else {
2680 LOG(LS_WARNING) << "Unknown codec " << ToString(codec);
2681 return false;
2682 }
2683 }
2684 return true;
2685}
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002686} // namespace cricket
2687
2688#endif // HAVE_WEBRTC_VOICE