blob: 38b6c54b73c050fcc6382e1544c81004f9c19b00 [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
solenberg0b675462015-10-09 01:37:09 -0700135bool ValidateStreamParams(const StreamParams& sp) {
136 if (sp.ssrcs.empty()) {
137 LOG(LS_ERROR) << "No SSRCs in stream parameters: " << sp.ToString();
138 return false;
139 }
140 if (sp.ssrcs.size() > 1) {
141 LOG(LS_ERROR) << "Multiple SSRCs in stream parameters: " << sp.ToString();
142 return false;
143 }
144 return true;
145}
146
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000147// Dumps an AudioCodec in RFC 2327-ish format.
solenbergd97ec302015-10-07 01:40:33 -0700148std::string ToString(const AudioCodec& codec) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000149 std::stringstream ss;
150 ss << codec.name << "/" << codec.clockrate << "/" << codec.channels
151 << " (" << codec.id << ")";
152 return ss.str();
153}
Minyue Li7100dcd2015-03-27 05:05:59 +0100154
solenbergd97ec302015-10-07 01:40:33 -0700155std::string ToString(const webrtc::CodecInst& codec) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000156 std::stringstream ss;
157 ss << codec.plname << "/" << codec.plfreq << "/" << codec.channels
158 << " (" << codec.pltype << ")";
159 return ss.str();
160}
161
solenbergd97ec302015-10-07 01:40:33 -0700162bool IsCodec(const AudioCodec& codec, const char* ref_name) {
Minyue Li7100dcd2015-03-27 05:05:59 +0100163 return (_stricmp(codec.name.c_str(), ref_name) == 0);
164}
165
solenbergd97ec302015-10-07 01:40:33 -0700166bool IsCodec(const webrtc::CodecInst& codec, const char* ref_name) {
Minyue Li7100dcd2015-03-27 05:05:59 +0100167 return (_stricmp(codec.plname, ref_name) == 0);
168}
169
solenbergd97ec302015-10-07 01:40:33 -0700170bool FindCodec(const std::vector<AudioCodec>& codecs,
solenberg26c8c912015-11-27 04:00:25 -0800171 const AudioCodec& codec,
172 AudioCodec* found_codec) {
Fredrik Solenbergaf9fb212015-08-26 10:45:53 +0200173 for (const AudioCodec& c : codecs) {
174 if (c.Matches(codec)) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000175 if (found_codec != NULL) {
Fredrik Solenbergaf9fb212015-08-26 10:45:53 +0200176 *found_codec = c;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000177 }
178 return true;
179 }
180 }
181 return false;
182}
wu@webrtc.org1d1ffc92013-10-16 18:12:02 +0000183
solenberg0b675462015-10-09 01:37:09 -0700184bool VerifyUniquePayloadTypes(const std::vector<AudioCodec>& codecs) {
185 if (codecs.empty()) {
186 return true;
187 }
188 std::vector<int> payload_types;
189 for (const AudioCodec& codec : codecs) {
190 payload_types.push_back(codec.id);
191 }
192 std::sort(payload_types.begin(), payload_types.end());
193 auto it = std::unique(payload_types.begin(), payload_types.end());
194 return it == payload_types.end();
195}
196
solenbergd97ec302015-10-07 01:40:33 -0700197bool IsNackEnabled(const AudioCodec& codec) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000198 return codec.HasFeedbackParam(FeedbackParam(kRtcpFbParamNack,
199 kParamValueEmpty));
200}
201
Minyue Li7100dcd2015-03-27 05:05:59 +0100202// Return true if codec.params[feature] == "1", false otherwise.
solenberg26c8c912015-11-27 04:00:25 -0800203bool IsCodecFeatureEnabled(const AudioCodec& codec, const char* feature) {
Minyue Li7100dcd2015-03-27 05:05:59 +0100204 int value;
205 return codec.GetParam(feature, &value) && value == 1;
206}
207
208// Use params[kCodecParamMaxAverageBitrate] if it is defined, use codec.bitrate
209// otherwise. If the value (either from params or codec.bitrate) <=0, use the
210// default configuration. If the value is beyond feasible bit rate of Opus,
211// clamp it. Returns the Opus bit rate for operation.
solenbergd97ec302015-10-07 01:40:33 -0700212int GetOpusBitrate(const AudioCodec& codec, int max_playback_rate) {
Minyue Li7100dcd2015-03-27 05:05:59 +0100213 int bitrate = 0;
214 bool use_param = true;
215 if (!codec.GetParam(kCodecParamMaxAverageBitrate, &bitrate)) {
216 bitrate = codec.bitrate;
217 use_param = false;
218 }
219 if (bitrate <= 0) {
220 if (max_playback_rate <= 8000) {
221 bitrate = kOpusBitrateNb;
222 } else if (max_playback_rate <= 16000) {
223 bitrate = kOpusBitrateWb;
224 } else {
225 bitrate = kOpusBitrateFb;
226 }
227
228 if (IsCodecFeatureEnabled(codec, kCodecParamStereo)) {
229 bitrate *= 2;
230 }
231 } else if (bitrate < kOpusMinBitrate || bitrate > kOpusMaxBitrate) {
232 bitrate = (bitrate < kOpusMinBitrate) ? kOpusMinBitrate : kOpusMaxBitrate;
233 std::string rate_source =
234 use_param ? "Codec parameter \"maxaveragebitrate\"" :
235 "Supplied Opus bitrate";
236 LOG(LS_WARNING) << rate_source
237 << " is invalid and is replaced by: "
238 << bitrate;
239 }
240 return bitrate;
241}
242
243// Returns kOpusDefaultPlaybackRate if params[kCodecParamMaxPlaybackRate] is not
244// defined. Returns the value of params[kCodecParamMaxPlaybackRate] otherwise.
solenbergd97ec302015-10-07 01:40:33 -0700245int GetOpusMaxPlaybackRate(const AudioCodec& codec) {
Minyue Li7100dcd2015-03-27 05:05:59 +0100246 int value;
247 if (codec.GetParam(kCodecParamMaxPlaybackRate, &value)) {
248 return value;
249 }
250 return kOpusDefaultMaxPlaybackRate;
251}
252
solenbergd97ec302015-10-07 01:40:33 -0700253void GetOpusConfig(const AudioCodec& codec, webrtc::CodecInst* voe_codec,
Minyue Li7100dcd2015-03-27 05:05:59 +0100254 bool* enable_codec_fec, int* max_playback_rate,
255 bool* enable_codec_dtx) {
256 *enable_codec_fec = IsCodecFeatureEnabled(codec, kCodecParamUseInbandFec);
257 *enable_codec_dtx = IsCodecFeatureEnabled(codec, kCodecParamUseDtx);
258 *max_playback_rate = GetOpusMaxPlaybackRate(codec);
259
260 // If OPUS, change what we send according to the "stereo" codec
261 // parameter, and not the "channels" parameter. We set
262 // voe_codec.channels to 2 if "stereo=1" and 1 otherwise. If
263 // the bitrate is not specified, i.e. is <= zero, we set it to the
264 // appropriate default value for mono or stereo Opus.
265
266 voe_codec->channels = IsCodecFeatureEnabled(codec, kCodecParamStereo) ? 2 : 1;
267 voe_codec->rate = GetOpusBitrate(codec, *max_playback_rate);
268}
269
solenberg566ef242015-11-06 15:34:49 -0800270webrtc::AudioState::Config MakeAudioStateConfig(VoEWrapper* voe_wrapper) {
271 webrtc::AudioState::Config config;
272 config.voice_engine = voe_wrapper->engine();
273 return config;
274}
275
solenberg26c8c912015-11-27 04:00:25 -0800276class WebRtcVoiceCodecs final {
277 public:
278 // TODO(solenberg): Do this filtering once off-line, add a simple AudioCodec
279 // list and add a test which verifies VoE supports the listed codecs.
280 static std::vector<AudioCodec> SupportedCodecs() {
281 LOG(LS_INFO) << "WebRtc VoiceEngine codecs:";
282 std::vector<AudioCodec> result;
283 for (webrtc::CodecInst voe_codec : webrtc::acm2::RentACodec::Database()) {
284 // Change the sample rate of G722 to 8000 to match SDP.
285 MaybeFixupG722(&voe_codec, 8000);
buildbot@webrtc.org13d67762014-05-02 17:33:29 +0000286 // Skip uncompressed formats.
Minyue Li7100dcd2015-03-27 05:05:59 +0100287 if (IsCodec(voe_codec, kL16CodecName)) {
buildbot@webrtc.org13d67762014-05-02 17:33:29 +0000288 continue;
289 }
290
291 const CodecPref* pref = NULL;
tfarina5237aaf2015-11-10 23:44:30 -0800292 for (size_t j = 0; j < arraysize(kCodecPrefs); ++j) {
Minyue Li7100dcd2015-03-27 05:05:59 +0100293 if (IsCodec(voe_codec, kCodecPrefs[j].name) &&
buildbot@webrtc.org13d67762014-05-02 17:33:29 +0000294 kCodecPrefs[j].clockrate == voe_codec.plfreq &&
295 kCodecPrefs[j].channels == voe_codec.channels) {
296 pref = &kCodecPrefs[j];
297 break;
298 }
299 }
300
301 if (pref) {
302 // Use the payload type that we've configured in our pref table;
303 // use the offset in our pref table to determine the sort order.
tfarina5237aaf2015-11-10 23:44:30 -0800304 AudioCodec codec(
305 pref->payload_type, voe_codec.plname, voe_codec.plfreq,
306 voe_codec.rate, voe_codec.channels,
307 static_cast<int>(arraysize(kCodecPrefs)) - (pref - kCodecPrefs));
buildbot@webrtc.org13d67762014-05-02 17:33:29 +0000308 LOG(LS_INFO) << ToString(codec);
Minyue Li7100dcd2015-03-27 05:05:59 +0100309 if (IsCodec(codec, kIsacCodecName)) {
minyue@webrtc.org26236952014-10-29 02:27:08 +0000310 // Indicate auto-bitrate in signaling.
buildbot@webrtc.org13d67762014-05-02 17:33:29 +0000311 codec.bitrate = 0;
312 }
Minyue Li7100dcd2015-03-27 05:05:59 +0100313 if (IsCodec(codec, kOpusCodecName)) {
buildbot@webrtc.org13d67762014-05-02 17:33:29 +0000314 // Only add fmtp parameters that differ from the spec.
315 if (kPreferredMinPTime != kOpusDefaultMinPTime) {
316 codec.params[kCodecParamMinPTime] =
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000317 rtc::ToString(kPreferredMinPTime);
buildbot@webrtc.org13d67762014-05-02 17:33:29 +0000318 }
319 if (kPreferredMaxPTime != kOpusDefaultMaxPTime) {
320 codec.params[kCodecParamMaxPTime] =
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000321 rtc::ToString(kPreferredMaxPTime);
buildbot@webrtc.org13d67762014-05-02 17:33:29 +0000322 }
pkasting@chromium.orgd3245462015-02-23 21:28:22 +0000323 codec.SetParam(kCodecParamUseInbandFec, 1);
minyue@webrtc.org4ef22d12014-11-17 09:26:39 +0000324
325 // TODO(hellner): Add ptime, sprop-stereo, and stereo
buildbot@webrtc.org13d67762014-05-02 17:33:29 +0000326 // when they can be set to values other than the default.
327 }
solenberg26c8c912015-11-27 04:00:25 -0800328 result.push_back(codec);
buildbot@webrtc.org13d67762014-05-02 17:33:29 +0000329 } else {
330 LOG(LS_WARNING) << "Unexpected codec: " << ToString(voe_codec);
331 }
332 }
solenberg26c8c912015-11-27 04:00:25 -0800333 // Make sure they are in local preference order.
334 std::sort(result.begin(), result.end(), &AudioCodec::Preferable);
335 return result;
buildbot@webrtc.org13d67762014-05-02 17:33:29 +0000336 }
buildbot@webrtc.org13d67762014-05-02 17:33:29 +0000337
solenberg26c8c912015-11-27 04:00:25 -0800338 static bool ToCodecInst(const AudioCodec& in,
339 webrtc::CodecInst* out) {
340 for (webrtc::CodecInst voe_codec : webrtc::acm2::RentACodec::Database()) {
341 // Change the sample rate of G722 to 8000 to match SDP.
342 MaybeFixupG722(&voe_codec, 8000);
343 AudioCodec codec(voe_codec.pltype, voe_codec.plname, voe_codec.plfreq,
344 voe_codec.rate, voe_codec.channels, 0);
345 bool multi_rate = IsCodecMultiRate(voe_codec);
346 // Allow arbitrary rates for ISAC to be specified.
347 if (multi_rate) {
348 // Set codec.bitrate to 0 so the check for codec.Matches() passes.
349 codec.bitrate = 0;
350 }
351 if (codec.Matches(in)) {
352 if (out) {
353 // Fixup the payload type.
354 voe_codec.pltype = in.id;
355
356 // Set bitrate if specified.
357 if (multi_rate && in.bitrate != 0) {
358 voe_codec.rate = in.bitrate;
359 }
360
361 // Reset G722 sample rate to 16000 to match WebRTC.
362 MaybeFixupG722(&voe_codec, 16000);
363
364 // Apply codec-specific settings.
365 if (IsCodec(codec, kIsacCodecName)) {
366 // If ISAC and an explicit bitrate is not specified,
367 // enable auto bitrate adjustment.
368 voe_codec.rate = (in.bitrate > 0) ? in.bitrate : -1;
369 }
370 *out = voe_codec;
371 }
372 return true;
373 }
374 }
henrik.lundin@webrtc.org8038d422014-11-11 08:38:24 +0000375 return false;
henrik.lundin@webrtc.orgf85dbce2014-11-07 12:25:00 +0000376 }
solenberg26c8c912015-11-27 04:00:25 -0800377
378 static bool IsCodecMultiRate(const webrtc::CodecInst& codec) {
379 for (size_t i = 0; i < arraysize(kCodecPrefs); ++i) {
380 if (IsCodec(codec, kCodecPrefs[i].name) &&
381 kCodecPrefs[i].clockrate == codec.plfreq) {
382 return kCodecPrefs[i].is_multi_rate;
383 }
384 }
385 return false;
386 }
387
388 // If the AudioCodec param kCodecParamPTime is set, then we will set it to
389 // codec pacsize if it's valid, or we will pick the next smallest value we
390 // support.
391 // TODO(Brave): Query supported packet sizes from ACM when the API is ready.
392 static bool SetPTimeAsPacketSize(webrtc::CodecInst* codec, int ptime_ms) {
393 for (const CodecPref& codec_pref : kCodecPrefs) {
394 if ((IsCodec(*codec, codec_pref.name) &&
395 codec_pref.clockrate == codec->plfreq) ||
396 IsCodec(*codec, kG722CodecName)) {
397 int packet_size_ms = SelectPacketSize(codec_pref, ptime_ms);
398 if (packet_size_ms) {
399 // Convert unit from milli-seconds to samples.
400 codec->pacsize = (codec->plfreq / 1000) * packet_size_ms;
401 return true;
402 }
403 }
404 }
405 return false;
406 }
407
408 private:
409 static const int kMaxNumPacketSize = 6;
410 struct CodecPref {
411 const char* name;
412 int clockrate;
413 int channels;
414 int payload_type;
415 bool is_multi_rate;
416 int packet_sizes_ms[kMaxNumPacketSize];
417 };
418 // Note: keep the supported packet sizes in ascending order.
419 static const CodecPref kCodecPrefs[12];
420
421 static int SelectPacketSize(const CodecPref& codec_pref, int ptime_ms) {
422 int selected_packet_size_ms = codec_pref.packet_sizes_ms[0];
423 for (int packet_size_ms : codec_pref.packet_sizes_ms) {
424 if (packet_size_ms && packet_size_ms <= ptime_ms) {
425 selected_packet_size_ms = packet_size_ms;
426 }
427 }
428 return selected_packet_size_ms;
429 }
430
431 // Changes RTP timestamp rate of G722. This is due to the "bug" in the RFC
432 // which says that G722 should be advertised as 8 kHz although it is a 16 kHz
433 // codec.
434 static void MaybeFixupG722(webrtc::CodecInst* voe_codec, int new_plfreq) {
435 if (IsCodec(*voe_codec, kG722CodecName)) {
436 // If the ASSERT triggers, the codec definition in WebRTC VoiceEngine
437 // has changed, and this special case is no longer needed.
438 RTC_DCHECK(voe_codec->plfreq != new_plfreq);
439 voe_codec->plfreq = new_plfreq;
440 }
441 }
442};
443
444const WebRtcVoiceCodecs::CodecPref WebRtcVoiceCodecs::kCodecPrefs[12] = {
445 { kOpusCodecName, 48000, 2, 111, true, { 10, 20, 40, 60 } },
446 { kIsacCodecName, 16000, 1, 103, true, { 30, 60 } },
447 { kIsacCodecName, 32000, 1, 104, true, { 30 } },
448 // G722 should be advertised as 8000 Hz because of the RFC "bug".
449 { kG722CodecName, 8000, 1, 9, false, { 10, 20, 30, 40, 50, 60 } },
450 { kIlbcCodecName, 8000, 1, 102, false, { 20, 30, 40, 60 } },
451 { kPcmuCodecName, 8000, 1, 0, false, { 10, 20, 30, 40, 50, 60 } },
452 { kPcmaCodecName, 8000, 1, 8, false, { 10, 20, 30, 40, 50, 60 } },
453 { kCnCodecName, 32000, 1, 106, false, { } },
454 { kCnCodecName, 16000, 1, 105, false, { } },
455 { kCnCodecName, 8000, 1, 13, false, { } },
456 { kRedCodecName, 8000, 1, 127, false, { } },
457 { kDtmfCodecName, 8000, 1, 126, false, { } },
458};
459} // namespace {
460
461bool WebRtcVoiceEngine::ToCodecInst(const AudioCodec& in,
462 webrtc::CodecInst* out) {
463 return WebRtcVoiceCodecs::ToCodecInst(in, out);
464}
465
466WebRtcVoiceEngine::WebRtcVoiceEngine()
467 : voe_wrapper_(new VoEWrapper()),
468 audio_state_(webrtc::AudioState::Create(MakeAudioStateConfig(voe()))) {
469 Construct();
470}
471
472WebRtcVoiceEngine::WebRtcVoiceEngine(VoEWrapper* voe_wrapper)
473 : voe_wrapper_(voe_wrapper) {
474 Construct();
475}
476
477void WebRtcVoiceEngine::Construct() {
478 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
479 LOG(LS_VERBOSE) << "WebRtcVoiceEngine::WebRtcVoiceEngine";
480
481 signal_thread_checker_.DetachFromThread();
482 std::memset(&default_agc_config_, 0, sizeof(default_agc_config_));
solenberg246b8172015-12-08 09:50:23 -0800483 voe_config_.Set<webrtc::VoicePacing>(new webrtc::VoicePacing(true));
solenberg26c8c912015-11-27 04:00:25 -0800484
485 webrtc::Trace::set_level_filter(kDefaultTraceFilter);
486 webrtc::Trace::SetTraceCallback(this);
487
488 // Load our audio codec list.
489 codecs_ = WebRtcVoiceCodecs::SupportedCodecs();
henrik.lundin@webrtc.orgf85dbce2014-11-07 12:25:00 +0000490}
491
buildbot@webrtc.org13d67762014-05-02 17:33:29 +0000492WebRtcVoiceEngine::~WebRtcVoiceEngine() {
solenberg566ef242015-11-06 15:34:49 -0800493 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
buildbot@webrtc.org13d67762014-05-02 17:33:29 +0000494 LOG(LS_VERBOSE) << "WebRtcVoiceEngine::~WebRtcVoiceEngine";
buildbot@webrtc.org13d67762014-05-02 17:33:29 +0000495 if (adm_) {
496 voe_wrapper_.reset();
497 adm_->Release();
498 adm_ = NULL;
499 }
solenbergbd138382015-11-20 16:08:07 -0800500 webrtc::Trace::SetTraceCallback(nullptr);
buildbot@webrtc.org13d67762014-05-02 17:33:29 +0000501}
502
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000503bool WebRtcVoiceEngine::Init(rtc::Thread* worker_thread) {
solenberg566ef242015-11-06 15:34:49 -0800504 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
henrikg91d6ede2015-09-17 00:24:34 -0700505 RTC_DCHECK(worker_thread == rtc::Thread::Current());
buildbot@webrtc.org13d67762014-05-02 17:33:29 +0000506 LOG(LS_INFO) << "WebRtcVoiceEngine::Init";
507 bool res = InitInternal();
508 if (res) {
509 LOG(LS_INFO) << "WebRtcVoiceEngine::Init Done!";
510 } else {
511 LOG(LS_ERROR) << "WebRtcVoiceEngine::Init failed";
512 Terminate();
513 }
514 return res;
515}
516
517bool WebRtcVoiceEngine::InitInternal() {
solenberg566ef242015-11-06 15:34:49 -0800518 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
buildbot@webrtc.org13d67762014-05-02 17:33:29 +0000519 // Temporarily turn logging level up for the Init call
solenbergbd138382015-11-20 16:08:07 -0800520 webrtc::Trace::set_level_filter(kElevatedTraceFilter);
solenberg2515af22015-12-02 06:19:36 -0800521 LOG(LS_INFO) << webrtc::VoiceEngine::GetVersionString();
buildbot@webrtc.org13d67762014-05-02 17:33:29 +0000522 if (voe_wrapper_->base()->Init(adm_) == -1) {
523 LOG_RTCERR0_EX(Init, voe_wrapper_->error());
buildbot@webrtc.org13d67762014-05-02 17:33:29 +0000524 return false;
525 }
solenbergbd138382015-11-20 16:08:07 -0800526 webrtc::Trace::set_level_filter(kDefaultTraceFilter);
buildbot@webrtc.org13d67762014-05-02 17:33:29 +0000527
buildbot@webrtc.org13d67762014-05-02 17:33:29 +0000528 // Save the default AGC configuration settings. This must happen before
solenberg246b8172015-12-08 09:50:23 -0800529 // calling ApplyOptions or the default will be overwritten.
buildbot@webrtc.org13d67762014-05-02 17:33:29 +0000530 if (voe_wrapper_->processing()->GetAgcConfig(default_agc_config_) == -1) {
531 LOG_RTCERR0(GetAgcConfig);
532 return false;
533 }
534
buildbot@webrtc.org13d67762014-05-02 17:33:29 +0000535 // Print our codec list again for the call diagnostic log
536 LOG(LS_INFO) << "WebRtc VoiceEngine codecs:";
Fredrik Solenbergaf9fb212015-08-26 10:45:53 +0200537 for (const AudioCodec& codec : codecs_) {
538 LOG(LS_INFO) << ToString(codec);
buildbot@webrtc.org13d67762014-05-02 17:33:29 +0000539 }
540
solenberg246b8172015-12-08 09:50:23 -0800541 SetDefaultDevices();
542
buildbot@webrtc.org13d67762014-05-02 17:33:29 +0000543 initialized_ = true;
544 return true;
545}
546
buildbot@webrtc.org13d67762014-05-02 17:33:29 +0000547void WebRtcVoiceEngine::Terminate() {
solenberg566ef242015-11-06 15:34:49 -0800548 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
buildbot@webrtc.org13d67762014-05-02 17:33:29 +0000549 LOG(LS_INFO) << "WebRtcVoiceEngine::Terminate";
550 initialized_ = false;
551
552 StopAecDump();
553
buildbot@webrtc.org13d67762014-05-02 17:33:29 +0000554 voe_wrapper_->base()->Terminate();
buildbot@webrtc.org13d67762014-05-02 17:33:29 +0000555}
556
solenberg566ef242015-11-06 15:34:49 -0800557rtc::scoped_refptr<webrtc::AudioState>
558 WebRtcVoiceEngine::GetAudioState() const {
559 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
560 return audio_state_;
561}
562
Fredrik Solenberg709ed672015-09-15 12:26:33 +0200563VoiceMediaChannel* WebRtcVoiceEngine::CreateChannel(webrtc::Call* call,
Jelena Marusicc28a8962015-05-29 15:05:44 +0200564 const AudioOptions& options) {
solenberg566ef242015-11-06 15:34:49 -0800565 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
solenberg0a617e22015-10-20 15:49:38 -0700566 return new WebRtcVoiceMediaChannel(this, options, call);
buildbot@webrtc.org13d67762014-05-02 17:33:29 +0000567}
568
buildbot@webrtc.org13d67762014-05-02 17:33:29 +0000569bool WebRtcVoiceEngine::ApplyOptions(const AudioOptions& options_in) {
solenberg566ef242015-11-06 15:34:49 -0800570 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
henrikac14f5ff2015-09-23 14:08:33 +0200571 LOG(LS_INFO) << "ApplyOptions: " << options_in.ToString();
solenberg246b8172015-12-08 09:50:23 -0800572
573 // Default engine options.
574 AudioOptions options;
575 options.echo_cancellation = rtc::Optional<bool>(true);
576 options.auto_gain_control = rtc::Optional<bool>(true);
577 options.noise_suppression = rtc::Optional<bool>(true);
578 options.highpass_filter = rtc::Optional<bool>(true);
579 options.stereo_swapping = rtc::Optional<bool>(false);
580 options.audio_jitter_buffer_max_packets = rtc::Optional<int>(50);
581 options.audio_jitter_buffer_fast_accelerate = rtc::Optional<bool>(false);
582 options.typing_detection = rtc::Optional<bool>(true);
583 options.adjust_agc_delta = rtc::Optional<int>(0);
584 options.experimental_agc = rtc::Optional<bool>(false);
585 options.extended_filter_aec = rtc::Optional<bool>(false);
586 options.delay_agnostic_aec = rtc::Optional<bool>(false);
587 options.experimental_ns = rtc::Optional<bool>(false);
588 options.aec_dump = rtc::Optional<bool>(false);
589
590 // Apply any given options on top.
591 options.SetAll(options_in);
592
buildbot@webrtc.org13d67762014-05-02 17:33:29 +0000593 // kEcConference is AEC with high suppression.
594 webrtc::EcModes ec_mode = webrtc::kEcConference;
595 webrtc::AecmModes aecm_mode = webrtc::kAecmSpeakerphone;
596 webrtc::AgcModes agc_mode = webrtc::kAgcAdaptiveAnalog;
597 webrtc::NsModes ns_mode = webrtc::kNsHighSuppression;
kwiberg102c6a62015-10-30 02:47:38 -0700598 if (options.aecm_generate_comfort_noise) {
buildbot@webrtc.org13d67762014-05-02 17:33:29 +0000599 LOG(LS_VERBOSE) << "Comfort noise explicitly set to "
kwiberg102c6a62015-10-30 02:47:38 -0700600 << *options.aecm_generate_comfort_noise
601 << " (default is false).";
buildbot@webrtc.org13d67762014-05-02 17:33:29 +0000602 }
603
604#if defined(IOS)
605 // On iOS, VPIO provides built-in EC and AGC.
Karl Wibergbe579832015-11-10 22:34:18 +0100606 options.echo_cancellation = rtc::Optional<bool>(false);
607 options.auto_gain_control = rtc::Optional<bool>(false);
henrika86d907c2015-09-07 16:09:50 +0200608 LOG(LS_INFO) << "Always disable AEC and AGC on iOS. Use built-in instead.";
buildbot@webrtc.org13d67762014-05-02 17:33:29 +0000609#elif defined(ANDROID)
610 ec_mode = webrtc::kEcAecm;
611#endif
612
613#if defined(IOS) || defined(ANDROID)
614 // Set the AGC mode for iOS as well despite disabling it above, to avoid
615 // unsupported configuration errors from webrtc.
616 agc_mode = webrtc::kAgcFixedDigital;
Karl Wibergbe579832015-11-10 22:34:18 +0100617 options.typing_detection = rtc::Optional<bool>(false);
618 options.experimental_agc = rtc::Optional<bool>(false);
619 options.extended_filter_aec = rtc::Optional<bool>(false);
620 options.experimental_ns = rtc::Optional<bool>(false);
buildbot@webrtc.org13d67762014-05-02 17:33:29 +0000621#endif
622
Bjorn Volckerbf395c12015-03-25 22:45:56 +0100623 // Delay Agnostic AEC automatically turns on EC if not set except on iOS
624 // where the feature is not supported.
625 bool use_delay_agnostic_aec = false;
626#if !defined(IOS)
kwiberg102c6a62015-10-30 02:47:38 -0700627 if (options.delay_agnostic_aec) {
628 use_delay_agnostic_aec = *options.delay_agnostic_aec;
Bjorn Volckerbf395c12015-03-25 22:45:56 +0100629 if (use_delay_agnostic_aec) {
Karl Wibergbe579832015-11-10 22:34:18 +0100630 options.echo_cancellation = rtc::Optional<bool>(true);
631 options.extended_filter_aec = rtc::Optional<bool>(true);
Bjorn Volckerbf395c12015-03-25 22:45:56 +0100632 ec_mode = webrtc::kEcConference;
633 }
634 }
635#endif
636
buildbot@webrtc.org13d67762014-05-02 17:33:29 +0000637 webrtc::VoEAudioProcessing* voep = voe_wrapper_->processing();
638
kwiberg102c6a62015-10-30 02:47:38 -0700639 if (options.echo_cancellation) {
henrika@webrtc.orga954c072014-12-09 16:22:09 +0000640 // Check if platform supports built-in EC. Currently only supported on
641 // Android and in combination with Java based audio layer.
642 // TODO(henrika): investigate possibility to support built-in EC also
643 // in combination with Open SL ES audio.
644 const bool built_in_aec = voe_wrapper_->hw()->BuiltInAECIsAvailable();
Bjorn Volcker73f72102015-06-03 14:50:15 +0200645 if (built_in_aec) {
Bjorn Volckerccfc9392015-05-07 07:43:17 +0200646 // Built-in EC exists on this device and use_delay_agnostic_aec is not
647 // overriding it. Enable/Disable it according to the echo_cancellation
648 // audio option.
Bjorn Volcker73f72102015-06-03 14:50:15 +0200649 const bool enable_built_in_aec =
kwiberg102c6a62015-10-30 02:47:38 -0700650 *options.echo_cancellation && !use_delay_agnostic_aec;
Bjorn Volcker73f72102015-06-03 14:50:15 +0200651 if (voe_wrapper_->hw()->EnableBuiltInAEC(enable_built_in_aec) == 0 &&
652 enable_built_in_aec) {
Bjorn Volckerbf395c12015-03-25 22:45:56 +0100653 // Disable internal software EC if built-in EC is enabled,
henrika@webrtc.orga954c072014-12-09 16:22:09 +0000654 // i.e., replace the software EC with the built-in EC.
Karl Wibergbe579832015-11-10 22:34:18 +0100655 options.echo_cancellation = rtc::Optional<bool>(false);
henrika@webrtc.orga954c072014-12-09 16:22:09 +0000656 LOG(LS_INFO) << "Disabling EC since built-in EC will be used instead";
657 }
658 }
kwiberg102c6a62015-10-30 02:47:38 -0700659 if (voep->SetEcStatus(*options.echo_cancellation, ec_mode) == -1) {
660 LOG_RTCERR2(SetEcStatus, *options.echo_cancellation, ec_mode);
buildbot@webrtc.org13d67762014-05-02 17:33:29 +0000661 return false;
662 } else {
kwiberg102c6a62015-10-30 02:47:38 -0700663 LOG(LS_INFO) << "Echo control set to " << *options.echo_cancellation
henrika86d907c2015-09-07 16:09:50 +0200664 << " with mode " << ec_mode;
buildbot@webrtc.org13d67762014-05-02 17:33:29 +0000665 }
666#if !defined(ANDROID)
667 // TODO(ajm): Remove the error return on Android from webrtc.
kwiberg102c6a62015-10-30 02:47:38 -0700668 if (voep->SetEcMetricsStatus(*options.echo_cancellation) == -1) {
669 LOG_RTCERR1(SetEcMetricsStatus, *options.echo_cancellation);
buildbot@webrtc.org13d67762014-05-02 17:33:29 +0000670 return false;
671 }
672#endif
673 if (ec_mode == webrtc::kEcAecm) {
kwiberg102c6a62015-10-30 02:47:38 -0700674 bool cn = options.aecm_generate_comfort_noise.value_or(false);
675 if (voep->SetAecmMode(aecm_mode, cn) != 0) {
676 LOG_RTCERR2(SetAecmMode, aecm_mode, cn);
buildbot@webrtc.org13d67762014-05-02 17:33:29 +0000677 return false;
678 }
679 }
680 }
681
kwiberg102c6a62015-10-30 02:47:38 -0700682 if (options.auto_gain_control) {
henrikac14f5ff2015-09-23 14:08:33 +0200683 const bool built_in_agc = voe_wrapper_->hw()->BuiltInAGCIsAvailable();
684 if (built_in_agc) {
kwiberg102c6a62015-10-30 02:47:38 -0700685 if (voe_wrapper_->hw()->EnableBuiltInAGC(*options.auto_gain_control) ==
686 0 &&
687 *options.auto_gain_control) {
henrikac14f5ff2015-09-23 14:08:33 +0200688 // Disable internal software AGC if built-in AGC is enabled,
689 // i.e., replace the software AGC with the built-in AGC.
Karl Wibergbe579832015-11-10 22:34:18 +0100690 options.auto_gain_control = rtc::Optional<bool>(false);
henrikac14f5ff2015-09-23 14:08:33 +0200691 LOG(LS_INFO) << "Disabling AGC since built-in AGC will be used instead";
692 }
693 }
kwiberg102c6a62015-10-30 02:47:38 -0700694 if (voep->SetAgcStatus(*options.auto_gain_control, agc_mode) == -1) {
695 LOG_RTCERR2(SetAgcStatus, *options.auto_gain_control, agc_mode);
buildbot@webrtc.org13d67762014-05-02 17:33:29 +0000696 return false;
697 } else {
kwiberg102c6a62015-10-30 02:47:38 -0700698 LOG(LS_INFO) << "Auto gain set to " << *options.auto_gain_control
699 << " with mode " << agc_mode;
buildbot@webrtc.org13d67762014-05-02 17:33:29 +0000700 }
701 }
702
kwiberg102c6a62015-10-30 02:47:38 -0700703 if (options.tx_agc_target_dbov || options.tx_agc_digital_compression_gain ||
704 options.tx_agc_limiter) {
buildbot@webrtc.org13d67762014-05-02 17:33:29 +0000705 // Override default_agc_config_. Generally, an unset option means "leave
706 // the VoE bits alone" in this function, so we want whatever is set to be
707 // stored as the new "default". If we didn't, then setting e.g.
708 // tx_agc_target_dbov would reset digital compression gain and limiter
709 // settings.
710 // Also, if we don't update default_agc_config_, then adjust_agc_delta
711 // would be an offset from the original values, and not whatever was set
712 // explicitly.
kwiberg102c6a62015-10-30 02:47:38 -0700713 default_agc_config_.targetLeveldBOv = options.tx_agc_target_dbov.value_or(
714 default_agc_config_.targetLeveldBOv);
buildbot@webrtc.org13d67762014-05-02 17:33:29 +0000715 default_agc_config_.digitalCompressionGaindB =
kwiberg102c6a62015-10-30 02:47:38 -0700716 options.tx_agc_digital_compression_gain.value_or(
buildbot@webrtc.org13d67762014-05-02 17:33:29 +0000717 default_agc_config_.digitalCompressionGaindB);
718 default_agc_config_.limiterEnable =
kwiberg102c6a62015-10-30 02:47:38 -0700719 options.tx_agc_limiter.value_or(default_agc_config_.limiterEnable);
buildbot@webrtc.org13d67762014-05-02 17:33:29 +0000720 if (voe_wrapper_->processing()->SetAgcConfig(default_agc_config_) == -1) {
721 LOG_RTCERR3(SetAgcConfig,
722 default_agc_config_.targetLeveldBOv,
723 default_agc_config_.digitalCompressionGaindB,
724 default_agc_config_.limiterEnable);
725 return false;
726 }
727 }
728
kwiberg102c6a62015-10-30 02:47:38 -0700729 if (options.noise_suppression) {
henrikac14f5ff2015-09-23 14:08:33 +0200730 const bool built_in_ns = voe_wrapper_->hw()->BuiltInNSIsAvailable();
731 if (built_in_ns) {
kwiberg102c6a62015-10-30 02:47:38 -0700732 if (voe_wrapper_->hw()->EnableBuiltInNS(*options.noise_suppression) ==
733 0 &&
734 *options.noise_suppression) {
henrikac14f5ff2015-09-23 14:08:33 +0200735 // Disable internal software NS if built-in NS is enabled,
736 // i.e., replace the software NS with the built-in NS.
Karl Wibergbe579832015-11-10 22:34:18 +0100737 options.noise_suppression = rtc::Optional<bool>(false);
henrikac14f5ff2015-09-23 14:08:33 +0200738 LOG(LS_INFO) << "Disabling NS since built-in NS will be used instead";
739 }
740 }
kwiberg102c6a62015-10-30 02:47:38 -0700741 if (voep->SetNsStatus(*options.noise_suppression, ns_mode) == -1) {
742 LOG_RTCERR2(SetNsStatus, *options.noise_suppression, ns_mode);
buildbot@webrtc.org13d67762014-05-02 17:33:29 +0000743 return false;
744 } else {
kwiberg102c6a62015-10-30 02:47:38 -0700745 LOG(LS_INFO) << "Noise suppression set to " << *options.noise_suppression
henrikac14f5ff2015-09-23 14:08:33 +0200746 << " with mode " << ns_mode;
buildbot@webrtc.org13d67762014-05-02 17:33:29 +0000747 }
748 }
749
kwiberg102c6a62015-10-30 02:47:38 -0700750 if (options.highpass_filter) {
751 LOG(LS_INFO) << "High pass filter enabled? " << *options.highpass_filter;
752 if (voep->EnableHighPassFilter(*options.highpass_filter) == -1) {
753 LOG_RTCERR1(SetHighpassFilterStatus, *options.highpass_filter);
buildbot@webrtc.org13d67762014-05-02 17:33:29 +0000754 return false;
755 }
756 }
757
kwiberg102c6a62015-10-30 02:47:38 -0700758 if (options.stereo_swapping) {
759 LOG(LS_INFO) << "Stereo swapping enabled? " << *options.stereo_swapping;
760 voep->EnableStereoChannelSwapping(*options.stereo_swapping);
761 if (voep->IsStereoChannelSwappingEnabled() != *options.stereo_swapping) {
762 LOG_RTCERR1(EnableStereoChannelSwapping, *options.stereo_swapping);
buildbot@webrtc.org13d67762014-05-02 17:33:29 +0000763 return false;
764 }
765 }
766
kwiberg102c6a62015-10-30 02:47:38 -0700767 if (options.audio_jitter_buffer_max_packets) {
768 LOG(LS_INFO) << "NetEq capacity is "
769 << *options.audio_jitter_buffer_max_packets;
Henrik Lundin64dad832015-05-11 12:44:23 +0200770 voe_config_.Set<webrtc::NetEqCapacityConfig>(
kwiberg102c6a62015-10-30 02:47:38 -0700771 new webrtc::NetEqCapacityConfig(
772 *options.audio_jitter_buffer_max_packets));
Henrik Lundin64dad832015-05-11 12:44:23 +0200773 }
774
kwiberg102c6a62015-10-30 02:47:38 -0700775 if (options.audio_jitter_buffer_fast_accelerate) {
776 LOG(LS_INFO) << "NetEq fast mode? "
777 << *options.audio_jitter_buffer_fast_accelerate;
Henrik Lundin5263b3c2015-06-01 10:29:41 +0200778 voe_config_.Set<webrtc::NetEqFastAccelerate>(
kwiberg102c6a62015-10-30 02:47:38 -0700779 new webrtc::NetEqFastAccelerate(
780 *options.audio_jitter_buffer_fast_accelerate));
Henrik Lundin5263b3c2015-06-01 10:29:41 +0200781 }
782
kwiberg102c6a62015-10-30 02:47:38 -0700783 if (options.typing_detection) {
784 LOG(LS_INFO) << "Typing detection is enabled? "
785 << *options.typing_detection;
786 if (voep->SetTypingDetectionStatus(*options.typing_detection) == -1) {
buildbot@webrtc.org13d67762014-05-02 17:33:29 +0000787 // In case of error, log the info and continue
kwiberg102c6a62015-10-30 02:47:38 -0700788 LOG_RTCERR1(SetTypingDetectionStatus, *options.typing_detection);
buildbot@webrtc.org13d67762014-05-02 17:33:29 +0000789 }
790 }
791
kwiberg102c6a62015-10-30 02:47:38 -0700792 if (options.adjust_agc_delta) {
793 LOG(LS_INFO) << "Adjust agc delta is " << *options.adjust_agc_delta;
794 if (!AdjustAgcLevel(*options.adjust_agc_delta)) {
buildbot@webrtc.org13d67762014-05-02 17:33:29 +0000795 return false;
796 }
797 }
798
kwiberg102c6a62015-10-30 02:47:38 -0700799 if (options.aec_dump) {
800 LOG(LS_INFO) << "Aec dump is enabled? " << *options.aec_dump;
801 if (*options.aec_dump)
buildbot@webrtc.org13d67762014-05-02 17:33:29 +0000802 StartAecDump(kAecDumpByAudioOptionFilename);
803 else
804 StopAecDump();
805 }
806
buildbot@webrtc.org1f8a2372014-08-28 10:52:44 +0000807 webrtc::Config config;
808
kwiberg102c6a62015-10-30 02:47:38 -0700809 if (options.delay_agnostic_aec)
810 delay_agnostic_aec_ = options.delay_agnostic_aec;
811 if (delay_agnostic_aec_) {
812 LOG(LS_INFO) << "Delay agnostic aec is enabled? " << *delay_agnostic_aec_;
henrik.lundin0f133b92015-07-02 00:17:55 -0700813 config.Set<webrtc::DelayAgnostic>(
kwiberg102c6a62015-10-30 02:47:38 -0700814 new webrtc::DelayAgnostic(*delay_agnostic_aec_));
Bjorn Volckerbf395c12015-03-25 22:45:56 +0100815 }
816
kwiberg102c6a62015-10-30 02:47:38 -0700817 if (options.extended_filter_aec) {
818 extended_filter_aec_ = options.extended_filter_aec;
819 }
820 if (extended_filter_aec_) {
821 LOG(LS_INFO) << "Extended filter aec is enabled? " << *extended_filter_aec_;
Henrik Lundin441f6342015-06-09 16:03:13 +0200822 config.Set<webrtc::ExtendedFilter>(
kwiberg102c6a62015-10-30 02:47:38 -0700823 new webrtc::ExtendedFilter(*extended_filter_aec_));
buildbot@webrtc.org1f8a2372014-08-28 10:52:44 +0000824 }
825
kwiberg102c6a62015-10-30 02:47:38 -0700826 if (options.experimental_ns) {
827 experimental_ns_ = options.experimental_ns;
828 }
829 if (experimental_ns_) {
830 LOG(LS_INFO) << "Experimental ns is enabled? " << *experimental_ns_;
buildbot@webrtc.org1f8a2372014-08-28 10:52:44 +0000831 config.Set<webrtc::ExperimentalNs>(
kwiberg102c6a62015-10-30 02:47:38 -0700832 new webrtc::ExperimentalNs(*experimental_ns_));
buildbot@webrtc.org1f8a2372014-08-28 10:52:44 +0000833 }
buildbot@webrtc.org1f8a2372014-08-28 10:52:44 +0000834
835 // We check audioproc for the benefit of tests, since FakeWebRtcVoiceEngine
836 // returns NULL on audio_processing().
837 webrtc::AudioProcessing* audioproc = voe_wrapper_->base()->audio_processing();
838 if (audioproc) {
839 audioproc->SetExtraOptions(config);
840 }
841
kwiberg102c6a62015-10-30 02:47:38 -0700842 if (options.recording_sample_rate) {
843 LOG(LS_INFO) << "Recording sample rate is "
844 << *options.recording_sample_rate;
845 if (voe_wrapper_->hw()->SetRecordingSampleRate(
846 *options.recording_sample_rate)) {
847 LOG_RTCERR1(SetRecordingSampleRate, *options.recording_sample_rate);
buildbot@webrtc.org13d67762014-05-02 17:33:29 +0000848 }
849 }
850
kwiberg102c6a62015-10-30 02:47:38 -0700851 if (options.playout_sample_rate) {
852 LOG(LS_INFO) << "Playout sample rate is " << *options.playout_sample_rate;
853 if (voe_wrapper_->hw()->SetPlayoutSampleRate(
854 *options.playout_sample_rate)) {
855 LOG_RTCERR1(SetPlayoutSampleRate, *options.playout_sample_rate);
buildbot@webrtc.org13d67762014-05-02 17:33:29 +0000856 }
857 }
858
859 return true;
860}
861
solenberg246b8172015-12-08 09:50:23 -0800862void WebRtcVoiceEngine::SetDefaultDevices() {
solenberg566ef242015-11-06 15:34:49 -0800863 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
buildbot@webrtc.org13d67762014-05-02 17:33:29 +0000864#if !defined(IOS)
solenberg246b8172015-12-08 09:50:23 -0800865 int in_id = kDefaultAudioDeviceId;
866 int out_id = kDefaultAudioDeviceId;
867 LOG(LS_INFO) << "Setting microphone to (id=" << in_id
868 << ") and speaker to (id=" << out_id << ")";
buildbot@webrtc.org13d67762014-05-02 17:33:29 +0000869
solenbergc1a1b352015-09-22 13:31:20 -0700870 bool ret = true;
solenberg246b8172015-12-08 09:50:23 -0800871 if (voe_wrapper_->hw()->SetRecordingDevice(in_id) == -1) {
872 LOG_RTCERR1(SetRecordingDevice, in_id);
buildbot@webrtc.org13d67762014-05-02 17:33:29 +0000873 ret = false;
874 }
solenberg246b8172015-12-08 09:50:23 -0800875 webrtc::AudioProcessing* ap = voe()->base()->audio_processing();
876 if (ap) {
877 ap->Initialize();
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000878 }
879
solenberg246b8172015-12-08 09:50:23 -0800880 if (voe_wrapper_->hw()->SetPlayoutDevice(out_id) == -1) {
881 LOG_RTCERR1(SetPlayoutDevice, out_id);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000882 ret = false;
883 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000884
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000885 if (ret) {
solenberg246b8172015-12-08 09:50:23 -0800886 LOG(LS_INFO) << "Set microphone to (id=" << in_id
887 << ") and speaker to (id=" << out_id << ")";
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000888 }
wu@webrtc.orgcecfd182013-10-30 05:18:12 +0000889#endif // !IOS
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000890}
891
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000892bool WebRtcVoiceEngine::GetOutputVolume(int* level) {
solenberg566ef242015-11-06 15:34:49 -0800893 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000894 unsigned int ulevel;
895 if (voe_wrapper_->volume()->GetSpeakerVolume(ulevel) == -1) {
896 LOG_RTCERR1(GetSpeakerVolume, level);
897 return false;
898 }
899 *level = ulevel;
900 return true;
901}
902
903bool WebRtcVoiceEngine::SetOutputVolume(int level) {
solenberg566ef242015-11-06 15:34:49 -0800904 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
henrikg91d6ede2015-09-17 00:24:34 -0700905 RTC_DCHECK(level >= 0 && level <= 255);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000906 if (voe_wrapper_->volume()->SetSpeakerVolume(level) == -1) {
907 LOG_RTCERR1(SetSpeakerVolume, level);
908 return false;
909 }
910 return true;
911}
912
913int WebRtcVoiceEngine::GetInputLevel() {
solenberg566ef242015-11-06 15:34:49 -0800914 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000915 unsigned int ulevel;
916 return (voe_wrapper_->volume()->GetSpeechInputLevel(ulevel) != -1) ?
917 static_cast<int>(ulevel) : -1;
918}
919
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000920const std::vector<AudioCodec>& WebRtcVoiceEngine::codecs() {
solenberg566ef242015-11-06 15:34:49 -0800921 RTC_DCHECK(signal_thread_checker_.CalledOnValidThread());
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000922 return codecs_;
923}
924
Stefan Holmer9d69c3f2015-12-07 10:45:43 +0100925RtpCapabilities WebRtcVoiceEngine::GetCapabilities() const {
solenberg566ef242015-11-06 15:34:49 -0800926 RTC_DCHECK(signal_thread_checker_.CalledOnValidThread());
Stefan Holmer9d69c3f2015-12-07 10:45:43 +0100927 RtpCapabilities capabilities;
928 capabilities.header_extensions.push_back(RtpHeaderExtension(
929 kRtpAudioLevelHeaderExtension, kRtpAudioLevelHeaderExtensionDefaultId));
930 capabilities.header_extensions.push_back(
931 RtpHeaderExtension(kRtpAbsoluteSenderTimeHeaderExtension,
932 kRtpAbsoluteSenderTimeHeaderExtensionDefaultId));
Stefan Holmer9d69c3f2015-12-07 10:45:43 +0100933 return capabilities;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000934}
935
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000936int WebRtcVoiceEngine::GetLastEngineError() {
solenberg566ef242015-11-06 15:34:49 -0800937 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000938 return voe_wrapper_->error();
939}
940
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000941void WebRtcVoiceEngine::Print(webrtc::TraceLevel level, const char* trace,
942 int length) {
solenberg566ef242015-11-06 15:34:49 -0800943 // Note: This callback can happen on any thread!
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000944 rtc::LoggingSeverity sev = rtc::LS_VERBOSE;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000945 if (level == webrtc::kTraceError || level == webrtc::kTraceCritical)
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000946 sev = rtc::LS_ERROR;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000947 else if (level == webrtc::kTraceWarning)
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000948 sev = rtc::LS_WARNING;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000949 else if (level == webrtc::kTraceStateInfo || level == webrtc::kTraceInfo)
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000950 sev = rtc::LS_INFO;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000951 else if (level == webrtc::kTraceTerseInfo)
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000952 sev = rtc::LS_INFO;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000953
954 // Skip past boilerplate prefix text
955 if (length < 72) {
956 std::string msg(trace, length);
957 LOG(LS_ERROR) << "Malformed webrtc log message: ";
958 LOG_V(sev) << msg;
959 } else {
960 std::string msg(trace + 71, length - 72);
Peter Boströmd5c75b12015-09-23 13:24:32 +0200961 LOG_V(sev) << "webrtc: " << msg;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000962 }
963}
964
solenberg63b34542015-09-29 06:06:31 -0700965void WebRtcVoiceEngine::RegisterChannel(WebRtcVoiceMediaChannel* channel) {
solenberg566ef242015-11-06 15:34:49 -0800966 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
967 RTC_DCHECK(channel);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000968 channels_.push_back(channel);
969}
970
solenberg63b34542015-09-29 06:06:31 -0700971void WebRtcVoiceEngine::UnregisterChannel(WebRtcVoiceMediaChannel* channel) {
solenberg566ef242015-11-06 15:34:49 -0800972 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
solenberg63b34542015-09-29 06:06:31 -0700973 auto it = std::find(channels_.begin(), channels_.end(), channel);
solenberg566ef242015-11-06 15:34:49 -0800974 RTC_DCHECK(it != channels_.end());
975 channels_.erase(it);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000976}
977
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000978// Adjusts the default AGC target level by the specified delta.
979// NB: If we start messing with other config fields, we'll want
980// to save the current webrtc::AgcConfig as well.
981bool WebRtcVoiceEngine::AdjustAgcLevel(int delta) {
solenberg566ef242015-11-06 15:34:49 -0800982 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000983 webrtc::AgcConfig config = default_agc_config_;
984 config.targetLeveldBOv -= delta;
985
986 LOG(LS_INFO) << "Adjusting AGC level from default -"
987 << default_agc_config_.targetLeveldBOv << "dB to -"
988 << config.targetLeveldBOv << "dB";
989
990 if (voe_wrapper_->processing()->SetAgcConfig(config) == -1) {
991 LOG_RTCERR1(SetAgcConfig, config.targetLeveldBOv);
992 return false;
993 }
994 return true;
995}
996
Fredrik Solenbergccb49e72015-05-19 11:37:56 +0200997bool WebRtcVoiceEngine::SetAudioDeviceModule(webrtc::AudioDeviceModule* adm) {
solenberg566ef242015-11-06 15:34:49 -0800998 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000999 if (initialized_) {
1000 LOG(LS_WARNING) << "SetAudioDeviceModule can not be called after Init.";
1001 return false;
1002 }
1003 if (adm_) {
1004 adm_->Release();
1005 adm_ = NULL;
1006 }
1007 if (adm) {
1008 adm_ = adm;
1009 adm_->AddRef();
1010 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001011 return true;
1012}
1013
ivocf4f5cb02015-12-19 10:02:30 -08001014bool WebRtcVoiceEngine::StartAecDump(rtc::PlatformFile file,
1015 int64_t max_size_bytes) {
solenberg566ef242015-11-06 15:34:49 -08001016 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00001017 FILE* aec_dump_file_stream = rtc::FdopenPlatformFileForWriting(file);
wu@webrtc.orga8910d22014-01-23 22:12:45 +00001018 if (!aec_dump_file_stream) {
1019 LOG(LS_ERROR) << "Could not open AEC dump file stream.";
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00001020 if (!rtc::ClosePlatformFile(file))
wu@webrtc.orga8910d22014-01-23 22:12:45 +00001021 LOG(LS_WARNING) << "Could not close file.";
1022 return false;
1023 }
wu@webrtc.orga9890802013-12-13 00:21:03 +00001024 StopAecDump();
ivocf4f5cb02015-12-19 10:02:30 -08001025 if (voe_wrapper_->base()->audio_processing()->StartDebugRecording(
1026 aec_dump_file_stream, max_size_bytes) !=
wu@webrtc.orga9890802013-12-13 00:21:03 +00001027 webrtc::AudioProcessing::kNoError) {
wu@webrtc.orga8910d22014-01-23 22:12:45 +00001028 LOG_RTCERR0(StartDebugRecording);
1029 fclose(aec_dump_file_stream);
wu@webrtc.orga9890802013-12-13 00:21:03 +00001030 return false;
1031 }
1032 is_dumping_aec_ = true;
1033 return true;
wu@webrtc.orga9890802013-12-13 00:21:03 +00001034}
1035
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001036void WebRtcVoiceEngine::StartAecDump(const std::string& filename) {
solenberg566ef242015-11-06 15:34:49 -08001037 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001038 if (!is_dumping_aec_) {
1039 // Start dumping AEC when we are not dumping.
ivocf4f5cb02015-12-19 10:02:30 -08001040 if (voe_wrapper_->base()->audio_processing()->StartDebugRecording(
1041 filename.c_str(), -1) != webrtc::AudioProcessing::kNoError) {
wu@webrtc.orga9890802013-12-13 00:21:03 +00001042 LOG_RTCERR1(StartDebugRecording, filename.c_str());
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001043 } else {
1044 is_dumping_aec_ = true;
1045 }
1046 }
1047}
1048
1049void WebRtcVoiceEngine::StopAecDump() {
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 // Stop dumping AEC when we are dumping.
ivocf4f5cb02015-12-19 10:02:30 -08001053 if (voe_wrapper_->base()->audio_processing()->StopDebugRecording() !=
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001054 webrtc::AudioProcessing::kNoError) {
1055 LOG_RTCERR0(StopDebugRecording);
1056 }
1057 is_dumping_aec_ = false;
1058 }
1059}
1060
ivoc112a3d82015-10-16 02:22:18 -07001061bool WebRtcVoiceEngine::StartRtcEventLog(rtc::PlatformFile file) {
solenberg566ef242015-11-06 15:34:49 -08001062 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
ivoc112a3d82015-10-16 02:22:18 -07001063 return voe_wrapper_->codec()->GetEventLog()->StartLogging(file);
1064}
1065
1066void WebRtcVoiceEngine::StopRtcEventLog() {
solenberg566ef242015-11-06 15:34:49 -08001067 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
ivoc112a3d82015-10-16 02:22:18 -07001068 voe_wrapper_->codec()->GetEventLog()->StopLogging();
1069}
1070
solenberg0a617e22015-10-20 15:49:38 -07001071int WebRtcVoiceEngine::CreateVoEChannel() {
solenberg566ef242015-11-06 15:34:49 -08001072 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
solenberg0a617e22015-10-20 15:49:38 -07001073 return voe_wrapper_->base()->CreateChannel(voe_config_);
sergeyu@chromium.org5bc25c42013-12-05 00:24:06 +00001074}
1075
solenbergc96df772015-10-21 13:01:53 -07001076class WebRtcVoiceMediaChannel::WebRtcAudioSendStream
mallinath@webrtc.org67ee6b92014-02-03 16:57:16 +00001077 : public AudioRenderer::Sink {
1078 public:
solenbergc96df772015-10-21 13:01:53 -07001079 WebRtcAudioSendStream(int ch, webrtc::AudioTransport* voe_audio_transport,
solenberg3a941542015-11-16 07:34:50 -08001080 uint32_t ssrc, const std::string& c_name,
1081 const std::vector<webrtc::RtpExtension>& extensions,
1082 webrtc::Call* call)
solenberg7add0582015-11-20 09:59:34 -08001083 : voe_audio_transport_(voe_audio_transport),
solenberg3a941542015-11-16 07:34:50 -08001084 call_(call),
1085 config_(nullptr) {
solenberg85a04962015-10-27 03:35:21 -07001086 RTC_DCHECK_GE(ch, 0);
1087 // TODO(solenberg): Once we're not using FakeWebRtcVoiceEngine anymore:
1088 // RTC_DCHECK(voe_audio_transport);
solenbergc96df772015-10-21 13:01:53 -07001089 RTC_DCHECK(call);
solenberg85a04962015-10-27 03:35:21 -07001090 audio_capture_thread_checker_.DetachFromThread();
solenberg3a941542015-11-16 07:34:50 -08001091 config_.rtp.ssrc = ssrc;
1092 config_.rtp.c_name = c_name;
1093 config_.voe_channel_id = ch;
1094 RecreateAudioSendStream(extensions);
solenbergc96df772015-10-21 13:01:53 -07001095 }
solenberg3a941542015-11-16 07:34:50 -08001096
solenbergc96df772015-10-21 13:01:53 -07001097 ~WebRtcAudioSendStream() override {
solenberg566ef242015-11-06 15:34:49 -08001098 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
solenbergc96df772015-10-21 13:01:53 -07001099 Stop();
1100 call_->DestroyAudioSendStream(stream_);
1101 }
henrike@webrtc.org1e09a712013-07-26 19:17:59 +00001102
solenberg3a941542015-11-16 07:34:50 -08001103 void RecreateAudioSendStream(
1104 const std::vector<webrtc::RtpExtension>& extensions) {
1105 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
1106 if (stream_) {
1107 call_->DestroyAudioSendStream(stream_);
1108 stream_ = nullptr;
1109 }
1110 config_.rtp.extensions = extensions;
1111 RTC_DCHECK(!stream_);
1112 stream_ = call_->CreateAudioSendStream(config_);
1113 RTC_CHECK(stream_);
1114 }
1115
Fredrik Solenbergb5727682015-12-04 15:22:19 +01001116 bool SendTelephoneEvent(int payload_type, uint8_t event,
1117 uint32_t duration_ms) {
1118 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
1119 RTC_DCHECK(stream_);
1120 return stream_->SendTelephoneEvent(payload_type, event, duration_ms);
1121 }
1122
solenberg3a941542015-11-16 07:34:50 -08001123 webrtc::AudioSendStream::Stats GetStats() const {
1124 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
1125 RTC_DCHECK(stream_);
1126 return stream_->GetStats();
1127 }
1128
mallinath@webrtc.org67ee6b92014-02-03 16:57:16 +00001129 // Starts the rendering by setting a sink to the renderer to get data
1130 // callback.
henrike@webrtc.orga7b98182014-02-21 15:51:43 +00001131 // This method is called on the libjingle worker thread.
mallinath@webrtc.org67ee6b92014-02-03 16:57:16 +00001132 // TODO(xians): Make sure Start() is called only once.
1133 void Start(AudioRenderer* renderer) {
solenberg566ef242015-11-06 15:34:49 -08001134 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
solenbergc96df772015-10-21 13:01:53 -07001135 RTC_DCHECK(renderer);
1136 if (renderer_) {
henrikg91d6ede2015-09-17 00:24:34 -07001137 RTC_DCHECK(renderer_ == renderer);
mallinath@webrtc.org67ee6b92014-02-03 16:57:16 +00001138 return;
1139 }
mallinath@webrtc.org67ee6b92014-02-03 16:57:16 +00001140 renderer->SetSink(this);
1141 renderer_ = renderer;
1142 }
1143
solenbergc96df772015-10-21 13:01:53 -07001144 // Stops rendering by setting the sink of the renderer to nullptr. No data
mallinath@webrtc.org67ee6b92014-02-03 16:57:16 +00001145 // callback will be received after this method.
henrike@webrtc.orga7b98182014-02-21 15:51:43 +00001146 // This method is called on the libjingle worker thread.
mallinath@webrtc.org67ee6b92014-02-03 16:57:16 +00001147 void Stop() {
solenberg566ef242015-11-06 15:34:49 -08001148 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
solenbergc96df772015-10-21 13:01:53 -07001149 if (renderer_) {
1150 renderer_->SetSink(nullptr);
1151 renderer_ = nullptr;
solenberg98c68862015-10-09 03:27:14 -07001152 }
mallinath@webrtc.org67ee6b92014-02-03 16:57:16 +00001153 }
1154
1155 // AudioRenderer::Sink implementation.
henrike@webrtc.orga7b98182014-02-21 15:51:43 +00001156 // This method is called on the audio thread.
kjellander@webrtc.org14665ff2015-03-04 12:58:35 +00001157 void OnData(const void* audio_data,
1158 int bits_per_sample,
1159 int sample_rate,
1160 int number_of_channels,
Peter Kastingdce40cf2015-08-24 14:52:23 -07001161 size_t number_of_frames) override {
solenberg566ef242015-11-06 15:34:49 -08001162 RTC_DCHECK(!worker_thread_checker_.CalledOnValidThread());
solenberg85a04962015-10-27 03:35:21 -07001163 RTC_DCHECK(audio_capture_thread_checker_.CalledOnValidThread());
solenbergc96df772015-10-21 13:01:53 -07001164 RTC_DCHECK(voe_audio_transport_);
solenberg7add0582015-11-20 09:59:34 -08001165 voe_audio_transport_->OnData(config_.voe_channel_id,
henrike@webrtc.orga7b98182014-02-21 15:51:43 +00001166 audio_data,
1167 bits_per_sample,
1168 sample_rate,
1169 number_of_channels,
1170 number_of_frames);
henrike@webrtc.orga7b98182014-02-21 15:51:43 +00001171 }
1172
1173 // Callback from the |renderer_| when it is going away. In case Start() has
1174 // never been called, this callback won't be triggered.
kjellander@webrtc.org14665ff2015-03-04 12:58:35 +00001175 void OnClose() override {
solenberg566ef242015-11-06 15:34:49 -08001176 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
solenbergc96df772015-10-21 13:01:53 -07001177 // Set |renderer_| to nullptr to make sure no more callback will get into
henrike@webrtc.orga7b98182014-02-21 15:51:43 +00001178 // the renderer.
solenbergc96df772015-10-21 13:01:53 -07001179 renderer_ = nullptr;
mallinath@webrtc.org67ee6b92014-02-03 16:57:16 +00001180 }
1181
1182 // Accessor to the VoE channel ID.
solenberg85a04962015-10-27 03:35:21 -07001183 int channel() const {
solenberg566ef242015-11-06 15:34:49 -08001184 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
solenberg7add0582015-11-20 09:59:34 -08001185 return config_.voe_channel_id;
solenberg85a04962015-10-27 03:35:21 -07001186 }
mallinath@webrtc.org67ee6b92014-02-03 16:57:16 +00001187
1188 private:
solenberg566ef242015-11-06 15:34:49 -08001189 rtc::ThreadChecker worker_thread_checker_;
solenberg85a04962015-10-27 03:35:21 -07001190 rtc::ThreadChecker audio_capture_thread_checker_;
solenbergc96df772015-10-21 13:01:53 -07001191 webrtc::AudioTransport* const voe_audio_transport_ = nullptr;
1192 webrtc::Call* call_ = nullptr;
solenberg3a941542015-11-16 07:34:50 -08001193 webrtc::AudioSendStream::Config config_;
1194 // The stream is owned by WebRtcAudioSendStream and may be reallocated if
1195 // configuration changes.
solenbergc96df772015-10-21 13:01:53 -07001196 webrtc::AudioSendStream* stream_ = nullptr;
mallinath@webrtc.org67ee6b92014-02-03 16:57:16 +00001197
1198 // Raw pointer to AudioRenderer owned by LocalAudioTrackHandler.
1199 // PeerConnection will make sure invalidating the pointer before the object
1200 // goes away.
solenbergc96df772015-10-21 13:01:53 -07001201 AudioRenderer* renderer_ = nullptr;
henrike@webrtc.orga7b98182014-02-21 15:51:43 +00001202
solenbergc96df772015-10-21 13:01:53 -07001203 RTC_DISALLOW_IMPLICIT_CONSTRUCTORS(WebRtcAudioSendStream);
1204};
1205
1206class WebRtcVoiceMediaChannel::WebRtcAudioReceiveStream {
1207 public:
solenberg7add0582015-11-20 09:59:34 -08001208 WebRtcAudioReceiveStream(int ch, uint32_t remote_ssrc, uint32_t local_ssrc,
1209 bool use_combined_bwe, const std::string& sync_group,
1210 const std::vector<webrtc::RtpExtension>& extensions,
1211 webrtc::Call* call)
1212 : call_(call),
1213 config_() {
1214 RTC_DCHECK_GE(ch, 0);
1215 RTC_DCHECK(call);
1216 config_.rtp.remote_ssrc = remote_ssrc;
1217 config_.rtp.local_ssrc = local_ssrc;
1218 config_.voe_channel_id = ch;
1219 config_.sync_group = sync_group;
1220 RecreateAudioReceiveStream(use_combined_bwe, extensions);
1221 }
solenbergc96df772015-10-21 13:01:53 -07001222
solenberg7add0582015-11-20 09:59:34 -08001223 ~WebRtcAudioReceiveStream() {
1224 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
1225 call_->DestroyAudioReceiveStream(stream_);
1226 }
1227
1228 void RecreateAudioReceiveStream(
1229 const std::vector<webrtc::RtpExtension>& extensions) {
1230 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
1231 RecreateAudioReceiveStream(config_.combined_audio_video_bwe, extensions);
1232 }
1233 void RecreateAudioReceiveStream(bool use_combined_bwe) {
1234 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
1235 RecreateAudioReceiveStream(use_combined_bwe, config_.rtp.extensions);
1236 }
1237
1238 webrtc::AudioReceiveStream::Stats GetStats() const {
1239 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
1240 RTC_DCHECK(stream_);
1241 return stream_->GetStats();
1242 }
1243
1244 int channel() const {
1245 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
1246 return config_.voe_channel_id;
1247 }
solenbergc96df772015-10-21 13:01:53 -07001248
Tommif888bb52015-12-12 01:37:01 +01001249 void SetRawAudioSink(rtc::scoped_ptr<webrtc::AudioSinkInterface> sink) {
1250 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
1251 stream_->SetSink(std::move(sink));
1252 }
1253
solenbergc96df772015-10-21 13:01:53 -07001254 private:
solenberg7add0582015-11-20 09:59:34 -08001255 void RecreateAudioReceiveStream(bool use_combined_bwe,
1256 const std::vector<webrtc::RtpExtension>& extensions) {
1257 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
1258 if (stream_) {
1259 call_->DestroyAudioReceiveStream(stream_);
1260 stream_ = nullptr;
1261 }
1262 config_.rtp.extensions = extensions;
1263 config_.combined_audio_video_bwe = use_combined_bwe;
1264 RTC_DCHECK(!stream_);
1265 stream_ = call_->CreateAudioReceiveStream(config_);
1266 RTC_CHECK(stream_);
1267 }
1268
1269 rtc::ThreadChecker worker_thread_checker_;
1270 webrtc::Call* call_ = nullptr;
1271 webrtc::AudioReceiveStream::Config config_;
1272 // The stream is owned by WebRtcAudioReceiveStream and may be reallocated if
1273 // configuration changes.
1274 webrtc::AudioReceiveStream* stream_ = nullptr;
solenbergc96df772015-10-21 13:01:53 -07001275
1276 RTC_DISALLOW_IMPLICIT_CONSTRUCTORS(WebRtcAudioReceiveStream);
henrike@webrtc.org1e09a712013-07-26 19:17:59 +00001277};
1278
Fredrik Solenberg709ed672015-09-15 12:26:33 +02001279WebRtcVoiceMediaChannel::WebRtcVoiceMediaChannel(WebRtcVoiceEngine* engine,
Fredrik Solenbergb071a192015-09-17 16:42:56 +02001280 const AudioOptions& options,
Fredrik Solenberg709ed672015-09-15 12:26:33 +02001281 webrtc::Call* call)
solenberg566ef242015-11-06 15:34:49 -08001282 : engine_(engine), call_(call) {
solenberg0a617e22015-10-20 15:49:38 -07001283 LOG(LS_VERBOSE) << "WebRtcVoiceMediaChannel::WebRtcVoiceMediaChannel";
solenberg566ef242015-11-06 15:34:49 -08001284 RTC_DCHECK(call);
solenberg0a617e22015-10-20 15:49:38 -07001285 engine->RegisterChannel(this);
Fredrik Solenbergb071a192015-09-17 16:42:56 +02001286 SetOptions(options);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001287}
1288
1289WebRtcVoiceMediaChannel::~WebRtcVoiceMediaChannel() {
solenberg566ef242015-11-06 15:34:49 -08001290 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
solenberg0a617e22015-10-20 15:49:38 -07001291 LOG(LS_VERBOSE) << "WebRtcVoiceMediaChannel::~WebRtcVoiceMediaChannel";
solenberg7add0582015-11-20 09:59:34 -08001292 // TODO(solenberg): Should be able to delete the streams directly, without
1293 // going through RemoveNnStream(), once stream objects handle
1294 // all (de)configuration.
solenbergc96df772015-10-21 13:01:53 -07001295 while (!send_streams_.empty()) {
1296 RemoveSendStream(send_streams_.begin()->first);
solenbergd97ec302015-10-07 01:40:33 -07001297 }
solenberg7add0582015-11-20 09:59:34 -08001298 while (!recv_streams_.empty()) {
1299 RemoveRecvStream(recv_streams_.begin()->first);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001300 }
solenberg0a617e22015-10-20 15:49:38 -07001301 engine()->UnregisterChannel(this);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001302}
1303
Peter Thatcherc2ee2c82015-08-07 16:05:34 -07001304bool WebRtcVoiceMediaChannel::SetSendParameters(
1305 const AudioSendParameters& params) {
solenberg566ef242015-11-06 15:34:49 -08001306 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
solenberg7e4e01a2015-12-02 08:05:01 -08001307 LOG(LS_INFO) << "WebRtcVoiceMediaChannel::SetSendParameters: "
1308 << params.ToString();
Peter Thatcherc2ee2c82015-08-07 16:05:34 -07001309 // TODO(pthatcher): Refactor this to be more clean now that we have
1310 // all the information at once.
solenberg3a941542015-11-16 07:34:50 -08001311
1312 if (!SetSendCodecs(params.codecs)) {
1313 return false;
1314 }
1315
solenberg7e4e01a2015-12-02 08:05:01 -08001316 if (!ValidateRtpExtensions(params.extensions)) {
1317 return false;
1318 }
1319 std::vector<webrtc::RtpExtension> filtered_extensions =
1320 FilterRtpExtensions(params.extensions,
1321 webrtc::RtpExtension::IsSupportedForAudio, true);
1322 if (send_rtp_extensions_ != filtered_extensions) {
1323 send_rtp_extensions_.swap(filtered_extensions);
solenberg3a941542015-11-16 07:34:50 -08001324 for (auto& it : send_streams_) {
1325 it.second->RecreateAudioSendStream(send_rtp_extensions_);
1326 }
1327 }
1328
1329 if (!SetMaxSendBandwidth(params.max_bandwidth_bps)) {
1330 return false;
1331 }
1332 return SetOptions(params.options);
Peter Thatcherc2ee2c82015-08-07 16:05:34 -07001333}
1334
1335bool WebRtcVoiceMediaChannel::SetRecvParameters(
1336 const AudioRecvParameters& params) {
solenberg566ef242015-11-06 15:34:49 -08001337 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
solenberg7e4e01a2015-12-02 08:05:01 -08001338 LOG(LS_INFO) << "WebRtcVoiceMediaChannel::SetRecvParameters: "
1339 << params.ToString();
Peter Thatcherc2ee2c82015-08-07 16:05:34 -07001340 // TODO(pthatcher): Refactor this to be more clean now that we have
1341 // all the information at once.
solenberg7add0582015-11-20 09:59:34 -08001342
1343 if (!SetRecvCodecs(params.codecs)) {
1344 return false;
1345 }
1346
solenberg7e4e01a2015-12-02 08:05:01 -08001347 if (!ValidateRtpExtensions(params.extensions)) {
1348 return false;
1349 }
1350 std::vector<webrtc::RtpExtension> filtered_extensions =
1351 FilterRtpExtensions(params.extensions,
1352 webrtc::RtpExtension::IsSupportedForAudio, false);
1353 if (recv_rtp_extensions_ != filtered_extensions) {
1354 recv_rtp_extensions_.swap(filtered_extensions);
solenberg7add0582015-11-20 09:59:34 -08001355 for (auto& it : recv_streams_) {
1356 it.second->RecreateAudioReceiveStream(recv_rtp_extensions_);
1357 }
1358 }
1359
1360 return true;
Peter Thatcherc2ee2c82015-08-07 16:05:34 -07001361}
1362
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001363bool WebRtcVoiceMediaChannel::SetOptions(const AudioOptions& options) {
solenberg566ef242015-11-06 15:34:49 -08001364 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001365 LOG(LS_INFO) << "Setting voice channel options: "
1366 << options.ToString();
1367
wu@webrtc.orgde305012013-10-31 15:40:38 +00001368 // Check if DSCP value is changed from previous.
1369 bool dscp_option_changed = (options_.dscp != options.dscp);
1370
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001371 // We retain all of the existing options, and apply the given ones
1372 // on top. This means there is no way to "clear" options such that
1373 // they go back to the engine default.
1374 options_.SetAll(options);
solenberg246b8172015-12-08 09:50:23 -08001375 if (!engine()->ApplyOptions(options_)) {
1376 LOG(LS_WARNING) <<
1377 "Failed to apply engine options during channel SetOptions.";
1378 return false;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001379 }
1380
wu@webrtc.orgde305012013-10-31 15:40:38 +00001381 if (dscp_option_changed) {
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00001382 rtc::DiffServCodePoint dscp = rtc::DSCP_DEFAULT;
solenberg246b8172015-12-08 09:50:23 -08001383 if (options_.dscp.value_or(false)) {
wu@webrtc.orgde305012013-10-31 15:40:38 +00001384 dscp = kAudioDscpValue;
solenberg246b8172015-12-08 09:50:23 -08001385 }
wu@webrtc.orgde305012013-10-31 15:40:38 +00001386 if (MediaChannel::SetDscp(dscp) != 0) {
1387 LOG(LS_WARNING) << "Failed to set DSCP settings for audio channel";
1388 }
1389 }
solenberg8fb30c32015-10-13 03:06:58 -07001390
solenbergc96df772015-10-21 13:01:53 -07001391 // TODO(solenberg): Don't recreate unless options changed.
solenberg7add0582015-11-20 09:59:34 -08001392 for (auto& it : recv_streams_) {
1393 it.second->RecreateAudioReceiveStream(
1394 options_.combined_audio_video_bwe.value_or(false));
1395 }
solenberg8fb30c32015-10-13 03:06:58 -07001396
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001397 LOG(LS_INFO) << "Set voice channel options. Current options: "
1398 << options_.ToString();
1399 return true;
1400}
1401
1402bool WebRtcVoiceMediaChannel::SetRecvCodecs(
1403 const std::vector<AudioCodec>& codecs) {
solenberg566ef242015-11-06 15:34:49 -08001404 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
solenberg8fb30c32015-10-13 03:06:58 -07001405
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001406 // Set the payload types to be used for incoming media.
solenberg0b675462015-10-09 01:37:09 -07001407 LOG(LS_INFO) << "Setting receive voice codecs.";
solenberg0b675462015-10-09 01:37:09 -07001408
1409 if (!VerifyUniquePayloadTypes(codecs)) {
1410 LOG(LS_ERROR) << "Codec payload types overlap.";
1411 return false;
1412 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001413
1414 std::vector<AudioCodec> new_codecs;
1415 // Find all new codecs. We allow adding new codecs but don't allow changing
1416 // the payload type of codecs that is already configured since we might
1417 // already be receiving packets with that payload type.
Fredrik Solenbergaf9fb212015-08-26 10:45:53 +02001418 for (const AudioCodec& codec : codecs) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001419 AudioCodec old_codec;
Fredrik Solenbergaf9fb212015-08-26 10:45:53 +02001420 if (FindCodec(recv_codecs_, codec, &old_codec)) {
1421 if (old_codec.id != codec.id) {
1422 LOG(LS_ERROR) << codec.name << " payload type changed.";
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001423 return false;
1424 }
1425 } else {
Fredrik Solenbergaf9fb212015-08-26 10:45:53 +02001426 new_codecs.push_back(codec);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001427 }
1428 }
1429 if (new_codecs.empty()) {
1430 // There are no new codecs to configure. Already configured codecs are
1431 // never removed.
1432 return true;
1433 }
1434
1435 if (playout_) {
1436 // Receive codecs can not be changed while playing. So we temporarily
1437 // pause playout.
1438 PausePlayout();
1439 }
1440
solenberg26c8c912015-11-27 04:00:25 -08001441 bool result = true;
1442 for (const AudioCodec& codec : new_codecs) {
1443 webrtc::CodecInst voe_codec;
1444 if (WebRtcVoiceEngine::ToCodecInst(codec, &voe_codec)) {
1445 LOG(LS_INFO) << ToString(codec);
1446 voe_codec.pltype = codec.id;
1447 for (const auto& ch : recv_streams_) {
1448 if (engine()->voe()->codec()->SetRecPayloadType(
1449 ch.second->channel(), voe_codec) == -1) {
1450 LOG_RTCERR2(SetRecPayloadType, ch.second->channel(),
1451 ToString(voe_codec));
1452 result = false;
1453 }
1454 }
1455 } else {
1456 LOG(LS_WARNING) << "Unknown codec " << ToString(codec);
1457 result = false;
1458 break;
1459 }
1460 }
Fredrik Solenbergaf9fb212015-08-26 10:45:53 +02001461 if (result) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001462 recv_codecs_ = codecs;
1463 }
1464
1465 if (desired_playout_ && !playout_) {
1466 ResumePlayout();
1467 }
Fredrik Solenbergaf9fb212015-08-26 10:45:53 +02001468 return result;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001469}
1470
1471bool WebRtcVoiceMediaChannel::SetSendCodecs(
wu@webrtc.orgcadf9042013-08-30 21:24:16 +00001472 int channel, const std::vector<AudioCodec>& codecs) {
buildbot@webrtc.orgae740dd2014-06-17 10:56:41 +00001473 // Disable VAD, FEC, and RED unless we know the other side wants them.
wu@webrtc.orgcadf9042013-08-30 21:24:16 +00001474 engine()->voe()->codec()->SetVADStatus(channel, false);
1475 engine()->voe()->rtp()->SetNACKStatus(channel, false, 0);
buildbot@webrtc.orgae740dd2014-06-17 10:56:41 +00001476 engine()->voe()->rtp()->SetREDStatus(channel, false);
1477 engine()->voe()->codec()->SetFECStatus(channel, false);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001478
1479 // Scan through the list to figure out the codec to use for sending, along
Fredrik Solenbergb5727682015-12-04 15:22:19 +01001480 // with the proper configuration for VAD.
henrike@webrtc.org704bf9e2014-02-27 17:52:04 +00001481 bool found_send_codec = false;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001482 webrtc::CodecInst send_codec;
1483 memset(&send_codec, 0, sizeof(send_codec));
1484
wu@webrtc.org05e7b442014-04-01 17:44:24 +00001485 bool nack_enabled = nack_enabled_;
buildbot@webrtc.org3ffa1f92014-07-02 19:51:26 +00001486 bool enable_codec_fec = false;
Minyue Li7100dcd2015-03-27 05:05:59 +01001487 bool enable_opus_dtx = false;
minyue@webrtc.org26236952014-10-29 02:27:08 +00001488 int opus_max_playback_rate = 0;
buildbot@webrtc.org5d639b32014-09-10 07:57:12 +00001489
henrike@webrtc.org704bf9e2014-02-27 17:52:04 +00001490 // Set send codec (the first non-telephone-event/CN codec)
Fredrik Solenbergaf9fb212015-08-26 10:45:53 +02001491 for (const AudioCodec& codec : codecs) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001492 // Ignore codecs we don't know about. The negotiation step should prevent
1493 // this, but double-check to be sure.
1494 webrtc::CodecInst voe_codec;
solenberg26c8c912015-11-27 04:00:25 -08001495 if (!WebRtcVoiceEngine::ToCodecInst(codec, &voe_codec)) {
Fredrik Solenbergaf9fb212015-08-26 10:45:53 +02001496 LOG(LS_WARNING) << "Unknown codec " << ToString(codec);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001497 continue;
1498 }
1499
Fredrik Solenbergaf9fb212015-08-26 10:45:53 +02001500 if (IsCodec(codec, kDtmfCodecName) || IsCodec(codec, kCnCodecName)) {
henrike@webrtc.org704bf9e2014-02-27 17:52:04 +00001501 // Skip telephone-event/CN codec, which will be handled later.
1502 continue;
1503 }
1504
henrike@webrtc.org704bf9e2014-02-27 17:52:04 +00001505 // We'll use the first codec in the list to actually send audio data.
1506 // Be sure to use the payload type requested by the remote side.
buildbot@webrtc.orgae740dd2014-06-17 10:56:41 +00001507 // "red", for RED audio, is a special case where the actual codec to be
henrike@webrtc.org704bf9e2014-02-27 17:52:04 +00001508 // used is specified in params.
Fredrik Solenbergaf9fb212015-08-26 10:45:53 +02001509 if (IsCodec(codec, kRedCodecName)) {
henrike@webrtc.org704bf9e2014-02-27 17:52:04 +00001510 // Parse out the RED parameters. If we fail, just ignore RED;
1511 // we don't support all possible params/usage scenarios.
Fredrik Solenbergaf9fb212015-08-26 10:45:53 +02001512 if (!GetRedSendCodec(codec, codecs, &send_codec)) {
henrike@webrtc.org704bf9e2014-02-27 17:52:04 +00001513 continue;
1514 }
1515
1516 // Enable redundant encoding of the specified codec. Treat any
1517 // failure as a fatal internal error.
buildbot@webrtc.orgae740dd2014-06-17 10:56:41 +00001518 LOG(LS_INFO) << "Enabling RED on channel " << channel;
Fredrik Solenbergaf9fb212015-08-26 10:45:53 +02001519 if (engine()->voe()->rtp()->SetREDStatus(channel, true, codec.id) == -1) {
1520 LOG_RTCERR3(SetREDStatus, channel, true, codec.id);
henrike@webrtc.org704bf9e2014-02-27 17:52:04 +00001521 return false;
1522 }
1523 } else {
1524 send_codec = voe_codec;
Fredrik Solenbergaf9fb212015-08-26 10:45:53 +02001525 nack_enabled = IsNackEnabled(codec);
Minyue Li7100dcd2015-03-27 05:05:59 +01001526 // For Opus as the send codec, we are to determine inband FEC, maximum
1527 // playback rate, and opus internal dtx.
Fredrik Solenbergaf9fb212015-08-26 10:45:53 +02001528 if (IsCodec(codec, kOpusCodecName)) {
1529 GetOpusConfig(codec, &send_codec, &enable_codec_fec,
Minyue Li7100dcd2015-03-27 05:05:59 +01001530 &opus_max_playback_rate, &enable_opus_dtx);
buildbot@webrtc.org5d639b32014-09-10 07:57:12 +00001531 }
Brave Yao5225dd82015-03-26 07:39:19 +08001532
1533 // Set packet size if the AudioCodec param kCodecParamPTime is set.
1534 int ptime_ms = 0;
Fredrik Solenbergaf9fb212015-08-26 10:45:53 +02001535 if (codec.GetParam(kCodecParamPTime, &ptime_ms)) {
solenberg26c8c912015-11-27 04:00:25 -08001536 if (!WebRtcVoiceCodecs::SetPTimeAsPacketSize(&send_codec, ptime_ms)) {
Brave Yao5225dd82015-03-26 07:39:19 +08001537 LOG(LS_WARNING) << "Failed to set packet size for codec "
1538 << send_codec.plname;
1539 return false;
1540 }
1541 }
henrike@webrtc.org704bf9e2014-02-27 17:52:04 +00001542 }
1543 found_send_codec = true;
1544 break;
1545 }
1546
wu@webrtc.org05e7b442014-04-01 17:44:24 +00001547 if (nack_enabled_ != nack_enabled) {
1548 SetNack(channel, nack_enabled);
1549 nack_enabled_ = nack_enabled;
1550 }
1551
henrike@webrtc.org704bf9e2014-02-27 17:52:04 +00001552 if (!found_send_codec) {
1553 LOG(LS_WARNING) << "Received empty list of codecs.";
1554 return false;
1555 }
1556
1557 // Set the codec immediately, since SetVADStatus() depends on whether
1558 // the current codec is mono or stereo.
1559 if (!SetSendCodec(channel, send_codec))
1560 return false;
1561
buildbot@webrtc.org3ffa1f92014-07-02 19:51:26 +00001562 // FEC should be enabled after SetSendCodec.
1563 if (enable_codec_fec) {
1564 LOG(LS_INFO) << "Attempt to enable codec internal FEC on channel "
1565 << channel;
buildbot@webrtc.org3ffa1f92014-07-02 19:51:26 +00001566 if (engine()->voe()->codec()->SetFECStatus(channel, true) == -1) {
1567 // Enable codec internal FEC. Treat any failure as fatal internal error.
1568 LOG_RTCERR2(SetFECStatus, channel, true);
1569 return false;
1570 }
buildbot@webrtc.org3ffa1f92014-07-02 19:51:26 +00001571 }
1572
Minyue Li7100dcd2015-03-27 05:05:59 +01001573 if (IsCodec(send_codec, kOpusCodecName)) {
1574 // DTX and maxplaybackrate should be set after SetSendCodec. Because current
1575 // send codec has to be Opus.
1576
1577 // Set Opus internal DTX.
1578 LOG(LS_INFO) << "Attempt to "
solenbergbd138382015-11-20 16:08:07 -08001579 << (enable_opus_dtx ? "enable" : "disable")
Minyue Li7100dcd2015-03-27 05:05:59 +01001580 << " Opus DTX on channel "
buildbot@webrtc.org5d639b32014-09-10 07:57:12 +00001581 << channel;
Minyue Li7100dcd2015-03-27 05:05:59 +01001582 if (engine()->voe()->codec()->SetOpusDtx(channel, enable_opus_dtx)) {
1583 LOG_RTCERR2(SetOpusDtx, channel, enable_opus_dtx);
1584 return false;
1585 }
1586
1587 // If opus_max_playback_rate <= 0, the default maximum playback rate
1588 // (48 kHz) will be used.
1589 if (opus_max_playback_rate > 0) {
1590 LOG(LS_INFO) << "Attempt to set maximum playback rate to "
1591 << opus_max_playback_rate
1592 << " Hz on channel "
1593 << channel;
1594 if (engine()->voe()->codec()->SetOpusMaxPlaybackRate(
1595 channel, opus_max_playback_rate) == -1) {
1596 LOG_RTCERR2(SetOpusMaxPlaybackRate, channel, opus_max_playback_rate);
1597 return false;
1598 }
buildbot@webrtc.org5d639b32014-09-10 07:57:12 +00001599 }
buildbot@webrtc.org5d639b32014-09-10 07:57:12 +00001600 }
1601
henrike@webrtc.org704bf9e2014-02-27 17:52:04 +00001602 // Always update the |send_codec_| to the currently set send codec.
1603 send_codec_.reset(new webrtc::CodecInst(send_codec));
1604
minyue@webrtc.org26236952014-10-29 02:27:08 +00001605 if (send_bitrate_setting_) {
1606 SetSendBitrateInternal(send_bitrate_bps_);
henrike@webrtc.org704bf9e2014-02-27 17:52:04 +00001607 }
1608
Fredrik Solenbergb5727682015-12-04 15:22:19 +01001609 // Loop through the codecs list again to config the CN codec.
Fredrik Solenbergaf9fb212015-08-26 10:45:53 +02001610 for (const AudioCodec& codec : codecs) {
henrike@webrtc.org704bf9e2014-02-27 17:52:04 +00001611 // Ignore codecs we don't know about. The negotiation step should prevent
1612 // this, but double-check to be sure.
1613 webrtc::CodecInst voe_codec;
solenberg26c8c912015-11-27 04:00:25 -08001614 if (!WebRtcVoiceEngine::ToCodecInst(codec, &voe_codec)) {
Fredrik Solenbergaf9fb212015-08-26 10:45:53 +02001615 LOG(LS_WARNING) << "Unknown codec " << ToString(codec);
henrike@webrtc.org704bf9e2014-02-27 17:52:04 +00001616 continue;
1617 }
1618
Fredrik Solenbergb5727682015-12-04 15:22:19 +01001619 if (IsCodec(codec, kCnCodecName)) {
henrike@webrtc.org704bf9e2014-02-27 17:52:04 +00001620 // Turn voice activity detection/comfort noise on if supported.
1621 // Set the wideband CN payload type appropriately.
1622 // (narrowband always uses the static payload type 13).
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001623 webrtc::PayloadFrequencies cn_freq;
Fredrik Solenbergaf9fb212015-08-26 10:45:53 +02001624 switch (codec.clockrate) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001625 case 8000:
1626 cn_freq = webrtc::kFreq8000Hz;
1627 break;
1628 case 16000:
1629 cn_freq = webrtc::kFreq16000Hz;
1630 break;
1631 case 32000:
1632 cn_freq = webrtc::kFreq32000Hz;
1633 break;
1634 default:
Fredrik Solenbergaf9fb212015-08-26 10:45:53 +02001635 LOG(LS_WARNING) << "CN frequency " << codec.clockrate
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001636 << " not supported.";
1637 continue;
1638 }
wu@webrtc.orgcadf9042013-08-30 21:24:16 +00001639 // Set the CN payloadtype and the VAD status.
1640 // The CN payload type for 8000 Hz clockrate is fixed at 13.
1641 if (cn_freq != webrtc::kFreq8000Hz) {
1642 if (engine()->voe()->codec()->SetSendCNPayloadType(
Fredrik Solenbergaf9fb212015-08-26 10:45:53 +02001643 channel, codec.id, cn_freq) == -1) {
1644 LOG_RTCERR3(SetSendCNPayloadType, channel, codec.id, cn_freq);
wu@webrtc.orgcadf9042013-08-30 21:24:16 +00001645 // TODO(ajm): This failure condition will be removed from VoE.
1646 // Restore the return here when we update to a new enough webrtc.
1647 //
1648 // Not returning false because the SetSendCNPayloadType will fail if
1649 // the channel is already sending.
1650 // This can happen if the remote description is applied twice, for
1651 // example in the case of ROAP on top of JSEP, where both side will
1652 // send the offer.
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001653 }
wu@webrtc.orgcadf9042013-08-30 21:24:16 +00001654 }
wu@webrtc.orgcadf9042013-08-30 21:24:16 +00001655 // Only turn on VAD if we have a CN payload type that matches the
1656 // clockrate for the codec we are going to use.
Fredrik Solenbergaf9fb212015-08-26 10:45:53 +02001657 if (codec.clockrate == send_codec.plfreq && send_codec.channels != 2) {
Minyue Li7100dcd2015-03-27 05:05:59 +01001658 // TODO(minyue): If CN frequency == 48000 Hz is allowed, consider the
1659 // interaction between VAD and Opus FEC.
wu@webrtc.orgcadf9042013-08-30 21:24:16 +00001660 LOG(LS_INFO) << "Enabling VAD";
1661 if (engine()->voe()->codec()->SetVADStatus(channel, true) == -1) {
1662 LOG_RTCERR2(SetVADStatus, channel, true);
1663 return false;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001664 }
1665 }
1666 }
wu@webrtc.org1d1ffc92013-10-16 18:12:02 +00001667 }
wu@webrtc.orgcadf9042013-08-30 21:24:16 +00001668 return true;
1669}
1670
1671bool WebRtcVoiceMediaChannel::SetSendCodecs(
1672 const std::vector<AudioCodec>& codecs) {
solenberg566ef242015-11-06 15:34:49 -08001673 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
Fredrik Solenbergb5727682015-12-04 15:22:19 +01001674 // TODO(solenberg): Validate input - that payload types don't overlap, are
1675 // within range, filter out codecs we don't support,
1676 // redundant codecs etc.
solenbergd97ec302015-10-07 01:40:33 -07001677
Fredrik Solenbergb5727682015-12-04 15:22:19 +01001678 // Find the DTMF telephone event "codec" payload type.
1679 dtmf_payload_type_ = rtc::Optional<int>();
Fredrik Solenbergaf9fb212015-08-26 10:45:53 +02001680 for (const AudioCodec& codec : codecs) {
Fredrik Solenbergaf9fb212015-08-26 10:45:53 +02001681 if (IsCodec(codec, kDtmfCodecName)) {
Fredrik Solenbergb5727682015-12-04 15:22:19 +01001682 dtmf_payload_type_ = rtc::Optional<int>(codec.id);
1683 break;
wu@webrtc.orgcadf9042013-08-30 21:24:16 +00001684 }
1685 }
1686
1687 // Cache the codecs in order to configure the channel created later.
1688 send_codecs_ = codecs;
solenbergc96df772015-10-21 13:01:53 -07001689 for (const auto& ch : send_streams_) {
Fredrik Solenbergaf9fb212015-08-26 10:45:53 +02001690 if (!SetSendCodecs(ch.second->channel(), codecs)) {
wu@webrtc.orgcadf9042013-08-30 21:24:16 +00001691 return false;
1692 }
1693 }
1694
wu@webrtc.org05e7b442014-04-01 17:44:24 +00001695 // Set nack status on receive channels and update |nack_enabled_|.
solenberg7add0582015-11-20 09:59:34 -08001696 for (const auto& ch : recv_streams_) {
solenberg0a617e22015-10-20 15:49:38 -07001697 SetNack(ch.second->channel(), nack_enabled_);
wu@webrtc.org9dba5252013-08-05 20:36:57 +00001698 }
solenberg0a617e22015-10-20 15:49:38 -07001699
1700 return true;
wu@webrtc.org9dba5252013-08-05 20:36:57 +00001701}
1702
wu@webrtc.orgcadf9042013-08-30 21:24:16 +00001703void WebRtcVoiceMediaChannel::SetNack(int channel, bool nack_enabled) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001704 if (nack_enabled) {
wu@webrtc.orgcadf9042013-08-30 21:24:16 +00001705 LOG(LS_INFO) << "Enabling NACK for channel " << channel;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001706 engine()->voe()->rtp()->SetNACKStatus(channel, true, kNackMaxPackets);
1707 } else {
wu@webrtc.orgcadf9042013-08-30 21:24:16 +00001708 LOG(LS_INFO) << "Disabling NACK for channel " << channel;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001709 engine()->voe()->rtp()->SetNACKStatus(channel, false, 0);
1710 }
1711}
1712
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001713bool WebRtcVoiceMediaChannel::SetSendCodec(
wu@webrtc.org9dba5252013-08-05 20:36:57 +00001714 int channel, const webrtc::CodecInst& send_codec) {
1715 LOG(LS_INFO) << "Send channel " << channel << " selected voice codec "
1716 << ToString(send_codec) << ", bitrate=" << send_codec.rate;
1717
wu@webrtc.org05e7b442014-04-01 17:44:24 +00001718 webrtc::CodecInst current_codec;
1719 if (engine()->voe()->codec()->GetSendCodec(channel, current_codec) == 0 &&
1720 (send_codec == current_codec)) {
1721 // Codec is already configured, we can return without setting it again.
1722 return true;
1723 }
1724
wu@webrtc.org9dba5252013-08-05 20:36:57 +00001725 if (engine()->voe()->codec()->SetSendCodec(channel, send_codec) == -1) {
1726 LOG_RTCERR2(SetSendCodec, channel, ToString(send_codec));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001727 return false;
1728 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001729 return true;
1730}
1731
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001732bool WebRtcVoiceMediaChannel::SetPlayout(bool playout) {
1733 desired_playout_ = playout;
1734 return ChangePlayout(desired_playout_);
1735}
1736
1737bool WebRtcVoiceMediaChannel::PausePlayout() {
1738 return ChangePlayout(false);
1739}
1740
1741bool WebRtcVoiceMediaChannel::ResumePlayout() {
1742 return ChangePlayout(desired_playout_);
1743}
1744
1745bool WebRtcVoiceMediaChannel::ChangePlayout(bool playout) {
solenberg566ef242015-11-06 15:34:49 -08001746 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001747 if (playout_ == playout) {
1748 return true;
1749 }
1750
solenberg7add0582015-11-20 09:59:34 -08001751 for (const auto& ch : recv_streams_) {
Fredrik Solenbergaf9fb212015-08-26 10:45:53 +02001752 if (!SetPlayout(ch.second->channel(), playout)) {
henrike@webrtc.org1e09a712013-07-26 19:17:59 +00001753 LOG(LS_ERROR) << "SetPlayout " << playout << " on channel "
Fredrik Solenbergaf9fb212015-08-26 10:45:53 +02001754 << ch.second->channel() << " failed";
solenberg1ac56142015-10-13 03:58:19 -07001755 return false;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001756 }
1757 }
solenberg1ac56142015-10-13 03:58:19 -07001758 playout_ = playout;
1759 return true;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001760}
1761
1762bool WebRtcVoiceMediaChannel::SetSend(SendFlags send) {
1763 desired_send_ = send;
solenbergc96df772015-10-21 13:01:53 -07001764 if (!send_streams_.empty()) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001765 return ChangeSend(desired_send_);
solenbergc96df772015-10-21 13:01:53 -07001766 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001767 return true;
1768}
1769
1770bool WebRtcVoiceMediaChannel::PauseSend() {
1771 return ChangeSend(SEND_NOTHING);
1772}
1773
1774bool WebRtcVoiceMediaChannel::ResumeSend() {
1775 return ChangeSend(desired_send_);
1776}
1777
1778bool WebRtcVoiceMediaChannel::ChangeSend(SendFlags send) {
1779 if (send_ == send) {
1780 return true;
1781 }
1782
solenberg246b8172015-12-08 09:50:23 -08001783 // Apply channel specific options when channel is enabled for sending.
solenberg63b34542015-09-29 06:06:31 -07001784 if (send == SEND_MICROPHONE) {
1785 engine()->ApplyOptions(options_);
1786 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001787
wu@webrtc.org9dba5252013-08-05 20:36:57 +00001788 // Change the settings on each send channel.
solenbergc96df772015-10-21 13:01:53 -07001789 for (const auto& ch : send_streams_) {
solenberg63b34542015-09-29 06:06:31 -07001790 if (!ChangeSend(ch.second->channel(), send)) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001791 return false;
solenberg63b34542015-09-29 06:06:31 -07001792 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001793 }
wu@webrtc.org9dba5252013-08-05 20:36:57 +00001794
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001795 send_ = send;
1796 return true;
1797}
1798
wu@webrtc.org9dba5252013-08-05 20:36:57 +00001799bool WebRtcVoiceMediaChannel::ChangeSend(int channel, SendFlags send) {
1800 if (send == SEND_MICROPHONE) {
1801 if (engine()->voe()->base()->StartSend(channel) == -1) {
1802 LOG_RTCERR1(StartSend, channel);
1803 return false;
1804 }
wu@webrtc.org9dba5252013-08-05 20:36:57 +00001805 } else { // SEND_NOTHING
henrikg91d6ede2015-09-17 00:24:34 -07001806 RTC_DCHECK(send == SEND_NOTHING);
wu@webrtc.org9dba5252013-08-05 20:36:57 +00001807 if (engine()->voe()->base()->StopSend(channel) == -1) {
1808 LOG_RTCERR1(StopSend, channel);
1809 return false;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001810 }
1811 }
1812
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001813 return true;
1814}
1815
Peter Boström0c4e06b2015-10-07 12:23:21 +02001816bool WebRtcVoiceMediaChannel::SetAudioSend(uint32_t ssrc,
1817 bool enable,
solenberg1dd98f32015-09-10 01:57:14 -07001818 const AudioOptions* options,
1819 AudioRenderer* renderer) {
solenberg566ef242015-11-06 15:34:49 -08001820 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
solenberg1dd98f32015-09-10 01:57:14 -07001821 // TODO(solenberg): The state change should be fully rolled back if any one of
1822 // these calls fail.
1823 if (!SetLocalRenderer(ssrc, renderer)) {
1824 return false;
1825 }
solenbergdfc8f4f2015-10-01 02:31:10 -07001826 if (!MuteStream(ssrc, !enable)) {
solenberg1dd98f32015-09-10 01:57:14 -07001827 return false;
1828 }
solenbergdfc8f4f2015-10-01 02:31:10 -07001829 if (enable && options) {
solenberg1dd98f32015-09-10 01:57:14 -07001830 return SetOptions(*options);
1831 }
1832 return true;
1833}
1834
solenberg0a617e22015-10-20 15:49:38 -07001835int WebRtcVoiceMediaChannel::CreateVoEChannel() {
1836 int id = engine()->CreateVoEChannel();
1837 if (id == -1) {
1838 LOG_RTCERR0(CreateVoEChannel);
1839 return -1;
wu@webrtc.org9dba5252013-08-05 20:36:57 +00001840 }
solenberg0a617e22015-10-20 15:49:38 -07001841 if (engine()->voe()->network()->RegisterExternalTransport(id, *this) == -1) {
1842 LOG_RTCERR2(RegisterExternalTransport, id, this);
1843 engine()->voe()->base()->DeleteChannel(id);
1844 return -1;
1845 }
1846 return id;
wu@webrtc.org9dba5252013-08-05 20:36:57 +00001847}
1848
solenberg7add0582015-11-20 09:59:34 -08001849bool WebRtcVoiceMediaChannel::DeleteVoEChannel(int channel) {
wu@webrtc.org9dba5252013-08-05 20:36:57 +00001850 if (engine()->voe()->network()->DeRegisterExternalTransport(channel) == -1) {
1851 LOG_RTCERR1(DeRegisterExternalTransport, channel);
1852 }
wu@webrtc.org9dba5252013-08-05 20:36:57 +00001853 if (engine()->voe()->base()->DeleteChannel(channel) == -1) {
1854 LOG_RTCERR1(DeleteChannel, channel);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001855 return false;
1856 }
wu@webrtc.org9dba5252013-08-05 20:36:57 +00001857 return true;
1858}
henrike@webrtc.org1e09a712013-07-26 19:17:59 +00001859
wu@webrtc.org9dba5252013-08-05 20:36:57 +00001860bool WebRtcVoiceMediaChannel::AddSendStream(const StreamParams& sp) {
solenberg566ef242015-11-06 15:34:49 -08001861 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
solenberg0a617e22015-10-20 15:49:38 -07001862 LOG(LS_INFO) << "AddSendStream: " << sp.ToString();
1863
1864 uint32_t ssrc = sp.first_ssrc();
1865 RTC_DCHECK(0 != ssrc);
1866
1867 if (GetSendChannelId(ssrc) != -1) {
1868 LOG(LS_ERROR) << "Stream already exists with ssrc " << ssrc;
wu@webrtc.org9dba5252013-08-05 20:36:57 +00001869 return false;
1870 }
1871
solenberg0a617e22015-10-20 15:49:38 -07001872 // Create a new channel for sending audio data.
1873 int channel = CreateVoEChannel();
1874 if (channel == -1) {
1875 return false;
wu@webrtc.org9dba5252013-08-05 20:36:57 +00001876 }
wu@webrtc.org9dba5252013-08-05 20:36:57 +00001877
solenbergc96df772015-10-21 13:01:53 -07001878 // Save the channel to send_streams_, so that RemoveSendStream() can still
wu@webrtc.org9dba5252013-08-05 20:36:57 +00001879 // delete the channel in case failure happens below.
mallinath@webrtc.org67ee6b92014-02-03 16:57:16 +00001880 webrtc::AudioTransport* audio_transport =
1881 engine()->voe()->base()->audio_transport();
solenberg3a941542015-11-16 07:34:50 -08001882 send_streams_.insert(std::make_pair(ssrc, new WebRtcAudioSendStream(
1883 channel, audio_transport, ssrc, sp.cname, send_rtp_extensions_, call_)));
wu@webrtc.org9dba5252013-08-05 20:36:57 +00001884
solenberg0a617e22015-10-20 15:49:38 -07001885 // Set the current codecs to be used for the new channel. We need to do this
1886 // after adding the channel to send_channels_, because of how max bitrate is
1887 // currently being configured by SetSendCodec().
1888 if (!send_codecs_.empty() && !SetSendCodecs(channel, send_codecs_)) {
1889 RemoveSendStream(ssrc);
wu@webrtc.org9dba5252013-08-05 20:36:57 +00001890 return false;
1891 }
1892
1893 // At this point the channel's local SSRC has been updated. If the channel is
solenberg0a617e22015-10-20 15:49:38 -07001894 // the first send channel make sure that all the receive channels are updated
1895 // with the same SSRC in order to send receiver reports.
solenbergc96df772015-10-21 13:01:53 -07001896 if (send_streams_.size() == 1) {
solenberg0a617e22015-10-20 15:49:38 -07001897 receiver_reports_ssrc_ = ssrc;
solenberg7add0582015-11-20 09:59:34 -08001898 for (const auto& stream : recv_streams_) {
1899 int recv_channel = stream.second->channel();
solenberg0a617e22015-10-20 15:49:38 -07001900 if (engine()->voe()->rtp()->SetLocalSSRC(recv_channel, ssrc) != 0) {
solenberg7add0582015-11-20 09:59:34 -08001901 LOG_RTCERR2(SetLocalSSRC, recv_channel, ssrc);
solenberg1ac56142015-10-13 03:58:19 -07001902 return false;
wu@webrtc.org9dba5252013-08-05 20:36:57 +00001903 }
solenberg0a617e22015-10-20 15:49:38 -07001904 engine()->voe()->base()->AssociateSendChannel(recv_channel, channel);
1905 LOG(LS_INFO) << "VoiceEngine channel #" << recv_channel
1906 << " is associated with channel #" << channel << ".";
wu@webrtc.org9dba5252013-08-05 20:36:57 +00001907 }
1908 }
1909
wu@webrtc.org9dba5252013-08-05 20:36:57 +00001910 return ChangeSend(channel, desired_send_);
1911}
1912
Peter Boström0c4e06b2015-10-07 12:23:21 +02001913bool WebRtcVoiceMediaChannel::RemoveSendStream(uint32_t ssrc) {
solenberg566ef242015-11-06 15:34:49 -08001914 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
solenberg3a941542015-11-16 07:34:50 -08001915 LOG(LS_INFO) << "RemoveSendStream: " << ssrc;
1916
solenbergc96df772015-10-21 13:01:53 -07001917 auto it = send_streams_.find(ssrc);
1918 if (it == send_streams_.end()) {
wu@webrtc.org9dba5252013-08-05 20:36:57 +00001919 LOG(LS_WARNING) << "Try to remove stream with ssrc " << ssrc
1920 << " which doesn't exist.";
1921 return false;
1922 }
1923
mallinath@webrtc.org67ee6b92014-02-03 16:57:16 +00001924 int channel = it->second->channel();
wu@webrtc.org9dba5252013-08-05 20:36:57 +00001925 ChangeSend(channel, SEND_NOTHING);
1926
solenberg7add0582015-11-20 09:59:34 -08001927 // Clean up and delete the send stream+channel.
solenberg0a617e22015-10-20 15:49:38 -07001928 LOG(LS_INFO) << "Removing audio send stream " << ssrc
1929 << " with VoiceEngine channel #" << channel << ".";
solenberg7add0582015-11-20 09:59:34 -08001930 delete it->second;
1931 send_streams_.erase(it);
1932 if (!DeleteVoEChannel(channel)) {
solenberg0a617e22015-10-20 15:49:38 -07001933 return false;
wu@webrtc.org9dba5252013-08-05 20:36:57 +00001934 }
solenbergc96df772015-10-21 13:01:53 -07001935 if (send_streams_.empty()) {
wu@webrtc.org9dba5252013-08-05 20:36:57 +00001936 ChangeSend(SEND_NOTHING);
solenberg0a617e22015-10-20 15:49:38 -07001937 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001938 return true;
1939}
1940
1941bool WebRtcVoiceMediaChannel::AddRecvStream(const StreamParams& sp) {
solenberg566ef242015-11-06 15:34:49 -08001942 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
solenbergd97ec302015-10-07 01:40:33 -07001943 LOG(LS_INFO) << "AddRecvStream: " << sp.ToString();
1944
solenberg0b675462015-10-09 01:37:09 -07001945 if (!ValidateStreamParams(sp)) {
wu@webrtc.org78187522013-10-07 23:32:02 +00001946 return false;
1947 }
1948
solenberg7add0582015-11-20 09:59:34 -08001949 const uint32_t ssrc = sp.first_ssrc();
solenberg0b675462015-10-09 01:37:09 -07001950 if (ssrc == 0) {
1951 LOG(LS_WARNING) << "AddRecvStream with ssrc==0 is not supported.";
1952 return false;
1953 }
1954
solenberg1ac56142015-10-13 03:58:19 -07001955 // Remove the default receive stream if one had been created with this ssrc;
1956 // we'll recreate it then.
1957 if (IsDefaultRecvStream(ssrc)) {
1958 RemoveRecvStream(ssrc);
1959 }
solenberg0b675462015-10-09 01:37:09 -07001960
solenberg7add0582015-11-20 09:59:34 -08001961 if (GetReceiveChannelId(ssrc) != -1) {
henrike@webrtc.org1e09a712013-07-26 19:17:59 +00001962 LOG(LS_ERROR) << "Stream already exists with ssrc " << ssrc;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001963 return false;
1964 }
Fredrik Solenberg4b60c732015-05-07 14:07:48 +02001965
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001966 // Create a new channel for receiving audio data.
solenberg7add0582015-11-20 09:59:34 -08001967 const int channel = CreateVoEChannel();
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001968 if (channel == -1) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001969 return false;
1970 }
Minyue2013aec2015-05-13 14:14:42 +02001971
solenberg1ac56142015-10-13 03:58:19 -07001972 // Turn off all supported codecs.
solenberg26c8c912015-11-27 04:00:25 -08001973 // TODO(solenberg): Remove once "no codecs" is the default state of a stream.
1974 for (webrtc::CodecInst voe_codec : webrtc::acm2::RentACodec::Database()) {
1975 voe_codec.pltype = -1;
1976 if (engine()->voe()->codec()->SetRecPayloadType(channel, voe_codec) == -1) {
1977 LOG_RTCERR2(SetRecPayloadType, channel, ToString(voe_codec));
1978 DeleteVoEChannel(channel);
1979 return false;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001980 }
1981 }
1982
solenberg1ac56142015-10-13 03:58:19 -07001983 // Only enable those configured for this channel.
1984 for (const auto& codec : recv_codecs_) {
1985 webrtc::CodecInst voe_codec;
solenberg26c8c912015-11-27 04:00:25 -08001986 if (WebRtcVoiceEngine::ToCodecInst(codec, &voe_codec)) {
solenberg1ac56142015-10-13 03:58:19 -07001987 voe_codec.pltype = codec.id;
1988 if (engine()->voe()->codec()->SetRecPayloadType(
1989 channel, voe_codec) == -1) {
1990 LOG_RTCERR2(SetRecPayloadType, channel, ToString(voe_codec));
solenberg7add0582015-11-20 09:59:34 -08001991 DeleteVoEChannel(channel);
solenberg1ac56142015-10-13 03:58:19 -07001992 return false;
1993 }
henrike@webrtc.org1e09a712013-07-26 19:17:59 +00001994 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001995 }
solenberg8fb30c32015-10-13 03:06:58 -07001996
solenberg7add0582015-11-20 09:59:34 -08001997 const int send_channel = GetSendChannelId(receiver_reports_ssrc_);
1998 if (send_channel != -1) {
1999 // Associate receive channel with first send channel (so the receive channel
2000 // can obtain RTT from the send channel)
2001 engine()->voe()->base()->AssociateSendChannel(channel, send_channel);
2002 LOG(LS_INFO) << "VoiceEngine channel #" << channel
2003 << " is associated with channel #" << send_channel << ".";
buildbot@webrtc.org150835e2014-05-06 15:54:38 +00002004 }
2005
solenberg7add0582015-11-20 09:59:34 -08002006 recv_streams_.insert(std::make_pair(ssrc, new WebRtcAudioReceiveStream(
2007 channel, ssrc, receiver_reports_ssrc_,
2008 options_.combined_audio_video_bwe.value_or(false), sp.sync_label,
2009 recv_rtp_extensions_, call_)));
2010
2011 SetNack(channel, nack_enabled_);
solenberg1ac56142015-10-13 03:58:19 -07002012 SetPlayout(channel, playout_);
solenberg7add0582015-11-20 09:59:34 -08002013
solenberg1ac56142015-10-13 03:58:19 -07002014 return true;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002015}
2016
Peter Boström0c4e06b2015-10-07 12:23:21 +02002017bool WebRtcVoiceMediaChannel::RemoveRecvStream(uint32_t ssrc) {
solenberg566ef242015-11-06 15:34:49 -08002018 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
solenbergd97ec302015-10-07 01:40:33 -07002019 LOG(LS_INFO) << "RemoveRecvStream: " << ssrc;
2020
solenberg7add0582015-11-20 09:59:34 -08002021 const auto it = recv_streams_.find(ssrc);
2022 if (it == recv_streams_.end()) {
wu@webrtc.org9dba5252013-08-05 20:36:57 +00002023 LOG(LS_WARNING) << "Try to remove stream with ssrc " << ssrc
2024 << " which doesn't exist.";
henrike@webrtc.org1e09a712013-07-26 19:17:59 +00002025 return false;
wu@webrtc.org9dba5252013-08-05 20:36:57 +00002026 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002027
solenberg1ac56142015-10-13 03:58:19 -07002028 // Deregister default channel, if that's the one being destroyed.
2029 if (IsDefaultRecvStream(ssrc)) {
2030 default_recv_ssrc_ = -1;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002031 }
henrike@webrtc.org1e09a712013-07-26 19:17:59 +00002032
solenberg7add0582015-11-20 09:59:34 -08002033 const int channel = it->second->channel();
2034
2035 // Clean up and delete the receive stream+channel.
2036 LOG(LS_INFO) << "Removing audio receive stream " << ssrc
mallinath@webrtc.org67ee6b92014-02-03 16:57:16 +00002037 << " with VoiceEngine channel #" << channel << ".";
Tommif888bb52015-12-12 01:37:01 +01002038 it->second->SetRawAudioSink(nullptr);
solenberg7add0582015-11-20 09:59:34 -08002039 delete it->second;
2040 recv_streams_.erase(it);
2041 return DeleteVoEChannel(channel);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002042}
2043
Peter Boström0c4e06b2015-10-07 12:23:21 +02002044bool WebRtcVoiceMediaChannel::SetLocalRenderer(uint32_t ssrc,
henrike@webrtc.org1e09a712013-07-26 19:17:59 +00002045 AudioRenderer* renderer) {
solenbergc96df772015-10-21 13:01:53 -07002046 auto it = send_streams_.find(ssrc);
2047 if (it == send_streams_.end()) {
wu@webrtc.org9dba5252013-08-05 20:36:57 +00002048 if (renderer) {
2049 // Return an error if trying to set a valid renderer with an invalid ssrc.
2050 LOG(LS_ERROR) << "SetLocalRenderer failed with ssrc "<< ssrc;
2051 return false;
2052 }
2053
2054 // The channel likely has gone away, do nothing.
henrike@webrtc.org1e09a712013-07-26 19:17:59 +00002055 return true;
henrike@webrtc.org1e09a712013-07-26 19:17:59 +00002056 }
2057
solenberg1ac56142015-10-13 03:58:19 -07002058 if (renderer) {
mallinath@webrtc.org67ee6b92014-02-03 16:57:16 +00002059 it->second->Start(renderer);
solenberg1ac56142015-10-13 03:58:19 -07002060 } else {
mallinath@webrtc.org67ee6b92014-02-03 16:57:16 +00002061 it->second->Stop();
solenberg1ac56142015-10-13 03:58:19 -07002062 }
henrike@webrtc.org1e09a712013-07-26 19:17:59 +00002063
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002064 return true;
2065}
2066
2067bool WebRtcVoiceMediaChannel::GetActiveStreams(
2068 AudioInfo::StreamList* actives) {
solenberg566ef242015-11-06 15:34:49 -08002069 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002070 actives->clear();
solenberg7add0582015-11-20 09:59:34 -08002071 for (const auto& ch : recv_streams_) {
Fredrik Solenbergaf9fb212015-08-26 10:45:53 +02002072 int level = GetOutputLevel(ch.second->channel());
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002073 if (level > 0) {
Fredrik Solenbergaf9fb212015-08-26 10:45:53 +02002074 actives->push_back(std::make_pair(ch.first, level));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002075 }
2076 }
2077 return true;
2078}
2079
2080int WebRtcVoiceMediaChannel::GetOutputLevel() {
solenberg566ef242015-11-06 15:34:49 -08002081 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
solenberg1ac56142015-10-13 03:58:19 -07002082 int highest = 0;
solenberg7add0582015-11-20 09:59:34 -08002083 for (const auto& ch : recv_streams_) {
solenberg8fb30c32015-10-13 03:06:58 -07002084 highest = std::max(GetOutputLevel(ch.second->channel()), highest);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002085 }
2086 return highest;
2087}
2088
2089int WebRtcVoiceMediaChannel::GetTimeSinceLastTyping() {
2090 int ret;
2091 if (engine()->voe()->processing()->TimeSinceLastTyping(ret) == -1) {
2092 // In case of error, log the info and continue
2093 LOG_RTCERR0(TimeSinceLastTyping);
2094 ret = -1;
2095 } else {
2096 ret *= 1000; // We return ms, webrtc returns seconds.
2097 }
2098 return ret;
2099}
2100
2101void WebRtcVoiceMediaChannel::SetTypingDetectionParameters(int time_window,
2102 int cost_per_typing, int reporting_threshold, int penalty_decay,
2103 int type_event_delay) {
2104 if (engine()->voe()->processing()->SetTypingDetectionParameters(
2105 time_window, cost_per_typing,
2106 reporting_threshold, penalty_decay, type_event_delay) == -1) {
2107 // In case of error, log the info and continue
2108 LOG_RTCERR5(SetTypingDetectionParameters, time_window,
2109 cost_per_typing, reporting_threshold, penalty_decay,
2110 type_event_delay);
2111 }
2112}
2113
solenberg4bac9c52015-10-09 02:32:53 -07002114bool WebRtcVoiceMediaChannel::SetOutputVolume(uint32_t ssrc, double volume) {
solenberg566ef242015-11-06 15:34:49 -08002115 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
solenberg1ac56142015-10-13 03:58:19 -07002116 if (ssrc == 0) {
2117 default_recv_volume_ = volume;
2118 if (default_recv_ssrc_ == -1) {
2119 return true;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002120 }
solenberg1ac56142015-10-13 03:58:19 -07002121 ssrc = static_cast<uint32_t>(default_recv_ssrc_);
2122 }
2123 int ch_id = GetReceiveChannelId(ssrc);
2124 if (ch_id < 0) {
2125 LOG(LS_WARNING) << "Cannot find channel for ssrc:" << ssrc;
2126 return false;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002127 }
2128
solenberg1ac56142015-10-13 03:58:19 -07002129 if (-1 == engine()->voe()->volume()->SetChannelOutputVolumeScaling(ch_id,
2130 volume)) {
2131 LOG_RTCERR2(SetChannelOutputVolumeScaling, ch_id, volume);
2132 return false;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002133 }
solenberg1ac56142015-10-13 03:58:19 -07002134 LOG(LS_INFO) << "SetOutputVolume to " << volume
2135 << " for channel " << ch_id << " and ssrc " << ssrc;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002136 return true;
2137}
2138
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002139bool WebRtcVoiceMediaChannel::CanInsertDtmf() {
Fredrik Solenbergb5727682015-12-04 15:22:19 +01002140 return dtmf_payload_type_ ? true : false;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002141}
2142
solenberg1d63dd02015-12-02 12:35:09 -08002143bool WebRtcVoiceMediaChannel::InsertDtmf(uint32_t ssrc, int event,
2144 int duration) {
solenberg566ef242015-11-06 15:34:49 -08002145 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
Fredrik Solenbergb5727682015-12-04 15:22:19 +01002146 LOG(LS_INFO) << "WebRtcVoiceMediaChannel::InsertDtmf";
2147 if (!dtmf_payload_type_) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002148 return false;
2149 }
2150
Fredrik Solenbergb5727682015-12-04 15:22:19 +01002151 // Figure out which WebRtcAudioSendStream to send the event on.
2152 auto it = ssrc != 0 ? send_streams_.find(ssrc) : send_streams_.begin();
2153 if (it == send_streams_.end()) {
2154 LOG(LS_WARNING) << "The specified ssrc " << ssrc << " is not in use.";
solenberg1d63dd02015-12-02 12:35:09 -08002155 return false;
2156 }
Fredrik Solenbergb5727682015-12-04 15:22:19 +01002157 if (event < kMinTelephoneEventCode ||
2158 event > kMaxTelephoneEventCode) {
2159 LOG(LS_WARNING) << "DTMF event code " << event << " out of range.";
solenberg1d63dd02015-12-02 12:35:09 -08002160 return false;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002161 }
Fredrik Solenbergb5727682015-12-04 15:22:19 +01002162 if (duration < kMinTelephoneEventDuration ||
2163 duration > kMaxTelephoneEventDuration) {
2164 LOG(LS_WARNING) << "DTMF event duration " << duration << " out of range.";
2165 return false;
2166 }
2167 return it->second->SendTelephoneEvent(*dtmf_payload_type_, event, duration);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002168}
2169
wu@webrtc.orga9890802013-12-13 00:21:03 +00002170void WebRtcVoiceMediaChannel::OnPacketReceived(
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00002171 rtc::Buffer* packet, const rtc::PacketTime& packet_time) {
solenberg566ef242015-11-06 15:34:49 -08002172 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
Fredrik Solenberg4b60c732015-05-07 14:07:48 +02002173
solenberg1ac56142015-10-13 03:58:19 -07002174 uint32_t ssrc = 0;
2175 if (!GetRtpSsrc(packet->data(), packet->size(), &ssrc)) {
2176 return;
2177 }
2178
solenberg7e63ef02015-11-20 00:19:43 -08002179 // If we don't have a default channel, and the SSRC is unknown, create a
2180 // default channel.
2181 if (default_recv_ssrc_ == -1 && GetReceiveChannelId(ssrc) == -1) {
solenberg1ac56142015-10-13 03:58:19 -07002182 StreamParams sp;
2183 sp.ssrcs.push_back(ssrc);
2184 LOG(LS_INFO) << "Creating default receive stream for SSRC=" << ssrc << ".";
2185 if (!AddRecvStream(sp)) {
2186 LOG(LS_WARNING) << "Could not create default receive stream.";
2187 return;
2188 }
2189 default_recv_ssrc_ = ssrc;
2190 SetOutputVolume(default_recv_ssrc_, default_recv_volume_);
2191 }
2192
2193 // Forward packet to Call. If the SSRC is unknown we'll return after this.
Fredrik Solenberg709ed672015-09-15 12:26:33 +02002194 const webrtc::PacketTime webrtc_packet_time(packet_time.timestamp,
2195 packet_time.not_before);
solenberg1ac56142015-10-13 03:58:19 -07002196 webrtc::PacketReceiver::DeliveryStatus delivery_result =
2197 call_->Receiver()->DeliverPacket(webrtc::MediaType::AUDIO,
2198 reinterpret_cast<const uint8_t*>(packet->data()), packet->size(),
2199 webrtc_packet_time);
2200 if (webrtc::PacketReceiver::DELIVERY_OK != delivery_result) {
solenberg7e63ef02015-11-20 00:19:43 -08002201 // If the SSRC is unknown here, route it to the default channel, if we have
2202 // one. See: https://bugs.chromium.org/p/webrtc/issues/detail?id=5208
2203 if (default_recv_ssrc_ == -1) {
2204 return;
2205 } else {
2206 ssrc = default_recv_ssrc_;
2207 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002208 }
2209
solenberg1ac56142015-10-13 03:58:19 -07002210 // Find the channel to send this packet to. It must exist since webrtc::Call
2211 // was able to demux the packet.
2212 int channel = GetReceiveChannelId(ssrc);
2213 RTC_DCHECK(channel != -1);
2214
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002215 // Pass it off to the decoder.
henrike@webrtc.org28654cb2013-07-22 21:07:49 +00002216 engine()->voe()->network()->ReceivedRTPPacket(
solenberg1ac56142015-10-13 03:58:19 -07002217 channel, packet->data(), packet->size(), webrtc_packet_time);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002218}
2219
wu@webrtc.orga9890802013-12-13 00:21:03 +00002220void WebRtcVoiceMediaChannel::OnRtcpReceived(
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00002221 rtc::Buffer* packet, const rtc::PacketTime& packet_time) {
solenberg566ef242015-11-06 15:34:49 -08002222 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
Fredrik Solenberg4b60c732015-05-07 14:07:48 +02002223
Fredrik Solenberg709ed672015-09-15 12:26:33 +02002224 // Forward packet to Call as well.
2225 const webrtc::PacketTime webrtc_packet_time(packet_time.timestamp,
2226 packet_time.not_before);
2227 call_->Receiver()->DeliverPacket(webrtc::MediaType::AUDIO,
2228 reinterpret_cast<const uint8_t*>(packet->data()), packet->size(),
2229 webrtc_packet_time);
Fredrik Solenberg4b60c732015-05-07 14:07:48 +02002230
wu@webrtc.org9dba5252013-08-05 20:36:57 +00002231 // Sending channels need all RTCP packets with feedback information.
2232 // Even sender reports can contain attached report blocks.
2233 // Receiving channels need sender reports in order to create
2234 // correct receiver reports.
2235 int type = 0;
kwiberg@webrtc.orgeebcab52015-03-24 09:19:06 +00002236 if (!GetRtcpType(packet->data(), packet->size(), &type)) {
wu@webrtc.org9dba5252013-08-05 20:36:57 +00002237 LOG(LS_WARNING) << "Failed to parse type from received RTCP packet";
2238 return;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002239 }
2240
solenberg0b675462015-10-09 01:37:09 -07002241 // If it is a sender report, find the receive channel that is listening.
wu@webrtc.org9dba5252013-08-05 20:36:57 +00002242 if (type == kRtcpTypeSR) {
solenberg0b675462015-10-09 01:37:09 -07002243 uint32_t ssrc = 0;
2244 if (!GetRtcpSsrc(packet->data(), packet->size(), &ssrc)) {
2245 return;
2246 }
2247 int recv_channel_id = GetReceiveChannelId(ssrc);
2248 if (recv_channel_id != -1) {
wu@webrtc.org9dba5252013-08-05 20:36:57 +00002249 engine()->voe()->network()->ReceivedRTCPPacket(
solenberg0b675462015-10-09 01:37:09 -07002250 recv_channel_id, packet->data(), packet->size());
wu@webrtc.org9dba5252013-08-05 20:36:57 +00002251 }
2252 }
2253
2254 // SR may continue RR and any RR entry may correspond to any one of the send
2255 // channels. So all RTCP packets must be forwarded all send channels. VoE
2256 // will filter out RR internally.
solenbergc96df772015-10-21 13:01:53 -07002257 for (const auto& ch : send_streams_) {
wu@webrtc.org9dba5252013-08-05 20:36:57 +00002258 engine()->voe()->network()->ReceivedRTCPPacket(
Fredrik Solenbergaf9fb212015-08-26 10:45:53 +02002259 ch.second->channel(), packet->data(), packet->size());
wu@webrtc.org9dba5252013-08-05 20:36:57 +00002260 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002261}
2262
Peter Boström0c4e06b2015-10-07 12:23:21 +02002263bool WebRtcVoiceMediaChannel::MuteStream(uint32_t ssrc, bool muted) {
solenberg566ef242015-11-06 15:34:49 -08002264 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
solenberg0a617e22015-10-20 15:49:38 -07002265 int channel = GetSendChannelId(ssrc);
wu@webrtc.org9dba5252013-08-05 20:36:57 +00002266 if (channel == -1) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002267 LOG(LS_WARNING) << "The specified ssrc " << ssrc << " is not in use.";
2268 return false;
2269 }
wu@webrtc.org9dba5252013-08-05 20:36:57 +00002270 if (engine()->voe()->volume()->SetInputMute(channel, muted) == -1) {
2271 LOG_RTCERR2(SetInputMute, channel, muted);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002272 return false;
2273 }
buildbot@webrtc.org6b21b712014-07-31 15:08:53 +00002274 // We set the AGC to mute state only when all the channels are muted.
2275 // This implementation is not ideal, instead we should signal the AGC when
2276 // the mic channel is muted/unmuted. We can't do it today because there
2277 // is no good way to know which stream is mapping to the mic channel.
2278 bool all_muted = muted;
solenbergc96df772015-10-21 13:01:53 -07002279 for (const auto& ch : send_streams_) {
Fredrik Solenbergaf9fb212015-08-26 10:45:53 +02002280 if (!all_muted) {
2281 break;
2282 }
2283 if (engine()->voe()->volume()->GetInputMute(ch.second->channel(),
buildbot@webrtc.org6b21b712014-07-31 15:08:53 +00002284 all_muted)) {
Fredrik Solenbergaf9fb212015-08-26 10:45:53 +02002285 LOG_RTCERR1(GetInputMute, ch.second->channel());
buildbot@webrtc.org6b21b712014-07-31 15:08:53 +00002286 return false;
2287 }
2288 }
2289
2290 webrtc::AudioProcessing* ap = engine()->voe()->base()->audio_processing();
solenberg0a617e22015-10-20 15:49:38 -07002291 if (ap) {
buildbot@webrtc.org6b21b712014-07-31 15:08:53 +00002292 ap->set_output_will_be_muted(all_muted);
solenberg0a617e22015-10-20 15:49:38 -07002293 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002294 return true;
2295}
2296
minyue@webrtc.org26236952014-10-29 02:27:08 +00002297// TODO(minyue): SetMaxSendBandwidth() is subject to be renamed to
2298// SetMaxSendBitrate() in future.
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00002299bool WebRtcVoiceMediaChannel::SetMaxSendBandwidth(int bps) {
minyue@webrtc.org26236952014-10-29 02:27:08 +00002300 LOG(LS_INFO) << "WebRtcVoiceMediaChannel::SetMaxSendBandwidth.";
minyue@webrtc.org26236952014-10-29 02:27:08 +00002301 return SetSendBitrateInternal(bps);
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00002302}
2303
minyue@webrtc.org26236952014-10-29 02:27:08 +00002304bool WebRtcVoiceMediaChannel::SetSendBitrateInternal(int bps) {
2305 LOG(LS_INFO) << "WebRtcVoiceMediaChannel::SetSendBitrateInternal.";
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00002306
minyue@webrtc.org26236952014-10-29 02:27:08 +00002307 send_bitrate_setting_ = true;
2308 send_bitrate_bps_ = bps;
wu@webrtc.org1d1ffc92013-10-16 18:12:02 +00002309
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002310 if (!send_codec_) {
wu@webrtc.org1d1ffc92013-10-16 18:12:02 +00002311 LOG(LS_INFO) << "The send codec has not been set up yet. "
minyue@webrtc.org26236952014-10-29 02:27:08 +00002312 << "The send bitrate setting will be applied later.";
wu@webrtc.org1d1ffc92013-10-16 18:12:02 +00002313 return true;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002314 }
2315
minyue@webrtc.org26236952014-10-29 02:27:08 +00002316 // Bitrate is auto by default.
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00002317 // TODO(bemasc): Fix this so that if SetMaxSendBandwidth(50) is followed by
2318 // SetMaxSendBandwith(0), the second call removes the previous limit.
2319 if (bps <= 0)
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002320 return true;
2321
2322 webrtc::CodecInst codec = *send_codec_;
solenberg26c8c912015-11-27 04:00:25 -08002323 bool is_multi_rate = WebRtcVoiceCodecs::IsCodecMultiRate(codec);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002324
2325 if (is_multi_rate) {
2326 // If codec is multi-rate then just set the bitrate.
2327 codec.rate = bps;
solenbergc96df772015-10-21 13:01:53 -07002328 for (const auto& ch : send_streams_) {
solenberg0a617e22015-10-20 15:49:38 -07002329 if (!SetSendCodec(ch.second->channel(), codec)) {
2330 LOG(LS_INFO) << "Failed to set codec " << codec.plname
2331 << " to bitrate " << bps << " bps.";
2332 return false;
2333 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002334 }
2335 return true;
2336 } else {
2337 // If codec is not multi-rate and |bps| is less than the fixed bitrate
2338 // then fail. If codec is not multi-rate and |bps| exceeds or equal the
2339 // fixed bitrate then ignore.
2340 if (bps < codec.rate) {
2341 LOG(LS_INFO) << "Failed to set codec " << codec.plname
2342 << " to bitrate " << bps << " bps"
2343 << ", requires at least " << codec.rate << " bps.";
2344 return false;
2345 }
2346 return true;
2347 }
2348}
2349
2350bool WebRtcVoiceMediaChannel::GetStats(VoiceMediaInfo* info) {
solenberg566ef242015-11-06 15:34:49 -08002351 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
solenberg85a04962015-10-27 03:35:21 -07002352 RTC_DCHECK(info);
solenbergd97ec302015-10-07 01:40:33 -07002353
solenberg85a04962015-10-27 03:35:21 -07002354 // Get SSRC and stats for each sender.
2355 RTC_DCHECK(info->senders.size() == 0);
2356 for (const auto& stream : send_streams_) {
2357 webrtc::AudioSendStream::Stats stats = stream.second->GetStats();
wu@webrtc.org9dba5252013-08-05 20:36:57 +00002358 VoiceSenderInfo sinfo;
solenberg85a04962015-10-27 03:35:21 -07002359 sinfo.add_ssrc(stats.local_ssrc);
2360 sinfo.bytes_sent = stats.bytes_sent;
2361 sinfo.packets_sent = stats.packets_sent;
2362 sinfo.packets_lost = stats.packets_lost;
2363 sinfo.fraction_lost = stats.fraction_lost;
2364 sinfo.codec_name = stats.codec_name;
2365 sinfo.ext_seqnum = stats.ext_seqnum;
2366 sinfo.jitter_ms = stats.jitter_ms;
2367 sinfo.rtt_ms = stats.rtt_ms;
2368 sinfo.audio_level = stats.audio_level;
2369 sinfo.aec_quality_min = stats.aec_quality_min;
2370 sinfo.echo_delay_median_ms = stats.echo_delay_median_ms;
2371 sinfo.echo_delay_std_ms = stats.echo_delay_std_ms;
2372 sinfo.echo_return_loss = stats.echo_return_loss;
2373 sinfo.echo_return_loss_enhancement = stats.echo_return_loss_enhancement;
solenberg566ef242015-11-06 15:34:49 -08002374 sinfo.typing_noise_detected =
2375 (send_ == SEND_NOTHING ? false : stats.typing_noise_detected);
wu@webrtc.org9dba5252013-08-05 20:36:57 +00002376 info->senders.push_back(sinfo);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002377 }
2378
solenberg85a04962015-10-27 03:35:21 -07002379 // Get SSRC and stats for each receiver.
2380 RTC_DCHECK(info->receivers.size() == 0);
solenberg7add0582015-11-20 09:59:34 -08002381 for (const auto& stream : recv_streams_) {
Fredrik Solenberg4f4ec0a2015-10-22 10:49:27 +02002382 webrtc::AudioReceiveStream::Stats stats = stream.second->GetStats();
2383 VoiceReceiverInfo rinfo;
2384 rinfo.add_ssrc(stats.remote_ssrc);
2385 rinfo.bytes_rcvd = stats.bytes_rcvd;
2386 rinfo.packets_rcvd = stats.packets_rcvd;
2387 rinfo.packets_lost = stats.packets_lost;
2388 rinfo.fraction_lost = stats.fraction_lost;
2389 rinfo.codec_name = stats.codec_name;
2390 rinfo.ext_seqnum = stats.ext_seqnum;
2391 rinfo.jitter_ms = stats.jitter_ms;
2392 rinfo.jitter_buffer_ms = stats.jitter_buffer_ms;
2393 rinfo.jitter_buffer_preferred_ms = stats.jitter_buffer_preferred_ms;
2394 rinfo.delay_estimate_ms = stats.delay_estimate_ms;
2395 rinfo.audio_level = stats.audio_level;
2396 rinfo.expand_rate = stats.expand_rate;
2397 rinfo.speech_expand_rate = stats.speech_expand_rate;
2398 rinfo.secondary_decoded_rate = stats.secondary_decoded_rate;
2399 rinfo.accelerate_rate = stats.accelerate_rate;
2400 rinfo.preemptive_expand_rate = stats.preemptive_expand_rate;
2401 rinfo.decoding_calls_to_silence_generator =
2402 stats.decoding_calls_to_silence_generator;
2403 rinfo.decoding_calls_to_neteq = stats.decoding_calls_to_neteq;
2404 rinfo.decoding_normal = stats.decoding_normal;
2405 rinfo.decoding_plc = stats.decoding_plc;
2406 rinfo.decoding_cng = stats.decoding_cng;
2407 rinfo.decoding_plc_cng = stats.decoding_plc_cng;
2408 rinfo.capture_start_ntp_time_ms = stats.capture_start_ntp_time_ms;
2409 info->receivers.push_back(rinfo);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002410 }
2411
2412 return true;
2413}
2414
Tommif888bb52015-12-12 01:37:01 +01002415void WebRtcVoiceMediaChannel::SetRawAudioSink(
2416 uint32_t ssrc,
2417 rtc::scoped_ptr<webrtc::AudioSinkInterface> sink) {
2418 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
2419 LOG(LS_VERBOSE) << "WebRtcVoiceMediaChannel::SetRawAudioSink";
2420 const auto it = recv_streams_.find(ssrc);
2421 if (it == recv_streams_.end()) {
2422 LOG(LS_WARNING) << "SetRawAudioSink: no recv stream" << ssrc;
2423 return;
2424 }
2425 it->second->SetRawAudioSink(std::move(sink));
2426}
2427
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002428int WebRtcVoiceMediaChannel::GetOutputLevel(int channel) {
solenbergd97ec302015-10-07 01:40:33 -07002429 unsigned int ulevel = 0;
2430 int ret = engine()->voe()->volume()->GetSpeechOutputLevel(channel, ulevel);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002431 return (ret == 0) ? static_cast<int>(ulevel) : -1;
2432}
2433
Peter Boström0c4e06b2015-10-07 12:23:21 +02002434int WebRtcVoiceMediaChannel::GetReceiveChannelId(uint32_t ssrc) const {
solenberg566ef242015-11-06 15:34:49 -08002435 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
solenberg7add0582015-11-20 09:59:34 -08002436 const auto it = recv_streams_.find(ssrc);
2437 if (it != recv_streams_.end()) {
mallinath@webrtc.org67ee6b92014-02-03 16:57:16 +00002438 return it->second->channel();
solenberg8fb30c32015-10-13 03:06:58 -07002439 }
solenberg1ac56142015-10-13 03:58:19 -07002440 return -1;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002441}
2442
Peter Boström0c4e06b2015-10-07 12:23:21 +02002443int WebRtcVoiceMediaChannel::GetSendChannelId(uint32_t ssrc) const {
solenberg566ef242015-11-06 15:34:49 -08002444 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
solenbergc96df772015-10-21 13:01:53 -07002445 const auto it = send_streams_.find(ssrc);
2446 if (it != send_streams_.end()) {
mallinath@webrtc.org67ee6b92014-02-03 16:57:16 +00002447 return it->second->channel();
solenberg8fb30c32015-10-13 03:06:58 -07002448 }
wu@webrtc.org9dba5252013-08-05 20:36:57 +00002449 return -1;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002450}
2451
2452bool WebRtcVoiceMediaChannel::GetRedSendCodec(const AudioCodec& red_codec,
2453 const std::vector<AudioCodec>& all_codecs, webrtc::CodecInst* send_codec) {
2454 // Get the RED encodings from the parameter with no name. This may
2455 // change based on what is discussed on the Jingle list.
2456 // The encoding parameter is of the form "a/b"; we only support where
2457 // a == b. Verify this and parse out the value into red_pt.
2458 // If the parameter value is absent (as it will be until we wire up the
2459 // signaling of this message), use the second codec specified (i.e. the
2460 // one after "red") as the encoding parameter.
2461 int red_pt = -1;
2462 std::string red_params;
2463 CodecParameterMap::const_iterator it = red_codec.params.find("");
2464 if (it != red_codec.params.end()) {
2465 red_params = it->second;
2466 std::vector<std::string> red_pts;
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00002467 if (rtc::split(red_params, '/', &red_pts) != 2 ||
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002468 red_pts[0] != red_pts[1] ||
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00002469 !rtc::FromString(red_pts[0], &red_pt)) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002470 LOG(LS_WARNING) << "RED params " << red_params << " not supported.";
2471 return false;
2472 }
2473 } else if (red_codec.params.empty()) {
2474 LOG(LS_WARNING) << "RED params not present, using defaults";
2475 if (all_codecs.size() > 1) {
2476 red_pt = all_codecs[1].id;
2477 }
2478 }
2479
2480 // Try to find red_pt in |codecs|.
Fredrik Solenbergaf9fb212015-08-26 10:45:53 +02002481 for (const AudioCodec& codec : all_codecs) {
2482 if (codec.id == red_pt) {
2483 // If we find the right codec, that will be the codec we pass to
2484 // SetSendCodec, with the desired payload type.
solenberg26c8c912015-11-27 04:00:25 -08002485 if (WebRtcVoiceEngine::ToCodecInst(codec, send_codec)) {
Fredrik Solenbergaf9fb212015-08-26 10:45:53 +02002486 return true;
2487 } else {
2488 break;
2489 }
2490 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002491 }
Fredrik Solenbergaf9fb212015-08-26 10:45:53 +02002492 LOG(LS_WARNING) << "RED params " << red_params << " are invalid.";
2493 return false;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002494}
2495
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002496bool WebRtcVoiceMediaChannel::SetPlayout(int channel, bool playout) {
2497 if (playout) {
2498 LOG(LS_INFO) << "Starting playout for channel #" << channel;
2499 if (engine()->voe()->base()->StartPlayout(channel) == -1) {
2500 LOG_RTCERR1(StartPlayout, channel);
2501 return false;
2502 }
2503 } else {
2504 LOG(LS_INFO) << "Stopping playout for channel #" << channel;
2505 engine()->voe()->base()->StopPlayout(channel);
2506 }
2507 return true;
2508}
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002509} // namespace cricket
2510
2511#endif // HAVE_WEBRTC_VOICE