blob: a496316b42149637757846ec3e20f89b32915f02 [file] [log] [blame]
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001/*
kjellander1afca732016-02-07 20:46:45 -08002 * Copyright (c) 2004 The WebRTC project authors. All Rights Reserved.
henrike@webrtc.org28e20752013-07-10 00:45:36 +00003 *
kjellander1afca732016-02-07 20:46:45 -08004 * Use of this source code is governed by a BSD-style license
5 * that can be found in the LICENSE file in the root of the source
6 * tree. An additional intellectual property rights grant can be found
7 * in the file PATENTS. All contributing project authors may
8 * be found in the AUTHORS file in the root of the source tree.
henrike@webrtc.org28e20752013-07-10 00:45:36 +00009 */
10
11#ifdef HAVE_CONFIG_H
12#include <config.h>
13#endif
14
15#ifdef HAVE_WEBRTC_VOICE
16
kjellander@webrtc.org5ad12972016-02-12 06:39:40 +010017#include "webrtc/media/engine/webrtcvoiceengine.h"
henrike@webrtc.org28e20752013-07-10 00:45:36 +000018
19#include <algorithm>
20#include <cstdio>
21#include <string>
22#include <vector>
23
kjellander@webrtc.org7ffeab52016-02-26 22:46:09 +010024#include "webrtc/audio_sink.h"
tfarina5237aaf2015-11-10 23:44:30 -080025#include "webrtc/base/arraysize.h"
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +000026#include "webrtc/base/base64.h"
27#include "webrtc/base/byteorder.h"
28#include "webrtc/base/common.h"
29#include "webrtc/base/helpers.h"
30#include "webrtc/base/logging.h"
31#include "webrtc/base/stringencode.h"
32#include "webrtc/base/stringutils.h"
Peter Boströmca8b4042016-03-08 14:24:13 -080033#include "webrtc/base/trace_event.h"
ivoc112a3d82015-10-16 02:22:18 -070034#include "webrtc/call/rtc_event_log.h"
mallinath@webrtc.orga27be8e2013-09-27 23:04:10 +000035#include "webrtc/common.h"
kjellandera96e2d72016-02-04 23:52:28 -080036#include "webrtc/media/base/audioframe.h"
Taylor Brandstetter1a018dc2016-03-08 12:37:39 -080037#include "webrtc/media/base/audiosource.h"
kjellanderf4752772016-03-02 05:42:30 -080038#include "webrtc/media/base/mediaconstants.h"
kjellandera96e2d72016-02-04 23:52:28 -080039#include "webrtc/media/base/streamparams.h"
kjellander@webrtc.org5ad12972016-02-12 06:39:40 +010040#include "webrtc/media/engine/webrtcmediaengine.h"
41#include "webrtc/media/engine/webrtcvoe.h"
solenberg26c8c912015-11-27 04:00:25 -080042#include "webrtc/modules/audio_coding/acm2/rent_a_codec.h"
henrike@webrtc.org28e20752013-07-10 00:45:36 +000043#include "webrtc/modules/audio_processing/include/audio_processing.h"
Henrik Kjellander98f53512015-10-28 18:17:40 +010044#include "webrtc/system_wrappers/include/field_trial.h"
solenbergbd138382015-11-20 16:08:07 -080045#include "webrtc/system_wrappers/include/trace.h"
henrike@webrtc.org28e20752013-07-10 00:45:36 +000046
henrike@webrtc.org28e20752013-07-10 00:45:36 +000047namespace cricket {
solenbergd97ec302015-10-07 01:40:33 -070048namespace {
henrike@webrtc.org28e20752013-07-10 00:45:36 +000049
solenbergbd138382015-11-20 16:08:07 -080050const int kDefaultTraceFilter = webrtc::kTraceNone | webrtc::kTraceTerseInfo |
51 webrtc::kTraceWarning | webrtc::kTraceError |
52 webrtc::kTraceCritical;
53const int kElevatedTraceFilter = kDefaultTraceFilter | webrtc::kTraceStateInfo |
54 webrtc::kTraceInfo;
55
henrike@webrtc.org28e20752013-07-10 00:45:36 +000056// On Windows Vista and newer, Microsoft introduced the concept of "Default
57// Communications Device". This means that there are two types of default
58// devices (old Wave Audio style default and Default Communications Device).
59//
60// On Windows systems which only support Wave Audio style default, uses either
61// -1 or 0 to select the default device.
henrike@webrtc.org28e20752013-07-10 00:45:36 +000062#ifdef WIN32
solenbergd97ec302015-10-07 01:40:33 -070063const int kDefaultAudioDeviceId = -1;
henrike@webrtc.org28e20752013-07-10 00:45:36 +000064#else
solenbergd97ec302015-10-07 01:40:33 -070065const int kDefaultAudioDeviceId = 0;
henrike@webrtc.org28e20752013-07-10 00:45:36 +000066#endif
67
henrike@webrtc.org28e20752013-07-10 00:45:36 +000068// Parameter used for NACK.
69// This value is equivalent to 5 seconds of audio data at 20 ms per packet.
solenbergd97ec302015-10-07 01:40:33 -070070const int kNackMaxPackets = 250;
minyue@webrtc.org2dc6f312014-10-31 05:33:10 +000071
72// Codec parameters for Opus.
henrike@webrtc.org1e09a712013-07-26 19:17:59 +000073// draft-spittka-payload-rtp-opus-03
minyue@webrtc.org2dc6f312014-10-31 05:33:10 +000074
75// Recommended bitrates:
76// 8-12 kb/s for NB speech,
77// 16-20 kb/s for WB speech,
78// 28-40 kb/s for FB speech,
79// 48-64 kb/s for FB mono music, and
80// 64-128 kb/s for FB stereo music.
81// The current implementation applies the following values to mono signals,
82// and multiplies them by 2 for stereo.
solenbergd97ec302015-10-07 01:40:33 -070083const int kOpusBitrateNb = 12000;
84const int kOpusBitrateWb = 20000;
85const int kOpusBitrateFb = 32000;
minyue@webrtc.org2dc6f312014-10-31 05:33:10 +000086
henrike@webrtc.org1e09a712013-07-26 19:17:59 +000087// Opus bitrate should be in the range between 6000 and 510000.
solenbergd97ec302015-10-07 01:40:33 -070088const int kOpusMinBitrate = 6000;
89const int kOpusMaxBitrate = 510000;
buildbot@webrtc.org5d639b32014-09-10 07:57:12 +000090
wu@webrtc.orgde305012013-10-31 15:40:38 +000091// Default audio dscp value.
92// See http://tools.ietf.org/html/rfc2474 for details.
93// See also http://tools.ietf.org/html/draft-jennings-rtcweb-qos-00
solenbergd97ec302015-10-07 01:40:33 -070094const rtc::DiffServCodePoint kAudioDscpValue = rtc::DSCP_EF;
henrike@webrtc.org1e09a712013-07-26 19:17:59 +000095
Fredrik Solenbergb5727682015-12-04 15:22:19 +010096// Constants from voice_engine_defines.h.
97const int kMinTelephoneEventCode = 0; // RFC4733 (Section 2.3.1)
98const int kMaxTelephoneEventCode = 255;
99const int kMinTelephoneEventDuration = 100;
100const int kMaxTelephoneEventDuration = 60000; // Actual limit is 2^16
101
deadbeef884f5852016-01-15 09:20:04 -0800102class ProxySink : public webrtc::AudioSinkInterface {
103 public:
104 ProxySink(AudioSinkInterface* sink) : sink_(sink) { RTC_DCHECK(sink); }
105
106 void OnData(const Data& audio) override { sink_->OnData(audio); }
107
108 private:
109 webrtc::AudioSinkInterface* sink_;
110};
111
solenberg0b675462015-10-09 01:37:09 -0700112bool ValidateStreamParams(const StreamParams& sp) {
113 if (sp.ssrcs.empty()) {
114 LOG(LS_ERROR) << "No SSRCs in stream parameters: " << sp.ToString();
115 return false;
116 }
117 if (sp.ssrcs.size() > 1) {
118 LOG(LS_ERROR) << "Multiple SSRCs in stream parameters: " << sp.ToString();
119 return false;
120 }
121 return true;
122}
123
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000124// Dumps an AudioCodec in RFC 2327-ish format.
solenbergd97ec302015-10-07 01:40:33 -0700125std::string ToString(const AudioCodec& codec) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000126 std::stringstream ss;
127 ss << codec.name << "/" << codec.clockrate << "/" << codec.channels
128 << " (" << codec.id << ")";
129 return ss.str();
130}
Minyue Li7100dcd2015-03-27 05:05:59 +0100131
solenbergd97ec302015-10-07 01:40:33 -0700132std::string ToString(const webrtc::CodecInst& codec) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000133 std::stringstream ss;
134 ss << codec.plname << "/" << codec.plfreq << "/" << codec.channels
135 << " (" << codec.pltype << ")";
136 return ss.str();
137}
138
solenbergd97ec302015-10-07 01:40:33 -0700139bool IsCodec(const AudioCodec& codec, const char* ref_name) {
Minyue Li7100dcd2015-03-27 05:05:59 +0100140 return (_stricmp(codec.name.c_str(), ref_name) == 0);
141}
142
solenbergd97ec302015-10-07 01:40:33 -0700143bool IsCodec(const webrtc::CodecInst& codec, const char* ref_name) {
Minyue Li7100dcd2015-03-27 05:05:59 +0100144 return (_stricmp(codec.plname, ref_name) == 0);
145}
146
solenbergd97ec302015-10-07 01:40:33 -0700147bool FindCodec(const std::vector<AudioCodec>& codecs,
solenberg26c8c912015-11-27 04:00:25 -0800148 const AudioCodec& codec,
149 AudioCodec* found_codec) {
Fredrik Solenbergaf9fb212015-08-26 10:45:53 +0200150 for (const AudioCodec& c : codecs) {
151 if (c.Matches(codec)) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000152 if (found_codec != NULL) {
Fredrik Solenbergaf9fb212015-08-26 10:45:53 +0200153 *found_codec = c;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000154 }
155 return true;
156 }
157 }
158 return false;
159}
wu@webrtc.org1d1ffc92013-10-16 18:12:02 +0000160
solenberg0b675462015-10-09 01:37:09 -0700161bool VerifyUniquePayloadTypes(const std::vector<AudioCodec>& codecs) {
162 if (codecs.empty()) {
163 return true;
164 }
165 std::vector<int> payload_types;
166 for (const AudioCodec& codec : codecs) {
167 payload_types.push_back(codec.id);
168 }
169 std::sort(payload_types.begin(), payload_types.end());
170 auto it = std::unique(payload_types.begin(), payload_types.end());
171 return it == payload_types.end();
172}
173
Minyue Li7100dcd2015-03-27 05:05:59 +0100174// Return true if codec.params[feature] == "1", false otherwise.
solenberg26c8c912015-11-27 04:00:25 -0800175bool IsCodecFeatureEnabled(const AudioCodec& codec, const char* feature) {
Minyue Li7100dcd2015-03-27 05:05:59 +0100176 int value;
177 return codec.GetParam(feature, &value) && value == 1;
178}
179
180// Use params[kCodecParamMaxAverageBitrate] if it is defined, use codec.bitrate
181// otherwise. If the value (either from params or codec.bitrate) <=0, use the
182// default configuration. If the value is beyond feasible bit rate of Opus,
183// clamp it. Returns the Opus bit rate for operation.
solenbergd97ec302015-10-07 01:40:33 -0700184int GetOpusBitrate(const AudioCodec& codec, int max_playback_rate) {
Minyue Li7100dcd2015-03-27 05:05:59 +0100185 int bitrate = 0;
186 bool use_param = true;
187 if (!codec.GetParam(kCodecParamMaxAverageBitrate, &bitrate)) {
188 bitrate = codec.bitrate;
189 use_param = false;
190 }
191 if (bitrate <= 0) {
192 if (max_playback_rate <= 8000) {
193 bitrate = kOpusBitrateNb;
194 } else if (max_playback_rate <= 16000) {
195 bitrate = kOpusBitrateWb;
196 } else {
197 bitrate = kOpusBitrateFb;
198 }
199
200 if (IsCodecFeatureEnabled(codec, kCodecParamStereo)) {
201 bitrate *= 2;
202 }
203 } else if (bitrate < kOpusMinBitrate || bitrate > kOpusMaxBitrate) {
204 bitrate = (bitrate < kOpusMinBitrate) ? kOpusMinBitrate : kOpusMaxBitrate;
205 std::string rate_source =
206 use_param ? "Codec parameter \"maxaveragebitrate\"" :
207 "Supplied Opus bitrate";
208 LOG(LS_WARNING) << rate_source
209 << " is invalid and is replaced by: "
210 << bitrate;
211 }
212 return bitrate;
213}
214
215// Returns kOpusDefaultPlaybackRate if params[kCodecParamMaxPlaybackRate] is not
216// defined. Returns the value of params[kCodecParamMaxPlaybackRate] otherwise.
solenbergd97ec302015-10-07 01:40:33 -0700217int GetOpusMaxPlaybackRate(const AudioCodec& codec) {
Minyue Li7100dcd2015-03-27 05:05:59 +0100218 int value;
219 if (codec.GetParam(kCodecParamMaxPlaybackRate, &value)) {
220 return value;
221 }
222 return kOpusDefaultMaxPlaybackRate;
223}
224
solenbergd97ec302015-10-07 01:40:33 -0700225void GetOpusConfig(const AudioCodec& codec, webrtc::CodecInst* voe_codec,
Minyue Li7100dcd2015-03-27 05:05:59 +0100226 bool* enable_codec_fec, int* max_playback_rate,
227 bool* enable_codec_dtx) {
228 *enable_codec_fec = IsCodecFeatureEnabled(codec, kCodecParamUseInbandFec);
229 *enable_codec_dtx = IsCodecFeatureEnabled(codec, kCodecParamUseDtx);
230 *max_playback_rate = GetOpusMaxPlaybackRate(codec);
231
232 // If OPUS, change what we send according to the "stereo" codec
233 // parameter, and not the "channels" parameter. We set
234 // voe_codec.channels to 2 if "stereo=1" and 1 otherwise. If
235 // the bitrate is not specified, i.e. is <= zero, we set it to the
236 // appropriate default value for mono or stereo Opus.
237
238 voe_codec->channels = IsCodecFeatureEnabled(codec, kCodecParamStereo) ? 2 : 1;
239 voe_codec->rate = GetOpusBitrate(codec, *max_playback_rate);
240}
241
solenberg566ef242015-11-06 15:34:49 -0800242webrtc::AudioState::Config MakeAudioStateConfig(VoEWrapper* voe_wrapper) {
243 webrtc::AudioState::Config config;
244 config.voice_engine = voe_wrapper->engine();
245 return config;
246}
247
solenberg26c8c912015-11-27 04:00:25 -0800248class WebRtcVoiceCodecs final {
249 public:
250 // TODO(solenberg): Do this filtering once off-line, add a simple AudioCodec
251 // list and add a test which verifies VoE supports the listed codecs.
252 static std::vector<AudioCodec> SupportedCodecs() {
253 LOG(LS_INFO) << "WebRtc VoiceEngine codecs:";
254 std::vector<AudioCodec> result;
255 for (webrtc::CodecInst voe_codec : webrtc::acm2::RentACodec::Database()) {
256 // Change the sample rate of G722 to 8000 to match SDP.
257 MaybeFixupG722(&voe_codec, 8000);
buildbot@webrtc.org13d67762014-05-02 17:33:29 +0000258 // Skip uncompressed formats.
Minyue Li7100dcd2015-03-27 05:05:59 +0100259 if (IsCodec(voe_codec, kL16CodecName)) {
buildbot@webrtc.org13d67762014-05-02 17:33:29 +0000260 continue;
261 }
262
263 const CodecPref* pref = NULL;
tfarina5237aaf2015-11-10 23:44:30 -0800264 for (size_t j = 0; j < arraysize(kCodecPrefs); ++j) {
Minyue Li7100dcd2015-03-27 05:05:59 +0100265 if (IsCodec(voe_codec, kCodecPrefs[j].name) &&
buildbot@webrtc.org13d67762014-05-02 17:33:29 +0000266 kCodecPrefs[j].clockrate == voe_codec.plfreq &&
267 kCodecPrefs[j].channels == voe_codec.channels) {
268 pref = &kCodecPrefs[j];
269 break;
270 }
271 }
272
273 if (pref) {
274 // Use the payload type that we've configured in our pref table;
275 // use the offset in our pref table to determine the sort order.
tfarina5237aaf2015-11-10 23:44:30 -0800276 AudioCodec codec(
277 pref->payload_type, voe_codec.plname, voe_codec.plfreq,
278 voe_codec.rate, voe_codec.channels,
279 static_cast<int>(arraysize(kCodecPrefs)) - (pref - kCodecPrefs));
buildbot@webrtc.org13d67762014-05-02 17:33:29 +0000280 LOG(LS_INFO) << ToString(codec);
Minyue Li7100dcd2015-03-27 05:05:59 +0100281 if (IsCodec(codec, kIsacCodecName)) {
minyue@webrtc.org26236952014-10-29 02:27:08 +0000282 // Indicate auto-bitrate in signaling.
buildbot@webrtc.org13d67762014-05-02 17:33:29 +0000283 codec.bitrate = 0;
284 }
Minyue Li7100dcd2015-03-27 05:05:59 +0100285 if (IsCodec(codec, kOpusCodecName)) {
buildbot@webrtc.org13d67762014-05-02 17:33:29 +0000286 // Only add fmtp parameters that differ from the spec.
287 if (kPreferredMinPTime != kOpusDefaultMinPTime) {
288 codec.params[kCodecParamMinPTime] =
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000289 rtc::ToString(kPreferredMinPTime);
buildbot@webrtc.org13d67762014-05-02 17:33:29 +0000290 }
291 if (kPreferredMaxPTime != kOpusDefaultMaxPTime) {
292 codec.params[kCodecParamMaxPTime] =
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000293 rtc::ToString(kPreferredMaxPTime);
buildbot@webrtc.org13d67762014-05-02 17:33:29 +0000294 }
pkasting@chromium.orgd3245462015-02-23 21:28:22 +0000295 codec.SetParam(kCodecParamUseInbandFec, 1);
stefanba4c0e42016-02-04 04:12:24 -0800296 codec.AddFeedbackParam(
297 FeedbackParam(kRtcpFbParamTransportCc, kParamValueEmpty));
minyue@webrtc.org4ef22d12014-11-17 09:26:39 +0000298
299 // TODO(hellner): Add ptime, sprop-stereo, and stereo
buildbot@webrtc.org13d67762014-05-02 17:33:29 +0000300 // when they can be set to values other than the default.
301 }
solenberg26c8c912015-11-27 04:00:25 -0800302 result.push_back(codec);
buildbot@webrtc.org13d67762014-05-02 17:33:29 +0000303 } else {
304 LOG(LS_WARNING) << "Unexpected codec: " << ToString(voe_codec);
305 }
306 }
solenberg26c8c912015-11-27 04:00:25 -0800307 // Make sure they are in local preference order.
308 std::sort(result.begin(), result.end(), &AudioCodec::Preferable);
309 return result;
buildbot@webrtc.org13d67762014-05-02 17:33:29 +0000310 }
buildbot@webrtc.org13d67762014-05-02 17:33:29 +0000311
solenberg26c8c912015-11-27 04:00:25 -0800312 static bool ToCodecInst(const AudioCodec& in,
313 webrtc::CodecInst* out) {
314 for (webrtc::CodecInst voe_codec : webrtc::acm2::RentACodec::Database()) {
315 // Change the sample rate of G722 to 8000 to match SDP.
316 MaybeFixupG722(&voe_codec, 8000);
317 AudioCodec codec(voe_codec.pltype, voe_codec.plname, voe_codec.plfreq,
318 voe_codec.rate, voe_codec.channels, 0);
319 bool multi_rate = IsCodecMultiRate(voe_codec);
320 // Allow arbitrary rates for ISAC to be specified.
321 if (multi_rate) {
322 // Set codec.bitrate to 0 so the check for codec.Matches() passes.
323 codec.bitrate = 0;
324 }
325 if (codec.Matches(in)) {
326 if (out) {
327 // Fixup the payload type.
328 voe_codec.pltype = in.id;
329
330 // Set bitrate if specified.
331 if (multi_rate && in.bitrate != 0) {
332 voe_codec.rate = in.bitrate;
333 }
334
335 // Reset G722 sample rate to 16000 to match WebRTC.
336 MaybeFixupG722(&voe_codec, 16000);
337
338 // Apply codec-specific settings.
339 if (IsCodec(codec, kIsacCodecName)) {
340 // If ISAC and an explicit bitrate is not specified,
341 // enable auto bitrate adjustment.
342 voe_codec.rate = (in.bitrate > 0) ? in.bitrate : -1;
343 }
344 *out = voe_codec;
345 }
346 return true;
347 }
348 }
henrik.lundin@webrtc.org8038d422014-11-11 08:38:24 +0000349 return false;
henrik.lundin@webrtc.orgf85dbce2014-11-07 12:25:00 +0000350 }
solenberg26c8c912015-11-27 04:00:25 -0800351
352 static bool IsCodecMultiRate(const webrtc::CodecInst& codec) {
353 for (size_t i = 0; i < arraysize(kCodecPrefs); ++i) {
354 if (IsCodec(codec, kCodecPrefs[i].name) &&
355 kCodecPrefs[i].clockrate == codec.plfreq) {
356 return kCodecPrefs[i].is_multi_rate;
357 }
358 }
359 return false;
360 }
361
362 // If the AudioCodec param kCodecParamPTime is set, then we will set it to
363 // codec pacsize if it's valid, or we will pick the next smallest value we
364 // support.
365 // TODO(Brave): Query supported packet sizes from ACM when the API is ready.
366 static bool SetPTimeAsPacketSize(webrtc::CodecInst* codec, int ptime_ms) {
367 for (const CodecPref& codec_pref : kCodecPrefs) {
368 if ((IsCodec(*codec, codec_pref.name) &&
369 codec_pref.clockrate == codec->plfreq) ||
370 IsCodec(*codec, kG722CodecName)) {
371 int packet_size_ms = SelectPacketSize(codec_pref, ptime_ms);
372 if (packet_size_ms) {
373 // Convert unit from milli-seconds to samples.
374 codec->pacsize = (codec->plfreq / 1000) * packet_size_ms;
375 return true;
376 }
377 }
378 }
379 return false;
380 }
381
stefanba4c0e42016-02-04 04:12:24 -0800382 static const AudioCodec* GetPreferredCodec(
383 const std::vector<AudioCodec>& codecs,
solenberg72e29d22016-03-08 06:35:16 -0800384 webrtc::CodecInst* out,
stefanba4c0e42016-02-04 04:12:24 -0800385 int* red_payload_type) {
solenberg72e29d22016-03-08 06:35:16 -0800386 RTC_DCHECK(out);
stefanba4c0e42016-02-04 04:12:24 -0800387 RTC_DCHECK(red_payload_type);
388 // Select the preferred send codec (the first non-telephone-event/CN codec).
389 for (const AudioCodec& codec : codecs) {
390 *red_payload_type = -1;
391 if (IsCodec(codec, kDtmfCodecName) || IsCodec(codec, kCnCodecName)) {
392 // Skip telephone-event/CN codec, which will be handled later.
393 continue;
394 }
395
396 // We'll use the first codec in the list to actually send audio data.
397 // Be sure to use the payload type requested by the remote side.
398 // "red", for RED audio, is a special case where the actual codec to be
399 // used is specified in params.
400 const AudioCodec* found_codec = &codec;
401 if (IsCodec(*found_codec, kRedCodecName)) {
402 // Parse out the RED parameters. If we fail, just ignore RED;
403 // we don't support all possible params/usage scenarios.
404 *red_payload_type = codec.id;
405 found_codec = GetRedSendCodec(*found_codec, codecs);
406 if (!found_codec) {
407 continue;
408 }
409 }
410 // Ignore codecs we don't know about. The negotiation step should prevent
411 // this, but double-check to be sure.
solenberg72e29d22016-03-08 06:35:16 -0800412 webrtc::CodecInst voe_codec = {0};
413 if (!ToCodecInst(*found_codec, &voe_codec)) {
stefanba4c0e42016-02-04 04:12:24 -0800414 LOG(LS_WARNING) << "Unknown codec " << ToString(*found_codec);
415 continue;
416 }
solenberg72e29d22016-03-08 06:35:16 -0800417 *out = voe_codec;
stefanba4c0e42016-02-04 04:12:24 -0800418 return found_codec;
419 }
420 return nullptr;
421 }
422
solenberg26c8c912015-11-27 04:00:25 -0800423 private:
424 static const int kMaxNumPacketSize = 6;
425 struct CodecPref {
426 const char* name;
427 int clockrate;
Peter Kasting69558702016-01-12 16:26:35 -0800428 size_t channels;
solenberg26c8c912015-11-27 04:00:25 -0800429 int payload_type;
430 bool is_multi_rate;
431 int packet_sizes_ms[kMaxNumPacketSize];
432 };
433 // Note: keep the supported packet sizes in ascending order.
434 static const CodecPref kCodecPrefs[12];
435
436 static int SelectPacketSize(const CodecPref& codec_pref, int ptime_ms) {
437 int selected_packet_size_ms = codec_pref.packet_sizes_ms[0];
438 for (int packet_size_ms : codec_pref.packet_sizes_ms) {
439 if (packet_size_ms && packet_size_ms <= ptime_ms) {
440 selected_packet_size_ms = packet_size_ms;
441 }
442 }
443 return selected_packet_size_ms;
444 }
445
446 // Changes RTP timestamp rate of G722. This is due to the "bug" in the RFC
447 // which says that G722 should be advertised as 8 kHz although it is a 16 kHz
448 // codec.
449 static void MaybeFixupG722(webrtc::CodecInst* voe_codec, int new_plfreq) {
450 if (IsCodec(*voe_codec, kG722CodecName)) {
451 // If the ASSERT triggers, the codec definition in WebRTC VoiceEngine
452 // has changed, and this special case is no longer needed.
453 RTC_DCHECK(voe_codec->plfreq != new_plfreq);
454 voe_codec->plfreq = new_plfreq;
455 }
456 }
stefanba4c0e42016-02-04 04:12:24 -0800457
458 static const AudioCodec* GetRedSendCodec(
459 const AudioCodec& red_codec,
460 const std::vector<AudioCodec>& all_codecs) {
461 // Get the RED encodings from the parameter with no name. This may
462 // change based on what is discussed on the Jingle list.
463 // The encoding parameter is of the form "a/b"; we only support where
464 // a == b. Verify this and parse out the value into red_pt.
465 // If the parameter value is absent (as it will be until we wire up the
466 // signaling of this message), use the second codec specified (i.e. the
467 // one after "red") as the encoding parameter.
468 int red_pt = -1;
469 std::string red_params;
470 CodecParameterMap::const_iterator it = red_codec.params.find("");
471 if (it != red_codec.params.end()) {
472 red_params = it->second;
473 std::vector<std::string> red_pts;
474 if (rtc::split(red_params, '/', &red_pts) != 2 ||
475 red_pts[0] != red_pts[1] || !rtc::FromString(red_pts[0], &red_pt)) {
476 LOG(LS_WARNING) << "RED params " << red_params << " not supported.";
477 return nullptr;
478 }
479 } else if (red_codec.params.empty()) {
480 LOG(LS_WARNING) << "RED params not present, using defaults";
481 if (all_codecs.size() > 1) {
482 red_pt = all_codecs[1].id;
483 }
484 }
485
486 // Try to find red_pt in |codecs|.
487 for (const AudioCodec& codec : all_codecs) {
488 if (codec.id == red_pt) {
489 return &codec;
490 }
491 }
492 LOG(LS_WARNING) << "RED params " << red_params << " are invalid.";
493 return nullptr;
494 }
solenberg26c8c912015-11-27 04:00:25 -0800495};
496
497const WebRtcVoiceCodecs::CodecPref WebRtcVoiceCodecs::kCodecPrefs[12] = {
498 { kOpusCodecName, 48000, 2, 111, true, { 10, 20, 40, 60 } },
499 { kIsacCodecName, 16000, 1, 103, true, { 30, 60 } },
500 { kIsacCodecName, 32000, 1, 104, true, { 30 } },
501 // G722 should be advertised as 8000 Hz because of the RFC "bug".
502 { kG722CodecName, 8000, 1, 9, false, { 10, 20, 30, 40, 50, 60 } },
503 { kIlbcCodecName, 8000, 1, 102, false, { 20, 30, 40, 60 } },
504 { kPcmuCodecName, 8000, 1, 0, false, { 10, 20, 30, 40, 50, 60 } },
505 { kPcmaCodecName, 8000, 1, 8, false, { 10, 20, 30, 40, 50, 60 } },
506 { kCnCodecName, 32000, 1, 106, false, { } },
507 { kCnCodecName, 16000, 1, 105, false, { } },
508 { kCnCodecName, 8000, 1, 13, false, { } },
509 { kRedCodecName, 8000, 1, 127, false, { } },
510 { kDtmfCodecName, 8000, 1, 126, false, { } },
511};
512} // namespace {
513
514bool WebRtcVoiceEngine::ToCodecInst(const AudioCodec& in,
515 webrtc::CodecInst* out) {
516 return WebRtcVoiceCodecs::ToCodecInst(in, out);
517}
518
519WebRtcVoiceEngine::WebRtcVoiceEngine()
520 : voe_wrapper_(new VoEWrapper()),
521 audio_state_(webrtc::AudioState::Create(MakeAudioStateConfig(voe()))) {
522 Construct();
523}
524
525WebRtcVoiceEngine::WebRtcVoiceEngine(VoEWrapper* voe_wrapper)
526 : voe_wrapper_(voe_wrapper) {
527 Construct();
528}
529
530void WebRtcVoiceEngine::Construct() {
531 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
532 LOG(LS_VERBOSE) << "WebRtcVoiceEngine::WebRtcVoiceEngine";
533
534 signal_thread_checker_.DetachFromThread();
535 std::memset(&default_agc_config_, 0, sizeof(default_agc_config_));
solenberg246b8172015-12-08 09:50:23 -0800536 voe_config_.Set<webrtc::VoicePacing>(new webrtc::VoicePacing(true));
solenberg26c8c912015-11-27 04:00:25 -0800537
538 webrtc::Trace::set_level_filter(kDefaultTraceFilter);
539 webrtc::Trace::SetTraceCallback(this);
540
541 // Load our audio codec list.
542 codecs_ = WebRtcVoiceCodecs::SupportedCodecs();
henrik.lundin@webrtc.orgf85dbce2014-11-07 12:25:00 +0000543}
544
buildbot@webrtc.org13d67762014-05-02 17:33:29 +0000545WebRtcVoiceEngine::~WebRtcVoiceEngine() {
solenberg566ef242015-11-06 15:34:49 -0800546 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
buildbot@webrtc.org13d67762014-05-02 17:33:29 +0000547 LOG(LS_VERBOSE) << "WebRtcVoiceEngine::~WebRtcVoiceEngine";
buildbot@webrtc.org13d67762014-05-02 17:33:29 +0000548 if (adm_) {
549 voe_wrapper_.reset();
550 adm_->Release();
551 adm_ = NULL;
552 }
solenbergbd138382015-11-20 16:08:07 -0800553 webrtc::Trace::SetTraceCallback(nullptr);
buildbot@webrtc.org13d67762014-05-02 17:33:29 +0000554}
555
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000556bool WebRtcVoiceEngine::Init(rtc::Thread* worker_thread) {
solenberg566ef242015-11-06 15:34:49 -0800557 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
henrikg91d6ede2015-09-17 00:24:34 -0700558 RTC_DCHECK(worker_thread == rtc::Thread::Current());
buildbot@webrtc.org13d67762014-05-02 17:33:29 +0000559 LOG(LS_INFO) << "WebRtcVoiceEngine::Init";
560 bool res = InitInternal();
561 if (res) {
562 LOG(LS_INFO) << "WebRtcVoiceEngine::Init Done!";
563 } else {
564 LOG(LS_ERROR) << "WebRtcVoiceEngine::Init failed";
565 Terminate();
566 }
567 return res;
568}
569
570bool WebRtcVoiceEngine::InitInternal() {
solenberg566ef242015-11-06 15:34:49 -0800571 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
solenberg72e29d22016-03-08 06:35:16 -0800572 // Temporarily turn logging level up for the Init call.
solenbergbd138382015-11-20 16:08:07 -0800573 webrtc::Trace::set_level_filter(kElevatedTraceFilter);
solenberg2515af22015-12-02 06:19:36 -0800574 LOG(LS_INFO) << webrtc::VoiceEngine::GetVersionString();
buildbot@webrtc.org13d67762014-05-02 17:33:29 +0000575 if (voe_wrapper_->base()->Init(adm_) == -1) {
576 LOG_RTCERR0_EX(Init, voe_wrapper_->error());
buildbot@webrtc.org13d67762014-05-02 17:33:29 +0000577 return false;
578 }
solenbergbd138382015-11-20 16:08:07 -0800579 webrtc::Trace::set_level_filter(kDefaultTraceFilter);
buildbot@webrtc.org13d67762014-05-02 17:33:29 +0000580
buildbot@webrtc.org13d67762014-05-02 17:33:29 +0000581 // Save the default AGC configuration settings. This must happen before
solenberg246b8172015-12-08 09:50:23 -0800582 // calling ApplyOptions or the default will be overwritten.
buildbot@webrtc.org13d67762014-05-02 17:33:29 +0000583 if (voe_wrapper_->processing()->GetAgcConfig(default_agc_config_) == -1) {
584 LOG_RTCERR0(GetAgcConfig);
585 return false;
586 }
587
solenberg0f7d2932016-01-15 01:40:39 -0800588 // Set default engine options.
589 {
590 AudioOptions options;
591 options.echo_cancellation = rtc::Optional<bool>(true);
592 options.auto_gain_control = rtc::Optional<bool>(true);
593 options.noise_suppression = rtc::Optional<bool>(true);
594 options.highpass_filter = rtc::Optional<bool>(true);
595 options.stereo_swapping = rtc::Optional<bool>(false);
596 options.audio_jitter_buffer_max_packets = rtc::Optional<int>(50);
597 options.audio_jitter_buffer_fast_accelerate = rtc::Optional<bool>(false);
598 options.typing_detection = rtc::Optional<bool>(true);
599 options.adjust_agc_delta = rtc::Optional<int>(0);
600 options.experimental_agc = rtc::Optional<bool>(false);
601 options.extended_filter_aec = rtc::Optional<bool>(false);
602 options.delay_agnostic_aec = rtc::Optional<bool>(false);
603 options.experimental_ns = rtc::Optional<bool>(false);
solenberg0f7d2932016-01-15 01:40:39 -0800604 if (!ApplyOptions(options)) {
605 return false;
606 }
607 }
608
solenberg72e29d22016-03-08 06:35:16 -0800609 // Print our codec list again for the call diagnostic log.
buildbot@webrtc.org13d67762014-05-02 17:33:29 +0000610 LOG(LS_INFO) << "WebRtc VoiceEngine codecs:";
Fredrik Solenbergaf9fb212015-08-26 10:45:53 +0200611 for (const AudioCodec& codec : codecs_) {
612 LOG(LS_INFO) << ToString(codec);
buildbot@webrtc.org13d67762014-05-02 17:33:29 +0000613 }
614
solenberg246b8172015-12-08 09:50:23 -0800615 SetDefaultDevices();
616
buildbot@webrtc.org13d67762014-05-02 17:33:29 +0000617 initialized_ = true;
618 return true;
619}
620
buildbot@webrtc.org13d67762014-05-02 17:33:29 +0000621void WebRtcVoiceEngine::Terminate() {
solenberg566ef242015-11-06 15:34:49 -0800622 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
buildbot@webrtc.org13d67762014-05-02 17:33:29 +0000623 LOG(LS_INFO) << "WebRtcVoiceEngine::Terminate";
624 initialized_ = false;
625
626 StopAecDump();
627
buildbot@webrtc.org13d67762014-05-02 17:33:29 +0000628 voe_wrapper_->base()->Terminate();
buildbot@webrtc.org13d67762014-05-02 17:33:29 +0000629}
630
solenberg566ef242015-11-06 15:34:49 -0800631rtc::scoped_refptr<webrtc::AudioState>
632 WebRtcVoiceEngine::GetAudioState() const {
633 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
634 return audio_state_;
635}
636
nisse51542be2016-02-12 02:27:06 -0800637VoiceMediaChannel* WebRtcVoiceEngine::CreateChannel(
638 webrtc::Call* call,
639 const MediaConfig& config,
Jelena Marusicc28a8962015-05-29 15:05:44 +0200640 const AudioOptions& options) {
solenberg566ef242015-11-06 15:34:49 -0800641 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
nisse51542be2016-02-12 02:27:06 -0800642 return new WebRtcVoiceMediaChannel(this, config, options, call);
buildbot@webrtc.org13d67762014-05-02 17:33:29 +0000643}
644
buildbot@webrtc.org13d67762014-05-02 17:33:29 +0000645bool WebRtcVoiceEngine::ApplyOptions(const AudioOptions& options_in) {
solenberg566ef242015-11-06 15:34:49 -0800646 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
henrikac14f5ff2015-09-23 14:08:33 +0200647 LOG(LS_INFO) << "ApplyOptions: " << options_in.ToString();
solenberg0f7d2932016-01-15 01:40:39 -0800648 AudioOptions options = options_in; // The options are modified below.
solenberg246b8172015-12-08 09:50:23 -0800649
buildbot@webrtc.org13d67762014-05-02 17:33:29 +0000650 // kEcConference is AEC with high suppression.
651 webrtc::EcModes ec_mode = webrtc::kEcConference;
652 webrtc::AecmModes aecm_mode = webrtc::kAecmSpeakerphone;
653 webrtc::AgcModes agc_mode = webrtc::kAgcAdaptiveAnalog;
654 webrtc::NsModes ns_mode = webrtc::kNsHighSuppression;
kwiberg102c6a62015-10-30 02:47:38 -0700655 if (options.aecm_generate_comfort_noise) {
buildbot@webrtc.org13d67762014-05-02 17:33:29 +0000656 LOG(LS_VERBOSE) << "Comfort noise explicitly set to "
kwiberg102c6a62015-10-30 02:47:38 -0700657 << *options.aecm_generate_comfort_noise
658 << " (default is false).";
buildbot@webrtc.org13d67762014-05-02 17:33:29 +0000659 }
660
kjellanderfcfc8042016-01-14 11:01:09 -0800661#if defined(WEBRTC_IOS)
buildbot@webrtc.org13d67762014-05-02 17:33:29 +0000662 // On iOS, VPIO provides built-in EC and AGC.
Karl Wibergbe579832015-11-10 22:34:18 +0100663 options.echo_cancellation = rtc::Optional<bool>(false);
664 options.auto_gain_control = rtc::Optional<bool>(false);
henrika86d907c2015-09-07 16:09:50 +0200665 LOG(LS_INFO) << "Always disable AEC and AGC on iOS. Use built-in instead.";
buildbot@webrtc.org13d67762014-05-02 17:33:29 +0000666#elif defined(ANDROID)
667 ec_mode = webrtc::kEcAecm;
668#endif
669
kjellanderfcfc8042016-01-14 11:01:09 -0800670#if defined(WEBRTC_IOS) || defined(ANDROID)
buildbot@webrtc.org13d67762014-05-02 17:33:29 +0000671 // Set the AGC mode for iOS as well despite disabling it above, to avoid
672 // unsupported configuration errors from webrtc.
673 agc_mode = webrtc::kAgcFixedDigital;
Karl Wibergbe579832015-11-10 22:34:18 +0100674 options.typing_detection = rtc::Optional<bool>(false);
675 options.experimental_agc = rtc::Optional<bool>(false);
676 options.extended_filter_aec = rtc::Optional<bool>(false);
677 options.experimental_ns = rtc::Optional<bool>(false);
buildbot@webrtc.org13d67762014-05-02 17:33:29 +0000678#endif
679
Bjorn Volckerbf395c12015-03-25 22:45:56 +0100680 // Delay Agnostic AEC automatically turns on EC if not set except on iOS
681 // where the feature is not supported.
682 bool use_delay_agnostic_aec = false;
kjellanderfcfc8042016-01-14 11:01:09 -0800683#if !defined(WEBRTC_IOS)
kwiberg102c6a62015-10-30 02:47:38 -0700684 if (options.delay_agnostic_aec) {
685 use_delay_agnostic_aec = *options.delay_agnostic_aec;
Bjorn Volckerbf395c12015-03-25 22:45:56 +0100686 if (use_delay_agnostic_aec) {
Karl Wibergbe579832015-11-10 22:34:18 +0100687 options.echo_cancellation = rtc::Optional<bool>(true);
688 options.extended_filter_aec = rtc::Optional<bool>(true);
Bjorn Volckerbf395c12015-03-25 22:45:56 +0100689 ec_mode = webrtc::kEcConference;
690 }
691 }
692#endif
693
buildbot@webrtc.org13d67762014-05-02 17:33:29 +0000694 webrtc::VoEAudioProcessing* voep = voe_wrapper_->processing();
695
kwiberg102c6a62015-10-30 02:47:38 -0700696 if (options.echo_cancellation) {
henrika@webrtc.orga954c072014-12-09 16:22:09 +0000697 // Check if platform supports built-in EC. Currently only supported on
698 // Android and in combination with Java based audio layer.
699 // TODO(henrika): investigate possibility to support built-in EC also
700 // in combination with Open SL ES audio.
701 const bool built_in_aec = voe_wrapper_->hw()->BuiltInAECIsAvailable();
Bjorn Volcker73f72102015-06-03 14:50:15 +0200702 if (built_in_aec) {
Bjorn Volckerccfc9392015-05-07 07:43:17 +0200703 // Built-in EC exists on this device and use_delay_agnostic_aec is not
704 // overriding it. Enable/Disable it according to the echo_cancellation
705 // audio option.
Bjorn Volcker73f72102015-06-03 14:50:15 +0200706 const bool enable_built_in_aec =
kwiberg102c6a62015-10-30 02:47:38 -0700707 *options.echo_cancellation && !use_delay_agnostic_aec;
Bjorn Volcker73f72102015-06-03 14:50:15 +0200708 if (voe_wrapper_->hw()->EnableBuiltInAEC(enable_built_in_aec) == 0 &&
709 enable_built_in_aec) {
Bjorn Volckerbf395c12015-03-25 22:45:56 +0100710 // Disable internal software EC if built-in EC is enabled,
henrika@webrtc.orga954c072014-12-09 16:22:09 +0000711 // i.e., replace the software EC with the built-in EC.
Karl Wibergbe579832015-11-10 22:34:18 +0100712 options.echo_cancellation = rtc::Optional<bool>(false);
henrika@webrtc.orga954c072014-12-09 16:22:09 +0000713 LOG(LS_INFO) << "Disabling EC since built-in EC will be used instead";
714 }
715 }
kwiberg102c6a62015-10-30 02:47:38 -0700716 if (voep->SetEcStatus(*options.echo_cancellation, ec_mode) == -1) {
717 LOG_RTCERR2(SetEcStatus, *options.echo_cancellation, ec_mode);
buildbot@webrtc.org13d67762014-05-02 17:33:29 +0000718 return false;
719 } else {
kwiberg102c6a62015-10-30 02:47:38 -0700720 LOG(LS_INFO) << "Echo control set to " << *options.echo_cancellation
henrika86d907c2015-09-07 16:09:50 +0200721 << " with mode " << ec_mode;
buildbot@webrtc.org13d67762014-05-02 17:33:29 +0000722 }
723#if !defined(ANDROID)
724 // TODO(ajm): Remove the error return on Android from webrtc.
kwiberg102c6a62015-10-30 02:47:38 -0700725 if (voep->SetEcMetricsStatus(*options.echo_cancellation) == -1) {
726 LOG_RTCERR1(SetEcMetricsStatus, *options.echo_cancellation);
buildbot@webrtc.org13d67762014-05-02 17:33:29 +0000727 return false;
728 }
729#endif
730 if (ec_mode == webrtc::kEcAecm) {
kwiberg102c6a62015-10-30 02:47:38 -0700731 bool cn = options.aecm_generate_comfort_noise.value_or(false);
732 if (voep->SetAecmMode(aecm_mode, cn) != 0) {
733 LOG_RTCERR2(SetAecmMode, aecm_mode, cn);
buildbot@webrtc.org13d67762014-05-02 17:33:29 +0000734 return false;
735 }
736 }
737 }
738
kwiberg102c6a62015-10-30 02:47:38 -0700739 if (options.auto_gain_control) {
henrikac14f5ff2015-09-23 14:08:33 +0200740 const bool built_in_agc = voe_wrapper_->hw()->BuiltInAGCIsAvailable();
741 if (built_in_agc) {
kwiberg102c6a62015-10-30 02:47:38 -0700742 if (voe_wrapper_->hw()->EnableBuiltInAGC(*options.auto_gain_control) ==
743 0 &&
744 *options.auto_gain_control) {
henrikac14f5ff2015-09-23 14:08:33 +0200745 // Disable internal software AGC if built-in AGC is enabled,
746 // i.e., replace the software AGC with the built-in AGC.
Karl Wibergbe579832015-11-10 22:34:18 +0100747 options.auto_gain_control = rtc::Optional<bool>(false);
henrikac14f5ff2015-09-23 14:08:33 +0200748 LOG(LS_INFO) << "Disabling AGC since built-in AGC will be used instead";
749 }
750 }
kwiberg102c6a62015-10-30 02:47:38 -0700751 if (voep->SetAgcStatus(*options.auto_gain_control, agc_mode) == -1) {
752 LOG_RTCERR2(SetAgcStatus, *options.auto_gain_control, agc_mode);
buildbot@webrtc.org13d67762014-05-02 17:33:29 +0000753 return false;
754 } else {
kwiberg102c6a62015-10-30 02:47:38 -0700755 LOG(LS_INFO) << "Auto gain set to " << *options.auto_gain_control
756 << " with mode " << agc_mode;
buildbot@webrtc.org13d67762014-05-02 17:33:29 +0000757 }
758 }
759
kwiberg102c6a62015-10-30 02:47:38 -0700760 if (options.tx_agc_target_dbov || options.tx_agc_digital_compression_gain ||
761 options.tx_agc_limiter) {
buildbot@webrtc.org13d67762014-05-02 17:33:29 +0000762 // Override default_agc_config_. Generally, an unset option means "leave
763 // the VoE bits alone" in this function, so we want whatever is set to be
764 // stored as the new "default". If we didn't, then setting e.g.
765 // tx_agc_target_dbov would reset digital compression gain and limiter
766 // settings.
767 // Also, if we don't update default_agc_config_, then adjust_agc_delta
768 // would be an offset from the original values, and not whatever was set
769 // explicitly.
kwiberg102c6a62015-10-30 02:47:38 -0700770 default_agc_config_.targetLeveldBOv = options.tx_agc_target_dbov.value_or(
771 default_agc_config_.targetLeveldBOv);
buildbot@webrtc.org13d67762014-05-02 17:33:29 +0000772 default_agc_config_.digitalCompressionGaindB =
kwiberg102c6a62015-10-30 02:47:38 -0700773 options.tx_agc_digital_compression_gain.value_or(
buildbot@webrtc.org13d67762014-05-02 17:33:29 +0000774 default_agc_config_.digitalCompressionGaindB);
775 default_agc_config_.limiterEnable =
kwiberg102c6a62015-10-30 02:47:38 -0700776 options.tx_agc_limiter.value_or(default_agc_config_.limiterEnable);
buildbot@webrtc.org13d67762014-05-02 17:33:29 +0000777 if (voe_wrapper_->processing()->SetAgcConfig(default_agc_config_) == -1) {
778 LOG_RTCERR3(SetAgcConfig,
779 default_agc_config_.targetLeveldBOv,
780 default_agc_config_.digitalCompressionGaindB,
781 default_agc_config_.limiterEnable);
782 return false;
783 }
784 }
785
kwiberg102c6a62015-10-30 02:47:38 -0700786 if (options.noise_suppression) {
henrikac14f5ff2015-09-23 14:08:33 +0200787 const bool built_in_ns = voe_wrapper_->hw()->BuiltInNSIsAvailable();
788 if (built_in_ns) {
kwiberg102c6a62015-10-30 02:47:38 -0700789 if (voe_wrapper_->hw()->EnableBuiltInNS(*options.noise_suppression) ==
790 0 &&
791 *options.noise_suppression) {
henrikac14f5ff2015-09-23 14:08:33 +0200792 // Disable internal software NS if built-in NS is enabled,
793 // i.e., replace the software NS with the built-in NS.
Karl Wibergbe579832015-11-10 22:34:18 +0100794 options.noise_suppression = rtc::Optional<bool>(false);
henrikac14f5ff2015-09-23 14:08:33 +0200795 LOG(LS_INFO) << "Disabling NS since built-in NS will be used instead";
796 }
797 }
kwiberg102c6a62015-10-30 02:47:38 -0700798 if (voep->SetNsStatus(*options.noise_suppression, ns_mode) == -1) {
799 LOG_RTCERR2(SetNsStatus, *options.noise_suppression, ns_mode);
buildbot@webrtc.org13d67762014-05-02 17:33:29 +0000800 return false;
801 } else {
kwiberg102c6a62015-10-30 02:47:38 -0700802 LOG(LS_INFO) << "Noise suppression set to " << *options.noise_suppression
henrikac14f5ff2015-09-23 14:08:33 +0200803 << " with mode " << ns_mode;
buildbot@webrtc.org13d67762014-05-02 17:33:29 +0000804 }
805 }
806
kwiberg102c6a62015-10-30 02:47:38 -0700807 if (options.highpass_filter) {
808 LOG(LS_INFO) << "High pass filter enabled? " << *options.highpass_filter;
809 if (voep->EnableHighPassFilter(*options.highpass_filter) == -1) {
810 LOG_RTCERR1(SetHighpassFilterStatus, *options.highpass_filter);
buildbot@webrtc.org13d67762014-05-02 17:33:29 +0000811 return false;
812 }
813 }
814
kwiberg102c6a62015-10-30 02:47:38 -0700815 if (options.stereo_swapping) {
816 LOG(LS_INFO) << "Stereo swapping enabled? " << *options.stereo_swapping;
817 voep->EnableStereoChannelSwapping(*options.stereo_swapping);
818 if (voep->IsStereoChannelSwappingEnabled() != *options.stereo_swapping) {
819 LOG_RTCERR1(EnableStereoChannelSwapping, *options.stereo_swapping);
buildbot@webrtc.org13d67762014-05-02 17:33:29 +0000820 return false;
821 }
822 }
823
kwiberg102c6a62015-10-30 02:47:38 -0700824 if (options.audio_jitter_buffer_max_packets) {
825 LOG(LS_INFO) << "NetEq capacity is "
826 << *options.audio_jitter_buffer_max_packets;
Henrik Lundin64dad832015-05-11 12:44:23 +0200827 voe_config_.Set<webrtc::NetEqCapacityConfig>(
kwiberg102c6a62015-10-30 02:47:38 -0700828 new webrtc::NetEqCapacityConfig(
829 *options.audio_jitter_buffer_max_packets));
Henrik Lundin64dad832015-05-11 12:44:23 +0200830 }
831
kwiberg102c6a62015-10-30 02:47:38 -0700832 if (options.audio_jitter_buffer_fast_accelerate) {
833 LOG(LS_INFO) << "NetEq fast mode? "
834 << *options.audio_jitter_buffer_fast_accelerate;
Henrik Lundin5263b3c2015-06-01 10:29:41 +0200835 voe_config_.Set<webrtc::NetEqFastAccelerate>(
kwiberg102c6a62015-10-30 02:47:38 -0700836 new webrtc::NetEqFastAccelerate(
837 *options.audio_jitter_buffer_fast_accelerate));
Henrik Lundin5263b3c2015-06-01 10:29:41 +0200838 }
839
kwiberg102c6a62015-10-30 02:47:38 -0700840 if (options.typing_detection) {
841 LOG(LS_INFO) << "Typing detection is enabled? "
842 << *options.typing_detection;
843 if (voep->SetTypingDetectionStatus(*options.typing_detection) == -1) {
buildbot@webrtc.org13d67762014-05-02 17:33:29 +0000844 // In case of error, log the info and continue
kwiberg102c6a62015-10-30 02:47:38 -0700845 LOG_RTCERR1(SetTypingDetectionStatus, *options.typing_detection);
buildbot@webrtc.org13d67762014-05-02 17:33:29 +0000846 }
847 }
848
kwiberg102c6a62015-10-30 02:47:38 -0700849 if (options.adjust_agc_delta) {
850 LOG(LS_INFO) << "Adjust agc delta is " << *options.adjust_agc_delta;
851 if (!AdjustAgcLevel(*options.adjust_agc_delta)) {
buildbot@webrtc.org13d67762014-05-02 17:33:29 +0000852 return false;
853 }
854 }
855
buildbot@webrtc.org1f8a2372014-08-28 10:52:44 +0000856 webrtc::Config config;
857
kwiberg102c6a62015-10-30 02:47:38 -0700858 if (options.delay_agnostic_aec)
859 delay_agnostic_aec_ = options.delay_agnostic_aec;
860 if (delay_agnostic_aec_) {
861 LOG(LS_INFO) << "Delay agnostic aec is enabled? " << *delay_agnostic_aec_;
henrik.lundin0f133b92015-07-02 00:17:55 -0700862 config.Set<webrtc::DelayAgnostic>(
kwiberg102c6a62015-10-30 02:47:38 -0700863 new webrtc::DelayAgnostic(*delay_agnostic_aec_));
Bjorn Volckerbf395c12015-03-25 22:45:56 +0100864 }
865
kwiberg102c6a62015-10-30 02:47:38 -0700866 if (options.extended_filter_aec) {
867 extended_filter_aec_ = options.extended_filter_aec;
868 }
869 if (extended_filter_aec_) {
870 LOG(LS_INFO) << "Extended filter aec is enabled? " << *extended_filter_aec_;
Henrik Lundin441f6342015-06-09 16:03:13 +0200871 config.Set<webrtc::ExtendedFilter>(
kwiberg102c6a62015-10-30 02:47:38 -0700872 new webrtc::ExtendedFilter(*extended_filter_aec_));
buildbot@webrtc.org1f8a2372014-08-28 10:52:44 +0000873 }
874
kwiberg102c6a62015-10-30 02:47:38 -0700875 if (options.experimental_ns) {
876 experimental_ns_ = options.experimental_ns;
877 }
878 if (experimental_ns_) {
879 LOG(LS_INFO) << "Experimental ns is enabled? " << *experimental_ns_;
buildbot@webrtc.org1f8a2372014-08-28 10:52:44 +0000880 config.Set<webrtc::ExperimentalNs>(
kwiberg102c6a62015-10-30 02:47:38 -0700881 new webrtc::ExperimentalNs(*experimental_ns_));
buildbot@webrtc.org1f8a2372014-08-28 10:52:44 +0000882 }
buildbot@webrtc.org1f8a2372014-08-28 10:52:44 +0000883
884 // We check audioproc for the benefit of tests, since FakeWebRtcVoiceEngine
885 // returns NULL on audio_processing().
886 webrtc::AudioProcessing* audioproc = voe_wrapper_->base()->audio_processing();
887 if (audioproc) {
888 audioproc->SetExtraOptions(config);
889 }
890
kwiberg102c6a62015-10-30 02:47:38 -0700891 if (options.recording_sample_rate) {
892 LOG(LS_INFO) << "Recording sample rate is "
893 << *options.recording_sample_rate;
894 if (voe_wrapper_->hw()->SetRecordingSampleRate(
895 *options.recording_sample_rate)) {
896 LOG_RTCERR1(SetRecordingSampleRate, *options.recording_sample_rate);
buildbot@webrtc.org13d67762014-05-02 17:33:29 +0000897 }
898 }
899
kwiberg102c6a62015-10-30 02:47:38 -0700900 if (options.playout_sample_rate) {
901 LOG(LS_INFO) << "Playout sample rate is " << *options.playout_sample_rate;
902 if (voe_wrapper_->hw()->SetPlayoutSampleRate(
903 *options.playout_sample_rate)) {
904 LOG_RTCERR1(SetPlayoutSampleRate, *options.playout_sample_rate);
buildbot@webrtc.org13d67762014-05-02 17:33:29 +0000905 }
906 }
907
908 return true;
909}
910
solenberg246b8172015-12-08 09:50:23 -0800911void WebRtcVoiceEngine::SetDefaultDevices() {
solenberg566ef242015-11-06 15:34:49 -0800912 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
kjellanderfcfc8042016-01-14 11:01:09 -0800913#if !defined(WEBRTC_IOS)
solenberg246b8172015-12-08 09:50:23 -0800914 int in_id = kDefaultAudioDeviceId;
915 int out_id = kDefaultAudioDeviceId;
916 LOG(LS_INFO) << "Setting microphone to (id=" << in_id
917 << ") and speaker to (id=" << out_id << ")";
buildbot@webrtc.org13d67762014-05-02 17:33:29 +0000918
solenbergc1a1b352015-09-22 13:31:20 -0700919 bool ret = true;
solenberg246b8172015-12-08 09:50:23 -0800920 if (voe_wrapper_->hw()->SetRecordingDevice(in_id) == -1) {
921 LOG_RTCERR1(SetRecordingDevice, in_id);
buildbot@webrtc.org13d67762014-05-02 17:33:29 +0000922 ret = false;
923 }
solenberg246b8172015-12-08 09:50:23 -0800924 webrtc::AudioProcessing* ap = voe()->base()->audio_processing();
925 if (ap) {
926 ap->Initialize();
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000927 }
928
solenberg246b8172015-12-08 09:50:23 -0800929 if (voe_wrapper_->hw()->SetPlayoutDevice(out_id) == -1) {
930 LOG_RTCERR1(SetPlayoutDevice, out_id);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000931 ret = false;
932 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000933
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000934 if (ret) {
solenberg246b8172015-12-08 09:50:23 -0800935 LOG(LS_INFO) << "Set microphone to (id=" << in_id
936 << ") and speaker to (id=" << out_id << ")";
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000937 }
kjellanderfcfc8042016-01-14 11:01:09 -0800938#endif // !WEBRTC_IOS
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000939}
940
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000941bool WebRtcVoiceEngine::GetOutputVolume(int* level) {
solenberg566ef242015-11-06 15:34:49 -0800942 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000943 unsigned int ulevel;
944 if (voe_wrapper_->volume()->GetSpeakerVolume(ulevel) == -1) {
945 LOG_RTCERR1(GetSpeakerVolume, level);
946 return false;
947 }
948 *level = ulevel;
949 return true;
950}
951
952bool WebRtcVoiceEngine::SetOutputVolume(int level) {
solenberg566ef242015-11-06 15:34:49 -0800953 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
henrikg91d6ede2015-09-17 00:24:34 -0700954 RTC_DCHECK(level >= 0 && level <= 255);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000955 if (voe_wrapper_->volume()->SetSpeakerVolume(level) == -1) {
956 LOG_RTCERR1(SetSpeakerVolume, level);
957 return false;
958 }
959 return true;
960}
961
962int WebRtcVoiceEngine::GetInputLevel() {
solenberg566ef242015-11-06 15:34:49 -0800963 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000964 unsigned int ulevel;
965 return (voe_wrapper_->volume()->GetSpeechInputLevel(ulevel) != -1) ?
966 static_cast<int>(ulevel) : -1;
967}
968
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000969const std::vector<AudioCodec>& WebRtcVoiceEngine::codecs() {
solenberg566ef242015-11-06 15:34:49 -0800970 RTC_DCHECK(signal_thread_checker_.CalledOnValidThread());
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000971 return codecs_;
972}
973
Stefan Holmer9d69c3f2015-12-07 10:45:43 +0100974RtpCapabilities WebRtcVoiceEngine::GetCapabilities() const {
solenberg566ef242015-11-06 15:34:49 -0800975 RTC_DCHECK(signal_thread_checker_.CalledOnValidThread());
Stefan Holmer9d69c3f2015-12-07 10:45:43 +0100976 RtpCapabilities capabilities;
977 capabilities.header_extensions.push_back(RtpHeaderExtension(
978 kRtpAudioLevelHeaderExtension, kRtpAudioLevelHeaderExtensionDefaultId));
979 capabilities.header_extensions.push_back(
980 RtpHeaderExtension(kRtpAbsoluteSenderTimeHeaderExtension,
981 kRtpAbsoluteSenderTimeHeaderExtensionDefaultId));
stefanba4c0e42016-02-04 04:12:24 -0800982 if (webrtc::field_trial::FindFullName("WebRTC-Audio-SendSideBwe") ==
983 "Enabled") {
984 capabilities.header_extensions.push_back(RtpHeaderExtension(
985 kRtpTransportSequenceNumberHeaderExtension,
986 kRtpTransportSequenceNumberHeaderExtensionDefaultId));
987 }
Stefan Holmer9d69c3f2015-12-07 10:45:43 +0100988 return capabilities;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000989}
990
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000991int WebRtcVoiceEngine::GetLastEngineError() {
solenberg566ef242015-11-06 15:34:49 -0800992 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000993 return voe_wrapper_->error();
994}
995
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000996void WebRtcVoiceEngine::Print(webrtc::TraceLevel level, const char* trace,
997 int length) {
solenberg566ef242015-11-06 15:34:49 -0800998 // Note: This callback can happen on any thread!
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000999 rtc::LoggingSeverity sev = rtc::LS_VERBOSE;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001000 if (level == webrtc::kTraceError || level == webrtc::kTraceCritical)
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00001001 sev = rtc::LS_ERROR;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001002 else if (level == webrtc::kTraceWarning)
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00001003 sev = rtc::LS_WARNING;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001004 else if (level == webrtc::kTraceStateInfo || level == webrtc::kTraceInfo)
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00001005 sev = rtc::LS_INFO;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001006 else if (level == webrtc::kTraceTerseInfo)
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00001007 sev = rtc::LS_INFO;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001008
solenberg72e29d22016-03-08 06:35:16 -08001009 // Skip past boilerplate prefix text.
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001010 if (length < 72) {
1011 std::string msg(trace, length);
1012 LOG(LS_ERROR) << "Malformed webrtc log message: ";
1013 LOG_V(sev) << msg;
1014 } else {
1015 std::string msg(trace + 71, length - 72);
Peter Boströmd5c75b12015-09-23 13:24:32 +02001016 LOG_V(sev) << "webrtc: " << msg;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001017 }
1018}
1019
solenberg63b34542015-09-29 06:06:31 -07001020void WebRtcVoiceEngine::RegisterChannel(WebRtcVoiceMediaChannel* channel) {
solenberg566ef242015-11-06 15:34:49 -08001021 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
1022 RTC_DCHECK(channel);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001023 channels_.push_back(channel);
1024}
1025
solenberg63b34542015-09-29 06:06:31 -07001026void WebRtcVoiceEngine::UnregisterChannel(WebRtcVoiceMediaChannel* channel) {
solenberg566ef242015-11-06 15:34:49 -08001027 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
solenberg63b34542015-09-29 06:06:31 -07001028 auto it = std::find(channels_.begin(), channels_.end(), channel);
solenberg566ef242015-11-06 15:34:49 -08001029 RTC_DCHECK(it != channels_.end());
1030 channels_.erase(it);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001031}
1032
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001033// Adjusts the default AGC target level by the specified delta.
1034// NB: If we start messing with other config fields, we'll want
1035// to save the current webrtc::AgcConfig as well.
1036bool WebRtcVoiceEngine::AdjustAgcLevel(int delta) {
solenberg566ef242015-11-06 15:34:49 -08001037 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001038 webrtc::AgcConfig config = default_agc_config_;
1039 config.targetLeveldBOv -= delta;
1040
1041 LOG(LS_INFO) << "Adjusting AGC level from default -"
1042 << default_agc_config_.targetLeveldBOv << "dB to -"
1043 << config.targetLeveldBOv << "dB";
1044
1045 if (voe_wrapper_->processing()->SetAgcConfig(config) == -1) {
1046 LOG_RTCERR1(SetAgcConfig, config.targetLeveldBOv);
1047 return false;
1048 }
1049 return true;
1050}
1051
Fredrik Solenbergccb49e72015-05-19 11:37:56 +02001052bool WebRtcVoiceEngine::SetAudioDeviceModule(webrtc::AudioDeviceModule* adm) {
solenberg566ef242015-11-06 15:34:49 -08001053 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001054 if (initialized_) {
1055 LOG(LS_WARNING) << "SetAudioDeviceModule can not be called after Init.";
1056 return false;
1057 }
1058 if (adm_) {
1059 adm_->Release();
1060 adm_ = NULL;
1061 }
1062 if (adm) {
1063 adm_ = adm;
1064 adm_->AddRef();
1065 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001066 return true;
1067}
1068
ivocd66b44d2016-01-15 03:06:36 -08001069bool WebRtcVoiceEngine::StartAecDump(rtc::PlatformFile file,
1070 int64_t max_size_bytes) {
solenberg566ef242015-11-06 15:34:49 -08001071 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00001072 FILE* aec_dump_file_stream = rtc::FdopenPlatformFileForWriting(file);
wu@webrtc.orga8910d22014-01-23 22:12:45 +00001073 if (!aec_dump_file_stream) {
1074 LOG(LS_ERROR) << "Could not open AEC dump file stream.";
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00001075 if (!rtc::ClosePlatformFile(file))
wu@webrtc.orga8910d22014-01-23 22:12:45 +00001076 LOG(LS_WARNING) << "Could not close file.";
1077 return false;
1078 }
wu@webrtc.orga9890802013-12-13 00:21:03 +00001079 StopAecDump();
ivocd66b44d2016-01-15 03:06:36 -08001080 if (voe_wrapper_->base()->audio_processing()->StartDebugRecording(
1081 aec_dump_file_stream, max_size_bytes) !=
wu@webrtc.orga9890802013-12-13 00:21:03 +00001082 webrtc::AudioProcessing::kNoError) {
wu@webrtc.orga8910d22014-01-23 22:12:45 +00001083 LOG_RTCERR0(StartDebugRecording);
1084 fclose(aec_dump_file_stream);
wu@webrtc.orga9890802013-12-13 00:21:03 +00001085 return false;
1086 }
1087 is_dumping_aec_ = true;
1088 return true;
wu@webrtc.orga9890802013-12-13 00:21:03 +00001089}
1090
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001091void WebRtcVoiceEngine::StartAecDump(const std::string& filename) {
solenberg566ef242015-11-06 15:34:49 -08001092 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001093 if (!is_dumping_aec_) {
1094 // Start dumping AEC when we are not dumping.
ivocd66b44d2016-01-15 03:06:36 -08001095 if (voe_wrapper_->base()->audio_processing()->StartDebugRecording(
1096 filename.c_str(), -1) != webrtc::AudioProcessing::kNoError) {
wu@webrtc.orga9890802013-12-13 00:21:03 +00001097 LOG_RTCERR1(StartDebugRecording, filename.c_str());
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001098 } else {
1099 is_dumping_aec_ = true;
1100 }
1101 }
1102}
1103
1104void WebRtcVoiceEngine::StopAecDump() {
solenberg566ef242015-11-06 15:34:49 -08001105 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001106 if (is_dumping_aec_) {
1107 // Stop dumping AEC when we are dumping.
ivocd66b44d2016-01-15 03:06:36 -08001108 if (voe_wrapper_->base()->audio_processing()->StopDebugRecording() !=
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001109 webrtc::AudioProcessing::kNoError) {
1110 LOG_RTCERR0(StopDebugRecording);
1111 }
1112 is_dumping_aec_ = false;
1113 }
1114}
1115
ivoc112a3d82015-10-16 02:22:18 -07001116bool WebRtcVoiceEngine::StartRtcEventLog(rtc::PlatformFile file) {
solenberg566ef242015-11-06 15:34:49 -08001117 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
ivoc20834ca2016-02-04 06:33:37 -08001118 webrtc::RtcEventLog* event_log = voe_wrapper_->codec()->GetEventLog();
1119 if (event_log) {
1120 return event_log->StartLogging(file);
1121 }
1122 LOG_RTCERR0(StartRtcEventLog);
1123 return false;
ivoc112a3d82015-10-16 02:22:18 -07001124}
1125
1126void WebRtcVoiceEngine::StopRtcEventLog() {
solenberg566ef242015-11-06 15:34:49 -08001127 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
ivoc20834ca2016-02-04 06:33:37 -08001128 webrtc::RtcEventLog* event_log = voe_wrapper_->codec()->GetEventLog();
1129 if (event_log) {
1130 event_log->StopLogging();
1131 return;
1132 }
1133 LOG_RTCERR0(StopRtcEventLog);
ivoc112a3d82015-10-16 02:22:18 -07001134}
1135
solenberg0a617e22015-10-20 15:49:38 -07001136int WebRtcVoiceEngine::CreateVoEChannel() {
solenberg566ef242015-11-06 15:34:49 -08001137 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
solenberg0a617e22015-10-20 15:49:38 -07001138 return voe_wrapper_->base()->CreateChannel(voe_config_);
sergeyu@chromium.org5bc25c42013-12-05 00:24:06 +00001139}
1140
solenbergc96df772015-10-21 13:01:53 -07001141class WebRtcVoiceMediaChannel::WebRtcAudioSendStream
Taylor Brandstetter1a018dc2016-03-08 12:37:39 -08001142 : public AudioSource::Sink {
mallinath@webrtc.org67ee6b92014-02-03 16:57:16 +00001143 public:
solenbergc96df772015-10-21 13:01:53 -07001144 WebRtcAudioSendStream(int ch, webrtc::AudioTransport* voe_audio_transport,
solenberg3a941542015-11-16 07:34:50 -08001145 uint32_t ssrc, const std::string& c_name,
1146 const std::vector<webrtc::RtpExtension>& extensions,
1147 webrtc::Call* call)
solenberg7add0582015-11-20 09:59:34 -08001148 : voe_audio_transport_(voe_audio_transport),
solenberg3a941542015-11-16 07:34:50 -08001149 call_(call),
1150 config_(nullptr) {
solenberg85a04962015-10-27 03:35:21 -07001151 RTC_DCHECK_GE(ch, 0);
1152 // TODO(solenberg): Once we're not using FakeWebRtcVoiceEngine anymore:
1153 // RTC_DCHECK(voe_audio_transport);
solenbergc96df772015-10-21 13:01:53 -07001154 RTC_DCHECK(call);
solenberg85a04962015-10-27 03:35:21 -07001155 audio_capture_thread_checker_.DetachFromThread();
solenberg3a941542015-11-16 07:34:50 -08001156 config_.rtp.ssrc = ssrc;
1157 config_.rtp.c_name = c_name;
1158 config_.voe_channel_id = ch;
1159 RecreateAudioSendStream(extensions);
solenbergc96df772015-10-21 13:01:53 -07001160 }
solenberg3a941542015-11-16 07:34:50 -08001161
solenbergc96df772015-10-21 13:01:53 -07001162 ~WebRtcAudioSendStream() override {
solenberg566ef242015-11-06 15:34:49 -08001163 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
Taylor Brandstetter1a018dc2016-03-08 12:37:39 -08001164 ClearSource();
solenbergc96df772015-10-21 13:01:53 -07001165 call_->DestroyAudioSendStream(stream_);
1166 }
henrike@webrtc.org1e09a712013-07-26 19:17:59 +00001167
solenberg3a941542015-11-16 07:34:50 -08001168 void RecreateAudioSendStream(
1169 const std::vector<webrtc::RtpExtension>& extensions) {
1170 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
1171 if (stream_) {
1172 call_->DestroyAudioSendStream(stream_);
1173 stream_ = nullptr;
1174 }
1175 config_.rtp.extensions = extensions;
1176 RTC_DCHECK(!stream_);
1177 stream_ = call_->CreateAudioSendStream(config_);
1178 RTC_CHECK(stream_);
1179 }
1180
solenberg8842c3e2016-03-11 03:06:41 -08001181 bool SendTelephoneEvent(int payload_type, int event, int duration_ms) {
Fredrik Solenbergb5727682015-12-04 15:22:19 +01001182 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
1183 RTC_DCHECK(stream_);
1184 return stream_->SendTelephoneEvent(payload_type, event, duration_ms);
1185 }
1186
Taylor Brandstetter1a018dc2016-03-08 12:37:39 -08001187 void SetSend(bool send) {
1188 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
1189 send_ = send;
1190 UpdateSendState();
1191 }
1192
solenberg3a941542015-11-16 07:34:50 -08001193 webrtc::AudioSendStream::Stats GetStats() const {
1194 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
1195 RTC_DCHECK(stream_);
1196 return stream_->GetStats();
1197 }
1198
Taylor Brandstetter1a018dc2016-03-08 12:37:39 -08001199 // Starts the sending by setting ourselves as a sink to the AudioSource to
1200 // get data callbacks.
henrike@webrtc.orga7b98182014-02-21 15:51:43 +00001201 // This method is called on the libjingle worker thread.
mallinath@webrtc.org67ee6b92014-02-03 16:57:16 +00001202 // TODO(xians): Make sure Start() is called only once.
Taylor Brandstetter1a018dc2016-03-08 12:37:39 -08001203 void SetSource(AudioSource* source) {
solenberg566ef242015-11-06 15:34:49 -08001204 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
Taylor Brandstetter1a018dc2016-03-08 12:37:39 -08001205 RTC_DCHECK(source);
1206 if (source_) {
1207 RTC_DCHECK(source_ == source);
mallinath@webrtc.org67ee6b92014-02-03 16:57:16 +00001208 return;
1209 }
Taylor Brandstetter1a018dc2016-03-08 12:37:39 -08001210 source->SetSink(this);
1211 source_ = source;
1212 UpdateSendState();
mallinath@webrtc.org67ee6b92014-02-03 16:57:16 +00001213 }
1214
Taylor Brandstetter1a018dc2016-03-08 12:37:39 -08001215 // Stops sending by setting the sink of the AudioSource to nullptr. No data
mallinath@webrtc.org67ee6b92014-02-03 16:57:16 +00001216 // callback will be received after this method.
henrike@webrtc.orga7b98182014-02-21 15:51:43 +00001217 // This method is called on the libjingle worker thread.
Taylor Brandstetter1a018dc2016-03-08 12:37:39 -08001218 void ClearSource() {
solenberg566ef242015-11-06 15:34:49 -08001219 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
Taylor Brandstetter1a018dc2016-03-08 12:37:39 -08001220 if (source_) {
1221 source_->SetSink(nullptr);
1222 source_ = nullptr;
solenberg98c68862015-10-09 03:27:14 -07001223 }
Taylor Brandstetter1a018dc2016-03-08 12:37:39 -08001224 UpdateSendState();
mallinath@webrtc.org67ee6b92014-02-03 16:57:16 +00001225 }
1226
Taylor Brandstetter1a018dc2016-03-08 12:37:39 -08001227 // AudioSource::Sink implementation.
henrike@webrtc.orga7b98182014-02-21 15:51:43 +00001228 // This method is called on the audio thread.
kjellander@webrtc.org14665ff2015-03-04 12:58:35 +00001229 void OnData(const void* audio_data,
1230 int bits_per_sample,
1231 int sample_rate,
Peter Kasting69558702016-01-12 16:26:35 -08001232 size_t number_of_channels,
Peter Kastingdce40cf2015-08-24 14:52:23 -07001233 size_t number_of_frames) override {
solenberg566ef242015-11-06 15:34:49 -08001234 RTC_DCHECK(!worker_thread_checker_.CalledOnValidThread());
solenberg85a04962015-10-27 03:35:21 -07001235 RTC_DCHECK(audio_capture_thread_checker_.CalledOnValidThread());
solenbergc96df772015-10-21 13:01:53 -07001236 RTC_DCHECK(voe_audio_transport_);
solenberg7add0582015-11-20 09:59:34 -08001237 voe_audio_transport_->OnData(config_.voe_channel_id,
henrike@webrtc.orga7b98182014-02-21 15:51:43 +00001238 audio_data,
1239 bits_per_sample,
1240 sample_rate,
1241 number_of_channels,
1242 number_of_frames);
henrike@webrtc.orga7b98182014-02-21 15:51:43 +00001243 }
1244
Taylor Brandstetter1a018dc2016-03-08 12:37:39 -08001245 // Callback from the |source_| when it is going away. In case Start() has
henrike@webrtc.orga7b98182014-02-21 15:51:43 +00001246 // never been called, this callback won't be triggered.
kjellander@webrtc.org14665ff2015-03-04 12:58:35 +00001247 void OnClose() override {
solenberg566ef242015-11-06 15:34:49 -08001248 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
Taylor Brandstetter1a018dc2016-03-08 12:37:39 -08001249 // Set |source_| to nullptr to make sure no more callback will get into
1250 // the source.
1251 source_ = nullptr;
1252 UpdateSendState();
mallinath@webrtc.org67ee6b92014-02-03 16:57:16 +00001253 }
1254
1255 // Accessor to the VoE channel ID.
solenberg85a04962015-10-27 03:35:21 -07001256 int channel() const {
solenberg566ef242015-11-06 15:34:49 -08001257 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
solenberg7add0582015-11-20 09:59:34 -08001258 return config_.voe_channel_id;
solenberg85a04962015-10-27 03:35:21 -07001259 }
mallinath@webrtc.org67ee6b92014-02-03 16:57:16 +00001260
1261 private:
Taylor Brandstetter1a018dc2016-03-08 12:37:39 -08001262 void UpdateSendState() {
1263 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
1264 RTC_DCHECK(stream_);
1265 if (send_ && source_ != nullptr) {
1266 stream_->Start();
1267 } else { // !send || source_ = nullptr
1268 stream_->Stop();
1269 }
1270 }
1271
solenberg566ef242015-11-06 15:34:49 -08001272 rtc::ThreadChecker worker_thread_checker_;
solenberg85a04962015-10-27 03:35:21 -07001273 rtc::ThreadChecker audio_capture_thread_checker_;
solenbergc96df772015-10-21 13:01:53 -07001274 webrtc::AudioTransport* const voe_audio_transport_ = nullptr;
1275 webrtc::Call* call_ = nullptr;
solenberg3a941542015-11-16 07:34:50 -08001276 webrtc::AudioSendStream::Config config_;
1277 // The stream is owned by WebRtcAudioSendStream and may be reallocated if
1278 // configuration changes.
solenbergc96df772015-10-21 13:01:53 -07001279 webrtc::AudioSendStream* stream_ = nullptr;
mallinath@webrtc.org67ee6b92014-02-03 16:57:16 +00001280
Taylor Brandstetter1a018dc2016-03-08 12:37:39 -08001281 // Raw pointer to AudioSource owned by LocalAudioTrackHandler.
mallinath@webrtc.org67ee6b92014-02-03 16:57:16 +00001282 // PeerConnection will make sure invalidating the pointer before the object
1283 // goes away.
Taylor Brandstetter1a018dc2016-03-08 12:37:39 -08001284 AudioSource* source_ = nullptr;
1285 bool send_ = false;
henrike@webrtc.orga7b98182014-02-21 15:51:43 +00001286
solenbergc96df772015-10-21 13:01:53 -07001287 RTC_DISALLOW_IMPLICIT_CONSTRUCTORS(WebRtcAudioSendStream);
1288};
1289
1290class WebRtcVoiceMediaChannel::WebRtcAudioReceiveStream {
1291 public:
stefanba4c0e42016-02-04 04:12:24 -08001292 WebRtcAudioReceiveStream(int ch,
1293 uint32_t remote_ssrc,
1294 uint32_t local_ssrc,
1295 bool use_transport_cc,
1296 const std::string& sync_group,
solenberg7add0582015-11-20 09:59:34 -08001297 const std::vector<webrtc::RtpExtension>& extensions,
1298 webrtc::Call* call)
stefanba4c0e42016-02-04 04:12:24 -08001299 : call_(call), config_() {
solenberg7add0582015-11-20 09:59:34 -08001300 RTC_DCHECK_GE(ch, 0);
1301 RTC_DCHECK(call);
1302 config_.rtp.remote_ssrc = remote_ssrc;
1303 config_.rtp.local_ssrc = local_ssrc;
1304 config_.voe_channel_id = ch;
1305 config_.sync_group = sync_group;
stefanba4c0e42016-02-04 04:12:24 -08001306 RecreateAudioReceiveStream(use_transport_cc, extensions);
solenberg7add0582015-11-20 09:59:34 -08001307 }
solenbergc96df772015-10-21 13:01:53 -07001308
solenberg7add0582015-11-20 09:59:34 -08001309 ~WebRtcAudioReceiveStream() {
1310 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
1311 call_->DestroyAudioReceiveStream(stream_);
1312 }
1313
1314 void RecreateAudioReceiveStream(
1315 const std::vector<webrtc::RtpExtension>& extensions) {
1316 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
stefanba4c0e42016-02-04 04:12:24 -08001317 RecreateAudioReceiveStream(config_.rtp.transport_cc, extensions);
solenberg7add0582015-11-20 09:59:34 -08001318 }
stefanba4c0e42016-02-04 04:12:24 -08001319 void RecreateAudioReceiveStream(bool use_transport_cc) {
solenberg7add0582015-11-20 09:59:34 -08001320 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
stefanba4c0e42016-02-04 04:12:24 -08001321 RecreateAudioReceiveStream(use_transport_cc, config_.rtp.extensions);
solenberg7add0582015-11-20 09:59:34 -08001322 }
1323
1324 webrtc::AudioReceiveStream::Stats GetStats() const {
1325 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
1326 RTC_DCHECK(stream_);
1327 return stream_->GetStats();
1328 }
1329
1330 int channel() const {
1331 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
1332 return config_.voe_channel_id;
1333 }
solenbergc96df772015-10-21 13:01:53 -07001334
kwiberg686a8ef2016-02-26 03:00:35 -08001335 void SetRawAudioSink(std::unique_ptr<webrtc::AudioSinkInterface> sink) {
Tommif888bb52015-12-12 01:37:01 +01001336 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
kwiberg686a8ef2016-02-26 03:00:35 -08001337 stream_->SetSink(std::move(sink));
Tommif888bb52015-12-12 01:37:01 +01001338 }
1339
solenbergc96df772015-10-21 13:01:53 -07001340 private:
stefanba4c0e42016-02-04 04:12:24 -08001341 void RecreateAudioReceiveStream(
1342 bool use_transport_cc,
solenberg7add0582015-11-20 09:59:34 -08001343 const std::vector<webrtc::RtpExtension>& extensions) {
1344 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
1345 if (stream_) {
1346 call_->DestroyAudioReceiveStream(stream_);
1347 stream_ = nullptr;
1348 }
1349 config_.rtp.extensions = extensions;
stefanba4c0e42016-02-04 04:12:24 -08001350 config_.rtp.transport_cc = use_transport_cc;
solenberg7add0582015-11-20 09:59:34 -08001351 RTC_DCHECK(!stream_);
1352 stream_ = call_->CreateAudioReceiveStream(config_);
1353 RTC_CHECK(stream_);
1354 }
1355
1356 rtc::ThreadChecker worker_thread_checker_;
1357 webrtc::Call* call_ = nullptr;
1358 webrtc::AudioReceiveStream::Config config_;
1359 // The stream is owned by WebRtcAudioReceiveStream and may be reallocated if
1360 // configuration changes.
1361 webrtc::AudioReceiveStream* stream_ = nullptr;
solenbergc96df772015-10-21 13:01:53 -07001362
1363 RTC_DISALLOW_IMPLICIT_CONSTRUCTORS(WebRtcAudioReceiveStream);
henrike@webrtc.org1e09a712013-07-26 19:17:59 +00001364};
1365
Fredrik Solenberg709ed672015-09-15 12:26:33 +02001366WebRtcVoiceMediaChannel::WebRtcVoiceMediaChannel(WebRtcVoiceEngine* engine,
nisse51542be2016-02-12 02:27:06 -08001367 const MediaConfig& config,
Fredrik Solenbergb071a192015-09-17 16:42:56 +02001368 const AudioOptions& options,
Fredrik Solenberg709ed672015-09-15 12:26:33 +02001369 webrtc::Call* call)
nisse51542be2016-02-12 02:27:06 -08001370 : VoiceMediaChannel(config), engine_(engine), call_(call) {
solenberg0a617e22015-10-20 15:49:38 -07001371 LOG(LS_VERBOSE) << "WebRtcVoiceMediaChannel::WebRtcVoiceMediaChannel";
solenberg566ef242015-11-06 15:34:49 -08001372 RTC_DCHECK(call);
solenberg0a617e22015-10-20 15:49:38 -07001373 engine->RegisterChannel(this);
Fredrik Solenbergb071a192015-09-17 16:42:56 +02001374 SetOptions(options);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001375}
1376
1377WebRtcVoiceMediaChannel::~WebRtcVoiceMediaChannel() {
solenberg566ef242015-11-06 15:34:49 -08001378 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
solenberg0a617e22015-10-20 15:49:38 -07001379 LOG(LS_VERBOSE) << "WebRtcVoiceMediaChannel::~WebRtcVoiceMediaChannel";
solenberg7add0582015-11-20 09:59:34 -08001380 // TODO(solenberg): Should be able to delete the streams directly, without
1381 // going through RemoveNnStream(), once stream objects handle
1382 // all (de)configuration.
solenbergc96df772015-10-21 13:01:53 -07001383 while (!send_streams_.empty()) {
1384 RemoveSendStream(send_streams_.begin()->first);
solenbergd97ec302015-10-07 01:40:33 -07001385 }
solenberg7add0582015-11-20 09:59:34 -08001386 while (!recv_streams_.empty()) {
1387 RemoveRecvStream(recv_streams_.begin()->first);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001388 }
solenberg0a617e22015-10-20 15:49:38 -07001389 engine()->UnregisterChannel(this);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001390}
1391
nisse51542be2016-02-12 02:27:06 -08001392rtc::DiffServCodePoint WebRtcVoiceMediaChannel::PreferredDscp() const {
1393 return kAudioDscpValue;
1394}
1395
Peter Thatcherc2ee2c82015-08-07 16:05:34 -07001396bool WebRtcVoiceMediaChannel::SetSendParameters(
1397 const AudioSendParameters& params) {
Peter Boströmca8b4042016-03-08 14:24:13 -08001398 TRACE_EVENT0("webrtc", "WebRtcVoiceMediaChannel::SetSendParameters");
solenberg566ef242015-11-06 15:34:49 -08001399 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
solenberg7e4e01a2015-12-02 08:05:01 -08001400 LOG(LS_INFO) << "WebRtcVoiceMediaChannel::SetSendParameters: "
1401 << params.ToString();
Peter Thatcherc2ee2c82015-08-07 16:05:34 -07001402 // TODO(pthatcher): Refactor this to be more clean now that we have
1403 // all the information at once.
solenberg3a941542015-11-16 07:34:50 -08001404
1405 if (!SetSendCodecs(params.codecs)) {
1406 return false;
1407 }
1408
solenberg7e4e01a2015-12-02 08:05:01 -08001409 if (!ValidateRtpExtensions(params.extensions)) {
1410 return false;
1411 }
1412 std::vector<webrtc::RtpExtension> filtered_extensions =
1413 FilterRtpExtensions(params.extensions,
1414 webrtc::RtpExtension::IsSupportedForAudio, true);
1415 if (send_rtp_extensions_ != filtered_extensions) {
1416 send_rtp_extensions_.swap(filtered_extensions);
solenberg3a941542015-11-16 07:34:50 -08001417 for (auto& it : send_streams_) {
1418 it.second->RecreateAudioSendStream(send_rtp_extensions_);
1419 }
1420 }
1421
1422 if (!SetMaxSendBandwidth(params.max_bandwidth_bps)) {
1423 return false;
1424 }
1425 return SetOptions(params.options);
Peter Thatcherc2ee2c82015-08-07 16:05:34 -07001426}
1427
1428bool WebRtcVoiceMediaChannel::SetRecvParameters(
1429 const AudioRecvParameters& params) {
Peter Boströmca8b4042016-03-08 14:24:13 -08001430 TRACE_EVENT0("webrtc", "WebRtcVoiceMediaChannel::SetRecvParameters");
solenberg566ef242015-11-06 15:34:49 -08001431 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
solenberg7e4e01a2015-12-02 08:05:01 -08001432 LOG(LS_INFO) << "WebRtcVoiceMediaChannel::SetRecvParameters: "
1433 << params.ToString();
Peter Thatcherc2ee2c82015-08-07 16:05:34 -07001434 // TODO(pthatcher): Refactor this to be more clean now that we have
1435 // all the information at once.
solenberg7add0582015-11-20 09:59:34 -08001436
1437 if (!SetRecvCodecs(params.codecs)) {
1438 return false;
1439 }
1440
solenberg7e4e01a2015-12-02 08:05:01 -08001441 if (!ValidateRtpExtensions(params.extensions)) {
1442 return false;
1443 }
1444 std::vector<webrtc::RtpExtension> filtered_extensions =
1445 FilterRtpExtensions(params.extensions,
1446 webrtc::RtpExtension::IsSupportedForAudio, false);
1447 if (recv_rtp_extensions_ != filtered_extensions) {
1448 recv_rtp_extensions_.swap(filtered_extensions);
solenberg7add0582015-11-20 09:59:34 -08001449 for (auto& it : recv_streams_) {
1450 it.second->RecreateAudioReceiveStream(recv_rtp_extensions_);
1451 }
1452 }
solenberg7add0582015-11-20 09:59:34 -08001453 return true;
Peter Thatcherc2ee2c82015-08-07 16:05:34 -07001454}
1455
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001456bool WebRtcVoiceMediaChannel::SetOptions(const AudioOptions& options) {
solenberg566ef242015-11-06 15:34:49 -08001457 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001458 LOG(LS_INFO) << "Setting voice channel options: "
1459 << options.ToString();
1460
1461 // We retain all of the existing options, and apply the given ones
1462 // on top. This means there is no way to "clear" options such that
1463 // they go back to the engine default.
1464 options_.SetAll(options);
solenberg246b8172015-12-08 09:50:23 -08001465 if (!engine()->ApplyOptions(options_)) {
1466 LOG(LS_WARNING) <<
1467 "Failed to apply engine options during channel SetOptions.";
1468 return false;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001469 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001470 LOG(LS_INFO) << "Set voice channel options. Current options: "
1471 << options_.ToString();
1472 return true;
1473}
1474
1475bool WebRtcVoiceMediaChannel::SetRecvCodecs(
1476 const std::vector<AudioCodec>& codecs) {
solenberg566ef242015-11-06 15:34:49 -08001477 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
solenberg8fb30c32015-10-13 03:06:58 -07001478
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001479 // Set the payload types to be used for incoming media.
solenberg0b675462015-10-09 01:37:09 -07001480 LOG(LS_INFO) << "Setting receive voice codecs.";
solenberg0b675462015-10-09 01:37:09 -07001481
1482 if (!VerifyUniquePayloadTypes(codecs)) {
1483 LOG(LS_ERROR) << "Codec payload types overlap.";
1484 return false;
1485 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001486
1487 std::vector<AudioCodec> new_codecs;
1488 // Find all new codecs. We allow adding new codecs but don't allow changing
1489 // the payload type of codecs that is already configured since we might
1490 // already be receiving packets with that payload type.
Fredrik Solenbergaf9fb212015-08-26 10:45:53 +02001491 for (const AudioCodec& codec : codecs) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001492 AudioCodec old_codec;
Fredrik Solenbergaf9fb212015-08-26 10:45:53 +02001493 if (FindCodec(recv_codecs_, codec, &old_codec)) {
1494 if (old_codec.id != codec.id) {
1495 LOG(LS_ERROR) << codec.name << " payload type changed.";
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001496 return false;
1497 }
1498 } else {
Fredrik Solenbergaf9fb212015-08-26 10:45:53 +02001499 new_codecs.push_back(codec);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001500 }
1501 }
1502 if (new_codecs.empty()) {
1503 // There are no new codecs to configure. Already configured codecs are
1504 // never removed.
1505 return true;
1506 }
1507
1508 if (playout_) {
1509 // Receive codecs can not be changed while playing. So we temporarily
1510 // pause playout.
1511 PausePlayout();
1512 }
1513
solenberg26c8c912015-11-27 04:00:25 -08001514 bool result = true;
1515 for (const AudioCodec& codec : new_codecs) {
solenberg72e29d22016-03-08 06:35:16 -08001516 webrtc::CodecInst voe_codec = {0};
solenberg26c8c912015-11-27 04:00:25 -08001517 if (WebRtcVoiceEngine::ToCodecInst(codec, &voe_codec)) {
1518 LOG(LS_INFO) << ToString(codec);
1519 voe_codec.pltype = codec.id;
1520 for (const auto& ch : recv_streams_) {
1521 if (engine()->voe()->codec()->SetRecPayloadType(
1522 ch.second->channel(), voe_codec) == -1) {
1523 LOG_RTCERR2(SetRecPayloadType, ch.second->channel(),
1524 ToString(voe_codec));
1525 result = false;
1526 }
1527 }
1528 } else {
1529 LOG(LS_WARNING) << "Unknown codec " << ToString(codec);
1530 result = false;
1531 break;
1532 }
1533 }
Fredrik Solenbergaf9fb212015-08-26 10:45:53 +02001534 if (result) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001535 recv_codecs_ = codecs;
1536 }
1537
1538 if (desired_playout_ && !playout_) {
1539 ResumePlayout();
1540 }
Fredrik Solenbergaf9fb212015-08-26 10:45:53 +02001541 return result;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001542}
1543
solenberg72e29d22016-03-08 06:35:16 -08001544// Utility function called from SetSendParameters() to extract current send
1545// codec settings from the given list of codecs (originally from SDP). Both send
1546// and receive streams may be reconfigured based on the new settings.
wu@webrtc.orgcadf9042013-08-30 21:24:16 +00001547bool WebRtcVoiceMediaChannel::SetSendCodecs(
1548 const std::vector<AudioCodec>& codecs) {
solenberg566ef242015-11-06 15:34:49 -08001549 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
Fredrik Solenbergb5727682015-12-04 15:22:19 +01001550 // TODO(solenberg): Validate input - that payload types don't overlap, are
1551 // within range, filter out codecs we don't support,
1552 // redundant codecs etc.
solenbergd97ec302015-10-07 01:40:33 -07001553
Fredrik Solenbergb5727682015-12-04 15:22:19 +01001554 // Find the DTMF telephone event "codec" payload type.
1555 dtmf_payload_type_ = rtc::Optional<int>();
Fredrik Solenbergaf9fb212015-08-26 10:45:53 +02001556 for (const AudioCodec& codec : codecs) {
Fredrik Solenbergaf9fb212015-08-26 10:45:53 +02001557 if (IsCodec(codec, kDtmfCodecName)) {
Fredrik Solenbergb5727682015-12-04 15:22:19 +01001558 dtmf_payload_type_ = rtc::Optional<int>(codec.id);
1559 break;
wu@webrtc.orgcadf9042013-08-30 21:24:16 +00001560 }
1561 }
1562
solenberg72e29d22016-03-08 06:35:16 -08001563 // Scan through the list to figure out the codec to use for sending, along
1564 // with the proper configuration for VAD, CNG, RED, NACK and Opus-specific
1565 // parameters.
1566 {
1567 SendCodecSpec send_codec_spec;
1568 send_codec_spec.nack_enabled = send_codec_spec_.nack_enabled;
1569
1570 // Find send codec (the first non-telephone-event/CN codec).
1571 const AudioCodec* codec = WebRtcVoiceCodecs::GetPreferredCodec(
1572 codecs, &send_codec_spec.codec_inst, &send_codec_spec.red_payload_type);
1573 if (!codec) {
1574 LOG(LS_WARNING) << "Received empty list of codecs.";
1575 return false;
1576 }
1577
1578 send_codec_spec.transport_cc_enabled = HasTransportCc(*codec);
1579
1580 // This condition is apparently here because Opus does not support RED and
1581 // FEC simultaneously. However, DTX and max playback rate shouldn't have
1582 // such limitations.
1583 // TODO(solenberg): Refactor this logic once we create AudioEncoders here.
1584 if (send_codec_spec.red_payload_type == -1) {
1585 send_codec_spec.nack_enabled = HasNack(*codec);
1586 // For Opus as the send codec, we are to determine inband FEC, maximum
1587 // playback rate, and opus internal dtx.
1588 if (IsCodec(*codec, kOpusCodecName)) {
1589 GetOpusConfig(*codec, &send_codec_spec.codec_inst,
1590 &send_codec_spec.enable_codec_fec,
1591 &send_codec_spec.opus_max_playback_rate,
1592 &send_codec_spec.enable_opus_dtx);
1593 }
1594
1595 // Set packet size if the AudioCodec param kCodecParamPTime is set.
1596 int ptime_ms = 0;
1597 if (codec->GetParam(kCodecParamPTime, &ptime_ms)) {
1598 if (!WebRtcVoiceCodecs::SetPTimeAsPacketSize(
1599 &send_codec_spec.codec_inst, ptime_ms)) {
1600 LOG(LS_WARNING) << "Failed to set packet size for codec "
1601 << send_codec_spec.codec_inst.plname;
1602 return false;
1603 }
1604 }
1605 }
1606
1607 // Loop through the codecs list again to find the CN codec.
1608 // TODO(solenberg): Break out into a separate function?
1609 for (const AudioCodec& codec : codecs) {
1610 // Ignore codecs we don't know about. The negotiation step should prevent
1611 // this, but double-check to be sure.
1612 webrtc::CodecInst voe_codec = {0};
1613 if (!WebRtcVoiceEngine::ToCodecInst(codec, &voe_codec)) {
1614 LOG(LS_WARNING) << "Unknown codec " << ToString(codec);
1615 continue;
1616 }
1617
1618 if (IsCodec(codec, kCnCodecName)) {
1619 // Turn voice activity detection/comfort noise on if supported.
1620 // Set the wideband CN payload type appropriately.
1621 // (narrowband always uses the static payload type 13).
1622 int cng_plfreq = -1;
1623 switch (codec.clockrate) {
1624 case 8000:
1625 case 16000:
1626 case 32000:
1627 cng_plfreq = codec.clockrate;
1628 break;
1629 default:
1630 LOG(LS_WARNING) << "CN frequency " << codec.clockrate
1631 << " not supported.";
1632 continue;
1633 }
1634 send_codec_spec.cng_payload_type = codec.id;
1635 send_codec_spec.cng_plfreq = cng_plfreq;
1636 break;
1637 }
1638 }
1639
1640 // Latch in the new state.
1641 send_codec_spec_ = std::move(send_codec_spec);
1642 }
1643
wu@webrtc.orgcadf9042013-08-30 21:24:16 +00001644 // Cache the codecs in order to configure the channel created later.
solenbergc96df772015-10-21 13:01:53 -07001645 for (const auto& ch : send_streams_) {
solenberg72e29d22016-03-08 06:35:16 -08001646 if (!SetSendCodecs(ch.second->channel())) {
wu@webrtc.orgcadf9042013-08-30 21:24:16 +00001647 return false;
1648 }
1649 }
1650
solenberg72e29d22016-03-08 06:35:16 -08001651 // Set nack status on receive channels.
1652 if (!send_streams_.empty()) {
1653 for (const auto& kv : recv_streams_) {
1654 SetNack(kv.second->channel(), send_codec_spec_.nack_enabled);
1655 }
wu@webrtc.org9dba5252013-08-05 20:36:57 +00001656 }
solenberg0a617e22015-10-20 15:49:38 -07001657
stefanba4c0e42016-02-04 04:12:24 -08001658 // Check if the transport cc feedback has changed on the preferred send codec,
1659 // and in that case reconfigure all receive streams.
solenberg72e29d22016-03-08 06:35:16 -08001660 if (recv_transport_cc_enabled_ != send_codec_spec_.transport_cc_enabled) {
1661 LOG(LS_INFO) << "Recreate all the receive streams because the send "
1662 "codec has changed.";
1663 recv_transport_cc_enabled_ = send_codec_spec_.transport_cc_enabled;
1664 for (auto& kv : recv_streams_) {
1665 kv.second->RecreateAudioReceiveStream(recv_transport_cc_enabled_);
1666 }
1667 }
1668
1669 return true;
1670}
1671
1672// Apply current codec settings to a single voe::Channel used for sending.
1673bool WebRtcVoiceMediaChannel::SetSendCodecs(int channel) {
1674 // Disable VAD, FEC, and RED unless we know the other side wants them.
1675 engine()->voe()->codec()->SetVADStatus(channel, false);
1676 engine()->voe()->rtp()->SetNACKStatus(channel, false, 0);
1677 engine()->voe()->rtp()->SetREDStatus(channel, false);
1678 engine()->voe()->codec()->SetFECStatus(channel, false);
1679
1680 if (send_codec_spec_.red_payload_type != -1) {
1681 // Enable redundant encoding of the specified codec. Treat any
1682 // failure as a fatal internal error.
1683 LOG(LS_INFO) << "Enabling RED on channel " << channel;
1684 if (engine()->voe()->rtp()->SetREDStatus(channel, true,
1685 send_codec_spec_.red_payload_type) == -1) {
1686 LOG_RTCERR3(SetREDStatus, channel, true,
1687 send_codec_spec_.red_payload_type);
1688 return false;
1689 }
1690 }
1691
1692 SetNack(channel, send_codec_spec_.nack_enabled);
1693
1694 // Set the codec immediately, since SetVADStatus() depends on whether
1695 // the current codec is mono or stereo.
1696 if (!SetSendCodec(channel, send_codec_spec_.codec_inst)) {
1697 return false;
1698 }
1699
1700 // FEC should be enabled after SetSendCodec.
1701 if (send_codec_spec_.enable_codec_fec) {
1702 LOG(LS_INFO) << "Attempt to enable codec internal FEC on channel "
1703 << channel;
1704 if (engine()->voe()->codec()->SetFECStatus(channel, true) == -1) {
1705 // Enable codec internal FEC. Treat any failure as fatal internal error.
1706 LOG_RTCERR2(SetFECStatus, channel, true);
1707 return false;
1708 }
1709 }
1710
1711 if (IsCodec(send_codec_spec_.codec_inst, kOpusCodecName)) {
1712 // DTX and maxplaybackrate should be set after SetSendCodec. Because current
1713 // send codec has to be Opus.
1714
1715 // Set Opus internal DTX.
1716 LOG(LS_INFO) << "Attempt to "
1717 << (send_codec_spec_.enable_opus_dtx ? "enable" : "disable")
1718 << " Opus DTX on channel "
1719 << channel;
1720 if (engine()->voe()->codec()->SetOpusDtx(channel,
1721 send_codec_spec_.enable_opus_dtx)) {
1722 LOG_RTCERR2(SetOpusDtx, channel, send_codec_spec_.enable_opus_dtx);
1723 return false;
1724 }
1725
1726 // If opus_max_playback_rate <= 0, the default maximum playback rate
1727 // (48 kHz) will be used.
1728 if (send_codec_spec_.opus_max_playback_rate > 0) {
1729 LOG(LS_INFO) << "Attempt to set maximum playback rate to "
1730 << send_codec_spec_.opus_max_playback_rate
1731 << " Hz on channel "
1732 << channel;
1733 if (engine()->voe()->codec()->SetOpusMaxPlaybackRate(
1734 channel, send_codec_spec_.opus_max_playback_rate) == -1) {
1735 LOG_RTCERR2(SetOpusMaxPlaybackRate, channel,
1736 send_codec_spec_.opus_max_playback_rate);
1737 return false;
stefanba4c0e42016-02-04 04:12:24 -08001738 }
1739 }
1740 }
1741
solenberg72e29d22016-03-08 06:35:16 -08001742 if (send_bitrate_setting_) {
1743 SetSendBitrateInternal(send_bitrate_bps_);
1744 }
1745
1746 // Set the CN payloadtype and the VAD status.
1747 if (send_codec_spec_.cng_payload_type != -1) {
1748 // The CN payload type for 8000 Hz clockrate is fixed at 13.
1749 if (send_codec_spec_.cng_plfreq != 8000) {
1750 webrtc::PayloadFrequencies cn_freq;
1751 switch (send_codec_spec_.cng_plfreq) {
1752 case 16000:
1753 cn_freq = webrtc::kFreq16000Hz;
1754 break;
1755 case 32000:
1756 cn_freq = webrtc::kFreq32000Hz;
1757 break;
1758 default:
1759 RTC_NOTREACHED();
1760 return false;
1761 }
1762 if (engine()->voe()->codec()->SetSendCNPayloadType(
1763 channel, send_codec_spec_.cng_payload_type, cn_freq) == -1) {
1764 LOG_RTCERR3(SetSendCNPayloadType, channel,
1765 send_codec_spec_.cng_payload_type, cn_freq);
1766 // TODO(ajm): This failure condition will be removed from VoE.
1767 // Restore the return here when we update to a new enough webrtc.
1768 //
1769 // Not returning false because the SetSendCNPayloadType will fail if
1770 // the channel is already sending.
1771 // This can happen if the remote description is applied twice, for
1772 // example in the case of ROAP on top of JSEP, where both side will
1773 // send the offer.
1774 }
1775 }
1776
1777 // Only turn on VAD if we have a CN payload type that matches the
1778 // clockrate for the codec we are going to use.
1779 if (send_codec_spec_.cng_plfreq == send_codec_spec_.codec_inst.plfreq &&
1780 send_codec_spec_.codec_inst.channels == 1) {
1781 // TODO(minyue): If CN frequency == 48000 Hz is allowed, consider the
1782 // interaction between VAD and Opus FEC.
1783 LOG(LS_INFO) << "Enabling VAD";
1784 if (engine()->voe()->codec()->SetVADStatus(channel, true) == -1) {
1785 LOG_RTCERR2(SetVADStatus, channel, true);
1786 return false;
1787 }
1788 }
1789 }
solenberg0a617e22015-10-20 15:49:38 -07001790 return true;
wu@webrtc.org9dba5252013-08-05 20:36:57 +00001791}
1792
wu@webrtc.orgcadf9042013-08-30 21:24:16 +00001793void WebRtcVoiceMediaChannel::SetNack(int channel, bool nack_enabled) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001794 if (nack_enabled) {
wu@webrtc.orgcadf9042013-08-30 21:24:16 +00001795 LOG(LS_INFO) << "Enabling NACK for channel " << channel;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001796 engine()->voe()->rtp()->SetNACKStatus(channel, true, kNackMaxPackets);
1797 } else {
wu@webrtc.orgcadf9042013-08-30 21:24:16 +00001798 LOG(LS_INFO) << "Disabling NACK for channel " << channel;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001799 engine()->voe()->rtp()->SetNACKStatus(channel, false, 0);
1800 }
1801}
1802
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001803bool WebRtcVoiceMediaChannel::SetSendCodec(
wu@webrtc.org9dba5252013-08-05 20:36:57 +00001804 int channel, const webrtc::CodecInst& send_codec) {
1805 LOG(LS_INFO) << "Send channel " << channel << " selected voice codec "
1806 << ToString(send_codec) << ", bitrate=" << send_codec.rate;
1807
solenberg72e29d22016-03-08 06:35:16 -08001808 webrtc::CodecInst current_codec = {0};
wu@webrtc.org05e7b442014-04-01 17:44:24 +00001809 if (engine()->voe()->codec()->GetSendCodec(channel, current_codec) == 0 &&
1810 (send_codec == current_codec)) {
1811 // Codec is already configured, we can return without setting it again.
1812 return true;
1813 }
1814
wu@webrtc.org9dba5252013-08-05 20:36:57 +00001815 if (engine()->voe()->codec()->SetSendCodec(channel, send_codec) == -1) {
1816 LOG_RTCERR2(SetSendCodec, channel, ToString(send_codec));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001817 return false;
1818 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001819 return true;
1820}
1821
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001822bool WebRtcVoiceMediaChannel::SetPlayout(bool playout) {
1823 desired_playout_ = playout;
1824 return ChangePlayout(desired_playout_);
1825}
1826
1827bool WebRtcVoiceMediaChannel::PausePlayout() {
1828 return ChangePlayout(false);
1829}
1830
1831bool WebRtcVoiceMediaChannel::ResumePlayout() {
1832 return ChangePlayout(desired_playout_);
1833}
1834
1835bool WebRtcVoiceMediaChannel::ChangePlayout(bool playout) {
Peter Boströmca8b4042016-03-08 14:24:13 -08001836 TRACE_EVENT0("webrtc", "WebRtcVoiceMediaChannel::ChangePlayout");
solenberg566ef242015-11-06 15:34:49 -08001837 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001838 if (playout_ == playout) {
1839 return true;
1840 }
1841
solenberg7add0582015-11-20 09:59:34 -08001842 for (const auto& ch : recv_streams_) {
Fredrik Solenbergaf9fb212015-08-26 10:45:53 +02001843 if (!SetPlayout(ch.second->channel(), playout)) {
henrike@webrtc.org1e09a712013-07-26 19:17:59 +00001844 LOG(LS_ERROR) << "SetPlayout " << playout << " on channel "
Fredrik Solenbergaf9fb212015-08-26 10:45:53 +02001845 << ch.second->channel() << " failed";
solenberg1ac56142015-10-13 03:58:19 -07001846 return false;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001847 }
1848 }
solenberg1ac56142015-10-13 03:58:19 -07001849 playout_ = playout;
1850 return true;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001851}
1852
Taylor Brandstetter1a018dc2016-03-08 12:37:39 -08001853void WebRtcVoiceMediaChannel::SetSend(bool send) {
Peter Boströmca8b4042016-03-08 14:24:13 -08001854 TRACE_EVENT0("webrtc", "WebRtcVoiceMediaChannel::SetSend");
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001855 if (send_ == send) {
Taylor Brandstetter1a018dc2016-03-08 12:37:39 -08001856 return;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001857 }
1858
solenberg246b8172015-12-08 09:50:23 -08001859 // Apply channel specific options when channel is enabled for sending.
Taylor Brandstetter1a018dc2016-03-08 12:37:39 -08001860 if (send) {
solenberg63b34542015-09-29 06:06:31 -07001861 engine()->ApplyOptions(options_);
1862 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001863
wu@webrtc.org9dba5252013-08-05 20:36:57 +00001864 // Change the settings on each send channel.
Taylor Brandstetter1a018dc2016-03-08 12:37:39 -08001865 for (auto& kv : send_streams_) {
1866 kv.second->SetSend(send);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001867 }
wu@webrtc.org9dba5252013-08-05 20:36:57 +00001868
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001869 send_ = send;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001870}
1871
Peter Boström0c4e06b2015-10-07 12:23:21 +02001872bool WebRtcVoiceMediaChannel::SetAudioSend(uint32_t ssrc,
1873 bool enable,
solenberg1dd98f32015-09-10 01:57:14 -07001874 const AudioOptions* options,
Taylor Brandstetter1a018dc2016-03-08 12:37:39 -08001875 AudioSource* source) {
solenberg566ef242015-11-06 15:34:49 -08001876 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
solenberg1dd98f32015-09-10 01:57:14 -07001877 // TODO(solenberg): The state change should be fully rolled back if any one of
1878 // these calls fail.
Taylor Brandstetter1a018dc2016-03-08 12:37:39 -08001879 if (!SetLocalSource(ssrc, source)) {
solenberg1dd98f32015-09-10 01:57:14 -07001880 return false;
1881 }
solenbergdfc8f4f2015-10-01 02:31:10 -07001882 if (!MuteStream(ssrc, !enable)) {
solenberg1dd98f32015-09-10 01:57:14 -07001883 return false;
1884 }
solenbergdfc8f4f2015-10-01 02:31:10 -07001885 if (enable && options) {
solenberg1dd98f32015-09-10 01:57:14 -07001886 return SetOptions(*options);
1887 }
1888 return true;
1889}
1890
solenberg0a617e22015-10-20 15:49:38 -07001891int WebRtcVoiceMediaChannel::CreateVoEChannel() {
1892 int id = engine()->CreateVoEChannel();
1893 if (id == -1) {
1894 LOG_RTCERR0(CreateVoEChannel);
1895 return -1;
wu@webrtc.org9dba5252013-08-05 20:36:57 +00001896 }
solenberg0a617e22015-10-20 15:49:38 -07001897 if (engine()->voe()->network()->RegisterExternalTransport(id, *this) == -1) {
1898 LOG_RTCERR2(RegisterExternalTransport, id, this);
1899 engine()->voe()->base()->DeleteChannel(id);
1900 return -1;
1901 }
1902 return id;
wu@webrtc.org9dba5252013-08-05 20:36:57 +00001903}
1904
solenberg7add0582015-11-20 09:59:34 -08001905bool WebRtcVoiceMediaChannel::DeleteVoEChannel(int channel) {
wu@webrtc.org9dba5252013-08-05 20:36:57 +00001906 if (engine()->voe()->network()->DeRegisterExternalTransport(channel) == -1) {
1907 LOG_RTCERR1(DeRegisterExternalTransport, channel);
1908 }
wu@webrtc.org9dba5252013-08-05 20:36:57 +00001909 if (engine()->voe()->base()->DeleteChannel(channel) == -1) {
1910 LOG_RTCERR1(DeleteChannel, channel);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001911 return false;
1912 }
wu@webrtc.org9dba5252013-08-05 20:36:57 +00001913 return true;
1914}
henrike@webrtc.org1e09a712013-07-26 19:17:59 +00001915
wu@webrtc.org9dba5252013-08-05 20:36:57 +00001916bool WebRtcVoiceMediaChannel::AddSendStream(const StreamParams& sp) {
Peter Boströmca8b4042016-03-08 14:24:13 -08001917 TRACE_EVENT0("webrtc", "WebRtcVoiceMediaChannel::AddSendStream");
solenberg566ef242015-11-06 15:34:49 -08001918 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
solenberg0a617e22015-10-20 15:49:38 -07001919 LOG(LS_INFO) << "AddSendStream: " << sp.ToString();
1920
1921 uint32_t ssrc = sp.first_ssrc();
1922 RTC_DCHECK(0 != ssrc);
1923
1924 if (GetSendChannelId(ssrc) != -1) {
1925 LOG(LS_ERROR) << "Stream already exists with ssrc " << ssrc;
wu@webrtc.org9dba5252013-08-05 20:36:57 +00001926 return false;
1927 }
1928
solenberg0a617e22015-10-20 15:49:38 -07001929 // Create a new channel for sending audio data.
1930 int channel = CreateVoEChannel();
1931 if (channel == -1) {
1932 return false;
wu@webrtc.org9dba5252013-08-05 20:36:57 +00001933 }
wu@webrtc.org9dba5252013-08-05 20:36:57 +00001934
solenbergc96df772015-10-21 13:01:53 -07001935 // Save the channel to send_streams_, so that RemoveSendStream() can still
wu@webrtc.org9dba5252013-08-05 20:36:57 +00001936 // delete the channel in case failure happens below.
mallinath@webrtc.org67ee6b92014-02-03 16:57:16 +00001937 webrtc::AudioTransport* audio_transport =
1938 engine()->voe()->base()->audio_transport();
solenberg3a941542015-11-16 07:34:50 -08001939 send_streams_.insert(std::make_pair(ssrc, new WebRtcAudioSendStream(
1940 channel, audio_transport, ssrc, sp.cname, send_rtp_extensions_, call_)));
wu@webrtc.org9dba5252013-08-05 20:36:57 +00001941
solenberg0a617e22015-10-20 15:49:38 -07001942 // Set the current codecs to be used for the new channel. We need to do this
1943 // after adding the channel to send_channels_, because of how max bitrate is
1944 // currently being configured by SetSendCodec().
solenberg72e29d22016-03-08 06:35:16 -08001945 if (HasSendCodec() && !SetSendCodecs(channel)) {
solenberg0a617e22015-10-20 15:49:38 -07001946 RemoveSendStream(ssrc);
wu@webrtc.org9dba5252013-08-05 20:36:57 +00001947 return false;
1948 }
1949
1950 // At this point the channel's local SSRC has been updated. If the channel is
solenberg0a617e22015-10-20 15:49:38 -07001951 // the first send channel make sure that all the receive channels are updated
1952 // with the same SSRC in order to send receiver reports.
solenbergc96df772015-10-21 13:01:53 -07001953 if (send_streams_.size() == 1) {
solenberg0a617e22015-10-20 15:49:38 -07001954 receiver_reports_ssrc_ = ssrc;
solenberg7add0582015-11-20 09:59:34 -08001955 for (const auto& stream : recv_streams_) {
1956 int recv_channel = stream.second->channel();
solenberg0a617e22015-10-20 15:49:38 -07001957 if (engine()->voe()->rtp()->SetLocalSSRC(recv_channel, ssrc) != 0) {
solenberg7add0582015-11-20 09:59:34 -08001958 LOG_RTCERR2(SetLocalSSRC, recv_channel, ssrc);
solenberg1ac56142015-10-13 03:58:19 -07001959 return false;
wu@webrtc.org9dba5252013-08-05 20:36:57 +00001960 }
solenberg0a617e22015-10-20 15:49:38 -07001961 engine()->voe()->base()->AssociateSendChannel(recv_channel, channel);
1962 LOG(LS_INFO) << "VoiceEngine channel #" << recv_channel
1963 << " is associated with channel #" << channel << ".";
wu@webrtc.org9dba5252013-08-05 20:36:57 +00001964 }
1965 }
1966
Taylor Brandstetter1a018dc2016-03-08 12:37:39 -08001967 send_streams_[ssrc]->SetSend(send_);
1968 return true;
wu@webrtc.org9dba5252013-08-05 20:36:57 +00001969}
1970
Peter Boström0c4e06b2015-10-07 12:23:21 +02001971bool WebRtcVoiceMediaChannel::RemoveSendStream(uint32_t ssrc) {
Peter Boströmca8b4042016-03-08 14:24:13 -08001972 TRACE_EVENT0("webrtc", "WebRtcVoiceMediaChannel::RemoveSendStream");
solenberg566ef242015-11-06 15:34:49 -08001973 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
solenberg3a941542015-11-16 07:34:50 -08001974 LOG(LS_INFO) << "RemoveSendStream: " << ssrc;
1975
solenbergc96df772015-10-21 13:01:53 -07001976 auto it = send_streams_.find(ssrc);
1977 if (it == send_streams_.end()) {
wu@webrtc.org9dba5252013-08-05 20:36:57 +00001978 LOG(LS_WARNING) << "Try to remove stream with ssrc " << ssrc
1979 << " which doesn't exist.";
1980 return false;
1981 }
1982
Taylor Brandstetter1a018dc2016-03-08 12:37:39 -08001983 it->second->SetSend(false);
wu@webrtc.org9dba5252013-08-05 20:36:57 +00001984
solenberg7add0582015-11-20 09:59:34 -08001985 // Clean up and delete the send stream+channel.
Taylor Brandstetter1a018dc2016-03-08 12:37:39 -08001986 int channel = it->second->channel();
solenberg0a617e22015-10-20 15:49:38 -07001987 LOG(LS_INFO) << "Removing audio send stream " << ssrc
1988 << " with VoiceEngine channel #" << channel << ".";
solenberg7add0582015-11-20 09:59:34 -08001989 delete it->second;
1990 send_streams_.erase(it);
1991 if (!DeleteVoEChannel(channel)) {
solenberg0a617e22015-10-20 15:49:38 -07001992 return false;
wu@webrtc.org9dba5252013-08-05 20:36:57 +00001993 }
solenbergc96df772015-10-21 13:01:53 -07001994 if (send_streams_.empty()) {
Taylor Brandstetter1a018dc2016-03-08 12:37:39 -08001995 SetSend(false);
solenberg0a617e22015-10-20 15:49:38 -07001996 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001997 return true;
1998}
1999
2000bool WebRtcVoiceMediaChannel::AddRecvStream(const StreamParams& sp) {
Peter Boströmca8b4042016-03-08 14:24:13 -08002001 TRACE_EVENT0("webrtc", "WebRtcVoiceMediaChannel::AddRecvStream");
solenberg566ef242015-11-06 15:34:49 -08002002 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
solenbergd97ec302015-10-07 01:40:33 -07002003 LOG(LS_INFO) << "AddRecvStream: " << sp.ToString();
2004
solenberg0b675462015-10-09 01:37:09 -07002005 if (!ValidateStreamParams(sp)) {
wu@webrtc.org78187522013-10-07 23:32:02 +00002006 return false;
2007 }
2008
solenberg7add0582015-11-20 09:59:34 -08002009 const uint32_t ssrc = sp.first_ssrc();
solenberg0b675462015-10-09 01:37:09 -07002010 if (ssrc == 0) {
2011 LOG(LS_WARNING) << "AddRecvStream with ssrc==0 is not supported.";
2012 return false;
2013 }
2014
solenberg1ac56142015-10-13 03:58:19 -07002015 // Remove the default receive stream if one had been created with this ssrc;
2016 // we'll recreate it then.
2017 if (IsDefaultRecvStream(ssrc)) {
2018 RemoveRecvStream(ssrc);
2019 }
solenberg0b675462015-10-09 01:37:09 -07002020
solenberg7add0582015-11-20 09:59:34 -08002021 if (GetReceiveChannelId(ssrc) != -1) {
henrike@webrtc.org1e09a712013-07-26 19:17:59 +00002022 LOG(LS_ERROR) << "Stream already exists with ssrc " << ssrc;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002023 return false;
2024 }
Fredrik Solenberg4b60c732015-05-07 14:07:48 +02002025
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002026 // Create a new channel for receiving audio data.
solenberg7add0582015-11-20 09:59:34 -08002027 const int channel = CreateVoEChannel();
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002028 if (channel == -1) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002029 return false;
2030 }
Minyue2013aec2015-05-13 14:14:42 +02002031
solenberg1ac56142015-10-13 03:58:19 -07002032 // Turn off all supported codecs.
solenberg26c8c912015-11-27 04:00:25 -08002033 // TODO(solenberg): Remove once "no codecs" is the default state of a stream.
2034 for (webrtc::CodecInst voe_codec : webrtc::acm2::RentACodec::Database()) {
2035 voe_codec.pltype = -1;
2036 if (engine()->voe()->codec()->SetRecPayloadType(channel, voe_codec) == -1) {
2037 LOG_RTCERR2(SetRecPayloadType, channel, ToString(voe_codec));
2038 DeleteVoEChannel(channel);
2039 return false;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002040 }
2041 }
2042
solenberg1ac56142015-10-13 03:58:19 -07002043 // Only enable those configured for this channel.
2044 for (const auto& codec : recv_codecs_) {
solenberg72e29d22016-03-08 06:35:16 -08002045 webrtc::CodecInst voe_codec = {0};
solenberg26c8c912015-11-27 04:00:25 -08002046 if (WebRtcVoiceEngine::ToCodecInst(codec, &voe_codec)) {
solenberg1ac56142015-10-13 03:58:19 -07002047 voe_codec.pltype = codec.id;
2048 if (engine()->voe()->codec()->SetRecPayloadType(
2049 channel, voe_codec) == -1) {
2050 LOG_RTCERR2(SetRecPayloadType, channel, ToString(voe_codec));
solenberg7add0582015-11-20 09:59:34 -08002051 DeleteVoEChannel(channel);
solenberg1ac56142015-10-13 03:58:19 -07002052 return false;
2053 }
henrike@webrtc.org1e09a712013-07-26 19:17:59 +00002054 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002055 }
solenberg8fb30c32015-10-13 03:06:58 -07002056
solenberg7add0582015-11-20 09:59:34 -08002057 const int send_channel = GetSendChannelId(receiver_reports_ssrc_);
2058 if (send_channel != -1) {
2059 // Associate receive channel with first send channel (so the receive channel
2060 // can obtain RTT from the send channel)
2061 engine()->voe()->base()->AssociateSendChannel(channel, send_channel);
2062 LOG(LS_INFO) << "VoiceEngine channel #" << channel
2063 << " is associated with channel #" << send_channel << ".";
buildbot@webrtc.org150835e2014-05-06 15:54:38 +00002064 }
2065
stefanba4c0e42016-02-04 04:12:24 -08002066 recv_streams_.insert(std::make_pair(
2067 ssrc, new WebRtcAudioReceiveStream(channel, ssrc, receiver_reports_ssrc_,
solenberg72e29d22016-03-08 06:35:16 -08002068 recv_transport_cc_enabled_,
2069 sp.sync_label, recv_rtp_extensions_,
2070 call_)));
solenberg7add0582015-11-20 09:59:34 -08002071
solenberg72e29d22016-03-08 06:35:16 -08002072 SetNack(channel, send_codec_spec_.nack_enabled);
solenberg1ac56142015-10-13 03:58:19 -07002073 SetPlayout(channel, playout_);
solenberg7add0582015-11-20 09:59:34 -08002074
solenberg1ac56142015-10-13 03:58:19 -07002075 return true;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002076}
2077
Peter Boström0c4e06b2015-10-07 12:23:21 +02002078bool WebRtcVoiceMediaChannel::RemoveRecvStream(uint32_t ssrc) {
Peter Boströmca8b4042016-03-08 14:24:13 -08002079 TRACE_EVENT0("webrtc", "WebRtcVoiceMediaChannel::RemoveRecvStream");
solenberg566ef242015-11-06 15:34:49 -08002080 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
solenbergd97ec302015-10-07 01:40:33 -07002081 LOG(LS_INFO) << "RemoveRecvStream: " << ssrc;
2082
solenberg7add0582015-11-20 09:59:34 -08002083 const auto it = recv_streams_.find(ssrc);
2084 if (it == recv_streams_.end()) {
wu@webrtc.org9dba5252013-08-05 20:36:57 +00002085 LOG(LS_WARNING) << "Try to remove stream with ssrc " << ssrc
2086 << " which doesn't exist.";
henrike@webrtc.org1e09a712013-07-26 19:17:59 +00002087 return false;
wu@webrtc.org9dba5252013-08-05 20:36:57 +00002088 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002089
solenberg1ac56142015-10-13 03:58:19 -07002090 // Deregister default channel, if that's the one being destroyed.
2091 if (IsDefaultRecvStream(ssrc)) {
2092 default_recv_ssrc_ = -1;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002093 }
henrike@webrtc.org1e09a712013-07-26 19:17:59 +00002094
solenberg7add0582015-11-20 09:59:34 -08002095 const int channel = it->second->channel();
2096
2097 // Clean up and delete the receive stream+channel.
2098 LOG(LS_INFO) << "Removing audio receive stream " << ssrc
mallinath@webrtc.org67ee6b92014-02-03 16:57:16 +00002099 << " with VoiceEngine channel #" << channel << ".";
Tommif888bb52015-12-12 01:37:01 +01002100 it->second->SetRawAudioSink(nullptr);
solenberg7add0582015-11-20 09:59:34 -08002101 delete it->second;
2102 recv_streams_.erase(it);
2103 return DeleteVoEChannel(channel);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002104}
2105
Taylor Brandstetter1a018dc2016-03-08 12:37:39 -08002106bool WebRtcVoiceMediaChannel::SetLocalSource(uint32_t ssrc,
2107 AudioSource* source) {
solenbergc96df772015-10-21 13:01:53 -07002108 auto it = send_streams_.find(ssrc);
2109 if (it == send_streams_.end()) {
Taylor Brandstetter1a018dc2016-03-08 12:37:39 -08002110 if (source) {
2111 // Return an error if trying to set a valid source with an invalid ssrc.
2112 LOG(LS_ERROR) << "SetLocalSource failed with ssrc " << ssrc;
wu@webrtc.org9dba5252013-08-05 20:36:57 +00002113 return false;
2114 }
2115
2116 // The channel likely has gone away, do nothing.
henrike@webrtc.org1e09a712013-07-26 19:17:59 +00002117 return true;
henrike@webrtc.org1e09a712013-07-26 19:17:59 +00002118 }
2119
Taylor Brandstetter1a018dc2016-03-08 12:37:39 -08002120 if (source) {
2121 it->second->SetSource(source);
solenberg1ac56142015-10-13 03:58:19 -07002122 } else {
Taylor Brandstetter1a018dc2016-03-08 12:37:39 -08002123 it->second->ClearSource();
solenberg1ac56142015-10-13 03:58:19 -07002124 }
henrike@webrtc.org1e09a712013-07-26 19:17:59 +00002125
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002126 return true;
2127}
2128
2129bool WebRtcVoiceMediaChannel::GetActiveStreams(
2130 AudioInfo::StreamList* actives) {
solenberg566ef242015-11-06 15:34:49 -08002131 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002132 actives->clear();
solenberg7add0582015-11-20 09:59:34 -08002133 for (const auto& ch : recv_streams_) {
Fredrik Solenbergaf9fb212015-08-26 10:45:53 +02002134 int level = GetOutputLevel(ch.second->channel());
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002135 if (level > 0) {
Fredrik Solenbergaf9fb212015-08-26 10:45:53 +02002136 actives->push_back(std::make_pair(ch.first, level));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002137 }
2138 }
2139 return true;
2140}
2141
2142int WebRtcVoiceMediaChannel::GetOutputLevel() {
solenberg566ef242015-11-06 15:34:49 -08002143 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
solenberg1ac56142015-10-13 03:58:19 -07002144 int highest = 0;
solenberg7add0582015-11-20 09:59:34 -08002145 for (const auto& ch : recv_streams_) {
solenberg8fb30c32015-10-13 03:06:58 -07002146 highest = std::max(GetOutputLevel(ch.second->channel()), highest);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002147 }
2148 return highest;
2149}
2150
2151int WebRtcVoiceMediaChannel::GetTimeSinceLastTyping() {
2152 int ret;
2153 if (engine()->voe()->processing()->TimeSinceLastTyping(ret) == -1) {
2154 // In case of error, log the info and continue
2155 LOG_RTCERR0(TimeSinceLastTyping);
2156 ret = -1;
2157 } else {
2158 ret *= 1000; // We return ms, webrtc returns seconds.
2159 }
2160 return ret;
2161}
2162
2163void WebRtcVoiceMediaChannel::SetTypingDetectionParameters(int time_window,
2164 int cost_per_typing, int reporting_threshold, int penalty_decay,
2165 int type_event_delay) {
2166 if (engine()->voe()->processing()->SetTypingDetectionParameters(
2167 time_window, cost_per_typing,
2168 reporting_threshold, penalty_decay, type_event_delay) == -1) {
2169 // In case of error, log the info and continue
2170 LOG_RTCERR5(SetTypingDetectionParameters, time_window,
2171 cost_per_typing, reporting_threshold, penalty_decay,
2172 type_event_delay);
2173 }
2174}
2175
solenberg4bac9c52015-10-09 02:32:53 -07002176bool WebRtcVoiceMediaChannel::SetOutputVolume(uint32_t ssrc, double volume) {
solenberg566ef242015-11-06 15:34:49 -08002177 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
solenberg1ac56142015-10-13 03:58:19 -07002178 if (ssrc == 0) {
2179 default_recv_volume_ = volume;
2180 if (default_recv_ssrc_ == -1) {
2181 return true;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002182 }
solenberg1ac56142015-10-13 03:58:19 -07002183 ssrc = static_cast<uint32_t>(default_recv_ssrc_);
2184 }
2185 int ch_id = GetReceiveChannelId(ssrc);
2186 if (ch_id < 0) {
2187 LOG(LS_WARNING) << "Cannot find channel for ssrc:" << ssrc;
2188 return false;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002189 }
2190
solenberg1ac56142015-10-13 03:58:19 -07002191 if (-1 == engine()->voe()->volume()->SetChannelOutputVolumeScaling(ch_id,
2192 volume)) {
2193 LOG_RTCERR2(SetChannelOutputVolumeScaling, ch_id, volume);
2194 return false;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002195 }
solenberg1ac56142015-10-13 03:58:19 -07002196 LOG(LS_INFO) << "SetOutputVolume to " << volume
2197 << " for channel " << ch_id << " and ssrc " << ssrc;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002198 return true;
2199}
2200
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002201bool WebRtcVoiceMediaChannel::CanInsertDtmf() {
Fredrik Solenbergb5727682015-12-04 15:22:19 +01002202 return dtmf_payload_type_ ? true : false;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002203}
2204
solenberg1d63dd02015-12-02 12:35:09 -08002205bool WebRtcVoiceMediaChannel::InsertDtmf(uint32_t ssrc, int event,
2206 int duration) {
solenberg566ef242015-11-06 15:34:49 -08002207 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
Fredrik Solenbergb5727682015-12-04 15:22:19 +01002208 LOG(LS_INFO) << "WebRtcVoiceMediaChannel::InsertDtmf";
2209 if (!dtmf_payload_type_) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002210 return false;
2211 }
2212
Fredrik Solenbergb5727682015-12-04 15:22:19 +01002213 // Figure out which WebRtcAudioSendStream to send the event on.
2214 auto it = ssrc != 0 ? send_streams_.find(ssrc) : send_streams_.begin();
2215 if (it == send_streams_.end()) {
2216 LOG(LS_WARNING) << "The specified ssrc " << ssrc << " is not in use.";
solenberg1d63dd02015-12-02 12:35:09 -08002217 return false;
2218 }
Fredrik Solenbergb5727682015-12-04 15:22:19 +01002219 if (event < kMinTelephoneEventCode ||
2220 event > kMaxTelephoneEventCode) {
2221 LOG(LS_WARNING) << "DTMF event code " << event << " out of range.";
solenberg1d63dd02015-12-02 12:35:09 -08002222 return false;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002223 }
Fredrik Solenbergb5727682015-12-04 15:22:19 +01002224 if (duration < kMinTelephoneEventDuration ||
2225 duration > kMaxTelephoneEventDuration) {
2226 LOG(LS_WARNING) << "DTMF event duration " << duration << " out of range.";
2227 return false;
2228 }
2229 return it->second->SendTelephoneEvent(*dtmf_payload_type_, event, duration);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002230}
2231
wu@webrtc.orga9890802013-12-13 00:21:03 +00002232void WebRtcVoiceMediaChannel::OnPacketReceived(
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00002233 rtc::Buffer* packet, const rtc::PacketTime& packet_time) {
solenberg566ef242015-11-06 15:34:49 -08002234 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
Fredrik Solenberg4b60c732015-05-07 14:07:48 +02002235
solenberg1ac56142015-10-13 03:58:19 -07002236 uint32_t ssrc = 0;
2237 if (!GetRtpSsrc(packet->data(), packet->size(), &ssrc)) {
2238 return;
2239 }
2240
solenberg7e63ef02015-11-20 00:19:43 -08002241 // If we don't have a default channel, and the SSRC is unknown, create a
2242 // default channel.
2243 if (default_recv_ssrc_ == -1 && GetReceiveChannelId(ssrc) == -1) {
solenberg1ac56142015-10-13 03:58:19 -07002244 StreamParams sp;
2245 sp.ssrcs.push_back(ssrc);
2246 LOG(LS_INFO) << "Creating default receive stream for SSRC=" << ssrc << ".";
2247 if (!AddRecvStream(sp)) {
2248 LOG(LS_WARNING) << "Could not create default receive stream.";
2249 return;
2250 }
2251 default_recv_ssrc_ = ssrc;
2252 SetOutputVolume(default_recv_ssrc_, default_recv_volume_);
deadbeef884f5852016-01-15 09:20:04 -08002253 if (default_sink_) {
kwiberg686a8ef2016-02-26 03:00:35 -08002254 std::unique_ptr<webrtc::AudioSinkInterface> proxy_sink(
deadbeef884f5852016-01-15 09:20:04 -08002255 new ProxySink(default_sink_.get()));
2256 SetRawAudioSink(default_recv_ssrc_, std::move(proxy_sink));
2257 }
solenberg1ac56142015-10-13 03:58:19 -07002258 }
2259
2260 // Forward packet to Call. If the SSRC is unknown we'll return after this.
Fredrik Solenberg709ed672015-09-15 12:26:33 +02002261 const webrtc::PacketTime webrtc_packet_time(packet_time.timestamp,
2262 packet_time.not_before);
solenberg1ac56142015-10-13 03:58:19 -07002263 webrtc::PacketReceiver::DeliveryStatus delivery_result =
2264 call_->Receiver()->DeliverPacket(webrtc::MediaType::AUDIO,
2265 reinterpret_cast<const uint8_t*>(packet->data()), packet->size(),
2266 webrtc_packet_time);
2267 if (webrtc::PacketReceiver::DELIVERY_OK != delivery_result) {
solenberg7e63ef02015-11-20 00:19:43 -08002268 // If the SSRC is unknown here, route it to the default channel, if we have
2269 // one. See: https://bugs.chromium.org/p/webrtc/issues/detail?id=5208
2270 if (default_recv_ssrc_ == -1) {
2271 return;
2272 } else {
2273 ssrc = default_recv_ssrc_;
2274 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002275 }
2276
solenberg1ac56142015-10-13 03:58:19 -07002277 // Find the channel to send this packet to. It must exist since webrtc::Call
2278 // was able to demux the packet.
2279 int channel = GetReceiveChannelId(ssrc);
2280 RTC_DCHECK(channel != -1);
2281
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002282 // Pass it off to the decoder.
henrike@webrtc.org28654cb2013-07-22 21:07:49 +00002283 engine()->voe()->network()->ReceivedRTPPacket(
solenberg1ac56142015-10-13 03:58:19 -07002284 channel, packet->data(), packet->size(), webrtc_packet_time);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002285}
2286
wu@webrtc.orga9890802013-12-13 00:21:03 +00002287void WebRtcVoiceMediaChannel::OnRtcpReceived(
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00002288 rtc::Buffer* packet, const rtc::PacketTime& packet_time) {
solenberg566ef242015-11-06 15:34:49 -08002289 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
Fredrik Solenberg4b60c732015-05-07 14:07:48 +02002290
Fredrik Solenberg709ed672015-09-15 12:26:33 +02002291 // Forward packet to Call as well.
2292 const webrtc::PacketTime webrtc_packet_time(packet_time.timestamp,
2293 packet_time.not_before);
2294 call_->Receiver()->DeliverPacket(webrtc::MediaType::AUDIO,
2295 reinterpret_cast<const uint8_t*>(packet->data()), packet->size(),
2296 webrtc_packet_time);
Fredrik Solenberg4b60c732015-05-07 14:07:48 +02002297
wu@webrtc.org9dba5252013-08-05 20:36:57 +00002298 // Sending channels need all RTCP packets with feedback information.
2299 // Even sender reports can contain attached report blocks.
2300 // Receiving channels need sender reports in order to create
2301 // correct receiver reports.
2302 int type = 0;
kwiberg@webrtc.orgeebcab52015-03-24 09:19:06 +00002303 if (!GetRtcpType(packet->data(), packet->size(), &type)) {
wu@webrtc.org9dba5252013-08-05 20:36:57 +00002304 LOG(LS_WARNING) << "Failed to parse type from received RTCP packet";
2305 return;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002306 }
2307
solenberg0b675462015-10-09 01:37:09 -07002308 // If it is a sender report, find the receive channel that is listening.
wu@webrtc.org9dba5252013-08-05 20:36:57 +00002309 if (type == kRtcpTypeSR) {
solenberg0b675462015-10-09 01:37:09 -07002310 uint32_t ssrc = 0;
2311 if (!GetRtcpSsrc(packet->data(), packet->size(), &ssrc)) {
2312 return;
2313 }
2314 int recv_channel_id = GetReceiveChannelId(ssrc);
2315 if (recv_channel_id != -1) {
wu@webrtc.org9dba5252013-08-05 20:36:57 +00002316 engine()->voe()->network()->ReceivedRTCPPacket(
solenberg0b675462015-10-09 01:37:09 -07002317 recv_channel_id, packet->data(), packet->size());
wu@webrtc.org9dba5252013-08-05 20:36:57 +00002318 }
2319 }
2320
2321 // SR may continue RR and any RR entry may correspond to any one of the send
2322 // channels. So all RTCP packets must be forwarded all send channels. VoE
2323 // will filter out RR internally.
solenbergc96df772015-10-21 13:01:53 -07002324 for (const auto& ch : send_streams_) {
wu@webrtc.org9dba5252013-08-05 20:36:57 +00002325 engine()->voe()->network()->ReceivedRTCPPacket(
Fredrik Solenbergaf9fb212015-08-26 10:45:53 +02002326 ch.second->channel(), packet->data(), packet->size());
wu@webrtc.org9dba5252013-08-05 20:36:57 +00002327 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002328}
2329
Peter Boström0c4e06b2015-10-07 12:23:21 +02002330bool WebRtcVoiceMediaChannel::MuteStream(uint32_t ssrc, bool muted) {
solenberg566ef242015-11-06 15:34:49 -08002331 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
solenberg0a617e22015-10-20 15:49:38 -07002332 int channel = GetSendChannelId(ssrc);
wu@webrtc.org9dba5252013-08-05 20:36:57 +00002333 if (channel == -1) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002334 LOG(LS_WARNING) << "The specified ssrc " << ssrc << " is not in use.";
2335 return false;
2336 }
wu@webrtc.org9dba5252013-08-05 20:36:57 +00002337 if (engine()->voe()->volume()->SetInputMute(channel, muted) == -1) {
2338 LOG_RTCERR2(SetInputMute, channel, muted);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002339 return false;
2340 }
buildbot@webrtc.org6b21b712014-07-31 15:08:53 +00002341 // We set the AGC to mute state only when all the channels are muted.
2342 // This implementation is not ideal, instead we should signal the AGC when
2343 // the mic channel is muted/unmuted. We can't do it today because there
2344 // is no good way to know which stream is mapping to the mic channel.
2345 bool all_muted = muted;
solenbergc96df772015-10-21 13:01:53 -07002346 for (const auto& ch : send_streams_) {
Fredrik Solenbergaf9fb212015-08-26 10:45:53 +02002347 if (!all_muted) {
2348 break;
2349 }
2350 if (engine()->voe()->volume()->GetInputMute(ch.second->channel(),
buildbot@webrtc.org6b21b712014-07-31 15:08:53 +00002351 all_muted)) {
Fredrik Solenbergaf9fb212015-08-26 10:45:53 +02002352 LOG_RTCERR1(GetInputMute, ch.second->channel());
buildbot@webrtc.org6b21b712014-07-31 15:08:53 +00002353 return false;
2354 }
2355 }
2356
2357 webrtc::AudioProcessing* ap = engine()->voe()->base()->audio_processing();
solenberg0a617e22015-10-20 15:49:38 -07002358 if (ap) {
buildbot@webrtc.org6b21b712014-07-31 15:08:53 +00002359 ap->set_output_will_be_muted(all_muted);
solenberg0a617e22015-10-20 15:49:38 -07002360 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002361 return true;
2362}
2363
minyue@webrtc.org26236952014-10-29 02:27:08 +00002364// TODO(minyue): SetMaxSendBandwidth() is subject to be renamed to
2365// SetMaxSendBitrate() in future.
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00002366bool WebRtcVoiceMediaChannel::SetMaxSendBandwidth(int bps) {
minyue@webrtc.org26236952014-10-29 02:27:08 +00002367 LOG(LS_INFO) << "WebRtcVoiceMediaChannel::SetMaxSendBandwidth.";
minyue@webrtc.org26236952014-10-29 02:27:08 +00002368 return SetSendBitrateInternal(bps);
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00002369}
2370
minyue@webrtc.org26236952014-10-29 02:27:08 +00002371bool WebRtcVoiceMediaChannel::SetSendBitrateInternal(int bps) {
2372 LOG(LS_INFO) << "WebRtcVoiceMediaChannel::SetSendBitrateInternal.";
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00002373
minyue@webrtc.org26236952014-10-29 02:27:08 +00002374 send_bitrate_setting_ = true;
2375 send_bitrate_bps_ = bps;
wu@webrtc.org1d1ffc92013-10-16 18:12:02 +00002376
solenberg72e29d22016-03-08 06:35:16 -08002377 if (!HasSendCodec()) {
wu@webrtc.org1d1ffc92013-10-16 18:12:02 +00002378 LOG(LS_INFO) << "The send codec has not been set up yet. "
minyue@webrtc.org26236952014-10-29 02:27:08 +00002379 << "The send bitrate setting will be applied later.";
wu@webrtc.org1d1ffc92013-10-16 18:12:02 +00002380 return true;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002381 }
2382
minyue@webrtc.org26236952014-10-29 02:27:08 +00002383 // Bitrate is auto by default.
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00002384 // TODO(bemasc): Fix this so that if SetMaxSendBandwidth(50) is followed by
2385 // SetMaxSendBandwith(0), the second call removes the previous limit.
2386 if (bps <= 0)
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002387 return true;
2388
solenberg72e29d22016-03-08 06:35:16 -08002389 webrtc::CodecInst codec = send_codec_spec_.codec_inst;
solenberg26c8c912015-11-27 04:00:25 -08002390 bool is_multi_rate = WebRtcVoiceCodecs::IsCodecMultiRate(codec);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002391
2392 if (is_multi_rate) {
2393 // If codec is multi-rate then just set the bitrate.
2394 codec.rate = bps;
solenbergc96df772015-10-21 13:01:53 -07002395 for (const auto& ch : send_streams_) {
solenberg0a617e22015-10-20 15:49:38 -07002396 if (!SetSendCodec(ch.second->channel(), codec)) {
2397 LOG(LS_INFO) << "Failed to set codec " << codec.plname
2398 << " to bitrate " << bps << " bps.";
2399 return false;
2400 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002401 }
2402 return true;
2403 } else {
2404 // If codec is not multi-rate and |bps| is less than the fixed bitrate
2405 // then fail. If codec is not multi-rate and |bps| exceeds or equal the
2406 // fixed bitrate then ignore.
2407 if (bps < codec.rate) {
2408 LOG(LS_INFO) << "Failed to set codec " << codec.plname
2409 << " to bitrate " << bps << " bps"
2410 << ", requires at least " << codec.rate << " bps.";
2411 return false;
2412 }
2413 return true;
2414 }
2415}
2416
2417bool WebRtcVoiceMediaChannel::GetStats(VoiceMediaInfo* info) {
Peter Boströmca8b4042016-03-08 14:24:13 -08002418 TRACE_EVENT0("webrtc", "WebRtcVoiceMediaChannel::GetStats");
solenberg566ef242015-11-06 15:34:49 -08002419 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
solenberg85a04962015-10-27 03:35:21 -07002420 RTC_DCHECK(info);
solenbergd97ec302015-10-07 01:40:33 -07002421
solenberg85a04962015-10-27 03:35:21 -07002422 // Get SSRC and stats for each sender.
2423 RTC_DCHECK(info->senders.size() == 0);
2424 for (const auto& stream : send_streams_) {
2425 webrtc::AudioSendStream::Stats stats = stream.second->GetStats();
wu@webrtc.org9dba5252013-08-05 20:36:57 +00002426 VoiceSenderInfo sinfo;
solenberg85a04962015-10-27 03:35:21 -07002427 sinfo.add_ssrc(stats.local_ssrc);
2428 sinfo.bytes_sent = stats.bytes_sent;
2429 sinfo.packets_sent = stats.packets_sent;
2430 sinfo.packets_lost = stats.packets_lost;
2431 sinfo.fraction_lost = stats.fraction_lost;
2432 sinfo.codec_name = stats.codec_name;
2433 sinfo.ext_seqnum = stats.ext_seqnum;
2434 sinfo.jitter_ms = stats.jitter_ms;
2435 sinfo.rtt_ms = stats.rtt_ms;
2436 sinfo.audio_level = stats.audio_level;
2437 sinfo.aec_quality_min = stats.aec_quality_min;
2438 sinfo.echo_delay_median_ms = stats.echo_delay_median_ms;
2439 sinfo.echo_delay_std_ms = stats.echo_delay_std_ms;
2440 sinfo.echo_return_loss = stats.echo_return_loss;
2441 sinfo.echo_return_loss_enhancement = stats.echo_return_loss_enhancement;
Taylor Brandstetter1a018dc2016-03-08 12:37:39 -08002442 sinfo.typing_noise_detected = (send_ ? stats.typing_noise_detected : false);
wu@webrtc.org9dba5252013-08-05 20:36:57 +00002443 info->senders.push_back(sinfo);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002444 }
2445
solenberg85a04962015-10-27 03:35:21 -07002446 // Get SSRC and stats for each receiver.
2447 RTC_DCHECK(info->receivers.size() == 0);
solenberg7add0582015-11-20 09:59:34 -08002448 for (const auto& stream : recv_streams_) {
Fredrik Solenberg4f4ec0a2015-10-22 10:49:27 +02002449 webrtc::AudioReceiveStream::Stats stats = stream.second->GetStats();
2450 VoiceReceiverInfo rinfo;
2451 rinfo.add_ssrc(stats.remote_ssrc);
2452 rinfo.bytes_rcvd = stats.bytes_rcvd;
2453 rinfo.packets_rcvd = stats.packets_rcvd;
2454 rinfo.packets_lost = stats.packets_lost;
2455 rinfo.fraction_lost = stats.fraction_lost;
2456 rinfo.codec_name = stats.codec_name;
2457 rinfo.ext_seqnum = stats.ext_seqnum;
2458 rinfo.jitter_ms = stats.jitter_ms;
2459 rinfo.jitter_buffer_ms = stats.jitter_buffer_ms;
2460 rinfo.jitter_buffer_preferred_ms = stats.jitter_buffer_preferred_ms;
2461 rinfo.delay_estimate_ms = stats.delay_estimate_ms;
2462 rinfo.audio_level = stats.audio_level;
2463 rinfo.expand_rate = stats.expand_rate;
2464 rinfo.speech_expand_rate = stats.speech_expand_rate;
2465 rinfo.secondary_decoded_rate = stats.secondary_decoded_rate;
2466 rinfo.accelerate_rate = stats.accelerate_rate;
2467 rinfo.preemptive_expand_rate = stats.preemptive_expand_rate;
2468 rinfo.decoding_calls_to_silence_generator =
2469 stats.decoding_calls_to_silence_generator;
2470 rinfo.decoding_calls_to_neteq = stats.decoding_calls_to_neteq;
2471 rinfo.decoding_normal = stats.decoding_normal;
2472 rinfo.decoding_plc = stats.decoding_plc;
2473 rinfo.decoding_cng = stats.decoding_cng;
2474 rinfo.decoding_plc_cng = stats.decoding_plc_cng;
2475 rinfo.capture_start_ntp_time_ms = stats.capture_start_ntp_time_ms;
2476 info->receivers.push_back(rinfo);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002477 }
2478
2479 return true;
2480}
2481
Tommif888bb52015-12-12 01:37:01 +01002482void WebRtcVoiceMediaChannel::SetRawAudioSink(
2483 uint32_t ssrc,
kwiberg686a8ef2016-02-26 03:00:35 -08002484 std::unique_ptr<webrtc::AudioSinkInterface> sink) {
Tommif888bb52015-12-12 01:37:01 +01002485 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
deadbeef884f5852016-01-15 09:20:04 -08002486 LOG(LS_VERBOSE) << "WebRtcVoiceMediaChannel::SetRawAudioSink: ssrc:" << ssrc
2487 << " " << (sink ? "(ptr)" : "NULL");
2488 if (ssrc == 0) {
2489 if (default_recv_ssrc_ != -1) {
kwiberg686a8ef2016-02-26 03:00:35 -08002490 std::unique_ptr<webrtc::AudioSinkInterface> proxy_sink(
deadbeef884f5852016-01-15 09:20:04 -08002491 sink ? new ProxySink(sink.get()) : nullptr);
2492 SetRawAudioSink(default_recv_ssrc_, std::move(proxy_sink));
2493 }
2494 default_sink_ = std::move(sink);
2495 return;
2496 }
Tommif888bb52015-12-12 01:37:01 +01002497 const auto it = recv_streams_.find(ssrc);
2498 if (it == recv_streams_.end()) {
2499 LOG(LS_WARNING) << "SetRawAudioSink: no recv stream" << ssrc;
2500 return;
2501 }
deadbeef2d110be2016-01-13 12:00:26 -08002502 it->second->SetRawAudioSink(std::move(sink));
Tommif888bb52015-12-12 01:37:01 +01002503}
2504
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002505int WebRtcVoiceMediaChannel::GetOutputLevel(int channel) {
solenbergd97ec302015-10-07 01:40:33 -07002506 unsigned int ulevel = 0;
2507 int ret = engine()->voe()->volume()->GetSpeechOutputLevel(channel, ulevel);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002508 return (ret == 0) ? static_cast<int>(ulevel) : -1;
2509}
2510
Peter Boström0c4e06b2015-10-07 12:23:21 +02002511int WebRtcVoiceMediaChannel::GetReceiveChannelId(uint32_t ssrc) const {
solenberg566ef242015-11-06 15:34:49 -08002512 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
solenberg7add0582015-11-20 09:59:34 -08002513 const auto it = recv_streams_.find(ssrc);
2514 if (it != recv_streams_.end()) {
mallinath@webrtc.org67ee6b92014-02-03 16:57:16 +00002515 return it->second->channel();
solenberg8fb30c32015-10-13 03:06:58 -07002516 }
solenberg1ac56142015-10-13 03:58:19 -07002517 return -1;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002518}
2519
Peter Boström0c4e06b2015-10-07 12:23:21 +02002520int WebRtcVoiceMediaChannel::GetSendChannelId(uint32_t ssrc) const {
solenberg566ef242015-11-06 15:34:49 -08002521 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
solenbergc96df772015-10-21 13:01:53 -07002522 const auto it = send_streams_.find(ssrc);
2523 if (it != send_streams_.end()) {
mallinath@webrtc.org67ee6b92014-02-03 16:57:16 +00002524 return it->second->channel();
solenberg8fb30c32015-10-13 03:06:58 -07002525 }
wu@webrtc.org9dba5252013-08-05 20:36:57 +00002526 return -1;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002527}
2528
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002529bool WebRtcVoiceMediaChannel::SetPlayout(int channel, bool playout) {
2530 if (playout) {
2531 LOG(LS_INFO) << "Starting playout for channel #" << channel;
2532 if (engine()->voe()->base()->StartPlayout(channel) == -1) {
2533 LOG_RTCERR1(StartPlayout, channel);
2534 return false;
2535 }
2536 } else {
2537 LOG(LS_INFO) << "Stopping playout for channel #" << channel;
2538 engine()->voe()->base()->StopPlayout(channel);
2539 }
2540 return true;
2541}
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002542} // namespace cricket
2543
2544#endif // HAVE_WEBRTC_VOICE