blob: c81ecdf78e3bc61e1ae1ab58cab4fd0bd32188b2 [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"
henrike@webrtc.org28e20752013-07-10 00:45:36 +000058
henrike@webrtc.org28e20752013-07-10 00:45:36 +000059namespace cricket {
solenbergd97ec302015-10-07 01:40:33 -070060namespace {
henrike@webrtc.org28e20752013-07-10 00:45:36 +000061
solenbergd97ec302015-10-07 01:40:33 -070062const int kMaxNumPacketSize = 6;
henrike@webrtc.org28e20752013-07-10 00:45:36 +000063struct CodecPref {
64 const char* name;
65 int clockrate;
66 int channels;
67 int payload_type;
68 bool is_multi_rate;
Brave Yao5225dd82015-03-26 07:39:19 +080069 int packet_sizes_ms[kMaxNumPacketSize];
henrike@webrtc.org28e20752013-07-10 00:45:36 +000070};
Brave Yao5225dd82015-03-26 07:39:19 +080071// Note: keep the supported packet sizes in ascending order.
solenbergd97ec302015-10-07 01:40:33 -070072const CodecPref kCodecPrefs[] = {
Brave Yao5225dd82015-03-26 07:39:19 +080073 { kOpusCodecName, 48000, 2, 111, true, { 10, 20, 40, 60 } },
74 { kIsacCodecName, 16000, 1, 103, true, { 30, 60 } },
75 { kIsacCodecName, 32000, 1, 104, true, { 30 } },
henrik.lundin@webrtc.org8038d422014-11-11 08:38:24 +000076 // G722 should be advertised as 8000 Hz because of the RFC "bug".
Brave Yao5225dd82015-03-26 07:39:19 +080077 { kG722CodecName, 8000, 1, 9, false, { 10, 20, 30, 40, 50, 60 } },
78 { kIlbcCodecName, 8000, 1, 102, false, { 20, 30, 40, 60 } },
79 { kPcmuCodecName, 8000, 1, 0, false, { 10, 20, 30, 40, 50, 60 } },
80 { kPcmaCodecName, 8000, 1, 8, false, { 10, 20, 30, 40, 50, 60 } },
Brave Yao5225dd82015-03-26 07:39:19 +080081 { kCnCodecName, 32000, 1, 106, false, { } },
82 { kCnCodecName, 16000, 1, 105, false, { } },
83 { kCnCodecName, 8000, 1, 13, false, { } },
84 { kRedCodecName, 8000, 1, 127, false, { } },
85 { kDtmfCodecName, 8000, 1, 126, false, { } },
henrike@webrtc.org28e20752013-07-10 00:45:36 +000086};
87
88// For Linux/Mac, using the default device is done by specifying index 0 for
89// VoE 4.0 and not -1 (which was the case for VoE 3.5).
90//
91// On Windows Vista and newer, Microsoft introduced the concept of "Default
92// Communications Device". This means that there are two types of default
93// devices (old Wave Audio style default and Default Communications Device).
94//
95// On Windows systems which only support Wave Audio style default, uses either
96// -1 or 0 to select the default device.
97//
98// On Windows systems which support both "Default Communication Device" and
99// old Wave Audio style default, use -1 for Default Communications Device and
100// -2 for Wave Audio style default, which is what we want to use for clips.
101// It's not clear yet whether the -2 index is handled properly on other OSes.
102
103#ifdef WIN32
solenbergd97ec302015-10-07 01:40:33 -0700104const int kDefaultAudioDeviceId = -1;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000105#else
solenbergd97ec302015-10-07 01:40:33 -0700106const int kDefaultAudioDeviceId = 0;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000107#endif
108
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000109// Parameter used for NACK.
110// This value is equivalent to 5 seconds of audio data at 20 ms per packet.
solenbergd97ec302015-10-07 01:40:33 -0700111const int kNackMaxPackets = 250;
minyue@webrtc.org2dc6f312014-10-31 05:33:10 +0000112
113// Codec parameters for Opus.
henrike@webrtc.org1e09a712013-07-26 19:17:59 +0000114// draft-spittka-payload-rtp-opus-03
minyue@webrtc.org2dc6f312014-10-31 05:33:10 +0000115
116// Recommended bitrates:
117// 8-12 kb/s for NB speech,
118// 16-20 kb/s for WB speech,
119// 28-40 kb/s for FB speech,
120// 48-64 kb/s for FB mono music, and
121// 64-128 kb/s for FB stereo music.
122// The current implementation applies the following values to mono signals,
123// and multiplies them by 2 for stereo.
solenbergd97ec302015-10-07 01:40:33 -0700124const int kOpusBitrateNb = 12000;
125const int kOpusBitrateWb = 20000;
126const int kOpusBitrateFb = 32000;
minyue@webrtc.org2dc6f312014-10-31 05:33:10 +0000127
henrike@webrtc.org1e09a712013-07-26 19:17:59 +0000128// Opus bitrate should be in the range between 6000 and 510000.
solenbergd97ec302015-10-07 01:40:33 -0700129const int kOpusMinBitrate = 6000;
130const int kOpusMaxBitrate = 510000;
buildbot@webrtc.org5d639b32014-09-10 07:57:12 +0000131
wu@webrtc.orgde305012013-10-31 15:40:38 +0000132// Default audio dscp value.
133// See http://tools.ietf.org/html/rfc2474 for details.
134// See also http://tools.ietf.org/html/draft-jennings-rtcweb-qos-00
solenbergd97ec302015-10-07 01:40:33 -0700135const rtc::DiffServCodePoint kAudioDscpValue = rtc::DSCP_EF;
henrike@webrtc.org1e09a712013-07-26 19:17:59 +0000136
sergeyu@chromium.orga59696b2013-09-13 23:48:58 +0000137// Ensure we open the file in a writeable path on ChromeOS and Android. This
138// workaround can be removed when it's possible to specify a filename for audio
139// option based AEC dumps.
henrike@webrtc.org1e09a712013-07-26 19:17:59 +0000140//
141// TODO(grunell): Use a string in the options instead of hardcoding it here
142// and let the embedder choose the filename (crbug.com/264223).
143//
sergeyu@chromium.orga59696b2013-09-13 23:48:58 +0000144// NOTE(ajm): Don't use hardcoded paths on platforms not explicitly specified
145// below.
146#if defined(CHROMEOS)
solenbergd97ec302015-10-07 01:40:33 -0700147const char kAecDumpByAudioOptionFilename[] = "/tmp/audio.aecdump";
sergeyu@chromium.orga59696b2013-09-13 23:48:58 +0000148#elif defined(ANDROID)
solenbergd97ec302015-10-07 01:40:33 -0700149const char kAecDumpByAudioOptionFilename[] = "/sdcard/audio.aecdump";
henrike@webrtc.org1e09a712013-07-26 19:17:59 +0000150#else
solenbergd97ec302015-10-07 01:40:33 -0700151const char kAecDumpByAudioOptionFilename[] = "audio.aecdump";
henrike@webrtc.org1e09a712013-07-26 19:17:59 +0000152#endif
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000153
solenberg0b675462015-10-09 01:37:09 -0700154bool ValidateStreamParams(const StreamParams& sp) {
155 if (sp.ssrcs.empty()) {
156 LOG(LS_ERROR) << "No SSRCs in stream parameters: " << sp.ToString();
157 return false;
158 }
159 if (sp.ssrcs.size() > 1) {
160 LOG(LS_ERROR) << "Multiple SSRCs in stream parameters: " << sp.ToString();
161 return false;
162 }
163 return true;
164}
165
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000166// Dumps an AudioCodec in RFC 2327-ish format.
solenbergd97ec302015-10-07 01:40:33 -0700167std::string ToString(const AudioCodec& codec) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000168 std::stringstream ss;
169 ss << codec.name << "/" << codec.clockrate << "/" << codec.channels
170 << " (" << codec.id << ")";
171 return ss.str();
172}
Minyue Li7100dcd2015-03-27 05:05:59 +0100173
solenbergd97ec302015-10-07 01:40:33 -0700174std::string ToString(const webrtc::CodecInst& codec) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000175 std::stringstream ss;
176 ss << codec.plname << "/" << codec.plfreq << "/" << codec.channels
177 << " (" << codec.pltype << ")";
178 return ss.str();
179}
180
solenbergd97ec302015-10-07 01:40:33 -0700181void LogMultiline(rtc::LoggingSeverity sev, char* text) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000182 const char* delim = "\r\n";
183 for (char* tok = strtok(text, delim); tok; tok = strtok(NULL, delim)) {
184 LOG_V(sev) << tok;
185 }
186}
187
188// Severity is an integer because it comes is assumed to be from command line.
solenbergd97ec302015-10-07 01:40:33 -0700189int SeverityToFilter(int severity) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000190 int filter = webrtc::kTraceNone;
191 switch (severity) {
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000192 case rtc::LS_VERBOSE:
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000193 filter |= webrtc::kTraceAll;
Henrik Kjellander7c027b62015-04-22 13:21:30 +0200194 FALLTHROUGH();
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000195 case rtc::LS_INFO:
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000196 filter |= (webrtc::kTraceStateInfo | webrtc::kTraceInfo);
Henrik Kjellander7c027b62015-04-22 13:21:30 +0200197 FALLTHROUGH();
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000198 case rtc::LS_WARNING:
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000199 filter |= (webrtc::kTraceTerseInfo | webrtc::kTraceWarning);
Henrik Kjellander7c027b62015-04-22 13:21:30 +0200200 FALLTHROUGH();
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000201 case rtc::LS_ERROR:
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000202 filter |= (webrtc::kTraceError | webrtc::kTraceCritical);
203 }
204 return filter;
205}
206
solenbergd97ec302015-10-07 01:40:33 -0700207bool IsCodec(const AudioCodec& codec, const char* ref_name) {
Minyue Li7100dcd2015-03-27 05:05:59 +0100208 return (_stricmp(codec.name.c_str(), ref_name) == 0);
209}
210
solenbergd97ec302015-10-07 01:40:33 -0700211bool IsCodec(const webrtc::CodecInst& codec, const char* ref_name) {
Minyue Li7100dcd2015-03-27 05:05:59 +0100212 return (_stricmp(codec.plname, ref_name) == 0);
213}
214
solenbergd97ec302015-10-07 01:40:33 -0700215bool IsCodecMultiRate(const webrtc::CodecInst& codec) {
tfarina5237aaf2015-11-10 23:44:30 -0800216 for (size_t i = 0; i < arraysize(kCodecPrefs); ++i) {
Minyue Li7100dcd2015-03-27 05:05:59 +0100217 if (IsCodec(codec, kCodecPrefs[i].name) &&
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000218 kCodecPrefs[i].clockrate == codec.plfreq) {
219 return kCodecPrefs[i].is_multi_rate;
220 }
221 }
222 return false;
223}
224
solenbergd97ec302015-10-07 01:40:33 -0700225bool FindCodec(const std::vector<AudioCodec>& codecs,
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000226 const AudioCodec& codec,
227 AudioCodec* found_codec) {
Fredrik Solenbergaf9fb212015-08-26 10:45:53 +0200228 for (const AudioCodec& c : codecs) {
229 if (c.Matches(codec)) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000230 if (found_codec != NULL) {
Fredrik Solenbergaf9fb212015-08-26 10:45:53 +0200231 *found_codec = c;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000232 }
233 return true;
234 }
235 }
236 return false;
237}
wu@webrtc.org1d1ffc92013-10-16 18:12:02 +0000238
solenberg0b675462015-10-09 01:37:09 -0700239bool VerifyUniquePayloadTypes(const std::vector<AudioCodec>& codecs) {
240 if (codecs.empty()) {
241 return true;
242 }
243 std::vector<int> payload_types;
244 for (const AudioCodec& codec : codecs) {
245 payload_types.push_back(codec.id);
246 }
247 std::sort(payload_types.begin(), payload_types.end());
248 auto it = std::unique(payload_types.begin(), payload_types.end());
249 return it == payload_types.end();
250}
251
solenbergd97ec302015-10-07 01:40:33 -0700252bool IsNackEnabled(const AudioCodec& codec) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000253 return codec.HasFeedbackParam(FeedbackParam(kRtcpFbParamNack,
254 kParamValueEmpty));
255}
256
solenbergd97ec302015-10-07 01:40:33 -0700257int SelectPacketSize(const CodecPref& codec_pref, int ptime_ms) {
Brave Yao5225dd82015-03-26 07:39:19 +0800258 int selected_packet_size_ms = codec_pref.packet_sizes_ms[0];
259 for (int packet_size_ms : codec_pref.packet_sizes_ms) {
260 if (packet_size_ms && packet_size_ms <= ptime_ms) {
261 selected_packet_size_ms = packet_size_ms;
262 }
263 }
264 return selected_packet_size_ms;
265}
266
267// If the AudioCodec param kCodecParamPTime is set, then we will set it to codec
268// pacsize if it's valid, or we will pick the next smallest value we support.
269// TODO(Brave): Query supported packet sizes from ACM when the API is ready.
solenbergd97ec302015-10-07 01:40:33 -0700270bool SetPTimeAsPacketSize(webrtc::CodecInst* codec, int ptime_ms) {
Brave Yao5225dd82015-03-26 07:39:19 +0800271 for (const CodecPref& codec_pref : kCodecPrefs) {
Minyue Li7100dcd2015-03-27 05:05:59 +0100272 if ((IsCodec(*codec, codec_pref.name) &&
Brave Yao5225dd82015-03-26 07:39:19 +0800273 codec_pref.clockrate == codec->plfreq) ||
Minyue Li7100dcd2015-03-27 05:05:59 +0100274 IsCodec(*codec, kG722CodecName)) {
Brave Yao5225dd82015-03-26 07:39:19 +0800275 int packet_size_ms = SelectPacketSize(codec_pref, ptime_ms);
276 if (packet_size_ms) {
277 // Convert unit from milli-seconds to samples.
278 codec->pacsize = (codec->plfreq / 1000) * packet_size_ms;
279 return true;
280 }
281 }
282 }
283 return false;
284}
285
Minyue Li7100dcd2015-03-27 05:05:59 +0100286// Return true if codec.params[feature] == "1", false otherwise.
solenbergd97ec302015-10-07 01:40:33 -0700287bool IsCodecFeatureEnabled(const AudioCodec& codec,
Minyue Li7100dcd2015-03-27 05:05:59 +0100288 const char* feature) {
289 int value;
290 return codec.GetParam(feature, &value) && value == 1;
291}
292
293// Use params[kCodecParamMaxAverageBitrate] if it is defined, use codec.bitrate
294// otherwise. If the value (either from params or codec.bitrate) <=0, use the
295// default configuration. If the value is beyond feasible bit rate of Opus,
296// clamp it. Returns the Opus bit rate for operation.
solenbergd97ec302015-10-07 01:40:33 -0700297int GetOpusBitrate(const AudioCodec& codec, int max_playback_rate) {
Minyue Li7100dcd2015-03-27 05:05:59 +0100298 int bitrate = 0;
299 bool use_param = true;
300 if (!codec.GetParam(kCodecParamMaxAverageBitrate, &bitrate)) {
301 bitrate = codec.bitrate;
302 use_param = false;
303 }
304 if (bitrate <= 0) {
305 if (max_playback_rate <= 8000) {
306 bitrate = kOpusBitrateNb;
307 } else if (max_playback_rate <= 16000) {
308 bitrate = kOpusBitrateWb;
309 } else {
310 bitrate = kOpusBitrateFb;
311 }
312
313 if (IsCodecFeatureEnabled(codec, kCodecParamStereo)) {
314 bitrate *= 2;
315 }
316 } else if (bitrate < kOpusMinBitrate || bitrate > kOpusMaxBitrate) {
317 bitrate = (bitrate < kOpusMinBitrate) ? kOpusMinBitrate : kOpusMaxBitrate;
318 std::string rate_source =
319 use_param ? "Codec parameter \"maxaveragebitrate\"" :
320 "Supplied Opus bitrate";
321 LOG(LS_WARNING) << rate_source
322 << " is invalid and is replaced by: "
323 << bitrate;
324 }
325 return bitrate;
326}
327
328// Returns kOpusDefaultPlaybackRate if params[kCodecParamMaxPlaybackRate] is not
329// defined. Returns the value of params[kCodecParamMaxPlaybackRate] otherwise.
solenbergd97ec302015-10-07 01:40:33 -0700330int GetOpusMaxPlaybackRate(const AudioCodec& codec) {
Minyue Li7100dcd2015-03-27 05:05:59 +0100331 int value;
332 if (codec.GetParam(kCodecParamMaxPlaybackRate, &value)) {
333 return value;
334 }
335 return kOpusDefaultMaxPlaybackRate;
336}
337
solenbergd97ec302015-10-07 01:40:33 -0700338void GetOpusConfig(const AudioCodec& codec, webrtc::CodecInst* voe_codec,
Minyue Li7100dcd2015-03-27 05:05:59 +0100339 bool* enable_codec_fec, int* max_playback_rate,
340 bool* enable_codec_dtx) {
341 *enable_codec_fec = IsCodecFeatureEnabled(codec, kCodecParamUseInbandFec);
342 *enable_codec_dtx = IsCodecFeatureEnabled(codec, kCodecParamUseDtx);
343 *max_playback_rate = GetOpusMaxPlaybackRate(codec);
344
345 // If OPUS, change what we send according to the "stereo" codec
346 // parameter, and not the "channels" parameter. We set
347 // voe_codec.channels to 2 if "stereo=1" and 1 otherwise. If
348 // the bitrate is not specified, i.e. is <= zero, we set it to the
349 // appropriate default value for mono or stereo Opus.
350
351 voe_codec->channels = IsCodecFeatureEnabled(codec, kCodecParamStereo) ? 2 : 1;
352 voe_codec->rate = GetOpusBitrate(codec, *max_playback_rate);
353}
354
355// Changes RTP timestamp rate of G722. This is due to the "bug" in the RFC
356// which says that G722 should be advertised as 8 kHz although it is a 16 kHz
357// codec.
solenbergd97ec302015-10-07 01:40:33 -0700358void MaybeFixupG722(webrtc::CodecInst* voe_codec, int new_plfreq) {
Minyue Li7100dcd2015-03-27 05:05:59 +0100359 if (IsCodec(*voe_codec, kG722CodecName)) {
360 // If the ASSERT triggers, the codec definition in WebRTC VoiceEngine
361 // has changed, and this special case is no longer needed.
henrikg91d6ede2015-09-17 00:24:34 -0700362 RTC_DCHECK(voe_codec->plfreq != new_plfreq);
Minyue Li7100dcd2015-03-27 05:05:59 +0100363 voe_codec->plfreq = new_plfreq;
364 }
365}
366
wu@webrtc.org1d1ffc92013-10-16 18:12:02 +0000367// Gets the default set of options applied to the engine. Historically, these
368// were supplied as a combination of flags from the channel manager (ec, agc,
369// ns, and highpass) and the rest hardcoded in InitInternal.
solenbergd97ec302015-10-07 01:40:33 -0700370AudioOptions GetDefaultEngineOptions() {
wu@webrtc.org1d1ffc92013-10-16 18:12:02 +0000371 AudioOptions options;
Karl Wibergbe579832015-11-10 22:34:18 +0100372 options.echo_cancellation = rtc::Optional<bool>(true);
373 options.auto_gain_control = rtc::Optional<bool>(true);
374 options.noise_suppression = rtc::Optional<bool>(true);
375 options.highpass_filter = rtc::Optional<bool>(true);
376 options.stereo_swapping = rtc::Optional<bool>(false);
377 options.audio_jitter_buffer_max_packets = rtc::Optional<int>(50);
378 options.audio_jitter_buffer_fast_accelerate = rtc::Optional<bool>(false);
379 options.typing_detection = rtc::Optional<bool>(true);
380 options.adjust_agc_delta = rtc::Optional<int>(0);
381 options.experimental_agc = rtc::Optional<bool>(false);
382 options.extended_filter_aec = rtc::Optional<bool>(false);
383 options.delay_agnostic_aec = rtc::Optional<bool>(false);
384 options.experimental_ns = rtc::Optional<bool>(false);
385 options.aec_dump = rtc::Optional<bool>(false);
wu@webrtc.org1d1ffc92013-10-16 18:12:02 +0000386 return options;
387}
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000388
solenbergd97ec302015-10-07 01:40:33 -0700389std::string GetEnableString(bool enable) {
Minyue Li7100dcd2015-03-27 05:05:59 +0100390 return enable ? "enable" : "disable";
Brave Yao5225dd82015-03-26 07:39:19 +0800391}
solenberg566ef242015-11-06 15:34:49 -0800392
393webrtc::AudioState::Config MakeAudioStateConfig(VoEWrapper* voe_wrapper) {
394 webrtc::AudioState::Config config;
395 config.voice_engine = voe_wrapper->engine();
396 return config;
397}
398
solenberg3a941542015-11-16 07:34:50 -0800399std::vector<webrtc::RtpExtension> FindAudioRtpHeaderExtensions(
400 const std::vector<RtpHeaderExtension>& extensions) {
401 std::vector<webrtc::RtpExtension> result;
402 for (const auto& extension : extensions) {
403 if (extension.uri == kRtpAbsoluteSenderTimeHeaderExtension ||
404 extension.uri == kRtpAudioLevelHeaderExtension) {
405 result.push_back({extension.uri, extension.id});
406 } else {
407 LOG(LS_WARNING) << "Unsupported RTP extension: " << extension.ToString();
408 }
409 }
410 return result;
411}
solenbergd97ec302015-10-07 01:40:33 -0700412} // namespace {
Brave Yao5225dd82015-03-26 07:39:19 +0800413
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000414WebRtcVoiceEngine::WebRtcVoiceEngine()
415 : voe_wrapper_(new VoEWrapper()),
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000416 tracing_(new VoETraceWrapper()),
solenberg566ef242015-11-06 15:34:49 -0800417 audio_state_(webrtc::AudioState::Create(MakeAudioStateConfig(voe()))),
418 log_filter_(SeverityToFilter(kDefaultLogSeverity)) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000419 Construct();
420}
421
422WebRtcVoiceEngine::WebRtcVoiceEngine(VoEWrapper* voe_wrapper,
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000423 VoETraceWrapper* tracing)
424 : voe_wrapper_(voe_wrapper),
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000425 tracing_(tracing),
solenberg566ef242015-11-06 15:34:49 -0800426 log_filter_(SeverityToFilter(kDefaultLogSeverity)) {
buildbot@webrtc.org13d67762014-05-02 17:33:29 +0000427 Construct();
428}
429
430void WebRtcVoiceEngine::Construct() {
solenberg566ef242015-11-06 15:34:49 -0800431 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
432 signal_thread_checker_.DetachFromThread();
433 std::memset(&default_agc_config_, 0, sizeof(default_agc_config_));
buildbot@webrtc.org13d67762014-05-02 17:33:29 +0000434 SetTraceFilter(log_filter_);
buildbot@webrtc.org13d67762014-05-02 17:33:29 +0000435 LOG(LS_VERBOSE) << "WebRtcVoiceEngine::WebRtcVoiceEngine";
436 SetTraceOptions("");
437 if (tracing_->SetTraceCallback(this) == -1) {
438 LOG_RTCERR0(SetTraceCallback);
439 }
buildbot@webrtc.org13d67762014-05-02 17:33:29 +0000440
441 // Load our audio codec list.
442 ConstructCodecs();
443
444 // Load our RTP Header extensions.
445 rtp_header_extensions_.push_back(
446 RtpHeaderExtension(kRtpAudioLevelHeaderExtension,
447 kRtpAudioLevelHeaderExtensionDefaultId));
448 rtp_header_extensions_.push_back(
449 RtpHeaderExtension(kRtpAbsoluteSenderTimeHeaderExtension,
450 kRtpAbsoluteSenderTimeHeaderExtensionDefaultId));
stefanc1aeaf02015-10-15 07:26:07 -0700451 if (webrtc::field_trial::FindFullName("WebRTC-SendSideBwe") == "Enabled") {
452 rtp_header_extensions_.push_back(RtpHeaderExtension(
453 kRtpTransportSequenceNumberHeaderExtension,
454 kRtpTransportSequenceNumberHeaderExtensionDefaultId));
455 }
buildbot@webrtc.org13d67762014-05-02 17:33:29 +0000456 options_ = GetDefaultEngineOptions();
457}
458
buildbot@webrtc.org13d67762014-05-02 17:33:29 +0000459void WebRtcVoiceEngine::ConstructCodecs() {
solenberg566ef242015-11-06 15:34:49 -0800460 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
buildbot@webrtc.org13d67762014-05-02 17:33:29 +0000461 LOG(LS_INFO) << "WebRtc VoiceEngine codecs:";
462 int ncodecs = voe_wrapper_->codec()->NumOfCodecs();
463 for (int i = 0; i < ncodecs; ++i) {
464 webrtc::CodecInst voe_codec;
henrik.lundin@webrtc.org8038d422014-11-11 08:38:24 +0000465 if (GetVoeCodec(i, &voe_codec)) {
buildbot@webrtc.org13d67762014-05-02 17:33:29 +0000466 // Skip uncompressed formats.
Minyue Li7100dcd2015-03-27 05:05:59 +0100467 if (IsCodec(voe_codec, kL16CodecName)) {
buildbot@webrtc.org13d67762014-05-02 17:33:29 +0000468 continue;
469 }
470
471 const CodecPref* pref = NULL;
tfarina5237aaf2015-11-10 23:44:30 -0800472 for (size_t j = 0; j < arraysize(kCodecPrefs); ++j) {
Minyue Li7100dcd2015-03-27 05:05:59 +0100473 if (IsCodec(voe_codec, kCodecPrefs[j].name) &&
buildbot@webrtc.org13d67762014-05-02 17:33:29 +0000474 kCodecPrefs[j].clockrate == voe_codec.plfreq &&
475 kCodecPrefs[j].channels == voe_codec.channels) {
476 pref = &kCodecPrefs[j];
477 break;
478 }
479 }
480
481 if (pref) {
482 // Use the payload type that we've configured in our pref table;
483 // use the offset in our pref table to determine the sort order.
tfarina5237aaf2015-11-10 23:44:30 -0800484 AudioCodec codec(
485 pref->payload_type, voe_codec.plname, voe_codec.plfreq,
486 voe_codec.rate, voe_codec.channels,
487 static_cast<int>(arraysize(kCodecPrefs)) - (pref - kCodecPrefs));
buildbot@webrtc.org13d67762014-05-02 17:33:29 +0000488 LOG(LS_INFO) << ToString(codec);
Minyue Li7100dcd2015-03-27 05:05:59 +0100489 if (IsCodec(codec, kIsacCodecName)) {
minyue@webrtc.org26236952014-10-29 02:27:08 +0000490 // Indicate auto-bitrate in signaling.
buildbot@webrtc.org13d67762014-05-02 17:33:29 +0000491 codec.bitrate = 0;
492 }
Minyue Li7100dcd2015-03-27 05:05:59 +0100493 if (IsCodec(codec, kOpusCodecName)) {
buildbot@webrtc.org13d67762014-05-02 17:33:29 +0000494 // Only add fmtp parameters that differ from the spec.
495 if (kPreferredMinPTime != kOpusDefaultMinPTime) {
496 codec.params[kCodecParamMinPTime] =
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000497 rtc::ToString(kPreferredMinPTime);
buildbot@webrtc.org13d67762014-05-02 17:33:29 +0000498 }
499 if (kPreferredMaxPTime != kOpusDefaultMaxPTime) {
500 codec.params[kCodecParamMaxPTime] =
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000501 rtc::ToString(kPreferredMaxPTime);
buildbot@webrtc.org13d67762014-05-02 17:33:29 +0000502 }
pkasting@chromium.orgd3245462015-02-23 21:28:22 +0000503 codec.SetParam(kCodecParamUseInbandFec, 1);
minyue@webrtc.org4ef22d12014-11-17 09:26:39 +0000504
505 // TODO(hellner): Add ptime, sprop-stereo, and stereo
buildbot@webrtc.org13d67762014-05-02 17:33:29 +0000506 // when they can be set to values other than the default.
507 }
508 codecs_.push_back(codec);
509 } else {
510 LOG(LS_WARNING) << "Unexpected codec: " << ToString(voe_codec);
511 }
512 }
513 }
514 // Make sure they are in local preference order.
515 std::sort(codecs_.begin(), codecs_.end(), &AudioCodec::Preferable);
516}
517
henrik.lundin@webrtc.org8038d422014-11-11 08:38:24 +0000518bool WebRtcVoiceEngine::GetVoeCodec(int index, webrtc::CodecInst* codec) {
solenberg566ef242015-11-06 15:34:49 -0800519 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
henrik.lundin@webrtc.org8038d422014-11-11 08:38:24 +0000520 if (voe_wrapper_->codec()->GetCodec(index, *codec) == -1) {
521 return false;
henrik.lundin@webrtc.orgf85dbce2014-11-07 12:25:00 +0000522 }
henrik.lundin@webrtc.org8038d422014-11-11 08:38:24 +0000523 // Change the sample rate of G722 to 8000 to match SDP.
524 MaybeFixupG722(codec, 8000);
525 return true;
henrik.lundin@webrtc.orgf85dbce2014-11-07 12:25:00 +0000526}
527
buildbot@webrtc.org13d67762014-05-02 17:33:29 +0000528WebRtcVoiceEngine::~WebRtcVoiceEngine() {
solenberg566ef242015-11-06 15:34:49 -0800529 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
buildbot@webrtc.org13d67762014-05-02 17:33:29 +0000530 LOG(LS_VERBOSE) << "WebRtcVoiceEngine::~WebRtcVoiceEngine";
buildbot@webrtc.org13d67762014-05-02 17:33:29 +0000531 if (adm_) {
532 voe_wrapper_.reset();
533 adm_->Release();
534 adm_ = NULL;
535 }
buildbot@webrtc.org13d67762014-05-02 17:33:29 +0000536
buildbot@webrtc.org13d67762014-05-02 17:33:29 +0000537 tracing_->SetTraceCallback(NULL);
538}
539
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000540bool WebRtcVoiceEngine::Init(rtc::Thread* worker_thread) {
solenberg566ef242015-11-06 15:34:49 -0800541 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
henrikg91d6ede2015-09-17 00:24:34 -0700542 RTC_DCHECK(worker_thread == rtc::Thread::Current());
buildbot@webrtc.org13d67762014-05-02 17:33:29 +0000543 LOG(LS_INFO) << "WebRtcVoiceEngine::Init";
544 bool res = InitInternal();
545 if (res) {
546 LOG(LS_INFO) << "WebRtcVoiceEngine::Init Done!";
547 } else {
548 LOG(LS_ERROR) << "WebRtcVoiceEngine::Init failed";
549 Terminate();
550 }
551 return res;
552}
553
554bool WebRtcVoiceEngine::InitInternal() {
solenberg566ef242015-11-06 15:34:49 -0800555 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
buildbot@webrtc.org13d67762014-05-02 17:33:29 +0000556 // Temporarily turn logging level up for the Init call
557 int old_filter = log_filter_;
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000558 int extended_filter = log_filter_ | SeverityToFilter(rtc::LS_INFO);
buildbot@webrtc.org13d67762014-05-02 17:33:29 +0000559 SetTraceFilter(extended_filter);
560 SetTraceOptions("");
561
562 // Init WebRtc VoiceEngine.
563 if (voe_wrapper_->base()->Init(adm_) == -1) {
564 LOG_RTCERR0_EX(Init, voe_wrapper_->error());
565 SetTraceFilter(old_filter);
566 return false;
567 }
568
569 SetTraceFilter(old_filter);
570 SetTraceOptions(log_options_);
571
572 // Log the VoiceEngine version info
573 char buffer[1024] = "";
574 voe_wrapper_->base()->GetVersion(buffer);
575 LOG(LS_INFO) << "WebRtc VoiceEngine Version:";
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000576 LogMultiline(rtc::LS_INFO, buffer);
buildbot@webrtc.org13d67762014-05-02 17:33:29 +0000577
578 // Save the default AGC configuration settings. This must happen before
579 // calling SetOptions or the default will be overwritten.
580 if (voe_wrapper_->processing()->GetAgcConfig(default_agc_config_) == -1) {
581 LOG_RTCERR0(GetAgcConfig);
582 return false;
583 }
584
585 // Set defaults for options, so that ApplyOptions applies them explicitly
586 // when we clear option (channel) overrides. External clients can still
587 // modify the defaults via SetOptions (on the media engine).
588 if (!SetOptions(GetDefaultEngineOptions())) {
589 return false;
590 }
591
592 // Print our codec list again for the call diagnostic log
593 LOG(LS_INFO) << "WebRtc VoiceEngine codecs:";
Fredrik Solenbergaf9fb212015-08-26 10:45:53 +0200594 for (const AudioCodec& codec : codecs_) {
595 LOG(LS_INFO) << ToString(codec);
buildbot@webrtc.org13d67762014-05-02 17:33:29 +0000596 }
597
598 // Disable the DTMF playout when a tone is sent.
599 // PlayDtmfTone will be used if local playout is needed.
600 if (voe_wrapper_->dtmf()->SetDtmfFeedbackStatus(false) == -1) {
601 LOG_RTCERR1(SetDtmfFeedbackStatus, false);
602 }
603
604 initialized_ = true;
605 return true;
606}
607
buildbot@webrtc.org13d67762014-05-02 17:33:29 +0000608void WebRtcVoiceEngine::Terminate() {
solenberg566ef242015-11-06 15:34:49 -0800609 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
buildbot@webrtc.org13d67762014-05-02 17:33:29 +0000610 LOG(LS_INFO) << "WebRtcVoiceEngine::Terminate";
611 initialized_ = false;
612
613 StopAecDump();
614
buildbot@webrtc.org13d67762014-05-02 17:33:29 +0000615 voe_wrapper_->base()->Terminate();
buildbot@webrtc.org13d67762014-05-02 17:33:29 +0000616}
617
solenberg566ef242015-11-06 15:34:49 -0800618rtc::scoped_refptr<webrtc::AudioState>
619 WebRtcVoiceEngine::GetAudioState() const {
620 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
621 return audio_state_;
622}
623
Fredrik Solenberg709ed672015-09-15 12:26:33 +0200624VoiceMediaChannel* WebRtcVoiceEngine::CreateChannel(webrtc::Call* call,
Jelena Marusicc28a8962015-05-29 15:05:44 +0200625 const AudioOptions& options) {
solenberg566ef242015-11-06 15:34:49 -0800626 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
solenberg0a617e22015-10-20 15:49:38 -0700627 return new WebRtcVoiceMediaChannel(this, options, call);
buildbot@webrtc.org13d67762014-05-02 17:33:29 +0000628}
629
buildbot@webrtc.org13d67762014-05-02 17:33:29 +0000630bool WebRtcVoiceEngine::SetOptions(const AudioOptions& options) {
solenberg566ef242015-11-06 15:34:49 -0800631 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
buildbot@webrtc.org13d67762014-05-02 17:33:29 +0000632 if (!ApplyOptions(options)) {
633 return false;
634 }
635 options_ = options;
636 return true;
637}
638
buildbot@webrtc.org13d67762014-05-02 17:33:29 +0000639// AudioOptions defaults are set in InitInternal (for options with corresponding
640// MediaEngineInterface flags) and in SetOptions(int) for flagless options.
641bool WebRtcVoiceEngine::ApplyOptions(const AudioOptions& options_in) {
solenberg566ef242015-11-06 15:34:49 -0800642 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
henrikac14f5ff2015-09-23 14:08:33 +0200643 LOG(LS_INFO) << "ApplyOptions: " << options_in.ToString();
buildbot@webrtc.org13d67762014-05-02 17:33:29 +0000644 AudioOptions options = options_in; // The options are modified below.
645 // kEcConference is AEC with high suppression.
646 webrtc::EcModes ec_mode = webrtc::kEcConference;
647 webrtc::AecmModes aecm_mode = webrtc::kAecmSpeakerphone;
648 webrtc::AgcModes agc_mode = webrtc::kAgcAdaptiveAnalog;
649 webrtc::NsModes ns_mode = webrtc::kNsHighSuppression;
kwiberg102c6a62015-10-30 02:47:38 -0700650 if (options.aecm_generate_comfort_noise) {
buildbot@webrtc.org13d67762014-05-02 17:33:29 +0000651 LOG(LS_VERBOSE) << "Comfort noise explicitly set to "
kwiberg102c6a62015-10-30 02:47:38 -0700652 << *options.aecm_generate_comfort_noise
653 << " (default is false).";
buildbot@webrtc.org13d67762014-05-02 17:33:29 +0000654 }
655
656#if defined(IOS)
657 // On iOS, VPIO provides built-in EC and AGC.
Karl Wibergbe579832015-11-10 22:34:18 +0100658 options.echo_cancellation = rtc::Optional<bool>(false);
659 options.auto_gain_control = rtc::Optional<bool>(false);
henrika86d907c2015-09-07 16:09:50 +0200660 LOG(LS_INFO) << "Always disable AEC and AGC on iOS. Use built-in instead.";
buildbot@webrtc.org13d67762014-05-02 17:33:29 +0000661#elif defined(ANDROID)
662 ec_mode = webrtc::kEcAecm;
663#endif
664
665#if defined(IOS) || defined(ANDROID)
666 // Set the AGC mode for iOS as well despite disabling it above, to avoid
667 // unsupported configuration errors from webrtc.
668 agc_mode = webrtc::kAgcFixedDigital;
Karl Wibergbe579832015-11-10 22:34:18 +0100669 options.typing_detection = rtc::Optional<bool>(false);
670 options.experimental_agc = rtc::Optional<bool>(false);
671 options.extended_filter_aec = rtc::Optional<bool>(false);
672 options.experimental_ns = rtc::Optional<bool>(false);
buildbot@webrtc.org13d67762014-05-02 17:33:29 +0000673#endif
674
Bjorn Volckerbf395c12015-03-25 22:45:56 +0100675 // Delay Agnostic AEC automatically turns on EC if not set except on iOS
676 // where the feature is not supported.
677 bool use_delay_agnostic_aec = false;
678#if !defined(IOS)
kwiberg102c6a62015-10-30 02:47:38 -0700679 if (options.delay_agnostic_aec) {
680 use_delay_agnostic_aec = *options.delay_agnostic_aec;
Bjorn Volckerbf395c12015-03-25 22:45:56 +0100681 if (use_delay_agnostic_aec) {
Karl Wibergbe579832015-11-10 22:34:18 +0100682 options.echo_cancellation = rtc::Optional<bool>(true);
683 options.extended_filter_aec = rtc::Optional<bool>(true);
Bjorn Volckerbf395c12015-03-25 22:45:56 +0100684 ec_mode = webrtc::kEcConference;
685 }
686 }
687#endif
688
buildbot@webrtc.org13d67762014-05-02 17:33:29 +0000689 webrtc::VoEAudioProcessing* voep = voe_wrapper_->processing();
690
kwiberg102c6a62015-10-30 02:47:38 -0700691 if (options.echo_cancellation) {
henrika@webrtc.orga954c072014-12-09 16:22:09 +0000692 // Check if platform supports built-in EC. Currently only supported on
693 // Android and in combination with Java based audio layer.
694 // TODO(henrika): investigate possibility to support built-in EC also
695 // in combination with Open SL ES audio.
696 const bool built_in_aec = voe_wrapper_->hw()->BuiltInAECIsAvailable();
Bjorn Volcker73f72102015-06-03 14:50:15 +0200697 if (built_in_aec) {
Bjorn Volckerccfc9392015-05-07 07:43:17 +0200698 // Built-in EC exists on this device and use_delay_agnostic_aec is not
699 // overriding it. Enable/Disable it according to the echo_cancellation
700 // audio option.
Bjorn Volcker73f72102015-06-03 14:50:15 +0200701 const bool enable_built_in_aec =
kwiberg102c6a62015-10-30 02:47:38 -0700702 *options.echo_cancellation && !use_delay_agnostic_aec;
Bjorn Volcker73f72102015-06-03 14:50:15 +0200703 if (voe_wrapper_->hw()->EnableBuiltInAEC(enable_built_in_aec) == 0 &&
704 enable_built_in_aec) {
Bjorn Volckerbf395c12015-03-25 22:45:56 +0100705 // Disable internal software EC if built-in EC is enabled,
henrika@webrtc.orga954c072014-12-09 16:22:09 +0000706 // i.e., replace the software EC with the built-in EC.
Karl Wibergbe579832015-11-10 22:34:18 +0100707 options.echo_cancellation = rtc::Optional<bool>(false);
henrika@webrtc.orga954c072014-12-09 16:22:09 +0000708 LOG(LS_INFO) << "Disabling EC since built-in EC will be used instead";
709 }
710 }
kwiberg102c6a62015-10-30 02:47:38 -0700711 if (voep->SetEcStatus(*options.echo_cancellation, ec_mode) == -1) {
712 LOG_RTCERR2(SetEcStatus, *options.echo_cancellation, ec_mode);
buildbot@webrtc.org13d67762014-05-02 17:33:29 +0000713 return false;
714 } else {
kwiberg102c6a62015-10-30 02:47:38 -0700715 LOG(LS_INFO) << "Echo control set to " << *options.echo_cancellation
henrika86d907c2015-09-07 16:09:50 +0200716 << " with mode " << ec_mode;
buildbot@webrtc.org13d67762014-05-02 17:33:29 +0000717 }
718#if !defined(ANDROID)
719 // TODO(ajm): Remove the error return on Android from webrtc.
kwiberg102c6a62015-10-30 02:47:38 -0700720 if (voep->SetEcMetricsStatus(*options.echo_cancellation) == -1) {
721 LOG_RTCERR1(SetEcMetricsStatus, *options.echo_cancellation);
buildbot@webrtc.org13d67762014-05-02 17:33:29 +0000722 return false;
723 }
724#endif
725 if (ec_mode == webrtc::kEcAecm) {
kwiberg102c6a62015-10-30 02:47:38 -0700726 bool cn = options.aecm_generate_comfort_noise.value_or(false);
727 if (voep->SetAecmMode(aecm_mode, cn) != 0) {
728 LOG_RTCERR2(SetAecmMode, aecm_mode, cn);
buildbot@webrtc.org13d67762014-05-02 17:33:29 +0000729 return false;
730 }
731 }
732 }
733
kwiberg102c6a62015-10-30 02:47:38 -0700734 if (options.auto_gain_control) {
henrikac14f5ff2015-09-23 14:08:33 +0200735 const bool built_in_agc = voe_wrapper_->hw()->BuiltInAGCIsAvailable();
736 if (built_in_agc) {
kwiberg102c6a62015-10-30 02:47:38 -0700737 if (voe_wrapper_->hw()->EnableBuiltInAGC(*options.auto_gain_control) ==
738 0 &&
739 *options.auto_gain_control) {
henrikac14f5ff2015-09-23 14:08:33 +0200740 // Disable internal software AGC if built-in AGC is enabled,
741 // i.e., replace the software AGC with the built-in AGC.
Karl Wibergbe579832015-11-10 22:34:18 +0100742 options.auto_gain_control = rtc::Optional<bool>(false);
henrikac14f5ff2015-09-23 14:08:33 +0200743 LOG(LS_INFO) << "Disabling AGC since built-in AGC will be used instead";
744 }
745 }
kwiberg102c6a62015-10-30 02:47:38 -0700746 if (voep->SetAgcStatus(*options.auto_gain_control, agc_mode) == -1) {
747 LOG_RTCERR2(SetAgcStatus, *options.auto_gain_control, agc_mode);
buildbot@webrtc.org13d67762014-05-02 17:33:29 +0000748 return false;
749 } else {
kwiberg102c6a62015-10-30 02:47:38 -0700750 LOG(LS_INFO) << "Auto gain set to " << *options.auto_gain_control
751 << " with mode " << agc_mode;
buildbot@webrtc.org13d67762014-05-02 17:33:29 +0000752 }
753 }
754
kwiberg102c6a62015-10-30 02:47:38 -0700755 if (options.tx_agc_target_dbov || options.tx_agc_digital_compression_gain ||
756 options.tx_agc_limiter) {
buildbot@webrtc.org13d67762014-05-02 17:33:29 +0000757 // Override default_agc_config_. Generally, an unset option means "leave
758 // the VoE bits alone" in this function, so we want whatever is set to be
759 // stored as the new "default". If we didn't, then setting e.g.
760 // tx_agc_target_dbov would reset digital compression gain and limiter
761 // settings.
762 // Also, if we don't update default_agc_config_, then adjust_agc_delta
763 // would be an offset from the original values, and not whatever was set
764 // explicitly.
kwiberg102c6a62015-10-30 02:47:38 -0700765 default_agc_config_.targetLeveldBOv = options.tx_agc_target_dbov.value_or(
766 default_agc_config_.targetLeveldBOv);
buildbot@webrtc.org13d67762014-05-02 17:33:29 +0000767 default_agc_config_.digitalCompressionGaindB =
kwiberg102c6a62015-10-30 02:47:38 -0700768 options.tx_agc_digital_compression_gain.value_or(
buildbot@webrtc.org13d67762014-05-02 17:33:29 +0000769 default_agc_config_.digitalCompressionGaindB);
770 default_agc_config_.limiterEnable =
kwiberg102c6a62015-10-30 02:47:38 -0700771 options.tx_agc_limiter.value_or(default_agc_config_.limiterEnable);
buildbot@webrtc.org13d67762014-05-02 17:33:29 +0000772 if (voe_wrapper_->processing()->SetAgcConfig(default_agc_config_) == -1) {
773 LOG_RTCERR3(SetAgcConfig,
774 default_agc_config_.targetLeveldBOv,
775 default_agc_config_.digitalCompressionGaindB,
776 default_agc_config_.limiterEnable);
777 return false;
778 }
779 }
780
kwiberg102c6a62015-10-30 02:47:38 -0700781 if (options.noise_suppression) {
henrikac14f5ff2015-09-23 14:08:33 +0200782 const bool built_in_ns = voe_wrapper_->hw()->BuiltInNSIsAvailable();
783 if (built_in_ns) {
kwiberg102c6a62015-10-30 02:47:38 -0700784 if (voe_wrapper_->hw()->EnableBuiltInNS(*options.noise_suppression) ==
785 0 &&
786 *options.noise_suppression) {
henrikac14f5ff2015-09-23 14:08:33 +0200787 // Disable internal software NS if built-in NS is enabled,
788 // i.e., replace the software NS with the built-in NS.
Karl Wibergbe579832015-11-10 22:34:18 +0100789 options.noise_suppression = rtc::Optional<bool>(false);
henrikac14f5ff2015-09-23 14:08:33 +0200790 LOG(LS_INFO) << "Disabling NS since built-in NS will be used instead";
791 }
792 }
kwiberg102c6a62015-10-30 02:47:38 -0700793 if (voep->SetNsStatus(*options.noise_suppression, ns_mode) == -1) {
794 LOG_RTCERR2(SetNsStatus, *options.noise_suppression, ns_mode);
buildbot@webrtc.org13d67762014-05-02 17:33:29 +0000795 return false;
796 } else {
kwiberg102c6a62015-10-30 02:47:38 -0700797 LOG(LS_INFO) << "Noise suppression set to " << *options.noise_suppression
henrikac14f5ff2015-09-23 14:08:33 +0200798 << " with mode " << ns_mode;
buildbot@webrtc.org13d67762014-05-02 17:33:29 +0000799 }
800 }
801
kwiberg102c6a62015-10-30 02:47:38 -0700802 if (options.highpass_filter) {
803 LOG(LS_INFO) << "High pass filter enabled? " << *options.highpass_filter;
804 if (voep->EnableHighPassFilter(*options.highpass_filter) == -1) {
805 LOG_RTCERR1(SetHighpassFilterStatus, *options.highpass_filter);
buildbot@webrtc.org13d67762014-05-02 17:33:29 +0000806 return false;
807 }
808 }
809
kwiberg102c6a62015-10-30 02:47:38 -0700810 if (options.stereo_swapping) {
811 LOG(LS_INFO) << "Stereo swapping enabled? " << *options.stereo_swapping;
812 voep->EnableStereoChannelSwapping(*options.stereo_swapping);
813 if (voep->IsStereoChannelSwappingEnabled() != *options.stereo_swapping) {
814 LOG_RTCERR1(EnableStereoChannelSwapping, *options.stereo_swapping);
buildbot@webrtc.org13d67762014-05-02 17:33:29 +0000815 return false;
816 }
817 }
818
kwiberg102c6a62015-10-30 02:47:38 -0700819 if (options.audio_jitter_buffer_max_packets) {
820 LOG(LS_INFO) << "NetEq capacity is "
821 << *options.audio_jitter_buffer_max_packets;
Henrik Lundin64dad832015-05-11 12:44:23 +0200822 voe_config_.Set<webrtc::NetEqCapacityConfig>(
kwiberg102c6a62015-10-30 02:47:38 -0700823 new webrtc::NetEqCapacityConfig(
824 *options.audio_jitter_buffer_max_packets));
Henrik Lundin64dad832015-05-11 12:44:23 +0200825 }
826
kwiberg102c6a62015-10-30 02:47:38 -0700827 if (options.audio_jitter_buffer_fast_accelerate) {
828 LOG(LS_INFO) << "NetEq fast mode? "
829 << *options.audio_jitter_buffer_fast_accelerate;
Henrik Lundin5263b3c2015-06-01 10:29:41 +0200830 voe_config_.Set<webrtc::NetEqFastAccelerate>(
kwiberg102c6a62015-10-30 02:47:38 -0700831 new webrtc::NetEqFastAccelerate(
832 *options.audio_jitter_buffer_fast_accelerate));
Henrik Lundin5263b3c2015-06-01 10:29:41 +0200833 }
834
kwiberg102c6a62015-10-30 02:47:38 -0700835 if (options.typing_detection) {
836 LOG(LS_INFO) << "Typing detection is enabled? "
837 << *options.typing_detection;
838 if (voep->SetTypingDetectionStatus(*options.typing_detection) == -1) {
buildbot@webrtc.org13d67762014-05-02 17:33:29 +0000839 // In case of error, log the info and continue
kwiberg102c6a62015-10-30 02:47:38 -0700840 LOG_RTCERR1(SetTypingDetectionStatus, *options.typing_detection);
buildbot@webrtc.org13d67762014-05-02 17:33:29 +0000841 }
842 }
843
kwiberg102c6a62015-10-30 02:47:38 -0700844 if (options.adjust_agc_delta) {
845 LOG(LS_INFO) << "Adjust agc delta is " << *options.adjust_agc_delta;
846 if (!AdjustAgcLevel(*options.adjust_agc_delta)) {
buildbot@webrtc.org13d67762014-05-02 17:33:29 +0000847 return false;
848 }
849 }
850
kwiberg102c6a62015-10-30 02:47:38 -0700851 if (options.aec_dump) {
852 LOG(LS_INFO) << "Aec dump is enabled? " << *options.aec_dump;
853 if (*options.aec_dump)
buildbot@webrtc.org13d67762014-05-02 17:33:29 +0000854 StartAecDump(kAecDumpByAudioOptionFilename);
855 else
856 StopAecDump();
857 }
858
buildbot@webrtc.org1f8a2372014-08-28 10:52:44 +0000859 webrtc::Config config;
860
kwiberg102c6a62015-10-30 02:47:38 -0700861 if (options.delay_agnostic_aec)
862 delay_agnostic_aec_ = options.delay_agnostic_aec;
863 if (delay_agnostic_aec_) {
864 LOG(LS_INFO) << "Delay agnostic aec is enabled? " << *delay_agnostic_aec_;
henrik.lundin0f133b92015-07-02 00:17:55 -0700865 config.Set<webrtc::DelayAgnostic>(
kwiberg102c6a62015-10-30 02:47:38 -0700866 new webrtc::DelayAgnostic(*delay_agnostic_aec_));
Bjorn Volckerbf395c12015-03-25 22:45:56 +0100867 }
868
kwiberg102c6a62015-10-30 02:47:38 -0700869 if (options.extended_filter_aec) {
870 extended_filter_aec_ = options.extended_filter_aec;
871 }
872 if (extended_filter_aec_) {
873 LOG(LS_INFO) << "Extended filter aec is enabled? " << *extended_filter_aec_;
Henrik Lundin441f6342015-06-09 16:03:13 +0200874 config.Set<webrtc::ExtendedFilter>(
kwiberg102c6a62015-10-30 02:47:38 -0700875 new webrtc::ExtendedFilter(*extended_filter_aec_));
buildbot@webrtc.org1f8a2372014-08-28 10:52:44 +0000876 }
877
kwiberg102c6a62015-10-30 02:47:38 -0700878 if (options.experimental_ns) {
879 experimental_ns_ = options.experimental_ns;
880 }
881 if (experimental_ns_) {
882 LOG(LS_INFO) << "Experimental ns is enabled? " << *experimental_ns_;
buildbot@webrtc.org1f8a2372014-08-28 10:52:44 +0000883 config.Set<webrtc::ExperimentalNs>(
kwiberg102c6a62015-10-30 02:47:38 -0700884 new webrtc::ExperimentalNs(*experimental_ns_));
buildbot@webrtc.org1f8a2372014-08-28 10:52:44 +0000885 }
buildbot@webrtc.org1f8a2372014-08-28 10:52:44 +0000886
887 // We check audioproc for the benefit of tests, since FakeWebRtcVoiceEngine
888 // returns NULL on audio_processing().
889 webrtc::AudioProcessing* audioproc = voe_wrapper_->base()->audio_processing();
890 if (audioproc) {
891 audioproc->SetExtraOptions(config);
892 }
893
kwiberg102c6a62015-10-30 02:47:38 -0700894 if (options.recording_sample_rate) {
895 LOG(LS_INFO) << "Recording sample rate is "
896 << *options.recording_sample_rate;
897 if (voe_wrapper_->hw()->SetRecordingSampleRate(
898 *options.recording_sample_rate)) {
899 LOG_RTCERR1(SetRecordingSampleRate, *options.recording_sample_rate);
buildbot@webrtc.org13d67762014-05-02 17:33:29 +0000900 }
901 }
902
kwiberg102c6a62015-10-30 02:47:38 -0700903 if (options.playout_sample_rate) {
904 LOG(LS_INFO) << "Playout sample rate is " << *options.playout_sample_rate;
905 if (voe_wrapper_->hw()->SetPlayoutSampleRate(
906 *options.playout_sample_rate)) {
907 LOG_RTCERR1(SetPlayoutSampleRate, *options.playout_sample_rate);
buildbot@webrtc.org13d67762014-05-02 17:33:29 +0000908 }
909 }
910
911 return true;
912}
913
buildbot@webrtc.org13d67762014-05-02 17:33:29 +0000914// TODO(juberti): Refactor this so that the core logic can be used to set the
915// soundclip device. At that time, reinstate the soundclip pause/resume code.
916bool WebRtcVoiceEngine::SetDevices(const Device* in_device,
917 const Device* out_device) {
solenberg566ef242015-11-06 15:34:49 -0800918 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
buildbot@webrtc.org13d67762014-05-02 17:33:29 +0000919#if !defined(IOS)
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000920 int in_id = in_device ? rtc::FromString<int>(in_device->id) :
buildbot@webrtc.org13d67762014-05-02 17:33:29 +0000921 kDefaultAudioDeviceId;
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000922 int out_id = out_device ? rtc::FromString<int>(out_device->id) :
buildbot@webrtc.org13d67762014-05-02 17:33:29 +0000923 kDefaultAudioDeviceId;
924 // The device manager uses -1 as the default device, which was the case for
925 // VoE 3.5. VoE 4.0, however, uses 0 as the default in Linux and Mac.
926#ifndef WIN32
927 if (-1 == in_id) {
928 in_id = kDefaultAudioDeviceId;
929 }
930 if (-1 == out_id) {
931 out_id = kDefaultAudioDeviceId;
932 }
933#endif
934
935 std::string in_name = (in_id != kDefaultAudioDeviceId) ?
936 in_device->name : "Default device";
937 std::string out_name = (out_id != kDefaultAudioDeviceId) ?
938 out_device->name : "Default device";
939 LOG(LS_INFO) << "Setting microphone to (id=" << in_id << ", name=" << in_name
940 << ") and speaker to (id=" << out_id << ", name=" << out_name
941 << ")";
942
buildbot@webrtc.org13d67762014-05-02 17:33:29 +0000943 // Must also pause all audio playback and capture.
solenbergc1a1b352015-09-22 13:31:20 -0700944 bool ret = true;
Fredrik Solenbergaf9fb212015-08-26 10:45:53 +0200945 for (WebRtcVoiceMediaChannel* channel : channels_) {
buildbot@webrtc.org13d67762014-05-02 17:33:29 +0000946 if (!channel->PausePlayout()) {
947 LOG(LS_WARNING) << "Failed to pause playout";
948 ret = false;
949 }
950 if (!channel->PauseSend()) {
951 LOG(LS_WARNING) << "Failed to pause send";
952 ret = false;
953 }
954 }
955
956 // Find the recording device id in VoiceEngine and set recording device.
957 if (!FindWebRtcAudioDeviceId(true, in_name, in_id, &in_id)) {
958 ret = false;
959 }
960 if (ret) {
961 if (voe_wrapper_->hw()->SetRecordingDevice(in_id) == -1) {
962 LOG_RTCERR2(SetRecordingDevice, in_name, in_id);
963 ret = false;
964 }
buildbot@webrtc.org6b21b712014-07-31 15:08:53 +0000965 webrtc::AudioProcessing* ap = voe()->base()->audio_processing();
966 if (ap)
967 ap->Initialize();
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000968 }
969
970 // Find the playout device id in VoiceEngine and set playout device.
971 if (!FindWebRtcAudioDeviceId(false, out_name, out_id, &out_id)) {
972 LOG(LS_WARNING) << "Failed to find VoiceEngine device id for " << out_name;
973 ret = false;
974 }
975 if (ret) {
976 if (voe_wrapper_->hw()->SetPlayoutDevice(out_id) == -1) {
sergeyu@chromium.org5bc25c42013-12-05 00:24:06 +0000977 LOG_RTCERR2(SetPlayoutDevice, out_name, out_id);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000978 ret = false;
979 }
980 }
981
982 // Resume all audio playback and capture.
Fredrik Solenbergaf9fb212015-08-26 10:45:53 +0200983 for (WebRtcVoiceMediaChannel* channel : channels_) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000984 if (!channel->ResumePlayout()) {
985 LOG(LS_WARNING) << "Failed to resume playout";
986 ret = false;
987 }
988 if (!channel->ResumeSend()) {
989 LOG(LS_WARNING) << "Failed to resume send";
990 ret = false;
991 }
992 }
993
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000994 if (ret) {
995 LOG(LS_INFO) << "Set microphone to (id=" << in_id <<" name=" << in_name
996 << ") and speaker to (id="<< out_id << " name=" << out_name
997 << ")";
998 }
999
1000 return ret;
1001#else
1002 return true;
wu@webrtc.orgcecfd182013-10-30 05:18:12 +00001003#endif // !IOS
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001004}
1005
1006bool WebRtcVoiceEngine::FindWebRtcAudioDeviceId(
1007 bool is_input, const std::string& dev_name, int dev_id, int* rtc_id) {
solenberg566ef242015-11-06 15:34:49 -08001008 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001009 // In Linux, VoiceEngine uses the same device dev_id as the device manager.
wu@webrtc.orgcecfd182013-10-30 05:18:12 +00001010#if defined(LINUX) || defined(ANDROID)
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001011 *rtc_id = dev_id;
1012 return true;
1013#else
1014 // In Windows and Mac, we need to find the VoiceEngine device id by name
1015 // unless the input dev_id is the default device id.
1016 if (kDefaultAudioDeviceId == dev_id) {
1017 *rtc_id = dev_id;
1018 return true;
1019 }
1020
1021 // Get the number of VoiceEngine audio devices.
1022 int count = 0;
1023 if (is_input) {
1024 if (-1 == voe_wrapper_->hw()->GetNumOfRecordingDevices(count)) {
1025 LOG_RTCERR0(GetNumOfRecordingDevices);
1026 return false;
1027 }
1028 } else {
1029 if (-1 == voe_wrapper_->hw()->GetNumOfPlayoutDevices(count)) {
1030 LOG_RTCERR0(GetNumOfPlayoutDevices);
1031 return false;
1032 }
1033 }
1034
1035 for (int i = 0; i < count; ++i) {
1036 char name[128];
1037 char guid[128];
1038 if (is_input) {
1039 voe_wrapper_->hw()->GetRecordingDeviceName(i, name, guid);
1040 LOG(LS_VERBOSE) << "VoiceEngine microphone " << i << ": " << name;
1041 } else {
1042 voe_wrapper_->hw()->GetPlayoutDeviceName(i, name, guid);
1043 LOG(LS_VERBOSE) << "VoiceEngine speaker " << i << ": " << name;
1044 }
1045
1046 std::string webrtc_name(name);
1047 if (dev_name.compare(0, webrtc_name.size(), webrtc_name) == 0) {
1048 *rtc_id = i;
1049 return true;
1050 }
1051 }
1052 LOG(LS_WARNING) << "VoiceEngine cannot find device: " << dev_name;
1053 return false;
1054#endif
1055}
1056
1057bool WebRtcVoiceEngine::GetOutputVolume(int* level) {
solenberg566ef242015-11-06 15:34:49 -08001058 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001059 unsigned int ulevel;
1060 if (voe_wrapper_->volume()->GetSpeakerVolume(ulevel) == -1) {
1061 LOG_RTCERR1(GetSpeakerVolume, level);
1062 return false;
1063 }
1064 *level = ulevel;
1065 return true;
1066}
1067
1068bool WebRtcVoiceEngine::SetOutputVolume(int level) {
solenberg566ef242015-11-06 15:34:49 -08001069 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
henrikg91d6ede2015-09-17 00:24:34 -07001070 RTC_DCHECK(level >= 0 && level <= 255);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001071 if (voe_wrapper_->volume()->SetSpeakerVolume(level) == -1) {
1072 LOG_RTCERR1(SetSpeakerVolume, level);
1073 return false;
1074 }
1075 return true;
1076}
1077
1078int WebRtcVoiceEngine::GetInputLevel() {
solenberg566ef242015-11-06 15:34:49 -08001079 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001080 unsigned int ulevel;
1081 return (voe_wrapper_->volume()->GetSpeechInputLevel(ulevel) != -1) ?
1082 static_cast<int>(ulevel) : -1;
1083}
1084
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001085const std::vector<AudioCodec>& WebRtcVoiceEngine::codecs() {
solenberg566ef242015-11-06 15:34:49 -08001086 RTC_DCHECK(signal_thread_checker_.CalledOnValidThread());
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001087 return codecs_;
1088}
1089
1090bool WebRtcVoiceEngine::FindCodec(const AudioCodec& in) {
solenberg566ef242015-11-06 15:34:49 -08001091 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001092 return FindWebRtcCodec(in, NULL);
1093}
1094
1095// Get the VoiceEngine codec that matches |in|, with the supplied settings.
1096bool WebRtcVoiceEngine::FindWebRtcCodec(const AudioCodec& in,
1097 webrtc::CodecInst* out) {
solenberg566ef242015-11-06 15:34:49 -08001098 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001099 int ncodecs = voe_wrapper_->codec()->NumOfCodecs();
1100 for (int i = 0; i < ncodecs; ++i) {
1101 webrtc::CodecInst voe_codec;
henrik.lundin@webrtc.org8038d422014-11-11 08:38:24 +00001102 if (GetVoeCodec(i, &voe_codec)) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001103 AudioCodec codec(voe_codec.pltype, voe_codec.plname, voe_codec.plfreq,
1104 voe_codec.rate, voe_codec.channels, 0);
1105 bool multi_rate = IsCodecMultiRate(voe_codec);
1106 // Allow arbitrary rates for ISAC to be specified.
1107 if (multi_rate) {
1108 // Set codec.bitrate to 0 so the check for codec.Matches() passes.
1109 codec.bitrate = 0;
1110 }
1111 if (codec.Matches(in)) {
1112 if (out) {
1113 // Fixup the payload type.
1114 voe_codec.pltype = in.id;
1115
1116 // Set bitrate if specified.
1117 if (multi_rate && in.bitrate != 0) {
1118 voe_codec.rate = in.bitrate;
1119 }
1120
henrik.lundin@webrtc.orgf85dbce2014-11-07 12:25:00 +00001121 // Reset G722 sample rate to 16000 to match WebRTC.
1122 MaybeFixupG722(&voe_codec, 16000);
1123
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001124 // Apply codec-specific settings.
Minyue Li7100dcd2015-03-27 05:05:59 +01001125 if (IsCodec(codec, kIsacCodecName)) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001126 // If ISAC and an explicit bitrate is not specified,
minyue@webrtc.org26236952014-10-29 02:27:08 +00001127 // enable auto bitrate adjustment.
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001128 voe_codec.rate = (in.bitrate > 0) ? in.bitrate : -1;
1129 }
1130 *out = voe_codec;
1131 }
1132 return true;
1133 }
1134 }
1135 }
1136 return false;
1137}
solenberg7add0582015-11-20 09:59:34 -08001138
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001139const std::vector<RtpHeaderExtension>&
1140WebRtcVoiceEngine::rtp_header_extensions() const {
solenberg566ef242015-11-06 15:34:49 -08001141 RTC_DCHECK(signal_thread_checker_.CalledOnValidThread());
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001142 return rtp_header_extensions_;
1143}
1144
1145void WebRtcVoiceEngine::SetLogging(int min_sev, const char* filter) {
solenberg566ef242015-11-06 15:34:49 -08001146 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001147 // if min_sev == -1, we keep the current log level.
1148 if (min_sev >= 0) {
1149 SetTraceFilter(SeverityToFilter(min_sev));
1150 }
1151 log_options_ = filter;
1152 SetTraceOptions(initialized_ ? log_options_ : "");
1153}
1154
1155int WebRtcVoiceEngine::GetLastEngineError() {
solenberg566ef242015-11-06 15:34:49 -08001156 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001157 return voe_wrapper_->error();
1158}
1159
1160void WebRtcVoiceEngine::SetTraceFilter(int filter) {
solenberg566ef242015-11-06 15:34:49 -08001161 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001162 log_filter_ = filter;
1163 tracing_->SetTraceFilter(filter);
1164}
1165
1166// We suppport three different logging settings for VoiceEngine:
1167// 1. Observer callback that goes into talk diagnostic logfile.
1168// Use --logfile and --loglevel
1169//
1170// 2. Encrypted VoiceEngine log for debugging VoiceEngine.
1171// Use --voice_loglevel --voice_logfilter "tracefile file_name"
1172//
1173// 3. EC log and dump for debugging QualityEngine.
1174// Use --voice_loglevel --voice_logfilter "recordEC file_name"
1175//
1176// For more details see: "https://sites.google.com/a/google.com/wavelet/Home/
1177// Magic-Flute--RTC-Engine-/Magic-Flute-Command-Line-Parameters"
1178void WebRtcVoiceEngine::SetTraceOptions(const std::string& options) {
solenberg566ef242015-11-06 15:34:49 -08001179 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001180 // Set encrypted trace file.
1181 std::vector<std::string> opts;
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00001182 rtc::tokenize(options, ' ', '"', '"', &opts);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001183 std::vector<std::string>::iterator tracefile =
1184 std::find(opts.begin(), opts.end(), "tracefile");
1185 if (tracefile != opts.end() && ++tracefile != opts.end()) {
1186 // Write encrypted debug output (at same loglevel) to file
1187 // EncryptedTraceFile no longer supported.
1188 if (tracing_->SetTraceFile(tracefile->c_str()) == -1) {
1189 LOG_RTCERR1(SetTraceFile, *tracefile);
1190 }
1191 }
1192
wu@webrtc.org97077a32013-10-25 21:18:33 +00001193 // Allow trace options to override the trace filter. We default
1194 // it to log_filter_ (as a translation of libjingle log levels)
1195 // elsewhere, but this allows clients to explicitly set webrtc
1196 // log levels.
1197 std::vector<std::string>::iterator tracefilter =
1198 std::find(opts.begin(), opts.end(), "tracefilter");
1199 if (tracefilter != opts.end() && ++tracefilter != opts.end()) {
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00001200 if (!tracing_->SetTraceFilter(rtc::FromString<int>(*tracefilter))) {
wu@webrtc.org97077a32013-10-25 21:18:33 +00001201 LOG_RTCERR1(SetTraceFilter, *tracefilter);
1202 }
1203 }
1204
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001205 // Set AEC dump file
1206 std::vector<std::string>::iterator recordEC =
1207 std::find(opts.begin(), opts.end(), "recordEC");
1208 if (recordEC != opts.end()) {
1209 ++recordEC;
1210 if (recordEC != opts.end())
1211 StartAecDump(recordEC->c_str());
1212 else
1213 StopAecDump();
1214 }
1215}
1216
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001217void WebRtcVoiceEngine::Print(webrtc::TraceLevel level, const char* trace,
1218 int length) {
solenberg566ef242015-11-06 15:34:49 -08001219 // Note: This callback can happen on any thread!
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00001220 rtc::LoggingSeverity sev = rtc::LS_VERBOSE;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001221 if (level == webrtc::kTraceError || level == webrtc::kTraceCritical)
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00001222 sev = rtc::LS_ERROR;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001223 else if (level == webrtc::kTraceWarning)
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00001224 sev = rtc::LS_WARNING;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001225 else if (level == webrtc::kTraceStateInfo || level == webrtc::kTraceInfo)
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00001226 sev = rtc::LS_INFO;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001227 else if (level == webrtc::kTraceTerseInfo)
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00001228 sev = rtc::LS_INFO;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001229
1230 // Skip past boilerplate prefix text
1231 if (length < 72) {
1232 std::string msg(trace, length);
1233 LOG(LS_ERROR) << "Malformed webrtc log message: ";
1234 LOG_V(sev) << msg;
1235 } else {
1236 std::string msg(trace + 71, length - 72);
Peter Boströmd5c75b12015-09-23 13:24:32 +02001237 LOG_V(sev) << "webrtc: " << msg;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001238 }
1239}
1240
solenberg63b34542015-09-29 06:06:31 -07001241void WebRtcVoiceEngine::RegisterChannel(WebRtcVoiceMediaChannel* channel) {
solenberg566ef242015-11-06 15:34:49 -08001242 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
1243 RTC_DCHECK(channel);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001244 channels_.push_back(channel);
1245}
1246
solenberg63b34542015-09-29 06:06:31 -07001247void WebRtcVoiceEngine::UnregisterChannel(WebRtcVoiceMediaChannel* channel) {
solenberg566ef242015-11-06 15:34:49 -08001248 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
solenberg63b34542015-09-29 06:06:31 -07001249 auto it = std::find(channels_.begin(), channels_.end(), channel);
solenberg566ef242015-11-06 15:34:49 -08001250 RTC_DCHECK(it != channels_.end());
1251 channels_.erase(it);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001252}
1253
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001254// Adjusts the default AGC target level by the specified delta.
1255// NB: If we start messing with other config fields, we'll want
1256// to save the current webrtc::AgcConfig as well.
1257bool WebRtcVoiceEngine::AdjustAgcLevel(int delta) {
solenberg566ef242015-11-06 15:34:49 -08001258 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001259 webrtc::AgcConfig config = default_agc_config_;
1260 config.targetLeveldBOv -= delta;
1261
1262 LOG(LS_INFO) << "Adjusting AGC level from default -"
1263 << default_agc_config_.targetLeveldBOv << "dB to -"
1264 << config.targetLeveldBOv << "dB";
1265
1266 if (voe_wrapper_->processing()->SetAgcConfig(config) == -1) {
1267 LOG_RTCERR1(SetAgcConfig, config.targetLeveldBOv);
1268 return false;
1269 }
1270 return true;
1271}
1272
Fredrik Solenbergccb49e72015-05-19 11:37:56 +02001273bool WebRtcVoiceEngine::SetAudioDeviceModule(webrtc::AudioDeviceModule* adm) {
solenberg566ef242015-11-06 15:34:49 -08001274 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001275 if (initialized_) {
1276 LOG(LS_WARNING) << "SetAudioDeviceModule can not be called after Init.";
1277 return false;
1278 }
1279 if (adm_) {
1280 adm_->Release();
1281 adm_ = NULL;
1282 }
1283 if (adm) {
1284 adm_ = adm;
1285 adm_->AddRef();
1286 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001287 return true;
1288}
1289
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00001290bool WebRtcVoiceEngine::StartAecDump(rtc::PlatformFile file) {
solenberg566ef242015-11-06 15:34:49 -08001291 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00001292 FILE* aec_dump_file_stream = rtc::FdopenPlatformFileForWriting(file);
wu@webrtc.orga8910d22014-01-23 22:12:45 +00001293 if (!aec_dump_file_stream) {
1294 LOG(LS_ERROR) << "Could not open AEC dump file stream.";
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00001295 if (!rtc::ClosePlatformFile(file))
wu@webrtc.orga8910d22014-01-23 22:12:45 +00001296 LOG(LS_WARNING) << "Could not close file.";
1297 return false;
1298 }
wu@webrtc.orga9890802013-12-13 00:21:03 +00001299 StopAecDump();
wu@webrtc.orga8910d22014-01-23 22:12:45 +00001300 if (voe_wrapper_->processing()->StartDebugRecording(aec_dump_file_stream) !=
wu@webrtc.orga9890802013-12-13 00:21:03 +00001301 webrtc::AudioProcessing::kNoError) {
wu@webrtc.orga8910d22014-01-23 22:12:45 +00001302 LOG_RTCERR0(StartDebugRecording);
1303 fclose(aec_dump_file_stream);
wu@webrtc.orga9890802013-12-13 00:21:03 +00001304 return false;
1305 }
1306 is_dumping_aec_ = true;
1307 return true;
wu@webrtc.orga9890802013-12-13 00:21:03 +00001308}
1309
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001310void WebRtcVoiceEngine::StartAecDump(const std::string& filename) {
solenberg566ef242015-11-06 15:34:49 -08001311 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001312 if (!is_dumping_aec_) {
1313 // Start dumping AEC when we are not dumping.
1314 if (voe_wrapper_->processing()->StartDebugRecording(
1315 filename.c_str()) != webrtc::AudioProcessing::kNoError) {
wu@webrtc.orga9890802013-12-13 00:21:03 +00001316 LOG_RTCERR1(StartDebugRecording, filename.c_str());
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001317 } else {
1318 is_dumping_aec_ = true;
1319 }
1320 }
1321}
1322
1323void WebRtcVoiceEngine::StopAecDump() {
solenberg566ef242015-11-06 15:34:49 -08001324 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001325 if (is_dumping_aec_) {
1326 // Stop dumping AEC when we are dumping.
1327 if (voe_wrapper_->processing()->StopDebugRecording() !=
1328 webrtc::AudioProcessing::kNoError) {
1329 LOG_RTCERR0(StopDebugRecording);
1330 }
1331 is_dumping_aec_ = false;
1332 }
1333}
1334
ivoc112a3d82015-10-16 02:22:18 -07001335bool WebRtcVoiceEngine::StartRtcEventLog(rtc::PlatformFile file) {
solenberg566ef242015-11-06 15:34:49 -08001336 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
ivoc112a3d82015-10-16 02:22:18 -07001337 return voe_wrapper_->codec()->GetEventLog()->StartLogging(file);
1338}
1339
1340void WebRtcVoiceEngine::StopRtcEventLog() {
solenberg566ef242015-11-06 15:34:49 -08001341 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
ivoc112a3d82015-10-16 02:22:18 -07001342 voe_wrapper_->codec()->GetEventLog()->StopLogging();
1343}
1344
solenberg0a617e22015-10-20 15:49:38 -07001345int WebRtcVoiceEngine::CreateVoEChannel() {
solenberg566ef242015-11-06 15:34:49 -08001346 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
solenberg0a617e22015-10-20 15:49:38 -07001347 return voe_wrapper_->base()->CreateChannel(voe_config_);
sergeyu@chromium.org5bc25c42013-12-05 00:24:06 +00001348}
1349
solenbergc96df772015-10-21 13:01:53 -07001350class WebRtcVoiceMediaChannel::WebRtcAudioSendStream
mallinath@webrtc.org67ee6b92014-02-03 16:57:16 +00001351 : public AudioRenderer::Sink {
1352 public:
solenbergc96df772015-10-21 13:01:53 -07001353 WebRtcAudioSendStream(int ch, webrtc::AudioTransport* voe_audio_transport,
solenberg3a941542015-11-16 07:34:50 -08001354 uint32_t ssrc, const std::string& c_name,
1355 const std::vector<webrtc::RtpExtension>& extensions,
1356 webrtc::Call* call)
solenberg7add0582015-11-20 09:59:34 -08001357 : voe_audio_transport_(voe_audio_transport),
solenberg3a941542015-11-16 07:34:50 -08001358 call_(call),
1359 config_(nullptr) {
solenberg85a04962015-10-27 03:35:21 -07001360 RTC_DCHECK_GE(ch, 0);
1361 // TODO(solenberg): Once we're not using FakeWebRtcVoiceEngine anymore:
1362 // RTC_DCHECK(voe_audio_transport);
solenbergc96df772015-10-21 13:01:53 -07001363 RTC_DCHECK(call);
solenberg85a04962015-10-27 03:35:21 -07001364 audio_capture_thread_checker_.DetachFromThread();
solenberg3a941542015-11-16 07:34:50 -08001365 config_.rtp.ssrc = ssrc;
1366 config_.rtp.c_name = c_name;
1367 config_.voe_channel_id = ch;
1368 RecreateAudioSendStream(extensions);
solenbergc96df772015-10-21 13:01:53 -07001369 }
solenberg3a941542015-11-16 07:34:50 -08001370
solenbergc96df772015-10-21 13:01:53 -07001371 ~WebRtcAudioSendStream() override {
solenberg566ef242015-11-06 15:34:49 -08001372 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
solenbergc96df772015-10-21 13:01:53 -07001373 Stop();
1374 call_->DestroyAudioSendStream(stream_);
1375 }
henrike@webrtc.org1e09a712013-07-26 19:17:59 +00001376
solenberg3a941542015-11-16 07:34:50 -08001377 void RecreateAudioSendStream(
1378 const std::vector<webrtc::RtpExtension>& extensions) {
1379 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
1380 if (stream_) {
1381 call_->DestroyAudioSendStream(stream_);
1382 stream_ = nullptr;
1383 }
1384 config_.rtp.extensions = extensions;
1385 RTC_DCHECK(!stream_);
1386 stream_ = call_->CreateAudioSendStream(config_);
1387 RTC_CHECK(stream_);
1388 }
1389
1390 webrtc::AudioSendStream::Stats GetStats() const {
1391 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
1392 RTC_DCHECK(stream_);
1393 return stream_->GetStats();
1394 }
1395
mallinath@webrtc.org67ee6b92014-02-03 16:57:16 +00001396 // Starts the rendering by setting a sink to the renderer to get data
1397 // callback.
henrike@webrtc.orga7b98182014-02-21 15:51:43 +00001398 // This method is called on the libjingle worker thread.
mallinath@webrtc.org67ee6b92014-02-03 16:57:16 +00001399 // TODO(xians): Make sure Start() is called only once.
1400 void Start(AudioRenderer* renderer) {
solenberg566ef242015-11-06 15:34:49 -08001401 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
solenbergc96df772015-10-21 13:01:53 -07001402 RTC_DCHECK(renderer);
1403 if (renderer_) {
henrikg91d6ede2015-09-17 00:24:34 -07001404 RTC_DCHECK(renderer_ == renderer);
mallinath@webrtc.org67ee6b92014-02-03 16:57:16 +00001405 return;
1406 }
mallinath@webrtc.org67ee6b92014-02-03 16:57:16 +00001407 renderer->SetSink(this);
1408 renderer_ = renderer;
1409 }
1410
solenbergc96df772015-10-21 13:01:53 -07001411 // Stops rendering by setting the sink of the renderer to nullptr. No data
mallinath@webrtc.org67ee6b92014-02-03 16:57:16 +00001412 // callback will be received after this method.
henrike@webrtc.orga7b98182014-02-21 15:51:43 +00001413 // This method is called on the libjingle worker thread.
mallinath@webrtc.org67ee6b92014-02-03 16:57:16 +00001414 void Stop() {
solenberg566ef242015-11-06 15:34:49 -08001415 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
solenbergc96df772015-10-21 13:01:53 -07001416 if (renderer_) {
1417 renderer_->SetSink(nullptr);
1418 renderer_ = nullptr;
solenberg98c68862015-10-09 03:27:14 -07001419 }
mallinath@webrtc.org67ee6b92014-02-03 16:57:16 +00001420 }
1421
1422 // AudioRenderer::Sink implementation.
henrike@webrtc.orga7b98182014-02-21 15:51:43 +00001423 // This method is called on the audio thread.
kjellander@webrtc.org14665ff2015-03-04 12:58:35 +00001424 void OnData(const void* audio_data,
1425 int bits_per_sample,
1426 int sample_rate,
1427 int number_of_channels,
Peter Kastingdce40cf2015-08-24 14:52:23 -07001428 size_t number_of_frames) override {
solenberg566ef242015-11-06 15:34:49 -08001429 RTC_DCHECK(!worker_thread_checker_.CalledOnValidThread());
solenberg85a04962015-10-27 03:35:21 -07001430 RTC_DCHECK(audio_capture_thread_checker_.CalledOnValidThread());
solenbergc96df772015-10-21 13:01:53 -07001431 RTC_DCHECK(voe_audio_transport_);
solenberg7add0582015-11-20 09:59:34 -08001432 voe_audio_transport_->OnData(config_.voe_channel_id,
henrike@webrtc.orga7b98182014-02-21 15:51:43 +00001433 audio_data,
1434 bits_per_sample,
1435 sample_rate,
1436 number_of_channels,
1437 number_of_frames);
henrike@webrtc.orga7b98182014-02-21 15:51:43 +00001438 }
1439
1440 // Callback from the |renderer_| when it is going away. In case Start() has
1441 // never been called, this callback won't be triggered.
kjellander@webrtc.org14665ff2015-03-04 12:58:35 +00001442 void OnClose() override {
solenberg566ef242015-11-06 15:34:49 -08001443 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
solenbergc96df772015-10-21 13:01:53 -07001444 // Set |renderer_| to nullptr to make sure no more callback will get into
henrike@webrtc.orga7b98182014-02-21 15:51:43 +00001445 // the renderer.
solenbergc96df772015-10-21 13:01:53 -07001446 renderer_ = nullptr;
mallinath@webrtc.org67ee6b92014-02-03 16:57:16 +00001447 }
1448
1449 // Accessor to the VoE channel ID.
solenberg85a04962015-10-27 03:35:21 -07001450 int channel() const {
solenberg566ef242015-11-06 15:34:49 -08001451 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
solenberg7add0582015-11-20 09:59:34 -08001452 return config_.voe_channel_id;
solenberg85a04962015-10-27 03:35:21 -07001453 }
mallinath@webrtc.org67ee6b92014-02-03 16:57:16 +00001454
1455 private:
solenberg566ef242015-11-06 15:34:49 -08001456 rtc::ThreadChecker worker_thread_checker_;
solenberg85a04962015-10-27 03:35:21 -07001457 rtc::ThreadChecker audio_capture_thread_checker_;
solenbergc96df772015-10-21 13:01:53 -07001458 webrtc::AudioTransport* const voe_audio_transport_ = nullptr;
1459 webrtc::Call* call_ = nullptr;
solenberg3a941542015-11-16 07:34:50 -08001460 webrtc::AudioSendStream::Config config_;
1461 // The stream is owned by WebRtcAudioSendStream and may be reallocated if
1462 // configuration changes.
solenbergc96df772015-10-21 13:01:53 -07001463 webrtc::AudioSendStream* stream_ = nullptr;
mallinath@webrtc.org67ee6b92014-02-03 16:57:16 +00001464
1465 // Raw pointer to AudioRenderer owned by LocalAudioTrackHandler.
1466 // PeerConnection will make sure invalidating the pointer before the object
1467 // goes away.
solenbergc96df772015-10-21 13:01:53 -07001468 AudioRenderer* renderer_ = nullptr;
henrike@webrtc.orga7b98182014-02-21 15:51:43 +00001469
solenbergc96df772015-10-21 13:01:53 -07001470 RTC_DISALLOW_IMPLICIT_CONSTRUCTORS(WebRtcAudioSendStream);
1471};
1472
1473class WebRtcVoiceMediaChannel::WebRtcAudioReceiveStream {
1474 public:
solenberg7add0582015-11-20 09:59:34 -08001475 WebRtcAudioReceiveStream(int ch, uint32_t remote_ssrc, uint32_t local_ssrc,
1476 bool use_combined_bwe, const std::string& sync_group,
1477 const std::vector<webrtc::RtpExtension>& extensions,
1478 webrtc::Call* call)
1479 : call_(call),
1480 config_() {
1481 RTC_DCHECK_GE(ch, 0);
1482 RTC_DCHECK(call);
1483 config_.rtp.remote_ssrc = remote_ssrc;
1484 config_.rtp.local_ssrc = local_ssrc;
1485 config_.voe_channel_id = ch;
1486 config_.sync_group = sync_group;
1487 RecreateAudioReceiveStream(use_combined_bwe, extensions);
1488 }
solenbergc96df772015-10-21 13:01:53 -07001489
solenberg7add0582015-11-20 09:59:34 -08001490 ~WebRtcAudioReceiveStream() {
1491 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
1492 call_->DestroyAudioReceiveStream(stream_);
1493 }
1494
1495 void RecreateAudioReceiveStream(
1496 const std::vector<webrtc::RtpExtension>& extensions) {
1497 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
1498 RecreateAudioReceiveStream(config_.combined_audio_video_bwe, extensions);
1499 }
1500 void RecreateAudioReceiveStream(bool use_combined_bwe) {
1501 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
1502 RecreateAudioReceiveStream(use_combined_bwe, config_.rtp.extensions);
1503 }
1504
1505 webrtc::AudioReceiveStream::Stats GetStats() const {
1506 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
1507 RTC_DCHECK(stream_);
1508 return stream_->GetStats();
1509 }
1510
1511 int channel() const {
1512 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
1513 return config_.voe_channel_id;
1514 }
solenbergc96df772015-10-21 13:01:53 -07001515
1516 private:
solenberg7add0582015-11-20 09:59:34 -08001517 void RecreateAudioReceiveStream(bool use_combined_bwe,
1518 const std::vector<webrtc::RtpExtension>& extensions) {
1519 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
1520 if (stream_) {
1521 call_->DestroyAudioReceiveStream(stream_);
1522 stream_ = nullptr;
1523 }
1524 config_.rtp.extensions = extensions;
1525 config_.combined_audio_video_bwe = use_combined_bwe;
1526 RTC_DCHECK(!stream_);
1527 stream_ = call_->CreateAudioReceiveStream(config_);
1528 RTC_CHECK(stream_);
1529 }
1530
1531 rtc::ThreadChecker worker_thread_checker_;
1532 webrtc::Call* call_ = nullptr;
1533 webrtc::AudioReceiveStream::Config config_;
1534 // The stream is owned by WebRtcAudioReceiveStream and may be reallocated if
1535 // configuration changes.
1536 webrtc::AudioReceiveStream* stream_ = nullptr;
solenbergc96df772015-10-21 13:01:53 -07001537
1538 RTC_DISALLOW_IMPLICIT_CONSTRUCTORS(WebRtcAudioReceiveStream);
henrike@webrtc.org1e09a712013-07-26 19:17:59 +00001539};
1540
Fredrik Solenberg709ed672015-09-15 12:26:33 +02001541WebRtcVoiceMediaChannel::WebRtcVoiceMediaChannel(WebRtcVoiceEngine* engine,
Fredrik Solenbergb071a192015-09-17 16:42:56 +02001542 const AudioOptions& options,
Fredrik Solenberg709ed672015-09-15 12:26:33 +02001543 webrtc::Call* call)
solenberg566ef242015-11-06 15:34:49 -08001544 : engine_(engine), call_(call) {
solenberg0a617e22015-10-20 15:49:38 -07001545 LOG(LS_VERBOSE) << "WebRtcVoiceMediaChannel::WebRtcVoiceMediaChannel";
solenberg566ef242015-11-06 15:34:49 -08001546 RTC_DCHECK(call);
solenberg0a617e22015-10-20 15:49:38 -07001547 engine->RegisterChannel(this);
Fredrik Solenbergb071a192015-09-17 16:42:56 +02001548 SetOptions(options);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001549}
1550
1551WebRtcVoiceMediaChannel::~WebRtcVoiceMediaChannel() {
solenberg566ef242015-11-06 15:34:49 -08001552 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
solenberg0a617e22015-10-20 15:49:38 -07001553 LOG(LS_VERBOSE) << "WebRtcVoiceMediaChannel::~WebRtcVoiceMediaChannel";
solenberg7add0582015-11-20 09:59:34 -08001554 // TODO(solenberg): Should be able to delete the streams directly, without
1555 // going through RemoveNnStream(), once stream objects handle
1556 // all (de)configuration.
solenbergc96df772015-10-21 13:01:53 -07001557 while (!send_streams_.empty()) {
1558 RemoveSendStream(send_streams_.begin()->first);
solenbergd97ec302015-10-07 01:40:33 -07001559 }
solenberg7add0582015-11-20 09:59:34 -08001560 while (!recv_streams_.empty()) {
1561 RemoveRecvStream(recv_streams_.begin()->first);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001562 }
solenberg0a617e22015-10-20 15:49:38 -07001563 engine()->UnregisterChannel(this);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001564}
1565
Peter Thatcherc2ee2c82015-08-07 16:05:34 -07001566bool WebRtcVoiceMediaChannel::SetSendParameters(
1567 const AudioSendParameters& params) {
solenberg566ef242015-11-06 15:34:49 -08001568 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
Peter Thatcherc2ee2c82015-08-07 16:05:34 -07001569 // TODO(pthatcher): Refactor this to be more clean now that we have
1570 // all the information at once.
solenberg3a941542015-11-16 07:34:50 -08001571
1572 if (!SetSendCodecs(params.codecs)) {
1573 return false;
1574 }
1575
1576 std::vector<webrtc::RtpExtension> send_rtp_extensions =
1577 FindAudioRtpHeaderExtensions(params.extensions);
1578 if (send_rtp_extensions_ != send_rtp_extensions) {
1579 send_rtp_extensions_.swap(send_rtp_extensions);
1580 for (auto& it : send_streams_) {
1581 it.second->RecreateAudioSendStream(send_rtp_extensions_);
1582 }
1583 }
1584
1585 if (!SetMaxSendBandwidth(params.max_bandwidth_bps)) {
1586 return false;
1587 }
1588 return SetOptions(params.options);
Peter Thatcherc2ee2c82015-08-07 16:05:34 -07001589}
1590
1591bool WebRtcVoiceMediaChannel::SetRecvParameters(
1592 const AudioRecvParameters& params) {
solenberg566ef242015-11-06 15:34:49 -08001593 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
Peter Thatcherc2ee2c82015-08-07 16:05:34 -07001594 // TODO(pthatcher): Refactor this to be more clean now that we have
1595 // all the information at once.
solenberg7add0582015-11-20 09:59:34 -08001596
1597 if (!SetRecvCodecs(params.codecs)) {
1598 return false;
1599 }
1600
1601 std::vector<webrtc::RtpExtension> recv_rtp_extensions =
1602 FindAudioRtpHeaderExtensions(params.extensions);
1603 if (recv_rtp_extensions_ != recv_rtp_extensions) {
1604 recv_rtp_extensions_.swap(recv_rtp_extensions);
1605 for (auto& it : recv_streams_) {
1606 it.second->RecreateAudioReceiveStream(recv_rtp_extensions_);
1607 }
1608 }
1609
1610 return true;
Peter Thatcherc2ee2c82015-08-07 16:05:34 -07001611}
1612
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001613bool WebRtcVoiceMediaChannel::SetOptions(const AudioOptions& options) {
solenberg566ef242015-11-06 15:34:49 -08001614 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001615 LOG(LS_INFO) << "Setting voice channel options: "
1616 << options.ToString();
1617
wu@webrtc.orgde305012013-10-31 15:40:38 +00001618 // Check if DSCP value is changed from previous.
1619 bool dscp_option_changed = (options_.dscp != options.dscp);
1620
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001621 // We retain all of the existing options, and apply the given ones
1622 // on top. This means there is no way to "clear" options such that
1623 // they go back to the engine default.
1624 options_.SetAll(options);
1625
1626 if (send_ != SEND_NOTHING) {
solenberg63b34542015-09-29 06:06:31 -07001627 if (!engine()->ApplyOptions(options_)) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001628 LOG(LS_WARNING) <<
solenberg63b34542015-09-29 06:06:31 -07001629 "Failed to apply engine options during channel SetOptions.";
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001630 return false;
1631 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001632 }
1633
wu@webrtc.orgde305012013-10-31 15:40:38 +00001634 if (dscp_option_changed) {
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00001635 rtc::DiffServCodePoint dscp = rtc::DSCP_DEFAULT;
kwiberg102c6a62015-10-30 02:47:38 -07001636 if (options_.dscp.value_or(false))
wu@webrtc.orgde305012013-10-31 15:40:38 +00001637 dscp = kAudioDscpValue;
1638 if (MediaChannel::SetDscp(dscp) != 0) {
1639 LOG(LS_WARNING) << "Failed to set DSCP settings for audio channel";
1640 }
1641 }
solenberg8fb30c32015-10-13 03:06:58 -07001642
solenbergc96df772015-10-21 13:01:53 -07001643 // TODO(solenberg): Don't recreate unless options changed.
solenberg7add0582015-11-20 09:59:34 -08001644 for (auto& it : recv_streams_) {
1645 it.second->RecreateAudioReceiveStream(
1646 options_.combined_audio_video_bwe.value_or(false));
1647 }
solenberg8fb30c32015-10-13 03:06:58 -07001648
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001649 LOG(LS_INFO) << "Set voice channel options. Current options: "
1650 << options_.ToString();
1651 return true;
1652}
1653
1654bool WebRtcVoiceMediaChannel::SetRecvCodecs(
1655 const std::vector<AudioCodec>& codecs) {
solenberg566ef242015-11-06 15:34:49 -08001656 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
solenberg8fb30c32015-10-13 03:06:58 -07001657
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001658 // Set the payload types to be used for incoming media.
solenberg0b675462015-10-09 01:37:09 -07001659 LOG(LS_INFO) << "Setting receive voice codecs.";
solenberg0b675462015-10-09 01:37:09 -07001660
1661 if (!VerifyUniquePayloadTypes(codecs)) {
1662 LOG(LS_ERROR) << "Codec payload types overlap.";
1663 return false;
1664 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001665
1666 std::vector<AudioCodec> new_codecs;
1667 // Find all new codecs. We allow adding new codecs but don't allow changing
1668 // the payload type of codecs that is already configured since we might
1669 // already be receiving packets with that payload type.
Fredrik Solenbergaf9fb212015-08-26 10:45:53 +02001670 for (const AudioCodec& codec : codecs) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001671 AudioCodec old_codec;
Fredrik Solenbergaf9fb212015-08-26 10:45:53 +02001672 if (FindCodec(recv_codecs_, codec, &old_codec)) {
1673 if (old_codec.id != codec.id) {
1674 LOG(LS_ERROR) << codec.name << " payload type changed.";
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001675 return false;
1676 }
1677 } else {
Fredrik Solenbergaf9fb212015-08-26 10:45:53 +02001678 new_codecs.push_back(codec);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001679 }
1680 }
1681 if (new_codecs.empty()) {
1682 // There are no new codecs to configure. Already configured codecs are
1683 // never removed.
1684 return true;
1685 }
1686
1687 if (playout_) {
1688 // Receive codecs can not be changed while playing. So we temporarily
1689 // pause playout.
1690 PausePlayout();
1691 }
1692
Fredrik Solenbergaf9fb212015-08-26 10:45:53 +02001693 bool result = SetRecvCodecsInternal(new_codecs);
1694 if (result) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001695 recv_codecs_ = codecs;
1696 }
1697
1698 if (desired_playout_ && !playout_) {
1699 ResumePlayout();
1700 }
Fredrik Solenbergaf9fb212015-08-26 10:45:53 +02001701 return result;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001702}
1703
1704bool WebRtcVoiceMediaChannel::SetSendCodecs(
wu@webrtc.orgcadf9042013-08-30 21:24:16 +00001705 int channel, const std::vector<AudioCodec>& codecs) {
buildbot@webrtc.orgae740dd2014-06-17 10:56:41 +00001706 // Disable VAD, FEC, and RED unless we know the other side wants them.
wu@webrtc.orgcadf9042013-08-30 21:24:16 +00001707 engine()->voe()->codec()->SetVADStatus(channel, false);
1708 engine()->voe()->rtp()->SetNACKStatus(channel, false, 0);
buildbot@webrtc.orgae740dd2014-06-17 10:56:41 +00001709 engine()->voe()->rtp()->SetREDStatus(channel, false);
1710 engine()->voe()->codec()->SetFECStatus(channel, false);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001711
1712 // Scan through the list to figure out the codec to use for sending, along
1713 // with the proper configuration for VAD and DTMF.
henrike@webrtc.org704bf9e2014-02-27 17:52:04 +00001714 bool found_send_codec = false;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001715 webrtc::CodecInst send_codec;
1716 memset(&send_codec, 0, sizeof(send_codec));
1717
wu@webrtc.org05e7b442014-04-01 17:44:24 +00001718 bool nack_enabled = nack_enabled_;
buildbot@webrtc.org3ffa1f92014-07-02 19:51:26 +00001719 bool enable_codec_fec = false;
Minyue Li7100dcd2015-03-27 05:05:59 +01001720 bool enable_opus_dtx = false;
minyue@webrtc.org26236952014-10-29 02:27:08 +00001721 int opus_max_playback_rate = 0;
buildbot@webrtc.org5d639b32014-09-10 07:57:12 +00001722
henrike@webrtc.org704bf9e2014-02-27 17:52:04 +00001723 // Set send codec (the first non-telephone-event/CN codec)
Fredrik Solenbergaf9fb212015-08-26 10:45:53 +02001724 for (const AudioCodec& codec : codecs) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001725 // Ignore codecs we don't know about. The negotiation step should prevent
1726 // this, but double-check to be sure.
1727 webrtc::CodecInst voe_codec;
Fredrik Solenbergaf9fb212015-08-26 10:45:53 +02001728 if (!engine()->FindWebRtcCodec(codec, &voe_codec)) {
1729 LOG(LS_WARNING) << "Unknown codec " << ToString(codec);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001730 continue;
1731 }
1732
Fredrik Solenbergaf9fb212015-08-26 10:45:53 +02001733 if (IsCodec(codec, kDtmfCodecName) || IsCodec(codec, kCnCodecName)) {
henrike@webrtc.org704bf9e2014-02-27 17:52:04 +00001734 // Skip telephone-event/CN codec, which will be handled later.
1735 continue;
1736 }
1737
henrike@webrtc.org704bf9e2014-02-27 17:52:04 +00001738 // We'll use the first codec in the list to actually send audio data.
1739 // Be sure to use the payload type requested by the remote side.
buildbot@webrtc.orgae740dd2014-06-17 10:56:41 +00001740 // "red", for RED audio, is a special case where the actual codec to be
henrike@webrtc.org704bf9e2014-02-27 17:52:04 +00001741 // used is specified in params.
Fredrik Solenbergaf9fb212015-08-26 10:45:53 +02001742 if (IsCodec(codec, kRedCodecName)) {
henrike@webrtc.org704bf9e2014-02-27 17:52:04 +00001743 // Parse out the RED parameters. If we fail, just ignore RED;
1744 // we don't support all possible params/usage scenarios.
Fredrik Solenbergaf9fb212015-08-26 10:45:53 +02001745 if (!GetRedSendCodec(codec, codecs, &send_codec)) {
henrike@webrtc.org704bf9e2014-02-27 17:52:04 +00001746 continue;
1747 }
1748
1749 // Enable redundant encoding of the specified codec. Treat any
1750 // failure as a fatal internal error.
buildbot@webrtc.orgae740dd2014-06-17 10:56:41 +00001751 LOG(LS_INFO) << "Enabling RED on channel " << channel;
Fredrik Solenbergaf9fb212015-08-26 10:45:53 +02001752 if (engine()->voe()->rtp()->SetREDStatus(channel, true, codec.id) == -1) {
1753 LOG_RTCERR3(SetREDStatus, channel, true, codec.id);
henrike@webrtc.org704bf9e2014-02-27 17:52:04 +00001754 return false;
1755 }
1756 } else {
1757 send_codec = voe_codec;
Fredrik Solenbergaf9fb212015-08-26 10:45:53 +02001758 nack_enabled = IsNackEnabled(codec);
Minyue Li7100dcd2015-03-27 05:05:59 +01001759 // For Opus as the send codec, we are to determine inband FEC, maximum
1760 // playback rate, and opus internal dtx.
Fredrik Solenbergaf9fb212015-08-26 10:45:53 +02001761 if (IsCodec(codec, kOpusCodecName)) {
1762 GetOpusConfig(codec, &send_codec, &enable_codec_fec,
Minyue Li7100dcd2015-03-27 05:05:59 +01001763 &opus_max_playback_rate, &enable_opus_dtx);
buildbot@webrtc.org5d639b32014-09-10 07:57:12 +00001764 }
Brave Yao5225dd82015-03-26 07:39:19 +08001765
1766 // Set packet size if the AudioCodec param kCodecParamPTime is set.
1767 int ptime_ms = 0;
Fredrik Solenbergaf9fb212015-08-26 10:45:53 +02001768 if (codec.GetParam(kCodecParamPTime, &ptime_ms)) {
Brave Yao5225dd82015-03-26 07:39:19 +08001769 if (!SetPTimeAsPacketSize(&send_codec, ptime_ms)) {
1770 LOG(LS_WARNING) << "Failed to set packet size for codec "
1771 << send_codec.plname;
1772 return false;
1773 }
1774 }
henrike@webrtc.org704bf9e2014-02-27 17:52:04 +00001775 }
1776 found_send_codec = true;
1777 break;
1778 }
1779
wu@webrtc.org05e7b442014-04-01 17:44:24 +00001780 if (nack_enabled_ != nack_enabled) {
1781 SetNack(channel, nack_enabled);
1782 nack_enabled_ = nack_enabled;
1783 }
1784
henrike@webrtc.org704bf9e2014-02-27 17:52:04 +00001785 if (!found_send_codec) {
1786 LOG(LS_WARNING) << "Received empty list of codecs.";
1787 return false;
1788 }
1789
1790 // Set the codec immediately, since SetVADStatus() depends on whether
1791 // the current codec is mono or stereo.
1792 if (!SetSendCodec(channel, send_codec))
1793 return false;
1794
buildbot@webrtc.org3ffa1f92014-07-02 19:51:26 +00001795 // FEC should be enabled after SetSendCodec.
1796 if (enable_codec_fec) {
1797 LOG(LS_INFO) << "Attempt to enable codec internal FEC on channel "
1798 << channel;
buildbot@webrtc.org3ffa1f92014-07-02 19:51:26 +00001799 if (engine()->voe()->codec()->SetFECStatus(channel, true) == -1) {
1800 // Enable codec internal FEC. Treat any failure as fatal internal error.
1801 LOG_RTCERR2(SetFECStatus, channel, true);
1802 return false;
1803 }
buildbot@webrtc.org3ffa1f92014-07-02 19:51:26 +00001804 }
1805
Minyue Li7100dcd2015-03-27 05:05:59 +01001806 if (IsCodec(send_codec, kOpusCodecName)) {
1807 // DTX and maxplaybackrate should be set after SetSendCodec. Because current
1808 // send codec has to be Opus.
1809
1810 // Set Opus internal DTX.
1811 LOG(LS_INFO) << "Attempt to "
1812 << GetEnableString(enable_opus_dtx)
1813 << " Opus DTX on channel "
buildbot@webrtc.org5d639b32014-09-10 07:57:12 +00001814 << channel;
Minyue Li7100dcd2015-03-27 05:05:59 +01001815 if (engine()->voe()->codec()->SetOpusDtx(channel, enable_opus_dtx)) {
1816 LOG_RTCERR2(SetOpusDtx, channel, enable_opus_dtx);
1817 return false;
1818 }
1819
1820 // If opus_max_playback_rate <= 0, the default maximum playback rate
1821 // (48 kHz) will be used.
1822 if (opus_max_playback_rate > 0) {
1823 LOG(LS_INFO) << "Attempt to set maximum playback rate to "
1824 << opus_max_playback_rate
1825 << " Hz on channel "
1826 << channel;
1827 if (engine()->voe()->codec()->SetOpusMaxPlaybackRate(
1828 channel, opus_max_playback_rate) == -1) {
1829 LOG_RTCERR2(SetOpusMaxPlaybackRate, channel, opus_max_playback_rate);
1830 return false;
1831 }
buildbot@webrtc.org5d639b32014-09-10 07:57:12 +00001832 }
buildbot@webrtc.org5d639b32014-09-10 07:57:12 +00001833 }
1834
henrike@webrtc.org704bf9e2014-02-27 17:52:04 +00001835 // Always update the |send_codec_| to the currently set send codec.
1836 send_codec_.reset(new webrtc::CodecInst(send_codec));
1837
minyue@webrtc.org26236952014-10-29 02:27:08 +00001838 if (send_bitrate_setting_) {
1839 SetSendBitrateInternal(send_bitrate_bps_);
henrike@webrtc.org704bf9e2014-02-27 17:52:04 +00001840 }
1841
1842 // Loop through the codecs list again to config the telephone-event/CN codec.
Fredrik Solenbergaf9fb212015-08-26 10:45:53 +02001843 for (const AudioCodec& codec : codecs) {
henrike@webrtc.org704bf9e2014-02-27 17:52:04 +00001844 // Ignore codecs we don't know about. The negotiation step should prevent
1845 // this, but double-check to be sure.
1846 webrtc::CodecInst voe_codec;
Fredrik Solenbergaf9fb212015-08-26 10:45:53 +02001847 if (!engine()->FindWebRtcCodec(codec, &voe_codec)) {
1848 LOG(LS_WARNING) << "Unknown codec " << ToString(codec);
henrike@webrtc.org704bf9e2014-02-27 17:52:04 +00001849 continue;
1850 }
1851
wu@webrtc.org9dba5252013-08-05 20:36:57 +00001852 // Find the DTMF telephone event "codec" and tell VoiceEngine channels
1853 // about it.
Fredrik Solenbergaf9fb212015-08-26 10:45:53 +02001854 if (IsCodec(codec, kDtmfCodecName)) {
wu@webrtc.orgcadf9042013-08-30 21:24:16 +00001855 if (engine()->voe()->dtmf()->SetSendTelephoneEventPayloadType(
Fredrik Solenbergaf9fb212015-08-26 10:45:53 +02001856 channel, codec.id) == -1) {
1857 LOG_RTCERR2(SetSendTelephoneEventPayloadType, channel, codec.id);
wu@webrtc.orgcadf9042013-08-30 21:24:16 +00001858 return false;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001859 }
Fredrik Solenbergaf9fb212015-08-26 10:45:53 +02001860 } else if (IsCodec(codec, kCnCodecName)) {
henrike@webrtc.org704bf9e2014-02-27 17:52:04 +00001861 // Turn voice activity detection/comfort noise on if supported.
1862 // Set the wideband CN payload type appropriately.
1863 // (narrowband always uses the static payload type 13).
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001864 webrtc::PayloadFrequencies cn_freq;
Fredrik Solenbergaf9fb212015-08-26 10:45:53 +02001865 switch (codec.clockrate) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001866 case 8000:
1867 cn_freq = webrtc::kFreq8000Hz;
1868 break;
1869 case 16000:
1870 cn_freq = webrtc::kFreq16000Hz;
1871 break;
1872 case 32000:
1873 cn_freq = webrtc::kFreq32000Hz;
1874 break;
1875 default:
Fredrik Solenbergaf9fb212015-08-26 10:45:53 +02001876 LOG(LS_WARNING) << "CN frequency " << codec.clockrate
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001877 << " not supported.";
1878 continue;
1879 }
wu@webrtc.orgcadf9042013-08-30 21:24:16 +00001880 // Set the CN payloadtype and the VAD status.
1881 // The CN payload type for 8000 Hz clockrate is fixed at 13.
1882 if (cn_freq != webrtc::kFreq8000Hz) {
1883 if (engine()->voe()->codec()->SetSendCNPayloadType(
Fredrik Solenbergaf9fb212015-08-26 10:45:53 +02001884 channel, codec.id, cn_freq) == -1) {
1885 LOG_RTCERR3(SetSendCNPayloadType, channel, codec.id, cn_freq);
wu@webrtc.orgcadf9042013-08-30 21:24:16 +00001886 // TODO(ajm): This failure condition will be removed from VoE.
1887 // Restore the return here when we update to a new enough webrtc.
1888 //
1889 // Not returning false because the SetSendCNPayloadType will fail if
1890 // the channel is already sending.
1891 // This can happen if the remote description is applied twice, for
1892 // example in the case of ROAP on top of JSEP, where both side will
1893 // send the offer.
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001894 }
wu@webrtc.orgcadf9042013-08-30 21:24:16 +00001895 }
wu@webrtc.orgcadf9042013-08-30 21:24:16 +00001896 // Only turn on VAD if we have a CN payload type that matches the
1897 // clockrate for the codec we are going to use.
Fredrik Solenbergaf9fb212015-08-26 10:45:53 +02001898 if (codec.clockrate == send_codec.plfreq && send_codec.channels != 2) {
Minyue Li7100dcd2015-03-27 05:05:59 +01001899 // TODO(minyue): If CN frequency == 48000 Hz is allowed, consider the
1900 // interaction between VAD and Opus FEC.
wu@webrtc.orgcadf9042013-08-30 21:24:16 +00001901 LOG(LS_INFO) << "Enabling VAD";
1902 if (engine()->voe()->codec()->SetVADStatus(channel, true) == -1) {
1903 LOG_RTCERR2(SetVADStatus, channel, true);
1904 return false;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001905 }
1906 }
1907 }
wu@webrtc.org1d1ffc92013-10-16 18:12:02 +00001908 }
wu@webrtc.orgcadf9042013-08-30 21:24:16 +00001909 return true;
1910}
1911
1912bool WebRtcVoiceMediaChannel::SetSendCodecs(
1913 const std::vector<AudioCodec>& codecs) {
solenberg566ef242015-11-06 15:34:49 -08001914 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
solenbergd97ec302015-10-07 01:40:33 -07001915
wu@webrtc.orgcadf9042013-08-30 21:24:16 +00001916 dtmf_allowed_ = false;
Fredrik Solenbergaf9fb212015-08-26 10:45:53 +02001917 for (const AudioCodec& codec : codecs) {
wu@webrtc.orgcadf9042013-08-30 21:24:16 +00001918 // Find the DTMF telephone event "codec".
Fredrik Solenbergaf9fb212015-08-26 10:45:53 +02001919 if (IsCodec(codec, kDtmfCodecName)) {
wu@webrtc.orgcadf9042013-08-30 21:24:16 +00001920 dtmf_allowed_ = true;
1921 }
1922 }
1923
1924 // Cache the codecs in order to configure the channel created later.
1925 send_codecs_ = codecs;
solenbergc96df772015-10-21 13:01:53 -07001926 for (const auto& ch : send_streams_) {
Fredrik Solenbergaf9fb212015-08-26 10:45:53 +02001927 if (!SetSendCodecs(ch.second->channel(), codecs)) {
wu@webrtc.orgcadf9042013-08-30 21:24:16 +00001928 return false;
1929 }
1930 }
1931
wu@webrtc.org05e7b442014-04-01 17:44:24 +00001932 // Set nack status on receive channels and update |nack_enabled_|.
solenberg7add0582015-11-20 09:59:34 -08001933 for (const auto& ch : recv_streams_) {
solenberg0a617e22015-10-20 15:49:38 -07001934 SetNack(ch.second->channel(), nack_enabled_);
wu@webrtc.org9dba5252013-08-05 20:36:57 +00001935 }
solenberg0a617e22015-10-20 15:49:38 -07001936
1937 return true;
wu@webrtc.org9dba5252013-08-05 20:36:57 +00001938}
1939
wu@webrtc.orgcadf9042013-08-30 21:24:16 +00001940void WebRtcVoiceMediaChannel::SetNack(int channel, bool nack_enabled) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001941 if (nack_enabled) {
wu@webrtc.orgcadf9042013-08-30 21:24:16 +00001942 LOG(LS_INFO) << "Enabling NACK for channel " << channel;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001943 engine()->voe()->rtp()->SetNACKStatus(channel, true, kNackMaxPackets);
1944 } else {
wu@webrtc.orgcadf9042013-08-30 21:24:16 +00001945 LOG(LS_INFO) << "Disabling NACK for channel " << channel;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001946 engine()->voe()->rtp()->SetNACKStatus(channel, false, 0);
1947 }
1948}
1949
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001950bool WebRtcVoiceMediaChannel::SetSendCodec(
wu@webrtc.org9dba5252013-08-05 20:36:57 +00001951 int channel, const webrtc::CodecInst& send_codec) {
1952 LOG(LS_INFO) << "Send channel " << channel << " selected voice codec "
1953 << ToString(send_codec) << ", bitrate=" << send_codec.rate;
1954
wu@webrtc.org05e7b442014-04-01 17:44:24 +00001955 webrtc::CodecInst current_codec;
1956 if (engine()->voe()->codec()->GetSendCodec(channel, current_codec) == 0 &&
1957 (send_codec == current_codec)) {
1958 // Codec is already configured, we can return without setting it again.
1959 return true;
1960 }
1961
wu@webrtc.org9dba5252013-08-05 20:36:57 +00001962 if (engine()->voe()->codec()->SetSendCodec(channel, send_codec) == -1) {
1963 LOG_RTCERR2(SetSendCodec, channel, ToString(send_codec));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001964 return false;
1965 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001966 return true;
1967}
1968
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001969bool WebRtcVoiceMediaChannel::SetPlayout(bool playout) {
1970 desired_playout_ = playout;
1971 return ChangePlayout(desired_playout_);
1972}
1973
1974bool WebRtcVoiceMediaChannel::PausePlayout() {
1975 return ChangePlayout(false);
1976}
1977
1978bool WebRtcVoiceMediaChannel::ResumePlayout() {
1979 return ChangePlayout(desired_playout_);
1980}
1981
1982bool WebRtcVoiceMediaChannel::ChangePlayout(bool playout) {
solenberg566ef242015-11-06 15:34:49 -08001983 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001984 if (playout_ == playout) {
1985 return true;
1986 }
1987
solenberg7add0582015-11-20 09:59:34 -08001988 for (const auto& ch : recv_streams_) {
Fredrik Solenbergaf9fb212015-08-26 10:45:53 +02001989 if (!SetPlayout(ch.second->channel(), playout)) {
henrike@webrtc.org1e09a712013-07-26 19:17:59 +00001990 LOG(LS_ERROR) << "SetPlayout " << playout << " on channel "
Fredrik Solenbergaf9fb212015-08-26 10:45:53 +02001991 << ch.second->channel() << " failed";
solenberg1ac56142015-10-13 03:58:19 -07001992 return false;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001993 }
1994 }
solenberg1ac56142015-10-13 03:58:19 -07001995 playout_ = playout;
1996 return true;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001997}
1998
1999bool WebRtcVoiceMediaChannel::SetSend(SendFlags send) {
2000 desired_send_ = send;
solenbergc96df772015-10-21 13:01:53 -07002001 if (!send_streams_.empty()) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002002 return ChangeSend(desired_send_);
solenbergc96df772015-10-21 13:01:53 -07002003 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002004 return true;
2005}
2006
2007bool WebRtcVoiceMediaChannel::PauseSend() {
2008 return ChangeSend(SEND_NOTHING);
2009}
2010
2011bool WebRtcVoiceMediaChannel::ResumeSend() {
2012 return ChangeSend(desired_send_);
2013}
2014
2015bool WebRtcVoiceMediaChannel::ChangeSend(SendFlags send) {
2016 if (send_ == send) {
2017 return true;
2018 }
2019
solenberg63b34542015-09-29 06:06:31 -07002020 // Apply channel specific options.
2021 if (send == SEND_MICROPHONE) {
2022 engine()->ApplyOptions(options_);
2023 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002024
wu@webrtc.org9dba5252013-08-05 20:36:57 +00002025 // Change the settings on each send channel.
solenbergc96df772015-10-21 13:01:53 -07002026 for (const auto& ch : send_streams_) {
solenberg63b34542015-09-29 06:06:31 -07002027 if (!ChangeSend(ch.second->channel(), send)) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002028 return false;
solenberg63b34542015-09-29 06:06:31 -07002029 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002030 }
wu@webrtc.org9dba5252013-08-05 20:36:57 +00002031
solenberg63b34542015-09-29 06:06:31 -07002032 // Clear up the options after stopping sending. Since we may previously have
2033 // applied the channel specific options, now apply the original options stored
2034 // in WebRtcVoiceEngine.
2035 if (send == SEND_NOTHING) {
2036 engine()->ApplyOptions(engine()->GetOptions());
2037 }
wu@webrtc.org9dba5252013-08-05 20:36:57 +00002038
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002039 send_ = send;
2040 return true;
2041}
2042
wu@webrtc.org9dba5252013-08-05 20:36:57 +00002043bool WebRtcVoiceMediaChannel::ChangeSend(int channel, SendFlags send) {
2044 if (send == SEND_MICROPHONE) {
2045 if (engine()->voe()->base()->StartSend(channel) == -1) {
2046 LOG_RTCERR1(StartSend, channel);
2047 return false;
2048 }
wu@webrtc.org9dba5252013-08-05 20:36:57 +00002049 } else { // SEND_NOTHING
henrikg91d6ede2015-09-17 00:24:34 -07002050 RTC_DCHECK(send == SEND_NOTHING);
wu@webrtc.org9dba5252013-08-05 20:36:57 +00002051 if (engine()->voe()->base()->StopSend(channel) == -1) {
2052 LOG_RTCERR1(StopSend, channel);
2053 return false;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002054 }
2055 }
2056
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002057 return true;
2058}
2059
Peter Boström0c4e06b2015-10-07 12:23:21 +02002060bool WebRtcVoiceMediaChannel::SetAudioSend(uint32_t ssrc,
2061 bool enable,
solenberg1dd98f32015-09-10 01:57:14 -07002062 const AudioOptions* options,
2063 AudioRenderer* renderer) {
solenberg566ef242015-11-06 15:34:49 -08002064 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
solenberg1dd98f32015-09-10 01:57:14 -07002065 // TODO(solenberg): The state change should be fully rolled back if any one of
2066 // these calls fail.
2067 if (!SetLocalRenderer(ssrc, renderer)) {
2068 return false;
2069 }
solenbergdfc8f4f2015-10-01 02:31:10 -07002070 if (!MuteStream(ssrc, !enable)) {
solenberg1dd98f32015-09-10 01:57:14 -07002071 return false;
2072 }
solenbergdfc8f4f2015-10-01 02:31:10 -07002073 if (enable && options) {
solenberg1dd98f32015-09-10 01:57:14 -07002074 return SetOptions(*options);
2075 }
2076 return true;
2077}
2078
solenberg0a617e22015-10-20 15:49:38 -07002079int WebRtcVoiceMediaChannel::CreateVoEChannel() {
2080 int id = engine()->CreateVoEChannel();
2081 if (id == -1) {
2082 LOG_RTCERR0(CreateVoEChannel);
2083 return -1;
wu@webrtc.org9dba5252013-08-05 20:36:57 +00002084 }
solenberg0a617e22015-10-20 15:49:38 -07002085 if (engine()->voe()->network()->RegisterExternalTransport(id, *this) == -1) {
2086 LOG_RTCERR2(RegisterExternalTransport, id, this);
2087 engine()->voe()->base()->DeleteChannel(id);
2088 return -1;
2089 }
2090 return id;
wu@webrtc.org9dba5252013-08-05 20:36:57 +00002091}
2092
solenberg7add0582015-11-20 09:59:34 -08002093bool WebRtcVoiceMediaChannel::DeleteVoEChannel(int channel) {
wu@webrtc.org9dba5252013-08-05 20:36:57 +00002094 if (engine()->voe()->network()->DeRegisterExternalTransport(channel) == -1) {
2095 LOG_RTCERR1(DeRegisterExternalTransport, channel);
2096 }
wu@webrtc.org9dba5252013-08-05 20:36:57 +00002097 if (engine()->voe()->base()->DeleteChannel(channel) == -1) {
2098 LOG_RTCERR1(DeleteChannel, channel);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002099 return false;
2100 }
wu@webrtc.org9dba5252013-08-05 20:36:57 +00002101 return true;
2102}
henrike@webrtc.org1e09a712013-07-26 19:17:59 +00002103
wu@webrtc.org9dba5252013-08-05 20:36:57 +00002104bool WebRtcVoiceMediaChannel::AddSendStream(const StreamParams& sp) {
solenberg566ef242015-11-06 15:34:49 -08002105 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
solenberg0a617e22015-10-20 15:49:38 -07002106 LOG(LS_INFO) << "AddSendStream: " << sp.ToString();
2107
2108 uint32_t ssrc = sp.first_ssrc();
2109 RTC_DCHECK(0 != ssrc);
2110
2111 if (GetSendChannelId(ssrc) != -1) {
2112 LOG(LS_ERROR) << "Stream already exists with ssrc " << ssrc;
wu@webrtc.org9dba5252013-08-05 20:36:57 +00002113 return false;
2114 }
2115
solenberg0a617e22015-10-20 15:49:38 -07002116 // Create a new channel for sending audio data.
2117 int channel = CreateVoEChannel();
2118 if (channel == -1) {
2119 return false;
wu@webrtc.org9dba5252013-08-05 20:36:57 +00002120 }
wu@webrtc.org9dba5252013-08-05 20:36:57 +00002121
solenbergc96df772015-10-21 13:01:53 -07002122 // Save the channel to send_streams_, so that RemoveSendStream() can still
wu@webrtc.org9dba5252013-08-05 20:36:57 +00002123 // delete the channel in case failure happens below.
mallinath@webrtc.org67ee6b92014-02-03 16:57:16 +00002124 webrtc::AudioTransport* audio_transport =
2125 engine()->voe()->base()->audio_transport();
solenberg3a941542015-11-16 07:34:50 -08002126 send_streams_.insert(std::make_pair(ssrc, new WebRtcAudioSendStream(
2127 channel, audio_transport, ssrc, sp.cname, send_rtp_extensions_, call_)));
wu@webrtc.org9dba5252013-08-05 20:36:57 +00002128
solenberg0a617e22015-10-20 15:49:38 -07002129 // Set the current codecs to be used for the new channel. We need to do this
2130 // after adding the channel to send_channels_, because of how max bitrate is
2131 // currently being configured by SetSendCodec().
2132 if (!send_codecs_.empty() && !SetSendCodecs(channel, send_codecs_)) {
2133 RemoveSendStream(ssrc);
wu@webrtc.org9dba5252013-08-05 20:36:57 +00002134 return false;
2135 }
2136
2137 // At this point the channel's local SSRC has been updated. If the channel is
solenberg0a617e22015-10-20 15:49:38 -07002138 // the first send channel make sure that all the receive channels are updated
2139 // with the same SSRC in order to send receiver reports.
solenbergc96df772015-10-21 13:01:53 -07002140 if (send_streams_.size() == 1) {
solenberg0a617e22015-10-20 15:49:38 -07002141 receiver_reports_ssrc_ = ssrc;
solenberg7add0582015-11-20 09:59:34 -08002142 for (const auto& stream : recv_streams_) {
2143 int recv_channel = stream.second->channel();
solenberg0a617e22015-10-20 15:49:38 -07002144 if (engine()->voe()->rtp()->SetLocalSSRC(recv_channel, ssrc) != 0) {
solenberg7add0582015-11-20 09:59:34 -08002145 LOG_RTCERR2(SetLocalSSRC, recv_channel, ssrc);
solenberg1ac56142015-10-13 03:58:19 -07002146 return false;
wu@webrtc.org9dba5252013-08-05 20:36:57 +00002147 }
solenberg0a617e22015-10-20 15:49:38 -07002148 engine()->voe()->base()->AssociateSendChannel(recv_channel, channel);
2149 LOG(LS_INFO) << "VoiceEngine channel #" << recv_channel
2150 << " is associated with channel #" << channel << ".";
wu@webrtc.org9dba5252013-08-05 20:36:57 +00002151 }
2152 }
2153
wu@webrtc.org9dba5252013-08-05 20:36:57 +00002154 return ChangeSend(channel, desired_send_);
2155}
2156
Peter Boström0c4e06b2015-10-07 12:23:21 +02002157bool WebRtcVoiceMediaChannel::RemoveSendStream(uint32_t ssrc) {
solenberg566ef242015-11-06 15:34:49 -08002158 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
solenberg3a941542015-11-16 07:34:50 -08002159 LOG(LS_INFO) << "RemoveSendStream: " << ssrc;
2160
solenbergc96df772015-10-21 13:01:53 -07002161 auto it = send_streams_.find(ssrc);
2162 if (it == send_streams_.end()) {
wu@webrtc.org9dba5252013-08-05 20:36:57 +00002163 LOG(LS_WARNING) << "Try to remove stream with ssrc " << ssrc
2164 << " which doesn't exist.";
2165 return false;
2166 }
2167
mallinath@webrtc.org67ee6b92014-02-03 16:57:16 +00002168 int channel = it->second->channel();
wu@webrtc.org9dba5252013-08-05 20:36:57 +00002169 ChangeSend(channel, SEND_NOTHING);
2170
solenberg7add0582015-11-20 09:59:34 -08002171 // Clean up and delete the send stream+channel.
solenberg0a617e22015-10-20 15:49:38 -07002172 LOG(LS_INFO) << "Removing audio send stream " << ssrc
2173 << " with VoiceEngine channel #" << channel << ".";
solenberg7add0582015-11-20 09:59:34 -08002174 delete it->second;
2175 send_streams_.erase(it);
2176 if (!DeleteVoEChannel(channel)) {
solenberg0a617e22015-10-20 15:49:38 -07002177 return false;
wu@webrtc.org9dba5252013-08-05 20:36:57 +00002178 }
solenbergc96df772015-10-21 13:01:53 -07002179 if (send_streams_.empty()) {
wu@webrtc.org9dba5252013-08-05 20:36:57 +00002180 ChangeSend(SEND_NOTHING);
solenberg0a617e22015-10-20 15:49:38 -07002181 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002182 return true;
2183}
2184
2185bool WebRtcVoiceMediaChannel::AddRecvStream(const StreamParams& sp) {
solenberg566ef242015-11-06 15:34:49 -08002186 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
solenbergd97ec302015-10-07 01:40:33 -07002187 LOG(LS_INFO) << "AddRecvStream: " << sp.ToString();
2188
solenberg0b675462015-10-09 01:37:09 -07002189 if (!ValidateStreamParams(sp)) {
wu@webrtc.org78187522013-10-07 23:32:02 +00002190 return false;
2191 }
2192
solenberg7add0582015-11-20 09:59:34 -08002193 const uint32_t ssrc = sp.first_ssrc();
solenberg0b675462015-10-09 01:37:09 -07002194 if (ssrc == 0) {
2195 LOG(LS_WARNING) << "AddRecvStream with ssrc==0 is not supported.";
2196 return false;
2197 }
2198
solenberg1ac56142015-10-13 03:58:19 -07002199 // Remove the default receive stream if one had been created with this ssrc;
2200 // we'll recreate it then.
2201 if (IsDefaultRecvStream(ssrc)) {
2202 RemoveRecvStream(ssrc);
2203 }
solenberg0b675462015-10-09 01:37:09 -07002204
solenberg7add0582015-11-20 09:59:34 -08002205 if (GetReceiveChannelId(ssrc) != -1) {
henrike@webrtc.org1e09a712013-07-26 19:17:59 +00002206 LOG(LS_ERROR) << "Stream already exists with ssrc " << ssrc;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002207 return false;
2208 }
Fredrik Solenberg4b60c732015-05-07 14:07:48 +02002209
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002210 // Create a new channel for receiving audio data.
solenberg7add0582015-11-20 09:59:34 -08002211 const int channel = CreateVoEChannel();
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002212 if (channel == -1) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002213 return false;
2214 }
Minyue2013aec2015-05-13 14:14:42 +02002215
solenberg1ac56142015-10-13 03:58:19 -07002216 // Turn off all supported codecs.
solenberg7add0582015-11-20 09:59:34 -08002217 const int ncodecs = engine()->voe()->codec()->NumOfCodecs();
solenberg1ac56142015-10-13 03:58:19 -07002218 for (int i = 0; i < ncodecs; ++i) {
2219 webrtc::CodecInst voe_codec;
2220 if (engine()->voe()->codec()->GetCodec(i, voe_codec) != -1) {
2221 voe_codec.pltype = -1;
2222 if (engine()->voe()->codec()->SetRecPayloadType(
2223 channel, voe_codec) == -1) {
2224 LOG_RTCERR2(SetRecPayloadType, channel, ToString(voe_codec));
solenberg7add0582015-11-20 09:59:34 -08002225 DeleteVoEChannel(channel);
solenberg1ac56142015-10-13 03:58:19 -07002226 return false;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002227 }
2228 }
2229 }
2230
solenberg1ac56142015-10-13 03:58:19 -07002231 // Only enable those configured for this channel.
2232 for (const auto& codec : recv_codecs_) {
2233 webrtc::CodecInst voe_codec;
2234 if (engine()->FindWebRtcCodec(codec, &voe_codec)) {
2235 voe_codec.pltype = codec.id;
2236 if (engine()->voe()->codec()->SetRecPayloadType(
2237 channel, voe_codec) == -1) {
2238 LOG_RTCERR2(SetRecPayloadType, channel, ToString(voe_codec));
solenberg7add0582015-11-20 09:59:34 -08002239 DeleteVoEChannel(channel);
solenberg1ac56142015-10-13 03:58:19 -07002240 return false;
2241 }
henrike@webrtc.org1e09a712013-07-26 19:17:59 +00002242 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002243 }
solenberg8fb30c32015-10-13 03:06:58 -07002244
solenberg7add0582015-11-20 09:59:34 -08002245 const int send_channel = GetSendChannelId(receiver_reports_ssrc_);
2246 if (send_channel != -1) {
2247 // Associate receive channel with first send channel (so the receive channel
2248 // can obtain RTT from the send channel)
2249 engine()->voe()->base()->AssociateSendChannel(channel, send_channel);
2250 LOG(LS_INFO) << "VoiceEngine channel #" << channel
2251 << " is associated with channel #" << send_channel << ".";
buildbot@webrtc.org150835e2014-05-06 15:54:38 +00002252 }
2253
solenberg7add0582015-11-20 09:59:34 -08002254 recv_streams_.insert(std::make_pair(ssrc, new WebRtcAudioReceiveStream(
2255 channel, ssrc, receiver_reports_ssrc_,
2256 options_.combined_audio_video_bwe.value_or(false), sp.sync_label,
2257 recv_rtp_extensions_, call_)));
2258
2259 SetNack(channel, nack_enabled_);
solenberg1ac56142015-10-13 03:58:19 -07002260 SetPlayout(channel, playout_);
solenberg7add0582015-11-20 09:59:34 -08002261
solenberg1ac56142015-10-13 03:58:19 -07002262 return true;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002263}
2264
Peter Boström0c4e06b2015-10-07 12:23:21 +02002265bool WebRtcVoiceMediaChannel::RemoveRecvStream(uint32_t ssrc) {
solenberg566ef242015-11-06 15:34:49 -08002266 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
solenbergd97ec302015-10-07 01:40:33 -07002267 LOG(LS_INFO) << "RemoveRecvStream: " << ssrc;
2268
solenberg7add0582015-11-20 09:59:34 -08002269 const auto it = recv_streams_.find(ssrc);
2270 if (it == recv_streams_.end()) {
wu@webrtc.org9dba5252013-08-05 20:36:57 +00002271 LOG(LS_WARNING) << "Try to remove stream with ssrc " << ssrc
2272 << " which doesn't exist.";
henrike@webrtc.org1e09a712013-07-26 19:17:59 +00002273 return false;
wu@webrtc.org9dba5252013-08-05 20:36:57 +00002274 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002275
solenberg1ac56142015-10-13 03:58:19 -07002276 // Deregister default channel, if that's the one being destroyed.
2277 if (IsDefaultRecvStream(ssrc)) {
2278 default_recv_ssrc_ = -1;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002279 }
henrike@webrtc.org1e09a712013-07-26 19:17:59 +00002280
solenberg7add0582015-11-20 09:59:34 -08002281 const int channel = it->second->channel();
2282
2283 // Clean up and delete the receive stream+channel.
2284 LOG(LS_INFO) << "Removing audio receive stream " << ssrc
mallinath@webrtc.org67ee6b92014-02-03 16:57:16 +00002285 << " with VoiceEngine channel #" << channel << ".";
solenberg7add0582015-11-20 09:59:34 -08002286 delete it->second;
2287 recv_streams_.erase(it);
2288 return DeleteVoEChannel(channel);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002289}
2290
Peter Boström0c4e06b2015-10-07 12:23:21 +02002291bool WebRtcVoiceMediaChannel::SetLocalRenderer(uint32_t ssrc,
henrike@webrtc.org1e09a712013-07-26 19:17:59 +00002292 AudioRenderer* renderer) {
solenbergc96df772015-10-21 13:01:53 -07002293 auto it = send_streams_.find(ssrc);
2294 if (it == send_streams_.end()) {
wu@webrtc.org9dba5252013-08-05 20:36:57 +00002295 if (renderer) {
2296 // Return an error if trying to set a valid renderer with an invalid ssrc.
2297 LOG(LS_ERROR) << "SetLocalRenderer failed with ssrc "<< ssrc;
2298 return false;
2299 }
2300
2301 // The channel likely has gone away, do nothing.
henrike@webrtc.org1e09a712013-07-26 19:17:59 +00002302 return true;
henrike@webrtc.org1e09a712013-07-26 19:17:59 +00002303 }
2304
solenberg1ac56142015-10-13 03:58:19 -07002305 if (renderer) {
mallinath@webrtc.org67ee6b92014-02-03 16:57:16 +00002306 it->second->Start(renderer);
solenberg1ac56142015-10-13 03:58:19 -07002307 } else {
mallinath@webrtc.org67ee6b92014-02-03 16:57:16 +00002308 it->second->Stop();
solenberg1ac56142015-10-13 03:58:19 -07002309 }
henrike@webrtc.org1e09a712013-07-26 19:17:59 +00002310
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002311 return true;
2312}
2313
2314bool WebRtcVoiceMediaChannel::GetActiveStreams(
2315 AudioInfo::StreamList* actives) {
solenberg566ef242015-11-06 15:34:49 -08002316 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002317 actives->clear();
solenberg7add0582015-11-20 09:59:34 -08002318 for (const auto& ch : recv_streams_) {
Fredrik Solenbergaf9fb212015-08-26 10:45:53 +02002319 int level = GetOutputLevel(ch.second->channel());
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002320 if (level > 0) {
Fredrik Solenbergaf9fb212015-08-26 10:45:53 +02002321 actives->push_back(std::make_pair(ch.first, level));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002322 }
2323 }
2324 return true;
2325}
2326
2327int WebRtcVoiceMediaChannel::GetOutputLevel() {
solenberg566ef242015-11-06 15:34:49 -08002328 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
solenberg1ac56142015-10-13 03:58:19 -07002329 int highest = 0;
solenberg7add0582015-11-20 09:59:34 -08002330 for (const auto& ch : recv_streams_) {
solenberg8fb30c32015-10-13 03:06:58 -07002331 highest = std::max(GetOutputLevel(ch.second->channel()), highest);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002332 }
2333 return highest;
2334}
2335
2336int WebRtcVoiceMediaChannel::GetTimeSinceLastTyping() {
2337 int ret;
2338 if (engine()->voe()->processing()->TimeSinceLastTyping(ret) == -1) {
2339 // In case of error, log the info and continue
2340 LOG_RTCERR0(TimeSinceLastTyping);
2341 ret = -1;
2342 } else {
2343 ret *= 1000; // We return ms, webrtc returns seconds.
2344 }
2345 return ret;
2346}
2347
2348void WebRtcVoiceMediaChannel::SetTypingDetectionParameters(int time_window,
2349 int cost_per_typing, int reporting_threshold, int penalty_decay,
2350 int type_event_delay) {
2351 if (engine()->voe()->processing()->SetTypingDetectionParameters(
2352 time_window, cost_per_typing,
2353 reporting_threshold, penalty_decay, type_event_delay) == -1) {
2354 // In case of error, log the info and continue
2355 LOG_RTCERR5(SetTypingDetectionParameters, time_window,
2356 cost_per_typing, reporting_threshold, penalty_decay,
2357 type_event_delay);
2358 }
2359}
2360
solenberg4bac9c52015-10-09 02:32:53 -07002361bool WebRtcVoiceMediaChannel::SetOutputVolume(uint32_t ssrc, double volume) {
solenberg566ef242015-11-06 15:34:49 -08002362 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
solenberg1ac56142015-10-13 03:58:19 -07002363 if (ssrc == 0) {
2364 default_recv_volume_ = volume;
2365 if (default_recv_ssrc_ == -1) {
2366 return true;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002367 }
solenberg1ac56142015-10-13 03:58:19 -07002368 ssrc = static_cast<uint32_t>(default_recv_ssrc_);
2369 }
2370 int ch_id = GetReceiveChannelId(ssrc);
2371 if (ch_id < 0) {
2372 LOG(LS_WARNING) << "Cannot find channel for ssrc:" << ssrc;
2373 return false;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002374 }
2375
solenberg1ac56142015-10-13 03:58:19 -07002376 if (-1 == engine()->voe()->volume()->SetChannelOutputVolumeScaling(ch_id,
2377 volume)) {
2378 LOG_RTCERR2(SetChannelOutputVolumeScaling, ch_id, volume);
2379 return false;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002380 }
solenberg1ac56142015-10-13 03:58:19 -07002381 LOG(LS_INFO) << "SetOutputVolume to " << volume
2382 << " for channel " << ch_id << " and ssrc " << ssrc;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002383 return true;
2384}
2385
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002386bool WebRtcVoiceMediaChannel::CanInsertDtmf() {
2387 return dtmf_allowed_;
2388}
2389
Peter Boström0c4e06b2015-10-07 12:23:21 +02002390bool WebRtcVoiceMediaChannel::InsertDtmf(uint32_t ssrc,
2391 int event,
2392 int duration,
2393 int flags) {
solenberg566ef242015-11-06 15:34:49 -08002394 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002395 if (!dtmf_allowed_) {
2396 return false;
2397 }
2398
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002399 // Send the event.
2400 if (flags & cricket::DF_SEND) {
wu@webrtc.orgcadf9042013-08-30 21:24:16 +00002401 int channel = -1;
2402 if (ssrc == 0) {
solenbergc96df772015-10-21 13:01:53 -07002403 if (send_streams_.size() > 0) {
2404 channel = send_streams_.begin()->second->channel();
wu@webrtc.orgcadf9042013-08-30 21:24:16 +00002405 }
2406 } else {
solenbergd97ec302015-10-07 01:40:33 -07002407 channel = GetSendChannelId(ssrc);
wu@webrtc.orgcadf9042013-08-30 21:24:16 +00002408 }
wu@webrtc.org9dba5252013-08-05 20:36:57 +00002409 if (channel == -1) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002410 LOG(LS_WARNING) << "InsertDtmf - The specified ssrc "
2411 << ssrc << " is not in use.";
2412 return false;
2413 }
2414 // Send DTMF using out-of-band DTMF. ("true", as 3rd arg)
wu@webrtc.org9dba5252013-08-05 20:36:57 +00002415 if (engine()->voe()->dtmf()->SendTelephoneEvent(
2416 channel, event, true, duration) == -1) {
2417 LOG_RTCERR4(SendTelephoneEvent, channel, event, true, duration);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002418 return false;
2419 }
2420 }
2421
2422 // Play the event.
2423 if (flags & cricket::DF_PLAY) {
2424 // Play DTMF tone locally.
2425 if (engine()->voe()->dtmf()->PlayDtmfTone(event, duration) == -1) {
2426 LOG_RTCERR2(PlayDtmfTone, event, duration);
2427 return false;
2428 }
2429 }
2430
2431 return true;
2432}
2433
wu@webrtc.orga9890802013-12-13 00:21:03 +00002434void WebRtcVoiceMediaChannel::OnPacketReceived(
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00002435 rtc::Buffer* packet, const rtc::PacketTime& packet_time) {
solenberg566ef242015-11-06 15:34:49 -08002436 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
Fredrik Solenberg4b60c732015-05-07 14:07:48 +02002437
solenberg1ac56142015-10-13 03:58:19 -07002438 uint32_t ssrc = 0;
2439 if (!GetRtpSsrc(packet->data(), packet->size(), &ssrc)) {
2440 return;
2441 }
2442
solenberg7e63ef02015-11-20 00:19:43 -08002443 // If we don't have a default channel, and the SSRC is unknown, create a
2444 // default channel.
2445 if (default_recv_ssrc_ == -1 && GetReceiveChannelId(ssrc) == -1) {
solenberg1ac56142015-10-13 03:58:19 -07002446 StreamParams sp;
2447 sp.ssrcs.push_back(ssrc);
2448 LOG(LS_INFO) << "Creating default receive stream for SSRC=" << ssrc << ".";
2449 if (!AddRecvStream(sp)) {
2450 LOG(LS_WARNING) << "Could not create default receive stream.";
2451 return;
2452 }
2453 default_recv_ssrc_ = ssrc;
2454 SetOutputVolume(default_recv_ssrc_, default_recv_volume_);
2455 }
2456
2457 // Forward packet to Call. If the SSRC is unknown we'll return after this.
Fredrik Solenberg709ed672015-09-15 12:26:33 +02002458 const webrtc::PacketTime webrtc_packet_time(packet_time.timestamp,
2459 packet_time.not_before);
solenberg1ac56142015-10-13 03:58:19 -07002460 webrtc::PacketReceiver::DeliveryStatus delivery_result =
2461 call_->Receiver()->DeliverPacket(webrtc::MediaType::AUDIO,
2462 reinterpret_cast<const uint8_t*>(packet->data()), packet->size(),
2463 webrtc_packet_time);
2464 if (webrtc::PacketReceiver::DELIVERY_OK != delivery_result) {
solenberg7e63ef02015-11-20 00:19:43 -08002465 // If the SSRC is unknown here, route it to the default channel, if we have
2466 // one. See: https://bugs.chromium.org/p/webrtc/issues/detail?id=5208
2467 if (default_recv_ssrc_ == -1) {
2468 return;
2469 } else {
2470 ssrc = default_recv_ssrc_;
2471 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002472 }
2473
solenberg1ac56142015-10-13 03:58:19 -07002474 // Find the channel to send this packet to. It must exist since webrtc::Call
2475 // was able to demux the packet.
2476 int channel = GetReceiveChannelId(ssrc);
2477 RTC_DCHECK(channel != -1);
2478
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002479 // Pass it off to the decoder.
henrike@webrtc.org28654cb2013-07-22 21:07:49 +00002480 engine()->voe()->network()->ReceivedRTPPacket(
solenberg1ac56142015-10-13 03:58:19 -07002481 channel, packet->data(), packet->size(), webrtc_packet_time);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002482}
2483
wu@webrtc.orga9890802013-12-13 00:21:03 +00002484void WebRtcVoiceMediaChannel::OnRtcpReceived(
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00002485 rtc::Buffer* packet, const rtc::PacketTime& packet_time) {
solenberg566ef242015-11-06 15:34:49 -08002486 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
Fredrik Solenberg4b60c732015-05-07 14:07:48 +02002487
Fredrik Solenberg709ed672015-09-15 12:26:33 +02002488 // Forward packet to Call as well.
2489 const webrtc::PacketTime webrtc_packet_time(packet_time.timestamp,
2490 packet_time.not_before);
2491 call_->Receiver()->DeliverPacket(webrtc::MediaType::AUDIO,
2492 reinterpret_cast<const uint8_t*>(packet->data()), packet->size(),
2493 webrtc_packet_time);
Fredrik Solenberg4b60c732015-05-07 14:07:48 +02002494
wu@webrtc.org9dba5252013-08-05 20:36:57 +00002495 // Sending channels need all RTCP packets with feedback information.
2496 // Even sender reports can contain attached report blocks.
2497 // Receiving channels need sender reports in order to create
2498 // correct receiver reports.
2499 int type = 0;
kwiberg@webrtc.orgeebcab52015-03-24 09:19:06 +00002500 if (!GetRtcpType(packet->data(), packet->size(), &type)) {
wu@webrtc.org9dba5252013-08-05 20:36:57 +00002501 LOG(LS_WARNING) << "Failed to parse type from received RTCP packet";
2502 return;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002503 }
2504
solenberg0b675462015-10-09 01:37:09 -07002505 // If it is a sender report, find the receive channel that is listening.
wu@webrtc.org9dba5252013-08-05 20:36:57 +00002506 if (type == kRtcpTypeSR) {
solenberg0b675462015-10-09 01:37:09 -07002507 uint32_t ssrc = 0;
2508 if (!GetRtcpSsrc(packet->data(), packet->size(), &ssrc)) {
2509 return;
2510 }
2511 int recv_channel_id = GetReceiveChannelId(ssrc);
2512 if (recv_channel_id != -1) {
wu@webrtc.org9dba5252013-08-05 20:36:57 +00002513 engine()->voe()->network()->ReceivedRTCPPacket(
solenberg0b675462015-10-09 01:37:09 -07002514 recv_channel_id, packet->data(), packet->size());
wu@webrtc.org9dba5252013-08-05 20:36:57 +00002515 }
2516 }
2517
2518 // SR may continue RR and any RR entry may correspond to any one of the send
2519 // channels. So all RTCP packets must be forwarded all send channels. VoE
2520 // will filter out RR internally.
solenbergc96df772015-10-21 13:01:53 -07002521 for (const auto& ch : send_streams_) {
wu@webrtc.org9dba5252013-08-05 20:36:57 +00002522 engine()->voe()->network()->ReceivedRTCPPacket(
Fredrik Solenbergaf9fb212015-08-26 10:45:53 +02002523 ch.second->channel(), packet->data(), packet->size());
wu@webrtc.org9dba5252013-08-05 20:36:57 +00002524 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002525}
2526
Peter Boström0c4e06b2015-10-07 12:23:21 +02002527bool WebRtcVoiceMediaChannel::MuteStream(uint32_t ssrc, bool muted) {
solenberg566ef242015-11-06 15:34:49 -08002528 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
solenberg0a617e22015-10-20 15:49:38 -07002529 int channel = GetSendChannelId(ssrc);
wu@webrtc.org9dba5252013-08-05 20:36:57 +00002530 if (channel == -1) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002531 LOG(LS_WARNING) << "The specified ssrc " << ssrc << " is not in use.";
2532 return false;
2533 }
wu@webrtc.org9dba5252013-08-05 20:36:57 +00002534 if (engine()->voe()->volume()->SetInputMute(channel, muted) == -1) {
2535 LOG_RTCERR2(SetInputMute, channel, muted);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002536 return false;
2537 }
buildbot@webrtc.org6b21b712014-07-31 15:08:53 +00002538 // We set the AGC to mute state only when all the channels are muted.
2539 // This implementation is not ideal, instead we should signal the AGC when
2540 // the mic channel is muted/unmuted. We can't do it today because there
2541 // is no good way to know which stream is mapping to the mic channel.
2542 bool all_muted = muted;
solenbergc96df772015-10-21 13:01:53 -07002543 for (const auto& ch : send_streams_) {
Fredrik Solenbergaf9fb212015-08-26 10:45:53 +02002544 if (!all_muted) {
2545 break;
2546 }
2547 if (engine()->voe()->volume()->GetInputMute(ch.second->channel(),
buildbot@webrtc.org6b21b712014-07-31 15:08:53 +00002548 all_muted)) {
Fredrik Solenbergaf9fb212015-08-26 10:45:53 +02002549 LOG_RTCERR1(GetInputMute, ch.second->channel());
buildbot@webrtc.org6b21b712014-07-31 15:08:53 +00002550 return false;
2551 }
2552 }
2553
2554 webrtc::AudioProcessing* ap = engine()->voe()->base()->audio_processing();
solenberg0a617e22015-10-20 15:49:38 -07002555 if (ap) {
buildbot@webrtc.org6b21b712014-07-31 15:08:53 +00002556 ap->set_output_will_be_muted(all_muted);
solenberg0a617e22015-10-20 15:49:38 -07002557 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002558 return true;
2559}
2560
minyue@webrtc.org26236952014-10-29 02:27:08 +00002561// TODO(minyue): SetMaxSendBandwidth() is subject to be renamed to
2562// SetMaxSendBitrate() in future.
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00002563bool WebRtcVoiceMediaChannel::SetMaxSendBandwidth(int bps) {
minyue@webrtc.org26236952014-10-29 02:27:08 +00002564 LOG(LS_INFO) << "WebRtcVoiceMediaChannel::SetMaxSendBandwidth.";
minyue@webrtc.org26236952014-10-29 02:27:08 +00002565 return SetSendBitrateInternal(bps);
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00002566}
2567
minyue@webrtc.org26236952014-10-29 02:27:08 +00002568bool WebRtcVoiceMediaChannel::SetSendBitrateInternal(int bps) {
2569 LOG(LS_INFO) << "WebRtcVoiceMediaChannel::SetSendBitrateInternal.";
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00002570
minyue@webrtc.org26236952014-10-29 02:27:08 +00002571 send_bitrate_setting_ = true;
2572 send_bitrate_bps_ = bps;
wu@webrtc.org1d1ffc92013-10-16 18:12:02 +00002573
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002574 if (!send_codec_) {
wu@webrtc.org1d1ffc92013-10-16 18:12:02 +00002575 LOG(LS_INFO) << "The send codec has not been set up yet. "
minyue@webrtc.org26236952014-10-29 02:27:08 +00002576 << "The send bitrate setting will be applied later.";
wu@webrtc.org1d1ffc92013-10-16 18:12:02 +00002577 return true;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002578 }
2579
minyue@webrtc.org26236952014-10-29 02:27:08 +00002580 // Bitrate is auto by default.
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00002581 // TODO(bemasc): Fix this so that if SetMaxSendBandwidth(50) is followed by
2582 // SetMaxSendBandwith(0), the second call removes the previous limit.
2583 if (bps <= 0)
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002584 return true;
2585
2586 webrtc::CodecInst codec = *send_codec_;
2587 bool is_multi_rate = IsCodecMultiRate(codec);
2588
2589 if (is_multi_rate) {
2590 // If codec is multi-rate then just set the bitrate.
2591 codec.rate = bps;
solenbergc96df772015-10-21 13:01:53 -07002592 for (const auto& ch : send_streams_) {
solenberg0a617e22015-10-20 15:49:38 -07002593 if (!SetSendCodec(ch.second->channel(), codec)) {
2594 LOG(LS_INFO) << "Failed to set codec " << codec.plname
2595 << " to bitrate " << bps << " bps.";
2596 return false;
2597 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002598 }
2599 return true;
2600 } else {
2601 // If codec is not multi-rate and |bps| is less than the fixed bitrate
2602 // then fail. If codec is not multi-rate and |bps| exceeds or equal the
2603 // fixed bitrate then ignore.
2604 if (bps < codec.rate) {
2605 LOG(LS_INFO) << "Failed to set codec " << codec.plname
2606 << " to bitrate " << bps << " bps"
2607 << ", requires at least " << codec.rate << " bps.";
2608 return false;
2609 }
2610 return true;
2611 }
2612}
2613
2614bool WebRtcVoiceMediaChannel::GetStats(VoiceMediaInfo* info) {
solenberg566ef242015-11-06 15:34:49 -08002615 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
solenberg85a04962015-10-27 03:35:21 -07002616 RTC_DCHECK(info);
solenbergd97ec302015-10-07 01:40:33 -07002617
solenberg85a04962015-10-27 03:35:21 -07002618 // Get SSRC and stats for each sender.
2619 RTC_DCHECK(info->senders.size() == 0);
2620 for (const auto& stream : send_streams_) {
2621 webrtc::AudioSendStream::Stats stats = stream.second->GetStats();
wu@webrtc.org9dba5252013-08-05 20:36:57 +00002622 VoiceSenderInfo sinfo;
solenberg85a04962015-10-27 03:35:21 -07002623 sinfo.add_ssrc(stats.local_ssrc);
2624 sinfo.bytes_sent = stats.bytes_sent;
2625 sinfo.packets_sent = stats.packets_sent;
2626 sinfo.packets_lost = stats.packets_lost;
2627 sinfo.fraction_lost = stats.fraction_lost;
2628 sinfo.codec_name = stats.codec_name;
2629 sinfo.ext_seqnum = stats.ext_seqnum;
2630 sinfo.jitter_ms = stats.jitter_ms;
2631 sinfo.rtt_ms = stats.rtt_ms;
2632 sinfo.audio_level = stats.audio_level;
2633 sinfo.aec_quality_min = stats.aec_quality_min;
2634 sinfo.echo_delay_median_ms = stats.echo_delay_median_ms;
2635 sinfo.echo_delay_std_ms = stats.echo_delay_std_ms;
2636 sinfo.echo_return_loss = stats.echo_return_loss;
2637 sinfo.echo_return_loss_enhancement = stats.echo_return_loss_enhancement;
solenberg566ef242015-11-06 15:34:49 -08002638 sinfo.typing_noise_detected =
2639 (send_ == SEND_NOTHING ? false : stats.typing_noise_detected);
wu@webrtc.org9dba5252013-08-05 20:36:57 +00002640 info->senders.push_back(sinfo);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002641 }
2642
solenberg85a04962015-10-27 03:35:21 -07002643 // Get SSRC and stats for each receiver.
2644 RTC_DCHECK(info->receivers.size() == 0);
solenberg7add0582015-11-20 09:59:34 -08002645 for (const auto& stream : recv_streams_) {
Fredrik Solenberg4f4ec0a2015-10-22 10:49:27 +02002646 webrtc::AudioReceiveStream::Stats stats = stream.second->GetStats();
2647 VoiceReceiverInfo rinfo;
2648 rinfo.add_ssrc(stats.remote_ssrc);
2649 rinfo.bytes_rcvd = stats.bytes_rcvd;
2650 rinfo.packets_rcvd = stats.packets_rcvd;
2651 rinfo.packets_lost = stats.packets_lost;
2652 rinfo.fraction_lost = stats.fraction_lost;
2653 rinfo.codec_name = stats.codec_name;
2654 rinfo.ext_seqnum = stats.ext_seqnum;
2655 rinfo.jitter_ms = stats.jitter_ms;
2656 rinfo.jitter_buffer_ms = stats.jitter_buffer_ms;
2657 rinfo.jitter_buffer_preferred_ms = stats.jitter_buffer_preferred_ms;
2658 rinfo.delay_estimate_ms = stats.delay_estimate_ms;
2659 rinfo.audio_level = stats.audio_level;
2660 rinfo.expand_rate = stats.expand_rate;
2661 rinfo.speech_expand_rate = stats.speech_expand_rate;
2662 rinfo.secondary_decoded_rate = stats.secondary_decoded_rate;
2663 rinfo.accelerate_rate = stats.accelerate_rate;
2664 rinfo.preemptive_expand_rate = stats.preemptive_expand_rate;
2665 rinfo.decoding_calls_to_silence_generator =
2666 stats.decoding_calls_to_silence_generator;
2667 rinfo.decoding_calls_to_neteq = stats.decoding_calls_to_neteq;
2668 rinfo.decoding_normal = stats.decoding_normal;
2669 rinfo.decoding_plc = stats.decoding_plc;
2670 rinfo.decoding_cng = stats.decoding_cng;
2671 rinfo.decoding_plc_cng = stats.decoding_plc_cng;
2672 rinfo.capture_start_ntp_time_ms = stats.capture_start_ntp_time_ms;
2673 info->receivers.push_back(rinfo);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002674 }
2675
2676 return true;
2677}
2678
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002679int WebRtcVoiceMediaChannel::GetOutputLevel(int channel) {
solenbergd97ec302015-10-07 01:40:33 -07002680 unsigned int ulevel = 0;
2681 int ret = engine()->voe()->volume()->GetSpeechOutputLevel(channel, ulevel);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002682 return (ret == 0) ? static_cast<int>(ulevel) : -1;
2683}
2684
Peter Boström0c4e06b2015-10-07 12:23:21 +02002685int WebRtcVoiceMediaChannel::GetReceiveChannelId(uint32_t ssrc) const {
solenberg566ef242015-11-06 15:34:49 -08002686 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
solenberg7add0582015-11-20 09:59:34 -08002687 const auto it = recv_streams_.find(ssrc);
2688 if (it != recv_streams_.end()) {
mallinath@webrtc.org67ee6b92014-02-03 16:57:16 +00002689 return it->second->channel();
solenberg8fb30c32015-10-13 03:06:58 -07002690 }
solenberg1ac56142015-10-13 03:58:19 -07002691 return -1;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002692}
2693
Peter Boström0c4e06b2015-10-07 12:23:21 +02002694int WebRtcVoiceMediaChannel::GetSendChannelId(uint32_t ssrc) const {
solenberg566ef242015-11-06 15:34:49 -08002695 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
solenbergc96df772015-10-21 13:01:53 -07002696 const auto it = send_streams_.find(ssrc);
2697 if (it != send_streams_.end()) {
mallinath@webrtc.org67ee6b92014-02-03 16:57:16 +00002698 return it->second->channel();
solenberg8fb30c32015-10-13 03:06:58 -07002699 }
wu@webrtc.org9dba5252013-08-05 20:36:57 +00002700 return -1;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002701}
2702
2703bool WebRtcVoiceMediaChannel::GetRedSendCodec(const AudioCodec& red_codec,
2704 const std::vector<AudioCodec>& all_codecs, webrtc::CodecInst* send_codec) {
2705 // Get the RED encodings from the parameter with no name. This may
2706 // change based on what is discussed on the Jingle list.
2707 // The encoding parameter is of the form "a/b"; we only support where
2708 // a == b. Verify this and parse out the value into red_pt.
2709 // If the parameter value is absent (as it will be until we wire up the
2710 // signaling of this message), use the second codec specified (i.e. the
2711 // one after "red") as the encoding parameter.
2712 int red_pt = -1;
2713 std::string red_params;
2714 CodecParameterMap::const_iterator it = red_codec.params.find("");
2715 if (it != red_codec.params.end()) {
2716 red_params = it->second;
2717 std::vector<std::string> red_pts;
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00002718 if (rtc::split(red_params, '/', &red_pts) != 2 ||
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002719 red_pts[0] != red_pts[1] ||
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00002720 !rtc::FromString(red_pts[0], &red_pt)) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002721 LOG(LS_WARNING) << "RED params " << red_params << " not supported.";
2722 return false;
2723 }
2724 } else if (red_codec.params.empty()) {
2725 LOG(LS_WARNING) << "RED params not present, using defaults";
2726 if (all_codecs.size() > 1) {
2727 red_pt = all_codecs[1].id;
2728 }
2729 }
2730
2731 // Try to find red_pt in |codecs|.
Fredrik Solenbergaf9fb212015-08-26 10:45:53 +02002732 for (const AudioCodec& codec : all_codecs) {
2733 if (codec.id == red_pt) {
2734 // If we find the right codec, that will be the codec we pass to
2735 // SetSendCodec, with the desired payload type.
2736 if (engine()->FindWebRtcCodec(codec, send_codec)) {
2737 return true;
2738 } else {
2739 break;
2740 }
2741 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002742 }
Fredrik Solenbergaf9fb212015-08-26 10:45:53 +02002743 LOG(LS_WARNING) << "RED params " << red_params << " are invalid.";
2744 return false;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002745}
2746
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002747bool WebRtcVoiceMediaChannel::SetPlayout(int channel, bool playout) {
2748 if (playout) {
2749 LOG(LS_INFO) << "Starting playout for channel #" << channel;
2750 if (engine()->voe()->base()->StartPlayout(channel) == -1) {
2751 LOG_RTCERR1(StartPlayout, channel);
2752 return false;
2753 }
2754 } else {
2755 LOG(LS_INFO) << "Stopping playout for channel #" << channel;
2756 engine()->voe()->base()->StopPlayout(channel);
2757 }
2758 return true;
2759}
2760
Fredrik Solenbergaf9fb212015-08-26 10:45:53 +02002761bool WebRtcVoiceMediaChannel::SetRecvCodecsInternal(
2762 const std::vector<AudioCodec>& new_codecs) {
solenberg566ef242015-11-06 15:34:49 -08002763 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
Fredrik Solenbergaf9fb212015-08-26 10:45:53 +02002764 for (const AudioCodec& codec : new_codecs) {
2765 webrtc::CodecInst voe_codec;
2766 if (engine()->FindWebRtcCodec(codec, &voe_codec)) {
2767 LOG(LS_INFO) << ToString(codec);
2768 voe_codec.pltype = codec.id;
solenberg7add0582015-11-20 09:59:34 -08002769 for (const auto& ch : recv_streams_) {
Fredrik Solenbergaf9fb212015-08-26 10:45:53 +02002770 if (engine()->voe()->codec()->SetRecPayloadType(
2771 ch.second->channel(), voe_codec) == -1) {
2772 LOG_RTCERR2(SetRecPayloadType, ch.second->channel(),
2773 ToString(voe_codec));
2774 return false;
2775 }
2776 }
2777 } else {
2778 LOG(LS_WARNING) << "Unknown codec " << ToString(codec);
2779 return false;
2780 }
2781 }
2782 return true;
2783}
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002784} // namespace cricket
2785
2786#endif // HAVE_WEBRTC_VOICE