blob: 46d780e3d9ecbcee7448b2bc70862d0ac8c41cb2 [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"
Tommif888bb52015-12-12 01:37:01 +010047#include "webrtc/audio/audio_sink.h"
tfarina5237aaf2015-11-10 23:44:30 -080048#include "webrtc/base/arraysize.h"
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +000049#include "webrtc/base/base64.h"
50#include "webrtc/base/byteorder.h"
51#include "webrtc/base/common.h"
52#include "webrtc/base/helpers.h"
53#include "webrtc/base/logging.h"
54#include "webrtc/base/stringencode.h"
55#include "webrtc/base/stringutils.h"
ivoc112a3d82015-10-16 02:22:18 -070056#include "webrtc/call/rtc_event_log.h"
mallinath@webrtc.orga27be8e2013-09-27 23:04:10 +000057#include "webrtc/common.h"
solenberg26c8c912015-11-27 04:00:25 -080058#include "webrtc/modules/audio_coding/acm2/rent_a_codec.h"
henrike@webrtc.org28e20752013-07-10 00:45:36 +000059#include "webrtc/modules/audio_processing/include/audio_processing.h"
Henrik Kjellander98f53512015-10-28 18:17:40 +010060#include "webrtc/system_wrappers/include/field_trial.h"
solenbergbd138382015-11-20 16:08:07 -080061#include "webrtc/system_wrappers/include/trace.h"
henrike@webrtc.org28e20752013-07-10 00:45:36 +000062
henrike@webrtc.org28e20752013-07-10 00:45:36 +000063namespace cricket {
solenbergd97ec302015-10-07 01:40:33 -070064namespace {
henrike@webrtc.org28e20752013-07-10 00:45:36 +000065
solenbergbd138382015-11-20 16:08:07 -080066const int kDefaultTraceFilter = webrtc::kTraceNone | webrtc::kTraceTerseInfo |
67 webrtc::kTraceWarning | webrtc::kTraceError |
68 webrtc::kTraceCritical;
69const int kElevatedTraceFilter = kDefaultTraceFilter | webrtc::kTraceStateInfo |
70 webrtc::kTraceInfo;
71
henrike@webrtc.org28e20752013-07-10 00:45:36 +000072// On Windows Vista and newer, Microsoft introduced the concept of "Default
73// Communications Device". This means that there are two types of default
74// devices (old Wave Audio style default and Default Communications Device).
75//
76// On Windows systems which only support Wave Audio style default, uses either
77// -1 or 0 to select the default device.
henrike@webrtc.org28e20752013-07-10 00:45:36 +000078#ifdef WIN32
solenbergd97ec302015-10-07 01:40:33 -070079const int kDefaultAudioDeviceId = -1;
henrike@webrtc.org28e20752013-07-10 00:45:36 +000080#else
solenbergd97ec302015-10-07 01:40:33 -070081const int kDefaultAudioDeviceId = 0;
henrike@webrtc.org28e20752013-07-10 00:45:36 +000082#endif
83
henrike@webrtc.org28e20752013-07-10 00:45:36 +000084// Parameter used for NACK.
85// This value is equivalent to 5 seconds of audio data at 20 ms per packet.
solenbergd97ec302015-10-07 01:40:33 -070086const int kNackMaxPackets = 250;
minyue@webrtc.org2dc6f312014-10-31 05:33:10 +000087
88// Codec parameters for Opus.
henrike@webrtc.org1e09a712013-07-26 19:17:59 +000089// draft-spittka-payload-rtp-opus-03
minyue@webrtc.org2dc6f312014-10-31 05:33:10 +000090
91// Recommended bitrates:
92// 8-12 kb/s for NB speech,
93// 16-20 kb/s for WB speech,
94// 28-40 kb/s for FB speech,
95// 48-64 kb/s for FB mono music, and
96// 64-128 kb/s for FB stereo music.
97// The current implementation applies the following values to mono signals,
98// and multiplies them by 2 for stereo.
solenbergd97ec302015-10-07 01:40:33 -070099const int kOpusBitrateNb = 12000;
100const int kOpusBitrateWb = 20000;
101const int kOpusBitrateFb = 32000;
minyue@webrtc.org2dc6f312014-10-31 05:33:10 +0000102
henrike@webrtc.org1e09a712013-07-26 19:17:59 +0000103// Opus bitrate should be in the range between 6000 and 510000.
solenbergd97ec302015-10-07 01:40:33 -0700104const int kOpusMinBitrate = 6000;
105const int kOpusMaxBitrate = 510000;
buildbot@webrtc.org5d639b32014-09-10 07:57:12 +0000106
wu@webrtc.orgde305012013-10-31 15:40:38 +0000107// Default audio dscp value.
108// See http://tools.ietf.org/html/rfc2474 for details.
109// See also http://tools.ietf.org/html/draft-jennings-rtcweb-qos-00
solenbergd97ec302015-10-07 01:40:33 -0700110const rtc::DiffServCodePoint kAudioDscpValue = rtc::DSCP_EF;
henrike@webrtc.org1e09a712013-07-26 19:17:59 +0000111
sergeyu@chromium.orga59696b2013-09-13 23:48:58 +0000112// Ensure we open the file in a writeable path on ChromeOS and Android. This
113// workaround can be removed when it's possible to specify a filename for audio
114// option based AEC dumps.
henrike@webrtc.org1e09a712013-07-26 19:17:59 +0000115//
116// TODO(grunell): Use a string in the options instead of hardcoding it here
117// and let the embedder choose the filename (crbug.com/264223).
118//
sergeyu@chromium.orga59696b2013-09-13 23:48:58 +0000119// NOTE(ajm): Don't use hardcoded paths on platforms not explicitly specified
120// below.
121#if defined(CHROMEOS)
solenbergd97ec302015-10-07 01:40:33 -0700122const char kAecDumpByAudioOptionFilename[] = "/tmp/audio.aecdump";
sergeyu@chromium.orga59696b2013-09-13 23:48:58 +0000123#elif defined(ANDROID)
solenbergd97ec302015-10-07 01:40:33 -0700124const char kAecDumpByAudioOptionFilename[] = "/sdcard/audio.aecdump";
henrike@webrtc.org1e09a712013-07-26 19:17:59 +0000125#else
solenbergd97ec302015-10-07 01:40:33 -0700126const char kAecDumpByAudioOptionFilename[] = "audio.aecdump";
henrike@webrtc.org1e09a712013-07-26 19:17:59 +0000127#endif
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000128
Fredrik Solenbergb5727682015-12-04 15:22:19 +0100129// Constants from voice_engine_defines.h.
130const int kMinTelephoneEventCode = 0; // RFC4733 (Section 2.3.1)
131const int kMaxTelephoneEventCode = 255;
132const int kMinTelephoneEventDuration = 100;
133const int kMaxTelephoneEventDuration = 60000; // Actual limit is 2^16
134
deadbeef884f5852016-01-15 09:20:04 -0800135class ProxySink : public webrtc::AudioSinkInterface {
136 public:
137 ProxySink(AudioSinkInterface* sink) : sink_(sink) { RTC_DCHECK(sink); }
138
139 void OnData(const Data& audio) override { sink_->OnData(audio); }
140
141 private:
142 webrtc::AudioSinkInterface* sink_;
143};
144
solenberg0b675462015-10-09 01:37:09 -0700145bool ValidateStreamParams(const StreamParams& sp) {
146 if (sp.ssrcs.empty()) {
147 LOG(LS_ERROR) << "No SSRCs in stream parameters: " << sp.ToString();
148 return false;
149 }
150 if (sp.ssrcs.size() > 1) {
151 LOG(LS_ERROR) << "Multiple SSRCs in stream parameters: " << sp.ToString();
152 return false;
153 }
154 return true;
155}
156
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000157// Dumps an AudioCodec in RFC 2327-ish format.
solenbergd97ec302015-10-07 01:40:33 -0700158std::string ToString(const AudioCodec& codec) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000159 std::stringstream ss;
160 ss << codec.name << "/" << codec.clockrate << "/" << codec.channels
161 << " (" << codec.id << ")";
162 return ss.str();
163}
Minyue Li7100dcd2015-03-27 05:05:59 +0100164
solenbergd97ec302015-10-07 01:40:33 -0700165std::string ToString(const webrtc::CodecInst& codec) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000166 std::stringstream ss;
167 ss << codec.plname << "/" << codec.plfreq << "/" << codec.channels
168 << " (" << codec.pltype << ")";
169 return ss.str();
170}
171
solenbergd97ec302015-10-07 01:40:33 -0700172bool IsCodec(const AudioCodec& codec, const char* ref_name) {
Minyue Li7100dcd2015-03-27 05:05:59 +0100173 return (_stricmp(codec.name.c_str(), ref_name) == 0);
174}
175
solenbergd97ec302015-10-07 01:40:33 -0700176bool IsCodec(const webrtc::CodecInst& codec, const char* ref_name) {
Minyue Li7100dcd2015-03-27 05:05:59 +0100177 return (_stricmp(codec.plname, ref_name) == 0);
178}
179
solenbergd97ec302015-10-07 01:40:33 -0700180bool FindCodec(const std::vector<AudioCodec>& codecs,
solenberg26c8c912015-11-27 04:00:25 -0800181 const AudioCodec& codec,
182 AudioCodec* found_codec) {
Fredrik Solenbergaf9fb212015-08-26 10:45:53 +0200183 for (const AudioCodec& c : codecs) {
184 if (c.Matches(codec)) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000185 if (found_codec != NULL) {
Fredrik Solenbergaf9fb212015-08-26 10:45:53 +0200186 *found_codec = c;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000187 }
188 return true;
189 }
190 }
191 return false;
192}
wu@webrtc.org1d1ffc92013-10-16 18:12:02 +0000193
solenberg0b675462015-10-09 01:37:09 -0700194bool VerifyUniquePayloadTypes(const std::vector<AudioCodec>& codecs) {
195 if (codecs.empty()) {
196 return true;
197 }
198 std::vector<int> payload_types;
199 for (const AudioCodec& codec : codecs) {
200 payload_types.push_back(codec.id);
201 }
202 std::sort(payload_types.begin(), payload_types.end());
203 auto it = std::unique(payload_types.begin(), payload_types.end());
204 return it == payload_types.end();
205}
206
Minyue Li7100dcd2015-03-27 05:05:59 +0100207// Return true if codec.params[feature] == "1", false otherwise.
solenberg26c8c912015-11-27 04:00:25 -0800208bool IsCodecFeatureEnabled(const AudioCodec& codec, const char* feature) {
Minyue Li7100dcd2015-03-27 05:05:59 +0100209 int value;
210 return codec.GetParam(feature, &value) && value == 1;
211}
212
213// Use params[kCodecParamMaxAverageBitrate] if it is defined, use codec.bitrate
214// otherwise. If the value (either from params or codec.bitrate) <=0, use the
215// default configuration. If the value is beyond feasible bit rate of Opus,
216// clamp it. Returns the Opus bit rate for operation.
solenbergd97ec302015-10-07 01:40:33 -0700217int GetOpusBitrate(const AudioCodec& codec, int max_playback_rate) {
Minyue Li7100dcd2015-03-27 05:05:59 +0100218 int bitrate = 0;
219 bool use_param = true;
220 if (!codec.GetParam(kCodecParamMaxAverageBitrate, &bitrate)) {
221 bitrate = codec.bitrate;
222 use_param = false;
223 }
224 if (bitrate <= 0) {
225 if (max_playback_rate <= 8000) {
226 bitrate = kOpusBitrateNb;
227 } else if (max_playback_rate <= 16000) {
228 bitrate = kOpusBitrateWb;
229 } else {
230 bitrate = kOpusBitrateFb;
231 }
232
233 if (IsCodecFeatureEnabled(codec, kCodecParamStereo)) {
234 bitrate *= 2;
235 }
236 } else if (bitrate < kOpusMinBitrate || bitrate > kOpusMaxBitrate) {
237 bitrate = (bitrate < kOpusMinBitrate) ? kOpusMinBitrate : kOpusMaxBitrate;
238 std::string rate_source =
239 use_param ? "Codec parameter \"maxaveragebitrate\"" :
240 "Supplied Opus bitrate";
241 LOG(LS_WARNING) << rate_source
242 << " is invalid and is replaced by: "
243 << bitrate;
244 }
245 return bitrate;
246}
247
248// Returns kOpusDefaultPlaybackRate if params[kCodecParamMaxPlaybackRate] is not
249// defined. Returns the value of params[kCodecParamMaxPlaybackRate] otherwise.
solenbergd97ec302015-10-07 01:40:33 -0700250int GetOpusMaxPlaybackRate(const AudioCodec& codec) {
Minyue Li7100dcd2015-03-27 05:05:59 +0100251 int value;
252 if (codec.GetParam(kCodecParamMaxPlaybackRate, &value)) {
253 return value;
254 }
255 return kOpusDefaultMaxPlaybackRate;
256}
257
solenbergd97ec302015-10-07 01:40:33 -0700258void GetOpusConfig(const AudioCodec& codec, webrtc::CodecInst* voe_codec,
Minyue Li7100dcd2015-03-27 05:05:59 +0100259 bool* enable_codec_fec, int* max_playback_rate,
260 bool* enable_codec_dtx) {
261 *enable_codec_fec = IsCodecFeatureEnabled(codec, kCodecParamUseInbandFec);
262 *enable_codec_dtx = IsCodecFeatureEnabled(codec, kCodecParamUseDtx);
263 *max_playback_rate = GetOpusMaxPlaybackRate(codec);
264
265 // If OPUS, change what we send according to the "stereo" codec
266 // parameter, and not the "channels" parameter. We set
267 // voe_codec.channels to 2 if "stereo=1" and 1 otherwise. If
268 // the bitrate is not specified, i.e. is <= zero, we set it to the
269 // appropriate default value for mono or stereo Opus.
270
271 voe_codec->channels = IsCodecFeatureEnabled(codec, kCodecParamStereo) ? 2 : 1;
272 voe_codec->rate = GetOpusBitrate(codec, *max_playback_rate);
273}
274
solenberg566ef242015-11-06 15:34:49 -0800275webrtc::AudioState::Config MakeAudioStateConfig(VoEWrapper* voe_wrapper) {
276 webrtc::AudioState::Config config;
277 config.voice_engine = voe_wrapper->engine();
278 return config;
279}
280
solenberg26c8c912015-11-27 04:00:25 -0800281class WebRtcVoiceCodecs final {
282 public:
283 // TODO(solenberg): Do this filtering once off-line, add a simple AudioCodec
284 // list and add a test which verifies VoE supports the listed codecs.
285 static std::vector<AudioCodec> SupportedCodecs() {
286 LOG(LS_INFO) << "WebRtc VoiceEngine codecs:";
287 std::vector<AudioCodec> result;
288 for (webrtc::CodecInst voe_codec : webrtc::acm2::RentACodec::Database()) {
289 // Change the sample rate of G722 to 8000 to match SDP.
290 MaybeFixupG722(&voe_codec, 8000);
buildbot@webrtc.org13d67762014-05-02 17:33:29 +0000291 // Skip uncompressed formats.
Minyue Li7100dcd2015-03-27 05:05:59 +0100292 if (IsCodec(voe_codec, kL16CodecName)) {
buildbot@webrtc.org13d67762014-05-02 17:33:29 +0000293 continue;
294 }
295
296 const CodecPref* pref = NULL;
tfarina5237aaf2015-11-10 23:44:30 -0800297 for (size_t j = 0; j < arraysize(kCodecPrefs); ++j) {
Minyue Li7100dcd2015-03-27 05:05:59 +0100298 if (IsCodec(voe_codec, kCodecPrefs[j].name) &&
buildbot@webrtc.org13d67762014-05-02 17:33:29 +0000299 kCodecPrefs[j].clockrate == voe_codec.plfreq &&
300 kCodecPrefs[j].channels == voe_codec.channels) {
301 pref = &kCodecPrefs[j];
302 break;
303 }
304 }
305
306 if (pref) {
307 // Use the payload type that we've configured in our pref table;
308 // use the offset in our pref table to determine the sort order.
tfarina5237aaf2015-11-10 23:44:30 -0800309 AudioCodec codec(
310 pref->payload_type, voe_codec.plname, voe_codec.plfreq,
311 voe_codec.rate, voe_codec.channels,
312 static_cast<int>(arraysize(kCodecPrefs)) - (pref - kCodecPrefs));
buildbot@webrtc.org13d67762014-05-02 17:33:29 +0000313 LOG(LS_INFO) << ToString(codec);
Minyue Li7100dcd2015-03-27 05:05:59 +0100314 if (IsCodec(codec, kIsacCodecName)) {
minyue@webrtc.org26236952014-10-29 02:27:08 +0000315 // Indicate auto-bitrate in signaling.
buildbot@webrtc.org13d67762014-05-02 17:33:29 +0000316 codec.bitrate = 0;
317 }
Minyue Li7100dcd2015-03-27 05:05:59 +0100318 if (IsCodec(codec, kOpusCodecName)) {
buildbot@webrtc.org13d67762014-05-02 17:33:29 +0000319 // Only add fmtp parameters that differ from the spec.
320 if (kPreferredMinPTime != kOpusDefaultMinPTime) {
321 codec.params[kCodecParamMinPTime] =
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000322 rtc::ToString(kPreferredMinPTime);
buildbot@webrtc.org13d67762014-05-02 17:33:29 +0000323 }
324 if (kPreferredMaxPTime != kOpusDefaultMaxPTime) {
325 codec.params[kCodecParamMaxPTime] =
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000326 rtc::ToString(kPreferredMaxPTime);
buildbot@webrtc.org13d67762014-05-02 17:33:29 +0000327 }
pkasting@chromium.orgd3245462015-02-23 21:28:22 +0000328 codec.SetParam(kCodecParamUseInbandFec, 1);
stefanba4c0e42016-02-04 04:12:24 -0800329 codec.AddFeedbackParam(
330 FeedbackParam(kRtcpFbParamTransportCc, kParamValueEmpty));
minyue@webrtc.org4ef22d12014-11-17 09:26:39 +0000331
332 // TODO(hellner): Add ptime, sprop-stereo, and stereo
buildbot@webrtc.org13d67762014-05-02 17:33:29 +0000333 // when they can be set to values other than the default.
334 }
solenberg26c8c912015-11-27 04:00:25 -0800335 result.push_back(codec);
buildbot@webrtc.org13d67762014-05-02 17:33:29 +0000336 } else {
337 LOG(LS_WARNING) << "Unexpected codec: " << ToString(voe_codec);
338 }
339 }
solenberg26c8c912015-11-27 04:00:25 -0800340 // Make sure they are in local preference order.
341 std::sort(result.begin(), result.end(), &AudioCodec::Preferable);
342 return result;
buildbot@webrtc.org13d67762014-05-02 17:33:29 +0000343 }
buildbot@webrtc.org13d67762014-05-02 17:33:29 +0000344
solenberg26c8c912015-11-27 04:00:25 -0800345 static bool ToCodecInst(const AudioCodec& in,
346 webrtc::CodecInst* out) {
347 for (webrtc::CodecInst voe_codec : webrtc::acm2::RentACodec::Database()) {
348 // Change the sample rate of G722 to 8000 to match SDP.
349 MaybeFixupG722(&voe_codec, 8000);
350 AudioCodec codec(voe_codec.pltype, voe_codec.plname, voe_codec.plfreq,
351 voe_codec.rate, voe_codec.channels, 0);
352 bool multi_rate = IsCodecMultiRate(voe_codec);
353 // Allow arbitrary rates for ISAC to be specified.
354 if (multi_rate) {
355 // Set codec.bitrate to 0 so the check for codec.Matches() passes.
356 codec.bitrate = 0;
357 }
358 if (codec.Matches(in)) {
359 if (out) {
360 // Fixup the payload type.
361 voe_codec.pltype = in.id;
362
363 // Set bitrate if specified.
364 if (multi_rate && in.bitrate != 0) {
365 voe_codec.rate = in.bitrate;
366 }
367
368 // Reset G722 sample rate to 16000 to match WebRTC.
369 MaybeFixupG722(&voe_codec, 16000);
370
371 // Apply codec-specific settings.
372 if (IsCodec(codec, kIsacCodecName)) {
373 // If ISAC and an explicit bitrate is not specified,
374 // enable auto bitrate adjustment.
375 voe_codec.rate = (in.bitrate > 0) ? in.bitrate : -1;
376 }
377 *out = voe_codec;
378 }
379 return true;
380 }
381 }
henrik.lundin@webrtc.org8038d422014-11-11 08:38:24 +0000382 return false;
henrik.lundin@webrtc.orgf85dbce2014-11-07 12:25:00 +0000383 }
solenberg26c8c912015-11-27 04:00:25 -0800384
385 static bool IsCodecMultiRate(const webrtc::CodecInst& codec) {
386 for (size_t i = 0; i < arraysize(kCodecPrefs); ++i) {
387 if (IsCodec(codec, kCodecPrefs[i].name) &&
388 kCodecPrefs[i].clockrate == codec.plfreq) {
389 return kCodecPrefs[i].is_multi_rate;
390 }
391 }
392 return false;
393 }
394
395 // If the AudioCodec param kCodecParamPTime is set, then we will set it to
396 // codec pacsize if it's valid, or we will pick the next smallest value we
397 // support.
398 // TODO(Brave): Query supported packet sizes from ACM when the API is ready.
399 static bool SetPTimeAsPacketSize(webrtc::CodecInst* codec, int ptime_ms) {
400 for (const CodecPref& codec_pref : kCodecPrefs) {
401 if ((IsCodec(*codec, codec_pref.name) &&
402 codec_pref.clockrate == codec->plfreq) ||
403 IsCodec(*codec, kG722CodecName)) {
404 int packet_size_ms = SelectPacketSize(codec_pref, ptime_ms);
405 if (packet_size_ms) {
406 // Convert unit from milli-seconds to samples.
407 codec->pacsize = (codec->plfreq / 1000) * packet_size_ms;
408 return true;
409 }
410 }
411 }
412 return false;
413 }
414
stefanba4c0e42016-02-04 04:12:24 -0800415 static const AudioCodec* GetPreferredCodec(
416 const std::vector<AudioCodec>& codecs,
417 webrtc::CodecInst* voe_codec,
418 int* red_payload_type) {
419 RTC_DCHECK(voe_codec);
420 RTC_DCHECK(red_payload_type);
421 // Select the preferred send codec (the first non-telephone-event/CN codec).
422 for (const AudioCodec& codec : codecs) {
423 *red_payload_type = -1;
424 if (IsCodec(codec, kDtmfCodecName) || IsCodec(codec, kCnCodecName)) {
425 // Skip telephone-event/CN codec, which will be handled later.
426 continue;
427 }
428
429 // We'll use the first codec in the list to actually send audio data.
430 // Be sure to use the payload type requested by the remote side.
431 // "red", for RED audio, is a special case where the actual codec to be
432 // used is specified in params.
433 const AudioCodec* found_codec = &codec;
434 if (IsCodec(*found_codec, kRedCodecName)) {
435 // Parse out the RED parameters. If we fail, just ignore RED;
436 // we don't support all possible params/usage scenarios.
437 *red_payload_type = codec.id;
438 found_codec = GetRedSendCodec(*found_codec, codecs);
439 if (!found_codec) {
440 continue;
441 }
442 }
443 // Ignore codecs we don't know about. The negotiation step should prevent
444 // this, but double-check to be sure.
445 if (!ToCodecInst(*found_codec, voe_codec)) {
446 LOG(LS_WARNING) << "Unknown codec " << ToString(*found_codec);
447 continue;
448 }
449 return found_codec;
450 }
451 return nullptr;
452 }
453
solenberg26c8c912015-11-27 04:00:25 -0800454 private:
455 static const int kMaxNumPacketSize = 6;
456 struct CodecPref {
457 const char* name;
458 int clockrate;
Peter Kasting69558702016-01-12 16:26:35 -0800459 size_t channels;
solenberg26c8c912015-11-27 04:00:25 -0800460 int payload_type;
461 bool is_multi_rate;
462 int packet_sizes_ms[kMaxNumPacketSize];
463 };
464 // Note: keep the supported packet sizes in ascending order.
465 static const CodecPref kCodecPrefs[12];
466
467 static int SelectPacketSize(const CodecPref& codec_pref, int ptime_ms) {
468 int selected_packet_size_ms = codec_pref.packet_sizes_ms[0];
469 for (int packet_size_ms : codec_pref.packet_sizes_ms) {
470 if (packet_size_ms && packet_size_ms <= ptime_ms) {
471 selected_packet_size_ms = packet_size_ms;
472 }
473 }
474 return selected_packet_size_ms;
475 }
476
477 // Changes RTP timestamp rate of G722. This is due to the "bug" in the RFC
478 // which says that G722 should be advertised as 8 kHz although it is a 16 kHz
479 // codec.
480 static void MaybeFixupG722(webrtc::CodecInst* voe_codec, int new_plfreq) {
481 if (IsCodec(*voe_codec, kG722CodecName)) {
482 // If the ASSERT triggers, the codec definition in WebRTC VoiceEngine
483 // has changed, and this special case is no longer needed.
484 RTC_DCHECK(voe_codec->plfreq != new_plfreq);
485 voe_codec->plfreq = new_plfreq;
486 }
487 }
stefanba4c0e42016-02-04 04:12:24 -0800488
489 static const AudioCodec* GetRedSendCodec(
490 const AudioCodec& red_codec,
491 const std::vector<AudioCodec>& all_codecs) {
492 // Get the RED encodings from the parameter with no name. This may
493 // change based on what is discussed on the Jingle list.
494 // The encoding parameter is of the form "a/b"; we only support where
495 // a == b. Verify this and parse out the value into red_pt.
496 // If the parameter value is absent (as it will be until we wire up the
497 // signaling of this message), use the second codec specified (i.e. the
498 // one after "red") as the encoding parameter.
499 int red_pt = -1;
500 std::string red_params;
501 CodecParameterMap::const_iterator it = red_codec.params.find("");
502 if (it != red_codec.params.end()) {
503 red_params = it->second;
504 std::vector<std::string> red_pts;
505 if (rtc::split(red_params, '/', &red_pts) != 2 ||
506 red_pts[0] != red_pts[1] || !rtc::FromString(red_pts[0], &red_pt)) {
507 LOG(LS_WARNING) << "RED params " << red_params << " not supported.";
508 return nullptr;
509 }
510 } else if (red_codec.params.empty()) {
511 LOG(LS_WARNING) << "RED params not present, using defaults";
512 if (all_codecs.size() > 1) {
513 red_pt = all_codecs[1].id;
514 }
515 }
516
517 // Try to find red_pt in |codecs|.
518 for (const AudioCodec& codec : all_codecs) {
519 if (codec.id == red_pt) {
520 return &codec;
521 }
522 }
523 LOG(LS_WARNING) << "RED params " << red_params << " are invalid.";
524 return nullptr;
525 }
solenberg26c8c912015-11-27 04:00:25 -0800526};
527
528const WebRtcVoiceCodecs::CodecPref WebRtcVoiceCodecs::kCodecPrefs[12] = {
529 { kOpusCodecName, 48000, 2, 111, true, { 10, 20, 40, 60 } },
530 { kIsacCodecName, 16000, 1, 103, true, { 30, 60 } },
531 { kIsacCodecName, 32000, 1, 104, true, { 30 } },
532 // G722 should be advertised as 8000 Hz because of the RFC "bug".
533 { kG722CodecName, 8000, 1, 9, false, { 10, 20, 30, 40, 50, 60 } },
534 { kIlbcCodecName, 8000, 1, 102, false, { 20, 30, 40, 60 } },
535 { kPcmuCodecName, 8000, 1, 0, false, { 10, 20, 30, 40, 50, 60 } },
536 { kPcmaCodecName, 8000, 1, 8, false, { 10, 20, 30, 40, 50, 60 } },
537 { kCnCodecName, 32000, 1, 106, false, { } },
538 { kCnCodecName, 16000, 1, 105, false, { } },
539 { kCnCodecName, 8000, 1, 13, false, { } },
540 { kRedCodecName, 8000, 1, 127, false, { } },
541 { kDtmfCodecName, 8000, 1, 126, false, { } },
542};
543} // namespace {
544
545bool WebRtcVoiceEngine::ToCodecInst(const AudioCodec& in,
546 webrtc::CodecInst* out) {
547 return WebRtcVoiceCodecs::ToCodecInst(in, out);
548}
549
550WebRtcVoiceEngine::WebRtcVoiceEngine()
551 : voe_wrapper_(new VoEWrapper()),
552 audio_state_(webrtc::AudioState::Create(MakeAudioStateConfig(voe()))) {
553 Construct();
554}
555
556WebRtcVoiceEngine::WebRtcVoiceEngine(VoEWrapper* voe_wrapper)
557 : voe_wrapper_(voe_wrapper) {
558 Construct();
559}
560
561void WebRtcVoiceEngine::Construct() {
562 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
563 LOG(LS_VERBOSE) << "WebRtcVoiceEngine::WebRtcVoiceEngine";
564
565 signal_thread_checker_.DetachFromThread();
566 std::memset(&default_agc_config_, 0, sizeof(default_agc_config_));
solenberg246b8172015-12-08 09:50:23 -0800567 voe_config_.Set<webrtc::VoicePacing>(new webrtc::VoicePacing(true));
solenberg26c8c912015-11-27 04:00:25 -0800568
569 webrtc::Trace::set_level_filter(kDefaultTraceFilter);
570 webrtc::Trace::SetTraceCallback(this);
571
572 // Load our audio codec list.
573 codecs_ = WebRtcVoiceCodecs::SupportedCodecs();
henrik.lundin@webrtc.orgf85dbce2014-11-07 12:25:00 +0000574}
575
buildbot@webrtc.org13d67762014-05-02 17:33:29 +0000576WebRtcVoiceEngine::~WebRtcVoiceEngine() {
solenberg566ef242015-11-06 15:34:49 -0800577 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
buildbot@webrtc.org13d67762014-05-02 17:33:29 +0000578 LOG(LS_VERBOSE) << "WebRtcVoiceEngine::~WebRtcVoiceEngine";
buildbot@webrtc.org13d67762014-05-02 17:33:29 +0000579 if (adm_) {
580 voe_wrapper_.reset();
581 adm_->Release();
582 adm_ = NULL;
583 }
solenbergbd138382015-11-20 16:08:07 -0800584 webrtc::Trace::SetTraceCallback(nullptr);
buildbot@webrtc.org13d67762014-05-02 17:33:29 +0000585}
586
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000587bool WebRtcVoiceEngine::Init(rtc::Thread* worker_thread) {
solenberg566ef242015-11-06 15:34:49 -0800588 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
henrikg91d6ede2015-09-17 00:24:34 -0700589 RTC_DCHECK(worker_thread == rtc::Thread::Current());
buildbot@webrtc.org13d67762014-05-02 17:33:29 +0000590 LOG(LS_INFO) << "WebRtcVoiceEngine::Init";
591 bool res = InitInternal();
592 if (res) {
593 LOG(LS_INFO) << "WebRtcVoiceEngine::Init Done!";
594 } else {
595 LOG(LS_ERROR) << "WebRtcVoiceEngine::Init failed";
596 Terminate();
597 }
598 return res;
599}
600
601bool WebRtcVoiceEngine::InitInternal() {
solenberg566ef242015-11-06 15:34:49 -0800602 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
buildbot@webrtc.org13d67762014-05-02 17:33:29 +0000603 // Temporarily turn logging level up for the Init call
solenbergbd138382015-11-20 16:08:07 -0800604 webrtc::Trace::set_level_filter(kElevatedTraceFilter);
solenberg2515af22015-12-02 06:19:36 -0800605 LOG(LS_INFO) << webrtc::VoiceEngine::GetVersionString();
buildbot@webrtc.org13d67762014-05-02 17:33:29 +0000606 if (voe_wrapper_->base()->Init(adm_) == -1) {
607 LOG_RTCERR0_EX(Init, voe_wrapper_->error());
buildbot@webrtc.org13d67762014-05-02 17:33:29 +0000608 return false;
609 }
solenbergbd138382015-11-20 16:08:07 -0800610 webrtc::Trace::set_level_filter(kDefaultTraceFilter);
buildbot@webrtc.org13d67762014-05-02 17:33:29 +0000611
buildbot@webrtc.org13d67762014-05-02 17:33:29 +0000612 // Save the default AGC configuration settings. This must happen before
solenberg246b8172015-12-08 09:50:23 -0800613 // calling ApplyOptions or the default will be overwritten.
buildbot@webrtc.org13d67762014-05-02 17:33:29 +0000614 if (voe_wrapper_->processing()->GetAgcConfig(default_agc_config_) == -1) {
615 LOG_RTCERR0(GetAgcConfig);
616 return false;
617 }
618
solenberg0f7d2932016-01-15 01:40:39 -0800619 // Set default engine options.
620 {
621 AudioOptions options;
622 options.echo_cancellation = rtc::Optional<bool>(true);
623 options.auto_gain_control = rtc::Optional<bool>(true);
624 options.noise_suppression = rtc::Optional<bool>(true);
625 options.highpass_filter = rtc::Optional<bool>(true);
626 options.stereo_swapping = rtc::Optional<bool>(false);
627 options.audio_jitter_buffer_max_packets = rtc::Optional<int>(50);
628 options.audio_jitter_buffer_fast_accelerate = rtc::Optional<bool>(false);
629 options.typing_detection = rtc::Optional<bool>(true);
630 options.adjust_agc_delta = rtc::Optional<int>(0);
631 options.experimental_agc = rtc::Optional<bool>(false);
632 options.extended_filter_aec = rtc::Optional<bool>(false);
633 options.delay_agnostic_aec = rtc::Optional<bool>(false);
634 options.experimental_ns = rtc::Optional<bool>(false);
635 options.aec_dump = rtc::Optional<bool>(false);
636 if (!ApplyOptions(options)) {
637 return false;
638 }
639 }
640
buildbot@webrtc.org13d67762014-05-02 17:33:29 +0000641 // Print our codec list again for the call diagnostic log
642 LOG(LS_INFO) << "WebRtc VoiceEngine codecs:";
Fredrik Solenbergaf9fb212015-08-26 10:45:53 +0200643 for (const AudioCodec& codec : codecs_) {
644 LOG(LS_INFO) << ToString(codec);
buildbot@webrtc.org13d67762014-05-02 17:33:29 +0000645 }
646
solenberg246b8172015-12-08 09:50:23 -0800647 SetDefaultDevices();
648
buildbot@webrtc.org13d67762014-05-02 17:33:29 +0000649 initialized_ = true;
650 return true;
651}
652
buildbot@webrtc.org13d67762014-05-02 17:33:29 +0000653void WebRtcVoiceEngine::Terminate() {
solenberg566ef242015-11-06 15:34:49 -0800654 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
buildbot@webrtc.org13d67762014-05-02 17:33:29 +0000655 LOG(LS_INFO) << "WebRtcVoiceEngine::Terminate";
656 initialized_ = false;
657
658 StopAecDump();
659
buildbot@webrtc.org13d67762014-05-02 17:33:29 +0000660 voe_wrapper_->base()->Terminate();
buildbot@webrtc.org13d67762014-05-02 17:33:29 +0000661}
662
solenberg566ef242015-11-06 15:34:49 -0800663rtc::scoped_refptr<webrtc::AudioState>
664 WebRtcVoiceEngine::GetAudioState() const {
665 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
666 return audio_state_;
667}
668
Fredrik Solenberg709ed672015-09-15 12:26:33 +0200669VoiceMediaChannel* WebRtcVoiceEngine::CreateChannel(webrtc::Call* call,
Jelena Marusicc28a8962015-05-29 15:05:44 +0200670 const AudioOptions& options) {
solenberg566ef242015-11-06 15:34:49 -0800671 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
solenberg0a617e22015-10-20 15:49:38 -0700672 return new WebRtcVoiceMediaChannel(this, options, call);
buildbot@webrtc.org13d67762014-05-02 17:33:29 +0000673}
674
buildbot@webrtc.org13d67762014-05-02 17:33:29 +0000675bool WebRtcVoiceEngine::ApplyOptions(const AudioOptions& options_in) {
solenberg566ef242015-11-06 15:34:49 -0800676 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
henrikac14f5ff2015-09-23 14:08:33 +0200677 LOG(LS_INFO) << "ApplyOptions: " << options_in.ToString();
solenberg0f7d2932016-01-15 01:40:39 -0800678 AudioOptions options = options_in; // The options are modified below.
solenberg246b8172015-12-08 09:50:23 -0800679
buildbot@webrtc.org13d67762014-05-02 17:33:29 +0000680 // kEcConference is AEC with high suppression.
681 webrtc::EcModes ec_mode = webrtc::kEcConference;
682 webrtc::AecmModes aecm_mode = webrtc::kAecmSpeakerphone;
683 webrtc::AgcModes agc_mode = webrtc::kAgcAdaptiveAnalog;
684 webrtc::NsModes ns_mode = webrtc::kNsHighSuppression;
kwiberg102c6a62015-10-30 02:47:38 -0700685 if (options.aecm_generate_comfort_noise) {
buildbot@webrtc.org13d67762014-05-02 17:33:29 +0000686 LOG(LS_VERBOSE) << "Comfort noise explicitly set to "
kwiberg102c6a62015-10-30 02:47:38 -0700687 << *options.aecm_generate_comfort_noise
688 << " (default is false).";
buildbot@webrtc.org13d67762014-05-02 17:33:29 +0000689 }
690
kjellanderfcfc8042016-01-14 11:01:09 -0800691#if defined(WEBRTC_IOS)
buildbot@webrtc.org13d67762014-05-02 17:33:29 +0000692 // On iOS, VPIO provides built-in EC and AGC.
Karl Wibergbe579832015-11-10 22:34:18 +0100693 options.echo_cancellation = rtc::Optional<bool>(false);
694 options.auto_gain_control = rtc::Optional<bool>(false);
henrika86d907c2015-09-07 16:09:50 +0200695 LOG(LS_INFO) << "Always disable AEC and AGC on iOS. Use built-in instead.";
buildbot@webrtc.org13d67762014-05-02 17:33:29 +0000696#elif defined(ANDROID)
697 ec_mode = webrtc::kEcAecm;
698#endif
699
kjellanderfcfc8042016-01-14 11:01:09 -0800700#if defined(WEBRTC_IOS) || defined(ANDROID)
buildbot@webrtc.org13d67762014-05-02 17:33:29 +0000701 // Set the AGC mode for iOS as well despite disabling it above, to avoid
702 // unsupported configuration errors from webrtc.
703 agc_mode = webrtc::kAgcFixedDigital;
Karl Wibergbe579832015-11-10 22:34:18 +0100704 options.typing_detection = rtc::Optional<bool>(false);
705 options.experimental_agc = rtc::Optional<bool>(false);
706 options.extended_filter_aec = rtc::Optional<bool>(false);
707 options.experimental_ns = rtc::Optional<bool>(false);
buildbot@webrtc.org13d67762014-05-02 17:33:29 +0000708#endif
709
Bjorn Volckerbf395c12015-03-25 22:45:56 +0100710 // Delay Agnostic AEC automatically turns on EC if not set except on iOS
711 // where the feature is not supported.
712 bool use_delay_agnostic_aec = false;
kjellanderfcfc8042016-01-14 11:01:09 -0800713#if !defined(WEBRTC_IOS)
kwiberg102c6a62015-10-30 02:47:38 -0700714 if (options.delay_agnostic_aec) {
715 use_delay_agnostic_aec = *options.delay_agnostic_aec;
Bjorn Volckerbf395c12015-03-25 22:45:56 +0100716 if (use_delay_agnostic_aec) {
Karl Wibergbe579832015-11-10 22:34:18 +0100717 options.echo_cancellation = rtc::Optional<bool>(true);
718 options.extended_filter_aec = rtc::Optional<bool>(true);
Bjorn Volckerbf395c12015-03-25 22:45:56 +0100719 ec_mode = webrtc::kEcConference;
720 }
721 }
722#endif
723
buildbot@webrtc.org13d67762014-05-02 17:33:29 +0000724 webrtc::VoEAudioProcessing* voep = voe_wrapper_->processing();
725
kwiberg102c6a62015-10-30 02:47:38 -0700726 if (options.echo_cancellation) {
henrika@webrtc.orga954c072014-12-09 16:22:09 +0000727 // Check if platform supports built-in EC. Currently only supported on
728 // Android and in combination with Java based audio layer.
729 // TODO(henrika): investigate possibility to support built-in EC also
730 // in combination with Open SL ES audio.
731 const bool built_in_aec = voe_wrapper_->hw()->BuiltInAECIsAvailable();
Bjorn Volcker73f72102015-06-03 14:50:15 +0200732 if (built_in_aec) {
Bjorn Volckerccfc9392015-05-07 07:43:17 +0200733 // Built-in EC exists on this device and use_delay_agnostic_aec is not
734 // overriding it. Enable/Disable it according to the echo_cancellation
735 // audio option.
Bjorn Volcker73f72102015-06-03 14:50:15 +0200736 const bool enable_built_in_aec =
kwiberg102c6a62015-10-30 02:47:38 -0700737 *options.echo_cancellation && !use_delay_agnostic_aec;
Bjorn Volcker73f72102015-06-03 14:50:15 +0200738 if (voe_wrapper_->hw()->EnableBuiltInAEC(enable_built_in_aec) == 0 &&
739 enable_built_in_aec) {
Bjorn Volckerbf395c12015-03-25 22:45:56 +0100740 // Disable internal software EC if built-in EC is enabled,
henrika@webrtc.orga954c072014-12-09 16:22:09 +0000741 // i.e., replace the software EC with the built-in EC.
Karl Wibergbe579832015-11-10 22:34:18 +0100742 options.echo_cancellation = rtc::Optional<bool>(false);
henrika@webrtc.orga954c072014-12-09 16:22:09 +0000743 LOG(LS_INFO) << "Disabling EC since built-in EC will be used instead";
744 }
745 }
kwiberg102c6a62015-10-30 02:47:38 -0700746 if (voep->SetEcStatus(*options.echo_cancellation, ec_mode) == -1) {
747 LOG_RTCERR2(SetEcStatus, *options.echo_cancellation, ec_mode);
buildbot@webrtc.org13d67762014-05-02 17:33:29 +0000748 return false;
749 } else {
kwiberg102c6a62015-10-30 02:47:38 -0700750 LOG(LS_INFO) << "Echo control set to " << *options.echo_cancellation
henrika86d907c2015-09-07 16:09:50 +0200751 << " with mode " << ec_mode;
buildbot@webrtc.org13d67762014-05-02 17:33:29 +0000752 }
753#if !defined(ANDROID)
754 // TODO(ajm): Remove the error return on Android from webrtc.
kwiberg102c6a62015-10-30 02:47:38 -0700755 if (voep->SetEcMetricsStatus(*options.echo_cancellation) == -1) {
756 LOG_RTCERR1(SetEcMetricsStatus, *options.echo_cancellation);
buildbot@webrtc.org13d67762014-05-02 17:33:29 +0000757 return false;
758 }
759#endif
760 if (ec_mode == webrtc::kEcAecm) {
kwiberg102c6a62015-10-30 02:47:38 -0700761 bool cn = options.aecm_generate_comfort_noise.value_or(false);
762 if (voep->SetAecmMode(aecm_mode, cn) != 0) {
763 LOG_RTCERR2(SetAecmMode, aecm_mode, cn);
buildbot@webrtc.org13d67762014-05-02 17:33:29 +0000764 return false;
765 }
766 }
767 }
768
kwiberg102c6a62015-10-30 02:47:38 -0700769 if (options.auto_gain_control) {
henrikac14f5ff2015-09-23 14:08:33 +0200770 const bool built_in_agc = voe_wrapper_->hw()->BuiltInAGCIsAvailable();
771 if (built_in_agc) {
kwiberg102c6a62015-10-30 02:47:38 -0700772 if (voe_wrapper_->hw()->EnableBuiltInAGC(*options.auto_gain_control) ==
773 0 &&
774 *options.auto_gain_control) {
henrikac14f5ff2015-09-23 14:08:33 +0200775 // Disable internal software AGC if built-in AGC is enabled,
776 // i.e., replace the software AGC with the built-in AGC.
Karl Wibergbe579832015-11-10 22:34:18 +0100777 options.auto_gain_control = rtc::Optional<bool>(false);
henrikac14f5ff2015-09-23 14:08:33 +0200778 LOG(LS_INFO) << "Disabling AGC since built-in AGC will be used instead";
779 }
780 }
kwiberg102c6a62015-10-30 02:47:38 -0700781 if (voep->SetAgcStatus(*options.auto_gain_control, agc_mode) == -1) {
782 LOG_RTCERR2(SetAgcStatus, *options.auto_gain_control, agc_mode);
buildbot@webrtc.org13d67762014-05-02 17:33:29 +0000783 return false;
784 } else {
kwiberg102c6a62015-10-30 02:47:38 -0700785 LOG(LS_INFO) << "Auto gain set to " << *options.auto_gain_control
786 << " with mode " << agc_mode;
buildbot@webrtc.org13d67762014-05-02 17:33:29 +0000787 }
788 }
789
kwiberg102c6a62015-10-30 02:47:38 -0700790 if (options.tx_agc_target_dbov || options.tx_agc_digital_compression_gain ||
791 options.tx_agc_limiter) {
buildbot@webrtc.org13d67762014-05-02 17:33:29 +0000792 // Override default_agc_config_. Generally, an unset option means "leave
793 // the VoE bits alone" in this function, so we want whatever is set to be
794 // stored as the new "default". If we didn't, then setting e.g.
795 // tx_agc_target_dbov would reset digital compression gain and limiter
796 // settings.
797 // Also, if we don't update default_agc_config_, then adjust_agc_delta
798 // would be an offset from the original values, and not whatever was set
799 // explicitly.
kwiberg102c6a62015-10-30 02:47:38 -0700800 default_agc_config_.targetLeveldBOv = options.tx_agc_target_dbov.value_or(
801 default_agc_config_.targetLeveldBOv);
buildbot@webrtc.org13d67762014-05-02 17:33:29 +0000802 default_agc_config_.digitalCompressionGaindB =
kwiberg102c6a62015-10-30 02:47:38 -0700803 options.tx_agc_digital_compression_gain.value_or(
buildbot@webrtc.org13d67762014-05-02 17:33:29 +0000804 default_agc_config_.digitalCompressionGaindB);
805 default_agc_config_.limiterEnable =
kwiberg102c6a62015-10-30 02:47:38 -0700806 options.tx_agc_limiter.value_or(default_agc_config_.limiterEnable);
buildbot@webrtc.org13d67762014-05-02 17:33:29 +0000807 if (voe_wrapper_->processing()->SetAgcConfig(default_agc_config_) == -1) {
808 LOG_RTCERR3(SetAgcConfig,
809 default_agc_config_.targetLeveldBOv,
810 default_agc_config_.digitalCompressionGaindB,
811 default_agc_config_.limiterEnable);
812 return false;
813 }
814 }
815
kwiberg102c6a62015-10-30 02:47:38 -0700816 if (options.noise_suppression) {
henrikac14f5ff2015-09-23 14:08:33 +0200817 const bool built_in_ns = voe_wrapper_->hw()->BuiltInNSIsAvailable();
818 if (built_in_ns) {
kwiberg102c6a62015-10-30 02:47:38 -0700819 if (voe_wrapper_->hw()->EnableBuiltInNS(*options.noise_suppression) ==
820 0 &&
821 *options.noise_suppression) {
henrikac14f5ff2015-09-23 14:08:33 +0200822 // Disable internal software NS if built-in NS is enabled,
823 // i.e., replace the software NS with the built-in NS.
Karl Wibergbe579832015-11-10 22:34:18 +0100824 options.noise_suppression = rtc::Optional<bool>(false);
henrikac14f5ff2015-09-23 14:08:33 +0200825 LOG(LS_INFO) << "Disabling NS since built-in NS will be used instead";
826 }
827 }
kwiberg102c6a62015-10-30 02:47:38 -0700828 if (voep->SetNsStatus(*options.noise_suppression, ns_mode) == -1) {
829 LOG_RTCERR2(SetNsStatus, *options.noise_suppression, ns_mode);
buildbot@webrtc.org13d67762014-05-02 17:33:29 +0000830 return false;
831 } else {
kwiberg102c6a62015-10-30 02:47:38 -0700832 LOG(LS_INFO) << "Noise suppression set to " << *options.noise_suppression
henrikac14f5ff2015-09-23 14:08:33 +0200833 << " with mode " << ns_mode;
buildbot@webrtc.org13d67762014-05-02 17:33:29 +0000834 }
835 }
836
kwiberg102c6a62015-10-30 02:47:38 -0700837 if (options.highpass_filter) {
838 LOG(LS_INFO) << "High pass filter enabled? " << *options.highpass_filter;
839 if (voep->EnableHighPassFilter(*options.highpass_filter) == -1) {
840 LOG_RTCERR1(SetHighpassFilterStatus, *options.highpass_filter);
buildbot@webrtc.org13d67762014-05-02 17:33:29 +0000841 return false;
842 }
843 }
844
kwiberg102c6a62015-10-30 02:47:38 -0700845 if (options.stereo_swapping) {
846 LOG(LS_INFO) << "Stereo swapping enabled? " << *options.stereo_swapping;
847 voep->EnableStereoChannelSwapping(*options.stereo_swapping);
848 if (voep->IsStereoChannelSwappingEnabled() != *options.stereo_swapping) {
849 LOG_RTCERR1(EnableStereoChannelSwapping, *options.stereo_swapping);
buildbot@webrtc.org13d67762014-05-02 17:33:29 +0000850 return false;
851 }
852 }
853
kwiberg102c6a62015-10-30 02:47:38 -0700854 if (options.audio_jitter_buffer_max_packets) {
855 LOG(LS_INFO) << "NetEq capacity is "
856 << *options.audio_jitter_buffer_max_packets;
Henrik Lundin64dad832015-05-11 12:44:23 +0200857 voe_config_.Set<webrtc::NetEqCapacityConfig>(
kwiberg102c6a62015-10-30 02:47:38 -0700858 new webrtc::NetEqCapacityConfig(
859 *options.audio_jitter_buffer_max_packets));
Henrik Lundin64dad832015-05-11 12:44:23 +0200860 }
861
kwiberg102c6a62015-10-30 02:47:38 -0700862 if (options.audio_jitter_buffer_fast_accelerate) {
863 LOG(LS_INFO) << "NetEq fast mode? "
864 << *options.audio_jitter_buffer_fast_accelerate;
Henrik Lundin5263b3c2015-06-01 10:29:41 +0200865 voe_config_.Set<webrtc::NetEqFastAccelerate>(
kwiberg102c6a62015-10-30 02:47:38 -0700866 new webrtc::NetEqFastAccelerate(
867 *options.audio_jitter_buffer_fast_accelerate));
Henrik Lundin5263b3c2015-06-01 10:29:41 +0200868 }
869
kwiberg102c6a62015-10-30 02:47:38 -0700870 if (options.typing_detection) {
871 LOG(LS_INFO) << "Typing detection is enabled? "
872 << *options.typing_detection;
873 if (voep->SetTypingDetectionStatus(*options.typing_detection) == -1) {
buildbot@webrtc.org13d67762014-05-02 17:33:29 +0000874 // In case of error, log the info and continue
kwiberg102c6a62015-10-30 02:47:38 -0700875 LOG_RTCERR1(SetTypingDetectionStatus, *options.typing_detection);
buildbot@webrtc.org13d67762014-05-02 17:33:29 +0000876 }
877 }
878
kwiberg102c6a62015-10-30 02:47:38 -0700879 if (options.adjust_agc_delta) {
880 LOG(LS_INFO) << "Adjust agc delta is " << *options.adjust_agc_delta;
881 if (!AdjustAgcLevel(*options.adjust_agc_delta)) {
buildbot@webrtc.org13d67762014-05-02 17:33:29 +0000882 return false;
883 }
884 }
885
kwiberg102c6a62015-10-30 02:47:38 -0700886 if (options.aec_dump) {
887 LOG(LS_INFO) << "Aec dump is enabled? " << *options.aec_dump;
888 if (*options.aec_dump)
buildbot@webrtc.org13d67762014-05-02 17:33:29 +0000889 StartAecDump(kAecDumpByAudioOptionFilename);
890 else
891 StopAecDump();
892 }
893
buildbot@webrtc.org1f8a2372014-08-28 10:52:44 +0000894 webrtc::Config config;
895
kwiberg102c6a62015-10-30 02:47:38 -0700896 if (options.delay_agnostic_aec)
897 delay_agnostic_aec_ = options.delay_agnostic_aec;
898 if (delay_agnostic_aec_) {
899 LOG(LS_INFO) << "Delay agnostic aec is enabled? " << *delay_agnostic_aec_;
henrik.lundin0f133b92015-07-02 00:17:55 -0700900 config.Set<webrtc::DelayAgnostic>(
kwiberg102c6a62015-10-30 02:47:38 -0700901 new webrtc::DelayAgnostic(*delay_agnostic_aec_));
Bjorn Volckerbf395c12015-03-25 22:45:56 +0100902 }
903
kwiberg102c6a62015-10-30 02:47:38 -0700904 if (options.extended_filter_aec) {
905 extended_filter_aec_ = options.extended_filter_aec;
906 }
907 if (extended_filter_aec_) {
908 LOG(LS_INFO) << "Extended filter aec is enabled? " << *extended_filter_aec_;
Henrik Lundin441f6342015-06-09 16:03:13 +0200909 config.Set<webrtc::ExtendedFilter>(
kwiberg102c6a62015-10-30 02:47:38 -0700910 new webrtc::ExtendedFilter(*extended_filter_aec_));
buildbot@webrtc.org1f8a2372014-08-28 10:52:44 +0000911 }
912
kwiberg102c6a62015-10-30 02:47:38 -0700913 if (options.experimental_ns) {
914 experimental_ns_ = options.experimental_ns;
915 }
916 if (experimental_ns_) {
917 LOG(LS_INFO) << "Experimental ns is enabled? " << *experimental_ns_;
buildbot@webrtc.org1f8a2372014-08-28 10:52:44 +0000918 config.Set<webrtc::ExperimentalNs>(
kwiberg102c6a62015-10-30 02:47:38 -0700919 new webrtc::ExperimentalNs(*experimental_ns_));
buildbot@webrtc.org1f8a2372014-08-28 10:52:44 +0000920 }
buildbot@webrtc.org1f8a2372014-08-28 10:52:44 +0000921
922 // We check audioproc for the benefit of tests, since FakeWebRtcVoiceEngine
923 // returns NULL on audio_processing().
924 webrtc::AudioProcessing* audioproc = voe_wrapper_->base()->audio_processing();
925 if (audioproc) {
926 audioproc->SetExtraOptions(config);
927 }
928
kwiberg102c6a62015-10-30 02:47:38 -0700929 if (options.recording_sample_rate) {
930 LOG(LS_INFO) << "Recording sample rate is "
931 << *options.recording_sample_rate;
932 if (voe_wrapper_->hw()->SetRecordingSampleRate(
933 *options.recording_sample_rate)) {
934 LOG_RTCERR1(SetRecordingSampleRate, *options.recording_sample_rate);
buildbot@webrtc.org13d67762014-05-02 17:33:29 +0000935 }
936 }
937
kwiberg102c6a62015-10-30 02:47:38 -0700938 if (options.playout_sample_rate) {
939 LOG(LS_INFO) << "Playout sample rate is " << *options.playout_sample_rate;
940 if (voe_wrapper_->hw()->SetPlayoutSampleRate(
941 *options.playout_sample_rate)) {
942 LOG_RTCERR1(SetPlayoutSampleRate, *options.playout_sample_rate);
buildbot@webrtc.org13d67762014-05-02 17:33:29 +0000943 }
944 }
945
946 return true;
947}
948
solenberg246b8172015-12-08 09:50:23 -0800949void WebRtcVoiceEngine::SetDefaultDevices() {
solenberg566ef242015-11-06 15:34:49 -0800950 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
kjellanderfcfc8042016-01-14 11:01:09 -0800951#if !defined(WEBRTC_IOS)
solenberg246b8172015-12-08 09:50:23 -0800952 int in_id = kDefaultAudioDeviceId;
953 int out_id = kDefaultAudioDeviceId;
954 LOG(LS_INFO) << "Setting microphone to (id=" << in_id
955 << ") and speaker to (id=" << out_id << ")";
buildbot@webrtc.org13d67762014-05-02 17:33:29 +0000956
solenbergc1a1b352015-09-22 13:31:20 -0700957 bool ret = true;
solenberg246b8172015-12-08 09:50:23 -0800958 if (voe_wrapper_->hw()->SetRecordingDevice(in_id) == -1) {
959 LOG_RTCERR1(SetRecordingDevice, in_id);
buildbot@webrtc.org13d67762014-05-02 17:33:29 +0000960 ret = false;
961 }
solenberg246b8172015-12-08 09:50:23 -0800962 webrtc::AudioProcessing* ap = voe()->base()->audio_processing();
963 if (ap) {
964 ap->Initialize();
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000965 }
966
solenberg246b8172015-12-08 09:50:23 -0800967 if (voe_wrapper_->hw()->SetPlayoutDevice(out_id) == -1) {
968 LOG_RTCERR1(SetPlayoutDevice, out_id);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000969 ret = false;
970 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000971
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000972 if (ret) {
solenberg246b8172015-12-08 09:50:23 -0800973 LOG(LS_INFO) << "Set microphone to (id=" << in_id
974 << ") and speaker to (id=" << out_id << ")";
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000975 }
kjellanderfcfc8042016-01-14 11:01:09 -0800976#endif // !WEBRTC_IOS
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000977}
978
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000979bool WebRtcVoiceEngine::GetOutputVolume(int* level) {
solenberg566ef242015-11-06 15:34:49 -0800980 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000981 unsigned int ulevel;
982 if (voe_wrapper_->volume()->GetSpeakerVolume(ulevel) == -1) {
983 LOG_RTCERR1(GetSpeakerVolume, level);
984 return false;
985 }
986 *level = ulevel;
987 return true;
988}
989
990bool WebRtcVoiceEngine::SetOutputVolume(int level) {
solenberg566ef242015-11-06 15:34:49 -0800991 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
henrikg91d6ede2015-09-17 00:24:34 -0700992 RTC_DCHECK(level >= 0 && level <= 255);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000993 if (voe_wrapper_->volume()->SetSpeakerVolume(level) == -1) {
994 LOG_RTCERR1(SetSpeakerVolume, level);
995 return false;
996 }
997 return true;
998}
999
1000int WebRtcVoiceEngine::GetInputLevel() {
solenberg566ef242015-11-06 15:34:49 -08001001 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001002 unsigned int ulevel;
1003 return (voe_wrapper_->volume()->GetSpeechInputLevel(ulevel) != -1) ?
1004 static_cast<int>(ulevel) : -1;
1005}
1006
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001007const std::vector<AudioCodec>& WebRtcVoiceEngine::codecs() {
solenberg566ef242015-11-06 15:34:49 -08001008 RTC_DCHECK(signal_thread_checker_.CalledOnValidThread());
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001009 return codecs_;
1010}
1011
Stefan Holmer9d69c3f2015-12-07 10:45:43 +01001012RtpCapabilities WebRtcVoiceEngine::GetCapabilities() const {
solenberg566ef242015-11-06 15:34:49 -08001013 RTC_DCHECK(signal_thread_checker_.CalledOnValidThread());
Stefan Holmer9d69c3f2015-12-07 10:45:43 +01001014 RtpCapabilities capabilities;
1015 capabilities.header_extensions.push_back(RtpHeaderExtension(
1016 kRtpAudioLevelHeaderExtension, kRtpAudioLevelHeaderExtensionDefaultId));
1017 capabilities.header_extensions.push_back(
1018 RtpHeaderExtension(kRtpAbsoluteSenderTimeHeaderExtension,
1019 kRtpAbsoluteSenderTimeHeaderExtensionDefaultId));
stefanba4c0e42016-02-04 04:12:24 -08001020 if (webrtc::field_trial::FindFullName("WebRTC-Audio-SendSideBwe") ==
1021 "Enabled") {
1022 capabilities.header_extensions.push_back(RtpHeaderExtension(
1023 kRtpTransportSequenceNumberHeaderExtension,
1024 kRtpTransportSequenceNumberHeaderExtensionDefaultId));
1025 }
Stefan Holmer9d69c3f2015-12-07 10:45:43 +01001026 return capabilities;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001027}
1028
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001029int WebRtcVoiceEngine::GetLastEngineError() {
solenberg566ef242015-11-06 15:34:49 -08001030 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001031 return voe_wrapper_->error();
1032}
1033
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001034void WebRtcVoiceEngine::Print(webrtc::TraceLevel level, const char* trace,
1035 int length) {
solenberg566ef242015-11-06 15:34:49 -08001036 // Note: This callback can happen on any thread!
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00001037 rtc::LoggingSeverity sev = rtc::LS_VERBOSE;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001038 if (level == webrtc::kTraceError || level == webrtc::kTraceCritical)
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00001039 sev = rtc::LS_ERROR;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001040 else if (level == webrtc::kTraceWarning)
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00001041 sev = rtc::LS_WARNING;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001042 else if (level == webrtc::kTraceStateInfo || level == webrtc::kTraceInfo)
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00001043 sev = rtc::LS_INFO;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001044 else if (level == webrtc::kTraceTerseInfo)
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00001045 sev = rtc::LS_INFO;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001046
1047 // Skip past boilerplate prefix text
1048 if (length < 72) {
1049 std::string msg(trace, length);
1050 LOG(LS_ERROR) << "Malformed webrtc log message: ";
1051 LOG_V(sev) << msg;
1052 } else {
1053 std::string msg(trace + 71, length - 72);
Peter Boströmd5c75b12015-09-23 13:24:32 +02001054 LOG_V(sev) << "webrtc: " << msg;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001055 }
1056}
1057
solenberg63b34542015-09-29 06:06:31 -07001058void WebRtcVoiceEngine::RegisterChannel(WebRtcVoiceMediaChannel* channel) {
solenberg566ef242015-11-06 15:34:49 -08001059 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
1060 RTC_DCHECK(channel);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001061 channels_.push_back(channel);
1062}
1063
solenberg63b34542015-09-29 06:06:31 -07001064void WebRtcVoiceEngine::UnregisterChannel(WebRtcVoiceMediaChannel* channel) {
solenberg566ef242015-11-06 15:34:49 -08001065 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
solenberg63b34542015-09-29 06:06:31 -07001066 auto it = std::find(channels_.begin(), channels_.end(), channel);
solenberg566ef242015-11-06 15:34:49 -08001067 RTC_DCHECK(it != channels_.end());
1068 channels_.erase(it);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001069}
1070
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001071// Adjusts the default AGC target level by the specified delta.
1072// NB: If we start messing with other config fields, we'll want
1073// to save the current webrtc::AgcConfig as well.
1074bool WebRtcVoiceEngine::AdjustAgcLevel(int delta) {
solenberg566ef242015-11-06 15:34:49 -08001075 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001076 webrtc::AgcConfig config = default_agc_config_;
1077 config.targetLeveldBOv -= delta;
1078
1079 LOG(LS_INFO) << "Adjusting AGC level from default -"
1080 << default_agc_config_.targetLeveldBOv << "dB to -"
1081 << config.targetLeveldBOv << "dB";
1082
1083 if (voe_wrapper_->processing()->SetAgcConfig(config) == -1) {
1084 LOG_RTCERR1(SetAgcConfig, config.targetLeveldBOv);
1085 return false;
1086 }
1087 return true;
1088}
1089
Fredrik Solenbergccb49e72015-05-19 11:37:56 +02001090bool WebRtcVoiceEngine::SetAudioDeviceModule(webrtc::AudioDeviceModule* adm) {
solenberg566ef242015-11-06 15:34:49 -08001091 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001092 if (initialized_) {
1093 LOG(LS_WARNING) << "SetAudioDeviceModule can not be called after Init.";
1094 return false;
1095 }
1096 if (adm_) {
1097 adm_->Release();
1098 adm_ = NULL;
1099 }
1100 if (adm) {
1101 adm_ = adm;
1102 adm_->AddRef();
1103 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001104 return true;
1105}
1106
ivocd66b44d2016-01-15 03:06:36 -08001107bool WebRtcVoiceEngine::StartAecDump(rtc::PlatformFile file,
1108 int64_t max_size_bytes) {
solenberg566ef242015-11-06 15:34:49 -08001109 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00001110 FILE* aec_dump_file_stream = rtc::FdopenPlatformFileForWriting(file);
wu@webrtc.orga8910d22014-01-23 22:12:45 +00001111 if (!aec_dump_file_stream) {
1112 LOG(LS_ERROR) << "Could not open AEC dump file stream.";
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00001113 if (!rtc::ClosePlatformFile(file))
wu@webrtc.orga8910d22014-01-23 22:12:45 +00001114 LOG(LS_WARNING) << "Could not close file.";
1115 return false;
1116 }
wu@webrtc.orga9890802013-12-13 00:21:03 +00001117 StopAecDump();
ivocd66b44d2016-01-15 03:06:36 -08001118 if (voe_wrapper_->base()->audio_processing()->StartDebugRecording(
1119 aec_dump_file_stream, max_size_bytes) !=
wu@webrtc.orga9890802013-12-13 00:21:03 +00001120 webrtc::AudioProcessing::kNoError) {
wu@webrtc.orga8910d22014-01-23 22:12:45 +00001121 LOG_RTCERR0(StartDebugRecording);
1122 fclose(aec_dump_file_stream);
wu@webrtc.orga9890802013-12-13 00:21:03 +00001123 return false;
1124 }
1125 is_dumping_aec_ = true;
1126 return true;
wu@webrtc.orga9890802013-12-13 00:21:03 +00001127}
1128
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001129void WebRtcVoiceEngine::StartAecDump(const std::string& filename) {
solenberg566ef242015-11-06 15:34:49 -08001130 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001131 if (!is_dumping_aec_) {
1132 // Start dumping AEC when we are not dumping.
ivocd66b44d2016-01-15 03:06:36 -08001133 if (voe_wrapper_->base()->audio_processing()->StartDebugRecording(
1134 filename.c_str(), -1) != webrtc::AudioProcessing::kNoError) {
wu@webrtc.orga9890802013-12-13 00:21:03 +00001135 LOG_RTCERR1(StartDebugRecording, filename.c_str());
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001136 } else {
1137 is_dumping_aec_ = true;
1138 }
1139 }
1140}
1141
1142void WebRtcVoiceEngine::StopAecDump() {
solenberg566ef242015-11-06 15:34:49 -08001143 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001144 if (is_dumping_aec_) {
1145 // Stop dumping AEC when we are dumping.
ivocd66b44d2016-01-15 03:06:36 -08001146 if (voe_wrapper_->base()->audio_processing()->StopDebugRecording() !=
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001147 webrtc::AudioProcessing::kNoError) {
1148 LOG_RTCERR0(StopDebugRecording);
1149 }
1150 is_dumping_aec_ = false;
1151 }
1152}
1153
ivoc112a3d82015-10-16 02:22:18 -07001154bool WebRtcVoiceEngine::StartRtcEventLog(rtc::PlatformFile file) {
solenberg566ef242015-11-06 15:34:49 -08001155 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
ivoc112a3d82015-10-16 02:22:18 -07001156 return voe_wrapper_->codec()->GetEventLog()->StartLogging(file);
1157}
1158
1159void WebRtcVoiceEngine::StopRtcEventLog() {
solenberg566ef242015-11-06 15:34:49 -08001160 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
ivoc112a3d82015-10-16 02:22:18 -07001161 voe_wrapper_->codec()->GetEventLog()->StopLogging();
1162}
1163
solenberg0a617e22015-10-20 15:49:38 -07001164int WebRtcVoiceEngine::CreateVoEChannel() {
solenberg566ef242015-11-06 15:34:49 -08001165 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
solenberg0a617e22015-10-20 15:49:38 -07001166 return voe_wrapper_->base()->CreateChannel(voe_config_);
sergeyu@chromium.org5bc25c42013-12-05 00:24:06 +00001167}
1168
solenbergc96df772015-10-21 13:01:53 -07001169class WebRtcVoiceMediaChannel::WebRtcAudioSendStream
mallinath@webrtc.org67ee6b92014-02-03 16:57:16 +00001170 : public AudioRenderer::Sink {
1171 public:
solenbergc96df772015-10-21 13:01:53 -07001172 WebRtcAudioSendStream(int ch, webrtc::AudioTransport* voe_audio_transport,
solenberg3a941542015-11-16 07:34:50 -08001173 uint32_t ssrc, const std::string& c_name,
1174 const std::vector<webrtc::RtpExtension>& extensions,
1175 webrtc::Call* call)
solenberg7add0582015-11-20 09:59:34 -08001176 : voe_audio_transport_(voe_audio_transport),
solenberg3a941542015-11-16 07:34:50 -08001177 call_(call),
1178 config_(nullptr) {
solenberg85a04962015-10-27 03:35:21 -07001179 RTC_DCHECK_GE(ch, 0);
1180 // TODO(solenberg): Once we're not using FakeWebRtcVoiceEngine anymore:
1181 // RTC_DCHECK(voe_audio_transport);
solenbergc96df772015-10-21 13:01:53 -07001182 RTC_DCHECK(call);
solenberg85a04962015-10-27 03:35:21 -07001183 audio_capture_thread_checker_.DetachFromThread();
solenberg3a941542015-11-16 07:34:50 -08001184 config_.rtp.ssrc = ssrc;
1185 config_.rtp.c_name = c_name;
1186 config_.voe_channel_id = ch;
1187 RecreateAudioSendStream(extensions);
solenbergc96df772015-10-21 13:01:53 -07001188 }
solenberg3a941542015-11-16 07:34:50 -08001189
solenbergc96df772015-10-21 13:01:53 -07001190 ~WebRtcAudioSendStream() override {
solenberg566ef242015-11-06 15:34:49 -08001191 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
solenbergc96df772015-10-21 13:01:53 -07001192 Stop();
1193 call_->DestroyAudioSendStream(stream_);
1194 }
henrike@webrtc.org1e09a712013-07-26 19:17:59 +00001195
solenberg3a941542015-11-16 07:34:50 -08001196 void RecreateAudioSendStream(
1197 const std::vector<webrtc::RtpExtension>& extensions) {
1198 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
1199 if (stream_) {
1200 call_->DestroyAudioSendStream(stream_);
1201 stream_ = nullptr;
1202 }
1203 config_.rtp.extensions = extensions;
1204 RTC_DCHECK(!stream_);
1205 stream_ = call_->CreateAudioSendStream(config_);
1206 RTC_CHECK(stream_);
1207 }
1208
Fredrik Solenbergb5727682015-12-04 15:22:19 +01001209 bool SendTelephoneEvent(int payload_type, uint8_t event,
1210 uint32_t duration_ms) {
1211 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
1212 RTC_DCHECK(stream_);
1213 return stream_->SendTelephoneEvent(payload_type, event, duration_ms);
1214 }
1215
solenberg3a941542015-11-16 07:34:50 -08001216 webrtc::AudioSendStream::Stats GetStats() const {
1217 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
1218 RTC_DCHECK(stream_);
1219 return stream_->GetStats();
1220 }
1221
mallinath@webrtc.org67ee6b92014-02-03 16:57:16 +00001222 // Starts the rendering by setting a sink to the renderer to get data
1223 // callback.
henrike@webrtc.orga7b98182014-02-21 15:51:43 +00001224 // This method is called on the libjingle worker thread.
mallinath@webrtc.org67ee6b92014-02-03 16:57:16 +00001225 // TODO(xians): Make sure Start() is called only once.
1226 void Start(AudioRenderer* renderer) {
solenberg566ef242015-11-06 15:34:49 -08001227 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
solenbergc96df772015-10-21 13:01:53 -07001228 RTC_DCHECK(renderer);
1229 if (renderer_) {
henrikg91d6ede2015-09-17 00:24:34 -07001230 RTC_DCHECK(renderer_ == renderer);
mallinath@webrtc.org67ee6b92014-02-03 16:57:16 +00001231 return;
1232 }
mallinath@webrtc.org67ee6b92014-02-03 16:57:16 +00001233 renderer->SetSink(this);
1234 renderer_ = renderer;
1235 }
1236
solenbergc96df772015-10-21 13:01:53 -07001237 // Stops rendering by setting the sink of the renderer to nullptr. No data
mallinath@webrtc.org67ee6b92014-02-03 16:57:16 +00001238 // callback will be received after this method.
henrike@webrtc.orga7b98182014-02-21 15:51:43 +00001239 // This method is called on the libjingle worker thread.
mallinath@webrtc.org67ee6b92014-02-03 16:57:16 +00001240 void Stop() {
solenberg566ef242015-11-06 15:34:49 -08001241 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
solenbergc96df772015-10-21 13:01:53 -07001242 if (renderer_) {
1243 renderer_->SetSink(nullptr);
1244 renderer_ = nullptr;
solenberg98c68862015-10-09 03:27:14 -07001245 }
mallinath@webrtc.org67ee6b92014-02-03 16:57:16 +00001246 }
1247
1248 // AudioRenderer::Sink implementation.
henrike@webrtc.orga7b98182014-02-21 15:51:43 +00001249 // This method is called on the audio thread.
kjellander@webrtc.org14665ff2015-03-04 12:58:35 +00001250 void OnData(const void* audio_data,
1251 int bits_per_sample,
1252 int sample_rate,
Peter Kasting69558702016-01-12 16:26:35 -08001253 size_t number_of_channels,
Peter Kastingdce40cf2015-08-24 14:52:23 -07001254 size_t number_of_frames) override {
solenberg566ef242015-11-06 15:34:49 -08001255 RTC_DCHECK(!worker_thread_checker_.CalledOnValidThread());
solenberg85a04962015-10-27 03:35:21 -07001256 RTC_DCHECK(audio_capture_thread_checker_.CalledOnValidThread());
solenbergc96df772015-10-21 13:01:53 -07001257 RTC_DCHECK(voe_audio_transport_);
solenberg7add0582015-11-20 09:59:34 -08001258 voe_audio_transport_->OnData(config_.voe_channel_id,
henrike@webrtc.orga7b98182014-02-21 15:51:43 +00001259 audio_data,
1260 bits_per_sample,
1261 sample_rate,
1262 number_of_channels,
1263 number_of_frames);
henrike@webrtc.orga7b98182014-02-21 15:51:43 +00001264 }
1265
1266 // Callback from the |renderer_| when it is going away. In case Start() has
1267 // never been called, this callback won't be triggered.
kjellander@webrtc.org14665ff2015-03-04 12:58:35 +00001268 void OnClose() override {
solenberg566ef242015-11-06 15:34:49 -08001269 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
solenbergc96df772015-10-21 13:01:53 -07001270 // Set |renderer_| to nullptr to make sure no more callback will get into
henrike@webrtc.orga7b98182014-02-21 15:51:43 +00001271 // the renderer.
solenbergc96df772015-10-21 13:01:53 -07001272 renderer_ = nullptr;
mallinath@webrtc.org67ee6b92014-02-03 16:57:16 +00001273 }
1274
1275 // Accessor to the VoE channel ID.
solenberg85a04962015-10-27 03:35:21 -07001276 int channel() const {
solenberg566ef242015-11-06 15:34:49 -08001277 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
solenberg7add0582015-11-20 09:59:34 -08001278 return config_.voe_channel_id;
solenberg85a04962015-10-27 03:35:21 -07001279 }
mallinath@webrtc.org67ee6b92014-02-03 16:57:16 +00001280
1281 private:
solenberg566ef242015-11-06 15:34:49 -08001282 rtc::ThreadChecker worker_thread_checker_;
solenberg85a04962015-10-27 03:35:21 -07001283 rtc::ThreadChecker audio_capture_thread_checker_;
solenbergc96df772015-10-21 13:01:53 -07001284 webrtc::AudioTransport* const voe_audio_transport_ = nullptr;
1285 webrtc::Call* call_ = nullptr;
solenberg3a941542015-11-16 07:34:50 -08001286 webrtc::AudioSendStream::Config config_;
1287 // The stream is owned by WebRtcAudioSendStream and may be reallocated if
1288 // configuration changes.
solenbergc96df772015-10-21 13:01:53 -07001289 webrtc::AudioSendStream* stream_ = nullptr;
mallinath@webrtc.org67ee6b92014-02-03 16:57:16 +00001290
1291 // Raw pointer to AudioRenderer owned by LocalAudioTrackHandler.
1292 // PeerConnection will make sure invalidating the pointer before the object
1293 // goes away.
solenbergc96df772015-10-21 13:01:53 -07001294 AudioRenderer* renderer_ = nullptr;
henrike@webrtc.orga7b98182014-02-21 15:51:43 +00001295
solenbergc96df772015-10-21 13:01:53 -07001296 RTC_DISALLOW_IMPLICIT_CONSTRUCTORS(WebRtcAudioSendStream);
1297};
1298
1299class WebRtcVoiceMediaChannel::WebRtcAudioReceiveStream {
1300 public:
stefanba4c0e42016-02-04 04:12:24 -08001301 WebRtcAudioReceiveStream(int ch,
1302 uint32_t remote_ssrc,
1303 uint32_t local_ssrc,
1304 bool use_transport_cc,
1305 const std::string& sync_group,
solenberg7add0582015-11-20 09:59:34 -08001306 const std::vector<webrtc::RtpExtension>& extensions,
1307 webrtc::Call* call)
stefanba4c0e42016-02-04 04:12:24 -08001308 : call_(call), config_() {
solenberg7add0582015-11-20 09:59:34 -08001309 RTC_DCHECK_GE(ch, 0);
1310 RTC_DCHECK(call);
1311 config_.rtp.remote_ssrc = remote_ssrc;
1312 config_.rtp.local_ssrc = local_ssrc;
1313 config_.voe_channel_id = ch;
1314 config_.sync_group = sync_group;
stefanba4c0e42016-02-04 04:12:24 -08001315 RecreateAudioReceiveStream(use_transport_cc, extensions);
solenberg7add0582015-11-20 09:59:34 -08001316 }
solenbergc96df772015-10-21 13:01:53 -07001317
solenberg7add0582015-11-20 09:59:34 -08001318 ~WebRtcAudioReceiveStream() {
1319 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
1320 call_->DestroyAudioReceiveStream(stream_);
1321 }
1322
1323 void RecreateAudioReceiveStream(
1324 const std::vector<webrtc::RtpExtension>& extensions) {
1325 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
stefanba4c0e42016-02-04 04:12:24 -08001326 RecreateAudioReceiveStream(config_.rtp.transport_cc, extensions);
solenberg7add0582015-11-20 09:59:34 -08001327 }
stefanba4c0e42016-02-04 04:12:24 -08001328 void RecreateAudioReceiveStream(bool use_transport_cc) {
solenberg7add0582015-11-20 09:59:34 -08001329 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
stefanba4c0e42016-02-04 04:12:24 -08001330 RecreateAudioReceiveStream(use_transport_cc, config_.rtp.extensions);
solenberg7add0582015-11-20 09:59:34 -08001331 }
1332
1333 webrtc::AudioReceiveStream::Stats GetStats() const {
1334 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
1335 RTC_DCHECK(stream_);
1336 return stream_->GetStats();
1337 }
1338
1339 int channel() const {
1340 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
1341 return config_.voe_channel_id;
1342 }
solenbergc96df772015-10-21 13:01:53 -07001343
deadbeef2d110be2016-01-13 12:00:26 -08001344 void SetRawAudioSink(rtc::scoped_ptr<webrtc::AudioSinkInterface> sink) {
Tommif888bb52015-12-12 01:37:01 +01001345 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
deadbeef2d110be2016-01-13 12:00:26 -08001346 stream_->SetSink(std::move(sink));
Tommif888bb52015-12-12 01:37:01 +01001347 }
1348
solenbergc96df772015-10-21 13:01:53 -07001349 private:
stefanba4c0e42016-02-04 04:12:24 -08001350 void RecreateAudioReceiveStream(
1351 bool use_transport_cc,
solenberg7add0582015-11-20 09:59:34 -08001352 const std::vector<webrtc::RtpExtension>& extensions) {
1353 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
1354 if (stream_) {
1355 call_->DestroyAudioReceiveStream(stream_);
1356 stream_ = nullptr;
1357 }
1358 config_.rtp.extensions = extensions;
stefanba4c0e42016-02-04 04:12:24 -08001359 config_.rtp.transport_cc = use_transport_cc;
solenberg7add0582015-11-20 09:59:34 -08001360 RTC_DCHECK(!stream_);
1361 stream_ = call_->CreateAudioReceiveStream(config_);
1362 RTC_CHECK(stream_);
1363 }
1364
1365 rtc::ThreadChecker worker_thread_checker_;
1366 webrtc::Call* call_ = nullptr;
1367 webrtc::AudioReceiveStream::Config config_;
1368 // The stream is owned by WebRtcAudioReceiveStream and may be reallocated if
1369 // configuration changes.
1370 webrtc::AudioReceiveStream* stream_ = nullptr;
solenbergc96df772015-10-21 13:01:53 -07001371
1372 RTC_DISALLOW_IMPLICIT_CONSTRUCTORS(WebRtcAudioReceiveStream);
henrike@webrtc.org1e09a712013-07-26 19:17:59 +00001373};
1374
Fredrik Solenberg709ed672015-09-15 12:26:33 +02001375WebRtcVoiceMediaChannel::WebRtcVoiceMediaChannel(WebRtcVoiceEngine* engine,
Fredrik Solenbergb071a192015-09-17 16:42:56 +02001376 const AudioOptions& options,
Fredrik Solenberg709ed672015-09-15 12:26:33 +02001377 webrtc::Call* call)
solenberg566ef242015-11-06 15:34:49 -08001378 : engine_(engine), call_(call) {
solenberg0a617e22015-10-20 15:49:38 -07001379 LOG(LS_VERBOSE) << "WebRtcVoiceMediaChannel::WebRtcVoiceMediaChannel";
solenberg566ef242015-11-06 15:34:49 -08001380 RTC_DCHECK(call);
solenberg0a617e22015-10-20 15:49:38 -07001381 engine->RegisterChannel(this);
Fredrik Solenbergb071a192015-09-17 16:42:56 +02001382 SetOptions(options);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001383}
1384
1385WebRtcVoiceMediaChannel::~WebRtcVoiceMediaChannel() {
solenberg566ef242015-11-06 15:34:49 -08001386 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
solenberg0a617e22015-10-20 15:49:38 -07001387 LOG(LS_VERBOSE) << "WebRtcVoiceMediaChannel::~WebRtcVoiceMediaChannel";
solenberg7add0582015-11-20 09:59:34 -08001388 // TODO(solenberg): Should be able to delete the streams directly, without
1389 // going through RemoveNnStream(), once stream objects handle
1390 // all (de)configuration.
solenbergc96df772015-10-21 13:01:53 -07001391 while (!send_streams_.empty()) {
1392 RemoveSendStream(send_streams_.begin()->first);
solenbergd97ec302015-10-07 01:40:33 -07001393 }
solenberg7add0582015-11-20 09:59:34 -08001394 while (!recv_streams_.empty()) {
1395 RemoveRecvStream(recv_streams_.begin()->first);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001396 }
solenberg0a617e22015-10-20 15:49:38 -07001397 engine()->UnregisterChannel(this);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001398}
1399
Peter Thatcherc2ee2c82015-08-07 16:05:34 -07001400bool WebRtcVoiceMediaChannel::SetSendParameters(
1401 const AudioSendParameters& params) {
solenberg566ef242015-11-06 15:34:49 -08001402 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
solenberg7e4e01a2015-12-02 08:05:01 -08001403 LOG(LS_INFO) << "WebRtcVoiceMediaChannel::SetSendParameters: "
1404 << params.ToString();
Peter Thatcherc2ee2c82015-08-07 16:05:34 -07001405 // TODO(pthatcher): Refactor this to be more clean now that we have
1406 // all the information at once.
solenberg3a941542015-11-16 07:34:50 -08001407
1408 if (!SetSendCodecs(params.codecs)) {
1409 return false;
1410 }
1411
solenberg7e4e01a2015-12-02 08:05:01 -08001412 if (!ValidateRtpExtensions(params.extensions)) {
1413 return false;
1414 }
1415 std::vector<webrtc::RtpExtension> filtered_extensions =
1416 FilterRtpExtensions(params.extensions,
1417 webrtc::RtpExtension::IsSupportedForAudio, true);
1418 if (send_rtp_extensions_ != filtered_extensions) {
1419 send_rtp_extensions_.swap(filtered_extensions);
solenberg3a941542015-11-16 07:34:50 -08001420 for (auto& it : send_streams_) {
1421 it.second->RecreateAudioSendStream(send_rtp_extensions_);
1422 }
1423 }
1424
1425 if (!SetMaxSendBandwidth(params.max_bandwidth_bps)) {
1426 return false;
1427 }
1428 return SetOptions(params.options);
Peter Thatcherc2ee2c82015-08-07 16:05:34 -07001429}
1430
1431bool WebRtcVoiceMediaChannel::SetRecvParameters(
1432 const AudioRecvParameters& params) {
solenberg566ef242015-11-06 15:34:49 -08001433 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
solenberg7e4e01a2015-12-02 08:05:01 -08001434 LOG(LS_INFO) << "WebRtcVoiceMediaChannel::SetRecvParameters: "
1435 << params.ToString();
Peter Thatcherc2ee2c82015-08-07 16:05:34 -07001436 // TODO(pthatcher): Refactor this to be more clean now that we have
1437 // all the information at once.
solenberg7add0582015-11-20 09:59:34 -08001438
1439 if (!SetRecvCodecs(params.codecs)) {
1440 return false;
1441 }
1442
solenberg7e4e01a2015-12-02 08:05:01 -08001443 if (!ValidateRtpExtensions(params.extensions)) {
1444 return false;
1445 }
1446 std::vector<webrtc::RtpExtension> filtered_extensions =
1447 FilterRtpExtensions(params.extensions,
1448 webrtc::RtpExtension::IsSupportedForAudio, false);
1449 if (recv_rtp_extensions_ != filtered_extensions) {
1450 recv_rtp_extensions_.swap(filtered_extensions);
solenberg7add0582015-11-20 09:59:34 -08001451 for (auto& it : recv_streams_) {
1452 it.second->RecreateAudioReceiveStream(recv_rtp_extensions_);
1453 }
1454 }
solenberg7add0582015-11-20 09:59:34 -08001455 return true;
Peter Thatcherc2ee2c82015-08-07 16:05:34 -07001456}
1457
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001458bool WebRtcVoiceMediaChannel::SetOptions(const AudioOptions& options) {
solenberg566ef242015-11-06 15:34:49 -08001459 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001460 LOG(LS_INFO) << "Setting voice channel options: "
1461 << options.ToString();
1462
wu@webrtc.orgde305012013-10-31 15:40:38 +00001463 // Check if DSCP value is changed from previous.
1464 bool dscp_option_changed = (options_.dscp != options.dscp);
1465
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001466 // We retain all of the existing options, and apply the given ones
1467 // on top. This means there is no way to "clear" options such that
1468 // they go back to the engine default.
1469 options_.SetAll(options);
solenberg246b8172015-12-08 09:50:23 -08001470 if (!engine()->ApplyOptions(options_)) {
1471 LOG(LS_WARNING) <<
1472 "Failed to apply engine options during channel SetOptions.";
1473 return false;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001474 }
1475
wu@webrtc.orgde305012013-10-31 15:40:38 +00001476 if (dscp_option_changed) {
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00001477 rtc::DiffServCodePoint dscp = rtc::DSCP_DEFAULT;
solenberg246b8172015-12-08 09:50:23 -08001478 if (options_.dscp.value_or(false)) {
wu@webrtc.orgde305012013-10-31 15:40:38 +00001479 dscp = kAudioDscpValue;
solenberg246b8172015-12-08 09:50:23 -08001480 }
wu@webrtc.orgde305012013-10-31 15:40:38 +00001481 if (MediaChannel::SetDscp(dscp) != 0) {
1482 LOG(LS_WARNING) << "Failed to set DSCP settings for audio channel";
1483 }
1484 }
solenberg8fb30c32015-10-13 03:06:58 -07001485
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001486 LOG(LS_INFO) << "Set voice channel options. Current options: "
1487 << options_.ToString();
1488 return true;
1489}
1490
1491bool WebRtcVoiceMediaChannel::SetRecvCodecs(
1492 const std::vector<AudioCodec>& codecs) {
solenberg566ef242015-11-06 15:34:49 -08001493 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
solenberg8fb30c32015-10-13 03:06:58 -07001494
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001495 // Set the payload types to be used for incoming media.
solenberg0b675462015-10-09 01:37:09 -07001496 LOG(LS_INFO) << "Setting receive voice codecs.";
solenberg0b675462015-10-09 01:37:09 -07001497
1498 if (!VerifyUniquePayloadTypes(codecs)) {
1499 LOG(LS_ERROR) << "Codec payload types overlap.";
1500 return false;
1501 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001502
1503 std::vector<AudioCodec> new_codecs;
1504 // Find all new codecs. We allow adding new codecs but don't allow changing
1505 // the payload type of codecs that is already configured since we might
1506 // already be receiving packets with that payload type.
Fredrik Solenbergaf9fb212015-08-26 10:45:53 +02001507 for (const AudioCodec& codec : codecs) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001508 AudioCodec old_codec;
Fredrik Solenbergaf9fb212015-08-26 10:45:53 +02001509 if (FindCodec(recv_codecs_, codec, &old_codec)) {
1510 if (old_codec.id != codec.id) {
1511 LOG(LS_ERROR) << codec.name << " payload type changed.";
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001512 return false;
1513 }
1514 } else {
Fredrik Solenbergaf9fb212015-08-26 10:45:53 +02001515 new_codecs.push_back(codec);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001516 }
1517 }
1518 if (new_codecs.empty()) {
1519 // There are no new codecs to configure. Already configured codecs are
1520 // never removed.
1521 return true;
1522 }
1523
1524 if (playout_) {
1525 // Receive codecs can not be changed while playing. So we temporarily
1526 // pause playout.
1527 PausePlayout();
1528 }
1529
solenberg26c8c912015-11-27 04:00:25 -08001530 bool result = true;
1531 for (const AudioCodec& codec : new_codecs) {
1532 webrtc::CodecInst voe_codec;
1533 if (WebRtcVoiceEngine::ToCodecInst(codec, &voe_codec)) {
1534 LOG(LS_INFO) << ToString(codec);
1535 voe_codec.pltype = codec.id;
1536 for (const auto& ch : recv_streams_) {
1537 if (engine()->voe()->codec()->SetRecPayloadType(
1538 ch.second->channel(), voe_codec) == -1) {
1539 LOG_RTCERR2(SetRecPayloadType, ch.second->channel(),
1540 ToString(voe_codec));
1541 result = false;
1542 }
1543 }
1544 } else {
1545 LOG(LS_WARNING) << "Unknown codec " << ToString(codec);
1546 result = false;
1547 break;
1548 }
1549 }
Fredrik Solenbergaf9fb212015-08-26 10:45:53 +02001550 if (result) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001551 recv_codecs_ = codecs;
1552 }
1553
1554 if (desired_playout_ && !playout_) {
1555 ResumePlayout();
1556 }
Fredrik Solenbergaf9fb212015-08-26 10:45:53 +02001557 return result;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001558}
1559
1560bool WebRtcVoiceMediaChannel::SetSendCodecs(
wu@webrtc.orgcadf9042013-08-30 21:24:16 +00001561 int channel, const std::vector<AudioCodec>& codecs) {
buildbot@webrtc.orgae740dd2014-06-17 10:56:41 +00001562 // Disable VAD, FEC, and RED unless we know the other side wants them.
wu@webrtc.orgcadf9042013-08-30 21:24:16 +00001563 engine()->voe()->codec()->SetVADStatus(channel, false);
1564 engine()->voe()->rtp()->SetNACKStatus(channel, false, 0);
buildbot@webrtc.orgae740dd2014-06-17 10:56:41 +00001565 engine()->voe()->rtp()->SetREDStatus(channel, false);
1566 engine()->voe()->codec()->SetFECStatus(channel, false);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001567
1568 // Scan through the list to figure out the codec to use for sending, along
Fredrik Solenbergb5727682015-12-04 15:22:19 +01001569 // with the proper configuration for VAD.
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001570 webrtc::CodecInst send_codec;
1571 memset(&send_codec, 0, sizeof(send_codec));
1572
wu@webrtc.org05e7b442014-04-01 17:44:24 +00001573 bool nack_enabled = nack_enabled_;
buildbot@webrtc.org3ffa1f92014-07-02 19:51:26 +00001574 bool enable_codec_fec = false;
Minyue Li7100dcd2015-03-27 05:05:59 +01001575 bool enable_opus_dtx = false;
minyue@webrtc.org26236952014-10-29 02:27:08 +00001576 int opus_max_playback_rate = 0;
stefanba4c0e42016-02-04 04:12:24 -08001577 int red_payload_type = -1;
buildbot@webrtc.org5d639b32014-09-10 07:57:12 +00001578
henrike@webrtc.org704bf9e2014-02-27 17:52:04 +00001579 // Set send codec (the first non-telephone-event/CN codec)
stefanba4c0e42016-02-04 04:12:24 -08001580 const AudioCodec* codec = WebRtcVoiceCodecs::GetPreferredCodec(
1581 codecs, &send_codec, &red_payload_type);
1582 if (codec) {
1583 if (red_payload_type != -1) {
henrike@webrtc.org704bf9e2014-02-27 17:52:04 +00001584 // Enable redundant encoding of the specified codec. Treat any
1585 // failure as a fatal internal error.
buildbot@webrtc.orgae740dd2014-06-17 10:56:41 +00001586 LOG(LS_INFO) << "Enabling RED on channel " << channel;
stefanba4c0e42016-02-04 04:12:24 -08001587 if (engine()->voe()->rtp()->SetREDStatus(channel, true,
1588 red_payload_type) == -1) {
1589 LOG_RTCERR3(SetREDStatus, channel, true, red_payload_type);
henrike@webrtc.org704bf9e2014-02-27 17:52:04 +00001590 return false;
1591 }
1592 } else {
stefanba4c0e42016-02-04 04:12:24 -08001593 nack_enabled = HasNack(*codec);
Minyue Li7100dcd2015-03-27 05:05:59 +01001594 // For Opus as the send codec, we are to determine inband FEC, maximum
1595 // playback rate, and opus internal dtx.
stefanba4c0e42016-02-04 04:12:24 -08001596 if (IsCodec(*codec, kOpusCodecName)) {
1597 GetOpusConfig(*codec, &send_codec, &enable_codec_fec,
Minyue Li7100dcd2015-03-27 05:05:59 +01001598 &opus_max_playback_rate, &enable_opus_dtx);
buildbot@webrtc.org5d639b32014-09-10 07:57:12 +00001599 }
Brave Yao5225dd82015-03-26 07:39:19 +08001600
1601 // Set packet size if the AudioCodec param kCodecParamPTime is set.
1602 int ptime_ms = 0;
stefanba4c0e42016-02-04 04:12:24 -08001603 if (codec->GetParam(kCodecParamPTime, &ptime_ms)) {
solenberg26c8c912015-11-27 04:00:25 -08001604 if (!WebRtcVoiceCodecs::SetPTimeAsPacketSize(&send_codec, ptime_ms)) {
Brave Yao5225dd82015-03-26 07:39:19 +08001605 LOG(LS_WARNING) << "Failed to set packet size for codec "
1606 << send_codec.plname;
1607 return false;
1608 }
1609 }
henrike@webrtc.org704bf9e2014-02-27 17:52:04 +00001610 }
henrike@webrtc.org704bf9e2014-02-27 17:52:04 +00001611 }
1612
wu@webrtc.org05e7b442014-04-01 17:44:24 +00001613 if (nack_enabled_ != nack_enabled) {
1614 SetNack(channel, nack_enabled);
1615 nack_enabled_ = nack_enabled;
1616 }
stefanba4c0e42016-02-04 04:12:24 -08001617 if (!codec) {
henrike@webrtc.org704bf9e2014-02-27 17:52:04 +00001618 LOG(LS_WARNING) << "Received empty list of codecs.";
1619 return false;
1620 }
1621
1622 // Set the codec immediately, since SetVADStatus() depends on whether
1623 // the current codec is mono or stereo.
1624 if (!SetSendCodec(channel, send_codec))
1625 return false;
1626
buildbot@webrtc.org3ffa1f92014-07-02 19:51:26 +00001627 // FEC should be enabled after SetSendCodec.
1628 if (enable_codec_fec) {
1629 LOG(LS_INFO) << "Attempt to enable codec internal FEC on channel "
1630 << channel;
buildbot@webrtc.org3ffa1f92014-07-02 19:51:26 +00001631 if (engine()->voe()->codec()->SetFECStatus(channel, true) == -1) {
1632 // Enable codec internal FEC. Treat any failure as fatal internal error.
1633 LOG_RTCERR2(SetFECStatus, channel, true);
1634 return false;
1635 }
buildbot@webrtc.org3ffa1f92014-07-02 19:51:26 +00001636 }
1637
Minyue Li7100dcd2015-03-27 05:05:59 +01001638 if (IsCodec(send_codec, kOpusCodecName)) {
1639 // DTX and maxplaybackrate should be set after SetSendCodec. Because current
1640 // send codec has to be Opus.
1641
1642 // Set Opus internal DTX.
1643 LOG(LS_INFO) << "Attempt to "
solenbergbd138382015-11-20 16:08:07 -08001644 << (enable_opus_dtx ? "enable" : "disable")
Minyue Li7100dcd2015-03-27 05:05:59 +01001645 << " Opus DTX on channel "
buildbot@webrtc.org5d639b32014-09-10 07:57:12 +00001646 << channel;
Minyue Li7100dcd2015-03-27 05:05:59 +01001647 if (engine()->voe()->codec()->SetOpusDtx(channel, enable_opus_dtx)) {
1648 LOG_RTCERR2(SetOpusDtx, channel, enable_opus_dtx);
1649 return false;
1650 }
1651
1652 // If opus_max_playback_rate <= 0, the default maximum playback rate
1653 // (48 kHz) will be used.
1654 if (opus_max_playback_rate > 0) {
1655 LOG(LS_INFO) << "Attempt to set maximum playback rate to "
1656 << opus_max_playback_rate
1657 << " Hz on channel "
1658 << channel;
1659 if (engine()->voe()->codec()->SetOpusMaxPlaybackRate(
1660 channel, opus_max_playback_rate) == -1) {
1661 LOG_RTCERR2(SetOpusMaxPlaybackRate, channel, opus_max_playback_rate);
1662 return false;
1663 }
buildbot@webrtc.org5d639b32014-09-10 07:57:12 +00001664 }
buildbot@webrtc.org5d639b32014-09-10 07:57:12 +00001665 }
1666
henrike@webrtc.org704bf9e2014-02-27 17:52:04 +00001667 // Always update the |send_codec_| to the currently set send codec.
1668 send_codec_.reset(new webrtc::CodecInst(send_codec));
1669
minyue@webrtc.org26236952014-10-29 02:27:08 +00001670 if (send_bitrate_setting_) {
1671 SetSendBitrateInternal(send_bitrate_bps_);
henrike@webrtc.org704bf9e2014-02-27 17:52:04 +00001672 }
1673
Fredrik Solenbergb5727682015-12-04 15:22:19 +01001674 // Loop through the codecs list again to config the CN codec.
Fredrik Solenbergaf9fb212015-08-26 10:45:53 +02001675 for (const AudioCodec& codec : codecs) {
henrike@webrtc.org704bf9e2014-02-27 17:52:04 +00001676 // Ignore codecs we don't know about. The negotiation step should prevent
1677 // this, but double-check to be sure.
1678 webrtc::CodecInst voe_codec;
solenberg26c8c912015-11-27 04:00:25 -08001679 if (!WebRtcVoiceEngine::ToCodecInst(codec, &voe_codec)) {
Fredrik Solenbergaf9fb212015-08-26 10:45:53 +02001680 LOG(LS_WARNING) << "Unknown codec " << ToString(codec);
henrike@webrtc.org704bf9e2014-02-27 17:52:04 +00001681 continue;
1682 }
1683
Fredrik Solenbergb5727682015-12-04 15:22:19 +01001684 if (IsCodec(codec, kCnCodecName)) {
henrike@webrtc.org704bf9e2014-02-27 17:52:04 +00001685 // Turn voice activity detection/comfort noise on if supported.
1686 // Set the wideband CN payload type appropriately.
1687 // (narrowband always uses the static payload type 13).
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001688 webrtc::PayloadFrequencies cn_freq;
Fredrik Solenbergaf9fb212015-08-26 10:45:53 +02001689 switch (codec.clockrate) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001690 case 8000:
1691 cn_freq = webrtc::kFreq8000Hz;
1692 break;
1693 case 16000:
1694 cn_freq = webrtc::kFreq16000Hz;
1695 break;
1696 case 32000:
1697 cn_freq = webrtc::kFreq32000Hz;
1698 break;
1699 default:
Fredrik Solenbergaf9fb212015-08-26 10:45:53 +02001700 LOG(LS_WARNING) << "CN frequency " << codec.clockrate
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001701 << " not supported.";
1702 continue;
1703 }
wu@webrtc.orgcadf9042013-08-30 21:24:16 +00001704 // Set the CN payloadtype and the VAD status.
1705 // The CN payload type for 8000 Hz clockrate is fixed at 13.
1706 if (cn_freq != webrtc::kFreq8000Hz) {
1707 if (engine()->voe()->codec()->SetSendCNPayloadType(
Fredrik Solenbergaf9fb212015-08-26 10:45:53 +02001708 channel, codec.id, cn_freq) == -1) {
1709 LOG_RTCERR3(SetSendCNPayloadType, channel, codec.id, cn_freq);
wu@webrtc.orgcadf9042013-08-30 21:24:16 +00001710 // TODO(ajm): This failure condition will be removed from VoE.
1711 // Restore the return here when we update to a new enough webrtc.
1712 //
1713 // Not returning false because the SetSendCNPayloadType will fail if
1714 // the channel is already sending.
1715 // This can happen if the remote description is applied twice, for
1716 // example in the case of ROAP on top of JSEP, where both side will
1717 // send the offer.
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001718 }
wu@webrtc.orgcadf9042013-08-30 21:24:16 +00001719 }
wu@webrtc.orgcadf9042013-08-30 21:24:16 +00001720 // Only turn on VAD if we have a CN payload type that matches the
1721 // clockrate for the codec we are going to use.
Fredrik Solenbergaf9fb212015-08-26 10:45:53 +02001722 if (codec.clockrate == send_codec.plfreq && send_codec.channels != 2) {
Minyue Li7100dcd2015-03-27 05:05:59 +01001723 // TODO(minyue): If CN frequency == 48000 Hz is allowed, consider the
1724 // interaction between VAD and Opus FEC.
wu@webrtc.orgcadf9042013-08-30 21:24:16 +00001725 LOG(LS_INFO) << "Enabling VAD";
1726 if (engine()->voe()->codec()->SetVADStatus(channel, true) == -1) {
1727 LOG_RTCERR2(SetVADStatus, channel, true);
1728 return false;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001729 }
1730 }
1731 }
wu@webrtc.org1d1ffc92013-10-16 18:12:02 +00001732 }
wu@webrtc.orgcadf9042013-08-30 21:24:16 +00001733 return true;
1734}
1735
1736bool WebRtcVoiceMediaChannel::SetSendCodecs(
1737 const std::vector<AudioCodec>& codecs) {
solenberg566ef242015-11-06 15:34:49 -08001738 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
Fredrik Solenbergb5727682015-12-04 15:22:19 +01001739 // TODO(solenberg): Validate input - that payload types don't overlap, are
1740 // within range, filter out codecs we don't support,
1741 // redundant codecs etc.
solenbergd97ec302015-10-07 01:40:33 -07001742
Fredrik Solenbergb5727682015-12-04 15:22:19 +01001743 // Find the DTMF telephone event "codec" payload type.
1744 dtmf_payload_type_ = rtc::Optional<int>();
Fredrik Solenbergaf9fb212015-08-26 10:45:53 +02001745 for (const AudioCodec& codec : codecs) {
Fredrik Solenbergaf9fb212015-08-26 10:45:53 +02001746 if (IsCodec(codec, kDtmfCodecName)) {
Fredrik Solenbergb5727682015-12-04 15:22:19 +01001747 dtmf_payload_type_ = rtc::Optional<int>(codec.id);
1748 break;
wu@webrtc.orgcadf9042013-08-30 21:24:16 +00001749 }
1750 }
1751
1752 // Cache the codecs in order to configure the channel created later.
1753 send_codecs_ = codecs;
solenbergc96df772015-10-21 13:01:53 -07001754 for (const auto& ch : send_streams_) {
Fredrik Solenbergaf9fb212015-08-26 10:45:53 +02001755 if (!SetSendCodecs(ch.second->channel(), codecs)) {
wu@webrtc.orgcadf9042013-08-30 21:24:16 +00001756 return false;
1757 }
1758 }
1759
wu@webrtc.org05e7b442014-04-01 17:44:24 +00001760 // Set nack status on receive channels and update |nack_enabled_|.
solenberg7add0582015-11-20 09:59:34 -08001761 for (const auto& ch : recv_streams_) {
solenberg0a617e22015-10-20 15:49:38 -07001762 SetNack(ch.second->channel(), nack_enabled_);
wu@webrtc.org9dba5252013-08-05 20:36:57 +00001763 }
solenberg0a617e22015-10-20 15:49:38 -07001764
stefanba4c0e42016-02-04 04:12:24 -08001765 // Check if the transport cc feedback has changed on the preferred send codec,
1766 // and in that case reconfigure all receive streams.
1767 webrtc::CodecInst voe_codec;
1768 int red_payload_type;
1769 const AudioCodec* send_codec = WebRtcVoiceCodecs::GetPreferredCodec(
1770 send_codecs_, &voe_codec, &red_payload_type);
1771 if (send_codec) {
1772 bool transport_cc = HasTransportCc(*send_codec);
1773 if (transport_cc_enabled_ != transport_cc) {
1774 LOG(LS_INFO) << "Recreate all the receive streams because the send "
1775 "codec has changed.";
1776 transport_cc_enabled_ = transport_cc;
1777 for (auto& kv : recv_streams_) {
1778 RTC_DCHECK(kv.second != nullptr);
1779 kv.second->RecreateAudioReceiveStream(transport_cc_enabled_);
1780 }
1781 }
1782 }
1783
solenberg0a617e22015-10-20 15:49:38 -07001784 return true;
wu@webrtc.org9dba5252013-08-05 20:36:57 +00001785}
1786
wu@webrtc.orgcadf9042013-08-30 21:24:16 +00001787void WebRtcVoiceMediaChannel::SetNack(int channel, bool nack_enabled) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001788 if (nack_enabled) {
wu@webrtc.orgcadf9042013-08-30 21:24:16 +00001789 LOG(LS_INFO) << "Enabling NACK for channel " << channel;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001790 engine()->voe()->rtp()->SetNACKStatus(channel, true, kNackMaxPackets);
1791 } else {
wu@webrtc.orgcadf9042013-08-30 21:24:16 +00001792 LOG(LS_INFO) << "Disabling NACK for channel " << channel;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001793 engine()->voe()->rtp()->SetNACKStatus(channel, false, 0);
1794 }
1795}
1796
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001797bool WebRtcVoiceMediaChannel::SetSendCodec(
wu@webrtc.org9dba5252013-08-05 20:36:57 +00001798 int channel, const webrtc::CodecInst& send_codec) {
1799 LOG(LS_INFO) << "Send channel " << channel << " selected voice codec "
1800 << ToString(send_codec) << ", bitrate=" << send_codec.rate;
1801
wu@webrtc.org05e7b442014-04-01 17:44:24 +00001802 webrtc::CodecInst current_codec;
1803 if (engine()->voe()->codec()->GetSendCodec(channel, current_codec) == 0 &&
1804 (send_codec == current_codec)) {
1805 // Codec is already configured, we can return without setting it again.
1806 return true;
1807 }
1808
wu@webrtc.org9dba5252013-08-05 20:36:57 +00001809 if (engine()->voe()->codec()->SetSendCodec(channel, send_codec) == -1) {
1810 LOG_RTCERR2(SetSendCodec, channel, ToString(send_codec));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001811 return false;
1812 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001813 return true;
1814}
1815
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001816bool WebRtcVoiceMediaChannel::SetPlayout(bool playout) {
1817 desired_playout_ = playout;
1818 return ChangePlayout(desired_playout_);
1819}
1820
1821bool WebRtcVoiceMediaChannel::PausePlayout() {
1822 return ChangePlayout(false);
1823}
1824
1825bool WebRtcVoiceMediaChannel::ResumePlayout() {
1826 return ChangePlayout(desired_playout_);
1827}
1828
1829bool WebRtcVoiceMediaChannel::ChangePlayout(bool playout) {
solenberg566ef242015-11-06 15:34:49 -08001830 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001831 if (playout_ == playout) {
1832 return true;
1833 }
1834
solenberg7add0582015-11-20 09:59:34 -08001835 for (const auto& ch : recv_streams_) {
Fredrik Solenbergaf9fb212015-08-26 10:45:53 +02001836 if (!SetPlayout(ch.second->channel(), playout)) {
henrike@webrtc.org1e09a712013-07-26 19:17:59 +00001837 LOG(LS_ERROR) << "SetPlayout " << playout << " on channel "
Fredrik Solenbergaf9fb212015-08-26 10:45:53 +02001838 << ch.second->channel() << " failed";
solenberg1ac56142015-10-13 03:58:19 -07001839 return false;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001840 }
1841 }
solenberg1ac56142015-10-13 03:58:19 -07001842 playout_ = playout;
1843 return true;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001844}
1845
1846bool WebRtcVoiceMediaChannel::SetSend(SendFlags send) {
1847 desired_send_ = send;
solenbergc96df772015-10-21 13:01:53 -07001848 if (!send_streams_.empty()) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001849 return ChangeSend(desired_send_);
solenbergc96df772015-10-21 13:01:53 -07001850 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001851 return true;
1852}
1853
1854bool WebRtcVoiceMediaChannel::PauseSend() {
1855 return ChangeSend(SEND_NOTHING);
1856}
1857
1858bool WebRtcVoiceMediaChannel::ResumeSend() {
1859 return ChangeSend(desired_send_);
1860}
1861
1862bool WebRtcVoiceMediaChannel::ChangeSend(SendFlags send) {
1863 if (send_ == send) {
1864 return true;
1865 }
1866
solenberg246b8172015-12-08 09:50:23 -08001867 // Apply channel specific options when channel is enabled for sending.
solenberg63b34542015-09-29 06:06:31 -07001868 if (send == SEND_MICROPHONE) {
1869 engine()->ApplyOptions(options_);
1870 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001871
wu@webrtc.org9dba5252013-08-05 20:36:57 +00001872 // Change the settings on each send channel.
solenbergc96df772015-10-21 13:01:53 -07001873 for (const auto& ch : send_streams_) {
solenberg63b34542015-09-29 06:06:31 -07001874 if (!ChangeSend(ch.second->channel(), send)) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001875 return false;
solenberg63b34542015-09-29 06:06:31 -07001876 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001877 }
wu@webrtc.org9dba5252013-08-05 20:36:57 +00001878
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001879 send_ = send;
1880 return true;
1881}
1882
wu@webrtc.org9dba5252013-08-05 20:36:57 +00001883bool WebRtcVoiceMediaChannel::ChangeSend(int channel, SendFlags send) {
1884 if (send == SEND_MICROPHONE) {
1885 if (engine()->voe()->base()->StartSend(channel) == -1) {
1886 LOG_RTCERR1(StartSend, channel);
1887 return false;
1888 }
wu@webrtc.org9dba5252013-08-05 20:36:57 +00001889 } else { // SEND_NOTHING
henrikg91d6ede2015-09-17 00:24:34 -07001890 RTC_DCHECK(send == SEND_NOTHING);
wu@webrtc.org9dba5252013-08-05 20:36:57 +00001891 if (engine()->voe()->base()->StopSend(channel) == -1) {
1892 LOG_RTCERR1(StopSend, channel);
1893 return false;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001894 }
1895 }
1896
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001897 return true;
1898}
1899
Peter Boström0c4e06b2015-10-07 12:23:21 +02001900bool WebRtcVoiceMediaChannel::SetAudioSend(uint32_t ssrc,
1901 bool enable,
solenberg1dd98f32015-09-10 01:57:14 -07001902 const AudioOptions* options,
1903 AudioRenderer* renderer) {
solenberg566ef242015-11-06 15:34:49 -08001904 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
solenberg1dd98f32015-09-10 01:57:14 -07001905 // TODO(solenberg): The state change should be fully rolled back if any one of
1906 // these calls fail.
1907 if (!SetLocalRenderer(ssrc, renderer)) {
1908 return false;
1909 }
solenbergdfc8f4f2015-10-01 02:31:10 -07001910 if (!MuteStream(ssrc, !enable)) {
solenberg1dd98f32015-09-10 01:57:14 -07001911 return false;
1912 }
solenbergdfc8f4f2015-10-01 02:31:10 -07001913 if (enable && options) {
solenberg1dd98f32015-09-10 01:57:14 -07001914 return SetOptions(*options);
1915 }
1916 return true;
1917}
1918
solenberg0a617e22015-10-20 15:49:38 -07001919int WebRtcVoiceMediaChannel::CreateVoEChannel() {
1920 int id = engine()->CreateVoEChannel();
1921 if (id == -1) {
1922 LOG_RTCERR0(CreateVoEChannel);
1923 return -1;
wu@webrtc.org9dba5252013-08-05 20:36:57 +00001924 }
solenberg0a617e22015-10-20 15:49:38 -07001925 if (engine()->voe()->network()->RegisterExternalTransport(id, *this) == -1) {
1926 LOG_RTCERR2(RegisterExternalTransport, id, this);
1927 engine()->voe()->base()->DeleteChannel(id);
1928 return -1;
1929 }
1930 return id;
wu@webrtc.org9dba5252013-08-05 20:36:57 +00001931}
1932
solenberg7add0582015-11-20 09:59:34 -08001933bool WebRtcVoiceMediaChannel::DeleteVoEChannel(int channel) {
wu@webrtc.org9dba5252013-08-05 20:36:57 +00001934 if (engine()->voe()->network()->DeRegisterExternalTransport(channel) == -1) {
1935 LOG_RTCERR1(DeRegisterExternalTransport, channel);
1936 }
wu@webrtc.org9dba5252013-08-05 20:36:57 +00001937 if (engine()->voe()->base()->DeleteChannel(channel) == -1) {
1938 LOG_RTCERR1(DeleteChannel, channel);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001939 return false;
1940 }
wu@webrtc.org9dba5252013-08-05 20:36:57 +00001941 return true;
1942}
henrike@webrtc.org1e09a712013-07-26 19:17:59 +00001943
wu@webrtc.org9dba5252013-08-05 20:36:57 +00001944bool WebRtcVoiceMediaChannel::AddSendStream(const StreamParams& sp) {
solenberg566ef242015-11-06 15:34:49 -08001945 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
solenberg0a617e22015-10-20 15:49:38 -07001946 LOG(LS_INFO) << "AddSendStream: " << sp.ToString();
1947
1948 uint32_t ssrc = sp.first_ssrc();
1949 RTC_DCHECK(0 != ssrc);
1950
1951 if (GetSendChannelId(ssrc) != -1) {
1952 LOG(LS_ERROR) << "Stream already exists with ssrc " << ssrc;
wu@webrtc.org9dba5252013-08-05 20:36:57 +00001953 return false;
1954 }
1955
solenberg0a617e22015-10-20 15:49:38 -07001956 // Create a new channel for sending audio data.
1957 int channel = CreateVoEChannel();
1958 if (channel == -1) {
1959 return false;
wu@webrtc.org9dba5252013-08-05 20:36:57 +00001960 }
wu@webrtc.org9dba5252013-08-05 20:36:57 +00001961
solenbergc96df772015-10-21 13:01:53 -07001962 // Save the channel to send_streams_, so that RemoveSendStream() can still
wu@webrtc.org9dba5252013-08-05 20:36:57 +00001963 // delete the channel in case failure happens below.
mallinath@webrtc.org67ee6b92014-02-03 16:57:16 +00001964 webrtc::AudioTransport* audio_transport =
1965 engine()->voe()->base()->audio_transport();
solenberg3a941542015-11-16 07:34:50 -08001966 send_streams_.insert(std::make_pair(ssrc, new WebRtcAudioSendStream(
1967 channel, audio_transport, ssrc, sp.cname, send_rtp_extensions_, call_)));
wu@webrtc.org9dba5252013-08-05 20:36:57 +00001968
solenberg0a617e22015-10-20 15:49:38 -07001969 // Set the current codecs to be used for the new channel. We need to do this
1970 // after adding the channel to send_channels_, because of how max bitrate is
1971 // currently being configured by SetSendCodec().
1972 if (!send_codecs_.empty() && !SetSendCodecs(channel, send_codecs_)) {
1973 RemoveSendStream(ssrc);
wu@webrtc.org9dba5252013-08-05 20:36:57 +00001974 return false;
1975 }
1976
1977 // At this point the channel's local SSRC has been updated. If the channel is
solenberg0a617e22015-10-20 15:49:38 -07001978 // the first send channel make sure that all the receive channels are updated
1979 // with the same SSRC in order to send receiver reports.
solenbergc96df772015-10-21 13:01:53 -07001980 if (send_streams_.size() == 1) {
solenberg0a617e22015-10-20 15:49:38 -07001981 receiver_reports_ssrc_ = ssrc;
solenberg7add0582015-11-20 09:59:34 -08001982 for (const auto& stream : recv_streams_) {
1983 int recv_channel = stream.second->channel();
solenberg0a617e22015-10-20 15:49:38 -07001984 if (engine()->voe()->rtp()->SetLocalSSRC(recv_channel, ssrc) != 0) {
solenberg7add0582015-11-20 09:59:34 -08001985 LOG_RTCERR2(SetLocalSSRC, recv_channel, ssrc);
solenberg1ac56142015-10-13 03:58:19 -07001986 return false;
wu@webrtc.org9dba5252013-08-05 20:36:57 +00001987 }
solenberg0a617e22015-10-20 15:49:38 -07001988 engine()->voe()->base()->AssociateSendChannel(recv_channel, channel);
1989 LOG(LS_INFO) << "VoiceEngine channel #" << recv_channel
1990 << " is associated with channel #" << channel << ".";
wu@webrtc.org9dba5252013-08-05 20:36:57 +00001991 }
1992 }
1993
wu@webrtc.org9dba5252013-08-05 20:36:57 +00001994 return ChangeSend(channel, desired_send_);
1995}
1996
Peter Boström0c4e06b2015-10-07 12:23:21 +02001997bool WebRtcVoiceMediaChannel::RemoveSendStream(uint32_t ssrc) {
solenberg566ef242015-11-06 15:34:49 -08001998 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
solenberg3a941542015-11-16 07:34:50 -08001999 LOG(LS_INFO) << "RemoveSendStream: " << ssrc;
2000
solenbergc96df772015-10-21 13:01:53 -07002001 auto it = send_streams_.find(ssrc);
2002 if (it == send_streams_.end()) {
wu@webrtc.org9dba5252013-08-05 20:36:57 +00002003 LOG(LS_WARNING) << "Try to remove stream with ssrc " << ssrc
2004 << " which doesn't exist.";
2005 return false;
2006 }
2007
mallinath@webrtc.org67ee6b92014-02-03 16:57:16 +00002008 int channel = it->second->channel();
wu@webrtc.org9dba5252013-08-05 20:36:57 +00002009 ChangeSend(channel, SEND_NOTHING);
2010
solenberg7add0582015-11-20 09:59:34 -08002011 // Clean up and delete the send stream+channel.
solenberg0a617e22015-10-20 15:49:38 -07002012 LOG(LS_INFO) << "Removing audio send stream " << ssrc
2013 << " with VoiceEngine channel #" << channel << ".";
solenberg7add0582015-11-20 09:59:34 -08002014 delete it->second;
2015 send_streams_.erase(it);
2016 if (!DeleteVoEChannel(channel)) {
solenberg0a617e22015-10-20 15:49:38 -07002017 return false;
wu@webrtc.org9dba5252013-08-05 20:36:57 +00002018 }
solenbergc96df772015-10-21 13:01:53 -07002019 if (send_streams_.empty()) {
wu@webrtc.org9dba5252013-08-05 20:36:57 +00002020 ChangeSend(SEND_NOTHING);
solenberg0a617e22015-10-20 15:49:38 -07002021 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002022 return true;
2023}
2024
2025bool WebRtcVoiceMediaChannel::AddRecvStream(const StreamParams& sp) {
solenberg566ef242015-11-06 15:34:49 -08002026 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
solenbergd97ec302015-10-07 01:40:33 -07002027 LOG(LS_INFO) << "AddRecvStream: " << sp.ToString();
2028
solenberg0b675462015-10-09 01:37:09 -07002029 if (!ValidateStreamParams(sp)) {
wu@webrtc.org78187522013-10-07 23:32:02 +00002030 return false;
2031 }
2032
solenberg7add0582015-11-20 09:59:34 -08002033 const uint32_t ssrc = sp.first_ssrc();
solenberg0b675462015-10-09 01:37:09 -07002034 if (ssrc == 0) {
2035 LOG(LS_WARNING) << "AddRecvStream with ssrc==0 is not supported.";
2036 return false;
2037 }
2038
solenberg1ac56142015-10-13 03:58:19 -07002039 // Remove the default receive stream if one had been created with this ssrc;
2040 // we'll recreate it then.
2041 if (IsDefaultRecvStream(ssrc)) {
2042 RemoveRecvStream(ssrc);
2043 }
solenberg0b675462015-10-09 01:37:09 -07002044
solenberg7add0582015-11-20 09:59:34 -08002045 if (GetReceiveChannelId(ssrc) != -1) {
henrike@webrtc.org1e09a712013-07-26 19:17:59 +00002046 LOG(LS_ERROR) << "Stream already exists with ssrc " << ssrc;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002047 return false;
2048 }
Fredrik Solenberg4b60c732015-05-07 14:07:48 +02002049
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002050 // Create a new channel for receiving audio data.
solenberg7add0582015-11-20 09:59:34 -08002051 const int channel = CreateVoEChannel();
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002052 if (channel == -1) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002053 return false;
2054 }
Minyue2013aec2015-05-13 14:14:42 +02002055
solenberg1ac56142015-10-13 03:58:19 -07002056 // Turn off all supported codecs.
solenberg26c8c912015-11-27 04:00:25 -08002057 // TODO(solenberg): Remove once "no codecs" is the default state of a stream.
2058 for (webrtc::CodecInst voe_codec : webrtc::acm2::RentACodec::Database()) {
2059 voe_codec.pltype = -1;
2060 if (engine()->voe()->codec()->SetRecPayloadType(channel, voe_codec) == -1) {
2061 LOG_RTCERR2(SetRecPayloadType, channel, ToString(voe_codec));
2062 DeleteVoEChannel(channel);
2063 return false;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002064 }
2065 }
2066
solenberg1ac56142015-10-13 03:58:19 -07002067 // Only enable those configured for this channel.
2068 for (const auto& codec : recv_codecs_) {
2069 webrtc::CodecInst voe_codec;
solenberg26c8c912015-11-27 04:00:25 -08002070 if (WebRtcVoiceEngine::ToCodecInst(codec, &voe_codec)) {
solenberg1ac56142015-10-13 03:58:19 -07002071 voe_codec.pltype = codec.id;
2072 if (engine()->voe()->codec()->SetRecPayloadType(
2073 channel, voe_codec) == -1) {
2074 LOG_RTCERR2(SetRecPayloadType, channel, ToString(voe_codec));
solenberg7add0582015-11-20 09:59:34 -08002075 DeleteVoEChannel(channel);
solenberg1ac56142015-10-13 03:58:19 -07002076 return false;
2077 }
henrike@webrtc.org1e09a712013-07-26 19:17:59 +00002078 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002079 }
solenberg8fb30c32015-10-13 03:06:58 -07002080
solenberg7add0582015-11-20 09:59:34 -08002081 const int send_channel = GetSendChannelId(receiver_reports_ssrc_);
2082 if (send_channel != -1) {
2083 // Associate receive channel with first send channel (so the receive channel
2084 // can obtain RTT from the send channel)
2085 engine()->voe()->base()->AssociateSendChannel(channel, send_channel);
2086 LOG(LS_INFO) << "VoiceEngine channel #" << channel
2087 << " is associated with channel #" << send_channel << ".";
buildbot@webrtc.org150835e2014-05-06 15:54:38 +00002088 }
2089
stefanba4c0e42016-02-04 04:12:24 -08002090 transport_cc_enabled_ =
2091 !send_codecs_.empty() ? HasTransportCc(send_codecs_[0]) : false;
2092
2093 recv_streams_.insert(std::make_pair(
2094 ssrc, new WebRtcAudioReceiveStream(channel, ssrc, receiver_reports_ssrc_,
2095 transport_cc_enabled_, sp.sync_label,
2096 recv_rtp_extensions_, call_)));
solenberg7add0582015-11-20 09:59:34 -08002097
2098 SetNack(channel, nack_enabled_);
solenberg1ac56142015-10-13 03:58:19 -07002099 SetPlayout(channel, playout_);
solenberg7add0582015-11-20 09:59:34 -08002100
solenberg1ac56142015-10-13 03:58:19 -07002101 return true;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002102}
2103
Peter Boström0c4e06b2015-10-07 12:23:21 +02002104bool WebRtcVoiceMediaChannel::RemoveRecvStream(uint32_t ssrc) {
solenberg566ef242015-11-06 15:34:49 -08002105 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
solenbergd97ec302015-10-07 01:40:33 -07002106 LOG(LS_INFO) << "RemoveRecvStream: " << ssrc;
2107
solenberg7add0582015-11-20 09:59:34 -08002108 const auto it = recv_streams_.find(ssrc);
2109 if (it == recv_streams_.end()) {
wu@webrtc.org9dba5252013-08-05 20:36:57 +00002110 LOG(LS_WARNING) << "Try to remove stream with ssrc " << ssrc
2111 << " which doesn't exist.";
henrike@webrtc.org1e09a712013-07-26 19:17:59 +00002112 return false;
wu@webrtc.org9dba5252013-08-05 20:36:57 +00002113 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002114
solenberg1ac56142015-10-13 03:58:19 -07002115 // Deregister default channel, if that's the one being destroyed.
2116 if (IsDefaultRecvStream(ssrc)) {
2117 default_recv_ssrc_ = -1;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002118 }
henrike@webrtc.org1e09a712013-07-26 19:17:59 +00002119
solenberg7add0582015-11-20 09:59:34 -08002120 const int channel = it->second->channel();
2121
2122 // Clean up and delete the receive stream+channel.
2123 LOG(LS_INFO) << "Removing audio receive stream " << ssrc
mallinath@webrtc.org67ee6b92014-02-03 16:57:16 +00002124 << " with VoiceEngine channel #" << channel << ".";
Tommif888bb52015-12-12 01:37:01 +01002125 it->second->SetRawAudioSink(nullptr);
solenberg7add0582015-11-20 09:59:34 -08002126 delete it->second;
2127 recv_streams_.erase(it);
2128 return DeleteVoEChannel(channel);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002129}
2130
Peter Boström0c4e06b2015-10-07 12:23:21 +02002131bool WebRtcVoiceMediaChannel::SetLocalRenderer(uint32_t ssrc,
henrike@webrtc.org1e09a712013-07-26 19:17:59 +00002132 AudioRenderer* renderer) {
solenbergc96df772015-10-21 13:01:53 -07002133 auto it = send_streams_.find(ssrc);
2134 if (it == send_streams_.end()) {
wu@webrtc.org9dba5252013-08-05 20:36:57 +00002135 if (renderer) {
2136 // Return an error if trying to set a valid renderer with an invalid ssrc.
2137 LOG(LS_ERROR) << "SetLocalRenderer failed with ssrc "<< ssrc;
2138 return false;
2139 }
2140
2141 // The channel likely has gone away, do nothing.
henrike@webrtc.org1e09a712013-07-26 19:17:59 +00002142 return true;
henrike@webrtc.org1e09a712013-07-26 19:17:59 +00002143 }
2144
solenberg1ac56142015-10-13 03:58:19 -07002145 if (renderer) {
mallinath@webrtc.org67ee6b92014-02-03 16:57:16 +00002146 it->second->Start(renderer);
solenberg1ac56142015-10-13 03:58:19 -07002147 } else {
mallinath@webrtc.org67ee6b92014-02-03 16:57:16 +00002148 it->second->Stop();
solenberg1ac56142015-10-13 03:58:19 -07002149 }
henrike@webrtc.org1e09a712013-07-26 19:17:59 +00002150
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002151 return true;
2152}
2153
2154bool WebRtcVoiceMediaChannel::GetActiveStreams(
2155 AudioInfo::StreamList* actives) {
solenberg566ef242015-11-06 15:34:49 -08002156 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002157 actives->clear();
solenberg7add0582015-11-20 09:59:34 -08002158 for (const auto& ch : recv_streams_) {
Fredrik Solenbergaf9fb212015-08-26 10:45:53 +02002159 int level = GetOutputLevel(ch.second->channel());
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002160 if (level > 0) {
Fredrik Solenbergaf9fb212015-08-26 10:45:53 +02002161 actives->push_back(std::make_pair(ch.first, level));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002162 }
2163 }
2164 return true;
2165}
2166
2167int WebRtcVoiceMediaChannel::GetOutputLevel() {
solenberg566ef242015-11-06 15:34:49 -08002168 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
solenberg1ac56142015-10-13 03:58:19 -07002169 int highest = 0;
solenberg7add0582015-11-20 09:59:34 -08002170 for (const auto& ch : recv_streams_) {
solenberg8fb30c32015-10-13 03:06:58 -07002171 highest = std::max(GetOutputLevel(ch.second->channel()), highest);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002172 }
2173 return highest;
2174}
2175
2176int WebRtcVoiceMediaChannel::GetTimeSinceLastTyping() {
2177 int ret;
2178 if (engine()->voe()->processing()->TimeSinceLastTyping(ret) == -1) {
2179 // In case of error, log the info and continue
2180 LOG_RTCERR0(TimeSinceLastTyping);
2181 ret = -1;
2182 } else {
2183 ret *= 1000; // We return ms, webrtc returns seconds.
2184 }
2185 return ret;
2186}
2187
2188void WebRtcVoiceMediaChannel::SetTypingDetectionParameters(int time_window,
2189 int cost_per_typing, int reporting_threshold, int penalty_decay,
2190 int type_event_delay) {
2191 if (engine()->voe()->processing()->SetTypingDetectionParameters(
2192 time_window, cost_per_typing,
2193 reporting_threshold, penalty_decay, type_event_delay) == -1) {
2194 // In case of error, log the info and continue
2195 LOG_RTCERR5(SetTypingDetectionParameters, time_window,
2196 cost_per_typing, reporting_threshold, penalty_decay,
2197 type_event_delay);
2198 }
2199}
2200
solenberg4bac9c52015-10-09 02:32:53 -07002201bool WebRtcVoiceMediaChannel::SetOutputVolume(uint32_t ssrc, double volume) {
solenberg566ef242015-11-06 15:34:49 -08002202 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
solenberg1ac56142015-10-13 03:58:19 -07002203 if (ssrc == 0) {
2204 default_recv_volume_ = volume;
2205 if (default_recv_ssrc_ == -1) {
2206 return true;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002207 }
solenberg1ac56142015-10-13 03:58:19 -07002208 ssrc = static_cast<uint32_t>(default_recv_ssrc_);
2209 }
2210 int ch_id = GetReceiveChannelId(ssrc);
2211 if (ch_id < 0) {
2212 LOG(LS_WARNING) << "Cannot find channel for ssrc:" << ssrc;
2213 return false;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002214 }
2215
solenberg1ac56142015-10-13 03:58:19 -07002216 if (-1 == engine()->voe()->volume()->SetChannelOutputVolumeScaling(ch_id,
2217 volume)) {
2218 LOG_RTCERR2(SetChannelOutputVolumeScaling, ch_id, volume);
2219 return false;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002220 }
solenberg1ac56142015-10-13 03:58:19 -07002221 LOG(LS_INFO) << "SetOutputVolume to " << volume
2222 << " for channel " << ch_id << " and ssrc " << ssrc;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002223 return true;
2224}
2225
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002226bool WebRtcVoiceMediaChannel::CanInsertDtmf() {
Fredrik Solenbergb5727682015-12-04 15:22:19 +01002227 return dtmf_payload_type_ ? true : false;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002228}
2229
solenberg1d63dd02015-12-02 12:35:09 -08002230bool WebRtcVoiceMediaChannel::InsertDtmf(uint32_t ssrc, int event,
2231 int duration) {
solenberg566ef242015-11-06 15:34:49 -08002232 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
Fredrik Solenbergb5727682015-12-04 15:22:19 +01002233 LOG(LS_INFO) << "WebRtcVoiceMediaChannel::InsertDtmf";
2234 if (!dtmf_payload_type_) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002235 return false;
2236 }
2237
Fredrik Solenbergb5727682015-12-04 15:22:19 +01002238 // Figure out which WebRtcAudioSendStream to send the event on.
2239 auto it = ssrc != 0 ? send_streams_.find(ssrc) : send_streams_.begin();
2240 if (it == send_streams_.end()) {
2241 LOG(LS_WARNING) << "The specified ssrc " << ssrc << " is not in use.";
solenberg1d63dd02015-12-02 12:35:09 -08002242 return false;
2243 }
Fredrik Solenbergb5727682015-12-04 15:22:19 +01002244 if (event < kMinTelephoneEventCode ||
2245 event > kMaxTelephoneEventCode) {
2246 LOG(LS_WARNING) << "DTMF event code " << event << " out of range.";
solenberg1d63dd02015-12-02 12:35:09 -08002247 return false;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002248 }
Fredrik Solenbergb5727682015-12-04 15:22:19 +01002249 if (duration < kMinTelephoneEventDuration ||
2250 duration > kMaxTelephoneEventDuration) {
2251 LOG(LS_WARNING) << "DTMF event duration " << duration << " out of range.";
2252 return false;
2253 }
2254 return it->second->SendTelephoneEvent(*dtmf_payload_type_, event, duration);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002255}
2256
wu@webrtc.orga9890802013-12-13 00:21:03 +00002257void WebRtcVoiceMediaChannel::OnPacketReceived(
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00002258 rtc::Buffer* packet, const rtc::PacketTime& packet_time) {
solenberg566ef242015-11-06 15:34:49 -08002259 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
Fredrik Solenberg4b60c732015-05-07 14:07:48 +02002260
solenberg1ac56142015-10-13 03:58:19 -07002261 uint32_t ssrc = 0;
2262 if (!GetRtpSsrc(packet->data(), packet->size(), &ssrc)) {
2263 return;
2264 }
2265
solenberg7e63ef02015-11-20 00:19:43 -08002266 // If we don't have a default channel, and the SSRC is unknown, create a
2267 // default channel.
2268 if (default_recv_ssrc_ == -1 && GetReceiveChannelId(ssrc) == -1) {
solenberg1ac56142015-10-13 03:58:19 -07002269 StreamParams sp;
2270 sp.ssrcs.push_back(ssrc);
2271 LOG(LS_INFO) << "Creating default receive stream for SSRC=" << ssrc << ".";
2272 if (!AddRecvStream(sp)) {
2273 LOG(LS_WARNING) << "Could not create default receive stream.";
2274 return;
2275 }
2276 default_recv_ssrc_ = ssrc;
2277 SetOutputVolume(default_recv_ssrc_, default_recv_volume_);
deadbeef884f5852016-01-15 09:20:04 -08002278 if (default_sink_) {
2279 rtc::scoped_ptr<webrtc::AudioSinkInterface> proxy_sink(
2280 new ProxySink(default_sink_.get()));
2281 SetRawAudioSink(default_recv_ssrc_, std::move(proxy_sink));
2282 }
solenberg1ac56142015-10-13 03:58:19 -07002283 }
2284
2285 // Forward packet to Call. If the SSRC is unknown we'll return after this.
Fredrik Solenberg709ed672015-09-15 12:26:33 +02002286 const webrtc::PacketTime webrtc_packet_time(packet_time.timestamp,
2287 packet_time.not_before);
solenberg1ac56142015-10-13 03:58:19 -07002288 webrtc::PacketReceiver::DeliveryStatus delivery_result =
2289 call_->Receiver()->DeliverPacket(webrtc::MediaType::AUDIO,
2290 reinterpret_cast<const uint8_t*>(packet->data()), packet->size(),
2291 webrtc_packet_time);
2292 if (webrtc::PacketReceiver::DELIVERY_OK != delivery_result) {
solenberg7e63ef02015-11-20 00:19:43 -08002293 // If the SSRC is unknown here, route it to the default channel, if we have
2294 // one. See: https://bugs.chromium.org/p/webrtc/issues/detail?id=5208
2295 if (default_recv_ssrc_ == -1) {
2296 return;
2297 } else {
2298 ssrc = default_recv_ssrc_;
2299 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002300 }
2301
solenberg1ac56142015-10-13 03:58:19 -07002302 // Find the channel to send this packet to. It must exist since webrtc::Call
2303 // was able to demux the packet.
2304 int channel = GetReceiveChannelId(ssrc);
2305 RTC_DCHECK(channel != -1);
2306
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002307 // Pass it off to the decoder.
henrike@webrtc.org28654cb2013-07-22 21:07:49 +00002308 engine()->voe()->network()->ReceivedRTPPacket(
solenberg1ac56142015-10-13 03:58:19 -07002309 channel, packet->data(), packet->size(), webrtc_packet_time);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002310}
2311
wu@webrtc.orga9890802013-12-13 00:21:03 +00002312void WebRtcVoiceMediaChannel::OnRtcpReceived(
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00002313 rtc::Buffer* packet, const rtc::PacketTime& packet_time) {
solenberg566ef242015-11-06 15:34:49 -08002314 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
Fredrik Solenberg4b60c732015-05-07 14:07:48 +02002315
Fredrik Solenberg709ed672015-09-15 12:26:33 +02002316 // Forward packet to Call as well.
2317 const webrtc::PacketTime webrtc_packet_time(packet_time.timestamp,
2318 packet_time.not_before);
2319 call_->Receiver()->DeliverPacket(webrtc::MediaType::AUDIO,
2320 reinterpret_cast<const uint8_t*>(packet->data()), packet->size(),
2321 webrtc_packet_time);
Fredrik Solenberg4b60c732015-05-07 14:07:48 +02002322
wu@webrtc.org9dba5252013-08-05 20:36:57 +00002323 // Sending channels need all RTCP packets with feedback information.
2324 // Even sender reports can contain attached report blocks.
2325 // Receiving channels need sender reports in order to create
2326 // correct receiver reports.
2327 int type = 0;
kwiberg@webrtc.orgeebcab52015-03-24 09:19:06 +00002328 if (!GetRtcpType(packet->data(), packet->size(), &type)) {
wu@webrtc.org9dba5252013-08-05 20:36:57 +00002329 LOG(LS_WARNING) << "Failed to parse type from received RTCP packet";
2330 return;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002331 }
2332
solenberg0b675462015-10-09 01:37:09 -07002333 // If it is a sender report, find the receive channel that is listening.
wu@webrtc.org9dba5252013-08-05 20:36:57 +00002334 if (type == kRtcpTypeSR) {
solenberg0b675462015-10-09 01:37:09 -07002335 uint32_t ssrc = 0;
2336 if (!GetRtcpSsrc(packet->data(), packet->size(), &ssrc)) {
2337 return;
2338 }
2339 int recv_channel_id = GetReceiveChannelId(ssrc);
2340 if (recv_channel_id != -1) {
wu@webrtc.org9dba5252013-08-05 20:36:57 +00002341 engine()->voe()->network()->ReceivedRTCPPacket(
solenberg0b675462015-10-09 01:37:09 -07002342 recv_channel_id, packet->data(), packet->size());
wu@webrtc.org9dba5252013-08-05 20:36:57 +00002343 }
2344 }
2345
2346 // SR may continue RR and any RR entry may correspond to any one of the send
2347 // channels. So all RTCP packets must be forwarded all send channels. VoE
2348 // will filter out RR internally.
solenbergc96df772015-10-21 13:01:53 -07002349 for (const auto& ch : send_streams_) {
wu@webrtc.org9dba5252013-08-05 20:36:57 +00002350 engine()->voe()->network()->ReceivedRTCPPacket(
Fredrik Solenbergaf9fb212015-08-26 10:45:53 +02002351 ch.second->channel(), packet->data(), packet->size());
wu@webrtc.org9dba5252013-08-05 20:36:57 +00002352 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002353}
2354
Peter Boström0c4e06b2015-10-07 12:23:21 +02002355bool WebRtcVoiceMediaChannel::MuteStream(uint32_t ssrc, bool muted) {
solenberg566ef242015-11-06 15:34:49 -08002356 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
solenberg0a617e22015-10-20 15:49:38 -07002357 int channel = GetSendChannelId(ssrc);
wu@webrtc.org9dba5252013-08-05 20:36:57 +00002358 if (channel == -1) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002359 LOG(LS_WARNING) << "The specified ssrc " << ssrc << " is not in use.";
2360 return false;
2361 }
wu@webrtc.org9dba5252013-08-05 20:36:57 +00002362 if (engine()->voe()->volume()->SetInputMute(channel, muted) == -1) {
2363 LOG_RTCERR2(SetInputMute, channel, muted);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002364 return false;
2365 }
buildbot@webrtc.org6b21b712014-07-31 15:08:53 +00002366 // We set the AGC to mute state only when all the channels are muted.
2367 // This implementation is not ideal, instead we should signal the AGC when
2368 // the mic channel is muted/unmuted. We can't do it today because there
2369 // is no good way to know which stream is mapping to the mic channel.
2370 bool all_muted = muted;
solenbergc96df772015-10-21 13:01:53 -07002371 for (const auto& ch : send_streams_) {
Fredrik Solenbergaf9fb212015-08-26 10:45:53 +02002372 if (!all_muted) {
2373 break;
2374 }
2375 if (engine()->voe()->volume()->GetInputMute(ch.second->channel(),
buildbot@webrtc.org6b21b712014-07-31 15:08:53 +00002376 all_muted)) {
Fredrik Solenbergaf9fb212015-08-26 10:45:53 +02002377 LOG_RTCERR1(GetInputMute, ch.second->channel());
buildbot@webrtc.org6b21b712014-07-31 15:08:53 +00002378 return false;
2379 }
2380 }
2381
2382 webrtc::AudioProcessing* ap = engine()->voe()->base()->audio_processing();
solenberg0a617e22015-10-20 15:49:38 -07002383 if (ap) {
buildbot@webrtc.org6b21b712014-07-31 15:08:53 +00002384 ap->set_output_will_be_muted(all_muted);
solenberg0a617e22015-10-20 15:49:38 -07002385 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002386 return true;
2387}
2388
minyue@webrtc.org26236952014-10-29 02:27:08 +00002389// TODO(minyue): SetMaxSendBandwidth() is subject to be renamed to
2390// SetMaxSendBitrate() in future.
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00002391bool WebRtcVoiceMediaChannel::SetMaxSendBandwidth(int bps) {
minyue@webrtc.org26236952014-10-29 02:27:08 +00002392 LOG(LS_INFO) << "WebRtcVoiceMediaChannel::SetMaxSendBandwidth.";
minyue@webrtc.org26236952014-10-29 02:27:08 +00002393 return SetSendBitrateInternal(bps);
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00002394}
2395
minyue@webrtc.org26236952014-10-29 02:27:08 +00002396bool WebRtcVoiceMediaChannel::SetSendBitrateInternal(int bps) {
2397 LOG(LS_INFO) << "WebRtcVoiceMediaChannel::SetSendBitrateInternal.";
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00002398
minyue@webrtc.org26236952014-10-29 02:27:08 +00002399 send_bitrate_setting_ = true;
2400 send_bitrate_bps_ = bps;
wu@webrtc.org1d1ffc92013-10-16 18:12:02 +00002401
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002402 if (!send_codec_) {
wu@webrtc.org1d1ffc92013-10-16 18:12:02 +00002403 LOG(LS_INFO) << "The send codec has not been set up yet. "
minyue@webrtc.org26236952014-10-29 02:27:08 +00002404 << "The send bitrate setting will be applied later.";
wu@webrtc.org1d1ffc92013-10-16 18:12:02 +00002405 return true;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002406 }
2407
minyue@webrtc.org26236952014-10-29 02:27:08 +00002408 // Bitrate is auto by default.
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00002409 // TODO(bemasc): Fix this so that if SetMaxSendBandwidth(50) is followed by
2410 // SetMaxSendBandwith(0), the second call removes the previous limit.
2411 if (bps <= 0)
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002412 return true;
2413
2414 webrtc::CodecInst codec = *send_codec_;
solenberg26c8c912015-11-27 04:00:25 -08002415 bool is_multi_rate = WebRtcVoiceCodecs::IsCodecMultiRate(codec);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002416
2417 if (is_multi_rate) {
2418 // If codec is multi-rate then just set the bitrate.
2419 codec.rate = bps;
solenbergc96df772015-10-21 13:01:53 -07002420 for (const auto& ch : send_streams_) {
solenberg0a617e22015-10-20 15:49:38 -07002421 if (!SetSendCodec(ch.second->channel(), codec)) {
2422 LOG(LS_INFO) << "Failed to set codec " << codec.plname
2423 << " to bitrate " << bps << " bps.";
2424 return false;
2425 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002426 }
2427 return true;
2428 } else {
2429 // If codec is not multi-rate and |bps| is less than the fixed bitrate
2430 // then fail. If codec is not multi-rate and |bps| exceeds or equal the
2431 // fixed bitrate then ignore.
2432 if (bps < codec.rate) {
2433 LOG(LS_INFO) << "Failed to set codec " << codec.plname
2434 << " to bitrate " << bps << " bps"
2435 << ", requires at least " << codec.rate << " bps.";
2436 return false;
2437 }
2438 return true;
2439 }
2440}
2441
2442bool WebRtcVoiceMediaChannel::GetStats(VoiceMediaInfo* info) {
solenberg566ef242015-11-06 15:34:49 -08002443 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
solenberg85a04962015-10-27 03:35:21 -07002444 RTC_DCHECK(info);
solenbergd97ec302015-10-07 01:40:33 -07002445
solenberg85a04962015-10-27 03:35:21 -07002446 // Get SSRC and stats for each sender.
2447 RTC_DCHECK(info->senders.size() == 0);
2448 for (const auto& stream : send_streams_) {
2449 webrtc::AudioSendStream::Stats stats = stream.second->GetStats();
wu@webrtc.org9dba5252013-08-05 20:36:57 +00002450 VoiceSenderInfo sinfo;
solenberg85a04962015-10-27 03:35:21 -07002451 sinfo.add_ssrc(stats.local_ssrc);
2452 sinfo.bytes_sent = stats.bytes_sent;
2453 sinfo.packets_sent = stats.packets_sent;
2454 sinfo.packets_lost = stats.packets_lost;
2455 sinfo.fraction_lost = stats.fraction_lost;
2456 sinfo.codec_name = stats.codec_name;
2457 sinfo.ext_seqnum = stats.ext_seqnum;
2458 sinfo.jitter_ms = stats.jitter_ms;
2459 sinfo.rtt_ms = stats.rtt_ms;
2460 sinfo.audio_level = stats.audio_level;
2461 sinfo.aec_quality_min = stats.aec_quality_min;
2462 sinfo.echo_delay_median_ms = stats.echo_delay_median_ms;
2463 sinfo.echo_delay_std_ms = stats.echo_delay_std_ms;
2464 sinfo.echo_return_loss = stats.echo_return_loss;
2465 sinfo.echo_return_loss_enhancement = stats.echo_return_loss_enhancement;
solenberg566ef242015-11-06 15:34:49 -08002466 sinfo.typing_noise_detected =
2467 (send_ == SEND_NOTHING ? false : stats.typing_noise_detected);
wu@webrtc.org9dba5252013-08-05 20:36:57 +00002468 info->senders.push_back(sinfo);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002469 }
2470
solenberg85a04962015-10-27 03:35:21 -07002471 // Get SSRC and stats for each receiver.
2472 RTC_DCHECK(info->receivers.size() == 0);
solenberg7add0582015-11-20 09:59:34 -08002473 for (const auto& stream : recv_streams_) {
Fredrik Solenberg4f4ec0a2015-10-22 10:49:27 +02002474 webrtc::AudioReceiveStream::Stats stats = stream.second->GetStats();
2475 VoiceReceiverInfo rinfo;
2476 rinfo.add_ssrc(stats.remote_ssrc);
2477 rinfo.bytes_rcvd = stats.bytes_rcvd;
2478 rinfo.packets_rcvd = stats.packets_rcvd;
2479 rinfo.packets_lost = stats.packets_lost;
2480 rinfo.fraction_lost = stats.fraction_lost;
2481 rinfo.codec_name = stats.codec_name;
2482 rinfo.ext_seqnum = stats.ext_seqnum;
2483 rinfo.jitter_ms = stats.jitter_ms;
2484 rinfo.jitter_buffer_ms = stats.jitter_buffer_ms;
2485 rinfo.jitter_buffer_preferred_ms = stats.jitter_buffer_preferred_ms;
2486 rinfo.delay_estimate_ms = stats.delay_estimate_ms;
2487 rinfo.audio_level = stats.audio_level;
2488 rinfo.expand_rate = stats.expand_rate;
2489 rinfo.speech_expand_rate = stats.speech_expand_rate;
2490 rinfo.secondary_decoded_rate = stats.secondary_decoded_rate;
2491 rinfo.accelerate_rate = stats.accelerate_rate;
2492 rinfo.preemptive_expand_rate = stats.preemptive_expand_rate;
2493 rinfo.decoding_calls_to_silence_generator =
2494 stats.decoding_calls_to_silence_generator;
2495 rinfo.decoding_calls_to_neteq = stats.decoding_calls_to_neteq;
2496 rinfo.decoding_normal = stats.decoding_normal;
2497 rinfo.decoding_plc = stats.decoding_plc;
2498 rinfo.decoding_cng = stats.decoding_cng;
2499 rinfo.decoding_plc_cng = stats.decoding_plc_cng;
2500 rinfo.capture_start_ntp_time_ms = stats.capture_start_ntp_time_ms;
2501 info->receivers.push_back(rinfo);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002502 }
2503
2504 return true;
2505}
2506
Tommif888bb52015-12-12 01:37:01 +01002507void WebRtcVoiceMediaChannel::SetRawAudioSink(
2508 uint32_t ssrc,
deadbeef2d110be2016-01-13 12:00:26 -08002509 rtc::scoped_ptr<webrtc::AudioSinkInterface> sink) {
Tommif888bb52015-12-12 01:37:01 +01002510 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
deadbeef884f5852016-01-15 09:20:04 -08002511 LOG(LS_VERBOSE) << "WebRtcVoiceMediaChannel::SetRawAudioSink: ssrc:" << ssrc
2512 << " " << (sink ? "(ptr)" : "NULL");
2513 if (ssrc == 0) {
2514 if (default_recv_ssrc_ != -1) {
2515 rtc::scoped_ptr<webrtc::AudioSinkInterface> proxy_sink(
2516 sink ? new ProxySink(sink.get()) : nullptr);
2517 SetRawAudioSink(default_recv_ssrc_, std::move(proxy_sink));
2518 }
2519 default_sink_ = std::move(sink);
2520 return;
2521 }
Tommif888bb52015-12-12 01:37:01 +01002522 const auto it = recv_streams_.find(ssrc);
2523 if (it == recv_streams_.end()) {
2524 LOG(LS_WARNING) << "SetRawAudioSink: no recv stream" << ssrc;
2525 return;
2526 }
deadbeef2d110be2016-01-13 12:00:26 -08002527 it->second->SetRawAudioSink(std::move(sink));
Tommif888bb52015-12-12 01:37:01 +01002528}
2529
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002530int WebRtcVoiceMediaChannel::GetOutputLevel(int channel) {
solenbergd97ec302015-10-07 01:40:33 -07002531 unsigned int ulevel = 0;
2532 int ret = engine()->voe()->volume()->GetSpeechOutputLevel(channel, ulevel);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002533 return (ret == 0) ? static_cast<int>(ulevel) : -1;
2534}
2535
Peter Boström0c4e06b2015-10-07 12:23:21 +02002536int WebRtcVoiceMediaChannel::GetReceiveChannelId(uint32_t ssrc) const {
solenberg566ef242015-11-06 15:34:49 -08002537 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
solenberg7add0582015-11-20 09:59:34 -08002538 const auto it = recv_streams_.find(ssrc);
2539 if (it != recv_streams_.end()) {
mallinath@webrtc.org67ee6b92014-02-03 16:57:16 +00002540 return it->second->channel();
solenberg8fb30c32015-10-13 03:06:58 -07002541 }
solenberg1ac56142015-10-13 03:58:19 -07002542 return -1;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002543}
2544
Peter Boström0c4e06b2015-10-07 12:23:21 +02002545int WebRtcVoiceMediaChannel::GetSendChannelId(uint32_t ssrc) const {
solenberg566ef242015-11-06 15:34:49 -08002546 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
solenbergc96df772015-10-21 13:01:53 -07002547 const auto it = send_streams_.find(ssrc);
2548 if (it != send_streams_.end()) {
mallinath@webrtc.org67ee6b92014-02-03 16:57:16 +00002549 return it->second->channel();
solenberg8fb30c32015-10-13 03:06:58 -07002550 }
wu@webrtc.org9dba5252013-08-05 20:36:57 +00002551 return -1;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002552}
2553
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002554bool WebRtcVoiceMediaChannel::SetPlayout(int channel, bool playout) {
2555 if (playout) {
2556 LOG(LS_INFO) << "Starting playout for channel #" << channel;
2557 if (engine()->voe()->base()->StartPlayout(channel) == -1) {
2558 LOG_RTCERR1(StartPlayout, channel);
2559 return false;
2560 }
2561 } else {
2562 LOG(LS_INFO) << "Stopping playout for channel #" << channel;
2563 engine()->voe()->base()->StopPlayout(channel);
2564 }
2565 return true;
2566}
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002567} // namespace cricket
2568
2569#endif // HAVE_WEBRTC_VOICE