blob: fd0fc4be173e3300b2d37fd307a81aa6a73cdab0 [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
solenberg26c8c912015-11-27 04:00:25 -0800520 options_ = GetDefaultEngineOptions();
Stefan Holmerb86d4e42015-12-07 10:26:18 +0100521 voe_config_.Set<webrtc::VoicePacing>(new webrtc::VoicePacing(true));
henrik.lundin@webrtc.orgf85dbce2014-11-07 12:25:00 +0000522}
523
buildbot@webrtc.org13d67762014-05-02 17:33:29 +0000524WebRtcVoiceEngine::~WebRtcVoiceEngine() {
solenberg566ef242015-11-06 15:34:49 -0800525 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
buildbot@webrtc.org13d67762014-05-02 17:33:29 +0000526 LOG(LS_VERBOSE) << "WebRtcVoiceEngine::~WebRtcVoiceEngine";
buildbot@webrtc.org13d67762014-05-02 17:33:29 +0000527 if (adm_) {
528 voe_wrapper_.reset();
529 adm_->Release();
530 adm_ = NULL;
531 }
solenbergbd138382015-11-20 16:08:07 -0800532 webrtc::Trace::SetTraceCallback(nullptr);
buildbot@webrtc.org13d67762014-05-02 17:33:29 +0000533}
534
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000535bool WebRtcVoiceEngine::Init(rtc::Thread* worker_thread) {
solenberg566ef242015-11-06 15:34:49 -0800536 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
henrikg91d6ede2015-09-17 00:24:34 -0700537 RTC_DCHECK(worker_thread == rtc::Thread::Current());
buildbot@webrtc.org13d67762014-05-02 17:33:29 +0000538 LOG(LS_INFO) << "WebRtcVoiceEngine::Init";
539 bool res = InitInternal();
540 if (res) {
541 LOG(LS_INFO) << "WebRtcVoiceEngine::Init Done!";
542 } else {
543 LOG(LS_ERROR) << "WebRtcVoiceEngine::Init failed";
544 Terminate();
545 }
546 return res;
547}
548
549bool WebRtcVoiceEngine::InitInternal() {
solenberg566ef242015-11-06 15:34:49 -0800550 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
buildbot@webrtc.org13d67762014-05-02 17:33:29 +0000551 // Temporarily turn logging level up for the Init call
solenbergbd138382015-11-20 16:08:07 -0800552 webrtc::Trace::set_level_filter(kElevatedTraceFilter);
solenberg2515af22015-12-02 06:19:36 -0800553 LOG(LS_INFO) << webrtc::VoiceEngine::GetVersionString();
buildbot@webrtc.org13d67762014-05-02 17:33:29 +0000554 if (voe_wrapper_->base()->Init(adm_) == -1) {
555 LOG_RTCERR0_EX(Init, voe_wrapper_->error());
buildbot@webrtc.org13d67762014-05-02 17:33:29 +0000556 return false;
557 }
solenbergbd138382015-11-20 16:08:07 -0800558 webrtc::Trace::set_level_filter(kDefaultTraceFilter);
buildbot@webrtc.org13d67762014-05-02 17:33:29 +0000559
buildbot@webrtc.org13d67762014-05-02 17:33:29 +0000560 // Save the default AGC configuration settings. This must happen before
561 // calling SetOptions or the default will be overwritten.
562 if (voe_wrapper_->processing()->GetAgcConfig(default_agc_config_) == -1) {
563 LOG_RTCERR0(GetAgcConfig);
564 return false;
565 }
566
567 // Set defaults for options, so that ApplyOptions applies them explicitly
568 // when we clear option (channel) overrides. External clients can still
569 // modify the defaults via SetOptions (on the media engine).
570 if (!SetOptions(GetDefaultEngineOptions())) {
571 return false;
572 }
573
574 // Print our codec list again for the call diagnostic log
575 LOG(LS_INFO) << "WebRtc VoiceEngine codecs:";
Fredrik Solenbergaf9fb212015-08-26 10:45:53 +0200576 for (const AudioCodec& codec : codecs_) {
577 LOG(LS_INFO) << ToString(codec);
buildbot@webrtc.org13d67762014-05-02 17:33:29 +0000578 }
579
buildbot@webrtc.org13d67762014-05-02 17:33:29 +0000580 initialized_ = true;
581 return true;
582}
583
buildbot@webrtc.org13d67762014-05-02 17:33:29 +0000584void WebRtcVoiceEngine::Terminate() {
solenberg566ef242015-11-06 15:34:49 -0800585 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
buildbot@webrtc.org13d67762014-05-02 17:33:29 +0000586 LOG(LS_INFO) << "WebRtcVoiceEngine::Terminate";
587 initialized_ = false;
588
589 StopAecDump();
590
buildbot@webrtc.org13d67762014-05-02 17:33:29 +0000591 voe_wrapper_->base()->Terminate();
buildbot@webrtc.org13d67762014-05-02 17:33:29 +0000592}
593
solenberg566ef242015-11-06 15:34:49 -0800594rtc::scoped_refptr<webrtc::AudioState>
595 WebRtcVoiceEngine::GetAudioState() const {
596 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
597 return audio_state_;
598}
599
Fredrik Solenberg709ed672015-09-15 12:26:33 +0200600VoiceMediaChannel* WebRtcVoiceEngine::CreateChannel(webrtc::Call* call,
Jelena Marusicc28a8962015-05-29 15:05:44 +0200601 const AudioOptions& options) {
solenberg566ef242015-11-06 15:34:49 -0800602 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
solenberg0a617e22015-10-20 15:49:38 -0700603 return new WebRtcVoiceMediaChannel(this, options, call);
buildbot@webrtc.org13d67762014-05-02 17:33:29 +0000604}
605
buildbot@webrtc.org13d67762014-05-02 17:33:29 +0000606bool WebRtcVoiceEngine::SetOptions(const AudioOptions& options) {
solenberg566ef242015-11-06 15:34:49 -0800607 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
buildbot@webrtc.org13d67762014-05-02 17:33:29 +0000608 if (!ApplyOptions(options)) {
609 return false;
610 }
611 options_ = options;
612 return true;
613}
614
buildbot@webrtc.org13d67762014-05-02 17:33:29 +0000615// AudioOptions defaults are set in InitInternal (for options with corresponding
616// MediaEngineInterface flags) and in SetOptions(int) for flagless options.
617bool WebRtcVoiceEngine::ApplyOptions(const AudioOptions& options_in) {
solenberg566ef242015-11-06 15:34:49 -0800618 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
henrikac14f5ff2015-09-23 14:08:33 +0200619 LOG(LS_INFO) << "ApplyOptions: " << options_in.ToString();
buildbot@webrtc.org13d67762014-05-02 17:33:29 +0000620 AudioOptions options = options_in; // The options are modified below.
621 // kEcConference is AEC with high suppression.
622 webrtc::EcModes ec_mode = webrtc::kEcConference;
623 webrtc::AecmModes aecm_mode = webrtc::kAecmSpeakerphone;
624 webrtc::AgcModes agc_mode = webrtc::kAgcAdaptiveAnalog;
625 webrtc::NsModes ns_mode = webrtc::kNsHighSuppression;
kwiberg102c6a62015-10-30 02:47:38 -0700626 if (options.aecm_generate_comfort_noise) {
buildbot@webrtc.org13d67762014-05-02 17:33:29 +0000627 LOG(LS_VERBOSE) << "Comfort noise explicitly set to "
kwiberg102c6a62015-10-30 02:47:38 -0700628 << *options.aecm_generate_comfort_noise
629 << " (default is false).";
buildbot@webrtc.org13d67762014-05-02 17:33:29 +0000630 }
631
632#if defined(IOS)
633 // On iOS, VPIO provides built-in EC and AGC.
Karl Wibergbe579832015-11-10 22:34:18 +0100634 options.echo_cancellation = rtc::Optional<bool>(false);
635 options.auto_gain_control = rtc::Optional<bool>(false);
henrika86d907c2015-09-07 16:09:50 +0200636 LOG(LS_INFO) << "Always disable AEC and AGC on iOS. Use built-in instead.";
buildbot@webrtc.org13d67762014-05-02 17:33:29 +0000637#elif defined(ANDROID)
638 ec_mode = webrtc::kEcAecm;
639#endif
640
641#if defined(IOS) || defined(ANDROID)
642 // Set the AGC mode for iOS as well despite disabling it above, to avoid
643 // unsupported configuration errors from webrtc.
644 agc_mode = webrtc::kAgcFixedDigital;
Karl Wibergbe579832015-11-10 22:34:18 +0100645 options.typing_detection = rtc::Optional<bool>(false);
646 options.experimental_agc = rtc::Optional<bool>(false);
647 options.extended_filter_aec = rtc::Optional<bool>(false);
648 options.experimental_ns = rtc::Optional<bool>(false);
buildbot@webrtc.org13d67762014-05-02 17:33:29 +0000649#endif
650
Bjorn Volckerbf395c12015-03-25 22:45:56 +0100651 // Delay Agnostic AEC automatically turns on EC if not set except on iOS
652 // where the feature is not supported.
653 bool use_delay_agnostic_aec = false;
654#if !defined(IOS)
kwiberg102c6a62015-10-30 02:47:38 -0700655 if (options.delay_agnostic_aec) {
656 use_delay_agnostic_aec = *options.delay_agnostic_aec;
Bjorn Volckerbf395c12015-03-25 22:45:56 +0100657 if (use_delay_agnostic_aec) {
Karl Wibergbe579832015-11-10 22:34:18 +0100658 options.echo_cancellation = rtc::Optional<bool>(true);
659 options.extended_filter_aec = rtc::Optional<bool>(true);
Bjorn Volckerbf395c12015-03-25 22:45:56 +0100660 ec_mode = webrtc::kEcConference;
661 }
662 }
663#endif
664
buildbot@webrtc.org13d67762014-05-02 17:33:29 +0000665 webrtc::VoEAudioProcessing* voep = voe_wrapper_->processing();
666
kwiberg102c6a62015-10-30 02:47:38 -0700667 if (options.echo_cancellation) {
henrika@webrtc.orga954c072014-12-09 16:22:09 +0000668 // Check if platform supports built-in EC. Currently only supported on
669 // Android and in combination with Java based audio layer.
670 // TODO(henrika): investigate possibility to support built-in EC also
671 // in combination with Open SL ES audio.
672 const bool built_in_aec = voe_wrapper_->hw()->BuiltInAECIsAvailable();
Bjorn Volcker73f72102015-06-03 14:50:15 +0200673 if (built_in_aec) {
Bjorn Volckerccfc9392015-05-07 07:43:17 +0200674 // Built-in EC exists on this device and use_delay_agnostic_aec is not
675 // overriding it. Enable/Disable it according to the echo_cancellation
676 // audio option.
Bjorn Volcker73f72102015-06-03 14:50:15 +0200677 const bool enable_built_in_aec =
kwiberg102c6a62015-10-30 02:47:38 -0700678 *options.echo_cancellation && !use_delay_agnostic_aec;
Bjorn Volcker73f72102015-06-03 14:50:15 +0200679 if (voe_wrapper_->hw()->EnableBuiltInAEC(enable_built_in_aec) == 0 &&
680 enable_built_in_aec) {
Bjorn Volckerbf395c12015-03-25 22:45:56 +0100681 // Disable internal software EC if built-in EC is enabled,
henrika@webrtc.orga954c072014-12-09 16:22:09 +0000682 // i.e., replace the software EC with the built-in EC.
Karl Wibergbe579832015-11-10 22:34:18 +0100683 options.echo_cancellation = rtc::Optional<bool>(false);
henrika@webrtc.orga954c072014-12-09 16:22:09 +0000684 LOG(LS_INFO) << "Disabling EC since built-in EC will be used instead";
685 }
686 }
kwiberg102c6a62015-10-30 02:47:38 -0700687 if (voep->SetEcStatus(*options.echo_cancellation, ec_mode) == -1) {
688 LOG_RTCERR2(SetEcStatus, *options.echo_cancellation, ec_mode);
buildbot@webrtc.org13d67762014-05-02 17:33:29 +0000689 return false;
690 } else {
kwiberg102c6a62015-10-30 02:47:38 -0700691 LOG(LS_INFO) << "Echo control set to " << *options.echo_cancellation
henrika86d907c2015-09-07 16:09:50 +0200692 << " with mode " << ec_mode;
buildbot@webrtc.org13d67762014-05-02 17:33:29 +0000693 }
694#if !defined(ANDROID)
695 // TODO(ajm): Remove the error return on Android from webrtc.
kwiberg102c6a62015-10-30 02:47:38 -0700696 if (voep->SetEcMetricsStatus(*options.echo_cancellation) == -1) {
697 LOG_RTCERR1(SetEcMetricsStatus, *options.echo_cancellation);
buildbot@webrtc.org13d67762014-05-02 17:33:29 +0000698 return false;
699 }
700#endif
701 if (ec_mode == webrtc::kEcAecm) {
kwiberg102c6a62015-10-30 02:47:38 -0700702 bool cn = options.aecm_generate_comfort_noise.value_or(false);
703 if (voep->SetAecmMode(aecm_mode, cn) != 0) {
704 LOG_RTCERR2(SetAecmMode, aecm_mode, cn);
buildbot@webrtc.org13d67762014-05-02 17:33:29 +0000705 return false;
706 }
707 }
708 }
709
kwiberg102c6a62015-10-30 02:47:38 -0700710 if (options.auto_gain_control) {
henrikac14f5ff2015-09-23 14:08:33 +0200711 const bool built_in_agc = voe_wrapper_->hw()->BuiltInAGCIsAvailable();
712 if (built_in_agc) {
kwiberg102c6a62015-10-30 02:47:38 -0700713 if (voe_wrapper_->hw()->EnableBuiltInAGC(*options.auto_gain_control) ==
714 0 &&
715 *options.auto_gain_control) {
henrikac14f5ff2015-09-23 14:08:33 +0200716 // Disable internal software AGC if built-in AGC is enabled,
717 // i.e., replace the software AGC with the built-in AGC.
Karl Wibergbe579832015-11-10 22:34:18 +0100718 options.auto_gain_control = rtc::Optional<bool>(false);
henrikac14f5ff2015-09-23 14:08:33 +0200719 LOG(LS_INFO) << "Disabling AGC since built-in AGC will be used instead";
720 }
721 }
kwiberg102c6a62015-10-30 02:47:38 -0700722 if (voep->SetAgcStatus(*options.auto_gain_control, agc_mode) == -1) {
723 LOG_RTCERR2(SetAgcStatus, *options.auto_gain_control, agc_mode);
buildbot@webrtc.org13d67762014-05-02 17:33:29 +0000724 return false;
725 } else {
kwiberg102c6a62015-10-30 02:47:38 -0700726 LOG(LS_INFO) << "Auto gain set to " << *options.auto_gain_control
727 << " with mode " << agc_mode;
buildbot@webrtc.org13d67762014-05-02 17:33:29 +0000728 }
729 }
730
kwiberg102c6a62015-10-30 02:47:38 -0700731 if (options.tx_agc_target_dbov || options.tx_agc_digital_compression_gain ||
732 options.tx_agc_limiter) {
buildbot@webrtc.org13d67762014-05-02 17:33:29 +0000733 // Override default_agc_config_. Generally, an unset option means "leave
734 // the VoE bits alone" in this function, so we want whatever is set to be
735 // stored as the new "default". If we didn't, then setting e.g.
736 // tx_agc_target_dbov would reset digital compression gain and limiter
737 // settings.
738 // Also, if we don't update default_agc_config_, then adjust_agc_delta
739 // would be an offset from the original values, and not whatever was set
740 // explicitly.
kwiberg102c6a62015-10-30 02:47:38 -0700741 default_agc_config_.targetLeveldBOv = options.tx_agc_target_dbov.value_or(
742 default_agc_config_.targetLeveldBOv);
buildbot@webrtc.org13d67762014-05-02 17:33:29 +0000743 default_agc_config_.digitalCompressionGaindB =
kwiberg102c6a62015-10-30 02:47:38 -0700744 options.tx_agc_digital_compression_gain.value_or(
buildbot@webrtc.org13d67762014-05-02 17:33:29 +0000745 default_agc_config_.digitalCompressionGaindB);
746 default_agc_config_.limiterEnable =
kwiberg102c6a62015-10-30 02:47:38 -0700747 options.tx_agc_limiter.value_or(default_agc_config_.limiterEnable);
buildbot@webrtc.org13d67762014-05-02 17:33:29 +0000748 if (voe_wrapper_->processing()->SetAgcConfig(default_agc_config_) == -1) {
749 LOG_RTCERR3(SetAgcConfig,
750 default_agc_config_.targetLeveldBOv,
751 default_agc_config_.digitalCompressionGaindB,
752 default_agc_config_.limiterEnable);
753 return false;
754 }
755 }
756
kwiberg102c6a62015-10-30 02:47:38 -0700757 if (options.noise_suppression) {
henrikac14f5ff2015-09-23 14:08:33 +0200758 const bool built_in_ns = voe_wrapper_->hw()->BuiltInNSIsAvailable();
759 if (built_in_ns) {
kwiberg102c6a62015-10-30 02:47:38 -0700760 if (voe_wrapper_->hw()->EnableBuiltInNS(*options.noise_suppression) ==
761 0 &&
762 *options.noise_suppression) {
henrikac14f5ff2015-09-23 14:08:33 +0200763 // Disable internal software NS if built-in NS is enabled,
764 // i.e., replace the software NS with the built-in NS.
Karl Wibergbe579832015-11-10 22:34:18 +0100765 options.noise_suppression = rtc::Optional<bool>(false);
henrikac14f5ff2015-09-23 14:08:33 +0200766 LOG(LS_INFO) << "Disabling NS since built-in NS will be used instead";
767 }
768 }
kwiberg102c6a62015-10-30 02:47:38 -0700769 if (voep->SetNsStatus(*options.noise_suppression, ns_mode) == -1) {
770 LOG_RTCERR2(SetNsStatus, *options.noise_suppression, ns_mode);
buildbot@webrtc.org13d67762014-05-02 17:33:29 +0000771 return false;
772 } else {
kwiberg102c6a62015-10-30 02:47:38 -0700773 LOG(LS_INFO) << "Noise suppression set to " << *options.noise_suppression
henrikac14f5ff2015-09-23 14:08:33 +0200774 << " with mode " << ns_mode;
buildbot@webrtc.org13d67762014-05-02 17:33:29 +0000775 }
776 }
777
kwiberg102c6a62015-10-30 02:47:38 -0700778 if (options.highpass_filter) {
779 LOG(LS_INFO) << "High pass filter enabled? " << *options.highpass_filter;
780 if (voep->EnableHighPassFilter(*options.highpass_filter) == -1) {
781 LOG_RTCERR1(SetHighpassFilterStatus, *options.highpass_filter);
buildbot@webrtc.org13d67762014-05-02 17:33:29 +0000782 return false;
783 }
784 }
785
kwiberg102c6a62015-10-30 02:47:38 -0700786 if (options.stereo_swapping) {
787 LOG(LS_INFO) << "Stereo swapping enabled? " << *options.stereo_swapping;
788 voep->EnableStereoChannelSwapping(*options.stereo_swapping);
789 if (voep->IsStereoChannelSwappingEnabled() != *options.stereo_swapping) {
790 LOG_RTCERR1(EnableStereoChannelSwapping, *options.stereo_swapping);
buildbot@webrtc.org13d67762014-05-02 17:33:29 +0000791 return false;
792 }
793 }
794
kwiberg102c6a62015-10-30 02:47:38 -0700795 if (options.audio_jitter_buffer_max_packets) {
796 LOG(LS_INFO) << "NetEq capacity is "
797 << *options.audio_jitter_buffer_max_packets;
Henrik Lundin64dad832015-05-11 12:44:23 +0200798 voe_config_.Set<webrtc::NetEqCapacityConfig>(
kwiberg102c6a62015-10-30 02:47:38 -0700799 new webrtc::NetEqCapacityConfig(
800 *options.audio_jitter_buffer_max_packets));
Henrik Lundin64dad832015-05-11 12:44:23 +0200801 }
802
kwiberg102c6a62015-10-30 02:47:38 -0700803 if (options.audio_jitter_buffer_fast_accelerate) {
804 LOG(LS_INFO) << "NetEq fast mode? "
805 << *options.audio_jitter_buffer_fast_accelerate;
Henrik Lundin5263b3c2015-06-01 10:29:41 +0200806 voe_config_.Set<webrtc::NetEqFastAccelerate>(
kwiberg102c6a62015-10-30 02:47:38 -0700807 new webrtc::NetEqFastAccelerate(
808 *options.audio_jitter_buffer_fast_accelerate));
Henrik Lundin5263b3c2015-06-01 10:29:41 +0200809 }
810
kwiberg102c6a62015-10-30 02:47:38 -0700811 if (options.typing_detection) {
812 LOG(LS_INFO) << "Typing detection is enabled? "
813 << *options.typing_detection;
814 if (voep->SetTypingDetectionStatus(*options.typing_detection) == -1) {
buildbot@webrtc.org13d67762014-05-02 17:33:29 +0000815 // In case of error, log the info and continue
kwiberg102c6a62015-10-30 02:47:38 -0700816 LOG_RTCERR1(SetTypingDetectionStatus, *options.typing_detection);
buildbot@webrtc.org13d67762014-05-02 17:33:29 +0000817 }
818 }
819
kwiberg102c6a62015-10-30 02:47:38 -0700820 if (options.adjust_agc_delta) {
821 LOG(LS_INFO) << "Adjust agc delta is " << *options.adjust_agc_delta;
822 if (!AdjustAgcLevel(*options.adjust_agc_delta)) {
buildbot@webrtc.org13d67762014-05-02 17:33:29 +0000823 return false;
824 }
825 }
826
kwiberg102c6a62015-10-30 02:47:38 -0700827 if (options.aec_dump) {
828 LOG(LS_INFO) << "Aec dump is enabled? " << *options.aec_dump;
829 if (*options.aec_dump)
buildbot@webrtc.org13d67762014-05-02 17:33:29 +0000830 StartAecDump(kAecDumpByAudioOptionFilename);
831 else
832 StopAecDump();
833 }
834
buildbot@webrtc.org1f8a2372014-08-28 10:52:44 +0000835 webrtc::Config config;
836
kwiberg102c6a62015-10-30 02:47:38 -0700837 if (options.delay_agnostic_aec)
838 delay_agnostic_aec_ = options.delay_agnostic_aec;
839 if (delay_agnostic_aec_) {
840 LOG(LS_INFO) << "Delay agnostic aec is enabled? " << *delay_agnostic_aec_;
henrik.lundin0f133b92015-07-02 00:17:55 -0700841 config.Set<webrtc::DelayAgnostic>(
kwiberg102c6a62015-10-30 02:47:38 -0700842 new webrtc::DelayAgnostic(*delay_agnostic_aec_));
Bjorn Volckerbf395c12015-03-25 22:45:56 +0100843 }
844
kwiberg102c6a62015-10-30 02:47:38 -0700845 if (options.extended_filter_aec) {
846 extended_filter_aec_ = options.extended_filter_aec;
847 }
848 if (extended_filter_aec_) {
849 LOG(LS_INFO) << "Extended filter aec is enabled? " << *extended_filter_aec_;
Henrik Lundin441f6342015-06-09 16:03:13 +0200850 config.Set<webrtc::ExtendedFilter>(
kwiberg102c6a62015-10-30 02:47:38 -0700851 new webrtc::ExtendedFilter(*extended_filter_aec_));
buildbot@webrtc.org1f8a2372014-08-28 10:52:44 +0000852 }
853
kwiberg102c6a62015-10-30 02:47:38 -0700854 if (options.experimental_ns) {
855 experimental_ns_ = options.experimental_ns;
856 }
857 if (experimental_ns_) {
858 LOG(LS_INFO) << "Experimental ns is enabled? " << *experimental_ns_;
buildbot@webrtc.org1f8a2372014-08-28 10:52:44 +0000859 config.Set<webrtc::ExperimentalNs>(
kwiberg102c6a62015-10-30 02:47:38 -0700860 new webrtc::ExperimentalNs(*experimental_ns_));
buildbot@webrtc.org1f8a2372014-08-28 10:52:44 +0000861 }
buildbot@webrtc.org1f8a2372014-08-28 10:52:44 +0000862
863 // We check audioproc for the benefit of tests, since FakeWebRtcVoiceEngine
864 // returns NULL on audio_processing().
865 webrtc::AudioProcessing* audioproc = voe_wrapper_->base()->audio_processing();
866 if (audioproc) {
867 audioproc->SetExtraOptions(config);
868 }
869
kwiberg102c6a62015-10-30 02:47:38 -0700870 if (options.recording_sample_rate) {
871 LOG(LS_INFO) << "Recording sample rate is "
872 << *options.recording_sample_rate;
873 if (voe_wrapper_->hw()->SetRecordingSampleRate(
874 *options.recording_sample_rate)) {
875 LOG_RTCERR1(SetRecordingSampleRate, *options.recording_sample_rate);
buildbot@webrtc.org13d67762014-05-02 17:33:29 +0000876 }
877 }
878
kwiberg102c6a62015-10-30 02:47:38 -0700879 if (options.playout_sample_rate) {
880 LOG(LS_INFO) << "Playout sample rate is " << *options.playout_sample_rate;
881 if (voe_wrapper_->hw()->SetPlayoutSampleRate(
882 *options.playout_sample_rate)) {
883 LOG_RTCERR1(SetPlayoutSampleRate, *options.playout_sample_rate);
buildbot@webrtc.org13d67762014-05-02 17:33:29 +0000884 }
885 }
886
887 return true;
888}
889
buildbot@webrtc.org13d67762014-05-02 17:33:29 +0000890// TODO(juberti): Refactor this so that the core logic can be used to set the
891// soundclip device. At that time, reinstate the soundclip pause/resume code.
892bool WebRtcVoiceEngine::SetDevices(const Device* in_device,
893 const Device* out_device) {
solenberg566ef242015-11-06 15:34:49 -0800894 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
buildbot@webrtc.org13d67762014-05-02 17:33:29 +0000895#if !defined(IOS)
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000896 int in_id = in_device ? rtc::FromString<int>(in_device->id) :
buildbot@webrtc.org13d67762014-05-02 17:33:29 +0000897 kDefaultAudioDeviceId;
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000898 int out_id = out_device ? rtc::FromString<int>(out_device->id) :
buildbot@webrtc.org13d67762014-05-02 17:33:29 +0000899 kDefaultAudioDeviceId;
900 // The device manager uses -1 as the default device, which was the case for
901 // VoE 3.5. VoE 4.0, however, uses 0 as the default in Linux and Mac.
902#ifndef WIN32
903 if (-1 == in_id) {
904 in_id = kDefaultAudioDeviceId;
905 }
906 if (-1 == out_id) {
907 out_id = kDefaultAudioDeviceId;
908 }
909#endif
910
911 std::string in_name = (in_id != kDefaultAudioDeviceId) ?
912 in_device->name : "Default device";
913 std::string out_name = (out_id != kDefaultAudioDeviceId) ?
914 out_device->name : "Default device";
915 LOG(LS_INFO) << "Setting microphone to (id=" << in_id << ", name=" << in_name
916 << ") and speaker to (id=" << out_id << ", name=" << out_name
917 << ")";
918
buildbot@webrtc.org13d67762014-05-02 17:33:29 +0000919 // Must also pause all audio playback and capture.
solenbergc1a1b352015-09-22 13:31:20 -0700920 bool ret = true;
Fredrik Solenbergaf9fb212015-08-26 10:45:53 +0200921 for (WebRtcVoiceMediaChannel* channel : channels_) {
buildbot@webrtc.org13d67762014-05-02 17:33:29 +0000922 if (!channel->PausePlayout()) {
923 LOG(LS_WARNING) << "Failed to pause playout";
924 ret = false;
925 }
926 if (!channel->PauseSend()) {
927 LOG(LS_WARNING) << "Failed to pause send";
928 ret = false;
929 }
930 }
931
932 // Find the recording device id in VoiceEngine and set recording device.
933 if (!FindWebRtcAudioDeviceId(true, in_name, in_id, &in_id)) {
934 ret = false;
935 }
936 if (ret) {
937 if (voe_wrapper_->hw()->SetRecordingDevice(in_id) == -1) {
938 LOG_RTCERR2(SetRecordingDevice, in_name, in_id);
939 ret = false;
940 }
buildbot@webrtc.org6b21b712014-07-31 15:08:53 +0000941 webrtc::AudioProcessing* ap = voe()->base()->audio_processing();
942 if (ap)
943 ap->Initialize();
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000944 }
945
946 // Find the playout device id in VoiceEngine and set playout device.
947 if (!FindWebRtcAudioDeviceId(false, out_name, out_id, &out_id)) {
948 LOG(LS_WARNING) << "Failed to find VoiceEngine device id for " << out_name;
949 ret = false;
950 }
951 if (ret) {
952 if (voe_wrapper_->hw()->SetPlayoutDevice(out_id) == -1) {
sergeyu@chromium.org5bc25c42013-12-05 00:24:06 +0000953 LOG_RTCERR2(SetPlayoutDevice, out_name, out_id);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000954 ret = false;
955 }
956 }
957
958 // Resume all audio playback and capture.
Fredrik Solenbergaf9fb212015-08-26 10:45:53 +0200959 for (WebRtcVoiceMediaChannel* channel : channels_) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000960 if (!channel->ResumePlayout()) {
961 LOG(LS_WARNING) << "Failed to resume playout";
962 ret = false;
963 }
964 if (!channel->ResumeSend()) {
965 LOG(LS_WARNING) << "Failed to resume send";
966 ret = false;
967 }
968 }
969
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000970 if (ret) {
971 LOG(LS_INFO) << "Set microphone to (id=" << in_id <<" name=" << in_name
972 << ") and speaker to (id="<< out_id << " name=" << out_name
973 << ")";
974 }
975
976 return ret;
977#else
978 return true;
wu@webrtc.orgcecfd182013-10-30 05:18:12 +0000979#endif // !IOS
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000980}
981
982bool WebRtcVoiceEngine::FindWebRtcAudioDeviceId(
983 bool is_input, const std::string& dev_name, int dev_id, int* rtc_id) {
solenberg566ef242015-11-06 15:34:49 -0800984 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000985 // In Linux, VoiceEngine uses the same device dev_id as the device manager.
wu@webrtc.orgcecfd182013-10-30 05:18:12 +0000986#if defined(LINUX) || defined(ANDROID)
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000987 *rtc_id = dev_id;
988 return true;
989#else
990 // In Windows and Mac, we need to find the VoiceEngine device id by name
991 // unless the input dev_id is the default device id.
992 if (kDefaultAudioDeviceId == dev_id) {
993 *rtc_id = dev_id;
994 return true;
995 }
996
997 // Get the number of VoiceEngine audio devices.
998 int count = 0;
999 if (is_input) {
1000 if (-1 == voe_wrapper_->hw()->GetNumOfRecordingDevices(count)) {
1001 LOG_RTCERR0(GetNumOfRecordingDevices);
1002 return false;
1003 }
1004 } else {
1005 if (-1 == voe_wrapper_->hw()->GetNumOfPlayoutDevices(count)) {
1006 LOG_RTCERR0(GetNumOfPlayoutDevices);
1007 return false;
1008 }
1009 }
1010
1011 for (int i = 0; i < count; ++i) {
1012 char name[128];
1013 char guid[128];
1014 if (is_input) {
1015 voe_wrapper_->hw()->GetRecordingDeviceName(i, name, guid);
1016 LOG(LS_VERBOSE) << "VoiceEngine microphone " << i << ": " << name;
1017 } else {
1018 voe_wrapper_->hw()->GetPlayoutDeviceName(i, name, guid);
1019 LOG(LS_VERBOSE) << "VoiceEngine speaker " << i << ": " << name;
1020 }
1021
1022 std::string webrtc_name(name);
1023 if (dev_name.compare(0, webrtc_name.size(), webrtc_name) == 0) {
1024 *rtc_id = i;
1025 return true;
1026 }
1027 }
1028 LOG(LS_WARNING) << "VoiceEngine cannot find device: " << dev_name;
1029 return false;
1030#endif
1031}
1032
1033bool WebRtcVoiceEngine::GetOutputVolume(int* level) {
solenberg566ef242015-11-06 15:34:49 -08001034 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001035 unsigned int ulevel;
1036 if (voe_wrapper_->volume()->GetSpeakerVolume(ulevel) == -1) {
1037 LOG_RTCERR1(GetSpeakerVolume, level);
1038 return false;
1039 }
1040 *level = ulevel;
1041 return true;
1042}
1043
1044bool WebRtcVoiceEngine::SetOutputVolume(int level) {
solenberg566ef242015-11-06 15:34:49 -08001045 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
henrikg91d6ede2015-09-17 00:24:34 -07001046 RTC_DCHECK(level >= 0 && level <= 255);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001047 if (voe_wrapper_->volume()->SetSpeakerVolume(level) == -1) {
1048 LOG_RTCERR1(SetSpeakerVolume, level);
1049 return false;
1050 }
1051 return true;
1052}
1053
1054int WebRtcVoiceEngine::GetInputLevel() {
solenberg566ef242015-11-06 15:34:49 -08001055 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001056 unsigned int ulevel;
1057 return (voe_wrapper_->volume()->GetSpeechInputLevel(ulevel) != -1) ?
1058 static_cast<int>(ulevel) : -1;
1059}
1060
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001061const std::vector<AudioCodec>& WebRtcVoiceEngine::codecs() {
solenberg566ef242015-11-06 15:34:49 -08001062 RTC_DCHECK(signal_thread_checker_.CalledOnValidThread());
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001063 return codecs_;
1064}
1065
Stefan Holmer9d69c3f2015-12-07 10:45:43 +01001066RtpCapabilities WebRtcVoiceEngine::GetCapabilities() const {
solenberg566ef242015-11-06 15:34:49 -08001067 RTC_DCHECK(signal_thread_checker_.CalledOnValidThread());
Stefan Holmer9d69c3f2015-12-07 10:45:43 +01001068 RtpCapabilities capabilities;
1069 capabilities.header_extensions.push_back(RtpHeaderExtension(
1070 kRtpAudioLevelHeaderExtension, kRtpAudioLevelHeaderExtensionDefaultId));
1071 capabilities.header_extensions.push_back(
1072 RtpHeaderExtension(kRtpAbsoluteSenderTimeHeaderExtension,
1073 kRtpAbsoluteSenderTimeHeaderExtensionDefaultId));
1074 if (webrtc::field_trial::FindFullName("WebRTC-SendSideBwe") == "Enabled") {
1075 capabilities.header_extensions.push_back(RtpHeaderExtension(
1076 kRtpTransportSequenceNumberHeaderExtension,
1077 kRtpTransportSequenceNumberHeaderExtensionDefaultId));
1078 }
1079 return capabilities;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001080}
1081
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001082int WebRtcVoiceEngine::GetLastEngineError() {
solenberg566ef242015-11-06 15:34:49 -08001083 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001084 return voe_wrapper_->error();
1085}
1086
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001087void WebRtcVoiceEngine::Print(webrtc::TraceLevel level, const char* trace,
1088 int length) {
solenberg566ef242015-11-06 15:34:49 -08001089 // Note: This callback can happen on any thread!
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00001090 rtc::LoggingSeverity sev = rtc::LS_VERBOSE;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001091 if (level == webrtc::kTraceError || level == webrtc::kTraceCritical)
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00001092 sev = rtc::LS_ERROR;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001093 else if (level == webrtc::kTraceWarning)
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00001094 sev = rtc::LS_WARNING;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001095 else if (level == webrtc::kTraceStateInfo || level == webrtc::kTraceInfo)
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00001096 sev = rtc::LS_INFO;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001097 else if (level == webrtc::kTraceTerseInfo)
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00001098 sev = rtc::LS_INFO;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001099
1100 // Skip past boilerplate prefix text
1101 if (length < 72) {
1102 std::string msg(trace, length);
1103 LOG(LS_ERROR) << "Malformed webrtc log message: ";
1104 LOG_V(sev) << msg;
1105 } else {
1106 std::string msg(trace + 71, length - 72);
Peter Boströmd5c75b12015-09-23 13:24:32 +02001107 LOG_V(sev) << "webrtc: " << msg;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001108 }
1109}
1110
solenberg63b34542015-09-29 06:06:31 -07001111void WebRtcVoiceEngine::RegisterChannel(WebRtcVoiceMediaChannel* channel) {
solenberg566ef242015-11-06 15:34:49 -08001112 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
1113 RTC_DCHECK(channel);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001114 channels_.push_back(channel);
1115}
1116
solenberg63b34542015-09-29 06:06:31 -07001117void WebRtcVoiceEngine::UnregisterChannel(WebRtcVoiceMediaChannel* channel) {
solenberg566ef242015-11-06 15:34:49 -08001118 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
solenberg63b34542015-09-29 06:06:31 -07001119 auto it = std::find(channels_.begin(), channels_.end(), channel);
solenberg566ef242015-11-06 15:34:49 -08001120 RTC_DCHECK(it != channels_.end());
1121 channels_.erase(it);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001122}
1123
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001124// Adjusts the default AGC target level by the specified delta.
1125// NB: If we start messing with other config fields, we'll want
1126// to save the current webrtc::AgcConfig as well.
1127bool WebRtcVoiceEngine::AdjustAgcLevel(int delta) {
solenberg566ef242015-11-06 15:34:49 -08001128 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001129 webrtc::AgcConfig config = default_agc_config_;
1130 config.targetLeveldBOv -= delta;
1131
1132 LOG(LS_INFO) << "Adjusting AGC level from default -"
1133 << default_agc_config_.targetLeveldBOv << "dB to -"
1134 << config.targetLeveldBOv << "dB";
1135
1136 if (voe_wrapper_->processing()->SetAgcConfig(config) == -1) {
1137 LOG_RTCERR1(SetAgcConfig, config.targetLeveldBOv);
1138 return false;
1139 }
1140 return true;
1141}
1142
Fredrik Solenbergccb49e72015-05-19 11:37:56 +02001143bool WebRtcVoiceEngine::SetAudioDeviceModule(webrtc::AudioDeviceModule* adm) {
solenberg566ef242015-11-06 15:34:49 -08001144 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001145 if (initialized_) {
1146 LOG(LS_WARNING) << "SetAudioDeviceModule can not be called after Init.";
1147 return false;
1148 }
1149 if (adm_) {
1150 adm_->Release();
1151 adm_ = NULL;
1152 }
1153 if (adm) {
1154 adm_ = adm;
1155 adm_->AddRef();
1156 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001157 return true;
1158}
1159
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00001160bool WebRtcVoiceEngine::StartAecDump(rtc::PlatformFile file) {
solenberg566ef242015-11-06 15:34:49 -08001161 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00001162 FILE* aec_dump_file_stream = rtc::FdopenPlatformFileForWriting(file);
wu@webrtc.orga8910d22014-01-23 22:12:45 +00001163 if (!aec_dump_file_stream) {
1164 LOG(LS_ERROR) << "Could not open AEC dump file stream.";
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00001165 if (!rtc::ClosePlatformFile(file))
wu@webrtc.orga8910d22014-01-23 22:12:45 +00001166 LOG(LS_WARNING) << "Could not close file.";
1167 return false;
1168 }
wu@webrtc.orga9890802013-12-13 00:21:03 +00001169 StopAecDump();
wu@webrtc.orga8910d22014-01-23 22:12:45 +00001170 if (voe_wrapper_->processing()->StartDebugRecording(aec_dump_file_stream) !=
wu@webrtc.orga9890802013-12-13 00:21:03 +00001171 webrtc::AudioProcessing::kNoError) {
wu@webrtc.orga8910d22014-01-23 22:12:45 +00001172 LOG_RTCERR0(StartDebugRecording);
1173 fclose(aec_dump_file_stream);
wu@webrtc.orga9890802013-12-13 00:21:03 +00001174 return false;
1175 }
1176 is_dumping_aec_ = true;
1177 return true;
wu@webrtc.orga9890802013-12-13 00:21:03 +00001178}
1179
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001180void WebRtcVoiceEngine::StartAecDump(const std::string& filename) {
solenberg566ef242015-11-06 15:34:49 -08001181 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001182 if (!is_dumping_aec_) {
1183 // Start dumping AEC when we are not dumping.
1184 if (voe_wrapper_->processing()->StartDebugRecording(
1185 filename.c_str()) != webrtc::AudioProcessing::kNoError) {
wu@webrtc.orga9890802013-12-13 00:21:03 +00001186 LOG_RTCERR1(StartDebugRecording, filename.c_str());
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001187 } else {
1188 is_dumping_aec_ = true;
1189 }
1190 }
1191}
1192
1193void WebRtcVoiceEngine::StopAecDump() {
solenberg566ef242015-11-06 15:34:49 -08001194 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001195 if (is_dumping_aec_) {
1196 // Stop dumping AEC when we are dumping.
1197 if (voe_wrapper_->processing()->StopDebugRecording() !=
1198 webrtc::AudioProcessing::kNoError) {
1199 LOG_RTCERR0(StopDebugRecording);
1200 }
1201 is_dumping_aec_ = false;
1202 }
1203}
1204
ivoc112a3d82015-10-16 02:22:18 -07001205bool WebRtcVoiceEngine::StartRtcEventLog(rtc::PlatformFile file) {
solenberg566ef242015-11-06 15:34:49 -08001206 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
ivoc112a3d82015-10-16 02:22:18 -07001207 return voe_wrapper_->codec()->GetEventLog()->StartLogging(file);
1208}
1209
1210void WebRtcVoiceEngine::StopRtcEventLog() {
solenberg566ef242015-11-06 15:34:49 -08001211 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
ivoc112a3d82015-10-16 02:22:18 -07001212 voe_wrapper_->codec()->GetEventLog()->StopLogging();
1213}
1214
solenberg0a617e22015-10-20 15:49:38 -07001215int WebRtcVoiceEngine::CreateVoEChannel() {
solenberg566ef242015-11-06 15:34:49 -08001216 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
solenberg0a617e22015-10-20 15:49:38 -07001217 return voe_wrapper_->base()->CreateChannel(voe_config_);
sergeyu@chromium.org5bc25c42013-12-05 00:24:06 +00001218}
1219
solenbergc96df772015-10-21 13:01:53 -07001220class WebRtcVoiceMediaChannel::WebRtcAudioSendStream
mallinath@webrtc.org67ee6b92014-02-03 16:57:16 +00001221 : public AudioRenderer::Sink {
1222 public:
solenbergc96df772015-10-21 13:01:53 -07001223 WebRtcAudioSendStream(int ch, webrtc::AudioTransport* voe_audio_transport,
solenberg3a941542015-11-16 07:34:50 -08001224 uint32_t ssrc, const std::string& c_name,
1225 const std::vector<webrtc::RtpExtension>& extensions,
1226 webrtc::Call* call)
solenberg7add0582015-11-20 09:59:34 -08001227 : voe_audio_transport_(voe_audio_transport),
solenberg3a941542015-11-16 07:34:50 -08001228 call_(call),
1229 config_(nullptr) {
solenberg85a04962015-10-27 03:35:21 -07001230 RTC_DCHECK_GE(ch, 0);
1231 // TODO(solenberg): Once we're not using FakeWebRtcVoiceEngine anymore:
1232 // RTC_DCHECK(voe_audio_transport);
solenbergc96df772015-10-21 13:01:53 -07001233 RTC_DCHECK(call);
solenberg85a04962015-10-27 03:35:21 -07001234 audio_capture_thread_checker_.DetachFromThread();
solenberg3a941542015-11-16 07:34:50 -08001235 config_.rtp.ssrc = ssrc;
1236 config_.rtp.c_name = c_name;
1237 config_.voe_channel_id = ch;
1238 RecreateAudioSendStream(extensions);
solenbergc96df772015-10-21 13:01:53 -07001239 }
solenberg3a941542015-11-16 07:34:50 -08001240
solenbergc96df772015-10-21 13:01:53 -07001241 ~WebRtcAudioSendStream() override {
solenberg566ef242015-11-06 15:34:49 -08001242 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
solenbergc96df772015-10-21 13:01:53 -07001243 Stop();
1244 call_->DestroyAudioSendStream(stream_);
1245 }
henrike@webrtc.org1e09a712013-07-26 19:17:59 +00001246
solenberg3a941542015-11-16 07:34:50 -08001247 void RecreateAudioSendStream(
1248 const std::vector<webrtc::RtpExtension>& extensions) {
1249 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
1250 if (stream_) {
1251 call_->DestroyAudioSendStream(stream_);
1252 stream_ = nullptr;
1253 }
1254 config_.rtp.extensions = extensions;
1255 RTC_DCHECK(!stream_);
1256 stream_ = call_->CreateAudioSendStream(config_);
1257 RTC_CHECK(stream_);
1258 }
1259
Fredrik Solenbergb5727682015-12-04 15:22:19 +01001260 bool SendTelephoneEvent(int payload_type, uint8_t event,
1261 uint32_t duration_ms) {
1262 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
1263 RTC_DCHECK(stream_);
1264 return stream_->SendTelephoneEvent(payload_type, event, duration_ms);
1265 }
1266
solenberg3a941542015-11-16 07:34:50 -08001267 webrtc::AudioSendStream::Stats GetStats() const {
1268 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
1269 RTC_DCHECK(stream_);
1270 return stream_->GetStats();
1271 }
1272
mallinath@webrtc.org67ee6b92014-02-03 16:57:16 +00001273 // Starts the rendering by setting a sink to the renderer to get data
1274 // callback.
henrike@webrtc.orga7b98182014-02-21 15:51:43 +00001275 // This method is called on the libjingle worker thread.
mallinath@webrtc.org67ee6b92014-02-03 16:57:16 +00001276 // TODO(xians): Make sure Start() is called only once.
1277 void Start(AudioRenderer* renderer) {
solenberg566ef242015-11-06 15:34:49 -08001278 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
solenbergc96df772015-10-21 13:01:53 -07001279 RTC_DCHECK(renderer);
1280 if (renderer_) {
henrikg91d6ede2015-09-17 00:24:34 -07001281 RTC_DCHECK(renderer_ == renderer);
mallinath@webrtc.org67ee6b92014-02-03 16:57:16 +00001282 return;
1283 }
mallinath@webrtc.org67ee6b92014-02-03 16:57:16 +00001284 renderer->SetSink(this);
1285 renderer_ = renderer;
1286 }
1287
solenbergc96df772015-10-21 13:01:53 -07001288 // Stops rendering by setting the sink of the renderer to nullptr. No data
mallinath@webrtc.org67ee6b92014-02-03 16:57:16 +00001289 // callback will be received after this method.
henrike@webrtc.orga7b98182014-02-21 15:51:43 +00001290 // This method is called on the libjingle worker thread.
mallinath@webrtc.org67ee6b92014-02-03 16:57:16 +00001291 void Stop() {
solenberg566ef242015-11-06 15:34:49 -08001292 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
solenbergc96df772015-10-21 13:01:53 -07001293 if (renderer_) {
1294 renderer_->SetSink(nullptr);
1295 renderer_ = nullptr;
solenberg98c68862015-10-09 03:27:14 -07001296 }
mallinath@webrtc.org67ee6b92014-02-03 16:57:16 +00001297 }
1298
1299 // AudioRenderer::Sink implementation.
henrike@webrtc.orga7b98182014-02-21 15:51:43 +00001300 // This method is called on the audio thread.
kjellander@webrtc.org14665ff2015-03-04 12:58:35 +00001301 void OnData(const void* audio_data,
1302 int bits_per_sample,
1303 int sample_rate,
1304 int number_of_channels,
Peter Kastingdce40cf2015-08-24 14:52:23 -07001305 size_t number_of_frames) override {
solenberg566ef242015-11-06 15:34:49 -08001306 RTC_DCHECK(!worker_thread_checker_.CalledOnValidThread());
solenberg85a04962015-10-27 03:35:21 -07001307 RTC_DCHECK(audio_capture_thread_checker_.CalledOnValidThread());
solenbergc96df772015-10-21 13:01:53 -07001308 RTC_DCHECK(voe_audio_transport_);
solenberg7add0582015-11-20 09:59:34 -08001309 voe_audio_transport_->OnData(config_.voe_channel_id,
henrike@webrtc.orga7b98182014-02-21 15:51:43 +00001310 audio_data,
1311 bits_per_sample,
1312 sample_rate,
1313 number_of_channels,
1314 number_of_frames);
henrike@webrtc.orga7b98182014-02-21 15:51:43 +00001315 }
1316
1317 // Callback from the |renderer_| when it is going away. In case Start() has
1318 // never been called, this callback won't be triggered.
kjellander@webrtc.org14665ff2015-03-04 12:58:35 +00001319 void OnClose() override {
solenberg566ef242015-11-06 15:34:49 -08001320 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
solenbergc96df772015-10-21 13:01:53 -07001321 // Set |renderer_| to nullptr to make sure no more callback will get into
henrike@webrtc.orga7b98182014-02-21 15:51:43 +00001322 // the renderer.
solenbergc96df772015-10-21 13:01:53 -07001323 renderer_ = nullptr;
mallinath@webrtc.org67ee6b92014-02-03 16:57:16 +00001324 }
1325
1326 // Accessor to the VoE channel ID.
solenberg85a04962015-10-27 03:35:21 -07001327 int channel() const {
solenberg566ef242015-11-06 15:34:49 -08001328 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
solenberg7add0582015-11-20 09:59:34 -08001329 return config_.voe_channel_id;
solenberg85a04962015-10-27 03:35:21 -07001330 }
mallinath@webrtc.org67ee6b92014-02-03 16:57:16 +00001331
1332 private:
solenberg566ef242015-11-06 15:34:49 -08001333 rtc::ThreadChecker worker_thread_checker_;
solenberg85a04962015-10-27 03:35:21 -07001334 rtc::ThreadChecker audio_capture_thread_checker_;
solenbergc96df772015-10-21 13:01:53 -07001335 webrtc::AudioTransport* const voe_audio_transport_ = nullptr;
1336 webrtc::Call* call_ = nullptr;
solenberg3a941542015-11-16 07:34:50 -08001337 webrtc::AudioSendStream::Config config_;
1338 // The stream is owned by WebRtcAudioSendStream and may be reallocated if
1339 // configuration changes.
solenbergc96df772015-10-21 13:01:53 -07001340 webrtc::AudioSendStream* stream_ = nullptr;
mallinath@webrtc.org67ee6b92014-02-03 16:57:16 +00001341
1342 // Raw pointer to AudioRenderer owned by LocalAudioTrackHandler.
1343 // PeerConnection will make sure invalidating the pointer before the object
1344 // goes away.
solenbergc96df772015-10-21 13:01:53 -07001345 AudioRenderer* renderer_ = nullptr;
henrike@webrtc.orga7b98182014-02-21 15:51:43 +00001346
solenbergc96df772015-10-21 13:01:53 -07001347 RTC_DISALLOW_IMPLICIT_CONSTRUCTORS(WebRtcAudioSendStream);
1348};
1349
1350class WebRtcVoiceMediaChannel::WebRtcAudioReceiveStream {
1351 public:
solenberg7add0582015-11-20 09:59:34 -08001352 WebRtcAudioReceiveStream(int ch, uint32_t remote_ssrc, uint32_t local_ssrc,
1353 bool use_combined_bwe, const std::string& sync_group,
1354 const std::vector<webrtc::RtpExtension>& extensions,
1355 webrtc::Call* call)
1356 : call_(call),
1357 config_() {
1358 RTC_DCHECK_GE(ch, 0);
1359 RTC_DCHECK(call);
1360 config_.rtp.remote_ssrc = remote_ssrc;
1361 config_.rtp.local_ssrc = local_ssrc;
1362 config_.voe_channel_id = ch;
1363 config_.sync_group = sync_group;
1364 RecreateAudioReceiveStream(use_combined_bwe, extensions);
1365 }
solenbergc96df772015-10-21 13:01:53 -07001366
solenberg7add0582015-11-20 09:59:34 -08001367 ~WebRtcAudioReceiveStream() {
1368 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
1369 call_->DestroyAudioReceiveStream(stream_);
1370 }
1371
1372 void RecreateAudioReceiveStream(
1373 const std::vector<webrtc::RtpExtension>& extensions) {
1374 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
1375 RecreateAudioReceiveStream(config_.combined_audio_video_bwe, extensions);
1376 }
1377 void RecreateAudioReceiveStream(bool use_combined_bwe) {
1378 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
1379 RecreateAudioReceiveStream(use_combined_bwe, config_.rtp.extensions);
1380 }
1381
1382 webrtc::AudioReceiveStream::Stats GetStats() const {
1383 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
1384 RTC_DCHECK(stream_);
1385 return stream_->GetStats();
1386 }
1387
1388 int channel() const {
1389 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
1390 return config_.voe_channel_id;
1391 }
solenbergc96df772015-10-21 13:01:53 -07001392
1393 private:
solenberg7add0582015-11-20 09:59:34 -08001394 void RecreateAudioReceiveStream(bool use_combined_bwe,
1395 const std::vector<webrtc::RtpExtension>& extensions) {
1396 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
1397 if (stream_) {
1398 call_->DestroyAudioReceiveStream(stream_);
1399 stream_ = nullptr;
1400 }
1401 config_.rtp.extensions = extensions;
1402 config_.combined_audio_video_bwe = use_combined_bwe;
1403 RTC_DCHECK(!stream_);
1404 stream_ = call_->CreateAudioReceiveStream(config_);
1405 RTC_CHECK(stream_);
1406 }
1407
1408 rtc::ThreadChecker worker_thread_checker_;
1409 webrtc::Call* call_ = nullptr;
1410 webrtc::AudioReceiveStream::Config config_;
1411 // The stream is owned by WebRtcAudioReceiveStream and may be reallocated if
1412 // configuration changes.
1413 webrtc::AudioReceiveStream* stream_ = nullptr;
solenbergc96df772015-10-21 13:01:53 -07001414
1415 RTC_DISALLOW_IMPLICIT_CONSTRUCTORS(WebRtcAudioReceiveStream);
henrike@webrtc.org1e09a712013-07-26 19:17:59 +00001416};
1417
Fredrik Solenberg709ed672015-09-15 12:26:33 +02001418WebRtcVoiceMediaChannel::WebRtcVoiceMediaChannel(WebRtcVoiceEngine* engine,
Fredrik Solenbergb071a192015-09-17 16:42:56 +02001419 const AudioOptions& options,
Fredrik Solenberg709ed672015-09-15 12:26:33 +02001420 webrtc::Call* call)
solenberg566ef242015-11-06 15:34:49 -08001421 : engine_(engine), call_(call) {
solenberg0a617e22015-10-20 15:49:38 -07001422 LOG(LS_VERBOSE) << "WebRtcVoiceMediaChannel::WebRtcVoiceMediaChannel";
solenberg566ef242015-11-06 15:34:49 -08001423 RTC_DCHECK(call);
solenberg0a617e22015-10-20 15:49:38 -07001424 engine->RegisterChannel(this);
Fredrik Solenbergb071a192015-09-17 16:42:56 +02001425 SetOptions(options);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001426}
1427
1428WebRtcVoiceMediaChannel::~WebRtcVoiceMediaChannel() {
solenberg566ef242015-11-06 15:34:49 -08001429 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
solenberg0a617e22015-10-20 15:49:38 -07001430 LOG(LS_VERBOSE) << "WebRtcVoiceMediaChannel::~WebRtcVoiceMediaChannel";
solenberg7add0582015-11-20 09:59:34 -08001431 // TODO(solenberg): Should be able to delete the streams directly, without
1432 // going through RemoveNnStream(), once stream objects handle
1433 // all (de)configuration.
solenbergc96df772015-10-21 13:01:53 -07001434 while (!send_streams_.empty()) {
1435 RemoveSendStream(send_streams_.begin()->first);
solenbergd97ec302015-10-07 01:40:33 -07001436 }
solenberg7add0582015-11-20 09:59:34 -08001437 while (!recv_streams_.empty()) {
1438 RemoveRecvStream(recv_streams_.begin()->first);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001439 }
solenberg0a617e22015-10-20 15:49:38 -07001440 engine()->UnregisterChannel(this);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001441}
1442
Peter Thatcherc2ee2c82015-08-07 16:05:34 -07001443bool WebRtcVoiceMediaChannel::SetSendParameters(
1444 const AudioSendParameters& params) {
solenberg566ef242015-11-06 15:34:49 -08001445 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
solenberg7e4e01a2015-12-02 08:05:01 -08001446 LOG(LS_INFO) << "WebRtcVoiceMediaChannel::SetSendParameters: "
1447 << params.ToString();
Peter Thatcherc2ee2c82015-08-07 16:05:34 -07001448 // TODO(pthatcher): Refactor this to be more clean now that we have
1449 // all the information at once.
solenberg3a941542015-11-16 07:34:50 -08001450
1451 if (!SetSendCodecs(params.codecs)) {
1452 return false;
1453 }
1454
solenberg7e4e01a2015-12-02 08:05:01 -08001455 if (!ValidateRtpExtensions(params.extensions)) {
1456 return false;
1457 }
1458 std::vector<webrtc::RtpExtension> filtered_extensions =
1459 FilterRtpExtensions(params.extensions,
1460 webrtc::RtpExtension::IsSupportedForAudio, true);
1461 if (send_rtp_extensions_ != filtered_extensions) {
1462 send_rtp_extensions_.swap(filtered_extensions);
solenberg3a941542015-11-16 07:34:50 -08001463 for (auto& it : send_streams_) {
1464 it.second->RecreateAudioSendStream(send_rtp_extensions_);
1465 }
1466 }
1467
1468 if (!SetMaxSendBandwidth(params.max_bandwidth_bps)) {
1469 return false;
1470 }
1471 return SetOptions(params.options);
Peter Thatcherc2ee2c82015-08-07 16:05:34 -07001472}
1473
1474bool WebRtcVoiceMediaChannel::SetRecvParameters(
1475 const AudioRecvParameters& params) {
solenberg566ef242015-11-06 15:34:49 -08001476 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
solenberg7e4e01a2015-12-02 08:05:01 -08001477 LOG(LS_INFO) << "WebRtcVoiceMediaChannel::SetRecvParameters: "
1478 << params.ToString();
Peter Thatcherc2ee2c82015-08-07 16:05:34 -07001479 // TODO(pthatcher): Refactor this to be more clean now that we have
1480 // all the information at once.
solenberg7add0582015-11-20 09:59:34 -08001481
1482 if (!SetRecvCodecs(params.codecs)) {
1483 return false;
1484 }
1485
solenberg7e4e01a2015-12-02 08:05:01 -08001486 if (!ValidateRtpExtensions(params.extensions)) {
1487 return false;
1488 }
1489 std::vector<webrtc::RtpExtension> filtered_extensions =
1490 FilterRtpExtensions(params.extensions,
1491 webrtc::RtpExtension::IsSupportedForAudio, false);
1492 if (recv_rtp_extensions_ != filtered_extensions) {
1493 recv_rtp_extensions_.swap(filtered_extensions);
solenberg7add0582015-11-20 09:59:34 -08001494 for (auto& it : recv_streams_) {
1495 it.second->RecreateAudioReceiveStream(recv_rtp_extensions_);
1496 }
1497 }
1498
1499 return true;
Peter Thatcherc2ee2c82015-08-07 16:05:34 -07001500}
1501
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001502bool WebRtcVoiceMediaChannel::SetOptions(const AudioOptions& options) {
solenberg566ef242015-11-06 15:34:49 -08001503 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001504 LOG(LS_INFO) << "Setting voice channel options: "
1505 << options.ToString();
1506
wu@webrtc.orgde305012013-10-31 15:40:38 +00001507 // Check if DSCP value is changed from previous.
1508 bool dscp_option_changed = (options_.dscp != options.dscp);
1509
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001510 // We retain all of the existing options, and apply the given ones
1511 // on top. This means there is no way to "clear" options such that
1512 // they go back to the engine default.
1513 options_.SetAll(options);
1514
1515 if (send_ != SEND_NOTHING) {
solenberg63b34542015-09-29 06:06:31 -07001516 if (!engine()->ApplyOptions(options_)) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001517 LOG(LS_WARNING) <<
solenberg63b34542015-09-29 06:06:31 -07001518 "Failed to apply engine options during channel SetOptions.";
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001519 return false;
1520 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001521 }
1522
wu@webrtc.orgde305012013-10-31 15:40:38 +00001523 if (dscp_option_changed) {
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00001524 rtc::DiffServCodePoint dscp = rtc::DSCP_DEFAULT;
kwiberg102c6a62015-10-30 02:47:38 -07001525 if (options_.dscp.value_or(false))
wu@webrtc.orgde305012013-10-31 15:40:38 +00001526 dscp = kAudioDscpValue;
1527 if (MediaChannel::SetDscp(dscp) != 0) {
1528 LOG(LS_WARNING) << "Failed to set DSCP settings for audio channel";
1529 }
1530 }
solenberg8fb30c32015-10-13 03:06:58 -07001531
solenbergc96df772015-10-21 13:01:53 -07001532 // TODO(solenberg): Don't recreate unless options changed.
solenberg7add0582015-11-20 09:59:34 -08001533 for (auto& it : recv_streams_) {
1534 it.second->RecreateAudioReceiveStream(
1535 options_.combined_audio_video_bwe.value_or(false));
1536 }
solenberg8fb30c32015-10-13 03:06:58 -07001537
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001538 LOG(LS_INFO) << "Set voice channel options. Current options: "
1539 << options_.ToString();
1540 return true;
1541}
1542
1543bool WebRtcVoiceMediaChannel::SetRecvCodecs(
1544 const std::vector<AudioCodec>& codecs) {
solenberg566ef242015-11-06 15:34:49 -08001545 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
solenberg8fb30c32015-10-13 03:06:58 -07001546
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001547 // Set the payload types to be used for incoming media.
solenberg0b675462015-10-09 01:37:09 -07001548 LOG(LS_INFO) << "Setting receive voice codecs.";
solenberg0b675462015-10-09 01:37:09 -07001549
1550 if (!VerifyUniquePayloadTypes(codecs)) {
1551 LOG(LS_ERROR) << "Codec payload types overlap.";
1552 return false;
1553 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001554
1555 std::vector<AudioCodec> new_codecs;
1556 // Find all new codecs. We allow adding new codecs but don't allow changing
1557 // the payload type of codecs that is already configured since we might
1558 // already be receiving packets with that payload type.
Fredrik Solenbergaf9fb212015-08-26 10:45:53 +02001559 for (const AudioCodec& codec : codecs) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001560 AudioCodec old_codec;
Fredrik Solenbergaf9fb212015-08-26 10:45:53 +02001561 if (FindCodec(recv_codecs_, codec, &old_codec)) {
1562 if (old_codec.id != codec.id) {
1563 LOG(LS_ERROR) << codec.name << " payload type changed.";
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001564 return false;
1565 }
1566 } else {
Fredrik Solenbergaf9fb212015-08-26 10:45:53 +02001567 new_codecs.push_back(codec);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001568 }
1569 }
1570 if (new_codecs.empty()) {
1571 // There are no new codecs to configure. Already configured codecs are
1572 // never removed.
1573 return true;
1574 }
1575
1576 if (playout_) {
1577 // Receive codecs can not be changed while playing. So we temporarily
1578 // pause playout.
1579 PausePlayout();
1580 }
1581
solenberg26c8c912015-11-27 04:00:25 -08001582 bool result = true;
1583 for (const AudioCodec& codec : new_codecs) {
1584 webrtc::CodecInst voe_codec;
1585 if (WebRtcVoiceEngine::ToCodecInst(codec, &voe_codec)) {
1586 LOG(LS_INFO) << ToString(codec);
1587 voe_codec.pltype = codec.id;
1588 for (const auto& ch : recv_streams_) {
1589 if (engine()->voe()->codec()->SetRecPayloadType(
1590 ch.second->channel(), voe_codec) == -1) {
1591 LOG_RTCERR2(SetRecPayloadType, ch.second->channel(),
1592 ToString(voe_codec));
1593 result = false;
1594 }
1595 }
1596 } else {
1597 LOG(LS_WARNING) << "Unknown codec " << ToString(codec);
1598 result = false;
1599 break;
1600 }
1601 }
Fredrik Solenbergaf9fb212015-08-26 10:45:53 +02001602 if (result) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001603 recv_codecs_ = codecs;
1604 }
1605
1606 if (desired_playout_ && !playout_) {
1607 ResumePlayout();
1608 }
Fredrik Solenbergaf9fb212015-08-26 10:45:53 +02001609 return result;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001610}
1611
1612bool WebRtcVoiceMediaChannel::SetSendCodecs(
wu@webrtc.orgcadf9042013-08-30 21:24:16 +00001613 int channel, const std::vector<AudioCodec>& codecs) {
buildbot@webrtc.orgae740dd2014-06-17 10:56:41 +00001614 // Disable VAD, FEC, and RED unless we know the other side wants them.
wu@webrtc.orgcadf9042013-08-30 21:24:16 +00001615 engine()->voe()->codec()->SetVADStatus(channel, false);
1616 engine()->voe()->rtp()->SetNACKStatus(channel, false, 0);
buildbot@webrtc.orgae740dd2014-06-17 10:56:41 +00001617 engine()->voe()->rtp()->SetREDStatus(channel, false);
1618 engine()->voe()->codec()->SetFECStatus(channel, false);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001619
1620 // Scan through the list to figure out the codec to use for sending, along
Fredrik Solenbergb5727682015-12-04 15:22:19 +01001621 // with the proper configuration for VAD.
henrike@webrtc.org704bf9e2014-02-27 17:52:04 +00001622 bool found_send_codec = false;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001623 webrtc::CodecInst send_codec;
1624 memset(&send_codec, 0, sizeof(send_codec));
1625
wu@webrtc.org05e7b442014-04-01 17:44:24 +00001626 bool nack_enabled = nack_enabled_;
buildbot@webrtc.org3ffa1f92014-07-02 19:51:26 +00001627 bool enable_codec_fec = false;
Minyue Li7100dcd2015-03-27 05:05:59 +01001628 bool enable_opus_dtx = false;
minyue@webrtc.org26236952014-10-29 02:27:08 +00001629 int opus_max_playback_rate = 0;
buildbot@webrtc.org5d639b32014-09-10 07:57:12 +00001630
henrike@webrtc.org704bf9e2014-02-27 17:52:04 +00001631 // Set send codec (the first non-telephone-event/CN codec)
Fredrik Solenbergaf9fb212015-08-26 10:45:53 +02001632 for (const AudioCodec& codec : codecs) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001633 // Ignore codecs we don't know about. The negotiation step should prevent
1634 // this, but double-check to be sure.
1635 webrtc::CodecInst voe_codec;
solenberg26c8c912015-11-27 04:00:25 -08001636 if (!WebRtcVoiceEngine::ToCodecInst(codec, &voe_codec)) {
Fredrik Solenbergaf9fb212015-08-26 10:45:53 +02001637 LOG(LS_WARNING) << "Unknown codec " << ToString(codec);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001638 continue;
1639 }
1640
Fredrik Solenbergaf9fb212015-08-26 10:45:53 +02001641 if (IsCodec(codec, kDtmfCodecName) || IsCodec(codec, kCnCodecName)) {
henrike@webrtc.org704bf9e2014-02-27 17:52:04 +00001642 // Skip telephone-event/CN codec, which will be handled later.
1643 continue;
1644 }
1645
henrike@webrtc.org704bf9e2014-02-27 17:52:04 +00001646 // We'll use the first codec in the list to actually send audio data.
1647 // Be sure to use the payload type requested by the remote side.
buildbot@webrtc.orgae740dd2014-06-17 10:56:41 +00001648 // "red", for RED audio, is a special case where the actual codec to be
henrike@webrtc.org704bf9e2014-02-27 17:52:04 +00001649 // used is specified in params.
Fredrik Solenbergaf9fb212015-08-26 10:45:53 +02001650 if (IsCodec(codec, kRedCodecName)) {
henrike@webrtc.org704bf9e2014-02-27 17:52:04 +00001651 // Parse out the RED parameters. If we fail, just ignore RED;
1652 // we don't support all possible params/usage scenarios.
Fredrik Solenbergaf9fb212015-08-26 10:45:53 +02001653 if (!GetRedSendCodec(codec, codecs, &send_codec)) {
henrike@webrtc.org704bf9e2014-02-27 17:52:04 +00001654 continue;
1655 }
1656
1657 // Enable redundant encoding of the specified codec. Treat any
1658 // failure as a fatal internal error.
buildbot@webrtc.orgae740dd2014-06-17 10:56:41 +00001659 LOG(LS_INFO) << "Enabling RED on channel " << channel;
Fredrik Solenbergaf9fb212015-08-26 10:45:53 +02001660 if (engine()->voe()->rtp()->SetREDStatus(channel, true, codec.id) == -1) {
1661 LOG_RTCERR3(SetREDStatus, channel, true, codec.id);
henrike@webrtc.org704bf9e2014-02-27 17:52:04 +00001662 return false;
1663 }
1664 } else {
1665 send_codec = voe_codec;
Fredrik Solenbergaf9fb212015-08-26 10:45:53 +02001666 nack_enabled = IsNackEnabled(codec);
Minyue Li7100dcd2015-03-27 05:05:59 +01001667 // For Opus as the send codec, we are to determine inband FEC, maximum
1668 // playback rate, and opus internal dtx.
Fredrik Solenbergaf9fb212015-08-26 10:45:53 +02001669 if (IsCodec(codec, kOpusCodecName)) {
1670 GetOpusConfig(codec, &send_codec, &enable_codec_fec,
Minyue Li7100dcd2015-03-27 05:05:59 +01001671 &opus_max_playback_rate, &enable_opus_dtx);
buildbot@webrtc.org5d639b32014-09-10 07:57:12 +00001672 }
Brave Yao5225dd82015-03-26 07:39:19 +08001673
1674 // Set packet size if the AudioCodec param kCodecParamPTime is set.
1675 int ptime_ms = 0;
Fredrik Solenbergaf9fb212015-08-26 10:45:53 +02001676 if (codec.GetParam(kCodecParamPTime, &ptime_ms)) {
solenberg26c8c912015-11-27 04:00:25 -08001677 if (!WebRtcVoiceCodecs::SetPTimeAsPacketSize(&send_codec, ptime_ms)) {
Brave Yao5225dd82015-03-26 07:39:19 +08001678 LOG(LS_WARNING) << "Failed to set packet size for codec "
1679 << send_codec.plname;
1680 return false;
1681 }
1682 }
henrike@webrtc.org704bf9e2014-02-27 17:52:04 +00001683 }
1684 found_send_codec = true;
1685 break;
1686 }
1687
wu@webrtc.org05e7b442014-04-01 17:44:24 +00001688 if (nack_enabled_ != nack_enabled) {
1689 SetNack(channel, nack_enabled);
1690 nack_enabled_ = nack_enabled;
1691 }
1692
henrike@webrtc.org704bf9e2014-02-27 17:52:04 +00001693 if (!found_send_codec) {
1694 LOG(LS_WARNING) << "Received empty list of codecs.";
1695 return false;
1696 }
1697
1698 // Set the codec immediately, since SetVADStatus() depends on whether
1699 // the current codec is mono or stereo.
1700 if (!SetSendCodec(channel, send_codec))
1701 return false;
1702
buildbot@webrtc.org3ffa1f92014-07-02 19:51:26 +00001703 // FEC should be enabled after SetSendCodec.
1704 if (enable_codec_fec) {
1705 LOG(LS_INFO) << "Attempt to enable codec internal FEC on channel "
1706 << channel;
buildbot@webrtc.org3ffa1f92014-07-02 19:51:26 +00001707 if (engine()->voe()->codec()->SetFECStatus(channel, true) == -1) {
1708 // Enable codec internal FEC. Treat any failure as fatal internal error.
1709 LOG_RTCERR2(SetFECStatus, channel, true);
1710 return false;
1711 }
buildbot@webrtc.org3ffa1f92014-07-02 19:51:26 +00001712 }
1713
Minyue Li7100dcd2015-03-27 05:05:59 +01001714 if (IsCodec(send_codec, kOpusCodecName)) {
1715 // DTX and maxplaybackrate should be set after SetSendCodec. Because current
1716 // send codec has to be Opus.
1717
1718 // Set Opus internal DTX.
1719 LOG(LS_INFO) << "Attempt to "
solenbergbd138382015-11-20 16:08:07 -08001720 << (enable_opus_dtx ? "enable" : "disable")
Minyue Li7100dcd2015-03-27 05:05:59 +01001721 << " Opus DTX on channel "
buildbot@webrtc.org5d639b32014-09-10 07:57:12 +00001722 << channel;
Minyue Li7100dcd2015-03-27 05:05:59 +01001723 if (engine()->voe()->codec()->SetOpusDtx(channel, enable_opus_dtx)) {
1724 LOG_RTCERR2(SetOpusDtx, channel, enable_opus_dtx);
1725 return false;
1726 }
1727
1728 // If opus_max_playback_rate <= 0, the default maximum playback rate
1729 // (48 kHz) will be used.
1730 if (opus_max_playback_rate > 0) {
1731 LOG(LS_INFO) << "Attempt to set maximum playback rate to "
1732 << opus_max_playback_rate
1733 << " Hz on channel "
1734 << channel;
1735 if (engine()->voe()->codec()->SetOpusMaxPlaybackRate(
1736 channel, opus_max_playback_rate) == -1) {
1737 LOG_RTCERR2(SetOpusMaxPlaybackRate, channel, opus_max_playback_rate);
1738 return false;
1739 }
buildbot@webrtc.org5d639b32014-09-10 07:57:12 +00001740 }
buildbot@webrtc.org5d639b32014-09-10 07:57:12 +00001741 }
1742
henrike@webrtc.org704bf9e2014-02-27 17:52:04 +00001743 // Always update the |send_codec_| to the currently set send codec.
1744 send_codec_.reset(new webrtc::CodecInst(send_codec));
1745
minyue@webrtc.org26236952014-10-29 02:27:08 +00001746 if (send_bitrate_setting_) {
1747 SetSendBitrateInternal(send_bitrate_bps_);
henrike@webrtc.org704bf9e2014-02-27 17:52:04 +00001748 }
1749
Fredrik Solenbergb5727682015-12-04 15:22:19 +01001750 // Loop through the codecs list again to config the CN codec.
Fredrik Solenbergaf9fb212015-08-26 10:45:53 +02001751 for (const AudioCodec& codec : codecs) {
henrike@webrtc.org704bf9e2014-02-27 17:52:04 +00001752 // Ignore codecs we don't know about. The negotiation step should prevent
1753 // this, but double-check to be sure.
1754 webrtc::CodecInst voe_codec;
solenberg26c8c912015-11-27 04:00:25 -08001755 if (!WebRtcVoiceEngine::ToCodecInst(codec, &voe_codec)) {
Fredrik Solenbergaf9fb212015-08-26 10:45:53 +02001756 LOG(LS_WARNING) << "Unknown codec " << ToString(codec);
henrike@webrtc.org704bf9e2014-02-27 17:52:04 +00001757 continue;
1758 }
1759
Fredrik Solenbergb5727682015-12-04 15:22:19 +01001760 if (IsCodec(codec, kCnCodecName)) {
henrike@webrtc.org704bf9e2014-02-27 17:52:04 +00001761 // Turn voice activity detection/comfort noise on if supported.
1762 // Set the wideband CN payload type appropriately.
1763 // (narrowband always uses the static payload type 13).
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001764 webrtc::PayloadFrequencies cn_freq;
Fredrik Solenbergaf9fb212015-08-26 10:45:53 +02001765 switch (codec.clockrate) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001766 case 8000:
1767 cn_freq = webrtc::kFreq8000Hz;
1768 break;
1769 case 16000:
1770 cn_freq = webrtc::kFreq16000Hz;
1771 break;
1772 case 32000:
1773 cn_freq = webrtc::kFreq32000Hz;
1774 break;
1775 default:
Fredrik Solenbergaf9fb212015-08-26 10:45:53 +02001776 LOG(LS_WARNING) << "CN frequency " << codec.clockrate
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001777 << " not supported.";
1778 continue;
1779 }
wu@webrtc.orgcadf9042013-08-30 21:24:16 +00001780 // Set the CN payloadtype and the VAD status.
1781 // The CN payload type for 8000 Hz clockrate is fixed at 13.
1782 if (cn_freq != webrtc::kFreq8000Hz) {
1783 if (engine()->voe()->codec()->SetSendCNPayloadType(
Fredrik Solenbergaf9fb212015-08-26 10:45:53 +02001784 channel, codec.id, cn_freq) == -1) {
1785 LOG_RTCERR3(SetSendCNPayloadType, channel, codec.id, cn_freq);
wu@webrtc.orgcadf9042013-08-30 21:24:16 +00001786 // TODO(ajm): This failure condition will be removed from VoE.
1787 // Restore the return here when we update to a new enough webrtc.
1788 //
1789 // Not returning false because the SetSendCNPayloadType will fail if
1790 // the channel is already sending.
1791 // This can happen if the remote description is applied twice, for
1792 // example in the case of ROAP on top of JSEP, where both side will
1793 // send the offer.
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001794 }
wu@webrtc.orgcadf9042013-08-30 21:24:16 +00001795 }
wu@webrtc.orgcadf9042013-08-30 21:24:16 +00001796 // Only turn on VAD if we have a CN payload type that matches the
1797 // clockrate for the codec we are going to use.
Fredrik Solenbergaf9fb212015-08-26 10:45:53 +02001798 if (codec.clockrate == send_codec.plfreq && send_codec.channels != 2) {
Minyue Li7100dcd2015-03-27 05:05:59 +01001799 // TODO(minyue): If CN frequency == 48000 Hz is allowed, consider the
1800 // interaction between VAD and Opus FEC.
wu@webrtc.orgcadf9042013-08-30 21:24:16 +00001801 LOG(LS_INFO) << "Enabling VAD";
1802 if (engine()->voe()->codec()->SetVADStatus(channel, true) == -1) {
1803 LOG_RTCERR2(SetVADStatus, channel, true);
1804 return false;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001805 }
1806 }
1807 }
wu@webrtc.org1d1ffc92013-10-16 18:12:02 +00001808 }
wu@webrtc.orgcadf9042013-08-30 21:24:16 +00001809 return true;
1810}
1811
1812bool WebRtcVoiceMediaChannel::SetSendCodecs(
1813 const std::vector<AudioCodec>& codecs) {
solenberg566ef242015-11-06 15:34:49 -08001814 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
Fredrik Solenbergb5727682015-12-04 15:22:19 +01001815 // TODO(solenberg): Validate input - that payload types don't overlap, are
1816 // within range, filter out codecs we don't support,
1817 // redundant codecs etc.
solenbergd97ec302015-10-07 01:40:33 -07001818
Fredrik Solenbergb5727682015-12-04 15:22:19 +01001819 // Find the DTMF telephone event "codec" payload type.
1820 dtmf_payload_type_ = rtc::Optional<int>();
Fredrik Solenbergaf9fb212015-08-26 10:45:53 +02001821 for (const AudioCodec& codec : codecs) {
Fredrik Solenbergaf9fb212015-08-26 10:45:53 +02001822 if (IsCodec(codec, kDtmfCodecName)) {
Fredrik Solenbergb5727682015-12-04 15:22:19 +01001823 dtmf_payload_type_ = rtc::Optional<int>(codec.id);
1824 break;
wu@webrtc.orgcadf9042013-08-30 21:24:16 +00001825 }
1826 }
1827
1828 // Cache the codecs in order to configure the channel created later.
1829 send_codecs_ = codecs;
solenbergc96df772015-10-21 13:01:53 -07001830 for (const auto& ch : send_streams_) {
Fredrik Solenbergaf9fb212015-08-26 10:45:53 +02001831 if (!SetSendCodecs(ch.second->channel(), codecs)) {
wu@webrtc.orgcadf9042013-08-30 21:24:16 +00001832 return false;
1833 }
1834 }
1835
wu@webrtc.org05e7b442014-04-01 17:44:24 +00001836 // Set nack status on receive channels and update |nack_enabled_|.
solenberg7add0582015-11-20 09:59:34 -08001837 for (const auto& ch : recv_streams_) {
solenberg0a617e22015-10-20 15:49:38 -07001838 SetNack(ch.second->channel(), nack_enabled_);
wu@webrtc.org9dba5252013-08-05 20:36:57 +00001839 }
solenberg0a617e22015-10-20 15:49:38 -07001840
1841 return true;
wu@webrtc.org9dba5252013-08-05 20:36:57 +00001842}
1843
wu@webrtc.orgcadf9042013-08-30 21:24:16 +00001844void WebRtcVoiceMediaChannel::SetNack(int channel, bool nack_enabled) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001845 if (nack_enabled) {
wu@webrtc.orgcadf9042013-08-30 21:24:16 +00001846 LOG(LS_INFO) << "Enabling NACK for channel " << channel;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001847 engine()->voe()->rtp()->SetNACKStatus(channel, true, kNackMaxPackets);
1848 } else {
wu@webrtc.orgcadf9042013-08-30 21:24:16 +00001849 LOG(LS_INFO) << "Disabling NACK for channel " << channel;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001850 engine()->voe()->rtp()->SetNACKStatus(channel, false, 0);
1851 }
1852}
1853
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001854bool WebRtcVoiceMediaChannel::SetSendCodec(
wu@webrtc.org9dba5252013-08-05 20:36:57 +00001855 int channel, const webrtc::CodecInst& send_codec) {
1856 LOG(LS_INFO) << "Send channel " << channel << " selected voice codec "
1857 << ToString(send_codec) << ", bitrate=" << send_codec.rate;
1858
wu@webrtc.org05e7b442014-04-01 17:44:24 +00001859 webrtc::CodecInst current_codec;
1860 if (engine()->voe()->codec()->GetSendCodec(channel, current_codec) == 0 &&
1861 (send_codec == current_codec)) {
1862 // Codec is already configured, we can return without setting it again.
1863 return true;
1864 }
1865
wu@webrtc.org9dba5252013-08-05 20:36:57 +00001866 if (engine()->voe()->codec()->SetSendCodec(channel, send_codec) == -1) {
1867 LOG_RTCERR2(SetSendCodec, channel, ToString(send_codec));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001868 return false;
1869 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001870 return true;
1871}
1872
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001873bool WebRtcVoiceMediaChannel::SetPlayout(bool playout) {
1874 desired_playout_ = playout;
1875 return ChangePlayout(desired_playout_);
1876}
1877
1878bool WebRtcVoiceMediaChannel::PausePlayout() {
1879 return ChangePlayout(false);
1880}
1881
1882bool WebRtcVoiceMediaChannel::ResumePlayout() {
1883 return ChangePlayout(desired_playout_);
1884}
1885
1886bool WebRtcVoiceMediaChannel::ChangePlayout(bool playout) {
solenberg566ef242015-11-06 15:34:49 -08001887 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001888 if (playout_ == playout) {
1889 return true;
1890 }
1891
solenberg7add0582015-11-20 09:59:34 -08001892 for (const auto& ch : recv_streams_) {
Fredrik Solenbergaf9fb212015-08-26 10:45:53 +02001893 if (!SetPlayout(ch.second->channel(), playout)) {
henrike@webrtc.org1e09a712013-07-26 19:17:59 +00001894 LOG(LS_ERROR) << "SetPlayout " << playout << " on channel "
Fredrik Solenbergaf9fb212015-08-26 10:45:53 +02001895 << ch.second->channel() << " failed";
solenberg1ac56142015-10-13 03:58:19 -07001896 return false;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001897 }
1898 }
solenberg1ac56142015-10-13 03:58:19 -07001899 playout_ = playout;
1900 return true;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001901}
1902
1903bool WebRtcVoiceMediaChannel::SetSend(SendFlags send) {
1904 desired_send_ = send;
solenbergc96df772015-10-21 13:01:53 -07001905 if (!send_streams_.empty()) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001906 return ChangeSend(desired_send_);
solenbergc96df772015-10-21 13:01:53 -07001907 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001908 return true;
1909}
1910
1911bool WebRtcVoiceMediaChannel::PauseSend() {
1912 return ChangeSend(SEND_NOTHING);
1913}
1914
1915bool WebRtcVoiceMediaChannel::ResumeSend() {
1916 return ChangeSend(desired_send_);
1917}
1918
1919bool WebRtcVoiceMediaChannel::ChangeSend(SendFlags send) {
1920 if (send_ == send) {
1921 return true;
1922 }
1923
solenberg63b34542015-09-29 06:06:31 -07001924 // Apply channel specific options.
1925 if (send == SEND_MICROPHONE) {
1926 engine()->ApplyOptions(options_);
1927 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001928
wu@webrtc.org9dba5252013-08-05 20:36:57 +00001929 // Change the settings on each send channel.
solenbergc96df772015-10-21 13:01:53 -07001930 for (const auto& ch : send_streams_) {
solenberg63b34542015-09-29 06:06:31 -07001931 if (!ChangeSend(ch.second->channel(), send)) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001932 return false;
solenberg63b34542015-09-29 06:06:31 -07001933 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001934 }
wu@webrtc.org9dba5252013-08-05 20:36:57 +00001935
solenberg63b34542015-09-29 06:06:31 -07001936 // Clear up the options after stopping sending. Since we may previously have
1937 // applied the channel specific options, now apply the original options stored
1938 // in WebRtcVoiceEngine.
1939 if (send == SEND_NOTHING) {
1940 engine()->ApplyOptions(engine()->GetOptions());
1941 }
wu@webrtc.org9dba5252013-08-05 20:36:57 +00001942
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001943 send_ = send;
1944 return true;
1945}
1946
wu@webrtc.org9dba5252013-08-05 20:36:57 +00001947bool WebRtcVoiceMediaChannel::ChangeSend(int channel, SendFlags send) {
1948 if (send == SEND_MICROPHONE) {
1949 if (engine()->voe()->base()->StartSend(channel) == -1) {
1950 LOG_RTCERR1(StartSend, channel);
1951 return false;
1952 }
wu@webrtc.org9dba5252013-08-05 20:36:57 +00001953 } else { // SEND_NOTHING
henrikg91d6ede2015-09-17 00:24:34 -07001954 RTC_DCHECK(send == SEND_NOTHING);
wu@webrtc.org9dba5252013-08-05 20:36:57 +00001955 if (engine()->voe()->base()->StopSend(channel) == -1) {
1956 LOG_RTCERR1(StopSend, channel);
1957 return false;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001958 }
1959 }
1960
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001961 return true;
1962}
1963
Peter Boström0c4e06b2015-10-07 12:23:21 +02001964bool WebRtcVoiceMediaChannel::SetAudioSend(uint32_t ssrc,
1965 bool enable,
solenberg1dd98f32015-09-10 01:57:14 -07001966 const AudioOptions* options,
1967 AudioRenderer* renderer) {
solenberg566ef242015-11-06 15:34:49 -08001968 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
solenberg1dd98f32015-09-10 01:57:14 -07001969 // TODO(solenberg): The state change should be fully rolled back if any one of
1970 // these calls fail.
1971 if (!SetLocalRenderer(ssrc, renderer)) {
1972 return false;
1973 }
solenbergdfc8f4f2015-10-01 02:31:10 -07001974 if (!MuteStream(ssrc, !enable)) {
solenberg1dd98f32015-09-10 01:57:14 -07001975 return false;
1976 }
solenbergdfc8f4f2015-10-01 02:31:10 -07001977 if (enable && options) {
solenberg1dd98f32015-09-10 01:57:14 -07001978 return SetOptions(*options);
1979 }
1980 return true;
1981}
1982
solenberg0a617e22015-10-20 15:49:38 -07001983int WebRtcVoiceMediaChannel::CreateVoEChannel() {
1984 int id = engine()->CreateVoEChannel();
1985 if (id == -1) {
1986 LOG_RTCERR0(CreateVoEChannel);
1987 return -1;
wu@webrtc.org9dba5252013-08-05 20:36:57 +00001988 }
solenberg0a617e22015-10-20 15:49:38 -07001989 if (engine()->voe()->network()->RegisterExternalTransport(id, *this) == -1) {
1990 LOG_RTCERR2(RegisterExternalTransport, id, this);
1991 engine()->voe()->base()->DeleteChannel(id);
1992 return -1;
1993 }
1994 return id;
wu@webrtc.org9dba5252013-08-05 20:36:57 +00001995}
1996
solenberg7add0582015-11-20 09:59:34 -08001997bool WebRtcVoiceMediaChannel::DeleteVoEChannel(int channel) {
wu@webrtc.org9dba5252013-08-05 20:36:57 +00001998 if (engine()->voe()->network()->DeRegisterExternalTransport(channel) == -1) {
1999 LOG_RTCERR1(DeRegisterExternalTransport, channel);
2000 }
wu@webrtc.org9dba5252013-08-05 20:36:57 +00002001 if (engine()->voe()->base()->DeleteChannel(channel) == -1) {
2002 LOG_RTCERR1(DeleteChannel, channel);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002003 return false;
2004 }
wu@webrtc.org9dba5252013-08-05 20:36:57 +00002005 return true;
2006}
henrike@webrtc.org1e09a712013-07-26 19:17:59 +00002007
wu@webrtc.org9dba5252013-08-05 20:36:57 +00002008bool WebRtcVoiceMediaChannel::AddSendStream(const StreamParams& sp) {
solenberg566ef242015-11-06 15:34:49 -08002009 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
solenberg0a617e22015-10-20 15:49:38 -07002010 LOG(LS_INFO) << "AddSendStream: " << sp.ToString();
2011
2012 uint32_t ssrc = sp.first_ssrc();
2013 RTC_DCHECK(0 != ssrc);
2014
2015 if (GetSendChannelId(ssrc) != -1) {
2016 LOG(LS_ERROR) << "Stream already exists with ssrc " << ssrc;
wu@webrtc.org9dba5252013-08-05 20:36:57 +00002017 return false;
2018 }
2019
solenberg0a617e22015-10-20 15:49:38 -07002020 // Create a new channel for sending audio data.
2021 int channel = CreateVoEChannel();
2022 if (channel == -1) {
2023 return false;
wu@webrtc.org9dba5252013-08-05 20:36:57 +00002024 }
wu@webrtc.org9dba5252013-08-05 20:36:57 +00002025
solenbergc96df772015-10-21 13:01:53 -07002026 // Save the channel to send_streams_, so that RemoveSendStream() can still
wu@webrtc.org9dba5252013-08-05 20:36:57 +00002027 // delete the channel in case failure happens below.
mallinath@webrtc.org67ee6b92014-02-03 16:57:16 +00002028 webrtc::AudioTransport* audio_transport =
2029 engine()->voe()->base()->audio_transport();
solenberg3a941542015-11-16 07:34:50 -08002030 send_streams_.insert(std::make_pair(ssrc, new WebRtcAudioSendStream(
2031 channel, audio_transport, ssrc, sp.cname, send_rtp_extensions_, call_)));
wu@webrtc.org9dba5252013-08-05 20:36:57 +00002032
solenberg0a617e22015-10-20 15:49:38 -07002033 // Set the current codecs to be used for the new channel. We need to do this
2034 // after adding the channel to send_channels_, because of how max bitrate is
2035 // currently being configured by SetSendCodec().
2036 if (!send_codecs_.empty() && !SetSendCodecs(channel, send_codecs_)) {
2037 RemoveSendStream(ssrc);
wu@webrtc.org9dba5252013-08-05 20:36:57 +00002038 return false;
2039 }
2040
2041 // At this point the channel's local SSRC has been updated. If the channel is
solenberg0a617e22015-10-20 15:49:38 -07002042 // the first send channel make sure that all the receive channels are updated
2043 // with the same SSRC in order to send receiver reports.
solenbergc96df772015-10-21 13:01:53 -07002044 if (send_streams_.size() == 1) {
solenberg0a617e22015-10-20 15:49:38 -07002045 receiver_reports_ssrc_ = ssrc;
solenberg7add0582015-11-20 09:59:34 -08002046 for (const auto& stream : recv_streams_) {
2047 int recv_channel = stream.second->channel();
solenberg0a617e22015-10-20 15:49:38 -07002048 if (engine()->voe()->rtp()->SetLocalSSRC(recv_channel, ssrc) != 0) {
solenberg7add0582015-11-20 09:59:34 -08002049 LOG_RTCERR2(SetLocalSSRC, recv_channel, ssrc);
solenberg1ac56142015-10-13 03:58:19 -07002050 return false;
wu@webrtc.org9dba5252013-08-05 20:36:57 +00002051 }
solenberg0a617e22015-10-20 15:49:38 -07002052 engine()->voe()->base()->AssociateSendChannel(recv_channel, channel);
2053 LOG(LS_INFO) << "VoiceEngine channel #" << recv_channel
2054 << " is associated with channel #" << channel << ".";
wu@webrtc.org9dba5252013-08-05 20:36:57 +00002055 }
2056 }
2057
wu@webrtc.org9dba5252013-08-05 20:36:57 +00002058 return ChangeSend(channel, desired_send_);
2059}
2060
Peter Boström0c4e06b2015-10-07 12:23:21 +02002061bool WebRtcVoiceMediaChannel::RemoveSendStream(uint32_t ssrc) {
solenberg566ef242015-11-06 15:34:49 -08002062 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
solenberg3a941542015-11-16 07:34:50 -08002063 LOG(LS_INFO) << "RemoveSendStream: " << ssrc;
2064
solenbergc96df772015-10-21 13:01:53 -07002065 auto it = send_streams_.find(ssrc);
2066 if (it == send_streams_.end()) {
wu@webrtc.org9dba5252013-08-05 20:36:57 +00002067 LOG(LS_WARNING) << "Try to remove stream with ssrc " << ssrc
2068 << " which doesn't exist.";
2069 return false;
2070 }
2071
mallinath@webrtc.org67ee6b92014-02-03 16:57:16 +00002072 int channel = it->second->channel();
wu@webrtc.org9dba5252013-08-05 20:36:57 +00002073 ChangeSend(channel, SEND_NOTHING);
2074
solenberg7add0582015-11-20 09:59:34 -08002075 // Clean up and delete the send stream+channel.
solenberg0a617e22015-10-20 15:49:38 -07002076 LOG(LS_INFO) << "Removing audio send stream " << ssrc
2077 << " with VoiceEngine channel #" << channel << ".";
solenberg7add0582015-11-20 09:59:34 -08002078 delete it->second;
2079 send_streams_.erase(it);
2080 if (!DeleteVoEChannel(channel)) {
solenberg0a617e22015-10-20 15:49:38 -07002081 return false;
wu@webrtc.org9dba5252013-08-05 20:36:57 +00002082 }
solenbergc96df772015-10-21 13:01:53 -07002083 if (send_streams_.empty()) {
wu@webrtc.org9dba5252013-08-05 20:36:57 +00002084 ChangeSend(SEND_NOTHING);
solenberg0a617e22015-10-20 15:49:38 -07002085 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002086 return true;
2087}
2088
2089bool WebRtcVoiceMediaChannel::AddRecvStream(const StreamParams& sp) {
solenberg566ef242015-11-06 15:34:49 -08002090 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
solenbergd97ec302015-10-07 01:40:33 -07002091 LOG(LS_INFO) << "AddRecvStream: " << sp.ToString();
2092
solenberg0b675462015-10-09 01:37:09 -07002093 if (!ValidateStreamParams(sp)) {
wu@webrtc.org78187522013-10-07 23:32:02 +00002094 return false;
2095 }
2096
solenberg7add0582015-11-20 09:59:34 -08002097 const uint32_t ssrc = sp.first_ssrc();
solenberg0b675462015-10-09 01:37:09 -07002098 if (ssrc == 0) {
2099 LOG(LS_WARNING) << "AddRecvStream with ssrc==0 is not supported.";
2100 return false;
2101 }
2102
solenberg1ac56142015-10-13 03:58:19 -07002103 // Remove the default receive stream if one had been created with this ssrc;
2104 // we'll recreate it then.
2105 if (IsDefaultRecvStream(ssrc)) {
2106 RemoveRecvStream(ssrc);
2107 }
solenberg0b675462015-10-09 01:37:09 -07002108
solenberg7add0582015-11-20 09:59:34 -08002109 if (GetReceiveChannelId(ssrc) != -1) {
henrike@webrtc.org1e09a712013-07-26 19:17:59 +00002110 LOG(LS_ERROR) << "Stream already exists with ssrc " << ssrc;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002111 return false;
2112 }
Fredrik Solenberg4b60c732015-05-07 14:07:48 +02002113
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002114 // Create a new channel for receiving audio data.
solenberg7add0582015-11-20 09:59:34 -08002115 const int channel = CreateVoEChannel();
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002116 if (channel == -1) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002117 return false;
2118 }
Minyue2013aec2015-05-13 14:14:42 +02002119
solenberg1ac56142015-10-13 03:58:19 -07002120 // Turn off all supported codecs.
solenberg26c8c912015-11-27 04:00:25 -08002121 // TODO(solenberg): Remove once "no codecs" is the default state of a stream.
2122 for (webrtc::CodecInst voe_codec : webrtc::acm2::RentACodec::Database()) {
2123 voe_codec.pltype = -1;
2124 if (engine()->voe()->codec()->SetRecPayloadType(channel, voe_codec) == -1) {
2125 LOG_RTCERR2(SetRecPayloadType, channel, ToString(voe_codec));
2126 DeleteVoEChannel(channel);
2127 return false;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002128 }
2129 }
2130
solenberg1ac56142015-10-13 03:58:19 -07002131 // Only enable those configured for this channel.
2132 for (const auto& codec : recv_codecs_) {
2133 webrtc::CodecInst voe_codec;
solenberg26c8c912015-11-27 04:00:25 -08002134 if (WebRtcVoiceEngine::ToCodecInst(codec, &voe_codec)) {
solenberg1ac56142015-10-13 03:58:19 -07002135 voe_codec.pltype = codec.id;
2136 if (engine()->voe()->codec()->SetRecPayloadType(
2137 channel, voe_codec) == -1) {
2138 LOG_RTCERR2(SetRecPayloadType, channel, ToString(voe_codec));
solenberg7add0582015-11-20 09:59:34 -08002139 DeleteVoEChannel(channel);
solenberg1ac56142015-10-13 03:58:19 -07002140 return false;
2141 }
henrike@webrtc.org1e09a712013-07-26 19:17:59 +00002142 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002143 }
solenberg8fb30c32015-10-13 03:06:58 -07002144
solenberg7add0582015-11-20 09:59:34 -08002145 const int send_channel = GetSendChannelId(receiver_reports_ssrc_);
2146 if (send_channel != -1) {
2147 // Associate receive channel with first send channel (so the receive channel
2148 // can obtain RTT from the send channel)
2149 engine()->voe()->base()->AssociateSendChannel(channel, send_channel);
2150 LOG(LS_INFO) << "VoiceEngine channel #" << channel
2151 << " is associated with channel #" << send_channel << ".";
buildbot@webrtc.org150835e2014-05-06 15:54:38 +00002152 }
2153
solenberg7add0582015-11-20 09:59:34 -08002154 recv_streams_.insert(std::make_pair(ssrc, new WebRtcAudioReceiveStream(
2155 channel, ssrc, receiver_reports_ssrc_,
2156 options_.combined_audio_video_bwe.value_or(false), sp.sync_label,
2157 recv_rtp_extensions_, call_)));
2158
2159 SetNack(channel, nack_enabled_);
solenberg1ac56142015-10-13 03:58:19 -07002160 SetPlayout(channel, playout_);
solenberg7add0582015-11-20 09:59:34 -08002161
solenberg1ac56142015-10-13 03:58:19 -07002162 return true;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002163}
2164
Peter Boström0c4e06b2015-10-07 12:23:21 +02002165bool WebRtcVoiceMediaChannel::RemoveRecvStream(uint32_t ssrc) {
solenberg566ef242015-11-06 15:34:49 -08002166 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
solenbergd97ec302015-10-07 01:40:33 -07002167 LOG(LS_INFO) << "RemoveRecvStream: " << ssrc;
2168
solenberg7add0582015-11-20 09:59:34 -08002169 const auto it = recv_streams_.find(ssrc);
2170 if (it == recv_streams_.end()) {
wu@webrtc.org9dba5252013-08-05 20:36:57 +00002171 LOG(LS_WARNING) << "Try to remove stream with ssrc " << ssrc
2172 << " which doesn't exist.";
henrike@webrtc.org1e09a712013-07-26 19:17:59 +00002173 return false;
wu@webrtc.org9dba5252013-08-05 20:36:57 +00002174 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002175
solenberg1ac56142015-10-13 03:58:19 -07002176 // Deregister default channel, if that's the one being destroyed.
2177 if (IsDefaultRecvStream(ssrc)) {
2178 default_recv_ssrc_ = -1;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002179 }
henrike@webrtc.org1e09a712013-07-26 19:17:59 +00002180
solenberg7add0582015-11-20 09:59:34 -08002181 const int channel = it->second->channel();
2182
2183 // Clean up and delete the receive stream+channel.
2184 LOG(LS_INFO) << "Removing audio receive stream " << ssrc
mallinath@webrtc.org67ee6b92014-02-03 16:57:16 +00002185 << " with VoiceEngine channel #" << channel << ".";
solenberg7add0582015-11-20 09:59:34 -08002186 delete it->second;
2187 recv_streams_.erase(it);
2188 return DeleteVoEChannel(channel);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002189}
2190
Peter Boström0c4e06b2015-10-07 12:23:21 +02002191bool WebRtcVoiceMediaChannel::SetLocalRenderer(uint32_t ssrc,
henrike@webrtc.org1e09a712013-07-26 19:17:59 +00002192 AudioRenderer* renderer) {
solenbergc96df772015-10-21 13:01:53 -07002193 auto it = send_streams_.find(ssrc);
2194 if (it == send_streams_.end()) {
wu@webrtc.org9dba5252013-08-05 20:36:57 +00002195 if (renderer) {
2196 // Return an error if trying to set a valid renderer with an invalid ssrc.
2197 LOG(LS_ERROR) << "SetLocalRenderer failed with ssrc "<< ssrc;
2198 return false;
2199 }
2200
2201 // The channel likely has gone away, do nothing.
henrike@webrtc.org1e09a712013-07-26 19:17:59 +00002202 return true;
henrike@webrtc.org1e09a712013-07-26 19:17:59 +00002203 }
2204
solenberg1ac56142015-10-13 03:58:19 -07002205 if (renderer) {
mallinath@webrtc.org67ee6b92014-02-03 16:57:16 +00002206 it->second->Start(renderer);
solenberg1ac56142015-10-13 03:58:19 -07002207 } else {
mallinath@webrtc.org67ee6b92014-02-03 16:57:16 +00002208 it->second->Stop();
solenberg1ac56142015-10-13 03:58:19 -07002209 }
henrike@webrtc.org1e09a712013-07-26 19:17:59 +00002210
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002211 return true;
2212}
2213
2214bool WebRtcVoiceMediaChannel::GetActiveStreams(
2215 AudioInfo::StreamList* actives) {
solenberg566ef242015-11-06 15:34:49 -08002216 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002217 actives->clear();
solenberg7add0582015-11-20 09:59:34 -08002218 for (const auto& ch : recv_streams_) {
Fredrik Solenbergaf9fb212015-08-26 10:45:53 +02002219 int level = GetOutputLevel(ch.second->channel());
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002220 if (level > 0) {
Fredrik Solenbergaf9fb212015-08-26 10:45:53 +02002221 actives->push_back(std::make_pair(ch.first, level));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002222 }
2223 }
2224 return true;
2225}
2226
2227int WebRtcVoiceMediaChannel::GetOutputLevel() {
solenberg566ef242015-11-06 15:34:49 -08002228 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
solenberg1ac56142015-10-13 03:58:19 -07002229 int highest = 0;
solenberg7add0582015-11-20 09:59:34 -08002230 for (const auto& ch : recv_streams_) {
solenberg8fb30c32015-10-13 03:06:58 -07002231 highest = std::max(GetOutputLevel(ch.second->channel()), highest);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002232 }
2233 return highest;
2234}
2235
2236int WebRtcVoiceMediaChannel::GetTimeSinceLastTyping() {
2237 int ret;
2238 if (engine()->voe()->processing()->TimeSinceLastTyping(ret) == -1) {
2239 // In case of error, log the info and continue
2240 LOG_RTCERR0(TimeSinceLastTyping);
2241 ret = -1;
2242 } else {
2243 ret *= 1000; // We return ms, webrtc returns seconds.
2244 }
2245 return ret;
2246}
2247
2248void WebRtcVoiceMediaChannel::SetTypingDetectionParameters(int time_window,
2249 int cost_per_typing, int reporting_threshold, int penalty_decay,
2250 int type_event_delay) {
2251 if (engine()->voe()->processing()->SetTypingDetectionParameters(
2252 time_window, cost_per_typing,
2253 reporting_threshold, penalty_decay, type_event_delay) == -1) {
2254 // In case of error, log the info and continue
2255 LOG_RTCERR5(SetTypingDetectionParameters, time_window,
2256 cost_per_typing, reporting_threshold, penalty_decay,
2257 type_event_delay);
2258 }
2259}
2260
solenberg4bac9c52015-10-09 02:32:53 -07002261bool WebRtcVoiceMediaChannel::SetOutputVolume(uint32_t ssrc, double volume) {
solenberg566ef242015-11-06 15:34:49 -08002262 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
solenberg1ac56142015-10-13 03:58:19 -07002263 if (ssrc == 0) {
2264 default_recv_volume_ = volume;
2265 if (default_recv_ssrc_ == -1) {
2266 return true;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002267 }
solenberg1ac56142015-10-13 03:58:19 -07002268 ssrc = static_cast<uint32_t>(default_recv_ssrc_);
2269 }
2270 int ch_id = GetReceiveChannelId(ssrc);
2271 if (ch_id < 0) {
2272 LOG(LS_WARNING) << "Cannot find channel for ssrc:" << ssrc;
2273 return false;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002274 }
2275
solenberg1ac56142015-10-13 03:58:19 -07002276 if (-1 == engine()->voe()->volume()->SetChannelOutputVolumeScaling(ch_id,
2277 volume)) {
2278 LOG_RTCERR2(SetChannelOutputVolumeScaling, ch_id, volume);
2279 return false;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002280 }
solenberg1ac56142015-10-13 03:58:19 -07002281 LOG(LS_INFO) << "SetOutputVolume to " << volume
2282 << " for channel " << ch_id << " and ssrc " << ssrc;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002283 return true;
2284}
2285
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002286bool WebRtcVoiceMediaChannel::CanInsertDtmf() {
Fredrik Solenbergb5727682015-12-04 15:22:19 +01002287 return dtmf_payload_type_ ? true : false;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002288}
2289
solenberg1d63dd02015-12-02 12:35:09 -08002290bool WebRtcVoiceMediaChannel::InsertDtmf(uint32_t ssrc, int event,
2291 int duration) {
solenberg566ef242015-11-06 15:34:49 -08002292 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
Fredrik Solenbergb5727682015-12-04 15:22:19 +01002293 LOG(LS_INFO) << "WebRtcVoiceMediaChannel::InsertDtmf";
2294 if (!dtmf_payload_type_) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002295 return false;
2296 }
2297
Fredrik Solenbergb5727682015-12-04 15:22:19 +01002298 // Figure out which WebRtcAudioSendStream to send the event on.
2299 auto it = ssrc != 0 ? send_streams_.find(ssrc) : send_streams_.begin();
2300 if (it == send_streams_.end()) {
2301 LOG(LS_WARNING) << "The specified ssrc " << ssrc << " is not in use.";
solenberg1d63dd02015-12-02 12:35:09 -08002302 return false;
2303 }
Fredrik Solenbergb5727682015-12-04 15:22:19 +01002304 if (event < kMinTelephoneEventCode ||
2305 event > kMaxTelephoneEventCode) {
2306 LOG(LS_WARNING) << "DTMF event code " << event << " out of range.";
solenberg1d63dd02015-12-02 12:35:09 -08002307 return false;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002308 }
Fredrik Solenbergb5727682015-12-04 15:22:19 +01002309 if (duration < kMinTelephoneEventDuration ||
2310 duration > kMaxTelephoneEventDuration) {
2311 LOG(LS_WARNING) << "DTMF event duration " << duration << " out of range.";
2312 return false;
2313 }
2314 return it->second->SendTelephoneEvent(*dtmf_payload_type_, event, duration);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002315}
2316
wu@webrtc.orga9890802013-12-13 00:21:03 +00002317void WebRtcVoiceMediaChannel::OnPacketReceived(
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00002318 rtc::Buffer* packet, const rtc::PacketTime& packet_time) {
solenberg566ef242015-11-06 15:34:49 -08002319 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
Fredrik Solenberg4b60c732015-05-07 14:07:48 +02002320
solenberg1ac56142015-10-13 03:58:19 -07002321 uint32_t ssrc = 0;
2322 if (!GetRtpSsrc(packet->data(), packet->size(), &ssrc)) {
2323 return;
2324 }
2325
solenberg7e63ef02015-11-20 00:19:43 -08002326 // If we don't have a default channel, and the SSRC is unknown, create a
2327 // default channel.
2328 if (default_recv_ssrc_ == -1 && GetReceiveChannelId(ssrc) == -1) {
solenberg1ac56142015-10-13 03:58:19 -07002329 StreamParams sp;
2330 sp.ssrcs.push_back(ssrc);
2331 LOG(LS_INFO) << "Creating default receive stream for SSRC=" << ssrc << ".";
2332 if (!AddRecvStream(sp)) {
2333 LOG(LS_WARNING) << "Could not create default receive stream.";
2334 return;
2335 }
2336 default_recv_ssrc_ = ssrc;
2337 SetOutputVolume(default_recv_ssrc_, default_recv_volume_);
2338 }
2339
2340 // Forward packet to Call. If the SSRC is unknown we'll return after this.
Fredrik Solenberg709ed672015-09-15 12:26:33 +02002341 const webrtc::PacketTime webrtc_packet_time(packet_time.timestamp,
2342 packet_time.not_before);
solenberg1ac56142015-10-13 03:58:19 -07002343 webrtc::PacketReceiver::DeliveryStatus delivery_result =
2344 call_->Receiver()->DeliverPacket(webrtc::MediaType::AUDIO,
2345 reinterpret_cast<const uint8_t*>(packet->data()), packet->size(),
2346 webrtc_packet_time);
2347 if (webrtc::PacketReceiver::DELIVERY_OK != delivery_result) {
solenberg7e63ef02015-11-20 00:19:43 -08002348 // If the SSRC is unknown here, route it to the default channel, if we have
2349 // one. See: https://bugs.chromium.org/p/webrtc/issues/detail?id=5208
2350 if (default_recv_ssrc_ == -1) {
2351 return;
2352 } else {
2353 ssrc = default_recv_ssrc_;
2354 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002355 }
2356
solenberg1ac56142015-10-13 03:58:19 -07002357 // Find the channel to send this packet to. It must exist since webrtc::Call
2358 // was able to demux the packet.
2359 int channel = GetReceiveChannelId(ssrc);
2360 RTC_DCHECK(channel != -1);
2361
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002362 // Pass it off to the decoder.
henrike@webrtc.org28654cb2013-07-22 21:07:49 +00002363 engine()->voe()->network()->ReceivedRTPPacket(
solenberg1ac56142015-10-13 03:58:19 -07002364 channel, packet->data(), packet->size(), webrtc_packet_time);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002365}
2366
wu@webrtc.orga9890802013-12-13 00:21:03 +00002367void WebRtcVoiceMediaChannel::OnRtcpReceived(
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00002368 rtc::Buffer* packet, const rtc::PacketTime& packet_time) {
solenberg566ef242015-11-06 15:34:49 -08002369 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
Fredrik Solenberg4b60c732015-05-07 14:07:48 +02002370
Fredrik Solenberg709ed672015-09-15 12:26:33 +02002371 // Forward packet to Call as well.
2372 const webrtc::PacketTime webrtc_packet_time(packet_time.timestamp,
2373 packet_time.not_before);
2374 call_->Receiver()->DeliverPacket(webrtc::MediaType::AUDIO,
2375 reinterpret_cast<const uint8_t*>(packet->data()), packet->size(),
2376 webrtc_packet_time);
Fredrik Solenberg4b60c732015-05-07 14:07:48 +02002377
wu@webrtc.org9dba5252013-08-05 20:36:57 +00002378 // Sending channels need all RTCP packets with feedback information.
2379 // Even sender reports can contain attached report blocks.
2380 // Receiving channels need sender reports in order to create
2381 // correct receiver reports.
2382 int type = 0;
kwiberg@webrtc.orgeebcab52015-03-24 09:19:06 +00002383 if (!GetRtcpType(packet->data(), packet->size(), &type)) {
wu@webrtc.org9dba5252013-08-05 20:36:57 +00002384 LOG(LS_WARNING) << "Failed to parse type from received RTCP packet";
2385 return;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002386 }
2387
solenberg0b675462015-10-09 01:37:09 -07002388 // If it is a sender report, find the receive channel that is listening.
wu@webrtc.org9dba5252013-08-05 20:36:57 +00002389 if (type == kRtcpTypeSR) {
solenberg0b675462015-10-09 01:37:09 -07002390 uint32_t ssrc = 0;
2391 if (!GetRtcpSsrc(packet->data(), packet->size(), &ssrc)) {
2392 return;
2393 }
2394 int recv_channel_id = GetReceiveChannelId(ssrc);
2395 if (recv_channel_id != -1) {
wu@webrtc.org9dba5252013-08-05 20:36:57 +00002396 engine()->voe()->network()->ReceivedRTCPPacket(
solenberg0b675462015-10-09 01:37:09 -07002397 recv_channel_id, packet->data(), packet->size());
wu@webrtc.org9dba5252013-08-05 20:36:57 +00002398 }
2399 }
2400
2401 // SR may continue RR and any RR entry may correspond to any one of the send
2402 // channels. So all RTCP packets must be forwarded all send channels. VoE
2403 // will filter out RR internally.
solenbergc96df772015-10-21 13:01:53 -07002404 for (const auto& ch : send_streams_) {
wu@webrtc.org9dba5252013-08-05 20:36:57 +00002405 engine()->voe()->network()->ReceivedRTCPPacket(
Fredrik Solenbergaf9fb212015-08-26 10:45:53 +02002406 ch.second->channel(), packet->data(), packet->size());
wu@webrtc.org9dba5252013-08-05 20:36:57 +00002407 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002408}
2409
Peter Boström0c4e06b2015-10-07 12:23:21 +02002410bool WebRtcVoiceMediaChannel::MuteStream(uint32_t ssrc, bool muted) {
solenberg566ef242015-11-06 15:34:49 -08002411 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
solenberg0a617e22015-10-20 15:49:38 -07002412 int channel = GetSendChannelId(ssrc);
wu@webrtc.org9dba5252013-08-05 20:36:57 +00002413 if (channel == -1) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002414 LOG(LS_WARNING) << "The specified ssrc " << ssrc << " is not in use.";
2415 return false;
2416 }
wu@webrtc.org9dba5252013-08-05 20:36:57 +00002417 if (engine()->voe()->volume()->SetInputMute(channel, muted) == -1) {
2418 LOG_RTCERR2(SetInputMute, channel, muted);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002419 return false;
2420 }
buildbot@webrtc.org6b21b712014-07-31 15:08:53 +00002421 // We set the AGC to mute state only when all the channels are muted.
2422 // This implementation is not ideal, instead we should signal the AGC when
2423 // the mic channel is muted/unmuted. We can't do it today because there
2424 // is no good way to know which stream is mapping to the mic channel.
2425 bool all_muted = muted;
solenbergc96df772015-10-21 13:01:53 -07002426 for (const auto& ch : send_streams_) {
Fredrik Solenbergaf9fb212015-08-26 10:45:53 +02002427 if (!all_muted) {
2428 break;
2429 }
2430 if (engine()->voe()->volume()->GetInputMute(ch.second->channel(),
buildbot@webrtc.org6b21b712014-07-31 15:08:53 +00002431 all_muted)) {
Fredrik Solenbergaf9fb212015-08-26 10:45:53 +02002432 LOG_RTCERR1(GetInputMute, ch.second->channel());
buildbot@webrtc.org6b21b712014-07-31 15:08:53 +00002433 return false;
2434 }
2435 }
2436
2437 webrtc::AudioProcessing* ap = engine()->voe()->base()->audio_processing();
solenberg0a617e22015-10-20 15:49:38 -07002438 if (ap) {
buildbot@webrtc.org6b21b712014-07-31 15:08:53 +00002439 ap->set_output_will_be_muted(all_muted);
solenberg0a617e22015-10-20 15:49:38 -07002440 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002441 return true;
2442}
2443
minyue@webrtc.org26236952014-10-29 02:27:08 +00002444// TODO(minyue): SetMaxSendBandwidth() is subject to be renamed to
2445// SetMaxSendBitrate() in future.
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00002446bool WebRtcVoiceMediaChannel::SetMaxSendBandwidth(int bps) {
minyue@webrtc.org26236952014-10-29 02:27:08 +00002447 LOG(LS_INFO) << "WebRtcVoiceMediaChannel::SetMaxSendBandwidth.";
minyue@webrtc.org26236952014-10-29 02:27:08 +00002448 return SetSendBitrateInternal(bps);
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00002449}
2450
minyue@webrtc.org26236952014-10-29 02:27:08 +00002451bool WebRtcVoiceMediaChannel::SetSendBitrateInternal(int bps) {
2452 LOG(LS_INFO) << "WebRtcVoiceMediaChannel::SetSendBitrateInternal.";
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00002453
minyue@webrtc.org26236952014-10-29 02:27:08 +00002454 send_bitrate_setting_ = true;
2455 send_bitrate_bps_ = bps;
wu@webrtc.org1d1ffc92013-10-16 18:12:02 +00002456
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002457 if (!send_codec_) {
wu@webrtc.org1d1ffc92013-10-16 18:12:02 +00002458 LOG(LS_INFO) << "The send codec has not been set up yet. "
minyue@webrtc.org26236952014-10-29 02:27:08 +00002459 << "The send bitrate setting will be applied later.";
wu@webrtc.org1d1ffc92013-10-16 18:12:02 +00002460 return true;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002461 }
2462
minyue@webrtc.org26236952014-10-29 02:27:08 +00002463 // Bitrate is auto by default.
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00002464 // TODO(bemasc): Fix this so that if SetMaxSendBandwidth(50) is followed by
2465 // SetMaxSendBandwith(0), the second call removes the previous limit.
2466 if (bps <= 0)
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002467 return true;
2468
2469 webrtc::CodecInst codec = *send_codec_;
solenberg26c8c912015-11-27 04:00:25 -08002470 bool is_multi_rate = WebRtcVoiceCodecs::IsCodecMultiRate(codec);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002471
2472 if (is_multi_rate) {
2473 // If codec is multi-rate then just set the bitrate.
2474 codec.rate = bps;
solenbergc96df772015-10-21 13:01:53 -07002475 for (const auto& ch : send_streams_) {
solenberg0a617e22015-10-20 15:49:38 -07002476 if (!SetSendCodec(ch.second->channel(), codec)) {
2477 LOG(LS_INFO) << "Failed to set codec " << codec.plname
2478 << " to bitrate " << bps << " bps.";
2479 return false;
2480 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002481 }
2482 return true;
2483 } else {
2484 // If codec is not multi-rate and |bps| is less than the fixed bitrate
2485 // then fail. If codec is not multi-rate and |bps| exceeds or equal the
2486 // fixed bitrate then ignore.
2487 if (bps < codec.rate) {
2488 LOG(LS_INFO) << "Failed to set codec " << codec.plname
2489 << " to bitrate " << bps << " bps"
2490 << ", requires at least " << codec.rate << " bps.";
2491 return false;
2492 }
2493 return true;
2494 }
2495}
2496
2497bool WebRtcVoiceMediaChannel::GetStats(VoiceMediaInfo* info) {
solenberg566ef242015-11-06 15:34:49 -08002498 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
solenberg85a04962015-10-27 03:35:21 -07002499 RTC_DCHECK(info);
solenbergd97ec302015-10-07 01:40:33 -07002500
solenberg85a04962015-10-27 03:35:21 -07002501 // Get SSRC and stats for each sender.
2502 RTC_DCHECK(info->senders.size() == 0);
2503 for (const auto& stream : send_streams_) {
2504 webrtc::AudioSendStream::Stats stats = stream.second->GetStats();
wu@webrtc.org9dba5252013-08-05 20:36:57 +00002505 VoiceSenderInfo sinfo;
solenberg85a04962015-10-27 03:35:21 -07002506 sinfo.add_ssrc(stats.local_ssrc);
2507 sinfo.bytes_sent = stats.bytes_sent;
2508 sinfo.packets_sent = stats.packets_sent;
2509 sinfo.packets_lost = stats.packets_lost;
2510 sinfo.fraction_lost = stats.fraction_lost;
2511 sinfo.codec_name = stats.codec_name;
2512 sinfo.ext_seqnum = stats.ext_seqnum;
2513 sinfo.jitter_ms = stats.jitter_ms;
2514 sinfo.rtt_ms = stats.rtt_ms;
2515 sinfo.audio_level = stats.audio_level;
2516 sinfo.aec_quality_min = stats.aec_quality_min;
2517 sinfo.echo_delay_median_ms = stats.echo_delay_median_ms;
2518 sinfo.echo_delay_std_ms = stats.echo_delay_std_ms;
2519 sinfo.echo_return_loss = stats.echo_return_loss;
2520 sinfo.echo_return_loss_enhancement = stats.echo_return_loss_enhancement;
solenberg566ef242015-11-06 15:34:49 -08002521 sinfo.typing_noise_detected =
2522 (send_ == SEND_NOTHING ? false : stats.typing_noise_detected);
wu@webrtc.org9dba5252013-08-05 20:36:57 +00002523 info->senders.push_back(sinfo);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002524 }
2525
solenberg85a04962015-10-27 03:35:21 -07002526 // Get SSRC and stats for each receiver.
2527 RTC_DCHECK(info->receivers.size() == 0);
solenberg7add0582015-11-20 09:59:34 -08002528 for (const auto& stream : recv_streams_) {
Fredrik Solenberg4f4ec0a2015-10-22 10:49:27 +02002529 webrtc::AudioReceiveStream::Stats stats = stream.second->GetStats();
2530 VoiceReceiverInfo rinfo;
2531 rinfo.add_ssrc(stats.remote_ssrc);
2532 rinfo.bytes_rcvd = stats.bytes_rcvd;
2533 rinfo.packets_rcvd = stats.packets_rcvd;
2534 rinfo.packets_lost = stats.packets_lost;
2535 rinfo.fraction_lost = stats.fraction_lost;
2536 rinfo.codec_name = stats.codec_name;
2537 rinfo.ext_seqnum = stats.ext_seqnum;
2538 rinfo.jitter_ms = stats.jitter_ms;
2539 rinfo.jitter_buffer_ms = stats.jitter_buffer_ms;
2540 rinfo.jitter_buffer_preferred_ms = stats.jitter_buffer_preferred_ms;
2541 rinfo.delay_estimate_ms = stats.delay_estimate_ms;
2542 rinfo.audio_level = stats.audio_level;
2543 rinfo.expand_rate = stats.expand_rate;
2544 rinfo.speech_expand_rate = stats.speech_expand_rate;
2545 rinfo.secondary_decoded_rate = stats.secondary_decoded_rate;
2546 rinfo.accelerate_rate = stats.accelerate_rate;
2547 rinfo.preemptive_expand_rate = stats.preemptive_expand_rate;
2548 rinfo.decoding_calls_to_silence_generator =
2549 stats.decoding_calls_to_silence_generator;
2550 rinfo.decoding_calls_to_neteq = stats.decoding_calls_to_neteq;
2551 rinfo.decoding_normal = stats.decoding_normal;
2552 rinfo.decoding_plc = stats.decoding_plc;
2553 rinfo.decoding_cng = stats.decoding_cng;
2554 rinfo.decoding_plc_cng = stats.decoding_plc_cng;
2555 rinfo.capture_start_ntp_time_ms = stats.capture_start_ntp_time_ms;
2556 info->receivers.push_back(rinfo);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002557 }
2558
2559 return true;
2560}
2561
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002562int WebRtcVoiceMediaChannel::GetOutputLevel(int channel) {
solenbergd97ec302015-10-07 01:40:33 -07002563 unsigned int ulevel = 0;
2564 int ret = engine()->voe()->volume()->GetSpeechOutputLevel(channel, ulevel);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002565 return (ret == 0) ? static_cast<int>(ulevel) : -1;
2566}
2567
Peter Boström0c4e06b2015-10-07 12:23:21 +02002568int WebRtcVoiceMediaChannel::GetReceiveChannelId(uint32_t ssrc) const {
solenberg566ef242015-11-06 15:34:49 -08002569 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
solenberg7add0582015-11-20 09:59:34 -08002570 const auto it = recv_streams_.find(ssrc);
2571 if (it != recv_streams_.end()) {
mallinath@webrtc.org67ee6b92014-02-03 16:57:16 +00002572 return it->second->channel();
solenberg8fb30c32015-10-13 03:06:58 -07002573 }
solenberg1ac56142015-10-13 03:58:19 -07002574 return -1;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002575}
2576
Peter Boström0c4e06b2015-10-07 12:23:21 +02002577int WebRtcVoiceMediaChannel::GetSendChannelId(uint32_t ssrc) const {
solenberg566ef242015-11-06 15:34:49 -08002578 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
solenbergc96df772015-10-21 13:01:53 -07002579 const auto it = send_streams_.find(ssrc);
2580 if (it != send_streams_.end()) {
mallinath@webrtc.org67ee6b92014-02-03 16:57:16 +00002581 return it->second->channel();
solenberg8fb30c32015-10-13 03:06:58 -07002582 }
wu@webrtc.org9dba5252013-08-05 20:36:57 +00002583 return -1;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002584}
2585
2586bool WebRtcVoiceMediaChannel::GetRedSendCodec(const AudioCodec& red_codec,
2587 const std::vector<AudioCodec>& all_codecs, webrtc::CodecInst* send_codec) {
2588 // Get the RED encodings from the parameter with no name. This may
2589 // change based on what is discussed on the Jingle list.
2590 // The encoding parameter is of the form "a/b"; we only support where
2591 // a == b. Verify this and parse out the value into red_pt.
2592 // If the parameter value is absent (as it will be until we wire up the
2593 // signaling of this message), use the second codec specified (i.e. the
2594 // one after "red") as the encoding parameter.
2595 int red_pt = -1;
2596 std::string red_params;
2597 CodecParameterMap::const_iterator it = red_codec.params.find("");
2598 if (it != red_codec.params.end()) {
2599 red_params = it->second;
2600 std::vector<std::string> red_pts;
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00002601 if (rtc::split(red_params, '/', &red_pts) != 2 ||
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002602 red_pts[0] != red_pts[1] ||
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00002603 !rtc::FromString(red_pts[0], &red_pt)) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002604 LOG(LS_WARNING) << "RED params " << red_params << " not supported.";
2605 return false;
2606 }
2607 } else if (red_codec.params.empty()) {
2608 LOG(LS_WARNING) << "RED params not present, using defaults";
2609 if (all_codecs.size() > 1) {
2610 red_pt = all_codecs[1].id;
2611 }
2612 }
2613
2614 // Try to find red_pt in |codecs|.
Fredrik Solenbergaf9fb212015-08-26 10:45:53 +02002615 for (const AudioCodec& codec : all_codecs) {
2616 if (codec.id == red_pt) {
2617 // If we find the right codec, that will be the codec we pass to
2618 // SetSendCodec, with the desired payload type.
solenberg26c8c912015-11-27 04:00:25 -08002619 if (WebRtcVoiceEngine::ToCodecInst(codec, send_codec)) {
Fredrik Solenbergaf9fb212015-08-26 10:45:53 +02002620 return true;
2621 } else {
2622 break;
2623 }
2624 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002625 }
Fredrik Solenbergaf9fb212015-08-26 10:45:53 +02002626 LOG(LS_WARNING) << "RED params " << red_params << " are invalid.";
2627 return false;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002628}
2629
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002630bool WebRtcVoiceMediaChannel::SetPlayout(int channel, bool playout) {
2631 if (playout) {
2632 LOG(LS_INFO) << "Starting playout for channel #" << channel;
2633 if (engine()->voe()->base()->StartPlayout(channel) == -1) {
2634 LOG_RTCERR1(StartPlayout, channel);
2635 return false;
2636 }
2637 } else {
2638 LOG(LS_INFO) << "Stopping playout for channel #" << channel;
2639 engine()->voe()->base()->StopPlayout(channel);
2640 }
2641 return true;
2642}
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002643} // namespace cricket
2644
2645#endif // HAVE_WEBRTC_VOICE