blob: 1ffc66b8fa71acdb68529320f1ecc93d306469b0 [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
Fredrik Solenbergb5727682015-12-04 15:22:19 +0100137// Constants from voice_engine_defines.h.
138const int kMinTelephoneEventCode = 0; // RFC4733 (Section 2.3.1)
139const int kMaxTelephoneEventCode = 255;
140const int kMinTelephoneEventDuration = 100;
141const int kMaxTelephoneEventDuration = 60000; // Actual limit is 2^16
142
solenberg0b675462015-10-09 01:37:09 -0700143bool ValidateStreamParams(const StreamParams& sp) {
144 if (sp.ssrcs.empty()) {
145 LOG(LS_ERROR) << "No SSRCs in stream parameters: " << sp.ToString();
146 return false;
147 }
148 if (sp.ssrcs.size() > 1) {
149 LOG(LS_ERROR) << "Multiple SSRCs in stream parameters: " << sp.ToString();
150 return false;
151 }
152 return true;
153}
154
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000155// Dumps an AudioCodec in RFC 2327-ish format.
solenbergd97ec302015-10-07 01:40:33 -0700156std::string ToString(const AudioCodec& codec) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000157 std::stringstream ss;
158 ss << codec.name << "/" << codec.clockrate << "/" << codec.channels
159 << " (" << codec.id << ")";
160 return ss.str();
161}
Minyue Li7100dcd2015-03-27 05:05:59 +0100162
solenbergd97ec302015-10-07 01:40:33 -0700163std::string ToString(const webrtc::CodecInst& codec) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000164 std::stringstream ss;
165 ss << codec.plname << "/" << codec.plfreq << "/" << codec.channels
166 << " (" << codec.pltype << ")";
167 return ss.str();
168}
169
solenbergd97ec302015-10-07 01:40:33 -0700170bool IsCodec(const AudioCodec& codec, const char* ref_name) {
Minyue Li7100dcd2015-03-27 05:05:59 +0100171 return (_stricmp(codec.name.c_str(), ref_name) == 0);
172}
173
solenbergd97ec302015-10-07 01:40:33 -0700174bool IsCodec(const webrtc::CodecInst& codec, const char* ref_name) {
Minyue Li7100dcd2015-03-27 05:05:59 +0100175 return (_stricmp(codec.plname, ref_name) == 0);
176}
177
solenbergd97ec302015-10-07 01:40:33 -0700178bool FindCodec(const std::vector<AudioCodec>& codecs,
solenberg26c8c912015-11-27 04:00:25 -0800179 const AudioCodec& codec,
180 AudioCodec* found_codec) {
Fredrik Solenbergaf9fb212015-08-26 10:45:53 +0200181 for (const AudioCodec& c : codecs) {
182 if (c.Matches(codec)) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000183 if (found_codec != NULL) {
Fredrik Solenbergaf9fb212015-08-26 10:45:53 +0200184 *found_codec = c;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000185 }
186 return true;
187 }
188 }
189 return false;
190}
wu@webrtc.org1d1ffc92013-10-16 18:12:02 +0000191
solenberg0b675462015-10-09 01:37:09 -0700192bool VerifyUniquePayloadTypes(const std::vector<AudioCodec>& codecs) {
193 if (codecs.empty()) {
194 return true;
195 }
196 std::vector<int> payload_types;
197 for (const AudioCodec& codec : codecs) {
198 payload_types.push_back(codec.id);
199 }
200 std::sort(payload_types.begin(), payload_types.end());
201 auto it = std::unique(payload_types.begin(), payload_types.end());
202 return it == payload_types.end();
203}
204
solenbergd97ec302015-10-07 01:40:33 -0700205bool IsNackEnabled(const AudioCodec& codec) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000206 return codec.HasFeedbackParam(FeedbackParam(kRtcpFbParamNack,
207 kParamValueEmpty));
208}
209
Minyue Li7100dcd2015-03-27 05:05:59 +0100210// Return true if codec.params[feature] == "1", false otherwise.
solenberg26c8c912015-11-27 04:00:25 -0800211bool IsCodecFeatureEnabled(const AudioCodec& codec, const char* feature) {
Minyue Li7100dcd2015-03-27 05:05:59 +0100212 int value;
213 return codec.GetParam(feature, &value) && value == 1;
214}
215
216// Use params[kCodecParamMaxAverageBitrate] if it is defined, use codec.bitrate
217// otherwise. If the value (either from params or codec.bitrate) <=0, use the
218// default configuration. If the value is beyond feasible bit rate of Opus,
219// clamp it. Returns the Opus bit rate for operation.
solenbergd97ec302015-10-07 01:40:33 -0700220int GetOpusBitrate(const AudioCodec& codec, int max_playback_rate) {
Minyue Li7100dcd2015-03-27 05:05:59 +0100221 int bitrate = 0;
222 bool use_param = true;
223 if (!codec.GetParam(kCodecParamMaxAverageBitrate, &bitrate)) {
224 bitrate = codec.bitrate;
225 use_param = false;
226 }
227 if (bitrate <= 0) {
228 if (max_playback_rate <= 8000) {
229 bitrate = kOpusBitrateNb;
230 } else if (max_playback_rate <= 16000) {
231 bitrate = kOpusBitrateWb;
232 } else {
233 bitrate = kOpusBitrateFb;
234 }
235
236 if (IsCodecFeatureEnabled(codec, kCodecParamStereo)) {
237 bitrate *= 2;
238 }
239 } else if (bitrate < kOpusMinBitrate || bitrate > kOpusMaxBitrate) {
240 bitrate = (bitrate < kOpusMinBitrate) ? kOpusMinBitrate : kOpusMaxBitrate;
241 std::string rate_source =
242 use_param ? "Codec parameter \"maxaveragebitrate\"" :
243 "Supplied Opus bitrate";
244 LOG(LS_WARNING) << rate_source
245 << " is invalid and is replaced by: "
246 << bitrate;
247 }
248 return bitrate;
249}
250
251// Returns kOpusDefaultPlaybackRate if params[kCodecParamMaxPlaybackRate] is not
252// defined. Returns the value of params[kCodecParamMaxPlaybackRate] otherwise.
solenbergd97ec302015-10-07 01:40:33 -0700253int GetOpusMaxPlaybackRate(const AudioCodec& codec) {
Minyue Li7100dcd2015-03-27 05:05:59 +0100254 int value;
255 if (codec.GetParam(kCodecParamMaxPlaybackRate, &value)) {
256 return value;
257 }
258 return kOpusDefaultMaxPlaybackRate;
259}
260
solenbergd97ec302015-10-07 01:40:33 -0700261void GetOpusConfig(const AudioCodec& codec, webrtc::CodecInst* voe_codec,
Minyue Li7100dcd2015-03-27 05:05:59 +0100262 bool* enable_codec_fec, int* max_playback_rate,
263 bool* enable_codec_dtx) {
264 *enable_codec_fec = IsCodecFeatureEnabled(codec, kCodecParamUseInbandFec);
265 *enable_codec_dtx = IsCodecFeatureEnabled(codec, kCodecParamUseDtx);
266 *max_playback_rate = GetOpusMaxPlaybackRate(codec);
267
268 // If OPUS, change what we send according to the "stereo" codec
269 // parameter, and not the "channels" parameter. We set
270 // voe_codec.channels to 2 if "stereo=1" and 1 otherwise. If
271 // the bitrate is not specified, i.e. is <= zero, we set it to the
272 // appropriate default value for mono or stereo Opus.
273
274 voe_codec->channels = IsCodecFeatureEnabled(codec, kCodecParamStereo) ? 2 : 1;
275 voe_codec->rate = GetOpusBitrate(codec, *max_playback_rate);
276}
277
wu@webrtc.org1d1ffc92013-10-16 18:12:02 +0000278// Gets the default set of options applied to the engine. Historically, these
279// were supplied as a combination of flags from the channel manager (ec, agc,
280// ns, and highpass) and the rest hardcoded in InitInternal.
solenbergd97ec302015-10-07 01:40:33 -0700281AudioOptions GetDefaultEngineOptions() {
wu@webrtc.org1d1ffc92013-10-16 18:12:02 +0000282 AudioOptions options;
Karl Wibergbe579832015-11-10 22:34:18 +0100283 options.echo_cancellation = rtc::Optional<bool>(true);
284 options.auto_gain_control = rtc::Optional<bool>(true);
285 options.noise_suppression = rtc::Optional<bool>(true);
286 options.highpass_filter = rtc::Optional<bool>(true);
287 options.stereo_swapping = rtc::Optional<bool>(false);
288 options.audio_jitter_buffer_max_packets = rtc::Optional<int>(50);
289 options.audio_jitter_buffer_fast_accelerate = rtc::Optional<bool>(false);
290 options.typing_detection = rtc::Optional<bool>(true);
291 options.adjust_agc_delta = rtc::Optional<int>(0);
292 options.experimental_agc = rtc::Optional<bool>(false);
293 options.extended_filter_aec = rtc::Optional<bool>(false);
294 options.delay_agnostic_aec = rtc::Optional<bool>(false);
295 options.experimental_ns = rtc::Optional<bool>(false);
296 options.aec_dump = rtc::Optional<bool>(false);
wu@webrtc.org1d1ffc92013-10-16 18:12:02 +0000297 return options;
298}
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000299
solenberg566ef242015-11-06 15:34:49 -0800300webrtc::AudioState::Config MakeAudioStateConfig(VoEWrapper* voe_wrapper) {
301 webrtc::AudioState::Config config;
302 config.voice_engine = voe_wrapper->engine();
303 return config;
304}
305
solenberg26c8c912015-11-27 04:00:25 -0800306class WebRtcVoiceCodecs final {
307 public:
308 // TODO(solenberg): Do this filtering once off-line, add a simple AudioCodec
309 // list and add a test which verifies VoE supports the listed codecs.
310 static std::vector<AudioCodec> SupportedCodecs() {
311 LOG(LS_INFO) << "WebRtc VoiceEngine codecs:";
312 std::vector<AudioCodec> result;
313 for (webrtc::CodecInst voe_codec : webrtc::acm2::RentACodec::Database()) {
314 // Change the sample rate of G722 to 8000 to match SDP.
315 MaybeFixupG722(&voe_codec, 8000);
buildbot@webrtc.org13d67762014-05-02 17:33:29 +0000316 // Skip uncompressed formats.
Minyue Li7100dcd2015-03-27 05:05:59 +0100317 if (IsCodec(voe_codec, kL16CodecName)) {
buildbot@webrtc.org13d67762014-05-02 17:33:29 +0000318 continue;
319 }
320
321 const CodecPref* pref = NULL;
tfarina5237aaf2015-11-10 23:44:30 -0800322 for (size_t j = 0; j < arraysize(kCodecPrefs); ++j) {
Minyue Li7100dcd2015-03-27 05:05:59 +0100323 if (IsCodec(voe_codec, kCodecPrefs[j].name) &&
buildbot@webrtc.org13d67762014-05-02 17:33:29 +0000324 kCodecPrefs[j].clockrate == voe_codec.plfreq &&
325 kCodecPrefs[j].channels == voe_codec.channels) {
326 pref = &kCodecPrefs[j];
327 break;
328 }
329 }
330
331 if (pref) {
332 // Use the payload type that we've configured in our pref table;
333 // use the offset in our pref table to determine the sort order.
tfarina5237aaf2015-11-10 23:44:30 -0800334 AudioCodec codec(
335 pref->payload_type, voe_codec.plname, voe_codec.plfreq,
336 voe_codec.rate, voe_codec.channels,
337 static_cast<int>(arraysize(kCodecPrefs)) - (pref - kCodecPrefs));
buildbot@webrtc.org13d67762014-05-02 17:33:29 +0000338 LOG(LS_INFO) << ToString(codec);
Minyue Li7100dcd2015-03-27 05:05:59 +0100339 if (IsCodec(codec, kIsacCodecName)) {
minyue@webrtc.org26236952014-10-29 02:27:08 +0000340 // Indicate auto-bitrate in signaling.
buildbot@webrtc.org13d67762014-05-02 17:33:29 +0000341 codec.bitrate = 0;
342 }
Minyue Li7100dcd2015-03-27 05:05:59 +0100343 if (IsCodec(codec, kOpusCodecName)) {
buildbot@webrtc.org13d67762014-05-02 17:33:29 +0000344 // Only add fmtp parameters that differ from the spec.
345 if (kPreferredMinPTime != kOpusDefaultMinPTime) {
346 codec.params[kCodecParamMinPTime] =
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000347 rtc::ToString(kPreferredMinPTime);
buildbot@webrtc.org13d67762014-05-02 17:33:29 +0000348 }
349 if (kPreferredMaxPTime != kOpusDefaultMaxPTime) {
350 codec.params[kCodecParamMaxPTime] =
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000351 rtc::ToString(kPreferredMaxPTime);
buildbot@webrtc.org13d67762014-05-02 17:33:29 +0000352 }
pkasting@chromium.orgd3245462015-02-23 21:28:22 +0000353 codec.SetParam(kCodecParamUseInbandFec, 1);
minyue@webrtc.org4ef22d12014-11-17 09:26:39 +0000354
355 // TODO(hellner): Add ptime, sprop-stereo, and stereo
buildbot@webrtc.org13d67762014-05-02 17:33:29 +0000356 // when they can be set to values other than the default.
357 }
solenberg26c8c912015-11-27 04:00:25 -0800358 result.push_back(codec);
buildbot@webrtc.org13d67762014-05-02 17:33:29 +0000359 } else {
360 LOG(LS_WARNING) << "Unexpected codec: " << ToString(voe_codec);
361 }
362 }
solenberg26c8c912015-11-27 04:00:25 -0800363 // Make sure they are in local preference order.
364 std::sort(result.begin(), result.end(), &AudioCodec::Preferable);
365 return result;
buildbot@webrtc.org13d67762014-05-02 17:33:29 +0000366 }
buildbot@webrtc.org13d67762014-05-02 17:33:29 +0000367
solenberg26c8c912015-11-27 04:00:25 -0800368 static bool ToCodecInst(const AudioCodec& in,
369 webrtc::CodecInst* out) {
370 for (webrtc::CodecInst voe_codec : webrtc::acm2::RentACodec::Database()) {
371 // Change the sample rate of G722 to 8000 to match SDP.
372 MaybeFixupG722(&voe_codec, 8000);
373 AudioCodec codec(voe_codec.pltype, voe_codec.plname, voe_codec.plfreq,
374 voe_codec.rate, voe_codec.channels, 0);
375 bool multi_rate = IsCodecMultiRate(voe_codec);
376 // Allow arbitrary rates for ISAC to be specified.
377 if (multi_rate) {
378 // Set codec.bitrate to 0 so the check for codec.Matches() passes.
379 codec.bitrate = 0;
380 }
381 if (codec.Matches(in)) {
382 if (out) {
383 // Fixup the payload type.
384 voe_codec.pltype = in.id;
385
386 // Set bitrate if specified.
387 if (multi_rate && in.bitrate != 0) {
388 voe_codec.rate = in.bitrate;
389 }
390
391 // Reset G722 sample rate to 16000 to match WebRTC.
392 MaybeFixupG722(&voe_codec, 16000);
393
394 // Apply codec-specific settings.
395 if (IsCodec(codec, kIsacCodecName)) {
396 // If ISAC and an explicit bitrate is not specified,
397 // enable auto bitrate adjustment.
398 voe_codec.rate = (in.bitrate > 0) ? in.bitrate : -1;
399 }
400 *out = voe_codec;
401 }
402 return true;
403 }
404 }
henrik.lundin@webrtc.org8038d422014-11-11 08:38:24 +0000405 return false;
henrik.lundin@webrtc.orgf85dbce2014-11-07 12:25:00 +0000406 }
solenberg26c8c912015-11-27 04:00:25 -0800407
408 static bool IsCodecMultiRate(const webrtc::CodecInst& codec) {
409 for (size_t i = 0; i < arraysize(kCodecPrefs); ++i) {
410 if (IsCodec(codec, kCodecPrefs[i].name) &&
411 kCodecPrefs[i].clockrate == codec.plfreq) {
412 return kCodecPrefs[i].is_multi_rate;
413 }
414 }
415 return false;
416 }
417
418 // If the AudioCodec param kCodecParamPTime is set, then we will set it to
419 // codec pacsize if it's valid, or we will pick the next smallest value we
420 // support.
421 // TODO(Brave): Query supported packet sizes from ACM when the API is ready.
422 static bool SetPTimeAsPacketSize(webrtc::CodecInst* codec, int ptime_ms) {
423 for (const CodecPref& codec_pref : kCodecPrefs) {
424 if ((IsCodec(*codec, codec_pref.name) &&
425 codec_pref.clockrate == codec->plfreq) ||
426 IsCodec(*codec, kG722CodecName)) {
427 int packet_size_ms = SelectPacketSize(codec_pref, ptime_ms);
428 if (packet_size_ms) {
429 // Convert unit from milli-seconds to samples.
430 codec->pacsize = (codec->plfreq / 1000) * packet_size_ms;
431 return true;
432 }
433 }
434 }
435 return false;
436 }
437
438 private:
439 static const int kMaxNumPacketSize = 6;
440 struct CodecPref {
441 const char* name;
442 int clockrate;
443 int channels;
444 int payload_type;
445 bool is_multi_rate;
446 int packet_sizes_ms[kMaxNumPacketSize];
447 };
448 // Note: keep the supported packet sizes in ascending order.
449 static const CodecPref kCodecPrefs[12];
450
451 static int SelectPacketSize(const CodecPref& codec_pref, int ptime_ms) {
452 int selected_packet_size_ms = codec_pref.packet_sizes_ms[0];
453 for (int packet_size_ms : codec_pref.packet_sizes_ms) {
454 if (packet_size_ms && packet_size_ms <= ptime_ms) {
455 selected_packet_size_ms = packet_size_ms;
456 }
457 }
458 return selected_packet_size_ms;
459 }
460
461 // Changes RTP timestamp rate of G722. This is due to the "bug" in the RFC
462 // which says that G722 should be advertised as 8 kHz although it is a 16 kHz
463 // codec.
464 static void MaybeFixupG722(webrtc::CodecInst* voe_codec, int new_plfreq) {
465 if (IsCodec(*voe_codec, kG722CodecName)) {
466 // If the ASSERT triggers, the codec definition in WebRTC VoiceEngine
467 // has changed, and this special case is no longer needed.
468 RTC_DCHECK(voe_codec->plfreq != new_plfreq);
469 voe_codec->plfreq = new_plfreq;
470 }
471 }
472};
473
474const WebRtcVoiceCodecs::CodecPref WebRtcVoiceCodecs::kCodecPrefs[12] = {
475 { kOpusCodecName, 48000, 2, 111, true, { 10, 20, 40, 60 } },
476 { kIsacCodecName, 16000, 1, 103, true, { 30, 60 } },
477 { kIsacCodecName, 32000, 1, 104, true, { 30 } },
478 // G722 should be advertised as 8000 Hz because of the RFC "bug".
479 { kG722CodecName, 8000, 1, 9, false, { 10, 20, 30, 40, 50, 60 } },
480 { kIlbcCodecName, 8000, 1, 102, false, { 20, 30, 40, 60 } },
481 { kPcmuCodecName, 8000, 1, 0, false, { 10, 20, 30, 40, 50, 60 } },
482 { kPcmaCodecName, 8000, 1, 8, false, { 10, 20, 30, 40, 50, 60 } },
483 { kCnCodecName, 32000, 1, 106, false, { } },
484 { kCnCodecName, 16000, 1, 105, false, { } },
485 { kCnCodecName, 8000, 1, 13, false, { } },
486 { kRedCodecName, 8000, 1, 127, false, { } },
487 { kDtmfCodecName, 8000, 1, 126, false, { } },
488};
489} // namespace {
490
491bool WebRtcVoiceEngine::ToCodecInst(const AudioCodec& in,
492 webrtc::CodecInst* out) {
493 return WebRtcVoiceCodecs::ToCodecInst(in, out);
494}
495
496WebRtcVoiceEngine::WebRtcVoiceEngine()
497 : voe_wrapper_(new VoEWrapper()),
498 audio_state_(webrtc::AudioState::Create(MakeAudioStateConfig(voe()))) {
499 Construct();
500}
501
502WebRtcVoiceEngine::WebRtcVoiceEngine(VoEWrapper* voe_wrapper)
503 : voe_wrapper_(voe_wrapper) {
504 Construct();
505}
506
507void WebRtcVoiceEngine::Construct() {
508 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
509 LOG(LS_VERBOSE) << "WebRtcVoiceEngine::WebRtcVoiceEngine";
510
511 signal_thread_checker_.DetachFromThread();
512 std::memset(&default_agc_config_, 0, sizeof(default_agc_config_));
513
514 webrtc::Trace::set_level_filter(kDefaultTraceFilter);
515 webrtc::Trace::SetTraceCallback(this);
516
517 // Load our audio codec list.
518 codecs_ = WebRtcVoiceCodecs::SupportedCodecs();
519
520 // Load our RTP Header extensions.
521 rtp_header_extensions_.push_back(
522 RtpHeaderExtension(kRtpAudioLevelHeaderExtension,
523 kRtpAudioLevelHeaderExtensionDefaultId));
524 rtp_header_extensions_.push_back(
525 RtpHeaderExtension(kRtpAbsoluteSenderTimeHeaderExtension,
526 kRtpAbsoluteSenderTimeHeaderExtensionDefaultId));
527 if (webrtc::field_trial::FindFullName("WebRTC-SendSideBwe") == "Enabled") {
528 rtp_header_extensions_.push_back(RtpHeaderExtension(
529 kRtpTransportSequenceNumberHeaderExtension,
530 kRtpTransportSequenceNumberHeaderExtensionDefaultId));
531 }
532 options_ = GetDefaultEngineOptions();
Stefan Holmerb86d4e42015-12-07 10:26:18 +0100533 voe_config_.Set<webrtc::VoicePacing>(new webrtc::VoicePacing(true));
henrik.lundin@webrtc.orgf85dbce2014-11-07 12:25:00 +0000534}
535
buildbot@webrtc.org13d67762014-05-02 17:33:29 +0000536WebRtcVoiceEngine::~WebRtcVoiceEngine() {
solenberg566ef242015-11-06 15:34:49 -0800537 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
buildbot@webrtc.org13d67762014-05-02 17:33:29 +0000538 LOG(LS_VERBOSE) << "WebRtcVoiceEngine::~WebRtcVoiceEngine";
buildbot@webrtc.org13d67762014-05-02 17:33:29 +0000539 if (adm_) {
540 voe_wrapper_.reset();
541 adm_->Release();
542 adm_ = NULL;
543 }
solenbergbd138382015-11-20 16:08:07 -0800544 webrtc::Trace::SetTraceCallback(nullptr);
buildbot@webrtc.org13d67762014-05-02 17:33:29 +0000545}
546
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000547bool WebRtcVoiceEngine::Init(rtc::Thread* worker_thread) {
solenberg566ef242015-11-06 15:34:49 -0800548 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
henrikg91d6ede2015-09-17 00:24:34 -0700549 RTC_DCHECK(worker_thread == rtc::Thread::Current());
buildbot@webrtc.org13d67762014-05-02 17:33:29 +0000550 LOG(LS_INFO) << "WebRtcVoiceEngine::Init";
551 bool res = InitInternal();
552 if (res) {
553 LOG(LS_INFO) << "WebRtcVoiceEngine::Init Done!";
554 } else {
555 LOG(LS_ERROR) << "WebRtcVoiceEngine::Init failed";
556 Terminate();
557 }
558 return res;
559}
560
561bool WebRtcVoiceEngine::InitInternal() {
solenberg566ef242015-11-06 15:34:49 -0800562 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
buildbot@webrtc.org13d67762014-05-02 17:33:29 +0000563 // Temporarily turn logging level up for the Init call
solenbergbd138382015-11-20 16:08:07 -0800564 webrtc::Trace::set_level_filter(kElevatedTraceFilter);
solenberg2515af22015-12-02 06:19:36 -0800565 LOG(LS_INFO) << webrtc::VoiceEngine::GetVersionString();
buildbot@webrtc.org13d67762014-05-02 17:33:29 +0000566 if (voe_wrapper_->base()->Init(adm_) == -1) {
567 LOG_RTCERR0_EX(Init, voe_wrapper_->error());
buildbot@webrtc.org13d67762014-05-02 17:33:29 +0000568 return false;
569 }
solenbergbd138382015-11-20 16:08:07 -0800570 webrtc::Trace::set_level_filter(kDefaultTraceFilter);
buildbot@webrtc.org13d67762014-05-02 17:33:29 +0000571
buildbot@webrtc.org13d67762014-05-02 17:33:29 +0000572 // Save the default AGC configuration settings. This must happen before
573 // calling SetOptions or the default will be overwritten.
574 if (voe_wrapper_->processing()->GetAgcConfig(default_agc_config_) == -1) {
575 LOG_RTCERR0(GetAgcConfig);
576 return false;
577 }
578
579 // Set defaults for options, so that ApplyOptions applies them explicitly
580 // when we clear option (channel) overrides. External clients can still
581 // modify the defaults via SetOptions (on the media engine).
582 if (!SetOptions(GetDefaultEngineOptions())) {
583 return false;
584 }
585
586 // Print our codec list again for the call diagnostic log
587 LOG(LS_INFO) << "WebRtc VoiceEngine codecs:";
Fredrik Solenbergaf9fb212015-08-26 10:45:53 +0200588 for (const AudioCodec& codec : codecs_) {
589 LOG(LS_INFO) << ToString(codec);
buildbot@webrtc.org13d67762014-05-02 17:33:29 +0000590 }
591
buildbot@webrtc.org13d67762014-05-02 17:33:29 +0000592 initialized_ = true;
593 return true;
594}
595
buildbot@webrtc.org13d67762014-05-02 17:33:29 +0000596void WebRtcVoiceEngine::Terminate() {
solenberg566ef242015-11-06 15:34:49 -0800597 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
buildbot@webrtc.org13d67762014-05-02 17:33:29 +0000598 LOG(LS_INFO) << "WebRtcVoiceEngine::Terminate";
599 initialized_ = false;
600
601 StopAecDump();
602
buildbot@webrtc.org13d67762014-05-02 17:33:29 +0000603 voe_wrapper_->base()->Terminate();
buildbot@webrtc.org13d67762014-05-02 17:33:29 +0000604}
605
solenberg566ef242015-11-06 15:34:49 -0800606rtc::scoped_refptr<webrtc::AudioState>
607 WebRtcVoiceEngine::GetAudioState() const {
608 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
609 return audio_state_;
610}
611
Fredrik Solenberg709ed672015-09-15 12:26:33 +0200612VoiceMediaChannel* WebRtcVoiceEngine::CreateChannel(webrtc::Call* call,
Jelena Marusicc28a8962015-05-29 15:05:44 +0200613 const AudioOptions& options) {
solenberg566ef242015-11-06 15:34:49 -0800614 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
solenberg0a617e22015-10-20 15:49:38 -0700615 return new WebRtcVoiceMediaChannel(this, options, call);
buildbot@webrtc.org13d67762014-05-02 17:33:29 +0000616}
617
buildbot@webrtc.org13d67762014-05-02 17:33:29 +0000618bool WebRtcVoiceEngine::SetOptions(const AudioOptions& options) {
solenberg566ef242015-11-06 15:34:49 -0800619 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
buildbot@webrtc.org13d67762014-05-02 17:33:29 +0000620 if (!ApplyOptions(options)) {
621 return false;
622 }
623 options_ = options;
624 return true;
625}
626
buildbot@webrtc.org13d67762014-05-02 17:33:29 +0000627// AudioOptions defaults are set in InitInternal (for options with corresponding
628// MediaEngineInterface flags) and in SetOptions(int) for flagless options.
629bool WebRtcVoiceEngine::ApplyOptions(const AudioOptions& options_in) {
solenberg566ef242015-11-06 15:34:49 -0800630 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
henrikac14f5ff2015-09-23 14:08:33 +0200631 LOG(LS_INFO) << "ApplyOptions: " << options_in.ToString();
buildbot@webrtc.org13d67762014-05-02 17:33:29 +0000632 AudioOptions options = options_in; // The options are modified below.
633 // kEcConference is AEC with high suppression.
634 webrtc::EcModes ec_mode = webrtc::kEcConference;
635 webrtc::AecmModes aecm_mode = webrtc::kAecmSpeakerphone;
636 webrtc::AgcModes agc_mode = webrtc::kAgcAdaptiveAnalog;
637 webrtc::NsModes ns_mode = webrtc::kNsHighSuppression;
kwiberg102c6a62015-10-30 02:47:38 -0700638 if (options.aecm_generate_comfort_noise) {
buildbot@webrtc.org13d67762014-05-02 17:33:29 +0000639 LOG(LS_VERBOSE) << "Comfort noise explicitly set to "
kwiberg102c6a62015-10-30 02:47:38 -0700640 << *options.aecm_generate_comfort_noise
641 << " (default is false).";
buildbot@webrtc.org13d67762014-05-02 17:33:29 +0000642 }
643
644#if defined(IOS)
645 // On iOS, VPIO provides built-in EC and AGC.
Karl Wibergbe579832015-11-10 22:34:18 +0100646 options.echo_cancellation = rtc::Optional<bool>(false);
647 options.auto_gain_control = rtc::Optional<bool>(false);
henrika86d907c2015-09-07 16:09:50 +0200648 LOG(LS_INFO) << "Always disable AEC and AGC on iOS. Use built-in instead.";
buildbot@webrtc.org13d67762014-05-02 17:33:29 +0000649#elif defined(ANDROID)
650 ec_mode = webrtc::kEcAecm;
651#endif
652
653#if defined(IOS) || defined(ANDROID)
654 // Set the AGC mode for iOS as well despite disabling it above, to avoid
655 // unsupported configuration errors from webrtc.
656 agc_mode = webrtc::kAgcFixedDigital;
Karl Wibergbe579832015-11-10 22:34:18 +0100657 options.typing_detection = rtc::Optional<bool>(false);
658 options.experimental_agc = rtc::Optional<bool>(false);
659 options.extended_filter_aec = rtc::Optional<bool>(false);
660 options.experimental_ns = rtc::Optional<bool>(false);
buildbot@webrtc.org13d67762014-05-02 17:33:29 +0000661#endif
662
Bjorn Volckerbf395c12015-03-25 22:45:56 +0100663 // Delay Agnostic AEC automatically turns on EC if not set except on iOS
664 // where the feature is not supported.
665 bool use_delay_agnostic_aec = false;
666#if !defined(IOS)
kwiberg102c6a62015-10-30 02:47:38 -0700667 if (options.delay_agnostic_aec) {
668 use_delay_agnostic_aec = *options.delay_agnostic_aec;
Bjorn Volckerbf395c12015-03-25 22:45:56 +0100669 if (use_delay_agnostic_aec) {
Karl Wibergbe579832015-11-10 22:34:18 +0100670 options.echo_cancellation = rtc::Optional<bool>(true);
671 options.extended_filter_aec = rtc::Optional<bool>(true);
Bjorn Volckerbf395c12015-03-25 22:45:56 +0100672 ec_mode = webrtc::kEcConference;
673 }
674 }
675#endif
676
buildbot@webrtc.org13d67762014-05-02 17:33:29 +0000677 webrtc::VoEAudioProcessing* voep = voe_wrapper_->processing();
678
kwiberg102c6a62015-10-30 02:47:38 -0700679 if (options.echo_cancellation) {
henrika@webrtc.orga954c072014-12-09 16:22:09 +0000680 // Check if platform supports built-in EC. Currently only supported on
681 // Android and in combination with Java based audio layer.
682 // TODO(henrika): investigate possibility to support built-in EC also
683 // in combination with Open SL ES audio.
684 const bool built_in_aec = voe_wrapper_->hw()->BuiltInAECIsAvailable();
Bjorn Volcker73f72102015-06-03 14:50:15 +0200685 if (built_in_aec) {
Bjorn Volckerccfc9392015-05-07 07:43:17 +0200686 // Built-in EC exists on this device and use_delay_agnostic_aec is not
687 // overriding it. Enable/Disable it according to the echo_cancellation
688 // audio option.
Bjorn Volcker73f72102015-06-03 14:50:15 +0200689 const bool enable_built_in_aec =
kwiberg102c6a62015-10-30 02:47:38 -0700690 *options.echo_cancellation && !use_delay_agnostic_aec;
Bjorn Volcker73f72102015-06-03 14:50:15 +0200691 if (voe_wrapper_->hw()->EnableBuiltInAEC(enable_built_in_aec) == 0 &&
692 enable_built_in_aec) {
Bjorn Volckerbf395c12015-03-25 22:45:56 +0100693 // Disable internal software EC if built-in EC is enabled,
henrika@webrtc.orga954c072014-12-09 16:22:09 +0000694 // i.e., replace the software EC with the built-in EC.
Karl Wibergbe579832015-11-10 22:34:18 +0100695 options.echo_cancellation = rtc::Optional<bool>(false);
henrika@webrtc.orga954c072014-12-09 16:22:09 +0000696 LOG(LS_INFO) << "Disabling EC since built-in EC will be used instead";
697 }
698 }
kwiberg102c6a62015-10-30 02:47:38 -0700699 if (voep->SetEcStatus(*options.echo_cancellation, ec_mode) == -1) {
700 LOG_RTCERR2(SetEcStatus, *options.echo_cancellation, ec_mode);
buildbot@webrtc.org13d67762014-05-02 17:33:29 +0000701 return false;
702 } else {
kwiberg102c6a62015-10-30 02:47:38 -0700703 LOG(LS_INFO) << "Echo control set to " << *options.echo_cancellation
henrika86d907c2015-09-07 16:09:50 +0200704 << " with mode " << ec_mode;
buildbot@webrtc.org13d67762014-05-02 17:33:29 +0000705 }
706#if !defined(ANDROID)
707 // TODO(ajm): Remove the error return on Android from webrtc.
kwiberg102c6a62015-10-30 02:47:38 -0700708 if (voep->SetEcMetricsStatus(*options.echo_cancellation) == -1) {
709 LOG_RTCERR1(SetEcMetricsStatus, *options.echo_cancellation);
buildbot@webrtc.org13d67762014-05-02 17:33:29 +0000710 return false;
711 }
712#endif
713 if (ec_mode == webrtc::kEcAecm) {
kwiberg102c6a62015-10-30 02:47:38 -0700714 bool cn = options.aecm_generate_comfort_noise.value_or(false);
715 if (voep->SetAecmMode(aecm_mode, cn) != 0) {
716 LOG_RTCERR2(SetAecmMode, aecm_mode, cn);
buildbot@webrtc.org13d67762014-05-02 17:33:29 +0000717 return false;
718 }
719 }
720 }
721
kwiberg102c6a62015-10-30 02:47:38 -0700722 if (options.auto_gain_control) {
henrikac14f5ff2015-09-23 14:08:33 +0200723 const bool built_in_agc = voe_wrapper_->hw()->BuiltInAGCIsAvailable();
724 if (built_in_agc) {
kwiberg102c6a62015-10-30 02:47:38 -0700725 if (voe_wrapper_->hw()->EnableBuiltInAGC(*options.auto_gain_control) ==
726 0 &&
727 *options.auto_gain_control) {
henrikac14f5ff2015-09-23 14:08:33 +0200728 // Disable internal software AGC if built-in AGC is enabled,
729 // i.e., replace the software AGC with the built-in AGC.
Karl Wibergbe579832015-11-10 22:34:18 +0100730 options.auto_gain_control = rtc::Optional<bool>(false);
henrikac14f5ff2015-09-23 14:08:33 +0200731 LOG(LS_INFO) << "Disabling AGC since built-in AGC will be used instead";
732 }
733 }
kwiberg102c6a62015-10-30 02:47:38 -0700734 if (voep->SetAgcStatus(*options.auto_gain_control, agc_mode) == -1) {
735 LOG_RTCERR2(SetAgcStatus, *options.auto_gain_control, agc_mode);
buildbot@webrtc.org13d67762014-05-02 17:33:29 +0000736 return false;
737 } else {
kwiberg102c6a62015-10-30 02:47:38 -0700738 LOG(LS_INFO) << "Auto gain set to " << *options.auto_gain_control
739 << " with mode " << agc_mode;
buildbot@webrtc.org13d67762014-05-02 17:33:29 +0000740 }
741 }
742
kwiberg102c6a62015-10-30 02:47:38 -0700743 if (options.tx_agc_target_dbov || options.tx_agc_digital_compression_gain ||
744 options.tx_agc_limiter) {
buildbot@webrtc.org13d67762014-05-02 17:33:29 +0000745 // Override default_agc_config_. Generally, an unset option means "leave
746 // the VoE bits alone" in this function, so we want whatever is set to be
747 // stored as the new "default". If we didn't, then setting e.g.
748 // tx_agc_target_dbov would reset digital compression gain and limiter
749 // settings.
750 // Also, if we don't update default_agc_config_, then adjust_agc_delta
751 // would be an offset from the original values, and not whatever was set
752 // explicitly.
kwiberg102c6a62015-10-30 02:47:38 -0700753 default_agc_config_.targetLeveldBOv = options.tx_agc_target_dbov.value_or(
754 default_agc_config_.targetLeveldBOv);
buildbot@webrtc.org13d67762014-05-02 17:33:29 +0000755 default_agc_config_.digitalCompressionGaindB =
kwiberg102c6a62015-10-30 02:47:38 -0700756 options.tx_agc_digital_compression_gain.value_or(
buildbot@webrtc.org13d67762014-05-02 17:33:29 +0000757 default_agc_config_.digitalCompressionGaindB);
758 default_agc_config_.limiterEnable =
kwiberg102c6a62015-10-30 02:47:38 -0700759 options.tx_agc_limiter.value_or(default_agc_config_.limiterEnable);
buildbot@webrtc.org13d67762014-05-02 17:33:29 +0000760 if (voe_wrapper_->processing()->SetAgcConfig(default_agc_config_) == -1) {
761 LOG_RTCERR3(SetAgcConfig,
762 default_agc_config_.targetLeveldBOv,
763 default_agc_config_.digitalCompressionGaindB,
764 default_agc_config_.limiterEnable);
765 return false;
766 }
767 }
768
kwiberg102c6a62015-10-30 02:47:38 -0700769 if (options.noise_suppression) {
henrikac14f5ff2015-09-23 14:08:33 +0200770 const bool built_in_ns = voe_wrapper_->hw()->BuiltInNSIsAvailable();
771 if (built_in_ns) {
kwiberg102c6a62015-10-30 02:47:38 -0700772 if (voe_wrapper_->hw()->EnableBuiltInNS(*options.noise_suppression) ==
773 0 &&
774 *options.noise_suppression) {
henrikac14f5ff2015-09-23 14:08:33 +0200775 // Disable internal software NS if built-in NS is enabled,
776 // i.e., replace the software NS with the built-in NS.
Karl Wibergbe579832015-11-10 22:34:18 +0100777 options.noise_suppression = rtc::Optional<bool>(false);
henrikac14f5ff2015-09-23 14:08:33 +0200778 LOG(LS_INFO) << "Disabling NS since built-in NS will be used instead";
779 }
780 }
kwiberg102c6a62015-10-30 02:47:38 -0700781 if (voep->SetNsStatus(*options.noise_suppression, ns_mode) == -1) {
782 LOG_RTCERR2(SetNsStatus, *options.noise_suppression, ns_mode);
buildbot@webrtc.org13d67762014-05-02 17:33:29 +0000783 return false;
784 } else {
kwiberg102c6a62015-10-30 02:47:38 -0700785 LOG(LS_INFO) << "Noise suppression set to " << *options.noise_suppression
henrikac14f5ff2015-09-23 14:08:33 +0200786 << " with mode " << ns_mode;
buildbot@webrtc.org13d67762014-05-02 17:33:29 +0000787 }
788 }
789
kwiberg102c6a62015-10-30 02:47:38 -0700790 if (options.highpass_filter) {
791 LOG(LS_INFO) << "High pass filter enabled? " << *options.highpass_filter;
792 if (voep->EnableHighPassFilter(*options.highpass_filter) == -1) {
793 LOG_RTCERR1(SetHighpassFilterStatus, *options.highpass_filter);
buildbot@webrtc.org13d67762014-05-02 17:33:29 +0000794 return false;
795 }
796 }
797
kwiberg102c6a62015-10-30 02:47:38 -0700798 if (options.stereo_swapping) {
799 LOG(LS_INFO) << "Stereo swapping enabled? " << *options.stereo_swapping;
800 voep->EnableStereoChannelSwapping(*options.stereo_swapping);
801 if (voep->IsStereoChannelSwappingEnabled() != *options.stereo_swapping) {
802 LOG_RTCERR1(EnableStereoChannelSwapping, *options.stereo_swapping);
buildbot@webrtc.org13d67762014-05-02 17:33:29 +0000803 return false;
804 }
805 }
806
kwiberg102c6a62015-10-30 02:47:38 -0700807 if (options.audio_jitter_buffer_max_packets) {
808 LOG(LS_INFO) << "NetEq capacity is "
809 << *options.audio_jitter_buffer_max_packets;
Henrik Lundin64dad832015-05-11 12:44:23 +0200810 voe_config_.Set<webrtc::NetEqCapacityConfig>(
kwiberg102c6a62015-10-30 02:47:38 -0700811 new webrtc::NetEqCapacityConfig(
812 *options.audio_jitter_buffer_max_packets));
Henrik Lundin64dad832015-05-11 12:44:23 +0200813 }
814
kwiberg102c6a62015-10-30 02:47:38 -0700815 if (options.audio_jitter_buffer_fast_accelerate) {
816 LOG(LS_INFO) << "NetEq fast mode? "
817 << *options.audio_jitter_buffer_fast_accelerate;
Henrik Lundin5263b3c2015-06-01 10:29:41 +0200818 voe_config_.Set<webrtc::NetEqFastAccelerate>(
kwiberg102c6a62015-10-30 02:47:38 -0700819 new webrtc::NetEqFastAccelerate(
820 *options.audio_jitter_buffer_fast_accelerate));
Henrik Lundin5263b3c2015-06-01 10:29:41 +0200821 }
822
kwiberg102c6a62015-10-30 02:47:38 -0700823 if (options.typing_detection) {
824 LOG(LS_INFO) << "Typing detection is enabled? "
825 << *options.typing_detection;
826 if (voep->SetTypingDetectionStatus(*options.typing_detection) == -1) {
buildbot@webrtc.org13d67762014-05-02 17:33:29 +0000827 // In case of error, log the info and continue
kwiberg102c6a62015-10-30 02:47:38 -0700828 LOG_RTCERR1(SetTypingDetectionStatus, *options.typing_detection);
buildbot@webrtc.org13d67762014-05-02 17:33:29 +0000829 }
830 }
831
kwiberg102c6a62015-10-30 02:47:38 -0700832 if (options.adjust_agc_delta) {
833 LOG(LS_INFO) << "Adjust agc delta is " << *options.adjust_agc_delta;
834 if (!AdjustAgcLevel(*options.adjust_agc_delta)) {
buildbot@webrtc.org13d67762014-05-02 17:33:29 +0000835 return false;
836 }
837 }
838
kwiberg102c6a62015-10-30 02:47:38 -0700839 if (options.aec_dump) {
840 LOG(LS_INFO) << "Aec dump is enabled? " << *options.aec_dump;
841 if (*options.aec_dump)
buildbot@webrtc.org13d67762014-05-02 17:33:29 +0000842 StartAecDump(kAecDumpByAudioOptionFilename);
843 else
844 StopAecDump();
845 }
846
buildbot@webrtc.org1f8a2372014-08-28 10:52:44 +0000847 webrtc::Config config;
848
kwiberg102c6a62015-10-30 02:47:38 -0700849 if (options.delay_agnostic_aec)
850 delay_agnostic_aec_ = options.delay_agnostic_aec;
851 if (delay_agnostic_aec_) {
852 LOG(LS_INFO) << "Delay agnostic aec is enabled? " << *delay_agnostic_aec_;
henrik.lundin0f133b92015-07-02 00:17:55 -0700853 config.Set<webrtc::DelayAgnostic>(
kwiberg102c6a62015-10-30 02:47:38 -0700854 new webrtc::DelayAgnostic(*delay_agnostic_aec_));
Bjorn Volckerbf395c12015-03-25 22:45:56 +0100855 }
856
kwiberg102c6a62015-10-30 02:47:38 -0700857 if (options.extended_filter_aec) {
858 extended_filter_aec_ = options.extended_filter_aec;
859 }
860 if (extended_filter_aec_) {
861 LOG(LS_INFO) << "Extended filter aec is enabled? " << *extended_filter_aec_;
Henrik Lundin441f6342015-06-09 16:03:13 +0200862 config.Set<webrtc::ExtendedFilter>(
kwiberg102c6a62015-10-30 02:47:38 -0700863 new webrtc::ExtendedFilter(*extended_filter_aec_));
buildbot@webrtc.org1f8a2372014-08-28 10:52:44 +0000864 }
865
kwiberg102c6a62015-10-30 02:47:38 -0700866 if (options.experimental_ns) {
867 experimental_ns_ = options.experimental_ns;
868 }
869 if (experimental_ns_) {
870 LOG(LS_INFO) << "Experimental ns is enabled? " << *experimental_ns_;
buildbot@webrtc.org1f8a2372014-08-28 10:52:44 +0000871 config.Set<webrtc::ExperimentalNs>(
kwiberg102c6a62015-10-30 02:47:38 -0700872 new webrtc::ExperimentalNs(*experimental_ns_));
buildbot@webrtc.org1f8a2372014-08-28 10:52:44 +0000873 }
buildbot@webrtc.org1f8a2372014-08-28 10:52:44 +0000874
875 // We check audioproc for the benefit of tests, since FakeWebRtcVoiceEngine
876 // returns NULL on audio_processing().
877 webrtc::AudioProcessing* audioproc = voe_wrapper_->base()->audio_processing();
878 if (audioproc) {
879 audioproc->SetExtraOptions(config);
880 }
881
kwiberg102c6a62015-10-30 02:47:38 -0700882 if (options.recording_sample_rate) {
883 LOG(LS_INFO) << "Recording sample rate is "
884 << *options.recording_sample_rate;
885 if (voe_wrapper_->hw()->SetRecordingSampleRate(
886 *options.recording_sample_rate)) {
887 LOG_RTCERR1(SetRecordingSampleRate, *options.recording_sample_rate);
buildbot@webrtc.org13d67762014-05-02 17:33:29 +0000888 }
889 }
890
kwiberg102c6a62015-10-30 02:47:38 -0700891 if (options.playout_sample_rate) {
892 LOG(LS_INFO) << "Playout sample rate is " << *options.playout_sample_rate;
893 if (voe_wrapper_->hw()->SetPlayoutSampleRate(
894 *options.playout_sample_rate)) {
895 LOG_RTCERR1(SetPlayoutSampleRate, *options.playout_sample_rate);
buildbot@webrtc.org13d67762014-05-02 17:33:29 +0000896 }
897 }
898
899 return true;
900}
901
buildbot@webrtc.org13d67762014-05-02 17:33:29 +0000902// TODO(juberti): Refactor this so that the core logic can be used to set the
903// soundclip device. At that time, reinstate the soundclip pause/resume code.
904bool WebRtcVoiceEngine::SetDevices(const Device* in_device,
905 const Device* out_device) {
solenberg566ef242015-11-06 15:34:49 -0800906 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
buildbot@webrtc.org13d67762014-05-02 17:33:29 +0000907#if !defined(IOS)
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000908 int in_id = in_device ? rtc::FromString<int>(in_device->id) :
buildbot@webrtc.org13d67762014-05-02 17:33:29 +0000909 kDefaultAudioDeviceId;
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000910 int out_id = out_device ? rtc::FromString<int>(out_device->id) :
buildbot@webrtc.org13d67762014-05-02 17:33:29 +0000911 kDefaultAudioDeviceId;
912 // The device manager uses -1 as the default device, which was the case for
913 // VoE 3.5. VoE 4.0, however, uses 0 as the default in Linux and Mac.
914#ifndef WIN32
915 if (-1 == in_id) {
916 in_id = kDefaultAudioDeviceId;
917 }
918 if (-1 == out_id) {
919 out_id = kDefaultAudioDeviceId;
920 }
921#endif
922
923 std::string in_name = (in_id != kDefaultAudioDeviceId) ?
924 in_device->name : "Default device";
925 std::string out_name = (out_id != kDefaultAudioDeviceId) ?
926 out_device->name : "Default device";
927 LOG(LS_INFO) << "Setting microphone to (id=" << in_id << ", name=" << in_name
928 << ") and speaker to (id=" << out_id << ", name=" << out_name
929 << ")";
930
buildbot@webrtc.org13d67762014-05-02 17:33:29 +0000931 // Must also pause all audio playback and capture.
solenbergc1a1b352015-09-22 13:31:20 -0700932 bool ret = true;
Fredrik Solenbergaf9fb212015-08-26 10:45:53 +0200933 for (WebRtcVoiceMediaChannel* channel : channels_) {
buildbot@webrtc.org13d67762014-05-02 17:33:29 +0000934 if (!channel->PausePlayout()) {
935 LOG(LS_WARNING) << "Failed to pause playout";
936 ret = false;
937 }
938 if (!channel->PauseSend()) {
939 LOG(LS_WARNING) << "Failed to pause send";
940 ret = false;
941 }
942 }
943
944 // Find the recording device id in VoiceEngine and set recording device.
945 if (!FindWebRtcAudioDeviceId(true, in_name, in_id, &in_id)) {
946 ret = false;
947 }
948 if (ret) {
949 if (voe_wrapper_->hw()->SetRecordingDevice(in_id) == -1) {
950 LOG_RTCERR2(SetRecordingDevice, in_name, in_id);
951 ret = false;
952 }
buildbot@webrtc.org6b21b712014-07-31 15:08:53 +0000953 webrtc::AudioProcessing* ap = voe()->base()->audio_processing();
954 if (ap)
955 ap->Initialize();
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000956 }
957
958 // Find the playout device id in VoiceEngine and set playout device.
959 if (!FindWebRtcAudioDeviceId(false, out_name, out_id, &out_id)) {
960 LOG(LS_WARNING) << "Failed to find VoiceEngine device id for " << out_name;
961 ret = false;
962 }
963 if (ret) {
964 if (voe_wrapper_->hw()->SetPlayoutDevice(out_id) == -1) {
sergeyu@chromium.org5bc25c42013-12-05 00:24:06 +0000965 LOG_RTCERR2(SetPlayoutDevice, out_name, out_id);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000966 ret = false;
967 }
968 }
969
970 // Resume all audio playback and capture.
Fredrik Solenbergaf9fb212015-08-26 10:45:53 +0200971 for (WebRtcVoiceMediaChannel* channel : channels_) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000972 if (!channel->ResumePlayout()) {
973 LOG(LS_WARNING) << "Failed to resume playout";
974 ret = false;
975 }
976 if (!channel->ResumeSend()) {
977 LOG(LS_WARNING) << "Failed to resume send";
978 ret = false;
979 }
980 }
981
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000982 if (ret) {
983 LOG(LS_INFO) << "Set microphone to (id=" << in_id <<" name=" << in_name
984 << ") and speaker to (id="<< out_id << " name=" << out_name
985 << ")";
986 }
987
988 return ret;
989#else
990 return true;
wu@webrtc.orgcecfd182013-10-30 05:18:12 +0000991#endif // !IOS
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000992}
993
994bool WebRtcVoiceEngine::FindWebRtcAudioDeviceId(
995 bool is_input, const std::string& dev_name, int dev_id, int* rtc_id) {
solenberg566ef242015-11-06 15:34:49 -0800996 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000997 // In Linux, VoiceEngine uses the same device dev_id as the device manager.
wu@webrtc.orgcecfd182013-10-30 05:18:12 +0000998#if defined(LINUX) || defined(ANDROID)
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000999 *rtc_id = dev_id;
1000 return true;
1001#else
1002 // In Windows and Mac, we need to find the VoiceEngine device id by name
1003 // unless the input dev_id is the default device id.
1004 if (kDefaultAudioDeviceId == dev_id) {
1005 *rtc_id = dev_id;
1006 return true;
1007 }
1008
1009 // Get the number of VoiceEngine audio devices.
1010 int count = 0;
1011 if (is_input) {
1012 if (-1 == voe_wrapper_->hw()->GetNumOfRecordingDevices(count)) {
1013 LOG_RTCERR0(GetNumOfRecordingDevices);
1014 return false;
1015 }
1016 } else {
1017 if (-1 == voe_wrapper_->hw()->GetNumOfPlayoutDevices(count)) {
1018 LOG_RTCERR0(GetNumOfPlayoutDevices);
1019 return false;
1020 }
1021 }
1022
1023 for (int i = 0; i < count; ++i) {
1024 char name[128];
1025 char guid[128];
1026 if (is_input) {
1027 voe_wrapper_->hw()->GetRecordingDeviceName(i, name, guid);
1028 LOG(LS_VERBOSE) << "VoiceEngine microphone " << i << ": " << name;
1029 } else {
1030 voe_wrapper_->hw()->GetPlayoutDeviceName(i, name, guid);
1031 LOG(LS_VERBOSE) << "VoiceEngine speaker " << i << ": " << name;
1032 }
1033
1034 std::string webrtc_name(name);
1035 if (dev_name.compare(0, webrtc_name.size(), webrtc_name) == 0) {
1036 *rtc_id = i;
1037 return true;
1038 }
1039 }
1040 LOG(LS_WARNING) << "VoiceEngine cannot find device: " << dev_name;
1041 return false;
1042#endif
1043}
1044
1045bool WebRtcVoiceEngine::GetOutputVolume(int* level) {
solenberg566ef242015-11-06 15:34:49 -08001046 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001047 unsigned int ulevel;
1048 if (voe_wrapper_->volume()->GetSpeakerVolume(ulevel) == -1) {
1049 LOG_RTCERR1(GetSpeakerVolume, level);
1050 return false;
1051 }
1052 *level = ulevel;
1053 return true;
1054}
1055
1056bool WebRtcVoiceEngine::SetOutputVolume(int level) {
solenberg566ef242015-11-06 15:34:49 -08001057 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
henrikg91d6ede2015-09-17 00:24:34 -07001058 RTC_DCHECK(level >= 0 && level <= 255);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001059 if (voe_wrapper_->volume()->SetSpeakerVolume(level) == -1) {
1060 LOG_RTCERR1(SetSpeakerVolume, level);
1061 return false;
1062 }
1063 return true;
1064}
1065
1066int WebRtcVoiceEngine::GetInputLevel() {
solenberg566ef242015-11-06 15:34:49 -08001067 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001068 unsigned int ulevel;
1069 return (voe_wrapper_->volume()->GetSpeechInputLevel(ulevel) != -1) ?
1070 static_cast<int>(ulevel) : -1;
1071}
1072
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001073const std::vector<AudioCodec>& WebRtcVoiceEngine::codecs() {
solenberg566ef242015-11-06 15:34:49 -08001074 RTC_DCHECK(signal_thread_checker_.CalledOnValidThread());
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001075 return codecs_;
1076}
1077
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001078const std::vector<RtpHeaderExtension>&
1079WebRtcVoiceEngine::rtp_header_extensions() const {
solenberg566ef242015-11-06 15:34:49 -08001080 RTC_DCHECK(signal_thread_checker_.CalledOnValidThread());
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001081 return rtp_header_extensions_;
1082}
1083
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001084int WebRtcVoiceEngine::GetLastEngineError() {
solenberg566ef242015-11-06 15:34:49 -08001085 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001086 return voe_wrapper_->error();
1087}
1088
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001089void WebRtcVoiceEngine::Print(webrtc::TraceLevel level, const char* trace,
1090 int length) {
solenberg566ef242015-11-06 15:34:49 -08001091 // Note: This callback can happen on any thread!
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00001092 rtc::LoggingSeverity sev = rtc::LS_VERBOSE;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001093 if (level == webrtc::kTraceError || level == webrtc::kTraceCritical)
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00001094 sev = rtc::LS_ERROR;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001095 else if (level == webrtc::kTraceWarning)
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00001096 sev = rtc::LS_WARNING;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001097 else if (level == webrtc::kTraceStateInfo || level == webrtc::kTraceInfo)
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00001098 sev = rtc::LS_INFO;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001099 else if (level == webrtc::kTraceTerseInfo)
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00001100 sev = rtc::LS_INFO;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001101
1102 // Skip past boilerplate prefix text
1103 if (length < 72) {
1104 std::string msg(trace, length);
1105 LOG(LS_ERROR) << "Malformed webrtc log message: ";
1106 LOG_V(sev) << msg;
1107 } else {
1108 std::string msg(trace + 71, length - 72);
Peter Boströmd5c75b12015-09-23 13:24:32 +02001109 LOG_V(sev) << "webrtc: " << msg;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001110 }
1111}
1112
solenberg63b34542015-09-29 06:06:31 -07001113void WebRtcVoiceEngine::RegisterChannel(WebRtcVoiceMediaChannel* channel) {
solenberg566ef242015-11-06 15:34:49 -08001114 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
1115 RTC_DCHECK(channel);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001116 channels_.push_back(channel);
1117}
1118
solenberg63b34542015-09-29 06:06:31 -07001119void WebRtcVoiceEngine::UnregisterChannel(WebRtcVoiceMediaChannel* channel) {
solenberg566ef242015-11-06 15:34:49 -08001120 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
solenberg63b34542015-09-29 06:06:31 -07001121 auto it = std::find(channels_.begin(), channels_.end(), channel);
solenberg566ef242015-11-06 15:34:49 -08001122 RTC_DCHECK(it != channels_.end());
1123 channels_.erase(it);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001124}
1125
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001126// Adjusts the default AGC target level by the specified delta.
1127// NB: If we start messing with other config fields, we'll want
1128// to save the current webrtc::AgcConfig as well.
1129bool WebRtcVoiceEngine::AdjustAgcLevel(int delta) {
solenberg566ef242015-11-06 15:34:49 -08001130 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001131 webrtc::AgcConfig config = default_agc_config_;
1132 config.targetLeveldBOv -= delta;
1133
1134 LOG(LS_INFO) << "Adjusting AGC level from default -"
1135 << default_agc_config_.targetLeveldBOv << "dB to -"
1136 << config.targetLeveldBOv << "dB";
1137
1138 if (voe_wrapper_->processing()->SetAgcConfig(config) == -1) {
1139 LOG_RTCERR1(SetAgcConfig, config.targetLeveldBOv);
1140 return false;
1141 }
1142 return true;
1143}
1144
Fredrik Solenbergccb49e72015-05-19 11:37:56 +02001145bool WebRtcVoiceEngine::SetAudioDeviceModule(webrtc::AudioDeviceModule* adm) {
solenberg566ef242015-11-06 15:34:49 -08001146 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001147 if (initialized_) {
1148 LOG(LS_WARNING) << "SetAudioDeviceModule can not be called after Init.";
1149 return false;
1150 }
1151 if (adm_) {
1152 adm_->Release();
1153 adm_ = NULL;
1154 }
1155 if (adm) {
1156 adm_ = adm;
1157 adm_->AddRef();
1158 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001159 return true;
1160}
1161
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00001162bool WebRtcVoiceEngine::StartAecDump(rtc::PlatformFile file) {
solenberg566ef242015-11-06 15:34:49 -08001163 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00001164 FILE* aec_dump_file_stream = rtc::FdopenPlatformFileForWriting(file);
wu@webrtc.orga8910d22014-01-23 22:12:45 +00001165 if (!aec_dump_file_stream) {
1166 LOG(LS_ERROR) << "Could not open AEC dump file stream.";
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00001167 if (!rtc::ClosePlatformFile(file))
wu@webrtc.orga8910d22014-01-23 22:12:45 +00001168 LOG(LS_WARNING) << "Could not close file.";
1169 return false;
1170 }
wu@webrtc.orga9890802013-12-13 00:21:03 +00001171 StopAecDump();
wu@webrtc.orga8910d22014-01-23 22:12:45 +00001172 if (voe_wrapper_->processing()->StartDebugRecording(aec_dump_file_stream) !=
wu@webrtc.orga9890802013-12-13 00:21:03 +00001173 webrtc::AudioProcessing::kNoError) {
wu@webrtc.orga8910d22014-01-23 22:12:45 +00001174 LOG_RTCERR0(StartDebugRecording);
1175 fclose(aec_dump_file_stream);
wu@webrtc.orga9890802013-12-13 00:21:03 +00001176 return false;
1177 }
1178 is_dumping_aec_ = true;
1179 return true;
wu@webrtc.orga9890802013-12-13 00:21:03 +00001180}
1181
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001182void WebRtcVoiceEngine::StartAecDump(const std::string& filename) {
solenberg566ef242015-11-06 15:34:49 -08001183 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001184 if (!is_dumping_aec_) {
1185 // Start dumping AEC when we are not dumping.
1186 if (voe_wrapper_->processing()->StartDebugRecording(
1187 filename.c_str()) != webrtc::AudioProcessing::kNoError) {
wu@webrtc.orga9890802013-12-13 00:21:03 +00001188 LOG_RTCERR1(StartDebugRecording, filename.c_str());
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001189 } else {
1190 is_dumping_aec_ = true;
1191 }
1192 }
1193}
1194
1195void WebRtcVoiceEngine::StopAecDump() {
solenberg566ef242015-11-06 15:34:49 -08001196 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001197 if (is_dumping_aec_) {
1198 // Stop dumping AEC when we are dumping.
1199 if (voe_wrapper_->processing()->StopDebugRecording() !=
1200 webrtc::AudioProcessing::kNoError) {
1201 LOG_RTCERR0(StopDebugRecording);
1202 }
1203 is_dumping_aec_ = false;
1204 }
1205}
1206
ivoc112a3d82015-10-16 02:22:18 -07001207bool WebRtcVoiceEngine::StartRtcEventLog(rtc::PlatformFile file) {
solenberg566ef242015-11-06 15:34:49 -08001208 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
ivoc112a3d82015-10-16 02:22:18 -07001209 return voe_wrapper_->codec()->GetEventLog()->StartLogging(file);
1210}
1211
1212void WebRtcVoiceEngine::StopRtcEventLog() {
solenberg566ef242015-11-06 15:34:49 -08001213 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
ivoc112a3d82015-10-16 02:22:18 -07001214 voe_wrapper_->codec()->GetEventLog()->StopLogging();
1215}
1216
solenberg0a617e22015-10-20 15:49:38 -07001217int WebRtcVoiceEngine::CreateVoEChannel() {
solenberg566ef242015-11-06 15:34:49 -08001218 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
solenberg0a617e22015-10-20 15:49:38 -07001219 return voe_wrapper_->base()->CreateChannel(voe_config_);
sergeyu@chromium.org5bc25c42013-12-05 00:24:06 +00001220}
1221
solenbergc96df772015-10-21 13:01:53 -07001222class WebRtcVoiceMediaChannel::WebRtcAudioSendStream
mallinath@webrtc.org67ee6b92014-02-03 16:57:16 +00001223 : public AudioRenderer::Sink {
1224 public:
solenbergc96df772015-10-21 13:01:53 -07001225 WebRtcAudioSendStream(int ch, webrtc::AudioTransport* voe_audio_transport,
solenberg3a941542015-11-16 07:34:50 -08001226 uint32_t ssrc, const std::string& c_name,
1227 const std::vector<webrtc::RtpExtension>& extensions,
1228 webrtc::Call* call)
solenberg7add0582015-11-20 09:59:34 -08001229 : voe_audio_transport_(voe_audio_transport),
solenberg3a941542015-11-16 07:34:50 -08001230 call_(call),
1231 config_(nullptr) {
solenberg85a04962015-10-27 03:35:21 -07001232 RTC_DCHECK_GE(ch, 0);
1233 // TODO(solenberg): Once we're not using FakeWebRtcVoiceEngine anymore:
1234 // RTC_DCHECK(voe_audio_transport);
solenbergc96df772015-10-21 13:01:53 -07001235 RTC_DCHECK(call);
solenberg85a04962015-10-27 03:35:21 -07001236 audio_capture_thread_checker_.DetachFromThread();
solenberg3a941542015-11-16 07:34:50 -08001237 config_.rtp.ssrc = ssrc;
1238 config_.rtp.c_name = c_name;
1239 config_.voe_channel_id = ch;
1240 RecreateAudioSendStream(extensions);
solenbergc96df772015-10-21 13:01:53 -07001241 }
solenberg3a941542015-11-16 07:34:50 -08001242
solenbergc96df772015-10-21 13:01:53 -07001243 ~WebRtcAudioSendStream() override {
solenberg566ef242015-11-06 15:34:49 -08001244 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
solenbergc96df772015-10-21 13:01:53 -07001245 Stop();
1246 call_->DestroyAudioSendStream(stream_);
1247 }
henrike@webrtc.org1e09a712013-07-26 19:17:59 +00001248
solenberg3a941542015-11-16 07:34:50 -08001249 void RecreateAudioSendStream(
1250 const std::vector<webrtc::RtpExtension>& extensions) {
1251 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
1252 if (stream_) {
1253 call_->DestroyAudioSendStream(stream_);
1254 stream_ = nullptr;
1255 }
1256 config_.rtp.extensions = extensions;
1257 RTC_DCHECK(!stream_);
1258 stream_ = call_->CreateAudioSendStream(config_);
1259 RTC_CHECK(stream_);
1260 }
1261
Fredrik Solenbergb5727682015-12-04 15:22:19 +01001262 bool SendTelephoneEvent(int payload_type, uint8_t event,
1263 uint32_t duration_ms) {
1264 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
1265 RTC_DCHECK(stream_);
1266 return stream_->SendTelephoneEvent(payload_type, event, duration_ms);
1267 }
1268
solenberg3a941542015-11-16 07:34:50 -08001269 webrtc::AudioSendStream::Stats GetStats() const {
1270 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
1271 RTC_DCHECK(stream_);
1272 return stream_->GetStats();
1273 }
1274
mallinath@webrtc.org67ee6b92014-02-03 16:57:16 +00001275 // Starts the rendering by setting a sink to the renderer to get data
1276 // callback.
henrike@webrtc.orga7b98182014-02-21 15:51:43 +00001277 // This method is called on the libjingle worker thread.
mallinath@webrtc.org67ee6b92014-02-03 16:57:16 +00001278 // TODO(xians): Make sure Start() is called only once.
1279 void Start(AudioRenderer* renderer) {
solenberg566ef242015-11-06 15:34:49 -08001280 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
solenbergc96df772015-10-21 13:01:53 -07001281 RTC_DCHECK(renderer);
1282 if (renderer_) {
henrikg91d6ede2015-09-17 00:24:34 -07001283 RTC_DCHECK(renderer_ == renderer);
mallinath@webrtc.org67ee6b92014-02-03 16:57:16 +00001284 return;
1285 }
mallinath@webrtc.org67ee6b92014-02-03 16:57:16 +00001286 renderer->SetSink(this);
1287 renderer_ = renderer;
1288 }
1289
solenbergc96df772015-10-21 13:01:53 -07001290 // Stops rendering by setting the sink of the renderer to nullptr. No data
mallinath@webrtc.org67ee6b92014-02-03 16:57:16 +00001291 // callback will be received after this method.
henrike@webrtc.orga7b98182014-02-21 15:51:43 +00001292 // This method is called on the libjingle worker thread.
mallinath@webrtc.org67ee6b92014-02-03 16:57:16 +00001293 void Stop() {
solenberg566ef242015-11-06 15:34:49 -08001294 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
solenbergc96df772015-10-21 13:01:53 -07001295 if (renderer_) {
1296 renderer_->SetSink(nullptr);
1297 renderer_ = nullptr;
solenberg98c68862015-10-09 03:27:14 -07001298 }
mallinath@webrtc.org67ee6b92014-02-03 16:57:16 +00001299 }
1300
1301 // AudioRenderer::Sink implementation.
henrike@webrtc.orga7b98182014-02-21 15:51:43 +00001302 // This method is called on the audio thread.
kjellander@webrtc.org14665ff2015-03-04 12:58:35 +00001303 void OnData(const void* audio_data,
1304 int bits_per_sample,
1305 int sample_rate,
1306 int number_of_channels,
Peter Kastingdce40cf2015-08-24 14:52:23 -07001307 size_t number_of_frames) override {
solenberg566ef242015-11-06 15:34:49 -08001308 RTC_DCHECK(!worker_thread_checker_.CalledOnValidThread());
solenberg85a04962015-10-27 03:35:21 -07001309 RTC_DCHECK(audio_capture_thread_checker_.CalledOnValidThread());
solenbergc96df772015-10-21 13:01:53 -07001310 RTC_DCHECK(voe_audio_transport_);
solenberg7add0582015-11-20 09:59:34 -08001311 voe_audio_transport_->OnData(config_.voe_channel_id,
henrike@webrtc.orga7b98182014-02-21 15:51:43 +00001312 audio_data,
1313 bits_per_sample,
1314 sample_rate,
1315 number_of_channels,
1316 number_of_frames);
henrike@webrtc.orga7b98182014-02-21 15:51:43 +00001317 }
1318
1319 // Callback from the |renderer_| when it is going away. In case Start() has
1320 // never been called, this callback won't be triggered.
kjellander@webrtc.org14665ff2015-03-04 12:58:35 +00001321 void OnClose() override {
solenberg566ef242015-11-06 15:34:49 -08001322 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
solenbergc96df772015-10-21 13:01:53 -07001323 // Set |renderer_| to nullptr to make sure no more callback will get into
henrike@webrtc.orga7b98182014-02-21 15:51:43 +00001324 // the renderer.
solenbergc96df772015-10-21 13:01:53 -07001325 renderer_ = nullptr;
mallinath@webrtc.org67ee6b92014-02-03 16:57:16 +00001326 }
1327
1328 // Accessor to the VoE channel ID.
solenberg85a04962015-10-27 03:35:21 -07001329 int channel() const {
solenberg566ef242015-11-06 15:34:49 -08001330 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
solenberg7add0582015-11-20 09:59:34 -08001331 return config_.voe_channel_id;
solenberg85a04962015-10-27 03:35:21 -07001332 }
mallinath@webrtc.org67ee6b92014-02-03 16:57:16 +00001333
1334 private:
solenberg566ef242015-11-06 15:34:49 -08001335 rtc::ThreadChecker worker_thread_checker_;
solenberg85a04962015-10-27 03:35:21 -07001336 rtc::ThreadChecker audio_capture_thread_checker_;
solenbergc96df772015-10-21 13:01:53 -07001337 webrtc::AudioTransport* const voe_audio_transport_ = nullptr;
1338 webrtc::Call* call_ = nullptr;
solenberg3a941542015-11-16 07:34:50 -08001339 webrtc::AudioSendStream::Config config_;
1340 // The stream is owned by WebRtcAudioSendStream and may be reallocated if
1341 // configuration changes.
solenbergc96df772015-10-21 13:01:53 -07001342 webrtc::AudioSendStream* stream_ = nullptr;
mallinath@webrtc.org67ee6b92014-02-03 16:57:16 +00001343
1344 // Raw pointer to AudioRenderer owned by LocalAudioTrackHandler.
1345 // PeerConnection will make sure invalidating the pointer before the object
1346 // goes away.
solenbergc96df772015-10-21 13:01:53 -07001347 AudioRenderer* renderer_ = nullptr;
henrike@webrtc.orga7b98182014-02-21 15:51:43 +00001348
solenbergc96df772015-10-21 13:01:53 -07001349 RTC_DISALLOW_IMPLICIT_CONSTRUCTORS(WebRtcAudioSendStream);
1350};
1351
1352class WebRtcVoiceMediaChannel::WebRtcAudioReceiveStream {
1353 public:
solenberg7add0582015-11-20 09:59:34 -08001354 WebRtcAudioReceiveStream(int ch, uint32_t remote_ssrc, uint32_t local_ssrc,
1355 bool use_combined_bwe, const std::string& sync_group,
1356 const std::vector<webrtc::RtpExtension>& extensions,
1357 webrtc::Call* call)
1358 : call_(call),
1359 config_() {
1360 RTC_DCHECK_GE(ch, 0);
1361 RTC_DCHECK(call);
1362 config_.rtp.remote_ssrc = remote_ssrc;
1363 config_.rtp.local_ssrc = local_ssrc;
1364 config_.voe_channel_id = ch;
1365 config_.sync_group = sync_group;
1366 RecreateAudioReceiveStream(use_combined_bwe, extensions);
1367 }
solenbergc96df772015-10-21 13:01:53 -07001368
solenberg7add0582015-11-20 09:59:34 -08001369 ~WebRtcAudioReceiveStream() {
1370 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
1371 call_->DestroyAudioReceiveStream(stream_);
1372 }
1373
1374 void RecreateAudioReceiveStream(
1375 const std::vector<webrtc::RtpExtension>& extensions) {
1376 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
1377 RecreateAudioReceiveStream(config_.combined_audio_video_bwe, extensions);
1378 }
1379 void RecreateAudioReceiveStream(bool use_combined_bwe) {
1380 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
1381 RecreateAudioReceiveStream(use_combined_bwe, config_.rtp.extensions);
1382 }
1383
1384 webrtc::AudioReceiveStream::Stats GetStats() const {
1385 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
1386 RTC_DCHECK(stream_);
1387 return stream_->GetStats();
1388 }
1389
1390 int channel() const {
1391 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
1392 return config_.voe_channel_id;
1393 }
solenbergc96df772015-10-21 13:01:53 -07001394
1395 private:
solenberg7add0582015-11-20 09:59:34 -08001396 void RecreateAudioReceiveStream(bool use_combined_bwe,
1397 const std::vector<webrtc::RtpExtension>& extensions) {
1398 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
1399 if (stream_) {
1400 call_->DestroyAudioReceiveStream(stream_);
1401 stream_ = nullptr;
1402 }
1403 config_.rtp.extensions = extensions;
1404 config_.combined_audio_video_bwe = use_combined_bwe;
1405 RTC_DCHECK(!stream_);
1406 stream_ = call_->CreateAudioReceiveStream(config_);
1407 RTC_CHECK(stream_);
1408 }
1409
1410 rtc::ThreadChecker worker_thread_checker_;
1411 webrtc::Call* call_ = nullptr;
1412 webrtc::AudioReceiveStream::Config config_;
1413 // The stream is owned by WebRtcAudioReceiveStream and may be reallocated if
1414 // configuration changes.
1415 webrtc::AudioReceiveStream* stream_ = nullptr;
solenbergc96df772015-10-21 13:01:53 -07001416
1417 RTC_DISALLOW_IMPLICIT_CONSTRUCTORS(WebRtcAudioReceiveStream);
henrike@webrtc.org1e09a712013-07-26 19:17:59 +00001418};
1419
Fredrik Solenberg709ed672015-09-15 12:26:33 +02001420WebRtcVoiceMediaChannel::WebRtcVoiceMediaChannel(WebRtcVoiceEngine* engine,
Fredrik Solenbergb071a192015-09-17 16:42:56 +02001421 const AudioOptions& options,
Fredrik Solenberg709ed672015-09-15 12:26:33 +02001422 webrtc::Call* call)
solenberg566ef242015-11-06 15:34:49 -08001423 : engine_(engine), call_(call) {
solenberg0a617e22015-10-20 15:49:38 -07001424 LOG(LS_VERBOSE) << "WebRtcVoiceMediaChannel::WebRtcVoiceMediaChannel";
solenberg566ef242015-11-06 15:34:49 -08001425 RTC_DCHECK(call);
solenberg0a617e22015-10-20 15:49:38 -07001426 engine->RegisterChannel(this);
Fredrik Solenbergb071a192015-09-17 16:42:56 +02001427 SetOptions(options);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001428}
1429
1430WebRtcVoiceMediaChannel::~WebRtcVoiceMediaChannel() {
solenberg566ef242015-11-06 15:34:49 -08001431 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
solenberg0a617e22015-10-20 15:49:38 -07001432 LOG(LS_VERBOSE) << "WebRtcVoiceMediaChannel::~WebRtcVoiceMediaChannel";
solenberg7add0582015-11-20 09:59:34 -08001433 // TODO(solenberg): Should be able to delete the streams directly, without
1434 // going through RemoveNnStream(), once stream objects handle
1435 // all (de)configuration.
solenbergc96df772015-10-21 13:01:53 -07001436 while (!send_streams_.empty()) {
1437 RemoveSendStream(send_streams_.begin()->first);
solenbergd97ec302015-10-07 01:40:33 -07001438 }
solenberg7add0582015-11-20 09:59:34 -08001439 while (!recv_streams_.empty()) {
1440 RemoveRecvStream(recv_streams_.begin()->first);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001441 }
solenberg0a617e22015-10-20 15:49:38 -07001442 engine()->UnregisterChannel(this);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001443}
1444
Peter Thatcherc2ee2c82015-08-07 16:05:34 -07001445bool WebRtcVoiceMediaChannel::SetSendParameters(
1446 const AudioSendParameters& params) {
solenberg566ef242015-11-06 15:34:49 -08001447 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
solenberg7e4e01a2015-12-02 08:05:01 -08001448 LOG(LS_INFO) << "WebRtcVoiceMediaChannel::SetSendParameters: "
1449 << params.ToString();
Peter Thatcherc2ee2c82015-08-07 16:05:34 -07001450 // TODO(pthatcher): Refactor this to be more clean now that we have
1451 // all the information at once.
solenberg3a941542015-11-16 07:34:50 -08001452
1453 if (!SetSendCodecs(params.codecs)) {
1454 return false;
1455 }
1456
solenberg7e4e01a2015-12-02 08:05:01 -08001457 if (!ValidateRtpExtensions(params.extensions)) {
1458 return false;
1459 }
1460 std::vector<webrtc::RtpExtension> filtered_extensions =
1461 FilterRtpExtensions(params.extensions,
1462 webrtc::RtpExtension::IsSupportedForAudio, true);
1463 if (send_rtp_extensions_ != filtered_extensions) {
1464 send_rtp_extensions_.swap(filtered_extensions);
solenberg3a941542015-11-16 07:34:50 -08001465 for (auto& it : send_streams_) {
1466 it.second->RecreateAudioSendStream(send_rtp_extensions_);
1467 }
1468 }
1469
1470 if (!SetMaxSendBandwidth(params.max_bandwidth_bps)) {
1471 return false;
1472 }
1473 return SetOptions(params.options);
Peter Thatcherc2ee2c82015-08-07 16:05:34 -07001474}
1475
1476bool WebRtcVoiceMediaChannel::SetRecvParameters(
1477 const AudioRecvParameters& params) {
solenberg566ef242015-11-06 15:34:49 -08001478 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
solenberg7e4e01a2015-12-02 08:05:01 -08001479 LOG(LS_INFO) << "WebRtcVoiceMediaChannel::SetRecvParameters: "
1480 << params.ToString();
Peter Thatcherc2ee2c82015-08-07 16:05:34 -07001481 // TODO(pthatcher): Refactor this to be more clean now that we have
1482 // all the information at once.
solenberg7add0582015-11-20 09:59:34 -08001483
1484 if (!SetRecvCodecs(params.codecs)) {
1485 return false;
1486 }
1487
solenberg7e4e01a2015-12-02 08:05:01 -08001488 if (!ValidateRtpExtensions(params.extensions)) {
1489 return false;
1490 }
1491 std::vector<webrtc::RtpExtension> filtered_extensions =
1492 FilterRtpExtensions(params.extensions,
1493 webrtc::RtpExtension::IsSupportedForAudio, false);
1494 if (recv_rtp_extensions_ != filtered_extensions) {
1495 recv_rtp_extensions_.swap(filtered_extensions);
solenberg7add0582015-11-20 09:59:34 -08001496 for (auto& it : recv_streams_) {
1497 it.second->RecreateAudioReceiveStream(recv_rtp_extensions_);
1498 }
1499 }
1500
1501 return true;
Peter Thatcherc2ee2c82015-08-07 16:05:34 -07001502}
1503
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001504bool WebRtcVoiceMediaChannel::SetOptions(const AudioOptions& options) {
solenberg566ef242015-11-06 15:34:49 -08001505 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001506 LOG(LS_INFO) << "Setting voice channel options: "
1507 << options.ToString();
1508
wu@webrtc.orgde305012013-10-31 15:40:38 +00001509 // Check if DSCP value is changed from previous.
1510 bool dscp_option_changed = (options_.dscp != options.dscp);
1511
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001512 // We retain all of the existing options, and apply the given ones
1513 // on top. This means there is no way to "clear" options such that
1514 // they go back to the engine default.
1515 options_.SetAll(options);
1516
1517 if (send_ != SEND_NOTHING) {
solenberg63b34542015-09-29 06:06:31 -07001518 if (!engine()->ApplyOptions(options_)) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001519 LOG(LS_WARNING) <<
solenberg63b34542015-09-29 06:06:31 -07001520 "Failed to apply engine options during channel SetOptions.";
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001521 return false;
1522 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001523 }
1524
wu@webrtc.orgde305012013-10-31 15:40:38 +00001525 if (dscp_option_changed) {
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00001526 rtc::DiffServCodePoint dscp = rtc::DSCP_DEFAULT;
kwiberg102c6a62015-10-30 02:47:38 -07001527 if (options_.dscp.value_or(false))
wu@webrtc.orgde305012013-10-31 15:40:38 +00001528 dscp = kAudioDscpValue;
1529 if (MediaChannel::SetDscp(dscp) != 0) {
1530 LOG(LS_WARNING) << "Failed to set DSCP settings for audio channel";
1531 }
1532 }
solenberg8fb30c32015-10-13 03:06:58 -07001533
solenbergc96df772015-10-21 13:01:53 -07001534 // TODO(solenberg): Don't recreate unless options changed.
solenberg7add0582015-11-20 09:59:34 -08001535 for (auto& it : recv_streams_) {
1536 it.second->RecreateAudioReceiveStream(
1537 options_.combined_audio_video_bwe.value_or(false));
1538 }
solenberg8fb30c32015-10-13 03:06:58 -07001539
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001540 LOG(LS_INFO) << "Set voice channel options. Current options: "
1541 << options_.ToString();
1542 return true;
1543}
1544
1545bool WebRtcVoiceMediaChannel::SetRecvCodecs(
1546 const std::vector<AudioCodec>& codecs) {
solenberg566ef242015-11-06 15:34:49 -08001547 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
solenberg8fb30c32015-10-13 03:06:58 -07001548
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001549 // Set the payload types to be used for incoming media.
solenberg0b675462015-10-09 01:37:09 -07001550 LOG(LS_INFO) << "Setting receive voice codecs.";
solenberg0b675462015-10-09 01:37:09 -07001551
1552 if (!VerifyUniquePayloadTypes(codecs)) {
1553 LOG(LS_ERROR) << "Codec payload types overlap.";
1554 return false;
1555 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001556
1557 std::vector<AudioCodec> new_codecs;
1558 // Find all new codecs. We allow adding new codecs but don't allow changing
1559 // the payload type of codecs that is already configured since we might
1560 // already be receiving packets with that payload type.
Fredrik Solenbergaf9fb212015-08-26 10:45:53 +02001561 for (const AudioCodec& codec : codecs) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001562 AudioCodec old_codec;
Fredrik Solenbergaf9fb212015-08-26 10:45:53 +02001563 if (FindCodec(recv_codecs_, codec, &old_codec)) {
1564 if (old_codec.id != codec.id) {
1565 LOG(LS_ERROR) << codec.name << " payload type changed.";
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001566 return false;
1567 }
1568 } else {
Fredrik Solenbergaf9fb212015-08-26 10:45:53 +02001569 new_codecs.push_back(codec);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001570 }
1571 }
1572 if (new_codecs.empty()) {
1573 // There are no new codecs to configure. Already configured codecs are
1574 // never removed.
1575 return true;
1576 }
1577
1578 if (playout_) {
1579 // Receive codecs can not be changed while playing. So we temporarily
1580 // pause playout.
1581 PausePlayout();
1582 }
1583
solenberg26c8c912015-11-27 04:00:25 -08001584 bool result = true;
1585 for (const AudioCodec& codec : new_codecs) {
1586 webrtc::CodecInst voe_codec;
1587 if (WebRtcVoiceEngine::ToCodecInst(codec, &voe_codec)) {
1588 LOG(LS_INFO) << ToString(codec);
1589 voe_codec.pltype = codec.id;
1590 for (const auto& ch : recv_streams_) {
1591 if (engine()->voe()->codec()->SetRecPayloadType(
1592 ch.second->channel(), voe_codec) == -1) {
1593 LOG_RTCERR2(SetRecPayloadType, ch.second->channel(),
1594 ToString(voe_codec));
1595 result = false;
1596 }
1597 }
1598 } else {
1599 LOG(LS_WARNING) << "Unknown codec " << ToString(codec);
1600 result = false;
1601 break;
1602 }
1603 }
Fredrik Solenbergaf9fb212015-08-26 10:45:53 +02001604 if (result) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001605 recv_codecs_ = codecs;
1606 }
1607
1608 if (desired_playout_ && !playout_) {
1609 ResumePlayout();
1610 }
Fredrik Solenbergaf9fb212015-08-26 10:45:53 +02001611 return result;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001612}
1613
1614bool WebRtcVoiceMediaChannel::SetSendCodecs(
wu@webrtc.orgcadf9042013-08-30 21:24:16 +00001615 int channel, const std::vector<AudioCodec>& codecs) {
buildbot@webrtc.orgae740dd2014-06-17 10:56:41 +00001616 // Disable VAD, FEC, and RED unless we know the other side wants them.
wu@webrtc.orgcadf9042013-08-30 21:24:16 +00001617 engine()->voe()->codec()->SetVADStatus(channel, false);
1618 engine()->voe()->rtp()->SetNACKStatus(channel, false, 0);
buildbot@webrtc.orgae740dd2014-06-17 10:56:41 +00001619 engine()->voe()->rtp()->SetREDStatus(channel, false);
1620 engine()->voe()->codec()->SetFECStatus(channel, false);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001621
1622 // Scan through the list to figure out the codec to use for sending, along
Fredrik Solenbergb5727682015-12-04 15:22:19 +01001623 // with the proper configuration for VAD.
henrike@webrtc.org704bf9e2014-02-27 17:52:04 +00001624 bool found_send_codec = false;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001625 webrtc::CodecInst send_codec;
1626 memset(&send_codec, 0, sizeof(send_codec));
1627
wu@webrtc.org05e7b442014-04-01 17:44:24 +00001628 bool nack_enabled = nack_enabled_;
buildbot@webrtc.org3ffa1f92014-07-02 19:51:26 +00001629 bool enable_codec_fec = false;
Minyue Li7100dcd2015-03-27 05:05:59 +01001630 bool enable_opus_dtx = false;
minyue@webrtc.org26236952014-10-29 02:27:08 +00001631 int opus_max_playback_rate = 0;
buildbot@webrtc.org5d639b32014-09-10 07:57:12 +00001632
henrike@webrtc.org704bf9e2014-02-27 17:52:04 +00001633 // Set send codec (the first non-telephone-event/CN codec)
Fredrik Solenbergaf9fb212015-08-26 10:45:53 +02001634 for (const AudioCodec& codec : codecs) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001635 // Ignore codecs we don't know about. The negotiation step should prevent
1636 // this, but double-check to be sure.
1637 webrtc::CodecInst voe_codec;
solenberg26c8c912015-11-27 04:00:25 -08001638 if (!WebRtcVoiceEngine::ToCodecInst(codec, &voe_codec)) {
Fredrik Solenbergaf9fb212015-08-26 10:45:53 +02001639 LOG(LS_WARNING) << "Unknown codec " << ToString(codec);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001640 continue;
1641 }
1642
Fredrik Solenbergaf9fb212015-08-26 10:45:53 +02001643 if (IsCodec(codec, kDtmfCodecName) || IsCodec(codec, kCnCodecName)) {
henrike@webrtc.org704bf9e2014-02-27 17:52:04 +00001644 // Skip telephone-event/CN codec, which will be handled later.
1645 continue;
1646 }
1647
henrike@webrtc.org704bf9e2014-02-27 17:52:04 +00001648 // We'll use the first codec in the list to actually send audio data.
1649 // Be sure to use the payload type requested by the remote side.
buildbot@webrtc.orgae740dd2014-06-17 10:56:41 +00001650 // "red", for RED audio, is a special case where the actual codec to be
henrike@webrtc.org704bf9e2014-02-27 17:52:04 +00001651 // used is specified in params.
Fredrik Solenbergaf9fb212015-08-26 10:45:53 +02001652 if (IsCodec(codec, kRedCodecName)) {
henrike@webrtc.org704bf9e2014-02-27 17:52:04 +00001653 // Parse out the RED parameters. If we fail, just ignore RED;
1654 // we don't support all possible params/usage scenarios.
Fredrik Solenbergaf9fb212015-08-26 10:45:53 +02001655 if (!GetRedSendCodec(codec, codecs, &send_codec)) {
henrike@webrtc.org704bf9e2014-02-27 17:52:04 +00001656 continue;
1657 }
1658
1659 // Enable redundant encoding of the specified codec. Treat any
1660 // failure as a fatal internal error.
buildbot@webrtc.orgae740dd2014-06-17 10:56:41 +00001661 LOG(LS_INFO) << "Enabling RED on channel " << channel;
Fredrik Solenbergaf9fb212015-08-26 10:45:53 +02001662 if (engine()->voe()->rtp()->SetREDStatus(channel, true, codec.id) == -1) {
1663 LOG_RTCERR3(SetREDStatus, channel, true, codec.id);
henrike@webrtc.org704bf9e2014-02-27 17:52:04 +00001664 return false;
1665 }
1666 } else {
1667 send_codec = voe_codec;
Fredrik Solenbergaf9fb212015-08-26 10:45:53 +02001668 nack_enabled = IsNackEnabled(codec);
Minyue Li7100dcd2015-03-27 05:05:59 +01001669 // For Opus as the send codec, we are to determine inband FEC, maximum
1670 // playback rate, and opus internal dtx.
Fredrik Solenbergaf9fb212015-08-26 10:45:53 +02001671 if (IsCodec(codec, kOpusCodecName)) {
1672 GetOpusConfig(codec, &send_codec, &enable_codec_fec,
Minyue Li7100dcd2015-03-27 05:05:59 +01001673 &opus_max_playback_rate, &enable_opus_dtx);
buildbot@webrtc.org5d639b32014-09-10 07:57:12 +00001674 }
Brave Yao5225dd82015-03-26 07:39:19 +08001675
1676 // Set packet size if the AudioCodec param kCodecParamPTime is set.
1677 int ptime_ms = 0;
Fredrik Solenbergaf9fb212015-08-26 10:45:53 +02001678 if (codec.GetParam(kCodecParamPTime, &ptime_ms)) {
solenberg26c8c912015-11-27 04:00:25 -08001679 if (!WebRtcVoiceCodecs::SetPTimeAsPacketSize(&send_codec, ptime_ms)) {
Brave Yao5225dd82015-03-26 07:39:19 +08001680 LOG(LS_WARNING) << "Failed to set packet size for codec "
1681 << send_codec.plname;
1682 return false;
1683 }
1684 }
henrike@webrtc.org704bf9e2014-02-27 17:52:04 +00001685 }
1686 found_send_codec = true;
1687 break;
1688 }
1689
wu@webrtc.org05e7b442014-04-01 17:44:24 +00001690 if (nack_enabled_ != nack_enabled) {
1691 SetNack(channel, nack_enabled);
1692 nack_enabled_ = nack_enabled;
1693 }
1694
henrike@webrtc.org704bf9e2014-02-27 17:52:04 +00001695 if (!found_send_codec) {
1696 LOG(LS_WARNING) << "Received empty list of codecs.";
1697 return false;
1698 }
1699
1700 // Set the codec immediately, since SetVADStatus() depends on whether
1701 // the current codec is mono or stereo.
1702 if (!SetSendCodec(channel, send_codec))
1703 return false;
1704
buildbot@webrtc.org3ffa1f92014-07-02 19:51:26 +00001705 // FEC should be enabled after SetSendCodec.
1706 if (enable_codec_fec) {
1707 LOG(LS_INFO) << "Attempt to enable codec internal FEC on channel "
1708 << channel;
buildbot@webrtc.org3ffa1f92014-07-02 19:51:26 +00001709 if (engine()->voe()->codec()->SetFECStatus(channel, true) == -1) {
1710 // Enable codec internal FEC. Treat any failure as fatal internal error.
1711 LOG_RTCERR2(SetFECStatus, channel, true);
1712 return false;
1713 }
buildbot@webrtc.org3ffa1f92014-07-02 19:51:26 +00001714 }
1715
Minyue Li7100dcd2015-03-27 05:05:59 +01001716 if (IsCodec(send_codec, kOpusCodecName)) {
1717 // DTX and maxplaybackrate should be set after SetSendCodec. Because current
1718 // send codec has to be Opus.
1719
1720 // Set Opus internal DTX.
1721 LOG(LS_INFO) << "Attempt to "
solenbergbd138382015-11-20 16:08:07 -08001722 << (enable_opus_dtx ? "enable" : "disable")
Minyue Li7100dcd2015-03-27 05:05:59 +01001723 << " Opus DTX on channel "
buildbot@webrtc.org5d639b32014-09-10 07:57:12 +00001724 << channel;
Minyue Li7100dcd2015-03-27 05:05:59 +01001725 if (engine()->voe()->codec()->SetOpusDtx(channel, enable_opus_dtx)) {
1726 LOG_RTCERR2(SetOpusDtx, channel, enable_opus_dtx);
1727 return false;
1728 }
1729
1730 // If opus_max_playback_rate <= 0, the default maximum playback rate
1731 // (48 kHz) will be used.
1732 if (opus_max_playback_rate > 0) {
1733 LOG(LS_INFO) << "Attempt to set maximum playback rate to "
1734 << opus_max_playback_rate
1735 << " Hz on channel "
1736 << channel;
1737 if (engine()->voe()->codec()->SetOpusMaxPlaybackRate(
1738 channel, opus_max_playback_rate) == -1) {
1739 LOG_RTCERR2(SetOpusMaxPlaybackRate, channel, opus_max_playback_rate);
1740 return false;
1741 }
buildbot@webrtc.org5d639b32014-09-10 07:57:12 +00001742 }
buildbot@webrtc.org5d639b32014-09-10 07:57:12 +00001743 }
1744
henrike@webrtc.org704bf9e2014-02-27 17:52:04 +00001745 // Always update the |send_codec_| to the currently set send codec.
1746 send_codec_.reset(new webrtc::CodecInst(send_codec));
1747
minyue@webrtc.org26236952014-10-29 02:27:08 +00001748 if (send_bitrate_setting_) {
1749 SetSendBitrateInternal(send_bitrate_bps_);
henrike@webrtc.org704bf9e2014-02-27 17:52:04 +00001750 }
1751
Fredrik Solenbergb5727682015-12-04 15:22:19 +01001752 // Loop through the codecs list again to config the CN codec.
Fredrik Solenbergaf9fb212015-08-26 10:45:53 +02001753 for (const AudioCodec& codec : codecs) {
henrike@webrtc.org704bf9e2014-02-27 17:52:04 +00001754 // Ignore codecs we don't know about. The negotiation step should prevent
1755 // this, but double-check to be sure.
1756 webrtc::CodecInst voe_codec;
solenberg26c8c912015-11-27 04:00:25 -08001757 if (!WebRtcVoiceEngine::ToCodecInst(codec, &voe_codec)) {
Fredrik Solenbergaf9fb212015-08-26 10:45:53 +02001758 LOG(LS_WARNING) << "Unknown codec " << ToString(codec);
henrike@webrtc.org704bf9e2014-02-27 17:52:04 +00001759 continue;
1760 }
1761
Fredrik Solenbergb5727682015-12-04 15:22:19 +01001762 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());
Fredrik Solenbergb5727682015-12-04 15:22:19 +01001817 // TODO(solenberg): Validate input - that payload types don't overlap, are
1818 // within range, filter out codecs we don't support,
1819 // redundant codecs etc.
solenbergd97ec302015-10-07 01:40:33 -07001820
Fredrik Solenbergb5727682015-12-04 15:22:19 +01001821 // Find the DTMF telephone event "codec" payload type.
1822 dtmf_payload_type_ = rtc::Optional<int>();
Fredrik Solenbergaf9fb212015-08-26 10:45:53 +02001823 for (const AudioCodec& codec : codecs) {
Fredrik Solenbergaf9fb212015-08-26 10:45:53 +02001824 if (IsCodec(codec, kDtmfCodecName)) {
Fredrik Solenbergb5727682015-12-04 15:22:19 +01001825 dtmf_payload_type_ = rtc::Optional<int>(codec.id);
1826 break;
wu@webrtc.orgcadf9042013-08-30 21:24:16 +00001827 }
1828 }
1829
1830 // Cache the codecs in order to configure the channel created later.
1831 send_codecs_ = codecs;
solenbergc96df772015-10-21 13:01:53 -07001832 for (const auto& ch : send_streams_) {
Fredrik Solenbergaf9fb212015-08-26 10:45:53 +02001833 if (!SetSendCodecs(ch.second->channel(), codecs)) {
wu@webrtc.orgcadf9042013-08-30 21:24:16 +00001834 return false;
1835 }
1836 }
1837
wu@webrtc.org05e7b442014-04-01 17:44:24 +00001838 // Set nack status on receive channels and update |nack_enabled_|.
solenberg7add0582015-11-20 09:59:34 -08001839 for (const auto& ch : recv_streams_) {
solenberg0a617e22015-10-20 15:49:38 -07001840 SetNack(ch.second->channel(), nack_enabled_);
wu@webrtc.org9dba5252013-08-05 20:36:57 +00001841 }
solenberg0a617e22015-10-20 15:49:38 -07001842
1843 return true;
wu@webrtc.org9dba5252013-08-05 20:36:57 +00001844}
1845
wu@webrtc.orgcadf9042013-08-30 21:24:16 +00001846void WebRtcVoiceMediaChannel::SetNack(int channel, bool nack_enabled) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001847 if (nack_enabled) {
wu@webrtc.orgcadf9042013-08-30 21:24:16 +00001848 LOG(LS_INFO) << "Enabling NACK for channel " << channel;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001849 engine()->voe()->rtp()->SetNACKStatus(channel, true, kNackMaxPackets);
1850 } else {
wu@webrtc.orgcadf9042013-08-30 21:24:16 +00001851 LOG(LS_INFO) << "Disabling NACK for channel " << channel;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001852 engine()->voe()->rtp()->SetNACKStatus(channel, false, 0);
1853 }
1854}
1855
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001856bool WebRtcVoiceMediaChannel::SetSendCodec(
wu@webrtc.org9dba5252013-08-05 20:36:57 +00001857 int channel, const webrtc::CodecInst& send_codec) {
1858 LOG(LS_INFO) << "Send channel " << channel << " selected voice codec "
1859 << ToString(send_codec) << ", bitrate=" << send_codec.rate;
1860
wu@webrtc.org05e7b442014-04-01 17:44:24 +00001861 webrtc::CodecInst current_codec;
1862 if (engine()->voe()->codec()->GetSendCodec(channel, current_codec) == 0 &&
1863 (send_codec == current_codec)) {
1864 // Codec is already configured, we can return without setting it again.
1865 return true;
1866 }
1867
wu@webrtc.org9dba5252013-08-05 20:36:57 +00001868 if (engine()->voe()->codec()->SetSendCodec(channel, send_codec) == -1) {
1869 LOG_RTCERR2(SetSendCodec, channel, ToString(send_codec));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001870 return false;
1871 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001872 return true;
1873}
1874
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001875bool WebRtcVoiceMediaChannel::SetPlayout(bool playout) {
1876 desired_playout_ = playout;
1877 return ChangePlayout(desired_playout_);
1878}
1879
1880bool WebRtcVoiceMediaChannel::PausePlayout() {
1881 return ChangePlayout(false);
1882}
1883
1884bool WebRtcVoiceMediaChannel::ResumePlayout() {
1885 return ChangePlayout(desired_playout_);
1886}
1887
1888bool WebRtcVoiceMediaChannel::ChangePlayout(bool playout) {
solenberg566ef242015-11-06 15:34:49 -08001889 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001890 if (playout_ == playout) {
1891 return true;
1892 }
1893
solenberg7add0582015-11-20 09:59:34 -08001894 for (const auto& ch : recv_streams_) {
Fredrik Solenbergaf9fb212015-08-26 10:45:53 +02001895 if (!SetPlayout(ch.second->channel(), playout)) {
henrike@webrtc.org1e09a712013-07-26 19:17:59 +00001896 LOG(LS_ERROR) << "SetPlayout " << playout << " on channel "
Fredrik Solenbergaf9fb212015-08-26 10:45:53 +02001897 << ch.second->channel() << " failed";
solenberg1ac56142015-10-13 03:58:19 -07001898 return false;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001899 }
1900 }
solenberg1ac56142015-10-13 03:58:19 -07001901 playout_ = playout;
1902 return true;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001903}
1904
1905bool WebRtcVoiceMediaChannel::SetSend(SendFlags send) {
1906 desired_send_ = send;
solenbergc96df772015-10-21 13:01:53 -07001907 if (!send_streams_.empty()) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001908 return ChangeSend(desired_send_);
solenbergc96df772015-10-21 13:01:53 -07001909 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001910 return true;
1911}
1912
1913bool WebRtcVoiceMediaChannel::PauseSend() {
1914 return ChangeSend(SEND_NOTHING);
1915}
1916
1917bool WebRtcVoiceMediaChannel::ResumeSend() {
1918 return ChangeSend(desired_send_);
1919}
1920
1921bool WebRtcVoiceMediaChannel::ChangeSend(SendFlags send) {
1922 if (send_ == send) {
1923 return true;
1924 }
1925
solenberg63b34542015-09-29 06:06:31 -07001926 // Apply channel specific options.
1927 if (send == SEND_MICROPHONE) {
1928 engine()->ApplyOptions(options_);
1929 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001930
wu@webrtc.org9dba5252013-08-05 20:36:57 +00001931 // Change the settings on each send channel.
solenbergc96df772015-10-21 13:01:53 -07001932 for (const auto& ch : send_streams_) {
solenberg63b34542015-09-29 06:06:31 -07001933 if (!ChangeSend(ch.second->channel(), send)) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001934 return false;
solenberg63b34542015-09-29 06:06:31 -07001935 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001936 }
wu@webrtc.org9dba5252013-08-05 20:36:57 +00001937
solenberg63b34542015-09-29 06:06:31 -07001938 // Clear up the options after stopping sending. Since we may previously have
1939 // applied the channel specific options, now apply the original options stored
1940 // in WebRtcVoiceEngine.
1941 if (send == SEND_NOTHING) {
1942 engine()->ApplyOptions(engine()->GetOptions());
1943 }
wu@webrtc.org9dba5252013-08-05 20:36:57 +00001944
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001945 send_ = send;
1946 return true;
1947}
1948
wu@webrtc.org9dba5252013-08-05 20:36:57 +00001949bool WebRtcVoiceMediaChannel::ChangeSend(int channel, SendFlags send) {
1950 if (send == SEND_MICROPHONE) {
1951 if (engine()->voe()->base()->StartSend(channel) == -1) {
1952 LOG_RTCERR1(StartSend, channel);
1953 return false;
1954 }
wu@webrtc.org9dba5252013-08-05 20:36:57 +00001955 } else { // SEND_NOTHING
henrikg91d6ede2015-09-17 00:24:34 -07001956 RTC_DCHECK(send == SEND_NOTHING);
wu@webrtc.org9dba5252013-08-05 20:36:57 +00001957 if (engine()->voe()->base()->StopSend(channel) == -1) {
1958 LOG_RTCERR1(StopSend, channel);
1959 return false;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001960 }
1961 }
1962
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001963 return true;
1964}
1965
Peter Boström0c4e06b2015-10-07 12:23:21 +02001966bool WebRtcVoiceMediaChannel::SetAudioSend(uint32_t ssrc,
1967 bool enable,
solenberg1dd98f32015-09-10 01:57:14 -07001968 const AudioOptions* options,
1969 AudioRenderer* renderer) {
solenberg566ef242015-11-06 15:34:49 -08001970 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
solenberg1dd98f32015-09-10 01:57:14 -07001971 // TODO(solenberg): The state change should be fully rolled back if any one of
1972 // these calls fail.
1973 if (!SetLocalRenderer(ssrc, renderer)) {
1974 return false;
1975 }
solenbergdfc8f4f2015-10-01 02:31:10 -07001976 if (!MuteStream(ssrc, !enable)) {
solenberg1dd98f32015-09-10 01:57:14 -07001977 return false;
1978 }
solenbergdfc8f4f2015-10-01 02:31:10 -07001979 if (enable && options) {
solenberg1dd98f32015-09-10 01:57:14 -07001980 return SetOptions(*options);
1981 }
1982 return true;
1983}
1984
solenberg0a617e22015-10-20 15:49:38 -07001985int WebRtcVoiceMediaChannel::CreateVoEChannel() {
1986 int id = engine()->CreateVoEChannel();
1987 if (id == -1) {
1988 LOG_RTCERR0(CreateVoEChannel);
1989 return -1;
wu@webrtc.org9dba5252013-08-05 20:36:57 +00001990 }
solenberg0a617e22015-10-20 15:49:38 -07001991 if (engine()->voe()->network()->RegisterExternalTransport(id, *this) == -1) {
1992 LOG_RTCERR2(RegisterExternalTransport, id, this);
1993 engine()->voe()->base()->DeleteChannel(id);
1994 return -1;
1995 }
1996 return id;
wu@webrtc.org9dba5252013-08-05 20:36:57 +00001997}
1998
solenberg7add0582015-11-20 09:59:34 -08001999bool WebRtcVoiceMediaChannel::DeleteVoEChannel(int channel) {
wu@webrtc.org9dba5252013-08-05 20:36:57 +00002000 if (engine()->voe()->network()->DeRegisterExternalTransport(channel) == -1) {
2001 LOG_RTCERR1(DeRegisterExternalTransport, channel);
2002 }
wu@webrtc.org9dba5252013-08-05 20:36:57 +00002003 if (engine()->voe()->base()->DeleteChannel(channel) == -1) {
2004 LOG_RTCERR1(DeleteChannel, channel);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002005 return false;
2006 }
wu@webrtc.org9dba5252013-08-05 20:36:57 +00002007 return true;
2008}
henrike@webrtc.org1e09a712013-07-26 19:17:59 +00002009
wu@webrtc.org9dba5252013-08-05 20:36:57 +00002010bool WebRtcVoiceMediaChannel::AddSendStream(const StreamParams& sp) {
solenberg566ef242015-11-06 15:34:49 -08002011 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
solenberg0a617e22015-10-20 15:49:38 -07002012 LOG(LS_INFO) << "AddSendStream: " << sp.ToString();
2013
2014 uint32_t ssrc = sp.first_ssrc();
2015 RTC_DCHECK(0 != ssrc);
2016
2017 if (GetSendChannelId(ssrc) != -1) {
2018 LOG(LS_ERROR) << "Stream already exists with ssrc " << ssrc;
wu@webrtc.org9dba5252013-08-05 20:36:57 +00002019 return false;
2020 }
2021
solenberg0a617e22015-10-20 15:49:38 -07002022 // Create a new channel for sending audio data.
2023 int channel = CreateVoEChannel();
2024 if (channel == -1) {
2025 return false;
wu@webrtc.org9dba5252013-08-05 20:36:57 +00002026 }
wu@webrtc.org9dba5252013-08-05 20:36:57 +00002027
solenbergc96df772015-10-21 13:01:53 -07002028 // Save the channel to send_streams_, so that RemoveSendStream() can still
wu@webrtc.org9dba5252013-08-05 20:36:57 +00002029 // delete the channel in case failure happens below.
mallinath@webrtc.org67ee6b92014-02-03 16:57:16 +00002030 webrtc::AudioTransport* audio_transport =
2031 engine()->voe()->base()->audio_transport();
solenberg3a941542015-11-16 07:34:50 -08002032 send_streams_.insert(std::make_pair(ssrc, new WebRtcAudioSendStream(
2033 channel, audio_transport, ssrc, sp.cname, send_rtp_extensions_, call_)));
wu@webrtc.org9dba5252013-08-05 20:36:57 +00002034
solenberg0a617e22015-10-20 15:49:38 -07002035 // Set the current codecs to be used for the new channel. We need to do this
2036 // after adding the channel to send_channels_, because of how max bitrate is
2037 // currently being configured by SetSendCodec().
2038 if (!send_codecs_.empty() && !SetSendCodecs(channel, send_codecs_)) {
2039 RemoveSendStream(ssrc);
wu@webrtc.org9dba5252013-08-05 20:36:57 +00002040 return false;
2041 }
2042
2043 // At this point the channel's local SSRC has been updated. If the channel is
solenberg0a617e22015-10-20 15:49:38 -07002044 // the first send channel make sure that all the receive channels are updated
2045 // with the same SSRC in order to send receiver reports.
solenbergc96df772015-10-21 13:01:53 -07002046 if (send_streams_.size() == 1) {
solenberg0a617e22015-10-20 15:49:38 -07002047 receiver_reports_ssrc_ = ssrc;
solenberg7add0582015-11-20 09:59:34 -08002048 for (const auto& stream : recv_streams_) {
2049 int recv_channel = stream.second->channel();
solenberg0a617e22015-10-20 15:49:38 -07002050 if (engine()->voe()->rtp()->SetLocalSSRC(recv_channel, ssrc) != 0) {
solenberg7add0582015-11-20 09:59:34 -08002051 LOG_RTCERR2(SetLocalSSRC, recv_channel, ssrc);
solenberg1ac56142015-10-13 03:58:19 -07002052 return false;
wu@webrtc.org9dba5252013-08-05 20:36:57 +00002053 }
solenberg0a617e22015-10-20 15:49:38 -07002054 engine()->voe()->base()->AssociateSendChannel(recv_channel, channel);
2055 LOG(LS_INFO) << "VoiceEngine channel #" << recv_channel
2056 << " is associated with channel #" << channel << ".";
wu@webrtc.org9dba5252013-08-05 20:36:57 +00002057 }
2058 }
2059
wu@webrtc.org9dba5252013-08-05 20:36:57 +00002060 return ChangeSend(channel, desired_send_);
2061}
2062
Peter Boström0c4e06b2015-10-07 12:23:21 +02002063bool WebRtcVoiceMediaChannel::RemoveSendStream(uint32_t ssrc) {
solenberg566ef242015-11-06 15:34:49 -08002064 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
solenberg3a941542015-11-16 07:34:50 -08002065 LOG(LS_INFO) << "RemoveSendStream: " << ssrc;
2066
solenbergc96df772015-10-21 13:01:53 -07002067 auto it = send_streams_.find(ssrc);
2068 if (it == send_streams_.end()) {
wu@webrtc.org9dba5252013-08-05 20:36:57 +00002069 LOG(LS_WARNING) << "Try to remove stream with ssrc " << ssrc
2070 << " which doesn't exist.";
2071 return false;
2072 }
2073
mallinath@webrtc.org67ee6b92014-02-03 16:57:16 +00002074 int channel = it->second->channel();
wu@webrtc.org9dba5252013-08-05 20:36:57 +00002075 ChangeSend(channel, SEND_NOTHING);
2076
solenberg7add0582015-11-20 09:59:34 -08002077 // Clean up and delete the send stream+channel.
solenberg0a617e22015-10-20 15:49:38 -07002078 LOG(LS_INFO) << "Removing audio send stream " << ssrc
2079 << " with VoiceEngine channel #" << channel << ".";
solenberg7add0582015-11-20 09:59:34 -08002080 delete it->second;
2081 send_streams_.erase(it);
2082 if (!DeleteVoEChannel(channel)) {
solenberg0a617e22015-10-20 15:49:38 -07002083 return false;
wu@webrtc.org9dba5252013-08-05 20:36:57 +00002084 }
solenbergc96df772015-10-21 13:01:53 -07002085 if (send_streams_.empty()) {
wu@webrtc.org9dba5252013-08-05 20:36:57 +00002086 ChangeSend(SEND_NOTHING);
solenberg0a617e22015-10-20 15:49:38 -07002087 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002088 return true;
2089}
2090
2091bool WebRtcVoiceMediaChannel::AddRecvStream(const StreamParams& sp) {
solenberg566ef242015-11-06 15:34:49 -08002092 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
solenbergd97ec302015-10-07 01:40:33 -07002093 LOG(LS_INFO) << "AddRecvStream: " << sp.ToString();
2094
solenberg0b675462015-10-09 01:37:09 -07002095 if (!ValidateStreamParams(sp)) {
wu@webrtc.org78187522013-10-07 23:32:02 +00002096 return false;
2097 }
2098
solenberg7add0582015-11-20 09:59:34 -08002099 const uint32_t ssrc = sp.first_ssrc();
solenberg0b675462015-10-09 01:37:09 -07002100 if (ssrc == 0) {
2101 LOG(LS_WARNING) << "AddRecvStream with ssrc==0 is not supported.";
2102 return false;
2103 }
2104
solenberg1ac56142015-10-13 03:58:19 -07002105 // Remove the default receive stream if one had been created with this ssrc;
2106 // we'll recreate it then.
2107 if (IsDefaultRecvStream(ssrc)) {
2108 RemoveRecvStream(ssrc);
2109 }
solenberg0b675462015-10-09 01:37:09 -07002110
solenberg7add0582015-11-20 09:59:34 -08002111 if (GetReceiveChannelId(ssrc) != -1) {
henrike@webrtc.org1e09a712013-07-26 19:17:59 +00002112 LOG(LS_ERROR) << "Stream already exists with ssrc " << ssrc;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002113 return false;
2114 }
Fredrik Solenberg4b60c732015-05-07 14:07:48 +02002115
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002116 // Create a new channel for receiving audio data.
solenberg7add0582015-11-20 09:59:34 -08002117 const int channel = CreateVoEChannel();
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002118 if (channel == -1) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002119 return false;
2120 }
Minyue2013aec2015-05-13 14:14:42 +02002121
solenberg1ac56142015-10-13 03:58:19 -07002122 // Turn off all supported codecs.
solenberg26c8c912015-11-27 04:00:25 -08002123 // TODO(solenberg): Remove once "no codecs" is the default state of a stream.
2124 for (webrtc::CodecInst voe_codec : webrtc::acm2::RentACodec::Database()) {
2125 voe_codec.pltype = -1;
2126 if (engine()->voe()->codec()->SetRecPayloadType(channel, voe_codec) == -1) {
2127 LOG_RTCERR2(SetRecPayloadType, channel, ToString(voe_codec));
2128 DeleteVoEChannel(channel);
2129 return false;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002130 }
2131 }
2132
solenberg1ac56142015-10-13 03:58:19 -07002133 // Only enable those configured for this channel.
2134 for (const auto& codec : recv_codecs_) {
2135 webrtc::CodecInst voe_codec;
solenberg26c8c912015-11-27 04:00:25 -08002136 if (WebRtcVoiceEngine::ToCodecInst(codec, &voe_codec)) {
solenberg1ac56142015-10-13 03:58:19 -07002137 voe_codec.pltype = codec.id;
2138 if (engine()->voe()->codec()->SetRecPayloadType(
2139 channel, voe_codec) == -1) {
2140 LOG_RTCERR2(SetRecPayloadType, channel, ToString(voe_codec));
solenberg7add0582015-11-20 09:59:34 -08002141 DeleteVoEChannel(channel);
solenberg1ac56142015-10-13 03:58:19 -07002142 return false;
2143 }
henrike@webrtc.org1e09a712013-07-26 19:17:59 +00002144 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002145 }
solenberg8fb30c32015-10-13 03:06:58 -07002146
solenberg7add0582015-11-20 09:59:34 -08002147 const int send_channel = GetSendChannelId(receiver_reports_ssrc_);
2148 if (send_channel != -1) {
2149 // Associate receive channel with first send channel (so the receive channel
2150 // can obtain RTT from the send channel)
2151 engine()->voe()->base()->AssociateSendChannel(channel, send_channel);
2152 LOG(LS_INFO) << "VoiceEngine channel #" << channel
2153 << " is associated with channel #" << send_channel << ".";
buildbot@webrtc.org150835e2014-05-06 15:54:38 +00002154 }
2155
solenberg7add0582015-11-20 09:59:34 -08002156 recv_streams_.insert(std::make_pair(ssrc, new WebRtcAudioReceiveStream(
2157 channel, ssrc, receiver_reports_ssrc_,
2158 options_.combined_audio_video_bwe.value_or(false), sp.sync_label,
2159 recv_rtp_extensions_, call_)));
2160
2161 SetNack(channel, nack_enabled_);
solenberg1ac56142015-10-13 03:58:19 -07002162 SetPlayout(channel, playout_);
solenberg7add0582015-11-20 09:59:34 -08002163
solenberg1ac56142015-10-13 03:58:19 -07002164 return true;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002165}
2166
Peter Boström0c4e06b2015-10-07 12:23:21 +02002167bool WebRtcVoiceMediaChannel::RemoveRecvStream(uint32_t ssrc) {
solenberg566ef242015-11-06 15:34:49 -08002168 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
solenbergd97ec302015-10-07 01:40:33 -07002169 LOG(LS_INFO) << "RemoveRecvStream: " << ssrc;
2170
solenberg7add0582015-11-20 09:59:34 -08002171 const auto it = recv_streams_.find(ssrc);
2172 if (it == recv_streams_.end()) {
wu@webrtc.org9dba5252013-08-05 20:36:57 +00002173 LOG(LS_WARNING) << "Try to remove stream with ssrc " << ssrc
2174 << " which doesn't exist.";
henrike@webrtc.org1e09a712013-07-26 19:17:59 +00002175 return false;
wu@webrtc.org9dba5252013-08-05 20:36:57 +00002176 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002177
solenberg1ac56142015-10-13 03:58:19 -07002178 // Deregister default channel, if that's the one being destroyed.
2179 if (IsDefaultRecvStream(ssrc)) {
2180 default_recv_ssrc_ = -1;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002181 }
henrike@webrtc.org1e09a712013-07-26 19:17:59 +00002182
solenberg7add0582015-11-20 09:59:34 -08002183 const int channel = it->second->channel();
2184
2185 // Clean up and delete the receive stream+channel.
2186 LOG(LS_INFO) << "Removing audio receive stream " << ssrc
mallinath@webrtc.org67ee6b92014-02-03 16:57:16 +00002187 << " with VoiceEngine channel #" << channel << ".";
solenberg7add0582015-11-20 09:59:34 -08002188 delete it->second;
2189 recv_streams_.erase(it);
2190 return DeleteVoEChannel(channel);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002191}
2192
Peter Boström0c4e06b2015-10-07 12:23:21 +02002193bool WebRtcVoiceMediaChannel::SetLocalRenderer(uint32_t ssrc,
henrike@webrtc.org1e09a712013-07-26 19:17:59 +00002194 AudioRenderer* renderer) {
solenbergc96df772015-10-21 13:01:53 -07002195 auto it = send_streams_.find(ssrc);
2196 if (it == send_streams_.end()) {
wu@webrtc.org9dba5252013-08-05 20:36:57 +00002197 if (renderer) {
2198 // Return an error if trying to set a valid renderer with an invalid ssrc.
2199 LOG(LS_ERROR) << "SetLocalRenderer failed with ssrc "<< ssrc;
2200 return false;
2201 }
2202
2203 // The channel likely has gone away, do nothing.
henrike@webrtc.org1e09a712013-07-26 19:17:59 +00002204 return true;
henrike@webrtc.org1e09a712013-07-26 19:17:59 +00002205 }
2206
solenberg1ac56142015-10-13 03:58:19 -07002207 if (renderer) {
mallinath@webrtc.org67ee6b92014-02-03 16:57:16 +00002208 it->second->Start(renderer);
solenberg1ac56142015-10-13 03:58:19 -07002209 } else {
mallinath@webrtc.org67ee6b92014-02-03 16:57:16 +00002210 it->second->Stop();
solenberg1ac56142015-10-13 03:58:19 -07002211 }
henrike@webrtc.org1e09a712013-07-26 19:17:59 +00002212
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002213 return true;
2214}
2215
2216bool WebRtcVoiceMediaChannel::GetActiveStreams(
2217 AudioInfo::StreamList* actives) {
solenberg566ef242015-11-06 15:34:49 -08002218 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002219 actives->clear();
solenberg7add0582015-11-20 09:59:34 -08002220 for (const auto& ch : recv_streams_) {
Fredrik Solenbergaf9fb212015-08-26 10:45:53 +02002221 int level = GetOutputLevel(ch.second->channel());
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002222 if (level > 0) {
Fredrik Solenbergaf9fb212015-08-26 10:45:53 +02002223 actives->push_back(std::make_pair(ch.first, level));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002224 }
2225 }
2226 return true;
2227}
2228
2229int WebRtcVoiceMediaChannel::GetOutputLevel() {
solenberg566ef242015-11-06 15:34:49 -08002230 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
solenberg1ac56142015-10-13 03:58:19 -07002231 int highest = 0;
solenberg7add0582015-11-20 09:59:34 -08002232 for (const auto& ch : recv_streams_) {
solenberg8fb30c32015-10-13 03:06:58 -07002233 highest = std::max(GetOutputLevel(ch.second->channel()), highest);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002234 }
2235 return highest;
2236}
2237
2238int WebRtcVoiceMediaChannel::GetTimeSinceLastTyping() {
2239 int ret;
2240 if (engine()->voe()->processing()->TimeSinceLastTyping(ret) == -1) {
2241 // In case of error, log the info and continue
2242 LOG_RTCERR0(TimeSinceLastTyping);
2243 ret = -1;
2244 } else {
2245 ret *= 1000; // We return ms, webrtc returns seconds.
2246 }
2247 return ret;
2248}
2249
2250void WebRtcVoiceMediaChannel::SetTypingDetectionParameters(int time_window,
2251 int cost_per_typing, int reporting_threshold, int penalty_decay,
2252 int type_event_delay) {
2253 if (engine()->voe()->processing()->SetTypingDetectionParameters(
2254 time_window, cost_per_typing,
2255 reporting_threshold, penalty_decay, type_event_delay) == -1) {
2256 // In case of error, log the info and continue
2257 LOG_RTCERR5(SetTypingDetectionParameters, time_window,
2258 cost_per_typing, reporting_threshold, penalty_decay,
2259 type_event_delay);
2260 }
2261}
2262
solenberg4bac9c52015-10-09 02:32:53 -07002263bool WebRtcVoiceMediaChannel::SetOutputVolume(uint32_t ssrc, double volume) {
solenberg566ef242015-11-06 15:34:49 -08002264 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
solenberg1ac56142015-10-13 03:58:19 -07002265 if (ssrc == 0) {
2266 default_recv_volume_ = volume;
2267 if (default_recv_ssrc_ == -1) {
2268 return true;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002269 }
solenberg1ac56142015-10-13 03:58:19 -07002270 ssrc = static_cast<uint32_t>(default_recv_ssrc_);
2271 }
2272 int ch_id = GetReceiveChannelId(ssrc);
2273 if (ch_id < 0) {
2274 LOG(LS_WARNING) << "Cannot find channel for ssrc:" << ssrc;
2275 return false;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002276 }
2277
solenberg1ac56142015-10-13 03:58:19 -07002278 if (-1 == engine()->voe()->volume()->SetChannelOutputVolumeScaling(ch_id,
2279 volume)) {
2280 LOG_RTCERR2(SetChannelOutputVolumeScaling, ch_id, volume);
2281 return false;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002282 }
solenberg1ac56142015-10-13 03:58:19 -07002283 LOG(LS_INFO) << "SetOutputVolume to " << volume
2284 << " for channel " << ch_id << " and ssrc " << ssrc;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002285 return true;
2286}
2287
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002288bool WebRtcVoiceMediaChannel::CanInsertDtmf() {
Fredrik Solenbergb5727682015-12-04 15:22:19 +01002289 return dtmf_payload_type_ ? true : false;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002290}
2291
solenberg1d63dd02015-12-02 12:35:09 -08002292bool WebRtcVoiceMediaChannel::InsertDtmf(uint32_t ssrc, int event,
2293 int duration) {
solenberg566ef242015-11-06 15:34:49 -08002294 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
Fredrik Solenbergb5727682015-12-04 15:22:19 +01002295 LOG(LS_INFO) << "WebRtcVoiceMediaChannel::InsertDtmf";
2296 if (!dtmf_payload_type_) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002297 return false;
2298 }
2299
Fredrik Solenbergb5727682015-12-04 15:22:19 +01002300 // Figure out which WebRtcAudioSendStream to send the event on.
2301 auto it = ssrc != 0 ? send_streams_.find(ssrc) : send_streams_.begin();
2302 if (it == send_streams_.end()) {
2303 LOG(LS_WARNING) << "The specified ssrc " << ssrc << " is not in use.";
solenberg1d63dd02015-12-02 12:35:09 -08002304 return false;
2305 }
Fredrik Solenbergb5727682015-12-04 15:22:19 +01002306 if (event < kMinTelephoneEventCode ||
2307 event > kMaxTelephoneEventCode) {
2308 LOG(LS_WARNING) << "DTMF event code " << event << " out of range.";
solenberg1d63dd02015-12-02 12:35:09 -08002309 return false;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002310 }
Fredrik Solenbergb5727682015-12-04 15:22:19 +01002311 if (duration < kMinTelephoneEventDuration ||
2312 duration > kMaxTelephoneEventDuration) {
2313 LOG(LS_WARNING) << "DTMF event duration " << duration << " out of range.";
2314 return false;
2315 }
2316 return it->second->SendTelephoneEvent(*dtmf_payload_type_, event, duration);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002317}
2318
wu@webrtc.orga9890802013-12-13 00:21:03 +00002319void WebRtcVoiceMediaChannel::OnPacketReceived(
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00002320 rtc::Buffer* packet, const rtc::PacketTime& packet_time) {
solenberg566ef242015-11-06 15:34:49 -08002321 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
Fredrik Solenberg4b60c732015-05-07 14:07:48 +02002322
solenberg1ac56142015-10-13 03:58:19 -07002323 uint32_t ssrc = 0;
2324 if (!GetRtpSsrc(packet->data(), packet->size(), &ssrc)) {
2325 return;
2326 }
2327
solenberg7e63ef02015-11-20 00:19:43 -08002328 // If we don't have a default channel, and the SSRC is unknown, create a
2329 // default channel.
2330 if (default_recv_ssrc_ == -1 && GetReceiveChannelId(ssrc) == -1) {
solenberg1ac56142015-10-13 03:58:19 -07002331 StreamParams sp;
2332 sp.ssrcs.push_back(ssrc);
2333 LOG(LS_INFO) << "Creating default receive stream for SSRC=" << ssrc << ".";
2334 if (!AddRecvStream(sp)) {
2335 LOG(LS_WARNING) << "Could not create default receive stream.";
2336 return;
2337 }
2338 default_recv_ssrc_ = ssrc;
2339 SetOutputVolume(default_recv_ssrc_, default_recv_volume_);
2340 }
2341
2342 // Forward packet to Call. If the SSRC is unknown we'll return after this.
Fredrik Solenberg709ed672015-09-15 12:26:33 +02002343 const webrtc::PacketTime webrtc_packet_time(packet_time.timestamp,
2344 packet_time.not_before);
solenberg1ac56142015-10-13 03:58:19 -07002345 webrtc::PacketReceiver::DeliveryStatus delivery_result =
2346 call_->Receiver()->DeliverPacket(webrtc::MediaType::AUDIO,
2347 reinterpret_cast<const uint8_t*>(packet->data()), packet->size(),
2348 webrtc_packet_time);
2349 if (webrtc::PacketReceiver::DELIVERY_OK != delivery_result) {
solenberg7e63ef02015-11-20 00:19:43 -08002350 // If the SSRC is unknown here, route it to the default channel, if we have
2351 // one. See: https://bugs.chromium.org/p/webrtc/issues/detail?id=5208
2352 if (default_recv_ssrc_ == -1) {
2353 return;
2354 } else {
2355 ssrc = default_recv_ssrc_;
2356 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002357 }
2358
solenberg1ac56142015-10-13 03:58:19 -07002359 // Find the channel to send this packet to. It must exist since webrtc::Call
2360 // was able to demux the packet.
2361 int channel = GetReceiveChannelId(ssrc);
2362 RTC_DCHECK(channel != -1);
2363
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002364 // Pass it off to the decoder.
henrike@webrtc.org28654cb2013-07-22 21:07:49 +00002365 engine()->voe()->network()->ReceivedRTPPacket(
solenberg1ac56142015-10-13 03:58:19 -07002366 channel, packet->data(), packet->size(), webrtc_packet_time);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002367}
2368
wu@webrtc.orga9890802013-12-13 00:21:03 +00002369void WebRtcVoiceMediaChannel::OnRtcpReceived(
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00002370 rtc::Buffer* packet, const rtc::PacketTime& packet_time) {
solenberg566ef242015-11-06 15:34:49 -08002371 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
Fredrik Solenberg4b60c732015-05-07 14:07:48 +02002372
Fredrik Solenberg709ed672015-09-15 12:26:33 +02002373 // Forward packet to Call as well.
2374 const webrtc::PacketTime webrtc_packet_time(packet_time.timestamp,
2375 packet_time.not_before);
2376 call_->Receiver()->DeliverPacket(webrtc::MediaType::AUDIO,
2377 reinterpret_cast<const uint8_t*>(packet->data()), packet->size(),
2378 webrtc_packet_time);
Fredrik Solenberg4b60c732015-05-07 14:07:48 +02002379
wu@webrtc.org9dba5252013-08-05 20:36:57 +00002380 // Sending channels need all RTCP packets with feedback information.
2381 // Even sender reports can contain attached report blocks.
2382 // Receiving channels need sender reports in order to create
2383 // correct receiver reports.
2384 int type = 0;
kwiberg@webrtc.orgeebcab52015-03-24 09:19:06 +00002385 if (!GetRtcpType(packet->data(), packet->size(), &type)) {
wu@webrtc.org9dba5252013-08-05 20:36:57 +00002386 LOG(LS_WARNING) << "Failed to parse type from received RTCP packet";
2387 return;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002388 }
2389
solenberg0b675462015-10-09 01:37:09 -07002390 // If it is a sender report, find the receive channel that is listening.
wu@webrtc.org9dba5252013-08-05 20:36:57 +00002391 if (type == kRtcpTypeSR) {
solenberg0b675462015-10-09 01:37:09 -07002392 uint32_t ssrc = 0;
2393 if (!GetRtcpSsrc(packet->data(), packet->size(), &ssrc)) {
2394 return;
2395 }
2396 int recv_channel_id = GetReceiveChannelId(ssrc);
2397 if (recv_channel_id != -1) {
wu@webrtc.org9dba5252013-08-05 20:36:57 +00002398 engine()->voe()->network()->ReceivedRTCPPacket(
solenberg0b675462015-10-09 01:37:09 -07002399 recv_channel_id, packet->data(), packet->size());
wu@webrtc.org9dba5252013-08-05 20:36:57 +00002400 }
2401 }
2402
2403 // SR may continue RR and any RR entry may correspond to any one of the send
2404 // channels. So all RTCP packets must be forwarded all send channels. VoE
2405 // will filter out RR internally.
solenbergc96df772015-10-21 13:01:53 -07002406 for (const auto& ch : send_streams_) {
wu@webrtc.org9dba5252013-08-05 20:36:57 +00002407 engine()->voe()->network()->ReceivedRTCPPacket(
Fredrik Solenbergaf9fb212015-08-26 10:45:53 +02002408 ch.second->channel(), packet->data(), packet->size());
wu@webrtc.org9dba5252013-08-05 20:36:57 +00002409 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002410}
2411
Peter Boström0c4e06b2015-10-07 12:23:21 +02002412bool WebRtcVoiceMediaChannel::MuteStream(uint32_t ssrc, bool muted) {
solenberg566ef242015-11-06 15:34:49 -08002413 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
solenberg0a617e22015-10-20 15:49:38 -07002414 int channel = GetSendChannelId(ssrc);
wu@webrtc.org9dba5252013-08-05 20:36:57 +00002415 if (channel == -1) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002416 LOG(LS_WARNING) << "The specified ssrc " << ssrc << " is not in use.";
2417 return false;
2418 }
wu@webrtc.org9dba5252013-08-05 20:36:57 +00002419 if (engine()->voe()->volume()->SetInputMute(channel, muted) == -1) {
2420 LOG_RTCERR2(SetInputMute, channel, muted);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002421 return false;
2422 }
buildbot@webrtc.org6b21b712014-07-31 15:08:53 +00002423 // We set the AGC to mute state only when all the channels are muted.
2424 // This implementation is not ideal, instead we should signal the AGC when
2425 // the mic channel is muted/unmuted. We can't do it today because there
2426 // is no good way to know which stream is mapping to the mic channel.
2427 bool all_muted = muted;
solenbergc96df772015-10-21 13:01:53 -07002428 for (const auto& ch : send_streams_) {
Fredrik Solenbergaf9fb212015-08-26 10:45:53 +02002429 if (!all_muted) {
2430 break;
2431 }
2432 if (engine()->voe()->volume()->GetInputMute(ch.second->channel(),
buildbot@webrtc.org6b21b712014-07-31 15:08:53 +00002433 all_muted)) {
Fredrik Solenbergaf9fb212015-08-26 10:45:53 +02002434 LOG_RTCERR1(GetInputMute, ch.second->channel());
buildbot@webrtc.org6b21b712014-07-31 15:08:53 +00002435 return false;
2436 }
2437 }
2438
2439 webrtc::AudioProcessing* ap = engine()->voe()->base()->audio_processing();
solenberg0a617e22015-10-20 15:49:38 -07002440 if (ap) {
buildbot@webrtc.org6b21b712014-07-31 15:08:53 +00002441 ap->set_output_will_be_muted(all_muted);
solenberg0a617e22015-10-20 15:49:38 -07002442 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002443 return true;
2444}
2445
minyue@webrtc.org26236952014-10-29 02:27:08 +00002446// TODO(minyue): SetMaxSendBandwidth() is subject to be renamed to
2447// SetMaxSendBitrate() in future.
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00002448bool WebRtcVoiceMediaChannel::SetMaxSendBandwidth(int bps) {
minyue@webrtc.org26236952014-10-29 02:27:08 +00002449 LOG(LS_INFO) << "WebRtcVoiceMediaChannel::SetMaxSendBandwidth.";
minyue@webrtc.org26236952014-10-29 02:27:08 +00002450 return SetSendBitrateInternal(bps);
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00002451}
2452
minyue@webrtc.org26236952014-10-29 02:27:08 +00002453bool WebRtcVoiceMediaChannel::SetSendBitrateInternal(int bps) {
2454 LOG(LS_INFO) << "WebRtcVoiceMediaChannel::SetSendBitrateInternal.";
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00002455
minyue@webrtc.org26236952014-10-29 02:27:08 +00002456 send_bitrate_setting_ = true;
2457 send_bitrate_bps_ = bps;
wu@webrtc.org1d1ffc92013-10-16 18:12:02 +00002458
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002459 if (!send_codec_) {
wu@webrtc.org1d1ffc92013-10-16 18:12:02 +00002460 LOG(LS_INFO) << "The send codec has not been set up yet. "
minyue@webrtc.org26236952014-10-29 02:27:08 +00002461 << "The send bitrate setting will be applied later.";
wu@webrtc.org1d1ffc92013-10-16 18:12:02 +00002462 return true;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002463 }
2464
minyue@webrtc.org26236952014-10-29 02:27:08 +00002465 // Bitrate is auto by default.
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00002466 // TODO(bemasc): Fix this so that if SetMaxSendBandwidth(50) is followed by
2467 // SetMaxSendBandwith(0), the second call removes the previous limit.
2468 if (bps <= 0)
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002469 return true;
2470
2471 webrtc::CodecInst codec = *send_codec_;
solenberg26c8c912015-11-27 04:00:25 -08002472 bool is_multi_rate = WebRtcVoiceCodecs::IsCodecMultiRate(codec);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002473
2474 if (is_multi_rate) {
2475 // If codec is multi-rate then just set the bitrate.
2476 codec.rate = bps;
solenbergc96df772015-10-21 13:01:53 -07002477 for (const auto& ch : send_streams_) {
solenberg0a617e22015-10-20 15:49:38 -07002478 if (!SetSendCodec(ch.second->channel(), codec)) {
2479 LOG(LS_INFO) << "Failed to set codec " << codec.plname
2480 << " to bitrate " << bps << " bps.";
2481 return false;
2482 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002483 }
2484 return true;
2485 } else {
2486 // If codec is not multi-rate and |bps| is less than the fixed bitrate
2487 // then fail. If codec is not multi-rate and |bps| exceeds or equal the
2488 // fixed bitrate then ignore.
2489 if (bps < codec.rate) {
2490 LOG(LS_INFO) << "Failed to set codec " << codec.plname
2491 << " to bitrate " << bps << " bps"
2492 << ", requires at least " << codec.rate << " bps.";
2493 return false;
2494 }
2495 return true;
2496 }
2497}
2498
2499bool WebRtcVoiceMediaChannel::GetStats(VoiceMediaInfo* info) {
solenberg566ef242015-11-06 15:34:49 -08002500 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
solenberg85a04962015-10-27 03:35:21 -07002501 RTC_DCHECK(info);
solenbergd97ec302015-10-07 01:40:33 -07002502
solenberg85a04962015-10-27 03:35:21 -07002503 // Get SSRC and stats for each sender.
2504 RTC_DCHECK(info->senders.size() == 0);
2505 for (const auto& stream : send_streams_) {
2506 webrtc::AudioSendStream::Stats stats = stream.second->GetStats();
wu@webrtc.org9dba5252013-08-05 20:36:57 +00002507 VoiceSenderInfo sinfo;
solenberg85a04962015-10-27 03:35:21 -07002508 sinfo.add_ssrc(stats.local_ssrc);
2509 sinfo.bytes_sent = stats.bytes_sent;
2510 sinfo.packets_sent = stats.packets_sent;
2511 sinfo.packets_lost = stats.packets_lost;
2512 sinfo.fraction_lost = stats.fraction_lost;
2513 sinfo.codec_name = stats.codec_name;
2514 sinfo.ext_seqnum = stats.ext_seqnum;
2515 sinfo.jitter_ms = stats.jitter_ms;
2516 sinfo.rtt_ms = stats.rtt_ms;
2517 sinfo.audio_level = stats.audio_level;
2518 sinfo.aec_quality_min = stats.aec_quality_min;
2519 sinfo.echo_delay_median_ms = stats.echo_delay_median_ms;
2520 sinfo.echo_delay_std_ms = stats.echo_delay_std_ms;
2521 sinfo.echo_return_loss = stats.echo_return_loss;
2522 sinfo.echo_return_loss_enhancement = stats.echo_return_loss_enhancement;
solenberg566ef242015-11-06 15:34:49 -08002523 sinfo.typing_noise_detected =
2524 (send_ == SEND_NOTHING ? false : stats.typing_noise_detected);
wu@webrtc.org9dba5252013-08-05 20:36:57 +00002525 info->senders.push_back(sinfo);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002526 }
2527
solenberg85a04962015-10-27 03:35:21 -07002528 // Get SSRC and stats for each receiver.
2529 RTC_DCHECK(info->receivers.size() == 0);
solenberg7add0582015-11-20 09:59:34 -08002530 for (const auto& stream : recv_streams_) {
Fredrik Solenberg4f4ec0a2015-10-22 10:49:27 +02002531 webrtc::AudioReceiveStream::Stats stats = stream.second->GetStats();
2532 VoiceReceiverInfo rinfo;
2533 rinfo.add_ssrc(stats.remote_ssrc);
2534 rinfo.bytes_rcvd = stats.bytes_rcvd;
2535 rinfo.packets_rcvd = stats.packets_rcvd;
2536 rinfo.packets_lost = stats.packets_lost;
2537 rinfo.fraction_lost = stats.fraction_lost;
2538 rinfo.codec_name = stats.codec_name;
2539 rinfo.ext_seqnum = stats.ext_seqnum;
2540 rinfo.jitter_ms = stats.jitter_ms;
2541 rinfo.jitter_buffer_ms = stats.jitter_buffer_ms;
2542 rinfo.jitter_buffer_preferred_ms = stats.jitter_buffer_preferred_ms;
2543 rinfo.delay_estimate_ms = stats.delay_estimate_ms;
2544 rinfo.audio_level = stats.audio_level;
2545 rinfo.expand_rate = stats.expand_rate;
2546 rinfo.speech_expand_rate = stats.speech_expand_rate;
2547 rinfo.secondary_decoded_rate = stats.secondary_decoded_rate;
2548 rinfo.accelerate_rate = stats.accelerate_rate;
2549 rinfo.preemptive_expand_rate = stats.preemptive_expand_rate;
2550 rinfo.decoding_calls_to_silence_generator =
2551 stats.decoding_calls_to_silence_generator;
2552 rinfo.decoding_calls_to_neteq = stats.decoding_calls_to_neteq;
2553 rinfo.decoding_normal = stats.decoding_normal;
2554 rinfo.decoding_plc = stats.decoding_plc;
2555 rinfo.decoding_cng = stats.decoding_cng;
2556 rinfo.decoding_plc_cng = stats.decoding_plc_cng;
2557 rinfo.capture_start_ntp_time_ms = stats.capture_start_ntp_time_ms;
2558 info->receivers.push_back(rinfo);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002559 }
2560
2561 return true;
2562}
2563
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002564int WebRtcVoiceMediaChannel::GetOutputLevel(int channel) {
solenbergd97ec302015-10-07 01:40:33 -07002565 unsigned int ulevel = 0;
2566 int ret = engine()->voe()->volume()->GetSpeechOutputLevel(channel, ulevel);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002567 return (ret == 0) ? static_cast<int>(ulevel) : -1;
2568}
2569
Peter Boström0c4e06b2015-10-07 12:23:21 +02002570int WebRtcVoiceMediaChannel::GetReceiveChannelId(uint32_t ssrc) const {
solenberg566ef242015-11-06 15:34:49 -08002571 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
solenberg7add0582015-11-20 09:59:34 -08002572 const auto it = recv_streams_.find(ssrc);
2573 if (it != recv_streams_.end()) {
mallinath@webrtc.org67ee6b92014-02-03 16:57:16 +00002574 return it->second->channel();
solenberg8fb30c32015-10-13 03:06:58 -07002575 }
solenberg1ac56142015-10-13 03:58:19 -07002576 return -1;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002577}
2578
Peter Boström0c4e06b2015-10-07 12:23:21 +02002579int WebRtcVoiceMediaChannel::GetSendChannelId(uint32_t ssrc) const {
solenberg566ef242015-11-06 15:34:49 -08002580 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
solenbergc96df772015-10-21 13:01:53 -07002581 const auto it = send_streams_.find(ssrc);
2582 if (it != send_streams_.end()) {
mallinath@webrtc.org67ee6b92014-02-03 16:57:16 +00002583 return it->second->channel();
solenberg8fb30c32015-10-13 03:06:58 -07002584 }
wu@webrtc.org9dba5252013-08-05 20:36:57 +00002585 return -1;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002586}
2587
2588bool WebRtcVoiceMediaChannel::GetRedSendCodec(const AudioCodec& red_codec,
2589 const std::vector<AudioCodec>& all_codecs, webrtc::CodecInst* send_codec) {
2590 // Get the RED encodings from the parameter with no name. This may
2591 // change based on what is discussed on the Jingle list.
2592 // The encoding parameter is of the form "a/b"; we only support where
2593 // a == b. Verify this and parse out the value into red_pt.
2594 // If the parameter value is absent (as it will be until we wire up the
2595 // signaling of this message), use the second codec specified (i.e. the
2596 // one after "red") as the encoding parameter.
2597 int red_pt = -1;
2598 std::string red_params;
2599 CodecParameterMap::const_iterator it = red_codec.params.find("");
2600 if (it != red_codec.params.end()) {
2601 red_params = it->second;
2602 std::vector<std::string> red_pts;
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00002603 if (rtc::split(red_params, '/', &red_pts) != 2 ||
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002604 red_pts[0] != red_pts[1] ||
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00002605 !rtc::FromString(red_pts[0], &red_pt)) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002606 LOG(LS_WARNING) << "RED params " << red_params << " not supported.";
2607 return false;
2608 }
2609 } else if (red_codec.params.empty()) {
2610 LOG(LS_WARNING) << "RED params not present, using defaults";
2611 if (all_codecs.size() > 1) {
2612 red_pt = all_codecs[1].id;
2613 }
2614 }
2615
2616 // Try to find red_pt in |codecs|.
Fredrik Solenbergaf9fb212015-08-26 10:45:53 +02002617 for (const AudioCodec& codec : all_codecs) {
2618 if (codec.id == red_pt) {
2619 // If we find the right codec, that will be the codec we pass to
2620 // SetSendCodec, with the desired payload type.
solenberg26c8c912015-11-27 04:00:25 -08002621 if (WebRtcVoiceEngine::ToCodecInst(codec, send_codec)) {
Fredrik Solenbergaf9fb212015-08-26 10:45:53 +02002622 return true;
2623 } else {
2624 break;
2625 }
2626 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002627 }
Fredrik Solenbergaf9fb212015-08-26 10:45:53 +02002628 LOG(LS_WARNING) << "RED params " << red_params << " are invalid.";
2629 return false;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002630}
2631
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002632bool WebRtcVoiceMediaChannel::SetPlayout(int channel, bool playout) {
2633 if (playout) {
2634 LOG(LS_INFO) << "Starting playout for channel #" << channel;
2635 if (engine()->voe()->base()->StartPlayout(channel) == -1) {
2636 LOG_RTCERR1(StartPlayout, channel);
2637 return false;
2638 }
2639 } else {
2640 LOG(LS_INFO) << "Stopping playout for channel #" << channel;
2641 engine()->voe()->base()->StopPlayout(channel);
2642 }
2643 return true;
2644}
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002645} // namespace cricket
2646
2647#endif // HAVE_WEBRTC_VOICE