blob: 7b3e0ef8379194e0fb7d8017bf3592fdd4680952 [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
henrike@webrtc.org28e20752013-07-10 00:45:36 +000011#ifdef HAVE_WEBRTC_VOICE
12
kjellander@webrtc.org5ad12972016-02-12 06:39:40 +010013#include "webrtc/media/engine/webrtcvoiceengine.h"
henrike@webrtc.org28e20752013-07-10 00:45:36 +000014
15#include <algorithm>
16#include <cstdio>
17#include <string>
18#include <vector>
19
kjellander@webrtc.org7ffeab52016-02-26 22:46:09 +010020#include "webrtc/audio_sink.h"
tfarina5237aaf2015-11-10 23:44:30 -080021#include "webrtc/base/arraysize.h"
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +000022#include "webrtc/base/base64.h"
23#include "webrtc/base/byteorder.h"
24#include "webrtc/base/common.h"
kwiberg4485ffb2016-04-26 08:14:39 -070025#include "webrtc/base/constructormagic.h"
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +000026#include "webrtc/base/helpers.h"
27#include "webrtc/base/logging.h"
28#include "webrtc/base/stringencode.h"
29#include "webrtc/base/stringutils.h"
Peter Boströmca8b4042016-03-08 14:24:13 -080030#include "webrtc/base/trace_event.h"
ivoc112a3d82015-10-16 02:22:18 -070031#include "webrtc/call/rtc_event_log.h"
mallinath@webrtc.orga27be8e2013-09-27 23:04:10 +000032#include "webrtc/common.h"
Taylor Brandstetter1a018dc2016-03-08 12:37:39 -080033#include "webrtc/media/base/audiosource.h"
kjellanderf4752772016-03-02 05:42:30 -080034#include "webrtc/media/base/mediaconstants.h"
kjellandera96e2d72016-02-04 23:52:28 -080035#include "webrtc/media/base/streamparams.h"
kjellander@webrtc.org5ad12972016-02-12 06:39:40 +010036#include "webrtc/media/engine/webrtcmediaengine.h"
37#include "webrtc/media/engine/webrtcvoe.h"
solenberg26c8c912015-11-27 04:00:25 -080038#include "webrtc/modules/audio_coding/acm2/rent_a_codec.h"
henrike@webrtc.org28e20752013-07-10 00:45:36 +000039#include "webrtc/modules/audio_processing/include/audio_processing.h"
Henrik Kjellander98f53512015-10-28 18:17:40 +010040#include "webrtc/system_wrappers/include/field_trial.h"
solenbergbd138382015-11-20 16:08:07 -080041#include "webrtc/system_wrappers/include/trace.h"
henrike@webrtc.org28e20752013-07-10 00:45:36 +000042
henrike@webrtc.org28e20752013-07-10 00:45:36 +000043namespace cricket {
solenbergd97ec302015-10-07 01:40:33 -070044namespace {
henrike@webrtc.org28e20752013-07-10 00:45:36 +000045
solenbergbd138382015-11-20 16:08:07 -080046const int kDefaultTraceFilter = webrtc::kTraceNone | webrtc::kTraceTerseInfo |
47 webrtc::kTraceWarning | webrtc::kTraceError |
48 webrtc::kTraceCritical;
49const int kElevatedTraceFilter = kDefaultTraceFilter | webrtc::kTraceStateInfo |
50 webrtc::kTraceInfo;
51
henrike@webrtc.org28e20752013-07-10 00:45:36 +000052// On Windows Vista and newer, Microsoft introduced the concept of "Default
53// Communications Device". This means that there are two types of default
54// devices (old Wave Audio style default and Default Communications Device).
55//
56// On Windows systems which only support Wave Audio style default, uses either
57// -1 or 0 to select the default device.
henrike@webrtc.org28e20752013-07-10 00:45:36 +000058#ifdef WIN32
solenbergd97ec302015-10-07 01:40:33 -070059const int kDefaultAudioDeviceId = -1;
solenberg8ad582d2016-03-16 09:34:56 -070060#elif !defined(WEBRTC_IOS)
solenbergd97ec302015-10-07 01:40:33 -070061const int kDefaultAudioDeviceId = 0;
henrike@webrtc.org28e20752013-07-10 00:45:36 +000062#endif
63
henrike@webrtc.org28e20752013-07-10 00:45:36 +000064// Parameter used for NACK.
65// This value is equivalent to 5 seconds of audio data at 20 ms per packet.
solenbergd97ec302015-10-07 01:40:33 -070066const int kNackMaxPackets = 250;
solenberg971cab02016-06-14 10:02:41 -070067constexpr int kNackRtpHistoryMs = 5000;
minyue@webrtc.org2dc6f312014-10-31 05:33:10 +000068
69// Codec parameters for Opus.
henrike@webrtc.org1e09a712013-07-26 19:17:59 +000070// draft-spittka-payload-rtp-opus-03
minyue@webrtc.org2dc6f312014-10-31 05:33:10 +000071
72// Recommended bitrates:
73// 8-12 kb/s for NB speech,
74// 16-20 kb/s for WB speech,
75// 28-40 kb/s for FB speech,
76// 48-64 kb/s for FB mono music, and
77// 64-128 kb/s for FB stereo music.
78// The current implementation applies the following values to mono signals,
79// and multiplies them by 2 for stereo.
solenbergd97ec302015-10-07 01:40:33 -070080const int kOpusBitrateNb = 12000;
81const int kOpusBitrateWb = 20000;
82const int kOpusBitrateFb = 32000;
minyue@webrtc.org2dc6f312014-10-31 05:33:10 +000083
henrike@webrtc.org1e09a712013-07-26 19:17:59 +000084// Opus bitrate should be in the range between 6000 and 510000.
solenbergd97ec302015-10-07 01:40:33 -070085const int kOpusMinBitrate = 6000;
86const int kOpusMaxBitrate = 510000;
buildbot@webrtc.org5d639b32014-09-10 07:57:12 +000087
deadbeef80346142016-04-27 14:17:10 -070088// iSAC bitrate should be <= 56000.
89const int kIsacMaxBitrate = 56000;
90
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
solenberg31642aa2016-03-14 08:00:37 -0700102const int kMinPayloadType = 0;
103const int kMaxPayloadType = 127;
104
deadbeef884f5852016-01-15 09:20:04 -0800105class ProxySink : public webrtc::AudioSinkInterface {
106 public:
107 ProxySink(AudioSinkInterface* sink) : sink_(sink) { RTC_DCHECK(sink); }
108
109 void OnData(const Data& audio) override { sink_->OnData(audio); }
110
111 private:
112 webrtc::AudioSinkInterface* sink_;
113};
114
solenberg0b675462015-10-09 01:37:09 -0700115bool ValidateStreamParams(const StreamParams& sp) {
116 if (sp.ssrcs.empty()) {
117 LOG(LS_ERROR) << "No SSRCs in stream parameters: " << sp.ToString();
118 return false;
119 }
120 if (sp.ssrcs.size() > 1) {
121 LOG(LS_ERROR) << "Multiple SSRCs in stream parameters: " << sp.ToString();
122 return false;
123 }
124 return true;
125}
126
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000127// Dumps an AudioCodec in RFC 2327-ish format.
solenbergd97ec302015-10-07 01:40:33 -0700128std::string ToString(const AudioCodec& codec) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000129 std::stringstream ss;
130 ss << codec.name << "/" << codec.clockrate << "/" << codec.channels
131 << " (" << codec.id << ")";
132 return ss.str();
133}
Minyue Li7100dcd2015-03-27 05:05:59 +0100134
solenbergd97ec302015-10-07 01:40:33 -0700135std::string ToString(const webrtc::CodecInst& codec) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000136 std::stringstream ss;
137 ss << codec.plname << "/" << codec.plfreq << "/" << codec.channels
138 << " (" << codec.pltype << ")";
139 return ss.str();
140}
141
solenbergd97ec302015-10-07 01:40:33 -0700142bool IsCodec(const AudioCodec& codec, const char* ref_name) {
Minyue Li7100dcd2015-03-27 05:05:59 +0100143 return (_stricmp(codec.name.c_str(), ref_name) == 0);
144}
145
solenbergd97ec302015-10-07 01:40:33 -0700146bool IsCodec(const webrtc::CodecInst& codec, const char* ref_name) {
Minyue Li7100dcd2015-03-27 05:05:59 +0100147 return (_stricmp(codec.plname, ref_name) == 0);
148}
149
solenbergd97ec302015-10-07 01:40:33 -0700150bool FindCodec(const std::vector<AudioCodec>& codecs,
solenberg26c8c912015-11-27 04:00:25 -0800151 const AudioCodec& codec,
152 AudioCodec* found_codec) {
Fredrik Solenbergaf9fb212015-08-26 10:45:53 +0200153 for (const AudioCodec& c : codecs) {
154 if (c.Matches(codec)) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000155 if (found_codec != NULL) {
Fredrik Solenbergaf9fb212015-08-26 10:45:53 +0200156 *found_codec = c;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000157 }
158 return true;
159 }
160 }
161 return false;
162}
wu@webrtc.org1d1ffc92013-10-16 18:12:02 +0000163
solenberg0b675462015-10-09 01:37:09 -0700164bool VerifyUniquePayloadTypes(const std::vector<AudioCodec>& codecs) {
165 if (codecs.empty()) {
166 return true;
167 }
168 std::vector<int> payload_types;
169 for (const AudioCodec& codec : codecs) {
170 payload_types.push_back(codec.id);
171 }
172 std::sort(payload_types.begin(), payload_types.end());
173 auto it = std::unique(payload_types.begin(), payload_types.end());
174 return it == payload_types.end();
175}
176
Minyue Li7100dcd2015-03-27 05:05:59 +0100177// Return true if codec.params[feature] == "1", false otherwise.
solenberg26c8c912015-11-27 04:00:25 -0800178bool IsCodecFeatureEnabled(const AudioCodec& codec, const char* feature) {
Minyue Li7100dcd2015-03-27 05:05:59 +0100179 int value;
180 return codec.GetParam(feature, &value) && value == 1;
181}
182
183// Use params[kCodecParamMaxAverageBitrate] if it is defined, use codec.bitrate
184// otherwise. If the value (either from params or codec.bitrate) <=0, use the
185// default configuration. If the value is beyond feasible bit rate of Opus,
186// clamp it. Returns the Opus bit rate for operation.
solenbergd97ec302015-10-07 01:40:33 -0700187int GetOpusBitrate(const AudioCodec& codec, int max_playback_rate) {
Minyue Li7100dcd2015-03-27 05:05:59 +0100188 int bitrate = 0;
189 bool use_param = true;
190 if (!codec.GetParam(kCodecParamMaxAverageBitrate, &bitrate)) {
191 bitrate = codec.bitrate;
192 use_param = false;
193 }
194 if (bitrate <= 0) {
195 if (max_playback_rate <= 8000) {
196 bitrate = kOpusBitrateNb;
197 } else if (max_playback_rate <= 16000) {
198 bitrate = kOpusBitrateWb;
199 } else {
200 bitrate = kOpusBitrateFb;
201 }
202
203 if (IsCodecFeatureEnabled(codec, kCodecParamStereo)) {
204 bitrate *= 2;
205 }
206 } else if (bitrate < kOpusMinBitrate || bitrate > kOpusMaxBitrate) {
207 bitrate = (bitrate < kOpusMinBitrate) ? kOpusMinBitrate : kOpusMaxBitrate;
208 std::string rate_source =
209 use_param ? "Codec parameter \"maxaveragebitrate\"" :
210 "Supplied Opus bitrate";
211 LOG(LS_WARNING) << rate_source
212 << " is invalid and is replaced by: "
213 << bitrate;
214 }
215 return bitrate;
216}
217
218// Returns kOpusDefaultPlaybackRate if params[kCodecParamMaxPlaybackRate] is not
219// defined. Returns the value of params[kCodecParamMaxPlaybackRate] otherwise.
solenbergd97ec302015-10-07 01:40:33 -0700220int GetOpusMaxPlaybackRate(const AudioCodec& codec) {
Minyue Li7100dcd2015-03-27 05:05:59 +0100221 int value;
222 if (codec.GetParam(kCodecParamMaxPlaybackRate, &value)) {
223 return value;
224 }
225 return kOpusDefaultMaxPlaybackRate;
226}
227
solenbergd97ec302015-10-07 01:40:33 -0700228void GetOpusConfig(const AudioCodec& codec, webrtc::CodecInst* voe_codec,
Minyue Li7100dcd2015-03-27 05:05:59 +0100229 bool* enable_codec_fec, int* max_playback_rate,
230 bool* enable_codec_dtx) {
231 *enable_codec_fec = IsCodecFeatureEnabled(codec, kCodecParamUseInbandFec);
232 *enable_codec_dtx = IsCodecFeatureEnabled(codec, kCodecParamUseDtx);
233 *max_playback_rate = GetOpusMaxPlaybackRate(codec);
234
235 // If OPUS, change what we send according to the "stereo" codec
236 // parameter, and not the "channels" parameter. We set
237 // voe_codec.channels to 2 if "stereo=1" and 1 otherwise. If
238 // the bitrate is not specified, i.e. is <= zero, we set it to the
239 // appropriate default value for mono or stereo Opus.
240
241 voe_codec->channels = IsCodecFeatureEnabled(codec, kCodecParamStereo) ? 2 : 1;
242 voe_codec->rate = GetOpusBitrate(codec, *max_playback_rate);
243}
244
solenberg566ef242015-11-06 15:34:49 -0800245webrtc::AudioState::Config MakeAudioStateConfig(VoEWrapper* voe_wrapper) {
246 webrtc::AudioState::Config config;
247 config.voice_engine = voe_wrapper->engine();
248 return config;
249}
250
solenberg26c8c912015-11-27 04:00:25 -0800251class WebRtcVoiceCodecs final {
252 public:
253 // TODO(solenberg): Do this filtering once off-line, add a simple AudioCodec
254 // list and add a test which verifies VoE supports the listed codecs.
255 static std::vector<AudioCodec> SupportedCodecs() {
solenberg26c8c912015-11-27 04:00:25 -0800256 std::vector<AudioCodec> result;
deadbeef67cf2c12016-04-13 10:07:16 -0700257 // Iterate first over our preferred codecs list, so that the results are
258 // added in order of preference.
259 for (size_t i = 0; i < arraysize(kCodecPrefs); ++i) {
260 const CodecPref* pref = &kCodecPrefs[i];
261 for (webrtc::CodecInst voe_codec : webrtc::acm2::RentACodec::Database()) {
262 // Change the sample rate of G722 to 8000 to match SDP.
263 MaybeFixupG722(&voe_codec, 8000);
264 // Skip uncompressed formats.
265 if (IsCodec(voe_codec, kL16CodecName)) {
266 continue;
buildbot@webrtc.org13d67762014-05-02 17:33:29 +0000267 }
buildbot@webrtc.org13d67762014-05-02 17:33:29 +0000268
deadbeef67cf2c12016-04-13 10:07:16 -0700269 if (!IsCodec(voe_codec, pref->name) ||
270 pref->clockrate != voe_codec.plfreq ||
271 pref->channels != voe_codec.channels) {
272 // Not a match.
273 continue;
274 }
275
276 AudioCodec codec(pref->payload_type, voe_codec.plname, voe_codec.plfreq,
277 voe_codec.rate, voe_codec.channels);
278 LOG(LS_INFO) << "Adding supported codec: " << ToString(codec);
Minyue Li7100dcd2015-03-27 05:05:59 +0100279 if (IsCodec(codec, kIsacCodecName)) {
minyue@webrtc.org26236952014-10-29 02:27:08 +0000280 // Indicate auto-bitrate in signaling.
buildbot@webrtc.org13d67762014-05-02 17:33:29 +0000281 codec.bitrate = 0;
282 }
Minyue Li7100dcd2015-03-27 05:05:59 +0100283 if (IsCodec(codec, kOpusCodecName)) {
buildbot@webrtc.org13d67762014-05-02 17:33:29 +0000284 // Only add fmtp parameters that differ from the spec.
285 if (kPreferredMinPTime != kOpusDefaultMinPTime) {
286 codec.params[kCodecParamMinPTime] =
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000287 rtc::ToString(kPreferredMinPTime);
buildbot@webrtc.org13d67762014-05-02 17:33:29 +0000288 }
289 if (kPreferredMaxPTime != kOpusDefaultMaxPTime) {
290 codec.params[kCodecParamMaxPTime] =
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000291 rtc::ToString(kPreferredMaxPTime);
buildbot@webrtc.org13d67762014-05-02 17:33:29 +0000292 }
pkasting@chromium.orgd3245462015-02-23 21:28:22 +0000293 codec.SetParam(kCodecParamUseInbandFec, 1);
stefanba4c0e42016-02-04 04:12:24 -0800294 codec.AddFeedbackParam(
295 FeedbackParam(kRtcpFbParamTransportCc, kParamValueEmpty));
minyue@webrtc.org4ef22d12014-11-17 09:26:39 +0000296
297 // TODO(hellner): Add ptime, sprop-stereo, and stereo
buildbot@webrtc.org13d67762014-05-02 17:33:29 +0000298 // when they can be set to values other than the default.
299 }
solenberg26c8c912015-11-27 04:00:25 -0800300 result.push_back(codec);
buildbot@webrtc.org13d67762014-05-02 17:33:29 +0000301 }
302 }
solenberg26c8c912015-11-27 04:00:25 -0800303 return result;
buildbot@webrtc.org13d67762014-05-02 17:33:29 +0000304 }
buildbot@webrtc.org13d67762014-05-02 17:33:29 +0000305
solenberg26c8c912015-11-27 04:00:25 -0800306 static bool ToCodecInst(const AudioCodec& in,
307 webrtc::CodecInst* out) {
308 for (webrtc::CodecInst voe_codec : webrtc::acm2::RentACodec::Database()) {
309 // Change the sample rate of G722 to 8000 to match SDP.
310 MaybeFixupG722(&voe_codec, 8000);
311 AudioCodec codec(voe_codec.pltype, voe_codec.plname, voe_codec.plfreq,
deadbeef67cf2c12016-04-13 10:07:16 -0700312 voe_codec.rate, voe_codec.channels);
solenberg26c8c912015-11-27 04:00:25 -0800313 bool multi_rate = IsCodecMultiRate(voe_codec);
314 // Allow arbitrary rates for ISAC to be specified.
315 if (multi_rate) {
316 // Set codec.bitrate to 0 so the check for codec.Matches() passes.
317 codec.bitrate = 0;
318 }
319 if (codec.Matches(in)) {
320 if (out) {
321 // Fixup the payload type.
322 voe_codec.pltype = in.id;
323
324 // Set bitrate if specified.
325 if (multi_rate && in.bitrate != 0) {
326 voe_codec.rate = in.bitrate;
327 }
328
329 // Reset G722 sample rate to 16000 to match WebRTC.
330 MaybeFixupG722(&voe_codec, 16000);
331
332 // Apply codec-specific settings.
333 if (IsCodec(codec, kIsacCodecName)) {
334 // If ISAC and an explicit bitrate is not specified,
335 // enable auto bitrate adjustment.
336 voe_codec.rate = (in.bitrate > 0) ? in.bitrate : -1;
337 }
338 *out = voe_codec;
339 }
340 return true;
341 }
342 }
henrik.lundin@webrtc.org8038d422014-11-11 08:38:24 +0000343 return false;
henrik.lundin@webrtc.orgf85dbce2014-11-07 12:25:00 +0000344 }
solenberg26c8c912015-11-27 04:00:25 -0800345
346 static bool IsCodecMultiRate(const webrtc::CodecInst& codec) {
347 for (size_t i = 0; i < arraysize(kCodecPrefs); ++i) {
348 if (IsCodec(codec, kCodecPrefs[i].name) &&
349 kCodecPrefs[i].clockrate == codec.plfreq) {
350 return kCodecPrefs[i].is_multi_rate;
351 }
352 }
353 return false;
354 }
355
deadbeef80346142016-04-27 14:17:10 -0700356 static int MaxBitrateBps(const webrtc::CodecInst& codec) {
357 for (size_t i = 0; i < arraysize(kCodecPrefs); ++i) {
358 if (IsCodec(codec, kCodecPrefs[i].name) &&
359 kCodecPrefs[i].clockrate == codec.plfreq) {
360 return kCodecPrefs[i].max_bitrate_bps;
361 }
362 }
363 return 0;
364 }
365
solenberg26c8c912015-11-27 04:00:25 -0800366 // If the AudioCodec param kCodecParamPTime is set, then we will set it to
367 // codec pacsize if it's valid, or we will pick the next smallest value we
368 // support.
369 // TODO(Brave): Query supported packet sizes from ACM when the API is ready.
370 static bool SetPTimeAsPacketSize(webrtc::CodecInst* codec, int ptime_ms) {
371 for (const CodecPref& codec_pref : kCodecPrefs) {
372 if ((IsCodec(*codec, codec_pref.name) &&
373 codec_pref.clockrate == codec->plfreq) ||
374 IsCodec(*codec, kG722CodecName)) {
375 int packet_size_ms = SelectPacketSize(codec_pref, ptime_ms);
376 if (packet_size_ms) {
377 // Convert unit from milli-seconds to samples.
378 codec->pacsize = (codec->plfreq / 1000) * packet_size_ms;
379 return true;
380 }
381 }
382 }
383 return false;
384 }
385
stefanba4c0e42016-02-04 04:12:24 -0800386 static const AudioCodec* GetPreferredCodec(
387 const std::vector<AudioCodec>& codecs,
kwiberg68061362016-06-14 08:04:47 -0700388 webrtc::CodecInst* out) {
solenberg72e29d22016-03-08 06:35:16 -0800389 RTC_DCHECK(out);
stefanba4c0e42016-02-04 04:12:24 -0800390 // Select the preferred send codec (the first non-telephone-event/CN codec).
391 for (const AudioCodec& codec : codecs) {
stefanba4c0e42016-02-04 04:12:24 -0800392 if (IsCodec(codec, kDtmfCodecName) || IsCodec(codec, kCnCodecName)) {
393 // Skip telephone-event/CN codec, which will be handled later.
394 continue;
395 }
396
397 // We'll use the first codec in the list to actually send audio data.
398 // Be sure to use the payload type requested by the remote side.
stefanba4c0e42016-02-04 04:12:24 -0800399 // Ignore codecs we don't know about. The negotiation step should prevent
400 // this, but double-check to be sure.
solenberg72e29d22016-03-08 06:35:16 -0800401 webrtc::CodecInst voe_codec = {0};
kwiberg68061362016-06-14 08:04:47 -0700402 if (!ToCodecInst(codec, &voe_codec)) {
403 LOG(LS_WARNING) << "Unknown codec " << ToString(codec);
stefanba4c0e42016-02-04 04:12:24 -0800404 continue;
405 }
solenberg72e29d22016-03-08 06:35:16 -0800406 *out = voe_codec;
kwiberg68061362016-06-14 08:04:47 -0700407 return &codec;
stefanba4c0e42016-02-04 04:12:24 -0800408 }
409 return nullptr;
410 }
411
solenberg26c8c912015-11-27 04:00:25 -0800412 private:
413 static const int kMaxNumPacketSize = 6;
414 struct CodecPref {
415 const char* name;
416 int clockrate;
Peter Kasting69558702016-01-12 16:26:35 -0800417 size_t channels;
solenberg26c8c912015-11-27 04:00:25 -0800418 int payload_type;
419 bool is_multi_rate;
420 int packet_sizes_ms[kMaxNumPacketSize];
deadbeef80346142016-04-27 14:17:10 -0700421 int max_bitrate_bps;
solenberg26c8c912015-11-27 04:00:25 -0800422 };
423 // Note: keep the supported packet sizes in ascending order.
kwiberg68061362016-06-14 08:04:47 -0700424 static const CodecPref kCodecPrefs[11];
solenberg26c8c912015-11-27 04:00:25 -0800425
426 static int SelectPacketSize(const CodecPref& codec_pref, int ptime_ms) {
427 int selected_packet_size_ms = codec_pref.packet_sizes_ms[0];
428 for (int packet_size_ms : codec_pref.packet_sizes_ms) {
429 if (packet_size_ms && packet_size_ms <= ptime_ms) {
430 selected_packet_size_ms = packet_size_ms;
431 }
432 }
433 return selected_packet_size_ms;
434 }
435
436 // Changes RTP timestamp rate of G722. This is due to the "bug" in the RFC
437 // which says that G722 should be advertised as 8 kHz although it is a 16 kHz
438 // codec.
439 static void MaybeFixupG722(webrtc::CodecInst* voe_codec, int new_plfreq) {
440 if (IsCodec(*voe_codec, kG722CodecName)) {
441 // If the ASSERT triggers, the codec definition in WebRTC VoiceEngine
442 // has changed, and this special case is no longer needed.
443 RTC_DCHECK(voe_codec->plfreq != new_plfreq);
444 voe_codec->plfreq = new_plfreq;
445 }
446 }
447};
448
kwiberg68061362016-06-14 08:04:47 -0700449const WebRtcVoiceCodecs::CodecPref WebRtcVoiceCodecs::kCodecPrefs[11] = {
deadbeef80346142016-04-27 14:17:10 -0700450 {kOpusCodecName, 48000, 2, 111, true, {10, 20, 40, 60}, kOpusMaxBitrate},
451 {kIsacCodecName, 16000, 1, 103, true, {30, 60}, kIsacMaxBitrate},
452 {kIsacCodecName, 32000, 1, 104, true, {30}, kIsacMaxBitrate},
453 // G722 should be advertised as 8000 Hz because of the RFC "bug".
454 {kG722CodecName, 8000, 1, 9, false, {10, 20, 30, 40, 50, 60}},
455 {kIlbcCodecName, 8000, 1, 102, false, {20, 30, 40, 60}},
456 {kPcmuCodecName, 8000, 1, 0, false, {10, 20, 30, 40, 50, 60}},
457 {kPcmaCodecName, 8000, 1, 8, false, {10, 20, 30, 40, 50, 60}},
458 {kCnCodecName, 32000, 1, 106, false, {}},
459 {kCnCodecName, 16000, 1, 105, false, {}},
460 {kCnCodecName, 8000, 1, 13, false, {}},
kwiberg68061362016-06-14 08:04:47 -0700461 {kDtmfCodecName, 8000, 1, 126, false, {}}
solenberg26c8c912015-11-27 04:00:25 -0800462};
463} // namespace {
464
solenberg971cab02016-06-14 10:02:41 -0700465bool SendCodecSpec::operator==(const SendCodecSpec& rhs) const {
466 if (nack_enabled != rhs.nack_enabled) {
467 return false;
468 }
469 if (transport_cc_enabled != rhs.transport_cc_enabled) {
470 return false;
471 }
472 if (enable_codec_fec != rhs.enable_codec_fec) {
473 return false;
474 }
475 if (enable_opus_dtx != rhs.enable_opus_dtx) {
476 return false;
477 }
478 if (opus_max_playback_rate != rhs.opus_max_playback_rate) {
479 return false;
480 }
481 if (red_payload_type != rhs.red_payload_type) {
482 return false;
483 }
484 if (cng_payload_type != rhs.cng_payload_type) {
485 return false;
486 }
487 if (cng_plfreq != rhs.cng_plfreq) {
488 return false;
489 }
490 if (codec_inst != rhs.codec_inst) {
491 return false;
492 }
493 return true;
494}
495
496bool SendCodecSpec::operator!=(const SendCodecSpec& rhs) const {
497 return !(*this == rhs);
498}
499
solenberg26c8c912015-11-27 04:00:25 -0800500bool WebRtcVoiceEngine::ToCodecInst(const AudioCodec& in,
501 webrtc::CodecInst* out) {
502 return WebRtcVoiceCodecs::ToCodecInst(in, out);
503}
504
ossu29b1a8d2016-06-13 07:34:51 -0700505WebRtcVoiceEngine::WebRtcVoiceEngine(
506 webrtc::AudioDeviceModule* adm,
507 const rtc::scoped_refptr<webrtc::AudioDecoderFactory>& decoder_factory)
508 : WebRtcVoiceEngine(adm, decoder_factory, new VoEWrapper()) {
solenbergff976312016-03-30 23:28:51 -0700509 audio_state_ = webrtc::AudioState::Create(MakeAudioStateConfig(voe()));
solenberg26c8c912015-11-27 04:00:25 -0800510}
511
ossu29b1a8d2016-06-13 07:34:51 -0700512WebRtcVoiceEngine::WebRtcVoiceEngine(
513 webrtc::AudioDeviceModule* adm,
514 const rtc::scoped_refptr<webrtc::AudioDecoderFactory>& decoder_factory,
515 VoEWrapper* voe_wrapper)
516 : adm_(adm), decoder_factory_(decoder_factory), voe_wrapper_(voe_wrapper) {
solenberg26c8c912015-11-27 04:00:25 -0800517 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
solenbergff976312016-03-30 23:28:51 -0700518 LOG(LS_INFO) << "WebRtcVoiceEngine::WebRtcVoiceEngine";
519 RTC_DCHECK(voe_wrapper);
solenberg26c8c912015-11-27 04:00:25 -0800520
521 signal_thread_checker_.DetachFromThread();
solenberg26c8c912015-11-27 04:00:25 -0800522
523 // Load our audio codec list.
solenbergff976312016-03-30 23:28:51 -0700524 LOG(LS_INFO) << "Supported codecs in order of preference:";
solenberg26c8c912015-11-27 04:00:25 -0800525 codecs_ = WebRtcVoiceCodecs::SupportedCodecs();
solenbergff976312016-03-30 23:28:51 -0700526 for (const AudioCodec& codec : codecs_) {
527 LOG(LS_INFO) << ToString(codec);
buildbot@webrtc.org13d67762014-05-02 17:33:29 +0000528 }
buildbot@webrtc.org13d67762014-05-02 17:33:29 +0000529
solenbergff976312016-03-30 23:28:51 -0700530 voe_config_.Set<webrtc::VoicePacing>(new webrtc::VoicePacing(true));
buildbot@webrtc.org13d67762014-05-02 17:33:29 +0000531
solenbergff976312016-03-30 23:28:51 -0700532 // Temporarily turn logging level up for the Init() call.
533 webrtc::Trace::SetTraceCallback(this);
solenbergbd138382015-11-20 16:08:07 -0800534 webrtc::Trace::set_level_filter(kElevatedTraceFilter);
solenberg2515af22015-12-02 06:19:36 -0800535 LOG(LS_INFO) << webrtc::VoiceEngine::GetVersionString();
ossu29b1a8d2016-06-13 07:34:51 -0700536 RTC_CHECK_EQ(0, voe_wrapper_->base()->Init(adm_.get(), nullptr,
537 decoder_factory_));
solenbergbd138382015-11-20 16:08:07 -0800538 webrtc::Trace::set_level_filter(kDefaultTraceFilter);
buildbot@webrtc.org13d67762014-05-02 17:33:29 +0000539
solenbergff976312016-03-30 23:28:51 -0700540 // No ADM supplied? Get the default one from VoE.
541 if (!adm_) {
542 adm_ = voe_wrapper_->base()->audio_device_module();
543 }
544 RTC_DCHECK(adm_);
545
buildbot@webrtc.org13d67762014-05-02 17:33:29 +0000546 // Save the default AGC configuration settings. This must happen before
solenberg246b8172015-12-08 09:50:23 -0800547 // calling ApplyOptions or the default will be overwritten.
solenbergff976312016-03-30 23:28:51 -0700548 int error = voe_wrapper_->processing()->GetAgcConfig(default_agc_config_);
549 RTC_DCHECK_EQ(0, error);
buildbot@webrtc.org13d67762014-05-02 17:33:29 +0000550
solenberg0f7d2932016-01-15 01:40:39 -0800551 // Set default engine options.
552 {
553 AudioOptions options;
554 options.echo_cancellation = rtc::Optional<bool>(true);
555 options.auto_gain_control = rtc::Optional<bool>(true);
556 options.noise_suppression = rtc::Optional<bool>(true);
557 options.highpass_filter = rtc::Optional<bool>(true);
558 options.stereo_swapping = rtc::Optional<bool>(false);
559 options.audio_jitter_buffer_max_packets = rtc::Optional<int>(50);
560 options.audio_jitter_buffer_fast_accelerate = rtc::Optional<bool>(false);
561 options.typing_detection = rtc::Optional<bool>(true);
562 options.adjust_agc_delta = rtc::Optional<int>(0);
563 options.experimental_agc = rtc::Optional<bool>(false);
564 options.extended_filter_aec = rtc::Optional<bool>(false);
565 options.delay_agnostic_aec = rtc::Optional<bool>(false);
566 options.experimental_ns = rtc::Optional<bool>(false);
Alejandro Luebsc9b0c262016-05-16 15:32:38 -0700567 options.intelligibility_enhancer = rtc::Optional<bool>(false);
solenbergff976312016-03-30 23:28:51 -0700568 bool error = ApplyOptions(options);
569 RTC_DCHECK(error);
buildbot@webrtc.org13d67762014-05-02 17:33:29 +0000570 }
571
solenberg246b8172015-12-08 09:50:23 -0800572 SetDefaultDevices();
buildbot@webrtc.org13d67762014-05-02 17:33:29 +0000573}
574
solenbergff976312016-03-30 23:28:51 -0700575WebRtcVoiceEngine::~WebRtcVoiceEngine() {
solenberg566ef242015-11-06 15:34:49 -0800576 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
solenbergff976312016-03-30 23:28:51 -0700577 LOG(LS_INFO) << "WebRtcVoiceEngine::~WebRtcVoiceEngine";
buildbot@webrtc.org13d67762014-05-02 17:33:29 +0000578 StopAecDump();
buildbot@webrtc.org13d67762014-05-02 17:33:29 +0000579 voe_wrapper_->base()->Terminate();
solenbergff976312016-03-30 23:28:51 -0700580 webrtc::Trace::SetTraceCallback(nullptr);
buildbot@webrtc.org13d67762014-05-02 17:33:29 +0000581}
582
solenberg566ef242015-11-06 15:34:49 -0800583rtc::scoped_refptr<webrtc::AudioState>
584 WebRtcVoiceEngine::GetAudioState() const {
585 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
586 return audio_state_;
587}
588
nisse51542be2016-02-12 02:27:06 -0800589VoiceMediaChannel* WebRtcVoiceEngine::CreateChannel(
590 webrtc::Call* call,
591 const MediaConfig& config,
Jelena Marusicc28a8962015-05-29 15:05:44 +0200592 const AudioOptions& options) {
solenberg566ef242015-11-06 15:34:49 -0800593 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
nisse51542be2016-02-12 02:27:06 -0800594 return new WebRtcVoiceMediaChannel(this, config, options, call);
buildbot@webrtc.org13d67762014-05-02 17:33:29 +0000595}
596
buildbot@webrtc.org13d67762014-05-02 17:33:29 +0000597bool WebRtcVoiceEngine::ApplyOptions(const AudioOptions& options_in) {
solenberg566ef242015-11-06 15:34:49 -0800598 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
solenbergff976312016-03-30 23:28:51 -0700599 LOG(LS_INFO) << "WebRtcVoiceEngine::ApplyOptions: " << options_in.ToString();
solenberg0f7d2932016-01-15 01:40:39 -0800600 AudioOptions options = options_in; // The options are modified below.
solenberg246b8172015-12-08 09:50:23 -0800601
buildbot@webrtc.org13d67762014-05-02 17:33:29 +0000602 // kEcConference is AEC with high suppression.
603 webrtc::EcModes ec_mode = webrtc::kEcConference;
604 webrtc::AecmModes aecm_mode = webrtc::kAecmSpeakerphone;
605 webrtc::AgcModes agc_mode = webrtc::kAgcAdaptiveAnalog;
606 webrtc::NsModes ns_mode = webrtc::kNsHighSuppression;
kwiberg102c6a62015-10-30 02:47:38 -0700607 if (options.aecm_generate_comfort_noise) {
buildbot@webrtc.org13d67762014-05-02 17:33:29 +0000608 LOG(LS_VERBOSE) << "Comfort noise explicitly set to "
kwiberg102c6a62015-10-30 02:47:38 -0700609 << *options.aecm_generate_comfort_noise
610 << " (default is false).";
buildbot@webrtc.org13d67762014-05-02 17:33:29 +0000611 }
612
kjellanderfcfc8042016-01-14 11:01:09 -0800613#if defined(WEBRTC_IOS)
buildbot@webrtc.org13d67762014-05-02 17:33:29 +0000614 // On iOS, VPIO provides built-in EC and AGC.
Karl Wibergbe579832015-11-10 22:34:18 +0100615 options.echo_cancellation = rtc::Optional<bool>(false);
616 options.auto_gain_control = rtc::Optional<bool>(false);
henrika86d907c2015-09-07 16:09:50 +0200617 LOG(LS_INFO) << "Always disable AEC and AGC on iOS. Use built-in instead.";
buildbot@webrtc.org13d67762014-05-02 17:33:29 +0000618#elif defined(ANDROID)
619 ec_mode = webrtc::kEcAecm;
620#endif
621
kjellanderfcfc8042016-01-14 11:01:09 -0800622#if defined(WEBRTC_IOS) || defined(ANDROID)
buildbot@webrtc.org13d67762014-05-02 17:33:29 +0000623 // Set the AGC mode for iOS as well despite disabling it above, to avoid
624 // unsupported configuration errors from webrtc.
625 agc_mode = webrtc::kAgcFixedDigital;
Karl Wibergbe579832015-11-10 22:34:18 +0100626 options.typing_detection = rtc::Optional<bool>(false);
627 options.experimental_agc = rtc::Optional<bool>(false);
628 options.extended_filter_aec = rtc::Optional<bool>(false);
629 options.experimental_ns = rtc::Optional<bool>(false);
buildbot@webrtc.org13d67762014-05-02 17:33:29 +0000630#endif
631
Bjorn Volckerbf395c12015-03-25 22:45:56 +0100632 // Delay Agnostic AEC automatically turns on EC if not set except on iOS
633 // where the feature is not supported.
634 bool use_delay_agnostic_aec = false;
kjellanderfcfc8042016-01-14 11:01:09 -0800635#if !defined(WEBRTC_IOS)
kwiberg102c6a62015-10-30 02:47:38 -0700636 if (options.delay_agnostic_aec) {
637 use_delay_agnostic_aec = *options.delay_agnostic_aec;
Bjorn Volckerbf395c12015-03-25 22:45:56 +0100638 if (use_delay_agnostic_aec) {
Karl Wibergbe579832015-11-10 22:34:18 +0100639 options.echo_cancellation = rtc::Optional<bool>(true);
640 options.extended_filter_aec = rtc::Optional<bool>(true);
Bjorn Volckerbf395c12015-03-25 22:45:56 +0100641 ec_mode = webrtc::kEcConference;
642 }
643 }
644#endif
645
buildbot@webrtc.org13d67762014-05-02 17:33:29 +0000646 webrtc::VoEAudioProcessing* voep = voe_wrapper_->processing();
647
kwiberg102c6a62015-10-30 02:47:38 -0700648 if (options.echo_cancellation) {
henrika@webrtc.orga954c072014-12-09 16:22:09 +0000649 // Check if platform supports built-in EC. Currently only supported on
650 // Android and in combination with Java based audio layer.
651 // TODO(henrika): investigate possibility to support built-in EC also
652 // in combination with Open SL ES audio.
solenberg5b5129a2016-04-08 05:35:48 -0700653 const bool built_in_aec = adm()->BuiltInAECIsAvailable();
Bjorn Volcker73f72102015-06-03 14:50:15 +0200654 if (built_in_aec) {
Bjorn Volckerccfc9392015-05-07 07:43:17 +0200655 // Built-in EC exists on this device and use_delay_agnostic_aec is not
656 // overriding it. Enable/Disable it according to the echo_cancellation
657 // audio option.
Bjorn Volcker73f72102015-06-03 14:50:15 +0200658 const bool enable_built_in_aec =
kwiberg102c6a62015-10-30 02:47:38 -0700659 *options.echo_cancellation && !use_delay_agnostic_aec;
solenberg5b5129a2016-04-08 05:35:48 -0700660 if (adm()->EnableBuiltInAEC(enable_built_in_aec) == 0 &&
Bjorn Volcker73f72102015-06-03 14:50:15 +0200661 enable_built_in_aec) {
Bjorn Volckerbf395c12015-03-25 22:45:56 +0100662 // Disable internal software EC if built-in EC is enabled,
henrika@webrtc.orga954c072014-12-09 16:22:09 +0000663 // i.e., replace the software EC with the built-in EC.
Karl Wibergbe579832015-11-10 22:34:18 +0100664 options.echo_cancellation = rtc::Optional<bool>(false);
henrika@webrtc.orga954c072014-12-09 16:22:09 +0000665 LOG(LS_INFO) << "Disabling EC since built-in EC will be used instead";
666 }
667 }
kwiberg102c6a62015-10-30 02:47:38 -0700668 if (voep->SetEcStatus(*options.echo_cancellation, ec_mode) == -1) {
669 LOG_RTCERR2(SetEcStatus, *options.echo_cancellation, ec_mode);
buildbot@webrtc.org13d67762014-05-02 17:33:29 +0000670 return false;
671 } else {
kwiberg102c6a62015-10-30 02:47:38 -0700672 LOG(LS_INFO) << "Echo control set to " << *options.echo_cancellation
henrika86d907c2015-09-07 16:09:50 +0200673 << " with mode " << ec_mode;
buildbot@webrtc.org13d67762014-05-02 17:33:29 +0000674 }
675#if !defined(ANDROID)
676 // TODO(ajm): Remove the error return on Android from webrtc.
kwiberg102c6a62015-10-30 02:47:38 -0700677 if (voep->SetEcMetricsStatus(*options.echo_cancellation) == -1) {
678 LOG_RTCERR1(SetEcMetricsStatus, *options.echo_cancellation);
buildbot@webrtc.org13d67762014-05-02 17:33:29 +0000679 return false;
680 }
681#endif
682 if (ec_mode == webrtc::kEcAecm) {
kwiberg102c6a62015-10-30 02:47:38 -0700683 bool cn = options.aecm_generate_comfort_noise.value_or(false);
684 if (voep->SetAecmMode(aecm_mode, cn) != 0) {
685 LOG_RTCERR2(SetAecmMode, aecm_mode, cn);
buildbot@webrtc.org13d67762014-05-02 17:33:29 +0000686 return false;
687 }
688 }
689 }
690
kwiberg102c6a62015-10-30 02:47:38 -0700691 if (options.auto_gain_control) {
solenberg5b5129a2016-04-08 05:35:48 -0700692 const bool built_in_agc = adm()->BuiltInAGCIsAvailable();
henrikac14f5ff2015-09-23 14:08:33 +0200693 if (built_in_agc) {
solenberg5b5129a2016-04-08 05:35:48 -0700694 if (adm()->EnableBuiltInAGC(*options.auto_gain_control) == 0 &&
kwiberg102c6a62015-10-30 02:47:38 -0700695 *options.auto_gain_control) {
henrikac14f5ff2015-09-23 14:08:33 +0200696 // Disable internal software AGC if built-in AGC is enabled,
697 // i.e., replace the software AGC with the built-in AGC.
Karl Wibergbe579832015-11-10 22:34:18 +0100698 options.auto_gain_control = rtc::Optional<bool>(false);
henrikac14f5ff2015-09-23 14:08:33 +0200699 LOG(LS_INFO) << "Disabling AGC since built-in AGC will be used instead";
700 }
701 }
kwiberg102c6a62015-10-30 02:47:38 -0700702 if (voep->SetAgcStatus(*options.auto_gain_control, agc_mode) == -1) {
703 LOG_RTCERR2(SetAgcStatus, *options.auto_gain_control, agc_mode);
buildbot@webrtc.org13d67762014-05-02 17:33:29 +0000704 return false;
705 } else {
kwiberg102c6a62015-10-30 02:47:38 -0700706 LOG(LS_INFO) << "Auto gain set to " << *options.auto_gain_control
707 << " with mode " << agc_mode;
buildbot@webrtc.org13d67762014-05-02 17:33:29 +0000708 }
709 }
710
kwiberg102c6a62015-10-30 02:47:38 -0700711 if (options.tx_agc_target_dbov || options.tx_agc_digital_compression_gain ||
712 options.tx_agc_limiter) {
buildbot@webrtc.org13d67762014-05-02 17:33:29 +0000713 // Override default_agc_config_. Generally, an unset option means "leave
714 // the VoE bits alone" in this function, so we want whatever is set to be
715 // stored as the new "default". If we didn't, then setting e.g.
716 // tx_agc_target_dbov would reset digital compression gain and limiter
717 // settings.
718 // Also, if we don't update default_agc_config_, then adjust_agc_delta
719 // would be an offset from the original values, and not whatever was set
720 // explicitly.
kwiberg102c6a62015-10-30 02:47:38 -0700721 default_agc_config_.targetLeveldBOv = options.tx_agc_target_dbov.value_or(
722 default_agc_config_.targetLeveldBOv);
buildbot@webrtc.org13d67762014-05-02 17:33:29 +0000723 default_agc_config_.digitalCompressionGaindB =
kwiberg102c6a62015-10-30 02:47:38 -0700724 options.tx_agc_digital_compression_gain.value_or(
buildbot@webrtc.org13d67762014-05-02 17:33:29 +0000725 default_agc_config_.digitalCompressionGaindB);
726 default_agc_config_.limiterEnable =
kwiberg102c6a62015-10-30 02:47:38 -0700727 options.tx_agc_limiter.value_or(default_agc_config_.limiterEnable);
buildbot@webrtc.org13d67762014-05-02 17:33:29 +0000728 if (voe_wrapper_->processing()->SetAgcConfig(default_agc_config_) == -1) {
729 LOG_RTCERR3(SetAgcConfig,
730 default_agc_config_.targetLeveldBOv,
731 default_agc_config_.digitalCompressionGaindB,
732 default_agc_config_.limiterEnable);
733 return false;
734 }
735 }
736
Alejandro Luebsc9b0c262016-05-16 15:32:38 -0700737 if (options.intelligibility_enhancer) {
738 intelligibility_enhancer_ = options.intelligibility_enhancer;
739 }
740 if (intelligibility_enhancer_ && *intelligibility_enhancer_) {
741 LOG(LS_INFO) << "Enabling NS when Intelligibility Enhancer is active.";
742 options.noise_suppression = intelligibility_enhancer_;
743 }
744
kwiberg102c6a62015-10-30 02:47:38 -0700745 if (options.noise_suppression) {
Alejandro Luebsc9b0c262016-05-16 15:32:38 -0700746 if (adm()->BuiltInNSIsAvailable()) {
747 bool builtin_ns =
748 *options.noise_suppression &&
749 !(intelligibility_enhancer_ && *intelligibility_enhancer_);
750 if (adm()->EnableBuiltInNS(builtin_ns) == 0 && builtin_ns) {
henrikac14f5ff2015-09-23 14:08:33 +0200751 // Disable internal software NS if built-in NS is enabled,
752 // i.e., replace the software NS with the built-in NS.
Karl Wibergbe579832015-11-10 22:34:18 +0100753 options.noise_suppression = rtc::Optional<bool>(false);
henrikac14f5ff2015-09-23 14:08:33 +0200754 LOG(LS_INFO) << "Disabling NS since built-in NS will be used instead";
755 }
756 }
kwiberg102c6a62015-10-30 02:47:38 -0700757 if (voep->SetNsStatus(*options.noise_suppression, ns_mode) == -1) {
758 LOG_RTCERR2(SetNsStatus, *options.noise_suppression, ns_mode);
buildbot@webrtc.org13d67762014-05-02 17:33:29 +0000759 return false;
760 } else {
kwiberg102c6a62015-10-30 02:47:38 -0700761 LOG(LS_INFO) << "Noise suppression set to " << *options.noise_suppression
henrikac14f5ff2015-09-23 14:08:33 +0200762 << " with mode " << ns_mode;
buildbot@webrtc.org13d67762014-05-02 17:33:29 +0000763 }
764 }
765
kwiberg102c6a62015-10-30 02:47:38 -0700766 if (options.highpass_filter) {
767 LOG(LS_INFO) << "High pass filter enabled? " << *options.highpass_filter;
768 if (voep->EnableHighPassFilter(*options.highpass_filter) == -1) {
769 LOG_RTCERR1(SetHighpassFilterStatus, *options.highpass_filter);
buildbot@webrtc.org13d67762014-05-02 17:33:29 +0000770 return false;
771 }
772 }
773
kwiberg102c6a62015-10-30 02:47:38 -0700774 if (options.stereo_swapping) {
775 LOG(LS_INFO) << "Stereo swapping enabled? " << *options.stereo_swapping;
776 voep->EnableStereoChannelSwapping(*options.stereo_swapping);
777 if (voep->IsStereoChannelSwappingEnabled() != *options.stereo_swapping) {
778 LOG_RTCERR1(EnableStereoChannelSwapping, *options.stereo_swapping);
buildbot@webrtc.org13d67762014-05-02 17:33:29 +0000779 return false;
780 }
781 }
782
kwiberg102c6a62015-10-30 02:47:38 -0700783 if (options.audio_jitter_buffer_max_packets) {
784 LOG(LS_INFO) << "NetEq capacity is "
785 << *options.audio_jitter_buffer_max_packets;
Henrik Lundin64dad832015-05-11 12:44:23 +0200786 voe_config_.Set<webrtc::NetEqCapacityConfig>(
kwiberg102c6a62015-10-30 02:47:38 -0700787 new webrtc::NetEqCapacityConfig(
788 *options.audio_jitter_buffer_max_packets));
Henrik Lundin64dad832015-05-11 12:44:23 +0200789 }
790
kwiberg102c6a62015-10-30 02:47:38 -0700791 if (options.audio_jitter_buffer_fast_accelerate) {
792 LOG(LS_INFO) << "NetEq fast mode? "
793 << *options.audio_jitter_buffer_fast_accelerate;
Henrik Lundin5263b3c2015-06-01 10:29:41 +0200794 voe_config_.Set<webrtc::NetEqFastAccelerate>(
kwiberg102c6a62015-10-30 02:47:38 -0700795 new webrtc::NetEqFastAccelerate(
796 *options.audio_jitter_buffer_fast_accelerate));
Henrik Lundin5263b3c2015-06-01 10:29:41 +0200797 }
798
kwiberg102c6a62015-10-30 02:47:38 -0700799 if (options.typing_detection) {
800 LOG(LS_INFO) << "Typing detection is enabled? "
801 << *options.typing_detection;
802 if (voep->SetTypingDetectionStatus(*options.typing_detection) == -1) {
buildbot@webrtc.org13d67762014-05-02 17:33:29 +0000803 // In case of error, log the info and continue
kwiberg102c6a62015-10-30 02:47:38 -0700804 LOG_RTCERR1(SetTypingDetectionStatus, *options.typing_detection);
buildbot@webrtc.org13d67762014-05-02 17:33:29 +0000805 }
806 }
807
kwiberg102c6a62015-10-30 02:47:38 -0700808 if (options.adjust_agc_delta) {
809 LOG(LS_INFO) << "Adjust agc delta is " << *options.adjust_agc_delta;
810 if (!AdjustAgcLevel(*options.adjust_agc_delta)) {
buildbot@webrtc.org13d67762014-05-02 17:33:29 +0000811 return false;
812 }
813 }
814
buildbot@webrtc.org1f8a2372014-08-28 10:52:44 +0000815 webrtc::Config config;
816
kwiberg102c6a62015-10-30 02:47:38 -0700817 if (options.delay_agnostic_aec)
818 delay_agnostic_aec_ = options.delay_agnostic_aec;
819 if (delay_agnostic_aec_) {
820 LOG(LS_INFO) << "Delay agnostic aec is enabled? " << *delay_agnostic_aec_;
henrik.lundin0f133b92015-07-02 00:17:55 -0700821 config.Set<webrtc::DelayAgnostic>(
kwiberg102c6a62015-10-30 02:47:38 -0700822 new webrtc::DelayAgnostic(*delay_agnostic_aec_));
Bjorn Volckerbf395c12015-03-25 22:45:56 +0100823 }
824
kwiberg102c6a62015-10-30 02:47:38 -0700825 if (options.extended_filter_aec) {
826 extended_filter_aec_ = options.extended_filter_aec;
827 }
828 if (extended_filter_aec_) {
829 LOG(LS_INFO) << "Extended filter aec is enabled? " << *extended_filter_aec_;
Henrik Lundin441f6342015-06-09 16:03:13 +0200830 config.Set<webrtc::ExtendedFilter>(
kwiberg102c6a62015-10-30 02:47:38 -0700831 new webrtc::ExtendedFilter(*extended_filter_aec_));
buildbot@webrtc.org1f8a2372014-08-28 10:52:44 +0000832 }
833
kwiberg102c6a62015-10-30 02:47:38 -0700834 if (options.experimental_ns) {
835 experimental_ns_ = options.experimental_ns;
836 }
837 if (experimental_ns_) {
838 LOG(LS_INFO) << "Experimental ns is enabled? " << *experimental_ns_;
buildbot@webrtc.org1f8a2372014-08-28 10:52:44 +0000839 config.Set<webrtc::ExperimentalNs>(
kwiberg102c6a62015-10-30 02:47:38 -0700840 new webrtc::ExperimentalNs(*experimental_ns_));
buildbot@webrtc.org1f8a2372014-08-28 10:52:44 +0000841 }
buildbot@webrtc.org1f8a2372014-08-28 10:52:44 +0000842
Alejandro Luebsc9b0c262016-05-16 15:32:38 -0700843 if (intelligibility_enhancer_) {
844 LOG(LS_INFO) << "Intelligibility Enhancer is enabled? "
845 << *intelligibility_enhancer_;
846 config.Set<webrtc::Intelligibility>(
847 new webrtc::Intelligibility(*intelligibility_enhancer_));
848 }
849
buildbot@webrtc.org1f8a2372014-08-28 10:52:44 +0000850 // We check audioproc for the benefit of tests, since FakeWebRtcVoiceEngine
851 // returns NULL on audio_processing().
852 webrtc::AudioProcessing* audioproc = voe_wrapper_->base()->audio_processing();
853 if (audioproc) {
854 audioproc->SetExtraOptions(config);
855 }
856
kwiberg102c6a62015-10-30 02:47:38 -0700857 if (options.recording_sample_rate) {
858 LOG(LS_INFO) << "Recording sample rate is "
859 << *options.recording_sample_rate;
solenberg5b5129a2016-04-08 05:35:48 -0700860 if (adm()->SetRecordingSampleRate(*options.recording_sample_rate)) {
kwiberg102c6a62015-10-30 02:47:38 -0700861 LOG_RTCERR1(SetRecordingSampleRate, *options.recording_sample_rate);
buildbot@webrtc.org13d67762014-05-02 17:33:29 +0000862 }
863 }
864
kwiberg102c6a62015-10-30 02:47:38 -0700865 if (options.playout_sample_rate) {
866 LOG(LS_INFO) << "Playout sample rate is " << *options.playout_sample_rate;
solenberg5b5129a2016-04-08 05:35:48 -0700867 if (adm()->SetPlayoutSampleRate(*options.playout_sample_rate)) {
kwiberg102c6a62015-10-30 02:47:38 -0700868 LOG_RTCERR1(SetPlayoutSampleRate, *options.playout_sample_rate);
buildbot@webrtc.org13d67762014-05-02 17:33:29 +0000869 }
870 }
871
872 return true;
873}
874
solenberg246b8172015-12-08 09:50:23 -0800875void WebRtcVoiceEngine::SetDefaultDevices() {
solenberg566ef242015-11-06 15:34:49 -0800876 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
kjellanderfcfc8042016-01-14 11:01:09 -0800877#if !defined(WEBRTC_IOS)
solenberg246b8172015-12-08 09:50:23 -0800878 int in_id = kDefaultAudioDeviceId;
879 int out_id = kDefaultAudioDeviceId;
880 LOG(LS_INFO) << "Setting microphone to (id=" << in_id
881 << ") and speaker to (id=" << out_id << ")";
buildbot@webrtc.org13d67762014-05-02 17:33:29 +0000882
solenbergc1a1b352015-09-22 13:31:20 -0700883 bool ret = true;
solenberg246b8172015-12-08 09:50:23 -0800884 if (voe_wrapper_->hw()->SetRecordingDevice(in_id) == -1) {
885 LOG_RTCERR1(SetRecordingDevice, in_id);
buildbot@webrtc.org13d67762014-05-02 17:33:29 +0000886 ret = false;
887 }
solenberg246b8172015-12-08 09:50:23 -0800888 webrtc::AudioProcessing* ap = voe()->base()->audio_processing();
889 if (ap) {
890 ap->Initialize();
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000891 }
892
solenberg246b8172015-12-08 09:50:23 -0800893 if (voe_wrapper_->hw()->SetPlayoutDevice(out_id) == -1) {
894 LOG_RTCERR1(SetPlayoutDevice, out_id);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000895 ret = false;
896 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000897
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000898 if (ret) {
solenberg246b8172015-12-08 09:50:23 -0800899 LOG(LS_INFO) << "Set microphone to (id=" << in_id
900 << ") and speaker to (id=" << out_id << ")";
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000901 }
kjellanderfcfc8042016-01-14 11:01:09 -0800902#endif // !WEBRTC_IOS
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000903}
904
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000905int WebRtcVoiceEngine::GetInputLevel() {
solenberg566ef242015-11-06 15:34:49 -0800906 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000907 unsigned int ulevel;
908 return (voe_wrapper_->volume()->GetSpeechInputLevel(ulevel) != -1) ?
909 static_cast<int>(ulevel) : -1;
910}
911
ossudedfd282016-06-14 07:12:39 -0700912const std::vector<AudioCodec>& WebRtcVoiceEngine::send_codecs() const {
913 RTC_DCHECK(signal_thread_checker_.CalledOnValidThread());
914 return codecs_;
915}
916
917const std::vector<AudioCodec>& WebRtcVoiceEngine::recv_codecs() const {
solenberg566ef242015-11-06 15:34:49 -0800918 RTC_DCHECK(signal_thread_checker_.CalledOnValidThread());
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000919 return codecs_;
920}
921
Stefan Holmer9d69c3f2015-12-07 10:45:43 +0100922RtpCapabilities WebRtcVoiceEngine::GetCapabilities() const {
solenberg566ef242015-11-06 15:34:49 -0800923 RTC_DCHECK(signal_thread_checker_.CalledOnValidThread());
Stefan Holmer9d69c3f2015-12-07 10:45:43 +0100924 RtpCapabilities capabilities;
Stefan Holmer9d69c3f2015-12-07 10:45:43 +0100925 capabilities.header_extensions.push_back(
isheriff6f8d6862016-05-26 11:24:55 -0700926 webrtc::RtpExtension(webrtc::RtpExtension::kAudioLevelUri,
927 webrtc::RtpExtension::kAudioLevelDefaultId));
928 capabilities.header_extensions.push_back(
929 webrtc::RtpExtension(webrtc::RtpExtension::kAbsSendTimeUri,
930 webrtc::RtpExtension::kAbsSendTimeDefaultId));
stefanba4c0e42016-02-04 04:12:24 -0800931 if (webrtc::field_trial::FindFullName("WebRTC-Audio-SendSideBwe") ==
932 "Enabled") {
isheriff6f8d6862016-05-26 11:24:55 -0700933 capabilities.header_extensions.push_back(webrtc::RtpExtension(
934 webrtc::RtpExtension::kTransportSequenceNumberUri,
935 webrtc::RtpExtension::kTransportSequenceNumberDefaultId));
stefanba4c0e42016-02-04 04:12:24 -0800936 }
Stefan Holmer9d69c3f2015-12-07 10:45:43 +0100937 return capabilities;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000938}
939
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000940int WebRtcVoiceEngine::GetLastEngineError() {
solenberg566ef242015-11-06 15:34:49 -0800941 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000942 return voe_wrapper_->error();
943}
944
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000945void WebRtcVoiceEngine::Print(webrtc::TraceLevel level, const char* trace,
946 int length) {
solenberg566ef242015-11-06 15:34:49 -0800947 // Note: This callback can happen on any thread!
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000948 rtc::LoggingSeverity sev = rtc::LS_VERBOSE;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000949 if (level == webrtc::kTraceError || level == webrtc::kTraceCritical)
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000950 sev = rtc::LS_ERROR;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000951 else if (level == webrtc::kTraceWarning)
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000952 sev = rtc::LS_WARNING;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000953 else if (level == webrtc::kTraceStateInfo || level == webrtc::kTraceInfo)
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000954 sev = rtc::LS_INFO;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000955 else if (level == webrtc::kTraceTerseInfo)
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +0000956 sev = rtc::LS_INFO;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000957
solenberg72e29d22016-03-08 06:35:16 -0800958 // Skip past boilerplate prefix text.
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000959 if (length < 72) {
960 std::string msg(trace, length);
961 LOG(LS_ERROR) << "Malformed webrtc log message: ";
962 LOG_V(sev) << msg;
963 } else {
964 std::string msg(trace + 71, length - 72);
Peter Boströmd5c75b12015-09-23 13:24:32 +0200965 LOG_V(sev) << "webrtc: " << msg;
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000966 }
967}
968
solenberg63b34542015-09-29 06:06:31 -0700969void WebRtcVoiceEngine::RegisterChannel(WebRtcVoiceMediaChannel* channel) {
solenberg566ef242015-11-06 15:34:49 -0800970 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
971 RTC_DCHECK(channel);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000972 channels_.push_back(channel);
973}
974
solenberg63b34542015-09-29 06:06:31 -0700975void WebRtcVoiceEngine::UnregisterChannel(WebRtcVoiceMediaChannel* channel) {
solenberg566ef242015-11-06 15:34:49 -0800976 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
solenberg63b34542015-09-29 06:06:31 -0700977 auto it = std::find(channels_.begin(), channels_.end(), channel);
solenberg566ef242015-11-06 15:34:49 -0800978 RTC_DCHECK(it != channels_.end());
979 channels_.erase(it);
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000980}
981
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000982// Adjusts the default AGC target level by the specified delta.
983// NB: If we start messing with other config fields, we'll want
984// to save the current webrtc::AgcConfig as well.
985bool WebRtcVoiceEngine::AdjustAgcLevel(int delta) {
solenberg566ef242015-11-06 15:34:49 -0800986 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
henrike@webrtc.org28e20752013-07-10 00:45:36 +0000987 webrtc::AgcConfig config = default_agc_config_;
988 config.targetLeveldBOv -= delta;
989
990 LOG(LS_INFO) << "Adjusting AGC level from default -"
991 << default_agc_config_.targetLeveldBOv << "dB to -"
992 << config.targetLeveldBOv << "dB";
993
994 if (voe_wrapper_->processing()->SetAgcConfig(config) == -1) {
995 LOG_RTCERR1(SetAgcConfig, config.targetLeveldBOv);
996 return false;
997 }
998 return true;
999}
1000
ivocd66b44d2016-01-15 03:06:36 -08001001bool WebRtcVoiceEngine::StartAecDump(rtc::PlatformFile file,
1002 int64_t max_size_bytes) {
solenberg566ef242015-11-06 15:34:49 -08001003 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00001004 FILE* aec_dump_file_stream = rtc::FdopenPlatformFileForWriting(file);
wu@webrtc.orga8910d22014-01-23 22:12:45 +00001005 if (!aec_dump_file_stream) {
1006 LOG(LS_ERROR) << "Could not open AEC dump file stream.";
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +00001007 if (!rtc::ClosePlatformFile(file))
wu@webrtc.orga8910d22014-01-23 22:12:45 +00001008 LOG(LS_WARNING) << "Could not close file.";
1009 return false;
1010 }
wu@webrtc.orga9890802013-12-13 00:21:03 +00001011 StopAecDump();
ivocd66b44d2016-01-15 03:06:36 -08001012 if (voe_wrapper_->base()->audio_processing()->StartDebugRecording(
1013 aec_dump_file_stream, max_size_bytes) !=
wu@webrtc.orga9890802013-12-13 00:21:03 +00001014 webrtc::AudioProcessing::kNoError) {
wu@webrtc.orga8910d22014-01-23 22:12:45 +00001015 LOG_RTCERR0(StartDebugRecording);
1016 fclose(aec_dump_file_stream);
wu@webrtc.orga9890802013-12-13 00:21:03 +00001017 return false;
1018 }
1019 is_dumping_aec_ = true;
1020 return true;
wu@webrtc.orga9890802013-12-13 00:21:03 +00001021}
1022
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001023void WebRtcVoiceEngine::StartAecDump(const std::string& filename) {
solenberg566ef242015-11-06 15:34:49 -08001024 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001025 if (!is_dumping_aec_) {
1026 // Start dumping AEC when we are not dumping.
ivocd66b44d2016-01-15 03:06:36 -08001027 if (voe_wrapper_->base()->audio_processing()->StartDebugRecording(
1028 filename.c_str(), -1) != webrtc::AudioProcessing::kNoError) {
wu@webrtc.orga9890802013-12-13 00:21:03 +00001029 LOG_RTCERR1(StartDebugRecording, filename.c_str());
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001030 } else {
1031 is_dumping_aec_ = true;
1032 }
1033 }
1034}
1035
1036void WebRtcVoiceEngine::StopAecDump() {
solenberg566ef242015-11-06 15:34:49 -08001037 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001038 if (is_dumping_aec_) {
1039 // Stop dumping AEC when we are dumping.
ivocd66b44d2016-01-15 03:06:36 -08001040 if (voe_wrapper_->base()->audio_processing()->StopDebugRecording() !=
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001041 webrtc::AudioProcessing::kNoError) {
1042 LOG_RTCERR0(StopDebugRecording);
1043 }
1044 is_dumping_aec_ = false;
1045 }
1046}
1047
ivocc1513ee2016-05-13 08:30:39 -07001048bool WebRtcVoiceEngine::StartRtcEventLog(rtc::PlatformFile file,
1049 int64_t max_size_bytes) {
solenberg566ef242015-11-06 15:34:49 -08001050 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
ivoc20834ca2016-02-04 06:33:37 -08001051 webrtc::RtcEventLog* event_log = voe_wrapper_->codec()->GetEventLog();
1052 if (event_log) {
ivocc1513ee2016-05-13 08:30:39 -07001053 return event_log->StartLogging(file, max_size_bytes);
ivoc20834ca2016-02-04 06:33:37 -08001054 }
1055 LOG_RTCERR0(StartRtcEventLog);
1056 return false;
ivoc112a3d82015-10-16 02:22:18 -07001057}
1058
1059void WebRtcVoiceEngine::StopRtcEventLog() {
solenberg566ef242015-11-06 15:34:49 -08001060 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
ivoc20834ca2016-02-04 06:33:37 -08001061 webrtc::RtcEventLog* event_log = voe_wrapper_->codec()->GetEventLog();
1062 if (event_log) {
1063 event_log->StopLogging();
1064 return;
1065 }
1066 LOG_RTCERR0(StopRtcEventLog);
ivoc112a3d82015-10-16 02:22:18 -07001067}
1068
solenberg0a617e22015-10-20 15:49:38 -07001069int WebRtcVoiceEngine::CreateVoEChannel() {
solenberg566ef242015-11-06 15:34:49 -08001070 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
solenberg0a617e22015-10-20 15:49:38 -07001071 return voe_wrapper_->base()->CreateChannel(voe_config_);
sergeyu@chromium.org5bc25c42013-12-05 00:24:06 +00001072}
1073
solenberg5b5129a2016-04-08 05:35:48 -07001074webrtc::AudioDeviceModule* WebRtcVoiceEngine::adm() {
1075 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
1076 RTC_DCHECK(adm_);
1077 return adm_;
1078}
1079
solenbergc96df772015-10-21 13:01:53 -07001080class WebRtcVoiceMediaChannel::WebRtcAudioSendStream
Taylor Brandstetter1a018dc2016-03-08 12:37:39 -08001081 : public AudioSource::Sink {
mallinath@webrtc.org67ee6b92014-02-03 16:57:16 +00001082 public:
skvlade0d46372016-04-07 22:59:22 -07001083 WebRtcAudioSendStream(int ch,
1084 webrtc::AudioTransport* voe_audio_transport,
1085 uint32_t ssrc,
1086 const std::string& c_name,
solenberg971cab02016-06-14 10:02:41 -07001087 const SendCodecSpec& send_codec_spec,
solenberg3a941542015-11-16 07:34:50 -08001088 const std::vector<webrtc::RtpExtension>& extensions,
mflodman3d7db262016-04-29 00:57:13 -07001089 webrtc::Call* call,
1090 webrtc::Transport* send_transport)
solenberg7add0582015-11-20 09:59:34 -08001091 : voe_audio_transport_(voe_audio_transport),
solenberg3a941542015-11-16 07:34:50 -08001092 call_(call),
mflodman3d7db262016-04-29 00:57:13 -07001093 config_(send_transport),
skvlade0d46372016-04-07 22:59:22 -07001094 rtp_parameters_(CreateRtpParametersWithOneEncoding()) {
solenberg85a04962015-10-27 03:35:21 -07001095 RTC_DCHECK_GE(ch, 0);
1096 // TODO(solenberg): Once we're not using FakeWebRtcVoiceEngine anymore:
1097 // RTC_DCHECK(voe_audio_transport);
solenbergc96df772015-10-21 13:01:53 -07001098 RTC_DCHECK(call);
solenberg85a04962015-10-27 03:35:21 -07001099 audio_capture_thread_checker_.DetachFromThread();
solenberg3a941542015-11-16 07:34:50 -08001100 config_.rtp.ssrc = ssrc;
1101 config_.rtp.c_name = c_name;
1102 config_.voe_channel_id = ch;
solenberg971cab02016-06-14 10:02:41 -07001103 config_.rtp.extensions = extensions;
1104 RecreateAudioSendStream(send_codec_spec);
solenbergc96df772015-10-21 13:01:53 -07001105 }
solenberg3a941542015-11-16 07:34:50 -08001106
solenbergc96df772015-10-21 13:01:53 -07001107 ~WebRtcAudioSendStream() override {
solenberg566ef242015-11-06 15:34:49 -08001108 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
Taylor Brandstetter1a018dc2016-03-08 12:37:39 -08001109 ClearSource();
solenbergc96df772015-10-21 13:01:53 -07001110 call_->DestroyAudioSendStream(stream_);
1111 }
henrike@webrtc.org1e09a712013-07-26 19:17:59 +00001112
solenberg971cab02016-06-14 10:02:41 -07001113 void RecreateAudioSendStream(const SendCodecSpec& send_codec_spec) {
1114 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
1115 if (stream_) {
1116 call_->DestroyAudioSendStream(stream_);
1117 stream_ = nullptr;
1118 }
1119 config_.rtp.nack.rtp_history_ms =
1120 send_codec_spec.nack_enabled ? kNackRtpHistoryMs : 0;
1121 RTC_DCHECK(!stream_);
1122 stream_ = call_->CreateAudioSendStream(config_);
1123 RTC_CHECK(stream_);
1124 UpdateSendState();
1125 }
1126
solenberg3a941542015-11-16 07:34:50 -08001127 void RecreateAudioSendStream(
1128 const std::vector<webrtc::RtpExtension>& extensions) {
1129 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
1130 if (stream_) {
1131 call_->DestroyAudioSendStream(stream_);
1132 stream_ = nullptr;
1133 }
1134 config_.rtp.extensions = extensions;
1135 RTC_DCHECK(!stream_);
1136 stream_ = call_->CreateAudioSendStream(config_);
1137 RTC_CHECK(stream_);
solenberg6d6e7c52016-04-13 09:07:30 -07001138 UpdateSendState();
solenberg3a941542015-11-16 07:34:50 -08001139 }
1140
solenberg8842c3e2016-03-11 03:06:41 -08001141 bool SendTelephoneEvent(int payload_type, int event, int duration_ms) {
Fredrik Solenbergb5727682015-12-04 15:22:19 +01001142 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
1143 RTC_DCHECK(stream_);
1144 return stream_->SendTelephoneEvent(payload_type, event, duration_ms);
1145 }
1146
Taylor Brandstetter1a018dc2016-03-08 12:37:39 -08001147 void SetSend(bool send) {
1148 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
1149 send_ = send;
1150 UpdateSendState();
1151 }
1152
solenberg3a941542015-11-16 07:34:50 -08001153 webrtc::AudioSendStream::Stats GetStats() const {
1154 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
1155 RTC_DCHECK(stream_);
1156 return stream_->GetStats();
1157 }
1158
Taylor Brandstetter1a018dc2016-03-08 12:37:39 -08001159 // Starts the sending by setting ourselves as a sink to the AudioSource to
1160 // get data callbacks.
henrike@webrtc.orga7b98182014-02-21 15:51:43 +00001161 // This method is called on the libjingle worker thread.
mallinath@webrtc.org67ee6b92014-02-03 16:57:16 +00001162 // TODO(xians): Make sure Start() is called only once.
Taylor Brandstetter1a018dc2016-03-08 12:37:39 -08001163 void SetSource(AudioSource* source) {
solenberg566ef242015-11-06 15:34:49 -08001164 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
Taylor Brandstetter1a018dc2016-03-08 12:37:39 -08001165 RTC_DCHECK(source);
1166 if (source_) {
1167 RTC_DCHECK(source_ == source);
mallinath@webrtc.org67ee6b92014-02-03 16:57:16 +00001168 return;
1169 }
Taylor Brandstetter1a018dc2016-03-08 12:37:39 -08001170 source->SetSink(this);
1171 source_ = source;
1172 UpdateSendState();
mallinath@webrtc.org67ee6b92014-02-03 16:57:16 +00001173 }
1174
Taylor Brandstetter1a018dc2016-03-08 12:37:39 -08001175 // Stops sending by setting the sink of the AudioSource to nullptr. No data
mallinath@webrtc.org67ee6b92014-02-03 16:57:16 +00001176 // callback will be received after this method.
henrike@webrtc.orga7b98182014-02-21 15:51:43 +00001177 // This method is called on the libjingle worker thread.
Taylor Brandstetter1a018dc2016-03-08 12:37:39 -08001178 void ClearSource() {
solenberg566ef242015-11-06 15:34:49 -08001179 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
Taylor Brandstetter1a018dc2016-03-08 12:37:39 -08001180 if (source_) {
1181 source_->SetSink(nullptr);
1182 source_ = nullptr;
solenberg98c68862015-10-09 03:27:14 -07001183 }
Taylor Brandstetter1a018dc2016-03-08 12:37:39 -08001184 UpdateSendState();
mallinath@webrtc.org67ee6b92014-02-03 16:57:16 +00001185 }
1186
Taylor Brandstetter1a018dc2016-03-08 12:37:39 -08001187 // AudioSource::Sink implementation.
henrike@webrtc.orga7b98182014-02-21 15:51:43 +00001188 // This method is called on the audio thread.
kjellander@webrtc.org14665ff2015-03-04 12:58:35 +00001189 void OnData(const void* audio_data,
1190 int bits_per_sample,
1191 int sample_rate,
Peter Kasting69558702016-01-12 16:26:35 -08001192 size_t number_of_channels,
Peter Kastingdce40cf2015-08-24 14:52:23 -07001193 size_t number_of_frames) override {
solenberg566ef242015-11-06 15:34:49 -08001194 RTC_DCHECK(!worker_thread_checker_.CalledOnValidThread());
solenberg85a04962015-10-27 03:35:21 -07001195 RTC_DCHECK(audio_capture_thread_checker_.CalledOnValidThread());
solenbergc96df772015-10-21 13:01:53 -07001196 RTC_DCHECK(voe_audio_transport_);
solenberg7add0582015-11-20 09:59:34 -08001197 voe_audio_transport_->OnData(config_.voe_channel_id,
henrike@webrtc.orga7b98182014-02-21 15:51:43 +00001198 audio_data,
1199 bits_per_sample,
1200 sample_rate,
1201 number_of_channels,
1202 number_of_frames);
henrike@webrtc.orga7b98182014-02-21 15:51:43 +00001203 }
1204
Taylor Brandstetter1a018dc2016-03-08 12:37:39 -08001205 // Callback from the |source_| when it is going away. In case Start() has
henrike@webrtc.orga7b98182014-02-21 15:51:43 +00001206 // never been called, this callback won't be triggered.
kjellander@webrtc.org14665ff2015-03-04 12:58:35 +00001207 void OnClose() override {
solenberg566ef242015-11-06 15:34:49 -08001208 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
Taylor Brandstetter1a018dc2016-03-08 12:37:39 -08001209 // Set |source_| to nullptr to make sure no more callback will get into
1210 // the source.
1211 source_ = nullptr;
1212 UpdateSendState();
mallinath@webrtc.org67ee6b92014-02-03 16:57:16 +00001213 }
1214
1215 // Accessor to the VoE channel ID.
solenberg85a04962015-10-27 03:35:21 -07001216 int channel() const {
solenberg566ef242015-11-06 15:34:49 -08001217 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
solenberg7add0582015-11-20 09:59:34 -08001218 return config_.voe_channel_id;
solenberg85a04962015-10-27 03:35:21 -07001219 }
mallinath@webrtc.org67ee6b92014-02-03 16:57:16 +00001220
skvlade0d46372016-04-07 22:59:22 -07001221 const webrtc::RtpParameters& rtp_parameters() const {
1222 return rtp_parameters_;
1223 }
1224
Taylor Brandstetter55dd7082016-05-03 13:50:11 -07001225 void SetRtpParameters(const webrtc::RtpParameters& parameters) {
skvlade0d46372016-04-07 22:59:22 -07001226 RTC_CHECK_EQ(1UL, parameters.encodings.size());
1227 rtp_parameters_ = parameters;
Taylor Brandstetter55dd7082016-05-03 13:50:11 -07001228 // parameters.encodings[0].active could have changed.
1229 UpdateSendState();
skvlade0d46372016-04-07 22:59:22 -07001230 }
1231
mallinath@webrtc.org67ee6b92014-02-03 16:57:16 +00001232 private:
Taylor Brandstetter1a018dc2016-03-08 12:37:39 -08001233 void UpdateSendState() {
1234 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
1235 RTC_DCHECK(stream_);
Taylor Brandstetter55dd7082016-05-03 13:50:11 -07001236 RTC_DCHECK_EQ(1UL, rtp_parameters_.encodings.size());
1237 if (send_ && source_ != nullptr && rtp_parameters_.encodings[0].active) {
Taylor Brandstetter1a018dc2016-03-08 12:37:39 -08001238 stream_->Start();
1239 } else { // !send || source_ = nullptr
1240 stream_->Stop();
1241 }
1242 }
1243
solenberg566ef242015-11-06 15:34:49 -08001244 rtc::ThreadChecker worker_thread_checker_;
solenberg85a04962015-10-27 03:35:21 -07001245 rtc::ThreadChecker audio_capture_thread_checker_;
solenbergc96df772015-10-21 13:01:53 -07001246 webrtc::AudioTransport* const voe_audio_transport_ = nullptr;
1247 webrtc::Call* call_ = nullptr;
solenberg3a941542015-11-16 07:34:50 -08001248 webrtc::AudioSendStream::Config config_;
1249 // The stream is owned by WebRtcAudioSendStream and may be reallocated if
1250 // configuration changes.
solenbergc96df772015-10-21 13:01:53 -07001251 webrtc::AudioSendStream* stream_ = nullptr;
mallinath@webrtc.org67ee6b92014-02-03 16:57:16 +00001252
Taylor Brandstetter1a018dc2016-03-08 12:37:39 -08001253 // Raw pointer to AudioSource owned by LocalAudioTrackHandler.
mallinath@webrtc.org67ee6b92014-02-03 16:57:16 +00001254 // PeerConnection will make sure invalidating the pointer before the object
1255 // goes away.
Taylor Brandstetter1a018dc2016-03-08 12:37:39 -08001256 AudioSource* source_ = nullptr;
1257 bool send_ = false;
skvlade0d46372016-04-07 22:59:22 -07001258 webrtc::RtpParameters rtp_parameters_;
henrike@webrtc.orga7b98182014-02-21 15:51:43 +00001259
solenbergc96df772015-10-21 13:01:53 -07001260 RTC_DISALLOW_IMPLICIT_CONSTRUCTORS(WebRtcAudioSendStream);
1261};
1262
1263class WebRtcVoiceMediaChannel::WebRtcAudioReceiveStream {
1264 public:
ossu29b1a8d2016-06-13 07:34:51 -07001265 WebRtcAudioReceiveStream(
1266 int ch,
1267 uint32_t remote_ssrc,
1268 uint32_t local_ssrc,
1269 bool use_transport_cc,
1270 const std::string& sync_group,
1271 const std::vector<webrtc::RtpExtension>& extensions,
1272 webrtc::Call* call,
1273 webrtc::Transport* rtcp_send_transport,
1274 const rtc::scoped_refptr<webrtc::AudioDecoderFactory>& decoder_factory)
stefanba4c0e42016-02-04 04:12:24 -08001275 : call_(call), config_() {
solenberg7add0582015-11-20 09:59:34 -08001276 RTC_DCHECK_GE(ch, 0);
1277 RTC_DCHECK(call);
1278 config_.rtp.remote_ssrc = remote_ssrc;
1279 config_.rtp.local_ssrc = local_ssrc;
solenberg31fec402016-05-06 02:13:12 -07001280 config_.rtcp_send_transport = rtcp_send_transport;
solenberg7add0582015-11-20 09:59:34 -08001281 config_.voe_channel_id = ch;
1282 config_.sync_group = sync_group;
ossu29b1a8d2016-06-13 07:34:51 -07001283 config_.decoder_factory = decoder_factory;
stefanba4c0e42016-02-04 04:12:24 -08001284 RecreateAudioReceiveStream(use_transport_cc, extensions);
solenberg7add0582015-11-20 09:59:34 -08001285 }
solenbergc96df772015-10-21 13:01:53 -07001286
solenberg7add0582015-11-20 09:59:34 -08001287 ~WebRtcAudioReceiveStream() {
1288 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
1289 call_->DestroyAudioReceiveStream(stream_);
1290 }
1291
1292 void RecreateAudioReceiveStream(
1293 const std::vector<webrtc::RtpExtension>& extensions) {
1294 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
stefanba4c0e42016-02-04 04:12:24 -08001295 RecreateAudioReceiveStream(config_.rtp.transport_cc, extensions);
solenberg7add0582015-11-20 09:59:34 -08001296 }
stefanba4c0e42016-02-04 04:12:24 -08001297 void RecreateAudioReceiveStream(bool use_transport_cc) {
solenberg7add0582015-11-20 09:59:34 -08001298 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
stefanba4c0e42016-02-04 04:12:24 -08001299 RecreateAudioReceiveStream(use_transport_cc, config_.rtp.extensions);
solenberg7add0582015-11-20 09:59:34 -08001300 }
1301
1302 webrtc::AudioReceiveStream::Stats GetStats() const {
1303 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
1304 RTC_DCHECK(stream_);
1305 return stream_->GetStats();
1306 }
1307
1308 int channel() const {
1309 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
1310 return config_.voe_channel_id;
1311 }
solenbergc96df772015-10-21 13:01:53 -07001312
kwiberg686a8ef2016-02-26 03:00:35 -08001313 void SetRawAudioSink(std::unique_ptr<webrtc::AudioSinkInterface> sink) {
Tommif888bb52015-12-12 01:37:01 +01001314 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
kwiberg686a8ef2016-02-26 03:00:35 -08001315 stream_->SetSink(std::move(sink));
Tommif888bb52015-12-12 01:37:01 +01001316 }
1317
solenbergc96df772015-10-21 13:01:53 -07001318 private:
stefanba4c0e42016-02-04 04:12:24 -08001319 void RecreateAudioReceiveStream(
1320 bool use_transport_cc,
solenberg7add0582015-11-20 09:59:34 -08001321 const std::vector<webrtc::RtpExtension>& extensions) {
1322 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
1323 if (stream_) {
1324 call_->DestroyAudioReceiveStream(stream_);
1325 stream_ = nullptr;
1326 }
1327 config_.rtp.extensions = extensions;
stefanba4c0e42016-02-04 04:12:24 -08001328 config_.rtp.transport_cc = use_transport_cc;
solenberg7add0582015-11-20 09:59:34 -08001329 RTC_DCHECK(!stream_);
1330 stream_ = call_->CreateAudioReceiveStream(config_);
1331 RTC_CHECK(stream_);
1332 }
1333
1334 rtc::ThreadChecker worker_thread_checker_;
1335 webrtc::Call* call_ = nullptr;
1336 webrtc::AudioReceiveStream::Config config_;
1337 // The stream is owned by WebRtcAudioReceiveStream and may be reallocated if
1338 // configuration changes.
1339 webrtc::AudioReceiveStream* stream_ = nullptr;
solenbergc96df772015-10-21 13:01:53 -07001340
1341 RTC_DISALLOW_IMPLICIT_CONSTRUCTORS(WebRtcAudioReceiveStream);
henrike@webrtc.org1e09a712013-07-26 19:17:59 +00001342};
1343
Fredrik Solenberg709ed672015-09-15 12:26:33 +02001344WebRtcVoiceMediaChannel::WebRtcVoiceMediaChannel(WebRtcVoiceEngine* engine,
nisse51542be2016-02-12 02:27:06 -08001345 const MediaConfig& config,
Fredrik Solenbergb071a192015-09-17 16:42:56 +02001346 const AudioOptions& options,
Fredrik Solenberg709ed672015-09-15 12:26:33 +02001347 webrtc::Call* call)
nisse51542be2016-02-12 02:27:06 -08001348 : VoiceMediaChannel(config), engine_(engine), call_(call) {
solenberg0a617e22015-10-20 15:49:38 -07001349 LOG(LS_VERBOSE) << "WebRtcVoiceMediaChannel::WebRtcVoiceMediaChannel";
solenberg566ef242015-11-06 15:34:49 -08001350 RTC_DCHECK(call);
solenberg0a617e22015-10-20 15:49:38 -07001351 engine->RegisterChannel(this);
Fredrik Solenbergb071a192015-09-17 16:42:56 +02001352 SetOptions(options);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001353}
1354
1355WebRtcVoiceMediaChannel::~WebRtcVoiceMediaChannel() {
solenberg566ef242015-11-06 15:34:49 -08001356 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
solenberg0a617e22015-10-20 15:49:38 -07001357 LOG(LS_VERBOSE) << "WebRtcVoiceMediaChannel::~WebRtcVoiceMediaChannel";
solenberg7add0582015-11-20 09:59:34 -08001358 // TODO(solenberg): Should be able to delete the streams directly, without
1359 // going through RemoveNnStream(), once stream objects handle
1360 // all (de)configuration.
solenbergc96df772015-10-21 13:01:53 -07001361 while (!send_streams_.empty()) {
1362 RemoveSendStream(send_streams_.begin()->first);
solenbergd97ec302015-10-07 01:40:33 -07001363 }
solenberg7add0582015-11-20 09:59:34 -08001364 while (!recv_streams_.empty()) {
1365 RemoveRecvStream(recv_streams_.begin()->first);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001366 }
solenberg0a617e22015-10-20 15:49:38 -07001367 engine()->UnregisterChannel(this);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001368}
1369
nisse51542be2016-02-12 02:27:06 -08001370rtc::DiffServCodePoint WebRtcVoiceMediaChannel::PreferredDscp() const {
1371 return kAudioDscpValue;
1372}
1373
Peter Thatcherc2ee2c82015-08-07 16:05:34 -07001374bool WebRtcVoiceMediaChannel::SetSendParameters(
1375 const AudioSendParameters& params) {
Peter Boströmca8b4042016-03-08 14:24:13 -08001376 TRACE_EVENT0("webrtc", "WebRtcVoiceMediaChannel::SetSendParameters");
solenberg566ef242015-11-06 15:34:49 -08001377 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
solenberg7e4e01a2015-12-02 08:05:01 -08001378 LOG(LS_INFO) << "WebRtcVoiceMediaChannel::SetSendParameters: "
1379 << params.ToString();
Peter Thatcherc2ee2c82015-08-07 16:05:34 -07001380 // TODO(pthatcher): Refactor this to be more clean now that we have
1381 // all the information at once.
solenberg3a941542015-11-16 07:34:50 -08001382
1383 if (!SetSendCodecs(params.codecs)) {
1384 return false;
1385 }
1386
solenberg7e4e01a2015-12-02 08:05:01 -08001387 if (!ValidateRtpExtensions(params.extensions)) {
1388 return false;
1389 }
1390 std::vector<webrtc::RtpExtension> filtered_extensions =
1391 FilterRtpExtensions(params.extensions,
1392 webrtc::RtpExtension::IsSupportedForAudio, true);
1393 if (send_rtp_extensions_ != filtered_extensions) {
1394 send_rtp_extensions_.swap(filtered_extensions);
solenberg3a941542015-11-16 07:34:50 -08001395 for (auto& it : send_streams_) {
1396 it.second->RecreateAudioSendStream(send_rtp_extensions_);
1397 }
1398 }
1399
deadbeef80346142016-04-27 14:17:10 -07001400 if (!SetMaxSendBitrate(params.max_bandwidth_bps)) {
solenberg3a941542015-11-16 07:34:50 -08001401 return false;
1402 }
1403 return SetOptions(params.options);
Peter Thatcherc2ee2c82015-08-07 16:05:34 -07001404}
1405
1406bool WebRtcVoiceMediaChannel::SetRecvParameters(
1407 const AudioRecvParameters& params) {
Peter Boströmca8b4042016-03-08 14:24:13 -08001408 TRACE_EVENT0("webrtc", "WebRtcVoiceMediaChannel::SetRecvParameters");
solenberg566ef242015-11-06 15:34:49 -08001409 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
solenberg7e4e01a2015-12-02 08:05:01 -08001410 LOG(LS_INFO) << "WebRtcVoiceMediaChannel::SetRecvParameters: "
1411 << params.ToString();
Peter Thatcherc2ee2c82015-08-07 16:05:34 -07001412 // TODO(pthatcher): Refactor this to be more clean now that we have
1413 // all the information at once.
solenberg7add0582015-11-20 09:59:34 -08001414
1415 if (!SetRecvCodecs(params.codecs)) {
1416 return false;
1417 }
1418
solenberg7e4e01a2015-12-02 08:05:01 -08001419 if (!ValidateRtpExtensions(params.extensions)) {
1420 return false;
1421 }
1422 std::vector<webrtc::RtpExtension> filtered_extensions =
1423 FilterRtpExtensions(params.extensions,
1424 webrtc::RtpExtension::IsSupportedForAudio, false);
1425 if (recv_rtp_extensions_ != filtered_extensions) {
1426 recv_rtp_extensions_.swap(filtered_extensions);
solenberg7add0582015-11-20 09:59:34 -08001427 for (auto& it : recv_streams_) {
1428 it.second->RecreateAudioReceiveStream(recv_rtp_extensions_);
1429 }
1430 }
solenberg7add0582015-11-20 09:59:34 -08001431 return true;
Peter Thatcherc2ee2c82015-08-07 16:05:34 -07001432}
1433
Taylor Brandstetterdb0cd9e2016-05-16 11:40:30 -07001434webrtc::RtpParameters WebRtcVoiceMediaChannel::GetRtpSendParameters(
skvlade0d46372016-04-07 22:59:22 -07001435 uint32_t ssrc) const {
1436 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
1437 auto it = send_streams_.find(ssrc);
1438 if (it == send_streams_.end()) {
Taylor Brandstetterdb0cd9e2016-05-16 11:40:30 -07001439 LOG(LS_WARNING) << "Attempting to get RTP send parameters for stream "
1440 << "with ssrc " << ssrc << " which doesn't exist.";
skvlade0d46372016-04-07 22:59:22 -07001441 return webrtc::RtpParameters();
1442 }
1443
Taylor Brandstetter0cd086b2016-04-20 16:23:10 -07001444 webrtc::RtpParameters rtp_params = it->second->rtp_parameters();
1445 // Need to add the common list of codecs to the send stream-specific
1446 // RTP parameters.
1447 for (const AudioCodec& codec : send_codecs_) {
1448 rtp_params.codecs.push_back(codec.ToCodecParameters());
1449 }
1450 return rtp_params;
skvlade0d46372016-04-07 22:59:22 -07001451}
1452
Taylor Brandstetterdb0cd9e2016-05-16 11:40:30 -07001453bool WebRtcVoiceMediaChannel::SetRtpSendParameters(
skvlade0d46372016-04-07 22:59:22 -07001454 uint32_t ssrc,
1455 const webrtc::RtpParameters& parameters) {
1456 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
1457 if (!ValidateRtpParameters(parameters)) {
1458 return false;
1459 }
1460 auto it = send_streams_.find(ssrc);
1461 if (it == send_streams_.end()) {
Taylor Brandstetterdb0cd9e2016-05-16 11:40:30 -07001462 LOG(LS_WARNING) << "Attempting to set RTP send parameters for stream "
1463 << "with ssrc " << ssrc << " which doesn't exist.";
skvlade0d46372016-04-07 22:59:22 -07001464 return false;
1465 }
1466
Taylor Brandstetterdb0cd9e2016-05-16 11:40:30 -07001467 // TODO(deadbeef): Handle setting parameters with a list of codecs in a
1468 // different order (which should change the send codec).
1469 webrtc::RtpParameters current_parameters = GetRtpSendParameters(ssrc);
1470 if (current_parameters.codecs != parameters.codecs) {
1471 LOG(LS_ERROR) << "Using SetParameters to change the set of codecs "
1472 << "is not currently supported.";
1473 return false;
1474 }
1475
1476 if (!SetChannelSendParameters(it->second->channel(), parameters)) {
1477 LOG(LS_WARNING) << "Failed to set send RtpParameters.";
skvlade0d46372016-04-07 22:59:22 -07001478 return false;
1479 }
Taylor Brandstetter0cd086b2016-04-20 16:23:10 -07001480 // Codecs are handled at the WebRtcVoiceMediaChannel level.
1481 webrtc::RtpParameters reduced_params = parameters;
1482 reduced_params.codecs.clear();
Taylor Brandstetter55dd7082016-05-03 13:50:11 -07001483 it->second->SetRtpParameters(reduced_params);
skvlade0d46372016-04-07 22:59:22 -07001484 return true;
1485}
1486
Taylor Brandstetterdb0cd9e2016-05-16 11:40:30 -07001487webrtc::RtpParameters WebRtcVoiceMediaChannel::GetRtpReceiveParameters(
1488 uint32_t ssrc) const {
1489 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
1490 auto it = recv_streams_.find(ssrc);
1491 if (it == recv_streams_.end()) {
1492 LOG(LS_WARNING) << "Attempting to get RTP receive parameters for stream "
1493 << "with ssrc " << ssrc << " which doesn't exist.";
1494 return webrtc::RtpParameters();
1495 }
1496
1497 // TODO(deadbeef): Return stream-specific parameters.
1498 webrtc::RtpParameters rtp_params = CreateRtpParametersWithOneEncoding();
1499 for (const AudioCodec& codec : recv_codecs_) {
1500 rtp_params.codecs.push_back(codec.ToCodecParameters());
1501 }
1502 return rtp_params;
1503}
1504
1505bool WebRtcVoiceMediaChannel::SetRtpReceiveParameters(
1506 uint32_t ssrc,
1507 const webrtc::RtpParameters& parameters) {
1508 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
1509 if (!ValidateRtpParameters(parameters)) {
1510 return false;
1511 }
1512 auto it = recv_streams_.find(ssrc);
1513 if (it == recv_streams_.end()) {
1514 LOG(LS_WARNING) << "Attempting to set RTP receive parameters for stream "
1515 << "with ssrc " << ssrc << " which doesn't exist.";
1516 return false;
1517 }
1518
1519 webrtc::RtpParameters current_parameters = GetRtpReceiveParameters(ssrc);
1520 if (current_parameters != parameters) {
1521 LOG(LS_ERROR) << "Changing the RTP receive parameters is currently "
1522 << "unsupported.";
1523 return false;
1524 }
1525 return true;
1526}
1527
skvlade0d46372016-04-07 22:59:22 -07001528bool WebRtcVoiceMediaChannel::ValidateRtpParameters(
1529 const webrtc::RtpParameters& rtp_parameters) {
1530 if (rtp_parameters.encodings.size() != 1) {
1531 LOG(LS_ERROR)
1532 << "Attempted to set RtpParameters without exactly one encoding";
1533 return false;
1534 }
1535 return true;
1536}
1537
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001538bool WebRtcVoiceMediaChannel::SetOptions(const AudioOptions& options) {
solenberg566ef242015-11-06 15:34:49 -08001539 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001540 LOG(LS_INFO) << "Setting voice channel options: "
1541 << options.ToString();
1542
1543 // We retain all of the existing options, and apply the given ones
1544 // on top. This means there is no way to "clear" options such that
1545 // they go back to the engine default.
1546 options_.SetAll(options);
solenberg246b8172015-12-08 09:50:23 -08001547 if (!engine()->ApplyOptions(options_)) {
1548 LOG(LS_WARNING) <<
1549 "Failed to apply engine options during channel SetOptions.";
1550 return false;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001551 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001552 LOG(LS_INFO) << "Set voice channel options. Current options: "
1553 << options_.ToString();
1554 return true;
1555}
1556
1557bool WebRtcVoiceMediaChannel::SetRecvCodecs(
1558 const std::vector<AudioCodec>& codecs) {
solenberg566ef242015-11-06 15:34:49 -08001559 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
solenberg8fb30c32015-10-13 03:06:58 -07001560
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001561 // Set the payload types to be used for incoming media.
solenberg0b675462015-10-09 01:37:09 -07001562 LOG(LS_INFO) << "Setting receive voice codecs.";
solenberg0b675462015-10-09 01:37:09 -07001563
1564 if (!VerifyUniquePayloadTypes(codecs)) {
1565 LOG(LS_ERROR) << "Codec payload types overlap.";
1566 return false;
1567 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001568
1569 std::vector<AudioCodec> new_codecs;
1570 // Find all new codecs. We allow adding new codecs but don't allow changing
1571 // the payload type of codecs that is already configured since we might
1572 // already be receiving packets with that payload type.
Fredrik Solenbergaf9fb212015-08-26 10:45:53 +02001573 for (const AudioCodec& codec : codecs) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001574 AudioCodec old_codec;
Fredrik Solenbergaf9fb212015-08-26 10:45:53 +02001575 if (FindCodec(recv_codecs_, codec, &old_codec)) {
1576 if (old_codec.id != codec.id) {
1577 LOG(LS_ERROR) << codec.name << " payload type changed.";
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001578 return false;
1579 }
1580 } else {
Fredrik Solenbergaf9fb212015-08-26 10:45:53 +02001581 new_codecs.push_back(codec);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001582 }
1583 }
1584 if (new_codecs.empty()) {
1585 // There are no new codecs to configure. Already configured codecs are
1586 // never removed.
1587 return true;
1588 }
1589
1590 if (playout_) {
1591 // Receive codecs can not be changed while playing. So we temporarily
1592 // pause playout.
1593 PausePlayout();
1594 }
1595
solenberg26c8c912015-11-27 04:00:25 -08001596 bool result = true;
1597 for (const AudioCodec& codec : new_codecs) {
solenberg72e29d22016-03-08 06:35:16 -08001598 webrtc::CodecInst voe_codec = {0};
solenberg26c8c912015-11-27 04:00:25 -08001599 if (WebRtcVoiceEngine::ToCodecInst(codec, &voe_codec)) {
1600 LOG(LS_INFO) << ToString(codec);
1601 voe_codec.pltype = codec.id;
1602 for (const auto& ch : recv_streams_) {
1603 if (engine()->voe()->codec()->SetRecPayloadType(
1604 ch.second->channel(), voe_codec) == -1) {
1605 LOG_RTCERR2(SetRecPayloadType, ch.second->channel(),
1606 ToString(voe_codec));
1607 result = false;
1608 }
1609 }
1610 } else {
1611 LOG(LS_WARNING) << "Unknown codec " << ToString(codec);
1612 result = false;
1613 break;
1614 }
1615 }
Fredrik Solenbergaf9fb212015-08-26 10:45:53 +02001616 if (result) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001617 recv_codecs_ = codecs;
1618 }
1619
1620 if (desired_playout_ && !playout_) {
1621 ResumePlayout();
1622 }
Fredrik Solenbergaf9fb212015-08-26 10:45:53 +02001623 return result;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001624}
1625
solenberg72e29d22016-03-08 06:35:16 -08001626// Utility function called from SetSendParameters() to extract current send
1627// codec settings from the given list of codecs (originally from SDP). Both send
1628// and receive streams may be reconfigured based on the new settings.
wu@webrtc.orgcadf9042013-08-30 21:24:16 +00001629bool WebRtcVoiceMediaChannel::SetSendCodecs(
1630 const std::vector<AudioCodec>& codecs) {
solenberg566ef242015-11-06 15:34:49 -08001631 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
Fredrik Solenbergb5727682015-12-04 15:22:19 +01001632 // TODO(solenberg): Validate input - that payload types don't overlap, are
1633 // within range, filter out codecs we don't support,
solenberg31642aa2016-03-14 08:00:37 -07001634 // redundant codecs etc - the same way it is done for
1635 // RtpHeaderExtensions.
solenbergd97ec302015-10-07 01:40:33 -07001636
Fredrik Solenbergb5727682015-12-04 15:22:19 +01001637 // Find the DTMF telephone event "codec" payload type.
1638 dtmf_payload_type_ = rtc::Optional<int>();
Fredrik Solenbergaf9fb212015-08-26 10:45:53 +02001639 for (const AudioCodec& codec : codecs) {
Fredrik Solenbergaf9fb212015-08-26 10:45:53 +02001640 if (IsCodec(codec, kDtmfCodecName)) {
solenberg31642aa2016-03-14 08:00:37 -07001641 if (codec.id < kMinPayloadType || codec.id > kMaxPayloadType) {
1642 return false;
1643 }
Fredrik Solenbergb5727682015-12-04 15:22:19 +01001644 dtmf_payload_type_ = rtc::Optional<int>(codec.id);
1645 break;
wu@webrtc.orgcadf9042013-08-30 21:24:16 +00001646 }
1647 }
1648
solenberg72e29d22016-03-08 06:35:16 -08001649 // Scan through the list to figure out the codec to use for sending, along
kwiberg68061362016-06-14 08:04:47 -07001650 // with the proper configuration for VAD, CNG, NACK and Opus-specific
solenberg72e29d22016-03-08 06:35:16 -08001651 // parameters.
kwiberg68061362016-06-14 08:04:47 -07001652 // TODO(solenberg): Refactor this logic once we create AudioEncoders here.
solenberg971cab02016-06-14 10:02:41 -07001653 SendCodecSpec send_codec_spec;
solenberg72e29d22016-03-08 06:35:16 -08001654 {
solenberg72e29d22016-03-08 06:35:16 -08001655 send_codec_spec.nack_enabled = send_codec_spec_.nack_enabled;
1656
1657 // Find send codec (the first non-telephone-event/CN codec).
1658 const AudioCodec* codec = WebRtcVoiceCodecs::GetPreferredCodec(
kwiberg68061362016-06-14 08:04:47 -07001659 codecs, &send_codec_spec.codec_inst);
solenberg72e29d22016-03-08 06:35:16 -08001660 if (!codec) {
1661 LOG(LS_WARNING) << "Received empty list of codecs.";
1662 return false;
1663 }
1664
1665 send_codec_spec.transport_cc_enabled = HasTransportCc(*codec);
kwiberg68061362016-06-14 08:04:47 -07001666 send_codec_spec.nack_enabled = HasNack(*codec);
solenberg72e29d22016-03-08 06:35:16 -08001667
kwiberg68061362016-06-14 08:04:47 -07001668 // For Opus as the send codec, we are to determine inband FEC, maximum
1669 // playback rate, and opus internal dtx.
1670 if (IsCodec(*codec, kOpusCodecName)) {
1671 GetOpusConfig(*codec, &send_codec_spec.codec_inst,
1672 &send_codec_spec.enable_codec_fec,
1673 &send_codec_spec.opus_max_playback_rate,
1674 &send_codec_spec.enable_opus_dtx);
1675 }
solenberg72e29d22016-03-08 06:35:16 -08001676
kwiberg68061362016-06-14 08:04:47 -07001677 // Set packet size if the AudioCodec param kCodecParamPTime is set.
1678 int ptime_ms = 0;
1679 if (codec->GetParam(kCodecParamPTime, &ptime_ms)) {
1680 if (!WebRtcVoiceCodecs::SetPTimeAsPacketSize(
1681 &send_codec_spec.codec_inst, ptime_ms)) {
1682 LOG(LS_WARNING) << "Failed to set packet size for codec "
1683 << send_codec_spec.codec_inst.plname;
1684 return false;
solenberg72e29d22016-03-08 06:35:16 -08001685 }
1686 }
1687
1688 // Loop through the codecs list again to find the CN codec.
1689 // TODO(solenberg): Break out into a separate function?
1690 for (const AudioCodec& codec : codecs) {
1691 // Ignore codecs we don't know about. The negotiation step should prevent
1692 // this, but double-check to be sure.
1693 webrtc::CodecInst voe_codec = {0};
1694 if (!WebRtcVoiceEngine::ToCodecInst(codec, &voe_codec)) {
1695 LOG(LS_WARNING) << "Unknown codec " << ToString(codec);
1696 continue;
1697 }
1698
1699 if (IsCodec(codec, kCnCodecName)) {
1700 // Turn voice activity detection/comfort noise on if supported.
1701 // Set the wideband CN payload type appropriately.
1702 // (narrowband always uses the static payload type 13).
1703 int cng_plfreq = -1;
1704 switch (codec.clockrate) {
1705 case 8000:
1706 case 16000:
1707 case 32000:
1708 cng_plfreq = codec.clockrate;
1709 break;
1710 default:
1711 LOG(LS_WARNING) << "CN frequency " << codec.clockrate
1712 << " not supported.";
1713 continue;
1714 }
1715 send_codec_spec.cng_payload_type = codec.id;
1716 send_codec_spec.cng_plfreq = cng_plfreq;
1717 break;
1718 }
1719 }
solenberg72e29d22016-03-08 06:35:16 -08001720 }
1721
solenberg971cab02016-06-14 10:02:41 -07001722 // Apply new settings to all streams.
1723 if (send_codec_spec_ != send_codec_spec) {
1724 send_codec_spec_ = std::move(send_codec_spec);
1725 for (const auto& kv : send_streams_) {
1726 kv.second->RecreateAudioSendStream(send_codec_spec_);
1727 if (!SetSendCodecs(kv.second->channel(), kv.second->rtp_parameters())) {
1728 return false;
1729 }
wu@webrtc.orgcadf9042013-08-30 21:24:16 +00001730 }
1731 }
1732
solenberg72e29d22016-03-08 06:35:16 -08001733 // Set nack status on receive channels.
deadbeefb56069e2016-05-06 04:57:03 -07001734 for (const auto& kv : recv_streams_) {
1735 SetNack(kv.second->channel(), send_codec_spec_.nack_enabled);
wu@webrtc.org9dba5252013-08-05 20:36:57 +00001736 }
solenberg0a617e22015-10-20 15:49:38 -07001737
stefanba4c0e42016-02-04 04:12:24 -08001738 // Check if the transport cc feedback has changed on the preferred send codec,
1739 // and in that case reconfigure all receive streams.
solenberg72e29d22016-03-08 06:35:16 -08001740 if (recv_transport_cc_enabled_ != send_codec_spec_.transport_cc_enabled) {
1741 LOG(LS_INFO) << "Recreate all the receive streams because the send "
1742 "codec has changed.";
1743 recv_transport_cc_enabled_ = send_codec_spec_.transport_cc_enabled;
1744 for (auto& kv : recv_streams_) {
1745 kv.second->RecreateAudioReceiveStream(recv_transport_cc_enabled_);
1746 }
1747 }
1748
Taylor Brandstetter0cd086b2016-04-20 16:23:10 -07001749 send_codecs_ = codecs;
solenberg72e29d22016-03-08 06:35:16 -08001750 return true;
1751}
1752
1753// Apply current codec settings to a single voe::Channel used for sending.
skvlade0d46372016-04-07 22:59:22 -07001754bool WebRtcVoiceMediaChannel::SetSendCodecs(
1755 int channel,
1756 const webrtc::RtpParameters& rtp_parameters) {
solenberg971cab02016-06-14 10:02:41 -07001757 // Disable VAD and FEC unless we know the other side wants them.
solenberg72e29d22016-03-08 06:35:16 -08001758 engine()->voe()->codec()->SetVADStatus(channel, false);
solenberg72e29d22016-03-08 06:35:16 -08001759 engine()->voe()->codec()->SetFECStatus(channel, false);
1760
solenberg72e29d22016-03-08 06:35:16 -08001761 // Set the codec immediately, since SetVADStatus() depends on whether
1762 // the current codec is mono or stereo.
1763 if (!SetSendCodec(channel, send_codec_spec_.codec_inst)) {
1764 return false;
1765 }
1766
1767 // FEC should be enabled after SetSendCodec.
1768 if (send_codec_spec_.enable_codec_fec) {
1769 LOG(LS_INFO) << "Attempt to enable codec internal FEC on channel "
1770 << channel;
1771 if (engine()->voe()->codec()->SetFECStatus(channel, true) == -1) {
1772 // Enable codec internal FEC. Treat any failure as fatal internal error.
1773 LOG_RTCERR2(SetFECStatus, channel, true);
1774 return false;
1775 }
1776 }
1777
1778 if (IsCodec(send_codec_spec_.codec_inst, kOpusCodecName)) {
1779 // DTX and maxplaybackrate should be set after SetSendCodec. Because current
1780 // send codec has to be Opus.
1781
1782 // Set Opus internal DTX.
1783 LOG(LS_INFO) << "Attempt to "
1784 << (send_codec_spec_.enable_opus_dtx ? "enable" : "disable")
1785 << " Opus DTX on channel "
1786 << channel;
1787 if (engine()->voe()->codec()->SetOpusDtx(channel,
1788 send_codec_spec_.enable_opus_dtx)) {
1789 LOG_RTCERR2(SetOpusDtx, channel, send_codec_spec_.enable_opus_dtx);
1790 return false;
1791 }
1792
1793 // If opus_max_playback_rate <= 0, the default maximum playback rate
1794 // (48 kHz) will be used.
1795 if (send_codec_spec_.opus_max_playback_rate > 0) {
1796 LOG(LS_INFO) << "Attempt to set maximum playback rate to "
1797 << send_codec_spec_.opus_max_playback_rate
1798 << " Hz on channel "
1799 << channel;
1800 if (engine()->voe()->codec()->SetOpusMaxPlaybackRate(
1801 channel, send_codec_spec_.opus_max_playback_rate) == -1) {
1802 LOG_RTCERR2(SetOpusMaxPlaybackRate, channel,
1803 send_codec_spec_.opus_max_playback_rate);
1804 return false;
stefanba4c0e42016-02-04 04:12:24 -08001805 }
1806 }
1807 }
deadbeef80346142016-04-27 14:17:10 -07001808 // TODO(solenberg): SetMaxSendBitrate() yields another call to SetSendCodec().
skvlade0d46372016-04-07 22:59:22 -07001809 // Check if it is possible to fuse with the previous call in this function.
Taylor Brandstetterdb0cd9e2016-05-16 11:40:30 -07001810 SetChannelSendParameters(channel, rtp_parameters);
solenberg72e29d22016-03-08 06:35:16 -08001811
1812 // Set the CN payloadtype and the VAD status.
1813 if (send_codec_spec_.cng_payload_type != -1) {
1814 // The CN payload type for 8000 Hz clockrate is fixed at 13.
1815 if (send_codec_spec_.cng_plfreq != 8000) {
1816 webrtc::PayloadFrequencies cn_freq;
1817 switch (send_codec_spec_.cng_plfreq) {
1818 case 16000:
1819 cn_freq = webrtc::kFreq16000Hz;
1820 break;
1821 case 32000:
1822 cn_freq = webrtc::kFreq32000Hz;
1823 break;
1824 default:
1825 RTC_NOTREACHED();
1826 return false;
1827 }
1828 if (engine()->voe()->codec()->SetSendCNPayloadType(
1829 channel, send_codec_spec_.cng_payload_type, cn_freq) == -1) {
1830 LOG_RTCERR3(SetSendCNPayloadType, channel,
1831 send_codec_spec_.cng_payload_type, cn_freq);
1832 // TODO(ajm): This failure condition will be removed from VoE.
1833 // Restore the return here when we update to a new enough webrtc.
1834 //
1835 // Not returning false because the SetSendCNPayloadType will fail if
1836 // the channel is already sending.
1837 // This can happen if the remote description is applied twice, for
1838 // example in the case of ROAP on top of JSEP, where both side will
1839 // send the offer.
1840 }
1841 }
1842
1843 // Only turn on VAD if we have a CN payload type that matches the
1844 // clockrate for the codec we are going to use.
1845 if (send_codec_spec_.cng_plfreq == send_codec_spec_.codec_inst.plfreq &&
1846 send_codec_spec_.codec_inst.channels == 1) {
1847 // TODO(minyue): If CN frequency == 48000 Hz is allowed, consider the
1848 // interaction between VAD and Opus FEC.
1849 LOG(LS_INFO) << "Enabling VAD";
1850 if (engine()->voe()->codec()->SetVADStatus(channel, true) == -1) {
1851 LOG_RTCERR2(SetVADStatus, channel, true);
1852 return false;
1853 }
1854 }
1855 }
solenberg0a617e22015-10-20 15:49:38 -07001856 return true;
wu@webrtc.org9dba5252013-08-05 20:36:57 +00001857}
1858
wu@webrtc.orgcadf9042013-08-30 21:24:16 +00001859void WebRtcVoiceMediaChannel::SetNack(int channel, bool nack_enabled) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001860 if (nack_enabled) {
wu@webrtc.orgcadf9042013-08-30 21:24:16 +00001861 LOG(LS_INFO) << "Enabling NACK for channel " << channel;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001862 engine()->voe()->rtp()->SetNACKStatus(channel, true, kNackMaxPackets);
1863 } else {
wu@webrtc.orgcadf9042013-08-30 21:24:16 +00001864 LOG(LS_INFO) << "Disabling NACK for channel " << channel;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001865 engine()->voe()->rtp()->SetNACKStatus(channel, false, 0);
1866 }
1867}
1868
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001869bool WebRtcVoiceMediaChannel::SetSendCodec(
wu@webrtc.org9dba5252013-08-05 20:36:57 +00001870 int channel, const webrtc::CodecInst& send_codec) {
1871 LOG(LS_INFO) << "Send channel " << channel << " selected voice codec "
1872 << ToString(send_codec) << ", bitrate=" << send_codec.rate;
1873
solenberg72e29d22016-03-08 06:35:16 -08001874 webrtc::CodecInst current_codec = {0};
wu@webrtc.org05e7b442014-04-01 17:44:24 +00001875 if (engine()->voe()->codec()->GetSendCodec(channel, current_codec) == 0 &&
1876 (send_codec == current_codec)) {
1877 // Codec is already configured, we can return without setting it again.
1878 return true;
1879 }
1880
wu@webrtc.org9dba5252013-08-05 20:36:57 +00001881 if (engine()->voe()->codec()->SetSendCodec(channel, send_codec) == -1) {
1882 LOG_RTCERR2(SetSendCodec, channel, ToString(send_codec));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001883 return false;
1884 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001885 return true;
1886}
1887
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001888bool WebRtcVoiceMediaChannel::SetPlayout(bool playout) {
1889 desired_playout_ = playout;
1890 return ChangePlayout(desired_playout_);
1891}
1892
1893bool WebRtcVoiceMediaChannel::PausePlayout() {
1894 return ChangePlayout(false);
1895}
1896
1897bool WebRtcVoiceMediaChannel::ResumePlayout() {
1898 return ChangePlayout(desired_playout_);
1899}
1900
1901bool WebRtcVoiceMediaChannel::ChangePlayout(bool playout) {
Peter Boströmca8b4042016-03-08 14:24:13 -08001902 TRACE_EVENT0("webrtc", "WebRtcVoiceMediaChannel::ChangePlayout");
solenberg566ef242015-11-06 15:34:49 -08001903 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001904 if (playout_ == playout) {
1905 return true;
1906 }
1907
solenberg7add0582015-11-20 09:59:34 -08001908 for (const auto& ch : recv_streams_) {
Fredrik Solenbergaf9fb212015-08-26 10:45:53 +02001909 if (!SetPlayout(ch.second->channel(), playout)) {
henrike@webrtc.org1e09a712013-07-26 19:17:59 +00001910 LOG(LS_ERROR) << "SetPlayout " << playout << " on channel "
Fredrik Solenbergaf9fb212015-08-26 10:45:53 +02001911 << ch.second->channel() << " failed";
solenberg1ac56142015-10-13 03:58:19 -07001912 return false;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001913 }
1914 }
solenberg1ac56142015-10-13 03:58:19 -07001915 playout_ = playout;
1916 return true;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001917}
1918
Taylor Brandstetter1a018dc2016-03-08 12:37:39 -08001919void WebRtcVoiceMediaChannel::SetSend(bool send) {
Peter Boströmca8b4042016-03-08 14:24:13 -08001920 TRACE_EVENT0("webrtc", "WebRtcVoiceMediaChannel::SetSend");
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001921 if (send_ == send) {
Taylor Brandstetter1a018dc2016-03-08 12:37:39 -08001922 return;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001923 }
1924
solenbergd53a3f92016-04-14 13:56:37 -07001925 // Apply channel specific options, and initialize the ADM for recording (this
1926 // may take time on some platforms, e.g. Android).
Taylor Brandstetter1a018dc2016-03-08 12:37:39 -08001927 if (send) {
solenberg63b34542015-09-29 06:06:31 -07001928 engine()->ApplyOptions(options_);
solenbergd53a3f92016-04-14 13:56:37 -07001929
1930 // InitRecording() may return an error if the ADM is already recording.
1931 if (!engine()->adm()->RecordingIsInitialized() &&
1932 !engine()->adm()->Recording()) {
1933 if (engine()->adm()->InitRecording() != 0) {
1934 LOG(LS_WARNING) << "Failed to initialize recording";
1935 }
1936 }
solenberg63b34542015-09-29 06:06:31 -07001937 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001938
wu@webrtc.org9dba5252013-08-05 20:36:57 +00001939 // Change the settings on each send channel.
Taylor Brandstetter1a018dc2016-03-08 12:37:39 -08001940 for (auto& kv : send_streams_) {
1941 kv.second->SetSend(send);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001942 }
wu@webrtc.org9dba5252013-08-05 20:36:57 +00001943
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001944 send_ = send;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001945}
1946
Peter Boström0c4e06b2015-10-07 12:23:21 +02001947bool WebRtcVoiceMediaChannel::SetAudioSend(uint32_t ssrc,
1948 bool enable,
solenberg1dd98f32015-09-10 01:57:14 -07001949 const AudioOptions* options,
Taylor Brandstetter1a018dc2016-03-08 12:37:39 -08001950 AudioSource* source) {
solenberg566ef242015-11-06 15:34:49 -08001951 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
solenberg1dd98f32015-09-10 01:57:14 -07001952 // TODO(solenberg): The state change should be fully rolled back if any one of
1953 // these calls fail.
Taylor Brandstetter1a018dc2016-03-08 12:37:39 -08001954 if (!SetLocalSource(ssrc, source)) {
solenberg1dd98f32015-09-10 01:57:14 -07001955 return false;
1956 }
solenbergdfc8f4f2015-10-01 02:31:10 -07001957 if (!MuteStream(ssrc, !enable)) {
solenberg1dd98f32015-09-10 01:57:14 -07001958 return false;
1959 }
solenbergdfc8f4f2015-10-01 02:31:10 -07001960 if (enable && options) {
solenberg1dd98f32015-09-10 01:57:14 -07001961 return SetOptions(*options);
1962 }
1963 return true;
1964}
1965
solenberg0a617e22015-10-20 15:49:38 -07001966int WebRtcVoiceMediaChannel::CreateVoEChannel() {
1967 int id = engine()->CreateVoEChannel();
1968 if (id == -1) {
1969 LOG_RTCERR0(CreateVoEChannel);
1970 return -1;
wu@webrtc.org9dba5252013-08-05 20:36:57 +00001971 }
mflodman3d7db262016-04-29 00:57:13 -07001972
solenberg0a617e22015-10-20 15:49:38 -07001973 return id;
wu@webrtc.org9dba5252013-08-05 20:36:57 +00001974}
1975
solenberg7add0582015-11-20 09:59:34 -08001976bool WebRtcVoiceMediaChannel::DeleteVoEChannel(int channel) {
wu@webrtc.org9dba5252013-08-05 20:36:57 +00001977 if (engine()->voe()->base()->DeleteChannel(channel) == -1) {
1978 LOG_RTCERR1(DeleteChannel, channel);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00001979 return false;
1980 }
wu@webrtc.org9dba5252013-08-05 20:36:57 +00001981 return true;
1982}
henrike@webrtc.org1e09a712013-07-26 19:17:59 +00001983
wu@webrtc.org9dba5252013-08-05 20:36:57 +00001984bool WebRtcVoiceMediaChannel::AddSendStream(const StreamParams& sp) {
Peter Boströmca8b4042016-03-08 14:24:13 -08001985 TRACE_EVENT0("webrtc", "WebRtcVoiceMediaChannel::AddSendStream");
solenberg566ef242015-11-06 15:34:49 -08001986 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
solenberg0a617e22015-10-20 15:49:38 -07001987 LOG(LS_INFO) << "AddSendStream: " << sp.ToString();
1988
1989 uint32_t ssrc = sp.first_ssrc();
1990 RTC_DCHECK(0 != ssrc);
1991
1992 if (GetSendChannelId(ssrc) != -1) {
1993 LOG(LS_ERROR) << "Stream already exists with ssrc " << ssrc;
wu@webrtc.org9dba5252013-08-05 20:36:57 +00001994 return false;
1995 }
1996
solenberg0a617e22015-10-20 15:49:38 -07001997 // Create a new channel for sending audio data.
1998 int channel = CreateVoEChannel();
1999 if (channel == -1) {
2000 return false;
wu@webrtc.org9dba5252013-08-05 20:36:57 +00002001 }
wu@webrtc.org9dba5252013-08-05 20:36:57 +00002002
solenbergc96df772015-10-21 13:01:53 -07002003 // Save the channel to send_streams_, so that RemoveSendStream() can still
wu@webrtc.org9dba5252013-08-05 20:36:57 +00002004 // delete the channel in case failure happens below.
mallinath@webrtc.org67ee6b92014-02-03 16:57:16 +00002005 webrtc::AudioTransport* audio_transport =
2006 engine()->voe()->base()->audio_transport();
mflodman3d7db262016-04-29 00:57:13 -07002007
skvlade0d46372016-04-07 22:59:22 -07002008 WebRtcAudioSendStream* stream = new WebRtcAudioSendStream(
solenberg971cab02016-06-14 10:02:41 -07002009 channel, audio_transport, ssrc, sp.cname, send_codec_spec_,
2010 send_rtp_extensions_, call_, this);
skvlade0d46372016-04-07 22:59:22 -07002011 send_streams_.insert(std::make_pair(ssrc, stream));
wu@webrtc.org9dba5252013-08-05 20:36:57 +00002012
solenberg0a617e22015-10-20 15:49:38 -07002013 // Set the current codecs to be used for the new channel. We need to do this
2014 // after adding the channel to send_channels_, because of how max bitrate is
2015 // currently being configured by SetSendCodec().
skvlade0d46372016-04-07 22:59:22 -07002016 if (HasSendCodec() && !SetSendCodecs(channel, stream->rtp_parameters())) {
solenberg0a617e22015-10-20 15:49:38 -07002017 RemoveSendStream(ssrc);
wu@webrtc.org9dba5252013-08-05 20:36:57 +00002018 return false;
2019 }
2020
2021 // At this point the channel's local SSRC has been updated. If the channel is
solenberg0a617e22015-10-20 15:49:38 -07002022 // the first send channel make sure that all the receive channels are updated
2023 // with the same SSRC in order to send receiver reports.
solenbergc96df772015-10-21 13:01:53 -07002024 if (send_streams_.size() == 1) {
solenberg0a617e22015-10-20 15:49:38 -07002025 receiver_reports_ssrc_ = ssrc;
solenberg7add0582015-11-20 09:59:34 -08002026 for (const auto& stream : recv_streams_) {
2027 int recv_channel = stream.second->channel();
solenberg0a617e22015-10-20 15:49:38 -07002028 if (engine()->voe()->rtp()->SetLocalSSRC(recv_channel, ssrc) != 0) {
solenberg7add0582015-11-20 09:59:34 -08002029 LOG_RTCERR2(SetLocalSSRC, recv_channel, ssrc);
solenberg1ac56142015-10-13 03:58:19 -07002030 return false;
wu@webrtc.org9dba5252013-08-05 20:36:57 +00002031 }
solenberg0a617e22015-10-20 15:49:38 -07002032 engine()->voe()->base()->AssociateSendChannel(recv_channel, channel);
2033 LOG(LS_INFO) << "VoiceEngine channel #" << recv_channel
2034 << " is associated with channel #" << channel << ".";
wu@webrtc.org9dba5252013-08-05 20:36:57 +00002035 }
2036 }
2037
Taylor Brandstetter1a018dc2016-03-08 12:37:39 -08002038 send_streams_[ssrc]->SetSend(send_);
2039 return true;
wu@webrtc.org9dba5252013-08-05 20:36:57 +00002040}
2041
Peter Boström0c4e06b2015-10-07 12:23:21 +02002042bool WebRtcVoiceMediaChannel::RemoveSendStream(uint32_t ssrc) {
Peter Boströmca8b4042016-03-08 14:24:13 -08002043 TRACE_EVENT0("webrtc", "WebRtcVoiceMediaChannel::RemoveSendStream");
solenberg566ef242015-11-06 15:34:49 -08002044 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
solenberg3a941542015-11-16 07:34:50 -08002045 LOG(LS_INFO) << "RemoveSendStream: " << ssrc;
2046
solenbergc96df772015-10-21 13:01:53 -07002047 auto it = send_streams_.find(ssrc);
2048 if (it == send_streams_.end()) {
wu@webrtc.org9dba5252013-08-05 20:36:57 +00002049 LOG(LS_WARNING) << "Try to remove stream with ssrc " << ssrc
2050 << " which doesn't exist.";
2051 return false;
2052 }
2053
Taylor Brandstetter1a018dc2016-03-08 12:37:39 -08002054 it->second->SetSend(false);
wu@webrtc.org9dba5252013-08-05 20:36:57 +00002055
solenberg7add0582015-11-20 09:59:34 -08002056 // Clean up and delete the send stream+channel.
Taylor Brandstetter1a018dc2016-03-08 12:37:39 -08002057 int channel = it->second->channel();
solenberg0a617e22015-10-20 15:49:38 -07002058 LOG(LS_INFO) << "Removing audio send stream " << ssrc
2059 << " with VoiceEngine channel #" << channel << ".";
solenberg7add0582015-11-20 09:59:34 -08002060 delete it->second;
2061 send_streams_.erase(it);
2062 if (!DeleteVoEChannel(channel)) {
solenberg0a617e22015-10-20 15:49:38 -07002063 return false;
wu@webrtc.org9dba5252013-08-05 20:36:57 +00002064 }
solenbergc96df772015-10-21 13:01:53 -07002065 if (send_streams_.empty()) {
Taylor Brandstetter1a018dc2016-03-08 12:37:39 -08002066 SetSend(false);
solenberg0a617e22015-10-20 15:49:38 -07002067 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002068 return true;
2069}
2070
2071bool WebRtcVoiceMediaChannel::AddRecvStream(const StreamParams& sp) {
Peter Boströmca8b4042016-03-08 14:24:13 -08002072 TRACE_EVENT0("webrtc", "WebRtcVoiceMediaChannel::AddRecvStream");
solenberg566ef242015-11-06 15:34:49 -08002073 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
solenbergd97ec302015-10-07 01:40:33 -07002074 LOG(LS_INFO) << "AddRecvStream: " << sp.ToString();
2075
solenberg0b675462015-10-09 01:37:09 -07002076 if (!ValidateStreamParams(sp)) {
wu@webrtc.org78187522013-10-07 23:32:02 +00002077 return false;
2078 }
2079
solenberg7add0582015-11-20 09:59:34 -08002080 const uint32_t ssrc = sp.first_ssrc();
solenberg0b675462015-10-09 01:37:09 -07002081 if (ssrc == 0) {
2082 LOG(LS_WARNING) << "AddRecvStream with ssrc==0 is not supported.";
2083 return false;
2084 }
2085
solenberg1ac56142015-10-13 03:58:19 -07002086 // Remove the default receive stream if one had been created with this ssrc;
2087 // we'll recreate it then.
2088 if (IsDefaultRecvStream(ssrc)) {
2089 RemoveRecvStream(ssrc);
2090 }
solenberg0b675462015-10-09 01:37:09 -07002091
solenberg7add0582015-11-20 09:59:34 -08002092 if (GetReceiveChannelId(ssrc) != -1) {
henrike@webrtc.org1e09a712013-07-26 19:17:59 +00002093 LOG(LS_ERROR) << "Stream already exists with ssrc " << ssrc;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002094 return false;
2095 }
Fredrik Solenberg4b60c732015-05-07 14:07:48 +02002096
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002097 // Create a new channel for receiving audio data.
solenberg7add0582015-11-20 09:59:34 -08002098 const int channel = CreateVoEChannel();
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002099 if (channel == -1) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002100 return false;
2101 }
Minyue2013aec2015-05-13 14:14:42 +02002102
solenberg1ac56142015-10-13 03:58:19 -07002103 // Turn off all supported codecs.
solenberg26c8c912015-11-27 04:00:25 -08002104 // TODO(solenberg): Remove once "no codecs" is the default state of a stream.
2105 for (webrtc::CodecInst voe_codec : webrtc::acm2::RentACodec::Database()) {
2106 voe_codec.pltype = -1;
2107 if (engine()->voe()->codec()->SetRecPayloadType(channel, voe_codec) == -1) {
2108 LOG_RTCERR2(SetRecPayloadType, channel, ToString(voe_codec));
2109 DeleteVoEChannel(channel);
2110 return false;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002111 }
2112 }
2113
solenberg1ac56142015-10-13 03:58:19 -07002114 // Only enable those configured for this channel.
2115 for (const auto& codec : recv_codecs_) {
solenberg72e29d22016-03-08 06:35:16 -08002116 webrtc::CodecInst voe_codec = {0};
solenberg26c8c912015-11-27 04:00:25 -08002117 if (WebRtcVoiceEngine::ToCodecInst(codec, &voe_codec)) {
solenberg1ac56142015-10-13 03:58:19 -07002118 voe_codec.pltype = codec.id;
2119 if (engine()->voe()->codec()->SetRecPayloadType(
2120 channel, voe_codec) == -1) {
2121 LOG_RTCERR2(SetRecPayloadType, channel, ToString(voe_codec));
solenberg7add0582015-11-20 09:59:34 -08002122 DeleteVoEChannel(channel);
solenberg1ac56142015-10-13 03:58:19 -07002123 return false;
2124 }
henrike@webrtc.org1e09a712013-07-26 19:17:59 +00002125 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002126 }
solenberg8fb30c32015-10-13 03:06:58 -07002127
solenberg7add0582015-11-20 09:59:34 -08002128 const int send_channel = GetSendChannelId(receiver_reports_ssrc_);
2129 if (send_channel != -1) {
2130 // Associate receive channel with first send channel (so the receive channel
2131 // can obtain RTT from the send channel)
2132 engine()->voe()->base()->AssociateSendChannel(channel, send_channel);
2133 LOG(LS_INFO) << "VoiceEngine channel #" << channel
2134 << " is associated with channel #" << send_channel << ".";
buildbot@webrtc.org150835e2014-05-06 15:54:38 +00002135 }
2136
stefanba4c0e42016-02-04 04:12:24 -08002137 recv_streams_.insert(std::make_pair(
2138 ssrc, new WebRtcAudioReceiveStream(channel, ssrc, receiver_reports_ssrc_,
solenberg72e29d22016-03-08 06:35:16 -08002139 recv_transport_cc_enabled_,
2140 sp.sync_label, recv_rtp_extensions_,
ossu29b1a8d2016-06-13 07:34:51 -07002141 call_, this,
2142 engine()->decoder_factory_)));
solenberg7add0582015-11-20 09:59:34 -08002143
solenberg72e29d22016-03-08 06:35:16 -08002144 SetNack(channel, send_codec_spec_.nack_enabled);
solenberg1ac56142015-10-13 03:58:19 -07002145 SetPlayout(channel, playout_);
solenberg7add0582015-11-20 09:59:34 -08002146
solenberg1ac56142015-10-13 03:58:19 -07002147 return true;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002148}
2149
Peter Boström0c4e06b2015-10-07 12:23:21 +02002150bool WebRtcVoiceMediaChannel::RemoveRecvStream(uint32_t ssrc) {
Peter Boströmca8b4042016-03-08 14:24:13 -08002151 TRACE_EVENT0("webrtc", "WebRtcVoiceMediaChannel::RemoveRecvStream");
solenberg566ef242015-11-06 15:34:49 -08002152 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
solenbergd97ec302015-10-07 01:40:33 -07002153 LOG(LS_INFO) << "RemoveRecvStream: " << ssrc;
2154
solenberg7add0582015-11-20 09:59:34 -08002155 const auto it = recv_streams_.find(ssrc);
2156 if (it == recv_streams_.end()) {
wu@webrtc.org9dba5252013-08-05 20:36:57 +00002157 LOG(LS_WARNING) << "Try to remove stream with ssrc " << ssrc
2158 << " which doesn't exist.";
henrike@webrtc.org1e09a712013-07-26 19:17:59 +00002159 return false;
wu@webrtc.org9dba5252013-08-05 20:36:57 +00002160 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002161
solenberg1ac56142015-10-13 03:58:19 -07002162 // Deregister default channel, if that's the one being destroyed.
2163 if (IsDefaultRecvStream(ssrc)) {
2164 default_recv_ssrc_ = -1;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002165 }
henrike@webrtc.org1e09a712013-07-26 19:17:59 +00002166
solenberg7add0582015-11-20 09:59:34 -08002167 const int channel = it->second->channel();
2168
2169 // Clean up and delete the receive stream+channel.
2170 LOG(LS_INFO) << "Removing audio receive stream " << ssrc
mallinath@webrtc.org67ee6b92014-02-03 16:57:16 +00002171 << " with VoiceEngine channel #" << channel << ".";
Tommif888bb52015-12-12 01:37:01 +01002172 it->second->SetRawAudioSink(nullptr);
solenberg7add0582015-11-20 09:59:34 -08002173 delete it->second;
2174 recv_streams_.erase(it);
2175 return DeleteVoEChannel(channel);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002176}
2177
Taylor Brandstetter1a018dc2016-03-08 12:37:39 -08002178bool WebRtcVoiceMediaChannel::SetLocalSource(uint32_t ssrc,
2179 AudioSource* source) {
solenbergc96df772015-10-21 13:01:53 -07002180 auto it = send_streams_.find(ssrc);
2181 if (it == send_streams_.end()) {
Taylor Brandstetter1a018dc2016-03-08 12:37:39 -08002182 if (source) {
2183 // Return an error if trying to set a valid source with an invalid ssrc.
2184 LOG(LS_ERROR) << "SetLocalSource failed with ssrc " << ssrc;
wu@webrtc.org9dba5252013-08-05 20:36:57 +00002185 return false;
2186 }
2187
2188 // The channel likely has gone away, do nothing.
henrike@webrtc.org1e09a712013-07-26 19:17:59 +00002189 return true;
henrike@webrtc.org1e09a712013-07-26 19:17:59 +00002190 }
2191
Taylor Brandstetter1a018dc2016-03-08 12:37:39 -08002192 if (source) {
2193 it->second->SetSource(source);
solenberg1ac56142015-10-13 03:58:19 -07002194 } else {
Taylor Brandstetter1a018dc2016-03-08 12:37:39 -08002195 it->second->ClearSource();
solenberg1ac56142015-10-13 03:58:19 -07002196 }
henrike@webrtc.org1e09a712013-07-26 19:17:59 +00002197
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002198 return true;
2199}
2200
2201bool WebRtcVoiceMediaChannel::GetActiveStreams(
2202 AudioInfo::StreamList* actives) {
solenberg566ef242015-11-06 15:34:49 -08002203 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002204 actives->clear();
solenberg7add0582015-11-20 09:59:34 -08002205 for (const auto& ch : recv_streams_) {
Fredrik Solenbergaf9fb212015-08-26 10:45:53 +02002206 int level = GetOutputLevel(ch.second->channel());
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002207 if (level > 0) {
Fredrik Solenbergaf9fb212015-08-26 10:45:53 +02002208 actives->push_back(std::make_pair(ch.first, level));
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002209 }
2210 }
2211 return true;
2212}
2213
2214int WebRtcVoiceMediaChannel::GetOutputLevel() {
solenberg566ef242015-11-06 15:34:49 -08002215 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
solenberg1ac56142015-10-13 03:58:19 -07002216 int highest = 0;
solenberg7add0582015-11-20 09:59:34 -08002217 for (const auto& ch : recv_streams_) {
solenberg8fb30c32015-10-13 03:06:58 -07002218 highest = std::max(GetOutputLevel(ch.second->channel()), highest);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002219 }
2220 return highest;
2221}
2222
2223int WebRtcVoiceMediaChannel::GetTimeSinceLastTyping() {
2224 int ret;
2225 if (engine()->voe()->processing()->TimeSinceLastTyping(ret) == -1) {
2226 // In case of error, log the info and continue
2227 LOG_RTCERR0(TimeSinceLastTyping);
2228 ret = -1;
2229 } else {
2230 ret *= 1000; // We return ms, webrtc returns seconds.
2231 }
2232 return ret;
2233}
2234
2235void WebRtcVoiceMediaChannel::SetTypingDetectionParameters(int time_window,
2236 int cost_per_typing, int reporting_threshold, int penalty_decay,
2237 int type_event_delay) {
2238 if (engine()->voe()->processing()->SetTypingDetectionParameters(
2239 time_window, cost_per_typing,
2240 reporting_threshold, penalty_decay, type_event_delay) == -1) {
2241 // In case of error, log the info and continue
2242 LOG_RTCERR5(SetTypingDetectionParameters, time_window,
2243 cost_per_typing, reporting_threshold, penalty_decay,
2244 type_event_delay);
2245 }
2246}
2247
solenberg4bac9c52015-10-09 02:32:53 -07002248bool WebRtcVoiceMediaChannel::SetOutputVolume(uint32_t ssrc, double volume) {
solenberg566ef242015-11-06 15:34:49 -08002249 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
solenberg1ac56142015-10-13 03:58:19 -07002250 if (ssrc == 0) {
2251 default_recv_volume_ = volume;
2252 if (default_recv_ssrc_ == -1) {
2253 return true;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002254 }
solenberg1ac56142015-10-13 03:58:19 -07002255 ssrc = static_cast<uint32_t>(default_recv_ssrc_);
2256 }
2257 int ch_id = GetReceiveChannelId(ssrc);
2258 if (ch_id < 0) {
2259 LOG(LS_WARNING) << "Cannot find channel for ssrc:" << ssrc;
2260 return false;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002261 }
2262
solenberg1ac56142015-10-13 03:58:19 -07002263 if (-1 == engine()->voe()->volume()->SetChannelOutputVolumeScaling(ch_id,
2264 volume)) {
2265 LOG_RTCERR2(SetChannelOutputVolumeScaling, ch_id, volume);
2266 return false;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002267 }
solenberg1ac56142015-10-13 03:58:19 -07002268 LOG(LS_INFO) << "SetOutputVolume to " << volume
2269 << " for channel " << ch_id << " and ssrc " << ssrc;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002270 return true;
2271}
2272
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002273bool WebRtcVoiceMediaChannel::CanInsertDtmf() {
Fredrik Solenbergb5727682015-12-04 15:22:19 +01002274 return dtmf_payload_type_ ? true : false;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002275}
2276
solenberg1d63dd02015-12-02 12:35:09 -08002277bool WebRtcVoiceMediaChannel::InsertDtmf(uint32_t ssrc, int event,
2278 int duration) {
solenberg566ef242015-11-06 15:34:49 -08002279 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
Fredrik Solenbergb5727682015-12-04 15:22:19 +01002280 LOG(LS_INFO) << "WebRtcVoiceMediaChannel::InsertDtmf";
2281 if (!dtmf_payload_type_) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002282 return false;
2283 }
2284
Fredrik Solenbergb5727682015-12-04 15:22:19 +01002285 // Figure out which WebRtcAudioSendStream to send the event on.
2286 auto it = ssrc != 0 ? send_streams_.find(ssrc) : send_streams_.begin();
2287 if (it == send_streams_.end()) {
2288 LOG(LS_WARNING) << "The specified ssrc " << ssrc << " is not in use.";
solenberg1d63dd02015-12-02 12:35:09 -08002289 return false;
2290 }
Fredrik Solenbergb5727682015-12-04 15:22:19 +01002291 if (event < kMinTelephoneEventCode ||
2292 event > kMaxTelephoneEventCode) {
2293 LOG(LS_WARNING) << "DTMF event code " << event << " out of range.";
solenberg1d63dd02015-12-02 12:35:09 -08002294 return false;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002295 }
Fredrik Solenbergb5727682015-12-04 15:22:19 +01002296 if (duration < kMinTelephoneEventDuration ||
2297 duration > kMaxTelephoneEventDuration) {
2298 LOG(LS_WARNING) << "DTMF event duration " << duration << " out of range.";
2299 return false;
2300 }
2301 return it->second->SendTelephoneEvent(*dtmf_payload_type_, event, duration);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002302}
2303
wu@webrtc.orga9890802013-12-13 00:21:03 +00002304void WebRtcVoiceMediaChannel::OnPacketReceived(
jbaucheec21bd2016-03-20 06:15:43 -07002305 rtc::CopyOnWriteBuffer* packet, const rtc::PacketTime& packet_time) {
solenberg566ef242015-11-06 15:34:49 -08002306 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
Fredrik Solenberg4b60c732015-05-07 14:07:48 +02002307
mflodman3d7db262016-04-29 00:57:13 -07002308 const webrtc::PacketTime webrtc_packet_time(packet_time.timestamp,
2309 packet_time.not_before);
2310 webrtc::PacketReceiver::DeliveryStatus delivery_result =
2311 call_->Receiver()->DeliverPacket(webrtc::MediaType::AUDIO,
2312 packet->cdata(), packet->size(),
2313 webrtc_packet_time);
mflodman3d7db262016-04-29 00:57:13 -07002314 if (delivery_result != webrtc::PacketReceiver::DELIVERY_UNKNOWN_SSRC) {
2315 return;
2316 }
2317
2318 // Create a default receive stream for this unsignalled and previously not
2319 // received ssrc. If there already is a default receive stream, delete it.
2320 // See: https://bugs.chromium.org/p/webrtc/issues/detail?id=5208
solenberg1ac56142015-10-13 03:58:19 -07002321 uint32_t ssrc = 0;
jbaucheec21bd2016-03-20 06:15:43 -07002322 if (!GetRtpSsrc(packet->cdata(), packet->size(), &ssrc)) {
solenberg1ac56142015-10-13 03:58:19 -07002323 return;
2324 }
2325
mflodman3d7db262016-04-29 00:57:13 -07002326 if (default_recv_ssrc_ != -1) {
2327 LOG(LS_INFO) << "Removing default receive stream with ssrc "
2328 << default_recv_ssrc_;
2329 RTC_DCHECK_NE(ssrc, default_recv_ssrc_);
2330 RemoveRecvStream(default_recv_ssrc_);
2331 default_recv_ssrc_ = -1;
solenberg1ac56142015-10-13 03:58:19 -07002332 }
2333
mflodman3d7db262016-04-29 00:57:13 -07002334 StreamParams sp;
2335 sp.ssrcs.push_back(ssrc);
2336 LOG(LS_INFO) << "Creating default receive stream for SSRC=" << ssrc << ".";
2337 if (!AddRecvStream(sp)) {
2338 LOG(LS_WARNING) << "Could not create default receive stream.";
2339 return;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002340 }
mflodman3d7db262016-04-29 00:57:13 -07002341 default_recv_ssrc_ = ssrc;
2342 SetOutputVolume(default_recv_ssrc_, default_recv_volume_);
2343 if (default_sink_) {
2344 std::unique_ptr<webrtc::AudioSinkInterface> proxy_sink(
2345 new ProxySink(default_sink_.get()));
2346 SetRawAudioSink(default_recv_ssrc_, std::move(proxy_sink));
2347 }
2348 delivery_result = call_->Receiver()->DeliverPacket(webrtc::MediaType::AUDIO,
2349 packet->cdata(),
2350 packet->size(),
2351 webrtc_packet_time);
2352 RTC_DCHECK_NE(webrtc::PacketReceiver::DELIVERY_UNKNOWN_SSRC, delivery_result);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002353}
2354
wu@webrtc.orga9890802013-12-13 00:21:03 +00002355void WebRtcVoiceMediaChannel::OnRtcpReceived(
jbaucheec21bd2016-03-20 06:15:43 -07002356 rtc::CopyOnWriteBuffer* packet, const rtc::PacketTime& packet_time) {
solenberg566ef242015-11-06 15:34:49 -08002357 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
Fredrik Solenberg4b60c732015-05-07 14:07:48 +02002358
Fredrik Solenberg709ed672015-09-15 12:26:33 +02002359 // Forward packet to Call as well.
2360 const webrtc::PacketTime webrtc_packet_time(packet_time.timestamp,
2361 packet_time.not_before);
2362 call_->Receiver()->DeliverPacket(webrtc::MediaType::AUDIO,
jbaucheec21bd2016-03-20 06:15:43 -07002363 packet->cdata(), packet->size(), webrtc_packet_time);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002364}
2365
Honghai Zhangcc411c02016-03-29 17:27:21 -07002366void WebRtcVoiceMediaChannel::OnNetworkRouteChanged(
2367 const std::string& transport_name,
Honghai Zhang0e533ef2016-04-19 15:41:36 -07002368 const rtc::NetworkRoute& network_route) {
2369 call_->OnNetworkRouteChanged(transport_name, network_route);
Honghai Zhangcc411c02016-03-29 17:27:21 -07002370}
2371
Peter Boström0c4e06b2015-10-07 12:23:21 +02002372bool WebRtcVoiceMediaChannel::MuteStream(uint32_t ssrc, bool muted) {
solenberg566ef242015-11-06 15:34:49 -08002373 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
solenberg0a617e22015-10-20 15:49:38 -07002374 int channel = GetSendChannelId(ssrc);
wu@webrtc.org9dba5252013-08-05 20:36:57 +00002375 if (channel == -1) {
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002376 LOG(LS_WARNING) << "The specified ssrc " << ssrc << " is not in use.";
2377 return false;
2378 }
wu@webrtc.org9dba5252013-08-05 20:36:57 +00002379 if (engine()->voe()->volume()->SetInputMute(channel, muted) == -1) {
2380 LOG_RTCERR2(SetInputMute, channel, muted);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002381 return false;
2382 }
buildbot@webrtc.org6b21b712014-07-31 15:08:53 +00002383 // We set the AGC to mute state only when all the channels are muted.
2384 // This implementation is not ideal, instead we should signal the AGC when
2385 // the mic channel is muted/unmuted. We can't do it today because there
2386 // is no good way to know which stream is mapping to the mic channel.
2387 bool all_muted = muted;
solenbergc96df772015-10-21 13:01:53 -07002388 for (const auto& ch : send_streams_) {
Fredrik Solenbergaf9fb212015-08-26 10:45:53 +02002389 if (!all_muted) {
2390 break;
2391 }
2392 if (engine()->voe()->volume()->GetInputMute(ch.second->channel(),
buildbot@webrtc.org6b21b712014-07-31 15:08:53 +00002393 all_muted)) {
Fredrik Solenbergaf9fb212015-08-26 10:45:53 +02002394 LOG_RTCERR1(GetInputMute, ch.second->channel());
buildbot@webrtc.org6b21b712014-07-31 15:08:53 +00002395 return false;
2396 }
2397 }
2398
2399 webrtc::AudioProcessing* ap = engine()->voe()->base()->audio_processing();
solenberg0a617e22015-10-20 15:49:38 -07002400 if (ap) {
buildbot@webrtc.org6b21b712014-07-31 15:08:53 +00002401 ap->set_output_will_be_muted(all_muted);
solenberg0a617e22015-10-20 15:49:38 -07002402 }
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002403 return true;
2404}
2405
deadbeef80346142016-04-27 14:17:10 -07002406bool WebRtcVoiceMediaChannel::SetMaxSendBitrate(int bps) {
2407 LOG(LS_INFO) << "WebRtcVoiceMediaChannel::SetMaxSendBitrate.";
2408 max_send_bitrate_bps_ = bps;
skvlade0d46372016-04-07 22:59:22 -07002409
2410 for (const auto& kv : send_streams_) {
Taylor Brandstetterdb0cd9e2016-05-16 11:40:30 -07002411 if (!SetChannelSendParameters(kv.second->channel(),
2412 kv.second->rtp_parameters())) {
skvlade0d46372016-04-07 22:59:22 -07002413 return false;
2414 }
2415 }
2416 return true;
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00002417}
2418
Taylor Brandstetterdb0cd9e2016-05-16 11:40:30 -07002419bool WebRtcVoiceMediaChannel::SetChannelSendParameters(
skvlade0d46372016-04-07 22:59:22 -07002420 int channel,
2421 const webrtc::RtpParameters& parameters) {
2422 RTC_CHECK_EQ(1UL, parameters.encodings.size());
Taylor Brandstetter0cd086b2016-04-20 16:23:10 -07002423 // TODO(deadbeef): Handle setting parameters with a list of codecs in a
2424 // different order (which should change the send codec).
deadbeef80346142016-04-27 14:17:10 -07002425 return SetMaxSendBitrate(
2426 channel, MinPositive(max_send_bitrate_bps_,
2427 parameters.encodings[0].max_bitrate_bps));
skvlade0d46372016-04-07 22:59:22 -07002428}
sergeyu@chromium.org4b26e2e2014-01-15 23:15:54 +00002429
deadbeef80346142016-04-27 14:17:10 -07002430bool WebRtcVoiceMediaChannel::SetMaxSendBitrate(int channel, int bps) {
skvlade0d46372016-04-07 22:59:22 -07002431 // Bitrate is auto by default.
2432 // TODO(bemasc): Fix this so that if SetMaxSendBandwidth(50) is followed by
2433 // SetMaxSendBandwith(0), the second call removes the previous limit.
deadbeef80346142016-04-27 14:17:10 -07002434 if (bps <= 0) {
skvlade0d46372016-04-07 22:59:22 -07002435 return true;
deadbeef80346142016-04-27 14:17:10 -07002436 }
wu@webrtc.org1d1ffc92013-10-16 18:12:02 +00002437
solenberg72e29d22016-03-08 06:35:16 -08002438 if (!HasSendCodec()) {
wu@webrtc.org1d1ffc92013-10-16 18:12:02 +00002439 LOG(LS_INFO) << "The send codec has not been set up yet. "
minyue@webrtc.org26236952014-10-29 02:27:08 +00002440 << "The send bitrate setting will be applied later.";
wu@webrtc.org1d1ffc92013-10-16 18:12:02 +00002441 return true;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002442 }
2443
solenberg72e29d22016-03-08 06:35:16 -08002444 webrtc::CodecInst codec = send_codec_spec_.codec_inst;
solenberg26c8c912015-11-27 04:00:25 -08002445 bool is_multi_rate = WebRtcVoiceCodecs::IsCodecMultiRate(codec);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002446
2447 if (is_multi_rate) {
2448 // If codec is multi-rate then just set the bitrate.
deadbeef80346142016-04-27 14:17:10 -07002449 int max_bitrate_bps = WebRtcVoiceCodecs::MaxBitrateBps(codec);
2450 codec.rate = std::min(bps, max_bitrate_bps);
2451 LOG(LS_INFO) << "Setting codec " << codec.plname << " to bitrate " << bps
2452 << " bps.";
skvlade0d46372016-04-07 22:59:22 -07002453 if (!SetSendCodec(channel, codec)) {
deadbeef80346142016-04-27 14:17:10 -07002454 LOG(LS_ERROR) << "Failed to set codec " << codec.plname << " to bitrate "
2455 << bps << " bps.";
skvlade0d46372016-04-07 22:59:22 -07002456 return false;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002457 }
2458 return true;
2459 } else {
2460 // If codec is not multi-rate and |bps| is less than the fixed bitrate
2461 // then fail. If codec is not multi-rate and |bps| exceeds or equal the
2462 // fixed bitrate then ignore.
2463 if (bps < codec.rate) {
deadbeef80346142016-04-27 14:17:10 -07002464 LOG(LS_ERROR) << "Failed to set codec " << codec.plname << " to bitrate "
2465 << bps << " bps"
2466 << ", requires at least " << codec.rate << " bps.";
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002467 return false;
2468 }
2469 return true;
2470 }
2471}
2472
skvlad7a43d252016-03-22 15:32:27 -07002473void WebRtcVoiceMediaChannel::OnReadyToSend(bool ready) {
2474 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
2475 LOG(LS_VERBOSE) << "OnReadyToSend: " << (ready ? "Ready." : "Not ready.");
2476 call_->SignalChannelNetworkState(
2477 webrtc::MediaType::AUDIO,
2478 ready ? webrtc::kNetworkUp : webrtc::kNetworkDown);
2479}
2480
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002481bool WebRtcVoiceMediaChannel::GetStats(VoiceMediaInfo* info) {
Peter Boströmca8b4042016-03-08 14:24:13 -08002482 TRACE_EVENT0("webrtc", "WebRtcVoiceMediaChannel::GetStats");
solenberg566ef242015-11-06 15:34:49 -08002483 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
solenberg85a04962015-10-27 03:35:21 -07002484 RTC_DCHECK(info);
solenbergd97ec302015-10-07 01:40:33 -07002485
solenberg85a04962015-10-27 03:35:21 -07002486 // Get SSRC and stats for each sender.
2487 RTC_DCHECK(info->senders.size() == 0);
2488 for (const auto& stream : send_streams_) {
2489 webrtc::AudioSendStream::Stats stats = stream.second->GetStats();
wu@webrtc.org9dba5252013-08-05 20:36:57 +00002490 VoiceSenderInfo sinfo;
solenberg85a04962015-10-27 03:35:21 -07002491 sinfo.add_ssrc(stats.local_ssrc);
2492 sinfo.bytes_sent = stats.bytes_sent;
2493 sinfo.packets_sent = stats.packets_sent;
2494 sinfo.packets_lost = stats.packets_lost;
2495 sinfo.fraction_lost = stats.fraction_lost;
2496 sinfo.codec_name = stats.codec_name;
2497 sinfo.ext_seqnum = stats.ext_seqnum;
2498 sinfo.jitter_ms = stats.jitter_ms;
2499 sinfo.rtt_ms = stats.rtt_ms;
2500 sinfo.audio_level = stats.audio_level;
2501 sinfo.aec_quality_min = stats.aec_quality_min;
2502 sinfo.echo_delay_median_ms = stats.echo_delay_median_ms;
2503 sinfo.echo_delay_std_ms = stats.echo_delay_std_ms;
2504 sinfo.echo_return_loss = stats.echo_return_loss;
2505 sinfo.echo_return_loss_enhancement = stats.echo_return_loss_enhancement;
Taylor Brandstetter1a018dc2016-03-08 12:37:39 -08002506 sinfo.typing_noise_detected = (send_ ? stats.typing_noise_detected : false);
wu@webrtc.org9dba5252013-08-05 20:36:57 +00002507 info->senders.push_back(sinfo);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002508 }
2509
solenberg85a04962015-10-27 03:35:21 -07002510 // Get SSRC and stats for each receiver.
2511 RTC_DCHECK(info->receivers.size() == 0);
solenberg7add0582015-11-20 09:59:34 -08002512 for (const auto& stream : recv_streams_) {
Fredrik Solenberg4f4ec0a2015-10-22 10:49:27 +02002513 webrtc::AudioReceiveStream::Stats stats = stream.second->GetStats();
2514 VoiceReceiverInfo rinfo;
2515 rinfo.add_ssrc(stats.remote_ssrc);
2516 rinfo.bytes_rcvd = stats.bytes_rcvd;
2517 rinfo.packets_rcvd = stats.packets_rcvd;
2518 rinfo.packets_lost = stats.packets_lost;
2519 rinfo.fraction_lost = stats.fraction_lost;
2520 rinfo.codec_name = stats.codec_name;
2521 rinfo.ext_seqnum = stats.ext_seqnum;
2522 rinfo.jitter_ms = stats.jitter_ms;
2523 rinfo.jitter_buffer_ms = stats.jitter_buffer_ms;
2524 rinfo.jitter_buffer_preferred_ms = stats.jitter_buffer_preferred_ms;
2525 rinfo.delay_estimate_ms = stats.delay_estimate_ms;
2526 rinfo.audio_level = stats.audio_level;
2527 rinfo.expand_rate = stats.expand_rate;
2528 rinfo.speech_expand_rate = stats.speech_expand_rate;
2529 rinfo.secondary_decoded_rate = stats.secondary_decoded_rate;
2530 rinfo.accelerate_rate = stats.accelerate_rate;
2531 rinfo.preemptive_expand_rate = stats.preemptive_expand_rate;
2532 rinfo.decoding_calls_to_silence_generator =
2533 stats.decoding_calls_to_silence_generator;
2534 rinfo.decoding_calls_to_neteq = stats.decoding_calls_to_neteq;
2535 rinfo.decoding_normal = stats.decoding_normal;
2536 rinfo.decoding_plc = stats.decoding_plc;
2537 rinfo.decoding_cng = stats.decoding_cng;
2538 rinfo.decoding_plc_cng = stats.decoding_plc_cng;
2539 rinfo.capture_start_ntp_time_ms = stats.capture_start_ntp_time_ms;
2540 info->receivers.push_back(rinfo);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002541 }
2542
2543 return true;
2544}
2545
Tommif888bb52015-12-12 01:37:01 +01002546void WebRtcVoiceMediaChannel::SetRawAudioSink(
2547 uint32_t ssrc,
kwiberg686a8ef2016-02-26 03:00:35 -08002548 std::unique_ptr<webrtc::AudioSinkInterface> sink) {
Tommif888bb52015-12-12 01:37:01 +01002549 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
deadbeef884f5852016-01-15 09:20:04 -08002550 LOG(LS_VERBOSE) << "WebRtcVoiceMediaChannel::SetRawAudioSink: ssrc:" << ssrc
2551 << " " << (sink ? "(ptr)" : "NULL");
2552 if (ssrc == 0) {
2553 if (default_recv_ssrc_ != -1) {
kwiberg686a8ef2016-02-26 03:00:35 -08002554 std::unique_ptr<webrtc::AudioSinkInterface> proxy_sink(
deadbeef884f5852016-01-15 09:20:04 -08002555 sink ? new ProxySink(sink.get()) : nullptr);
2556 SetRawAudioSink(default_recv_ssrc_, std::move(proxy_sink));
2557 }
2558 default_sink_ = std::move(sink);
2559 return;
2560 }
Tommif888bb52015-12-12 01:37:01 +01002561 const auto it = recv_streams_.find(ssrc);
2562 if (it == recv_streams_.end()) {
2563 LOG(LS_WARNING) << "SetRawAudioSink: no recv stream" << ssrc;
2564 return;
2565 }
deadbeef2d110be2016-01-13 12:00:26 -08002566 it->second->SetRawAudioSink(std::move(sink));
Tommif888bb52015-12-12 01:37:01 +01002567}
2568
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002569int WebRtcVoiceMediaChannel::GetOutputLevel(int channel) {
solenbergd97ec302015-10-07 01:40:33 -07002570 unsigned int ulevel = 0;
2571 int ret = engine()->voe()->volume()->GetSpeechOutputLevel(channel, ulevel);
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002572 return (ret == 0) ? static_cast<int>(ulevel) : -1;
2573}
2574
Peter Boström0c4e06b2015-10-07 12:23:21 +02002575int WebRtcVoiceMediaChannel::GetReceiveChannelId(uint32_t ssrc) const {
solenberg566ef242015-11-06 15:34:49 -08002576 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
solenberg7add0582015-11-20 09:59:34 -08002577 const auto it = recv_streams_.find(ssrc);
2578 if (it != recv_streams_.end()) {
mallinath@webrtc.org67ee6b92014-02-03 16:57:16 +00002579 return it->second->channel();
solenberg8fb30c32015-10-13 03:06:58 -07002580 }
solenberg1ac56142015-10-13 03:58:19 -07002581 return -1;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002582}
2583
Peter Boström0c4e06b2015-10-07 12:23:21 +02002584int WebRtcVoiceMediaChannel::GetSendChannelId(uint32_t ssrc) const {
solenberg566ef242015-11-06 15:34:49 -08002585 RTC_DCHECK(worker_thread_checker_.CalledOnValidThread());
solenbergc96df772015-10-21 13:01:53 -07002586 const auto it = send_streams_.find(ssrc);
2587 if (it != send_streams_.end()) {
mallinath@webrtc.org67ee6b92014-02-03 16:57:16 +00002588 return it->second->channel();
solenberg8fb30c32015-10-13 03:06:58 -07002589 }
wu@webrtc.org9dba5252013-08-05 20:36:57 +00002590 return -1;
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002591}
2592
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002593bool WebRtcVoiceMediaChannel::SetPlayout(int channel, bool playout) {
2594 if (playout) {
2595 LOG(LS_INFO) << "Starting playout for channel #" << channel;
2596 if (engine()->voe()->base()->StartPlayout(channel) == -1) {
2597 LOG_RTCERR1(StartPlayout, channel);
2598 return false;
2599 }
2600 } else {
2601 LOG(LS_INFO) << "Stopping playout for channel #" << channel;
2602 engine()->voe()->base()->StopPlayout(channel);
2603 }
2604 return true;
2605}
henrike@webrtc.org28e20752013-07-10 00:45:36 +00002606} // namespace cricket
2607
2608#endif // HAVE_WEBRTC_VOICE