blob: 4bd0400dfa8e394642f22a65f77a601b7efec2f7 [file] [log] [blame]
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001/*
2 * libjingle
3 * Copyright 2004 Google Inc.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions are met:
7 *
8 * 1. Redistributions of source code must retain the above copyright notice,
9 * this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright notice,
11 * this list of conditions and the following disclaimer in the documentation
12 * and/or other materials provided with the distribution.
13 * 3. The name of the author may not be used to endorse or promote products
14 * derived from this software without specific prior written permission.
15 *
16 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED
17 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
18 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO
19 * EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
20 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
21 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
22 * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
23 * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
24 * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
25 * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26 */
27
28#ifdef HAVE_CONFIG_H
29#include <config.h>
30#endif
31
32#ifdef HAVE_WEBRTC_VOICE
33
34#include "talk/media/webrtc/webrtcvoiceengine.h"
35
36#include <algorithm>
37#include <cstdio>
38#include <string>
39#include <vector>
40
Thiago Farinaef883092015-04-06 10:36:41 +000041#include "talk/media/base/audioframe.h"
buildbot@webrtc.orga09a9992014-08-13 17:26:08 +000042#include "talk/media/base/audiorenderer.h"
43#include "talk/media/base/constants.h"
44#include "talk/media/base/streamparams.h"
buildbot@webrtc.orga09a9992014-08-13 17:26:08 +000045#include "talk/media/webrtc/webrtcvoe.h"
tfarina5237aaf2015-11-10 23:44:30 -080046#include "webrtc/base/arraysize.h"
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +000047#include "webrtc/base/base64.h"
48#include "webrtc/base/byteorder.h"
49#include "webrtc/base/common.h"
50#include "webrtc/base/helpers.h"
51#include "webrtc/base/logging.h"
52#include "webrtc/base/stringencode.h"
53#include "webrtc/base/stringutils.h"
ivoc112a3d82015-10-16 02:22:18 -070054#include "webrtc/call/rtc_event_log.h"
mallinath@webrtc.orga27be8e2013-09-27 23:04:10 +000055#include "webrtc/common.h"
solenberg26c8c912015-11-27 04:00:25 -080056#include "webrtc/modules/audio_coding/acm2/rent_a_codec.h"
henrike@webrtc.org28e20752013-07-10 00:45:36 +000057#include "webrtc/modules/audio_processing/include/audio_processing.h"
Henrik Kjellander98f53512015-10-28 18:17:40 +010058#include "webrtc/system_wrappers/include/field_trial.h"
solenbergbd138382015-11-20 16:08:07 -080059#include "webrtc/system_wrappers/include/trace.h"
henrike@webrtc.org28e20752013-07-10 00:45:36 +000060
henrike@webrtc.org28e20752013-07-10 00:45:36 +000061namespace cricket {
solenbergd97ec302015-10-07 01:40:33 -070062namespace {
henrike@webrtc.org28e20752013-07-10 00:45:36 +000063
solenbergbd138382015-11-20 16:08:07 -080064const int kDefaultTraceFilter = webrtc::kTraceNone | webrtc::kTraceTerseInfo |
65 webrtc::kTraceWarning | webrtc::kTraceError |
66 webrtc::kTraceCritical;
67const int kElevatedTraceFilter = kDefaultTraceFilter | webrtc::kTraceStateInfo |
68 webrtc::kTraceInfo;
69
henrike@webrtc.org28e20752013-07-10 00:45:36 +000070// For Linux/Mac, using the default device is done by specifying index 0 for
71// VoE 4.0 and not -1 (which was the case for VoE 3.5).
72//
73// On Windows Vista and newer, Microsoft introduced the concept of "Default
74// Communications Device". This means that there are two types of default
75// devices (old Wave Audio style default and Default Communications Device).
76//
77// On Windows systems which only support Wave Audio style default, uses either
78// -1 or 0 to select the default device.
79//
80// On Windows systems which support both "Default Communication Device" and
81// old Wave Audio style default, use -1 for Default Communications Device and
82// -2 for Wave Audio style default, which is what we want to use for clips.
83// It's not clear yet whether the -2 index is handled properly on other OSes.
84
85#ifdef WIN32
solenbergd97ec302015-10-07 01:40:33 -070086const int kDefaultAudioDeviceId = -1;
henrike@webrtc.org28e20752013-07-10 00:45:36 +000087#else
solenbergd97ec302015-10-07 01:40:33 -070088const int kDefaultAudioDeviceId = 0;
henrike@webrtc.org28e20752013-07-10 00:45:36 +000089#endif
90
henrike@webrtc.org28e20752013-07-10 00:45:36 +000091// Parameter used for NACK.
92// This value is equivalent to 5 seconds of audio data at 20 ms per packet.
solenbergd97ec302015-10-07 01:40:33 -070093const int kNackMaxPackets = 250;
minyue@webrtc.org2dc6f312014-10-31 05:33:10 +000094
95// Codec parameters for Opus.
henrike@webrtc.org1e09a712013-07-26 19:17:59 +000096// draft-spittka-payload-rtp-opus-03
minyue@webrtc.org2dc6f312014-10-31 05:33:10 +000097
98// Recommended bitrates:
99// 8-12 kb/s for NB speech,
100// 16-20 kb/s for WB speech,
101// 28-40 kb/s for FB speech,
102// 48-64 kb/s for FB mono music, and
103// 64-128 kb/s for FB stereo music.
104// The current implementation applies the following values to mono signals,
105// and multiplies them by 2 for stereo.
solenbergd97ec302015-10-07 01:40:33 -0700106const int kOpusBitrateNb = 12000;
107const int kOpusBitrateWb = 20000;
108const int kOpusBitrateFb = 32000;
minyue@webrtc.org2dc6f312014-10-31 05:33:10 +0000109
henrike@webrtc.org1e09a712013-07-26 19:17:59 +0000110// Opus bitrate should be in the range between 6000 and 510000.
solenbergd97ec302015-10-07 01:40:33 -0700111const int kOpusMinBitrate = 6000;
112const int kOpusMaxBitrate = 510000;
buildbot@webrtc.org5d639b32014-09-10 07:57:12 +0000113
wu@webrtc.orgde305012013-10-31 15:40:38 +0000114// Default audio dscp value.
115// See http://tools.ietf.org/html/rfc2474 for details.
116// See also http://tools.ietf.org/html/draft-jennings-rtcweb-qos-00
solenbergd97ec302015-10-07 01:40:33 -0700117const rtc::DiffServCodePoint kAudioDscpValue = rtc::DSCP_EF;
henrike@webrtc.org1e09a712013-07-26 19:17:59 +0000118
sergeyu@chromium.orga59696b2013-09-13 23:48:58 +0000119// Ensure we open the file in a writeable path on ChromeOS and Android. This
120// workaround can be removed when it's possible to specify a filename for audio
121// option based AEC dumps.
henrike@webrtc.org1e09a712013-07-26 19:17:59 +0000122//
123// TODO(grunell): Use a string in the options instead of hardcoding it here
124// and let the embedder choose the filename (crbug.com/264223).
125//
sergeyu@chromium.orga59696b2013-09-13 23:48:58 +0000126// NOTE(ajm): Don't use hardcoded paths on platforms not explicitly specified
127// below.
128#if defined(CHROMEOS)
solenbergd97ec302015-10-07 01:40:33 -0700129const char kAecDumpByAudioOptionFilename[] = "/tmp/audio.aecdump";
sergeyu@chromium.orga59696b2013-09-13 23:48:58 +0000130#elif defined(ANDROID)
solenbergd97ec302015-10-07 01:40:33 -0700131const char kAecDumpByAudioOptionFilename[] = "/sdcard/audio.aecdump";
henrike@webrtc.org1e09a712013-07-26 19:17:59 +0000132#else
solenbergd97ec302015-10-07 01:40:33 -0700133const char kAecDumpByAudioOptionFilename[] = "audio.aecdump";
henrike@webrtc.org1e09a712013-07-26 19:17:59 +0000134#endif
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000135
solenberg0b675462015-10-09 01:37:09 -0700136bool ValidateStreamParams(const StreamParams& sp) {
137 if (sp.ssrcs.empty()) {
138 LOG(LS_ERROR) << "No SSRCs in stream parameters: " << sp.ToString();
139 return false;
140 }
141 if (sp.ssrcs.size() > 1) {
142 LOG(LS_ERROR) << "Multiple SSRCs in stream parameters: " << sp.ToString();
143 return false;
144 }
145 return true;
146}
147
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000148// Dumps an AudioCodec in RFC 2327-ish format.
solenbergd97ec302015-10-07 01:40:33 -0700149std::string ToString(const AudioCodec& codec) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000150 std::stringstream ss;
151 ss << codec.name << "/" << codec.clockrate << "/" << codec.channels
152 << " (" << codec.id << ")";
153 return ss.str();
154}
Minyue Li7100dcd2015-03-27 05:05:59 +0100155
solenbergd97ec302015-10-07 01:40:33 -0700156std::string ToString(const webrtc::CodecInst& codec) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000157 std::stringstream ss;
158 ss << codec.plname << "/" << codec.plfreq << "/" << codec.channels
159 << " (" << codec.pltype << ")";
160 return ss.str();
161}
162
solenbergd97ec302015-10-07 01:40:33 -0700163bool IsCodec(const AudioCodec& codec, const char* ref_name) {
Minyue Li7100dcd2015-03-27 05:05:59 +0100164 return (_stricmp(codec.name.c_str(), ref_name) == 0);
165}
166
solenbergd97ec302015-10-07 01:40:33 -0700167bool IsCodec(const webrtc::CodecInst& codec, const char* ref_name) {
Minyue Li7100dcd2015-03-27 05:05:59 +0100168 return (_stricmp(codec.plname, ref_name) == 0);
169}
170
solenbergd97ec302015-10-07 01:40:33 -0700171bool FindCodec(const std::vector<AudioCodec>& codecs,
solenberg26c8c912015-11-27 04:00:25 -0800172 const AudioCodec& codec,
173 AudioCodec* found_codec) {
Fredrik Solenbergaf9fb212015-08-26 10:45:53 +0200174 for (const AudioCodec& c : codecs) {
175 if (c.Matches(codec)) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000176 if (found_codec != NULL) {
Fredrik Solenbergaf9fb212015-08-26 10:45:53 +0200177 *found_codec = c;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000178 }
179 return true;
180 }
181 }
182 return false;
183}
wu@webrtc.org1d1ffc92013-10-16 18:12:02 +0000184
solenberg0b675462015-10-09 01:37:09 -0700185bool VerifyUniquePayloadTypes(const std::vector<AudioCodec>& codecs) {
186 if (codecs.empty()) {
187 return true;
188 }
189 std::vector<int> payload_types;
190 for (const AudioCodec& codec : codecs) {
191 payload_types.push_back(codec.id);
192 }
193 std::sort(payload_types.begin(), payload_types.end());
194 auto it = std::unique(payload_types.begin(), payload_types.end());
195 return it == payload_types.end();
196}
197
solenbergd97ec302015-10-07 01:40:33 -0700198bool IsNackEnabled(const AudioCodec& codec) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000199 return codec.HasFeedbackParam(FeedbackParam(kRtcpFbParamNack,
200 kParamValueEmpty));
201}
202
Minyue Li7100dcd2015-03-27 05:05:59 +0100203// Return true if codec.params[feature] == "1", false otherwise.
solenberg26c8c912015-11-27 04:00:25 -0800204bool IsCodecFeatureEnabled(const AudioCodec& codec, const char* feature) {
Minyue Li7100dcd2015-03-27 05:05:59 +0100205 int value;
206 return codec.GetParam(feature, &value) && value == 1;
207}
208
209// Use params[kCodecParamMaxAverageBitrate] if it is defined, use codec.bitrate
210// otherwise. If the value (either from params or codec.bitrate) <=0, use the
211// default configuration. If the value is beyond feasible bit rate of Opus,
212// clamp it. Returns the Opus bit rate for operation.
solenbergd97ec302015-10-07 01:40:33 -0700213int GetOpusBitrate(const AudioCodec& codec, int max_playback_rate) {
Minyue Li7100dcd2015-03-27 05:05:59 +0100214 int bitrate = 0;
215 bool use_param = true;
216 if (!codec.GetParam(kCodecParamMaxAverageBitrate, &bitrate)) {
217 bitrate = codec.bitrate;
218 use_param = false;
219 }
220 if (bitrate <= 0) {
221 if (max_playback_rate <= 8000) {
222 bitrate = kOpusBitrateNb;
223 } else if (max_playback_rate <= 16000) {
224 bitrate = kOpusBitrateWb;
225 } else {
226 bitrate = kOpusBitrateFb;
227 }
228
229 if (IsCodecFeatureEnabled(codec, kCodecParamStereo)) {
230 bitrate *= 2;
231 }
232 } else if (bitrate < kOpusMinBitrate || bitrate > kOpusMaxBitrate) {
233 bitrate = (bitrate < kOpusMinBitrate) ? kOpusMinBitrate : kOpusMaxBitrate;
234 std::string rate_source =
235 use_param ? "Codec parameter \"maxaveragebitrate\"" :
236 "Supplied Opus bitrate";
237 LOG(LS_WARNING) << rate_source
238 << " is invalid and is replaced by: "
239 << bitrate;
240 }
241 return bitrate;
242}
243
244// Returns kOpusDefaultPlaybackRate if params[kCodecParamMaxPlaybackRate] is not
245// defined. Returns the value of params[kCodecParamMaxPlaybackRate] otherwise.
solenbergd97ec302015-10-07 01:40:33 -0700246int GetOpusMaxPlaybackRate(const AudioCodec& codec) {
Minyue Li7100dcd2015-03-27 05:05:59 +0100247 int value;
248 if (codec.GetParam(kCodecParamMaxPlaybackRate, &value)) {
249 return value;
250 }
251 return kOpusDefaultMaxPlaybackRate;
252}
253
solenbergd97ec302015-10-07 01:40:33 -0700254void GetOpusConfig(const AudioCodec& codec, webrtc::CodecInst* voe_codec,
Minyue Li7100dcd2015-03-27 05:05:59 +0100255 bool* enable_codec_fec, int* max_playback_rate,
256 bool* enable_codec_dtx) {
257 *enable_codec_fec = IsCodecFeatureEnabled(codec, kCodecParamUseInbandFec);
258 *enable_codec_dtx = IsCodecFeatureEnabled(codec, kCodecParamUseDtx);
259 *max_playback_rate = GetOpusMaxPlaybackRate(codec);
260
261 // If OPUS, change what we send according to the "stereo" codec
262 // parameter, and not the "channels" parameter. We set
263 // voe_codec.channels to 2 if "stereo=1" and 1 otherwise. If
264 // the bitrate is not specified, i.e. is <= zero, we set it to the
265 // appropriate default value for mono or stereo Opus.
266
267 voe_codec->channels = IsCodecFeatureEnabled(codec, kCodecParamStereo) ? 2 : 1;
268 voe_codec->rate = GetOpusBitrate(codec, *max_playback_rate);
269}
270
wu@webrtc.org1d1ffc92013-10-16 18:12:02 +0000271// Gets the default set of options applied to the engine. Historically, these
272// were supplied as a combination of flags from the channel manager (ec, agc,
273// ns, and highpass) and the rest hardcoded in InitInternal.
solenbergd97ec302015-10-07 01:40:33 -0700274AudioOptions GetDefaultEngineOptions() {
wu@webrtc.org1d1ffc92013-10-16 18:12:02 +0000275 AudioOptions options;
Karl Wibergbe579832015-11-10 22:34:18 +0100276 options.echo_cancellation = rtc::Optional<bool>(true);
277 options.auto_gain_control = rtc::Optional<bool>(true);
278 options.noise_suppression = rtc::Optional<bool>(true);
279 options.highpass_filter = rtc::Optional<bool>(true);
280 options.stereo_swapping = rtc::Optional<bool>(false);
281 options.audio_jitter_buffer_max_packets = rtc::Optional<int>(50);
282 options.audio_jitter_buffer_fast_accelerate = rtc::Optional<bool>(false);
283 options.typing_detection = rtc::Optional<bool>(true);
284 options.adjust_agc_delta = rtc::Optional<int>(0);
285 options.experimental_agc = rtc::Optional<bool>(false);
286 options.extended_filter_aec = rtc::Optional<bool>(false);
287 options.delay_agnostic_aec = rtc::Optional<bool>(false);
288 options.experimental_ns = rtc::Optional<bool>(false);
289 options.aec_dump = rtc::Optional<bool>(false);
wu@webrtc.org1d1ffc92013-10-16 18:12:02 +0000290 return options;
291}
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000292
solenberg566ef242015-11-06 15:34:49 -0800293webrtc::AudioState::Config MakeAudioStateConfig(VoEWrapper* voe_wrapper) {
294 webrtc::AudioState::Config config;
295 config.voice_engine = voe_wrapper->engine();
296 return config;
297}
298
solenberg3a941542015-11-16 07:34:50 -0800299std::vector<webrtc::RtpExtension> FindAudioRtpHeaderExtensions(
300 const std::vector<RtpHeaderExtension>& extensions) {
301 std::vector<webrtc::RtpExtension> result;
302 for (const auto& extension : extensions) {
303 if (extension.uri == kRtpAbsoluteSenderTimeHeaderExtension ||
304 extension.uri == kRtpAudioLevelHeaderExtension) {
305 result.push_back({extension.uri, extension.id});
306 } else {
307 LOG(LS_WARNING) << "Unsupported RTP extension: " << extension.ToString();
308 }
309 }
310 return result;
311}
Brave Yao5225dd82015-03-26 07:39:19 +0800312
solenberg26c8c912015-11-27 04:00:25 -0800313class WebRtcVoiceCodecs final {
314 public:
315 // TODO(solenberg): Do this filtering once off-line, add a simple AudioCodec
316 // list and add a test which verifies VoE supports the listed codecs.
317 static std::vector<AudioCodec> SupportedCodecs() {
318 LOG(LS_INFO) << "WebRtc VoiceEngine codecs:";
319 std::vector<AudioCodec> result;
320 for (webrtc::CodecInst voe_codec : webrtc::acm2::RentACodec::Database()) {
321 // Change the sample rate of G722 to 8000 to match SDP.
322 MaybeFixupG722(&voe_codec, 8000);
buildbot@webrtc.org13d67762014-05-02 17:33:29 +0000323 // Skip uncompressed formats.
Minyue Li7100dcd2015-03-27 05:05:59 +0100324 if (IsCodec(voe_codec, kL16CodecName)) {
buildbot@webrtc.org13d67762014-05-02 17:33:29 +0000325 continue;
326 }
327
328 const CodecPref* pref = NULL;
tfarina5237aaf2015-11-10 23:44:30 -0800329 for (size_t j = 0; j < arraysize(kCodecPrefs); ++j) {
Minyue Li7100dcd2015-03-27 05:05:59 +0100330 if (IsCodec(voe_codec, kCodecPrefs[j].name) &&
buildbot@webrtc.org13d67762014-05-02 17:33:29 +0000331 kCodecPrefs[j].clockrate == voe_codec.plfreq &&
332 kCodecPrefs[j].channels == voe_codec.channels) {
333 pref = &kCodecPrefs[j];
334 break;
335 }
336 }
337
338 if (pref) {
339 // Use the payload type that we've configured in our pref table;
340 // use the offset in our pref table to determine the sort order.
tfarina5237aaf2015-11-10 23:44:30 -0800341 AudioCodec codec(
342 pref->payload_type, voe_codec.plname, voe_codec.plfreq,
343 voe_codec.rate, voe_codec.channels,
344 static_cast<int>(arraysize(kCodecPrefs)) - (pref - kCodecPrefs));
buildbot@webrtc.org13d67762014-05-02 17:33:29 +0000345 LOG(LS_INFO) << ToString(codec);
Minyue Li7100dcd2015-03-27 05:05:59 +0100346 if (IsCodec(codec, kIsacCodecName)) {
minyue@webrtc.org26236952014-10-29 02:27:08 +0000347 // Indicate auto-bitrate in signaling.
buildbot@webrtc.org13d67762014-05-02 17:33:29 +0000348 codec.bitrate = 0;
349 }
Minyue Li7100dcd2015-03-27 05:05:59 +0100350 if (IsCodec(codec, kOpusCodecName)) {
buildbot@webrtc.org13d67762014-05-02 17:33:29 +0000351 // Only add fmtp parameters that differ from the spec.
352 if (kPreferredMinPTime != kOpusDefaultMinPTime) {
353 codec.params[kCodecParamMinPTime] =
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000354 rtc::ToString(kPreferredMinPTime);
buildbot@webrtc.org13d67762014-05-02 17:33:29 +0000355 }
356 if (kPreferredMaxPTime != kOpusDefaultMaxPTime) {
357 codec.params[kCodecParamMaxPTime] =
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000358 rtc::ToString(kPreferredMaxPTime);
buildbot@webrtc.org13d67762014-05-02 17:33:29 +0000359 }
pkasting@chromium.orgd3245462015-02-23 21:28:22 +0000360 codec.SetParam(kCodecParamUseInbandFec, 1);
minyue@webrtc.org4ef22d12014-11-17 09:26:39 +0000361
362 // TODO(hellner): Add ptime, sprop-stereo, and stereo
buildbot@webrtc.org13d67762014-05-02 17:33:29 +0000363 // when they can be set to values other than the default.
364 }
solenberg26c8c912015-11-27 04:00:25 -0800365 result.push_back(codec);
buildbot@webrtc.org13d67762014-05-02 17:33:29 +0000366 } else {
367 LOG(LS_WARNING) << "Unexpected codec: " << ToString(voe_codec);
368 }
369 }
solenberg26c8c912015-11-27 04:00:25 -0800370 // Make sure they are in local preference order.
371 std::sort(result.begin(), result.end(), &AudioCodec::Preferable);
372 return result;
buildbot@webrtc.org13d67762014-05-02 17:33:29 +0000373 }
buildbot@webrtc.org13d67762014-05-02 17:33:29 +0000374
solenberg26c8c912015-11-27 04:00:25 -0800375 static bool ToCodecInst(const AudioCodec& in,
376 webrtc::CodecInst* out) {
377 for (webrtc::CodecInst voe_codec : webrtc::acm2::RentACodec::Database()) {
378 // Change the sample rate of G722 to 8000 to match SDP.
379 MaybeFixupG722(&voe_codec, 8000);
380 AudioCodec codec(voe_codec.pltype, voe_codec.plname, voe_codec.plfreq,
381 voe_codec.rate, voe_codec.channels, 0);
382 bool multi_rate = IsCodecMultiRate(voe_codec);
383 // Allow arbitrary rates for ISAC to be specified.
384 if (multi_rate) {
385 // Set codec.bitrate to 0 so the check for codec.Matches() passes.
386 codec.bitrate = 0;
387 }
388 if (codec.Matches(in)) {
389 if (out) {
390 // Fixup the payload type.
391 voe_codec.pltype = in.id;
392
393 // Set bitrate if specified.
394 if (multi_rate && in.bitrate != 0) {
395 voe_codec.rate = in.bitrate;
396 }
397
398 // Reset G722 sample rate to 16000 to match WebRTC.
399 MaybeFixupG722(&voe_codec, 16000);
400
401 // Apply codec-specific settings.
402 if (IsCodec(codec, kIsacCodecName)) {
403 // If ISAC and an explicit bitrate is not specified,
404 // enable auto bitrate adjustment.
405 voe_codec.rate = (in.bitrate > 0) ? in.bitrate : -1;
406 }
407 *out = voe_codec;
408 }
409 return true;
410 }
411 }
henrik.lundin@webrtc.org8038d422014-11-11 08:38:24 +0000412 return false;
henrik.lundin@webrtc.orgf85dbce2014-11-07 12:25:00 +0000413 }
solenberg26c8c912015-11-27 04:00:25 -0800414
415 static bool IsCodecMultiRate(const webrtc::CodecInst& codec) {
416 for (size_t i = 0; i < arraysize(kCodecPrefs); ++i) {
417 if (IsCodec(codec, kCodecPrefs[i].name) &&
418 kCodecPrefs[i].clockrate == codec.plfreq) {
419 return kCodecPrefs[i].is_multi_rate;
420 }
421 }
422 return false;
423 }
424
425 // If the AudioCodec param kCodecParamPTime is set, then we will set it to
426 // codec pacsize if it's valid, or we will pick the next smallest value we
427 // support.
428 // TODO(Brave): Query supported packet sizes from ACM when the API is ready.
429 static bool SetPTimeAsPacketSize(webrtc::CodecInst* codec, int ptime_ms) {
430 for (const CodecPref& codec_pref : kCodecPrefs) {
431 if ((IsCodec(*codec, codec_pref.name) &&
432 codec_pref.clockrate == codec->plfreq) ||
433 IsCodec(*codec, kG722CodecName)) {
434 int packet_size_ms = SelectPacketSize(codec_pref, ptime_ms);
435 if (packet_size_ms) {
436 // Convert unit from milli-seconds to samples.
437 codec->pacsize = (codec->plfreq / 1000) * packet_size_ms;
438 return true;
439 }
440 }
441 }
442 return false;
443 }
444
445 private:
446 static const int kMaxNumPacketSize = 6;
447 struct CodecPref {
448 const char* name;
449 int clockrate;
450 int channels;
451 int payload_type;
452 bool is_multi_rate;
453 int packet_sizes_ms[kMaxNumPacketSize];
454 };
455 // Note: keep the supported packet sizes in ascending order.
456 static const CodecPref kCodecPrefs[12];
457
458 static int SelectPacketSize(const CodecPref& codec_pref, int ptime_ms) {
459 int selected_packet_size_ms = codec_pref.packet_sizes_ms[0];
460 for (int packet_size_ms : codec_pref.packet_sizes_ms) {
461 if (packet_size_ms && packet_size_ms <= ptime_ms) {
462 selected_packet_size_ms = packet_size_ms;
463 }
464 }
465 return selected_packet_size_ms;
466 }
467
468 // Changes RTP timestamp rate of G722. This is due to the "bug" in the RFC
469 // which says that G722 should be advertised as 8 kHz although it is a 16 kHz
470 // codec.
471 static void MaybeFixupG722(webrtc::CodecInst* voe_codec, int new_plfreq) {
472 if (IsCodec(*voe_codec, kG722CodecName)) {
473 // If the ASSERT triggers, the codec definition in WebRTC VoiceEngine
474 // has changed, and this special case is no longer needed.
475 RTC_DCHECK(voe_codec->plfreq != new_plfreq);
476 voe_codec->plfreq = new_plfreq;
477 }
478 }
479};
480
481const WebRtcVoiceCodecs::CodecPref WebRtcVoiceCodecs::kCodecPrefs[12] = {
482 { kOpusCodecName, 48000, 2, 111, true, { 10, 20, 40, 60 } },
483 { kIsacCodecName, 16000, 1, 103, true, { 30, 60 } },
484 { kIsacCodecName, 32000, 1, 104, true, { 30 } },
485 // G722 should be advertised as 8000 Hz because of the RFC "bug".
486 { kG722CodecName, 8000, 1, 9, false, { 10, 20, 30, 40, 50, 60 } },
487 { kIlbcCodecName, 8000, 1, 102, false, { 20, 30, 40, 60 } },
488 { kPcmuCodecName, 8000, 1, 0, false, { 10, 20, 30, 40, 50, 60 } },
489 { kPcmaCodecName, 8000, 1, 8, false, { 10, 20, 30, 40, 50, 60 } },
490 { kCnCodecName, 32000, 1, 106, false, { } },
491 { kCnCodecName, 16000, 1, 105, false, { } },
492 { kCnCodecName, 8000, 1, 13, false, { } },
493 { kRedCodecName, 8000, 1, 127, false, { } },
494 { kDtmfCodecName, 8000, 1, 126, false, { } },
495};
496} // namespace {
497
498bool WebRtcVoiceEngine::ToCodecInst(const AudioCodec& in,
499 webrtc::CodecInst* out) {
500 return WebRtcVoiceCodecs::ToCodecInst(in, out);
501}
502
503WebRtcVoiceEngine::WebRtcVoiceEngine()
504 : voe_wrapper_(new VoEWrapper()),
505 audio_state_(webrtc::AudioState::Create(MakeAudioStateConfig(voe()))) {
506 Construct();
507}
508
509WebRtcVoiceEngine::WebRtcVoiceEngine(VoEWrapper* voe_wrapper)
510 : voe_wrapper_(voe_wrapper) {
511 Construct();
512}
513
514void WebRtcVoiceEngine::Construct() {
515 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
516 LOG(LS_VERBOSE) << "WebRtcVoiceEngine::WebRtcVoiceEngine";
517
518 signal_thread_checker_.DetachFromThread();
519 std::memset(&default_agc_config_, 0, sizeof(default_agc_config_));
520
521 webrtc::Trace::set_level_filter(kDefaultTraceFilter);
522 webrtc::Trace::SetTraceCallback(this);
523
524 // Load our audio codec list.
525 codecs_ = WebRtcVoiceCodecs::SupportedCodecs();
526
527 // Load our RTP Header extensions.
528 rtp_header_extensions_.push_back(
529 RtpHeaderExtension(kRtpAudioLevelHeaderExtension,
530 kRtpAudioLevelHeaderExtensionDefaultId));
531 rtp_header_extensions_.push_back(
532 RtpHeaderExtension(kRtpAbsoluteSenderTimeHeaderExtension,
533 kRtpAbsoluteSenderTimeHeaderExtensionDefaultId));
534 if (webrtc::field_trial::FindFullName("WebRTC-SendSideBwe") == "Enabled") {
535 rtp_header_extensions_.push_back(RtpHeaderExtension(
536 kRtpTransportSequenceNumberHeaderExtension,
537 kRtpTransportSequenceNumberHeaderExtensionDefaultId));
538 }
539 options_ = GetDefaultEngineOptions();
henrik.lundin@webrtc.orgf85dbce2014-11-07 12:25:00 +0000540}
541
buildbot@webrtc.org13d67762014-05-02 17:33:29 +0000542WebRtcVoiceEngine::~WebRtcVoiceEngine() {
solenberg566ef242015-11-06 15:34:49 -0800543 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
buildbot@webrtc.org13d67762014-05-02 17:33:29 +0000544 LOG(LS_VERBOSE) << "WebRtcVoiceEngine::~WebRtcVoiceEngine";
buildbot@webrtc.org13d67762014-05-02 17:33:29 +0000545 if (adm_) {
546 voe_wrapper_.reset();
547 adm_->Release();
548 adm_ = NULL;
549 }
solenbergbd138382015-11-20 16:08:07 -0800550 webrtc::Trace::SetTraceCallback(nullptr);
buildbot@webrtc.org13d67762014-05-02 17:33:29 +0000551}
552
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000553bool WebRtcVoiceEngine::Init(rtc::Thread* worker_thread) {
solenberg566ef242015-11-06 15:34:49 -0800554 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
henrikg91d6ede2015-09-17 00:24:34 -0700555 RTC_DCHECK(worker_thread == rtc::Thread::Current());
buildbot@webrtc.org13d67762014-05-02 17:33:29 +0000556 LOG(LS_INFO) << "WebRtcVoiceEngine::Init";
557 bool res = InitInternal();
558 if (res) {
559 LOG(LS_INFO) << "WebRtcVoiceEngine::Init Done!";
560 } else {
561 LOG(LS_ERROR) << "WebRtcVoiceEngine::Init failed";
562 Terminate();
563 }
564 return res;
565}
566
567bool WebRtcVoiceEngine::InitInternal() {
solenberg566ef242015-11-06 15:34:49 -0800568 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
buildbot@webrtc.org13d67762014-05-02 17:33:29 +0000569 // Temporarily turn logging level up for the Init call
solenbergbd138382015-11-20 16:08:07 -0800570 webrtc::Trace::set_level_filter(kElevatedTraceFilter);
buildbot@webrtc.org13d67762014-05-02 17:33:29 +0000571 if (voe_wrapper_->base()->Init(adm_) == -1) {
572 LOG_RTCERR0_EX(Init, voe_wrapper_->error());
buildbot@webrtc.org13d67762014-05-02 17:33:29 +0000573 return false;
574 }
solenbergbd138382015-11-20 16:08:07 -0800575 webrtc::Trace::set_level_filter(kDefaultTraceFilter);
buildbot@webrtc.org13d67762014-05-02 17:33:29 +0000576
577 // Log the VoiceEngine version info
solenberg26c8c912015-11-27 04:00:25 -0800578 {
579 char buffer[1024] = "";
580 voe_wrapper_->base()->GetVersion(buffer);
581 LOG(LS_INFO) << "WebRtc VoiceEngine Version:";
582 const char* delim = "\r\n";
583 for (char* tok = strtok(buffer, delim); tok; tok = strtok(NULL, delim)) {
584 LOG(LS_INFO) << tok;
585 }
586 }
buildbot@webrtc.org13d67762014-05-02 17:33:29 +0000587
588 // Save the default AGC configuration settings. This must happen before
589 // calling SetOptions or the default will be overwritten.
590 if (voe_wrapper_->processing()->GetAgcConfig(default_agc_config_) == -1) {
591 LOG_RTCERR0(GetAgcConfig);
592 return false;
593 }
594
595 // Set defaults for options, so that ApplyOptions applies them explicitly
596 // when we clear option (channel) overrides. External clients can still
597 // modify the defaults via SetOptions (on the media engine).
598 if (!SetOptions(GetDefaultEngineOptions())) {
599 return false;
600 }
601
602 // Print our codec list again for the call diagnostic log
603 LOG(LS_INFO) << "WebRtc VoiceEngine codecs:";
Fredrik Solenbergaf9fb212015-08-26 10:45:53 +0200604 for (const AudioCodec& codec : codecs_) {
605 LOG(LS_INFO) << ToString(codec);
buildbot@webrtc.org13d67762014-05-02 17:33:29 +0000606 }
607
608 // Disable the DTMF playout when a tone is sent.
609 // PlayDtmfTone will be used if local playout is needed.
610 if (voe_wrapper_->dtmf()->SetDtmfFeedbackStatus(false) == -1) {
611 LOG_RTCERR1(SetDtmfFeedbackStatus, false);
612 }
613
614 initialized_ = true;
615 return true;
616}
617
buildbot@webrtc.org13d67762014-05-02 17:33:29 +0000618void WebRtcVoiceEngine::Terminate() {
solenberg566ef242015-11-06 15:34:49 -0800619 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
buildbot@webrtc.org13d67762014-05-02 17:33:29 +0000620 LOG(LS_INFO) << "WebRtcVoiceEngine::Terminate";
621 initialized_ = false;
622
623 StopAecDump();
624
buildbot@webrtc.org13d67762014-05-02 17:33:29 +0000625 voe_wrapper_->base()->Terminate();
buildbot@webrtc.org13d67762014-05-02 17:33:29 +0000626}
627
solenberg566ef242015-11-06 15:34:49 -0800628rtc::scoped_refptr<webrtc::AudioState>
629 WebRtcVoiceEngine::GetAudioState() const {
630 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
631 return audio_state_;
632}
633
Fredrik Solenberg709ed672015-09-15 12:26:33 +0200634VoiceMediaChannel* WebRtcVoiceEngine::CreateChannel(webrtc::Call* call,
Jelena Marusicc28a8962015-05-29 15:05:44 +0200635 const AudioOptions& options) {
solenberg566ef242015-11-06 15:34:49 -0800636 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
solenberg0a617e22015-10-20 15:49:38 -0700637 return new WebRtcVoiceMediaChannel(this, options, call);
buildbot@webrtc.org13d67762014-05-02 17:33:29 +0000638}
639
buildbot@webrtc.org13d67762014-05-02 17:33:29 +0000640bool WebRtcVoiceEngine::SetOptions(const AudioOptions& options) {
solenberg566ef242015-11-06 15:34:49 -0800641 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
buildbot@webrtc.org13d67762014-05-02 17:33:29 +0000642 if (!ApplyOptions(options)) {
643 return false;
644 }
645 options_ = options;
646 return true;
647}
648
buildbot@webrtc.org13d67762014-05-02 17:33:29 +0000649// AudioOptions defaults are set in InitInternal (for options with corresponding
650// MediaEngineInterface flags) and in SetOptions(int) for flagless options.
651bool WebRtcVoiceEngine::ApplyOptions(const AudioOptions& options_in) {
solenberg566ef242015-11-06 15:34:49 -0800652 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
henrikac14f5ff2015-09-23 14:08:33 +0200653 LOG(LS_INFO) << "ApplyOptions: " << options_in.ToString();
buildbot@webrtc.org13d67762014-05-02 17:33:29 +0000654 AudioOptions options = options_in; // The options are modified below.
655 // kEcConference is AEC with high suppression.
656 webrtc::EcModes ec_mode = webrtc::kEcConference;
657 webrtc::AecmModes aecm_mode = webrtc::kAecmSpeakerphone;
658 webrtc::AgcModes agc_mode = webrtc::kAgcAdaptiveAnalog;
659 webrtc::NsModes ns_mode = webrtc::kNsHighSuppression;
kwiberg102c6a62015-10-30 02:47:38 -0700660 if (options.aecm_generate_comfort_noise) {
buildbot@webrtc.org13d67762014-05-02 17:33:29 +0000661 LOG(LS_VERBOSE) << "Comfort noise explicitly set to "
kwiberg102c6a62015-10-30 02:47:38 -0700662 << *options.aecm_generate_comfort_noise
663 << " (default is false).";
buildbot@webrtc.org13d67762014-05-02 17:33:29 +0000664 }
665
666#if defined(IOS)
667 // On iOS, VPIO provides built-in EC and AGC.
Karl Wibergbe579832015-11-10 22:34:18 +0100668 options.echo_cancellation = rtc::Optional<bool>(false);
669 options.auto_gain_control = rtc::Optional<bool>(false);
henrika86d907c2015-09-07 16:09:50 +0200670 LOG(LS_INFO) << "Always disable AEC and AGC on iOS. Use built-in instead.";
buildbot@webrtc.org13d67762014-05-02 17:33:29 +0000671#elif defined(ANDROID)
672 ec_mode = webrtc::kEcAecm;
673#endif
674
675#if defined(IOS) || defined(ANDROID)
676 // Set the AGC mode for iOS as well despite disabling it above, to avoid
677 // unsupported configuration errors from webrtc.
678 agc_mode = webrtc::kAgcFixedDigital;
Karl Wibergbe579832015-11-10 22:34:18 +0100679 options.typing_detection = rtc::Optional<bool>(false);
680 options.experimental_agc = rtc::Optional<bool>(false);
681 options.extended_filter_aec = rtc::Optional<bool>(false);
682 options.experimental_ns = rtc::Optional<bool>(false);
buildbot@webrtc.org13d67762014-05-02 17:33:29 +0000683#endif
684
Bjorn Volckerbf395c12015-03-25 22:45:56 +0100685 // Delay Agnostic AEC automatically turns on EC if not set except on iOS
686 // where the feature is not supported.
687 bool use_delay_agnostic_aec = false;
688#if !defined(IOS)
kwiberg102c6a62015-10-30 02:47:38 -0700689 if (options.delay_agnostic_aec) {
690 use_delay_agnostic_aec = *options.delay_agnostic_aec;
Bjorn Volckerbf395c12015-03-25 22:45:56 +0100691 if (use_delay_agnostic_aec) {
Karl Wibergbe579832015-11-10 22:34:18 +0100692 options.echo_cancellation = rtc::Optional<bool>(true);
693 options.extended_filter_aec = rtc::Optional<bool>(true);
Bjorn Volckerbf395c12015-03-25 22:45:56 +0100694 ec_mode = webrtc::kEcConference;
695 }
696 }
697#endif
698
buildbot@webrtc.org13d67762014-05-02 17:33:29 +0000699 webrtc::VoEAudioProcessing* voep = voe_wrapper_->processing();
700
kwiberg102c6a62015-10-30 02:47:38 -0700701 if (options.echo_cancellation) {
henrika@webrtc.orga954c072014-12-09 16:22:09 +0000702 // Check if platform supports built-in EC. Currently only supported on
703 // Android and in combination with Java based audio layer.
704 // TODO(henrika): investigate possibility to support built-in EC also
705 // in combination with Open SL ES audio.
706 const bool built_in_aec = voe_wrapper_->hw()->BuiltInAECIsAvailable();
Bjorn Volcker73f72102015-06-03 14:50:15 +0200707 if (built_in_aec) {
Bjorn Volckerccfc9392015-05-07 07:43:17 +0200708 // Built-in EC exists on this device and use_delay_agnostic_aec is not
709 // overriding it. Enable/Disable it according to the echo_cancellation
710 // audio option.
Bjorn Volcker73f72102015-06-03 14:50:15 +0200711 const bool enable_built_in_aec =
kwiberg102c6a62015-10-30 02:47:38 -0700712 *options.echo_cancellation && !use_delay_agnostic_aec;
Bjorn Volcker73f72102015-06-03 14:50:15 +0200713 if (voe_wrapper_->hw()->EnableBuiltInAEC(enable_built_in_aec) == 0 &&
714 enable_built_in_aec) {
Bjorn Volckerbf395c12015-03-25 22:45:56 +0100715 // Disable internal software EC if built-in EC is enabled,
henrika@webrtc.orga954c072014-12-09 16:22:09 +0000716 // i.e., replace the software EC with the built-in EC.
Karl Wibergbe579832015-11-10 22:34:18 +0100717 options.echo_cancellation = rtc::Optional<bool>(false);
henrika@webrtc.orga954c072014-12-09 16:22:09 +0000718 LOG(LS_INFO) << "Disabling EC since built-in EC will be used instead";
719 }
720 }
kwiberg102c6a62015-10-30 02:47:38 -0700721 if (voep->SetEcStatus(*options.echo_cancellation, ec_mode) == -1) {
722 LOG_RTCERR2(SetEcStatus, *options.echo_cancellation, ec_mode);
buildbot@webrtc.org13d67762014-05-02 17:33:29 +0000723 return false;
724 } else {
kwiberg102c6a62015-10-30 02:47:38 -0700725 LOG(LS_INFO) << "Echo control set to " << *options.echo_cancellation
henrika86d907c2015-09-07 16:09:50 +0200726 << " with mode " << ec_mode;
buildbot@webrtc.org13d67762014-05-02 17:33:29 +0000727 }
728#if !defined(ANDROID)
729 // TODO(ajm): Remove the error return on Android from webrtc.
kwiberg102c6a62015-10-30 02:47:38 -0700730 if (voep->SetEcMetricsStatus(*options.echo_cancellation) == -1) {
731 LOG_RTCERR1(SetEcMetricsStatus, *options.echo_cancellation);
buildbot@webrtc.org13d67762014-05-02 17:33:29 +0000732 return false;
733 }
734#endif
735 if (ec_mode == webrtc::kEcAecm) {
kwiberg102c6a62015-10-30 02:47:38 -0700736 bool cn = options.aecm_generate_comfort_noise.value_or(false);
737 if (voep->SetAecmMode(aecm_mode, cn) != 0) {
738 LOG_RTCERR2(SetAecmMode, aecm_mode, cn);
buildbot@webrtc.org13d67762014-05-02 17:33:29 +0000739 return false;
740 }
741 }
742 }
743
kwiberg102c6a62015-10-30 02:47:38 -0700744 if (options.auto_gain_control) {
henrikac14f5ff2015-09-23 14:08:33 +0200745 const bool built_in_agc = voe_wrapper_->hw()->BuiltInAGCIsAvailable();
746 if (built_in_agc) {
kwiberg102c6a62015-10-30 02:47:38 -0700747 if (voe_wrapper_->hw()->EnableBuiltInAGC(*options.auto_gain_control) ==
748 0 &&
749 *options.auto_gain_control) {
henrikac14f5ff2015-09-23 14:08:33 +0200750 // Disable internal software AGC if built-in AGC is enabled,
751 // i.e., replace the software AGC with the built-in AGC.
Karl Wibergbe579832015-11-10 22:34:18 +0100752 options.auto_gain_control = rtc::Optional<bool>(false);
henrikac14f5ff2015-09-23 14:08:33 +0200753 LOG(LS_INFO) << "Disabling AGC since built-in AGC will be used instead";
754 }
755 }
kwiberg102c6a62015-10-30 02:47:38 -0700756 if (voep->SetAgcStatus(*options.auto_gain_control, agc_mode) == -1) {
757 LOG_RTCERR2(SetAgcStatus, *options.auto_gain_control, agc_mode);
buildbot@webrtc.org13d67762014-05-02 17:33:29 +0000758 return false;
759 } else {
kwiberg102c6a62015-10-30 02:47:38 -0700760 LOG(LS_INFO) << "Auto gain set to " << *options.auto_gain_control
761 << " with mode " << agc_mode;
buildbot@webrtc.org13d67762014-05-02 17:33:29 +0000762 }
763 }
764
kwiberg102c6a62015-10-30 02:47:38 -0700765 if (options.tx_agc_target_dbov || options.tx_agc_digital_compression_gain ||
766 options.tx_agc_limiter) {
buildbot@webrtc.org13d67762014-05-02 17:33:29 +0000767 // Override default_agc_config_. Generally, an unset option means "leave
768 // the VoE bits alone" in this function, so we want whatever is set to be
769 // stored as the new "default". If we didn't, then setting e.g.
770 // tx_agc_target_dbov would reset digital compression gain and limiter
771 // settings.
772 // Also, if we don't update default_agc_config_, then adjust_agc_delta
773 // would be an offset from the original values, and not whatever was set
774 // explicitly.
kwiberg102c6a62015-10-30 02:47:38 -0700775 default_agc_config_.targetLeveldBOv = options.tx_agc_target_dbov.value_or(
776 default_agc_config_.targetLeveldBOv);
buildbot@webrtc.org13d67762014-05-02 17:33:29 +0000777 default_agc_config_.digitalCompressionGaindB =
kwiberg102c6a62015-10-30 02:47:38 -0700778 options.tx_agc_digital_compression_gain.value_or(
buildbot@webrtc.org13d67762014-05-02 17:33:29 +0000779 default_agc_config_.digitalCompressionGaindB);
780 default_agc_config_.limiterEnable =
kwiberg102c6a62015-10-30 02:47:38 -0700781 options.tx_agc_limiter.value_or(default_agc_config_.limiterEnable);
buildbot@webrtc.org13d67762014-05-02 17:33:29 +0000782 if (voe_wrapper_->processing()->SetAgcConfig(default_agc_config_) == -1) {
783 LOG_RTCERR3(SetAgcConfig,
784 default_agc_config_.targetLeveldBOv,
785 default_agc_config_.digitalCompressionGaindB,
786 default_agc_config_.limiterEnable);
787 return false;
788 }
789 }
790
kwiberg102c6a62015-10-30 02:47:38 -0700791 if (options.noise_suppression) {
henrikac14f5ff2015-09-23 14:08:33 +0200792 const bool built_in_ns = voe_wrapper_->hw()->BuiltInNSIsAvailable();
793 if (built_in_ns) {
kwiberg102c6a62015-10-30 02:47:38 -0700794 if (voe_wrapper_->hw()->EnableBuiltInNS(*options.noise_suppression) ==
795 0 &&
796 *options.noise_suppression) {
henrikac14f5ff2015-09-23 14:08:33 +0200797 // Disable internal software NS if built-in NS is enabled,
798 // i.e., replace the software NS with the built-in NS.
Karl Wibergbe579832015-11-10 22:34:18 +0100799 options.noise_suppression = rtc::Optional<bool>(false);
henrikac14f5ff2015-09-23 14:08:33 +0200800 LOG(LS_INFO) << "Disabling NS since built-in NS will be used instead";
801 }
802 }
kwiberg102c6a62015-10-30 02:47:38 -0700803 if (voep->SetNsStatus(*options.noise_suppression, ns_mode) == -1) {
804 LOG_RTCERR2(SetNsStatus, *options.noise_suppression, ns_mode);
buildbot@webrtc.org13d67762014-05-02 17:33:29 +0000805 return false;
806 } else {
kwiberg102c6a62015-10-30 02:47:38 -0700807 LOG(LS_INFO) << "Noise suppression set to " << *options.noise_suppression
henrikac14f5ff2015-09-23 14:08:33 +0200808 << " with mode " << ns_mode;
buildbot@webrtc.org13d67762014-05-02 17:33:29 +0000809 }
810 }
811
kwiberg102c6a62015-10-30 02:47:38 -0700812 if (options.highpass_filter) {
813 LOG(LS_INFO) << "High pass filter enabled? " << *options.highpass_filter;
814 if (voep->EnableHighPassFilter(*options.highpass_filter) == -1) {
815 LOG_RTCERR1(SetHighpassFilterStatus, *options.highpass_filter);
buildbot@webrtc.org13d67762014-05-02 17:33:29 +0000816 return false;
817 }
818 }
819
kwiberg102c6a62015-10-30 02:47:38 -0700820 if (options.stereo_swapping) {
821 LOG(LS_INFO) << "Stereo swapping enabled? " << *options.stereo_swapping;
822 voep->EnableStereoChannelSwapping(*options.stereo_swapping);
823 if (voep->IsStereoChannelSwappingEnabled() != *options.stereo_swapping) {
824 LOG_RTCERR1(EnableStereoChannelSwapping, *options.stereo_swapping);
buildbot@webrtc.org13d67762014-05-02 17:33:29 +0000825 return false;
826 }
827 }
828
kwiberg102c6a62015-10-30 02:47:38 -0700829 if (options.audio_jitter_buffer_max_packets) {
830 LOG(LS_INFO) << "NetEq capacity is "
831 << *options.audio_jitter_buffer_max_packets;
Henrik Lundin64dad832015-05-11 12:44:23 +0200832 voe_config_.Set<webrtc::NetEqCapacityConfig>(
kwiberg102c6a62015-10-30 02:47:38 -0700833 new webrtc::NetEqCapacityConfig(
834 *options.audio_jitter_buffer_max_packets));
Henrik Lundin64dad832015-05-11 12:44:23 +0200835 }
836
kwiberg102c6a62015-10-30 02:47:38 -0700837 if (options.audio_jitter_buffer_fast_accelerate) {
838 LOG(LS_INFO) << "NetEq fast mode? "
839 << *options.audio_jitter_buffer_fast_accelerate;
Henrik Lundin5263b3c2015-06-01 10:29:41 +0200840 voe_config_.Set<webrtc::NetEqFastAccelerate>(
kwiberg102c6a62015-10-30 02:47:38 -0700841 new webrtc::NetEqFastAccelerate(
842 *options.audio_jitter_buffer_fast_accelerate));
Henrik Lundin5263b3c2015-06-01 10:29:41 +0200843 }
844
kwiberg102c6a62015-10-30 02:47:38 -0700845 if (options.typing_detection) {
846 LOG(LS_INFO) << "Typing detection is enabled? "
847 << *options.typing_detection;
848 if (voep->SetTypingDetectionStatus(*options.typing_detection) == -1) {
buildbot@webrtc.org13d67762014-05-02 17:33:29 +0000849 // In case of error, log the info and continue
kwiberg102c6a62015-10-30 02:47:38 -0700850 LOG_RTCERR1(SetTypingDetectionStatus, *options.typing_detection);
buildbot@webrtc.org13d67762014-05-02 17:33:29 +0000851 }
852 }
853
kwiberg102c6a62015-10-30 02:47:38 -0700854 if (options.adjust_agc_delta) {
855 LOG(LS_INFO) << "Adjust agc delta is " << *options.adjust_agc_delta;
856 if (!AdjustAgcLevel(*options.adjust_agc_delta)) {
buildbot@webrtc.org13d67762014-05-02 17:33:29 +0000857 return false;
858 }
859 }
860
kwiberg102c6a62015-10-30 02:47:38 -0700861 if (options.aec_dump) {
862 LOG(LS_INFO) << "Aec dump is enabled? " << *options.aec_dump;
863 if (*options.aec_dump)
buildbot@webrtc.org13d67762014-05-02 17:33:29 +0000864 StartAecDump(kAecDumpByAudioOptionFilename);
865 else
866 StopAecDump();
867 }
868
buildbot@webrtc.org1f8a2372014-08-28 10:52:44 +0000869 webrtc::Config config;
870
kwiberg102c6a62015-10-30 02:47:38 -0700871 if (options.delay_agnostic_aec)
872 delay_agnostic_aec_ = options.delay_agnostic_aec;
873 if (delay_agnostic_aec_) {
874 LOG(LS_INFO) << "Delay agnostic aec is enabled? " << *delay_agnostic_aec_;
henrik.lundin0f133b92015-07-02 00:17:55 -0700875 config.Set<webrtc::DelayAgnostic>(
kwiberg102c6a62015-10-30 02:47:38 -0700876 new webrtc::DelayAgnostic(*delay_agnostic_aec_));
Bjorn Volckerbf395c12015-03-25 22:45:56 +0100877 }
878
kwiberg102c6a62015-10-30 02:47:38 -0700879 if (options.extended_filter_aec) {
880 extended_filter_aec_ = options.extended_filter_aec;
881 }
882 if (extended_filter_aec_) {
883 LOG(LS_INFO) << "Extended filter aec is enabled? " << *extended_filter_aec_;
Henrik Lundin441f6342015-06-09 16:03:13 +0200884 config.Set<webrtc::ExtendedFilter>(
kwiberg102c6a62015-10-30 02:47:38 -0700885 new webrtc::ExtendedFilter(*extended_filter_aec_));
buildbot@webrtc.org1f8a2372014-08-28 10:52:44 +0000886 }
887
kwiberg102c6a62015-10-30 02:47:38 -0700888 if (options.experimental_ns) {
889 experimental_ns_ = options.experimental_ns;
890 }
891 if (experimental_ns_) {
892 LOG(LS_INFO) << "Experimental ns is enabled? " << *experimental_ns_;
buildbot@webrtc.org1f8a2372014-08-28 10:52:44 +0000893 config.Set<webrtc::ExperimentalNs>(
kwiberg102c6a62015-10-30 02:47:38 -0700894 new webrtc::ExperimentalNs(*experimental_ns_));
buildbot@webrtc.org1f8a2372014-08-28 10:52:44 +0000895 }
buildbot@webrtc.org1f8a2372014-08-28 10:52:44 +0000896
897 // We check audioproc for the benefit of tests, since FakeWebRtcVoiceEngine
898 // returns NULL on audio_processing().
899 webrtc::AudioProcessing* audioproc = voe_wrapper_->base()->audio_processing();
900 if (audioproc) {
901 audioproc->SetExtraOptions(config);
902 }
903
kwiberg102c6a62015-10-30 02:47:38 -0700904 if (options.recording_sample_rate) {
905 LOG(LS_INFO) << "Recording sample rate is "
906 << *options.recording_sample_rate;
907 if (voe_wrapper_->hw()->SetRecordingSampleRate(
908 *options.recording_sample_rate)) {
909 LOG_RTCERR1(SetRecordingSampleRate, *options.recording_sample_rate);
buildbot@webrtc.org13d67762014-05-02 17:33:29 +0000910 }
911 }
912
kwiberg102c6a62015-10-30 02:47:38 -0700913 if (options.playout_sample_rate) {
914 LOG(LS_INFO) << "Playout sample rate is " << *options.playout_sample_rate;
915 if (voe_wrapper_->hw()->SetPlayoutSampleRate(
916 *options.playout_sample_rate)) {
917 LOG_RTCERR1(SetPlayoutSampleRate, *options.playout_sample_rate);
buildbot@webrtc.org13d67762014-05-02 17:33:29 +0000918 }
919 }
920
921 return true;
922}
923
buildbot@webrtc.org13d67762014-05-02 17:33:29 +0000924// TODO(juberti): Refactor this so that the core logic can be used to set the
925// soundclip device. At that time, reinstate the soundclip pause/resume code.
926bool WebRtcVoiceEngine::SetDevices(const Device* in_device,
927 const Device* out_device) {
solenberg566ef242015-11-06 15:34:49 -0800928 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
buildbot@webrtc.org13d67762014-05-02 17:33:29 +0000929#if !defined(IOS)
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000930 int in_id = in_device ? rtc::FromString<int>(in_device->id) :
buildbot@webrtc.org13d67762014-05-02 17:33:29 +0000931 kDefaultAudioDeviceId;
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000932 int out_id = out_device ? rtc::FromString<int>(out_device->id) :
buildbot@webrtc.org13d67762014-05-02 17:33:29 +0000933 kDefaultAudioDeviceId;
934 // The device manager uses -1 as the default device, which was the case for
935 // VoE 3.5. VoE 4.0, however, uses 0 as the default in Linux and Mac.
936#ifndef WIN32
937 if (-1 == in_id) {
938 in_id = kDefaultAudioDeviceId;
939 }
940 if (-1 == out_id) {
941 out_id = kDefaultAudioDeviceId;
942 }
943#endif
944
945 std::string in_name = (in_id != kDefaultAudioDeviceId) ?
946 in_device->name : "Default device";
947 std::string out_name = (out_id != kDefaultAudioDeviceId) ?
948 out_device->name : "Default device";
949 LOG(LS_INFO) << "Setting microphone to (id=" << in_id << ", name=" << in_name
950 << ") and speaker to (id=" << out_id << ", name=" << out_name
951 << ")";
952
buildbot@webrtc.org13d67762014-05-02 17:33:29 +0000953 // Must also pause all audio playback and capture.
solenbergc1a1b352015-09-22 13:31:20 -0700954 bool ret = true;
Fredrik Solenbergaf9fb212015-08-26 10:45:53 +0200955 for (WebRtcVoiceMediaChannel* channel : channels_) {
buildbot@webrtc.org13d67762014-05-02 17:33:29 +0000956 if (!channel->PausePlayout()) {
957 LOG(LS_WARNING) << "Failed to pause playout";
958 ret = false;
959 }
960 if (!channel->PauseSend()) {
961 LOG(LS_WARNING) << "Failed to pause send";
962 ret = false;
963 }
964 }
965
966 // Find the recording device id in VoiceEngine and set recording device.
967 if (!FindWebRtcAudioDeviceId(true, in_name, in_id, &in_id)) {
968 ret = false;
969 }
970 if (ret) {
971 if (voe_wrapper_->hw()->SetRecordingDevice(in_id) == -1) {
972 LOG_RTCERR2(SetRecordingDevice, in_name, in_id);
973 ret = false;
974 }
buildbot@webrtc.org6b21b712014-07-31 15:08:53 +0000975 webrtc::AudioProcessing* ap = voe()->base()->audio_processing();
976 if (ap)
977 ap->Initialize();
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000978 }
979
980 // Find the playout device id in VoiceEngine and set playout device.
981 if (!FindWebRtcAudioDeviceId(false, out_name, out_id, &out_id)) {
982 LOG(LS_WARNING) << "Failed to find VoiceEngine device id for " << out_name;
983 ret = false;
984 }
985 if (ret) {
986 if (voe_wrapper_->hw()->SetPlayoutDevice(out_id) == -1) {
sergeyu@chromium.org5bc25c42013-12-05 00:24:06 +0000987 LOG_RTCERR2(SetPlayoutDevice, out_name, out_id);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000988 ret = false;
989 }
990 }
991
992 // Resume all audio playback and capture.
Fredrik Solenbergaf9fb212015-08-26 10:45:53 +0200993 for (WebRtcVoiceMediaChannel* channel : channels_) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000994 if (!channel->ResumePlayout()) {
995 LOG(LS_WARNING) << "Failed to resume playout";
996 ret = false;
997 }
998 if (!channel->ResumeSend()) {
999 LOG(LS_WARNING) << "Failed to resume send";
1000 ret = false;
1001 }
1002 }
1003
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001004 if (ret) {
1005 LOG(LS_INFO) << "Set microphone to (id=" << in_id <<" name=" << in_name
1006 << ") and speaker to (id="<< out_id << " name=" << out_name
1007 << ")";
1008 }
1009
1010 return ret;
1011#else
1012 return true;
wu@webrtc.orgcecfd182013-10-30 05:18:12 +00001013#endif // !IOS
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001014}
1015
1016bool WebRtcVoiceEngine::FindWebRtcAudioDeviceId(
1017 bool is_input, const std::string& dev_name, int dev_id, int* rtc_id) {
solenberg566ef242015-11-06 15:34:49 -08001018 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001019 // In Linux, VoiceEngine uses the same device dev_id as the device manager.
wu@webrtc.orgcecfd182013-10-30 05:18:12 +00001020#if defined(LINUX) || defined(ANDROID)
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001021 *rtc_id = dev_id;
1022 return true;
1023#else
1024 // In Windows and Mac, we need to find the VoiceEngine device id by name
1025 // unless the input dev_id is the default device id.
1026 if (kDefaultAudioDeviceId == dev_id) {
1027 *rtc_id = dev_id;
1028 return true;
1029 }
1030
1031 // Get the number of VoiceEngine audio devices.
1032 int count = 0;
1033 if (is_input) {
1034 if (-1 == voe_wrapper_->hw()->GetNumOfRecordingDevices(count)) {
1035 LOG_RTCERR0(GetNumOfRecordingDevices);
1036 return false;
1037 }
1038 } else {
1039 if (-1 == voe_wrapper_->hw()->GetNumOfPlayoutDevices(count)) {
1040 LOG_RTCERR0(GetNumOfPlayoutDevices);
1041 return false;
1042 }
1043 }
1044
1045 for (int i = 0; i < count; ++i) {
1046 char name[128];
1047 char guid[128];
1048 if (is_input) {
1049 voe_wrapper_->hw()->GetRecordingDeviceName(i, name, guid);
1050 LOG(LS_VERBOSE) << "VoiceEngine microphone " << i << ": " << name;
1051 } else {
1052 voe_wrapper_->hw()->GetPlayoutDeviceName(i, name, guid);
1053 LOG(LS_VERBOSE) << "VoiceEngine speaker " << i << ": " << name;
1054 }
1055
1056 std::string webrtc_name(name);
1057 if (dev_name.compare(0, webrtc_name.size(), webrtc_name) == 0) {
1058 *rtc_id = i;
1059 return true;
1060 }
1061 }
1062 LOG(LS_WARNING) << "VoiceEngine cannot find device: " << dev_name;
1063 return false;
1064#endif
1065}
1066
1067bool WebRtcVoiceEngine::GetOutputVolume(int* level) {
solenberg566ef242015-11-06 15:34:49 -08001068 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001069 unsigned int ulevel;
1070 if (voe_wrapper_->volume()->GetSpeakerVolume(ulevel) == -1) {
1071 LOG_RTCERR1(GetSpeakerVolume, level);
1072 return false;
1073 }
1074 *level = ulevel;
1075 return true;
1076}
1077
1078bool WebRtcVoiceEngine::SetOutputVolume(int level) {
solenberg566ef242015-11-06 15:34:49 -08001079 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
henrikg91d6ede2015-09-17 00:24:34 -07001080 RTC_DCHECK(level >= 0 && level <= 255);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001081 if (voe_wrapper_->volume()->SetSpeakerVolume(level) == -1) {
1082 LOG_RTCERR1(SetSpeakerVolume, level);
1083 return false;
1084 }
1085 return true;
1086}
1087
1088int WebRtcVoiceEngine::GetInputLevel() {
solenberg566ef242015-11-06 15:34:49 -08001089 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001090 unsigned int ulevel;
1091 return (voe_wrapper_->volume()->GetSpeechInputLevel(ulevel) != -1) ?
1092 static_cast<int>(ulevel) : -1;
1093}
1094
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001095const std::vector<AudioCodec>& WebRtcVoiceEngine::codecs() {
solenberg566ef242015-11-06 15:34:49 -08001096 RTC_DCHECK(signal_thread_checker_.CalledOnValidThread());
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001097 return codecs_;
1098}
1099
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001100const std::vector<RtpHeaderExtension>&
1101WebRtcVoiceEngine::rtp_header_extensions() const {
solenberg566ef242015-11-06 15:34:49 -08001102 RTC_DCHECK(signal_thread_checker_.CalledOnValidThread());
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001103 return rtp_header_extensions_;
1104}
1105
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001106int WebRtcVoiceEngine::GetLastEngineError() {
solenberg566ef242015-11-06 15:34:49 -08001107 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001108 return voe_wrapper_->error();
1109}
1110
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001111void WebRtcVoiceEngine::Print(webrtc::TraceLevel level, const char* trace,
1112 int length) {
solenberg566ef242015-11-06 15:34:49 -08001113 // Note: This callback can happen on any thread!
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00001114 rtc::LoggingSeverity sev = rtc::LS_VERBOSE;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001115 if (level == webrtc::kTraceError || level == webrtc::kTraceCritical)
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00001116 sev = rtc::LS_ERROR;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001117 else if (level == webrtc::kTraceWarning)
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00001118 sev = rtc::LS_WARNING;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001119 else if (level == webrtc::kTraceStateInfo || level == webrtc::kTraceInfo)
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00001120 sev = rtc::LS_INFO;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001121 else if (level == webrtc::kTraceTerseInfo)
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00001122 sev = rtc::LS_INFO;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001123
1124 // Skip past boilerplate prefix text
1125 if (length < 72) {
1126 std::string msg(trace, length);
1127 LOG(LS_ERROR) << "Malformed webrtc log message: ";
1128 LOG_V(sev) << msg;
1129 } else {
1130 std::string msg(trace + 71, length - 72);
Peter Boströmd5c75b12015-09-23 13:24:32 +02001131 LOG_V(sev) << "webrtc: " << msg;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001132 }
1133}
1134
solenberg63b34542015-09-29 06:06:31 -07001135void WebRtcVoiceEngine::RegisterChannel(WebRtcVoiceMediaChannel* channel) {
solenberg566ef242015-11-06 15:34:49 -08001136 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
1137 RTC_DCHECK(channel);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001138 channels_.push_back(channel);
1139}
1140
solenberg63b34542015-09-29 06:06:31 -07001141void WebRtcVoiceEngine::UnregisterChannel(WebRtcVoiceMediaChannel* channel) {
solenberg566ef242015-11-06 15:34:49 -08001142 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
solenberg63b34542015-09-29 06:06:31 -07001143 auto it = std::find(channels_.begin(), channels_.end(), channel);
solenberg566ef242015-11-06 15:34:49 -08001144 RTC_DCHECK(it != channels_.end());
1145 channels_.erase(it);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001146}
1147
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001148// Adjusts the default AGC target level by the specified delta.
1149// NB: If we start messing with other config fields, we'll want
1150// to save the current webrtc::AgcConfig as well.
1151bool WebRtcVoiceEngine::AdjustAgcLevel(int delta) {
solenberg566ef242015-11-06 15:34:49 -08001152 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001153 webrtc::AgcConfig config = default_agc_config_;
1154 config.targetLeveldBOv -= delta;
1155
1156 LOG(LS_INFO) << "Adjusting AGC level from default -"
1157 << default_agc_config_.targetLeveldBOv << "dB to -"
1158 << config.targetLeveldBOv << "dB";
1159
1160 if (voe_wrapper_->processing()->SetAgcConfig(config) == -1) {
1161 LOG_RTCERR1(SetAgcConfig, config.targetLeveldBOv);
1162 return false;
1163 }
1164 return true;
1165}
1166
Fredrik Solenbergccb49e72015-05-19 11:37:56 +02001167bool WebRtcVoiceEngine::SetAudioDeviceModule(webrtc::AudioDeviceModule* adm) {
solenberg566ef242015-11-06 15:34:49 -08001168 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001169 if (initialized_) {
1170 LOG(LS_WARNING) << "SetAudioDeviceModule can not be called after Init.";
1171 return false;
1172 }
1173 if (adm_) {
1174 adm_->Release();
1175 adm_ = NULL;
1176 }
1177 if (adm) {
1178 adm_ = adm;
1179 adm_->AddRef();
1180 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001181 return true;
1182}
1183
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00001184bool WebRtcVoiceEngine::StartAecDump(rtc::PlatformFile file) {
solenberg566ef242015-11-06 15:34:49 -08001185 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00001186 FILE* aec_dump_file_stream = rtc::FdopenPlatformFileForWriting(file);
wu@webrtc.orga8910d22014-01-23 22:12:45 +00001187 if (!aec_dump_file_stream) {
1188 LOG(LS_ERROR) << "Could not open AEC dump file stream.";
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00001189 if (!rtc::ClosePlatformFile(file))
wu@webrtc.orga8910d22014-01-23 22:12:45 +00001190 LOG(LS_WARNING) << "Could not close file.";
1191 return false;
1192 }
wu@webrtc.orga9890802013-12-13 00:21:03 +00001193 StopAecDump();
wu@webrtc.orga8910d22014-01-23 22:12:45 +00001194 if (voe_wrapper_->processing()->StartDebugRecording(aec_dump_file_stream) !=
wu@webrtc.orga9890802013-12-13 00:21:03 +00001195 webrtc::AudioProcessing::kNoError) {
wu@webrtc.orga8910d22014-01-23 22:12:45 +00001196 LOG_RTCERR0(StartDebugRecording);
1197 fclose(aec_dump_file_stream);
wu@webrtc.orga9890802013-12-13 00:21:03 +00001198 return false;
1199 }
1200 is_dumping_aec_ = true;
1201 return true;
wu@webrtc.orga9890802013-12-13 00:21:03 +00001202}
1203
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001204void WebRtcVoiceEngine::StartAecDump(const std::string& filename) {
solenberg566ef242015-11-06 15:34:49 -08001205 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001206 if (!is_dumping_aec_) {
1207 // Start dumping AEC when we are not dumping.
1208 if (voe_wrapper_->processing()->StartDebugRecording(
1209 filename.c_str()) != webrtc::AudioProcessing::kNoError) {
wu@webrtc.orga9890802013-12-13 00:21:03 +00001210 LOG_RTCERR1(StartDebugRecording, filename.c_str());
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001211 } else {
1212 is_dumping_aec_ = true;
1213 }
1214 }
1215}
1216
1217void WebRtcVoiceEngine::StopAecDump() {
solenberg566ef242015-11-06 15:34:49 -08001218 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001219 if (is_dumping_aec_) {
1220 // Stop dumping AEC when we are dumping.
1221 if (voe_wrapper_->processing()->StopDebugRecording() !=
1222 webrtc::AudioProcessing::kNoError) {
1223 LOG_RTCERR0(StopDebugRecording);
1224 }
1225 is_dumping_aec_ = false;
1226 }
1227}
1228
ivoc112a3d82015-10-16 02:22:18 -07001229bool WebRtcVoiceEngine::StartRtcEventLog(rtc::PlatformFile file) {
solenberg566ef242015-11-06 15:34:49 -08001230 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
ivoc112a3d82015-10-16 02:22:18 -07001231 return voe_wrapper_->codec()->GetEventLog()->StartLogging(file);
1232}
1233
1234void WebRtcVoiceEngine::StopRtcEventLog() {
solenberg566ef242015-11-06 15:34:49 -08001235 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
ivoc112a3d82015-10-16 02:22:18 -07001236 voe_wrapper_->codec()->GetEventLog()->StopLogging();
1237}
1238
solenberg0a617e22015-10-20 15:49:38 -07001239int WebRtcVoiceEngine::CreateVoEChannel() {
solenberg566ef242015-11-06 15:34:49 -08001240 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
solenberg0a617e22015-10-20 15:49:38 -07001241 return voe_wrapper_->base()->CreateChannel(voe_config_);
sergeyu@chromium.org5bc25c42013-12-05 00:24:06 +00001242}
1243
solenbergc96df772015-10-21 13:01:53 -07001244class WebRtcVoiceMediaChannel::WebRtcAudioSendStream
mallinath@webrtc.org67ee6b92014-02-03 16:57:16 +00001245 : public AudioRenderer::Sink {
1246 public:
solenbergc96df772015-10-21 13:01:53 -07001247 WebRtcAudioSendStream(int ch, webrtc::AudioTransport* voe_audio_transport,
solenberg3a941542015-11-16 07:34:50 -08001248 uint32_t ssrc, const std::string& c_name,
1249 const std::vector<webrtc::RtpExtension>& extensions,
1250 webrtc::Call* call)
solenberg7add0582015-11-20 09:59:34 -08001251 : voe_audio_transport_(voe_audio_transport),
solenberg3a941542015-11-16 07:34:50 -08001252 call_(call),
1253 config_(nullptr) {
solenberg85a04962015-10-27 03:35:21 -07001254 RTC_DCHECK_GE(ch, 0);
1255 // TODO(solenberg): Once we're not using FakeWebRtcVoiceEngine anymore:
1256 // RTC_DCHECK(voe_audio_transport);
solenbergc96df772015-10-21 13:01:53 -07001257 RTC_DCHECK(call);
solenberg85a04962015-10-27 03:35:21 -07001258 audio_capture_thread_checker_.DetachFromThread();
solenberg3a941542015-11-16 07:34:50 -08001259 config_.rtp.ssrc = ssrc;
1260 config_.rtp.c_name = c_name;
1261 config_.voe_channel_id = ch;
1262 RecreateAudioSendStream(extensions);
solenbergc96df772015-10-21 13:01:53 -07001263 }
solenberg3a941542015-11-16 07:34:50 -08001264
solenbergc96df772015-10-21 13:01:53 -07001265 ~WebRtcAudioSendStream() override {
solenberg566ef242015-11-06 15:34:49 -08001266 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
solenbergc96df772015-10-21 13:01:53 -07001267 Stop();
1268 call_->DestroyAudioSendStream(stream_);
1269 }
henrike@webrtc.org1e09a712013-07-26 19:17:59 +00001270
solenberg3a941542015-11-16 07:34:50 -08001271 void RecreateAudioSendStream(
1272 const std::vector<webrtc::RtpExtension>& extensions) {
1273 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
1274 if (stream_) {
1275 call_->DestroyAudioSendStream(stream_);
1276 stream_ = nullptr;
1277 }
1278 config_.rtp.extensions = extensions;
1279 RTC_DCHECK(!stream_);
1280 stream_ = call_->CreateAudioSendStream(config_);
1281 RTC_CHECK(stream_);
1282 }
1283
1284 webrtc::AudioSendStream::Stats GetStats() const {
1285 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
1286 RTC_DCHECK(stream_);
1287 return stream_->GetStats();
1288 }
1289
mallinath@webrtc.org67ee6b92014-02-03 16:57:16 +00001290 // Starts the rendering by setting a sink to the renderer to get data
1291 // callback.
henrike@webrtc.orga7b98182014-02-21 15:51:43 +00001292 // This method is called on the libjingle worker thread.
mallinath@webrtc.org67ee6b92014-02-03 16:57:16 +00001293 // TODO(xians): Make sure Start() is called only once.
1294 void Start(AudioRenderer* renderer) {
solenberg566ef242015-11-06 15:34:49 -08001295 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
solenbergc96df772015-10-21 13:01:53 -07001296 RTC_DCHECK(renderer);
1297 if (renderer_) {
henrikg91d6ede2015-09-17 00:24:34 -07001298 RTC_DCHECK(renderer_ == renderer);
mallinath@webrtc.org67ee6b92014-02-03 16:57:16 +00001299 return;
1300 }
mallinath@webrtc.org67ee6b92014-02-03 16:57:16 +00001301 renderer->SetSink(this);
1302 renderer_ = renderer;
1303 }
1304
solenbergc96df772015-10-21 13:01:53 -07001305 // Stops rendering by setting the sink of the renderer to nullptr. No data
mallinath@webrtc.org67ee6b92014-02-03 16:57:16 +00001306 // callback will be received after this method.
henrike@webrtc.orga7b98182014-02-21 15:51:43 +00001307 // This method is called on the libjingle worker thread.
mallinath@webrtc.org67ee6b92014-02-03 16:57:16 +00001308 void Stop() {
solenberg566ef242015-11-06 15:34:49 -08001309 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
solenbergc96df772015-10-21 13:01:53 -07001310 if (renderer_) {
1311 renderer_->SetSink(nullptr);
1312 renderer_ = nullptr;
solenberg98c68862015-10-09 03:27:14 -07001313 }
mallinath@webrtc.org67ee6b92014-02-03 16:57:16 +00001314 }
1315
1316 // AudioRenderer::Sink implementation.
henrike@webrtc.orga7b98182014-02-21 15:51:43 +00001317 // This method is called on the audio thread.
kjellander@webrtc.org14665ff2015-03-04 12:58:35 +00001318 void OnData(const void* audio_data,
1319 int bits_per_sample,
1320 int sample_rate,
1321 int number_of_channels,
Peter Kastingdce40cf2015-08-24 14:52:23 -07001322 size_t number_of_frames) override {
solenberg566ef242015-11-06 15:34:49 -08001323 RTC_DCHECK(!worker_thread_checker_.CalledOnValidThread());
solenberg85a04962015-10-27 03:35:21 -07001324 RTC_DCHECK(audio_capture_thread_checker_.CalledOnValidThread());
solenbergc96df772015-10-21 13:01:53 -07001325 RTC_DCHECK(voe_audio_transport_);
solenberg7add0582015-11-20 09:59:34 -08001326 voe_audio_transport_->OnData(config_.voe_channel_id,
henrike@webrtc.orga7b98182014-02-21 15:51:43 +00001327 audio_data,
1328 bits_per_sample,
1329 sample_rate,
1330 number_of_channels,
1331 number_of_frames);
henrike@webrtc.orga7b98182014-02-21 15:51:43 +00001332 }
1333
1334 // Callback from the |renderer_| when it is going away. In case Start() has
1335 // never been called, this callback won't be triggered.
kjellander@webrtc.org14665ff2015-03-04 12:58:35 +00001336 void OnClose() override {
solenberg566ef242015-11-06 15:34:49 -08001337 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
solenbergc96df772015-10-21 13:01:53 -07001338 // Set |renderer_| to nullptr to make sure no more callback will get into
henrike@webrtc.orga7b98182014-02-21 15:51:43 +00001339 // the renderer.
solenbergc96df772015-10-21 13:01:53 -07001340 renderer_ = nullptr;
mallinath@webrtc.org67ee6b92014-02-03 16:57:16 +00001341 }
1342
1343 // Accessor to the VoE channel ID.
solenberg85a04962015-10-27 03:35:21 -07001344 int channel() const {
solenberg566ef242015-11-06 15:34:49 -08001345 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
solenberg7add0582015-11-20 09:59:34 -08001346 return config_.voe_channel_id;
solenberg85a04962015-10-27 03:35:21 -07001347 }
mallinath@webrtc.org67ee6b92014-02-03 16:57:16 +00001348
1349 private:
solenberg566ef242015-11-06 15:34:49 -08001350 rtc::ThreadChecker worker_thread_checker_;
solenberg85a04962015-10-27 03:35:21 -07001351 rtc::ThreadChecker audio_capture_thread_checker_;
solenbergc96df772015-10-21 13:01:53 -07001352 webrtc::AudioTransport* const voe_audio_transport_ = nullptr;
1353 webrtc::Call* call_ = nullptr;
solenberg3a941542015-11-16 07:34:50 -08001354 webrtc::AudioSendStream::Config config_;
1355 // The stream is owned by WebRtcAudioSendStream and may be reallocated if
1356 // configuration changes.
solenbergc96df772015-10-21 13:01:53 -07001357 webrtc::AudioSendStream* stream_ = nullptr;
mallinath@webrtc.org67ee6b92014-02-03 16:57:16 +00001358
1359 // Raw pointer to AudioRenderer owned by LocalAudioTrackHandler.
1360 // PeerConnection will make sure invalidating the pointer before the object
1361 // goes away.
solenbergc96df772015-10-21 13:01:53 -07001362 AudioRenderer* renderer_ = nullptr;
henrike@webrtc.orga7b98182014-02-21 15:51:43 +00001363
solenbergc96df772015-10-21 13:01:53 -07001364 RTC_DISALLOW_IMPLICIT_CONSTRUCTORS(WebRtcAudioSendStream);
1365};
1366
1367class WebRtcVoiceMediaChannel::WebRtcAudioReceiveStream {
1368 public:
solenberg7add0582015-11-20 09:59:34 -08001369 WebRtcAudioReceiveStream(int ch, uint32_t remote_ssrc, uint32_t local_ssrc,
1370 bool use_combined_bwe, const std::string& sync_group,
1371 const std::vector<webrtc::RtpExtension>& extensions,
1372 webrtc::Call* call)
1373 : call_(call),
1374 config_() {
1375 RTC_DCHECK_GE(ch, 0);
1376 RTC_DCHECK(call);
1377 config_.rtp.remote_ssrc = remote_ssrc;
1378 config_.rtp.local_ssrc = local_ssrc;
1379 config_.voe_channel_id = ch;
1380 config_.sync_group = sync_group;
1381 RecreateAudioReceiveStream(use_combined_bwe, extensions);
1382 }
solenbergc96df772015-10-21 13:01:53 -07001383
solenberg7add0582015-11-20 09:59:34 -08001384 ~WebRtcAudioReceiveStream() {
1385 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
1386 call_->DestroyAudioReceiveStream(stream_);
1387 }
1388
1389 void RecreateAudioReceiveStream(
1390 const std::vector<webrtc::RtpExtension>& extensions) {
1391 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
1392 RecreateAudioReceiveStream(config_.combined_audio_video_bwe, extensions);
1393 }
1394 void RecreateAudioReceiveStream(bool use_combined_bwe) {
1395 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
1396 RecreateAudioReceiveStream(use_combined_bwe, config_.rtp.extensions);
1397 }
1398
1399 webrtc::AudioReceiveStream::Stats GetStats() const {
1400 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
1401 RTC_DCHECK(stream_);
1402 return stream_->GetStats();
1403 }
1404
1405 int channel() const {
1406 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
1407 return config_.voe_channel_id;
1408 }
solenbergc96df772015-10-21 13:01:53 -07001409
1410 private:
solenberg7add0582015-11-20 09:59:34 -08001411 void RecreateAudioReceiveStream(bool use_combined_bwe,
1412 const std::vector<webrtc::RtpExtension>& extensions) {
1413 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
1414 if (stream_) {
1415 call_->DestroyAudioReceiveStream(stream_);
1416 stream_ = nullptr;
1417 }
1418 config_.rtp.extensions = extensions;
1419 config_.combined_audio_video_bwe = use_combined_bwe;
1420 RTC_DCHECK(!stream_);
1421 stream_ = call_->CreateAudioReceiveStream(config_);
1422 RTC_CHECK(stream_);
1423 }
1424
1425 rtc::ThreadChecker worker_thread_checker_;
1426 webrtc::Call* call_ = nullptr;
1427 webrtc::AudioReceiveStream::Config config_;
1428 // The stream is owned by WebRtcAudioReceiveStream and may be reallocated if
1429 // configuration changes.
1430 webrtc::AudioReceiveStream* stream_ = nullptr;
solenbergc96df772015-10-21 13:01:53 -07001431
1432 RTC_DISALLOW_IMPLICIT_CONSTRUCTORS(WebRtcAudioReceiveStream);
henrike@webrtc.org1e09a712013-07-26 19:17:59 +00001433};
1434
Fredrik Solenberg709ed672015-09-15 12:26:33 +02001435WebRtcVoiceMediaChannel::WebRtcVoiceMediaChannel(WebRtcVoiceEngine* engine,
Fredrik Solenbergb071a192015-09-17 16:42:56 +02001436 const AudioOptions& options,
Fredrik Solenberg709ed672015-09-15 12:26:33 +02001437 webrtc::Call* call)
solenberg566ef242015-11-06 15:34:49 -08001438 : engine_(engine), call_(call) {
solenberg0a617e22015-10-20 15:49:38 -07001439 LOG(LS_VERBOSE) << "WebRtcVoiceMediaChannel::WebRtcVoiceMediaChannel";
solenberg566ef242015-11-06 15:34:49 -08001440 RTC_DCHECK(call);
solenberg0a617e22015-10-20 15:49:38 -07001441 engine->RegisterChannel(this);
Fredrik Solenbergb071a192015-09-17 16:42:56 +02001442 SetOptions(options);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001443}
1444
1445WebRtcVoiceMediaChannel::~WebRtcVoiceMediaChannel() {
solenberg566ef242015-11-06 15:34:49 -08001446 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
solenberg0a617e22015-10-20 15:49:38 -07001447 LOG(LS_VERBOSE) << "WebRtcVoiceMediaChannel::~WebRtcVoiceMediaChannel";
solenberg7add0582015-11-20 09:59:34 -08001448 // TODO(solenberg): Should be able to delete the streams directly, without
1449 // going through RemoveNnStream(), once stream objects handle
1450 // all (de)configuration.
solenbergc96df772015-10-21 13:01:53 -07001451 while (!send_streams_.empty()) {
1452 RemoveSendStream(send_streams_.begin()->first);
solenbergd97ec302015-10-07 01:40:33 -07001453 }
solenberg7add0582015-11-20 09:59:34 -08001454 while (!recv_streams_.empty()) {
1455 RemoveRecvStream(recv_streams_.begin()->first);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001456 }
solenberg0a617e22015-10-20 15:49:38 -07001457 engine()->UnregisterChannel(this);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001458}
1459
Peter Thatcherc2ee2c82015-08-07 16:05:34 -07001460bool WebRtcVoiceMediaChannel::SetSendParameters(
1461 const AudioSendParameters& params) {
solenberg566ef242015-11-06 15:34:49 -08001462 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
Peter Thatcherc2ee2c82015-08-07 16:05:34 -07001463 // TODO(pthatcher): Refactor this to be more clean now that we have
1464 // all the information at once.
solenberg3a941542015-11-16 07:34:50 -08001465
1466 if (!SetSendCodecs(params.codecs)) {
1467 return false;
1468 }
1469
1470 std::vector<webrtc::RtpExtension> send_rtp_extensions =
1471 FindAudioRtpHeaderExtensions(params.extensions);
1472 if (send_rtp_extensions_ != send_rtp_extensions) {
1473 send_rtp_extensions_.swap(send_rtp_extensions);
1474 for (auto& it : send_streams_) {
1475 it.second->RecreateAudioSendStream(send_rtp_extensions_);
1476 }
1477 }
1478
1479 if (!SetMaxSendBandwidth(params.max_bandwidth_bps)) {
1480 return false;
1481 }
1482 return SetOptions(params.options);
Peter Thatcherc2ee2c82015-08-07 16:05:34 -07001483}
1484
1485bool WebRtcVoiceMediaChannel::SetRecvParameters(
1486 const AudioRecvParameters& params) {
solenberg566ef242015-11-06 15:34:49 -08001487 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
Peter Thatcherc2ee2c82015-08-07 16:05:34 -07001488 // TODO(pthatcher): Refactor this to be more clean now that we have
1489 // all the information at once.
solenberg7add0582015-11-20 09:59:34 -08001490
1491 if (!SetRecvCodecs(params.codecs)) {
1492 return false;
1493 }
1494
1495 std::vector<webrtc::RtpExtension> recv_rtp_extensions =
1496 FindAudioRtpHeaderExtensions(params.extensions);
1497 if (recv_rtp_extensions_ != recv_rtp_extensions) {
1498 recv_rtp_extensions_.swap(recv_rtp_extensions);
1499 for (auto& it : recv_streams_) {
1500 it.second->RecreateAudioReceiveStream(recv_rtp_extensions_);
1501 }
1502 }
1503
1504 return true;
Peter Thatcherc2ee2c82015-08-07 16:05:34 -07001505}
1506
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001507bool WebRtcVoiceMediaChannel::SetOptions(const AudioOptions& options) {
solenberg566ef242015-11-06 15:34:49 -08001508 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001509 LOG(LS_INFO) << "Setting voice channel options: "
1510 << options.ToString();
1511
wu@webrtc.orgde305012013-10-31 15:40:38 +00001512 // Check if DSCP value is changed from previous.
1513 bool dscp_option_changed = (options_.dscp != options.dscp);
1514
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001515 // We retain all of the existing options, and apply the given ones
1516 // on top. This means there is no way to "clear" options such that
1517 // they go back to the engine default.
1518 options_.SetAll(options);
1519
1520 if (send_ != SEND_NOTHING) {
solenberg63b34542015-09-29 06:06:31 -07001521 if (!engine()->ApplyOptions(options_)) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001522 LOG(LS_WARNING) <<
solenberg63b34542015-09-29 06:06:31 -07001523 "Failed to apply engine options during channel SetOptions.";
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001524 return false;
1525 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001526 }
1527
wu@webrtc.orgde305012013-10-31 15:40:38 +00001528 if (dscp_option_changed) {
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00001529 rtc::DiffServCodePoint dscp = rtc::DSCP_DEFAULT;
kwiberg102c6a62015-10-30 02:47:38 -07001530 if (options_.dscp.value_or(false))
wu@webrtc.orgde305012013-10-31 15:40:38 +00001531 dscp = kAudioDscpValue;
1532 if (MediaChannel::SetDscp(dscp) != 0) {
1533 LOG(LS_WARNING) << "Failed to set DSCP settings for audio channel";
1534 }
1535 }
solenberg8fb30c32015-10-13 03:06:58 -07001536
solenbergc96df772015-10-21 13:01:53 -07001537 // TODO(solenberg): Don't recreate unless options changed.
solenberg7add0582015-11-20 09:59:34 -08001538 for (auto& it : recv_streams_) {
1539 it.second->RecreateAudioReceiveStream(
1540 options_.combined_audio_video_bwe.value_or(false));
1541 }
solenberg8fb30c32015-10-13 03:06:58 -07001542
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001543 LOG(LS_INFO) << "Set voice channel options. Current options: "
1544 << options_.ToString();
1545 return true;
1546}
1547
1548bool WebRtcVoiceMediaChannel::SetRecvCodecs(
1549 const std::vector<AudioCodec>& codecs) {
solenberg566ef242015-11-06 15:34:49 -08001550 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
solenberg8fb30c32015-10-13 03:06:58 -07001551
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001552 // Set the payload types to be used for incoming media.
solenberg0b675462015-10-09 01:37:09 -07001553 LOG(LS_INFO) << "Setting receive voice codecs.";
solenberg0b675462015-10-09 01:37:09 -07001554
1555 if (!VerifyUniquePayloadTypes(codecs)) {
1556 LOG(LS_ERROR) << "Codec payload types overlap.";
1557 return false;
1558 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001559
1560 std::vector<AudioCodec> new_codecs;
1561 // Find all new codecs. We allow adding new codecs but don't allow changing
1562 // the payload type of codecs that is already configured since we might
1563 // already be receiving packets with that payload type.
Fredrik Solenbergaf9fb212015-08-26 10:45:53 +02001564 for (const AudioCodec& codec : codecs) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001565 AudioCodec old_codec;
Fredrik Solenbergaf9fb212015-08-26 10:45:53 +02001566 if (FindCodec(recv_codecs_, codec, &old_codec)) {
1567 if (old_codec.id != codec.id) {
1568 LOG(LS_ERROR) << codec.name << " payload type changed.";
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001569 return false;
1570 }
1571 } else {
Fredrik Solenbergaf9fb212015-08-26 10:45:53 +02001572 new_codecs.push_back(codec);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001573 }
1574 }
1575 if (new_codecs.empty()) {
1576 // There are no new codecs to configure. Already configured codecs are
1577 // never removed.
1578 return true;
1579 }
1580
1581 if (playout_) {
1582 // Receive codecs can not be changed while playing. So we temporarily
1583 // pause playout.
1584 PausePlayout();
1585 }
1586
solenberg26c8c912015-11-27 04:00:25 -08001587 bool result = true;
1588 for (const AudioCodec& codec : new_codecs) {
1589 webrtc::CodecInst voe_codec;
1590 if (WebRtcVoiceEngine::ToCodecInst(codec, &voe_codec)) {
1591 LOG(LS_INFO) << ToString(codec);
1592 voe_codec.pltype = codec.id;
1593 for (const auto& ch : recv_streams_) {
1594 if (engine()->voe()->codec()->SetRecPayloadType(
1595 ch.second->channel(), voe_codec) == -1) {
1596 LOG_RTCERR2(SetRecPayloadType, ch.second->channel(),
1597 ToString(voe_codec));
1598 result = false;
1599 }
1600 }
1601 } else {
1602 LOG(LS_WARNING) << "Unknown codec " << ToString(codec);
1603 result = false;
1604 break;
1605 }
1606 }
Fredrik Solenbergaf9fb212015-08-26 10:45:53 +02001607 if (result) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001608 recv_codecs_ = codecs;
1609 }
1610
1611 if (desired_playout_ && !playout_) {
1612 ResumePlayout();
1613 }
Fredrik Solenbergaf9fb212015-08-26 10:45:53 +02001614 return result;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001615}
1616
1617bool WebRtcVoiceMediaChannel::SetSendCodecs(
wu@webrtc.orgcadf9042013-08-30 21:24:16 +00001618 int channel, const std::vector<AudioCodec>& codecs) {
buildbot@webrtc.orgae740dd2014-06-17 10:56:41 +00001619 // Disable VAD, FEC, and RED unless we know the other side wants them.
wu@webrtc.orgcadf9042013-08-30 21:24:16 +00001620 engine()->voe()->codec()->SetVADStatus(channel, false);
1621 engine()->voe()->rtp()->SetNACKStatus(channel, false, 0);
buildbot@webrtc.orgae740dd2014-06-17 10:56:41 +00001622 engine()->voe()->rtp()->SetREDStatus(channel, false);
1623 engine()->voe()->codec()->SetFECStatus(channel, false);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001624
1625 // Scan through the list to figure out the codec to use for sending, along
1626 // with the proper configuration for VAD and DTMF.
henrike@webrtc.org704bf9e2014-02-27 17:52:04 +00001627 bool found_send_codec = false;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001628 webrtc::CodecInst send_codec;
1629 memset(&send_codec, 0, sizeof(send_codec));
1630
wu@webrtc.org05e7b442014-04-01 17:44:24 +00001631 bool nack_enabled = nack_enabled_;
buildbot@webrtc.org3ffa1f92014-07-02 19:51:26 +00001632 bool enable_codec_fec = false;
Minyue Li7100dcd2015-03-27 05:05:59 +01001633 bool enable_opus_dtx = false;
minyue@webrtc.org26236952014-10-29 02:27:08 +00001634 int opus_max_playback_rate = 0;
buildbot@webrtc.org5d639b32014-09-10 07:57:12 +00001635
henrike@webrtc.org704bf9e2014-02-27 17:52:04 +00001636 // Set send codec (the first non-telephone-event/CN codec)
Fredrik Solenbergaf9fb212015-08-26 10:45:53 +02001637 for (const AudioCodec& codec : codecs) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001638 // Ignore codecs we don't know about. The negotiation step should prevent
1639 // this, but double-check to be sure.
1640 webrtc::CodecInst voe_codec;
solenberg26c8c912015-11-27 04:00:25 -08001641 if (!WebRtcVoiceEngine::ToCodecInst(codec, &voe_codec)) {
Fredrik Solenbergaf9fb212015-08-26 10:45:53 +02001642 LOG(LS_WARNING) << "Unknown codec " << ToString(codec);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001643 continue;
1644 }
1645
Fredrik Solenbergaf9fb212015-08-26 10:45:53 +02001646 if (IsCodec(codec, kDtmfCodecName) || IsCodec(codec, kCnCodecName)) {
henrike@webrtc.org704bf9e2014-02-27 17:52:04 +00001647 // Skip telephone-event/CN codec, which will be handled later.
1648 continue;
1649 }
1650
henrike@webrtc.org704bf9e2014-02-27 17:52:04 +00001651 // We'll use the first codec in the list to actually send audio data.
1652 // Be sure to use the payload type requested by the remote side.
buildbot@webrtc.orgae740dd2014-06-17 10:56:41 +00001653 // "red", for RED audio, is a special case where the actual codec to be
henrike@webrtc.org704bf9e2014-02-27 17:52:04 +00001654 // used is specified in params.
Fredrik Solenbergaf9fb212015-08-26 10:45:53 +02001655 if (IsCodec(codec, kRedCodecName)) {
henrike@webrtc.org704bf9e2014-02-27 17:52:04 +00001656 // Parse out the RED parameters. If we fail, just ignore RED;
1657 // we don't support all possible params/usage scenarios.
Fredrik Solenbergaf9fb212015-08-26 10:45:53 +02001658 if (!GetRedSendCodec(codec, codecs, &send_codec)) {
henrike@webrtc.org704bf9e2014-02-27 17:52:04 +00001659 continue;
1660 }
1661
1662 // Enable redundant encoding of the specified codec. Treat any
1663 // failure as a fatal internal error.
buildbot@webrtc.orgae740dd2014-06-17 10:56:41 +00001664 LOG(LS_INFO) << "Enabling RED on channel " << channel;
Fredrik Solenbergaf9fb212015-08-26 10:45:53 +02001665 if (engine()->voe()->rtp()->SetREDStatus(channel, true, codec.id) == -1) {
1666 LOG_RTCERR3(SetREDStatus, channel, true, codec.id);
henrike@webrtc.org704bf9e2014-02-27 17:52:04 +00001667 return false;
1668 }
1669 } else {
1670 send_codec = voe_codec;
Fredrik Solenbergaf9fb212015-08-26 10:45:53 +02001671 nack_enabled = IsNackEnabled(codec);
Minyue Li7100dcd2015-03-27 05:05:59 +01001672 // For Opus as the send codec, we are to determine inband FEC, maximum
1673 // playback rate, and opus internal dtx.
Fredrik Solenbergaf9fb212015-08-26 10:45:53 +02001674 if (IsCodec(codec, kOpusCodecName)) {
1675 GetOpusConfig(codec, &send_codec, &enable_codec_fec,
Minyue Li7100dcd2015-03-27 05:05:59 +01001676 &opus_max_playback_rate, &enable_opus_dtx);
buildbot@webrtc.org5d639b32014-09-10 07:57:12 +00001677 }
Brave Yao5225dd82015-03-26 07:39:19 +08001678
1679 // Set packet size if the AudioCodec param kCodecParamPTime is set.
1680 int ptime_ms = 0;
Fredrik Solenbergaf9fb212015-08-26 10:45:53 +02001681 if (codec.GetParam(kCodecParamPTime, &ptime_ms)) {
solenberg26c8c912015-11-27 04:00:25 -08001682 if (!WebRtcVoiceCodecs::SetPTimeAsPacketSize(&send_codec, ptime_ms)) {
Brave Yao5225dd82015-03-26 07:39:19 +08001683 LOG(LS_WARNING) << "Failed to set packet size for codec "
1684 << send_codec.plname;
1685 return false;
1686 }
1687 }
henrike@webrtc.org704bf9e2014-02-27 17:52:04 +00001688 }
1689 found_send_codec = true;
1690 break;
1691 }
1692
wu@webrtc.org05e7b442014-04-01 17:44:24 +00001693 if (nack_enabled_ != nack_enabled) {
1694 SetNack(channel, nack_enabled);
1695 nack_enabled_ = nack_enabled;
1696 }
1697
henrike@webrtc.org704bf9e2014-02-27 17:52:04 +00001698 if (!found_send_codec) {
1699 LOG(LS_WARNING) << "Received empty list of codecs.";
1700 return false;
1701 }
1702
1703 // Set the codec immediately, since SetVADStatus() depends on whether
1704 // the current codec is mono or stereo.
1705 if (!SetSendCodec(channel, send_codec))
1706 return false;
1707
buildbot@webrtc.org3ffa1f92014-07-02 19:51:26 +00001708 // FEC should be enabled after SetSendCodec.
1709 if (enable_codec_fec) {
1710 LOG(LS_INFO) << "Attempt to enable codec internal FEC on channel "
1711 << channel;
buildbot@webrtc.org3ffa1f92014-07-02 19:51:26 +00001712 if (engine()->voe()->codec()->SetFECStatus(channel, true) == -1) {
1713 // Enable codec internal FEC. Treat any failure as fatal internal error.
1714 LOG_RTCERR2(SetFECStatus, channel, true);
1715 return false;
1716 }
buildbot@webrtc.org3ffa1f92014-07-02 19:51:26 +00001717 }
1718
Minyue Li7100dcd2015-03-27 05:05:59 +01001719 if (IsCodec(send_codec, kOpusCodecName)) {
1720 // DTX and maxplaybackrate should be set after SetSendCodec. Because current
1721 // send codec has to be Opus.
1722
1723 // Set Opus internal DTX.
1724 LOG(LS_INFO) << "Attempt to "
solenbergbd138382015-11-20 16:08:07 -08001725 << (enable_opus_dtx ? "enable" : "disable")
Minyue Li7100dcd2015-03-27 05:05:59 +01001726 << " Opus DTX on channel "
buildbot@webrtc.org5d639b32014-09-10 07:57:12 +00001727 << channel;
Minyue Li7100dcd2015-03-27 05:05:59 +01001728 if (engine()->voe()->codec()->SetOpusDtx(channel, enable_opus_dtx)) {
1729 LOG_RTCERR2(SetOpusDtx, channel, enable_opus_dtx);
1730 return false;
1731 }
1732
1733 // If opus_max_playback_rate <= 0, the default maximum playback rate
1734 // (48 kHz) will be used.
1735 if (opus_max_playback_rate > 0) {
1736 LOG(LS_INFO) << "Attempt to set maximum playback rate to "
1737 << opus_max_playback_rate
1738 << " Hz on channel "
1739 << channel;
1740 if (engine()->voe()->codec()->SetOpusMaxPlaybackRate(
1741 channel, opus_max_playback_rate) == -1) {
1742 LOG_RTCERR2(SetOpusMaxPlaybackRate, channel, opus_max_playback_rate);
1743 return false;
1744 }
buildbot@webrtc.org5d639b32014-09-10 07:57:12 +00001745 }
buildbot@webrtc.org5d639b32014-09-10 07:57:12 +00001746 }
1747
henrike@webrtc.org704bf9e2014-02-27 17:52:04 +00001748 // Always update the |send_codec_| to the currently set send codec.
1749 send_codec_.reset(new webrtc::CodecInst(send_codec));
1750
minyue@webrtc.org26236952014-10-29 02:27:08 +00001751 if (send_bitrate_setting_) {
1752 SetSendBitrateInternal(send_bitrate_bps_);
henrike@webrtc.org704bf9e2014-02-27 17:52:04 +00001753 }
1754
1755 // Loop through the codecs list again to config the telephone-event/CN codec.
Fredrik Solenbergaf9fb212015-08-26 10:45:53 +02001756 for (const AudioCodec& codec : codecs) {
henrike@webrtc.org704bf9e2014-02-27 17:52:04 +00001757 // Ignore codecs we don't know about. The negotiation step should prevent
1758 // this, but double-check to be sure.
1759 webrtc::CodecInst voe_codec;
solenberg26c8c912015-11-27 04:00:25 -08001760 if (!WebRtcVoiceEngine::ToCodecInst(codec, &voe_codec)) {
Fredrik Solenbergaf9fb212015-08-26 10:45:53 +02001761 LOG(LS_WARNING) << "Unknown codec " << ToString(codec);
henrike@webrtc.org704bf9e2014-02-27 17:52:04 +00001762 continue;
1763 }
1764
wu@webrtc.org9dba5252013-08-05 20:36:57 +00001765 // Find the DTMF telephone event "codec" and tell VoiceEngine channels
1766 // about it.
Fredrik Solenbergaf9fb212015-08-26 10:45:53 +02001767 if (IsCodec(codec, kDtmfCodecName)) {
wu@webrtc.orgcadf9042013-08-30 21:24:16 +00001768 if (engine()->voe()->dtmf()->SetSendTelephoneEventPayloadType(
Fredrik Solenbergaf9fb212015-08-26 10:45:53 +02001769 channel, codec.id) == -1) {
1770 LOG_RTCERR2(SetSendTelephoneEventPayloadType, channel, codec.id);
wu@webrtc.orgcadf9042013-08-30 21:24:16 +00001771 return false;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001772 }
Fredrik Solenbergaf9fb212015-08-26 10:45:53 +02001773 } else if (IsCodec(codec, kCnCodecName)) {
henrike@webrtc.org704bf9e2014-02-27 17:52:04 +00001774 // Turn voice activity detection/comfort noise on if supported.
1775 // Set the wideband CN payload type appropriately.
1776 // (narrowband always uses the static payload type 13).
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001777 webrtc::PayloadFrequencies cn_freq;
Fredrik Solenbergaf9fb212015-08-26 10:45:53 +02001778 switch (codec.clockrate) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001779 case 8000:
1780 cn_freq = webrtc::kFreq8000Hz;
1781 break;
1782 case 16000:
1783 cn_freq = webrtc::kFreq16000Hz;
1784 break;
1785 case 32000:
1786 cn_freq = webrtc::kFreq32000Hz;
1787 break;
1788 default:
Fredrik Solenbergaf9fb212015-08-26 10:45:53 +02001789 LOG(LS_WARNING) << "CN frequency " << codec.clockrate
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001790 << " not supported.";
1791 continue;
1792 }
wu@webrtc.orgcadf9042013-08-30 21:24:16 +00001793 // Set the CN payloadtype and the VAD status.
1794 // The CN payload type for 8000 Hz clockrate is fixed at 13.
1795 if (cn_freq != webrtc::kFreq8000Hz) {
1796 if (engine()->voe()->codec()->SetSendCNPayloadType(
Fredrik Solenbergaf9fb212015-08-26 10:45:53 +02001797 channel, codec.id, cn_freq) == -1) {
1798 LOG_RTCERR3(SetSendCNPayloadType, channel, codec.id, cn_freq);
wu@webrtc.orgcadf9042013-08-30 21:24:16 +00001799 // TODO(ajm): This failure condition will be removed from VoE.
1800 // Restore the return here when we update to a new enough webrtc.
1801 //
1802 // Not returning false because the SetSendCNPayloadType will fail if
1803 // the channel is already sending.
1804 // This can happen if the remote description is applied twice, for
1805 // example in the case of ROAP on top of JSEP, where both side will
1806 // send the offer.
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001807 }
wu@webrtc.orgcadf9042013-08-30 21:24:16 +00001808 }
wu@webrtc.orgcadf9042013-08-30 21:24:16 +00001809 // Only turn on VAD if we have a CN payload type that matches the
1810 // clockrate for the codec we are going to use.
Fredrik Solenbergaf9fb212015-08-26 10:45:53 +02001811 if (codec.clockrate == send_codec.plfreq && send_codec.channels != 2) {
Minyue Li7100dcd2015-03-27 05:05:59 +01001812 // TODO(minyue): If CN frequency == 48000 Hz is allowed, consider the
1813 // interaction between VAD and Opus FEC.
wu@webrtc.orgcadf9042013-08-30 21:24:16 +00001814 LOG(LS_INFO) << "Enabling VAD";
1815 if (engine()->voe()->codec()->SetVADStatus(channel, true) == -1) {
1816 LOG_RTCERR2(SetVADStatus, channel, true);
1817 return false;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001818 }
1819 }
1820 }
wu@webrtc.org1d1ffc92013-10-16 18:12:02 +00001821 }
wu@webrtc.orgcadf9042013-08-30 21:24:16 +00001822 return true;
1823}
1824
1825bool WebRtcVoiceMediaChannel::SetSendCodecs(
1826 const std::vector<AudioCodec>& codecs) {
solenberg566ef242015-11-06 15:34:49 -08001827 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
solenbergd97ec302015-10-07 01:40:33 -07001828
wu@webrtc.orgcadf9042013-08-30 21:24:16 +00001829 dtmf_allowed_ = false;
Fredrik Solenbergaf9fb212015-08-26 10:45:53 +02001830 for (const AudioCodec& codec : codecs) {
wu@webrtc.orgcadf9042013-08-30 21:24:16 +00001831 // Find the DTMF telephone event "codec".
Fredrik Solenbergaf9fb212015-08-26 10:45:53 +02001832 if (IsCodec(codec, kDtmfCodecName)) {
wu@webrtc.orgcadf9042013-08-30 21:24:16 +00001833 dtmf_allowed_ = true;
1834 }
1835 }
1836
1837 // Cache the codecs in order to configure the channel created later.
1838 send_codecs_ = codecs;
solenbergc96df772015-10-21 13:01:53 -07001839 for (const auto& ch : send_streams_) {
Fredrik Solenbergaf9fb212015-08-26 10:45:53 +02001840 if (!SetSendCodecs(ch.second->channel(), codecs)) {
wu@webrtc.orgcadf9042013-08-30 21:24:16 +00001841 return false;
1842 }
1843 }
1844
wu@webrtc.org05e7b442014-04-01 17:44:24 +00001845 // Set nack status on receive channels and update |nack_enabled_|.
solenberg7add0582015-11-20 09:59:34 -08001846 for (const auto& ch : recv_streams_) {
solenberg0a617e22015-10-20 15:49:38 -07001847 SetNack(ch.second->channel(), nack_enabled_);
wu@webrtc.org9dba5252013-08-05 20:36:57 +00001848 }
solenberg0a617e22015-10-20 15:49:38 -07001849
1850 return true;
wu@webrtc.org9dba5252013-08-05 20:36:57 +00001851}
1852
wu@webrtc.orgcadf9042013-08-30 21:24:16 +00001853void WebRtcVoiceMediaChannel::SetNack(int channel, bool nack_enabled) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001854 if (nack_enabled) {
wu@webrtc.orgcadf9042013-08-30 21:24:16 +00001855 LOG(LS_INFO) << "Enabling NACK for channel " << channel;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001856 engine()->voe()->rtp()->SetNACKStatus(channel, true, kNackMaxPackets);
1857 } else {
wu@webrtc.orgcadf9042013-08-30 21:24:16 +00001858 LOG(LS_INFO) << "Disabling NACK for channel " << channel;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001859 engine()->voe()->rtp()->SetNACKStatus(channel, false, 0);
1860 }
1861}
1862
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001863bool WebRtcVoiceMediaChannel::SetSendCodec(
wu@webrtc.org9dba5252013-08-05 20:36:57 +00001864 int channel, const webrtc::CodecInst& send_codec) {
1865 LOG(LS_INFO) << "Send channel " << channel << " selected voice codec "
1866 << ToString(send_codec) << ", bitrate=" << send_codec.rate;
1867
wu@webrtc.org05e7b442014-04-01 17:44:24 +00001868 webrtc::CodecInst current_codec;
1869 if (engine()->voe()->codec()->GetSendCodec(channel, current_codec) == 0 &&
1870 (send_codec == current_codec)) {
1871 // Codec is already configured, we can return without setting it again.
1872 return true;
1873 }
1874
wu@webrtc.org9dba5252013-08-05 20:36:57 +00001875 if (engine()->voe()->codec()->SetSendCodec(channel, send_codec) == -1) {
1876 LOG_RTCERR2(SetSendCodec, channel, ToString(send_codec));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001877 return false;
1878 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001879 return true;
1880}
1881
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001882bool WebRtcVoiceMediaChannel::SetPlayout(bool playout) {
1883 desired_playout_ = playout;
1884 return ChangePlayout(desired_playout_);
1885}
1886
1887bool WebRtcVoiceMediaChannel::PausePlayout() {
1888 return ChangePlayout(false);
1889}
1890
1891bool WebRtcVoiceMediaChannel::ResumePlayout() {
1892 return ChangePlayout(desired_playout_);
1893}
1894
1895bool WebRtcVoiceMediaChannel::ChangePlayout(bool playout) {
solenberg566ef242015-11-06 15:34:49 -08001896 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001897 if (playout_ == playout) {
1898 return true;
1899 }
1900
solenberg7add0582015-11-20 09:59:34 -08001901 for (const auto& ch : recv_streams_) {
Fredrik Solenbergaf9fb212015-08-26 10:45:53 +02001902 if (!SetPlayout(ch.second->channel(), playout)) {
henrike@webrtc.org1e09a712013-07-26 19:17:59 +00001903 LOG(LS_ERROR) << "SetPlayout " << playout << " on channel "
Fredrik Solenbergaf9fb212015-08-26 10:45:53 +02001904 << ch.second->channel() << " failed";
solenberg1ac56142015-10-13 03:58:19 -07001905 return false;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001906 }
1907 }
solenberg1ac56142015-10-13 03:58:19 -07001908 playout_ = playout;
1909 return true;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001910}
1911
1912bool WebRtcVoiceMediaChannel::SetSend(SendFlags send) {
1913 desired_send_ = send;
solenbergc96df772015-10-21 13:01:53 -07001914 if (!send_streams_.empty()) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001915 return ChangeSend(desired_send_);
solenbergc96df772015-10-21 13:01:53 -07001916 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001917 return true;
1918}
1919
1920bool WebRtcVoiceMediaChannel::PauseSend() {
1921 return ChangeSend(SEND_NOTHING);
1922}
1923
1924bool WebRtcVoiceMediaChannel::ResumeSend() {
1925 return ChangeSend(desired_send_);
1926}
1927
1928bool WebRtcVoiceMediaChannel::ChangeSend(SendFlags send) {
1929 if (send_ == send) {
1930 return true;
1931 }
1932
solenberg63b34542015-09-29 06:06:31 -07001933 // Apply channel specific options.
1934 if (send == SEND_MICROPHONE) {
1935 engine()->ApplyOptions(options_);
1936 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001937
wu@webrtc.org9dba5252013-08-05 20:36:57 +00001938 // Change the settings on each send channel.
solenbergc96df772015-10-21 13:01:53 -07001939 for (const auto& ch : send_streams_) {
solenberg63b34542015-09-29 06:06:31 -07001940 if (!ChangeSend(ch.second->channel(), send)) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001941 return false;
solenberg63b34542015-09-29 06:06:31 -07001942 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001943 }
wu@webrtc.org9dba5252013-08-05 20:36:57 +00001944
solenberg63b34542015-09-29 06:06:31 -07001945 // Clear up the options after stopping sending. Since we may previously have
1946 // applied the channel specific options, now apply the original options stored
1947 // in WebRtcVoiceEngine.
1948 if (send == SEND_NOTHING) {
1949 engine()->ApplyOptions(engine()->GetOptions());
1950 }
wu@webrtc.org9dba5252013-08-05 20:36:57 +00001951
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001952 send_ = send;
1953 return true;
1954}
1955
wu@webrtc.org9dba5252013-08-05 20:36:57 +00001956bool WebRtcVoiceMediaChannel::ChangeSend(int channel, SendFlags send) {
1957 if (send == SEND_MICROPHONE) {
1958 if (engine()->voe()->base()->StartSend(channel) == -1) {
1959 LOG_RTCERR1(StartSend, channel);
1960 return false;
1961 }
wu@webrtc.org9dba5252013-08-05 20:36:57 +00001962 } else { // SEND_NOTHING
henrikg91d6ede2015-09-17 00:24:34 -07001963 RTC_DCHECK(send == SEND_NOTHING);
wu@webrtc.org9dba5252013-08-05 20:36:57 +00001964 if (engine()->voe()->base()->StopSend(channel) == -1) {
1965 LOG_RTCERR1(StopSend, channel);
1966 return false;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001967 }
1968 }
1969
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001970 return true;
1971}
1972
Peter Boström0c4e06b2015-10-07 12:23:21 +02001973bool WebRtcVoiceMediaChannel::SetAudioSend(uint32_t ssrc,
1974 bool enable,
solenberg1dd98f32015-09-10 01:57:14 -07001975 const AudioOptions* options,
1976 AudioRenderer* renderer) {
solenberg566ef242015-11-06 15:34:49 -08001977 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
solenberg1dd98f32015-09-10 01:57:14 -07001978 // TODO(solenberg): The state change should be fully rolled back if any one of
1979 // these calls fail.
1980 if (!SetLocalRenderer(ssrc, renderer)) {
1981 return false;
1982 }
solenbergdfc8f4f2015-10-01 02:31:10 -07001983 if (!MuteStream(ssrc, !enable)) {
solenberg1dd98f32015-09-10 01:57:14 -07001984 return false;
1985 }
solenbergdfc8f4f2015-10-01 02:31:10 -07001986 if (enable && options) {
solenberg1dd98f32015-09-10 01:57:14 -07001987 return SetOptions(*options);
1988 }
1989 return true;
1990}
1991
solenberg0a617e22015-10-20 15:49:38 -07001992int WebRtcVoiceMediaChannel::CreateVoEChannel() {
1993 int id = engine()->CreateVoEChannel();
1994 if (id == -1) {
1995 LOG_RTCERR0(CreateVoEChannel);
1996 return -1;
wu@webrtc.org9dba5252013-08-05 20:36:57 +00001997 }
solenberg0a617e22015-10-20 15:49:38 -07001998 if (engine()->voe()->network()->RegisterExternalTransport(id, *this) == -1) {
1999 LOG_RTCERR2(RegisterExternalTransport, id, this);
2000 engine()->voe()->base()->DeleteChannel(id);
2001 return -1;
2002 }
2003 return id;
wu@webrtc.org9dba5252013-08-05 20:36:57 +00002004}
2005
solenberg7add0582015-11-20 09:59:34 -08002006bool WebRtcVoiceMediaChannel::DeleteVoEChannel(int channel) {
wu@webrtc.org9dba5252013-08-05 20:36:57 +00002007 if (engine()->voe()->network()->DeRegisterExternalTransport(channel) == -1) {
2008 LOG_RTCERR1(DeRegisterExternalTransport, channel);
2009 }
wu@webrtc.org9dba5252013-08-05 20:36:57 +00002010 if (engine()->voe()->base()->DeleteChannel(channel) == -1) {
2011 LOG_RTCERR1(DeleteChannel, channel);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002012 return false;
2013 }
wu@webrtc.org9dba5252013-08-05 20:36:57 +00002014 return true;
2015}
henrike@webrtc.org1e09a712013-07-26 19:17:59 +00002016
wu@webrtc.org9dba5252013-08-05 20:36:57 +00002017bool WebRtcVoiceMediaChannel::AddSendStream(const StreamParams& sp) {
solenberg566ef242015-11-06 15:34:49 -08002018 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
solenberg0a617e22015-10-20 15:49:38 -07002019 LOG(LS_INFO) << "AddSendStream: " << sp.ToString();
2020
2021 uint32_t ssrc = sp.first_ssrc();
2022 RTC_DCHECK(0 != ssrc);
2023
2024 if (GetSendChannelId(ssrc) != -1) {
2025 LOG(LS_ERROR) << "Stream already exists with ssrc " << ssrc;
wu@webrtc.org9dba5252013-08-05 20:36:57 +00002026 return false;
2027 }
2028
solenberg0a617e22015-10-20 15:49:38 -07002029 // Create a new channel for sending audio data.
2030 int channel = CreateVoEChannel();
2031 if (channel == -1) {
2032 return false;
wu@webrtc.org9dba5252013-08-05 20:36:57 +00002033 }
wu@webrtc.org9dba5252013-08-05 20:36:57 +00002034
solenbergc96df772015-10-21 13:01:53 -07002035 // Save the channel to send_streams_, so that RemoveSendStream() can still
wu@webrtc.org9dba5252013-08-05 20:36:57 +00002036 // delete the channel in case failure happens below.
mallinath@webrtc.org67ee6b92014-02-03 16:57:16 +00002037 webrtc::AudioTransport* audio_transport =
2038 engine()->voe()->base()->audio_transport();
solenberg3a941542015-11-16 07:34:50 -08002039 send_streams_.insert(std::make_pair(ssrc, new WebRtcAudioSendStream(
2040 channel, audio_transport, ssrc, sp.cname, send_rtp_extensions_, call_)));
wu@webrtc.org9dba5252013-08-05 20:36:57 +00002041
solenberg0a617e22015-10-20 15:49:38 -07002042 // Set the current codecs to be used for the new channel. We need to do this
2043 // after adding the channel to send_channels_, because of how max bitrate is
2044 // currently being configured by SetSendCodec().
2045 if (!send_codecs_.empty() && !SetSendCodecs(channel, send_codecs_)) {
2046 RemoveSendStream(ssrc);
wu@webrtc.org9dba5252013-08-05 20:36:57 +00002047 return false;
2048 }
2049
2050 // At this point the channel's local SSRC has been updated. If the channel is
solenberg0a617e22015-10-20 15:49:38 -07002051 // the first send channel make sure that all the receive channels are updated
2052 // with the same SSRC in order to send receiver reports.
solenbergc96df772015-10-21 13:01:53 -07002053 if (send_streams_.size() == 1) {
solenberg0a617e22015-10-20 15:49:38 -07002054 receiver_reports_ssrc_ = ssrc;
solenberg7add0582015-11-20 09:59:34 -08002055 for (const auto& stream : recv_streams_) {
2056 int recv_channel = stream.second->channel();
solenberg0a617e22015-10-20 15:49:38 -07002057 if (engine()->voe()->rtp()->SetLocalSSRC(recv_channel, ssrc) != 0) {
solenberg7add0582015-11-20 09:59:34 -08002058 LOG_RTCERR2(SetLocalSSRC, recv_channel, ssrc);
solenberg1ac56142015-10-13 03:58:19 -07002059 return false;
wu@webrtc.org9dba5252013-08-05 20:36:57 +00002060 }
solenberg0a617e22015-10-20 15:49:38 -07002061 engine()->voe()->base()->AssociateSendChannel(recv_channel, channel);
2062 LOG(LS_INFO) << "VoiceEngine channel #" << recv_channel
2063 << " is associated with channel #" << channel << ".";
wu@webrtc.org9dba5252013-08-05 20:36:57 +00002064 }
2065 }
2066
wu@webrtc.org9dba5252013-08-05 20:36:57 +00002067 return ChangeSend(channel, desired_send_);
2068}
2069
Peter Boström0c4e06b2015-10-07 12:23:21 +02002070bool WebRtcVoiceMediaChannel::RemoveSendStream(uint32_t ssrc) {
solenberg566ef242015-11-06 15:34:49 -08002071 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
solenberg3a941542015-11-16 07:34:50 -08002072 LOG(LS_INFO) << "RemoveSendStream: " << ssrc;
2073
solenbergc96df772015-10-21 13:01:53 -07002074 auto it = send_streams_.find(ssrc);
2075 if (it == send_streams_.end()) {
wu@webrtc.org9dba5252013-08-05 20:36:57 +00002076 LOG(LS_WARNING) << "Try to remove stream with ssrc " << ssrc
2077 << " which doesn't exist.";
2078 return false;
2079 }
2080
mallinath@webrtc.org67ee6b92014-02-03 16:57:16 +00002081 int channel = it->second->channel();
wu@webrtc.org9dba5252013-08-05 20:36:57 +00002082 ChangeSend(channel, SEND_NOTHING);
2083
solenberg7add0582015-11-20 09:59:34 -08002084 // Clean up and delete the send stream+channel.
solenberg0a617e22015-10-20 15:49:38 -07002085 LOG(LS_INFO) << "Removing audio send stream " << ssrc
2086 << " with VoiceEngine channel #" << channel << ".";
solenberg7add0582015-11-20 09:59:34 -08002087 delete it->second;
2088 send_streams_.erase(it);
2089 if (!DeleteVoEChannel(channel)) {
solenberg0a617e22015-10-20 15:49:38 -07002090 return false;
wu@webrtc.org9dba5252013-08-05 20:36:57 +00002091 }
solenbergc96df772015-10-21 13:01:53 -07002092 if (send_streams_.empty()) {
wu@webrtc.org9dba5252013-08-05 20:36:57 +00002093 ChangeSend(SEND_NOTHING);
solenberg0a617e22015-10-20 15:49:38 -07002094 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002095 return true;
2096}
2097
2098bool WebRtcVoiceMediaChannel::AddRecvStream(const StreamParams& sp) {
solenberg566ef242015-11-06 15:34:49 -08002099 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
solenbergd97ec302015-10-07 01:40:33 -07002100 LOG(LS_INFO) << "AddRecvStream: " << sp.ToString();
2101
solenberg0b675462015-10-09 01:37:09 -07002102 if (!ValidateStreamParams(sp)) {
wu@webrtc.org78187522013-10-07 23:32:02 +00002103 return false;
2104 }
2105
solenberg7add0582015-11-20 09:59:34 -08002106 const uint32_t ssrc = sp.first_ssrc();
solenberg0b675462015-10-09 01:37:09 -07002107 if (ssrc == 0) {
2108 LOG(LS_WARNING) << "AddRecvStream with ssrc==0 is not supported.";
2109 return false;
2110 }
2111
solenberg1ac56142015-10-13 03:58:19 -07002112 // Remove the default receive stream if one had been created with this ssrc;
2113 // we'll recreate it then.
2114 if (IsDefaultRecvStream(ssrc)) {
2115 RemoveRecvStream(ssrc);
2116 }
solenberg0b675462015-10-09 01:37:09 -07002117
solenberg7add0582015-11-20 09:59:34 -08002118 if (GetReceiveChannelId(ssrc) != -1) {
henrike@webrtc.org1e09a712013-07-26 19:17:59 +00002119 LOG(LS_ERROR) << "Stream already exists with ssrc " << ssrc;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002120 return false;
2121 }
Fredrik Solenberg4b60c732015-05-07 14:07:48 +02002122
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002123 // Create a new channel for receiving audio data.
solenberg7add0582015-11-20 09:59:34 -08002124 const int channel = CreateVoEChannel();
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002125 if (channel == -1) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002126 return false;
2127 }
Minyue2013aec2015-05-13 14:14:42 +02002128
solenberg1ac56142015-10-13 03:58:19 -07002129 // Turn off all supported codecs.
solenberg26c8c912015-11-27 04:00:25 -08002130 // TODO(solenberg): Remove once "no codecs" is the default state of a stream.
2131 for (webrtc::CodecInst voe_codec : webrtc::acm2::RentACodec::Database()) {
2132 voe_codec.pltype = -1;
2133 if (engine()->voe()->codec()->SetRecPayloadType(channel, voe_codec) == -1) {
2134 LOG_RTCERR2(SetRecPayloadType, channel, ToString(voe_codec));
2135 DeleteVoEChannel(channel);
2136 return false;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002137 }
2138 }
2139
solenberg1ac56142015-10-13 03:58:19 -07002140 // Only enable those configured for this channel.
2141 for (const auto& codec : recv_codecs_) {
2142 webrtc::CodecInst voe_codec;
solenberg26c8c912015-11-27 04:00:25 -08002143 if (WebRtcVoiceEngine::ToCodecInst(codec, &voe_codec)) {
solenberg1ac56142015-10-13 03:58:19 -07002144 voe_codec.pltype = codec.id;
2145 if (engine()->voe()->codec()->SetRecPayloadType(
2146 channel, voe_codec) == -1) {
2147 LOG_RTCERR2(SetRecPayloadType, channel, ToString(voe_codec));
solenberg7add0582015-11-20 09:59:34 -08002148 DeleteVoEChannel(channel);
solenberg1ac56142015-10-13 03:58:19 -07002149 return false;
2150 }
henrike@webrtc.org1e09a712013-07-26 19:17:59 +00002151 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002152 }
solenberg8fb30c32015-10-13 03:06:58 -07002153
solenberg7add0582015-11-20 09:59:34 -08002154 const int send_channel = GetSendChannelId(receiver_reports_ssrc_);
2155 if (send_channel != -1) {
2156 // Associate receive channel with first send channel (so the receive channel
2157 // can obtain RTT from the send channel)
2158 engine()->voe()->base()->AssociateSendChannel(channel, send_channel);
2159 LOG(LS_INFO) << "VoiceEngine channel #" << channel
2160 << " is associated with channel #" << send_channel << ".";
buildbot@webrtc.org150835e2014-05-06 15:54:38 +00002161 }
2162
solenberg7add0582015-11-20 09:59:34 -08002163 recv_streams_.insert(std::make_pair(ssrc, new WebRtcAudioReceiveStream(
2164 channel, ssrc, receiver_reports_ssrc_,
2165 options_.combined_audio_video_bwe.value_or(false), sp.sync_label,
2166 recv_rtp_extensions_, call_)));
2167
2168 SetNack(channel, nack_enabled_);
solenberg1ac56142015-10-13 03:58:19 -07002169 SetPlayout(channel, playout_);
solenberg7add0582015-11-20 09:59:34 -08002170
solenberg1ac56142015-10-13 03:58:19 -07002171 return true;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002172}
2173
Peter Boström0c4e06b2015-10-07 12:23:21 +02002174bool WebRtcVoiceMediaChannel::RemoveRecvStream(uint32_t ssrc) {
solenberg566ef242015-11-06 15:34:49 -08002175 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
solenbergd97ec302015-10-07 01:40:33 -07002176 LOG(LS_INFO) << "RemoveRecvStream: " << ssrc;
2177
solenberg7add0582015-11-20 09:59:34 -08002178 const auto it = recv_streams_.find(ssrc);
2179 if (it == recv_streams_.end()) {
wu@webrtc.org9dba5252013-08-05 20:36:57 +00002180 LOG(LS_WARNING) << "Try to remove stream with ssrc " << ssrc
2181 << " which doesn't exist.";
henrike@webrtc.org1e09a712013-07-26 19:17:59 +00002182 return false;
wu@webrtc.org9dba5252013-08-05 20:36:57 +00002183 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002184
solenberg1ac56142015-10-13 03:58:19 -07002185 // Deregister default channel, if that's the one being destroyed.
2186 if (IsDefaultRecvStream(ssrc)) {
2187 default_recv_ssrc_ = -1;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002188 }
henrike@webrtc.org1e09a712013-07-26 19:17:59 +00002189
solenberg7add0582015-11-20 09:59:34 -08002190 const int channel = it->second->channel();
2191
2192 // Clean up and delete the receive stream+channel.
2193 LOG(LS_INFO) << "Removing audio receive stream " << ssrc
mallinath@webrtc.org67ee6b92014-02-03 16:57:16 +00002194 << " with VoiceEngine channel #" << channel << ".";
solenberg7add0582015-11-20 09:59:34 -08002195 delete it->second;
2196 recv_streams_.erase(it);
2197 return DeleteVoEChannel(channel);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002198}
2199
Peter Boström0c4e06b2015-10-07 12:23:21 +02002200bool WebRtcVoiceMediaChannel::SetLocalRenderer(uint32_t ssrc,
henrike@webrtc.org1e09a712013-07-26 19:17:59 +00002201 AudioRenderer* renderer) {
solenbergc96df772015-10-21 13:01:53 -07002202 auto it = send_streams_.find(ssrc);
2203 if (it == send_streams_.end()) {
wu@webrtc.org9dba5252013-08-05 20:36:57 +00002204 if (renderer) {
2205 // Return an error if trying to set a valid renderer with an invalid ssrc.
2206 LOG(LS_ERROR) << "SetLocalRenderer failed with ssrc "<< ssrc;
2207 return false;
2208 }
2209
2210 // The channel likely has gone away, do nothing.
henrike@webrtc.org1e09a712013-07-26 19:17:59 +00002211 return true;
henrike@webrtc.org1e09a712013-07-26 19:17:59 +00002212 }
2213
solenberg1ac56142015-10-13 03:58:19 -07002214 if (renderer) {
mallinath@webrtc.org67ee6b92014-02-03 16:57:16 +00002215 it->second->Start(renderer);
solenberg1ac56142015-10-13 03:58:19 -07002216 } else {
mallinath@webrtc.org67ee6b92014-02-03 16:57:16 +00002217 it->second->Stop();
solenberg1ac56142015-10-13 03:58:19 -07002218 }
henrike@webrtc.org1e09a712013-07-26 19:17:59 +00002219
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002220 return true;
2221}
2222
2223bool WebRtcVoiceMediaChannel::GetActiveStreams(
2224 AudioInfo::StreamList* actives) {
solenberg566ef242015-11-06 15:34:49 -08002225 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002226 actives->clear();
solenberg7add0582015-11-20 09:59:34 -08002227 for (const auto& ch : recv_streams_) {
Fredrik Solenbergaf9fb212015-08-26 10:45:53 +02002228 int level = GetOutputLevel(ch.second->channel());
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002229 if (level > 0) {
Fredrik Solenbergaf9fb212015-08-26 10:45:53 +02002230 actives->push_back(std::make_pair(ch.first, level));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002231 }
2232 }
2233 return true;
2234}
2235
2236int WebRtcVoiceMediaChannel::GetOutputLevel() {
solenberg566ef242015-11-06 15:34:49 -08002237 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
solenberg1ac56142015-10-13 03:58:19 -07002238 int highest = 0;
solenberg7add0582015-11-20 09:59:34 -08002239 for (const auto& ch : recv_streams_) {
solenberg8fb30c32015-10-13 03:06:58 -07002240 highest = std::max(GetOutputLevel(ch.second->channel()), highest);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002241 }
2242 return highest;
2243}
2244
2245int WebRtcVoiceMediaChannel::GetTimeSinceLastTyping() {
2246 int ret;
2247 if (engine()->voe()->processing()->TimeSinceLastTyping(ret) == -1) {
2248 // In case of error, log the info and continue
2249 LOG_RTCERR0(TimeSinceLastTyping);
2250 ret = -1;
2251 } else {
2252 ret *= 1000; // We return ms, webrtc returns seconds.
2253 }
2254 return ret;
2255}
2256
2257void WebRtcVoiceMediaChannel::SetTypingDetectionParameters(int time_window,
2258 int cost_per_typing, int reporting_threshold, int penalty_decay,
2259 int type_event_delay) {
2260 if (engine()->voe()->processing()->SetTypingDetectionParameters(
2261 time_window, cost_per_typing,
2262 reporting_threshold, penalty_decay, type_event_delay) == -1) {
2263 // In case of error, log the info and continue
2264 LOG_RTCERR5(SetTypingDetectionParameters, time_window,
2265 cost_per_typing, reporting_threshold, penalty_decay,
2266 type_event_delay);
2267 }
2268}
2269
solenberg4bac9c52015-10-09 02:32:53 -07002270bool WebRtcVoiceMediaChannel::SetOutputVolume(uint32_t ssrc, double volume) {
solenberg566ef242015-11-06 15:34:49 -08002271 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
solenberg1ac56142015-10-13 03:58:19 -07002272 if (ssrc == 0) {
2273 default_recv_volume_ = volume;
2274 if (default_recv_ssrc_ == -1) {
2275 return true;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002276 }
solenberg1ac56142015-10-13 03:58:19 -07002277 ssrc = static_cast<uint32_t>(default_recv_ssrc_);
2278 }
2279 int ch_id = GetReceiveChannelId(ssrc);
2280 if (ch_id < 0) {
2281 LOG(LS_WARNING) << "Cannot find channel for ssrc:" << ssrc;
2282 return false;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002283 }
2284
solenberg1ac56142015-10-13 03:58:19 -07002285 if (-1 == engine()->voe()->volume()->SetChannelOutputVolumeScaling(ch_id,
2286 volume)) {
2287 LOG_RTCERR2(SetChannelOutputVolumeScaling, ch_id, volume);
2288 return false;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002289 }
solenberg1ac56142015-10-13 03:58:19 -07002290 LOG(LS_INFO) << "SetOutputVolume to " << volume
2291 << " for channel " << ch_id << " and ssrc " << ssrc;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002292 return true;
2293}
2294
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002295bool WebRtcVoiceMediaChannel::CanInsertDtmf() {
2296 return dtmf_allowed_;
2297}
2298
Peter Boström0c4e06b2015-10-07 12:23:21 +02002299bool WebRtcVoiceMediaChannel::InsertDtmf(uint32_t ssrc,
2300 int event,
2301 int duration,
2302 int flags) {
solenberg566ef242015-11-06 15:34:49 -08002303 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002304 if (!dtmf_allowed_) {
2305 return false;
2306 }
2307
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002308 // Send the event.
2309 if (flags & cricket::DF_SEND) {
wu@webrtc.orgcadf9042013-08-30 21:24:16 +00002310 int channel = -1;
2311 if (ssrc == 0) {
solenbergc96df772015-10-21 13:01:53 -07002312 if (send_streams_.size() > 0) {
2313 channel = send_streams_.begin()->second->channel();
wu@webrtc.orgcadf9042013-08-30 21:24:16 +00002314 }
2315 } else {
solenbergd97ec302015-10-07 01:40:33 -07002316 channel = GetSendChannelId(ssrc);
wu@webrtc.orgcadf9042013-08-30 21:24:16 +00002317 }
wu@webrtc.org9dba5252013-08-05 20:36:57 +00002318 if (channel == -1) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002319 LOG(LS_WARNING) << "InsertDtmf - The specified ssrc "
2320 << ssrc << " is not in use.";
2321 return false;
2322 }
2323 // Send DTMF using out-of-band DTMF. ("true", as 3rd arg)
wu@webrtc.org9dba5252013-08-05 20:36:57 +00002324 if (engine()->voe()->dtmf()->SendTelephoneEvent(
2325 channel, event, true, duration) == -1) {
2326 LOG_RTCERR4(SendTelephoneEvent, channel, event, true, duration);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002327 return false;
2328 }
2329 }
2330
2331 // Play the event.
2332 if (flags & cricket::DF_PLAY) {
2333 // Play DTMF tone locally.
2334 if (engine()->voe()->dtmf()->PlayDtmfTone(event, duration) == -1) {
2335 LOG_RTCERR2(PlayDtmfTone, event, duration);
2336 return false;
2337 }
2338 }
2339
2340 return true;
2341}
2342
wu@webrtc.orga9890802013-12-13 00:21:03 +00002343void WebRtcVoiceMediaChannel::OnPacketReceived(
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00002344 rtc::Buffer* packet, const rtc::PacketTime& packet_time) {
solenberg566ef242015-11-06 15:34:49 -08002345 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
Fredrik Solenberg4b60c732015-05-07 14:07:48 +02002346
solenberg1ac56142015-10-13 03:58:19 -07002347 uint32_t ssrc = 0;
2348 if (!GetRtpSsrc(packet->data(), packet->size(), &ssrc)) {
2349 return;
2350 }
2351
solenberg7e63ef02015-11-20 00:19:43 -08002352 // If we don't have a default channel, and the SSRC is unknown, create a
2353 // default channel.
2354 if (default_recv_ssrc_ == -1 && GetReceiveChannelId(ssrc) == -1) {
solenberg1ac56142015-10-13 03:58:19 -07002355 StreamParams sp;
2356 sp.ssrcs.push_back(ssrc);
2357 LOG(LS_INFO) << "Creating default receive stream for SSRC=" << ssrc << ".";
2358 if (!AddRecvStream(sp)) {
2359 LOG(LS_WARNING) << "Could not create default receive stream.";
2360 return;
2361 }
2362 default_recv_ssrc_ = ssrc;
2363 SetOutputVolume(default_recv_ssrc_, default_recv_volume_);
2364 }
2365
2366 // Forward packet to Call. If the SSRC is unknown we'll return after this.
Fredrik Solenberg709ed672015-09-15 12:26:33 +02002367 const webrtc::PacketTime webrtc_packet_time(packet_time.timestamp,
2368 packet_time.not_before);
solenberg1ac56142015-10-13 03:58:19 -07002369 webrtc::PacketReceiver::DeliveryStatus delivery_result =
2370 call_->Receiver()->DeliverPacket(webrtc::MediaType::AUDIO,
2371 reinterpret_cast<const uint8_t*>(packet->data()), packet->size(),
2372 webrtc_packet_time);
2373 if (webrtc::PacketReceiver::DELIVERY_OK != delivery_result) {
solenberg7e63ef02015-11-20 00:19:43 -08002374 // If the SSRC is unknown here, route it to the default channel, if we have
2375 // one. See: https://bugs.chromium.org/p/webrtc/issues/detail?id=5208
2376 if (default_recv_ssrc_ == -1) {
2377 return;
2378 } else {
2379 ssrc = default_recv_ssrc_;
2380 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002381 }
2382
solenberg1ac56142015-10-13 03:58:19 -07002383 // Find the channel to send this packet to. It must exist since webrtc::Call
2384 // was able to demux the packet.
2385 int channel = GetReceiveChannelId(ssrc);
2386 RTC_DCHECK(channel != -1);
2387
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002388 // Pass it off to the decoder.
henrike@webrtc.org28654cb2013-07-22 21:07:49 +00002389 engine()->voe()->network()->ReceivedRTPPacket(
solenberg1ac56142015-10-13 03:58:19 -07002390 channel, packet->data(), packet->size(), webrtc_packet_time);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002391}
2392
wu@webrtc.orga9890802013-12-13 00:21:03 +00002393void WebRtcVoiceMediaChannel::OnRtcpReceived(
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00002394 rtc::Buffer* packet, const rtc::PacketTime& packet_time) {
solenberg566ef242015-11-06 15:34:49 -08002395 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
Fredrik Solenberg4b60c732015-05-07 14:07:48 +02002396
Fredrik Solenberg709ed672015-09-15 12:26:33 +02002397 // Forward packet to Call as well.
2398 const webrtc::PacketTime webrtc_packet_time(packet_time.timestamp,
2399 packet_time.not_before);
2400 call_->Receiver()->DeliverPacket(webrtc::MediaType::AUDIO,
2401 reinterpret_cast<const uint8_t*>(packet->data()), packet->size(),
2402 webrtc_packet_time);
Fredrik Solenberg4b60c732015-05-07 14:07:48 +02002403
wu@webrtc.org9dba5252013-08-05 20:36:57 +00002404 // Sending channels need all RTCP packets with feedback information.
2405 // Even sender reports can contain attached report blocks.
2406 // Receiving channels need sender reports in order to create
2407 // correct receiver reports.
2408 int type = 0;
kwiberg@webrtc.orgeebcab52015-03-24 09:19:06 +00002409 if (!GetRtcpType(packet->data(), packet->size(), &type)) {
wu@webrtc.org9dba5252013-08-05 20:36:57 +00002410 LOG(LS_WARNING) << "Failed to parse type from received RTCP packet";
2411 return;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002412 }
2413
solenberg0b675462015-10-09 01:37:09 -07002414 // If it is a sender report, find the receive channel that is listening.
wu@webrtc.org9dba5252013-08-05 20:36:57 +00002415 if (type == kRtcpTypeSR) {
solenberg0b675462015-10-09 01:37:09 -07002416 uint32_t ssrc = 0;
2417 if (!GetRtcpSsrc(packet->data(), packet->size(), &ssrc)) {
2418 return;
2419 }
2420 int recv_channel_id = GetReceiveChannelId(ssrc);
2421 if (recv_channel_id != -1) {
wu@webrtc.org9dba5252013-08-05 20:36:57 +00002422 engine()->voe()->network()->ReceivedRTCPPacket(
solenberg0b675462015-10-09 01:37:09 -07002423 recv_channel_id, packet->data(), packet->size());
wu@webrtc.org9dba5252013-08-05 20:36:57 +00002424 }
2425 }
2426
2427 // SR may continue RR and any RR entry may correspond to any one of the send
2428 // channels. So all RTCP packets must be forwarded all send channels. VoE
2429 // will filter out RR internally.
solenbergc96df772015-10-21 13:01:53 -07002430 for (const auto& ch : send_streams_) {
wu@webrtc.org9dba5252013-08-05 20:36:57 +00002431 engine()->voe()->network()->ReceivedRTCPPacket(
Fredrik Solenbergaf9fb212015-08-26 10:45:53 +02002432 ch.second->channel(), packet->data(), packet->size());
wu@webrtc.org9dba5252013-08-05 20:36:57 +00002433 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002434}
2435
Peter Boström0c4e06b2015-10-07 12:23:21 +02002436bool WebRtcVoiceMediaChannel::MuteStream(uint32_t ssrc, bool muted) {
solenberg566ef242015-11-06 15:34:49 -08002437 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
solenberg0a617e22015-10-20 15:49:38 -07002438 int channel = GetSendChannelId(ssrc);
wu@webrtc.org9dba5252013-08-05 20:36:57 +00002439 if (channel == -1) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002440 LOG(LS_WARNING) << "The specified ssrc " << ssrc << " is not in use.";
2441 return false;
2442 }
wu@webrtc.org9dba5252013-08-05 20:36:57 +00002443 if (engine()->voe()->volume()->SetInputMute(channel, muted) == -1) {
2444 LOG_RTCERR2(SetInputMute, channel, muted);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002445 return false;
2446 }
buildbot@webrtc.org6b21b712014-07-31 15:08:53 +00002447 // We set the AGC to mute state only when all the channels are muted.
2448 // This implementation is not ideal, instead we should signal the AGC when
2449 // the mic channel is muted/unmuted. We can't do it today because there
2450 // is no good way to know which stream is mapping to the mic channel.
2451 bool all_muted = muted;
solenbergc96df772015-10-21 13:01:53 -07002452 for (const auto& ch : send_streams_) {
Fredrik Solenbergaf9fb212015-08-26 10:45:53 +02002453 if (!all_muted) {
2454 break;
2455 }
2456 if (engine()->voe()->volume()->GetInputMute(ch.second->channel(),
buildbot@webrtc.org6b21b712014-07-31 15:08:53 +00002457 all_muted)) {
Fredrik Solenbergaf9fb212015-08-26 10:45:53 +02002458 LOG_RTCERR1(GetInputMute, ch.second->channel());
buildbot@webrtc.org6b21b712014-07-31 15:08:53 +00002459 return false;
2460 }
2461 }
2462
2463 webrtc::AudioProcessing* ap = engine()->voe()->base()->audio_processing();
solenberg0a617e22015-10-20 15:49:38 -07002464 if (ap) {
buildbot@webrtc.org6b21b712014-07-31 15:08:53 +00002465 ap->set_output_will_be_muted(all_muted);
solenberg0a617e22015-10-20 15:49:38 -07002466 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002467 return true;
2468}
2469
minyue@webrtc.org26236952014-10-29 02:27:08 +00002470// TODO(minyue): SetMaxSendBandwidth() is subject to be renamed to
2471// SetMaxSendBitrate() in future.
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00002472bool WebRtcVoiceMediaChannel::SetMaxSendBandwidth(int bps) {
minyue@webrtc.org26236952014-10-29 02:27:08 +00002473 LOG(LS_INFO) << "WebRtcVoiceMediaChannel::SetMaxSendBandwidth.";
minyue@webrtc.org26236952014-10-29 02:27:08 +00002474 return SetSendBitrateInternal(bps);
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00002475}
2476
minyue@webrtc.org26236952014-10-29 02:27:08 +00002477bool WebRtcVoiceMediaChannel::SetSendBitrateInternal(int bps) {
2478 LOG(LS_INFO) << "WebRtcVoiceMediaChannel::SetSendBitrateInternal.";
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00002479
minyue@webrtc.org26236952014-10-29 02:27:08 +00002480 send_bitrate_setting_ = true;
2481 send_bitrate_bps_ = bps;
wu@webrtc.org1d1ffc92013-10-16 18:12:02 +00002482
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002483 if (!send_codec_) {
wu@webrtc.org1d1ffc92013-10-16 18:12:02 +00002484 LOG(LS_INFO) << "The send codec has not been set up yet. "
minyue@webrtc.org26236952014-10-29 02:27:08 +00002485 << "The send bitrate setting will be applied later.";
wu@webrtc.org1d1ffc92013-10-16 18:12:02 +00002486 return true;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002487 }
2488
minyue@webrtc.org26236952014-10-29 02:27:08 +00002489 // Bitrate is auto by default.
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00002490 // TODO(bemasc): Fix this so that if SetMaxSendBandwidth(50) is followed by
2491 // SetMaxSendBandwith(0), the second call removes the previous limit.
2492 if (bps <= 0)
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002493 return true;
2494
2495 webrtc::CodecInst codec = *send_codec_;
solenberg26c8c912015-11-27 04:00:25 -08002496 bool is_multi_rate = WebRtcVoiceCodecs::IsCodecMultiRate(codec);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002497
2498 if (is_multi_rate) {
2499 // If codec is multi-rate then just set the bitrate.
2500 codec.rate = bps;
solenbergc96df772015-10-21 13:01:53 -07002501 for (const auto& ch : send_streams_) {
solenberg0a617e22015-10-20 15:49:38 -07002502 if (!SetSendCodec(ch.second->channel(), codec)) {
2503 LOG(LS_INFO) << "Failed to set codec " << codec.plname
2504 << " to bitrate " << bps << " bps.";
2505 return false;
2506 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002507 }
2508 return true;
2509 } else {
2510 // If codec is not multi-rate and |bps| is less than the fixed bitrate
2511 // then fail. If codec is not multi-rate and |bps| exceeds or equal the
2512 // fixed bitrate then ignore.
2513 if (bps < codec.rate) {
2514 LOG(LS_INFO) << "Failed to set codec " << codec.plname
2515 << " to bitrate " << bps << " bps"
2516 << ", requires at least " << codec.rate << " bps.";
2517 return false;
2518 }
2519 return true;
2520 }
2521}
2522
2523bool WebRtcVoiceMediaChannel::GetStats(VoiceMediaInfo* info) {
solenberg566ef242015-11-06 15:34:49 -08002524 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
solenberg85a04962015-10-27 03:35:21 -07002525 RTC_DCHECK(info);
solenbergd97ec302015-10-07 01:40:33 -07002526
solenberg85a04962015-10-27 03:35:21 -07002527 // Get SSRC and stats for each sender.
2528 RTC_DCHECK(info->senders.size() == 0);
2529 for (const auto& stream : send_streams_) {
2530 webrtc::AudioSendStream::Stats stats = stream.second->GetStats();
wu@webrtc.org9dba5252013-08-05 20:36:57 +00002531 VoiceSenderInfo sinfo;
solenberg85a04962015-10-27 03:35:21 -07002532 sinfo.add_ssrc(stats.local_ssrc);
2533 sinfo.bytes_sent = stats.bytes_sent;
2534 sinfo.packets_sent = stats.packets_sent;
2535 sinfo.packets_lost = stats.packets_lost;
2536 sinfo.fraction_lost = stats.fraction_lost;
2537 sinfo.codec_name = stats.codec_name;
2538 sinfo.ext_seqnum = stats.ext_seqnum;
2539 sinfo.jitter_ms = stats.jitter_ms;
2540 sinfo.rtt_ms = stats.rtt_ms;
2541 sinfo.audio_level = stats.audio_level;
2542 sinfo.aec_quality_min = stats.aec_quality_min;
2543 sinfo.echo_delay_median_ms = stats.echo_delay_median_ms;
2544 sinfo.echo_delay_std_ms = stats.echo_delay_std_ms;
2545 sinfo.echo_return_loss = stats.echo_return_loss;
2546 sinfo.echo_return_loss_enhancement = stats.echo_return_loss_enhancement;
solenberg566ef242015-11-06 15:34:49 -08002547 sinfo.typing_noise_detected =
2548 (send_ == SEND_NOTHING ? false : stats.typing_noise_detected);
wu@webrtc.org9dba5252013-08-05 20:36:57 +00002549 info->senders.push_back(sinfo);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002550 }
2551
solenberg85a04962015-10-27 03:35:21 -07002552 // Get SSRC and stats for each receiver.
2553 RTC_DCHECK(info->receivers.size() == 0);
solenberg7add0582015-11-20 09:59:34 -08002554 for (const auto& stream : recv_streams_) {
Fredrik Solenberg4f4ec0a2015-10-22 10:49:27 +02002555 webrtc::AudioReceiveStream::Stats stats = stream.second->GetStats();
2556 VoiceReceiverInfo rinfo;
2557 rinfo.add_ssrc(stats.remote_ssrc);
2558 rinfo.bytes_rcvd = stats.bytes_rcvd;
2559 rinfo.packets_rcvd = stats.packets_rcvd;
2560 rinfo.packets_lost = stats.packets_lost;
2561 rinfo.fraction_lost = stats.fraction_lost;
2562 rinfo.codec_name = stats.codec_name;
2563 rinfo.ext_seqnum = stats.ext_seqnum;
2564 rinfo.jitter_ms = stats.jitter_ms;
2565 rinfo.jitter_buffer_ms = stats.jitter_buffer_ms;
2566 rinfo.jitter_buffer_preferred_ms = stats.jitter_buffer_preferred_ms;
2567 rinfo.delay_estimate_ms = stats.delay_estimate_ms;
2568 rinfo.audio_level = stats.audio_level;
2569 rinfo.expand_rate = stats.expand_rate;
2570 rinfo.speech_expand_rate = stats.speech_expand_rate;
2571 rinfo.secondary_decoded_rate = stats.secondary_decoded_rate;
2572 rinfo.accelerate_rate = stats.accelerate_rate;
2573 rinfo.preemptive_expand_rate = stats.preemptive_expand_rate;
2574 rinfo.decoding_calls_to_silence_generator =
2575 stats.decoding_calls_to_silence_generator;
2576 rinfo.decoding_calls_to_neteq = stats.decoding_calls_to_neteq;
2577 rinfo.decoding_normal = stats.decoding_normal;
2578 rinfo.decoding_plc = stats.decoding_plc;
2579 rinfo.decoding_cng = stats.decoding_cng;
2580 rinfo.decoding_plc_cng = stats.decoding_plc_cng;
2581 rinfo.capture_start_ntp_time_ms = stats.capture_start_ntp_time_ms;
2582 info->receivers.push_back(rinfo);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002583 }
2584
2585 return true;
2586}
2587
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002588int WebRtcVoiceMediaChannel::GetOutputLevel(int channel) {
solenbergd97ec302015-10-07 01:40:33 -07002589 unsigned int ulevel = 0;
2590 int ret = engine()->voe()->volume()->GetSpeechOutputLevel(channel, ulevel);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002591 return (ret == 0) ? static_cast<int>(ulevel) : -1;
2592}
2593
Peter Boström0c4e06b2015-10-07 12:23:21 +02002594int WebRtcVoiceMediaChannel::GetReceiveChannelId(uint32_t ssrc) const {
solenberg566ef242015-11-06 15:34:49 -08002595 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
solenberg7add0582015-11-20 09:59:34 -08002596 const auto it = recv_streams_.find(ssrc);
2597 if (it != recv_streams_.end()) {
mallinath@webrtc.org67ee6b92014-02-03 16:57:16 +00002598 return it->second->channel();
solenberg8fb30c32015-10-13 03:06:58 -07002599 }
solenberg1ac56142015-10-13 03:58:19 -07002600 return -1;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002601}
2602
Peter Boström0c4e06b2015-10-07 12:23:21 +02002603int WebRtcVoiceMediaChannel::GetSendChannelId(uint32_t ssrc) const {
solenberg566ef242015-11-06 15:34:49 -08002604 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
solenbergc96df772015-10-21 13:01:53 -07002605 const auto it = send_streams_.find(ssrc);
2606 if (it != send_streams_.end()) {
mallinath@webrtc.org67ee6b92014-02-03 16:57:16 +00002607 return it->second->channel();
solenberg8fb30c32015-10-13 03:06:58 -07002608 }
wu@webrtc.org9dba5252013-08-05 20:36:57 +00002609 return -1;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002610}
2611
2612bool WebRtcVoiceMediaChannel::GetRedSendCodec(const AudioCodec& red_codec,
2613 const std::vector<AudioCodec>& all_codecs, webrtc::CodecInst* send_codec) {
2614 // Get the RED encodings from the parameter with no name. This may
2615 // change based on what is discussed on the Jingle list.
2616 // The encoding parameter is of the form "a/b"; we only support where
2617 // a == b. Verify this and parse out the value into red_pt.
2618 // If the parameter value is absent (as it will be until we wire up the
2619 // signaling of this message), use the second codec specified (i.e. the
2620 // one after "red") as the encoding parameter.
2621 int red_pt = -1;
2622 std::string red_params;
2623 CodecParameterMap::const_iterator it = red_codec.params.find("");
2624 if (it != red_codec.params.end()) {
2625 red_params = it->second;
2626 std::vector<std::string> red_pts;
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00002627 if (rtc::split(red_params, '/', &red_pts) != 2 ||
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002628 red_pts[0] != red_pts[1] ||
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00002629 !rtc::FromString(red_pts[0], &red_pt)) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002630 LOG(LS_WARNING) << "RED params " << red_params << " not supported.";
2631 return false;
2632 }
2633 } else if (red_codec.params.empty()) {
2634 LOG(LS_WARNING) << "RED params not present, using defaults";
2635 if (all_codecs.size() > 1) {
2636 red_pt = all_codecs[1].id;
2637 }
2638 }
2639
2640 // Try to find red_pt in |codecs|.
Fredrik Solenbergaf9fb212015-08-26 10:45:53 +02002641 for (const AudioCodec& codec : all_codecs) {
2642 if (codec.id == red_pt) {
2643 // If we find the right codec, that will be the codec we pass to
2644 // SetSendCodec, with the desired payload type.
solenberg26c8c912015-11-27 04:00:25 -08002645 if (WebRtcVoiceEngine::ToCodecInst(codec, send_codec)) {
Fredrik Solenbergaf9fb212015-08-26 10:45:53 +02002646 return true;
2647 } else {
2648 break;
2649 }
2650 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002651 }
Fredrik Solenbergaf9fb212015-08-26 10:45:53 +02002652 LOG(LS_WARNING) << "RED params " << red_params << " are invalid.";
2653 return false;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002654}
2655
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002656bool WebRtcVoiceMediaChannel::SetPlayout(int channel, bool playout) {
2657 if (playout) {
2658 LOG(LS_INFO) << "Starting playout for channel #" << channel;
2659 if (engine()->voe()->base()->StartPlayout(channel) == -1) {
2660 LOG_RTCERR1(StartPlayout, channel);
2661 return false;
2662 }
2663 } else {
2664 LOG(LS_INFO) << "Stopping playout for channel #" << channel;
2665 engine()->voe()->base()->StopPlayout(channel);
2666 }
2667 return true;
2668}
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002669} // namespace cricket
2670
2671#endif // HAVE_WEBRTC_VOICE