blob: ee170587325b479a2e460327d7dfd11b597c70fa [file] [log] [blame]
niklase@google.com470e71d2011-07-07 08:21:25 +00001/*
henrika@webrtc.org2919e952012-01-31 08:45:03 +00002 * Copyright (c) 2012 The WebRTC project authors. All Rights Reserved.
niklase@google.com470e71d2011-07-07 08:21:25 +00003 *
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.org6388c3e2013-02-12 21:42:18 +000011#include "webrtc/voice_engine/channel.h"
niklase@google.com470e71d2011-07-07 08:21:25 +000012
Henrik Lundin64dad832015-05-11 12:44:23 +020013#include <algorithm>
14
Ivo Creusenae856f22015-09-17 16:30:16 +020015#include "webrtc/base/checks.h"
pkasting@chromium.org4591fbd2014-11-20 22:28:14 +000016#include "webrtc/base/format_macros.h"
wu@webrtc.org94454b72014-06-05 20:34:08 +000017#include "webrtc/base/timeutils.h"
minyue@webrtc.orge509f942013-09-12 17:03:00 +000018#include "webrtc/common.h"
Henrik Lundin64dad832015-05-11 12:44:23 +020019#include "webrtc/config.h"
turaj@webrtc.org6388c3e2013-02-12 21:42:18 +000020#include "webrtc/modules/audio_device/include/audio_device.h"
21#include "webrtc/modules/audio_processing/include/audio_processing.h"
henrik.lundin@webrtc.orgd6692992014-03-20 12:04:09 +000022#include "webrtc/modules/interface/module_common_types.h"
wu@webrtc.org822fbd82013-08-15 23:38:54 +000023#include "webrtc/modules/rtp_rtcp/interface/receive_statistics.h"
24#include "webrtc/modules/rtp_rtcp/interface/rtp_payload_registry.h"
25#include "webrtc/modules/rtp_rtcp/interface/rtp_receiver.h"
26#include "webrtc/modules/rtp_rtcp/source/rtp_receiver_strategy.h"
turaj@webrtc.org6388c3e2013-02-12 21:42:18 +000027#include "webrtc/modules/utility/interface/audio_frame_operations.h"
28#include "webrtc/modules/utility/interface/process_thread.h"
turaj@webrtc.org6388c3e2013-02-12 21:42:18 +000029#include "webrtc/system_wrappers/interface/critical_section_wrapper.h"
30#include "webrtc/system_wrappers/interface/logging.h"
31#include "webrtc/system_wrappers/interface/trace.h"
32#include "webrtc/voice_engine/include/voe_base.h"
33#include "webrtc/voice_engine/include/voe_external_media.h"
34#include "webrtc/voice_engine/include/voe_rtp_rtcp.h"
35#include "webrtc/voice_engine/output_mixer.h"
36#include "webrtc/voice_engine/statistics.h"
37#include "webrtc/voice_engine/transmit_mixer.h"
38#include "webrtc/voice_engine/utility.h"
niklase@google.com470e71d2011-07-07 08:21:25 +000039
40#if defined(_WIN32)
41#include <Qos.h>
42#endif
43
andrew@webrtc.org50419b02012-11-14 19:07:54 +000044namespace webrtc {
45namespace voe {
niklase@google.com470e71d2011-07-07 08:21:25 +000046
sprang@webrtc.org54ae4ff2013-12-19 13:26:02 +000047// Extend the default RTCP statistics struct with max_jitter, defined as the
48// maximum jitter value seen in an RTCP report block.
49struct ChannelStatistics : public RtcpStatistics {
50 ChannelStatistics() : rtcp(), max_jitter(0) {}
51
52 RtcpStatistics rtcp;
53 uint32_t max_jitter;
54};
55
56// Statistics callback, called at each generation of a new RTCP report block.
57class StatisticsProxy : public RtcpStatisticsCallback {
58 public:
59 StatisticsProxy(uint32_t ssrc)
60 : stats_lock_(CriticalSectionWrapper::CreateCriticalSection()),
61 ssrc_(ssrc) {}
62 virtual ~StatisticsProxy() {}
63
kjellander@webrtc.org14665ff2015-03-04 12:58:35 +000064 void StatisticsUpdated(const RtcpStatistics& statistics,
65 uint32_t ssrc) override {
sprang@webrtc.org54ae4ff2013-12-19 13:26:02 +000066 if (ssrc != ssrc_)
67 return;
68
69 CriticalSectionScoped cs(stats_lock_.get());
70 stats_.rtcp = statistics;
71 if (statistics.jitter > stats_.max_jitter) {
72 stats_.max_jitter = statistics.jitter;
73 }
74 }
75
kjellander@webrtc.org14665ff2015-03-04 12:58:35 +000076 void CNameChanged(const char* cname, uint32_t ssrc) override {}
pbos@webrtc.orgce4e9a32014-12-18 13:50:16 +000077
sprang@webrtc.org54ae4ff2013-12-19 13:26:02 +000078 ChannelStatistics GetStats() {
79 CriticalSectionScoped cs(stats_lock_.get());
80 return stats_;
81 }
82
83 private:
84 // StatisticsUpdated calls are triggered from threads in the RTP module,
85 // while GetStats calls can be triggered from the public voice engine API,
86 // hence synchronization is needed.
kwiberg@webrtc.org00b8f6b2015-02-26 14:34:55 +000087 rtc::scoped_ptr<CriticalSectionWrapper> stats_lock_;
sprang@webrtc.org54ae4ff2013-12-19 13:26:02 +000088 const uint32_t ssrc_;
89 ChannelStatistics stats_;
90};
91
mflodman@webrtc.org0a7d4ee2015-02-17 12:57:14 +000092class VoERtcpObserver : public RtcpBandwidthObserver {
minyue@webrtc.orgc1a40a72014-05-28 09:52:06 +000093 public:
mflodman@webrtc.org0a7d4ee2015-02-17 12:57:14 +000094 explicit VoERtcpObserver(Channel* owner) : owner_(owner) {}
95 virtual ~VoERtcpObserver() {}
minyue@webrtc.orgc1a40a72014-05-28 09:52:06 +000096
mflodman@webrtc.org0a7d4ee2015-02-17 12:57:14 +000097 void OnReceivedEstimatedBitrate(uint32_t bitrate) override {
98 // Not used for Voice Engine.
99 }
100
kjellander@webrtc.org14665ff2015-03-04 12:58:35 +0000101 void OnReceivedRtcpReceiverReport(const ReportBlockList& report_blocks,
102 int64_t rtt,
103 int64_t now_ms) override {
mflodman@webrtc.org0a7d4ee2015-02-17 12:57:14 +0000104 // TODO(mflodman): Do we need to aggregate reports here or can we jut send
105 // what we get? I.e. do we ever get multiple reports bundled into one RTCP
106 // report for VoiceEngine?
107 if (report_blocks.empty())
108 return;
109
110 int fraction_lost_aggregate = 0;
111 int total_number_of_packets = 0;
112
113 // If receiving multiple report blocks, calculate the weighted average based
114 // on the number of packets a report refers to.
115 for (ReportBlockList::const_iterator block_it = report_blocks.begin();
116 block_it != report_blocks.end(); ++block_it) {
117 // Find the previous extended high sequence number for this remote SSRC,
118 // to calculate the number of RTP packets this report refers to. Ignore if
119 // we haven't seen this SSRC before.
120 std::map<uint32_t, uint32_t>::iterator seq_num_it =
121 extended_max_sequence_number_.find(block_it->sourceSSRC);
122 int number_of_packets = 0;
123 if (seq_num_it != extended_max_sequence_number_.end()) {
124 number_of_packets = block_it->extendedHighSeqNum - seq_num_it->second;
125 }
126 fraction_lost_aggregate += number_of_packets * block_it->fractionLost;
127 total_number_of_packets += number_of_packets;
128
129 extended_max_sequence_number_[block_it->sourceSSRC] =
130 block_it->extendedHighSeqNum;
131 }
132 int weighted_fraction_lost = 0;
133 if (total_number_of_packets > 0) {
134 weighted_fraction_lost = (fraction_lost_aggregate +
135 total_number_of_packets / 2) / total_number_of_packets;
136 }
137 owner_->OnIncomingFractionLoss(weighted_fraction_lost);
minyue@webrtc.orgc1a40a72014-05-28 09:52:06 +0000138 }
139
140 private:
141 Channel* owner_;
mflodman@webrtc.org0a7d4ee2015-02-17 12:57:14 +0000142 // Maps remote side ssrc to extended highest sequence number received.
143 std::map<uint32_t, uint32_t> extended_max_sequence_number_;
minyue@webrtc.orgc1a40a72014-05-28 09:52:06 +0000144};
145
pbos@webrtc.org6141e132013-04-09 10:09:10 +0000146int32_t
niklase@google.com470e71d2011-07-07 08:21:25 +0000147Channel::SendData(FrameType frameType,
pbos@webrtc.org6141e132013-04-09 10:09:10 +0000148 uint8_t payloadType,
149 uint32_t timeStamp,
150 const uint8_t* payloadData,
pkasting@chromium.org4591fbd2014-11-20 22:28:14 +0000151 size_t payloadSize,
niklase@google.com470e71d2011-07-07 08:21:25 +0000152 const RTPFragmentationHeader* fragmentation)
153{
154 WEBRTC_TRACE(kTraceStream, kTraceVoice, VoEId(_instanceId,_channelId),
155 "Channel::SendData(frameType=%u, payloadType=%u, timeStamp=%u,"
pkasting@chromium.org4591fbd2014-11-20 22:28:14 +0000156 " payloadSize=%" PRIuS ", fragmentation=0x%x)",
157 frameType, payloadType, timeStamp,
158 payloadSize, fragmentation);
niklase@google.com470e71d2011-07-07 08:21:25 +0000159
160 if (_includeAudioLevelIndication)
161 {
162 // Store current audio level in the RTP/RTCP module.
163 // The level will be used in combination with voice-activity state
164 // (frameType) to add an RTP header extension
andrew@webrtc.org382c0c22014-05-05 18:22:21 +0000165 _rtpRtcpModule->SetAudioLevel(rms_level_.RMS());
niklase@google.com470e71d2011-07-07 08:21:25 +0000166 }
167
168 // Push data from ACM to RTP/RTCP-module to deliver audio frame for
169 // packetization.
170 // This call will trigger Transport::SendPacket() from the RTP/RTCP module.
pwestin@webrtc.org2853dde2012-05-11 11:08:54 +0000171 if (_rtpRtcpModule->SendOutgoingData((FrameType&)frameType,
niklase@google.com470e71d2011-07-07 08:21:25 +0000172 payloadType,
173 timeStamp,
stefan@webrtc.orgddfdfed2012-07-03 13:21:22 +0000174 // Leaving the time when this frame was
175 // received from the capture device as
176 // undefined for voice for now.
177 -1,
niklase@google.com470e71d2011-07-07 08:21:25 +0000178 payloadData,
179 payloadSize,
180 fragmentation) == -1)
181 {
182 _engineStatisticsPtr->SetLastError(
183 VE_RTP_RTCP_MODULE_ERROR, kTraceWarning,
184 "Channel::SendData() failed to send data to RTP/RTCP module");
185 return -1;
186 }
187
188 _lastLocalTimeStamp = timeStamp;
189 _lastPayloadType = payloadType;
190
191 return 0;
192}
193
pbos@webrtc.org6141e132013-04-09 10:09:10 +0000194int32_t
henrik.lundin@webrtc.orge9217b42015-03-06 07:50:34 +0000195Channel::InFrameType(FrameType frame_type)
niklase@google.com470e71d2011-07-07 08:21:25 +0000196{
197 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId,_channelId),
henrik.lundin@webrtc.orge9217b42015-03-06 07:50:34 +0000198 "Channel::InFrameType(frame_type=%d)", frame_type);
niklase@google.com470e71d2011-07-07 08:21:25 +0000199
mflodman@webrtc.org9a065d12012-03-07 08:12:21 +0000200 CriticalSectionScoped cs(&_callbackCritSect);
henrik.lundin@webrtc.orge9217b42015-03-06 07:50:34 +0000201 _sendFrameType = (frame_type == kAudioFrameSpeech);
niklase@google.com470e71d2011-07-07 08:21:25 +0000202 return 0;
203}
204
pbos@webrtc.org6141e132013-04-09 10:09:10 +0000205int32_t
pbos@webrtc.org92135212013-05-14 08:31:39 +0000206Channel::OnRxVadDetected(int vadDecision)
niklase@google.com470e71d2011-07-07 08:21:25 +0000207{
mflodman@webrtc.org9a065d12012-03-07 08:12:21 +0000208 CriticalSectionScoped cs(&_callbackCritSect);
niklase@google.com470e71d2011-07-07 08:21:25 +0000209 if (_rxVadObserverPtr)
210 {
211 _rxVadObserverPtr->OnRxVad(_channelId, vadDecision);
212 }
213
214 return 0;
215}
216
pbos2d566682015-09-28 09:59:31 -0700217bool
218Channel::SendRtp(const uint8_t *data, size_t len)
niklase@google.com470e71d2011-07-07 08:21:25 +0000219{
niklase@google.com470e71d2011-07-07 08:21:25 +0000220 WEBRTC_TRACE(kTraceStream, kTraceVoice, VoEId(_instanceId,_channelId),
Peter Boströmac547a62015-09-17 23:03:57 +0200221 "Channel::SendPacket(channel=%d, len=%" PRIuS ")", len);
niklase@google.com470e71d2011-07-07 08:21:25 +0000222
wu@webrtc.orgfb648da2013-10-18 21:10:51 +0000223 CriticalSectionScoped cs(&_callbackCritSect);
224
niklase@google.com470e71d2011-07-07 08:21:25 +0000225 if (_transportPtr == NULL)
226 {
227 WEBRTC_TRACE(kTraceError, kTraceVoice, VoEId(_instanceId,_channelId),
228 "Channel::SendPacket() failed to send RTP packet due to"
229 " invalid transport object");
pbos2d566682015-09-28 09:59:31 -0700230 return false;
niklase@google.com470e71d2011-07-07 08:21:25 +0000231 }
232
pbos@webrtc.org6141e132013-04-09 10:09:10 +0000233 uint8_t* bufferToSendPtr = (uint8_t*)data;
pkasting@chromium.org4591fbd2014-11-20 22:28:14 +0000234 size_t bufferLength = len;
niklase@google.com470e71d2011-07-07 08:21:25 +0000235
pbos2d566682015-09-28 09:59:31 -0700236 if (!_transportPtr->SendRtp(bufferToSendPtr, bufferLength)) {
wu@webrtc.orgfb648da2013-10-18 21:10:51 +0000237 std::string transport_name =
238 _externalTransport ? "external transport" : "WebRtc sockets";
239 WEBRTC_TRACE(kTraceError, kTraceVoice,
240 VoEId(_instanceId,_channelId),
241 "Channel::SendPacket() RTP transmission using %s failed",
242 transport_name.c_str());
pbos2d566682015-09-28 09:59:31 -0700243 return false;
niklase@google.com470e71d2011-07-07 08:21:25 +0000244 }
pbos2d566682015-09-28 09:59:31 -0700245 return true;
niklase@google.com470e71d2011-07-07 08:21:25 +0000246}
247
pbos2d566682015-09-28 09:59:31 -0700248bool
249Channel::SendRtcp(const uint8_t *data, size_t len)
niklase@google.com470e71d2011-07-07 08:21:25 +0000250{
niklase@google.com470e71d2011-07-07 08:21:25 +0000251 WEBRTC_TRACE(kTraceStream, kTraceVoice, VoEId(_instanceId,_channelId),
pbos2d566682015-09-28 09:59:31 -0700252 "Channel::SendRtcp(len=%" PRIuS ")", len);
niklase@google.com470e71d2011-07-07 08:21:25 +0000253
wu@webrtc.orgfb648da2013-10-18 21:10:51 +0000254 CriticalSectionScoped cs(&_callbackCritSect);
255 if (_transportPtr == NULL)
niklase@google.com470e71d2011-07-07 08:21:25 +0000256 {
wu@webrtc.orgfb648da2013-10-18 21:10:51 +0000257 WEBRTC_TRACE(kTraceError, kTraceVoice,
258 VoEId(_instanceId,_channelId),
pbos2d566682015-09-28 09:59:31 -0700259 "Channel::SendRtcp() failed to send RTCP packet"
wu@webrtc.orgfb648da2013-10-18 21:10:51 +0000260 " due to invalid transport object");
pbos2d566682015-09-28 09:59:31 -0700261 return false;
niklase@google.com470e71d2011-07-07 08:21:25 +0000262 }
263
pbos@webrtc.org6141e132013-04-09 10:09:10 +0000264 uint8_t* bufferToSendPtr = (uint8_t*)data;
pkasting@chromium.org4591fbd2014-11-20 22:28:14 +0000265 size_t bufferLength = len;
niklase@google.com470e71d2011-07-07 08:21:25 +0000266
pbos2d566682015-09-28 09:59:31 -0700267 int n = _transportPtr->SendRtcp(bufferToSendPtr, bufferLength);
wu@webrtc.orgfb648da2013-10-18 21:10:51 +0000268 if (n < 0) {
269 std::string transport_name =
270 _externalTransport ? "external transport" : "WebRtc sockets";
271 WEBRTC_TRACE(kTraceInfo, kTraceVoice,
272 VoEId(_instanceId,_channelId),
pbos2d566682015-09-28 09:59:31 -0700273 "Channel::SendRtcp() transmission using %s failed",
wu@webrtc.orgfb648da2013-10-18 21:10:51 +0000274 transport_name.c_str());
pbos2d566682015-09-28 09:59:31 -0700275 return false;
niklase@google.com470e71d2011-07-07 08:21:25 +0000276 }
pbos2d566682015-09-28 09:59:31 -0700277 return true;
niklase@google.com470e71d2011-07-07 08:21:25 +0000278}
279
Peter Boströmac547a62015-09-17 23:03:57 +0200280void Channel::OnPlayTelephoneEvent(uint8_t event,
281 uint16_t lengthMs,
282 uint8_t volume) {
niklase@google.com470e71d2011-07-07 08:21:25 +0000283 WEBRTC_TRACE(kTraceStream, kTraceVoice, VoEId(_instanceId,_channelId),
Peter Boströmac547a62015-09-17 23:03:57 +0200284 "Channel::OnPlayTelephoneEvent(event=%u, lengthMs=%u,"
285 " volume=%u)", event, lengthMs, volume);
niklase@google.com470e71d2011-07-07 08:21:25 +0000286
287 if (!_playOutbandDtmfEvent || (event > 15))
288 {
289 // Ignore callback since feedback is disabled or event is not a
290 // Dtmf tone event.
291 return;
292 }
293
294 assert(_outputMixerPtr != NULL);
295
296 // Start playing out the Dtmf tone (if playout is enabled).
297 // Reduce length of tone with 80ms to the reduce risk of echo.
298 _outputMixerPtr->PlayDtmfTone(event, lengthMs - 80, volume);
299}
300
301void
Peter Boströmac547a62015-09-17 23:03:57 +0200302Channel::OnIncomingSSRCChanged(uint32_t ssrc)
niklase@google.com470e71d2011-07-07 08:21:25 +0000303{
304 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId,_channelId),
Peter Boströmac547a62015-09-17 23:03:57 +0200305 "Channel::OnIncomingSSRCChanged(SSRC=%d)", ssrc);
niklase@google.com470e71d2011-07-07 08:21:25 +0000306
dwkang@webrtc.orgb295a3f2013-08-29 07:34:12 +0000307 // Update ssrc so that NTP for AV sync can be updated.
308 _rtpRtcpModule->SetRemoteSSRC(ssrc);
niklase@google.com470e71d2011-07-07 08:21:25 +0000309}
310
Peter Boströmac547a62015-09-17 23:03:57 +0200311void Channel::OnIncomingCSRCChanged(uint32_t CSRC, bool added) {
312 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId, _channelId),
313 "Channel::OnIncomingCSRCChanged(CSRC=%d, added=%d)", CSRC,
314 added);
niklase@google.com470e71d2011-07-07 08:21:25 +0000315}
316
Peter Boströmac547a62015-09-17 23:03:57 +0200317int32_t Channel::OnInitializeDecoder(
pbos@webrtc.org92135212013-05-14 08:31:39 +0000318 int8_t payloadType,
leozwang@webrtc.org813e4b02012-03-01 18:34:25 +0000319 const char payloadName[RTP_PAYLOAD_NAME_SIZE],
pbos@webrtc.org92135212013-05-14 08:31:39 +0000320 int frequency,
321 uint8_t channels,
Peter Boströmac547a62015-09-17 23:03:57 +0200322 uint32_t rate) {
niklase@google.com470e71d2011-07-07 08:21:25 +0000323 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId,_channelId),
Peter Boströmac547a62015-09-17 23:03:57 +0200324 "Channel::OnInitializeDecoder(payloadType=%d, "
niklase@google.com470e71d2011-07-07 08:21:25 +0000325 "payloadName=%s, frequency=%u, channels=%u, rate=%u)",
Peter Boströmac547a62015-09-17 23:03:57 +0200326 payloadType, payloadName, frequency, channels, rate);
niklase@google.com470e71d2011-07-07 08:21:25 +0000327
henrika@webrtc.orgf75901f2012-01-16 08:45:42 +0000328 CodecInst receiveCodec = {0};
329 CodecInst dummyCodec = {0};
niklase@google.com470e71d2011-07-07 08:21:25 +0000330
331 receiveCodec.pltype = payloadType;
niklase@google.com470e71d2011-07-07 08:21:25 +0000332 receiveCodec.plfreq = frequency;
333 receiveCodec.channels = channels;
334 receiveCodec.rate = rate;
henrika@webrtc.orgf75901f2012-01-16 08:45:42 +0000335 strncpy(receiveCodec.plname, payloadName, RTP_PAYLOAD_NAME_SIZE - 1);
andrew@webrtc.orgae1a58b2013-01-22 04:44:30 +0000336
andrew@webrtc.orgeb524d92013-09-23 23:02:24 +0000337 audio_coding_->Codec(payloadName, &dummyCodec, frequency, channels);
niklase@google.com470e71d2011-07-07 08:21:25 +0000338 receiveCodec.pacsize = dummyCodec.pacsize;
339
340 // Register the new codec to the ACM
andrew@webrtc.orgeb524d92013-09-23 23:02:24 +0000341 if (audio_coding_->RegisterReceiveCodec(receiveCodec) == -1)
niklase@google.com470e71d2011-07-07 08:21:25 +0000342 {
343 WEBRTC_TRACE(kTraceWarning, kTraceVoice,
andrew@webrtc.orgceb148c2011-08-23 17:53:54 +0000344 VoEId(_instanceId, _channelId),
niklase@google.com470e71d2011-07-07 08:21:25 +0000345 "Channel::OnInitializeDecoder() invalid codec ("
346 "pt=%d, name=%s) received - 1", payloadType, payloadName);
347 _engineStatisticsPtr->SetLastError(VE_AUDIO_CODING_MODULE_ERROR);
348 return -1;
349 }
350
351 return 0;
352}
353
pbos@webrtc.org6141e132013-04-09 10:09:10 +0000354int32_t
355Channel::OnReceivedPayloadData(const uint8_t* payloadData,
pkasting@chromium.org4591fbd2014-11-20 22:28:14 +0000356 size_t payloadSize,
niklase@google.com470e71d2011-07-07 08:21:25 +0000357 const WebRtcRTPHeader* rtpHeader)
358{
359 WEBRTC_TRACE(kTraceStream, kTraceVoice, VoEId(_instanceId,_channelId),
pkasting@chromium.org4591fbd2014-11-20 22:28:14 +0000360 "Channel::OnReceivedPayloadData(payloadSize=%" PRIuS ","
niklase@google.com470e71d2011-07-07 08:21:25 +0000361 " payloadType=%u, audioChannel=%u)",
362 payloadSize,
363 rtpHeader->header.payloadType,
364 rtpHeader->type.Audio.channel);
365
henrika@webrtc.org944cbeb2014-03-18 10:32:33 +0000366 if (!channel_state_.Get().playing)
niklase@google.com470e71d2011-07-07 08:21:25 +0000367 {
368 // Avoid inserting into NetEQ when we are not playing. Count the
369 // packet as discarded.
370 WEBRTC_TRACE(kTraceStream, kTraceVoice,
371 VoEId(_instanceId, _channelId),
372 "received packet is discarded since playing is not"
373 " activated");
374 _numberOfDiscardedPackets++;
375 return 0;
376 }
377
378 // Push the incoming payload (parsed and ready for decoding) into the ACM
andrew@webrtc.orgeb524d92013-09-23 23:02:24 +0000379 if (audio_coding_->IncomingPacket(payloadData,
380 payloadSize,
381 *rtpHeader) != 0)
niklase@google.com470e71d2011-07-07 08:21:25 +0000382 {
383 _engineStatisticsPtr->SetLastError(
384 VE_AUDIO_CODING_MODULE_ERROR, kTraceWarning,
385 "Channel::OnReceivedPayloadData() unable to push data to the ACM");
386 return -1;
387 }
388
pwestin@webrtc.orgd30859e2013-06-06 21:09:01 +0000389 // Update the packet delay.
niklase@google.com470e71d2011-07-07 08:21:25 +0000390 UpdatePacketDelay(rtpHeader->header.timestamp,
391 rtpHeader->header.sequenceNumber);
pwestin@webrtc.orgd30859e2013-06-06 21:09:01 +0000392
pkasting@chromium.org16825b12015-01-12 21:51:21 +0000393 int64_t round_trip_time = 0;
wu@webrtc.org822fbd82013-08-15 23:38:54 +0000394 _rtpRtcpModule->RTT(rtp_receiver_->SSRC(), &round_trip_time,
395 NULL, NULL, NULL);
pwestin@webrtc.orgd30859e2013-06-06 21:09:01 +0000396
andrew@webrtc.orgeb524d92013-09-23 23:02:24 +0000397 std::vector<uint16_t> nack_list = audio_coding_->GetNackList(
wu@webrtc.org822fbd82013-08-15 23:38:54 +0000398 round_trip_time);
399 if (!nack_list.empty()) {
400 // Can't use nack_list.data() since it's not supported by all
401 // compilers.
402 ResendPackets(&(nack_list[0]), static_cast<int>(nack_list.size()));
pwestin@webrtc.orgd30859e2013-06-06 21:09:01 +0000403 }
niklase@google.com470e71d2011-07-07 08:21:25 +0000404 return 0;
405}
406
stefan@webrtc.org7bb8f022013-09-06 13:40:11 +0000407bool Channel::OnRecoveredPacket(const uint8_t* rtp_packet,
pkasting@chromium.org4591fbd2014-11-20 22:28:14 +0000408 size_t rtp_packet_length) {
stefan@webrtc.org7bb8f022013-09-06 13:40:11 +0000409 RTPHeader header;
410 if (!rtp_header_parser_->Parse(rtp_packet, rtp_packet_length, &header)) {
411 WEBRTC_TRACE(kTraceDebug, webrtc::kTraceVoice, _channelId,
412 "IncomingPacket invalid RTP header");
413 return false;
414 }
415 header.payload_type_frequency =
416 rtp_payload_registry_->GetPayloadTypeFrequency(header.payloadType);
417 if (header.payload_type_frequency < 0)
418 return false;
419 return ReceivePacket(rtp_packet, rtp_packet_length, header, false);
420}
421
minyuel0f4b3732015-08-31 16:04:32 +0200422int32_t Channel::GetAudioFrame(int32_t id, AudioFrame* audioFrame)
niklase@google.com470e71d2011-07-07 08:21:25 +0000423{
Ivo Creusenae856f22015-09-17 16:30:16 +0200424 if (event_log_) {
425 unsigned int ssrc;
426 RTC_CHECK_EQ(GetLocalSSRC(ssrc), 0);
427 event_log_->LogAudioPlayout(ssrc);
428 }
niklase@google.com470e71d2011-07-07 08:21:25 +0000429 // Get 10ms raw PCM data from the ACM (mixer limits output frequency)
minyuel0f4b3732015-08-31 16:04:32 +0200430 if (audio_coding_->PlayoutData10Ms(audioFrame->sample_rate_hz_,
431 audioFrame) == -1)
niklase@google.com470e71d2011-07-07 08:21:25 +0000432 {
433 WEBRTC_TRACE(kTraceError, kTraceVoice,
434 VoEId(_instanceId,_channelId),
435 "Channel::GetAudioFrame() PlayoutData10Ms() failed!");
andrew@webrtc.org7859e102012-01-13 00:30:11 +0000436 // In all likelihood, the audio in this frame is garbage. We return an
437 // error so that the audio mixer module doesn't add it to the mix. As
438 // a result, it won't be played out and the actions skipped here are
439 // irrelevant.
440 return -1;
niklase@google.com470e71d2011-07-07 08:21:25 +0000441 }
442
443 if (_RxVadDetection)
444 {
minyuel0f4b3732015-08-31 16:04:32 +0200445 UpdateRxVadDetection(*audioFrame);
niklase@google.com470e71d2011-07-07 08:21:25 +0000446 }
447
448 // Convert module ID to internal VoE channel ID
minyuel0f4b3732015-08-31 16:04:32 +0200449 audioFrame->id_ = VoEChannelId(audioFrame->id_);
niklase@google.com470e71d2011-07-07 08:21:25 +0000450 // Store speech type for dead-or-alive detection
minyuel0f4b3732015-08-31 16:04:32 +0200451 _outputSpeechType = audioFrame->speech_type_;
niklase@google.com470e71d2011-07-07 08:21:25 +0000452
henrika@webrtc.org944cbeb2014-03-18 10:32:33 +0000453 ChannelState::State state = channel_state_.Get();
454
455 if (state.rx_apm_is_enabled) {
minyuel0f4b3732015-08-31 16:04:32 +0200456 int err = rx_audioproc_->ProcessStream(audioFrame);
andrew@webrtc.org60730cf2014-01-07 17:45:09 +0000457 if (err) {
458 LOG(LS_ERROR) << "ProcessStream() error: " << err;
459 assert(false);
460 }
niklase@google.com470e71d2011-07-07 08:21:25 +0000461 }
462
wu@webrtc.org63420662013-10-17 18:28:55 +0000463 float output_gain = 1.0f;
464 float left_pan = 1.0f;
465 float right_pan = 1.0f;
niklase@google.com470e71d2011-07-07 08:21:25 +0000466 {
wu@webrtc.org63420662013-10-17 18:28:55 +0000467 CriticalSectionScoped cs(&volume_settings_critsect_);
468 output_gain = _outputGain;
469 left_pan = _panLeft;
470 right_pan= _panRight;
471 }
472
473 // Output volume scaling
474 if (output_gain < 0.99f || output_gain > 1.01f)
475 {
minyuel0f4b3732015-08-31 16:04:32 +0200476 AudioFrameOperations::ScaleWithSat(output_gain, *audioFrame);
niklase@google.com470e71d2011-07-07 08:21:25 +0000477 }
478
479 // Scale left and/or right channel(s) if stereo and master balance is
480 // active
481
wu@webrtc.org63420662013-10-17 18:28:55 +0000482 if (left_pan != 1.0f || right_pan != 1.0f)
niklase@google.com470e71d2011-07-07 08:21:25 +0000483 {
minyuel0f4b3732015-08-31 16:04:32 +0200484 if (audioFrame->num_channels_ == 1)
niklase@google.com470e71d2011-07-07 08:21:25 +0000485 {
486 // Emulate stereo mode since panning is active.
487 // The mono signal is copied to both left and right channels here.
minyuel0f4b3732015-08-31 16:04:32 +0200488 AudioFrameOperations::MonoToStereo(audioFrame);
niklase@google.com470e71d2011-07-07 08:21:25 +0000489 }
490 // For true stereo mode (when we are receiving a stereo signal), no
491 // action is needed.
492
493 // Do the panning operation (the audio frame contains stereo at this
494 // stage)
minyuel0f4b3732015-08-31 16:04:32 +0200495 AudioFrameOperations::Scale(left_pan, right_pan, *audioFrame);
niklase@google.com470e71d2011-07-07 08:21:25 +0000496 }
497
498 // Mix decoded PCM output with file if file mixing is enabled
henrika@webrtc.org944cbeb2014-03-18 10:32:33 +0000499 if (state.output_file_playing)
niklase@google.com470e71d2011-07-07 08:21:25 +0000500 {
minyuel0f4b3732015-08-31 16:04:32 +0200501 MixAudioWithFile(*audioFrame, audioFrame->sample_rate_hz_);
niklase@google.com470e71d2011-07-07 08:21:25 +0000502 }
503
niklase@google.com470e71d2011-07-07 08:21:25 +0000504 // External media
505 if (_outputExternalMedia)
506 {
mflodman@webrtc.org9a065d12012-03-07 08:12:21 +0000507 CriticalSectionScoped cs(&_callbackCritSect);
minyuel0f4b3732015-08-31 16:04:32 +0200508 const bool isStereo = (audioFrame->num_channels_ == 2);
niklase@google.com470e71d2011-07-07 08:21:25 +0000509 if (_outputExternalMediaCallbackPtr)
510 {
511 _outputExternalMediaCallbackPtr->Process(
512 _channelId,
513 kPlaybackPerChannel,
minyuel0f4b3732015-08-31 16:04:32 +0200514 (int16_t*)audioFrame->data_,
515 audioFrame->samples_per_channel_,
516 audioFrame->sample_rate_hz_,
niklase@google.com470e71d2011-07-07 08:21:25 +0000517 isStereo);
518 }
519 }
520
521 // Record playout if enabled
522 {
mflodman@webrtc.org9a065d12012-03-07 08:12:21 +0000523 CriticalSectionScoped cs(&_fileCritSect);
niklase@google.com470e71d2011-07-07 08:21:25 +0000524
525 if (_outputFileRecording && _outputFileRecorderPtr)
526 {
minyuel0f4b3732015-08-31 16:04:32 +0200527 _outputFileRecorderPtr->RecordAudioToFile(*audioFrame);
niklase@google.com470e71d2011-07-07 08:21:25 +0000528 }
529 }
530
531 // Measure audio level (0-9)
minyuel0f4b3732015-08-31 16:04:32 +0200532 _outputAudioLevel.ComputeLevel(*audioFrame);
niklase@google.com470e71d2011-07-07 08:21:25 +0000533
minyuel0f4b3732015-08-31 16:04:32 +0200534 if (capture_start_rtp_time_stamp_ < 0 && audioFrame->timestamp_ != 0) {
wu@webrtc.org94454b72014-06-05 20:34:08 +0000535 // The first frame with a valid rtp timestamp.
minyuel0f4b3732015-08-31 16:04:32 +0200536 capture_start_rtp_time_stamp_ = audioFrame->timestamp_;
wu@webrtc.org94454b72014-06-05 20:34:08 +0000537 }
538
539 if (capture_start_rtp_time_stamp_ >= 0) {
540 // audioFrame.timestamp_ should be valid from now on.
541
542 // Compute elapsed time.
543 int64_t unwrap_timestamp =
minyuel0f4b3732015-08-31 16:04:32 +0200544 rtp_ts_wraparound_handler_->Unwrap(audioFrame->timestamp_);
545 audioFrame->elapsed_time_ms_ =
wu@webrtc.org94454b72014-06-05 20:34:08 +0000546 (unwrap_timestamp - capture_start_rtp_time_stamp_) /
547 (GetPlayoutFrequency() / 1000);
548
stefan@webrtc.org8e24d872014-09-02 18:58:24 +0000549 {
wu@webrtc.orgcb711f72014-05-19 17:39:11 +0000550 CriticalSectionScoped lock(ts_stats_lock_.get());
stefan@webrtc.org8e24d872014-09-02 18:58:24 +0000551 // Compute ntp time.
minyuel0f4b3732015-08-31 16:04:32 +0200552 audioFrame->ntp_time_ms_ = ntp_estimator_.Estimate(
553 audioFrame->timestamp_);
stefan@webrtc.org8e24d872014-09-02 18:58:24 +0000554 // |ntp_time_ms_| won't be valid until at least 2 RTCP SRs are received.
minyuel0f4b3732015-08-31 16:04:32 +0200555 if (audioFrame->ntp_time_ms_ > 0) {
stefan@webrtc.org8e24d872014-09-02 18:58:24 +0000556 // Compute |capture_start_ntp_time_ms_| so that
557 // |capture_start_ntp_time_ms_| + |elapsed_time_ms_| == |ntp_time_ms_|
558 capture_start_ntp_time_ms_ =
minyuel0f4b3732015-08-31 16:04:32 +0200559 audioFrame->ntp_time_ms_ - audioFrame->elapsed_time_ms_;
stefan@webrtc.org8e24d872014-09-02 18:58:24 +0000560 }
wu@webrtc.orgcb711f72014-05-19 17:39:11 +0000561 }
562 }
563
niklase@google.com470e71d2011-07-07 08:21:25 +0000564 return 0;
565}
566
pbos@webrtc.org6141e132013-04-09 10:09:10 +0000567int32_t
minyuel0f4b3732015-08-31 16:04:32 +0200568Channel::NeededFrequency(int32_t id) const
niklase@google.com470e71d2011-07-07 08:21:25 +0000569{
570 WEBRTC_TRACE(kTraceStream, kTraceVoice, VoEId(_instanceId,_channelId),
571 "Channel::NeededFrequency(id=%d)", id);
572
573 int highestNeeded = 0;
574
575 // Determine highest needed receive frequency
andrew@webrtc.orgeb524d92013-09-23 23:02:24 +0000576 int32_t receiveFrequency = audio_coding_->ReceiveFrequency();
niklase@google.com470e71d2011-07-07 08:21:25 +0000577
578 // Return the bigger of playout and receive frequency in the ACM.
andrew@webrtc.orgeb524d92013-09-23 23:02:24 +0000579 if (audio_coding_->PlayoutFrequency() > receiveFrequency)
niklase@google.com470e71d2011-07-07 08:21:25 +0000580 {
andrew@webrtc.orgeb524d92013-09-23 23:02:24 +0000581 highestNeeded = audio_coding_->PlayoutFrequency();
niklase@google.com470e71d2011-07-07 08:21:25 +0000582 }
583 else
584 {
585 highestNeeded = receiveFrequency;
586 }
587
588 // Special case, if we're playing a file on the playout side
589 // we take that frequency into consideration as well
590 // This is not needed on sending side, since the codec will
591 // limit the spectrum anyway.
henrika@webrtc.org944cbeb2014-03-18 10:32:33 +0000592 if (channel_state_.Get().output_file_playing)
niklase@google.com470e71d2011-07-07 08:21:25 +0000593 {
mflodman@webrtc.org9a065d12012-03-07 08:12:21 +0000594 CriticalSectionScoped cs(&_fileCritSect);
henrika@webrtc.org944cbeb2014-03-18 10:32:33 +0000595 if (_outputFilePlayerPtr)
niklase@google.com470e71d2011-07-07 08:21:25 +0000596 {
597 if(_outputFilePlayerPtr->Frequency()>highestNeeded)
598 {
599 highestNeeded=_outputFilePlayerPtr->Frequency();
600 }
601 }
602 }
603
604 return(highestNeeded);
605}
606
ivocb04965c2015-09-09 00:09:43 -0700607int32_t Channel::CreateChannel(Channel*& channel,
608 int32_t channelId,
609 uint32_t instanceId,
610 RtcEventLog* const event_log,
611 const Config& config) {
niklase@google.com470e71d2011-07-07 08:21:25 +0000612 WEBRTC_TRACE(kTraceMemory, kTraceVoice, VoEId(instanceId,channelId),
613 "Channel::CreateChannel(channelId=%d, instanceId=%d)",
614 channelId, instanceId);
615
ivocb04965c2015-09-09 00:09:43 -0700616 channel = new Channel(channelId, instanceId, event_log, config);
niklase@google.com470e71d2011-07-07 08:21:25 +0000617 if (channel == NULL)
618 {
619 WEBRTC_TRACE(kTraceMemory, kTraceVoice,
620 VoEId(instanceId,channelId),
621 "Channel::CreateChannel() unable to allocate memory for"
622 " channel");
623 return -1;
624 }
625 return 0;
626}
627
628void
pbos@webrtc.org92135212013-05-14 08:31:39 +0000629Channel::PlayNotification(int32_t id, uint32_t durationMs)
niklase@google.com470e71d2011-07-07 08:21:25 +0000630{
631 WEBRTC_TRACE(kTraceStream, kTraceVoice, VoEId(_instanceId,_channelId),
632 "Channel::PlayNotification(id=%d, durationMs=%d)",
633 id, durationMs);
634
635 // Not implement yet
636}
637
638void
pbos@webrtc.org92135212013-05-14 08:31:39 +0000639Channel::RecordNotification(int32_t id, uint32_t durationMs)
niklase@google.com470e71d2011-07-07 08:21:25 +0000640{
641 WEBRTC_TRACE(kTraceStream, kTraceVoice, VoEId(_instanceId,_channelId),
642 "Channel::RecordNotification(id=%d, durationMs=%d)",
643 id, durationMs);
644
645 // Not implement yet
646}
647
648void
pbos@webrtc.org92135212013-05-14 08:31:39 +0000649Channel::PlayFileEnded(int32_t id)
niklase@google.com470e71d2011-07-07 08:21:25 +0000650{
651 WEBRTC_TRACE(kTraceStream, kTraceVoice, VoEId(_instanceId,_channelId),
652 "Channel::PlayFileEnded(id=%d)", id);
653
654 if (id == _inputFilePlayerId)
655 {
henrika@webrtc.org944cbeb2014-03-18 10:32:33 +0000656 channel_state_.SetInputFilePlaying(false);
niklase@google.com470e71d2011-07-07 08:21:25 +0000657 WEBRTC_TRACE(kTraceStateInfo, kTraceVoice,
658 VoEId(_instanceId,_channelId),
659 "Channel::PlayFileEnded() => input file player module is"
660 " shutdown");
661 }
662 else if (id == _outputFilePlayerId)
663 {
henrika@webrtc.org944cbeb2014-03-18 10:32:33 +0000664 channel_state_.SetOutputFilePlaying(false);
niklase@google.com470e71d2011-07-07 08:21:25 +0000665 WEBRTC_TRACE(kTraceStateInfo, kTraceVoice,
666 VoEId(_instanceId,_channelId),
667 "Channel::PlayFileEnded() => output file player module is"
668 " shutdown");
669 }
670}
671
672void
pbos@webrtc.org92135212013-05-14 08:31:39 +0000673Channel::RecordFileEnded(int32_t id)
niklase@google.com470e71d2011-07-07 08:21:25 +0000674{
675 WEBRTC_TRACE(kTraceStream, kTraceVoice, VoEId(_instanceId,_channelId),
676 "Channel::RecordFileEnded(id=%d)", id);
677
678 assert(id == _outputFileRecorderId);
679
mflodman@webrtc.org9a065d12012-03-07 08:12:21 +0000680 CriticalSectionScoped cs(&_fileCritSect);
niklase@google.com470e71d2011-07-07 08:21:25 +0000681
682 _outputFileRecording = false;
683 WEBRTC_TRACE(kTraceStateInfo, kTraceVoice,
684 VoEId(_instanceId,_channelId),
685 "Channel::RecordFileEnded() => output file recorder module is"
686 " shutdown");
687}
688
pbos@webrtc.org92135212013-05-14 08:31:39 +0000689Channel::Channel(int32_t channelId,
minyue@webrtc.orge509f942013-09-12 17:03:00 +0000690 uint32_t instanceId,
ivocb04965c2015-09-09 00:09:43 -0700691 RtcEventLog* const event_log,
692 const Config& config)
693 : _fileCritSect(*CriticalSectionWrapper::CreateCriticalSection()),
niklase@google.com470e71d2011-07-07 08:21:25 +0000694 _callbackCritSect(*CriticalSectionWrapper::CreateCriticalSection()),
wu@webrtc.org63420662013-10-17 18:28:55 +0000695 volume_settings_critsect_(*CriticalSectionWrapper::CreateCriticalSection()),
niklase@google.com470e71d2011-07-07 08:21:25 +0000696 _instanceId(instanceId),
xians@google.com22963ab2011-08-03 12:40:23 +0000697 _channelId(channelId),
Ivo Creusenae856f22015-09-17 16:30:16 +0200698 event_log_(event_log),
stefan@webrtc.orga5cb98c2013-05-29 12:12:51 +0000699 rtp_header_parser_(RtpHeaderParser::Create()),
wu@webrtc.org822fbd82013-08-15 23:38:54 +0000700 rtp_payload_registry_(
andresp@webrtc.orgdc80bae2014-04-08 11:06:12 +0000701 new RTPPayloadRegistry(RTPPayloadStrategy::CreateStrategy(true))),
ivocb04965c2015-09-09 00:09:43 -0700702 rtp_receive_statistics_(
703 ReceiveStatistics::Create(Clock::GetRealTimeClock())),
704 rtp_receiver_(
Peter Boströmac547a62015-09-17 23:03:57 +0200705 RtpReceiver::CreateAudioReceiver(Clock::GetRealTimeClock(),
ivocb04965c2015-09-09 00:09:43 -0700706 this,
707 this,
708 this,
709 rtp_payload_registry_.get())),
wu@webrtc.org822fbd82013-08-15 23:38:54 +0000710 telephone_event_handler_(rtp_receiver_->GetTelephoneEventHandler()),
niklase@google.com470e71d2011-07-07 08:21:25 +0000711 _outputAudioLevel(),
niklase@google.com470e71d2011-07-07 08:21:25 +0000712 _externalTransport(false),
niklase@google.com470e71d2011-07-07 08:21:25 +0000713 _inputFilePlayerPtr(NULL),
714 _outputFilePlayerPtr(NULL),
715 _outputFileRecorderPtr(NULL),
716 // Avoid conflict with other channels by adding 1024 - 1026,
717 // won't use as much as 1024 channels.
718 _inputFilePlayerId(VoEModuleId(instanceId, channelId) + 1024),
719 _outputFilePlayerId(VoEModuleId(instanceId, channelId) + 1025),
720 _outputFileRecorderId(VoEModuleId(instanceId, channelId) + 1026),
niklase@google.com470e71d2011-07-07 08:21:25 +0000721 _outputFileRecording(false),
xians@google.com22963ab2011-08-03 12:40:23 +0000722 _inbandDtmfQueue(VoEModuleId(instanceId, channelId)),
723 _inbandDtmfGenerator(VoEModuleId(instanceId, channelId)),
xians@google.com22963ab2011-08-03 12:40:23 +0000724 _outputExternalMedia(false),
niklase@google.com470e71d2011-07-07 08:21:25 +0000725 _inputExternalMediaCallbackPtr(NULL),
726 _outputExternalMediaCallbackPtr(NULL),
ivocb04965c2015-09-09 00:09:43 -0700727 _timeStamp(0), // This is just an offset, RTP module will add it's own
728 // random offset
xians@google.com22963ab2011-08-03 12:40:23 +0000729 _sendTelephoneEventPayloadType(106),
stefan@webrtc.org8e24d872014-09-02 18:58:24 +0000730 ntp_estimator_(Clock::GetRealTimeClock()),
turaj@webrtc.org167b6df2013-12-13 21:05:07 +0000731 jitter_buffer_playout_timestamp_(0),
pwestin@webrtc.org1de01352013-04-11 20:23:35 +0000732 playout_timestamp_rtp_(0),
733 playout_timestamp_rtcp_(0),
sprang@webrtc.org54ae4ff2013-12-19 13:26:02 +0000734 playout_delay_ms_(0),
xians@google.com22963ab2011-08-03 12:40:23 +0000735 _numberOfDiscardedPackets(0),
xians@webrtc.org09e8c472013-07-31 16:30:19 +0000736 send_sequence_number_(0),
wu@webrtc.orgcb711f72014-05-19 17:39:11 +0000737 ts_stats_lock_(CriticalSectionWrapper::CreateCriticalSection()),
wu@webrtc.org94454b72014-06-05 20:34:08 +0000738 rtp_ts_wraparound_handler_(new rtc::TimestampWrapAroundHandler()),
739 capture_start_rtp_time_stamp_(-1),
wu@webrtc.orgcb711f72014-05-19 17:39:11 +0000740 capture_start_ntp_time_ms_(-1),
xians@google.com22963ab2011-08-03 12:40:23 +0000741 _engineStatisticsPtr(NULL),
henrika@webrtc.org2919e952012-01-31 08:45:03 +0000742 _outputMixerPtr(NULL),
743 _transmitMixerPtr(NULL),
xians@google.com22963ab2011-08-03 12:40:23 +0000744 _moduleProcessThreadPtr(NULL),
745 _audioDeviceModulePtr(NULL),
746 _voiceEngineObserverPtr(NULL),
747 _callbackCritSectPtr(NULL),
748 _transportPtr(NULL),
xians@google.com22963ab2011-08-03 12:40:23 +0000749 _rxVadObserverPtr(NULL),
750 _oldVadDecision(-1),
751 _sendFrameType(0),
roosa@google.com1b60ceb2012-12-12 23:00:29 +0000752 _externalMixing(false),
xians@google.com22963ab2011-08-03 12:40:23 +0000753 _mixFileWithMicrophone(false),
niklase@google.com470e71d2011-07-07 08:21:25 +0000754 _mute(false),
755 _panLeft(1.0f),
756 _panRight(1.0f),
757 _outputGain(1.0f),
758 _playOutbandDtmfEvent(false),
759 _playInbandDtmfEvent(false),
niklase@google.com470e71d2011-07-07 08:21:25 +0000760 _lastLocalTimeStamp(0),
761 _lastPayloadType(0),
xians@google.com22963ab2011-08-03 12:40:23 +0000762 _includeAudioLevelIndication(false),
niklase@google.com470e71d2011-07-07 08:21:25 +0000763 _outputSpeechType(AudioFrame::kNormalSpeech),
deadbeef74375882015-08-13 12:09:10 -0700764 video_sync_lock_(CriticalSectionWrapper::CreateCriticalSection()),
pwestin@webrtc.org1de01352013-04-11 20:23:35 +0000765 _average_jitter_buffer_delay_us(0),
niklase@google.com470e71d2011-07-07 08:21:25 +0000766 _previousTimestamp(0),
767 _recPacketDelayMs(20),
768 _RxVadDetection(false),
niklase@google.com470e71d2011-07-07 08:21:25 +0000769 _rxAgcIsEnabled(false),
stefan@webrtc.org7bb8f022013-09-06 13:40:11 +0000770 _rxNsIsEnabled(false),
minyue@webrtc.orgc1a40a72014-05-28 09:52:06 +0000771 restored_packet_in_use_(false),
mflodman@webrtc.org0a7d4ee2015-02-17 12:57:14 +0000772 rtcp_observer_(new VoERtcpObserver(this)),
Minyue2013aec2015-05-13 14:14:42 +0200773 network_predictor_(new NetworkPredictor(Clock::GetRealTimeClock())),
774 assoc_send_channel_lock_(CriticalSectionWrapper::CreateCriticalSection()),
ivocb04965c2015-09-09 00:09:43 -0700775 associate_send_channel_(ChannelOwner(nullptr)) {
niklase@google.com470e71d2011-07-07 08:21:25 +0000776 WEBRTC_TRACE(kTraceMemory, kTraceVoice, VoEId(_instanceId,_channelId),
777 "Channel::Channel() - ctor");
Henrik Lundin64dad832015-05-11 12:44:23 +0200778 AudioCodingModule::Config acm_config;
779 acm_config.id = VoEModuleId(instanceId, channelId);
780 if (config.Get<NetEqCapacityConfig>().enabled) {
781 // Clamping the buffer capacity at 20 packets. While going lower will
782 // probably work, it makes little sense.
783 acm_config.neteq_config.max_packets_in_buffer =
784 std::max(20, config.Get<NetEqCapacityConfig>().capacity);
785 }
Henrik Lundin5263b3c2015-06-01 10:29:41 +0200786 acm_config.neteq_config.enable_fast_accelerate =
787 config.Get<NetEqFastAccelerate>().enabled;
Henrik Lundin64dad832015-05-11 12:44:23 +0200788 audio_coding_.reset(AudioCodingModule::Create(acm_config));
789
niklase@google.com470e71d2011-07-07 08:21:25 +0000790 _inbandDtmfQueue.ResetDtmf();
791 _inbandDtmfGenerator.Init();
792 _outputAudioLevel.Clear();
793
pwestin@webrtc.org2853dde2012-05-11 11:08:54 +0000794 RtpRtcp::Configuration configuration;
pwestin@webrtc.org2853dde2012-05-11 11:08:54 +0000795 configuration.audio = true;
pwestin@webrtc.org2853dde2012-05-11 11:08:54 +0000796 configuration.outgoing_transport = this;
pwestin@webrtc.org2853dde2012-05-11 11:08:54 +0000797 configuration.audio_messages = this;
wu@webrtc.org822fbd82013-08-15 23:38:54 +0000798 configuration.receive_statistics = rtp_receive_statistics_.get();
mflodman@webrtc.org0a7d4ee2015-02-17 12:57:14 +0000799 configuration.bandwidth_callback = rtcp_observer_.get();
pwestin@webrtc.org2853dde2012-05-11 11:08:54 +0000800
801 _rtpRtcpModule.reset(RtpRtcp::CreateRtpRtcp(configuration));
sprang@webrtc.org54ae4ff2013-12-19 13:26:02 +0000802
803 statistics_proxy_.reset(new StatisticsProxy(_rtpRtcpModule->SSRC()));
804 rtp_receive_statistics_->RegisterRtcpStatisticsCallback(
805 statistics_proxy_.get());
aluebs@webrtc.orgf927fd62014-04-16 11:58:18 +0000806
807 Config audioproc_config;
808 audioproc_config.Set<ExperimentalAgc>(new ExperimentalAgc(false));
809 rx_audioproc_.reset(AudioProcessing::Create(audioproc_config));
niklase@google.com470e71d2011-07-07 08:21:25 +0000810}
811
812Channel::~Channel()
813{
sprang@webrtc.org54ae4ff2013-12-19 13:26:02 +0000814 rtp_receive_statistics_->RegisterRtcpStatisticsCallback(NULL);
niklase@google.com470e71d2011-07-07 08:21:25 +0000815 WEBRTC_TRACE(kTraceMemory, kTraceVoice, VoEId(_instanceId,_channelId),
816 "Channel::~Channel() - dtor");
817
818 if (_outputExternalMedia)
819 {
820 DeRegisterExternalMediaProcessing(kPlaybackPerChannel);
821 }
henrika@webrtc.org944cbeb2014-03-18 10:32:33 +0000822 if (channel_state_.Get().input_external_media)
niklase@google.com470e71d2011-07-07 08:21:25 +0000823 {
824 DeRegisterExternalMediaProcessing(kRecordingPerChannel);
825 }
826 StopSend();
niklase@google.com470e71d2011-07-07 08:21:25 +0000827 StopPlayout();
828
829 {
mflodman@webrtc.org9a065d12012-03-07 08:12:21 +0000830 CriticalSectionScoped cs(&_fileCritSect);
niklase@google.com470e71d2011-07-07 08:21:25 +0000831 if (_inputFilePlayerPtr)
832 {
833 _inputFilePlayerPtr->RegisterModuleFileCallback(NULL);
834 _inputFilePlayerPtr->StopPlayingFile();
835 FilePlayer::DestroyFilePlayer(_inputFilePlayerPtr);
836 _inputFilePlayerPtr = NULL;
837 }
838 if (_outputFilePlayerPtr)
839 {
840 _outputFilePlayerPtr->RegisterModuleFileCallback(NULL);
841 _outputFilePlayerPtr->StopPlayingFile();
842 FilePlayer::DestroyFilePlayer(_outputFilePlayerPtr);
843 _outputFilePlayerPtr = NULL;
844 }
845 if (_outputFileRecorderPtr)
846 {
847 _outputFileRecorderPtr->RegisterModuleFileCallback(NULL);
848 _outputFileRecorderPtr->StopRecording();
849 FileRecorder::DestroyFileRecorder(_outputFileRecorderPtr);
850 _outputFileRecorderPtr = NULL;
851 }
852 }
853
854 // The order to safely shutdown modules in a channel is:
855 // 1. De-register callbacks in modules
856 // 2. De-register modules in process thread
857 // 3. Destroy modules
andrew@webrtc.orgeb524d92013-09-23 23:02:24 +0000858 if (audio_coding_->RegisterTransportCallback(NULL) == -1)
niklase@google.com470e71d2011-07-07 08:21:25 +0000859 {
860 WEBRTC_TRACE(kTraceWarning, kTraceVoice,
861 VoEId(_instanceId,_channelId),
862 "~Channel() failed to de-register transport callback"
863 " (Audio coding module)");
864 }
andrew@webrtc.orgeb524d92013-09-23 23:02:24 +0000865 if (audio_coding_->RegisterVADCallback(NULL) == -1)
niklase@google.com470e71d2011-07-07 08:21:25 +0000866 {
867 WEBRTC_TRACE(kTraceWarning, kTraceVoice,
868 VoEId(_instanceId,_channelId),
869 "~Channel() failed to de-register VAD callback"
870 " (Audio coding module)");
871 }
niklase@google.com470e71d2011-07-07 08:21:25 +0000872 // De-register modules in process thread
tommi@webrtc.org3985f012015-02-27 13:36:34 +0000873 _moduleProcessThreadPtr->DeRegisterModule(_rtpRtcpModule.get());
874
niklase@google.com470e71d2011-07-07 08:21:25 +0000875 // End of modules shutdown
876
877 // Delete other objects
niklase@google.com470e71d2011-07-07 08:21:25 +0000878 delete &_callbackCritSect;
niklase@google.com470e71d2011-07-07 08:21:25 +0000879 delete &_fileCritSect;
wu@webrtc.org63420662013-10-17 18:28:55 +0000880 delete &volume_settings_critsect_;
niklase@google.com470e71d2011-07-07 08:21:25 +0000881}
882
pbos@webrtc.org6141e132013-04-09 10:09:10 +0000883int32_t
niklase@google.com470e71d2011-07-07 08:21:25 +0000884Channel::Init()
885{
886 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId,_channelId),
887 "Channel::Init()");
888
henrika@webrtc.org944cbeb2014-03-18 10:32:33 +0000889 channel_state_.Reset();
890
niklase@google.com470e71d2011-07-07 08:21:25 +0000891 // --- Initial sanity
892
893 if ((_engineStatisticsPtr == NULL) ||
894 (_moduleProcessThreadPtr == NULL))
895 {
896 WEBRTC_TRACE(kTraceError, kTraceVoice,
897 VoEId(_instanceId,_channelId),
898 "Channel::Init() must call SetEngineInformation() first");
899 return -1;
900 }
901
902 // --- Add modules to process thread (for periodic schedulation)
903
tommi@webrtc.org3985f012015-02-27 13:36:34 +0000904 _moduleProcessThreadPtr->RegisterModule(_rtpRtcpModule.get());
905
pwestin@webrtc.orgc450a192012-01-04 15:00:12 +0000906 // --- ACM initialization
niklase@google.com470e71d2011-07-07 08:21:25 +0000907
henrik.lundin061b79a2015-09-18 01:29:11 -0700908 if (audio_coding_->InitializeReceiver() == -1) {
niklase@google.com470e71d2011-07-07 08:21:25 +0000909 _engineStatisticsPtr->SetLastError(
910 VE_AUDIO_CODING_MODULE_ERROR, kTraceError,
911 "Channel::Init() unable to initialize the ACM - 1");
912 return -1;
913 }
914
915 // --- RTP/RTCP module initialization
916
917 // Ensure that RTCP is enabled by default for the created channel.
918 // Note that, the module will keep generating RTCP until it is explicitly
919 // disabled by the user.
920 // After StopListen (when no sockets exists), RTCP packets will no longer
921 // be transmitted since the Transport object will then be invalid.
wu@webrtc.org822fbd82013-08-15 23:38:54 +0000922 telephone_event_handler_->SetTelephoneEventForwardToDecoder(true);
923 // RTCP is enabled by default.
pbos@webrtc.orgd16e8392014-12-19 13:49:55 +0000924 _rtpRtcpModule->SetRTCPStatus(kRtcpCompound);
925 // --- Register all permanent callbacks
niklase@google.com470e71d2011-07-07 08:21:25 +0000926 const bool fail =
andrew@webrtc.orgeb524d92013-09-23 23:02:24 +0000927 (audio_coding_->RegisterTransportCallback(this) == -1) ||
928 (audio_coding_->RegisterVADCallback(this) == -1);
niklase@google.com470e71d2011-07-07 08:21:25 +0000929
930 if (fail)
931 {
932 _engineStatisticsPtr->SetLastError(
933 VE_CANNOT_INIT_CHANNEL, kTraceError,
934 "Channel::Init() callbacks not registered");
935 return -1;
936 }
937
938 // --- Register all supported codecs to the receiving side of the
939 // RTP/RTCP module
940
941 CodecInst codec;
pbos@webrtc.org6141e132013-04-09 10:09:10 +0000942 const uint8_t nSupportedCodecs = AudioCodingModule::NumberOfCodecs();
niklase@google.com470e71d2011-07-07 08:21:25 +0000943
944 for (int idx = 0; idx < nSupportedCodecs; idx++)
945 {
946 // Open up the RTP/RTCP receiver for all supported codecs
andrew@webrtc.orgeb524d92013-09-23 23:02:24 +0000947 if ((audio_coding_->Codec(idx, &codec) == -1) ||
wu@webrtc.org822fbd82013-08-15 23:38:54 +0000948 (rtp_receiver_->RegisterReceivePayload(
949 codec.plname,
950 codec.pltype,
951 codec.plfreq,
952 codec.channels,
953 (codec.rate < 0) ? 0 : codec.rate) == -1))
niklase@google.com470e71d2011-07-07 08:21:25 +0000954 {
955 WEBRTC_TRACE(kTraceWarning, kTraceVoice,
956 VoEId(_instanceId,_channelId),
957 "Channel::Init() unable to register %s (%d/%d/%d/%d) "
958 "to RTP/RTCP receiver",
959 codec.plname, codec.pltype, codec.plfreq,
960 codec.channels, codec.rate);
961 }
962 else
963 {
964 WEBRTC_TRACE(kTraceInfo, kTraceVoice,
965 VoEId(_instanceId,_channelId),
966 "Channel::Init() %s (%d/%d/%d/%d) has been added to "
967 "the RTP/RTCP receiver",
968 codec.plname, codec.pltype, codec.plfreq,
969 codec.channels, codec.rate);
970 }
971
972 // Ensure that PCMU is used as default codec on the sending side
tina.legrand@webrtc.org45175852012-06-01 09:27:35 +0000973 if (!STR_CASE_CMP(codec.plname, "PCMU") && (codec.channels == 1))
niklase@google.com470e71d2011-07-07 08:21:25 +0000974 {
975 SetSendCodec(codec);
976 }
977
978 // Register default PT for outband 'telephone-event'
979 if (!STR_CASE_CMP(codec.plname, "telephone-event"))
980 {
pwestin@webrtc.org2853dde2012-05-11 11:08:54 +0000981 if ((_rtpRtcpModule->RegisterSendPayload(codec) == -1) ||
andrew@webrtc.orgeb524d92013-09-23 23:02:24 +0000982 (audio_coding_->RegisterReceiveCodec(codec) == -1))
niklase@google.com470e71d2011-07-07 08:21:25 +0000983 {
984 WEBRTC_TRACE(kTraceWarning, kTraceVoice,
985 VoEId(_instanceId,_channelId),
986 "Channel::Init() failed to register outband "
987 "'telephone-event' (%d/%d) correctly",
988 codec.pltype, codec.plfreq);
989 }
990 }
991
992 if (!STR_CASE_CMP(codec.plname, "CN"))
993 {
andrew@webrtc.orgeb524d92013-09-23 23:02:24 +0000994 if ((audio_coding_->RegisterSendCodec(codec) == -1) ||
995 (audio_coding_->RegisterReceiveCodec(codec) == -1) ||
pwestin@webrtc.org2853dde2012-05-11 11:08:54 +0000996 (_rtpRtcpModule->RegisterSendPayload(codec) == -1))
niklase@google.com470e71d2011-07-07 08:21:25 +0000997 {
998 WEBRTC_TRACE(kTraceWarning, kTraceVoice,
999 VoEId(_instanceId,_channelId),
1000 "Channel::Init() failed to register CN (%d/%d) "
1001 "correctly - 1",
1002 codec.pltype, codec.plfreq);
1003 }
1004 }
1005#ifdef WEBRTC_CODEC_RED
1006 // Register RED to the receiving side of the ACM.
1007 // We will not receive an OnInitializeDecoder() callback for RED.
1008 if (!STR_CASE_CMP(codec.plname, "RED"))
1009 {
andrew@webrtc.orgeb524d92013-09-23 23:02:24 +00001010 if (audio_coding_->RegisterReceiveCodec(codec) == -1)
niklase@google.com470e71d2011-07-07 08:21:25 +00001011 {
1012 WEBRTC_TRACE(kTraceWarning, kTraceVoice,
1013 VoEId(_instanceId,_channelId),
1014 "Channel::Init() failed to register RED (%d/%d) "
1015 "correctly",
1016 codec.pltype, codec.plfreq);
1017 }
1018 }
1019#endif
1020 }
pwestin@webrtc.org684f0572013-03-13 23:20:57 +00001021
andrew@webrtc.org6c264cc2013-10-04 17:54:09 +00001022 if (rx_audioproc_->noise_suppression()->set_level(kDefaultNsMode) != 0) {
1023 LOG_FERR1(LS_ERROR, noise_suppression()->set_level, kDefaultNsMode);
1024 return -1;
niklase@google.com470e71d2011-07-07 08:21:25 +00001025 }
andrew@webrtc.org6c264cc2013-10-04 17:54:09 +00001026 if (rx_audioproc_->gain_control()->set_mode(kDefaultRxAgcMode) != 0) {
1027 LOG_FERR1(LS_ERROR, gain_control()->set_mode, kDefaultRxAgcMode);
1028 return -1;
niklase@google.com470e71d2011-07-07 08:21:25 +00001029 }
1030
1031 return 0;
1032}
1033
pbos@webrtc.org6141e132013-04-09 10:09:10 +00001034int32_t
niklase@google.com470e71d2011-07-07 08:21:25 +00001035Channel::SetEngineInformation(Statistics& engineStatistics,
1036 OutputMixer& outputMixer,
1037 voe::TransmitMixer& transmitMixer,
1038 ProcessThread& moduleProcessThread,
1039 AudioDeviceModule& audioDeviceModule,
1040 VoiceEngineObserver* voiceEngineObserver,
1041 CriticalSectionWrapper* callbackCritSect)
1042{
1043 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId,_channelId),
1044 "Channel::SetEngineInformation()");
1045 _engineStatisticsPtr = &engineStatistics;
1046 _outputMixerPtr = &outputMixer;
1047 _transmitMixerPtr = &transmitMixer,
1048 _moduleProcessThreadPtr = &moduleProcessThread;
1049 _audioDeviceModulePtr = &audioDeviceModule;
1050 _voiceEngineObserverPtr = voiceEngineObserver;
1051 _callbackCritSectPtr = callbackCritSect;
1052 return 0;
1053}
1054
pbos@webrtc.org6141e132013-04-09 10:09:10 +00001055int32_t
niklase@google.com470e71d2011-07-07 08:21:25 +00001056Channel::UpdateLocalTimeStamp()
1057{
1058
Peter Kastingb7e50542015-06-11 12:55:50 -07001059 _timeStamp += static_cast<uint32_t>(_audioFrame.samples_per_channel_);
niklase@google.com470e71d2011-07-07 08:21:25 +00001060 return 0;
1061}
1062
pbos@webrtc.org6141e132013-04-09 10:09:10 +00001063int32_t
niklase@google.com470e71d2011-07-07 08:21:25 +00001064Channel::StartPlayout()
1065{
1066 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId,_channelId),
1067 "Channel::StartPlayout()");
henrika@webrtc.org944cbeb2014-03-18 10:32:33 +00001068 if (channel_state_.Get().playing)
niklase@google.com470e71d2011-07-07 08:21:25 +00001069 {
1070 return 0;
1071 }
roosa@google.com1b60ceb2012-12-12 23:00:29 +00001072
1073 if (!_externalMixing) {
1074 // Add participant as candidates for mixing.
1075 if (_outputMixerPtr->SetMixabilityStatus(*this, true) != 0)
1076 {
1077 _engineStatisticsPtr->SetLastError(
1078 VE_AUDIO_CONF_MIX_MODULE_ERROR, kTraceError,
1079 "StartPlayout() failed to add participant to mixer");
1080 return -1;
1081 }
niklase@google.com470e71d2011-07-07 08:21:25 +00001082 }
1083
henrika@webrtc.org944cbeb2014-03-18 10:32:33 +00001084 channel_state_.SetPlaying(true);
braveyao@webrtc.orgab129902012-06-04 03:26:39 +00001085 if (RegisterFilePlayingToMixer() != 0)
1086 return -1;
1087
niklase@google.com470e71d2011-07-07 08:21:25 +00001088 return 0;
1089}
1090
pbos@webrtc.org6141e132013-04-09 10:09:10 +00001091int32_t
niklase@google.com470e71d2011-07-07 08:21:25 +00001092Channel::StopPlayout()
1093{
1094 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId,_channelId),
1095 "Channel::StopPlayout()");
henrika@webrtc.org944cbeb2014-03-18 10:32:33 +00001096 if (!channel_state_.Get().playing)
niklase@google.com470e71d2011-07-07 08:21:25 +00001097 {
1098 return 0;
1099 }
roosa@google.com1b60ceb2012-12-12 23:00:29 +00001100
1101 if (!_externalMixing) {
1102 // Remove participant as candidates for mixing
1103 if (_outputMixerPtr->SetMixabilityStatus(*this, false) != 0)
1104 {
1105 _engineStatisticsPtr->SetLastError(
1106 VE_AUDIO_CONF_MIX_MODULE_ERROR, kTraceError,
1107 "StopPlayout() failed to remove participant from mixer");
1108 return -1;
1109 }
niklase@google.com470e71d2011-07-07 08:21:25 +00001110 }
1111
henrika@webrtc.org944cbeb2014-03-18 10:32:33 +00001112 channel_state_.SetPlaying(false);
niklase@google.com470e71d2011-07-07 08:21:25 +00001113 _outputAudioLevel.Clear();
1114
1115 return 0;
1116}
1117
pbos@webrtc.org6141e132013-04-09 10:09:10 +00001118int32_t
niklase@google.com470e71d2011-07-07 08:21:25 +00001119Channel::StartSend()
1120{
1121 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId,_channelId),
1122 "Channel::StartSend()");
xians@webrtc.org09e8c472013-07-31 16:30:19 +00001123 // Resume the previous sequence number which was reset by StopSend().
henrika@webrtc.org944cbeb2014-03-18 10:32:33 +00001124 // This needs to be done before |sending| is set to true.
xians@webrtc.org09e8c472013-07-31 16:30:19 +00001125 if (send_sequence_number_)
1126 SetInitSequenceNumber(send_sequence_number_);
1127
henrika@webrtc.org944cbeb2014-03-18 10:32:33 +00001128 if (channel_state_.Get().sending)
niklase@google.com470e71d2011-07-07 08:21:25 +00001129 {
henrika@webrtc.org944cbeb2014-03-18 10:32:33 +00001130 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +00001131 }
henrika@webrtc.org944cbeb2014-03-18 10:32:33 +00001132 channel_state_.SetSending(true);
xians@webrtc.orge07247a2011-11-28 16:31:28 +00001133
pwestin@webrtc.org2853dde2012-05-11 11:08:54 +00001134 if (_rtpRtcpModule->SetSendingStatus(true) != 0)
niklase@google.com470e71d2011-07-07 08:21:25 +00001135 {
1136 _engineStatisticsPtr->SetLastError(
1137 VE_RTP_RTCP_MODULE_ERROR, kTraceError,
1138 "StartSend() RTP/RTCP failed to start sending");
mflodman@webrtc.org9a065d12012-03-07 08:12:21 +00001139 CriticalSectionScoped cs(&_callbackCritSect);
henrika@webrtc.org944cbeb2014-03-18 10:32:33 +00001140 channel_state_.SetSending(false);
niklase@google.com470e71d2011-07-07 08:21:25 +00001141 return -1;
1142 }
xians@webrtc.orge07247a2011-11-28 16:31:28 +00001143
niklase@google.com470e71d2011-07-07 08:21:25 +00001144 return 0;
1145}
1146
pbos@webrtc.org6141e132013-04-09 10:09:10 +00001147int32_t
niklase@google.com470e71d2011-07-07 08:21:25 +00001148Channel::StopSend()
1149{
1150 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId,_channelId),
1151 "Channel::StopSend()");
henrika@webrtc.org944cbeb2014-03-18 10:32:33 +00001152 if (!channel_state_.Get().sending)
niklase@google.com470e71d2011-07-07 08:21:25 +00001153 {
henrika@webrtc.org944cbeb2014-03-18 10:32:33 +00001154 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +00001155 }
henrika@webrtc.org944cbeb2014-03-18 10:32:33 +00001156 channel_state_.SetSending(false);
xians@webrtc.orge07247a2011-11-28 16:31:28 +00001157
xians@webrtc.org09e8c472013-07-31 16:30:19 +00001158 // Store the sequence number to be able to pick up the same sequence for
1159 // the next StartSend(). This is needed for restarting device, otherwise
1160 // it might cause libSRTP to complain about packets being replayed.
1161 // TODO(xians): Remove this workaround after RtpRtcpModule's refactoring
1162 // CL is landed. See issue
1163 // https://code.google.com/p/webrtc/issues/detail?id=2111 .
1164 send_sequence_number_ = _rtpRtcpModule->SequenceNumber();
1165
niklase@google.com470e71d2011-07-07 08:21:25 +00001166 // Reset sending SSRC and sequence number and triggers direct transmission
1167 // of RTCP BYE
pbosd4362982015-07-07 08:32:48 -07001168 if (_rtpRtcpModule->SetSendingStatus(false) == -1)
niklase@google.com470e71d2011-07-07 08:21:25 +00001169 {
1170 _engineStatisticsPtr->SetLastError(
1171 VE_RTP_RTCP_MODULE_ERROR, kTraceWarning,
1172 "StartSend() RTP/RTCP failed to stop sending");
1173 }
1174
niklase@google.com470e71d2011-07-07 08:21:25 +00001175 return 0;
1176}
1177
pbos@webrtc.org6141e132013-04-09 10:09:10 +00001178int32_t
niklase@google.com470e71d2011-07-07 08:21:25 +00001179Channel::StartReceiving()
1180{
1181 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId,_channelId),
1182 "Channel::StartReceiving()");
henrika@webrtc.org944cbeb2014-03-18 10:32:33 +00001183 if (channel_state_.Get().receiving)
niklase@google.com470e71d2011-07-07 08:21:25 +00001184 {
1185 return 0;
1186 }
henrika@webrtc.org944cbeb2014-03-18 10:32:33 +00001187 channel_state_.SetReceiving(true);
niklase@google.com470e71d2011-07-07 08:21:25 +00001188 _numberOfDiscardedPackets = 0;
1189 return 0;
1190}
1191
pbos@webrtc.org6141e132013-04-09 10:09:10 +00001192int32_t
niklase@google.com470e71d2011-07-07 08:21:25 +00001193Channel::StopReceiving()
1194{
1195 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId,_channelId),
1196 "Channel::StopReceiving()");
henrika@webrtc.org944cbeb2014-03-18 10:32:33 +00001197 if (!channel_state_.Get().receiving)
niklase@google.com470e71d2011-07-07 08:21:25 +00001198 {
1199 return 0;
1200 }
pwestin@webrtc.org684f0572013-03-13 23:20:57 +00001201
henrika@webrtc.org944cbeb2014-03-18 10:32:33 +00001202 channel_state_.SetReceiving(false);
niklase@google.com470e71d2011-07-07 08:21:25 +00001203 return 0;
1204}
1205
pbos@webrtc.org6141e132013-04-09 10:09:10 +00001206int32_t
niklase@google.com470e71d2011-07-07 08:21:25 +00001207Channel::RegisterVoiceEngineObserver(VoiceEngineObserver& observer)
1208{
1209 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId,_channelId),
1210 "Channel::RegisterVoiceEngineObserver()");
mflodman@webrtc.org9a065d12012-03-07 08:12:21 +00001211 CriticalSectionScoped cs(&_callbackCritSect);
niklase@google.com470e71d2011-07-07 08:21:25 +00001212
1213 if (_voiceEngineObserverPtr)
1214 {
1215 _engineStatisticsPtr->SetLastError(
1216 VE_INVALID_OPERATION, kTraceError,
1217 "RegisterVoiceEngineObserver() observer already enabled");
1218 return -1;
1219 }
1220 _voiceEngineObserverPtr = &observer;
1221 return 0;
1222}
1223
pbos@webrtc.org6141e132013-04-09 10:09:10 +00001224int32_t
niklase@google.com470e71d2011-07-07 08:21:25 +00001225Channel::DeRegisterVoiceEngineObserver()
1226{
1227 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId,_channelId),
1228 "Channel::DeRegisterVoiceEngineObserver()");
mflodman@webrtc.org9a065d12012-03-07 08:12:21 +00001229 CriticalSectionScoped cs(&_callbackCritSect);
niklase@google.com470e71d2011-07-07 08:21:25 +00001230
1231 if (!_voiceEngineObserverPtr)
1232 {
1233 _engineStatisticsPtr->SetLastError(
1234 VE_INVALID_OPERATION, kTraceWarning,
1235 "DeRegisterVoiceEngineObserver() observer already disabled");
1236 return 0;
1237 }
1238 _voiceEngineObserverPtr = NULL;
1239 return 0;
1240}
1241
pbos@webrtc.org6141e132013-04-09 10:09:10 +00001242int32_t
niklase@google.com470e71d2011-07-07 08:21:25 +00001243Channel::GetSendCodec(CodecInst& codec)
1244{
andrew@webrtc.orgeb524d92013-09-23 23:02:24 +00001245 return (audio_coding_->SendCodec(&codec));
niklase@google.com470e71d2011-07-07 08:21:25 +00001246}
1247
pbos@webrtc.org6141e132013-04-09 10:09:10 +00001248int32_t
niklase@google.com470e71d2011-07-07 08:21:25 +00001249Channel::GetRecCodec(CodecInst& codec)
1250{
andrew@webrtc.orgeb524d92013-09-23 23:02:24 +00001251 return (audio_coding_->ReceiveCodec(&codec));
niklase@google.com470e71d2011-07-07 08:21:25 +00001252}
1253
pbos@webrtc.org6141e132013-04-09 10:09:10 +00001254int32_t
niklase@google.com470e71d2011-07-07 08:21:25 +00001255Channel::SetSendCodec(const CodecInst& codec)
1256{
1257 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId,_channelId),
1258 "Channel::SetSendCodec()");
1259
andrew@webrtc.orgeb524d92013-09-23 23:02:24 +00001260 if (audio_coding_->RegisterSendCodec(codec) != 0)
niklase@google.com470e71d2011-07-07 08:21:25 +00001261 {
1262 WEBRTC_TRACE(kTraceError, kTraceVoice, VoEId(_instanceId,_channelId),
1263 "SetSendCodec() failed to register codec to ACM");
1264 return -1;
1265 }
1266
pwestin@webrtc.org2853dde2012-05-11 11:08:54 +00001267 if (_rtpRtcpModule->RegisterSendPayload(codec) != 0)
niklase@google.com470e71d2011-07-07 08:21:25 +00001268 {
pwestin@webrtc.org2853dde2012-05-11 11:08:54 +00001269 _rtpRtcpModule->DeRegisterSendPayload(codec.pltype);
1270 if (_rtpRtcpModule->RegisterSendPayload(codec) != 0)
niklase@google.com470e71d2011-07-07 08:21:25 +00001271 {
1272 WEBRTC_TRACE(
1273 kTraceError, kTraceVoice, VoEId(_instanceId,_channelId),
1274 "SetSendCodec() failed to register codec to"
1275 " RTP/RTCP module");
1276 return -1;
1277 }
1278 }
1279
pwestin@webrtc.org2853dde2012-05-11 11:08:54 +00001280 if (_rtpRtcpModule->SetAudioPacketSize(codec.pacsize) != 0)
niklase@google.com470e71d2011-07-07 08:21:25 +00001281 {
1282 WEBRTC_TRACE(kTraceError, kTraceVoice, VoEId(_instanceId,_channelId),
1283 "SetSendCodec() failed to set audio packet size");
1284 return -1;
1285 }
1286
1287 return 0;
1288}
1289
Ivo Creusenadf89b72015-04-29 16:03:33 +02001290void Channel::SetBitRate(int bitrate_bps) {
1291 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId, _channelId),
1292 "Channel::SetBitRate(bitrate_bps=%d)", bitrate_bps);
1293 audio_coding_->SetBitRate(bitrate_bps);
1294}
1295
mflodman@webrtc.org0a7d4ee2015-02-17 12:57:14 +00001296void Channel::OnIncomingFractionLoss(int fraction_lost) {
minyue@webrtc.org74aaf292014-07-16 21:28:26 +00001297 network_predictor_->UpdatePacketLossRate(fraction_lost);
mflodman@webrtc.org0a7d4ee2015-02-17 12:57:14 +00001298 uint8_t average_fraction_loss = network_predictor_->GetLossRate();
1299
minyue@webrtc.orgc1a40a72014-05-28 09:52:06 +00001300 // Normalizes rate to 0 - 100.
mflodman@webrtc.org0a7d4ee2015-02-17 12:57:14 +00001301 if (audio_coding_->SetPacketLossRate(
1302 100 * average_fraction_loss / 255) != 0) {
minyue@webrtc.orgc1a40a72014-05-28 09:52:06 +00001303 assert(false); // This should not happen.
1304 }
1305}
1306
pbos@webrtc.org6141e132013-04-09 10:09:10 +00001307int32_t
niklase@google.com470e71d2011-07-07 08:21:25 +00001308Channel::SetVADStatus(bool enableVAD, ACMVADMode mode, bool disableDTX)
1309{
1310 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId,_channelId),
1311 "Channel::SetVADStatus(mode=%d)", mode);
henrik.lundin@webrtc.org664ccb72015-01-28 14:49:05 +00001312 assert(!(disableDTX && enableVAD)); // disableDTX mode is deprecated.
niklase@google.com470e71d2011-07-07 08:21:25 +00001313 // To disable VAD, DTX must be disabled too
1314 disableDTX = ((enableVAD == false) ? true : disableDTX);
andrew@webrtc.orgeb524d92013-09-23 23:02:24 +00001315 if (audio_coding_->SetVAD(!disableDTX, enableVAD, mode) != 0)
niklase@google.com470e71d2011-07-07 08:21:25 +00001316 {
1317 _engineStatisticsPtr->SetLastError(
1318 VE_AUDIO_CODING_MODULE_ERROR, kTraceError,
1319 "SetVADStatus() failed to set VAD");
1320 return -1;
1321 }
1322 return 0;
1323}
1324
pbos@webrtc.org6141e132013-04-09 10:09:10 +00001325int32_t
niklase@google.com470e71d2011-07-07 08:21:25 +00001326Channel::GetVADStatus(bool& enabledVAD, ACMVADMode& mode, bool& disabledDTX)
1327{
andrew@webrtc.orgeb524d92013-09-23 23:02:24 +00001328 if (audio_coding_->VAD(&disabledDTX, &enabledVAD, &mode) != 0)
niklase@google.com470e71d2011-07-07 08:21:25 +00001329 {
1330 _engineStatisticsPtr->SetLastError(
1331 VE_AUDIO_CODING_MODULE_ERROR, kTraceError,
1332 "GetVADStatus() failed to get VAD status");
1333 return -1;
1334 }
1335 disabledDTX = !disabledDTX;
1336 return 0;
1337}
1338
pbos@webrtc.org6141e132013-04-09 10:09:10 +00001339int32_t
niklase@google.com470e71d2011-07-07 08:21:25 +00001340Channel::SetRecPayloadType(const CodecInst& codec)
1341{
1342 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId,_channelId),
1343 "Channel::SetRecPayloadType()");
1344
henrika@webrtc.org944cbeb2014-03-18 10:32:33 +00001345 if (channel_state_.Get().playing)
niklase@google.com470e71d2011-07-07 08:21:25 +00001346 {
1347 _engineStatisticsPtr->SetLastError(
1348 VE_ALREADY_PLAYING, kTraceError,
1349 "SetRecPayloadType() unable to set PT while playing");
1350 return -1;
1351 }
henrika@webrtc.org944cbeb2014-03-18 10:32:33 +00001352 if (channel_state_.Get().receiving)
niklase@google.com470e71d2011-07-07 08:21:25 +00001353 {
1354 _engineStatisticsPtr->SetLastError(
1355 VE_ALREADY_LISTENING, kTraceError,
1356 "SetRecPayloadType() unable to set PT while listening");
1357 return -1;
1358 }
1359
1360 if (codec.pltype == -1)
1361 {
1362 // De-register the selected codec (RTP/RTCP module and ACM)
1363
pbos@webrtc.org6141e132013-04-09 10:09:10 +00001364 int8_t pltype(-1);
niklase@google.com470e71d2011-07-07 08:21:25 +00001365 CodecInst rxCodec = codec;
1366
1367 // Get payload type for the given codec
wu@webrtc.org822fbd82013-08-15 23:38:54 +00001368 rtp_payload_registry_->ReceivePayloadType(
1369 rxCodec.plname,
1370 rxCodec.plfreq,
1371 rxCodec.channels,
1372 (rxCodec.rate < 0) ? 0 : rxCodec.rate,
1373 &pltype);
niklase@google.com470e71d2011-07-07 08:21:25 +00001374 rxCodec.pltype = pltype;
1375
wu@webrtc.org822fbd82013-08-15 23:38:54 +00001376 if (rtp_receiver_->DeRegisterReceivePayload(pltype) != 0)
niklase@google.com470e71d2011-07-07 08:21:25 +00001377 {
1378 _engineStatisticsPtr->SetLastError(
1379 VE_RTP_RTCP_MODULE_ERROR,
1380 kTraceError,
1381 "SetRecPayloadType() RTP/RTCP-module deregistration "
1382 "failed");
1383 return -1;
1384 }
andrew@webrtc.orgeb524d92013-09-23 23:02:24 +00001385 if (audio_coding_->UnregisterReceiveCodec(rxCodec.pltype) != 0)
niklase@google.com470e71d2011-07-07 08:21:25 +00001386 {
1387 _engineStatisticsPtr->SetLastError(
1388 VE_AUDIO_CODING_MODULE_ERROR, kTraceError,
1389 "SetRecPayloadType() ACM deregistration failed - 1");
1390 return -1;
1391 }
1392 return 0;
1393 }
1394
wu@webrtc.org822fbd82013-08-15 23:38:54 +00001395 if (rtp_receiver_->RegisterReceivePayload(
1396 codec.plname,
1397 codec.pltype,
1398 codec.plfreq,
1399 codec.channels,
1400 (codec.rate < 0) ? 0 : codec.rate) != 0)
niklase@google.com470e71d2011-07-07 08:21:25 +00001401 {
1402 // First attempt to register failed => de-register and try again
wu@webrtc.org822fbd82013-08-15 23:38:54 +00001403 rtp_receiver_->DeRegisterReceivePayload(codec.pltype);
1404 if (rtp_receiver_->RegisterReceivePayload(
1405 codec.plname,
1406 codec.pltype,
1407 codec.plfreq,
1408 codec.channels,
1409 (codec.rate < 0) ? 0 : codec.rate) != 0)
niklase@google.com470e71d2011-07-07 08:21:25 +00001410 {
1411 _engineStatisticsPtr->SetLastError(
1412 VE_RTP_RTCP_MODULE_ERROR, kTraceError,
1413 "SetRecPayloadType() RTP/RTCP-module registration failed");
1414 return -1;
1415 }
1416 }
andrew@webrtc.orgeb524d92013-09-23 23:02:24 +00001417 if (audio_coding_->RegisterReceiveCodec(codec) != 0)
niklase@google.com470e71d2011-07-07 08:21:25 +00001418 {
andrew@webrtc.orgeb524d92013-09-23 23:02:24 +00001419 audio_coding_->UnregisterReceiveCodec(codec.pltype);
1420 if (audio_coding_->RegisterReceiveCodec(codec) != 0)
niklase@google.com470e71d2011-07-07 08:21:25 +00001421 {
1422 _engineStatisticsPtr->SetLastError(
1423 VE_AUDIO_CODING_MODULE_ERROR, kTraceError,
1424 "SetRecPayloadType() ACM registration failed - 1");
1425 return -1;
1426 }
1427 }
1428 return 0;
1429}
1430
pbos@webrtc.org6141e132013-04-09 10:09:10 +00001431int32_t
niklase@google.com470e71d2011-07-07 08:21:25 +00001432Channel::GetRecPayloadType(CodecInst& codec)
1433{
pbos@webrtc.org6141e132013-04-09 10:09:10 +00001434 int8_t payloadType(-1);
wu@webrtc.org822fbd82013-08-15 23:38:54 +00001435 if (rtp_payload_registry_->ReceivePayloadType(
1436 codec.plname,
1437 codec.plfreq,
1438 codec.channels,
1439 (codec.rate < 0) ? 0 : codec.rate,
1440 &payloadType) != 0)
niklase@google.com470e71d2011-07-07 08:21:25 +00001441 {
1442 _engineStatisticsPtr->SetLastError(
henrika@webrtc.org37198002012-06-18 11:00:12 +00001443 VE_RTP_RTCP_MODULE_ERROR, kTraceWarning,
niklase@google.com470e71d2011-07-07 08:21:25 +00001444 "GetRecPayloadType() failed to retrieve RX payload type");
1445 return -1;
1446 }
1447 codec.pltype = payloadType;
niklase@google.com470e71d2011-07-07 08:21:25 +00001448 return 0;
1449}
1450
pbos@webrtc.org6141e132013-04-09 10:09:10 +00001451int32_t
niklase@google.com470e71d2011-07-07 08:21:25 +00001452Channel::SetSendCNPayloadType(int type, PayloadFrequencies frequency)
1453{
1454 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId,_channelId),
1455 "Channel::SetSendCNPayloadType()");
1456
1457 CodecInst codec;
pbos@webrtc.org6141e132013-04-09 10:09:10 +00001458 int32_t samplingFreqHz(-1);
tina.legrand@webrtc.org45175852012-06-01 09:27:35 +00001459 const int kMono = 1;
niklase@google.com470e71d2011-07-07 08:21:25 +00001460 if (frequency == kFreq32000Hz)
1461 samplingFreqHz = 32000;
1462 else if (frequency == kFreq16000Hz)
1463 samplingFreqHz = 16000;
1464
andrew@webrtc.orgeb524d92013-09-23 23:02:24 +00001465 if (audio_coding_->Codec("CN", &codec, samplingFreqHz, kMono) == -1)
niklase@google.com470e71d2011-07-07 08:21:25 +00001466 {
1467 _engineStatisticsPtr->SetLastError(
1468 VE_AUDIO_CODING_MODULE_ERROR, kTraceError,
1469 "SetSendCNPayloadType() failed to retrieve default CN codec "
1470 "settings");
1471 return -1;
1472 }
1473
1474 // Modify the payload type (must be set to dynamic range)
1475 codec.pltype = type;
1476
andrew@webrtc.orgeb524d92013-09-23 23:02:24 +00001477 if (audio_coding_->RegisterSendCodec(codec) != 0)
niklase@google.com470e71d2011-07-07 08:21:25 +00001478 {
1479 _engineStatisticsPtr->SetLastError(
1480 VE_AUDIO_CODING_MODULE_ERROR, kTraceError,
1481 "SetSendCNPayloadType() failed to register CN to ACM");
1482 return -1;
1483 }
1484
pwestin@webrtc.org2853dde2012-05-11 11:08:54 +00001485 if (_rtpRtcpModule->RegisterSendPayload(codec) != 0)
niklase@google.com470e71d2011-07-07 08:21:25 +00001486 {
pwestin@webrtc.org2853dde2012-05-11 11:08:54 +00001487 _rtpRtcpModule->DeRegisterSendPayload(codec.pltype);
1488 if (_rtpRtcpModule->RegisterSendPayload(codec) != 0)
niklase@google.com470e71d2011-07-07 08:21:25 +00001489 {
1490 _engineStatisticsPtr->SetLastError(
1491 VE_RTP_RTCP_MODULE_ERROR, kTraceError,
1492 "SetSendCNPayloadType() failed to register CN to RTP/RTCP "
1493 "module");
1494 return -1;
1495 }
1496 }
1497 return 0;
1498}
1499
minyue@webrtc.orgadee8f92014-09-03 12:28:06 +00001500int Channel::SetOpusMaxPlaybackRate(int frequency_hz) {
minyue@webrtc.org6aac93b2014-08-12 08:13:33 +00001501 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId, _channelId),
minyue@webrtc.orgadee8f92014-09-03 12:28:06 +00001502 "Channel::SetOpusMaxPlaybackRate()");
minyue@webrtc.org6aac93b2014-08-12 08:13:33 +00001503
minyue@webrtc.orgadee8f92014-09-03 12:28:06 +00001504 if (audio_coding_->SetOpusMaxPlaybackRate(frequency_hz) != 0) {
minyue@webrtc.org6aac93b2014-08-12 08:13:33 +00001505 _engineStatisticsPtr->SetLastError(
1506 VE_AUDIO_CODING_MODULE_ERROR, kTraceError,
minyue@webrtc.orgadee8f92014-09-03 12:28:06 +00001507 "SetOpusMaxPlaybackRate() failed to set maximum playback rate");
minyue@webrtc.org6aac93b2014-08-12 08:13:33 +00001508 return -1;
1509 }
1510 return 0;
1511}
1512
minyue@webrtc.org9b2e1142015-03-13 09:38:07 +00001513int Channel::SetOpusDtx(bool enable_dtx) {
1514 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId, _channelId),
1515 "Channel::SetOpusDtx(%d)", enable_dtx);
Minyue Li092041c2015-05-11 12:19:35 +02001516 int ret = enable_dtx ? audio_coding_->EnableOpusDtx()
minyue@webrtc.org9b2e1142015-03-13 09:38:07 +00001517 : audio_coding_->DisableOpusDtx();
1518 if (ret != 0) {
1519 _engineStatisticsPtr->SetLastError(
1520 VE_AUDIO_CODING_MODULE_ERROR, kTraceError, "SetOpusDtx() failed");
1521 return -1;
1522 }
1523 return 0;
1524}
1525
pbos@webrtc.org6141e132013-04-09 10:09:10 +00001526int32_t Channel::RegisterExternalTransport(Transport& transport)
niklase@google.com470e71d2011-07-07 08:21:25 +00001527{
1528 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId, _channelId),
1529 "Channel::RegisterExternalTransport()");
1530
mflodman@webrtc.org9a065d12012-03-07 08:12:21 +00001531 CriticalSectionScoped cs(&_callbackCritSect);
niklase@google.com470e71d2011-07-07 08:21:25 +00001532
niklase@google.com470e71d2011-07-07 08:21:25 +00001533 if (_externalTransport)
1534 {
1535 _engineStatisticsPtr->SetLastError(VE_INVALID_OPERATION,
1536 kTraceError,
1537 "RegisterExternalTransport() external transport already enabled");
1538 return -1;
1539 }
1540 _externalTransport = true;
1541 _transportPtr = &transport;
1542 return 0;
1543}
1544
pbos@webrtc.org6141e132013-04-09 10:09:10 +00001545int32_t
niklase@google.com470e71d2011-07-07 08:21:25 +00001546Channel::DeRegisterExternalTransport()
1547{
1548 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId,_channelId),
1549 "Channel::DeRegisterExternalTransport()");
1550
mflodman@webrtc.org9a065d12012-03-07 08:12:21 +00001551 CriticalSectionScoped cs(&_callbackCritSect);
xians@webrtc.org83661f52011-11-25 10:58:15 +00001552
niklase@google.com470e71d2011-07-07 08:21:25 +00001553 if (!_transportPtr)
1554 {
1555 _engineStatisticsPtr->SetLastError(
1556 VE_INVALID_OPERATION, kTraceWarning,
1557 "DeRegisterExternalTransport() external transport already "
1558 "disabled");
1559 return 0;
1560 }
1561 _externalTransport = false;
niklase@google.com470e71d2011-07-07 08:21:25 +00001562 _transportPtr = NULL;
1563 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId,_channelId),
1564 "DeRegisterExternalTransport() all transport is disabled");
niklase@google.com470e71d2011-07-07 08:21:25 +00001565 return 0;
1566}
1567
pkasting@chromium.org4591fbd2014-11-20 22:28:14 +00001568int32_t Channel::ReceivedRTPPacket(const int8_t* data, size_t length,
solenberg@webrtc.orgb1f50102014-03-24 10:38:25 +00001569 const PacketTime& packet_time) {
pwestin@webrtc.org0c459572013-04-03 15:43:57 +00001570 WEBRTC_TRACE(kTraceStream, kTraceVoice, VoEId(_instanceId,_channelId),
1571 "Channel::ReceivedRTPPacket()");
1572
1573 // Store playout timestamp for the received RTP packet
pwestin@webrtc.org1de01352013-04-11 20:23:35 +00001574 UpdatePlayoutTimestamp(false);
pwestin@webrtc.org0c459572013-04-03 15:43:57 +00001575
stefan@webrtc.org7bb8f022013-09-06 13:40:11 +00001576 const uint8_t* received_packet = reinterpret_cast<const uint8_t*>(data);
stefan@webrtc.orga5cb98c2013-05-29 12:12:51 +00001577 RTPHeader header;
stefan@webrtc.org7bb8f022013-09-06 13:40:11 +00001578 if (!rtp_header_parser_->Parse(received_packet, length, &header)) {
1579 WEBRTC_TRACE(webrtc::kTraceDebug, webrtc::kTraceVoice, _channelId,
1580 "Incoming packet: invalid RTP header");
stefan@webrtc.orga5cb98c2013-05-29 12:12:51 +00001581 return -1;
1582 }
wu@webrtc.org822fbd82013-08-15 23:38:54 +00001583 header.payload_type_frequency =
1584 rtp_payload_registry_->GetPayloadTypeFrequency(header.payloadType);
stefan@webrtc.org7bb8f022013-09-06 13:40:11 +00001585 if (header.payload_type_frequency < 0)
wu@webrtc.org822fbd82013-08-15 23:38:54 +00001586 return -1;
stefan@webrtc.org48df3812013-11-08 15:18:52 +00001587 bool in_order = IsPacketInOrder(header);
stefan@webrtc.org7bb8f022013-09-06 13:40:11 +00001588 rtp_receive_statistics_->IncomingPacket(header, length,
stefan@webrtc.org48df3812013-11-08 15:18:52 +00001589 IsPacketRetransmitted(header, in_order));
stefan@webrtc.org7bb8f022013-09-06 13:40:11 +00001590 rtp_payload_registry_->SetIncomingPayloadType(header);
solenberg@webrtc.orgb1f50102014-03-24 10:38:25 +00001591
stefan@webrtc.org48df3812013-11-08 15:18:52 +00001592 return ReceivePacket(received_packet, length, header, in_order) ? 0 : -1;
stefan@webrtc.org7bb8f022013-09-06 13:40:11 +00001593}
1594
1595bool Channel::ReceivePacket(const uint8_t* packet,
pkasting@chromium.org4591fbd2014-11-20 22:28:14 +00001596 size_t packet_length,
stefan@webrtc.org7bb8f022013-09-06 13:40:11 +00001597 const RTPHeader& header,
1598 bool in_order) {
minyue@webrtc.org456f0142015-01-23 11:58:42 +00001599 if (rtp_payload_registry_->IsRtx(header)) {
1600 return HandleRtxPacket(packet, packet_length, header);
wu@webrtc.org822fbd82013-08-15 23:38:54 +00001601 }
stefan@webrtc.org7bb8f022013-09-06 13:40:11 +00001602 const uint8_t* payload = packet + header.headerLength;
pkasting@chromium.org4591fbd2014-11-20 22:28:14 +00001603 assert(packet_length >= header.headerLength);
1604 size_t payload_length = packet_length - header.headerLength;
wu@webrtc.org822fbd82013-08-15 23:38:54 +00001605 PayloadUnion payload_specific;
1606 if (!rtp_payload_registry_->GetPayloadSpecifics(header.payloadType,
stefan@webrtc.org7bb8f022013-09-06 13:40:11 +00001607 &payload_specific)) {
1608 return false;
wu@webrtc.org822fbd82013-08-15 23:38:54 +00001609 }
stefan@webrtc.org7bb8f022013-09-06 13:40:11 +00001610 return rtp_receiver_->IncomingRtpPacket(header, payload, payload_length,
1611 payload_specific, in_order);
1612}
1613
minyue@webrtc.org456f0142015-01-23 11:58:42 +00001614bool Channel::HandleRtxPacket(const uint8_t* packet,
1615 size_t packet_length,
1616 const RTPHeader& header) {
stefan@webrtc.org7bb8f022013-09-06 13:40:11 +00001617 if (!rtp_payload_registry_->IsRtx(header))
1618 return false;
1619
1620 // Remove the RTX header and parse the original RTP header.
1621 if (packet_length < header.headerLength)
1622 return false;
1623 if (packet_length > kVoiceEngineMaxIpPacketSizeBytes)
1624 return false;
1625 if (restored_packet_in_use_) {
1626 WEBRTC_TRACE(webrtc::kTraceDebug, webrtc::kTraceVoice, _channelId,
1627 "Multiple RTX headers detected, dropping packet");
1628 return false;
pwestin@webrtc.org0c459572013-04-03 15:43:57 +00001629 }
stefan@webrtc.org7bb8f022013-09-06 13:40:11 +00001630 uint8_t* restored_packet_ptr = restored_packet_;
1631 if (!rtp_payload_registry_->RestoreOriginalPacket(
1632 &restored_packet_ptr, packet, &packet_length, rtp_receiver_->SSRC(),
1633 header)) {
1634 WEBRTC_TRACE(webrtc::kTraceDebug, webrtc::kTraceVoice, _channelId,
1635 "Incoming RTX packet: invalid RTP header");
1636 return false;
1637 }
1638 restored_packet_in_use_ = true;
1639 bool ret = OnRecoveredPacket(restored_packet_ptr, packet_length);
1640 restored_packet_in_use_ = false;
1641 return ret;
1642}
1643
1644bool Channel::IsPacketInOrder(const RTPHeader& header) const {
1645 StreamStatistician* statistician =
1646 rtp_receive_statistics_->GetStatistician(header.ssrc);
1647 if (!statistician)
1648 return false;
1649 return statistician->IsPacketInOrder(header.sequenceNumber);
niklase@google.com470e71d2011-07-07 08:21:25 +00001650}
1651
stefan@webrtc.org48df3812013-11-08 15:18:52 +00001652bool Channel::IsPacketRetransmitted(const RTPHeader& header,
1653 bool in_order) const {
stefan@webrtc.org7bb8f022013-09-06 13:40:11 +00001654 // Retransmissions are handled separately if RTX is enabled.
1655 if (rtp_payload_registry_->RtxEnabled())
1656 return false;
1657 StreamStatistician* statistician =
1658 rtp_receive_statistics_->GetStatistician(header.ssrc);
1659 if (!statistician)
1660 return false;
1661 // Check if this is a retransmission.
pkasting@chromium.org16825b12015-01-12 21:51:21 +00001662 int64_t min_rtt = 0;
stefan@webrtc.org7bb8f022013-09-06 13:40:11 +00001663 _rtpRtcpModule->RTT(rtp_receiver_->SSRC(), NULL, NULL, &min_rtt, NULL);
stefan@webrtc.org48df3812013-11-08 15:18:52 +00001664 return !in_order &&
stefan@webrtc.org7bb8f022013-09-06 13:40:11 +00001665 statistician->IsRetransmitOfOldPacket(header, min_rtt);
wu@webrtc.org822fbd82013-08-15 23:38:54 +00001666}
1667
pkasting@chromium.org4591fbd2014-11-20 22:28:14 +00001668int32_t Channel::ReceivedRTCPPacket(const int8_t* data, size_t length) {
pwestin@webrtc.org0c459572013-04-03 15:43:57 +00001669 WEBRTC_TRACE(kTraceStream, kTraceVoice, VoEId(_instanceId,_channelId),
1670 "Channel::ReceivedRTCPPacket()");
1671 // Store playout timestamp for the received RTCP packet
pwestin@webrtc.org1de01352013-04-11 20:23:35 +00001672 UpdatePlayoutTimestamp(true);
pwestin@webrtc.org0c459572013-04-03 15:43:57 +00001673
pwestin@webrtc.org0c459572013-04-03 15:43:57 +00001674 // Deliver RTCP packet to RTP/RTCP module for parsing
pkasting@chromium.org4591fbd2014-11-20 22:28:14 +00001675 if (_rtpRtcpModule->IncomingRtcpPacket((const uint8_t*)data, length) == -1) {
pwestin@webrtc.org0c459572013-04-03 15:43:57 +00001676 _engineStatisticsPtr->SetLastError(
1677 VE_SOCKET_TRANSPORT_MODULE_ERROR, kTraceWarning,
1678 "Channel::IncomingRTPPacket() RTCP packet is invalid");
1679 }
wu@webrtc.org82c4b852014-05-20 22:55:01 +00001680
Minyue2013aec2015-05-13 14:14:42 +02001681 int64_t rtt = GetRTT(true);
1682 if (rtt == 0) {
1683 // Waiting for valid RTT.
1684 return 0;
1685 }
1686 uint32_t ntp_secs = 0;
1687 uint32_t ntp_frac = 0;
1688 uint32_t rtp_timestamp = 0;
1689 if (0 != _rtpRtcpModule->RemoteNTP(&ntp_secs, &ntp_frac, NULL, NULL,
1690 &rtp_timestamp)) {
1691 // Waiting for RTCP.
1692 return 0;
1693 }
1694
stefan@webrtc.org8e24d872014-09-02 18:58:24 +00001695 {
1696 CriticalSectionScoped lock(ts_stats_lock_.get());
minyue@webrtc.org2c0cdbc2014-10-09 10:52:43 +00001697 ntp_estimator_.UpdateRtcpTimestamp(rtt, ntp_secs, ntp_frac, rtp_timestamp);
stefan@webrtc.org8e24d872014-09-02 18:58:24 +00001698 }
pwestin@webrtc.org0c459572013-04-03 15:43:57 +00001699 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +00001700}
1701
niklase@google.com470e71d2011-07-07 08:21:25 +00001702int Channel::StartPlayingFileLocally(const char* fileName,
pbos@webrtc.org92135212013-05-14 08:31:39 +00001703 bool loop,
1704 FileFormats format,
1705 int startPosition,
1706 float volumeScaling,
1707 int stopPosition,
niklase@google.com470e71d2011-07-07 08:21:25 +00001708 const CodecInst* codecInst)
1709{
1710 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId,_channelId),
1711 "Channel::StartPlayingFileLocally(fileNameUTF8[]=%s, loop=%d,"
1712 " format=%d, volumeScaling=%5.3f, startPosition=%d, "
1713 "stopPosition=%d)", fileName, loop, format, volumeScaling,
1714 startPosition, stopPosition);
1715
henrika@webrtc.org944cbeb2014-03-18 10:32:33 +00001716 if (channel_state_.Get().output_file_playing)
niklase@google.com470e71d2011-07-07 08:21:25 +00001717 {
1718 _engineStatisticsPtr->SetLastError(
1719 VE_ALREADY_PLAYING, kTraceError,
1720 "StartPlayingFileLocally() is already playing");
1721 return -1;
1722 }
1723
niklase@google.com470e71d2011-07-07 08:21:25 +00001724 {
mflodman@webrtc.org9a065d12012-03-07 08:12:21 +00001725 CriticalSectionScoped cs(&_fileCritSect);
henrike@webrtc.orgb37c6282011-10-31 23:53:04 +00001726
1727 if (_outputFilePlayerPtr)
1728 {
1729 _outputFilePlayerPtr->RegisterModuleFileCallback(NULL);
1730 FilePlayer::DestroyFilePlayer(_outputFilePlayerPtr);
1731 _outputFilePlayerPtr = NULL;
1732 }
1733
1734 _outputFilePlayerPtr = FilePlayer::CreateFilePlayer(
1735 _outputFilePlayerId, (const FileFormats)format);
1736
1737 if (_outputFilePlayerPtr == NULL)
1738 {
1739 _engineStatisticsPtr->SetLastError(
1740 VE_INVALID_ARGUMENT, kTraceError,
henrike@webrtc.org31d30702011-11-18 19:59:32 +00001741 "StartPlayingFileLocally() filePlayer format is not correct");
henrike@webrtc.orgb37c6282011-10-31 23:53:04 +00001742 return -1;
1743 }
1744
pbos@webrtc.org6141e132013-04-09 10:09:10 +00001745 const uint32_t notificationTime(0);
henrike@webrtc.orgb37c6282011-10-31 23:53:04 +00001746
1747 if (_outputFilePlayerPtr->StartPlayingFile(
1748 fileName,
1749 loop,
1750 startPosition,
1751 volumeScaling,
1752 notificationTime,
1753 stopPosition,
1754 (const CodecInst*)codecInst) != 0)
1755 {
1756 _engineStatisticsPtr->SetLastError(
1757 VE_BAD_FILE, kTraceError,
1758 "StartPlayingFile() failed to start file playout");
1759 _outputFilePlayerPtr->StopPlayingFile();
1760 FilePlayer::DestroyFilePlayer(_outputFilePlayerPtr);
1761 _outputFilePlayerPtr = NULL;
1762 return -1;
1763 }
1764 _outputFilePlayerPtr->RegisterModuleFileCallback(this);
henrika@webrtc.org944cbeb2014-03-18 10:32:33 +00001765 channel_state_.SetOutputFilePlaying(true);
niklase@google.com470e71d2011-07-07 08:21:25 +00001766 }
braveyao@webrtc.orgab129902012-06-04 03:26:39 +00001767
1768 if (RegisterFilePlayingToMixer() != 0)
henrike@webrtc.org066f9e52011-10-28 23:15:47 +00001769 return -1;
niklase@google.com470e71d2011-07-07 08:21:25 +00001770
1771 return 0;
1772}
1773
1774int Channel::StartPlayingFileLocally(InStream* stream,
pbos@webrtc.org92135212013-05-14 08:31:39 +00001775 FileFormats format,
1776 int startPosition,
1777 float volumeScaling,
1778 int stopPosition,
niklase@google.com470e71d2011-07-07 08:21:25 +00001779 const CodecInst* codecInst)
1780{
1781 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId,_channelId),
1782 "Channel::StartPlayingFileLocally(format=%d,"
1783 " volumeScaling=%5.3f, startPosition=%d, stopPosition=%d)",
1784 format, volumeScaling, startPosition, stopPosition);
1785
1786 if(stream == NULL)
1787 {
1788 _engineStatisticsPtr->SetLastError(
1789 VE_BAD_FILE, kTraceError,
1790 "StartPlayingFileLocally() NULL as input stream");
1791 return -1;
1792 }
1793
1794
henrika@webrtc.org944cbeb2014-03-18 10:32:33 +00001795 if (channel_state_.Get().output_file_playing)
niklase@google.com470e71d2011-07-07 08:21:25 +00001796 {
1797 _engineStatisticsPtr->SetLastError(
1798 VE_ALREADY_PLAYING, kTraceError,
1799 "StartPlayingFileLocally() is already playing");
1800 return -1;
1801 }
1802
niklase@google.com470e71d2011-07-07 08:21:25 +00001803 {
mflodman@webrtc.org9a065d12012-03-07 08:12:21 +00001804 CriticalSectionScoped cs(&_fileCritSect);
henrike@webrtc.orgb37c6282011-10-31 23:53:04 +00001805
1806 // Destroy the old instance
1807 if (_outputFilePlayerPtr)
1808 {
1809 _outputFilePlayerPtr->RegisterModuleFileCallback(NULL);
1810 FilePlayer::DestroyFilePlayer(_outputFilePlayerPtr);
1811 _outputFilePlayerPtr = NULL;
1812 }
1813
1814 // Create the instance
1815 _outputFilePlayerPtr = FilePlayer::CreateFilePlayer(
1816 _outputFilePlayerId,
1817 (const FileFormats)format);
1818
1819 if (_outputFilePlayerPtr == NULL)
1820 {
1821 _engineStatisticsPtr->SetLastError(
1822 VE_INVALID_ARGUMENT, kTraceError,
1823 "StartPlayingFileLocally() filePlayer format isnot correct");
1824 return -1;
1825 }
1826
pbos@webrtc.org6141e132013-04-09 10:09:10 +00001827 const uint32_t notificationTime(0);
henrike@webrtc.orgb37c6282011-10-31 23:53:04 +00001828
1829 if (_outputFilePlayerPtr->StartPlayingFile(*stream, startPosition,
1830 volumeScaling,
1831 notificationTime,
1832 stopPosition, codecInst) != 0)
1833 {
1834 _engineStatisticsPtr->SetLastError(VE_BAD_FILE, kTraceError,
1835 "StartPlayingFile() failed to "
1836 "start file playout");
1837 _outputFilePlayerPtr->StopPlayingFile();
1838 FilePlayer::DestroyFilePlayer(_outputFilePlayerPtr);
1839 _outputFilePlayerPtr = NULL;
1840 return -1;
1841 }
1842 _outputFilePlayerPtr->RegisterModuleFileCallback(this);
henrika@webrtc.org944cbeb2014-03-18 10:32:33 +00001843 channel_state_.SetOutputFilePlaying(true);
niklase@google.com470e71d2011-07-07 08:21:25 +00001844 }
braveyao@webrtc.orgab129902012-06-04 03:26:39 +00001845
1846 if (RegisterFilePlayingToMixer() != 0)
henrike@webrtc.org066f9e52011-10-28 23:15:47 +00001847 return -1;
niklase@google.com470e71d2011-07-07 08:21:25 +00001848
niklase@google.com470e71d2011-07-07 08:21:25 +00001849 return 0;
1850}
1851
1852int Channel::StopPlayingFileLocally()
1853{
1854 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId,_channelId),
1855 "Channel::StopPlayingFileLocally()");
1856
henrika@webrtc.org944cbeb2014-03-18 10:32:33 +00001857 if (!channel_state_.Get().output_file_playing)
niklase@google.com470e71d2011-07-07 08:21:25 +00001858 {
niklase@google.com470e71d2011-07-07 08:21:25 +00001859 return 0;
1860 }
1861
niklase@google.com470e71d2011-07-07 08:21:25 +00001862 {
mflodman@webrtc.org9a065d12012-03-07 08:12:21 +00001863 CriticalSectionScoped cs(&_fileCritSect);
henrike@webrtc.orgb37c6282011-10-31 23:53:04 +00001864
1865 if (_outputFilePlayerPtr->StopPlayingFile() != 0)
1866 {
1867 _engineStatisticsPtr->SetLastError(
1868 VE_STOP_RECORDING_FAILED, kTraceError,
1869 "StopPlayingFile() could not stop playing");
1870 return -1;
1871 }
1872 _outputFilePlayerPtr->RegisterModuleFileCallback(NULL);
1873 FilePlayer::DestroyFilePlayer(_outputFilePlayerPtr);
1874 _outputFilePlayerPtr = NULL;
henrika@webrtc.org944cbeb2014-03-18 10:32:33 +00001875 channel_state_.SetOutputFilePlaying(false);
niklase@google.com470e71d2011-07-07 08:21:25 +00001876 }
henrike@webrtc.orgb37c6282011-10-31 23:53:04 +00001877 // _fileCritSect cannot be taken while calling
1878 // SetAnonymousMixibilityStatus. Refer to comments in
1879 // StartPlayingFileLocally(const char* ...) for more details.
henrike@webrtc.org066f9e52011-10-28 23:15:47 +00001880 if (_outputMixerPtr->SetAnonymousMixabilityStatus(*this, false) != 0)
1881 {
1882 _engineStatisticsPtr->SetLastError(
1883 VE_AUDIO_CONF_MIX_MODULE_ERROR, kTraceError,
henrike@webrtc.orgb37c6282011-10-31 23:53:04 +00001884 "StopPlayingFile() failed to stop participant from playing as"
1885 "file in the mixer");
henrike@webrtc.org066f9e52011-10-28 23:15:47 +00001886 return -1;
1887 }
niklase@google.com470e71d2011-07-07 08:21:25 +00001888
1889 return 0;
1890}
1891
1892int Channel::IsPlayingFileLocally() const
1893{
henrika@webrtc.org944cbeb2014-03-18 10:32:33 +00001894 return channel_state_.Get().output_file_playing;
niklase@google.com470e71d2011-07-07 08:21:25 +00001895}
1896
braveyao@webrtc.orgab129902012-06-04 03:26:39 +00001897int Channel::RegisterFilePlayingToMixer()
1898{
1899 // Return success for not registering for file playing to mixer if:
1900 // 1. playing file before playout is started on that channel.
1901 // 2. starting playout without file playing on that channel.
henrika@webrtc.org944cbeb2014-03-18 10:32:33 +00001902 if (!channel_state_.Get().playing ||
1903 !channel_state_.Get().output_file_playing)
braveyao@webrtc.orgab129902012-06-04 03:26:39 +00001904 {
1905 return 0;
1906 }
1907
1908 // |_fileCritSect| cannot be taken while calling
1909 // SetAnonymousMixabilityStatus() since as soon as the participant is added
1910 // frames can be pulled by the mixer. Since the frames are generated from
1911 // the file, _fileCritSect will be taken. This would result in a deadlock.
1912 if (_outputMixerPtr->SetAnonymousMixabilityStatus(*this, true) != 0)
1913 {
henrika@webrtc.org944cbeb2014-03-18 10:32:33 +00001914 channel_state_.SetOutputFilePlaying(false);
braveyao@webrtc.orgab129902012-06-04 03:26:39 +00001915 CriticalSectionScoped cs(&_fileCritSect);
braveyao@webrtc.orgab129902012-06-04 03:26:39 +00001916 _engineStatisticsPtr->SetLastError(
1917 VE_AUDIO_CONF_MIX_MODULE_ERROR, kTraceError,
1918 "StartPlayingFile() failed to add participant as file to mixer");
1919 _outputFilePlayerPtr->StopPlayingFile();
1920 FilePlayer::DestroyFilePlayer(_outputFilePlayerPtr);
1921 _outputFilePlayerPtr = NULL;
1922 return -1;
1923 }
1924
1925 return 0;
1926}
1927
niklase@google.com470e71d2011-07-07 08:21:25 +00001928int Channel::StartPlayingFileAsMicrophone(const char* fileName,
pbos@webrtc.org92135212013-05-14 08:31:39 +00001929 bool loop,
1930 FileFormats format,
1931 int startPosition,
1932 float volumeScaling,
1933 int stopPosition,
niklase@google.com470e71d2011-07-07 08:21:25 +00001934 const CodecInst* codecInst)
1935{
1936 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId,_channelId),
1937 "Channel::StartPlayingFileAsMicrophone(fileNameUTF8[]=%s, "
1938 "loop=%d, format=%d, volumeScaling=%5.3f, startPosition=%d, "
1939 "stopPosition=%d)", fileName, loop, format, volumeScaling,
1940 startPosition, stopPosition);
1941
henrika@webrtc.org944cbeb2014-03-18 10:32:33 +00001942 CriticalSectionScoped cs(&_fileCritSect);
1943
1944 if (channel_state_.Get().input_file_playing)
niklase@google.com470e71d2011-07-07 08:21:25 +00001945 {
1946 _engineStatisticsPtr->SetLastError(
1947 VE_ALREADY_PLAYING, kTraceWarning,
1948 "StartPlayingFileAsMicrophone() filePlayer is playing");
1949 return 0;
1950 }
1951
niklase@google.com470e71d2011-07-07 08:21:25 +00001952 // Destroy the old instance
1953 if (_inputFilePlayerPtr)
1954 {
1955 _inputFilePlayerPtr->RegisterModuleFileCallback(NULL);
1956 FilePlayer::DestroyFilePlayer(_inputFilePlayerPtr);
1957 _inputFilePlayerPtr = NULL;
1958 }
1959
1960 // Create the instance
1961 _inputFilePlayerPtr = FilePlayer::CreateFilePlayer(
1962 _inputFilePlayerId, (const FileFormats)format);
1963
1964 if (_inputFilePlayerPtr == NULL)
1965 {
1966 _engineStatisticsPtr->SetLastError(
1967 VE_INVALID_ARGUMENT, kTraceError,
1968 "StartPlayingFileAsMicrophone() filePlayer format isnot correct");
1969 return -1;
1970 }
1971
pbos@webrtc.org6141e132013-04-09 10:09:10 +00001972 const uint32_t notificationTime(0);
niklase@google.com470e71d2011-07-07 08:21:25 +00001973
1974 if (_inputFilePlayerPtr->StartPlayingFile(
1975 fileName,
1976 loop,
1977 startPosition,
1978 volumeScaling,
1979 notificationTime,
1980 stopPosition,
1981 (const CodecInst*)codecInst) != 0)
1982 {
1983 _engineStatisticsPtr->SetLastError(
1984 VE_BAD_FILE, kTraceError,
1985 "StartPlayingFile() failed to start file playout");
1986 _inputFilePlayerPtr->StopPlayingFile();
1987 FilePlayer::DestroyFilePlayer(_inputFilePlayerPtr);
1988 _inputFilePlayerPtr = NULL;
1989 return -1;
1990 }
1991 _inputFilePlayerPtr->RegisterModuleFileCallback(this);
henrika@webrtc.org944cbeb2014-03-18 10:32:33 +00001992 channel_state_.SetInputFilePlaying(true);
niklase@google.com470e71d2011-07-07 08:21:25 +00001993
1994 return 0;
1995}
1996
1997int Channel::StartPlayingFileAsMicrophone(InStream* stream,
pbos@webrtc.org92135212013-05-14 08:31:39 +00001998 FileFormats format,
1999 int startPosition,
2000 float volumeScaling,
2001 int stopPosition,
niklase@google.com470e71d2011-07-07 08:21:25 +00002002 const CodecInst* codecInst)
2003{
2004 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId,_channelId),
2005 "Channel::StartPlayingFileAsMicrophone(format=%d, "
2006 "volumeScaling=%5.3f, startPosition=%d, stopPosition=%d)",
2007 format, volumeScaling, startPosition, stopPosition);
2008
2009 if(stream == NULL)
2010 {
2011 _engineStatisticsPtr->SetLastError(
2012 VE_BAD_FILE, kTraceError,
2013 "StartPlayingFileAsMicrophone NULL as input stream");
2014 return -1;
2015 }
2016
henrika@webrtc.org944cbeb2014-03-18 10:32:33 +00002017 CriticalSectionScoped cs(&_fileCritSect);
2018
2019 if (channel_state_.Get().input_file_playing)
niklase@google.com470e71d2011-07-07 08:21:25 +00002020 {
2021 _engineStatisticsPtr->SetLastError(
2022 VE_ALREADY_PLAYING, kTraceWarning,
2023 "StartPlayingFileAsMicrophone() is playing");
2024 return 0;
2025 }
2026
niklase@google.com470e71d2011-07-07 08:21:25 +00002027 // Destroy the old instance
2028 if (_inputFilePlayerPtr)
2029 {
2030 _inputFilePlayerPtr->RegisterModuleFileCallback(NULL);
2031 FilePlayer::DestroyFilePlayer(_inputFilePlayerPtr);
2032 _inputFilePlayerPtr = NULL;
2033 }
2034
2035 // Create the instance
2036 _inputFilePlayerPtr = FilePlayer::CreateFilePlayer(
2037 _inputFilePlayerId, (const FileFormats)format);
2038
2039 if (_inputFilePlayerPtr == NULL)
2040 {
2041 _engineStatisticsPtr->SetLastError(
2042 VE_INVALID_ARGUMENT, kTraceError,
2043 "StartPlayingInputFile() filePlayer format isnot correct");
2044 return -1;
2045 }
2046
pbos@webrtc.org6141e132013-04-09 10:09:10 +00002047 const uint32_t notificationTime(0);
niklase@google.com470e71d2011-07-07 08:21:25 +00002048
2049 if (_inputFilePlayerPtr->StartPlayingFile(*stream, startPosition,
2050 volumeScaling, notificationTime,
2051 stopPosition, codecInst) != 0)
2052 {
2053 _engineStatisticsPtr->SetLastError(VE_BAD_FILE, kTraceError,
2054 "StartPlayingFile() failed to start "
2055 "file playout");
2056 _inputFilePlayerPtr->StopPlayingFile();
2057 FilePlayer::DestroyFilePlayer(_inputFilePlayerPtr);
2058 _inputFilePlayerPtr = NULL;
2059 return -1;
2060 }
andrew@webrtc.orgae1a58b2013-01-22 04:44:30 +00002061
niklase@google.com470e71d2011-07-07 08:21:25 +00002062 _inputFilePlayerPtr->RegisterModuleFileCallback(this);
henrika@webrtc.org944cbeb2014-03-18 10:32:33 +00002063 channel_state_.SetInputFilePlaying(true);
niklase@google.com470e71d2011-07-07 08:21:25 +00002064
2065 return 0;
2066}
2067
2068int Channel::StopPlayingFileAsMicrophone()
2069{
2070 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId,_channelId),
2071 "Channel::StopPlayingFileAsMicrophone()");
2072
henrika@webrtc.org944cbeb2014-03-18 10:32:33 +00002073 CriticalSectionScoped cs(&_fileCritSect);
2074
2075 if (!channel_state_.Get().input_file_playing)
niklase@google.com470e71d2011-07-07 08:21:25 +00002076 {
niklase@google.com470e71d2011-07-07 08:21:25 +00002077 return 0;
2078 }
2079
niklase@google.com470e71d2011-07-07 08:21:25 +00002080 if (_inputFilePlayerPtr->StopPlayingFile() != 0)
2081 {
2082 _engineStatisticsPtr->SetLastError(
2083 VE_STOP_RECORDING_FAILED, kTraceError,
2084 "StopPlayingFile() could not stop playing");
2085 return -1;
2086 }
2087 _inputFilePlayerPtr->RegisterModuleFileCallback(NULL);
2088 FilePlayer::DestroyFilePlayer(_inputFilePlayerPtr);
2089 _inputFilePlayerPtr = NULL;
henrika@webrtc.org944cbeb2014-03-18 10:32:33 +00002090 channel_state_.SetInputFilePlaying(false);
niklase@google.com470e71d2011-07-07 08:21:25 +00002091
2092 return 0;
2093}
2094
2095int Channel::IsPlayingFileAsMicrophone() const
2096{
henrika@webrtc.org944cbeb2014-03-18 10:32:33 +00002097 return channel_state_.Get().input_file_playing;
niklase@google.com470e71d2011-07-07 08:21:25 +00002098}
2099
leozwang@webrtc.org813e4b02012-03-01 18:34:25 +00002100int Channel::StartRecordingPlayout(const char* fileName,
niklase@google.com470e71d2011-07-07 08:21:25 +00002101 const CodecInst* codecInst)
2102{
2103 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId,_channelId),
2104 "Channel::StartRecordingPlayout(fileName=%s)", fileName);
2105
2106 if (_outputFileRecording)
2107 {
2108 WEBRTC_TRACE(kTraceWarning, kTraceVoice, VoEId(_instanceId,-1),
2109 "StartRecordingPlayout() is already recording");
2110 return 0;
2111 }
2112
2113 FileFormats format;
pbos@webrtc.org6141e132013-04-09 10:09:10 +00002114 const uint32_t notificationTime(0); // Not supported in VoE
niklase@google.com470e71d2011-07-07 08:21:25 +00002115 CodecInst dummyCodec={100,"L16",16000,320,1,320000};
2116
niklas.enbom@webrtc.org40197d72012-03-26 08:45:47 +00002117 if ((codecInst != NULL) &&
2118 ((codecInst->channels < 1) || (codecInst->channels > 2)))
niklase@google.com470e71d2011-07-07 08:21:25 +00002119 {
2120 _engineStatisticsPtr->SetLastError(
2121 VE_BAD_ARGUMENT, kTraceError,
2122 "StartRecordingPlayout() invalid compression");
2123 return(-1);
2124 }
2125 if(codecInst == NULL)
2126 {
2127 format = kFileFormatPcm16kHzFile;
2128 codecInst=&dummyCodec;
2129 }
2130 else if((STR_CASE_CMP(codecInst->plname,"L16") == 0) ||
2131 (STR_CASE_CMP(codecInst->plname,"PCMU") == 0) ||
2132 (STR_CASE_CMP(codecInst->plname,"PCMA") == 0))
2133 {
2134 format = kFileFormatWavFile;
2135 }
2136 else
2137 {
2138 format = kFileFormatCompressedFile;
2139 }
2140
mflodman@webrtc.org9a065d12012-03-07 08:12:21 +00002141 CriticalSectionScoped cs(&_fileCritSect);
niklase@google.com470e71d2011-07-07 08:21:25 +00002142
2143 // Destroy the old instance
2144 if (_outputFileRecorderPtr)
2145 {
2146 _outputFileRecorderPtr->RegisterModuleFileCallback(NULL);
2147 FileRecorder::DestroyFileRecorder(_outputFileRecorderPtr);
2148 _outputFileRecorderPtr = NULL;
2149 }
2150
2151 _outputFileRecorderPtr = FileRecorder::CreateFileRecorder(
2152 _outputFileRecorderId, (const FileFormats)format);
2153 if (_outputFileRecorderPtr == NULL)
2154 {
2155 _engineStatisticsPtr->SetLastError(
2156 VE_INVALID_ARGUMENT, kTraceError,
2157 "StartRecordingPlayout() fileRecorder format isnot correct");
2158 return -1;
2159 }
2160
2161 if (_outputFileRecorderPtr->StartRecordingAudioFile(
2162 fileName, (const CodecInst&)*codecInst, notificationTime) != 0)
2163 {
2164 _engineStatisticsPtr->SetLastError(
2165 VE_BAD_FILE, kTraceError,
2166 "StartRecordingAudioFile() failed to start file recording");
2167 _outputFileRecorderPtr->StopRecording();
2168 FileRecorder::DestroyFileRecorder(_outputFileRecorderPtr);
2169 _outputFileRecorderPtr = NULL;
2170 return -1;
2171 }
2172 _outputFileRecorderPtr->RegisterModuleFileCallback(this);
2173 _outputFileRecording = true;
2174
2175 return 0;
2176}
2177
2178int Channel::StartRecordingPlayout(OutStream* stream,
2179 const CodecInst* codecInst)
2180{
2181 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId,_channelId),
2182 "Channel::StartRecordingPlayout()");
2183
2184 if (_outputFileRecording)
2185 {
2186 WEBRTC_TRACE(kTraceWarning, kTraceVoice, VoEId(_instanceId,-1),
2187 "StartRecordingPlayout() is already recording");
2188 return 0;
2189 }
2190
2191 FileFormats format;
pbos@webrtc.org6141e132013-04-09 10:09:10 +00002192 const uint32_t notificationTime(0); // Not supported in VoE
niklase@google.com470e71d2011-07-07 08:21:25 +00002193 CodecInst dummyCodec={100,"L16",16000,320,1,320000};
2194
2195 if (codecInst != NULL && codecInst->channels != 1)
2196 {
2197 _engineStatisticsPtr->SetLastError(
2198 VE_BAD_ARGUMENT, kTraceError,
2199 "StartRecordingPlayout() invalid compression");
2200 return(-1);
2201 }
2202 if(codecInst == NULL)
2203 {
2204 format = kFileFormatPcm16kHzFile;
2205 codecInst=&dummyCodec;
2206 }
2207 else if((STR_CASE_CMP(codecInst->plname,"L16") == 0) ||
2208 (STR_CASE_CMP(codecInst->plname,"PCMU") == 0) ||
2209 (STR_CASE_CMP(codecInst->plname,"PCMA") == 0))
2210 {
2211 format = kFileFormatWavFile;
2212 }
2213 else
2214 {
2215 format = kFileFormatCompressedFile;
2216 }
2217
mflodman@webrtc.org9a065d12012-03-07 08:12:21 +00002218 CriticalSectionScoped cs(&_fileCritSect);
niklase@google.com470e71d2011-07-07 08:21:25 +00002219
2220 // Destroy the old instance
2221 if (_outputFileRecorderPtr)
2222 {
2223 _outputFileRecorderPtr->RegisterModuleFileCallback(NULL);
2224 FileRecorder::DestroyFileRecorder(_outputFileRecorderPtr);
2225 _outputFileRecorderPtr = NULL;
2226 }
2227
2228 _outputFileRecorderPtr = FileRecorder::CreateFileRecorder(
2229 _outputFileRecorderId, (const FileFormats)format);
2230 if (_outputFileRecorderPtr == NULL)
2231 {
2232 _engineStatisticsPtr->SetLastError(
2233 VE_INVALID_ARGUMENT, kTraceError,
2234 "StartRecordingPlayout() fileRecorder format isnot correct");
2235 return -1;
2236 }
2237
2238 if (_outputFileRecorderPtr->StartRecordingAudioFile(*stream, *codecInst,
2239 notificationTime) != 0)
2240 {
2241 _engineStatisticsPtr->SetLastError(VE_BAD_FILE, kTraceError,
2242 "StartRecordingPlayout() failed to "
2243 "start file recording");
2244 _outputFileRecorderPtr->StopRecording();
2245 FileRecorder::DestroyFileRecorder(_outputFileRecorderPtr);
2246 _outputFileRecorderPtr = NULL;
2247 return -1;
2248 }
andrew@webrtc.orgae1a58b2013-01-22 04:44:30 +00002249
niklase@google.com470e71d2011-07-07 08:21:25 +00002250 _outputFileRecorderPtr->RegisterModuleFileCallback(this);
2251 _outputFileRecording = true;
2252
2253 return 0;
2254}
2255
2256int Channel::StopRecordingPlayout()
2257{
2258 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId,-1),
2259 "Channel::StopRecordingPlayout()");
2260
2261 if (!_outputFileRecording)
2262 {
2263 WEBRTC_TRACE(kTraceError, kTraceVoice, VoEId(_instanceId,-1),
2264 "StopRecordingPlayout() isnot recording");
2265 return -1;
2266 }
2267
2268
mflodman@webrtc.org9a065d12012-03-07 08:12:21 +00002269 CriticalSectionScoped cs(&_fileCritSect);
niklase@google.com470e71d2011-07-07 08:21:25 +00002270
2271 if (_outputFileRecorderPtr->StopRecording() != 0)
2272 {
2273 _engineStatisticsPtr->SetLastError(
2274 VE_STOP_RECORDING_FAILED, kTraceError,
2275 "StopRecording() could not stop recording");
2276 return(-1);
2277 }
2278 _outputFileRecorderPtr->RegisterModuleFileCallback(NULL);
2279 FileRecorder::DestroyFileRecorder(_outputFileRecorderPtr);
2280 _outputFileRecorderPtr = NULL;
2281 _outputFileRecording = false;
2282
2283 return 0;
2284}
2285
2286void
2287Channel::SetMixWithMicStatus(bool mix)
2288{
henrika@webrtc.org944cbeb2014-03-18 10:32:33 +00002289 CriticalSectionScoped cs(&_fileCritSect);
niklase@google.com470e71d2011-07-07 08:21:25 +00002290 _mixFileWithMicrophone=mix;
2291}
2292
2293int
pbos@webrtc.org6141e132013-04-09 10:09:10 +00002294Channel::GetSpeechOutputLevel(uint32_t& level) const
niklase@google.com470e71d2011-07-07 08:21:25 +00002295{
pbos@webrtc.org6141e132013-04-09 10:09:10 +00002296 int8_t currentLevel = _outputAudioLevel.Level();
2297 level = static_cast<int32_t> (currentLevel);
niklase@google.com470e71d2011-07-07 08:21:25 +00002298 return 0;
2299}
2300
2301int
pbos@webrtc.org6141e132013-04-09 10:09:10 +00002302Channel::GetSpeechOutputLevelFullRange(uint32_t& level) const
niklase@google.com470e71d2011-07-07 08:21:25 +00002303{
pbos@webrtc.org6141e132013-04-09 10:09:10 +00002304 int16_t currentLevel = _outputAudioLevel.LevelFullRange();
2305 level = static_cast<int32_t> (currentLevel);
niklase@google.com470e71d2011-07-07 08:21:25 +00002306 return 0;
2307}
2308
2309int
2310Channel::SetMute(bool enable)
2311{
wu@webrtc.org63420662013-10-17 18:28:55 +00002312 CriticalSectionScoped cs(&volume_settings_critsect_);
niklase@google.com470e71d2011-07-07 08:21:25 +00002313 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId,_channelId),
2314 "Channel::SetMute(enable=%d)", enable);
2315 _mute = enable;
2316 return 0;
2317}
2318
2319bool
2320Channel::Mute() const
2321{
wu@webrtc.org63420662013-10-17 18:28:55 +00002322 CriticalSectionScoped cs(&volume_settings_critsect_);
niklase@google.com470e71d2011-07-07 08:21:25 +00002323 return _mute;
2324}
2325
2326int
2327Channel::SetOutputVolumePan(float left, float right)
2328{
wu@webrtc.org63420662013-10-17 18:28:55 +00002329 CriticalSectionScoped cs(&volume_settings_critsect_);
niklase@google.com470e71d2011-07-07 08:21:25 +00002330 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId,_channelId),
2331 "Channel::SetOutputVolumePan()");
2332 _panLeft = left;
2333 _panRight = right;
2334 return 0;
2335}
2336
2337int
2338Channel::GetOutputVolumePan(float& left, float& right) const
2339{
wu@webrtc.org63420662013-10-17 18:28:55 +00002340 CriticalSectionScoped cs(&volume_settings_critsect_);
niklase@google.com470e71d2011-07-07 08:21:25 +00002341 left = _panLeft;
2342 right = _panRight;
niklase@google.com470e71d2011-07-07 08:21:25 +00002343 return 0;
2344}
2345
2346int
2347Channel::SetChannelOutputVolumeScaling(float scaling)
2348{
wu@webrtc.org63420662013-10-17 18:28:55 +00002349 CriticalSectionScoped cs(&volume_settings_critsect_);
niklase@google.com470e71d2011-07-07 08:21:25 +00002350 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId,_channelId),
2351 "Channel::SetChannelOutputVolumeScaling()");
2352 _outputGain = scaling;
2353 return 0;
2354}
2355
2356int
2357Channel::GetChannelOutputVolumeScaling(float& scaling) const
2358{
wu@webrtc.org63420662013-10-17 18:28:55 +00002359 CriticalSectionScoped cs(&volume_settings_critsect_);
niklase@google.com470e71d2011-07-07 08:21:25 +00002360 scaling = _outputGain;
niklase@google.com470e71d2011-07-07 08:21:25 +00002361 return 0;
2362}
2363
niklase@google.com470e71d2011-07-07 08:21:25 +00002364int Channel::SendTelephoneEventOutband(unsigned char eventCode,
wu@webrtc.org822fbd82013-08-15 23:38:54 +00002365 int lengthMs, int attenuationDb,
2366 bool playDtmfEvent)
niklase@google.com470e71d2011-07-07 08:21:25 +00002367{
2368 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId, _channelId),
2369 "Channel::SendTelephoneEventOutband(..., playDtmfEvent=%d)",
2370 playDtmfEvent);
2371
2372 _playOutbandDtmfEvent = playDtmfEvent;
2373
pwestin@webrtc.org2853dde2012-05-11 11:08:54 +00002374 if (_rtpRtcpModule->SendTelephoneEventOutband(eventCode, lengthMs,
niklase@google.com470e71d2011-07-07 08:21:25 +00002375 attenuationDb) != 0)
2376 {
2377 _engineStatisticsPtr->SetLastError(
2378 VE_SEND_DTMF_FAILED,
2379 kTraceWarning,
2380 "SendTelephoneEventOutband() failed to send event");
2381 return -1;
2382 }
2383 return 0;
2384}
2385
2386int Channel::SendTelephoneEventInband(unsigned char eventCode,
2387 int lengthMs,
2388 int attenuationDb,
2389 bool playDtmfEvent)
2390{
2391 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId, _channelId),
2392 "Channel::SendTelephoneEventInband(..., playDtmfEvent=%d)",
2393 playDtmfEvent);
2394
2395 _playInbandDtmfEvent = playDtmfEvent;
2396 _inbandDtmfQueue.AddDtmf(eventCode, lengthMs, attenuationDb);
2397
2398 return 0;
2399}
2400
2401int
niklase@google.com470e71d2011-07-07 08:21:25 +00002402Channel::SetSendTelephoneEventPayloadType(unsigned char type)
2403{
2404 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId,_channelId),
2405 "Channel::SetSendTelephoneEventPayloadType()");
andrew@webrtc.orgf81f9f82011-08-19 22:56:22 +00002406 if (type > 127)
niklase@google.com470e71d2011-07-07 08:21:25 +00002407 {
2408 _engineStatisticsPtr->SetLastError(
2409 VE_INVALID_ARGUMENT, kTraceError,
2410 "SetSendTelephoneEventPayloadType() invalid type");
2411 return -1;
2412 }
pbos@webrtc.org5b10d8f2013-07-11 15:50:07 +00002413 CodecInst codec = {};
pwestin@webrtc.org1da1ce02011-10-13 15:19:55 +00002414 codec.plfreq = 8000;
2415 codec.pltype = type;
2416 memcpy(codec.plname, "telephone-event", 16);
pwestin@webrtc.org2853dde2012-05-11 11:08:54 +00002417 if (_rtpRtcpModule->RegisterSendPayload(codec) != 0)
niklase@google.com470e71d2011-07-07 08:21:25 +00002418 {
henrika@webrtc.org4392d5f2013-04-17 07:34:25 +00002419 _rtpRtcpModule->DeRegisterSendPayload(codec.pltype);
2420 if (_rtpRtcpModule->RegisterSendPayload(codec) != 0) {
2421 _engineStatisticsPtr->SetLastError(
2422 VE_RTP_RTCP_MODULE_ERROR, kTraceError,
2423 "SetSendTelephoneEventPayloadType() failed to register send"
2424 "payload type");
2425 return -1;
2426 }
niklase@google.com470e71d2011-07-07 08:21:25 +00002427 }
2428 _sendTelephoneEventPayloadType = type;
2429 return 0;
2430}
2431
2432int
2433Channel::GetSendTelephoneEventPayloadType(unsigned char& type)
2434{
niklase@google.com470e71d2011-07-07 08:21:25 +00002435 type = _sendTelephoneEventPayloadType;
niklase@google.com470e71d2011-07-07 08:21:25 +00002436 return 0;
2437}
2438
niklase@google.com470e71d2011-07-07 08:21:25 +00002439int
2440Channel::UpdateRxVadDetection(AudioFrame& audioFrame)
2441{
2442 WEBRTC_TRACE(kTraceStream, kTraceVoice, VoEId(_instanceId,_channelId),
2443 "Channel::UpdateRxVadDetection()");
2444
2445 int vadDecision = 1;
2446
andrew@webrtc.org63a50982012-05-02 23:56:37 +00002447 vadDecision = (audioFrame.vad_activity_ == AudioFrame::kVadActive)? 1 : 0;
niklase@google.com470e71d2011-07-07 08:21:25 +00002448
2449 if ((vadDecision != _oldVadDecision) && _rxVadObserverPtr)
2450 {
2451 OnRxVadDetected(vadDecision);
2452 _oldVadDecision = vadDecision;
2453 }
2454
2455 WEBRTC_TRACE(kTraceStream, kTraceVoice, VoEId(_instanceId,_channelId),
2456 "Channel::UpdateRxVadDetection() => vadDecision=%d",
2457 vadDecision);
2458 return 0;
2459}
2460
2461int
2462Channel::RegisterRxVadObserver(VoERxVadCallback &observer)
2463{
2464 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId,_channelId),
2465 "Channel::RegisterRxVadObserver()");
mflodman@webrtc.org9a065d12012-03-07 08:12:21 +00002466 CriticalSectionScoped cs(&_callbackCritSect);
niklase@google.com470e71d2011-07-07 08:21:25 +00002467
2468 if (_rxVadObserverPtr)
2469 {
2470 _engineStatisticsPtr->SetLastError(
2471 VE_INVALID_OPERATION, kTraceError,
2472 "RegisterRxVadObserver() observer already enabled");
2473 return -1;
2474 }
niklase@google.com470e71d2011-07-07 08:21:25 +00002475 _rxVadObserverPtr = &observer;
2476 _RxVadDetection = true;
2477 return 0;
2478}
2479
2480int
2481Channel::DeRegisterRxVadObserver()
2482{
2483 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId,_channelId),
2484 "Channel::DeRegisterRxVadObserver()");
mflodman@webrtc.org9a065d12012-03-07 08:12:21 +00002485 CriticalSectionScoped cs(&_callbackCritSect);
niklase@google.com470e71d2011-07-07 08:21:25 +00002486
2487 if (!_rxVadObserverPtr)
2488 {
2489 _engineStatisticsPtr->SetLastError(
2490 VE_INVALID_OPERATION, kTraceWarning,
2491 "DeRegisterRxVadObserver() observer already disabled");
2492 return 0;
2493 }
2494 _rxVadObserverPtr = NULL;
2495 _RxVadDetection = false;
2496 return 0;
2497}
2498
2499int
2500Channel::VoiceActivityIndicator(int &activity)
2501{
2502 activity = _sendFrameType;
niklase@google.com470e71d2011-07-07 08:21:25 +00002503 return 0;
2504}
2505
2506#ifdef WEBRTC_VOICE_ENGINE_AGC
2507
2508int
pbos@webrtc.org92135212013-05-14 08:31:39 +00002509Channel::SetRxAgcStatus(bool enable, AgcModes mode)
niklase@google.com470e71d2011-07-07 08:21:25 +00002510{
2511 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId,_channelId),
2512 "Channel::SetRxAgcStatus(enable=%d, mode=%d)",
2513 (int)enable, (int)mode);
2514
andrew@webrtc.org6c264cc2013-10-04 17:54:09 +00002515 GainControl::Mode agcMode = kDefaultRxAgcMode;
niklase@google.com470e71d2011-07-07 08:21:25 +00002516 switch (mode)
2517 {
2518 case kAgcDefault:
niklase@google.com470e71d2011-07-07 08:21:25 +00002519 break;
2520 case kAgcUnchanged:
andrew@webrtc.orgf3930e92013-09-18 22:37:32 +00002521 agcMode = rx_audioproc_->gain_control()->mode();
niklase@google.com470e71d2011-07-07 08:21:25 +00002522 break;
2523 case kAgcFixedDigital:
2524 agcMode = GainControl::kFixedDigital;
2525 break;
2526 case kAgcAdaptiveDigital:
2527 agcMode =GainControl::kAdaptiveDigital;
2528 break;
2529 default:
2530 _engineStatisticsPtr->SetLastError(
2531 VE_INVALID_ARGUMENT, kTraceError,
2532 "SetRxAgcStatus() invalid Agc mode");
2533 return -1;
2534 }
2535
andrew@webrtc.orgf3930e92013-09-18 22:37:32 +00002536 if (rx_audioproc_->gain_control()->set_mode(agcMode) != 0)
niklase@google.com470e71d2011-07-07 08:21:25 +00002537 {
2538 _engineStatisticsPtr->SetLastError(
2539 VE_APM_ERROR, kTraceError,
2540 "SetRxAgcStatus() failed to set Agc mode");
2541 return -1;
2542 }
andrew@webrtc.orgf3930e92013-09-18 22:37:32 +00002543 if (rx_audioproc_->gain_control()->Enable(enable) != 0)
niklase@google.com470e71d2011-07-07 08:21:25 +00002544 {
2545 _engineStatisticsPtr->SetLastError(
2546 VE_APM_ERROR, kTraceError,
2547 "SetRxAgcStatus() failed to set Agc state");
2548 return -1;
2549 }
2550
2551 _rxAgcIsEnabled = enable;
henrika@webrtc.org944cbeb2014-03-18 10:32:33 +00002552 channel_state_.SetRxApmIsEnabled(_rxAgcIsEnabled || _rxNsIsEnabled);
niklase@google.com470e71d2011-07-07 08:21:25 +00002553
2554 return 0;
2555}
2556
2557int
2558Channel::GetRxAgcStatus(bool& enabled, AgcModes& mode)
2559{
andrew@webrtc.orgf3930e92013-09-18 22:37:32 +00002560 bool enable = rx_audioproc_->gain_control()->is_enabled();
niklase@google.com470e71d2011-07-07 08:21:25 +00002561 GainControl::Mode agcMode =
andrew@webrtc.orgf3930e92013-09-18 22:37:32 +00002562 rx_audioproc_->gain_control()->mode();
niklase@google.com470e71d2011-07-07 08:21:25 +00002563
2564 enabled = enable;
2565
2566 switch (agcMode)
2567 {
2568 case GainControl::kFixedDigital:
2569 mode = kAgcFixedDigital;
2570 break;
2571 case GainControl::kAdaptiveDigital:
2572 mode = kAgcAdaptiveDigital;
2573 break;
2574 default:
2575 _engineStatisticsPtr->SetLastError(
2576 VE_APM_ERROR, kTraceError,
2577 "GetRxAgcStatus() invalid Agc mode");
2578 return -1;
2579 }
2580
2581 return 0;
2582}
2583
2584int
pbos@webrtc.org92135212013-05-14 08:31:39 +00002585Channel::SetRxAgcConfig(AgcConfig config)
niklase@google.com470e71d2011-07-07 08:21:25 +00002586{
2587 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId,_channelId),
2588 "Channel::SetRxAgcConfig()");
2589
andrew@webrtc.orgf3930e92013-09-18 22:37:32 +00002590 if (rx_audioproc_->gain_control()->set_target_level_dbfs(
niklase@google.com470e71d2011-07-07 08:21:25 +00002591 config.targetLeveldBOv) != 0)
2592 {
2593 _engineStatisticsPtr->SetLastError(
2594 VE_APM_ERROR, kTraceError,
2595 "SetRxAgcConfig() failed to set target peak |level|"
2596 "(or envelope) of the Agc");
2597 return -1;
2598 }
andrew@webrtc.orgf3930e92013-09-18 22:37:32 +00002599 if (rx_audioproc_->gain_control()->set_compression_gain_db(
niklase@google.com470e71d2011-07-07 08:21:25 +00002600 config.digitalCompressionGaindB) != 0)
2601 {
2602 _engineStatisticsPtr->SetLastError(
2603 VE_APM_ERROR, kTraceError,
2604 "SetRxAgcConfig() failed to set the range in |gain| the"
2605 " digital compression stage may apply");
2606 return -1;
2607 }
andrew@webrtc.orgf3930e92013-09-18 22:37:32 +00002608 if (rx_audioproc_->gain_control()->enable_limiter(
niklase@google.com470e71d2011-07-07 08:21:25 +00002609 config.limiterEnable) != 0)
2610 {
2611 _engineStatisticsPtr->SetLastError(
2612 VE_APM_ERROR, kTraceError,
2613 "SetRxAgcConfig() failed to set hard limiter to the signal");
2614 return -1;
2615 }
2616
2617 return 0;
2618}
2619
2620int
2621Channel::GetRxAgcConfig(AgcConfig& config)
2622{
niklase@google.com470e71d2011-07-07 08:21:25 +00002623 config.targetLeveldBOv =
andrew@webrtc.orgf3930e92013-09-18 22:37:32 +00002624 rx_audioproc_->gain_control()->target_level_dbfs();
niklase@google.com470e71d2011-07-07 08:21:25 +00002625 config.digitalCompressionGaindB =
andrew@webrtc.orgf3930e92013-09-18 22:37:32 +00002626 rx_audioproc_->gain_control()->compression_gain_db();
niklase@google.com470e71d2011-07-07 08:21:25 +00002627 config.limiterEnable =
andrew@webrtc.orgf3930e92013-09-18 22:37:32 +00002628 rx_audioproc_->gain_control()->is_limiter_enabled();
niklase@google.com470e71d2011-07-07 08:21:25 +00002629
niklase@google.com470e71d2011-07-07 08:21:25 +00002630 return 0;
2631}
2632
2633#endif // #ifdef WEBRTC_VOICE_ENGINE_AGC
2634
2635#ifdef WEBRTC_VOICE_ENGINE_NR
2636
2637int
pbos@webrtc.org92135212013-05-14 08:31:39 +00002638Channel::SetRxNsStatus(bool enable, NsModes mode)
niklase@google.com470e71d2011-07-07 08:21:25 +00002639{
2640 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId,_channelId),
2641 "Channel::SetRxNsStatus(enable=%d, mode=%d)",
2642 (int)enable, (int)mode);
2643
andrew@webrtc.org6c264cc2013-10-04 17:54:09 +00002644 NoiseSuppression::Level nsLevel = kDefaultNsMode;
niklase@google.com470e71d2011-07-07 08:21:25 +00002645 switch (mode)
2646 {
2647
2648 case kNsDefault:
niklase@google.com470e71d2011-07-07 08:21:25 +00002649 break;
2650 case kNsUnchanged:
andrew@webrtc.orgf3930e92013-09-18 22:37:32 +00002651 nsLevel = rx_audioproc_->noise_suppression()->level();
niklase@google.com470e71d2011-07-07 08:21:25 +00002652 break;
2653 case kNsConference:
2654 nsLevel = NoiseSuppression::kHigh;
2655 break;
2656 case kNsLowSuppression:
2657 nsLevel = NoiseSuppression::kLow;
2658 break;
2659 case kNsModerateSuppression:
2660 nsLevel = NoiseSuppression::kModerate;
2661 break;
2662 case kNsHighSuppression:
2663 nsLevel = NoiseSuppression::kHigh;
2664 break;
2665 case kNsVeryHighSuppression:
2666 nsLevel = NoiseSuppression::kVeryHigh;
2667 break;
niklase@google.com470e71d2011-07-07 08:21:25 +00002668 }
2669
andrew@webrtc.orgf3930e92013-09-18 22:37:32 +00002670 if (rx_audioproc_->noise_suppression()->set_level(nsLevel)
niklase@google.com470e71d2011-07-07 08:21:25 +00002671 != 0)
2672 {
2673 _engineStatisticsPtr->SetLastError(
2674 VE_APM_ERROR, kTraceError,
andrew@webrtc.org6c264cc2013-10-04 17:54:09 +00002675 "SetRxNsStatus() failed to set NS level");
niklase@google.com470e71d2011-07-07 08:21:25 +00002676 return -1;
2677 }
andrew@webrtc.orgf3930e92013-09-18 22:37:32 +00002678 if (rx_audioproc_->noise_suppression()->Enable(enable) != 0)
niklase@google.com470e71d2011-07-07 08:21:25 +00002679 {
2680 _engineStatisticsPtr->SetLastError(
2681 VE_APM_ERROR, kTraceError,
andrew@webrtc.org6c264cc2013-10-04 17:54:09 +00002682 "SetRxNsStatus() failed to set NS state");
niklase@google.com470e71d2011-07-07 08:21:25 +00002683 return -1;
2684 }
2685
2686 _rxNsIsEnabled = enable;
henrika@webrtc.org944cbeb2014-03-18 10:32:33 +00002687 channel_state_.SetRxApmIsEnabled(_rxAgcIsEnabled || _rxNsIsEnabled);
niklase@google.com470e71d2011-07-07 08:21:25 +00002688
2689 return 0;
2690}
2691
2692int
2693Channel::GetRxNsStatus(bool& enabled, NsModes& mode)
2694{
niklase@google.com470e71d2011-07-07 08:21:25 +00002695 bool enable =
andrew@webrtc.orgf3930e92013-09-18 22:37:32 +00002696 rx_audioproc_->noise_suppression()->is_enabled();
niklase@google.com470e71d2011-07-07 08:21:25 +00002697 NoiseSuppression::Level ncLevel =
andrew@webrtc.orgf3930e92013-09-18 22:37:32 +00002698 rx_audioproc_->noise_suppression()->level();
niklase@google.com470e71d2011-07-07 08:21:25 +00002699
2700 enabled = enable;
2701
2702 switch (ncLevel)
2703 {
2704 case NoiseSuppression::kLow:
2705 mode = kNsLowSuppression;
2706 break;
2707 case NoiseSuppression::kModerate:
2708 mode = kNsModerateSuppression;
2709 break;
2710 case NoiseSuppression::kHigh:
2711 mode = kNsHighSuppression;
2712 break;
2713 case NoiseSuppression::kVeryHigh:
2714 mode = kNsVeryHighSuppression;
2715 break;
niklase@google.com470e71d2011-07-07 08:21:25 +00002716 }
2717
niklase@google.com470e71d2011-07-07 08:21:25 +00002718 return 0;
2719}
2720
2721#endif // #ifdef WEBRTC_VOICE_ENGINE_NR
2722
2723int
niklase@google.com470e71d2011-07-07 08:21:25 +00002724Channel::SetLocalSSRC(unsigned int ssrc)
2725{
2726 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId, _channelId),
2727 "Channel::SetLocalSSRC()");
henrika@webrtc.org944cbeb2014-03-18 10:32:33 +00002728 if (channel_state_.Get().sending)
niklase@google.com470e71d2011-07-07 08:21:25 +00002729 {
2730 _engineStatisticsPtr->SetLastError(
2731 VE_ALREADY_SENDING, kTraceError,
2732 "SetLocalSSRC() already sending");
2733 return -1;
2734 }
stefan@webrtc.orgef927552014-06-05 08:25:29 +00002735 _rtpRtcpModule->SetSSRC(ssrc);
niklase@google.com470e71d2011-07-07 08:21:25 +00002736 return 0;
2737}
2738
2739int
2740Channel::GetLocalSSRC(unsigned int& ssrc)
2741{
pwestin@webrtc.org2853dde2012-05-11 11:08:54 +00002742 ssrc = _rtpRtcpModule->SSRC();
niklase@google.com470e71d2011-07-07 08:21:25 +00002743 return 0;
2744}
2745
2746int
2747Channel::GetRemoteSSRC(unsigned int& ssrc)
2748{
wu@webrtc.org822fbd82013-08-15 23:38:54 +00002749 ssrc = rtp_receiver_->SSRC();
niklase@google.com470e71d2011-07-07 08:21:25 +00002750 return 0;
2751}
2752
wu@webrtc.orgebdb0e32014-03-06 23:49:08 +00002753int Channel::SetSendAudioLevelIndicationStatus(bool enable, unsigned char id) {
andrew@webrtc.orgf3930e92013-09-18 22:37:32 +00002754 _includeAudioLevelIndication = enable;
wu@webrtc.orgebdb0e32014-03-06 23:49:08 +00002755 return SetSendRtpHeaderExtension(enable, kRtpExtensionAudioLevel, id);
niklase@google.com470e71d2011-07-07 08:21:25 +00002756}
andrew@webrtc.orgf3930e92013-09-18 22:37:32 +00002757
wu@webrtc.org93fd25c2014-04-24 20:33:08 +00002758int Channel::SetReceiveAudioLevelIndicationStatus(bool enable,
2759 unsigned char id) {
2760 rtp_header_parser_->DeregisterRtpHeaderExtension(
2761 kRtpExtensionAudioLevel);
2762 if (enable && !rtp_header_parser_->RegisterRtpHeaderExtension(
2763 kRtpExtensionAudioLevel, id)) {
2764 return -1;
2765 }
2766 return 0;
2767}
2768
wu@webrtc.orgebdb0e32014-03-06 23:49:08 +00002769int Channel::SetSendAbsoluteSenderTimeStatus(bool enable, unsigned char id) {
2770 return SetSendRtpHeaderExtension(enable, kRtpExtensionAbsoluteSendTime, id);
2771}
2772
2773int Channel::SetReceiveAbsoluteSenderTimeStatus(bool enable, unsigned char id) {
2774 rtp_header_parser_->DeregisterRtpHeaderExtension(
2775 kRtpExtensionAbsoluteSendTime);
solenberg@webrtc.orgb1f50102014-03-24 10:38:25 +00002776 if (enable && !rtp_header_parser_->RegisterRtpHeaderExtension(
2777 kRtpExtensionAbsoluteSendTime, id)) {
2778 return -1;
wu@webrtc.orgebdb0e32014-03-06 23:49:08 +00002779 }
2780 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +00002781}
2782
pbos@webrtc.orgd16e8392014-12-19 13:49:55 +00002783void Channel::SetRTCPStatus(bool enable) {
2784 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId, _channelId),
2785 "Channel::SetRTCPStatus()");
2786 _rtpRtcpModule->SetRTCPStatus(enable ? kRtcpCompound : kRtcpOff);
niklase@google.com470e71d2011-07-07 08:21:25 +00002787}
2788
2789int
2790Channel::GetRTCPStatus(bool& enabled)
2791{
pwestin@webrtc.org2853dde2012-05-11 11:08:54 +00002792 RTCPMethod method = _rtpRtcpModule->RTCP();
niklase@google.com470e71d2011-07-07 08:21:25 +00002793 enabled = (method != kRtcpOff);
niklase@google.com470e71d2011-07-07 08:21:25 +00002794 return 0;
2795}
2796
2797int
2798Channel::SetRTCP_CNAME(const char cName[256])
2799{
2800 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId, _channelId),
2801 "Channel::SetRTCP_CNAME()");
pwestin@webrtc.org2853dde2012-05-11 11:08:54 +00002802 if (_rtpRtcpModule->SetCNAME(cName) != 0)
niklase@google.com470e71d2011-07-07 08:21:25 +00002803 {
2804 _engineStatisticsPtr->SetLastError(
2805 VE_RTP_RTCP_MODULE_ERROR, kTraceError,
2806 "SetRTCP_CNAME() failed to set RTCP CNAME");
2807 return -1;
2808 }
2809 return 0;
2810}
2811
2812int
niklase@google.com470e71d2011-07-07 08:21:25 +00002813Channel::GetRemoteRTCP_CNAME(char cName[256])
2814{
2815 if (cName == NULL)
2816 {
2817 _engineStatisticsPtr->SetLastError(
2818 VE_INVALID_ARGUMENT, kTraceError,
2819 "GetRemoteRTCP_CNAME() invalid CNAME input buffer");
2820 return -1;
2821 }
leozwang@webrtc.org813e4b02012-03-01 18:34:25 +00002822 char cname[RTCP_CNAME_SIZE];
wu@webrtc.org822fbd82013-08-15 23:38:54 +00002823 const uint32_t remoteSSRC = rtp_receiver_->SSRC();
pwestin@webrtc.org2853dde2012-05-11 11:08:54 +00002824 if (_rtpRtcpModule->RemoteCNAME(remoteSSRC, cname) != 0)
niklase@google.com470e71d2011-07-07 08:21:25 +00002825 {
2826 _engineStatisticsPtr->SetLastError(
2827 VE_CANNOT_RETRIEVE_CNAME, kTraceError,
2828 "GetRemoteRTCP_CNAME() failed to retrieve remote RTCP CNAME");
2829 return -1;
2830 }
2831 strcpy(cName, cname);
niklase@google.com470e71d2011-07-07 08:21:25 +00002832 return 0;
2833}
2834
2835int
2836Channel::GetRemoteRTCPData(
2837 unsigned int& NTPHigh,
2838 unsigned int& NTPLow,
2839 unsigned int& timestamp,
2840 unsigned int& playoutTimestamp,
2841 unsigned int* jitter,
2842 unsigned short* fractionLost)
2843{
2844 // --- Information from sender info in received Sender Reports
2845
2846 RTCPSenderInfo senderInfo;
pwestin@webrtc.org2853dde2012-05-11 11:08:54 +00002847 if (_rtpRtcpModule->RemoteRTCPStat(&senderInfo) != 0)
niklase@google.com470e71d2011-07-07 08:21:25 +00002848 {
2849 _engineStatisticsPtr->SetLastError(
2850 VE_RTP_RTCP_MODULE_ERROR, kTraceError,
wu@webrtc.orgfcd12b32011-09-15 20:49:50 +00002851 "GetRemoteRTCPData() failed to retrieve sender info for remote "
niklase@google.com470e71d2011-07-07 08:21:25 +00002852 "side");
2853 return -1;
2854 }
2855
2856 // We only utilize 12 out of 20 bytes in the sender info (ignores packet
2857 // and octet count)
2858 NTPHigh = senderInfo.NTPseconds;
2859 NTPLow = senderInfo.NTPfraction;
2860 timestamp = senderInfo.RTPtimeStamp;
2861
niklase@google.com470e71d2011-07-07 08:21:25 +00002862 // --- Locally derived information
2863
2864 // This value is updated on each incoming RTCP packet (0 when no packet
2865 // has been received)
pwestin@webrtc.org1de01352013-04-11 20:23:35 +00002866 playoutTimestamp = playout_timestamp_rtcp_;
niklase@google.com470e71d2011-07-07 08:21:25 +00002867
niklase@google.com470e71d2011-07-07 08:21:25 +00002868 if (NULL != jitter || NULL != fractionLost)
2869 {
perkj@webrtc.orgce5990c2012-01-11 13:00:08 +00002870 // Get all RTCP receiver report blocks that have been received on this
2871 // channel. If we receive RTP packets from a remote source we know the
2872 // remote SSRC and use the report block from him.
2873 // Otherwise use the first report block.
2874 std::vector<RTCPReportBlock> remote_stats;
pwestin@webrtc.org2853dde2012-05-11 11:08:54 +00002875 if (_rtpRtcpModule->RemoteRTCPStat(&remote_stats) != 0 ||
perkj@webrtc.orgce5990c2012-01-11 13:00:08 +00002876 remote_stats.empty()) {
2877 WEBRTC_TRACE(kTraceWarning, kTraceVoice,
2878 VoEId(_instanceId, _channelId),
2879 "GetRemoteRTCPData() failed to measure statistics due"
2880 " to lack of received RTP and/or RTCP packets");
2881 return -1;
niklase@google.com470e71d2011-07-07 08:21:25 +00002882 }
perkj@webrtc.orgce5990c2012-01-11 13:00:08 +00002883
wu@webrtc.org822fbd82013-08-15 23:38:54 +00002884 uint32_t remoteSSRC = rtp_receiver_->SSRC();
perkj@webrtc.orgce5990c2012-01-11 13:00:08 +00002885 std::vector<RTCPReportBlock>::const_iterator it = remote_stats.begin();
2886 for (; it != remote_stats.end(); ++it) {
2887 if (it->remoteSSRC == remoteSSRC)
2888 break;
niklase@google.com470e71d2011-07-07 08:21:25 +00002889 }
perkj@webrtc.orgce5990c2012-01-11 13:00:08 +00002890
2891 if (it == remote_stats.end()) {
2892 // If we have not received any RTCP packets from this SSRC it probably
2893 // means that we have not received any RTP packets.
2894 // Use the first received report block instead.
2895 it = remote_stats.begin();
2896 remoteSSRC = it->remoteSSRC;
niklase@google.com470e71d2011-07-07 08:21:25 +00002897 }
perkj@webrtc.orgce5990c2012-01-11 13:00:08 +00002898
xians@webrtc.org79af7342012-01-31 12:22:14 +00002899 if (jitter) {
2900 *jitter = it->jitter;
xians@webrtc.org79af7342012-01-31 12:22:14 +00002901 }
perkj@webrtc.orgce5990c2012-01-11 13:00:08 +00002902
xians@webrtc.org79af7342012-01-31 12:22:14 +00002903 if (fractionLost) {
2904 *fractionLost = it->fractionLost;
xians@webrtc.org79af7342012-01-31 12:22:14 +00002905 }
niklase@google.com470e71d2011-07-07 08:21:25 +00002906 }
2907 return 0;
2908}
2909
2910int
pbos@webrtc.org92135212013-05-14 08:31:39 +00002911Channel::SendApplicationDefinedRTCPPacket(unsigned char subType,
niklase@google.com470e71d2011-07-07 08:21:25 +00002912 unsigned int name,
2913 const char* data,
2914 unsigned short dataLengthInBytes)
2915{
2916 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId, _channelId),
2917 "Channel::SendApplicationDefinedRTCPPacket()");
henrika@webrtc.org944cbeb2014-03-18 10:32:33 +00002918 if (!channel_state_.Get().sending)
niklase@google.com470e71d2011-07-07 08:21:25 +00002919 {
2920 _engineStatisticsPtr->SetLastError(
2921 VE_NOT_SENDING, kTraceError,
2922 "SendApplicationDefinedRTCPPacket() not sending");
2923 return -1;
2924 }
2925 if (NULL == data)
2926 {
2927 _engineStatisticsPtr->SetLastError(
2928 VE_INVALID_ARGUMENT, kTraceError,
2929 "SendApplicationDefinedRTCPPacket() invalid data value");
2930 return -1;
2931 }
2932 if (dataLengthInBytes % 4 != 0)
2933 {
2934 _engineStatisticsPtr->SetLastError(
2935 VE_INVALID_ARGUMENT, kTraceError,
2936 "SendApplicationDefinedRTCPPacket() invalid length value");
2937 return -1;
2938 }
pwestin@webrtc.org2853dde2012-05-11 11:08:54 +00002939 RTCPMethod status = _rtpRtcpModule->RTCP();
niklase@google.com470e71d2011-07-07 08:21:25 +00002940 if (status == kRtcpOff)
2941 {
2942 _engineStatisticsPtr->SetLastError(
2943 VE_RTCP_ERROR, kTraceError,
2944 "SendApplicationDefinedRTCPPacket() RTCP is disabled");
2945 return -1;
2946 }
2947
2948 // Create and schedule the RTCP APP packet for transmission
pwestin@webrtc.org2853dde2012-05-11 11:08:54 +00002949 if (_rtpRtcpModule->SetRTCPApplicationSpecificData(
niklase@google.com470e71d2011-07-07 08:21:25 +00002950 subType,
2951 name,
2952 (const unsigned char*) data,
2953 dataLengthInBytes) != 0)
2954 {
2955 _engineStatisticsPtr->SetLastError(
2956 VE_SEND_ERROR, kTraceError,
2957 "SendApplicationDefinedRTCPPacket() failed to send RTCP packet");
2958 return -1;
2959 }
2960 return 0;
2961}
2962
2963int
2964Channel::GetRTPStatistics(
2965 unsigned int& averageJitterMs,
2966 unsigned int& maxJitterMs,
2967 unsigned int& discardedPackets)
2968{
niklase@google.com470e71d2011-07-07 08:21:25 +00002969 // The jitter statistics is updated for each received RTP packet and is
2970 // based on received packets.
sprang@webrtc.org54ae4ff2013-12-19 13:26:02 +00002971 if (_rtpRtcpModule->RTCP() == kRtcpOff) {
2972 // If RTCP is off, there is no timed thread in the RTCP module regularly
2973 // generating new stats, trigger the update manually here instead.
2974 StreamStatistician* statistician =
2975 rtp_receive_statistics_->GetStatistician(rtp_receiver_->SSRC());
2976 if (statistician) {
2977 // Don't use returned statistics, use data from proxy instead so that
2978 // max jitter can be fetched atomically.
2979 RtcpStatistics s;
2980 statistician->GetStatistics(&s, true);
2981 }
niklase@google.com470e71d2011-07-07 08:21:25 +00002982 }
2983
sprang@webrtc.org54ae4ff2013-12-19 13:26:02 +00002984 ChannelStatistics stats = statistics_proxy_->GetStats();
andrew@webrtc.orgeb524d92013-09-23 23:02:24 +00002985 const int32_t playoutFrequency = audio_coding_->PlayoutFrequency();
sprang@webrtc.org54ae4ff2013-12-19 13:26:02 +00002986 if (playoutFrequency > 0) {
2987 // Scale RTP statistics given the current playout frequency
2988 maxJitterMs = stats.max_jitter / (playoutFrequency / 1000);
2989 averageJitterMs = stats.rtcp.jitter / (playoutFrequency / 1000);
niklase@google.com470e71d2011-07-07 08:21:25 +00002990 }
2991
2992 discardedPackets = _numberOfDiscardedPackets;
2993
niklase@google.com470e71d2011-07-07 08:21:25 +00002994 return 0;
2995}
2996
henrika@webrtc.org8a2fc882012-08-22 08:53:55 +00002997int Channel::GetRemoteRTCPReportBlocks(
2998 std::vector<ReportBlock>* report_blocks) {
2999 if (report_blocks == NULL) {
3000 _engineStatisticsPtr->SetLastError(VE_INVALID_ARGUMENT, kTraceError,
3001 "GetRemoteRTCPReportBlock()s invalid report_blocks.");
3002 return -1;
3003 }
3004
3005 // Get the report blocks from the latest received RTCP Sender or Receiver
3006 // Report. Each element in the vector contains the sender's SSRC and a
3007 // report block according to RFC 3550.
3008 std::vector<RTCPReportBlock> rtcp_report_blocks;
3009 if (_rtpRtcpModule->RemoteRTCPStat(&rtcp_report_blocks) != 0) {
henrika@webrtc.org8a2fc882012-08-22 08:53:55 +00003010 return -1;
3011 }
3012
3013 if (rtcp_report_blocks.empty())
3014 return 0;
3015
3016 std::vector<RTCPReportBlock>::const_iterator it = rtcp_report_blocks.begin();
3017 for (; it != rtcp_report_blocks.end(); ++it) {
3018 ReportBlock report_block;
3019 report_block.sender_SSRC = it->remoteSSRC;
3020 report_block.source_SSRC = it->sourceSSRC;
3021 report_block.fraction_lost = it->fractionLost;
3022 report_block.cumulative_num_packets_lost = it->cumulativeLost;
3023 report_block.extended_highest_sequence_number = it->extendedHighSeqNum;
3024 report_block.interarrival_jitter = it->jitter;
3025 report_block.last_SR_timestamp = it->lastSR;
3026 report_block.delay_since_last_SR = it->delaySinceLastSR;
3027 report_blocks->push_back(report_block);
3028 }
3029 return 0;
3030}
3031
niklase@google.com470e71d2011-07-07 08:21:25 +00003032int
3033Channel::GetRTPStatistics(CallStatistics& stats)
3034{
wu@webrtc.orgcb711f72014-05-19 17:39:11 +00003035 // --- RtcpStatistics
niklase@google.com470e71d2011-07-07 08:21:25 +00003036
3037 // The jitter statistics is updated for each received RTP packet and is
3038 // based on received packets.
sprang@webrtc.org54ae4ff2013-12-19 13:26:02 +00003039 RtcpStatistics statistics;
stefan@webrtc.org286fe0b2013-08-21 20:58:21 +00003040 StreamStatistician* statistician =
3041 rtp_receive_statistics_->GetStatistician(rtp_receiver_->SSRC());
3042 if (!statistician || !statistician->GetStatistics(
wu@webrtc.org822fbd82013-08-15 23:38:54 +00003043 &statistics, _rtpRtcpModule->RTCP() == kRtcpOff)) {
3044 _engineStatisticsPtr->SetLastError(
3045 VE_CANNOT_RETRIEVE_RTP_STAT, kTraceWarning,
3046 "GetRTPStatistics() failed to read RTP statistics from the "
3047 "RTP/RTCP module");
niklase@google.com470e71d2011-07-07 08:21:25 +00003048 }
3049
wu@webrtc.org822fbd82013-08-15 23:38:54 +00003050 stats.fractionLost = statistics.fraction_lost;
3051 stats.cumulativeLost = statistics.cumulative_lost;
3052 stats.extendedMax = statistics.extended_max_sequence_number;
3053 stats.jitterSamples = statistics.jitter;
niklase@google.com470e71d2011-07-07 08:21:25 +00003054
wu@webrtc.orgcb711f72014-05-19 17:39:11 +00003055 // --- RTT
Minyue2013aec2015-05-13 14:14:42 +02003056 stats.rttMs = GetRTT(true);
niklase@google.com470e71d2011-07-07 08:21:25 +00003057
wu@webrtc.orgcb711f72014-05-19 17:39:11 +00003058 // --- Data counters
niklase@google.com470e71d2011-07-07 08:21:25 +00003059
pkasting@chromium.org4591fbd2014-11-20 22:28:14 +00003060 size_t bytesSent(0);
pbos@webrtc.org6141e132013-04-09 10:09:10 +00003061 uint32_t packetsSent(0);
pkasting@chromium.org4591fbd2014-11-20 22:28:14 +00003062 size_t bytesReceived(0);
pbos@webrtc.org6141e132013-04-09 10:09:10 +00003063 uint32_t packetsReceived(0);
niklase@google.com470e71d2011-07-07 08:21:25 +00003064
stefan@webrtc.org286fe0b2013-08-21 20:58:21 +00003065 if (statistician) {
3066 statistician->GetDataCounters(&bytesReceived, &packetsReceived);
3067 }
wu@webrtc.org822fbd82013-08-15 23:38:54 +00003068
pwestin@webrtc.org2853dde2012-05-11 11:08:54 +00003069 if (_rtpRtcpModule->DataCountersRTP(&bytesSent,
wu@webrtc.org822fbd82013-08-15 23:38:54 +00003070 &packetsSent) != 0)
niklase@google.com470e71d2011-07-07 08:21:25 +00003071 {
3072 WEBRTC_TRACE(kTraceWarning, kTraceVoice,
3073 VoEId(_instanceId, _channelId),
3074 "GetRTPStatistics() failed to retrieve RTP datacounters =>"
wu@webrtc.orgfcd12b32011-09-15 20:49:50 +00003075 " output will not be complete");
niklase@google.com470e71d2011-07-07 08:21:25 +00003076 }
3077
3078 stats.bytesSent = bytesSent;
3079 stats.packetsSent = packetsSent;
3080 stats.bytesReceived = bytesReceived;
3081 stats.packetsReceived = packetsReceived;
3082
wu@webrtc.orgcb711f72014-05-19 17:39:11 +00003083 // --- Timestamps
3084 {
3085 CriticalSectionScoped lock(ts_stats_lock_.get());
3086 stats.capture_start_ntp_time_ms_ = capture_start_ntp_time_ms_;
3087 }
niklase@google.com470e71d2011-07-07 08:21:25 +00003088 return 0;
3089}
3090
minyue@webrtc.orgc1a40a72014-05-28 09:52:06 +00003091int Channel::SetREDStatus(bool enable, int redPayloadtype) {
turaj@webrtc.org42259e72012-12-11 02:15:12 +00003092 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId, _channelId),
minyue@webrtc.orgc1a40a72014-05-28 09:52:06 +00003093 "Channel::SetREDStatus()");
niklase@google.com470e71d2011-07-07 08:21:25 +00003094
turaj@webrtc.org8c8ad852013-01-31 18:20:17 +00003095 if (enable) {
3096 if (redPayloadtype < 0 || redPayloadtype > 127) {
3097 _engineStatisticsPtr->SetLastError(
3098 VE_PLTYPE_ERROR, kTraceError,
minyue@webrtc.orgc1a40a72014-05-28 09:52:06 +00003099 "SetREDStatus() invalid RED payload type");
turaj@webrtc.org8c8ad852013-01-31 18:20:17 +00003100 return -1;
3101 }
3102
3103 if (SetRedPayloadType(redPayloadtype) < 0) {
3104 _engineStatisticsPtr->SetLastError(
3105 VE_CODEC_ERROR, kTraceError,
3106 "SetSecondarySendCodec() Failed to register RED ACM");
3107 return -1;
3108 }
turaj@webrtc.org42259e72012-12-11 02:15:12 +00003109 }
niklase@google.com470e71d2011-07-07 08:21:25 +00003110
minyue@webrtc.orgaa5ea1c2014-05-23 15:16:51 +00003111 if (audio_coding_->SetREDStatus(enable) != 0) {
turaj@webrtc.org42259e72012-12-11 02:15:12 +00003112 _engineStatisticsPtr->SetLastError(
3113 VE_AUDIO_CODING_MODULE_ERROR, kTraceError,
minyue@webrtc.orgaa5ea1c2014-05-23 15:16:51 +00003114 "SetREDStatus() failed to set RED state in the ACM");
turaj@webrtc.org42259e72012-12-11 02:15:12 +00003115 return -1;
3116 }
3117 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +00003118}
3119
3120int
minyue@webrtc.orgc1a40a72014-05-28 09:52:06 +00003121Channel::GetREDStatus(bool& enabled, int& redPayloadtype)
niklase@google.com470e71d2011-07-07 08:21:25 +00003122{
minyue@webrtc.orgaa5ea1c2014-05-23 15:16:51 +00003123 enabled = audio_coding_->REDStatus();
niklase@google.com470e71d2011-07-07 08:21:25 +00003124 if (enabled)
3125 {
pbos@webrtc.org6141e132013-04-09 10:09:10 +00003126 int8_t payloadType(0);
pwestin@webrtc.org2853dde2012-05-11 11:08:54 +00003127 if (_rtpRtcpModule->SendREDPayloadType(payloadType) != 0)
niklase@google.com470e71d2011-07-07 08:21:25 +00003128 {
3129 _engineStatisticsPtr->SetLastError(
3130 VE_RTP_RTCP_MODULE_ERROR, kTraceError,
minyue@webrtc.orgc1a40a72014-05-28 09:52:06 +00003131 "GetREDStatus() failed to retrieve RED PT from RTP/RTCP "
niklase@google.com470e71d2011-07-07 08:21:25 +00003132 "module");
3133 return -1;
3134 }
pkasting@chromium.orgdf9a41d2015-01-26 22:35:29 +00003135 redPayloadtype = payloadType;
niklase@google.com470e71d2011-07-07 08:21:25 +00003136 return 0;
3137 }
niklase@google.com470e71d2011-07-07 08:21:25 +00003138 return 0;
3139}
3140
minyue@webrtc.orgc1a40a72014-05-28 09:52:06 +00003141int Channel::SetCodecFECStatus(bool enable) {
3142 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId, _channelId),
3143 "Channel::SetCodecFECStatus()");
3144
3145 if (audio_coding_->SetCodecFEC(enable) != 0) {
3146 _engineStatisticsPtr->SetLastError(
3147 VE_AUDIO_CODING_MODULE_ERROR, kTraceError,
3148 "SetCodecFECStatus() failed to set FEC state");
3149 return -1;
3150 }
3151 return 0;
3152}
3153
3154bool Channel::GetCodecFECStatus() {
3155 bool enabled = audio_coding_->CodecFEC();
minyue@webrtc.orgc1a40a72014-05-28 09:52:06 +00003156 return enabled;
3157}
3158
pwestin@webrtc.orgdb249952013-06-05 15:33:20 +00003159void Channel::SetNACKStatus(bool enable, int maxNumberOfPackets) {
3160 // None of these functions can fail.
3161 _rtpRtcpModule->SetStorePacketsStatus(enable, maxNumberOfPackets);
stefan@webrtc.org7bb8f022013-09-06 13:40:11 +00003162 rtp_receive_statistics_->SetMaxReorderingThreshold(maxNumberOfPackets);
3163 rtp_receiver_->SetNACKStatus(enable ? kNackRtcp : kNackOff);
pwestin@webrtc.orgd30859e2013-06-06 21:09:01 +00003164 if (enable)
andrew@webrtc.orgeb524d92013-09-23 23:02:24 +00003165 audio_coding_->EnableNack(maxNumberOfPackets);
pwestin@webrtc.orgd30859e2013-06-06 21:09:01 +00003166 else
andrew@webrtc.orgeb524d92013-09-23 23:02:24 +00003167 audio_coding_->DisableNack();
pwestin@webrtc.orgdb249952013-06-05 15:33:20 +00003168}
3169
pwestin@webrtc.orgd30859e2013-06-06 21:09:01 +00003170// Called when we are missing one or more packets.
3171int Channel::ResendPackets(const uint16_t* sequence_numbers, int length) {
pwestin@webrtc.orgdb249952013-06-05 15:33:20 +00003172 return _rtpRtcpModule->SendNACK(sequence_numbers, length);
3173}
3174
pbos@webrtc.org6141e132013-04-09 10:09:10 +00003175uint32_t
andrew@webrtc.org755b04a2011-11-15 16:57:56 +00003176Channel::Demultiplex(const AudioFrame& audioFrame)
niklase@google.com470e71d2011-07-07 08:21:25 +00003177{
3178 WEBRTC_TRACE(kTraceStream, kTraceVoice, VoEId(_instanceId,_channelId),
andrew@webrtc.org755b04a2011-11-15 16:57:56 +00003179 "Channel::Demultiplex()");
andrew@webrtc.orgae1a58b2013-01-22 04:44:30 +00003180 _audioFrame.CopyFrom(audioFrame);
andrew@webrtc.org63a50982012-05-02 23:56:37 +00003181 _audioFrame.id_ = _channelId;
niklase@google.com470e71d2011-07-07 08:21:25 +00003182 return 0;
3183}
3184
xians@webrtc.org2f84afa2013-07-31 16:23:37 +00003185void Channel::Demultiplex(const int16_t* audio_data,
xians@webrtc.org8fff1f02013-07-31 16:27:42 +00003186 int sample_rate,
Peter Kastingdce40cf2015-08-24 14:52:23 -07003187 size_t number_of_frames,
xians@webrtc.org8fff1f02013-07-31 16:27:42 +00003188 int number_of_channels) {
xians@webrtc.org2f84afa2013-07-31 16:23:37 +00003189 CodecInst codec;
3190 GetSendCodec(codec);
xians@webrtc.org2f84afa2013-07-31 16:23:37 +00003191
Alejandro Luebscdfe20b2015-09-23 12:49:12 -07003192 // Never upsample or upmix the capture signal here. This should be done at the
3193 // end of the send chain.
3194 _audioFrame.sample_rate_hz_ = std::min(codec.plfreq, sample_rate);
3195 _audioFrame.num_channels_ = std::min(number_of_channels, codec.channels);
3196 RemixAndResample(audio_data, number_of_frames, number_of_channels,
3197 sample_rate, &input_resampler_, &_audioFrame);
xians@webrtc.org2f84afa2013-07-31 16:23:37 +00003198}
3199
pbos@webrtc.org6141e132013-04-09 10:09:10 +00003200uint32_t
xians@google.com0b0665a2011-08-08 08:18:44 +00003201Channel::PrepareEncodeAndSend(int mixingFrequency)
niklase@google.com470e71d2011-07-07 08:21:25 +00003202{
3203 WEBRTC_TRACE(kTraceStream, kTraceVoice, VoEId(_instanceId,_channelId),
3204 "Channel::PrepareEncodeAndSend()");
3205
andrew@webrtc.org63a50982012-05-02 23:56:37 +00003206 if (_audioFrame.samples_per_channel_ == 0)
niklase@google.com470e71d2011-07-07 08:21:25 +00003207 {
3208 WEBRTC_TRACE(kTraceWarning, kTraceVoice, VoEId(_instanceId,_channelId),
3209 "Channel::PrepareEncodeAndSend() invalid audio frame");
tommi@webrtc.orgeec6ecd2014-07-11 19:09:59 +00003210 return 0xFFFFFFFF;
niklase@google.com470e71d2011-07-07 08:21:25 +00003211 }
3212
henrika@webrtc.org944cbeb2014-03-18 10:32:33 +00003213 if (channel_state_.Get().input_file_playing)
niklase@google.com470e71d2011-07-07 08:21:25 +00003214 {
3215 MixOrReplaceAudioWithFile(mixingFrequency);
3216 }
3217
andrew@webrtc.org21299d42014-05-14 19:00:59 +00003218 bool is_muted = Mute(); // Cache locally as Mute() takes a lock.
3219 if (is_muted) {
3220 AudioFrameOperations::Mute(_audioFrame);
niklase@google.com470e71d2011-07-07 08:21:25 +00003221 }
3222
henrika@webrtc.org944cbeb2014-03-18 10:32:33 +00003223 if (channel_state_.Get().input_external_media)
niklase@google.com470e71d2011-07-07 08:21:25 +00003224 {
mflodman@webrtc.org9a065d12012-03-07 08:12:21 +00003225 CriticalSectionScoped cs(&_callbackCritSect);
andrew@webrtc.org63a50982012-05-02 23:56:37 +00003226 const bool isStereo = (_audioFrame.num_channels_ == 2);
niklase@google.com470e71d2011-07-07 08:21:25 +00003227 if (_inputExternalMediaCallbackPtr)
3228 {
3229 _inputExternalMediaCallbackPtr->Process(
3230 _channelId,
3231 kRecordingPerChannel,
pbos@webrtc.org6141e132013-04-09 10:09:10 +00003232 (int16_t*)_audioFrame.data_,
andrew@webrtc.org63a50982012-05-02 23:56:37 +00003233 _audioFrame.samples_per_channel_,
3234 _audioFrame.sample_rate_hz_,
niklase@google.com470e71d2011-07-07 08:21:25 +00003235 isStereo);
3236 }
3237 }
3238
3239 InsertInbandDtmfTone();
3240
andrew@webrtc.org60730cf2014-01-07 17:45:09 +00003241 if (_includeAudioLevelIndication) {
Peter Kastingdce40cf2015-08-24 14:52:23 -07003242 size_t length =
3243 _audioFrame.samples_per_channel_ * _audioFrame.num_channels_;
andrew@webrtc.org21299d42014-05-14 19:00:59 +00003244 if (is_muted) {
3245 rms_level_.ProcessMuted(length);
3246 } else {
3247 rms_level_.Process(_audioFrame.data_, length);
3248 }
andrew@webrtc.org755b04a2011-11-15 16:57:56 +00003249 }
3250
niklase@google.com470e71d2011-07-07 08:21:25 +00003251 return 0;
3252}
3253
pbos@webrtc.org6141e132013-04-09 10:09:10 +00003254uint32_t
niklase@google.com470e71d2011-07-07 08:21:25 +00003255Channel::EncodeAndSend()
3256{
3257 WEBRTC_TRACE(kTraceStream, kTraceVoice, VoEId(_instanceId,_channelId),
3258 "Channel::EncodeAndSend()");
3259
andrew@webrtc.org63a50982012-05-02 23:56:37 +00003260 assert(_audioFrame.num_channels_ <= 2);
3261 if (_audioFrame.samples_per_channel_ == 0)
niklase@google.com470e71d2011-07-07 08:21:25 +00003262 {
3263 WEBRTC_TRACE(kTraceWarning, kTraceVoice, VoEId(_instanceId,_channelId),
3264 "Channel::EncodeAndSend() invalid audio frame");
tommi@webrtc.orgeec6ecd2014-07-11 19:09:59 +00003265 return 0xFFFFFFFF;
niklase@google.com470e71d2011-07-07 08:21:25 +00003266 }
3267
andrew@webrtc.org63a50982012-05-02 23:56:37 +00003268 _audioFrame.id_ = _channelId;
niklase@google.com470e71d2011-07-07 08:21:25 +00003269
3270 // --- Add 10ms of raw (PCM) audio data to the encoder @ 32kHz.
3271
3272 // The ACM resamples internally.
andrew@webrtc.org63a50982012-05-02 23:56:37 +00003273 _audioFrame.timestamp_ = _timeStamp;
henrik.lundin@webrtc.orgf56c1622015-03-02 12:29:30 +00003274 // This call will trigger AudioPacketizationCallback::SendData if encoding
3275 // is done and payload is ready for packetization and transmission.
3276 // Otherwise, it will return without invoking the callback.
3277 if (audio_coding_->Add10MsData((AudioFrame&)_audioFrame) < 0)
niklase@google.com470e71d2011-07-07 08:21:25 +00003278 {
3279 WEBRTC_TRACE(kTraceError, kTraceVoice, VoEId(_instanceId,_channelId),
3280 "Channel::EncodeAndSend() ACM encoding failed");
tommi@webrtc.orgeec6ecd2014-07-11 19:09:59 +00003281 return 0xFFFFFFFF;
niklase@google.com470e71d2011-07-07 08:21:25 +00003282 }
3283
Peter Kastingb7e50542015-06-11 12:55:50 -07003284 _timeStamp += static_cast<uint32_t>(_audioFrame.samples_per_channel_);
henrik.lundin@webrtc.orgf56c1622015-03-02 12:29:30 +00003285 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +00003286}
3287
Minyue2013aec2015-05-13 14:14:42 +02003288void Channel::DisassociateSendChannel(int channel_id) {
3289 CriticalSectionScoped lock(assoc_send_channel_lock_.get());
3290 Channel* channel = associate_send_channel_.channel();
3291 if (channel && channel->ChannelId() == channel_id) {
3292 // If this channel is associated with a send channel of the specified
3293 // Channel ID, disassociate with it.
3294 ChannelOwner ref(NULL);
3295 associate_send_channel_ = ref;
3296 }
3297}
3298
niklase@google.com470e71d2011-07-07 08:21:25 +00003299int Channel::RegisterExternalMediaProcessing(
3300 ProcessingTypes type,
3301 VoEMediaProcess& processObject)
3302{
3303 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId,_channelId),
3304 "Channel::RegisterExternalMediaProcessing()");
3305
mflodman@webrtc.org9a065d12012-03-07 08:12:21 +00003306 CriticalSectionScoped cs(&_callbackCritSect);
niklase@google.com470e71d2011-07-07 08:21:25 +00003307
3308 if (kPlaybackPerChannel == type)
3309 {
3310 if (_outputExternalMediaCallbackPtr)
3311 {
3312 _engineStatisticsPtr->SetLastError(
3313 VE_INVALID_OPERATION, kTraceError,
3314 "Channel::RegisterExternalMediaProcessing() "
3315 "output external media already enabled");
3316 return -1;
3317 }
3318 _outputExternalMediaCallbackPtr = &processObject;
3319 _outputExternalMedia = true;
3320 }
3321 else if (kRecordingPerChannel == type)
3322 {
3323 if (_inputExternalMediaCallbackPtr)
3324 {
3325 _engineStatisticsPtr->SetLastError(
3326 VE_INVALID_OPERATION, kTraceError,
3327 "Channel::RegisterExternalMediaProcessing() "
3328 "output external media already enabled");
3329 return -1;
3330 }
3331 _inputExternalMediaCallbackPtr = &processObject;
henrika@webrtc.org944cbeb2014-03-18 10:32:33 +00003332 channel_state_.SetInputExternalMedia(true);
niklase@google.com470e71d2011-07-07 08:21:25 +00003333 }
3334 return 0;
3335}
3336
3337int Channel::DeRegisterExternalMediaProcessing(ProcessingTypes type)
3338{
3339 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId,_channelId),
3340 "Channel::DeRegisterExternalMediaProcessing()");
3341
mflodman@webrtc.org9a065d12012-03-07 08:12:21 +00003342 CriticalSectionScoped cs(&_callbackCritSect);
niklase@google.com470e71d2011-07-07 08:21:25 +00003343
3344 if (kPlaybackPerChannel == type)
3345 {
3346 if (!_outputExternalMediaCallbackPtr)
3347 {
3348 _engineStatisticsPtr->SetLastError(
3349 VE_INVALID_OPERATION, kTraceWarning,
3350 "Channel::DeRegisterExternalMediaProcessing() "
3351 "output external media already disabled");
3352 return 0;
3353 }
3354 _outputExternalMedia = false;
3355 _outputExternalMediaCallbackPtr = NULL;
3356 }
3357 else if (kRecordingPerChannel == type)
3358 {
3359 if (!_inputExternalMediaCallbackPtr)
3360 {
3361 _engineStatisticsPtr->SetLastError(
3362 VE_INVALID_OPERATION, kTraceWarning,
3363 "Channel::DeRegisterExternalMediaProcessing() "
3364 "input external media already disabled");
3365 return 0;
3366 }
henrika@webrtc.org944cbeb2014-03-18 10:32:33 +00003367 channel_state_.SetInputExternalMedia(false);
niklase@google.com470e71d2011-07-07 08:21:25 +00003368 _inputExternalMediaCallbackPtr = NULL;
3369 }
3370
3371 return 0;
3372}
3373
roosa@google.com1b60ceb2012-12-12 23:00:29 +00003374int Channel::SetExternalMixing(bool enabled) {
3375 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId,_channelId),
3376 "Channel::SetExternalMixing(enabled=%d)", enabled);
3377
henrika@webrtc.org944cbeb2014-03-18 10:32:33 +00003378 if (channel_state_.Get().playing)
roosa@google.com1b60ceb2012-12-12 23:00:29 +00003379 {
3380 _engineStatisticsPtr->SetLastError(
3381 VE_INVALID_OPERATION, kTraceError,
3382 "Channel::SetExternalMixing() "
3383 "external mixing cannot be changed while playing.");
3384 return -1;
3385 }
3386
3387 _externalMixing = enabled;
3388
3389 return 0;
3390}
3391
niklase@google.com470e71d2011-07-07 08:21:25 +00003392int
niklase@google.com470e71d2011-07-07 08:21:25 +00003393Channel::GetNetworkStatistics(NetworkStatistics& stats)
3394{
minyue@webrtc.orgc0bd7be2015-02-18 15:24:13 +00003395 return audio_coding_->GetNetworkStatistics(&stats);
niklase@google.com470e71d2011-07-07 08:21:25 +00003396}
3397
wu@webrtc.org24301a62013-12-13 19:17:43 +00003398void Channel::GetDecodingCallStatistics(AudioDecodingCallStats* stats) const {
3399 audio_coding_->GetDecodingCallStatistics(stats);
3400}
3401
pwestin@webrtc.org1de01352013-04-11 20:23:35 +00003402bool Channel::GetDelayEstimate(int* jitter_buffer_delay_ms,
3403 int* playout_buffer_delay_ms) const {
deadbeef74375882015-08-13 12:09:10 -07003404 CriticalSectionScoped cs(video_sync_lock_.get());
pwestin@webrtc.org1de01352013-04-11 20:23:35 +00003405 if (_average_jitter_buffer_delay_us == 0) {
pwestin@webrtc.org1de01352013-04-11 20:23:35 +00003406 return false;
3407 }
3408 *jitter_buffer_delay_ms = (_average_jitter_buffer_delay_us + 500) / 1000 +
3409 _recPacketDelayMs;
3410 *playout_buffer_delay_ms = playout_delay_ms_;
pwestin@webrtc.org1de01352013-04-11 20:23:35 +00003411 return true;
niklase@google.com470e71d2011-07-07 08:21:25 +00003412}
3413
deadbeef74375882015-08-13 12:09:10 -07003414int Channel::LeastRequiredDelayMs() const {
3415 return audio_coding_->LeastRequiredDelayMs();
3416}
3417
turaj@webrtc.org6388c3e2013-02-12 21:42:18 +00003418int Channel::SetInitialPlayoutDelay(int delay_ms)
3419{
3420 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId,_channelId),
3421 "Channel::SetInitialPlayoutDelay()");
3422 if ((delay_ms < kVoiceEngineMinMinPlayoutDelayMs) ||
3423 (delay_ms > kVoiceEngineMaxMinPlayoutDelayMs))
3424 {
3425 _engineStatisticsPtr->SetLastError(
3426 VE_INVALID_ARGUMENT, kTraceError,
3427 "SetInitialPlayoutDelay() invalid min delay");
3428 return -1;
3429 }
andrew@webrtc.orgeb524d92013-09-23 23:02:24 +00003430 if (audio_coding_->SetInitialPlayoutDelay(delay_ms) != 0)
turaj@webrtc.org6388c3e2013-02-12 21:42:18 +00003431 {
3432 _engineStatisticsPtr->SetLastError(
3433 VE_AUDIO_CODING_MODULE_ERROR, kTraceError,
3434 "SetInitialPlayoutDelay() failed to set min playout delay");
3435 return -1;
3436 }
3437 return 0;
3438}
3439
3440
niklase@google.com470e71d2011-07-07 08:21:25 +00003441int
3442Channel::SetMinimumPlayoutDelay(int delayMs)
3443{
3444 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId,_channelId),
3445 "Channel::SetMinimumPlayoutDelay()");
3446 if ((delayMs < kVoiceEngineMinMinPlayoutDelayMs) ||
3447 (delayMs > kVoiceEngineMaxMinPlayoutDelayMs))
3448 {
3449 _engineStatisticsPtr->SetLastError(
3450 VE_INVALID_ARGUMENT, kTraceError,
3451 "SetMinimumPlayoutDelay() invalid min delay");
3452 return -1;
3453 }
andrew@webrtc.orgeb524d92013-09-23 23:02:24 +00003454 if (audio_coding_->SetMinimumPlayoutDelay(delayMs) != 0)
niklase@google.com470e71d2011-07-07 08:21:25 +00003455 {
3456 _engineStatisticsPtr->SetLastError(
3457 VE_AUDIO_CODING_MODULE_ERROR, kTraceError,
3458 "SetMinimumPlayoutDelay() failed to set min playout delay");
3459 return -1;
3460 }
3461 return 0;
3462}
3463
pwestin@webrtc.org1de01352013-04-11 20:23:35 +00003464int Channel::GetPlayoutTimestamp(unsigned int& timestamp) {
deadbeef74375882015-08-13 12:09:10 -07003465 uint32_t playout_timestamp_rtp = 0;
3466 {
3467 CriticalSectionScoped cs(video_sync_lock_.get());
3468 playout_timestamp_rtp = playout_timestamp_rtp_;
3469 }
3470 if (playout_timestamp_rtp == 0) {
pwestin@webrtc.org1de01352013-04-11 20:23:35 +00003471 _engineStatisticsPtr->SetLastError(
3472 VE_CANNOT_RETRIEVE_VALUE, kTraceError,
3473 "GetPlayoutTimestamp() failed to retrieve timestamp");
3474 return -1;
3475 }
deadbeef74375882015-08-13 12:09:10 -07003476 timestamp = playout_timestamp_rtp;
pwestin@webrtc.org1de01352013-04-11 20:23:35 +00003477 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +00003478}
3479
pbos@webrtc.orgd16e8392014-12-19 13:49:55 +00003480int Channel::SetInitTimestamp(unsigned int timestamp) {
3481 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId, _channelId),
niklase@google.com470e71d2011-07-07 08:21:25 +00003482 "Channel::SetInitTimestamp()");
pbos@webrtc.orgd16e8392014-12-19 13:49:55 +00003483 if (channel_state_.Get().sending) {
3484 _engineStatisticsPtr->SetLastError(VE_SENDING, kTraceError,
3485 "SetInitTimestamp() already sending");
3486 return -1;
3487 }
3488 _rtpRtcpModule->SetStartTimestamp(timestamp);
3489 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +00003490}
3491
pbos@webrtc.orgd16e8392014-12-19 13:49:55 +00003492int Channel::SetInitSequenceNumber(short sequenceNumber) {
3493 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId, _channelId),
3494 "Channel::SetInitSequenceNumber()");
3495 if (channel_state_.Get().sending) {
3496 _engineStatisticsPtr->SetLastError(
3497 VE_SENDING, kTraceError, "SetInitSequenceNumber() already sending");
3498 return -1;
3499 }
3500 _rtpRtcpModule->SetSequenceNumber(sequenceNumber);
3501 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +00003502}
3503
3504int
wu@webrtc.org822fbd82013-08-15 23:38:54 +00003505Channel::GetRtpRtcp(RtpRtcp** rtpRtcpModule, RtpReceiver** rtp_receiver) const
niklase@google.com470e71d2011-07-07 08:21:25 +00003506{
wu@webrtc.org822fbd82013-08-15 23:38:54 +00003507 *rtpRtcpModule = _rtpRtcpModule.get();
3508 *rtp_receiver = rtp_receiver_.get();
niklase@google.com470e71d2011-07-07 08:21:25 +00003509 return 0;
3510}
3511
andrew@webrtc.orge59a0ac2012-05-08 17:12:40 +00003512// TODO(andrew): refactor Mix functions here and in transmit_mixer.cc to use
3513// a shared helper.
pbos@webrtc.org6141e132013-04-09 10:09:10 +00003514int32_t
pbos@webrtc.org92135212013-05-14 08:31:39 +00003515Channel::MixOrReplaceAudioWithFile(int mixingFrequency)
niklase@google.com470e71d2011-07-07 08:21:25 +00003516{
kwiberg@webrtc.org00b8f6b2015-02-26 14:34:55 +00003517 rtc::scoped_ptr<int16_t[]> fileBuffer(new int16_t[640]);
Peter Kastingdce40cf2015-08-24 14:52:23 -07003518 size_t fileSamples(0);
niklase@google.com470e71d2011-07-07 08:21:25 +00003519
3520 {
mflodman@webrtc.org9a065d12012-03-07 08:12:21 +00003521 CriticalSectionScoped cs(&_fileCritSect);
niklase@google.com470e71d2011-07-07 08:21:25 +00003522
3523 if (_inputFilePlayerPtr == NULL)
3524 {
3525 WEBRTC_TRACE(kTraceWarning, kTraceVoice,
3526 VoEId(_instanceId, _channelId),
3527 "Channel::MixOrReplaceAudioWithFile() fileplayer"
3528 " doesnt exist");
3529 return -1;
3530 }
3531
braveyao@webrtc.orgd7131432012-03-29 10:39:44 +00003532 if (_inputFilePlayerPtr->Get10msAudioFromFile(fileBuffer.get(),
niklase@google.com470e71d2011-07-07 08:21:25 +00003533 fileSamples,
3534 mixingFrequency) == -1)
3535 {
3536 WEBRTC_TRACE(kTraceWarning, kTraceVoice,
3537 VoEId(_instanceId, _channelId),
3538 "Channel::MixOrReplaceAudioWithFile() file mixing "
3539 "failed");
3540 return -1;
3541 }
3542 if (fileSamples == 0)
3543 {
3544 WEBRTC_TRACE(kTraceWarning, kTraceVoice,
3545 VoEId(_instanceId, _channelId),
3546 "Channel::MixOrReplaceAudioWithFile() file is ended");
3547 return 0;
3548 }
3549 }
3550
andrew@webrtc.org63a50982012-05-02 23:56:37 +00003551 assert(_audioFrame.samples_per_channel_ == fileSamples);
niklase@google.com470e71d2011-07-07 08:21:25 +00003552
3553 if (_mixFileWithMicrophone)
3554 {
braveyao@webrtc.orgd7131432012-03-29 10:39:44 +00003555 // Currently file stream is always mono.
3556 // TODO(xians): Change the code when FilePlayer supports real stereo.
andrew@webrtc.org40ee3d02014-04-03 21:56:01 +00003557 MixWithSat(_audioFrame.data_,
3558 _audioFrame.num_channels_,
3559 fileBuffer.get(),
3560 1,
3561 fileSamples);
niklase@google.com470e71d2011-07-07 08:21:25 +00003562 }
3563 else
3564 {
braveyao@webrtc.orgd7131432012-03-29 10:39:44 +00003565 // Replace ACM audio with file.
3566 // Currently file stream is always mono.
3567 // TODO(xians): Change the code when FilePlayer supports real stereo.
niklase@google.com470e71d2011-07-07 08:21:25 +00003568 _audioFrame.UpdateFrame(_channelId,
tommi@webrtc.orgeec6ecd2014-07-11 19:09:59 +00003569 0xFFFFFFFF,
braveyao@webrtc.orgd7131432012-03-29 10:39:44 +00003570 fileBuffer.get(),
andrew@webrtc.orge59a0ac2012-05-08 17:12:40 +00003571 fileSamples,
niklase@google.com470e71d2011-07-07 08:21:25 +00003572 mixingFrequency,
3573 AudioFrame::kNormalSpeech,
3574 AudioFrame::kVadUnknown,
3575 1);
3576
3577 }
3578 return 0;
3579}
3580
pbos@webrtc.org6141e132013-04-09 10:09:10 +00003581int32_t
niklase@google.com470e71d2011-07-07 08:21:25 +00003582Channel::MixAudioWithFile(AudioFrame& audioFrame,
pbos@webrtc.org92135212013-05-14 08:31:39 +00003583 int mixingFrequency)
niklase@google.com470e71d2011-07-07 08:21:25 +00003584{
minyue@webrtc.org2a8df7c2014-08-06 10:05:19 +00003585 assert(mixingFrequency <= 48000);
niklase@google.com470e71d2011-07-07 08:21:25 +00003586
kwiberg@webrtc.org00b8f6b2015-02-26 14:34:55 +00003587 rtc::scoped_ptr<int16_t[]> fileBuffer(new int16_t[960]);
Peter Kastingdce40cf2015-08-24 14:52:23 -07003588 size_t fileSamples(0);
niklase@google.com470e71d2011-07-07 08:21:25 +00003589
3590 {
mflodman@webrtc.org9a065d12012-03-07 08:12:21 +00003591 CriticalSectionScoped cs(&_fileCritSect);
niklase@google.com470e71d2011-07-07 08:21:25 +00003592
3593 if (_outputFilePlayerPtr == NULL)
3594 {
3595 WEBRTC_TRACE(kTraceWarning, kTraceVoice,
3596 VoEId(_instanceId, _channelId),
3597 "Channel::MixAudioWithFile() file mixing failed");
3598 return -1;
3599 }
3600
3601 // We should get the frequency we ask for.
braveyao@webrtc.orgd7131432012-03-29 10:39:44 +00003602 if (_outputFilePlayerPtr->Get10msAudioFromFile(fileBuffer.get(),
niklase@google.com470e71d2011-07-07 08:21:25 +00003603 fileSamples,
3604 mixingFrequency) == -1)
3605 {
3606 WEBRTC_TRACE(kTraceWarning, kTraceVoice,
3607 VoEId(_instanceId, _channelId),
3608 "Channel::MixAudioWithFile() file mixing failed");
3609 return -1;
3610 }
3611 }
3612
andrew@webrtc.org63a50982012-05-02 23:56:37 +00003613 if (audioFrame.samples_per_channel_ == fileSamples)
niklase@google.com470e71d2011-07-07 08:21:25 +00003614 {
braveyao@webrtc.orgd7131432012-03-29 10:39:44 +00003615 // Currently file stream is always mono.
3616 // TODO(xians): Change the code when FilePlayer supports real stereo.
andrew@webrtc.org40ee3d02014-04-03 21:56:01 +00003617 MixWithSat(audioFrame.data_,
3618 audioFrame.num_channels_,
3619 fileBuffer.get(),
3620 1,
3621 fileSamples);
niklase@google.com470e71d2011-07-07 08:21:25 +00003622 }
3623 else
3624 {
3625 WEBRTC_TRACE(kTraceWarning, kTraceVoice, VoEId(_instanceId,_channelId),
Peter Kastingdce40cf2015-08-24 14:52:23 -07003626 "Channel::MixAudioWithFile() samples_per_channel_(%" PRIuS ") != "
3627 "fileSamples(%" PRIuS ")",
andrew@webrtc.org63a50982012-05-02 23:56:37 +00003628 audioFrame.samples_per_channel_, fileSamples);
niklase@google.com470e71d2011-07-07 08:21:25 +00003629 return -1;
3630 }
3631
3632 return 0;
3633}
3634
3635int
3636Channel::InsertInbandDtmfTone()
3637{
niklas.enbom@webrtc.orgaf26f642011-11-16 12:41:36 +00003638 // Check if we should start a new tone.
niklase@google.com470e71d2011-07-07 08:21:25 +00003639 if (_inbandDtmfQueue.PendingDtmf() &&
3640 !_inbandDtmfGenerator.IsAddingTone() &&
3641 _inbandDtmfGenerator.DelaySinceLastTone() >
3642 kMinTelephoneEventSeparationMs)
3643 {
pbos@webrtc.org6141e132013-04-09 10:09:10 +00003644 int8_t eventCode(0);
3645 uint16_t lengthMs(0);
3646 uint8_t attenuationDb(0);
niklase@google.com470e71d2011-07-07 08:21:25 +00003647
3648 eventCode = _inbandDtmfQueue.NextDtmf(&lengthMs, &attenuationDb);
3649 _inbandDtmfGenerator.AddTone(eventCode, lengthMs, attenuationDb);
3650 if (_playInbandDtmfEvent)
3651 {
3652 // Add tone to output mixer using a reduced length to minimize
3653 // risk of echo.
3654 _outputMixerPtr->PlayDtmfTone(eventCode, lengthMs - 80,
3655 attenuationDb);
3656 }
3657 }
3658
3659 if (_inbandDtmfGenerator.IsAddingTone())
3660 {
pbos@webrtc.org6141e132013-04-09 10:09:10 +00003661 uint16_t frequency(0);
niklase@google.com470e71d2011-07-07 08:21:25 +00003662 _inbandDtmfGenerator.GetSampleRate(frequency);
3663
andrew@webrtc.org63a50982012-05-02 23:56:37 +00003664 if (frequency != _audioFrame.sample_rate_hz_)
niklase@google.com470e71d2011-07-07 08:21:25 +00003665 {
3666 // Update sample rate of Dtmf tone since the mixing frequency
3667 // has changed.
3668 _inbandDtmfGenerator.SetSampleRate(
pbos@webrtc.org6141e132013-04-09 10:09:10 +00003669 (uint16_t) (_audioFrame.sample_rate_hz_));
niklase@google.com470e71d2011-07-07 08:21:25 +00003670 // Reset the tone to be added taking the new sample rate into
3671 // account.
3672 _inbandDtmfGenerator.ResetTone();
3673 }
andrew@webrtc.orgae1a58b2013-01-22 04:44:30 +00003674
pbos@webrtc.org6141e132013-04-09 10:09:10 +00003675 int16_t toneBuffer[320];
3676 uint16_t toneSamples(0);
niklase@google.com470e71d2011-07-07 08:21:25 +00003677 // Get 10ms tone segment and set time since last tone to zero
3678 if (_inbandDtmfGenerator.Get10msTone(toneBuffer, toneSamples) == -1)
3679 {
3680 WEBRTC_TRACE(kTraceWarning, kTraceVoice,
3681 VoEId(_instanceId, _channelId),
3682 "Channel::EncodeAndSend() inserting Dtmf failed");
3683 return -1;
3684 }
3685
niklas.enbom@webrtc.orgaf26f642011-11-16 12:41:36 +00003686 // Replace mixed audio with DTMF tone.
Peter Kastingdce40cf2015-08-24 14:52:23 -07003687 for (size_t sample = 0;
andrew@webrtc.org63a50982012-05-02 23:56:37 +00003688 sample < _audioFrame.samples_per_channel_;
niklas.enbom@webrtc.orgaf26f642011-11-16 12:41:36 +00003689 sample++)
3690 {
andrew@webrtc.orgae1a58b2013-01-22 04:44:30 +00003691 for (int channel = 0;
3692 channel < _audioFrame.num_channels_;
niklas.enbom@webrtc.orgaf26f642011-11-16 12:41:36 +00003693 channel++)
3694 {
Peter Kastingdce40cf2015-08-24 14:52:23 -07003695 const size_t index =
3696 sample * _audioFrame.num_channels_ + channel;
andrew@webrtc.orgae1a58b2013-01-22 04:44:30 +00003697 _audioFrame.data_[index] = toneBuffer[sample];
niklas.enbom@webrtc.orgaf26f642011-11-16 12:41:36 +00003698 }
3699 }
andrew@webrtc.orgae1a58b2013-01-22 04:44:30 +00003700
andrew@webrtc.org63a50982012-05-02 23:56:37 +00003701 assert(_audioFrame.samples_per_channel_ == toneSamples);
niklase@google.com470e71d2011-07-07 08:21:25 +00003702 } else
3703 {
3704 // Add 10ms to "delay-since-last-tone" counter
3705 _inbandDtmfGenerator.UpdateDelaySinceLastTone();
3706 }
3707 return 0;
3708}
3709
deadbeef74375882015-08-13 12:09:10 -07003710void Channel::UpdatePlayoutTimestamp(bool rtcp) {
3711 uint32_t playout_timestamp = 0;
3712
3713 if (audio_coding_->PlayoutTimestamp(&playout_timestamp) == -1) {
3714 // This can happen if this channel has not been received any RTP packet. In
3715 // this case, NetEq is not capable of computing playout timestamp.
3716 return;
3717 }
3718
3719 uint16_t delay_ms = 0;
3720 if (_audioDeviceModulePtr->PlayoutDelay(&delay_ms) == -1) {
3721 WEBRTC_TRACE(kTraceWarning, kTraceVoice, VoEId(_instanceId,_channelId),
3722 "Channel::UpdatePlayoutTimestamp() failed to read playout"
3723 " delay from the ADM");
3724 _engineStatisticsPtr->SetLastError(
3725 VE_CANNOT_RETRIEVE_VALUE, kTraceError,
3726 "UpdatePlayoutTimestamp() failed to retrieve playout delay");
3727 return;
3728 }
3729
3730 jitter_buffer_playout_timestamp_ = playout_timestamp;
3731
3732 // Remove the playout delay.
3733 playout_timestamp -= (delay_ms * (GetPlayoutFrequency() / 1000));
3734
3735 WEBRTC_TRACE(kTraceStream, kTraceVoice, VoEId(_instanceId,_channelId),
3736 "Channel::UpdatePlayoutTimestamp() => playoutTimestamp = %lu",
3737 playout_timestamp);
3738
3739 {
3740 CriticalSectionScoped cs(video_sync_lock_.get());
3741 if (rtcp) {
3742 playout_timestamp_rtcp_ = playout_timestamp;
3743 } else {
3744 playout_timestamp_rtp_ = playout_timestamp;
3745 }
3746 playout_delay_ms_ = delay_ms;
3747 }
3748}
3749
pwestin@webrtc.org1de01352013-04-11 20:23:35 +00003750// Called for incoming RTP packets after successful RTP header parsing.
3751void Channel::UpdatePacketDelay(uint32_t rtp_timestamp,
3752 uint16_t sequence_number) {
3753 WEBRTC_TRACE(kTraceStream, kTraceVoice, VoEId(_instanceId,_channelId),
3754 "Channel::UpdatePacketDelay(timestamp=%lu, sequenceNumber=%u)",
3755 rtp_timestamp, sequence_number);
niklase@google.com470e71d2011-07-07 08:21:25 +00003756
pwestin@webrtc.org1de01352013-04-11 20:23:35 +00003757 // Get frequency of last received payload
wu@webrtc.org94454b72014-06-05 20:34:08 +00003758 int rtp_receive_frequency = GetPlayoutFrequency();
niklase@google.com470e71d2011-07-07 08:21:25 +00003759
turaj@webrtc.org167b6df2013-12-13 21:05:07 +00003760 // |jitter_buffer_playout_timestamp_| updated in UpdatePlayoutTimestamp for
3761 // every incoming packet.
3762 uint32_t timestamp_diff_ms = (rtp_timestamp -
3763 jitter_buffer_playout_timestamp_) / (rtp_receive_frequency / 1000);
henrik.lundin@webrtc.orgd6692992014-03-20 12:04:09 +00003764 if (!IsNewerTimestamp(rtp_timestamp, jitter_buffer_playout_timestamp_) ||
3765 timestamp_diff_ms > (2 * kVoiceEngineMaxMinPlayoutDelayMs)) {
3766 // If |jitter_buffer_playout_timestamp_| is newer than the incoming RTP
3767 // timestamp, the resulting difference is negative, but is set to zero.
3768 // This can happen when a network glitch causes a packet to arrive late,
3769 // and during long comfort noise periods with clock drift.
3770 timestamp_diff_ms = 0;
3771 }
niklase@google.com470e71d2011-07-07 08:21:25 +00003772
pwestin@webrtc.org1de01352013-04-11 20:23:35 +00003773 uint16_t packet_delay_ms = (rtp_timestamp - _previousTimestamp) /
3774 (rtp_receive_frequency / 1000);
niklase@google.com470e71d2011-07-07 08:21:25 +00003775
pwestin@webrtc.org1de01352013-04-11 20:23:35 +00003776 _previousTimestamp = rtp_timestamp;
niklase@google.com470e71d2011-07-07 08:21:25 +00003777
pwestin@webrtc.org1de01352013-04-11 20:23:35 +00003778 if (timestamp_diff_ms == 0) return;
niklase@google.com470e71d2011-07-07 08:21:25 +00003779
deadbeef74375882015-08-13 12:09:10 -07003780 {
3781 CriticalSectionScoped cs(video_sync_lock_.get());
niklase@google.com470e71d2011-07-07 08:21:25 +00003782
deadbeef74375882015-08-13 12:09:10 -07003783 if (packet_delay_ms >= 10 && packet_delay_ms <= 60) {
3784 _recPacketDelayMs = packet_delay_ms;
3785 }
pwestin@webrtc.org1de01352013-04-11 20:23:35 +00003786
deadbeef74375882015-08-13 12:09:10 -07003787 if (_average_jitter_buffer_delay_us == 0) {
3788 _average_jitter_buffer_delay_us = timestamp_diff_ms * 1000;
3789 return;
3790 }
3791
3792 // Filter average delay value using exponential filter (alpha is
3793 // 7/8). We derive 1000 *_average_jitter_buffer_delay_us here (reduces
3794 // risk of rounding error) and compensate for it in GetDelayEstimate()
3795 // later.
3796 _average_jitter_buffer_delay_us = (_average_jitter_buffer_delay_us * 7 +
3797 1000 * timestamp_diff_ms + 500) / 8;
3798 }
niklase@google.com470e71d2011-07-07 08:21:25 +00003799}
3800
3801void
3802Channel::RegisterReceiveCodecsToRTPModule()
3803{
3804 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId,_channelId),
3805 "Channel::RegisterReceiveCodecsToRTPModule()");
3806
niklase@google.com470e71d2011-07-07 08:21:25 +00003807 CodecInst codec;
pbos@webrtc.org6141e132013-04-09 10:09:10 +00003808 const uint8_t nSupportedCodecs = AudioCodingModule::NumberOfCodecs();
niklase@google.com470e71d2011-07-07 08:21:25 +00003809
3810 for (int idx = 0; idx < nSupportedCodecs; idx++)
3811 {
3812 // Open up the RTP/RTCP receiver for all supported codecs
andrew@webrtc.orgeb524d92013-09-23 23:02:24 +00003813 if ((audio_coding_->Codec(idx, &codec) == -1) ||
wu@webrtc.org822fbd82013-08-15 23:38:54 +00003814 (rtp_receiver_->RegisterReceivePayload(
3815 codec.plname,
3816 codec.pltype,
3817 codec.plfreq,
3818 codec.channels,
3819 (codec.rate < 0) ? 0 : codec.rate) == -1))
niklase@google.com470e71d2011-07-07 08:21:25 +00003820 {
Peter Boströmd5c75b12015-09-23 13:24:32 +02003821 WEBRTC_TRACE(kTraceWarning,
niklase@google.com470e71d2011-07-07 08:21:25 +00003822 kTraceVoice,
3823 VoEId(_instanceId, _channelId),
3824 "Channel::RegisterReceiveCodecsToRTPModule() unable"
3825 " to register %s (%d/%d/%d/%d) to RTP/RTCP receiver",
3826 codec.plname, codec.pltype, codec.plfreq,
3827 codec.channels, codec.rate);
3828 }
3829 else
3830 {
Peter Boströmd5c75b12015-09-23 13:24:32 +02003831 WEBRTC_TRACE(kTraceInfo,
niklase@google.com470e71d2011-07-07 08:21:25 +00003832 kTraceVoice,
3833 VoEId(_instanceId, _channelId),
3834 "Channel::RegisterReceiveCodecsToRTPModule() %s "
wu@webrtc.orgfcd12b32011-09-15 20:49:50 +00003835 "(%d/%d/%d/%d) has been added to the RTP/RTCP "
niklase@google.com470e71d2011-07-07 08:21:25 +00003836 "receiver",
3837 codec.plname, codec.pltype, codec.plfreq,
3838 codec.channels, codec.rate);
3839 }
3840 }
3841}
3842
turaj@webrtc.org8c8ad852013-01-31 18:20:17 +00003843// Assuming this method is called with valid payload type.
turaj@webrtc.org42259e72012-12-11 02:15:12 +00003844int Channel::SetRedPayloadType(int red_payload_type) {
turaj@webrtc.org42259e72012-12-11 02:15:12 +00003845 CodecInst codec;
3846 bool found_red = false;
3847
3848 // Get default RED settings from the ACM database
3849 const int num_codecs = AudioCodingModule::NumberOfCodecs();
3850 for (int idx = 0; idx < num_codecs; idx++) {
andrew@webrtc.orgeb524d92013-09-23 23:02:24 +00003851 audio_coding_->Codec(idx, &codec);
turaj@webrtc.org42259e72012-12-11 02:15:12 +00003852 if (!STR_CASE_CMP(codec.plname, "RED")) {
3853 found_red = true;
3854 break;
3855 }
3856 }
3857
3858 if (!found_red) {
3859 _engineStatisticsPtr->SetLastError(
3860 VE_CODEC_ERROR, kTraceError,
3861 "SetRedPayloadType() RED is not supported");
3862 return -1;
3863 }
3864
turaj@webrtc.org9d532fd2013-01-31 18:34:19 +00003865 codec.pltype = red_payload_type;
andrew@webrtc.orgeb524d92013-09-23 23:02:24 +00003866 if (audio_coding_->RegisterSendCodec(codec) < 0) {
turaj@webrtc.org42259e72012-12-11 02:15:12 +00003867 _engineStatisticsPtr->SetLastError(
3868 VE_AUDIO_CODING_MODULE_ERROR, kTraceError,
3869 "SetRedPayloadType() RED registration in ACM module failed");
3870 return -1;
3871 }
3872
3873 if (_rtpRtcpModule->SetSendREDPayloadType(red_payload_type) != 0) {
3874 _engineStatisticsPtr->SetLastError(
3875 VE_RTP_RTCP_MODULE_ERROR, kTraceError,
3876 "SetRedPayloadType() RED registration in RTP/RTCP module failed");
3877 return -1;
3878 }
3879 return 0;
3880}
3881
wu@webrtc.orgebdb0e32014-03-06 23:49:08 +00003882int Channel::SetSendRtpHeaderExtension(bool enable, RTPExtensionType type,
3883 unsigned char id) {
3884 int error = 0;
3885 _rtpRtcpModule->DeregisterSendRtpHeaderExtension(type);
3886 if (enable) {
3887 error = _rtpRtcpModule->RegisterSendRtpHeaderExtension(type, id);
3888 }
3889 return error;
3890}
minyue@webrtc.orgc1a40a72014-05-28 09:52:06 +00003891
wu@webrtc.org94454b72014-06-05 20:34:08 +00003892int32_t Channel::GetPlayoutFrequency() {
3893 int32_t playout_frequency = audio_coding_->PlayoutFrequency();
3894 CodecInst current_recive_codec;
3895 if (audio_coding_->ReceiveCodec(&current_recive_codec) == 0) {
3896 if (STR_CASE_CMP("G722", current_recive_codec.plname) == 0) {
3897 // Even though the actual sampling rate for G.722 audio is
3898 // 16,000 Hz, the RTP clock rate for the G722 payload format is
3899 // 8,000 Hz because that value was erroneously assigned in
3900 // RFC 1890 and must remain unchanged for backward compatibility.
3901 playout_frequency = 8000;
3902 } else if (STR_CASE_CMP("opus", current_recive_codec.plname) == 0) {
3903 // We are resampling Opus internally to 32,000 Hz until all our
3904 // DSP routines can operate at 48,000 Hz, but the RTP clock
3905 // rate for the Opus payload format is standardized to 48,000 Hz,
3906 // because that is the maximum supported decoding sampling rate.
3907 playout_frequency = 48000;
3908 }
3909 }
3910 return playout_frequency;
3911}
3912
Minyue2013aec2015-05-13 14:14:42 +02003913int64_t Channel::GetRTT(bool allow_associate_channel) const {
minyue@webrtc.org2b58a442014-09-11 07:51:53 +00003914 RTCPMethod method = _rtpRtcpModule->RTCP();
3915 if (method == kRtcpOff) {
minyue@webrtc.org2b58a442014-09-11 07:51:53 +00003916 return 0;
3917 }
3918 std::vector<RTCPReportBlock> report_blocks;
3919 _rtpRtcpModule->RemoteRTCPStat(&report_blocks);
Minyue2013aec2015-05-13 14:14:42 +02003920
3921 int64_t rtt = 0;
minyue@webrtc.org2b58a442014-09-11 07:51:53 +00003922 if (report_blocks.empty()) {
Minyue2013aec2015-05-13 14:14:42 +02003923 if (allow_associate_channel) {
3924 CriticalSectionScoped lock(assoc_send_channel_lock_.get());
3925 Channel* channel = associate_send_channel_.channel();
3926 // Tries to get RTT from an associated channel. This is important for
3927 // receive-only channels.
3928 if (channel) {
3929 // To prevent infinite recursion and deadlock, calling GetRTT of
3930 // associate channel should always use "false" for argument:
3931 // |allow_associate_channel|.
3932 rtt = channel->GetRTT(false);
3933 }
3934 }
3935 return rtt;
minyue@webrtc.org2b58a442014-09-11 07:51:53 +00003936 }
3937
3938 uint32_t remoteSSRC = rtp_receiver_->SSRC();
3939 std::vector<RTCPReportBlock>::const_iterator it = report_blocks.begin();
3940 for (; it != report_blocks.end(); ++it) {
3941 if (it->remoteSSRC == remoteSSRC)
3942 break;
3943 }
3944 if (it == report_blocks.end()) {
3945 // We have not received packets with SSRC matching the report blocks.
3946 // To calculate RTT we try with the SSRC of the first report block.
3947 // This is very important for send-only channels where we don't know
3948 // the SSRC of the other end.
3949 remoteSSRC = report_blocks[0].remoteSSRC;
3950 }
Minyue2013aec2015-05-13 14:14:42 +02003951
pkasting@chromium.org16825b12015-01-12 21:51:21 +00003952 int64_t avg_rtt = 0;
3953 int64_t max_rtt= 0;
3954 int64_t min_rtt = 0;
minyue@webrtc.org2b58a442014-09-11 07:51:53 +00003955 if (_rtpRtcpModule->RTT(remoteSSRC, &rtt, &avg_rtt, &min_rtt, &max_rtt)
3956 != 0) {
minyue@webrtc.org2b58a442014-09-11 07:51:53 +00003957 return 0;
3958 }
pkasting@chromium.org16825b12015-01-12 21:51:21 +00003959 return rtt;
minyue@webrtc.org2b58a442014-09-11 07:51:53 +00003960}
3961
pbos@webrtc.orgd900e8b2013-07-03 15:12:26 +00003962} // namespace voe
3963} // namespace webrtc