blob: e811a273dee0d8de92e3852f0ce732c39419aff8 [file] [log] [blame]
andrew@webrtc.orgb015cbe2012-10-22 18:19:23 +00001/*
2 * Copyright (c) 2012 The WebRTC project authors. All Rights Reserved.
3 *
4 * 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.
9 */
10
turaj@webrtc.orgead8a5b2013-02-12 21:42:18 +000011#include "webrtc/voice_engine/channel.h"
andrew@webrtc.orgb015cbe2012-10-22 18:19:23 +000012
wu@webrtc.org81f8df92014-06-05 20:34:08 +000013#include "webrtc/base/timeutils.h"
minyue@webrtc.org4489c512013-09-12 17:03:00 +000014#include "webrtc/common.h"
turaj@webrtc.orgead8a5b2013-02-12 21:42:18 +000015#include "webrtc/modules/audio_device/include/audio_device.h"
16#include "webrtc/modules/audio_processing/include/audio_processing.h"
henrik.lundin@webrtc.orga5db8e32014-03-20 12:04:09 +000017#include "webrtc/modules/interface/module_common_types.h"
wu@webrtc.org7fc75bb2013-08-15 23:38:54 +000018#include "webrtc/modules/rtp_rtcp/interface/receive_statistics.h"
wu@webrtc.org881a32d2014-05-20 22:55:01 +000019#include "webrtc/modules/rtp_rtcp/interface/remote_ntp_time_estimator.h"
wu@webrtc.org7fc75bb2013-08-15 23:38:54 +000020#include "webrtc/modules/rtp_rtcp/interface/rtp_payload_registry.h"
21#include "webrtc/modules/rtp_rtcp/interface/rtp_receiver.h"
22#include "webrtc/modules/rtp_rtcp/source/rtp_receiver_strategy.h"
turaj@webrtc.orgead8a5b2013-02-12 21:42:18 +000023#include "webrtc/modules/utility/interface/audio_frame_operations.h"
24#include "webrtc/modules/utility/interface/process_thread.h"
25#include "webrtc/modules/utility/interface/rtp_dump.h"
26#include "webrtc/system_wrappers/interface/critical_section_wrapper.h"
27#include "webrtc/system_wrappers/interface/logging.h"
28#include "webrtc/system_wrappers/interface/trace.h"
solenberg@webrtc.orgfec6b6e2014-03-24 10:38:25 +000029#include "webrtc/video_engine/include/vie_network.h"
turaj@webrtc.orgead8a5b2013-02-12 21:42:18 +000030#include "webrtc/voice_engine/include/voe_base.h"
31#include "webrtc/voice_engine/include/voe_external_media.h"
32#include "webrtc/voice_engine/include/voe_rtp_rtcp.h"
33#include "webrtc/voice_engine/output_mixer.h"
34#include "webrtc/voice_engine/statistics.h"
35#include "webrtc/voice_engine/transmit_mixer.h"
36#include "webrtc/voice_engine/utility.h"
andrew@webrtc.orgb015cbe2012-10-22 18:19:23 +000037
38#if defined(_WIN32)
39#include <Qos.h>
40#endif
41
andrew@webrtc.orgd898c012012-11-14 19:07:54 +000042namespace webrtc {
43namespace voe {
andrew@webrtc.orgb015cbe2012-10-22 18:19:23 +000044
sprang@webrtc.org4f1f5fa2013-12-19 13:26:02 +000045// Extend the default RTCP statistics struct with max_jitter, defined as the
46// maximum jitter value seen in an RTCP report block.
47struct ChannelStatistics : public RtcpStatistics {
48 ChannelStatistics() : rtcp(), max_jitter(0) {}
49
50 RtcpStatistics rtcp;
51 uint32_t max_jitter;
52};
53
54// Statistics callback, called at each generation of a new RTCP report block.
55class StatisticsProxy : public RtcpStatisticsCallback {
56 public:
57 StatisticsProxy(uint32_t ssrc)
58 : stats_lock_(CriticalSectionWrapper::CreateCriticalSection()),
59 ssrc_(ssrc) {}
60 virtual ~StatisticsProxy() {}
61
62 virtual void StatisticsUpdated(const RtcpStatistics& statistics,
63 uint32_t ssrc) OVERRIDE {
64 if (ssrc != ssrc_)
65 return;
66
67 CriticalSectionScoped cs(stats_lock_.get());
68 stats_.rtcp = statistics;
69 if (statistics.jitter > stats_.max_jitter) {
70 stats_.max_jitter = statistics.jitter;
71 }
72 }
73
74 void ResetStatistics() {
75 CriticalSectionScoped cs(stats_lock_.get());
76 stats_ = ChannelStatistics();
77 }
78
79 ChannelStatistics GetStats() {
80 CriticalSectionScoped cs(stats_lock_.get());
81 return stats_;
82 }
83
84 private:
85 // StatisticsUpdated calls are triggered from threads in the RTP module,
86 // while GetStats calls can be triggered from the public voice engine API,
87 // hence synchronization is needed.
88 scoped_ptr<CriticalSectionWrapper> stats_lock_;
89 const uint32_t ssrc_;
90 ChannelStatistics stats_;
91};
92
minyue@webrtc.orgdd671de2014-05-28 09:52:06 +000093class VoEBitrateObserver : public BitrateObserver {
94 public:
95 explicit VoEBitrateObserver(Channel* owner)
96 : owner_(owner) {}
97 virtual ~VoEBitrateObserver() {}
98
99 // Implements BitrateObserver.
100 virtual void OnNetworkChanged(const uint32_t bitrate_bps,
101 const uint8_t fraction_lost,
102 const uint32_t rtt) OVERRIDE {
103 // |fraction_lost| has a scale of 0 - 255.
104 owner_->OnNetworkChanged(bitrate_bps, fraction_lost, rtt);
105 }
106
107 private:
108 Channel* owner_;
109};
110
pbos@webrtc.org54f03bc2013-04-09 10:09:10 +0000111int32_t
andrew@webrtc.orgb015cbe2012-10-22 18:19:23 +0000112Channel::SendData(FrameType frameType,
pbos@webrtc.org54f03bc2013-04-09 10:09:10 +0000113 uint8_t payloadType,
114 uint32_t timeStamp,
115 const uint8_t* payloadData,
116 uint16_t payloadSize,
andrew@webrtc.orgb015cbe2012-10-22 18:19:23 +0000117 const RTPFragmentationHeader* fragmentation)
118{
119 WEBRTC_TRACE(kTraceStream, kTraceVoice, VoEId(_instanceId,_channelId),
120 "Channel::SendData(frameType=%u, payloadType=%u, timeStamp=%u,"
121 " payloadSize=%u, fragmentation=0x%x)",
122 frameType, payloadType, timeStamp, payloadSize, fragmentation);
123
124 if (_includeAudioLevelIndication)
125 {
andrew@webrtc.orgb015cbe2012-10-22 18:19:23 +0000126 // Store current audio level in the RTP/RTCP module.
127 // The level will be used in combination with voice-activity state
128 // (frameType) to add an RTP header extension
andrew@webrtc.org3cd0f7c2014-05-05 18:22:21 +0000129 _rtpRtcpModule->SetAudioLevel(rms_level_.RMS());
andrew@webrtc.orgb015cbe2012-10-22 18:19:23 +0000130 }
131
132 // Push data from ACM to RTP/RTCP-module to deliver audio frame for
133 // packetization.
134 // This call will trigger Transport::SendPacket() from the RTP/RTCP module.
135 if (_rtpRtcpModule->SendOutgoingData((FrameType&)frameType,
136 payloadType,
137 timeStamp,
138 // Leaving the time when this frame was
139 // received from the capture device as
140 // undefined for voice for now.
141 -1,
142 payloadData,
143 payloadSize,
144 fragmentation) == -1)
145 {
146 _engineStatisticsPtr->SetLastError(
147 VE_RTP_RTCP_MODULE_ERROR, kTraceWarning,
148 "Channel::SendData() failed to send data to RTP/RTCP module");
149 return -1;
150 }
151
152 _lastLocalTimeStamp = timeStamp;
153 _lastPayloadType = payloadType;
154
155 return 0;
156}
157
pbos@webrtc.org54f03bc2013-04-09 10:09:10 +0000158int32_t
159Channel::InFrameType(int16_t frameType)
andrew@webrtc.orgb015cbe2012-10-22 18:19:23 +0000160{
161 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId,_channelId),
162 "Channel::InFrameType(frameType=%d)", frameType);
163
164 CriticalSectionScoped cs(&_callbackCritSect);
165 // 1 indicates speech
166 _sendFrameType = (frameType == 1) ? 1 : 0;
167 return 0;
168}
169
pbos@webrtc.org54f03bc2013-04-09 10:09:10 +0000170int32_t
pbos@webrtc.orgca7a9a22013-05-14 08:31:39 +0000171Channel::OnRxVadDetected(int vadDecision)
andrew@webrtc.orgb015cbe2012-10-22 18:19:23 +0000172{
173 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId, _channelId),
174 "Channel::OnRxVadDetected(vadDecision=%d)", vadDecision);
175
176 CriticalSectionScoped cs(&_callbackCritSect);
177 if (_rxVadObserverPtr)
178 {
179 _rxVadObserverPtr->OnRxVad(_channelId, vadDecision);
180 }
181
182 return 0;
183}
184
185int
186Channel::SendPacket(int channel, const void *data, int len)
187{
188 channel = VoEChannelId(channel);
189 assert(channel == _channelId);
190
191 WEBRTC_TRACE(kTraceStream, kTraceVoice, VoEId(_instanceId,_channelId),
192 "Channel::SendPacket(channel=%d, len=%d)", channel, len);
193
wu@webrtc.orgb27e6702013-10-18 21:10:51 +0000194 CriticalSectionScoped cs(&_callbackCritSect);
195
andrew@webrtc.orgb015cbe2012-10-22 18:19:23 +0000196 if (_transportPtr == NULL)
197 {
198 WEBRTC_TRACE(kTraceError, kTraceVoice, VoEId(_instanceId,_channelId),
199 "Channel::SendPacket() failed to send RTP packet due to"
200 " invalid transport object");
201 return -1;
202 }
203
pbos@webrtc.org54f03bc2013-04-09 10:09:10 +0000204 uint8_t* bufferToSendPtr = (uint8_t*)data;
205 int32_t bufferLength = len;
andrew@webrtc.orgb015cbe2012-10-22 18:19:23 +0000206
207 // Dump the RTP packet to a file (if RTP dump is enabled).
pbos@webrtc.org54f03bc2013-04-09 10:09:10 +0000208 if (_rtpDumpOut.DumpPacket((const uint8_t*)data, len) == -1)
andrew@webrtc.orgb015cbe2012-10-22 18:19:23 +0000209 {
210 WEBRTC_TRACE(kTraceWarning, kTraceVoice,
211 VoEId(_instanceId,_channelId),
212 "Channel::SendPacket() RTP dump to output file failed");
213 }
214
wu@webrtc.orgb27e6702013-10-18 21:10:51 +0000215 int n = _transportPtr->SendPacket(channel, bufferToSendPtr,
216 bufferLength);
217 if (n < 0) {
218 std::string transport_name =
219 _externalTransport ? "external transport" : "WebRtc sockets";
220 WEBRTC_TRACE(kTraceError, kTraceVoice,
221 VoEId(_instanceId,_channelId),
222 "Channel::SendPacket() RTP transmission using %s failed",
223 transport_name.c_str());
224 return -1;
andrew@webrtc.orgb015cbe2012-10-22 18:19:23 +0000225 }
wu@webrtc.orgb27e6702013-10-18 21:10:51 +0000226 return n;
andrew@webrtc.orgb015cbe2012-10-22 18:19:23 +0000227}
228
229int
230Channel::SendRTCPPacket(int channel, const void *data, int len)
231{
232 channel = VoEChannelId(channel);
233 assert(channel == _channelId);
234
235 WEBRTC_TRACE(kTraceStream, kTraceVoice, VoEId(_instanceId,_channelId),
236 "Channel::SendRTCPPacket(channel=%d, len=%d)", channel, len);
237
wu@webrtc.orgb27e6702013-10-18 21:10:51 +0000238 CriticalSectionScoped cs(&_callbackCritSect);
239 if (_transportPtr == NULL)
andrew@webrtc.orgb015cbe2012-10-22 18:19:23 +0000240 {
wu@webrtc.orgb27e6702013-10-18 21:10:51 +0000241 WEBRTC_TRACE(kTraceError, kTraceVoice,
242 VoEId(_instanceId,_channelId),
243 "Channel::SendRTCPPacket() failed to send RTCP packet"
244 " due to invalid transport object");
245 return -1;
andrew@webrtc.orgb015cbe2012-10-22 18:19:23 +0000246 }
247
pbos@webrtc.org54f03bc2013-04-09 10:09:10 +0000248 uint8_t* bufferToSendPtr = (uint8_t*)data;
249 int32_t bufferLength = len;
andrew@webrtc.orgb015cbe2012-10-22 18:19:23 +0000250
251 // Dump the RTCP packet to a file (if RTP dump is enabled).
pbos@webrtc.org54f03bc2013-04-09 10:09:10 +0000252 if (_rtpDumpOut.DumpPacket((const uint8_t*)data, len) == -1)
andrew@webrtc.orgb015cbe2012-10-22 18:19:23 +0000253 {
254 WEBRTC_TRACE(kTraceWarning, kTraceVoice,
255 VoEId(_instanceId,_channelId),
256 "Channel::SendPacket() RTCP dump to output file failed");
257 }
258
wu@webrtc.orgb27e6702013-10-18 21:10:51 +0000259 int n = _transportPtr->SendRTCPPacket(channel,
260 bufferToSendPtr,
261 bufferLength);
262 if (n < 0) {
263 std::string transport_name =
264 _externalTransport ? "external transport" : "WebRtc sockets";
265 WEBRTC_TRACE(kTraceInfo, kTraceVoice,
266 VoEId(_instanceId,_channelId),
267 "Channel::SendRTCPPacket() transmission using %s failed",
268 transport_name.c_str());
269 return -1;
andrew@webrtc.orgb015cbe2012-10-22 18:19:23 +0000270 }
wu@webrtc.orgb27e6702013-10-18 21:10:51 +0000271 return n;
andrew@webrtc.orgb015cbe2012-10-22 18:19:23 +0000272}
273
274void
pbos@webrtc.orgca7a9a22013-05-14 08:31:39 +0000275Channel::OnPlayTelephoneEvent(int32_t id,
276 uint8_t event,
277 uint16_t lengthMs,
278 uint8_t volume)
andrew@webrtc.orgb015cbe2012-10-22 18:19:23 +0000279{
280 WEBRTC_TRACE(kTraceStream, kTraceVoice, VoEId(_instanceId,_channelId),
281 "Channel::OnPlayTelephoneEvent(id=%d, event=%u, lengthMs=%u,"
282 " volume=%u)", id, event, lengthMs, volume);
283
284 if (!_playOutbandDtmfEvent || (event > 15))
285 {
286 // Ignore callback since feedback is disabled or event is not a
287 // Dtmf tone event.
288 return;
289 }
290
291 assert(_outputMixerPtr != NULL);
292
293 // Start playing out the Dtmf tone (if playout is enabled).
294 // Reduce length of tone with 80ms to the reduce risk of echo.
295 _outputMixerPtr->PlayDtmfTone(event, lengthMs - 80, volume);
296}
297
298void
stefan@webrtc.orga20e2d42013-08-21 20:58:21 +0000299Channel::OnIncomingSSRCChanged(int32_t id, uint32_t ssrc)
andrew@webrtc.orgb015cbe2012-10-22 18:19:23 +0000300{
301 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId,_channelId),
302 "Channel::OnIncomingSSRCChanged(id=%d, SSRC=%d)",
stefan@webrtc.orga20e2d42013-08-21 20:58:21 +0000303 id, ssrc);
andrew@webrtc.orgb015cbe2012-10-22 18:19:23 +0000304
dwkang@webrtc.orgc766a742013-08-29 07:34:12 +0000305 // Update ssrc so that NTP for AV sync can be updated.
306 _rtpRtcpModule->SetRemoteSSRC(ssrc);
andrew@webrtc.orgb015cbe2012-10-22 18:19:23 +0000307}
308
pbos@webrtc.orgca7a9a22013-05-14 08:31:39 +0000309void Channel::OnIncomingCSRCChanged(int32_t id,
310 uint32_t CSRC,
311 bool added)
andrew@webrtc.orgb015cbe2012-10-22 18:19:23 +0000312{
313 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId,_channelId),
314 "Channel::OnIncomingCSRCChanged(id=%d, CSRC=%d, added=%d)",
315 id, CSRC, added);
andrew@webrtc.orgb015cbe2012-10-22 18:19:23 +0000316}
317
stefan@webrtc.orga20e2d42013-08-21 20:58:21 +0000318void Channel::ResetStatistics(uint32_t ssrc) {
319 StreamStatistician* statistician =
320 rtp_receive_statistics_->GetStatistician(ssrc);
321 if (statistician) {
322 statistician->ResetStatistics();
323 }
sprang@webrtc.org4f1f5fa2013-12-19 13:26:02 +0000324 statistics_proxy_->ResetStatistics();
wu@webrtc.org7fc75bb2013-08-15 23:38:54 +0000325}
326
andrew@webrtc.orgb015cbe2012-10-22 18:19:23 +0000327void
pbos@webrtc.orgca7a9a22013-05-14 08:31:39 +0000328Channel::OnApplicationDataReceived(int32_t id,
329 uint8_t subType,
330 uint32_t name,
331 uint16_t length,
pbos@webrtc.org54f03bc2013-04-09 10:09:10 +0000332 const uint8_t* data)
andrew@webrtc.orgb015cbe2012-10-22 18:19:23 +0000333{
334 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId,_channelId),
335 "Channel::OnApplicationDataReceived(id=%d, subType=%u,"
336 " name=%u, length=%u)",
337 id, subType, name, length);
338
pbos@webrtc.org54f03bc2013-04-09 10:09:10 +0000339 int32_t channel = VoEChannelId(id);
andrew@webrtc.orgb015cbe2012-10-22 18:19:23 +0000340 assert(channel == _channelId);
341
342 if (_rtcpObserver)
343 {
344 CriticalSectionScoped cs(&_callbackCritSect);
345
346 if (_rtcpObserverPtr)
347 {
348 _rtcpObserverPtr->OnApplicationDataReceived(channel,
349 subType,
350 name,
351 data,
352 length);
353 }
354 }
355}
356
pbos@webrtc.org54f03bc2013-04-09 10:09:10 +0000357int32_t
andrew@webrtc.orgb015cbe2012-10-22 18:19:23 +0000358Channel::OnInitializeDecoder(
pbos@webrtc.orgca7a9a22013-05-14 08:31:39 +0000359 int32_t id,
360 int8_t payloadType,
andrew@webrtc.orgb015cbe2012-10-22 18:19:23 +0000361 const char payloadName[RTP_PAYLOAD_NAME_SIZE],
pbos@webrtc.orgca7a9a22013-05-14 08:31:39 +0000362 int frequency,
363 uint8_t channels,
364 uint32_t rate)
andrew@webrtc.orgb015cbe2012-10-22 18:19:23 +0000365{
366 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId,_channelId),
367 "Channel::OnInitializeDecoder(id=%d, payloadType=%d, "
368 "payloadName=%s, frequency=%u, channels=%u, rate=%u)",
369 id, payloadType, payloadName, frequency, channels, rate);
370
371 assert(VoEChannelId(id) == _channelId);
372
373 CodecInst receiveCodec = {0};
374 CodecInst dummyCodec = {0};
375
376 receiveCodec.pltype = payloadType;
377 receiveCodec.plfreq = frequency;
378 receiveCodec.channels = channels;
379 receiveCodec.rate = rate;
380 strncpy(receiveCodec.plname, payloadName, RTP_PAYLOAD_NAME_SIZE - 1);
andrew@webrtc.orgd4682362013-01-22 04:44:30 +0000381
andrew@webrtc.org510ee1b2013-09-23 23:02:24 +0000382 audio_coding_->Codec(payloadName, &dummyCodec, frequency, channels);
andrew@webrtc.orgb015cbe2012-10-22 18:19:23 +0000383 receiveCodec.pacsize = dummyCodec.pacsize;
384
385 // Register the new codec to the ACM
andrew@webrtc.org510ee1b2013-09-23 23:02:24 +0000386 if (audio_coding_->RegisterReceiveCodec(receiveCodec) == -1)
andrew@webrtc.orgb015cbe2012-10-22 18:19:23 +0000387 {
388 WEBRTC_TRACE(kTraceWarning, kTraceVoice,
389 VoEId(_instanceId, _channelId),
390 "Channel::OnInitializeDecoder() invalid codec ("
391 "pt=%d, name=%s) received - 1", payloadType, payloadName);
392 _engineStatisticsPtr->SetLastError(VE_AUDIO_CODING_MODULE_ERROR);
393 return -1;
394 }
395
396 return 0;
397}
398
399void
pbos@webrtc.orgca7a9a22013-05-14 08:31:39 +0000400Channel::OnPacketTimeout(int32_t id)
andrew@webrtc.orgb015cbe2012-10-22 18:19:23 +0000401{
402 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId,_channelId),
403 "Channel::OnPacketTimeout(id=%d)", id);
404
405 CriticalSectionScoped cs(_callbackCritSectPtr);
406 if (_voiceEngineObserverPtr)
407 {
henrika@webrtc.orgdf08c5d2014-03-18 10:32:33 +0000408 if (channel_state_.Get().receiving || _externalTransport)
andrew@webrtc.orgb015cbe2012-10-22 18:19:23 +0000409 {
pbos@webrtc.org54f03bc2013-04-09 10:09:10 +0000410 int32_t channel = VoEChannelId(id);
andrew@webrtc.orgb015cbe2012-10-22 18:19:23 +0000411 assert(channel == _channelId);
412 // Ensure that next OnReceivedPacket() callback will trigger
413 // a VE_PACKET_RECEIPT_RESTARTED callback.
414 _rtpPacketTimedOut = true;
415 // Deliver callback to the observer
416 WEBRTC_TRACE(kTraceInfo, kTraceVoice,
417 VoEId(_instanceId,_channelId),
418 "Channel::OnPacketTimeout() => "
419 "CallbackOnError(VE_RECEIVE_PACKET_TIMEOUT)");
420 _voiceEngineObserverPtr->CallbackOnError(channel,
421 VE_RECEIVE_PACKET_TIMEOUT);
422 }
423 }
424}
425
426void
pbos@webrtc.orgca7a9a22013-05-14 08:31:39 +0000427Channel::OnReceivedPacket(int32_t id,
428 RtpRtcpPacketType packetType)
andrew@webrtc.orgb015cbe2012-10-22 18:19:23 +0000429{
430 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId,_channelId),
431 "Channel::OnReceivedPacket(id=%d, packetType=%d)",
432 id, packetType);
433
434 assert(VoEChannelId(id) == _channelId);
435
436 // Notify only for the case when we have restarted an RTP session.
437 if (_rtpPacketTimedOut && (kPacketRtp == packetType))
438 {
439 CriticalSectionScoped cs(_callbackCritSectPtr);
440 if (_voiceEngineObserverPtr)
441 {
pbos@webrtc.org54f03bc2013-04-09 10:09:10 +0000442 int32_t channel = VoEChannelId(id);
andrew@webrtc.orgb015cbe2012-10-22 18:19:23 +0000443 assert(channel == _channelId);
444 // Reset timeout mechanism
445 _rtpPacketTimedOut = false;
446 // Deliver callback to the observer
447 WEBRTC_TRACE(kTraceInfo, kTraceVoice,
448 VoEId(_instanceId,_channelId),
449 "Channel::OnPacketTimeout() =>"
450 " CallbackOnError(VE_PACKET_RECEIPT_RESTARTED)");
451 _voiceEngineObserverPtr->CallbackOnError(
452 channel,
453 VE_PACKET_RECEIPT_RESTARTED);
454 }
455 }
456}
457
458void
pbos@webrtc.orgca7a9a22013-05-14 08:31:39 +0000459Channel::OnPeriodicDeadOrAlive(int32_t id,
460 RTPAliveType alive)
andrew@webrtc.orgb015cbe2012-10-22 18:19:23 +0000461{
462 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId,_channelId),
463 "Channel::OnPeriodicDeadOrAlive(id=%d, alive=%d)", id, alive);
464
henrika@webrtc.org1d25eac2013-04-05 14:34:57 +0000465 {
466 CriticalSectionScoped cs(&_callbackCritSect);
467 if (!_connectionObserver)
468 return;
469 }
andrew@webrtc.orgb015cbe2012-10-22 18:19:23 +0000470
pbos@webrtc.org54f03bc2013-04-09 10:09:10 +0000471 int32_t channel = VoEChannelId(id);
andrew@webrtc.orgb015cbe2012-10-22 18:19:23 +0000472 assert(channel == _channelId);
473
474 // Use Alive as default to limit risk of false Dead detections
475 bool isAlive(true);
476
477 // Always mark the connection as Dead when the module reports kRtpDead
478 if (kRtpDead == alive)
479 {
480 isAlive = false;
481 }
482
483 // It is possible that the connection is alive even if no RTP packet has
484 // been received for a long time since the other side might use VAD/DTX
485 // and a low SID-packet update rate.
henrika@webrtc.orgdf08c5d2014-03-18 10:32:33 +0000486 if ((kRtpNoRtp == alive) && channel_state_.Get().playing)
andrew@webrtc.orgb015cbe2012-10-22 18:19:23 +0000487 {
488 // Detect Alive for all NetEQ states except for the case when we are
489 // in PLC_CNG state.
490 // PLC_CNG <=> background noise only due to long expand or error.
491 // Note that, the case where the other side stops sending during CNG
492 // state will be detected as Alive. Dead is is not set until after
493 // missing RTCP packets for at least twelve seconds (handled
494 // internally by the RTP/RTCP module).
495 isAlive = (_outputSpeechType != AudioFrame::kPLCCNG);
496 }
497
andrew@webrtc.orgb015cbe2012-10-22 18:19:23 +0000498 // Send callback to the registered observer
499 if (_connectionObserver)
500 {
501 CriticalSectionScoped cs(&_callbackCritSect);
502 if (_connectionObserverPtr)
503 {
504 _connectionObserverPtr->OnPeriodicDeadOrAlive(channel, isAlive);
505 }
506 }
507}
508
pbos@webrtc.org54f03bc2013-04-09 10:09:10 +0000509int32_t
510Channel::OnReceivedPayloadData(const uint8_t* payloadData,
pbos@webrtc.orgca7a9a22013-05-14 08:31:39 +0000511 uint16_t payloadSize,
andrew@webrtc.orgb015cbe2012-10-22 18:19:23 +0000512 const WebRtcRTPHeader* rtpHeader)
513{
514 WEBRTC_TRACE(kTraceStream, kTraceVoice, VoEId(_instanceId,_channelId),
515 "Channel::OnReceivedPayloadData(payloadSize=%d,"
516 " payloadType=%u, audioChannel=%u)",
517 payloadSize,
518 rtpHeader->header.payloadType,
519 rtpHeader->type.Audio.channel);
520
henrika@webrtc.orgdf08c5d2014-03-18 10:32:33 +0000521 if (!channel_state_.Get().playing)
andrew@webrtc.orgb015cbe2012-10-22 18:19:23 +0000522 {
523 // Avoid inserting into NetEQ when we are not playing. Count the
524 // packet as discarded.
525 WEBRTC_TRACE(kTraceStream, kTraceVoice,
526 VoEId(_instanceId, _channelId),
527 "received packet is discarded since playing is not"
528 " activated");
529 _numberOfDiscardedPackets++;
530 return 0;
531 }
532
533 // Push the incoming payload (parsed and ready for decoding) into the ACM
andrew@webrtc.org510ee1b2013-09-23 23:02:24 +0000534 if (audio_coding_->IncomingPacket(payloadData,
535 payloadSize,
536 *rtpHeader) != 0)
andrew@webrtc.orgb015cbe2012-10-22 18:19:23 +0000537 {
538 _engineStatisticsPtr->SetLastError(
539 VE_AUDIO_CODING_MODULE_ERROR, kTraceWarning,
540 "Channel::OnReceivedPayloadData() unable to push data to the ACM");
541 return -1;
542 }
543
pwestin@webrtc.org4aa9f1a2013-06-06 21:09:01 +0000544 // Update the packet delay.
andrew@webrtc.orgb015cbe2012-10-22 18:19:23 +0000545 UpdatePacketDelay(rtpHeader->header.timestamp,
546 rtpHeader->header.sequenceNumber);
pwestin@webrtc.org4aa9f1a2013-06-06 21:09:01 +0000547
wu@webrtc.org7fc75bb2013-08-15 23:38:54 +0000548 uint16_t round_trip_time = 0;
549 _rtpRtcpModule->RTT(rtp_receiver_->SSRC(), &round_trip_time,
550 NULL, NULL, NULL);
pwestin@webrtc.org4aa9f1a2013-06-06 21:09:01 +0000551
andrew@webrtc.org510ee1b2013-09-23 23:02:24 +0000552 std::vector<uint16_t> nack_list = audio_coding_->GetNackList(
wu@webrtc.org7fc75bb2013-08-15 23:38:54 +0000553 round_trip_time);
554 if (!nack_list.empty()) {
555 // Can't use nack_list.data() since it's not supported by all
556 // compilers.
557 ResendPackets(&(nack_list[0]), static_cast<int>(nack_list.size()));
pwestin@webrtc.org4aa9f1a2013-06-06 21:09:01 +0000558 }
andrew@webrtc.orgb015cbe2012-10-22 18:19:23 +0000559 return 0;
560}
561
stefan@webrtc.orgdb74c612013-09-06 13:40:11 +0000562bool Channel::OnRecoveredPacket(const uint8_t* rtp_packet,
563 int rtp_packet_length) {
564 RTPHeader header;
565 if (!rtp_header_parser_->Parse(rtp_packet, rtp_packet_length, &header)) {
566 WEBRTC_TRACE(kTraceDebug, webrtc::kTraceVoice, _channelId,
567 "IncomingPacket invalid RTP header");
568 return false;
569 }
570 header.payload_type_frequency =
571 rtp_payload_registry_->GetPayloadTypeFrequency(header.payloadType);
572 if (header.payload_type_frequency < 0)
573 return false;
574 return ReceivePacket(rtp_packet, rtp_packet_length, header, false);
575}
576
pbos@webrtc.orgca7a9a22013-05-14 08:31:39 +0000577int32_t Channel::GetAudioFrame(int32_t id, AudioFrame& audioFrame)
andrew@webrtc.orgb015cbe2012-10-22 18:19:23 +0000578{
579 WEBRTC_TRACE(kTraceStream, kTraceVoice, VoEId(_instanceId,_channelId),
580 "Channel::GetAudioFrame(id=%d)", id);
581
582 // Get 10ms raw PCM data from the ACM (mixer limits output frequency)
andrew@webrtc.org510ee1b2013-09-23 23:02:24 +0000583 if (audio_coding_->PlayoutData10Ms(audioFrame.sample_rate_hz_,
584 &audioFrame) == -1)
andrew@webrtc.orgb015cbe2012-10-22 18:19:23 +0000585 {
586 WEBRTC_TRACE(kTraceError, kTraceVoice,
587 VoEId(_instanceId,_channelId),
588 "Channel::GetAudioFrame() PlayoutData10Ms() failed!");
589 // In all likelihood, the audio in this frame is garbage. We return an
590 // error so that the audio mixer module doesn't add it to the mix. As
591 // a result, it won't be played out and the actions skipped here are
592 // irrelevant.
593 return -1;
594 }
595
596 if (_RxVadDetection)
597 {
598 UpdateRxVadDetection(audioFrame);
599 }
600
601 // Convert module ID to internal VoE channel ID
602 audioFrame.id_ = VoEChannelId(audioFrame.id_);
603 // Store speech type for dead-or-alive detection
604 _outputSpeechType = audioFrame.speech_type_;
605
henrika@webrtc.orgdf08c5d2014-03-18 10:32:33 +0000606 ChannelState::State state = channel_state_.Get();
607
608 if (state.rx_apm_is_enabled) {
andrew@webrtc.orge95dc252014-01-07 17:45:09 +0000609 int err = rx_audioproc_->ProcessStream(&audioFrame);
610 if (err) {
611 LOG(LS_ERROR) << "ProcessStream() error: " << err;
612 assert(false);
613 }
andrew@webrtc.orgb015cbe2012-10-22 18:19:23 +0000614 }
615
wu@webrtc.orgf7651ef2013-10-17 18:28:55 +0000616 float output_gain = 1.0f;
617 float left_pan = 1.0f;
618 float right_pan = 1.0f;
andrew@webrtc.orgb015cbe2012-10-22 18:19:23 +0000619 {
wu@webrtc.orgf7651ef2013-10-17 18:28:55 +0000620 CriticalSectionScoped cs(&volume_settings_critsect_);
621 output_gain = _outputGain;
622 left_pan = _panLeft;
623 right_pan= _panRight;
624 }
625
626 // Output volume scaling
627 if (output_gain < 0.99f || output_gain > 1.01f)
628 {
629 AudioFrameOperations::ScaleWithSat(output_gain, audioFrame);
andrew@webrtc.orgb015cbe2012-10-22 18:19:23 +0000630 }
631
632 // Scale left and/or right channel(s) if stereo and master balance is
633 // active
634
wu@webrtc.orgf7651ef2013-10-17 18:28:55 +0000635 if (left_pan != 1.0f || right_pan != 1.0f)
andrew@webrtc.orgb015cbe2012-10-22 18:19:23 +0000636 {
637 if (audioFrame.num_channels_ == 1)
638 {
639 // Emulate stereo mode since panning is active.
640 // The mono signal is copied to both left and right channels here.
641 AudioFrameOperations::MonoToStereo(&audioFrame);
642 }
643 // For true stereo mode (when we are receiving a stereo signal), no
644 // action is needed.
645
646 // Do the panning operation (the audio frame contains stereo at this
647 // stage)
wu@webrtc.orgf7651ef2013-10-17 18:28:55 +0000648 AudioFrameOperations::Scale(left_pan, right_pan, audioFrame);
andrew@webrtc.orgb015cbe2012-10-22 18:19:23 +0000649 }
650
651 // Mix decoded PCM output with file if file mixing is enabled
henrika@webrtc.orgdf08c5d2014-03-18 10:32:33 +0000652 if (state.output_file_playing)
andrew@webrtc.orgb015cbe2012-10-22 18:19:23 +0000653 {
654 MixAudioWithFile(audioFrame, audioFrame.sample_rate_hz_);
655 }
656
andrew@webrtc.orgb015cbe2012-10-22 18:19:23 +0000657 // External media
658 if (_outputExternalMedia)
659 {
660 CriticalSectionScoped cs(&_callbackCritSect);
661 const bool isStereo = (audioFrame.num_channels_ == 2);
662 if (_outputExternalMediaCallbackPtr)
663 {
664 _outputExternalMediaCallbackPtr->Process(
665 _channelId,
666 kPlaybackPerChannel,
pbos@webrtc.org54f03bc2013-04-09 10:09:10 +0000667 (int16_t*)audioFrame.data_,
andrew@webrtc.orgb015cbe2012-10-22 18:19:23 +0000668 audioFrame.samples_per_channel_,
669 audioFrame.sample_rate_hz_,
670 isStereo);
671 }
672 }
673
674 // Record playout if enabled
675 {
676 CriticalSectionScoped cs(&_fileCritSect);
677
678 if (_outputFileRecording && _outputFileRecorderPtr)
679 {
680 _outputFileRecorderPtr->RecordAudioToFile(audioFrame);
681 }
682 }
683
684 // Measure audio level (0-9)
685 _outputAudioLevel.ComputeLevel(audioFrame);
686
wu@webrtc.org81f8df92014-06-05 20:34:08 +0000687 if (capture_start_rtp_time_stamp_ < 0 && audioFrame.timestamp_ != 0) {
688 // The first frame with a valid rtp timestamp.
wu@webrtc.org22f69bd2014-05-19 17:39:11 +0000689 capture_start_rtp_time_stamp_ = audioFrame.timestamp_;
wu@webrtc.org81f8df92014-06-05 20:34:08 +0000690 }
691
692 if (capture_start_rtp_time_stamp_ >= 0) {
693 // audioFrame.timestamp_ should be valid from now on.
694
695 // Compute elapsed time.
696 int64_t unwrap_timestamp =
697 rtp_ts_wraparound_handler_->Unwrap(audioFrame.timestamp_);
698 audioFrame.elapsed_time_ms_ =
699 (unwrap_timestamp - capture_start_rtp_time_stamp_) /
700 (GetPlayoutFrequency() / 1000);
701
702 // Compute ntp time.
703 audioFrame.ntp_time_ms_ = ntp_estimator_->Estimate(audioFrame.timestamp_);
wu@webrtc.org22f69bd2014-05-19 17:39:11 +0000704 // |ntp_time_ms_| won't be valid until at least 2 RTCP SRs are received.
705 if (audioFrame.ntp_time_ms_ > 0) {
706 // Compute |capture_start_ntp_time_ms_| so that
wu@webrtc.org81f8df92014-06-05 20:34:08 +0000707 // |capture_start_ntp_time_ms_| + |elapsed_time_ms_| == |ntp_time_ms_|
wu@webrtc.org22f69bd2014-05-19 17:39:11 +0000708 CriticalSectionScoped lock(ts_stats_lock_.get());
wu@webrtc.org81f8df92014-06-05 20:34:08 +0000709 capture_start_ntp_time_ms_ =
710 audioFrame.ntp_time_ms_ - audioFrame.elapsed_time_ms_;
wu@webrtc.org22f69bd2014-05-19 17:39:11 +0000711 }
712 }
713
andrew@webrtc.orgb015cbe2012-10-22 18:19:23 +0000714 return 0;
715}
716
pbos@webrtc.org54f03bc2013-04-09 10:09:10 +0000717int32_t
pbos@webrtc.orgca7a9a22013-05-14 08:31:39 +0000718Channel::NeededFrequency(int32_t id)
andrew@webrtc.orgb015cbe2012-10-22 18:19:23 +0000719{
720 WEBRTC_TRACE(kTraceStream, kTraceVoice, VoEId(_instanceId,_channelId),
721 "Channel::NeededFrequency(id=%d)", id);
722
723 int highestNeeded = 0;
724
725 // Determine highest needed receive frequency
andrew@webrtc.org510ee1b2013-09-23 23:02:24 +0000726 int32_t receiveFrequency = audio_coding_->ReceiveFrequency();
andrew@webrtc.orgb015cbe2012-10-22 18:19:23 +0000727
728 // Return the bigger of playout and receive frequency in the ACM.
andrew@webrtc.org510ee1b2013-09-23 23:02:24 +0000729 if (audio_coding_->PlayoutFrequency() > receiveFrequency)
andrew@webrtc.orgb015cbe2012-10-22 18:19:23 +0000730 {
andrew@webrtc.org510ee1b2013-09-23 23:02:24 +0000731 highestNeeded = audio_coding_->PlayoutFrequency();
andrew@webrtc.orgb015cbe2012-10-22 18:19:23 +0000732 }
733 else
734 {
735 highestNeeded = receiveFrequency;
736 }
737
738 // Special case, if we're playing a file on the playout side
739 // we take that frequency into consideration as well
740 // This is not needed on sending side, since the codec will
741 // limit the spectrum anyway.
henrika@webrtc.orgdf08c5d2014-03-18 10:32:33 +0000742 if (channel_state_.Get().output_file_playing)
andrew@webrtc.orgb015cbe2012-10-22 18:19:23 +0000743 {
744 CriticalSectionScoped cs(&_fileCritSect);
henrika@webrtc.orgdf08c5d2014-03-18 10:32:33 +0000745 if (_outputFilePlayerPtr)
andrew@webrtc.orgb015cbe2012-10-22 18:19:23 +0000746 {
747 if(_outputFilePlayerPtr->Frequency()>highestNeeded)
748 {
749 highestNeeded=_outputFilePlayerPtr->Frequency();
750 }
751 }
752 }
753
754 return(highestNeeded);
755}
756
pbos@webrtc.org54f03bc2013-04-09 10:09:10 +0000757int32_t
andrew@webrtc.orgb015cbe2012-10-22 18:19:23 +0000758Channel::CreateChannel(Channel*& channel,
pbos@webrtc.orgca7a9a22013-05-14 08:31:39 +0000759 int32_t channelId,
minyue@webrtc.org4489c512013-09-12 17:03:00 +0000760 uint32_t instanceId,
761 const Config& config)
andrew@webrtc.orgb015cbe2012-10-22 18:19:23 +0000762{
763 WEBRTC_TRACE(kTraceMemory, kTraceVoice, VoEId(instanceId,channelId),
764 "Channel::CreateChannel(channelId=%d, instanceId=%d)",
765 channelId, instanceId);
766
minyue@webrtc.org4489c512013-09-12 17:03:00 +0000767 channel = new Channel(channelId, instanceId, config);
andrew@webrtc.orgb015cbe2012-10-22 18:19:23 +0000768 if (channel == NULL)
769 {
770 WEBRTC_TRACE(kTraceMemory, kTraceVoice,
771 VoEId(instanceId,channelId),
772 "Channel::CreateChannel() unable to allocate memory for"
773 " channel");
774 return -1;
775 }
776 return 0;
777}
778
779void
pbos@webrtc.orgca7a9a22013-05-14 08:31:39 +0000780Channel::PlayNotification(int32_t id, uint32_t durationMs)
andrew@webrtc.orgb015cbe2012-10-22 18:19:23 +0000781{
782 WEBRTC_TRACE(kTraceStream, kTraceVoice, VoEId(_instanceId,_channelId),
783 "Channel::PlayNotification(id=%d, durationMs=%d)",
784 id, durationMs);
785
786 // Not implement yet
787}
788
789void
pbos@webrtc.orgca7a9a22013-05-14 08:31:39 +0000790Channel::RecordNotification(int32_t id, uint32_t durationMs)
andrew@webrtc.orgb015cbe2012-10-22 18:19:23 +0000791{
792 WEBRTC_TRACE(kTraceStream, kTraceVoice, VoEId(_instanceId,_channelId),
793 "Channel::RecordNotification(id=%d, durationMs=%d)",
794 id, durationMs);
795
796 // Not implement yet
797}
798
799void
pbos@webrtc.orgca7a9a22013-05-14 08:31:39 +0000800Channel::PlayFileEnded(int32_t id)
andrew@webrtc.orgb015cbe2012-10-22 18:19:23 +0000801{
802 WEBRTC_TRACE(kTraceStream, kTraceVoice, VoEId(_instanceId,_channelId),
803 "Channel::PlayFileEnded(id=%d)", id);
804
805 if (id == _inputFilePlayerId)
806 {
henrika@webrtc.orgdf08c5d2014-03-18 10:32:33 +0000807 channel_state_.SetInputFilePlaying(false);
andrew@webrtc.orgb015cbe2012-10-22 18:19:23 +0000808 WEBRTC_TRACE(kTraceStateInfo, kTraceVoice,
809 VoEId(_instanceId,_channelId),
810 "Channel::PlayFileEnded() => input file player module is"
811 " shutdown");
812 }
813 else if (id == _outputFilePlayerId)
814 {
henrika@webrtc.orgdf08c5d2014-03-18 10:32:33 +0000815 channel_state_.SetOutputFilePlaying(false);
andrew@webrtc.orgb015cbe2012-10-22 18:19:23 +0000816 WEBRTC_TRACE(kTraceStateInfo, kTraceVoice,
817 VoEId(_instanceId,_channelId),
818 "Channel::PlayFileEnded() => output file player module is"
819 " shutdown");
820 }
821}
822
823void
pbos@webrtc.orgca7a9a22013-05-14 08:31:39 +0000824Channel::RecordFileEnded(int32_t id)
andrew@webrtc.orgb015cbe2012-10-22 18:19:23 +0000825{
826 WEBRTC_TRACE(kTraceStream, kTraceVoice, VoEId(_instanceId,_channelId),
827 "Channel::RecordFileEnded(id=%d)", id);
828
829 assert(id == _outputFileRecorderId);
830
831 CriticalSectionScoped cs(&_fileCritSect);
832
833 _outputFileRecording = false;
834 WEBRTC_TRACE(kTraceStateInfo, kTraceVoice,
835 VoEId(_instanceId,_channelId),
836 "Channel::RecordFileEnded() => output file recorder module is"
837 " shutdown");
838}
839
pbos@webrtc.orgca7a9a22013-05-14 08:31:39 +0000840Channel::Channel(int32_t channelId,
minyue@webrtc.org4489c512013-09-12 17:03:00 +0000841 uint32_t instanceId,
842 const Config& config) :
andrew@webrtc.orgb015cbe2012-10-22 18:19:23 +0000843 _fileCritSect(*CriticalSectionWrapper::CreateCriticalSection()),
844 _callbackCritSect(*CriticalSectionWrapper::CreateCriticalSection()),
wu@webrtc.orgf7651ef2013-10-17 18:28:55 +0000845 volume_settings_critsect_(*CriticalSectionWrapper::CreateCriticalSection()),
andrew@webrtc.orgb015cbe2012-10-22 18:19:23 +0000846 _instanceId(instanceId),
847 _channelId(channelId),
stefan@webrtc.org6696fba2013-05-29 12:12:51 +0000848 rtp_header_parser_(RtpHeaderParser::Create()),
wu@webrtc.org7fc75bb2013-08-15 23:38:54 +0000849 rtp_payload_registry_(
andresp@webrtc.org99681312014-04-08 11:06:12 +0000850 new RTPPayloadRegistry(RTPPayloadStrategy::CreateStrategy(true))),
wu@webrtc.org7fc75bb2013-08-15 23:38:54 +0000851 rtp_receive_statistics_(ReceiveStatistics::Create(
852 Clock::GetRealTimeClock())),
853 rtp_receiver_(RtpReceiver::CreateAudioReceiver(
854 VoEModuleId(instanceId, channelId), Clock::GetRealTimeClock(), this,
855 this, this, rtp_payload_registry_.get())),
856 telephone_event_handler_(rtp_receiver_->GetTelephoneEventHandler()),
henrik.lundin@webrtc.org6ce37202014-04-22 19:04:34 +0000857 audio_coding_(AudioCodingModule::Create(
andrew@webrtc.orgb015cbe2012-10-22 18:19:23 +0000858 VoEModuleId(instanceId, channelId))),
andrew@webrtc.orgb015cbe2012-10-22 18:19:23 +0000859 _rtpDumpIn(*RtpDump::CreateRtpDump()),
860 _rtpDumpOut(*RtpDump::CreateRtpDump()),
861 _outputAudioLevel(),
862 _externalTransport(false),
sprang@webrtc.org4f1f5fa2013-12-19 13:26:02 +0000863 _audioLevel_dBov(0),
andrew@webrtc.orgb015cbe2012-10-22 18:19:23 +0000864 _inputFilePlayerPtr(NULL),
865 _outputFilePlayerPtr(NULL),
866 _outputFileRecorderPtr(NULL),
867 // Avoid conflict with other channels by adding 1024 - 1026,
868 // won't use as much as 1024 channels.
869 _inputFilePlayerId(VoEModuleId(instanceId, channelId) + 1024),
870 _outputFilePlayerId(VoEModuleId(instanceId, channelId) + 1025),
871 _outputFileRecorderId(VoEModuleId(instanceId, channelId) + 1026),
andrew@webrtc.orgb015cbe2012-10-22 18:19:23 +0000872 _outputFileRecording(false),
873 _inbandDtmfQueue(VoEModuleId(instanceId, channelId)),
874 _inbandDtmfGenerator(VoEModuleId(instanceId, channelId)),
andrew@webrtc.orgb015cbe2012-10-22 18:19:23 +0000875 _outputExternalMedia(false),
876 _inputExternalMediaCallbackPtr(NULL),
877 _outputExternalMediaCallbackPtr(NULL),
andrew@webrtc.orgb015cbe2012-10-22 18:19:23 +0000878 _timeStamp(0), // This is just an offset, RTP module will add it's own random offset
879 _sendTelephoneEventPayloadType(106),
wu@webrtc.org881a32d2014-05-20 22:55:01 +0000880 ntp_estimator_(new RemoteNtpTimeEstimator(Clock::GetRealTimeClock())),
turaj@webrtc.orgf1b92fd2013-12-13 21:05:07 +0000881 jitter_buffer_playout_timestamp_(0),
pwestin@webrtc.orgf2724972013-04-11 20:23:35 +0000882 playout_timestamp_rtp_(0),
883 playout_timestamp_rtcp_(0),
sprang@webrtc.org4f1f5fa2013-12-19 13:26:02 +0000884 playout_delay_ms_(0),
andrew@webrtc.orgb015cbe2012-10-22 18:19:23 +0000885 _numberOfDiscardedPackets(0),
xians@webrtc.org5ce87232013-07-31 16:30:19 +0000886 send_sequence_number_(0),
wu@webrtc.org22f69bd2014-05-19 17:39:11 +0000887 ts_stats_lock_(CriticalSectionWrapper::CreateCriticalSection()),
wu@webrtc.org81f8df92014-06-05 20:34:08 +0000888 rtp_ts_wraparound_handler_(new rtc::TimestampWrapAroundHandler()),
889 capture_start_rtp_time_stamp_(-1),
wu@webrtc.org22f69bd2014-05-19 17:39:11 +0000890 capture_start_ntp_time_ms_(-1),
andrew@webrtc.orgb015cbe2012-10-22 18:19:23 +0000891 _engineStatisticsPtr(NULL),
892 _outputMixerPtr(NULL),
893 _transmitMixerPtr(NULL),
894 _moduleProcessThreadPtr(NULL),
895 _audioDeviceModulePtr(NULL),
896 _voiceEngineObserverPtr(NULL),
897 _callbackCritSectPtr(NULL),
898 _transportPtr(NULL),
andrew@webrtc.orgb015cbe2012-10-22 18:19:23 +0000899 _rxVadObserverPtr(NULL),
900 _oldVadDecision(-1),
901 _sendFrameType(0),
andrew@webrtc.orgb015cbe2012-10-22 18:19:23 +0000902 _rtcpObserverPtr(NULL),
andrew@webrtc.orgb015cbe2012-10-22 18:19:23 +0000903 _externalPlayout(false),
roosa@google.comb9e3afc2012-12-12 23:00:29 +0000904 _externalMixing(false),
andrew@webrtc.orgb015cbe2012-10-22 18:19:23 +0000905 _mixFileWithMicrophone(false),
andrew@webrtc.orgb015cbe2012-10-22 18:19:23 +0000906 _rtcpObserver(false),
907 _mute(false),
908 _panLeft(1.0f),
909 _panRight(1.0f),
910 _outputGain(1.0f),
andrew@webrtc.orgb015cbe2012-10-22 18:19:23 +0000911 _playOutbandDtmfEvent(false),
912 _playInbandDtmfEvent(false),
andrew@webrtc.orgb015cbe2012-10-22 18:19:23 +0000913 _lastLocalTimeStamp(0),
914 _lastPayloadType(0),
915 _includeAudioLevelIndication(false),
916 _rtpPacketTimedOut(false),
917 _rtpPacketTimeOutIsEnabled(false),
918 _rtpTimeOutSeconds(0),
919 _connectionObserver(false),
920 _connectionObserverPtr(NULL),
andrew@webrtc.orgb015cbe2012-10-22 18:19:23 +0000921 _outputSpeechType(AudioFrame::kNormalSpeech),
solenberg@webrtc.orgfec6b6e2014-03-24 10:38:25 +0000922 vie_network_(NULL),
923 video_channel_(-1),
pwestin@webrtc.orgf2724972013-04-11 20:23:35 +0000924 _average_jitter_buffer_delay_us(0),
turaj@webrtc.orgd5577342013-05-22 20:39:43 +0000925 least_required_delay_ms_(0),
andrew@webrtc.orgb015cbe2012-10-22 18:19:23 +0000926 _previousTimestamp(0),
927 _recPacketDelayMs(20),
928 _RxVadDetection(false),
andrew@webrtc.orgb015cbe2012-10-22 18:19:23 +0000929 _rxAgcIsEnabled(false),
stefan@webrtc.orgdb74c612013-09-06 13:40:11 +0000930 _rxNsIsEnabled(false),
minyue@webrtc.orgdd671de2014-05-28 09:52:06 +0000931 restored_packet_in_use_(false),
932 bitrate_controller_(
933 BitrateController::CreateBitrateController(Clock::GetRealTimeClock(),
934 true)),
935 rtcp_bandwidth_observer_(
936 bitrate_controller_->CreateRtcpBandwidthObserver()),
minyue@webrtc.org31b38da2014-07-16 21:28:26 +0000937 send_bitrate_observer_(new VoEBitrateObserver(this)),
938 network_predictor_(new NetworkPredictor(Clock::GetRealTimeClock()))
andrew@webrtc.orgb015cbe2012-10-22 18:19:23 +0000939{
940 WEBRTC_TRACE(kTraceMemory, kTraceVoice, VoEId(_instanceId,_channelId),
941 "Channel::Channel() - ctor");
942 _inbandDtmfQueue.ResetDtmf();
943 _inbandDtmfGenerator.Init();
944 _outputAudioLevel.Clear();
945
946 RtpRtcp::Configuration configuration;
947 configuration.id = VoEModuleId(instanceId, channelId);
948 configuration.audio = true;
andrew@webrtc.orgb015cbe2012-10-22 18:19:23 +0000949 configuration.outgoing_transport = this;
950 configuration.rtcp_feedback = this;
951 configuration.audio_messages = this;
wu@webrtc.org7fc75bb2013-08-15 23:38:54 +0000952 configuration.receive_statistics = rtp_receive_statistics_.get();
minyue@webrtc.orgdd671de2014-05-28 09:52:06 +0000953 configuration.bandwidth_callback = rtcp_bandwidth_observer_.get();
andrew@webrtc.orgb015cbe2012-10-22 18:19:23 +0000954
955 _rtpRtcpModule.reset(RtpRtcp::CreateRtpRtcp(configuration));
sprang@webrtc.org4f1f5fa2013-12-19 13:26:02 +0000956
957 statistics_proxy_.reset(new StatisticsProxy(_rtpRtcpModule->SSRC()));
958 rtp_receive_statistics_->RegisterRtcpStatisticsCallback(
959 statistics_proxy_.get());
aluebs@webrtc.org1a07e422014-04-16 11:58:18 +0000960
961 Config audioproc_config;
962 audioproc_config.Set<ExperimentalAgc>(new ExperimentalAgc(false));
963 rx_audioproc_.reset(AudioProcessing::Create(audioproc_config));
andrew@webrtc.orgb015cbe2012-10-22 18:19:23 +0000964}
965
966Channel::~Channel()
967{
sprang@webrtc.org4f1f5fa2013-12-19 13:26:02 +0000968 rtp_receive_statistics_->RegisterRtcpStatisticsCallback(NULL);
andrew@webrtc.orgb015cbe2012-10-22 18:19:23 +0000969 WEBRTC_TRACE(kTraceMemory, kTraceVoice, VoEId(_instanceId,_channelId),
970 "Channel::~Channel() - dtor");
971
972 if (_outputExternalMedia)
973 {
974 DeRegisterExternalMediaProcessing(kPlaybackPerChannel);
975 }
henrika@webrtc.orgdf08c5d2014-03-18 10:32:33 +0000976 if (channel_state_.Get().input_external_media)
andrew@webrtc.orgb015cbe2012-10-22 18:19:23 +0000977 {
978 DeRegisterExternalMediaProcessing(kRecordingPerChannel);
979 }
980 StopSend();
andrew@webrtc.orgb015cbe2012-10-22 18:19:23 +0000981 StopPlayout();
982
983 {
984 CriticalSectionScoped cs(&_fileCritSect);
985 if (_inputFilePlayerPtr)
986 {
987 _inputFilePlayerPtr->RegisterModuleFileCallback(NULL);
988 _inputFilePlayerPtr->StopPlayingFile();
989 FilePlayer::DestroyFilePlayer(_inputFilePlayerPtr);
990 _inputFilePlayerPtr = NULL;
991 }
992 if (_outputFilePlayerPtr)
993 {
994 _outputFilePlayerPtr->RegisterModuleFileCallback(NULL);
995 _outputFilePlayerPtr->StopPlayingFile();
996 FilePlayer::DestroyFilePlayer(_outputFilePlayerPtr);
997 _outputFilePlayerPtr = NULL;
998 }
999 if (_outputFileRecorderPtr)
1000 {
1001 _outputFileRecorderPtr->RegisterModuleFileCallback(NULL);
1002 _outputFileRecorderPtr->StopRecording();
1003 FileRecorder::DestroyFileRecorder(_outputFileRecorderPtr);
1004 _outputFileRecorderPtr = NULL;
1005 }
1006 }
1007
1008 // The order to safely shutdown modules in a channel is:
1009 // 1. De-register callbacks in modules
1010 // 2. De-register modules in process thread
1011 // 3. Destroy modules
andrew@webrtc.org510ee1b2013-09-23 23:02:24 +00001012 if (audio_coding_->RegisterTransportCallback(NULL) == -1)
andrew@webrtc.orgb015cbe2012-10-22 18:19:23 +00001013 {
1014 WEBRTC_TRACE(kTraceWarning, kTraceVoice,
1015 VoEId(_instanceId,_channelId),
1016 "~Channel() failed to de-register transport callback"
1017 " (Audio coding module)");
1018 }
andrew@webrtc.org510ee1b2013-09-23 23:02:24 +00001019 if (audio_coding_->RegisterVADCallback(NULL) == -1)
andrew@webrtc.orgb015cbe2012-10-22 18:19:23 +00001020 {
1021 WEBRTC_TRACE(kTraceWarning, kTraceVoice,
1022 VoEId(_instanceId,_channelId),
1023 "~Channel() failed to de-register VAD callback"
1024 " (Audio coding module)");
1025 }
andrew@webrtc.orgb015cbe2012-10-22 18:19:23 +00001026 // De-register modules in process thread
andrew@webrtc.orgb015cbe2012-10-22 18:19:23 +00001027 if (_moduleProcessThreadPtr->DeRegisterModule(_rtpRtcpModule.get()) == -1)
1028 {
1029 WEBRTC_TRACE(kTraceInfo, kTraceVoice,
1030 VoEId(_instanceId,_channelId),
1031 "~Channel() failed to deregister RTP/RTCP module");
1032 }
andrew@webrtc.orgb015cbe2012-10-22 18:19:23 +00001033 // End of modules shutdown
1034
1035 // Delete other objects
solenberg@webrtc.orgfec6b6e2014-03-24 10:38:25 +00001036 if (vie_network_) {
1037 vie_network_->Release();
1038 vie_network_ = NULL;
1039 }
andrew@webrtc.orgb015cbe2012-10-22 18:19:23 +00001040 RtpDump::DestroyRtpDump(&_rtpDumpIn);
1041 RtpDump::DestroyRtpDump(&_rtpDumpOut);
andrew@webrtc.orgb015cbe2012-10-22 18:19:23 +00001042 delete &_callbackCritSect;
1043 delete &_fileCritSect;
wu@webrtc.orgf7651ef2013-10-17 18:28:55 +00001044 delete &volume_settings_critsect_;
andrew@webrtc.orgb015cbe2012-10-22 18:19:23 +00001045}
1046
pbos@webrtc.org54f03bc2013-04-09 10:09:10 +00001047int32_t
andrew@webrtc.orgb015cbe2012-10-22 18:19:23 +00001048Channel::Init()
1049{
1050 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId,_channelId),
1051 "Channel::Init()");
1052
henrika@webrtc.orgdf08c5d2014-03-18 10:32:33 +00001053 channel_state_.Reset();
1054
andrew@webrtc.orgb015cbe2012-10-22 18:19:23 +00001055 // --- Initial sanity
1056
1057 if ((_engineStatisticsPtr == NULL) ||
1058 (_moduleProcessThreadPtr == NULL))
1059 {
1060 WEBRTC_TRACE(kTraceError, kTraceVoice,
1061 VoEId(_instanceId,_channelId),
1062 "Channel::Init() must call SetEngineInformation() first");
1063 return -1;
1064 }
1065
1066 // --- Add modules to process thread (for periodic schedulation)
1067
1068 const bool processThreadFail =
1069 ((_moduleProcessThreadPtr->RegisterModule(_rtpRtcpModule.get()) != 0) ||
andrew@webrtc.orgb015cbe2012-10-22 18:19:23 +00001070 false);
andrew@webrtc.orgb015cbe2012-10-22 18:19:23 +00001071 if (processThreadFail)
1072 {
1073 _engineStatisticsPtr->SetLastError(
1074 VE_CANNOT_INIT_CHANNEL, kTraceError,
1075 "Channel::Init() modules not registered");
1076 return -1;
1077 }
1078 // --- ACM initialization
1079
andrew@webrtc.org510ee1b2013-09-23 23:02:24 +00001080 if ((audio_coding_->InitializeReceiver() == -1) ||
andrew@webrtc.orgb015cbe2012-10-22 18:19:23 +00001081#ifdef WEBRTC_CODEC_AVT
1082 // out-of-band Dtmf tones are played out by default
andrew@webrtc.org510ee1b2013-09-23 23:02:24 +00001083 (audio_coding_->SetDtmfPlayoutStatus(true) == -1) ||
andrew@webrtc.orgb015cbe2012-10-22 18:19:23 +00001084#endif
andrew@webrtc.org510ee1b2013-09-23 23:02:24 +00001085 (audio_coding_->InitializeSender() == -1))
andrew@webrtc.orgb015cbe2012-10-22 18:19:23 +00001086 {
1087 _engineStatisticsPtr->SetLastError(
1088 VE_AUDIO_CODING_MODULE_ERROR, kTraceError,
1089 "Channel::Init() unable to initialize the ACM - 1");
1090 return -1;
1091 }
1092
1093 // --- RTP/RTCP module initialization
1094
1095 // Ensure that RTCP is enabled by default for the created channel.
1096 // Note that, the module will keep generating RTCP until it is explicitly
1097 // disabled by the user.
1098 // After StopListen (when no sockets exists), RTCP packets will no longer
1099 // be transmitted since the Transport object will then be invalid.
wu@webrtc.org7fc75bb2013-08-15 23:38:54 +00001100 telephone_event_handler_->SetTelephoneEventForwardToDecoder(true);
1101 // RTCP is enabled by default.
1102 if (_rtpRtcpModule->SetRTCPStatus(kRtcpCompound) == -1)
andrew@webrtc.orgb015cbe2012-10-22 18:19:23 +00001103 {
1104 _engineStatisticsPtr->SetLastError(
1105 VE_RTP_RTCP_MODULE_ERROR, kTraceError,
1106 "Channel::Init() RTP/RTCP module not initialized");
1107 return -1;
1108 }
1109
1110 // --- Register all permanent callbacks
1111 const bool fail =
andrew@webrtc.org510ee1b2013-09-23 23:02:24 +00001112 (audio_coding_->RegisterTransportCallback(this) == -1) ||
1113 (audio_coding_->RegisterVADCallback(this) == -1);
andrew@webrtc.orgb015cbe2012-10-22 18:19:23 +00001114
1115 if (fail)
1116 {
1117 _engineStatisticsPtr->SetLastError(
1118 VE_CANNOT_INIT_CHANNEL, kTraceError,
1119 "Channel::Init() callbacks not registered");
1120 return -1;
1121 }
1122
1123 // --- Register all supported codecs to the receiving side of the
1124 // RTP/RTCP module
1125
1126 CodecInst codec;
pbos@webrtc.org54f03bc2013-04-09 10:09:10 +00001127 const uint8_t nSupportedCodecs = AudioCodingModule::NumberOfCodecs();
andrew@webrtc.orgb015cbe2012-10-22 18:19:23 +00001128
1129 for (int idx = 0; idx < nSupportedCodecs; idx++)
1130 {
1131 // Open up the RTP/RTCP receiver for all supported codecs
andrew@webrtc.org510ee1b2013-09-23 23:02:24 +00001132 if ((audio_coding_->Codec(idx, &codec) == -1) ||
wu@webrtc.org7fc75bb2013-08-15 23:38:54 +00001133 (rtp_receiver_->RegisterReceivePayload(
1134 codec.plname,
1135 codec.pltype,
1136 codec.plfreq,
1137 codec.channels,
1138 (codec.rate < 0) ? 0 : codec.rate) == -1))
andrew@webrtc.orgb015cbe2012-10-22 18:19:23 +00001139 {
1140 WEBRTC_TRACE(kTraceWarning, kTraceVoice,
1141 VoEId(_instanceId,_channelId),
1142 "Channel::Init() unable to register %s (%d/%d/%d/%d) "
1143 "to RTP/RTCP receiver",
1144 codec.plname, codec.pltype, codec.plfreq,
1145 codec.channels, codec.rate);
1146 }
1147 else
1148 {
1149 WEBRTC_TRACE(kTraceInfo, kTraceVoice,
1150 VoEId(_instanceId,_channelId),
1151 "Channel::Init() %s (%d/%d/%d/%d) has been added to "
1152 "the RTP/RTCP receiver",
1153 codec.plname, codec.pltype, codec.plfreq,
1154 codec.channels, codec.rate);
1155 }
1156
1157 // Ensure that PCMU is used as default codec on the sending side
1158 if (!STR_CASE_CMP(codec.plname, "PCMU") && (codec.channels == 1))
1159 {
1160 SetSendCodec(codec);
1161 }
1162
1163 // Register default PT for outband 'telephone-event'
1164 if (!STR_CASE_CMP(codec.plname, "telephone-event"))
1165 {
1166 if ((_rtpRtcpModule->RegisterSendPayload(codec) == -1) ||
andrew@webrtc.org510ee1b2013-09-23 23:02:24 +00001167 (audio_coding_->RegisterReceiveCodec(codec) == -1))
andrew@webrtc.orgb015cbe2012-10-22 18:19:23 +00001168 {
1169 WEBRTC_TRACE(kTraceWarning, kTraceVoice,
1170 VoEId(_instanceId,_channelId),
1171 "Channel::Init() failed to register outband "
1172 "'telephone-event' (%d/%d) correctly",
1173 codec.pltype, codec.plfreq);
1174 }
1175 }
1176
1177 if (!STR_CASE_CMP(codec.plname, "CN"))
1178 {
andrew@webrtc.org510ee1b2013-09-23 23:02:24 +00001179 if ((audio_coding_->RegisterSendCodec(codec) == -1) ||
1180 (audio_coding_->RegisterReceiveCodec(codec) == -1) ||
andrew@webrtc.orgb015cbe2012-10-22 18:19:23 +00001181 (_rtpRtcpModule->RegisterSendPayload(codec) == -1))
1182 {
1183 WEBRTC_TRACE(kTraceWarning, kTraceVoice,
1184 VoEId(_instanceId,_channelId),
1185 "Channel::Init() failed to register CN (%d/%d) "
1186 "correctly - 1",
1187 codec.pltype, codec.plfreq);
1188 }
1189 }
1190#ifdef WEBRTC_CODEC_RED
1191 // Register RED to the receiving side of the ACM.
1192 // We will not receive an OnInitializeDecoder() callback for RED.
1193 if (!STR_CASE_CMP(codec.plname, "RED"))
1194 {
andrew@webrtc.org510ee1b2013-09-23 23:02:24 +00001195 if (audio_coding_->RegisterReceiveCodec(codec) == -1)
andrew@webrtc.orgb015cbe2012-10-22 18:19:23 +00001196 {
1197 WEBRTC_TRACE(kTraceWarning, kTraceVoice,
1198 VoEId(_instanceId,_channelId),
1199 "Channel::Init() failed to register RED (%d/%d) "
1200 "correctly",
1201 codec.pltype, codec.plfreq);
1202 }
1203 }
1204#endif
1205 }
pwestin@webrtc.org912b7f72013-03-13 23:20:57 +00001206
andrew@webrtc.orge06943f2013-10-04 17:54:09 +00001207 if (rx_audioproc_->noise_suppression()->set_level(kDefaultNsMode) != 0) {
1208 LOG_FERR1(LS_ERROR, noise_suppression()->set_level, kDefaultNsMode);
1209 return -1;
andrew@webrtc.orgb015cbe2012-10-22 18:19:23 +00001210 }
andrew@webrtc.orge06943f2013-10-04 17:54:09 +00001211 if (rx_audioproc_->gain_control()->set_mode(kDefaultRxAgcMode) != 0) {
1212 LOG_FERR1(LS_ERROR, gain_control()->set_mode, kDefaultRxAgcMode);
1213 return -1;
andrew@webrtc.orgb015cbe2012-10-22 18:19:23 +00001214 }
1215
1216 return 0;
1217}
1218
pbos@webrtc.org54f03bc2013-04-09 10:09:10 +00001219int32_t
andrew@webrtc.orgb015cbe2012-10-22 18:19:23 +00001220Channel::SetEngineInformation(Statistics& engineStatistics,
1221 OutputMixer& outputMixer,
1222 voe::TransmitMixer& transmitMixer,
1223 ProcessThread& moduleProcessThread,
1224 AudioDeviceModule& audioDeviceModule,
1225 VoiceEngineObserver* voiceEngineObserver,
1226 CriticalSectionWrapper* callbackCritSect)
1227{
1228 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId,_channelId),
1229 "Channel::SetEngineInformation()");
1230 _engineStatisticsPtr = &engineStatistics;
1231 _outputMixerPtr = &outputMixer;
1232 _transmitMixerPtr = &transmitMixer,
1233 _moduleProcessThreadPtr = &moduleProcessThread;
1234 _audioDeviceModulePtr = &audioDeviceModule;
1235 _voiceEngineObserverPtr = voiceEngineObserver;
1236 _callbackCritSectPtr = callbackCritSect;
1237 return 0;
1238}
1239
pbos@webrtc.org54f03bc2013-04-09 10:09:10 +00001240int32_t
andrew@webrtc.orgb015cbe2012-10-22 18:19:23 +00001241Channel::UpdateLocalTimeStamp()
1242{
1243
1244 _timeStamp += _audioFrame.samples_per_channel_;
1245 return 0;
1246}
1247
pbos@webrtc.org54f03bc2013-04-09 10:09:10 +00001248int32_t
andrew@webrtc.orgb015cbe2012-10-22 18:19:23 +00001249Channel::StartPlayout()
1250{
1251 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId,_channelId),
1252 "Channel::StartPlayout()");
henrika@webrtc.orgdf08c5d2014-03-18 10:32:33 +00001253 if (channel_state_.Get().playing)
andrew@webrtc.orgb015cbe2012-10-22 18:19:23 +00001254 {
1255 return 0;
1256 }
roosa@google.comb9e3afc2012-12-12 23:00:29 +00001257
1258 if (!_externalMixing) {
1259 // Add participant as candidates for mixing.
1260 if (_outputMixerPtr->SetMixabilityStatus(*this, true) != 0)
1261 {
1262 _engineStatisticsPtr->SetLastError(
1263 VE_AUDIO_CONF_MIX_MODULE_ERROR, kTraceError,
1264 "StartPlayout() failed to add participant to mixer");
1265 return -1;
1266 }
andrew@webrtc.orgb015cbe2012-10-22 18:19:23 +00001267 }
1268
henrika@webrtc.orgdf08c5d2014-03-18 10:32:33 +00001269 channel_state_.SetPlaying(true);
andrew@webrtc.orgb015cbe2012-10-22 18:19:23 +00001270 if (RegisterFilePlayingToMixer() != 0)
1271 return -1;
1272
1273 return 0;
1274}
1275
pbos@webrtc.org54f03bc2013-04-09 10:09:10 +00001276int32_t
andrew@webrtc.orgb015cbe2012-10-22 18:19:23 +00001277Channel::StopPlayout()
1278{
1279 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId,_channelId),
1280 "Channel::StopPlayout()");
henrika@webrtc.orgdf08c5d2014-03-18 10:32:33 +00001281 if (!channel_state_.Get().playing)
andrew@webrtc.orgb015cbe2012-10-22 18:19:23 +00001282 {
1283 return 0;
1284 }
roosa@google.comb9e3afc2012-12-12 23:00:29 +00001285
1286 if (!_externalMixing) {
1287 // Remove participant as candidates for mixing
1288 if (_outputMixerPtr->SetMixabilityStatus(*this, false) != 0)
1289 {
1290 _engineStatisticsPtr->SetLastError(
1291 VE_AUDIO_CONF_MIX_MODULE_ERROR, kTraceError,
1292 "StopPlayout() failed to remove participant from mixer");
1293 return -1;
1294 }
andrew@webrtc.orgb015cbe2012-10-22 18:19:23 +00001295 }
1296
henrika@webrtc.orgdf08c5d2014-03-18 10:32:33 +00001297 channel_state_.SetPlaying(false);
andrew@webrtc.orgb015cbe2012-10-22 18:19:23 +00001298 _outputAudioLevel.Clear();
1299
1300 return 0;
1301}
1302
pbos@webrtc.org54f03bc2013-04-09 10:09:10 +00001303int32_t
andrew@webrtc.orgb015cbe2012-10-22 18:19:23 +00001304Channel::StartSend()
1305{
1306 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId,_channelId),
1307 "Channel::StartSend()");
xians@webrtc.org5ce87232013-07-31 16:30:19 +00001308 // Resume the previous sequence number which was reset by StopSend().
henrika@webrtc.orgdf08c5d2014-03-18 10:32:33 +00001309 // This needs to be done before |sending| is set to true.
xians@webrtc.org5ce87232013-07-31 16:30:19 +00001310 if (send_sequence_number_)
1311 SetInitSequenceNumber(send_sequence_number_);
1312
henrika@webrtc.orgdf08c5d2014-03-18 10:32:33 +00001313 if (channel_state_.Get().sending)
andrew@webrtc.orgb015cbe2012-10-22 18:19:23 +00001314 {
henrika@webrtc.orgdf08c5d2014-03-18 10:32:33 +00001315 return 0;
andrew@webrtc.orgb015cbe2012-10-22 18:19:23 +00001316 }
henrika@webrtc.orgdf08c5d2014-03-18 10:32:33 +00001317 channel_state_.SetSending(true);
andrew@webrtc.orgb015cbe2012-10-22 18:19:23 +00001318
1319 if (_rtpRtcpModule->SetSendingStatus(true) != 0)
1320 {
1321 _engineStatisticsPtr->SetLastError(
1322 VE_RTP_RTCP_MODULE_ERROR, kTraceError,
1323 "StartSend() RTP/RTCP failed to start sending");
1324 CriticalSectionScoped cs(&_callbackCritSect);
henrika@webrtc.orgdf08c5d2014-03-18 10:32:33 +00001325 channel_state_.SetSending(false);
andrew@webrtc.orgb015cbe2012-10-22 18:19:23 +00001326 return -1;
1327 }
1328
1329 return 0;
1330}
1331
pbos@webrtc.org54f03bc2013-04-09 10:09:10 +00001332int32_t
andrew@webrtc.orgb015cbe2012-10-22 18:19:23 +00001333Channel::StopSend()
1334{
1335 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId,_channelId),
1336 "Channel::StopSend()");
henrika@webrtc.orgdf08c5d2014-03-18 10:32:33 +00001337 if (!channel_state_.Get().sending)
andrew@webrtc.orgb015cbe2012-10-22 18:19:23 +00001338 {
henrika@webrtc.orgdf08c5d2014-03-18 10:32:33 +00001339 return 0;
andrew@webrtc.orgb015cbe2012-10-22 18:19:23 +00001340 }
henrika@webrtc.orgdf08c5d2014-03-18 10:32:33 +00001341 channel_state_.SetSending(false);
andrew@webrtc.orgb015cbe2012-10-22 18:19:23 +00001342
xians@webrtc.org5ce87232013-07-31 16:30:19 +00001343 // Store the sequence number to be able to pick up the same sequence for
1344 // the next StartSend(). This is needed for restarting device, otherwise
1345 // it might cause libSRTP to complain about packets being replayed.
1346 // TODO(xians): Remove this workaround after RtpRtcpModule's refactoring
1347 // CL is landed. See issue
1348 // https://code.google.com/p/webrtc/issues/detail?id=2111 .
1349 send_sequence_number_ = _rtpRtcpModule->SequenceNumber();
1350
andrew@webrtc.orgb015cbe2012-10-22 18:19:23 +00001351 // Reset sending SSRC and sequence number and triggers direct transmission
1352 // of RTCP BYE
1353 if (_rtpRtcpModule->SetSendingStatus(false) == -1 ||
1354 _rtpRtcpModule->ResetSendDataCountersRTP() == -1)
1355 {
1356 _engineStatisticsPtr->SetLastError(
1357 VE_RTP_RTCP_MODULE_ERROR, kTraceWarning,
1358 "StartSend() RTP/RTCP failed to stop sending");
1359 }
1360
1361 return 0;
1362}
1363
pbos@webrtc.org54f03bc2013-04-09 10:09:10 +00001364int32_t
andrew@webrtc.orgb015cbe2012-10-22 18:19:23 +00001365Channel::StartReceiving()
1366{
1367 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId,_channelId),
1368 "Channel::StartReceiving()");
henrika@webrtc.orgdf08c5d2014-03-18 10:32:33 +00001369 if (channel_state_.Get().receiving)
andrew@webrtc.orgb015cbe2012-10-22 18:19:23 +00001370 {
1371 return 0;
1372 }
henrika@webrtc.orgdf08c5d2014-03-18 10:32:33 +00001373 channel_state_.SetReceiving(true);
andrew@webrtc.orgb015cbe2012-10-22 18:19:23 +00001374 _numberOfDiscardedPackets = 0;
1375 return 0;
1376}
1377
pbos@webrtc.org54f03bc2013-04-09 10:09:10 +00001378int32_t
andrew@webrtc.orgb015cbe2012-10-22 18:19:23 +00001379Channel::StopReceiving()
1380{
1381 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId,_channelId),
1382 "Channel::StopReceiving()");
henrika@webrtc.orgdf08c5d2014-03-18 10:32:33 +00001383 if (!channel_state_.Get().receiving)
andrew@webrtc.orgb015cbe2012-10-22 18:19:23 +00001384 {
1385 return 0;
1386 }
pwestin@webrtc.org912b7f72013-03-13 23:20:57 +00001387
henrika@webrtc.orgdf08c5d2014-03-18 10:32:33 +00001388 channel_state_.SetReceiving(false);
andrew@webrtc.orgb015cbe2012-10-22 18:19:23 +00001389 return 0;
1390}
1391
pbos@webrtc.org54f03bc2013-04-09 10:09:10 +00001392int32_t
andrew@webrtc.orgb015cbe2012-10-22 18:19:23 +00001393Channel::SetNetEQPlayoutMode(NetEqModes mode)
1394{
1395 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId,_channelId),
1396 "Channel::SetNetEQPlayoutMode()");
1397 AudioPlayoutMode playoutMode(voice);
1398 switch (mode)
1399 {
1400 case kNetEqDefault:
1401 playoutMode = voice;
1402 break;
1403 case kNetEqStreaming:
1404 playoutMode = streaming;
1405 break;
1406 case kNetEqFax:
1407 playoutMode = fax;
1408 break;
roosa@google.com90d333e2012-12-12 21:59:14 +00001409 case kNetEqOff:
1410 playoutMode = off;
1411 break;
andrew@webrtc.orgb015cbe2012-10-22 18:19:23 +00001412 }
andrew@webrtc.org510ee1b2013-09-23 23:02:24 +00001413 if (audio_coding_->SetPlayoutMode(playoutMode) != 0)
andrew@webrtc.orgb015cbe2012-10-22 18:19:23 +00001414 {
1415 _engineStatisticsPtr->SetLastError(
1416 VE_AUDIO_CODING_MODULE_ERROR, kTraceError,
1417 "SetNetEQPlayoutMode() failed to set playout mode");
1418 return -1;
1419 }
1420 return 0;
1421}
1422
pbos@webrtc.org54f03bc2013-04-09 10:09:10 +00001423int32_t
andrew@webrtc.orgb015cbe2012-10-22 18:19:23 +00001424Channel::GetNetEQPlayoutMode(NetEqModes& mode)
1425{
andrew@webrtc.org510ee1b2013-09-23 23:02:24 +00001426 const AudioPlayoutMode playoutMode = audio_coding_->PlayoutMode();
andrew@webrtc.orgb015cbe2012-10-22 18:19:23 +00001427 switch (playoutMode)
1428 {
1429 case voice:
1430 mode = kNetEqDefault;
1431 break;
1432 case streaming:
1433 mode = kNetEqStreaming;
1434 break;
1435 case fax:
1436 mode = kNetEqFax;
1437 break;
roosa@google.com90d333e2012-12-12 21:59:14 +00001438 case off:
1439 mode = kNetEqOff;
andrew@webrtc.orgb015cbe2012-10-22 18:19:23 +00001440 }
1441 WEBRTC_TRACE(kTraceStateInfo, kTraceVoice,
1442 VoEId(_instanceId,_channelId),
1443 "Channel::GetNetEQPlayoutMode() => mode=%u", mode);
1444 return 0;
1445}
1446
pbos@webrtc.org54f03bc2013-04-09 10:09:10 +00001447int32_t
andrew@webrtc.orgb015cbe2012-10-22 18:19:23 +00001448Channel::RegisterVoiceEngineObserver(VoiceEngineObserver& observer)
1449{
1450 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId,_channelId),
1451 "Channel::RegisterVoiceEngineObserver()");
1452 CriticalSectionScoped cs(&_callbackCritSect);
1453
1454 if (_voiceEngineObserverPtr)
1455 {
1456 _engineStatisticsPtr->SetLastError(
1457 VE_INVALID_OPERATION, kTraceError,
1458 "RegisterVoiceEngineObserver() observer already enabled");
1459 return -1;
1460 }
1461 _voiceEngineObserverPtr = &observer;
1462 return 0;
1463}
1464
pbos@webrtc.org54f03bc2013-04-09 10:09:10 +00001465int32_t
andrew@webrtc.orgb015cbe2012-10-22 18:19:23 +00001466Channel::DeRegisterVoiceEngineObserver()
1467{
1468 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId,_channelId),
1469 "Channel::DeRegisterVoiceEngineObserver()");
1470 CriticalSectionScoped cs(&_callbackCritSect);
1471
1472 if (!_voiceEngineObserverPtr)
1473 {
1474 _engineStatisticsPtr->SetLastError(
1475 VE_INVALID_OPERATION, kTraceWarning,
1476 "DeRegisterVoiceEngineObserver() observer already disabled");
1477 return 0;
1478 }
1479 _voiceEngineObserverPtr = NULL;
1480 return 0;
1481}
1482
pbos@webrtc.org54f03bc2013-04-09 10:09:10 +00001483int32_t
andrew@webrtc.orgb015cbe2012-10-22 18:19:23 +00001484Channel::GetSendCodec(CodecInst& codec)
1485{
andrew@webrtc.org510ee1b2013-09-23 23:02:24 +00001486 return (audio_coding_->SendCodec(&codec));
andrew@webrtc.orgb015cbe2012-10-22 18:19:23 +00001487}
1488
pbos@webrtc.org54f03bc2013-04-09 10:09:10 +00001489int32_t
andrew@webrtc.orgb015cbe2012-10-22 18:19:23 +00001490Channel::GetRecCodec(CodecInst& codec)
1491{
andrew@webrtc.org510ee1b2013-09-23 23:02:24 +00001492 return (audio_coding_->ReceiveCodec(&codec));
andrew@webrtc.orgb015cbe2012-10-22 18:19:23 +00001493}
1494
pbos@webrtc.org54f03bc2013-04-09 10:09:10 +00001495int32_t
andrew@webrtc.orgb015cbe2012-10-22 18:19:23 +00001496Channel::SetSendCodec(const CodecInst& codec)
1497{
1498 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId,_channelId),
1499 "Channel::SetSendCodec()");
1500
andrew@webrtc.org510ee1b2013-09-23 23:02:24 +00001501 if (audio_coding_->RegisterSendCodec(codec) != 0)
andrew@webrtc.orgb015cbe2012-10-22 18:19:23 +00001502 {
1503 WEBRTC_TRACE(kTraceError, kTraceVoice, VoEId(_instanceId,_channelId),
1504 "SetSendCodec() failed to register codec to ACM");
1505 return -1;
1506 }
1507
1508 if (_rtpRtcpModule->RegisterSendPayload(codec) != 0)
1509 {
1510 _rtpRtcpModule->DeRegisterSendPayload(codec.pltype);
1511 if (_rtpRtcpModule->RegisterSendPayload(codec) != 0)
1512 {
1513 WEBRTC_TRACE(
1514 kTraceError, kTraceVoice, VoEId(_instanceId,_channelId),
1515 "SetSendCodec() failed to register codec to"
1516 " RTP/RTCP module");
1517 return -1;
1518 }
1519 }
1520
1521 if (_rtpRtcpModule->SetAudioPacketSize(codec.pacsize) != 0)
1522 {
1523 WEBRTC_TRACE(kTraceError, kTraceVoice, VoEId(_instanceId,_channelId),
1524 "SetSendCodec() failed to set audio packet size");
1525 return -1;
1526 }
1527
minyue@webrtc.orgdd671de2014-05-28 09:52:06 +00001528 bitrate_controller_->SetBitrateObserver(send_bitrate_observer_.get(),
1529 codec.rate, 0, 0);
1530
andrew@webrtc.orgb015cbe2012-10-22 18:19:23 +00001531 return 0;
1532}
1533
minyue@webrtc.orgdd671de2014-05-28 09:52:06 +00001534void
1535Channel::OnNetworkChanged(const uint32_t bitrate_bps,
1536 const uint8_t fraction_lost, // 0 - 255.
1537 const uint32_t rtt) {
1538 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId,_channelId),
1539 "Channel::OnNetworkChanged(bitrate_bps=%d, fration_lost=%d, rtt=%d)",
1540 bitrate_bps, fraction_lost, rtt);
minyue@webrtc.org31b38da2014-07-16 21:28:26 +00001541 // |fraction_lost| from BitrateObserver is short time observation of packet
1542 // loss rate from past. We use network predictor to make a more reasonable
1543 // loss rate estimation.
1544 network_predictor_->UpdatePacketLossRate(fraction_lost);
1545 uint8_t loss_rate = network_predictor_->GetLossRate();
minyue@webrtc.orgdd671de2014-05-28 09:52:06 +00001546 // Normalizes rate to 0 - 100.
minyue@webrtc.org31b38da2014-07-16 21:28:26 +00001547 if (audio_coding_->SetPacketLossRate(100 * loss_rate / 255) != 0) {
minyue@webrtc.orgdd671de2014-05-28 09:52:06 +00001548 _engineStatisticsPtr->SetLastError(VE_AUDIO_CODING_MODULE_ERROR,
1549 kTraceError, "OnNetworkChanged() failed to set packet loss rate");
1550 assert(false); // This should not happen.
1551 }
1552}
1553
pbos@webrtc.org54f03bc2013-04-09 10:09:10 +00001554int32_t
andrew@webrtc.orgb015cbe2012-10-22 18:19:23 +00001555Channel::SetVADStatus(bool enableVAD, ACMVADMode mode, bool disableDTX)
1556{
1557 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId,_channelId),
1558 "Channel::SetVADStatus(mode=%d)", mode);
1559 // To disable VAD, DTX must be disabled too
1560 disableDTX = ((enableVAD == false) ? true : disableDTX);
andrew@webrtc.org510ee1b2013-09-23 23:02:24 +00001561 if (audio_coding_->SetVAD(!disableDTX, enableVAD, mode) != 0)
andrew@webrtc.orgb015cbe2012-10-22 18:19:23 +00001562 {
1563 _engineStatisticsPtr->SetLastError(
1564 VE_AUDIO_CODING_MODULE_ERROR, kTraceError,
1565 "SetVADStatus() failed to set VAD");
1566 return -1;
1567 }
1568 return 0;
1569}
1570
pbos@webrtc.org54f03bc2013-04-09 10:09:10 +00001571int32_t
andrew@webrtc.orgb015cbe2012-10-22 18:19:23 +00001572Channel::GetVADStatus(bool& enabledVAD, ACMVADMode& mode, bool& disabledDTX)
1573{
1574 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId,_channelId),
1575 "Channel::GetVADStatus");
andrew@webrtc.org510ee1b2013-09-23 23:02:24 +00001576 if (audio_coding_->VAD(&disabledDTX, &enabledVAD, &mode) != 0)
andrew@webrtc.orgb015cbe2012-10-22 18:19:23 +00001577 {
1578 _engineStatisticsPtr->SetLastError(
1579 VE_AUDIO_CODING_MODULE_ERROR, kTraceError,
1580 "GetVADStatus() failed to get VAD status");
1581 return -1;
1582 }
1583 disabledDTX = !disabledDTX;
1584 return 0;
1585}
1586
pbos@webrtc.org54f03bc2013-04-09 10:09:10 +00001587int32_t
andrew@webrtc.orgb015cbe2012-10-22 18:19:23 +00001588Channel::SetRecPayloadType(const CodecInst& codec)
1589{
1590 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId,_channelId),
1591 "Channel::SetRecPayloadType()");
1592
henrika@webrtc.orgdf08c5d2014-03-18 10:32:33 +00001593 if (channel_state_.Get().playing)
andrew@webrtc.orgb015cbe2012-10-22 18:19:23 +00001594 {
1595 _engineStatisticsPtr->SetLastError(
1596 VE_ALREADY_PLAYING, kTraceError,
1597 "SetRecPayloadType() unable to set PT while playing");
1598 return -1;
1599 }
henrika@webrtc.orgdf08c5d2014-03-18 10:32:33 +00001600 if (channel_state_.Get().receiving)
andrew@webrtc.orgb015cbe2012-10-22 18:19:23 +00001601 {
1602 _engineStatisticsPtr->SetLastError(
1603 VE_ALREADY_LISTENING, kTraceError,
1604 "SetRecPayloadType() unable to set PT while listening");
1605 return -1;
1606 }
1607
1608 if (codec.pltype == -1)
1609 {
1610 // De-register the selected codec (RTP/RTCP module and ACM)
1611
pbos@webrtc.org54f03bc2013-04-09 10:09:10 +00001612 int8_t pltype(-1);
andrew@webrtc.orgb015cbe2012-10-22 18:19:23 +00001613 CodecInst rxCodec = codec;
1614
1615 // Get payload type for the given codec
wu@webrtc.org7fc75bb2013-08-15 23:38:54 +00001616 rtp_payload_registry_->ReceivePayloadType(
1617 rxCodec.plname,
1618 rxCodec.plfreq,
1619 rxCodec.channels,
1620 (rxCodec.rate < 0) ? 0 : rxCodec.rate,
1621 &pltype);
andrew@webrtc.orgb015cbe2012-10-22 18:19:23 +00001622 rxCodec.pltype = pltype;
1623
wu@webrtc.org7fc75bb2013-08-15 23:38:54 +00001624 if (rtp_receiver_->DeRegisterReceivePayload(pltype) != 0)
andrew@webrtc.orgb015cbe2012-10-22 18:19:23 +00001625 {
1626 _engineStatisticsPtr->SetLastError(
1627 VE_RTP_RTCP_MODULE_ERROR,
1628 kTraceError,
1629 "SetRecPayloadType() RTP/RTCP-module deregistration "
1630 "failed");
1631 return -1;
1632 }
andrew@webrtc.org510ee1b2013-09-23 23:02:24 +00001633 if (audio_coding_->UnregisterReceiveCodec(rxCodec.pltype) != 0)
andrew@webrtc.orgb015cbe2012-10-22 18:19:23 +00001634 {
1635 _engineStatisticsPtr->SetLastError(
1636 VE_AUDIO_CODING_MODULE_ERROR, kTraceError,
1637 "SetRecPayloadType() ACM deregistration failed - 1");
1638 return -1;
1639 }
1640 return 0;
1641 }
1642
wu@webrtc.org7fc75bb2013-08-15 23:38:54 +00001643 if (rtp_receiver_->RegisterReceivePayload(
1644 codec.plname,
1645 codec.pltype,
1646 codec.plfreq,
1647 codec.channels,
1648 (codec.rate < 0) ? 0 : codec.rate) != 0)
andrew@webrtc.orgb015cbe2012-10-22 18:19:23 +00001649 {
1650 // First attempt to register failed => de-register and try again
wu@webrtc.org7fc75bb2013-08-15 23:38:54 +00001651 rtp_receiver_->DeRegisterReceivePayload(codec.pltype);
1652 if (rtp_receiver_->RegisterReceivePayload(
1653 codec.plname,
1654 codec.pltype,
1655 codec.plfreq,
1656 codec.channels,
1657 (codec.rate < 0) ? 0 : codec.rate) != 0)
andrew@webrtc.orgb015cbe2012-10-22 18:19:23 +00001658 {
1659 _engineStatisticsPtr->SetLastError(
1660 VE_RTP_RTCP_MODULE_ERROR, kTraceError,
1661 "SetRecPayloadType() RTP/RTCP-module registration failed");
1662 return -1;
1663 }
1664 }
andrew@webrtc.org510ee1b2013-09-23 23:02:24 +00001665 if (audio_coding_->RegisterReceiveCodec(codec) != 0)
andrew@webrtc.orgb015cbe2012-10-22 18:19:23 +00001666 {
andrew@webrtc.org510ee1b2013-09-23 23:02:24 +00001667 audio_coding_->UnregisterReceiveCodec(codec.pltype);
1668 if (audio_coding_->RegisterReceiveCodec(codec) != 0)
andrew@webrtc.orgb015cbe2012-10-22 18:19:23 +00001669 {
1670 _engineStatisticsPtr->SetLastError(
1671 VE_AUDIO_CODING_MODULE_ERROR, kTraceError,
1672 "SetRecPayloadType() ACM registration failed - 1");
1673 return -1;
1674 }
1675 }
1676 return 0;
1677}
1678
pbos@webrtc.org54f03bc2013-04-09 10:09:10 +00001679int32_t
andrew@webrtc.orgb015cbe2012-10-22 18:19:23 +00001680Channel::GetRecPayloadType(CodecInst& codec)
1681{
1682 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId,_channelId),
1683 "Channel::GetRecPayloadType()");
pbos@webrtc.org54f03bc2013-04-09 10:09:10 +00001684 int8_t payloadType(-1);
wu@webrtc.org7fc75bb2013-08-15 23:38:54 +00001685 if (rtp_payload_registry_->ReceivePayloadType(
1686 codec.plname,
1687 codec.plfreq,
1688 codec.channels,
1689 (codec.rate < 0) ? 0 : codec.rate,
1690 &payloadType) != 0)
andrew@webrtc.orgb015cbe2012-10-22 18:19:23 +00001691 {
1692 _engineStatisticsPtr->SetLastError(
1693 VE_RTP_RTCP_MODULE_ERROR, kTraceWarning,
1694 "GetRecPayloadType() failed to retrieve RX payload type");
1695 return -1;
1696 }
1697 codec.pltype = payloadType;
1698 WEBRTC_TRACE(kTraceStateInfo, kTraceVoice, VoEId(_instanceId,_channelId),
1699 "Channel::GetRecPayloadType() => pltype=%u", codec.pltype);
1700 return 0;
1701}
1702
pbos@webrtc.org54f03bc2013-04-09 10:09:10 +00001703int32_t
andrew@webrtc.orgb015cbe2012-10-22 18:19:23 +00001704Channel::SetSendCNPayloadType(int type, PayloadFrequencies frequency)
1705{
1706 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId,_channelId),
1707 "Channel::SetSendCNPayloadType()");
1708
1709 CodecInst codec;
pbos@webrtc.org54f03bc2013-04-09 10:09:10 +00001710 int32_t samplingFreqHz(-1);
andrew@webrtc.orgb015cbe2012-10-22 18:19:23 +00001711 const int kMono = 1;
1712 if (frequency == kFreq32000Hz)
1713 samplingFreqHz = 32000;
1714 else if (frequency == kFreq16000Hz)
1715 samplingFreqHz = 16000;
1716
andrew@webrtc.org510ee1b2013-09-23 23:02:24 +00001717 if (audio_coding_->Codec("CN", &codec, samplingFreqHz, kMono) == -1)
andrew@webrtc.orgb015cbe2012-10-22 18:19:23 +00001718 {
1719 _engineStatisticsPtr->SetLastError(
1720 VE_AUDIO_CODING_MODULE_ERROR, kTraceError,
1721 "SetSendCNPayloadType() failed to retrieve default CN codec "
1722 "settings");
1723 return -1;
1724 }
1725
1726 // Modify the payload type (must be set to dynamic range)
1727 codec.pltype = type;
1728
andrew@webrtc.org510ee1b2013-09-23 23:02:24 +00001729 if (audio_coding_->RegisterSendCodec(codec) != 0)
andrew@webrtc.orgb015cbe2012-10-22 18:19:23 +00001730 {
1731 _engineStatisticsPtr->SetLastError(
1732 VE_AUDIO_CODING_MODULE_ERROR, kTraceError,
1733 "SetSendCNPayloadType() failed to register CN to ACM");
1734 return -1;
1735 }
1736
1737 if (_rtpRtcpModule->RegisterSendPayload(codec) != 0)
1738 {
1739 _rtpRtcpModule->DeRegisterSendPayload(codec.pltype);
1740 if (_rtpRtcpModule->RegisterSendPayload(codec) != 0)
1741 {
1742 _engineStatisticsPtr->SetLastError(
1743 VE_RTP_RTCP_MODULE_ERROR, kTraceError,
1744 "SetSendCNPayloadType() failed to register CN to RTP/RTCP "
1745 "module");
1746 return -1;
1747 }
1748 }
1749 return 0;
1750}
1751
pbos@webrtc.org54f03bc2013-04-09 10:09:10 +00001752int32_t Channel::RegisterExternalTransport(Transport& transport)
andrew@webrtc.orgb015cbe2012-10-22 18:19:23 +00001753{
1754 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId, _channelId),
1755 "Channel::RegisterExternalTransport()");
1756
1757 CriticalSectionScoped cs(&_callbackCritSect);
1758
andrew@webrtc.orgb015cbe2012-10-22 18:19:23 +00001759 if (_externalTransport)
1760 {
1761 _engineStatisticsPtr->SetLastError(VE_INVALID_OPERATION,
1762 kTraceError,
1763 "RegisterExternalTransport() external transport already enabled");
1764 return -1;
1765 }
1766 _externalTransport = true;
1767 _transportPtr = &transport;
1768 return 0;
1769}
1770
pbos@webrtc.org54f03bc2013-04-09 10:09:10 +00001771int32_t
andrew@webrtc.orgb015cbe2012-10-22 18:19:23 +00001772Channel::DeRegisterExternalTransport()
1773{
1774 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId,_channelId),
1775 "Channel::DeRegisterExternalTransport()");
1776
1777 CriticalSectionScoped cs(&_callbackCritSect);
1778
1779 if (!_transportPtr)
1780 {
1781 _engineStatisticsPtr->SetLastError(
1782 VE_INVALID_OPERATION, kTraceWarning,
1783 "DeRegisterExternalTransport() external transport already "
1784 "disabled");
1785 return 0;
1786 }
1787 _externalTransport = false;
andrew@webrtc.orgb015cbe2012-10-22 18:19:23 +00001788 _transportPtr = NULL;
1789 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId,_channelId),
1790 "DeRegisterExternalTransport() all transport is disabled");
andrew@webrtc.orgb015cbe2012-10-22 18:19:23 +00001791 return 0;
1792}
1793
solenberg@webrtc.orgfec6b6e2014-03-24 10:38:25 +00001794int32_t Channel::ReceivedRTPPacket(const int8_t* data, int32_t length,
1795 const PacketTime& packet_time) {
pwestin@webrtc.orge4932182013-04-03 15:43:57 +00001796 WEBRTC_TRACE(kTraceStream, kTraceVoice, VoEId(_instanceId,_channelId),
1797 "Channel::ReceivedRTPPacket()");
1798
1799 // Store playout timestamp for the received RTP packet
pwestin@webrtc.orgf2724972013-04-11 20:23:35 +00001800 UpdatePlayoutTimestamp(false);
pwestin@webrtc.orge4932182013-04-03 15:43:57 +00001801
1802 // Dump the RTP packet to a file (if RTP dump is enabled).
pbos@webrtc.org54f03bc2013-04-09 10:09:10 +00001803 if (_rtpDumpIn.DumpPacket((const uint8_t*)data,
1804 (uint16_t)length) == -1) {
pwestin@webrtc.orge4932182013-04-03 15:43:57 +00001805 WEBRTC_TRACE(kTraceWarning, kTraceVoice,
1806 VoEId(_instanceId,_channelId),
1807 "Channel::SendPacket() RTP dump to input file failed");
1808 }
stefan@webrtc.orgdb74c612013-09-06 13:40:11 +00001809 const uint8_t* received_packet = reinterpret_cast<const uint8_t*>(data);
stefan@webrtc.org6696fba2013-05-29 12:12:51 +00001810 RTPHeader header;
stefan@webrtc.orgdb74c612013-09-06 13:40:11 +00001811 if (!rtp_header_parser_->Parse(received_packet, length, &header)) {
1812 WEBRTC_TRACE(webrtc::kTraceDebug, webrtc::kTraceVoice, _channelId,
1813 "Incoming packet: invalid RTP header");
stefan@webrtc.org6696fba2013-05-29 12:12:51 +00001814 return -1;
1815 }
wu@webrtc.org7fc75bb2013-08-15 23:38:54 +00001816 header.payload_type_frequency =
1817 rtp_payload_registry_->GetPayloadTypeFrequency(header.payloadType);
stefan@webrtc.orgdb74c612013-09-06 13:40:11 +00001818 if (header.payload_type_frequency < 0)
wu@webrtc.org7fc75bb2013-08-15 23:38:54 +00001819 return -1;
stefan@webrtc.org7e97e4c2013-11-08 15:18:52 +00001820 bool in_order = IsPacketInOrder(header);
stefan@webrtc.orgdb74c612013-09-06 13:40:11 +00001821 rtp_receive_statistics_->IncomingPacket(header, length,
stefan@webrtc.org7e97e4c2013-11-08 15:18:52 +00001822 IsPacketRetransmitted(header, in_order));
stefan@webrtc.orgdb74c612013-09-06 13:40:11 +00001823 rtp_payload_registry_->SetIncomingPayloadType(header);
solenberg@webrtc.orgfec6b6e2014-03-24 10:38:25 +00001824
1825 // Forward any packets to ViE bandwidth estimator, if enabled.
1826 {
1827 CriticalSectionScoped cs(&_callbackCritSect);
1828 if (vie_network_) {
1829 int64_t arrival_time_ms;
1830 if (packet_time.timestamp != -1) {
1831 arrival_time_ms = (packet_time.timestamp + 500) / 1000;
1832 } else {
1833 arrival_time_ms = TickTime::MillisecondTimestamp();
1834 }
1835 int payload_length = length - header.headerLength;
1836 vie_network_->ReceivedBWEPacket(video_channel_, arrival_time_ms,
1837 payload_length, header);
1838 }
1839 }
1840
stefan@webrtc.org7e97e4c2013-11-08 15:18:52 +00001841 return ReceivePacket(received_packet, length, header, in_order) ? 0 : -1;
stefan@webrtc.orgdb74c612013-09-06 13:40:11 +00001842}
1843
1844bool Channel::ReceivePacket(const uint8_t* packet,
1845 int packet_length,
1846 const RTPHeader& header,
1847 bool in_order) {
1848 if (rtp_payload_registry_->IsEncapsulated(header)) {
1849 return HandleEncapsulation(packet, packet_length, header);
wu@webrtc.org7fc75bb2013-08-15 23:38:54 +00001850 }
stefan@webrtc.orgdb74c612013-09-06 13:40:11 +00001851 const uint8_t* payload = packet + header.headerLength;
1852 int payload_length = packet_length - header.headerLength;
1853 assert(payload_length >= 0);
wu@webrtc.org7fc75bb2013-08-15 23:38:54 +00001854 PayloadUnion payload_specific;
1855 if (!rtp_payload_registry_->GetPayloadSpecifics(header.payloadType,
stefan@webrtc.orgdb74c612013-09-06 13:40:11 +00001856 &payload_specific)) {
1857 return false;
wu@webrtc.org7fc75bb2013-08-15 23:38:54 +00001858 }
stefan@webrtc.orgdb74c612013-09-06 13:40:11 +00001859 return rtp_receiver_->IncomingRtpPacket(header, payload, payload_length,
1860 payload_specific, in_order);
1861}
1862
1863bool Channel::HandleEncapsulation(const uint8_t* packet,
1864 int packet_length,
1865 const RTPHeader& header) {
1866 if (!rtp_payload_registry_->IsRtx(header))
1867 return false;
1868
1869 // Remove the RTX header and parse the original RTP header.
1870 if (packet_length < header.headerLength)
1871 return false;
1872 if (packet_length > kVoiceEngineMaxIpPacketSizeBytes)
1873 return false;
1874 if (restored_packet_in_use_) {
1875 WEBRTC_TRACE(webrtc::kTraceDebug, webrtc::kTraceVoice, _channelId,
1876 "Multiple RTX headers detected, dropping packet");
1877 return false;
pwestin@webrtc.orge4932182013-04-03 15:43:57 +00001878 }
stefan@webrtc.orgdb74c612013-09-06 13:40:11 +00001879 uint8_t* restored_packet_ptr = restored_packet_;
1880 if (!rtp_payload_registry_->RestoreOriginalPacket(
1881 &restored_packet_ptr, packet, &packet_length, rtp_receiver_->SSRC(),
1882 header)) {
1883 WEBRTC_TRACE(webrtc::kTraceDebug, webrtc::kTraceVoice, _channelId,
1884 "Incoming RTX packet: invalid RTP header");
1885 return false;
1886 }
1887 restored_packet_in_use_ = true;
1888 bool ret = OnRecoveredPacket(restored_packet_ptr, packet_length);
1889 restored_packet_in_use_ = false;
1890 return ret;
1891}
1892
1893bool Channel::IsPacketInOrder(const RTPHeader& header) const {
1894 StreamStatistician* statistician =
1895 rtp_receive_statistics_->GetStatistician(header.ssrc);
1896 if (!statistician)
1897 return false;
1898 return statistician->IsPacketInOrder(header.sequenceNumber);
andrew@webrtc.orgb015cbe2012-10-22 18:19:23 +00001899}
1900
stefan@webrtc.org7e97e4c2013-11-08 15:18:52 +00001901bool Channel::IsPacketRetransmitted(const RTPHeader& header,
1902 bool in_order) const {
stefan@webrtc.orgdb74c612013-09-06 13:40:11 +00001903 // Retransmissions are handled separately if RTX is enabled.
1904 if (rtp_payload_registry_->RtxEnabled())
1905 return false;
1906 StreamStatistician* statistician =
1907 rtp_receive_statistics_->GetStatistician(header.ssrc);
1908 if (!statistician)
1909 return false;
1910 // Check if this is a retransmission.
1911 uint16_t min_rtt = 0;
1912 _rtpRtcpModule->RTT(rtp_receiver_->SSRC(), NULL, NULL, &min_rtt, NULL);
stefan@webrtc.org7e97e4c2013-11-08 15:18:52 +00001913 return !in_order &&
stefan@webrtc.orgdb74c612013-09-06 13:40:11 +00001914 statistician->IsRetransmitOfOldPacket(header, min_rtt);
wu@webrtc.org7fc75bb2013-08-15 23:38:54 +00001915}
1916
pbos@webrtc.org54f03bc2013-04-09 10:09:10 +00001917int32_t Channel::ReceivedRTCPPacket(const int8_t* data, int32_t length) {
pwestin@webrtc.orge4932182013-04-03 15:43:57 +00001918 WEBRTC_TRACE(kTraceStream, kTraceVoice, VoEId(_instanceId,_channelId),
1919 "Channel::ReceivedRTCPPacket()");
1920 // Store playout timestamp for the received RTCP packet
pwestin@webrtc.orgf2724972013-04-11 20:23:35 +00001921 UpdatePlayoutTimestamp(true);
pwestin@webrtc.orge4932182013-04-03 15:43:57 +00001922
1923 // Dump the RTCP packet to a file (if RTP dump is enabled).
pbos@webrtc.org54f03bc2013-04-09 10:09:10 +00001924 if (_rtpDumpIn.DumpPacket((const uint8_t*)data,
1925 (uint16_t)length) == -1) {
pwestin@webrtc.orge4932182013-04-03 15:43:57 +00001926 WEBRTC_TRACE(kTraceWarning, kTraceVoice,
1927 VoEId(_instanceId,_channelId),
1928 "Channel::SendPacket() RTCP dump to input file failed");
1929 }
1930
1931 // Deliver RTCP packet to RTP/RTCP module for parsing
stefan@webrtc.org6696fba2013-05-29 12:12:51 +00001932 if (_rtpRtcpModule->IncomingRtcpPacket((const uint8_t*)data,
1933 (uint16_t)length) == -1) {
pwestin@webrtc.orge4932182013-04-03 15:43:57 +00001934 _engineStatisticsPtr->SetLastError(
1935 VE_SOCKET_TRANSPORT_MODULE_ERROR, kTraceWarning,
1936 "Channel::IncomingRTPPacket() RTCP packet is invalid");
1937 }
wu@webrtc.org881a32d2014-05-20 22:55:01 +00001938
1939 ntp_estimator_->UpdateRtcpTimestamp(rtp_receiver_->SSRC(),
1940 _rtpRtcpModule.get());
pwestin@webrtc.orge4932182013-04-03 15:43:57 +00001941 return 0;
andrew@webrtc.orgb015cbe2012-10-22 18:19:23 +00001942}
1943
andrew@webrtc.orgb015cbe2012-10-22 18:19:23 +00001944int Channel::StartPlayingFileLocally(const char* fileName,
pbos@webrtc.orgca7a9a22013-05-14 08:31:39 +00001945 bool loop,
1946 FileFormats format,
1947 int startPosition,
1948 float volumeScaling,
1949 int stopPosition,
andrew@webrtc.orgb015cbe2012-10-22 18:19:23 +00001950 const CodecInst* codecInst)
1951{
1952 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId,_channelId),
1953 "Channel::StartPlayingFileLocally(fileNameUTF8[]=%s, loop=%d,"
1954 " format=%d, volumeScaling=%5.3f, startPosition=%d, "
1955 "stopPosition=%d)", fileName, loop, format, volumeScaling,
1956 startPosition, stopPosition);
1957
henrika@webrtc.orgdf08c5d2014-03-18 10:32:33 +00001958 if (channel_state_.Get().output_file_playing)
andrew@webrtc.orgb015cbe2012-10-22 18:19:23 +00001959 {
1960 _engineStatisticsPtr->SetLastError(
1961 VE_ALREADY_PLAYING, kTraceError,
1962 "StartPlayingFileLocally() is already playing");
1963 return -1;
1964 }
1965
1966 {
1967 CriticalSectionScoped cs(&_fileCritSect);
1968
1969 if (_outputFilePlayerPtr)
1970 {
1971 _outputFilePlayerPtr->RegisterModuleFileCallback(NULL);
1972 FilePlayer::DestroyFilePlayer(_outputFilePlayerPtr);
1973 _outputFilePlayerPtr = NULL;
1974 }
1975
1976 _outputFilePlayerPtr = FilePlayer::CreateFilePlayer(
1977 _outputFilePlayerId, (const FileFormats)format);
1978
1979 if (_outputFilePlayerPtr == NULL)
1980 {
1981 _engineStatisticsPtr->SetLastError(
1982 VE_INVALID_ARGUMENT, kTraceError,
1983 "StartPlayingFileLocally() filePlayer format is not correct");
1984 return -1;
1985 }
1986
pbos@webrtc.org54f03bc2013-04-09 10:09:10 +00001987 const uint32_t notificationTime(0);
andrew@webrtc.orgb015cbe2012-10-22 18:19:23 +00001988
1989 if (_outputFilePlayerPtr->StartPlayingFile(
1990 fileName,
1991 loop,
1992 startPosition,
1993 volumeScaling,
1994 notificationTime,
1995 stopPosition,
1996 (const CodecInst*)codecInst) != 0)
1997 {
1998 _engineStatisticsPtr->SetLastError(
1999 VE_BAD_FILE, kTraceError,
2000 "StartPlayingFile() failed to start file playout");
2001 _outputFilePlayerPtr->StopPlayingFile();
2002 FilePlayer::DestroyFilePlayer(_outputFilePlayerPtr);
2003 _outputFilePlayerPtr = NULL;
2004 return -1;
2005 }
2006 _outputFilePlayerPtr->RegisterModuleFileCallback(this);
henrika@webrtc.orgdf08c5d2014-03-18 10:32:33 +00002007 channel_state_.SetOutputFilePlaying(true);
andrew@webrtc.orgb015cbe2012-10-22 18:19:23 +00002008 }
2009
2010 if (RegisterFilePlayingToMixer() != 0)
2011 return -1;
2012
2013 return 0;
2014}
2015
2016int Channel::StartPlayingFileLocally(InStream* stream,
pbos@webrtc.orgca7a9a22013-05-14 08:31:39 +00002017 FileFormats format,
2018 int startPosition,
2019 float volumeScaling,
2020 int stopPosition,
andrew@webrtc.orgb015cbe2012-10-22 18:19:23 +00002021 const CodecInst* codecInst)
2022{
2023 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId,_channelId),
2024 "Channel::StartPlayingFileLocally(format=%d,"
2025 " volumeScaling=%5.3f, startPosition=%d, stopPosition=%d)",
2026 format, volumeScaling, startPosition, stopPosition);
2027
2028 if(stream == NULL)
2029 {
2030 _engineStatisticsPtr->SetLastError(
2031 VE_BAD_FILE, kTraceError,
2032 "StartPlayingFileLocally() NULL as input stream");
2033 return -1;
2034 }
2035
2036
henrika@webrtc.orgdf08c5d2014-03-18 10:32:33 +00002037 if (channel_state_.Get().output_file_playing)
andrew@webrtc.orgb015cbe2012-10-22 18:19:23 +00002038 {
2039 _engineStatisticsPtr->SetLastError(
2040 VE_ALREADY_PLAYING, kTraceError,
2041 "StartPlayingFileLocally() is already playing");
2042 return -1;
2043 }
2044
2045 {
2046 CriticalSectionScoped cs(&_fileCritSect);
2047
2048 // Destroy the old instance
2049 if (_outputFilePlayerPtr)
2050 {
2051 _outputFilePlayerPtr->RegisterModuleFileCallback(NULL);
2052 FilePlayer::DestroyFilePlayer(_outputFilePlayerPtr);
2053 _outputFilePlayerPtr = NULL;
2054 }
2055
2056 // Create the instance
2057 _outputFilePlayerPtr = FilePlayer::CreateFilePlayer(
2058 _outputFilePlayerId,
2059 (const FileFormats)format);
2060
2061 if (_outputFilePlayerPtr == NULL)
2062 {
2063 _engineStatisticsPtr->SetLastError(
2064 VE_INVALID_ARGUMENT, kTraceError,
2065 "StartPlayingFileLocally() filePlayer format isnot correct");
2066 return -1;
2067 }
2068
pbos@webrtc.org54f03bc2013-04-09 10:09:10 +00002069 const uint32_t notificationTime(0);
andrew@webrtc.orgb015cbe2012-10-22 18:19:23 +00002070
2071 if (_outputFilePlayerPtr->StartPlayingFile(*stream, startPosition,
2072 volumeScaling,
2073 notificationTime,
2074 stopPosition, codecInst) != 0)
2075 {
2076 _engineStatisticsPtr->SetLastError(VE_BAD_FILE, kTraceError,
2077 "StartPlayingFile() failed to "
2078 "start file playout");
2079 _outputFilePlayerPtr->StopPlayingFile();
2080 FilePlayer::DestroyFilePlayer(_outputFilePlayerPtr);
2081 _outputFilePlayerPtr = NULL;
2082 return -1;
2083 }
2084 _outputFilePlayerPtr->RegisterModuleFileCallback(this);
henrika@webrtc.orgdf08c5d2014-03-18 10:32:33 +00002085 channel_state_.SetOutputFilePlaying(true);
andrew@webrtc.orgb015cbe2012-10-22 18:19:23 +00002086 }
2087
2088 if (RegisterFilePlayingToMixer() != 0)
2089 return -1;
2090
2091 return 0;
2092}
2093
2094int Channel::StopPlayingFileLocally()
2095{
2096 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId,_channelId),
2097 "Channel::StopPlayingFileLocally()");
2098
henrika@webrtc.orgdf08c5d2014-03-18 10:32:33 +00002099 if (!channel_state_.Get().output_file_playing)
andrew@webrtc.orgb015cbe2012-10-22 18:19:23 +00002100 {
2101 _engineStatisticsPtr->SetLastError(
2102 VE_INVALID_OPERATION, kTraceWarning,
2103 "StopPlayingFileLocally() isnot playing");
2104 return 0;
2105 }
2106
2107 {
2108 CriticalSectionScoped cs(&_fileCritSect);
2109
2110 if (_outputFilePlayerPtr->StopPlayingFile() != 0)
2111 {
2112 _engineStatisticsPtr->SetLastError(
2113 VE_STOP_RECORDING_FAILED, kTraceError,
2114 "StopPlayingFile() could not stop playing");
2115 return -1;
2116 }
2117 _outputFilePlayerPtr->RegisterModuleFileCallback(NULL);
2118 FilePlayer::DestroyFilePlayer(_outputFilePlayerPtr);
2119 _outputFilePlayerPtr = NULL;
henrika@webrtc.orgdf08c5d2014-03-18 10:32:33 +00002120 channel_state_.SetOutputFilePlaying(false);
andrew@webrtc.orgb015cbe2012-10-22 18:19:23 +00002121 }
2122 // _fileCritSect cannot be taken while calling
2123 // SetAnonymousMixibilityStatus. Refer to comments in
2124 // StartPlayingFileLocally(const char* ...) for more details.
2125 if (_outputMixerPtr->SetAnonymousMixabilityStatus(*this, false) != 0)
2126 {
2127 _engineStatisticsPtr->SetLastError(
2128 VE_AUDIO_CONF_MIX_MODULE_ERROR, kTraceError,
2129 "StopPlayingFile() failed to stop participant from playing as"
2130 "file in the mixer");
2131 return -1;
2132 }
2133
2134 return 0;
2135}
2136
2137int Channel::IsPlayingFileLocally() const
2138{
2139 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId,_channelId),
2140 "Channel::IsPlayingFileLocally()");
2141
henrika@webrtc.orgdf08c5d2014-03-18 10:32:33 +00002142 return channel_state_.Get().output_file_playing;
andrew@webrtc.orgb015cbe2012-10-22 18:19:23 +00002143}
2144
2145int Channel::RegisterFilePlayingToMixer()
2146{
2147 // Return success for not registering for file playing to mixer if:
2148 // 1. playing file before playout is started on that channel.
2149 // 2. starting playout without file playing on that channel.
henrika@webrtc.orgdf08c5d2014-03-18 10:32:33 +00002150 if (!channel_state_.Get().playing ||
2151 !channel_state_.Get().output_file_playing)
andrew@webrtc.orgb015cbe2012-10-22 18:19:23 +00002152 {
2153 return 0;
2154 }
2155
2156 // |_fileCritSect| cannot be taken while calling
2157 // SetAnonymousMixabilityStatus() since as soon as the participant is added
2158 // frames can be pulled by the mixer. Since the frames are generated from
2159 // the file, _fileCritSect will be taken. This would result in a deadlock.
2160 if (_outputMixerPtr->SetAnonymousMixabilityStatus(*this, true) != 0)
2161 {
henrika@webrtc.orgdf08c5d2014-03-18 10:32:33 +00002162 channel_state_.SetOutputFilePlaying(false);
andrew@webrtc.orgb015cbe2012-10-22 18:19:23 +00002163 CriticalSectionScoped cs(&_fileCritSect);
andrew@webrtc.orgb015cbe2012-10-22 18:19:23 +00002164 _engineStatisticsPtr->SetLastError(
2165 VE_AUDIO_CONF_MIX_MODULE_ERROR, kTraceError,
2166 "StartPlayingFile() failed to add participant as file to mixer");
2167 _outputFilePlayerPtr->StopPlayingFile();
2168 FilePlayer::DestroyFilePlayer(_outputFilePlayerPtr);
2169 _outputFilePlayerPtr = NULL;
2170 return -1;
2171 }
2172
2173 return 0;
2174}
2175
andrew@webrtc.orgb015cbe2012-10-22 18:19:23 +00002176int Channel::StartPlayingFileAsMicrophone(const char* fileName,
pbos@webrtc.orgca7a9a22013-05-14 08:31:39 +00002177 bool loop,
2178 FileFormats format,
2179 int startPosition,
2180 float volumeScaling,
2181 int stopPosition,
andrew@webrtc.orgb015cbe2012-10-22 18:19:23 +00002182 const CodecInst* codecInst)
2183{
2184 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId,_channelId),
2185 "Channel::StartPlayingFileAsMicrophone(fileNameUTF8[]=%s, "
2186 "loop=%d, format=%d, volumeScaling=%5.3f, startPosition=%d, "
2187 "stopPosition=%d)", fileName, loop, format, volumeScaling,
2188 startPosition, stopPosition);
2189
henrika@webrtc.orgdf08c5d2014-03-18 10:32:33 +00002190 CriticalSectionScoped cs(&_fileCritSect);
2191
2192 if (channel_state_.Get().input_file_playing)
andrew@webrtc.orgb015cbe2012-10-22 18:19:23 +00002193 {
2194 _engineStatisticsPtr->SetLastError(
2195 VE_ALREADY_PLAYING, kTraceWarning,
2196 "StartPlayingFileAsMicrophone() filePlayer is playing");
2197 return 0;
2198 }
2199
andrew@webrtc.orgb015cbe2012-10-22 18:19:23 +00002200 // Destroy the old instance
2201 if (_inputFilePlayerPtr)
2202 {
2203 _inputFilePlayerPtr->RegisterModuleFileCallback(NULL);
2204 FilePlayer::DestroyFilePlayer(_inputFilePlayerPtr);
2205 _inputFilePlayerPtr = NULL;
2206 }
2207
2208 // Create the instance
2209 _inputFilePlayerPtr = FilePlayer::CreateFilePlayer(
2210 _inputFilePlayerId, (const FileFormats)format);
2211
2212 if (_inputFilePlayerPtr == NULL)
2213 {
2214 _engineStatisticsPtr->SetLastError(
2215 VE_INVALID_ARGUMENT, kTraceError,
2216 "StartPlayingFileAsMicrophone() filePlayer format isnot correct");
2217 return -1;
2218 }
2219
pbos@webrtc.org54f03bc2013-04-09 10:09:10 +00002220 const uint32_t notificationTime(0);
andrew@webrtc.orgb015cbe2012-10-22 18:19:23 +00002221
2222 if (_inputFilePlayerPtr->StartPlayingFile(
2223 fileName,
2224 loop,
2225 startPosition,
2226 volumeScaling,
2227 notificationTime,
2228 stopPosition,
2229 (const CodecInst*)codecInst) != 0)
2230 {
2231 _engineStatisticsPtr->SetLastError(
2232 VE_BAD_FILE, kTraceError,
2233 "StartPlayingFile() failed to start file playout");
2234 _inputFilePlayerPtr->StopPlayingFile();
2235 FilePlayer::DestroyFilePlayer(_inputFilePlayerPtr);
2236 _inputFilePlayerPtr = NULL;
2237 return -1;
2238 }
2239 _inputFilePlayerPtr->RegisterModuleFileCallback(this);
henrika@webrtc.orgdf08c5d2014-03-18 10:32:33 +00002240 channel_state_.SetInputFilePlaying(true);
andrew@webrtc.orgb015cbe2012-10-22 18:19:23 +00002241
2242 return 0;
2243}
2244
2245int Channel::StartPlayingFileAsMicrophone(InStream* stream,
pbos@webrtc.orgca7a9a22013-05-14 08:31:39 +00002246 FileFormats format,
2247 int startPosition,
2248 float volumeScaling,
2249 int stopPosition,
andrew@webrtc.orgb015cbe2012-10-22 18:19:23 +00002250 const CodecInst* codecInst)
2251{
2252 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId,_channelId),
2253 "Channel::StartPlayingFileAsMicrophone(format=%d, "
2254 "volumeScaling=%5.3f, startPosition=%d, stopPosition=%d)",
2255 format, volumeScaling, startPosition, stopPosition);
2256
2257 if(stream == NULL)
2258 {
2259 _engineStatisticsPtr->SetLastError(
2260 VE_BAD_FILE, kTraceError,
2261 "StartPlayingFileAsMicrophone NULL as input stream");
2262 return -1;
2263 }
2264
henrika@webrtc.orgdf08c5d2014-03-18 10:32:33 +00002265 CriticalSectionScoped cs(&_fileCritSect);
2266
2267 if (channel_state_.Get().input_file_playing)
andrew@webrtc.orgb015cbe2012-10-22 18:19:23 +00002268 {
2269 _engineStatisticsPtr->SetLastError(
2270 VE_ALREADY_PLAYING, kTraceWarning,
2271 "StartPlayingFileAsMicrophone() is playing");
2272 return 0;
2273 }
2274
andrew@webrtc.orgb015cbe2012-10-22 18:19:23 +00002275 // Destroy the old instance
2276 if (_inputFilePlayerPtr)
2277 {
2278 _inputFilePlayerPtr->RegisterModuleFileCallback(NULL);
2279 FilePlayer::DestroyFilePlayer(_inputFilePlayerPtr);
2280 _inputFilePlayerPtr = NULL;
2281 }
2282
2283 // Create the instance
2284 _inputFilePlayerPtr = FilePlayer::CreateFilePlayer(
2285 _inputFilePlayerId, (const FileFormats)format);
2286
2287 if (_inputFilePlayerPtr == NULL)
2288 {
2289 _engineStatisticsPtr->SetLastError(
2290 VE_INVALID_ARGUMENT, kTraceError,
2291 "StartPlayingInputFile() filePlayer format isnot correct");
2292 return -1;
2293 }
2294
pbos@webrtc.org54f03bc2013-04-09 10:09:10 +00002295 const uint32_t notificationTime(0);
andrew@webrtc.orgb015cbe2012-10-22 18:19:23 +00002296
2297 if (_inputFilePlayerPtr->StartPlayingFile(*stream, startPosition,
2298 volumeScaling, notificationTime,
2299 stopPosition, codecInst) != 0)
2300 {
2301 _engineStatisticsPtr->SetLastError(VE_BAD_FILE, kTraceError,
2302 "StartPlayingFile() failed to start "
2303 "file playout");
2304 _inputFilePlayerPtr->StopPlayingFile();
2305 FilePlayer::DestroyFilePlayer(_inputFilePlayerPtr);
2306 _inputFilePlayerPtr = NULL;
2307 return -1;
2308 }
andrew@webrtc.orgd4682362013-01-22 04:44:30 +00002309
andrew@webrtc.orgb015cbe2012-10-22 18:19:23 +00002310 _inputFilePlayerPtr->RegisterModuleFileCallback(this);
henrika@webrtc.orgdf08c5d2014-03-18 10:32:33 +00002311 channel_state_.SetInputFilePlaying(true);
andrew@webrtc.orgb015cbe2012-10-22 18:19:23 +00002312
2313 return 0;
2314}
2315
2316int Channel::StopPlayingFileAsMicrophone()
2317{
2318 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId,_channelId),
2319 "Channel::StopPlayingFileAsMicrophone()");
2320
henrika@webrtc.orgdf08c5d2014-03-18 10:32:33 +00002321 CriticalSectionScoped cs(&_fileCritSect);
2322
2323 if (!channel_state_.Get().input_file_playing)
andrew@webrtc.orgb015cbe2012-10-22 18:19:23 +00002324 {
2325 _engineStatisticsPtr->SetLastError(
2326 VE_INVALID_OPERATION, kTraceWarning,
2327 "StopPlayingFileAsMicrophone() isnot playing");
2328 return 0;
2329 }
2330
andrew@webrtc.orgb015cbe2012-10-22 18:19:23 +00002331 if (_inputFilePlayerPtr->StopPlayingFile() != 0)
2332 {
2333 _engineStatisticsPtr->SetLastError(
2334 VE_STOP_RECORDING_FAILED, kTraceError,
2335 "StopPlayingFile() could not stop playing");
2336 return -1;
2337 }
2338 _inputFilePlayerPtr->RegisterModuleFileCallback(NULL);
2339 FilePlayer::DestroyFilePlayer(_inputFilePlayerPtr);
2340 _inputFilePlayerPtr = NULL;
henrika@webrtc.orgdf08c5d2014-03-18 10:32:33 +00002341 channel_state_.SetInputFilePlaying(false);
andrew@webrtc.orgb015cbe2012-10-22 18:19:23 +00002342
2343 return 0;
2344}
2345
2346int Channel::IsPlayingFileAsMicrophone() const
2347{
2348 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId,_channelId),
2349 "Channel::IsPlayingFileAsMicrophone()");
henrika@webrtc.orgdf08c5d2014-03-18 10:32:33 +00002350 return channel_state_.Get().input_file_playing;
andrew@webrtc.orgb015cbe2012-10-22 18:19:23 +00002351}
2352
andrew@webrtc.orgb015cbe2012-10-22 18:19:23 +00002353int Channel::StartRecordingPlayout(const char* fileName,
2354 const CodecInst* codecInst)
2355{
2356 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId,_channelId),
2357 "Channel::StartRecordingPlayout(fileName=%s)", fileName);
2358
2359 if (_outputFileRecording)
2360 {
2361 WEBRTC_TRACE(kTraceWarning, kTraceVoice, VoEId(_instanceId,-1),
2362 "StartRecordingPlayout() is already recording");
2363 return 0;
2364 }
2365
2366 FileFormats format;
pbos@webrtc.org54f03bc2013-04-09 10:09:10 +00002367 const uint32_t notificationTime(0); // Not supported in VoE
andrew@webrtc.orgb015cbe2012-10-22 18:19:23 +00002368 CodecInst dummyCodec={100,"L16",16000,320,1,320000};
2369
2370 if ((codecInst != NULL) &&
2371 ((codecInst->channels < 1) || (codecInst->channels > 2)))
2372 {
2373 _engineStatisticsPtr->SetLastError(
2374 VE_BAD_ARGUMENT, kTraceError,
2375 "StartRecordingPlayout() invalid compression");
2376 return(-1);
2377 }
2378 if(codecInst == NULL)
2379 {
2380 format = kFileFormatPcm16kHzFile;
2381 codecInst=&dummyCodec;
2382 }
2383 else if((STR_CASE_CMP(codecInst->plname,"L16") == 0) ||
2384 (STR_CASE_CMP(codecInst->plname,"PCMU") == 0) ||
2385 (STR_CASE_CMP(codecInst->plname,"PCMA") == 0))
2386 {
2387 format = kFileFormatWavFile;
2388 }
2389 else
2390 {
2391 format = kFileFormatCompressedFile;
2392 }
2393
2394 CriticalSectionScoped cs(&_fileCritSect);
2395
2396 // Destroy the old instance
2397 if (_outputFileRecorderPtr)
2398 {
2399 _outputFileRecorderPtr->RegisterModuleFileCallback(NULL);
2400 FileRecorder::DestroyFileRecorder(_outputFileRecorderPtr);
2401 _outputFileRecorderPtr = NULL;
2402 }
2403
2404 _outputFileRecorderPtr = FileRecorder::CreateFileRecorder(
2405 _outputFileRecorderId, (const FileFormats)format);
2406 if (_outputFileRecorderPtr == NULL)
2407 {
2408 _engineStatisticsPtr->SetLastError(
2409 VE_INVALID_ARGUMENT, kTraceError,
2410 "StartRecordingPlayout() fileRecorder format isnot correct");
2411 return -1;
2412 }
2413
2414 if (_outputFileRecorderPtr->StartRecordingAudioFile(
2415 fileName, (const CodecInst&)*codecInst, notificationTime) != 0)
2416 {
2417 _engineStatisticsPtr->SetLastError(
2418 VE_BAD_FILE, kTraceError,
2419 "StartRecordingAudioFile() failed to start file recording");
2420 _outputFileRecorderPtr->StopRecording();
2421 FileRecorder::DestroyFileRecorder(_outputFileRecorderPtr);
2422 _outputFileRecorderPtr = NULL;
2423 return -1;
2424 }
2425 _outputFileRecorderPtr->RegisterModuleFileCallback(this);
2426 _outputFileRecording = true;
2427
2428 return 0;
2429}
2430
2431int Channel::StartRecordingPlayout(OutStream* stream,
2432 const CodecInst* codecInst)
2433{
2434 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId,_channelId),
2435 "Channel::StartRecordingPlayout()");
2436
2437 if (_outputFileRecording)
2438 {
2439 WEBRTC_TRACE(kTraceWarning, kTraceVoice, VoEId(_instanceId,-1),
2440 "StartRecordingPlayout() is already recording");
2441 return 0;
2442 }
2443
2444 FileFormats format;
pbos@webrtc.org54f03bc2013-04-09 10:09:10 +00002445 const uint32_t notificationTime(0); // Not supported in VoE
andrew@webrtc.orgb015cbe2012-10-22 18:19:23 +00002446 CodecInst dummyCodec={100,"L16",16000,320,1,320000};
2447
2448 if (codecInst != NULL && codecInst->channels != 1)
2449 {
2450 _engineStatisticsPtr->SetLastError(
2451 VE_BAD_ARGUMENT, kTraceError,
2452 "StartRecordingPlayout() invalid compression");
2453 return(-1);
2454 }
2455 if(codecInst == NULL)
2456 {
2457 format = kFileFormatPcm16kHzFile;
2458 codecInst=&dummyCodec;
2459 }
2460 else if((STR_CASE_CMP(codecInst->plname,"L16") == 0) ||
2461 (STR_CASE_CMP(codecInst->plname,"PCMU") == 0) ||
2462 (STR_CASE_CMP(codecInst->plname,"PCMA") == 0))
2463 {
2464 format = kFileFormatWavFile;
2465 }
2466 else
2467 {
2468 format = kFileFormatCompressedFile;
2469 }
2470
2471 CriticalSectionScoped cs(&_fileCritSect);
2472
2473 // Destroy the old instance
2474 if (_outputFileRecorderPtr)
2475 {
2476 _outputFileRecorderPtr->RegisterModuleFileCallback(NULL);
2477 FileRecorder::DestroyFileRecorder(_outputFileRecorderPtr);
2478 _outputFileRecorderPtr = NULL;
2479 }
2480
2481 _outputFileRecorderPtr = FileRecorder::CreateFileRecorder(
2482 _outputFileRecorderId, (const FileFormats)format);
2483 if (_outputFileRecorderPtr == NULL)
2484 {
2485 _engineStatisticsPtr->SetLastError(
2486 VE_INVALID_ARGUMENT, kTraceError,
2487 "StartRecordingPlayout() fileRecorder format isnot correct");
2488 return -1;
2489 }
2490
2491 if (_outputFileRecorderPtr->StartRecordingAudioFile(*stream, *codecInst,
2492 notificationTime) != 0)
2493 {
2494 _engineStatisticsPtr->SetLastError(VE_BAD_FILE, kTraceError,
2495 "StartRecordingPlayout() failed to "
2496 "start file recording");
2497 _outputFileRecorderPtr->StopRecording();
2498 FileRecorder::DestroyFileRecorder(_outputFileRecorderPtr);
2499 _outputFileRecorderPtr = NULL;
2500 return -1;
2501 }
andrew@webrtc.orgd4682362013-01-22 04:44:30 +00002502
andrew@webrtc.orgb015cbe2012-10-22 18:19:23 +00002503 _outputFileRecorderPtr->RegisterModuleFileCallback(this);
2504 _outputFileRecording = true;
2505
2506 return 0;
2507}
2508
2509int Channel::StopRecordingPlayout()
2510{
2511 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId,-1),
2512 "Channel::StopRecordingPlayout()");
2513
2514 if (!_outputFileRecording)
2515 {
2516 WEBRTC_TRACE(kTraceError, kTraceVoice, VoEId(_instanceId,-1),
2517 "StopRecordingPlayout() isnot recording");
2518 return -1;
2519 }
2520
2521
2522 CriticalSectionScoped cs(&_fileCritSect);
2523
2524 if (_outputFileRecorderPtr->StopRecording() != 0)
2525 {
2526 _engineStatisticsPtr->SetLastError(
2527 VE_STOP_RECORDING_FAILED, kTraceError,
2528 "StopRecording() could not stop recording");
2529 return(-1);
2530 }
2531 _outputFileRecorderPtr->RegisterModuleFileCallback(NULL);
2532 FileRecorder::DestroyFileRecorder(_outputFileRecorderPtr);
2533 _outputFileRecorderPtr = NULL;
2534 _outputFileRecording = false;
2535
2536 return 0;
2537}
2538
2539void
2540Channel::SetMixWithMicStatus(bool mix)
2541{
henrika@webrtc.orgdf08c5d2014-03-18 10:32:33 +00002542 CriticalSectionScoped cs(&_fileCritSect);
andrew@webrtc.orgb015cbe2012-10-22 18:19:23 +00002543 _mixFileWithMicrophone=mix;
2544}
2545
2546int
pbos@webrtc.org54f03bc2013-04-09 10:09:10 +00002547Channel::GetSpeechOutputLevel(uint32_t& level) const
andrew@webrtc.orgb015cbe2012-10-22 18:19:23 +00002548{
pbos@webrtc.org54f03bc2013-04-09 10:09:10 +00002549 int8_t currentLevel = _outputAudioLevel.Level();
2550 level = static_cast<int32_t> (currentLevel);
andrew@webrtc.orgb015cbe2012-10-22 18:19:23 +00002551 WEBRTC_TRACE(kTraceStateInfo, kTraceVoice,
2552 VoEId(_instanceId,_channelId),
2553 "GetSpeechOutputLevel() => level=%u", level);
2554 return 0;
2555}
2556
2557int
pbos@webrtc.org54f03bc2013-04-09 10:09:10 +00002558Channel::GetSpeechOutputLevelFullRange(uint32_t& level) const
andrew@webrtc.orgb015cbe2012-10-22 18:19:23 +00002559{
pbos@webrtc.org54f03bc2013-04-09 10:09:10 +00002560 int16_t currentLevel = _outputAudioLevel.LevelFullRange();
2561 level = static_cast<int32_t> (currentLevel);
andrew@webrtc.orgb015cbe2012-10-22 18:19:23 +00002562 WEBRTC_TRACE(kTraceStateInfo, kTraceVoice,
2563 VoEId(_instanceId,_channelId),
2564 "GetSpeechOutputLevelFullRange() => level=%u", level);
2565 return 0;
2566}
2567
2568int
2569Channel::SetMute(bool enable)
2570{
wu@webrtc.orgf7651ef2013-10-17 18:28:55 +00002571 CriticalSectionScoped cs(&volume_settings_critsect_);
andrew@webrtc.orgb015cbe2012-10-22 18:19:23 +00002572 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId,_channelId),
2573 "Channel::SetMute(enable=%d)", enable);
2574 _mute = enable;
2575 return 0;
2576}
2577
2578bool
2579Channel::Mute() const
2580{
wu@webrtc.orgf7651ef2013-10-17 18:28:55 +00002581 CriticalSectionScoped cs(&volume_settings_critsect_);
andrew@webrtc.orgb015cbe2012-10-22 18:19:23 +00002582 return _mute;
2583}
2584
2585int
2586Channel::SetOutputVolumePan(float left, float right)
2587{
wu@webrtc.orgf7651ef2013-10-17 18:28:55 +00002588 CriticalSectionScoped cs(&volume_settings_critsect_);
andrew@webrtc.orgb015cbe2012-10-22 18:19:23 +00002589 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId,_channelId),
2590 "Channel::SetOutputVolumePan()");
2591 _panLeft = left;
2592 _panRight = right;
2593 return 0;
2594}
2595
2596int
2597Channel::GetOutputVolumePan(float& left, float& right) const
2598{
wu@webrtc.orgf7651ef2013-10-17 18:28:55 +00002599 CriticalSectionScoped cs(&volume_settings_critsect_);
andrew@webrtc.orgb015cbe2012-10-22 18:19:23 +00002600 left = _panLeft;
2601 right = _panRight;
2602 WEBRTC_TRACE(kTraceStateInfo, kTraceVoice,
2603 VoEId(_instanceId,_channelId),
2604 "GetOutputVolumePan() => left=%3.2f, right=%3.2f", left, right);
2605 return 0;
2606}
2607
2608int
2609Channel::SetChannelOutputVolumeScaling(float scaling)
2610{
wu@webrtc.orgf7651ef2013-10-17 18:28:55 +00002611 CriticalSectionScoped cs(&volume_settings_critsect_);
andrew@webrtc.orgb015cbe2012-10-22 18:19:23 +00002612 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId,_channelId),
2613 "Channel::SetChannelOutputVolumeScaling()");
2614 _outputGain = scaling;
2615 return 0;
2616}
2617
2618int
2619Channel::GetChannelOutputVolumeScaling(float& scaling) const
2620{
wu@webrtc.orgf7651ef2013-10-17 18:28:55 +00002621 CriticalSectionScoped cs(&volume_settings_critsect_);
andrew@webrtc.orgb015cbe2012-10-22 18:19:23 +00002622 scaling = _outputGain;
2623 WEBRTC_TRACE(kTraceStateInfo, kTraceVoice,
2624 VoEId(_instanceId,_channelId),
2625 "GetChannelOutputVolumeScaling() => scaling=%3.2f", scaling);
2626 return 0;
2627}
2628
andrew@webrtc.orgb015cbe2012-10-22 18:19:23 +00002629int Channel::SendTelephoneEventOutband(unsigned char eventCode,
wu@webrtc.org7fc75bb2013-08-15 23:38:54 +00002630 int lengthMs, int attenuationDb,
2631 bool playDtmfEvent)
andrew@webrtc.orgb015cbe2012-10-22 18:19:23 +00002632{
2633 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId, _channelId),
2634 "Channel::SendTelephoneEventOutband(..., playDtmfEvent=%d)",
2635 playDtmfEvent);
2636
2637 _playOutbandDtmfEvent = playDtmfEvent;
2638
2639 if (_rtpRtcpModule->SendTelephoneEventOutband(eventCode, lengthMs,
2640 attenuationDb) != 0)
2641 {
2642 _engineStatisticsPtr->SetLastError(
2643 VE_SEND_DTMF_FAILED,
2644 kTraceWarning,
2645 "SendTelephoneEventOutband() failed to send event");
2646 return -1;
2647 }
2648 return 0;
2649}
2650
2651int Channel::SendTelephoneEventInband(unsigned char eventCode,
2652 int lengthMs,
2653 int attenuationDb,
2654 bool playDtmfEvent)
2655{
2656 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId, _channelId),
2657 "Channel::SendTelephoneEventInband(..., playDtmfEvent=%d)",
2658 playDtmfEvent);
2659
2660 _playInbandDtmfEvent = playDtmfEvent;
2661 _inbandDtmfQueue.AddDtmf(eventCode, lengthMs, attenuationDb);
2662
2663 return 0;
2664}
2665
2666int
2667Channel::SetDtmfPlayoutStatus(bool enable)
2668{
2669 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId,_channelId),
2670 "Channel::SetDtmfPlayoutStatus()");
andrew@webrtc.org510ee1b2013-09-23 23:02:24 +00002671 if (audio_coding_->SetDtmfPlayoutStatus(enable) != 0)
andrew@webrtc.orgb015cbe2012-10-22 18:19:23 +00002672 {
2673 _engineStatisticsPtr->SetLastError(
2674 VE_AUDIO_CODING_MODULE_ERROR, kTraceWarning,
2675 "SetDtmfPlayoutStatus() failed to set Dtmf playout");
2676 return -1;
2677 }
2678 return 0;
2679}
2680
2681bool
2682Channel::DtmfPlayoutStatus() const
2683{
andrew@webrtc.org510ee1b2013-09-23 23:02:24 +00002684 return audio_coding_->DtmfPlayoutStatus();
andrew@webrtc.orgb015cbe2012-10-22 18:19:23 +00002685}
2686
2687int
2688Channel::SetSendTelephoneEventPayloadType(unsigned char type)
2689{
2690 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId,_channelId),
2691 "Channel::SetSendTelephoneEventPayloadType()");
2692 if (type > 127)
2693 {
2694 _engineStatisticsPtr->SetLastError(
2695 VE_INVALID_ARGUMENT, kTraceError,
2696 "SetSendTelephoneEventPayloadType() invalid type");
2697 return -1;
2698 }
pbos@webrtc.org6a4acb92013-07-11 15:50:07 +00002699 CodecInst codec = {};
andrew@webrtc.orgb015cbe2012-10-22 18:19:23 +00002700 codec.plfreq = 8000;
2701 codec.pltype = type;
2702 memcpy(codec.plname, "telephone-event", 16);
2703 if (_rtpRtcpModule->RegisterSendPayload(codec) != 0)
2704 {
henrika@webrtc.org570c4a52013-04-17 07:34:25 +00002705 _rtpRtcpModule->DeRegisterSendPayload(codec.pltype);
2706 if (_rtpRtcpModule->RegisterSendPayload(codec) != 0) {
2707 _engineStatisticsPtr->SetLastError(
2708 VE_RTP_RTCP_MODULE_ERROR, kTraceError,
2709 "SetSendTelephoneEventPayloadType() failed to register send"
2710 "payload type");
2711 return -1;
2712 }
andrew@webrtc.orgb015cbe2012-10-22 18:19:23 +00002713 }
2714 _sendTelephoneEventPayloadType = type;
2715 return 0;
2716}
2717
2718int
2719Channel::GetSendTelephoneEventPayloadType(unsigned char& type)
2720{
2721 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId,_channelId),
2722 "Channel::GetSendTelephoneEventPayloadType()");
2723 type = _sendTelephoneEventPayloadType;
2724 WEBRTC_TRACE(kTraceStateInfo, kTraceVoice,
2725 VoEId(_instanceId,_channelId),
2726 "GetSendTelephoneEventPayloadType() => type=%u", type);
2727 return 0;
2728}
2729
andrew@webrtc.orgb015cbe2012-10-22 18:19:23 +00002730int
2731Channel::UpdateRxVadDetection(AudioFrame& audioFrame)
2732{
2733 WEBRTC_TRACE(kTraceStream, kTraceVoice, VoEId(_instanceId,_channelId),
2734 "Channel::UpdateRxVadDetection()");
2735
2736 int vadDecision = 1;
2737
2738 vadDecision = (audioFrame.vad_activity_ == AudioFrame::kVadActive)? 1 : 0;
2739
2740 if ((vadDecision != _oldVadDecision) && _rxVadObserverPtr)
2741 {
2742 OnRxVadDetected(vadDecision);
2743 _oldVadDecision = vadDecision;
2744 }
2745
2746 WEBRTC_TRACE(kTraceStream, kTraceVoice, VoEId(_instanceId,_channelId),
2747 "Channel::UpdateRxVadDetection() => vadDecision=%d",
2748 vadDecision);
2749 return 0;
2750}
2751
2752int
2753Channel::RegisterRxVadObserver(VoERxVadCallback &observer)
2754{
2755 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId,_channelId),
2756 "Channel::RegisterRxVadObserver()");
2757 CriticalSectionScoped cs(&_callbackCritSect);
2758
2759 if (_rxVadObserverPtr)
2760 {
2761 _engineStatisticsPtr->SetLastError(
2762 VE_INVALID_OPERATION, kTraceError,
2763 "RegisterRxVadObserver() observer already enabled");
2764 return -1;
2765 }
2766 _rxVadObserverPtr = &observer;
2767 _RxVadDetection = true;
2768 return 0;
2769}
2770
2771int
2772Channel::DeRegisterRxVadObserver()
2773{
2774 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId,_channelId),
2775 "Channel::DeRegisterRxVadObserver()");
2776 CriticalSectionScoped cs(&_callbackCritSect);
2777
2778 if (!_rxVadObserverPtr)
2779 {
2780 _engineStatisticsPtr->SetLastError(
2781 VE_INVALID_OPERATION, kTraceWarning,
2782 "DeRegisterRxVadObserver() observer already disabled");
2783 return 0;
2784 }
2785 _rxVadObserverPtr = NULL;
2786 _RxVadDetection = false;
2787 return 0;
2788}
2789
2790int
2791Channel::VoiceActivityIndicator(int &activity)
2792{
2793 activity = _sendFrameType;
2794
2795 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId,_channelId),
andrew@webrtc.orge06943f2013-10-04 17:54:09 +00002796 "Channel::VoiceActivityIndicator(indicator=%d)", activity);
andrew@webrtc.orgb015cbe2012-10-22 18:19:23 +00002797 return 0;
2798}
2799
2800#ifdef WEBRTC_VOICE_ENGINE_AGC
2801
2802int
pbos@webrtc.orgca7a9a22013-05-14 08:31:39 +00002803Channel::SetRxAgcStatus(bool enable, AgcModes mode)
andrew@webrtc.orgb015cbe2012-10-22 18:19:23 +00002804{
2805 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId,_channelId),
2806 "Channel::SetRxAgcStatus(enable=%d, mode=%d)",
2807 (int)enable, (int)mode);
2808
andrew@webrtc.orge06943f2013-10-04 17:54:09 +00002809 GainControl::Mode agcMode = kDefaultRxAgcMode;
andrew@webrtc.orgb015cbe2012-10-22 18:19:23 +00002810 switch (mode)
2811 {
2812 case kAgcDefault:
andrew@webrtc.orgb015cbe2012-10-22 18:19:23 +00002813 break;
2814 case kAgcUnchanged:
andrew@webrtc.org80142aa2013-09-18 22:37:32 +00002815 agcMode = rx_audioproc_->gain_control()->mode();
andrew@webrtc.orgb015cbe2012-10-22 18:19:23 +00002816 break;
2817 case kAgcFixedDigital:
2818 agcMode = GainControl::kFixedDigital;
2819 break;
2820 case kAgcAdaptiveDigital:
2821 agcMode =GainControl::kAdaptiveDigital;
2822 break;
2823 default:
2824 _engineStatisticsPtr->SetLastError(
2825 VE_INVALID_ARGUMENT, kTraceError,
2826 "SetRxAgcStatus() invalid Agc mode");
2827 return -1;
2828 }
2829
andrew@webrtc.org80142aa2013-09-18 22:37:32 +00002830 if (rx_audioproc_->gain_control()->set_mode(agcMode) != 0)
andrew@webrtc.orgb015cbe2012-10-22 18:19:23 +00002831 {
2832 _engineStatisticsPtr->SetLastError(
2833 VE_APM_ERROR, kTraceError,
2834 "SetRxAgcStatus() failed to set Agc mode");
2835 return -1;
2836 }
andrew@webrtc.org80142aa2013-09-18 22:37:32 +00002837 if (rx_audioproc_->gain_control()->Enable(enable) != 0)
andrew@webrtc.orgb015cbe2012-10-22 18:19:23 +00002838 {
2839 _engineStatisticsPtr->SetLastError(
2840 VE_APM_ERROR, kTraceError,
2841 "SetRxAgcStatus() failed to set Agc state");
2842 return -1;
2843 }
2844
2845 _rxAgcIsEnabled = enable;
henrika@webrtc.orgdf08c5d2014-03-18 10:32:33 +00002846 channel_state_.SetRxApmIsEnabled(_rxAgcIsEnabled || _rxNsIsEnabled);
andrew@webrtc.orgb015cbe2012-10-22 18:19:23 +00002847
2848 return 0;
2849}
2850
2851int
2852Channel::GetRxAgcStatus(bool& enabled, AgcModes& mode)
2853{
2854 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId,_channelId),
2855 "Channel::GetRxAgcStatus(enable=?, mode=?)");
2856
andrew@webrtc.org80142aa2013-09-18 22:37:32 +00002857 bool enable = rx_audioproc_->gain_control()->is_enabled();
andrew@webrtc.orgb015cbe2012-10-22 18:19:23 +00002858 GainControl::Mode agcMode =
andrew@webrtc.org80142aa2013-09-18 22:37:32 +00002859 rx_audioproc_->gain_control()->mode();
andrew@webrtc.orgb015cbe2012-10-22 18:19:23 +00002860
2861 enabled = enable;
2862
2863 switch (agcMode)
2864 {
2865 case GainControl::kFixedDigital:
2866 mode = kAgcFixedDigital;
2867 break;
2868 case GainControl::kAdaptiveDigital:
2869 mode = kAgcAdaptiveDigital;
2870 break;
2871 default:
2872 _engineStatisticsPtr->SetLastError(
2873 VE_APM_ERROR, kTraceError,
2874 "GetRxAgcStatus() invalid Agc mode");
2875 return -1;
2876 }
2877
2878 return 0;
2879}
2880
2881int
pbos@webrtc.orgca7a9a22013-05-14 08:31:39 +00002882Channel::SetRxAgcConfig(AgcConfig config)
andrew@webrtc.orgb015cbe2012-10-22 18:19:23 +00002883{
2884 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId,_channelId),
2885 "Channel::SetRxAgcConfig()");
2886
andrew@webrtc.org80142aa2013-09-18 22:37:32 +00002887 if (rx_audioproc_->gain_control()->set_target_level_dbfs(
andrew@webrtc.orgb015cbe2012-10-22 18:19:23 +00002888 config.targetLeveldBOv) != 0)
2889 {
2890 _engineStatisticsPtr->SetLastError(
2891 VE_APM_ERROR, kTraceError,
2892 "SetRxAgcConfig() failed to set target peak |level|"
2893 "(or envelope) of the Agc");
2894 return -1;
2895 }
andrew@webrtc.org80142aa2013-09-18 22:37:32 +00002896 if (rx_audioproc_->gain_control()->set_compression_gain_db(
andrew@webrtc.orgb015cbe2012-10-22 18:19:23 +00002897 config.digitalCompressionGaindB) != 0)
2898 {
2899 _engineStatisticsPtr->SetLastError(
2900 VE_APM_ERROR, kTraceError,
2901 "SetRxAgcConfig() failed to set the range in |gain| the"
2902 " digital compression stage may apply");
2903 return -1;
2904 }
andrew@webrtc.org80142aa2013-09-18 22:37:32 +00002905 if (rx_audioproc_->gain_control()->enable_limiter(
andrew@webrtc.orgb015cbe2012-10-22 18:19:23 +00002906 config.limiterEnable) != 0)
2907 {
2908 _engineStatisticsPtr->SetLastError(
2909 VE_APM_ERROR, kTraceError,
2910 "SetRxAgcConfig() failed to set hard limiter to the signal");
2911 return -1;
2912 }
2913
2914 return 0;
2915}
2916
2917int
2918Channel::GetRxAgcConfig(AgcConfig& config)
2919{
2920 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId,_channelId),
2921 "Channel::GetRxAgcConfig(config=%?)");
2922
2923 config.targetLeveldBOv =
andrew@webrtc.org80142aa2013-09-18 22:37:32 +00002924 rx_audioproc_->gain_control()->target_level_dbfs();
andrew@webrtc.orgb015cbe2012-10-22 18:19:23 +00002925 config.digitalCompressionGaindB =
andrew@webrtc.org80142aa2013-09-18 22:37:32 +00002926 rx_audioproc_->gain_control()->compression_gain_db();
andrew@webrtc.orgb015cbe2012-10-22 18:19:23 +00002927 config.limiterEnable =
andrew@webrtc.org80142aa2013-09-18 22:37:32 +00002928 rx_audioproc_->gain_control()->is_limiter_enabled();
andrew@webrtc.orgb015cbe2012-10-22 18:19:23 +00002929
2930 WEBRTC_TRACE(kTraceStateInfo, kTraceVoice,
2931 VoEId(_instanceId,_channelId), "GetRxAgcConfig() => "
2932 "targetLeveldBOv=%u, digitalCompressionGaindB=%u,"
2933 " limiterEnable=%d",
2934 config.targetLeveldBOv,
2935 config.digitalCompressionGaindB,
2936 config.limiterEnable);
2937
2938 return 0;
2939}
2940
2941#endif // #ifdef WEBRTC_VOICE_ENGINE_AGC
2942
2943#ifdef WEBRTC_VOICE_ENGINE_NR
2944
2945int
pbos@webrtc.orgca7a9a22013-05-14 08:31:39 +00002946Channel::SetRxNsStatus(bool enable, NsModes mode)
andrew@webrtc.orgb015cbe2012-10-22 18:19:23 +00002947{
2948 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId,_channelId),
2949 "Channel::SetRxNsStatus(enable=%d, mode=%d)",
2950 (int)enable, (int)mode);
2951
andrew@webrtc.orge06943f2013-10-04 17:54:09 +00002952 NoiseSuppression::Level nsLevel = kDefaultNsMode;
andrew@webrtc.orgb015cbe2012-10-22 18:19:23 +00002953 switch (mode)
2954 {
2955
2956 case kNsDefault:
andrew@webrtc.orgb015cbe2012-10-22 18:19:23 +00002957 break;
2958 case kNsUnchanged:
andrew@webrtc.org80142aa2013-09-18 22:37:32 +00002959 nsLevel = rx_audioproc_->noise_suppression()->level();
andrew@webrtc.orgb015cbe2012-10-22 18:19:23 +00002960 break;
2961 case kNsConference:
2962 nsLevel = NoiseSuppression::kHigh;
2963 break;
2964 case kNsLowSuppression:
2965 nsLevel = NoiseSuppression::kLow;
2966 break;
2967 case kNsModerateSuppression:
2968 nsLevel = NoiseSuppression::kModerate;
2969 break;
2970 case kNsHighSuppression:
2971 nsLevel = NoiseSuppression::kHigh;
2972 break;
2973 case kNsVeryHighSuppression:
2974 nsLevel = NoiseSuppression::kVeryHigh;
2975 break;
2976 }
2977
andrew@webrtc.org80142aa2013-09-18 22:37:32 +00002978 if (rx_audioproc_->noise_suppression()->set_level(nsLevel)
andrew@webrtc.orgb015cbe2012-10-22 18:19:23 +00002979 != 0)
2980 {
2981 _engineStatisticsPtr->SetLastError(
2982 VE_APM_ERROR, kTraceError,
andrew@webrtc.orge06943f2013-10-04 17:54:09 +00002983 "SetRxNsStatus() failed to set NS level");
andrew@webrtc.orgb015cbe2012-10-22 18:19:23 +00002984 return -1;
2985 }
andrew@webrtc.org80142aa2013-09-18 22:37:32 +00002986 if (rx_audioproc_->noise_suppression()->Enable(enable) != 0)
andrew@webrtc.orgb015cbe2012-10-22 18:19:23 +00002987 {
2988 _engineStatisticsPtr->SetLastError(
2989 VE_APM_ERROR, kTraceError,
andrew@webrtc.orge06943f2013-10-04 17:54:09 +00002990 "SetRxNsStatus() failed to set NS state");
andrew@webrtc.orgb015cbe2012-10-22 18:19:23 +00002991 return -1;
2992 }
2993
2994 _rxNsIsEnabled = enable;
henrika@webrtc.orgdf08c5d2014-03-18 10:32:33 +00002995 channel_state_.SetRxApmIsEnabled(_rxAgcIsEnabled || _rxNsIsEnabled);
andrew@webrtc.orgb015cbe2012-10-22 18:19:23 +00002996
2997 return 0;
2998}
2999
3000int
3001Channel::GetRxNsStatus(bool& enabled, NsModes& mode)
3002{
3003 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId,_channelId),
3004 "Channel::GetRxNsStatus(enable=?, mode=?)");
3005
3006 bool enable =
andrew@webrtc.org80142aa2013-09-18 22:37:32 +00003007 rx_audioproc_->noise_suppression()->is_enabled();
andrew@webrtc.orgb015cbe2012-10-22 18:19:23 +00003008 NoiseSuppression::Level ncLevel =
andrew@webrtc.org80142aa2013-09-18 22:37:32 +00003009 rx_audioproc_->noise_suppression()->level();
andrew@webrtc.orgb015cbe2012-10-22 18:19:23 +00003010
3011 enabled = enable;
3012
3013 switch (ncLevel)
3014 {
3015 case NoiseSuppression::kLow:
3016 mode = kNsLowSuppression;
3017 break;
3018 case NoiseSuppression::kModerate:
3019 mode = kNsModerateSuppression;
3020 break;
3021 case NoiseSuppression::kHigh:
3022 mode = kNsHighSuppression;
3023 break;
3024 case NoiseSuppression::kVeryHigh:
3025 mode = kNsVeryHighSuppression;
3026 break;
3027 }
3028
3029 WEBRTC_TRACE(kTraceStateInfo, kTraceVoice,
3030 VoEId(_instanceId,_channelId),
3031 "GetRxNsStatus() => enabled=%d, mode=%d", enabled, mode);
3032 return 0;
3033}
3034
3035#endif // #ifdef WEBRTC_VOICE_ENGINE_NR
3036
3037int
andrew@webrtc.orgb015cbe2012-10-22 18:19:23 +00003038Channel::RegisterRTCPObserver(VoERTCPObserver& observer)
3039{
3040 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId,_channelId),
3041 "Channel::RegisterRTCPObserver()");
3042 CriticalSectionScoped cs(&_callbackCritSect);
3043
3044 if (_rtcpObserverPtr)
3045 {
3046 _engineStatisticsPtr->SetLastError(
3047 VE_INVALID_OPERATION, kTraceError,
3048 "RegisterRTCPObserver() observer already enabled");
3049 return -1;
3050 }
3051
3052 _rtcpObserverPtr = &observer;
3053 _rtcpObserver = true;
3054
3055 return 0;
3056}
3057
3058int
3059Channel::DeRegisterRTCPObserver()
3060{
3061 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId, _channelId),
3062 "Channel::DeRegisterRTCPObserver()");
3063 CriticalSectionScoped cs(&_callbackCritSect);
3064
3065 if (!_rtcpObserverPtr)
3066 {
3067 _engineStatisticsPtr->SetLastError(
3068 VE_INVALID_OPERATION, kTraceWarning,
3069 "DeRegisterRTCPObserver() observer already disabled");
3070 return 0;
3071 }
3072
3073 _rtcpObserver = false;
3074 _rtcpObserverPtr = NULL;
3075
3076 return 0;
3077}
3078
3079int
3080Channel::SetLocalSSRC(unsigned int ssrc)
3081{
3082 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId, _channelId),
3083 "Channel::SetLocalSSRC()");
henrika@webrtc.orgdf08c5d2014-03-18 10:32:33 +00003084 if (channel_state_.Get().sending)
andrew@webrtc.orgb015cbe2012-10-22 18:19:23 +00003085 {
3086 _engineStatisticsPtr->SetLastError(
3087 VE_ALREADY_SENDING, kTraceError,
3088 "SetLocalSSRC() already sending");
3089 return -1;
3090 }
stefan@webrtc.org903e7462014-06-05 08:25:29 +00003091 _rtpRtcpModule->SetSSRC(ssrc);
andrew@webrtc.orgb015cbe2012-10-22 18:19:23 +00003092 return 0;
3093}
3094
3095int
3096Channel::GetLocalSSRC(unsigned int& ssrc)
3097{
3098 ssrc = _rtpRtcpModule->SSRC();
3099 WEBRTC_TRACE(kTraceStateInfo, kTraceVoice,
3100 VoEId(_instanceId,_channelId),
3101 "GetLocalSSRC() => ssrc=%lu", ssrc);
3102 return 0;
3103}
3104
3105int
3106Channel::GetRemoteSSRC(unsigned int& ssrc)
3107{
wu@webrtc.org7fc75bb2013-08-15 23:38:54 +00003108 ssrc = rtp_receiver_->SSRC();
andrew@webrtc.orgb015cbe2012-10-22 18:19:23 +00003109 WEBRTC_TRACE(kTraceStateInfo, kTraceVoice,
3110 VoEId(_instanceId,_channelId),
3111 "GetRemoteSSRC() => ssrc=%lu", ssrc);
3112 return 0;
3113}
3114
wu@webrtc.org9a823222014-03-06 23:49:08 +00003115int Channel::SetSendAudioLevelIndicationStatus(bool enable, unsigned char id) {
andrew@webrtc.org80142aa2013-09-18 22:37:32 +00003116 _includeAudioLevelIndication = enable;
wu@webrtc.org9a823222014-03-06 23:49:08 +00003117 return SetSendRtpHeaderExtension(enable, kRtpExtensionAudioLevel, id);
andrew@webrtc.orgb015cbe2012-10-22 18:19:23 +00003118}
andrew@webrtc.org80142aa2013-09-18 22:37:32 +00003119
wu@webrtc.org47e54ba2014-04-24 20:33:08 +00003120int Channel::SetReceiveAudioLevelIndicationStatus(bool enable,
3121 unsigned char id) {
3122 rtp_header_parser_->DeregisterRtpHeaderExtension(
3123 kRtpExtensionAudioLevel);
3124 if (enable && !rtp_header_parser_->RegisterRtpHeaderExtension(
3125 kRtpExtensionAudioLevel, id)) {
3126 return -1;
3127 }
3128 return 0;
3129}
3130
wu@webrtc.org9a823222014-03-06 23:49:08 +00003131int Channel::SetSendAbsoluteSenderTimeStatus(bool enable, unsigned char id) {
3132 return SetSendRtpHeaderExtension(enable, kRtpExtensionAbsoluteSendTime, id);
3133}
3134
3135int Channel::SetReceiveAbsoluteSenderTimeStatus(bool enable, unsigned char id) {
3136 rtp_header_parser_->DeregisterRtpHeaderExtension(
3137 kRtpExtensionAbsoluteSendTime);
solenberg@webrtc.orgfec6b6e2014-03-24 10:38:25 +00003138 if (enable && !rtp_header_parser_->RegisterRtpHeaderExtension(
3139 kRtpExtensionAbsoluteSendTime, id)) {
3140 return -1;
wu@webrtc.org9a823222014-03-06 23:49:08 +00003141 }
3142 return 0;
andrew@webrtc.orgb015cbe2012-10-22 18:19:23 +00003143}
3144
3145int
3146Channel::SetRTCPStatus(bool enable)
3147{
3148 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId,_channelId),
3149 "Channel::SetRTCPStatus()");
3150 if (_rtpRtcpModule->SetRTCPStatus(enable ?
3151 kRtcpCompound : kRtcpOff) != 0)
3152 {
3153 _engineStatisticsPtr->SetLastError(
3154 VE_RTP_RTCP_MODULE_ERROR, kTraceError,
3155 "SetRTCPStatus() failed to set RTCP status");
3156 return -1;
3157 }
3158 return 0;
3159}
3160
3161int
3162Channel::GetRTCPStatus(bool& enabled)
3163{
3164 RTCPMethod method = _rtpRtcpModule->RTCP();
3165 enabled = (method != kRtcpOff);
3166 WEBRTC_TRACE(kTraceStateInfo, kTraceVoice,
3167 VoEId(_instanceId,_channelId),
3168 "GetRTCPStatus() => enabled=%d", enabled);
3169 return 0;
3170}
3171
3172int
3173Channel::SetRTCP_CNAME(const char cName[256])
3174{
3175 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId, _channelId),
3176 "Channel::SetRTCP_CNAME()");
3177 if (_rtpRtcpModule->SetCNAME(cName) != 0)
3178 {
3179 _engineStatisticsPtr->SetLastError(
3180 VE_RTP_RTCP_MODULE_ERROR, kTraceError,
3181 "SetRTCP_CNAME() failed to set RTCP CNAME");
3182 return -1;
3183 }
3184 return 0;
3185}
3186
3187int
andrew@webrtc.orgb015cbe2012-10-22 18:19:23 +00003188Channel::GetRemoteRTCP_CNAME(char cName[256])
3189{
3190 if (cName == NULL)
3191 {
3192 _engineStatisticsPtr->SetLastError(
3193 VE_INVALID_ARGUMENT, kTraceError,
3194 "GetRemoteRTCP_CNAME() invalid CNAME input buffer");
3195 return -1;
3196 }
3197 char cname[RTCP_CNAME_SIZE];
wu@webrtc.org7fc75bb2013-08-15 23:38:54 +00003198 const uint32_t remoteSSRC = rtp_receiver_->SSRC();
andrew@webrtc.orgb015cbe2012-10-22 18:19:23 +00003199 if (_rtpRtcpModule->RemoteCNAME(remoteSSRC, cname) != 0)
3200 {
3201 _engineStatisticsPtr->SetLastError(
3202 VE_CANNOT_RETRIEVE_CNAME, kTraceError,
3203 "GetRemoteRTCP_CNAME() failed to retrieve remote RTCP CNAME");
3204 return -1;
3205 }
3206 strcpy(cName, cname);
3207 WEBRTC_TRACE(kTraceStateInfo, kTraceVoice,
3208 VoEId(_instanceId, _channelId),
3209 "GetRemoteRTCP_CNAME() => cName=%s", cName);
3210 return 0;
3211}
3212
3213int
3214Channel::GetRemoteRTCPData(
3215 unsigned int& NTPHigh,
3216 unsigned int& NTPLow,
3217 unsigned int& timestamp,
3218 unsigned int& playoutTimestamp,
3219 unsigned int* jitter,
3220 unsigned short* fractionLost)
3221{
3222 // --- Information from sender info in received Sender Reports
3223
3224 RTCPSenderInfo senderInfo;
3225 if (_rtpRtcpModule->RemoteRTCPStat(&senderInfo) != 0)
3226 {
3227 _engineStatisticsPtr->SetLastError(
3228 VE_RTP_RTCP_MODULE_ERROR, kTraceError,
3229 "GetRemoteRTCPData() failed to retrieve sender info for remote "
3230 "side");
3231 return -1;
3232 }
3233
3234 // We only utilize 12 out of 20 bytes in the sender info (ignores packet
3235 // and octet count)
3236 NTPHigh = senderInfo.NTPseconds;
3237 NTPLow = senderInfo.NTPfraction;
3238 timestamp = senderInfo.RTPtimeStamp;
3239
3240 WEBRTC_TRACE(kTraceStateInfo, kTraceVoice,
3241 VoEId(_instanceId, _channelId),
3242 "GetRemoteRTCPData() => NTPHigh=%lu, NTPLow=%lu, "
3243 "timestamp=%lu",
3244 NTPHigh, NTPLow, timestamp);
3245
3246 // --- Locally derived information
3247
3248 // This value is updated on each incoming RTCP packet (0 when no packet
3249 // has been received)
pwestin@webrtc.orgf2724972013-04-11 20:23:35 +00003250 playoutTimestamp = playout_timestamp_rtcp_;
andrew@webrtc.orgb015cbe2012-10-22 18:19:23 +00003251
3252 WEBRTC_TRACE(kTraceStateInfo, kTraceVoice,
3253 VoEId(_instanceId, _channelId),
3254 "GetRemoteRTCPData() => playoutTimestamp=%lu",
pwestin@webrtc.orgf2724972013-04-11 20:23:35 +00003255 playout_timestamp_rtcp_);
andrew@webrtc.orgb015cbe2012-10-22 18:19:23 +00003256
3257 if (NULL != jitter || NULL != fractionLost)
3258 {
3259 // Get all RTCP receiver report blocks that have been received on this
3260 // channel. If we receive RTP packets from a remote source we know the
3261 // remote SSRC and use the report block from him.
3262 // Otherwise use the first report block.
3263 std::vector<RTCPReportBlock> remote_stats;
3264 if (_rtpRtcpModule->RemoteRTCPStat(&remote_stats) != 0 ||
3265 remote_stats.empty()) {
3266 WEBRTC_TRACE(kTraceWarning, kTraceVoice,
3267 VoEId(_instanceId, _channelId),
3268 "GetRemoteRTCPData() failed to measure statistics due"
3269 " to lack of received RTP and/or RTCP packets");
3270 return -1;
3271 }
3272
wu@webrtc.org7fc75bb2013-08-15 23:38:54 +00003273 uint32_t remoteSSRC = rtp_receiver_->SSRC();
andrew@webrtc.orgb015cbe2012-10-22 18:19:23 +00003274 std::vector<RTCPReportBlock>::const_iterator it = remote_stats.begin();
3275 for (; it != remote_stats.end(); ++it) {
3276 if (it->remoteSSRC == remoteSSRC)
3277 break;
3278 }
3279
3280 if (it == remote_stats.end()) {
3281 // If we have not received any RTCP packets from this SSRC it probably
3282 // means that we have not received any RTP packets.
3283 // Use the first received report block instead.
3284 it = remote_stats.begin();
3285 remoteSSRC = it->remoteSSRC;
3286 }
3287
3288 if (jitter) {
3289 *jitter = it->jitter;
3290 WEBRTC_TRACE(kTraceStateInfo, kTraceVoice,
3291 VoEId(_instanceId, _channelId),
3292 "GetRemoteRTCPData() => jitter = %lu", *jitter);
3293 }
3294
3295 if (fractionLost) {
3296 *fractionLost = it->fractionLost;
3297 WEBRTC_TRACE(kTraceStateInfo, kTraceVoice,
3298 VoEId(_instanceId, _channelId),
3299 "GetRemoteRTCPData() => fractionLost = %lu",
3300 *fractionLost);
3301 }
3302 }
3303 return 0;
3304}
3305
3306int
pbos@webrtc.orgca7a9a22013-05-14 08:31:39 +00003307Channel::SendApplicationDefinedRTCPPacket(unsigned char subType,
andrew@webrtc.orgb015cbe2012-10-22 18:19:23 +00003308 unsigned int name,
3309 const char* data,
3310 unsigned short dataLengthInBytes)
3311{
3312 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId, _channelId),
3313 "Channel::SendApplicationDefinedRTCPPacket()");
henrika@webrtc.orgdf08c5d2014-03-18 10:32:33 +00003314 if (!channel_state_.Get().sending)
andrew@webrtc.orgb015cbe2012-10-22 18:19:23 +00003315 {
3316 _engineStatisticsPtr->SetLastError(
3317 VE_NOT_SENDING, kTraceError,
3318 "SendApplicationDefinedRTCPPacket() not sending");
3319 return -1;
3320 }
3321 if (NULL == data)
3322 {
3323 _engineStatisticsPtr->SetLastError(
3324 VE_INVALID_ARGUMENT, kTraceError,
3325 "SendApplicationDefinedRTCPPacket() invalid data value");
3326 return -1;
3327 }
3328 if (dataLengthInBytes % 4 != 0)
3329 {
3330 _engineStatisticsPtr->SetLastError(
3331 VE_INVALID_ARGUMENT, kTraceError,
3332 "SendApplicationDefinedRTCPPacket() invalid length value");
3333 return -1;
3334 }
3335 RTCPMethod status = _rtpRtcpModule->RTCP();
3336 if (status == kRtcpOff)
3337 {
3338 _engineStatisticsPtr->SetLastError(
3339 VE_RTCP_ERROR, kTraceError,
3340 "SendApplicationDefinedRTCPPacket() RTCP is disabled");
3341 return -1;
3342 }
3343
3344 // Create and schedule the RTCP APP packet for transmission
3345 if (_rtpRtcpModule->SetRTCPApplicationSpecificData(
3346 subType,
3347 name,
3348 (const unsigned char*) data,
3349 dataLengthInBytes) != 0)
3350 {
3351 _engineStatisticsPtr->SetLastError(
3352 VE_SEND_ERROR, kTraceError,
3353 "SendApplicationDefinedRTCPPacket() failed to send RTCP packet");
3354 return -1;
3355 }
3356 return 0;
3357}
3358
3359int
3360Channel::GetRTPStatistics(
3361 unsigned int& averageJitterMs,
3362 unsigned int& maxJitterMs,
3363 unsigned int& discardedPackets)
3364{
andrew@webrtc.orgb015cbe2012-10-22 18:19:23 +00003365 // The jitter statistics is updated for each received RTP packet and is
3366 // based on received packets.
sprang@webrtc.org4f1f5fa2013-12-19 13:26:02 +00003367 if (_rtpRtcpModule->RTCP() == kRtcpOff) {
3368 // If RTCP is off, there is no timed thread in the RTCP module regularly
3369 // generating new stats, trigger the update manually here instead.
3370 StreamStatistician* statistician =
3371 rtp_receive_statistics_->GetStatistician(rtp_receiver_->SSRC());
3372 if (statistician) {
3373 // Don't use returned statistics, use data from proxy instead so that
3374 // max jitter can be fetched atomically.
3375 RtcpStatistics s;
3376 statistician->GetStatistics(&s, true);
3377 }
andrew@webrtc.orgb015cbe2012-10-22 18:19:23 +00003378 }
3379
sprang@webrtc.org4f1f5fa2013-12-19 13:26:02 +00003380 ChannelStatistics stats = statistics_proxy_->GetStats();
andrew@webrtc.org510ee1b2013-09-23 23:02:24 +00003381 const int32_t playoutFrequency = audio_coding_->PlayoutFrequency();
sprang@webrtc.org4f1f5fa2013-12-19 13:26:02 +00003382 if (playoutFrequency > 0) {
3383 // Scale RTP statistics given the current playout frequency
3384 maxJitterMs = stats.max_jitter / (playoutFrequency / 1000);
3385 averageJitterMs = stats.rtcp.jitter / (playoutFrequency / 1000);
andrew@webrtc.orgb015cbe2012-10-22 18:19:23 +00003386 }
3387
3388 discardedPackets = _numberOfDiscardedPackets;
3389
3390 WEBRTC_TRACE(kTraceStateInfo, kTraceVoice,
3391 VoEId(_instanceId, _channelId),
3392 "GetRTPStatistics() => averageJitterMs = %lu, maxJitterMs = %lu,"
3393 " discardedPackets = %lu)",
3394 averageJitterMs, maxJitterMs, discardedPackets);
3395 return 0;
3396}
3397
andrew@webrtc.orgb015cbe2012-10-22 18:19:23 +00003398int Channel::GetRemoteRTCPReportBlocks(
3399 std::vector<ReportBlock>* report_blocks) {
3400 if (report_blocks == NULL) {
3401 _engineStatisticsPtr->SetLastError(VE_INVALID_ARGUMENT, kTraceError,
3402 "GetRemoteRTCPReportBlock()s invalid report_blocks.");
3403 return -1;
3404 }
3405
3406 // Get the report blocks from the latest received RTCP Sender or Receiver
3407 // Report. Each element in the vector contains the sender's SSRC and a
3408 // report block according to RFC 3550.
3409 std::vector<RTCPReportBlock> rtcp_report_blocks;
3410 if (_rtpRtcpModule->RemoteRTCPStat(&rtcp_report_blocks) != 0) {
3411 _engineStatisticsPtr->SetLastError(VE_RTP_RTCP_MODULE_ERROR, kTraceError,
3412 "GetRemoteRTCPReportBlocks() failed to read RTCP SR/RR report block.");
3413 return -1;
3414 }
3415
3416 if (rtcp_report_blocks.empty())
3417 return 0;
3418
3419 std::vector<RTCPReportBlock>::const_iterator it = rtcp_report_blocks.begin();
3420 for (; it != rtcp_report_blocks.end(); ++it) {
3421 ReportBlock report_block;
3422 report_block.sender_SSRC = it->remoteSSRC;
3423 report_block.source_SSRC = it->sourceSSRC;
3424 report_block.fraction_lost = it->fractionLost;
3425 report_block.cumulative_num_packets_lost = it->cumulativeLost;
3426 report_block.extended_highest_sequence_number = it->extendedHighSeqNum;
3427 report_block.interarrival_jitter = it->jitter;
3428 report_block.last_SR_timestamp = it->lastSR;
3429 report_block.delay_since_last_SR = it->delaySinceLastSR;
3430 report_blocks->push_back(report_block);
3431 }
3432 return 0;
3433}
3434
3435int
3436Channel::GetRTPStatistics(CallStatistics& stats)
3437{
wu@webrtc.org22f69bd2014-05-19 17:39:11 +00003438 // --- RtcpStatistics
andrew@webrtc.orgb015cbe2012-10-22 18:19:23 +00003439
3440 // The jitter statistics is updated for each received RTP packet and is
3441 // based on received packets.
sprang@webrtc.org4f1f5fa2013-12-19 13:26:02 +00003442 RtcpStatistics statistics;
stefan@webrtc.orga20e2d42013-08-21 20:58:21 +00003443 StreamStatistician* statistician =
3444 rtp_receive_statistics_->GetStatistician(rtp_receiver_->SSRC());
3445 if (!statistician || !statistician->GetStatistics(
wu@webrtc.org7fc75bb2013-08-15 23:38:54 +00003446 &statistics, _rtpRtcpModule->RTCP() == kRtcpOff)) {
3447 _engineStatisticsPtr->SetLastError(
3448 VE_CANNOT_RETRIEVE_RTP_STAT, kTraceWarning,
3449 "GetRTPStatistics() failed to read RTP statistics from the "
3450 "RTP/RTCP module");
andrew@webrtc.orgb015cbe2012-10-22 18:19:23 +00003451 }
3452
wu@webrtc.org7fc75bb2013-08-15 23:38:54 +00003453 stats.fractionLost = statistics.fraction_lost;
3454 stats.cumulativeLost = statistics.cumulative_lost;
3455 stats.extendedMax = statistics.extended_max_sequence_number;
3456 stats.jitterSamples = statistics.jitter;
andrew@webrtc.orgb015cbe2012-10-22 18:19:23 +00003457
3458 WEBRTC_TRACE(kTraceStateInfo, kTraceVoice,
3459 VoEId(_instanceId, _channelId),
3460 "GetRTPStatistics() => fractionLost=%lu, cumulativeLost=%lu,"
3461 " extendedMax=%lu, jitterSamples=%li)",
3462 stats.fractionLost, stats.cumulativeLost, stats.extendedMax,
3463 stats.jitterSamples);
3464
wu@webrtc.org22f69bd2014-05-19 17:39:11 +00003465 // --- RTT
andrew@webrtc.orgb015cbe2012-10-22 18:19:23 +00003466
pbos@webrtc.org54f03bc2013-04-09 10:09:10 +00003467 uint16_t RTT(0);
andrew@webrtc.orgb015cbe2012-10-22 18:19:23 +00003468 RTCPMethod method = _rtpRtcpModule->RTCP();
3469 if (method == kRtcpOff)
3470 {
3471 WEBRTC_TRACE(kTraceWarning, kTraceVoice,
3472 VoEId(_instanceId, _channelId),
3473 "GetRTPStatistics() RTCP is disabled => valid RTT "
3474 "measurements cannot be retrieved");
3475 } else
3476 {
3477 // The remote SSRC will be zero if no RTP packet has been received.
wu@webrtc.org7fc75bb2013-08-15 23:38:54 +00003478 uint32_t remoteSSRC = rtp_receiver_->SSRC();
andrew@webrtc.orgb015cbe2012-10-22 18:19:23 +00003479 if (remoteSSRC > 0)
3480 {
pbos@webrtc.org54f03bc2013-04-09 10:09:10 +00003481 uint16_t avgRTT(0);
3482 uint16_t maxRTT(0);
3483 uint16_t minRTT(0);
andrew@webrtc.orgb015cbe2012-10-22 18:19:23 +00003484
3485 if (_rtpRtcpModule->RTT(remoteSSRC, &RTT, &avgRTT, &minRTT, &maxRTT)
3486 != 0)
3487 {
3488 WEBRTC_TRACE(kTraceWarning, kTraceVoice,
3489 VoEId(_instanceId, _channelId),
3490 "GetRTPStatistics() failed to retrieve RTT from "
3491 "the RTP/RTCP module");
3492 }
3493 } else
3494 {
3495 WEBRTC_TRACE(kTraceWarning, kTraceVoice,
3496 VoEId(_instanceId, _channelId),
3497 "GetRTPStatistics() failed to measure RTT since no "
3498 "RTP packets have been received yet");
3499 }
3500 }
3501
3502 stats.rttMs = static_cast<int> (RTT);
3503
3504 WEBRTC_TRACE(kTraceStateInfo, kTraceVoice,
3505 VoEId(_instanceId, _channelId),
3506 "GetRTPStatistics() => rttMs=%d", stats.rttMs);
3507
wu@webrtc.org22f69bd2014-05-19 17:39:11 +00003508 // --- Data counters
andrew@webrtc.orgb015cbe2012-10-22 18:19:23 +00003509
pbos@webrtc.org54f03bc2013-04-09 10:09:10 +00003510 uint32_t bytesSent(0);
3511 uint32_t packetsSent(0);
3512 uint32_t bytesReceived(0);
3513 uint32_t packetsReceived(0);
andrew@webrtc.orgb015cbe2012-10-22 18:19:23 +00003514
stefan@webrtc.orga20e2d42013-08-21 20:58:21 +00003515 if (statistician) {
3516 statistician->GetDataCounters(&bytesReceived, &packetsReceived);
3517 }
wu@webrtc.org7fc75bb2013-08-15 23:38:54 +00003518
andrew@webrtc.orgb015cbe2012-10-22 18:19:23 +00003519 if (_rtpRtcpModule->DataCountersRTP(&bytesSent,
wu@webrtc.org7fc75bb2013-08-15 23:38:54 +00003520 &packetsSent) != 0)
andrew@webrtc.orgb015cbe2012-10-22 18:19:23 +00003521 {
3522 WEBRTC_TRACE(kTraceWarning, kTraceVoice,
3523 VoEId(_instanceId, _channelId),
3524 "GetRTPStatistics() failed to retrieve RTP datacounters =>"
3525 " output will not be complete");
3526 }
3527
3528 stats.bytesSent = bytesSent;
3529 stats.packetsSent = packetsSent;
3530 stats.bytesReceived = bytesReceived;
3531 stats.packetsReceived = packetsReceived;
3532
3533 WEBRTC_TRACE(kTraceStateInfo, kTraceVoice,
3534 VoEId(_instanceId, _channelId),
3535 "GetRTPStatistics() => bytesSent=%d, packetsSent=%d,"
3536 " bytesReceived=%d, packetsReceived=%d)",
3537 stats.bytesSent, stats.packetsSent, stats.bytesReceived,
3538 stats.packetsReceived);
3539
wu@webrtc.org22f69bd2014-05-19 17:39:11 +00003540 // --- Timestamps
3541 {
3542 CriticalSectionScoped lock(ts_stats_lock_.get());
3543 stats.capture_start_ntp_time_ms_ = capture_start_ntp_time_ms_;
3544 }
andrew@webrtc.orgb015cbe2012-10-22 18:19:23 +00003545 return 0;
3546}
3547
minyue@webrtc.orgdd671de2014-05-28 09:52:06 +00003548int Channel::SetREDStatus(bool enable, int redPayloadtype) {
turaj@webrtc.org7db52902012-12-11 02:15:12 +00003549 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId, _channelId),
minyue@webrtc.orgdd671de2014-05-28 09:52:06 +00003550 "Channel::SetREDStatus()");
andrew@webrtc.orgb015cbe2012-10-22 18:19:23 +00003551
turaj@webrtc.org040f8002013-01-31 18:20:17 +00003552 if (enable) {
3553 if (redPayloadtype < 0 || redPayloadtype > 127) {
3554 _engineStatisticsPtr->SetLastError(
3555 VE_PLTYPE_ERROR, kTraceError,
minyue@webrtc.orgdd671de2014-05-28 09:52:06 +00003556 "SetREDStatus() invalid RED payload type");
turaj@webrtc.org040f8002013-01-31 18:20:17 +00003557 return -1;
3558 }
3559
3560 if (SetRedPayloadType(redPayloadtype) < 0) {
3561 _engineStatisticsPtr->SetLastError(
3562 VE_CODEC_ERROR, kTraceError,
3563 "SetSecondarySendCodec() Failed to register RED ACM");
3564 return -1;
3565 }
turaj@webrtc.org7db52902012-12-11 02:15:12 +00003566 }
andrew@webrtc.orgb015cbe2012-10-22 18:19:23 +00003567
minyue@webrtc.org91c0a252014-05-23 15:16:51 +00003568 if (audio_coding_->SetREDStatus(enable) != 0) {
turaj@webrtc.org7db52902012-12-11 02:15:12 +00003569 _engineStatisticsPtr->SetLastError(
3570 VE_AUDIO_CODING_MODULE_ERROR, kTraceError,
minyue@webrtc.org91c0a252014-05-23 15:16:51 +00003571 "SetREDStatus() failed to set RED state in the ACM");
turaj@webrtc.org7db52902012-12-11 02:15:12 +00003572 return -1;
3573 }
3574 return 0;
andrew@webrtc.orgb015cbe2012-10-22 18:19:23 +00003575}
3576
3577int
minyue@webrtc.orgdd671de2014-05-28 09:52:06 +00003578Channel::GetREDStatus(bool& enabled, int& redPayloadtype)
andrew@webrtc.orgb015cbe2012-10-22 18:19:23 +00003579{
minyue@webrtc.org91c0a252014-05-23 15:16:51 +00003580 enabled = audio_coding_->REDStatus();
andrew@webrtc.orgb015cbe2012-10-22 18:19:23 +00003581 if (enabled)
3582 {
pbos@webrtc.org54f03bc2013-04-09 10:09:10 +00003583 int8_t payloadType(0);
andrew@webrtc.orgb015cbe2012-10-22 18:19:23 +00003584 if (_rtpRtcpModule->SendREDPayloadType(payloadType) != 0)
3585 {
3586 _engineStatisticsPtr->SetLastError(
3587 VE_RTP_RTCP_MODULE_ERROR, kTraceError,
minyue@webrtc.orgdd671de2014-05-28 09:52:06 +00003588 "GetREDStatus() failed to retrieve RED PT from RTP/RTCP "
andrew@webrtc.orgb015cbe2012-10-22 18:19:23 +00003589 "module");
3590 return -1;
3591 }
3592 WEBRTC_TRACE(kTraceStateInfo, kTraceVoice,
3593 VoEId(_instanceId, _channelId),
minyue@webrtc.orgdd671de2014-05-28 09:52:06 +00003594 "GetREDStatus() => enabled=%d, redPayloadtype=%d",
andrew@webrtc.orgb015cbe2012-10-22 18:19:23 +00003595 enabled, redPayloadtype);
3596 return 0;
3597 }
3598 WEBRTC_TRACE(kTraceStateInfo, kTraceVoice,
3599 VoEId(_instanceId, _channelId),
minyue@webrtc.orgdd671de2014-05-28 09:52:06 +00003600 "GetREDStatus() => enabled=%d", enabled);
andrew@webrtc.orgb015cbe2012-10-22 18:19:23 +00003601 return 0;
3602}
3603
minyue@webrtc.orgdd671de2014-05-28 09:52:06 +00003604int Channel::SetCodecFECStatus(bool enable) {
3605 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId, _channelId),
3606 "Channel::SetCodecFECStatus()");
3607
3608 if (audio_coding_->SetCodecFEC(enable) != 0) {
3609 _engineStatisticsPtr->SetLastError(
3610 VE_AUDIO_CODING_MODULE_ERROR, kTraceError,
3611 "SetCodecFECStatus() failed to set FEC state");
3612 return -1;
3613 }
3614 return 0;
3615}
3616
3617bool Channel::GetCodecFECStatus() {
3618 bool enabled = audio_coding_->CodecFEC();
3619 WEBRTC_TRACE(kTraceStateInfo, kTraceVoice,
3620 VoEId(_instanceId, _channelId),
3621 "GetCodecFECStatus() => enabled=%d", enabled);
3622 return enabled;
3623}
3624
pwestin@webrtc.orgb8171ff2013-06-05 15:33:20 +00003625void Channel::SetNACKStatus(bool enable, int maxNumberOfPackets) {
3626 // None of these functions can fail.
3627 _rtpRtcpModule->SetStorePacketsStatus(enable, maxNumberOfPackets);
stefan@webrtc.orgdb74c612013-09-06 13:40:11 +00003628 rtp_receive_statistics_->SetMaxReorderingThreshold(maxNumberOfPackets);
3629 rtp_receiver_->SetNACKStatus(enable ? kNackRtcp : kNackOff);
pwestin@webrtc.org4aa9f1a2013-06-06 21:09:01 +00003630 if (enable)
andrew@webrtc.org510ee1b2013-09-23 23:02:24 +00003631 audio_coding_->EnableNack(maxNumberOfPackets);
pwestin@webrtc.org4aa9f1a2013-06-06 21:09:01 +00003632 else
andrew@webrtc.org510ee1b2013-09-23 23:02:24 +00003633 audio_coding_->DisableNack();
pwestin@webrtc.orgb8171ff2013-06-05 15:33:20 +00003634}
3635
pwestin@webrtc.org4aa9f1a2013-06-06 21:09:01 +00003636// Called when we are missing one or more packets.
3637int Channel::ResendPackets(const uint16_t* sequence_numbers, int length) {
pwestin@webrtc.orgb8171ff2013-06-05 15:33:20 +00003638 return _rtpRtcpModule->SendNACK(sequence_numbers, length);
3639}
3640
andrew@webrtc.orgb015cbe2012-10-22 18:19:23 +00003641int
3642Channel::StartRTPDump(const char fileNameUTF8[1024],
3643 RTPDirections direction)
3644{
3645 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId, _channelId),
3646 "Channel::StartRTPDump()");
3647 if ((direction != kRtpIncoming) && (direction != kRtpOutgoing))
3648 {
3649 _engineStatisticsPtr->SetLastError(
3650 VE_INVALID_ARGUMENT, kTraceError,
3651 "StartRTPDump() invalid RTP direction");
3652 return -1;
3653 }
3654 RtpDump* rtpDumpPtr = (direction == kRtpIncoming) ?
3655 &_rtpDumpIn : &_rtpDumpOut;
3656 if (rtpDumpPtr == NULL)
3657 {
3658 assert(false);
3659 return -1;
3660 }
3661 if (rtpDumpPtr->IsActive())
3662 {
3663 rtpDumpPtr->Stop();
3664 }
3665 if (rtpDumpPtr->Start(fileNameUTF8) != 0)
3666 {
3667 _engineStatisticsPtr->SetLastError(
3668 VE_BAD_FILE, kTraceError,
3669 "StartRTPDump() failed to create file");
3670 return -1;
3671 }
3672 return 0;
3673}
3674
3675int
3676Channel::StopRTPDump(RTPDirections direction)
3677{
3678 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId, _channelId),
3679 "Channel::StopRTPDump()");
3680 if ((direction != kRtpIncoming) && (direction != kRtpOutgoing))
3681 {
3682 _engineStatisticsPtr->SetLastError(
3683 VE_INVALID_ARGUMENT, kTraceError,
3684 "StopRTPDump() invalid RTP direction");
3685 return -1;
3686 }
3687 RtpDump* rtpDumpPtr = (direction == kRtpIncoming) ?
3688 &_rtpDumpIn : &_rtpDumpOut;
3689 if (rtpDumpPtr == NULL)
3690 {
3691 assert(false);
3692 return -1;
3693 }
3694 if (!rtpDumpPtr->IsActive())
3695 {
3696 return 0;
3697 }
3698 return rtpDumpPtr->Stop();
3699}
3700
3701bool
3702Channel::RTPDumpIsActive(RTPDirections direction)
3703{
3704 if ((direction != kRtpIncoming) &&
3705 (direction != kRtpOutgoing))
3706 {
3707 _engineStatisticsPtr->SetLastError(
3708 VE_INVALID_ARGUMENT, kTraceError,
3709 "RTPDumpIsActive() invalid RTP direction");
3710 return false;
3711 }
3712 RtpDump* rtpDumpPtr = (direction == kRtpIncoming) ?
3713 &_rtpDumpIn : &_rtpDumpOut;
3714 return rtpDumpPtr->IsActive();
3715}
3716
solenberg@webrtc.orgfec6b6e2014-03-24 10:38:25 +00003717void Channel::SetVideoEngineBWETarget(ViENetwork* vie_network,
3718 int video_channel) {
3719 CriticalSectionScoped cs(&_callbackCritSect);
3720 if (vie_network_) {
3721 vie_network_->Release();
3722 vie_network_ = NULL;
3723 }
3724 video_channel_ = -1;
3725
3726 if (vie_network != NULL && video_channel != -1) {
3727 vie_network_ = vie_network;
3728 video_channel_ = video_channel;
3729 }
3730}
3731
pbos@webrtc.org54f03bc2013-04-09 10:09:10 +00003732uint32_t
andrew@webrtc.orgb015cbe2012-10-22 18:19:23 +00003733Channel::Demultiplex(const AudioFrame& audioFrame)
3734{
3735 WEBRTC_TRACE(kTraceStream, kTraceVoice, VoEId(_instanceId,_channelId),
3736 "Channel::Demultiplex()");
andrew@webrtc.orgd4682362013-01-22 04:44:30 +00003737 _audioFrame.CopyFrom(audioFrame);
andrew@webrtc.orgb015cbe2012-10-22 18:19:23 +00003738 _audioFrame.id_ = _channelId;
3739 return 0;
3740}
3741
xians@webrtc.org44f12392013-07-31 16:23:37 +00003742void Channel::Demultiplex(const int16_t* audio_data,
xians@webrtc.org0e6fa8c2013-07-31 16:27:42 +00003743 int sample_rate,
xians@webrtc.org44f12392013-07-31 16:23:37 +00003744 int number_of_frames,
xians@webrtc.org0e6fa8c2013-07-31 16:27:42 +00003745 int number_of_channels) {
xians@webrtc.org44f12392013-07-31 16:23:37 +00003746 CodecInst codec;
3747 GetSendCodec(codec);
xians@webrtc.org44f12392013-07-31 16:23:37 +00003748
andrew@webrtc.orgf7c73b52014-04-03 21:56:01 +00003749 if (!mono_recording_audio_.get()) {
3750 // Temporary space for DownConvertToCodecFormat.
3751 mono_recording_audio_.reset(new int16_t[kMaxMonoDataSizeSamples]);
xians@webrtc.org44f12392013-07-31 16:23:37 +00003752 }
andrew@webrtc.orgf7c73b52014-04-03 21:56:01 +00003753 DownConvertToCodecFormat(audio_data,
3754 number_of_frames,
3755 number_of_channels,
3756 sample_rate,
3757 codec.channels,
3758 codec.plfreq,
3759 mono_recording_audio_.get(),
3760 &input_resampler_,
3761 &_audioFrame);
xians@webrtc.org44f12392013-07-31 16:23:37 +00003762}
3763
pbos@webrtc.org54f03bc2013-04-09 10:09:10 +00003764uint32_t
andrew@webrtc.orgb015cbe2012-10-22 18:19:23 +00003765Channel::PrepareEncodeAndSend(int mixingFrequency)
3766{
3767 WEBRTC_TRACE(kTraceStream, kTraceVoice, VoEId(_instanceId,_channelId),
3768 "Channel::PrepareEncodeAndSend()");
3769
3770 if (_audioFrame.samples_per_channel_ == 0)
3771 {
3772 WEBRTC_TRACE(kTraceWarning, kTraceVoice, VoEId(_instanceId,_channelId),
3773 "Channel::PrepareEncodeAndSend() invalid audio frame");
tommi@webrtc.org9fbd3ec2014-07-11 19:09:59 +00003774 return 0xFFFFFFFF;
andrew@webrtc.orgb015cbe2012-10-22 18:19:23 +00003775 }
3776
henrika@webrtc.orgdf08c5d2014-03-18 10:32:33 +00003777 if (channel_state_.Get().input_file_playing)
andrew@webrtc.orgb015cbe2012-10-22 18:19:23 +00003778 {
3779 MixOrReplaceAudioWithFile(mixingFrequency);
3780 }
3781
andrew@webrtc.org7d20dda2014-05-14 19:00:59 +00003782 bool is_muted = Mute(); // Cache locally as Mute() takes a lock.
3783 if (is_muted) {
3784 AudioFrameOperations::Mute(_audioFrame);
andrew@webrtc.orgb015cbe2012-10-22 18:19:23 +00003785 }
3786
henrika@webrtc.orgdf08c5d2014-03-18 10:32:33 +00003787 if (channel_state_.Get().input_external_media)
andrew@webrtc.orgb015cbe2012-10-22 18:19:23 +00003788 {
3789 CriticalSectionScoped cs(&_callbackCritSect);
3790 const bool isStereo = (_audioFrame.num_channels_ == 2);
3791 if (_inputExternalMediaCallbackPtr)
3792 {
3793 _inputExternalMediaCallbackPtr->Process(
3794 _channelId,
3795 kRecordingPerChannel,
pbos@webrtc.org54f03bc2013-04-09 10:09:10 +00003796 (int16_t*)_audioFrame.data_,
andrew@webrtc.orgb015cbe2012-10-22 18:19:23 +00003797 _audioFrame.samples_per_channel_,
3798 _audioFrame.sample_rate_hz_,
3799 isStereo);
3800 }
3801 }
3802
3803 InsertInbandDtmfTone();
3804
andrew@webrtc.orge95dc252014-01-07 17:45:09 +00003805 if (_includeAudioLevelIndication) {
andrew@webrtc.org3cd0f7c2014-05-05 18:22:21 +00003806 int length = _audioFrame.samples_per_channel_ * _audioFrame.num_channels_;
andrew@webrtc.org7d20dda2014-05-14 19:00:59 +00003807 if (is_muted) {
3808 rms_level_.ProcessMuted(length);
3809 } else {
3810 rms_level_.Process(_audioFrame.data_, length);
3811 }
andrew@webrtc.orgb015cbe2012-10-22 18:19:23 +00003812 }
3813
3814 return 0;
3815}
3816
pbos@webrtc.org54f03bc2013-04-09 10:09:10 +00003817uint32_t
andrew@webrtc.orgb015cbe2012-10-22 18:19:23 +00003818Channel::EncodeAndSend()
3819{
3820 WEBRTC_TRACE(kTraceStream, kTraceVoice, VoEId(_instanceId,_channelId),
3821 "Channel::EncodeAndSend()");
3822
3823 assert(_audioFrame.num_channels_ <= 2);
3824 if (_audioFrame.samples_per_channel_ == 0)
3825 {
3826 WEBRTC_TRACE(kTraceWarning, kTraceVoice, VoEId(_instanceId,_channelId),
3827 "Channel::EncodeAndSend() invalid audio frame");
tommi@webrtc.org9fbd3ec2014-07-11 19:09:59 +00003828 return 0xFFFFFFFF;
andrew@webrtc.orgb015cbe2012-10-22 18:19:23 +00003829 }
3830
3831 _audioFrame.id_ = _channelId;
3832
3833 // --- Add 10ms of raw (PCM) audio data to the encoder @ 32kHz.
3834
3835 // The ACM resamples internally.
3836 _audioFrame.timestamp_ = _timeStamp;
andrew@webrtc.org510ee1b2013-09-23 23:02:24 +00003837 if (audio_coding_->Add10MsData((AudioFrame&)_audioFrame) != 0)
andrew@webrtc.orgb015cbe2012-10-22 18:19:23 +00003838 {
3839 WEBRTC_TRACE(kTraceError, kTraceVoice, VoEId(_instanceId,_channelId),
3840 "Channel::EncodeAndSend() ACM encoding failed");
tommi@webrtc.org9fbd3ec2014-07-11 19:09:59 +00003841 return 0xFFFFFFFF;
andrew@webrtc.orgb015cbe2012-10-22 18:19:23 +00003842 }
3843
3844 _timeStamp += _audioFrame.samples_per_channel_;
3845
3846 // --- Encode if complete frame is ready
3847
3848 // This call will trigger AudioPacketizationCallback::SendData if encoding
3849 // is done and payload is ready for packetization and transmission.
andrew@webrtc.org510ee1b2013-09-23 23:02:24 +00003850 return audio_coding_->Process();
andrew@webrtc.orgb015cbe2012-10-22 18:19:23 +00003851}
3852
3853int Channel::RegisterExternalMediaProcessing(
3854 ProcessingTypes type,
3855 VoEMediaProcess& processObject)
3856{
3857 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId,_channelId),
3858 "Channel::RegisterExternalMediaProcessing()");
3859
3860 CriticalSectionScoped cs(&_callbackCritSect);
3861
3862 if (kPlaybackPerChannel == type)
3863 {
3864 if (_outputExternalMediaCallbackPtr)
3865 {
3866 _engineStatisticsPtr->SetLastError(
3867 VE_INVALID_OPERATION, kTraceError,
3868 "Channel::RegisterExternalMediaProcessing() "
3869 "output external media already enabled");
3870 return -1;
3871 }
3872 _outputExternalMediaCallbackPtr = &processObject;
3873 _outputExternalMedia = true;
3874 }
3875 else if (kRecordingPerChannel == type)
3876 {
3877 if (_inputExternalMediaCallbackPtr)
3878 {
3879 _engineStatisticsPtr->SetLastError(
3880 VE_INVALID_OPERATION, kTraceError,
3881 "Channel::RegisterExternalMediaProcessing() "
3882 "output external media already enabled");
3883 return -1;
3884 }
3885 _inputExternalMediaCallbackPtr = &processObject;
henrika@webrtc.orgdf08c5d2014-03-18 10:32:33 +00003886 channel_state_.SetInputExternalMedia(true);
andrew@webrtc.orgb015cbe2012-10-22 18:19:23 +00003887 }
3888 return 0;
3889}
3890
3891int Channel::DeRegisterExternalMediaProcessing(ProcessingTypes type)
3892{
3893 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId,_channelId),
3894 "Channel::DeRegisterExternalMediaProcessing()");
3895
3896 CriticalSectionScoped cs(&_callbackCritSect);
3897
3898 if (kPlaybackPerChannel == type)
3899 {
3900 if (!_outputExternalMediaCallbackPtr)
3901 {
3902 _engineStatisticsPtr->SetLastError(
3903 VE_INVALID_OPERATION, kTraceWarning,
3904 "Channel::DeRegisterExternalMediaProcessing() "
3905 "output external media already disabled");
3906 return 0;
3907 }
3908 _outputExternalMedia = false;
3909 _outputExternalMediaCallbackPtr = NULL;
3910 }
3911 else if (kRecordingPerChannel == type)
3912 {
3913 if (!_inputExternalMediaCallbackPtr)
3914 {
3915 _engineStatisticsPtr->SetLastError(
3916 VE_INVALID_OPERATION, kTraceWarning,
3917 "Channel::DeRegisterExternalMediaProcessing() "
3918 "input external media already disabled");
3919 return 0;
3920 }
henrika@webrtc.orgdf08c5d2014-03-18 10:32:33 +00003921 channel_state_.SetInputExternalMedia(false);
andrew@webrtc.orgb015cbe2012-10-22 18:19:23 +00003922 _inputExternalMediaCallbackPtr = NULL;
3923 }
3924
3925 return 0;
3926}
3927
roosa@google.comb9e3afc2012-12-12 23:00:29 +00003928int Channel::SetExternalMixing(bool enabled) {
3929 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId,_channelId),
3930 "Channel::SetExternalMixing(enabled=%d)", enabled);
3931
henrika@webrtc.orgdf08c5d2014-03-18 10:32:33 +00003932 if (channel_state_.Get().playing)
roosa@google.comb9e3afc2012-12-12 23:00:29 +00003933 {
3934 _engineStatisticsPtr->SetLastError(
3935 VE_INVALID_OPERATION, kTraceError,
3936 "Channel::SetExternalMixing() "
3937 "external mixing cannot be changed while playing.");
3938 return -1;
3939 }
3940
3941 _externalMixing = enabled;
3942
3943 return 0;
3944}
3945
andrew@webrtc.orgb015cbe2012-10-22 18:19:23 +00003946int
andrew@webrtc.orgb015cbe2012-10-22 18:19:23 +00003947Channel::GetNetworkStatistics(NetworkStatistics& stats)
3948{
3949 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId,_channelId),
3950 "Channel::GetNetworkStatistics()");
tina.legrand@webrtc.orge9bb4e52013-02-21 10:27:48 +00003951 ACMNetworkStatistics acm_stats;
andrew@webrtc.org510ee1b2013-09-23 23:02:24 +00003952 int return_value = audio_coding_->NetworkStatistics(&acm_stats);
tina.legrand@webrtc.orge9bb4e52013-02-21 10:27:48 +00003953 if (return_value >= 0) {
3954 memcpy(&stats, &acm_stats, sizeof(NetworkStatistics));
3955 }
3956 return return_value;
andrew@webrtc.orgb015cbe2012-10-22 18:19:23 +00003957}
3958
wu@webrtc.org79d6daf2013-12-13 19:17:43 +00003959void Channel::GetDecodingCallStatistics(AudioDecodingCallStats* stats) const {
3960 audio_coding_->GetDecodingCallStatistics(stats);
3961}
3962
pwestin@webrtc.orgf2724972013-04-11 20:23:35 +00003963bool Channel::GetDelayEstimate(int* jitter_buffer_delay_ms,
3964 int* playout_buffer_delay_ms) const {
3965 if (_average_jitter_buffer_delay_us == 0) {
andrew@webrtc.orgb015cbe2012-10-22 18:19:23 +00003966 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId,_channelId),
pwestin@webrtc.orgf2724972013-04-11 20:23:35 +00003967 "Channel::GetDelayEstimate() no valid estimate.");
3968 return false;
3969 }
3970 *jitter_buffer_delay_ms = (_average_jitter_buffer_delay_us + 500) / 1000 +
3971 _recPacketDelayMs;
3972 *playout_buffer_delay_ms = playout_delay_ms_;
3973 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId,_channelId),
3974 "Channel::GetDelayEstimate()");
3975 return true;
andrew@webrtc.orgb015cbe2012-10-22 18:19:23 +00003976}
3977
turaj@webrtc.orgead8a5b2013-02-12 21:42:18 +00003978int Channel::SetInitialPlayoutDelay(int delay_ms)
3979{
3980 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId,_channelId),
3981 "Channel::SetInitialPlayoutDelay()");
3982 if ((delay_ms < kVoiceEngineMinMinPlayoutDelayMs) ||
3983 (delay_ms > kVoiceEngineMaxMinPlayoutDelayMs))
3984 {
3985 _engineStatisticsPtr->SetLastError(
3986 VE_INVALID_ARGUMENT, kTraceError,
3987 "SetInitialPlayoutDelay() invalid min delay");
3988 return -1;
3989 }
andrew@webrtc.org510ee1b2013-09-23 23:02:24 +00003990 if (audio_coding_->SetInitialPlayoutDelay(delay_ms) != 0)
turaj@webrtc.orgead8a5b2013-02-12 21:42:18 +00003991 {
3992 _engineStatisticsPtr->SetLastError(
3993 VE_AUDIO_CODING_MODULE_ERROR, kTraceError,
3994 "SetInitialPlayoutDelay() failed to set min playout delay");
3995 return -1;
3996 }
3997 return 0;
3998}
3999
4000
andrew@webrtc.orgb015cbe2012-10-22 18:19:23 +00004001int
4002Channel::SetMinimumPlayoutDelay(int delayMs)
4003{
4004 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId,_channelId),
4005 "Channel::SetMinimumPlayoutDelay()");
4006 if ((delayMs < kVoiceEngineMinMinPlayoutDelayMs) ||
4007 (delayMs > kVoiceEngineMaxMinPlayoutDelayMs))
4008 {
4009 _engineStatisticsPtr->SetLastError(
4010 VE_INVALID_ARGUMENT, kTraceError,
4011 "SetMinimumPlayoutDelay() invalid min delay");
4012 return -1;
4013 }
andrew@webrtc.org510ee1b2013-09-23 23:02:24 +00004014 if (audio_coding_->SetMinimumPlayoutDelay(delayMs) != 0)
andrew@webrtc.orgb015cbe2012-10-22 18:19:23 +00004015 {
4016 _engineStatisticsPtr->SetLastError(
4017 VE_AUDIO_CODING_MODULE_ERROR, kTraceError,
4018 "SetMinimumPlayoutDelay() failed to set min playout delay");
4019 return -1;
4020 }
4021 return 0;
4022}
4023
pwestin@webrtc.orgf2724972013-04-11 20:23:35 +00004024void Channel::UpdatePlayoutTimestamp(bool rtcp) {
4025 uint32_t playout_timestamp = 0;
4026
andrew@webrtc.org510ee1b2013-09-23 23:02:24 +00004027 if (audio_coding_->PlayoutTimestamp(&playout_timestamp) == -1) {
turaj@webrtc.orgca4bc682014-07-25 17:50:10 +00004028 // This can happen if this channel has not been received any RTP packet. In
4029 // this case, NetEq is not capable of computing playout timestamp.
pwestin@webrtc.orgf2724972013-04-11 20:23:35 +00004030 return;
4031 }
4032
4033 uint16_t delay_ms = 0;
4034 if (_audioDeviceModulePtr->PlayoutDelay(&delay_ms) == -1) {
4035 WEBRTC_TRACE(kTraceWarning, kTraceVoice, VoEId(_instanceId,_channelId),
4036 "Channel::UpdatePlayoutTimestamp() failed to read playout"
4037 " delay from the ADM");
4038 _engineStatisticsPtr->SetLastError(
4039 VE_CANNOT_RETRIEVE_VALUE, kTraceError,
4040 "UpdatePlayoutTimestamp() failed to retrieve playout delay");
4041 return;
4042 }
4043
turaj@webrtc.orgf1b92fd2013-12-13 21:05:07 +00004044 jitter_buffer_playout_timestamp_ = playout_timestamp;
4045
pwestin@webrtc.orgf2724972013-04-11 20:23:35 +00004046 // Remove the playout delay.
wu@webrtc.org81f8df92014-06-05 20:34:08 +00004047 playout_timestamp -= (delay_ms * (GetPlayoutFrequency() / 1000));
pwestin@webrtc.orgf2724972013-04-11 20:23:35 +00004048
4049 WEBRTC_TRACE(kTraceStream, kTraceVoice, VoEId(_instanceId,_channelId),
4050 "Channel::UpdatePlayoutTimestamp() => playoutTimestamp = %lu",
4051 playout_timestamp);
4052
4053 if (rtcp) {
4054 playout_timestamp_rtcp_ = playout_timestamp;
4055 } else {
4056 playout_timestamp_rtp_ = playout_timestamp;
4057 }
4058 playout_delay_ms_ = delay_ms;
4059}
4060
4061int Channel::GetPlayoutTimestamp(unsigned int& timestamp) {
4062 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId,_channelId),
4063 "Channel::GetPlayoutTimestamp()");
4064 if (playout_timestamp_rtp_ == 0) {
4065 _engineStatisticsPtr->SetLastError(
4066 VE_CANNOT_RETRIEVE_VALUE, kTraceError,
4067 "GetPlayoutTimestamp() failed to retrieve timestamp");
4068 return -1;
4069 }
4070 timestamp = playout_timestamp_rtp_;
4071 WEBRTC_TRACE(kTraceStateInfo, kTraceVoice,
4072 VoEId(_instanceId,_channelId),
4073 "GetPlayoutTimestamp() => timestamp=%u", timestamp);
4074 return 0;
andrew@webrtc.orgb015cbe2012-10-22 18:19:23 +00004075}
4076
4077int
4078Channel::SetInitTimestamp(unsigned int timestamp)
4079{
4080 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId,_channelId),
4081 "Channel::SetInitTimestamp()");
henrika@webrtc.orgdf08c5d2014-03-18 10:32:33 +00004082 if (channel_state_.Get().sending)
andrew@webrtc.orgb015cbe2012-10-22 18:19:23 +00004083 {
4084 _engineStatisticsPtr->SetLastError(
4085 VE_SENDING, kTraceError, "SetInitTimestamp() already sending");
4086 return -1;
4087 }
4088 if (_rtpRtcpModule->SetStartTimestamp(timestamp) != 0)
4089 {
4090 _engineStatisticsPtr->SetLastError(
4091 VE_RTP_RTCP_MODULE_ERROR, kTraceError,
4092 "SetInitTimestamp() failed to set timestamp");
4093 return -1;
4094 }
4095 return 0;
4096}
4097
4098int
4099Channel::SetInitSequenceNumber(short sequenceNumber)
4100{
4101 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId,_channelId),
4102 "Channel::SetInitSequenceNumber()");
henrika@webrtc.orgdf08c5d2014-03-18 10:32:33 +00004103 if (channel_state_.Get().sending)
andrew@webrtc.orgb015cbe2012-10-22 18:19:23 +00004104 {
4105 _engineStatisticsPtr->SetLastError(
4106 VE_SENDING, kTraceError,
4107 "SetInitSequenceNumber() already sending");
4108 return -1;
4109 }
4110 if (_rtpRtcpModule->SetSequenceNumber(sequenceNumber) != 0)
4111 {
4112 _engineStatisticsPtr->SetLastError(
4113 VE_RTP_RTCP_MODULE_ERROR, kTraceError,
4114 "SetInitSequenceNumber() failed to set sequence number");
4115 return -1;
4116 }
4117 return 0;
4118}
4119
4120int
wu@webrtc.org7fc75bb2013-08-15 23:38:54 +00004121Channel::GetRtpRtcp(RtpRtcp** rtpRtcpModule, RtpReceiver** rtp_receiver) const
andrew@webrtc.orgb015cbe2012-10-22 18:19:23 +00004122{
4123 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId,_channelId),
4124 "Channel::GetRtpRtcp()");
wu@webrtc.org7fc75bb2013-08-15 23:38:54 +00004125 *rtpRtcpModule = _rtpRtcpModule.get();
4126 *rtp_receiver = rtp_receiver_.get();
andrew@webrtc.orgb015cbe2012-10-22 18:19:23 +00004127 return 0;
4128}
4129
4130// TODO(andrew): refactor Mix functions here and in transmit_mixer.cc to use
4131// a shared helper.
pbos@webrtc.org54f03bc2013-04-09 10:09:10 +00004132int32_t
pbos@webrtc.orgca7a9a22013-05-14 08:31:39 +00004133Channel::MixOrReplaceAudioWithFile(int mixingFrequency)
andrew@webrtc.orgb015cbe2012-10-22 18:19:23 +00004134{
andrew@webrtc.orgba476162014-04-25 23:10:28 +00004135 scoped_ptr<int16_t[]> fileBuffer(new int16_t[640]);
andrew@webrtc.orgb015cbe2012-10-22 18:19:23 +00004136 int fileSamples(0);
4137
4138 {
4139 CriticalSectionScoped cs(&_fileCritSect);
4140
4141 if (_inputFilePlayerPtr == NULL)
4142 {
4143 WEBRTC_TRACE(kTraceWarning, kTraceVoice,
4144 VoEId(_instanceId, _channelId),
4145 "Channel::MixOrReplaceAudioWithFile() fileplayer"
4146 " doesnt exist");
4147 return -1;
4148 }
4149
4150 if (_inputFilePlayerPtr->Get10msAudioFromFile(fileBuffer.get(),
4151 fileSamples,
4152 mixingFrequency) == -1)
4153 {
4154 WEBRTC_TRACE(kTraceWarning, kTraceVoice,
4155 VoEId(_instanceId, _channelId),
4156 "Channel::MixOrReplaceAudioWithFile() file mixing "
4157 "failed");
4158 return -1;
4159 }
4160 if (fileSamples == 0)
4161 {
4162 WEBRTC_TRACE(kTraceWarning, kTraceVoice,
4163 VoEId(_instanceId, _channelId),
4164 "Channel::MixOrReplaceAudioWithFile() file is ended");
4165 return 0;
4166 }
4167 }
4168
4169 assert(_audioFrame.samples_per_channel_ == fileSamples);
4170
4171 if (_mixFileWithMicrophone)
4172 {
4173 // Currently file stream is always mono.
4174 // TODO(xians): Change the code when FilePlayer supports real stereo.
andrew@webrtc.orgf7c73b52014-04-03 21:56:01 +00004175 MixWithSat(_audioFrame.data_,
4176 _audioFrame.num_channels_,
4177 fileBuffer.get(),
4178 1,
4179 fileSamples);
andrew@webrtc.orgb015cbe2012-10-22 18:19:23 +00004180 }
4181 else
4182 {
4183 // Replace ACM audio with file.
4184 // Currently file stream is always mono.
4185 // TODO(xians): Change the code when FilePlayer supports real stereo.
4186 _audioFrame.UpdateFrame(_channelId,
tommi@webrtc.org9fbd3ec2014-07-11 19:09:59 +00004187 0xFFFFFFFF,
andrew@webrtc.orgb015cbe2012-10-22 18:19:23 +00004188 fileBuffer.get(),
4189 fileSamples,
4190 mixingFrequency,
4191 AudioFrame::kNormalSpeech,
4192 AudioFrame::kVadUnknown,
4193 1);
4194
4195 }
4196 return 0;
4197}
4198
pbos@webrtc.org54f03bc2013-04-09 10:09:10 +00004199int32_t
andrew@webrtc.orgb015cbe2012-10-22 18:19:23 +00004200Channel::MixAudioWithFile(AudioFrame& audioFrame,
pbos@webrtc.orgca7a9a22013-05-14 08:31:39 +00004201 int mixingFrequency)
andrew@webrtc.orgb015cbe2012-10-22 18:19:23 +00004202{
4203 assert(mixingFrequency <= 32000);
4204
andrew@webrtc.orgba476162014-04-25 23:10:28 +00004205 scoped_ptr<int16_t[]> fileBuffer(new int16_t[640]);
andrew@webrtc.orgb015cbe2012-10-22 18:19:23 +00004206 int fileSamples(0);
4207
4208 {
4209 CriticalSectionScoped cs(&_fileCritSect);
4210
4211 if (_outputFilePlayerPtr == NULL)
4212 {
4213 WEBRTC_TRACE(kTraceWarning, kTraceVoice,
4214 VoEId(_instanceId, _channelId),
4215 "Channel::MixAudioWithFile() file mixing failed");
4216 return -1;
4217 }
4218
4219 // We should get the frequency we ask for.
4220 if (_outputFilePlayerPtr->Get10msAudioFromFile(fileBuffer.get(),
4221 fileSamples,
4222 mixingFrequency) == -1)
4223 {
4224 WEBRTC_TRACE(kTraceWarning, kTraceVoice,
4225 VoEId(_instanceId, _channelId),
4226 "Channel::MixAudioWithFile() file mixing failed");
4227 return -1;
4228 }
4229 }
4230
4231 if (audioFrame.samples_per_channel_ == fileSamples)
4232 {
4233 // Currently file stream is always mono.
4234 // TODO(xians): Change the code when FilePlayer supports real stereo.
andrew@webrtc.orgf7c73b52014-04-03 21:56:01 +00004235 MixWithSat(audioFrame.data_,
4236 audioFrame.num_channels_,
4237 fileBuffer.get(),
4238 1,
4239 fileSamples);
andrew@webrtc.orgb015cbe2012-10-22 18:19:23 +00004240 }
4241 else
4242 {
4243 WEBRTC_TRACE(kTraceWarning, kTraceVoice, VoEId(_instanceId,_channelId),
4244 "Channel::MixAudioWithFile() samples_per_channel_(%d) != "
4245 "fileSamples(%d)",
4246 audioFrame.samples_per_channel_, fileSamples);
4247 return -1;
4248 }
4249
4250 return 0;
4251}
4252
4253int
4254Channel::InsertInbandDtmfTone()
4255{
4256 // Check if we should start a new tone.
4257 if (_inbandDtmfQueue.PendingDtmf() &&
4258 !_inbandDtmfGenerator.IsAddingTone() &&
4259 _inbandDtmfGenerator.DelaySinceLastTone() >
4260 kMinTelephoneEventSeparationMs)
4261 {
pbos@webrtc.org54f03bc2013-04-09 10:09:10 +00004262 int8_t eventCode(0);
4263 uint16_t lengthMs(0);
4264 uint8_t attenuationDb(0);
andrew@webrtc.orgb015cbe2012-10-22 18:19:23 +00004265
4266 eventCode = _inbandDtmfQueue.NextDtmf(&lengthMs, &attenuationDb);
4267 _inbandDtmfGenerator.AddTone(eventCode, lengthMs, attenuationDb);
4268 if (_playInbandDtmfEvent)
4269 {
4270 // Add tone to output mixer using a reduced length to minimize
4271 // risk of echo.
4272 _outputMixerPtr->PlayDtmfTone(eventCode, lengthMs - 80,
4273 attenuationDb);
4274 }
4275 }
4276
4277 if (_inbandDtmfGenerator.IsAddingTone())
4278 {
pbos@webrtc.org54f03bc2013-04-09 10:09:10 +00004279 uint16_t frequency(0);
andrew@webrtc.orgb015cbe2012-10-22 18:19:23 +00004280 _inbandDtmfGenerator.GetSampleRate(frequency);
4281
4282 if (frequency != _audioFrame.sample_rate_hz_)
4283 {
4284 // Update sample rate of Dtmf tone since the mixing frequency
4285 // has changed.
4286 _inbandDtmfGenerator.SetSampleRate(
pbos@webrtc.org54f03bc2013-04-09 10:09:10 +00004287 (uint16_t) (_audioFrame.sample_rate_hz_));
andrew@webrtc.orgb015cbe2012-10-22 18:19:23 +00004288 // Reset the tone to be added taking the new sample rate into
4289 // account.
4290 _inbandDtmfGenerator.ResetTone();
4291 }
andrew@webrtc.orgd4682362013-01-22 04:44:30 +00004292
pbos@webrtc.org54f03bc2013-04-09 10:09:10 +00004293 int16_t toneBuffer[320];
4294 uint16_t toneSamples(0);
andrew@webrtc.orgb015cbe2012-10-22 18:19:23 +00004295 // Get 10ms tone segment and set time since last tone to zero
4296 if (_inbandDtmfGenerator.Get10msTone(toneBuffer, toneSamples) == -1)
4297 {
4298 WEBRTC_TRACE(kTraceWarning, kTraceVoice,
4299 VoEId(_instanceId, _channelId),
4300 "Channel::EncodeAndSend() inserting Dtmf failed");
4301 return -1;
4302 }
4303
4304 // Replace mixed audio with DTMF tone.
andrew@webrtc.orgd4682362013-01-22 04:44:30 +00004305 for (int sample = 0;
andrew@webrtc.orgb015cbe2012-10-22 18:19:23 +00004306 sample < _audioFrame.samples_per_channel_;
4307 sample++)
4308 {
andrew@webrtc.orgd4682362013-01-22 04:44:30 +00004309 for (int channel = 0;
4310 channel < _audioFrame.num_channels_;
andrew@webrtc.orgb015cbe2012-10-22 18:19:23 +00004311 channel++)
4312 {
andrew@webrtc.orgd4682362013-01-22 04:44:30 +00004313 const int index = sample * _audioFrame.num_channels_ + channel;
4314 _audioFrame.data_[index] = toneBuffer[sample];
andrew@webrtc.orgb015cbe2012-10-22 18:19:23 +00004315 }
4316 }
andrew@webrtc.orgd4682362013-01-22 04:44:30 +00004317
andrew@webrtc.orgb015cbe2012-10-22 18:19:23 +00004318 assert(_audioFrame.samples_per_channel_ == toneSamples);
4319 } else
4320 {
4321 // Add 10ms to "delay-since-last-tone" counter
4322 _inbandDtmfGenerator.UpdateDelaySinceLastTone();
4323 }
4324 return 0;
4325}
4326
pbos@webrtc.org54f03bc2013-04-09 10:09:10 +00004327int32_t
andrew@webrtc.orgb015cbe2012-10-22 18:19:23 +00004328Channel::SendPacketRaw(const void *data, int len, bool RTCP)
4329{
wu@webrtc.orgb27e6702013-10-18 21:10:51 +00004330 CriticalSectionScoped cs(&_callbackCritSect);
andrew@webrtc.orgb015cbe2012-10-22 18:19:23 +00004331 if (_transportPtr == NULL)
4332 {
4333 return -1;
4334 }
4335 if (!RTCP)
4336 {
4337 return _transportPtr->SendPacket(_channelId, data, len);
4338 }
4339 else
4340 {
4341 return _transportPtr->SendRTCPPacket(_channelId, data, len);
4342 }
4343}
4344
pwestin@webrtc.orgf2724972013-04-11 20:23:35 +00004345// Called for incoming RTP packets after successful RTP header parsing.
4346void Channel::UpdatePacketDelay(uint32_t rtp_timestamp,
4347 uint16_t sequence_number) {
4348 WEBRTC_TRACE(kTraceStream, kTraceVoice, VoEId(_instanceId,_channelId),
4349 "Channel::UpdatePacketDelay(timestamp=%lu, sequenceNumber=%u)",
4350 rtp_timestamp, sequence_number);
andrew@webrtc.orgb015cbe2012-10-22 18:19:23 +00004351
pwestin@webrtc.orgf2724972013-04-11 20:23:35 +00004352 // Get frequency of last received payload
wu@webrtc.org81f8df92014-06-05 20:34:08 +00004353 int rtp_receive_frequency = GetPlayoutFrequency();
andrew@webrtc.orgb015cbe2012-10-22 18:19:23 +00004354
turaj@webrtc.orgd5577342013-05-22 20:39:43 +00004355 // Update the least required delay.
andrew@webrtc.org510ee1b2013-09-23 23:02:24 +00004356 least_required_delay_ms_ = audio_coding_->LeastRequiredDelayMs();
turaj@webrtc.orgd5577342013-05-22 20:39:43 +00004357
turaj@webrtc.orgf1b92fd2013-12-13 21:05:07 +00004358 // |jitter_buffer_playout_timestamp_| updated in UpdatePlayoutTimestamp for
4359 // every incoming packet.
4360 uint32_t timestamp_diff_ms = (rtp_timestamp -
4361 jitter_buffer_playout_timestamp_) / (rtp_receive_frequency / 1000);
henrik.lundin@webrtc.orga5db8e32014-03-20 12:04:09 +00004362 if (!IsNewerTimestamp(rtp_timestamp, jitter_buffer_playout_timestamp_) ||
4363 timestamp_diff_ms > (2 * kVoiceEngineMaxMinPlayoutDelayMs)) {
4364 // If |jitter_buffer_playout_timestamp_| is newer than the incoming RTP
4365 // timestamp, the resulting difference is negative, but is set to zero.
4366 // This can happen when a network glitch causes a packet to arrive late,
4367 // and during long comfort noise periods with clock drift.
4368 timestamp_diff_ms = 0;
4369 }
andrew@webrtc.orgb015cbe2012-10-22 18:19:23 +00004370
pwestin@webrtc.orgf2724972013-04-11 20:23:35 +00004371 uint16_t packet_delay_ms = (rtp_timestamp - _previousTimestamp) /
4372 (rtp_receive_frequency / 1000);
andrew@webrtc.orgb015cbe2012-10-22 18:19:23 +00004373
pwestin@webrtc.orgf2724972013-04-11 20:23:35 +00004374 _previousTimestamp = rtp_timestamp;
andrew@webrtc.orgb015cbe2012-10-22 18:19:23 +00004375
pwestin@webrtc.orgf2724972013-04-11 20:23:35 +00004376 if (timestamp_diff_ms == 0) return;
andrew@webrtc.orgb015cbe2012-10-22 18:19:23 +00004377
pwestin@webrtc.orgf2724972013-04-11 20:23:35 +00004378 if (packet_delay_ms >= 10 && packet_delay_ms <= 60) {
4379 _recPacketDelayMs = packet_delay_ms;
4380 }
andrew@webrtc.orgb015cbe2012-10-22 18:19:23 +00004381
pwestin@webrtc.orgf2724972013-04-11 20:23:35 +00004382 if (_average_jitter_buffer_delay_us == 0) {
4383 _average_jitter_buffer_delay_us = timestamp_diff_ms * 1000;
4384 return;
4385 }
4386
4387 // Filter average delay value using exponential filter (alpha is
4388 // 7/8). We derive 1000 *_average_jitter_buffer_delay_us here (reduces
4389 // risk of rounding error) and compensate for it in GetDelayEstimate()
4390 // later.
4391 _average_jitter_buffer_delay_us = (_average_jitter_buffer_delay_us * 7 +
4392 1000 * timestamp_diff_ms + 500) / 8;
andrew@webrtc.orgb015cbe2012-10-22 18:19:23 +00004393}
4394
4395void
4396Channel::RegisterReceiveCodecsToRTPModule()
4397{
4398 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId,_channelId),
4399 "Channel::RegisterReceiveCodecsToRTPModule()");
4400
4401
4402 CodecInst codec;
pbos@webrtc.org54f03bc2013-04-09 10:09:10 +00004403 const uint8_t nSupportedCodecs = AudioCodingModule::NumberOfCodecs();
andrew@webrtc.orgb015cbe2012-10-22 18:19:23 +00004404
4405 for (int idx = 0; idx < nSupportedCodecs; idx++)
4406 {
4407 // Open up the RTP/RTCP receiver for all supported codecs
andrew@webrtc.org510ee1b2013-09-23 23:02:24 +00004408 if ((audio_coding_->Codec(idx, &codec) == -1) ||
wu@webrtc.org7fc75bb2013-08-15 23:38:54 +00004409 (rtp_receiver_->RegisterReceivePayload(
4410 codec.plname,
4411 codec.pltype,
4412 codec.plfreq,
4413 codec.channels,
4414 (codec.rate < 0) ? 0 : codec.rate) == -1))
andrew@webrtc.orgb015cbe2012-10-22 18:19:23 +00004415 {
4416 WEBRTC_TRACE(
4417 kTraceWarning,
4418 kTraceVoice,
4419 VoEId(_instanceId, _channelId),
4420 "Channel::RegisterReceiveCodecsToRTPModule() unable"
4421 " to register %s (%d/%d/%d/%d) to RTP/RTCP receiver",
4422 codec.plname, codec.pltype, codec.plfreq,
4423 codec.channels, codec.rate);
4424 }
4425 else
4426 {
4427 WEBRTC_TRACE(
4428 kTraceInfo,
4429 kTraceVoice,
4430 VoEId(_instanceId, _channelId),
4431 "Channel::RegisterReceiveCodecsToRTPModule() %s "
4432 "(%d/%d/%d/%d) has been added to the RTP/RTCP "
4433 "receiver",
4434 codec.plname, codec.pltype, codec.plfreq,
4435 codec.channels, codec.rate);
4436 }
4437 }
4438}
4439
turaj@webrtc.org7db52902012-12-11 02:15:12 +00004440int Channel::SetSecondarySendCodec(const CodecInst& codec,
4441 int red_payload_type) {
turaj@webrtc.org040f8002013-01-31 18:20:17 +00004442 // Sanity check for payload type.
4443 if (red_payload_type < 0 || red_payload_type > 127) {
4444 _engineStatisticsPtr->SetLastError(
4445 VE_PLTYPE_ERROR, kTraceError,
4446 "SetRedPayloadType() invalid RED payload type");
4447 return -1;
4448 }
4449
turaj@webrtc.org7db52902012-12-11 02:15:12 +00004450 if (SetRedPayloadType(red_payload_type) < 0) {
4451 _engineStatisticsPtr->SetLastError(
4452 VE_AUDIO_CODING_MODULE_ERROR, kTraceError,
4453 "SetSecondarySendCodec() Failed to register RED ACM");
4454 return -1;
4455 }
andrew@webrtc.org510ee1b2013-09-23 23:02:24 +00004456 if (audio_coding_->RegisterSecondarySendCodec(codec) < 0) {
turaj@webrtc.org7db52902012-12-11 02:15:12 +00004457 _engineStatisticsPtr->SetLastError(
4458 VE_AUDIO_CODING_MODULE_ERROR, kTraceError,
4459 "SetSecondarySendCodec() Failed to register secondary send codec in "
4460 "ACM");
4461 return -1;
4462 }
4463
4464 return 0;
4465}
4466
4467void Channel::RemoveSecondarySendCodec() {
andrew@webrtc.org510ee1b2013-09-23 23:02:24 +00004468 audio_coding_->UnregisterSecondarySendCodec();
turaj@webrtc.org7db52902012-12-11 02:15:12 +00004469}
4470
4471int Channel::GetSecondarySendCodec(CodecInst* codec) {
andrew@webrtc.org510ee1b2013-09-23 23:02:24 +00004472 if (audio_coding_->SecondarySendCodec(codec) < 0) {
turaj@webrtc.org7db52902012-12-11 02:15:12 +00004473 _engineStatisticsPtr->SetLastError(
4474 VE_AUDIO_CODING_MODULE_ERROR, kTraceError,
4475 "GetSecondarySendCodec() Failed to get secondary sent codec from ACM");
4476 return -1;
4477 }
4478 return 0;
4479}
4480
turaj@webrtc.org040f8002013-01-31 18:20:17 +00004481// Assuming this method is called with valid payload type.
turaj@webrtc.org7db52902012-12-11 02:15:12 +00004482int Channel::SetRedPayloadType(int red_payload_type) {
turaj@webrtc.org7db52902012-12-11 02:15:12 +00004483 CodecInst codec;
4484 bool found_red = false;
4485
4486 // Get default RED settings from the ACM database
4487 const int num_codecs = AudioCodingModule::NumberOfCodecs();
4488 for (int idx = 0; idx < num_codecs; idx++) {
andrew@webrtc.org510ee1b2013-09-23 23:02:24 +00004489 audio_coding_->Codec(idx, &codec);
turaj@webrtc.org7db52902012-12-11 02:15:12 +00004490 if (!STR_CASE_CMP(codec.plname, "RED")) {
4491 found_red = true;
4492 break;
4493 }
4494 }
4495
4496 if (!found_red) {
4497 _engineStatisticsPtr->SetLastError(
4498 VE_CODEC_ERROR, kTraceError,
4499 "SetRedPayloadType() RED is not supported");
4500 return -1;
4501 }
4502
turaj@webrtc.org2344ebe2013-01-31 18:34:19 +00004503 codec.pltype = red_payload_type;
andrew@webrtc.org510ee1b2013-09-23 23:02:24 +00004504 if (audio_coding_->RegisterSendCodec(codec) < 0) {
turaj@webrtc.org7db52902012-12-11 02:15:12 +00004505 _engineStatisticsPtr->SetLastError(
4506 VE_AUDIO_CODING_MODULE_ERROR, kTraceError,
4507 "SetRedPayloadType() RED registration in ACM module failed");
4508 return -1;
4509 }
4510
4511 if (_rtpRtcpModule->SetSendREDPayloadType(red_payload_type) != 0) {
4512 _engineStatisticsPtr->SetLastError(
4513 VE_RTP_RTCP_MODULE_ERROR, kTraceError,
4514 "SetRedPayloadType() RED registration in RTP/RTCP module failed");
4515 return -1;
4516 }
4517 return 0;
4518}
4519
wu@webrtc.org9a823222014-03-06 23:49:08 +00004520int Channel::SetSendRtpHeaderExtension(bool enable, RTPExtensionType type,
4521 unsigned char id) {
4522 int error = 0;
4523 _rtpRtcpModule->DeregisterSendRtpHeaderExtension(type);
4524 if (enable) {
4525 error = _rtpRtcpModule->RegisterSendRtpHeaderExtension(type, id);
4526 }
4527 return error;
4528}
minyue@webrtc.orgdd671de2014-05-28 09:52:06 +00004529
wu@webrtc.org81f8df92014-06-05 20:34:08 +00004530int32_t Channel::GetPlayoutFrequency() {
4531 int32_t playout_frequency = audio_coding_->PlayoutFrequency();
4532 CodecInst current_recive_codec;
4533 if (audio_coding_->ReceiveCodec(&current_recive_codec) == 0) {
4534 if (STR_CASE_CMP("G722", current_recive_codec.plname) == 0) {
4535 // Even though the actual sampling rate for G.722 audio is
4536 // 16,000 Hz, the RTP clock rate for the G722 payload format is
4537 // 8,000 Hz because that value was erroneously assigned in
4538 // RFC 1890 and must remain unchanged for backward compatibility.
4539 playout_frequency = 8000;
4540 } else if (STR_CASE_CMP("opus", current_recive_codec.plname) == 0) {
4541 // We are resampling Opus internally to 32,000 Hz until all our
4542 // DSP routines can operate at 48,000 Hz, but the RTP clock
4543 // rate for the Opus payload format is standardized to 48,000 Hz,
4544 // because that is the maximum supported decoding sampling rate.
4545 playout_frequency = 48000;
4546 }
4547 }
4548 return playout_frequency;
4549}
4550
pbos@webrtc.org3b89e102013-07-03 15:12:26 +00004551} // namespace voe
4552} // namespace webrtc