blob: 2634a24630741224d85c1a5006811b297ecfa394 [file] [log] [blame]
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001/*
2 * libjingle
3 * Copyright 2004 Google Inc.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions are met:
7 *
8 * 1. Redistributions of source code must retain the above copyright notice,
9 * this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright notice,
11 * this list of conditions and the following disclaimer in the documentation
12 * and/or other materials provided with the distribution.
13 * 3. The name of the author may not be used to endorse or promote products
14 * derived from this software without specific prior written permission.
15 *
16 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED
17 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
18 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO
19 * EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
20 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
21 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
22 * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
23 * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
24 * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
25 * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26 */
27
28#ifdef HAVE_CONFIG_H
29#include <config.h>
30#endif
31
32#ifdef HAVE_WEBRTC_VOICE
33
34#include "talk/media/webrtc/webrtcvoiceengine.h"
35
36#include <algorithm>
37#include <cstdio>
38#include <string>
39#include <vector>
40
Thiago Farinaef883092015-04-06 10:36:41 +000041#include "talk/media/base/audioframe.h"
buildbot@webrtc.orga09a9992014-08-13 17:26:08 +000042#include "talk/media/base/audiorenderer.h"
43#include "talk/media/base/constants.h"
44#include "talk/media/base/streamparams.h"
solenberg7e4e01a2015-12-02 08:05:01 -080045#include "talk/media/webrtc/webrtcmediaengine.h"
buildbot@webrtc.orga09a9992014-08-13 17:26:08 +000046#include "talk/media/webrtc/webrtcvoe.h"
Tommif888bb52015-12-12 01:37:01 +010047#include "webrtc/audio/audio_sink.h"
tfarina5237aaf2015-11-10 23:44:30 -080048#include "webrtc/base/arraysize.h"
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +000049#include "webrtc/base/base64.h"
50#include "webrtc/base/byteorder.h"
51#include "webrtc/base/common.h"
52#include "webrtc/base/helpers.h"
53#include "webrtc/base/logging.h"
54#include "webrtc/base/stringencode.h"
55#include "webrtc/base/stringutils.h"
ivoc112a3d82015-10-16 02:22:18 -070056#include "webrtc/call/rtc_event_log.h"
mallinath@webrtc.orga27be8e2013-09-27 23:04:10 +000057#include "webrtc/common.h"
solenberg26c8c912015-11-27 04:00:25 -080058#include "webrtc/modules/audio_coding/acm2/rent_a_codec.h"
henrike@webrtc.org28e20752013-07-10 00:45:36 +000059#include "webrtc/modules/audio_processing/include/audio_processing.h"
Henrik Kjellander98f53512015-10-28 18:17:40 +010060#include "webrtc/system_wrappers/include/field_trial.h"
solenbergbd138382015-11-20 16:08:07 -080061#include "webrtc/system_wrappers/include/trace.h"
henrike@webrtc.org28e20752013-07-10 00:45:36 +000062
henrike@webrtc.org28e20752013-07-10 00:45:36 +000063namespace cricket {
solenbergd97ec302015-10-07 01:40:33 -070064namespace {
henrike@webrtc.org28e20752013-07-10 00:45:36 +000065
solenbergbd138382015-11-20 16:08:07 -080066const int kDefaultTraceFilter = webrtc::kTraceNone | webrtc::kTraceTerseInfo |
67 webrtc::kTraceWarning | webrtc::kTraceError |
68 webrtc::kTraceCritical;
69const int kElevatedTraceFilter = kDefaultTraceFilter | webrtc::kTraceStateInfo |
70 webrtc::kTraceInfo;
71
henrike@webrtc.org28e20752013-07-10 00:45:36 +000072// On Windows Vista and newer, Microsoft introduced the concept of "Default
73// Communications Device". This means that there are two types of default
74// devices (old Wave Audio style default and Default Communications Device).
75//
76// On Windows systems which only support Wave Audio style default, uses either
77// -1 or 0 to select the default device.
henrike@webrtc.org28e20752013-07-10 00:45:36 +000078#ifdef WIN32
solenbergd97ec302015-10-07 01:40:33 -070079const int kDefaultAudioDeviceId = -1;
henrike@webrtc.org28e20752013-07-10 00:45:36 +000080#else
solenbergd97ec302015-10-07 01:40:33 -070081const int kDefaultAudioDeviceId = 0;
henrike@webrtc.org28e20752013-07-10 00:45:36 +000082#endif
83
henrike@webrtc.org28e20752013-07-10 00:45:36 +000084// Parameter used for NACK.
85// This value is equivalent to 5 seconds of audio data at 20 ms per packet.
solenbergd97ec302015-10-07 01:40:33 -070086const int kNackMaxPackets = 250;
minyue@webrtc.org2dc6f312014-10-31 05:33:10 +000087
88// Codec parameters for Opus.
henrike@webrtc.org1e09a712013-07-26 19:17:59 +000089// draft-spittka-payload-rtp-opus-03
minyue@webrtc.org2dc6f312014-10-31 05:33:10 +000090
91// Recommended bitrates:
92// 8-12 kb/s for NB speech,
93// 16-20 kb/s for WB speech,
94// 28-40 kb/s for FB speech,
95// 48-64 kb/s for FB mono music, and
96// 64-128 kb/s for FB stereo music.
97// The current implementation applies the following values to mono signals,
98// and multiplies them by 2 for stereo.
solenbergd97ec302015-10-07 01:40:33 -070099const int kOpusBitrateNb = 12000;
100const int kOpusBitrateWb = 20000;
101const int kOpusBitrateFb = 32000;
minyue@webrtc.org2dc6f312014-10-31 05:33:10 +0000102
henrike@webrtc.org1e09a712013-07-26 19:17:59 +0000103// Opus bitrate should be in the range between 6000 and 510000.
solenbergd97ec302015-10-07 01:40:33 -0700104const int kOpusMinBitrate = 6000;
105const int kOpusMaxBitrate = 510000;
buildbot@webrtc.org5d639b32014-09-10 07:57:12 +0000106
wu@webrtc.orgde305012013-10-31 15:40:38 +0000107// Default audio dscp value.
108// See http://tools.ietf.org/html/rfc2474 for details.
109// See also http://tools.ietf.org/html/draft-jennings-rtcweb-qos-00
solenbergd97ec302015-10-07 01:40:33 -0700110const rtc::DiffServCodePoint kAudioDscpValue = rtc::DSCP_EF;
henrike@webrtc.org1e09a712013-07-26 19:17:59 +0000111
sergeyu@chromium.orga59696b2013-09-13 23:48:58 +0000112// Ensure we open the file in a writeable path on ChromeOS and Android. This
113// workaround can be removed when it's possible to specify a filename for audio
114// option based AEC dumps.
henrike@webrtc.org1e09a712013-07-26 19:17:59 +0000115//
116// TODO(grunell): Use a string in the options instead of hardcoding it here
117// and let the embedder choose the filename (crbug.com/264223).
118//
sergeyu@chromium.orga59696b2013-09-13 23:48:58 +0000119// NOTE(ajm): Don't use hardcoded paths on platforms not explicitly specified
120// below.
121#if defined(CHROMEOS)
solenbergd97ec302015-10-07 01:40:33 -0700122const char kAecDumpByAudioOptionFilename[] = "/tmp/audio.aecdump";
sergeyu@chromium.orga59696b2013-09-13 23:48:58 +0000123#elif defined(ANDROID)
solenbergd97ec302015-10-07 01:40:33 -0700124const char kAecDumpByAudioOptionFilename[] = "/sdcard/audio.aecdump";
henrike@webrtc.org1e09a712013-07-26 19:17:59 +0000125#else
solenbergd97ec302015-10-07 01:40:33 -0700126const char kAecDumpByAudioOptionFilename[] = "audio.aecdump";
henrike@webrtc.org1e09a712013-07-26 19:17:59 +0000127#endif
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000128
Fredrik Solenbergb5727682015-12-04 15:22:19 +0100129// Constants from voice_engine_defines.h.
130const int kMinTelephoneEventCode = 0; // RFC4733 (Section 2.3.1)
131const int kMaxTelephoneEventCode = 255;
132const int kMinTelephoneEventDuration = 100;
133const int kMaxTelephoneEventDuration = 60000; // Actual limit is 2^16
134
solenberg0b675462015-10-09 01:37:09 -0700135bool ValidateStreamParams(const StreamParams& sp) {
136 if (sp.ssrcs.empty()) {
137 LOG(LS_ERROR) << "No SSRCs in stream parameters: " << sp.ToString();
138 return false;
139 }
140 if (sp.ssrcs.size() > 1) {
141 LOG(LS_ERROR) << "Multiple SSRCs in stream parameters: " << sp.ToString();
142 return false;
143 }
144 return true;
145}
146
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000147// Dumps an AudioCodec in RFC 2327-ish format.
solenbergd97ec302015-10-07 01:40:33 -0700148std::string ToString(const AudioCodec& codec) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000149 std::stringstream ss;
150 ss << codec.name << "/" << codec.clockrate << "/" << codec.channels
151 << " (" << codec.id << ")";
152 return ss.str();
153}
Minyue Li7100dcd2015-03-27 05:05:59 +0100154
solenbergd97ec302015-10-07 01:40:33 -0700155std::string ToString(const webrtc::CodecInst& codec) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000156 std::stringstream ss;
157 ss << codec.plname << "/" << codec.plfreq << "/" << codec.channels
158 << " (" << codec.pltype << ")";
159 return ss.str();
160}
161
solenbergd97ec302015-10-07 01:40:33 -0700162bool IsCodec(const AudioCodec& codec, const char* ref_name) {
Minyue Li7100dcd2015-03-27 05:05:59 +0100163 return (_stricmp(codec.name.c_str(), ref_name) == 0);
164}
165
solenbergd97ec302015-10-07 01:40:33 -0700166bool IsCodec(const webrtc::CodecInst& codec, const char* ref_name) {
Minyue Li7100dcd2015-03-27 05:05:59 +0100167 return (_stricmp(codec.plname, ref_name) == 0);
168}
169
solenbergd97ec302015-10-07 01:40:33 -0700170bool FindCodec(const std::vector<AudioCodec>& codecs,
solenberg26c8c912015-11-27 04:00:25 -0800171 const AudioCodec& codec,
172 AudioCodec* found_codec) {
Fredrik Solenbergaf9fb212015-08-26 10:45:53 +0200173 for (const AudioCodec& c : codecs) {
174 if (c.Matches(codec)) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000175 if (found_codec != NULL) {
Fredrik Solenbergaf9fb212015-08-26 10:45:53 +0200176 *found_codec = c;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000177 }
178 return true;
179 }
180 }
181 return false;
182}
wu@webrtc.org1d1ffc92013-10-16 18:12:02 +0000183
solenberg0b675462015-10-09 01:37:09 -0700184bool VerifyUniquePayloadTypes(const std::vector<AudioCodec>& codecs) {
185 if (codecs.empty()) {
186 return true;
187 }
188 std::vector<int> payload_types;
189 for (const AudioCodec& codec : codecs) {
190 payload_types.push_back(codec.id);
191 }
192 std::sort(payload_types.begin(), payload_types.end());
193 auto it = std::unique(payload_types.begin(), payload_types.end());
194 return it == payload_types.end();
195}
196
solenbergd97ec302015-10-07 01:40:33 -0700197bool IsNackEnabled(const AudioCodec& codec) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000198 return codec.HasFeedbackParam(FeedbackParam(kRtcpFbParamNack,
199 kParamValueEmpty));
200}
201
Minyue Li7100dcd2015-03-27 05:05:59 +0100202// Return true if codec.params[feature] == "1", false otherwise.
solenberg26c8c912015-11-27 04:00:25 -0800203bool IsCodecFeatureEnabled(const AudioCodec& codec, const char* feature) {
Minyue Li7100dcd2015-03-27 05:05:59 +0100204 int value;
205 return codec.GetParam(feature, &value) && value == 1;
206}
207
208// Use params[kCodecParamMaxAverageBitrate] if it is defined, use codec.bitrate
209// otherwise. If the value (either from params or codec.bitrate) <=0, use the
210// default configuration. If the value is beyond feasible bit rate of Opus,
211// clamp it. Returns the Opus bit rate for operation.
solenbergd97ec302015-10-07 01:40:33 -0700212int GetOpusBitrate(const AudioCodec& codec, int max_playback_rate) {
Minyue Li7100dcd2015-03-27 05:05:59 +0100213 int bitrate = 0;
214 bool use_param = true;
215 if (!codec.GetParam(kCodecParamMaxAverageBitrate, &bitrate)) {
216 bitrate = codec.bitrate;
217 use_param = false;
218 }
219 if (bitrate <= 0) {
220 if (max_playback_rate <= 8000) {
221 bitrate = kOpusBitrateNb;
222 } else if (max_playback_rate <= 16000) {
223 bitrate = kOpusBitrateWb;
224 } else {
225 bitrate = kOpusBitrateFb;
226 }
227
228 if (IsCodecFeatureEnabled(codec, kCodecParamStereo)) {
229 bitrate *= 2;
230 }
231 } else if (bitrate < kOpusMinBitrate || bitrate > kOpusMaxBitrate) {
232 bitrate = (bitrate < kOpusMinBitrate) ? kOpusMinBitrate : kOpusMaxBitrate;
233 std::string rate_source =
234 use_param ? "Codec parameter \"maxaveragebitrate\"" :
235 "Supplied Opus bitrate";
236 LOG(LS_WARNING) << rate_source
237 << " is invalid and is replaced by: "
238 << bitrate;
239 }
240 return bitrate;
241}
242
243// Returns kOpusDefaultPlaybackRate if params[kCodecParamMaxPlaybackRate] is not
244// defined. Returns the value of params[kCodecParamMaxPlaybackRate] otherwise.
solenbergd97ec302015-10-07 01:40:33 -0700245int GetOpusMaxPlaybackRate(const AudioCodec& codec) {
Minyue Li7100dcd2015-03-27 05:05:59 +0100246 int value;
247 if (codec.GetParam(kCodecParamMaxPlaybackRate, &value)) {
248 return value;
249 }
250 return kOpusDefaultMaxPlaybackRate;
251}
252
solenbergd97ec302015-10-07 01:40:33 -0700253void GetOpusConfig(const AudioCodec& codec, webrtc::CodecInst* voe_codec,
Minyue Li7100dcd2015-03-27 05:05:59 +0100254 bool* enable_codec_fec, int* max_playback_rate,
255 bool* enable_codec_dtx) {
256 *enable_codec_fec = IsCodecFeatureEnabled(codec, kCodecParamUseInbandFec);
257 *enable_codec_dtx = IsCodecFeatureEnabled(codec, kCodecParamUseDtx);
258 *max_playback_rate = GetOpusMaxPlaybackRate(codec);
259
260 // If OPUS, change what we send according to the "stereo" codec
261 // parameter, and not the "channels" parameter. We set
262 // voe_codec.channels to 2 if "stereo=1" and 1 otherwise. If
263 // the bitrate is not specified, i.e. is <= zero, we set it to the
264 // appropriate default value for mono or stereo Opus.
265
266 voe_codec->channels = IsCodecFeatureEnabled(codec, kCodecParamStereo) ? 2 : 1;
267 voe_codec->rate = GetOpusBitrate(codec, *max_playback_rate);
268}
269
solenberg566ef242015-11-06 15:34:49 -0800270webrtc::AudioState::Config MakeAudioStateConfig(VoEWrapper* voe_wrapper) {
271 webrtc::AudioState::Config config;
272 config.voice_engine = voe_wrapper->engine();
273 return config;
274}
275
solenberg26c8c912015-11-27 04:00:25 -0800276class WebRtcVoiceCodecs final {
277 public:
278 // TODO(solenberg): Do this filtering once off-line, add a simple AudioCodec
279 // list and add a test which verifies VoE supports the listed codecs.
280 static std::vector<AudioCodec> SupportedCodecs() {
281 LOG(LS_INFO) << "WebRtc VoiceEngine codecs:";
282 std::vector<AudioCodec> result;
283 for (webrtc::CodecInst voe_codec : webrtc::acm2::RentACodec::Database()) {
284 // Change the sample rate of G722 to 8000 to match SDP.
285 MaybeFixupG722(&voe_codec, 8000);
buildbot@webrtc.org13d67762014-05-02 17:33:29 +0000286 // Skip uncompressed formats.
Minyue Li7100dcd2015-03-27 05:05:59 +0100287 if (IsCodec(voe_codec, kL16CodecName)) {
buildbot@webrtc.org13d67762014-05-02 17:33:29 +0000288 continue;
289 }
290
291 const CodecPref* pref = NULL;
tfarina5237aaf2015-11-10 23:44:30 -0800292 for (size_t j = 0; j < arraysize(kCodecPrefs); ++j) {
Minyue Li7100dcd2015-03-27 05:05:59 +0100293 if (IsCodec(voe_codec, kCodecPrefs[j].name) &&
buildbot@webrtc.org13d67762014-05-02 17:33:29 +0000294 kCodecPrefs[j].clockrate == voe_codec.plfreq &&
295 kCodecPrefs[j].channels == voe_codec.channels) {
296 pref = &kCodecPrefs[j];
297 break;
298 }
299 }
300
301 if (pref) {
302 // Use the payload type that we've configured in our pref table;
303 // use the offset in our pref table to determine the sort order.
tfarina5237aaf2015-11-10 23:44:30 -0800304 AudioCodec codec(
305 pref->payload_type, voe_codec.plname, voe_codec.plfreq,
306 voe_codec.rate, voe_codec.channels,
307 static_cast<int>(arraysize(kCodecPrefs)) - (pref - kCodecPrefs));
buildbot@webrtc.org13d67762014-05-02 17:33:29 +0000308 LOG(LS_INFO) << ToString(codec);
Minyue Li7100dcd2015-03-27 05:05:59 +0100309 if (IsCodec(codec, kIsacCodecName)) {
minyue@webrtc.org26236952014-10-29 02:27:08 +0000310 // Indicate auto-bitrate in signaling.
buildbot@webrtc.org13d67762014-05-02 17:33:29 +0000311 codec.bitrate = 0;
312 }
Minyue Li7100dcd2015-03-27 05:05:59 +0100313 if (IsCodec(codec, kOpusCodecName)) {
buildbot@webrtc.org13d67762014-05-02 17:33:29 +0000314 // Only add fmtp parameters that differ from the spec.
315 if (kPreferredMinPTime != kOpusDefaultMinPTime) {
316 codec.params[kCodecParamMinPTime] =
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000317 rtc::ToString(kPreferredMinPTime);
buildbot@webrtc.org13d67762014-05-02 17:33:29 +0000318 }
319 if (kPreferredMaxPTime != kOpusDefaultMaxPTime) {
320 codec.params[kCodecParamMaxPTime] =
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000321 rtc::ToString(kPreferredMaxPTime);
buildbot@webrtc.org13d67762014-05-02 17:33:29 +0000322 }
pkasting@chromium.orgd3245462015-02-23 21:28:22 +0000323 codec.SetParam(kCodecParamUseInbandFec, 1);
minyue@webrtc.org4ef22d12014-11-17 09:26:39 +0000324
325 // TODO(hellner): Add ptime, sprop-stereo, and stereo
buildbot@webrtc.org13d67762014-05-02 17:33:29 +0000326 // when they can be set to values other than the default.
327 }
solenberg26c8c912015-11-27 04:00:25 -0800328 result.push_back(codec);
buildbot@webrtc.org13d67762014-05-02 17:33:29 +0000329 } else {
330 LOG(LS_WARNING) << "Unexpected codec: " << ToString(voe_codec);
331 }
332 }
solenberg26c8c912015-11-27 04:00:25 -0800333 // Make sure they are in local preference order.
334 std::sort(result.begin(), result.end(), &AudioCodec::Preferable);
335 return result;
buildbot@webrtc.org13d67762014-05-02 17:33:29 +0000336 }
buildbot@webrtc.org13d67762014-05-02 17:33:29 +0000337
solenberg26c8c912015-11-27 04:00:25 -0800338 static bool ToCodecInst(const AudioCodec& in,
339 webrtc::CodecInst* out) {
340 for (webrtc::CodecInst voe_codec : webrtc::acm2::RentACodec::Database()) {
341 // Change the sample rate of G722 to 8000 to match SDP.
342 MaybeFixupG722(&voe_codec, 8000);
343 AudioCodec codec(voe_codec.pltype, voe_codec.plname, voe_codec.plfreq,
344 voe_codec.rate, voe_codec.channels, 0);
345 bool multi_rate = IsCodecMultiRate(voe_codec);
346 // Allow arbitrary rates for ISAC to be specified.
347 if (multi_rate) {
348 // Set codec.bitrate to 0 so the check for codec.Matches() passes.
349 codec.bitrate = 0;
350 }
351 if (codec.Matches(in)) {
352 if (out) {
353 // Fixup the payload type.
354 voe_codec.pltype = in.id;
355
356 // Set bitrate if specified.
357 if (multi_rate && in.bitrate != 0) {
358 voe_codec.rate = in.bitrate;
359 }
360
361 // Reset G722 sample rate to 16000 to match WebRTC.
362 MaybeFixupG722(&voe_codec, 16000);
363
364 // Apply codec-specific settings.
365 if (IsCodec(codec, kIsacCodecName)) {
366 // If ISAC and an explicit bitrate is not specified,
367 // enable auto bitrate adjustment.
368 voe_codec.rate = (in.bitrate > 0) ? in.bitrate : -1;
369 }
370 *out = voe_codec;
371 }
372 return true;
373 }
374 }
henrik.lundin@webrtc.org8038d422014-11-11 08:38:24 +0000375 return false;
henrik.lundin@webrtc.orgf85dbce2014-11-07 12:25:00 +0000376 }
solenberg26c8c912015-11-27 04:00:25 -0800377
378 static bool IsCodecMultiRate(const webrtc::CodecInst& codec) {
379 for (size_t i = 0; i < arraysize(kCodecPrefs); ++i) {
380 if (IsCodec(codec, kCodecPrefs[i].name) &&
381 kCodecPrefs[i].clockrate == codec.plfreq) {
382 return kCodecPrefs[i].is_multi_rate;
383 }
384 }
385 return false;
386 }
387
388 // If the AudioCodec param kCodecParamPTime is set, then we will set it to
389 // codec pacsize if it's valid, or we will pick the next smallest value we
390 // support.
391 // TODO(Brave): Query supported packet sizes from ACM when the API is ready.
392 static bool SetPTimeAsPacketSize(webrtc::CodecInst* codec, int ptime_ms) {
393 for (const CodecPref& codec_pref : kCodecPrefs) {
394 if ((IsCodec(*codec, codec_pref.name) &&
395 codec_pref.clockrate == codec->plfreq) ||
396 IsCodec(*codec, kG722CodecName)) {
397 int packet_size_ms = SelectPacketSize(codec_pref, ptime_ms);
398 if (packet_size_ms) {
399 // Convert unit from milli-seconds to samples.
400 codec->pacsize = (codec->plfreq / 1000) * packet_size_ms;
401 return true;
402 }
403 }
404 }
405 return false;
406 }
407
408 private:
409 static const int kMaxNumPacketSize = 6;
410 struct CodecPref {
411 const char* name;
412 int clockrate;
413 int channels;
414 int payload_type;
415 bool is_multi_rate;
416 int packet_sizes_ms[kMaxNumPacketSize];
417 };
418 // Note: keep the supported packet sizes in ascending order.
419 static const CodecPref kCodecPrefs[12];
420
421 static int SelectPacketSize(const CodecPref& codec_pref, int ptime_ms) {
422 int selected_packet_size_ms = codec_pref.packet_sizes_ms[0];
423 for (int packet_size_ms : codec_pref.packet_sizes_ms) {
424 if (packet_size_ms && packet_size_ms <= ptime_ms) {
425 selected_packet_size_ms = packet_size_ms;
426 }
427 }
428 return selected_packet_size_ms;
429 }
430
431 // Changes RTP timestamp rate of G722. This is due to the "bug" in the RFC
432 // which says that G722 should be advertised as 8 kHz although it is a 16 kHz
433 // codec.
434 static void MaybeFixupG722(webrtc::CodecInst* voe_codec, int new_plfreq) {
435 if (IsCodec(*voe_codec, kG722CodecName)) {
436 // If the ASSERT triggers, the codec definition in WebRTC VoiceEngine
437 // has changed, and this special case is no longer needed.
438 RTC_DCHECK(voe_codec->plfreq != new_plfreq);
439 voe_codec->plfreq = new_plfreq;
440 }
441 }
442};
443
444const WebRtcVoiceCodecs::CodecPref WebRtcVoiceCodecs::kCodecPrefs[12] = {
445 { kOpusCodecName, 48000, 2, 111, true, { 10, 20, 40, 60 } },
446 { kIsacCodecName, 16000, 1, 103, true, { 30, 60 } },
447 { kIsacCodecName, 32000, 1, 104, true, { 30 } },
448 // G722 should be advertised as 8000 Hz because of the RFC "bug".
449 { kG722CodecName, 8000, 1, 9, false, { 10, 20, 30, 40, 50, 60 } },
450 { kIlbcCodecName, 8000, 1, 102, false, { 20, 30, 40, 60 } },
451 { kPcmuCodecName, 8000, 1, 0, false, { 10, 20, 30, 40, 50, 60 } },
452 { kPcmaCodecName, 8000, 1, 8, false, { 10, 20, 30, 40, 50, 60 } },
453 { kCnCodecName, 32000, 1, 106, false, { } },
454 { kCnCodecName, 16000, 1, 105, false, { } },
455 { kCnCodecName, 8000, 1, 13, false, { } },
456 { kRedCodecName, 8000, 1, 127, false, { } },
457 { kDtmfCodecName, 8000, 1, 126, false, { } },
458};
459} // namespace {
460
461bool WebRtcVoiceEngine::ToCodecInst(const AudioCodec& in,
462 webrtc::CodecInst* out) {
463 return WebRtcVoiceCodecs::ToCodecInst(in, out);
464}
465
466WebRtcVoiceEngine::WebRtcVoiceEngine()
467 : voe_wrapper_(new VoEWrapper()),
468 audio_state_(webrtc::AudioState::Create(MakeAudioStateConfig(voe()))) {
469 Construct();
470}
471
472WebRtcVoiceEngine::WebRtcVoiceEngine(VoEWrapper* voe_wrapper)
473 : voe_wrapper_(voe_wrapper) {
474 Construct();
475}
476
477void WebRtcVoiceEngine::Construct() {
478 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
479 LOG(LS_VERBOSE) << "WebRtcVoiceEngine::WebRtcVoiceEngine";
480
481 signal_thread_checker_.DetachFromThread();
482 std::memset(&default_agc_config_, 0, sizeof(default_agc_config_));
solenberg246b8172015-12-08 09:50:23 -0800483 voe_config_.Set<webrtc::VoicePacing>(new webrtc::VoicePacing(true));
solenberg26c8c912015-11-27 04:00:25 -0800484
485 webrtc::Trace::set_level_filter(kDefaultTraceFilter);
486 webrtc::Trace::SetTraceCallback(this);
487
488 // Load our audio codec list.
489 codecs_ = WebRtcVoiceCodecs::SupportedCodecs();
henrik.lundin@webrtc.orgf85dbce2014-11-07 12:25:00 +0000490}
491
buildbot@webrtc.org13d67762014-05-02 17:33:29 +0000492WebRtcVoiceEngine::~WebRtcVoiceEngine() {
solenberg566ef242015-11-06 15:34:49 -0800493 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
buildbot@webrtc.org13d67762014-05-02 17:33:29 +0000494 LOG(LS_VERBOSE) << "WebRtcVoiceEngine::~WebRtcVoiceEngine";
buildbot@webrtc.org13d67762014-05-02 17:33:29 +0000495 if (adm_) {
496 voe_wrapper_.reset();
497 adm_->Release();
498 adm_ = NULL;
499 }
solenbergbd138382015-11-20 16:08:07 -0800500 webrtc::Trace::SetTraceCallback(nullptr);
buildbot@webrtc.org13d67762014-05-02 17:33:29 +0000501}
502
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000503bool WebRtcVoiceEngine::Init(rtc::Thread* worker_thread) {
solenberg566ef242015-11-06 15:34:49 -0800504 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
henrikg91d6ede2015-09-17 00:24:34 -0700505 RTC_DCHECK(worker_thread == rtc::Thread::Current());
buildbot@webrtc.org13d67762014-05-02 17:33:29 +0000506 LOG(LS_INFO) << "WebRtcVoiceEngine::Init";
507 bool res = InitInternal();
508 if (res) {
509 LOG(LS_INFO) << "WebRtcVoiceEngine::Init Done!";
510 } else {
511 LOG(LS_ERROR) << "WebRtcVoiceEngine::Init failed";
512 Terminate();
513 }
514 return res;
515}
516
517bool WebRtcVoiceEngine::InitInternal() {
solenberg566ef242015-11-06 15:34:49 -0800518 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
buildbot@webrtc.org13d67762014-05-02 17:33:29 +0000519 // Temporarily turn logging level up for the Init call
solenbergbd138382015-11-20 16:08:07 -0800520 webrtc::Trace::set_level_filter(kElevatedTraceFilter);
solenberg2515af22015-12-02 06:19:36 -0800521 LOG(LS_INFO) << webrtc::VoiceEngine::GetVersionString();
buildbot@webrtc.org13d67762014-05-02 17:33:29 +0000522 if (voe_wrapper_->base()->Init(adm_) == -1) {
523 LOG_RTCERR0_EX(Init, voe_wrapper_->error());
buildbot@webrtc.org13d67762014-05-02 17:33:29 +0000524 return false;
525 }
solenbergbd138382015-11-20 16:08:07 -0800526 webrtc::Trace::set_level_filter(kDefaultTraceFilter);
buildbot@webrtc.org13d67762014-05-02 17:33:29 +0000527
buildbot@webrtc.org13d67762014-05-02 17:33:29 +0000528 // Save the default AGC configuration settings. This must happen before
solenberg246b8172015-12-08 09:50:23 -0800529 // calling ApplyOptions or the default will be overwritten.
buildbot@webrtc.org13d67762014-05-02 17:33:29 +0000530 if (voe_wrapper_->processing()->GetAgcConfig(default_agc_config_) == -1) {
531 LOG_RTCERR0(GetAgcConfig);
532 return false;
533 }
534
buildbot@webrtc.org13d67762014-05-02 17:33:29 +0000535 // Print our codec list again for the call diagnostic log
536 LOG(LS_INFO) << "WebRtc VoiceEngine codecs:";
Fredrik Solenbergaf9fb212015-08-26 10:45:53 +0200537 for (const AudioCodec& codec : codecs_) {
538 LOG(LS_INFO) << ToString(codec);
buildbot@webrtc.org13d67762014-05-02 17:33:29 +0000539 }
540
solenberg246b8172015-12-08 09:50:23 -0800541 SetDefaultDevices();
542
buildbot@webrtc.org13d67762014-05-02 17:33:29 +0000543 initialized_ = true;
544 return true;
545}
546
buildbot@webrtc.org13d67762014-05-02 17:33:29 +0000547void WebRtcVoiceEngine::Terminate() {
solenberg566ef242015-11-06 15:34:49 -0800548 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
buildbot@webrtc.org13d67762014-05-02 17:33:29 +0000549 LOG(LS_INFO) << "WebRtcVoiceEngine::Terminate";
550 initialized_ = false;
551
552 StopAecDump();
553
buildbot@webrtc.org13d67762014-05-02 17:33:29 +0000554 voe_wrapper_->base()->Terminate();
buildbot@webrtc.org13d67762014-05-02 17:33:29 +0000555}
556
solenberg566ef242015-11-06 15:34:49 -0800557rtc::scoped_refptr<webrtc::AudioState>
558 WebRtcVoiceEngine::GetAudioState() const {
559 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
560 return audio_state_;
561}
562
Fredrik Solenberg709ed672015-09-15 12:26:33 +0200563VoiceMediaChannel* WebRtcVoiceEngine::CreateChannel(webrtc::Call* call,
Jelena Marusicc28a8962015-05-29 15:05:44 +0200564 const AudioOptions& options) {
solenberg566ef242015-11-06 15:34:49 -0800565 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
solenberg0a617e22015-10-20 15:49:38 -0700566 return new WebRtcVoiceMediaChannel(this, options, call);
buildbot@webrtc.org13d67762014-05-02 17:33:29 +0000567}
568
buildbot@webrtc.org13d67762014-05-02 17:33:29 +0000569bool WebRtcVoiceEngine::ApplyOptions(const AudioOptions& options_in) {
solenberg566ef242015-11-06 15:34:49 -0800570 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
henrikac14f5ff2015-09-23 14:08:33 +0200571 LOG(LS_INFO) << "ApplyOptions: " << options_in.ToString();
solenberg246b8172015-12-08 09:50:23 -0800572
573 // Default engine options.
574 AudioOptions options;
575 options.echo_cancellation = rtc::Optional<bool>(true);
576 options.auto_gain_control = rtc::Optional<bool>(true);
577 options.noise_suppression = rtc::Optional<bool>(true);
578 options.highpass_filter = rtc::Optional<bool>(true);
579 options.stereo_swapping = rtc::Optional<bool>(false);
580 options.audio_jitter_buffer_max_packets = rtc::Optional<int>(50);
581 options.audio_jitter_buffer_fast_accelerate = rtc::Optional<bool>(false);
582 options.typing_detection = rtc::Optional<bool>(true);
583 options.adjust_agc_delta = rtc::Optional<int>(0);
584 options.experimental_agc = rtc::Optional<bool>(false);
585 options.extended_filter_aec = rtc::Optional<bool>(false);
586 options.delay_agnostic_aec = rtc::Optional<bool>(false);
587 options.experimental_ns = rtc::Optional<bool>(false);
588 options.aec_dump = rtc::Optional<bool>(false);
589
590 // Apply any given options on top.
591 options.SetAll(options_in);
592
buildbot@webrtc.org13d67762014-05-02 17:33:29 +0000593 // kEcConference is AEC with high suppression.
594 webrtc::EcModes ec_mode = webrtc::kEcConference;
595 webrtc::AecmModes aecm_mode = webrtc::kAecmSpeakerphone;
596 webrtc::AgcModes agc_mode = webrtc::kAgcAdaptiveAnalog;
597 webrtc::NsModes ns_mode = webrtc::kNsHighSuppression;
kwiberg102c6a62015-10-30 02:47:38 -0700598 if (options.aecm_generate_comfort_noise) {
buildbot@webrtc.org13d67762014-05-02 17:33:29 +0000599 LOG(LS_VERBOSE) << "Comfort noise explicitly set to "
kwiberg102c6a62015-10-30 02:47:38 -0700600 << *options.aecm_generate_comfort_noise
601 << " (default is false).";
buildbot@webrtc.org13d67762014-05-02 17:33:29 +0000602 }
603
604#if defined(IOS)
605 // On iOS, VPIO provides built-in EC and AGC.
Karl Wibergbe579832015-11-10 22:34:18 +0100606 options.echo_cancellation = rtc::Optional<bool>(false);
607 options.auto_gain_control = rtc::Optional<bool>(false);
henrika86d907c2015-09-07 16:09:50 +0200608 LOG(LS_INFO) << "Always disable AEC and AGC on iOS. Use built-in instead.";
buildbot@webrtc.org13d67762014-05-02 17:33:29 +0000609#elif defined(ANDROID)
610 ec_mode = webrtc::kEcAecm;
611#endif
612
613#if defined(IOS) || defined(ANDROID)
614 // Set the AGC mode for iOS as well despite disabling it above, to avoid
615 // unsupported configuration errors from webrtc.
616 agc_mode = webrtc::kAgcFixedDigital;
Karl Wibergbe579832015-11-10 22:34:18 +0100617 options.typing_detection = rtc::Optional<bool>(false);
618 options.experimental_agc = rtc::Optional<bool>(false);
619 options.extended_filter_aec = rtc::Optional<bool>(false);
620 options.experimental_ns = rtc::Optional<bool>(false);
buildbot@webrtc.org13d67762014-05-02 17:33:29 +0000621#endif
622
Bjorn Volckerbf395c12015-03-25 22:45:56 +0100623 // Delay Agnostic AEC automatically turns on EC if not set except on iOS
624 // where the feature is not supported.
625 bool use_delay_agnostic_aec = false;
626#if !defined(IOS)
kwiberg102c6a62015-10-30 02:47:38 -0700627 if (options.delay_agnostic_aec) {
628 use_delay_agnostic_aec = *options.delay_agnostic_aec;
Bjorn Volckerbf395c12015-03-25 22:45:56 +0100629 if (use_delay_agnostic_aec) {
Karl Wibergbe579832015-11-10 22:34:18 +0100630 options.echo_cancellation = rtc::Optional<bool>(true);
631 options.extended_filter_aec = rtc::Optional<bool>(true);
Bjorn Volckerbf395c12015-03-25 22:45:56 +0100632 ec_mode = webrtc::kEcConference;
633 }
634 }
635#endif
636
buildbot@webrtc.org13d67762014-05-02 17:33:29 +0000637 webrtc::VoEAudioProcessing* voep = voe_wrapper_->processing();
638
kwiberg102c6a62015-10-30 02:47:38 -0700639 if (options.echo_cancellation) {
henrika@webrtc.orga954c072014-12-09 16:22:09 +0000640 // Check if platform supports built-in EC. Currently only supported on
641 // Android and in combination with Java based audio layer.
642 // TODO(henrika): investigate possibility to support built-in EC also
643 // in combination with Open SL ES audio.
644 const bool built_in_aec = voe_wrapper_->hw()->BuiltInAECIsAvailable();
Bjorn Volcker73f72102015-06-03 14:50:15 +0200645 if (built_in_aec) {
Bjorn Volckerccfc9392015-05-07 07:43:17 +0200646 // Built-in EC exists on this device and use_delay_agnostic_aec is not
647 // overriding it. Enable/Disable it according to the echo_cancellation
648 // audio option.
Bjorn Volcker73f72102015-06-03 14:50:15 +0200649 const bool enable_built_in_aec =
kwiberg102c6a62015-10-30 02:47:38 -0700650 *options.echo_cancellation && !use_delay_agnostic_aec;
Bjorn Volcker73f72102015-06-03 14:50:15 +0200651 if (voe_wrapper_->hw()->EnableBuiltInAEC(enable_built_in_aec) == 0 &&
652 enable_built_in_aec) {
Bjorn Volckerbf395c12015-03-25 22:45:56 +0100653 // Disable internal software EC if built-in EC is enabled,
henrika@webrtc.orga954c072014-12-09 16:22:09 +0000654 // i.e., replace the software EC with the built-in EC.
Karl Wibergbe579832015-11-10 22:34:18 +0100655 options.echo_cancellation = rtc::Optional<bool>(false);
henrika@webrtc.orga954c072014-12-09 16:22:09 +0000656 LOG(LS_INFO) << "Disabling EC since built-in EC will be used instead";
657 }
658 }
kwiberg102c6a62015-10-30 02:47:38 -0700659 if (voep->SetEcStatus(*options.echo_cancellation, ec_mode) == -1) {
660 LOG_RTCERR2(SetEcStatus, *options.echo_cancellation, ec_mode);
buildbot@webrtc.org13d67762014-05-02 17:33:29 +0000661 return false;
662 } else {
kwiberg102c6a62015-10-30 02:47:38 -0700663 LOG(LS_INFO) << "Echo control set to " << *options.echo_cancellation
henrika86d907c2015-09-07 16:09:50 +0200664 << " with mode " << ec_mode;
buildbot@webrtc.org13d67762014-05-02 17:33:29 +0000665 }
666#if !defined(ANDROID)
667 // TODO(ajm): Remove the error return on Android from webrtc.
kwiberg102c6a62015-10-30 02:47:38 -0700668 if (voep->SetEcMetricsStatus(*options.echo_cancellation) == -1) {
669 LOG_RTCERR1(SetEcMetricsStatus, *options.echo_cancellation);
buildbot@webrtc.org13d67762014-05-02 17:33:29 +0000670 return false;
671 }
672#endif
673 if (ec_mode == webrtc::kEcAecm) {
kwiberg102c6a62015-10-30 02:47:38 -0700674 bool cn = options.aecm_generate_comfort_noise.value_or(false);
675 if (voep->SetAecmMode(aecm_mode, cn) != 0) {
676 LOG_RTCERR2(SetAecmMode, aecm_mode, cn);
buildbot@webrtc.org13d67762014-05-02 17:33:29 +0000677 return false;
678 }
679 }
680 }
681
kwiberg102c6a62015-10-30 02:47:38 -0700682 if (options.auto_gain_control) {
henrikac14f5ff2015-09-23 14:08:33 +0200683 const bool built_in_agc = voe_wrapper_->hw()->BuiltInAGCIsAvailable();
684 if (built_in_agc) {
kwiberg102c6a62015-10-30 02:47:38 -0700685 if (voe_wrapper_->hw()->EnableBuiltInAGC(*options.auto_gain_control) ==
686 0 &&
687 *options.auto_gain_control) {
henrikac14f5ff2015-09-23 14:08:33 +0200688 // Disable internal software AGC if built-in AGC is enabled,
689 // i.e., replace the software AGC with the built-in AGC.
Karl Wibergbe579832015-11-10 22:34:18 +0100690 options.auto_gain_control = rtc::Optional<bool>(false);
henrikac14f5ff2015-09-23 14:08:33 +0200691 LOG(LS_INFO) << "Disabling AGC since built-in AGC will be used instead";
692 }
693 }
kwiberg102c6a62015-10-30 02:47:38 -0700694 if (voep->SetAgcStatus(*options.auto_gain_control, agc_mode) == -1) {
695 LOG_RTCERR2(SetAgcStatus, *options.auto_gain_control, agc_mode);
buildbot@webrtc.org13d67762014-05-02 17:33:29 +0000696 return false;
697 } else {
kwiberg102c6a62015-10-30 02:47:38 -0700698 LOG(LS_INFO) << "Auto gain set to " << *options.auto_gain_control
699 << " with mode " << agc_mode;
buildbot@webrtc.org13d67762014-05-02 17:33:29 +0000700 }
701 }
702
kwiberg102c6a62015-10-30 02:47:38 -0700703 if (options.tx_agc_target_dbov || options.tx_agc_digital_compression_gain ||
704 options.tx_agc_limiter) {
buildbot@webrtc.org13d67762014-05-02 17:33:29 +0000705 // Override default_agc_config_. Generally, an unset option means "leave
706 // the VoE bits alone" in this function, so we want whatever is set to be
707 // stored as the new "default". If we didn't, then setting e.g.
708 // tx_agc_target_dbov would reset digital compression gain and limiter
709 // settings.
710 // Also, if we don't update default_agc_config_, then adjust_agc_delta
711 // would be an offset from the original values, and not whatever was set
712 // explicitly.
kwiberg102c6a62015-10-30 02:47:38 -0700713 default_agc_config_.targetLeveldBOv = options.tx_agc_target_dbov.value_or(
714 default_agc_config_.targetLeveldBOv);
buildbot@webrtc.org13d67762014-05-02 17:33:29 +0000715 default_agc_config_.digitalCompressionGaindB =
kwiberg102c6a62015-10-30 02:47:38 -0700716 options.tx_agc_digital_compression_gain.value_or(
buildbot@webrtc.org13d67762014-05-02 17:33:29 +0000717 default_agc_config_.digitalCompressionGaindB);
718 default_agc_config_.limiterEnable =
kwiberg102c6a62015-10-30 02:47:38 -0700719 options.tx_agc_limiter.value_or(default_agc_config_.limiterEnable);
buildbot@webrtc.org13d67762014-05-02 17:33:29 +0000720 if (voe_wrapper_->processing()->SetAgcConfig(default_agc_config_) == -1) {
721 LOG_RTCERR3(SetAgcConfig,
722 default_agc_config_.targetLeveldBOv,
723 default_agc_config_.digitalCompressionGaindB,
724 default_agc_config_.limiterEnable);
725 return false;
726 }
727 }
728
kwiberg102c6a62015-10-30 02:47:38 -0700729 if (options.noise_suppression) {
henrikac14f5ff2015-09-23 14:08:33 +0200730 const bool built_in_ns = voe_wrapper_->hw()->BuiltInNSIsAvailable();
731 if (built_in_ns) {
kwiberg102c6a62015-10-30 02:47:38 -0700732 if (voe_wrapper_->hw()->EnableBuiltInNS(*options.noise_suppression) ==
733 0 &&
734 *options.noise_suppression) {
henrikac14f5ff2015-09-23 14:08:33 +0200735 // Disable internal software NS if built-in NS is enabled,
736 // i.e., replace the software NS with the built-in NS.
Karl Wibergbe579832015-11-10 22:34:18 +0100737 options.noise_suppression = rtc::Optional<bool>(false);
henrikac14f5ff2015-09-23 14:08:33 +0200738 LOG(LS_INFO) << "Disabling NS since built-in NS will be used instead";
739 }
740 }
kwiberg102c6a62015-10-30 02:47:38 -0700741 if (voep->SetNsStatus(*options.noise_suppression, ns_mode) == -1) {
742 LOG_RTCERR2(SetNsStatus, *options.noise_suppression, ns_mode);
buildbot@webrtc.org13d67762014-05-02 17:33:29 +0000743 return false;
744 } else {
kwiberg102c6a62015-10-30 02:47:38 -0700745 LOG(LS_INFO) << "Noise suppression set to " << *options.noise_suppression
henrikac14f5ff2015-09-23 14:08:33 +0200746 << " with mode " << ns_mode;
buildbot@webrtc.org13d67762014-05-02 17:33:29 +0000747 }
748 }
749
kwiberg102c6a62015-10-30 02:47:38 -0700750 if (options.highpass_filter) {
751 LOG(LS_INFO) << "High pass filter enabled? " << *options.highpass_filter;
752 if (voep->EnableHighPassFilter(*options.highpass_filter) == -1) {
753 LOG_RTCERR1(SetHighpassFilterStatus, *options.highpass_filter);
buildbot@webrtc.org13d67762014-05-02 17:33:29 +0000754 return false;
755 }
756 }
757
kwiberg102c6a62015-10-30 02:47:38 -0700758 if (options.stereo_swapping) {
759 LOG(LS_INFO) << "Stereo swapping enabled? " << *options.stereo_swapping;
760 voep->EnableStereoChannelSwapping(*options.stereo_swapping);
761 if (voep->IsStereoChannelSwappingEnabled() != *options.stereo_swapping) {
762 LOG_RTCERR1(EnableStereoChannelSwapping, *options.stereo_swapping);
buildbot@webrtc.org13d67762014-05-02 17:33:29 +0000763 return false;
764 }
765 }
766
kwiberg102c6a62015-10-30 02:47:38 -0700767 if (options.audio_jitter_buffer_max_packets) {
768 LOG(LS_INFO) << "NetEq capacity is "
769 << *options.audio_jitter_buffer_max_packets;
Henrik Lundin64dad832015-05-11 12:44:23 +0200770 voe_config_.Set<webrtc::NetEqCapacityConfig>(
kwiberg102c6a62015-10-30 02:47:38 -0700771 new webrtc::NetEqCapacityConfig(
772 *options.audio_jitter_buffer_max_packets));
Henrik Lundin64dad832015-05-11 12:44:23 +0200773 }
774
kwiberg102c6a62015-10-30 02:47:38 -0700775 if (options.audio_jitter_buffer_fast_accelerate) {
776 LOG(LS_INFO) << "NetEq fast mode? "
777 << *options.audio_jitter_buffer_fast_accelerate;
Henrik Lundin5263b3c2015-06-01 10:29:41 +0200778 voe_config_.Set<webrtc::NetEqFastAccelerate>(
kwiberg102c6a62015-10-30 02:47:38 -0700779 new webrtc::NetEqFastAccelerate(
780 *options.audio_jitter_buffer_fast_accelerate));
Henrik Lundin5263b3c2015-06-01 10:29:41 +0200781 }
782
kwiberg102c6a62015-10-30 02:47:38 -0700783 if (options.typing_detection) {
784 LOG(LS_INFO) << "Typing detection is enabled? "
785 << *options.typing_detection;
786 if (voep->SetTypingDetectionStatus(*options.typing_detection) == -1) {
buildbot@webrtc.org13d67762014-05-02 17:33:29 +0000787 // In case of error, log the info and continue
kwiberg102c6a62015-10-30 02:47:38 -0700788 LOG_RTCERR1(SetTypingDetectionStatus, *options.typing_detection);
buildbot@webrtc.org13d67762014-05-02 17:33:29 +0000789 }
790 }
791
kwiberg102c6a62015-10-30 02:47:38 -0700792 if (options.adjust_agc_delta) {
793 LOG(LS_INFO) << "Adjust agc delta is " << *options.adjust_agc_delta;
794 if (!AdjustAgcLevel(*options.adjust_agc_delta)) {
buildbot@webrtc.org13d67762014-05-02 17:33:29 +0000795 return false;
796 }
797 }
798
kwiberg102c6a62015-10-30 02:47:38 -0700799 if (options.aec_dump) {
800 LOG(LS_INFO) << "Aec dump is enabled? " << *options.aec_dump;
801 if (*options.aec_dump)
buildbot@webrtc.org13d67762014-05-02 17:33:29 +0000802 StartAecDump(kAecDumpByAudioOptionFilename);
803 else
804 StopAecDump();
805 }
806
buildbot@webrtc.org1f8a2372014-08-28 10:52:44 +0000807 webrtc::Config config;
808
kwiberg102c6a62015-10-30 02:47:38 -0700809 if (options.delay_agnostic_aec)
810 delay_agnostic_aec_ = options.delay_agnostic_aec;
811 if (delay_agnostic_aec_) {
812 LOG(LS_INFO) << "Delay agnostic aec is enabled? " << *delay_agnostic_aec_;
henrik.lundin0f133b92015-07-02 00:17:55 -0700813 config.Set<webrtc::DelayAgnostic>(
kwiberg102c6a62015-10-30 02:47:38 -0700814 new webrtc::DelayAgnostic(*delay_agnostic_aec_));
Bjorn Volckerbf395c12015-03-25 22:45:56 +0100815 }
816
kwiberg102c6a62015-10-30 02:47:38 -0700817 if (options.extended_filter_aec) {
818 extended_filter_aec_ = options.extended_filter_aec;
819 }
820 if (extended_filter_aec_) {
821 LOG(LS_INFO) << "Extended filter aec is enabled? " << *extended_filter_aec_;
Henrik Lundin441f6342015-06-09 16:03:13 +0200822 config.Set<webrtc::ExtendedFilter>(
kwiberg102c6a62015-10-30 02:47:38 -0700823 new webrtc::ExtendedFilter(*extended_filter_aec_));
buildbot@webrtc.org1f8a2372014-08-28 10:52:44 +0000824 }
825
kwiberg102c6a62015-10-30 02:47:38 -0700826 if (options.experimental_ns) {
827 experimental_ns_ = options.experimental_ns;
828 }
829 if (experimental_ns_) {
830 LOG(LS_INFO) << "Experimental ns is enabled? " << *experimental_ns_;
buildbot@webrtc.org1f8a2372014-08-28 10:52:44 +0000831 config.Set<webrtc::ExperimentalNs>(
kwiberg102c6a62015-10-30 02:47:38 -0700832 new webrtc::ExperimentalNs(*experimental_ns_));
buildbot@webrtc.org1f8a2372014-08-28 10:52:44 +0000833 }
buildbot@webrtc.org1f8a2372014-08-28 10:52:44 +0000834
835 // We check audioproc for the benefit of tests, since FakeWebRtcVoiceEngine
836 // returns NULL on audio_processing().
837 webrtc::AudioProcessing* audioproc = voe_wrapper_->base()->audio_processing();
838 if (audioproc) {
839 audioproc->SetExtraOptions(config);
840 }
841
kwiberg102c6a62015-10-30 02:47:38 -0700842 if (options.recording_sample_rate) {
843 LOG(LS_INFO) << "Recording sample rate is "
844 << *options.recording_sample_rate;
845 if (voe_wrapper_->hw()->SetRecordingSampleRate(
846 *options.recording_sample_rate)) {
847 LOG_RTCERR1(SetRecordingSampleRate, *options.recording_sample_rate);
buildbot@webrtc.org13d67762014-05-02 17:33:29 +0000848 }
849 }
850
kwiberg102c6a62015-10-30 02:47:38 -0700851 if (options.playout_sample_rate) {
852 LOG(LS_INFO) << "Playout sample rate is " << *options.playout_sample_rate;
853 if (voe_wrapper_->hw()->SetPlayoutSampleRate(
854 *options.playout_sample_rate)) {
855 LOG_RTCERR1(SetPlayoutSampleRate, *options.playout_sample_rate);
buildbot@webrtc.org13d67762014-05-02 17:33:29 +0000856 }
857 }
858
859 return true;
860}
861
solenberg246b8172015-12-08 09:50:23 -0800862void WebRtcVoiceEngine::SetDefaultDevices() {
solenberg566ef242015-11-06 15:34:49 -0800863 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
buildbot@webrtc.org13d67762014-05-02 17:33:29 +0000864#if !defined(IOS)
solenberg246b8172015-12-08 09:50:23 -0800865 int in_id = kDefaultAudioDeviceId;
866 int out_id = kDefaultAudioDeviceId;
867 LOG(LS_INFO) << "Setting microphone to (id=" << in_id
868 << ") and speaker to (id=" << out_id << ")";
buildbot@webrtc.org13d67762014-05-02 17:33:29 +0000869
solenbergc1a1b352015-09-22 13:31:20 -0700870 bool ret = true;
solenberg246b8172015-12-08 09:50:23 -0800871 if (voe_wrapper_->hw()->SetRecordingDevice(in_id) == -1) {
872 LOG_RTCERR1(SetRecordingDevice, in_id);
buildbot@webrtc.org13d67762014-05-02 17:33:29 +0000873 ret = false;
874 }
solenberg246b8172015-12-08 09:50:23 -0800875 webrtc::AudioProcessing* ap = voe()->base()->audio_processing();
876 if (ap) {
877 ap->Initialize();
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000878 }
879
solenberg246b8172015-12-08 09:50:23 -0800880 if (voe_wrapper_->hw()->SetPlayoutDevice(out_id) == -1) {
881 LOG_RTCERR1(SetPlayoutDevice, out_id);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000882 ret = false;
883 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000884
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000885 if (ret) {
solenberg246b8172015-12-08 09:50:23 -0800886 LOG(LS_INFO) << "Set microphone to (id=" << in_id
887 << ") and speaker to (id=" << out_id << ")";
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000888 }
wu@webrtc.orgcecfd182013-10-30 05:18:12 +0000889#endif // !IOS
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000890}
891
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000892bool WebRtcVoiceEngine::GetOutputVolume(int* level) {
solenberg566ef242015-11-06 15:34:49 -0800893 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000894 unsigned int ulevel;
895 if (voe_wrapper_->volume()->GetSpeakerVolume(ulevel) == -1) {
896 LOG_RTCERR1(GetSpeakerVolume, level);
897 return false;
898 }
899 *level = ulevel;
900 return true;
901}
902
903bool WebRtcVoiceEngine::SetOutputVolume(int level) {
solenberg566ef242015-11-06 15:34:49 -0800904 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
henrikg91d6ede2015-09-17 00:24:34 -0700905 RTC_DCHECK(level >= 0 && level <= 255);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000906 if (voe_wrapper_->volume()->SetSpeakerVolume(level) == -1) {
907 LOG_RTCERR1(SetSpeakerVolume, level);
908 return false;
909 }
910 return true;
911}
912
913int WebRtcVoiceEngine::GetInputLevel() {
solenberg566ef242015-11-06 15:34:49 -0800914 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000915 unsigned int ulevel;
916 return (voe_wrapper_->volume()->GetSpeechInputLevel(ulevel) != -1) ?
917 static_cast<int>(ulevel) : -1;
918}
919
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000920const std::vector<AudioCodec>& WebRtcVoiceEngine::codecs() {
solenberg566ef242015-11-06 15:34:49 -0800921 RTC_DCHECK(signal_thread_checker_.CalledOnValidThread());
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000922 return codecs_;
923}
924
Stefan Holmer9d69c3f2015-12-07 10:45:43 +0100925RtpCapabilities WebRtcVoiceEngine::GetCapabilities() const {
solenberg566ef242015-11-06 15:34:49 -0800926 RTC_DCHECK(signal_thread_checker_.CalledOnValidThread());
Stefan Holmer9d69c3f2015-12-07 10:45:43 +0100927 RtpCapabilities capabilities;
928 capabilities.header_extensions.push_back(RtpHeaderExtension(
929 kRtpAudioLevelHeaderExtension, kRtpAudioLevelHeaderExtensionDefaultId));
930 capabilities.header_extensions.push_back(
931 RtpHeaderExtension(kRtpAbsoluteSenderTimeHeaderExtension,
932 kRtpAbsoluteSenderTimeHeaderExtensionDefaultId));
933 if (webrtc::field_trial::FindFullName("WebRTC-SendSideBwe") == "Enabled") {
934 capabilities.header_extensions.push_back(RtpHeaderExtension(
935 kRtpTransportSequenceNumberHeaderExtension,
936 kRtpTransportSequenceNumberHeaderExtensionDefaultId));
937 }
938 return capabilities;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000939}
940
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000941int WebRtcVoiceEngine::GetLastEngineError() {
solenberg566ef242015-11-06 15:34:49 -0800942 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000943 return voe_wrapper_->error();
944}
945
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000946void WebRtcVoiceEngine::Print(webrtc::TraceLevel level, const char* trace,
947 int length) {
solenberg566ef242015-11-06 15:34:49 -0800948 // Note: This callback can happen on any thread!
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000949 rtc::LoggingSeverity sev = rtc::LS_VERBOSE;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000950 if (level == webrtc::kTraceError || level == webrtc::kTraceCritical)
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000951 sev = rtc::LS_ERROR;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000952 else if (level == webrtc::kTraceWarning)
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000953 sev = rtc::LS_WARNING;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000954 else if (level == webrtc::kTraceStateInfo || level == webrtc::kTraceInfo)
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000955 sev = rtc::LS_INFO;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000956 else if (level == webrtc::kTraceTerseInfo)
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000957 sev = rtc::LS_INFO;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000958
959 // Skip past boilerplate prefix text
960 if (length < 72) {
961 std::string msg(trace, length);
962 LOG(LS_ERROR) << "Malformed webrtc log message: ";
963 LOG_V(sev) << msg;
964 } else {
965 std::string msg(trace + 71, length - 72);
Peter Boströmd5c75b12015-09-23 13:24:32 +0200966 LOG_V(sev) << "webrtc: " << msg;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000967 }
968}
969
solenberg63b34542015-09-29 06:06:31 -0700970void WebRtcVoiceEngine::RegisterChannel(WebRtcVoiceMediaChannel* channel) {
solenberg566ef242015-11-06 15:34:49 -0800971 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
972 RTC_DCHECK(channel);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000973 channels_.push_back(channel);
974}
975
solenberg63b34542015-09-29 06:06:31 -0700976void WebRtcVoiceEngine::UnregisterChannel(WebRtcVoiceMediaChannel* channel) {
solenberg566ef242015-11-06 15:34:49 -0800977 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
solenberg63b34542015-09-29 06:06:31 -0700978 auto it = std::find(channels_.begin(), channels_.end(), channel);
solenberg566ef242015-11-06 15:34:49 -0800979 RTC_DCHECK(it != channels_.end());
980 channels_.erase(it);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000981}
982
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000983// Adjusts the default AGC target level by the specified delta.
984// NB: If we start messing with other config fields, we'll want
985// to save the current webrtc::AgcConfig as well.
986bool WebRtcVoiceEngine::AdjustAgcLevel(int delta) {
solenberg566ef242015-11-06 15:34:49 -0800987 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000988 webrtc::AgcConfig config = default_agc_config_;
989 config.targetLeveldBOv -= delta;
990
991 LOG(LS_INFO) << "Adjusting AGC level from default -"
992 << default_agc_config_.targetLeveldBOv << "dB to -"
993 << config.targetLeveldBOv << "dB";
994
995 if (voe_wrapper_->processing()->SetAgcConfig(config) == -1) {
996 LOG_RTCERR1(SetAgcConfig, config.targetLeveldBOv);
997 return false;
998 }
999 return true;
1000}
1001
Fredrik Solenbergccb49e72015-05-19 11:37:56 +02001002bool WebRtcVoiceEngine::SetAudioDeviceModule(webrtc::AudioDeviceModule* adm) {
solenberg566ef242015-11-06 15:34:49 -08001003 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001004 if (initialized_) {
1005 LOG(LS_WARNING) << "SetAudioDeviceModule can not be called after Init.";
1006 return false;
1007 }
1008 if (adm_) {
1009 adm_->Release();
1010 adm_ = NULL;
1011 }
1012 if (adm) {
1013 adm_ = adm;
1014 adm_->AddRef();
1015 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001016 return true;
1017}
1018
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00001019bool WebRtcVoiceEngine::StartAecDump(rtc::PlatformFile file) {
solenberg566ef242015-11-06 15:34:49 -08001020 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00001021 FILE* aec_dump_file_stream = rtc::FdopenPlatformFileForWriting(file);
wu@webrtc.orga8910d22014-01-23 22:12:45 +00001022 if (!aec_dump_file_stream) {
1023 LOG(LS_ERROR) << "Could not open AEC dump file stream.";
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00001024 if (!rtc::ClosePlatformFile(file))
wu@webrtc.orga8910d22014-01-23 22:12:45 +00001025 LOG(LS_WARNING) << "Could not close file.";
1026 return false;
1027 }
wu@webrtc.orga9890802013-12-13 00:21:03 +00001028 StopAecDump();
wu@webrtc.orga8910d22014-01-23 22:12:45 +00001029 if (voe_wrapper_->processing()->StartDebugRecording(aec_dump_file_stream) !=
wu@webrtc.orga9890802013-12-13 00:21:03 +00001030 webrtc::AudioProcessing::kNoError) {
wu@webrtc.orga8910d22014-01-23 22:12:45 +00001031 LOG_RTCERR0(StartDebugRecording);
1032 fclose(aec_dump_file_stream);
wu@webrtc.orga9890802013-12-13 00:21:03 +00001033 return false;
1034 }
1035 is_dumping_aec_ = true;
1036 return true;
wu@webrtc.orga9890802013-12-13 00:21:03 +00001037}
1038
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001039void WebRtcVoiceEngine::StartAecDump(const std::string& filename) {
solenberg566ef242015-11-06 15:34:49 -08001040 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001041 if (!is_dumping_aec_) {
1042 // Start dumping AEC when we are not dumping.
1043 if (voe_wrapper_->processing()->StartDebugRecording(
1044 filename.c_str()) != webrtc::AudioProcessing::kNoError) {
wu@webrtc.orga9890802013-12-13 00:21:03 +00001045 LOG_RTCERR1(StartDebugRecording, filename.c_str());
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001046 } else {
1047 is_dumping_aec_ = true;
1048 }
1049 }
1050}
1051
1052void WebRtcVoiceEngine::StopAecDump() {
solenberg566ef242015-11-06 15:34:49 -08001053 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001054 if (is_dumping_aec_) {
1055 // Stop dumping AEC when we are dumping.
1056 if (voe_wrapper_->processing()->StopDebugRecording() !=
1057 webrtc::AudioProcessing::kNoError) {
1058 LOG_RTCERR0(StopDebugRecording);
1059 }
1060 is_dumping_aec_ = false;
1061 }
1062}
1063
ivoc112a3d82015-10-16 02:22:18 -07001064bool WebRtcVoiceEngine::StartRtcEventLog(rtc::PlatformFile file) {
solenberg566ef242015-11-06 15:34:49 -08001065 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
ivoc112a3d82015-10-16 02:22:18 -07001066 return voe_wrapper_->codec()->GetEventLog()->StartLogging(file);
1067}
1068
1069void WebRtcVoiceEngine::StopRtcEventLog() {
solenberg566ef242015-11-06 15:34:49 -08001070 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
ivoc112a3d82015-10-16 02:22:18 -07001071 voe_wrapper_->codec()->GetEventLog()->StopLogging();
1072}
1073
solenberg0a617e22015-10-20 15:49:38 -07001074int WebRtcVoiceEngine::CreateVoEChannel() {
solenberg566ef242015-11-06 15:34:49 -08001075 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
solenberg0a617e22015-10-20 15:49:38 -07001076 return voe_wrapper_->base()->CreateChannel(voe_config_);
sergeyu@chromium.org5bc25c42013-12-05 00:24:06 +00001077}
1078
solenbergc96df772015-10-21 13:01:53 -07001079class WebRtcVoiceMediaChannel::WebRtcAudioSendStream
mallinath@webrtc.org67ee6b92014-02-03 16:57:16 +00001080 : public AudioRenderer::Sink {
1081 public:
solenbergc96df772015-10-21 13:01:53 -07001082 WebRtcAudioSendStream(int ch, webrtc::AudioTransport* voe_audio_transport,
solenberg3a941542015-11-16 07:34:50 -08001083 uint32_t ssrc, const std::string& c_name,
1084 const std::vector<webrtc::RtpExtension>& extensions,
1085 webrtc::Call* call)
solenberg7add0582015-11-20 09:59:34 -08001086 : voe_audio_transport_(voe_audio_transport),
solenberg3a941542015-11-16 07:34:50 -08001087 call_(call),
1088 config_(nullptr) {
solenberg85a04962015-10-27 03:35:21 -07001089 RTC_DCHECK_GE(ch, 0);
1090 // TODO(solenberg): Once we're not using FakeWebRtcVoiceEngine anymore:
1091 // RTC_DCHECK(voe_audio_transport);
solenbergc96df772015-10-21 13:01:53 -07001092 RTC_DCHECK(call);
solenberg85a04962015-10-27 03:35:21 -07001093 audio_capture_thread_checker_.DetachFromThread();
solenberg3a941542015-11-16 07:34:50 -08001094 config_.rtp.ssrc = ssrc;
1095 config_.rtp.c_name = c_name;
1096 config_.voe_channel_id = ch;
1097 RecreateAudioSendStream(extensions);
solenbergc96df772015-10-21 13:01:53 -07001098 }
solenberg3a941542015-11-16 07:34:50 -08001099
solenbergc96df772015-10-21 13:01:53 -07001100 ~WebRtcAudioSendStream() override {
solenberg566ef242015-11-06 15:34:49 -08001101 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
solenbergc96df772015-10-21 13:01:53 -07001102 Stop();
1103 call_->DestroyAudioSendStream(stream_);
1104 }
henrike@webrtc.org1e09a712013-07-26 19:17:59 +00001105
solenberg3a941542015-11-16 07:34:50 -08001106 void RecreateAudioSendStream(
1107 const std::vector<webrtc::RtpExtension>& extensions) {
1108 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
1109 if (stream_) {
1110 call_->DestroyAudioSendStream(stream_);
1111 stream_ = nullptr;
1112 }
1113 config_.rtp.extensions = extensions;
1114 RTC_DCHECK(!stream_);
1115 stream_ = call_->CreateAudioSendStream(config_);
1116 RTC_CHECK(stream_);
1117 }
1118
Fredrik Solenbergb5727682015-12-04 15:22:19 +01001119 bool SendTelephoneEvent(int payload_type, uint8_t event,
1120 uint32_t duration_ms) {
1121 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
1122 RTC_DCHECK(stream_);
1123 return stream_->SendTelephoneEvent(payload_type, event, duration_ms);
1124 }
1125
solenberg3a941542015-11-16 07:34:50 -08001126 webrtc::AudioSendStream::Stats GetStats() const {
1127 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
1128 RTC_DCHECK(stream_);
1129 return stream_->GetStats();
1130 }
1131
mallinath@webrtc.org67ee6b92014-02-03 16:57:16 +00001132 // Starts the rendering by setting a sink to the renderer to get data
1133 // callback.
henrike@webrtc.orga7b98182014-02-21 15:51:43 +00001134 // This method is called on the libjingle worker thread.
mallinath@webrtc.org67ee6b92014-02-03 16:57:16 +00001135 // TODO(xians): Make sure Start() is called only once.
1136 void Start(AudioRenderer* renderer) {
solenberg566ef242015-11-06 15:34:49 -08001137 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
solenbergc96df772015-10-21 13:01:53 -07001138 RTC_DCHECK(renderer);
1139 if (renderer_) {
henrikg91d6ede2015-09-17 00:24:34 -07001140 RTC_DCHECK(renderer_ == renderer);
mallinath@webrtc.org67ee6b92014-02-03 16:57:16 +00001141 return;
1142 }
mallinath@webrtc.org67ee6b92014-02-03 16:57:16 +00001143 renderer->SetSink(this);
1144 renderer_ = renderer;
1145 }
1146
solenbergc96df772015-10-21 13:01:53 -07001147 // Stops rendering by setting the sink of the renderer to nullptr. No data
mallinath@webrtc.org67ee6b92014-02-03 16:57:16 +00001148 // callback will be received after this method.
henrike@webrtc.orga7b98182014-02-21 15:51:43 +00001149 // This method is called on the libjingle worker thread.
mallinath@webrtc.org67ee6b92014-02-03 16:57:16 +00001150 void Stop() {
solenberg566ef242015-11-06 15:34:49 -08001151 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
solenbergc96df772015-10-21 13:01:53 -07001152 if (renderer_) {
1153 renderer_->SetSink(nullptr);
1154 renderer_ = nullptr;
solenberg98c68862015-10-09 03:27:14 -07001155 }
mallinath@webrtc.org67ee6b92014-02-03 16:57:16 +00001156 }
1157
1158 // AudioRenderer::Sink implementation.
henrike@webrtc.orga7b98182014-02-21 15:51:43 +00001159 // This method is called on the audio thread.
kjellander@webrtc.org14665ff2015-03-04 12:58:35 +00001160 void OnData(const void* audio_data,
1161 int bits_per_sample,
1162 int sample_rate,
1163 int number_of_channels,
Peter Kastingdce40cf2015-08-24 14:52:23 -07001164 size_t number_of_frames) override {
solenberg566ef242015-11-06 15:34:49 -08001165 RTC_DCHECK(!worker_thread_checker_.CalledOnValidThread());
solenberg85a04962015-10-27 03:35:21 -07001166 RTC_DCHECK(audio_capture_thread_checker_.CalledOnValidThread());
solenbergc96df772015-10-21 13:01:53 -07001167 RTC_DCHECK(voe_audio_transport_);
solenberg7add0582015-11-20 09:59:34 -08001168 voe_audio_transport_->OnData(config_.voe_channel_id,
henrike@webrtc.orga7b98182014-02-21 15:51:43 +00001169 audio_data,
1170 bits_per_sample,
1171 sample_rate,
1172 number_of_channels,
1173 number_of_frames);
henrike@webrtc.orga7b98182014-02-21 15:51:43 +00001174 }
1175
1176 // Callback from the |renderer_| when it is going away. In case Start() has
1177 // never been called, this callback won't be triggered.
kjellander@webrtc.org14665ff2015-03-04 12:58:35 +00001178 void OnClose() override {
solenberg566ef242015-11-06 15:34:49 -08001179 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
solenbergc96df772015-10-21 13:01:53 -07001180 // Set |renderer_| to nullptr to make sure no more callback will get into
henrike@webrtc.orga7b98182014-02-21 15:51:43 +00001181 // the renderer.
solenbergc96df772015-10-21 13:01:53 -07001182 renderer_ = nullptr;
mallinath@webrtc.org67ee6b92014-02-03 16:57:16 +00001183 }
1184
1185 // Accessor to the VoE channel ID.
solenberg85a04962015-10-27 03:35:21 -07001186 int channel() const {
solenberg566ef242015-11-06 15:34:49 -08001187 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
solenberg7add0582015-11-20 09:59:34 -08001188 return config_.voe_channel_id;
solenberg85a04962015-10-27 03:35:21 -07001189 }
mallinath@webrtc.org67ee6b92014-02-03 16:57:16 +00001190
1191 private:
solenberg566ef242015-11-06 15:34:49 -08001192 rtc::ThreadChecker worker_thread_checker_;
solenberg85a04962015-10-27 03:35:21 -07001193 rtc::ThreadChecker audio_capture_thread_checker_;
solenbergc96df772015-10-21 13:01:53 -07001194 webrtc::AudioTransport* const voe_audio_transport_ = nullptr;
1195 webrtc::Call* call_ = nullptr;
solenberg3a941542015-11-16 07:34:50 -08001196 webrtc::AudioSendStream::Config config_;
1197 // The stream is owned by WebRtcAudioSendStream and may be reallocated if
1198 // configuration changes.
solenbergc96df772015-10-21 13:01:53 -07001199 webrtc::AudioSendStream* stream_ = nullptr;
mallinath@webrtc.org67ee6b92014-02-03 16:57:16 +00001200
1201 // Raw pointer to AudioRenderer owned by LocalAudioTrackHandler.
1202 // PeerConnection will make sure invalidating the pointer before the object
1203 // goes away.
solenbergc96df772015-10-21 13:01:53 -07001204 AudioRenderer* renderer_ = nullptr;
henrike@webrtc.orga7b98182014-02-21 15:51:43 +00001205
solenbergc96df772015-10-21 13:01:53 -07001206 RTC_DISALLOW_IMPLICIT_CONSTRUCTORS(WebRtcAudioSendStream);
1207};
1208
1209class WebRtcVoiceMediaChannel::WebRtcAudioReceiveStream {
1210 public:
solenberg7add0582015-11-20 09:59:34 -08001211 WebRtcAudioReceiveStream(int ch, uint32_t remote_ssrc, uint32_t local_ssrc,
1212 bool use_combined_bwe, const std::string& sync_group,
1213 const std::vector<webrtc::RtpExtension>& extensions,
1214 webrtc::Call* call)
1215 : call_(call),
1216 config_() {
1217 RTC_DCHECK_GE(ch, 0);
1218 RTC_DCHECK(call);
1219 config_.rtp.remote_ssrc = remote_ssrc;
1220 config_.rtp.local_ssrc = local_ssrc;
1221 config_.voe_channel_id = ch;
1222 config_.sync_group = sync_group;
1223 RecreateAudioReceiveStream(use_combined_bwe, extensions);
1224 }
solenbergc96df772015-10-21 13:01:53 -07001225
solenberg7add0582015-11-20 09:59:34 -08001226 ~WebRtcAudioReceiveStream() {
1227 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
1228 call_->DestroyAudioReceiveStream(stream_);
1229 }
1230
1231 void RecreateAudioReceiveStream(
1232 const std::vector<webrtc::RtpExtension>& extensions) {
1233 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
1234 RecreateAudioReceiveStream(config_.combined_audio_video_bwe, extensions);
1235 }
1236 void RecreateAudioReceiveStream(bool use_combined_bwe) {
1237 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
1238 RecreateAudioReceiveStream(use_combined_bwe, config_.rtp.extensions);
1239 }
1240
1241 webrtc::AudioReceiveStream::Stats GetStats() const {
1242 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
1243 RTC_DCHECK(stream_);
1244 return stream_->GetStats();
1245 }
1246
1247 int channel() const {
1248 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
1249 return config_.voe_channel_id;
1250 }
solenbergc96df772015-10-21 13:01:53 -07001251
Tommif888bb52015-12-12 01:37:01 +01001252 void SetRawAudioSink(rtc::scoped_ptr<webrtc::AudioSinkInterface> sink) {
1253 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
1254 stream_->SetSink(std::move(sink));
1255 }
1256
solenbergc96df772015-10-21 13:01:53 -07001257 private:
solenberg7add0582015-11-20 09:59:34 -08001258 void RecreateAudioReceiveStream(bool use_combined_bwe,
1259 const std::vector<webrtc::RtpExtension>& extensions) {
1260 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
1261 if (stream_) {
1262 call_->DestroyAudioReceiveStream(stream_);
1263 stream_ = nullptr;
1264 }
1265 config_.rtp.extensions = extensions;
1266 config_.combined_audio_video_bwe = use_combined_bwe;
1267 RTC_DCHECK(!stream_);
1268 stream_ = call_->CreateAudioReceiveStream(config_);
1269 RTC_CHECK(stream_);
1270 }
1271
1272 rtc::ThreadChecker worker_thread_checker_;
1273 webrtc::Call* call_ = nullptr;
1274 webrtc::AudioReceiveStream::Config config_;
1275 // The stream is owned by WebRtcAudioReceiveStream and may be reallocated if
1276 // configuration changes.
1277 webrtc::AudioReceiveStream* stream_ = nullptr;
solenbergc96df772015-10-21 13:01:53 -07001278
1279 RTC_DISALLOW_IMPLICIT_CONSTRUCTORS(WebRtcAudioReceiveStream);
henrike@webrtc.org1e09a712013-07-26 19:17:59 +00001280};
1281
Fredrik Solenberg709ed672015-09-15 12:26:33 +02001282WebRtcVoiceMediaChannel::WebRtcVoiceMediaChannel(WebRtcVoiceEngine* engine,
Fredrik Solenbergb071a192015-09-17 16:42:56 +02001283 const AudioOptions& options,
Fredrik Solenberg709ed672015-09-15 12:26:33 +02001284 webrtc::Call* call)
solenberg566ef242015-11-06 15:34:49 -08001285 : engine_(engine), call_(call) {
solenberg0a617e22015-10-20 15:49:38 -07001286 LOG(LS_VERBOSE) << "WebRtcVoiceMediaChannel::WebRtcVoiceMediaChannel";
solenberg566ef242015-11-06 15:34:49 -08001287 RTC_DCHECK(call);
solenberg0a617e22015-10-20 15:49:38 -07001288 engine->RegisterChannel(this);
Fredrik Solenbergb071a192015-09-17 16:42:56 +02001289 SetOptions(options);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001290}
1291
1292WebRtcVoiceMediaChannel::~WebRtcVoiceMediaChannel() {
solenberg566ef242015-11-06 15:34:49 -08001293 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
solenberg0a617e22015-10-20 15:49:38 -07001294 LOG(LS_VERBOSE) << "WebRtcVoiceMediaChannel::~WebRtcVoiceMediaChannel";
solenberg7add0582015-11-20 09:59:34 -08001295 // TODO(solenberg): Should be able to delete the streams directly, without
1296 // going through RemoveNnStream(), once stream objects handle
1297 // all (de)configuration.
solenbergc96df772015-10-21 13:01:53 -07001298 while (!send_streams_.empty()) {
1299 RemoveSendStream(send_streams_.begin()->first);
solenbergd97ec302015-10-07 01:40:33 -07001300 }
solenberg7add0582015-11-20 09:59:34 -08001301 while (!recv_streams_.empty()) {
1302 RemoveRecvStream(recv_streams_.begin()->first);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001303 }
solenberg0a617e22015-10-20 15:49:38 -07001304 engine()->UnregisterChannel(this);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001305}
1306
Peter Thatcherc2ee2c82015-08-07 16:05:34 -07001307bool WebRtcVoiceMediaChannel::SetSendParameters(
1308 const AudioSendParameters& params) {
solenberg566ef242015-11-06 15:34:49 -08001309 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
solenberg7e4e01a2015-12-02 08:05:01 -08001310 LOG(LS_INFO) << "WebRtcVoiceMediaChannel::SetSendParameters: "
1311 << params.ToString();
Peter Thatcherc2ee2c82015-08-07 16:05:34 -07001312 // TODO(pthatcher): Refactor this to be more clean now that we have
1313 // all the information at once.
solenberg3a941542015-11-16 07:34:50 -08001314
1315 if (!SetSendCodecs(params.codecs)) {
1316 return false;
1317 }
1318
solenberg7e4e01a2015-12-02 08:05:01 -08001319 if (!ValidateRtpExtensions(params.extensions)) {
1320 return false;
1321 }
1322 std::vector<webrtc::RtpExtension> filtered_extensions =
1323 FilterRtpExtensions(params.extensions,
1324 webrtc::RtpExtension::IsSupportedForAudio, true);
1325 if (send_rtp_extensions_ != filtered_extensions) {
1326 send_rtp_extensions_.swap(filtered_extensions);
solenberg3a941542015-11-16 07:34:50 -08001327 for (auto& it : send_streams_) {
1328 it.second->RecreateAudioSendStream(send_rtp_extensions_);
1329 }
1330 }
1331
1332 if (!SetMaxSendBandwidth(params.max_bandwidth_bps)) {
1333 return false;
1334 }
1335 return SetOptions(params.options);
Peter Thatcherc2ee2c82015-08-07 16:05:34 -07001336}
1337
1338bool WebRtcVoiceMediaChannel::SetRecvParameters(
1339 const AudioRecvParameters& params) {
solenberg566ef242015-11-06 15:34:49 -08001340 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
solenberg7e4e01a2015-12-02 08:05:01 -08001341 LOG(LS_INFO) << "WebRtcVoiceMediaChannel::SetRecvParameters: "
1342 << params.ToString();
Peter Thatcherc2ee2c82015-08-07 16:05:34 -07001343 // TODO(pthatcher): Refactor this to be more clean now that we have
1344 // all the information at once.
solenberg7add0582015-11-20 09:59:34 -08001345
1346 if (!SetRecvCodecs(params.codecs)) {
1347 return false;
1348 }
1349
solenberg7e4e01a2015-12-02 08:05:01 -08001350 if (!ValidateRtpExtensions(params.extensions)) {
1351 return false;
1352 }
1353 std::vector<webrtc::RtpExtension> filtered_extensions =
1354 FilterRtpExtensions(params.extensions,
1355 webrtc::RtpExtension::IsSupportedForAudio, false);
1356 if (recv_rtp_extensions_ != filtered_extensions) {
1357 recv_rtp_extensions_.swap(filtered_extensions);
solenberg7add0582015-11-20 09:59:34 -08001358 for (auto& it : recv_streams_) {
1359 it.second->RecreateAudioReceiveStream(recv_rtp_extensions_);
1360 }
1361 }
1362
1363 return true;
Peter Thatcherc2ee2c82015-08-07 16:05:34 -07001364}
1365
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001366bool WebRtcVoiceMediaChannel::SetOptions(const AudioOptions& options) {
solenberg566ef242015-11-06 15:34:49 -08001367 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001368 LOG(LS_INFO) << "Setting voice channel options: "
1369 << options.ToString();
1370
wu@webrtc.orgde305012013-10-31 15:40:38 +00001371 // Check if DSCP value is changed from previous.
1372 bool dscp_option_changed = (options_.dscp != options.dscp);
1373
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001374 // We retain all of the existing options, and apply the given ones
1375 // on top. This means there is no way to "clear" options such that
1376 // they go back to the engine default.
1377 options_.SetAll(options);
solenberg246b8172015-12-08 09:50:23 -08001378 if (!engine()->ApplyOptions(options_)) {
1379 LOG(LS_WARNING) <<
1380 "Failed to apply engine options during channel SetOptions.";
1381 return false;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001382 }
1383
wu@webrtc.orgde305012013-10-31 15:40:38 +00001384 if (dscp_option_changed) {
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00001385 rtc::DiffServCodePoint dscp = rtc::DSCP_DEFAULT;
solenberg246b8172015-12-08 09:50:23 -08001386 if (options_.dscp.value_or(false)) {
wu@webrtc.orgde305012013-10-31 15:40:38 +00001387 dscp = kAudioDscpValue;
solenberg246b8172015-12-08 09:50:23 -08001388 }
wu@webrtc.orgde305012013-10-31 15:40:38 +00001389 if (MediaChannel::SetDscp(dscp) != 0) {
1390 LOG(LS_WARNING) << "Failed to set DSCP settings for audio channel";
1391 }
1392 }
solenberg8fb30c32015-10-13 03:06:58 -07001393
solenbergc96df772015-10-21 13:01:53 -07001394 // TODO(solenberg): Don't recreate unless options changed.
solenberg7add0582015-11-20 09:59:34 -08001395 for (auto& it : recv_streams_) {
1396 it.second->RecreateAudioReceiveStream(
1397 options_.combined_audio_video_bwe.value_or(false));
1398 }
solenberg8fb30c32015-10-13 03:06:58 -07001399
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001400 LOG(LS_INFO) << "Set voice channel options. Current options: "
1401 << options_.ToString();
1402 return true;
1403}
1404
1405bool WebRtcVoiceMediaChannel::SetRecvCodecs(
1406 const std::vector<AudioCodec>& codecs) {
solenberg566ef242015-11-06 15:34:49 -08001407 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
solenberg8fb30c32015-10-13 03:06:58 -07001408
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001409 // Set the payload types to be used for incoming media.
solenberg0b675462015-10-09 01:37:09 -07001410 LOG(LS_INFO) << "Setting receive voice codecs.";
solenberg0b675462015-10-09 01:37:09 -07001411
1412 if (!VerifyUniquePayloadTypes(codecs)) {
1413 LOG(LS_ERROR) << "Codec payload types overlap.";
1414 return false;
1415 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001416
1417 std::vector<AudioCodec> new_codecs;
1418 // Find all new codecs. We allow adding new codecs but don't allow changing
1419 // the payload type of codecs that is already configured since we might
1420 // already be receiving packets with that payload type.
Fredrik Solenbergaf9fb212015-08-26 10:45:53 +02001421 for (const AudioCodec& codec : codecs) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001422 AudioCodec old_codec;
Fredrik Solenbergaf9fb212015-08-26 10:45:53 +02001423 if (FindCodec(recv_codecs_, codec, &old_codec)) {
1424 if (old_codec.id != codec.id) {
1425 LOG(LS_ERROR) << codec.name << " payload type changed.";
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001426 return false;
1427 }
1428 } else {
Fredrik Solenbergaf9fb212015-08-26 10:45:53 +02001429 new_codecs.push_back(codec);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001430 }
1431 }
1432 if (new_codecs.empty()) {
1433 // There are no new codecs to configure. Already configured codecs are
1434 // never removed.
1435 return true;
1436 }
1437
1438 if (playout_) {
1439 // Receive codecs can not be changed while playing. So we temporarily
1440 // pause playout.
1441 PausePlayout();
1442 }
1443
solenberg26c8c912015-11-27 04:00:25 -08001444 bool result = true;
1445 for (const AudioCodec& codec : new_codecs) {
1446 webrtc::CodecInst voe_codec;
1447 if (WebRtcVoiceEngine::ToCodecInst(codec, &voe_codec)) {
1448 LOG(LS_INFO) << ToString(codec);
1449 voe_codec.pltype = codec.id;
1450 for (const auto& ch : recv_streams_) {
1451 if (engine()->voe()->codec()->SetRecPayloadType(
1452 ch.second->channel(), voe_codec) == -1) {
1453 LOG_RTCERR2(SetRecPayloadType, ch.second->channel(),
1454 ToString(voe_codec));
1455 result = false;
1456 }
1457 }
1458 } else {
1459 LOG(LS_WARNING) << "Unknown codec " << ToString(codec);
1460 result = false;
1461 break;
1462 }
1463 }
Fredrik Solenbergaf9fb212015-08-26 10:45:53 +02001464 if (result) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001465 recv_codecs_ = codecs;
1466 }
1467
1468 if (desired_playout_ && !playout_) {
1469 ResumePlayout();
1470 }
Fredrik Solenbergaf9fb212015-08-26 10:45:53 +02001471 return result;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001472}
1473
1474bool WebRtcVoiceMediaChannel::SetSendCodecs(
wu@webrtc.orgcadf9042013-08-30 21:24:16 +00001475 int channel, const std::vector<AudioCodec>& codecs) {
buildbot@webrtc.orgae740dd2014-06-17 10:56:41 +00001476 // Disable VAD, FEC, and RED unless we know the other side wants them.
wu@webrtc.orgcadf9042013-08-30 21:24:16 +00001477 engine()->voe()->codec()->SetVADStatus(channel, false);
1478 engine()->voe()->rtp()->SetNACKStatus(channel, false, 0);
buildbot@webrtc.orgae740dd2014-06-17 10:56:41 +00001479 engine()->voe()->rtp()->SetREDStatus(channel, false);
1480 engine()->voe()->codec()->SetFECStatus(channel, false);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001481
1482 // Scan through the list to figure out the codec to use for sending, along
Fredrik Solenbergb5727682015-12-04 15:22:19 +01001483 // with the proper configuration for VAD.
henrike@webrtc.org704bf9e2014-02-27 17:52:04 +00001484 bool found_send_codec = false;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001485 webrtc::CodecInst send_codec;
1486 memset(&send_codec, 0, sizeof(send_codec));
1487
wu@webrtc.org05e7b442014-04-01 17:44:24 +00001488 bool nack_enabled = nack_enabled_;
buildbot@webrtc.org3ffa1f92014-07-02 19:51:26 +00001489 bool enable_codec_fec = false;
Minyue Li7100dcd2015-03-27 05:05:59 +01001490 bool enable_opus_dtx = false;
minyue@webrtc.org26236952014-10-29 02:27:08 +00001491 int opus_max_playback_rate = 0;
buildbot@webrtc.org5d639b32014-09-10 07:57:12 +00001492
henrike@webrtc.org704bf9e2014-02-27 17:52:04 +00001493 // Set send codec (the first non-telephone-event/CN codec)
Fredrik Solenbergaf9fb212015-08-26 10:45:53 +02001494 for (const AudioCodec& codec : codecs) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001495 // Ignore codecs we don't know about. The negotiation step should prevent
1496 // this, but double-check to be sure.
1497 webrtc::CodecInst voe_codec;
solenberg26c8c912015-11-27 04:00:25 -08001498 if (!WebRtcVoiceEngine::ToCodecInst(codec, &voe_codec)) {
Fredrik Solenbergaf9fb212015-08-26 10:45:53 +02001499 LOG(LS_WARNING) << "Unknown codec " << ToString(codec);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001500 continue;
1501 }
1502
Fredrik Solenbergaf9fb212015-08-26 10:45:53 +02001503 if (IsCodec(codec, kDtmfCodecName) || IsCodec(codec, kCnCodecName)) {
henrike@webrtc.org704bf9e2014-02-27 17:52:04 +00001504 // Skip telephone-event/CN codec, which will be handled later.
1505 continue;
1506 }
1507
henrike@webrtc.org704bf9e2014-02-27 17:52:04 +00001508 // We'll use the first codec in the list to actually send audio data.
1509 // Be sure to use the payload type requested by the remote side.
buildbot@webrtc.orgae740dd2014-06-17 10:56:41 +00001510 // "red", for RED audio, is a special case where the actual codec to be
henrike@webrtc.org704bf9e2014-02-27 17:52:04 +00001511 // used is specified in params.
Fredrik Solenbergaf9fb212015-08-26 10:45:53 +02001512 if (IsCodec(codec, kRedCodecName)) {
henrike@webrtc.org704bf9e2014-02-27 17:52:04 +00001513 // Parse out the RED parameters. If we fail, just ignore RED;
1514 // we don't support all possible params/usage scenarios.
Fredrik Solenbergaf9fb212015-08-26 10:45:53 +02001515 if (!GetRedSendCodec(codec, codecs, &send_codec)) {
henrike@webrtc.org704bf9e2014-02-27 17:52:04 +00001516 continue;
1517 }
1518
1519 // Enable redundant encoding of the specified codec. Treat any
1520 // failure as a fatal internal error.
buildbot@webrtc.orgae740dd2014-06-17 10:56:41 +00001521 LOG(LS_INFO) << "Enabling RED on channel " << channel;
Fredrik Solenbergaf9fb212015-08-26 10:45:53 +02001522 if (engine()->voe()->rtp()->SetREDStatus(channel, true, codec.id) == -1) {
1523 LOG_RTCERR3(SetREDStatus, channel, true, codec.id);
henrike@webrtc.org704bf9e2014-02-27 17:52:04 +00001524 return false;
1525 }
1526 } else {
1527 send_codec = voe_codec;
Fredrik Solenbergaf9fb212015-08-26 10:45:53 +02001528 nack_enabled = IsNackEnabled(codec);
Minyue Li7100dcd2015-03-27 05:05:59 +01001529 // For Opus as the send codec, we are to determine inband FEC, maximum
1530 // playback rate, and opus internal dtx.
Fredrik Solenbergaf9fb212015-08-26 10:45:53 +02001531 if (IsCodec(codec, kOpusCodecName)) {
1532 GetOpusConfig(codec, &send_codec, &enable_codec_fec,
Minyue Li7100dcd2015-03-27 05:05:59 +01001533 &opus_max_playback_rate, &enable_opus_dtx);
buildbot@webrtc.org5d639b32014-09-10 07:57:12 +00001534 }
Brave Yao5225dd82015-03-26 07:39:19 +08001535
1536 // Set packet size if the AudioCodec param kCodecParamPTime is set.
1537 int ptime_ms = 0;
Fredrik Solenbergaf9fb212015-08-26 10:45:53 +02001538 if (codec.GetParam(kCodecParamPTime, &ptime_ms)) {
solenberg26c8c912015-11-27 04:00:25 -08001539 if (!WebRtcVoiceCodecs::SetPTimeAsPacketSize(&send_codec, ptime_ms)) {
Brave Yao5225dd82015-03-26 07:39:19 +08001540 LOG(LS_WARNING) << "Failed to set packet size for codec "
1541 << send_codec.plname;
1542 return false;
1543 }
1544 }
henrike@webrtc.org704bf9e2014-02-27 17:52:04 +00001545 }
1546 found_send_codec = true;
1547 break;
1548 }
1549
wu@webrtc.org05e7b442014-04-01 17:44:24 +00001550 if (nack_enabled_ != nack_enabled) {
1551 SetNack(channel, nack_enabled);
1552 nack_enabled_ = nack_enabled;
1553 }
1554
henrike@webrtc.org704bf9e2014-02-27 17:52:04 +00001555 if (!found_send_codec) {
1556 LOG(LS_WARNING) << "Received empty list of codecs.";
1557 return false;
1558 }
1559
1560 // Set the codec immediately, since SetVADStatus() depends on whether
1561 // the current codec is mono or stereo.
1562 if (!SetSendCodec(channel, send_codec))
1563 return false;
1564
buildbot@webrtc.org3ffa1f92014-07-02 19:51:26 +00001565 // FEC should be enabled after SetSendCodec.
1566 if (enable_codec_fec) {
1567 LOG(LS_INFO) << "Attempt to enable codec internal FEC on channel "
1568 << channel;
buildbot@webrtc.org3ffa1f92014-07-02 19:51:26 +00001569 if (engine()->voe()->codec()->SetFECStatus(channel, true) == -1) {
1570 // Enable codec internal FEC. Treat any failure as fatal internal error.
1571 LOG_RTCERR2(SetFECStatus, channel, true);
1572 return false;
1573 }
buildbot@webrtc.org3ffa1f92014-07-02 19:51:26 +00001574 }
1575
Minyue Li7100dcd2015-03-27 05:05:59 +01001576 if (IsCodec(send_codec, kOpusCodecName)) {
1577 // DTX and maxplaybackrate should be set after SetSendCodec. Because current
1578 // send codec has to be Opus.
1579
1580 // Set Opus internal DTX.
1581 LOG(LS_INFO) << "Attempt to "
solenbergbd138382015-11-20 16:08:07 -08001582 << (enable_opus_dtx ? "enable" : "disable")
Minyue Li7100dcd2015-03-27 05:05:59 +01001583 << " Opus DTX on channel "
buildbot@webrtc.org5d639b32014-09-10 07:57:12 +00001584 << channel;
Minyue Li7100dcd2015-03-27 05:05:59 +01001585 if (engine()->voe()->codec()->SetOpusDtx(channel, enable_opus_dtx)) {
1586 LOG_RTCERR2(SetOpusDtx, channel, enable_opus_dtx);
1587 return false;
1588 }
1589
1590 // If opus_max_playback_rate <= 0, the default maximum playback rate
1591 // (48 kHz) will be used.
1592 if (opus_max_playback_rate > 0) {
1593 LOG(LS_INFO) << "Attempt to set maximum playback rate to "
1594 << opus_max_playback_rate
1595 << " Hz on channel "
1596 << channel;
1597 if (engine()->voe()->codec()->SetOpusMaxPlaybackRate(
1598 channel, opus_max_playback_rate) == -1) {
1599 LOG_RTCERR2(SetOpusMaxPlaybackRate, channel, opus_max_playback_rate);
1600 return false;
1601 }
buildbot@webrtc.org5d639b32014-09-10 07:57:12 +00001602 }
buildbot@webrtc.org5d639b32014-09-10 07:57:12 +00001603 }
1604
henrike@webrtc.org704bf9e2014-02-27 17:52:04 +00001605 // Always update the |send_codec_| to the currently set send codec.
1606 send_codec_.reset(new webrtc::CodecInst(send_codec));
1607
minyue@webrtc.org26236952014-10-29 02:27:08 +00001608 if (send_bitrate_setting_) {
1609 SetSendBitrateInternal(send_bitrate_bps_);
henrike@webrtc.org704bf9e2014-02-27 17:52:04 +00001610 }
1611
Fredrik Solenbergb5727682015-12-04 15:22:19 +01001612 // Loop through the codecs list again to config the CN codec.
Fredrik Solenbergaf9fb212015-08-26 10:45:53 +02001613 for (const AudioCodec& codec : codecs) {
henrike@webrtc.org704bf9e2014-02-27 17:52:04 +00001614 // Ignore codecs we don't know about. The negotiation step should prevent
1615 // this, but double-check to be sure.
1616 webrtc::CodecInst voe_codec;
solenberg26c8c912015-11-27 04:00:25 -08001617 if (!WebRtcVoiceEngine::ToCodecInst(codec, &voe_codec)) {
Fredrik Solenbergaf9fb212015-08-26 10:45:53 +02001618 LOG(LS_WARNING) << "Unknown codec " << ToString(codec);
henrike@webrtc.org704bf9e2014-02-27 17:52:04 +00001619 continue;
1620 }
1621
Fredrik Solenbergb5727682015-12-04 15:22:19 +01001622 if (IsCodec(codec, kCnCodecName)) {
henrike@webrtc.org704bf9e2014-02-27 17:52:04 +00001623 // Turn voice activity detection/comfort noise on if supported.
1624 // Set the wideband CN payload type appropriately.
1625 // (narrowband always uses the static payload type 13).
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001626 webrtc::PayloadFrequencies cn_freq;
Fredrik Solenbergaf9fb212015-08-26 10:45:53 +02001627 switch (codec.clockrate) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001628 case 8000:
1629 cn_freq = webrtc::kFreq8000Hz;
1630 break;
1631 case 16000:
1632 cn_freq = webrtc::kFreq16000Hz;
1633 break;
1634 case 32000:
1635 cn_freq = webrtc::kFreq32000Hz;
1636 break;
1637 default:
Fredrik Solenbergaf9fb212015-08-26 10:45:53 +02001638 LOG(LS_WARNING) << "CN frequency " << codec.clockrate
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001639 << " not supported.";
1640 continue;
1641 }
wu@webrtc.orgcadf9042013-08-30 21:24:16 +00001642 // Set the CN payloadtype and the VAD status.
1643 // The CN payload type for 8000 Hz clockrate is fixed at 13.
1644 if (cn_freq != webrtc::kFreq8000Hz) {
1645 if (engine()->voe()->codec()->SetSendCNPayloadType(
Fredrik Solenbergaf9fb212015-08-26 10:45:53 +02001646 channel, codec.id, cn_freq) == -1) {
1647 LOG_RTCERR3(SetSendCNPayloadType, channel, codec.id, cn_freq);
wu@webrtc.orgcadf9042013-08-30 21:24:16 +00001648 // TODO(ajm): This failure condition will be removed from VoE.
1649 // Restore the return here when we update to a new enough webrtc.
1650 //
1651 // Not returning false because the SetSendCNPayloadType will fail if
1652 // the channel is already sending.
1653 // This can happen if the remote description is applied twice, for
1654 // example in the case of ROAP on top of JSEP, where both side will
1655 // send the offer.
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001656 }
wu@webrtc.orgcadf9042013-08-30 21:24:16 +00001657 }
wu@webrtc.orgcadf9042013-08-30 21:24:16 +00001658 // Only turn on VAD if we have a CN payload type that matches the
1659 // clockrate for the codec we are going to use.
Fredrik Solenbergaf9fb212015-08-26 10:45:53 +02001660 if (codec.clockrate == send_codec.plfreq && send_codec.channels != 2) {
Minyue Li7100dcd2015-03-27 05:05:59 +01001661 // TODO(minyue): If CN frequency == 48000 Hz is allowed, consider the
1662 // interaction between VAD and Opus FEC.
wu@webrtc.orgcadf9042013-08-30 21:24:16 +00001663 LOG(LS_INFO) << "Enabling VAD";
1664 if (engine()->voe()->codec()->SetVADStatus(channel, true) == -1) {
1665 LOG_RTCERR2(SetVADStatus, channel, true);
1666 return false;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001667 }
1668 }
1669 }
wu@webrtc.org1d1ffc92013-10-16 18:12:02 +00001670 }
wu@webrtc.orgcadf9042013-08-30 21:24:16 +00001671 return true;
1672}
1673
1674bool WebRtcVoiceMediaChannel::SetSendCodecs(
1675 const std::vector<AudioCodec>& codecs) {
solenberg566ef242015-11-06 15:34:49 -08001676 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
Fredrik Solenbergb5727682015-12-04 15:22:19 +01001677 // TODO(solenberg): Validate input - that payload types don't overlap, are
1678 // within range, filter out codecs we don't support,
1679 // redundant codecs etc.
solenbergd97ec302015-10-07 01:40:33 -07001680
Fredrik Solenbergb5727682015-12-04 15:22:19 +01001681 // Find the DTMF telephone event "codec" payload type.
1682 dtmf_payload_type_ = rtc::Optional<int>();
Fredrik Solenbergaf9fb212015-08-26 10:45:53 +02001683 for (const AudioCodec& codec : codecs) {
Fredrik Solenbergaf9fb212015-08-26 10:45:53 +02001684 if (IsCodec(codec, kDtmfCodecName)) {
Fredrik Solenbergb5727682015-12-04 15:22:19 +01001685 dtmf_payload_type_ = rtc::Optional<int>(codec.id);
1686 break;
wu@webrtc.orgcadf9042013-08-30 21:24:16 +00001687 }
1688 }
1689
1690 // Cache the codecs in order to configure the channel created later.
1691 send_codecs_ = codecs;
solenbergc96df772015-10-21 13:01:53 -07001692 for (const auto& ch : send_streams_) {
Fredrik Solenbergaf9fb212015-08-26 10:45:53 +02001693 if (!SetSendCodecs(ch.second->channel(), codecs)) {
wu@webrtc.orgcadf9042013-08-30 21:24:16 +00001694 return false;
1695 }
1696 }
1697
wu@webrtc.org05e7b442014-04-01 17:44:24 +00001698 // Set nack status on receive channels and update |nack_enabled_|.
solenberg7add0582015-11-20 09:59:34 -08001699 for (const auto& ch : recv_streams_) {
solenberg0a617e22015-10-20 15:49:38 -07001700 SetNack(ch.second->channel(), nack_enabled_);
wu@webrtc.org9dba5252013-08-05 20:36:57 +00001701 }
solenberg0a617e22015-10-20 15:49:38 -07001702
1703 return true;
wu@webrtc.org9dba5252013-08-05 20:36:57 +00001704}
1705
wu@webrtc.orgcadf9042013-08-30 21:24:16 +00001706void WebRtcVoiceMediaChannel::SetNack(int channel, bool nack_enabled) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001707 if (nack_enabled) {
wu@webrtc.orgcadf9042013-08-30 21:24:16 +00001708 LOG(LS_INFO) << "Enabling NACK for channel " << channel;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001709 engine()->voe()->rtp()->SetNACKStatus(channel, true, kNackMaxPackets);
1710 } else {
wu@webrtc.orgcadf9042013-08-30 21:24:16 +00001711 LOG(LS_INFO) << "Disabling NACK for channel " << channel;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001712 engine()->voe()->rtp()->SetNACKStatus(channel, false, 0);
1713 }
1714}
1715
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001716bool WebRtcVoiceMediaChannel::SetSendCodec(
wu@webrtc.org9dba5252013-08-05 20:36:57 +00001717 int channel, const webrtc::CodecInst& send_codec) {
1718 LOG(LS_INFO) << "Send channel " << channel << " selected voice codec "
1719 << ToString(send_codec) << ", bitrate=" << send_codec.rate;
1720
wu@webrtc.org05e7b442014-04-01 17:44:24 +00001721 webrtc::CodecInst current_codec;
1722 if (engine()->voe()->codec()->GetSendCodec(channel, current_codec) == 0 &&
1723 (send_codec == current_codec)) {
1724 // Codec is already configured, we can return without setting it again.
1725 return true;
1726 }
1727
wu@webrtc.org9dba5252013-08-05 20:36:57 +00001728 if (engine()->voe()->codec()->SetSendCodec(channel, send_codec) == -1) {
1729 LOG_RTCERR2(SetSendCodec, channel, ToString(send_codec));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001730 return false;
1731 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001732 return true;
1733}
1734
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001735bool WebRtcVoiceMediaChannel::SetPlayout(bool playout) {
1736 desired_playout_ = playout;
1737 return ChangePlayout(desired_playout_);
1738}
1739
1740bool WebRtcVoiceMediaChannel::PausePlayout() {
1741 return ChangePlayout(false);
1742}
1743
1744bool WebRtcVoiceMediaChannel::ResumePlayout() {
1745 return ChangePlayout(desired_playout_);
1746}
1747
1748bool WebRtcVoiceMediaChannel::ChangePlayout(bool playout) {
solenberg566ef242015-11-06 15:34:49 -08001749 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001750 if (playout_ == playout) {
1751 return true;
1752 }
1753
solenberg7add0582015-11-20 09:59:34 -08001754 for (const auto& ch : recv_streams_) {
Fredrik Solenbergaf9fb212015-08-26 10:45:53 +02001755 if (!SetPlayout(ch.second->channel(), playout)) {
henrike@webrtc.org1e09a712013-07-26 19:17:59 +00001756 LOG(LS_ERROR) << "SetPlayout " << playout << " on channel "
Fredrik Solenbergaf9fb212015-08-26 10:45:53 +02001757 << ch.second->channel() << " failed";
solenberg1ac56142015-10-13 03:58:19 -07001758 return false;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001759 }
1760 }
solenberg1ac56142015-10-13 03:58:19 -07001761 playout_ = playout;
1762 return true;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001763}
1764
1765bool WebRtcVoiceMediaChannel::SetSend(SendFlags send) {
1766 desired_send_ = send;
solenbergc96df772015-10-21 13:01:53 -07001767 if (!send_streams_.empty()) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001768 return ChangeSend(desired_send_);
solenbergc96df772015-10-21 13:01:53 -07001769 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001770 return true;
1771}
1772
1773bool WebRtcVoiceMediaChannel::PauseSend() {
1774 return ChangeSend(SEND_NOTHING);
1775}
1776
1777bool WebRtcVoiceMediaChannel::ResumeSend() {
1778 return ChangeSend(desired_send_);
1779}
1780
1781bool WebRtcVoiceMediaChannel::ChangeSend(SendFlags send) {
1782 if (send_ == send) {
1783 return true;
1784 }
1785
solenberg246b8172015-12-08 09:50:23 -08001786 // Apply channel specific options when channel is enabled for sending.
solenberg63b34542015-09-29 06:06:31 -07001787 if (send == SEND_MICROPHONE) {
1788 engine()->ApplyOptions(options_);
1789 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001790
wu@webrtc.org9dba5252013-08-05 20:36:57 +00001791 // Change the settings on each send channel.
solenbergc96df772015-10-21 13:01:53 -07001792 for (const auto& ch : send_streams_) {
solenberg63b34542015-09-29 06:06:31 -07001793 if (!ChangeSend(ch.second->channel(), send)) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001794 return false;
solenberg63b34542015-09-29 06:06:31 -07001795 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001796 }
wu@webrtc.org9dba5252013-08-05 20:36:57 +00001797
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001798 send_ = send;
1799 return true;
1800}
1801
wu@webrtc.org9dba5252013-08-05 20:36:57 +00001802bool WebRtcVoiceMediaChannel::ChangeSend(int channel, SendFlags send) {
1803 if (send == SEND_MICROPHONE) {
1804 if (engine()->voe()->base()->StartSend(channel) == -1) {
1805 LOG_RTCERR1(StartSend, channel);
1806 return false;
1807 }
wu@webrtc.org9dba5252013-08-05 20:36:57 +00001808 } else { // SEND_NOTHING
henrikg91d6ede2015-09-17 00:24:34 -07001809 RTC_DCHECK(send == SEND_NOTHING);
wu@webrtc.org9dba5252013-08-05 20:36:57 +00001810 if (engine()->voe()->base()->StopSend(channel) == -1) {
1811 LOG_RTCERR1(StopSend, channel);
1812 return false;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001813 }
1814 }
1815
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001816 return true;
1817}
1818
Peter Boström0c4e06b2015-10-07 12:23:21 +02001819bool WebRtcVoiceMediaChannel::SetAudioSend(uint32_t ssrc,
1820 bool enable,
solenberg1dd98f32015-09-10 01:57:14 -07001821 const AudioOptions* options,
1822 AudioRenderer* renderer) {
solenberg566ef242015-11-06 15:34:49 -08001823 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
solenberg1dd98f32015-09-10 01:57:14 -07001824 // TODO(solenberg): The state change should be fully rolled back if any one of
1825 // these calls fail.
1826 if (!SetLocalRenderer(ssrc, renderer)) {
1827 return false;
1828 }
solenbergdfc8f4f2015-10-01 02:31:10 -07001829 if (!MuteStream(ssrc, !enable)) {
solenberg1dd98f32015-09-10 01:57:14 -07001830 return false;
1831 }
solenbergdfc8f4f2015-10-01 02:31:10 -07001832 if (enable && options) {
solenberg1dd98f32015-09-10 01:57:14 -07001833 return SetOptions(*options);
1834 }
1835 return true;
1836}
1837
solenberg0a617e22015-10-20 15:49:38 -07001838int WebRtcVoiceMediaChannel::CreateVoEChannel() {
1839 int id = engine()->CreateVoEChannel();
1840 if (id == -1) {
1841 LOG_RTCERR0(CreateVoEChannel);
1842 return -1;
wu@webrtc.org9dba5252013-08-05 20:36:57 +00001843 }
solenberg0a617e22015-10-20 15:49:38 -07001844 if (engine()->voe()->network()->RegisterExternalTransport(id, *this) == -1) {
1845 LOG_RTCERR2(RegisterExternalTransport, id, this);
1846 engine()->voe()->base()->DeleteChannel(id);
1847 return -1;
1848 }
1849 return id;
wu@webrtc.org9dba5252013-08-05 20:36:57 +00001850}
1851
solenberg7add0582015-11-20 09:59:34 -08001852bool WebRtcVoiceMediaChannel::DeleteVoEChannel(int channel) {
wu@webrtc.org9dba5252013-08-05 20:36:57 +00001853 if (engine()->voe()->network()->DeRegisterExternalTransport(channel) == -1) {
1854 LOG_RTCERR1(DeRegisterExternalTransport, channel);
1855 }
wu@webrtc.org9dba5252013-08-05 20:36:57 +00001856 if (engine()->voe()->base()->DeleteChannel(channel) == -1) {
1857 LOG_RTCERR1(DeleteChannel, channel);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001858 return false;
1859 }
wu@webrtc.org9dba5252013-08-05 20:36:57 +00001860 return true;
1861}
henrike@webrtc.org1e09a712013-07-26 19:17:59 +00001862
wu@webrtc.org9dba5252013-08-05 20:36:57 +00001863bool WebRtcVoiceMediaChannel::AddSendStream(const StreamParams& sp) {
solenberg566ef242015-11-06 15:34:49 -08001864 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
solenberg0a617e22015-10-20 15:49:38 -07001865 LOG(LS_INFO) << "AddSendStream: " << sp.ToString();
1866
1867 uint32_t ssrc = sp.first_ssrc();
1868 RTC_DCHECK(0 != ssrc);
1869
1870 if (GetSendChannelId(ssrc) != -1) {
1871 LOG(LS_ERROR) << "Stream already exists with ssrc " << ssrc;
wu@webrtc.org9dba5252013-08-05 20:36:57 +00001872 return false;
1873 }
1874
solenberg0a617e22015-10-20 15:49:38 -07001875 // Create a new channel for sending audio data.
1876 int channel = CreateVoEChannel();
1877 if (channel == -1) {
1878 return false;
wu@webrtc.org9dba5252013-08-05 20:36:57 +00001879 }
wu@webrtc.org9dba5252013-08-05 20:36:57 +00001880
solenbergc96df772015-10-21 13:01:53 -07001881 // Save the channel to send_streams_, so that RemoveSendStream() can still
wu@webrtc.org9dba5252013-08-05 20:36:57 +00001882 // delete the channel in case failure happens below.
mallinath@webrtc.org67ee6b92014-02-03 16:57:16 +00001883 webrtc::AudioTransport* audio_transport =
1884 engine()->voe()->base()->audio_transport();
solenberg3a941542015-11-16 07:34:50 -08001885 send_streams_.insert(std::make_pair(ssrc, new WebRtcAudioSendStream(
1886 channel, audio_transport, ssrc, sp.cname, send_rtp_extensions_, call_)));
wu@webrtc.org9dba5252013-08-05 20:36:57 +00001887
solenberg0a617e22015-10-20 15:49:38 -07001888 // Set the current codecs to be used for the new channel. We need to do this
1889 // after adding the channel to send_channels_, because of how max bitrate is
1890 // currently being configured by SetSendCodec().
1891 if (!send_codecs_.empty() && !SetSendCodecs(channel, send_codecs_)) {
1892 RemoveSendStream(ssrc);
wu@webrtc.org9dba5252013-08-05 20:36:57 +00001893 return false;
1894 }
1895
1896 // At this point the channel's local SSRC has been updated. If the channel is
solenberg0a617e22015-10-20 15:49:38 -07001897 // the first send channel make sure that all the receive channels are updated
1898 // with the same SSRC in order to send receiver reports.
solenbergc96df772015-10-21 13:01:53 -07001899 if (send_streams_.size() == 1) {
solenberg0a617e22015-10-20 15:49:38 -07001900 receiver_reports_ssrc_ = ssrc;
solenberg7add0582015-11-20 09:59:34 -08001901 for (const auto& stream : recv_streams_) {
1902 int recv_channel = stream.second->channel();
solenberg0a617e22015-10-20 15:49:38 -07001903 if (engine()->voe()->rtp()->SetLocalSSRC(recv_channel, ssrc) != 0) {
solenberg7add0582015-11-20 09:59:34 -08001904 LOG_RTCERR2(SetLocalSSRC, recv_channel, ssrc);
solenberg1ac56142015-10-13 03:58:19 -07001905 return false;
wu@webrtc.org9dba5252013-08-05 20:36:57 +00001906 }
solenberg0a617e22015-10-20 15:49:38 -07001907 engine()->voe()->base()->AssociateSendChannel(recv_channel, channel);
1908 LOG(LS_INFO) << "VoiceEngine channel #" << recv_channel
1909 << " is associated with channel #" << channel << ".";
wu@webrtc.org9dba5252013-08-05 20:36:57 +00001910 }
1911 }
1912
wu@webrtc.org9dba5252013-08-05 20:36:57 +00001913 return ChangeSend(channel, desired_send_);
1914}
1915
Peter Boström0c4e06b2015-10-07 12:23:21 +02001916bool WebRtcVoiceMediaChannel::RemoveSendStream(uint32_t ssrc) {
solenberg566ef242015-11-06 15:34:49 -08001917 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
solenberg3a941542015-11-16 07:34:50 -08001918 LOG(LS_INFO) << "RemoveSendStream: " << ssrc;
1919
solenbergc96df772015-10-21 13:01:53 -07001920 auto it = send_streams_.find(ssrc);
1921 if (it == send_streams_.end()) {
wu@webrtc.org9dba5252013-08-05 20:36:57 +00001922 LOG(LS_WARNING) << "Try to remove stream with ssrc " << ssrc
1923 << " which doesn't exist.";
1924 return false;
1925 }
1926
mallinath@webrtc.org67ee6b92014-02-03 16:57:16 +00001927 int channel = it->second->channel();
wu@webrtc.org9dba5252013-08-05 20:36:57 +00001928 ChangeSend(channel, SEND_NOTHING);
1929
solenberg7add0582015-11-20 09:59:34 -08001930 // Clean up and delete the send stream+channel.
solenberg0a617e22015-10-20 15:49:38 -07001931 LOG(LS_INFO) << "Removing audio send stream " << ssrc
1932 << " with VoiceEngine channel #" << channel << ".";
solenberg7add0582015-11-20 09:59:34 -08001933 delete it->second;
1934 send_streams_.erase(it);
1935 if (!DeleteVoEChannel(channel)) {
solenberg0a617e22015-10-20 15:49:38 -07001936 return false;
wu@webrtc.org9dba5252013-08-05 20:36:57 +00001937 }
solenbergc96df772015-10-21 13:01:53 -07001938 if (send_streams_.empty()) {
wu@webrtc.org9dba5252013-08-05 20:36:57 +00001939 ChangeSend(SEND_NOTHING);
solenberg0a617e22015-10-20 15:49:38 -07001940 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001941 return true;
1942}
1943
1944bool WebRtcVoiceMediaChannel::AddRecvStream(const StreamParams& sp) {
solenberg566ef242015-11-06 15:34:49 -08001945 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
solenbergd97ec302015-10-07 01:40:33 -07001946 LOG(LS_INFO) << "AddRecvStream: " << sp.ToString();
1947
solenberg0b675462015-10-09 01:37:09 -07001948 if (!ValidateStreamParams(sp)) {
wu@webrtc.org78187522013-10-07 23:32:02 +00001949 return false;
1950 }
1951
solenberg7add0582015-11-20 09:59:34 -08001952 const uint32_t ssrc = sp.first_ssrc();
solenberg0b675462015-10-09 01:37:09 -07001953 if (ssrc == 0) {
1954 LOG(LS_WARNING) << "AddRecvStream with ssrc==0 is not supported.";
1955 return false;
1956 }
1957
solenberg1ac56142015-10-13 03:58:19 -07001958 // Remove the default receive stream if one had been created with this ssrc;
1959 // we'll recreate it then.
1960 if (IsDefaultRecvStream(ssrc)) {
1961 RemoveRecvStream(ssrc);
1962 }
solenberg0b675462015-10-09 01:37:09 -07001963
solenberg7add0582015-11-20 09:59:34 -08001964 if (GetReceiveChannelId(ssrc) != -1) {
henrike@webrtc.org1e09a712013-07-26 19:17:59 +00001965 LOG(LS_ERROR) << "Stream already exists with ssrc " << ssrc;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001966 return false;
1967 }
Fredrik Solenberg4b60c732015-05-07 14:07:48 +02001968
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001969 // Create a new channel for receiving audio data.
solenberg7add0582015-11-20 09:59:34 -08001970 const int channel = CreateVoEChannel();
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001971 if (channel == -1) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001972 return false;
1973 }
Minyue2013aec2015-05-13 14:14:42 +02001974
solenberg1ac56142015-10-13 03:58:19 -07001975 // Turn off all supported codecs.
solenberg26c8c912015-11-27 04:00:25 -08001976 // TODO(solenberg): Remove once "no codecs" is the default state of a stream.
1977 for (webrtc::CodecInst voe_codec : webrtc::acm2::RentACodec::Database()) {
1978 voe_codec.pltype = -1;
1979 if (engine()->voe()->codec()->SetRecPayloadType(channel, voe_codec) == -1) {
1980 LOG_RTCERR2(SetRecPayloadType, channel, ToString(voe_codec));
1981 DeleteVoEChannel(channel);
1982 return false;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001983 }
1984 }
1985
solenberg1ac56142015-10-13 03:58:19 -07001986 // Only enable those configured for this channel.
1987 for (const auto& codec : recv_codecs_) {
1988 webrtc::CodecInst voe_codec;
solenberg26c8c912015-11-27 04:00:25 -08001989 if (WebRtcVoiceEngine::ToCodecInst(codec, &voe_codec)) {
solenberg1ac56142015-10-13 03:58:19 -07001990 voe_codec.pltype = codec.id;
1991 if (engine()->voe()->codec()->SetRecPayloadType(
1992 channel, voe_codec) == -1) {
1993 LOG_RTCERR2(SetRecPayloadType, channel, ToString(voe_codec));
solenberg7add0582015-11-20 09:59:34 -08001994 DeleteVoEChannel(channel);
solenberg1ac56142015-10-13 03:58:19 -07001995 return false;
1996 }
henrike@webrtc.org1e09a712013-07-26 19:17:59 +00001997 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001998 }
solenberg8fb30c32015-10-13 03:06:58 -07001999
solenberg7add0582015-11-20 09:59:34 -08002000 const int send_channel = GetSendChannelId(receiver_reports_ssrc_);
2001 if (send_channel != -1) {
2002 // Associate receive channel with first send channel (so the receive channel
2003 // can obtain RTT from the send channel)
2004 engine()->voe()->base()->AssociateSendChannel(channel, send_channel);
2005 LOG(LS_INFO) << "VoiceEngine channel #" << channel
2006 << " is associated with channel #" << send_channel << ".";
buildbot@webrtc.org150835e2014-05-06 15:54:38 +00002007 }
2008
solenberg7add0582015-11-20 09:59:34 -08002009 recv_streams_.insert(std::make_pair(ssrc, new WebRtcAudioReceiveStream(
2010 channel, ssrc, receiver_reports_ssrc_,
2011 options_.combined_audio_video_bwe.value_or(false), sp.sync_label,
2012 recv_rtp_extensions_, call_)));
2013
2014 SetNack(channel, nack_enabled_);
solenberg1ac56142015-10-13 03:58:19 -07002015 SetPlayout(channel, playout_);
solenberg7add0582015-11-20 09:59:34 -08002016
solenberg1ac56142015-10-13 03:58:19 -07002017 return true;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002018}
2019
Peter Boström0c4e06b2015-10-07 12:23:21 +02002020bool WebRtcVoiceMediaChannel::RemoveRecvStream(uint32_t ssrc) {
solenberg566ef242015-11-06 15:34:49 -08002021 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
solenbergd97ec302015-10-07 01:40:33 -07002022 LOG(LS_INFO) << "RemoveRecvStream: " << ssrc;
2023
solenberg7add0582015-11-20 09:59:34 -08002024 const auto it = recv_streams_.find(ssrc);
2025 if (it == recv_streams_.end()) {
wu@webrtc.org9dba5252013-08-05 20:36:57 +00002026 LOG(LS_WARNING) << "Try to remove stream with ssrc " << ssrc
2027 << " which doesn't exist.";
henrike@webrtc.org1e09a712013-07-26 19:17:59 +00002028 return false;
wu@webrtc.org9dba5252013-08-05 20:36:57 +00002029 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002030
solenberg1ac56142015-10-13 03:58:19 -07002031 // Deregister default channel, if that's the one being destroyed.
2032 if (IsDefaultRecvStream(ssrc)) {
2033 default_recv_ssrc_ = -1;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002034 }
henrike@webrtc.org1e09a712013-07-26 19:17:59 +00002035
solenberg7add0582015-11-20 09:59:34 -08002036 const int channel = it->second->channel();
2037
2038 // Clean up and delete the receive stream+channel.
2039 LOG(LS_INFO) << "Removing audio receive stream " << ssrc
mallinath@webrtc.org67ee6b92014-02-03 16:57:16 +00002040 << " with VoiceEngine channel #" << channel << ".";
Tommif888bb52015-12-12 01:37:01 +01002041 it->second->SetRawAudioSink(nullptr);
solenberg7add0582015-11-20 09:59:34 -08002042 delete it->second;
2043 recv_streams_.erase(it);
2044 return DeleteVoEChannel(channel);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002045}
2046
Peter Boström0c4e06b2015-10-07 12:23:21 +02002047bool WebRtcVoiceMediaChannel::SetLocalRenderer(uint32_t ssrc,
henrike@webrtc.org1e09a712013-07-26 19:17:59 +00002048 AudioRenderer* renderer) {
solenbergc96df772015-10-21 13:01:53 -07002049 auto it = send_streams_.find(ssrc);
2050 if (it == send_streams_.end()) {
wu@webrtc.org9dba5252013-08-05 20:36:57 +00002051 if (renderer) {
2052 // Return an error if trying to set a valid renderer with an invalid ssrc.
2053 LOG(LS_ERROR) << "SetLocalRenderer failed with ssrc "<< ssrc;
2054 return false;
2055 }
2056
2057 // The channel likely has gone away, do nothing.
henrike@webrtc.org1e09a712013-07-26 19:17:59 +00002058 return true;
henrike@webrtc.org1e09a712013-07-26 19:17:59 +00002059 }
2060
solenberg1ac56142015-10-13 03:58:19 -07002061 if (renderer) {
mallinath@webrtc.org67ee6b92014-02-03 16:57:16 +00002062 it->second->Start(renderer);
solenberg1ac56142015-10-13 03:58:19 -07002063 } else {
mallinath@webrtc.org67ee6b92014-02-03 16:57:16 +00002064 it->second->Stop();
solenberg1ac56142015-10-13 03:58:19 -07002065 }
henrike@webrtc.org1e09a712013-07-26 19:17:59 +00002066
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002067 return true;
2068}
2069
2070bool WebRtcVoiceMediaChannel::GetActiveStreams(
2071 AudioInfo::StreamList* actives) {
solenberg566ef242015-11-06 15:34:49 -08002072 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002073 actives->clear();
solenberg7add0582015-11-20 09:59:34 -08002074 for (const auto& ch : recv_streams_) {
Fredrik Solenbergaf9fb212015-08-26 10:45:53 +02002075 int level = GetOutputLevel(ch.second->channel());
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002076 if (level > 0) {
Fredrik Solenbergaf9fb212015-08-26 10:45:53 +02002077 actives->push_back(std::make_pair(ch.first, level));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002078 }
2079 }
2080 return true;
2081}
2082
2083int WebRtcVoiceMediaChannel::GetOutputLevel() {
solenberg566ef242015-11-06 15:34:49 -08002084 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
solenberg1ac56142015-10-13 03:58:19 -07002085 int highest = 0;
solenberg7add0582015-11-20 09:59:34 -08002086 for (const auto& ch : recv_streams_) {
solenberg8fb30c32015-10-13 03:06:58 -07002087 highest = std::max(GetOutputLevel(ch.second->channel()), highest);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002088 }
2089 return highest;
2090}
2091
2092int WebRtcVoiceMediaChannel::GetTimeSinceLastTyping() {
2093 int ret;
2094 if (engine()->voe()->processing()->TimeSinceLastTyping(ret) == -1) {
2095 // In case of error, log the info and continue
2096 LOG_RTCERR0(TimeSinceLastTyping);
2097 ret = -1;
2098 } else {
2099 ret *= 1000; // We return ms, webrtc returns seconds.
2100 }
2101 return ret;
2102}
2103
2104void WebRtcVoiceMediaChannel::SetTypingDetectionParameters(int time_window,
2105 int cost_per_typing, int reporting_threshold, int penalty_decay,
2106 int type_event_delay) {
2107 if (engine()->voe()->processing()->SetTypingDetectionParameters(
2108 time_window, cost_per_typing,
2109 reporting_threshold, penalty_decay, type_event_delay) == -1) {
2110 // In case of error, log the info and continue
2111 LOG_RTCERR5(SetTypingDetectionParameters, time_window,
2112 cost_per_typing, reporting_threshold, penalty_decay,
2113 type_event_delay);
2114 }
2115}
2116
solenberg4bac9c52015-10-09 02:32:53 -07002117bool WebRtcVoiceMediaChannel::SetOutputVolume(uint32_t ssrc, double volume) {
solenberg566ef242015-11-06 15:34:49 -08002118 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
solenberg1ac56142015-10-13 03:58:19 -07002119 if (ssrc == 0) {
2120 default_recv_volume_ = volume;
2121 if (default_recv_ssrc_ == -1) {
2122 return true;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002123 }
solenberg1ac56142015-10-13 03:58:19 -07002124 ssrc = static_cast<uint32_t>(default_recv_ssrc_);
2125 }
2126 int ch_id = GetReceiveChannelId(ssrc);
2127 if (ch_id < 0) {
2128 LOG(LS_WARNING) << "Cannot find channel for ssrc:" << ssrc;
2129 return false;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002130 }
2131
solenberg1ac56142015-10-13 03:58:19 -07002132 if (-1 == engine()->voe()->volume()->SetChannelOutputVolumeScaling(ch_id,
2133 volume)) {
2134 LOG_RTCERR2(SetChannelOutputVolumeScaling, ch_id, volume);
2135 return false;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002136 }
solenberg1ac56142015-10-13 03:58:19 -07002137 LOG(LS_INFO) << "SetOutputVolume to " << volume
2138 << " for channel " << ch_id << " and ssrc " << ssrc;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002139 return true;
2140}
2141
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002142bool WebRtcVoiceMediaChannel::CanInsertDtmf() {
Fredrik Solenbergb5727682015-12-04 15:22:19 +01002143 return dtmf_payload_type_ ? true : false;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002144}
2145
solenberg1d63dd02015-12-02 12:35:09 -08002146bool WebRtcVoiceMediaChannel::InsertDtmf(uint32_t ssrc, int event,
2147 int duration) {
solenberg566ef242015-11-06 15:34:49 -08002148 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
Fredrik Solenbergb5727682015-12-04 15:22:19 +01002149 LOG(LS_INFO) << "WebRtcVoiceMediaChannel::InsertDtmf";
2150 if (!dtmf_payload_type_) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002151 return false;
2152 }
2153
Fredrik Solenbergb5727682015-12-04 15:22:19 +01002154 // Figure out which WebRtcAudioSendStream to send the event on.
2155 auto it = ssrc != 0 ? send_streams_.find(ssrc) : send_streams_.begin();
2156 if (it == send_streams_.end()) {
2157 LOG(LS_WARNING) << "The specified ssrc " << ssrc << " is not in use.";
solenberg1d63dd02015-12-02 12:35:09 -08002158 return false;
2159 }
Fredrik Solenbergb5727682015-12-04 15:22:19 +01002160 if (event < kMinTelephoneEventCode ||
2161 event > kMaxTelephoneEventCode) {
2162 LOG(LS_WARNING) << "DTMF event code " << event << " out of range.";
solenberg1d63dd02015-12-02 12:35:09 -08002163 return false;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002164 }
Fredrik Solenbergb5727682015-12-04 15:22:19 +01002165 if (duration < kMinTelephoneEventDuration ||
2166 duration > kMaxTelephoneEventDuration) {
2167 LOG(LS_WARNING) << "DTMF event duration " << duration << " out of range.";
2168 return false;
2169 }
2170 return it->second->SendTelephoneEvent(*dtmf_payload_type_, event, duration);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002171}
2172
wu@webrtc.orga9890802013-12-13 00:21:03 +00002173void WebRtcVoiceMediaChannel::OnPacketReceived(
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00002174 rtc::Buffer* packet, const rtc::PacketTime& packet_time) {
solenberg566ef242015-11-06 15:34:49 -08002175 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
Fredrik Solenberg4b60c732015-05-07 14:07:48 +02002176
solenberg1ac56142015-10-13 03:58:19 -07002177 uint32_t ssrc = 0;
2178 if (!GetRtpSsrc(packet->data(), packet->size(), &ssrc)) {
2179 return;
2180 }
2181
solenberg7e63ef02015-11-20 00:19:43 -08002182 // If we don't have a default channel, and the SSRC is unknown, create a
2183 // default channel.
2184 if (default_recv_ssrc_ == -1 && GetReceiveChannelId(ssrc) == -1) {
solenberg1ac56142015-10-13 03:58:19 -07002185 StreamParams sp;
2186 sp.ssrcs.push_back(ssrc);
2187 LOG(LS_INFO) << "Creating default receive stream for SSRC=" << ssrc << ".";
2188 if (!AddRecvStream(sp)) {
2189 LOG(LS_WARNING) << "Could not create default receive stream.";
2190 return;
2191 }
2192 default_recv_ssrc_ = ssrc;
2193 SetOutputVolume(default_recv_ssrc_, default_recv_volume_);
2194 }
2195
2196 // Forward packet to Call. If the SSRC is unknown we'll return after this.
Fredrik Solenberg709ed672015-09-15 12:26:33 +02002197 const webrtc::PacketTime webrtc_packet_time(packet_time.timestamp,
2198 packet_time.not_before);
solenberg1ac56142015-10-13 03:58:19 -07002199 webrtc::PacketReceiver::DeliveryStatus delivery_result =
2200 call_->Receiver()->DeliverPacket(webrtc::MediaType::AUDIO,
2201 reinterpret_cast<const uint8_t*>(packet->data()), packet->size(),
2202 webrtc_packet_time);
2203 if (webrtc::PacketReceiver::DELIVERY_OK != delivery_result) {
solenberg7e63ef02015-11-20 00:19:43 -08002204 // If the SSRC is unknown here, route it to the default channel, if we have
2205 // one. See: https://bugs.chromium.org/p/webrtc/issues/detail?id=5208
2206 if (default_recv_ssrc_ == -1) {
2207 return;
2208 } else {
2209 ssrc = default_recv_ssrc_;
2210 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002211 }
2212
solenberg1ac56142015-10-13 03:58:19 -07002213 // Find the channel to send this packet to. It must exist since webrtc::Call
2214 // was able to demux the packet.
2215 int channel = GetReceiveChannelId(ssrc);
2216 RTC_DCHECK(channel != -1);
2217
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002218 // Pass it off to the decoder.
henrike@webrtc.org28654cb2013-07-22 21:07:49 +00002219 engine()->voe()->network()->ReceivedRTPPacket(
solenberg1ac56142015-10-13 03:58:19 -07002220 channel, packet->data(), packet->size(), webrtc_packet_time);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002221}
2222
wu@webrtc.orga9890802013-12-13 00:21:03 +00002223void WebRtcVoiceMediaChannel::OnRtcpReceived(
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00002224 rtc::Buffer* packet, const rtc::PacketTime& packet_time) {
solenberg566ef242015-11-06 15:34:49 -08002225 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
Fredrik Solenberg4b60c732015-05-07 14:07:48 +02002226
Fredrik Solenberg709ed672015-09-15 12:26:33 +02002227 // Forward packet to Call as well.
2228 const webrtc::PacketTime webrtc_packet_time(packet_time.timestamp,
2229 packet_time.not_before);
2230 call_->Receiver()->DeliverPacket(webrtc::MediaType::AUDIO,
2231 reinterpret_cast<const uint8_t*>(packet->data()), packet->size(),
2232 webrtc_packet_time);
Fredrik Solenberg4b60c732015-05-07 14:07:48 +02002233
wu@webrtc.org9dba5252013-08-05 20:36:57 +00002234 // Sending channels need all RTCP packets with feedback information.
2235 // Even sender reports can contain attached report blocks.
2236 // Receiving channels need sender reports in order to create
2237 // correct receiver reports.
2238 int type = 0;
kwiberg@webrtc.orgeebcab52015-03-24 09:19:06 +00002239 if (!GetRtcpType(packet->data(), packet->size(), &type)) {
wu@webrtc.org9dba5252013-08-05 20:36:57 +00002240 LOG(LS_WARNING) << "Failed to parse type from received RTCP packet";
2241 return;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002242 }
2243
solenberg0b675462015-10-09 01:37:09 -07002244 // If it is a sender report, find the receive channel that is listening.
wu@webrtc.org9dba5252013-08-05 20:36:57 +00002245 if (type == kRtcpTypeSR) {
solenberg0b675462015-10-09 01:37:09 -07002246 uint32_t ssrc = 0;
2247 if (!GetRtcpSsrc(packet->data(), packet->size(), &ssrc)) {
2248 return;
2249 }
2250 int recv_channel_id = GetReceiveChannelId(ssrc);
2251 if (recv_channel_id != -1) {
wu@webrtc.org9dba5252013-08-05 20:36:57 +00002252 engine()->voe()->network()->ReceivedRTCPPacket(
solenberg0b675462015-10-09 01:37:09 -07002253 recv_channel_id, packet->data(), packet->size());
wu@webrtc.org9dba5252013-08-05 20:36:57 +00002254 }
2255 }
2256
2257 // SR may continue RR and any RR entry may correspond to any one of the send
2258 // channels. So all RTCP packets must be forwarded all send channels. VoE
2259 // will filter out RR internally.
solenbergc96df772015-10-21 13:01:53 -07002260 for (const auto& ch : send_streams_) {
wu@webrtc.org9dba5252013-08-05 20:36:57 +00002261 engine()->voe()->network()->ReceivedRTCPPacket(
Fredrik Solenbergaf9fb212015-08-26 10:45:53 +02002262 ch.second->channel(), packet->data(), packet->size());
wu@webrtc.org9dba5252013-08-05 20:36:57 +00002263 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002264}
2265
Peter Boström0c4e06b2015-10-07 12:23:21 +02002266bool WebRtcVoiceMediaChannel::MuteStream(uint32_t ssrc, bool muted) {
solenberg566ef242015-11-06 15:34:49 -08002267 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
solenberg0a617e22015-10-20 15:49:38 -07002268 int channel = GetSendChannelId(ssrc);
wu@webrtc.org9dba5252013-08-05 20:36:57 +00002269 if (channel == -1) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002270 LOG(LS_WARNING) << "The specified ssrc " << ssrc << " is not in use.";
2271 return false;
2272 }
wu@webrtc.org9dba5252013-08-05 20:36:57 +00002273 if (engine()->voe()->volume()->SetInputMute(channel, muted) == -1) {
2274 LOG_RTCERR2(SetInputMute, channel, muted);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002275 return false;
2276 }
buildbot@webrtc.org6b21b712014-07-31 15:08:53 +00002277 // We set the AGC to mute state only when all the channels are muted.
2278 // This implementation is not ideal, instead we should signal the AGC when
2279 // the mic channel is muted/unmuted. We can't do it today because there
2280 // is no good way to know which stream is mapping to the mic channel.
2281 bool all_muted = muted;
solenbergc96df772015-10-21 13:01:53 -07002282 for (const auto& ch : send_streams_) {
Fredrik Solenbergaf9fb212015-08-26 10:45:53 +02002283 if (!all_muted) {
2284 break;
2285 }
2286 if (engine()->voe()->volume()->GetInputMute(ch.second->channel(),
buildbot@webrtc.org6b21b712014-07-31 15:08:53 +00002287 all_muted)) {
Fredrik Solenbergaf9fb212015-08-26 10:45:53 +02002288 LOG_RTCERR1(GetInputMute, ch.second->channel());
buildbot@webrtc.org6b21b712014-07-31 15:08:53 +00002289 return false;
2290 }
2291 }
2292
2293 webrtc::AudioProcessing* ap = engine()->voe()->base()->audio_processing();
solenberg0a617e22015-10-20 15:49:38 -07002294 if (ap) {
buildbot@webrtc.org6b21b712014-07-31 15:08:53 +00002295 ap->set_output_will_be_muted(all_muted);
solenberg0a617e22015-10-20 15:49:38 -07002296 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002297 return true;
2298}
2299
minyue@webrtc.org26236952014-10-29 02:27:08 +00002300// TODO(minyue): SetMaxSendBandwidth() is subject to be renamed to
2301// SetMaxSendBitrate() in future.
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00002302bool WebRtcVoiceMediaChannel::SetMaxSendBandwidth(int bps) {
minyue@webrtc.org26236952014-10-29 02:27:08 +00002303 LOG(LS_INFO) << "WebRtcVoiceMediaChannel::SetMaxSendBandwidth.";
minyue@webrtc.org26236952014-10-29 02:27:08 +00002304 return SetSendBitrateInternal(bps);
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00002305}
2306
minyue@webrtc.org26236952014-10-29 02:27:08 +00002307bool WebRtcVoiceMediaChannel::SetSendBitrateInternal(int bps) {
2308 LOG(LS_INFO) << "WebRtcVoiceMediaChannel::SetSendBitrateInternal.";
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00002309
minyue@webrtc.org26236952014-10-29 02:27:08 +00002310 send_bitrate_setting_ = true;
2311 send_bitrate_bps_ = bps;
wu@webrtc.org1d1ffc92013-10-16 18:12:02 +00002312
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002313 if (!send_codec_) {
wu@webrtc.org1d1ffc92013-10-16 18:12:02 +00002314 LOG(LS_INFO) << "The send codec has not been set up yet. "
minyue@webrtc.org26236952014-10-29 02:27:08 +00002315 << "The send bitrate setting will be applied later.";
wu@webrtc.org1d1ffc92013-10-16 18:12:02 +00002316 return true;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002317 }
2318
minyue@webrtc.org26236952014-10-29 02:27:08 +00002319 // Bitrate is auto by default.
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00002320 // TODO(bemasc): Fix this so that if SetMaxSendBandwidth(50) is followed by
2321 // SetMaxSendBandwith(0), the second call removes the previous limit.
2322 if (bps <= 0)
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002323 return true;
2324
2325 webrtc::CodecInst codec = *send_codec_;
solenberg26c8c912015-11-27 04:00:25 -08002326 bool is_multi_rate = WebRtcVoiceCodecs::IsCodecMultiRate(codec);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002327
2328 if (is_multi_rate) {
2329 // If codec is multi-rate then just set the bitrate.
2330 codec.rate = bps;
solenbergc96df772015-10-21 13:01:53 -07002331 for (const auto& ch : send_streams_) {
solenberg0a617e22015-10-20 15:49:38 -07002332 if (!SetSendCodec(ch.second->channel(), codec)) {
2333 LOG(LS_INFO) << "Failed to set codec " << codec.plname
2334 << " to bitrate " << bps << " bps.";
2335 return false;
2336 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002337 }
2338 return true;
2339 } else {
2340 // If codec is not multi-rate and |bps| is less than the fixed bitrate
2341 // then fail. If codec is not multi-rate and |bps| exceeds or equal the
2342 // fixed bitrate then ignore.
2343 if (bps < codec.rate) {
2344 LOG(LS_INFO) << "Failed to set codec " << codec.plname
2345 << " to bitrate " << bps << " bps"
2346 << ", requires at least " << codec.rate << " bps.";
2347 return false;
2348 }
2349 return true;
2350 }
2351}
2352
2353bool WebRtcVoiceMediaChannel::GetStats(VoiceMediaInfo* info) {
solenberg566ef242015-11-06 15:34:49 -08002354 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
solenberg85a04962015-10-27 03:35:21 -07002355 RTC_DCHECK(info);
solenbergd97ec302015-10-07 01:40:33 -07002356
solenberg85a04962015-10-27 03:35:21 -07002357 // Get SSRC and stats for each sender.
2358 RTC_DCHECK(info->senders.size() == 0);
2359 for (const auto& stream : send_streams_) {
2360 webrtc::AudioSendStream::Stats stats = stream.second->GetStats();
wu@webrtc.org9dba5252013-08-05 20:36:57 +00002361 VoiceSenderInfo sinfo;
solenberg85a04962015-10-27 03:35:21 -07002362 sinfo.add_ssrc(stats.local_ssrc);
2363 sinfo.bytes_sent = stats.bytes_sent;
2364 sinfo.packets_sent = stats.packets_sent;
2365 sinfo.packets_lost = stats.packets_lost;
2366 sinfo.fraction_lost = stats.fraction_lost;
2367 sinfo.codec_name = stats.codec_name;
2368 sinfo.ext_seqnum = stats.ext_seqnum;
2369 sinfo.jitter_ms = stats.jitter_ms;
2370 sinfo.rtt_ms = stats.rtt_ms;
2371 sinfo.audio_level = stats.audio_level;
2372 sinfo.aec_quality_min = stats.aec_quality_min;
2373 sinfo.echo_delay_median_ms = stats.echo_delay_median_ms;
2374 sinfo.echo_delay_std_ms = stats.echo_delay_std_ms;
2375 sinfo.echo_return_loss = stats.echo_return_loss;
2376 sinfo.echo_return_loss_enhancement = stats.echo_return_loss_enhancement;
solenberg566ef242015-11-06 15:34:49 -08002377 sinfo.typing_noise_detected =
2378 (send_ == SEND_NOTHING ? false : stats.typing_noise_detected);
wu@webrtc.org9dba5252013-08-05 20:36:57 +00002379 info->senders.push_back(sinfo);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002380 }
2381
solenberg85a04962015-10-27 03:35:21 -07002382 // Get SSRC and stats for each receiver.
2383 RTC_DCHECK(info->receivers.size() == 0);
solenberg7add0582015-11-20 09:59:34 -08002384 for (const auto& stream : recv_streams_) {
Fredrik Solenberg4f4ec0a2015-10-22 10:49:27 +02002385 webrtc::AudioReceiveStream::Stats stats = stream.second->GetStats();
2386 VoiceReceiverInfo rinfo;
2387 rinfo.add_ssrc(stats.remote_ssrc);
2388 rinfo.bytes_rcvd = stats.bytes_rcvd;
2389 rinfo.packets_rcvd = stats.packets_rcvd;
2390 rinfo.packets_lost = stats.packets_lost;
2391 rinfo.fraction_lost = stats.fraction_lost;
2392 rinfo.codec_name = stats.codec_name;
2393 rinfo.ext_seqnum = stats.ext_seqnum;
2394 rinfo.jitter_ms = stats.jitter_ms;
2395 rinfo.jitter_buffer_ms = stats.jitter_buffer_ms;
2396 rinfo.jitter_buffer_preferred_ms = stats.jitter_buffer_preferred_ms;
2397 rinfo.delay_estimate_ms = stats.delay_estimate_ms;
2398 rinfo.audio_level = stats.audio_level;
2399 rinfo.expand_rate = stats.expand_rate;
2400 rinfo.speech_expand_rate = stats.speech_expand_rate;
2401 rinfo.secondary_decoded_rate = stats.secondary_decoded_rate;
2402 rinfo.accelerate_rate = stats.accelerate_rate;
2403 rinfo.preemptive_expand_rate = stats.preemptive_expand_rate;
2404 rinfo.decoding_calls_to_silence_generator =
2405 stats.decoding_calls_to_silence_generator;
2406 rinfo.decoding_calls_to_neteq = stats.decoding_calls_to_neteq;
2407 rinfo.decoding_normal = stats.decoding_normal;
2408 rinfo.decoding_plc = stats.decoding_plc;
2409 rinfo.decoding_cng = stats.decoding_cng;
2410 rinfo.decoding_plc_cng = stats.decoding_plc_cng;
2411 rinfo.capture_start_ntp_time_ms = stats.capture_start_ntp_time_ms;
2412 info->receivers.push_back(rinfo);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002413 }
2414
2415 return true;
2416}
2417
Tommif888bb52015-12-12 01:37:01 +01002418void WebRtcVoiceMediaChannel::SetRawAudioSink(
2419 uint32_t ssrc,
2420 rtc::scoped_ptr<webrtc::AudioSinkInterface> sink) {
2421 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
2422 LOG(LS_VERBOSE) << "WebRtcVoiceMediaChannel::SetRawAudioSink";
2423 const auto it = recv_streams_.find(ssrc);
2424 if (it == recv_streams_.end()) {
2425 LOG(LS_WARNING) << "SetRawAudioSink: no recv stream" << ssrc;
2426 return;
2427 }
2428 it->second->SetRawAudioSink(std::move(sink));
2429}
2430
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002431int WebRtcVoiceMediaChannel::GetOutputLevel(int channel) {
solenbergd97ec302015-10-07 01:40:33 -07002432 unsigned int ulevel = 0;
2433 int ret = engine()->voe()->volume()->GetSpeechOutputLevel(channel, ulevel);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002434 return (ret == 0) ? static_cast<int>(ulevel) : -1;
2435}
2436
Peter Boström0c4e06b2015-10-07 12:23:21 +02002437int WebRtcVoiceMediaChannel::GetReceiveChannelId(uint32_t ssrc) const {
solenberg566ef242015-11-06 15:34:49 -08002438 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
solenberg7add0582015-11-20 09:59:34 -08002439 const auto it = recv_streams_.find(ssrc);
2440 if (it != recv_streams_.end()) {
mallinath@webrtc.org67ee6b92014-02-03 16:57:16 +00002441 return it->second->channel();
solenberg8fb30c32015-10-13 03:06:58 -07002442 }
solenberg1ac56142015-10-13 03:58:19 -07002443 return -1;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002444}
2445
Peter Boström0c4e06b2015-10-07 12:23:21 +02002446int WebRtcVoiceMediaChannel::GetSendChannelId(uint32_t ssrc) const {
solenberg566ef242015-11-06 15:34:49 -08002447 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
solenbergc96df772015-10-21 13:01:53 -07002448 const auto it = send_streams_.find(ssrc);
2449 if (it != send_streams_.end()) {
mallinath@webrtc.org67ee6b92014-02-03 16:57:16 +00002450 return it->second->channel();
solenberg8fb30c32015-10-13 03:06:58 -07002451 }
wu@webrtc.org9dba5252013-08-05 20:36:57 +00002452 return -1;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002453}
2454
2455bool WebRtcVoiceMediaChannel::GetRedSendCodec(const AudioCodec& red_codec,
2456 const std::vector<AudioCodec>& all_codecs, webrtc::CodecInst* send_codec) {
2457 // Get the RED encodings from the parameter with no name. This may
2458 // change based on what is discussed on the Jingle list.
2459 // The encoding parameter is of the form "a/b"; we only support where
2460 // a == b. Verify this and parse out the value into red_pt.
2461 // If the parameter value is absent (as it will be until we wire up the
2462 // signaling of this message), use the second codec specified (i.e. the
2463 // one after "red") as the encoding parameter.
2464 int red_pt = -1;
2465 std::string red_params;
2466 CodecParameterMap::const_iterator it = red_codec.params.find("");
2467 if (it != red_codec.params.end()) {
2468 red_params = it->second;
2469 std::vector<std::string> red_pts;
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00002470 if (rtc::split(red_params, '/', &red_pts) != 2 ||
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002471 red_pts[0] != red_pts[1] ||
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00002472 !rtc::FromString(red_pts[0], &red_pt)) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002473 LOG(LS_WARNING) << "RED params " << red_params << " not supported.";
2474 return false;
2475 }
2476 } else if (red_codec.params.empty()) {
2477 LOG(LS_WARNING) << "RED params not present, using defaults";
2478 if (all_codecs.size() > 1) {
2479 red_pt = all_codecs[1].id;
2480 }
2481 }
2482
2483 // Try to find red_pt in |codecs|.
Fredrik Solenbergaf9fb212015-08-26 10:45:53 +02002484 for (const AudioCodec& codec : all_codecs) {
2485 if (codec.id == red_pt) {
2486 // If we find the right codec, that will be the codec we pass to
2487 // SetSendCodec, with the desired payload type.
solenberg26c8c912015-11-27 04:00:25 -08002488 if (WebRtcVoiceEngine::ToCodecInst(codec, send_codec)) {
Fredrik Solenbergaf9fb212015-08-26 10:45:53 +02002489 return true;
2490 } else {
2491 break;
2492 }
2493 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002494 }
Fredrik Solenbergaf9fb212015-08-26 10:45:53 +02002495 LOG(LS_WARNING) << "RED params " << red_params << " are invalid.";
2496 return false;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002497}
2498
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002499bool WebRtcVoiceMediaChannel::SetPlayout(int channel, bool playout) {
2500 if (playout) {
2501 LOG(LS_INFO) << "Starting playout for channel #" << channel;
2502 if (engine()->voe()->base()->StartPlayout(channel) == -1) {
2503 LOG_RTCERR1(StartPlayout, channel);
2504 return false;
2505 }
2506 } else {
2507 LOG(LS_INFO) << "Stopping playout for channel #" << channel;
2508 engine()->voe()->base()->StopPlayout(channel);
2509 }
2510 return true;
2511}
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002512} // namespace cricket
2513
2514#endif // HAVE_WEBRTC_VOICE