blob: 95bcc5cfaacf7161ccc73de902cf537edc5d85fd [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
198Channel::InFrameType(int16_t frameType)
niklase@google.com470e71d2011-07-07 08:21:25 +0000199{
200 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId,_channelId),
201 "Channel::InFrameType(frameType=%d)", frameType);
202
mflodman@webrtc.org9a065d12012-03-07 08:12:21 +0000203 CriticalSectionScoped cs(&_callbackCritSect);
niklase@google.com470e71d2011-07-07 08:21:25 +0000204 // 1 indicates speech
205 _sendFrameType = (frameType == 1) ? 1 : 0;
206 return 0;
207}
208
pbos@webrtc.org6141e132013-04-09 10:09:10 +0000209int32_t
pbos@webrtc.org92135212013-05-14 08:31:39 +0000210Channel::OnRxVadDetected(int vadDecision)
niklase@google.com470e71d2011-07-07 08:21:25 +0000211{
212 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId, _channelId),
213 "Channel::OnRxVadDetected(vadDecision=%d)", vadDecision);
214
mflodman@webrtc.org9a065d12012-03-07 08:12:21 +0000215 CriticalSectionScoped cs(&_callbackCritSect);
niklase@google.com470e71d2011-07-07 08:21:25 +0000216 if (_rxVadObserverPtr)
217 {
218 _rxVadObserverPtr->OnRxVad(_channelId, vadDecision);
219 }
220
221 return 0;
222}
223
224int
pkasting@chromium.org4591fbd2014-11-20 22:28:14 +0000225Channel::SendPacket(int channel, const void *data, size_t len)
niklase@google.com470e71d2011-07-07 08:21:25 +0000226{
227 channel = VoEChannelId(channel);
228 assert(channel == _channelId);
229
230 WEBRTC_TRACE(kTraceStream, kTraceVoice, VoEId(_instanceId,_channelId),
pkasting@chromium.org4591fbd2014-11-20 22:28:14 +0000231 "Channel::SendPacket(channel=%d, len=%" PRIuS ")", channel,
232 len);
niklase@google.com470e71d2011-07-07 08:21:25 +0000233
wu@webrtc.orgfb648da2013-10-18 21:10:51 +0000234 CriticalSectionScoped cs(&_callbackCritSect);
235
niklase@google.com470e71d2011-07-07 08:21:25 +0000236 if (_transportPtr == NULL)
237 {
238 WEBRTC_TRACE(kTraceError, kTraceVoice, VoEId(_instanceId,_channelId),
239 "Channel::SendPacket() failed to send RTP packet due to"
240 " invalid transport object");
241 return -1;
242 }
243
pbos@webrtc.org6141e132013-04-09 10:09:10 +0000244 uint8_t* bufferToSendPtr = (uint8_t*)data;
pkasting@chromium.org4591fbd2014-11-20 22:28:14 +0000245 size_t bufferLength = len;
niklase@google.com470e71d2011-07-07 08:21:25 +0000246
247 // Dump the RTP packet to a file (if RTP dump is enabled).
pbos@webrtc.org6141e132013-04-09 10:09:10 +0000248 if (_rtpDumpOut.DumpPacket((const uint8_t*)data, len) == -1)
niklase@google.com470e71d2011-07-07 08:21:25 +0000249 {
250 WEBRTC_TRACE(kTraceWarning, kTraceVoice,
251 VoEId(_instanceId,_channelId),
252 "Channel::SendPacket() RTP dump to output file failed");
253 }
254
wu@webrtc.orgfb648da2013-10-18 21:10:51 +0000255 int n = _transportPtr->SendPacket(channel, bufferToSendPtr,
256 bufferLength);
257 if (n < 0) {
258 std::string transport_name =
259 _externalTransport ? "external transport" : "WebRtc sockets";
260 WEBRTC_TRACE(kTraceError, kTraceVoice,
261 VoEId(_instanceId,_channelId),
262 "Channel::SendPacket() RTP transmission using %s failed",
263 transport_name.c_str());
264 return -1;
niklase@google.com470e71d2011-07-07 08:21:25 +0000265 }
wu@webrtc.orgfb648da2013-10-18 21:10:51 +0000266 return n;
niklase@google.com470e71d2011-07-07 08:21:25 +0000267}
268
269int
pkasting@chromium.org4591fbd2014-11-20 22:28:14 +0000270Channel::SendRTCPPacket(int channel, const void *data, size_t len)
niklase@google.com470e71d2011-07-07 08:21:25 +0000271{
272 channel = VoEChannelId(channel);
273 assert(channel == _channelId);
274
275 WEBRTC_TRACE(kTraceStream, kTraceVoice, VoEId(_instanceId,_channelId),
pkasting@chromium.org4591fbd2014-11-20 22:28:14 +0000276 "Channel::SendRTCPPacket(channel=%d, len=%" PRIuS ")", channel,
277 len);
niklase@google.com470e71d2011-07-07 08:21:25 +0000278
wu@webrtc.orgfb648da2013-10-18 21:10:51 +0000279 CriticalSectionScoped cs(&_callbackCritSect);
280 if (_transportPtr == NULL)
niklase@google.com470e71d2011-07-07 08:21:25 +0000281 {
wu@webrtc.orgfb648da2013-10-18 21:10:51 +0000282 WEBRTC_TRACE(kTraceError, kTraceVoice,
283 VoEId(_instanceId,_channelId),
284 "Channel::SendRTCPPacket() failed to send RTCP packet"
285 " due to invalid transport object");
286 return -1;
niklase@google.com470e71d2011-07-07 08:21:25 +0000287 }
288
pbos@webrtc.org6141e132013-04-09 10:09:10 +0000289 uint8_t* bufferToSendPtr = (uint8_t*)data;
pkasting@chromium.org4591fbd2014-11-20 22:28:14 +0000290 size_t bufferLength = len;
niklase@google.com470e71d2011-07-07 08:21:25 +0000291
292 // Dump the RTCP packet to a file (if RTP dump is enabled).
pbos@webrtc.org6141e132013-04-09 10:09:10 +0000293 if (_rtpDumpOut.DumpPacket((const uint8_t*)data, len) == -1)
niklase@google.com470e71d2011-07-07 08:21:25 +0000294 {
295 WEBRTC_TRACE(kTraceWarning, kTraceVoice,
296 VoEId(_instanceId,_channelId),
297 "Channel::SendPacket() RTCP dump to output file failed");
298 }
299
wu@webrtc.orgfb648da2013-10-18 21:10:51 +0000300 int n = _transportPtr->SendRTCPPacket(channel,
301 bufferToSendPtr,
302 bufferLength);
303 if (n < 0) {
304 std::string transport_name =
305 _externalTransport ? "external transport" : "WebRtc sockets";
306 WEBRTC_TRACE(kTraceInfo, kTraceVoice,
307 VoEId(_instanceId,_channelId),
308 "Channel::SendRTCPPacket() transmission using %s failed",
309 transport_name.c_str());
310 return -1;
niklase@google.com470e71d2011-07-07 08:21:25 +0000311 }
wu@webrtc.orgfb648da2013-10-18 21:10:51 +0000312 return n;
niklase@google.com470e71d2011-07-07 08:21:25 +0000313}
314
315void
pbos@webrtc.org92135212013-05-14 08:31:39 +0000316Channel::OnPlayTelephoneEvent(int32_t id,
317 uint8_t event,
318 uint16_t lengthMs,
319 uint8_t volume)
niklase@google.com470e71d2011-07-07 08:21:25 +0000320{
321 WEBRTC_TRACE(kTraceStream, kTraceVoice, VoEId(_instanceId,_channelId),
322 "Channel::OnPlayTelephoneEvent(id=%d, event=%u, lengthMs=%u,"
wu@webrtc.orgfcd12b32011-09-15 20:49:50 +0000323 " volume=%u)", id, event, lengthMs, volume);
niklase@google.com470e71d2011-07-07 08:21:25 +0000324
325 if (!_playOutbandDtmfEvent || (event > 15))
326 {
327 // Ignore callback since feedback is disabled or event is not a
328 // Dtmf tone event.
329 return;
330 }
331
332 assert(_outputMixerPtr != NULL);
333
334 // Start playing out the Dtmf tone (if playout is enabled).
335 // Reduce length of tone with 80ms to the reduce risk of echo.
336 _outputMixerPtr->PlayDtmfTone(event, lengthMs - 80, volume);
337}
338
339void
stefan@webrtc.org286fe0b2013-08-21 20:58:21 +0000340Channel::OnIncomingSSRCChanged(int32_t id, uint32_t ssrc)
niklase@google.com470e71d2011-07-07 08:21:25 +0000341{
342 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId,_channelId),
343 "Channel::OnIncomingSSRCChanged(id=%d, SSRC=%d)",
stefan@webrtc.org286fe0b2013-08-21 20:58:21 +0000344 id, ssrc);
niklase@google.com470e71d2011-07-07 08:21:25 +0000345
dwkang@webrtc.orgb295a3f2013-08-29 07:34:12 +0000346 // Update ssrc so that NTP for AV sync can be updated.
347 _rtpRtcpModule->SetRemoteSSRC(ssrc);
niklase@google.com470e71d2011-07-07 08:21:25 +0000348}
349
pbos@webrtc.org92135212013-05-14 08:31:39 +0000350void Channel::OnIncomingCSRCChanged(int32_t id,
351 uint32_t CSRC,
352 bool added)
niklase@google.com470e71d2011-07-07 08:21:25 +0000353{
354 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId,_channelId),
355 "Channel::OnIncomingCSRCChanged(id=%d, CSRC=%d, added=%d)",
356 id, CSRC, added);
niklase@google.com470e71d2011-07-07 08:21:25 +0000357}
358
stefan@webrtc.org286fe0b2013-08-21 20:58:21 +0000359void Channel::ResetStatistics(uint32_t ssrc) {
360 StreamStatistician* statistician =
361 rtp_receive_statistics_->GetStatistician(ssrc);
362 if (statistician) {
363 statistician->ResetStatistics();
364 }
sprang@webrtc.org54ae4ff2013-12-19 13:26:02 +0000365 statistics_proxy_->ResetStatistics();
wu@webrtc.org822fbd82013-08-15 23:38:54 +0000366}
367
pbos@webrtc.org6141e132013-04-09 10:09:10 +0000368int32_t
niklase@google.com470e71d2011-07-07 08:21:25 +0000369Channel::OnInitializeDecoder(
pbos@webrtc.org92135212013-05-14 08:31:39 +0000370 int32_t id,
371 int8_t payloadType,
leozwang@webrtc.org813e4b02012-03-01 18:34:25 +0000372 const char payloadName[RTP_PAYLOAD_NAME_SIZE],
pbos@webrtc.org92135212013-05-14 08:31:39 +0000373 int frequency,
374 uint8_t channels,
375 uint32_t rate)
niklase@google.com470e71d2011-07-07 08:21:25 +0000376{
377 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId,_channelId),
378 "Channel::OnInitializeDecoder(id=%d, payloadType=%d, "
379 "payloadName=%s, frequency=%u, channels=%u, rate=%u)",
380 id, payloadType, payloadName, frequency, channels, rate);
381
andrew@webrtc.orgceb148c2011-08-23 17:53:54 +0000382 assert(VoEChannelId(id) == _channelId);
niklase@google.com470e71d2011-07-07 08:21:25 +0000383
henrika@webrtc.orgf75901f2012-01-16 08:45:42 +0000384 CodecInst receiveCodec = {0};
385 CodecInst dummyCodec = {0};
niklase@google.com470e71d2011-07-07 08:21:25 +0000386
387 receiveCodec.pltype = payloadType;
niklase@google.com470e71d2011-07-07 08:21:25 +0000388 receiveCodec.plfreq = frequency;
389 receiveCodec.channels = channels;
390 receiveCodec.rate = rate;
henrika@webrtc.orgf75901f2012-01-16 08:45:42 +0000391 strncpy(receiveCodec.plname, payloadName, RTP_PAYLOAD_NAME_SIZE - 1);
andrew@webrtc.orgae1a58b2013-01-22 04:44:30 +0000392
andrew@webrtc.orgeb524d92013-09-23 23:02:24 +0000393 audio_coding_->Codec(payloadName, &dummyCodec, frequency, channels);
niklase@google.com470e71d2011-07-07 08:21:25 +0000394 receiveCodec.pacsize = dummyCodec.pacsize;
395
396 // Register the new codec to the ACM
andrew@webrtc.orgeb524d92013-09-23 23:02:24 +0000397 if (audio_coding_->RegisterReceiveCodec(receiveCodec) == -1)
niklase@google.com470e71d2011-07-07 08:21:25 +0000398 {
399 WEBRTC_TRACE(kTraceWarning, kTraceVoice,
andrew@webrtc.orgceb148c2011-08-23 17:53:54 +0000400 VoEId(_instanceId, _channelId),
niklase@google.com470e71d2011-07-07 08:21:25 +0000401 "Channel::OnInitializeDecoder() invalid codec ("
402 "pt=%d, name=%s) received - 1", payloadType, payloadName);
403 _engineStatisticsPtr->SetLastError(VE_AUDIO_CODING_MODULE_ERROR);
404 return -1;
405 }
406
407 return 0;
408}
409
pbos@webrtc.org6141e132013-04-09 10:09:10 +0000410int32_t
411Channel::OnReceivedPayloadData(const uint8_t* payloadData,
pkasting@chromium.org4591fbd2014-11-20 22:28:14 +0000412 size_t payloadSize,
niklase@google.com470e71d2011-07-07 08:21:25 +0000413 const WebRtcRTPHeader* rtpHeader)
414{
415 WEBRTC_TRACE(kTraceStream, kTraceVoice, VoEId(_instanceId,_channelId),
pkasting@chromium.org4591fbd2014-11-20 22:28:14 +0000416 "Channel::OnReceivedPayloadData(payloadSize=%" PRIuS ","
niklase@google.com470e71d2011-07-07 08:21:25 +0000417 " payloadType=%u, audioChannel=%u)",
418 payloadSize,
419 rtpHeader->header.payloadType,
420 rtpHeader->type.Audio.channel);
421
henrika@webrtc.org944cbeb2014-03-18 10:32:33 +0000422 if (!channel_state_.Get().playing)
niklase@google.com470e71d2011-07-07 08:21:25 +0000423 {
424 // Avoid inserting into NetEQ when we are not playing. Count the
425 // packet as discarded.
426 WEBRTC_TRACE(kTraceStream, kTraceVoice,
427 VoEId(_instanceId, _channelId),
428 "received packet is discarded since playing is not"
429 " activated");
430 _numberOfDiscardedPackets++;
431 return 0;
432 }
433
434 // Push the incoming payload (parsed and ready for decoding) into the ACM
andrew@webrtc.orgeb524d92013-09-23 23:02:24 +0000435 if (audio_coding_->IncomingPacket(payloadData,
436 payloadSize,
437 *rtpHeader) != 0)
niklase@google.com470e71d2011-07-07 08:21:25 +0000438 {
439 _engineStatisticsPtr->SetLastError(
440 VE_AUDIO_CODING_MODULE_ERROR, kTraceWarning,
441 "Channel::OnReceivedPayloadData() unable to push data to the ACM");
442 return -1;
443 }
444
pwestin@webrtc.orgd30859e2013-06-06 21:09:01 +0000445 // Update the packet delay.
niklase@google.com470e71d2011-07-07 08:21:25 +0000446 UpdatePacketDelay(rtpHeader->header.timestamp,
447 rtpHeader->header.sequenceNumber);
pwestin@webrtc.orgd30859e2013-06-06 21:09:01 +0000448
pkasting@chromium.org16825b12015-01-12 21:51:21 +0000449 int64_t round_trip_time = 0;
wu@webrtc.org822fbd82013-08-15 23:38:54 +0000450 _rtpRtcpModule->RTT(rtp_receiver_->SSRC(), &round_trip_time,
451 NULL, NULL, NULL);
pwestin@webrtc.orgd30859e2013-06-06 21:09:01 +0000452
andrew@webrtc.orgeb524d92013-09-23 23:02:24 +0000453 std::vector<uint16_t> nack_list = audio_coding_->GetNackList(
wu@webrtc.org822fbd82013-08-15 23:38:54 +0000454 round_trip_time);
455 if (!nack_list.empty()) {
456 // Can't use nack_list.data() since it's not supported by all
457 // compilers.
458 ResendPackets(&(nack_list[0]), static_cast<int>(nack_list.size()));
pwestin@webrtc.orgd30859e2013-06-06 21:09:01 +0000459 }
niklase@google.com470e71d2011-07-07 08:21:25 +0000460 return 0;
461}
462
stefan@webrtc.org7bb8f022013-09-06 13:40:11 +0000463bool Channel::OnRecoveredPacket(const uint8_t* rtp_packet,
pkasting@chromium.org4591fbd2014-11-20 22:28:14 +0000464 size_t rtp_packet_length) {
stefan@webrtc.org7bb8f022013-09-06 13:40:11 +0000465 RTPHeader header;
466 if (!rtp_header_parser_->Parse(rtp_packet, rtp_packet_length, &header)) {
467 WEBRTC_TRACE(kTraceDebug, webrtc::kTraceVoice, _channelId,
468 "IncomingPacket invalid RTP header");
469 return false;
470 }
471 header.payload_type_frequency =
472 rtp_payload_registry_->GetPayloadTypeFrequency(header.payloadType);
473 if (header.payload_type_frequency < 0)
474 return false;
475 return ReceivePacket(rtp_packet, rtp_packet_length, header, false);
476}
477
pbos@webrtc.org92135212013-05-14 08:31:39 +0000478int32_t Channel::GetAudioFrame(int32_t id, AudioFrame& audioFrame)
niklase@google.com470e71d2011-07-07 08:21:25 +0000479{
480 WEBRTC_TRACE(kTraceStream, kTraceVoice, VoEId(_instanceId,_channelId),
481 "Channel::GetAudioFrame(id=%d)", id);
482
483 // Get 10ms raw PCM data from the ACM (mixer limits output frequency)
andrew@webrtc.orgeb524d92013-09-23 23:02:24 +0000484 if (audio_coding_->PlayoutData10Ms(audioFrame.sample_rate_hz_,
485 &audioFrame) == -1)
niklase@google.com470e71d2011-07-07 08:21:25 +0000486 {
487 WEBRTC_TRACE(kTraceError, kTraceVoice,
488 VoEId(_instanceId,_channelId),
489 "Channel::GetAudioFrame() PlayoutData10Ms() failed!");
andrew@webrtc.org7859e102012-01-13 00:30:11 +0000490 // In all likelihood, the audio in this frame is garbage. We return an
491 // error so that the audio mixer module doesn't add it to the mix. As
492 // a result, it won't be played out and the actions skipped here are
493 // irrelevant.
494 return -1;
niklase@google.com470e71d2011-07-07 08:21:25 +0000495 }
496
497 if (_RxVadDetection)
498 {
499 UpdateRxVadDetection(audioFrame);
500 }
501
502 // Convert module ID to internal VoE channel ID
andrew@webrtc.org63a50982012-05-02 23:56:37 +0000503 audioFrame.id_ = VoEChannelId(audioFrame.id_);
niklase@google.com470e71d2011-07-07 08:21:25 +0000504 // Store speech type for dead-or-alive detection
andrew@webrtc.org63a50982012-05-02 23:56:37 +0000505 _outputSpeechType = audioFrame.speech_type_;
niklase@google.com470e71d2011-07-07 08:21:25 +0000506
henrika@webrtc.org944cbeb2014-03-18 10:32:33 +0000507 ChannelState::State state = channel_state_.Get();
508
509 if (state.rx_apm_is_enabled) {
andrew@webrtc.org60730cf2014-01-07 17:45:09 +0000510 int err = rx_audioproc_->ProcessStream(&audioFrame);
511 if (err) {
512 LOG(LS_ERROR) << "ProcessStream() error: " << err;
513 assert(false);
514 }
niklase@google.com470e71d2011-07-07 08:21:25 +0000515 }
516
wu@webrtc.org63420662013-10-17 18:28:55 +0000517 float output_gain = 1.0f;
518 float left_pan = 1.0f;
519 float right_pan = 1.0f;
niklase@google.com470e71d2011-07-07 08:21:25 +0000520 {
wu@webrtc.org63420662013-10-17 18:28:55 +0000521 CriticalSectionScoped cs(&volume_settings_critsect_);
522 output_gain = _outputGain;
523 left_pan = _panLeft;
524 right_pan= _panRight;
525 }
526
527 // Output volume scaling
528 if (output_gain < 0.99f || output_gain > 1.01f)
529 {
530 AudioFrameOperations::ScaleWithSat(output_gain, audioFrame);
niklase@google.com470e71d2011-07-07 08:21:25 +0000531 }
532
533 // Scale left and/or right channel(s) if stereo and master balance is
534 // active
535
wu@webrtc.org63420662013-10-17 18:28:55 +0000536 if (left_pan != 1.0f || right_pan != 1.0f)
niklase@google.com470e71d2011-07-07 08:21:25 +0000537 {
andrew@webrtc.org63a50982012-05-02 23:56:37 +0000538 if (audioFrame.num_channels_ == 1)
niklase@google.com470e71d2011-07-07 08:21:25 +0000539 {
540 // Emulate stereo mode since panning is active.
541 // The mono signal is copied to both left and right channels here.
andrew@webrtc.org4ecea3e2012-06-27 03:25:31 +0000542 AudioFrameOperations::MonoToStereo(&audioFrame);
niklase@google.com470e71d2011-07-07 08:21:25 +0000543 }
544 // For true stereo mode (when we are receiving a stereo signal), no
545 // action is needed.
546
547 // Do the panning operation (the audio frame contains stereo at this
548 // stage)
wu@webrtc.org63420662013-10-17 18:28:55 +0000549 AudioFrameOperations::Scale(left_pan, right_pan, audioFrame);
niklase@google.com470e71d2011-07-07 08:21:25 +0000550 }
551
552 // Mix decoded PCM output with file if file mixing is enabled
henrika@webrtc.org944cbeb2014-03-18 10:32:33 +0000553 if (state.output_file_playing)
niklase@google.com470e71d2011-07-07 08:21:25 +0000554 {
andrew@webrtc.org63a50982012-05-02 23:56:37 +0000555 MixAudioWithFile(audioFrame, audioFrame.sample_rate_hz_);
niklase@google.com470e71d2011-07-07 08:21:25 +0000556 }
557
niklase@google.com470e71d2011-07-07 08:21:25 +0000558 // External media
559 if (_outputExternalMedia)
560 {
mflodman@webrtc.org9a065d12012-03-07 08:12:21 +0000561 CriticalSectionScoped cs(&_callbackCritSect);
andrew@webrtc.org63a50982012-05-02 23:56:37 +0000562 const bool isStereo = (audioFrame.num_channels_ == 2);
niklase@google.com470e71d2011-07-07 08:21:25 +0000563 if (_outputExternalMediaCallbackPtr)
564 {
565 _outputExternalMediaCallbackPtr->Process(
566 _channelId,
567 kPlaybackPerChannel,
pbos@webrtc.org6141e132013-04-09 10:09:10 +0000568 (int16_t*)audioFrame.data_,
andrew@webrtc.org63a50982012-05-02 23:56:37 +0000569 audioFrame.samples_per_channel_,
570 audioFrame.sample_rate_hz_,
niklase@google.com470e71d2011-07-07 08:21:25 +0000571 isStereo);
572 }
573 }
574
575 // Record playout if enabled
576 {
mflodman@webrtc.org9a065d12012-03-07 08:12:21 +0000577 CriticalSectionScoped cs(&_fileCritSect);
niklase@google.com470e71d2011-07-07 08:21:25 +0000578
579 if (_outputFileRecording && _outputFileRecorderPtr)
580 {
niklas.enbom@webrtc.org5398d952012-03-26 08:11:25 +0000581 _outputFileRecorderPtr->RecordAudioToFile(audioFrame);
niklase@google.com470e71d2011-07-07 08:21:25 +0000582 }
583 }
584
585 // Measure audio level (0-9)
586 _outputAudioLevel.ComputeLevel(audioFrame);
587
wu@webrtc.org94454b72014-06-05 20:34:08 +0000588 if (capture_start_rtp_time_stamp_ < 0 && audioFrame.timestamp_ != 0) {
589 // The first frame with a valid rtp timestamp.
wu@webrtc.orgcb711f72014-05-19 17:39:11 +0000590 capture_start_rtp_time_stamp_ = audioFrame.timestamp_;
wu@webrtc.org94454b72014-06-05 20:34:08 +0000591 }
592
593 if (capture_start_rtp_time_stamp_ >= 0) {
594 // audioFrame.timestamp_ should be valid from now on.
595
596 // Compute elapsed time.
597 int64_t unwrap_timestamp =
598 rtp_ts_wraparound_handler_->Unwrap(audioFrame.timestamp_);
599 audioFrame.elapsed_time_ms_ =
600 (unwrap_timestamp - capture_start_rtp_time_stamp_) /
601 (GetPlayoutFrequency() / 1000);
602
stefan@webrtc.org8e24d872014-09-02 18:58:24 +0000603 {
wu@webrtc.orgcb711f72014-05-19 17:39:11 +0000604 CriticalSectionScoped lock(ts_stats_lock_.get());
stefan@webrtc.org8e24d872014-09-02 18:58:24 +0000605 // Compute ntp time.
606 audioFrame.ntp_time_ms_ = ntp_estimator_.Estimate(
607 audioFrame.timestamp_);
608 // |ntp_time_ms_| won't be valid until at least 2 RTCP SRs are received.
609 if (audioFrame.ntp_time_ms_ > 0) {
610 // Compute |capture_start_ntp_time_ms_| so that
611 // |capture_start_ntp_time_ms_| + |elapsed_time_ms_| == |ntp_time_ms_|
612 capture_start_ntp_time_ms_ =
613 audioFrame.ntp_time_ms_ - audioFrame.elapsed_time_ms_;
614 }
wu@webrtc.orgcb711f72014-05-19 17:39:11 +0000615 }
616 }
617
niklase@google.com470e71d2011-07-07 08:21:25 +0000618 return 0;
619}
620
pbos@webrtc.org6141e132013-04-09 10:09:10 +0000621int32_t
pbos@webrtc.org92135212013-05-14 08:31:39 +0000622Channel::NeededFrequency(int32_t id)
niklase@google.com470e71d2011-07-07 08:21:25 +0000623{
624 WEBRTC_TRACE(kTraceStream, kTraceVoice, VoEId(_instanceId,_channelId),
625 "Channel::NeededFrequency(id=%d)", id);
626
627 int highestNeeded = 0;
628
629 // Determine highest needed receive frequency
andrew@webrtc.orgeb524d92013-09-23 23:02:24 +0000630 int32_t receiveFrequency = audio_coding_->ReceiveFrequency();
niklase@google.com470e71d2011-07-07 08:21:25 +0000631
632 // Return the bigger of playout and receive frequency in the ACM.
andrew@webrtc.orgeb524d92013-09-23 23:02:24 +0000633 if (audio_coding_->PlayoutFrequency() > receiveFrequency)
niklase@google.com470e71d2011-07-07 08:21:25 +0000634 {
andrew@webrtc.orgeb524d92013-09-23 23:02:24 +0000635 highestNeeded = audio_coding_->PlayoutFrequency();
niklase@google.com470e71d2011-07-07 08:21:25 +0000636 }
637 else
638 {
639 highestNeeded = receiveFrequency;
640 }
641
642 // Special case, if we're playing a file on the playout side
643 // we take that frequency into consideration as well
644 // This is not needed on sending side, since the codec will
645 // limit the spectrum anyway.
henrika@webrtc.org944cbeb2014-03-18 10:32:33 +0000646 if (channel_state_.Get().output_file_playing)
niklase@google.com470e71d2011-07-07 08:21:25 +0000647 {
mflodman@webrtc.org9a065d12012-03-07 08:12:21 +0000648 CriticalSectionScoped cs(&_fileCritSect);
henrika@webrtc.org944cbeb2014-03-18 10:32:33 +0000649 if (_outputFilePlayerPtr)
niklase@google.com470e71d2011-07-07 08:21:25 +0000650 {
651 if(_outputFilePlayerPtr->Frequency()>highestNeeded)
652 {
653 highestNeeded=_outputFilePlayerPtr->Frequency();
654 }
655 }
656 }
657
658 return(highestNeeded);
659}
660
pbos@webrtc.org6141e132013-04-09 10:09:10 +0000661int32_t
niklase@google.com470e71d2011-07-07 08:21:25 +0000662Channel::CreateChannel(Channel*& channel,
pbos@webrtc.org92135212013-05-14 08:31:39 +0000663 int32_t channelId,
minyue@webrtc.orge509f942013-09-12 17:03:00 +0000664 uint32_t instanceId,
665 const Config& config)
niklase@google.com470e71d2011-07-07 08:21:25 +0000666{
667 WEBRTC_TRACE(kTraceMemory, kTraceVoice, VoEId(instanceId,channelId),
668 "Channel::CreateChannel(channelId=%d, instanceId=%d)",
669 channelId, instanceId);
670
minyue@webrtc.orge509f942013-09-12 17:03:00 +0000671 channel = new Channel(channelId, instanceId, config);
niklase@google.com470e71d2011-07-07 08:21:25 +0000672 if (channel == NULL)
673 {
674 WEBRTC_TRACE(kTraceMemory, kTraceVoice,
675 VoEId(instanceId,channelId),
676 "Channel::CreateChannel() unable to allocate memory for"
677 " channel");
678 return -1;
679 }
680 return 0;
681}
682
683void
pbos@webrtc.org92135212013-05-14 08:31:39 +0000684Channel::PlayNotification(int32_t id, uint32_t durationMs)
niklase@google.com470e71d2011-07-07 08:21:25 +0000685{
686 WEBRTC_TRACE(kTraceStream, kTraceVoice, VoEId(_instanceId,_channelId),
687 "Channel::PlayNotification(id=%d, durationMs=%d)",
688 id, durationMs);
689
690 // Not implement yet
691}
692
693void
pbos@webrtc.org92135212013-05-14 08:31:39 +0000694Channel::RecordNotification(int32_t id, uint32_t durationMs)
niklase@google.com470e71d2011-07-07 08:21:25 +0000695{
696 WEBRTC_TRACE(kTraceStream, kTraceVoice, VoEId(_instanceId,_channelId),
697 "Channel::RecordNotification(id=%d, durationMs=%d)",
698 id, durationMs);
699
700 // Not implement yet
701}
702
703void
pbos@webrtc.org92135212013-05-14 08:31:39 +0000704Channel::PlayFileEnded(int32_t id)
niklase@google.com470e71d2011-07-07 08:21:25 +0000705{
706 WEBRTC_TRACE(kTraceStream, kTraceVoice, VoEId(_instanceId,_channelId),
707 "Channel::PlayFileEnded(id=%d)", id);
708
709 if (id == _inputFilePlayerId)
710 {
henrika@webrtc.org944cbeb2014-03-18 10:32:33 +0000711 channel_state_.SetInputFilePlaying(false);
niklase@google.com470e71d2011-07-07 08:21:25 +0000712 WEBRTC_TRACE(kTraceStateInfo, kTraceVoice,
713 VoEId(_instanceId,_channelId),
714 "Channel::PlayFileEnded() => input file player module is"
715 " shutdown");
716 }
717 else if (id == _outputFilePlayerId)
718 {
henrika@webrtc.org944cbeb2014-03-18 10:32:33 +0000719 channel_state_.SetOutputFilePlaying(false);
niklase@google.com470e71d2011-07-07 08:21:25 +0000720 WEBRTC_TRACE(kTraceStateInfo, kTraceVoice,
721 VoEId(_instanceId,_channelId),
722 "Channel::PlayFileEnded() => output file player module is"
723 " shutdown");
724 }
725}
726
727void
pbos@webrtc.org92135212013-05-14 08:31:39 +0000728Channel::RecordFileEnded(int32_t id)
niklase@google.com470e71d2011-07-07 08:21:25 +0000729{
730 WEBRTC_TRACE(kTraceStream, kTraceVoice, VoEId(_instanceId,_channelId),
731 "Channel::RecordFileEnded(id=%d)", id);
732
733 assert(id == _outputFileRecorderId);
734
mflodman@webrtc.org9a065d12012-03-07 08:12:21 +0000735 CriticalSectionScoped cs(&_fileCritSect);
niklase@google.com470e71d2011-07-07 08:21:25 +0000736
737 _outputFileRecording = false;
738 WEBRTC_TRACE(kTraceStateInfo, kTraceVoice,
739 VoEId(_instanceId,_channelId),
740 "Channel::RecordFileEnded() => output file recorder module is"
741 " shutdown");
742}
743
pbos@webrtc.org92135212013-05-14 08:31:39 +0000744Channel::Channel(int32_t channelId,
minyue@webrtc.orge509f942013-09-12 17:03:00 +0000745 uint32_t instanceId,
746 const Config& config) :
niklase@google.com470e71d2011-07-07 08:21:25 +0000747 _fileCritSect(*CriticalSectionWrapper::CreateCriticalSection()),
748 _callbackCritSect(*CriticalSectionWrapper::CreateCriticalSection()),
wu@webrtc.org63420662013-10-17 18:28:55 +0000749 volume_settings_critsect_(*CriticalSectionWrapper::CreateCriticalSection()),
niklase@google.com470e71d2011-07-07 08:21:25 +0000750 _instanceId(instanceId),
xians@google.com22963ab2011-08-03 12:40:23 +0000751 _channelId(channelId),
stefan@webrtc.orga5cb98c2013-05-29 12:12:51 +0000752 rtp_header_parser_(RtpHeaderParser::Create()),
wu@webrtc.org822fbd82013-08-15 23:38:54 +0000753 rtp_payload_registry_(
andresp@webrtc.orgdc80bae2014-04-08 11:06:12 +0000754 new RTPPayloadRegistry(RTPPayloadStrategy::CreateStrategy(true))),
wu@webrtc.org822fbd82013-08-15 23:38:54 +0000755 rtp_receive_statistics_(ReceiveStatistics::Create(
756 Clock::GetRealTimeClock())),
757 rtp_receiver_(RtpReceiver::CreateAudioReceiver(
758 VoEModuleId(instanceId, channelId), Clock::GetRealTimeClock(), this,
759 this, this, rtp_payload_registry_.get())),
760 telephone_event_handler_(rtp_receiver_->GetTelephoneEventHandler()),
henrik.lundin@webrtc.org34fe0152014-04-22 19:04:34 +0000761 audio_coding_(AudioCodingModule::Create(
xians@google.com22963ab2011-08-03 12:40:23 +0000762 VoEModuleId(instanceId, channelId))),
niklase@google.com470e71d2011-07-07 08:21:25 +0000763 _rtpDumpIn(*RtpDump::CreateRtpDump()),
764 _rtpDumpOut(*RtpDump::CreateRtpDump()),
niklase@google.com470e71d2011-07-07 08:21:25 +0000765 _outputAudioLevel(),
niklase@google.com470e71d2011-07-07 08:21:25 +0000766 _externalTransport(false),
niklase@google.com470e71d2011-07-07 08:21:25 +0000767 _inputFilePlayerPtr(NULL),
768 _outputFilePlayerPtr(NULL),
769 _outputFileRecorderPtr(NULL),
770 // Avoid conflict with other channels by adding 1024 - 1026,
771 // won't use as much as 1024 channels.
772 _inputFilePlayerId(VoEModuleId(instanceId, channelId) + 1024),
773 _outputFilePlayerId(VoEModuleId(instanceId, channelId) + 1025),
774 _outputFileRecorderId(VoEModuleId(instanceId, channelId) + 1026),
niklase@google.com470e71d2011-07-07 08:21:25 +0000775 _outputFileRecording(false),
xians@google.com22963ab2011-08-03 12:40:23 +0000776 _inbandDtmfQueue(VoEModuleId(instanceId, channelId)),
777 _inbandDtmfGenerator(VoEModuleId(instanceId, channelId)),
xians@google.com22963ab2011-08-03 12:40:23 +0000778 _outputExternalMedia(false),
niklase@google.com470e71d2011-07-07 08:21:25 +0000779 _inputExternalMediaCallbackPtr(NULL),
780 _outputExternalMediaCallbackPtr(NULL),
xians@google.com22963ab2011-08-03 12:40:23 +0000781 _timeStamp(0), // This is just an offset, RTP module will add it's own random offset
782 _sendTelephoneEventPayloadType(106),
stefan@webrtc.org8e24d872014-09-02 18:58:24 +0000783 ntp_estimator_(Clock::GetRealTimeClock()),
turaj@webrtc.org167b6df2013-12-13 21:05:07 +0000784 jitter_buffer_playout_timestamp_(0),
pwestin@webrtc.org1de01352013-04-11 20:23:35 +0000785 playout_timestamp_rtp_(0),
786 playout_timestamp_rtcp_(0),
sprang@webrtc.org54ae4ff2013-12-19 13:26:02 +0000787 playout_delay_ms_(0),
xians@google.com22963ab2011-08-03 12:40:23 +0000788 _numberOfDiscardedPackets(0),
xians@webrtc.org09e8c472013-07-31 16:30:19 +0000789 send_sequence_number_(0),
wu@webrtc.orgcb711f72014-05-19 17:39:11 +0000790 ts_stats_lock_(CriticalSectionWrapper::CreateCriticalSection()),
wu@webrtc.org94454b72014-06-05 20:34:08 +0000791 rtp_ts_wraparound_handler_(new rtc::TimestampWrapAroundHandler()),
792 capture_start_rtp_time_stamp_(-1),
wu@webrtc.orgcb711f72014-05-19 17:39:11 +0000793 capture_start_ntp_time_ms_(-1),
xians@google.com22963ab2011-08-03 12:40:23 +0000794 _engineStatisticsPtr(NULL),
henrika@webrtc.org2919e952012-01-31 08:45:03 +0000795 _outputMixerPtr(NULL),
796 _transmitMixerPtr(NULL),
xians@google.com22963ab2011-08-03 12:40:23 +0000797 _moduleProcessThreadPtr(NULL),
798 _audioDeviceModulePtr(NULL),
799 _voiceEngineObserverPtr(NULL),
800 _callbackCritSectPtr(NULL),
801 _transportPtr(NULL),
xians@google.com22963ab2011-08-03 12:40:23 +0000802 _rxVadObserverPtr(NULL),
803 _oldVadDecision(-1),
804 _sendFrameType(0),
roosa@google.com1b60ceb2012-12-12 23:00:29 +0000805 _externalMixing(false),
xians@google.com22963ab2011-08-03 12:40:23 +0000806 _mixFileWithMicrophone(false),
niklase@google.com470e71d2011-07-07 08:21:25 +0000807 _mute(false),
808 _panLeft(1.0f),
809 _panRight(1.0f),
810 _outputGain(1.0f),
811 _playOutbandDtmfEvent(false),
812 _playInbandDtmfEvent(false),
niklase@google.com470e71d2011-07-07 08:21:25 +0000813 _lastLocalTimeStamp(0),
814 _lastPayloadType(0),
xians@google.com22963ab2011-08-03 12:40:23 +0000815 _includeAudioLevelIndication(false),
niklase@google.com470e71d2011-07-07 08:21:25 +0000816 _outputSpeechType(AudioFrame::kNormalSpeech),
solenberg@webrtc.orgb1f50102014-03-24 10:38:25 +0000817 vie_network_(NULL),
818 video_channel_(-1),
pwestin@webrtc.org1de01352013-04-11 20:23:35 +0000819 _average_jitter_buffer_delay_us(0),
turaj@webrtc.orge46c8d32013-05-22 20:39:43 +0000820 least_required_delay_ms_(0),
niklase@google.com470e71d2011-07-07 08:21:25 +0000821 _previousTimestamp(0),
822 _recPacketDelayMs(20),
823 _RxVadDetection(false),
niklase@google.com470e71d2011-07-07 08:21:25 +0000824 _rxAgcIsEnabled(false),
stefan@webrtc.org7bb8f022013-09-06 13:40:11 +0000825 _rxNsIsEnabled(false),
minyue@webrtc.orgc1a40a72014-05-28 09:52:06 +0000826 restored_packet_in_use_(false),
mflodman@webrtc.org0a7d4ee2015-02-17 12:57:14 +0000827 rtcp_observer_(new VoERtcpObserver(this)),
minyue@webrtc.org74aaf292014-07-16 21:28:26 +0000828 network_predictor_(new NetworkPredictor(Clock::GetRealTimeClock()))
niklase@google.com470e71d2011-07-07 08:21:25 +0000829{
830 WEBRTC_TRACE(kTraceMemory, kTraceVoice, VoEId(_instanceId,_channelId),
831 "Channel::Channel() - ctor");
832 _inbandDtmfQueue.ResetDtmf();
833 _inbandDtmfGenerator.Init();
834 _outputAudioLevel.Clear();
835
pwestin@webrtc.org2853dde2012-05-11 11:08:54 +0000836 RtpRtcp::Configuration configuration;
837 configuration.id = VoEModuleId(instanceId, channelId);
838 configuration.audio = true;
pwestin@webrtc.org2853dde2012-05-11 11:08:54 +0000839 configuration.outgoing_transport = this;
pwestin@webrtc.org2853dde2012-05-11 11:08:54 +0000840 configuration.audio_messages = this;
wu@webrtc.org822fbd82013-08-15 23:38:54 +0000841 configuration.receive_statistics = rtp_receive_statistics_.get();
mflodman@webrtc.org0a7d4ee2015-02-17 12:57:14 +0000842 configuration.bandwidth_callback = rtcp_observer_.get();
pwestin@webrtc.org2853dde2012-05-11 11:08:54 +0000843
844 _rtpRtcpModule.reset(RtpRtcp::CreateRtpRtcp(configuration));
sprang@webrtc.org54ae4ff2013-12-19 13:26:02 +0000845
846 statistics_proxy_.reset(new StatisticsProxy(_rtpRtcpModule->SSRC()));
847 rtp_receive_statistics_->RegisterRtcpStatisticsCallback(
848 statistics_proxy_.get());
aluebs@webrtc.orgf927fd62014-04-16 11:58:18 +0000849
850 Config audioproc_config;
851 audioproc_config.Set<ExperimentalAgc>(new ExperimentalAgc(false));
852 rx_audioproc_.reset(AudioProcessing::Create(audioproc_config));
niklase@google.com470e71d2011-07-07 08:21:25 +0000853}
854
855Channel::~Channel()
856{
sprang@webrtc.org54ae4ff2013-12-19 13:26:02 +0000857 rtp_receive_statistics_->RegisterRtcpStatisticsCallback(NULL);
niklase@google.com470e71d2011-07-07 08:21:25 +0000858 WEBRTC_TRACE(kTraceMemory, kTraceVoice, VoEId(_instanceId,_channelId),
859 "Channel::~Channel() - dtor");
860
861 if (_outputExternalMedia)
862 {
863 DeRegisterExternalMediaProcessing(kPlaybackPerChannel);
864 }
henrika@webrtc.org944cbeb2014-03-18 10:32:33 +0000865 if (channel_state_.Get().input_external_media)
niklase@google.com470e71d2011-07-07 08:21:25 +0000866 {
867 DeRegisterExternalMediaProcessing(kRecordingPerChannel);
868 }
869 StopSend();
niklase@google.com470e71d2011-07-07 08:21:25 +0000870 StopPlayout();
871
872 {
mflodman@webrtc.org9a065d12012-03-07 08:12:21 +0000873 CriticalSectionScoped cs(&_fileCritSect);
niklase@google.com470e71d2011-07-07 08:21:25 +0000874 if (_inputFilePlayerPtr)
875 {
876 _inputFilePlayerPtr->RegisterModuleFileCallback(NULL);
877 _inputFilePlayerPtr->StopPlayingFile();
878 FilePlayer::DestroyFilePlayer(_inputFilePlayerPtr);
879 _inputFilePlayerPtr = NULL;
880 }
881 if (_outputFilePlayerPtr)
882 {
883 _outputFilePlayerPtr->RegisterModuleFileCallback(NULL);
884 _outputFilePlayerPtr->StopPlayingFile();
885 FilePlayer::DestroyFilePlayer(_outputFilePlayerPtr);
886 _outputFilePlayerPtr = NULL;
887 }
888 if (_outputFileRecorderPtr)
889 {
890 _outputFileRecorderPtr->RegisterModuleFileCallback(NULL);
891 _outputFileRecorderPtr->StopRecording();
892 FileRecorder::DestroyFileRecorder(_outputFileRecorderPtr);
893 _outputFileRecorderPtr = NULL;
894 }
895 }
896
897 // The order to safely shutdown modules in a channel is:
898 // 1. De-register callbacks in modules
899 // 2. De-register modules in process thread
900 // 3. Destroy modules
andrew@webrtc.orgeb524d92013-09-23 23:02:24 +0000901 if (audio_coding_->RegisterTransportCallback(NULL) == -1)
niklase@google.com470e71d2011-07-07 08:21:25 +0000902 {
903 WEBRTC_TRACE(kTraceWarning, kTraceVoice,
904 VoEId(_instanceId,_channelId),
905 "~Channel() failed to de-register transport callback"
906 " (Audio coding module)");
907 }
andrew@webrtc.orgeb524d92013-09-23 23:02:24 +0000908 if (audio_coding_->RegisterVADCallback(NULL) == -1)
niklase@google.com470e71d2011-07-07 08:21:25 +0000909 {
910 WEBRTC_TRACE(kTraceWarning, kTraceVoice,
911 VoEId(_instanceId,_channelId),
912 "~Channel() failed to de-register VAD callback"
913 " (Audio coding module)");
914 }
niklase@google.com470e71d2011-07-07 08:21:25 +0000915 // De-register modules in process thread
tommi@webrtc.org3985f012015-02-27 13:36:34 +0000916 _moduleProcessThreadPtr->DeRegisterModule(_rtpRtcpModule.get());
917
niklase@google.com470e71d2011-07-07 08:21:25 +0000918 // End of modules shutdown
919
920 // Delete other objects
solenberg@webrtc.orgb1f50102014-03-24 10:38:25 +0000921 if (vie_network_) {
922 vie_network_->Release();
923 vie_network_ = NULL;
924 }
niklase@google.com470e71d2011-07-07 08:21:25 +0000925 RtpDump::DestroyRtpDump(&_rtpDumpIn);
926 RtpDump::DestroyRtpDump(&_rtpDumpOut);
niklase@google.com470e71d2011-07-07 08:21:25 +0000927 delete &_callbackCritSect;
niklase@google.com470e71d2011-07-07 08:21:25 +0000928 delete &_fileCritSect;
wu@webrtc.org63420662013-10-17 18:28:55 +0000929 delete &volume_settings_critsect_;
niklase@google.com470e71d2011-07-07 08:21:25 +0000930}
931
pbos@webrtc.org6141e132013-04-09 10:09:10 +0000932int32_t
niklase@google.com470e71d2011-07-07 08:21:25 +0000933Channel::Init()
934{
935 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId,_channelId),
936 "Channel::Init()");
937
henrika@webrtc.org944cbeb2014-03-18 10:32:33 +0000938 channel_state_.Reset();
939
niklase@google.com470e71d2011-07-07 08:21:25 +0000940 // --- Initial sanity
941
942 if ((_engineStatisticsPtr == NULL) ||
943 (_moduleProcessThreadPtr == NULL))
944 {
945 WEBRTC_TRACE(kTraceError, kTraceVoice,
946 VoEId(_instanceId,_channelId),
947 "Channel::Init() must call SetEngineInformation() first");
948 return -1;
949 }
950
951 // --- Add modules to process thread (for periodic schedulation)
952
tommi@webrtc.org3985f012015-02-27 13:36:34 +0000953 _moduleProcessThreadPtr->RegisterModule(_rtpRtcpModule.get());
954
pwestin@webrtc.orgc450a192012-01-04 15:00:12 +0000955 // --- ACM initialization
niklase@google.com470e71d2011-07-07 08:21:25 +0000956
andrew@webrtc.orgeb524d92013-09-23 23:02:24 +0000957 if ((audio_coding_->InitializeReceiver() == -1) ||
niklase@google.com470e71d2011-07-07 08:21:25 +0000958#ifdef WEBRTC_CODEC_AVT
959 // out-of-band Dtmf tones are played out by default
andrew@webrtc.orgeb524d92013-09-23 23:02:24 +0000960 (audio_coding_->SetDtmfPlayoutStatus(true) == -1) ||
niklase@google.com470e71d2011-07-07 08:21:25 +0000961#endif
andrew@webrtc.orgeb524d92013-09-23 23:02:24 +0000962 (audio_coding_->InitializeSender() == -1))
niklase@google.com470e71d2011-07-07 08:21:25 +0000963 {
964 _engineStatisticsPtr->SetLastError(
965 VE_AUDIO_CODING_MODULE_ERROR, kTraceError,
966 "Channel::Init() unable to initialize the ACM - 1");
967 return -1;
968 }
969
970 // --- RTP/RTCP module initialization
971
972 // Ensure that RTCP is enabled by default for the created channel.
973 // Note that, the module will keep generating RTCP until it is explicitly
974 // disabled by the user.
975 // After StopListen (when no sockets exists), RTCP packets will no longer
976 // be transmitted since the Transport object will then be invalid.
wu@webrtc.org822fbd82013-08-15 23:38:54 +0000977 telephone_event_handler_->SetTelephoneEventForwardToDecoder(true);
978 // RTCP is enabled by default.
pbos@webrtc.orgd16e8392014-12-19 13:49:55 +0000979 _rtpRtcpModule->SetRTCPStatus(kRtcpCompound);
980 // --- Register all permanent callbacks
niklase@google.com470e71d2011-07-07 08:21:25 +0000981 const bool fail =
andrew@webrtc.orgeb524d92013-09-23 23:02:24 +0000982 (audio_coding_->RegisterTransportCallback(this) == -1) ||
983 (audio_coding_->RegisterVADCallback(this) == -1);
niklase@google.com470e71d2011-07-07 08:21:25 +0000984
985 if (fail)
986 {
987 _engineStatisticsPtr->SetLastError(
988 VE_CANNOT_INIT_CHANNEL, kTraceError,
989 "Channel::Init() callbacks not registered");
990 return -1;
991 }
992
993 // --- Register all supported codecs to the receiving side of the
994 // RTP/RTCP module
995
996 CodecInst codec;
pbos@webrtc.org6141e132013-04-09 10:09:10 +0000997 const uint8_t nSupportedCodecs = AudioCodingModule::NumberOfCodecs();
niklase@google.com470e71d2011-07-07 08:21:25 +0000998
999 for (int idx = 0; idx < nSupportedCodecs; idx++)
1000 {
1001 // Open up the RTP/RTCP receiver for all supported codecs
andrew@webrtc.orgeb524d92013-09-23 23:02:24 +00001002 if ((audio_coding_->Codec(idx, &codec) == -1) ||
wu@webrtc.org822fbd82013-08-15 23:38:54 +00001003 (rtp_receiver_->RegisterReceivePayload(
1004 codec.plname,
1005 codec.pltype,
1006 codec.plfreq,
1007 codec.channels,
1008 (codec.rate < 0) ? 0 : codec.rate) == -1))
niklase@google.com470e71d2011-07-07 08:21:25 +00001009 {
1010 WEBRTC_TRACE(kTraceWarning, kTraceVoice,
1011 VoEId(_instanceId,_channelId),
1012 "Channel::Init() unable to register %s (%d/%d/%d/%d) "
1013 "to RTP/RTCP receiver",
1014 codec.plname, codec.pltype, codec.plfreq,
1015 codec.channels, codec.rate);
1016 }
1017 else
1018 {
1019 WEBRTC_TRACE(kTraceInfo, kTraceVoice,
1020 VoEId(_instanceId,_channelId),
1021 "Channel::Init() %s (%d/%d/%d/%d) has been added to "
1022 "the RTP/RTCP receiver",
1023 codec.plname, codec.pltype, codec.plfreq,
1024 codec.channels, codec.rate);
1025 }
1026
1027 // Ensure that PCMU is used as default codec on the sending side
tina.legrand@webrtc.org45175852012-06-01 09:27:35 +00001028 if (!STR_CASE_CMP(codec.plname, "PCMU") && (codec.channels == 1))
niklase@google.com470e71d2011-07-07 08:21:25 +00001029 {
1030 SetSendCodec(codec);
1031 }
1032
1033 // Register default PT for outband 'telephone-event'
1034 if (!STR_CASE_CMP(codec.plname, "telephone-event"))
1035 {
pwestin@webrtc.org2853dde2012-05-11 11:08:54 +00001036 if ((_rtpRtcpModule->RegisterSendPayload(codec) == -1) ||
andrew@webrtc.orgeb524d92013-09-23 23:02:24 +00001037 (audio_coding_->RegisterReceiveCodec(codec) == -1))
niklase@google.com470e71d2011-07-07 08:21:25 +00001038 {
1039 WEBRTC_TRACE(kTraceWarning, kTraceVoice,
1040 VoEId(_instanceId,_channelId),
1041 "Channel::Init() failed to register outband "
1042 "'telephone-event' (%d/%d) correctly",
1043 codec.pltype, codec.plfreq);
1044 }
1045 }
1046
1047 if (!STR_CASE_CMP(codec.plname, "CN"))
1048 {
andrew@webrtc.orgeb524d92013-09-23 23:02:24 +00001049 if ((audio_coding_->RegisterSendCodec(codec) == -1) ||
1050 (audio_coding_->RegisterReceiveCodec(codec) == -1) ||
pwestin@webrtc.org2853dde2012-05-11 11:08:54 +00001051 (_rtpRtcpModule->RegisterSendPayload(codec) == -1))
niklase@google.com470e71d2011-07-07 08:21:25 +00001052 {
1053 WEBRTC_TRACE(kTraceWarning, kTraceVoice,
1054 VoEId(_instanceId,_channelId),
1055 "Channel::Init() failed to register CN (%d/%d) "
1056 "correctly - 1",
1057 codec.pltype, codec.plfreq);
1058 }
1059 }
1060#ifdef WEBRTC_CODEC_RED
1061 // Register RED to the receiving side of the ACM.
1062 // We will not receive an OnInitializeDecoder() callback for RED.
1063 if (!STR_CASE_CMP(codec.plname, "RED"))
1064 {
andrew@webrtc.orgeb524d92013-09-23 23:02:24 +00001065 if (audio_coding_->RegisterReceiveCodec(codec) == -1)
niklase@google.com470e71d2011-07-07 08:21:25 +00001066 {
1067 WEBRTC_TRACE(kTraceWarning, kTraceVoice,
1068 VoEId(_instanceId,_channelId),
1069 "Channel::Init() failed to register RED (%d/%d) "
1070 "correctly",
1071 codec.pltype, codec.plfreq);
1072 }
1073 }
1074#endif
1075 }
pwestin@webrtc.org684f0572013-03-13 23:20:57 +00001076
andrew@webrtc.org6c264cc2013-10-04 17:54:09 +00001077 if (rx_audioproc_->noise_suppression()->set_level(kDefaultNsMode) != 0) {
1078 LOG_FERR1(LS_ERROR, noise_suppression()->set_level, kDefaultNsMode);
1079 return -1;
niklase@google.com470e71d2011-07-07 08:21:25 +00001080 }
andrew@webrtc.org6c264cc2013-10-04 17:54:09 +00001081 if (rx_audioproc_->gain_control()->set_mode(kDefaultRxAgcMode) != 0) {
1082 LOG_FERR1(LS_ERROR, gain_control()->set_mode, kDefaultRxAgcMode);
1083 return -1;
niklase@google.com470e71d2011-07-07 08:21:25 +00001084 }
1085
1086 return 0;
1087}
1088
pbos@webrtc.org6141e132013-04-09 10:09:10 +00001089int32_t
niklase@google.com470e71d2011-07-07 08:21:25 +00001090Channel::SetEngineInformation(Statistics& engineStatistics,
1091 OutputMixer& outputMixer,
1092 voe::TransmitMixer& transmitMixer,
1093 ProcessThread& moduleProcessThread,
1094 AudioDeviceModule& audioDeviceModule,
1095 VoiceEngineObserver* voiceEngineObserver,
1096 CriticalSectionWrapper* callbackCritSect)
1097{
1098 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId,_channelId),
1099 "Channel::SetEngineInformation()");
1100 _engineStatisticsPtr = &engineStatistics;
1101 _outputMixerPtr = &outputMixer;
1102 _transmitMixerPtr = &transmitMixer,
1103 _moduleProcessThreadPtr = &moduleProcessThread;
1104 _audioDeviceModulePtr = &audioDeviceModule;
1105 _voiceEngineObserverPtr = voiceEngineObserver;
1106 _callbackCritSectPtr = callbackCritSect;
1107 return 0;
1108}
1109
pbos@webrtc.org6141e132013-04-09 10:09:10 +00001110int32_t
niklase@google.com470e71d2011-07-07 08:21:25 +00001111Channel::UpdateLocalTimeStamp()
1112{
1113
andrew@webrtc.org63a50982012-05-02 23:56:37 +00001114 _timeStamp += _audioFrame.samples_per_channel_;
niklase@google.com470e71d2011-07-07 08:21:25 +00001115 return 0;
1116}
1117
pbos@webrtc.org6141e132013-04-09 10:09:10 +00001118int32_t
niklase@google.com470e71d2011-07-07 08:21:25 +00001119Channel::StartPlayout()
1120{
1121 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId,_channelId),
1122 "Channel::StartPlayout()");
henrika@webrtc.org944cbeb2014-03-18 10:32:33 +00001123 if (channel_state_.Get().playing)
niklase@google.com470e71d2011-07-07 08:21:25 +00001124 {
1125 return 0;
1126 }
roosa@google.com1b60ceb2012-12-12 23:00:29 +00001127
1128 if (!_externalMixing) {
1129 // Add participant as candidates for mixing.
1130 if (_outputMixerPtr->SetMixabilityStatus(*this, true) != 0)
1131 {
1132 _engineStatisticsPtr->SetLastError(
1133 VE_AUDIO_CONF_MIX_MODULE_ERROR, kTraceError,
1134 "StartPlayout() failed to add participant to mixer");
1135 return -1;
1136 }
niklase@google.com470e71d2011-07-07 08:21:25 +00001137 }
1138
henrika@webrtc.org944cbeb2014-03-18 10:32:33 +00001139 channel_state_.SetPlaying(true);
braveyao@webrtc.orgab129902012-06-04 03:26:39 +00001140 if (RegisterFilePlayingToMixer() != 0)
1141 return -1;
1142
niklase@google.com470e71d2011-07-07 08:21:25 +00001143 return 0;
1144}
1145
pbos@webrtc.org6141e132013-04-09 10:09:10 +00001146int32_t
niklase@google.com470e71d2011-07-07 08:21:25 +00001147Channel::StopPlayout()
1148{
1149 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId,_channelId),
1150 "Channel::StopPlayout()");
henrika@webrtc.org944cbeb2014-03-18 10:32:33 +00001151 if (!channel_state_.Get().playing)
niklase@google.com470e71d2011-07-07 08:21:25 +00001152 {
1153 return 0;
1154 }
roosa@google.com1b60ceb2012-12-12 23:00:29 +00001155
1156 if (!_externalMixing) {
1157 // Remove participant as candidates for mixing
1158 if (_outputMixerPtr->SetMixabilityStatus(*this, false) != 0)
1159 {
1160 _engineStatisticsPtr->SetLastError(
1161 VE_AUDIO_CONF_MIX_MODULE_ERROR, kTraceError,
1162 "StopPlayout() failed to remove participant from mixer");
1163 return -1;
1164 }
niklase@google.com470e71d2011-07-07 08:21:25 +00001165 }
1166
henrika@webrtc.org944cbeb2014-03-18 10:32:33 +00001167 channel_state_.SetPlaying(false);
niklase@google.com470e71d2011-07-07 08:21:25 +00001168 _outputAudioLevel.Clear();
1169
1170 return 0;
1171}
1172
pbos@webrtc.org6141e132013-04-09 10:09:10 +00001173int32_t
niklase@google.com470e71d2011-07-07 08:21:25 +00001174Channel::StartSend()
1175{
1176 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId,_channelId),
1177 "Channel::StartSend()");
xians@webrtc.org09e8c472013-07-31 16:30:19 +00001178 // Resume the previous sequence number which was reset by StopSend().
henrika@webrtc.org944cbeb2014-03-18 10:32:33 +00001179 // This needs to be done before |sending| is set to true.
xians@webrtc.org09e8c472013-07-31 16:30:19 +00001180 if (send_sequence_number_)
1181 SetInitSequenceNumber(send_sequence_number_);
1182
henrika@webrtc.org944cbeb2014-03-18 10:32:33 +00001183 if (channel_state_.Get().sending)
niklase@google.com470e71d2011-07-07 08:21:25 +00001184 {
henrika@webrtc.org944cbeb2014-03-18 10:32:33 +00001185 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +00001186 }
henrika@webrtc.org944cbeb2014-03-18 10:32:33 +00001187 channel_state_.SetSending(true);
xians@webrtc.orge07247a2011-11-28 16:31:28 +00001188
pwestin@webrtc.org2853dde2012-05-11 11:08:54 +00001189 if (_rtpRtcpModule->SetSendingStatus(true) != 0)
niklase@google.com470e71d2011-07-07 08:21:25 +00001190 {
1191 _engineStatisticsPtr->SetLastError(
1192 VE_RTP_RTCP_MODULE_ERROR, kTraceError,
1193 "StartSend() RTP/RTCP failed to start sending");
mflodman@webrtc.org9a065d12012-03-07 08:12:21 +00001194 CriticalSectionScoped cs(&_callbackCritSect);
henrika@webrtc.org944cbeb2014-03-18 10:32:33 +00001195 channel_state_.SetSending(false);
niklase@google.com470e71d2011-07-07 08:21:25 +00001196 return -1;
1197 }
xians@webrtc.orge07247a2011-11-28 16:31:28 +00001198
niklase@google.com470e71d2011-07-07 08:21:25 +00001199 return 0;
1200}
1201
pbos@webrtc.org6141e132013-04-09 10:09:10 +00001202int32_t
niklase@google.com470e71d2011-07-07 08:21:25 +00001203Channel::StopSend()
1204{
1205 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId,_channelId),
1206 "Channel::StopSend()");
henrika@webrtc.org944cbeb2014-03-18 10:32:33 +00001207 if (!channel_state_.Get().sending)
niklase@google.com470e71d2011-07-07 08:21:25 +00001208 {
henrika@webrtc.org944cbeb2014-03-18 10:32:33 +00001209 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +00001210 }
henrika@webrtc.org944cbeb2014-03-18 10:32:33 +00001211 channel_state_.SetSending(false);
xians@webrtc.orge07247a2011-11-28 16:31:28 +00001212
xians@webrtc.org09e8c472013-07-31 16:30:19 +00001213 // Store the sequence number to be able to pick up the same sequence for
1214 // the next StartSend(). This is needed for restarting device, otherwise
1215 // it might cause libSRTP to complain about packets being replayed.
1216 // TODO(xians): Remove this workaround after RtpRtcpModule's refactoring
1217 // CL is landed. See issue
1218 // https://code.google.com/p/webrtc/issues/detail?id=2111 .
1219 send_sequence_number_ = _rtpRtcpModule->SequenceNumber();
1220
niklase@google.com470e71d2011-07-07 08:21:25 +00001221 // Reset sending SSRC and sequence number and triggers direct transmission
1222 // of RTCP BYE
pwestin@webrtc.org2853dde2012-05-11 11:08:54 +00001223 if (_rtpRtcpModule->SetSendingStatus(false) == -1 ||
1224 _rtpRtcpModule->ResetSendDataCountersRTP() == -1)
niklase@google.com470e71d2011-07-07 08:21:25 +00001225 {
1226 _engineStatisticsPtr->SetLastError(
1227 VE_RTP_RTCP_MODULE_ERROR, kTraceWarning,
1228 "StartSend() RTP/RTCP failed to stop sending");
1229 }
1230
niklase@google.com470e71d2011-07-07 08:21:25 +00001231 return 0;
1232}
1233
pbos@webrtc.org6141e132013-04-09 10:09:10 +00001234int32_t
niklase@google.com470e71d2011-07-07 08:21:25 +00001235Channel::StartReceiving()
1236{
1237 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId,_channelId),
1238 "Channel::StartReceiving()");
henrika@webrtc.org944cbeb2014-03-18 10:32:33 +00001239 if (channel_state_.Get().receiving)
niklase@google.com470e71d2011-07-07 08:21:25 +00001240 {
1241 return 0;
1242 }
henrika@webrtc.org944cbeb2014-03-18 10:32:33 +00001243 channel_state_.SetReceiving(true);
niklase@google.com470e71d2011-07-07 08:21:25 +00001244 _numberOfDiscardedPackets = 0;
1245 return 0;
1246}
1247
pbos@webrtc.org6141e132013-04-09 10:09:10 +00001248int32_t
niklase@google.com470e71d2011-07-07 08:21:25 +00001249Channel::StopReceiving()
1250{
1251 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId,_channelId),
1252 "Channel::StopReceiving()");
henrika@webrtc.org944cbeb2014-03-18 10:32:33 +00001253 if (!channel_state_.Get().receiving)
niklase@google.com470e71d2011-07-07 08:21:25 +00001254 {
1255 return 0;
1256 }
pwestin@webrtc.org684f0572013-03-13 23:20:57 +00001257
henrika@webrtc.org944cbeb2014-03-18 10:32:33 +00001258 channel_state_.SetReceiving(false);
niklase@google.com470e71d2011-07-07 08:21:25 +00001259 return 0;
1260}
1261
pbos@webrtc.org6141e132013-04-09 10:09:10 +00001262int32_t
niklase@google.com470e71d2011-07-07 08:21:25 +00001263Channel::RegisterVoiceEngineObserver(VoiceEngineObserver& observer)
1264{
1265 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId,_channelId),
1266 "Channel::RegisterVoiceEngineObserver()");
mflodman@webrtc.org9a065d12012-03-07 08:12:21 +00001267 CriticalSectionScoped cs(&_callbackCritSect);
niklase@google.com470e71d2011-07-07 08:21:25 +00001268
1269 if (_voiceEngineObserverPtr)
1270 {
1271 _engineStatisticsPtr->SetLastError(
1272 VE_INVALID_OPERATION, kTraceError,
1273 "RegisterVoiceEngineObserver() observer already enabled");
1274 return -1;
1275 }
1276 _voiceEngineObserverPtr = &observer;
1277 return 0;
1278}
1279
pbos@webrtc.org6141e132013-04-09 10:09:10 +00001280int32_t
niklase@google.com470e71d2011-07-07 08:21:25 +00001281Channel::DeRegisterVoiceEngineObserver()
1282{
1283 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId,_channelId),
1284 "Channel::DeRegisterVoiceEngineObserver()");
mflodman@webrtc.org9a065d12012-03-07 08:12:21 +00001285 CriticalSectionScoped cs(&_callbackCritSect);
niklase@google.com470e71d2011-07-07 08:21:25 +00001286
1287 if (!_voiceEngineObserverPtr)
1288 {
1289 _engineStatisticsPtr->SetLastError(
1290 VE_INVALID_OPERATION, kTraceWarning,
1291 "DeRegisterVoiceEngineObserver() observer already disabled");
1292 return 0;
1293 }
1294 _voiceEngineObserverPtr = NULL;
1295 return 0;
1296}
1297
pbos@webrtc.org6141e132013-04-09 10:09:10 +00001298int32_t
niklase@google.com470e71d2011-07-07 08:21:25 +00001299Channel::GetSendCodec(CodecInst& codec)
1300{
andrew@webrtc.orgeb524d92013-09-23 23:02:24 +00001301 return (audio_coding_->SendCodec(&codec));
niklase@google.com470e71d2011-07-07 08:21:25 +00001302}
1303
pbos@webrtc.org6141e132013-04-09 10:09:10 +00001304int32_t
niklase@google.com470e71d2011-07-07 08:21:25 +00001305Channel::GetRecCodec(CodecInst& codec)
1306{
andrew@webrtc.orgeb524d92013-09-23 23:02:24 +00001307 return (audio_coding_->ReceiveCodec(&codec));
niklase@google.com470e71d2011-07-07 08:21:25 +00001308}
1309
pbos@webrtc.org6141e132013-04-09 10:09:10 +00001310int32_t
niklase@google.com470e71d2011-07-07 08:21:25 +00001311Channel::SetSendCodec(const CodecInst& codec)
1312{
1313 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId,_channelId),
1314 "Channel::SetSendCodec()");
1315
andrew@webrtc.orgeb524d92013-09-23 23:02:24 +00001316 if (audio_coding_->RegisterSendCodec(codec) != 0)
niklase@google.com470e71d2011-07-07 08:21:25 +00001317 {
1318 WEBRTC_TRACE(kTraceError, kTraceVoice, VoEId(_instanceId,_channelId),
1319 "SetSendCodec() failed to register codec to ACM");
1320 return -1;
1321 }
1322
pwestin@webrtc.org2853dde2012-05-11 11:08:54 +00001323 if (_rtpRtcpModule->RegisterSendPayload(codec) != 0)
niklase@google.com470e71d2011-07-07 08:21:25 +00001324 {
pwestin@webrtc.org2853dde2012-05-11 11:08:54 +00001325 _rtpRtcpModule->DeRegisterSendPayload(codec.pltype);
1326 if (_rtpRtcpModule->RegisterSendPayload(codec) != 0)
niklase@google.com470e71d2011-07-07 08:21:25 +00001327 {
1328 WEBRTC_TRACE(
1329 kTraceError, kTraceVoice, VoEId(_instanceId,_channelId),
1330 "SetSendCodec() failed to register codec to"
1331 " RTP/RTCP module");
1332 return -1;
1333 }
1334 }
1335
pwestin@webrtc.org2853dde2012-05-11 11:08:54 +00001336 if (_rtpRtcpModule->SetAudioPacketSize(codec.pacsize) != 0)
niklase@google.com470e71d2011-07-07 08:21:25 +00001337 {
1338 WEBRTC_TRACE(kTraceError, kTraceVoice, VoEId(_instanceId,_channelId),
1339 "SetSendCodec() failed to set audio packet size");
1340 return -1;
1341 }
1342
1343 return 0;
1344}
1345
mflodman@webrtc.org0a7d4ee2015-02-17 12:57:14 +00001346void Channel::OnIncomingFractionLoss(int fraction_lost) {
minyue@webrtc.org74aaf292014-07-16 21:28:26 +00001347 network_predictor_->UpdatePacketLossRate(fraction_lost);
mflodman@webrtc.org0a7d4ee2015-02-17 12:57:14 +00001348 uint8_t average_fraction_loss = network_predictor_->GetLossRate();
1349
minyue@webrtc.orgc1a40a72014-05-28 09:52:06 +00001350 // Normalizes rate to 0 - 100.
mflodman@webrtc.org0a7d4ee2015-02-17 12:57:14 +00001351 if (audio_coding_->SetPacketLossRate(
1352 100 * average_fraction_loss / 255) != 0) {
minyue@webrtc.orgc1a40a72014-05-28 09:52:06 +00001353 assert(false); // This should not happen.
1354 }
1355}
1356
pbos@webrtc.org6141e132013-04-09 10:09:10 +00001357int32_t
niklase@google.com470e71d2011-07-07 08:21:25 +00001358Channel::SetVADStatus(bool enableVAD, ACMVADMode mode, bool disableDTX)
1359{
1360 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId,_channelId),
1361 "Channel::SetVADStatus(mode=%d)", mode);
henrik.lundin@webrtc.org664ccb72015-01-28 14:49:05 +00001362 assert(!(disableDTX && enableVAD)); // disableDTX mode is deprecated.
niklase@google.com470e71d2011-07-07 08:21:25 +00001363 // To disable VAD, DTX must be disabled too
1364 disableDTX = ((enableVAD == false) ? true : disableDTX);
andrew@webrtc.orgeb524d92013-09-23 23:02:24 +00001365 if (audio_coding_->SetVAD(!disableDTX, enableVAD, mode) != 0)
niklase@google.com470e71d2011-07-07 08:21:25 +00001366 {
1367 _engineStatisticsPtr->SetLastError(
1368 VE_AUDIO_CODING_MODULE_ERROR, kTraceError,
1369 "SetVADStatus() failed to set VAD");
1370 return -1;
1371 }
1372 return 0;
1373}
1374
pbos@webrtc.org6141e132013-04-09 10:09:10 +00001375int32_t
niklase@google.com470e71d2011-07-07 08:21:25 +00001376Channel::GetVADStatus(bool& enabledVAD, ACMVADMode& mode, bool& disabledDTX)
1377{
1378 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId,_channelId),
1379 "Channel::GetVADStatus");
andrew@webrtc.orgeb524d92013-09-23 23:02:24 +00001380 if (audio_coding_->VAD(&disabledDTX, &enabledVAD, &mode) != 0)
niklase@google.com470e71d2011-07-07 08:21:25 +00001381 {
1382 _engineStatisticsPtr->SetLastError(
1383 VE_AUDIO_CODING_MODULE_ERROR, kTraceError,
1384 "GetVADStatus() failed to get VAD status");
1385 return -1;
1386 }
1387 disabledDTX = !disabledDTX;
1388 return 0;
1389}
1390
pbos@webrtc.org6141e132013-04-09 10:09:10 +00001391int32_t
niklase@google.com470e71d2011-07-07 08:21:25 +00001392Channel::SetRecPayloadType(const CodecInst& codec)
1393{
1394 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId,_channelId),
1395 "Channel::SetRecPayloadType()");
1396
henrika@webrtc.org944cbeb2014-03-18 10:32:33 +00001397 if (channel_state_.Get().playing)
niklase@google.com470e71d2011-07-07 08:21:25 +00001398 {
1399 _engineStatisticsPtr->SetLastError(
1400 VE_ALREADY_PLAYING, kTraceError,
1401 "SetRecPayloadType() unable to set PT while playing");
1402 return -1;
1403 }
henrika@webrtc.org944cbeb2014-03-18 10:32:33 +00001404 if (channel_state_.Get().receiving)
niklase@google.com470e71d2011-07-07 08:21:25 +00001405 {
1406 _engineStatisticsPtr->SetLastError(
1407 VE_ALREADY_LISTENING, kTraceError,
1408 "SetRecPayloadType() unable to set PT while listening");
1409 return -1;
1410 }
1411
1412 if (codec.pltype == -1)
1413 {
1414 // De-register the selected codec (RTP/RTCP module and ACM)
1415
pbos@webrtc.org6141e132013-04-09 10:09:10 +00001416 int8_t pltype(-1);
niklase@google.com470e71d2011-07-07 08:21:25 +00001417 CodecInst rxCodec = codec;
1418
1419 // Get payload type for the given codec
wu@webrtc.org822fbd82013-08-15 23:38:54 +00001420 rtp_payload_registry_->ReceivePayloadType(
1421 rxCodec.plname,
1422 rxCodec.plfreq,
1423 rxCodec.channels,
1424 (rxCodec.rate < 0) ? 0 : rxCodec.rate,
1425 &pltype);
niklase@google.com470e71d2011-07-07 08:21:25 +00001426 rxCodec.pltype = pltype;
1427
wu@webrtc.org822fbd82013-08-15 23:38:54 +00001428 if (rtp_receiver_->DeRegisterReceivePayload(pltype) != 0)
niklase@google.com470e71d2011-07-07 08:21:25 +00001429 {
1430 _engineStatisticsPtr->SetLastError(
1431 VE_RTP_RTCP_MODULE_ERROR,
1432 kTraceError,
1433 "SetRecPayloadType() RTP/RTCP-module deregistration "
1434 "failed");
1435 return -1;
1436 }
andrew@webrtc.orgeb524d92013-09-23 23:02:24 +00001437 if (audio_coding_->UnregisterReceiveCodec(rxCodec.pltype) != 0)
niklase@google.com470e71d2011-07-07 08:21:25 +00001438 {
1439 _engineStatisticsPtr->SetLastError(
1440 VE_AUDIO_CODING_MODULE_ERROR, kTraceError,
1441 "SetRecPayloadType() ACM deregistration failed - 1");
1442 return -1;
1443 }
1444 return 0;
1445 }
1446
wu@webrtc.org822fbd82013-08-15 23:38:54 +00001447 if (rtp_receiver_->RegisterReceivePayload(
1448 codec.plname,
1449 codec.pltype,
1450 codec.plfreq,
1451 codec.channels,
1452 (codec.rate < 0) ? 0 : codec.rate) != 0)
niklase@google.com470e71d2011-07-07 08:21:25 +00001453 {
1454 // First attempt to register failed => de-register and try again
wu@webrtc.org822fbd82013-08-15 23:38:54 +00001455 rtp_receiver_->DeRegisterReceivePayload(codec.pltype);
1456 if (rtp_receiver_->RegisterReceivePayload(
1457 codec.plname,
1458 codec.pltype,
1459 codec.plfreq,
1460 codec.channels,
1461 (codec.rate < 0) ? 0 : codec.rate) != 0)
niklase@google.com470e71d2011-07-07 08:21:25 +00001462 {
1463 _engineStatisticsPtr->SetLastError(
1464 VE_RTP_RTCP_MODULE_ERROR, kTraceError,
1465 "SetRecPayloadType() RTP/RTCP-module registration failed");
1466 return -1;
1467 }
1468 }
andrew@webrtc.orgeb524d92013-09-23 23:02:24 +00001469 if (audio_coding_->RegisterReceiveCodec(codec) != 0)
niklase@google.com470e71d2011-07-07 08:21:25 +00001470 {
andrew@webrtc.orgeb524d92013-09-23 23:02:24 +00001471 audio_coding_->UnregisterReceiveCodec(codec.pltype);
1472 if (audio_coding_->RegisterReceiveCodec(codec) != 0)
niklase@google.com470e71d2011-07-07 08:21:25 +00001473 {
1474 _engineStatisticsPtr->SetLastError(
1475 VE_AUDIO_CODING_MODULE_ERROR, kTraceError,
1476 "SetRecPayloadType() ACM registration failed - 1");
1477 return -1;
1478 }
1479 }
1480 return 0;
1481}
1482
pbos@webrtc.org6141e132013-04-09 10:09:10 +00001483int32_t
niklase@google.com470e71d2011-07-07 08:21:25 +00001484Channel::GetRecPayloadType(CodecInst& codec)
1485{
1486 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId,_channelId),
1487 "Channel::GetRecPayloadType()");
pbos@webrtc.org6141e132013-04-09 10:09:10 +00001488 int8_t payloadType(-1);
wu@webrtc.org822fbd82013-08-15 23:38:54 +00001489 if (rtp_payload_registry_->ReceivePayloadType(
1490 codec.plname,
1491 codec.plfreq,
1492 codec.channels,
1493 (codec.rate < 0) ? 0 : codec.rate,
1494 &payloadType) != 0)
niklase@google.com470e71d2011-07-07 08:21:25 +00001495 {
1496 _engineStatisticsPtr->SetLastError(
henrika@webrtc.org37198002012-06-18 11:00:12 +00001497 VE_RTP_RTCP_MODULE_ERROR, kTraceWarning,
niklase@google.com470e71d2011-07-07 08:21:25 +00001498 "GetRecPayloadType() failed to retrieve RX payload type");
1499 return -1;
1500 }
1501 codec.pltype = payloadType;
1502 WEBRTC_TRACE(kTraceStateInfo, kTraceVoice, VoEId(_instanceId,_channelId),
pkasting@chromium.orgd3245462015-02-23 21:28:22 +00001503 "Channel::GetRecPayloadType() => pltype=%d", codec.pltype);
niklase@google.com470e71d2011-07-07 08:21:25 +00001504 return 0;
1505}
1506
pbos@webrtc.org6141e132013-04-09 10:09:10 +00001507int32_t
niklase@google.com470e71d2011-07-07 08:21:25 +00001508Channel::SetSendCNPayloadType(int type, PayloadFrequencies frequency)
1509{
1510 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId,_channelId),
1511 "Channel::SetSendCNPayloadType()");
1512
1513 CodecInst codec;
pbos@webrtc.org6141e132013-04-09 10:09:10 +00001514 int32_t samplingFreqHz(-1);
tina.legrand@webrtc.org45175852012-06-01 09:27:35 +00001515 const int kMono = 1;
niklase@google.com470e71d2011-07-07 08:21:25 +00001516 if (frequency == kFreq32000Hz)
1517 samplingFreqHz = 32000;
1518 else if (frequency == kFreq16000Hz)
1519 samplingFreqHz = 16000;
1520
andrew@webrtc.orgeb524d92013-09-23 23:02:24 +00001521 if (audio_coding_->Codec("CN", &codec, samplingFreqHz, kMono) == -1)
niklase@google.com470e71d2011-07-07 08:21:25 +00001522 {
1523 _engineStatisticsPtr->SetLastError(
1524 VE_AUDIO_CODING_MODULE_ERROR, kTraceError,
1525 "SetSendCNPayloadType() failed to retrieve default CN codec "
1526 "settings");
1527 return -1;
1528 }
1529
1530 // Modify the payload type (must be set to dynamic range)
1531 codec.pltype = type;
1532
andrew@webrtc.orgeb524d92013-09-23 23:02:24 +00001533 if (audio_coding_->RegisterSendCodec(codec) != 0)
niklase@google.com470e71d2011-07-07 08:21:25 +00001534 {
1535 _engineStatisticsPtr->SetLastError(
1536 VE_AUDIO_CODING_MODULE_ERROR, kTraceError,
1537 "SetSendCNPayloadType() failed to register CN to ACM");
1538 return -1;
1539 }
1540
pwestin@webrtc.org2853dde2012-05-11 11:08:54 +00001541 if (_rtpRtcpModule->RegisterSendPayload(codec) != 0)
niklase@google.com470e71d2011-07-07 08:21:25 +00001542 {
pwestin@webrtc.org2853dde2012-05-11 11:08:54 +00001543 _rtpRtcpModule->DeRegisterSendPayload(codec.pltype);
1544 if (_rtpRtcpModule->RegisterSendPayload(codec) != 0)
niklase@google.com470e71d2011-07-07 08:21:25 +00001545 {
1546 _engineStatisticsPtr->SetLastError(
1547 VE_RTP_RTCP_MODULE_ERROR, kTraceError,
1548 "SetSendCNPayloadType() failed to register CN to RTP/RTCP "
1549 "module");
1550 return -1;
1551 }
1552 }
1553 return 0;
1554}
1555
minyue@webrtc.orgadee8f92014-09-03 12:28:06 +00001556int Channel::SetOpusMaxPlaybackRate(int frequency_hz) {
minyue@webrtc.org6aac93b2014-08-12 08:13:33 +00001557 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId, _channelId),
minyue@webrtc.orgadee8f92014-09-03 12:28:06 +00001558 "Channel::SetOpusMaxPlaybackRate()");
minyue@webrtc.org6aac93b2014-08-12 08:13:33 +00001559
minyue@webrtc.orgadee8f92014-09-03 12:28:06 +00001560 if (audio_coding_->SetOpusMaxPlaybackRate(frequency_hz) != 0) {
minyue@webrtc.org6aac93b2014-08-12 08:13:33 +00001561 _engineStatisticsPtr->SetLastError(
1562 VE_AUDIO_CODING_MODULE_ERROR, kTraceError,
minyue@webrtc.orgadee8f92014-09-03 12:28:06 +00001563 "SetOpusMaxPlaybackRate() failed to set maximum playback rate");
minyue@webrtc.org6aac93b2014-08-12 08:13:33 +00001564 return -1;
1565 }
1566 return 0;
1567}
1568
pbos@webrtc.org6141e132013-04-09 10:09:10 +00001569int32_t Channel::RegisterExternalTransport(Transport& transport)
niklase@google.com470e71d2011-07-07 08:21:25 +00001570{
1571 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId, _channelId),
1572 "Channel::RegisterExternalTransport()");
1573
mflodman@webrtc.org9a065d12012-03-07 08:12:21 +00001574 CriticalSectionScoped cs(&_callbackCritSect);
niklase@google.com470e71d2011-07-07 08:21:25 +00001575
niklase@google.com470e71d2011-07-07 08:21:25 +00001576 if (_externalTransport)
1577 {
1578 _engineStatisticsPtr->SetLastError(VE_INVALID_OPERATION,
1579 kTraceError,
1580 "RegisterExternalTransport() external transport already enabled");
1581 return -1;
1582 }
1583 _externalTransport = true;
1584 _transportPtr = &transport;
1585 return 0;
1586}
1587
pbos@webrtc.org6141e132013-04-09 10:09:10 +00001588int32_t
niklase@google.com470e71d2011-07-07 08:21:25 +00001589Channel::DeRegisterExternalTransport()
1590{
1591 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId,_channelId),
1592 "Channel::DeRegisterExternalTransport()");
1593
mflodman@webrtc.org9a065d12012-03-07 08:12:21 +00001594 CriticalSectionScoped cs(&_callbackCritSect);
xians@webrtc.org83661f52011-11-25 10:58:15 +00001595
niklase@google.com470e71d2011-07-07 08:21:25 +00001596 if (!_transportPtr)
1597 {
1598 _engineStatisticsPtr->SetLastError(
1599 VE_INVALID_OPERATION, kTraceWarning,
1600 "DeRegisterExternalTransport() external transport already "
1601 "disabled");
1602 return 0;
1603 }
1604 _externalTransport = false;
niklase@google.com470e71d2011-07-07 08:21:25 +00001605 _transportPtr = NULL;
1606 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId,_channelId),
1607 "DeRegisterExternalTransport() all transport is disabled");
niklase@google.com470e71d2011-07-07 08:21:25 +00001608 return 0;
1609}
1610
pkasting@chromium.org4591fbd2014-11-20 22:28:14 +00001611int32_t Channel::ReceivedRTPPacket(const int8_t* data, size_t length,
solenberg@webrtc.orgb1f50102014-03-24 10:38:25 +00001612 const PacketTime& packet_time) {
pwestin@webrtc.org0c459572013-04-03 15:43:57 +00001613 WEBRTC_TRACE(kTraceStream, kTraceVoice, VoEId(_instanceId,_channelId),
1614 "Channel::ReceivedRTPPacket()");
1615
1616 // Store playout timestamp for the received RTP packet
pwestin@webrtc.org1de01352013-04-11 20:23:35 +00001617 UpdatePlayoutTimestamp(false);
pwestin@webrtc.org0c459572013-04-03 15:43:57 +00001618
1619 // Dump the RTP packet to a file (if RTP dump is enabled).
pbos@webrtc.org6141e132013-04-09 10:09:10 +00001620 if (_rtpDumpIn.DumpPacket((const uint8_t*)data,
1621 (uint16_t)length) == -1) {
pwestin@webrtc.org0c459572013-04-03 15:43:57 +00001622 WEBRTC_TRACE(kTraceWarning, kTraceVoice,
1623 VoEId(_instanceId,_channelId),
1624 "Channel::SendPacket() RTP dump to input file failed");
1625 }
stefan@webrtc.org7bb8f022013-09-06 13:40:11 +00001626 const uint8_t* received_packet = reinterpret_cast<const uint8_t*>(data);
stefan@webrtc.orga5cb98c2013-05-29 12:12:51 +00001627 RTPHeader header;
stefan@webrtc.org7bb8f022013-09-06 13:40:11 +00001628 if (!rtp_header_parser_->Parse(received_packet, length, &header)) {
1629 WEBRTC_TRACE(webrtc::kTraceDebug, webrtc::kTraceVoice, _channelId,
1630 "Incoming packet: invalid RTP header");
stefan@webrtc.orga5cb98c2013-05-29 12:12:51 +00001631 return -1;
1632 }
wu@webrtc.org822fbd82013-08-15 23:38:54 +00001633 header.payload_type_frequency =
1634 rtp_payload_registry_->GetPayloadTypeFrequency(header.payloadType);
stefan@webrtc.org7bb8f022013-09-06 13:40:11 +00001635 if (header.payload_type_frequency < 0)
wu@webrtc.org822fbd82013-08-15 23:38:54 +00001636 return -1;
stefan@webrtc.org48df3812013-11-08 15:18:52 +00001637 bool in_order = IsPacketInOrder(header);
stefan@webrtc.org7bb8f022013-09-06 13:40:11 +00001638 rtp_receive_statistics_->IncomingPacket(header, length,
stefan@webrtc.org48df3812013-11-08 15:18:52 +00001639 IsPacketRetransmitted(header, in_order));
stefan@webrtc.org7bb8f022013-09-06 13:40:11 +00001640 rtp_payload_registry_->SetIncomingPayloadType(header);
solenberg@webrtc.orgb1f50102014-03-24 10:38:25 +00001641
1642 // Forward any packets to ViE bandwidth estimator, if enabled.
1643 {
1644 CriticalSectionScoped cs(&_callbackCritSect);
1645 if (vie_network_) {
1646 int64_t arrival_time_ms;
1647 if (packet_time.timestamp != -1) {
1648 arrival_time_ms = (packet_time.timestamp + 500) / 1000;
1649 } else {
1650 arrival_time_ms = TickTime::MillisecondTimestamp();
1651 }
pkasting@chromium.org4591fbd2014-11-20 22:28:14 +00001652 size_t payload_length = length - header.headerLength;
solenberg@webrtc.orgb1f50102014-03-24 10:38:25 +00001653 vie_network_->ReceivedBWEPacket(video_channel_, arrival_time_ms,
1654 payload_length, header);
1655 }
1656 }
1657
stefan@webrtc.org48df3812013-11-08 15:18:52 +00001658 return ReceivePacket(received_packet, length, header, in_order) ? 0 : -1;
stefan@webrtc.org7bb8f022013-09-06 13:40:11 +00001659}
1660
1661bool Channel::ReceivePacket(const uint8_t* packet,
pkasting@chromium.org4591fbd2014-11-20 22:28:14 +00001662 size_t packet_length,
stefan@webrtc.org7bb8f022013-09-06 13:40:11 +00001663 const RTPHeader& header,
1664 bool in_order) {
minyue@webrtc.org456f0142015-01-23 11:58:42 +00001665 if (rtp_payload_registry_->IsRtx(header)) {
1666 return HandleRtxPacket(packet, packet_length, header);
wu@webrtc.org822fbd82013-08-15 23:38:54 +00001667 }
stefan@webrtc.org7bb8f022013-09-06 13:40:11 +00001668 const uint8_t* payload = packet + header.headerLength;
pkasting@chromium.org4591fbd2014-11-20 22:28:14 +00001669 assert(packet_length >= header.headerLength);
1670 size_t payload_length = packet_length - header.headerLength;
wu@webrtc.org822fbd82013-08-15 23:38:54 +00001671 PayloadUnion payload_specific;
1672 if (!rtp_payload_registry_->GetPayloadSpecifics(header.payloadType,
stefan@webrtc.org7bb8f022013-09-06 13:40:11 +00001673 &payload_specific)) {
1674 return false;
wu@webrtc.org822fbd82013-08-15 23:38:54 +00001675 }
stefan@webrtc.org7bb8f022013-09-06 13:40:11 +00001676 return rtp_receiver_->IncomingRtpPacket(header, payload, payload_length,
1677 payload_specific, in_order);
1678}
1679
minyue@webrtc.org456f0142015-01-23 11:58:42 +00001680bool Channel::HandleRtxPacket(const uint8_t* packet,
1681 size_t packet_length,
1682 const RTPHeader& header) {
stefan@webrtc.org7bb8f022013-09-06 13:40:11 +00001683 if (!rtp_payload_registry_->IsRtx(header))
1684 return false;
1685
1686 // Remove the RTX header and parse the original RTP header.
1687 if (packet_length < header.headerLength)
1688 return false;
1689 if (packet_length > kVoiceEngineMaxIpPacketSizeBytes)
1690 return false;
1691 if (restored_packet_in_use_) {
1692 WEBRTC_TRACE(webrtc::kTraceDebug, webrtc::kTraceVoice, _channelId,
1693 "Multiple RTX headers detected, dropping packet");
1694 return false;
pwestin@webrtc.org0c459572013-04-03 15:43:57 +00001695 }
stefan@webrtc.org7bb8f022013-09-06 13:40:11 +00001696 uint8_t* restored_packet_ptr = restored_packet_;
1697 if (!rtp_payload_registry_->RestoreOriginalPacket(
1698 &restored_packet_ptr, packet, &packet_length, rtp_receiver_->SSRC(),
1699 header)) {
1700 WEBRTC_TRACE(webrtc::kTraceDebug, webrtc::kTraceVoice, _channelId,
1701 "Incoming RTX packet: invalid RTP header");
1702 return false;
1703 }
1704 restored_packet_in_use_ = true;
1705 bool ret = OnRecoveredPacket(restored_packet_ptr, packet_length);
1706 restored_packet_in_use_ = false;
1707 return ret;
1708}
1709
1710bool Channel::IsPacketInOrder(const RTPHeader& header) const {
1711 StreamStatistician* statistician =
1712 rtp_receive_statistics_->GetStatistician(header.ssrc);
1713 if (!statistician)
1714 return false;
1715 return statistician->IsPacketInOrder(header.sequenceNumber);
niklase@google.com470e71d2011-07-07 08:21:25 +00001716}
1717
stefan@webrtc.org48df3812013-11-08 15:18:52 +00001718bool Channel::IsPacketRetransmitted(const RTPHeader& header,
1719 bool in_order) const {
stefan@webrtc.org7bb8f022013-09-06 13:40:11 +00001720 // Retransmissions are handled separately if RTX is enabled.
1721 if (rtp_payload_registry_->RtxEnabled())
1722 return false;
1723 StreamStatistician* statistician =
1724 rtp_receive_statistics_->GetStatistician(header.ssrc);
1725 if (!statistician)
1726 return false;
1727 // Check if this is a retransmission.
pkasting@chromium.org16825b12015-01-12 21:51:21 +00001728 int64_t min_rtt = 0;
stefan@webrtc.org7bb8f022013-09-06 13:40:11 +00001729 _rtpRtcpModule->RTT(rtp_receiver_->SSRC(), NULL, NULL, &min_rtt, NULL);
stefan@webrtc.org48df3812013-11-08 15:18:52 +00001730 return !in_order &&
stefan@webrtc.org7bb8f022013-09-06 13:40:11 +00001731 statistician->IsRetransmitOfOldPacket(header, min_rtt);
wu@webrtc.org822fbd82013-08-15 23:38:54 +00001732}
1733
pkasting@chromium.org4591fbd2014-11-20 22:28:14 +00001734int32_t Channel::ReceivedRTCPPacket(const int8_t* data, size_t length) {
pwestin@webrtc.org0c459572013-04-03 15:43:57 +00001735 WEBRTC_TRACE(kTraceStream, kTraceVoice, VoEId(_instanceId,_channelId),
1736 "Channel::ReceivedRTCPPacket()");
1737 // Store playout timestamp for the received RTCP packet
pwestin@webrtc.org1de01352013-04-11 20:23:35 +00001738 UpdatePlayoutTimestamp(true);
pwestin@webrtc.org0c459572013-04-03 15:43:57 +00001739
1740 // Dump the RTCP packet to a file (if RTP dump is enabled).
pkasting@chromium.org4591fbd2014-11-20 22:28:14 +00001741 if (_rtpDumpIn.DumpPacket((const uint8_t*)data, length) == -1) {
pwestin@webrtc.org0c459572013-04-03 15:43:57 +00001742 WEBRTC_TRACE(kTraceWarning, kTraceVoice,
1743 VoEId(_instanceId,_channelId),
1744 "Channel::SendPacket() RTCP dump to input file failed");
1745 }
1746
1747 // Deliver RTCP packet to RTP/RTCP module for parsing
pkasting@chromium.org4591fbd2014-11-20 22:28:14 +00001748 if (_rtpRtcpModule->IncomingRtcpPacket((const uint8_t*)data, length) == -1) {
pwestin@webrtc.org0c459572013-04-03 15:43:57 +00001749 _engineStatisticsPtr->SetLastError(
1750 VE_SOCKET_TRANSPORT_MODULE_ERROR, kTraceWarning,
1751 "Channel::IncomingRTPPacket() RTCP packet is invalid");
1752 }
wu@webrtc.org82c4b852014-05-20 22:55:01 +00001753
stefan@webrtc.org8e24d872014-09-02 18:58:24 +00001754 {
1755 CriticalSectionScoped lock(ts_stats_lock_.get());
pkasting@chromium.org16825b12015-01-12 21:51:21 +00001756 int64_t rtt = GetRTT();
minyue@webrtc.org2c0cdbc2014-10-09 10:52:43 +00001757 if (rtt == 0) {
1758 // Waiting for valid RTT.
1759 return 0;
1760 }
1761 uint32_t ntp_secs = 0;
1762 uint32_t ntp_frac = 0;
1763 uint32_t rtp_timestamp = 0;
1764 if (0 != _rtpRtcpModule->RemoteNTP(&ntp_secs, &ntp_frac, NULL, NULL,
1765 &rtp_timestamp)) {
1766 // Waiting for RTCP.
1767 return 0;
1768 }
1769 ntp_estimator_.UpdateRtcpTimestamp(rtt, ntp_secs, ntp_frac, rtp_timestamp);
stefan@webrtc.org8e24d872014-09-02 18:58:24 +00001770 }
pwestin@webrtc.org0c459572013-04-03 15:43:57 +00001771 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +00001772}
1773
niklase@google.com470e71d2011-07-07 08:21:25 +00001774int Channel::StartPlayingFileLocally(const char* fileName,
pbos@webrtc.org92135212013-05-14 08:31:39 +00001775 bool loop,
1776 FileFormats format,
1777 int startPosition,
1778 float volumeScaling,
1779 int stopPosition,
niklase@google.com470e71d2011-07-07 08:21:25 +00001780 const CodecInst* codecInst)
1781{
1782 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId,_channelId),
1783 "Channel::StartPlayingFileLocally(fileNameUTF8[]=%s, loop=%d,"
1784 " format=%d, volumeScaling=%5.3f, startPosition=%d, "
1785 "stopPosition=%d)", fileName, loop, format, volumeScaling,
1786 startPosition, stopPosition);
1787
henrika@webrtc.org944cbeb2014-03-18 10:32:33 +00001788 if (channel_state_.Get().output_file_playing)
niklase@google.com470e71d2011-07-07 08:21:25 +00001789 {
1790 _engineStatisticsPtr->SetLastError(
1791 VE_ALREADY_PLAYING, kTraceError,
1792 "StartPlayingFileLocally() is already playing");
1793 return -1;
1794 }
1795
niklase@google.com470e71d2011-07-07 08:21:25 +00001796 {
mflodman@webrtc.org9a065d12012-03-07 08:12:21 +00001797 CriticalSectionScoped cs(&_fileCritSect);
henrike@webrtc.orgb37c6282011-10-31 23:53:04 +00001798
1799 if (_outputFilePlayerPtr)
1800 {
1801 _outputFilePlayerPtr->RegisterModuleFileCallback(NULL);
1802 FilePlayer::DestroyFilePlayer(_outputFilePlayerPtr);
1803 _outputFilePlayerPtr = NULL;
1804 }
1805
1806 _outputFilePlayerPtr = FilePlayer::CreateFilePlayer(
1807 _outputFilePlayerId, (const FileFormats)format);
1808
1809 if (_outputFilePlayerPtr == NULL)
1810 {
1811 _engineStatisticsPtr->SetLastError(
1812 VE_INVALID_ARGUMENT, kTraceError,
henrike@webrtc.org31d30702011-11-18 19:59:32 +00001813 "StartPlayingFileLocally() filePlayer format is not correct");
henrike@webrtc.orgb37c6282011-10-31 23:53:04 +00001814 return -1;
1815 }
1816
pbos@webrtc.org6141e132013-04-09 10:09:10 +00001817 const uint32_t notificationTime(0);
henrike@webrtc.orgb37c6282011-10-31 23:53:04 +00001818
1819 if (_outputFilePlayerPtr->StartPlayingFile(
1820 fileName,
1821 loop,
1822 startPosition,
1823 volumeScaling,
1824 notificationTime,
1825 stopPosition,
1826 (const CodecInst*)codecInst) != 0)
1827 {
1828 _engineStatisticsPtr->SetLastError(
1829 VE_BAD_FILE, kTraceError,
1830 "StartPlayingFile() failed to start file playout");
1831 _outputFilePlayerPtr->StopPlayingFile();
1832 FilePlayer::DestroyFilePlayer(_outputFilePlayerPtr);
1833 _outputFilePlayerPtr = NULL;
1834 return -1;
1835 }
1836 _outputFilePlayerPtr->RegisterModuleFileCallback(this);
henrika@webrtc.org944cbeb2014-03-18 10:32:33 +00001837 channel_state_.SetOutputFilePlaying(true);
niklase@google.com470e71d2011-07-07 08:21:25 +00001838 }
braveyao@webrtc.orgab129902012-06-04 03:26:39 +00001839
1840 if (RegisterFilePlayingToMixer() != 0)
henrike@webrtc.org066f9e52011-10-28 23:15:47 +00001841 return -1;
niklase@google.com470e71d2011-07-07 08:21:25 +00001842
1843 return 0;
1844}
1845
1846int Channel::StartPlayingFileLocally(InStream* stream,
pbos@webrtc.org92135212013-05-14 08:31:39 +00001847 FileFormats format,
1848 int startPosition,
1849 float volumeScaling,
1850 int stopPosition,
niklase@google.com470e71d2011-07-07 08:21:25 +00001851 const CodecInst* codecInst)
1852{
1853 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId,_channelId),
1854 "Channel::StartPlayingFileLocally(format=%d,"
1855 " volumeScaling=%5.3f, startPosition=%d, stopPosition=%d)",
1856 format, volumeScaling, startPosition, stopPosition);
1857
1858 if(stream == NULL)
1859 {
1860 _engineStatisticsPtr->SetLastError(
1861 VE_BAD_FILE, kTraceError,
1862 "StartPlayingFileLocally() NULL as input stream");
1863 return -1;
1864 }
1865
1866
henrika@webrtc.org944cbeb2014-03-18 10:32:33 +00001867 if (channel_state_.Get().output_file_playing)
niklase@google.com470e71d2011-07-07 08:21:25 +00001868 {
1869 _engineStatisticsPtr->SetLastError(
1870 VE_ALREADY_PLAYING, kTraceError,
1871 "StartPlayingFileLocally() is already playing");
1872 return -1;
1873 }
1874
niklase@google.com470e71d2011-07-07 08:21:25 +00001875 {
mflodman@webrtc.org9a065d12012-03-07 08:12:21 +00001876 CriticalSectionScoped cs(&_fileCritSect);
henrike@webrtc.orgb37c6282011-10-31 23:53:04 +00001877
1878 // Destroy the old instance
1879 if (_outputFilePlayerPtr)
1880 {
1881 _outputFilePlayerPtr->RegisterModuleFileCallback(NULL);
1882 FilePlayer::DestroyFilePlayer(_outputFilePlayerPtr);
1883 _outputFilePlayerPtr = NULL;
1884 }
1885
1886 // Create the instance
1887 _outputFilePlayerPtr = FilePlayer::CreateFilePlayer(
1888 _outputFilePlayerId,
1889 (const FileFormats)format);
1890
1891 if (_outputFilePlayerPtr == NULL)
1892 {
1893 _engineStatisticsPtr->SetLastError(
1894 VE_INVALID_ARGUMENT, kTraceError,
1895 "StartPlayingFileLocally() filePlayer format isnot correct");
1896 return -1;
1897 }
1898
pbos@webrtc.org6141e132013-04-09 10:09:10 +00001899 const uint32_t notificationTime(0);
henrike@webrtc.orgb37c6282011-10-31 23:53:04 +00001900
1901 if (_outputFilePlayerPtr->StartPlayingFile(*stream, startPosition,
1902 volumeScaling,
1903 notificationTime,
1904 stopPosition, codecInst) != 0)
1905 {
1906 _engineStatisticsPtr->SetLastError(VE_BAD_FILE, kTraceError,
1907 "StartPlayingFile() failed to "
1908 "start file playout");
1909 _outputFilePlayerPtr->StopPlayingFile();
1910 FilePlayer::DestroyFilePlayer(_outputFilePlayerPtr);
1911 _outputFilePlayerPtr = NULL;
1912 return -1;
1913 }
1914 _outputFilePlayerPtr->RegisterModuleFileCallback(this);
henrika@webrtc.org944cbeb2014-03-18 10:32:33 +00001915 channel_state_.SetOutputFilePlaying(true);
niklase@google.com470e71d2011-07-07 08:21:25 +00001916 }
braveyao@webrtc.orgab129902012-06-04 03:26:39 +00001917
1918 if (RegisterFilePlayingToMixer() != 0)
henrike@webrtc.org066f9e52011-10-28 23:15:47 +00001919 return -1;
niklase@google.com470e71d2011-07-07 08:21:25 +00001920
niklase@google.com470e71d2011-07-07 08:21:25 +00001921 return 0;
1922}
1923
1924int Channel::StopPlayingFileLocally()
1925{
1926 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId,_channelId),
1927 "Channel::StopPlayingFileLocally()");
1928
henrika@webrtc.org944cbeb2014-03-18 10:32:33 +00001929 if (!channel_state_.Get().output_file_playing)
niklase@google.com470e71d2011-07-07 08:21:25 +00001930 {
1931 _engineStatisticsPtr->SetLastError(
1932 VE_INVALID_OPERATION, kTraceWarning,
1933 "StopPlayingFileLocally() isnot playing");
1934 return 0;
1935 }
1936
niklase@google.com470e71d2011-07-07 08:21:25 +00001937 {
mflodman@webrtc.org9a065d12012-03-07 08:12:21 +00001938 CriticalSectionScoped cs(&_fileCritSect);
henrike@webrtc.orgb37c6282011-10-31 23:53:04 +00001939
1940 if (_outputFilePlayerPtr->StopPlayingFile() != 0)
1941 {
1942 _engineStatisticsPtr->SetLastError(
1943 VE_STOP_RECORDING_FAILED, kTraceError,
1944 "StopPlayingFile() could not stop playing");
1945 return -1;
1946 }
1947 _outputFilePlayerPtr->RegisterModuleFileCallback(NULL);
1948 FilePlayer::DestroyFilePlayer(_outputFilePlayerPtr);
1949 _outputFilePlayerPtr = NULL;
henrika@webrtc.org944cbeb2014-03-18 10:32:33 +00001950 channel_state_.SetOutputFilePlaying(false);
niklase@google.com470e71d2011-07-07 08:21:25 +00001951 }
henrike@webrtc.orgb37c6282011-10-31 23:53:04 +00001952 // _fileCritSect cannot be taken while calling
1953 // SetAnonymousMixibilityStatus. Refer to comments in
1954 // StartPlayingFileLocally(const char* ...) for more details.
henrike@webrtc.org066f9e52011-10-28 23:15:47 +00001955 if (_outputMixerPtr->SetAnonymousMixabilityStatus(*this, false) != 0)
1956 {
1957 _engineStatisticsPtr->SetLastError(
1958 VE_AUDIO_CONF_MIX_MODULE_ERROR, kTraceError,
henrike@webrtc.orgb37c6282011-10-31 23:53:04 +00001959 "StopPlayingFile() failed to stop participant from playing as"
1960 "file in the mixer");
henrike@webrtc.org066f9e52011-10-28 23:15:47 +00001961 return -1;
1962 }
niklase@google.com470e71d2011-07-07 08:21:25 +00001963
1964 return 0;
1965}
1966
1967int Channel::IsPlayingFileLocally() const
1968{
1969 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId,_channelId),
1970 "Channel::IsPlayingFileLocally()");
1971
henrika@webrtc.org944cbeb2014-03-18 10:32:33 +00001972 return channel_state_.Get().output_file_playing;
niklase@google.com470e71d2011-07-07 08:21:25 +00001973}
1974
braveyao@webrtc.orgab129902012-06-04 03:26:39 +00001975int Channel::RegisterFilePlayingToMixer()
1976{
1977 // Return success for not registering for file playing to mixer if:
1978 // 1. playing file before playout is started on that channel.
1979 // 2. starting playout without file playing on that channel.
henrika@webrtc.org944cbeb2014-03-18 10:32:33 +00001980 if (!channel_state_.Get().playing ||
1981 !channel_state_.Get().output_file_playing)
braveyao@webrtc.orgab129902012-06-04 03:26:39 +00001982 {
1983 return 0;
1984 }
1985
1986 // |_fileCritSect| cannot be taken while calling
1987 // SetAnonymousMixabilityStatus() since as soon as the participant is added
1988 // frames can be pulled by the mixer. Since the frames are generated from
1989 // the file, _fileCritSect will be taken. This would result in a deadlock.
1990 if (_outputMixerPtr->SetAnonymousMixabilityStatus(*this, true) != 0)
1991 {
henrika@webrtc.org944cbeb2014-03-18 10:32:33 +00001992 channel_state_.SetOutputFilePlaying(false);
braveyao@webrtc.orgab129902012-06-04 03:26:39 +00001993 CriticalSectionScoped cs(&_fileCritSect);
braveyao@webrtc.orgab129902012-06-04 03:26:39 +00001994 _engineStatisticsPtr->SetLastError(
1995 VE_AUDIO_CONF_MIX_MODULE_ERROR, kTraceError,
1996 "StartPlayingFile() failed to add participant as file to mixer");
1997 _outputFilePlayerPtr->StopPlayingFile();
1998 FilePlayer::DestroyFilePlayer(_outputFilePlayerPtr);
1999 _outputFilePlayerPtr = NULL;
2000 return -1;
2001 }
2002
2003 return 0;
2004}
2005
niklase@google.com470e71d2011-07-07 08:21:25 +00002006int Channel::StartPlayingFileAsMicrophone(const char* fileName,
pbos@webrtc.org92135212013-05-14 08:31:39 +00002007 bool loop,
2008 FileFormats format,
2009 int startPosition,
2010 float volumeScaling,
2011 int stopPosition,
niklase@google.com470e71d2011-07-07 08:21:25 +00002012 const CodecInst* codecInst)
2013{
2014 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId,_channelId),
2015 "Channel::StartPlayingFileAsMicrophone(fileNameUTF8[]=%s, "
2016 "loop=%d, format=%d, volumeScaling=%5.3f, startPosition=%d, "
2017 "stopPosition=%d)", fileName, loop, format, volumeScaling,
2018 startPosition, stopPosition);
2019
henrika@webrtc.org944cbeb2014-03-18 10:32:33 +00002020 CriticalSectionScoped cs(&_fileCritSect);
2021
2022 if (channel_state_.Get().input_file_playing)
niklase@google.com470e71d2011-07-07 08:21:25 +00002023 {
2024 _engineStatisticsPtr->SetLastError(
2025 VE_ALREADY_PLAYING, kTraceWarning,
2026 "StartPlayingFileAsMicrophone() filePlayer is playing");
2027 return 0;
2028 }
2029
niklase@google.com470e71d2011-07-07 08:21:25 +00002030 // Destroy the old instance
2031 if (_inputFilePlayerPtr)
2032 {
2033 _inputFilePlayerPtr->RegisterModuleFileCallback(NULL);
2034 FilePlayer::DestroyFilePlayer(_inputFilePlayerPtr);
2035 _inputFilePlayerPtr = NULL;
2036 }
2037
2038 // Create the instance
2039 _inputFilePlayerPtr = FilePlayer::CreateFilePlayer(
2040 _inputFilePlayerId, (const FileFormats)format);
2041
2042 if (_inputFilePlayerPtr == NULL)
2043 {
2044 _engineStatisticsPtr->SetLastError(
2045 VE_INVALID_ARGUMENT, kTraceError,
2046 "StartPlayingFileAsMicrophone() filePlayer format isnot correct");
2047 return -1;
2048 }
2049
pbos@webrtc.org6141e132013-04-09 10:09:10 +00002050 const uint32_t notificationTime(0);
niklase@google.com470e71d2011-07-07 08:21:25 +00002051
2052 if (_inputFilePlayerPtr->StartPlayingFile(
2053 fileName,
2054 loop,
2055 startPosition,
2056 volumeScaling,
2057 notificationTime,
2058 stopPosition,
2059 (const CodecInst*)codecInst) != 0)
2060 {
2061 _engineStatisticsPtr->SetLastError(
2062 VE_BAD_FILE, kTraceError,
2063 "StartPlayingFile() failed to start file playout");
2064 _inputFilePlayerPtr->StopPlayingFile();
2065 FilePlayer::DestroyFilePlayer(_inputFilePlayerPtr);
2066 _inputFilePlayerPtr = NULL;
2067 return -1;
2068 }
2069 _inputFilePlayerPtr->RegisterModuleFileCallback(this);
henrika@webrtc.org944cbeb2014-03-18 10:32:33 +00002070 channel_state_.SetInputFilePlaying(true);
niklase@google.com470e71d2011-07-07 08:21:25 +00002071
2072 return 0;
2073}
2074
2075int Channel::StartPlayingFileAsMicrophone(InStream* stream,
pbos@webrtc.org92135212013-05-14 08:31:39 +00002076 FileFormats format,
2077 int startPosition,
2078 float volumeScaling,
2079 int stopPosition,
niklase@google.com470e71d2011-07-07 08:21:25 +00002080 const CodecInst* codecInst)
2081{
2082 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId,_channelId),
2083 "Channel::StartPlayingFileAsMicrophone(format=%d, "
2084 "volumeScaling=%5.3f, startPosition=%d, stopPosition=%d)",
2085 format, volumeScaling, startPosition, stopPosition);
2086
2087 if(stream == NULL)
2088 {
2089 _engineStatisticsPtr->SetLastError(
2090 VE_BAD_FILE, kTraceError,
2091 "StartPlayingFileAsMicrophone NULL as input stream");
2092 return -1;
2093 }
2094
henrika@webrtc.org944cbeb2014-03-18 10:32:33 +00002095 CriticalSectionScoped cs(&_fileCritSect);
2096
2097 if (channel_state_.Get().input_file_playing)
niklase@google.com470e71d2011-07-07 08:21:25 +00002098 {
2099 _engineStatisticsPtr->SetLastError(
2100 VE_ALREADY_PLAYING, kTraceWarning,
2101 "StartPlayingFileAsMicrophone() is playing");
2102 return 0;
2103 }
2104
niklase@google.com470e71d2011-07-07 08:21:25 +00002105 // Destroy the old instance
2106 if (_inputFilePlayerPtr)
2107 {
2108 _inputFilePlayerPtr->RegisterModuleFileCallback(NULL);
2109 FilePlayer::DestroyFilePlayer(_inputFilePlayerPtr);
2110 _inputFilePlayerPtr = NULL;
2111 }
2112
2113 // Create the instance
2114 _inputFilePlayerPtr = FilePlayer::CreateFilePlayer(
2115 _inputFilePlayerId, (const FileFormats)format);
2116
2117 if (_inputFilePlayerPtr == NULL)
2118 {
2119 _engineStatisticsPtr->SetLastError(
2120 VE_INVALID_ARGUMENT, kTraceError,
2121 "StartPlayingInputFile() filePlayer format isnot correct");
2122 return -1;
2123 }
2124
pbos@webrtc.org6141e132013-04-09 10:09:10 +00002125 const uint32_t notificationTime(0);
niklase@google.com470e71d2011-07-07 08:21:25 +00002126
2127 if (_inputFilePlayerPtr->StartPlayingFile(*stream, startPosition,
2128 volumeScaling, notificationTime,
2129 stopPosition, codecInst) != 0)
2130 {
2131 _engineStatisticsPtr->SetLastError(VE_BAD_FILE, kTraceError,
2132 "StartPlayingFile() failed to start "
2133 "file playout");
2134 _inputFilePlayerPtr->StopPlayingFile();
2135 FilePlayer::DestroyFilePlayer(_inputFilePlayerPtr);
2136 _inputFilePlayerPtr = NULL;
2137 return -1;
2138 }
andrew@webrtc.orgae1a58b2013-01-22 04:44:30 +00002139
niklase@google.com470e71d2011-07-07 08:21:25 +00002140 _inputFilePlayerPtr->RegisterModuleFileCallback(this);
henrika@webrtc.org944cbeb2014-03-18 10:32:33 +00002141 channel_state_.SetInputFilePlaying(true);
niklase@google.com470e71d2011-07-07 08:21:25 +00002142
2143 return 0;
2144}
2145
2146int Channel::StopPlayingFileAsMicrophone()
2147{
2148 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId,_channelId),
2149 "Channel::StopPlayingFileAsMicrophone()");
2150
henrika@webrtc.org944cbeb2014-03-18 10:32:33 +00002151 CriticalSectionScoped cs(&_fileCritSect);
2152
2153 if (!channel_state_.Get().input_file_playing)
niklase@google.com470e71d2011-07-07 08:21:25 +00002154 {
2155 _engineStatisticsPtr->SetLastError(
2156 VE_INVALID_OPERATION, kTraceWarning,
2157 "StopPlayingFileAsMicrophone() isnot playing");
2158 return 0;
2159 }
2160
niklase@google.com470e71d2011-07-07 08:21:25 +00002161 if (_inputFilePlayerPtr->StopPlayingFile() != 0)
2162 {
2163 _engineStatisticsPtr->SetLastError(
2164 VE_STOP_RECORDING_FAILED, kTraceError,
2165 "StopPlayingFile() could not stop playing");
2166 return -1;
2167 }
2168 _inputFilePlayerPtr->RegisterModuleFileCallback(NULL);
2169 FilePlayer::DestroyFilePlayer(_inputFilePlayerPtr);
2170 _inputFilePlayerPtr = NULL;
henrika@webrtc.org944cbeb2014-03-18 10:32:33 +00002171 channel_state_.SetInputFilePlaying(false);
niklase@google.com470e71d2011-07-07 08:21:25 +00002172
2173 return 0;
2174}
2175
2176int Channel::IsPlayingFileAsMicrophone() const
2177{
2178 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId,_channelId),
2179 "Channel::IsPlayingFileAsMicrophone()");
henrika@webrtc.org944cbeb2014-03-18 10:32:33 +00002180 return channel_state_.Get().input_file_playing;
niklase@google.com470e71d2011-07-07 08:21:25 +00002181}
2182
leozwang@webrtc.org813e4b02012-03-01 18:34:25 +00002183int Channel::StartRecordingPlayout(const char* fileName,
niklase@google.com470e71d2011-07-07 08:21:25 +00002184 const CodecInst* codecInst)
2185{
2186 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId,_channelId),
2187 "Channel::StartRecordingPlayout(fileName=%s)", fileName);
2188
2189 if (_outputFileRecording)
2190 {
2191 WEBRTC_TRACE(kTraceWarning, kTraceVoice, VoEId(_instanceId,-1),
2192 "StartRecordingPlayout() is already recording");
2193 return 0;
2194 }
2195
2196 FileFormats format;
pbos@webrtc.org6141e132013-04-09 10:09:10 +00002197 const uint32_t notificationTime(0); // Not supported in VoE
niklase@google.com470e71d2011-07-07 08:21:25 +00002198 CodecInst dummyCodec={100,"L16",16000,320,1,320000};
2199
niklas.enbom@webrtc.org40197d72012-03-26 08:45:47 +00002200 if ((codecInst != NULL) &&
2201 ((codecInst->channels < 1) || (codecInst->channels > 2)))
niklase@google.com470e71d2011-07-07 08:21:25 +00002202 {
2203 _engineStatisticsPtr->SetLastError(
2204 VE_BAD_ARGUMENT, kTraceError,
2205 "StartRecordingPlayout() invalid compression");
2206 return(-1);
2207 }
2208 if(codecInst == NULL)
2209 {
2210 format = kFileFormatPcm16kHzFile;
2211 codecInst=&dummyCodec;
2212 }
2213 else if((STR_CASE_CMP(codecInst->plname,"L16") == 0) ||
2214 (STR_CASE_CMP(codecInst->plname,"PCMU") == 0) ||
2215 (STR_CASE_CMP(codecInst->plname,"PCMA") == 0))
2216 {
2217 format = kFileFormatWavFile;
2218 }
2219 else
2220 {
2221 format = kFileFormatCompressedFile;
2222 }
2223
mflodman@webrtc.org9a065d12012-03-07 08:12:21 +00002224 CriticalSectionScoped cs(&_fileCritSect);
niklase@google.com470e71d2011-07-07 08:21:25 +00002225
2226 // Destroy the old instance
2227 if (_outputFileRecorderPtr)
2228 {
2229 _outputFileRecorderPtr->RegisterModuleFileCallback(NULL);
2230 FileRecorder::DestroyFileRecorder(_outputFileRecorderPtr);
2231 _outputFileRecorderPtr = NULL;
2232 }
2233
2234 _outputFileRecorderPtr = FileRecorder::CreateFileRecorder(
2235 _outputFileRecorderId, (const FileFormats)format);
2236 if (_outputFileRecorderPtr == NULL)
2237 {
2238 _engineStatisticsPtr->SetLastError(
2239 VE_INVALID_ARGUMENT, kTraceError,
2240 "StartRecordingPlayout() fileRecorder format isnot correct");
2241 return -1;
2242 }
2243
2244 if (_outputFileRecorderPtr->StartRecordingAudioFile(
2245 fileName, (const CodecInst&)*codecInst, notificationTime) != 0)
2246 {
2247 _engineStatisticsPtr->SetLastError(
2248 VE_BAD_FILE, kTraceError,
2249 "StartRecordingAudioFile() failed to start file recording");
2250 _outputFileRecorderPtr->StopRecording();
2251 FileRecorder::DestroyFileRecorder(_outputFileRecorderPtr);
2252 _outputFileRecorderPtr = NULL;
2253 return -1;
2254 }
2255 _outputFileRecorderPtr->RegisterModuleFileCallback(this);
2256 _outputFileRecording = true;
2257
2258 return 0;
2259}
2260
2261int Channel::StartRecordingPlayout(OutStream* stream,
2262 const CodecInst* codecInst)
2263{
2264 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId,_channelId),
2265 "Channel::StartRecordingPlayout()");
2266
2267 if (_outputFileRecording)
2268 {
2269 WEBRTC_TRACE(kTraceWarning, kTraceVoice, VoEId(_instanceId,-1),
2270 "StartRecordingPlayout() is already recording");
2271 return 0;
2272 }
2273
2274 FileFormats format;
pbos@webrtc.org6141e132013-04-09 10:09:10 +00002275 const uint32_t notificationTime(0); // Not supported in VoE
niklase@google.com470e71d2011-07-07 08:21:25 +00002276 CodecInst dummyCodec={100,"L16",16000,320,1,320000};
2277
2278 if (codecInst != NULL && codecInst->channels != 1)
2279 {
2280 _engineStatisticsPtr->SetLastError(
2281 VE_BAD_ARGUMENT, kTraceError,
2282 "StartRecordingPlayout() invalid compression");
2283 return(-1);
2284 }
2285 if(codecInst == NULL)
2286 {
2287 format = kFileFormatPcm16kHzFile;
2288 codecInst=&dummyCodec;
2289 }
2290 else if((STR_CASE_CMP(codecInst->plname,"L16") == 0) ||
2291 (STR_CASE_CMP(codecInst->plname,"PCMU") == 0) ||
2292 (STR_CASE_CMP(codecInst->plname,"PCMA") == 0))
2293 {
2294 format = kFileFormatWavFile;
2295 }
2296 else
2297 {
2298 format = kFileFormatCompressedFile;
2299 }
2300
mflodman@webrtc.org9a065d12012-03-07 08:12:21 +00002301 CriticalSectionScoped cs(&_fileCritSect);
niklase@google.com470e71d2011-07-07 08:21:25 +00002302
2303 // Destroy the old instance
2304 if (_outputFileRecorderPtr)
2305 {
2306 _outputFileRecorderPtr->RegisterModuleFileCallback(NULL);
2307 FileRecorder::DestroyFileRecorder(_outputFileRecorderPtr);
2308 _outputFileRecorderPtr = NULL;
2309 }
2310
2311 _outputFileRecorderPtr = FileRecorder::CreateFileRecorder(
2312 _outputFileRecorderId, (const FileFormats)format);
2313 if (_outputFileRecorderPtr == NULL)
2314 {
2315 _engineStatisticsPtr->SetLastError(
2316 VE_INVALID_ARGUMENT, kTraceError,
2317 "StartRecordingPlayout() fileRecorder format isnot correct");
2318 return -1;
2319 }
2320
2321 if (_outputFileRecorderPtr->StartRecordingAudioFile(*stream, *codecInst,
2322 notificationTime) != 0)
2323 {
2324 _engineStatisticsPtr->SetLastError(VE_BAD_FILE, kTraceError,
2325 "StartRecordingPlayout() failed to "
2326 "start file recording");
2327 _outputFileRecorderPtr->StopRecording();
2328 FileRecorder::DestroyFileRecorder(_outputFileRecorderPtr);
2329 _outputFileRecorderPtr = NULL;
2330 return -1;
2331 }
andrew@webrtc.orgae1a58b2013-01-22 04:44:30 +00002332
niklase@google.com470e71d2011-07-07 08:21:25 +00002333 _outputFileRecorderPtr->RegisterModuleFileCallback(this);
2334 _outputFileRecording = true;
2335
2336 return 0;
2337}
2338
2339int Channel::StopRecordingPlayout()
2340{
2341 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId,-1),
2342 "Channel::StopRecordingPlayout()");
2343
2344 if (!_outputFileRecording)
2345 {
2346 WEBRTC_TRACE(kTraceError, kTraceVoice, VoEId(_instanceId,-1),
2347 "StopRecordingPlayout() isnot recording");
2348 return -1;
2349 }
2350
2351
mflodman@webrtc.org9a065d12012-03-07 08:12:21 +00002352 CriticalSectionScoped cs(&_fileCritSect);
niklase@google.com470e71d2011-07-07 08:21:25 +00002353
2354 if (_outputFileRecorderPtr->StopRecording() != 0)
2355 {
2356 _engineStatisticsPtr->SetLastError(
2357 VE_STOP_RECORDING_FAILED, kTraceError,
2358 "StopRecording() could not stop recording");
2359 return(-1);
2360 }
2361 _outputFileRecorderPtr->RegisterModuleFileCallback(NULL);
2362 FileRecorder::DestroyFileRecorder(_outputFileRecorderPtr);
2363 _outputFileRecorderPtr = NULL;
2364 _outputFileRecording = false;
2365
2366 return 0;
2367}
2368
2369void
2370Channel::SetMixWithMicStatus(bool mix)
2371{
henrika@webrtc.org944cbeb2014-03-18 10:32:33 +00002372 CriticalSectionScoped cs(&_fileCritSect);
niklase@google.com470e71d2011-07-07 08:21:25 +00002373 _mixFileWithMicrophone=mix;
2374}
2375
2376int
pbos@webrtc.org6141e132013-04-09 10:09:10 +00002377Channel::GetSpeechOutputLevel(uint32_t& level) const
niklase@google.com470e71d2011-07-07 08:21:25 +00002378{
pbos@webrtc.org6141e132013-04-09 10:09:10 +00002379 int8_t currentLevel = _outputAudioLevel.Level();
2380 level = static_cast<int32_t> (currentLevel);
niklase@google.com470e71d2011-07-07 08:21:25 +00002381 WEBRTC_TRACE(kTraceStateInfo, kTraceVoice,
2382 VoEId(_instanceId,_channelId),
2383 "GetSpeechOutputLevel() => level=%u", level);
2384 return 0;
2385}
2386
2387int
pbos@webrtc.org6141e132013-04-09 10:09:10 +00002388Channel::GetSpeechOutputLevelFullRange(uint32_t& level) const
niklase@google.com470e71d2011-07-07 08:21:25 +00002389{
pbos@webrtc.org6141e132013-04-09 10:09:10 +00002390 int16_t currentLevel = _outputAudioLevel.LevelFullRange();
2391 level = static_cast<int32_t> (currentLevel);
niklase@google.com470e71d2011-07-07 08:21:25 +00002392 WEBRTC_TRACE(kTraceStateInfo, kTraceVoice,
2393 VoEId(_instanceId,_channelId),
2394 "GetSpeechOutputLevelFullRange() => level=%u", level);
2395 return 0;
2396}
2397
2398int
2399Channel::SetMute(bool enable)
2400{
wu@webrtc.org63420662013-10-17 18:28:55 +00002401 CriticalSectionScoped cs(&volume_settings_critsect_);
niklase@google.com470e71d2011-07-07 08:21:25 +00002402 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId,_channelId),
2403 "Channel::SetMute(enable=%d)", enable);
2404 _mute = enable;
2405 return 0;
2406}
2407
2408bool
2409Channel::Mute() const
2410{
wu@webrtc.org63420662013-10-17 18:28:55 +00002411 CriticalSectionScoped cs(&volume_settings_critsect_);
niklase@google.com470e71d2011-07-07 08:21:25 +00002412 return _mute;
2413}
2414
2415int
2416Channel::SetOutputVolumePan(float left, float right)
2417{
wu@webrtc.org63420662013-10-17 18:28:55 +00002418 CriticalSectionScoped cs(&volume_settings_critsect_);
niklase@google.com470e71d2011-07-07 08:21:25 +00002419 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId,_channelId),
2420 "Channel::SetOutputVolumePan()");
2421 _panLeft = left;
2422 _panRight = right;
2423 return 0;
2424}
2425
2426int
2427Channel::GetOutputVolumePan(float& left, float& right) const
2428{
wu@webrtc.org63420662013-10-17 18:28:55 +00002429 CriticalSectionScoped cs(&volume_settings_critsect_);
niklase@google.com470e71d2011-07-07 08:21:25 +00002430 left = _panLeft;
2431 right = _panRight;
2432 WEBRTC_TRACE(kTraceStateInfo, kTraceVoice,
2433 VoEId(_instanceId,_channelId),
2434 "GetOutputVolumePan() => left=%3.2f, right=%3.2f", left, right);
2435 return 0;
2436}
2437
2438int
2439Channel::SetChannelOutputVolumeScaling(float scaling)
2440{
wu@webrtc.org63420662013-10-17 18:28:55 +00002441 CriticalSectionScoped cs(&volume_settings_critsect_);
niklase@google.com470e71d2011-07-07 08:21:25 +00002442 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId,_channelId),
2443 "Channel::SetChannelOutputVolumeScaling()");
2444 _outputGain = scaling;
2445 return 0;
2446}
2447
2448int
2449Channel::GetChannelOutputVolumeScaling(float& scaling) const
2450{
wu@webrtc.org63420662013-10-17 18:28:55 +00002451 CriticalSectionScoped cs(&volume_settings_critsect_);
niklase@google.com470e71d2011-07-07 08:21:25 +00002452 scaling = _outputGain;
2453 WEBRTC_TRACE(kTraceStateInfo, kTraceVoice,
2454 VoEId(_instanceId,_channelId),
2455 "GetChannelOutputVolumeScaling() => scaling=%3.2f", scaling);
2456 return 0;
2457}
2458
niklase@google.com470e71d2011-07-07 08:21:25 +00002459int Channel::SendTelephoneEventOutband(unsigned char eventCode,
wu@webrtc.org822fbd82013-08-15 23:38:54 +00002460 int lengthMs, int attenuationDb,
2461 bool playDtmfEvent)
niklase@google.com470e71d2011-07-07 08:21:25 +00002462{
2463 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId, _channelId),
2464 "Channel::SendTelephoneEventOutband(..., playDtmfEvent=%d)",
2465 playDtmfEvent);
2466
2467 _playOutbandDtmfEvent = playDtmfEvent;
2468
pwestin@webrtc.org2853dde2012-05-11 11:08:54 +00002469 if (_rtpRtcpModule->SendTelephoneEventOutband(eventCode, lengthMs,
niklase@google.com470e71d2011-07-07 08:21:25 +00002470 attenuationDb) != 0)
2471 {
2472 _engineStatisticsPtr->SetLastError(
2473 VE_SEND_DTMF_FAILED,
2474 kTraceWarning,
2475 "SendTelephoneEventOutband() failed to send event");
2476 return -1;
2477 }
2478 return 0;
2479}
2480
2481int Channel::SendTelephoneEventInband(unsigned char eventCode,
2482 int lengthMs,
2483 int attenuationDb,
2484 bool playDtmfEvent)
2485{
2486 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId, _channelId),
2487 "Channel::SendTelephoneEventInband(..., playDtmfEvent=%d)",
2488 playDtmfEvent);
2489
2490 _playInbandDtmfEvent = playDtmfEvent;
2491 _inbandDtmfQueue.AddDtmf(eventCode, lengthMs, attenuationDb);
2492
2493 return 0;
2494}
2495
2496int
niklase@google.com470e71d2011-07-07 08:21:25 +00002497Channel::SetSendTelephoneEventPayloadType(unsigned char type)
2498{
2499 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId,_channelId),
2500 "Channel::SetSendTelephoneEventPayloadType()");
andrew@webrtc.orgf81f9f82011-08-19 22:56:22 +00002501 if (type > 127)
niklase@google.com470e71d2011-07-07 08:21:25 +00002502 {
2503 _engineStatisticsPtr->SetLastError(
2504 VE_INVALID_ARGUMENT, kTraceError,
2505 "SetSendTelephoneEventPayloadType() invalid type");
2506 return -1;
2507 }
pbos@webrtc.org5b10d8f2013-07-11 15:50:07 +00002508 CodecInst codec = {};
pwestin@webrtc.org1da1ce02011-10-13 15:19:55 +00002509 codec.plfreq = 8000;
2510 codec.pltype = type;
2511 memcpy(codec.plname, "telephone-event", 16);
pwestin@webrtc.org2853dde2012-05-11 11:08:54 +00002512 if (_rtpRtcpModule->RegisterSendPayload(codec) != 0)
niklase@google.com470e71d2011-07-07 08:21:25 +00002513 {
henrika@webrtc.org4392d5f2013-04-17 07:34:25 +00002514 _rtpRtcpModule->DeRegisterSendPayload(codec.pltype);
2515 if (_rtpRtcpModule->RegisterSendPayload(codec) != 0) {
2516 _engineStatisticsPtr->SetLastError(
2517 VE_RTP_RTCP_MODULE_ERROR, kTraceError,
2518 "SetSendTelephoneEventPayloadType() failed to register send"
2519 "payload type");
2520 return -1;
2521 }
niklase@google.com470e71d2011-07-07 08:21:25 +00002522 }
2523 _sendTelephoneEventPayloadType = type;
2524 return 0;
2525}
2526
2527int
2528Channel::GetSendTelephoneEventPayloadType(unsigned char& type)
2529{
2530 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId,_channelId),
2531 "Channel::GetSendTelephoneEventPayloadType()");
2532 type = _sendTelephoneEventPayloadType;
2533 WEBRTC_TRACE(kTraceStateInfo, kTraceVoice,
2534 VoEId(_instanceId,_channelId),
2535 "GetSendTelephoneEventPayloadType() => type=%u", type);
2536 return 0;
2537}
2538
niklase@google.com470e71d2011-07-07 08:21:25 +00002539int
2540Channel::UpdateRxVadDetection(AudioFrame& audioFrame)
2541{
2542 WEBRTC_TRACE(kTraceStream, kTraceVoice, VoEId(_instanceId,_channelId),
2543 "Channel::UpdateRxVadDetection()");
2544
2545 int vadDecision = 1;
2546
andrew@webrtc.org63a50982012-05-02 23:56:37 +00002547 vadDecision = (audioFrame.vad_activity_ == AudioFrame::kVadActive)? 1 : 0;
niklase@google.com470e71d2011-07-07 08:21:25 +00002548
2549 if ((vadDecision != _oldVadDecision) && _rxVadObserverPtr)
2550 {
2551 OnRxVadDetected(vadDecision);
2552 _oldVadDecision = vadDecision;
2553 }
2554
2555 WEBRTC_TRACE(kTraceStream, kTraceVoice, VoEId(_instanceId,_channelId),
2556 "Channel::UpdateRxVadDetection() => vadDecision=%d",
2557 vadDecision);
2558 return 0;
2559}
2560
2561int
2562Channel::RegisterRxVadObserver(VoERxVadCallback &observer)
2563{
2564 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId,_channelId),
2565 "Channel::RegisterRxVadObserver()");
mflodman@webrtc.org9a065d12012-03-07 08:12:21 +00002566 CriticalSectionScoped cs(&_callbackCritSect);
niklase@google.com470e71d2011-07-07 08:21:25 +00002567
2568 if (_rxVadObserverPtr)
2569 {
2570 _engineStatisticsPtr->SetLastError(
2571 VE_INVALID_OPERATION, kTraceError,
2572 "RegisterRxVadObserver() observer already enabled");
2573 return -1;
2574 }
niklase@google.com470e71d2011-07-07 08:21:25 +00002575 _rxVadObserverPtr = &observer;
2576 _RxVadDetection = true;
2577 return 0;
2578}
2579
2580int
2581Channel::DeRegisterRxVadObserver()
2582{
2583 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId,_channelId),
2584 "Channel::DeRegisterRxVadObserver()");
mflodman@webrtc.org9a065d12012-03-07 08:12:21 +00002585 CriticalSectionScoped cs(&_callbackCritSect);
niklase@google.com470e71d2011-07-07 08:21:25 +00002586
2587 if (!_rxVadObserverPtr)
2588 {
2589 _engineStatisticsPtr->SetLastError(
2590 VE_INVALID_OPERATION, kTraceWarning,
2591 "DeRegisterRxVadObserver() observer already disabled");
2592 return 0;
2593 }
2594 _rxVadObserverPtr = NULL;
2595 _RxVadDetection = false;
2596 return 0;
2597}
2598
2599int
2600Channel::VoiceActivityIndicator(int &activity)
2601{
2602 activity = _sendFrameType;
2603
2604 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId,_channelId),
andrew@webrtc.org6c264cc2013-10-04 17:54:09 +00002605 "Channel::VoiceActivityIndicator(indicator=%d)", activity);
niklase@google.com470e71d2011-07-07 08:21:25 +00002606 return 0;
2607}
2608
2609#ifdef WEBRTC_VOICE_ENGINE_AGC
2610
2611int
pbos@webrtc.org92135212013-05-14 08:31:39 +00002612Channel::SetRxAgcStatus(bool enable, AgcModes mode)
niklase@google.com470e71d2011-07-07 08:21:25 +00002613{
2614 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId,_channelId),
2615 "Channel::SetRxAgcStatus(enable=%d, mode=%d)",
2616 (int)enable, (int)mode);
2617
andrew@webrtc.org6c264cc2013-10-04 17:54:09 +00002618 GainControl::Mode agcMode = kDefaultRxAgcMode;
niklase@google.com470e71d2011-07-07 08:21:25 +00002619 switch (mode)
2620 {
2621 case kAgcDefault:
niklase@google.com470e71d2011-07-07 08:21:25 +00002622 break;
2623 case kAgcUnchanged:
andrew@webrtc.orgf3930e92013-09-18 22:37:32 +00002624 agcMode = rx_audioproc_->gain_control()->mode();
niklase@google.com470e71d2011-07-07 08:21:25 +00002625 break;
2626 case kAgcFixedDigital:
2627 agcMode = GainControl::kFixedDigital;
2628 break;
2629 case kAgcAdaptiveDigital:
2630 agcMode =GainControl::kAdaptiveDigital;
2631 break;
2632 default:
2633 _engineStatisticsPtr->SetLastError(
2634 VE_INVALID_ARGUMENT, kTraceError,
2635 "SetRxAgcStatus() invalid Agc mode");
2636 return -1;
2637 }
2638
andrew@webrtc.orgf3930e92013-09-18 22:37:32 +00002639 if (rx_audioproc_->gain_control()->set_mode(agcMode) != 0)
niklase@google.com470e71d2011-07-07 08:21:25 +00002640 {
2641 _engineStatisticsPtr->SetLastError(
2642 VE_APM_ERROR, kTraceError,
2643 "SetRxAgcStatus() failed to set Agc mode");
2644 return -1;
2645 }
andrew@webrtc.orgf3930e92013-09-18 22:37:32 +00002646 if (rx_audioproc_->gain_control()->Enable(enable) != 0)
niklase@google.com470e71d2011-07-07 08:21:25 +00002647 {
2648 _engineStatisticsPtr->SetLastError(
2649 VE_APM_ERROR, kTraceError,
2650 "SetRxAgcStatus() failed to set Agc state");
2651 return -1;
2652 }
2653
2654 _rxAgcIsEnabled = enable;
henrika@webrtc.org944cbeb2014-03-18 10:32:33 +00002655 channel_state_.SetRxApmIsEnabled(_rxAgcIsEnabled || _rxNsIsEnabled);
niklase@google.com470e71d2011-07-07 08:21:25 +00002656
2657 return 0;
2658}
2659
2660int
2661Channel::GetRxAgcStatus(bool& enabled, AgcModes& mode)
2662{
2663 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId,_channelId),
2664 "Channel::GetRxAgcStatus(enable=?, mode=?)");
2665
andrew@webrtc.orgf3930e92013-09-18 22:37:32 +00002666 bool enable = rx_audioproc_->gain_control()->is_enabled();
niklase@google.com470e71d2011-07-07 08:21:25 +00002667 GainControl::Mode agcMode =
andrew@webrtc.orgf3930e92013-09-18 22:37:32 +00002668 rx_audioproc_->gain_control()->mode();
niklase@google.com470e71d2011-07-07 08:21:25 +00002669
2670 enabled = enable;
2671
2672 switch (agcMode)
2673 {
2674 case GainControl::kFixedDigital:
2675 mode = kAgcFixedDigital;
2676 break;
2677 case GainControl::kAdaptiveDigital:
2678 mode = kAgcAdaptiveDigital;
2679 break;
2680 default:
2681 _engineStatisticsPtr->SetLastError(
2682 VE_APM_ERROR, kTraceError,
2683 "GetRxAgcStatus() invalid Agc mode");
2684 return -1;
2685 }
2686
2687 return 0;
2688}
2689
2690int
pbos@webrtc.org92135212013-05-14 08:31:39 +00002691Channel::SetRxAgcConfig(AgcConfig config)
niklase@google.com470e71d2011-07-07 08:21:25 +00002692{
2693 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId,_channelId),
2694 "Channel::SetRxAgcConfig()");
2695
andrew@webrtc.orgf3930e92013-09-18 22:37:32 +00002696 if (rx_audioproc_->gain_control()->set_target_level_dbfs(
niklase@google.com470e71d2011-07-07 08:21:25 +00002697 config.targetLeveldBOv) != 0)
2698 {
2699 _engineStatisticsPtr->SetLastError(
2700 VE_APM_ERROR, kTraceError,
2701 "SetRxAgcConfig() failed to set target peak |level|"
2702 "(or envelope) of the Agc");
2703 return -1;
2704 }
andrew@webrtc.orgf3930e92013-09-18 22:37:32 +00002705 if (rx_audioproc_->gain_control()->set_compression_gain_db(
niklase@google.com470e71d2011-07-07 08:21:25 +00002706 config.digitalCompressionGaindB) != 0)
2707 {
2708 _engineStatisticsPtr->SetLastError(
2709 VE_APM_ERROR, kTraceError,
2710 "SetRxAgcConfig() failed to set the range in |gain| the"
2711 " digital compression stage may apply");
2712 return -1;
2713 }
andrew@webrtc.orgf3930e92013-09-18 22:37:32 +00002714 if (rx_audioproc_->gain_control()->enable_limiter(
niklase@google.com470e71d2011-07-07 08:21:25 +00002715 config.limiterEnable) != 0)
2716 {
2717 _engineStatisticsPtr->SetLastError(
2718 VE_APM_ERROR, kTraceError,
2719 "SetRxAgcConfig() failed to set hard limiter to the signal");
2720 return -1;
2721 }
2722
2723 return 0;
2724}
2725
2726int
2727Channel::GetRxAgcConfig(AgcConfig& config)
2728{
2729 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId,_channelId),
2730 "Channel::GetRxAgcConfig(config=%?)");
2731
2732 config.targetLeveldBOv =
andrew@webrtc.orgf3930e92013-09-18 22:37:32 +00002733 rx_audioproc_->gain_control()->target_level_dbfs();
niklase@google.com470e71d2011-07-07 08:21:25 +00002734 config.digitalCompressionGaindB =
andrew@webrtc.orgf3930e92013-09-18 22:37:32 +00002735 rx_audioproc_->gain_control()->compression_gain_db();
niklase@google.com470e71d2011-07-07 08:21:25 +00002736 config.limiterEnable =
andrew@webrtc.orgf3930e92013-09-18 22:37:32 +00002737 rx_audioproc_->gain_control()->is_limiter_enabled();
niklase@google.com470e71d2011-07-07 08:21:25 +00002738
2739 WEBRTC_TRACE(kTraceStateInfo, kTraceVoice,
2740 VoEId(_instanceId,_channelId), "GetRxAgcConfig() => "
2741 "targetLeveldBOv=%u, digitalCompressionGaindB=%u,"
2742 " limiterEnable=%d",
2743 config.targetLeveldBOv,
2744 config.digitalCompressionGaindB,
2745 config.limiterEnable);
2746
2747 return 0;
2748}
2749
2750#endif // #ifdef WEBRTC_VOICE_ENGINE_AGC
2751
2752#ifdef WEBRTC_VOICE_ENGINE_NR
2753
2754int
pbos@webrtc.org92135212013-05-14 08:31:39 +00002755Channel::SetRxNsStatus(bool enable, NsModes mode)
niklase@google.com470e71d2011-07-07 08:21:25 +00002756{
2757 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId,_channelId),
2758 "Channel::SetRxNsStatus(enable=%d, mode=%d)",
2759 (int)enable, (int)mode);
2760
andrew@webrtc.org6c264cc2013-10-04 17:54:09 +00002761 NoiseSuppression::Level nsLevel = kDefaultNsMode;
niklase@google.com470e71d2011-07-07 08:21:25 +00002762 switch (mode)
2763 {
2764
2765 case kNsDefault:
niklase@google.com470e71d2011-07-07 08:21:25 +00002766 break;
2767 case kNsUnchanged:
andrew@webrtc.orgf3930e92013-09-18 22:37:32 +00002768 nsLevel = rx_audioproc_->noise_suppression()->level();
niklase@google.com470e71d2011-07-07 08:21:25 +00002769 break;
2770 case kNsConference:
2771 nsLevel = NoiseSuppression::kHigh;
2772 break;
2773 case kNsLowSuppression:
2774 nsLevel = NoiseSuppression::kLow;
2775 break;
2776 case kNsModerateSuppression:
2777 nsLevel = NoiseSuppression::kModerate;
2778 break;
2779 case kNsHighSuppression:
2780 nsLevel = NoiseSuppression::kHigh;
2781 break;
2782 case kNsVeryHighSuppression:
2783 nsLevel = NoiseSuppression::kVeryHigh;
2784 break;
niklase@google.com470e71d2011-07-07 08:21:25 +00002785 }
2786
andrew@webrtc.orgf3930e92013-09-18 22:37:32 +00002787 if (rx_audioproc_->noise_suppression()->set_level(nsLevel)
niklase@google.com470e71d2011-07-07 08:21:25 +00002788 != 0)
2789 {
2790 _engineStatisticsPtr->SetLastError(
2791 VE_APM_ERROR, kTraceError,
andrew@webrtc.org6c264cc2013-10-04 17:54:09 +00002792 "SetRxNsStatus() failed to set NS level");
niklase@google.com470e71d2011-07-07 08:21:25 +00002793 return -1;
2794 }
andrew@webrtc.orgf3930e92013-09-18 22:37:32 +00002795 if (rx_audioproc_->noise_suppression()->Enable(enable) != 0)
niklase@google.com470e71d2011-07-07 08:21:25 +00002796 {
2797 _engineStatisticsPtr->SetLastError(
2798 VE_APM_ERROR, kTraceError,
andrew@webrtc.org6c264cc2013-10-04 17:54:09 +00002799 "SetRxNsStatus() failed to set NS state");
niklase@google.com470e71d2011-07-07 08:21:25 +00002800 return -1;
2801 }
2802
2803 _rxNsIsEnabled = enable;
henrika@webrtc.org944cbeb2014-03-18 10:32:33 +00002804 channel_state_.SetRxApmIsEnabled(_rxAgcIsEnabled || _rxNsIsEnabled);
niklase@google.com470e71d2011-07-07 08:21:25 +00002805
2806 return 0;
2807}
2808
2809int
2810Channel::GetRxNsStatus(bool& enabled, NsModes& mode)
2811{
2812 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId,_channelId),
2813 "Channel::GetRxNsStatus(enable=?, mode=?)");
2814
2815 bool enable =
andrew@webrtc.orgf3930e92013-09-18 22:37:32 +00002816 rx_audioproc_->noise_suppression()->is_enabled();
niklase@google.com470e71d2011-07-07 08:21:25 +00002817 NoiseSuppression::Level ncLevel =
andrew@webrtc.orgf3930e92013-09-18 22:37:32 +00002818 rx_audioproc_->noise_suppression()->level();
niklase@google.com470e71d2011-07-07 08:21:25 +00002819
2820 enabled = enable;
2821
2822 switch (ncLevel)
2823 {
2824 case NoiseSuppression::kLow:
2825 mode = kNsLowSuppression;
2826 break;
2827 case NoiseSuppression::kModerate:
2828 mode = kNsModerateSuppression;
2829 break;
2830 case NoiseSuppression::kHigh:
2831 mode = kNsHighSuppression;
2832 break;
2833 case NoiseSuppression::kVeryHigh:
2834 mode = kNsVeryHighSuppression;
2835 break;
niklase@google.com470e71d2011-07-07 08:21:25 +00002836 }
2837
2838 WEBRTC_TRACE(kTraceStateInfo, kTraceVoice,
2839 VoEId(_instanceId,_channelId),
2840 "GetRxNsStatus() => enabled=%d, mode=%d", enabled, mode);
2841 return 0;
2842}
2843
2844#endif // #ifdef WEBRTC_VOICE_ENGINE_NR
2845
2846int
niklase@google.com470e71d2011-07-07 08:21:25 +00002847Channel::SetLocalSSRC(unsigned int ssrc)
2848{
2849 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId, _channelId),
2850 "Channel::SetLocalSSRC()");
henrika@webrtc.org944cbeb2014-03-18 10:32:33 +00002851 if (channel_state_.Get().sending)
niklase@google.com470e71d2011-07-07 08:21:25 +00002852 {
2853 _engineStatisticsPtr->SetLastError(
2854 VE_ALREADY_SENDING, kTraceError,
2855 "SetLocalSSRC() already sending");
2856 return -1;
2857 }
stefan@webrtc.orgef927552014-06-05 08:25:29 +00002858 _rtpRtcpModule->SetSSRC(ssrc);
niklase@google.com470e71d2011-07-07 08:21:25 +00002859 return 0;
2860}
2861
2862int
2863Channel::GetLocalSSRC(unsigned int& ssrc)
2864{
pwestin@webrtc.org2853dde2012-05-11 11:08:54 +00002865 ssrc = _rtpRtcpModule->SSRC();
niklase@google.com470e71d2011-07-07 08:21:25 +00002866 WEBRTC_TRACE(kTraceStateInfo, kTraceVoice,
2867 VoEId(_instanceId,_channelId),
2868 "GetLocalSSRC() => ssrc=%lu", ssrc);
2869 return 0;
2870}
2871
2872int
2873Channel::GetRemoteSSRC(unsigned int& ssrc)
2874{
wu@webrtc.org822fbd82013-08-15 23:38:54 +00002875 ssrc = rtp_receiver_->SSRC();
niklase@google.com470e71d2011-07-07 08:21:25 +00002876 WEBRTC_TRACE(kTraceStateInfo, kTraceVoice,
2877 VoEId(_instanceId,_channelId),
2878 "GetRemoteSSRC() => ssrc=%lu", ssrc);
2879 return 0;
2880}
2881
wu@webrtc.orgebdb0e32014-03-06 23:49:08 +00002882int Channel::SetSendAudioLevelIndicationStatus(bool enable, unsigned char id) {
andrew@webrtc.orgf3930e92013-09-18 22:37:32 +00002883 _includeAudioLevelIndication = enable;
wu@webrtc.orgebdb0e32014-03-06 23:49:08 +00002884 return SetSendRtpHeaderExtension(enable, kRtpExtensionAudioLevel, id);
niklase@google.com470e71d2011-07-07 08:21:25 +00002885}
andrew@webrtc.orgf3930e92013-09-18 22:37:32 +00002886
wu@webrtc.org93fd25c2014-04-24 20:33:08 +00002887int Channel::SetReceiveAudioLevelIndicationStatus(bool enable,
2888 unsigned char id) {
2889 rtp_header_parser_->DeregisterRtpHeaderExtension(
2890 kRtpExtensionAudioLevel);
2891 if (enable && !rtp_header_parser_->RegisterRtpHeaderExtension(
2892 kRtpExtensionAudioLevel, id)) {
2893 return -1;
2894 }
2895 return 0;
2896}
2897
wu@webrtc.orgebdb0e32014-03-06 23:49:08 +00002898int Channel::SetSendAbsoluteSenderTimeStatus(bool enable, unsigned char id) {
2899 return SetSendRtpHeaderExtension(enable, kRtpExtensionAbsoluteSendTime, id);
2900}
2901
2902int Channel::SetReceiveAbsoluteSenderTimeStatus(bool enable, unsigned char id) {
2903 rtp_header_parser_->DeregisterRtpHeaderExtension(
2904 kRtpExtensionAbsoluteSendTime);
solenberg@webrtc.orgb1f50102014-03-24 10:38:25 +00002905 if (enable && !rtp_header_parser_->RegisterRtpHeaderExtension(
2906 kRtpExtensionAbsoluteSendTime, id)) {
2907 return -1;
wu@webrtc.orgebdb0e32014-03-06 23:49:08 +00002908 }
2909 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +00002910}
2911
pbos@webrtc.orgd16e8392014-12-19 13:49:55 +00002912void Channel::SetRTCPStatus(bool enable) {
2913 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId, _channelId),
2914 "Channel::SetRTCPStatus()");
2915 _rtpRtcpModule->SetRTCPStatus(enable ? kRtcpCompound : kRtcpOff);
niklase@google.com470e71d2011-07-07 08:21:25 +00002916}
2917
2918int
2919Channel::GetRTCPStatus(bool& enabled)
2920{
pwestin@webrtc.org2853dde2012-05-11 11:08:54 +00002921 RTCPMethod method = _rtpRtcpModule->RTCP();
niklase@google.com470e71d2011-07-07 08:21:25 +00002922 enabled = (method != kRtcpOff);
2923 WEBRTC_TRACE(kTraceStateInfo, kTraceVoice,
2924 VoEId(_instanceId,_channelId),
2925 "GetRTCPStatus() => enabled=%d", enabled);
2926 return 0;
2927}
2928
2929int
2930Channel::SetRTCP_CNAME(const char cName[256])
2931{
2932 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId, _channelId),
2933 "Channel::SetRTCP_CNAME()");
pwestin@webrtc.org2853dde2012-05-11 11:08:54 +00002934 if (_rtpRtcpModule->SetCNAME(cName) != 0)
niklase@google.com470e71d2011-07-07 08:21:25 +00002935 {
2936 _engineStatisticsPtr->SetLastError(
2937 VE_RTP_RTCP_MODULE_ERROR, kTraceError,
2938 "SetRTCP_CNAME() failed to set RTCP CNAME");
2939 return -1;
2940 }
2941 return 0;
2942}
2943
2944int
niklase@google.com470e71d2011-07-07 08:21:25 +00002945Channel::GetRemoteRTCP_CNAME(char cName[256])
2946{
2947 if (cName == NULL)
2948 {
2949 _engineStatisticsPtr->SetLastError(
2950 VE_INVALID_ARGUMENT, kTraceError,
2951 "GetRemoteRTCP_CNAME() invalid CNAME input buffer");
2952 return -1;
2953 }
leozwang@webrtc.org813e4b02012-03-01 18:34:25 +00002954 char cname[RTCP_CNAME_SIZE];
wu@webrtc.org822fbd82013-08-15 23:38:54 +00002955 const uint32_t remoteSSRC = rtp_receiver_->SSRC();
pwestin@webrtc.org2853dde2012-05-11 11:08:54 +00002956 if (_rtpRtcpModule->RemoteCNAME(remoteSSRC, cname) != 0)
niklase@google.com470e71d2011-07-07 08:21:25 +00002957 {
2958 _engineStatisticsPtr->SetLastError(
2959 VE_CANNOT_RETRIEVE_CNAME, kTraceError,
2960 "GetRemoteRTCP_CNAME() failed to retrieve remote RTCP CNAME");
2961 return -1;
2962 }
2963 strcpy(cName, cname);
2964 WEBRTC_TRACE(kTraceStateInfo, kTraceVoice,
2965 VoEId(_instanceId, _channelId),
2966 "GetRemoteRTCP_CNAME() => cName=%s", cName);
2967 return 0;
2968}
2969
2970int
2971Channel::GetRemoteRTCPData(
2972 unsigned int& NTPHigh,
2973 unsigned int& NTPLow,
2974 unsigned int& timestamp,
2975 unsigned int& playoutTimestamp,
2976 unsigned int* jitter,
2977 unsigned short* fractionLost)
2978{
2979 // --- Information from sender info in received Sender Reports
2980
2981 RTCPSenderInfo senderInfo;
pwestin@webrtc.org2853dde2012-05-11 11:08:54 +00002982 if (_rtpRtcpModule->RemoteRTCPStat(&senderInfo) != 0)
niklase@google.com470e71d2011-07-07 08:21:25 +00002983 {
2984 _engineStatisticsPtr->SetLastError(
2985 VE_RTP_RTCP_MODULE_ERROR, kTraceError,
wu@webrtc.orgfcd12b32011-09-15 20:49:50 +00002986 "GetRemoteRTCPData() failed to retrieve sender info for remote "
niklase@google.com470e71d2011-07-07 08:21:25 +00002987 "side");
2988 return -1;
2989 }
2990
2991 // We only utilize 12 out of 20 bytes in the sender info (ignores packet
2992 // and octet count)
2993 NTPHigh = senderInfo.NTPseconds;
2994 NTPLow = senderInfo.NTPfraction;
2995 timestamp = senderInfo.RTPtimeStamp;
2996
2997 WEBRTC_TRACE(kTraceStateInfo, kTraceVoice,
2998 VoEId(_instanceId, _channelId),
2999 "GetRemoteRTCPData() => NTPHigh=%lu, NTPLow=%lu, "
3000 "timestamp=%lu",
3001 NTPHigh, NTPLow, timestamp);
3002
3003 // --- Locally derived information
3004
3005 // This value is updated on each incoming RTCP packet (0 when no packet
3006 // has been received)
pwestin@webrtc.org1de01352013-04-11 20:23:35 +00003007 playoutTimestamp = playout_timestamp_rtcp_;
niklase@google.com470e71d2011-07-07 08:21:25 +00003008
3009 WEBRTC_TRACE(kTraceStateInfo, kTraceVoice,
3010 VoEId(_instanceId, _channelId),
3011 "GetRemoteRTCPData() => playoutTimestamp=%lu",
pwestin@webrtc.org1de01352013-04-11 20:23:35 +00003012 playout_timestamp_rtcp_);
niklase@google.com470e71d2011-07-07 08:21:25 +00003013
3014 if (NULL != jitter || NULL != fractionLost)
3015 {
perkj@webrtc.orgce5990c2012-01-11 13:00:08 +00003016 // Get all RTCP receiver report blocks that have been received on this
3017 // channel. If we receive RTP packets from a remote source we know the
3018 // remote SSRC and use the report block from him.
3019 // Otherwise use the first report block.
3020 std::vector<RTCPReportBlock> remote_stats;
pwestin@webrtc.org2853dde2012-05-11 11:08:54 +00003021 if (_rtpRtcpModule->RemoteRTCPStat(&remote_stats) != 0 ||
perkj@webrtc.orgce5990c2012-01-11 13:00:08 +00003022 remote_stats.empty()) {
3023 WEBRTC_TRACE(kTraceWarning, kTraceVoice,
3024 VoEId(_instanceId, _channelId),
3025 "GetRemoteRTCPData() failed to measure statistics due"
3026 " to lack of received RTP and/or RTCP packets");
3027 return -1;
niklase@google.com470e71d2011-07-07 08:21:25 +00003028 }
perkj@webrtc.orgce5990c2012-01-11 13:00:08 +00003029
wu@webrtc.org822fbd82013-08-15 23:38:54 +00003030 uint32_t remoteSSRC = rtp_receiver_->SSRC();
perkj@webrtc.orgce5990c2012-01-11 13:00:08 +00003031 std::vector<RTCPReportBlock>::const_iterator it = remote_stats.begin();
3032 for (; it != remote_stats.end(); ++it) {
3033 if (it->remoteSSRC == remoteSSRC)
3034 break;
niklase@google.com470e71d2011-07-07 08:21:25 +00003035 }
perkj@webrtc.orgce5990c2012-01-11 13:00:08 +00003036
3037 if (it == remote_stats.end()) {
3038 // If we have not received any RTCP packets from this SSRC it probably
3039 // means that we have not received any RTP packets.
3040 // Use the first received report block instead.
3041 it = remote_stats.begin();
3042 remoteSSRC = it->remoteSSRC;
niklase@google.com470e71d2011-07-07 08:21:25 +00003043 }
perkj@webrtc.orgce5990c2012-01-11 13:00:08 +00003044
xians@webrtc.org79af7342012-01-31 12:22:14 +00003045 if (jitter) {
3046 *jitter = it->jitter;
3047 WEBRTC_TRACE(kTraceStateInfo, kTraceVoice,
3048 VoEId(_instanceId, _channelId),
3049 "GetRemoteRTCPData() => jitter = %lu", *jitter);
3050 }
perkj@webrtc.orgce5990c2012-01-11 13:00:08 +00003051
xians@webrtc.org79af7342012-01-31 12:22:14 +00003052 if (fractionLost) {
3053 *fractionLost = it->fractionLost;
3054 WEBRTC_TRACE(kTraceStateInfo, kTraceVoice,
3055 VoEId(_instanceId, _channelId),
3056 "GetRemoteRTCPData() => fractionLost = %lu",
3057 *fractionLost);
3058 }
niklase@google.com470e71d2011-07-07 08:21:25 +00003059 }
3060 return 0;
3061}
3062
3063int
pbos@webrtc.org92135212013-05-14 08:31:39 +00003064Channel::SendApplicationDefinedRTCPPacket(unsigned char subType,
niklase@google.com470e71d2011-07-07 08:21:25 +00003065 unsigned int name,
3066 const char* data,
3067 unsigned short dataLengthInBytes)
3068{
3069 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId, _channelId),
3070 "Channel::SendApplicationDefinedRTCPPacket()");
henrika@webrtc.org944cbeb2014-03-18 10:32:33 +00003071 if (!channel_state_.Get().sending)
niklase@google.com470e71d2011-07-07 08:21:25 +00003072 {
3073 _engineStatisticsPtr->SetLastError(
3074 VE_NOT_SENDING, kTraceError,
3075 "SendApplicationDefinedRTCPPacket() not sending");
3076 return -1;
3077 }
3078 if (NULL == data)
3079 {
3080 _engineStatisticsPtr->SetLastError(
3081 VE_INVALID_ARGUMENT, kTraceError,
3082 "SendApplicationDefinedRTCPPacket() invalid data value");
3083 return -1;
3084 }
3085 if (dataLengthInBytes % 4 != 0)
3086 {
3087 _engineStatisticsPtr->SetLastError(
3088 VE_INVALID_ARGUMENT, kTraceError,
3089 "SendApplicationDefinedRTCPPacket() invalid length value");
3090 return -1;
3091 }
pwestin@webrtc.org2853dde2012-05-11 11:08:54 +00003092 RTCPMethod status = _rtpRtcpModule->RTCP();
niklase@google.com470e71d2011-07-07 08:21:25 +00003093 if (status == kRtcpOff)
3094 {
3095 _engineStatisticsPtr->SetLastError(
3096 VE_RTCP_ERROR, kTraceError,
3097 "SendApplicationDefinedRTCPPacket() RTCP is disabled");
3098 return -1;
3099 }
3100
3101 // Create and schedule the RTCP APP packet for transmission
pwestin@webrtc.org2853dde2012-05-11 11:08:54 +00003102 if (_rtpRtcpModule->SetRTCPApplicationSpecificData(
niklase@google.com470e71d2011-07-07 08:21:25 +00003103 subType,
3104 name,
3105 (const unsigned char*) data,
3106 dataLengthInBytes) != 0)
3107 {
3108 _engineStatisticsPtr->SetLastError(
3109 VE_SEND_ERROR, kTraceError,
3110 "SendApplicationDefinedRTCPPacket() failed to send RTCP packet");
3111 return -1;
3112 }
3113 return 0;
3114}
3115
3116int
3117Channel::GetRTPStatistics(
3118 unsigned int& averageJitterMs,
3119 unsigned int& maxJitterMs,
3120 unsigned int& discardedPackets)
3121{
niklase@google.com470e71d2011-07-07 08:21:25 +00003122 // The jitter statistics is updated for each received RTP packet and is
3123 // based on received packets.
sprang@webrtc.org54ae4ff2013-12-19 13:26:02 +00003124 if (_rtpRtcpModule->RTCP() == kRtcpOff) {
3125 // If RTCP is off, there is no timed thread in the RTCP module regularly
3126 // generating new stats, trigger the update manually here instead.
3127 StreamStatistician* statistician =
3128 rtp_receive_statistics_->GetStatistician(rtp_receiver_->SSRC());
3129 if (statistician) {
3130 // Don't use returned statistics, use data from proxy instead so that
3131 // max jitter can be fetched atomically.
3132 RtcpStatistics s;
3133 statistician->GetStatistics(&s, true);
3134 }
niklase@google.com470e71d2011-07-07 08:21:25 +00003135 }
3136
sprang@webrtc.org54ae4ff2013-12-19 13:26:02 +00003137 ChannelStatistics stats = statistics_proxy_->GetStats();
andrew@webrtc.orgeb524d92013-09-23 23:02:24 +00003138 const int32_t playoutFrequency = audio_coding_->PlayoutFrequency();
sprang@webrtc.org54ae4ff2013-12-19 13:26:02 +00003139 if (playoutFrequency > 0) {
3140 // Scale RTP statistics given the current playout frequency
3141 maxJitterMs = stats.max_jitter / (playoutFrequency / 1000);
3142 averageJitterMs = stats.rtcp.jitter / (playoutFrequency / 1000);
niklase@google.com470e71d2011-07-07 08:21:25 +00003143 }
3144
3145 discardedPackets = _numberOfDiscardedPackets;
3146
3147 WEBRTC_TRACE(kTraceStateInfo, kTraceVoice,
3148 VoEId(_instanceId, _channelId),
3149 "GetRTPStatistics() => averageJitterMs = %lu, maxJitterMs = %lu,"
wu@webrtc.orgfcd12b32011-09-15 20:49:50 +00003150 " discardedPackets = %lu)",
niklase@google.com470e71d2011-07-07 08:21:25 +00003151 averageJitterMs, maxJitterMs, discardedPackets);
3152 return 0;
3153}
3154
henrika@webrtc.org8a2fc882012-08-22 08:53:55 +00003155int Channel::GetRemoteRTCPReportBlocks(
3156 std::vector<ReportBlock>* report_blocks) {
3157 if (report_blocks == NULL) {
3158 _engineStatisticsPtr->SetLastError(VE_INVALID_ARGUMENT, kTraceError,
3159 "GetRemoteRTCPReportBlock()s invalid report_blocks.");
3160 return -1;
3161 }
3162
3163 // Get the report blocks from the latest received RTCP Sender or Receiver
3164 // Report. Each element in the vector contains the sender's SSRC and a
3165 // report block according to RFC 3550.
3166 std::vector<RTCPReportBlock> rtcp_report_blocks;
3167 if (_rtpRtcpModule->RemoteRTCPStat(&rtcp_report_blocks) != 0) {
3168 _engineStatisticsPtr->SetLastError(VE_RTP_RTCP_MODULE_ERROR, kTraceError,
3169 "GetRemoteRTCPReportBlocks() failed to read RTCP SR/RR report block.");
3170 return -1;
3171 }
3172
3173 if (rtcp_report_blocks.empty())
3174 return 0;
3175
3176 std::vector<RTCPReportBlock>::const_iterator it = rtcp_report_blocks.begin();
3177 for (; it != rtcp_report_blocks.end(); ++it) {
3178 ReportBlock report_block;
3179 report_block.sender_SSRC = it->remoteSSRC;
3180 report_block.source_SSRC = it->sourceSSRC;
3181 report_block.fraction_lost = it->fractionLost;
3182 report_block.cumulative_num_packets_lost = it->cumulativeLost;
3183 report_block.extended_highest_sequence_number = it->extendedHighSeqNum;
3184 report_block.interarrival_jitter = it->jitter;
3185 report_block.last_SR_timestamp = it->lastSR;
3186 report_block.delay_since_last_SR = it->delaySinceLastSR;
3187 report_blocks->push_back(report_block);
3188 }
3189 return 0;
3190}
3191
niklase@google.com470e71d2011-07-07 08:21:25 +00003192int
3193Channel::GetRTPStatistics(CallStatistics& stats)
3194{
wu@webrtc.orgcb711f72014-05-19 17:39:11 +00003195 // --- RtcpStatistics
niklase@google.com470e71d2011-07-07 08:21:25 +00003196
3197 // The jitter statistics is updated for each received RTP packet and is
3198 // based on received packets.
sprang@webrtc.org54ae4ff2013-12-19 13:26:02 +00003199 RtcpStatistics statistics;
stefan@webrtc.org286fe0b2013-08-21 20:58:21 +00003200 StreamStatistician* statistician =
3201 rtp_receive_statistics_->GetStatistician(rtp_receiver_->SSRC());
3202 if (!statistician || !statistician->GetStatistics(
wu@webrtc.org822fbd82013-08-15 23:38:54 +00003203 &statistics, _rtpRtcpModule->RTCP() == kRtcpOff)) {
3204 _engineStatisticsPtr->SetLastError(
3205 VE_CANNOT_RETRIEVE_RTP_STAT, kTraceWarning,
3206 "GetRTPStatistics() failed to read RTP statistics from the "
3207 "RTP/RTCP module");
niklase@google.com470e71d2011-07-07 08:21:25 +00003208 }
3209
wu@webrtc.org822fbd82013-08-15 23:38:54 +00003210 stats.fractionLost = statistics.fraction_lost;
3211 stats.cumulativeLost = statistics.cumulative_lost;
3212 stats.extendedMax = statistics.extended_max_sequence_number;
3213 stats.jitterSamples = statistics.jitter;
niklase@google.com470e71d2011-07-07 08:21:25 +00003214
3215 WEBRTC_TRACE(kTraceStateInfo, kTraceVoice,
3216 VoEId(_instanceId, _channelId),
3217 "GetRTPStatistics() => fractionLost=%lu, cumulativeLost=%lu,"
wu@webrtc.orgfcd12b32011-09-15 20:49:50 +00003218 " extendedMax=%lu, jitterSamples=%li)",
niklase@google.com470e71d2011-07-07 08:21:25 +00003219 stats.fractionLost, stats.cumulativeLost, stats.extendedMax,
3220 stats.jitterSamples);
3221
wu@webrtc.orgcb711f72014-05-19 17:39:11 +00003222 // --- RTT
minyue@webrtc.org2b58a442014-09-11 07:51:53 +00003223 stats.rttMs = GetRTT();
minyue@webrtc.org6fd93082014-12-15 14:56:44 +00003224 if (stats.rttMs == 0) {
3225 WEBRTC_TRACE(kTraceWarning, kTraceVoice, VoEId(_instanceId, _channelId),
3226 "GetRTPStatistics() failed to get RTT");
3227 } else {
3228 WEBRTC_TRACE(kTraceStateInfo, kTraceVoice, VoEId(_instanceId, _channelId),
pkasting@chromium.org16825b12015-01-12 21:51:21 +00003229 "GetRTPStatistics() => rttMs=%" PRId64, stats.rttMs);
minyue@webrtc.org6fd93082014-12-15 14:56:44 +00003230 }
niklase@google.com470e71d2011-07-07 08:21:25 +00003231
wu@webrtc.orgcb711f72014-05-19 17:39:11 +00003232 // --- Data counters
niklase@google.com470e71d2011-07-07 08:21:25 +00003233
pkasting@chromium.org4591fbd2014-11-20 22:28:14 +00003234 size_t bytesSent(0);
pbos@webrtc.org6141e132013-04-09 10:09:10 +00003235 uint32_t packetsSent(0);
pkasting@chromium.org4591fbd2014-11-20 22:28:14 +00003236 size_t bytesReceived(0);
pbos@webrtc.org6141e132013-04-09 10:09:10 +00003237 uint32_t packetsReceived(0);
niklase@google.com470e71d2011-07-07 08:21:25 +00003238
stefan@webrtc.org286fe0b2013-08-21 20:58:21 +00003239 if (statistician) {
3240 statistician->GetDataCounters(&bytesReceived, &packetsReceived);
3241 }
wu@webrtc.org822fbd82013-08-15 23:38:54 +00003242
pwestin@webrtc.org2853dde2012-05-11 11:08:54 +00003243 if (_rtpRtcpModule->DataCountersRTP(&bytesSent,
wu@webrtc.org822fbd82013-08-15 23:38:54 +00003244 &packetsSent) != 0)
niklase@google.com470e71d2011-07-07 08:21:25 +00003245 {
3246 WEBRTC_TRACE(kTraceWarning, kTraceVoice,
3247 VoEId(_instanceId, _channelId),
3248 "GetRTPStatistics() failed to retrieve RTP datacounters =>"
wu@webrtc.orgfcd12b32011-09-15 20:49:50 +00003249 " output will not be complete");
niklase@google.com470e71d2011-07-07 08:21:25 +00003250 }
3251
3252 stats.bytesSent = bytesSent;
3253 stats.packetsSent = packetsSent;
3254 stats.bytesReceived = bytesReceived;
3255 stats.packetsReceived = packetsReceived;
3256
3257 WEBRTC_TRACE(kTraceStateInfo, kTraceVoice,
3258 VoEId(_instanceId, _channelId),
pkasting@chromium.org4591fbd2014-11-20 22:28:14 +00003259 "GetRTPStatistics() => bytesSent=%" PRIuS ", packetsSent=%d,"
3260 " bytesReceived=%" PRIuS ", packetsReceived=%d)",
niklase@google.com470e71d2011-07-07 08:21:25 +00003261 stats.bytesSent, stats.packetsSent, stats.bytesReceived,
3262 stats.packetsReceived);
3263
wu@webrtc.orgcb711f72014-05-19 17:39:11 +00003264 // --- Timestamps
3265 {
3266 CriticalSectionScoped lock(ts_stats_lock_.get());
3267 stats.capture_start_ntp_time_ms_ = capture_start_ntp_time_ms_;
3268 }
niklase@google.com470e71d2011-07-07 08:21:25 +00003269 return 0;
3270}
3271
minyue@webrtc.orgc1a40a72014-05-28 09:52:06 +00003272int Channel::SetREDStatus(bool enable, int redPayloadtype) {
turaj@webrtc.org42259e72012-12-11 02:15:12 +00003273 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId, _channelId),
minyue@webrtc.orgc1a40a72014-05-28 09:52:06 +00003274 "Channel::SetREDStatus()");
niklase@google.com470e71d2011-07-07 08:21:25 +00003275
turaj@webrtc.org8c8ad852013-01-31 18:20:17 +00003276 if (enable) {
3277 if (redPayloadtype < 0 || redPayloadtype > 127) {
3278 _engineStatisticsPtr->SetLastError(
3279 VE_PLTYPE_ERROR, kTraceError,
minyue@webrtc.orgc1a40a72014-05-28 09:52:06 +00003280 "SetREDStatus() invalid RED payload type");
turaj@webrtc.org8c8ad852013-01-31 18:20:17 +00003281 return -1;
3282 }
3283
3284 if (SetRedPayloadType(redPayloadtype) < 0) {
3285 _engineStatisticsPtr->SetLastError(
3286 VE_CODEC_ERROR, kTraceError,
3287 "SetSecondarySendCodec() Failed to register RED ACM");
3288 return -1;
3289 }
turaj@webrtc.org42259e72012-12-11 02:15:12 +00003290 }
niklase@google.com470e71d2011-07-07 08:21:25 +00003291
minyue@webrtc.orgaa5ea1c2014-05-23 15:16:51 +00003292 if (audio_coding_->SetREDStatus(enable) != 0) {
turaj@webrtc.org42259e72012-12-11 02:15:12 +00003293 _engineStatisticsPtr->SetLastError(
3294 VE_AUDIO_CODING_MODULE_ERROR, kTraceError,
minyue@webrtc.orgaa5ea1c2014-05-23 15:16:51 +00003295 "SetREDStatus() failed to set RED state in the ACM");
turaj@webrtc.org42259e72012-12-11 02:15:12 +00003296 return -1;
3297 }
3298 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +00003299}
3300
3301int
minyue@webrtc.orgc1a40a72014-05-28 09:52:06 +00003302Channel::GetREDStatus(bool& enabled, int& redPayloadtype)
niklase@google.com470e71d2011-07-07 08:21:25 +00003303{
minyue@webrtc.orgaa5ea1c2014-05-23 15:16:51 +00003304 enabled = audio_coding_->REDStatus();
niklase@google.com470e71d2011-07-07 08:21:25 +00003305 if (enabled)
3306 {
pbos@webrtc.org6141e132013-04-09 10:09:10 +00003307 int8_t payloadType(0);
pwestin@webrtc.org2853dde2012-05-11 11:08:54 +00003308 if (_rtpRtcpModule->SendREDPayloadType(payloadType) != 0)
niklase@google.com470e71d2011-07-07 08:21:25 +00003309 {
3310 _engineStatisticsPtr->SetLastError(
3311 VE_RTP_RTCP_MODULE_ERROR, kTraceError,
minyue@webrtc.orgc1a40a72014-05-28 09:52:06 +00003312 "GetREDStatus() failed to retrieve RED PT from RTP/RTCP "
niklase@google.com470e71d2011-07-07 08:21:25 +00003313 "module");
3314 return -1;
3315 }
pkasting@chromium.orgdf9a41d2015-01-26 22:35:29 +00003316 redPayloadtype = payloadType;
niklase@google.com470e71d2011-07-07 08:21:25 +00003317 WEBRTC_TRACE(kTraceStateInfo, kTraceVoice,
3318 VoEId(_instanceId, _channelId),
minyue@webrtc.orgc1a40a72014-05-28 09:52:06 +00003319 "GetREDStatus() => enabled=%d, redPayloadtype=%d",
niklase@google.com470e71d2011-07-07 08:21:25 +00003320 enabled, redPayloadtype);
3321 return 0;
3322 }
3323 WEBRTC_TRACE(kTraceStateInfo, kTraceVoice,
3324 VoEId(_instanceId, _channelId),
minyue@webrtc.orgc1a40a72014-05-28 09:52:06 +00003325 "GetREDStatus() => enabled=%d", enabled);
niklase@google.com470e71d2011-07-07 08:21:25 +00003326 return 0;
3327}
3328
minyue@webrtc.orgc1a40a72014-05-28 09:52:06 +00003329int Channel::SetCodecFECStatus(bool enable) {
3330 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId, _channelId),
3331 "Channel::SetCodecFECStatus()");
3332
3333 if (audio_coding_->SetCodecFEC(enable) != 0) {
3334 _engineStatisticsPtr->SetLastError(
3335 VE_AUDIO_CODING_MODULE_ERROR, kTraceError,
3336 "SetCodecFECStatus() failed to set FEC state");
3337 return -1;
3338 }
3339 return 0;
3340}
3341
3342bool Channel::GetCodecFECStatus() {
3343 bool enabled = audio_coding_->CodecFEC();
3344 WEBRTC_TRACE(kTraceStateInfo, kTraceVoice,
3345 VoEId(_instanceId, _channelId),
3346 "GetCodecFECStatus() => enabled=%d", enabled);
3347 return enabled;
3348}
3349
pwestin@webrtc.orgdb249952013-06-05 15:33:20 +00003350void Channel::SetNACKStatus(bool enable, int maxNumberOfPackets) {
3351 // None of these functions can fail.
3352 _rtpRtcpModule->SetStorePacketsStatus(enable, maxNumberOfPackets);
stefan@webrtc.org7bb8f022013-09-06 13:40:11 +00003353 rtp_receive_statistics_->SetMaxReorderingThreshold(maxNumberOfPackets);
3354 rtp_receiver_->SetNACKStatus(enable ? kNackRtcp : kNackOff);
pwestin@webrtc.orgd30859e2013-06-06 21:09:01 +00003355 if (enable)
andrew@webrtc.orgeb524d92013-09-23 23:02:24 +00003356 audio_coding_->EnableNack(maxNumberOfPackets);
pwestin@webrtc.orgd30859e2013-06-06 21:09:01 +00003357 else
andrew@webrtc.orgeb524d92013-09-23 23:02:24 +00003358 audio_coding_->DisableNack();
pwestin@webrtc.orgdb249952013-06-05 15:33:20 +00003359}
3360
pwestin@webrtc.orgd30859e2013-06-06 21:09:01 +00003361// Called when we are missing one or more packets.
3362int Channel::ResendPackets(const uint16_t* sequence_numbers, int length) {
pwestin@webrtc.orgdb249952013-06-05 15:33:20 +00003363 return _rtpRtcpModule->SendNACK(sequence_numbers, length);
3364}
3365
niklase@google.com470e71d2011-07-07 08:21:25 +00003366int
niklase@google.com470e71d2011-07-07 08:21:25 +00003367Channel::StartRTPDump(const char fileNameUTF8[1024],
3368 RTPDirections direction)
3369{
3370 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId, _channelId),
3371 "Channel::StartRTPDump()");
3372 if ((direction != kRtpIncoming) && (direction != kRtpOutgoing))
3373 {
3374 _engineStatisticsPtr->SetLastError(
3375 VE_INVALID_ARGUMENT, kTraceError,
3376 "StartRTPDump() invalid RTP direction");
3377 return -1;
3378 }
3379 RtpDump* rtpDumpPtr = (direction == kRtpIncoming) ?
3380 &_rtpDumpIn : &_rtpDumpOut;
3381 if (rtpDumpPtr == NULL)
3382 {
3383 assert(false);
3384 return -1;
3385 }
3386 if (rtpDumpPtr->IsActive())
3387 {
3388 rtpDumpPtr->Stop();
3389 }
3390 if (rtpDumpPtr->Start(fileNameUTF8) != 0)
3391 {
3392 _engineStatisticsPtr->SetLastError(
3393 VE_BAD_FILE, kTraceError,
3394 "StartRTPDump() failed to create file");
3395 return -1;
3396 }
3397 return 0;
3398}
3399
3400int
3401Channel::StopRTPDump(RTPDirections direction)
3402{
3403 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId, _channelId),
3404 "Channel::StopRTPDump()");
3405 if ((direction != kRtpIncoming) && (direction != kRtpOutgoing))
3406 {
3407 _engineStatisticsPtr->SetLastError(
3408 VE_INVALID_ARGUMENT, kTraceError,
3409 "StopRTPDump() invalid RTP direction");
3410 return -1;
3411 }
3412 RtpDump* rtpDumpPtr = (direction == kRtpIncoming) ?
3413 &_rtpDumpIn : &_rtpDumpOut;
3414 if (rtpDumpPtr == NULL)
3415 {
3416 assert(false);
3417 return -1;
3418 }
3419 if (!rtpDumpPtr->IsActive())
3420 {
3421 return 0;
3422 }
3423 return rtpDumpPtr->Stop();
3424}
3425
3426bool
3427Channel::RTPDumpIsActive(RTPDirections direction)
3428{
3429 if ((direction != kRtpIncoming) &&
3430 (direction != kRtpOutgoing))
3431 {
3432 _engineStatisticsPtr->SetLastError(
3433 VE_INVALID_ARGUMENT, kTraceError,
3434 "RTPDumpIsActive() invalid RTP direction");
3435 return false;
3436 }
3437 RtpDump* rtpDumpPtr = (direction == kRtpIncoming) ?
3438 &_rtpDumpIn : &_rtpDumpOut;
3439 return rtpDumpPtr->IsActive();
3440}
3441
solenberg@webrtc.orgb1f50102014-03-24 10:38:25 +00003442void Channel::SetVideoEngineBWETarget(ViENetwork* vie_network,
3443 int video_channel) {
3444 CriticalSectionScoped cs(&_callbackCritSect);
3445 if (vie_network_) {
3446 vie_network_->Release();
3447 vie_network_ = NULL;
3448 }
3449 video_channel_ = -1;
3450
3451 if (vie_network != NULL && video_channel != -1) {
3452 vie_network_ = vie_network;
3453 video_channel_ = video_channel;
3454 }
3455}
3456
pbos@webrtc.org6141e132013-04-09 10:09:10 +00003457uint32_t
andrew@webrtc.org755b04a2011-11-15 16:57:56 +00003458Channel::Demultiplex(const AudioFrame& audioFrame)
niklase@google.com470e71d2011-07-07 08:21:25 +00003459{
3460 WEBRTC_TRACE(kTraceStream, kTraceVoice, VoEId(_instanceId,_channelId),
andrew@webrtc.org755b04a2011-11-15 16:57:56 +00003461 "Channel::Demultiplex()");
andrew@webrtc.orgae1a58b2013-01-22 04:44:30 +00003462 _audioFrame.CopyFrom(audioFrame);
andrew@webrtc.org63a50982012-05-02 23:56:37 +00003463 _audioFrame.id_ = _channelId;
niklase@google.com470e71d2011-07-07 08:21:25 +00003464 return 0;
3465}
3466
xians@webrtc.org2f84afa2013-07-31 16:23:37 +00003467void Channel::Demultiplex(const int16_t* audio_data,
xians@webrtc.org8fff1f02013-07-31 16:27:42 +00003468 int sample_rate,
xians@webrtc.org2f84afa2013-07-31 16:23:37 +00003469 int number_of_frames,
xians@webrtc.org8fff1f02013-07-31 16:27:42 +00003470 int number_of_channels) {
xians@webrtc.org2f84afa2013-07-31 16:23:37 +00003471 CodecInst codec;
3472 GetSendCodec(codec);
xians@webrtc.org2f84afa2013-07-31 16:23:37 +00003473
andrew@webrtc.org40ee3d02014-04-03 21:56:01 +00003474 if (!mono_recording_audio_.get()) {
3475 // Temporary space for DownConvertToCodecFormat.
3476 mono_recording_audio_.reset(new int16_t[kMaxMonoDataSizeSamples]);
xians@webrtc.org2f84afa2013-07-31 16:23:37 +00003477 }
andrew@webrtc.org40ee3d02014-04-03 21:56:01 +00003478 DownConvertToCodecFormat(audio_data,
3479 number_of_frames,
3480 number_of_channels,
3481 sample_rate,
3482 codec.channels,
3483 codec.plfreq,
3484 mono_recording_audio_.get(),
3485 &input_resampler_,
3486 &_audioFrame);
xians@webrtc.org2f84afa2013-07-31 16:23:37 +00003487}
3488
pbos@webrtc.org6141e132013-04-09 10:09:10 +00003489uint32_t
xians@google.com0b0665a2011-08-08 08:18:44 +00003490Channel::PrepareEncodeAndSend(int mixingFrequency)
niklase@google.com470e71d2011-07-07 08:21:25 +00003491{
3492 WEBRTC_TRACE(kTraceStream, kTraceVoice, VoEId(_instanceId,_channelId),
3493 "Channel::PrepareEncodeAndSend()");
3494
andrew@webrtc.org63a50982012-05-02 23:56:37 +00003495 if (_audioFrame.samples_per_channel_ == 0)
niklase@google.com470e71d2011-07-07 08:21:25 +00003496 {
3497 WEBRTC_TRACE(kTraceWarning, kTraceVoice, VoEId(_instanceId,_channelId),
3498 "Channel::PrepareEncodeAndSend() invalid audio frame");
tommi@webrtc.orgeec6ecd2014-07-11 19:09:59 +00003499 return 0xFFFFFFFF;
niklase@google.com470e71d2011-07-07 08:21:25 +00003500 }
3501
henrika@webrtc.org944cbeb2014-03-18 10:32:33 +00003502 if (channel_state_.Get().input_file_playing)
niklase@google.com470e71d2011-07-07 08:21:25 +00003503 {
3504 MixOrReplaceAudioWithFile(mixingFrequency);
3505 }
3506
andrew@webrtc.org21299d42014-05-14 19:00:59 +00003507 bool is_muted = Mute(); // Cache locally as Mute() takes a lock.
3508 if (is_muted) {
3509 AudioFrameOperations::Mute(_audioFrame);
niklase@google.com470e71d2011-07-07 08:21:25 +00003510 }
3511
henrika@webrtc.org944cbeb2014-03-18 10:32:33 +00003512 if (channel_state_.Get().input_external_media)
niklase@google.com470e71d2011-07-07 08:21:25 +00003513 {
mflodman@webrtc.org9a065d12012-03-07 08:12:21 +00003514 CriticalSectionScoped cs(&_callbackCritSect);
andrew@webrtc.org63a50982012-05-02 23:56:37 +00003515 const bool isStereo = (_audioFrame.num_channels_ == 2);
niklase@google.com470e71d2011-07-07 08:21:25 +00003516 if (_inputExternalMediaCallbackPtr)
3517 {
3518 _inputExternalMediaCallbackPtr->Process(
3519 _channelId,
3520 kRecordingPerChannel,
pbos@webrtc.org6141e132013-04-09 10:09:10 +00003521 (int16_t*)_audioFrame.data_,
andrew@webrtc.org63a50982012-05-02 23:56:37 +00003522 _audioFrame.samples_per_channel_,
3523 _audioFrame.sample_rate_hz_,
niklase@google.com470e71d2011-07-07 08:21:25 +00003524 isStereo);
3525 }
3526 }
3527
3528 InsertInbandDtmfTone();
3529
andrew@webrtc.org60730cf2014-01-07 17:45:09 +00003530 if (_includeAudioLevelIndication) {
andrew@webrtc.org382c0c22014-05-05 18:22:21 +00003531 int length = _audioFrame.samples_per_channel_ * _audioFrame.num_channels_;
andrew@webrtc.org21299d42014-05-14 19:00:59 +00003532 if (is_muted) {
3533 rms_level_.ProcessMuted(length);
3534 } else {
3535 rms_level_.Process(_audioFrame.data_, length);
3536 }
andrew@webrtc.org755b04a2011-11-15 16:57:56 +00003537 }
3538
niklase@google.com470e71d2011-07-07 08:21:25 +00003539 return 0;
3540}
3541
pbos@webrtc.org6141e132013-04-09 10:09:10 +00003542uint32_t
niklase@google.com470e71d2011-07-07 08:21:25 +00003543Channel::EncodeAndSend()
3544{
3545 WEBRTC_TRACE(kTraceStream, kTraceVoice, VoEId(_instanceId,_channelId),
3546 "Channel::EncodeAndSend()");
3547
andrew@webrtc.org63a50982012-05-02 23:56:37 +00003548 assert(_audioFrame.num_channels_ <= 2);
3549 if (_audioFrame.samples_per_channel_ == 0)
niklase@google.com470e71d2011-07-07 08:21:25 +00003550 {
3551 WEBRTC_TRACE(kTraceWarning, kTraceVoice, VoEId(_instanceId,_channelId),
3552 "Channel::EncodeAndSend() invalid audio frame");
tommi@webrtc.orgeec6ecd2014-07-11 19:09:59 +00003553 return 0xFFFFFFFF;
niklase@google.com470e71d2011-07-07 08:21:25 +00003554 }
3555
andrew@webrtc.org63a50982012-05-02 23:56:37 +00003556 _audioFrame.id_ = _channelId;
niklase@google.com470e71d2011-07-07 08:21:25 +00003557
3558 // --- Add 10ms of raw (PCM) audio data to the encoder @ 32kHz.
3559
3560 // The ACM resamples internally.
andrew@webrtc.org63a50982012-05-02 23:56:37 +00003561 _audioFrame.timestamp_ = _timeStamp;
henrik.lundin@webrtc.orgf56c1622015-03-02 12:29:30 +00003562 // This call will trigger AudioPacketizationCallback::SendData if encoding
3563 // is done and payload is ready for packetization and transmission.
3564 // Otherwise, it will return without invoking the callback.
3565 if (audio_coding_->Add10MsData((AudioFrame&)_audioFrame) < 0)
niklase@google.com470e71d2011-07-07 08:21:25 +00003566 {
3567 WEBRTC_TRACE(kTraceError, kTraceVoice, VoEId(_instanceId,_channelId),
3568 "Channel::EncodeAndSend() ACM encoding failed");
tommi@webrtc.orgeec6ecd2014-07-11 19:09:59 +00003569 return 0xFFFFFFFF;
niklase@google.com470e71d2011-07-07 08:21:25 +00003570 }
3571
andrew@webrtc.org63a50982012-05-02 23:56:37 +00003572 _timeStamp += _audioFrame.samples_per_channel_;
henrik.lundin@webrtc.orgf56c1622015-03-02 12:29:30 +00003573 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +00003574}
3575
3576int Channel::RegisterExternalMediaProcessing(
3577 ProcessingTypes type,
3578 VoEMediaProcess& processObject)
3579{
3580 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId,_channelId),
3581 "Channel::RegisterExternalMediaProcessing()");
3582
mflodman@webrtc.org9a065d12012-03-07 08:12:21 +00003583 CriticalSectionScoped cs(&_callbackCritSect);
niklase@google.com470e71d2011-07-07 08:21:25 +00003584
3585 if (kPlaybackPerChannel == type)
3586 {
3587 if (_outputExternalMediaCallbackPtr)
3588 {
3589 _engineStatisticsPtr->SetLastError(
3590 VE_INVALID_OPERATION, kTraceError,
3591 "Channel::RegisterExternalMediaProcessing() "
3592 "output external media already enabled");
3593 return -1;
3594 }
3595 _outputExternalMediaCallbackPtr = &processObject;
3596 _outputExternalMedia = true;
3597 }
3598 else if (kRecordingPerChannel == type)
3599 {
3600 if (_inputExternalMediaCallbackPtr)
3601 {
3602 _engineStatisticsPtr->SetLastError(
3603 VE_INVALID_OPERATION, kTraceError,
3604 "Channel::RegisterExternalMediaProcessing() "
3605 "output external media already enabled");
3606 return -1;
3607 }
3608 _inputExternalMediaCallbackPtr = &processObject;
henrika@webrtc.org944cbeb2014-03-18 10:32:33 +00003609 channel_state_.SetInputExternalMedia(true);
niklase@google.com470e71d2011-07-07 08:21:25 +00003610 }
3611 return 0;
3612}
3613
3614int Channel::DeRegisterExternalMediaProcessing(ProcessingTypes type)
3615{
3616 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId,_channelId),
3617 "Channel::DeRegisterExternalMediaProcessing()");
3618
mflodman@webrtc.org9a065d12012-03-07 08:12:21 +00003619 CriticalSectionScoped cs(&_callbackCritSect);
niklase@google.com470e71d2011-07-07 08:21:25 +00003620
3621 if (kPlaybackPerChannel == type)
3622 {
3623 if (!_outputExternalMediaCallbackPtr)
3624 {
3625 _engineStatisticsPtr->SetLastError(
3626 VE_INVALID_OPERATION, kTraceWarning,
3627 "Channel::DeRegisterExternalMediaProcessing() "
3628 "output external media already disabled");
3629 return 0;
3630 }
3631 _outputExternalMedia = false;
3632 _outputExternalMediaCallbackPtr = NULL;
3633 }
3634 else if (kRecordingPerChannel == type)
3635 {
3636 if (!_inputExternalMediaCallbackPtr)
3637 {
3638 _engineStatisticsPtr->SetLastError(
3639 VE_INVALID_OPERATION, kTraceWarning,
3640 "Channel::DeRegisterExternalMediaProcessing() "
3641 "input external media already disabled");
3642 return 0;
3643 }
henrika@webrtc.org944cbeb2014-03-18 10:32:33 +00003644 channel_state_.SetInputExternalMedia(false);
niklase@google.com470e71d2011-07-07 08:21:25 +00003645 _inputExternalMediaCallbackPtr = NULL;
3646 }
3647
3648 return 0;
3649}
3650
roosa@google.com1b60ceb2012-12-12 23:00:29 +00003651int Channel::SetExternalMixing(bool enabled) {
3652 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId,_channelId),
3653 "Channel::SetExternalMixing(enabled=%d)", enabled);
3654
henrika@webrtc.org944cbeb2014-03-18 10:32:33 +00003655 if (channel_state_.Get().playing)
roosa@google.com1b60ceb2012-12-12 23:00:29 +00003656 {
3657 _engineStatisticsPtr->SetLastError(
3658 VE_INVALID_OPERATION, kTraceError,
3659 "Channel::SetExternalMixing() "
3660 "external mixing cannot be changed while playing.");
3661 return -1;
3662 }
3663
3664 _externalMixing = enabled;
3665
3666 return 0;
3667}
3668
niklase@google.com470e71d2011-07-07 08:21:25 +00003669int
niklase@google.com470e71d2011-07-07 08:21:25 +00003670Channel::GetNetworkStatistics(NetworkStatistics& stats)
3671{
3672 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId,_channelId),
3673 "Channel::GetNetworkStatistics()");
minyue@webrtc.orgc0bd7be2015-02-18 15:24:13 +00003674 return audio_coding_->GetNetworkStatistics(&stats);
niklase@google.com470e71d2011-07-07 08:21:25 +00003675}
3676
wu@webrtc.org24301a62013-12-13 19:17:43 +00003677void Channel::GetDecodingCallStatistics(AudioDecodingCallStats* stats) const {
3678 audio_coding_->GetDecodingCallStatistics(stats);
3679}
3680
pwestin@webrtc.org1de01352013-04-11 20:23:35 +00003681bool Channel::GetDelayEstimate(int* jitter_buffer_delay_ms,
3682 int* playout_buffer_delay_ms) const {
3683 if (_average_jitter_buffer_delay_us == 0) {
niklase@google.com470e71d2011-07-07 08:21:25 +00003684 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId,_channelId),
pwestin@webrtc.org1de01352013-04-11 20:23:35 +00003685 "Channel::GetDelayEstimate() no valid estimate.");
3686 return false;
3687 }
3688 *jitter_buffer_delay_ms = (_average_jitter_buffer_delay_us + 500) / 1000 +
3689 _recPacketDelayMs;
3690 *playout_buffer_delay_ms = playout_delay_ms_;
3691 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId,_channelId),
3692 "Channel::GetDelayEstimate()");
3693 return true;
niklase@google.com470e71d2011-07-07 08:21:25 +00003694}
3695
turaj@webrtc.org6388c3e2013-02-12 21:42:18 +00003696int Channel::SetInitialPlayoutDelay(int delay_ms)
3697{
3698 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId,_channelId),
3699 "Channel::SetInitialPlayoutDelay()");
3700 if ((delay_ms < kVoiceEngineMinMinPlayoutDelayMs) ||
3701 (delay_ms > kVoiceEngineMaxMinPlayoutDelayMs))
3702 {
3703 _engineStatisticsPtr->SetLastError(
3704 VE_INVALID_ARGUMENT, kTraceError,
3705 "SetInitialPlayoutDelay() invalid min delay");
3706 return -1;
3707 }
andrew@webrtc.orgeb524d92013-09-23 23:02:24 +00003708 if (audio_coding_->SetInitialPlayoutDelay(delay_ms) != 0)
turaj@webrtc.org6388c3e2013-02-12 21:42:18 +00003709 {
3710 _engineStatisticsPtr->SetLastError(
3711 VE_AUDIO_CODING_MODULE_ERROR, kTraceError,
3712 "SetInitialPlayoutDelay() failed to set min playout delay");
3713 return -1;
3714 }
3715 return 0;
3716}
3717
3718
niklase@google.com470e71d2011-07-07 08:21:25 +00003719int
3720Channel::SetMinimumPlayoutDelay(int delayMs)
3721{
3722 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId,_channelId),
3723 "Channel::SetMinimumPlayoutDelay()");
3724 if ((delayMs < kVoiceEngineMinMinPlayoutDelayMs) ||
3725 (delayMs > kVoiceEngineMaxMinPlayoutDelayMs))
3726 {
3727 _engineStatisticsPtr->SetLastError(
3728 VE_INVALID_ARGUMENT, kTraceError,
3729 "SetMinimumPlayoutDelay() invalid min delay");
3730 return -1;
3731 }
andrew@webrtc.orgeb524d92013-09-23 23:02:24 +00003732 if (audio_coding_->SetMinimumPlayoutDelay(delayMs) != 0)
niklase@google.com470e71d2011-07-07 08:21:25 +00003733 {
3734 _engineStatisticsPtr->SetLastError(
3735 VE_AUDIO_CODING_MODULE_ERROR, kTraceError,
3736 "SetMinimumPlayoutDelay() failed to set min playout delay");
3737 return -1;
3738 }
3739 return 0;
3740}
3741
pwestin@webrtc.org1de01352013-04-11 20:23:35 +00003742void Channel::UpdatePlayoutTimestamp(bool rtcp) {
3743 uint32_t playout_timestamp = 0;
3744
andrew@webrtc.orgeb524d92013-09-23 23:02:24 +00003745 if (audio_coding_->PlayoutTimestamp(&playout_timestamp) == -1) {
turaj@webrtc.org1ebd2e92014-07-25 17:50:10 +00003746 // This can happen if this channel has not been received any RTP packet. In
3747 // this case, NetEq is not capable of computing playout timestamp.
pwestin@webrtc.org1de01352013-04-11 20:23:35 +00003748 return;
3749 }
3750
3751 uint16_t delay_ms = 0;
3752 if (_audioDeviceModulePtr->PlayoutDelay(&delay_ms) == -1) {
3753 WEBRTC_TRACE(kTraceWarning, kTraceVoice, VoEId(_instanceId,_channelId),
3754 "Channel::UpdatePlayoutTimestamp() failed to read playout"
3755 " delay from the ADM");
3756 _engineStatisticsPtr->SetLastError(
3757 VE_CANNOT_RETRIEVE_VALUE, kTraceError,
3758 "UpdatePlayoutTimestamp() failed to retrieve playout delay");
3759 return;
3760 }
3761
turaj@webrtc.org167b6df2013-12-13 21:05:07 +00003762 jitter_buffer_playout_timestamp_ = playout_timestamp;
3763
pwestin@webrtc.org1de01352013-04-11 20:23:35 +00003764 // Remove the playout delay.
wu@webrtc.org94454b72014-06-05 20:34:08 +00003765 playout_timestamp -= (delay_ms * (GetPlayoutFrequency() / 1000));
pwestin@webrtc.org1de01352013-04-11 20:23:35 +00003766
3767 WEBRTC_TRACE(kTraceStream, kTraceVoice, VoEId(_instanceId,_channelId),
3768 "Channel::UpdatePlayoutTimestamp() => playoutTimestamp = %lu",
3769 playout_timestamp);
3770
3771 if (rtcp) {
3772 playout_timestamp_rtcp_ = playout_timestamp;
3773 } else {
3774 playout_timestamp_rtp_ = playout_timestamp;
3775 }
3776 playout_delay_ms_ = delay_ms;
3777}
3778
3779int Channel::GetPlayoutTimestamp(unsigned int& timestamp) {
3780 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId,_channelId),
3781 "Channel::GetPlayoutTimestamp()");
3782 if (playout_timestamp_rtp_ == 0) {
3783 _engineStatisticsPtr->SetLastError(
3784 VE_CANNOT_RETRIEVE_VALUE, kTraceError,
3785 "GetPlayoutTimestamp() failed to retrieve timestamp");
3786 return -1;
3787 }
3788 timestamp = playout_timestamp_rtp_;
3789 WEBRTC_TRACE(kTraceStateInfo, kTraceVoice,
3790 VoEId(_instanceId,_channelId),
3791 "GetPlayoutTimestamp() => timestamp=%u", timestamp);
3792 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +00003793}
3794
pbos@webrtc.orgd16e8392014-12-19 13:49:55 +00003795int Channel::SetInitTimestamp(unsigned int timestamp) {
3796 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId, _channelId),
niklase@google.com470e71d2011-07-07 08:21:25 +00003797 "Channel::SetInitTimestamp()");
pbos@webrtc.orgd16e8392014-12-19 13:49:55 +00003798 if (channel_state_.Get().sending) {
3799 _engineStatisticsPtr->SetLastError(VE_SENDING, kTraceError,
3800 "SetInitTimestamp() already sending");
3801 return -1;
3802 }
3803 _rtpRtcpModule->SetStartTimestamp(timestamp);
3804 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +00003805}
3806
pbos@webrtc.orgd16e8392014-12-19 13:49:55 +00003807int Channel::SetInitSequenceNumber(short sequenceNumber) {
3808 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId, _channelId),
3809 "Channel::SetInitSequenceNumber()");
3810 if (channel_state_.Get().sending) {
3811 _engineStatisticsPtr->SetLastError(
3812 VE_SENDING, kTraceError, "SetInitSequenceNumber() already sending");
3813 return -1;
3814 }
3815 _rtpRtcpModule->SetSequenceNumber(sequenceNumber);
3816 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +00003817}
3818
3819int
wu@webrtc.org822fbd82013-08-15 23:38:54 +00003820Channel::GetRtpRtcp(RtpRtcp** rtpRtcpModule, RtpReceiver** rtp_receiver) const
niklase@google.com470e71d2011-07-07 08:21:25 +00003821{
3822 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId,_channelId),
3823 "Channel::GetRtpRtcp()");
wu@webrtc.org822fbd82013-08-15 23:38:54 +00003824 *rtpRtcpModule = _rtpRtcpModule.get();
3825 *rtp_receiver = rtp_receiver_.get();
niklase@google.com470e71d2011-07-07 08:21:25 +00003826 return 0;
3827}
3828
andrew@webrtc.orge59a0ac2012-05-08 17:12:40 +00003829// TODO(andrew): refactor Mix functions here and in transmit_mixer.cc to use
3830// a shared helper.
pbos@webrtc.org6141e132013-04-09 10:09:10 +00003831int32_t
pbos@webrtc.org92135212013-05-14 08:31:39 +00003832Channel::MixOrReplaceAudioWithFile(int mixingFrequency)
niklase@google.com470e71d2011-07-07 08:21:25 +00003833{
kwiberg@webrtc.org00b8f6b2015-02-26 14:34:55 +00003834 rtc::scoped_ptr<int16_t[]> fileBuffer(new int16_t[640]);
andrew@webrtc.orge59a0ac2012-05-08 17:12:40 +00003835 int fileSamples(0);
niklase@google.com470e71d2011-07-07 08:21:25 +00003836
3837 {
mflodman@webrtc.org9a065d12012-03-07 08:12:21 +00003838 CriticalSectionScoped cs(&_fileCritSect);
niklase@google.com470e71d2011-07-07 08:21:25 +00003839
3840 if (_inputFilePlayerPtr == NULL)
3841 {
3842 WEBRTC_TRACE(kTraceWarning, kTraceVoice,
3843 VoEId(_instanceId, _channelId),
3844 "Channel::MixOrReplaceAudioWithFile() fileplayer"
3845 " doesnt exist");
3846 return -1;
3847 }
3848
braveyao@webrtc.orgd7131432012-03-29 10:39:44 +00003849 if (_inputFilePlayerPtr->Get10msAudioFromFile(fileBuffer.get(),
niklase@google.com470e71d2011-07-07 08:21:25 +00003850 fileSamples,
3851 mixingFrequency) == -1)
3852 {
3853 WEBRTC_TRACE(kTraceWarning, kTraceVoice,
3854 VoEId(_instanceId, _channelId),
3855 "Channel::MixOrReplaceAudioWithFile() file mixing "
3856 "failed");
3857 return -1;
3858 }
3859 if (fileSamples == 0)
3860 {
3861 WEBRTC_TRACE(kTraceWarning, kTraceVoice,
3862 VoEId(_instanceId, _channelId),
3863 "Channel::MixOrReplaceAudioWithFile() file is ended");
3864 return 0;
3865 }
3866 }
3867
andrew@webrtc.org63a50982012-05-02 23:56:37 +00003868 assert(_audioFrame.samples_per_channel_ == fileSamples);
niklase@google.com470e71d2011-07-07 08:21:25 +00003869
3870 if (_mixFileWithMicrophone)
3871 {
braveyao@webrtc.orgd7131432012-03-29 10:39:44 +00003872 // Currently file stream is always mono.
3873 // TODO(xians): Change the code when FilePlayer supports real stereo.
andrew@webrtc.org40ee3d02014-04-03 21:56:01 +00003874 MixWithSat(_audioFrame.data_,
3875 _audioFrame.num_channels_,
3876 fileBuffer.get(),
3877 1,
3878 fileSamples);
niklase@google.com470e71d2011-07-07 08:21:25 +00003879 }
3880 else
3881 {
braveyao@webrtc.orgd7131432012-03-29 10:39:44 +00003882 // Replace ACM audio with file.
3883 // Currently file stream is always mono.
3884 // TODO(xians): Change the code when FilePlayer supports real stereo.
niklase@google.com470e71d2011-07-07 08:21:25 +00003885 _audioFrame.UpdateFrame(_channelId,
tommi@webrtc.orgeec6ecd2014-07-11 19:09:59 +00003886 0xFFFFFFFF,
braveyao@webrtc.orgd7131432012-03-29 10:39:44 +00003887 fileBuffer.get(),
andrew@webrtc.orge59a0ac2012-05-08 17:12:40 +00003888 fileSamples,
niklase@google.com470e71d2011-07-07 08:21:25 +00003889 mixingFrequency,
3890 AudioFrame::kNormalSpeech,
3891 AudioFrame::kVadUnknown,
3892 1);
3893
3894 }
3895 return 0;
3896}
3897
pbos@webrtc.org6141e132013-04-09 10:09:10 +00003898int32_t
niklase@google.com470e71d2011-07-07 08:21:25 +00003899Channel::MixAudioWithFile(AudioFrame& audioFrame,
pbos@webrtc.org92135212013-05-14 08:31:39 +00003900 int mixingFrequency)
niklase@google.com470e71d2011-07-07 08:21:25 +00003901{
minyue@webrtc.org2a8df7c2014-08-06 10:05:19 +00003902 assert(mixingFrequency <= 48000);
niklase@google.com470e71d2011-07-07 08:21:25 +00003903
kwiberg@webrtc.org00b8f6b2015-02-26 14:34:55 +00003904 rtc::scoped_ptr<int16_t[]> fileBuffer(new int16_t[960]);
andrew@webrtc.orge59a0ac2012-05-08 17:12:40 +00003905 int fileSamples(0);
niklase@google.com470e71d2011-07-07 08:21:25 +00003906
3907 {
mflodman@webrtc.org9a065d12012-03-07 08:12:21 +00003908 CriticalSectionScoped cs(&_fileCritSect);
niklase@google.com470e71d2011-07-07 08:21:25 +00003909
3910 if (_outputFilePlayerPtr == NULL)
3911 {
3912 WEBRTC_TRACE(kTraceWarning, kTraceVoice,
3913 VoEId(_instanceId, _channelId),
3914 "Channel::MixAudioWithFile() file mixing failed");
3915 return -1;
3916 }
3917
3918 // We should get the frequency we ask for.
braveyao@webrtc.orgd7131432012-03-29 10:39:44 +00003919 if (_outputFilePlayerPtr->Get10msAudioFromFile(fileBuffer.get(),
niklase@google.com470e71d2011-07-07 08:21:25 +00003920 fileSamples,
3921 mixingFrequency) == -1)
3922 {
3923 WEBRTC_TRACE(kTraceWarning, kTraceVoice,
3924 VoEId(_instanceId, _channelId),
3925 "Channel::MixAudioWithFile() file mixing failed");
3926 return -1;
3927 }
3928 }
3929
andrew@webrtc.org63a50982012-05-02 23:56:37 +00003930 if (audioFrame.samples_per_channel_ == fileSamples)
niklase@google.com470e71d2011-07-07 08:21:25 +00003931 {
braveyao@webrtc.orgd7131432012-03-29 10:39:44 +00003932 // Currently file stream is always mono.
3933 // TODO(xians): Change the code when FilePlayer supports real stereo.
andrew@webrtc.org40ee3d02014-04-03 21:56:01 +00003934 MixWithSat(audioFrame.data_,
3935 audioFrame.num_channels_,
3936 fileBuffer.get(),
3937 1,
3938 fileSamples);
niklase@google.com470e71d2011-07-07 08:21:25 +00003939 }
3940 else
3941 {
3942 WEBRTC_TRACE(kTraceWarning, kTraceVoice, VoEId(_instanceId,_channelId),
andrew@webrtc.org63a50982012-05-02 23:56:37 +00003943 "Channel::MixAudioWithFile() samples_per_channel_(%d) != "
niklase@google.com470e71d2011-07-07 08:21:25 +00003944 "fileSamples(%d)",
andrew@webrtc.org63a50982012-05-02 23:56:37 +00003945 audioFrame.samples_per_channel_, fileSamples);
niklase@google.com470e71d2011-07-07 08:21:25 +00003946 return -1;
3947 }
3948
3949 return 0;
3950}
3951
3952int
3953Channel::InsertInbandDtmfTone()
3954{
niklas.enbom@webrtc.orgaf26f642011-11-16 12:41:36 +00003955 // Check if we should start a new tone.
niklase@google.com470e71d2011-07-07 08:21:25 +00003956 if (_inbandDtmfQueue.PendingDtmf() &&
3957 !_inbandDtmfGenerator.IsAddingTone() &&
3958 _inbandDtmfGenerator.DelaySinceLastTone() >
3959 kMinTelephoneEventSeparationMs)
3960 {
pbos@webrtc.org6141e132013-04-09 10:09:10 +00003961 int8_t eventCode(0);
3962 uint16_t lengthMs(0);
3963 uint8_t attenuationDb(0);
niklase@google.com470e71d2011-07-07 08:21:25 +00003964
3965 eventCode = _inbandDtmfQueue.NextDtmf(&lengthMs, &attenuationDb);
3966 _inbandDtmfGenerator.AddTone(eventCode, lengthMs, attenuationDb);
3967 if (_playInbandDtmfEvent)
3968 {
3969 // Add tone to output mixer using a reduced length to minimize
3970 // risk of echo.
3971 _outputMixerPtr->PlayDtmfTone(eventCode, lengthMs - 80,
3972 attenuationDb);
3973 }
3974 }
3975
3976 if (_inbandDtmfGenerator.IsAddingTone())
3977 {
pbos@webrtc.org6141e132013-04-09 10:09:10 +00003978 uint16_t frequency(0);
niklase@google.com470e71d2011-07-07 08:21:25 +00003979 _inbandDtmfGenerator.GetSampleRate(frequency);
3980
andrew@webrtc.org63a50982012-05-02 23:56:37 +00003981 if (frequency != _audioFrame.sample_rate_hz_)
niklase@google.com470e71d2011-07-07 08:21:25 +00003982 {
3983 // Update sample rate of Dtmf tone since the mixing frequency
3984 // has changed.
3985 _inbandDtmfGenerator.SetSampleRate(
pbos@webrtc.org6141e132013-04-09 10:09:10 +00003986 (uint16_t) (_audioFrame.sample_rate_hz_));
niklase@google.com470e71d2011-07-07 08:21:25 +00003987 // Reset the tone to be added taking the new sample rate into
3988 // account.
3989 _inbandDtmfGenerator.ResetTone();
3990 }
andrew@webrtc.orgae1a58b2013-01-22 04:44:30 +00003991
pbos@webrtc.org6141e132013-04-09 10:09:10 +00003992 int16_t toneBuffer[320];
3993 uint16_t toneSamples(0);
niklase@google.com470e71d2011-07-07 08:21:25 +00003994 // Get 10ms tone segment and set time since last tone to zero
3995 if (_inbandDtmfGenerator.Get10msTone(toneBuffer, toneSamples) == -1)
3996 {
3997 WEBRTC_TRACE(kTraceWarning, kTraceVoice,
3998 VoEId(_instanceId, _channelId),
3999 "Channel::EncodeAndSend() inserting Dtmf failed");
4000 return -1;
4001 }
4002
niklas.enbom@webrtc.orgaf26f642011-11-16 12:41:36 +00004003 // Replace mixed audio with DTMF tone.
andrew@webrtc.orgae1a58b2013-01-22 04:44:30 +00004004 for (int sample = 0;
andrew@webrtc.org63a50982012-05-02 23:56:37 +00004005 sample < _audioFrame.samples_per_channel_;
niklas.enbom@webrtc.orgaf26f642011-11-16 12:41:36 +00004006 sample++)
4007 {
andrew@webrtc.orgae1a58b2013-01-22 04:44:30 +00004008 for (int channel = 0;
4009 channel < _audioFrame.num_channels_;
niklas.enbom@webrtc.orgaf26f642011-11-16 12:41:36 +00004010 channel++)
4011 {
andrew@webrtc.orgae1a58b2013-01-22 04:44:30 +00004012 const int index = sample * _audioFrame.num_channels_ + channel;
4013 _audioFrame.data_[index] = toneBuffer[sample];
niklas.enbom@webrtc.orgaf26f642011-11-16 12:41:36 +00004014 }
4015 }
andrew@webrtc.orgae1a58b2013-01-22 04:44:30 +00004016
andrew@webrtc.org63a50982012-05-02 23:56:37 +00004017 assert(_audioFrame.samples_per_channel_ == toneSamples);
niklase@google.com470e71d2011-07-07 08:21:25 +00004018 } else
4019 {
4020 // Add 10ms to "delay-since-last-tone" counter
4021 _inbandDtmfGenerator.UpdateDelaySinceLastTone();
4022 }
4023 return 0;
4024}
4025
pbos@webrtc.org6141e132013-04-09 10:09:10 +00004026int32_t
pkasting@chromium.org4591fbd2014-11-20 22:28:14 +00004027Channel::SendPacketRaw(const void *data, size_t len, bool RTCP)
niklase@google.com470e71d2011-07-07 08:21:25 +00004028{
wu@webrtc.orgfb648da2013-10-18 21:10:51 +00004029 CriticalSectionScoped cs(&_callbackCritSect);
niklase@google.com470e71d2011-07-07 08:21:25 +00004030 if (_transportPtr == NULL)
4031 {
4032 return -1;
4033 }
4034 if (!RTCP)
4035 {
4036 return _transportPtr->SendPacket(_channelId, data, len);
4037 }
4038 else
4039 {
4040 return _transportPtr->SendRTCPPacket(_channelId, data, len);
4041 }
4042}
4043
pwestin@webrtc.org1de01352013-04-11 20:23:35 +00004044// Called for incoming RTP packets after successful RTP header parsing.
4045void Channel::UpdatePacketDelay(uint32_t rtp_timestamp,
4046 uint16_t sequence_number) {
4047 WEBRTC_TRACE(kTraceStream, kTraceVoice, VoEId(_instanceId,_channelId),
4048 "Channel::UpdatePacketDelay(timestamp=%lu, sequenceNumber=%u)",
4049 rtp_timestamp, sequence_number);
niklase@google.com470e71d2011-07-07 08:21:25 +00004050
pwestin@webrtc.org1de01352013-04-11 20:23:35 +00004051 // Get frequency of last received payload
wu@webrtc.org94454b72014-06-05 20:34:08 +00004052 int rtp_receive_frequency = GetPlayoutFrequency();
niklase@google.com470e71d2011-07-07 08:21:25 +00004053
turaj@webrtc.orge46c8d32013-05-22 20:39:43 +00004054 // Update the least required delay.
andrew@webrtc.orgeb524d92013-09-23 23:02:24 +00004055 least_required_delay_ms_ = audio_coding_->LeastRequiredDelayMs();
turaj@webrtc.orge46c8d32013-05-22 20:39:43 +00004056
turaj@webrtc.org167b6df2013-12-13 21:05:07 +00004057 // |jitter_buffer_playout_timestamp_| updated in UpdatePlayoutTimestamp for
4058 // every incoming packet.
4059 uint32_t timestamp_diff_ms = (rtp_timestamp -
4060 jitter_buffer_playout_timestamp_) / (rtp_receive_frequency / 1000);
henrik.lundin@webrtc.orgd6692992014-03-20 12:04:09 +00004061 if (!IsNewerTimestamp(rtp_timestamp, jitter_buffer_playout_timestamp_) ||
4062 timestamp_diff_ms > (2 * kVoiceEngineMaxMinPlayoutDelayMs)) {
4063 // If |jitter_buffer_playout_timestamp_| is newer than the incoming RTP
4064 // timestamp, the resulting difference is negative, but is set to zero.
4065 // This can happen when a network glitch causes a packet to arrive late,
4066 // and during long comfort noise periods with clock drift.
4067 timestamp_diff_ms = 0;
4068 }
niklase@google.com470e71d2011-07-07 08:21:25 +00004069
pwestin@webrtc.org1de01352013-04-11 20:23:35 +00004070 uint16_t packet_delay_ms = (rtp_timestamp - _previousTimestamp) /
4071 (rtp_receive_frequency / 1000);
niklase@google.com470e71d2011-07-07 08:21:25 +00004072
pwestin@webrtc.org1de01352013-04-11 20:23:35 +00004073 _previousTimestamp = rtp_timestamp;
niklase@google.com470e71d2011-07-07 08:21:25 +00004074
pwestin@webrtc.org1de01352013-04-11 20:23:35 +00004075 if (timestamp_diff_ms == 0) return;
niklase@google.com470e71d2011-07-07 08:21:25 +00004076
pwestin@webrtc.org1de01352013-04-11 20:23:35 +00004077 if (packet_delay_ms >= 10 && packet_delay_ms <= 60) {
4078 _recPacketDelayMs = packet_delay_ms;
4079 }
niklase@google.com470e71d2011-07-07 08:21:25 +00004080
pwestin@webrtc.org1de01352013-04-11 20:23:35 +00004081 if (_average_jitter_buffer_delay_us == 0) {
4082 _average_jitter_buffer_delay_us = timestamp_diff_ms * 1000;
4083 return;
4084 }
4085
4086 // Filter average delay value using exponential filter (alpha is
4087 // 7/8). We derive 1000 *_average_jitter_buffer_delay_us here (reduces
4088 // risk of rounding error) and compensate for it in GetDelayEstimate()
4089 // later.
4090 _average_jitter_buffer_delay_us = (_average_jitter_buffer_delay_us * 7 +
4091 1000 * timestamp_diff_ms + 500) / 8;
niklase@google.com470e71d2011-07-07 08:21:25 +00004092}
4093
4094void
4095Channel::RegisterReceiveCodecsToRTPModule()
4096{
4097 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId,_channelId),
4098 "Channel::RegisterReceiveCodecsToRTPModule()");
4099
4100
4101 CodecInst codec;
pbos@webrtc.org6141e132013-04-09 10:09:10 +00004102 const uint8_t nSupportedCodecs = AudioCodingModule::NumberOfCodecs();
niklase@google.com470e71d2011-07-07 08:21:25 +00004103
4104 for (int idx = 0; idx < nSupportedCodecs; idx++)
4105 {
4106 // Open up the RTP/RTCP receiver for all supported codecs
andrew@webrtc.orgeb524d92013-09-23 23:02:24 +00004107 if ((audio_coding_->Codec(idx, &codec) == -1) ||
wu@webrtc.org822fbd82013-08-15 23:38:54 +00004108 (rtp_receiver_->RegisterReceivePayload(
4109 codec.plname,
4110 codec.pltype,
4111 codec.plfreq,
4112 codec.channels,
4113 (codec.rate < 0) ? 0 : codec.rate) == -1))
niklase@google.com470e71d2011-07-07 08:21:25 +00004114 {
4115 WEBRTC_TRACE(
4116 kTraceWarning,
4117 kTraceVoice,
4118 VoEId(_instanceId, _channelId),
4119 "Channel::RegisterReceiveCodecsToRTPModule() unable"
4120 " to register %s (%d/%d/%d/%d) to RTP/RTCP receiver",
4121 codec.plname, codec.pltype, codec.plfreq,
4122 codec.channels, codec.rate);
4123 }
4124 else
4125 {
4126 WEBRTC_TRACE(
4127 kTraceInfo,
4128 kTraceVoice,
4129 VoEId(_instanceId, _channelId),
4130 "Channel::RegisterReceiveCodecsToRTPModule() %s "
wu@webrtc.orgfcd12b32011-09-15 20:49:50 +00004131 "(%d/%d/%d/%d) has been added to the RTP/RTCP "
niklase@google.com470e71d2011-07-07 08:21:25 +00004132 "receiver",
4133 codec.plname, codec.pltype, codec.plfreq,
4134 codec.channels, codec.rate);
4135 }
4136 }
4137}
4138
turaj@webrtc.org8c8ad852013-01-31 18:20:17 +00004139// Assuming this method is called with valid payload type.
turaj@webrtc.org42259e72012-12-11 02:15:12 +00004140int Channel::SetRedPayloadType(int red_payload_type) {
turaj@webrtc.org42259e72012-12-11 02:15:12 +00004141 CodecInst codec;
4142 bool found_red = false;
4143
4144 // Get default RED settings from the ACM database
4145 const int num_codecs = AudioCodingModule::NumberOfCodecs();
4146 for (int idx = 0; idx < num_codecs; idx++) {
andrew@webrtc.orgeb524d92013-09-23 23:02:24 +00004147 audio_coding_->Codec(idx, &codec);
turaj@webrtc.org42259e72012-12-11 02:15:12 +00004148 if (!STR_CASE_CMP(codec.plname, "RED")) {
4149 found_red = true;
4150 break;
4151 }
4152 }
4153
4154 if (!found_red) {
4155 _engineStatisticsPtr->SetLastError(
4156 VE_CODEC_ERROR, kTraceError,
4157 "SetRedPayloadType() RED is not supported");
4158 return -1;
4159 }
4160
turaj@webrtc.org9d532fd2013-01-31 18:34:19 +00004161 codec.pltype = red_payload_type;
andrew@webrtc.orgeb524d92013-09-23 23:02:24 +00004162 if (audio_coding_->RegisterSendCodec(codec) < 0) {
turaj@webrtc.org42259e72012-12-11 02:15:12 +00004163 _engineStatisticsPtr->SetLastError(
4164 VE_AUDIO_CODING_MODULE_ERROR, kTraceError,
4165 "SetRedPayloadType() RED registration in ACM module failed");
4166 return -1;
4167 }
4168
4169 if (_rtpRtcpModule->SetSendREDPayloadType(red_payload_type) != 0) {
4170 _engineStatisticsPtr->SetLastError(
4171 VE_RTP_RTCP_MODULE_ERROR, kTraceError,
4172 "SetRedPayloadType() RED registration in RTP/RTCP module failed");
4173 return -1;
4174 }
4175 return 0;
4176}
4177
wu@webrtc.orgebdb0e32014-03-06 23:49:08 +00004178int Channel::SetSendRtpHeaderExtension(bool enable, RTPExtensionType type,
4179 unsigned char id) {
4180 int error = 0;
4181 _rtpRtcpModule->DeregisterSendRtpHeaderExtension(type);
4182 if (enable) {
4183 error = _rtpRtcpModule->RegisterSendRtpHeaderExtension(type, id);
4184 }
4185 return error;
4186}
minyue@webrtc.orgc1a40a72014-05-28 09:52:06 +00004187
wu@webrtc.org94454b72014-06-05 20:34:08 +00004188int32_t Channel::GetPlayoutFrequency() {
4189 int32_t playout_frequency = audio_coding_->PlayoutFrequency();
4190 CodecInst current_recive_codec;
4191 if (audio_coding_->ReceiveCodec(&current_recive_codec) == 0) {
4192 if (STR_CASE_CMP("G722", current_recive_codec.plname) == 0) {
4193 // Even though the actual sampling rate for G.722 audio is
4194 // 16,000 Hz, the RTP clock rate for the G722 payload format is
4195 // 8,000 Hz because that value was erroneously assigned in
4196 // RFC 1890 and must remain unchanged for backward compatibility.
4197 playout_frequency = 8000;
4198 } else if (STR_CASE_CMP("opus", current_recive_codec.plname) == 0) {
4199 // We are resampling Opus internally to 32,000 Hz until all our
4200 // DSP routines can operate at 48,000 Hz, but the RTP clock
4201 // rate for the Opus payload format is standardized to 48,000 Hz,
4202 // because that is the maximum supported decoding sampling rate.
4203 playout_frequency = 48000;
4204 }
4205 }
4206 return playout_frequency;
4207}
4208
pkasting@chromium.org16825b12015-01-12 21:51:21 +00004209int64_t Channel::GetRTT() const {
minyue@webrtc.org2b58a442014-09-11 07:51:53 +00004210 RTCPMethod method = _rtpRtcpModule->RTCP();
4211 if (method == kRtcpOff) {
minyue@webrtc.org2b58a442014-09-11 07:51:53 +00004212 return 0;
4213 }
4214 std::vector<RTCPReportBlock> report_blocks;
4215 _rtpRtcpModule->RemoteRTCPStat(&report_blocks);
4216 if (report_blocks.empty()) {
minyue@webrtc.org2b58a442014-09-11 07:51:53 +00004217 return 0;
4218 }
4219
4220 uint32_t remoteSSRC = rtp_receiver_->SSRC();
4221 std::vector<RTCPReportBlock>::const_iterator it = report_blocks.begin();
4222 for (; it != report_blocks.end(); ++it) {
4223 if (it->remoteSSRC == remoteSSRC)
4224 break;
4225 }
4226 if (it == report_blocks.end()) {
4227 // We have not received packets with SSRC matching the report blocks.
4228 // To calculate RTT we try with the SSRC of the first report block.
4229 // This is very important for send-only channels where we don't know
4230 // the SSRC of the other end.
4231 remoteSSRC = report_blocks[0].remoteSSRC;
4232 }
pkasting@chromium.org16825b12015-01-12 21:51:21 +00004233 int64_t rtt = 0;
4234 int64_t avg_rtt = 0;
4235 int64_t max_rtt= 0;
4236 int64_t min_rtt = 0;
minyue@webrtc.org2b58a442014-09-11 07:51:53 +00004237 if (_rtpRtcpModule->RTT(remoteSSRC, &rtt, &avg_rtt, &min_rtt, &max_rtt)
4238 != 0) {
minyue@webrtc.org2b58a442014-09-11 07:51:53 +00004239 return 0;
4240 }
pkasting@chromium.org16825b12015-01-12 21:51:21 +00004241 return rtt;
minyue@webrtc.org2b58a442014-09-11 07:51:53 +00004242}
4243
pbos@webrtc.orgd900e8b2013-07-03 15:12:26 +00004244} // namespace voe
4245} // namespace webrtc