blob: eed1195975704aea781d564eefca5a9953b164d4 [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"
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +000046#include "webrtc/base/base64.h"
47#include "webrtc/base/byteorder.h"
48#include "webrtc/base/common.h"
49#include "webrtc/base/helpers.h"
50#include "webrtc/base/logging.h"
51#include "webrtc/base/stringencode.h"
52#include "webrtc/base/stringutils.h"
ivoc112a3d82015-10-16 02:22:18 -070053#include "webrtc/call/rtc_event_log.h"
mallinath@webrtc.orga27be8e2013-09-27 23:04:10 +000054#include "webrtc/common.h"
henrike@webrtc.org28e20752013-07-10 00:45:36 +000055#include "webrtc/modules/audio_processing/include/audio_processing.h"
stefanc1aeaf02015-10-15 07:26:07 -070056#include "webrtc/system_wrappers/interface/field_trial.h"
henrike@webrtc.org28e20752013-07-10 00:45:36 +000057
henrike@webrtc.org28e20752013-07-10 00:45:36 +000058namespace cricket {
solenbergd97ec302015-10-07 01:40:33 -070059namespace {
henrike@webrtc.org28e20752013-07-10 00:45:36 +000060
solenbergd97ec302015-10-07 01:40:33 -070061const int kMaxNumPacketSize = 6;
henrike@webrtc.org28e20752013-07-10 00:45:36 +000062struct CodecPref {
63 const char* name;
64 int clockrate;
65 int channels;
66 int payload_type;
67 bool is_multi_rate;
Brave Yao5225dd82015-03-26 07:39:19 +080068 int packet_sizes_ms[kMaxNumPacketSize];
henrike@webrtc.org28e20752013-07-10 00:45:36 +000069};
Brave Yao5225dd82015-03-26 07:39:19 +080070// Note: keep the supported packet sizes in ascending order.
solenbergd97ec302015-10-07 01:40:33 -070071const CodecPref kCodecPrefs[] = {
Brave Yao5225dd82015-03-26 07:39:19 +080072 { kOpusCodecName, 48000, 2, 111, true, { 10, 20, 40, 60 } },
73 { kIsacCodecName, 16000, 1, 103, true, { 30, 60 } },
74 { kIsacCodecName, 32000, 1, 104, true, { 30 } },
henrik.lundin@webrtc.org8038d422014-11-11 08:38:24 +000075 // G722 should be advertised as 8000 Hz because of the RFC "bug".
Brave Yao5225dd82015-03-26 07:39:19 +080076 { kG722CodecName, 8000, 1, 9, false, { 10, 20, 30, 40, 50, 60 } },
77 { kIlbcCodecName, 8000, 1, 102, false, { 20, 30, 40, 60 } },
78 { kPcmuCodecName, 8000, 1, 0, false, { 10, 20, 30, 40, 50, 60 } },
79 { kPcmaCodecName, 8000, 1, 8, false, { 10, 20, 30, 40, 50, 60 } },
Brave Yao5225dd82015-03-26 07:39:19 +080080 { kCnCodecName, 32000, 1, 106, false, { } },
81 { kCnCodecName, 16000, 1, 105, false, { } },
82 { kCnCodecName, 8000, 1, 13, false, { } },
83 { kRedCodecName, 8000, 1, 127, false, { } },
84 { kDtmfCodecName, 8000, 1, 126, false, { } },
henrike@webrtc.org28e20752013-07-10 00:45:36 +000085};
86
87// For Linux/Mac, using the default device is done by specifying index 0 for
88// VoE 4.0 and not -1 (which was the case for VoE 3.5).
89//
90// On Windows Vista and newer, Microsoft introduced the concept of "Default
91// Communications Device". This means that there are two types of default
92// devices (old Wave Audio style default and Default Communications Device).
93//
94// On Windows systems which only support Wave Audio style default, uses either
95// -1 or 0 to select the default device.
96//
97// On Windows systems which support both "Default Communication Device" and
98// old Wave Audio style default, use -1 for Default Communications Device and
99// -2 for Wave Audio style default, which is what we want to use for clips.
100// It's not clear yet whether the -2 index is handled properly on other OSes.
101
102#ifdef WIN32
solenbergd97ec302015-10-07 01:40:33 -0700103const int kDefaultAudioDeviceId = -1;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000104#else
solenbergd97ec302015-10-07 01:40:33 -0700105const int kDefaultAudioDeviceId = 0;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000106#endif
107
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000108// Parameter used for NACK.
109// This value is equivalent to 5 seconds of audio data at 20 ms per packet.
solenbergd97ec302015-10-07 01:40:33 -0700110const int kNackMaxPackets = 250;
minyue@webrtc.org2dc6f312014-10-31 05:33:10 +0000111
112// Codec parameters for Opus.
henrike@webrtc.org1e09a712013-07-26 19:17:59 +0000113// draft-spittka-payload-rtp-opus-03
minyue@webrtc.org2dc6f312014-10-31 05:33:10 +0000114
115// Recommended bitrates:
116// 8-12 kb/s for NB speech,
117// 16-20 kb/s for WB speech,
118// 28-40 kb/s for FB speech,
119// 48-64 kb/s for FB mono music, and
120// 64-128 kb/s for FB stereo music.
121// The current implementation applies the following values to mono signals,
122// and multiplies them by 2 for stereo.
solenbergd97ec302015-10-07 01:40:33 -0700123const int kOpusBitrateNb = 12000;
124const int kOpusBitrateWb = 20000;
125const int kOpusBitrateFb = 32000;
minyue@webrtc.org2dc6f312014-10-31 05:33:10 +0000126
henrike@webrtc.org1e09a712013-07-26 19:17:59 +0000127// Opus bitrate should be in the range between 6000 and 510000.
solenbergd97ec302015-10-07 01:40:33 -0700128const int kOpusMinBitrate = 6000;
129const int kOpusMaxBitrate = 510000;
buildbot@webrtc.org5d639b32014-09-10 07:57:12 +0000130
wu@webrtc.orgde305012013-10-31 15:40:38 +0000131// Default audio dscp value.
132// See http://tools.ietf.org/html/rfc2474 for details.
133// See also http://tools.ietf.org/html/draft-jennings-rtcweb-qos-00
solenbergd97ec302015-10-07 01:40:33 -0700134const rtc::DiffServCodePoint kAudioDscpValue = rtc::DSCP_EF;
henrike@webrtc.org1e09a712013-07-26 19:17:59 +0000135
sergeyu@chromium.orga59696b2013-09-13 23:48:58 +0000136// Ensure we open the file in a writeable path on ChromeOS and Android. This
137// workaround can be removed when it's possible to specify a filename for audio
138// option based AEC dumps.
henrike@webrtc.org1e09a712013-07-26 19:17:59 +0000139//
140// TODO(grunell): Use a string in the options instead of hardcoding it here
141// and let the embedder choose the filename (crbug.com/264223).
142//
sergeyu@chromium.orga59696b2013-09-13 23:48:58 +0000143// NOTE(ajm): Don't use hardcoded paths on platforms not explicitly specified
144// below.
145#if defined(CHROMEOS)
solenbergd97ec302015-10-07 01:40:33 -0700146const char kAecDumpByAudioOptionFilename[] = "/tmp/audio.aecdump";
sergeyu@chromium.orga59696b2013-09-13 23:48:58 +0000147#elif defined(ANDROID)
solenbergd97ec302015-10-07 01:40:33 -0700148const char kAecDumpByAudioOptionFilename[] = "/sdcard/audio.aecdump";
henrike@webrtc.org1e09a712013-07-26 19:17:59 +0000149#else
solenbergd97ec302015-10-07 01:40:33 -0700150const char kAecDumpByAudioOptionFilename[] = "audio.aecdump";
henrike@webrtc.org1e09a712013-07-26 19:17:59 +0000151#endif
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000152
solenberg0b675462015-10-09 01:37:09 -0700153bool ValidateStreamParams(const StreamParams& sp) {
154 if (sp.ssrcs.empty()) {
155 LOG(LS_ERROR) << "No SSRCs in stream parameters: " << sp.ToString();
156 return false;
157 }
158 if (sp.ssrcs.size() > 1) {
159 LOG(LS_ERROR) << "Multiple SSRCs in stream parameters: " << sp.ToString();
160 return false;
161 }
162 return true;
163}
164
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000165// Dumps an AudioCodec in RFC 2327-ish format.
solenbergd97ec302015-10-07 01:40:33 -0700166std::string ToString(const AudioCodec& codec) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000167 std::stringstream ss;
168 ss << codec.name << "/" << codec.clockrate << "/" << codec.channels
169 << " (" << codec.id << ")";
170 return ss.str();
171}
Minyue Li7100dcd2015-03-27 05:05:59 +0100172
solenbergd97ec302015-10-07 01:40:33 -0700173std::string ToString(const webrtc::CodecInst& codec) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000174 std::stringstream ss;
175 ss << codec.plname << "/" << codec.plfreq << "/" << codec.channels
176 << " (" << codec.pltype << ")";
177 return ss.str();
178}
179
solenbergd97ec302015-10-07 01:40:33 -0700180void LogMultiline(rtc::LoggingSeverity sev, char* text) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000181 const char* delim = "\r\n";
182 for (char* tok = strtok(text, delim); tok; tok = strtok(NULL, delim)) {
183 LOG_V(sev) << tok;
184 }
185}
186
187// Severity is an integer because it comes is assumed to be from command line.
solenbergd97ec302015-10-07 01:40:33 -0700188int SeverityToFilter(int severity) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000189 int filter = webrtc::kTraceNone;
190 switch (severity) {
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000191 case rtc::LS_VERBOSE:
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000192 filter |= webrtc::kTraceAll;
Henrik Kjellander7c027b62015-04-22 13:21:30 +0200193 FALLTHROUGH();
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000194 case rtc::LS_INFO:
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000195 filter |= (webrtc::kTraceStateInfo | webrtc::kTraceInfo);
Henrik Kjellander7c027b62015-04-22 13:21:30 +0200196 FALLTHROUGH();
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000197 case rtc::LS_WARNING:
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000198 filter |= (webrtc::kTraceTerseInfo | webrtc::kTraceWarning);
Henrik Kjellander7c027b62015-04-22 13:21:30 +0200199 FALLTHROUGH();
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000200 case rtc::LS_ERROR:
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000201 filter |= (webrtc::kTraceError | webrtc::kTraceCritical);
202 }
203 return filter;
204}
205
solenbergd97ec302015-10-07 01:40:33 -0700206bool IsCodec(const AudioCodec& codec, const char* ref_name) {
Minyue Li7100dcd2015-03-27 05:05:59 +0100207 return (_stricmp(codec.name.c_str(), ref_name) == 0);
208}
209
solenbergd97ec302015-10-07 01:40:33 -0700210bool IsCodec(const webrtc::CodecInst& codec, const char* ref_name) {
Minyue Li7100dcd2015-03-27 05:05:59 +0100211 return (_stricmp(codec.plname, ref_name) == 0);
212}
213
solenbergd97ec302015-10-07 01:40:33 -0700214bool IsCodecMultiRate(const webrtc::CodecInst& codec) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000215 for (size_t i = 0; i < ARRAY_SIZE(kCodecPrefs); ++i) {
Minyue Li7100dcd2015-03-27 05:05:59 +0100216 if (IsCodec(codec, kCodecPrefs[i].name) &&
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000217 kCodecPrefs[i].clockrate == codec.plfreq) {
218 return kCodecPrefs[i].is_multi_rate;
219 }
220 }
221 return false;
222}
223
solenbergd97ec302015-10-07 01:40:33 -0700224bool FindCodec(const std::vector<AudioCodec>& codecs,
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000225 const AudioCodec& codec,
226 AudioCodec* found_codec) {
Fredrik Solenbergaf9fb212015-08-26 10:45:53 +0200227 for (const AudioCodec& c : codecs) {
228 if (c.Matches(codec)) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000229 if (found_codec != NULL) {
Fredrik Solenbergaf9fb212015-08-26 10:45:53 +0200230 *found_codec = c;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000231 }
232 return true;
233 }
234 }
235 return false;
236}
wu@webrtc.org1d1ffc92013-10-16 18:12:02 +0000237
solenberg0b675462015-10-09 01:37:09 -0700238bool VerifyUniquePayloadTypes(const std::vector<AudioCodec>& codecs) {
239 if (codecs.empty()) {
240 return true;
241 }
242 std::vector<int> payload_types;
243 for (const AudioCodec& codec : codecs) {
244 payload_types.push_back(codec.id);
245 }
246 std::sort(payload_types.begin(), payload_types.end());
247 auto it = std::unique(payload_types.begin(), payload_types.end());
248 return it == payload_types.end();
249}
250
solenbergd97ec302015-10-07 01:40:33 -0700251bool IsNackEnabled(const AudioCodec& codec) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000252 return codec.HasFeedbackParam(FeedbackParam(kRtcpFbParamNack,
253 kParamValueEmpty));
254}
255
solenbergd97ec302015-10-07 01:40:33 -0700256int SelectPacketSize(const CodecPref& codec_pref, int ptime_ms) {
Brave Yao5225dd82015-03-26 07:39:19 +0800257 int selected_packet_size_ms = codec_pref.packet_sizes_ms[0];
258 for (int packet_size_ms : codec_pref.packet_sizes_ms) {
259 if (packet_size_ms && packet_size_ms <= ptime_ms) {
260 selected_packet_size_ms = packet_size_ms;
261 }
262 }
263 return selected_packet_size_ms;
264}
265
266// If the AudioCodec param kCodecParamPTime is set, then we will set it to codec
267// pacsize if it's valid, or we will pick the next smallest value we support.
268// TODO(Brave): Query supported packet sizes from ACM when the API is ready.
solenbergd97ec302015-10-07 01:40:33 -0700269bool SetPTimeAsPacketSize(webrtc::CodecInst* codec, int ptime_ms) {
Brave Yao5225dd82015-03-26 07:39:19 +0800270 for (const CodecPref& codec_pref : kCodecPrefs) {
Minyue Li7100dcd2015-03-27 05:05:59 +0100271 if ((IsCodec(*codec, codec_pref.name) &&
Brave Yao5225dd82015-03-26 07:39:19 +0800272 codec_pref.clockrate == codec->plfreq) ||
Minyue Li7100dcd2015-03-27 05:05:59 +0100273 IsCodec(*codec, kG722CodecName)) {
Brave Yao5225dd82015-03-26 07:39:19 +0800274 int packet_size_ms = SelectPacketSize(codec_pref, ptime_ms);
275 if (packet_size_ms) {
276 // Convert unit from milli-seconds to samples.
277 codec->pacsize = (codec->plfreq / 1000) * packet_size_ms;
278 return true;
279 }
280 }
281 }
282 return false;
283}
284
Minyue Li7100dcd2015-03-27 05:05:59 +0100285// Return true if codec.params[feature] == "1", false otherwise.
solenbergd97ec302015-10-07 01:40:33 -0700286bool IsCodecFeatureEnabled(const AudioCodec& codec,
Minyue Li7100dcd2015-03-27 05:05:59 +0100287 const char* feature) {
288 int value;
289 return codec.GetParam(feature, &value) && value == 1;
290}
291
292// Use params[kCodecParamMaxAverageBitrate] if it is defined, use codec.bitrate
293// otherwise. If the value (either from params or codec.bitrate) <=0, use the
294// default configuration. If the value is beyond feasible bit rate of Opus,
295// clamp it. Returns the Opus bit rate for operation.
solenbergd97ec302015-10-07 01:40:33 -0700296int GetOpusBitrate(const AudioCodec& codec, int max_playback_rate) {
Minyue Li7100dcd2015-03-27 05:05:59 +0100297 int bitrate = 0;
298 bool use_param = true;
299 if (!codec.GetParam(kCodecParamMaxAverageBitrate, &bitrate)) {
300 bitrate = codec.bitrate;
301 use_param = false;
302 }
303 if (bitrate <= 0) {
304 if (max_playback_rate <= 8000) {
305 bitrate = kOpusBitrateNb;
306 } else if (max_playback_rate <= 16000) {
307 bitrate = kOpusBitrateWb;
308 } else {
309 bitrate = kOpusBitrateFb;
310 }
311
312 if (IsCodecFeatureEnabled(codec, kCodecParamStereo)) {
313 bitrate *= 2;
314 }
315 } else if (bitrate < kOpusMinBitrate || bitrate > kOpusMaxBitrate) {
316 bitrate = (bitrate < kOpusMinBitrate) ? kOpusMinBitrate : kOpusMaxBitrate;
317 std::string rate_source =
318 use_param ? "Codec parameter \"maxaveragebitrate\"" :
319 "Supplied Opus bitrate";
320 LOG(LS_WARNING) << rate_source
321 << " is invalid and is replaced by: "
322 << bitrate;
323 }
324 return bitrate;
325}
326
327// Returns kOpusDefaultPlaybackRate if params[kCodecParamMaxPlaybackRate] is not
328// defined. Returns the value of params[kCodecParamMaxPlaybackRate] otherwise.
solenbergd97ec302015-10-07 01:40:33 -0700329int GetOpusMaxPlaybackRate(const AudioCodec& codec) {
Minyue Li7100dcd2015-03-27 05:05:59 +0100330 int value;
331 if (codec.GetParam(kCodecParamMaxPlaybackRate, &value)) {
332 return value;
333 }
334 return kOpusDefaultMaxPlaybackRate;
335}
336
solenbergd97ec302015-10-07 01:40:33 -0700337void GetOpusConfig(const AudioCodec& codec, webrtc::CodecInst* voe_codec,
Minyue Li7100dcd2015-03-27 05:05:59 +0100338 bool* enable_codec_fec, int* max_playback_rate,
339 bool* enable_codec_dtx) {
340 *enable_codec_fec = IsCodecFeatureEnabled(codec, kCodecParamUseInbandFec);
341 *enable_codec_dtx = IsCodecFeatureEnabled(codec, kCodecParamUseDtx);
342 *max_playback_rate = GetOpusMaxPlaybackRate(codec);
343
344 // If OPUS, change what we send according to the "stereo" codec
345 // parameter, and not the "channels" parameter. We set
346 // voe_codec.channels to 2 if "stereo=1" and 1 otherwise. If
347 // the bitrate is not specified, i.e. is <= zero, we set it to the
348 // appropriate default value for mono or stereo Opus.
349
350 voe_codec->channels = IsCodecFeatureEnabled(codec, kCodecParamStereo) ? 2 : 1;
351 voe_codec->rate = GetOpusBitrate(codec, *max_playback_rate);
352}
353
354// Changes RTP timestamp rate of G722. This is due to the "bug" in the RFC
355// which says that G722 should be advertised as 8 kHz although it is a 16 kHz
356// codec.
solenbergd97ec302015-10-07 01:40:33 -0700357void MaybeFixupG722(webrtc::CodecInst* voe_codec, int new_plfreq) {
Minyue Li7100dcd2015-03-27 05:05:59 +0100358 if (IsCodec(*voe_codec, kG722CodecName)) {
359 // If the ASSERT triggers, the codec definition in WebRTC VoiceEngine
360 // has changed, and this special case is no longer needed.
henrikg91d6ede2015-09-17 00:24:34 -0700361 RTC_DCHECK(voe_codec->plfreq != new_plfreq);
Minyue Li7100dcd2015-03-27 05:05:59 +0100362 voe_codec->plfreq = new_plfreq;
363 }
364}
365
wu@webrtc.org1d1ffc92013-10-16 18:12:02 +0000366// Gets the default set of options applied to the engine. Historically, these
367// were supplied as a combination of flags from the channel manager (ec, agc,
368// ns, and highpass) and the rest hardcoded in InitInternal.
solenbergd97ec302015-10-07 01:40:33 -0700369AudioOptions GetDefaultEngineOptions() {
wu@webrtc.org1d1ffc92013-10-16 18:12:02 +0000370 AudioOptions options;
371 options.echo_cancellation.Set(true);
372 options.auto_gain_control.Set(true);
373 options.noise_suppression.Set(true);
374 options.highpass_filter.Set(true);
375 options.stereo_swapping.Set(false);
Henrik Lundin64dad832015-05-11 12:44:23 +0200376 options.audio_jitter_buffer_max_packets.Set(50);
Henrik Lundin5263b3c2015-06-01 10:29:41 +0200377 options.audio_jitter_buffer_fast_accelerate.Set(false);
wu@webrtc.org1d1ffc92013-10-16 18:12:02 +0000378 options.typing_detection.Set(true);
wu@webrtc.org1d1ffc92013-10-16 18:12:02 +0000379 options.adjust_agc_delta.Set(0);
380 options.experimental_agc.Set(false);
Henrik Lundin441f6342015-06-09 16:03:13 +0200381 options.extended_filter_aec.Set(false);
Bjorn Volckerbf395c12015-03-25 22:45:56 +0100382 options.delay_agnostic_aec.Set(false);
sergeyu@chromium.org9cf037b2014-02-07 19:03:26 +0000383 options.experimental_ns.Set(false);
wu@webrtc.org1d1ffc92013-10-16 18:12:02 +0000384 options.aec_dump.Set(false);
385 return options;
386}
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000387
solenbergd97ec302015-10-07 01:40:33 -0700388std::string GetEnableString(bool enable) {
Minyue Li7100dcd2015-03-27 05:05:59 +0100389 return enable ? "enable" : "disable";
Brave Yao5225dd82015-03-26 07:39:19 +0800390}
solenbergd97ec302015-10-07 01:40:33 -0700391} // namespace {
Brave Yao5225dd82015-03-26 07:39:19 +0800392
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000393WebRtcVoiceEngine::WebRtcVoiceEngine()
394 : voe_wrapper_(new VoEWrapper()),
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000395 tracing_(new VoETraceWrapper()),
396 adm_(NULL),
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000397 log_filter_(SeverityToFilter(kDefaultLogSeverity)),
Fredrik Solenberg7d173362015-09-23 12:23:21 +0200398 is_dumping_aec_(false) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000399 Construct();
400}
401
402WebRtcVoiceEngine::WebRtcVoiceEngine(VoEWrapper* voe_wrapper,
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000403 VoETraceWrapper* tracing)
404 : voe_wrapper_(voe_wrapper),
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000405 tracing_(tracing),
406 adm_(NULL),
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000407 log_filter_(SeverityToFilter(kDefaultLogSeverity)),
Fredrik Solenberg7d173362015-09-23 12:23:21 +0200408 is_dumping_aec_(false) {
buildbot@webrtc.org13d67762014-05-02 17:33:29 +0000409 Construct();
410}
411
412void WebRtcVoiceEngine::Construct() {
413 SetTraceFilter(log_filter_);
414 initialized_ = false;
415 LOG(LS_VERBOSE) << "WebRtcVoiceEngine::WebRtcVoiceEngine";
416 SetTraceOptions("");
417 if (tracing_->SetTraceCallback(this) == -1) {
418 LOG_RTCERR0(SetTraceCallback);
419 }
420 if (voe_wrapper_->base()->RegisterVoiceEngineObserver(*this) == -1) {
421 LOG_RTCERR0(RegisterVoiceEngineObserver);
422 }
423 // Clear the default agc state.
424 memset(&default_agc_config_, 0, sizeof(default_agc_config_));
425
426 // Load our audio codec list.
427 ConstructCodecs();
428
429 // Load our RTP Header extensions.
430 rtp_header_extensions_.push_back(
431 RtpHeaderExtension(kRtpAudioLevelHeaderExtension,
432 kRtpAudioLevelHeaderExtensionDefaultId));
433 rtp_header_extensions_.push_back(
434 RtpHeaderExtension(kRtpAbsoluteSenderTimeHeaderExtension,
435 kRtpAbsoluteSenderTimeHeaderExtensionDefaultId));
stefanc1aeaf02015-10-15 07:26:07 -0700436 if (webrtc::field_trial::FindFullName("WebRTC-SendSideBwe") == "Enabled") {
437 rtp_header_extensions_.push_back(RtpHeaderExtension(
438 kRtpTransportSequenceNumberHeaderExtension,
439 kRtpTransportSequenceNumberHeaderExtensionDefaultId));
440 }
buildbot@webrtc.org13d67762014-05-02 17:33:29 +0000441 options_ = GetDefaultEngineOptions();
442}
443
buildbot@webrtc.org13d67762014-05-02 17:33:29 +0000444void WebRtcVoiceEngine::ConstructCodecs() {
445 LOG(LS_INFO) << "WebRtc VoiceEngine codecs:";
446 int ncodecs = voe_wrapper_->codec()->NumOfCodecs();
447 for (int i = 0; i < ncodecs; ++i) {
448 webrtc::CodecInst voe_codec;
henrik.lundin@webrtc.org8038d422014-11-11 08:38:24 +0000449 if (GetVoeCodec(i, &voe_codec)) {
buildbot@webrtc.org13d67762014-05-02 17:33:29 +0000450 // Skip uncompressed formats.
Minyue Li7100dcd2015-03-27 05:05:59 +0100451 if (IsCodec(voe_codec, kL16CodecName)) {
buildbot@webrtc.org13d67762014-05-02 17:33:29 +0000452 continue;
453 }
454
455 const CodecPref* pref = NULL;
456 for (size_t j = 0; j < ARRAY_SIZE(kCodecPrefs); ++j) {
Minyue Li7100dcd2015-03-27 05:05:59 +0100457 if (IsCodec(voe_codec, kCodecPrefs[j].name) &&
buildbot@webrtc.org13d67762014-05-02 17:33:29 +0000458 kCodecPrefs[j].clockrate == voe_codec.plfreq &&
459 kCodecPrefs[j].channels == voe_codec.channels) {
460 pref = &kCodecPrefs[j];
461 break;
462 }
463 }
464
465 if (pref) {
466 // Use the payload type that we've configured in our pref table;
467 // use the offset in our pref table to determine the sort order.
468 AudioCodec codec(pref->payload_type, voe_codec.plname, voe_codec.plfreq,
469 voe_codec.rate, voe_codec.channels,
470 ARRAY_SIZE(kCodecPrefs) - (pref - kCodecPrefs));
471 LOG(LS_INFO) << ToString(codec);
Minyue Li7100dcd2015-03-27 05:05:59 +0100472 if (IsCodec(codec, kIsacCodecName)) {
minyue@webrtc.org26236952014-10-29 02:27:08 +0000473 // Indicate auto-bitrate in signaling.
buildbot@webrtc.org13d67762014-05-02 17:33:29 +0000474 codec.bitrate = 0;
475 }
Minyue Li7100dcd2015-03-27 05:05:59 +0100476 if (IsCodec(codec, kOpusCodecName)) {
buildbot@webrtc.org13d67762014-05-02 17:33:29 +0000477 // Only add fmtp parameters that differ from the spec.
478 if (kPreferredMinPTime != kOpusDefaultMinPTime) {
479 codec.params[kCodecParamMinPTime] =
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000480 rtc::ToString(kPreferredMinPTime);
buildbot@webrtc.org13d67762014-05-02 17:33:29 +0000481 }
482 if (kPreferredMaxPTime != kOpusDefaultMaxPTime) {
483 codec.params[kCodecParamMaxPTime] =
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000484 rtc::ToString(kPreferredMaxPTime);
buildbot@webrtc.org13d67762014-05-02 17:33:29 +0000485 }
pkasting@chromium.orgd3245462015-02-23 21:28:22 +0000486 codec.SetParam(kCodecParamUseInbandFec, 1);
minyue@webrtc.org4ef22d12014-11-17 09:26:39 +0000487
488 // TODO(hellner): Add ptime, sprop-stereo, and stereo
buildbot@webrtc.org13d67762014-05-02 17:33:29 +0000489 // when they can be set to values other than the default.
490 }
491 codecs_.push_back(codec);
492 } else {
493 LOG(LS_WARNING) << "Unexpected codec: " << ToString(voe_codec);
494 }
495 }
496 }
497 // Make sure they are in local preference order.
498 std::sort(codecs_.begin(), codecs_.end(), &AudioCodec::Preferable);
499}
500
henrik.lundin@webrtc.org8038d422014-11-11 08:38:24 +0000501bool WebRtcVoiceEngine::GetVoeCodec(int index, webrtc::CodecInst* codec) {
502 if (voe_wrapper_->codec()->GetCodec(index, *codec) == -1) {
503 return false;
henrik.lundin@webrtc.orgf85dbce2014-11-07 12:25:00 +0000504 }
henrik.lundin@webrtc.org8038d422014-11-11 08:38:24 +0000505 // Change the sample rate of G722 to 8000 to match SDP.
506 MaybeFixupG722(codec, 8000);
507 return true;
henrik.lundin@webrtc.orgf85dbce2014-11-07 12:25:00 +0000508}
509
buildbot@webrtc.org13d67762014-05-02 17:33:29 +0000510WebRtcVoiceEngine::~WebRtcVoiceEngine() {
511 LOG(LS_VERBOSE) << "WebRtcVoiceEngine::~WebRtcVoiceEngine";
512 if (voe_wrapper_->base()->DeRegisterVoiceEngineObserver() == -1) {
513 LOG_RTCERR0(DeRegisterVoiceEngineObserver);
514 }
515 if (adm_) {
516 voe_wrapper_.reset();
517 adm_->Release();
518 adm_ = NULL;
519 }
buildbot@webrtc.org13d67762014-05-02 17:33:29 +0000520
buildbot@webrtc.org13d67762014-05-02 17:33:29 +0000521 tracing_->SetTraceCallback(NULL);
522}
523
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000524bool WebRtcVoiceEngine::Init(rtc::Thread* worker_thread) {
henrikg91d6ede2015-09-17 00:24:34 -0700525 RTC_DCHECK(worker_thread == rtc::Thread::Current());
buildbot@webrtc.org13d67762014-05-02 17:33:29 +0000526 LOG(LS_INFO) << "WebRtcVoiceEngine::Init";
527 bool res = InitInternal();
528 if (res) {
529 LOG(LS_INFO) << "WebRtcVoiceEngine::Init Done!";
530 } else {
531 LOG(LS_ERROR) << "WebRtcVoiceEngine::Init failed";
532 Terminate();
533 }
534 return res;
535}
536
537bool WebRtcVoiceEngine::InitInternal() {
538 // Temporarily turn logging level up for the Init call
539 int old_filter = log_filter_;
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000540 int extended_filter = log_filter_ | SeverityToFilter(rtc::LS_INFO);
buildbot@webrtc.org13d67762014-05-02 17:33:29 +0000541 SetTraceFilter(extended_filter);
542 SetTraceOptions("");
543
544 // Init WebRtc VoiceEngine.
545 if (voe_wrapper_->base()->Init(adm_) == -1) {
546 LOG_RTCERR0_EX(Init, voe_wrapper_->error());
547 SetTraceFilter(old_filter);
548 return false;
549 }
550
551 SetTraceFilter(old_filter);
552 SetTraceOptions(log_options_);
553
554 // Log the VoiceEngine version info
555 char buffer[1024] = "";
556 voe_wrapper_->base()->GetVersion(buffer);
557 LOG(LS_INFO) << "WebRtc VoiceEngine Version:";
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000558 LogMultiline(rtc::LS_INFO, buffer);
buildbot@webrtc.org13d67762014-05-02 17:33:29 +0000559
560 // Save the default AGC configuration settings. This must happen before
561 // calling SetOptions or the default will be overwritten.
562 if (voe_wrapper_->processing()->GetAgcConfig(default_agc_config_) == -1) {
563 LOG_RTCERR0(GetAgcConfig);
564 return false;
565 }
566
567 // Set defaults for options, so that ApplyOptions applies them explicitly
568 // when we clear option (channel) overrides. External clients can still
569 // modify the defaults via SetOptions (on the media engine).
570 if (!SetOptions(GetDefaultEngineOptions())) {
571 return false;
572 }
573
574 // Print our codec list again for the call diagnostic log
575 LOG(LS_INFO) << "WebRtc VoiceEngine codecs:";
Fredrik Solenbergaf9fb212015-08-26 10:45:53 +0200576 for (const AudioCodec& codec : codecs_) {
577 LOG(LS_INFO) << ToString(codec);
buildbot@webrtc.org13d67762014-05-02 17:33:29 +0000578 }
579
580 // Disable the DTMF playout when a tone is sent.
581 // PlayDtmfTone will be used if local playout is needed.
582 if (voe_wrapper_->dtmf()->SetDtmfFeedbackStatus(false) == -1) {
583 LOG_RTCERR1(SetDtmfFeedbackStatus, false);
584 }
585
586 initialized_ = true;
587 return true;
588}
589
buildbot@webrtc.org13d67762014-05-02 17:33:29 +0000590void WebRtcVoiceEngine::Terminate() {
591 LOG(LS_INFO) << "WebRtcVoiceEngine::Terminate";
592 initialized_ = false;
593
594 StopAecDump();
595
buildbot@webrtc.org13d67762014-05-02 17:33:29 +0000596 voe_wrapper_->base()->Terminate();
buildbot@webrtc.org13d67762014-05-02 17:33:29 +0000597}
598
Fredrik Solenberg709ed672015-09-15 12:26:33 +0200599VoiceMediaChannel* WebRtcVoiceEngine::CreateChannel(webrtc::Call* call,
Jelena Marusicc28a8962015-05-29 15:05:44 +0200600 const AudioOptions& options) {
solenberg0a617e22015-10-20 15:49:38 -0700601 return new WebRtcVoiceMediaChannel(this, options, call);
buildbot@webrtc.org13d67762014-05-02 17:33:29 +0000602}
603
buildbot@webrtc.org13d67762014-05-02 17:33:29 +0000604bool WebRtcVoiceEngine::SetOptions(const AudioOptions& options) {
605 if (!ApplyOptions(options)) {
606 return false;
607 }
608 options_ = options;
609 return true;
610}
611
buildbot@webrtc.org13d67762014-05-02 17:33:29 +0000612// AudioOptions defaults are set in InitInternal (for options with corresponding
613// MediaEngineInterface flags) and in SetOptions(int) for flagless options.
614bool WebRtcVoiceEngine::ApplyOptions(const AudioOptions& options_in) {
henrikac14f5ff2015-09-23 14:08:33 +0200615 LOG(LS_INFO) << "ApplyOptions: " << options_in.ToString();
buildbot@webrtc.org13d67762014-05-02 17:33:29 +0000616 AudioOptions options = options_in; // The options are modified below.
617 // kEcConference is AEC with high suppression.
618 webrtc::EcModes ec_mode = webrtc::kEcConference;
619 webrtc::AecmModes aecm_mode = webrtc::kAecmSpeakerphone;
620 webrtc::AgcModes agc_mode = webrtc::kAgcAdaptiveAnalog;
621 webrtc::NsModes ns_mode = webrtc::kNsHighSuppression;
622 bool aecm_comfort_noise = false;
623 if (options.aecm_generate_comfort_noise.Get(&aecm_comfort_noise)) {
624 LOG(LS_VERBOSE) << "Comfort noise explicitly set to "
625 << aecm_comfort_noise << " (default is false).";
626 }
627
628#if defined(IOS)
629 // On iOS, VPIO provides built-in EC and AGC.
630 options.echo_cancellation.Set(false);
631 options.auto_gain_control.Set(false);
henrika86d907c2015-09-07 16:09:50 +0200632 LOG(LS_INFO) << "Always disable AEC and AGC on iOS. Use built-in instead.";
buildbot@webrtc.org13d67762014-05-02 17:33:29 +0000633#elif defined(ANDROID)
634 ec_mode = webrtc::kEcAecm;
635#endif
636
637#if defined(IOS) || defined(ANDROID)
638 // Set the AGC mode for iOS as well despite disabling it above, to avoid
639 // unsupported configuration errors from webrtc.
640 agc_mode = webrtc::kAgcFixedDigital;
641 options.typing_detection.Set(false);
642 options.experimental_agc.Set(false);
Henrik Lundin441f6342015-06-09 16:03:13 +0200643 options.extended_filter_aec.Set(false);
buildbot@webrtc.org13d67762014-05-02 17:33:29 +0000644 options.experimental_ns.Set(false);
645#endif
646
Bjorn Volckerbf395c12015-03-25 22:45:56 +0100647 // Delay Agnostic AEC automatically turns on EC if not set except on iOS
648 // where the feature is not supported.
649 bool use_delay_agnostic_aec = false;
650#if !defined(IOS)
651 if (options.delay_agnostic_aec.Get(&use_delay_agnostic_aec)) {
652 if (use_delay_agnostic_aec) {
653 options.echo_cancellation.Set(true);
Henrik Lundin441f6342015-06-09 16:03:13 +0200654 options.extended_filter_aec.Set(true);
Bjorn Volckerbf395c12015-03-25 22:45:56 +0100655 ec_mode = webrtc::kEcConference;
656 }
657 }
658#endif
659
buildbot@webrtc.org13d67762014-05-02 17:33:29 +0000660 webrtc::VoEAudioProcessing* voep = voe_wrapper_->processing();
661
henrika@webrtc.orga954c072014-12-09 16:22:09 +0000662 bool echo_cancellation = false;
buildbot@webrtc.org13d67762014-05-02 17:33:29 +0000663 if (options.echo_cancellation.Get(&echo_cancellation)) {
henrika@webrtc.orga954c072014-12-09 16:22:09 +0000664 // Check if platform supports built-in EC. Currently only supported on
665 // Android and in combination with Java based audio layer.
666 // TODO(henrika): investigate possibility to support built-in EC also
667 // in combination with Open SL ES audio.
668 const bool built_in_aec = voe_wrapper_->hw()->BuiltInAECIsAvailable();
Bjorn Volcker73f72102015-06-03 14:50:15 +0200669 if (built_in_aec) {
Bjorn Volckerccfc9392015-05-07 07:43:17 +0200670 // Built-in EC exists on this device and use_delay_agnostic_aec is not
671 // overriding it. Enable/Disable it according to the echo_cancellation
672 // audio option.
Bjorn Volcker73f72102015-06-03 14:50:15 +0200673 const bool enable_built_in_aec =
674 echo_cancellation && !use_delay_agnostic_aec;
675 if (voe_wrapper_->hw()->EnableBuiltInAEC(enable_built_in_aec) == 0 &&
676 enable_built_in_aec) {
Bjorn Volckerbf395c12015-03-25 22:45:56 +0100677 // Disable internal software EC if built-in EC is enabled,
henrika@webrtc.orga954c072014-12-09 16:22:09 +0000678 // i.e., replace the software EC with the built-in EC.
679 options.echo_cancellation.Set(false);
bjornv@webrtc.org3f118232015-03-16 14:22:03 +0000680 echo_cancellation = false;
henrika@webrtc.orga954c072014-12-09 16:22:09 +0000681 LOG(LS_INFO) << "Disabling EC since built-in EC will be used instead";
682 }
683 }
buildbot@webrtc.org13d67762014-05-02 17:33:29 +0000684 if (voep->SetEcStatus(echo_cancellation, ec_mode) == -1) {
685 LOG_RTCERR2(SetEcStatus, echo_cancellation, ec_mode);
686 return false;
687 } else {
henrika86d907c2015-09-07 16:09:50 +0200688 LOG(LS_INFO) << "Echo control set to " << echo_cancellation
689 << " with mode " << ec_mode;
buildbot@webrtc.org13d67762014-05-02 17:33:29 +0000690 }
691#if !defined(ANDROID)
692 // TODO(ajm): Remove the error return on Android from webrtc.
693 if (voep->SetEcMetricsStatus(echo_cancellation) == -1) {
694 LOG_RTCERR1(SetEcMetricsStatus, echo_cancellation);
695 return false;
696 }
697#endif
698 if (ec_mode == webrtc::kEcAecm) {
699 if (voep->SetAecmMode(aecm_mode, aecm_comfort_noise) != 0) {
700 LOG_RTCERR2(SetAecmMode, aecm_mode, aecm_comfort_noise);
701 return false;
702 }
703 }
704 }
705
henrikac14f5ff2015-09-23 14:08:33 +0200706 bool auto_gain_control = false;
buildbot@webrtc.org13d67762014-05-02 17:33:29 +0000707 if (options.auto_gain_control.Get(&auto_gain_control)) {
henrikac14f5ff2015-09-23 14:08:33 +0200708 const bool built_in_agc = voe_wrapper_->hw()->BuiltInAGCIsAvailable();
709 if (built_in_agc) {
710 if (voe_wrapper_->hw()->EnableBuiltInAGC(auto_gain_control) == 0 &&
711 auto_gain_control) {
712 // Disable internal software AGC if built-in AGC is enabled,
713 // i.e., replace the software AGC with the built-in AGC.
714 options.auto_gain_control.Set(false);
715 auto_gain_control = false;
716 LOG(LS_INFO) << "Disabling AGC since built-in AGC will be used instead";
717 }
718 }
buildbot@webrtc.org13d67762014-05-02 17:33:29 +0000719 if (voep->SetAgcStatus(auto_gain_control, agc_mode) == -1) {
720 LOG_RTCERR2(SetAgcStatus, auto_gain_control, agc_mode);
721 return false;
722 } else {
henrika86d907c2015-09-07 16:09:50 +0200723 LOG(LS_INFO) << "Auto gain set to " << auto_gain_control << " with mode "
724 << agc_mode;
buildbot@webrtc.org13d67762014-05-02 17:33:29 +0000725 }
726 }
727
728 if (options.tx_agc_target_dbov.IsSet() ||
729 options.tx_agc_digital_compression_gain.IsSet() ||
730 options.tx_agc_limiter.IsSet()) {
731 // Override default_agc_config_. Generally, an unset option means "leave
732 // the VoE bits alone" in this function, so we want whatever is set to be
733 // stored as the new "default". If we didn't, then setting e.g.
734 // tx_agc_target_dbov would reset digital compression gain and limiter
735 // settings.
736 // Also, if we don't update default_agc_config_, then adjust_agc_delta
737 // would be an offset from the original values, and not whatever was set
738 // explicitly.
739 default_agc_config_.targetLeveldBOv =
740 options.tx_agc_target_dbov.GetWithDefaultIfUnset(
741 default_agc_config_.targetLeveldBOv);
742 default_agc_config_.digitalCompressionGaindB =
743 options.tx_agc_digital_compression_gain.GetWithDefaultIfUnset(
744 default_agc_config_.digitalCompressionGaindB);
745 default_agc_config_.limiterEnable =
746 options.tx_agc_limiter.GetWithDefaultIfUnset(
747 default_agc_config_.limiterEnable);
748 if (voe_wrapper_->processing()->SetAgcConfig(default_agc_config_) == -1) {
749 LOG_RTCERR3(SetAgcConfig,
750 default_agc_config_.targetLeveldBOv,
751 default_agc_config_.digitalCompressionGaindB,
752 default_agc_config_.limiterEnable);
753 return false;
754 }
755 }
756
henrikac14f5ff2015-09-23 14:08:33 +0200757 bool noise_suppression = false;
buildbot@webrtc.org13d67762014-05-02 17:33:29 +0000758 if (options.noise_suppression.Get(&noise_suppression)) {
henrikac14f5ff2015-09-23 14:08:33 +0200759 const bool built_in_ns = voe_wrapper_->hw()->BuiltInNSIsAvailable();
760 if (built_in_ns) {
761 if (voe_wrapper_->hw()->EnableBuiltInNS(noise_suppression) == 0 &&
762 noise_suppression) {
763 // Disable internal software NS if built-in NS is enabled,
764 // i.e., replace the software NS with the built-in NS.
765 options.noise_suppression.Set(false);
766 noise_suppression = false;
767 LOG(LS_INFO) << "Disabling NS since built-in NS will be used instead";
768 }
769 }
buildbot@webrtc.org13d67762014-05-02 17:33:29 +0000770 if (voep->SetNsStatus(noise_suppression, ns_mode) == -1) {
771 LOG_RTCERR2(SetNsStatus, noise_suppression, ns_mode);
772 return false;
773 } else {
henrikac14f5ff2015-09-23 14:08:33 +0200774 LOG(LS_INFO) << "Noise suppression set to " << noise_suppression
775 << " with mode " << ns_mode;
buildbot@webrtc.org13d67762014-05-02 17:33:29 +0000776 }
777 }
778
buildbot@webrtc.org13d67762014-05-02 17:33:29 +0000779 bool highpass_filter;
780 if (options.highpass_filter.Get(&highpass_filter)) {
781 LOG(LS_INFO) << "High pass filter enabled? " << highpass_filter;
782 if (voep->EnableHighPassFilter(highpass_filter) == -1) {
783 LOG_RTCERR1(SetHighpassFilterStatus, highpass_filter);
784 return false;
785 }
786 }
787
788 bool stereo_swapping;
789 if (options.stereo_swapping.Get(&stereo_swapping)) {
790 LOG(LS_INFO) << "Stereo swapping enabled? " << stereo_swapping;
791 voep->EnableStereoChannelSwapping(stereo_swapping);
792 if (voep->IsStereoChannelSwappingEnabled() != stereo_swapping) {
793 LOG_RTCERR1(EnableStereoChannelSwapping, stereo_swapping);
794 return false;
795 }
796 }
797
Henrik Lundin64dad832015-05-11 12:44:23 +0200798 int audio_jitter_buffer_max_packets;
799 if (options.audio_jitter_buffer_max_packets.Get(
800 &audio_jitter_buffer_max_packets)) {
801 LOG(LS_INFO) << "NetEq capacity is " << audio_jitter_buffer_max_packets;
802 voe_config_.Set<webrtc::NetEqCapacityConfig>(
803 new webrtc::NetEqCapacityConfig(audio_jitter_buffer_max_packets));
804 }
805
Henrik Lundin5263b3c2015-06-01 10:29:41 +0200806 bool audio_jitter_buffer_fast_accelerate;
807 if (options.audio_jitter_buffer_fast_accelerate.Get(
808 &audio_jitter_buffer_fast_accelerate)) {
809 LOG(LS_INFO) << "NetEq fast mode? " << audio_jitter_buffer_fast_accelerate;
810 voe_config_.Set<webrtc::NetEqFastAccelerate>(
811 new webrtc::NetEqFastAccelerate(audio_jitter_buffer_fast_accelerate));
812 }
813
buildbot@webrtc.org13d67762014-05-02 17:33:29 +0000814 bool typing_detection;
815 if (options.typing_detection.Get(&typing_detection)) {
816 LOG(LS_INFO) << "Typing detection is enabled? " << typing_detection;
817 if (voep->SetTypingDetectionStatus(typing_detection) == -1) {
818 // In case of error, log the info and continue
819 LOG_RTCERR1(SetTypingDetectionStatus, typing_detection);
820 }
821 }
822
823 int adjust_agc_delta;
824 if (options.adjust_agc_delta.Get(&adjust_agc_delta)) {
825 LOG(LS_INFO) << "Adjust agc delta is " << adjust_agc_delta;
826 if (!AdjustAgcLevel(adjust_agc_delta)) {
827 return false;
828 }
829 }
830
831 bool aec_dump;
832 if (options.aec_dump.Get(&aec_dump)) {
833 LOG(LS_INFO) << "Aec dump is enabled? " << aec_dump;
834 if (aec_dump)
835 StartAecDump(kAecDumpByAudioOptionFilename);
836 else
837 StopAecDump();
838 }
839
buildbot@webrtc.org1f8a2372014-08-28 10:52:44 +0000840 webrtc::Config config;
841
Bjorn Volckerbf395c12015-03-25 22:45:56 +0100842 delay_agnostic_aec_.SetFrom(options.delay_agnostic_aec);
843 bool delay_agnostic_aec;
844 if (delay_agnostic_aec_.Get(&delay_agnostic_aec)) {
845 LOG(LS_INFO) << "Delay agnostic aec is enabled? " << delay_agnostic_aec;
henrik.lundin0f133b92015-07-02 00:17:55 -0700846 config.Set<webrtc::DelayAgnostic>(
847 new webrtc::DelayAgnostic(delay_agnostic_aec));
Bjorn Volckerbf395c12015-03-25 22:45:56 +0100848 }
849
Henrik Lundin441f6342015-06-09 16:03:13 +0200850 extended_filter_aec_.SetFrom(options.extended_filter_aec);
851 bool extended_filter;
852 if (extended_filter_aec_.Get(&extended_filter)) {
853 LOG(LS_INFO) << "Extended filter aec is enabled? " << extended_filter;
854 config.Set<webrtc::ExtendedFilter>(
855 new webrtc::ExtendedFilter(extended_filter));
buildbot@webrtc.org1f8a2372014-08-28 10:52:44 +0000856 }
857
buildbot@webrtc.org1f8a2372014-08-28 10:52:44 +0000858 experimental_ns_.SetFrom(options.experimental_ns);
859 bool experimental_ns;
860 if (experimental_ns_.Get(&experimental_ns)) {
861 LOG(LS_INFO) << "Experimental ns is enabled? " << experimental_ns;
862 config.Set<webrtc::ExperimentalNs>(
863 new webrtc::ExperimentalNs(experimental_ns));
864 }
buildbot@webrtc.org1f8a2372014-08-28 10:52:44 +0000865
866 // We check audioproc for the benefit of tests, since FakeWebRtcVoiceEngine
867 // returns NULL on audio_processing().
868 webrtc::AudioProcessing* audioproc = voe_wrapper_->base()->audio_processing();
869 if (audioproc) {
870 audioproc->SetExtraOptions(config);
871 }
872
Peter Boström0c4e06b2015-10-07 12:23:21 +0200873 uint32_t recording_sample_rate;
buildbot@webrtc.org13d67762014-05-02 17:33:29 +0000874 if (options.recording_sample_rate.Get(&recording_sample_rate)) {
875 LOG(LS_INFO) << "Recording sample rate is " << recording_sample_rate;
876 if (voe_wrapper_->hw()->SetRecordingSampleRate(recording_sample_rate)) {
877 LOG_RTCERR1(SetRecordingSampleRate, recording_sample_rate);
878 }
879 }
880
Peter Boström0c4e06b2015-10-07 12:23:21 +0200881 uint32_t playout_sample_rate;
buildbot@webrtc.org13d67762014-05-02 17:33:29 +0000882 if (options.playout_sample_rate.Get(&playout_sample_rate)) {
883 LOG(LS_INFO) << "Playout sample rate is " << playout_sample_rate;
884 if (voe_wrapper_->hw()->SetPlayoutSampleRate(playout_sample_rate)) {
885 LOG_RTCERR1(SetPlayoutSampleRate, playout_sample_rate);
886 }
887 }
888
889 return true;
890}
891
buildbot@webrtc.org13d67762014-05-02 17:33:29 +0000892// TODO(juberti): Refactor this so that the core logic can be used to set the
893// soundclip device. At that time, reinstate the soundclip pause/resume code.
894bool WebRtcVoiceEngine::SetDevices(const Device* in_device,
895 const Device* out_device) {
896#if !defined(IOS)
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000897 int in_id = in_device ? rtc::FromString<int>(in_device->id) :
buildbot@webrtc.org13d67762014-05-02 17:33:29 +0000898 kDefaultAudioDeviceId;
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000899 int out_id = out_device ? rtc::FromString<int>(out_device->id) :
buildbot@webrtc.org13d67762014-05-02 17:33:29 +0000900 kDefaultAudioDeviceId;
901 // The device manager uses -1 as the default device, which was the case for
902 // VoE 3.5. VoE 4.0, however, uses 0 as the default in Linux and Mac.
903#ifndef WIN32
904 if (-1 == in_id) {
905 in_id = kDefaultAudioDeviceId;
906 }
907 if (-1 == out_id) {
908 out_id = kDefaultAudioDeviceId;
909 }
910#endif
911
912 std::string in_name = (in_id != kDefaultAudioDeviceId) ?
913 in_device->name : "Default device";
914 std::string out_name = (out_id != kDefaultAudioDeviceId) ?
915 out_device->name : "Default device";
916 LOG(LS_INFO) << "Setting microphone to (id=" << in_id << ", name=" << in_name
917 << ") and speaker to (id=" << out_id << ", name=" << out_name
918 << ")";
919
buildbot@webrtc.org13d67762014-05-02 17:33:29 +0000920 // Must also pause all audio playback and capture.
solenbergc1a1b352015-09-22 13:31:20 -0700921 bool ret = true;
Fredrik Solenbergaf9fb212015-08-26 10:45:53 +0200922 for (WebRtcVoiceMediaChannel* channel : channels_) {
buildbot@webrtc.org13d67762014-05-02 17:33:29 +0000923 if (!channel->PausePlayout()) {
924 LOG(LS_WARNING) << "Failed to pause playout";
925 ret = false;
926 }
927 if (!channel->PauseSend()) {
928 LOG(LS_WARNING) << "Failed to pause send";
929 ret = false;
930 }
931 }
932
933 // Find the recording device id in VoiceEngine and set recording device.
934 if (!FindWebRtcAudioDeviceId(true, in_name, in_id, &in_id)) {
935 ret = false;
936 }
937 if (ret) {
938 if (voe_wrapper_->hw()->SetRecordingDevice(in_id) == -1) {
939 LOG_RTCERR2(SetRecordingDevice, in_name, in_id);
940 ret = false;
941 }
buildbot@webrtc.org6b21b712014-07-31 15:08:53 +0000942 webrtc::AudioProcessing* ap = voe()->base()->audio_processing();
943 if (ap)
944 ap->Initialize();
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000945 }
946
947 // Find the playout device id in VoiceEngine and set playout device.
948 if (!FindWebRtcAudioDeviceId(false, out_name, out_id, &out_id)) {
949 LOG(LS_WARNING) << "Failed to find VoiceEngine device id for " << out_name;
950 ret = false;
951 }
952 if (ret) {
953 if (voe_wrapper_->hw()->SetPlayoutDevice(out_id) == -1) {
sergeyu@chromium.org5bc25c42013-12-05 00:24:06 +0000954 LOG_RTCERR2(SetPlayoutDevice, out_name, out_id);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000955 ret = false;
956 }
957 }
958
959 // Resume all audio playback and capture.
Fredrik Solenbergaf9fb212015-08-26 10:45:53 +0200960 for (WebRtcVoiceMediaChannel* channel : channels_) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000961 if (!channel->ResumePlayout()) {
962 LOG(LS_WARNING) << "Failed to resume playout";
963 ret = false;
964 }
965 if (!channel->ResumeSend()) {
966 LOG(LS_WARNING) << "Failed to resume send";
967 ret = false;
968 }
969 }
970
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000971 if (ret) {
972 LOG(LS_INFO) << "Set microphone to (id=" << in_id <<" name=" << in_name
973 << ") and speaker to (id="<< out_id << " name=" << out_name
974 << ")";
975 }
976
977 return ret;
978#else
979 return true;
wu@webrtc.orgcecfd182013-10-30 05:18:12 +0000980#endif // !IOS
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000981}
982
983bool WebRtcVoiceEngine::FindWebRtcAudioDeviceId(
984 bool is_input, const std::string& dev_name, int dev_id, int* rtc_id) {
985 // In Linux, VoiceEngine uses the same device dev_id as the device manager.
wu@webrtc.orgcecfd182013-10-30 05:18:12 +0000986#if defined(LINUX) || defined(ANDROID)
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000987 *rtc_id = dev_id;
988 return true;
989#else
990 // In Windows and Mac, we need to find the VoiceEngine device id by name
991 // unless the input dev_id is the default device id.
992 if (kDefaultAudioDeviceId == dev_id) {
993 *rtc_id = dev_id;
994 return true;
995 }
996
997 // Get the number of VoiceEngine audio devices.
998 int count = 0;
999 if (is_input) {
1000 if (-1 == voe_wrapper_->hw()->GetNumOfRecordingDevices(count)) {
1001 LOG_RTCERR0(GetNumOfRecordingDevices);
1002 return false;
1003 }
1004 } else {
1005 if (-1 == voe_wrapper_->hw()->GetNumOfPlayoutDevices(count)) {
1006 LOG_RTCERR0(GetNumOfPlayoutDevices);
1007 return false;
1008 }
1009 }
1010
1011 for (int i = 0; i < count; ++i) {
1012 char name[128];
1013 char guid[128];
1014 if (is_input) {
1015 voe_wrapper_->hw()->GetRecordingDeviceName(i, name, guid);
1016 LOG(LS_VERBOSE) << "VoiceEngine microphone " << i << ": " << name;
1017 } else {
1018 voe_wrapper_->hw()->GetPlayoutDeviceName(i, name, guid);
1019 LOG(LS_VERBOSE) << "VoiceEngine speaker " << i << ": " << name;
1020 }
1021
1022 std::string webrtc_name(name);
1023 if (dev_name.compare(0, webrtc_name.size(), webrtc_name) == 0) {
1024 *rtc_id = i;
1025 return true;
1026 }
1027 }
1028 LOG(LS_WARNING) << "VoiceEngine cannot find device: " << dev_name;
1029 return false;
1030#endif
1031}
1032
1033bool WebRtcVoiceEngine::GetOutputVolume(int* level) {
1034 unsigned int ulevel;
1035 if (voe_wrapper_->volume()->GetSpeakerVolume(ulevel) == -1) {
1036 LOG_RTCERR1(GetSpeakerVolume, level);
1037 return false;
1038 }
1039 *level = ulevel;
1040 return true;
1041}
1042
1043bool WebRtcVoiceEngine::SetOutputVolume(int level) {
henrikg91d6ede2015-09-17 00:24:34 -07001044 RTC_DCHECK(level >= 0 && level <= 255);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001045 if (voe_wrapper_->volume()->SetSpeakerVolume(level) == -1) {
1046 LOG_RTCERR1(SetSpeakerVolume, level);
1047 return false;
1048 }
1049 return true;
1050}
1051
1052int WebRtcVoiceEngine::GetInputLevel() {
1053 unsigned int ulevel;
1054 return (voe_wrapper_->volume()->GetSpeechInputLevel(ulevel) != -1) ?
1055 static_cast<int>(ulevel) : -1;
1056}
1057
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001058const std::vector<AudioCodec>& WebRtcVoiceEngine::codecs() {
1059 return codecs_;
1060}
1061
1062bool WebRtcVoiceEngine::FindCodec(const AudioCodec& in) {
1063 return FindWebRtcCodec(in, NULL);
1064}
1065
1066// Get the VoiceEngine codec that matches |in|, with the supplied settings.
1067bool WebRtcVoiceEngine::FindWebRtcCodec(const AudioCodec& in,
1068 webrtc::CodecInst* out) {
1069 int ncodecs = voe_wrapper_->codec()->NumOfCodecs();
1070 for (int i = 0; i < ncodecs; ++i) {
1071 webrtc::CodecInst voe_codec;
henrik.lundin@webrtc.org8038d422014-11-11 08:38:24 +00001072 if (GetVoeCodec(i, &voe_codec)) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001073 AudioCodec codec(voe_codec.pltype, voe_codec.plname, voe_codec.plfreq,
1074 voe_codec.rate, voe_codec.channels, 0);
1075 bool multi_rate = IsCodecMultiRate(voe_codec);
1076 // Allow arbitrary rates for ISAC to be specified.
1077 if (multi_rate) {
1078 // Set codec.bitrate to 0 so the check for codec.Matches() passes.
1079 codec.bitrate = 0;
1080 }
1081 if (codec.Matches(in)) {
1082 if (out) {
1083 // Fixup the payload type.
1084 voe_codec.pltype = in.id;
1085
1086 // Set bitrate if specified.
1087 if (multi_rate && in.bitrate != 0) {
1088 voe_codec.rate = in.bitrate;
1089 }
1090
henrik.lundin@webrtc.orgf85dbce2014-11-07 12:25:00 +00001091 // Reset G722 sample rate to 16000 to match WebRTC.
1092 MaybeFixupG722(&voe_codec, 16000);
1093
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001094 // Apply codec-specific settings.
Minyue Li7100dcd2015-03-27 05:05:59 +01001095 if (IsCodec(codec, kIsacCodecName)) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001096 // If ISAC and an explicit bitrate is not specified,
minyue@webrtc.org26236952014-10-29 02:27:08 +00001097 // enable auto bitrate adjustment.
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001098 voe_codec.rate = (in.bitrate > 0) ? in.bitrate : -1;
1099 }
1100 *out = voe_codec;
1101 }
1102 return true;
1103 }
1104 }
1105 }
1106 return false;
1107}
1108const std::vector<RtpHeaderExtension>&
1109WebRtcVoiceEngine::rtp_header_extensions() const {
1110 return rtp_header_extensions_;
1111}
1112
1113void WebRtcVoiceEngine::SetLogging(int min_sev, const char* filter) {
1114 // if min_sev == -1, we keep the current log level.
1115 if (min_sev >= 0) {
1116 SetTraceFilter(SeverityToFilter(min_sev));
1117 }
1118 log_options_ = filter;
1119 SetTraceOptions(initialized_ ? log_options_ : "");
1120}
1121
1122int WebRtcVoiceEngine::GetLastEngineError() {
1123 return voe_wrapper_->error();
1124}
1125
1126void WebRtcVoiceEngine::SetTraceFilter(int filter) {
1127 log_filter_ = filter;
1128 tracing_->SetTraceFilter(filter);
1129}
1130
1131// We suppport three different logging settings for VoiceEngine:
1132// 1. Observer callback that goes into talk diagnostic logfile.
1133// Use --logfile and --loglevel
1134//
1135// 2. Encrypted VoiceEngine log for debugging VoiceEngine.
1136// Use --voice_loglevel --voice_logfilter "tracefile file_name"
1137//
1138// 3. EC log and dump for debugging QualityEngine.
1139// Use --voice_loglevel --voice_logfilter "recordEC file_name"
1140//
1141// For more details see: "https://sites.google.com/a/google.com/wavelet/Home/
1142// Magic-Flute--RTC-Engine-/Magic-Flute-Command-Line-Parameters"
1143void WebRtcVoiceEngine::SetTraceOptions(const std::string& options) {
1144 // Set encrypted trace file.
1145 std::vector<std::string> opts;
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00001146 rtc::tokenize(options, ' ', '"', '"', &opts);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001147 std::vector<std::string>::iterator tracefile =
1148 std::find(opts.begin(), opts.end(), "tracefile");
1149 if (tracefile != opts.end() && ++tracefile != opts.end()) {
1150 // Write encrypted debug output (at same loglevel) to file
1151 // EncryptedTraceFile no longer supported.
1152 if (tracing_->SetTraceFile(tracefile->c_str()) == -1) {
1153 LOG_RTCERR1(SetTraceFile, *tracefile);
1154 }
1155 }
1156
wu@webrtc.org97077a32013-10-25 21:18:33 +00001157 // Allow trace options to override the trace filter. We default
1158 // it to log_filter_ (as a translation of libjingle log levels)
1159 // elsewhere, but this allows clients to explicitly set webrtc
1160 // log levels.
1161 std::vector<std::string>::iterator tracefilter =
1162 std::find(opts.begin(), opts.end(), "tracefilter");
1163 if (tracefilter != opts.end() && ++tracefilter != opts.end()) {
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00001164 if (!tracing_->SetTraceFilter(rtc::FromString<int>(*tracefilter))) {
wu@webrtc.org97077a32013-10-25 21:18:33 +00001165 LOG_RTCERR1(SetTraceFilter, *tracefilter);
1166 }
1167 }
1168
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001169 // Set AEC dump file
1170 std::vector<std::string>::iterator recordEC =
1171 std::find(opts.begin(), opts.end(), "recordEC");
1172 if (recordEC != opts.end()) {
1173 ++recordEC;
1174 if (recordEC != opts.end())
1175 StartAecDump(recordEC->c_str());
1176 else
1177 StopAecDump();
1178 }
1179}
1180
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001181void WebRtcVoiceEngine::Print(webrtc::TraceLevel level, const char* trace,
1182 int length) {
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00001183 rtc::LoggingSeverity sev = rtc::LS_VERBOSE;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001184 if (level == webrtc::kTraceError || level == webrtc::kTraceCritical)
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00001185 sev = rtc::LS_ERROR;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001186 else if (level == webrtc::kTraceWarning)
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00001187 sev = rtc::LS_WARNING;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001188 else if (level == webrtc::kTraceStateInfo || level == webrtc::kTraceInfo)
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00001189 sev = rtc::LS_INFO;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001190 else if (level == webrtc::kTraceTerseInfo)
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00001191 sev = rtc::LS_INFO;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001192
1193 // Skip past boilerplate prefix text
1194 if (length < 72) {
1195 std::string msg(trace, length);
1196 LOG(LS_ERROR) << "Malformed webrtc log message: ";
1197 LOG_V(sev) << msg;
1198 } else {
1199 std::string msg(trace + 71, length - 72);
Peter Boströmd5c75b12015-09-23 13:24:32 +02001200 LOG_V(sev) << "webrtc: " << msg;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001201 }
1202}
1203
solenbergd97ec302015-10-07 01:40:33 -07001204void WebRtcVoiceEngine::CallbackOnError(int channel_id, int err_code) {
1205 RTC_DCHECK(channel_id == -1);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001206 LOG(LS_WARNING) << "VoiceEngine error " << err_code << " reported on channel "
solenbergd97ec302015-10-07 01:40:33 -07001207 << channel_id << ".";
1208 rtc::CritScope lock(&channels_cs_);
1209 for (WebRtcVoiceMediaChannel* channel : channels_) {
1210 channel->OnError(err_code);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001211 }
1212}
1213
solenberg63b34542015-09-29 06:06:31 -07001214void WebRtcVoiceEngine::RegisterChannel(WebRtcVoiceMediaChannel* channel) {
solenbergd97ec302015-10-07 01:40:33 -07001215 RTC_DCHECK(channel != NULL);
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00001216 rtc::CritScope lock(&channels_cs_);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001217 channels_.push_back(channel);
1218}
1219
solenberg63b34542015-09-29 06:06:31 -07001220void WebRtcVoiceEngine::UnregisterChannel(WebRtcVoiceMediaChannel* channel) {
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00001221 rtc::CritScope lock(&channels_cs_);
solenberg63b34542015-09-29 06:06:31 -07001222 auto it = std::find(channels_.begin(), channels_.end(), channel);
1223 if (it != channels_.end()) {
1224 channels_.erase(it);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001225 }
1226}
1227
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001228// Adjusts the default AGC target level by the specified delta.
1229// NB: If we start messing with other config fields, we'll want
1230// to save the current webrtc::AgcConfig as well.
1231bool WebRtcVoiceEngine::AdjustAgcLevel(int delta) {
1232 webrtc::AgcConfig config = default_agc_config_;
1233 config.targetLeveldBOv -= delta;
1234
1235 LOG(LS_INFO) << "Adjusting AGC level from default -"
1236 << default_agc_config_.targetLeveldBOv << "dB to -"
1237 << config.targetLeveldBOv << "dB";
1238
1239 if (voe_wrapper_->processing()->SetAgcConfig(config) == -1) {
1240 LOG_RTCERR1(SetAgcConfig, config.targetLeveldBOv);
1241 return false;
1242 }
1243 return true;
1244}
1245
Fredrik Solenbergccb49e72015-05-19 11:37:56 +02001246bool WebRtcVoiceEngine::SetAudioDeviceModule(webrtc::AudioDeviceModule* adm) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001247 if (initialized_) {
1248 LOG(LS_WARNING) << "SetAudioDeviceModule can not be called after Init.";
1249 return false;
1250 }
1251 if (adm_) {
1252 adm_->Release();
1253 adm_ = NULL;
1254 }
1255 if (adm) {
1256 adm_ = adm;
1257 adm_->AddRef();
1258 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001259 return true;
1260}
1261
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00001262bool WebRtcVoiceEngine::StartAecDump(rtc::PlatformFile file) {
1263 FILE* aec_dump_file_stream = rtc::FdopenPlatformFileForWriting(file);
wu@webrtc.orga8910d22014-01-23 22:12:45 +00001264 if (!aec_dump_file_stream) {
1265 LOG(LS_ERROR) << "Could not open AEC dump file stream.";
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00001266 if (!rtc::ClosePlatformFile(file))
wu@webrtc.orga8910d22014-01-23 22:12:45 +00001267 LOG(LS_WARNING) << "Could not close file.";
1268 return false;
1269 }
wu@webrtc.orga9890802013-12-13 00:21:03 +00001270 StopAecDump();
wu@webrtc.orga8910d22014-01-23 22:12:45 +00001271 if (voe_wrapper_->processing()->StartDebugRecording(aec_dump_file_stream) !=
wu@webrtc.orga9890802013-12-13 00:21:03 +00001272 webrtc::AudioProcessing::kNoError) {
wu@webrtc.orga8910d22014-01-23 22:12:45 +00001273 LOG_RTCERR0(StartDebugRecording);
1274 fclose(aec_dump_file_stream);
wu@webrtc.orga9890802013-12-13 00:21:03 +00001275 return false;
1276 }
1277 is_dumping_aec_ = true;
1278 return true;
wu@webrtc.orga9890802013-12-13 00:21:03 +00001279}
1280
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001281void WebRtcVoiceEngine::StartAecDump(const std::string& filename) {
1282 if (!is_dumping_aec_) {
1283 // Start dumping AEC when we are not dumping.
1284 if (voe_wrapper_->processing()->StartDebugRecording(
1285 filename.c_str()) != webrtc::AudioProcessing::kNoError) {
wu@webrtc.orga9890802013-12-13 00:21:03 +00001286 LOG_RTCERR1(StartDebugRecording, filename.c_str());
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001287 } else {
1288 is_dumping_aec_ = true;
1289 }
1290 }
1291}
1292
1293void WebRtcVoiceEngine::StopAecDump() {
1294 if (is_dumping_aec_) {
1295 // Stop dumping AEC when we are dumping.
1296 if (voe_wrapper_->processing()->StopDebugRecording() !=
1297 webrtc::AudioProcessing::kNoError) {
1298 LOG_RTCERR0(StopDebugRecording);
1299 }
1300 is_dumping_aec_ = false;
1301 }
1302}
1303
ivoc112a3d82015-10-16 02:22:18 -07001304bool WebRtcVoiceEngine::StartRtcEventLog(rtc::PlatformFile file) {
1305 return voe_wrapper_->codec()->GetEventLog()->StartLogging(file);
1306}
1307
1308void WebRtcVoiceEngine::StopRtcEventLog() {
1309 voe_wrapper_->codec()->GetEventLog()->StopLogging();
1310}
1311
solenberg0a617e22015-10-20 15:49:38 -07001312int WebRtcVoiceEngine::CreateVoEChannel() {
1313 return voe_wrapper_->base()->CreateChannel(voe_config_);
sergeyu@chromium.org5bc25c42013-12-05 00:24:06 +00001314}
1315
solenbergc96df772015-10-21 13:01:53 -07001316class WebRtcVoiceMediaChannel::WebRtcAudioSendStream
mallinath@webrtc.org67ee6b92014-02-03 16:57:16 +00001317 : public AudioRenderer::Sink {
1318 public:
solenbergc96df772015-10-21 13:01:53 -07001319 WebRtcAudioSendStream(int ch, webrtc::AudioTransport* voe_audio_transport,
1320 uint32_t ssrc, webrtc::Call* call)
mallinath@webrtc.org67ee6b92014-02-03 16:57:16 +00001321 : channel_(ch),
1322 voe_audio_transport_(voe_audio_transport),
solenbergc96df772015-10-21 13:01:53 -07001323 call_(call) {
1324 RTC_DCHECK(call);
1325 webrtc::AudioSendStream::Config config(nullptr);
1326 config.voe_channel_id = channel_;
1327 config.rtp.ssrc = ssrc;
1328 stream_ = call_->CreateAudioSendStream(config);
1329 RTC_DCHECK(stream_);
1330 }
1331 ~WebRtcAudioSendStream() override {
1332 Stop();
1333 call_->DestroyAudioSendStream(stream_);
1334 }
henrike@webrtc.org1e09a712013-07-26 19:17:59 +00001335
mallinath@webrtc.org67ee6b92014-02-03 16:57:16 +00001336 // Starts the rendering by setting a sink to the renderer to get data
1337 // callback.
henrike@webrtc.orga7b98182014-02-21 15:51:43 +00001338 // This method is called on the libjingle worker thread.
mallinath@webrtc.org67ee6b92014-02-03 16:57:16 +00001339 // TODO(xians): Make sure Start() is called only once.
1340 void Start(AudioRenderer* renderer) {
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00001341 rtc::CritScope lock(&lock_);
solenbergc96df772015-10-21 13:01:53 -07001342 RTC_DCHECK(renderer);
1343 if (renderer_) {
henrikg91d6ede2015-09-17 00:24:34 -07001344 RTC_DCHECK(renderer_ == renderer);
mallinath@webrtc.org67ee6b92014-02-03 16:57:16 +00001345 return;
1346 }
mallinath@webrtc.org67ee6b92014-02-03 16:57:16 +00001347 renderer->SetSink(this);
1348 renderer_ = renderer;
1349 }
1350
solenbergc96df772015-10-21 13:01:53 -07001351 // Stops rendering by setting the sink of the renderer to nullptr. No data
mallinath@webrtc.org67ee6b92014-02-03 16:57:16 +00001352 // callback will be received after this method.
henrike@webrtc.orga7b98182014-02-21 15:51:43 +00001353 // This method is called on the libjingle worker thread.
mallinath@webrtc.org67ee6b92014-02-03 16:57:16 +00001354 void Stop() {
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00001355 rtc::CritScope lock(&lock_);
solenbergc96df772015-10-21 13:01:53 -07001356 if (renderer_) {
1357 renderer_->SetSink(nullptr);
1358 renderer_ = nullptr;
solenberg98c68862015-10-09 03:27:14 -07001359 }
mallinath@webrtc.org67ee6b92014-02-03 16:57:16 +00001360 }
1361
1362 // AudioRenderer::Sink implementation.
henrike@webrtc.orga7b98182014-02-21 15:51:43 +00001363 // This method is called on the audio thread.
kjellander@webrtc.org14665ff2015-03-04 12:58:35 +00001364 void OnData(const void* audio_data,
1365 int bits_per_sample,
1366 int sample_rate,
1367 int number_of_channels,
Peter Kastingdce40cf2015-08-24 14:52:23 -07001368 size_t number_of_frames) override {
solenbergc96df772015-10-21 13:01:53 -07001369 RTC_DCHECK(voe_audio_transport_);
henrike@webrtc.orga7b98182014-02-21 15:51:43 +00001370 voe_audio_transport_->OnData(channel_,
1371 audio_data,
1372 bits_per_sample,
1373 sample_rate,
1374 number_of_channels,
1375 number_of_frames);
henrike@webrtc.orga7b98182014-02-21 15:51:43 +00001376 }
1377
1378 // Callback from the |renderer_| when it is going away. In case Start() has
1379 // never been called, this callback won't be triggered.
kjellander@webrtc.org14665ff2015-03-04 12:58:35 +00001380 void OnClose() override {
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00001381 rtc::CritScope lock(&lock_);
solenbergc96df772015-10-21 13:01:53 -07001382 // Set |renderer_| to nullptr to make sure no more callback will get into
henrike@webrtc.orga7b98182014-02-21 15:51:43 +00001383 // the renderer.
solenbergc96df772015-10-21 13:01:53 -07001384 renderer_ = nullptr;
mallinath@webrtc.org67ee6b92014-02-03 16:57:16 +00001385 }
1386
1387 // Accessor to the VoE channel ID.
1388 int channel() const { return channel_; }
1389
1390 private:
solenbergc96df772015-10-21 13:01:53 -07001391 const int channel_ = -1;
1392 webrtc::AudioTransport* const voe_audio_transport_ = nullptr;
1393 webrtc::Call* call_ = nullptr;
1394 webrtc::AudioSendStream* stream_ = nullptr;
mallinath@webrtc.org67ee6b92014-02-03 16:57:16 +00001395
1396 // Raw pointer to AudioRenderer owned by LocalAudioTrackHandler.
1397 // PeerConnection will make sure invalidating the pointer before the object
1398 // goes away.
solenbergc96df772015-10-21 13:01:53 -07001399 AudioRenderer* renderer_ = nullptr;
henrike@webrtc.orga7b98182014-02-21 15:51:43 +00001400
1401 // Protects |renderer_| in Start(), Stop() and OnClose().
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00001402 rtc::CriticalSection lock_;
solenbergc96df772015-10-21 13:01:53 -07001403
1404 RTC_DISALLOW_IMPLICIT_CONSTRUCTORS(WebRtcAudioSendStream);
1405};
1406
1407class WebRtcVoiceMediaChannel::WebRtcAudioReceiveStream {
1408 public:
1409 explicit WebRtcAudioReceiveStream(int voe_channel_id)
1410 : channel_(voe_channel_id) {}
1411
1412 int channel() { return channel_; }
1413
1414 private:
1415 int channel_;
1416
1417 RTC_DISALLOW_IMPLICIT_CONSTRUCTORS(WebRtcAudioReceiveStream);
henrike@webrtc.org1e09a712013-07-26 19:17:59 +00001418};
1419
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001420// WebRtcVoiceMediaChannel
Fredrik Solenberg709ed672015-09-15 12:26:33 +02001421WebRtcVoiceMediaChannel::WebRtcVoiceMediaChannel(WebRtcVoiceEngine* engine,
Fredrik Solenbergb071a192015-09-17 16:42:56 +02001422 const AudioOptions& options,
Fredrik Solenberg709ed672015-09-15 12:26:33 +02001423 webrtc::Call* call)
Fredrik Solenberge444a3d2015-05-07 16:42:08 +02001424 : engine_(engine),
minyue@webrtc.org26236952014-10-29 02:27:08 +00001425 send_bitrate_setting_(false),
1426 send_bitrate_bps_(0),
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001427 options_(),
1428 dtmf_allowed_(false),
1429 desired_playout_(false),
1430 nack_enabled_(false),
1431 playout_(false),
wu@webrtc.org967bfff2013-09-19 05:49:50 +00001432 typing_noise_detected_(false),
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001433 desired_send_(SEND_NOTHING),
1434 send_(SEND_NOTHING),
solenberg1ac56142015-10-13 03:58:19 -07001435 call_(call) {
solenbergd97ec302015-10-07 01:40:33 -07001436 RTC_DCHECK(thread_checker_.CalledOnValidThread());
solenberg0a617e22015-10-20 15:49:38 -07001437 LOG(LS_VERBOSE) << "WebRtcVoiceMediaChannel::WebRtcVoiceMediaChannel";
henrikg91d6ede2015-09-17 00:24:34 -07001438 RTC_DCHECK(nullptr != call);
solenberg0a617e22015-10-20 15:49:38 -07001439 engine->RegisterChannel(this);
Fredrik Solenbergb071a192015-09-17 16:42:56 +02001440 SetOptions(options);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001441}
1442
1443WebRtcVoiceMediaChannel::~WebRtcVoiceMediaChannel() {
solenbergd97ec302015-10-07 01:40:33 -07001444 RTC_DCHECK(thread_checker_.CalledOnValidThread());
solenberg0a617e22015-10-20 15:49:38 -07001445 LOG(LS_VERBOSE) << "WebRtcVoiceMediaChannel::~WebRtcVoiceMediaChannel";
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001446
solenberg0a617e22015-10-20 15:49:38 -07001447 // Remove any remaining send streams.
solenbergc96df772015-10-21 13:01:53 -07001448 while (!send_streams_.empty()) {
1449 RemoveSendStream(send_streams_.begin()->first);
solenbergd97ec302015-10-07 01:40:33 -07001450 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001451
solenberg0a617e22015-10-20 15:49:38 -07001452 // Remove any remaining receive streams.
henrike@webrtc.org1e09a712013-07-26 19:17:59 +00001453 while (!receive_channels_.empty()) {
1454 RemoveRecvStream(receive_channels_.begin()->first);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001455 }
henrikg91d6ede2015-09-17 00:24:34 -07001456 RTC_DCHECK(receive_streams_.empty());
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001457
solenberg0a617e22015-10-20 15:49:38 -07001458 // Unregister ourselves from the engine.
1459 engine()->UnregisterChannel(this);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001460}
1461
Peter Thatcherc2ee2c82015-08-07 16:05:34 -07001462bool WebRtcVoiceMediaChannel::SetSendParameters(
1463 const AudioSendParameters& params) {
solenbergd97ec302015-10-07 01:40:33 -07001464 RTC_DCHECK(thread_checker_.CalledOnValidThread());
Peter Thatcherc2ee2c82015-08-07 16:05:34 -07001465 // TODO(pthatcher): Refactor this to be more clean now that we have
1466 // all the information at once.
1467 return (SetSendCodecs(params.codecs) &&
1468 SetSendRtpHeaderExtensions(params.extensions) &&
1469 SetMaxSendBandwidth(params.max_bandwidth_bps) &&
1470 SetOptions(params.options));
1471}
1472
1473bool WebRtcVoiceMediaChannel::SetRecvParameters(
1474 const AudioRecvParameters& params) {
solenbergd97ec302015-10-07 01:40:33 -07001475 RTC_DCHECK(thread_checker_.CalledOnValidThread());
Peter Thatcherc2ee2c82015-08-07 16:05:34 -07001476 // TODO(pthatcher): Refactor this to be more clean now that we have
1477 // all the information at once.
1478 return (SetRecvCodecs(params.codecs) &&
1479 SetRecvRtpHeaderExtensions(params.extensions));
1480}
1481
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001482bool WebRtcVoiceMediaChannel::SetOptions(const AudioOptions& options) {
solenbergd97ec302015-10-07 01:40:33 -07001483 RTC_DCHECK(thread_checker_.CalledOnValidThread());
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001484 LOG(LS_INFO) << "Setting voice channel options: "
1485 << options.ToString();
1486
wu@webrtc.orgde305012013-10-31 15:40:38 +00001487 // Check if DSCP value is changed from previous.
1488 bool dscp_option_changed = (options_.dscp != options.dscp);
1489
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001490 // We retain all of the existing options, and apply the given ones
1491 // on top. This means there is no way to "clear" options such that
1492 // they go back to the engine default.
1493 options_.SetAll(options);
1494
1495 if (send_ != SEND_NOTHING) {
solenberg63b34542015-09-29 06:06:31 -07001496 if (!engine()->ApplyOptions(options_)) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001497 LOG(LS_WARNING) <<
solenberg63b34542015-09-29 06:06:31 -07001498 "Failed to apply engine options during channel SetOptions.";
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001499 return false;
1500 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001501 }
1502
wu@webrtc.orgde305012013-10-31 15:40:38 +00001503 if (dscp_option_changed) {
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00001504 rtc::DiffServCodePoint dscp = rtc::DSCP_DEFAULT;
henrika@webrtc.orgaebb1ad2014-01-14 10:00:58 +00001505 if (options_.dscp.GetWithDefaultIfUnset(false))
wu@webrtc.orgde305012013-10-31 15:40:38 +00001506 dscp = kAudioDscpValue;
1507 if (MediaChannel::SetDscp(dscp) != 0) {
1508 LOG(LS_WARNING) << "Failed to set DSCP settings for audio channel";
1509 }
1510 }
solenberg8fb30c32015-10-13 03:06:58 -07001511
solenbergc96df772015-10-21 13:01:53 -07001512 // TODO(solenberg): Don't recreate unless options changed.
Fredrik Solenberg709ed672015-09-15 12:26:33 +02001513 RecreateAudioReceiveStreams();
solenberg8fb30c32015-10-13 03:06:58 -07001514
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001515 LOG(LS_INFO) << "Set voice channel options. Current options: "
1516 << options_.ToString();
1517 return true;
1518}
1519
1520bool WebRtcVoiceMediaChannel::SetRecvCodecs(
1521 const std::vector<AudioCodec>& codecs) {
solenberg8fb30c32015-10-13 03:06:58 -07001522 RTC_DCHECK(thread_checker_.CalledOnValidThread());
1523
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001524 // Set the payload types to be used for incoming media.
solenberg0b675462015-10-09 01:37:09 -07001525 LOG(LS_INFO) << "Setting receive voice codecs.";
solenberg0b675462015-10-09 01:37:09 -07001526
1527 if (!VerifyUniquePayloadTypes(codecs)) {
1528 LOG(LS_ERROR) << "Codec payload types overlap.";
1529 return false;
1530 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001531
1532 std::vector<AudioCodec> new_codecs;
1533 // Find all new codecs. We allow adding new codecs but don't allow changing
1534 // the payload type of codecs that is already configured since we might
1535 // already be receiving packets with that payload type.
Fredrik Solenbergaf9fb212015-08-26 10:45:53 +02001536 for (const AudioCodec& codec : codecs) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001537 AudioCodec old_codec;
Fredrik Solenbergaf9fb212015-08-26 10:45:53 +02001538 if (FindCodec(recv_codecs_, codec, &old_codec)) {
1539 if (old_codec.id != codec.id) {
1540 LOG(LS_ERROR) << codec.name << " payload type changed.";
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001541 return false;
1542 }
1543 } else {
Fredrik Solenbergaf9fb212015-08-26 10:45:53 +02001544 new_codecs.push_back(codec);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001545 }
1546 }
1547 if (new_codecs.empty()) {
1548 // There are no new codecs to configure. Already configured codecs are
1549 // never removed.
1550 return true;
1551 }
1552
1553 if (playout_) {
1554 // Receive codecs can not be changed while playing. So we temporarily
1555 // pause playout.
1556 PausePlayout();
1557 }
1558
Fredrik Solenbergaf9fb212015-08-26 10:45:53 +02001559 bool result = SetRecvCodecsInternal(new_codecs);
1560 if (result) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001561 recv_codecs_ = codecs;
1562 }
1563
1564 if (desired_playout_ && !playout_) {
1565 ResumePlayout();
1566 }
Fredrik Solenbergaf9fb212015-08-26 10:45:53 +02001567 return result;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001568}
1569
1570bool WebRtcVoiceMediaChannel::SetSendCodecs(
wu@webrtc.orgcadf9042013-08-30 21:24:16 +00001571 int channel, const std::vector<AudioCodec>& codecs) {
buildbot@webrtc.orgae740dd2014-06-17 10:56:41 +00001572 // Disable VAD, FEC, and RED unless we know the other side wants them.
wu@webrtc.orgcadf9042013-08-30 21:24:16 +00001573 engine()->voe()->codec()->SetVADStatus(channel, false);
1574 engine()->voe()->rtp()->SetNACKStatus(channel, false, 0);
buildbot@webrtc.orgae740dd2014-06-17 10:56:41 +00001575 engine()->voe()->rtp()->SetREDStatus(channel, false);
1576 engine()->voe()->codec()->SetFECStatus(channel, false);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001577
1578 // Scan through the list to figure out the codec to use for sending, along
1579 // with the proper configuration for VAD and DTMF.
henrike@webrtc.org704bf9e2014-02-27 17:52:04 +00001580 bool found_send_codec = false;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001581 webrtc::CodecInst send_codec;
1582 memset(&send_codec, 0, sizeof(send_codec));
1583
wu@webrtc.org05e7b442014-04-01 17:44:24 +00001584 bool nack_enabled = nack_enabled_;
buildbot@webrtc.org3ffa1f92014-07-02 19:51:26 +00001585 bool enable_codec_fec = false;
Minyue Li7100dcd2015-03-27 05:05:59 +01001586 bool enable_opus_dtx = false;
minyue@webrtc.org26236952014-10-29 02:27:08 +00001587 int opus_max_playback_rate = 0;
buildbot@webrtc.org5d639b32014-09-10 07:57:12 +00001588
henrike@webrtc.org704bf9e2014-02-27 17:52:04 +00001589 // Set send codec (the first non-telephone-event/CN codec)
Fredrik Solenbergaf9fb212015-08-26 10:45:53 +02001590 for (const AudioCodec& codec : codecs) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001591 // Ignore codecs we don't know about. The negotiation step should prevent
1592 // this, but double-check to be sure.
1593 webrtc::CodecInst voe_codec;
Fredrik Solenbergaf9fb212015-08-26 10:45:53 +02001594 if (!engine()->FindWebRtcCodec(codec, &voe_codec)) {
1595 LOG(LS_WARNING) << "Unknown codec " << ToString(codec);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001596 continue;
1597 }
1598
Fredrik Solenbergaf9fb212015-08-26 10:45:53 +02001599 if (IsCodec(codec, kDtmfCodecName) || IsCodec(codec, kCnCodecName)) {
henrike@webrtc.org704bf9e2014-02-27 17:52:04 +00001600 // Skip telephone-event/CN codec, which will be handled later.
1601 continue;
1602 }
1603
henrike@webrtc.org704bf9e2014-02-27 17:52:04 +00001604 // We'll use the first codec in the list to actually send audio data.
1605 // Be sure to use the payload type requested by the remote side.
buildbot@webrtc.orgae740dd2014-06-17 10:56:41 +00001606 // "red", for RED audio, is a special case where the actual codec to be
henrike@webrtc.org704bf9e2014-02-27 17:52:04 +00001607 // used is specified in params.
Fredrik Solenbergaf9fb212015-08-26 10:45:53 +02001608 if (IsCodec(codec, kRedCodecName)) {
henrike@webrtc.org704bf9e2014-02-27 17:52:04 +00001609 // Parse out the RED parameters. If we fail, just ignore RED;
1610 // we don't support all possible params/usage scenarios.
Fredrik Solenbergaf9fb212015-08-26 10:45:53 +02001611 if (!GetRedSendCodec(codec, codecs, &send_codec)) {
henrike@webrtc.org704bf9e2014-02-27 17:52:04 +00001612 continue;
1613 }
1614
1615 // Enable redundant encoding of the specified codec. Treat any
1616 // failure as a fatal internal error.
buildbot@webrtc.orgae740dd2014-06-17 10:56:41 +00001617 LOG(LS_INFO) << "Enabling RED on channel " << channel;
Fredrik Solenbergaf9fb212015-08-26 10:45:53 +02001618 if (engine()->voe()->rtp()->SetREDStatus(channel, true, codec.id) == -1) {
1619 LOG_RTCERR3(SetREDStatus, channel, true, codec.id);
henrike@webrtc.org704bf9e2014-02-27 17:52:04 +00001620 return false;
1621 }
1622 } else {
1623 send_codec = voe_codec;
Fredrik Solenbergaf9fb212015-08-26 10:45:53 +02001624 nack_enabled = IsNackEnabled(codec);
Minyue Li7100dcd2015-03-27 05:05:59 +01001625 // For Opus as the send codec, we are to determine inband FEC, maximum
1626 // playback rate, and opus internal dtx.
Fredrik Solenbergaf9fb212015-08-26 10:45:53 +02001627 if (IsCodec(codec, kOpusCodecName)) {
1628 GetOpusConfig(codec, &send_codec, &enable_codec_fec,
Minyue Li7100dcd2015-03-27 05:05:59 +01001629 &opus_max_playback_rate, &enable_opus_dtx);
buildbot@webrtc.org5d639b32014-09-10 07:57:12 +00001630 }
Brave Yao5225dd82015-03-26 07:39:19 +08001631
1632 // Set packet size if the AudioCodec param kCodecParamPTime is set.
1633 int ptime_ms = 0;
Fredrik Solenbergaf9fb212015-08-26 10:45:53 +02001634 if (codec.GetParam(kCodecParamPTime, &ptime_ms)) {
Brave Yao5225dd82015-03-26 07:39:19 +08001635 if (!SetPTimeAsPacketSize(&send_codec, ptime_ms)) {
1636 LOG(LS_WARNING) << "Failed to set packet size for codec "
1637 << send_codec.plname;
1638 return false;
1639 }
1640 }
henrike@webrtc.org704bf9e2014-02-27 17:52:04 +00001641 }
1642 found_send_codec = true;
1643 break;
1644 }
1645
wu@webrtc.org05e7b442014-04-01 17:44:24 +00001646 if (nack_enabled_ != nack_enabled) {
1647 SetNack(channel, nack_enabled);
1648 nack_enabled_ = nack_enabled;
1649 }
1650
henrike@webrtc.org704bf9e2014-02-27 17:52:04 +00001651 if (!found_send_codec) {
1652 LOG(LS_WARNING) << "Received empty list of codecs.";
1653 return false;
1654 }
1655
1656 // Set the codec immediately, since SetVADStatus() depends on whether
1657 // the current codec is mono or stereo.
1658 if (!SetSendCodec(channel, send_codec))
1659 return false;
1660
buildbot@webrtc.org3ffa1f92014-07-02 19:51:26 +00001661 // FEC should be enabled after SetSendCodec.
1662 if (enable_codec_fec) {
1663 LOG(LS_INFO) << "Attempt to enable codec internal FEC on channel "
1664 << channel;
buildbot@webrtc.org3ffa1f92014-07-02 19:51:26 +00001665 if (engine()->voe()->codec()->SetFECStatus(channel, true) == -1) {
1666 // Enable codec internal FEC. Treat any failure as fatal internal error.
1667 LOG_RTCERR2(SetFECStatus, channel, true);
1668 return false;
1669 }
buildbot@webrtc.org3ffa1f92014-07-02 19:51:26 +00001670 }
1671
Minyue Li7100dcd2015-03-27 05:05:59 +01001672 if (IsCodec(send_codec, kOpusCodecName)) {
1673 // DTX and maxplaybackrate should be set after SetSendCodec. Because current
1674 // send codec has to be Opus.
1675
1676 // Set Opus internal DTX.
1677 LOG(LS_INFO) << "Attempt to "
1678 << GetEnableString(enable_opus_dtx)
1679 << " Opus DTX on channel "
buildbot@webrtc.org5d639b32014-09-10 07:57:12 +00001680 << channel;
Minyue Li7100dcd2015-03-27 05:05:59 +01001681 if (engine()->voe()->codec()->SetOpusDtx(channel, enable_opus_dtx)) {
1682 LOG_RTCERR2(SetOpusDtx, channel, enable_opus_dtx);
1683 return false;
1684 }
1685
1686 // If opus_max_playback_rate <= 0, the default maximum playback rate
1687 // (48 kHz) will be used.
1688 if (opus_max_playback_rate > 0) {
1689 LOG(LS_INFO) << "Attempt to set maximum playback rate to "
1690 << opus_max_playback_rate
1691 << " Hz on channel "
1692 << channel;
1693 if (engine()->voe()->codec()->SetOpusMaxPlaybackRate(
1694 channel, opus_max_playback_rate) == -1) {
1695 LOG_RTCERR2(SetOpusMaxPlaybackRate, channel, opus_max_playback_rate);
1696 return false;
1697 }
buildbot@webrtc.org5d639b32014-09-10 07:57:12 +00001698 }
buildbot@webrtc.org5d639b32014-09-10 07:57:12 +00001699 }
1700
henrike@webrtc.org704bf9e2014-02-27 17:52:04 +00001701 // Always update the |send_codec_| to the currently set send codec.
1702 send_codec_.reset(new webrtc::CodecInst(send_codec));
1703
minyue@webrtc.org26236952014-10-29 02:27:08 +00001704 if (send_bitrate_setting_) {
1705 SetSendBitrateInternal(send_bitrate_bps_);
henrike@webrtc.org704bf9e2014-02-27 17:52:04 +00001706 }
1707
1708 // Loop through the codecs list again to config the telephone-event/CN codec.
Fredrik Solenbergaf9fb212015-08-26 10:45:53 +02001709 for (const AudioCodec& codec : codecs) {
henrike@webrtc.org704bf9e2014-02-27 17:52:04 +00001710 // Ignore codecs we don't know about. The negotiation step should prevent
1711 // this, but double-check to be sure.
1712 webrtc::CodecInst voe_codec;
Fredrik Solenbergaf9fb212015-08-26 10:45:53 +02001713 if (!engine()->FindWebRtcCodec(codec, &voe_codec)) {
1714 LOG(LS_WARNING) << "Unknown codec " << ToString(codec);
henrike@webrtc.org704bf9e2014-02-27 17:52:04 +00001715 continue;
1716 }
1717
wu@webrtc.org9dba5252013-08-05 20:36:57 +00001718 // Find the DTMF telephone event "codec" and tell VoiceEngine channels
1719 // about it.
Fredrik Solenbergaf9fb212015-08-26 10:45:53 +02001720 if (IsCodec(codec, kDtmfCodecName)) {
wu@webrtc.orgcadf9042013-08-30 21:24:16 +00001721 if (engine()->voe()->dtmf()->SetSendTelephoneEventPayloadType(
Fredrik Solenbergaf9fb212015-08-26 10:45:53 +02001722 channel, codec.id) == -1) {
1723 LOG_RTCERR2(SetSendTelephoneEventPayloadType, channel, codec.id);
wu@webrtc.orgcadf9042013-08-30 21:24:16 +00001724 return false;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001725 }
Fredrik Solenbergaf9fb212015-08-26 10:45:53 +02001726 } else if (IsCodec(codec, kCnCodecName)) {
henrike@webrtc.org704bf9e2014-02-27 17:52:04 +00001727 // Turn voice activity detection/comfort noise on if supported.
1728 // Set the wideband CN payload type appropriately.
1729 // (narrowband always uses the static payload type 13).
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001730 webrtc::PayloadFrequencies cn_freq;
Fredrik Solenbergaf9fb212015-08-26 10:45:53 +02001731 switch (codec.clockrate) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001732 case 8000:
1733 cn_freq = webrtc::kFreq8000Hz;
1734 break;
1735 case 16000:
1736 cn_freq = webrtc::kFreq16000Hz;
1737 break;
1738 case 32000:
1739 cn_freq = webrtc::kFreq32000Hz;
1740 break;
1741 default:
Fredrik Solenbergaf9fb212015-08-26 10:45:53 +02001742 LOG(LS_WARNING) << "CN frequency " << codec.clockrate
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001743 << " not supported.";
1744 continue;
1745 }
wu@webrtc.orgcadf9042013-08-30 21:24:16 +00001746 // Set the CN payloadtype and the VAD status.
1747 // The CN payload type for 8000 Hz clockrate is fixed at 13.
1748 if (cn_freq != webrtc::kFreq8000Hz) {
1749 if (engine()->voe()->codec()->SetSendCNPayloadType(
Fredrik Solenbergaf9fb212015-08-26 10:45:53 +02001750 channel, codec.id, cn_freq) == -1) {
1751 LOG_RTCERR3(SetSendCNPayloadType, channel, codec.id, cn_freq);
wu@webrtc.orgcadf9042013-08-30 21:24:16 +00001752 // TODO(ajm): This failure condition will be removed from VoE.
1753 // Restore the return here when we update to a new enough webrtc.
1754 //
1755 // Not returning false because the SetSendCNPayloadType will fail if
1756 // the channel is already sending.
1757 // This can happen if the remote description is applied twice, for
1758 // example in the case of ROAP on top of JSEP, where both side will
1759 // send the offer.
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001760 }
wu@webrtc.orgcadf9042013-08-30 21:24:16 +00001761 }
wu@webrtc.orgcadf9042013-08-30 21:24:16 +00001762 // Only turn on VAD if we have a CN payload type that matches the
1763 // clockrate for the codec we are going to use.
Fredrik Solenbergaf9fb212015-08-26 10:45:53 +02001764 if (codec.clockrate == send_codec.plfreq && send_codec.channels != 2) {
Minyue Li7100dcd2015-03-27 05:05:59 +01001765 // TODO(minyue): If CN frequency == 48000 Hz is allowed, consider the
1766 // interaction between VAD and Opus FEC.
wu@webrtc.orgcadf9042013-08-30 21:24:16 +00001767 LOG(LS_INFO) << "Enabling VAD";
1768 if (engine()->voe()->codec()->SetVADStatus(channel, true) == -1) {
1769 LOG_RTCERR2(SetVADStatus, channel, true);
1770 return false;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001771 }
1772 }
1773 }
wu@webrtc.org1d1ffc92013-10-16 18:12:02 +00001774 }
wu@webrtc.orgcadf9042013-08-30 21:24:16 +00001775 return true;
1776}
1777
1778bool WebRtcVoiceMediaChannel::SetSendCodecs(
1779 const std::vector<AudioCodec>& codecs) {
solenbergd97ec302015-10-07 01:40:33 -07001780 RTC_DCHECK(thread_checker_.CalledOnValidThread());
1781
wu@webrtc.orgcadf9042013-08-30 21:24:16 +00001782 dtmf_allowed_ = false;
Fredrik Solenbergaf9fb212015-08-26 10:45:53 +02001783 for (const AudioCodec& codec : codecs) {
wu@webrtc.orgcadf9042013-08-30 21:24:16 +00001784 // Find the DTMF telephone event "codec".
Fredrik Solenbergaf9fb212015-08-26 10:45:53 +02001785 if (IsCodec(codec, kDtmfCodecName)) {
wu@webrtc.orgcadf9042013-08-30 21:24:16 +00001786 dtmf_allowed_ = true;
1787 }
1788 }
1789
1790 // Cache the codecs in order to configure the channel created later.
1791 send_codecs_ = codecs;
solenbergc96df772015-10-21 13:01:53 -07001792 for (const auto& ch : send_streams_) {
Fredrik Solenbergaf9fb212015-08-26 10:45:53 +02001793 if (!SetSendCodecs(ch.second->channel(), codecs)) {
wu@webrtc.orgcadf9042013-08-30 21:24:16 +00001794 return false;
1795 }
1796 }
1797
wu@webrtc.org05e7b442014-04-01 17:44:24 +00001798 // Set nack status on receive channels and update |nack_enabled_|.
solenberg0a617e22015-10-20 15:49:38 -07001799 for (const auto& ch : receive_channels_) {
1800 SetNack(ch.second->channel(), nack_enabled_);
wu@webrtc.org9dba5252013-08-05 20:36:57 +00001801 }
solenberg0a617e22015-10-20 15:49:38 -07001802
1803 return true;
wu@webrtc.org9dba5252013-08-05 20:36:57 +00001804}
1805
wu@webrtc.orgcadf9042013-08-30 21:24:16 +00001806void WebRtcVoiceMediaChannel::SetNack(int channel, bool nack_enabled) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001807 if (nack_enabled) {
wu@webrtc.orgcadf9042013-08-30 21:24:16 +00001808 LOG(LS_INFO) << "Enabling NACK for channel " << channel;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001809 engine()->voe()->rtp()->SetNACKStatus(channel, true, kNackMaxPackets);
1810 } else {
wu@webrtc.orgcadf9042013-08-30 21:24:16 +00001811 LOG(LS_INFO) << "Disabling NACK for channel " << channel;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001812 engine()->voe()->rtp()->SetNACKStatus(channel, false, 0);
1813 }
1814}
1815
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001816bool WebRtcVoiceMediaChannel::SetSendCodec(
wu@webrtc.org9dba5252013-08-05 20:36:57 +00001817 int channel, const webrtc::CodecInst& send_codec) {
1818 LOG(LS_INFO) << "Send channel " << channel << " selected voice codec "
1819 << ToString(send_codec) << ", bitrate=" << send_codec.rate;
1820
wu@webrtc.org05e7b442014-04-01 17:44:24 +00001821 webrtc::CodecInst current_codec;
1822 if (engine()->voe()->codec()->GetSendCodec(channel, current_codec) == 0 &&
1823 (send_codec == current_codec)) {
1824 // Codec is already configured, we can return without setting it again.
1825 return true;
1826 }
1827
wu@webrtc.org9dba5252013-08-05 20:36:57 +00001828 if (engine()->voe()->codec()->SetSendCodec(channel, send_codec) == -1) {
1829 LOG_RTCERR2(SetSendCodec, channel, ToString(send_codec));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001830 return false;
1831 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001832 return true;
1833}
1834
1835bool WebRtcVoiceMediaChannel::SetRecvRtpHeaderExtensions(
1836 const std::vector<RtpHeaderExtension>& extensions) {
solenbergd97ec302015-10-07 01:40:33 -07001837 RTC_DCHECK(thread_checker_.CalledOnValidThread());
buildbot@webrtc.org150835e2014-05-06 15:54:38 +00001838 if (receive_extensions_ == extensions) {
1839 return true;
1840 }
1841
Fredrik Solenbergaf9fb212015-08-26 10:45:53 +02001842 for (const auto& ch : receive_channels_) {
1843 if (!SetChannelRecvRtpHeaderExtensions(ch.second->channel(), extensions)) {
henrike@webrtc.org79047f92014-03-06 23:46:59 +00001844 return false;
1845 }
1846 }
buildbot@webrtc.org150835e2014-05-06 15:54:38 +00001847
1848 receive_extensions_ = extensions;
Fredrik Solenberg4b60c732015-05-07 14:07:48 +02001849
1850 // Recreate AudioReceiveStream:s.
1851 {
1852 std::vector<webrtc::RtpExtension> exts;
1853
1854 const RtpHeaderExtension* audio_level_extension =
1855 FindHeaderExtension(extensions, kRtpAudioLevelHeaderExtension);
1856 if (audio_level_extension) {
1857 exts.push_back({
1858 kRtpAudioLevelHeaderExtension, audio_level_extension->id});
1859 }
1860
1861 const RtpHeaderExtension* send_time_extension =
1862 FindHeaderExtension(extensions, kRtpAbsoluteSenderTimeHeaderExtension);
1863 if (send_time_extension) {
1864 exts.push_back({
1865 kRtpAbsoluteSenderTimeHeaderExtension, send_time_extension->id});
1866 }
1867
1868 recv_rtp_extensions_.swap(exts);
Fredrik Solenberg709ed672015-09-15 12:26:33 +02001869 RecreateAudioReceiveStreams();
Fredrik Solenberg4b60c732015-05-07 14:07:48 +02001870 }
1871
buildbot@webrtc.org150835e2014-05-06 15:54:38 +00001872 return true;
1873}
1874
1875bool WebRtcVoiceMediaChannel::SetChannelRecvRtpHeaderExtensions(
1876 int channel_id, const std::vector<RtpHeaderExtension>& extensions) {
buildbot@webrtc.org150835e2014-05-06 15:54:38 +00001877 const RtpHeaderExtension* audio_level_extension =
1878 FindHeaderExtension(extensions, kRtpAudioLevelHeaderExtension);
1879 if (!SetHeaderExtension(
1880 &webrtc::VoERTP_RTCP::SetReceiveAudioLevelIndicationStatus, channel_id,
1881 audio_level_extension)) {
1882 return false;
1883 }
buildbot@webrtc.org150835e2014-05-06 15:54:38 +00001884
1885 const RtpHeaderExtension* send_time_extension =
1886 FindHeaderExtension(extensions, kRtpAbsoluteSenderTimeHeaderExtension);
1887 if (!SetHeaderExtension(
1888 &webrtc::VoERTP_RTCP::SetReceiveAbsoluteSenderTimeStatus, channel_id,
1889 send_time_extension)) {
1890 return false;
1891 }
Fredrik Solenberg4b60c732015-05-07 14:07:48 +02001892
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001893 return true;
1894}
1895
1896bool WebRtcVoiceMediaChannel::SetSendRtpHeaderExtensions(
1897 const std::vector<RtpHeaderExtension>& extensions) {
solenbergd97ec302015-10-07 01:40:33 -07001898 RTC_DCHECK(thread_checker_.CalledOnValidThread());
buildbot@webrtc.org150835e2014-05-06 15:54:38 +00001899 if (send_extensions_ == extensions) {
1900 return true;
1901 }
1902
solenbergc96df772015-10-21 13:01:53 -07001903 for (const auto& ch : send_streams_) {
Fredrik Solenbergaf9fb212015-08-26 10:45:53 +02001904 if (!SetChannelSendRtpHeaderExtensions(ch.second->channel(), extensions)) {
buildbot@webrtc.org150835e2014-05-06 15:54:38 +00001905 return false;
1906 }
1907 }
1908
1909 send_extensions_ = extensions;
1910 return true;
1911}
1912
1913bool WebRtcVoiceMediaChannel::SetChannelSendRtpHeaderExtensions(
1914 int channel_id, const std::vector<RtpHeaderExtension>& extensions) {
henrike@webrtc.org79047f92014-03-06 23:46:59 +00001915 const RtpHeaderExtension* audio_level_extension =
1916 FindHeaderExtension(extensions, kRtpAudioLevelHeaderExtension);
buildbot@webrtc.org150835e2014-05-06 15:54:38 +00001917
henrike@webrtc.org79047f92014-03-06 23:46:59 +00001918 if (!SetHeaderExtension(
buildbot@webrtc.org150835e2014-05-06 15:54:38 +00001919 &webrtc::VoERTP_RTCP::SetSendAudioLevelIndicationStatus, channel_id,
henrike@webrtc.org79047f92014-03-06 23:46:59 +00001920 audio_level_extension)) {
1921 return false;
1922 }
buildbot@webrtc.org150835e2014-05-06 15:54:38 +00001923
1924 const RtpHeaderExtension* send_time_extension =
1925 FindHeaderExtension(extensions, kRtpAbsoluteSenderTimeHeaderExtension);
henrike@webrtc.org79047f92014-03-06 23:46:59 +00001926 if (!SetHeaderExtension(
buildbot@webrtc.org150835e2014-05-06 15:54:38 +00001927 &webrtc::VoERTP_RTCP::SetSendAbsoluteSenderTimeStatus, channel_id,
henrike@webrtc.org79047f92014-03-06 23:46:59 +00001928 send_time_extension)) {
1929 return false;
1930 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001931
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001932 return true;
1933}
1934
1935bool WebRtcVoiceMediaChannel::SetPlayout(bool playout) {
1936 desired_playout_ = playout;
1937 return ChangePlayout(desired_playout_);
1938}
1939
1940bool WebRtcVoiceMediaChannel::PausePlayout() {
1941 return ChangePlayout(false);
1942}
1943
1944bool WebRtcVoiceMediaChannel::ResumePlayout() {
1945 return ChangePlayout(desired_playout_);
1946}
1947
1948bool WebRtcVoiceMediaChannel::ChangePlayout(bool playout) {
solenbergd97ec302015-10-07 01:40:33 -07001949 RTC_DCHECK(thread_checker_.CalledOnValidThread());
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001950 if (playout_ == playout) {
1951 return true;
1952 }
1953
Fredrik Solenbergaf9fb212015-08-26 10:45:53 +02001954 for (const auto& ch : receive_channels_) {
1955 if (!SetPlayout(ch.second->channel(), playout)) {
henrike@webrtc.org1e09a712013-07-26 19:17:59 +00001956 LOG(LS_ERROR) << "SetPlayout " << playout << " on channel "
Fredrik Solenbergaf9fb212015-08-26 10:45:53 +02001957 << ch.second->channel() << " failed";
solenberg1ac56142015-10-13 03:58:19 -07001958 return false;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001959 }
1960 }
solenberg1ac56142015-10-13 03:58:19 -07001961 playout_ = playout;
1962 return true;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001963}
1964
1965bool WebRtcVoiceMediaChannel::SetSend(SendFlags send) {
1966 desired_send_ = send;
solenbergc96df772015-10-21 13:01:53 -07001967 if (!send_streams_.empty()) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001968 return ChangeSend(desired_send_);
solenbergc96df772015-10-21 13:01:53 -07001969 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001970 return true;
1971}
1972
1973bool WebRtcVoiceMediaChannel::PauseSend() {
1974 return ChangeSend(SEND_NOTHING);
1975}
1976
1977bool WebRtcVoiceMediaChannel::ResumeSend() {
1978 return ChangeSend(desired_send_);
1979}
1980
1981bool WebRtcVoiceMediaChannel::ChangeSend(SendFlags send) {
1982 if (send_ == send) {
1983 return true;
1984 }
1985
solenberg63b34542015-09-29 06:06:31 -07001986 // Apply channel specific options.
1987 if (send == SEND_MICROPHONE) {
1988 engine()->ApplyOptions(options_);
1989 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001990
wu@webrtc.org9dba5252013-08-05 20:36:57 +00001991 // Change the settings on each send channel.
solenbergc96df772015-10-21 13:01:53 -07001992 for (const auto& ch : send_streams_) {
solenberg63b34542015-09-29 06:06:31 -07001993 if (!ChangeSend(ch.second->channel(), send)) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001994 return false;
solenberg63b34542015-09-29 06:06:31 -07001995 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001996 }
wu@webrtc.org9dba5252013-08-05 20:36:57 +00001997
solenberg63b34542015-09-29 06:06:31 -07001998 // Clear up the options after stopping sending. Since we may previously have
1999 // applied the channel specific options, now apply the original options stored
2000 // in WebRtcVoiceEngine.
2001 if (send == SEND_NOTHING) {
2002 engine()->ApplyOptions(engine()->GetOptions());
2003 }
wu@webrtc.org9dba5252013-08-05 20:36:57 +00002004
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002005 send_ = send;
2006 return true;
2007}
2008
wu@webrtc.org9dba5252013-08-05 20:36:57 +00002009bool WebRtcVoiceMediaChannel::ChangeSend(int channel, SendFlags send) {
2010 if (send == SEND_MICROPHONE) {
2011 if (engine()->voe()->base()->StartSend(channel) == -1) {
2012 LOG_RTCERR1(StartSend, channel);
2013 return false;
2014 }
wu@webrtc.org9dba5252013-08-05 20:36:57 +00002015 } else { // SEND_NOTHING
henrikg91d6ede2015-09-17 00:24:34 -07002016 RTC_DCHECK(send == SEND_NOTHING);
wu@webrtc.org9dba5252013-08-05 20:36:57 +00002017 if (engine()->voe()->base()->StopSend(channel) == -1) {
2018 LOG_RTCERR1(StopSend, channel);
2019 return false;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002020 }
2021 }
2022
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002023 return true;
2024}
2025
Peter Boström0c4e06b2015-10-07 12:23:21 +02002026bool WebRtcVoiceMediaChannel::SetAudioSend(uint32_t ssrc,
2027 bool enable,
solenberg1dd98f32015-09-10 01:57:14 -07002028 const AudioOptions* options,
2029 AudioRenderer* renderer) {
solenbergd97ec302015-10-07 01:40:33 -07002030 RTC_DCHECK(thread_checker_.CalledOnValidThread());
solenberg1dd98f32015-09-10 01:57:14 -07002031 // TODO(solenberg): The state change should be fully rolled back if any one of
2032 // these calls fail.
2033 if (!SetLocalRenderer(ssrc, renderer)) {
2034 return false;
2035 }
solenbergdfc8f4f2015-10-01 02:31:10 -07002036 if (!MuteStream(ssrc, !enable)) {
solenberg1dd98f32015-09-10 01:57:14 -07002037 return false;
2038 }
solenbergdfc8f4f2015-10-01 02:31:10 -07002039 if (enable && options) {
solenberg1dd98f32015-09-10 01:57:14 -07002040 return SetOptions(*options);
2041 }
2042 return true;
2043}
2044
solenberg0a617e22015-10-20 15:49:38 -07002045int WebRtcVoiceMediaChannel::CreateVoEChannel() {
2046 int id = engine()->CreateVoEChannel();
2047 if (id == -1) {
2048 LOG_RTCERR0(CreateVoEChannel);
2049 return -1;
wu@webrtc.org9dba5252013-08-05 20:36:57 +00002050 }
solenberg0a617e22015-10-20 15:49:38 -07002051 if (engine()->voe()->network()->RegisterExternalTransport(id, *this) == -1) {
2052 LOG_RTCERR2(RegisterExternalTransport, id, this);
2053 engine()->voe()->base()->DeleteChannel(id);
2054 return -1;
2055 }
2056 return id;
wu@webrtc.org9dba5252013-08-05 20:36:57 +00002057}
2058
2059bool WebRtcVoiceMediaChannel::DeleteChannel(int channel) {
2060 if (engine()->voe()->network()->DeRegisterExternalTransport(channel) == -1) {
2061 LOG_RTCERR1(DeRegisterExternalTransport, channel);
2062 }
wu@webrtc.org9dba5252013-08-05 20:36:57 +00002063 if (engine()->voe()->base()->DeleteChannel(channel) == -1) {
2064 LOG_RTCERR1(DeleteChannel, channel);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002065 return false;
2066 }
wu@webrtc.org9dba5252013-08-05 20:36:57 +00002067 return true;
2068}
henrike@webrtc.org1e09a712013-07-26 19:17:59 +00002069
wu@webrtc.org9dba5252013-08-05 20:36:57 +00002070bool WebRtcVoiceMediaChannel::AddSendStream(const StreamParams& sp) {
solenbergd97ec302015-10-07 01:40:33 -07002071 RTC_DCHECK(thread_checker_.CalledOnValidThread());
solenberg0a617e22015-10-20 15:49:38 -07002072 LOG(LS_INFO) << "AddSendStream: " << sp.ToString();
2073
2074 uint32_t ssrc = sp.first_ssrc();
2075 RTC_DCHECK(0 != ssrc);
2076
2077 if (GetSendChannelId(ssrc) != -1) {
2078 LOG(LS_ERROR) << "Stream already exists with ssrc " << ssrc;
wu@webrtc.org9dba5252013-08-05 20:36:57 +00002079 return false;
2080 }
2081
solenberg0a617e22015-10-20 15:49:38 -07002082 // Create a new channel for sending audio data.
2083 int channel = CreateVoEChannel();
2084 if (channel == -1) {
2085 return false;
wu@webrtc.org9dba5252013-08-05 20:36:57 +00002086 }
wu@webrtc.org9dba5252013-08-05 20:36:57 +00002087
solenberg0a617e22015-10-20 15:49:38 -07002088 // Enable RTCP (for quality stats and feedback messages).
2089 if (engine()->voe()->rtp()->SetRTCPStatus(channel, true) == -1) {
2090 LOG_RTCERR2(SetRTCPStatus, channel, 1);
2091 }
2092
2093 SetChannelSendRtpHeaderExtensions(channel, send_extensions_);
2094
2095 // Set the local (send) SSRC.
2096 if (engine()->voe()->rtp()->SetLocalSSRC(channel, ssrc) == -1) {
2097 LOG_RTCERR2(SetLocalSSRC, channel, ssrc);
2098 DeleteChannel(channel);
2099 return false;
2100 }
2101
2102 if (engine()->voe()->rtp()->SetRTCP_CNAME(channel, sp.cname.c_str()) == -1) {
2103 LOG_RTCERR2(SetRTCP_CNAME, channel, sp.cname);
2104 DeleteChannel(channel);
2105 return false;
wu@webrtc.org9dba5252013-08-05 20:36:57 +00002106 }
2107
solenbergc96df772015-10-21 13:01:53 -07002108 // Save the channel to send_streams_, so that RemoveSendStream() can still
wu@webrtc.org9dba5252013-08-05 20:36:57 +00002109 // delete the channel in case failure happens below.
mallinath@webrtc.org67ee6b92014-02-03 16:57:16 +00002110 webrtc::AudioTransport* audio_transport =
2111 engine()->voe()->base()->audio_transport();
solenbergc96df772015-10-21 13:01:53 -07002112 send_streams_.insert(
solenberg0a617e22015-10-20 15:49:38 -07002113 std::make_pair(ssrc,
solenbergc96df772015-10-21 13:01:53 -07002114 new WebRtcAudioSendStream(channel, audio_transport, ssrc, call_)));
wu@webrtc.org9dba5252013-08-05 20:36:57 +00002115
solenberg0a617e22015-10-20 15:49:38 -07002116 // Set the current codecs to be used for the new channel. We need to do this
2117 // after adding the channel to send_channels_, because of how max bitrate is
2118 // currently being configured by SetSendCodec().
2119 if (!send_codecs_.empty() && !SetSendCodecs(channel, send_codecs_)) {
2120 RemoveSendStream(ssrc);
wu@webrtc.org9dba5252013-08-05 20:36:57 +00002121 return false;
2122 }
2123
2124 // At this point the channel's local SSRC has been updated. If the channel is
solenberg0a617e22015-10-20 15:49:38 -07002125 // the first send channel make sure that all the receive channels are updated
2126 // with the same SSRC in order to send receiver reports.
solenbergc96df772015-10-21 13:01:53 -07002127 if (send_streams_.size() == 1) {
solenberg0a617e22015-10-20 15:49:38 -07002128 receiver_reports_ssrc_ = ssrc;
Fredrik Solenbergaf9fb212015-08-26 10:45:53 +02002129 for (const auto& ch : receive_channels_) {
solenberg0a617e22015-10-20 15:49:38 -07002130 int recv_channel = ch.second->channel();
2131 if (engine()->voe()->rtp()->SetLocalSSRC(recv_channel, ssrc) != 0) {
2132 LOG_RTCERR2(SetLocalSSRC, ch.second->channel(), ssrc);
solenberg1ac56142015-10-13 03:58:19 -07002133 return false;
wu@webrtc.org9dba5252013-08-05 20:36:57 +00002134 }
solenberg0a617e22015-10-20 15:49:38 -07002135 engine()->voe()->base()->AssociateSendChannel(recv_channel, channel);
2136 LOG(LS_INFO) << "VoiceEngine channel #" << recv_channel
2137 << " is associated with channel #" << channel << ".";
wu@webrtc.org9dba5252013-08-05 20:36:57 +00002138 }
2139 }
2140
wu@webrtc.org9dba5252013-08-05 20:36:57 +00002141 return ChangeSend(channel, desired_send_);
2142}
2143
Peter Boström0c4e06b2015-10-07 12:23:21 +02002144bool WebRtcVoiceMediaChannel::RemoveSendStream(uint32_t ssrc) {
solenberg0a617e22015-10-20 15:49:38 -07002145 RTC_DCHECK(thread_checker_.CalledOnValidThread());
solenbergc96df772015-10-21 13:01:53 -07002146 auto it = send_streams_.find(ssrc);
2147 if (it == send_streams_.end()) {
wu@webrtc.org9dba5252013-08-05 20:36:57 +00002148 LOG(LS_WARNING) << "Try to remove stream with ssrc " << ssrc
2149 << " which doesn't exist.";
2150 return false;
2151 }
2152
mallinath@webrtc.org67ee6b92014-02-03 16:57:16 +00002153 int channel = it->second->channel();
wu@webrtc.org9dba5252013-08-05 20:36:57 +00002154 ChangeSend(channel, SEND_NOTHING);
2155
mallinath@webrtc.org67ee6b92014-02-03 16:57:16 +00002156 // Delete the WebRtcVoiceChannelRenderer object connected to the channel,
2157 // this will disconnect the audio renderer with the send channel.
2158 delete it->second;
solenbergc96df772015-10-21 13:01:53 -07002159 send_streams_.erase(it);
wu@webrtc.org9dba5252013-08-05 20:36:57 +00002160
solenberg0a617e22015-10-20 15:49:38 -07002161 // Clean up and delete the send channel.
2162 LOG(LS_INFO) << "Removing audio send stream " << ssrc
2163 << " with VoiceEngine channel #" << channel << ".";
2164 if (!DeleteChannel(channel)) {
2165 return false;
wu@webrtc.org9dba5252013-08-05 20:36:57 +00002166 }
solenbergc96df772015-10-21 13:01:53 -07002167 if (send_streams_.empty()) {
wu@webrtc.org9dba5252013-08-05 20:36:57 +00002168 ChangeSend(SEND_NOTHING);
solenberg0a617e22015-10-20 15:49:38 -07002169 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002170 return true;
2171}
2172
2173bool WebRtcVoiceMediaChannel::AddRecvStream(const StreamParams& sp) {
henrikg91d6ede2015-09-17 00:24:34 -07002174 RTC_DCHECK(thread_checker_.CalledOnValidThread());
solenbergd97ec302015-10-07 01:40:33 -07002175 LOG(LS_INFO) << "AddRecvStream: " << sp.ToString();
2176
solenberg0b675462015-10-09 01:37:09 -07002177 if (!ValidateStreamParams(sp)) {
wu@webrtc.org78187522013-10-07 23:32:02 +00002178 return false;
2179 }
2180
solenberg0b675462015-10-09 01:37:09 -07002181 uint32_t ssrc = sp.first_ssrc();
2182 if (ssrc == 0) {
2183 LOG(LS_WARNING) << "AddRecvStream with ssrc==0 is not supported.";
2184 return false;
2185 }
2186
solenberg1ac56142015-10-13 03:58:19 -07002187 // Remove the default receive stream if one had been created with this ssrc;
2188 // we'll recreate it then.
2189 if (IsDefaultRecvStream(ssrc)) {
2190 RemoveRecvStream(ssrc);
2191 }
solenberg0b675462015-10-09 01:37:09 -07002192
henrike@webrtc.org1e09a712013-07-26 19:17:59 +00002193 if (receive_channels_.find(ssrc) != receive_channels_.end()) {
2194 LOG(LS_ERROR) << "Stream already exists with ssrc " << ssrc;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002195 return false;
2196 }
henrikg91d6ede2015-09-17 00:24:34 -07002197 RTC_DCHECK(receive_stream_params_.find(ssrc) == receive_stream_params_.end());
Fredrik Solenberg4b60c732015-05-07 14:07:48 +02002198
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002199 // Create a new channel for receiving audio data.
solenberg0a617e22015-10-20 15:49:38 -07002200 int channel = CreateVoEChannel();
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002201 if (channel == -1) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002202 return false;
2203 }
wu@webrtc.org78187522013-10-07 23:32:02 +00002204 if (!ConfigureRecvChannel(channel)) {
2205 DeleteChannel(channel);
2206 return false;
2207 }
2208
solenbergc96df772015-10-21 13:01:53 -07002209 WebRtcAudioReceiveStream* stream = new WebRtcAudioReceiveStream(channel);
2210 receive_channels_.insert(std::make_pair(ssrc, stream));
pbos8fc7fa72015-07-15 08:02:58 -07002211 receive_stream_params_[ssrc] = sp;
Fredrik Solenberg709ed672015-09-15 12:26:33 +02002212 AddAudioReceiveStream(ssrc);
wu@webrtc.org78187522013-10-07 23:32:02 +00002213
2214 LOG(LS_INFO) << "New audio stream " << ssrc
2215 << " registered to VoiceEngine channel #"
2216 << channel << ".";
2217 return true;
2218}
2219
2220bool WebRtcVoiceMediaChannel::ConfigureRecvChannel(int channel) {
solenbergd97ec302015-10-07 01:40:33 -07002221 RTC_DCHECK(thread_checker_.CalledOnValidThread());
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002222
solenberg0a617e22015-10-20 15:49:38 -07002223 int send_channel = GetSendChannelId(receiver_reports_ssrc_);
2224 if (send_channel != -1) {
2225 // Associate receive channel with first send channel (so the receive channel
2226 // can obtain RTT from the send channel)
2227 engine()->voe()->base()->AssociateSendChannel(channel, send_channel);
2228 LOG(LS_INFO) << "VoiceEngine channel #" << channel
2229 << " is associated with channel #" << send_channel << ".";
2230 }
2231 if (engine()->voe()->rtp()->SetLocalSSRC(channel,
2232 receiver_reports_ssrc_) == -1) {
2233 LOG_RTCERR1(SetLocalSSRC, channel);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002234 return false;
2235 }
Minyue2013aec2015-05-13 14:14:42 +02002236
solenberg1ac56142015-10-13 03:58:19 -07002237 // Turn off all supported codecs.
2238 int ncodecs = engine()->voe()->codec()->NumOfCodecs();
2239 for (int i = 0; i < ncodecs; ++i) {
2240 webrtc::CodecInst voe_codec;
2241 if (engine()->voe()->codec()->GetCodec(i, voe_codec) != -1) {
2242 voe_codec.pltype = -1;
2243 if (engine()->voe()->codec()->SetRecPayloadType(
2244 channel, voe_codec) == -1) {
2245 LOG_RTCERR2(SetRecPayloadType, channel, ToString(voe_codec));
2246 return false;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002247 }
2248 }
2249 }
2250
solenberg1ac56142015-10-13 03:58:19 -07002251 // Only enable those configured for this channel.
2252 for (const auto& codec : recv_codecs_) {
2253 webrtc::CodecInst voe_codec;
2254 if (engine()->FindWebRtcCodec(codec, &voe_codec)) {
2255 voe_codec.pltype = codec.id;
2256 if (engine()->voe()->codec()->SetRecPayloadType(
2257 channel, voe_codec) == -1) {
2258 LOG_RTCERR2(SetRecPayloadType, channel, ToString(voe_codec));
2259 return false;
2260 }
henrike@webrtc.org1e09a712013-07-26 19:17:59 +00002261 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002262 }
solenberg8fb30c32015-10-13 03:06:58 -07002263
wu@webrtc.orgcadf9042013-08-30 21:24:16 +00002264 SetNack(channel, nack_enabled_);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002265
buildbot@webrtc.org150835e2014-05-06 15:54:38 +00002266 // Set RTP header extension for the new channel.
2267 if (!SetChannelRecvRtpHeaderExtensions(channel, receive_extensions_)) {
2268 return false;
2269 }
2270
solenberg1ac56142015-10-13 03:58:19 -07002271 SetPlayout(channel, playout_);
2272 return true;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002273}
2274
Peter Boström0c4e06b2015-10-07 12:23:21 +02002275bool WebRtcVoiceMediaChannel::RemoveRecvStream(uint32_t ssrc) {
henrikg91d6ede2015-09-17 00:24:34 -07002276 RTC_DCHECK(thread_checker_.CalledOnValidThread());
solenbergd97ec302015-10-07 01:40:33 -07002277 LOG(LS_INFO) << "RemoveRecvStream: " << ssrc;
2278
solenbergc96df772015-10-21 13:01:53 -07002279 auto it = receive_channels_.find(ssrc);
wu@webrtc.org9dba5252013-08-05 20:36:57 +00002280 if (it == receive_channels_.end()) {
2281 LOG(LS_WARNING) << "Try to remove stream with ssrc " << ssrc
2282 << " which doesn't exist.";
henrike@webrtc.org1e09a712013-07-26 19:17:59 +00002283 return false;
wu@webrtc.org9dba5252013-08-05 20:36:57 +00002284 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002285
Fredrik Solenberg709ed672015-09-15 12:26:33 +02002286 RemoveAudioReceiveStream(ssrc);
pbos8fc7fa72015-07-15 08:02:58 -07002287 receive_stream_params_.erase(ssrc);
Fredrik Solenberg4b60c732015-05-07 14:07:48 +02002288
mallinath@webrtc.org67ee6b92014-02-03 16:57:16 +00002289 const int channel = it->second->channel();
2290 delete it->second;
2291 receive_channels_.erase(it);
2292
solenberg1ac56142015-10-13 03:58:19 -07002293 // Deregister default channel, if that's the one being destroyed.
2294 if (IsDefaultRecvStream(ssrc)) {
2295 default_recv_ssrc_ = -1;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002296 }
henrike@webrtc.org1e09a712013-07-26 19:17:59 +00002297
henrike@webrtc.org1e09a712013-07-26 19:17:59 +00002298 LOG(LS_INFO) << "Removing audio stream " << ssrc
mallinath@webrtc.org67ee6b92014-02-03 16:57:16 +00002299 << " with VoiceEngine channel #" << channel << ".";
solenberg1ac56142015-10-13 03:58:19 -07002300 return DeleteChannel(channel);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002301}
2302
Peter Boström0c4e06b2015-10-07 12:23:21 +02002303bool WebRtcVoiceMediaChannel::SetLocalRenderer(uint32_t ssrc,
henrike@webrtc.org1e09a712013-07-26 19:17:59 +00002304 AudioRenderer* renderer) {
solenbergc96df772015-10-21 13:01:53 -07002305 auto it = send_streams_.find(ssrc);
2306 if (it == send_streams_.end()) {
wu@webrtc.org9dba5252013-08-05 20:36:57 +00002307 if (renderer) {
2308 // Return an error if trying to set a valid renderer with an invalid ssrc.
2309 LOG(LS_ERROR) << "SetLocalRenderer failed with ssrc "<< ssrc;
2310 return false;
2311 }
2312
2313 // The channel likely has gone away, do nothing.
henrike@webrtc.org1e09a712013-07-26 19:17:59 +00002314 return true;
henrike@webrtc.org1e09a712013-07-26 19:17:59 +00002315 }
2316
solenberg1ac56142015-10-13 03:58:19 -07002317 if (renderer) {
mallinath@webrtc.org67ee6b92014-02-03 16:57:16 +00002318 it->second->Start(renderer);
solenberg1ac56142015-10-13 03:58:19 -07002319 } else {
mallinath@webrtc.org67ee6b92014-02-03 16:57:16 +00002320 it->second->Stop();
solenberg1ac56142015-10-13 03:58:19 -07002321 }
henrike@webrtc.org1e09a712013-07-26 19:17:59 +00002322
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002323 return true;
2324}
2325
2326bool WebRtcVoiceMediaChannel::GetActiveStreams(
2327 AudioInfo::StreamList* actives) {
solenbergd97ec302015-10-07 01:40:33 -07002328 RTC_DCHECK(thread_checker_.CalledOnValidThread());
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002329 actives->clear();
Fredrik Solenbergaf9fb212015-08-26 10:45:53 +02002330 for (const auto& ch : receive_channels_) {
2331 int level = GetOutputLevel(ch.second->channel());
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002332 if (level > 0) {
Fredrik Solenbergaf9fb212015-08-26 10:45:53 +02002333 actives->push_back(std::make_pair(ch.first, level));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002334 }
2335 }
2336 return true;
2337}
2338
2339int WebRtcVoiceMediaChannel::GetOutputLevel() {
solenbergd97ec302015-10-07 01:40:33 -07002340 RTC_DCHECK(thread_checker_.CalledOnValidThread());
solenberg1ac56142015-10-13 03:58:19 -07002341 int highest = 0;
Fredrik Solenbergaf9fb212015-08-26 10:45:53 +02002342 for (const auto& ch : receive_channels_) {
solenberg8fb30c32015-10-13 03:06:58 -07002343 highest = std::max(GetOutputLevel(ch.second->channel()), highest);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002344 }
2345 return highest;
2346}
2347
2348int WebRtcVoiceMediaChannel::GetTimeSinceLastTyping() {
2349 int ret;
2350 if (engine()->voe()->processing()->TimeSinceLastTyping(ret) == -1) {
2351 // In case of error, log the info and continue
2352 LOG_RTCERR0(TimeSinceLastTyping);
2353 ret = -1;
2354 } else {
2355 ret *= 1000; // We return ms, webrtc returns seconds.
2356 }
2357 return ret;
2358}
2359
2360void WebRtcVoiceMediaChannel::SetTypingDetectionParameters(int time_window,
2361 int cost_per_typing, int reporting_threshold, int penalty_decay,
2362 int type_event_delay) {
2363 if (engine()->voe()->processing()->SetTypingDetectionParameters(
2364 time_window, cost_per_typing,
2365 reporting_threshold, penalty_decay, type_event_delay) == -1) {
2366 // In case of error, log the info and continue
2367 LOG_RTCERR5(SetTypingDetectionParameters, time_window,
2368 cost_per_typing, reporting_threshold, penalty_decay,
2369 type_event_delay);
2370 }
2371}
2372
solenberg4bac9c52015-10-09 02:32:53 -07002373bool WebRtcVoiceMediaChannel::SetOutputVolume(uint32_t ssrc, double volume) {
solenbergd97ec302015-10-07 01:40:33 -07002374 RTC_DCHECK(thread_checker_.CalledOnValidThread());
solenberg1ac56142015-10-13 03:58:19 -07002375 if (ssrc == 0) {
2376 default_recv_volume_ = volume;
2377 if (default_recv_ssrc_ == -1) {
2378 return true;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002379 }
solenberg1ac56142015-10-13 03:58:19 -07002380 ssrc = static_cast<uint32_t>(default_recv_ssrc_);
2381 }
2382 int ch_id = GetReceiveChannelId(ssrc);
2383 if (ch_id < 0) {
2384 LOG(LS_WARNING) << "Cannot find channel for ssrc:" << ssrc;
2385 return false;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002386 }
2387
solenberg1ac56142015-10-13 03:58:19 -07002388 if (-1 == engine()->voe()->volume()->SetChannelOutputVolumeScaling(ch_id,
2389 volume)) {
2390 LOG_RTCERR2(SetChannelOutputVolumeScaling, ch_id, volume);
2391 return false;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002392 }
solenberg1ac56142015-10-13 03:58:19 -07002393 LOG(LS_INFO) << "SetOutputVolume to " << volume
2394 << " for channel " << ch_id << " and ssrc " << ssrc;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002395 return true;
2396}
2397
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002398bool WebRtcVoiceMediaChannel::CanInsertDtmf() {
2399 return dtmf_allowed_;
2400}
2401
Peter Boström0c4e06b2015-10-07 12:23:21 +02002402bool WebRtcVoiceMediaChannel::InsertDtmf(uint32_t ssrc,
2403 int event,
2404 int duration,
2405 int flags) {
solenberg0a617e22015-10-20 15:49:38 -07002406 RTC_DCHECK(thread_checker_.CalledOnValidThread());
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002407 if (!dtmf_allowed_) {
2408 return false;
2409 }
2410
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002411 // Send the event.
2412 if (flags & cricket::DF_SEND) {
wu@webrtc.orgcadf9042013-08-30 21:24:16 +00002413 int channel = -1;
2414 if (ssrc == 0) {
solenbergc96df772015-10-21 13:01:53 -07002415 if (send_streams_.size() > 0) {
2416 channel = send_streams_.begin()->second->channel();
wu@webrtc.orgcadf9042013-08-30 21:24:16 +00002417 }
2418 } else {
solenbergd97ec302015-10-07 01:40:33 -07002419 channel = GetSendChannelId(ssrc);
wu@webrtc.orgcadf9042013-08-30 21:24:16 +00002420 }
wu@webrtc.org9dba5252013-08-05 20:36:57 +00002421 if (channel == -1) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002422 LOG(LS_WARNING) << "InsertDtmf - The specified ssrc "
2423 << ssrc << " is not in use.";
2424 return false;
2425 }
2426 // Send DTMF using out-of-band DTMF. ("true", as 3rd arg)
wu@webrtc.org9dba5252013-08-05 20:36:57 +00002427 if (engine()->voe()->dtmf()->SendTelephoneEvent(
2428 channel, event, true, duration) == -1) {
2429 LOG_RTCERR4(SendTelephoneEvent, channel, event, true, duration);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002430 return false;
2431 }
2432 }
2433
2434 // Play the event.
2435 if (flags & cricket::DF_PLAY) {
2436 // Play DTMF tone locally.
2437 if (engine()->voe()->dtmf()->PlayDtmfTone(event, duration) == -1) {
2438 LOG_RTCERR2(PlayDtmfTone, event, duration);
2439 return false;
2440 }
2441 }
2442
2443 return true;
2444}
2445
wu@webrtc.orga9890802013-12-13 00:21:03 +00002446void WebRtcVoiceMediaChannel::OnPacketReceived(
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00002447 rtc::Buffer* packet, const rtc::PacketTime& packet_time) {
henrikg91d6ede2015-09-17 00:24:34 -07002448 RTC_DCHECK(thread_checker_.CalledOnValidThread());
Fredrik Solenberg4b60c732015-05-07 14:07:48 +02002449
solenberg1ac56142015-10-13 03:58:19 -07002450 uint32_t ssrc = 0;
2451 if (!GetRtpSsrc(packet->data(), packet->size(), &ssrc)) {
2452 return;
2453 }
2454
2455 if (receive_channels_.empty()) {
2456 // Create new channel, which will be the default receive channel.
2457 StreamParams sp;
2458 sp.ssrcs.push_back(ssrc);
2459 LOG(LS_INFO) << "Creating default receive stream for SSRC=" << ssrc << ".";
2460 if (!AddRecvStream(sp)) {
2461 LOG(LS_WARNING) << "Could not create default receive stream.";
2462 return;
2463 }
2464 default_recv_ssrc_ = ssrc;
2465 SetOutputVolume(default_recv_ssrc_, default_recv_volume_);
2466 }
2467
2468 // Forward packet to Call. If the SSRC is unknown we'll return after this.
Fredrik Solenberg709ed672015-09-15 12:26:33 +02002469 const webrtc::PacketTime webrtc_packet_time(packet_time.timestamp,
2470 packet_time.not_before);
solenberg1ac56142015-10-13 03:58:19 -07002471 webrtc::PacketReceiver::DeliveryStatus delivery_result =
2472 call_->Receiver()->DeliverPacket(webrtc::MediaType::AUDIO,
2473 reinterpret_cast<const uint8_t*>(packet->data()), packet->size(),
2474 webrtc_packet_time);
2475 if (webrtc::PacketReceiver::DELIVERY_OK != delivery_result) {
2476 return;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002477 }
2478
solenberg1ac56142015-10-13 03:58:19 -07002479 // Find the channel to send this packet to. It must exist since webrtc::Call
2480 // was able to demux the packet.
2481 int channel = GetReceiveChannelId(ssrc);
2482 RTC_DCHECK(channel != -1);
2483
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002484 // Pass it off to the decoder.
henrike@webrtc.org28654cb2013-07-22 21:07:49 +00002485 engine()->voe()->network()->ReceivedRTPPacket(
solenberg1ac56142015-10-13 03:58:19 -07002486 channel, packet->data(), packet->size(), webrtc_packet_time);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002487}
2488
wu@webrtc.orga9890802013-12-13 00:21:03 +00002489void WebRtcVoiceMediaChannel::OnRtcpReceived(
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00002490 rtc::Buffer* packet, const rtc::PacketTime& packet_time) {
henrikg91d6ede2015-09-17 00:24:34 -07002491 RTC_DCHECK(thread_checker_.CalledOnValidThread());
Fredrik Solenberg4b60c732015-05-07 14:07:48 +02002492
Fredrik Solenberg709ed672015-09-15 12:26:33 +02002493 // Forward packet to Call as well.
2494 const webrtc::PacketTime webrtc_packet_time(packet_time.timestamp,
2495 packet_time.not_before);
2496 call_->Receiver()->DeliverPacket(webrtc::MediaType::AUDIO,
2497 reinterpret_cast<const uint8_t*>(packet->data()), packet->size(),
2498 webrtc_packet_time);
Fredrik Solenberg4b60c732015-05-07 14:07:48 +02002499
wu@webrtc.org9dba5252013-08-05 20:36:57 +00002500 // Sending channels need all RTCP packets with feedback information.
2501 // Even sender reports can contain attached report blocks.
2502 // Receiving channels need sender reports in order to create
2503 // correct receiver reports.
2504 int type = 0;
kwiberg@webrtc.orgeebcab52015-03-24 09:19:06 +00002505 if (!GetRtcpType(packet->data(), packet->size(), &type)) {
wu@webrtc.org9dba5252013-08-05 20:36:57 +00002506 LOG(LS_WARNING) << "Failed to parse type from received RTCP packet";
2507 return;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002508 }
2509
solenberg0b675462015-10-09 01:37:09 -07002510 // If it is a sender report, find the receive channel that is listening.
wu@webrtc.org9dba5252013-08-05 20:36:57 +00002511 if (type == kRtcpTypeSR) {
solenberg0b675462015-10-09 01:37:09 -07002512 uint32_t ssrc = 0;
2513 if (!GetRtcpSsrc(packet->data(), packet->size(), &ssrc)) {
2514 return;
2515 }
2516 int recv_channel_id = GetReceiveChannelId(ssrc);
2517 if (recv_channel_id != -1) {
wu@webrtc.org9dba5252013-08-05 20:36:57 +00002518 engine()->voe()->network()->ReceivedRTCPPacket(
solenberg0b675462015-10-09 01:37:09 -07002519 recv_channel_id, packet->data(), packet->size());
wu@webrtc.org9dba5252013-08-05 20:36:57 +00002520 }
2521 }
2522
2523 // SR may continue RR and any RR entry may correspond to any one of the send
2524 // channels. So all RTCP packets must be forwarded all send channels. VoE
2525 // will filter out RR internally.
solenbergc96df772015-10-21 13:01:53 -07002526 for (const auto& ch : send_streams_) {
wu@webrtc.org9dba5252013-08-05 20:36:57 +00002527 engine()->voe()->network()->ReceivedRTCPPacket(
Fredrik Solenbergaf9fb212015-08-26 10:45:53 +02002528 ch.second->channel(), packet->data(), packet->size());
wu@webrtc.org9dba5252013-08-05 20:36:57 +00002529 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002530}
2531
Peter Boström0c4e06b2015-10-07 12:23:21 +02002532bool WebRtcVoiceMediaChannel::MuteStream(uint32_t ssrc, bool muted) {
solenberg0a617e22015-10-20 15:49:38 -07002533 RTC_DCHECK(thread_checker_.CalledOnValidThread());
2534 int channel = GetSendChannelId(ssrc);
wu@webrtc.org9dba5252013-08-05 20:36:57 +00002535 if (channel == -1) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002536 LOG(LS_WARNING) << "The specified ssrc " << ssrc << " is not in use.";
2537 return false;
2538 }
wu@webrtc.org9dba5252013-08-05 20:36:57 +00002539 if (engine()->voe()->volume()->SetInputMute(channel, muted) == -1) {
2540 LOG_RTCERR2(SetInputMute, channel, muted);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002541 return false;
2542 }
buildbot@webrtc.org6b21b712014-07-31 15:08:53 +00002543 // We set the AGC to mute state only when all the channels are muted.
2544 // This implementation is not ideal, instead we should signal the AGC when
2545 // the mic channel is muted/unmuted. We can't do it today because there
2546 // is no good way to know which stream is mapping to the mic channel.
2547 bool all_muted = muted;
solenbergc96df772015-10-21 13:01:53 -07002548 for (const auto& ch : send_streams_) {
Fredrik Solenbergaf9fb212015-08-26 10:45:53 +02002549 if (!all_muted) {
2550 break;
2551 }
2552 if (engine()->voe()->volume()->GetInputMute(ch.second->channel(),
buildbot@webrtc.org6b21b712014-07-31 15:08:53 +00002553 all_muted)) {
Fredrik Solenbergaf9fb212015-08-26 10:45:53 +02002554 LOG_RTCERR1(GetInputMute, ch.second->channel());
buildbot@webrtc.org6b21b712014-07-31 15:08:53 +00002555 return false;
2556 }
2557 }
2558
2559 webrtc::AudioProcessing* ap = engine()->voe()->base()->audio_processing();
solenberg0a617e22015-10-20 15:49:38 -07002560 if (ap) {
buildbot@webrtc.org6b21b712014-07-31 15:08:53 +00002561 ap->set_output_will_be_muted(all_muted);
solenberg0a617e22015-10-20 15:49:38 -07002562 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002563 return true;
2564}
2565
minyue@webrtc.org26236952014-10-29 02:27:08 +00002566// TODO(minyue): SetMaxSendBandwidth() is subject to be renamed to
2567// SetMaxSendBitrate() in future.
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00002568bool WebRtcVoiceMediaChannel::SetMaxSendBandwidth(int bps) {
minyue@webrtc.org26236952014-10-29 02:27:08 +00002569 LOG(LS_INFO) << "WebRtcVoiceMediaChannel::SetMaxSendBandwidth.";
minyue@webrtc.org26236952014-10-29 02:27:08 +00002570 return SetSendBitrateInternal(bps);
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00002571}
2572
minyue@webrtc.org26236952014-10-29 02:27:08 +00002573bool WebRtcVoiceMediaChannel::SetSendBitrateInternal(int bps) {
2574 LOG(LS_INFO) << "WebRtcVoiceMediaChannel::SetSendBitrateInternal.";
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00002575
minyue@webrtc.org26236952014-10-29 02:27:08 +00002576 send_bitrate_setting_ = true;
2577 send_bitrate_bps_ = bps;
wu@webrtc.org1d1ffc92013-10-16 18:12:02 +00002578
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002579 if (!send_codec_) {
wu@webrtc.org1d1ffc92013-10-16 18:12:02 +00002580 LOG(LS_INFO) << "The send codec has not been set up yet. "
minyue@webrtc.org26236952014-10-29 02:27:08 +00002581 << "The send bitrate setting will be applied later.";
wu@webrtc.org1d1ffc92013-10-16 18:12:02 +00002582 return true;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002583 }
2584
minyue@webrtc.org26236952014-10-29 02:27:08 +00002585 // Bitrate is auto by default.
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00002586 // TODO(bemasc): Fix this so that if SetMaxSendBandwidth(50) is followed by
2587 // SetMaxSendBandwith(0), the second call removes the previous limit.
2588 if (bps <= 0)
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002589 return true;
2590
2591 webrtc::CodecInst codec = *send_codec_;
2592 bool is_multi_rate = IsCodecMultiRate(codec);
2593
2594 if (is_multi_rate) {
2595 // If codec is multi-rate then just set the bitrate.
2596 codec.rate = bps;
solenbergc96df772015-10-21 13:01:53 -07002597 for (const auto& ch : send_streams_) {
solenberg0a617e22015-10-20 15:49:38 -07002598 if (!SetSendCodec(ch.second->channel(), codec)) {
2599 LOG(LS_INFO) << "Failed to set codec " << codec.plname
2600 << " to bitrate " << bps << " bps.";
2601 return false;
2602 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002603 }
2604 return true;
2605 } else {
2606 // If codec is not multi-rate and |bps| is less than the fixed bitrate
2607 // then fail. If codec is not multi-rate and |bps| exceeds or equal the
2608 // fixed bitrate then ignore.
2609 if (bps < codec.rate) {
2610 LOG(LS_INFO) << "Failed to set codec " << codec.plname
2611 << " to bitrate " << bps << " bps"
2612 << ", requires at least " << codec.rate << " bps.";
2613 return false;
2614 }
2615 return true;
2616 }
2617}
2618
2619bool WebRtcVoiceMediaChannel::GetStats(VoiceMediaInfo* info) {
solenbergd97ec302015-10-07 01:40:33 -07002620 RTC_DCHECK(thread_checker_.CalledOnValidThread());
2621
wu@webrtc.org9dba5252013-08-05 20:36:57 +00002622 bool echo_metrics_on = false;
2623 // These can take on valid negative values, so use the lowest possible level
2624 // as default rather than -1.
2625 int echo_return_loss = -100;
2626 int echo_return_loss_enhancement = -100;
2627 // These can also be negative, but in practice -1 is only used to signal
2628 // insufficient data, since the resolution is limited to multiples of 4 ms.
2629 int echo_delay_median_ms = -1;
2630 int echo_delay_std_ms = -1;
2631 if (engine()->voe()->processing()->GetEcMetricsStatus(
2632 echo_metrics_on) != -1 && echo_metrics_on) {
2633 // TODO(ajm): we may want to use VoECallReport::GetEchoMetricsSummary
2634 // here, but it appears to be unsuitable currently. Revisit after this is
2635 // investigated: http://b/issue?id=5666755
2636 int erl, erle, rerl, anlp;
2637 if (engine()->voe()->processing()->GetEchoMetrics(
2638 erl, erle, rerl, anlp) != -1) {
2639 echo_return_loss = erl;
2640 echo_return_loss_enhancement = erle;
2641 }
2642
2643 int median, std;
bjornv@webrtc.orgcc64a9c2015-02-05 12:52:44 +00002644 float dummy;
2645 if (engine()->voe()->processing()->GetEcDelayMetrics(
2646 median, std, dummy) != -1) {
wu@webrtc.org9dba5252013-08-05 20:36:57 +00002647 echo_delay_median_ms = median;
2648 echo_delay_std_ms = std;
2649 }
2650 }
2651
solenberg43e83d42015-10-20 06:41:01 -07002652 webrtc::CallStatistics cs;
2653 unsigned int ssrc;
2654 webrtc::CodecInst codec;
2655 unsigned int level;
2656
solenbergc96df772015-10-21 13:01:53 -07002657 for (const auto& ch : send_streams_) {
Fredrik Solenbergaf9fb212015-08-26 10:45:53 +02002658 const int channel = ch.second->channel();
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002659
wu@webrtc.org9dba5252013-08-05 20:36:57 +00002660 // Fill in the sender info, based on what we know, and what the
2661 // remote side told us it got from its RTCP report.
2662 VoiceSenderInfo sinfo;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002663
wu@webrtc.org9dba5252013-08-05 20:36:57 +00002664 if (engine()->voe()->rtp()->GetRTCPStatistics(channel, cs) == -1 ||
2665 engine()->voe()->rtp()->GetLocalSSRC(channel, ssrc) == -1) {
2666 continue;
2667 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002668
sergeyu@chromium.org5bc25c42013-12-05 00:24:06 +00002669 sinfo.add_ssrc(ssrc);
wu@webrtc.org9dba5252013-08-05 20:36:57 +00002670 sinfo.codec_name = send_codec_.get() ? send_codec_->plname : "";
2671 sinfo.bytes_sent = cs.bytesSent;
2672 sinfo.packets_sent = cs.packetsSent;
2673 // RTT isn't known until a RTCP report is received. Until then, VoiceEngine
2674 // returns 0 to indicate an error value.
2675 sinfo.rtt_ms = (cs.rttMs > 0) ? cs.rttMs : -1;
2676
2677 // Get data from the last remote RTCP report. Use default values if no data
2678 // available.
2679 sinfo.fraction_lost = -1.0;
2680 sinfo.jitter_ms = -1;
2681 sinfo.packets_lost = -1;
2682 sinfo.ext_seqnum = -1;
2683 std::vector<webrtc::ReportBlock> receive_blocks;
2684 if (engine()->voe()->rtp()->GetRemoteRTCPReportBlocks(
2685 channel, &receive_blocks) != -1 &&
2686 engine()->voe()->codec()->GetSendCodec(channel, codec) != -1) {
Fredrik Solenbergaf9fb212015-08-26 10:45:53 +02002687 for (const webrtc::ReportBlock& block : receive_blocks) {
wu@webrtc.org9dba5252013-08-05 20:36:57 +00002688 // Lookup report for send ssrc only.
Fredrik Solenbergaf9fb212015-08-26 10:45:53 +02002689 if (block.source_SSRC == sinfo.ssrc()) {
wu@webrtc.org9dba5252013-08-05 20:36:57 +00002690 // Convert Q8 to floating point.
Fredrik Solenbergaf9fb212015-08-26 10:45:53 +02002691 sinfo.fraction_lost = static_cast<float>(block.fraction_lost) / 256;
wu@webrtc.org9dba5252013-08-05 20:36:57 +00002692 // Convert samples to milliseconds.
2693 if (codec.plfreq / 1000 > 0) {
Fredrik Solenbergaf9fb212015-08-26 10:45:53 +02002694 sinfo.jitter_ms = block.interarrival_jitter / (codec.plfreq / 1000);
wu@webrtc.org9dba5252013-08-05 20:36:57 +00002695 }
Fredrik Solenbergaf9fb212015-08-26 10:45:53 +02002696 sinfo.packets_lost = block.cumulative_num_packets_lost;
2697 sinfo.ext_seqnum = block.extended_highest_sequence_number;
wu@webrtc.org9dba5252013-08-05 20:36:57 +00002698 break;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002699 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002700 }
2701 }
wu@webrtc.org9dba5252013-08-05 20:36:57 +00002702
2703 // Local speech level.
2704 sinfo.audio_level = (engine()->voe()->volume()->
2705 GetSpeechInputLevelFullRange(level) != -1) ? level : -1;
2706
2707 // TODO(xians): We are injecting the same APM logging to all the send
2708 // channels here because there is no good way to know which send channel
2709 // is using the APM. The correct fix is to allow the send channels to have
2710 // their own APM so that we can feed the correct APM logging to different
2711 // send channels. See issue crbug/264611 .
2712 sinfo.echo_return_loss = echo_return_loss;
2713 sinfo.echo_return_loss_enhancement = echo_return_loss_enhancement;
2714 sinfo.echo_delay_median_ms = echo_delay_median_ms;
2715 sinfo.echo_delay_std_ms = echo_delay_std_ms;
mallinath@webrtc.orga27be8e2013-09-27 23:04:10 +00002716 // TODO(ajm): Re-enable this metric once we have a reliable implementation.
2717 sinfo.aec_quality_min = -1;
wu@webrtc.org967bfff2013-09-19 05:49:50 +00002718 sinfo.typing_noise_detected = typing_noise_detected_;
wu@webrtc.org9dba5252013-08-05 20:36:57 +00002719
2720 info->senders.push_back(sinfo);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002721 }
2722
solenberg1ac56142015-10-13 03:58:19 -07002723 // Get the SSRC and stats for each receiver.
solenberg43e83d42015-10-20 06:41:01 -07002724 for (const auto& ch : receive_channels_) {
2725 int ch_id = ch.second->channel();
2726 memset(&cs, 0, sizeof(cs));
2727 if (engine()->voe()->rtp()->GetRemoteSSRC(ch_id, ssrc) != -1 &&
2728 engine()->voe()->rtp()->GetRTCPStatistics(ch_id, cs) != -1 &&
2729 engine()->voe()->codec()->GetRecCodec(ch_id, codec) != -1) {
2730 VoiceReceiverInfo rinfo;
2731 rinfo.add_ssrc(ssrc);
2732 rinfo.bytes_rcvd = cs.bytesReceived;
2733 rinfo.packets_rcvd = cs.packetsReceived;
2734 // The next four fields are from the most recently sent RTCP report.
2735 // Convert Q8 to floating point.
2736 rinfo.fraction_lost = static_cast<float>(cs.fractionLost) / (1 << 8);
2737 rinfo.packets_lost = cs.cumulativeLost;
2738 rinfo.ext_seqnum = cs.extendedMax;
2739 rinfo.capture_start_ntp_time_ms = cs.capture_start_ntp_time_ms_;
2740 if (codec.pltype != -1) {
2741 rinfo.codec_name = codec.plname;
2742 }
2743 // Convert samples to milliseconds.
2744 if (codec.plfreq / 1000 > 0) {
2745 rinfo.jitter_ms = cs.jitterSamples / (codec.plfreq / 1000);
2746 }
2747
2748 // Get jitter buffer and total delay (alg + jitter + playout) stats.
2749 webrtc::NetworkStatistics ns;
2750 if (engine()->voe()->neteq() &&
2751 engine()->voe()->neteq()->GetNetworkStatistics(
2752 ch_id, ns) != -1) {
2753 rinfo.jitter_buffer_ms = ns.currentBufferSize;
2754 rinfo.jitter_buffer_preferred_ms = ns.preferredBufferSize;
2755 rinfo.expand_rate =
2756 static_cast<float>(ns.currentExpandRate) / (1 << 14);
2757 rinfo.speech_expand_rate =
2758 static_cast<float>(ns.currentSpeechExpandRate) / (1 << 14);
2759 rinfo.secondary_decoded_rate =
2760 static_cast<float>(ns.currentSecondaryDecodedRate) / (1 << 14);
2761 rinfo.accelerate_rate =
2762 static_cast<float>(ns.currentAccelerateRate) / (1 << 14);
2763 rinfo.preemptive_expand_rate =
2764 static_cast<float>(ns.currentPreemptiveRate) / (1 << 14);
2765 }
2766
2767 webrtc::AudioDecodingCallStats ds;
2768 if (engine()->voe()->neteq() &&
2769 engine()->voe()->neteq()->GetDecodingCallStatistics(
2770 ch_id, &ds) != -1) {
2771 rinfo.decoding_calls_to_silence_generator =
2772 ds.calls_to_silence_generator;
2773 rinfo.decoding_calls_to_neteq = ds.calls_to_neteq;
2774 rinfo.decoding_normal = ds.decoded_normal;
2775 rinfo.decoding_plc = ds.decoded_plc;
2776 rinfo.decoding_cng = ds.decoded_cng;
2777 rinfo.decoding_plc_cng = ds.decoded_plc_cng;
2778 }
2779
2780 if (engine()->voe()->sync()) {
2781 int jitter_buffer_delay_ms = 0;
2782 int playout_buffer_delay_ms = 0;
2783 engine()->voe()->sync()->GetDelayEstimate(
2784 ch_id, &jitter_buffer_delay_ms, &playout_buffer_delay_ms);
2785 rinfo.delay_estimate_ms = jitter_buffer_delay_ms +
2786 playout_buffer_delay_ms;
2787 }
2788
2789 // Get speech level.
2790 rinfo.audio_level = (engine()->voe()->volume()->
2791 GetSpeechOutputLevelFullRange(ch_id, level) != -1) ? level : -1;
2792 info->receivers.push_back(rinfo);
2793 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002794 }
2795
2796 return true;
2797}
2798
solenbergd97ec302015-10-07 01:40:33 -07002799void WebRtcVoiceMediaChannel::OnError(int error) {
2800 if (send_ == SEND_NOTHING) {
2801 return;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002802 }
wu@webrtc.org967bfff2013-09-19 05:49:50 +00002803 if (error == VE_TYPING_NOISE_WARNING) {
2804 typing_noise_detected_ = true;
2805 } else if (error == VE_TYPING_NOISE_OFF_WARNING) {
2806 typing_noise_detected_ = false;
2807 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002808}
2809
2810int WebRtcVoiceMediaChannel::GetOutputLevel(int channel) {
solenbergd97ec302015-10-07 01:40:33 -07002811 unsigned int ulevel = 0;
2812 int ret = engine()->voe()->volume()->GetSpeechOutputLevel(channel, ulevel);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002813 return (ret == 0) ? static_cast<int>(ulevel) : -1;
2814}
2815
Peter Boström0c4e06b2015-10-07 12:23:21 +02002816int WebRtcVoiceMediaChannel::GetReceiveChannelId(uint32_t ssrc) const {
solenbergd97ec302015-10-07 01:40:33 -07002817 RTC_DCHECK(thread_checker_.CalledOnValidThread());
solenbergc96df772015-10-21 13:01:53 -07002818 const auto it = receive_channels_.find(ssrc);
solenberg8fb30c32015-10-13 03:06:58 -07002819 if (it != receive_channels_.end()) {
mallinath@webrtc.org67ee6b92014-02-03 16:57:16 +00002820 return it->second->channel();
solenberg8fb30c32015-10-13 03:06:58 -07002821 }
solenberg1ac56142015-10-13 03:58:19 -07002822 return -1;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002823}
2824
Peter Boström0c4e06b2015-10-07 12:23:21 +02002825int WebRtcVoiceMediaChannel::GetSendChannelId(uint32_t ssrc) const {
solenbergd97ec302015-10-07 01:40:33 -07002826 RTC_DCHECK(thread_checker_.CalledOnValidThread());
solenbergc96df772015-10-21 13:01:53 -07002827 const auto it = send_streams_.find(ssrc);
2828 if (it != send_streams_.end()) {
mallinath@webrtc.org67ee6b92014-02-03 16:57:16 +00002829 return it->second->channel();
solenberg8fb30c32015-10-13 03:06:58 -07002830 }
wu@webrtc.org9dba5252013-08-05 20:36:57 +00002831 return -1;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002832}
2833
2834bool WebRtcVoiceMediaChannel::GetRedSendCodec(const AudioCodec& red_codec,
2835 const std::vector<AudioCodec>& all_codecs, webrtc::CodecInst* send_codec) {
2836 // Get the RED encodings from the parameter with no name. This may
2837 // change based on what is discussed on the Jingle list.
2838 // The encoding parameter is of the form "a/b"; we only support where
2839 // a == b. Verify this and parse out the value into red_pt.
2840 // If the parameter value is absent (as it will be until we wire up the
2841 // signaling of this message), use the second codec specified (i.e. the
2842 // one after "red") as the encoding parameter.
2843 int red_pt = -1;
2844 std::string red_params;
2845 CodecParameterMap::const_iterator it = red_codec.params.find("");
2846 if (it != red_codec.params.end()) {
2847 red_params = it->second;
2848 std::vector<std::string> red_pts;
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00002849 if (rtc::split(red_params, '/', &red_pts) != 2 ||
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002850 red_pts[0] != red_pts[1] ||
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00002851 !rtc::FromString(red_pts[0], &red_pt)) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002852 LOG(LS_WARNING) << "RED params " << red_params << " not supported.";
2853 return false;
2854 }
2855 } else if (red_codec.params.empty()) {
2856 LOG(LS_WARNING) << "RED params not present, using defaults";
2857 if (all_codecs.size() > 1) {
2858 red_pt = all_codecs[1].id;
2859 }
2860 }
2861
2862 // Try to find red_pt in |codecs|.
Fredrik Solenbergaf9fb212015-08-26 10:45:53 +02002863 for (const AudioCodec& codec : all_codecs) {
2864 if (codec.id == red_pt) {
2865 // If we find the right codec, that will be the codec we pass to
2866 // SetSendCodec, with the desired payload type.
2867 if (engine()->FindWebRtcCodec(codec, send_codec)) {
2868 return true;
2869 } else {
2870 break;
2871 }
2872 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002873 }
Fredrik Solenbergaf9fb212015-08-26 10:45:53 +02002874 LOG(LS_WARNING) << "RED params " << red_params << " are invalid.";
2875 return false;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002876}
2877
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002878bool WebRtcVoiceMediaChannel::SetPlayout(int channel, bool playout) {
2879 if (playout) {
2880 LOG(LS_INFO) << "Starting playout for channel #" << channel;
2881 if (engine()->voe()->base()->StartPlayout(channel) == -1) {
2882 LOG_RTCERR1(StartPlayout, channel);
2883 return false;
2884 }
2885 } else {
2886 LOG(LS_INFO) << "Stopping playout for channel #" << channel;
2887 engine()->voe()->base()->StopPlayout(channel);
2888 }
2889 return true;
2890}
2891
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002892// Convert VoiceEngine error code into VoiceMediaChannel::Error enum.
2893VoiceMediaChannel::Error
2894 WebRtcVoiceMediaChannel::WebRtcErrorToChannelError(int err_code) {
2895 switch (err_code) {
2896 case 0:
2897 return ERROR_NONE;
2898 case VE_CANNOT_START_RECORDING:
2899 case VE_MIC_VOL_ERROR:
2900 case VE_GET_MIC_VOL_ERROR:
2901 case VE_CANNOT_ACCESS_MIC_VOL:
2902 return ERROR_REC_DEVICE_OPEN_FAILED;
2903 case VE_SATURATION_WARNING:
2904 return ERROR_REC_DEVICE_SATURATION;
2905 case VE_REC_DEVICE_REMOVED:
2906 return ERROR_REC_DEVICE_REMOVED;
2907 case VE_RUNTIME_REC_WARNING:
2908 case VE_RUNTIME_REC_ERROR:
2909 return ERROR_REC_RUNTIME_ERROR;
2910 case VE_CANNOT_START_PLAYOUT:
2911 case VE_SPEAKER_VOL_ERROR:
2912 case VE_GET_SPEAKER_VOL_ERROR:
2913 case VE_CANNOT_ACCESS_SPEAKER_VOL:
2914 return ERROR_PLAY_DEVICE_OPEN_FAILED;
2915 case VE_RUNTIME_PLAY_WARNING:
2916 case VE_RUNTIME_PLAY_ERROR:
2917 return ERROR_PLAY_RUNTIME_ERROR;
2918 case VE_TYPING_NOISE_WARNING:
2919 return ERROR_REC_TYPING_NOISE_DETECTED;
2920 default:
2921 return VoiceMediaChannel::ERROR_OTHER;
2922 }
2923}
2924
henrike@webrtc.org79047f92014-03-06 23:46:59 +00002925bool WebRtcVoiceMediaChannel::SetHeaderExtension(ExtensionSetterFunction setter,
2926 int channel_id, const RtpHeaderExtension* extension) {
2927 bool enable = false;
buildbot@webrtc.org150835e2014-05-06 15:54:38 +00002928 int id = 0;
2929 std::string uri;
henrike@webrtc.org79047f92014-03-06 23:46:59 +00002930 if (extension) {
2931 enable = true;
2932 id = extension->id;
buildbot@webrtc.org150835e2014-05-06 15:54:38 +00002933 uri = extension->uri;
henrike@webrtc.org79047f92014-03-06 23:46:59 +00002934 }
2935 if ((engine()->voe()->rtp()->*setter)(channel_id, enable, id) != 0) {
buildbot@webrtc.org150835e2014-05-06 15:54:38 +00002936 LOG_RTCERR4(*setter, uri, channel_id, enable, id);
henrike@webrtc.org79047f92014-03-06 23:46:59 +00002937 return false;
2938 }
2939 return true;
2940}
2941
Fredrik Solenberg709ed672015-09-15 12:26:33 +02002942void WebRtcVoiceMediaChannel::RecreateAudioReceiveStreams() {
henrikg91d6ede2015-09-17 00:24:34 -07002943 RTC_DCHECK(thread_checker_.CalledOnValidThread());
Fredrik Solenberg709ed672015-09-15 12:26:33 +02002944 for (const auto& it : receive_channels_) {
2945 RemoveAudioReceiveStream(it.first);
2946 }
2947 for (const auto& it : receive_channels_) {
2948 AddAudioReceiveStream(it.first);
2949 }
2950}
2951
Peter Boström0c4e06b2015-10-07 12:23:21 +02002952void WebRtcVoiceMediaChannel::AddAudioReceiveStream(uint32_t ssrc) {
henrikg91d6ede2015-09-17 00:24:34 -07002953 RTC_DCHECK(thread_checker_.CalledOnValidThread());
solenbergc96df772015-10-21 13:01:53 -07002954 WebRtcAudioReceiveStream* stream = receive_channels_[ssrc];
2955 RTC_DCHECK(stream != nullptr);
henrikg91d6ede2015-09-17 00:24:34 -07002956 RTC_DCHECK(receive_streams_.find(ssrc) == receive_streams_.end());
pbos8fc7fa72015-07-15 08:02:58 -07002957 webrtc::AudioReceiveStream::Config config;
2958 config.rtp.remote_ssrc = ssrc;
2959 // Only add RTP extensions if we support combined A/V BWE.
pbos6bb1b6e2015-07-24 07:10:18 -07002960 config.rtp.extensions = recv_rtp_extensions_;
2961 config.combined_audio_video_bwe =
2962 options_.combined_audio_video_bwe.GetWithDefaultIfUnset(false);
solenbergc96df772015-10-21 13:01:53 -07002963 config.voe_channel_id = stream->channel();
pbos8fc7fa72015-07-15 08:02:58 -07002964 config.sync_group = receive_stream_params_[ssrc].sync_label;
2965 webrtc::AudioReceiveStream* s = call_->CreateAudioReceiveStream(config);
2966 receive_streams_.insert(std::make_pair(ssrc, s));
Fredrik Solenberg4b60c732015-05-07 14:07:48 +02002967}
2968
Peter Boström0c4e06b2015-10-07 12:23:21 +02002969void WebRtcVoiceMediaChannel::RemoveAudioReceiveStream(uint32_t ssrc) {
henrikg91d6ede2015-09-17 00:24:34 -07002970 RTC_DCHECK(thread_checker_.CalledOnValidThread());
Fredrik Solenberg709ed672015-09-15 12:26:33 +02002971 auto stream_it = receive_streams_.find(ssrc);
2972 if (stream_it != receive_streams_.end()) {
2973 call_->DestroyAudioReceiveStream(stream_it->second);
2974 receive_streams_.erase(stream_it);
Fredrik Solenberg4b60c732015-05-07 14:07:48 +02002975 }
2976}
2977
Fredrik Solenbergaf9fb212015-08-26 10:45:53 +02002978bool WebRtcVoiceMediaChannel::SetRecvCodecsInternal(
2979 const std::vector<AudioCodec>& new_codecs) {
solenbergd97ec302015-10-07 01:40:33 -07002980 RTC_DCHECK(thread_checker_.CalledOnValidThread());
Fredrik Solenbergaf9fb212015-08-26 10:45:53 +02002981 for (const AudioCodec& codec : new_codecs) {
2982 webrtc::CodecInst voe_codec;
2983 if (engine()->FindWebRtcCodec(codec, &voe_codec)) {
2984 LOG(LS_INFO) << ToString(codec);
2985 voe_codec.pltype = codec.id;
Fredrik Solenbergaf9fb212015-08-26 10:45:53 +02002986 for (const auto& ch : receive_channels_) {
2987 if (engine()->voe()->codec()->SetRecPayloadType(
2988 ch.second->channel(), voe_codec) == -1) {
2989 LOG_RTCERR2(SetRecPayloadType, ch.second->channel(),
2990 ToString(voe_codec));
2991 return false;
2992 }
2993 }
2994 } else {
2995 LOG(LS_WARNING) << "Unknown codec " << ToString(codec);
2996 return false;
2997 }
2998 }
2999 return true;
3000}
3001
henrike@webrtc.org28e20752013-07-10 00:45:36 +00003002} // namespace cricket
3003
3004#endif // HAVE_WEBRTC_VOICE