blob: 9742564985bca6c0437865dafbe76803608e384a [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;
Peter Kasting69558702016-01-12 16:26:35 -0800413 size_t channels;
solenberg26c8c912015-11-27 04:00:25 -0800414 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
ivoca4df27b2015-12-19 10:14:10 -08001014bool WebRtcVoiceEngine::StartAecDump(rtc::PlatformFile file) {
solenberg566ef242015-11-06 15:34:49 -08001015 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00001016 FILE* aec_dump_file_stream = rtc::FdopenPlatformFileForWriting(file);
wu@webrtc.orga8910d22014-01-23 22:12:45 +00001017 if (!aec_dump_file_stream) {
1018 LOG(LS_ERROR) << "Could not open AEC dump file stream.";
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00001019 if (!rtc::ClosePlatformFile(file))
wu@webrtc.orga8910d22014-01-23 22:12:45 +00001020 LOG(LS_WARNING) << "Could not close file.";
1021 return false;
1022 }
wu@webrtc.orga9890802013-12-13 00:21:03 +00001023 StopAecDump();
ivoca4df27b2015-12-19 10:14:10 -08001024 if (voe_wrapper_->processing()->StartDebugRecording(aec_dump_file_stream) !=
wu@webrtc.orga9890802013-12-13 00:21:03 +00001025 webrtc::AudioProcessing::kNoError) {
wu@webrtc.orga8910d22014-01-23 22:12:45 +00001026 LOG_RTCERR0(StartDebugRecording);
1027 fclose(aec_dump_file_stream);
wu@webrtc.orga9890802013-12-13 00:21:03 +00001028 return false;
1029 }
1030 is_dumping_aec_ = true;
1031 return true;
wu@webrtc.orga9890802013-12-13 00:21:03 +00001032}
1033
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001034void WebRtcVoiceEngine::StartAecDump(const std::string& filename) {
solenberg566ef242015-11-06 15:34:49 -08001035 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001036 if (!is_dumping_aec_) {
1037 // Start dumping AEC when we are not dumping.
ivoca4df27b2015-12-19 10:14:10 -08001038 if (voe_wrapper_->processing()->StartDebugRecording(
1039 filename.c_str()) != webrtc::AudioProcessing::kNoError) {
wu@webrtc.orga9890802013-12-13 00:21:03 +00001040 LOG_RTCERR1(StartDebugRecording, filename.c_str());
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001041 } else {
1042 is_dumping_aec_ = true;
1043 }
1044 }
1045}
1046
1047void WebRtcVoiceEngine::StopAecDump() {
solenberg566ef242015-11-06 15:34:49 -08001048 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001049 if (is_dumping_aec_) {
1050 // Stop dumping AEC when we are dumping.
ivoca4df27b2015-12-19 10:14:10 -08001051 if (voe_wrapper_->processing()->StopDebugRecording() !=
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001052 webrtc::AudioProcessing::kNoError) {
1053 LOG_RTCERR0(StopDebugRecording);
1054 }
1055 is_dumping_aec_ = false;
1056 }
1057}
1058
ivoc112a3d82015-10-16 02:22:18 -07001059bool WebRtcVoiceEngine::StartRtcEventLog(rtc::PlatformFile file) {
solenberg566ef242015-11-06 15:34:49 -08001060 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
ivoc112a3d82015-10-16 02:22:18 -07001061 return voe_wrapper_->codec()->GetEventLog()->StartLogging(file);
1062}
1063
1064void WebRtcVoiceEngine::StopRtcEventLog() {
solenberg566ef242015-11-06 15:34:49 -08001065 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
ivoc112a3d82015-10-16 02:22:18 -07001066 voe_wrapper_->codec()->GetEventLog()->StopLogging();
1067}
1068
solenberg0a617e22015-10-20 15:49:38 -07001069int WebRtcVoiceEngine::CreateVoEChannel() {
solenberg566ef242015-11-06 15:34:49 -08001070 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
solenberg0a617e22015-10-20 15:49:38 -07001071 return voe_wrapper_->base()->CreateChannel(voe_config_);
sergeyu@chromium.org5bc25c42013-12-05 00:24:06 +00001072}
1073
solenbergc96df772015-10-21 13:01:53 -07001074class WebRtcVoiceMediaChannel::WebRtcAudioSendStream
mallinath@webrtc.org67ee6b92014-02-03 16:57:16 +00001075 : public AudioRenderer::Sink {
1076 public:
solenbergc96df772015-10-21 13:01:53 -07001077 WebRtcAudioSendStream(int ch, webrtc::AudioTransport* voe_audio_transport,
solenberg3a941542015-11-16 07:34:50 -08001078 uint32_t ssrc, const std::string& c_name,
1079 const std::vector<webrtc::RtpExtension>& extensions,
1080 webrtc::Call* call)
solenberg7add0582015-11-20 09:59:34 -08001081 : voe_audio_transport_(voe_audio_transport),
solenberg3a941542015-11-16 07:34:50 -08001082 call_(call),
1083 config_(nullptr) {
solenberg85a04962015-10-27 03:35:21 -07001084 RTC_DCHECK_GE(ch, 0);
1085 // TODO(solenberg): Once we're not using FakeWebRtcVoiceEngine anymore:
1086 // RTC_DCHECK(voe_audio_transport);
solenbergc96df772015-10-21 13:01:53 -07001087 RTC_DCHECK(call);
solenberg85a04962015-10-27 03:35:21 -07001088 audio_capture_thread_checker_.DetachFromThread();
solenberg3a941542015-11-16 07:34:50 -08001089 config_.rtp.ssrc = ssrc;
1090 config_.rtp.c_name = c_name;
1091 config_.voe_channel_id = ch;
1092 RecreateAudioSendStream(extensions);
solenbergc96df772015-10-21 13:01:53 -07001093 }
solenberg3a941542015-11-16 07:34:50 -08001094
solenbergc96df772015-10-21 13:01:53 -07001095 ~WebRtcAudioSendStream() override {
solenberg566ef242015-11-06 15:34:49 -08001096 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
solenbergc96df772015-10-21 13:01:53 -07001097 Stop();
1098 call_->DestroyAudioSendStream(stream_);
1099 }
henrike@webrtc.org1e09a712013-07-26 19:17:59 +00001100
solenberg3a941542015-11-16 07:34:50 -08001101 void RecreateAudioSendStream(
1102 const std::vector<webrtc::RtpExtension>& extensions) {
1103 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
1104 if (stream_) {
1105 call_->DestroyAudioSendStream(stream_);
1106 stream_ = nullptr;
1107 }
1108 config_.rtp.extensions = extensions;
1109 RTC_DCHECK(!stream_);
1110 stream_ = call_->CreateAudioSendStream(config_);
1111 RTC_CHECK(stream_);
1112 }
1113
Fredrik Solenbergb5727682015-12-04 15:22:19 +01001114 bool SendTelephoneEvent(int payload_type, uint8_t event,
1115 uint32_t duration_ms) {
1116 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
1117 RTC_DCHECK(stream_);
1118 return stream_->SendTelephoneEvent(payload_type, event, duration_ms);
1119 }
1120
solenberg3a941542015-11-16 07:34:50 -08001121 webrtc::AudioSendStream::Stats GetStats() const {
1122 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
1123 RTC_DCHECK(stream_);
1124 return stream_->GetStats();
1125 }
1126
mallinath@webrtc.org67ee6b92014-02-03 16:57:16 +00001127 // Starts the rendering by setting a sink to the renderer to get data
1128 // callback.
henrike@webrtc.orga7b98182014-02-21 15:51:43 +00001129 // This method is called on the libjingle worker thread.
mallinath@webrtc.org67ee6b92014-02-03 16:57:16 +00001130 // TODO(xians): Make sure Start() is called only once.
1131 void Start(AudioRenderer* renderer) {
solenberg566ef242015-11-06 15:34:49 -08001132 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
solenbergc96df772015-10-21 13:01:53 -07001133 RTC_DCHECK(renderer);
1134 if (renderer_) {
henrikg91d6ede2015-09-17 00:24:34 -07001135 RTC_DCHECK(renderer_ == renderer);
mallinath@webrtc.org67ee6b92014-02-03 16:57:16 +00001136 return;
1137 }
mallinath@webrtc.org67ee6b92014-02-03 16:57:16 +00001138 renderer->SetSink(this);
1139 renderer_ = renderer;
1140 }
1141
solenbergc96df772015-10-21 13:01:53 -07001142 // Stops rendering by setting the sink of the renderer to nullptr. No data
mallinath@webrtc.org67ee6b92014-02-03 16:57:16 +00001143 // callback will be received after this method.
henrike@webrtc.orga7b98182014-02-21 15:51:43 +00001144 // This method is called on the libjingle worker thread.
mallinath@webrtc.org67ee6b92014-02-03 16:57:16 +00001145 void Stop() {
solenberg566ef242015-11-06 15:34:49 -08001146 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
solenbergc96df772015-10-21 13:01:53 -07001147 if (renderer_) {
1148 renderer_->SetSink(nullptr);
1149 renderer_ = nullptr;
solenberg98c68862015-10-09 03:27:14 -07001150 }
mallinath@webrtc.org67ee6b92014-02-03 16:57:16 +00001151 }
1152
1153 // AudioRenderer::Sink implementation.
henrike@webrtc.orga7b98182014-02-21 15:51:43 +00001154 // This method is called on the audio thread.
kjellander@webrtc.org14665ff2015-03-04 12:58:35 +00001155 void OnData(const void* audio_data,
1156 int bits_per_sample,
1157 int sample_rate,
Peter Kasting69558702016-01-12 16:26:35 -08001158 size_t number_of_channels,
Peter Kastingdce40cf2015-08-24 14:52:23 -07001159 size_t number_of_frames) override {
solenberg566ef242015-11-06 15:34:49 -08001160 RTC_DCHECK(!worker_thread_checker_.CalledOnValidThread());
solenberg85a04962015-10-27 03:35:21 -07001161 RTC_DCHECK(audio_capture_thread_checker_.CalledOnValidThread());
solenbergc96df772015-10-21 13:01:53 -07001162 RTC_DCHECK(voe_audio_transport_);
solenberg7add0582015-11-20 09:59:34 -08001163 voe_audio_transport_->OnData(config_.voe_channel_id,
henrike@webrtc.orga7b98182014-02-21 15:51:43 +00001164 audio_data,
1165 bits_per_sample,
1166 sample_rate,
1167 number_of_channels,
1168 number_of_frames);
henrike@webrtc.orga7b98182014-02-21 15:51:43 +00001169 }
1170
1171 // Callback from the |renderer_| when it is going away. In case Start() has
1172 // never been called, this callback won't be triggered.
kjellander@webrtc.org14665ff2015-03-04 12:58:35 +00001173 void OnClose() override {
solenberg566ef242015-11-06 15:34:49 -08001174 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
solenbergc96df772015-10-21 13:01:53 -07001175 // Set |renderer_| to nullptr to make sure no more callback will get into
henrike@webrtc.orga7b98182014-02-21 15:51:43 +00001176 // the renderer.
solenbergc96df772015-10-21 13:01:53 -07001177 renderer_ = nullptr;
mallinath@webrtc.org67ee6b92014-02-03 16:57:16 +00001178 }
1179
1180 // Accessor to the VoE channel ID.
solenberg85a04962015-10-27 03:35:21 -07001181 int channel() const {
solenberg566ef242015-11-06 15:34:49 -08001182 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
solenberg7add0582015-11-20 09:59:34 -08001183 return config_.voe_channel_id;
solenberg85a04962015-10-27 03:35:21 -07001184 }
mallinath@webrtc.org67ee6b92014-02-03 16:57:16 +00001185
1186 private:
solenberg566ef242015-11-06 15:34:49 -08001187 rtc::ThreadChecker worker_thread_checker_;
solenberg85a04962015-10-27 03:35:21 -07001188 rtc::ThreadChecker audio_capture_thread_checker_;
solenbergc96df772015-10-21 13:01:53 -07001189 webrtc::AudioTransport* const voe_audio_transport_ = nullptr;
1190 webrtc::Call* call_ = nullptr;
solenberg3a941542015-11-16 07:34:50 -08001191 webrtc::AudioSendStream::Config config_;
1192 // The stream is owned by WebRtcAudioSendStream and may be reallocated if
1193 // configuration changes.
solenbergc96df772015-10-21 13:01:53 -07001194 webrtc::AudioSendStream* stream_ = nullptr;
mallinath@webrtc.org67ee6b92014-02-03 16:57:16 +00001195
1196 // Raw pointer to AudioRenderer owned by LocalAudioTrackHandler.
1197 // PeerConnection will make sure invalidating the pointer before the object
1198 // goes away.
solenbergc96df772015-10-21 13:01:53 -07001199 AudioRenderer* renderer_ = nullptr;
henrike@webrtc.orga7b98182014-02-21 15:51:43 +00001200
solenbergc96df772015-10-21 13:01:53 -07001201 RTC_DISALLOW_IMPLICIT_CONSTRUCTORS(WebRtcAudioSendStream);
1202};
1203
1204class WebRtcVoiceMediaChannel::WebRtcAudioReceiveStream {
1205 public:
solenberg7add0582015-11-20 09:59:34 -08001206 WebRtcAudioReceiveStream(int ch, uint32_t remote_ssrc, uint32_t local_ssrc,
1207 bool use_combined_bwe, const std::string& sync_group,
1208 const std::vector<webrtc::RtpExtension>& extensions,
1209 webrtc::Call* call)
1210 : call_(call),
1211 config_() {
1212 RTC_DCHECK_GE(ch, 0);
1213 RTC_DCHECK(call);
1214 config_.rtp.remote_ssrc = remote_ssrc;
1215 config_.rtp.local_ssrc = local_ssrc;
1216 config_.voe_channel_id = ch;
1217 config_.sync_group = sync_group;
1218 RecreateAudioReceiveStream(use_combined_bwe, extensions);
1219 }
solenbergc96df772015-10-21 13:01:53 -07001220
solenberg7add0582015-11-20 09:59:34 -08001221 ~WebRtcAudioReceiveStream() {
1222 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
1223 call_->DestroyAudioReceiveStream(stream_);
1224 }
1225
1226 void RecreateAudioReceiveStream(
1227 const std::vector<webrtc::RtpExtension>& extensions) {
1228 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
1229 RecreateAudioReceiveStream(config_.combined_audio_video_bwe, extensions);
1230 }
1231 void RecreateAudioReceiveStream(bool use_combined_bwe) {
1232 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
1233 RecreateAudioReceiveStream(use_combined_bwe, config_.rtp.extensions);
1234 }
1235
1236 webrtc::AudioReceiveStream::Stats GetStats() const {
1237 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
1238 RTC_DCHECK(stream_);
1239 return stream_->GetStats();
1240 }
1241
1242 int channel() const {
1243 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
1244 return config_.voe_channel_id;
1245 }
solenbergc96df772015-10-21 13:01:53 -07001246
deadbeef2d110be2016-01-13 12:00:26 -08001247 void SetRawAudioSink(rtc::scoped_ptr<webrtc::AudioSinkInterface> sink) {
Tommif888bb52015-12-12 01:37:01 +01001248 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
deadbeef2d110be2016-01-13 12:00:26 -08001249 stream_->SetSink(std::move(sink));
Tommif888bb52015-12-12 01:37:01 +01001250 }
1251
solenbergc96df772015-10-21 13:01:53 -07001252 private:
solenberg7add0582015-11-20 09:59:34 -08001253 void RecreateAudioReceiveStream(bool use_combined_bwe,
1254 const std::vector<webrtc::RtpExtension>& extensions) {
1255 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
1256 if (stream_) {
1257 call_->DestroyAudioReceiveStream(stream_);
1258 stream_ = nullptr;
1259 }
1260 config_.rtp.extensions = extensions;
1261 config_.combined_audio_video_bwe = use_combined_bwe;
1262 RTC_DCHECK(!stream_);
1263 stream_ = call_->CreateAudioReceiveStream(config_);
1264 RTC_CHECK(stream_);
1265 }
1266
1267 rtc::ThreadChecker worker_thread_checker_;
1268 webrtc::Call* call_ = nullptr;
1269 webrtc::AudioReceiveStream::Config config_;
1270 // The stream is owned by WebRtcAudioReceiveStream and may be reallocated if
1271 // configuration changes.
1272 webrtc::AudioReceiveStream* stream_ = nullptr;
solenbergc96df772015-10-21 13:01:53 -07001273
1274 RTC_DISALLOW_IMPLICIT_CONSTRUCTORS(WebRtcAudioReceiveStream);
henrike@webrtc.org1e09a712013-07-26 19:17:59 +00001275};
1276
Fredrik Solenberg709ed672015-09-15 12:26:33 +02001277WebRtcVoiceMediaChannel::WebRtcVoiceMediaChannel(WebRtcVoiceEngine* engine,
Fredrik Solenbergb071a192015-09-17 16:42:56 +02001278 const AudioOptions& options,
Fredrik Solenberg709ed672015-09-15 12:26:33 +02001279 webrtc::Call* call)
solenberg566ef242015-11-06 15:34:49 -08001280 : engine_(engine), call_(call) {
solenberg0a617e22015-10-20 15:49:38 -07001281 LOG(LS_VERBOSE) << "WebRtcVoiceMediaChannel::WebRtcVoiceMediaChannel";
solenberg566ef242015-11-06 15:34:49 -08001282 RTC_DCHECK(call);
solenberg0a617e22015-10-20 15:49:38 -07001283 engine->RegisterChannel(this);
Fredrik Solenbergb071a192015-09-17 16:42:56 +02001284 SetOptions(options);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001285}
1286
1287WebRtcVoiceMediaChannel::~WebRtcVoiceMediaChannel() {
solenberg566ef242015-11-06 15:34:49 -08001288 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
solenberg0a617e22015-10-20 15:49:38 -07001289 LOG(LS_VERBOSE) << "WebRtcVoiceMediaChannel::~WebRtcVoiceMediaChannel";
solenberg7add0582015-11-20 09:59:34 -08001290 // TODO(solenberg): Should be able to delete the streams directly, without
1291 // going through RemoveNnStream(), once stream objects handle
1292 // all (de)configuration.
solenbergc96df772015-10-21 13:01:53 -07001293 while (!send_streams_.empty()) {
1294 RemoveSendStream(send_streams_.begin()->first);
solenbergd97ec302015-10-07 01:40:33 -07001295 }
solenberg7add0582015-11-20 09:59:34 -08001296 while (!recv_streams_.empty()) {
1297 RemoveRecvStream(recv_streams_.begin()->first);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001298 }
solenberg0a617e22015-10-20 15:49:38 -07001299 engine()->UnregisterChannel(this);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001300}
1301
Peter Thatcherc2ee2c82015-08-07 16:05:34 -07001302bool WebRtcVoiceMediaChannel::SetSendParameters(
1303 const AudioSendParameters& params) {
solenberg566ef242015-11-06 15:34:49 -08001304 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
solenberg7e4e01a2015-12-02 08:05:01 -08001305 LOG(LS_INFO) << "WebRtcVoiceMediaChannel::SetSendParameters: "
1306 << params.ToString();
Peter Thatcherc2ee2c82015-08-07 16:05:34 -07001307 // TODO(pthatcher): Refactor this to be more clean now that we have
1308 // all the information at once.
solenberg3a941542015-11-16 07:34:50 -08001309
1310 if (!SetSendCodecs(params.codecs)) {
1311 return false;
1312 }
1313
solenberg7e4e01a2015-12-02 08:05:01 -08001314 if (!ValidateRtpExtensions(params.extensions)) {
1315 return false;
1316 }
1317 std::vector<webrtc::RtpExtension> filtered_extensions =
1318 FilterRtpExtensions(params.extensions,
1319 webrtc::RtpExtension::IsSupportedForAudio, true);
1320 if (send_rtp_extensions_ != filtered_extensions) {
1321 send_rtp_extensions_.swap(filtered_extensions);
solenberg3a941542015-11-16 07:34:50 -08001322 for (auto& it : send_streams_) {
1323 it.second->RecreateAudioSendStream(send_rtp_extensions_);
1324 }
1325 }
1326
1327 if (!SetMaxSendBandwidth(params.max_bandwidth_bps)) {
1328 return false;
1329 }
1330 return SetOptions(params.options);
Peter Thatcherc2ee2c82015-08-07 16:05:34 -07001331}
1332
1333bool WebRtcVoiceMediaChannel::SetRecvParameters(
1334 const AudioRecvParameters& params) {
solenberg566ef242015-11-06 15:34:49 -08001335 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
solenberg7e4e01a2015-12-02 08:05:01 -08001336 LOG(LS_INFO) << "WebRtcVoiceMediaChannel::SetRecvParameters: "
1337 << params.ToString();
Peter Thatcherc2ee2c82015-08-07 16:05:34 -07001338 // TODO(pthatcher): Refactor this to be more clean now that we have
1339 // all the information at once.
solenberg7add0582015-11-20 09:59:34 -08001340
1341 if (!SetRecvCodecs(params.codecs)) {
1342 return false;
1343 }
1344
solenberg7e4e01a2015-12-02 08:05:01 -08001345 if (!ValidateRtpExtensions(params.extensions)) {
1346 return false;
1347 }
1348 std::vector<webrtc::RtpExtension> filtered_extensions =
1349 FilterRtpExtensions(params.extensions,
1350 webrtc::RtpExtension::IsSupportedForAudio, false);
1351 if (recv_rtp_extensions_ != filtered_extensions) {
1352 recv_rtp_extensions_.swap(filtered_extensions);
solenberg7add0582015-11-20 09:59:34 -08001353 for (auto& it : recv_streams_) {
1354 it.second->RecreateAudioReceiveStream(recv_rtp_extensions_);
1355 }
1356 }
1357
1358 return true;
Peter Thatcherc2ee2c82015-08-07 16:05:34 -07001359}
1360
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001361bool WebRtcVoiceMediaChannel::SetOptions(const AudioOptions& options) {
solenberg566ef242015-11-06 15:34:49 -08001362 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001363 LOG(LS_INFO) << "Setting voice channel options: "
1364 << options.ToString();
1365
wu@webrtc.orgde305012013-10-31 15:40:38 +00001366 // Check if DSCP value is changed from previous.
1367 bool dscp_option_changed = (options_.dscp != options.dscp);
1368
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001369 // We retain all of the existing options, and apply the given ones
1370 // on top. This means there is no way to "clear" options such that
1371 // they go back to the engine default.
1372 options_.SetAll(options);
solenberg246b8172015-12-08 09:50:23 -08001373 if (!engine()->ApplyOptions(options_)) {
1374 LOG(LS_WARNING) <<
1375 "Failed to apply engine options during channel SetOptions.";
1376 return false;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001377 }
1378
wu@webrtc.orgde305012013-10-31 15:40:38 +00001379 if (dscp_option_changed) {
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00001380 rtc::DiffServCodePoint dscp = rtc::DSCP_DEFAULT;
solenberg246b8172015-12-08 09:50:23 -08001381 if (options_.dscp.value_or(false)) {
wu@webrtc.orgde305012013-10-31 15:40:38 +00001382 dscp = kAudioDscpValue;
solenberg246b8172015-12-08 09:50:23 -08001383 }
wu@webrtc.orgde305012013-10-31 15:40:38 +00001384 if (MediaChannel::SetDscp(dscp) != 0) {
1385 LOG(LS_WARNING) << "Failed to set DSCP settings for audio channel";
1386 }
1387 }
solenberg8fb30c32015-10-13 03:06:58 -07001388
solenbergc96df772015-10-21 13:01:53 -07001389 // TODO(solenberg): Don't recreate unless options changed.
solenberg7add0582015-11-20 09:59:34 -08001390 for (auto& it : recv_streams_) {
1391 it.second->RecreateAudioReceiveStream(
1392 options_.combined_audio_video_bwe.value_or(false));
1393 }
solenberg8fb30c32015-10-13 03:06:58 -07001394
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001395 LOG(LS_INFO) << "Set voice channel options. Current options: "
1396 << options_.ToString();
1397 return true;
1398}
1399
1400bool WebRtcVoiceMediaChannel::SetRecvCodecs(
1401 const std::vector<AudioCodec>& codecs) {
solenberg566ef242015-11-06 15:34:49 -08001402 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
solenberg8fb30c32015-10-13 03:06:58 -07001403
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001404 // Set the payload types to be used for incoming media.
solenberg0b675462015-10-09 01:37:09 -07001405 LOG(LS_INFO) << "Setting receive voice codecs.";
solenberg0b675462015-10-09 01:37:09 -07001406
1407 if (!VerifyUniquePayloadTypes(codecs)) {
1408 LOG(LS_ERROR) << "Codec payload types overlap.";
1409 return false;
1410 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001411
1412 std::vector<AudioCodec> new_codecs;
1413 // Find all new codecs. We allow adding new codecs but don't allow changing
1414 // the payload type of codecs that is already configured since we might
1415 // already be receiving packets with that payload type.
Fredrik Solenbergaf9fb212015-08-26 10:45:53 +02001416 for (const AudioCodec& codec : codecs) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001417 AudioCodec old_codec;
Fredrik Solenbergaf9fb212015-08-26 10:45:53 +02001418 if (FindCodec(recv_codecs_, codec, &old_codec)) {
1419 if (old_codec.id != codec.id) {
1420 LOG(LS_ERROR) << codec.name << " payload type changed.";
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001421 return false;
1422 }
1423 } else {
Fredrik Solenbergaf9fb212015-08-26 10:45:53 +02001424 new_codecs.push_back(codec);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001425 }
1426 }
1427 if (new_codecs.empty()) {
1428 // There are no new codecs to configure. Already configured codecs are
1429 // never removed.
1430 return true;
1431 }
1432
1433 if (playout_) {
1434 // Receive codecs can not be changed while playing. So we temporarily
1435 // pause playout.
1436 PausePlayout();
1437 }
1438
solenberg26c8c912015-11-27 04:00:25 -08001439 bool result = true;
1440 for (const AudioCodec& codec : new_codecs) {
1441 webrtc::CodecInst voe_codec;
1442 if (WebRtcVoiceEngine::ToCodecInst(codec, &voe_codec)) {
1443 LOG(LS_INFO) << ToString(codec);
1444 voe_codec.pltype = codec.id;
1445 for (const auto& ch : recv_streams_) {
1446 if (engine()->voe()->codec()->SetRecPayloadType(
1447 ch.second->channel(), voe_codec) == -1) {
1448 LOG_RTCERR2(SetRecPayloadType, ch.second->channel(),
1449 ToString(voe_codec));
1450 result = false;
1451 }
1452 }
1453 } else {
1454 LOG(LS_WARNING) << "Unknown codec " << ToString(codec);
1455 result = false;
1456 break;
1457 }
1458 }
Fredrik Solenbergaf9fb212015-08-26 10:45:53 +02001459 if (result) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001460 recv_codecs_ = codecs;
1461 }
1462
1463 if (desired_playout_ && !playout_) {
1464 ResumePlayout();
1465 }
Fredrik Solenbergaf9fb212015-08-26 10:45:53 +02001466 return result;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001467}
1468
1469bool WebRtcVoiceMediaChannel::SetSendCodecs(
wu@webrtc.orgcadf9042013-08-30 21:24:16 +00001470 int channel, const std::vector<AudioCodec>& codecs) {
buildbot@webrtc.orgae740dd2014-06-17 10:56:41 +00001471 // Disable VAD, FEC, and RED unless we know the other side wants them.
wu@webrtc.orgcadf9042013-08-30 21:24:16 +00001472 engine()->voe()->codec()->SetVADStatus(channel, false);
1473 engine()->voe()->rtp()->SetNACKStatus(channel, false, 0);
buildbot@webrtc.orgae740dd2014-06-17 10:56:41 +00001474 engine()->voe()->rtp()->SetREDStatus(channel, false);
1475 engine()->voe()->codec()->SetFECStatus(channel, false);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001476
1477 // Scan through the list to figure out the codec to use for sending, along
Fredrik Solenbergb5727682015-12-04 15:22:19 +01001478 // with the proper configuration for VAD.
henrike@webrtc.org704bf9e2014-02-27 17:52:04 +00001479 bool found_send_codec = false;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001480 webrtc::CodecInst send_codec;
1481 memset(&send_codec, 0, sizeof(send_codec));
1482
wu@webrtc.org05e7b442014-04-01 17:44:24 +00001483 bool nack_enabled = nack_enabled_;
buildbot@webrtc.org3ffa1f92014-07-02 19:51:26 +00001484 bool enable_codec_fec = false;
Minyue Li7100dcd2015-03-27 05:05:59 +01001485 bool enable_opus_dtx = false;
minyue@webrtc.org26236952014-10-29 02:27:08 +00001486 int opus_max_playback_rate = 0;
buildbot@webrtc.org5d639b32014-09-10 07:57:12 +00001487
henrike@webrtc.org704bf9e2014-02-27 17:52:04 +00001488 // Set send codec (the first non-telephone-event/CN codec)
Fredrik Solenbergaf9fb212015-08-26 10:45:53 +02001489 for (const AudioCodec& codec : codecs) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001490 // Ignore codecs we don't know about. The negotiation step should prevent
1491 // this, but double-check to be sure.
1492 webrtc::CodecInst voe_codec;
solenberg26c8c912015-11-27 04:00:25 -08001493 if (!WebRtcVoiceEngine::ToCodecInst(codec, &voe_codec)) {
Fredrik Solenbergaf9fb212015-08-26 10:45:53 +02001494 LOG(LS_WARNING) << "Unknown codec " << ToString(codec);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001495 continue;
1496 }
1497
Fredrik Solenbergaf9fb212015-08-26 10:45:53 +02001498 if (IsCodec(codec, kDtmfCodecName) || IsCodec(codec, kCnCodecName)) {
henrike@webrtc.org704bf9e2014-02-27 17:52:04 +00001499 // Skip telephone-event/CN codec, which will be handled later.
1500 continue;
1501 }
1502
henrike@webrtc.org704bf9e2014-02-27 17:52:04 +00001503 // We'll use the first codec in the list to actually send audio data.
1504 // Be sure to use the payload type requested by the remote side.
buildbot@webrtc.orgae740dd2014-06-17 10:56:41 +00001505 // "red", for RED audio, is a special case where the actual codec to be
henrike@webrtc.org704bf9e2014-02-27 17:52:04 +00001506 // used is specified in params.
Fredrik Solenbergaf9fb212015-08-26 10:45:53 +02001507 if (IsCodec(codec, kRedCodecName)) {
henrike@webrtc.org704bf9e2014-02-27 17:52:04 +00001508 // Parse out the RED parameters. If we fail, just ignore RED;
1509 // we don't support all possible params/usage scenarios.
Fredrik Solenbergaf9fb212015-08-26 10:45:53 +02001510 if (!GetRedSendCodec(codec, codecs, &send_codec)) {
henrike@webrtc.org704bf9e2014-02-27 17:52:04 +00001511 continue;
1512 }
1513
1514 // Enable redundant encoding of the specified codec. Treat any
1515 // failure as a fatal internal error.
buildbot@webrtc.orgae740dd2014-06-17 10:56:41 +00001516 LOG(LS_INFO) << "Enabling RED on channel " << channel;
Fredrik Solenbergaf9fb212015-08-26 10:45:53 +02001517 if (engine()->voe()->rtp()->SetREDStatus(channel, true, codec.id) == -1) {
1518 LOG_RTCERR3(SetREDStatus, channel, true, codec.id);
henrike@webrtc.org704bf9e2014-02-27 17:52:04 +00001519 return false;
1520 }
1521 } else {
1522 send_codec = voe_codec;
Fredrik Solenbergaf9fb212015-08-26 10:45:53 +02001523 nack_enabled = IsNackEnabled(codec);
Minyue Li7100dcd2015-03-27 05:05:59 +01001524 // For Opus as the send codec, we are to determine inband FEC, maximum
1525 // playback rate, and opus internal dtx.
Fredrik Solenbergaf9fb212015-08-26 10:45:53 +02001526 if (IsCodec(codec, kOpusCodecName)) {
1527 GetOpusConfig(codec, &send_codec, &enable_codec_fec,
Minyue Li7100dcd2015-03-27 05:05:59 +01001528 &opus_max_playback_rate, &enable_opus_dtx);
buildbot@webrtc.org5d639b32014-09-10 07:57:12 +00001529 }
Brave Yao5225dd82015-03-26 07:39:19 +08001530
1531 // Set packet size if the AudioCodec param kCodecParamPTime is set.
1532 int ptime_ms = 0;
Fredrik Solenbergaf9fb212015-08-26 10:45:53 +02001533 if (codec.GetParam(kCodecParamPTime, &ptime_ms)) {
solenberg26c8c912015-11-27 04:00:25 -08001534 if (!WebRtcVoiceCodecs::SetPTimeAsPacketSize(&send_codec, ptime_ms)) {
Brave Yao5225dd82015-03-26 07:39:19 +08001535 LOG(LS_WARNING) << "Failed to set packet size for codec "
1536 << send_codec.plname;
1537 return false;
1538 }
1539 }
henrike@webrtc.org704bf9e2014-02-27 17:52:04 +00001540 }
1541 found_send_codec = true;
1542 break;
1543 }
1544
wu@webrtc.org05e7b442014-04-01 17:44:24 +00001545 if (nack_enabled_ != nack_enabled) {
1546 SetNack(channel, nack_enabled);
1547 nack_enabled_ = nack_enabled;
1548 }
1549
henrike@webrtc.org704bf9e2014-02-27 17:52:04 +00001550 if (!found_send_codec) {
1551 LOG(LS_WARNING) << "Received empty list of codecs.";
1552 return false;
1553 }
1554
1555 // Set the codec immediately, since SetVADStatus() depends on whether
1556 // the current codec is mono or stereo.
1557 if (!SetSendCodec(channel, send_codec))
1558 return false;
1559
buildbot@webrtc.org3ffa1f92014-07-02 19:51:26 +00001560 // FEC should be enabled after SetSendCodec.
1561 if (enable_codec_fec) {
1562 LOG(LS_INFO) << "Attempt to enable codec internal FEC on channel "
1563 << channel;
buildbot@webrtc.org3ffa1f92014-07-02 19:51:26 +00001564 if (engine()->voe()->codec()->SetFECStatus(channel, true) == -1) {
1565 // Enable codec internal FEC. Treat any failure as fatal internal error.
1566 LOG_RTCERR2(SetFECStatus, channel, true);
1567 return false;
1568 }
buildbot@webrtc.org3ffa1f92014-07-02 19:51:26 +00001569 }
1570
Minyue Li7100dcd2015-03-27 05:05:59 +01001571 if (IsCodec(send_codec, kOpusCodecName)) {
1572 // DTX and maxplaybackrate should be set after SetSendCodec. Because current
1573 // send codec has to be Opus.
1574
1575 // Set Opus internal DTX.
1576 LOG(LS_INFO) << "Attempt to "
solenbergbd138382015-11-20 16:08:07 -08001577 << (enable_opus_dtx ? "enable" : "disable")
Minyue Li7100dcd2015-03-27 05:05:59 +01001578 << " Opus DTX on channel "
buildbot@webrtc.org5d639b32014-09-10 07:57:12 +00001579 << channel;
Minyue Li7100dcd2015-03-27 05:05:59 +01001580 if (engine()->voe()->codec()->SetOpusDtx(channel, enable_opus_dtx)) {
1581 LOG_RTCERR2(SetOpusDtx, channel, enable_opus_dtx);
1582 return false;
1583 }
1584
1585 // If opus_max_playback_rate <= 0, the default maximum playback rate
1586 // (48 kHz) will be used.
1587 if (opus_max_playback_rate > 0) {
1588 LOG(LS_INFO) << "Attempt to set maximum playback rate to "
1589 << opus_max_playback_rate
1590 << " Hz on channel "
1591 << channel;
1592 if (engine()->voe()->codec()->SetOpusMaxPlaybackRate(
1593 channel, opus_max_playback_rate) == -1) {
1594 LOG_RTCERR2(SetOpusMaxPlaybackRate, channel, opus_max_playback_rate);
1595 return false;
1596 }
buildbot@webrtc.org5d639b32014-09-10 07:57:12 +00001597 }
buildbot@webrtc.org5d639b32014-09-10 07:57:12 +00001598 }
1599
henrike@webrtc.org704bf9e2014-02-27 17:52:04 +00001600 // Always update the |send_codec_| to the currently set send codec.
1601 send_codec_.reset(new webrtc::CodecInst(send_codec));
1602
minyue@webrtc.org26236952014-10-29 02:27:08 +00001603 if (send_bitrate_setting_) {
1604 SetSendBitrateInternal(send_bitrate_bps_);
henrike@webrtc.org704bf9e2014-02-27 17:52:04 +00001605 }
1606
Fredrik Solenbergb5727682015-12-04 15:22:19 +01001607 // Loop through the codecs list again to config the CN codec.
Fredrik Solenbergaf9fb212015-08-26 10:45:53 +02001608 for (const AudioCodec& codec : codecs) {
henrike@webrtc.org704bf9e2014-02-27 17:52:04 +00001609 // Ignore codecs we don't know about. The negotiation step should prevent
1610 // this, but double-check to be sure.
1611 webrtc::CodecInst voe_codec;
solenberg26c8c912015-11-27 04:00:25 -08001612 if (!WebRtcVoiceEngine::ToCodecInst(codec, &voe_codec)) {
Fredrik Solenbergaf9fb212015-08-26 10:45:53 +02001613 LOG(LS_WARNING) << "Unknown codec " << ToString(codec);
henrike@webrtc.org704bf9e2014-02-27 17:52:04 +00001614 continue;
1615 }
1616
Fredrik Solenbergb5727682015-12-04 15:22:19 +01001617 if (IsCodec(codec, kCnCodecName)) {
henrike@webrtc.org704bf9e2014-02-27 17:52:04 +00001618 // Turn voice activity detection/comfort noise on if supported.
1619 // Set the wideband CN payload type appropriately.
1620 // (narrowband always uses the static payload type 13).
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001621 webrtc::PayloadFrequencies cn_freq;
Fredrik Solenbergaf9fb212015-08-26 10:45:53 +02001622 switch (codec.clockrate) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001623 case 8000:
1624 cn_freq = webrtc::kFreq8000Hz;
1625 break;
1626 case 16000:
1627 cn_freq = webrtc::kFreq16000Hz;
1628 break;
1629 case 32000:
1630 cn_freq = webrtc::kFreq32000Hz;
1631 break;
1632 default:
Fredrik Solenbergaf9fb212015-08-26 10:45:53 +02001633 LOG(LS_WARNING) << "CN frequency " << codec.clockrate
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001634 << " not supported.";
1635 continue;
1636 }
wu@webrtc.orgcadf9042013-08-30 21:24:16 +00001637 // Set the CN payloadtype and the VAD status.
1638 // The CN payload type for 8000 Hz clockrate is fixed at 13.
1639 if (cn_freq != webrtc::kFreq8000Hz) {
1640 if (engine()->voe()->codec()->SetSendCNPayloadType(
Fredrik Solenbergaf9fb212015-08-26 10:45:53 +02001641 channel, codec.id, cn_freq) == -1) {
1642 LOG_RTCERR3(SetSendCNPayloadType, channel, codec.id, cn_freq);
wu@webrtc.orgcadf9042013-08-30 21:24:16 +00001643 // TODO(ajm): This failure condition will be removed from VoE.
1644 // Restore the return here when we update to a new enough webrtc.
1645 //
1646 // Not returning false because the SetSendCNPayloadType will fail if
1647 // the channel is already sending.
1648 // This can happen if the remote description is applied twice, for
1649 // example in the case of ROAP on top of JSEP, where both side will
1650 // send the offer.
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001651 }
wu@webrtc.orgcadf9042013-08-30 21:24:16 +00001652 }
wu@webrtc.orgcadf9042013-08-30 21:24:16 +00001653 // Only turn on VAD if we have a CN payload type that matches the
1654 // clockrate for the codec we are going to use.
Fredrik Solenbergaf9fb212015-08-26 10:45:53 +02001655 if (codec.clockrate == send_codec.plfreq && send_codec.channels != 2) {
Minyue Li7100dcd2015-03-27 05:05:59 +01001656 // TODO(minyue): If CN frequency == 48000 Hz is allowed, consider the
1657 // interaction between VAD and Opus FEC.
wu@webrtc.orgcadf9042013-08-30 21:24:16 +00001658 LOG(LS_INFO) << "Enabling VAD";
1659 if (engine()->voe()->codec()->SetVADStatus(channel, true) == -1) {
1660 LOG_RTCERR2(SetVADStatus, channel, true);
1661 return false;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001662 }
1663 }
1664 }
wu@webrtc.org1d1ffc92013-10-16 18:12:02 +00001665 }
wu@webrtc.orgcadf9042013-08-30 21:24:16 +00001666 return true;
1667}
1668
1669bool WebRtcVoiceMediaChannel::SetSendCodecs(
1670 const std::vector<AudioCodec>& codecs) {
solenberg566ef242015-11-06 15:34:49 -08001671 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
Fredrik Solenbergb5727682015-12-04 15:22:19 +01001672 // TODO(solenberg): Validate input - that payload types don't overlap, are
1673 // within range, filter out codecs we don't support,
1674 // redundant codecs etc.
solenbergd97ec302015-10-07 01:40:33 -07001675
Fredrik Solenbergb5727682015-12-04 15:22:19 +01001676 // Find the DTMF telephone event "codec" payload type.
1677 dtmf_payload_type_ = rtc::Optional<int>();
Fredrik Solenbergaf9fb212015-08-26 10:45:53 +02001678 for (const AudioCodec& codec : codecs) {
Fredrik Solenbergaf9fb212015-08-26 10:45:53 +02001679 if (IsCodec(codec, kDtmfCodecName)) {
Fredrik Solenbergb5727682015-12-04 15:22:19 +01001680 dtmf_payload_type_ = rtc::Optional<int>(codec.id);
1681 break;
wu@webrtc.orgcadf9042013-08-30 21:24:16 +00001682 }
1683 }
1684
1685 // Cache the codecs in order to configure the channel created later.
1686 send_codecs_ = codecs;
solenbergc96df772015-10-21 13:01:53 -07001687 for (const auto& ch : send_streams_) {
Fredrik Solenbergaf9fb212015-08-26 10:45:53 +02001688 if (!SetSendCodecs(ch.second->channel(), codecs)) {
wu@webrtc.orgcadf9042013-08-30 21:24:16 +00001689 return false;
1690 }
1691 }
1692
wu@webrtc.org05e7b442014-04-01 17:44:24 +00001693 // Set nack status on receive channels and update |nack_enabled_|.
solenberg7add0582015-11-20 09:59:34 -08001694 for (const auto& ch : recv_streams_) {
solenberg0a617e22015-10-20 15:49:38 -07001695 SetNack(ch.second->channel(), nack_enabled_);
wu@webrtc.org9dba5252013-08-05 20:36:57 +00001696 }
solenberg0a617e22015-10-20 15:49:38 -07001697
1698 return true;
wu@webrtc.org9dba5252013-08-05 20:36:57 +00001699}
1700
wu@webrtc.orgcadf9042013-08-30 21:24:16 +00001701void WebRtcVoiceMediaChannel::SetNack(int channel, bool nack_enabled) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001702 if (nack_enabled) {
wu@webrtc.orgcadf9042013-08-30 21:24:16 +00001703 LOG(LS_INFO) << "Enabling NACK for channel " << channel;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001704 engine()->voe()->rtp()->SetNACKStatus(channel, true, kNackMaxPackets);
1705 } else {
wu@webrtc.orgcadf9042013-08-30 21:24:16 +00001706 LOG(LS_INFO) << "Disabling NACK for channel " << channel;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001707 engine()->voe()->rtp()->SetNACKStatus(channel, false, 0);
1708 }
1709}
1710
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001711bool WebRtcVoiceMediaChannel::SetSendCodec(
wu@webrtc.org9dba5252013-08-05 20:36:57 +00001712 int channel, const webrtc::CodecInst& send_codec) {
1713 LOG(LS_INFO) << "Send channel " << channel << " selected voice codec "
1714 << ToString(send_codec) << ", bitrate=" << send_codec.rate;
1715
wu@webrtc.org05e7b442014-04-01 17:44:24 +00001716 webrtc::CodecInst current_codec;
1717 if (engine()->voe()->codec()->GetSendCodec(channel, current_codec) == 0 &&
1718 (send_codec == current_codec)) {
1719 // Codec is already configured, we can return without setting it again.
1720 return true;
1721 }
1722
wu@webrtc.org9dba5252013-08-05 20:36:57 +00001723 if (engine()->voe()->codec()->SetSendCodec(channel, send_codec) == -1) {
1724 LOG_RTCERR2(SetSendCodec, channel, ToString(send_codec));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001725 return false;
1726 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001727 return true;
1728}
1729
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001730bool WebRtcVoiceMediaChannel::SetPlayout(bool playout) {
1731 desired_playout_ = playout;
1732 return ChangePlayout(desired_playout_);
1733}
1734
1735bool WebRtcVoiceMediaChannel::PausePlayout() {
1736 return ChangePlayout(false);
1737}
1738
1739bool WebRtcVoiceMediaChannel::ResumePlayout() {
1740 return ChangePlayout(desired_playout_);
1741}
1742
1743bool WebRtcVoiceMediaChannel::ChangePlayout(bool playout) {
solenberg566ef242015-11-06 15:34:49 -08001744 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001745 if (playout_ == playout) {
1746 return true;
1747 }
1748
solenberg7add0582015-11-20 09:59:34 -08001749 for (const auto& ch : recv_streams_) {
Fredrik Solenbergaf9fb212015-08-26 10:45:53 +02001750 if (!SetPlayout(ch.second->channel(), playout)) {
henrike@webrtc.org1e09a712013-07-26 19:17:59 +00001751 LOG(LS_ERROR) << "SetPlayout " << playout << " on channel "
Fredrik Solenbergaf9fb212015-08-26 10:45:53 +02001752 << ch.second->channel() << " failed";
solenberg1ac56142015-10-13 03:58:19 -07001753 return false;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001754 }
1755 }
solenberg1ac56142015-10-13 03:58:19 -07001756 playout_ = playout;
1757 return true;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001758}
1759
1760bool WebRtcVoiceMediaChannel::SetSend(SendFlags send) {
1761 desired_send_ = send;
solenbergc96df772015-10-21 13:01:53 -07001762 if (!send_streams_.empty()) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001763 return ChangeSend(desired_send_);
solenbergc96df772015-10-21 13:01:53 -07001764 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001765 return true;
1766}
1767
1768bool WebRtcVoiceMediaChannel::PauseSend() {
1769 return ChangeSend(SEND_NOTHING);
1770}
1771
1772bool WebRtcVoiceMediaChannel::ResumeSend() {
1773 return ChangeSend(desired_send_);
1774}
1775
1776bool WebRtcVoiceMediaChannel::ChangeSend(SendFlags send) {
1777 if (send_ == send) {
1778 return true;
1779 }
1780
solenberg246b8172015-12-08 09:50:23 -08001781 // Apply channel specific options when channel is enabled for sending.
solenberg63b34542015-09-29 06:06:31 -07001782 if (send == SEND_MICROPHONE) {
1783 engine()->ApplyOptions(options_);
1784 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001785
wu@webrtc.org9dba5252013-08-05 20:36:57 +00001786 // Change the settings on each send channel.
solenbergc96df772015-10-21 13:01:53 -07001787 for (const auto& ch : send_streams_) {
solenberg63b34542015-09-29 06:06:31 -07001788 if (!ChangeSend(ch.second->channel(), send)) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001789 return false;
solenberg63b34542015-09-29 06:06:31 -07001790 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001791 }
wu@webrtc.org9dba5252013-08-05 20:36:57 +00001792
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001793 send_ = send;
1794 return true;
1795}
1796
wu@webrtc.org9dba5252013-08-05 20:36:57 +00001797bool WebRtcVoiceMediaChannel::ChangeSend(int channel, SendFlags send) {
1798 if (send == SEND_MICROPHONE) {
1799 if (engine()->voe()->base()->StartSend(channel) == -1) {
1800 LOG_RTCERR1(StartSend, channel);
1801 return false;
1802 }
wu@webrtc.org9dba5252013-08-05 20:36:57 +00001803 } else { // SEND_NOTHING
henrikg91d6ede2015-09-17 00:24:34 -07001804 RTC_DCHECK(send == SEND_NOTHING);
wu@webrtc.org9dba5252013-08-05 20:36:57 +00001805 if (engine()->voe()->base()->StopSend(channel) == -1) {
1806 LOG_RTCERR1(StopSend, channel);
1807 return false;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001808 }
1809 }
1810
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001811 return true;
1812}
1813
Peter Boström0c4e06b2015-10-07 12:23:21 +02001814bool WebRtcVoiceMediaChannel::SetAudioSend(uint32_t ssrc,
1815 bool enable,
solenberg1dd98f32015-09-10 01:57:14 -07001816 const AudioOptions* options,
1817 AudioRenderer* renderer) {
solenberg566ef242015-11-06 15:34:49 -08001818 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
solenberg1dd98f32015-09-10 01:57:14 -07001819 // TODO(solenberg): The state change should be fully rolled back if any one of
1820 // these calls fail.
1821 if (!SetLocalRenderer(ssrc, renderer)) {
1822 return false;
1823 }
solenbergdfc8f4f2015-10-01 02:31:10 -07001824 if (!MuteStream(ssrc, !enable)) {
solenberg1dd98f32015-09-10 01:57:14 -07001825 return false;
1826 }
solenbergdfc8f4f2015-10-01 02:31:10 -07001827 if (enable && options) {
solenberg1dd98f32015-09-10 01:57:14 -07001828 return SetOptions(*options);
1829 }
1830 return true;
1831}
1832
solenberg0a617e22015-10-20 15:49:38 -07001833int WebRtcVoiceMediaChannel::CreateVoEChannel() {
1834 int id = engine()->CreateVoEChannel();
1835 if (id == -1) {
1836 LOG_RTCERR0(CreateVoEChannel);
1837 return -1;
wu@webrtc.org9dba5252013-08-05 20:36:57 +00001838 }
solenberg0a617e22015-10-20 15:49:38 -07001839 if (engine()->voe()->network()->RegisterExternalTransport(id, *this) == -1) {
1840 LOG_RTCERR2(RegisterExternalTransport, id, this);
1841 engine()->voe()->base()->DeleteChannel(id);
1842 return -1;
1843 }
1844 return id;
wu@webrtc.org9dba5252013-08-05 20:36:57 +00001845}
1846
solenberg7add0582015-11-20 09:59:34 -08001847bool WebRtcVoiceMediaChannel::DeleteVoEChannel(int channel) {
wu@webrtc.org9dba5252013-08-05 20:36:57 +00001848 if (engine()->voe()->network()->DeRegisterExternalTransport(channel) == -1) {
1849 LOG_RTCERR1(DeRegisterExternalTransport, channel);
1850 }
wu@webrtc.org9dba5252013-08-05 20:36:57 +00001851 if (engine()->voe()->base()->DeleteChannel(channel) == -1) {
1852 LOG_RTCERR1(DeleteChannel, channel);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001853 return false;
1854 }
wu@webrtc.org9dba5252013-08-05 20:36:57 +00001855 return true;
1856}
henrike@webrtc.org1e09a712013-07-26 19:17:59 +00001857
wu@webrtc.org9dba5252013-08-05 20:36:57 +00001858bool WebRtcVoiceMediaChannel::AddSendStream(const StreamParams& sp) {
solenberg566ef242015-11-06 15:34:49 -08001859 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
solenberg0a617e22015-10-20 15:49:38 -07001860 LOG(LS_INFO) << "AddSendStream: " << sp.ToString();
1861
1862 uint32_t ssrc = sp.first_ssrc();
1863 RTC_DCHECK(0 != ssrc);
1864
1865 if (GetSendChannelId(ssrc) != -1) {
1866 LOG(LS_ERROR) << "Stream already exists with ssrc " << ssrc;
wu@webrtc.org9dba5252013-08-05 20:36:57 +00001867 return false;
1868 }
1869
solenberg0a617e22015-10-20 15:49:38 -07001870 // Create a new channel for sending audio data.
1871 int channel = CreateVoEChannel();
1872 if (channel == -1) {
1873 return false;
wu@webrtc.org9dba5252013-08-05 20:36:57 +00001874 }
wu@webrtc.org9dba5252013-08-05 20:36:57 +00001875
solenbergc96df772015-10-21 13:01:53 -07001876 // Save the channel to send_streams_, so that RemoveSendStream() can still
wu@webrtc.org9dba5252013-08-05 20:36:57 +00001877 // delete the channel in case failure happens below.
mallinath@webrtc.org67ee6b92014-02-03 16:57:16 +00001878 webrtc::AudioTransport* audio_transport =
1879 engine()->voe()->base()->audio_transport();
solenberg3a941542015-11-16 07:34:50 -08001880 send_streams_.insert(std::make_pair(ssrc, new WebRtcAudioSendStream(
1881 channel, audio_transport, ssrc, sp.cname, send_rtp_extensions_, call_)));
wu@webrtc.org9dba5252013-08-05 20:36:57 +00001882
solenberg0a617e22015-10-20 15:49:38 -07001883 // Set the current codecs to be used for the new channel. We need to do this
1884 // after adding the channel to send_channels_, because of how max bitrate is
1885 // currently being configured by SetSendCodec().
1886 if (!send_codecs_.empty() && !SetSendCodecs(channel, send_codecs_)) {
1887 RemoveSendStream(ssrc);
wu@webrtc.org9dba5252013-08-05 20:36:57 +00001888 return false;
1889 }
1890
1891 // At this point the channel's local SSRC has been updated. If the channel is
solenberg0a617e22015-10-20 15:49:38 -07001892 // the first send channel make sure that all the receive channels are updated
1893 // with the same SSRC in order to send receiver reports.
solenbergc96df772015-10-21 13:01:53 -07001894 if (send_streams_.size() == 1) {
solenberg0a617e22015-10-20 15:49:38 -07001895 receiver_reports_ssrc_ = ssrc;
solenberg7add0582015-11-20 09:59:34 -08001896 for (const auto& stream : recv_streams_) {
1897 int recv_channel = stream.second->channel();
solenberg0a617e22015-10-20 15:49:38 -07001898 if (engine()->voe()->rtp()->SetLocalSSRC(recv_channel, ssrc) != 0) {
solenberg7add0582015-11-20 09:59:34 -08001899 LOG_RTCERR2(SetLocalSSRC, recv_channel, ssrc);
solenberg1ac56142015-10-13 03:58:19 -07001900 return false;
wu@webrtc.org9dba5252013-08-05 20:36:57 +00001901 }
solenberg0a617e22015-10-20 15:49:38 -07001902 engine()->voe()->base()->AssociateSendChannel(recv_channel, channel);
1903 LOG(LS_INFO) << "VoiceEngine channel #" << recv_channel
1904 << " is associated with channel #" << channel << ".";
wu@webrtc.org9dba5252013-08-05 20:36:57 +00001905 }
1906 }
1907
wu@webrtc.org9dba5252013-08-05 20:36:57 +00001908 return ChangeSend(channel, desired_send_);
1909}
1910
Peter Boström0c4e06b2015-10-07 12:23:21 +02001911bool WebRtcVoiceMediaChannel::RemoveSendStream(uint32_t ssrc) {
solenberg566ef242015-11-06 15:34:49 -08001912 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
solenberg3a941542015-11-16 07:34:50 -08001913 LOG(LS_INFO) << "RemoveSendStream: " << ssrc;
1914
solenbergc96df772015-10-21 13:01:53 -07001915 auto it = send_streams_.find(ssrc);
1916 if (it == send_streams_.end()) {
wu@webrtc.org9dba5252013-08-05 20:36:57 +00001917 LOG(LS_WARNING) << "Try to remove stream with ssrc " << ssrc
1918 << " which doesn't exist.";
1919 return false;
1920 }
1921
mallinath@webrtc.org67ee6b92014-02-03 16:57:16 +00001922 int channel = it->second->channel();
wu@webrtc.org9dba5252013-08-05 20:36:57 +00001923 ChangeSend(channel, SEND_NOTHING);
1924
solenberg7add0582015-11-20 09:59:34 -08001925 // Clean up and delete the send stream+channel.
solenberg0a617e22015-10-20 15:49:38 -07001926 LOG(LS_INFO) << "Removing audio send stream " << ssrc
1927 << " with VoiceEngine channel #" << channel << ".";
solenberg7add0582015-11-20 09:59:34 -08001928 delete it->second;
1929 send_streams_.erase(it);
1930 if (!DeleteVoEChannel(channel)) {
solenberg0a617e22015-10-20 15:49:38 -07001931 return false;
wu@webrtc.org9dba5252013-08-05 20:36:57 +00001932 }
solenbergc96df772015-10-21 13:01:53 -07001933 if (send_streams_.empty()) {
wu@webrtc.org9dba5252013-08-05 20:36:57 +00001934 ChangeSend(SEND_NOTHING);
solenberg0a617e22015-10-20 15:49:38 -07001935 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001936 return true;
1937}
1938
1939bool WebRtcVoiceMediaChannel::AddRecvStream(const StreamParams& sp) {
solenberg566ef242015-11-06 15:34:49 -08001940 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
solenbergd97ec302015-10-07 01:40:33 -07001941 LOG(LS_INFO) << "AddRecvStream: " << sp.ToString();
1942
solenberg0b675462015-10-09 01:37:09 -07001943 if (!ValidateStreamParams(sp)) {
wu@webrtc.org78187522013-10-07 23:32:02 +00001944 return false;
1945 }
1946
solenberg7add0582015-11-20 09:59:34 -08001947 const uint32_t ssrc = sp.first_ssrc();
solenberg0b675462015-10-09 01:37:09 -07001948 if (ssrc == 0) {
1949 LOG(LS_WARNING) << "AddRecvStream with ssrc==0 is not supported.";
1950 return false;
1951 }
1952
solenberg1ac56142015-10-13 03:58:19 -07001953 // Remove the default receive stream if one had been created with this ssrc;
1954 // we'll recreate it then.
1955 if (IsDefaultRecvStream(ssrc)) {
1956 RemoveRecvStream(ssrc);
1957 }
solenberg0b675462015-10-09 01:37:09 -07001958
solenberg7add0582015-11-20 09:59:34 -08001959 if (GetReceiveChannelId(ssrc) != -1) {
henrike@webrtc.org1e09a712013-07-26 19:17:59 +00001960 LOG(LS_ERROR) << "Stream already exists with ssrc " << ssrc;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001961 return false;
1962 }
Fredrik Solenberg4b60c732015-05-07 14:07:48 +02001963
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001964 // Create a new channel for receiving audio data.
solenberg7add0582015-11-20 09:59:34 -08001965 const int channel = CreateVoEChannel();
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001966 if (channel == -1) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001967 return false;
1968 }
Minyue2013aec2015-05-13 14:14:42 +02001969
solenberg1ac56142015-10-13 03:58:19 -07001970 // Turn off all supported codecs.
solenberg26c8c912015-11-27 04:00:25 -08001971 // TODO(solenberg): Remove once "no codecs" is the default state of a stream.
1972 for (webrtc::CodecInst voe_codec : webrtc::acm2::RentACodec::Database()) {
1973 voe_codec.pltype = -1;
1974 if (engine()->voe()->codec()->SetRecPayloadType(channel, voe_codec) == -1) {
1975 LOG_RTCERR2(SetRecPayloadType, channel, ToString(voe_codec));
1976 DeleteVoEChannel(channel);
1977 return false;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001978 }
1979 }
1980
solenberg1ac56142015-10-13 03:58:19 -07001981 // Only enable those configured for this channel.
1982 for (const auto& codec : recv_codecs_) {
1983 webrtc::CodecInst voe_codec;
solenberg26c8c912015-11-27 04:00:25 -08001984 if (WebRtcVoiceEngine::ToCodecInst(codec, &voe_codec)) {
solenberg1ac56142015-10-13 03:58:19 -07001985 voe_codec.pltype = codec.id;
1986 if (engine()->voe()->codec()->SetRecPayloadType(
1987 channel, voe_codec) == -1) {
1988 LOG_RTCERR2(SetRecPayloadType, channel, ToString(voe_codec));
solenberg7add0582015-11-20 09:59:34 -08001989 DeleteVoEChannel(channel);
solenberg1ac56142015-10-13 03:58:19 -07001990 return false;
1991 }
henrike@webrtc.org1e09a712013-07-26 19:17:59 +00001992 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001993 }
solenberg8fb30c32015-10-13 03:06:58 -07001994
solenberg7add0582015-11-20 09:59:34 -08001995 const int send_channel = GetSendChannelId(receiver_reports_ssrc_);
1996 if (send_channel != -1) {
1997 // Associate receive channel with first send channel (so the receive channel
1998 // can obtain RTT from the send channel)
1999 engine()->voe()->base()->AssociateSendChannel(channel, send_channel);
2000 LOG(LS_INFO) << "VoiceEngine channel #" << channel
2001 << " is associated with channel #" << send_channel << ".";
buildbot@webrtc.org150835e2014-05-06 15:54:38 +00002002 }
2003
solenberg7add0582015-11-20 09:59:34 -08002004 recv_streams_.insert(std::make_pair(ssrc, new WebRtcAudioReceiveStream(
2005 channel, ssrc, receiver_reports_ssrc_,
2006 options_.combined_audio_video_bwe.value_or(false), sp.sync_label,
2007 recv_rtp_extensions_, call_)));
2008
2009 SetNack(channel, nack_enabled_);
solenberg1ac56142015-10-13 03:58:19 -07002010 SetPlayout(channel, playout_);
solenberg7add0582015-11-20 09:59:34 -08002011
solenberg1ac56142015-10-13 03:58:19 -07002012 return true;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002013}
2014
Peter Boström0c4e06b2015-10-07 12:23:21 +02002015bool WebRtcVoiceMediaChannel::RemoveRecvStream(uint32_t ssrc) {
solenberg566ef242015-11-06 15:34:49 -08002016 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
solenbergd97ec302015-10-07 01:40:33 -07002017 LOG(LS_INFO) << "RemoveRecvStream: " << ssrc;
2018
solenberg7add0582015-11-20 09:59:34 -08002019 const auto it = recv_streams_.find(ssrc);
2020 if (it == recv_streams_.end()) {
wu@webrtc.org9dba5252013-08-05 20:36:57 +00002021 LOG(LS_WARNING) << "Try to remove stream with ssrc " << ssrc
2022 << " which doesn't exist.";
henrike@webrtc.org1e09a712013-07-26 19:17:59 +00002023 return false;
wu@webrtc.org9dba5252013-08-05 20:36:57 +00002024 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002025
solenberg1ac56142015-10-13 03:58:19 -07002026 // Deregister default channel, if that's the one being destroyed.
2027 if (IsDefaultRecvStream(ssrc)) {
2028 default_recv_ssrc_ = -1;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002029 }
henrike@webrtc.org1e09a712013-07-26 19:17:59 +00002030
solenberg7add0582015-11-20 09:59:34 -08002031 const int channel = it->second->channel();
2032
2033 // Clean up and delete the receive stream+channel.
2034 LOG(LS_INFO) << "Removing audio receive stream " << ssrc
mallinath@webrtc.org67ee6b92014-02-03 16:57:16 +00002035 << " with VoiceEngine channel #" << channel << ".";
Tommif888bb52015-12-12 01:37:01 +01002036 it->second->SetRawAudioSink(nullptr);
solenberg7add0582015-11-20 09:59:34 -08002037 delete it->second;
2038 recv_streams_.erase(it);
2039 return DeleteVoEChannel(channel);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002040}
2041
Peter Boström0c4e06b2015-10-07 12:23:21 +02002042bool WebRtcVoiceMediaChannel::SetLocalRenderer(uint32_t ssrc,
henrike@webrtc.org1e09a712013-07-26 19:17:59 +00002043 AudioRenderer* renderer) {
solenbergc96df772015-10-21 13:01:53 -07002044 auto it = send_streams_.find(ssrc);
2045 if (it == send_streams_.end()) {
wu@webrtc.org9dba5252013-08-05 20:36:57 +00002046 if (renderer) {
2047 // Return an error if trying to set a valid renderer with an invalid ssrc.
2048 LOG(LS_ERROR) << "SetLocalRenderer failed with ssrc "<< ssrc;
2049 return false;
2050 }
2051
2052 // The channel likely has gone away, do nothing.
henrike@webrtc.org1e09a712013-07-26 19:17:59 +00002053 return true;
henrike@webrtc.org1e09a712013-07-26 19:17:59 +00002054 }
2055
solenberg1ac56142015-10-13 03:58:19 -07002056 if (renderer) {
mallinath@webrtc.org67ee6b92014-02-03 16:57:16 +00002057 it->second->Start(renderer);
solenberg1ac56142015-10-13 03:58:19 -07002058 } else {
mallinath@webrtc.org67ee6b92014-02-03 16:57:16 +00002059 it->second->Stop();
solenberg1ac56142015-10-13 03:58:19 -07002060 }
henrike@webrtc.org1e09a712013-07-26 19:17:59 +00002061
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002062 return true;
2063}
2064
2065bool WebRtcVoiceMediaChannel::GetActiveStreams(
2066 AudioInfo::StreamList* actives) {
solenberg566ef242015-11-06 15:34:49 -08002067 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002068 actives->clear();
solenberg7add0582015-11-20 09:59:34 -08002069 for (const auto& ch : recv_streams_) {
Fredrik Solenbergaf9fb212015-08-26 10:45:53 +02002070 int level = GetOutputLevel(ch.second->channel());
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002071 if (level > 0) {
Fredrik Solenbergaf9fb212015-08-26 10:45:53 +02002072 actives->push_back(std::make_pair(ch.first, level));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002073 }
2074 }
2075 return true;
2076}
2077
2078int WebRtcVoiceMediaChannel::GetOutputLevel() {
solenberg566ef242015-11-06 15:34:49 -08002079 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
solenberg1ac56142015-10-13 03:58:19 -07002080 int highest = 0;
solenberg7add0582015-11-20 09:59:34 -08002081 for (const auto& ch : recv_streams_) {
solenberg8fb30c32015-10-13 03:06:58 -07002082 highest = std::max(GetOutputLevel(ch.second->channel()), highest);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002083 }
2084 return highest;
2085}
2086
2087int WebRtcVoiceMediaChannel::GetTimeSinceLastTyping() {
2088 int ret;
2089 if (engine()->voe()->processing()->TimeSinceLastTyping(ret) == -1) {
2090 // In case of error, log the info and continue
2091 LOG_RTCERR0(TimeSinceLastTyping);
2092 ret = -1;
2093 } else {
2094 ret *= 1000; // We return ms, webrtc returns seconds.
2095 }
2096 return ret;
2097}
2098
2099void WebRtcVoiceMediaChannel::SetTypingDetectionParameters(int time_window,
2100 int cost_per_typing, int reporting_threshold, int penalty_decay,
2101 int type_event_delay) {
2102 if (engine()->voe()->processing()->SetTypingDetectionParameters(
2103 time_window, cost_per_typing,
2104 reporting_threshold, penalty_decay, type_event_delay) == -1) {
2105 // In case of error, log the info and continue
2106 LOG_RTCERR5(SetTypingDetectionParameters, time_window,
2107 cost_per_typing, reporting_threshold, penalty_decay,
2108 type_event_delay);
2109 }
2110}
2111
solenberg4bac9c52015-10-09 02:32:53 -07002112bool WebRtcVoiceMediaChannel::SetOutputVolume(uint32_t ssrc, double volume) {
solenberg566ef242015-11-06 15:34:49 -08002113 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
solenberg1ac56142015-10-13 03:58:19 -07002114 if (ssrc == 0) {
2115 default_recv_volume_ = volume;
2116 if (default_recv_ssrc_ == -1) {
2117 return true;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002118 }
solenberg1ac56142015-10-13 03:58:19 -07002119 ssrc = static_cast<uint32_t>(default_recv_ssrc_);
2120 }
2121 int ch_id = GetReceiveChannelId(ssrc);
2122 if (ch_id < 0) {
2123 LOG(LS_WARNING) << "Cannot find channel for ssrc:" << ssrc;
2124 return false;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002125 }
2126
solenberg1ac56142015-10-13 03:58:19 -07002127 if (-1 == engine()->voe()->volume()->SetChannelOutputVolumeScaling(ch_id,
2128 volume)) {
2129 LOG_RTCERR2(SetChannelOutputVolumeScaling, ch_id, volume);
2130 return false;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002131 }
solenberg1ac56142015-10-13 03:58:19 -07002132 LOG(LS_INFO) << "SetOutputVolume to " << volume
2133 << " for channel " << ch_id << " and ssrc " << ssrc;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002134 return true;
2135}
2136
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002137bool WebRtcVoiceMediaChannel::CanInsertDtmf() {
Fredrik Solenbergb5727682015-12-04 15:22:19 +01002138 return dtmf_payload_type_ ? true : false;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002139}
2140
solenberg1d63dd02015-12-02 12:35:09 -08002141bool WebRtcVoiceMediaChannel::InsertDtmf(uint32_t ssrc, int event,
2142 int duration) {
solenberg566ef242015-11-06 15:34:49 -08002143 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
Fredrik Solenbergb5727682015-12-04 15:22:19 +01002144 LOG(LS_INFO) << "WebRtcVoiceMediaChannel::InsertDtmf";
2145 if (!dtmf_payload_type_) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002146 return false;
2147 }
2148
Fredrik Solenbergb5727682015-12-04 15:22:19 +01002149 // Figure out which WebRtcAudioSendStream to send the event on.
2150 auto it = ssrc != 0 ? send_streams_.find(ssrc) : send_streams_.begin();
2151 if (it == send_streams_.end()) {
2152 LOG(LS_WARNING) << "The specified ssrc " << ssrc << " is not in use.";
solenberg1d63dd02015-12-02 12:35:09 -08002153 return false;
2154 }
Fredrik Solenbergb5727682015-12-04 15:22:19 +01002155 if (event < kMinTelephoneEventCode ||
2156 event > kMaxTelephoneEventCode) {
2157 LOG(LS_WARNING) << "DTMF event code " << event << " out of range.";
solenberg1d63dd02015-12-02 12:35:09 -08002158 return false;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002159 }
Fredrik Solenbergb5727682015-12-04 15:22:19 +01002160 if (duration < kMinTelephoneEventDuration ||
2161 duration > kMaxTelephoneEventDuration) {
2162 LOG(LS_WARNING) << "DTMF event duration " << duration << " out of range.";
2163 return false;
2164 }
2165 return it->second->SendTelephoneEvent(*dtmf_payload_type_, event, duration);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002166}
2167
wu@webrtc.orga9890802013-12-13 00:21:03 +00002168void WebRtcVoiceMediaChannel::OnPacketReceived(
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00002169 rtc::Buffer* packet, const rtc::PacketTime& packet_time) {
solenberg566ef242015-11-06 15:34:49 -08002170 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
Fredrik Solenberg4b60c732015-05-07 14:07:48 +02002171
solenberg1ac56142015-10-13 03:58:19 -07002172 uint32_t ssrc = 0;
2173 if (!GetRtpSsrc(packet->data(), packet->size(), &ssrc)) {
2174 return;
2175 }
2176
solenberg7e63ef02015-11-20 00:19:43 -08002177 // If we don't have a default channel, and the SSRC is unknown, create a
2178 // default channel.
2179 if (default_recv_ssrc_ == -1 && GetReceiveChannelId(ssrc) == -1) {
solenberg1ac56142015-10-13 03:58:19 -07002180 StreamParams sp;
2181 sp.ssrcs.push_back(ssrc);
2182 LOG(LS_INFO) << "Creating default receive stream for SSRC=" << ssrc << ".";
2183 if (!AddRecvStream(sp)) {
2184 LOG(LS_WARNING) << "Could not create default receive stream.";
2185 return;
2186 }
2187 default_recv_ssrc_ = ssrc;
2188 SetOutputVolume(default_recv_ssrc_, default_recv_volume_);
2189 }
2190
2191 // Forward packet to Call. If the SSRC is unknown we'll return after this.
Fredrik Solenberg709ed672015-09-15 12:26:33 +02002192 const webrtc::PacketTime webrtc_packet_time(packet_time.timestamp,
2193 packet_time.not_before);
solenberg1ac56142015-10-13 03:58:19 -07002194 webrtc::PacketReceiver::DeliveryStatus delivery_result =
2195 call_->Receiver()->DeliverPacket(webrtc::MediaType::AUDIO,
2196 reinterpret_cast<const uint8_t*>(packet->data()), packet->size(),
2197 webrtc_packet_time);
2198 if (webrtc::PacketReceiver::DELIVERY_OK != delivery_result) {
solenberg7e63ef02015-11-20 00:19:43 -08002199 // If the SSRC is unknown here, route it to the default channel, if we have
2200 // one. See: https://bugs.chromium.org/p/webrtc/issues/detail?id=5208
2201 if (default_recv_ssrc_ == -1) {
2202 return;
2203 } else {
2204 ssrc = default_recv_ssrc_;
2205 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002206 }
2207
solenberg1ac56142015-10-13 03:58:19 -07002208 // Find the channel to send this packet to. It must exist since webrtc::Call
2209 // was able to demux the packet.
2210 int channel = GetReceiveChannelId(ssrc);
2211 RTC_DCHECK(channel != -1);
2212
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002213 // Pass it off to the decoder.
henrike@webrtc.org28654cb2013-07-22 21:07:49 +00002214 engine()->voe()->network()->ReceivedRTPPacket(
solenberg1ac56142015-10-13 03:58:19 -07002215 channel, packet->data(), packet->size(), webrtc_packet_time);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002216}
2217
wu@webrtc.orga9890802013-12-13 00:21:03 +00002218void WebRtcVoiceMediaChannel::OnRtcpReceived(
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00002219 rtc::Buffer* packet, const rtc::PacketTime& packet_time) {
solenberg566ef242015-11-06 15:34:49 -08002220 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
Fredrik Solenberg4b60c732015-05-07 14:07:48 +02002221
Fredrik Solenberg709ed672015-09-15 12:26:33 +02002222 // Forward packet to Call as well.
2223 const webrtc::PacketTime webrtc_packet_time(packet_time.timestamp,
2224 packet_time.not_before);
2225 call_->Receiver()->DeliverPacket(webrtc::MediaType::AUDIO,
2226 reinterpret_cast<const uint8_t*>(packet->data()), packet->size(),
2227 webrtc_packet_time);
Fredrik Solenberg4b60c732015-05-07 14:07:48 +02002228
wu@webrtc.org9dba5252013-08-05 20:36:57 +00002229 // Sending channels need all RTCP packets with feedback information.
2230 // Even sender reports can contain attached report blocks.
2231 // Receiving channels need sender reports in order to create
2232 // correct receiver reports.
2233 int type = 0;
kwiberg@webrtc.orgeebcab52015-03-24 09:19:06 +00002234 if (!GetRtcpType(packet->data(), packet->size(), &type)) {
wu@webrtc.org9dba5252013-08-05 20:36:57 +00002235 LOG(LS_WARNING) << "Failed to parse type from received RTCP packet";
2236 return;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002237 }
2238
solenberg0b675462015-10-09 01:37:09 -07002239 // If it is a sender report, find the receive channel that is listening.
wu@webrtc.org9dba5252013-08-05 20:36:57 +00002240 if (type == kRtcpTypeSR) {
solenberg0b675462015-10-09 01:37:09 -07002241 uint32_t ssrc = 0;
2242 if (!GetRtcpSsrc(packet->data(), packet->size(), &ssrc)) {
2243 return;
2244 }
2245 int recv_channel_id = GetReceiveChannelId(ssrc);
2246 if (recv_channel_id != -1) {
wu@webrtc.org9dba5252013-08-05 20:36:57 +00002247 engine()->voe()->network()->ReceivedRTCPPacket(
solenberg0b675462015-10-09 01:37:09 -07002248 recv_channel_id, packet->data(), packet->size());
wu@webrtc.org9dba5252013-08-05 20:36:57 +00002249 }
2250 }
2251
2252 // SR may continue RR and any RR entry may correspond to any one of the send
2253 // channels. So all RTCP packets must be forwarded all send channels. VoE
2254 // will filter out RR internally.
solenbergc96df772015-10-21 13:01:53 -07002255 for (const auto& ch : send_streams_) {
wu@webrtc.org9dba5252013-08-05 20:36:57 +00002256 engine()->voe()->network()->ReceivedRTCPPacket(
Fredrik Solenbergaf9fb212015-08-26 10:45:53 +02002257 ch.second->channel(), packet->data(), packet->size());
wu@webrtc.org9dba5252013-08-05 20:36:57 +00002258 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002259}
2260
Peter Boström0c4e06b2015-10-07 12:23:21 +02002261bool WebRtcVoiceMediaChannel::MuteStream(uint32_t ssrc, bool muted) {
solenberg566ef242015-11-06 15:34:49 -08002262 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
solenberg0a617e22015-10-20 15:49:38 -07002263 int channel = GetSendChannelId(ssrc);
wu@webrtc.org9dba5252013-08-05 20:36:57 +00002264 if (channel == -1) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002265 LOG(LS_WARNING) << "The specified ssrc " << ssrc << " is not in use.";
2266 return false;
2267 }
wu@webrtc.org9dba5252013-08-05 20:36:57 +00002268 if (engine()->voe()->volume()->SetInputMute(channel, muted) == -1) {
2269 LOG_RTCERR2(SetInputMute, channel, muted);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002270 return false;
2271 }
buildbot@webrtc.org6b21b712014-07-31 15:08:53 +00002272 // We set the AGC to mute state only when all the channels are muted.
2273 // This implementation is not ideal, instead we should signal the AGC when
2274 // the mic channel is muted/unmuted. We can't do it today because there
2275 // is no good way to know which stream is mapping to the mic channel.
2276 bool all_muted = muted;
solenbergc96df772015-10-21 13:01:53 -07002277 for (const auto& ch : send_streams_) {
Fredrik Solenbergaf9fb212015-08-26 10:45:53 +02002278 if (!all_muted) {
2279 break;
2280 }
2281 if (engine()->voe()->volume()->GetInputMute(ch.second->channel(),
buildbot@webrtc.org6b21b712014-07-31 15:08:53 +00002282 all_muted)) {
Fredrik Solenbergaf9fb212015-08-26 10:45:53 +02002283 LOG_RTCERR1(GetInputMute, ch.second->channel());
buildbot@webrtc.org6b21b712014-07-31 15:08:53 +00002284 return false;
2285 }
2286 }
2287
2288 webrtc::AudioProcessing* ap = engine()->voe()->base()->audio_processing();
solenberg0a617e22015-10-20 15:49:38 -07002289 if (ap) {
buildbot@webrtc.org6b21b712014-07-31 15:08:53 +00002290 ap->set_output_will_be_muted(all_muted);
solenberg0a617e22015-10-20 15:49:38 -07002291 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002292 return true;
2293}
2294
minyue@webrtc.org26236952014-10-29 02:27:08 +00002295// TODO(minyue): SetMaxSendBandwidth() is subject to be renamed to
2296// SetMaxSendBitrate() in future.
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00002297bool WebRtcVoiceMediaChannel::SetMaxSendBandwidth(int bps) {
minyue@webrtc.org26236952014-10-29 02:27:08 +00002298 LOG(LS_INFO) << "WebRtcVoiceMediaChannel::SetMaxSendBandwidth.";
minyue@webrtc.org26236952014-10-29 02:27:08 +00002299 return SetSendBitrateInternal(bps);
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00002300}
2301
minyue@webrtc.org26236952014-10-29 02:27:08 +00002302bool WebRtcVoiceMediaChannel::SetSendBitrateInternal(int bps) {
2303 LOG(LS_INFO) << "WebRtcVoiceMediaChannel::SetSendBitrateInternal.";
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00002304
minyue@webrtc.org26236952014-10-29 02:27:08 +00002305 send_bitrate_setting_ = true;
2306 send_bitrate_bps_ = bps;
wu@webrtc.org1d1ffc92013-10-16 18:12:02 +00002307
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002308 if (!send_codec_) {
wu@webrtc.org1d1ffc92013-10-16 18:12:02 +00002309 LOG(LS_INFO) << "The send codec has not been set up yet. "
minyue@webrtc.org26236952014-10-29 02:27:08 +00002310 << "The send bitrate setting will be applied later.";
wu@webrtc.org1d1ffc92013-10-16 18:12:02 +00002311 return true;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002312 }
2313
minyue@webrtc.org26236952014-10-29 02:27:08 +00002314 // Bitrate is auto by default.
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00002315 // TODO(bemasc): Fix this so that if SetMaxSendBandwidth(50) is followed by
2316 // SetMaxSendBandwith(0), the second call removes the previous limit.
2317 if (bps <= 0)
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002318 return true;
2319
2320 webrtc::CodecInst codec = *send_codec_;
solenberg26c8c912015-11-27 04:00:25 -08002321 bool is_multi_rate = WebRtcVoiceCodecs::IsCodecMultiRate(codec);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002322
2323 if (is_multi_rate) {
2324 // If codec is multi-rate then just set the bitrate.
2325 codec.rate = bps;
solenbergc96df772015-10-21 13:01:53 -07002326 for (const auto& ch : send_streams_) {
solenberg0a617e22015-10-20 15:49:38 -07002327 if (!SetSendCodec(ch.second->channel(), codec)) {
2328 LOG(LS_INFO) << "Failed to set codec " << codec.plname
2329 << " to bitrate " << bps << " bps.";
2330 return false;
2331 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002332 }
2333 return true;
2334 } else {
2335 // If codec is not multi-rate and |bps| is less than the fixed bitrate
2336 // then fail. If codec is not multi-rate and |bps| exceeds or equal the
2337 // fixed bitrate then ignore.
2338 if (bps < codec.rate) {
2339 LOG(LS_INFO) << "Failed to set codec " << codec.plname
2340 << " to bitrate " << bps << " bps"
2341 << ", requires at least " << codec.rate << " bps.";
2342 return false;
2343 }
2344 return true;
2345 }
2346}
2347
2348bool WebRtcVoiceMediaChannel::GetStats(VoiceMediaInfo* info) {
solenberg566ef242015-11-06 15:34:49 -08002349 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
solenberg85a04962015-10-27 03:35:21 -07002350 RTC_DCHECK(info);
solenbergd97ec302015-10-07 01:40:33 -07002351
solenberg85a04962015-10-27 03:35:21 -07002352 // Get SSRC and stats for each sender.
2353 RTC_DCHECK(info->senders.size() == 0);
2354 for (const auto& stream : send_streams_) {
2355 webrtc::AudioSendStream::Stats stats = stream.second->GetStats();
wu@webrtc.org9dba5252013-08-05 20:36:57 +00002356 VoiceSenderInfo sinfo;
solenberg85a04962015-10-27 03:35:21 -07002357 sinfo.add_ssrc(stats.local_ssrc);
2358 sinfo.bytes_sent = stats.bytes_sent;
2359 sinfo.packets_sent = stats.packets_sent;
2360 sinfo.packets_lost = stats.packets_lost;
2361 sinfo.fraction_lost = stats.fraction_lost;
2362 sinfo.codec_name = stats.codec_name;
2363 sinfo.ext_seqnum = stats.ext_seqnum;
2364 sinfo.jitter_ms = stats.jitter_ms;
2365 sinfo.rtt_ms = stats.rtt_ms;
2366 sinfo.audio_level = stats.audio_level;
2367 sinfo.aec_quality_min = stats.aec_quality_min;
2368 sinfo.echo_delay_median_ms = stats.echo_delay_median_ms;
2369 sinfo.echo_delay_std_ms = stats.echo_delay_std_ms;
2370 sinfo.echo_return_loss = stats.echo_return_loss;
2371 sinfo.echo_return_loss_enhancement = stats.echo_return_loss_enhancement;
solenberg566ef242015-11-06 15:34:49 -08002372 sinfo.typing_noise_detected =
2373 (send_ == SEND_NOTHING ? false : stats.typing_noise_detected);
wu@webrtc.org9dba5252013-08-05 20:36:57 +00002374 info->senders.push_back(sinfo);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002375 }
2376
solenberg85a04962015-10-27 03:35:21 -07002377 // Get SSRC and stats for each receiver.
2378 RTC_DCHECK(info->receivers.size() == 0);
solenberg7add0582015-11-20 09:59:34 -08002379 for (const auto& stream : recv_streams_) {
Fredrik Solenberg4f4ec0a2015-10-22 10:49:27 +02002380 webrtc::AudioReceiveStream::Stats stats = stream.second->GetStats();
2381 VoiceReceiverInfo rinfo;
2382 rinfo.add_ssrc(stats.remote_ssrc);
2383 rinfo.bytes_rcvd = stats.bytes_rcvd;
2384 rinfo.packets_rcvd = stats.packets_rcvd;
2385 rinfo.packets_lost = stats.packets_lost;
2386 rinfo.fraction_lost = stats.fraction_lost;
2387 rinfo.codec_name = stats.codec_name;
2388 rinfo.ext_seqnum = stats.ext_seqnum;
2389 rinfo.jitter_ms = stats.jitter_ms;
2390 rinfo.jitter_buffer_ms = stats.jitter_buffer_ms;
2391 rinfo.jitter_buffer_preferred_ms = stats.jitter_buffer_preferred_ms;
2392 rinfo.delay_estimate_ms = stats.delay_estimate_ms;
2393 rinfo.audio_level = stats.audio_level;
2394 rinfo.expand_rate = stats.expand_rate;
2395 rinfo.speech_expand_rate = stats.speech_expand_rate;
2396 rinfo.secondary_decoded_rate = stats.secondary_decoded_rate;
2397 rinfo.accelerate_rate = stats.accelerate_rate;
2398 rinfo.preemptive_expand_rate = stats.preemptive_expand_rate;
2399 rinfo.decoding_calls_to_silence_generator =
2400 stats.decoding_calls_to_silence_generator;
2401 rinfo.decoding_calls_to_neteq = stats.decoding_calls_to_neteq;
2402 rinfo.decoding_normal = stats.decoding_normal;
2403 rinfo.decoding_plc = stats.decoding_plc;
2404 rinfo.decoding_cng = stats.decoding_cng;
2405 rinfo.decoding_plc_cng = stats.decoding_plc_cng;
2406 rinfo.capture_start_ntp_time_ms = stats.capture_start_ntp_time_ms;
2407 info->receivers.push_back(rinfo);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002408 }
2409
2410 return true;
2411}
2412
Tommif888bb52015-12-12 01:37:01 +01002413void WebRtcVoiceMediaChannel::SetRawAudioSink(
2414 uint32_t ssrc,
deadbeef2d110be2016-01-13 12:00:26 -08002415 rtc::scoped_ptr<webrtc::AudioSinkInterface> sink) {
Tommif888bb52015-12-12 01:37:01 +01002416 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
2417 LOG(LS_VERBOSE) << "WebRtcVoiceMediaChannel::SetRawAudioSink";
2418 const auto it = recv_streams_.find(ssrc);
2419 if (it == recv_streams_.end()) {
2420 LOG(LS_WARNING) << "SetRawAudioSink: no recv stream" << ssrc;
2421 return;
2422 }
deadbeef2d110be2016-01-13 12:00:26 -08002423 it->second->SetRawAudioSink(std::move(sink));
Tommif888bb52015-12-12 01:37:01 +01002424}
2425
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002426int WebRtcVoiceMediaChannel::GetOutputLevel(int channel) {
solenbergd97ec302015-10-07 01:40:33 -07002427 unsigned int ulevel = 0;
2428 int ret = engine()->voe()->volume()->GetSpeechOutputLevel(channel, ulevel);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002429 return (ret == 0) ? static_cast<int>(ulevel) : -1;
2430}
2431
Peter Boström0c4e06b2015-10-07 12:23:21 +02002432int WebRtcVoiceMediaChannel::GetReceiveChannelId(uint32_t ssrc) const {
solenberg566ef242015-11-06 15:34:49 -08002433 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
solenberg7add0582015-11-20 09:59:34 -08002434 const auto it = recv_streams_.find(ssrc);
2435 if (it != recv_streams_.end()) {
mallinath@webrtc.org67ee6b92014-02-03 16:57:16 +00002436 return it->second->channel();
solenberg8fb30c32015-10-13 03:06:58 -07002437 }
solenberg1ac56142015-10-13 03:58:19 -07002438 return -1;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002439}
2440
Peter Boström0c4e06b2015-10-07 12:23:21 +02002441int WebRtcVoiceMediaChannel::GetSendChannelId(uint32_t ssrc) const {
solenberg566ef242015-11-06 15:34:49 -08002442 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
solenbergc96df772015-10-21 13:01:53 -07002443 const auto it = send_streams_.find(ssrc);
2444 if (it != send_streams_.end()) {
mallinath@webrtc.org67ee6b92014-02-03 16:57:16 +00002445 return it->second->channel();
solenberg8fb30c32015-10-13 03:06:58 -07002446 }
wu@webrtc.org9dba5252013-08-05 20:36:57 +00002447 return -1;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002448}
2449
2450bool WebRtcVoiceMediaChannel::GetRedSendCodec(const AudioCodec& red_codec,
2451 const std::vector<AudioCodec>& all_codecs, webrtc::CodecInst* send_codec) {
2452 // Get the RED encodings from the parameter with no name. This may
2453 // change based on what is discussed on the Jingle list.
2454 // The encoding parameter is of the form "a/b"; we only support where
2455 // a == b. Verify this and parse out the value into red_pt.
2456 // If the parameter value is absent (as it will be until we wire up the
2457 // signaling of this message), use the second codec specified (i.e. the
2458 // one after "red") as the encoding parameter.
2459 int red_pt = -1;
2460 std::string red_params;
2461 CodecParameterMap::const_iterator it = red_codec.params.find("");
2462 if (it != red_codec.params.end()) {
2463 red_params = it->second;
2464 std::vector<std::string> red_pts;
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00002465 if (rtc::split(red_params, '/', &red_pts) != 2 ||
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002466 red_pts[0] != red_pts[1] ||
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00002467 !rtc::FromString(red_pts[0], &red_pt)) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002468 LOG(LS_WARNING) << "RED params " << red_params << " not supported.";
2469 return false;
2470 }
2471 } else if (red_codec.params.empty()) {
2472 LOG(LS_WARNING) << "RED params not present, using defaults";
2473 if (all_codecs.size() > 1) {
2474 red_pt = all_codecs[1].id;
2475 }
2476 }
2477
2478 // Try to find red_pt in |codecs|.
Fredrik Solenbergaf9fb212015-08-26 10:45:53 +02002479 for (const AudioCodec& codec : all_codecs) {
2480 if (codec.id == red_pt) {
2481 // If we find the right codec, that will be the codec we pass to
2482 // SetSendCodec, with the desired payload type.
solenberg26c8c912015-11-27 04:00:25 -08002483 if (WebRtcVoiceEngine::ToCodecInst(codec, send_codec)) {
Fredrik Solenbergaf9fb212015-08-26 10:45:53 +02002484 return true;
2485 } else {
2486 break;
2487 }
2488 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002489 }
Fredrik Solenbergaf9fb212015-08-26 10:45:53 +02002490 LOG(LS_WARNING) << "RED params " << red_params << " are invalid.";
2491 return false;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002492}
2493
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002494bool WebRtcVoiceMediaChannel::SetPlayout(int channel, bool playout) {
2495 if (playout) {
2496 LOG(LS_INFO) << "Starting playout for channel #" << channel;
2497 if (engine()->voe()->base()->StartPlayout(channel) == -1) {
2498 LOG_RTCERR1(StartPlayout, channel);
2499 return false;
2500 }
2501 } else {
2502 LOG(LS_INFO) << "Stopping playout for channel #" << channel;
2503 engine()->voe()->base()->StopPlayout(channel);
2504 }
2505 return true;
2506}
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002507} // namespace cricket
2508
2509#endif // HAVE_WEBRTC_VOICE