blob: 2df3a713e889e71752270c2960615685f981abc8 [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
pkasting@chromium.org4591fbd2014-11-20 22:28:14 +000013#include "webrtc/base/format_macros.h"
wu@webrtc.org94454b72014-06-05 20:34:08 +000014#include "webrtc/base/timeutils.h"
minyue@webrtc.orge509f942013-09-12 17:03:00 +000015#include "webrtc/common.h"
turaj@webrtc.org6388c3e2013-02-12 21:42:18 +000016#include "webrtc/modules/audio_device/include/audio_device.h"
17#include "webrtc/modules/audio_processing/include/audio_processing.h"
henrik.lundin@webrtc.orgd6692992014-03-20 12:04:09 +000018#include "webrtc/modules/interface/module_common_types.h"
wu@webrtc.org822fbd82013-08-15 23:38:54 +000019#include "webrtc/modules/rtp_rtcp/interface/receive_statistics.h"
20#include "webrtc/modules/rtp_rtcp/interface/rtp_payload_registry.h"
21#include "webrtc/modules/rtp_rtcp/interface/rtp_receiver.h"
22#include "webrtc/modules/rtp_rtcp/source/rtp_receiver_strategy.h"
turaj@webrtc.org6388c3e2013-02-12 21:42:18 +000023#include "webrtc/modules/utility/interface/audio_frame_operations.h"
24#include "webrtc/modules/utility/interface/process_thread.h"
25#include "webrtc/modules/utility/interface/rtp_dump.h"
26#include "webrtc/system_wrappers/interface/critical_section_wrapper.h"
27#include "webrtc/system_wrappers/interface/logging.h"
28#include "webrtc/system_wrappers/interface/trace.h"
solenberg@webrtc.orgb1f50102014-03-24 10:38:25 +000029#include "webrtc/video_engine/include/vie_network.h"
turaj@webrtc.org6388c3e2013-02-12 21:42:18 +000030#include "webrtc/voice_engine/include/voe_base.h"
31#include "webrtc/voice_engine/include/voe_external_media.h"
32#include "webrtc/voice_engine/include/voe_rtp_rtcp.h"
33#include "webrtc/voice_engine/output_mixer.h"
34#include "webrtc/voice_engine/statistics.h"
35#include "webrtc/voice_engine/transmit_mixer.h"
36#include "webrtc/voice_engine/utility.h"
niklase@google.com470e71d2011-07-07 08:21:25 +000037
38#if defined(_WIN32)
39#include <Qos.h>
40#endif
41
andrew@webrtc.org50419b02012-11-14 19:07:54 +000042namespace webrtc {
43namespace voe {
niklase@google.com470e71d2011-07-07 08:21:25 +000044
sprang@webrtc.org54ae4ff2013-12-19 13:26:02 +000045// Extend the default RTCP statistics struct with max_jitter, defined as the
46// maximum jitter value seen in an RTCP report block.
47struct ChannelStatistics : public RtcpStatistics {
48 ChannelStatistics() : rtcp(), max_jitter(0) {}
49
50 RtcpStatistics rtcp;
51 uint32_t max_jitter;
52};
53
54// Statistics callback, called at each generation of a new RTCP report block.
55class StatisticsProxy : public RtcpStatisticsCallback {
56 public:
57 StatisticsProxy(uint32_t ssrc)
58 : stats_lock_(CriticalSectionWrapper::CreateCriticalSection()),
59 ssrc_(ssrc) {}
60 virtual ~StatisticsProxy() {}
61
kjellander@webrtc.org14665ff2015-03-04 12:58:35 +000062 void StatisticsUpdated(const RtcpStatistics& statistics,
63 uint32_t ssrc) override {
sprang@webrtc.org54ae4ff2013-12-19 13:26:02 +000064 if (ssrc != ssrc_)
65 return;
66
67 CriticalSectionScoped cs(stats_lock_.get());
68 stats_.rtcp = statistics;
69 if (statistics.jitter > stats_.max_jitter) {
70 stats_.max_jitter = statistics.jitter;
71 }
72 }
73
kjellander@webrtc.org14665ff2015-03-04 12:58:35 +000074 void CNameChanged(const char* cname, uint32_t ssrc) override {}
pbos@webrtc.orgce4e9a32014-12-18 13:50:16 +000075
sprang@webrtc.org54ae4ff2013-12-19 13:26:02 +000076 void ResetStatistics() {
77 CriticalSectionScoped cs(stats_lock_.get());
78 stats_ = ChannelStatistics();
79 }
80
81 ChannelStatistics GetStats() {
82 CriticalSectionScoped cs(stats_lock_.get());
83 return stats_;
84 }
85
86 private:
87 // StatisticsUpdated calls are triggered from threads in the RTP module,
88 // while GetStats calls can be triggered from the public voice engine API,
89 // hence synchronization is needed.
kwiberg@webrtc.org00b8f6b2015-02-26 14:34:55 +000090 rtc::scoped_ptr<CriticalSectionWrapper> stats_lock_;
sprang@webrtc.org54ae4ff2013-12-19 13:26:02 +000091 const uint32_t ssrc_;
92 ChannelStatistics stats_;
93};
94
mflodman@webrtc.org0a7d4ee2015-02-17 12:57:14 +000095class VoERtcpObserver : public RtcpBandwidthObserver {
minyue@webrtc.orgc1a40a72014-05-28 09:52:06 +000096 public:
mflodman@webrtc.org0a7d4ee2015-02-17 12:57:14 +000097 explicit VoERtcpObserver(Channel* owner) : owner_(owner) {}
98 virtual ~VoERtcpObserver() {}
minyue@webrtc.orgc1a40a72014-05-28 09:52:06 +000099
mflodman@webrtc.org0a7d4ee2015-02-17 12:57:14 +0000100 void OnReceivedEstimatedBitrate(uint32_t bitrate) override {
101 // Not used for Voice Engine.
102 }
103
kjellander@webrtc.org14665ff2015-03-04 12:58:35 +0000104 void OnReceivedRtcpReceiverReport(const ReportBlockList& report_blocks,
105 int64_t rtt,
106 int64_t now_ms) override {
mflodman@webrtc.org0a7d4ee2015-02-17 12:57:14 +0000107 // TODO(mflodman): Do we need to aggregate reports here or can we jut send
108 // what we get? I.e. do we ever get multiple reports bundled into one RTCP
109 // report for VoiceEngine?
110 if (report_blocks.empty())
111 return;
112
113 int fraction_lost_aggregate = 0;
114 int total_number_of_packets = 0;
115
116 // If receiving multiple report blocks, calculate the weighted average based
117 // on the number of packets a report refers to.
118 for (ReportBlockList::const_iterator block_it = report_blocks.begin();
119 block_it != report_blocks.end(); ++block_it) {
120 // Find the previous extended high sequence number for this remote SSRC,
121 // to calculate the number of RTP packets this report refers to. Ignore if
122 // we haven't seen this SSRC before.
123 std::map<uint32_t, uint32_t>::iterator seq_num_it =
124 extended_max_sequence_number_.find(block_it->sourceSSRC);
125 int number_of_packets = 0;
126 if (seq_num_it != extended_max_sequence_number_.end()) {
127 number_of_packets = block_it->extendedHighSeqNum - seq_num_it->second;
128 }
129 fraction_lost_aggregate += number_of_packets * block_it->fractionLost;
130 total_number_of_packets += number_of_packets;
131
132 extended_max_sequence_number_[block_it->sourceSSRC] =
133 block_it->extendedHighSeqNum;
134 }
135 int weighted_fraction_lost = 0;
136 if (total_number_of_packets > 0) {
137 weighted_fraction_lost = (fraction_lost_aggregate +
138 total_number_of_packets / 2) / total_number_of_packets;
139 }
140 owner_->OnIncomingFractionLoss(weighted_fraction_lost);
minyue@webrtc.orgc1a40a72014-05-28 09:52:06 +0000141 }
142
143 private:
144 Channel* owner_;
mflodman@webrtc.org0a7d4ee2015-02-17 12:57:14 +0000145 // Maps remote side ssrc to extended highest sequence number received.
146 std::map<uint32_t, uint32_t> extended_max_sequence_number_;
minyue@webrtc.orgc1a40a72014-05-28 09:52:06 +0000147};
148
pbos@webrtc.org6141e132013-04-09 10:09:10 +0000149int32_t
niklase@google.com470e71d2011-07-07 08:21:25 +0000150Channel::SendData(FrameType frameType,
pbos@webrtc.org6141e132013-04-09 10:09:10 +0000151 uint8_t payloadType,
152 uint32_t timeStamp,
153 const uint8_t* payloadData,
pkasting@chromium.org4591fbd2014-11-20 22:28:14 +0000154 size_t payloadSize,
niklase@google.com470e71d2011-07-07 08:21:25 +0000155 const RTPFragmentationHeader* fragmentation)
156{
157 WEBRTC_TRACE(kTraceStream, kTraceVoice, VoEId(_instanceId,_channelId),
158 "Channel::SendData(frameType=%u, payloadType=%u, timeStamp=%u,"
pkasting@chromium.org4591fbd2014-11-20 22:28:14 +0000159 " payloadSize=%" PRIuS ", fragmentation=0x%x)",
160 frameType, payloadType, timeStamp,
161 payloadSize, fragmentation);
niklase@google.com470e71d2011-07-07 08:21:25 +0000162
163 if (_includeAudioLevelIndication)
164 {
165 // Store current audio level in the RTP/RTCP module.
166 // The level will be used in combination with voice-activity state
167 // (frameType) to add an RTP header extension
andrew@webrtc.org382c0c22014-05-05 18:22:21 +0000168 _rtpRtcpModule->SetAudioLevel(rms_level_.RMS());
niklase@google.com470e71d2011-07-07 08:21:25 +0000169 }
170
171 // Push data from ACM to RTP/RTCP-module to deliver audio frame for
172 // packetization.
173 // This call will trigger Transport::SendPacket() from the RTP/RTCP module.
pwestin@webrtc.org2853dde2012-05-11 11:08:54 +0000174 if (_rtpRtcpModule->SendOutgoingData((FrameType&)frameType,
niklase@google.com470e71d2011-07-07 08:21:25 +0000175 payloadType,
176 timeStamp,
stefan@webrtc.orgddfdfed2012-07-03 13:21:22 +0000177 // Leaving the time when this frame was
178 // received from the capture device as
179 // undefined for voice for now.
180 -1,
niklase@google.com470e71d2011-07-07 08:21:25 +0000181 payloadData,
182 payloadSize,
183 fragmentation) == -1)
184 {
185 _engineStatisticsPtr->SetLastError(
186 VE_RTP_RTCP_MODULE_ERROR, kTraceWarning,
187 "Channel::SendData() failed to send data to RTP/RTCP module");
188 return -1;
189 }
190
191 _lastLocalTimeStamp = timeStamp;
192 _lastPayloadType = payloadType;
193
194 return 0;
195}
196
pbos@webrtc.org6141e132013-04-09 10:09:10 +0000197int32_t
henrik.lundin@webrtc.orge9217b42015-03-06 07:50:34 +0000198Channel::InFrameType(FrameType frame_type)
niklase@google.com470e71d2011-07-07 08:21:25 +0000199{
200 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId,_channelId),
henrik.lundin@webrtc.orge9217b42015-03-06 07:50:34 +0000201 "Channel::InFrameType(frame_type=%d)", frame_type);
niklase@google.com470e71d2011-07-07 08:21:25 +0000202
mflodman@webrtc.org9a065d12012-03-07 08:12:21 +0000203 CriticalSectionScoped cs(&_callbackCritSect);
henrik.lundin@webrtc.orge9217b42015-03-06 07:50:34 +0000204 _sendFrameType = (frame_type == kAudioFrameSpeech);
niklase@google.com470e71d2011-07-07 08:21:25 +0000205 return 0;
206}
207
pbos@webrtc.org6141e132013-04-09 10:09:10 +0000208int32_t
pbos@webrtc.org92135212013-05-14 08:31:39 +0000209Channel::OnRxVadDetected(int vadDecision)
niklase@google.com470e71d2011-07-07 08:21:25 +0000210{
211 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId, _channelId),
212 "Channel::OnRxVadDetected(vadDecision=%d)", vadDecision);
213
mflodman@webrtc.org9a065d12012-03-07 08:12:21 +0000214 CriticalSectionScoped cs(&_callbackCritSect);
niklase@google.com470e71d2011-07-07 08:21:25 +0000215 if (_rxVadObserverPtr)
216 {
217 _rxVadObserverPtr->OnRxVad(_channelId, vadDecision);
218 }
219
220 return 0;
221}
222
223int
pkasting@chromium.org4591fbd2014-11-20 22:28:14 +0000224Channel::SendPacket(int channel, const void *data, size_t len)
niklase@google.com470e71d2011-07-07 08:21:25 +0000225{
226 channel = VoEChannelId(channel);
227 assert(channel == _channelId);
228
229 WEBRTC_TRACE(kTraceStream, kTraceVoice, VoEId(_instanceId,_channelId),
pkasting@chromium.org4591fbd2014-11-20 22:28:14 +0000230 "Channel::SendPacket(channel=%d, len=%" PRIuS ")", channel,
231 len);
niklase@google.com470e71d2011-07-07 08:21:25 +0000232
wu@webrtc.orgfb648da2013-10-18 21:10:51 +0000233 CriticalSectionScoped cs(&_callbackCritSect);
234
niklase@google.com470e71d2011-07-07 08:21:25 +0000235 if (_transportPtr == NULL)
236 {
237 WEBRTC_TRACE(kTraceError, kTraceVoice, VoEId(_instanceId,_channelId),
238 "Channel::SendPacket() failed to send RTP packet due to"
239 " invalid transport object");
240 return -1;
241 }
242
pbos@webrtc.org6141e132013-04-09 10:09:10 +0000243 uint8_t* bufferToSendPtr = (uint8_t*)data;
pkasting@chromium.org4591fbd2014-11-20 22:28:14 +0000244 size_t bufferLength = len;
niklase@google.com470e71d2011-07-07 08:21:25 +0000245
246 // Dump the RTP packet to a file (if RTP dump is enabled).
pbos@webrtc.org6141e132013-04-09 10:09:10 +0000247 if (_rtpDumpOut.DumpPacket((const uint8_t*)data, len) == -1)
niklase@google.com470e71d2011-07-07 08:21:25 +0000248 {
249 WEBRTC_TRACE(kTraceWarning, kTraceVoice,
250 VoEId(_instanceId,_channelId),
251 "Channel::SendPacket() RTP dump to output file failed");
252 }
253
wu@webrtc.orgfb648da2013-10-18 21:10:51 +0000254 int n = _transportPtr->SendPacket(channel, bufferToSendPtr,
255 bufferLength);
256 if (n < 0) {
257 std::string transport_name =
258 _externalTransport ? "external transport" : "WebRtc sockets";
259 WEBRTC_TRACE(kTraceError, kTraceVoice,
260 VoEId(_instanceId,_channelId),
261 "Channel::SendPacket() RTP transmission using %s failed",
262 transport_name.c_str());
263 return -1;
niklase@google.com470e71d2011-07-07 08:21:25 +0000264 }
wu@webrtc.orgfb648da2013-10-18 21:10:51 +0000265 return n;
niklase@google.com470e71d2011-07-07 08:21:25 +0000266}
267
268int
pkasting@chromium.org4591fbd2014-11-20 22:28:14 +0000269Channel::SendRTCPPacket(int channel, const void *data, size_t len)
niklase@google.com470e71d2011-07-07 08:21:25 +0000270{
271 channel = VoEChannelId(channel);
272 assert(channel == _channelId);
273
274 WEBRTC_TRACE(kTraceStream, kTraceVoice, VoEId(_instanceId,_channelId),
pkasting@chromium.org4591fbd2014-11-20 22:28:14 +0000275 "Channel::SendRTCPPacket(channel=%d, len=%" PRIuS ")", channel,
276 len);
niklase@google.com470e71d2011-07-07 08:21:25 +0000277
wu@webrtc.orgfb648da2013-10-18 21:10:51 +0000278 CriticalSectionScoped cs(&_callbackCritSect);
279 if (_transportPtr == NULL)
niklase@google.com470e71d2011-07-07 08:21:25 +0000280 {
wu@webrtc.orgfb648da2013-10-18 21:10:51 +0000281 WEBRTC_TRACE(kTraceError, kTraceVoice,
282 VoEId(_instanceId,_channelId),
283 "Channel::SendRTCPPacket() failed to send RTCP packet"
284 " due to invalid transport object");
285 return -1;
niklase@google.com470e71d2011-07-07 08:21:25 +0000286 }
287
pbos@webrtc.org6141e132013-04-09 10:09:10 +0000288 uint8_t* bufferToSendPtr = (uint8_t*)data;
pkasting@chromium.org4591fbd2014-11-20 22:28:14 +0000289 size_t bufferLength = len;
niklase@google.com470e71d2011-07-07 08:21:25 +0000290
291 // Dump the RTCP packet to a file (if RTP dump is enabled).
pbos@webrtc.org6141e132013-04-09 10:09:10 +0000292 if (_rtpDumpOut.DumpPacket((const uint8_t*)data, len) == -1)
niklase@google.com470e71d2011-07-07 08:21:25 +0000293 {
294 WEBRTC_TRACE(kTraceWarning, kTraceVoice,
295 VoEId(_instanceId,_channelId),
296 "Channel::SendPacket() RTCP dump to output file failed");
297 }
298
wu@webrtc.orgfb648da2013-10-18 21:10:51 +0000299 int n = _transportPtr->SendRTCPPacket(channel,
300 bufferToSendPtr,
301 bufferLength);
302 if (n < 0) {
303 std::string transport_name =
304 _externalTransport ? "external transport" : "WebRtc sockets";
305 WEBRTC_TRACE(kTraceInfo, kTraceVoice,
306 VoEId(_instanceId,_channelId),
307 "Channel::SendRTCPPacket() transmission using %s failed",
308 transport_name.c_str());
309 return -1;
niklase@google.com470e71d2011-07-07 08:21:25 +0000310 }
wu@webrtc.orgfb648da2013-10-18 21:10:51 +0000311 return n;
niklase@google.com470e71d2011-07-07 08:21:25 +0000312}
313
314void
pbos@webrtc.org92135212013-05-14 08:31:39 +0000315Channel::OnPlayTelephoneEvent(int32_t id,
316 uint8_t event,
317 uint16_t lengthMs,
318 uint8_t volume)
niklase@google.com470e71d2011-07-07 08:21:25 +0000319{
320 WEBRTC_TRACE(kTraceStream, kTraceVoice, VoEId(_instanceId,_channelId),
321 "Channel::OnPlayTelephoneEvent(id=%d, event=%u, lengthMs=%u,"
wu@webrtc.orgfcd12b32011-09-15 20:49:50 +0000322 " volume=%u)", id, event, lengthMs, volume);
niklase@google.com470e71d2011-07-07 08:21:25 +0000323
324 if (!_playOutbandDtmfEvent || (event > 15))
325 {
326 // Ignore callback since feedback is disabled or event is not a
327 // Dtmf tone event.
328 return;
329 }
330
331 assert(_outputMixerPtr != NULL);
332
333 // Start playing out the Dtmf tone (if playout is enabled).
334 // Reduce length of tone with 80ms to the reduce risk of echo.
335 _outputMixerPtr->PlayDtmfTone(event, lengthMs - 80, volume);
336}
337
338void
stefan@webrtc.org286fe0b2013-08-21 20:58:21 +0000339Channel::OnIncomingSSRCChanged(int32_t id, uint32_t ssrc)
niklase@google.com470e71d2011-07-07 08:21:25 +0000340{
341 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId,_channelId),
342 "Channel::OnIncomingSSRCChanged(id=%d, SSRC=%d)",
stefan@webrtc.org286fe0b2013-08-21 20:58:21 +0000343 id, ssrc);
niklase@google.com470e71d2011-07-07 08:21:25 +0000344
dwkang@webrtc.orgb295a3f2013-08-29 07:34:12 +0000345 // Update ssrc so that NTP for AV sync can be updated.
346 _rtpRtcpModule->SetRemoteSSRC(ssrc);
niklase@google.com470e71d2011-07-07 08:21:25 +0000347}
348
pbos@webrtc.org92135212013-05-14 08:31:39 +0000349void Channel::OnIncomingCSRCChanged(int32_t id,
350 uint32_t CSRC,
351 bool added)
niklase@google.com470e71d2011-07-07 08:21:25 +0000352{
353 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId,_channelId),
354 "Channel::OnIncomingCSRCChanged(id=%d, CSRC=%d, added=%d)",
355 id, CSRC, added);
niklase@google.com470e71d2011-07-07 08:21:25 +0000356}
357
stefan@webrtc.org286fe0b2013-08-21 20:58:21 +0000358void Channel::ResetStatistics(uint32_t ssrc) {
359 StreamStatistician* statistician =
360 rtp_receive_statistics_->GetStatistician(ssrc);
361 if (statistician) {
362 statistician->ResetStatistics();
363 }
sprang@webrtc.org54ae4ff2013-12-19 13:26:02 +0000364 statistics_proxy_->ResetStatistics();
wu@webrtc.org822fbd82013-08-15 23:38:54 +0000365}
366
pbos@webrtc.org6141e132013-04-09 10:09:10 +0000367int32_t
niklase@google.com470e71d2011-07-07 08:21:25 +0000368Channel::OnInitializeDecoder(
pbos@webrtc.org92135212013-05-14 08:31:39 +0000369 int32_t id,
370 int8_t payloadType,
leozwang@webrtc.org813e4b02012-03-01 18:34:25 +0000371 const char payloadName[RTP_PAYLOAD_NAME_SIZE],
pbos@webrtc.org92135212013-05-14 08:31:39 +0000372 int frequency,
373 uint8_t channels,
374 uint32_t rate)
niklase@google.com470e71d2011-07-07 08:21:25 +0000375{
376 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId,_channelId),
377 "Channel::OnInitializeDecoder(id=%d, payloadType=%d, "
378 "payloadName=%s, frequency=%u, channels=%u, rate=%u)",
379 id, payloadType, payloadName, frequency, channels, rate);
380
andrew@webrtc.orgceb148c2011-08-23 17:53:54 +0000381 assert(VoEChannelId(id) == _channelId);
niklase@google.com470e71d2011-07-07 08:21:25 +0000382
henrika@webrtc.orgf75901f2012-01-16 08:45:42 +0000383 CodecInst receiveCodec = {0};
384 CodecInst dummyCodec = {0};
niklase@google.com470e71d2011-07-07 08:21:25 +0000385
386 receiveCodec.pltype = payloadType;
niklase@google.com470e71d2011-07-07 08:21:25 +0000387 receiveCodec.plfreq = frequency;
388 receiveCodec.channels = channels;
389 receiveCodec.rate = rate;
henrika@webrtc.orgf75901f2012-01-16 08:45:42 +0000390 strncpy(receiveCodec.plname, payloadName, RTP_PAYLOAD_NAME_SIZE - 1);
andrew@webrtc.orgae1a58b2013-01-22 04:44:30 +0000391
andrew@webrtc.orgeb524d92013-09-23 23:02:24 +0000392 audio_coding_->Codec(payloadName, &dummyCodec, frequency, channels);
niklase@google.com470e71d2011-07-07 08:21:25 +0000393 receiveCodec.pacsize = dummyCodec.pacsize;
394
395 // Register the new codec to the ACM
andrew@webrtc.orgeb524d92013-09-23 23:02:24 +0000396 if (audio_coding_->RegisterReceiveCodec(receiveCodec) == -1)
niklase@google.com470e71d2011-07-07 08:21:25 +0000397 {
398 WEBRTC_TRACE(kTraceWarning, kTraceVoice,
andrew@webrtc.orgceb148c2011-08-23 17:53:54 +0000399 VoEId(_instanceId, _channelId),
niklase@google.com470e71d2011-07-07 08:21:25 +0000400 "Channel::OnInitializeDecoder() invalid codec ("
401 "pt=%d, name=%s) received - 1", payloadType, payloadName);
402 _engineStatisticsPtr->SetLastError(VE_AUDIO_CODING_MODULE_ERROR);
403 return -1;
404 }
405
406 return 0;
407}
408
pbos@webrtc.org6141e132013-04-09 10:09:10 +0000409int32_t
410Channel::OnReceivedPayloadData(const uint8_t* payloadData,
pkasting@chromium.org4591fbd2014-11-20 22:28:14 +0000411 size_t payloadSize,
niklase@google.com470e71d2011-07-07 08:21:25 +0000412 const WebRtcRTPHeader* rtpHeader)
413{
414 WEBRTC_TRACE(kTraceStream, kTraceVoice, VoEId(_instanceId,_channelId),
pkasting@chromium.org4591fbd2014-11-20 22:28:14 +0000415 "Channel::OnReceivedPayloadData(payloadSize=%" PRIuS ","
niklase@google.com470e71d2011-07-07 08:21:25 +0000416 " payloadType=%u, audioChannel=%u)",
417 payloadSize,
418 rtpHeader->header.payloadType,
419 rtpHeader->type.Audio.channel);
420
henrika@webrtc.org944cbeb2014-03-18 10:32:33 +0000421 if (!channel_state_.Get().playing)
niklase@google.com470e71d2011-07-07 08:21:25 +0000422 {
423 // Avoid inserting into NetEQ when we are not playing. Count the
424 // packet as discarded.
425 WEBRTC_TRACE(kTraceStream, kTraceVoice,
426 VoEId(_instanceId, _channelId),
427 "received packet is discarded since playing is not"
428 " activated");
429 _numberOfDiscardedPackets++;
430 return 0;
431 }
432
433 // Push the incoming payload (parsed and ready for decoding) into the ACM
andrew@webrtc.orgeb524d92013-09-23 23:02:24 +0000434 if (audio_coding_->IncomingPacket(payloadData,
435 payloadSize,
436 *rtpHeader) != 0)
niklase@google.com470e71d2011-07-07 08:21:25 +0000437 {
438 _engineStatisticsPtr->SetLastError(
439 VE_AUDIO_CODING_MODULE_ERROR, kTraceWarning,
440 "Channel::OnReceivedPayloadData() unable to push data to the ACM");
441 return -1;
442 }
443
pwestin@webrtc.orgd30859e2013-06-06 21:09:01 +0000444 // Update the packet delay.
niklase@google.com470e71d2011-07-07 08:21:25 +0000445 UpdatePacketDelay(rtpHeader->header.timestamp,
446 rtpHeader->header.sequenceNumber);
pwestin@webrtc.orgd30859e2013-06-06 21:09:01 +0000447
pkasting@chromium.org16825b12015-01-12 21:51:21 +0000448 int64_t round_trip_time = 0;
wu@webrtc.org822fbd82013-08-15 23:38:54 +0000449 _rtpRtcpModule->RTT(rtp_receiver_->SSRC(), &round_trip_time,
450 NULL, NULL, NULL);
pwestin@webrtc.orgd30859e2013-06-06 21:09:01 +0000451
andrew@webrtc.orgeb524d92013-09-23 23:02:24 +0000452 std::vector<uint16_t> nack_list = audio_coding_->GetNackList(
wu@webrtc.org822fbd82013-08-15 23:38:54 +0000453 round_trip_time);
454 if (!nack_list.empty()) {
455 // Can't use nack_list.data() since it's not supported by all
456 // compilers.
457 ResendPackets(&(nack_list[0]), static_cast<int>(nack_list.size()));
pwestin@webrtc.orgd30859e2013-06-06 21:09:01 +0000458 }
niklase@google.com470e71d2011-07-07 08:21:25 +0000459 return 0;
460}
461
stefan@webrtc.org7bb8f022013-09-06 13:40:11 +0000462bool Channel::OnRecoveredPacket(const uint8_t* rtp_packet,
pkasting@chromium.org4591fbd2014-11-20 22:28:14 +0000463 size_t rtp_packet_length) {
stefan@webrtc.org7bb8f022013-09-06 13:40:11 +0000464 RTPHeader header;
465 if (!rtp_header_parser_->Parse(rtp_packet, rtp_packet_length, &header)) {
466 WEBRTC_TRACE(kTraceDebug, webrtc::kTraceVoice, _channelId,
467 "IncomingPacket invalid RTP header");
468 return false;
469 }
470 header.payload_type_frequency =
471 rtp_payload_registry_->GetPayloadTypeFrequency(header.payloadType);
472 if (header.payload_type_frequency < 0)
473 return false;
474 return ReceivePacket(rtp_packet, rtp_packet_length, header, false);
475}
476
pbos@webrtc.org92135212013-05-14 08:31:39 +0000477int32_t Channel::GetAudioFrame(int32_t id, AudioFrame& audioFrame)
niklase@google.com470e71d2011-07-07 08:21:25 +0000478{
479 WEBRTC_TRACE(kTraceStream, kTraceVoice, VoEId(_instanceId,_channelId),
480 "Channel::GetAudioFrame(id=%d)", id);
481
482 // Get 10ms raw PCM data from the ACM (mixer limits output frequency)
andrew@webrtc.orgeb524d92013-09-23 23:02:24 +0000483 if (audio_coding_->PlayoutData10Ms(audioFrame.sample_rate_hz_,
484 &audioFrame) == -1)
niklase@google.com470e71d2011-07-07 08:21:25 +0000485 {
486 WEBRTC_TRACE(kTraceError, kTraceVoice,
487 VoEId(_instanceId,_channelId),
488 "Channel::GetAudioFrame() PlayoutData10Ms() failed!");
andrew@webrtc.org7859e102012-01-13 00:30:11 +0000489 // In all likelihood, the audio in this frame is garbage. We return an
490 // error so that the audio mixer module doesn't add it to the mix. As
491 // a result, it won't be played out and the actions skipped here are
492 // irrelevant.
493 return -1;
niklase@google.com470e71d2011-07-07 08:21:25 +0000494 }
495
496 if (_RxVadDetection)
497 {
498 UpdateRxVadDetection(audioFrame);
499 }
500
501 // Convert module ID to internal VoE channel ID
andrew@webrtc.org63a50982012-05-02 23:56:37 +0000502 audioFrame.id_ = VoEChannelId(audioFrame.id_);
niklase@google.com470e71d2011-07-07 08:21:25 +0000503 // Store speech type for dead-or-alive detection
andrew@webrtc.org63a50982012-05-02 23:56:37 +0000504 _outputSpeechType = audioFrame.speech_type_;
niklase@google.com470e71d2011-07-07 08:21:25 +0000505
henrika@webrtc.org944cbeb2014-03-18 10:32:33 +0000506 ChannelState::State state = channel_state_.Get();
507
508 if (state.rx_apm_is_enabled) {
andrew@webrtc.org60730cf2014-01-07 17:45:09 +0000509 int err = rx_audioproc_->ProcessStream(&audioFrame);
510 if (err) {
511 LOG(LS_ERROR) << "ProcessStream() error: " << err;
512 assert(false);
513 }
niklase@google.com470e71d2011-07-07 08:21:25 +0000514 }
515
wu@webrtc.org63420662013-10-17 18:28:55 +0000516 float output_gain = 1.0f;
517 float left_pan = 1.0f;
518 float right_pan = 1.0f;
niklase@google.com470e71d2011-07-07 08:21:25 +0000519 {
wu@webrtc.org63420662013-10-17 18:28:55 +0000520 CriticalSectionScoped cs(&volume_settings_critsect_);
521 output_gain = _outputGain;
522 left_pan = _panLeft;
523 right_pan= _panRight;
524 }
525
526 // Output volume scaling
527 if (output_gain < 0.99f || output_gain > 1.01f)
528 {
529 AudioFrameOperations::ScaleWithSat(output_gain, audioFrame);
niklase@google.com470e71d2011-07-07 08:21:25 +0000530 }
531
532 // Scale left and/or right channel(s) if stereo and master balance is
533 // active
534
wu@webrtc.org63420662013-10-17 18:28:55 +0000535 if (left_pan != 1.0f || right_pan != 1.0f)
niklase@google.com470e71d2011-07-07 08:21:25 +0000536 {
andrew@webrtc.org63a50982012-05-02 23:56:37 +0000537 if (audioFrame.num_channels_ == 1)
niklase@google.com470e71d2011-07-07 08:21:25 +0000538 {
539 // Emulate stereo mode since panning is active.
540 // The mono signal is copied to both left and right channels here.
andrew@webrtc.org4ecea3e2012-06-27 03:25:31 +0000541 AudioFrameOperations::MonoToStereo(&audioFrame);
niklase@google.com470e71d2011-07-07 08:21:25 +0000542 }
543 // For true stereo mode (when we are receiving a stereo signal), no
544 // action is needed.
545
546 // Do the panning operation (the audio frame contains stereo at this
547 // stage)
wu@webrtc.org63420662013-10-17 18:28:55 +0000548 AudioFrameOperations::Scale(left_pan, right_pan, audioFrame);
niklase@google.com470e71d2011-07-07 08:21:25 +0000549 }
550
551 // Mix decoded PCM output with file if file mixing is enabled
henrika@webrtc.org944cbeb2014-03-18 10:32:33 +0000552 if (state.output_file_playing)
niklase@google.com470e71d2011-07-07 08:21:25 +0000553 {
andrew@webrtc.org63a50982012-05-02 23:56:37 +0000554 MixAudioWithFile(audioFrame, audioFrame.sample_rate_hz_);
niklase@google.com470e71d2011-07-07 08:21:25 +0000555 }
556
niklase@google.com470e71d2011-07-07 08:21:25 +0000557 // External media
558 if (_outputExternalMedia)
559 {
mflodman@webrtc.org9a065d12012-03-07 08:12:21 +0000560 CriticalSectionScoped cs(&_callbackCritSect);
andrew@webrtc.org63a50982012-05-02 23:56:37 +0000561 const bool isStereo = (audioFrame.num_channels_ == 2);
niklase@google.com470e71d2011-07-07 08:21:25 +0000562 if (_outputExternalMediaCallbackPtr)
563 {
564 _outputExternalMediaCallbackPtr->Process(
565 _channelId,
566 kPlaybackPerChannel,
pbos@webrtc.org6141e132013-04-09 10:09:10 +0000567 (int16_t*)audioFrame.data_,
andrew@webrtc.org63a50982012-05-02 23:56:37 +0000568 audioFrame.samples_per_channel_,
569 audioFrame.sample_rate_hz_,
niklase@google.com470e71d2011-07-07 08:21:25 +0000570 isStereo);
571 }
572 }
573
574 // Record playout if enabled
575 {
mflodman@webrtc.org9a065d12012-03-07 08:12:21 +0000576 CriticalSectionScoped cs(&_fileCritSect);
niklase@google.com470e71d2011-07-07 08:21:25 +0000577
578 if (_outputFileRecording && _outputFileRecorderPtr)
579 {
niklas.enbom@webrtc.org5398d952012-03-26 08:11:25 +0000580 _outputFileRecorderPtr->RecordAudioToFile(audioFrame);
niklase@google.com470e71d2011-07-07 08:21:25 +0000581 }
582 }
583
584 // Measure audio level (0-9)
585 _outputAudioLevel.ComputeLevel(audioFrame);
586
wu@webrtc.org94454b72014-06-05 20:34:08 +0000587 if (capture_start_rtp_time_stamp_ < 0 && audioFrame.timestamp_ != 0) {
588 // The first frame with a valid rtp timestamp.
wu@webrtc.orgcb711f72014-05-19 17:39:11 +0000589 capture_start_rtp_time_stamp_ = audioFrame.timestamp_;
wu@webrtc.org94454b72014-06-05 20:34:08 +0000590 }
591
592 if (capture_start_rtp_time_stamp_ >= 0) {
593 // audioFrame.timestamp_ should be valid from now on.
594
595 // Compute elapsed time.
596 int64_t unwrap_timestamp =
597 rtp_ts_wraparound_handler_->Unwrap(audioFrame.timestamp_);
598 audioFrame.elapsed_time_ms_ =
599 (unwrap_timestamp - capture_start_rtp_time_stamp_) /
600 (GetPlayoutFrequency() / 1000);
601
stefan@webrtc.org8e24d872014-09-02 18:58:24 +0000602 {
wu@webrtc.orgcb711f72014-05-19 17:39:11 +0000603 CriticalSectionScoped lock(ts_stats_lock_.get());
stefan@webrtc.org8e24d872014-09-02 18:58:24 +0000604 // Compute ntp time.
605 audioFrame.ntp_time_ms_ = ntp_estimator_.Estimate(
606 audioFrame.timestamp_);
607 // |ntp_time_ms_| won't be valid until at least 2 RTCP SRs are received.
608 if (audioFrame.ntp_time_ms_ > 0) {
609 // Compute |capture_start_ntp_time_ms_| so that
610 // |capture_start_ntp_time_ms_| + |elapsed_time_ms_| == |ntp_time_ms_|
611 capture_start_ntp_time_ms_ =
612 audioFrame.ntp_time_ms_ - audioFrame.elapsed_time_ms_;
613 }
wu@webrtc.orgcb711f72014-05-19 17:39:11 +0000614 }
615 }
616
niklase@google.com470e71d2011-07-07 08:21:25 +0000617 return 0;
618}
619
pbos@webrtc.org6141e132013-04-09 10:09:10 +0000620int32_t
pbos@webrtc.org92135212013-05-14 08:31:39 +0000621Channel::NeededFrequency(int32_t id)
niklase@google.com470e71d2011-07-07 08:21:25 +0000622{
623 WEBRTC_TRACE(kTraceStream, kTraceVoice, VoEId(_instanceId,_channelId),
624 "Channel::NeededFrequency(id=%d)", id);
625
626 int highestNeeded = 0;
627
628 // Determine highest needed receive frequency
andrew@webrtc.orgeb524d92013-09-23 23:02:24 +0000629 int32_t receiveFrequency = audio_coding_->ReceiveFrequency();
niklase@google.com470e71d2011-07-07 08:21:25 +0000630
631 // Return the bigger of playout and receive frequency in the ACM.
andrew@webrtc.orgeb524d92013-09-23 23:02:24 +0000632 if (audio_coding_->PlayoutFrequency() > receiveFrequency)
niklase@google.com470e71d2011-07-07 08:21:25 +0000633 {
andrew@webrtc.orgeb524d92013-09-23 23:02:24 +0000634 highestNeeded = audio_coding_->PlayoutFrequency();
niklase@google.com470e71d2011-07-07 08:21:25 +0000635 }
636 else
637 {
638 highestNeeded = receiveFrequency;
639 }
640
641 // Special case, if we're playing a file on the playout side
642 // we take that frequency into consideration as well
643 // This is not needed on sending side, since the codec will
644 // limit the spectrum anyway.
henrika@webrtc.org944cbeb2014-03-18 10:32:33 +0000645 if (channel_state_.Get().output_file_playing)
niklase@google.com470e71d2011-07-07 08:21:25 +0000646 {
mflodman@webrtc.org9a065d12012-03-07 08:12:21 +0000647 CriticalSectionScoped cs(&_fileCritSect);
henrika@webrtc.org944cbeb2014-03-18 10:32:33 +0000648 if (_outputFilePlayerPtr)
niklase@google.com470e71d2011-07-07 08:21:25 +0000649 {
650 if(_outputFilePlayerPtr->Frequency()>highestNeeded)
651 {
652 highestNeeded=_outputFilePlayerPtr->Frequency();
653 }
654 }
655 }
656
657 return(highestNeeded);
658}
659
pbos@webrtc.org6141e132013-04-09 10:09:10 +0000660int32_t
niklase@google.com470e71d2011-07-07 08:21:25 +0000661Channel::CreateChannel(Channel*& channel,
pbos@webrtc.org92135212013-05-14 08:31:39 +0000662 int32_t channelId,
minyue@webrtc.orge509f942013-09-12 17:03:00 +0000663 uint32_t instanceId,
664 const Config& config)
niklase@google.com470e71d2011-07-07 08:21:25 +0000665{
666 WEBRTC_TRACE(kTraceMemory, kTraceVoice, VoEId(instanceId,channelId),
667 "Channel::CreateChannel(channelId=%d, instanceId=%d)",
668 channelId, instanceId);
669
minyue@webrtc.orge509f942013-09-12 17:03:00 +0000670 channel = new Channel(channelId, instanceId, config);
niklase@google.com470e71d2011-07-07 08:21:25 +0000671 if (channel == NULL)
672 {
673 WEBRTC_TRACE(kTraceMemory, kTraceVoice,
674 VoEId(instanceId,channelId),
675 "Channel::CreateChannel() unable to allocate memory for"
676 " channel");
677 return -1;
678 }
679 return 0;
680}
681
682void
pbos@webrtc.org92135212013-05-14 08:31:39 +0000683Channel::PlayNotification(int32_t id, uint32_t durationMs)
niklase@google.com470e71d2011-07-07 08:21:25 +0000684{
685 WEBRTC_TRACE(kTraceStream, kTraceVoice, VoEId(_instanceId,_channelId),
686 "Channel::PlayNotification(id=%d, durationMs=%d)",
687 id, durationMs);
688
689 // Not implement yet
690}
691
692void
pbos@webrtc.org92135212013-05-14 08:31:39 +0000693Channel::RecordNotification(int32_t id, uint32_t durationMs)
niklase@google.com470e71d2011-07-07 08:21:25 +0000694{
695 WEBRTC_TRACE(kTraceStream, kTraceVoice, VoEId(_instanceId,_channelId),
696 "Channel::RecordNotification(id=%d, durationMs=%d)",
697 id, durationMs);
698
699 // Not implement yet
700}
701
702void
pbos@webrtc.org92135212013-05-14 08:31:39 +0000703Channel::PlayFileEnded(int32_t id)
niklase@google.com470e71d2011-07-07 08:21:25 +0000704{
705 WEBRTC_TRACE(kTraceStream, kTraceVoice, VoEId(_instanceId,_channelId),
706 "Channel::PlayFileEnded(id=%d)", id);
707
708 if (id == _inputFilePlayerId)
709 {
henrika@webrtc.org944cbeb2014-03-18 10:32:33 +0000710 channel_state_.SetInputFilePlaying(false);
niklase@google.com470e71d2011-07-07 08:21:25 +0000711 WEBRTC_TRACE(kTraceStateInfo, kTraceVoice,
712 VoEId(_instanceId,_channelId),
713 "Channel::PlayFileEnded() => input file player module is"
714 " shutdown");
715 }
716 else if (id == _outputFilePlayerId)
717 {
henrika@webrtc.org944cbeb2014-03-18 10:32:33 +0000718 channel_state_.SetOutputFilePlaying(false);
niklase@google.com470e71d2011-07-07 08:21:25 +0000719 WEBRTC_TRACE(kTraceStateInfo, kTraceVoice,
720 VoEId(_instanceId,_channelId),
721 "Channel::PlayFileEnded() => output file player module is"
722 " shutdown");
723 }
724}
725
726void
pbos@webrtc.org92135212013-05-14 08:31:39 +0000727Channel::RecordFileEnded(int32_t id)
niklase@google.com470e71d2011-07-07 08:21:25 +0000728{
729 WEBRTC_TRACE(kTraceStream, kTraceVoice, VoEId(_instanceId,_channelId),
730 "Channel::RecordFileEnded(id=%d)", id);
731
732 assert(id == _outputFileRecorderId);
733
mflodman@webrtc.org9a065d12012-03-07 08:12:21 +0000734 CriticalSectionScoped cs(&_fileCritSect);
niklase@google.com470e71d2011-07-07 08:21:25 +0000735
736 _outputFileRecording = false;
737 WEBRTC_TRACE(kTraceStateInfo, kTraceVoice,
738 VoEId(_instanceId,_channelId),
739 "Channel::RecordFileEnded() => output file recorder module is"
740 " shutdown");
741}
742
pbos@webrtc.org92135212013-05-14 08:31:39 +0000743Channel::Channel(int32_t channelId,
minyue@webrtc.orge509f942013-09-12 17:03:00 +0000744 uint32_t instanceId,
745 const Config& config) :
niklase@google.com470e71d2011-07-07 08:21:25 +0000746 _fileCritSect(*CriticalSectionWrapper::CreateCriticalSection()),
747 _callbackCritSect(*CriticalSectionWrapper::CreateCriticalSection()),
wu@webrtc.org63420662013-10-17 18:28:55 +0000748 volume_settings_critsect_(*CriticalSectionWrapper::CreateCriticalSection()),
niklase@google.com470e71d2011-07-07 08:21:25 +0000749 _instanceId(instanceId),
xians@google.com22963ab2011-08-03 12:40:23 +0000750 _channelId(channelId),
stefan@webrtc.orga5cb98c2013-05-29 12:12:51 +0000751 rtp_header_parser_(RtpHeaderParser::Create()),
wu@webrtc.org822fbd82013-08-15 23:38:54 +0000752 rtp_payload_registry_(
andresp@webrtc.orgdc80bae2014-04-08 11:06:12 +0000753 new RTPPayloadRegistry(RTPPayloadStrategy::CreateStrategy(true))),
wu@webrtc.org822fbd82013-08-15 23:38:54 +0000754 rtp_receive_statistics_(ReceiveStatistics::Create(
755 Clock::GetRealTimeClock())),
756 rtp_receiver_(RtpReceiver::CreateAudioReceiver(
757 VoEModuleId(instanceId, channelId), Clock::GetRealTimeClock(), this,
758 this, this, rtp_payload_registry_.get())),
759 telephone_event_handler_(rtp_receiver_->GetTelephoneEventHandler()),
henrik.lundin@webrtc.org34fe0152014-04-22 19:04:34 +0000760 audio_coding_(AudioCodingModule::Create(
xians@google.com22963ab2011-08-03 12:40:23 +0000761 VoEModuleId(instanceId, channelId))),
niklase@google.com470e71d2011-07-07 08:21:25 +0000762 _rtpDumpIn(*RtpDump::CreateRtpDump()),
763 _rtpDumpOut(*RtpDump::CreateRtpDump()),
niklase@google.com470e71d2011-07-07 08:21:25 +0000764 _outputAudioLevel(),
niklase@google.com470e71d2011-07-07 08:21:25 +0000765 _externalTransport(false),
niklase@google.com470e71d2011-07-07 08:21:25 +0000766 _inputFilePlayerPtr(NULL),
767 _outputFilePlayerPtr(NULL),
768 _outputFileRecorderPtr(NULL),
769 // Avoid conflict with other channels by adding 1024 - 1026,
770 // won't use as much as 1024 channels.
771 _inputFilePlayerId(VoEModuleId(instanceId, channelId) + 1024),
772 _outputFilePlayerId(VoEModuleId(instanceId, channelId) + 1025),
773 _outputFileRecorderId(VoEModuleId(instanceId, channelId) + 1026),
niklase@google.com470e71d2011-07-07 08:21:25 +0000774 _outputFileRecording(false),
xians@google.com22963ab2011-08-03 12:40:23 +0000775 _inbandDtmfQueue(VoEModuleId(instanceId, channelId)),
776 _inbandDtmfGenerator(VoEModuleId(instanceId, channelId)),
xians@google.com22963ab2011-08-03 12:40:23 +0000777 _outputExternalMedia(false),
niklase@google.com470e71d2011-07-07 08:21:25 +0000778 _inputExternalMediaCallbackPtr(NULL),
779 _outputExternalMediaCallbackPtr(NULL),
xians@google.com22963ab2011-08-03 12:40:23 +0000780 _timeStamp(0), // This is just an offset, RTP module will add it's own random offset
781 _sendTelephoneEventPayloadType(106),
stefan@webrtc.org8e24d872014-09-02 18:58:24 +0000782 ntp_estimator_(Clock::GetRealTimeClock()),
turaj@webrtc.org167b6df2013-12-13 21:05:07 +0000783 jitter_buffer_playout_timestamp_(0),
pwestin@webrtc.org1de01352013-04-11 20:23:35 +0000784 playout_timestamp_rtp_(0),
785 playout_timestamp_rtcp_(0),
sprang@webrtc.org54ae4ff2013-12-19 13:26:02 +0000786 playout_delay_ms_(0),
xians@google.com22963ab2011-08-03 12:40:23 +0000787 _numberOfDiscardedPackets(0),
xians@webrtc.org09e8c472013-07-31 16:30:19 +0000788 send_sequence_number_(0),
wu@webrtc.orgcb711f72014-05-19 17:39:11 +0000789 ts_stats_lock_(CriticalSectionWrapper::CreateCriticalSection()),
wu@webrtc.org94454b72014-06-05 20:34:08 +0000790 rtp_ts_wraparound_handler_(new rtc::TimestampWrapAroundHandler()),
791 capture_start_rtp_time_stamp_(-1),
wu@webrtc.orgcb711f72014-05-19 17:39:11 +0000792 capture_start_ntp_time_ms_(-1),
xians@google.com22963ab2011-08-03 12:40:23 +0000793 _engineStatisticsPtr(NULL),
henrika@webrtc.org2919e952012-01-31 08:45:03 +0000794 _outputMixerPtr(NULL),
795 _transmitMixerPtr(NULL),
xians@google.com22963ab2011-08-03 12:40:23 +0000796 _moduleProcessThreadPtr(NULL),
797 _audioDeviceModulePtr(NULL),
798 _voiceEngineObserverPtr(NULL),
799 _callbackCritSectPtr(NULL),
800 _transportPtr(NULL),
xians@google.com22963ab2011-08-03 12:40:23 +0000801 _rxVadObserverPtr(NULL),
802 _oldVadDecision(-1),
803 _sendFrameType(0),
roosa@google.com1b60ceb2012-12-12 23:00:29 +0000804 _externalMixing(false),
xians@google.com22963ab2011-08-03 12:40:23 +0000805 _mixFileWithMicrophone(false),
niklase@google.com470e71d2011-07-07 08:21:25 +0000806 _mute(false),
807 _panLeft(1.0f),
808 _panRight(1.0f),
809 _outputGain(1.0f),
810 _playOutbandDtmfEvent(false),
811 _playInbandDtmfEvent(false),
niklase@google.com470e71d2011-07-07 08:21:25 +0000812 _lastLocalTimeStamp(0),
813 _lastPayloadType(0),
xians@google.com22963ab2011-08-03 12:40:23 +0000814 _includeAudioLevelIndication(false),
niklase@google.com470e71d2011-07-07 08:21:25 +0000815 _outputSpeechType(AudioFrame::kNormalSpeech),
solenberg@webrtc.orgb1f50102014-03-24 10:38:25 +0000816 vie_network_(NULL),
817 video_channel_(-1),
pwestin@webrtc.org1de01352013-04-11 20:23:35 +0000818 _average_jitter_buffer_delay_us(0),
turaj@webrtc.orge46c8d32013-05-22 20:39:43 +0000819 least_required_delay_ms_(0),
niklase@google.com470e71d2011-07-07 08:21:25 +0000820 _previousTimestamp(0),
821 _recPacketDelayMs(20),
822 _RxVadDetection(false),
niklase@google.com470e71d2011-07-07 08:21:25 +0000823 _rxAgcIsEnabled(false),
stefan@webrtc.org7bb8f022013-09-06 13:40:11 +0000824 _rxNsIsEnabled(false),
minyue@webrtc.orgc1a40a72014-05-28 09:52:06 +0000825 restored_packet_in_use_(false),
mflodman@webrtc.org0a7d4ee2015-02-17 12:57:14 +0000826 rtcp_observer_(new VoERtcpObserver(this)),
minyue@webrtc.org74aaf292014-07-16 21:28:26 +0000827 network_predictor_(new NetworkPredictor(Clock::GetRealTimeClock()))
niklase@google.com470e71d2011-07-07 08:21:25 +0000828{
829 WEBRTC_TRACE(kTraceMemory, kTraceVoice, VoEId(_instanceId,_channelId),
830 "Channel::Channel() - ctor");
831 _inbandDtmfQueue.ResetDtmf();
832 _inbandDtmfGenerator.Init();
833 _outputAudioLevel.Clear();
834
pwestin@webrtc.org2853dde2012-05-11 11:08:54 +0000835 RtpRtcp::Configuration configuration;
836 configuration.id = VoEModuleId(instanceId, channelId);
837 configuration.audio = true;
pwestin@webrtc.org2853dde2012-05-11 11:08:54 +0000838 configuration.outgoing_transport = this;
pwestin@webrtc.org2853dde2012-05-11 11:08:54 +0000839 configuration.audio_messages = this;
wu@webrtc.org822fbd82013-08-15 23:38:54 +0000840 configuration.receive_statistics = rtp_receive_statistics_.get();
mflodman@webrtc.org0a7d4ee2015-02-17 12:57:14 +0000841 configuration.bandwidth_callback = rtcp_observer_.get();
pwestin@webrtc.org2853dde2012-05-11 11:08:54 +0000842
843 _rtpRtcpModule.reset(RtpRtcp::CreateRtpRtcp(configuration));
sprang@webrtc.org54ae4ff2013-12-19 13:26:02 +0000844
845 statistics_proxy_.reset(new StatisticsProxy(_rtpRtcpModule->SSRC()));
846 rtp_receive_statistics_->RegisterRtcpStatisticsCallback(
847 statistics_proxy_.get());
aluebs@webrtc.orgf927fd62014-04-16 11:58:18 +0000848
849 Config audioproc_config;
850 audioproc_config.Set<ExperimentalAgc>(new ExperimentalAgc(false));
851 rx_audioproc_.reset(AudioProcessing::Create(audioproc_config));
niklase@google.com470e71d2011-07-07 08:21:25 +0000852}
853
854Channel::~Channel()
855{
sprang@webrtc.org54ae4ff2013-12-19 13:26:02 +0000856 rtp_receive_statistics_->RegisterRtcpStatisticsCallback(NULL);
niklase@google.com470e71d2011-07-07 08:21:25 +0000857 WEBRTC_TRACE(kTraceMemory, kTraceVoice, VoEId(_instanceId,_channelId),
858 "Channel::~Channel() - dtor");
859
860 if (_outputExternalMedia)
861 {
862 DeRegisterExternalMediaProcessing(kPlaybackPerChannel);
863 }
henrika@webrtc.org944cbeb2014-03-18 10:32:33 +0000864 if (channel_state_.Get().input_external_media)
niklase@google.com470e71d2011-07-07 08:21:25 +0000865 {
866 DeRegisterExternalMediaProcessing(kRecordingPerChannel);
867 }
868 StopSend();
niklase@google.com470e71d2011-07-07 08:21:25 +0000869 StopPlayout();
870
871 {
mflodman@webrtc.org9a065d12012-03-07 08:12:21 +0000872 CriticalSectionScoped cs(&_fileCritSect);
niklase@google.com470e71d2011-07-07 08:21:25 +0000873 if (_inputFilePlayerPtr)
874 {
875 _inputFilePlayerPtr->RegisterModuleFileCallback(NULL);
876 _inputFilePlayerPtr->StopPlayingFile();
877 FilePlayer::DestroyFilePlayer(_inputFilePlayerPtr);
878 _inputFilePlayerPtr = NULL;
879 }
880 if (_outputFilePlayerPtr)
881 {
882 _outputFilePlayerPtr->RegisterModuleFileCallback(NULL);
883 _outputFilePlayerPtr->StopPlayingFile();
884 FilePlayer::DestroyFilePlayer(_outputFilePlayerPtr);
885 _outputFilePlayerPtr = NULL;
886 }
887 if (_outputFileRecorderPtr)
888 {
889 _outputFileRecorderPtr->RegisterModuleFileCallback(NULL);
890 _outputFileRecorderPtr->StopRecording();
891 FileRecorder::DestroyFileRecorder(_outputFileRecorderPtr);
892 _outputFileRecorderPtr = NULL;
893 }
894 }
895
896 // The order to safely shutdown modules in a channel is:
897 // 1. De-register callbacks in modules
898 // 2. De-register modules in process thread
899 // 3. Destroy modules
andrew@webrtc.orgeb524d92013-09-23 23:02:24 +0000900 if (audio_coding_->RegisterTransportCallback(NULL) == -1)
niklase@google.com470e71d2011-07-07 08:21:25 +0000901 {
902 WEBRTC_TRACE(kTraceWarning, kTraceVoice,
903 VoEId(_instanceId,_channelId),
904 "~Channel() failed to de-register transport callback"
905 " (Audio coding module)");
906 }
andrew@webrtc.orgeb524d92013-09-23 23:02:24 +0000907 if (audio_coding_->RegisterVADCallback(NULL) == -1)
niklase@google.com470e71d2011-07-07 08:21:25 +0000908 {
909 WEBRTC_TRACE(kTraceWarning, kTraceVoice,
910 VoEId(_instanceId,_channelId),
911 "~Channel() failed to de-register VAD callback"
912 " (Audio coding module)");
913 }
niklase@google.com470e71d2011-07-07 08:21:25 +0000914 // De-register modules in process thread
tommi@webrtc.org3985f012015-02-27 13:36:34 +0000915 _moduleProcessThreadPtr->DeRegisterModule(_rtpRtcpModule.get());
916
niklase@google.com470e71d2011-07-07 08:21:25 +0000917 // End of modules shutdown
918
919 // Delete other objects
solenberg@webrtc.orgb1f50102014-03-24 10:38:25 +0000920 if (vie_network_) {
921 vie_network_->Release();
922 vie_network_ = NULL;
923 }
niklase@google.com470e71d2011-07-07 08:21:25 +0000924 RtpDump::DestroyRtpDump(&_rtpDumpIn);
925 RtpDump::DestroyRtpDump(&_rtpDumpOut);
niklase@google.com470e71d2011-07-07 08:21:25 +0000926 delete &_callbackCritSect;
niklase@google.com470e71d2011-07-07 08:21:25 +0000927 delete &_fileCritSect;
wu@webrtc.org63420662013-10-17 18:28:55 +0000928 delete &volume_settings_critsect_;
niklase@google.com470e71d2011-07-07 08:21:25 +0000929}
930
pbos@webrtc.org6141e132013-04-09 10:09:10 +0000931int32_t
niklase@google.com470e71d2011-07-07 08:21:25 +0000932Channel::Init()
933{
934 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId,_channelId),
935 "Channel::Init()");
936
henrika@webrtc.org944cbeb2014-03-18 10:32:33 +0000937 channel_state_.Reset();
938
niklase@google.com470e71d2011-07-07 08:21:25 +0000939 // --- Initial sanity
940
941 if ((_engineStatisticsPtr == NULL) ||
942 (_moduleProcessThreadPtr == NULL))
943 {
944 WEBRTC_TRACE(kTraceError, kTraceVoice,
945 VoEId(_instanceId,_channelId),
946 "Channel::Init() must call SetEngineInformation() first");
947 return -1;
948 }
949
950 // --- Add modules to process thread (for periodic schedulation)
951
tommi@webrtc.org3985f012015-02-27 13:36:34 +0000952 _moduleProcessThreadPtr->RegisterModule(_rtpRtcpModule.get());
953
pwestin@webrtc.orgc450a192012-01-04 15:00:12 +0000954 // --- ACM initialization
niklase@google.com470e71d2011-07-07 08:21:25 +0000955
andrew@webrtc.orgeb524d92013-09-23 23:02:24 +0000956 if ((audio_coding_->InitializeReceiver() == -1) ||
niklase@google.com470e71d2011-07-07 08:21:25 +0000957#ifdef WEBRTC_CODEC_AVT
958 // out-of-band Dtmf tones are played out by default
andrew@webrtc.orgeb524d92013-09-23 23:02:24 +0000959 (audio_coding_->SetDtmfPlayoutStatus(true) == -1) ||
niklase@google.com470e71d2011-07-07 08:21:25 +0000960#endif
andrew@webrtc.orgeb524d92013-09-23 23:02:24 +0000961 (audio_coding_->InitializeSender() == -1))
niklase@google.com470e71d2011-07-07 08:21:25 +0000962 {
963 _engineStatisticsPtr->SetLastError(
964 VE_AUDIO_CODING_MODULE_ERROR, kTraceError,
965 "Channel::Init() unable to initialize the ACM - 1");
966 return -1;
967 }
968
969 // --- RTP/RTCP module initialization
970
971 // Ensure that RTCP is enabled by default for the created channel.
972 // Note that, the module will keep generating RTCP until it is explicitly
973 // disabled by the user.
974 // After StopListen (when no sockets exists), RTCP packets will no longer
975 // be transmitted since the Transport object will then be invalid.
wu@webrtc.org822fbd82013-08-15 23:38:54 +0000976 telephone_event_handler_->SetTelephoneEventForwardToDecoder(true);
977 // RTCP is enabled by default.
pbos@webrtc.orgd16e8392014-12-19 13:49:55 +0000978 _rtpRtcpModule->SetRTCPStatus(kRtcpCompound);
979 // --- Register all permanent callbacks
niklase@google.com470e71d2011-07-07 08:21:25 +0000980 const bool fail =
andrew@webrtc.orgeb524d92013-09-23 23:02:24 +0000981 (audio_coding_->RegisterTransportCallback(this) == -1) ||
982 (audio_coding_->RegisterVADCallback(this) == -1);
niklase@google.com470e71d2011-07-07 08:21:25 +0000983
984 if (fail)
985 {
986 _engineStatisticsPtr->SetLastError(
987 VE_CANNOT_INIT_CHANNEL, kTraceError,
988 "Channel::Init() callbacks not registered");
989 return -1;
990 }
991
992 // --- Register all supported codecs to the receiving side of the
993 // RTP/RTCP module
994
995 CodecInst codec;
pbos@webrtc.org6141e132013-04-09 10:09:10 +0000996 const uint8_t nSupportedCodecs = AudioCodingModule::NumberOfCodecs();
niklase@google.com470e71d2011-07-07 08:21:25 +0000997
998 for (int idx = 0; idx < nSupportedCodecs; idx++)
999 {
1000 // Open up the RTP/RTCP receiver for all supported codecs
andrew@webrtc.orgeb524d92013-09-23 23:02:24 +00001001 if ((audio_coding_->Codec(idx, &codec) == -1) ||
wu@webrtc.org822fbd82013-08-15 23:38:54 +00001002 (rtp_receiver_->RegisterReceivePayload(
1003 codec.plname,
1004 codec.pltype,
1005 codec.plfreq,
1006 codec.channels,
1007 (codec.rate < 0) ? 0 : codec.rate) == -1))
niklase@google.com470e71d2011-07-07 08:21:25 +00001008 {
1009 WEBRTC_TRACE(kTraceWarning, kTraceVoice,
1010 VoEId(_instanceId,_channelId),
1011 "Channel::Init() unable to register %s (%d/%d/%d/%d) "
1012 "to RTP/RTCP receiver",
1013 codec.plname, codec.pltype, codec.plfreq,
1014 codec.channels, codec.rate);
1015 }
1016 else
1017 {
1018 WEBRTC_TRACE(kTraceInfo, kTraceVoice,
1019 VoEId(_instanceId,_channelId),
1020 "Channel::Init() %s (%d/%d/%d/%d) has been added to "
1021 "the RTP/RTCP receiver",
1022 codec.plname, codec.pltype, codec.plfreq,
1023 codec.channels, codec.rate);
1024 }
1025
1026 // Ensure that PCMU is used as default codec on the sending side
tina.legrand@webrtc.org45175852012-06-01 09:27:35 +00001027 if (!STR_CASE_CMP(codec.plname, "PCMU") && (codec.channels == 1))
niklase@google.com470e71d2011-07-07 08:21:25 +00001028 {
1029 SetSendCodec(codec);
1030 }
1031
1032 // Register default PT for outband 'telephone-event'
1033 if (!STR_CASE_CMP(codec.plname, "telephone-event"))
1034 {
pwestin@webrtc.org2853dde2012-05-11 11:08:54 +00001035 if ((_rtpRtcpModule->RegisterSendPayload(codec) == -1) ||
andrew@webrtc.orgeb524d92013-09-23 23:02:24 +00001036 (audio_coding_->RegisterReceiveCodec(codec) == -1))
niklase@google.com470e71d2011-07-07 08:21:25 +00001037 {
1038 WEBRTC_TRACE(kTraceWarning, kTraceVoice,
1039 VoEId(_instanceId,_channelId),
1040 "Channel::Init() failed to register outband "
1041 "'telephone-event' (%d/%d) correctly",
1042 codec.pltype, codec.plfreq);
1043 }
1044 }
1045
1046 if (!STR_CASE_CMP(codec.plname, "CN"))
1047 {
andrew@webrtc.orgeb524d92013-09-23 23:02:24 +00001048 if ((audio_coding_->RegisterSendCodec(codec) == -1) ||
1049 (audio_coding_->RegisterReceiveCodec(codec) == -1) ||
pwestin@webrtc.org2853dde2012-05-11 11:08:54 +00001050 (_rtpRtcpModule->RegisterSendPayload(codec) == -1))
niklase@google.com470e71d2011-07-07 08:21:25 +00001051 {
1052 WEBRTC_TRACE(kTraceWarning, kTraceVoice,
1053 VoEId(_instanceId,_channelId),
1054 "Channel::Init() failed to register CN (%d/%d) "
1055 "correctly - 1",
1056 codec.pltype, codec.plfreq);
1057 }
1058 }
1059#ifdef WEBRTC_CODEC_RED
1060 // Register RED to the receiving side of the ACM.
1061 // We will not receive an OnInitializeDecoder() callback for RED.
1062 if (!STR_CASE_CMP(codec.plname, "RED"))
1063 {
andrew@webrtc.orgeb524d92013-09-23 23:02:24 +00001064 if (audio_coding_->RegisterReceiveCodec(codec) == -1)
niklase@google.com470e71d2011-07-07 08:21:25 +00001065 {
1066 WEBRTC_TRACE(kTraceWarning, kTraceVoice,
1067 VoEId(_instanceId,_channelId),
1068 "Channel::Init() failed to register RED (%d/%d) "
1069 "correctly",
1070 codec.pltype, codec.plfreq);
1071 }
1072 }
1073#endif
1074 }
pwestin@webrtc.org684f0572013-03-13 23:20:57 +00001075
andrew@webrtc.org6c264cc2013-10-04 17:54:09 +00001076 if (rx_audioproc_->noise_suppression()->set_level(kDefaultNsMode) != 0) {
1077 LOG_FERR1(LS_ERROR, noise_suppression()->set_level, kDefaultNsMode);
1078 return -1;
niklase@google.com470e71d2011-07-07 08:21:25 +00001079 }
andrew@webrtc.org6c264cc2013-10-04 17:54:09 +00001080 if (rx_audioproc_->gain_control()->set_mode(kDefaultRxAgcMode) != 0) {
1081 LOG_FERR1(LS_ERROR, gain_control()->set_mode, kDefaultRxAgcMode);
1082 return -1;
niklase@google.com470e71d2011-07-07 08:21:25 +00001083 }
1084
1085 return 0;
1086}
1087
pbos@webrtc.org6141e132013-04-09 10:09:10 +00001088int32_t
niklase@google.com470e71d2011-07-07 08:21:25 +00001089Channel::SetEngineInformation(Statistics& engineStatistics,
1090 OutputMixer& outputMixer,
1091 voe::TransmitMixer& transmitMixer,
1092 ProcessThread& moduleProcessThread,
1093 AudioDeviceModule& audioDeviceModule,
1094 VoiceEngineObserver* voiceEngineObserver,
1095 CriticalSectionWrapper* callbackCritSect)
1096{
1097 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId,_channelId),
1098 "Channel::SetEngineInformation()");
1099 _engineStatisticsPtr = &engineStatistics;
1100 _outputMixerPtr = &outputMixer;
1101 _transmitMixerPtr = &transmitMixer,
1102 _moduleProcessThreadPtr = &moduleProcessThread;
1103 _audioDeviceModulePtr = &audioDeviceModule;
1104 _voiceEngineObserverPtr = voiceEngineObserver;
1105 _callbackCritSectPtr = callbackCritSect;
1106 return 0;
1107}
1108
pbos@webrtc.org6141e132013-04-09 10:09:10 +00001109int32_t
niklase@google.com470e71d2011-07-07 08:21:25 +00001110Channel::UpdateLocalTimeStamp()
1111{
1112
andrew@webrtc.org63a50982012-05-02 23:56:37 +00001113 _timeStamp += _audioFrame.samples_per_channel_;
niklase@google.com470e71d2011-07-07 08:21:25 +00001114 return 0;
1115}
1116
pbos@webrtc.org6141e132013-04-09 10:09:10 +00001117int32_t
niklase@google.com470e71d2011-07-07 08:21:25 +00001118Channel::StartPlayout()
1119{
1120 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId,_channelId),
1121 "Channel::StartPlayout()");
henrika@webrtc.org944cbeb2014-03-18 10:32:33 +00001122 if (channel_state_.Get().playing)
niklase@google.com470e71d2011-07-07 08:21:25 +00001123 {
1124 return 0;
1125 }
roosa@google.com1b60ceb2012-12-12 23:00:29 +00001126
1127 if (!_externalMixing) {
1128 // Add participant as candidates for mixing.
1129 if (_outputMixerPtr->SetMixabilityStatus(*this, true) != 0)
1130 {
1131 _engineStatisticsPtr->SetLastError(
1132 VE_AUDIO_CONF_MIX_MODULE_ERROR, kTraceError,
1133 "StartPlayout() failed to add participant to mixer");
1134 return -1;
1135 }
niklase@google.com470e71d2011-07-07 08:21:25 +00001136 }
1137
henrika@webrtc.org944cbeb2014-03-18 10:32:33 +00001138 channel_state_.SetPlaying(true);
braveyao@webrtc.orgab129902012-06-04 03:26:39 +00001139 if (RegisterFilePlayingToMixer() != 0)
1140 return -1;
1141
niklase@google.com470e71d2011-07-07 08:21:25 +00001142 return 0;
1143}
1144
pbos@webrtc.org6141e132013-04-09 10:09:10 +00001145int32_t
niklase@google.com470e71d2011-07-07 08:21:25 +00001146Channel::StopPlayout()
1147{
1148 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId,_channelId),
1149 "Channel::StopPlayout()");
henrika@webrtc.org944cbeb2014-03-18 10:32:33 +00001150 if (!channel_state_.Get().playing)
niklase@google.com470e71d2011-07-07 08:21:25 +00001151 {
1152 return 0;
1153 }
roosa@google.com1b60ceb2012-12-12 23:00:29 +00001154
1155 if (!_externalMixing) {
1156 // Remove participant as candidates for mixing
1157 if (_outputMixerPtr->SetMixabilityStatus(*this, false) != 0)
1158 {
1159 _engineStatisticsPtr->SetLastError(
1160 VE_AUDIO_CONF_MIX_MODULE_ERROR, kTraceError,
1161 "StopPlayout() failed to remove participant from mixer");
1162 return -1;
1163 }
niklase@google.com470e71d2011-07-07 08:21:25 +00001164 }
1165
henrika@webrtc.org944cbeb2014-03-18 10:32:33 +00001166 channel_state_.SetPlaying(false);
niklase@google.com470e71d2011-07-07 08:21:25 +00001167 _outputAudioLevel.Clear();
1168
1169 return 0;
1170}
1171
pbos@webrtc.org6141e132013-04-09 10:09:10 +00001172int32_t
niklase@google.com470e71d2011-07-07 08:21:25 +00001173Channel::StartSend()
1174{
1175 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId,_channelId),
1176 "Channel::StartSend()");
xians@webrtc.org09e8c472013-07-31 16:30:19 +00001177 // Resume the previous sequence number which was reset by StopSend().
henrika@webrtc.org944cbeb2014-03-18 10:32:33 +00001178 // This needs to be done before |sending| is set to true.
xians@webrtc.org09e8c472013-07-31 16:30:19 +00001179 if (send_sequence_number_)
1180 SetInitSequenceNumber(send_sequence_number_);
1181
henrika@webrtc.org944cbeb2014-03-18 10:32:33 +00001182 if (channel_state_.Get().sending)
niklase@google.com470e71d2011-07-07 08:21:25 +00001183 {
henrika@webrtc.org944cbeb2014-03-18 10:32:33 +00001184 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +00001185 }
henrika@webrtc.org944cbeb2014-03-18 10:32:33 +00001186 channel_state_.SetSending(true);
xians@webrtc.orge07247a2011-11-28 16:31:28 +00001187
pwestin@webrtc.org2853dde2012-05-11 11:08:54 +00001188 if (_rtpRtcpModule->SetSendingStatus(true) != 0)
niklase@google.com470e71d2011-07-07 08:21:25 +00001189 {
1190 _engineStatisticsPtr->SetLastError(
1191 VE_RTP_RTCP_MODULE_ERROR, kTraceError,
1192 "StartSend() RTP/RTCP failed to start sending");
mflodman@webrtc.org9a065d12012-03-07 08:12:21 +00001193 CriticalSectionScoped cs(&_callbackCritSect);
henrika@webrtc.org944cbeb2014-03-18 10:32:33 +00001194 channel_state_.SetSending(false);
niklase@google.com470e71d2011-07-07 08:21:25 +00001195 return -1;
1196 }
xians@webrtc.orge07247a2011-11-28 16:31:28 +00001197
niklase@google.com470e71d2011-07-07 08:21:25 +00001198 return 0;
1199}
1200
pbos@webrtc.org6141e132013-04-09 10:09:10 +00001201int32_t
niklase@google.com470e71d2011-07-07 08:21:25 +00001202Channel::StopSend()
1203{
1204 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId,_channelId),
1205 "Channel::StopSend()");
henrika@webrtc.org944cbeb2014-03-18 10:32:33 +00001206 if (!channel_state_.Get().sending)
niklase@google.com470e71d2011-07-07 08:21:25 +00001207 {
henrika@webrtc.org944cbeb2014-03-18 10:32:33 +00001208 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +00001209 }
henrika@webrtc.org944cbeb2014-03-18 10:32:33 +00001210 channel_state_.SetSending(false);
xians@webrtc.orge07247a2011-11-28 16:31:28 +00001211
xians@webrtc.org09e8c472013-07-31 16:30:19 +00001212 // Store the sequence number to be able to pick up the same sequence for
1213 // the next StartSend(). This is needed for restarting device, otherwise
1214 // it might cause libSRTP to complain about packets being replayed.
1215 // TODO(xians): Remove this workaround after RtpRtcpModule's refactoring
1216 // CL is landed. See issue
1217 // https://code.google.com/p/webrtc/issues/detail?id=2111 .
1218 send_sequence_number_ = _rtpRtcpModule->SequenceNumber();
1219
niklase@google.com470e71d2011-07-07 08:21:25 +00001220 // Reset sending SSRC and sequence number and triggers direct transmission
1221 // of RTCP BYE
pwestin@webrtc.org2853dde2012-05-11 11:08:54 +00001222 if (_rtpRtcpModule->SetSendingStatus(false) == -1 ||
1223 _rtpRtcpModule->ResetSendDataCountersRTP() == -1)
niklase@google.com470e71d2011-07-07 08:21:25 +00001224 {
1225 _engineStatisticsPtr->SetLastError(
1226 VE_RTP_RTCP_MODULE_ERROR, kTraceWarning,
1227 "StartSend() RTP/RTCP failed to stop sending");
1228 }
1229
niklase@google.com470e71d2011-07-07 08:21:25 +00001230 return 0;
1231}
1232
pbos@webrtc.org6141e132013-04-09 10:09:10 +00001233int32_t
niklase@google.com470e71d2011-07-07 08:21:25 +00001234Channel::StartReceiving()
1235{
1236 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId,_channelId),
1237 "Channel::StartReceiving()");
henrika@webrtc.org944cbeb2014-03-18 10:32:33 +00001238 if (channel_state_.Get().receiving)
niklase@google.com470e71d2011-07-07 08:21:25 +00001239 {
1240 return 0;
1241 }
henrika@webrtc.org944cbeb2014-03-18 10:32:33 +00001242 channel_state_.SetReceiving(true);
niklase@google.com470e71d2011-07-07 08:21:25 +00001243 _numberOfDiscardedPackets = 0;
1244 return 0;
1245}
1246
pbos@webrtc.org6141e132013-04-09 10:09:10 +00001247int32_t
niklase@google.com470e71d2011-07-07 08:21:25 +00001248Channel::StopReceiving()
1249{
1250 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId,_channelId),
1251 "Channel::StopReceiving()");
henrika@webrtc.org944cbeb2014-03-18 10:32:33 +00001252 if (!channel_state_.Get().receiving)
niklase@google.com470e71d2011-07-07 08:21:25 +00001253 {
1254 return 0;
1255 }
pwestin@webrtc.org684f0572013-03-13 23:20:57 +00001256
henrika@webrtc.org944cbeb2014-03-18 10:32:33 +00001257 channel_state_.SetReceiving(false);
niklase@google.com470e71d2011-07-07 08:21:25 +00001258 return 0;
1259}
1260
pbos@webrtc.org6141e132013-04-09 10:09:10 +00001261int32_t
niklase@google.com470e71d2011-07-07 08:21:25 +00001262Channel::RegisterVoiceEngineObserver(VoiceEngineObserver& observer)
1263{
1264 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId,_channelId),
1265 "Channel::RegisterVoiceEngineObserver()");
mflodman@webrtc.org9a065d12012-03-07 08:12:21 +00001266 CriticalSectionScoped cs(&_callbackCritSect);
niklase@google.com470e71d2011-07-07 08:21:25 +00001267
1268 if (_voiceEngineObserverPtr)
1269 {
1270 _engineStatisticsPtr->SetLastError(
1271 VE_INVALID_OPERATION, kTraceError,
1272 "RegisterVoiceEngineObserver() observer already enabled");
1273 return -1;
1274 }
1275 _voiceEngineObserverPtr = &observer;
1276 return 0;
1277}
1278
pbos@webrtc.org6141e132013-04-09 10:09:10 +00001279int32_t
niklase@google.com470e71d2011-07-07 08:21:25 +00001280Channel::DeRegisterVoiceEngineObserver()
1281{
1282 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId,_channelId),
1283 "Channel::DeRegisterVoiceEngineObserver()");
mflodman@webrtc.org9a065d12012-03-07 08:12:21 +00001284 CriticalSectionScoped cs(&_callbackCritSect);
niklase@google.com470e71d2011-07-07 08:21:25 +00001285
1286 if (!_voiceEngineObserverPtr)
1287 {
1288 _engineStatisticsPtr->SetLastError(
1289 VE_INVALID_OPERATION, kTraceWarning,
1290 "DeRegisterVoiceEngineObserver() observer already disabled");
1291 return 0;
1292 }
1293 _voiceEngineObserverPtr = NULL;
1294 return 0;
1295}
1296
pbos@webrtc.org6141e132013-04-09 10:09:10 +00001297int32_t
niklase@google.com470e71d2011-07-07 08:21:25 +00001298Channel::GetSendCodec(CodecInst& codec)
1299{
andrew@webrtc.orgeb524d92013-09-23 23:02:24 +00001300 return (audio_coding_->SendCodec(&codec));
niklase@google.com470e71d2011-07-07 08:21:25 +00001301}
1302
pbos@webrtc.org6141e132013-04-09 10:09:10 +00001303int32_t
niklase@google.com470e71d2011-07-07 08:21:25 +00001304Channel::GetRecCodec(CodecInst& codec)
1305{
andrew@webrtc.orgeb524d92013-09-23 23:02:24 +00001306 return (audio_coding_->ReceiveCodec(&codec));
niklase@google.com470e71d2011-07-07 08:21:25 +00001307}
1308
pbos@webrtc.org6141e132013-04-09 10:09:10 +00001309int32_t
niklase@google.com470e71d2011-07-07 08:21:25 +00001310Channel::SetSendCodec(const CodecInst& codec)
1311{
1312 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId,_channelId),
1313 "Channel::SetSendCodec()");
1314
andrew@webrtc.orgeb524d92013-09-23 23:02:24 +00001315 if (audio_coding_->RegisterSendCodec(codec) != 0)
niklase@google.com470e71d2011-07-07 08:21:25 +00001316 {
1317 WEBRTC_TRACE(kTraceError, kTraceVoice, VoEId(_instanceId,_channelId),
1318 "SetSendCodec() failed to register codec to ACM");
1319 return -1;
1320 }
1321
pwestin@webrtc.org2853dde2012-05-11 11:08:54 +00001322 if (_rtpRtcpModule->RegisterSendPayload(codec) != 0)
niklase@google.com470e71d2011-07-07 08:21:25 +00001323 {
pwestin@webrtc.org2853dde2012-05-11 11:08:54 +00001324 _rtpRtcpModule->DeRegisterSendPayload(codec.pltype);
1325 if (_rtpRtcpModule->RegisterSendPayload(codec) != 0)
niklase@google.com470e71d2011-07-07 08:21:25 +00001326 {
1327 WEBRTC_TRACE(
1328 kTraceError, kTraceVoice, VoEId(_instanceId,_channelId),
1329 "SetSendCodec() failed to register codec to"
1330 " RTP/RTCP module");
1331 return -1;
1332 }
1333 }
1334
pwestin@webrtc.org2853dde2012-05-11 11:08:54 +00001335 if (_rtpRtcpModule->SetAudioPacketSize(codec.pacsize) != 0)
niklase@google.com470e71d2011-07-07 08:21:25 +00001336 {
1337 WEBRTC_TRACE(kTraceError, kTraceVoice, VoEId(_instanceId,_channelId),
1338 "SetSendCodec() failed to set audio packet size");
1339 return -1;
1340 }
1341
1342 return 0;
1343}
1344
mflodman@webrtc.org0a7d4ee2015-02-17 12:57:14 +00001345void Channel::OnIncomingFractionLoss(int fraction_lost) {
minyue@webrtc.org74aaf292014-07-16 21:28:26 +00001346 network_predictor_->UpdatePacketLossRate(fraction_lost);
mflodman@webrtc.org0a7d4ee2015-02-17 12:57:14 +00001347 uint8_t average_fraction_loss = network_predictor_->GetLossRate();
1348
minyue@webrtc.orgc1a40a72014-05-28 09:52:06 +00001349 // Normalizes rate to 0 - 100.
mflodman@webrtc.org0a7d4ee2015-02-17 12:57:14 +00001350 if (audio_coding_->SetPacketLossRate(
1351 100 * average_fraction_loss / 255) != 0) {
minyue@webrtc.orgc1a40a72014-05-28 09:52:06 +00001352 assert(false); // This should not happen.
1353 }
1354}
1355
pbos@webrtc.org6141e132013-04-09 10:09:10 +00001356int32_t
niklase@google.com470e71d2011-07-07 08:21:25 +00001357Channel::SetVADStatus(bool enableVAD, ACMVADMode mode, bool disableDTX)
1358{
1359 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId,_channelId),
1360 "Channel::SetVADStatus(mode=%d)", mode);
henrik.lundin@webrtc.org664ccb72015-01-28 14:49:05 +00001361 assert(!(disableDTX && enableVAD)); // disableDTX mode is deprecated.
niklase@google.com470e71d2011-07-07 08:21:25 +00001362 // To disable VAD, DTX must be disabled too
1363 disableDTX = ((enableVAD == false) ? true : disableDTX);
andrew@webrtc.orgeb524d92013-09-23 23:02:24 +00001364 if (audio_coding_->SetVAD(!disableDTX, enableVAD, mode) != 0)
niklase@google.com470e71d2011-07-07 08:21:25 +00001365 {
1366 _engineStatisticsPtr->SetLastError(
1367 VE_AUDIO_CODING_MODULE_ERROR, kTraceError,
1368 "SetVADStatus() failed to set VAD");
1369 return -1;
1370 }
1371 return 0;
1372}
1373
pbos@webrtc.org6141e132013-04-09 10:09:10 +00001374int32_t
niklase@google.com470e71d2011-07-07 08:21:25 +00001375Channel::GetVADStatus(bool& enabledVAD, ACMVADMode& mode, bool& disabledDTX)
1376{
1377 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId,_channelId),
1378 "Channel::GetVADStatus");
andrew@webrtc.orgeb524d92013-09-23 23:02:24 +00001379 if (audio_coding_->VAD(&disabledDTX, &enabledVAD, &mode) != 0)
niklase@google.com470e71d2011-07-07 08:21:25 +00001380 {
1381 _engineStatisticsPtr->SetLastError(
1382 VE_AUDIO_CODING_MODULE_ERROR, kTraceError,
1383 "GetVADStatus() failed to get VAD status");
1384 return -1;
1385 }
1386 disabledDTX = !disabledDTX;
1387 return 0;
1388}
1389
pbos@webrtc.org6141e132013-04-09 10:09:10 +00001390int32_t
niklase@google.com470e71d2011-07-07 08:21:25 +00001391Channel::SetRecPayloadType(const CodecInst& codec)
1392{
1393 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId,_channelId),
1394 "Channel::SetRecPayloadType()");
1395
henrika@webrtc.org944cbeb2014-03-18 10:32:33 +00001396 if (channel_state_.Get().playing)
niklase@google.com470e71d2011-07-07 08:21:25 +00001397 {
1398 _engineStatisticsPtr->SetLastError(
1399 VE_ALREADY_PLAYING, kTraceError,
1400 "SetRecPayloadType() unable to set PT while playing");
1401 return -1;
1402 }
henrika@webrtc.org944cbeb2014-03-18 10:32:33 +00001403 if (channel_state_.Get().receiving)
niklase@google.com470e71d2011-07-07 08:21:25 +00001404 {
1405 _engineStatisticsPtr->SetLastError(
1406 VE_ALREADY_LISTENING, kTraceError,
1407 "SetRecPayloadType() unable to set PT while listening");
1408 return -1;
1409 }
1410
1411 if (codec.pltype == -1)
1412 {
1413 // De-register the selected codec (RTP/RTCP module and ACM)
1414
pbos@webrtc.org6141e132013-04-09 10:09:10 +00001415 int8_t pltype(-1);
niklase@google.com470e71d2011-07-07 08:21:25 +00001416 CodecInst rxCodec = codec;
1417
1418 // Get payload type for the given codec
wu@webrtc.org822fbd82013-08-15 23:38:54 +00001419 rtp_payload_registry_->ReceivePayloadType(
1420 rxCodec.plname,
1421 rxCodec.plfreq,
1422 rxCodec.channels,
1423 (rxCodec.rate < 0) ? 0 : rxCodec.rate,
1424 &pltype);
niklase@google.com470e71d2011-07-07 08:21:25 +00001425 rxCodec.pltype = pltype;
1426
wu@webrtc.org822fbd82013-08-15 23:38:54 +00001427 if (rtp_receiver_->DeRegisterReceivePayload(pltype) != 0)
niklase@google.com470e71d2011-07-07 08:21:25 +00001428 {
1429 _engineStatisticsPtr->SetLastError(
1430 VE_RTP_RTCP_MODULE_ERROR,
1431 kTraceError,
1432 "SetRecPayloadType() RTP/RTCP-module deregistration "
1433 "failed");
1434 return -1;
1435 }
andrew@webrtc.orgeb524d92013-09-23 23:02:24 +00001436 if (audio_coding_->UnregisterReceiveCodec(rxCodec.pltype) != 0)
niklase@google.com470e71d2011-07-07 08:21:25 +00001437 {
1438 _engineStatisticsPtr->SetLastError(
1439 VE_AUDIO_CODING_MODULE_ERROR, kTraceError,
1440 "SetRecPayloadType() ACM deregistration failed - 1");
1441 return -1;
1442 }
1443 return 0;
1444 }
1445
wu@webrtc.org822fbd82013-08-15 23:38:54 +00001446 if (rtp_receiver_->RegisterReceivePayload(
1447 codec.plname,
1448 codec.pltype,
1449 codec.plfreq,
1450 codec.channels,
1451 (codec.rate < 0) ? 0 : codec.rate) != 0)
niklase@google.com470e71d2011-07-07 08:21:25 +00001452 {
1453 // First attempt to register failed => de-register and try again
wu@webrtc.org822fbd82013-08-15 23:38:54 +00001454 rtp_receiver_->DeRegisterReceivePayload(codec.pltype);
1455 if (rtp_receiver_->RegisterReceivePayload(
1456 codec.plname,
1457 codec.pltype,
1458 codec.plfreq,
1459 codec.channels,
1460 (codec.rate < 0) ? 0 : codec.rate) != 0)
niklase@google.com470e71d2011-07-07 08:21:25 +00001461 {
1462 _engineStatisticsPtr->SetLastError(
1463 VE_RTP_RTCP_MODULE_ERROR, kTraceError,
1464 "SetRecPayloadType() RTP/RTCP-module registration failed");
1465 return -1;
1466 }
1467 }
andrew@webrtc.orgeb524d92013-09-23 23:02:24 +00001468 if (audio_coding_->RegisterReceiveCodec(codec) != 0)
niklase@google.com470e71d2011-07-07 08:21:25 +00001469 {
andrew@webrtc.orgeb524d92013-09-23 23:02:24 +00001470 audio_coding_->UnregisterReceiveCodec(codec.pltype);
1471 if (audio_coding_->RegisterReceiveCodec(codec) != 0)
niklase@google.com470e71d2011-07-07 08:21:25 +00001472 {
1473 _engineStatisticsPtr->SetLastError(
1474 VE_AUDIO_CODING_MODULE_ERROR, kTraceError,
1475 "SetRecPayloadType() ACM registration failed - 1");
1476 return -1;
1477 }
1478 }
1479 return 0;
1480}
1481
pbos@webrtc.org6141e132013-04-09 10:09:10 +00001482int32_t
niklase@google.com470e71d2011-07-07 08:21:25 +00001483Channel::GetRecPayloadType(CodecInst& codec)
1484{
1485 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId,_channelId),
1486 "Channel::GetRecPayloadType()");
pbos@webrtc.org6141e132013-04-09 10:09:10 +00001487 int8_t payloadType(-1);
wu@webrtc.org822fbd82013-08-15 23:38:54 +00001488 if (rtp_payload_registry_->ReceivePayloadType(
1489 codec.plname,
1490 codec.plfreq,
1491 codec.channels,
1492 (codec.rate < 0) ? 0 : codec.rate,
1493 &payloadType) != 0)
niklase@google.com470e71d2011-07-07 08:21:25 +00001494 {
1495 _engineStatisticsPtr->SetLastError(
henrika@webrtc.org37198002012-06-18 11:00:12 +00001496 VE_RTP_RTCP_MODULE_ERROR, kTraceWarning,
niklase@google.com470e71d2011-07-07 08:21:25 +00001497 "GetRecPayloadType() failed to retrieve RX payload type");
1498 return -1;
1499 }
1500 codec.pltype = payloadType;
1501 WEBRTC_TRACE(kTraceStateInfo, kTraceVoice, VoEId(_instanceId,_channelId),
pkasting@chromium.orgd3245462015-02-23 21:28:22 +00001502 "Channel::GetRecPayloadType() => pltype=%d", codec.pltype);
niklase@google.com470e71d2011-07-07 08:21:25 +00001503 return 0;
1504}
1505
pbos@webrtc.org6141e132013-04-09 10:09:10 +00001506int32_t
niklase@google.com470e71d2011-07-07 08:21:25 +00001507Channel::SetSendCNPayloadType(int type, PayloadFrequencies frequency)
1508{
1509 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId,_channelId),
1510 "Channel::SetSendCNPayloadType()");
1511
1512 CodecInst codec;
pbos@webrtc.org6141e132013-04-09 10:09:10 +00001513 int32_t samplingFreqHz(-1);
tina.legrand@webrtc.org45175852012-06-01 09:27:35 +00001514 const int kMono = 1;
niklase@google.com470e71d2011-07-07 08:21:25 +00001515 if (frequency == kFreq32000Hz)
1516 samplingFreqHz = 32000;
1517 else if (frequency == kFreq16000Hz)
1518 samplingFreqHz = 16000;
1519
andrew@webrtc.orgeb524d92013-09-23 23:02:24 +00001520 if (audio_coding_->Codec("CN", &codec, samplingFreqHz, kMono) == -1)
niklase@google.com470e71d2011-07-07 08:21:25 +00001521 {
1522 _engineStatisticsPtr->SetLastError(
1523 VE_AUDIO_CODING_MODULE_ERROR, kTraceError,
1524 "SetSendCNPayloadType() failed to retrieve default CN codec "
1525 "settings");
1526 return -1;
1527 }
1528
1529 // Modify the payload type (must be set to dynamic range)
1530 codec.pltype = type;
1531
andrew@webrtc.orgeb524d92013-09-23 23:02:24 +00001532 if (audio_coding_->RegisterSendCodec(codec) != 0)
niklase@google.com470e71d2011-07-07 08:21:25 +00001533 {
1534 _engineStatisticsPtr->SetLastError(
1535 VE_AUDIO_CODING_MODULE_ERROR, kTraceError,
1536 "SetSendCNPayloadType() failed to register CN to ACM");
1537 return -1;
1538 }
1539
pwestin@webrtc.org2853dde2012-05-11 11:08:54 +00001540 if (_rtpRtcpModule->RegisterSendPayload(codec) != 0)
niklase@google.com470e71d2011-07-07 08:21:25 +00001541 {
pwestin@webrtc.org2853dde2012-05-11 11:08:54 +00001542 _rtpRtcpModule->DeRegisterSendPayload(codec.pltype);
1543 if (_rtpRtcpModule->RegisterSendPayload(codec) != 0)
niklase@google.com470e71d2011-07-07 08:21:25 +00001544 {
1545 _engineStatisticsPtr->SetLastError(
1546 VE_RTP_RTCP_MODULE_ERROR, kTraceError,
1547 "SetSendCNPayloadType() failed to register CN to RTP/RTCP "
1548 "module");
1549 return -1;
1550 }
1551 }
1552 return 0;
1553}
1554
minyue@webrtc.orgadee8f92014-09-03 12:28:06 +00001555int Channel::SetOpusMaxPlaybackRate(int frequency_hz) {
minyue@webrtc.org6aac93b2014-08-12 08:13:33 +00001556 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId, _channelId),
minyue@webrtc.orgadee8f92014-09-03 12:28:06 +00001557 "Channel::SetOpusMaxPlaybackRate()");
minyue@webrtc.org6aac93b2014-08-12 08:13:33 +00001558
minyue@webrtc.orgadee8f92014-09-03 12:28:06 +00001559 if (audio_coding_->SetOpusMaxPlaybackRate(frequency_hz) != 0) {
minyue@webrtc.org6aac93b2014-08-12 08:13:33 +00001560 _engineStatisticsPtr->SetLastError(
1561 VE_AUDIO_CODING_MODULE_ERROR, kTraceError,
minyue@webrtc.orgadee8f92014-09-03 12:28:06 +00001562 "SetOpusMaxPlaybackRate() failed to set maximum playback rate");
minyue@webrtc.org6aac93b2014-08-12 08:13:33 +00001563 return -1;
1564 }
1565 return 0;
1566}
1567
pbos@webrtc.org6141e132013-04-09 10:09:10 +00001568int32_t Channel::RegisterExternalTransport(Transport& transport)
niklase@google.com470e71d2011-07-07 08:21:25 +00001569{
1570 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId, _channelId),
1571 "Channel::RegisterExternalTransport()");
1572
mflodman@webrtc.org9a065d12012-03-07 08:12:21 +00001573 CriticalSectionScoped cs(&_callbackCritSect);
niklase@google.com470e71d2011-07-07 08:21:25 +00001574
niklase@google.com470e71d2011-07-07 08:21:25 +00001575 if (_externalTransport)
1576 {
1577 _engineStatisticsPtr->SetLastError(VE_INVALID_OPERATION,
1578 kTraceError,
1579 "RegisterExternalTransport() external transport already enabled");
1580 return -1;
1581 }
1582 _externalTransport = true;
1583 _transportPtr = &transport;
1584 return 0;
1585}
1586
pbos@webrtc.org6141e132013-04-09 10:09:10 +00001587int32_t
niklase@google.com470e71d2011-07-07 08:21:25 +00001588Channel::DeRegisterExternalTransport()
1589{
1590 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId,_channelId),
1591 "Channel::DeRegisterExternalTransport()");
1592
mflodman@webrtc.org9a065d12012-03-07 08:12:21 +00001593 CriticalSectionScoped cs(&_callbackCritSect);
xians@webrtc.org83661f52011-11-25 10:58:15 +00001594
niklase@google.com470e71d2011-07-07 08:21:25 +00001595 if (!_transportPtr)
1596 {
1597 _engineStatisticsPtr->SetLastError(
1598 VE_INVALID_OPERATION, kTraceWarning,
1599 "DeRegisterExternalTransport() external transport already "
1600 "disabled");
1601 return 0;
1602 }
1603 _externalTransport = false;
niklase@google.com470e71d2011-07-07 08:21:25 +00001604 _transportPtr = NULL;
1605 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId,_channelId),
1606 "DeRegisterExternalTransport() all transport is disabled");
niklase@google.com470e71d2011-07-07 08:21:25 +00001607 return 0;
1608}
1609
pkasting@chromium.org4591fbd2014-11-20 22:28:14 +00001610int32_t Channel::ReceivedRTPPacket(const int8_t* data, size_t length,
solenberg@webrtc.orgb1f50102014-03-24 10:38:25 +00001611 const PacketTime& packet_time) {
pwestin@webrtc.org0c459572013-04-03 15:43:57 +00001612 WEBRTC_TRACE(kTraceStream, kTraceVoice, VoEId(_instanceId,_channelId),
1613 "Channel::ReceivedRTPPacket()");
1614
1615 // Store playout timestamp for the received RTP packet
pwestin@webrtc.org1de01352013-04-11 20:23:35 +00001616 UpdatePlayoutTimestamp(false);
pwestin@webrtc.org0c459572013-04-03 15:43:57 +00001617
1618 // Dump the RTP packet to a file (if RTP dump is enabled).
pbos@webrtc.org6141e132013-04-09 10:09:10 +00001619 if (_rtpDumpIn.DumpPacket((const uint8_t*)data,
1620 (uint16_t)length) == -1) {
pwestin@webrtc.org0c459572013-04-03 15:43:57 +00001621 WEBRTC_TRACE(kTraceWarning, kTraceVoice,
1622 VoEId(_instanceId,_channelId),
1623 "Channel::SendPacket() RTP dump to input file failed");
1624 }
stefan@webrtc.org7bb8f022013-09-06 13:40:11 +00001625 const uint8_t* received_packet = reinterpret_cast<const uint8_t*>(data);
stefan@webrtc.orga5cb98c2013-05-29 12:12:51 +00001626 RTPHeader header;
stefan@webrtc.org7bb8f022013-09-06 13:40:11 +00001627 if (!rtp_header_parser_->Parse(received_packet, length, &header)) {
1628 WEBRTC_TRACE(webrtc::kTraceDebug, webrtc::kTraceVoice, _channelId,
1629 "Incoming packet: invalid RTP header");
stefan@webrtc.orga5cb98c2013-05-29 12:12:51 +00001630 return -1;
1631 }
wu@webrtc.org822fbd82013-08-15 23:38:54 +00001632 header.payload_type_frequency =
1633 rtp_payload_registry_->GetPayloadTypeFrequency(header.payloadType);
stefan@webrtc.org7bb8f022013-09-06 13:40:11 +00001634 if (header.payload_type_frequency < 0)
wu@webrtc.org822fbd82013-08-15 23:38:54 +00001635 return -1;
stefan@webrtc.org48df3812013-11-08 15:18:52 +00001636 bool in_order = IsPacketInOrder(header);
stefan@webrtc.org7bb8f022013-09-06 13:40:11 +00001637 rtp_receive_statistics_->IncomingPacket(header, length,
stefan@webrtc.org48df3812013-11-08 15:18:52 +00001638 IsPacketRetransmitted(header, in_order));
stefan@webrtc.org7bb8f022013-09-06 13:40:11 +00001639 rtp_payload_registry_->SetIncomingPayloadType(header);
solenberg@webrtc.orgb1f50102014-03-24 10:38:25 +00001640
1641 // Forward any packets to ViE bandwidth estimator, if enabled.
1642 {
1643 CriticalSectionScoped cs(&_callbackCritSect);
1644 if (vie_network_) {
1645 int64_t arrival_time_ms;
1646 if (packet_time.timestamp != -1) {
1647 arrival_time_ms = (packet_time.timestamp + 500) / 1000;
1648 } else {
1649 arrival_time_ms = TickTime::MillisecondTimestamp();
1650 }
pkasting@chromium.org4591fbd2014-11-20 22:28:14 +00001651 size_t payload_length = length - header.headerLength;
solenberg@webrtc.orgb1f50102014-03-24 10:38:25 +00001652 vie_network_->ReceivedBWEPacket(video_channel_, arrival_time_ms,
1653 payload_length, header);
1654 }
1655 }
1656
stefan@webrtc.org48df3812013-11-08 15:18:52 +00001657 return ReceivePacket(received_packet, length, header, in_order) ? 0 : -1;
stefan@webrtc.org7bb8f022013-09-06 13:40:11 +00001658}
1659
1660bool Channel::ReceivePacket(const uint8_t* packet,
pkasting@chromium.org4591fbd2014-11-20 22:28:14 +00001661 size_t packet_length,
stefan@webrtc.org7bb8f022013-09-06 13:40:11 +00001662 const RTPHeader& header,
1663 bool in_order) {
minyue@webrtc.org456f0142015-01-23 11:58:42 +00001664 if (rtp_payload_registry_->IsRtx(header)) {
1665 return HandleRtxPacket(packet, packet_length, header);
wu@webrtc.org822fbd82013-08-15 23:38:54 +00001666 }
stefan@webrtc.org7bb8f022013-09-06 13:40:11 +00001667 const uint8_t* payload = packet + header.headerLength;
pkasting@chromium.org4591fbd2014-11-20 22:28:14 +00001668 assert(packet_length >= header.headerLength);
1669 size_t payload_length = packet_length - header.headerLength;
wu@webrtc.org822fbd82013-08-15 23:38:54 +00001670 PayloadUnion payload_specific;
1671 if (!rtp_payload_registry_->GetPayloadSpecifics(header.payloadType,
stefan@webrtc.org7bb8f022013-09-06 13:40:11 +00001672 &payload_specific)) {
1673 return false;
wu@webrtc.org822fbd82013-08-15 23:38:54 +00001674 }
stefan@webrtc.org7bb8f022013-09-06 13:40:11 +00001675 return rtp_receiver_->IncomingRtpPacket(header, payload, payload_length,
1676 payload_specific, in_order);
1677}
1678
minyue@webrtc.org456f0142015-01-23 11:58:42 +00001679bool Channel::HandleRtxPacket(const uint8_t* packet,
1680 size_t packet_length,
1681 const RTPHeader& header) {
stefan@webrtc.org7bb8f022013-09-06 13:40:11 +00001682 if (!rtp_payload_registry_->IsRtx(header))
1683 return false;
1684
1685 // Remove the RTX header and parse the original RTP header.
1686 if (packet_length < header.headerLength)
1687 return false;
1688 if (packet_length > kVoiceEngineMaxIpPacketSizeBytes)
1689 return false;
1690 if (restored_packet_in_use_) {
1691 WEBRTC_TRACE(webrtc::kTraceDebug, webrtc::kTraceVoice, _channelId,
1692 "Multiple RTX headers detected, dropping packet");
1693 return false;
pwestin@webrtc.org0c459572013-04-03 15:43:57 +00001694 }
stefan@webrtc.org7bb8f022013-09-06 13:40:11 +00001695 uint8_t* restored_packet_ptr = restored_packet_;
1696 if (!rtp_payload_registry_->RestoreOriginalPacket(
1697 &restored_packet_ptr, packet, &packet_length, rtp_receiver_->SSRC(),
1698 header)) {
1699 WEBRTC_TRACE(webrtc::kTraceDebug, webrtc::kTraceVoice, _channelId,
1700 "Incoming RTX packet: invalid RTP header");
1701 return false;
1702 }
1703 restored_packet_in_use_ = true;
1704 bool ret = OnRecoveredPacket(restored_packet_ptr, packet_length);
1705 restored_packet_in_use_ = false;
1706 return ret;
1707}
1708
1709bool Channel::IsPacketInOrder(const RTPHeader& header) const {
1710 StreamStatistician* statistician =
1711 rtp_receive_statistics_->GetStatistician(header.ssrc);
1712 if (!statistician)
1713 return false;
1714 return statistician->IsPacketInOrder(header.sequenceNumber);
niklase@google.com470e71d2011-07-07 08:21:25 +00001715}
1716
stefan@webrtc.org48df3812013-11-08 15:18:52 +00001717bool Channel::IsPacketRetransmitted(const RTPHeader& header,
1718 bool in_order) const {
stefan@webrtc.org7bb8f022013-09-06 13:40:11 +00001719 // Retransmissions are handled separately if RTX is enabled.
1720 if (rtp_payload_registry_->RtxEnabled())
1721 return false;
1722 StreamStatistician* statistician =
1723 rtp_receive_statistics_->GetStatistician(header.ssrc);
1724 if (!statistician)
1725 return false;
1726 // Check if this is a retransmission.
pkasting@chromium.org16825b12015-01-12 21:51:21 +00001727 int64_t min_rtt = 0;
stefan@webrtc.org7bb8f022013-09-06 13:40:11 +00001728 _rtpRtcpModule->RTT(rtp_receiver_->SSRC(), NULL, NULL, &min_rtt, NULL);
stefan@webrtc.org48df3812013-11-08 15:18:52 +00001729 return !in_order &&
stefan@webrtc.org7bb8f022013-09-06 13:40:11 +00001730 statistician->IsRetransmitOfOldPacket(header, min_rtt);
wu@webrtc.org822fbd82013-08-15 23:38:54 +00001731}
1732
pkasting@chromium.org4591fbd2014-11-20 22:28:14 +00001733int32_t Channel::ReceivedRTCPPacket(const int8_t* data, size_t length) {
pwestin@webrtc.org0c459572013-04-03 15:43:57 +00001734 WEBRTC_TRACE(kTraceStream, kTraceVoice, VoEId(_instanceId,_channelId),
1735 "Channel::ReceivedRTCPPacket()");
1736 // Store playout timestamp for the received RTCP packet
pwestin@webrtc.org1de01352013-04-11 20:23:35 +00001737 UpdatePlayoutTimestamp(true);
pwestin@webrtc.org0c459572013-04-03 15:43:57 +00001738
1739 // Dump the RTCP packet to a file (if RTP dump is enabled).
pkasting@chromium.org4591fbd2014-11-20 22:28:14 +00001740 if (_rtpDumpIn.DumpPacket((const uint8_t*)data, length) == -1) {
pwestin@webrtc.org0c459572013-04-03 15:43:57 +00001741 WEBRTC_TRACE(kTraceWarning, kTraceVoice,
1742 VoEId(_instanceId,_channelId),
1743 "Channel::SendPacket() RTCP dump to input file failed");
1744 }
1745
1746 // Deliver RTCP packet to RTP/RTCP module for parsing
pkasting@chromium.org4591fbd2014-11-20 22:28:14 +00001747 if (_rtpRtcpModule->IncomingRtcpPacket((const uint8_t*)data, length) == -1) {
pwestin@webrtc.org0c459572013-04-03 15:43:57 +00001748 _engineStatisticsPtr->SetLastError(
1749 VE_SOCKET_TRANSPORT_MODULE_ERROR, kTraceWarning,
1750 "Channel::IncomingRTPPacket() RTCP packet is invalid");
1751 }
wu@webrtc.org82c4b852014-05-20 22:55:01 +00001752
stefan@webrtc.org8e24d872014-09-02 18:58:24 +00001753 {
1754 CriticalSectionScoped lock(ts_stats_lock_.get());
pkasting@chromium.org16825b12015-01-12 21:51:21 +00001755 int64_t rtt = GetRTT();
minyue@webrtc.org2c0cdbc2014-10-09 10:52:43 +00001756 if (rtt == 0) {
1757 // Waiting for valid RTT.
1758 return 0;
1759 }
1760 uint32_t ntp_secs = 0;
1761 uint32_t ntp_frac = 0;
1762 uint32_t rtp_timestamp = 0;
1763 if (0 != _rtpRtcpModule->RemoteNTP(&ntp_secs, &ntp_frac, NULL, NULL,
1764 &rtp_timestamp)) {
1765 // Waiting for RTCP.
1766 return 0;
1767 }
1768 ntp_estimator_.UpdateRtcpTimestamp(rtt, ntp_secs, ntp_frac, rtp_timestamp);
stefan@webrtc.org8e24d872014-09-02 18:58:24 +00001769 }
pwestin@webrtc.org0c459572013-04-03 15:43:57 +00001770 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +00001771}
1772
niklase@google.com470e71d2011-07-07 08:21:25 +00001773int Channel::StartPlayingFileLocally(const char* fileName,
pbos@webrtc.org92135212013-05-14 08:31:39 +00001774 bool loop,
1775 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(fileNameUTF8[]=%s, loop=%d,"
1783 " format=%d, volumeScaling=%5.3f, startPosition=%d, "
1784 "stopPosition=%d)", fileName, loop, format, volumeScaling,
1785 startPosition, stopPosition);
1786
henrika@webrtc.org944cbeb2014-03-18 10:32:33 +00001787 if (channel_state_.Get().output_file_playing)
niklase@google.com470e71d2011-07-07 08:21:25 +00001788 {
1789 _engineStatisticsPtr->SetLastError(
1790 VE_ALREADY_PLAYING, kTraceError,
1791 "StartPlayingFileLocally() is already playing");
1792 return -1;
1793 }
1794
niklase@google.com470e71d2011-07-07 08:21:25 +00001795 {
mflodman@webrtc.org9a065d12012-03-07 08:12:21 +00001796 CriticalSectionScoped cs(&_fileCritSect);
henrike@webrtc.orgb37c6282011-10-31 23:53:04 +00001797
1798 if (_outputFilePlayerPtr)
1799 {
1800 _outputFilePlayerPtr->RegisterModuleFileCallback(NULL);
1801 FilePlayer::DestroyFilePlayer(_outputFilePlayerPtr);
1802 _outputFilePlayerPtr = NULL;
1803 }
1804
1805 _outputFilePlayerPtr = FilePlayer::CreateFilePlayer(
1806 _outputFilePlayerId, (const FileFormats)format);
1807
1808 if (_outputFilePlayerPtr == NULL)
1809 {
1810 _engineStatisticsPtr->SetLastError(
1811 VE_INVALID_ARGUMENT, kTraceError,
henrike@webrtc.org31d30702011-11-18 19:59:32 +00001812 "StartPlayingFileLocally() filePlayer format is not correct");
henrike@webrtc.orgb37c6282011-10-31 23:53:04 +00001813 return -1;
1814 }
1815
pbos@webrtc.org6141e132013-04-09 10:09:10 +00001816 const uint32_t notificationTime(0);
henrike@webrtc.orgb37c6282011-10-31 23:53:04 +00001817
1818 if (_outputFilePlayerPtr->StartPlayingFile(
1819 fileName,
1820 loop,
1821 startPosition,
1822 volumeScaling,
1823 notificationTime,
1824 stopPosition,
1825 (const CodecInst*)codecInst) != 0)
1826 {
1827 _engineStatisticsPtr->SetLastError(
1828 VE_BAD_FILE, kTraceError,
1829 "StartPlayingFile() failed to start file playout");
1830 _outputFilePlayerPtr->StopPlayingFile();
1831 FilePlayer::DestroyFilePlayer(_outputFilePlayerPtr);
1832 _outputFilePlayerPtr = NULL;
1833 return -1;
1834 }
1835 _outputFilePlayerPtr->RegisterModuleFileCallback(this);
henrika@webrtc.org944cbeb2014-03-18 10:32:33 +00001836 channel_state_.SetOutputFilePlaying(true);
niklase@google.com470e71d2011-07-07 08:21:25 +00001837 }
braveyao@webrtc.orgab129902012-06-04 03:26:39 +00001838
1839 if (RegisterFilePlayingToMixer() != 0)
henrike@webrtc.org066f9e52011-10-28 23:15:47 +00001840 return -1;
niklase@google.com470e71d2011-07-07 08:21:25 +00001841
1842 return 0;
1843}
1844
1845int Channel::StartPlayingFileLocally(InStream* stream,
pbos@webrtc.org92135212013-05-14 08:31:39 +00001846 FileFormats format,
1847 int startPosition,
1848 float volumeScaling,
1849 int stopPosition,
niklase@google.com470e71d2011-07-07 08:21:25 +00001850 const CodecInst* codecInst)
1851{
1852 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId,_channelId),
1853 "Channel::StartPlayingFileLocally(format=%d,"
1854 " volumeScaling=%5.3f, startPosition=%d, stopPosition=%d)",
1855 format, volumeScaling, startPosition, stopPosition);
1856
1857 if(stream == NULL)
1858 {
1859 _engineStatisticsPtr->SetLastError(
1860 VE_BAD_FILE, kTraceError,
1861 "StartPlayingFileLocally() NULL as input stream");
1862 return -1;
1863 }
1864
1865
henrika@webrtc.org944cbeb2014-03-18 10:32:33 +00001866 if (channel_state_.Get().output_file_playing)
niklase@google.com470e71d2011-07-07 08:21:25 +00001867 {
1868 _engineStatisticsPtr->SetLastError(
1869 VE_ALREADY_PLAYING, kTraceError,
1870 "StartPlayingFileLocally() is already playing");
1871 return -1;
1872 }
1873
niklase@google.com470e71d2011-07-07 08:21:25 +00001874 {
mflodman@webrtc.org9a065d12012-03-07 08:12:21 +00001875 CriticalSectionScoped cs(&_fileCritSect);
henrike@webrtc.orgb37c6282011-10-31 23:53:04 +00001876
1877 // Destroy the old instance
1878 if (_outputFilePlayerPtr)
1879 {
1880 _outputFilePlayerPtr->RegisterModuleFileCallback(NULL);
1881 FilePlayer::DestroyFilePlayer(_outputFilePlayerPtr);
1882 _outputFilePlayerPtr = NULL;
1883 }
1884
1885 // Create the instance
1886 _outputFilePlayerPtr = FilePlayer::CreateFilePlayer(
1887 _outputFilePlayerId,
1888 (const FileFormats)format);
1889
1890 if (_outputFilePlayerPtr == NULL)
1891 {
1892 _engineStatisticsPtr->SetLastError(
1893 VE_INVALID_ARGUMENT, kTraceError,
1894 "StartPlayingFileLocally() filePlayer format isnot correct");
1895 return -1;
1896 }
1897
pbos@webrtc.org6141e132013-04-09 10:09:10 +00001898 const uint32_t notificationTime(0);
henrike@webrtc.orgb37c6282011-10-31 23:53:04 +00001899
1900 if (_outputFilePlayerPtr->StartPlayingFile(*stream, startPosition,
1901 volumeScaling,
1902 notificationTime,
1903 stopPosition, codecInst) != 0)
1904 {
1905 _engineStatisticsPtr->SetLastError(VE_BAD_FILE, kTraceError,
1906 "StartPlayingFile() failed to "
1907 "start file playout");
1908 _outputFilePlayerPtr->StopPlayingFile();
1909 FilePlayer::DestroyFilePlayer(_outputFilePlayerPtr);
1910 _outputFilePlayerPtr = NULL;
1911 return -1;
1912 }
1913 _outputFilePlayerPtr->RegisterModuleFileCallback(this);
henrika@webrtc.org944cbeb2014-03-18 10:32:33 +00001914 channel_state_.SetOutputFilePlaying(true);
niklase@google.com470e71d2011-07-07 08:21:25 +00001915 }
braveyao@webrtc.orgab129902012-06-04 03:26:39 +00001916
1917 if (RegisterFilePlayingToMixer() != 0)
henrike@webrtc.org066f9e52011-10-28 23:15:47 +00001918 return -1;
niklase@google.com470e71d2011-07-07 08:21:25 +00001919
niklase@google.com470e71d2011-07-07 08:21:25 +00001920 return 0;
1921}
1922
1923int Channel::StopPlayingFileLocally()
1924{
1925 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId,_channelId),
1926 "Channel::StopPlayingFileLocally()");
1927
henrika@webrtc.org944cbeb2014-03-18 10:32:33 +00001928 if (!channel_state_.Get().output_file_playing)
niklase@google.com470e71d2011-07-07 08:21:25 +00001929 {
1930 _engineStatisticsPtr->SetLastError(
1931 VE_INVALID_OPERATION, kTraceWarning,
1932 "StopPlayingFileLocally() isnot playing");
1933 return 0;
1934 }
1935
niklase@google.com470e71d2011-07-07 08:21:25 +00001936 {
mflodman@webrtc.org9a065d12012-03-07 08:12:21 +00001937 CriticalSectionScoped cs(&_fileCritSect);
henrike@webrtc.orgb37c6282011-10-31 23:53:04 +00001938
1939 if (_outputFilePlayerPtr->StopPlayingFile() != 0)
1940 {
1941 _engineStatisticsPtr->SetLastError(
1942 VE_STOP_RECORDING_FAILED, kTraceError,
1943 "StopPlayingFile() could not stop playing");
1944 return -1;
1945 }
1946 _outputFilePlayerPtr->RegisterModuleFileCallback(NULL);
1947 FilePlayer::DestroyFilePlayer(_outputFilePlayerPtr);
1948 _outputFilePlayerPtr = NULL;
henrika@webrtc.org944cbeb2014-03-18 10:32:33 +00001949 channel_state_.SetOutputFilePlaying(false);
niklase@google.com470e71d2011-07-07 08:21:25 +00001950 }
henrike@webrtc.orgb37c6282011-10-31 23:53:04 +00001951 // _fileCritSect cannot be taken while calling
1952 // SetAnonymousMixibilityStatus. Refer to comments in
1953 // StartPlayingFileLocally(const char* ...) for more details.
henrike@webrtc.org066f9e52011-10-28 23:15:47 +00001954 if (_outputMixerPtr->SetAnonymousMixabilityStatus(*this, false) != 0)
1955 {
1956 _engineStatisticsPtr->SetLastError(
1957 VE_AUDIO_CONF_MIX_MODULE_ERROR, kTraceError,
henrike@webrtc.orgb37c6282011-10-31 23:53:04 +00001958 "StopPlayingFile() failed to stop participant from playing as"
1959 "file in the mixer");
henrike@webrtc.org066f9e52011-10-28 23:15:47 +00001960 return -1;
1961 }
niklase@google.com470e71d2011-07-07 08:21:25 +00001962
1963 return 0;
1964}
1965
1966int Channel::IsPlayingFileLocally() const
1967{
1968 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId,_channelId),
1969 "Channel::IsPlayingFileLocally()");
1970
henrika@webrtc.org944cbeb2014-03-18 10:32:33 +00001971 return channel_state_.Get().output_file_playing;
niklase@google.com470e71d2011-07-07 08:21:25 +00001972}
1973
braveyao@webrtc.orgab129902012-06-04 03:26:39 +00001974int Channel::RegisterFilePlayingToMixer()
1975{
1976 // Return success for not registering for file playing to mixer if:
1977 // 1. playing file before playout is started on that channel.
1978 // 2. starting playout without file playing on that channel.
henrika@webrtc.org944cbeb2014-03-18 10:32:33 +00001979 if (!channel_state_.Get().playing ||
1980 !channel_state_.Get().output_file_playing)
braveyao@webrtc.orgab129902012-06-04 03:26:39 +00001981 {
1982 return 0;
1983 }
1984
1985 // |_fileCritSect| cannot be taken while calling
1986 // SetAnonymousMixabilityStatus() since as soon as the participant is added
1987 // frames can be pulled by the mixer. Since the frames are generated from
1988 // the file, _fileCritSect will be taken. This would result in a deadlock.
1989 if (_outputMixerPtr->SetAnonymousMixabilityStatus(*this, true) != 0)
1990 {
henrika@webrtc.org944cbeb2014-03-18 10:32:33 +00001991 channel_state_.SetOutputFilePlaying(false);
braveyao@webrtc.orgab129902012-06-04 03:26:39 +00001992 CriticalSectionScoped cs(&_fileCritSect);
braveyao@webrtc.orgab129902012-06-04 03:26:39 +00001993 _engineStatisticsPtr->SetLastError(
1994 VE_AUDIO_CONF_MIX_MODULE_ERROR, kTraceError,
1995 "StartPlayingFile() failed to add participant as file to mixer");
1996 _outputFilePlayerPtr->StopPlayingFile();
1997 FilePlayer::DestroyFilePlayer(_outputFilePlayerPtr);
1998 _outputFilePlayerPtr = NULL;
1999 return -1;
2000 }
2001
2002 return 0;
2003}
2004
niklase@google.com470e71d2011-07-07 08:21:25 +00002005int Channel::StartPlayingFileAsMicrophone(const char* fileName,
pbos@webrtc.org92135212013-05-14 08:31:39 +00002006 bool loop,
2007 FileFormats format,
2008 int startPosition,
2009 float volumeScaling,
2010 int stopPosition,
niklase@google.com470e71d2011-07-07 08:21:25 +00002011 const CodecInst* codecInst)
2012{
2013 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId,_channelId),
2014 "Channel::StartPlayingFileAsMicrophone(fileNameUTF8[]=%s, "
2015 "loop=%d, format=%d, volumeScaling=%5.3f, startPosition=%d, "
2016 "stopPosition=%d)", fileName, loop, format, volumeScaling,
2017 startPosition, stopPosition);
2018
henrika@webrtc.org944cbeb2014-03-18 10:32:33 +00002019 CriticalSectionScoped cs(&_fileCritSect);
2020
2021 if (channel_state_.Get().input_file_playing)
niklase@google.com470e71d2011-07-07 08:21:25 +00002022 {
2023 _engineStatisticsPtr->SetLastError(
2024 VE_ALREADY_PLAYING, kTraceWarning,
2025 "StartPlayingFileAsMicrophone() filePlayer is playing");
2026 return 0;
2027 }
2028
niklase@google.com470e71d2011-07-07 08:21:25 +00002029 // Destroy the old instance
2030 if (_inputFilePlayerPtr)
2031 {
2032 _inputFilePlayerPtr->RegisterModuleFileCallback(NULL);
2033 FilePlayer::DestroyFilePlayer(_inputFilePlayerPtr);
2034 _inputFilePlayerPtr = NULL;
2035 }
2036
2037 // Create the instance
2038 _inputFilePlayerPtr = FilePlayer::CreateFilePlayer(
2039 _inputFilePlayerId, (const FileFormats)format);
2040
2041 if (_inputFilePlayerPtr == NULL)
2042 {
2043 _engineStatisticsPtr->SetLastError(
2044 VE_INVALID_ARGUMENT, kTraceError,
2045 "StartPlayingFileAsMicrophone() filePlayer format isnot correct");
2046 return -1;
2047 }
2048
pbos@webrtc.org6141e132013-04-09 10:09:10 +00002049 const uint32_t notificationTime(0);
niklase@google.com470e71d2011-07-07 08:21:25 +00002050
2051 if (_inputFilePlayerPtr->StartPlayingFile(
2052 fileName,
2053 loop,
2054 startPosition,
2055 volumeScaling,
2056 notificationTime,
2057 stopPosition,
2058 (const CodecInst*)codecInst) != 0)
2059 {
2060 _engineStatisticsPtr->SetLastError(
2061 VE_BAD_FILE, kTraceError,
2062 "StartPlayingFile() failed to start file playout");
2063 _inputFilePlayerPtr->StopPlayingFile();
2064 FilePlayer::DestroyFilePlayer(_inputFilePlayerPtr);
2065 _inputFilePlayerPtr = NULL;
2066 return -1;
2067 }
2068 _inputFilePlayerPtr->RegisterModuleFileCallback(this);
henrika@webrtc.org944cbeb2014-03-18 10:32:33 +00002069 channel_state_.SetInputFilePlaying(true);
niklase@google.com470e71d2011-07-07 08:21:25 +00002070
2071 return 0;
2072}
2073
2074int Channel::StartPlayingFileAsMicrophone(InStream* stream,
pbos@webrtc.org92135212013-05-14 08:31:39 +00002075 FileFormats format,
2076 int startPosition,
2077 float volumeScaling,
2078 int stopPosition,
niklase@google.com470e71d2011-07-07 08:21:25 +00002079 const CodecInst* codecInst)
2080{
2081 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId,_channelId),
2082 "Channel::StartPlayingFileAsMicrophone(format=%d, "
2083 "volumeScaling=%5.3f, startPosition=%d, stopPosition=%d)",
2084 format, volumeScaling, startPosition, stopPosition);
2085
2086 if(stream == NULL)
2087 {
2088 _engineStatisticsPtr->SetLastError(
2089 VE_BAD_FILE, kTraceError,
2090 "StartPlayingFileAsMicrophone NULL as input stream");
2091 return -1;
2092 }
2093
henrika@webrtc.org944cbeb2014-03-18 10:32:33 +00002094 CriticalSectionScoped cs(&_fileCritSect);
2095
2096 if (channel_state_.Get().input_file_playing)
niklase@google.com470e71d2011-07-07 08:21:25 +00002097 {
2098 _engineStatisticsPtr->SetLastError(
2099 VE_ALREADY_PLAYING, kTraceWarning,
2100 "StartPlayingFileAsMicrophone() is playing");
2101 return 0;
2102 }
2103
niklase@google.com470e71d2011-07-07 08:21:25 +00002104 // Destroy the old instance
2105 if (_inputFilePlayerPtr)
2106 {
2107 _inputFilePlayerPtr->RegisterModuleFileCallback(NULL);
2108 FilePlayer::DestroyFilePlayer(_inputFilePlayerPtr);
2109 _inputFilePlayerPtr = NULL;
2110 }
2111
2112 // Create the instance
2113 _inputFilePlayerPtr = FilePlayer::CreateFilePlayer(
2114 _inputFilePlayerId, (const FileFormats)format);
2115
2116 if (_inputFilePlayerPtr == NULL)
2117 {
2118 _engineStatisticsPtr->SetLastError(
2119 VE_INVALID_ARGUMENT, kTraceError,
2120 "StartPlayingInputFile() filePlayer format isnot correct");
2121 return -1;
2122 }
2123
pbos@webrtc.org6141e132013-04-09 10:09:10 +00002124 const uint32_t notificationTime(0);
niklase@google.com470e71d2011-07-07 08:21:25 +00002125
2126 if (_inputFilePlayerPtr->StartPlayingFile(*stream, startPosition,
2127 volumeScaling, notificationTime,
2128 stopPosition, codecInst) != 0)
2129 {
2130 _engineStatisticsPtr->SetLastError(VE_BAD_FILE, kTraceError,
2131 "StartPlayingFile() failed to start "
2132 "file playout");
2133 _inputFilePlayerPtr->StopPlayingFile();
2134 FilePlayer::DestroyFilePlayer(_inputFilePlayerPtr);
2135 _inputFilePlayerPtr = NULL;
2136 return -1;
2137 }
andrew@webrtc.orgae1a58b2013-01-22 04:44:30 +00002138
niklase@google.com470e71d2011-07-07 08:21:25 +00002139 _inputFilePlayerPtr->RegisterModuleFileCallback(this);
henrika@webrtc.org944cbeb2014-03-18 10:32:33 +00002140 channel_state_.SetInputFilePlaying(true);
niklase@google.com470e71d2011-07-07 08:21:25 +00002141
2142 return 0;
2143}
2144
2145int Channel::StopPlayingFileAsMicrophone()
2146{
2147 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId,_channelId),
2148 "Channel::StopPlayingFileAsMicrophone()");
2149
henrika@webrtc.org944cbeb2014-03-18 10:32:33 +00002150 CriticalSectionScoped cs(&_fileCritSect);
2151
2152 if (!channel_state_.Get().input_file_playing)
niklase@google.com470e71d2011-07-07 08:21:25 +00002153 {
2154 _engineStatisticsPtr->SetLastError(
2155 VE_INVALID_OPERATION, kTraceWarning,
2156 "StopPlayingFileAsMicrophone() isnot playing");
2157 return 0;
2158 }
2159
niklase@google.com470e71d2011-07-07 08:21:25 +00002160 if (_inputFilePlayerPtr->StopPlayingFile() != 0)
2161 {
2162 _engineStatisticsPtr->SetLastError(
2163 VE_STOP_RECORDING_FAILED, kTraceError,
2164 "StopPlayingFile() could not stop playing");
2165 return -1;
2166 }
2167 _inputFilePlayerPtr->RegisterModuleFileCallback(NULL);
2168 FilePlayer::DestroyFilePlayer(_inputFilePlayerPtr);
2169 _inputFilePlayerPtr = NULL;
henrika@webrtc.org944cbeb2014-03-18 10:32:33 +00002170 channel_state_.SetInputFilePlaying(false);
niklase@google.com470e71d2011-07-07 08:21:25 +00002171
2172 return 0;
2173}
2174
2175int Channel::IsPlayingFileAsMicrophone() const
2176{
2177 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId,_channelId),
2178 "Channel::IsPlayingFileAsMicrophone()");
henrika@webrtc.org944cbeb2014-03-18 10:32:33 +00002179 return channel_state_.Get().input_file_playing;
niklase@google.com470e71d2011-07-07 08:21:25 +00002180}
2181
leozwang@webrtc.org813e4b02012-03-01 18:34:25 +00002182int Channel::StartRecordingPlayout(const char* fileName,
niklase@google.com470e71d2011-07-07 08:21:25 +00002183 const CodecInst* codecInst)
2184{
2185 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId,_channelId),
2186 "Channel::StartRecordingPlayout(fileName=%s)", fileName);
2187
2188 if (_outputFileRecording)
2189 {
2190 WEBRTC_TRACE(kTraceWarning, kTraceVoice, VoEId(_instanceId,-1),
2191 "StartRecordingPlayout() is already recording");
2192 return 0;
2193 }
2194
2195 FileFormats format;
pbos@webrtc.org6141e132013-04-09 10:09:10 +00002196 const uint32_t notificationTime(0); // Not supported in VoE
niklase@google.com470e71d2011-07-07 08:21:25 +00002197 CodecInst dummyCodec={100,"L16",16000,320,1,320000};
2198
niklas.enbom@webrtc.org40197d72012-03-26 08:45:47 +00002199 if ((codecInst != NULL) &&
2200 ((codecInst->channels < 1) || (codecInst->channels > 2)))
niklase@google.com470e71d2011-07-07 08:21:25 +00002201 {
2202 _engineStatisticsPtr->SetLastError(
2203 VE_BAD_ARGUMENT, kTraceError,
2204 "StartRecordingPlayout() invalid compression");
2205 return(-1);
2206 }
2207 if(codecInst == NULL)
2208 {
2209 format = kFileFormatPcm16kHzFile;
2210 codecInst=&dummyCodec;
2211 }
2212 else if((STR_CASE_CMP(codecInst->plname,"L16") == 0) ||
2213 (STR_CASE_CMP(codecInst->plname,"PCMU") == 0) ||
2214 (STR_CASE_CMP(codecInst->plname,"PCMA") == 0))
2215 {
2216 format = kFileFormatWavFile;
2217 }
2218 else
2219 {
2220 format = kFileFormatCompressedFile;
2221 }
2222
mflodman@webrtc.org9a065d12012-03-07 08:12:21 +00002223 CriticalSectionScoped cs(&_fileCritSect);
niklase@google.com470e71d2011-07-07 08:21:25 +00002224
2225 // Destroy the old instance
2226 if (_outputFileRecorderPtr)
2227 {
2228 _outputFileRecorderPtr->RegisterModuleFileCallback(NULL);
2229 FileRecorder::DestroyFileRecorder(_outputFileRecorderPtr);
2230 _outputFileRecorderPtr = NULL;
2231 }
2232
2233 _outputFileRecorderPtr = FileRecorder::CreateFileRecorder(
2234 _outputFileRecorderId, (const FileFormats)format);
2235 if (_outputFileRecorderPtr == NULL)
2236 {
2237 _engineStatisticsPtr->SetLastError(
2238 VE_INVALID_ARGUMENT, kTraceError,
2239 "StartRecordingPlayout() fileRecorder format isnot correct");
2240 return -1;
2241 }
2242
2243 if (_outputFileRecorderPtr->StartRecordingAudioFile(
2244 fileName, (const CodecInst&)*codecInst, notificationTime) != 0)
2245 {
2246 _engineStatisticsPtr->SetLastError(
2247 VE_BAD_FILE, kTraceError,
2248 "StartRecordingAudioFile() failed to start file recording");
2249 _outputFileRecorderPtr->StopRecording();
2250 FileRecorder::DestroyFileRecorder(_outputFileRecorderPtr);
2251 _outputFileRecorderPtr = NULL;
2252 return -1;
2253 }
2254 _outputFileRecorderPtr->RegisterModuleFileCallback(this);
2255 _outputFileRecording = true;
2256
2257 return 0;
2258}
2259
2260int Channel::StartRecordingPlayout(OutStream* stream,
2261 const CodecInst* codecInst)
2262{
2263 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId,_channelId),
2264 "Channel::StartRecordingPlayout()");
2265
2266 if (_outputFileRecording)
2267 {
2268 WEBRTC_TRACE(kTraceWarning, kTraceVoice, VoEId(_instanceId,-1),
2269 "StartRecordingPlayout() is already recording");
2270 return 0;
2271 }
2272
2273 FileFormats format;
pbos@webrtc.org6141e132013-04-09 10:09:10 +00002274 const uint32_t notificationTime(0); // Not supported in VoE
niklase@google.com470e71d2011-07-07 08:21:25 +00002275 CodecInst dummyCodec={100,"L16",16000,320,1,320000};
2276
2277 if (codecInst != NULL && codecInst->channels != 1)
2278 {
2279 _engineStatisticsPtr->SetLastError(
2280 VE_BAD_ARGUMENT, kTraceError,
2281 "StartRecordingPlayout() invalid compression");
2282 return(-1);
2283 }
2284 if(codecInst == NULL)
2285 {
2286 format = kFileFormatPcm16kHzFile;
2287 codecInst=&dummyCodec;
2288 }
2289 else if((STR_CASE_CMP(codecInst->plname,"L16") == 0) ||
2290 (STR_CASE_CMP(codecInst->plname,"PCMU") == 0) ||
2291 (STR_CASE_CMP(codecInst->plname,"PCMA") == 0))
2292 {
2293 format = kFileFormatWavFile;
2294 }
2295 else
2296 {
2297 format = kFileFormatCompressedFile;
2298 }
2299
mflodman@webrtc.org9a065d12012-03-07 08:12:21 +00002300 CriticalSectionScoped cs(&_fileCritSect);
niklase@google.com470e71d2011-07-07 08:21:25 +00002301
2302 // Destroy the old instance
2303 if (_outputFileRecorderPtr)
2304 {
2305 _outputFileRecorderPtr->RegisterModuleFileCallback(NULL);
2306 FileRecorder::DestroyFileRecorder(_outputFileRecorderPtr);
2307 _outputFileRecorderPtr = NULL;
2308 }
2309
2310 _outputFileRecorderPtr = FileRecorder::CreateFileRecorder(
2311 _outputFileRecorderId, (const FileFormats)format);
2312 if (_outputFileRecorderPtr == NULL)
2313 {
2314 _engineStatisticsPtr->SetLastError(
2315 VE_INVALID_ARGUMENT, kTraceError,
2316 "StartRecordingPlayout() fileRecorder format isnot correct");
2317 return -1;
2318 }
2319
2320 if (_outputFileRecorderPtr->StartRecordingAudioFile(*stream, *codecInst,
2321 notificationTime) != 0)
2322 {
2323 _engineStatisticsPtr->SetLastError(VE_BAD_FILE, kTraceError,
2324 "StartRecordingPlayout() failed to "
2325 "start file recording");
2326 _outputFileRecorderPtr->StopRecording();
2327 FileRecorder::DestroyFileRecorder(_outputFileRecorderPtr);
2328 _outputFileRecorderPtr = NULL;
2329 return -1;
2330 }
andrew@webrtc.orgae1a58b2013-01-22 04:44:30 +00002331
niklase@google.com470e71d2011-07-07 08:21:25 +00002332 _outputFileRecorderPtr->RegisterModuleFileCallback(this);
2333 _outputFileRecording = true;
2334
2335 return 0;
2336}
2337
2338int Channel::StopRecordingPlayout()
2339{
2340 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId,-1),
2341 "Channel::StopRecordingPlayout()");
2342
2343 if (!_outputFileRecording)
2344 {
2345 WEBRTC_TRACE(kTraceError, kTraceVoice, VoEId(_instanceId,-1),
2346 "StopRecordingPlayout() isnot recording");
2347 return -1;
2348 }
2349
2350
mflodman@webrtc.org9a065d12012-03-07 08:12:21 +00002351 CriticalSectionScoped cs(&_fileCritSect);
niklase@google.com470e71d2011-07-07 08:21:25 +00002352
2353 if (_outputFileRecorderPtr->StopRecording() != 0)
2354 {
2355 _engineStatisticsPtr->SetLastError(
2356 VE_STOP_RECORDING_FAILED, kTraceError,
2357 "StopRecording() could not stop recording");
2358 return(-1);
2359 }
2360 _outputFileRecorderPtr->RegisterModuleFileCallback(NULL);
2361 FileRecorder::DestroyFileRecorder(_outputFileRecorderPtr);
2362 _outputFileRecorderPtr = NULL;
2363 _outputFileRecording = false;
2364
2365 return 0;
2366}
2367
2368void
2369Channel::SetMixWithMicStatus(bool mix)
2370{
henrika@webrtc.org944cbeb2014-03-18 10:32:33 +00002371 CriticalSectionScoped cs(&_fileCritSect);
niklase@google.com470e71d2011-07-07 08:21:25 +00002372 _mixFileWithMicrophone=mix;
2373}
2374
2375int
pbos@webrtc.org6141e132013-04-09 10:09:10 +00002376Channel::GetSpeechOutputLevel(uint32_t& level) const
niklase@google.com470e71d2011-07-07 08:21:25 +00002377{
pbos@webrtc.org6141e132013-04-09 10:09:10 +00002378 int8_t currentLevel = _outputAudioLevel.Level();
2379 level = static_cast<int32_t> (currentLevel);
niklase@google.com470e71d2011-07-07 08:21:25 +00002380 WEBRTC_TRACE(kTraceStateInfo, kTraceVoice,
2381 VoEId(_instanceId,_channelId),
2382 "GetSpeechOutputLevel() => level=%u", level);
2383 return 0;
2384}
2385
2386int
pbos@webrtc.org6141e132013-04-09 10:09:10 +00002387Channel::GetSpeechOutputLevelFullRange(uint32_t& level) const
niklase@google.com470e71d2011-07-07 08:21:25 +00002388{
pbos@webrtc.org6141e132013-04-09 10:09:10 +00002389 int16_t currentLevel = _outputAudioLevel.LevelFullRange();
2390 level = static_cast<int32_t> (currentLevel);
niklase@google.com470e71d2011-07-07 08:21:25 +00002391 WEBRTC_TRACE(kTraceStateInfo, kTraceVoice,
2392 VoEId(_instanceId,_channelId),
2393 "GetSpeechOutputLevelFullRange() => level=%u", level);
2394 return 0;
2395}
2396
2397int
2398Channel::SetMute(bool enable)
2399{
wu@webrtc.org63420662013-10-17 18:28:55 +00002400 CriticalSectionScoped cs(&volume_settings_critsect_);
niklase@google.com470e71d2011-07-07 08:21:25 +00002401 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId,_channelId),
2402 "Channel::SetMute(enable=%d)", enable);
2403 _mute = enable;
2404 return 0;
2405}
2406
2407bool
2408Channel::Mute() const
2409{
wu@webrtc.org63420662013-10-17 18:28:55 +00002410 CriticalSectionScoped cs(&volume_settings_critsect_);
niklase@google.com470e71d2011-07-07 08:21:25 +00002411 return _mute;
2412}
2413
2414int
2415Channel::SetOutputVolumePan(float left, float right)
2416{
wu@webrtc.org63420662013-10-17 18:28:55 +00002417 CriticalSectionScoped cs(&volume_settings_critsect_);
niklase@google.com470e71d2011-07-07 08:21:25 +00002418 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId,_channelId),
2419 "Channel::SetOutputVolumePan()");
2420 _panLeft = left;
2421 _panRight = right;
2422 return 0;
2423}
2424
2425int
2426Channel::GetOutputVolumePan(float& left, float& right) const
2427{
wu@webrtc.org63420662013-10-17 18:28:55 +00002428 CriticalSectionScoped cs(&volume_settings_critsect_);
niklase@google.com470e71d2011-07-07 08:21:25 +00002429 left = _panLeft;
2430 right = _panRight;
2431 WEBRTC_TRACE(kTraceStateInfo, kTraceVoice,
2432 VoEId(_instanceId,_channelId),
2433 "GetOutputVolumePan() => left=%3.2f, right=%3.2f", left, right);
2434 return 0;
2435}
2436
2437int
2438Channel::SetChannelOutputVolumeScaling(float scaling)
2439{
wu@webrtc.org63420662013-10-17 18:28:55 +00002440 CriticalSectionScoped cs(&volume_settings_critsect_);
niklase@google.com470e71d2011-07-07 08:21:25 +00002441 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId,_channelId),
2442 "Channel::SetChannelOutputVolumeScaling()");
2443 _outputGain = scaling;
2444 return 0;
2445}
2446
2447int
2448Channel::GetChannelOutputVolumeScaling(float& scaling) const
2449{
wu@webrtc.org63420662013-10-17 18:28:55 +00002450 CriticalSectionScoped cs(&volume_settings_critsect_);
niklase@google.com470e71d2011-07-07 08:21:25 +00002451 scaling = _outputGain;
2452 WEBRTC_TRACE(kTraceStateInfo, kTraceVoice,
2453 VoEId(_instanceId,_channelId),
2454 "GetChannelOutputVolumeScaling() => scaling=%3.2f", scaling);
2455 return 0;
2456}
2457
niklase@google.com470e71d2011-07-07 08:21:25 +00002458int Channel::SendTelephoneEventOutband(unsigned char eventCode,
wu@webrtc.org822fbd82013-08-15 23:38:54 +00002459 int lengthMs, int attenuationDb,
2460 bool playDtmfEvent)
niklase@google.com470e71d2011-07-07 08:21:25 +00002461{
2462 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId, _channelId),
2463 "Channel::SendTelephoneEventOutband(..., playDtmfEvent=%d)",
2464 playDtmfEvent);
2465
2466 _playOutbandDtmfEvent = playDtmfEvent;
2467
pwestin@webrtc.org2853dde2012-05-11 11:08:54 +00002468 if (_rtpRtcpModule->SendTelephoneEventOutband(eventCode, lengthMs,
niklase@google.com470e71d2011-07-07 08:21:25 +00002469 attenuationDb) != 0)
2470 {
2471 _engineStatisticsPtr->SetLastError(
2472 VE_SEND_DTMF_FAILED,
2473 kTraceWarning,
2474 "SendTelephoneEventOutband() failed to send event");
2475 return -1;
2476 }
2477 return 0;
2478}
2479
2480int Channel::SendTelephoneEventInband(unsigned char eventCode,
2481 int lengthMs,
2482 int attenuationDb,
2483 bool playDtmfEvent)
2484{
2485 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId, _channelId),
2486 "Channel::SendTelephoneEventInband(..., playDtmfEvent=%d)",
2487 playDtmfEvent);
2488
2489 _playInbandDtmfEvent = playDtmfEvent;
2490 _inbandDtmfQueue.AddDtmf(eventCode, lengthMs, attenuationDb);
2491
2492 return 0;
2493}
2494
2495int
niklase@google.com470e71d2011-07-07 08:21:25 +00002496Channel::SetSendTelephoneEventPayloadType(unsigned char type)
2497{
2498 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId,_channelId),
2499 "Channel::SetSendTelephoneEventPayloadType()");
andrew@webrtc.orgf81f9f82011-08-19 22:56:22 +00002500 if (type > 127)
niklase@google.com470e71d2011-07-07 08:21:25 +00002501 {
2502 _engineStatisticsPtr->SetLastError(
2503 VE_INVALID_ARGUMENT, kTraceError,
2504 "SetSendTelephoneEventPayloadType() invalid type");
2505 return -1;
2506 }
pbos@webrtc.org5b10d8f2013-07-11 15:50:07 +00002507 CodecInst codec = {};
pwestin@webrtc.org1da1ce02011-10-13 15:19:55 +00002508 codec.plfreq = 8000;
2509 codec.pltype = type;
2510 memcpy(codec.plname, "telephone-event", 16);
pwestin@webrtc.org2853dde2012-05-11 11:08:54 +00002511 if (_rtpRtcpModule->RegisterSendPayload(codec) != 0)
niklase@google.com470e71d2011-07-07 08:21:25 +00002512 {
henrika@webrtc.org4392d5f2013-04-17 07:34:25 +00002513 _rtpRtcpModule->DeRegisterSendPayload(codec.pltype);
2514 if (_rtpRtcpModule->RegisterSendPayload(codec) != 0) {
2515 _engineStatisticsPtr->SetLastError(
2516 VE_RTP_RTCP_MODULE_ERROR, kTraceError,
2517 "SetSendTelephoneEventPayloadType() failed to register send"
2518 "payload type");
2519 return -1;
2520 }
niklase@google.com470e71d2011-07-07 08:21:25 +00002521 }
2522 _sendTelephoneEventPayloadType = type;
2523 return 0;
2524}
2525
2526int
2527Channel::GetSendTelephoneEventPayloadType(unsigned char& type)
2528{
2529 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId,_channelId),
2530 "Channel::GetSendTelephoneEventPayloadType()");
2531 type = _sendTelephoneEventPayloadType;
2532 WEBRTC_TRACE(kTraceStateInfo, kTraceVoice,
2533 VoEId(_instanceId,_channelId),
2534 "GetSendTelephoneEventPayloadType() => type=%u", type);
2535 return 0;
2536}
2537
niklase@google.com470e71d2011-07-07 08:21:25 +00002538int
2539Channel::UpdateRxVadDetection(AudioFrame& audioFrame)
2540{
2541 WEBRTC_TRACE(kTraceStream, kTraceVoice, VoEId(_instanceId,_channelId),
2542 "Channel::UpdateRxVadDetection()");
2543
2544 int vadDecision = 1;
2545
andrew@webrtc.org63a50982012-05-02 23:56:37 +00002546 vadDecision = (audioFrame.vad_activity_ == AudioFrame::kVadActive)? 1 : 0;
niklase@google.com470e71d2011-07-07 08:21:25 +00002547
2548 if ((vadDecision != _oldVadDecision) && _rxVadObserverPtr)
2549 {
2550 OnRxVadDetected(vadDecision);
2551 _oldVadDecision = vadDecision;
2552 }
2553
2554 WEBRTC_TRACE(kTraceStream, kTraceVoice, VoEId(_instanceId,_channelId),
2555 "Channel::UpdateRxVadDetection() => vadDecision=%d",
2556 vadDecision);
2557 return 0;
2558}
2559
2560int
2561Channel::RegisterRxVadObserver(VoERxVadCallback &observer)
2562{
2563 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId,_channelId),
2564 "Channel::RegisterRxVadObserver()");
mflodman@webrtc.org9a065d12012-03-07 08:12:21 +00002565 CriticalSectionScoped cs(&_callbackCritSect);
niklase@google.com470e71d2011-07-07 08:21:25 +00002566
2567 if (_rxVadObserverPtr)
2568 {
2569 _engineStatisticsPtr->SetLastError(
2570 VE_INVALID_OPERATION, kTraceError,
2571 "RegisterRxVadObserver() observer already enabled");
2572 return -1;
2573 }
niklase@google.com470e71d2011-07-07 08:21:25 +00002574 _rxVadObserverPtr = &observer;
2575 _RxVadDetection = true;
2576 return 0;
2577}
2578
2579int
2580Channel::DeRegisterRxVadObserver()
2581{
2582 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId,_channelId),
2583 "Channel::DeRegisterRxVadObserver()");
mflodman@webrtc.org9a065d12012-03-07 08:12:21 +00002584 CriticalSectionScoped cs(&_callbackCritSect);
niklase@google.com470e71d2011-07-07 08:21:25 +00002585
2586 if (!_rxVadObserverPtr)
2587 {
2588 _engineStatisticsPtr->SetLastError(
2589 VE_INVALID_OPERATION, kTraceWarning,
2590 "DeRegisterRxVadObserver() observer already disabled");
2591 return 0;
2592 }
2593 _rxVadObserverPtr = NULL;
2594 _RxVadDetection = false;
2595 return 0;
2596}
2597
2598int
2599Channel::VoiceActivityIndicator(int &activity)
2600{
2601 activity = _sendFrameType;
2602
2603 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId,_channelId),
andrew@webrtc.org6c264cc2013-10-04 17:54:09 +00002604 "Channel::VoiceActivityIndicator(indicator=%d)", activity);
niklase@google.com470e71d2011-07-07 08:21:25 +00002605 return 0;
2606}
2607
2608#ifdef WEBRTC_VOICE_ENGINE_AGC
2609
2610int
pbos@webrtc.org92135212013-05-14 08:31:39 +00002611Channel::SetRxAgcStatus(bool enable, AgcModes mode)
niklase@google.com470e71d2011-07-07 08:21:25 +00002612{
2613 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId,_channelId),
2614 "Channel::SetRxAgcStatus(enable=%d, mode=%d)",
2615 (int)enable, (int)mode);
2616
andrew@webrtc.org6c264cc2013-10-04 17:54:09 +00002617 GainControl::Mode agcMode = kDefaultRxAgcMode;
niklase@google.com470e71d2011-07-07 08:21:25 +00002618 switch (mode)
2619 {
2620 case kAgcDefault:
niklase@google.com470e71d2011-07-07 08:21:25 +00002621 break;
2622 case kAgcUnchanged:
andrew@webrtc.orgf3930e92013-09-18 22:37:32 +00002623 agcMode = rx_audioproc_->gain_control()->mode();
niklase@google.com470e71d2011-07-07 08:21:25 +00002624 break;
2625 case kAgcFixedDigital:
2626 agcMode = GainControl::kFixedDigital;
2627 break;
2628 case kAgcAdaptiveDigital:
2629 agcMode =GainControl::kAdaptiveDigital;
2630 break;
2631 default:
2632 _engineStatisticsPtr->SetLastError(
2633 VE_INVALID_ARGUMENT, kTraceError,
2634 "SetRxAgcStatus() invalid Agc mode");
2635 return -1;
2636 }
2637
andrew@webrtc.orgf3930e92013-09-18 22:37:32 +00002638 if (rx_audioproc_->gain_control()->set_mode(agcMode) != 0)
niklase@google.com470e71d2011-07-07 08:21:25 +00002639 {
2640 _engineStatisticsPtr->SetLastError(
2641 VE_APM_ERROR, kTraceError,
2642 "SetRxAgcStatus() failed to set Agc mode");
2643 return -1;
2644 }
andrew@webrtc.orgf3930e92013-09-18 22:37:32 +00002645 if (rx_audioproc_->gain_control()->Enable(enable) != 0)
niklase@google.com470e71d2011-07-07 08:21:25 +00002646 {
2647 _engineStatisticsPtr->SetLastError(
2648 VE_APM_ERROR, kTraceError,
2649 "SetRxAgcStatus() failed to set Agc state");
2650 return -1;
2651 }
2652
2653 _rxAgcIsEnabled = enable;
henrika@webrtc.org944cbeb2014-03-18 10:32:33 +00002654 channel_state_.SetRxApmIsEnabled(_rxAgcIsEnabled || _rxNsIsEnabled);
niklase@google.com470e71d2011-07-07 08:21:25 +00002655
2656 return 0;
2657}
2658
2659int
2660Channel::GetRxAgcStatus(bool& enabled, AgcModes& mode)
2661{
2662 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId,_channelId),
2663 "Channel::GetRxAgcStatus(enable=?, mode=?)");
2664
andrew@webrtc.orgf3930e92013-09-18 22:37:32 +00002665 bool enable = rx_audioproc_->gain_control()->is_enabled();
niklase@google.com470e71d2011-07-07 08:21:25 +00002666 GainControl::Mode agcMode =
andrew@webrtc.orgf3930e92013-09-18 22:37:32 +00002667 rx_audioproc_->gain_control()->mode();
niklase@google.com470e71d2011-07-07 08:21:25 +00002668
2669 enabled = enable;
2670
2671 switch (agcMode)
2672 {
2673 case GainControl::kFixedDigital:
2674 mode = kAgcFixedDigital;
2675 break;
2676 case GainControl::kAdaptiveDigital:
2677 mode = kAgcAdaptiveDigital;
2678 break;
2679 default:
2680 _engineStatisticsPtr->SetLastError(
2681 VE_APM_ERROR, kTraceError,
2682 "GetRxAgcStatus() invalid Agc mode");
2683 return -1;
2684 }
2685
2686 return 0;
2687}
2688
2689int
pbos@webrtc.org92135212013-05-14 08:31:39 +00002690Channel::SetRxAgcConfig(AgcConfig config)
niklase@google.com470e71d2011-07-07 08:21:25 +00002691{
2692 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId,_channelId),
2693 "Channel::SetRxAgcConfig()");
2694
andrew@webrtc.orgf3930e92013-09-18 22:37:32 +00002695 if (rx_audioproc_->gain_control()->set_target_level_dbfs(
niklase@google.com470e71d2011-07-07 08:21:25 +00002696 config.targetLeveldBOv) != 0)
2697 {
2698 _engineStatisticsPtr->SetLastError(
2699 VE_APM_ERROR, kTraceError,
2700 "SetRxAgcConfig() failed to set target peak |level|"
2701 "(or envelope) of the Agc");
2702 return -1;
2703 }
andrew@webrtc.orgf3930e92013-09-18 22:37:32 +00002704 if (rx_audioproc_->gain_control()->set_compression_gain_db(
niklase@google.com470e71d2011-07-07 08:21:25 +00002705 config.digitalCompressionGaindB) != 0)
2706 {
2707 _engineStatisticsPtr->SetLastError(
2708 VE_APM_ERROR, kTraceError,
2709 "SetRxAgcConfig() failed to set the range in |gain| the"
2710 " digital compression stage may apply");
2711 return -1;
2712 }
andrew@webrtc.orgf3930e92013-09-18 22:37:32 +00002713 if (rx_audioproc_->gain_control()->enable_limiter(
niklase@google.com470e71d2011-07-07 08:21:25 +00002714 config.limiterEnable) != 0)
2715 {
2716 _engineStatisticsPtr->SetLastError(
2717 VE_APM_ERROR, kTraceError,
2718 "SetRxAgcConfig() failed to set hard limiter to the signal");
2719 return -1;
2720 }
2721
2722 return 0;
2723}
2724
2725int
2726Channel::GetRxAgcConfig(AgcConfig& config)
2727{
2728 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId,_channelId),
2729 "Channel::GetRxAgcConfig(config=%?)");
2730
2731 config.targetLeveldBOv =
andrew@webrtc.orgf3930e92013-09-18 22:37:32 +00002732 rx_audioproc_->gain_control()->target_level_dbfs();
niklase@google.com470e71d2011-07-07 08:21:25 +00002733 config.digitalCompressionGaindB =
andrew@webrtc.orgf3930e92013-09-18 22:37:32 +00002734 rx_audioproc_->gain_control()->compression_gain_db();
niklase@google.com470e71d2011-07-07 08:21:25 +00002735 config.limiterEnable =
andrew@webrtc.orgf3930e92013-09-18 22:37:32 +00002736 rx_audioproc_->gain_control()->is_limiter_enabled();
niklase@google.com470e71d2011-07-07 08:21:25 +00002737
2738 WEBRTC_TRACE(kTraceStateInfo, kTraceVoice,
2739 VoEId(_instanceId,_channelId), "GetRxAgcConfig() => "
2740 "targetLeveldBOv=%u, digitalCompressionGaindB=%u,"
2741 " limiterEnable=%d",
2742 config.targetLeveldBOv,
2743 config.digitalCompressionGaindB,
2744 config.limiterEnable);
2745
2746 return 0;
2747}
2748
2749#endif // #ifdef WEBRTC_VOICE_ENGINE_AGC
2750
2751#ifdef WEBRTC_VOICE_ENGINE_NR
2752
2753int
pbos@webrtc.org92135212013-05-14 08:31:39 +00002754Channel::SetRxNsStatus(bool enable, NsModes mode)
niklase@google.com470e71d2011-07-07 08:21:25 +00002755{
2756 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId,_channelId),
2757 "Channel::SetRxNsStatus(enable=%d, mode=%d)",
2758 (int)enable, (int)mode);
2759
andrew@webrtc.org6c264cc2013-10-04 17:54:09 +00002760 NoiseSuppression::Level nsLevel = kDefaultNsMode;
niklase@google.com470e71d2011-07-07 08:21:25 +00002761 switch (mode)
2762 {
2763
2764 case kNsDefault:
niklase@google.com470e71d2011-07-07 08:21:25 +00002765 break;
2766 case kNsUnchanged:
andrew@webrtc.orgf3930e92013-09-18 22:37:32 +00002767 nsLevel = rx_audioproc_->noise_suppression()->level();
niklase@google.com470e71d2011-07-07 08:21:25 +00002768 break;
2769 case kNsConference:
2770 nsLevel = NoiseSuppression::kHigh;
2771 break;
2772 case kNsLowSuppression:
2773 nsLevel = NoiseSuppression::kLow;
2774 break;
2775 case kNsModerateSuppression:
2776 nsLevel = NoiseSuppression::kModerate;
2777 break;
2778 case kNsHighSuppression:
2779 nsLevel = NoiseSuppression::kHigh;
2780 break;
2781 case kNsVeryHighSuppression:
2782 nsLevel = NoiseSuppression::kVeryHigh;
2783 break;
niklase@google.com470e71d2011-07-07 08:21:25 +00002784 }
2785
andrew@webrtc.orgf3930e92013-09-18 22:37:32 +00002786 if (rx_audioproc_->noise_suppression()->set_level(nsLevel)
niklase@google.com470e71d2011-07-07 08:21:25 +00002787 != 0)
2788 {
2789 _engineStatisticsPtr->SetLastError(
2790 VE_APM_ERROR, kTraceError,
andrew@webrtc.org6c264cc2013-10-04 17:54:09 +00002791 "SetRxNsStatus() failed to set NS level");
niklase@google.com470e71d2011-07-07 08:21:25 +00002792 return -1;
2793 }
andrew@webrtc.orgf3930e92013-09-18 22:37:32 +00002794 if (rx_audioproc_->noise_suppression()->Enable(enable) != 0)
niklase@google.com470e71d2011-07-07 08:21:25 +00002795 {
2796 _engineStatisticsPtr->SetLastError(
2797 VE_APM_ERROR, kTraceError,
andrew@webrtc.org6c264cc2013-10-04 17:54:09 +00002798 "SetRxNsStatus() failed to set NS state");
niklase@google.com470e71d2011-07-07 08:21:25 +00002799 return -1;
2800 }
2801
2802 _rxNsIsEnabled = enable;
henrika@webrtc.org944cbeb2014-03-18 10:32:33 +00002803 channel_state_.SetRxApmIsEnabled(_rxAgcIsEnabled || _rxNsIsEnabled);
niklase@google.com470e71d2011-07-07 08:21:25 +00002804
2805 return 0;
2806}
2807
2808int
2809Channel::GetRxNsStatus(bool& enabled, NsModes& mode)
2810{
2811 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId,_channelId),
2812 "Channel::GetRxNsStatus(enable=?, mode=?)");
2813
2814 bool enable =
andrew@webrtc.orgf3930e92013-09-18 22:37:32 +00002815 rx_audioproc_->noise_suppression()->is_enabled();
niklase@google.com470e71d2011-07-07 08:21:25 +00002816 NoiseSuppression::Level ncLevel =
andrew@webrtc.orgf3930e92013-09-18 22:37:32 +00002817 rx_audioproc_->noise_suppression()->level();
niklase@google.com470e71d2011-07-07 08:21:25 +00002818
2819 enabled = enable;
2820
2821 switch (ncLevel)
2822 {
2823 case NoiseSuppression::kLow:
2824 mode = kNsLowSuppression;
2825 break;
2826 case NoiseSuppression::kModerate:
2827 mode = kNsModerateSuppression;
2828 break;
2829 case NoiseSuppression::kHigh:
2830 mode = kNsHighSuppression;
2831 break;
2832 case NoiseSuppression::kVeryHigh:
2833 mode = kNsVeryHighSuppression;
2834 break;
niklase@google.com470e71d2011-07-07 08:21:25 +00002835 }
2836
2837 WEBRTC_TRACE(kTraceStateInfo, kTraceVoice,
2838 VoEId(_instanceId,_channelId),
2839 "GetRxNsStatus() => enabled=%d, mode=%d", enabled, mode);
2840 return 0;
2841}
2842
2843#endif // #ifdef WEBRTC_VOICE_ENGINE_NR
2844
2845int
niklase@google.com470e71d2011-07-07 08:21:25 +00002846Channel::SetLocalSSRC(unsigned int ssrc)
2847{
2848 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId, _channelId),
2849 "Channel::SetLocalSSRC()");
henrika@webrtc.org944cbeb2014-03-18 10:32:33 +00002850 if (channel_state_.Get().sending)
niklase@google.com470e71d2011-07-07 08:21:25 +00002851 {
2852 _engineStatisticsPtr->SetLastError(
2853 VE_ALREADY_SENDING, kTraceError,
2854 "SetLocalSSRC() already sending");
2855 return -1;
2856 }
stefan@webrtc.orgef927552014-06-05 08:25:29 +00002857 _rtpRtcpModule->SetSSRC(ssrc);
niklase@google.com470e71d2011-07-07 08:21:25 +00002858 return 0;
2859}
2860
2861int
2862Channel::GetLocalSSRC(unsigned int& ssrc)
2863{
pwestin@webrtc.org2853dde2012-05-11 11:08:54 +00002864 ssrc = _rtpRtcpModule->SSRC();
niklase@google.com470e71d2011-07-07 08:21:25 +00002865 WEBRTC_TRACE(kTraceStateInfo, kTraceVoice,
2866 VoEId(_instanceId,_channelId),
2867 "GetLocalSSRC() => ssrc=%lu", ssrc);
2868 return 0;
2869}
2870
2871int
2872Channel::GetRemoteSSRC(unsigned int& ssrc)
2873{
wu@webrtc.org822fbd82013-08-15 23:38:54 +00002874 ssrc = rtp_receiver_->SSRC();
niklase@google.com470e71d2011-07-07 08:21:25 +00002875 WEBRTC_TRACE(kTraceStateInfo, kTraceVoice,
2876 VoEId(_instanceId,_channelId),
2877 "GetRemoteSSRC() => ssrc=%lu", ssrc);
2878 return 0;
2879}
2880
wu@webrtc.orgebdb0e32014-03-06 23:49:08 +00002881int Channel::SetSendAudioLevelIndicationStatus(bool enable, unsigned char id) {
andrew@webrtc.orgf3930e92013-09-18 22:37:32 +00002882 _includeAudioLevelIndication = enable;
wu@webrtc.orgebdb0e32014-03-06 23:49:08 +00002883 return SetSendRtpHeaderExtension(enable, kRtpExtensionAudioLevel, id);
niklase@google.com470e71d2011-07-07 08:21:25 +00002884}
andrew@webrtc.orgf3930e92013-09-18 22:37:32 +00002885
wu@webrtc.org93fd25c2014-04-24 20:33:08 +00002886int Channel::SetReceiveAudioLevelIndicationStatus(bool enable,
2887 unsigned char id) {
2888 rtp_header_parser_->DeregisterRtpHeaderExtension(
2889 kRtpExtensionAudioLevel);
2890 if (enable && !rtp_header_parser_->RegisterRtpHeaderExtension(
2891 kRtpExtensionAudioLevel, id)) {
2892 return -1;
2893 }
2894 return 0;
2895}
2896
wu@webrtc.orgebdb0e32014-03-06 23:49:08 +00002897int Channel::SetSendAbsoluteSenderTimeStatus(bool enable, unsigned char id) {
2898 return SetSendRtpHeaderExtension(enable, kRtpExtensionAbsoluteSendTime, id);
2899}
2900
2901int Channel::SetReceiveAbsoluteSenderTimeStatus(bool enable, unsigned char id) {
2902 rtp_header_parser_->DeregisterRtpHeaderExtension(
2903 kRtpExtensionAbsoluteSendTime);
solenberg@webrtc.orgb1f50102014-03-24 10:38:25 +00002904 if (enable && !rtp_header_parser_->RegisterRtpHeaderExtension(
2905 kRtpExtensionAbsoluteSendTime, id)) {
2906 return -1;
wu@webrtc.orgebdb0e32014-03-06 23:49:08 +00002907 }
2908 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +00002909}
2910
pbos@webrtc.orgd16e8392014-12-19 13:49:55 +00002911void Channel::SetRTCPStatus(bool enable) {
2912 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId, _channelId),
2913 "Channel::SetRTCPStatus()");
2914 _rtpRtcpModule->SetRTCPStatus(enable ? kRtcpCompound : kRtcpOff);
niklase@google.com470e71d2011-07-07 08:21:25 +00002915}
2916
2917int
2918Channel::GetRTCPStatus(bool& enabled)
2919{
pwestin@webrtc.org2853dde2012-05-11 11:08:54 +00002920 RTCPMethod method = _rtpRtcpModule->RTCP();
niklase@google.com470e71d2011-07-07 08:21:25 +00002921 enabled = (method != kRtcpOff);
2922 WEBRTC_TRACE(kTraceStateInfo, kTraceVoice,
2923 VoEId(_instanceId,_channelId),
2924 "GetRTCPStatus() => enabled=%d", enabled);
2925 return 0;
2926}
2927
2928int
2929Channel::SetRTCP_CNAME(const char cName[256])
2930{
2931 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId, _channelId),
2932 "Channel::SetRTCP_CNAME()");
pwestin@webrtc.org2853dde2012-05-11 11:08:54 +00002933 if (_rtpRtcpModule->SetCNAME(cName) != 0)
niklase@google.com470e71d2011-07-07 08:21:25 +00002934 {
2935 _engineStatisticsPtr->SetLastError(
2936 VE_RTP_RTCP_MODULE_ERROR, kTraceError,
2937 "SetRTCP_CNAME() failed to set RTCP CNAME");
2938 return -1;
2939 }
2940 return 0;
2941}
2942
2943int
niklase@google.com470e71d2011-07-07 08:21:25 +00002944Channel::GetRemoteRTCP_CNAME(char cName[256])
2945{
2946 if (cName == NULL)
2947 {
2948 _engineStatisticsPtr->SetLastError(
2949 VE_INVALID_ARGUMENT, kTraceError,
2950 "GetRemoteRTCP_CNAME() invalid CNAME input buffer");
2951 return -1;
2952 }
leozwang@webrtc.org813e4b02012-03-01 18:34:25 +00002953 char cname[RTCP_CNAME_SIZE];
wu@webrtc.org822fbd82013-08-15 23:38:54 +00002954 const uint32_t remoteSSRC = rtp_receiver_->SSRC();
pwestin@webrtc.org2853dde2012-05-11 11:08:54 +00002955 if (_rtpRtcpModule->RemoteCNAME(remoteSSRC, cname) != 0)
niklase@google.com470e71d2011-07-07 08:21:25 +00002956 {
2957 _engineStatisticsPtr->SetLastError(
2958 VE_CANNOT_RETRIEVE_CNAME, kTraceError,
2959 "GetRemoteRTCP_CNAME() failed to retrieve remote RTCP CNAME");
2960 return -1;
2961 }
2962 strcpy(cName, cname);
2963 WEBRTC_TRACE(kTraceStateInfo, kTraceVoice,
2964 VoEId(_instanceId, _channelId),
2965 "GetRemoteRTCP_CNAME() => cName=%s", cName);
2966 return 0;
2967}
2968
2969int
2970Channel::GetRemoteRTCPData(
2971 unsigned int& NTPHigh,
2972 unsigned int& NTPLow,
2973 unsigned int& timestamp,
2974 unsigned int& playoutTimestamp,
2975 unsigned int* jitter,
2976 unsigned short* fractionLost)
2977{
2978 // --- Information from sender info in received Sender Reports
2979
2980 RTCPSenderInfo senderInfo;
pwestin@webrtc.org2853dde2012-05-11 11:08:54 +00002981 if (_rtpRtcpModule->RemoteRTCPStat(&senderInfo) != 0)
niklase@google.com470e71d2011-07-07 08:21:25 +00002982 {
2983 _engineStatisticsPtr->SetLastError(
2984 VE_RTP_RTCP_MODULE_ERROR, kTraceError,
wu@webrtc.orgfcd12b32011-09-15 20:49:50 +00002985 "GetRemoteRTCPData() failed to retrieve sender info for remote "
niklase@google.com470e71d2011-07-07 08:21:25 +00002986 "side");
2987 return -1;
2988 }
2989
2990 // We only utilize 12 out of 20 bytes in the sender info (ignores packet
2991 // and octet count)
2992 NTPHigh = senderInfo.NTPseconds;
2993 NTPLow = senderInfo.NTPfraction;
2994 timestamp = senderInfo.RTPtimeStamp;
2995
2996 WEBRTC_TRACE(kTraceStateInfo, kTraceVoice,
2997 VoEId(_instanceId, _channelId),
2998 "GetRemoteRTCPData() => NTPHigh=%lu, NTPLow=%lu, "
2999 "timestamp=%lu",
3000 NTPHigh, NTPLow, timestamp);
3001
3002 // --- Locally derived information
3003
3004 // This value is updated on each incoming RTCP packet (0 when no packet
3005 // has been received)
pwestin@webrtc.org1de01352013-04-11 20:23:35 +00003006 playoutTimestamp = playout_timestamp_rtcp_;
niklase@google.com470e71d2011-07-07 08:21:25 +00003007
3008 WEBRTC_TRACE(kTraceStateInfo, kTraceVoice,
3009 VoEId(_instanceId, _channelId),
3010 "GetRemoteRTCPData() => playoutTimestamp=%lu",
pwestin@webrtc.org1de01352013-04-11 20:23:35 +00003011 playout_timestamp_rtcp_);
niklase@google.com470e71d2011-07-07 08:21:25 +00003012
3013 if (NULL != jitter || NULL != fractionLost)
3014 {
perkj@webrtc.orgce5990c2012-01-11 13:00:08 +00003015 // Get all RTCP receiver report blocks that have been received on this
3016 // channel. If we receive RTP packets from a remote source we know the
3017 // remote SSRC and use the report block from him.
3018 // Otherwise use the first report block.
3019 std::vector<RTCPReportBlock> remote_stats;
pwestin@webrtc.org2853dde2012-05-11 11:08:54 +00003020 if (_rtpRtcpModule->RemoteRTCPStat(&remote_stats) != 0 ||
perkj@webrtc.orgce5990c2012-01-11 13:00:08 +00003021 remote_stats.empty()) {
3022 WEBRTC_TRACE(kTraceWarning, kTraceVoice,
3023 VoEId(_instanceId, _channelId),
3024 "GetRemoteRTCPData() failed to measure statistics due"
3025 " to lack of received RTP and/or RTCP packets");
3026 return -1;
niklase@google.com470e71d2011-07-07 08:21:25 +00003027 }
perkj@webrtc.orgce5990c2012-01-11 13:00:08 +00003028
wu@webrtc.org822fbd82013-08-15 23:38:54 +00003029 uint32_t remoteSSRC = rtp_receiver_->SSRC();
perkj@webrtc.orgce5990c2012-01-11 13:00:08 +00003030 std::vector<RTCPReportBlock>::const_iterator it = remote_stats.begin();
3031 for (; it != remote_stats.end(); ++it) {
3032 if (it->remoteSSRC == remoteSSRC)
3033 break;
niklase@google.com470e71d2011-07-07 08:21:25 +00003034 }
perkj@webrtc.orgce5990c2012-01-11 13:00:08 +00003035
3036 if (it == remote_stats.end()) {
3037 // If we have not received any RTCP packets from this SSRC it probably
3038 // means that we have not received any RTP packets.
3039 // Use the first received report block instead.
3040 it = remote_stats.begin();
3041 remoteSSRC = it->remoteSSRC;
niklase@google.com470e71d2011-07-07 08:21:25 +00003042 }
perkj@webrtc.orgce5990c2012-01-11 13:00:08 +00003043
xians@webrtc.org79af7342012-01-31 12:22:14 +00003044 if (jitter) {
3045 *jitter = it->jitter;
3046 WEBRTC_TRACE(kTraceStateInfo, kTraceVoice,
3047 VoEId(_instanceId, _channelId),
3048 "GetRemoteRTCPData() => jitter = %lu", *jitter);
3049 }
perkj@webrtc.orgce5990c2012-01-11 13:00:08 +00003050
xians@webrtc.org79af7342012-01-31 12:22:14 +00003051 if (fractionLost) {
3052 *fractionLost = it->fractionLost;
3053 WEBRTC_TRACE(kTraceStateInfo, kTraceVoice,
3054 VoEId(_instanceId, _channelId),
3055 "GetRemoteRTCPData() => fractionLost = %lu",
3056 *fractionLost);
3057 }
niklase@google.com470e71d2011-07-07 08:21:25 +00003058 }
3059 return 0;
3060}
3061
3062int
pbos@webrtc.org92135212013-05-14 08:31:39 +00003063Channel::SendApplicationDefinedRTCPPacket(unsigned char subType,
niklase@google.com470e71d2011-07-07 08:21:25 +00003064 unsigned int name,
3065 const char* data,
3066 unsigned short dataLengthInBytes)
3067{
3068 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId, _channelId),
3069 "Channel::SendApplicationDefinedRTCPPacket()");
henrika@webrtc.org944cbeb2014-03-18 10:32:33 +00003070 if (!channel_state_.Get().sending)
niklase@google.com470e71d2011-07-07 08:21:25 +00003071 {
3072 _engineStatisticsPtr->SetLastError(
3073 VE_NOT_SENDING, kTraceError,
3074 "SendApplicationDefinedRTCPPacket() not sending");
3075 return -1;
3076 }
3077 if (NULL == data)
3078 {
3079 _engineStatisticsPtr->SetLastError(
3080 VE_INVALID_ARGUMENT, kTraceError,
3081 "SendApplicationDefinedRTCPPacket() invalid data value");
3082 return -1;
3083 }
3084 if (dataLengthInBytes % 4 != 0)
3085 {
3086 _engineStatisticsPtr->SetLastError(
3087 VE_INVALID_ARGUMENT, kTraceError,
3088 "SendApplicationDefinedRTCPPacket() invalid length value");
3089 return -1;
3090 }
pwestin@webrtc.org2853dde2012-05-11 11:08:54 +00003091 RTCPMethod status = _rtpRtcpModule->RTCP();
niklase@google.com470e71d2011-07-07 08:21:25 +00003092 if (status == kRtcpOff)
3093 {
3094 _engineStatisticsPtr->SetLastError(
3095 VE_RTCP_ERROR, kTraceError,
3096 "SendApplicationDefinedRTCPPacket() RTCP is disabled");
3097 return -1;
3098 }
3099
3100 // Create and schedule the RTCP APP packet for transmission
pwestin@webrtc.org2853dde2012-05-11 11:08:54 +00003101 if (_rtpRtcpModule->SetRTCPApplicationSpecificData(
niklase@google.com470e71d2011-07-07 08:21:25 +00003102 subType,
3103 name,
3104 (const unsigned char*) data,
3105 dataLengthInBytes) != 0)
3106 {
3107 _engineStatisticsPtr->SetLastError(
3108 VE_SEND_ERROR, kTraceError,
3109 "SendApplicationDefinedRTCPPacket() failed to send RTCP packet");
3110 return -1;
3111 }
3112 return 0;
3113}
3114
3115int
3116Channel::GetRTPStatistics(
3117 unsigned int& averageJitterMs,
3118 unsigned int& maxJitterMs,
3119 unsigned int& discardedPackets)
3120{
niklase@google.com470e71d2011-07-07 08:21:25 +00003121 // The jitter statistics is updated for each received RTP packet and is
3122 // based on received packets.
sprang@webrtc.org54ae4ff2013-12-19 13:26:02 +00003123 if (_rtpRtcpModule->RTCP() == kRtcpOff) {
3124 // If RTCP is off, there is no timed thread in the RTCP module regularly
3125 // generating new stats, trigger the update manually here instead.
3126 StreamStatistician* statistician =
3127 rtp_receive_statistics_->GetStatistician(rtp_receiver_->SSRC());
3128 if (statistician) {
3129 // Don't use returned statistics, use data from proxy instead so that
3130 // max jitter can be fetched atomically.
3131 RtcpStatistics s;
3132 statistician->GetStatistics(&s, true);
3133 }
niklase@google.com470e71d2011-07-07 08:21:25 +00003134 }
3135
sprang@webrtc.org54ae4ff2013-12-19 13:26:02 +00003136 ChannelStatistics stats = statistics_proxy_->GetStats();
andrew@webrtc.orgeb524d92013-09-23 23:02:24 +00003137 const int32_t playoutFrequency = audio_coding_->PlayoutFrequency();
sprang@webrtc.org54ae4ff2013-12-19 13:26:02 +00003138 if (playoutFrequency > 0) {
3139 // Scale RTP statistics given the current playout frequency
3140 maxJitterMs = stats.max_jitter / (playoutFrequency / 1000);
3141 averageJitterMs = stats.rtcp.jitter / (playoutFrequency / 1000);
niklase@google.com470e71d2011-07-07 08:21:25 +00003142 }
3143
3144 discardedPackets = _numberOfDiscardedPackets;
3145
3146 WEBRTC_TRACE(kTraceStateInfo, kTraceVoice,
3147 VoEId(_instanceId, _channelId),
3148 "GetRTPStatistics() => averageJitterMs = %lu, maxJitterMs = %lu,"
wu@webrtc.orgfcd12b32011-09-15 20:49:50 +00003149 " discardedPackets = %lu)",
niklase@google.com470e71d2011-07-07 08:21:25 +00003150 averageJitterMs, maxJitterMs, discardedPackets);
3151 return 0;
3152}
3153
henrika@webrtc.org8a2fc882012-08-22 08:53:55 +00003154int Channel::GetRemoteRTCPReportBlocks(
3155 std::vector<ReportBlock>* report_blocks) {
3156 if (report_blocks == NULL) {
3157 _engineStatisticsPtr->SetLastError(VE_INVALID_ARGUMENT, kTraceError,
3158 "GetRemoteRTCPReportBlock()s invalid report_blocks.");
3159 return -1;
3160 }
3161
3162 // Get the report blocks from the latest received RTCP Sender or Receiver
3163 // Report. Each element in the vector contains the sender's SSRC and a
3164 // report block according to RFC 3550.
3165 std::vector<RTCPReportBlock> rtcp_report_blocks;
3166 if (_rtpRtcpModule->RemoteRTCPStat(&rtcp_report_blocks) != 0) {
3167 _engineStatisticsPtr->SetLastError(VE_RTP_RTCP_MODULE_ERROR, kTraceError,
3168 "GetRemoteRTCPReportBlocks() failed to read RTCP SR/RR report block.");
3169 return -1;
3170 }
3171
3172 if (rtcp_report_blocks.empty())
3173 return 0;
3174
3175 std::vector<RTCPReportBlock>::const_iterator it = rtcp_report_blocks.begin();
3176 for (; it != rtcp_report_blocks.end(); ++it) {
3177 ReportBlock report_block;
3178 report_block.sender_SSRC = it->remoteSSRC;
3179 report_block.source_SSRC = it->sourceSSRC;
3180 report_block.fraction_lost = it->fractionLost;
3181 report_block.cumulative_num_packets_lost = it->cumulativeLost;
3182 report_block.extended_highest_sequence_number = it->extendedHighSeqNum;
3183 report_block.interarrival_jitter = it->jitter;
3184 report_block.last_SR_timestamp = it->lastSR;
3185 report_block.delay_since_last_SR = it->delaySinceLastSR;
3186 report_blocks->push_back(report_block);
3187 }
3188 return 0;
3189}
3190
niklase@google.com470e71d2011-07-07 08:21:25 +00003191int
3192Channel::GetRTPStatistics(CallStatistics& stats)
3193{
wu@webrtc.orgcb711f72014-05-19 17:39:11 +00003194 // --- RtcpStatistics
niklase@google.com470e71d2011-07-07 08:21:25 +00003195
3196 // The jitter statistics is updated for each received RTP packet and is
3197 // based on received packets.
sprang@webrtc.org54ae4ff2013-12-19 13:26:02 +00003198 RtcpStatistics statistics;
stefan@webrtc.org286fe0b2013-08-21 20:58:21 +00003199 StreamStatistician* statistician =
3200 rtp_receive_statistics_->GetStatistician(rtp_receiver_->SSRC());
3201 if (!statistician || !statistician->GetStatistics(
wu@webrtc.org822fbd82013-08-15 23:38:54 +00003202 &statistics, _rtpRtcpModule->RTCP() == kRtcpOff)) {
3203 _engineStatisticsPtr->SetLastError(
3204 VE_CANNOT_RETRIEVE_RTP_STAT, kTraceWarning,
3205 "GetRTPStatistics() failed to read RTP statistics from the "
3206 "RTP/RTCP module");
niklase@google.com470e71d2011-07-07 08:21:25 +00003207 }
3208
wu@webrtc.org822fbd82013-08-15 23:38:54 +00003209 stats.fractionLost = statistics.fraction_lost;
3210 stats.cumulativeLost = statistics.cumulative_lost;
3211 stats.extendedMax = statistics.extended_max_sequence_number;
3212 stats.jitterSamples = statistics.jitter;
niklase@google.com470e71d2011-07-07 08:21:25 +00003213
3214 WEBRTC_TRACE(kTraceStateInfo, kTraceVoice,
3215 VoEId(_instanceId, _channelId),
3216 "GetRTPStatistics() => fractionLost=%lu, cumulativeLost=%lu,"
wu@webrtc.orgfcd12b32011-09-15 20:49:50 +00003217 " extendedMax=%lu, jitterSamples=%li)",
niklase@google.com470e71d2011-07-07 08:21:25 +00003218 stats.fractionLost, stats.cumulativeLost, stats.extendedMax,
3219 stats.jitterSamples);
3220
wu@webrtc.orgcb711f72014-05-19 17:39:11 +00003221 // --- RTT
minyue@webrtc.org2b58a442014-09-11 07:51:53 +00003222 stats.rttMs = GetRTT();
minyue@webrtc.org6fd93082014-12-15 14:56:44 +00003223 if (stats.rttMs == 0) {
3224 WEBRTC_TRACE(kTraceWarning, kTraceVoice, VoEId(_instanceId, _channelId),
3225 "GetRTPStatistics() failed to get RTT");
3226 } else {
3227 WEBRTC_TRACE(kTraceStateInfo, kTraceVoice, VoEId(_instanceId, _channelId),
pkasting@chromium.org16825b12015-01-12 21:51:21 +00003228 "GetRTPStatistics() => rttMs=%" PRId64, stats.rttMs);
minyue@webrtc.org6fd93082014-12-15 14:56:44 +00003229 }
niklase@google.com470e71d2011-07-07 08:21:25 +00003230
wu@webrtc.orgcb711f72014-05-19 17:39:11 +00003231 // --- Data counters
niklase@google.com470e71d2011-07-07 08:21:25 +00003232
pkasting@chromium.org4591fbd2014-11-20 22:28:14 +00003233 size_t bytesSent(0);
pbos@webrtc.org6141e132013-04-09 10:09:10 +00003234 uint32_t packetsSent(0);
pkasting@chromium.org4591fbd2014-11-20 22:28:14 +00003235 size_t bytesReceived(0);
pbos@webrtc.org6141e132013-04-09 10:09:10 +00003236 uint32_t packetsReceived(0);
niklase@google.com470e71d2011-07-07 08:21:25 +00003237
stefan@webrtc.org286fe0b2013-08-21 20:58:21 +00003238 if (statistician) {
3239 statistician->GetDataCounters(&bytesReceived, &packetsReceived);
3240 }
wu@webrtc.org822fbd82013-08-15 23:38:54 +00003241
pwestin@webrtc.org2853dde2012-05-11 11:08:54 +00003242 if (_rtpRtcpModule->DataCountersRTP(&bytesSent,
wu@webrtc.org822fbd82013-08-15 23:38:54 +00003243 &packetsSent) != 0)
niklase@google.com470e71d2011-07-07 08:21:25 +00003244 {
3245 WEBRTC_TRACE(kTraceWarning, kTraceVoice,
3246 VoEId(_instanceId, _channelId),
3247 "GetRTPStatistics() failed to retrieve RTP datacounters =>"
wu@webrtc.orgfcd12b32011-09-15 20:49:50 +00003248 " output will not be complete");
niklase@google.com470e71d2011-07-07 08:21:25 +00003249 }
3250
3251 stats.bytesSent = bytesSent;
3252 stats.packetsSent = packetsSent;
3253 stats.bytesReceived = bytesReceived;
3254 stats.packetsReceived = packetsReceived;
3255
3256 WEBRTC_TRACE(kTraceStateInfo, kTraceVoice,
3257 VoEId(_instanceId, _channelId),
pkasting@chromium.org4591fbd2014-11-20 22:28:14 +00003258 "GetRTPStatistics() => bytesSent=%" PRIuS ", packetsSent=%d,"
3259 " bytesReceived=%" PRIuS ", packetsReceived=%d)",
niklase@google.com470e71d2011-07-07 08:21:25 +00003260 stats.bytesSent, stats.packetsSent, stats.bytesReceived,
3261 stats.packetsReceived);
3262
wu@webrtc.orgcb711f72014-05-19 17:39:11 +00003263 // --- Timestamps
3264 {
3265 CriticalSectionScoped lock(ts_stats_lock_.get());
3266 stats.capture_start_ntp_time_ms_ = capture_start_ntp_time_ms_;
3267 }
niklase@google.com470e71d2011-07-07 08:21:25 +00003268 return 0;
3269}
3270
minyue@webrtc.orgc1a40a72014-05-28 09:52:06 +00003271int Channel::SetREDStatus(bool enable, int redPayloadtype) {
turaj@webrtc.org42259e72012-12-11 02:15:12 +00003272 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId, _channelId),
minyue@webrtc.orgc1a40a72014-05-28 09:52:06 +00003273 "Channel::SetREDStatus()");
niklase@google.com470e71d2011-07-07 08:21:25 +00003274
turaj@webrtc.org8c8ad852013-01-31 18:20:17 +00003275 if (enable) {
3276 if (redPayloadtype < 0 || redPayloadtype > 127) {
3277 _engineStatisticsPtr->SetLastError(
3278 VE_PLTYPE_ERROR, kTraceError,
minyue@webrtc.orgc1a40a72014-05-28 09:52:06 +00003279 "SetREDStatus() invalid RED payload type");
turaj@webrtc.org8c8ad852013-01-31 18:20:17 +00003280 return -1;
3281 }
3282
3283 if (SetRedPayloadType(redPayloadtype) < 0) {
3284 _engineStatisticsPtr->SetLastError(
3285 VE_CODEC_ERROR, kTraceError,
3286 "SetSecondarySendCodec() Failed to register RED ACM");
3287 return -1;
3288 }
turaj@webrtc.org42259e72012-12-11 02:15:12 +00003289 }
niklase@google.com470e71d2011-07-07 08:21:25 +00003290
minyue@webrtc.orgaa5ea1c2014-05-23 15:16:51 +00003291 if (audio_coding_->SetREDStatus(enable) != 0) {
turaj@webrtc.org42259e72012-12-11 02:15:12 +00003292 _engineStatisticsPtr->SetLastError(
3293 VE_AUDIO_CODING_MODULE_ERROR, kTraceError,
minyue@webrtc.orgaa5ea1c2014-05-23 15:16:51 +00003294 "SetREDStatus() failed to set RED state in the ACM");
turaj@webrtc.org42259e72012-12-11 02:15:12 +00003295 return -1;
3296 }
3297 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +00003298}
3299
3300int
minyue@webrtc.orgc1a40a72014-05-28 09:52:06 +00003301Channel::GetREDStatus(bool& enabled, int& redPayloadtype)
niklase@google.com470e71d2011-07-07 08:21:25 +00003302{
minyue@webrtc.orgaa5ea1c2014-05-23 15:16:51 +00003303 enabled = audio_coding_->REDStatus();
niklase@google.com470e71d2011-07-07 08:21:25 +00003304 if (enabled)
3305 {
pbos@webrtc.org6141e132013-04-09 10:09:10 +00003306 int8_t payloadType(0);
pwestin@webrtc.org2853dde2012-05-11 11:08:54 +00003307 if (_rtpRtcpModule->SendREDPayloadType(payloadType) != 0)
niklase@google.com470e71d2011-07-07 08:21:25 +00003308 {
3309 _engineStatisticsPtr->SetLastError(
3310 VE_RTP_RTCP_MODULE_ERROR, kTraceError,
minyue@webrtc.orgc1a40a72014-05-28 09:52:06 +00003311 "GetREDStatus() failed to retrieve RED PT from RTP/RTCP "
niklase@google.com470e71d2011-07-07 08:21:25 +00003312 "module");
3313 return -1;
3314 }
pkasting@chromium.orgdf9a41d2015-01-26 22:35:29 +00003315 redPayloadtype = payloadType;
niklase@google.com470e71d2011-07-07 08:21:25 +00003316 WEBRTC_TRACE(kTraceStateInfo, kTraceVoice,
3317 VoEId(_instanceId, _channelId),
minyue@webrtc.orgc1a40a72014-05-28 09:52:06 +00003318 "GetREDStatus() => enabled=%d, redPayloadtype=%d",
niklase@google.com470e71d2011-07-07 08:21:25 +00003319 enabled, redPayloadtype);
3320 return 0;
3321 }
3322 WEBRTC_TRACE(kTraceStateInfo, kTraceVoice,
3323 VoEId(_instanceId, _channelId),
minyue@webrtc.orgc1a40a72014-05-28 09:52:06 +00003324 "GetREDStatus() => enabled=%d", enabled);
niklase@google.com470e71d2011-07-07 08:21:25 +00003325 return 0;
3326}
3327
minyue@webrtc.orgc1a40a72014-05-28 09:52:06 +00003328int Channel::SetCodecFECStatus(bool enable) {
3329 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId, _channelId),
3330 "Channel::SetCodecFECStatus()");
3331
3332 if (audio_coding_->SetCodecFEC(enable) != 0) {
3333 _engineStatisticsPtr->SetLastError(
3334 VE_AUDIO_CODING_MODULE_ERROR, kTraceError,
3335 "SetCodecFECStatus() failed to set FEC state");
3336 return -1;
3337 }
3338 return 0;
3339}
3340
3341bool Channel::GetCodecFECStatus() {
3342 bool enabled = audio_coding_->CodecFEC();
3343 WEBRTC_TRACE(kTraceStateInfo, kTraceVoice,
3344 VoEId(_instanceId, _channelId),
3345 "GetCodecFECStatus() => enabled=%d", enabled);
3346 return enabled;
3347}
3348
pwestin@webrtc.orgdb249952013-06-05 15:33:20 +00003349void Channel::SetNACKStatus(bool enable, int maxNumberOfPackets) {
3350 // None of these functions can fail.
3351 _rtpRtcpModule->SetStorePacketsStatus(enable, maxNumberOfPackets);
stefan@webrtc.org7bb8f022013-09-06 13:40:11 +00003352 rtp_receive_statistics_->SetMaxReorderingThreshold(maxNumberOfPackets);
3353 rtp_receiver_->SetNACKStatus(enable ? kNackRtcp : kNackOff);
pwestin@webrtc.orgd30859e2013-06-06 21:09:01 +00003354 if (enable)
andrew@webrtc.orgeb524d92013-09-23 23:02:24 +00003355 audio_coding_->EnableNack(maxNumberOfPackets);
pwestin@webrtc.orgd30859e2013-06-06 21:09:01 +00003356 else
andrew@webrtc.orgeb524d92013-09-23 23:02:24 +00003357 audio_coding_->DisableNack();
pwestin@webrtc.orgdb249952013-06-05 15:33:20 +00003358}
3359
pwestin@webrtc.orgd30859e2013-06-06 21:09:01 +00003360// Called when we are missing one or more packets.
3361int Channel::ResendPackets(const uint16_t* sequence_numbers, int length) {
pwestin@webrtc.orgdb249952013-06-05 15:33:20 +00003362 return _rtpRtcpModule->SendNACK(sequence_numbers, length);
3363}
3364
niklase@google.com470e71d2011-07-07 08:21:25 +00003365int
niklase@google.com470e71d2011-07-07 08:21:25 +00003366Channel::StartRTPDump(const char fileNameUTF8[1024],
3367 RTPDirections direction)
3368{
3369 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId, _channelId),
3370 "Channel::StartRTPDump()");
3371 if ((direction != kRtpIncoming) && (direction != kRtpOutgoing))
3372 {
3373 _engineStatisticsPtr->SetLastError(
3374 VE_INVALID_ARGUMENT, kTraceError,
3375 "StartRTPDump() invalid RTP direction");
3376 return -1;
3377 }
3378 RtpDump* rtpDumpPtr = (direction == kRtpIncoming) ?
3379 &_rtpDumpIn : &_rtpDumpOut;
3380 if (rtpDumpPtr == NULL)
3381 {
3382 assert(false);
3383 return -1;
3384 }
3385 if (rtpDumpPtr->IsActive())
3386 {
3387 rtpDumpPtr->Stop();
3388 }
3389 if (rtpDumpPtr->Start(fileNameUTF8) != 0)
3390 {
3391 _engineStatisticsPtr->SetLastError(
3392 VE_BAD_FILE, kTraceError,
3393 "StartRTPDump() failed to create file");
3394 return -1;
3395 }
3396 return 0;
3397}
3398
3399int
3400Channel::StopRTPDump(RTPDirections direction)
3401{
3402 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId, _channelId),
3403 "Channel::StopRTPDump()");
3404 if ((direction != kRtpIncoming) && (direction != kRtpOutgoing))
3405 {
3406 _engineStatisticsPtr->SetLastError(
3407 VE_INVALID_ARGUMENT, kTraceError,
3408 "StopRTPDump() invalid RTP direction");
3409 return -1;
3410 }
3411 RtpDump* rtpDumpPtr = (direction == kRtpIncoming) ?
3412 &_rtpDumpIn : &_rtpDumpOut;
3413 if (rtpDumpPtr == NULL)
3414 {
3415 assert(false);
3416 return -1;
3417 }
3418 if (!rtpDumpPtr->IsActive())
3419 {
3420 return 0;
3421 }
3422 return rtpDumpPtr->Stop();
3423}
3424
3425bool
3426Channel::RTPDumpIsActive(RTPDirections direction)
3427{
3428 if ((direction != kRtpIncoming) &&
3429 (direction != kRtpOutgoing))
3430 {
3431 _engineStatisticsPtr->SetLastError(
3432 VE_INVALID_ARGUMENT, kTraceError,
3433 "RTPDumpIsActive() invalid RTP direction");
3434 return false;
3435 }
3436 RtpDump* rtpDumpPtr = (direction == kRtpIncoming) ?
3437 &_rtpDumpIn : &_rtpDumpOut;
3438 return rtpDumpPtr->IsActive();
3439}
3440
solenberg@webrtc.orgb1f50102014-03-24 10:38:25 +00003441void Channel::SetVideoEngineBWETarget(ViENetwork* vie_network,
3442 int video_channel) {
3443 CriticalSectionScoped cs(&_callbackCritSect);
3444 if (vie_network_) {
3445 vie_network_->Release();
3446 vie_network_ = NULL;
3447 }
3448 video_channel_ = -1;
3449
3450 if (vie_network != NULL && video_channel != -1) {
3451 vie_network_ = vie_network;
3452 video_channel_ = video_channel;
3453 }
3454}
3455
pbos@webrtc.org6141e132013-04-09 10:09:10 +00003456uint32_t
andrew@webrtc.org755b04a2011-11-15 16:57:56 +00003457Channel::Demultiplex(const AudioFrame& audioFrame)
niklase@google.com470e71d2011-07-07 08:21:25 +00003458{
3459 WEBRTC_TRACE(kTraceStream, kTraceVoice, VoEId(_instanceId,_channelId),
andrew@webrtc.org755b04a2011-11-15 16:57:56 +00003460 "Channel::Demultiplex()");
andrew@webrtc.orgae1a58b2013-01-22 04:44:30 +00003461 _audioFrame.CopyFrom(audioFrame);
andrew@webrtc.org63a50982012-05-02 23:56:37 +00003462 _audioFrame.id_ = _channelId;
niklase@google.com470e71d2011-07-07 08:21:25 +00003463 return 0;
3464}
3465
xians@webrtc.org2f84afa2013-07-31 16:23:37 +00003466void Channel::Demultiplex(const int16_t* audio_data,
xians@webrtc.org8fff1f02013-07-31 16:27:42 +00003467 int sample_rate,
xians@webrtc.org2f84afa2013-07-31 16:23:37 +00003468 int number_of_frames,
xians@webrtc.org8fff1f02013-07-31 16:27:42 +00003469 int number_of_channels) {
xians@webrtc.org2f84afa2013-07-31 16:23:37 +00003470 CodecInst codec;
3471 GetSendCodec(codec);
xians@webrtc.org2f84afa2013-07-31 16:23:37 +00003472
andrew@webrtc.org40ee3d02014-04-03 21:56:01 +00003473 if (!mono_recording_audio_.get()) {
3474 // Temporary space for DownConvertToCodecFormat.
3475 mono_recording_audio_.reset(new int16_t[kMaxMonoDataSizeSamples]);
xians@webrtc.org2f84afa2013-07-31 16:23:37 +00003476 }
andrew@webrtc.org40ee3d02014-04-03 21:56:01 +00003477 DownConvertToCodecFormat(audio_data,
3478 number_of_frames,
3479 number_of_channels,
3480 sample_rate,
3481 codec.channels,
3482 codec.plfreq,
3483 mono_recording_audio_.get(),
3484 &input_resampler_,
3485 &_audioFrame);
xians@webrtc.org2f84afa2013-07-31 16:23:37 +00003486}
3487
pbos@webrtc.org6141e132013-04-09 10:09:10 +00003488uint32_t
xians@google.com0b0665a2011-08-08 08:18:44 +00003489Channel::PrepareEncodeAndSend(int mixingFrequency)
niklase@google.com470e71d2011-07-07 08:21:25 +00003490{
3491 WEBRTC_TRACE(kTraceStream, kTraceVoice, VoEId(_instanceId,_channelId),
3492 "Channel::PrepareEncodeAndSend()");
3493
andrew@webrtc.org63a50982012-05-02 23:56:37 +00003494 if (_audioFrame.samples_per_channel_ == 0)
niklase@google.com470e71d2011-07-07 08:21:25 +00003495 {
3496 WEBRTC_TRACE(kTraceWarning, kTraceVoice, VoEId(_instanceId,_channelId),
3497 "Channel::PrepareEncodeAndSend() invalid audio frame");
tommi@webrtc.orgeec6ecd2014-07-11 19:09:59 +00003498 return 0xFFFFFFFF;
niklase@google.com470e71d2011-07-07 08:21:25 +00003499 }
3500
henrika@webrtc.org944cbeb2014-03-18 10:32:33 +00003501 if (channel_state_.Get().input_file_playing)
niklase@google.com470e71d2011-07-07 08:21:25 +00003502 {
3503 MixOrReplaceAudioWithFile(mixingFrequency);
3504 }
3505
andrew@webrtc.org21299d42014-05-14 19:00:59 +00003506 bool is_muted = Mute(); // Cache locally as Mute() takes a lock.
3507 if (is_muted) {
3508 AudioFrameOperations::Mute(_audioFrame);
niklase@google.com470e71d2011-07-07 08:21:25 +00003509 }
3510
henrika@webrtc.org944cbeb2014-03-18 10:32:33 +00003511 if (channel_state_.Get().input_external_media)
niklase@google.com470e71d2011-07-07 08:21:25 +00003512 {
mflodman@webrtc.org9a065d12012-03-07 08:12:21 +00003513 CriticalSectionScoped cs(&_callbackCritSect);
andrew@webrtc.org63a50982012-05-02 23:56:37 +00003514 const bool isStereo = (_audioFrame.num_channels_ == 2);
niklase@google.com470e71d2011-07-07 08:21:25 +00003515 if (_inputExternalMediaCallbackPtr)
3516 {
3517 _inputExternalMediaCallbackPtr->Process(
3518 _channelId,
3519 kRecordingPerChannel,
pbos@webrtc.org6141e132013-04-09 10:09:10 +00003520 (int16_t*)_audioFrame.data_,
andrew@webrtc.org63a50982012-05-02 23:56:37 +00003521 _audioFrame.samples_per_channel_,
3522 _audioFrame.sample_rate_hz_,
niklase@google.com470e71d2011-07-07 08:21:25 +00003523 isStereo);
3524 }
3525 }
3526
3527 InsertInbandDtmfTone();
3528
andrew@webrtc.org60730cf2014-01-07 17:45:09 +00003529 if (_includeAudioLevelIndication) {
andrew@webrtc.org382c0c22014-05-05 18:22:21 +00003530 int length = _audioFrame.samples_per_channel_ * _audioFrame.num_channels_;
andrew@webrtc.org21299d42014-05-14 19:00:59 +00003531 if (is_muted) {
3532 rms_level_.ProcessMuted(length);
3533 } else {
3534 rms_level_.Process(_audioFrame.data_, length);
3535 }
andrew@webrtc.org755b04a2011-11-15 16:57:56 +00003536 }
3537
niklase@google.com470e71d2011-07-07 08:21:25 +00003538 return 0;
3539}
3540
pbos@webrtc.org6141e132013-04-09 10:09:10 +00003541uint32_t
niklase@google.com470e71d2011-07-07 08:21:25 +00003542Channel::EncodeAndSend()
3543{
3544 WEBRTC_TRACE(kTraceStream, kTraceVoice, VoEId(_instanceId,_channelId),
3545 "Channel::EncodeAndSend()");
3546
andrew@webrtc.org63a50982012-05-02 23:56:37 +00003547 assert(_audioFrame.num_channels_ <= 2);
3548 if (_audioFrame.samples_per_channel_ == 0)
niklase@google.com470e71d2011-07-07 08:21:25 +00003549 {
3550 WEBRTC_TRACE(kTraceWarning, kTraceVoice, VoEId(_instanceId,_channelId),
3551 "Channel::EncodeAndSend() invalid audio frame");
tommi@webrtc.orgeec6ecd2014-07-11 19:09:59 +00003552 return 0xFFFFFFFF;
niklase@google.com470e71d2011-07-07 08:21:25 +00003553 }
3554
andrew@webrtc.org63a50982012-05-02 23:56:37 +00003555 _audioFrame.id_ = _channelId;
niklase@google.com470e71d2011-07-07 08:21:25 +00003556
3557 // --- Add 10ms of raw (PCM) audio data to the encoder @ 32kHz.
3558
3559 // The ACM resamples internally.
andrew@webrtc.org63a50982012-05-02 23:56:37 +00003560 _audioFrame.timestamp_ = _timeStamp;
henrik.lundin@webrtc.orgf56c1622015-03-02 12:29:30 +00003561 // This call will trigger AudioPacketizationCallback::SendData if encoding
3562 // is done and payload is ready for packetization and transmission.
3563 // Otherwise, it will return without invoking the callback.
3564 if (audio_coding_->Add10MsData((AudioFrame&)_audioFrame) < 0)
niklase@google.com470e71d2011-07-07 08:21:25 +00003565 {
3566 WEBRTC_TRACE(kTraceError, kTraceVoice, VoEId(_instanceId,_channelId),
3567 "Channel::EncodeAndSend() ACM encoding failed");
tommi@webrtc.orgeec6ecd2014-07-11 19:09:59 +00003568 return 0xFFFFFFFF;
niklase@google.com470e71d2011-07-07 08:21:25 +00003569 }
3570
andrew@webrtc.org63a50982012-05-02 23:56:37 +00003571 _timeStamp += _audioFrame.samples_per_channel_;
henrik.lundin@webrtc.orgf56c1622015-03-02 12:29:30 +00003572 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +00003573}
3574
3575int Channel::RegisterExternalMediaProcessing(
3576 ProcessingTypes type,
3577 VoEMediaProcess& processObject)
3578{
3579 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId,_channelId),
3580 "Channel::RegisterExternalMediaProcessing()");
3581
mflodman@webrtc.org9a065d12012-03-07 08:12:21 +00003582 CriticalSectionScoped cs(&_callbackCritSect);
niklase@google.com470e71d2011-07-07 08:21:25 +00003583
3584 if (kPlaybackPerChannel == type)
3585 {
3586 if (_outputExternalMediaCallbackPtr)
3587 {
3588 _engineStatisticsPtr->SetLastError(
3589 VE_INVALID_OPERATION, kTraceError,
3590 "Channel::RegisterExternalMediaProcessing() "
3591 "output external media already enabled");
3592 return -1;
3593 }
3594 _outputExternalMediaCallbackPtr = &processObject;
3595 _outputExternalMedia = true;
3596 }
3597 else if (kRecordingPerChannel == type)
3598 {
3599 if (_inputExternalMediaCallbackPtr)
3600 {
3601 _engineStatisticsPtr->SetLastError(
3602 VE_INVALID_OPERATION, kTraceError,
3603 "Channel::RegisterExternalMediaProcessing() "
3604 "output external media already enabled");
3605 return -1;
3606 }
3607 _inputExternalMediaCallbackPtr = &processObject;
henrika@webrtc.org944cbeb2014-03-18 10:32:33 +00003608 channel_state_.SetInputExternalMedia(true);
niklase@google.com470e71d2011-07-07 08:21:25 +00003609 }
3610 return 0;
3611}
3612
3613int Channel::DeRegisterExternalMediaProcessing(ProcessingTypes type)
3614{
3615 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId,_channelId),
3616 "Channel::DeRegisterExternalMediaProcessing()");
3617
mflodman@webrtc.org9a065d12012-03-07 08:12:21 +00003618 CriticalSectionScoped cs(&_callbackCritSect);
niklase@google.com470e71d2011-07-07 08:21:25 +00003619
3620 if (kPlaybackPerChannel == type)
3621 {
3622 if (!_outputExternalMediaCallbackPtr)
3623 {
3624 _engineStatisticsPtr->SetLastError(
3625 VE_INVALID_OPERATION, kTraceWarning,
3626 "Channel::DeRegisterExternalMediaProcessing() "
3627 "output external media already disabled");
3628 return 0;
3629 }
3630 _outputExternalMedia = false;
3631 _outputExternalMediaCallbackPtr = NULL;
3632 }
3633 else if (kRecordingPerChannel == type)
3634 {
3635 if (!_inputExternalMediaCallbackPtr)
3636 {
3637 _engineStatisticsPtr->SetLastError(
3638 VE_INVALID_OPERATION, kTraceWarning,
3639 "Channel::DeRegisterExternalMediaProcessing() "
3640 "input external media already disabled");
3641 return 0;
3642 }
henrika@webrtc.org944cbeb2014-03-18 10:32:33 +00003643 channel_state_.SetInputExternalMedia(false);
niklase@google.com470e71d2011-07-07 08:21:25 +00003644 _inputExternalMediaCallbackPtr = NULL;
3645 }
3646
3647 return 0;
3648}
3649
roosa@google.com1b60ceb2012-12-12 23:00:29 +00003650int Channel::SetExternalMixing(bool enabled) {
3651 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId,_channelId),
3652 "Channel::SetExternalMixing(enabled=%d)", enabled);
3653
henrika@webrtc.org944cbeb2014-03-18 10:32:33 +00003654 if (channel_state_.Get().playing)
roosa@google.com1b60ceb2012-12-12 23:00:29 +00003655 {
3656 _engineStatisticsPtr->SetLastError(
3657 VE_INVALID_OPERATION, kTraceError,
3658 "Channel::SetExternalMixing() "
3659 "external mixing cannot be changed while playing.");
3660 return -1;
3661 }
3662
3663 _externalMixing = enabled;
3664
3665 return 0;
3666}
3667
niklase@google.com470e71d2011-07-07 08:21:25 +00003668int
niklase@google.com470e71d2011-07-07 08:21:25 +00003669Channel::GetNetworkStatistics(NetworkStatistics& stats)
3670{
3671 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId,_channelId),
3672 "Channel::GetNetworkStatistics()");
minyue@webrtc.orgc0bd7be2015-02-18 15:24:13 +00003673 return audio_coding_->GetNetworkStatistics(&stats);
niklase@google.com470e71d2011-07-07 08:21:25 +00003674}
3675
wu@webrtc.org24301a62013-12-13 19:17:43 +00003676void Channel::GetDecodingCallStatistics(AudioDecodingCallStats* stats) const {
3677 audio_coding_->GetDecodingCallStatistics(stats);
3678}
3679
pwestin@webrtc.org1de01352013-04-11 20:23:35 +00003680bool Channel::GetDelayEstimate(int* jitter_buffer_delay_ms,
3681 int* playout_buffer_delay_ms) const {
3682 if (_average_jitter_buffer_delay_us == 0) {
niklase@google.com470e71d2011-07-07 08:21:25 +00003683 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId,_channelId),
pwestin@webrtc.org1de01352013-04-11 20:23:35 +00003684 "Channel::GetDelayEstimate() no valid estimate.");
3685 return false;
3686 }
3687 *jitter_buffer_delay_ms = (_average_jitter_buffer_delay_us + 500) / 1000 +
3688 _recPacketDelayMs;
3689 *playout_buffer_delay_ms = playout_delay_ms_;
3690 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId,_channelId),
3691 "Channel::GetDelayEstimate()");
3692 return true;
niklase@google.com470e71d2011-07-07 08:21:25 +00003693}
3694
turaj@webrtc.org6388c3e2013-02-12 21:42:18 +00003695int Channel::SetInitialPlayoutDelay(int delay_ms)
3696{
3697 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId,_channelId),
3698 "Channel::SetInitialPlayoutDelay()");
3699 if ((delay_ms < kVoiceEngineMinMinPlayoutDelayMs) ||
3700 (delay_ms > kVoiceEngineMaxMinPlayoutDelayMs))
3701 {
3702 _engineStatisticsPtr->SetLastError(
3703 VE_INVALID_ARGUMENT, kTraceError,
3704 "SetInitialPlayoutDelay() invalid min delay");
3705 return -1;
3706 }
andrew@webrtc.orgeb524d92013-09-23 23:02:24 +00003707 if (audio_coding_->SetInitialPlayoutDelay(delay_ms) != 0)
turaj@webrtc.org6388c3e2013-02-12 21:42:18 +00003708 {
3709 _engineStatisticsPtr->SetLastError(
3710 VE_AUDIO_CODING_MODULE_ERROR, kTraceError,
3711 "SetInitialPlayoutDelay() failed to set min playout delay");
3712 return -1;
3713 }
3714 return 0;
3715}
3716
3717
niklase@google.com470e71d2011-07-07 08:21:25 +00003718int
3719Channel::SetMinimumPlayoutDelay(int delayMs)
3720{
3721 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId,_channelId),
3722 "Channel::SetMinimumPlayoutDelay()");
3723 if ((delayMs < kVoiceEngineMinMinPlayoutDelayMs) ||
3724 (delayMs > kVoiceEngineMaxMinPlayoutDelayMs))
3725 {
3726 _engineStatisticsPtr->SetLastError(
3727 VE_INVALID_ARGUMENT, kTraceError,
3728 "SetMinimumPlayoutDelay() invalid min delay");
3729 return -1;
3730 }
andrew@webrtc.orgeb524d92013-09-23 23:02:24 +00003731 if (audio_coding_->SetMinimumPlayoutDelay(delayMs) != 0)
niklase@google.com470e71d2011-07-07 08:21:25 +00003732 {
3733 _engineStatisticsPtr->SetLastError(
3734 VE_AUDIO_CODING_MODULE_ERROR, kTraceError,
3735 "SetMinimumPlayoutDelay() failed to set min playout delay");
3736 return -1;
3737 }
3738 return 0;
3739}
3740
pwestin@webrtc.org1de01352013-04-11 20:23:35 +00003741void Channel::UpdatePlayoutTimestamp(bool rtcp) {
3742 uint32_t playout_timestamp = 0;
3743
andrew@webrtc.orgeb524d92013-09-23 23:02:24 +00003744 if (audio_coding_->PlayoutTimestamp(&playout_timestamp) == -1) {
turaj@webrtc.org1ebd2e92014-07-25 17:50:10 +00003745 // This can happen if this channel has not been received any RTP packet. In
3746 // this case, NetEq is not capable of computing playout timestamp.
pwestin@webrtc.org1de01352013-04-11 20:23:35 +00003747 return;
3748 }
3749
3750 uint16_t delay_ms = 0;
3751 if (_audioDeviceModulePtr->PlayoutDelay(&delay_ms) == -1) {
3752 WEBRTC_TRACE(kTraceWarning, kTraceVoice, VoEId(_instanceId,_channelId),
3753 "Channel::UpdatePlayoutTimestamp() failed to read playout"
3754 " delay from the ADM");
3755 _engineStatisticsPtr->SetLastError(
3756 VE_CANNOT_RETRIEVE_VALUE, kTraceError,
3757 "UpdatePlayoutTimestamp() failed to retrieve playout delay");
3758 return;
3759 }
3760
turaj@webrtc.org167b6df2013-12-13 21:05:07 +00003761 jitter_buffer_playout_timestamp_ = playout_timestamp;
3762
pwestin@webrtc.org1de01352013-04-11 20:23:35 +00003763 // Remove the playout delay.
wu@webrtc.org94454b72014-06-05 20:34:08 +00003764 playout_timestamp -= (delay_ms * (GetPlayoutFrequency() / 1000));
pwestin@webrtc.org1de01352013-04-11 20:23:35 +00003765
3766 WEBRTC_TRACE(kTraceStream, kTraceVoice, VoEId(_instanceId,_channelId),
3767 "Channel::UpdatePlayoutTimestamp() => playoutTimestamp = %lu",
3768 playout_timestamp);
3769
3770 if (rtcp) {
3771 playout_timestamp_rtcp_ = playout_timestamp;
3772 } else {
3773 playout_timestamp_rtp_ = playout_timestamp;
3774 }
3775 playout_delay_ms_ = delay_ms;
3776}
3777
3778int Channel::GetPlayoutTimestamp(unsigned int& timestamp) {
3779 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId,_channelId),
3780 "Channel::GetPlayoutTimestamp()");
3781 if (playout_timestamp_rtp_ == 0) {
3782 _engineStatisticsPtr->SetLastError(
3783 VE_CANNOT_RETRIEVE_VALUE, kTraceError,
3784 "GetPlayoutTimestamp() failed to retrieve timestamp");
3785 return -1;
3786 }
3787 timestamp = playout_timestamp_rtp_;
3788 WEBRTC_TRACE(kTraceStateInfo, kTraceVoice,
3789 VoEId(_instanceId,_channelId),
3790 "GetPlayoutTimestamp() => timestamp=%u", timestamp);
3791 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +00003792}
3793
pbos@webrtc.orgd16e8392014-12-19 13:49:55 +00003794int Channel::SetInitTimestamp(unsigned int timestamp) {
3795 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId, _channelId),
niklase@google.com470e71d2011-07-07 08:21:25 +00003796 "Channel::SetInitTimestamp()");
pbos@webrtc.orgd16e8392014-12-19 13:49:55 +00003797 if (channel_state_.Get().sending) {
3798 _engineStatisticsPtr->SetLastError(VE_SENDING, kTraceError,
3799 "SetInitTimestamp() already sending");
3800 return -1;
3801 }
3802 _rtpRtcpModule->SetStartTimestamp(timestamp);
3803 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +00003804}
3805
pbos@webrtc.orgd16e8392014-12-19 13:49:55 +00003806int Channel::SetInitSequenceNumber(short sequenceNumber) {
3807 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId, _channelId),
3808 "Channel::SetInitSequenceNumber()");
3809 if (channel_state_.Get().sending) {
3810 _engineStatisticsPtr->SetLastError(
3811 VE_SENDING, kTraceError, "SetInitSequenceNumber() already sending");
3812 return -1;
3813 }
3814 _rtpRtcpModule->SetSequenceNumber(sequenceNumber);
3815 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +00003816}
3817
3818int
wu@webrtc.org822fbd82013-08-15 23:38:54 +00003819Channel::GetRtpRtcp(RtpRtcp** rtpRtcpModule, RtpReceiver** rtp_receiver) const
niklase@google.com470e71d2011-07-07 08:21:25 +00003820{
3821 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId,_channelId),
3822 "Channel::GetRtpRtcp()");
wu@webrtc.org822fbd82013-08-15 23:38:54 +00003823 *rtpRtcpModule = _rtpRtcpModule.get();
3824 *rtp_receiver = rtp_receiver_.get();
niklase@google.com470e71d2011-07-07 08:21:25 +00003825 return 0;
3826}
3827
andrew@webrtc.orge59a0ac2012-05-08 17:12:40 +00003828// TODO(andrew): refactor Mix functions here and in transmit_mixer.cc to use
3829// a shared helper.
pbos@webrtc.org6141e132013-04-09 10:09:10 +00003830int32_t
pbos@webrtc.org92135212013-05-14 08:31:39 +00003831Channel::MixOrReplaceAudioWithFile(int mixingFrequency)
niklase@google.com470e71d2011-07-07 08:21:25 +00003832{
kwiberg@webrtc.org00b8f6b2015-02-26 14:34:55 +00003833 rtc::scoped_ptr<int16_t[]> fileBuffer(new int16_t[640]);
andrew@webrtc.orge59a0ac2012-05-08 17:12:40 +00003834 int fileSamples(0);
niklase@google.com470e71d2011-07-07 08:21:25 +00003835
3836 {
mflodman@webrtc.org9a065d12012-03-07 08:12:21 +00003837 CriticalSectionScoped cs(&_fileCritSect);
niklase@google.com470e71d2011-07-07 08:21:25 +00003838
3839 if (_inputFilePlayerPtr == NULL)
3840 {
3841 WEBRTC_TRACE(kTraceWarning, kTraceVoice,
3842 VoEId(_instanceId, _channelId),
3843 "Channel::MixOrReplaceAudioWithFile() fileplayer"
3844 " doesnt exist");
3845 return -1;
3846 }
3847
braveyao@webrtc.orgd7131432012-03-29 10:39:44 +00003848 if (_inputFilePlayerPtr->Get10msAudioFromFile(fileBuffer.get(),
niklase@google.com470e71d2011-07-07 08:21:25 +00003849 fileSamples,
3850 mixingFrequency) == -1)
3851 {
3852 WEBRTC_TRACE(kTraceWarning, kTraceVoice,
3853 VoEId(_instanceId, _channelId),
3854 "Channel::MixOrReplaceAudioWithFile() file mixing "
3855 "failed");
3856 return -1;
3857 }
3858 if (fileSamples == 0)
3859 {
3860 WEBRTC_TRACE(kTraceWarning, kTraceVoice,
3861 VoEId(_instanceId, _channelId),
3862 "Channel::MixOrReplaceAudioWithFile() file is ended");
3863 return 0;
3864 }
3865 }
3866
andrew@webrtc.org63a50982012-05-02 23:56:37 +00003867 assert(_audioFrame.samples_per_channel_ == fileSamples);
niklase@google.com470e71d2011-07-07 08:21:25 +00003868
3869 if (_mixFileWithMicrophone)
3870 {
braveyao@webrtc.orgd7131432012-03-29 10:39:44 +00003871 // Currently file stream is always mono.
3872 // TODO(xians): Change the code when FilePlayer supports real stereo.
andrew@webrtc.org40ee3d02014-04-03 21:56:01 +00003873 MixWithSat(_audioFrame.data_,
3874 _audioFrame.num_channels_,
3875 fileBuffer.get(),
3876 1,
3877 fileSamples);
niklase@google.com470e71d2011-07-07 08:21:25 +00003878 }
3879 else
3880 {
braveyao@webrtc.orgd7131432012-03-29 10:39:44 +00003881 // Replace ACM audio with file.
3882 // Currently file stream is always mono.
3883 // TODO(xians): Change the code when FilePlayer supports real stereo.
niklase@google.com470e71d2011-07-07 08:21:25 +00003884 _audioFrame.UpdateFrame(_channelId,
tommi@webrtc.orgeec6ecd2014-07-11 19:09:59 +00003885 0xFFFFFFFF,
braveyao@webrtc.orgd7131432012-03-29 10:39:44 +00003886 fileBuffer.get(),
andrew@webrtc.orge59a0ac2012-05-08 17:12:40 +00003887 fileSamples,
niklase@google.com470e71d2011-07-07 08:21:25 +00003888 mixingFrequency,
3889 AudioFrame::kNormalSpeech,
3890 AudioFrame::kVadUnknown,
3891 1);
3892
3893 }
3894 return 0;
3895}
3896
pbos@webrtc.org6141e132013-04-09 10:09:10 +00003897int32_t
niklase@google.com470e71d2011-07-07 08:21:25 +00003898Channel::MixAudioWithFile(AudioFrame& audioFrame,
pbos@webrtc.org92135212013-05-14 08:31:39 +00003899 int mixingFrequency)
niklase@google.com470e71d2011-07-07 08:21:25 +00003900{
minyue@webrtc.org2a8df7c2014-08-06 10:05:19 +00003901 assert(mixingFrequency <= 48000);
niklase@google.com470e71d2011-07-07 08:21:25 +00003902
kwiberg@webrtc.org00b8f6b2015-02-26 14:34:55 +00003903 rtc::scoped_ptr<int16_t[]> fileBuffer(new int16_t[960]);
andrew@webrtc.orge59a0ac2012-05-08 17:12:40 +00003904 int fileSamples(0);
niklase@google.com470e71d2011-07-07 08:21:25 +00003905
3906 {
mflodman@webrtc.org9a065d12012-03-07 08:12:21 +00003907 CriticalSectionScoped cs(&_fileCritSect);
niklase@google.com470e71d2011-07-07 08:21:25 +00003908
3909 if (_outputFilePlayerPtr == NULL)
3910 {
3911 WEBRTC_TRACE(kTraceWarning, kTraceVoice,
3912 VoEId(_instanceId, _channelId),
3913 "Channel::MixAudioWithFile() file mixing failed");
3914 return -1;
3915 }
3916
3917 // We should get the frequency we ask for.
braveyao@webrtc.orgd7131432012-03-29 10:39:44 +00003918 if (_outputFilePlayerPtr->Get10msAudioFromFile(fileBuffer.get(),
niklase@google.com470e71d2011-07-07 08:21:25 +00003919 fileSamples,
3920 mixingFrequency) == -1)
3921 {
3922 WEBRTC_TRACE(kTraceWarning, kTraceVoice,
3923 VoEId(_instanceId, _channelId),
3924 "Channel::MixAudioWithFile() file mixing failed");
3925 return -1;
3926 }
3927 }
3928
andrew@webrtc.org63a50982012-05-02 23:56:37 +00003929 if (audioFrame.samples_per_channel_ == fileSamples)
niklase@google.com470e71d2011-07-07 08:21:25 +00003930 {
braveyao@webrtc.orgd7131432012-03-29 10:39:44 +00003931 // Currently file stream is always mono.
3932 // TODO(xians): Change the code when FilePlayer supports real stereo.
andrew@webrtc.org40ee3d02014-04-03 21:56:01 +00003933 MixWithSat(audioFrame.data_,
3934 audioFrame.num_channels_,
3935 fileBuffer.get(),
3936 1,
3937 fileSamples);
niklase@google.com470e71d2011-07-07 08:21:25 +00003938 }
3939 else
3940 {
3941 WEBRTC_TRACE(kTraceWarning, kTraceVoice, VoEId(_instanceId,_channelId),
andrew@webrtc.org63a50982012-05-02 23:56:37 +00003942 "Channel::MixAudioWithFile() samples_per_channel_(%d) != "
niklase@google.com470e71d2011-07-07 08:21:25 +00003943 "fileSamples(%d)",
andrew@webrtc.org63a50982012-05-02 23:56:37 +00003944 audioFrame.samples_per_channel_, fileSamples);
niklase@google.com470e71d2011-07-07 08:21:25 +00003945 return -1;
3946 }
3947
3948 return 0;
3949}
3950
3951int
3952Channel::InsertInbandDtmfTone()
3953{
niklas.enbom@webrtc.orgaf26f642011-11-16 12:41:36 +00003954 // Check if we should start a new tone.
niklase@google.com470e71d2011-07-07 08:21:25 +00003955 if (_inbandDtmfQueue.PendingDtmf() &&
3956 !_inbandDtmfGenerator.IsAddingTone() &&
3957 _inbandDtmfGenerator.DelaySinceLastTone() >
3958 kMinTelephoneEventSeparationMs)
3959 {
pbos@webrtc.org6141e132013-04-09 10:09:10 +00003960 int8_t eventCode(0);
3961 uint16_t lengthMs(0);
3962 uint8_t attenuationDb(0);
niklase@google.com470e71d2011-07-07 08:21:25 +00003963
3964 eventCode = _inbandDtmfQueue.NextDtmf(&lengthMs, &attenuationDb);
3965 _inbandDtmfGenerator.AddTone(eventCode, lengthMs, attenuationDb);
3966 if (_playInbandDtmfEvent)
3967 {
3968 // Add tone to output mixer using a reduced length to minimize
3969 // risk of echo.
3970 _outputMixerPtr->PlayDtmfTone(eventCode, lengthMs - 80,
3971 attenuationDb);
3972 }
3973 }
3974
3975 if (_inbandDtmfGenerator.IsAddingTone())
3976 {
pbos@webrtc.org6141e132013-04-09 10:09:10 +00003977 uint16_t frequency(0);
niklase@google.com470e71d2011-07-07 08:21:25 +00003978 _inbandDtmfGenerator.GetSampleRate(frequency);
3979
andrew@webrtc.org63a50982012-05-02 23:56:37 +00003980 if (frequency != _audioFrame.sample_rate_hz_)
niklase@google.com470e71d2011-07-07 08:21:25 +00003981 {
3982 // Update sample rate of Dtmf tone since the mixing frequency
3983 // has changed.
3984 _inbandDtmfGenerator.SetSampleRate(
pbos@webrtc.org6141e132013-04-09 10:09:10 +00003985 (uint16_t) (_audioFrame.sample_rate_hz_));
niklase@google.com470e71d2011-07-07 08:21:25 +00003986 // Reset the tone to be added taking the new sample rate into
3987 // account.
3988 _inbandDtmfGenerator.ResetTone();
3989 }
andrew@webrtc.orgae1a58b2013-01-22 04:44:30 +00003990
pbos@webrtc.org6141e132013-04-09 10:09:10 +00003991 int16_t toneBuffer[320];
3992 uint16_t toneSamples(0);
niklase@google.com470e71d2011-07-07 08:21:25 +00003993 // Get 10ms tone segment and set time since last tone to zero
3994 if (_inbandDtmfGenerator.Get10msTone(toneBuffer, toneSamples) == -1)
3995 {
3996 WEBRTC_TRACE(kTraceWarning, kTraceVoice,
3997 VoEId(_instanceId, _channelId),
3998 "Channel::EncodeAndSend() inserting Dtmf failed");
3999 return -1;
4000 }
4001
niklas.enbom@webrtc.orgaf26f642011-11-16 12:41:36 +00004002 // Replace mixed audio with DTMF tone.
andrew@webrtc.orgae1a58b2013-01-22 04:44:30 +00004003 for (int sample = 0;
andrew@webrtc.org63a50982012-05-02 23:56:37 +00004004 sample < _audioFrame.samples_per_channel_;
niklas.enbom@webrtc.orgaf26f642011-11-16 12:41:36 +00004005 sample++)
4006 {
andrew@webrtc.orgae1a58b2013-01-22 04:44:30 +00004007 for (int channel = 0;
4008 channel < _audioFrame.num_channels_;
niklas.enbom@webrtc.orgaf26f642011-11-16 12:41:36 +00004009 channel++)
4010 {
andrew@webrtc.orgae1a58b2013-01-22 04:44:30 +00004011 const int index = sample * _audioFrame.num_channels_ + channel;
4012 _audioFrame.data_[index] = toneBuffer[sample];
niklas.enbom@webrtc.orgaf26f642011-11-16 12:41:36 +00004013 }
4014 }
andrew@webrtc.orgae1a58b2013-01-22 04:44:30 +00004015
andrew@webrtc.org63a50982012-05-02 23:56:37 +00004016 assert(_audioFrame.samples_per_channel_ == toneSamples);
niklase@google.com470e71d2011-07-07 08:21:25 +00004017 } else
4018 {
4019 // Add 10ms to "delay-since-last-tone" counter
4020 _inbandDtmfGenerator.UpdateDelaySinceLastTone();
4021 }
4022 return 0;
4023}
4024
pbos@webrtc.org6141e132013-04-09 10:09:10 +00004025int32_t
pkasting@chromium.org4591fbd2014-11-20 22:28:14 +00004026Channel::SendPacketRaw(const void *data, size_t len, bool RTCP)
niklase@google.com470e71d2011-07-07 08:21:25 +00004027{
wu@webrtc.orgfb648da2013-10-18 21:10:51 +00004028 CriticalSectionScoped cs(&_callbackCritSect);
niklase@google.com470e71d2011-07-07 08:21:25 +00004029 if (_transportPtr == NULL)
4030 {
4031 return -1;
4032 }
4033 if (!RTCP)
4034 {
4035 return _transportPtr->SendPacket(_channelId, data, len);
4036 }
4037 else
4038 {
4039 return _transportPtr->SendRTCPPacket(_channelId, data, len);
4040 }
4041}
4042
pwestin@webrtc.org1de01352013-04-11 20:23:35 +00004043// Called for incoming RTP packets after successful RTP header parsing.
4044void Channel::UpdatePacketDelay(uint32_t rtp_timestamp,
4045 uint16_t sequence_number) {
4046 WEBRTC_TRACE(kTraceStream, kTraceVoice, VoEId(_instanceId,_channelId),
4047 "Channel::UpdatePacketDelay(timestamp=%lu, sequenceNumber=%u)",
4048 rtp_timestamp, sequence_number);
niklase@google.com470e71d2011-07-07 08:21:25 +00004049
pwestin@webrtc.org1de01352013-04-11 20:23:35 +00004050 // Get frequency of last received payload
wu@webrtc.org94454b72014-06-05 20:34:08 +00004051 int rtp_receive_frequency = GetPlayoutFrequency();
niklase@google.com470e71d2011-07-07 08:21:25 +00004052
turaj@webrtc.orge46c8d32013-05-22 20:39:43 +00004053 // Update the least required delay.
andrew@webrtc.orgeb524d92013-09-23 23:02:24 +00004054 least_required_delay_ms_ = audio_coding_->LeastRequiredDelayMs();
turaj@webrtc.orge46c8d32013-05-22 20:39:43 +00004055
turaj@webrtc.org167b6df2013-12-13 21:05:07 +00004056 // |jitter_buffer_playout_timestamp_| updated in UpdatePlayoutTimestamp for
4057 // every incoming packet.
4058 uint32_t timestamp_diff_ms = (rtp_timestamp -
4059 jitter_buffer_playout_timestamp_) / (rtp_receive_frequency / 1000);
henrik.lundin@webrtc.orgd6692992014-03-20 12:04:09 +00004060 if (!IsNewerTimestamp(rtp_timestamp, jitter_buffer_playout_timestamp_) ||
4061 timestamp_diff_ms > (2 * kVoiceEngineMaxMinPlayoutDelayMs)) {
4062 // If |jitter_buffer_playout_timestamp_| is newer than the incoming RTP
4063 // timestamp, the resulting difference is negative, but is set to zero.
4064 // This can happen when a network glitch causes a packet to arrive late,
4065 // and during long comfort noise periods with clock drift.
4066 timestamp_diff_ms = 0;
4067 }
niklase@google.com470e71d2011-07-07 08:21:25 +00004068
pwestin@webrtc.org1de01352013-04-11 20:23:35 +00004069 uint16_t packet_delay_ms = (rtp_timestamp - _previousTimestamp) /
4070 (rtp_receive_frequency / 1000);
niklase@google.com470e71d2011-07-07 08:21:25 +00004071
pwestin@webrtc.org1de01352013-04-11 20:23:35 +00004072 _previousTimestamp = rtp_timestamp;
niklase@google.com470e71d2011-07-07 08:21:25 +00004073
pwestin@webrtc.org1de01352013-04-11 20:23:35 +00004074 if (timestamp_diff_ms == 0) return;
niklase@google.com470e71d2011-07-07 08:21:25 +00004075
pwestin@webrtc.org1de01352013-04-11 20:23:35 +00004076 if (packet_delay_ms >= 10 && packet_delay_ms <= 60) {
4077 _recPacketDelayMs = packet_delay_ms;
4078 }
niklase@google.com470e71d2011-07-07 08:21:25 +00004079
pwestin@webrtc.org1de01352013-04-11 20:23:35 +00004080 if (_average_jitter_buffer_delay_us == 0) {
4081 _average_jitter_buffer_delay_us = timestamp_diff_ms * 1000;
4082 return;
4083 }
4084
4085 // Filter average delay value using exponential filter (alpha is
4086 // 7/8). We derive 1000 *_average_jitter_buffer_delay_us here (reduces
4087 // risk of rounding error) and compensate for it in GetDelayEstimate()
4088 // later.
4089 _average_jitter_buffer_delay_us = (_average_jitter_buffer_delay_us * 7 +
4090 1000 * timestamp_diff_ms + 500) / 8;
niklase@google.com470e71d2011-07-07 08:21:25 +00004091}
4092
4093void
4094Channel::RegisterReceiveCodecsToRTPModule()
4095{
4096 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId,_channelId),
4097 "Channel::RegisterReceiveCodecsToRTPModule()");
4098
4099
4100 CodecInst codec;
pbos@webrtc.org6141e132013-04-09 10:09:10 +00004101 const uint8_t nSupportedCodecs = AudioCodingModule::NumberOfCodecs();
niklase@google.com470e71d2011-07-07 08:21:25 +00004102
4103 for (int idx = 0; idx < nSupportedCodecs; idx++)
4104 {
4105 // Open up the RTP/RTCP receiver for all supported codecs
andrew@webrtc.orgeb524d92013-09-23 23:02:24 +00004106 if ((audio_coding_->Codec(idx, &codec) == -1) ||
wu@webrtc.org822fbd82013-08-15 23:38:54 +00004107 (rtp_receiver_->RegisterReceivePayload(
4108 codec.plname,
4109 codec.pltype,
4110 codec.plfreq,
4111 codec.channels,
4112 (codec.rate < 0) ? 0 : codec.rate) == -1))
niklase@google.com470e71d2011-07-07 08:21:25 +00004113 {
4114 WEBRTC_TRACE(
4115 kTraceWarning,
4116 kTraceVoice,
4117 VoEId(_instanceId, _channelId),
4118 "Channel::RegisterReceiveCodecsToRTPModule() unable"
4119 " to register %s (%d/%d/%d/%d) to RTP/RTCP receiver",
4120 codec.plname, codec.pltype, codec.plfreq,
4121 codec.channels, codec.rate);
4122 }
4123 else
4124 {
4125 WEBRTC_TRACE(
4126 kTraceInfo,
4127 kTraceVoice,
4128 VoEId(_instanceId, _channelId),
4129 "Channel::RegisterReceiveCodecsToRTPModule() %s "
wu@webrtc.orgfcd12b32011-09-15 20:49:50 +00004130 "(%d/%d/%d/%d) has been added to the RTP/RTCP "
niklase@google.com470e71d2011-07-07 08:21:25 +00004131 "receiver",
4132 codec.plname, codec.pltype, codec.plfreq,
4133 codec.channels, codec.rate);
4134 }
4135 }
4136}
4137
turaj@webrtc.org8c8ad852013-01-31 18:20:17 +00004138// Assuming this method is called with valid payload type.
turaj@webrtc.org42259e72012-12-11 02:15:12 +00004139int Channel::SetRedPayloadType(int red_payload_type) {
turaj@webrtc.org42259e72012-12-11 02:15:12 +00004140 CodecInst codec;
4141 bool found_red = false;
4142
4143 // Get default RED settings from the ACM database
4144 const int num_codecs = AudioCodingModule::NumberOfCodecs();
4145 for (int idx = 0; idx < num_codecs; idx++) {
andrew@webrtc.orgeb524d92013-09-23 23:02:24 +00004146 audio_coding_->Codec(idx, &codec);
turaj@webrtc.org42259e72012-12-11 02:15:12 +00004147 if (!STR_CASE_CMP(codec.plname, "RED")) {
4148 found_red = true;
4149 break;
4150 }
4151 }
4152
4153 if (!found_red) {
4154 _engineStatisticsPtr->SetLastError(
4155 VE_CODEC_ERROR, kTraceError,
4156 "SetRedPayloadType() RED is not supported");
4157 return -1;
4158 }
4159
turaj@webrtc.org9d532fd2013-01-31 18:34:19 +00004160 codec.pltype = red_payload_type;
andrew@webrtc.orgeb524d92013-09-23 23:02:24 +00004161 if (audio_coding_->RegisterSendCodec(codec) < 0) {
turaj@webrtc.org42259e72012-12-11 02:15:12 +00004162 _engineStatisticsPtr->SetLastError(
4163 VE_AUDIO_CODING_MODULE_ERROR, kTraceError,
4164 "SetRedPayloadType() RED registration in ACM module failed");
4165 return -1;
4166 }
4167
4168 if (_rtpRtcpModule->SetSendREDPayloadType(red_payload_type) != 0) {
4169 _engineStatisticsPtr->SetLastError(
4170 VE_RTP_RTCP_MODULE_ERROR, kTraceError,
4171 "SetRedPayloadType() RED registration in RTP/RTCP module failed");
4172 return -1;
4173 }
4174 return 0;
4175}
4176
wu@webrtc.orgebdb0e32014-03-06 23:49:08 +00004177int Channel::SetSendRtpHeaderExtension(bool enable, RTPExtensionType type,
4178 unsigned char id) {
4179 int error = 0;
4180 _rtpRtcpModule->DeregisterSendRtpHeaderExtension(type);
4181 if (enable) {
4182 error = _rtpRtcpModule->RegisterSendRtpHeaderExtension(type, id);
4183 }
4184 return error;
4185}
minyue@webrtc.orgc1a40a72014-05-28 09:52:06 +00004186
wu@webrtc.org94454b72014-06-05 20:34:08 +00004187int32_t Channel::GetPlayoutFrequency() {
4188 int32_t playout_frequency = audio_coding_->PlayoutFrequency();
4189 CodecInst current_recive_codec;
4190 if (audio_coding_->ReceiveCodec(&current_recive_codec) == 0) {
4191 if (STR_CASE_CMP("G722", current_recive_codec.plname) == 0) {
4192 // Even though the actual sampling rate for G.722 audio is
4193 // 16,000 Hz, the RTP clock rate for the G722 payload format is
4194 // 8,000 Hz because that value was erroneously assigned in
4195 // RFC 1890 and must remain unchanged for backward compatibility.
4196 playout_frequency = 8000;
4197 } else if (STR_CASE_CMP("opus", current_recive_codec.plname) == 0) {
4198 // We are resampling Opus internally to 32,000 Hz until all our
4199 // DSP routines can operate at 48,000 Hz, but the RTP clock
4200 // rate for the Opus payload format is standardized to 48,000 Hz,
4201 // because that is the maximum supported decoding sampling rate.
4202 playout_frequency = 48000;
4203 }
4204 }
4205 return playout_frequency;
4206}
4207
pkasting@chromium.org16825b12015-01-12 21:51:21 +00004208int64_t Channel::GetRTT() const {
minyue@webrtc.org2b58a442014-09-11 07:51:53 +00004209 RTCPMethod method = _rtpRtcpModule->RTCP();
4210 if (method == kRtcpOff) {
minyue@webrtc.org2b58a442014-09-11 07:51:53 +00004211 return 0;
4212 }
4213 std::vector<RTCPReportBlock> report_blocks;
4214 _rtpRtcpModule->RemoteRTCPStat(&report_blocks);
4215 if (report_blocks.empty()) {
minyue@webrtc.org2b58a442014-09-11 07:51:53 +00004216 return 0;
4217 }
4218
4219 uint32_t remoteSSRC = rtp_receiver_->SSRC();
4220 std::vector<RTCPReportBlock>::const_iterator it = report_blocks.begin();
4221 for (; it != report_blocks.end(); ++it) {
4222 if (it->remoteSSRC == remoteSSRC)
4223 break;
4224 }
4225 if (it == report_blocks.end()) {
4226 // We have not received packets with SSRC matching the report blocks.
4227 // To calculate RTT we try with the SSRC of the first report block.
4228 // This is very important for send-only channels where we don't know
4229 // the SSRC of the other end.
4230 remoteSSRC = report_blocks[0].remoteSSRC;
4231 }
pkasting@chromium.org16825b12015-01-12 21:51:21 +00004232 int64_t rtt = 0;
4233 int64_t avg_rtt = 0;
4234 int64_t max_rtt= 0;
4235 int64_t min_rtt = 0;
minyue@webrtc.org2b58a442014-09-11 07:51:53 +00004236 if (_rtpRtcpModule->RTT(remoteSSRC, &rtt, &avg_rtt, &min_rtt, &max_rtt)
4237 != 0) {
minyue@webrtc.org2b58a442014-09-11 07:51:53 +00004238 return 0;
4239 }
pkasting@chromium.org16825b12015-01-12 21:51:21 +00004240 return rtt;
minyue@webrtc.org2b58a442014-09-11 07:51:53 +00004241}
4242
pbos@webrtc.orgd900e8b2013-07-03 15:12:26 +00004243} // namespace voe
4244} // namespace webrtc