blob: c4f5f99c989b23c00e4866ee1f933a39fb007145 [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
solenbergd97ec302015-10-07 01:40:33 -0700207bool IsNackEnabled(const AudioCodec& codec) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000208 return codec.HasFeedbackParam(FeedbackParam(kRtcpFbParamNack,
209 kParamValueEmpty));
210}
211
Minyue Li7100dcd2015-03-27 05:05:59 +0100212// Return true if codec.params[feature] == "1", false otherwise.
solenberg26c8c912015-11-27 04:00:25 -0800213bool IsCodecFeatureEnabled(const AudioCodec& codec, const char* feature) {
Minyue Li7100dcd2015-03-27 05:05:59 +0100214 int value;
215 return codec.GetParam(feature, &value) && value == 1;
216}
217
218// Use params[kCodecParamMaxAverageBitrate] if it is defined, use codec.bitrate
219// otherwise. If the value (either from params or codec.bitrate) <=0, use the
220// default configuration. If the value is beyond feasible bit rate of Opus,
221// clamp it. Returns the Opus bit rate for operation.
solenbergd97ec302015-10-07 01:40:33 -0700222int GetOpusBitrate(const AudioCodec& codec, int max_playback_rate) {
Minyue Li7100dcd2015-03-27 05:05:59 +0100223 int bitrate = 0;
224 bool use_param = true;
225 if (!codec.GetParam(kCodecParamMaxAverageBitrate, &bitrate)) {
226 bitrate = codec.bitrate;
227 use_param = false;
228 }
229 if (bitrate <= 0) {
230 if (max_playback_rate <= 8000) {
231 bitrate = kOpusBitrateNb;
232 } else if (max_playback_rate <= 16000) {
233 bitrate = kOpusBitrateWb;
234 } else {
235 bitrate = kOpusBitrateFb;
236 }
237
238 if (IsCodecFeatureEnabled(codec, kCodecParamStereo)) {
239 bitrate *= 2;
240 }
241 } else if (bitrate < kOpusMinBitrate || bitrate > kOpusMaxBitrate) {
242 bitrate = (bitrate < kOpusMinBitrate) ? kOpusMinBitrate : kOpusMaxBitrate;
243 std::string rate_source =
244 use_param ? "Codec parameter \"maxaveragebitrate\"" :
245 "Supplied Opus bitrate";
246 LOG(LS_WARNING) << rate_source
247 << " is invalid and is replaced by: "
248 << bitrate;
249 }
250 return bitrate;
251}
252
253// Returns kOpusDefaultPlaybackRate if params[kCodecParamMaxPlaybackRate] is not
254// defined. Returns the value of params[kCodecParamMaxPlaybackRate] otherwise.
solenbergd97ec302015-10-07 01:40:33 -0700255int GetOpusMaxPlaybackRate(const AudioCodec& codec) {
Minyue Li7100dcd2015-03-27 05:05:59 +0100256 int value;
257 if (codec.GetParam(kCodecParamMaxPlaybackRate, &value)) {
258 return value;
259 }
260 return kOpusDefaultMaxPlaybackRate;
261}
262
solenbergd97ec302015-10-07 01:40:33 -0700263void GetOpusConfig(const AudioCodec& codec, webrtc::CodecInst* voe_codec,
Minyue Li7100dcd2015-03-27 05:05:59 +0100264 bool* enable_codec_fec, int* max_playback_rate,
265 bool* enable_codec_dtx) {
266 *enable_codec_fec = IsCodecFeatureEnabled(codec, kCodecParamUseInbandFec);
267 *enable_codec_dtx = IsCodecFeatureEnabled(codec, kCodecParamUseDtx);
268 *max_playback_rate = GetOpusMaxPlaybackRate(codec);
269
270 // If OPUS, change what we send according to the "stereo" codec
271 // parameter, and not the "channels" parameter. We set
272 // voe_codec.channels to 2 if "stereo=1" and 1 otherwise. If
273 // the bitrate is not specified, i.e. is <= zero, we set it to the
274 // appropriate default value for mono or stereo Opus.
275
276 voe_codec->channels = IsCodecFeatureEnabled(codec, kCodecParamStereo) ? 2 : 1;
277 voe_codec->rate = GetOpusBitrate(codec, *max_playback_rate);
278}
279
solenberg566ef242015-11-06 15:34:49 -0800280webrtc::AudioState::Config MakeAudioStateConfig(VoEWrapper* voe_wrapper) {
281 webrtc::AudioState::Config config;
282 config.voice_engine = voe_wrapper->engine();
283 return config;
284}
285
solenberg26c8c912015-11-27 04:00:25 -0800286class WebRtcVoiceCodecs final {
287 public:
288 // TODO(solenberg): Do this filtering once off-line, add a simple AudioCodec
289 // list and add a test which verifies VoE supports the listed codecs.
290 static std::vector<AudioCodec> SupportedCodecs() {
291 LOG(LS_INFO) << "WebRtc VoiceEngine codecs:";
292 std::vector<AudioCodec> result;
293 for (webrtc::CodecInst voe_codec : webrtc::acm2::RentACodec::Database()) {
294 // Change the sample rate of G722 to 8000 to match SDP.
295 MaybeFixupG722(&voe_codec, 8000);
buildbot@webrtc.org13d67762014-05-02 17:33:29 +0000296 // Skip uncompressed formats.
Minyue Li7100dcd2015-03-27 05:05:59 +0100297 if (IsCodec(voe_codec, kL16CodecName)) {
buildbot@webrtc.org13d67762014-05-02 17:33:29 +0000298 continue;
299 }
300
301 const CodecPref* pref = NULL;
tfarina5237aaf2015-11-10 23:44:30 -0800302 for (size_t j = 0; j < arraysize(kCodecPrefs); ++j) {
Minyue Li7100dcd2015-03-27 05:05:59 +0100303 if (IsCodec(voe_codec, kCodecPrefs[j].name) &&
buildbot@webrtc.org13d67762014-05-02 17:33:29 +0000304 kCodecPrefs[j].clockrate == voe_codec.plfreq &&
305 kCodecPrefs[j].channels == voe_codec.channels) {
306 pref = &kCodecPrefs[j];
307 break;
308 }
309 }
310
311 if (pref) {
312 // Use the payload type that we've configured in our pref table;
313 // use the offset in our pref table to determine the sort order.
tfarina5237aaf2015-11-10 23:44:30 -0800314 AudioCodec codec(
315 pref->payload_type, voe_codec.plname, voe_codec.plfreq,
316 voe_codec.rate, voe_codec.channels,
317 static_cast<int>(arraysize(kCodecPrefs)) - (pref - kCodecPrefs));
buildbot@webrtc.org13d67762014-05-02 17:33:29 +0000318 LOG(LS_INFO) << ToString(codec);
Minyue Li7100dcd2015-03-27 05:05:59 +0100319 if (IsCodec(codec, kIsacCodecName)) {
minyue@webrtc.org26236952014-10-29 02:27:08 +0000320 // Indicate auto-bitrate in signaling.
buildbot@webrtc.org13d67762014-05-02 17:33:29 +0000321 codec.bitrate = 0;
322 }
Minyue Li7100dcd2015-03-27 05:05:59 +0100323 if (IsCodec(codec, kOpusCodecName)) {
buildbot@webrtc.org13d67762014-05-02 17:33:29 +0000324 // Only add fmtp parameters that differ from the spec.
325 if (kPreferredMinPTime != kOpusDefaultMinPTime) {
326 codec.params[kCodecParamMinPTime] =
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000327 rtc::ToString(kPreferredMinPTime);
buildbot@webrtc.org13d67762014-05-02 17:33:29 +0000328 }
329 if (kPreferredMaxPTime != kOpusDefaultMaxPTime) {
330 codec.params[kCodecParamMaxPTime] =
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000331 rtc::ToString(kPreferredMaxPTime);
buildbot@webrtc.org13d67762014-05-02 17:33:29 +0000332 }
pkasting@chromium.orgd3245462015-02-23 21:28:22 +0000333 codec.SetParam(kCodecParamUseInbandFec, 1);
minyue@webrtc.org4ef22d12014-11-17 09:26:39 +0000334
335 // TODO(hellner): Add ptime, sprop-stereo, and stereo
buildbot@webrtc.org13d67762014-05-02 17:33:29 +0000336 // when they can be set to values other than the default.
337 }
solenberg26c8c912015-11-27 04:00:25 -0800338 result.push_back(codec);
buildbot@webrtc.org13d67762014-05-02 17:33:29 +0000339 } else {
340 LOG(LS_WARNING) << "Unexpected codec: " << ToString(voe_codec);
341 }
342 }
solenberg26c8c912015-11-27 04:00:25 -0800343 // Make sure they are in local preference order.
344 std::sort(result.begin(), result.end(), &AudioCodec::Preferable);
345 return result;
buildbot@webrtc.org13d67762014-05-02 17:33:29 +0000346 }
buildbot@webrtc.org13d67762014-05-02 17:33:29 +0000347
solenberg26c8c912015-11-27 04:00:25 -0800348 static bool ToCodecInst(const AudioCodec& in,
349 webrtc::CodecInst* out) {
350 for (webrtc::CodecInst voe_codec : webrtc::acm2::RentACodec::Database()) {
351 // Change the sample rate of G722 to 8000 to match SDP.
352 MaybeFixupG722(&voe_codec, 8000);
353 AudioCodec codec(voe_codec.pltype, voe_codec.plname, voe_codec.plfreq,
354 voe_codec.rate, voe_codec.channels, 0);
355 bool multi_rate = IsCodecMultiRate(voe_codec);
356 // Allow arbitrary rates for ISAC to be specified.
357 if (multi_rate) {
358 // Set codec.bitrate to 0 so the check for codec.Matches() passes.
359 codec.bitrate = 0;
360 }
361 if (codec.Matches(in)) {
362 if (out) {
363 // Fixup the payload type.
364 voe_codec.pltype = in.id;
365
366 // Set bitrate if specified.
367 if (multi_rate && in.bitrate != 0) {
368 voe_codec.rate = in.bitrate;
369 }
370
371 // Reset G722 sample rate to 16000 to match WebRTC.
372 MaybeFixupG722(&voe_codec, 16000);
373
374 // Apply codec-specific settings.
375 if (IsCodec(codec, kIsacCodecName)) {
376 // If ISAC and an explicit bitrate is not specified,
377 // enable auto bitrate adjustment.
378 voe_codec.rate = (in.bitrate > 0) ? in.bitrate : -1;
379 }
380 *out = voe_codec;
381 }
382 return true;
383 }
384 }
henrik.lundin@webrtc.org8038d422014-11-11 08:38:24 +0000385 return false;
henrik.lundin@webrtc.orgf85dbce2014-11-07 12:25:00 +0000386 }
solenberg26c8c912015-11-27 04:00:25 -0800387
388 static bool IsCodecMultiRate(const webrtc::CodecInst& codec) {
389 for (size_t i = 0; i < arraysize(kCodecPrefs); ++i) {
390 if (IsCodec(codec, kCodecPrefs[i].name) &&
391 kCodecPrefs[i].clockrate == codec.plfreq) {
392 return kCodecPrefs[i].is_multi_rate;
393 }
394 }
395 return false;
396 }
397
398 // If the AudioCodec param kCodecParamPTime is set, then we will set it to
399 // codec pacsize if it's valid, or we will pick the next smallest value we
400 // support.
401 // TODO(Brave): Query supported packet sizes from ACM when the API is ready.
402 static bool SetPTimeAsPacketSize(webrtc::CodecInst* codec, int ptime_ms) {
403 for (const CodecPref& codec_pref : kCodecPrefs) {
404 if ((IsCodec(*codec, codec_pref.name) &&
405 codec_pref.clockrate == codec->plfreq) ||
406 IsCodec(*codec, kG722CodecName)) {
407 int packet_size_ms = SelectPacketSize(codec_pref, ptime_ms);
408 if (packet_size_ms) {
409 // Convert unit from milli-seconds to samples.
410 codec->pacsize = (codec->plfreq / 1000) * packet_size_ms;
411 return true;
412 }
413 }
414 }
415 return false;
416 }
417
418 private:
419 static const int kMaxNumPacketSize = 6;
420 struct CodecPref {
421 const char* name;
422 int clockrate;
Peter Kasting69558702016-01-12 16:26:35 -0800423 size_t channels;
solenberg26c8c912015-11-27 04:00:25 -0800424 int payload_type;
425 bool is_multi_rate;
426 int packet_sizes_ms[kMaxNumPacketSize];
427 };
428 // Note: keep the supported packet sizes in ascending order.
429 static const CodecPref kCodecPrefs[12];
430
431 static int SelectPacketSize(const CodecPref& codec_pref, int ptime_ms) {
432 int selected_packet_size_ms = codec_pref.packet_sizes_ms[0];
433 for (int packet_size_ms : codec_pref.packet_sizes_ms) {
434 if (packet_size_ms && packet_size_ms <= ptime_ms) {
435 selected_packet_size_ms = packet_size_ms;
436 }
437 }
438 return selected_packet_size_ms;
439 }
440
441 // Changes RTP timestamp rate of G722. This is due to the "bug" in the RFC
442 // which says that G722 should be advertised as 8 kHz although it is a 16 kHz
443 // codec.
444 static void MaybeFixupG722(webrtc::CodecInst* voe_codec, int new_plfreq) {
445 if (IsCodec(*voe_codec, kG722CodecName)) {
446 // If the ASSERT triggers, the codec definition in WebRTC VoiceEngine
447 // has changed, and this special case is no longer needed.
448 RTC_DCHECK(voe_codec->plfreq != new_plfreq);
449 voe_codec->plfreq = new_plfreq;
450 }
451 }
452};
453
454const WebRtcVoiceCodecs::CodecPref WebRtcVoiceCodecs::kCodecPrefs[12] = {
455 { kOpusCodecName, 48000, 2, 111, true, { 10, 20, 40, 60 } },
456 { kIsacCodecName, 16000, 1, 103, true, { 30, 60 } },
457 { kIsacCodecName, 32000, 1, 104, true, { 30 } },
458 // G722 should be advertised as 8000 Hz because of the RFC "bug".
459 { kG722CodecName, 8000, 1, 9, false, { 10, 20, 30, 40, 50, 60 } },
460 { kIlbcCodecName, 8000, 1, 102, false, { 20, 30, 40, 60 } },
461 { kPcmuCodecName, 8000, 1, 0, false, { 10, 20, 30, 40, 50, 60 } },
462 { kPcmaCodecName, 8000, 1, 8, false, { 10, 20, 30, 40, 50, 60 } },
463 { kCnCodecName, 32000, 1, 106, false, { } },
464 { kCnCodecName, 16000, 1, 105, false, { } },
465 { kCnCodecName, 8000, 1, 13, false, { } },
466 { kRedCodecName, 8000, 1, 127, false, { } },
467 { kDtmfCodecName, 8000, 1, 126, false, { } },
468};
469} // namespace {
470
471bool WebRtcVoiceEngine::ToCodecInst(const AudioCodec& in,
472 webrtc::CodecInst* out) {
473 return WebRtcVoiceCodecs::ToCodecInst(in, out);
474}
475
476WebRtcVoiceEngine::WebRtcVoiceEngine()
477 : voe_wrapper_(new VoEWrapper()),
478 audio_state_(webrtc::AudioState::Create(MakeAudioStateConfig(voe()))) {
479 Construct();
480}
481
482WebRtcVoiceEngine::WebRtcVoiceEngine(VoEWrapper* voe_wrapper)
483 : voe_wrapper_(voe_wrapper) {
484 Construct();
485}
486
487void WebRtcVoiceEngine::Construct() {
488 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
489 LOG(LS_VERBOSE) << "WebRtcVoiceEngine::WebRtcVoiceEngine";
490
491 signal_thread_checker_.DetachFromThread();
492 std::memset(&default_agc_config_, 0, sizeof(default_agc_config_));
solenberg246b8172015-12-08 09:50:23 -0800493 voe_config_.Set<webrtc::VoicePacing>(new webrtc::VoicePacing(true));
solenberg26c8c912015-11-27 04:00:25 -0800494
495 webrtc::Trace::set_level_filter(kDefaultTraceFilter);
496 webrtc::Trace::SetTraceCallback(this);
497
498 // Load our audio codec list.
499 codecs_ = WebRtcVoiceCodecs::SupportedCodecs();
henrik.lundin@webrtc.orgf85dbce2014-11-07 12:25:00 +0000500}
501
buildbot@webrtc.org13d67762014-05-02 17:33:29 +0000502WebRtcVoiceEngine::~WebRtcVoiceEngine() {
solenberg566ef242015-11-06 15:34:49 -0800503 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
buildbot@webrtc.org13d67762014-05-02 17:33:29 +0000504 LOG(LS_VERBOSE) << "WebRtcVoiceEngine::~WebRtcVoiceEngine";
buildbot@webrtc.org13d67762014-05-02 17:33:29 +0000505 if (adm_) {
506 voe_wrapper_.reset();
507 adm_->Release();
508 adm_ = NULL;
509 }
solenbergbd138382015-11-20 16:08:07 -0800510 webrtc::Trace::SetTraceCallback(nullptr);
buildbot@webrtc.org13d67762014-05-02 17:33:29 +0000511}
512
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000513bool WebRtcVoiceEngine::Init(rtc::Thread* worker_thread) {
solenberg566ef242015-11-06 15:34:49 -0800514 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
henrikg91d6ede2015-09-17 00:24:34 -0700515 RTC_DCHECK(worker_thread == rtc::Thread::Current());
buildbot@webrtc.org13d67762014-05-02 17:33:29 +0000516 LOG(LS_INFO) << "WebRtcVoiceEngine::Init";
517 bool res = InitInternal();
518 if (res) {
519 LOG(LS_INFO) << "WebRtcVoiceEngine::Init Done!";
520 } else {
521 LOG(LS_ERROR) << "WebRtcVoiceEngine::Init failed";
522 Terminate();
523 }
524 return res;
525}
526
527bool WebRtcVoiceEngine::InitInternal() {
solenberg566ef242015-11-06 15:34:49 -0800528 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
buildbot@webrtc.org13d67762014-05-02 17:33:29 +0000529 // Temporarily turn logging level up for the Init call
solenbergbd138382015-11-20 16:08:07 -0800530 webrtc::Trace::set_level_filter(kElevatedTraceFilter);
solenberg2515af22015-12-02 06:19:36 -0800531 LOG(LS_INFO) << webrtc::VoiceEngine::GetVersionString();
buildbot@webrtc.org13d67762014-05-02 17:33:29 +0000532 if (voe_wrapper_->base()->Init(adm_) == -1) {
533 LOG_RTCERR0_EX(Init, voe_wrapper_->error());
buildbot@webrtc.org13d67762014-05-02 17:33:29 +0000534 return false;
535 }
solenbergbd138382015-11-20 16:08:07 -0800536 webrtc::Trace::set_level_filter(kDefaultTraceFilter);
buildbot@webrtc.org13d67762014-05-02 17:33:29 +0000537
buildbot@webrtc.org13d67762014-05-02 17:33:29 +0000538 // Save the default AGC configuration settings. This must happen before
solenberg246b8172015-12-08 09:50:23 -0800539 // calling ApplyOptions or the default will be overwritten.
buildbot@webrtc.org13d67762014-05-02 17:33:29 +0000540 if (voe_wrapper_->processing()->GetAgcConfig(default_agc_config_) == -1) {
541 LOG_RTCERR0(GetAgcConfig);
542 return false;
543 }
544
solenberg0f7d2932016-01-15 01:40:39 -0800545 // Set default engine options.
546 {
547 AudioOptions options;
548 options.echo_cancellation = rtc::Optional<bool>(true);
549 options.auto_gain_control = rtc::Optional<bool>(true);
550 options.noise_suppression = rtc::Optional<bool>(true);
551 options.highpass_filter = rtc::Optional<bool>(true);
552 options.stereo_swapping = rtc::Optional<bool>(false);
553 options.audio_jitter_buffer_max_packets = rtc::Optional<int>(50);
554 options.audio_jitter_buffer_fast_accelerate = rtc::Optional<bool>(false);
555 options.typing_detection = rtc::Optional<bool>(true);
556 options.adjust_agc_delta = rtc::Optional<int>(0);
557 options.experimental_agc = rtc::Optional<bool>(false);
558 options.extended_filter_aec = rtc::Optional<bool>(false);
559 options.delay_agnostic_aec = rtc::Optional<bool>(false);
560 options.experimental_ns = rtc::Optional<bool>(false);
561 options.aec_dump = rtc::Optional<bool>(false);
562 if (!ApplyOptions(options)) {
563 return false;
564 }
565 }
566
buildbot@webrtc.org13d67762014-05-02 17:33:29 +0000567 // Print our codec list again for the call diagnostic log
568 LOG(LS_INFO) << "WebRtc VoiceEngine codecs:";
Fredrik Solenbergaf9fb212015-08-26 10:45:53 +0200569 for (const AudioCodec& codec : codecs_) {
570 LOG(LS_INFO) << ToString(codec);
buildbot@webrtc.org13d67762014-05-02 17:33:29 +0000571 }
572
solenberg246b8172015-12-08 09:50:23 -0800573 SetDefaultDevices();
574
buildbot@webrtc.org13d67762014-05-02 17:33:29 +0000575 initialized_ = true;
576 return true;
577}
578
buildbot@webrtc.org13d67762014-05-02 17:33:29 +0000579void WebRtcVoiceEngine::Terminate() {
solenberg566ef242015-11-06 15:34:49 -0800580 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
buildbot@webrtc.org13d67762014-05-02 17:33:29 +0000581 LOG(LS_INFO) << "WebRtcVoiceEngine::Terminate";
582 initialized_ = false;
583
584 StopAecDump();
585
buildbot@webrtc.org13d67762014-05-02 17:33:29 +0000586 voe_wrapper_->base()->Terminate();
buildbot@webrtc.org13d67762014-05-02 17:33:29 +0000587}
588
solenberg566ef242015-11-06 15:34:49 -0800589rtc::scoped_refptr<webrtc::AudioState>
590 WebRtcVoiceEngine::GetAudioState() const {
591 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
592 return audio_state_;
593}
594
Fredrik Solenberg709ed672015-09-15 12:26:33 +0200595VoiceMediaChannel* WebRtcVoiceEngine::CreateChannel(webrtc::Call* call,
Jelena Marusicc28a8962015-05-29 15:05:44 +0200596 const AudioOptions& options) {
solenberg566ef242015-11-06 15:34:49 -0800597 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
solenberg0a617e22015-10-20 15:49:38 -0700598 return new WebRtcVoiceMediaChannel(this, options, call);
buildbot@webrtc.org13d67762014-05-02 17:33:29 +0000599}
600
buildbot@webrtc.org13d67762014-05-02 17:33:29 +0000601bool WebRtcVoiceEngine::ApplyOptions(const AudioOptions& options_in) {
solenberg566ef242015-11-06 15:34:49 -0800602 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
henrikac14f5ff2015-09-23 14:08:33 +0200603 LOG(LS_INFO) << "ApplyOptions: " << options_in.ToString();
solenberg0f7d2932016-01-15 01:40:39 -0800604 AudioOptions options = options_in; // The options are modified below.
solenberg246b8172015-12-08 09:50:23 -0800605
buildbot@webrtc.org13d67762014-05-02 17:33:29 +0000606 // kEcConference is AEC with high suppression.
607 webrtc::EcModes ec_mode = webrtc::kEcConference;
608 webrtc::AecmModes aecm_mode = webrtc::kAecmSpeakerphone;
609 webrtc::AgcModes agc_mode = webrtc::kAgcAdaptiveAnalog;
610 webrtc::NsModes ns_mode = webrtc::kNsHighSuppression;
kwiberg102c6a62015-10-30 02:47:38 -0700611 if (options.aecm_generate_comfort_noise) {
buildbot@webrtc.org13d67762014-05-02 17:33:29 +0000612 LOG(LS_VERBOSE) << "Comfort noise explicitly set to "
kwiberg102c6a62015-10-30 02:47:38 -0700613 << *options.aecm_generate_comfort_noise
614 << " (default is false).";
buildbot@webrtc.org13d67762014-05-02 17:33:29 +0000615 }
616
kjellanderfcfc8042016-01-14 11:01:09 -0800617#if defined(WEBRTC_IOS)
buildbot@webrtc.org13d67762014-05-02 17:33:29 +0000618 // On iOS, VPIO provides built-in EC and AGC.
Karl Wibergbe579832015-11-10 22:34:18 +0100619 options.echo_cancellation = rtc::Optional<bool>(false);
620 options.auto_gain_control = rtc::Optional<bool>(false);
henrika86d907c2015-09-07 16:09:50 +0200621 LOG(LS_INFO) << "Always disable AEC and AGC on iOS. Use built-in instead.";
buildbot@webrtc.org13d67762014-05-02 17:33:29 +0000622#elif defined(ANDROID)
623 ec_mode = webrtc::kEcAecm;
624#endif
625
kjellanderfcfc8042016-01-14 11:01:09 -0800626#if defined(WEBRTC_IOS) || defined(ANDROID)
buildbot@webrtc.org13d67762014-05-02 17:33:29 +0000627 // Set the AGC mode for iOS as well despite disabling it above, to avoid
628 // unsupported configuration errors from webrtc.
629 agc_mode = webrtc::kAgcFixedDigital;
Karl Wibergbe579832015-11-10 22:34:18 +0100630 options.typing_detection = rtc::Optional<bool>(false);
631 options.experimental_agc = rtc::Optional<bool>(false);
632 options.extended_filter_aec = rtc::Optional<bool>(false);
633 options.experimental_ns = rtc::Optional<bool>(false);
buildbot@webrtc.org13d67762014-05-02 17:33:29 +0000634#endif
635
Bjorn Volckerbf395c12015-03-25 22:45:56 +0100636 // Delay Agnostic AEC automatically turns on EC if not set except on iOS
637 // where the feature is not supported.
638 bool use_delay_agnostic_aec = false;
kjellanderfcfc8042016-01-14 11:01:09 -0800639#if !defined(WEBRTC_IOS)
kwiberg102c6a62015-10-30 02:47:38 -0700640 if (options.delay_agnostic_aec) {
641 use_delay_agnostic_aec = *options.delay_agnostic_aec;
Bjorn Volckerbf395c12015-03-25 22:45:56 +0100642 if (use_delay_agnostic_aec) {
Karl Wibergbe579832015-11-10 22:34:18 +0100643 options.echo_cancellation = rtc::Optional<bool>(true);
644 options.extended_filter_aec = rtc::Optional<bool>(true);
Bjorn Volckerbf395c12015-03-25 22:45:56 +0100645 ec_mode = webrtc::kEcConference;
646 }
647 }
648#endif
649
buildbot@webrtc.org13d67762014-05-02 17:33:29 +0000650 webrtc::VoEAudioProcessing* voep = voe_wrapper_->processing();
651
kwiberg102c6a62015-10-30 02:47:38 -0700652 if (options.echo_cancellation) {
henrika@webrtc.orga954c072014-12-09 16:22:09 +0000653 // Check if platform supports built-in EC. Currently only supported on
654 // Android and in combination with Java based audio layer.
655 // TODO(henrika): investigate possibility to support built-in EC also
656 // in combination with Open SL ES audio.
657 const bool built_in_aec = voe_wrapper_->hw()->BuiltInAECIsAvailable();
Bjorn Volcker73f72102015-06-03 14:50:15 +0200658 if (built_in_aec) {
Bjorn Volckerccfc9392015-05-07 07:43:17 +0200659 // Built-in EC exists on this device and use_delay_agnostic_aec is not
660 // overriding it. Enable/Disable it according to the echo_cancellation
661 // audio option.
Bjorn Volcker73f72102015-06-03 14:50:15 +0200662 const bool enable_built_in_aec =
kwiberg102c6a62015-10-30 02:47:38 -0700663 *options.echo_cancellation && !use_delay_agnostic_aec;
Bjorn Volcker73f72102015-06-03 14:50:15 +0200664 if (voe_wrapper_->hw()->EnableBuiltInAEC(enable_built_in_aec) == 0 &&
665 enable_built_in_aec) {
Bjorn Volckerbf395c12015-03-25 22:45:56 +0100666 // Disable internal software EC if built-in EC is enabled,
henrika@webrtc.orga954c072014-12-09 16:22:09 +0000667 // i.e., replace the software EC with the built-in EC.
Karl Wibergbe579832015-11-10 22:34:18 +0100668 options.echo_cancellation = rtc::Optional<bool>(false);
henrika@webrtc.orga954c072014-12-09 16:22:09 +0000669 LOG(LS_INFO) << "Disabling EC since built-in EC will be used instead";
670 }
671 }
kwiberg102c6a62015-10-30 02:47:38 -0700672 if (voep->SetEcStatus(*options.echo_cancellation, ec_mode) == -1) {
673 LOG_RTCERR2(SetEcStatus, *options.echo_cancellation, ec_mode);
buildbot@webrtc.org13d67762014-05-02 17:33:29 +0000674 return false;
675 } else {
kwiberg102c6a62015-10-30 02:47:38 -0700676 LOG(LS_INFO) << "Echo control set to " << *options.echo_cancellation
henrika86d907c2015-09-07 16:09:50 +0200677 << " with mode " << ec_mode;
buildbot@webrtc.org13d67762014-05-02 17:33:29 +0000678 }
679#if !defined(ANDROID)
680 // TODO(ajm): Remove the error return on Android from webrtc.
kwiberg102c6a62015-10-30 02:47:38 -0700681 if (voep->SetEcMetricsStatus(*options.echo_cancellation) == -1) {
682 LOG_RTCERR1(SetEcMetricsStatus, *options.echo_cancellation);
buildbot@webrtc.org13d67762014-05-02 17:33:29 +0000683 return false;
684 }
685#endif
686 if (ec_mode == webrtc::kEcAecm) {
kwiberg102c6a62015-10-30 02:47:38 -0700687 bool cn = options.aecm_generate_comfort_noise.value_or(false);
688 if (voep->SetAecmMode(aecm_mode, cn) != 0) {
689 LOG_RTCERR2(SetAecmMode, aecm_mode, cn);
buildbot@webrtc.org13d67762014-05-02 17:33:29 +0000690 return false;
691 }
692 }
693 }
694
kwiberg102c6a62015-10-30 02:47:38 -0700695 if (options.auto_gain_control) {
henrikac14f5ff2015-09-23 14:08:33 +0200696 const bool built_in_agc = voe_wrapper_->hw()->BuiltInAGCIsAvailable();
697 if (built_in_agc) {
kwiberg102c6a62015-10-30 02:47:38 -0700698 if (voe_wrapper_->hw()->EnableBuiltInAGC(*options.auto_gain_control) ==
699 0 &&
700 *options.auto_gain_control) {
henrikac14f5ff2015-09-23 14:08:33 +0200701 // Disable internal software AGC if built-in AGC is enabled,
702 // i.e., replace the software AGC with the built-in AGC.
Karl Wibergbe579832015-11-10 22:34:18 +0100703 options.auto_gain_control = rtc::Optional<bool>(false);
henrikac14f5ff2015-09-23 14:08:33 +0200704 LOG(LS_INFO) << "Disabling AGC since built-in AGC will be used instead";
705 }
706 }
kwiberg102c6a62015-10-30 02:47:38 -0700707 if (voep->SetAgcStatus(*options.auto_gain_control, agc_mode) == -1) {
708 LOG_RTCERR2(SetAgcStatus, *options.auto_gain_control, agc_mode);
buildbot@webrtc.org13d67762014-05-02 17:33:29 +0000709 return false;
710 } else {
kwiberg102c6a62015-10-30 02:47:38 -0700711 LOG(LS_INFO) << "Auto gain set to " << *options.auto_gain_control
712 << " with mode " << agc_mode;
buildbot@webrtc.org13d67762014-05-02 17:33:29 +0000713 }
714 }
715
kwiberg102c6a62015-10-30 02:47:38 -0700716 if (options.tx_agc_target_dbov || options.tx_agc_digital_compression_gain ||
717 options.tx_agc_limiter) {
buildbot@webrtc.org13d67762014-05-02 17:33:29 +0000718 // Override default_agc_config_. Generally, an unset option means "leave
719 // the VoE bits alone" in this function, so we want whatever is set to be
720 // stored as the new "default". If we didn't, then setting e.g.
721 // tx_agc_target_dbov would reset digital compression gain and limiter
722 // settings.
723 // Also, if we don't update default_agc_config_, then adjust_agc_delta
724 // would be an offset from the original values, and not whatever was set
725 // explicitly.
kwiberg102c6a62015-10-30 02:47:38 -0700726 default_agc_config_.targetLeveldBOv = options.tx_agc_target_dbov.value_or(
727 default_agc_config_.targetLeveldBOv);
buildbot@webrtc.org13d67762014-05-02 17:33:29 +0000728 default_agc_config_.digitalCompressionGaindB =
kwiberg102c6a62015-10-30 02:47:38 -0700729 options.tx_agc_digital_compression_gain.value_or(
buildbot@webrtc.org13d67762014-05-02 17:33:29 +0000730 default_agc_config_.digitalCompressionGaindB);
731 default_agc_config_.limiterEnable =
kwiberg102c6a62015-10-30 02:47:38 -0700732 options.tx_agc_limiter.value_or(default_agc_config_.limiterEnable);
buildbot@webrtc.org13d67762014-05-02 17:33:29 +0000733 if (voe_wrapper_->processing()->SetAgcConfig(default_agc_config_) == -1) {
734 LOG_RTCERR3(SetAgcConfig,
735 default_agc_config_.targetLeveldBOv,
736 default_agc_config_.digitalCompressionGaindB,
737 default_agc_config_.limiterEnable);
738 return false;
739 }
740 }
741
kwiberg102c6a62015-10-30 02:47:38 -0700742 if (options.noise_suppression) {
henrikac14f5ff2015-09-23 14:08:33 +0200743 const bool built_in_ns = voe_wrapper_->hw()->BuiltInNSIsAvailable();
744 if (built_in_ns) {
kwiberg102c6a62015-10-30 02:47:38 -0700745 if (voe_wrapper_->hw()->EnableBuiltInNS(*options.noise_suppression) ==
746 0 &&
747 *options.noise_suppression) {
henrikac14f5ff2015-09-23 14:08:33 +0200748 // Disable internal software NS if built-in NS is enabled,
749 // i.e., replace the software NS with the built-in NS.
Karl Wibergbe579832015-11-10 22:34:18 +0100750 options.noise_suppression = rtc::Optional<bool>(false);
henrikac14f5ff2015-09-23 14:08:33 +0200751 LOG(LS_INFO) << "Disabling NS since built-in NS will be used instead";
752 }
753 }
kwiberg102c6a62015-10-30 02:47:38 -0700754 if (voep->SetNsStatus(*options.noise_suppression, ns_mode) == -1) {
755 LOG_RTCERR2(SetNsStatus, *options.noise_suppression, ns_mode);
buildbot@webrtc.org13d67762014-05-02 17:33:29 +0000756 return false;
757 } else {
kwiberg102c6a62015-10-30 02:47:38 -0700758 LOG(LS_INFO) << "Noise suppression set to " << *options.noise_suppression
henrikac14f5ff2015-09-23 14:08:33 +0200759 << " with mode " << ns_mode;
buildbot@webrtc.org13d67762014-05-02 17:33:29 +0000760 }
761 }
762
kwiberg102c6a62015-10-30 02:47:38 -0700763 if (options.highpass_filter) {
764 LOG(LS_INFO) << "High pass filter enabled? " << *options.highpass_filter;
765 if (voep->EnableHighPassFilter(*options.highpass_filter) == -1) {
766 LOG_RTCERR1(SetHighpassFilterStatus, *options.highpass_filter);
buildbot@webrtc.org13d67762014-05-02 17:33:29 +0000767 return false;
768 }
769 }
770
kwiberg102c6a62015-10-30 02:47:38 -0700771 if (options.stereo_swapping) {
772 LOG(LS_INFO) << "Stereo swapping enabled? " << *options.stereo_swapping;
773 voep->EnableStereoChannelSwapping(*options.stereo_swapping);
774 if (voep->IsStereoChannelSwappingEnabled() != *options.stereo_swapping) {
775 LOG_RTCERR1(EnableStereoChannelSwapping, *options.stereo_swapping);
buildbot@webrtc.org13d67762014-05-02 17:33:29 +0000776 return false;
777 }
778 }
779
kwiberg102c6a62015-10-30 02:47:38 -0700780 if (options.audio_jitter_buffer_max_packets) {
781 LOG(LS_INFO) << "NetEq capacity is "
782 << *options.audio_jitter_buffer_max_packets;
Henrik Lundin64dad832015-05-11 12:44:23 +0200783 voe_config_.Set<webrtc::NetEqCapacityConfig>(
kwiberg102c6a62015-10-30 02:47:38 -0700784 new webrtc::NetEqCapacityConfig(
785 *options.audio_jitter_buffer_max_packets));
Henrik Lundin64dad832015-05-11 12:44:23 +0200786 }
787
kwiberg102c6a62015-10-30 02:47:38 -0700788 if (options.audio_jitter_buffer_fast_accelerate) {
789 LOG(LS_INFO) << "NetEq fast mode? "
790 << *options.audio_jitter_buffer_fast_accelerate;
Henrik Lundin5263b3c2015-06-01 10:29:41 +0200791 voe_config_.Set<webrtc::NetEqFastAccelerate>(
kwiberg102c6a62015-10-30 02:47:38 -0700792 new webrtc::NetEqFastAccelerate(
793 *options.audio_jitter_buffer_fast_accelerate));
Henrik Lundin5263b3c2015-06-01 10:29:41 +0200794 }
795
kwiberg102c6a62015-10-30 02:47:38 -0700796 if (options.typing_detection) {
797 LOG(LS_INFO) << "Typing detection is enabled? "
798 << *options.typing_detection;
799 if (voep->SetTypingDetectionStatus(*options.typing_detection) == -1) {
buildbot@webrtc.org13d67762014-05-02 17:33:29 +0000800 // In case of error, log the info and continue
kwiberg102c6a62015-10-30 02:47:38 -0700801 LOG_RTCERR1(SetTypingDetectionStatus, *options.typing_detection);
buildbot@webrtc.org13d67762014-05-02 17:33:29 +0000802 }
803 }
804
kwiberg102c6a62015-10-30 02:47:38 -0700805 if (options.adjust_agc_delta) {
806 LOG(LS_INFO) << "Adjust agc delta is " << *options.adjust_agc_delta;
807 if (!AdjustAgcLevel(*options.adjust_agc_delta)) {
buildbot@webrtc.org13d67762014-05-02 17:33:29 +0000808 return false;
809 }
810 }
811
kwiberg102c6a62015-10-30 02:47:38 -0700812 if (options.aec_dump) {
813 LOG(LS_INFO) << "Aec dump is enabled? " << *options.aec_dump;
814 if (*options.aec_dump)
buildbot@webrtc.org13d67762014-05-02 17:33:29 +0000815 StartAecDump(kAecDumpByAudioOptionFilename);
816 else
817 StopAecDump();
818 }
819
buildbot@webrtc.org1f8a2372014-08-28 10:52:44 +0000820 webrtc::Config config;
821
kwiberg102c6a62015-10-30 02:47:38 -0700822 if (options.delay_agnostic_aec)
823 delay_agnostic_aec_ = options.delay_agnostic_aec;
824 if (delay_agnostic_aec_) {
825 LOG(LS_INFO) << "Delay agnostic aec is enabled? " << *delay_agnostic_aec_;
henrik.lundin0f133b92015-07-02 00:17:55 -0700826 config.Set<webrtc::DelayAgnostic>(
kwiberg102c6a62015-10-30 02:47:38 -0700827 new webrtc::DelayAgnostic(*delay_agnostic_aec_));
Bjorn Volckerbf395c12015-03-25 22:45:56 +0100828 }
829
kwiberg102c6a62015-10-30 02:47:38 -0700830 if (options.extended_filter_aec) {
831 extended_filter_aec_ = options.extended_filter_aec;
832 }
833 if (extended_filter_aec_) {
834 LOG(LS_INFO) << "Extended filter aec is enabled? " << *extended_filter_aec_;
Henrik Lundin441f6342015-06-09 16:03:13 +0200835 config.Set<webrtc::ExtendedFilter>(
kwiberg102c6a62015-10-30 02:47:38 -0700836 new webrtc::ExtendedFilter(*extended_filter_aec_));
buildbot@webrtc.org1f8a2372014-08-28 10:52:44 +0000837 }
838
kwiberg102c6a62015-10-30 02:47:38 -0700839 if (options.experimental_ns) {
840 experimental_ns_ = options.experimental_ns;
841 }
842 if (experimental_ns_) {
843 LOG(LS_INFO) << "Experimental ns is enabled? " << *experimental_ns_;
buildbot@webrtc.org1f8a2372014-08-28 10:52:44 +0000844 config.Set<webrtc::ExperimentalNs>(
kwiberg102c6a62015-10-30 02:47:38 -0700845 new webrtc::ExperimentalNs(*experimental_ns_));
buildbot@webrtc.org1f8a2372014-08-28 10:52:44 +0000846 }
buildbot@webrtc.org1f8a2372014-08-28 10:52:44 +0000847
848 // We check audioproc for the benefit of tests, since FakeWebRtcVoiceEngine
849 // returns NULL on audio_processing().
850 webrtc::AudioProcessing* audioproc = voe_wrapper_->base()->audio_processing();
851 if (audioproc) {
852 audioproc->SetExtraOptions(config);
853 }
854
kwiberg102c6a62015-10-30 02:47:38 -0700855 if (options.recording_sample_rate) {
856 LOG(LS_INFO) << "Recording sample rate is "
857 << *options.recording_sample_rate;
858 if (voe_wrapper_->hw()->SetRecordingSampleRate(
859 *options.recording_sample_rate)) {
860 LOG_RTCERR1(SetRecordingSampleRate, *options.recording_sample_rate);
buildbot@webrtc.org13d67762014-05-02 17:33:29 +0000861 }
862 }
863
kwiberg102c6a62015-10-30 02:47:38 -0700864 if (options.playout_sample_rate) {
865 LOG(LS_INFO) << "Playout sample rate is " << *options.playout_sample_rate;
866 if (voe_wrapper_->hw()->SetPlayoutSampleRate(
867 *options.playout_sample_rate)) {
868 LOG_RTCERR1(SetPlayoutSampleRate, *options.playout_sample_rate);
buildbot@webrtc.org13d67762014-05-02 17:33:29 +0000869 }
870 }
871
872 return true;
873}
874
solenberg246b8172015-12-08 09:50:23 -0800875void WebRtcVoiceEngine::SetDefaultDevices() {
solenberg566ef242015-11-06 15:34:49 -0800876 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
kjellanderfcfc8042016-01-14 11:01:09 -0800877#if !defined(WEBRTC_IOS)
solenberg246b8172015-12-08 09:50:23 -0800878 int in_id = kDefaultAudioDeviceId;
879 int out_id = kDefaultAudioDeviceId;
880 LOG(LS_INFO) << "Setting microphone to (id=" << in_id
881 << ") and speaker to (id=" << out_id << ")";
buildbot@webrtc.org13d67762014-05-02 17:33:29 +0000882
solenbergc1a1b352015-09-22 13:31:20 -0700883 bool ret = true;
solenberg246b8172015-12-08 09:50:23 -0800884 if (voe_wrapper_->hw()->SetRecordingDevice(in_id) == -1) {
885 LOG_RTCERR1(SetRecordingDevice, in_id);
buildbot@webrtc.org13d67762014-05-02 17:33:29 +0000886 ret = false;
887 }
solenberg246b8172015-12-08 09:50:23 -0800888 webrtc::AudioProcessing* ap = voe()->base()->audio_processing();
889 if (ap) {
890 ap->Initialize();
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000891 }
892
solenberg246b8172015-12-08 09:50:23 -0800893 if (voe_wrapper_->hw()->SetPlayoutDevice(out_id) == -1) {
894 LOG_RTCERR1(SetPlayoutDevice, out_id);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000895 ret = false;
896 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000897
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000898 if (ret) {
solenberg246b8172015-12-08 09:50:23 -0800899 LOG(LS_INFO) << "Set microphone to (id=" << in_id
900 << ") and speaker to (id=" << out_id << ")";
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000901 }
kjellanderfcfc8042016-01-14 11:01:09 -0800902#endif // !WEBRTC_IOS
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000903}
904
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000905bool WebRtcVoiceEngine::GetOutputVolume(int* level) {
solenberg566ef242015-11-06 15:34:49 -0800906 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000907 unsigned int ulevel;
908 if (voe_wrapper_->volume()->GetSpeakerVolume(ulevel) == -1) {
909 LOG_RTCERR1(GetSpeakerVolume, level);
910 return false;
911 }
912 *level = ulevel;
913 return true;
914}
915
916bool WebRtcVoiceEngine::SetOutputVolume(int level) {
solenberg566ef242015-11-06 15:34:49 -0800917 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
henrikg91d6ede2015-09-17 00:24:34 -0700918 RTC_DCHECK(level >= 0 && level <= 255);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000919 if (voe_wrapper_->volume()->SetSpeakerVolume(level) == -1) {
920 LOG_RTCERR1(SetSpeakerVolume, level);
921 return false;
922 }
923 return true;
924}
925
926int WebRtcVoiceEngine::GetInputLevel() {
solenberg566ef242015-11-06 15:34:49 -0800927 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000928 unsigned int ulevel;
929 return (voe_wrapper_->volume()->GetSpeechInputLevel(ulevel) != -1) ?
930 static_cast<int>(ulevel) : -1;
931}
932
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000933const std::vector<AudioCodec>& WebRtcVoiceEngine::codecs() {
solenberg566ef242015-11-06 15:34:49 -0800934 RTC_DCHECK(signal_thread_checker_.CalledOnValidThread());
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000935 return codecs_;
936}
937
Stefan Holmer9d69c3f2015-12-07 10:45:43 +0100938RtpCapabilities WebRtcVoiceEngine::GetCapabilities() const {
solenberg566ef242015-11-06 15:34:49 -0800939 RTC_DCHECK(signal_thread_checker_.CalledOnValidThread());
Stefan Holmer9d69c3f2015-12-07 10:45:43 +0100940 RtpCapabilities capabilities;
941 capabilities.header_extensions.push_back(RtpHeaderExtension(
942 kRtpAudioLevelHeaderExtension, kRtpAudioLevelHeaderExtensionDefaultId));
943 capabilities.header_extensions.push_back(
944 RtpHeaderExtension(kRtpAbsoluteSenderTimeHeaderExtension,
945 kRtpAbsoluteSenderTimeHeaderExtensionDefaultId));
Stefan Holmer9d69c3f2015-12-07 10:45:43 +0100946 return capabilities;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000947}
948
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000949int WebRtcVoiceEngine::GetLastEngineError() {
solenberg566ef242015-11-06 15:34:49 -0800950 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000951 return voe_wrapper_->error();
952}
953
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000954void WebRtcVoiceEngine::Print(webrtc::TraceLevel level, const char* trace,
955 int length) {
solenberg566ef242015-11-06 15:34:49 -0800956 // Note: This callback can happen on any thread!
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000957 rtc::LoggingSeverity sev = rtc::LS_VERBOSE;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000958 if (level == webrtc::kTraceError || level == webrtc::kTraceCritical)
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000959 sev = rtc::LS_ERROR;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000960 else if (level == webrtc::kTraceWarning)
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000961 sev = rtc::LS_WARNING;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000962 else if (level == webrtc::kTraceStateInfo || level == webrtc::kTraceInfo)
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000963 sev = rtc::LS_INFO;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000964 else if (level == webrtc::kTraceTerseInfo)
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000965 sev = rtc::LS_INFO;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000966
967 // Skip past boilerplate prefix text
968 if (length < 72) {
969 std::string msg(trace, length);
970 LOG(LS_ERROR) << "Malformed webrtc log message: ";
971 LOG_V(sev) << msg;
972 } else {
973 std::string msg(trace + 71, length - 72);
Peter Boströmd5c75b12015-09-23 13:24:32 +0200974 LOG_V(sev) << "webrtc: " << msg;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000975 }
976}
977
solenberg63b34542015-09-29 06:06:31 -0700978void WebRtcVoiceEngine::RegisterChannel(WebRtcVoiceMediaChannel* channel) {
solenberg566ef242015-11-06 15:34:49 -0800979 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
980 RTC_DCHECK(channel);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000981 channels_.push_back(channel);
982}
983
solenberg63b34542015-09-29 06:06:31 -0700984void WebRtcVoiceEngine::UnregisterChannel(WebRtcVoiceMediaChannel* channel) {
solenberg566ef242015-11-06 15:34:49 -0800985 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
solenberg63b34542015-09-29 06:06:31 -0700986 auto it = std::find(channels_.begin(), channels_.end(), channel);
solenberg566ef242015-11-06 15:34:49 -0800987 RTC_DCHECK(it != channels_.end());
988 channels_.erase(it);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000989}
990
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000991// Adjusts the default AGC target level by the specified delta.
992// NB: If we start messing with other config fields, we'll want
993// to save the current webrtc::AgcConfig as well.
994bool WebRtcVoiceEngine::AdjustAgcLevel(int delta) {
solenberg566ef242015-11-06 15:34:49 -0800995 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000996 webrtc::AgcConfig config = default_agc_config_;
997 config.targetLeveldBOv -= delta;
998
999 LOG(LS_INFO) << "Adjusting AGC level from default -"
1000 << default_agc_config_.targetLeveldBOv << "dB to -"
1001 << config.targetLeveldBOv << "dB";
1002
1003 if (voe_wrapper_->processing()->SetAgcConfig(config) == -1) {
1004 LOG_RTCERR1(SetAgcConfig, config.targetLeveldBOv);
1005 return false;
1006 }
1007 return true;
1008}
1009
Fredrik Solenbergccb49e72015-05-19 11:37:56 +02001010bool WebRtcVoiceEngine::SetAudioDeviceModule(webrtc::AudioDeviceModule* adm) {
solenberg566ef242015-11-06 15:34:49 -08001011 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001012 if (initialized_) {
1013 LOG(LS_WARNING) << "SetAudioDeviceModule can not be called after Init.";
1014 return false;
1015 }
1016 if (adm_) {
1017 adm_->Release();
1018 adm_ = NULL;
1019 }
1020 if (adm) {
1021 adm_ = adm;
1022 adm_->AddRef();
1023 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001024 return true;
1025}
1026
ivocd66b44d2016-01-15 03:06:36 -08001027bool WebRtcVoiceEngine::StartAecDump(rtc::PlatformFile file,
1028 int64_t max_size_bytes) {
solenberg566ef242015-11-06 15:34:49 -08001029 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00001030 FILE* aec_dump_file_stream = rtc::FdopenPlatformFileForWriting(file);
wu@webrtc.orga8910d22014-01-23 22:12:45 +00001031 if (!aec_dump_file_stream) {
1032 LOG(LS_ERROR) << "Could not open AEC dump file stream.";
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00001033 if (!rtc::ClosePlatformFile(file))
wu@webrtc.orga8910d22014-01-23 22:12:45 +00001034 LOG(LS_WARNING) << "Could not close file.";
1035 return false;
1036 }
wu@webrtc.orga9890802013-12-13 00:21:03 +00001037 StopAecDump();
ivocd66b44d2016-01-15 03:06:36 -08001038 if (voe_wrapper_->base()->audio_processing()->StartDebugRecording(
1039 aec_dump_file_stream, max_size_bytes) !=
wu@webrtc.orga9890802013-12-13 00:21:03 +00001040 webrtc::AudioProcessing::kNoError) {
wu@webrtc.orga8910d22014-01-23 22:12:45 +00001041 LOG_RTCERR0(StartDebugRecording);
1042 fclose(aec_dump_file_stream);
wu@webrtc.orga9890802013-12-13 00:21:03 +00001043 return false;
1044 }
1045 is_dumping_aec_ = true;
1046 return true;
wu@webrtc.orga9890802013-12-13 00:21:03 +00001047}
1048
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001049void WebRtcVoiceEngine::StartAecDump(const std::string& filename) {
solenberg566ef242015-11-06 15:34:49 -08001050 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001051 if (!is_dumping_aec_) {
1052 // Start dumping AEC when we are not dumping.
ivocd66b44d2016-01-15 03:06:36 -08001053 if (voe_wrapper_->base()->audio_processing()->StartDebugRecording(
1054 filename.c_str(), -1) != webrtc::AudioProcessing::kNoError) {
wu@webrtc.orga9890802013-12-13 00:21:03 +00001055 LOG_RTCERR1(StartDebugRecording, filename.c_str());
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001056 } else {
1057 is_dumping_aec_ = true;
1058 }
1059 }
1060}
1061
1062void WebRtcVoiceEngine::StopAecDump() {
solenberg566ef242015-11-06 15:34:49 -08001063 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001064 if (is_dumping_aec_) {
1065 // Stop dumping AEC when we are dumping.
ivocd66b44d2016-01-15 03:06:36 -08001066 if (voe_wrapper_->base()->audio_processing()->StopDebugRecording() !=
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001067 webrtc::AudioProcessing::kNoError) {
1068 LOG_RTCERR0(StopDebugRecording);
1069 }
1070 is_dumping_aec_ = false;
1071 }
1072}
1073
ivoc112a3d82015-10-16 02:22:18 -07001074bool WebRtcVoiceEngine::StartRtcEventLog(rtc::PlatformFile file) {
solenberg566ef242015-11-06 15:34:49 -08001075 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
ivoc112a3d82015-10-16 02:22:18 -07001076 return voe_wrapper_->codec()->GetEventLog()->StartLogging(file);
1077}
1078
1079void WebRtcVoiceEngine::StopRtcEventLog() {
solenberg566ef242015-11-06 15:34:49 -08001080 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
ivoc112a3d82015-10-16 02:22:18 -07001081 voe_wrapper_->codec()->GetEventLog()->StopLogging();
1082}
1083
solenberg0a617e22015-10-20 15:49:38 -07001084int WebRtcVoiceEngine::CreateVoEChannel() {
solenberg566ef242015-11-06 15:34:49 -08001085 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
solenberg0a617e22015-10-20 15:49:38 -07001086 return voe_wrapper_->base()->CreateChannel(voe_config_);
sergeyu@chromium.org5bc25c42013-12-05 00:24:06 +00001087}
1088
solenbergc96df772015-10-21 13:01:53 -07001089class WebRtcVoiceMediaChannel::WebRtcAudioSendStream
mallinath@webrtc.org67ee6b92014-02-03 16:57:16 +00001090 : public AudioRenderer::Sink {
1091 public:
solenbergc96df772015-10-21 13:01:53 -07001092 WebRtcAudioSendStream(int ch, webrtc::AudioTransport* voe_audio_transport,
solenberg3a941542015-11-16 07:34:50 -08001093 uint32_t ssrc, const std::string& c_name,
1094 const std::vector<webrtc::RtpExtension>& extensions,
1095 webrtc::Call* call)
solenberg7add0582015-11-20 09:59:34 -08001096 : voe_audio_transport_(voe_audio_transport),
solenberg3a941542015-11-16 07:34:50 -08001097 call_(call),
1098 config_(nullptr) {
solenberg85a04962015-10-27 03:35:21 -07001099 RTC_DCHECK_GE(ch, 0);
1100 // TODO(solenberg): Once we're not using FakeWebRtcVoiceEngine anymore:
1101 // RTC_DCHECK(voe_audio_transport);
solenbergc96df772015-10-21 13:01:53 -07001102 RTC_DCHECK(call);
solenberg85a04962015-10-27 03:35:21 -07001103 audio_capture_thread_checker_.DetachFromThread();
solenberg3a941542015-11-16 07:34:50 -08001104 config_.rtp.ssrc = ssrc;
1105 config_.rtp.c_name = c_name;
1106 config_.voe_channel_id = ch;
1107 RecreateAudioSendStream(extensions);
solenbergc96df772015-10-21 13:01:53 -07001108 }
solenberg3a941542015-11-16 07:34:50 -08001109
solenbergc96df772015-10-21 13:01:53 -07001110 ~WebRtcAudioSendStream() override {
solenberg566ef242015-11-06 15:34:49 -08001111 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
solenbergc96df772015-10-21 13:01:53 -07001112 Stop();
1113 call_->DestroyAudioSendStream(stream_);
1114 }
henrike@webrtc.org1e09a712013-07-26 19:17:59 +00001115
solenberg3a941542015-11-16 07:34:50 -08001116 void RecreateAudioSendStream(
1117 const std::vector<webrtc::RtpExtension>& extensions) {
1118 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
1119 if (stream_) {
1120 call_->DestroyAudioSendStream(stream_);
1121 stream_ = nullptr;
1122 }
1123 config_.rtp.extensions = extensions;
1124 RTC_DCHECK(!stream_);
1125 stream_ = call_->CreateAudioSendStream(config_);
1126 RTC_CHECK(stream_);
1127 }
1128
Fredrik Solenbergb5727682015-12-04 15:22:19 +01001129 bool SendTelephoneEvent(int payload_type, uint8_t event,
1130 uint32_t duration_ms) {
1131 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
1132 RTC_DCHECK(stream_);
1133 return stream_->SendTelephoneEvent(payload_type, event, duration_ms);
1134 }
1135
solenberg3a941542015-11-16 07:34:50 -08001136 webrtc::AudioSendStream::Stats GetStats() const {
1137 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
1138 RTC_DCHECK(stream_);
1139 return stream_->GetStats();
1140 }
1141
mallinath@webrtc.org67ee6b92014-02-03 16:57:16 +00001142 // Starts the rendering by setting a sink to the renderer to get data
1143 // callback.
henrike@webrtc.orga7b98182014-02-21 15:51:43 +00001144 // This method is called on the libjingle worker thread.
mallinath@webrtc.org67ee6b92014-02-03 16:57:16 +00001145 // TODO(xians): Make sure Start() is called only once.
1146 void Start(AudioRenderer* renderer) {
solenberg566ef242015-11-06 15:34:49 -08001147 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
solenbergc96df772015-10-21 13:01:53 -07001148 RTC_DCHECK(renderer);
1149 if (renderer_) {
henrikg91d6ede2015-09-17 00:24:34 -07001150 RTC_DCHECK(renderer_ == renderer);
mallinath@webrtc.org67ee6b92014-02-03 16:57:16 +00001151 return;
1152 }
mallinath@webrtc.org67ee6b92014-02-03 16:57:16 +00001153 renderer->SetSink(this);
1154 renderer_ = renderer;
1155 }
1156
solenbergc96df772015-10-21 13:01:53 -07001157 // Stops rendering by setting the sink of the renderer to nullptr. No data
mallinath@webrtc.org67ee6b92014-02-03 16:57:16 +00001158 // callback will be received after this method.
henrike@webrtc.orga7b98182014-02-21 15:51:43 +00001159 // This method is called on the libjingle worker thread.
mallinath@webrtc.org67ee6b92014-02-03 16:57:16 +00001160 void Stop() {
solenberg566ef242015-11-06 15:34:49 -08001161 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
solenbergc96df772015-10-21 13:01:53 -07001162 if (renderer_) {
1163 renderer_->SetSink(nullptr);
1164 renderer_ = nullptr;
solenberg98c68862015-10-09 03:27:14 -07001165 }
mallinath@webrtc.org67ee6b92014-02-03 16:57:16 +00001166 }
1167
1168 // AudioRenderer::Sink implementation.
henrike@webrtc.orga7b98182014-02-21 15:51:43 +00001169 // This method is called on the audio thread.
kjellander@webrtc.org14665ff2015-03-04 12:58:35 +00001170 void OnData(const void* audio_data,
1171 int bits_per_sample,
1172 int sample_rate,
Peter Kasting69558702016-01-12 16:26:35 -08001173 size_t number_of_channels,
Peter Kastingdce40cf2015-08-24 14:52:23 -07001174 size_t number_of_frames) override {
solenberg566ef242015-11-06 15:34:49 -08001175 RTC_DCHECK(!worker_thread_checker_.CalledOnValidThread());
solenberg85a04962015-10-27 03:35:21 -07001176 RTC_DCHECK(audio_capture_thread_checker_.CalledOnValidThread());
solenbergc96df772015-10-21 13:01:53 -07001177 RTC_DCHECK(voe_audio_transport_);
solenberg7add0582015-11-20 09:59:34 -08001178 voe_audio_transport_->OnData(config_.voe_channel_id,
henrike@webrtc.orga7b98182014-02-21 15:51:43 +00001179 audio_data,
1180 bits_per_sample,
1181 sample_rate,
1182 number_of_channels,
1183 number_of_frames);
henrike@webrtc.orga7b98182014-02-21 15:51:43 +00001184 }
1185
1186 // Callback from the |renderer_| when it is going away. In case Start() has
1187 // never been called, this callback won't be triggered.
kjellander@webrtc.org14665ff2015-03-04 12:58:35 +00001188 void OnClose() override {
solenberg566ef242015-11-06 15:34:49 -08001189 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
solenbergc96df772015-10-21 13:01:53 -07001190 // Set |renderer_| to nullptr to make sure no more callback will get into
henrike@webrtc.orga7b98182014-02-21 15:51:43 +00001191 // the renderer.
solenbergc96df772015-10-21 13:01:53 -07001192 renderer_ = nullptr;
mallinath@webrtc.org67ee6b92014-02-03 16:57:16 +00001193 }
1194
1195 // Accessor to the VoE channel ID.
solenberg85a04962015-10-27 03:35:21 -07001196 int channel() const {
solenberg566ef242015-11-06 15:34:49 -08001197 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
solenberg7add0582015-11-20 09:59:34 -08001198 return config_.voe_channel_id;
solenberg85a04962015-10-27 03:35:21 -07001199 }
mallinath@webrtc.org67ee6b92014-02-03 16:57:16 +00001200
1201 private:
solenberg566ef242015-11-06 15:34:49 -08001202 rtc::ThreadChecker worker_thread_checker_;
solenberg85a04962015-10-27 03:35:21 -07001203 rtc::ThreadChecker audio_capture_thread_checker_;
solenbergc96df772015-10-21 13:01:53 -07001204 webrtc::AudioTransport* const voe_audio_transport_ = nullptr;
1205 webrtc::Call* call_ = nullptr;
solenberg3a941542015-11-16 07:34:50 -08001206 webrtc::AudioSendStream::Config config_;
1207 // The stream is owned by WebRtcAudioSendStream and may be reallocated if
1208 // configuration changes.
solenbergc96df772015-10-21 13:01:53 -07001209 webrtc::AudioSendStream* stream_ = nullptr;
mallinath@webrtc.org67ee6b92014-02-03 16:57:16 +00001210
1211 // Raw pointer to AudioRenderer owned by LocalAudioTrackHandler.
1212 // PeerConnection will make sure invalidating the pointer before the object
1213 // goes away.
solenbergc96df772015-10-21 13:01:53 -07001214 AudioRenderer* renderer_ = nullptr;
henrike@webrtc.orga7b98182014-02-21 15:51:43 +00001215
solenbergc96df772015-10-21 13:01:53 -07001216 RTC_DISALLOW_IMPLICIT_CONSTRUCTORS(WebRtcAudioSendStream);
1217};
1218
1219class WebRtcVoiceMediaChannel::WebRtcAudioReceiveStream {
1220 public:
solenberg7add0582015-11-20 09:59:34 -08001221 WebRtcAudioReceiveStream(int ch, uint32_t remote_ssrc, uint32_t local_ssrc,
1222 bool use_combined_bwe, const std::string& sync_group,
1223 const std::vector<webrtc::RtpExtension>& extensions,
1224 webrtc::Call* call)
1225 : call_(call),
1226 config_() {
1227 RTC_DCHECK_GE(ch, 0);
1228 RTC_DCHECK(call);
1229 config_.rtp.remote_ssrc = remote_ssrc;
1230 config_.rtp.local_ssrc = local_ssrc;
1231 config_.voe_channel_id = ch;
1232 config_.sync_group = sync_group;
1233 RecreateAudioReceiveStream(use_combined_bwe, extensions);
1234 }
solenbergc96df772015-10-21 13:01:53 -07001235
solenberg7add0582015-11-20 09:59:34 -08001236 ~WebRtcAudioReceiveStream() {
1237 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
1238 call_->DestroyAudioReceiveStream(stream_);
1239 }
1240
1241 void RecreateAudioReceiveStream(
1242 const std::vector<webrtc::RtpExtension>& extensions) {
1243 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
1244 RecreateAudioReceiveStream(config_.combined_audio_video_bwe, extensions);
1245 }
1246 void RecreateAudioReceiveStream(bool use_combined_bwe) {
1247 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
1248 RecreateAudioReceiveStream(use_combined_bwe, config_.rtp.extensions);
1249 }
1250
1251 webrtc::AudioReceiveStream::Stats GetStats() const {
1252 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
1253 RTC_DCHECK(stream_);
1254 return stream_->GetStats();
1255 }
1256
1257 int channel() const {
1258 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
1259 return config_.voe_channel_id;
1260 }
solenbergc96df772015-10-21 13:01:53 -07001261
deadbeef2d110be2016-01-13 12:00:26 -08001262 void SetRawAudioSink(rtc::scoped_ptr<webrtc::AudioSinkInterface> sink) {
Tommif888bb52015-12-12 01:37:01 +01001263 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
deadbeef2d110be2016-01-13 12:00:26 -08001264 stream_->SetSink(std::move(sink));
Tommif888bb52015-12-12 01:37:01 +01001265 }
1266
solenbergc96df772015-10-21 13:01:53 -07001267 private:
solenberg7add0582015-11-20 09:59:34 -08001268 void RecreateAudioReceiveStream(bool use_combined_bwe,
1269 const std::vector<webrtc::RtpExtension>& extensions) {
1270 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
1271 if (stream_) {
1272 call_->DestroyAudioReceiveStream(stream_);
1273 stream_ = nullptr;
1274 }
1275 config_.rtp.extensions = extensions;
1276 config_.combined_audio_video_bwe = use_combined_bwe;
1277 RTC_DCHECK(!stream_);
1278 stream_ = call_->CreateAudioReceiveStream(config_);
1279 RTC_CHECK(stream_);
1280 }
1281
1282 rtc::ThreadChecker worker_thread_checker_;
1283 webrtc::Call* call_ = nullptr;
1284 webrtc::AudioReceiveStream::Config config_;
1285 // The stream is owned by WebRtcAudioReceiveStream and may be reallocated if
1286 // configuration changes.
1287 webrtc::AudioReceiveStream* stream_ = nullptr;
solenbergc96df772015-10-21 13:01:53 -07001288
1289 RTC_DISALLOW_IMPLICIT_CONSTRUCTORS(WebRtcAudioReceiveStream);
henrike@webrtc.org1e09a712013-07-26 19:17:59 +00001290};
1291
Fredrik Solenberg709ed672015-09-15 12:26:33 +02001292WebRtcVoiceMediaChannel::WebRtcVoiceMediaChannel(WebRtcVoiceEngine* engine,
Fredrik Solenbergb071a192015-09-17 16:42:56 +02001293 const AudioOptions& options,
Fredrik Solenberg709ed672015-09-15 12:26:33 +02001294 webrtc::Call* call)
solenberg566ef242015-11-06 15:34:49 -08001295 : engine_(engine), call_(call) {
solenberg0a617e22015-10-20 15:49:38 -07001296 LOG(LS_VERBOSE) << "WebRtcVoiceMediaChannel::WebRtcVoiceMediaChannel";
solenberg566ef242015-11-06 15:34:49 -08001297 RTC_DCHECK(call);
solenberg0a617e22015-10-20 15:49:38 -07001298 engine->RegisterChannel(this);
Fredrik Solenbergb071a192015-09-17 16:42:56 +02001299 SetOptions(options);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001300}
1301
1302WebRtcVoiceMediaChannel::~WebRtcVoiceMediaChannel() {
solenberg566ef242015-11-06 15:34:49 -08001303 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
solenberg0a617e22015-10-20 15:49:38 -07001304 LOG(LS_VERBOSE) << "WebRtcVoiceMediaChannel::~WebRtcVoiceMediaChannel";
solenberg7add0582015-11-20 09:59:34 -08001305 // TODO(solenberg): Should be able to delete the streams directly, without
1306 // going through RemoveNnStream(), once stream objects handle
1307 // all (de)configuration.
solenbergc96df772015-10-21 13:01:53 -07001308 while (!send_streams_.empty()) {
1309 RemoveSendStream(send_streams_.begin()->first);
solenbergd97ec302015-10-07 01:40:33 -07001310 }
solenberg7add0582015-11-20 09:59:34 -08001311 while (!recv_streams_.empty()) {
1312 RemoveRecvStream(recv_streams_.begin()->first);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001313 }
solenberg0a617e22015-10-20 15:49:38 -07001314 engine()->UnregisterChannel(this);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001315}
1316
Peter Thatcherc2ee2c82015-08-07 16:05:34 -07001317bool WebRtcVoiceMediaChannel::SetSendParameters(
1318 const AudioSendParameters& params) {
solenberg566ef242015-11-06 15:34:49 -08001319 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
solenberg7e4e01a2015-12-02 08:05:01 -08001320 LOG(LS_INFO) << "WebRtcVoiceMediaChannel::SetSendParameters: "
1321 << params.ToString();
Peter Thatcherc2ee2c82015-08-07 16:05:34 -07001322 // TODO(pthatcher): Refactor this to be more clean now that we have
1323 // all the information at once.
solenberg3a941542015-11-16 07:34:50 -08001324
1325 if (!SetSendCodecs(params.codecs)) {
1326 return false;
1327 }
1328
solenberg7e4e01a2015-12-02 08:05:01 -08001329 if (!ValidateRtpExtensions(params.extensions)) {
1330 return false;
1331 }
1332 std::vector<webrtc::RtpExtension> filtered_extensions =
1333 FilterRtpExtensions(params.extensions,
1334 webrtc::RtpExtension::IsSupportedForAudio, true);
1335 if (send_rtp_extensions_ != filtered_extensions) {
1336 send_rtp_extensions_.swap(filtered_extensions);
solenberg3a941542015-11-16 07:34:50 -08001337 for (auto& it : send_streams_) {
1338 it.second->RecreateAudioSendStream(send_rtp_extensions_);
1339 }
1340 }
1341
1342 if (!SetMaxSendBandwidth(params.max_bandwidth_bps)) {
1343 return false;
1344 }
1345 return SetOptions(params.options);
Peter Thatcherc2ee2c82015-08-07 16:05:34 -07001346}
1347
1348bool WebRtcVoiceMediaChannel::SetRecvParameters(
1349 const AudioRecvParameters& params) {
solenberg566ef242015-11-06 15:34:49 -08001350 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
solenberg7e4e01a2015-12-02 08:05:01 -08001351 LOG(LS_INFO) << "WebRtcVoiceMediaChannel::SetRecvParameters: "
1352 << params.ToString();
Peter Thatcherc2ee2c82015-08-07 16:05:34 -07001353 // TODO(pthatcher): Refactor this to be more clean now that we have
1354 // all the information at once.
solenberg7add0582015-11-20 09:59:34 -08001355
1356 if (!SetRecvCodecs(params.codecs)) {
1357 return false;
1358 }
1359
solenberg7e4e01a2015-12-02 08:05:01 -08001360 if (!ValidateRtpExtensions(params.extensions)) {
1361 return false;
1362 }
1363 std::vector<webrtc::RtpExtension> filtered_extensions =
1364 FilterRtpExtensions(params.extensions,
1365 webrtc::RtpExtension::IsSupportedForAudio, false);
1366 if (recv_rtp_extensions_ != filtered_extensions) {
1367 recv_rtp_extensions_.swap(filtered_extensions);
solenberg7add0582015-11-20 09:59:34 -08001368 for (auto& it : recv_streams_) {
1369 it.second->RecreateAudioReceiveStream(recv_rtp_extensions_);
1370 }
1371 }
1372
1373 return true;
Peter Thatcherc2ee2c82015-08-07 16:05:34 -07001374}
1375
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001376bool WebRtcVoiceMediaChannel::SetOptions(const AudioOptions& options) {
solenberg566ef242015-11-06 15:34:49 -08001377 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001378 LOG(LS_INFO) << "Setting voice channel options: "
1379 << options.ToString();
1380
wu@webrtc.orgde305012013-10-31 15:40:38 +00001381 // Check if DSCP value is changed from previous.
1382 bool dscp_option_changed = (options_.dscp != options.dscp);
1383
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001384 // We retain all of the existing options, and apply the given ones
1385 // on top. This means there is no way to "clear" options such that
1386 // they go back to the engine default.
1387 options_.SetAll(options);
solenberg246b8172015-12-08 09:50:23 -08001388 if (!engine()->ApplyOptions(options_)) {
1389 LOG(LS_WARNING) <<
1390 "Failed to apply engine options during channel SetOptions.";
1391 return false;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001392 }
1393
wu@webrtc.orgde305012013-10-31 15:40:38 +00001394 if (dscp_option_changed) {
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00001395 rtc::DiffServCodePoint dscp = rtc::DSCP_DEFAULT;
solenberg246b8172015-12-08 09:50:23 -08001396 if (options_.dscp.value_or(false)) {
wu@webrtc.orgde305012013-10-31 15:40:38 +00001397 dscp = kAudioDscpValue;
solenberg246b8172015-12-08 09:50:23 -08001398 }
wu@webrtc.orgde305012013-10-31 15:40:38 +00001399 if (MediaChannel::SetDscp(dscp) != 0) {
1400 LOG(LS_WARNING) << "Failed to set DSCP settings for audio channel";
1401 }
1402 }
solenberg8fb30c32015-10-13 03:06:58 -07001403
solenbergc96df772015-10-21 13:01:53 -07001404 // TODO(solenberg): Don't recreate unless options changed.
solenberg7add0582015-11-20 09:59:34 -08001405 for (auto& it : recv_streams_) {
1406 it.second->RecreateAudioReceiveStream(
1407 options_.combined_audio_video_bwe.value_or(false));
1408 }
solenberg8fb30c32015-10-13 03:06:58 -07001409
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001410 LOG(LS_INFO) << "Set voice channel options. Current options: "
1411 << options_.ToString();
1412 return true;
1413}
1414
1415bool WebRtcVoiceMediaChannel::SetRecvCodecs(
1416 const std::vector<AudioCodec>& codecs) {
solenberg566ef242015-11-06 15:34:49 -08001417 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
solenberg8fb30c32015-10-13 03:06:58 -07001418
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001419 // Set the payload types to be used for incoming media.
solenberg0b675462015-10-09 01:37:09 -07001420 LOG(LS_INFO) << "Setting receive voice codecs.";
solenberg0b675462015-10-09 01:37:09 -07001421
1422 if (!VerifyUniquePayloadTypes(codecs)) {
1423 LOG(LS_ERROR) << "Codec payload types overlap.";
1424 return false;
1425 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001426
1427 std::vector<AudioCodec> new_codecs;
1428 // Find all new codecs. We allow adding new codecs but don't allow changing
1429 // the payload type of codecs that is already configured since we might
1430 // already be receiving packets with that payload type.
Fredrik Solenbergaf9fb212015-08-26 10:45:53 +02001431 for (const AudioCodec& codec : codecs) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001432 AudioCodec old_codec;
Fredrik Solenbergaf9fb212015-08-26 10:45:53 +02001433 if (FindCodec(recv_codecs_, codec, &old_codec)) {
1434 if (old_codec.id != codec.id) {
1435 LOG(LS_ERROR) << codec.name << " payload type changed.";
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001436 return false;
1437 }
1438 } else {
Fredrik Solenbergaf9fb212015-08-26 10:45:53 +02001439 new_codecs.push_back(codec);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001440 }
1441 }
1442 if (new_codecs.empty()) {
1443 // There are no new codecs to configure. Already configured codecs are
1444 // never removed.
1445 return true;
1446 }
1447
1448 if (playout_) {
1449 // Receive codecs can not be changed while playing. So we temporarily
1450 // pause playout.
1451 PausePlayout();
1452 }
1453
solenberg26c8c912015-11-27 04:00:25 -08001454 bool result = true;
1455 for (const AudioCodec& codec : new_codecs) {
1456 webrtc::CodecInst voe_codec;
1457 if (WebRtcVoiceEngine::ToCodecInst(codec, &voe_codec)) {
1458 LOG(LS_INFO) << ToString(codec);
1459 voe_codec.pltype = codec.id;
1460 for (const auto& ch : recv_streams_) {
1461 if (engine()->voe()->codec()->SetRecPayloadType(
1462 ch.second->channel(), voe_codec) == -1) {
1463 LOG_RTCERR2(SetRecPayloadType, ch.second->channel(),
1464 ToString(voe_codec));
1465 result = false;
1466 }
1467 }
1468 } else {
1469 LOG(LS_WARNING) << "Unknown codec " << ToString(codec);
1470 result = false;
1471 break;
1472 }
1473 }
Fredrik Solenbergaf9fb212015-08-26 10:45:53 +02001474 if (result) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001475 recv_codecs_ = codecs;
1476 }
1477
1478 if (desired_playout_ && !playout_) {
1479 ResumePlayout();
1480 }
Fredrik Solenbergaf9fb212015-08-26 10:45:53 +02001481 return result;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001482}
1483
1484bool WebRtcVoiceMediaChannel::SetSendCodecs(
wu@webrtc.orgcadf9042013-08-30 21:24:16 +00001485 int channel, const std::vector<AudioCodec>& codecs) {
buildbot@webrtc.orgae740dd2014-06-17 10:56:41 +00001486 // Disable VAD, FEC, and RED unless we know the other side wants them.
wu@webrtc.orgcadf9042013-08-30 21:24:16 +00001487 engine()->voe()->codec()->SetVADStatus(channel, false);
1488 engine()->voe()->rtp()->SetNACKStatus(channel, false, 0);
buildbot@webrtc.orgae740dd2014-06-17 10:56:41 +00001489 engine()->voe()->rtp()->SetREDStatus(channel, false);
1490 engine()->voe()->codec()->SetFECStatus(channel, false);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001491
1492 // Scan through the list to figure out the codec to use for sending, along
Fredrik Solenbergb5727682015-12-04 15:22:19 +01001493 // with the proper configuration for VAD.
henrike@webrtc.org704bf9e2014-02-27 17:52:04 +00001494 bool found_send_codec = false;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001495 webrtc::CodecInst send_codec;
1496 memset(&send_codec, 0, sizeof(send_codec));
1497
wu@webrtc.org05e7b442014-04-01 17:44:24 +00001498 bool nack_enabled = nack_enabled_;
buildbot@webrtc.org3ffa1f92014-07-02 19:51:26 +00001499 bool enable_codec_fec = false;
Minyue Li7100dcd2015-03-27 05:05:59 +01001500 bool enable_opus_dtx = false;
minyue@webrtc.org26236952014-10-29 02:27:08 +00001501 int opus_max_playback_rate = 0;
buildbot@webrtc.org5d639b32014-09-10 07:57:12 +00001502
henrike@webrtc.org704bf9e2014-02-27 17:52:04 +00001503 // Set send codec (the first non-telephone-event/CN codec)
Fredrik Solenbergaf9fb212015-08-26 10:45:53 +02001504 for (const AudioCodec& codec : codecs) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001505 // Ignore codecs we don't know about. The negotiation step should prevent
1506 // this, but double-check to be sure.
1507 webrtc::CodecInst voe_codec;
solenberg26c8c912015-11-27 04:00:25 -08001508 if (!WebRtcVoiceEngine::ToCodecInst(codec, &voe_codec)) {
Fredrik Solenbergaf9fb212015-08-26 10:45:53 +02001509 LOG(LS_WARNING) << "Unknown codec " << ToString(codec);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001510 continue;
1511 }
1512
Fredrik Solenbergaf9fb212015-08-26 10:45:53 +02001513 if (IsCodec(codec, kDtmfCodecName) || IsCodec(codec, kCnCodecName)) {
henrike@webrtc.org704bf9e2014-02-27 17:52:04 +00001514 // Skip telephone-event/CN codec, which will be handled later.
1515 continue;
1516 }
1517
henrike@webrtc.org704bf9e2014-02-27 17:52:04 +00001518 // We'll use the first codec in the list to actually send audio data.
1519 // Be sure to use the payload type requested by the remote side.
buildbot@webrtc.orgae740dd2014-06-17 10:56:41 +00001520 // "red", for RED audio, is a special case where the actual codec to be
henrike@webrtc.org704bf9e2014-02-27 17:52:04 +00001521 // used is specified in params.
Fredrik Solenbergaf9fb212015-08-26 10:45:53 +02001522 if (IsCodec(codec, kRedCodecName)) {
henrike@webrtc.org704bf9e2014-02-27 17:52:04 +00001523 // Parse out the RED parameters. If we fail, just ignore RED;
1524 // we don't support all possible params/usage scenarios.
Fredrik Solenbergaf9fb212015-08-26 10:45:53 +02001525 if (!GetRedSendCodec(codec, codecs, &send_codec)) {
henrike@webrtc.org704bf9e2014-02-27 17:52:04 +00001526 continue;
1527 }
1528
1529 // Enable redundant encoding of the specified codec. Treat any
1530 // failure as a fatal internal error.
buildbot@webrtc.orgae740dd2014-06-17 10:56:41 +00001531 LOG(LS_INFO) << "Enabling RED on channel " << channel;
Fredrik Solenbergaf9fb212015-08-26 10:45:53 +02001532 if (engine()->voe()->rtp()->SetREDStatus(channel, true, codec.id) == -1) {
1533 LOG_RTCERR3(SetREDStatus, channel, true, codec.id);
henrike@webrtc.org704bf9e2014-02-27 17:52:04 +00001534 return false;
1535 }
1536 } else {
1537 send_codec = voe_codec;
Fredrik Solenbergaf9fb212015-08-26 10:45:53 +02001538 nack_enabled = IsNackEnabled(codec);
Minyue Li7100dcd2015-03-27 05:05:59 +01001539 // For Opus as the send codec, we are to determine inband FEC, maximum
1540 // playback rate, and opus internal dtx.
Fredrik Solenbergaf9fb212015-08-26 10:45:53 +02001541 if (IsCodec(codec, kOpusCodecName)) {
1542 GetOpusConfig(codec, &send_codec, &enable_codec_fec,
Minyue Li7100dcd2015-03-27 05:05:59 +01001543 &opus_max_playback_rate, &enable_opus_dtx);
buildbot@webrtc.org5d639b32014-09-10 07:57:12 +00001544 }
Brave Yao5225dd82015-03-26 07:39:19 +08001545
1546 // Set packet size if the AudioCodec param kCodecParamPTime is set.
1547 int ptime_ms = 0;
Fredrik Solenbergaf9fb212015-08-26 10:45:53 +02001548 if (codec.GetParam(kCodecParamPTime, &ptime_ms)) {
solenberg26c8c912015-11-27 04:00:25 -08001549 if (!WebRtcVoiceCodecs::SetPTimeAsPacketSize(&send_codec, ptime_ms)) {
Brave Yao5225dd82015-03-26 07:39:19 +08001550 LOG(LS_WARNING) << "Failed to set packet size for codec "
1551 << send_codec.plname;
1552 return false;
1553 }
1554 }
henrike@webrtc.org704bf9e2014-02-27 17:52:04 +00001555 }
1556 found_send_codec = true;
1557 break;
1558 }
1559
wu@webrtc.org05e7b442014-04-01 17:44:24 +00001560 if (nack_enabled_ != nack_enabled) {
1561 SetNack(channel, nack_enabled);
1562 nack_enabled_ = nack_enabled;
1563 }
1564
henrike@webrtc.org704bf9e2014-02-27 17:52:04 +00001565 if (!found_send_codec) {
1566 LOG(LS_WARNING) << "Received empty list of codecs.";
1567 return false;
1568 }
1569
1570 // Set the codec immediately, since SetVADStatus() depends on whether
1571 // the current codec is mono or stereo.
1572 if (!SetSendCodec(channel, send_codec))
1573 return false;
1574
buildbot@webrtc.org3ffa1f92014-07-02 19:51:26 +00001575 // FEC should be enabled after SetSendCodec.
1576 if (enable_codec_fec) {
1577 LOG(LS_INFO) << "Attempt to enable codec internal FEC on channel "
1578 << channel;
buildbot@webrtc.org3ffa1f92014-07-02 19:51:26 +00001579 if (engine()->voe()->codec()->SetFECStatus(channel, true) == -1) {
1580 // Enable codec internal FEC. Treat any failure as fatal internal error.
1581 LOG_RTCERR2(SetFECStatus, channel, true);
1582 return false;
1583 }
buildbot@webrtc.org3ffa1f92014-07-02 19:51:26 +00001584 }
1585
Minyue Li7100dcd2015-03-27 05:05:59 +01001586 if (IsCodec(send_codec, kOpusCodecName)) {
1587 // DTX and maxplaybackrate should be set after SetSendCodec. Because current
1588 // send codec has to be Opus.
1589
1590 // Set Opus internal DTX.
1591 LOG(LS_INFO) << "Attempt to "
solenbergbd138382015-11-20 16:08:07 -08001592 << (enable_opus_dtx ? "enable" : "disable")
Minyue Li7100dcd2015-03-27 05:05:59 +01001593 << " Opus DTX on channel "
buildbot@webrtc.org5d639b32014-09-10 07:57:12 +00001594 << channel;
Minyue Li7100dcd2015-03-27 05:05:59 +01001595 if (engine()->voe()->codec()->SetOpusDtx(channel, enable_opus_dtx)) {
1596 LOG_RTCERR2(SetOpusDtx, channel, enable_opus_dtx);
1597 return false;
1598 }
1599
1600 // If opus_max_playback_rate <= 0, the default maximum playback rate
1601 // (48 kHz) will be used.
1602 if (opus_max_playback_rate > 0) {
1603 LOG(LS_INFO) << "Attempt to set maximum playback rate to "
1604 << opus_max_playback_rate
1605 << " Hz on channel "
1606 << channel;
1607 if (engine()->voe()->codec()->SetOpusMaxPlaybackRate(
1608 channel, opus_max_playback_rate) == -1) {
1609 LOG_RTCERR2(SetOpusMaxPlaybackRate, channel, opus_max_playback_rate);
1610 return false;
1611 }
buildbot@webrtc.org5d639b32014-09-10 07:57:12 +00001612 }
buildbot@webrtc.org5d639b32014-09-10 07:57:12 +00001613 }
1614
henrike@webrtc.org704bf9e2014-02-27 17:52:04 +00001615 // Always update the |send_codec_| to the currently set send codec.
1616 send_codec_.reset(new webrtc::CodecInst(send_codec));
1617
minyue@webrtc.org26236952014-10-29 02:27:08 +00001618 if (send_bitrate_setting_) {
1619 SetSendBitrateInternal(send_bitrate_bps_);
henrike@webrtc.org704bf9e2014-02-27 17:52:04 +00001620 }
1621
Fredrik Solenbergb5727682015-12-04 15:22:19 +01001622 // Loop through the codecs list again to config the CN codec.
Fredrik Solenbergaf9fb212015-08-26 10:45:53 +02001623 for (const AudioCodec& codec : codecs) {
henrike@webrtc.org704bf9e2014-02-27 17:52:04 +00001624 // Ignore codecs we don't know about. The negotiation step should prevent
1625 // this, but double-check to be sure.
1626 webrtc::CodecInst voe_codec;
solenberg26c8c912015-11-27 04:00:25 -08001627 if (!WebRtcVoiceEngine::ToCodecInst(codec, &voe_codec)) {
Fredrik Solenbergaf9fb212015-08-26 10:45:53 +02001628 LOG(LS_WARNING) << "Unknown codec " << ToString(codec);
henrike@webrtc.org704bf9e2014-02-27 17:52:04 +00001629 continue;
1630 }
1631
Fredrik Solenbergb5727682015-12-04 15:22:19 +01001632 if (IsCodec(codec, kCnCodecName)) {
henrike@webrtc.org704bf9e2014-02-27 17:52:04 +00001633 // Turn voice activity detection/comfort noise on if supported.
1634 // Set the wideband CN payload type appropriately.
1635 // (narrowband always uses the static payload type 13).
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001636 webrtc::PayloadFrequencies cn_freq;
Fredrik Solenbergaf9fb212015-08-26 10:45:53 +02001637 switch (codec.clockrate) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001638 case 8000:
1639 cn_freq = webrtc::kFreq8000Hz;
1640 break;
1641 case 16000:
1642 cn_freq = webrtc::kFreq16000Hz;
1643 break;
1644 case 32000:
1645 cn_freq = webrtc::kFreq32000Hz;
1646 break;
1647 default:
Fredrik Solenbergaf9fb212015-08-26 10:45:53 +02001648 LOG(LS_WARNING) << "CN frequency " << codec.clockrate
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001649 << " not supported.";
1650 continue;
1651 }
wu@webrtc.orgcadf9042013-08-30 21:24:16 +00001652 // Set the CN payloadtype and the VAD status.
1653 // The CN payload type for 8000 Hz clockrate is fixed at 13.
1654 if (cn_freq != webrtc::kFreq8000Hz) {
1655 if (engine()->voe()->codec()->SetSendCNPayloadType(
Fredrik Solenbergaf9fb212015-08-26 10:45:53 +02001656 channel, codec.id, cn_freq) == -1) {
1657 LOG_RTCERR3(SetSendCNPayloadType, channel, codec.id, cn_freq);
wu@webrtc.orgcadf9042013-08-30 21:24:16 +00001658 // TODO(ajm): This failure condition will be removed from VoE.
1659 // Restore the return here when we update to a new enough webrtc.
1660 //
1661 // Not returning false because the SetSendCNPayloadType will fail if
1662 // the channel is already sending.
1663 // This can happen if the remote description is applied twice, for
1664 // example in the case of ROAP on top of JSEP, where both side will
1665 // send the offer.
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001666 }
wu@webrtc.orgcadf9042013-08-30 21:24:16 +00001667 }
wu@webrtc.orgcadf9042013-08-30 21:24:16 +00001668 // Only turn on VAD if we have a CN payload type that matches the
1669 // clockrate for the codec we are going to use.
Fredrik Solenbergaf9fb212015-08-26 10:45:53 +02001670 if (codec.clockrate == send_codec.plfreq && send_codec.channels != 2) {
Minyue Li7100dcd2015-03-27 05:05:59 +01001671 // TODO(minyue): If CN frequency == 48000 Hz is allowed, consider the
1672 // interaction between VAD and Opus FEC.
wu@webrtc.orgcadf9042013-08-30 21:24:16 +00001673 LOG(LS_INFO) << "Enabling VAD";
1674 if (engine()->voe()->codec()->SetVADStatus(channel, true) == -1) {
1675 LOG_RTCERR2(SetVADStatus, channel, true);
1676 return false;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001677 }
1678 }
1679 }
wu@webrtc.org1d1ffc92013-10-16 18:12:02 +00001680 }
wu@webrtc.orgcadf9042013-08-30 21:24:16 +00001681 return true;
1682}
1683
1684bool WebRtcVoiceMediaChannel::SetSendCodecs(
1685 const std::vector<AudioCodec>& codecs) {
solenberg566ef242015-11-06 15:34:49 -08001686 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
Fredrik Solenbergb5727682015-12-04 15:22:19 +01001687 // TODO(solenberg): Validate input - that payload types don't overlap, are
1688 // within range, filter out codecs we don't support,
1689 // redundant codecs etc.
solenbergd97ec302015-10-07 01:40:33 -07001690
Fredrik Solenbergb5727682015-12-04 15:22:19 +01001691 // Find the DTMF telephone event "codec" payload type.
1692 dtmf_payload_type_ = rtc::Optional<int>();
Fredrik Solenbergaf9fb212015-08-26 10:45:53 +02001693 for (const AudioCodec& codec : codecs) {
Fredrik Solenbergaf9fb212015-08-26 10:45:53 +02001694 if (IsCodec(codec, kDtmfCodecName)) {
Fredrik Solenbergb5727682015-12-04 15:22:19 +01001695 dtmf_payload_type_ = rtc::Optional<int>(codec.id);
1696 break;
wu@webrtc.orgcadf9042013-08-30 21:24:16 +00001697 }
1698 }
1699
1700 // Cache the codecs in order to configure the channel created later.
1701 send_codecs_ = codecs;
solenbergc96df772015-10-21 13:01:53 -07001702 for (const auto& ch : send_streams_) {
Fredrik Solenbergaf9fb212015-08-26 10:45:53 +02001703 if (!SetSendCodecs(ch.second->channel(), codecs)) {
wu@webrtc.orgcadf9042013-08-30 21:24:16 +00001704 return false;
1705 }
1706 }
1707
wu@webrtc.org05e7b442014-04-01 17:44:24 +00001708 // Set nack status on receive channels and update |nack_enabled_|.
solenberg7add0582015-11-20 09:59:34 -08001709 for (const auto& ch : recv_streams_) {
solenberg0a617e22015-10-20 15:49:38 -07001710 SetNack(ch.second->channel(), nack_enabled_);
wu@webrtc.org9dba5252013-08-05 20:36:57 +00001711 }
solenberg0a617e22015-10-20 15:49:38 -07001712
1713 return true;
wu@webrtc.org9dba5252013-08-05 20:36:57 +00001714}
1715
wu@webrtc.orgcadf9042013-08-30 21:24:16 +00001716void WebRtcVoiceMediaChannel::SetNack(int channel, bool nack_enabled) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001717 if (nack_enabled) {
wu@webrtc.orgcadf9042013-08-30 21:24:16 +00001718 LOG(LS_INFO) << "Enabling NACK for channel " << channel;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001719 engine()->voe()->rtp()->SetNACKStatus(channel, true, kNackMaxPackets);
1720 } else {
wu@webrtc.orgcadf9042013-08-30 21:24:16 +00001721 LOG(LS_INFO) << "Disabling NACK for channel " << channel;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001722 engine()->voe()->rtp()->SetNACKStatus(channel, false, 0);
1723 }
1724}
1725
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001726bool WebRtcVoiceMediaChannel::SetSendCodec(
wu@webrtc.org9dba5252013-08-05 20:36:57 +00001727 int channel, const webrtc::CodecInst& send_codec) {
1728 LOG(LS_INFO) << "Send channel " << channel << " selected voice codec "
1729 << ToString(send_codec) << ", bitrate=" << send_codec.rate;
1730
wu@webrtc.org05e7b442014-04-01 17:44:24 +00001731 webrtc::CodecInst current_codec;
1732 if (engine()->voe()->codec()->GetSendCodec(channel, current_codec) == 0 &&
1733 (send_codec == current_codec)) {
1734 // Codec is already configured, we can return without setting it again.
1735 return true;
1736 }
1737
wu@webrtc.org9dba5252013-08-05 20:36:57 +00001738 if (engine()->voe()->codec()->SetSendCodec(channel, send_codec) == -1) {
1739 LOG_RTCERR2(SetSendCodec, channel, ToString(send_codec));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001740 return false;
1741 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001742 return true;
1743}
1744
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001745bool WebRtcVoiceMediaChannel::SetPlayout(bool playout) {
1746 desired_playout_ = playout;
1747 return ChangePlayout(desired_playout_);
1748}
1749
1750bool WebRtcVoiceMediaChannel::PausePlayout() {
1751 return ChangePlayout(false);
1752}
1753
1754bool WebRtcVoiceMediaChannel::ResumePlayout() {
1755 return ChangePlayout(desired_playout_);
1756}
1757
1758bool WebRtcVoiceMediaChannel::ChangePlayout(bool playout) {
solenberg566ef242015-11-06 15:34:49 -08001759 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001760 if (playout_ == playout) {
1761 return true;
1762 }
1763
solenberg7add0582015-11-20 09:59:34 -08001764 for (const auto& ch : recv_streams_) {
Fredrik Solenbergaf9fb212015-08-26 10:45:53 +02001765 if (!SetPlayout(ch.second->channel(), playout)) {
henrike@webrtc.org1e09a712013-07-26 19:17:59 +00001766 LOG(LS_ERROR) << "SetPlayout " << playout << " on channel "
Fredrik Solenbergaf9fb212015-08-26 10:45:53 +02001767 << ch.second->channel() << " failed";
solenberg1ac56142015-10-13 03:58:19 -07001768 return false;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001769 }
1770 }
solenberg1ac56142015-10-13 03:58:19 -07001771 playout_ = playout;
1772 return true;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001773}
1774
1775bool WebRtcVoiceMediaChannel::SetSend(SendFlags send) {
1776 desired_send_ = send;
solenbergc96df772015-10-21 13:01:53 -07001777 if (!send_streams_.empty()) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001778 return ChangeSend(desired_send_);
solenbergc96df772015-10-21 13:01:53 -07001779 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001780 return true;
1781}
1782
1783bool WebRtcVoiceMediaChannel::PauseSend() {
1784 return ChangeSend(SEND_NOTHING);
1785}
1786
1787bool WebRtcVoiceMediaChannel::ResumeSend() {
1788 return ChangeSend(desired_send_);
1789}
1790
1791bool WebRtcVoiceMediaChannel::ChangeSend(SendFlags send) {
1792 if (send_ == send) {
1793 return true;
1794 }
1795
solenberg246b8172015-12-08 09:50:23 -08001796 // Apply channel specific options when channel is enabled for sending.
solenberg63b34542015-09-29 06:06:31 -07001797 if (send == SEND_MICROPHONE) {
1798 engine()->ApplyOptions(options_);
1799 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001800
wu@webrtc.org9dba5252013-08-05 20:36:57 +00001801 // Change the settings on each send channel.
solenbergc96df772015-10-21 13:01:53 -07001802 for (const auto& ch : send_streams_) {
solenberg63b34542015-09-29 06:06:31 -07001803 if (!ChangeSend(ch.second->channel(), send)) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001804 return false;
solenberg63b34542015-09-29 06:06:31 -07001805 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001806 }
wu@webrtc.org9dba5252013-08-05 20:36:57 +00001807
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001808 send_ = send;
1809 return true;
1810}
1811
wu@webrtc.org9dba5252013-08-05 20:36:57 +00001812bool WebRtcVoiceMediaChannel::ChangeSend(int channel, SendFlags send) {
1813 if (send == SEND_MICROPHONE) {
1814 if (engine()->voe()->base()->StartSend(channel) == -1) {
1815 LOG_RTCERR1(StartSend, channel);
1816 return false;
1817 }
wu@webrtc.org9dba5252013-08-05 20:36:57 +00001818 } else { // SEND_NOTHING
henrikg91d6ede2015-09-17 00:24:34 -07001819 RTC_DCHECK(send == SEND_NOTHING);
wu@webrtc.org9dba5252013-08-05 20:36:57 +00001820 if (engine()->voe()->base()->StopSend(channel) == -1) {
1821 LOG_RTCERR1(StopSend, channel);
1822 return false;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001823 }
1824 }
1825
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001826 return true;
1827}
1828
Peter Boström0c4e06b2015-10-07 12:23:21 +02001829bool WebRtcVoiceMediaChannel::SetAudioSend(uint32_t ssrc,
1830 bool enable,
solenberg1dd98f32015-09-10 01:57:14 -07001831 const AudioOptions* options,
1832 AudioRenderer* renderer) {
solenberg566ef242015-11-06 15:34:49 -08001833 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
solenberg1dd98f32015-09-10 01:57:14 -07001834 // TODO(solenberg): The state change should be fully rolled back if any one of
1835 // these calls fail.
1836 if (!SetLocalRenderer(ssrc, renderer)) {
1837 return false;
1838 }
solenbergdfc8f4f2015-10-01 02:31:10 -07001839 if (!MuteStream(ssrc, !enable)) {
solenberg1dd98f32015-09-10 01:57:14 -07001840 return false;
1841 }
solenbergdfc8f4f2015-10-01 02:31:10 -07001842 if (enable && options) {
solenberg1dd98f32015-09-10 01:57:14 -07001843 return SetOptions(*options);
1844 }
1845 return true;
1846}
1847
solenberg0a617e22015-10-20 15:49:38 -07001848int WebRtcVoiceMediaChannel::CreateVoEChannel() {
1849 int id = engine()->CreateVoEChannel();
1850 if (id == -1) {
1851 LOG_RTCERR0(CreateVoEChannel);
1852 return -1;
wu@webrtc.org9dba5252013-08-05 20:36:57 +00001853 }
solenberg0a617e22015-10-20 15:49:38 -07001854 if (engine()->voe()->network()->RegisterExternalTransport(id, *this) == -1) {
1855 LOG_RTCERR2(RegisterExternalTransport, id, this);
1856 engine()->voe()->base()->DeleteChannel(id);
1857 return -1;
1858 }
1859 return id;
wu@webrtc.org9dba5252013-08-05 20:36:57 +00001860}
1861
solenberg7add0582015-11-20 09:59:34 -08001862bool WebRtcVoiceMediaChannel::DeleteVoEChannel(int channel) {
wu@webrtc.org9dba5252013-08-05 20:36:57 +00001863 if (engine()->voe()->network()->DeRegisterExternalTransport(channel) == -1) {
1864 LOG_RTCERR1(DeRegisterExternalTransport, channel);
1865 }
wu@webrtc.org9dba5252013-08-05 20:36:57 +00001866 if (engine()->voe()->base()->DeleteChannel(channel) == -1) {
1867 LOG_RTCERR1(DeleteChannel, channel);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001868 return false;
1869 }
wu@webrtc.org9dba5252013-08-05 20:36:57 +00001870 return true;
1871}
henrike@webrtc.org1e09a712013-07-26 19:17:59 +00001872
wu@webrtc.org9dba5252013-08-05 20:36:57 +00001873bool WebRtcVoiceMediaChannel::AddSendStream(const StreamParams& sp) {
solenberg566ef242015-11-06 15:34:49 -08001874 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
solenberg0a617e22015-10-20 15:49:38 -07001875 LOG(LS_INFO) << "AddSendStream: " << sp.ToString();
1876
1877 uint32_t ssrc = sp.first_ssrc();
1878 RTC_DCHECK(0 != ssrc);
1879
1880 if (GetSendChannelId(ssrc) != -1) {
1881 LOG(LS_ERROR) << "Stream already exists with ssrc " << ssrc;
wu@webrtc.org9dba5252013-08-05 20:36:57 +00001882 return false;
1883 }
1884
solenberg0a617e22015-10-20 15:49:38 -07001885 // Create a new channel for sending audio data.
1886 int channel = CreateVoEChannel();
1887 if (channel == -1) {
1888 return false;
wu@webrtc.org9dba5252013-08-05 20:36:57 +00001889 }
wu@webrtc.org9dba5252013-08-05 20:36:57 +00001890
solenbergc96df772015-10-21 13:01:53 -07001891 // Save the channel to send_streams_, so that RemoveSendStream() can still
wu@webrtc.org9dba5252013-08-05 20:36:57 +00001892 // delete the channel in case failure happens below.
mallinath@webrtc.org67ee6b92014-02-03 16:57:16 +00001893 webrtc::AudioTransport* audio_transport =
1894 engine()->voe()->base()->audio_transport();
solenberg3a941542015-11-16 07:34:50 -08001895 send_streams_.insert(std::make_pair(ssrc, new WebRtcAudioSendStream(
1896 channel, audio_transport, ssrc, sp.cname, send_rtp_extensions_, call_)));
wu@webrtc.org9dba5252013-08-05 20:36:57 +00001897
solenberg0a617e22015-10-20 15:49:38 -07001898 // Set the current codecs to be used for the new channel. We need to do this
1899 // after adding the channel to send_channels_, because of how max bitrate is
1900 // currently being configured by SetSendCodec().
1901 if (!send_codecs_.empty() && !SetSendCodecs(channel, send_codecs_)) {
1902 RemoveSendStream(ssrc);
wu@webrtc.org9dba5252013-08-05 20:36:57 +00001903 return false;
1904 }
1905
1906 // At this point the channel's local SSRC has been updated. If the channel is
solenberg0a617e22015-10-20 15:49:38 -07001907 // the first send channel make sure that all the receive channels are updated
1908 // with the same SSRC in order to send receiver reports.
solenbergc96df772015-10-21 13:01:53 -07001909 if (send_streams_.size() == 1) {
solenberg0a617e22015-10-20 15:49:38 -07001910 receiver_reports_ssrc_ = ssrc;
solenberg7add0582015-11-20 09:59:34 -08001911 for (const auto& stream : recv_streams_) {
1912 int recv_channel = stream.second->channel();
solenberg0a617e22015-10-20 15:49:38 -07001913 if (engine()->voe()->rtp()->SetLocalSSRC(recv_channel, ssrc) != 0) {
solenberg7add0582015-11-20 09:59:34 -08001914 LOG_RTCERR2(SetLocalSSRC, recv_channel, ssrc);
solenberg1ac56142015-10-13 03:58:19 -07001915 return false;
wu@webrtc.org9dba5252013-08-05 20:36:57 +00001916 }
solenberg0a617e22015-10-20 15:49:38 -07001917 engine()->voe()->base()->AssociateSendChannel(recv_channel, channel);
1918 LOG(LS_INFO) << "VoiceEngine channel #" << recv_channel
1919 << " is associated with channel #" << channel << ".";
wu@webrtc.org9dba5252013-08-05 20:36:57 +00001920 }
1921 }
1922
wu@webrtc.org9dba5252013-08-05 20:36:57 +00001923 return ChangeSend(channel, desired_send_);
1924}
1925
Peter Boström0c4e06b2015-10-07 12:23:21 +02001926bool WebRtcVoiceMediaChannel::RemoveSendStream(uint32_t ssrc) {
solenberg566ef242015-11-06 15:34:49 -08001927 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
solenberg3a941542015-11-16 07:34:50 -08001928 LOG(LS_INFO) << "RemoveSendStream: " << ssrc;
1929
solenbergc96df772015-10-21 13:01:53 -07001930 auto it = send_streams_.find(ssrc);
1931 if (it == send_streams_.end()) {
wu@webrtc.org9dba5252013-08-05 20:36:57 +00001932 LOG(LS_WARNING) << "Try to remove stream with ssrc " << ssrc
1933 << " which doesn't exist.";
1934 return false;
1935 }
1936
mallinath@webrtc.org67ee6b92014-02-03 16:57:16 +00001937 int channel = it->second->channel();
wu@webrtc.org9dba5252013-08-05 20:36:57 +00001938 ChangeSend(channel, SEND_NOTHING);
1939
solenberg7add0582015-11-20 09:59:34 -08001940 // Clean up and delete the send stream+channel.
solenberg0a617e22015-10-20 15:49:38 -07001941 LOG(LS_INFO) << "Removing audio send stream " << ssrc
1942 << " with VoiceEngine channel #" << channel << ".";
solenberg7add0582015-11-20 09:59:34 -08001943 delete it->second;
1944 send_streams_.erase(it);
1945 if (!DeleteVoEChannel(channel)) {
solenberg0a617e22015-10-20 15:49:38 -07001946 return false;
wu@webrtc.org9dba5252013-08-05 20:36:57 +00001947 }
solenbergc96df772015-10-21 13:01:53 -07001948 if (send_streams_.empty()) {
wu@webrtc.org9dba5252013-08-05 20:36:57 +00001949 ChangeSend(SEND_NOTHING);
solenberg0a617e22015-10-20 15:49:38 -07001950 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001951 return true;
1952}
1953
1954bool WebRtcVoiceMediaChannel::AddRecvStream(const StreamParams& sp) {
solenberg566ef242015-11-06 15:34:49 -08001955 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
solenbergd97ec302015-10-07 01:40:33 -07001956 LOG(LS_INFO) << "AddRecvStream: " << sp.ToString();
1957
solenberg0b675462015-10-09 01:37:09 -07001958 if (!ValidateStreamParams(sp)) {
wu@webrtc.org78187522013-10-07 23:32:02 +00001959 return false;
1960 }
1961
solenberg7add0582015-11-20 09:59:34 -08001962 const uint32_t ssrc = sp.first_ssrc();
solenberg0b675462015-10-09 01:37:09 -07001963 if (ssrc == 0) {
1964 LOG(LS_WARNING) << "AddRecvStream with ssrc==0 is not supported.";
1965 return false;
1966 }
1967
solenberg1ac56142015-10-13 03:58:19 -07001968 // Remove the default receive stream if one had been created with this ssrc;
1969 // we'll recreate it then.
1970 if (IsDefaultRecvStream(ssrc)) {
1971 RemoveRecvStream(ssrc);
1972 }
solenberg0b675462015-10-09 01:37:09 -07001973
solenberg7add0582015-11-20 09:59:34 -08001974 if (GetReceiveChannelId(ssrc) != -1) {
henrike@webrtc.org1e09a712013-07-26 19:17:59 +00001975 LOG(LS_ERROR) << "Stream already exists with ssrc " << ssrc;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001976 return false;
1977 }
Fredrik Solenberg4b60c732015-05-07 14:07:48 +02001978
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001979 // Create a new channel for receiving audio data.
solenberg7add0582015-11-20 09:59:34 -08001980 const int channel = CreateVoEChannel();
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001981 if (channel == -1) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001982 return false;
1983 }
Minyue2013aec2015-05-13 14:14:42 +02001984
solenberg1ac56142015-10-13 03:58:19 -07001985 // Turn off all supported codecs.
solenberg26c8c912015-11-27 04:00:25 -08001986 // TODO(solenberg): Remove once "no codecs" is the default state of a stream.
1987 for (webrtc::CodecInst voe_codec : webrtc::acm2::RentACodec::Database()) {
1988 voe_codec.pltype = -1;
1989 if (engine()->voe()->codec()->SetRecPayloadType(channel, voe_codec) == -1) {
1990 LOG_RTCERR2(SetRecPayloadType, channel, ToString(voe_codec));
1991 DeleteVoEChannel(channel);
1992 return false;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001993 }
1994 }
1995
solenberg1ac56142015-10-13 03:58:19 -07001996 // Only enable those configured for this channel.
1997 for (const auto& codec : recv_codecs_) {
1998 webrtc::CodecInst voe_codec;
solenberg26c8c912015-11-27 04:00:25 -08001999 if (WebRtcVoiceEngine::ToCodecInst(codec, &voe_codec)) {
solenberg1ac56142015-10-13 03:58:19 -07002000 voe_codec.pltype = codec.id;
2001 if (engine()->voe()->codec()->SetRecPayloadType(
2002 channel, voe_codec) == -1) {
2003 LOG_RTCERR2(SetRecPayloadType, channel, ToString(voe_codec));
solenberg7add0582015-11-20 09:59:34 -08002004 DeleteVoEChannel(channel);
solenberg1ac56142015-10-13 03:58:19 -07002005 return false;
2006 }
henrike@webrtc.org1e09a712013-07-26 19:17:59 +00002007 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002008 }
solenberg8fb30c32015-10-13 03:06:58 -07002009
solenberg7add0582015-11-20 09:59:34 -08002010 const int send_channel = GetSendChannelId(receiver_reports_ssrc_);
2011 if (send_channel != -1) {
2012 // Associate receive channel with first send channel (so the receive channel
2013 // can obtain RTT from the send channel)
2014 engine()->voe()->base()->AssociateSendChannel(channel, send_channel);
2015 LOG(LS_INFO) << "VoiceEngine channel #" << channel
2016 << " is associated with channel #" << send_channel << ".";
buildbot@webrtc.org150835e2014-05-06 15:54:38 +00002017 }
2018
solenberg7add0582015-11-20 09:59:34 -08002019 recv_streams_.insert(std::make_pair(ssrc, new WebRtcAudioReceiveStream(
2020 channel, ssrc, receiver_reports_ssrc_,
2021 options_.combined_audio_video_bwe.value_or(false), sp.sync_label,
2022 recv_rtp_extensions_, call_)));
2023
2024 SetNack(channel, nack_enabled_);
solenberg1ac56142015-10-13 03:58:19 -07002025 SetPlayout(channel, playout_);
solenberg7add0582015-11-20 09:59:34 -08002026
solenberg1ac56142015-10-13 03:58:19 -07002027 return true;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002028}
2029
Peter Boström0c4e06b2015-10-07 12:23:21 +02002030bool WebRtcVoiceMediaChannel::RemoveRecvStream(uint32_t ssrc) {
solenberg566ef242015-11-06 15:34:49 -08002031 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
solenbergd97ec302015-10-07 01:40:33 -07002032 LOG(LS_INFO) << "RemoveRecvStream: " << ssrc;
2033
solenberg7add0582015-11-20 09:59:34 -08002034 const auto it = recv_streams_.find(ssrc);
2035 if (it == recv_streams_.end()) {
wu@webrtc.org9dba5252013-08-05 20:36:57 +00002036 LOG(LS_WARNING) << "Try to remove stream with ssrc " << ssrc
2037 << " which doesn't exist.";
henrike@webrtc.org1e09a712013-07-26 19:17:59 +00002038 return false;
wu@webrtc.org9dba5252013-08-05 20:36:57 +00002039 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002040
solenberg1ac56142015-10-13 03:58:19 -07002041 // Deregister default channel, if that's the one being destroyed.
2042 if (IsDefaultRecvStream(ssrc)) {
2043 default_recv_ssrc_ = -1;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002044 }
henrike@webrtc.org1e09a712013-07-26 19:17:59 +00002045
solenberg7add0582015-11-20 09:59:34 -08002046 const int channel = it->second->channel();
2047
2048 // Clean up and delete the receive stream+channel.
2049 LOG(LS_INFO) << "Removing audio receive stream " << ssrc
mallinath@webrtc.org67ee6b92014-02-03 16:57:16 +00002050 << " with VoiceEngine channel #" << channel << ".";
Tommif888bb52015-12-12 01:37:01 +01002051 it->second->SetRawAudioSink(nullptr);
solenberg7add0582015-11-20 09:59:34 -08002052 delete it->second;
2053 recv_streams_.erase(it);
2054 return DeleteVoEChannel(channel);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002055}
2056
Peter Boström0c4e06b2015-10-07 12:23:21 +02002057bool WebRtcVoiceMediaChannel::SetLocalRenderer(uint32_t ssrc,
henrike@webrtc.org1e09a712013-07-26 19:17:59 +00002058 AudioRenderer* renderer) {
solenbergc96df772015-10-21 13:01:53 -07002059 auto it = send_streams_.find(ssrc);
2060 if (it == send_streams_.end()) {
wu@webrtc.org9dba5252013-08-05 20:36:57 +00002061 if (renderer) {
2062 // Return an error if trying to set a valid renderer with an invalid ssrc.
2063 LOG(LS_ERROR) << "SetLocalRenderer failed with ssrc "<< ssrc;
2064 return false;
2065 }
2066
2067 // The channel likely has gone away, do nothing.
henrike@webrtc.org1e09a712013-07-26 19:17:59 +00002068 return true;
henrike@webrtc.org1e09a712013-07-26 19:17:59 +00002069 }
2070
solenberg1ac56142015-10-13 03:58:19 -07002071 if (renderer) {
mallinath@webrtc.org67ee6b92014-02-03 16:57:16 +00002072 it->second->Start(renderer);
solenberg1ac56142015-10-13 03:58:19 -07002073 } else {
mallinath@webrtc.org67ee6b92014-02-03 16:57:16 +00002074 it->second->Stop();
solenberg1ac56142015-10-13 03:58:19 -07002075 }
henrike@webrtc.org1e09a712013-07-26 19:17:59 +00002076
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002077 return true;
2078}
2079
2080bool WebRtcVoiceMediaChannel::GetActiveStreams(
2081 AudioInfo::StreamList* actives) {
solenberg566ef242015-11-06 15:34:49 -08002082 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002083 actives->clear();
solenberg7add0582015-11-20 09:59:34 -08002084 for (const auto& ch : recv_streams_) {
Fredrik Solenbergaf9fb212015-08-26 10:45:53 +02002085 int level = GetOutputLevel(ch.second->channel());
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002086 if (level > 0) {
Fredrik Solenbergaf9fb212015-08-26 10:45:53 +02002087 actives->push_back(std::make_pair(ch.first, level));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002088 }
2089 }
2090 return true;
2091}
2092
2093int WebRtcVoiceMediaChannel::GetOutputLevel() {
solenberg566ef242015-11-06 15:34:49 -08002094 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
solenberg1ac56142015-10-13 03:58:19 -07002095 int highest = 0;
solenberg7add0582015-11-20 09:59:34 -08002096 for (const auto& ch : recv_streams_) {
solenberg8fb30c32015-10-13 03:06:58 -07002097 highest = std::max(GetOutputLevel(ch.second->channel()), highest);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002098 }
2099 return highest;
2100}
2101
2102int WebRtcVoiceMediaChannel::GetTimeSinceLastTyping() {
2103 int ret;
2104 if (engine()->voe()->processing()->TimeSinceLastTyping(ret) == -1) {
2105 // In case of error, log the info and continue
2106 LOG_RTCERR0(TimeSinceLastTyping);
2107 ret = -1;
2108 } else {
2109 ret *= 1000; // We return ms, webrtc returns seconds.
2110 }
2111 return ret;
2112}
2113
2114void WebRtcVoiceMediaChannel::SetTypingDetectionParameters(int time_window,
2115 int cost_per_typing, int reporting_threshold, int penalty_decay,
2116 int type_event_delay) {
2117 if (engine()->voe()->processing()->SetTypingDetectionParameters(
2118 time_window, cost_per_typing,
2119 reporting_threshold, penalty_decay, type_event_delay) == -1) {
2120 // In case of error, log the info and continue
2121 LOG_RTCERR5(SetTypingDetectionParameters, time_window,
2122 cost_per_typing, reporting_threshold, penalty_decay,
2123 type_event_delay);
2124 }
2125}
2126
solenberg4bac9c52015-10-09 02:32:53 -07002127bool WebRtcVoiceMediaChannel::SetOutputVolume(uint32_t ssrc, double volume) {
solenberg566ef242015-11-06 15:34:49 -08002128 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
solenberg1ac56142015-10-13 03:58:19 -07002129 if (ssrc == 0) {
2130 default_recv_volume_ = volume;
2131 if (default_recv_ssrc_ == -1) {
2132 return true;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002133 }
solenberg1ac56142015-10-13 03:58:19 -07002134 ssrc = static_cast<uint32_t>(default_recv_ssrc_);
2135 }
2136 int ch_id = GetReceiveChannelId(ssrc);
2137 if (ch_id < 0) {
2138 LOG(LS_WARNING) << "Cannot find channel for ssrc:" << ssrc;
2139 return false;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002140 }
2141
solenberg1ac56142015-10-13 03:58:19 -07002142 if (-1 == engine()->voe()->volume()->SetChannelOutputVolumeScaling(ch_id,
2143 volume)) {
2144 LOG_RTCERR2(SetChannelOutputVolumeScaling, ch_id, volume);
2145 return false;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002146 }
solenberg1ac56142015-10-13 03:58:19 -07002147 LOG(LS_INFO) << "SetOutputVolume to " << volume
2148 << " for channel " << ch_id << " and ssrc " << ssrc;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002149 return true;
2150}
2151
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002152bool WebRtcVoiceMediaChannel::CanInsertDtmf() {
Fredrik Solenbergb5727682015-12-04 15:22:19 +01002153 return dtmf_payload_type_ ? true : false;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002154}
2155
solenberg1d63dd02015-12-02 12:35:09 -08002156bool WebRtcVoiceMediaChannel::InsertDtmf(uint32_t ssrc, int event,
2157 int duration) {
solenberg566ef242015-11-06 15:34:49 -08002158 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
Fredrik Solenbergb5727682015-12-04 15:22:19 +01002159 LOG(LS_INFO) << "WebRtcVoiceMediaChannel::InsertDtmf";
2160 if (!dtmf_payload_type_) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002161 return false;
2162 }
2163
Fredrik Solenbergb5727682015-12-04 15:22:19 +01002164 // Figure out which WebRtcAudioSendStream to send the event on.
2165 auto it = ssrc != 0 ? send_streams_.find(ssrc) : send_streams_.begin();
2166 if (it == send_streams_.end()) {
2167 LOG(LS_WARNING) << "The specified ssrc " << ssrc << " is not in use.";
solenberg1d63dd02015-12-02 12:35:09 -08002168 return false;
2169 }
Fredrik Solenbergb5727682015-12-04 15:22:19 +01002170 if (event < kMinTelephoneEventCode ||
2171 event > kMaxTelephoneEventCode) {
2172 LOG(LS_WARNING) << "DTMF event code " << event << " out of range.";
solenberg1d63dd02015-12-02 12:35:09 -08002173 return false;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002174 }
Fredrik Solenbergb5727682015-12-04 15:22:19 +01002175 if (duration < kMinTelephoneEventDuration ||
2176 duration > kMaxTelephoneEventDuration) {
2177 LOG(LS_WARNING) << "DTMF event duration " << duration << " out of range.";
2178 return false;
2179 }
2180 return it->second->SendTelephoneEvent(*dtmf_payload_type_, event, duration);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002181}
2182
wu@webrtc.orga9890802013-12-13 00:21:03 +00002183void WebRtcVoiceMediaChannel::OnPacketReceived(
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00002184 rtc::Buffer* packet, const rtc::PacketTime& packet_time) {
solenberg566ef242015-11-06 15:34:49 -08002185 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
Fredrik Solenberg4b60c732015-05-07 14:07:48 +02002186
solenberg1ac56142015-10-13 03:58:19 -07002187 uint32_t ssrc = 0;
2188 if (!GetRtpSsrc(packet->data(), packet->size(), &ssrc)) {
2189 return;
2190 }
2191
solenberg7e63ef02015-11-20 00:19:43 -08002192 // If we don't have a default channel, and the SSRC is unknown, create a
2193 // default channel.
2194 if (default_recv_ssrc_ == -1 && GetReceiveChannelId(ssrc) == -1) {
solenberg1ac56142015-10-13 03:58:19 -07002195 StreamParams sp;
2196 sp.ssrcs.push_back(ssrc);
2197 LOG(LS_INFO) << "Creating default receive stream for SSRC=" << ssrc << ".";
2198 if (!AddRecvStream(sp)) {
2199 LOG(LS_WARNING) << "Could not create default receive stream.";
2200 return;
2201 }
2202 default_recv_ssrc_ = ssrc;
2203 SetOutputVolume(default_recv_ssrc_, default_recv_volume_);
deadbeef884f5852016-01-15 09:20:04 -08002204 if (default_sink_) {
2205 rtc::scoped_ptr<webrtc::AudioSinkInterface> proxy_sink(
2206 new ProxySink(default_sink_.get()));
2207 SetRawAudioSink(default_recv_ssrc_, std::move(proxy_sink));
2208 }
solenberg1ac56142015-10-13 03:58:19 -07002209 }
2210
2211 // Forward packet to Call. If the SSRC is unknown we'll return after this.
Fredrik Solenberg709ed672015-09-15 12:26:33 +02002212 const webrtc::PacketTime webrtc_packet_time(packet_time.timestamp,
2213 packet_time.not_before);
solenberg1ac56142015-10-13 03:58:19 -07002214 webrtc::PacketReceiver::DeliveryStatus delivery_result =
2215 call_->Receiver()->DeliverPacket(webrtc::MediaType::AUDIO,
2216 reinterpret_cast<const uint8_t*>(packet->data()), packet->size(),
2217 webrtc_packet_time);
2218 if (webrtc::PacketReceiver::DELIVERY_OK != delivery_result) {
solenberg7e63ef02015-11-20 00:19:43 -08002219 // If the SSRC is unknown here, route it to the default channel, if we have
2220 // one. See: https://bugs.chromium.org/p/webrtc/issues/detail?id=5208
2221 if (default_recv_ssrc_ == -1) {
2222 return;
2223 } else {
2224 ssrc = default_recv_ssrc_;
2225 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002226 }
2227
solenberg1ac56142015-10-13 03:58:19 -07002228 // Find the channel to send this packet to. It must exist since webrtc::Call
2229 // was able to demux the packet.
2230 int channel = GetReceiveChannelId(ssrc);
2231 RTC_DCHECK(channel != -1);
2232
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002233 // Pass it off to the decoder.
henrike@webrtc.org28654cb2013-07-22 21:07:49 +00002234 engine()->voe()->network()->ReceivedRTPPacket(
solenberg1ac56142015-10-13 03:58:19 -07002235 channel, packet->data(), packet->size(), webrtc_packet_time);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002236}
2237
wu@webrtc.orga9890802013-12-13 00:21:03 +00002238void WebRtcVoiceMediaChannel::OnRtcpReceived(
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00002239 rtc::Buffer* packet, const rtc::PacketTime& packet_time) {
solenberg566ef242015-11-06 15:34:49 -08002240 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
Fredrik Solenberg4b60c732015-05-07 14:07:48 +02002241
Fredrik Solenberg709ed672015-09-15 12:26:33 +02002242 // Forward packet to Call as well.
2243 const webrtc::PacketTime webrtc_packet_time(packet_time.timestamp,
2244 packet_time.not_before);
2245 call_->Receiver()->DeliverPacket(webrtc::MediaType::AUDIO,
2246 reinterpret_cast<const uint8_t*>(packet->data()), packet->size(),
2247 webrtc_packet_time);
Fredrik Solenberg4b60c732015-05-07 14:07:48 +02002248
wu@webrtc.org9dba5252013-08-05 20:36:57 +00002249 // Sending channels need all RTCP packets with feedback information.
2250 // Even sender reports can contain attached report blocks.
2251 // Receiving channels need sender reports in order to create
2252 // correct receiver reports.
2253 int type = 0;
kwiberg@webrtc.orgeebcab52015-03-24 09:19:06 +00002254 if (!GetRtcpType(packet->data(), packet->size(), &type)) {
wu@webrtc.org9dba5252013-08-05 20:36:57 +00002255 LOG(LS_WARNING) << "Failed to parse type from received RTCP packet";
2256 return;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002257 }
2258
solenberg0b675462015-10-09 01:37:09 -07002259 // If it is a sender report, find the receive channel that is listening.
wu@webrtc.org9dba5252013-08-05 20:36:57 +00002260 if (type == kRtcpTypeSR) {
solenberg0b675462015-10-09 01:37:09 -07002261 uint32_t ssrc = 0;
2262 if (!GetRtcpSsrc(packet->data(), packet->size(), &ssrc)) {
2263 return;
2264 }
2265 int recv_channel_id = GetReceiveChannelId(ssrc);
2266 if (recv_channel_id != -1) {
wu@webrtc.org9dba5252013-08-05 20:36:57 +00002267 engine()->voe()->network()->ReceivedRTCPPacket(
solenberg0b675462015-10-09 01:37:09 -07002268 recv_channel_id, packet->data(), packet->size());
wu@webrtc.org9dba5252013-08-05 20:36:57 +00002269 }
2270 }
2271
2272 // SR may continue RR and any RR entry may correspond to any one of the send
2273 // channels. So all RTCP packets must be forwarded all send channels. VoE
2274 // will filter out RR internally.
solenbergc96df772015-10-21 13:01:53 -07002275 for (const auto& ch : send_streams_) {
wu@webrtc.org9dba5252013-08-05 20:36:57 +00002276 engine()->voe()->network()->ReceivedRTCPPacket(
Fredrik Solenbergaf9fb212015-08-26 10:45:53 +02002277 ch.second->channel(), packet->data(), packet->size());
wu@webrtc.org9dba5252013-08-05 20:36:57 +00002278 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002279}
2280
Peter Boström0c4e06b2015-10-07 12:23:21 +02002281bool WebRtcVoiceMediaChannel::MuteStream(uint32_t ssrc, bool muted) {
solenberg566ef242015-11-06 15:34:49 -08002282 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
solenberg0a617e22015-10-20 15:49:38 -07002283 int channel = GetSendChannelId(ssrc);
wu@webrtc.org9dba5252013-08-05 20:36:57 +00002284 if (channel == -1) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002285 LOG(LS_WARNING) << "The specified ssrc " << ssrc << " is not in use.";
2286 return false;
2287 }
wu@webrtc.org9dba5252013-08-05 20:36:57 +00002288 if (engine()->voe()->volume()->SetInputMute(channel, muted) == -1) {
2289 LOG_RTCERR2(SetInputMute, channel, muted);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002290 return false;
2291 }
buildbot@webrtc.org6b21b712014-07-31 15:08:53 +00002292 // We set the AGC to mute state only when all the channels are muted.
2293 // This implementation is not ideal, instead we should signal the AGC when
2294 // the mic channel is muted/unmuted. We can't do it today because there
2295 // is no good way to know which stream is mapping to the mic channel.
2296 bool all_muted = muted;
solenbergc96df772015-10-21 13:01:53 -07002297 for (const auto& ch : send_streams_) {
Fredrik Solenbergaf9fb212015-08-26 10:45:53 +02002298 if (!all_muted) {
2299 break;
2300 }
2301 if (engine()->voe()->volume()->GetInputMute(ch.second->channel(),
buildbot@webrtc.org6b21b712014-07-31 15:08:53 +00002302 all_muted)) {
Fredrik Solenbergaf9fb212015-08-26 10:45:53 +02002303 LOG_RTCERR1(GetInputMute, ch.second->channel());
buildbot@webrtc.org6b21b712014-07-31 15:08:53 +00002304 return false;
2305 }
2306 }
2307
2308 webrtc::AudioProcessing* ap = engine()->voe()->base()->audio_processing();
solenberg0a617e22015-10-20 15:49:38 -07002309 if (ap) {
buildbot@webrtc.org6b21b712014-07-31 15:08:53 +00002310 ap->set_output_will_be_muted(all_muted);
solenberg0a617e22015-10-20 15:49:38 -07002311 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002312 return true;
2313}
2314
minyue@webrtc.org26236952014-10-29 02:27:08 +00002315// TODO(minyue): SetMaxSendBandwidth() is subject to be renamed to
2316// SetMaxSendBitrate() in future.
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00002317bool WebRtcVoiceMediaChannel::SetMaxSendBandwidth(int bps) {
minyue@webrtc.org26236952014-10-29 02:27:08 +00002318 LOG(LS_INFO) << "WebRtcVoiceMediaChannel::SetMaxSendBandwidth.";
minyue@webrtc.org26236952014-10-29 02:27:08 +00002319 return SetSendBitrateInternal(bps);
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00002320}
2321
minyue@webrtc.org26236952014-10-29 02:27:08 +00002322bool WebRtcVoiceMediaChannel::SetSendBitrateInternal(int bps) {
2323 LOG(LS_INFO) << "WebRtcVoiceMediaChannel::SetSendBitrateInternal.";
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00002324
minyue@webrtc.org26236952014-10-29 02:27:08 +00002325 send_bitrate_setting_ = true;
2326 send_bitrate_bps_ = bps;
wu@webrtc.org1d1ffc92013-10-16 18:12:02 +00002327
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002328 if (!send_codec_) {
wu@webrtc.org1d1ffc92013-10-16 18:12:02 +00002329 LOG(LS_INFO) << "The send codec has not been set up yet. "
minyue@webrtc.org26236952014-10-29 02:27:08 +00002330 << "The send bitrate setting will be applied later.";
wu@webrtc.org1d1ffc92013-10-16 18:12:02 +00002331 return true;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002332 }
2333
minyue@webrtc.org26236952014-10-29 02:27:08 +00002334 // Bitrate is auto by default.
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00002335 // TODO(bemasc): Fix this so that if SetMaxSendBandwidth(50) is followed by
2336 // SetMaxSendBandwith(0), the second call removes the previous limit.
2337 if (bps <= 0)
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002338 return true;
2339
2340 webrtc::CodecInst codec = *send_codec_;
solenberg26c8c912015-11-27 04:00:25 -08002341 bool is_multi_rate = WebRtcVoiceCodecs::IsCodecMultiRate(codec);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002342
2343 if (is_multi_rate) {
2344 // If codec is multi-rate then just set the bitrate.
2345 codec.rate = bps;
solenbergc96df772015-10-21 13:01:53 -07002346 for (const auto& ch : send_streams_) {
solenberg0a617e22015-10-20 15:49:38 -07002347 if (!SetSendCodec(ch.second->channel(), codec)) {
2348 LOG(LS_INFO) << "Failed to set codec " << codec.plname
2349 << " to bitrate " << bps << " bps.";
2350 return false;
2351 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002352 }
2353 return true;
2354 } else {
2355 // If codec is not multi-rate and |bps| is less than the fixed bitrate
2356 // then fail. If codec is not multi-rate and |bps| exceeds or equal the
2357 // fixed bitrate then ignore.
2358 if (bps < codec.rate) {
2359 LOG(LS_INFO) << "Failed to set codec " << codec.plname
2360 << " to bitrate " << bps << " bps"
2361 << ", requires at least " << codec.rate << " bps.";
2362 return false;
2363 }
2364 return true;
2365 }
2366}
2367
2368bool WebRtcVoiceMediaChannel::GetStats(VoiceMediaInfo* info) {
solenberg566ef242015-11-06 15:34:49 -08002369 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
solenberg85a04962015-10-27 03:35:21 -07002370 RTC_DCHECK(info);
solenbergd97ec302015-10-07 01:40:33 -07002371
solenberg85a04962015-10-27 03:35:21 -07002372 // Get SSRC and stats for each sender.
2373 RTC_DCHECK(info->senders.size() == 0);
2374 for (const auto& stream : send_streams_) {
2375 webrtc::AudioSendStream::Stats stats = stream.second->GetStats();
wu@webrtc.org9dba5252013-08-05 20:36:57 +00002376 VoiceSenderInfo sinfo;
solenberg85a04962015-10-27 03:35:21 -07002377 sinfo.add_ssrc(stats.local_ssrc);
2378 sinfo.bytes_sent = stats.bytes_sent;
2379 sinfo.packets_sent = stats.packets_sent;
2380 sinfo.packets_lost = stats.packets_lost;
2381 sinfo.fraction_lost = stats.fraction_lost;
2382 sinfo.codec_name = stats.codec_name;
2383 sinfo.ext_seqnum = stats.ext_seqnum;
2384 sinfo.jitter_ms = stats.jitter_ms;
2385 sinfo.rtt_ms = stats.rtt_ms;
2386 sinfo.audio_level = stats.audio_level;
2387 sinfo.aec_quality_min = stats.aec_quality_min;
2388 sinfo.echo_delay_median_ms = stats.echo_delay_median_ms;
2389 sinfo.echo_delay_std_ms = stats.echo_delay_std_ms;
2390 sinfo.echo_return_loss = stats.echo_return_loss;
2391 sinfo.echo_return_loss_enhancement = stats.echo_return_loss_enhancement;
solenberg566ef242015-11-06 15:34:49 -08002392 sinfo.typing_noise_detected =
2393 (send_ == SEND_NOTHING ? false : stats.typing_noise_detected);
wu@webrtc.org9dba5252013-08-05 20:36:57 +00002394 info->senders.push_back(sinfo);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002395 }
2396
solenberg85a04962015-10-27 03:35:21 -07002397 // Get SSRC and stats for each receiver.
2398 RTC_DCHECK(info->receivers.size() == 0);
solenberg7add0582015-11-20 09:59:34 -08002399 for (const auto& stream : recv_streams_) {
Fredrik Solenberg4f4ec0a2015-10-22 10:49:27 +02002400 webrtc::AudioReceiveStream::Stats stats = stream.second->GetStats();
2401 VoiceReceiverInfo rinfo;
2402 rinfo.add_ssrc(stats.remote_ssrc);
2403 rinfo.bytes_rcvd = stats.bytes_rcvd;
2404 rinfo.packets_rcvd = stats.packets_rcvd;
2405 rinfo.packets_lost = stats.packets_lost;
2406 rinfo.fraction_lost = stats.fraction_lost;
2407 rinfo.codec_name = stats.codec_name;
2408 rinfo.ext_seqnum = stats.ext_seqnum;
2409 rinfo.jitter_ms = stats.jitter_ms;
2410 rinfo.jitter_buffer_ms = stats.jitter_buffer_ms;
2411 rinfo.jitter_buffer_preferred_ms = stats.jitter_buffer_preferred_ms;
2412 rinfo.delay_estimate_ms = stats.delay_estimate_ms;
2413 rinfo.audio_level = stats.audio_level;
2414 rinfo.expand_rate = stats.expand_rate;
2415 rinfo.speech_expand_rate = stats.speech_expand_rate;
2416 rinfo.secondary_decoded_rate = stats.secondary_decoded_rate;
2417 rinfo.accelerate_rate = stats.accelerate_rate;
2418 rinfo.preemptive_expand_rate = stats.preemptive_expand_rate;
2419 rinfo.decoding_calls_to_silence_generator =
2420 stats.decoding_calls_to_silence_generator;
2421 rinfo.decoding_calls_to_neteq = stats.decoding_calls_to_neteq;
2422 rinfo.decoding_normal = stats.decoding_normal;
2423 rinfo.decoding_plc = stats.decoding_plc;
2424 rinfo.decoding_cng = stats.decoding_cng;
2425 rinfo.decoding_plc_cng = stats.decoding_plc_cng;
2426 rinfo.capture_start_ntp_time_ms = stats.capture_start_ntp_time_ms;
2427 info->receivers.push_back(rinfo);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002428 }
2429
2430 return true;
2431}
2432
Tommif888bb52015-12-12 01:37:01 +01002433void WebRtcVoiceMediaChannel::SetRawAudioSink(
2434 uint32_t ssrc,
deadbeef2d110be2016-01-13 12:00:26 -08002435 rtc::scoped_ptr<webrtc::AudioSinkInterface> sink) {
Tommif888bb52015-12-12 01:37:01 +01002436 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
deadbeef884f5852016-01-15 09:20:04 -08002437 LOG(LS_VERBOSE) << "WebRtcVoiceMediaChannel::SetRawAudioSink: ssrc:" << ssrc
2438 << " " << (sink ? "(ptr)" : "NULL");
2439 if (ssrc == 0) {
2440 if (default_recv_ssrc_ != -1) {
2441 rtc::scoped_ptr<webrtc::AudioSinkInterface> proxy_sink(
2442 sink ? new ProxySink(sink.get()) : nullptr);
2443 SetRawAudioSink(default_recv_ssrc_, std::move(proxy_sink));
2444 }
2445 default_sink_ = std::move(sink);
2446 return;
2447 }
Tommif888bb52015-12-12 01:37:01 +01002448 const auto it = recv_streams_.find(ssrc);
2449 if (it == recv_streams_.end()) {
2450 LOG(LS_WARNING) << "SetRawAudioSink: no recv stream" << ssrc;
2451 return;
2452 }
deadbeef2d110be2016-01-13 12:00:26 -08002453 it->second->SetRawAudioSink(std::move(sink));
Tommif888bb52015-12-12 01:37:01 +01002454}
2455
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002456int WebRtcVoiceMediaChannel::GetOutputLevel(int channel) {
solenbergd97ec302015-10-07 01:40:33 -07002457 unsigned int ulevel = 0;
2458 int ret = engine()->voe()->volume()->GetSpeechOutputLevel(channel, ulevel);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002459 return (ret == 0) ? static_cast<int>(ulevel) : -1;
2460}
2461
Peter Boström0c4e06b2015-10-07 12:23:21 +02002462int WebRtcVoiceMediaChannel::GetReceiveChannelId(uint32_t ssrc) const {
solenberg566ef242015-11-06 15:34:49 -08002463 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
solenberg7add0582015-11-20 09:59:34 -08002464 const auto it = recv_streams_.find(ssrc);
2465 if (it != recv_streams_.end()) {
mallinath@webrtc.org67ee6b92014-02-03 16:57:16 +00002466 return it->second->channel();
solenberg8fb30c32015-10-13 03:06:58 -07002467 }
solenberg1ac56142015-10-13 03:58:19 -07002468 return -1;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002469}
2470
Peter Boström0c4e06b2015-10-07 12:23:21 +02002471int WebRtcVoiceMediaChannel::GetSendChannelId(uint32_t ssrc) const {
solenberg566ef242015-11-06 15:34:49 -08002472 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
solenbergc96df772015-10-21 13:01:53 -07002473 const auto it = send_streams_.find(ssrc);
2474 if (it != send_streams_.end()) {
mallinath@webrtc.org67ee6b92014-02-03 16:57:16 +00002475 return it->second->channel();
solenberg8fb30c32015-10-13 03:06:58 -07002476 }
wu@webrtc.org9dba5252013-08-05 20:36:57 +00002477 return -1;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002478}
2479
2480bool WebRtcVoiceMediaChannel::GetRedSendCodec(const AudioCodec& red_codec,
2481 const std::vector<AudioCodec>& all_codecs, webrtc::CodecInst* send_codec) {
2482 // Get the RED encodings from the parameter with no name. This may
2483 // change based on what is discussed on the Jingle list.
2484 // The encoding parameter is of the form "a/b"; we only support where
2485 // a == b. Verify this and parse out the value into red_pt.
2486 // If the parameter value is absent (as it will be until we wire up the
2487 // signaling of this message), use the second codec specified (i.e. the
2488 // one after "red") as the encoding parameter.
2489 int red_pt = -1;
2490 std::string red_params;
2491 CodecParameterMap::const_iterator it = red_codec.params.find("");
2492 if (it != red_codec.params.end()) {
2493 red_params = it->second;
2494 std::vector<std::string> red_pts;
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00002495 if (rtc::split(red_params, '/', &red_pts) != 2 ||
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002496 red_pts[0] != red_pts[1] ||
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00002497 !rtc::FromString(red_pts[0], &red_pt)) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002498 LOG(LS_WARNING) << "RED params " << red_params << " not supported.";
2499 return false;
2500 }
2501 } else if (red_codec.params.empty()) {
2502 LOG(LS_WARNING) << "RED params not present, using defaults";
2503 if (all_codecs.size() > 1) {
2504 red_pt = all_codecs[1].id;
2505 }
2506 }
2507
2508 // Try to find red_pt in |codecs|.
Fredrik Solenbergaf9fb212015-08-26 10:45:53 +02002509 for (const AudioCodec& codec : all_codecs) {
2510 if (codec.id == red_pt) {
2511 // If we find the right codec, that will be the codec we pass to
2512 // SetSendCodec, with the desired payload type.
solenberg26c8c912015-11-27 04:00:25 -08002513 if (WebRtcVoiceEngine::ToCodecInst(codec, send_codec)) {
Fredrik Solenbergaf9fb212015-08-26 10:45:53 +02002514 return true;
2515 } else {
2516 break;
2517 }
2518 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002519 }
Fredrik Solenbergaf9fb212015-08-26 10:45:53 +02002520 LOG(LS_WARNING) << "RED params " << red_params << " are invalid.";
2521 return false;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002522}
2523
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002524bool WebRtcVoiceMediaChannel::SetPlayout(int channel, bool playout) {
2525 if (playout) {
2526 LOG(LS_INFO) << "Starting playout for channel #" << channel;
2527 if (engine()->voe()->base()->StartPlayout(channel) == -1) {
2528 LOG_RTCERR1(StartPlayout, channel);
2529 return false;
2530 }
2531 } else {
2532 LOG(LS_INFO) << "Stopping playout for channel #" << channel;
2533 engine()->voe()->base()->StopPlayout(channel);
2534 }
2535 return true;
2536}
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002537} // namespace cricket
2538
2539#endif // HAVE_WEBRTC_VOICE