blob: 76c0c0106b002aa4a73b0875acdd44d7f7acd8e0 [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"
solenberg7e4e01a2015-12-02 08:05:01 -080045#include "talk/media/webrtc/webrtcmediaengine.h"
buildbot@webrtc.orga09a9992014-08-13 17:26:08 +000046#include "talk/media/webrtc/webrtcvoe.h"
tfarina5237aaf2015-11-10 23:44:30 -080047#include "webrtc/base/arraysize.h"
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +000048#include "webrtc/base/base64.h"
49#include "webrtc/base/byteorder.h"
50#include "webrtc/base/common.h"
51#include "webrtc/base/helpers.h"
52#include "webrtc/base/logging.h"
53#include "webrtc/base/stringencode.h"
54#include "webrtc/base/stringutils.h"
ivoc112a3d82015-10-16 02:22:18 -070055#include "webrtc/call/rtc_event_log.h"
mallinath@webrtc.orga27be8e2013-09-27 23:04:10 +000056#include "webrtc/common.h"
solenberg26c8c912015-11-27 04:00:25 -080057#include "webrtc/modules/audio_coding/acm2/rent_a_codec.h"
henrike@webrtc.org28e20752013-07-10 00:45:36 +000058#include "webrtc/modules/audio_processing/include/audio_processing.h"
Henrik Kjellander98f53512015-10-28 18:17:40 +010059#include "webrtc/system_wrappers/include/field_trial.h"
solenbergbd138382015-11-20 16:08:07 -080060#include "webrtc/system_wrappers/include/trace.h"
henrike@webrtc.org28e20752013-07-10 00:45:36 +000061
henrike@webrtc.org28e20752013-07-10 00:45:36 +000062namespace cricket {
solenbergd97ec302015-10-07 01:40:33 -070063namespace {
henrike@webrtc.org28e20752013-07-10 00:45:36 +000064
solenbergbd138382015-11-20 16:08:07 -080065const int kDefaultTraceFilter = webrtc::kTraceNone | webrtc::kTraceTerseInfo |
66 webrtc::kTraceWarning | webrtc::kTraceError |
67 webrtc::kTraceCritical;
68const int kElevatedTraceFilter = kDefaultTraceFilter | webrtc::kTraceStateInfo |
69 webrtc::kTraceInfo;
70
henrike@webrtc.org28e20752013-07-10 00:45:36 +000071// For Linux/Mac, using the default device is done by specifying index 0 for
72// VoE 4.0 and not -1 (which was the case for VoE 3.5).
73//
74// On Windows Vista and newer, Microsoft introduced the concept of "Default
75// Communications Device". This means that there are two types of default
76// devices (old Wave Audio style default and Default Communications Device).
77//
78// On Windows systems which only support Wave Audio style default, uses either
79// -1 or 0 to select the default device.
80//
81// On Windows systems which support both "Default Communication Device" and
82// old Wave Audio style default, use -1 for Default Communications Device and
83// -2 for Wave Audio style default, which is what we want to use for clips.
84// It's not clear yet whether the -2 index is handled properly on other OSes.
85
86#ifdef WIN32
solenbergd97ec302015-10-07 01:40:33 -070087const int kDefaultAudioDeviceId = -1;
henrike@webrtc.org28e20752013-07-10 00:45:36 +000088#else
solenbergd97ec302015-10-07 01:40:33 -070089const int kDefaultAudioDeviceId = 0;
henrike@webrtc.org28e20752013-07-10 00:45:36 +000090#endif
91
henrike@webrtc.org28e20752013-07-10 00:45:36 +000092// Parameter used for NACK.
93// This value is equivalent to 5 seconds of audio data at 20 ms per packet.
solenbergd97ec302015-10-07 01:40:33 -070094const int kNackMaxPackets = 250;
minyue@webrtc.org2dc6f312014-10-31 05:33:10 +000095
96// Codec parameters for Opus.
henrike@webrtc.org1e09a712013-07-26 19:17:59 +000097// draft-spittka-payload-rtp-opus-03
minyue@webrtc.org2dc6f312014-10-31 05:33:10 +000098
99// Recommended bitrates:
100// 8-12 kb/s for NB speech,
101// 16-20 kb/s for WB speech,
102// 28-40 kb/s for FB speech,
103// 48-64 kb/s for FB mono music, and
104// 64-128 kb/s for FB stereo music.
105// The current implementation applies the following values to mono signals,
106// and multiplies them by 2 for stereo.
solenbergd97ec302015-10-07 01:40:33 -0700107const int kOpusBitrateNb = 12000;
108const int kOpusBitrateWb = 20000;
109const int kOpusBitrateFb = 32000;
minyue@webrtc.org2dc6f312014-10-31 05:33:10 +0000110
henrike@webrtc.org1e09a712013-07-26 19:17:59 +0000111// Opus bitrate should be in the range between 6000 and 510000.
solenbergd97ec302015-10-07 01:40:33 -0700112const int kOpusMinBitrate = 6000;
113const int kOpusMaxBitrate = 510000;
buildbot@webrtc.org5d639b32014-09-10 07:57:12 +0000114
wu@webrtc.orgde305012013-10-31 15:40:38 +0000115// Default audio dscp value.
116// See http://tools.ietf.org/html/rfc2474 for details.
117// See also http://tools.ietf.org/html/draft-jennings-rtcweb-qos-00
solenbergd97ec302015-10-07 01:40:33 -0700118const rtc::DiffServCodePoint kAudioDscpValue = rtc::DSCP_EF;
henrike@webrtc.org1e09a712013-07-26 19:17:59 +0000119
sergeyu@chromium.orga59696b2013-09-13 23:48:58 +0000120// Ensure we open the file in a writeable path on ChromeOS and Android. This
121// workaround can be removed when it's possible to specify a filename for audio
122// option based AEC dumps.
henrike@webrtc.org1e09a712013-07-26 19:17:59 +0000123//
124// TODO(grunell): Use a string in the options instead of hardcoding it here
125// and let the embedder choose the filename (crbug.com/264223).
126//
sergeyu@chromium.orga59696b2013-09-13 23:48:58 +0000127// NOTE(ajm): Don't use hardcoded paths on platforms not explicitly specified
128// below.
129#if defined(CHROMEOS)
solenbergd97ec302015-10-07 01:40:33 -0700130const char kAecDumpByAudioOptionFilename[] = "/tmp/audio.aecdump";
sergeyu@chromium.orga59696b2013-09-13 23:48:58 +0000131#elif defined(ANDROID)
solenbergd97ec302015-10-07 01:40:33 -0700132const char kAecDumpByAudioOptionFilename[] = "/sdcard/audio.aecdump";
henrike@webrtc.org1e09a712013-07-26 19:17:59 +0000133#else
solenbergd97ec302015-10-07 01:40:33 -0700134const char kAecDumpByAudioOptionFilename[] = "audio.aecdump";
henrike@webrtc.org1e09a712013-07-26 19:17:59 +0000135#endif
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000136
solenberg0b675462015-10-09 01:37:09 -0700137bool ValidateStreamParams(const StreamParams& sp) {
138 if (sp.ssrcs.empty()) {
139 LOG(LS_ERROR) << "No SSRCs in stream parameters: " << sp.ToString();
140 return false;
141 }
142 if (sp.ssrcs.size() > 1) {
143 LOG(LS_ERROR) << "Multiple SSRCs in stream parameters: " << sp.ToString();
144 return false;
145 }
146 return true;
147}
148
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000149// Dumps an AudioCodec in RFC 2327-ish format.
solenbergd97ec302015-10-07 01:40:33 -0700150std::string ToString(const AudioCodec& codec) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000151 std::stringstream ss;
152 ss << codec.name << "/" << codec.clockrate << "/" << codec.channels
153 << " (" << codec.id << ")";
154 return ss.str();
155}
Minyue Li7100dcd2015-03-27 05:05:59 +0100156
solenbergd97ec302015-10-07 01:40:33 -0700157std::string ToString(const webrtc::CodecInst& codec) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000158 std::stringstream ss;
159 ss << codec.plname << "/" << codec.plfreq << "/" << codec.channels
160 << " (" << codec.pltype << ")";
161 return ss.str();
162}
163
solenbergd97ec302015-10-07 01:40:33 -0700164bool IsCodec(const AudioCodec& codec, const char* ref_name) {
Minyue Li7100dcd2015-03-27 05:05:59 +0100165 return (_stricmp(codec.name.c_str(), ref_name) == 0);
166}
167
solenbergd97ec302015-10-07 01:40:33 -0700168bool IsCodec(const webrtc::CodecInst& codec, const char* ref_name) {
Minyue Li7100dcd2015-03-27 05:05:59 +0100169 return (_stricmp(codec.plname, ref_name) == 0);
170}
171
solenbergd97ec302015-10-07 01:40:33 -0700172bool FindCodec(const std::vector<AudioCodec>& codecs,
solenberg26c8c912015-11-27 04:00:25 -0800173 const AudioCodec& codec,
174 AudioCodec* found_codec) {
Fredrik Solenbergaf9fb212015-08-26 10:45:53 +0200175 for (const AudioCodec& c : codecs) {
176 if (c.Matches(codec)) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000177 if (found_codec != NULL) {
Fredrik Solenbergaf9fb212015-08-26 10:45:53 +0200178 *found_codec = c;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000179 }
180 return true;
181 }
182 }
183 return false;
184}
wu@webrtc.org1d1ffc92013-10-16 18:12:02 +0000185
solenberg0b675462015-10-09 01:37:09 -0700186bool VerifyUniquePayloadTypes(const std::vector<AudioCodec>& codecs) {
187 if (codecs.empty()) {
188 return true;
189 }
190 std::vector<int> payload_types;
191 for (const AudioCodec& codec : codecs) {
192 payload_types.push_back(codec.id);
193 }
194 std::sort(payload_types.begin(), payload_types.end());
195 auto it = std::unique(payload_types.begin(), payload_types.end());
196 return it == payload_types.end();
197}
198
solenbergd97ec302015-10-07 01:40:33 -0700199bool IsNackEnabled(const AudioCodec& codec) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000200 return codec.HasFeedbackParam(FeedbackParam(kRtcpFbParamNack,
201 kParamValueEmpty));
202}
203
Minyue Li7100dcd2015-03-27 05:05:59 +0100204// Return true if codec.params[feature] == "1", false otherwise.
solenberg26c8c912015-11-27 04:00:25 -0800205bool IsCodecFeatureEnabled(const AudioCodec& codec, const char* feature) {
Minyue Li7100dcd2015-03-27 05:05:59 +0100206 int value;
207 return codec.GetParam(feature, &value) && value == 1;
208}
209
210// Use params[kCodecParamMaxAverageBitrate] if it is defined, use codec.bitrate
211// otherwise. If the value (either from params or codec.bitrate) <=0, use the
212// default configuration. If the value is beyond feasible bit rate of Opus,
213// clamp it. Returns the Opus bit rate for operation.
solenbergd97ec302015-10-07 01:40:33 -0700214int GetOpusBitrate(const AudioCodec& codec, int max_playback_rate) {
Minyue Li7100dcd2015-03-27 05:05:59 +0100215 int bitrate = 0;
216 bool use_param = true;
217 if (!codec.GetParam(kCodecParamMaxAverageBitrate, &bitrate)) {
218 bitrate = codec.bitrate;
219 use_param = false;
220 }
221 if (bitrate <= 0) {
222 if (max_playback_rate <= 8000) {
223 bitrate = kOpusBitrateNb;
224 } else if (max_playback_rate <= 16000) {
225 bitrate = kOpusBitrateWb;
226 } else {
227 bitrate = kOpusBitrateFb;
228 }
229
230 if (IsCodecFeatureEnabled(codec, kCodecParamStereo)) {
231 bitrate *= 2;
232 }
233 } else if (bitrate < kOpusMinBitrate || bitrate > kOpusMaxBitrate) {
234 bitrate = (bitrate < kOpusMinBitrate) ? kOpusMinBitrate : kOpusMaxBitrate;
235 std::string rate_source =
236 use_param ? "Codec parameter \"maxaveragebitrate\"" :
237 "Supplied Opus bitrate";
238 LOG(LS_WARNING) << rate_source
239 << " is invalid and is replaced by: "
240 << bitrate;
241 }
242 return bitrate;
243}
244
245// Returns kOpusDefaultPlaybackRate if params[kCodecParamMaxPlaybackRate] is not
246// defined. Returns the value of params[kCodecParamMaxPlaybackRate] otherwise.
solenbergd97ec302015-10-07 01:40:33 -0700247int GetOpusMaxPlaybackRate(const AudioCodec& codec) {
Minyue Li7100dcd2015-03-27 05:05:59 +0100248 int value;
249 if (codec.GetParam(kCodecParamMaxPlaybackRate, &value)) {
250 return value;
251 }
252 return kOpusDefaultMaxPlaybackRate;
253}
254
solenbergd97ec302015-10-07 01:40:33 -0700255void GetOpusConfig(const AudioCodec& codec, webrtc::CodecInst* voe_codec,
Minyue Li7100dcd2015-03-27 05:05:59 +0100256 bool* enable_codec_fec, int* max_playback_rate,
257 bool* enable_codec_dtx) {
258 *enable_codec_fec = IsCodecFeatureEnabled(codec, kCodecParamUseInbandFec);
259 *enable_codec_dtx = IsCodecFeatureEnabled(codec, kCodecParamUseDtx);
260 *max_playback_rate = GetOpusMaxPlaybackRate(codec);
261
262 // If OPUS, change what we send according to the "stereo" codec
263 // parameter, and not the "channels" parameter. We set
264 // voe_codec.channels to 2 if "stereo=1" and 1 otherwise. If
265 // the bitrate is not specified, i.e. is <= zero, we set it to the
266 // appropriate default value for mono or stereo Opus.
267
268 voe_codec->channels = IsCodecFeatureEnabled(codec, kCodecParamStereo) ? 2 : 1;
269 voe_codec->rate = GetOpusBitrate(codec, *max_playback_rate);
270}
271
wu@webrtc.org1d1ffc92013-10-16 18:12:02 +0000272// Gets the default set of options applied to the engine. Historically, these
273// were supplied as a combination of flags from the channel manager (ec, agc,
274// ns, and highpass) and the rest hardcoded in InitInternal.
solenbergd97ec302015-10-07 01:40:33 -0700275AudioOptions GetDefaultEngineOptions() {
wu@webrtc.org1d1ffc92013-10-16 18:12:02 +0000276 AudioOptions options;
Karl Wibergbe579832015-11-10 22:34:18 +0100277 options.echo_cancellation = rtc::Optional<bool>(true);
278 options.auto_gain_control = rtc::Optional<bool>(true);
279 options.noise_suppression = rtc::Optional<bool>(true);
280 options.highpass_filter = rtc::Optional<bool>(true);
281 options.stereo_swapping = rtc::Optional<bool>(false);
282 options.audio_jitter_buffer_max_packets = rtc::Optional<int>(50);
283 options.audio_jitter_buffer_fast_accelerate = rtc::Optional<bool>(false);
284 options.typing_detection = rtc::Optional<bool>(true);
285 options.adjust_agc_delta = rtc::Optional<int>(0);
286 options.experimental_agc = rtc::Optional<bool>(false);
287 options.extended_filter_aec = rtc::Optional<bool>(false);
288 options.delay_agnostic_aec = rtc::Optional<bool>(false);
289 options.experimental_ns = rtc::Optional<bool>(false);
290 options.aec_dump = rtc::Optional<bool>(false);
wu@webrtc.org1d1ffc92013-10-16 18:12:02 +0000291 return options;
292}
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000293
solenberg566ef242015-11-06 15:34:49 -0800294webrtc::AudioState::Config MakeAudioStateConfig(VoEWrapper* voe_wrapper) {
295 webrtc::AudioState::Config config;
296 config.voice_engine = voe_wrapper->engine();
297 return config;
298}
299
solenberg26c8c912015-11-27 04:00:25 -0800300class WebRtcVoiceCodecs final {
301 public:
302 // TODO(solenberg): Do this filtering once off-line, add a simple AudioCodec
303 // list and add a test which verifies VoE supports the listed codecs.
304 static std::vector<AudioCodec> SupportedCodecs() {
305 LOG(LS_INFO) << "WebRtc VoiceEngine codecs:";
306 std::vector<AudioCodec> result;
307 for (webrtc::CodecInst voe_codec : webrtc::acm2::RentACodec::Database()) {
308 // Change the sample rate of G722 to 8000 to match SDP.
309 MaybeFixupG722(&voe_codec, 8000);
buildbot@webrtc.org13d67762014-05-02 17:33:29 +0000310 // Skip uncompressed formats.
Minyue Li7100dcd2015-03-27 05:05:59 +0100311 if (IsCodec(voe_codec, kL16CodecName)) {
buildbot@webrtc.org13d67762014-05-02 17:33:29 +0000312 continue;
313 }
314
315 const CodecPref* pref = NULL;
tfarina5237aaf2015-11-10 23:44:30 -0800316 for (size_t j = 0; j < arraysize(kCodecPrefs); ++j) {
Minyue Li7100dcd2015-03-27 05:05:59 +0100317 if (IsCodec(voe_codec, kCodecPrefs[j].name) &&
buildbot@webrtc.org13d67762014-05-02 17:33:29 +0000318 kCodecPrefs[j].clockrate == voe_codec.plfreq &&
319 kCodecPrefs[j].channels == voe_codec.channels) {
320 pref = &kCodecPrefs[j];
321 break;
322 }
323 }
324
325 if (pref) {
326 // Use the payload type that we've configured in our pref table;
327 // use the offset in our pref table to determine the sort order.
tfarina5237aaf2015-11-10 23:44:30 -0800328 AudioCodec codec(
329 pref->payload_type, voe_codec.plname, voe_codec.plfreq,
330 voe_codec.rate, voe_codec.channels,
331 static_cast<int>(arraysize(kCodecPrefs)) - (pref - kCodecPrefs));
buildbot@webrtc.org13d67762014-05-02 17:33:29 +0000332 LOG(LS_INFO) << ToString(codec);
Minyue Li7100dcd2015-03-27 05:05:59 +0100333 if (IsCodec(codec, kIsacCodecName)) {
minyue@webrtc.org26236952014-10-29 02:27:08 +0000334 // Indicate auto-bitrate in signaling.
buildbot@webrtc.org13d67762014-05-02 17:33:29 +0000335 codec.bitrate = 0;
336 }
Minyue Li7100dcd2015-03-27 05:05:59 +0100337 if (IsCodec(codec, kOpusCodecName)) {
buildbot@webrtc.org13d67762014-05-02 17:33:29 +0000338 // Only add fmtp parameters that differ from the spec.
339 if (kPreferredMinPTime != kOpusDefaultMinPTime) {
340 codec.params[kCodecParamMinPTime] =
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000341 rtc::ToString(kPreferredMinPTime);
buildbot@webrtc.org13d67762014-05-02 17:33:29 +0000342 }
343 if (kPreferredMaxPTime != kOpusDefaultMaxPTime) {
344 codec.params[kCodecParamMaxPTime] =
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000345 rtc::ToString(kPreferredMaxPTime);
buildbot@webrtc.org13d67762014-05-02 17:33:29 +0000346 }
pkasting@chromium.orgd3245462015-02-23 21:28:22 +0000347 codec.SetParam(kCodecParamUseInbandFec, 1);
minyue@webrtc.org4ef22d12014-11-17 09:26:39 +0000348
349 // TODO(hellner): Add ptime, sprop-stereo, and stereo
buildbot@webrtc.org13d67762014-05-02 17:33:29 +0000350 // when they can be set to values other than the default.
351 }
solenberg26c8c912015-11-27 04:00:25 -0800352 result.push_back(codec);
buildbot@webrtc.org13d67762014-05-02 17:33:29 +0000353 } else {
354 LOG(LS_WARNING) << "Unexpected codec: " << ToString(voe_codec);
355 }
356 }
solenberg26c8c912015-11-27 04:00:25 -0800357 // Make sure they are in local preference order.
358 std::sort(result.begin(), result.end(), &AudioCodec::Preferable);
359 return result;
buildbot@webrtc.org13d67762014-05-02 17:33:29 +0000360 }
buildbot@webrtc.org13d67762014-05-02 17:33:29 +0000361
solenberg26c8c912015-11-27 04:00:25 -0800362 static bool ToCodecInst(const AudioCodec& in,
363 webrtc::CodecInst* out) {
364 for (webrtc::CodecInst voe_codec : webrtc::acm2::RentACodec::Database()) {
365 // Change the sample rate of G722 to 8000 to match SDP.
366 MaybeFixupG722(&voe_codec, 8000);
367 AudioCodec codec(voe_codec.pltype, voe_codec.plname, voe_codec.plfreq,
368 voe_codec.rate, voe_codec.channels, 0);
369 bool multi_rate = IsCodecMultiRate(voe_codec);
370 // Allow arbitrary rates for ISAC to be specified.
371 if (multi_rate) {
372 // Set codec.bitrate to 0 so the check for codec.Matches() passes.
373 codec.bitrate = 0;
374 }
375 if (codec.Matches(in)) {
376 if (out) {
377 // Fixup the payload type.
378 voe_codec.pltype = in.id;
379
380 // Set bitrate if specified.
381 if (multi_rate && in.bitrate != 0) {
382 voe_codec.rate = in.bitrate;
383 }
384
385 // Reset G722 sample rate to 16000 to match WebRTC.
386 MaybeFixupG722(&voe_codec, 16000);
387
388 // Apply codec-specific settings.
389 if (IsCodec(codec, kIsacCodecName)) {
390 // If ISAC and an explicit bitrate is not specified,
391 // enable auto bitrate adjustment.
392 voe_codec.rate = (in.bitrate > 0) ? in.bitrate : -1;
393 }
394 *out = voe_codec;
395 }
396 return true;
397 }
398 }
henrik.lundin@webrtc.org8038d422014-11-11 08:38:24 +0000399 return false;
henrik.lundin@webrtc.orgf85dbce2014-11-07 12:25:00 +0000400 }
solenberg26c8c912015-11-27 04:00:25 -0800401
402 static bool IsCodecMultiRate(const webrtc::CodecInst& codec) {
403 for (size_t i = 0; i < arraysize(kCodecPrefs); ++i) {
404 if (IsCodec(codec, kCodecPrefs[i].name) &&
405 kCodecPrefs[i].clockrate == codec.plfreq) {
406 return kCodecPrefs[i].is_multi_rate;
407 }
408 }
409 return false;
410 }
411
412 // If the AudioCodec param kCodecParamPTime is set, then we will set it to
413 // codec pacsize if it's valid, or we will pick the next smallest value we
414 // support.
415 // TODO(Brave): Query supported packet sizes from ACM when the API is ready.
416 static bool SetPTimeAsPacketSize(webrtc::CodecInst* codec, int ptime_ms) {
417 for (const CodecPref& codec_pref : kCodecPrefs) {
418 if ((IsCodec(*codec, codec_pref.name) &&
419 codec_pref.clockrate == codec->plfreq) ||
420 IsCodec(*codec, kG722CodecName)) {
421 int packet_size_ms = SelectPacketSize(codec_pref, ptime_ms);
422 if (packet_size_ms) {
423 // Convert unit from milli-seconds to samples.
424 codec->pacsize = (codec->plfreq / 1000) * packet_size_ms;
425 return true;
426 }
427 }
428 }
429 return false;
430 }
431
432 private:
433 static const int kMaxNumPacketSize = 6;
434 struct CodecPref {
435 const char* name;
436 int clockrate;
437 int channels;
438 int payload_type;
439 bool is_multi_rate;
440 int packet_sizes_ms[kMaxNumPacketSize];
441 };
442 // Note: keep the supported packet sizes in ascending order.
443 static const CodecPref kCodecPrefs[12];
444
445 static int SelectPacketSize(const CodecPref& codec_pref, int ptime_ms) {
446 int selected_packet_size_ms = codec_pref.packet_sizes_ms[0];
447 for (int packet_size_ms : codec_pref.packet_sizes_ms) {
448 if (packet_size_ms && packet_size_ms <= ptime_ms) {
449 selected_packet_size_ms = packet_size_ms;
450 }
451 }
452 return selected_packet_size_ms;
453 }
454
455 // Changes RTP timestamp rate of G722. This is due to the "bug" in the RFC
456 // which says that G722 should be advertised as 8 kHz although it is a 16 kHz
457 // codec.
458 static void MaybeFixupG722(webrtc::CodecInst* voe_codec, int new_plfreq) {
459 if (IsCodec(*voe_codec, kG722CodecName)) {
460 // If the ASSERT triggers, the codec definition in WebRTC VoiceEngine
461 // has changed, and this special case is no longer needed.
462 RTC_DCHECK(voe_codec->plfreq != new_plfreq);
463 voe_codec->plfreq = new_plfreq;
464 }
465 }
466};
467
468const WebRtcVoiceCodecs::CodecPref WebRtcVoiceCodecs::kCodecPrefs[12] = {
469 { kOpusCodecName, 48000, 2, 111, true, { 10, 20, 40, 60 } },
470 { kIsacCodecName, 16000, 1, 103, true, { 30, 60 } },
471 { kIsacCodecName, 32000, 1, 104, true, { 30 } },
472 // G722 should be advertised as 8000 Hz because of the RFC "bug".
473 { kG722CodecName, 8000, 1, 9, false, { 10, 20, 30, 40, 50, 60 } },
474 { kIlbcCodecName, 8000, 1, 102, false, { 20, 30, 40, 60 } },
475 { kPcmuCodecName, 8000, 1, 0, false, { 10, 20, 30, 40, 50, 60 } },
476 { kPcmaCodecName, 8000, 1, 8, false, { 10, 20, 30, 40, 50, 60 } },
477 { kCnCodecName, 32000, 1, 106, false, { } },
478 { kCnCodecName, 16000, 1, 105, false, { } },
479 { kCnCodecName, 8000, 1, 13, false, { } },
480 { kRedCodecName, 8000, 1, 127, false, { } },
481 { kDtmfCodecName, 8000, 1, 126, false, { } },
482};
483} // namespace {
484
485bool WebRtcVoiceEngine::ToCodecInst(const AudioCodec& in,
486 webrtc::CodecInst* out) {
487 return WebRtcVoiceCodecs::ToCodecInst(in, out);
488}
489
490WebRtcVoiceEngine::WebRtcVoiceEngine()
491 : voe_wrapper_(new VoEWrapper()),
492 audio_state_(webrtc::AudioState::Create(MakeAudioStateConfig(voe()))) {
493 Construct();
494}
495
496WebRtcVoiceEngine::WebRtcVoiceEngine(VoEWrapper* voe_wrapper)
497 : voe_wrapper_(voe_wrapper) {
498 Construct();
499}
500
501void WebRtcVoiceEngine::Construct() {
502 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
503 LOG(LS_VERBOSE) << "WebRtcVoiceEngine::WebRtcVoiceEngine";
504
505 signal_thread_checker_.DetachFromThread();
506 std::memset(&default_agc_config_, 0, sizeof(default_agc_config_));
507
508 webrtc::Trace::set_level_filter(kDefaultTraceFilter);
509 webrtc::Trace::SetTraceCallback(this);
510
511 // Load our audio codec list.
512 codecs_ = WebRtcVoiceCodecs::SupportedCodecs();
513
514 // Load our RTP Header extensions.
515 rtp_header_extensions_.push_back(
516 RtpHeaderExtension(kRtpAudioLevelHeaderExtension,
517 kRtpAudioLevelHeaderExtensionDefaultId));
518 rtp_header_extensions_.push_back(
519 RtpHeaderExtension(kRtpAbsoluteSenderTimeHeaderExtension,
520 kRtpAbsoluteSenderTimeHeaderExtensionDefaultId));
521 if (webrtc::field_trial::FindFullName("WebRTC-SendSideBwe") == "Enabled") {
522 rtp_header_extensions_.push_back(RtpHeaderExtension(
523 kRtpTransportSequenceNumberHeaderExtension,
524 kRtpTransportSequenceNumberHeaderExtensionDefaultId));
525 }
526 options_ = GetDefaultEngineOptions();
henrik.lundin@webrtc.orgf85dbce2014-11-07 12:25:00 +0000527}
528
buildbot@webrtc.org13d67762014-05-02 17:33:29 +0000529WebRtcVoiceEngine::~WebRtcVoiceEngine() {
solenberg566ef242015-11-06 15:34:49 -0800530 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
buildbot@webrtc.org13d67762014-05-02 17:33:29 +0000531 LOG(LS_VERBOSE) << "WebRtcVoiceEngine::~WebRtcVoiceEngine";
buildbot@webrtc.org13d67762014-05-02 17:33:29 +0000532 if (adm_) {
533 voe_wrapper_.reset();
534 adm_->Release();
535 adm_ = NULL;
536 }
solenbergbd138382015-11-20 16:08:07 -0800537 webrtc::Trace::SetTraceCallback(nullptr);
buildbot@webrtc.org13d67762014-05-02 17:33:29 +0000538}
539
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000540bool WebRtcVoiceEngine::Init(rtc::Thread* worker_thread) {
solenberg566ef242015-11-06 15:34:49 -0800541 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
henrikg91d6ede2015-09-17 00:24:34 -0700542 RTC_DCHECK(worker_thread == rtc::Thread::Current());
buildbot@webrtc.org13d67762014-05-02 17:33:29 +0000543 LOG(LS_INFO) << "WebRtcVoiceEngine::Init";
544 bool res = InitInternal();
545 if (res) {
546 LOG(LS_INFO) << "WebRtcVoiceEngine::Init Done!";
547 } else {
548 LOG(LS_ERROR) << "WebRtcVoiceEngine::Init failed";
549 Terminate();
550 }
551 return res;
552}
553
554bool WebRtcVoiceEngine::InitInternal() {
solenberg566ef242015-11-06 15:34:49 -0800555 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
buildbot@webrtc.org13d67762014-05-02 17:33:29 +0000556 // Temporarily turn logging level up for the Init call
solenbergbd138382015-11-20 16:08:07 -0800557 webrtc::Trace::set_level_filter(kElevatedTraceFilter);
solenberg2515af22015-12-02 06:19:36 -0800558 LOG(LS_INFO) << webrtc::VoiceEngine::GetVersionString();
buildbot@webrtc.org13d67762014-05-02 17:33:29 +0000559 if (voe_wrapper_->base()->Init(adm_) == -1) {
560 LOG_RTCERR0_EX(Init, voe_wrapper_->error());
buildbot@webrtc.org13d67762014-05-02 17:33:29 +0000561 return false;
562 }
solenbergbd138382015-11-20 16:08:07 -0800563 webrtc::Trace::set_level_filter(kDefaultTraceFilter);
buildbot@webrtc.org13d67762014-05-02 17:33:29 +0000564
buildbot@webrtc.org13d67762014-05-02 17:33:29 +0000565 // Save the default AGC configuration settings. This must happen before
566 // calling SetOptions or the default will be overwritten.
567 if (voe_wrapper_->processing()->GetAgcConfig(default_agc_config_) == -1) {
568 LOG_RTCERR0(GetAgcConfig);
569 return false;
570 }
571
572 // Set defaults for options, so that ApplyOptions applies them explicitly
573 // when we clear option (channel) overrides. External clients can still
574 // modify the defaults via SetOptions (on the media engine).
575 if (!SetOptions(GetDefaultEngineOptions())) {
576 return false;
577 }
578
579 // Print our codec list again for the call diagnostic log
580 LOG(LS_INFO) << "WebRtc VoiceEngine codecs:";
Fredrik Solenbergaf9fb212015-08-26 10:45:53 +0200581 for (const AudioCodec& codec : codecs_) {
582 LOG(LS_INFO) << ToString(codec);
buildbot@webrtc.org13d67762014-05-02 17:33:29 +0000583 }
584
585 // Disable the DTMF playout when a tone is sent.
586 // PlayDtmfTone will be used if local playout is needed.
587 if (voe_wrapper_->dtmf()->SetDtmfFeedbackStatus(false) == -1) {
588 LOG_RTCERR1(SetDtmfFeedbackStatus, false);
589 }
590
591 initialized_ = true;
592 return true;
593}
594
buildbot@webrtc.org13d67762014-05-02 17:33:29 +0000595void WebRtcVoiceEngine::Terminate() {
solenberg566ef242015-11-06 15:34:49 -0800596 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
buildbot@webrtc.org13d67762014-05-02 17:33:29 +0000597 LOG(LS_INFO) << "WebRtcVoiceEngine::Terminate";
598 initialized_ = false;
599
600 StopAecDump();
601
buildbot@webrtc.org13d67762014-05-02 17:33:29 +0000602 voe_wrapper_->base()->Terminate();
buildbot@webrtc.org13d67762014-05-02 17:33:29 +0000603}
604
solenberg566ef242015-11-06 15:34:49 -0800605rtc::scoped_refptr<webrtc::AudioState>
606 WebRtcVoiceEngine::GetAudioState() const {
607 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
608 return audio_state_;
609}
610
Fredrik Solenberg709ed672015-09-15 12:26:33 +0200611VoiceMediaChannel* WebRtcVoiceEngine::CreateChannel(webrtc::Call* call,
Jelena Marusicc28a8962015-05-29 15:05:44 +0200612 const AudioOptions& options) {
solenberg566ef242015-11-06 15:34:49 -0800613 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
solenberg0a617e22015-10-20 15:49:38 -0700614 return new WebRtcVoiceMediaChannel(this, options, call);
buildbot@webrtc.org13d67762014-05-02 17:33:29 +0000615}
616
buildbot@webrtc.org13d67762014-05-02 17:33:29 +0000617bool WebRtcVoiceEngine::SetOptions(const AudioOptions& options) {
solenberg566ef242015-11-06 15:34:49 -0800618 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
buildbot@webrtc.org13d67762014-05-02 17:33:29 +0000619 if (!ApplyOptions(options)) {
620 return false;
621 }
622 options_ = options;
623 return true;
624}
625
buildbot@webrtc.org13d67762014-05-02 17:33:29 +0000626// AudioOptions defaults are set in InitInternal (for options with corresponding
627// MediaEngineInterface flags) and in SetOptions(int) for flagless options.
628bool WebRtcVoiceEngine::ApplyOptions(const AudioOptions& options_in) {
solenberg566ef242015-11-06 15:34:49 -0800629 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
henrikac14f5ff2015-09-23 14:08:33 +0200630 LOG(LS_INFO) << "ApplyOptions: " << options_in.ToString();
buildbot@webrtc.org13d67762014-05-02 17:33:29 +0000631 AudioOptions options = options_in; // The options are modified below.
632 // kEcConference is AEC with high suppression.
633 webrtc::EcModes ec_mode = webrtc::kEcConference;
634 webrtc::AecmModes aecm_mode = webrtc::kAecmSpeakerphone;
635 webrtc::AgcModes agc_mode = webrtc::kAgcAdaptiveAnalog;
636 webrtc::NsModes ns_mode = webrtc::kNsHighSuppression;
kwiberg102c6a62015-10-30 02:47:38 -0700637 if (options.aecm_generate_comfort_noise) {
buildbot@webrtc.org13d67762014-05-02 17:33:29 +0000638 LOG(LS_VERBOSE) << "Comfort noise explicitly set to "
kwiberg102c6a62015-10-30 02:47:38 -0700639 << *options.aecm_generate_comfort_noise
640 << " (default is false).";
buildbot@webrtc.org13d67762014-05-02 17:33:29 +0000641 }
642
643#if defined(IOS)
644 // On iOS, VPIO provides built-in EC and AGC.
Karl Wibergbe579832015-11-10 22:34:18 +0100645 options.echo_cancellation = rtc::Optional<bool>(false);
646 options.auto_gain_control = rtc::Optional<bool>(false);
henrika86d907c2015-09-07 16:09:50 +0200647 LOG(LS_INFO) << "Always disable AEC and AGC on iOS. Use built-in instead.";
buildbot@webrtc.org13d67762014-05-02 17:33:29 +0000648#elif defined(ANDROID)
649 ec_mode = webrtc::kEcAecm;
650#endif
651
652#if defined(IOS) || defined(ANDROID)
653 // Set the AGC mode for iOS as well despite disabling it above, to avoid
654 // unsupported configuration errors from webrtc.
655 agc_mode = webrtc::kAgcFixedDigital;
Karl Wibergbe579832015-11-10 22:34:18 +0100656 options.typing_detection = rtc::Optional<bool>(false);
657 options.experimental_agc = rtc::Optional<bool>(false);
658 options.extended_filter_aec = rtc::Optional<bool>(false);
659 options.experimental_ns = rtc::Optional<bool>(false);
buildbot@webrtc.org13d67762014-05-02 17:33:29 +0000660#endif
661
Bjorn Volckerbf395c12015-03-25 22:45:56 +0100662 // Delay Agnostic AEC automatically turns on EC if not set except on iOS
663 // where the feature is not supported.
664 bool use_delay_agnostic_aec = false;
665#if !defined(IOS)
kwiberg102c6a62015-10-30 02:47:38 -0700666 if (options.delay_agnostic_aec) {
667 use_delay_agnostic_aec = *options.delay_agnostic_aec;
Bjorn Volckerbf395c12015-03-25 22:45:56 +0100668 if (use_delay_agnostic_aec) {
Karl Wibergbe579832015-11-10 22:34:18 +0100669 options.echo_cancellation = rtc::Optional<bool>(true);
670 options.extended_filter_aec = rtc::Optional<bool>(true);
Bjorn Volckerbf395c12015-03-25 22:45:56 +0100671 ec_mode = webrtc::kEcConference;
672 }
673 }
674#endif
675
buildbot@webrtc.org13d67762014-05-02 17:33:29 +0000676 webrtc::VoEAudioProcessing* voep = voe_wrapper_->processing();
677
kwiberg102c6a62015-10-30 02:47:38 -0700678 if (options.echo_cancellation) {
henrika@webrtc.orga954c072014-12-09 16:22:09 +0000679 // Check if platform supports built-in EC. Currently only supported on
680 // Android and in combination with Java based audio layer.
681 // TODO(henrika): investigate possibility to support built-in EC also
682 // in combination with Open SL ES audio.
683 const bool built_in_aec = voe_wrapper_->hw()->BuiltInAECIsAvailable();
Bjorn Volcker73f72102015-06-03 14:50:15 +0200684 if (built_in_aec) {
Bjorn Volckerccfc9392015-05-07 07:43:17 +0200685 // Built-in EC exists on this device and use_delay_agnostic_aec is not
686 // overriding it. Enable/Disable it according to the echo_cancellation
687 // audio option.
Bjorn Volcker73f72102015-06-03 14:50:15 +0200688 const bool enable_built_in_aec =
kwiberg102c6a62015-10-30 02:47:38 -0700689 *options.echo_cancellation && !use_delay_agnostic_aec;
Bjorn Volcker73f72102015-06-03 14:50:15 +0200690 if (voe_wrapper_->hw()->EnableBuiltInAEC(enable_built_in_aec) == 0 &&
691 enable_built_in_aec) {
Bjorn Volckerbf395c12015-03-25 22:45:56 +0100692 // Disable internal software EC if built-in EC is enabled,
henrika@webrtc.orga954c072014-12-09 16:22:09 +0000693 // i.e., replace the software EC with the built-in EC.
Karl Wibergbe579832015-11-10 22:34:18 +0100694 options.echo_cancellation = rtc::Optional<bool>(false);
henrika@webrtc.orga954c072014-12-09 16:22:09 +0000695 LOG(LS_INFO) << "Disabling EC since built-in EC will be used instead";
696 }
697 }
kwiberg102c6a62015-10-30 02:47:38 -0700698 if (voep->SetEcStatus(*options.echo_cancellation, ec_mode) == -1) {
699 LOG_RTCERR2(SetEcStatus, *options.echo_cancellation, ec_mode);
buildbot@webrtc.org13d67762014-05-02 17:33:29 +0000700 return false;
701 } else {
kwiberg102c6a62015-10-30 02:47:38 -0700702 LOG(LS_INFO) << "Echo control set to " << *options.echo_cancellation
henrika86d907c2015-09-07 16:09:50 +0200703 << " with mode " << ec_mode;
buildbot@webrtc.org13d67762014-05-02 17:33:29 +0000704 }
705#if !defined(ANDROID)
706 // TODO(ajm): Remove the error return on Android from webrtc.
kwiberg102c6a62015-10-30 02:47:38 -0700707 if (voep->SetEcMetricsStatus(*options.echo_cancellation) == -1) {
708 LOG_RTCERR1(SetEcMetricsStatus, *options.echo_cancellation);
buildbot@webrtc.org13d67762014-05-02 17:33:29 +0000709 return false;
710 }
711#endif
712 if (ec_mode == webrtc::kEcAecm) {
kwiberg102c6a62015-10-30 02:47:38 -0700713 bool cn = options.aecm_generate_comfort_noise.value_or(false);
714 if (voep->SetAecmMode(aecm_mode, cn) != 0) {
715 LOG_RTCERR2(SetAecmMode, aecm_mode, cn);
buildbot@webrtc.org13d67762014-05-02 17:33:29 +0000716 return false;
717 }
718 }
719 }
720
kwiberg102c6a62015-10-30 02:47:38 -0700721 if (options.auto_gain_control) {
henrikac14f5ff2015-09-23 14:08:33 +0200722 const bool built_in_agc = voe_wrapper_->hw()->BuiltInAGCIsAvailable();
723 if (built_in_agc) {
kwiberg102c6a62015-10-30 02:47:38 -0700724 if (voe_wrapper_->hw()->EnableBuiltInAGC(*options.auto_gain_control) ==
725 0 &&
726 *options.auto_gain_control) {
henrikac14f5ff2015-09-23 14:08:33 +0200727 // Disable internal software AGC if built-in AGC is enabled,
728 // i.e., replace the software AGC with the built-in AGC.
Karl Wibergbe579832015-11-10 22:34:18 +0100729 options.auto_gain_control = rtc::Optional<bool>(false);
henrikac14f5ff2015-09-23 14:08:33 +0200730 LOG(LS_INFO) << "Disabling AGC since built-in AGC will be used instead";
731 }
732 }
kwiberg102c6a62015-10-30 02:47:38 -0700733 if (voep->SetAgcStatus(*options.auto_gain_control, agc_mode) == -1) {
734 LOG_RTCERR2(SetAgcStatus, *options.auto_gain_control, agc_mode);
buildbot@webrtc.org13d67762014-05-02 17:33:29 +0000735 return false;
736 } else {
kwiberg102c6a62015-10-30 02:47:38 -0700737 LOG(LS_INFO) << "Auto gain set to " << *options.auto_gain_control
738 << " with mode " << agc_mode;
buildbot@webrtc.org13d67762014-05-02 17:33:29 +0000739 }
740 }
741
kwiberg102c6a62015-10-30 02:47:38 -0700742 if (options.tx_agc_target_dbov || options.tx_agc_digital_compression_gain ||
743 options.tx_agc_limiter) {
buildbot@webrtc.org13d67762014-05-02 17:33:29 +0000744 // Override default_agc_config_. Generally, an unset option means "leave
745 // the VoE bits alone" in this function, so we want whatever is set to be
746 // stored as the new "default". If we didn't, then setting e.g.
747 // tx_agc_target_dbov would reset digital compression gain and limiter
748 // settings.
749 // Also, if we don't update default_agc_config_, then adjust_agc_delta
750 // would be an offset from the original values, and not whatever was set
751 // explicitly.
kwiberg102c6a62015-10-30 02:47:38 -0700752 default_agc_config_.targetLeveldBOv = options.tx_agc_target_dbov.value_or(
753 default_agc_config_.targetLeveldBOv);
buildbot@webrtc.org13d67762014-05-02 17:33:29 +0000754 default_agc_config_.digitalCompressionGaindB =
kwiberg102c6a62015-10-30 02:47:38 -0700755 options.tx_agc_digital_compression_gain.value_or(
buildbot@webrtc.org13d67762014-05-02 17:33:29 +0000756 default_agc_config_.digitalCompressionGaindB);
757 default_agc_config_.limiterEnable =
kwiberg102c6a62015-10-30 02:47:38 -0700758 options.tx_agc_limiter.value_or(default_agc_config_.limiterEnable);
buildbot@webrtc.org13d67762014-05-02 17:33:29 +0000759 if (voe_wrapper_->processing()->SetAgcConfig(default_agc_config_) == -1) {
760 LOG_RTCERR3(SetAgcConfig,
761 default_agc_config_.targetLeveldBOv,
762 default_agc_config_.digitalCompressionGaindB,
763 default_agc_config_.limiterEnable);
764 return false;
765 }
766 }
767
kwiberg102c6a62015-10-30 02:47:38 -0700768 if (options.noise_suppression) {
henrikac14f5ff2015-09-23 14:08:33 +0200769 const bool built_in_ns = voe_wrapper_->hw()->BuiltInNSIsAvailable();
770 if (built_in_ns) {
kwiberg102c6a62015-10-30 02:47:38 -0700771 if (voe_wrapper_->hw()->EnableBuiltInNS(*options.noise_suppression) ==
772 0 &&
773 *options.noise_suppression) {
henrikac14f5ff2015-09-23 14:08:33 +0200774 // Disable internal software NS if built-in NS is enabled,
775 // i.e., replace the software NS with the built-in NS.
Karl Wibergbe579832015-11-10 22:34:18 +0100776 options.noise_suppression = rtc::Optional<bool>(false);
henrikac14f5ff2015-09-23 14:08:33 +0200777 LOG(LS_INFO) << "Disabling NS since built-in NS will be used instead";
778 }
779 }
kwiberg102c6a62015-10-30 02:47:38 -0700780 if (voep->SetNsStatus(*options.noise_suppression, ns_mode) == -1) {
781 LOG_RTCERR2(SetNsStatus, *options.noise_suppression, ns_mode);
buildbot@webrtc.org13d67762014-05-02 17:33:29 +0000782 return false;
783 } else {
kwiberg102c6a62015-10-30 02:47:38 -0700784 LOG(LS_INFO) << "Noise suppression set to " << *options.noise_suppression
henrikac14f5ff2015-09-23 14:08:33 +0200785 << " with mode " << ns_mode;
buildbot@webrtc.org13d67762014-05-02 17:33:29 +0000786 }
787 }
788
kwiberg102c6a62015-10-30 02:47:38 -0700789 if (options.highpass_filter) {
790 LOG(LS_INFO) << "High pass filter enabled? " << *options.highpass_filter;
791 if (voep->EnableHighPassFilter(*options.highpass_filter) == -1) {
792 LOG_RTCERR1(SetHighpassFilterStatus, *options.highpass_filter);
buildbot@webrtc.org13d67762014-05-02 17:33:29 +0000793 return false;
794 }
795 }
796
kwiberg102c6a62015-10-30 02:47:38 -0700797 if (options.stereo_swapping) {
798 LOG(LS_INFO) << "Stereo swapping enabled? " << *options.stereo_swapping;
799 voep->EnableStereoChannelSwapping(*options.stereo_swapping);
800 if (voep->IsStereoChannelSwappingEnabled() != *options.stereo_swapping) {
801 LOG_RTCERR1(EnableStereoChannelSwapping, *options.stereo_swapping);
buildbot@webrtc.org13d67762014-05-02 17:33:29 +0000802 return false;
803 }
804 }
805
kwiberg102c6a62015-10-30 02:47:38 -0700806 if (options.audio_jitter_buffer_max_packets) {
807 LOG(LS_INFO) << "NetEq capacity is "
808 << *options.audio_jitter_buffer_max_packets;
Henrik Lundin64dad832015-05-11 12:44:23 +0200809 voe_config_.Set<webrtc::NetEqCapacityConfig>(
kwiberg102c6a62015-10-30 02:47:38 -0700810 new webrtc::NetEqCapacityConfig(
811 *options.audio_jitter_buffer_max_packets));
Henrik Lundin64dad832015-05-11 12:44:23 +0200812 }
813
kwiberg102c6a62015-10-30 02:47:38 -0700814 if (options.audio_jitter_buffer_fast_accelerate) {
815 LOG(LS_INFO) << "NetEq fast mode? "
816 << *options.audio_jitter_buffer_fast_accelerate;
Henrik Lundin5263b3c2015-06-01 10:29:41 +0200817 voe_config_.Set<webrtc::NetEqFastAccelerate>(
kwiberg102c6a62015-10-30 02:47:38 -0700818 new webrtc::NetEqFastAccelerate(
819 *options.audio_jitter_buffer_fast_accelerate));
Henrik Lundin5263b3c2015-06-01 10:29:41 +0200820 }
821
kwiberg102c6a62015-10-30 02:47:38 -0700822 if (options.typing_detection) {
823 LOG(LS_INFO) << "Typing detection is enabled? "
824 << *options.typing_detection;
825 if (voep->SetTypingDetectionStatus(*options.typing_detection) == -1) {
buildbot@webrtc.org13d67762014-05-02 17:33:29 +0000826 // In case of error, log the info and continue
kwiberg102c6a62015-10-30 02:47:38 -0700827 LOG_RTCERR1(SetTypingDetectionStatus, *options.typing_detection);
buildbot@webrtc.org13d67762014-05-02 17:33:29 +0000828 }
829 }
830
kwiberg102c6a62015-10-30 02:47:38 -0700831 if (options.adjust_agc_delta) {
832 LOG(LS_INFO) << "Adjust agc delta is " << *options.adjust_agc_delta;
833 if (!AdjustAgcLevel(*options.adjust_agc_delta)) {
buildbot@webrtc.org13d67762014-05-02 17:33:29 +0000834 return false;
835 }
836 }
837
kwiberg102c6a62015-10-30 02:47:38 -0700838 if (options.aec_dump) {
839 LOG(LS_INFO) << "Aec dump is enabled? " << *options.aec_dump;
840 if (*options.aec_dump)
buildbot@webrtc.org13d67762014-05-02 17:33:29 +0000841 StartAecDump(kAecDumpByAudioOptionFilename);
842 else
843 StopAecDump();
844 }
845
buildbot@webrtc.org1f8a2372014-08-28 10:52:44 +0000846 webrtc::Config config;
847
kwiberg102c6a62015-10-30 02:47:38 -0700848 if (options.delay_agnostic_aec)
849 delay_agnostic_aec_ = options.delay_agnostic_aec;
850 if (delay_agnostic_aec_) {
851 LOG(LS_INFO) << "Delay agnostic aec is enabled? " << *delay_agnostic_aec_;
henrik.lundin0f133b92015-07-02 00:17:55 -0700852 config.Set<webrtc::DelayAgnostic>(
kwiberg102c6a62015-10-30 02:47:38 -0700853 new webrtc::DelayAgnostic(*delay_agnostic_aec_));
Bjorn Volckerbf395c12015-03-25 22:45:56 +0100854 }
855
kwiberg102c6a62015-10-30 02:47:38 -0700856 if (options.extended_filter_aec) {
857 extended_filter_aec_ = options.extended_filter_aec;
858 }
859 if (extended_filter_aec_) {
860 LOG(LS_INFO) << "Extended filter aec is enabled? " << *extended_filter_aec_;
Henrik Lundin441f6342015-06-09 16:03:13 +0200861 config.Set<webrtc::ExtendedFilter>(
kwiberg102c6a62015-10-30 02:47:38 -0700862 new webrtc::ExtendedFilter(*extended_filter_aec_));
buildbot@webrtc.org1f8a2372014-08-28 10:52:44 +0000863 }
864
kwiberg102c6a62015-10-30 02:47:38 -0700865 if (options.experimental_ns) {
866 experimental_ns_ = options.experimental_ns;
867 }
868 if (experimental_ns_) {
869 LOG(LS_INFO) << "Experimental ns is enabled? " << *experimental_ns_;
buildbot@webrtc.org1f8a2372014-08-28 10:52:44 +0000870 config.Set<webrtc::ExperimentalNs>(
kwiberg102c6a62015-10-30 02:47:38 -0700871 new webrtc::ExperimentalNs(*experimental_ns_));
buildbot@webrtc.org1f8a2372014-08-28 10:52:44 +0000872 }
buildbot@webrtc.org1f8a2372014-08-28 10:52:44 +0000873
874 // We check audioproc for the benefit of tests, since FakeWebRtcVoiceEngine
875 // returns NULL on audio_processing().
876 webrtc::AudioProcessing* audioproc = voe_wrapper_->base()->audio_processing();
877 if (audioproc) {
878 audioproc->SetExtraOptions(config);
879 }
880
kwiberg102c6a62015-10-30 02:47:38 -0700881 if (options.recording_sample_rate) {
882 LOG(LS_INFO) << "Recording sample rate is "
883 << *options.recording_sample_rate;
884 if (voe_wrapper_->hw()->SetRecordingSampleRate(
885 *options.recording_sample_rate)) {
886 LOG_RTCERR1(SetRecordingSampleRate, *options.recording_sample_rate);
buildbot@webrtc.org13d67762014-05-02 17:33:29 +0000887 }
888 }
889
kwiberg102c6a62015-10-30 02:47:38 -0700890 if (options.playout_sample_rate) {
891 LOG(LS_INFO) << "Playout sample rate is " << *options.playout_sample_rate;
892 if (voe_wrapper_->hw()->SetPlayoutSampleRate(
893 *options.playout_sample_rate)) {
894 LOG_RTCERR1(SetPlayoutSampleRate, *options.playout_sample_rate);
buildbot@webrtc.org13d67762014-05-02 17:33:29 +0000895 }
896 }
897
898 return true;
899}
900
buildbot@webrtc.org13d67762014-05-02 17:33:29 +0000901// TODO(juberti): Refactor this so that the core logic can be used to set the
902// soundclip device. At that time, reinstate the soundclip pause/resume code.
903bool WebRtcVoiceEngine::SetDevices(const Device* in_device,
904 const Device* out_device) {
solenberg566ef242015-11-06 15:34:49 -0800905 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
buildbot@webrtc.org13d67762014-05-02 17:33:29 +0000906#if !defined(IOS)
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000907 int in_id = in_device ? rtc::FromString<int>(in_device->id) :
buildbot@webrtc.org13d67762014-05-02 17:33:29 +0000908 kDefaultAudioDeviceId;
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000909 int out_id = out_device ? rtc::FromString<int>(out_device->id) :
buildbot@webrtc.org13d67762014-05-02 17:33:29 +0000910 kDefaultAudioDeviceId;
911 // The device manager uses -1 as the default device, which was the case for
912 // VoE 3.5. VoE 4.0, however, uses 0 as the default in Linux and Mac.
913#ifndef WIN32
914 if (-1 == in_id) {
915 in_id = kDefaultAudioDeviceId;
916 }
917 if (-1 == out_id) {
918 out_id = kDefaultAudioDeviceId;
919 }
920#endif
921
922 std::string in_name = (in_id != kDefaultAudioDeviceId) ?
923 in_device->name : "Default device";
924 std::string out_name = (out_id != kDefaultAudioDeviceId) ?
925 out_device->name : "Default device";
926 LOG(LS_INFO) << "Setting microphone to (id=" << in_id << ", name=" << in_name
927 << ") and speaker to (id=" << out_id << ", name=" << out_name
928 << ")";
929
buildbot@webrtc.org13d67762014-05-02 17:33:29 +0000930 // Must also pause all audio playback and capture.
solenbergc1a1b352015-09-22 13:31:20 -0700931 bool ret = true;
Fredrik Solenbergaf9fb212015-08-26 10:45:53 +0200932 for (WebRtcVoiceMediaChannel* channel : channels_) {
buildbot@webrtc.org13d67762014-05-02 17:33:29 +0000933 if (!channel->PausePlayout()) {
934 LOG(LS_WARNING) << "Failed to pause playout";
935 ret = false;
936 }
937 if (!channel->PauseSend()) {
938 LOG(LS_WARNING) << "Failed to pause send";
939 ret = false;
940 }
941 }
942
943 // Find the recording device id in VoiceEngine and set recording device.
944 if (!FindWebRtcAudioDeviceId(true, in_name, in_id, &in_id)) {
945 ret = false;
946 }
947 if (ret) {
948 if (voe_wrapper_->hw()->SetRecordingDevice(in_id) == -1) {
949 LOG_RTCERR2(SetRecordingDevice, in_name, in_id);
950 ret = false;
951 }
buildbot@webrtc.org6b21b712014-07-31 15:08:53 +0000952 webrtc::AudioProcessing* ap = voe()->base()->audio_processing();
953 if (ap)
954 ap->Initialize();
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000955 }
956
957 // Find the playout device id in VoiceEngine and set playout device.
958 if (!FindWebRtcAudioDeviceId(false, out_name, out_id, &out_id)) {
959 LOG(LS_WARNING) << "Failed to find VoiceEngine device id for " << out_name;
960 ret = false;
961 }
962 if (ret) {
963 if (voe_wrapper_->hw()->SetPlayoutDevice(out_id) == -1) {
sergeyu@chromium.org5bc25c42013-12-05 00:24:06 +0000964 LOG_RTCERR2(SetPlayoutDevice, out_name, out_id);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000965 ret = false;
966 }
967 }
968
969 // Resume all audio playback and capture.
Fredrik Solenbergaf9fb212015-08-26 10:45:53 +0200970 for (WebRtcVoiceMediaChannel* channel : channels_) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000971 if (!channel->ResumePlayout()) {
972 LOG(LS_WARNING) << "Failed to resume playout";
973 ret = false;
974 }
975 if (!channel->ResumeSend()) {
976 LOG(LS_WARNING) << "Failed to resume send";
977 ret = false;
978 }
979 }
980
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000981 if (ret) {
982 LOG(LS_INFO) << "Set microphone to (id=" << in_id <<" name=" << in_name
983 << ") and speaker to (id="<< out_id << " name=" << out_name
984 << ")";
985 }
986
987 return ret;
988#else
989 return true;
wu@webrtc.orgcecfd182013-10-30 05:18:12 +0000990#endif // !IOS
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000991}
992
993bool WebRtcVoiceEngine::FindWebRtcAudioDeviceId(
994 bool is_input, const std::string& dev_name, int dev_id, int* rtc_id) {
solenberg566ef242015-11-06 15:34:49 -0800995 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000996 // In Linux, VoiceEngine uses the same device dev_id as the device manager.
wu@webrtc.orgcecfd182013-10-30 05:18:12 +0000997#if defined(LINUX) || defined(ANDROID)
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000998 *rtc_id = dev_id;
999 return true;
1000#else
1001 // In Windows and Mac, we need to find the VoiceEngine device id by name
1002 // unless the input dev_id is the default device id.
1003 if (kDefaultAudioDeviceId == dev_id) {
1004 *rtc_id = dev_id;
1005 return true;
1006 }
1007
1008 // Get the number of VoiceEngine audio devices.
1009 int count = 0;
1010 if (is_input) {
1011 if (-1 == voe_wrapper_->hw()->GetNumOfRecordingDevices(count)) {
1012 LOG_RTCERR0(GetNumOfRecordingDevices);
1013 return false;
1014 }
1015 } else {
1016 if (-1 == voe_wrapper_->hw()->GetNumOfPlayoutDevices(count)) {
1017 LOG_RTCERR0(GetNumOfPlayoutDevices);
1018 return false;
1019 }
1020 }
1021
1022 for (int i = 0; i < count; ++i) {
1023 char name[128];
1024 char guid[128];
1025 if (is_input) {
1026 voe_wrapper_->hw()->GetRecordingDeviceName(i, name, guid);
1027 LOG(LS_VERBOSE) << "VoiceEngine microphone " << i << ": " << name;
1028 } else {
1029 voe_wrapper_->hw()->GetPlayoutDeviceName(i, name, guid);
1030 LOG(LS_VERBOSE) << "VoiceEngine speaker " << i << ": " << name;
1031 }
1032
1033 std::string webrtc_name(name);
1034 if (dev_name.compare(0, webrtc_name.size(), webrtc_name) == 0) {
1035 *rtc_id = i;
1036 return true;
1037 }
1038 }
1039 LOG(LS_WARNING) << "VoiceEngine cannot find device: " << dev_name;
1040 return false;
1041#endif
1042}
1043
1044bool WebRtcVoiceEngine::GetOutputVolume(int* level) {
solenberg566ef242015-11-06 15:34:49 -08001045 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001046 unsigned int ulevel;
1047 if (voe_wrapper_->volume()->GetSpeakerVolume(ulevel) == -1) {
1048 LOG_RTCERR1(GetSpeakerVolume, level);
1049 return false;
1050 }
1051 *level = ulevel;
1052 return true;
1053}
1054
1055bool WebRtcVoiceEngine::SetOutputVolume(int level) {
solenberg566ef242015-11-06 15:34:49 -08001056 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
henrikg91d6ede2015-09-17 00:24:34 -07001057 RTC_DCHECK(level >= 0 && level <= 255);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001058 if (voe_wrapper_->volume()->SetSpeakerVolume(level) == -1) {
1059 LOG_RTCERR1(SetSpeakerVolume, level);
1060 return false;
1061 }
1062 return true;
1063}
1064
1065int WebRtcVoiceEngine::GetInputLevel() {
solenberg566ef242015-11-06 15:34:49 -08001066 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001067 unsigned int ulevel;
1068 return (voe_wrapper_->volume()->GetSpeechInputLevel(ulevel) != -1) ?
1069 static_cast<int>(ulevel) : -1;
1070}
1071
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001072const std::vector<AudioCodec>& WebRtcVoiceEngine::codecs() {
solenberg566ef242015-11-06 15:34:49 -08001073 RTC_DCHECK(signal_thread_checker_.CalledOnValidThread());
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001074 return codecs_;
1075}
1076
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001077const std::vector<RtpHeaderExtension>&
1078WebRtcVoiceEngine::rtp_header_extensions() const {
solenberg566ef242015-11-06 15:34:49 -08001079 RTC_DCHECK(signal_thread_checker_.CalledOnValidThread());
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001080 return rtp_header_extensions_;
1081}
1082
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001083int WebRtcVoiceEngine::GetLastEngineError() {
solenberg566ef242015-11-06 15:34:49 -08001084 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001085 return voe_wrapper_->error();
1086}
1087
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001088void WebRtcVoiceEngine::Print(webrtc::TraceLevel level, const char* trace,
1089 int length) {
solenberg566ef242015-11-06 15:34:49 -08001090 // Note: This callback can happen on any thread!
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00001091 rtc::LoggingSeverity sev = rtc::LS_VERBOSE;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001092 if (level == webrtc::kTraceError || level == webrtc::kTraceCritical)
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00001093 sev = rtc::LS_ERROR;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001094 else if (level == webrtc::kTraceWarning)
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00001095 sev = rtc::LS_WARNING;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001096 else if (level == webrtc::kTraceStateInfo || level == webrtc::kTraceInfo)
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00001097 sev = rtc::LS_INFO;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001098 else if (level == webrtc::kTraceTerseInfo)
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00001099 sev = rtc::LS_INFO;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001100
1101 // Skip past boilerplate prefix text
1102 if (length < 72) {
1103 std::string msg(trace, length);
1104 LOG(LS_ERROR) << "Malformed webrtc log message: ";
1105 LOG_V(sev) << msg;
1106 } else {
1107 std::string msg(trace + 71, length - 72);
Peter Boströmd5c75b12015-09-23 13:24:32 +02001108 LOG_V(sev) << "webrtc: " << msg;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001109 }
1110}
1111
solenberg63b34542015-09-29 06:06:31 -07001112void WebRtcVoiceEngine::RegisterChannel(WebRtcVoiceMediaChannel* channel) {
solenberg566ef242015-11-06 15:34:49 -08001113 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
1114 RTC_DCHECK(channel);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001115 channels_.push_back(channel);
1116}
1117
solenberg63b34542015-09-29 06:06:31 -07001118void WebRtcVoiceEngine::UnregisterChannel(WebRtcVoiceMediaChannel* channel) {
solenberg566ef242015-11-06 15:34:49 -08001119 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
solenberg63b34542015-09-29 06:06:31 -07001120 auto it = std::find(channels_.begin(), channels_.end(), channel);
solenberg566ef242015-11-06 15:34:49 -08001121 RTC_DCHECK(it != channels_.end());
1122 channels_.erase(it);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001123}
1124
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001125// Adjusts the default AGC target level by the specified delta.
1126// NB: If we start messing with other config fields, we'll want
1127// to save the current webrtc::AgcConfig as well.
1128bool WebRtcVoiceEngine::AdjustAgcLevel(int delta) {
solenberg566ef242015-11-06 15:34:49 -08001129 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001130 webrtc::AgcConfig config = default_agc_config_;
1131 config.targetLeveldBOv -= delta;
1132
1133 LOG(LS_INFO) << "Adjusting AGC level from default -"
1134 << default_agc_config_.targetLeveldBOv << "dB to -"
1135 << config.targetLeveldBOv << "dB";
1136
1137 if (voe_wrapper_->processing()->SetAgcConfig(config) == -1) {
1138 LOG_RTCERR1(SetAgcConfig, config.targetLeveldBOv);
1139 return false;
1140 }
1141 return true;
1142}
1143
Fredrik Solenbergccb49e72015-05-19 11:37:56 +02001144bool WebRtcVoiceEngine::SetAudioDeviceModule(webrtc::AudioDeviceModule* adm) {
solenberg566ef242015-11-06 15:34:49 -08001145 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001146 if (initialized_) {
1147 LOG(LS_WARNING) << "SetAudioDeviceModule can not be called after Init.";
1148 return false;
1149 }
1150 if (adm_) {
1151 adm_->Release();
1152 adm_ = NULL;
1153 }
1154 if (adm) {
1155 adm_ = adm;
1156 adm_->AddRef();
1157 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001158 return true;
1159}
1160
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00001161bool WebRtcVoiceEngine::StartAecDump(rtc::PlatformFile file) {
solenberg566ef242015-11-06 15:34:49 -08001162 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00001163 FILE* aec_dump_file_stream = rtc::FdopenPlatformFileForWriting(file);
wu@webrtc.orga8910d22014-01-23 22:12:45 +00001164 if (!aec_dump_file_stream) {
1165 LOG(LS_ERROR) << "Could not open AEC dump file stream.";
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00001166 if (!rtc::ClosePlatformFile(file))
wu@webrtc.orga8910d22014-01-23 22:12:45 +00001167 LOG(LS_WARNING) << "Could not close file.";
1168 return false;
1169 }
wu@webrtc.orga9890802013-12-13 00:21:03 +00001170 StopAecDump();
wu@webrtc.orga8910d22014-01-23 22:12:45 +00001171 if (voe_wrapper_->processing()->StartDebugRecording(aec_dump_file_stream) !=
wu@webrtc.orga9890802013-12-13 00:21:03 +00001172 webrtc::AudioProcessing::kNoError) {
wu@webrtc.orga8910d22014-01-23 22:12:45 +00001173 LOG_RTCERR0(StartDebugRecording);
1174 fclose(aec_dump_file_stream);
wu@webrtc.orga9890802013-12-13 00:21:03 +00001175 return false;
1176 }
1177 is_dumping_aec_ = true;
1178 return true;
wu@webrtc.orga9890802013-12-13 00:21:03 +00001179}
1180
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001181void WebRtcVoiceEngine::StartAecDump(const std::string& filename) {
solenberg566ef242015-11-06 15:34:49 -08001182 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001183 if (!is_dumping_aec_) {
1184 // Start dumping AEC when we are not dumping.
1185 if (voe_wrapper_->processing()->StartDebugRecording(
1186 filename.c_str()) != webrtc::AudioProcessing::kNoError) {
wu@webrtc.orga9890802013-12-13 00:21:03 +00001187 LOG_RTCERR1(StartDebugRecording, filename.c_str());
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001188 } else {
1189 is_dumping_aec_ = true;
1190 }
1191 }
1192}
1193
1194void WebRtcVoiceEngine::StopAecDump() {
solenberg566ef242015-11-06 15:34:49 -08001195 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001196 if (is_dumping_aec_) {
1197 // Stop dumping AEC when we are dumping.
1198 if (voe_wrapper_->processing()->StopDebugRecording() !=
1199 webrtc::AudioProcessing::kNoError) {
1200 LOG_RTCERR0(StopDebugRecording);
1201 }
1202 is_dumping_aec_ = false;
1203 }
1204}
1205
ivoc112a3d82015-10-16 02:22:18 -07001206bool WebRtcVoiceEngine::StartRtcEventLog(rtc::PlatformFile file) {
solenberg566ef242015-11-06 15:34:49 -08001207 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
ivoc112a3d82015-10-16 02:22:18 -07001208 return voe_wrapper_->codec()->GetEventLog()->StartLogging(file);
1209}
1210
1211void WebRtcVoiceEngine::StopRtcEventLog() {
solenberg566ef242015-11-06 15:34:49 -08001212 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
ivoc112a3d82015-10-16 02:22:18 -07001213 voe_wrapper_->codec()->GetEventLog()->StopLogging();
1214}
1215
solenberg0a617e22015-10-20 15:49:38 -07001216int WebRtcVoiceEngine::CreateVoEChannel() {
solenberg566ef242015-11-06 15:34:49 -08001217 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
solenberg0a617e22015-10-20 15:49:38 -07001218 return voe_wrapper_->base()->CreateChannel(voe_config_);
sergeyu@chromium.org5bc25c42013-12-05 00:24:06 +00001219}
1220
solenbergc96df772015-10-21 13:01:53 -07001221class WebRtcVoiceMediaChannel::WebRtcAudioSendStream
mallinath@webrtc.org67ee6b92014-02-03 16:57:16 +00001222 : public AudioRenderer::Sink {
1223 public:
solenbergc96df772015-10-21 13:01:53 -07001224 WebRtcAudioSendStream(int ch, webrtc::AudioTransport* voe_audio_transport,
solenberg3a941542015-11-16 07:34:50 -08001225 uint32_t ssrc, const std::string& c_name,
1226 const std::vector<webrtc::RtpExtension>& extensions,
1227 webrtc::Call* call)
solenberg7add0582015-11-20 09:59:34 -08001228 : voe_audio_transport_(voe_audio_transport),
solenberg3a941542015-11-16 07:34:50 -08001229 call_(call),
1230 config_(nullptr) {
solenberg85a04962015-10-27 03:35:21 -07001231 RTC_DCHECK_GE(ch, 0);
1232 // TODO(solenberg): Once we're not using FakeWebRtcVoiceEngine anymore:
1233 // RTC_DCHECK(voe_audio_transport);
solenbergc96df772015-10-21 13:01:53 -07001234 RTC_DCHECK(call);
solenberg85a04962015-10-27 03:35:21 -07001235 audio_capture_thread_checker_.DetachFromThread();
solenberg3a941542015-11-16 07:34:50 -08001236 config_.rtp.ssrc = ssrc;
1237 config_.rtp.c_name = c_name;
1238 config_.voe_channel_id = ch;
1239 RecreateAudioSendStream(extensions);
solenbergc96df772015-10-21 13:01:53 -07001240 }
solenberg3a941542015-11-16 07:34:50 -08001241
solenbergc96df772015-10-21 13:01:53 -07001242 ~WebRtcAudioSendStream() override {
solenberg566ef242015-11-06 15:34:49 -08001243 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
solenbergc96df772015-10-21 13:01:53 -07001244 Stop();
1245 call_->DestroyAudioSendStream(stream_);
1246 }
henrike@webrtc.org1e09a712013-07-26 19:17:59 +00001247
solenberg3a941542015-11-16 07:34:50 -08001248 void RecreateAudioSendStream(
1249 const std::vector<webrtc::RtpExtension>& extensions) {
1250 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
1251 if (stream_) {
1252 call_->DestroyAudioSendStream(stream_);
1253 stream_ = nullptr;
1254 }
1255 config_.rtp.extensions = extensions;
1256 RTC_DCHECK(!stream_);
1257 stream_ = call_->CreateAudioSendStream(config_);
1258 RTC_CHECK(stream_);
1259 }
1260
1261 webrtc::AudioSendStream::Stats GetStats() const {
1262 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
1263 RTC_DCHECK(stream_);
1264 return stream_->GetStats();
1265 }
1266
mallinath@webrtc.org67ee6b92014-02-03 16:57:16 +00001267 // Starts the rendering by setting a sink to the renderer to get data
1268 // callback.
henrike@webrtc.orga7b98182014-02-21 15:51:43 +00001269 // This method is called on the libjingle worker thread.
mallinath@webrtc.org67ee6b92014-02-03 16:57:16 +00001270 // TODO(xians): Make sure Start() is called only once.
1271 void Start(AudioRenderer* renderer) {
solenberg566ef242015-11-06 15:34:49 -08001272 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
solenbergc96df772015-10-21 13:01:53 -07001273 RTC_DCHECK(renderer);
1274 if (renderer_) {
henrikg91d6ede2015-09-17 00:24:34 -07001275 RTC_DCHECK(renderer_ == renderer);
mallinath@webrtc.org67ee6b92014-02-03 16:57:16 +00001276 return;
1277 }
mallinath@webrtc.org67ee6b92014-02-03 16:57:16 +00001278 renderer->SetSink(this);
1279 renderer_ = renderer;
1280 }
1281
solenbergc96df772015-10-21 13:01:53 -07001282 // Stops rendering by setting the sink of the renderer to nullptr. No data
mallinath@webrtc.org67ee6b92014-02-03 16:57:16 +00001283 // callback will be received after this method.
henrike@webrtc.orga7b98182014-02-21 15:51:43 +00001284 // This method is called on the libjingle worker thread.
mallinath@webrtc.org67ee6b92014-02-03 16:57:16 +00001285 void Stop() {
solenberg566ef242015-11-06 15:34:49 -08001286 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
solenbergc96df772015-10-21 13:01:53 -07001287 if (renderer_) {
1288 renderer_->SetSink(nullptr);
1289 renderer_ = nullptr;
solenberg98c68862015-10-09 03:27:14 -07001290 }
mallinath@webrtc.org67ee6b92014-02-03 16:57:16 +00001291 }
1292
1293 // AudioRenderer::Sink implementation.
henrike@webrtc.orga7b98182014-02-21 15:51:43 +00001294 // This method is called on the audio thread.
kjellander@webrtc.org14665ff2015-03-04 12:58:35 +00001295 void OnData(const void* audio_data,
1296 int bits_per_sample,
1297 int sample_rate,
1298 int number_of_channels,
Peter Kastingdce40cf2015-08-24 14:52:23 -07001299 size_t number_of_frames) override {
solenberg566ef242015-11-06 15:34:49 -08001300 RTC_DCHECK(!worker_thread_checker_.CalledOnValidThread());
solenberg85a04962015-10-27 03:35:21 -07001301 RTC_DCHECK(audio_capture_thread_checker_.CalledOnValidThread());
solenbergc96df772015-10-21 13:01:53 -07001302 RTC_DCHECK(voe_audio_transport_);
solenberg7add0582015-11-20 09:59:34 -08001303 voe_audio_transport_->OnData(config_.voe_channel_id,
henrike@webrtc.orga7b98182014-02-21 15:51:43 +00001304 audio_data,
1305 bits_per_sample,
1306 sample_rate,
1307 number_of_channels,
1308 number_of_frames);
henrike@webrtc.orga7b98182014-02-21 15:51:43 +00001309 }
1310
1311 // Callback from the |renderer_| when it is going away. In case Start() has
1312 // never been called, this callback won't be triggered.
kjellander@webrtc.org14665ff2015-03-04 12:58:35 +00001313 void OnClose() override {
solenberg566ef242015-11-06 15:34:49 -08001314 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
solenbergc96df772015-10-21 13:01:53 -07001315 // Set |renderer_| to nullptr to make sure no more callback will get into
henrike@webrtc.orga7b98182014-02-21 15:51:43 +00001316 // the renderer.
solenbergc96df772015-10-21 13:01:53 -07001317 renderer_ = nullptr;
mallinath@webrtc.org67ee6b92014-02-03 16:57:16 +00001318 }
1319
1320 // Accessor to the VoE channel ID.
solenberg85a04962015-10-27 03:35:21 -07001321 int channel() const {
solenberg566ef242015-11-06 15:34:49 -08001322 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
solenberg7add0582015-11-20 09:59:34 -08001323 return config_.voe_channel_id;
solenberg85a04962015-10-27 03:35:21 -07001324 }
mallinath@webrtc.org67ee6b92014-02-03 16:57:16 +00001325
1326 private:
solenberg566ef242015-11-06 15:34:49 -08001327 rtc::ThreadChecker worker_thread_checker_;
solenberg85a04962015-10-27 03:35:21 -07001328 rtc::ThreadChecker audio_capture_thread_checker_;
solenbergc96df772015-10-21 13:01:53 -07001329 webrtc::AudioTransport* const voe_audio_transport_ = nullptr;
1330 webrtc::Call* call_ = nullptr;
solenberg3a941542015-11-16 07:34:50 -08001331 webrtc::AudioSendStream::Config config_;
1332 // The stream is owned by WebRtcAudioSendStream and may be reallocated if
1333 // configuration changes.
solenbergc96df772015-10-21 13:01:53 -07001334 webrtc::AudioSendStream* stream_ = nullptr;
mallinath@webrtc.org67ee6b92014-02-03 16:57:16 +00001335
1336 // Raw pointer to AudioRenderer owned by LocalAudioTrackHandler.
1337 // PeerConnection will make sure invalidating the pointer before the object
1338 // goes away.
solenbergc96df772015-10-21 13:01:53 -07001339 AudioRenderer* renderer_ = nullptr;
henrike@webrtc.orga7b98182014-02-21 15:51:43 +00001340
solenbergc96df772015-10-21 13:01:53 -07001341 RTC_DISALLOW_IMPLICIT_CONSTRUCTORS(WebRtcAudioSendStream);
1342};
1343
1344class WebRtcVoiceMediaChannel::WebRtcAudioReceiveStream {
1345 public:
solenberg7add0582015-11-20 09:59:34 -08001346 WebRtcAudioReceiveStream(int ch, uint32_t remote_ssrc, uint32_t local_ssrc,
1347 bool use_combined_bwe, const std::string& sync_group,
1348 const std::vector<webrtc::RtpExtension>& extensions,
1349 webrtc::Call* call)
1350 : call_(call),
1351 config_() {
1352 RTC_DCHECK_GE(ch, 0);
1353 RTC_DCHECK(call);
1354 config_.rtp.remote_ssrc = remote_ssrc;
1355 config_.rtp.local_ssrc = local_ssrc;
1356 config_.voe_channel_id = ch;
1357 config_.sync_group = sync_group;
1358 RecreateAudioReceiveStream(use_combined_bwe, extensions);
1359 }
solenbergc96df772015-10-21 13:01:53 -07001360
solenberg7add0582015-11-20 09:59:34 -08001361 ~WebRtcAudioReceiveStream() {
1362 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
1363 call_->DestroyAudioReceiveStream(stream_);
1364 }
1365
1366 void RecreateAudioReceiveStream(
1367 const std::vector<webrtc::RtpExtension>& extensions) {
1368 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
1369 RecreateAudioReceiveStream(config_.combined_audio_video_bwe, extensions);
1370 }
1371 void RecreateAudioReceiveStream(bool use_combined_bwe) {
1372 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
1373 RecreateAudioReceiveStream(use_combined_bwe, config_.rtp.extensions);
1374 }
1375
1376 webrtc::AudioReceiveStream::Stats GetStats() const {
1377 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
1378 RTC_DCHECK(stream_);
1379 return stream_->GetStats();
1380 }
1381
1382 int channel() const {
1383 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
1384 return config_.voe_channel_id;
1385 }
solenbergc96df772015-10-21 13:01:53 -07001386
1387 private:
solenberg7add0582015-11-20 09:59:34 -08001388 void RecreateAudioReceiveStream(bool use_combined_bwe,
1389 const std::vector<webrtc::RtpExtension>& extensions) {
1390 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
1391 if (stream_) {
1392 call_->DestroyAudioReceiveStream(stream_);
1393 stream_ = nullptr;
1394 }
1395 config_.rtp.extensions = extensions;
1396 config_.combined_audio_video_bwe = use_combined_bwe;
1397 RTC_DCHECK(!stream_);
1398 stream_ = call_->CreateAudioReceiveStream(config_);
1399 RTC_CHECK(stream_);
1400 }
1401
1402 rtc::ThreadChecker worker_thread_checker_;
1403 webrtc::Call* call_ = nullptr;
1404 webrtc::AudioReceiveStream::Config config_;
1405 // The stream is owned by WebRtcAudioReceiveStream and may be reallocated if
1406 // configuration changes.
1407 webrtc::AudioReceiveStream* stream_ = nullptr;
solenbergc96df772015-10-21 13:01:53 -07001408
1409 RTC_DISALLOW_IMPLICIT_CONSTRUCTORS(WebRtcAudioReceiveStream);
henrike@webrtc.org1e09a712013-07-26 19:17:59 +00001410};
1411
Fredrik Solenberg709ed672015-09-15 12:26:33 +02001412WebRtcVoiceMediaChannel::WebRtcVoiceMediaChannel(WebRtcVoiceEngine* engine,
Fredrik Solenbergb071a192015-09-17 16:42:56 +02001413 const AudioOptions& options,
Fredrik Solenberg709ed672015-09-15 12:26:33 +02001414 webrtc::Call* call)
solenberg566ef242015-11-06 15:34:49 -08001415 : engine_(engine), call_(call) {
solenberg0a617e22015-10-20 15:49:38 -07001416 LOG(LS_VERBOSE) << "WebRtcVoiceMediaChannel::WebRtcVoiceMediaChannel";
solenberg566ef242015-11-06 15:34:49 -08001417 RTC_DCHECK(call);
solenberg0a617e22015-10-20 15:49:38 -07001418 engine->RegisterChannel(this);
Fredrik Solenbergb071a192015-09-17 16:42:56 +02001419 SetOptions(options);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001420}
1421
1422WebRtcVoiceMediaChannel::~WebRtcVoiceMediaChannel() {
solenberg566ef242015-11-06 15:34:49 -08001423 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
solenberg0a617e22015-10-20 15:49:38 -07001424 LOG(LS_VERBOSE) << "WebRtcVoiceMediaChannel::~WebRtcVoiceMediaChannel";
solenberg7add0582015-11-20 09:59:34 -08001425 // TODO(solenberg): Should be able to delete the streams directly, without
1426 // going through RemoveNnStream(), once stream objects handle
1427 // all (de)configuration.
solenbergc96df772015-10-21 13:01:53 -07001428 while (!send_streams_.empty()) {
1429 RemoveSendStream(send_streams_.begin()->first);
solenbergd97ec302015-10-07 01:40:33 -07001430 }
solenberg7add0582015-11-20 09:59:34 -08001431 while (!recv_streams_.empty()) {
1432 RemoveRecvStream(recv_streams_.begin()->first);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001433 }
solenberg0a617e22015-10-20 15:49:38 -07001434 engine()->UnregisterChannel(this);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001435}
1436
Peter Thatcherc2ee2c82015-08-07 16:05:34 -07001437bool WebRtcVoiceMediaChannel::SetSendParameters(
1438 const AudioSendParameters& params) {
solenberg566ef242015-11-06 15:34:49 -08001439 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
solenberg7e4e01a2015-12-02 08:05:01 -08001440 LOG(LS_INFO) << "WebRtcVoiceMediaChannel::SetSendParameters: "
1441 << params.ToString();
Peter Thatcherc2ee2c82015-08-07 16:05:34 -07001442 // TODO(pthatcher): Refactor this to be more clean now that we have
1443 // all the information at once.
solenberg3a941542015-11-16 07:34:50 -08001444
1445 if (!SetSendCodecs(params.codecs)) {
1446 return false;
1447 }
1448
solenberg7e4e01a2015-12-02 08:05:01 -08001449 if (!ValidateRtpExtensions(params.extensions)) {
1450 return false;
1451 }
1452 std::vector<webrtc::RtpExtension> filtered_extensions =
1453 FilterRtpExtensions(params.extensions,
1454 webrtc::RtpExtension::IsSupportedForAudio, true);
1455 if (send_rtp_extensions_ != filtered_extensions) {
1456 send_rtp_extensions_.swap(filtered_extensions);
solenberg3a941542015-11-16 07:34:50 -08001457 for (auto& it : send_streams_) {
1458 it.second->RecreateAudioSendStream(send_rtp_extensions_);
1459 }
1460 }
1461
1462 if (!SetMaxSendBandwidth(params.max_bandwidth_bps)) {
1463 return false;
1464 }
1465 return SetOptions(params.options);
Peter Thatcherc2ee2c82015-08-07 16:05:34 -07001466}
1467
1468bool WebRtcVoiceMediaChannel::SetRecvParameters(
1469 const AudioRecvParameters& params) {
solenberg566ef242015-11-06 15:34:49 -08001470 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
solenberg7e4e01a2015-12-02 08:05:01 -08001471 LOG(LS_INFO) << "WebRtcVoiceMediaChannel::SetRecvParameters: "
1472 << params.ToString();
Peter Thatcherc2ee2c82015-08-07 16:05:34 -07001473 // TODO(pthatcher): Refactor this to be more clean now that we have
1474 // all the information at once.
solenberg7add0582015-11-20 09:59:34 -08001475
1476 if (!SetRecvCodecs(params.codecs)) {
1477 return false;
1478 }
1479
solenberg7e4e01a2015-12-02 08:05:01 -08001480 if (!ValidateRtpExtensions(params.extensions)) {
1481 return false;
1482 }
1483 std::vector<webrtc::RtpExtension> filtered_extensions =
1484 FilterRtpExtensions(params.extensions,
1485 webrtc::RtpExtension::IsSupportedForAudio, false);
1486 if (recv_rtp_extensions_ != filtered_extensions) {
1487 recv_rtp_extensions_.swap(filtered_extensions);
solenberg7add0582015-11-20 09:59:34 -08001488 for (auto& it : recv_streams_) {
1489 it.second->RecreateAudioReceiveStream(recv_rtp_extensions_);
1490 }
1491 }
1492
1493 return true;
Peter Thatcherc2ee2c82015-08-07 16:05:34 -07001494}
1495
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001496bool WebRtcVoiceMediaChannel::SetOptions(const AudioOptions& options) {
solenberg566ef242015-11-06 15:34:49 -08001497 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001498 LOG(LS_INFO) << "Setting voice channel options: "
1499 << options.ToString();
1500
wu@webrtc.orgde305012013-10-31 15:40:38 +00001501 // Check if DSCP value is changed from previous.
1502 bool dscp_option_changed = (options_.dscp != options.dscp);
1503
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001504 // We retain all of the existing options, and apply the given ones
1505 // on top. This means there is no way to "clear" options such that
1506 // they go back to the engine default.
1507 options_.SetAll(options);
1508
1509 if (send_ != SEND_NOTHING) {
solenberg63b34542015-09-29 06:06:31 -07001510 if (!engine()->ApplyOptions(options_)) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001511 LOG(LS_WARNING) <<
solenberg63b34542015-09-29 06:06:31 -07001512 "Failed to apply engine options during channel SetOptions.";
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001513 return false;
1514 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001515 }
1516
wu@webrtc.orgde305012013-10-31 15:40:38 +00001517 if (dscp_option_changed) {
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00001518 rtc::DiffServCodePoint dscp = rtc::DSCP_DEFAULT;
kwiberg102c6a62015-10-30 02:47:38 -07001519 if (options_.dscp.value_or(false))
wu@webrtc.orgde305012013-10-31 15:40:38 +00001520 dscp = kAudioDscpValue;
1521 if (MediaChannel::SetDscp(dscp) != 0) {
1522 LOG(LS_WARNING) << "Failed to set DSCP settings for audio channel";
1523 }
1524 }
solenberg8fb30c32015-10-13 03:06:58 -07001525
solenbergc96df772015-10-21 13:01:53 -07001526 // TODO(solenberg): Don't recreate unless options changed.
solenberg7add0582015-11-20 09:59:34 -08001527 for (auto& it : recv_streams_) {
1528 it.second->RecreateAudioReceiveStream(
1529 options_.combined_audio_video_bwe.value_or(false));
1530 }
solenberg8fb30c32015-10-13 03:06:58 -07001531
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001532 LOG(LS_INFO) << "Set voice channel options. Current options: "
1533 << options_.ToString();
1534 return true;
1535}
1536
1537bool WebRtcVoiceMediaChannel::SetRecvCodecs(
1538 const std::vector<AudioCodec>& codecs) {
solenberg566ef242015-11-06 15:34:49 -08001539 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
solenberg8fb30c32015-10-13 03:06:58 -07001540
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001541 // Set the payload types to be used for incoming media.
solenberg0b675462015-10-09 01:37:09 -07001542 LOG(LS_INFO) << "Setting receive voice codecs.";
solenberg0b675462015-10-09 01:37:09 -07001543
1544 if (!VerifyUniquePayloadTypes(codecs)) {
1545 LOG(LS_ERROR) << "Codec payload types overlap.";
1546 return false;
1547 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001548
1549 std::vector<AudioCodec> new_codecs;
1550 // Find all new codecs. We allow adding new codecs but don't allow changing
1551 // the payload type of codecs that is already configured since we might
1552 // already be receiving packets with that payload type.
Fredrik Solenbergaf9fb212015-08-26 10:45:53 +02001553 for (const AudioCodec& codec : codecs) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001554 AudioCodec old_codec;
Fredrik Solenbergaf9fb212015-08-26 10:45:53 +02001555 if (FindCodec(recv_codecs_, codec, &old_codec)) {
1556 if (old_codec.id != codec.id) {
1557 LOG(LS_ERROR) << codec.name << " payload type changed.";
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001558 return false;
1559 }
1560 } else {
Fredrik Solenbergaf9fb212015-08-26 10:45:53 +02001561 new_codecs.push_back(codec);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001562 }
1563 }
1564 if (new_codecs.empty()) {
1565 // There are no new codecs to configure. Already configured codecs are
1566 // never removed.
1567 return true;
1568 }
1569
1570 if (playout_) {
1571 // Receive codecs can not be changed while playing. So we temporarily
1572 // pause playout.
1573 PausePlayout();
1574 }
1575
solenberg26c8c912015-11-27 04:00:25 -08001576 bool result = true;
1577 for (const AudioCodec& codec : new_codecs) {
1578 webrtc::CodecInst voe_codec;
1579 if (WebRtcVoiceEngine::ToCodecInst(codec, &voe_codec)) {
1580 LOG(LS_INFO) << ToString(codec);
1581 voe_codec.pltype = codec.id;
1582 for (const auto& ch : recv_streams_) {
1583 if (engine()->voe()->codec()->SetRecPayloadType(
1584 ch.second->channel(), voe_codec) == -1) {
1585 LOG_RTCERR2(SetRecPayloadType, ch.second->channel(),
1586 ToString(voe_codec));
1587 result = false;
1588 }
1589 }
1590 } else {
1591 LOG(LS_WARNING) << "Unknown codec " << ToString(codec);
1592 result = false;
1593 break;
1594 }
1595 }
Fredrik Solenbergaf9fb212015-08-26 10:45:53 +02001596 if (result) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001597 recv_codecs_ = codecs;
1598 }
1599
1600 if (desired_playout_ && !playout_) {
1601 ResumePlayout();
1602 }
Fredrik Solenbergaf9fb212015-08-26 10:45:53 +02001603 return result;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001604}
1605
1606bool WebRtcVoiceMediaChannel::SetSendCodecs(
wu@webrtc.orgcadf9042013-08-30 21:24:16 +00001607 int channel, const std::vector<AudioCodec>& codecs) {
buildbot@webrtc.orgae740dd2014-06-17 10:56:41 +00001608 // Disable VAD, FEC, and RED unless we know the other side wants them.
wu@webrtc.orgcadf9042013-08-30 21:24:16 +00001609 engine()->voe()->codec()->SetVADStatus(channel, false);
1610 engine()->voe()->rtp()->SetNACKStatus(channel, false, 0);
buildbot@webrtc.orgae740dd2014-06-17 10:56:41 +00001611 engine()->voe()->rtp()->SetREDStatus(channel, false);
1612 engine()->voe()->codec()->SetFECStatus(channel, false);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001613
1614 // Scan through the list to figure out the codec to use for sending, along
1615 // with the proper configuration for VAD and DTMF.
henrike@webrtc.org704bf9e2014-02-27 17:52:04 +00001616 bool found_send_codec = false;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001617 webrtc::CodecInst send_codec;
1618 memset(&send_codec, 0, sizeof(send_codec));
1619
wu@webrtc.org05e7b442014-04-01 17:44:24 +00001620 bool nack_enabled = nack_enabled_;
buildbot@webrtc.org3ffa1f92014-07-02 19:51:26 +00001621 bool enable_codec_fec = false;
Minyue Li7100dcd2015-03-27 05:05:59 +01001622 bool enable_opus_dtx = false;
minyue@webrtc.org26236952014-10-29 02:27:08 +00001623 int opus_max_playback_rate = 0;
buildbot@webrtc.org5d639b32014-09-10 07:57:12 +00001624
henrike@webrtc.org704bf9e2014-02-27 17:52:04 +00001625 // Set send codec (the first non-telephone-event/CN codec)
Fredrik Solenbergaf9fb212015-08-26 10:45:53 +02001626 for (const AudioCodec& codec : codecs) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001627 // Ignore codecs we don't know about. The negotiation step should prevent
1628 // this, but double-check to be sure.
1629 webrtc::CodecInst voe_codec;
solenberg26c8c912015-11-27 04:00:25 -08001630 if (!WebRtcVoiceEngine::ToCodecInst(codec, &voe_codec)) {
Fredrik Solenbergaf9fb212015-08-26 10:45:53 +02001631 LOG(LS_WARNING) << "Unknown codec " << ToString(codec);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001632 continue;
1633 }
1634
Fredrik Solenbergaf9fb212015-08-26 10:45:53 +02001635 if (IsCodec(codec, kDtmfCodecName) || IsCodec(codec, kCnCodecName)) {
henrike@webrtc.org704bf9e2014-02-27 17:52:04 +00001636 // Skip telephone-event/CN codec, which will be handled later.
1637 continue;
1638 }
1639
henrike@webrtc.org704bf9e2014-02-27 17:52:04 +00001640 // We'll use the first codec in the list to actually send audio data.
1641 // Be sure to use the payload type requested by the remote side.
buildbot@webrtc.orgae740dd2014-06-17 10:56:41 +00001642 // "red", for RED audio, is a special case where the actual codec to be
henrike@webrtc.org704bf9e2014-02-27 17:52:04 +00001643 // used is specified in params.
Fredrik Solenbergaf9fb212015-08-26 10:45:53 +02001644 if (IsCodec(codec, kRedCodecName)) {
henrike@webrtc.org704bf9e2014-02-27 17:52:04 +00001645 // Parse out the RED parameters. If we fail, just ignore RED;
1646 // we don't support all possible params/usage scenarios.
Fredrik Solenbergaf9fb212015-08-26 10:45:53 +02001647 if (!GetRedSendCodec(codec, codecs, &send_codec)) {
henrike@webrtc.org704bf9e2014-02-27 17:52:04 +00001648 continue;
1649 }
1650
1651 // Enable redundant encoding of the specified codec. Treat any
1652 // failure as a fatal internal error.
buildbot@webrtc.orgae740dd2014-06-17 10:56:41 +00001653 LOG(LS_INFO) << "Enabling RED on channel " << channel;
Fredrik Solenbergaf9fb212015-08-26 10:45:53 +02001654 if (engine()->voe()->rtp()->SetREDStatus(channel, true, codec.id) == -1) {
1655 LOG_RTCERR3(SetREDStatus, channel, true, codec.id);
henrike@webrtc.org704bf9e2014-02-27 17:52:04 +00001656 return false;
1657 }
1658 } else {
1659 send_codec = voe_codec;
Fredrik Solenbergaf9fb212015-08-26 10:45:53 +02001660 nack_enabled = IsNackEnabled(codec);
Minyue Li7100dcd2015-03-27 05:05:59 +01001661 // For Opus as the send codec, we are to determine inband FEC, maximum
1662 // playback rate, and opus internal dtx.
Fredrik Solenbergaf9fb212015-08-26 10:45:53 +02001663 if (IsCodec(codec, kOpusCodecName)) {
1664 GetOpusConfig(codec, &send_codec, &enable_codec_fec,
Minyue Li7100dcd2015-03-27 05:05:59 +01001665 &opus_max_playback_rate, &enable_opus_dtx);
buildbot@webrtc.org5d639b32014-09-10 07:57:12 +00001666 }
Brave Yao5225dd82015-03-26 07:39:19 +08001667
1668 // Set packet size if the AudioCodec param kCodecParamPTime is set.
1669 int ptime_ms = 0;
Fredrik Solenbergaf9fb212015-08-26 10:45:53 +02001670 if (codec.GetParam(kCodecParamPTime, &ptime_ms)) {
solenberg26c8c912015-11-27 04:00:25 -08001671 if (!WebRtcVoiceCodecs::SetPTimeAsPacketSize(&send_codec, ptime_ms)) {
Brave Yao5225dd82015-03-26 07:39:19 +08001672 LOG(LS_WARNING) << "Failed to set packet size for codec "
1673 << send_codec.plname;
1674 return false;
1675 }
1676 }
henrike@webrtc.org704bf9e2014-02-27 17:52:04 +00001677 }
1678 found_send_codec = true;
1679 break;
1680 }
1681
wu@webrtc.org05e7b442014-04-01 17:44:24 +00001682 if (nack_enabled_ != nack_enabled) {
1683 SetNack(channel, nack_enabled);
1684 nack_enabled_ = nack_enabled;
1685 }
1686
henrike@webrtc.org704bf9e2014-02-27 17:52:04 +00001687 if (!found_send_codec) {
1688 LOG(LS_WARNING) << "Received empty list of codecs.";
1689 return false;
1690 }
1691
1692 // Set the codec immediately, since SetVADStatus() depends on whether
1693 // the current codec is mono or stereo.
1694 if (!SetSendCodec(channel, send_codec))
1695 return false;
1696
buildbot@webrtc.org3ffa1f92014-07-02 19:51:26 +00001697 // FEC should be enabled after SetSendCodec.
1698 if (enable_codec_fec) {
1699 LOG(LS_INFO) << "Attempt to enable codec internal FEC on channel "
1700 << channel;
buildbot@webrtc.org3ffa1f92014-07-02 19:51:26 +00001701 if (engine()->voe()->codec()->SetFECStatus(channel, true) == -1) {
1702 // Enable codec internal FEC. Treat any failure as fatal internal error.
1703 LOG_RTCERR2(SetFECStatus, channel, true);
1704 return false;
1705 }
buildbot@webrtc.org3ffa1f92014-07-02 19:51:26 +00001706 }
1707
Minyue Li7100dcd2015-03-27 05:05:59 +01001708 if (IsCodec(send_codec, kOpusCodecName)) {
1709 // DTX and maxplaybackrate should be set after SetSendCodec. Because current
1710 // send codec has to be Opus.
1711
1712 // Set Opus internal DTX.
1713 LOG(LS_INFO) << "Attempt to "
solenbergbd138382015-11-20 16:08:07 -08001714 << (enable_opus_dtx ? "enable" : "disable")
Minyue Li7100dcd2015-03-27 05:05:59 +01001715 << " Opus DTX on channel "
buildbot@webrtc.org5d639b32014-09-10 07:57:12 +00001716 << channel;
Minyue Li7100dcd2015-03-27 05:05:59 +01001717 if (engine()->voe()->codec()->SetOpusDtx(channel, enable_opus_dtx)) {
1718 LOG_RTCERR2(SetOpusDtx, channel, enable_opus_dtx);
1719 return false;
1720 }
1721
1722 // If opus_max_playback_rate <= 0, the default maximum playback rate
1723 // (48 kHz) will be used.
1724 if (opus_max_playback_rate > 0) {
1725 LOG(LS_INFO) << "Attempt to set maximum playback rate to "
1726 << opus_max_playback_rate
1727 << " Hz on channel "
1728 << channel;
1729 if (engine()->voe()->codec()->SetOpusMaxPlaybackRate(
1730 channel, opus_max_playback_rate) == -1) {
1731 LOG_RTCERR2(SetOpusMaxPlaybackRate, channel, opus_max_playback_rate);
1732 return false;
1733 }
buildbot@webrtc.org5d639b32014-09-10 07:57:12 +00001734 }
buildbot@webrtc.org5d639b32014-09-10 07:57:12 +00001735 }
1736
henrike@webrtc.org704bf9e2014-02-27 17:52:04 +00001737 // Always update the |send_codec_| to the currently set send codec.
1738 send_codec_.reset(new webrtc::CodecInst(send_codec));
1739
minyue@webrtc.org26236952014-10-29 02:27:08 +00001740 if (send_bitrate_setting_) {
1741 SetSendBitrateInternal(send_bitrate_bps_);
henrike@webrtc.org704bf9e2014-02-27 17:52:04 +00001742 }
1743
1744 // Loop through the codecs list again to config the telephone-event/CN codec.
Fredrik Solenbergaf9fb212015-08-26 10:45:53 +02001745 for (const AudioCodec& codec : codecs) {
henrike@webrtc.org704bf9e2014-02-27 17:52:04 +00001746 // Ignore codecs we don't know about. The negotiation step should prevent
1747 // this, but double-check to be sure.
1748 webrtc::CodecInst voe_codec;
solenberg26c8c912015-11-27 04:00:25 -08001749 if (!WebRtcVoiceEngine::ToCodecInst(codec, &voe_codec)) {
Fredrik Solenbergaf9fb212015-08-26 10:45:53 +02001750 LOG(LS_WARNING) << "Unknown codec " << ToString(codec);
henrike@webrtc.org704bf9e2014-02-27 17:52:04 +00001751 continue;
1752 }
1753
wu@webrtc.org9dba5252013-08-05 20:36:57 +00001754 // Find the DTMF telephone event "codec" and tell VoiceEngine channels
1755 // about it.
Fredrik Solenbergaf9fb212015-08-26 10:45:53 +02001756 if (IsCodec(codec, kDtmfCodecName)) {
wu@webrtc.orgcadf9042013-08-30 21:24:16 +00001757 if (engine()->voe()->dtmf()->SetSendTelephoneEventPayloadType(
Fredrik Solenbergaf9fb212015-08-26 10:45:53 +02001758 channel, codec.id) == -1) {
1759 LOG_RTCERR2(SetSendTelephoneEventPayloadType, channel, codec.id);
wu@webrtc.orgcadf9042013-08-30 21:24:16 +00001760 return false;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001761 }
Fredrik Solenbergaf9fb212015-08-26 10:45:53 +02001762 } else if (IsCodec(codec, kCnCodecName)) {
henrike@webrtc.org704bf9e2014-02-27 17:52:04 +00001763 // Turn voice activity detection/comfort noise on if supported.
1764 // Set the wideband CN payload type appropriately.
1765 // (narrowband always uses the static payload type 13).
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001766 webrtc::PayloadFrequencies cn_freq;
Fredrik Solenbergaf9fb212015-08-26 10:45:53 +02001767 switch (codec.clockrate) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001768 case 8000:
1769 cn_freq = webrtc::kFreq8000Hz;
1770 break;
1771 case 16000:
1772 cn_freq = webrtc::kFreq16000Hz;
1773 break;
1774 case 32000:
1775 cn_freq = webrtc::kFreq32000Hz;
1776 break;
1777 default:
Fredrik Solenbergaf9fb212015-08-26 10:45:53 +02001778 LOG(LS_WARNING) << "CN frequency " << codec.clockrate
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001779 << " not supported.";
1780 continue;
1781 }
wu@webrtc.orgcadf9042013-08-30 21:24:16 +00001782 // Set the CN payloadtype and the VAD status.
1783 // The CN payload type for 8000 Hz clockrate is fixed at 13.
1784 if (cn_freq != webrtc::kFreq8000Hz) {
1785 if (engine()->voe()->codec()->SetSendCNPayloadType(
Fredrik Solenbergaf9fb212015-08-26 10:45:53 +02001786 channel, codec.id, cn_freq) == -1) {
1787 LOG_RTCERR3(SetSendCNPayloadType, channel, codec.id, cn_freq);
wu@webrtc.orgcadf9042013-08-30 21:24:16 +00001788 // TODO(ajm): This failure condition will be removed from VoE.
1789 // Restore the return here when we update to a new enough webrtc.
1790 //
1791 // Not returning false because the SetSendCNPayloadType will fail if
1792 // the channel is already sending.
1793 // This can happen if the remote description is applied twice, for
1794 // example in the case of ROAP on top of JSEP, where both side will
1795 // send the offer.
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001796 }
wu@webrtc.orgcadf9042013-08-30 21:24:16 +00001797 }
wu@webrtc.orgcadf9042013-08-30 21:24:16 +00001798 // Only turn on VAD if we have a CN payload type that matches the
1799 // clockrate for the codec we are going to use.
Fredrik Solenbergaf9fb212015-08-26 10:45:53 +02001800 if (codec.clockrate == send_codec.plfreq && send_codec.channels != 2) {
Minyue Li7100dcd2015-03-27 05:05:59 +01001801 // TODO(minyue): If CN frequency == 48000 Hz is allowed, consider the
1802 // interaction between VAD and Opus FEC.
wu@webrtc.orgcadf9042013-08-30 21:24:16 +00001803 LOG(LS_INFO) << "Enabling VAD";
1804 if (engine()->voe()->codec()->SetVADStatus(channel, true) == -1) {
1805 LOG_RTCERR2(SetVADStatus, channel, true);
1806 return false;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001807 }
1808 }
1809 }
wu@webrtc.org1d1ffc92013-10-16 18:12:02 +00001810 }
wu@webrtc.orgcadf9042013-08-30 21:24:16 +00001811 return true;
1812}
1813
1814bool WebRtcVoiceMediaChannel::SetSendCodecs(
1815 const std::vector<AudioCodec>& codecs) {
solenberg566ef242015-11-06 15:34:49 -08001816 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
solenbergd97ec302015-10-07 01:40:33 -07001817
wu@webrtc.orgcadf9042013-08-30 21:24:16 +00001818 dtmf_allowed_ = false;
Fredrik Solenbergaf9fb212015-08-26 10:45:53 +02001819 for (const AudioCodec& codec : codecs) {
wu@webrtc.orgcadf9042013-08-30 21:24:16 +00001820 // Find the DTMF telephone event "codec".
Fredrik Solenbergaf9fb212015-08-26 10:45:53 +02001821 if (IsCodec(codec, kDtmfCodecName)) {
wu@webrtc.orgcadf9042013-08-30 21:24:16 +00001822 dtmf_allowed_ = true;
1823 }
1824 }
1825
1826 // Cache the codecs in order to configure the channel created later.
1827 send_codecs_ = codecs;
solenbergc96df772015-10-21 13:01:53 -07001828 for (const auto& ch : send_streams_) {
Fredrik Solenbergaf9fb212015-08-26 10:45:53 +02001829 if (!SetSendCodecs(ch.second->channel(), codecs)) {
wu@webrtc.orgcadf9042013-08-30 21:24:16 +00001830 return false;
1831 }
1832 }
1833
wu@webrtc.org05e7b442014-04-01 17:44:24 +00001834 // Set nack status on receive channels and update |nack_enabled_|.
solenberg7add0582015-11-20 09:59:34 -08001835 for (const auto& ch : recv_streams_) {
solenberg0a617e22015-10-20 15:49:38 -07001836 SetNack(ch.second->channel(), nack_enabled_);
wu@webrtc.org9dba5252013-08-05 20:36:57 +00001837 }
solenberg0a617e22015-10-20 15:49:38 -07001838
1839 return true;
wu@webrtc.org9dba5252013-08-05 20:36:57 +00001840}
1841
wu@webrtc.orgcadf9042013-08-30 21:24:16 +00001842void WebRtcVoiceMediaChannel::SetNack(int channel, bool nack_enabled) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001843 if (nack_enabled) {
wu@webrtc.orgcadf9042013-08-30 21:24:16 +00001844 LOG(LS_INFO) << "Enabling NACK for channel " << channel;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001845 engine()->voe()->rtp()->SetNACKStatus(channel, true, kNackMaxPackets);
1846 } else {
wu@webrtc.orgcadf9042013-08-30 21:24:16 +00001847 LOG(LS_INFO) << "Disabling NACK for channel " << channel;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001848 engine()->voe()->rtp()->SetNACKStatus(channel, false, 0);
1849 }
1850}
1851
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001852bool WebRtcVoiceMediaChannel::SetSendCodec(
wu@webrtc.org9dba5252013-08-05 20:36:57 +00001853 int channel, const webrtc::CodecInst& send_codec) {
1854 LOG(LS_INFO) << "Send channel " << channel << " selected voice codec "
1855 << ToString(send_codec) << ", bitrate=" << send_codec.rate;
1856
wu@webrtc.org05e7b442014-04-01 17:44:24 +00001857 webrtc::CodecInst current_codec;
1858 if (engine()->voe()->codec()->GetSendCodec(channel, current_codec) == 0 &&
1859 (send_codec == current_codec)) {
1860 // Codec is already configured, we can return without setting it again.
1861 return true;
1862 }
1863
wu@webrtc.org9dba5252013-08-05 20:36:57 +00001864 if (engine()->voe()->codec()->SetSendCodec(channel, send_codec) == -1) {
1865 LOG_RTCERR2(SetSendCodec, channel, ToString(send_codec));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001866 return false;
1867 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001868 return true;
1869}
1870
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001871bool WebRtcVoiceMediaChannel::SetPlayout(bool playout) {
1872 desired_playout_ = playout;
1873 return ChangePlayout(desired_playout_);
1874}
1875
1876bool WebRtcVoiceMediaChannel::PausePlayout() {
1877 return ChangePlayout(false);
1878}
1879
1880bool WebRtcVoiceMediaChannel::ResumePlayout() {
1881 return ChangePlayout(desired_playout_);
1882}
1883
1884bool WebRtcVoiceMediaChannel::ChangePlayout(bool playout) {
solenberg566ef242015-11-06 15:34:49 -08001885 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001886 if (playout_ == playout) {
1887 return true;
1888 }
1889
solenberg7add0582015-11-20 09:59:34 -08001890 for (const auto& ch : recv_streams_) {
Fredrik Solenbergaf9fb212015-08-26 10:45:53 +02001891 if (!SetPlayout(ch.second->channel(), playout)) {
henrike@webrtc.org1e09a712013-07-26 19:17:59 +00001892 LOG(LS_ERROR) << "SetPlayout " << playout << " on channel "
Fredrik Solenbergaf9fb212015-08-26 10:45:53 +02001893 << ch.second->channel() << " failed";
solenberg1ac56142015-10-13 03:58:19 -07001894 return false;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001895 }
1896 }
solenberg1ac56142015-10-13 03:58:19 -07001897 playout_ = playout;
1898 return true;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001899}
1900
1901bool WebRtcVoiceMediaChannel::SetSend(SendFlags send) {
1902 desired_send_ = send;
solenbergc96df772015-10-21 13:01:53 -07001903 if (!send_streams_.empty()) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001904 return ChangeSend(desired_send_);
solenbergc96df772015-10-21 13:01:53 -07001905 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001906 return true;
1907}
1908
1909bool WebRtcVoiceMediaChannel::PauseSend() {
1910 return ChangeSend(SEND_NOTHING);
1911}
1912
1913bool WebRtcVoiceMediaChannel::ResumeSend() {
1914 return ChangeSend(desired_send_);
1915}
1916
1917bool WebRtcVoiceMediaChannel::ChangeSend(SendFlags send) {
1918 if (send_ == send) {
1919 return true;
1920 }
1921
solenberg63b34542015-09-29 06:06:31 -07001922 // Apply channel specific options.
1923 if (send == SEND_MICROPHONE) {
1924 engine()->ApplyOptions(options_);
1925 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001926
wu@webrtc.org9dba5252013-08-05 20:36:57 +00001927 // Change the settings on each send channel.
solenbergc96df772015-10-21 13:01:53 -07001928 for (const auto& ch : send_streams_) {
solenberg63b34542015-09-29 06:06:31 -07001929 if (!ChangeSend(ch.second->channel(), send)) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001930 return false;
solenberg63b34542015-09-29 06:06:31 -07001931 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001932 }
wu@webrtc.org9dba5252013-08-05 20:36:57 +00001933
solenberg63b34542015-09-29 06:06:31 -07001934 // Clear up the options after stopping sending. Since we may previously have
1935 // applied the channel specific options, now apply the original options stored
1936 // in WebRtcVoiceEngine.
1937 if (send == SEND_NOTHING) {
1938 engine()->ApplyOptions(engine()->GetOptions());
1939 }
wu@webrtc.org9dba5252013-08-05 20:36:57 +00001940
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001941 send_ = send;
1942 return true;
1943}
1944
wu@webrtc.org9dba5252013-08-05 20:36:57 +00001945bool WebRtcVoiceMediaChannel::ChangeSend(int channel, SendFlags send) {
1946 if (send == SEND_MICROPHONE) {
1947 if (engine()->voe()->base()->StartSend(channel) == -1) {
1948 LOG_RTCERR1(StartSend, channel);
1949 return false;
1950 }
wu@webrtc.org9dba5252013-08-05 20:36:57 +00001951 } else { // SEND_NOTHING
henrikg91d6ede2015-09-17 00:24:34 -07001952 RTC_DCHECK(send == SEND_NOTHING);
wu@webrtc.org9dba5252013-08-05 20:36:57 +00001953 if (engine()->voe()->base()->StopSend(channel) == -1) {
1954 LOG_RTCERR1(StopSend, channel);
1955 return false;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001956 }
1957 }
1958
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001959 return true;
1960}
1961
Peter Boström0c4e06b2015-10-07 12:23:21 +02001962bool WebRtcVoiceMediaChannel::SetAudioSend(uint32_t ssrc,
1963 bool enable,
solenberg1dd98f32015-09-10 01:57:14 -07001964 const AudioOptions* options,
1965 AudioRenderer* renderer) {
solenberg566ef242015-11-06 15:34:49 -08001966 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
solenberg1dd98f32015-09-10 01:57:14 -07001967 // TODO(solenberg): The state change should be fully rolled back if any one of
1968 // these calls fail.
1969 if (!SetLocalRenderer(ssrc, renderer)) {
1970 return false;
1971 }
solenbergdfc8f4f2015-10-01 02:31:10 -07001972 if (!MuteStream(ssrc, !enable)) {
solenberg1dd98f32015-09-10 01:57:14 -07001973 return false;
1974 }
solenbergdfc8f4f2015-10-01 02:31:10 -07001975 if (enable && options) {
solenberg1dd98f32015-09-10 01:57:14 -07001976 return SetOptions(*options);
1977 }
1978 return true;
1979}
1980
solenberg0a617e22015-10-20 15:49:38 -07001981int WebRtcVoiceMediaChannel::CreateVoEChannel() {
1982 int id = engine()->CreateVoEChannel();
1983 if (id == -1) {
1984 LOG_RTCERR0(CreateVoEChannel);
1985 return -1;
wu@webrtc.org9dba5252013-08-05 20:36:57 +00001986 }
solenberg0a617e22015-10-20 15:49:38 -07001987 if (engine()->voe()->network()->RegisterExternalTransport(id, *this) == -1) {
1988 LOG_RTCERR2(RegisterExternalTransport, id, this);
1989 engine()->voe()->base()->DeleteChannel(id);
1990 return -1;
1991 }
1992 return id;
wu@webrtc.org9dba5252013-08-05 20:36:57 +00001993}
1994
solenberg7add0582015-11-20 09:59:34 -08001995bool WebRtcVoiceMediaChannel::DeleteVoEChannel(int channel) {
wu@webrtc.org9dba5252013-08-05 20:36:57 +00001996 if (engine()->voe()->network()->DeRegisterExternalTransport(channel) == -1) {
1997 LOG_RTCERR1(DeRegisterExternalTransport, channel);
1998 }
wu@webrtc.org9dba5252013-08-05 20:36:57 +00001999 if (engine()->voe()->base()->DeleteChannel(channel) == -1) {
2000 LOG_RTCERR1(DeleteChannel, channel);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002001 return false;
2002 }
wu@webrtc.org9dba5252013-08-05 20:36:57 +00002003 return true;
2004}
henrike@webrtc.org1e09a712013-07-26 19:17:59 +00002005
wu@webrtc.org9dba5252013-08-05 20:36:57 +00002006bool WebRtcVoiceMediaChannel::AddSendStream(const StreamParams& sp) {
solenberg566ef242015-11-06 15:34:49 -08002007 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
solenberg0a617e22015-10-20 15:49:38 -07002008 LOG(LS_INFO) << "AddSendStream: " << sp.ToString();
2009
2010 uint32_t ssrc = sp.first_ssrc();
2011 RTC_DCHECK(0 != ssrc);
2012
2013 if (GetSendChannelId(ssrc) != -1) {
2014 LOG(LS_ERROR) << "Stream already exists with ssrc " << ssrc;
wu@webrtc.org9dba5252013-08-05 20:36:57 +00002015 return false;
2016 }
2017
solenberg0a617e22015-10-20 15:49:38 -07002018 // Create a new channel for sending audio data.
2019 int channel = CreateVoEChannel();
2020 if (channel == -1) {
2021 return false;
wu@webrtc.org9dba5252013-08-05 20:36:57 +00002022 }
wu@webrtc.org9dba5252013-08-05 20:36:57 +00002023
solenbergc96df772015-10-21 13:01:53 -07002024 // Save the channel to send_streams_, so that RemoveSendStream() can still
wu@webrtc.org9dba5252013-08-05 20:36:57 +00002025 // delete the channel in case failure happens below.
mallinath@webrtc.org67ee6b92014-02-03 16:57:16 +00002026 webrtc::AudioTransport* audio_transport =
2027 engine()->voe()->base()->audio_transport();
solenberg3a941542015-11-16 07:34:50 -08002028 send_streams_.insert(std::make_pair(ssrc, new WebRtcAudioSendStream(
2029 channel, audio_transport, ssrc, sp.cname, send_rtp_extensions_, call_)));
wu@webrtc.org9dba5252013-08-05 20:36:57 +00002030
solenberg0a617e22015-10-20 15:49:38 -07002031 // Set the current codecs to be used for the new channel. We need to do this
2032 // after adding the channel to send_channels_, because of how max bitrate is
2033 // currently being configured by SetSendCodec().
2034 if (!send_codecs_.empty() && !SetSendCodecs(channel, send_codecs_)) {
2035 RemoveSendStream(ssrc);
wu@webrtc.org9dba5252013-08-05 20:36:57 +00002036 return false;
2037 }
2038
2039 // At this point the channel's local SSRC has been updated. If the channel is
solenberg0a617e22015-10-20 15:49:38 -07002040 // the first send channel make sure that all the receive channels are updated
2041 // with the same SSRC in order to send receiver reports.
solenbergc96df772015-10-21 13:01:53 -07002042 if (send_streams_.size() == 1) {
solenberg0a617e22015-10-20 15:49:38 -07002043 receiver_reports_ssrc_ = ssrc;
solenberg7add0582015-11-20 09:59:34 -08002044 for (const auto& stream : recv_streams_) {
2045 int recv_channel = stream.second->channel();
solenberg0a617e22015-10-20 15:49:38 -07002046 if (engine()->voe()->rtp()->SetLocalSSRC(recv_channel, ssrc) != 0) {
solenberg7add0582015-11-20 09:59:34 -08002047 LOG_RTCERR2(SetLocalSSRC, recv_channel, ssrc);
solenberg1ac56142015-10-13 03:58:19 -07002048 return false;
wu@webrtc.org9dba5252013-08-05 20:36:57 +00002049 }
solenberg0a617e22015-10-20 15:49:38 -07002050 engine()->voe()->base()->AssociateSendChannel(recv_channel, channel);
2051 LOG(LS_INFO) << "VoiceEngine channel #" << recv_channel
2052 << " is associated with channel #" << channel << ".";
wu@webrtc.org9dba5252013-08-05 20:36:57 +00002053 }
2054 }
2055
wu@webrtc.org9dba5252013-08-05 20:36:57 +00002056 return ChangeSend(channel, desired_send_);
2057}
2058
Peter Boström0c4e06b2015-10-07 12:23:21 +02002059bool WebRtcVoiceMediaChannel::RemoveSendStream(uint32_t ssrc) {
solenberg566ef242015-11-06 15:34:49 -08002060 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
solenberg3a941542015-11-16 07:34:50 -08002061 LOG(LS_INFO) << "RemoveSendStream: " << ssrc;
2062
solenbergc96df772015-10-21 13:01:53 -07002063 auto it = send_streams_.find(ssrc);
2064 if (it == send_streams_.end()) {
wu@webrtc.org9dba5252013-08-05 20:36:57 +00002065 LOG(LS_WARNING) << "Try to remove stream with ssrc " << ssrc
2066 << " which doesn't exist.";
2067 return false;
2068 }
2069
mallinath@webrtc.org67ee6b92014-02-03 16:57:16 +00002070 int channel = it->second->channel();
wu@webrtc.org9dba5252013-08-05 20:36:57 +00002071 ChangeSend(channel, SEND_NOTHING);
2072
solenberg7add0582015-11-20 09:59:34 -08002073 // Clean up and delete the send stream+channel.
solenberg0a617e22015-10-20 15:49:38 -07002074 LOG(LS_INFO) << "Removing audio send stream " << ssrc
2075 << " with VoiceEngine channel #" << channel << ".";
solenberg7add0582015-11-20 09:59:34 -08002076 delete it->second;
2077 send_streams_.erase(it);
2078 if (!DeleteVoEChannel(channel)) {
solenberg0a617e22015-10-20 15:49:38 -07002079 return false;
wu@webrtc.org9dba5252013-08-05 20:36:57 +00002080 }
solenbergc96df772015-10-21 13:01:53 -07002081 if (send_streams_.empty()) {
wu@webrtc.org9dba5252013-08-05 20:36:57 +00002082 ChangeSend(SEND_NOTHING);
solenberg0a617e22015-10-20 15:49:38 -07002083 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002084 return true;
2085}
2086
2087bool WebRtcVoiceMediaChannel::AddRecvStream(const StreamParams& sp) {
solenberg566ef242015-11-06 15:34:49 -08002088 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
solenbergd97ec302015-10-07 01:40:33 -07002089 LOG(LS_INFO) << "AddRecvStream: " << sp.ToString();
2090
solenberg0b675462015-10-09 01:37:09 -07002091 if (!ValidateStreamParams(sp)) {
wu@webrtc.org78187522013-10-07 23:32:02 +00002092 return false;
2093 }
2094
solenberg7add0582015-11-20 09:59:34 -08002095 const uint32_t ssrc = sp.first_ssrc();
solenberg0b675462015-10-09 01:37:09 -07002096 if (ssrc == 0) {
2097 LOG(LS_WARNING) << "AddRecvStream with ssrc==0 is not supported.";
2098 return false;
2099 }
2100
solenberg1ac56142015-10-13 03:58:19 -07002101 // Remove the default receive stream if one had been created with this ssrc;
2102 // we'll recreate it then.
2103 if (IsDefaultRecvStream(ssrc)) {
2104 RemoveRecvStream(ssrc);
2105 }
solenberg0b675462015-10-09 01:37:09 -07002106
solenberg7add0582015-11-20 09:59:34 -08002107 if (GetReceiveChannelId(ssrc) != -1) {
henrike@webrtc.org1e09a712013-07-26 19:17:59 +00002108 LOG(LS_ERROR) << "Stream already exists with ssrc " << ssrc;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002109 return false;
2110 }
Fredrik Solenberg4b60c732015-05-07 14:07:48 +02002111
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002112 // Create a new channel for receiving audio data.
solenberg7add0582015-11-20 09:59:34 -08002113 const int channel = CreateVoEChannel();
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002114 if (channel == -1) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002115 return false;
2116 }
Minyue2013aec2015-05-13 14:14:42 +02002117
solenberg1ac56142015-10-13 03:58:19 -07002118 // Turn off all supported codecs.
solenberg26c8c912015-11-27 04:00:25 -08002119 // TODO(solenberg): Remove once "no codecs" is the default state of a stream.
2120 for (webrtc::CodecInst voe_codec : webrtc::acm2::RentACodec::Database()) {
2121 voe_codec.pltype = -1;
2122 if (engine()->voe()->codec()->SetRecPayloadType(channel, voe_codec) == -1) {
2123 LOG_RTCERR2(SetRecPayloadType, channel, ToString(voe_codec));
2124 DeleteVoEChannel(channel);
2125 return false;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002126 }
2127 }
2128
solenberg1ac56142015-10-13 03:58:19 -07002129 // Only enable those configured for this channel.
2130 for (const auto& codec : recv_codecs_) {
2131 webrtc::CodecInst voe_codec;
solenberg26c8c912015-11-27 04:00:25 -08002132 if (WebRtcVoiceEngine::ToCodecInst(codec, &voe_codec)) {
solenberg1ac56142015-10-13 03:58:19 -07002133 voe_codec.pltype = codec.id;
2134 if (engine()->voe()->codec()->SetRecPayloadType(
2135 channel, voe_codec) == -1) {
2136 LOG_RTCERR2(SetRecPayloadType, channel, ToString(voe_codec));
solenberg7add0582015-11-20 09:59:34 -08002137 DeleteVoEChannel(channel);
solenberg1ac56142015-10-13 03:58:19 -07002138 return false;
2139 }
henrike@webrtc.org1e09a712013-07-26 19:17:59 +00002140 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002141 }
solenberg8fb30c32015-10-13 03:06:58 -07002142
solenberg7add0582015-11-20 09:59:34 -08002143 const int send_channel = GetSendChannelId(receiver_reports_ssrc_);
2144 if (send_channel != -1) {
2145 // Associate receive channel with first send channel (so the receive channel
2146 // can obtain RTT from the send channel)
2147 engine()->voe()->base()->AssociateSendChannel(channel, send_channel);
2148 LOG(LS_INFO) << "VoiceEngine channel #" << channel
2149 << " is associated with channel #" << send_channel << ".";
buildbot@webrtc.org150835e2014-05-06 15:54:38 +00002150 }
2151
solenberg7add0582015-11-20 09:59:34 -08002152 recv_streams_.insert(std::make_pair(ssrc, new WebRtcAudioReceiveStream(
2153 channel, ssrc, receiver_reports_ssrc_,
2154 options_.combined_audio_video_bwe.value_or(false), sp.sync_label,
2155 recv_rtp_extensions_, call_)));
2156
2157 SetNack(channel, nack_enabled_);
solenberg1ac56142015-10-13 03:58:19 -07002158 SetPlayout(channel, playout_);
solenberg7add0582015-11-20 09:59:34 -08002159
solenberg1ac56142015-10-13 03:58:19 -07002160 return true;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002161}
2162
Peter Boström0c4e06b2015-10-07 12:23:21 +02002163bool WebRtcVoiceMediaChannel::RemoveRecvStream(uint32_t ssrc) {
solenberg566ef242015-11-06 15:34:49 -08002164 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
solenbergd97ec302015-10-07 01:40:33 -07002165 LOG(LS_INFO) << "RemoveRecvStream: " << ssrc;
2166
solenberg7add0582015-11-20 09:59:34 -08002167 const auto it = recv_streams_.find(ssrc);
2168 if (it == recv_streams_.end()) {
wu@webrtc.org9dba5252013-08-05 20:36:57 +00002169 LOG(LS_WARNING) << "Try to remove stream with ssrc " << ssrc
2170 << " which doesn't exist.";
henrike@webrtc.org1e09a712013-07-26 19:17:59 +00002171 return false;
wu@webrtc.org9dba5252013-08-05 20:36:57 +00002172 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002173
solenberg1ac56142015-10-13 03:58:19 -07002174 // Deregister default channel, if that's the one being destroyed.
2175 if (IsDefaultRecvStream(ssrc)) {
2176 default_recv_ssrc_ = -1;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002177 }
henrike@webrtc.org1e09a712013-07-26 19:17:59 +00002178
solenberg7add0582015-11-20 09:59:34 -08002179 const int channel = it->second->channel();
2180
2181 // Clean up and delete the receive stream+channel.
2182 LOG(LS_INFO) << "Removing audio receive stream " << ssrc
mallinath@webrtc.org67ee6b92014-02-03 16:57:16 +00002183 << " with VoiceEngine channel #" << channel << ".";
solenberg7add0582015-11-20 09:59:34 -08002184 delete it->second;
2185 recv_streams_.erase(it);
2186 return DeleteVoEChannel(channel);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002187}
2188
Peter Boström0c4e06b2015-10-07 12:23:21 +02002189bool WebRtcVoiceMediaChannel::SetLocalRenderer(uint32_t ssrc,
henrike@webrtc.org1e09a712013-07-26 19:17:59 +00002190 AudioRenderer* renderer) {
solenbergc96df772015-10-21 13:01:53 -07002191 auto it = send_streams_.find(ssrc);
2192 if (it == send_streams_.end()) {
wu@webrtc.org9dba5252013-08-05 20:36:57 +00002193 if (renderer) {
2194 // Return an error if trying to set a valid renderer with an invalid ssrc.
2195 LOG(LS_ERROR) << "SetLocalRenderer failed with ssrc "<< ssrc;
2196 return false;
2197 }
2198
2199 // The channel likely has gone away, do nothing.
henrike@webrtc.org1e09a712013-07-26 19:17:59 +00002200 return true;
henrike@webrtc.org1e09a712013-07-26 19:17:59 +00002201 }
2202
solenberg1ac56142015-10-13 03:58:19 -07002203 if (renderer) {
mallinath@webrtc.org67ee6b92014-02-03 16:57:16 +00002204 it->second->Start(renderer);
solenberg1ac56142015-10-13 03:58:19 -07002205 } else {
mallinath@webrtc.org67ee6b92014-02-03 16:57:16 +00002206 it->second->Stop();
solenberg1ac56142015-10-13 03:58:19 -07002207 }
henrike@webrtc.org1e09a712013-07-26 19:17:59 +00002208
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002209 return true;
2210}
2211
2212bool WebRtcVoiceMediaChannel::GetActiveStreams(
2213 AudioInfo::StreamList* actives) {
solenberg566ef242015-11-06 15:34:49 -08002214 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002215 actives->clear();
solenberg7add0582015-11-20 09:59:34 -08002216 for (const auto& ch : recv_streams_) {
Fredrik Solenbergaf9fb212015-08-26 10:45:53 +02002217 int level = GetOutputLevel(ch.second->channel());
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002218 if (level > 0) {
Fredrik Solenbergaf9fb212015-08-26 10:45:53 +02002219 actives->push_back(std::make_pair(ch.first, level));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002220 }
2221 }
2222 return true;
2223}
2224
2225int WebRtcVoiceMediaChannel::GetOutputLevel() {
solenberg566ef242015-11-06 15:34:49 -08002226 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
solenberg1ac56142015-10-13 03:58:19 -07002227 int highest = 0;
solenberg7add0582015-11-20 09:59:34 -08002228 for (const auto& ch : recv_streams_) {
solenberg8fb30c32015-10-13 03:06:58 -07002229 highest = std::max(GetOutputLevel(ch.second->channel()), highest);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002230 }
2231 return highest;
2232}
2233
2234int WebRtcVoiceMediaChannel::GetTimeSinceLastTyping() {
2235 int ret;
2236 if (engine()->voe()->processing()->TimeSinceLastTyping(ret) == -1) {
2237 // In case of error, log the info and continue
2238 LOG_RTCERR0(TimeSinceLastTyping);
2239 ret = -1;
2240 } else {
2241 ret *= 1000; // We return ms, webrtc returns seconds.
2242 }
2243 return ret;
2244}
2245
2246void WebRtcVoiceMediaChannel::SetTypingDetectionParameters(int time_window,
2247 int cost_per_typing, int reporting_threshold, int penalty_decay,
2248 int type_event_delay) {
2249 if (engine()->voe()->processing()->SetTypingDetectionParameters(
2250 time_window, cost_per_typing,
2251 reporting_threshold, penalty_decay, type_event_delay) == -1) {
2252 // In case of error, log the info and continue
2253 LOG_RTCERR5(SetTypingDetectionParameters, time_window,
2254 cost_per_typing, reporting_threshold, penalty_decay,
2255 type_event_delay);
2256 }
2257}
2258
solenberg4bac9c52015-10-09 02:32:53 -07002259bool WebRtcVoiceMediaChannel::SetOutputVolume(uint32_t ssrc, double volume) {
solenberg566ef242015-11-06 15:34:49 -08002260 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
solenberg1ac56142015-10-13 03:58:19 -07002261 if (ssrc == 0) {
2262 default_recv_volume_ = volume;
2263 if (default_recv_ssrc_ == -1) {
2264 return true;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002265 }
solenberg1ac56142015-10-13 03:58:19 -07002266 ssrc = static_cast<uint32_t>(default_recv_ssrc_);
2267 }
2268 int ch_id = GetReceiveChannelId(ssrc);
2269 if (ch_id < 0) {
2270 LOG(LS_WARNING) << "Cannot find channel for ssrc:" << ssrc;
2271 return false;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002272 }
2273
solenberg1ac56142015-10-13 03:58:19 -07002274 if (-1 == engine()->voe()->volume()->SetChannelOutputVolumeScaling(ch_id,
2275 volume)) {
2276 LOG_RTCERR2(SetChannelOutputVolumeScaling, ch_id, volume);
2277 return false;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002278 }
solenberg1ac56142015-10-13 03:58:19 -07002279 LOG(LS_INFO) << "SetOutputVolume to " << volume
2280 << " for channel " << ch_id << " and ssrc " << ssrc;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002281 return true;
2282}
2283
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002284bool WebRtcVoiceMediaChannel::CanInsertDtmf() {
2285 return dtmf_allowed_;
2286}
2287
Peter Boström0c4e06b2015-10-07 12:23:21 +02002288bool WebRtcVoiceMediaChannel::InsertDtmf(uint32_t ssrc,
2289 int event,
2290 int duration,
2291 int flags) {
solenberg566ef242015-11-06 15:34:49 -08002292 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002293 if (!dtmf_allowed_) {
2294 return false;
2295 }
2296
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002297 // Send the event.
2298 if (flags & cricket::DF_SEND) {
wu@webrtc.orgcadf9042013-08-30 21:24:16 +00002299 int channel = -1;
2300 if (ssrc == 0) {
solenbergc96df772015-10-21 13:01:53 -07002301 if (send_streams_.size() > 0) {
2302 channel = send_streams_.begin()->second->channel();
wu@webrtc.orgcadf9042013-08-30 21:24:16 +00002303 }
2304 } else {
solenbergd97ec302015-10-07 01:40:33 -07002305 channel = GetSendChannelId(ssrc);
wu@webrtc.orgcadf9042013-08-30 21:24:16 +00002306 }
wu@webrtc.org9dba5252013-08-05 20:36:57 +00002307 if (channel == -1) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002308 LOG(LS_WARNING) << "InsertDtmf - The specified ssrc "
2309 << ssrc << " is not in use.";
2310 return false;
2311 }
2312 // Send DTMF using out-of-band DTMF. ("true", as 3rd arg)
wu@webrtc.org9dba5252013-08-05 20:36:57 +00002313 if (engine()->voe()->dtmf()->SendTelephoneEvent(
2314 channel, event, true, duration) == -1) {
2315 LOG_RTCERR4(SendTelephoneEvent, channel, event, true, duration);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002316 return false;
2317 }
2318 }
2319
2320 // Play the event.
2321 if (flags & cricket::DF_PLAY) {
2322 // Play DTMF tone locally.
2323 if (engine()->voe()->dtmf()->PlayDtmfTone(event, duration) == -1) {
2324 LOG_RTCERR2(PlayDtmfTone, event, duration);
2325 return false;
2326 }
2327 }
2328
2329 return true;
2330}
2331
wu@webrtc.orga9890802013-12-13 00:21:03 +00002332void WebRtcVoiceMediaChannel::OnPacketReceived(
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00002333 rtc::Buffer* packet, const rtc::PacketTime& packet_time) {
solenberg566ef242015-11-06 15:34:49 -08002334 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
Fredrik Solenberg4b60c732015-05-07 14:07:48 +02002335
solenberg1ac56142015-10-13 03:58:19 -07002336 uint32_t ssrc = 0;
2337 if (!GetRtpSsrc(packet->data(), packet->size(), &ssrc)) {
2338 return;
2339 }
2340
solenberg7e63ef02015-11-20 00:19:43 -08002341 // If we don't have a default channel, and the SSRC is unknown, create a
2342 // default channel.
2343 if (default_recv_ssrc_ == -1 && GetReceiveChannelId(ssrc) == -1) {
solenberg1ac56142015-10-13 03:58:19 -07002344 StreamParams sp;
2345 sp.ssrcs.push_back(ssrc);
2346 LOG(LS_INFO) << "Creating default receive stream for SSRC=" << ssrc << ".";
2347 if (!AddRecvStream(sp)) {
2348 LOG(LS_WARNING) << "Could not create default receive stream.";
2349 return;
2350 }
2351 default_recv_ssrc_ = ssrc;
2352 SetOutputVolume(default_recv_ssrc_, default_recv_volume_);
2353 }
2354
2355 // Forward packet to Call. If the SSRC is unknown we'll return after this.
Fredrik Solenberg709ed672015-09-15 12:26:33 +02002356 const webrtc::PacketTime webrtc_packet_time(packet_time.timestamp,
2357 packet_time.not_before);
solenberg1ac56142015-10-13 03:58:19 -07002358 webrtc::PacketReceiver::DeliveryStatus delivery_result =
2359 call_->Receiver()->DeliverPacket(webrtc::MediaType::AUDIO,
2360 reinterpret_cast<const uint8_t*>(packet->data()), packet->size(),
2361 webrtc_packet_time);
2362 if (webrtc::PacketReceiver::DELIVERY_OK != delivery_result) {
solenberg7e63ef02015-11-20 00:19:43 -08002363 // If the SSRC is unknown here, route it to the default channel, if we have
2364 // one. See: https://bugs.chromium.org/p/webrtc/issues/detail?id=5208
2365 if (default_recv_ssrc_ == -1) {
2366 return;
2367 } else {
2368 ssrc = default_recv_ssrc_;
2369 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002370 }
2371
solenberg1ac56142015-10-13 03:58:19 -07002372 // Find the channel to send this packet to. It must exist since webrtc::Call
2373 // was able to demux the packet.
2374 int channel = GetReceiveChannelId(ssrc);
2375 RTC_DCHECK(channel != -1);
2376
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002377 // Pass it off to the decoder.
henrike@webrtc.org28654cb2013-07-22 21:07:49 +00002378 engine()->voe()->network()->ReceivedRTPPacket(
solenberg1ac56142015-10-13 03:58:19 -07002379 channel, packet->data(), packet->size(), webrtc_packet_time);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002380}
2381
wu@webrtc.orga9890802013-12-13 00:21:03 +00002382void WebRtcVoiceMediaChannel::OnRtcpReceived(
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00002383 rtc::Buffer* packet, const rtc::PacketTime& packet_time) {
solenberg566ef242015-11-06 15:34:49 -08002384 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
Fredrik Solenberg4b60c732015-05-07 14:07:48 +02002385
Fredrik Solenberg709ed672015-09-15 12:26:33 +02002386 // Forward packet to Call as well.
2387 const webrtc::PacketTime webrtc_packet_time(packet_time.timestamp,
2388 packet_time.not_before);
2389 call_->Receiver()->DeliverPacket(webrtc::MediaType::AUDIO,
2390 reinterpret_cast<const uint8_t*>(packet->data()), packet->size(),
2391 webrtc_packet_time);
Fredrik Solenberg4b60c732015-05-07 14:07:48 +02002392
wu@webrtc.org9dba5252013-08-05 20:36:57 +00002393 // Sending channels need all RTCP packets with feedback information.
2394 // Even sender reports can contain attached report blocks.
2395 // Receiving channels need sender reports in order to create
2396 // correct receiver reports.
2397 int type = 0;
kwiberg@webrtc.orgeebcab52015-03-24 09:19:06 +00002398 if (!GetRtcpType(packet->data(), packet->size(), &type)) {
wu@webrtc.org9dba5252013-08-05 20:36:57 +00002399 LOG(LS_WARNING) << "Failed to parse type from received RTCP packet";
2400 return;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002401 }
2402
solenberg0b675462015-10-09 01:37:09 -07002403 // If it is a sender report, find the receive channel that is listening.
wu@webrtc.org9dba5252013-08-05 20:36:57 +00002404 if (type == kRtcpTypeSR) {
solenberg0b675462015-10-09 01:37:09 -07002405 uint32_t ssrc = 0;
2406 if (!GetRtcpSsrc(packet->data(), packet->size(), &ssrc)) {
2407 return;
2408 }
2409 int recv_channel_id = GetReceiveChannelId(ssrc);
2410 if (recv_channel_id != -1) {
wu@webrtc.org9dba5252013-08-05 20:36:57 +00002411 engine()->voe()->network()->ReceivedRTCPPacket(
solenberg0b675462015-10-09 01:37:09 -07002412 recv_channel_id, packet->data(), packet->size());
wu@webrtc.org9dba5252013-08-05 20:36:57 +00002413 }
2414 }
2415
2416 // SR may continue RR and any RR entry may correspond to any one of the send
2417 // channels. So all RTCP packets must be forwarded all send channels. VoE
2418 // will filter out RR internally.
solenbergc96df772015-10-21 13:01:53 -07002419 for (const auto& ch : send_streams_) {
wu@webrtc.org9dba5252013-08-05 20:36:57 +00002420 engine()->voe()->network()->ReceivedRTCPPacket(
Fredrik Solenbergaf9fb212015-08-26 10:45:53 +02002421 ch.second->channel(), packet->data(), packet->size());
wu@webrtc.org9dba5252013-08-05 20:36:57 +00002422 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002423}
2424
Peter Boström0c4e06b2015-10-07 12:23:21 +02002425bool WebRtcVoiceMediaChannel::MuteStream(uint32_t ssrc, bool muted) {
solenberg566ef242015-11-06 15:34:49 -08002426 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
solenberg0a617e22015-10-20 15:49:38 -07002427 int channel = GetSendChannelId(ssrc);
wu@webrtc.org9dba5252013-08-05 20:36:57 +00002428 if (channel == -1) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002429 LOG(LS_WARNING) << "The specified ssrc " << ssrc << " is not in use.";
2430 return false;
2431 }
wu@webrtc.org9dba5252013-08-05 20:36:57 +00002432 if (engine()->voe()->volume()->SetInputMute(channel, muted) == -1) {
2433 LOG_RTCERR2(SetInputMute, channel, muted);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002434 return false;
2435 }
buildbot@webrtc.org6b21b712014-07-31 15:08:53 +00002436 // We set the AGC to mute state only when all the channels are muted.
2437 // This implementation is not ideal, instead we should signal the AGC when
2438 // the mic channel is muted/unmuted. We can't do it today because there
2439 // is no good way to know which stream is mapping to the mic channel.
2440 bool all_muted = muted;
solenbergc96df772015-10-21 13:01:53 -07002441 for (const auto& ch : send_streams_) {
Fredrik Solenbergaf9fb212015-08-26 10:45:53 +02002442 if (!all_muted) {
2443 break;
2444 }
2445 if (engine()->voe()->volume()->GetInputMute(ch.second->channel(),
buildbot@webrtc.org6b21b712014-07-31 15:08:53 +00002446 all_muted)) {
Fredrik Solenbergaf9fb212015-08-26 10:45:53 +02002447 LOG_RTCERR1(GetInputMute, ch.second->channel());
buildbot@webrtc.org6b21b712014-07-31 15:08:53 +00002448 return false;
2449 }
2450 }
2451
2452 webrtc::AudioProcessing* ap = engine()->voe()->base()->audio_processing();
solenberg0a617e22015-10-20 15:49:38 -07002453 if (ap) {
buildbot@webrtc.org6b21b712014-07-31 15:08:53 +00002454 ap->set_output_will_be_muted(all_muted);
solenberg0a617e22015-10-20 15:49:38 -07002455 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002456 return true;
2457}
2458
minyue@webrtc.org26236952014-10-29 02:27:08 +00002459// TODO(minyue): SetMaxSendBandwidth() is subject to be renamed to
2460// SetMaxSendBitrate() in future.
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00002461bool WebRtcVoiceMediaChannel::SetMaxSendBandwidth(int bps) {
minyue@webrtc.org26236952014-10-29 02:27:08 +00002462 LOG(LS_INFO) << "WebRtcVoiceMediaChannel::SetMaxSendBandwidth.";
minyue@webrtc.org26236952014-10-29 02:27:08 +00002463 return SetSendBitrateInternal(bps);
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00002464}
2465
minyue@webrtc.org26236952014-10-29 02:27:08 +00002466bool WebRtcVoiceMediaChannel::SetSendBitrateInternal(int bps) {
2467 LOG(LS_INFO) << "WebRtcVoiceMediaChannel::SetSendBitrateInternal.";
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00002468
minyue@webrtc.org26236952014-10-29 02:27:08 +00002469 send_bitrate_setting_ = true;
2470 send_bitrate_bps_ = bps;
wu@webrtc.org1d1ffc92013-10-16 18:12:02 +00002471
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002472 if (!send_codec_) {
wu@webrtc.org1d1ffc92013-10-16 18:12:02 +00002473 LOG(LS_INFO) << "The send codec has not been set up yet. "
minyue@webrtc.org26236952014-10-29 02:27:08 +00002474 << "The send bitrate setting will be applied later.";
wu@webrtc.org1d1ffc92013-10-16 18:12:02 +00002475 return true;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002476 }
2477
minyue@webrtc.org26236952014-10-29 02:27:08 +00002478 // Bitrate is auto by default.
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00002479 // TODO(bemasc): Fix this so that if SetMaxSendBandwidth(50) is followed by
2480 // SetMaxSendBandwith(0), the second call removes the previous limit.
2481 if (bps <= 0)
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002482 return true;
2483
2484 webrtc::CodecInst codec = *send_codec_;
solenberg26c8c912015-11-27 04:00:25 -08002485 bool is_multi_rate = WebRtcVoiceCodecs::IsCodecMultiRate(codec);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002486
2487 if (is_multi_rate) {
2488 // If codec is multi-rate then just set the bitrate.
2489 codec.rate = bps;
solenbergc96df772015-10-21 13:01:53 -07002490 for (const auto& ch : send_streams_) {
solenberg0a617e22015-10-20 15:49:38 -07002491 if (!SetSendCodec(ch.second->channel(), codec)) {
2492 LOG(LS_INFO) << "Failed to set codec " << codec.plname
2493 << " to bitrate " << bps << " bps.";
2494 return false;
2495 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002496 }
2497 return true;
2498 } else {
2499 // If codec is not multi-rate and |bps| is less than the fixed bitrate
2500 // then fail. If codec is not multi-rate and |bps| exceeds or equal the
2501 // fixed bitrate then ignore.
2502 if (bps < codec.rate) {
2503 LOG(LS_INFO) << "Failed to set codec " << codec.plname
2504 << " to bitrate " << bps << " bps"
2505 << ", requires at least " << codec.rate << " bps.";
2506 return false;
2507 }
2508 return true;
2509 }
2510}
2511
2512bool WebRtcVoiceMediaChannel::GetStats(VoiceMediaInfo* info) {
solenberg566ef242015-11-06 15:34:49 -08002513 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
solenberg85a04962015-10-27 03:35:21 -07002514 RTC_DCHECK(info);
solenbergd97ec302015-10-07 01:40:33 -07002515
solenberg85a04962015-10-27 03:35:21 -07002516 // Get SSRC and stats for each sender.
2517 RTC_DCHECK(info->senders.size() == 0);
2518 for (const auto& stream : send_streams_) {
2519 webrtc::AudioSendStream::Stats stats = stream.second->GetStats();
wu@webrtc.org9dba5252013-08-05 20:36:57 +00002520 VoiceSenderInfo sinfo;
solenberg85a04962015-10-27 03:35:21 -07002521 sinfo.add_ssrc(stats.local_ssrc);
2522 sinfo.bytes_sent = stats.bytes_sent;
2523 sinfo.packets_sent = stats.packets_sent;
2524 sinfo.packets_lost = stats.packets_lost;
2525 sinfo.fraction_lost = stats.fraction_lost;
2526 sinfo.codec_name = stats.codec_name;
2527 sinfo.ext_seqnum = stats.ext_seqnum;
2528 sinfo.jitter_ms = stats.jitter_ms;
2529 sinfo.rtt_ms = stats.rtt_ms;
2530 sinfo.audio_level = stats.audio_level;
2531 sinfo.aec_quality_min = stats.aec_quality_min;
2532 sinfo.echo_delay_median_ms = stats.echo_delay_median_ms;
2533 sinfo.echo_delay_std_ms = stats.echo_delay_std_ms;
2534 sinfo.echo_return_loss = stats.echo_return_loss;
2535 sinfo.echo_return_loss_enhancement = stats.echo_return_loss_enhancement;
solenberg566ef242015-11-06 15:34:49 -08002536 sinfo.typing_noise_detected =
2537 (send_ == SEND_NOTHING ? false : stats.typing_noise_detected);
wu@webrtc.org9dba5252013-08-05 20:36:57 +00002538 info->senders.push_back(sinfo);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002539 }
2540
solenberg85a04962015-10-27 03:35:21 -07002541 // Get SSRC and stats for each receiver.
2542 RTC_DCHECK(info->receivers.size() == 0);
solenberg7add0582015-11-20 09:59:34 -08002543 for (const auto& stream : recv_streams_) {
Fredrik Solenberg4f4ec0a2015-10-22 10:49:27 +02002544 webrtc::AudioReceiveStream::Stats stats = stream.second->GetStats();
2545 VoiceReceiverInfo rinfo;
2546 rinfo.add_ssrc(stats.remote_ssrc);
2547 rinfo.bytes_rcvd = stats.bytes_rcvd;
2548 rinfo.packets_rcvd = stats.packets_rcvd;
2549 rinfo.packets_lost = stats.packets_lost;
2550 rinfo.fraction_lost = stats.fraction_lost;
2551 rinfo.codec_name = stats.codec_name;
2552 rinfo.ext_seqnum = stats.ext_seqnum;
2553 rinfo.jitter_ms = stats.jitter_ms;
2554 rinfo.jitter_buffer_ms = stats.jitter_buffer_ms;
2555 rinfo.jitter_buffer_preferred_ms = stats.jitter_buffer_preferred_ms;
2556 rinfo.delay_estimate_ms = stats.delay_estimate_ms;
2557 rinfo.audio_level = stats.audio_level;
2558 rinfo.expand_rate = stats.expand_rate;
2559 rinfo.speech_expand_rate = stats.speech_expand_rate;
2560 rinfo.secondary_decoded_rate = stats.secondary_decoded_rate;
2561 rinfo.accelerate_rate = stats.accelerate_rate;
2562 rinfo.preemptive_expand_rate = stats.preemptive_expand_rate;
2563 rinfo.decoding_calls_to_silence_generator =
2564 stats.decoding_calls_to_silence_generator;
2565 rinfo.decoding_calls_to_neteq = stats.decoding_calls_to_neteq;
2566 rinfo.decoding_normal = stats.decoding_normal;
2567 rinfo.decoding_plc = stats.decoding_plc;
2568 rinfo.decoding_cng = stats.decoding_cng;
2569 rinfo.decoding_plc_cng = stats.decoding_plc_cng;
2570 rinfo.capture_start_ntp_time_ms = stats.capture_start_ntp_time_ms;
2571 info->receivers.push_back(rinfo);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002572 }
2573
2574 return true;
2575}
2576
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002577int WebRtcVoiceMediaChannel::GetOutputLevel(int channel) {
solenbergd97ec302015-10-07 01:40:33 -07002578 unsigned int ulevel = 0;
2579 int ret = engine()->voe()->volume()->GetSpeechOutputLevel(channel, ulevel);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002580 return (ret == 0) ? static_cast<int>(ulevel) : -1;
2581}
2582
Peter Boström0c4e06b2015-10-07 12:23:21 +02002583int WebRtcVoiceMediaChannel::GetReceiveChannelId(uint32_t ssrc) const {
solenberg566ef242015-11-06 15:34:49 -08002584 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
solenberg7add0582015-11-20 09:59:34 -08002585 const auto it = recv_streams_.find(ssrc);
2586 if (it != recv_streams_.end()) {
mallinath@webrtc.org67ee6b92014-02-03 16:57:16 +00002587 return it->second->channel();
solenberg8fb30c32015-10-13 03:06:58 -07002588 }
solenberg1ac56142015-10-13 03:58:19 -07002589 return -1;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002590}
2591
Peter Boström0c4e06b2015-10-07 12:23:21 +02002592int WebRtcVoiceMediaChannel::GetSendChannelId(uint32_t ssrc) const {
solenberg566ef242015-11-06 15:34:49 -08002593 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
solenbergc96df772015-10-21 13:01:53 -07002594 const auto it = send_streams_.find(ssrc);
2595 if (it != send_streams_.end()) {
mallinath@webrtc.org67ee6b92014-02-03 16:57:16 +00002596 return it->second->channel();
solenberg8fb30c32015-10-13 03:06:58 -07002597 }
wu@webrtc.org9dba5252013-08-05 20:36:57 +00002598 return -1;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002599}
2600
2601bool WebRtcVoiceMediaChannel::GetRedSendCodec(const AudioCodec& red_codec,
2602 const std::vector<AudioCodec>& all_codecs, webrtc::CodecInst* send_codec) {
2603 // Get the RED encodings from the parameter with no name. This may
2604 // change based on what is discussed on the Jingle list.
2605 // The encoding parameter is of the form "a/b"; we only support where
2606 // a == b. Verify this and parse out the value into red_pt.
2607 // If the parameter value is absent (as it will be until we wire up the
2608 // signaling of this message), use the second codec specified (i.e. the
2609 // one after "red") as the encoding parameter.
2610 int red_pt = -1;
2611 std::string red_params;
2612 CodecParameterMap::const_iterator it = red_codec.params.find("");
2613 if (it != red_codec.params.end()) {
2614 red_params = it->second;
2615 std::vector<std::string> red_pts;
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00002616 if (rtc::split(red_params, '/', &red_pts) != 2 ||
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002617 red_pts[0] != red_pts[1] ||
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00002618 !rtc::FromString(red_pts[0], &red_pt)) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002619 LOG(LS_WARNING) << "RED params " << red_params << " not supported.";
2620 return false;
2621 }
2622 } else if (red_codec.params.empty()) {
2623 LOG(LS_WARNING) << "RED params not present, using defaults";
2624 if (all_codecs.size() > 1) {
2625 red_pt = all_codecs[1].id;
2626 }
2627 }
2628
2629 // Try to find red_pt in |codecs|.
Fredrik Solenbergaf9fb212015-08-26 10:45:53 +02002630 for (const AudioCodec& codec : all_codecs) {
2631 if (codec.id == red_pt) {
2632 // If we find the right codec, that will be the codec we pass to
2633 // SetSendCodec, with the desired payload type.
solenberg26c8c912015-11-27 04:00:25 -08002634 if (WebRtcVoiceEngine::ToCodecInst(codec, send_codec)) {
Fredrik Solenbergaf9fb212015-08-26 10:45:53 +02002635 return true;
2636 } else {
2637 break;
2638 }
2639 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002640 }
Fredrik Solenbergaf9fb212015-08-26 10:45:53 +02002641 LOG(LS_WARNING) << "RED params " << red_params << " are invalid.";
2642 return false;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002643}
2644
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002645bool WebRtcVoiceMediaChannel::SetPlayout(int channel, bool playout) {
2646 if (playout) {
2647 LOG(LS_INFO) << "Starting playout for channel #" << channel;
2648 if (engine()->voe()->base()->StartPlayout(channel) == -1) {
2649 LOG_RTCERR1(StartPlayout, channel);
2650 return false;
2651 }
2652 } else {
2653 LOG(LS_INFO) << "Stopping playout for channel #" << channel;
2654 engine()->voe()->base()->StopPlayout(channel);
2655 }
2656 return true;
2657}
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002658} // namespace cricket
2659
2660#endif // HAVE_WEBRTC_VOICE