blob: 40a974bd3790eb1b0c2bf3264b287860e8e59f21 [file] [log] [blame]
niklase@google.com470e71d2011-07-07 08:21:25 +00001/*
henrika@webrtc.org2919e952012-01-31 08:45:03 +00002 * Copyright (c) 2012 The WebRTC project authors. All Rights Reserved.
niklase@google.com470e71d2011-07-07 08:21:25 +00003 *
4 * Use of this source code is governed by a BSD-style license
5 * that can be found in the LICENSE file in the root of the source
6 * tree. An additional intellectual property rights grant can be found
7 * in the file PATENTS. All contributing project authors may
8 * be found in the AUTHORS file in the root of the source tree.
9 */
10
turaj@webrtc.org6388c3e2013-02-12 21:42:18 +000011#include "webrtc/voice_engine/channel.h"
niklase@google.com470e71d2011-07-07 08:21:25 +000012
pkasting@chromium.org4591fbd2014-11-20 22:28:14 +000013#include "webrtc/base/format_macros.h"
wu@webrtc.org94454b72014-06-05 20:34:08 +000014#include "webrtc/base/timeutils.h"
minyue@webrtc.orge509f942013-09-12 17:03:00 +000015#include "webrtc/common.h"
turaj@webrtc.org6388c3e2013-02-12 21:42:18 +000016#include "webrtc/modules/audio_device/include/audio_device.h"
17#include "webrtc/modules/audio_processing/include/audio_processing.h"
henrik.lundin@webrtc.orgd6692992014-03-20 12:04:09 +000018#include "webrtc/modules/interface/module_common_types.h"
wu@webrtc.org822fbd82013-08-15 23:38:54 +000019#include "webrtc/modules/rtp_rtcp/interface/receive_statistics.h"
20#include "webrtc/modules/rtp_rtcp/interface/rtp_payload_registry.h"
21#include "webrtc/modules/rtp_rtcp/interface/rtp_receiver.h"
22#include "webrtc/modules/rtp_rtcp/source/rtp_receiver_strategy.h"
turaj@webrtc.org6388c3e2013-02-12 21:42:18 +000023#include "webrtc/modules/utility/interface/audio_frame_operations.h"
24#include "webrtc/modules/utility/interface/process_thread.h"
25#include "webrtc/modules/utility/interface/rtp_dump.h"
26#include "webrtc/system_wrappers/interface/critical_section_wrapper.h"
27#include "webrtc/system_wrappers/interface/logging.h"
28#include "webrtc/system_wrappers/interface/trace.h"
solenberg@webrtc.orgb1f50102014-03-24 10:38:25 +000029#include "webrtc/video_engine/include/vie_network.h"
turaj@webrtc.org6388c3e2013-02-12 21:42:18 +000030#include "webrtc/voice_engine/include/voe_base.h"
31#include "webrtc/voice_engine/include/voe_external_media.h"
32#include "webrtc/voice_engine/include/voe_rtp_rtcp.h"
33#include "webrtc/voice_engine/output_mixer.h"
34#include "webrtc/voice_engine/statistics.h"
35#include "webrtc/voice_engine/transmit_mixer.h"
36#include "webrtc/voice_engine/utility.h"
niklase@google.com470e71d2011-07-07 08:21:25 +000037
38#if defined(_WIN32)
39#include <Qos.h>
40#endif
41
andrew@webrtc.org50419b02012-11-14 19:07:54 +000042namespace webrtc {
43namespace voe {
niklase@google.com470e71d2011-07-07 08:21:25 +000044
sprang@webrtc.org54ae4ff2013-12-19 13:26:02 +000045// Extend the default RTCP statistics struct with max_jitter, defined as the
46// maximum jitter value seen in an RTCP report block.
47struct ChannelStatistics : public RtcpStatistics {
48 ChannelStatistics() : rtcp(), max_jitter(0) {}
49
50 RtcpStatistics rtcp;
51 uint32_t max_jitter;
52};
53
54// Statistics callback, called at each generation of a new RTCP report block.
55class StatisticsProxy : public RtcpStatisticsCallback {
56 public:
57 StatisticsProxy(uint32_t ssrc)
58 : stats_lock_(CriticalSectionWrapper::CreateCriticalSection()),
59 ssrc_(ssrc) {}
60 virtual ~StatisticsProxy() {}
61
kjellander@webrtc.org14665ff2015-03-04 12:58:35 +000062 void StatisticsUpdated(const RtcpStatistics& statistics,
63 uint32_t ssrc) override {
sprang@webrtc.org54ae4ff2013-12-19 13:26:02 +000064 if (ssrc != ssrc_)
65 return;
66
67 CriticalSectionScoped cs(stats_lock_.get());
68 stats_.rtcp = statistics;
69 if (statistics.jitter > stats_.max_jitter) {
70 stats_.max_jitter = statistics.jitter;
71 }
72 }
73
kjellander@webrtc.org14665ff2015-03-04 12:58:35 +000074 void CNameChanged(const char* cname, uint32_t ssrc) override {}
pbos@webrtc.orgce4e9a32014-12-18 13:50:16 +000075
sprang@webrtc.org54ae4ff2013-12-19 13:26:02 +000076 void ResetStatistics() {
77 CriticalSectionScoped cs(stats_lock_.get());
78 stats_ = ChannelStatistics();
79 }
80
81 ChannelStatistics GetStats() {
82 CriticalSectionScoped cs(stats_lock_.get());
83 return stats_;
84 }
85
86 private:
87 // StatisticsUpdated calls are triggered from threads in the RTP module,
88 // while GetStats calls can be triggered from the public voice engine API,
89 // hence synchronization is needed.
kwiberg@webrtc.org00b8f6b2015-02-26 14:34:55 +000090 rtc::scoped_ptr<CriticalSectionWrapper> stats_lock_;
sprang@webrtc.org54ae4ff2013-12-19 13:26:02 +000091 const uint32_t ssrc_;
92 ChannelStatistics stats_;
93};
94
mflodman@webrtc.org0a7d4ee2015-02-17 12:57:14 +000095class VoERtcpObserver : public RtcpBandwidthObserver {
minyue@webrtc.orgc1a40a72014-05-28 09:52:06 +000096 public:
mflodman@webrtc.org0a7d4ee2015-02-17 12:57:14 +000097 explicit VoERtcpObserver(Channel* owner) : owner_(owner) {}
98 virtual ~VoERtcpObserver() {}
minyue@webrtc.orgc1a40a72014-05-28 09:52:06 +000099
mflodman@webrtc.org0a7d4ee2015-02-17 12:57:14 +0000100 void OnReceivedEstimatedBitrate(uint32_t bitrate) override {
101 // Not used for Voice Engine.
102 }
103
kjellander@webrtc.org14665ff2015-03-04 12:58:35 +0000104 void OnReceivedRtcpReceiverReport(const ReportBlockList& report_blocks,
105 int64_t rtt,
106 int64_t now_ms) override {
mflodman@webrtc.org0a7d4ee2015-02-17 12:57:14 +0000107 // TODO(mflodman): Do we need to aggregate reports here or can we jut send
108 // what we get? I.e. do we ever get multiple reports bundled into one RTCP
109 // report for VoiceEngine?
110 if (report_blocks.empty())
111 return;
112
113 int fraction_lost_aggregate = 0;
114 int total_number_of_packets = 0;
115
116 // If receiving multiple report blocks, calculate the weighted average based
117 // on the number of packets a report refers to.
118 for (ReportBlockList::const_iterator block_it = report_blocks.begin();
119 block_it != report_blocks.end(); ++block_it) {
120 // Find the previous extended high sequence number for this remote SSRC,
121 // to calculate the number of RTP packets this report refers to. Ignore if
122 // we haven't seen this SSRC before.
123 std::map<uint32_t, uint32_t>::iterator seq_num_it =
124 extended_max_sequence_number_.find(block_it->sourceSSRC);
125 int number_of_packets = 0;
126 if (seq_num_it != extended_max_sequence_number_.end()) {
127 number_of_packets = block_it->extendedHighSeqNum - seq_num_it->second;
128 }
129 fraction_lost_aggregate += number_of_packets * block_it->fractionLost;
130 total_number_of_packets += number_of_packets;
131
132 extended_max_sequence_number_[block_it->sourceSSRC] =
133 block_it->extendedHighSeqNum;
134 }
135 int weighted_fraction_lost = 0;
136 if (total_number_of_packets > 0) {
137 weighted_fraction_lost = (fraction_lost_aggregate +
138 total_number_of_packets / 2) / total_number_of_packets;
139 }
140 owner_->OnIncomingFractionLoss(weighted_fraction_lost);
minyue@webrtc.orgc1a40a72014-05-28 09:52:06 +0000141 }
142
143 private:
144 Channel* owner_;
mflodman@webrtc.org0a7d4ee2015-02-17 12:57:14 +0000145 // Maps remote side ssrc to extended highest sequence number received.
146 std::map<uint32_t, uint32_t> extended_max_sequence_number_;
minyue@webrtc.orgc1a40a72014-05-28 09:52:06 +0000147};
148
pbos@webrtc.org6141e132013-04-09 10:09:10 +0000149int32_t
niklase@google.com470e71d2011-07-07 08:21:25 +0000150Channel::SendData(FrameType frameType,
pbos@webrtc.org6141e132013-04-09 10:09:10 +0000151 uint8_t payloadType,
152 uint32_t timeStamp,
153 const uint8_t* payloadData,
pkasting@chromium.org4591fbd2014-11-20 22:28:14 +0000154 size_t payloadSize,
niklase@google.com470e71d2011-07-07 08:21:25 +0000155 const RTPFragmentationHeader* fragmentation)
156{
157 WEBRTC_TRACE(kTraceStream, kTraceVoice, VoEId(_instanceId,_channelId),
158 "Channel::SendData(frameType=%u, payloadType=%u, timeStamp=%u,"
pkasting@chromium.org4591fbd2014-11-20 22:28:14 +0000159 " payloadSize=%" PRIuS ", fragmentation=0x%x)",
160 frameType, payloadType, timeStamp,
161 payloadSize, fragmentation);
niklase@google.com470e71d2011-07-07 08:21:25 +0000162
163 if (_includeAudioLevelIndication)
164 {
165 // Store current audio level in the RTP/RTCP module.
166 // The level will be used in combination with voice-activity state
167 // (frameType) to add an RTP header extension
andrew@webrtc.org382c0c22014-05-05 18:22:21 +0000168 _rtpRtcpModule->SetAudioLevel(rms_level_.RMS());
niklase@google.com470e71d2011-07-07 08:21:25 +0000169 }
170
171 // Push data from ACM to RTP/RTCP-module to deliver audio frame for
172 // packetization.
173 // This call will trigger Transport::SendPacket() from the RTP/RTCP module.
pwestin@webrtc.org2853dde2012-05-11 11:08:54 +0000174 if (_rtpRtcpModule->SendOutgoingData((FrameType&)frameType,
niklase@google.com470e71d2011-07-07 08:21:25 +0000175 payloadType,
176 timeStamp,
stefan@webrtc.orgddfdfed2012-07-03 13:21:22 +0000177 // Leaving the time when this frame was
178 // received from the capture device as
179 // undefined for voice for now.
180 -1,
niklase@google.com470e71d2011-07-07 08:21:25 +0000181 payloadData,
182 payloadSize,
183 fragmentation) == -1)
184 {
185 _engineStatisticsPtr->SetLastError(
186 VE_RTP_RTCP_MODULE_ERROR, kTraceWarning,
187 "Channel::SendData() failed to send data to RTP/RTCP module");
188 return -1;
189 }
190
191 _lastLocalTimeStamp = timeStamp;
192 _lastPayloadType = payloadType;
193
194 return 0;
195}
196
pbos@webrtc.org6141e132013-04-09 10:09:10 +0000197int32_t
henrik.lundin@webrtc.orge9217b42015-03-06 07:50:34 +0000198Channel::InFrameType(FrameType frame_type)
niklase@google.com470e71d2011-07-07 08:21:25 +0000199{
200 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId,_channelId),
henrik.lundin@webrtc.orge9217b42015-03-06 07:50:34 +0000201 "Channel::InFrameType(frame_type=%d)", frame_type);
niklase@google.com470e71d2011-07-07 08:21:25 +0000202
mflodman@webrtc.org9a065d12012-03-07 08:12:21 +0000203 CriticalSectionScoped cs(&_callbackCritSect);
henrik.lundin@webrtc.orge9217b42015-03-06 07:50:34 +0000204 _sendFrameType = (frame_type == kAudioFrameSpeech);
niklase@google.com470e71d2011-07-07 08:21:25 +0000205 return 0;
206}
207
pbos@webrtc.org6141e132013-04-09 10:09:10 +0000208int32_t
pbos@webrtc.org92135212013-05-14 08:31:39 +0000209Channel::OnRxVadDetected(int vadDecision)
niklase@google.com470e71d2011-07-07 08:21:25 +0000210{
211 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId, _channelId),
212 "Channel::OnRxVadDetected(vadDecision=%d)", vadDecision);
213
mflodman@webrtc.org9a065d12012-03-07 08:12:21 +0000214 CriticalSectionScoped cs(&_callbackCritSect);
niklase@google.com470e71d2011-07-07 08:21:25 +0000215 if (_rxVadObserverPtr)
216 {
217 _rxVadObserverPtr->OnRxVad(_channelId, vadDecision);
218 }
219
220 return 0;
221}
222
223int
pkasting@chromium.org4591fbd2014-11-20 22:28:14 +0000224Channel::SendPacket(int channel, const void *data, size_t len)
niklase@google.com470e71d2011-07-07 08:21:25 +0000225{
226 channel = VoEChannelId(channel);
227 assert(channel == _channelId);
228
229 WEBRTC_TRACE(kTraceStream, kTraceVoice, VoEId(_instanceId,_channelId),
pkasting@chromium.org4591fbd2014-11-20 22:28:14 +0000230 "Channel::SendPacket(channel=%d, len=%" PRIuS ")", channel,
231 len);
niklase@google.com470e71d2011-07-07 08:21:25 +0000232
wu@webrtc.orgfb648da2013-10-18 21:10:51 +0000233 CriticalSectionScoped cs(&_callbackCritSect);
234
niklase@google.com470e71d2011-07-07 08:21:25 +0000235 if (_transportPtr == NULL)
236 {
237 WEBRTC_TRACE(kTraceError, kTraceVoice, VoEId(_instanceId,_channelId),
238 "Channel::SendPacket() failed to send RTP packet due to"
239 " invalid transport object");
240 return -1;
241 }
242
pbos@webrtc.org6141e132013-04-09 10:09:10 +0000243 uint8_t* bufferToSendPtr = (uint8_t*)data;
pkasting@chromium.org4591fbd2014-11-20 22:28:14 +0000244 size_t bufferLength = len;
niklase@google.com470e71d2011-07-07 08:21:25 +0000245
246 // Dump the RTP packet to a file (if RTP dump is enabled).
pbos@webrtc.org6141e132013-04-09 10:09:10 +0000247 if (_rtpDumpOut.DumpPacket((const uint8_t*)data, len) == -1)
niklase@google.com470e71d2011-07-07 08:21:25 +0000248 {
249 WEBRTC_TRACE(kTraceWarning, kTraceVoice,
250 VoEId(_instanceId,_channelId),
251 "Channel::SendPacket() RTP dump to output file failed");
252 }
253
wu@webrtc.orgfb648da2013-10-18 21:10:51 +0000254 int n = _transportPtr->SendPacket(channel, bufferToSendPtr,
255 bufferLength);
256 if (n < 0) {
257 std::string transport_name =
258 _externalTransport ? "external transport" : "WebRtc sockets";
259 WEBRTC_TRACE(kTraceError, kTraceVoice,
260 VoEId(_instanceId,_channelId),
261 "Channel::SendPacket() RTP transmission using %s failed",
262 transport_name.c_str());
263 return -1;
niklase@google.com470e71d2011-07-07 08:21:25 +0000264 }
wu@webrtc.orgfb648da2013-10-18 21:10:51 +0000265 return n;
niklase@google.com470e71d2011-07-07 08:21:25 +0000266}
267
268int
pkasting@chromium.org4591fbd2014-11-20 22:28:14 +0000269Channel::SendRTCPPacket(int channel, const void *data, size_t len)
niklase@google.com470e71d2011-07-07 08:21:25 +0000270{
271 channel = VoEChannelId(channel);
272 assert(channel == _channelId);
273
274 WEBRTC_TRACE(kTraceStream, kTraceVoice, VoEId(_instanceId,_channelId),
pkasting@chromium.org4591fbd2014-11-20 22:28:14 +0000275 "Channel::SendRTCPPacket(channel=%d, len=%" PRIuS ")", channel,
276 len);
niklase@google.com470e71d2011-07-07 08:21:25 +0000277
wu@webrtc.orgfb648da2013-10-18 21:10:51 +0000278 CriticalSectionScoped cs(&_callbackCritSect);
279 if (_transportPtr == NULL)
niklase@google.com470e71d2011-07-07 08:21:25 +0000280 {
wu@webrtc.orgfb648da2013-10-18 21:10:51 +0000281 WEBRTC_TRACE(kTraceError, kTraceVoice,
282 VoEId(_instanceId,_channelId),
283 "Channel::SendRTCPPacket() failed to send RTCP packet"
284 " due to invalid transport object");
285 return -1;
niklase@google.com470e71d2011-07-07 08:21:25 +0000286 }
287
pbos@webrtc.org6141e132013-04-09 10:09:10 +0000288 uint8_t* bufferToSendPtr = (uint8_t*)data;
pkasting@chromium.org4591fbd2014-11-20 22:28:14 +0000289 size_t bufferLength = len;
niklase@google.com470e71d2011-07-07 08:21:25 +0000290
291 // Dump the RTCP packet to a file (if RTP dump is enabled).
pbos@webrtc.org6141e132013-04-09 10:09:10 +0000292 if (_rtpDumpOut.DumpPacket((const uint8_t*)data, len) == -1)
niklase@google.com470e71d2011-07-07 08:21:25 +0000293 {
294 WEBRTC_TRACE(kTraceWarning, kTraceVoice,
295 VoEId(_instanceId,_channelId),
296 "Channel::SendPacket() RTCP dump to output file failed");
297 }
298
wu@webrtc.orgfb648da2013-10-18 21:10:51 +0000299 int n = _transportPtr->SendRTCPPacket(channel,
300 bufferToSendPtr,
301 bufferLength);
302 if (n < 0) {
303 std::string transport_name =
304 _externalTransport ? "external transport" : "WebRtc sockets";
305 WEBRTC_TRACE(kTraceInfo, kTraceVoice,
306 VoEId(_instanceId,_channelId),
307 "Channel::SendRTCPPacket() transmission using %s failed",
308 transport_name.c_str());
309 return -1;
niklase@google.com470e71d2011-07-07 08:21:25 +0000310 }
wu@webrtc.orgfb648da2013-10-18 21:10:51 +0000311 return n;
niklase@google.com470e71d2011-07-07 08:21:25 +0000312}
313
314void
pbos@webrtc.org92135212013-05-14 08:31:39 +0000315Channel::OnPlayTelephoneEvent(int32_t id,
316 uint8_t event,
317 uint16_t lengthMs,
318 uint8_t volume)
niklase@google.com470e71d2011-07-07 08:21:25 +0000319{
320 WEBRTC_TRACE(kTraceStream, kTraceVoice, VoEId(_instanceId,_channelId),
321 "Channel::OnPlayTelephoneEvent(id=%d, event=%u, lengthMs=%u,"
wu@webrtc.orgfcd12b32011-09-15 20:49:50 +0000322 " volume=%u)", id, event, lengthMs, volume);
niklase@google.com470e71d2011-07-07 08:21:25 +0000323
324 if (!_playOutbandDtmfEvent || (event > 15))
325 {
326 // Ignore callback since feedback is disabled or event is not a
327 // Dtmf tone event.
328 return;
329 }
330
331 assert(_outputMixerPtr != NULL);
332
333 // Start playing out the Dtmf tone (if playout is enabled).
334 // Reduce length of tone with 80ms to the reduce risk of echo.
335 _outputMixerPtr->PlayDtmfTone(event, lengthMs - 80, volume);
336}
337
338void
stefan@webrtc.org286fe0b2013-08-21 20:58:21 +0000339Channel::OnIncomingSSRCChanged(int32_t id, uint32_t ssrc)
niklase@google.com470e71d2011-07-07 08:21:25 +0000340{
341 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId,_channelId),
342 "Channel::OnIncomingSSRCChanged(id=%d, SSRC=%d)",
stefan@webrtc.org286fe0b2013-08-21 20:58:21 +0000343 id, ssrc);
niklase@google.com470e71d2011-07-07 08:21:25 +0000344
dwkang@webrtc.orgb295a3f2013-08-29 07:34:12 +0000345 // Update ssrc so that NTP for AV sync can be updated.
346 _rtpRtcpModule->SetRemoteSSRC(ssrc);
niklase@google.com470e71d2011-07-07 08:21:25 +0000347}
348
pbos@webrtc.org92135212013-05-14 08:31:39 +0000349void Channel::OnIncomingCSRCChanged(int32_t id,
350 uint32_t CSRC,
351 bool added)
niklase@google.com470e71d2011-07-07 08:21:25 +0000352{
353 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId,_channelId),
354 "Channel::OnIncomingCSRCChanged(id=%d, CSRC=%d, added=%d)",
355 id, CSRC, added);
niklase@google.com470e71d2011-07-07 08:21:25 +0000356}
357
stefan@webrtc.org286fe0b2013-08-21 20:58:21 +0000358void Channel::ResetStatistics(uint32_t ssrc) {
359 StreamStatistician* statistician =
360 rtp_receive_statistics_->GetStatistician(ssrc);
361 if (statistician) {
362 statistician->ResetStatistics();
363 }
sprang@webrtc.org54ae4ff2013-12-19 13:26:02 +0000364 statistics_proxy_->ResetStatistics();
wu@webrtc.org822fbd82013-08-15 23:38:54 +0000365}
366
pbos@webrtc.org6141e132013-04-09 10:09:10 +0000367int32_t
niklase@google.com470e71d2011-07-07 08:21:25 +0000368Channel::OnInitializeDecoder(
pbos@webrtc.org92135212013-05-14 08:31:39 +0000369 int32_t id,
370 int8_t payloadType,
leozwang@webrtc.org813e4b02012-03-01 18:34:25 +0000371 const char payloadName[RTP_PAYLOAD_NAME_SIZE],
pbos@webrtc.org92135212013-05-14 08:31:39 +0000372 int frequency,
373 uint8_t channels,
374 uint32_t rate)
niklase@google.com470e71d2011-07-07 08:21:25 +0000375{
376 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId,_channelId),
377 "Channel::OnInitializeDecoder(id=%d, payloadType=%d, "
378 "payloadName=%s, frequency=%u, channels=%u, rate=%u)",
379 id, payloadType, payloadName, frequency, channels, rate);
380
andrew@webrtc.orgceb148c2011-08-23 17:53:54 +0000381 assert(VoEChannelId(id) == _channelId);
niklase@google.com470e71d2011-07-07 08:21:25 +0000382
henrika@webrtc.orgf75901f2012-01-16 08:45:42 +0000383 CodecInst receiveCodec = {0};
384 CodecInst dummyCodec = {0};
niklase@google.com470e71d2011-07-07 08:21:25 +0000385
386 receiveCodec.pltype = payloadType;
niklase@google.com470e71d2011-07-07 08:21:25 +0000387 receiveCodec.plfreq = frequency;
388 receiveCodec.channels = channels;
389 receiveCodec.rate = rate;
henrika@webrtc.orgf75901f2012-01-16 08:45:42 +0000390 strncpy(receiveCodec.plname, payloadName, RTP_PAYLOAD_NAME_SIZE - 1);
andrew@webrtc.orgae1a58b2013-01-22 04:44:30 +0000391
andrew@webrtc.orgeb524d92013-09-23 23:02:24 +0000392 audio_coding_->Codec(payloadName, &dummyCodec, frequency, channels);
niklase@google.com470e71d2011-07-07 08:21:25 +0000393 receiveCodec.pacsize = dummyCodec.pacsize;
394
395 // Register the new codec to the ACM
andrew@webrtc.orgeb524d92013-09-23 23:02:24 +0000396 if (audio_coding_->RegisterReceiveCodec(receiveCodec) == -1)
niklase@google.com470e71d2011-07-07 08:21:25 +0000397 {
398 WEBRTC_TRACE(kTraceWarning, kTraceVoice,
andrew@webrtc.orgceb148c2011-08-23 17:53:54 +0000399 VoEId(_instanceId, _channelId),
niklase@google.com470e71d2011-07-07 08:21:25 +0000400 "Channel::OnInitializeDecoder() invalid codec ("
401 "pt=%d, name=%s) received - 1", payloadType, payloadName);
402 _engineStatisticsPtr->SetLastError(VE_AUDIO_CODING_MODULE_ERROR);
403 return -1;
404 }
405
406 return 0;
407}
408
pbos@webrtc.org6141e132013-04-09 10:09:10 +0000409int32_t
410Channel::OnReceivedPayloadData(const uint8_t* payloadData,
pkasting@chromium.org4591fbd2014-11-20 22:28:14 +0000411 size_t payloadSize,
niklase@google.com470e71d2011-07-07 08:21:25 +0000412 const WebRtcRTPHeader* rtpHeader)
413{
414 WEBRTC_TRACE(kTraceStream, kTraceVoice, VoEId(_instanceId,_channelId),
pkasting@chromium.org4591fbd2014-11-20 22:28:14 +0000415 "Channel::OnReceivedPayloadData(payloadSize=%" PRIuS ","
niklase@google.com470e71d2011-07-07 08:21:25 +0000416 " payloadType=%u, audioChannel=%u)",
417 payloadSize,
418 rtpHeader->header.payloadType,
419 rtpHeader->type.Audio.channel);
420
henrika@webrtc.org944cbeb2014-03-18 10:32:33 +0000421 if (!channel_state_.Get().playing)
niklase@google.com470e71d2011-07-07 08:21:25 +0000422 {
423 // Avoid inserting into NetEQ when we are not playing. Count the
424 // packet as discarded.
425 WEBRTC_TRACE(kTraceStream, kTraceVoice,
426 VoEId(_instanceId, _channelId),
427 "received packet is discarded since playing is not"
428 " activated");
429 _numberOfDiscardedPackets++;
430 return 0;
431 }
432
433 // Push the incoming payload (parsed and ready for decoding) into the ACM
andrew@webrtc.orgeb524d92013-09-23 23:02:24 +0000434 if (audio_coding_->IncomingPacket(payloadData,
435 payloadSize,
436 *rtpHeader) != 0)
niklase@google.com470e71d2011-07-07 08:21:25 +0000437 {
438 _engineStatisticsPtr->SetLastError(
439 VE_AUDIO_CODING_MODULE_ERROR, kTraceWarning,
440 "Channel::OnReceivedPayloadData() unable to push data to the ACM");
441 return -1;
442 }
443
pwestin@webrtc.orgd30859e2013-06-06 21:09:01 +0000444 // Update the packet delay.
niklase@google.com470e71d2011-07-07 08:21:25 +0000445 UpdatePacketDelay(rtpHeader->header.timestamp,
446 rtpHeader->header.sequenceNumber);
pwestin@webrtc.orgd30859e2013-06-06 21:09:01 +0000447
pkasting@chromium.org16825b12015-01-12 21:51:21 +0000448 int64_t round_trip_time = 0;
wu@webrtc.org822fbd82013-08-15 23:38:54 +0000449 _rtpRtcpModule->RTT(rtp_receiver_->SSRC(), &round_trip_time,
450 NULL, NULL, NULL);
pwestin@webrtc.orgd30859e2013-06-06 21:09:01 +0000451
andrew@webrtc.orgeb524d92013-09-23 23:02:24 +0000452 std::vector<uint16_t> nack_list = audio_coding_->GetNackList(
wu@webrtc.org822fbd82013-08-15 23:38:54 +0000453 round_trip_time);
454 if (!nack_list.empty()) {
455 // Can't use nack_list.data() since it's not supported by all
456 // compilers.
457 ResendPackets(&(nack_list[0]), static_cast<int>(nack_list.size()));
pwestin@webrtc.orgd30859e2013-06-06 21:09:01 +0000458 }
niklase@google.com470e71d2011-07-07 08:21:25 +0000459 return 0;
460}
461
stefan@webrtc.org7bb8f022013-09-06 13:40:11 +0000462bool Channel::OnRecoveredPacket(const uint8_t* rtp_packet,
pkasting@chromium.org4591fbd2014-11-20 22:28:14 +0000463 size_t rtp_packet_length) {
stefan@webrtc.org7bb8f022013-09-06 13:40:11 +0000464 RTPHeader header;
465 if (!rtp_header_parser_->Parse(rtp_packet, rtp_packet_length, &header)) {
466 WEBRTC_TRACE(kTraceDebug, webrtc::kTraceVoice, _channelId,
467 "IncomingPacket invalid RTP header");
468 return false;
469 }
470 header.payload_type_frequency =
471 rtp_payload_registry_->GetPayloadTypeFrequency(header.payloadType);
472 if (header.payload_type_frequency < 0)
473 return false;
474 return ReceivePacket(rtp_packet, rtp_packet_length, header, false);
475}
476
pbos@webrtc.org92135212013-05-14 08:31:39 +0000477int32_t Channel::GetAudioFrame(int32_t id, AudioFrame& audioFrame)
niklase@google.com470e71d2011-07-07 08:21:25 +0000478{
479 WEBRTC_TRACE(kTraceStream, kTraceVoice, VoEId(_instanceId,_channelId),
480 "Channel::GetAudioFrame(id=%d)", id);
481
482 // Get 10ms raw PCM data from the ACM (mixer limits output frequency)
andrew@webrtc.orgeb524d92013-09-23 23:02:24 +0000483 if (audio_coding_->PlayoutData10Ms(audioFrame.sample_rate_hz_,
484 &audioFrame) == -1)
niklase@google.com470e71d2011-07-07 08:21:25 +0000485 {
486 WEBRTC_TRACE(kTraceError, kTraceVoice,
487 VoEId(_instanceId,_channelId),
488 "Channel::GetAudioFrame() PlayoutData10Ms() failed!");
andrew@webrtc.org7859e102012-01-13 00:30:11 +0000489 // In all likelihood, the audio in this frame is garbage. We return an
490 // error so that the audio mixer module doesn't add it to the mix. As
491 // a result, it won't be played out and the actions skipped here are
492 // irrelevant.
493 return -1;
niklase@google.com470e71d2011-07-07 08:21:25 +0000494 }
495
496 if (_RxVadDetection)
497 {
498 UpdateRxVadDetection(audioFrame);
499 }
500
501 // Convert module ID to internal VoE channel ID
andrew@webrtc.org63a50982012-05-02 23:56:37 +0000502 audioFrame.id_ = VoEChannelId(audioFrame.id_);
niklase@google.com470e71d2011-07-07 08:21:25 +0000503 // Store speech type for dead-or-alive detection
andrew@webrtc.org63a50982012-05-02 23:56:37 +0000504 _outputSpeechType = audioFrame.speech_type_;
niklase@google.com470e71d2011-07-07 08:21:25 +0000505
henrika@webrtc.org944cbeb2014-03-18 10:32:33 +0000506 ChannelState::State state = channel_state_.Get();
507
508 if (state.rx_apm_is_enabled) {
andrew@webrtc.org60730cf2014-01-07 17:45:09 +0000509 int err = rx_audioproc_->ProcessStream(&audioFrame);
510 if (err) {
511 LOG(LS_ERROR) << "ProcessStream() error: " << err;
512 assert(false);
513 }
niklase@google.com470e71d2011-07-07 08:21:25 +0000514 }
515
wu@webrtc.org63420662013-10-17 18:28:55 +0000516 float output_gain = 1.0f;
517 float left_pan = 1.0f;
518 float right_pan = 1.0f;
niklase@google.com470e71d2011-07-07 08:21:25 +0000519 {
wu@webrtc.org63420662013-10-17 18:28:55 +0000520 CriticalSectionScoped cs(&volume_settings_critsect_);
521 output_gain = _outputGain;
522 left_pan = _panLeft;
523 right_pan= _panRight;
524 }
525
526 // Output volume scaling
527 if (output_gain < 0.99f || output_gain > 1.01f)
528 {
529 AudioFrameOperations::ScaleWithSat(output_gain, audioFrame);
niklase@google.com470e71d2011-07-07 08:21:25 +0000530 }
531
532 // Scale left and/or right channel(s) if stereo and master balance is
533 // active
534
wu@webrtc.org63420662013-10-17 18:28:55 +0000535 if (left_pan != 1.0f || right_pan != 1.0f)
niklase@google.com470e71d2011-07-07 08:21:25 +0000536 {
andrew@webrtc.org63a50982012-05-02 23:56:37 +0000537 if (audioFrame.num_channels_ == 1)
niklase@google.com470e71d2011-07-07 08:21:25 +0000538 {
539 // Emulate stereo mode since panning is active.
540 // The mono signal is copied to both left and right channels here.
andrew@webrtc.org4ecea3e2012-06-27 03:25:31 +0000541 AudioFrameOperations::MonoToStereo(&audioFrame);
niklase@google.com470e71d2011-07-07 08:21:25 +0000542 }
543 // For true stereo mode (when we are receiving a stereo signal), no
544 // action is needed.
545
546 // Do the panning operation (the audio frame contains stereo at this
547 // stage)
wu@webrtc.org63420662013-10-17 18:28:55 +0000548 AudioFrameOperations::Scale(left_pan, right_pan, audioFrame);
niklase@google.com470e71d2011-07-07 08:21:25 +0000549 }
550
551 // Mix decoded PCM output with file if file mixing is enabled
henrika@webrtc.org944cbeb2014-03-18 10:32:33 +0000552 if (state.output_file_playing)
niklase@google.com470e71d2011-07-07 08:21:25 +0000553 {
andrew@webrtc.org63a50982012-05-02 23:56:37 +0000554 MixAudioWithFile(audioFrame, audioFrame.sample_rate_hz_);
niklase@google.com470e71d2011-07-07 08:21:25 +0000555 }
556
niklase@google.com470e71d2011-07-07 08:21:25 +0000557 // External media
558 if (_outputExternalMedia)
559 {
mflodman@webrtc.org9a065d12012-03-07 08:12:21 +0000560 CriticalSectionScoped cs(&_callbackCritSect);
andrew@webrtc.org63a50982012-05-02 23:56:37 +0000561 const bool isStereo = (audioFrame.num_channels_ == 2);
niklase@google.com470e71d2011-07-07 08:21:25 +0000562 if (_outputExternalMediaCallbackPtr)
563 {
564 _outputExternalMediaCallbackPtr->Process(
565 _channelId,
566 kPlaybackPerChannel,
pbos@webrtc.org6141e132013-04-09 10:09:10 +0000567 (int16_t*)audioFrame.data_,
andrew@webrtc.org63a50982012-05-02 23:56:37 +0000568 audioFrame.samples_per_channel_,
569 audioFrame.sample_rate_hz_,
niklase@google.com470e71d2011-07-07 08:21:25 +0000570 isStereo);
571 }
572 }
573
574 // Record playout if enabled
575 {
mflodman@webrtc.org9a065d12012-03-07 08:12:21 +0000576 CriticalSectionScoped cs(&_fileCritSect);
niklase@google.com470e71d2011-07-07 08:21:25 +0000577
578 if (_outputFileRecording && _outputFileRecorderPtr)
579 {
niklas.enbom@webrtc.org5398d952012-03-26 08:11:25 +0000580 _outputFileRecorderPtr->RecordAudioToFile(audioFrame);
niklase@google.com470e71d2011-07-07 08:21:25 +0000581 }
582 }
583
584 // Measure audio level (0-9)
585 _outputAudioLevel.ComputeLevel(audioFrame);
586
wu@webrtc.org94454b72014-06-05 20:34:08 +0000587 if (capture_start_rtp_time_stamp_ < 0 && audioFrame.timestamp_ != 0) {
588 // The first frame with a valid rtp timestamp.
wu@webrtc.orgcb711f72014-05-19 17:39:11 +0000589 capture_start_rtp_time_stamp_ = audioFrame.timestamp_;
wu@webrtc.org94454b72014-06-05 20:34:08 +0000590 }
591
592 if (capture_start_rtp_time_stamp_ >= 0) {
593 // audioFrame.timestamp_ should be valid from now on.
594
595 // Compute elapsed time.
596 int64_t unwrap_timestamp =
597 rtp_ts_wraparound_handler_->Unwrap(audioFrame.timestamp_);
598 audioFrame.elapsed_time_ms_ =
599 (unwrap_timestamp - capture_start_rtp_time_stamp_) /
600 (GetPlayoutFrequency() / 1000);
601
stefan@webrtc.org8e24d872014-09-02 18:58:24 +0000602 {
wu@webrtc.orgcb711f72014-05-19 17:39:11 +0000603 CriticalSectionScoped lock(ts_stats_lock_.get());
stefan@webrtc.org8e24d872014-09-02 18:58:24 +0000604 // Compute ntp time.
605 audioFrame.ntp_time_ms_ = ntp_estimator_.Estimate(
606 audioFrame.timestamp_);
607 // |ntp_time_ms_| won't be valid until at least 2 RTCP SRs are received.
608 if (audioFrame.ntp_time_ms_ > 0) {
609 // Compute |capture_start_ntp_time_ms_| so that
610 // |capture_start_ntp_time_ms_| + |elapsed_time_ms_| == |ntp_time_ms_|
611 capture_start_ntp_time_ms_ =
612 audioFrame.ntp_time_ms_ - audioFrame.elapsed_time_ms_;
613 }
wu@webrtc.orgcb711f72014-05-19 17:39:11 +0000614 }
615 }
616
niklase@google.com470e71d2011-07-07 08:21:25 +0000617 return 0;
618}
619
pbos@webrtc.org6141e132013-04-09 10:09:10 +0000620int32_t
pbos@webrtc.org92135212013-05-14 08:31:39 +0000621Channel::NeededFrequency(int32_t id)
niklase@google.com470e71d2011-07-07 08:21:25 +0000622{
623 WEBRTC_TRACE(kTraceStream, kTraceVoice, VoEId(_instanceId,_channelId),
624 "Channel::NeededFrequency(id=%d)", id);
625
626 int highestNeeded = 0;
627
628 // Determine highest needed receive frequency
andrew@webrtc.orgeb524d92013-09-23 23:02:24 +0000629 int32_t receiveFrequency = audio_coding_->ReceiveFrequency();
niklase@google.com470e71d2011-07-07 08:21:25 +0000630
631 // Return the bigger of playout and receive frequency in the ACM.
andrew@webrtc.orgeb524d92013-09-23 23:02:24 +0000632 if (audio_coding_->PlayoutFrequency() > receiveFrequency)
niklase@google.com470e71d2011-07-07 08:21:25 +0000633 {
andrew@webrtc.orgeb524d92013-09-23 23:02:24 +0000634 highestNeeded = audio_coding_->PlayoutFrequency();
niklase@google.com470e71d2011-07-07 08:21:25 +0000635 }
636 else
637 {
638 highestNeeded = receiveFrequency;
639 }
640
641 // Special case, if we're playing a file on the playout side
642 // we take that frequency into consideration as well
643 // This is not needed on sending side, since the codec will
644 // limit the spectrum anyway.
henrika@webrtc.org944cbeb2014-03-18 10:32:33 +0000645 if (channel_state_.Get().output_file_playing)
niklase@google.com470e71d2011-07-07 08:21:25 +0000646 {
mflodman@webrtc.org9a065d12012-03-07 08:12:21 +0000647 CriticalSectionScoped cs(&_fileCritSect);
henrika@webrtc.org944cbeb2014-03-18 10:32:33 +0000648 if (_outputFilePlayerPtr)
niklase@google.com470e71d2011-07-07 08:21:25 +0000649 {
650 if(_outputFilePlayerPtr->Frequency()>highestNeeded)
651 {
652 highestNeeded=_outputFilePlayerPtr->Frequency();
653 }
654 }
655 }
656
657 return(highestNeeded);
658}
659
pbos@webrtc.org6141e132013-04-09 10:09:10 +0000660int32_t
niklase@google.com470e71d2011-07-07 08:21:25 +0000661Channel::CreateChannel(Channel*& channel,
pbos@webrtc.org92135212013-05-14 08:31:39 +0000662 int32_t channelId,
minyue@webrtc.orge509f942013-09-12 17:03:00 +0000663 uint32_t instanceId,
664 const Config& config)
niklase@google.com470e71d2011-07-07 08:21:25 +0000665{
666 WEBRTC_TRACE(kTraceMemory, kTraceVoice, VoEId(instanceId,channelId),
667 "Channel::CreateChannel(channelId=%d, instanceId=%d)",
668 channelId, instanceId);
669
minyue@webrtc.orge509f942013-09-12 17:03:00 +0000670 channel = new Channel(channelId, instanceId, config);
niklase@google.com470e71d2011-07-07 08:21:25 +0000671 if (channel == NULL)
672 {
673 WEBRTC_TRACE(kTraceMemory, kTraceVoice,
674 VoEId(instanceId,channelId),
675 "Channel::CreateChannel() unable to allocate memory for"
676 " channel");
677 return -1;
678 }
679 return 0;
680}
681
682void
pbos@webrtc.org92135212013-05-14 08:31:39 +0000683Channel::PlayNotification(int32_t id, uint32_t durationMs)
niklase@google.com470e71d2011-07-07 08:21:25 +0000684{
685 WEBRTC_TRACE(kTraceStream, kTraceVoice, VoEId(_instanceId,_channelId),
686 "Channel::PlayNotification(id=%d, durationMs=%d)",
687 id, durationMs);
688
689 // Not implement yet
690}
691
692void
pbos@webrtc.org92135212013-05-14 08:31:39 +0000693Channel::RecordNotification(int32_t id, uint32_t durationMs)
niklase@google.com470e71d2011-07-07 08:21:25 +0000694{
695 WEBRTC_TRACE(kTraceStream, kTraceVoice, VoEId(_instanceId,_channelId),
696 "Channel::RecordNotification(id=%d, durationMs=%d)",
697 id, durationMs);
698
699 // Not implement yet
700}
701
702void
pbos@webrtc.org92135212013-05-14 08:31:39 +0000703Channel::PlayFileEnded(int32_t id)
niklase@google.com470e71d2011-07-07 08:21:25 +0000704{
705 WEBRTC_TRACE(kTraceStream, kTraceVoice, VoEId(_instanceId,_channelId),
706 "Channel::PlayFileEnded(id=%d)", id);
707
708 if (id == _inputFilePlayerId)
709 {
henrika@webrtc.org944cbeb2014-03-18 10:32:33 +0000710 channel_state_.SetInputFilePlaying(false);
niklase@google.com470e71d2011-07-07 08:21:25 +0000711 WEBRTC_TRACE(kTraceStateInfo, kTraceVoice,
712 VoEId(_instanceId,_channelId),
713 "Channel::PlayFileEnded() => input file player module is"
714 " shutdown");
715 }
716 else if (id == _outputFilePlayerId)
717 {
henrika@webrtc.org944cbeb2014-03-18 10:32:33 +0000718 channel_state_.SetOutputFilePlaying(false);
niklase@google.com470e71d2011-07-07 08:21:25 +0000719 WEBRTC_TRACE(kTraceStateInfo, kTraceVoice,
720 VoEId(_instanceId,_channelId),
721 "Channel::PlayFileEnded() => output file player module is"
722 " shutdown");
723 }
724}
725
726void
pbos@webrtc.org92135212013-05-14 08:31:39 +0000727Channel::RecordFileEnded(int32_t id)
niklase@google.com470e71d2011-07-07 08:21:25 +0000728{
729 WEBRTC_TRACE(kTraceStream, kTraceVoice, VoEId(_instanceId,_channelId),
730 "Channel::RecordFileEnded(id=%d)", id);
731
732 assert(id == _outputFileRecorderId);
733
mflodman@webrtc.org9a065d12012-03-07 08:12:21 +0000734 CriticalSectionScoped cs(&_fileCritSect);
niklase@google.com470e71d2011-07-07 08:21:25 +0000735
736 _outputFileRecording = false;
737 WEBRTC_TRACE(kTraceStateInfo, kTraceVoice,
738 VoEId(_instanceId,_channelId),
739 "Channel::RecordFileEnded() => output file recorder module is"
740 " shutdown");
741}
742
pbos@webrtc.org92135212013-05-14 08:31:39 +0000743Channel::Channel(int32_t channelId,
minyue@webrtc.orge509f942013-09-12 17:03:00 +0000744 uint32_t instanceId,
745 const Config& config) :
niklase@google.com470e71d2011-07-07 08:21:25 +0000746 _fileCritSect(*CriticalSectionWrapper::CreateCriticalSection()),
747 _callbackCritSect(*CriticalSectionWrapper::CreateCriticalSection()),
wu@webrtc.org63420662013-10-17 18:28:55 +0000748 volume_settings_critsect_(*CriticalSectionWrapper::CreateCriticalSection()),
niklase@google.com470e71d2011-07-07 08:21:25 +0000749 _instanceId(instanceId),
xians@google.com22963ab2011-08-03 12:40:23 +0000750 _channelId(channelId),
stefan@webrtc.orga5cb98c2013-05-29 12:12:51 +0000751 rtp_header_parser_(RtpHeaderParser::Create()),
wu@webrtc.org822fbd82013-08-15 23:38:54 +0000752 rtp_payload_registry_(
andresp@webrtc.orgdc80bae2014-04-08 11:06:12 +0000753 new RTPPayloadRegistry(RTPPayloadStrategy::CreateStrategy(true))),
wu@webrtc.org822fbd82013-08-15 23:38:54 +0000754 rtp_receive_statistics_(ReceiveStatistics::Create(
755 Clock::GetRealTimeClock())),
756 rtp_receiver_(RtpReceiver::CreateAudioReceiver(
757 VoEModuleId(instanceId, channelId), Clock::GetRealTimeClock(), this,
758 this, this, rtp_payload_registry_.get())),
759 telephone_event_handler_(rtp_receiver_->GetTelephoneEventHandler()),
henrik.lundin@webrtc.org34fe0152014-04-22 19:04:34 +0000760 audio_coding_(AudioCodingModule::Create(
xians@google.com22963ab2011-08-03 12:40:23 +0000761 VoEModuleId(instanceId, channelId))),
niklase@google.com470e71d2011-07-07 08:21:25 +0000762 _rtpDumpIn(*RtpDump::CreateRtpDump()),
763 _rtpDumpOut(*RtpDump::CreateRtpDump()),
niklase@google.com470e71d2011-07-07 08:21:25 +0000764 _outputAudioLevel(),
niklase@google.com470e71d2011-07-07 08:21:25 +0000765 _externalTransport(false),
niklase@google.com470e71d2011-07-07 08:21:25 +0000766 _inputFilePlayerPtr(NULL),
767 _outputFilePlayerPtr(NULL),
768 _outputFileRecorderPtr(NULL),
769 // Avoid conflict with other channels by adding 1024 - 1026,
770 // won't use as much as 1024 channels.
771 _inputFilePlayerId(VoEModuleId(instanceId, channelId) + 1024),
772 _outputFilePlayerId(VoEModuleId(instanceId, channelId) + 1025),
773 _outputFileRecorderId(VoEModuleId(instanceId, channelId) + 1026),
niklase@google.com470e71d2011-07-07 08:21:25 +0000774 _outputFileRecording(false),
xians@google.com22963ab2011-08-03 12:40:23 +0000775 _inbandDtmfQueue(VoEModuleId(instanceId, channelId)),
776 _inbandDtmfGenerator(VoEModuleId(instanceId, channelId)),
xians@google.com22963ab2011-08-03 12:40:23 +0000777 _outputExternalMedia(false),
niklase@google.com470e71d2011-07-07 08:21:25 +0000778 _inputExternalMediaCallbackPtr(NULL),
779 _outputExternalMediaCallbackPtr(NULL),
xians@google.com22963ab2011-08-03 12:40:23 +0000780 _timeStamp(0), // This is just an offset, RTP module will add it's own random offset
781 _sendTelephoneEventPayloadType(106),
stefan@webrtc.org8e24d872014-09-02 18:58:24 +0000782 ntp_estimator_(Clock::GetRealTimeClock()),
turaj@webrtc.org167b6df2013-12-13 21:05:07 +0000783 jitter_buffer_playout_timestamp_(0),
pwestin@webrtc.org1de01352013-04-11 20:23:35 +0000784 playout_timestamp_rtp_(0),
785 playout_timestamp_rtcp_(0),
sprang@webrtc.org54ae4ff2013-12-19 13:26:02 +0000786 playout_delay_ms_(0),
xians@google.com22963ab2011-08-03 12:40:23 +0000787 _numberOfDiscardedPackets(0),
xians@webrtc.org09e8c472013-07-31 16:30:19 +0000788 send_sequence_number_(0),
wu@webrtc.orgcb711f72014-05-19 17:39:11 +0000789 ts_stats_lock_(CriticalSectionWrapper::CreateCriticalSection()),
wu@webrtc.org94454b72014-06-05 20:34:08 +0000790 rtp_ts_wraparound_handler_(new rtc::TimestampWrapAroundHandler()),
791 capture_start_rtp_time_stamp_(-1),
wu@webrtc.orgcb711f72014-05-19 17:39:11 +0000792 capture_start_ntp_time_ms_(-1),
xians@google.com22963ab2011-08-03 12:40:23 +0000793 _engineStatisticsPtr(NULL),
henrika@webrtc.org2919e952012-01-31 08:45:03 +0000794 _outputMixerPtr(NULL),
795 _transmitMixerPtr(NULL),
xians@google.com22963ab2011-08-03 12:40:23 +0000796 _moduleProcessThreadPtr(NULL),
797 _audioDeviceModulePtr(NULL),
798 _voiceEngineObserverPtr(NULL),
799 _callbackCritSectPtr(NULL),
800 _transportPtr(NULL),
xians@google.com22963ab2011-08-03 12:40:23 +0000801 _rxVadObserverPtr(NULL),
802 _oldVadDecision(-1),
803 _sendFrameType(0),
roosa@google.com1b60ceb2012-12-12 23:00:29 +0000804 _externalMixing(false),
xians@google.com22963ab2011-08-03 12:40:23 +0000805 _mixFileWithMicrophone(false),
niklase@google.com470e71d2011-07-07 08:21:25 +0000806 _mute(false),
807 _panLeft(1.0f),
808 _panRight(1.0f),
809 _outputGain(1.0f),
810 _playOutbandDtmfEvent(false),
811 _playInbandDtmfEvent(false),
niklase@google.com470e71d2011-07-07 08:21:25 +0000812 _lastLocalTimeStamp(0),
813 _lastPayloadType(0),
xians@google.com22963ab2011-08-03 12:40:23 +0000814 _includeAudioLevelIndication(false),
niklase@google.com470e71d2011-07-07 08:21:25 +0000815 _outputSpeechType(AudioFrame::kNormalSpeech),
solenberg@webrtc.orgb1f50102014-03-24 10:38:25 +0000816 vie_network_(NULL),
817 video_channel_(-1),
pwestin@webrtc.org1de01352013-04-11 20:23:35 +0000818 _average_jitter_buffer_delay_us(0),
turaj@webrtc.orge46c8d32013-05-22 20:39:43 +0000819 least_required_delay_ms_(0),
niklase@google.com470e71d2011-07-07 08:21:25 +0000820 _previousTimestamp(0),
821 _recPacketDelayMs(20),
822 _RxVadDetection(false),
niklase@google.com470e71d2011-07-07 08:21:25 +0000823 _rxAgcIsEnabled(false),
stefan@webrtc.org7bb8f022013-09-06 13:40:11 +0000824 _rxNsIsEnabled(false),
minyue@webrtc.orgc1a40a72014-05-28 09:52:06 +0000825 restored_packet_in_use_(false),
mflodman@webrtc.org0a7d4ee2015-02-17 12:57:14 +0000826 rtcp_observer_(new VoERtcpObserver(this)),
minyue@webrtc.org74aaf292014-07-16 21:28:26 +0000827 network_predictor_(new NetworkPredictor(Clock::GetRealTimeClock()))
niklase@google.com470e71d2011-07-07 08:21:25 +0000828{
829 WEBRTC_TRACE(kTraceMemory, kTraceVoice, VoEId(_instanceId,_channelId),
830 "Channel::Channel() - ctor");
831 _inbandDtmfQueue.ResetDtmf();
832 _inbandDtmfGenerator.Init();
833 _outputAudioLevel.Clear();
834
pwestin@webrtc.org2853dde2012-05-11 11:08:54 +0000835 RtpRtcp::Configuration configuration;
836 configuration.id = VoEModuleId(instanceId, channelId);
837 configuration.audio = true;
pwestin@webrtc.org2853dde2012-05-11 11:08:54 +0000838 configuration.outgoing_transport = this;
pwestin@webrtc.org2853dde2012-05-11 11:08:54 +0000839 configuration.audio_messages = this;
wu@webrtc.org822fbd82013-08-15 23:38:54 +0000840 configuration.receive_statistics = rtp_receive_statistics_.get();
mflodman@webrtc.org0a7d4ee2015-02-17 12:57:14 +0000841 configuration.bandwidth_callback = rtcp_observer_.get();
pwestin@webrtc.org2853dde2012-05-11 11:08:54 +0000842
843 _rtpRtcpModule.reset(RtpRtcp::CreateRtpRtcp(configuration));
sprang@webrtc.org54ae4ff2013-12-19 13:26:02 +0000844
845 statistics_proxy_.reset(new StatisticsProxy(_rtpRtcpModule->SSRC()));
846 rtp_receive_statistics_->RegisterRtcpStatisticsCallback(
847 statistics_proxy_.get());
aluebs@webrtc.orgf927fd62014-04-16 11:58:18 +0000848
849 Config audioproc_config;
850 audioproc_config.Set<ExperimentalAgc>(new ExperimentalAgc(false));
851 rx_audioproc_.reset(AudioProcessing::Create(audioproc_config));
niklase@google.com470e71d2011-07-07 08:21:25 +0000852}
853
854Channel::~Channel()
855{
sprang@webrtc.org54ae4ff2013-12-19 13:26:02 +0000856 rtp_receive_statistics_->RegisterRtcpStatisticsCallback(NULL);
niklase@google.com470e71d2011-07-07 08:21:25 +0000857 WEBRTC_TRACE(kTraceMemory, kTraceVoice, VoEId(_instanceId,_channelId),
858 "Channel::~Channel() - dtor");
859
860 if (_outputExternalMedia)
861 {
862 DeRegisterExternalMediaProcessing(kPlaybackPerChannel);
863 }
henrika@webrtc.org944cbeb2014-03-18 10:32:33 +0000864 if (channel_state_.Get().input_external_media)
niklase@google.com470e71d2011-07-07 08:21:25 +0000865 {
866 DeRegisterExternalMediaProcessing(kRecordingPerChannel);
867 }
868 StopSend();
niklase@google.com470e71d2011-07-07 08:21:25 +0000869 StopPlayout();
870
871 {
mflodman@webrtc.org9a065d12012-03-07 08:12:21 +0000872 CriticalSectionScoped cs(&_fileCritSect);
niklase@google.com470e71d2011-07-07 08:21:25 +0000873 if (_inputFilePlayerPtr)
874 {
875 _inputFilePlayerPtr->RegisterModuleFileCallback(NULL);
876 _inputFilePlayerPtr->StopPlayingFile();
877 FilePlayer::DestroyFilePlayer(_inputFilePlayerPtr);
878 _inputFilePlayerPtr = NULL;
879 }
880 if (_outputFilePlayerPtr)
881 {
882 _outputFilePlayerPtr->RegisterModuleFileCallback(NULL);
883 _outputFilePlayerPtr->StopPlayingFile();
884 FilePlayer::DestroyFilePlayer(_outputFilePlayerPtr);
885 _outputFilePlayerPtr = NULL;
886 }
887 if (_outputFileRecorderPtr)
888 {
889 _outputFileRecorderPtr->RegisterModuleFileCallback(NULL);
890 _outputFileRecorderPtr->StopRecording();
891 FileRecorder::DestroyFileRecorder(_outputFileRecorderPtr);
892 _outputFileRecorderPtr = NULL;
893 }
894 }
895
896 // The order to safely shutdown modules in a channel is:
897 // 1. De-register callbacks in modules
898 // 2. De-register modules in process thread
899 // 3. Destroy modules
andrew@webrtc.orgeb524d92013-09-23 23:02:24 +0000900 if (audio_coding_->RegisterTransportCallback(NULL) == -1)
niklase@google.com470e71d2011-07-07 08:21:25 +0000901 {
902 WEBRTC_TRACE(kTraceWarning, kTraceVoice,
903 VoEId(_instanceId,_channelId),
904 "~Channel() failed to de-register transport callback"
905 " (Audio coding module)");
906 }
andrew@webrtc.orgeb524d92013-09-23 23:02:24 +0000907 if (audio_coding_->RegisterVADCallback(NULL) == -1)
niklase@google.com470e71d2011-07-07 08:21:25 +0000908 {
909 WEBRTC_TRACE(kTraceWarning, kTraceVoice,
910 VoEId(_instanceId,_channelId),
911 "~Channel() failed to de-register VAD callback"
912 " (Audio coding module)");
913 }
niklase@google.com470e71d2011-07-07 08:21:25 +0000914 // De-register modules in process thread
tommi@webrtc.org3985f012015-02-27 13:36:34 +0000915 _moduleProcessThreadPtr->DeRegisterModule(_rtpRtcpModule.get());
916
niklase@google.com470e71d2011-07-07 08:21:25 +0000917 // End of modules shutdown
918
919 // Delete other objects
solenberg@webrtc.orgb1f50102014-03-24 10:38:25 +0000920 if (vie_network_) {
921 vie_network_->Release();
922 vie_network_ = NULL;
923 }
niklase@google.com470e71d2011-07-07 08:21:25 +0000924 RtpDump::DestroyRtpDump(&_rtpDumpIn);
925 RtpDump::DestroyRtpDump(&_rtpDumpOut);
niklase@google.com470e71d2011-07-07 08:21:25 +0000926 delete &_callbackCritSect;
niklase@google.com470e71d2011-07-07 08:21:25 +0000927 delete &_fileCritSect;
wu@webrtc.org63420662013-10-17 18:28:55 +0000928 delete &volume_settings_critsect_;
niklase@google.com470e71d2011-07-07 08:21:25 +0000929}
930
pbos@webrtc.org6141e132013-04-09 10:09:10 +0000931int32_t
niklase@google.com470e71d2011-07-07 08:21:25 +0000932Channel::Init()
933{
934 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId,_channelId),
935 "Channel::Init()");
936
henrika@webrtc.org944cbeb2014-03-18 10:32:33 +0000937 channel_state_.Reset();
938
niklase@google.com470e71d2011-07-07 08:21:25 +0000939 // --- Initial sanity
940
941 if ((_engineStatisticsPtr == NULL) ||
942 (_moduleProcessThreadPtr == NULL))
943 {
944 WEBRTC_TRACE(kTraceError, kTraceVoice,
945 VoEId(_instanceId,_channelId),
946 "Channel::Init() must call SetEngineInformation() first");
947 return -1;
948 }
949
950 // --- Add modules to process thread (for periodic schedulation)
951
tommi@webrtc.org3985f012015-02-27 13:36:34 +0000952 _moduleProcessThreadPtr->RegisterModule(_rtpRtcpModule.get());
953
pwestin@webrtc.orgc450a192012-01-04 15:00:12 +0000954 // --- ACM initialization
niklase@google.com470e71d2011-07-07 08:21:25 +0000955
Henrik Lundin45c64492015-03-30 19:00:44 +0200956 if ((audio_coding_->InitializeReceiver() == -1)
niklase@google.com470e71d2011-07-07 08:21:25 +0000957#ifdef WEBRTC_CODEC_AVT
958 // out-of-band Dtmf tones are played out by default
Henrik Lundin45c64492015-03-30 19:00:44 +0200959 || (audio_coding_->SetDtmfPlayoutStatus(true) == -1)
niklase@google.com470e71d2011-07-07 08:21:25 +0000960#endif
Henrik Lundin45c64492015-03-30 19:00:44 +0200961 )
niklase@google.com470e71d2011-07-07 08:21:25 +0000962 {
963 _engineStatisticsPtr->SetLastError(
964 VE_AUDIO_CODING_MODULE_ERROR, kTraceError,
965 "Channel::Init() unable to initialize the ACM - 1");
966 return -1;
967 }
968
969 // --- RTP/RTCP module initialization
970
971 // Ensure that RTCP is enabled by default for the created channel.
972 // Note that, the module will keep generating RTCP until it is explicitly
973 // disabled by the user.
974 // After StopListen (when no sockets exists), RTCP packets will no longer
975 // be transmitted since the Transport object will then be invalid.
wu@webrtc.org822fbd82013-08-15 23:38:54 +0000976 telephone_event_handler_->SetTelephoneEventForwardToDecoder(true);
977 // RTCP is enabled by default.
pbos@webrtc.orgd16e8392014-12-19 13:49:55 +0000978 _rtpRtcpModule->SetRTCPStatus(kRtcpCompound);
979 // --- Register all permanent callbacks
niklase@google.com470e71d2011-07-07 08:21:25 +0000980 const bool fail =
andrew@webrtc.orgeb524d92013-09-23 23:02:24 +0000981 (audio_coding_->RegisterTransportCallback(this) == -1) ||
982 (audio_coding_->RegisterVADCallback(this) == -1);
niklase@google.com470e71d2011-07-07 08:21:25 +0000983
984 if (fail)
985 {
986 _engineStatisticsPtr->SetLastError(
987 VE_CANNOT_INIT_CHANNEL, kTraceError,
988 "Channel::Init() callbacks not registered");
989 return -1;
990 }
991
992 // --- Register all supported codecs to the receiving side of the
993 // RTP/RTCP module
994
995 CodecInst codec;
pbos@webrtc.org6141e132013-04-09 10:09:10 +0000996 const uint8_t nSupportedCodecs = AudioCodingModule::NumberOfCodecs();
niklase@google.com470e71d2011-07-07 08:21:25 +0000997
998 for (int idx = 0; idx < nSupportedCodecs; idx++)
999 {
1000 // Open up the RTP/RTCP receiver for all supported codecs
andrew@webrtc.orgeb524d92013-09-23 23:02:24 +00001001 if ((audio_coding_->Codec(idx, &codec) == -1) ||
wu@webrtc.org822fbd82013-08-15 23:38:54 +00001002 (rtp_receiver_->RegisterReceivePayload(
1003 codec.plname,
1004 codec.pltype,
1005 codec.plfreq,
1006 codec.channels,
1007 (codec.rate < 0) ? 0 : codec.rate) == -1))
niklase@google.com470e71d2011-07-07 08:21:25 +00001008 {
1009 WEBRTC_TRACE(kTraceWarning, kTraceVoice,
1010 VoEId(_instanceId,_channelId),
1011 "Channel::Init() unable to register %s (%d/%d/%d/%d) "
1012 "to RTP/RTCP receiver",
1013 codec.plname, codec.pltype, codec.plfreq,
1014 codec.channels, codec.rate);
1015 }
1016 else
1017 {
1018 WEBRTC_TRACE(kTraceInfo, kTraceVoice,
1019 VoEId(_instanceId,_channelId),
1020 "Channel::Init() %s (%d/%d/%d/%d) has been added to "
1021 "the RTP/RTCP receiver",
1022 codec.plname, codec.pltype, codec.plfreq,
1023 codec.channels, codec.rate);
1024 }
1025
1026 // Ensure that PCMU is used as default codec on the sending side
tina.legrand@webrtc.org45175852012-06-01 09:27:35 +00001027 if (!STR_CASE_CMP(codec.plname, "PCMU") && (codec.channels == 1))
niklase@google.com470e71d2011-07-07 08:21:25 +00001028 {
1029 SetSendCodec(codec);
1030 }
1031
1032 // Register default PT for outband 'telephone-event'
1033 if (!STR_CASE_CMP(codec.plname, "telephone-event"))
1034 {
pwestin@webrtc.org2853dde2012-05-11 11:08:54 +00001035 if ((_rtpRtcpModule->RegisterSendPayload(codec) == -1) ||
andrew@webrtc.orgeb524d92013-09-23 23:02:24 +00001036 (audio_coding_->RegisterReceiveCodec(codec) == -1))
niklase@google.com470e71d2011-07-07 08:21:25 +00001037 {
1038 WEBRTC_TRACE(kTraceWarning, kTraceVoice,
1039 VoEId(_instanceId,_channelId),
1040 "Channel::Init() failed to register outband "
1041 "'telephone-event' (%d/%d) correctly",
1042 codec.pltype, codec.plfreq);
1043 }
1044 }
1045
1046 if (!STR_CASE_CMP(codec.plname, "CN"))
1047 {
andrew@webrtc.orgeb524d92013-09-23 23:02:24 +00001048 if ((audio_coding_->RegisterSendCodec(codec) == -1) ||
1049 (audio_coding_->RegisterReceiveCodec(codec) == -1) ||
pwestin@webrtc.org2853dde2012-05-11 11:08:54 +00001050 (_rtpRtcpModule->RegisterSendPayload(codec) == -1))
niklase@google.com470e71d2011-07-07 08:21:25 +00001051 {
1052 WEBRTC_TRACE(kTraceWarning, kTraceVoice,
1053 VoEId(_instanceId,_channelId),
1054 "Channel::Init() failed to register CN (%d/%d) "
1055 "correctly - 1",
1056 codec.pltype, codec.plfreq);
1057 }
1058 }
1059#ifdef WEBRTC_CODEC_RED
1060 // Register RED to the receiving side of the ACM.
1061 // We will not receive an OnInitializeDecoder() callback for RED.
1062 if (!STR_CASE_CMP(codec.plname, "RED"))
1063 {
andrew@webrtc.orgeb524d92013-09-23 23:02:24 +00001064 if (audio_coding_->RegisterReceiveCodec(codec) == -1)
niklase@google.com470e71d2011-07-07 08:21:25 +00001065 {
1066 WEBRTC_TRACE(kTraceWarning, kTraceVoice,
1067 VoEId(_instanceId,_channelId),
1068 "Channel::Init() failed to register RED (%d/%d) "
1069 "correctly",
1070 codec.pltype, codec.plfreq);
1071 }
1072 }
1073#endif
1074 }
pwestin@webrtc.org684f0572013-03-13 23:20:57 +00001075
andrew@webrtc.org6c264cc2013-10-04 17:54:09 +00001076 if (rx_audioproc_->noise_suppression()->set_level(kDefaultNsMode) != 0) {
1077 LOG_FERR1(LS_ERROR, noise_suppression()->set_level, kDefaultNsMode);
1078 return -1;
niklase@google.com470e71d2011-07-07 08:21:25 +00001079 }
andrew@webrtc.org6c264cc2013-10-04 17:54:09 +00001080 if (rx_audioproc_->gain_control()->set_mode(kDefaultRxAgcMode) != 0) {
1081 LOG_FERR1(LS_ERROR, gain_control()->set_mode, kDefaultRxAgcMode);
1082 return -1;
niklase@google.com470e71d2011-07-07 08:21:25 +00001083 }
1084
1085 return 0;
1086}
1087
pbos@webrtc.org6141e132013-04-09 10:09:10 +00001088int32_t
niklase@google.com470e71d2011-07-07 08:21:25 +00001089Channel::SetEngineInformation(Statistics& engineStatistics,
1090 OutputMixer& outputMixer,
1091 voe::TransmitMixer& transmitMixer,
1092 ProcessThread& moduleProcessThread,
1093 AudioDeviceModule& audioDeviceModule,
1094 VoiceEngineObserver* voiceEngineObserver,
1095 CriticalSectionWrapper* callbackCritSect)
1096{
1097 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId,_channelId),
1098 "Channel::SetEngineInformation()");
1099 _engineStatisticsPtr = &engineStatistics;
1100 _outputMixerPtr = &outputMixer;
1101 _transmitMixerPtr = &transmitMixer,
1102 _moduleProcessThreadPtr = &moduleProcessThread;
1103 _audioDeviceModulePtr = &audioDeviceModule;
1104 _voiceEngineObserverPtr = voiceEngineObserver;
1105 _callbackCritSectPtr = callbackCritSect;
1106 return 0;
1107}
1108
pbos@webrtc.org6141e132013-04-09 10:09:10 +00001109int32_t
niklase@google.com470e71d2011-07-07 08:21:25 +00001110Channel::UpdateLocalTimeStamp()
1111{
1112
andrew@webrtc.org63a50982012-05-02 23:56:37 +00001113 _timeStamp += _audioFrame.samples_per_channel_;
niklase@google.com470e71d2011-07-07 08:21:25 +00001114 return 0;
1115}
1116
pbos@webrtc.org6141e132013-04-09 10:09:10 +00001117int32_t
niklase@google.com470e71d2011-07-07 08:21:25 +00001118Channel::StartPlayout()
1119{
1120 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId,_channelId),
1121 "Channel::StartPlayout()");
henrika@webrtc.org944cbeb2014-03-18 10:32:33 +00001122 if (channel_state_.Get().playing)
niklase@google.com470e71d2011-07-07 08:21:25 +00001123 {
1124 return 0;
1125 }
roosa@google.com1b60ceb2012-12-12 23:00:29 +00001126
1127 if (!_externalMixing) {
1128 // Add participant as candidates for mixing.
1129 if (_outputMixerPtr->SetMixabilityStatus(*this, true) != 0)
1130 {
1131 _engineStatisticsPtr->SetLastError(
1132 VE_AUDIO_CONF_MIX_MODULE_ERROR, kTraceError,
1133 "StartPlayout() failed to add participant to mixer");
1134 return -1;
1135 }
niklase@google.com470e71d2011-07-07 08:21:25 +00001136 }
1137
henrika@webrtc.org944cbeb2014-03-18 10:32:33 +00001138 channel_state_.SetPlaying(true);
braveyao@webrtc.orgab129902012-06-04 03:26:39 +00001139 if (RegisterFilePlayingToMixer() != 0)
1140 return -1;
1141
niklase@google.com470e71d2011-07-07 08:21:25 +00001142 return 0;
1143}
1144
pbos@webrtc.org6141e132013-04-09 10:09:10 +00001145int32_t
niklase@google.com470e71d2011-07-07 08:21:25 +00001146Channel::StopPlayout()
1147{
1148 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId,_channelId),
1149 "Channel::StopPlayout()");
henrika@webrtc.org944cbeb2014-03-18 10:32:33 +00001150 if (!channel_state_.Get().playing)
niklase@google.com470e71d2011-07-07 08:21:25 +00001151 {
1152 return 0;
1153 }
roosa@google.com1b60ceb2012-12-12 23:00:29 +00001154
1155 if (!_externalMixing) {
1156 // Remove participant as candidates for mixing
1157 if (_outputMixerPtr->SetMixabilityStatus(*this, false) != 0)
1158 {
1159 _engineStatisticsPtr->SetLastError(
1160 VE_AUDIO_CONF_MIX_MODULE_ERROR, kTraceError,
1161 "StopPlayout() failed to remove participant from mixer");
1162 return -1;
1163 }
niklase@google.com470e71d2011-07-07 08:21:25 +00001164 }
1165
henrika@webrtc.org944cbeb2014-03-18 10:32:33 +00001166 channel_state_.SetPlaying(false);
niklase@google.com470e71d2011-07-07 08:21:25 +00001167 _outputAudioLevel.Clear();
1168
1169 return 0;
1170}
1171
pbos@webrtc.org6141e132013-04-09 10:09:10 +00001172int32_t
niklase@google.com470e71d2011-07-07 08:21:25 +00001173Channel::StartSend()
1174{
1175 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId,_channelId),
1176 "Channel::StartSend()");
xians@webrtc.org09e8c472013-07-31 16:30:19 +00001177 // Resume the previous sequence number which was reset by StopSend().
henrika@webrtc.org944cbeb2014-03-18 10:32:33 +00001178 // This needs to be done before |sending| is set to true.
xians@webrtc.org09e8c472013-07-31 16:30:19 +00001179 if (send_sequence_number_)
1180 SetInitSequenceNumber(send_sequence_number_);
1181
henrika@webrtc.org944cbeb2014-03-18 10:32:33 +00001182 if (channel_state_.Get().sending)
niklase@google.com470e71d2011-07-07 08:21:25 +00001183 {
henrika@webrtc.org944cbeb2014-03-18 10:32:33 +00001184 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +00001185 }
henrika@webrtc.org944cbeb2014-03-18 10:32:33 +00001186 channel_state_.SetSending(true);
xians@webrtc.orge07247a2011-11-28 16:31:28 +00001187
pwestin@webrtc.org2853dde2012-05-11 11:08:54 +00001188 if (_rtpRtcpModule->SetSendingStatus(true) != 0)
niklase@google.com470e71d2011-07-07 08:21:25 +00001189 {
1190 _engineStatisticsPtr->SetLastError(
1191 VE_RTP_RTCP_MODULE_ERROR, kTraceError,
1192 "StartSend() RTP/RTCP failed to start sending");
mflodman@webrtc.org9a065d12012-03-07 08:12:21 +00001193 CriticalSectionScoped cs(&_callbackCritSect);
henrika@webrtc.org944cbeb2014-03-18 10:32:33 +00001194 channel_state_.SetSending(false);
niklase@google.com470e71d2011-07-07 08:21:25 +00001195 return -1;
1196 }
xians@webrtc.orge07247a2011-11-28 16:31:28 +00001197
niklase@google.com470e71d2011-07-07 08:21:25 +00001198 return 0;
1199}
1200
pbos@webrtc.org6141e132013-04-09 10:09:10 +00001201int32_t
niklase@google.com470e71d2011-07-07 08:21:25 +00001202Channel::StopSend()
1203{
1204 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId,_channelId),
1205 "Channel::StopSend()");
henrika@webrtc.org944cbeb2014-03-18 10:32:33 +00001206 if (!channel_state_.Get().sending)
niklase@google.com470e71d2011-07-07 08:21:25 +00001207 {
henrika@webrtc.org944cbeb2014-03-18 10:32:33 +00001208 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +00001209 }
henrika@webrtc.org944cbeb2014-03-18 10:32:33 +00001210 channel_state_.SetSending(false);
xians@webrtc.orge07247a2011-11-28 16:31:28 +00001211
xians@webrtc.org09e8c472013-07-31 16:30:19 +00001212 // Store the sequence number to be able to pick up the same sequence for
1213 // the next StartSend(). This is needed for restarting device, otherwise
1214 // it might cause libSRTP to complain about packets being replayed.
1215 // TODO(xians): Remove this workaround after RtpRtcpModule's refactoring
1216 // CL is landed. See issue
1217 // https://code.google.com/p/webrtc/issues/detail?id=2111 .
1218 send_sequence_number_ = _rtpRtcpModule->SequenceNumber();
1219
niklase@google.com470e71d2011-07-07 08:21:25 +00001220 // Reset sending SSRC and sequence number and triggers direct transmission
1221 // of RTCP BYE
pwestin@webrtc.org2853dde2012-05-11 11:08:54 +00001222 if (_rtpRtcpModule->SetSendingStatus(false) == -1 ||
1223 _rtpRtcpModule->ResetSendDataCountersRTP() == -1)
niklase@google.com470e71d2011-07-07 08:21:25 +00001224 {
1225 _engineStatisticsPtr->SetLastError(
1226 VE_RTP_RTCP_MODULE_ERROR, kTraceWarning,
1227 "StartSend() RTP/RTCP failed to stop sending");
1228 }
1229
niklase@google.com470e71d2011-07-07 08:21:25 +00001230 return 0;
1231}
1232
pbos@webrtc.org6141e132013-04-09 10:09:10 +00001233int32_t
niklase@google.com470e71d2011-07-07 08:21:25 +00001234Channel::StartReceiving()
1235{
1236 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId,_channelId),
1237 "Channel::StartReceiving()");
henrika@webrtc.org944cbeb2014-03-18 10:32:33 +00001238 if (channel_state_.Get().receiving)
niklase@google.com470e71d2011-07-07 08:21:25 +00001239 {
1240 return 0;
1241 }
henrika@webrtc.org944cbeb2014-03-18 10:32:33 +00001242 channel_state_.SetReceiving(true);
niklase@google.com470e71d2011-07-07 08:21:25 +00001243 _numberOfDiscardedPackets = 0;
1244 return 0;
1245}
1246
pbos@webrtc.org6141e132013-04-09 10:09:10 +00001247int32_t
niklase@google.com470e71d2011-07-07 08:21:25 +00001248Channel::StopReceiving()
1249{
1250 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId,_channelId),
1251 "Channel::StopReceiving()");
henrika@webrtc.org944cbeb2014-03-18 10:32:33 +00001252 if (!channel_state_.Get().receiving)
niklase@google.com470e71d2011-07-07 08:21:25 +00001253 {
1254 return 0;
1255 }
pwestin@webrtc.org684f0572013-03-13 23:20:57 +00001256
henrika@webrtc.org944cbeb2014-03-18 10:32:33 +00001257 channel_state_.SetReceiving(false);
niklase@google.com470e71d2011-07-07 08:21:25 +00001258 return 0;
1259}
1260
pbos@webrtc.org6141e132013-04-09 10:09:10 +00001261int32_t
niklase@google.com470e71d2011-07-07 08:21:25 +00001262Channel::RegisterVoiceEngineObserver(VoiceEngineObserver& observer)
1263{
1264 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId,_channelId),
1265 "Channel::RegisterVoiceEngineObserver()");
mflodman@webrtc.org9a065d12012-03-07 08:12:21 +00001266 CriticalSectionScoped cs(&_callbackCritSect);
niklase@google.com470e71d2011-07-07 08:21:25 +00001267
1268 if (_voiceEngineObserverPtr)
1269 {
1270 _engineStatisticsPtr->SetLastError(
1271 VE_INVALID_OPERATION, kTraceError,
1272 "RegisterVoiceEngineObserver() observer already enabled");
1273 return -1;
1274 }
1275 _voiceEngineObserverPtr = &observer;
1276 return 0;
1277}
1278
pbos@webrtc.org6141e132013-04-09 10:09:10 +00001279int32_t
niklase@google.com470e71d2011-07-07 08:21:25 +00001280Channel::DeRegisterVoiceEngineObserver()
1281{
1282 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId,_channelId),
1283 "Channel::DeRegisterVoiceEngineObserver()");
mflodman@webrtc.org9a065d12012-03-07 08:12:21 +00001284 CriticalSectionScoped cs(&_callbackCritSect);
niklase@google.com470e71d2011-07-07 08:21:25 +00001285
1286 if (!_voiceEngineObserverPtr)
1287 {
1288 _engineStatisticsPtr->SetLastError(
1289 VE_INVALID_OPERATION, kTraceWarning,
1290 "DeRegisterVoiceEngineObserver() observer already disabled");
1291 return 0;
1292 }
1293 _voiceEngineObserverPtr = NULL;
1294 return 0;
1295}
1296
pbos@webrtc.org6141e132013-04-09 10:09:10 +00001297int32_t
niklase@google.com470e71d2011-07-07 08:21:25 +00001298Channel::GetSendCodec(CodecInst& codec)
1299{
andrew@webrtc.orgeb524d92013-09-23 23:02:24 +00001300 return (audio_coding_->SendCodec(&codec));
niklase@google.com470e71d2011-07-07 08:21:25 +00001301}
1302
pbos@webrtc.org6141e132013-04-09 10:09:10 +00001303int32_t
niklase@google.com470e71d2011-07-07 08:21:25 +00001304Channel::GetRecCodec(CodecInst& codec)
1305{
andrew@webrtc.orgeb524d92013-09-23 23:02:24 +00001306 return (audio_coding_->ReceiveCodec(&codec));
niklase@google.com470e71d2011-07-07 08:21:25 +00001307}
1308
pbos@webrtc.org6141e132013-04-09 10:09:10 +00001309int32_t
niklase@google.com470e71d2011-07-07 08:21:25 +00001310Channel::SetSendCodec(const CodecInst& codec)
1311{
1312 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId,_channelId),
1313 "Channel::SetSendCodec()");
1314
andrew@webrtc.orgeb524d92013-09-23 23:02:24 +00001315 if (audio_coding_->RegisterSendCodec(codec) != 0)
niklase@google.com470e71d2011-07-07 08:21:25 +00001316 {
1317 WEBRTC_TRACE(kTraceError, kTraceVoice, VoEId(_instanceId,_channelId),
1318 "SetSendCodec() failed to register codec to ACM");
1319 return -1;
1320 }
1321
pwestin@webrtc.org2853dde2012-05-11 11:08:54 +00001322 if (_rtpRtcpModule->RegisterSendPayload(codec) != 0)
niklase@google.com470e71d2011-07-07 08:21:25 +00001323 {
pwestin@webrtc.org2853dde2012-05-11 11:08:54 +00001324 _rtpRtcpModule->DeRegisterSendPayload(codec.pltype);
1325 if (_rtpRtcpModule->RegisterSendPayload(codec) != 0)
niklase@google.com470e71d2011-07-07 08:21:25 +00001326 {
1327 WEBRTC_TRACE(
1328 kTraceError, kTraceVoice, VoEId(_instanceId,_channelId),
1329 "SetSendCodec() failed to register codec to"
1330 " RTP/RTCP module");
1331 return -1;
1332 }
1333 }
1334
pwestin@webrtc.org2853dde2012-05-11 11:08:54 +00001335 if (_rtpRtcpModule->SetAudioPacketSize(codec.pacsize) != 0)
niklase@google.com470e71d2011-07-07 08:21:25 +00001336 {
1337 WEBRTC_TRACE(kTraceError, kTraceVoice, VoEId(_instanceId,_channelId),
1338 "SetSendCodec() failed to set audio packet size");
1339 return -1;
1340 }
1341
1342 return 0;
1343}
1344
Ivo Creusenadf89b72015-04-29 16:03:33 +02001345void Channel::SetBitRate(int bitrate_bps) {
1346 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId, _channelId),
1347 "Channel::SetBitRate(bitrate_bps=%d)", bitrate_bps);
1348 audio_coding_->SetBitRate(bitrate_bps);
1349}
1350
mflodman@webrtc.org0a7d4ee2015-02-17 12:57:14 +00001351void Channel::OnIncomingFractionLoss(int fraction_lost) {
minyue@webrtc.org74aaf292014-07-16 21:28:26 +00001352 network_predictor_->UpdatePacketLossRate(fraction_lost);
mflodman@webrtc.org0a7d4ee2015-02-17 12:57:14 +00001353 uint8_t average_fraction_loss = network_predictor_->GetLossRate();
1354
minyue@webrtc.orgc1a40a72014-05-28 09:52:06 +00001355 // Normalizes rate to 0 - 100.
mflodman@webrtc.org0a7d4ee2015-02-17 12:57:14 +00001356 if (audio_coding_->SetPacketLossRate(
1357 100 * average_fraction_loss / 255) != 0) {
minyue@webrtc.orgc1a40a72014-05-28 09:52:06 +00001358 assert(false); // This should not happen.
1359 }
1360}
1361
pbos@webrtc.org6141e132013-04-09 10:09:10 +00001362int32_t
niklase@google.com470e71d2011-07-07 08:21:25 +00001363Channel::SetVADStatus(bool enableVAD, ACMVADMode mode, bool disableDTX)
1364{
1365 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId,_channelId),
1366 "Channel::SetVADStatus(mode=%d)", mode);
henrik.lundin@webrtc.org664ccb72015-01-28 14:49:05 +00001367 assert(!(disableDTX && enableVAD)); // disableDTX mode is deprecated.
niklase@google.com470e71d2011-07-07 08:21:25 +00001368 // To disable VAD, DTX must be disabled too
1369 disableDTX = ((enableVAD == false) ? true : disableDTX);
andrew@webrtc.orgeb524d92013-09-23 23:02:24 +00001370 if (audio_coding_->SetVAD(!disableDTX, enableVAD, mode) != 0)
niklase@google.com470e71d2011-07-07 08:21:25 +00001371 {
1372 _engineStatisticsPtr->SetLastError(
1373 VE_AUDIO_CODING_MODULE_ERROR, kTraceError,
1374 "SetVADStatus() failed to set VAD");
1375 return -1;
1376 }
1377 return 0;
1378}
1379
pbos@webrtc.org6141e132013-04-09 10:09:10 +00001380int32_t
niklase@google.com470e71d2011-07-07 08:21:25 +00001381Channel::GetVADStatus(bool& enabledVAD, ACMVADMode& mode, bool& disabledDTX)
1382{
1383 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId,_channelId),
1384 "Channel::GetVADStatus");
andrew@webrtc.orgeb524d92013-09-23 23:02:24 +00001385 if (audio_coding_->VAD(&disabledDTX, &enabledVAD, &mode) != 0)
niklase@google.com470e71d2011-07-07 08:21:25 +00001386 {
1387 _engineStatisticsPtr->SetLastError(
1388 VE_AUDIO_CODING_MODULE_ERROR, kTraceError,
1389 "GetVADStatus() failed to get VAD status");
1390 return -1;
1391 }
1392 disabledDTX = !disabledDTX;
1393 return 0;
1394}
1395
pbos@webrtc.org6141e132013-04-09 10:09:10 +00001396int32_t
niklase@google.com470e71d2011-07-07 08:21:25 +00001397Channel::SetRecPayloadType(const CodecInst& codec)
1398{
1399 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId,_channelId),
1400 "Channel::SetRecPayloadType()");
1401
henrika@webrtc.org944cbeb2014-03-18 10:32:33 +00001402 if (channel_state_.Get().playing)
niklase@google.com470e71d2011-07-07 08:21:25 +00001403 {
1404 _engineStatisticsPtr->SetLastError(
1405 VE_ALREADY_PLAYING, kTraceError,
1406 "SetRecPayloadType() unable to set PT while playing");
1407 return -1;
1408 }
henrika@webrtc.org944cbeb2014-03-18 10:32:33 +00001409 if (channel_state_.Get().receiving)
niklase@google.com470e71d2011-07-07 08:21:25 +00001410 {
1411 _engineStatisticsPtr->SetLastError(
1412 VE_ALREADY_LISTENING, kTraceError,
1413 "SetRecPayloadType() unable to set PT while listening");
1414 return -1;
1415 }
1416
1417 if (codec.pltype == -1)
1418 {
1419 // De-register the selected codec (RTP/RTCP module and ACM)
1420
pbos@webrtc.org6141e132013-04-09 10:09:10 +00001421 int8_t pltype(-1);
niklase@google.com470e71d2011-07-07 08:21:25 +00001422 CodecInst rxCodec = codec;
1423
1424 // Get payload type for the given codec
wu@webrtc.org822fbd82013-08-15 23:38:54 +00001425 rtp_payload_registry_->ReceivePayloadType(
1426 rxCodec.plname,
1427 rxCodec.plfreq,
1428 rxCodec.channels,
1429 (rxCodec.rate < 0) ? 0 : rxCodec.rate,
1430 &pltype);
niklase@google.com470e71d2011-07-07 08:21:25 +00001431 rxCodec.pltype = pltype;
1432
wu@webrtc.org822fbd82013-08-15 23:38:54 +00001433 if (rtp_receiver_->DeRegisterReceivePayload(pltype) != 0)
niklase@google.com470e71d2011-07-07 08:21:25 +00001434 {
1435 _engineStatisticsPtr->SetLastError(
1436 VE_RTP_RTCP_MODULE_ERROR,
1437 kTraceError,
1438 "SetRecPayloadType() RTP/RTCP-module deregistration "
1439 "failed");
1440 return -1;
1441 }
andrew@webrtc.orgeb524d92013-09-23 23:02:24 +00001442 if (audio_coding_->UnregisterReceiveCodec(rxCodec.pltype) != 0)
niklase@google.com470e71d2011-07-07 08:21:25 +00001443 {
1444 _engineStatisticsPtr->SetLastError(
1445 VE_AUDIO_CODING_MODULE_ERROR, kTraceError,
1446 "SetRecPayloadType() ACM deregistration failed - 1");
1447 return -1;
1448 }
1449 return 0;
1450 }
1451
wu@webrtc.org822fbd82013-08-15 23:38:54 +00001452 if (rtp_receiver_->RegisterReceivePayload(
1453 codec.plname,
1454 codec.pltype,
1455 codec.plfreq,
1456 codec.channels,
1457 (codec.rate < 0) ? 0 : codec.rate) != 0)
niklase@google.com470e71d2011-07-07 08:21:25 +00001458 {
1459 // First attempt to register failed => de-register and try again
wu@webrtc.org822fbd82013-08-15 23:38:54 +00001460 rtp_receiver_->DeRegisterReceivePayload(codec.pltype);
1461 if (rtp_receiver_->RegisterReceivePayload(
1462 codec.plname,
1463 codec.pltype,
1464 codec.plfreq,
1465 codec.channels,
1466 (codec.rate < 0) ? 0 : codec.rate) != 0)
niklase@google.com470e71d2011-07-07 08:21:25 +00001467 {
1468 _engineStatisticsPtr->SetLastError(
1469 VE_RTP_RTCP_MODULE_ERROR, kTraceError,
1470 "SetRecPayloadType() RTP/RTCP-module registration failed");
1471 return -1;
1472 }
1473 }
andrew@webrtc.orgeb524d92013-09-23 23:02:24 +00001474 if (audio_coding_->RegisterReceiveCodec(codec) != 0)
niklase@google.com470e71d2011-07-07 08:21:25 +00001475 {
andrew@webrtc.orgeb524d92013-09-23 23:02:24 +00001476 audio_coding_->UnregisterReceiveCodec(codec.pltype);
1477 if (audio_coding_->RegisterReceiveCodec(codec) != 0)
niklase@google.com470e71d2011-07-07 08:21:25 +00001478 {
1479 _engineStatisticsPtr->SetLastError(
1480 VE_AUDIO_CODING_MODULE_ERROR, kTraceError,
1481 "SetRecPayloadType() ACM registration failed - 1");
1482 return -1;
1483 }
1484 }
1485 return 0;
1486}
1487
pbos@webrtc.org6141e132013-04-09 10:09:10 +00001488int32_t
niklase@google.com470e71d2011-07-07 08:21:25 +00001489Channel::GetRecPayloadType(CodecInst& codec)
1490{
1491 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId,_channelId),
1492 "Channel::GetRecPayloadType()");
pbos@webrtc.org6141e132013-04-09 10:09:10 +00001493 int8_t payloadType(-1);
wu@webrtc.org822fbd82013-08-15 23:38:54 +00001494 if (rtp_payload_registry_->ReceivePayloadType(
1495 codec.plname,
1496 codec.plfreq,
1497 codec.channels,
1498 (codec.rate < 0) ? 0 : codec.rate,
1499 &payloadType) != 0)
niklase@google.com470e71d2011-07-07 08:21:25 +00001500 {
1501 _engineStatisticsPtr->SetLastError(
henrika@webrtc.org37198002012-06-18 11:00:12 +00001502 VE_RTP_RTCP_MODULE_ERROR, kTraceWarning,
niklase@google.com470e71d2011-07-07 08:21:25 +00001503 "GetRecPayloadType() failed to retrieve RX payload type");
1504 return -1;
1505 }
1506 codec.pltype = payloadType;
1507 WEBRTC_TRACE(kTraceStateInfo, kTraceVoice, VoEId(_instanceId,_channelId),
pkasting@chromium.orgd3245462015-02-23 21:28:22 +00001508 "Channel::GetRecPayloadType() => pltype=%d", codec.pltype);
niklase@google.com470e71d2011-07-07 08:21:25 +00001509 return 0;
1510}
1511
pbos@webrtc.org6141e132013-04-09 10:09:10 +00001512int32_t
niklase@google.com470e71d2011-07-07 08:21:25 +00001513Channel::SetSendCNPayloadType(int type, PayloadFrequencies frequency)
1514{
1515 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId,_channelId),
1516 "Channel::SetSendCNPayloadType()");
1517
1518 CodecInst codec;
pbos@webrtc.org6141e132013-04-09 10:09:10 +00001519 int32_t samplingFreqHz(-1);
tina.legrand@webrtc.org45175852012-06-01 09:27:35 +00001520 const int kMono = 1;
niklase@google.com470e71d2011-07-07 08:21:25 +00001521 if (frequency == kFreq32000Hz)
1522 samplingFreqHz = 32000;
1523 else if (frequency == kFreq16000Hz)
1524 samplingFreqHz = 16000;
1525
andrew@webrtc.orgeb524d92013-09-23 23:02:24 +00001526 if (audio_coding_->Codec("CN", &codec, samplingFreqHz, kMono) == -1)
niklase@google.com470e71d2011-07-07 08:21:25 +00001527 {
1528 _engineStatisticsPtr->SetLastError(
1529 VE_AUDIO_CODING_MODULE_ERROR, kTraceError,
1530 "SetSendCNPayloadType() failed to retrieve default CN codec "
1531 "settings");
1532 return -1;
1533 }
1534
1535 // Modify the payload type (must be set to dynamic range)
1536 codec.pltype = type;
1537
andrew@webrtc.orgeb524d92013-09-23 23:02:24 +00001538 if (audio_coding_->RegisterSendCodec(codec) != 0)
niklase@google.com470e71d2011-07-07 08:21:25 +00001539 {
1540 _engineStatisticsPtr->SetLastError(
1541 VE_AUDIO_CODING_MODULE_ERROR, kTraceError,
1542 "SetSendCNPayloadType() failed to register CN to ACM");
1543 return -1;
1544 }
1545
pwestin@webrtc.org2853dde2012-05-11 11:08:54 +00001546 if (_rtpRtcpModule->RegisterSendPayload(codec) != 0)
niklase@google.com470e71d2011-07-07 08:21:25 +00001547 {
pwestin@webrtc.org2853dde2012-05-11 11:08:54 +00001548 _rtpRtcpModule->DeRegisterSendPayload(codec.pltype);
1549 if (_rtpRtcpModule->RegisterSendPayload(codec) != 0)
niklase@google.com470e71d2011-07-07 08:21:25 +00001550 {
1551 _engineStatisticsPtr->SetLastError(
1552 VE_RTP_RTCP_MODULE_ERROR, kTraceError,
1553 "SetSendCNPayloadType() failed to register CN to RTP/RTCP "
1554 "module");
1555 return -1;
1556 }
1557 }
1558 return 0;
1559}
1560
minyue@webrtc.orgadee8f92014-09-03 12:28:06 +00001561int Channel::SetOpusMaxPlaybackRate(int frequency_hz) {
minyue@webrtc.org6aac93b2014-08-12 08:13:33 +00001562 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId, _channelId),
minyue@webrtc.orgadee8f92014-09-03 12:28:06 +00001563 "Channel::SetOpusMaxPlaybackRate()");
minyue@webrtc.org6aac93b2014-08-12 08:13:33 +00001564
minyue@webrtc.orgadee8f92014-09-03 12:28:06 +00001565 if (audio_coding_->SetOpusMaxPlaybackRate(frequency_hz) != 0) {
minyue@webrtc.org6aac93b2014-08-12 08:13:33 +00001566 _engineStatisticsPtr->SetLastError(
1567 VE_AUDIO_CODING_MODULE_ERROR, kTraceError,
minyue@webrtc.orgadee8f92014-09-03 12:28:06 +00001568 "SetOpusMaxPlaybackRate() failed to set maximum playback rate");
minyue@webrtc.org6aac93b2014-08-12 08:13:33 +00001569 return -1;
1570 }
1571 return 0;
1572}
1573
minyue@webrtc.org9b2e1142015-03-13 09:38:07 +00001574int Channel::SetOpusDtx(bool enable_dtx) {
1575 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId, _channelId),
1576 "Channel::SetOpusDtx(%d)", enable_dtx);
1577 int ret = enable_dtx ? audio_coding_->EnableOpusDtx(true)
1578 : audio_coding_->DisableOpusDtx();
1579 if (ret != 0) {
1580 _engineStatisticsPtr->SetLastError(
1581 VE_AUDIO_CODING_MODULE_ERROR, kTraceError, "SetOpusDtx() failed");
1582 return -1;
1583 }
1584 return 0;
1585}
1586
pbos@webrtc.org6141e132013-04-09 10:09:10 +00001587int32_t Channel::RegisterExternalTransport(Transport& transport)
niklase@google.com470e71d2011-07-07 08:21:25 +00001588{
1589 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId, _channelId),
1590 "Channel::RegisterExternalTransport()");
1591
mflodman@webrtc.org9a065d12012-03-07 08:12:21 +00001592 CriticalSectionScoped cs(&_callbackCritSect);
niklase@google.com470e71d2011-07-07 08:21:25 +00001593
niklase@google.com470e71d2011-07-07 08:21:25 +00001594 if (_externalTransport)
1595 {
1596 _engineStatisticsPtr->SetLastError(VE_INVALID_OPERATION,
1597 kTraceError,
1598 "RegisterExternalTransport() external transport already enabled");
1599 return -1;
1600 }
1601 _externalTransport = true;
1602 _transportPtr = &transport;
1603 return 0;
1604}
1605
pbos@webrtc.org6141e132013-04-09 10:09:10 +00001606int32_t
niklase@google.com470e71d2011-07-07 08:21:25 +00001607Channel::DeRegisterExternalTransport()
1608{
1609 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId,_channelId),
1610 "Channel::DeRegisterExternalTransport()");
1611
mflodman@webrtc.org9a065d12012-03-07 08:12:21 +00001612 CriticalSectionScoped cs(&_callbackCritSect);
xians@webrtc.org83661f52011-11-25 10:58:15 +00001613
niklase@google.com470e71d2011-07-07 08:21:25 +00001614 if (!_transportPtr)
1615 {
1616 _engineStatisticsPtr->SetLastError(
1617 VE_INVALID_OPERATION, kTraceWarning,
1618 "DeRegisterExternalTransport() external transport already "
1619 "disabled");
1620 return 0;
1621 }
1622 _externalTransport = false;
niklase@google.com470e71d2011-07-07 08:21:25 +00001623 _transportPtr = NULL;
1624 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId,_channelId),
1625 "DeRegisterExternalTransport() all transport is disabled");
niklase@google.com470e71d2011-07-07 08:21:25 +00001626 return 0;
1627}
1628
pkasting@chromium.org4591fbd2014-11-20 22:28:14 +00001629int32_t Channel::ReceivedRTPPacket(const int8_t* data, size_t length,
solenberg@webrtc.orgb1f50102014-03-24 10:38:25 +00001630 const PacketTime& packet_time) {
pwestin@webrtc.org0c459572013-04-03 15:43:57 +00001631 WEBRTC_TRACE(kTraceStream, kTraceVoice, VoEId(_instanceId,_channelId),
1632 "Channel::ReceivedRTPPacket()");
1633
1634 // Store playout timestamp for the received RTP packet
pwestin@webrtc.org1de01352013-04-11 20:23:35 +00001635 UpdatePlayoutTimestamp(false);
pwestin@webrtc.org0c459572013-04-03 15:43:57 +00001636
1637 // Dump the RTP packet to a file (if RTP dump is enabled).
pbos@webrtc.org6141e132013-04-09 10:09:10 +00001638 if (_rtpDumpIn.DumpPacket((const uint8_t*)data,
1639 (uint16_t)length) == -1) {
pwestin@webrtc.org0c459572013-04-03 15:43:57 +00001640 WEBRTC_TRACE(kTraceWarning, kTraceVoice,
1641 VoEId(_instanceId,_channelId),
1642 "Channel::SendPacket() RTP dump to input file failed");
1643 }
stefan@webrtc.org7bb8f022013-09-06 13:40:11 +00001644 const uint8_t* received_packet = reinterpret_cast<const uint8_t*>(data);
stefan@webrtc.orga5cb98c2013-05-29 12:12:51 +00001645 RTPHeader header;
stefan@webrtc.org7bb8f022013-09-06 13:40:11 +00001646 if (!rtp_header_parser_->Parse(received_packet, length, &header)) {
1647 WEBRTC_TRACE(webrtc::kTraceDebug, webrtc::kTraceVoice, _channelId,
1648 "Incoming packet: invalid RTP header");
stefan@webrtc.orga5cb98c2013-05-29 12:12:51 +00001649 return -1;
1650 }
wu@webrtc.org822fbd82013-08-15 23:38:54 +00001651 header.payload_type_frequency =
1652 rtp_payload_registry_->GetPayloadTypeFrequency(header.payloadType);
stefan@webrtc.org7bb8f022013-09-06 13:40:11 +00001653 if (header.payload_type_frequency < 0)
wu@webrtc.org822fbd82013-08-15 23:38:54 +00001654 return -1;
stefan@webrtc.org48df3812013-11-08 15:18:52 +00001655 bool in_order = IsPacketInOrder(header);
stefan@webrtc.org7bb8f022013-09-06 13:40:11 +00001656 rtp_receive_statistics_->IncomingPacket(header, length,
stefan@webrtc.org48df3812013-11-08 15:18:52 +00001657 IsPacketRetransmitted(header, in_order));
stefan@webrtc.org7bb8f022013-09-06 13:40:11 +00001658 rtp_payload_registry_->SetIncomingPayloadType(header);
solenberg@webrtc.orgb1f50102014-03-24 10:38:25 +00001659
1660 // Forward any packets to ViE bandwidth estimator, if enabled.
1661 {
1662 CriticalSectionScoped cs(&_callbackCritSect);
1663 if (vie_network_) {
1664 int64_t arrival_time_ms;
1665 if (packet_time.timestamp != -1) {
1666 arrival_time_ms = (packet_time.timestamp + 500) / 1000;
1667 } else {
1668 arrival_time_ms = TickTime::MillisecondTimestamp();
1669 }
pkasting@chromium.org4591fbd2014-11-20 22:28:14 +00001670 size_t payload_length = length - header.headerLength;
solenberg@webrtc.orgb1f50102014-03-24 10:38:25 +00001671 vie_network_->ReceivedBWEPacket(video_channel_, arrival_time_ms,
1672 payload_length, header);
1673 }
1674 }
1675
stefan@webrtc.org48df3812013-11-08 15:18:52 +00001676 return ReceivePacket(received_packet, length, header, in_order) ? 0 : -1;
stefan@webrtc.org7bb8f022013-09-06 13:40:11 +00001677}
1678
1679bool Channel::ReceivePacket(const uint8_t* packet,
pkasting@chromium.org4591fbd2014-11-20 22:28:14 +00001680 size_t packet_length,
stefan@webrtc.org7bb8f022013-09-06 13:40:11 +00001681 const RTPHeader& header,
1682 bool in_order) {
minyue@webrtc.org456f0142015-01-23 11:58:42 +00001683 if (rtp_payload_registry_->IsRtx(header)) {
1684 return HandleRtxPacket(packet, packet_length, header);
wu@webrtc.org822fbd82013-08-15 23:38:54 +00001685 }
stefan@webrtc.org7bb8f022013-09-06 13:40:11 +00001686 const uint8_t* payload = packet + header.headerLength;
pkasting@chromium.org4591fbd2014-11-20 22:28:14 +00001687 assert(packet_length >= header.headerLength);
1688 size_t payload_length = packet_length - header.headerLength;
wu@webrtc.org822fbd82013-08-15 23:38:54 +00001689 PayloadUnion payload_specific;
1690 if (!rtp_payload_registry_->GetPayloadSpecifics(header.payloadType,
stefan@webrtc.org7bb8f022013-09-06 13:40:11 +00001691 &payload_specific)) {
1692 return false;
wu@webrtc.org822fbd82013-08-15 23:38:54 +00001693 }
stefan@webrtc.org7bb8f022013-09-06 13:40:11 +00001694 return rtp_receiver_->IncomingRtpPacket(header, payload, payload_length,
1695 payload_specific, in_order);
1696}
1697
minyue@webrtc.org456f0142015-01-23 11:58:42 +00001698bool Channel::HandleRtxPacket(const uint8_t* packet,
1699 size_t packet_length,
1700 const RTPHeader& header) {
stefan@webrtc.org7bb8f022013-09-06 13:40:11 +00001701 if (!rtp_payload_registry_->IsRtx(header))
1702 return false;
1703
1704 // Remove the RTX header and parse the original RTP header.
1705 if (packet_length < header.headerLength)
1706 return false;
1707 if (packet_length > kVoiceEngineMaxIpPacketSizeBytes)
1708 return false;
1709 if (restored_packet_in_use_) {
1710 WEBRTC_TRACE(webrtc::kTraceDebug, webrtc::kTraceVoice, _channelId,
1711 "Multiple RTX headers detected, dropping packet");
1712 return false;
pwestin@webrtc.org0c459572013-04-03 15:43:57 +00001713 }
stefan@webrtc.org7bb8f022013-09-06 13:40:11 +00001714 uint8_t* restored_packet_ptr = restored_packet_;
1715 if (!rtp_payload_registry_->RestoreOriginalPacket(
1716 &restored_packet_ptr, packet, &packet_length, rtp_receiver_->SSRC(),
1717 header)) {
1718 WEBRTC_TRACE(webrtc::kTraceDebug, webrtc::kTraceVoice, _channelId,
1719 "Incoming RTX packet: invalid RTP header");
1720 return false;
1721 }
1722 restored_packet_in_use_ = true;
1723 bool ret = OnRecoveredPacket(restored_packet_ptr, packet_length);
1724 restored_packet_in_use_ = false;
1725 return ret;
1726}
1727
1728bool Channel::IsPacketInOrder(const RTPHeader& header) const {
1729 StreamStatistician* statistician =
1730 rtp_receive_statistics_->GetStatistician(header.ssrc);
1731 if (!statistician)
1732 return false;
1733 return statistician->IsPacketInOrder(header.sequenceNumber);
niklase@google.com470e71d2011-07-07 08:21:25 +00001734}
1735
stefan@webrtc.org48df3812013-11-08 15:18:52 +00001736bool Channel::IsPacketRetransmitted(const RTPHeader& header,
1737 bool in_order) const {
stefan@webrtc.org7bb8f022013-09-06 13:40:11 +00001738 // Retransmissions are handled separately if RTX is enabled.
1739 if (rtp_payload_registry_->RtxEnabled())
1740 return false;
1741 StreamStatistician* statistician =
1742 rtp_receive_statistics_->GetStatistician(header.ssrc);
1743 if (!statistician)
1744 return false;
1745 // Check if this is a retransmission.
pkasting@chromium.org16825b12015-01-12 21:51:21 +00001746 int64_t min_rtt = 0;
stefan@webrtc.org7bb8f022013-09-06 13:40:11 +00001747 _rtpRtcpModule->RTT(rtp_receiver_->SSRC(), NULL, NULL, &min_rtt, NULL);
stefan@webrtc.org48df3812013-11-08 15:18:52 +00001748 return !in_order &&
stefan@webrtc.org7bb8f022013-09-06 13:40:11 +00001749 statistician->IsRetransmitOfOldPacket(header, min_rtt);
wu@webrtc.org822fbd82013-08-15 23:38:54 +00001750}
1751
pkasting@chromium.org4591fbd2014-11-20 22:28:14 +00001752int32_t Channel::ReceivedRTCPPacket(const int8_t* data, size_t length) {
pwestin@webrtc.org0c459572013-04-03 15:43:57 +00001753 WEBRTC_TRACE(kTraceStream, kTraceVoice, VoEId(_instanceId,_channelId),
1754 "Channel::ReceivedRTCPPacket()");
1755 // Store playout timestamp for the received RTCP packet
pwestin@webrtc.org1de01352013-04-11 20:23:35 +00001756 UpdatePlayoutTimestamp(true);
pwestin@webrtc.org0c459572013-04-03 15:43:57 +00001757
1758 // Dump the RTCP packet to a file (if RTP dump is enabled).
pkasting@chromium.org4591fbd2014-11-20 22:28:14 +00001759 if (_rtpDumpIn.DumpPacket((const uint8_t*)data, length) == -1) {
pwestin@webrtc.org0c459572013-04-03 15:43:57 +00001760 WEBRTC_TRACE(kTraceWarning, kTraceVoice,
1761 VoEId(_instanceId,_channelId),
1762 "Channel::SendPacket() RTCP dump to input file failed");
1763 }
1764
1765 // Deliver RTCP packet to RTP/RTCP module for parsing
pkasting@chromium.org4591fbd2014-11-20 22:28:14 +00001766 if (_rtpRtcpModule->IncomingRtcpPacket((const uint8_t*)data, length) == -1) {
pwestin@webrtc.org0c459572013-04-03 15:43:57 +00001767 _engineStatisticsPtr->SetLastError(
1768 VE_SOCKET_TRANSPORT_MODULE_ERROR, kTraceWarning,
1769 "Channel::IncomingRTPPacket() RTCP packet is invalid");
1770 }
wu@webrtc.org82c4b852014-05-20 22:55:01 +00001771
stefan@webrtc.org8e24d872014-09-02 18:58:24 +00001772 {
1773 CriticalSectionScoped lock(ts_stats_lock_.get());
pkasting@chromium.org16825b12015-01-12 21:51:21 +00001774 int64_t rtt = GetRTT();
minyue@webrtc.org2c0cdbc2014-10-09 10:52:43 +00001775 if (rtt == 0) {
1776 // Waiting for valid RTT.
1777 return 0;
1778 }
1779 uint32_t ntp_secs = 0;
1780 uint32_t ntp_frac = 0;
1781 uint32_t rtp_timestamp = 0;
1782 if (0 != _rtpRtcpModule->RemoteNTP(&ntp_secs, &ntp_frac, NULL, NULL,
1783 &rtp_timestamp)) {
1784 // Waiting for RTCP.
1785 return 0;
1786 }
1787 ntp_estimator_.UpdateRtcpTimestamp(rtt, ntp_secs, ntp_frac, rtp_timestamp);
stefan@webrtc.org8e24d872014-09-02 18:58:24 +00001788 }
pwestin@webrtc.org0c459572013-04-03 15:43:57 +00001789 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +00001790}
1791
niklase@google.com470e71d2011-07-07 08:21:25 +00001792int Channel::StartPlayingFileLocally(const char* fileName,
pbos@webrtc.org92135212013-05-14 08:31:39 +00001793 bool loop,
1794 FileFormats format,
1795 int startPosition,
1796 float volumeScaling,
1797 int stopPosition,
niklase@google.com470e71d2011-07-07 08:21:25 +00001798 const CodecInst* codecInst)
1799{
1800 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId,_channelId),
1801 "Channel::StartPlayingFileLocally(fileNameUTF8[]=%s, loop=%d,"
1802 " format=%d, volumeScaling=%5.3f, startPosition=%d, "
1803 "stopPosition=%d)", fileName, loop, format, volumeScaling,
1804 startPosition, stopPosition);
1805
henrika@webrtc.org944cbeb2014-03-18 10:32:33 +00001806 if (channel_state_.Get().output_file_playing)
niklase@google.com470e71d2011-07-07 08:21:25 +00001807 {
1808 _engineStatisticsPtr->SetLastError(
1809 VE_ALREADY_PLAYING, kTraceError,
1810 "StartPlayingFileLocally() is already playing");
1811 return -1;
1812 }
1813
niklase@google.com470e71d2011-07-07 08:21:25 +00001814 {
mflodman@webrtc.org9a065d12012-03-07 08:12:21 +00001815 CriticalSectionScoped cs(&_fileCritSect);
henrike@webrtc.orgb37c6282011-10-31 23:53:04 +00001816
1817 if (_outputFilePlayerPtr)
1818 {
1819 _outputFilePlayerPtr->RegisterModuleFileCallback(NULL);
1820 FilePlayer::DestroyFilePlayer(_outputFilePlayerPtr);
1821 _outputFilePlayerPtr = NULL;
1822 }
1823
1824 _outputFilePlayerPtr = FilePlayer::CreateFilePlayer(
1825 _outputFilePlayerId, (const FileFormats)format);
1826
1827 if (_outputFilePlayerPtr == NULL)
1828 {
1829 _engineStatisticsPtr->SetLastError(
1830 VE_INVALID_ARGUMENT, kTraceError,
henrike@webrtc.org31d30702011-11-18 19:59:32 +00001831 "StartPlayingFileLocally() filePlayer format is not correct");
henrike@webrtc.orgb37c6282011-10-31 23:53:04 +00001832 return -1;
1833 }
1834
pbos@webrtc.org6141e132013-04-09 10:09:10 +00001835 const uint32_t notificationTime(0);
henrike@webrtc.orgb37c6282011-10-31 23:53:04 +00001836
1837 if (_outputFilePlayerPtr->StartPlayingFile(
1838 fileName,
1839 loop,
1840 startPosition,
1841 volumeScaling,
1842 notificationTime,
1843 stopPosition,
1844 (const CodecInst*)codecInst) != 0)
1845 {
1846 _engineStatisticsPtr->SetLastError(
1847 VE_BAD_FILE, kTraceError,
1848 "StartPlayingFile() failed to start file playout");
1849 _outputFilePlayerPtr->StopPlayingFile();
1850 FilePlayer::DestroyFilePlayer(_outputFilePlayerPtr);
1851 _outputFilePlayerPtr = NULL;
1852 return -1;
1853 }
1854 _outputFilePlayerPtr->RegisterModuleFileCallback(this);
henrika@webrtc.org944cbeb2014-03-18 10:32:33 +00001855 channel_state_.SetOutputFilePlaying(true);
niklase@google.com470e71d2011-07-07 08:21:25 +00001856 }
braveyao@webrtc.orgab129902012-06-04 03:26:39 +00001857
1858 if (RegisterFilePlayingToMixer() != 0)
henrike@webrtc.org066f9e52011-10-28 23:15:47 +00001859 return -1;
niklase@google.com470e71d2011-07-07 08:21:25 +00001860
1861 return 0;
1862}
1863
1864int Channel::StartPlayingFileLocally(InStream* stream,
pbos@webrtc.org92135212013-05-14 08:31:39 +00001865 FileFormats format,
1866 int startPosition,
1867 float volumeScaling,
1868 int stopPosition,
niklase@google.com470e71d2011-07-07 08:21:25 +00001869 const CodecInst* codecInst)
1870{
1871 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId,_channelId),
1872 "Channel::StartPlayingFileLocally(format=%d,"
1873 " volumeScaling=%5.3f, startPosition=%d, stopPosition=%d)",
1874 format, volumeScaling, startPosition, stopPosition);
1875
1876 if(stream == NULL)
1877 {
1878 _engineStatisticsPtr->SetLastError(
1879 VE_BAD_FILE, kTraceError,
1880 "StartPlayingFileLocally() NULL as input stream");
1881 return -1;
1882 }
1883
1884
henrika@webrtc.org944cbeb2014-03-18 10:32:33 +00001885 if (channel_state_.Get().output_file_playing)
niklase@google.com470e71d2011-07-07 08:21:25 +00001886 {
1887 _engineStatisticsPtr->SetLastError(
1888 VE_ALREADY_PLAYING, kTraceError,
1889 "StartPlayingFileLocally() is already playing");
1890 return -1;
1891 }
1892
niklase@google.com470e71d2011-07-07 08:21:25 +00001893 {
mflodman@webrtc.org9a065d12012-03-07 08:12:21 +00001894 CriticalSectionScoped cs(&_fileCritSect);
henrike@webrtc.orgb37c6282011-10-31 23:53:04 +00001895
1896 // Destroy the old instance
1897 if (_outputFilePlayerPtr)
1898 {
1899 _outputFilePlayerPtr->RegisterModuleFileCallback(NULL);
1900 FilePlayer::DestroyFilePlayer(_outputFilePlayerPtr);
1901 _outputFilePlayerPtr = NULL;
1902 }
1903
1904 // Create the instance
1905 _outputFilePlayerPtr = FilePlayer::CreateFilePlayer(
1906 _outputFilePlayerId,
1907 (const FileFormats)format);
1908
1909 if (_outputFilePlayerPtr == NULL)
1910 {
1911 _engineStatisticsPtr->SetLastError(
1912 VE_INVALID_ARGUMENT, kTraceError,
1913 "StartPlayingFileLocally() filePlayer format isnot correct");
1914 return -1;
1915 }
1916
pbos@webrtc.org6141e132013-04-09 10:09:10 +00001917 const uint32_t notificationTime(0);
henrike@webrtc.orgb37c6282011-10-31 23:53:04 +00001918
1919 if (_outputFilePlayerPtr->StartPlayingFile(*stream, startPosition,
1920 volumeScaling,
1921 notificationTime,
1922 stopPosition, codecInst) != 0)
1923 {
1924 _engineStatisticsPtr->SetLastError(VE_BAD_FILE, kTraceError,
1925 "StartPlayingFile() failed to "
1926 "start file playout");
1927 _outputFilePlayerPtr->StopPlayingFile();
1928 FilePlayer::DestroyFilePlayer(_outputFilePlayerPtr);
1929 _outputFilePlayerPtr = NULL;
1930 return -1;
1931 }
1932 _outputFilePlayerPtr->RegisterModuleFileCallback(this);
henrika@webrtc.org944cbeb2014-03-18 10:32:33 +00001933 channel_state_.SetOutputFilePlaying(true);
niklase@google.com470e71d2011-07-07 08:21:25 +00001934 }
braveyao@webrtc.orgab129902012-06-04 03:26:39 +00001935
1936 if (RegisterFilePlayingToMixer() != 0)
henrike@webrtc.org066f9e52011-10-28 23:15:47 +00001937 return -1;
niklase@google.com470e71d2011-07-07 08:21:25 +00001938
niklase@google.com470e71d2011-07-07 08:21:25 +00001939 return 0;
1940}
1941
1942int Channel::StopPlayingFileLocally()
1943{
1944 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId,_channelId),
1945 "Channel::StopPlayingFileLocally()");
1946
henrika@webrtc.org944cbeb2014-03-18 10:32:33 +00001947 if (!channel_state_.Get().output_file_playing)
niklase@google.com470e71d2011-07-07 08:21:25 +00001948 {
1949 _engineStatisticsPtr->SetLastError(
1950 VE_INVALID_OPERATION, kTraceWarning,
1951 "StopPlayingFileLocally() isnot playing");
1952 return 0;
1953 }
1954
niklase@google.com470e71d2011-07-07 08:21:25 +00001955 {
mflodman@webrtc.org9a065d12012-03-07 08:12:21 +00001956 CriticalSectionScoped cs(&_fileCritSect);
henrike@webrtc.orgb37c6282011-10-31 23:53:04 +00001957
1958 if (_outputFilePlayerPtr->StopPlayingFile() != 0)
1959 {
1960 _engineStatisticsPtr->SetLastError(
1961 VE_STOP_RECORDING_FAILED, kTraceError,
1962 "StopPlayingFile() could not stop playing");
1963 return -1;
1964 }
1965 _outputFilePlayerPtr->RegisterModuleFileCallback(NULL);
1966 FilePlayer::DestroyFilePlayer(_outputFilePlayerPtr);
1967 _outputFilePlayerPtr = NULL;
henrika@webrtc.org944cbeb2014-03-18 10:32:33 +00001968 channel_state_.SetOutputFilePlaying(false);
niklase@google.com470e71d2011-07-07 08:21:25 +00001969 }
henrike@webrtc.orgb37c6282011-10-31 23:53:04 +00001970 // _fileCritSect cannot be taken while calling
1971 // SetAnonymousMixibilityStatus. Refer to comments in
1972 // StartPlayingFileLocally(const char* ...) for more details.
henrike@webrtc.org066f9e52011-10-28 23:15:47 +00001973 if (_outputMixerPtr->SetAnonymousMixabilityStatus(*this, false) != 0)
1974 {
1975 _engineStatisticsPtr->SetLastError(
1976 VE_AUDIO_CONF_MIX_MODULE_ERROR, kTraceError,
henrike@webrtc.orgb37c6282011-10-31 23:53:04 +00001977 "StopPlayingFile() failed to stop participant from playing as"
1978 "file in the mixer");
henrike@webrtc.org066f9e52011-10-28 23:15:47 +00001979 return -1;
1980 }
niklase@google.com470e71d2011-07-07 08:21:25 +00001981
1982 return 0;
1983}
1984
1985int Channel::IsPlayingFileLocally() const
1986{
1987 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId,_channelId),
1988 "Channel::IsPlayingFileLocally()");
1989
henrika@webrtc.org944cbeb2014-03-18 10:32:33 +00001990 return channel_state_.Get().output_file_playing;
niklase@google.com470e71d2011-07-07 08:21:25 +00001991}
1992
braveyao@webrtc.orgab129902012-06-04 03:26:39 +00001993int Channel::RegisterFilePlayingToMixer()
1994{
1995 // Return success for not registering for file playing to mixer if:
1996 // 1. playing file before playout is started on that channel.
1997 // 2. starting playout without file playing on that channel.
henrika@webrtc.org944cbeb2014-03-18 10:32:33 +00001998 if (!channel_state_.Get().playing ||
1999 !channel_state_.Get().output_file_playing)
braveyao@webrtc.orgab129902012-06-04 03:26:39 +00002000 {
2001 return 0;
2002 }
2003
2004 // |_fileCritSect| cannot be taken while calling
2005 // SetAnonymousMixabilityStatus() since as soon as the participant is added
2006 // frames can be pulled by the mixer. Since the frames are generated from
2007 // the file, _fileCritSect will be taken. This would result in a deadlock.
2008 if (_outputMixerPtr->SetAnonymousMixabilityStatus(*this, true) != 0)
2009 {
henrika@webrtc.org944cbeb2014-03-18 10:32:33 +00002010 channel_state_.SetOutputFilePlaying(false);
braveyao@webrtc.orgab129902012-06-04 03:26:39 +00002011 CriticalSectionScoped cs(&_fileCritSect);
braveyao@webrtc.orgab129902012-06-04 03:26:39 +00002012 _engineStatisticsPtr->SetLastError(
2013 VE_AUDIO_CONF_MIX_MODULE_ERROR, kTraceError,
2014 "StartPlayingFile() failed to add participant as file to mixer");
2015 _outputFilePlayerPtr->StopPlayingFile();
2016 FilePlayer::DestroyFilePlayer(_outputFilePlayerPtr);
2017 _outputFilePlayerPtr = NULL;
2018 return -1;
2019 }
2020
2021 return 0;
2022}
2023
niklase@google.com470e71d2011-07-07 08:21:25 +00002024int Channel::StartPlayingFileAsMicrophone(const char* fileName,
pbos@webrtc.org92135212013-05-14 08:31:39 +00002025 bool loop,
2026 FileFormats format,
2027 int startPosition,
2028 float volumeScaling,
2029 int stopPosition,
niklase@google.com470e71d2011-07-07 08:21:25 +00002030 const CodecInst* codecInst)
2031{
2032 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId,_channelId),
2033 "Channel::StartPlayingFileAsMicrophone(fileNameUTF8[]=%s, "
2034 "loop=%d, format=%d, volumeScaling=%5.3f, startPosition=%d, "
2035 "stopPosition=%d)", fileName, loop, format, volumeScaling,
2036 startPosition, stopPosition);
2037
henrika@webrtc.org944cbeb2014-03-18 10:32:33 +00002038 CriticalSectionScoped cs(&_fileCritSect);
2039
2040 if (channel_state_.Get().input_file_playing)
niklase@google.com470e71d2011-07-07 08:21:25 +00002041 {
2042 _engineStatisticsPtr->SetLastError(
2043 VE_ALREADY_PLAYING, kTraceWarning,
2044 "StartPlayingFileAsMicrophone() filePlayer is playing");
2045 return 0;
2046 }
2047
niklase@google.com470e71d2011-07-07 08:21:25 +00002048 // Destroy the old instance
2049 if (_inputFilePlayerPtr)
2050 {
2051 _inputFilePlayerPtr->RegisterModuleFileCallback(NULL);
2052 FilePlayer::DestroyFilePlayer(_inputFilePlayerPtr);
2053 _inputFilePlayerPtr = NULL;
2054 }
2055
2056 // Create the instance
2057 _inputFilePlayerPtr = FilePlayer::CreateFilePlayer(
2058 _inputFilePlayerId, (const FileFormats)format);
2059
2060 if (_inputFilePlayerPtr == NULL)
2061 {
2062 _engineStatisticsPtr->SetLastError(
2063 VE_INVALID_ARGUMENT, kTraceError,
2064 "StartPlayingFileAsMicrophone() filePlayer format isnot correct");
2065 return -1;
2066 }
2067
pbos@webrtc.org6141e132013-04-09 10:09:10 +00002068 const uint32_t notificationTime(0);
niklase@google.com470e71d2011-07-07 08:21:25 +00002069
2070 if (_inputFilePlayerPtr->StartPlayingFile(
2071 fileName,
2072 loop,
2073 startPosition,
2074 volumeScaling,
2075 notificationTime,
2076 stopPosition,
2077 (const CodecInst*)codecInst) != 0)
2078 {
2079 _engineStatisticsPtr->SetLastError(
2080 VE_BAD_FILE, kTraceError,
2081 "StartPlayingFile() failed to start file playout");
2082 _inputFilePlayerPtr->StopPlayingFile();
2083 FilePlayer::DestroyFilePlayer(_inputFilePlayerPtr);
2084 _inputFilePlayerPtr = NULL;
2085 return -1;
2086 }
2087 _inputFilePlayerPtr->RegisterModuleFileCallback(this);
henrika@webrtc.org944cbeb2014-03-18 10:32:33 +00002088 channel_state_.SetInputFilePlaying(true);
niklase@google.com470e71d2011-07-07 08:21:25 +00002089
2090 return 0;
2091}
2092
2093int Channel::StartPlayingFileAsMicrophone(InStream* stream,
pbos@webrtc.org92135212013-05-14 08:31:39 +00002094 FileFormats format,
2095 int startPosition,
2096 float volumeScaling,
2097 int stopPosition,
niklase@google.com470e71d2011-07-07 08:21:25 +00002098 const CodecInst* codecInst)
2099{
2100 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId,_channelId),
2101 "Channel::StartPlayingFileAsMicrophone(format=%d, "
2102 "volumeScaling=%5.3f, startPosition=%d, stopPosition=%d)",
2103 format, volumeScaling, startPosition, stopPosition);
2104
2105 if(stream == NULL)
2106 {
2107 _engineStatisticsPtr->SetLastError(
2108 VE_BAD_FILE, kTraceError,
2109 "StartPlayingFileAsMicrophone NULL as input stream");
2110 return -1;
2111 }
2112
henrika@webrtc.org944cbeb2014-03-18 10:32:33 +00002113 CriticalSectionScoped cs(&_fileCritSect);
2114
2115 if (channel_state_.Get().input_file_playing)
niklase@google.com470e71d2011-07-07 08:21:25 +00002116 {
2117 _engineStatisticsPtr->SetLastError(
2118 VE_ALREADY_PLAYING, kTraceWarning,
2119 "StartPlayingFileAsMicrophone() is playing");
2120 return 0;
2121 }
2122
niklase@google.com470e71d2011-07-07 08:21:25 +00002123 // Destroy the old instance
2124 if (_inputFilePlayerPtr)
2125 {
2126 _inputFilePlayerPtr->RegisterModuleFileCallback(NULL);
2127 FilePlayer::DestroyFilePlayer(_inputFilePlayerPtr);
2128 _inputFilePlayerPtr = NULL;
2129 }
2130
2131 // Create the instance
2132 _inputFilePlayerPtr = FilePlayer::CreateFilePlayer(
2133 _inputFilePlayerId, (const FileFormats)format);
2134
2135 if (_inputFilePlayerPtr == NULL)
2136 {
2137 _engineStatisticsPtr->SetLastError(
2138 VE_INVALID_ARGUMENT, kTraceError,
2139 "StartPlayingInputFile() filePlayer format isnot correct");
2140 return -1;
2141 }
2142
pbos@webrtc.org6141e132013-04-09 10:09:10 +00002143 const uint32_t notificationTime(0);
niklase@google.com470e71d2011-07-07 08:21:25 +00002144
2145 if (_inputFilePlayerPtr->StartPlayingFile(*stream, startPosition,
2146 volumeScaling, notificationTime,
2147 stopPosition, codecInst) != 0)
2148 {
2149 _engineStatisticsPtr->SetLastError(VE_BAD_FILE, kTraceError,
2150 "StartPlayingFile() failed to start "
2151 "file playout");
2152 _inputFilePlayerPtr->StopPlayingFile();
2153 FilePlayer::DestroyFilePlayer(_inputFilePlayerPtr);
2154 _inputFilePlayerPtr = NULL;
2155 return -1;
2156 }
andrew@webrtc.orgae1a58b2013-01-22 04:44:30 +00002157
niklase@google.com470e71d2011-07-07 08:21:25 +00002158 _inputFilePlayerPtr->RegisterModuleFileCallback(this);
henrika@webrtc.org944cbeb2014-03-18 10:32:33 +00002159 channel_state_.SetInputFilePlaying(true);
niklase@google.com470e71d2011-07-07 08:21:25 +00002160
2161 return 0;
2162}
2163
2164int Channel::StopPlayingFileAsMicrophone()
2165{
2166 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId,_channelId),
2167 "Channel::StopPlayingFileAsMicrophone()");
2168
henrika@webrtc.org944cbeb2014-03-18 10:32:33 +00002169 CriticalSectionScoped cs(&_fileCritSect);
2170
2171 if (!channel_state_.Get().input_file_playing)
niklase@google.com470e71d2011-07-07 08:21:25 +00002172 {
2173 _engineStatisticsPtr->SetLastError(
2174 VE_INVALID_OPERATION, kTraceWarning,
2175 "StopPlayingFileAsMicrophone() isnot playing");
2176 return 0;
2177 }
2178
niklase@google.com470e71d2011-07-07 08:21:25 +00002179 if (_inputFilePlayerPtr->StopPlayingFile() != 0)
2180 {
2181 _engineStatisticsPtr->SetLastError(
2182 VE_STOP_RECORDING_FAILED, kTraceError,
2183 "StopPlayingFile() could not stop playing");
2184 return -1;
2185 }
2186 _inputFilePlayerPtr->RegisterModuleFileCallback(NULL);
2187 FilePlayer::DestroyFilePlayer(_inputFilePlayerPtr);
2188 _inputFilePlayerPtr = NULL;
henrika@webrtc.org944cbeb2014-03-18 10:32:33 +00002189 channel_state_.SetInputFilePlaying(false);
niklase@google.com470e71d2011-07-07 08:21:25 +00002190
2191 return 0;
2192}
2193
2194int Channel::IsPlayingFileAsMicrophone() const
2195{
2196 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId,_channelId),
2197 "Channel::IsPlayingFileAsMicrophone()");
henrika@webrtc.org944cbeb2014-03-18 10:32:33 +00002198 return channel_state_.Get().input_file_playing;
niklase@google.com470e71d2011-07-07 08:21:25 +00002199}
2200
leozwang@webrtc.org813e4b02012-03-01 18:34:25 +00002201int Channel::StartRecordingPlayout(const char* fileName,
niklase@google.com470e71d2011-07-07 08:21:25 +00002202 const CodecInst* codecInst)
2203{
2204 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId,_channelId),
2205 "Channel::StartRecordingPlayout(fileName=%s)", fileName);
2206
2207 if (_outputFileRecording)
2208 {
2209 WEBRTC_TRACE(kTraceWarning, kTraceVoice, VoEId(_instanceId,-1),
2210 "StartRecordingPlayout() is already recording");
2211 return 0;
2212 }
2213
2214 FileFormats format;
pbos@webrtc.org6141e132013-04-09 10:09:10 +00002215 const uint32_t notificationTime(0); // Not supported in VoE
niklase@google.com470e71d2011-07-07 08:21:25 +00002216 CodecInst dummyCodec={100,"L16",16000,320,1,320000};
2217
niklas.enbom@webrtc.org40197d72012-03-26 08:45:47 +00002218 if ((codecInst != NULL) &&
2219 ((codecInst->channels < 1) || (codecInst->channels > 2)))
niklase@google.com470e71d2011-07-07 08:21:25 +00002220 {
2221 _engineStatisticsPtr->SetLastError(
2222 VE_BAD_ARGUMENT, kTraceError,
2223 "StartRecordingPlayout() invalid compression");
2224 return(-1);
2225 }
2226 if(codecInst == NULL)
2227 {
2228 format = kFileFormatPcm16kHzFile;
2229 codecInst=&dummyCodec;
2230 }
2231 else if((STR_CASE_CMP(codecInst->plname,"L16") == 0) ||
2232 (STR_CASE_CMP(codecInst->plname,"PCMU") == 0) ||
2233 (STR_CASE_CMP(codecInst->plname,"PCMA") == 0))
2234 {
2235 format = kFileFormatWavFile;
2236 }
2237 else
2238 {
2239 format = kFileFormatCompressedFile;
2240 }
2241
mflodman@webrtc.org9a065d12012-03-07 08:12:21 +00002242 CriticalSectionScoped cs(&_fileCritSect);
niklase@google.com470e71d2011-07-07 08:21:25 +00002243
2244 // Destroy the old instance
2245 if (_outputFileRecorderPtr)
2246 {
2247 _outputFileRecorderPtr->RegisterModuleFileCallback(NULL);
2248 FileRecorder::DestroyFileRecorder(_outputFileRecorderPtr);
2249 _outputFileRecorderPtr = NULL;
2250 }
2251
2252 _outputFileRecorderPtr = FileRecorder::CreateFileRecorder(
2253 _outputFileRecorderId, (const FileFormats)format);
2254 if (_outputFileRecorderPtr == NULL)
2255 {
2256 _engineStatisticsPtr->SetLastError(
2257 VE_INVALID_ARGUMENT, kTraceError,
2258 "StartRecordingPlayout() fileRecorder format isnot correct");
2259 return -1;
2260 }
2261
2262 if (_outputFileRecorderPtr->StartRecordingAudioFile(
2263 fileName, (const CodecInst&)*codecInst, notificationTime) != 0)
2264 {
2265 _engineStatisticsPtr->SetLastError(
2266 VE_BAD_FILE, kTraceError,
2267 "StartRecordingAudioFile() failed to start file recording");
2268 _outputFileRecorderPtr->StopRecording();
2269 FileRecorder::DestroyFileRecorder(_outputFileRecorderPtr);
2270 _outputFileRecorderPtr = NULL;
2271 return -1;
2272 }
2273 _outputFileRecorderPtr->RegisterModuleFileCallback(this);
2274 _outputFileRecording = true;
2275
2276 return 0;
2277}
2278
2279int Channel::StartRecordingPlayout(OutStream* stream,
2280 const CodecInst* codecInst)
2281{
2282 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId,_channelId),
2283 "Channel::StartRecordingPlayout()");
2284
2285 if (_outputFileRecording)
2286 {
2287 WEBRTC_TRACE(kTraceWarning, kTraceVoice, VoEId(_instanceId,-1),
2288 "StartRecordingPlayout() is already recording");
2289 return 0;
2290 }
2291
2292 FileFormats format;
pbos@webrtc.org6141e132013-04-09 10:09:10 +00002293 const uint32_t notificationTime(0); // Not supported in VoE
niklase@google.com470e71d2011-07-07 08:21:25 +00002294 CodecInst dummyCodec={100,"L16",16000,320,1,320000};
2295
2296 if (codecInst != NULL && codecInst->channels != 1)
2297 {
2298 _engineStatisticsPtr->SetLastError(
2299 VE_BAD_ARGUMENT, kTraceError,
2300 "StartRecordingPlayout() invalid compression");
2301 return(-1);
2302 }
2303 if(codecInst == NULL)
2304 {
2305 format = kFileFormatPcm16kHzFile;
2306 codecInst=&dummyCodec;
2307 }
2308 else if((STR_CASE_CMP(codecInst->plname,"L16") == 0) ||
2309 (STR_CASE_CMP(codecInst->plname,"PCMU") == 0) ||
2310 (STR_CASE_CMP(codecInst->plname,"PCMA") == 0))
2311 {
2312 format = kFileFormatWavFile;
2313 }
2314 else
2315 {
2316 format = kFileFormatCompressedFile;
2317 }
2318
mflodman@webrtc.org9a065d12012-03-07 08:12:21 +00002319 CriticalSectionScoped cs(&_fileCritSect);
niklase@google.com470e71d2011-07-07 08:21:25 +00002320
2321 // Destroy the old instance
2322 if (_outputFileRecorderPtr)
2323 {
2324 _outputFileRecorderPtr->RegisterModuleFileCallback(NULL);
2325 FileRecorder::DestroyFileRecorder(_outputFileRecorderPtr);
2326 _outputFileRecorderPtr = NULL;
2327 }
2328
2329 _outputFileRecorderPtr = FileRecorder::CreateFileRecorder(
2330 _outputFileRecorderId, (const FileFormats)format);
2331 if (_outputFileRecorderPtr == NULL)
2332 {
2333 _engineStatisticsPtr->SetLastError(
2334 VE_INVALID_ARGUMENT, kTraceError,
2335 "StartRecordingPlayout() fileRecorder format isnot correct");
2336 return -1;
2337 }
2338
2339 if (_outputFileRecorderPtr->StartRecordingAudioFile(*stream, *codecInst,
2340 notificationTime) != 0)
2341 {
2342 _engineStatisticsPtr->SetLastError(VE_BAD_FILE, kTraceError,
2343 "StartRecordingPlayout() failed to "
2344 "start file recording");
2345 _outputFileRecorderPtr->StopRecording();
2346 FileRecorder::DestroyFileRecorder(_outputFileRecorderPtr);
2347 _outputFileRecorderPtr = NULL;
2348 return -1;
2349 }
andrew@webrtc.orgae1a58b2013-01-22 04:44:30 +00002350
niklase@google.com470e71d2011-07-07 08:21:25 +00002351 _outputFileRecorderPtr->RegisterModuleFileCallback(this);
2352 _outputFileRecording = true;
2353
2354 return 0;
2355}
2356
2357int Channel::StopRecordingPlayout()
2358{
2359 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId,-1),
2360 "Channel::StopRecordingPlayout()");
2361
2362 if (!_outputFileRecording)
2363 {
2364 WEBRTC_TRACE(kTraceError, kTraceVoice, VoEId(_instanceId,-1),
2365 "StopRecordingPlayout() isnot recording");
2366 return -1;
2367 }
2368
2369
mflodman@webrtc.org9a065d12012-03-07 08:12:21 +00002370 CriticalSectionScoped cs(&_fileCritSect);
niklase@google.com470e71d2011-07-07 08:21:25 +00002371
2372 if (_outputFileRecorderPtr->StopRecording() != 0)
2373 {
2374 _engineStatisticsPtr->SetLastError(
2375 VE_STOP_RECORDING_FAILED, kTraceError,
2376 "StopRecording() could not stop recording");
2377 return(-1);
2378 }
2379 _outputFileRecorderPtr->RegisterModuleFileCallback(NULL);
2380 FileRecorder::DestroyFileRecorder(_outputFileRecorderPtr);
2381 _outputFileRecorderPtr = NULL;
2382 _outputFileRecording = false;
2383
2384 return 0;
2385}
2386
2387void
2388Channel::SetMixWithMicStatus(bool mix)
2389{
henrika@webrtc.org944cbeb2014-03-18 10:32:33 +00002390 CriticalSectionScoped cs(&_fileCritSect);
niklase@google.com470e71d2011-07-07 08:21:25 +00002391 _mixFileWithMicrophone=mix;
2392}
2393
2394int
pbos@webrtc.org6141e132013-04-09 10:09:10 +00002395Channel::GetSpeechOutputLevel(uint32_t& level) const
niklase@google.com470e71d2011-07-07 08:21:25 +00002396{
pbos@webrtc.org6141e132013-04-09 10:09:10 +00002397 int8_t currentLevel = _outputAudioLevel.Level();
2398 level = static_cast<int32_t> (currentLevel);
niklase@google.com470e71d2011-07-07 08:21:25 +00002399 WEBRTC_TRACE(kTraceStateInfo, kTraceVoice,
2400 VoEId(_instanceId,_channelId),
2401 "GetSpeechOutputLevel() => level=%u", level);
2402 return 0;
2403}
2404
2405int
pbos@webrtc.org6141e132013-04-09 10:09:10 +00002406Channel::GetSpeechOutputLevelFullRange(uint32_t& level) const
niklase@google.com470e71d2011-07-07 08:21:25 +00002407{
pbos@webrtc.org6141e132013-04-09 10:09:10 +00002408 int16_t currentLevel = _outputAudioLevel.LevelFullRange();
2409 level = static_cast<int32_t> (currentLevel);
niklase@google.com470e71d2011-07-07 08:21:25 +00002410 WEBRTC_TRACE(kTraceStateInfo, kTraceVoice,
2411 VoEId(_instanceId,_channelId),
2412 "GetSpeechOutputLevelFullRange() => level=%u", level);
2413 return 0;
2414}
2415
2416int
2417Channel::SetMute(bool enable)
2418{
wu@webrtc.org63420662013-10-17 18:28:55 +00002419 CriticalSectionScoped cs(&volume_settings_critsect_);
niklase@google.com470e71d2011-07-07 08:21:25 +00002420 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId,_channelId),
2421 "Channel::SetMute(enable=%d)", enable);
2422 _mute = enable;
2423 return 0;
2424}
2425
2426bool
2427Channel::Mute() 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 return _mute;
2431}
2432
2433int
2434Channel::SetOutputVolumePan(float left, float right)
2435{
wu@webrtc.org63420662013-10-17 18:28:55 +00002436 CriticalSectionScoped cs(&volume_settings_critsect_);
niklase@google.com470e71d2011-07-07 08:21:25 +00002437 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId,_channelId),
2438 "Channel::SetOutputVolumePan()");
2439 _panLeft = left;
2440 _panRight = right;
2441 return 0;
2442}
2443
2444int
2445Channel::GetOutputVolumePan(float& left, float& right) const
2446{
wu@webrtc.org63420662013-10-17 18:28:55 +00002447 CriticalSectionScoped cs(&volume_settings_critsect_);
niklase@google.com470e71d2011-07-07 08:21:25 +00002448 left = _panLeft;
2449 right = _panRight;
2450 WEBRTC_TRACE(kTraceStateInfo, kTraceVoice,
2451 VoEId(_instanceId,_channelId),
2452 "GetOutputVolumePan() => left=%3.2f, right=%3.2f", left, right);
2453 return 0;
2454}
2455
2456int
2457Channel::SetChannelOutputVolumeScaling(float scaling)
2458{
wu@webrtc.org63420662013-10-17 18:28:55 +00002459 CriticalSectionScoped cs(&volume_settings_critsect_);
niklase@google.com470e71d2011-07-07 08:21:25 +00002460 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId,_channelId),
2461 "Channel::SetChannelOutputVolumeScaling()");
2462 _outputGain = scaling;
2463 return 0;
2464}
2465
2466int
2467Channel::GetChannelOutputVolumeScaling(float& scaling) const
2468{
wu@webrtc.org63420662013-10-17 18:28:55 +00002469 CriticalSectionScoped cs(&volume_settings_critsect_);
niklase@google.com470e71d2011-07-07 08:21:25 +00002470 scaling = _outputGain;
2471 WEBRTC_TRACE(kTraceStateInfo, kTraceVoice,
2472 VoEId(_instanceId,_channelId),
2473 "GetChannelOutputVolumeScaling() => scaling=%3.2f", scaling);
2474 return 0;
2475}
2476
niklase@google.com470e71d2011-07-07 08:21:25 +00002477int Channel::SendTelephoneEventOutband(unsigned char eventCode,
wu@webrtc.org822fbd82013-08-15 23:38:54 +00002478 int lengthMs, int attenuationDb,
2479 bool playDtmfEvent)
niklase@google.com470e71d2011-07-07 08:21:25 +00002480{
2481 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId, _channelId),
2482 "Channel::SendTelephoneEventOutband(..., playDtmfEvent=%d)",
2483 playDtmfEvent);
2484
2485 _playOutbandDtmfEvent = playDtmfEvent;
2486
pwestin@webrtc.org2853dde2012-05-11 11:08:54 +00002487 if (_rtpRtcpModule->SendTelephoneEventOutband(eventCode, lengthMs,
niklase@google.com470e71d2011-07-07 08:21:25 +00002488 attenuationDb) != 0)
2489 {
2490 _engineStatisticsPtr->SetLastError(
2491 VE_SEND_DTMF_FAILED,
2492 kTraceWarning,
2493 "SendTelephoneEventOutband() failed to send event");
2494 return -1;
2495 }
2496 return 0;
2497}
2498
2499int Channel::SendTelephoneEventInband(unsigned char eventCode,
2500 int lengthMs,
2501 int attenuationDb,
2502 bool playDtmfEvent)
2503{
2504 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId, _channelId),
2505 "Channel::SendTelephoneEventInband(..., playDtmfEvent=%d)",
2506 playDtmfEvent);
2507
2508 _playInbandDtmfEvent = playDtmfEvent;
2509 _inbandDtmfQueue.AddDtmf(eventCode, lengthMs, attenuationDb);
2510
2511 return 0;
2512}
2513
2514int
niklase@google.com470e71d2011-07-07 08:21:25 +00002515Channel::SetSendTelephoneEventPayloadType(unsigned char type)
2516{
2517 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId,_channelId),
2518 "Channel::SetSendTelephoneEventPayloadType()");
andrew@webrtc.orgf81f9f82011-08-19 22:56:22 +00002519 if (type > 127)
niklase@google.com470e71d2011-07-07 08:21:25 +00002520 {
2521 _engineStatisticsPtr->SetLastError(
2522 VE_INVALID_ARGUMENT, kTraceError,
2523 "SetSendTelephoneEventPayloadType() invalid type");
2524 return -1;
2525 }
pbos@webrtc.org5b10d8f2013-07-11 15:50:07 +00002526 CodecInst codec = {};
pwestin@webrtc.org1da1ce02011-10-13 15:19:55 +00002527 codec.plfreq = 8000;
2528 codec.pltype = type;
2529 memcpy(codec.plname, "telephone-event", 16);
pwestin@webrtc.org2853dde2012-05-11 11:08:54 +00002530 if (_rtpRtcpModule->RegisterSendPayload(codec) != 0)
niklase@google.com470e71d2011-07-07 08:21:25 +00002531 {
henrika@webrtc.org4392d5f2013-04-17 07:34:25 +00002532 _rtpRtcpModule->DeRegisterSendPayload(codec.pltype);
2533 if (_rtpRtcpModule->RegisterSendPayload(codec) != 0) {
2534 _engineStatisticsPtr->SetLastError(
2535 VE_RTP_RTCP_MODULE_ERROR, kTraceError,
2536 "SetSendTelephoneEventPayloadType() failed to register send"
2537 "payload type");
2538 return -1;
2539 }
niklase@google.com470e71d2011-07-07 08:21:25 +00002540 }
2541 _sendTelephoneEventPayloadType = type;
2542 return 0;
2543}
2544
2545int
2546Channel::GetSendTelephoneEventPayloadType(unsigned char& type)
2547{
2548 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId,_channelId),
2549 "Channel::GetSendTelephoneEventPayloadType()");
2550 type = _sendTelephoneEventPayloadType;
2551 WEBRTC_TRACE(kTraceStateInfo, kTraceVoice,
2552 VoEId(_instanceId,_channelId),
2553 "GetSendTelephoneEventPayloadType() => type=%u", type);
2554 return 0;
2555}
2556
niklase@google.com470e71d2011-07-07 08:21:25 +00002557int
2558Channel::UpdateRxVadDetection(AudioFrame& audioFrame)
2559{
2560 WEBRTC_TRACE(kTraceStream, kTraceVoice, VoEId(_instanceId,_channelId),
2561 "Channel::UpdateRxVadDetection()");
2562
2563 int vadDecision = 1;
2564
andrew@webrtc.org63a50982012-05-02 23:56:37 +00002565 vadDecision = (audioFrame.vad_activity_ == AudioFrame::kVadActive)? 1 : 0;
niklase@google.com470e71d2011-07-07 08:21:25 +00002566
2567 if ((vadDecision != _oldVadDecision) && _rxVadObserverPtr)
2568 {
2569 OnRxVadDetected(vadDecision);
2570 _oldVadDecision = vadDecision;
2571 }
2572
2573 WEBRTC_TRACE(kTraceStream, kTraceVoice, VoEId(_instanceId,_channelId),
2574 "Channel::UpdateRxVadDetection() => vadDecision=%d",
2575 vadDecision);
2576 return 0;
2577}
2578
2579int
2580Channel::RegisterRxVadObserver(VoERxVadCallback &observer)
2581{
2582 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId,_channelId),
2583 "Channel::RegisterRxVadObserver()");
mflodman@webrtc.org9a065d12012-03-07 08:12:21 +00002584 CriticalSectionScoped cs(&_callbackCritSect);
niklase@google.com470e71d2011-07-07 08:21:25 +00002585
2586 if (_rxVadObserverPtr)
2587 {
2588 _engineStatisticsPtr->SetLastError(
2589 VE_INVALID_OPERATION, kTraceError,
2590 "RegisterRxVadObserver() observer already enabled");
2591 return -1;
2592 }
niklase@google.com470e71d2011-07-07 08:21:25 +00002593 _rxVadObserverPtr = &observer;
2594 _RxVadDetection = true;
2595 return 0;
2596}
2597
2598int
2599Channel::DeRegisterRxVadObserver()
2600{
2601 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId,_channelId),
2602 "Channel::DeRegisterRxVadObserver()");
mflodman@webrtc.org9a065d12012-03-07 08:12:21 +00002603 CriticalSectionScoped cs(&_callbackCritSect);
niklase@google.com470e71d2011-07-07 08:21:25 +00002604
2605 if (!_rxVadObserverPtr)
2606 {
2607 _engineStatisticsPtr->SetLastError(
2608 VE_INVALID_OPERATION, kTraceWarning,
2609 "DeRegisterRxVadObserver() observer already disabled");
2610 return 0;
2611 }
2612 _rxVadObserverPtr = NULL;
2613 _RxVadDetection = false;
2614 return 0;
2615}
2616
2617int
2618Channel::VoiceActivityIndicator(int &activity)
2619{
2620 activity = _sendFrameType;
2621
2622 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId,_channelId),
andrew@webrtc.org6c264cc2013-10-04 17:54:09 +00002623 "Channel::VoiceActivityIndicator(indicator=%d)", activity);
niklase@google.com470e71d2011-07-07 08:21:25 +00002624 return 0;
2625}
2626
2627#ifdef WEBRTC_VOICE_ENGINE_AGC
2628
2629int
pbos@webrtc.org92135212013-05-14 08:31:39 +00002630Channel::SetRxAgcStatus(bool enable, AgcModes mode)
niklase@google.com470e71d2011-07-07 08:21:25 +00002631{
2632 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId,_channelId),
2633 "Channel::SetRxAgcStatus(enable=%d, mode=%d)",
2634 (int)enable, (int)mode);
2635
andrew@webrtc.org6c264cc2013-10-04 17:54:09 +00002636 GainControl::Mode agcMode = kDefaultRxAgcMode;
niklase@google.com470e71d2011-07-07 08:21:25 +00002637 switch (mode)
2638 {
2639 case kAgcDefault:
niklase@google.com470e71d2011-07-07 08:21:25 +00002640 break;
2641 case kAgcUnchanged:
andrew@webrtc.orgf3930e92013-09-18 22:37:32 +00002642 agcMode = rx_audioproc_->gain_control()->mode();
niklase@google.com470e71d2011-07-07 08:21:25 +00002643 break;
2644 case kAgcFixedDigital:
2645 agcMode = GainControl::kFixedDigital;
2646 break;
2647 case kAgcAdaptiveDigital:
2648 agcMode =GainControl::kAdaptiveDigital;
2649 break;
2650 default:
2651 _engineStatisticsPtr->SetLastError(
2652 VE_INVALID_ARGUMENT, kTraceError,
2653 "SetRxAgcStatus() invalid Agc mode");
2654 return -1;
2655 }
2656
andrew@webrtc.orgf3930e92013-09-18 22:37:32 +00002657 if (rx_audioproc_->gain_control()->set_mode(agcMode) != 0)
niklase@google.com470e71d2011-07-07 08:21:25 +00002658 {
2659 _engineStatisticsPtr->SetLastError(
2660 VE_APM_ERROR, kTraceError,
2661 "SetRxAgcStatus() failed to set Agc mode");
2662 return -1;
2663 }
andrew@webrtc.orgf3930e92013-09-18 22:37:32 +00002664 if (rx_audioproc_->gain_control()->Enable(enable) != 0)
niklase@google.com470e71d2011-07-07 08:21:25 +00002665 {
2666 _engineStatisticsPtr->SetLastError(
2667 VE_APM_ERROR, kTraceError,
2668 "SetRxAgcStatus() failed to set Agc state");
2669 return -1;
2670 }
2671
2672 _rxAgcIsEnabled = enable;
henrika@webrtc.org944cbeb2014-03-18 10:32:33 +00002673 channel_state_.SetRxApmIsEnabled(_rxAgcIsEnabled || _rxNsIsEnabled);
niklase@google.com470e71d2011-07-07 08:21:25 +00002674
2675 return 0;
2676}
2677
2678int
2679Channel::GetRxAgcStatus(bool& enabled, AgcModes& mode)
2680{
2681 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId,_channelId),
2682 "Channel::GetRxAgcStatus(enable=?, mode=?)");
2683
andrew@webrtc.orgf3930e92013-09-18 22:37:32 +00002684 bool enable = rx_audioproc_->gain_control()->is_enabled();
niklase@google.com470e71d2011-07-07 08:21:25 +00002685 GainControl::Mode agcMode =
andrew@webrtc.orgf3930e92013-09-18 22:37:32 +00002686 rx_audioproc_->gain_control()->mode();
niklase@google.com470e71d2011-07-07 08:21:25 +00002687
2688 enabled = enable;
2689
2690 switch (agcMode)
2691 {
2692 case GainControl::kFixedDigital:
2693 mode = kAgcFixedDigital;
2694 break;
2695 case GainControl::kAdaptiveDigital:
2696 mode = kAgcAdaptiveDigital;
2697 break;
2698 default:
2699 _engineStatisticsPtr->SetLastError(
2700 VE_APM_ERROR, kTraceError,
2701 "GetRxAgcStatus() invalid Agc mode");
2702 return -1;
2703 }
2704
2705 return 0;
2706}
2707
2708int
pbos@webrtc.org92135212013-05-14 08:31:39 +00002709Channel::SetRxAgcConfig(AgcConfig config)
niklase@google.com470e71d2011-07-07 08:21:25 +00002710{
2711 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId,_channelId),
2712 "Channel::SetRxAgcConfig()");
2713
andrew@webrtc.orgf3930e92013-09-18 22:37:32 +00002714 if (rx_audioproc_->gain_control()->set_target_level_dbfs(
niklase@google.com470e71d2011-07-07 08:21:25 +00002715 config.targetLeveldBOv) != 0)
2716 {
2717 _engineStatisticsPtr->SetLastError(
2718 VE_APM_ERROR, kTraceError,
2719 "SetRxAgcConfig() failed to set target peak |level|"
2720 "(or envelope) of the Agc");
2721 return -1;
2722 }
andrew@webrtc.orgf3930e92013-09-18 22:37:32 +00002723 if (rx_audioproc_->gain_control()->set_compression_gain_db(
niklase@google.com470e71d2011-07-07 08:21:25 +00002724 config.digitalCompressionGaindB) != 0)
2725 {
2726 _engineStatisticsPtr->SetLastError(
2727 VE_APM_ERROR, kTraceError,
2728 "SetRxAgcConfig() failed to set the range in |gain| the"
2729 " digital compression stage may apply");
2730 return -1;
2731 }
andrew@webrtc.orgf3930e92013-09-18 22:37:32 +00002732 if (rx_audioproc_->gain_control()->enable_limiter(
niklase@google.com470e71d2011-07-07 08:21:25 +00002733 config.limiterEnable) != 0)
2734 {
2735 _engineStatisticsPtr->SetLastError(
2736 VE_APM_ERROR, kTraceError,
2737 "SetRxAgcConfig() failed to set hard limiter to the signal");
2738 return -1;
2739 }
2740
2741 return 0;
2742}
2743
2744int
2745Channel::GetRxAgcConfig(AgcConfig& config)
2746{
2747 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId,_channelId),
2748 "Channel::GetRxAgcConfig(config=%?)");
2749
2750 config.targetLeveldBOv =
andrew@webrtc.orgf3930e92013-09-18 22:37:32 +00002751 rx_audioproc_->gain_control()->target_level_dbfs();
niklase@google.com470e71d2011-07-07 08:21:25 +00002752 config.digitalCompressionGaindB =
andrew@webrtc.orgf3930e92013-09-18 22:37:32 +00002753 rx_audioproc_->gain_control()->compression_gain_db();
niklase@google.com470e71d2011-07-07 08:21:25 +00002754 config.limiterEnable =
andrew@webrtc.orgf3930e92013-09-18 22:37:32 +00002755 rx_audioproc_->gain_control()->is_limiter_enabled();
niklase@google.com470e71d2011-07-07 08:21:25 +00002756
2757 WEBRTC_TRACE(kTraceStateInfo, kTraceVoice,
2758 VoEId(_instanceId,_channelId), "GetRxAgcConfig() => "
2759 "targetLeveldBOv=%u, digitalCompressionGaindB=%u,"
2760 " limiterEnable=%d",
2761 config.targetLeveldBOv,
2762 config.digitalCompressionGaindB,
2763 config.limiterEnable);
2764
2765 return 0;
2766}
2767
2768#endif // #ifdef WEBRTC_VOICE_ENGINE_AGC
2769
2770#ifdef WEBRTC_VOICE_ENGINE_NR
2771
2772int
pbos@webrtc.org92135212013-05-14 08:31:39 +00002773Channel::SetRxNsStatus(bool enable, NsModes mode)
niklase@google.com470e71d2011-07-07 08:21:25 +00002774{
2775 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId,_channelId),
2776 "Channel::SetRxNsStatus(enable=%d, mode=%d)",
2777 (int)enable, (int)mode);
2778
andrew@webrtc.org6c264cc2013-10-04 17:54:09 +00002779 NoiseSuppression::Level nsLevel = kDefaultNsMode;
niklase@google.com470e71d2011-07-07 08:21:25 +00002780 switch (mode)
2781 {
2782
2783 case kNsDefault:
niklase@google.com470e71d2011-07-07 08:21:25 +00002784 break;
2785 case kNsUnchanged:
andrew@webrtc.orgf3930e92013-09-18 22:37:32 +00002786 nsLevel = rx_audioproc_->noise_suppression()->level();
niklase@google.com470e71d2011-07-07 08:21:25 +00002787 break;
2788 case kNsConference:
2789 nsLevel = NoiseSuppression::kHigh;
2790 break;
2791 case kNsLowSuppression:
2792 nsLevel = NoiseSuppression::kLow;
2793 break;
2794 case kNsModerateSuppression:
2795 nsLevel = NoiseSuppression::kModerate;
2796 break;
2797 case kNsHighSuppression:
2798 nsLevel = NoiseSuppression::kHigh;
2799 break;
2800 case kNsVeryHighSuppression:
2801 nsLevel = NoiseSuppression::kVeryHigh;
2802 break;
niklase@google.com470e71d2011-07-07 08:21:25 +00002803 }
2804
andrew@webrtc.orgf3930e92013-09-18 22:37:32 +00002805 if (rx_audioproc_->noise_suppression()->set_level(nsLevel)
niklase@google.com470e71d2011-07-07 08:21:25 +00002806 != 0)
2807 {
2808 _engineStatisticsPtr->SetLastError(
2809 VE_APM_ERROR, kTraceError,
andrew@webrtc.org6c264cc2013-10-04 17:54:09 +00002810 "SetRxNsStatus() failed to set NS level");
niklase@google.com470e71d2011-07-07 08:21:25 +00002811 return -1;
2812 }
andrew@webrtc.orgf3930e92013-09-18 22:37:32 +00002813 if (rx_audioproc_->noise_suppression()->Enable(enable) != 0)
niklase@google.com470e71d2011-07-07 08:21:25 +00002814 {
2815 _engineStatisticsPtr->SetLastError(
2816 VE_APM_ERROR, kTraceError,
andrew@webrtc.org6c264cc2013-10-04 17:54:09 +00002817 "SetRxNsStatus() failed to set NS state");
niklase@google.com470e71d2011-07-07 08:21:25 +00002818 return -1;
2819 }
2820
2821 _rxNsIsEnabled = enable;
henrika@webrtc.org944cbeb2014-03-18 10:32:33 +00002822 channel_state_.SetRxApmIsEnabled(_rxAgcIsEnabled || _rxNsIsEnabled);
niklase@google.com470e71d2011-07-07 08:21:25 +00002823
2824 return 0;
2825}
2826
2827int
2828Channel::GetRxNsStatus(bool& enabled, NsModes& mode)
2829{
2830 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId,_channelId),
2831 "Channel::GetRxNsStatus(enable=?, mode=?)");
2832
2833 bool enable =
andrew@webrtc.orgf3930e92013-09-18 22:37:32 +00002834 rx_audioproc_->noise_suppression()->is_enabled();
niklase@google.com470e71d2011-07-07 08:21:25 +00002835 NoiseSuppression::Level ncLevel =
andrew@webrtc.orgf3930e92013-09-18 22:37:32 +00002836 rx_audioproc_->noise_suppression()->level();
niklase@google.com470e71d2011-07-07 08:21:25 +00002837
2838 enabled = enable;
2839
2840 switch (ncLevel)
2841 {
2842 case NoiseSuppression::kLow:
2843 mode = kNsLowSuppression;
2844 break;
2845 case NoiseSuppression::kModerate:
2846 mode = kNsModerateSuppression;
2847 break;
2848 case NoiseSuppression::kHigh:
2849 mode = kNsHighSuppression;
2850 break;
2851 case NoiseSuppression::kVeryHigh:
2852 mode = kNsVeryHighSuppression;
2853 break;
niklase@google.com470e71d2011-07-07 08:21:25 +00002854 }
2855
2856 WEBRTC_TRACE(kTraceStateInfo, kTraceVoice,
2857 VoEId(_instanceId,_channelId),
2858 "GetRxNsStatus() => enabled=%d, mode=%d", enabled, mode);
2859 return 0;
2860}
2861
2862#endif // #ifdef WEBRTC_VOICE_ENGINE_NR
2863
2864int
niklase@google.com470e71d2011-07-07 08:21:25 +00002865Channel::SetLocalSSRC(unsigned int ssrc)
2866{
2867 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId, _channelId),
2868 "Channel::SetLocalSSRC()");
henrika@webrtc.org944cbeb2014-03-18 10:32:33 +00002869 if (channel_state_.Get().sending)
niklase@google.com470e71d2011-07-07 08:21:25 +00002870 {
2871 _engineStatisticsPtr->SetLastError(
2872 VE_ALREADY_SENDING, kTraceError,
2873 "SetLocalSSRC() already sending");
2874 return -1;
2875 }
stefan@webrtc.orgef927552014-06-05 08:25:29 +00002876 _rtpRtcpModule->SetSSRC(ssrc);
niklase@google.com470e71d2011-07-07 08:21:25 +00002877 return 0;
2878}
2879
2880int
2881Channel::GetLocalSSRC(unsigned int& ssrc)
2882{
pwestin@webrtc.org2853dde2012-05-11 11:08:54 +00002883 ssrc = _rtpRtcpModule->SSRC();
niklase@google.com470e71d2011-07-07 08:21:25 +00002884 WEBRTC_TRACE(kTraceStateInfo, kTraceVoice,
2885 VoEId(_instanceId,_channelId),
2886 "GetLocalSSRC() => ssrc=%lu", ssrc);
2887 return 0;
2888}
2889
2890int
2891Channel::GetRemoteSSRC(unsigned int& ssrc)
2892{
wu@webrtc.org822fbd82013-08-15 23:38:54 +00002893 ssrc = rtp_receiver_->SSRC();
niklase@google.com470e71d2011-07-07 08:21:25 +00002894 WEBRTC_TRACE(kTraceStateInfo, kTraceVoice,
2895 VoEId(_instanceId,_channelId),
2896 "GetRemoteSSRC() => ssrc=%lu", ssrc);
2897 return 0;
2898}
2899
wu@webrtc.orgebdb0e32014-03-06 23:49:08 +00002900int Channel::SetSendAudioLevelIndicationStatus(bool enable, unsigned char id) {
andrew@webrtc.orgf3930e92013-09-18 22:37:32 +00002901 _includeAudioLevelIndication = enable;
wu@webrtc.orgebdb0e32014-03-06 23:49:08 +00002902 return SetSendRtpHeaderExtension(enable, kRtpExtensionAudioLevel, id);
niklase@google.com470e71d2011-07-07 08:21:25 +00002903}
andrew@webrtc.orgf3930e92013-09-18 22:37:32 +00002904
wu@webrtc.org93fd25c2014-04-24 20:33:08 +00002905int Channel::SetReceiveAudioLevelIndicationStatus(bool enable,
2906 unsigned char id) {
2907 rtp_header_parser_->DeregisterRtpHeaderExtension(
2908 kRtpExtensionAudioLevel);
2909 if (enable && !rtp_header_parser_->RegisterRtpHeaderExtension(
2910 kRtpExtensionAudioLevel, id)) {
2911 return -1;
2912 }
2913 return 0;
2914}
2915
wu@webrtc.orgebdb0e32014-03-06 23:49:08 +00002916int Channel::SetSendAbsoluteSenderTimeStatus(bool enable, unsigned char id) {
2917 return SetSendRtpHeaderExtension(enable, kRtpExtensionAbsoluteSendTime, id);
2918}
2919
2920int Channel::SetReceiveAbsoluteSenderTimeStatus(bool enable, unsigned char id) {
2921 rtp_header_parser_->DeregisterRtpHeaderExtension(
2922 kRtpExtensionAbsoluteSendTime);
solenberg@webrtc.orgb1f50102014-03-24 10:38:25 +00002923 if (enable && !rtp_header_parser_->RegisterRtpHeaderExtension(
2924 kRtpExtensionAbsoluteSendTime, id)) {
2925 return -1;
wu@webrtc.orgebdb0e32014-03-06 23:49:08 +00002926 }
2927 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +00002928}
2929
pbos@webrtc.orgd16e8392014-12-19 13:49:55 +00002930void Channel::SetRTCPStatus(bool enable) {
2931 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId, _channelId),
2932 "Channel::SetRTCPStatus()");
2933 _rtpRtcpModule->SetRTCPStatus(enable ? kRtcpCompound : kRtcpOff);
niklase@google.com470e71d2011-07-07 08:21:25 +00002934}
2935
2936int
2937Channel::GetRTCPStatus(bool& enabled)
2938{
pwestin@webrtc.org2853dde2012-05-11 11:08:54 +00002939 RTCPMethod method = _rtpRtcpModule->RTCP();
niklase@google.com470e71d2011-07-07 08:21:25 +00002940 enabled = (method != kRtcpOff);
2941 WEBRTC_TRACE(kTraceStateInfo, kTraceVoice,
2942 VoEId(_instanceId,_channelId),
2943 "GetRTCPStatus() => enabled=%d", enabled);
2944 return 0;
2945}
2946
2947int
2948Channel::SetRTCP_CNAME(const char cName[256])
2949{
2950 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId, _channelId),
2951 "Channel::SetRTCP_CNAME()");
pwestin@webrtc.org2853dde2012-05-11 11:08:54 +00002952 if (_rtpRtcpModule->SetCNAME(cName) != 0)
niklase@google.com470e71d2011-07-07 08:21:25 +00002953 {
2954 _engineStatisticsPtr->SetLastError(
2955 VE_RTP_RTCP_MODULE_ERROR, kTraceError,
2956 "SetRTCP_CNAME() failed to set RTCP CNAME");
2957 return -1;
2958 }
2959 return 0;
2960}
2961
2962int
niklase@google.com470e71d2011-07-07 08:21:25 +00002963Channel::GetRemoteRTCP_CNAME(char cName[256])
2964{
2965 if (cName == NULL)
2966 {
2967 _engineStatisticsPtr->SetLastError(
2968 VE_INVALID_ARGUMENT, kTraceError,
2969 "GetRemoteRTCP_CNAME() invalid CNAME input buffer");
2970 return -1;
2971 }
leozwang@webrtc.org813e4b02012-03-01 18:34:25 +00002972 char cname[RTCP_CNAME_SIZE];
wu@webrtc.org822fbd82013-08-15 23:38:54 +00002973 const uint32_t remoteSSRC = rtp_receiver_->SSRC();
pwestin@webrtc.org2853dde2012-05-11 11:08:54 +00002974 if (_rtpRtcpModule->RemoteCNAME(remoteSSRC, cname) != 0)
niklase@google.com470e71d2011-07-07 08:21:25 +00002975 {
2976 _engineStatisticsPtr->SetLastError(
2977 VE_CANNOT_RETRIEVE_CNAME, kTraceError,
2978 "GetRemoteRTCP_CNAME() failed to retrieve remote RTCP CNAME");
2979 return -1;
2980 }
2981 strcpy(cName, cname);
2982 WEBRTC_TRACE(kTraceStateInfo, kTraceVoice,
2983 VoEId(_instanceId, _channelId),
2984 "GetRemoteRTCP_CNAME() => cName=%s", cName);
2985 return 0;
2986}
2987
2988int
2989Channel::GetRemoteRTCPData(
2990 unsigned int& NTPHigh,
2991 unsigned int& NTPLow,
2992 unsigned int& timestamp,
2993 unsigned int& playoutTimestamp,
2994 unsigned int* jitter,
2995 unsigned short* fractionLost)
2996{
2997 // --- Information from sender info in received Sender Reports
2998
2999 RTCPSenderInfo senderInfo;
pwestin@webrtc.org2853dde2012-05-11 11:08:54 +00003000 if (_rtpRtcpModule->RemoteRTCPStat(&senderInfo) != 0)
niklase@google.com470e71d2011-07-07 08:21:25 +00003001 {
3002 _engineStatisticsPtr->SetLastError(
3003 VE_RTP_RTCP_MODULE_ERROR, kTraceError,
wu@webrtc.orgfcd12b32011-09-15 20:49:50 +00003004 "GetRemoteRTCPData() failed to retrieve sender info for remote "
niklase@google.com470e71d2011-07-07 08:21:25 +00003005 "side");
3006 return -1;
3007 }
3008
3009 // We only utilize 12 out of 20 bytes in the sender info (ignores packet
3010 // and octet count)
3011 NTPHigh = senderInfo.NTPseconds;
3012 NTPLow = senderInfo.NTPfraction;
3013 timestamp = senderInfo.RTPtimeStamp;
3014
3015 WEBRTC_TRACE(kTraceStateInfo, kTraceVoice,
3016 VoEId(_instanceId, _channelId),
3017 "GetRemoteRTCPData() => NTPHigh=%lu, NTPLow=%lu, "
3018 "timestamp=%lu",
3019 NTPHigh, NTPLow, timestamp);
3020
3021 // --- Locally derived information
3022
3023 // This value is updated on each incoming RTCP packet (0 when no packet
3024 // has been received)
pwestin@webrtc.org1de01352013-04-11 20:23:35 +00003025 playoutTimestamp = playout_timestamp_rtcp_;
niklase@google.com470e71d2011-07-07 08:21:25 +00003026
3027 WEBRTC_TRACE(kTraceStateInfo, kTraceVoice,
3028 VoEId(_instanceId, _channelId),
3029 "GetRemoteRTCPData() => playoutTimestamp=%lu",
pwestin@webrtc.org1de01352013-04-11 20:23:35 +00003030 playout_timestamp_rtcp_);
niklase@google.com470e71d2011-07-07 08:21:25 +00003031
3032 if (NULL != jitter || NULL != fractionLost)
3033 {
perkj@webrtc.orgce5990c2012-01-11 13:00:08 +00003034 // Get all RTCP receiver report blocks that have been received on this
3035 // channel. If we receive RTP packets from a remote source we know the
3036 // remote SSRC and use the report block from him.
3037 // Otherwise use the first report block.
3038 std::vector<RTCPReportBlock> remote_stats;
pwestin@webrtc.org2853dde2012-05-11 11:08:54 +00003039 if (_rtpRtcpModule->RemoteRTCPStat(&remote_stats) != 0 ||
perkj@webrtc.orgce5990c2012-01-11 13:00:08 +00003040 remote_stats.empty()) {
3041 WEBRTC_TRACE(kTraceWarning, kTraceVoice,
3042 VoEId(_instanceId, _channelId),
3043 "GetRemoteRTCPData() failed to measure statistics due"
3044 " to lack of received RTP and/or RTCP packets");
3045 return -1;
niklase@google.com470e71d2011-07-07 08:21:25 +00003046 }
perkj@webrtc.orgce5990c2012-01-11 13:00:08 +00003047
wu@webrtc.org822fbd82013-08-15 23:38:54 +00003048 uint32_t remoteSSRC = rtp_receiver_->SSRC();
perkj@webrtc.orgce5990c2012-01-11 13:00:08 +00003049 std::vector<RTCPReportBlock>::const_iterator it = remote_stats.begin();
3050 for (; it != remote_stats.end(); ++it) {
3051 if (it->remoteSSRC == remoteSSRC)
3052 break;
niklase@google.com470e71d2011-07-07 08:21:25 +00003053 }
perkj@webrtc.orgce5990c2012-01-11 13:00:08 +00003054
3055 if (it == remote_stats.end()) {
3056 // If we have not received any RTCP packets from this SSRC it probably
3057 // means that we have not received any RTP packets.
3058 // Use the first received report block instead.
3059 it = remote_stats.begin();
3060 remoteSSRC = it->remoteSSRC;
niklase@google.com470e71d2011-07-07 08:21:25 +00003061 }
perkj@webrtc.orgce5990c2012-01-11 13:00:08 +00003062
xians@webrtc.org79af7342012-01-31 12:22:14 +00003063 if (jitter) {
3064 *jitter = it->jitter;
3065 WEBRTC_TRACE(kTraceStateInfo, kTraceVoice,
3066 VoEId(_instanceId, _channelId),
3067 "GetRemoteRTCPData() => jitter = %lu", *jitter);
3068 }
perkj@webrtc.orgce5990c2012-01-11 13:00:08 +00003069
xians@webrtc.org79af7342012-01-31 12:22:14 +00003070 if (fractionLost) {
3071 *fractionLost = it->fractionLost;
3072 WEBRTC_TRACE(kTraceStateInfo, kTraceVoice,
3073 VoEId(_instanceId, _channelId),
3074 "GetRemoteRTCPData() => fractionLost = %lu",
3075 *fractionLost);
3076 }
niklase@google.com470e71d2011-07-07 08:21:25 +00003077 }
3078 return 0;
3079}
3080
3081int
pbos@webrtc.org92135212013-05-14 08:31:39 +00003082Channel::SendApplicationDefinedRTCPPacket(unsigned char subType,
niklase@google.com470e71d2011-07-07 08:21:25 +00003083 unsigned int name,
3084 const char* data,
3085 unsigned short dataLengthInBytes)
3086{
3087 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId, _channelId),
3088 "Channel::SendApplicationDefinedRTCPPacket()");
henrika@webrtc.org944cbeb2014-03-18 10:32:33 +00003089 if (!channel_state_.Get().sending)
niklase@google.com470e71d2011-07-07 08:21:25 +00003090 {
3091 _engineStatisticsPtr->SetLastError(
3092 VE_NOT_SENDING, kTraceError,
3093 "SendApplicationDefinedRTCPPacket() not sending");
3094 return -1;
3095 }
3096 if (NULL == data)
3097 {
3098 _engineStatisticsPtr->SetLastError(
3099 VE_INVALID_ARGUMENT, kTraceError,
3100 "SendApplicationDefinedRTCPPacket() invalid data value");
3101 return -1;
3102 }
3103 if (dataLengthInBytes % 4 != 0)
3104 {
3105 _engineStatisticsPtr->SetLastError(
3106 VE_INVALID_ARGUMENT, kTraceError,
3107 "SendApplicationDefinedRTCPPacket() invalid length value");
3108 return -1;
3109 }
pwestin@webrtc.org2853dde2012-05-11 11:08:54 +00003110 RTCPMethod status = _rtpRtcpModule->RTCP();
niklase@google.com470e71d2011-07-07 08:21:25 +00003111 if (status == kRtcpOff)
3112 {
3113 _engineStatisticsPtr->SetLastError(
3114 VE_RTCP_ERROR, kTraceError,
3115 "SendApplicationDefinedRTCPPacket() RTCP is disabled");
3116 return -1;
3117 }
3118
3119 // Create and schedule the RTCP APP packet for transmission
pwestin@webrtc.org2853dde2012-05-11 11:08:54 +00003120 if (_rtpRtcpModule->SetRTCPApplicationSpecificData(
niklase@google.com470e71d2011-07-07 08:21:25 +00003121 subType,
3122 name,
3123 (const unsigned char*) data,
3124 dataLengthInBytes) != 0)
3125 {
3126 _engineStatisticsPtr->SetLastError(
3127 VE_SEND_ERROR, kTraceError,
3128 "SendApplicationDefinedRTCPPacket() failed to send RTCP packet");
3129 return -1;
3130 }
3131 return 0;
3132}
3133
3134int
3135Channel::GetRTPStatistics(
3136 unsigned int& averageJitterMs,
3137 unsigned int& maxJitterMs,
3138 unsigned int& discardedPackets)
3139{
niklase@google.com470e71d2011-07-07 08:21:25 +00003140 // The jitter statistics is updated for each received RTP packet and is
3141 // based on received packets.
sprang@webrtc.org54ae4ff2013-12-19 13:26:02 +00003142 if (_rtpRtcpModule->RTCP() == kRtcpOff) {
3143 // If RTCP is off, there is no timed thread in the RTCP module regularly
3144 // generating new stats, trigger the update manually here instead.
3145 StreamStatistician* statistician =
3146 rtp_receive_statistics_->GetStatistician(rtp_receiver_->SSRC());
3147 if (statistician) {
3148 // Don't use returned statistics, use data from proxy instead so that
3149 // max jitter can be fetched atomically.
3150 RtcpStatistics s;
3151 statistician->GetStatistics(&s, true);
3152 }
niklase@google.com470e71d2011-07-07 08:21:25 +00003153 }
3154
sprang@webrtc.org54ae4ff2013-12-19 13:26:02 +00003155 ChannelStatistics stats = statistics_proxy_->GetStats();
andrew@webrtc.orgeb524d92013-09-23 23:02:24 +00003156 const int32_t playoutFrequency = audio_coding_->PlayoutFrequency();
sprang@webrtc.org54ae4ff2013-12-19 13:26:02 +00003157 if (playoutFrequency > 0) {
3158 // Scale RTP statistics given the current playout frequency
3159 maxJitterMs = stats.max_jitter / (playoutFrequency / 1000);
3160 averageJitterMs = stats.rtcp.jitter / (playoutFrequency / 1000);
niklase@google.com470e71d2011-07-07 08:21:25 +00003161 }
3162
3163 discardedPackets = _numberOfDiscardedPackets;
3164
3165 WEBRTC_TRACE(kTraceStateInfo, kTraceVoice,
3166 VoEId(_instanceId, _channelId),
3167 "GetRTPStatistics() => averageJitterMs = %lu, maxJitterMs = %lu,"
wu@webrtc.orgfcd12b32011-09-15 20:49:50 +00003168 " discardedPackets = %lu)",
niklase@google.com470e71d2011-07-07 08:21:25 +00003169 averageJitterMs, maxJitterMs, discardedPackets);
3170 return 0;
3171}
3172
henrika@webrtc.org8a2fc882012-08-22 08:53:55 +00003173int Channel::GetRemoteRTCPReportBlocks(
3174 std::vector<ReportBlock>* report_blocks) {
3175 if (report_blocks == NULL) {
3176 _engineStatisticsPtr->SetLastError(VE_INVALID_ARGUMENT, kTraceError,
3177 "GetRemoteRTCPReportBlock()s invalid report_blocks.");
3178 return -1;
3179 }
3180
3181 // Get the report blocks from the latest received RTCP Sender or Receiver
3182 // Report. Each element in the vector contains the sender's SSRC and a
3183 // report block according to RFC 3550.
3184 std::vector<RTCPReportBlock> rtcp_report_blocks;
3185 if (_rtpRtcpModule->RemoteRTCPStat(&rtcp_report_blocks) != 0) {
3186 _engineStatisticsPtr->SetLastError(VE_RTP_RTCP_MODULE_ERROR, kTraceError,
3187 "GetRemoteRTCPReportBlocks() failed to read RTCP SR/RR report block.");
3188 return -1;
3189 }
3190
3191 if (rtcp_report_blocks.empty())
3192 return 0;
3193
3194 std::vector<RTCPReportBlock>::const_iterator it = rtcp_report_blocks.begin();
3195 for (; it != rtcp_report_blocks.end(); ++it) {
3196 ReportBlock report_block;
3197 report_block.sender_SSRC = it->remoteSSRC;
3198 report_block.source_SSRC = it->sourceSSRC;
3199 report_block.fraction_lost = it->fractionLost;
3200 report_block.cumulative_num_packets_lost = it->cumulativeLost;
3201 report_block.extended_highest_sequence_number = it->extendedHighSeqNum;
3202 report_block.interarrival_jitter = it->jitter;
3203 report_block.last_SR_timestamp = it->lastSR;
3204 report_block.delay_since_last_SR = it->delaySinceLastSR;
3205 report_blocks->push_back(report_block);
3206 }
3207 return 0;
3208}
3209
niklase@google.com470e71d2011-07-07 08:21:25 +00003210int
3211Channel::GetRTPStatistics(CallStatistics& stats)
3212{
wu@webrtc.orgcb711f72014-05-19 17:39:11 +00003213 // --- RtcpStatistics
niklase@google.com470e71d2011-07-07 08:21:25 +00003214
3215 // The jitter statistics is updated for each received RTP packet and is
3216 // based on received packets.
sprang@webrtc.org54ae4ff2013-12-19 13:26:02 +00003217 RtcpStatistics statistics;
stefan@webrtc.org286fe0b2013-08-21 20:58:21 +00003218 StreamStatistician* statistician =
3219 rtp_receive_statistics_->GetStatistician(rtp_receiver_->SSRC());
3220 if (!statistician || !statistician->GetStatistics(
wu@webrtc.org822fbd82013-08-15 23:38:54 +00003221 &statistics, _rtpRtcpModule->RTCP() == kRtcpOff)) {
3222 _engineStatisticsPtr->SetLastError(
3223 VE_CANNOT_RETRIEVE_RTP_STAT, kTraceWarning,
3224 "GetRTPStatistics() failed to read RTP statistics from the "
3225 "RTP/RTCP module");
niklase@google.com470e71d2011-07-07 08:21:25 +00003226 }
3227
wu@webrtc.org822fbd82013-08-15 23:38:54 +00003228 stats.fractionLost = statistics.fraction_lost;
3229 stats.cumulativeLost = statistics.cumulative_lost;
3230 stats.extendedMax = statistics.extended_max_sequence_number;
3231 stats.jitterSamples = statistics.jitter;
niklase@google.com470e71d2011-07-07 08:21:25 +00003232
3233 WEBRTC_TRACE(kTraceStateInfo, kTraceVoice,
3234 VoEId(_instanceId, _channelId),
3235 "GetRTPStatistics() => fractionLost=%lu, cumulativeLost=%lu,"
wu@webrtc.orgfcd12b32011-09-15 20:49:50 +00003236 " extendedMax=%lu, jitterSamples=%li)",
niklase@google.com470e71d2011-07-07 08:21:25 +00003237 stats.fractionLost, stats.cumulativeLost, stats.extendedMax,
3238 stats.jitterSamples);
3239
wu@webrtc.orgcb711f72014-05-19 17:39:11 +00003240 // --- RTT
minyue@webrtc.org2b58a442014-09-11 07:51:53 +00003241 stats.rttMs = GetRTT();
minyue@webrtc.org6fd93082014-12-15 14:56:44 +00003242 if (stats.rttMs == 0) {
3243 WEBRTC_TRACE(kTraceWarning, kTraceVoice, VoEId(_instanceId, _channelId),
3244 "GetRTPStatistics() failed to get RTT");
3245 } else {
3246 WEBRTC_TRACE(kTraceStateInfo, kTraceVoice, VoEId(_instanceId, _channelId),
pkasting@chromium.org16825b12015-01-12 21:51:21 +00003247 "GetRTPStatistics() => rttMs=%" PRId64, stats.rttMs);
minyue@webrtc.org6fd93082014-12-15 14:56:44 +00003248 }
niklase@google.com470e71d2011-07-07 08:21:25 +00003249
wu@webrtc.orgcb711f72014-05-19 17:39:11 +00003250 // --- Data counters
niklase@google.com470e71d2011-07-07 08:21:25 +00003251
pkasting@chromium.org4591fbd2014-11-20 22:28:14 +00003252 size_t bytesSent(0);
pbos@webrtc.org6141e132013-04-09 10:09:10 +00003253 uint32_t packetsSent(0);
pkasting@chromium.org4591fbd2014-11-20 22:28:14 +00003254 size_t bytesReceived(0);
pbos@webrtc.org6141e132013-04-09 10:09:10 +00003255 uint32_t packetsReceived(0);
niklase@google.com470e71d2011-07-07 08:21:25 +00003256
stefan@webrtc.org286fe0b2013-08-21 20:58:21 +00003257 if (statistician) {
3258 statistician->GetDataCounters(&bytesReceived, &packetsReceived);
3259 }
wu@webrtc.org822fbd82013-08-15 23:38:54 +00003260
pwestin@webrtc.org2853dde2012-05-11 11:08:54 +00003261 if (_rtpRtcpModule->DataCountersRTP(&bytesSent,
wu@webrtc.org822fbd82013-08-15 23:38:54 +00003262 &packetsSent) != 0)
niklase@google.com470e71d2011-07-07 08:21:25 +00003263 {
3264 WEBRTC_TRACE(kTraceWarning, kTraceVoice,
3265 VoEId(_instanceId, _channelId),
3266 "GetRTPStatistics() failed to retrieve RTP datacounters =>"
wu@webrtc.orgfcd12b32011-09-15 20:49:50 +00003267 " output will not be complete");
niklase@google.com470e71d2011-07-07 08:21:25 +00003268 }
3269
3270 stats.bytesSent = bytesSent;
3271 stats.packetsSent = packetsSent;
3272 stats.bytesReceived = bytesReceived;
3273 stats.packetsReceived = packetsReceived;
3274
3275 WEBRTC_TRACE(kTraceStateInfo, kTraceVoice,
3276 VoEId(_instanceId, _channelId),
pkasting@chromium.org4591fbd2014-11-20 22:28:14 +00003277 "GetRTPStatistics() => bytesSent=%" PRIuS ", packetsSent=%d,"
3278 " bytesReceived=%" PRIuS ", packetsReceived=%d)",
niklase@google.com470e71d2011-07-07 08:21:25 +00003279 stats.bytesSent, stats.packetsSent, stats.bytesReceived,
3280 stats.packetsReceived);
3281
wu@webrtc.orgcb711f72014-05-19 17:39:11 +00003282 // --- Timestamps
3283 {
3284 CriticalSectionScoped lock(ts_stats_lock_.get());
3285 stats.capture_start_ntp_time_ms_ = capture_start_ntp_time_ms_;
3286 }
niklase@google.com470e71d2011-07-07 08:21:25 +00003287 return 0;
3288}
3289
minyue@webrtc.orgc1a40a72014-05-28 09:52:06 +00003290int Channel::SetREDStatus(bool enable, int redPayloadtype) {
turaj@webrtc.org42259e72012-12-11 02:15:12 +00003291 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId, _channelId),
minyue@webrtc.orgc1a40a72014-05-28 09:52:06 +00003292 "Channel::SetREDStatus()");
niklase@google.com470e71d2011-07-07 08:21:25 +00003293
turaj@webrtc.org8c8ad852013-01-31 18:20:17 +00003294 if (enable) {
3295 if (redPayloadtype < 0 || redPayloadtype > 127) {
3296 _engineStatisticsPtr->SetLastError(
3297 VE_PLTYPE_ERROR, kTraceError,
minyue@webrtc.orgc1a40a72014-05-28 09:52:06 +00003298 "SetREDStatus() invalid RED payload type");
turaj@webrtc.org8c8ad852013-01-31 18:20:17 +00003299 return -1;
3300 }
3301
3302 if (SetRedPayloadType(redPayloadtype) < 0) {
3303 _engineStatisticsPtr->SetLastError(
3304 VE_CODEC_ERROR, kTraceError,
3305 "SetSecondarySendCodec() Failed to register RED ACM");
3306 return -1;
3307 }
turaj@webrtc.org42259e72012-12-11 02:15:12 +00003308 }
niklase@google.com470e71d2011-07-07 08:21:25 +00003309
minyue@webrtc.orgaa5ea1c2014-05-23 15:16:51 +00003310 if (audio_coding_->SetREDStatus(enable) != 0) {
turaj@webrtc.org42259e72012-12-11 02:15:12 +00003311 _engineStatisticsPtr->SetLastError(
3312 VE_AUDIO_CODING_MODULE_ERROR, kTraceError,
minyue@webrtc.orgaa5ea1c2014-05-23 15:16:51 +00003313 "SetREDStatus() failed to set RED state in the ACM");
turaj@webrtc.org42259e72012-12-11 02:15:12 +00003314 return -1;
3315 }
3316 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +00003317}
3318
3319int
minyue@webrtc.orgc1a40a72014-05-28 09:52:06 +00003320Channel::GetREDStatus(bool& enabled, int& redPayloadtype)
niklase@google.com470e71d2011-07-07 08:21:25 +00003321{
minyue@webrtc.orgaa5ea1c2014-05-23 15:16:51 +00003322 enabled = audio_coding_->REDStatus();
niklase@google.com470e71d2011-07-07 08:21:25 +00003323 if (enabled)
3324 {
pbos@webrtc.org6141e132013-04-09 10:09:10 +00003325 int8_t payloadType(0);
pwestin@webrtc.org2853dde2012-05-11 11:08:54 +00003326 if (_rtpRtcpModule->SendREDPayloadType(payloadType) != 0)
niklase@google.com470e71d2011-07-07 08:21:25 +00003327 {
3328 _engineStatisticsPtr->SetLastError(
3329 VE_RTP_RTCP_MODULE_ERROR, kTraceError,
minyue@webrtc.orgc1a40a72014-05-28 09:52:06 +00003330 "GetREDStatus() failed to retrieve RED PT from RTP/RTCP "
niklase@google.com470e71d2011-07-07 08:21:25 +00003331 "module");
3332 return -1;
3333 }
pkasting@chromium.orgdf9a41d2015-01-26 22:35:29 +00003334 redPayloadtype = payloadType;
niklase@google.com470e71d2011-07-07 08:21:25 +00003335 WEBRTC_TRACE(kTraceStateInfo, kTraceVoice,
3336 VoEId(_instanceId, _channelId),
minyue@webrtc.orgc1a40a72014-05-28 09:52:06 +00003337 "GetREDStatus() => enabled=%d, redPayloadtype=%d",
niklase@google.com470e71d2011-07-07 08:21:25 +00003338 enabled, redPayloadtype);
3339 return 0;
3340 }
3341 WEBRTC_TRACE(kTraceStateInfo, kTraceVoice,
3342 VoEId(_instanceId, _channelId),
minyue@webrtc.orgc1a40a72014-05-28 09:52:06 +00003343 "GetREDStatus() => enabled=%d", enabled);
niklase@google.com470e71d2011-07-07 08:21:25 +00003344 return 0;
3345}
3346
minyue@webrtc.orgc1a40a72014-05-28 09:52:06 +00003347int Channel::SetCodecFECStatus(bool enable) {
3348 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId, _channelId),
3349 "Channel::SetCodecFECStatus()");
3350
3351 if (audio_coding_->SetCodecFEC(enable) != 0) {
3352 _engineStatisticsPtr->SetLastError(
3353 VE_AUDIO_CODING_MODULE_ERROR, kTraceError,
3354 "SetCodecFECStatus() failed to set FEC state");
3355 return -1;
3356 }
3357 return 0;
3358}
3359
3360bool Channel::GetCodecFECStatus() {
3361 bool enabled = audio_coding_->CodecFEC();
3362 WEBRTC_TRACE(kTraceStateInfo, kTraceVoice,
3363 VoEId(_instanceId, _channelId),
3364 "GetCodecFECStatus() => enabled=%d", enabled);
3365 return enabled;
3366}
3367
pwestin@webrtc.orgdb249952013-06-05 15:33:20 +00003368void Channel::SetNACKStatus(bool enable, int maxNumberOfPackets) {
3369 // None of these functions can fail.
3370 _rtpRtcpModule->SetStorePacketsStatus(enable, maxNumberOfPackets);
stefan@webrtc.org7bb8f022013-09-06 13:40:11 +00003371 rtp_receive_statistics_->SetMaxReorderingThreshold(maxNumberOfPackets);
3372 rtp_receiver_->SetNACKStatus(enable ? kNackRtcp : kNackOff);
pwestin@webrtc.orgd30859e2013-06-06 21:09:01 +00003373 if (enable)
andrew@webrtc.orgeb524d92013-09-23 23:02:24 +00003374 audio_coding_->EnableNack(maxNumberOfPackets);
pwestin@webrtc.orgd30859e2013-06-06 21:09:01 +00003375 else
andrew@webrtc.orgeb524d92013-09-23 23:02:24 +00003376 audio_coding_->DisableNack();
pwestin@webrtc.orgdb249952013-06-05 15:33:20 +00003377}
3378
pwestin@webrtc.orgd30859e2013-06-06 21:09:01 +00003379// Called when we are missing one or more packets.
3380int Channel::ResendPackets(const uint16_t* sequence_numbers, int length) {
pwestin@webrtc.orgdb249952013-06-05 15:33:20 +00003381 return _rtpRtcpModule->SendNACK(sequence_numbers, length);
3382}
3383
niklase@google.com470e71d2011-07-07 08:21:25 +00003384int
niklase@google.com470e71d2011-07-07 08:21:25 +00003385Channel::StartRTPDump(const char fileNameUTF8[1024],
3386 RTPDirections direction)
3387{
3388 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId, _channelId),
3389 "Channel::StartRTPDump()");
3390 if ((direction != kRtpIncoming) && (direction != kRtpOutgoing))
3391 {
3392 _engineStatisticsPtr->SetLastError(
3393 VE_INVALID_ARGUMENT, kTraceError,
3394 "StartRTPDump() invalid RTP direction");
3395 return -1;
3396 }
3397 RtpDump* rtpDumpPtr = (direction == kRtpIncoming) ?
3398 &_rtpDumpIn : &_rtpDumpOut;
3399 if (rtpDumpPtr == NULL)
3400 {
3401 assert(false);
3402 return -1;
3403 }
3404 if (rtpDumpPtr->IsActive())
3405 {
3406 rtpDumpPtr->Stop();
3407 }
3408 if (rtpDumpPtr->Start(fileNameUTF8) != 0)
3409 {
3410 _engineStatisticsPtr->SetLastError(
3411 VE_BAD_FILE, kTraceError,
3412 "StartRTPDump() failed to create file");
3413 return -1;
3414 }
3415 return 0;
3416}
3417
3418int
3419Channel::StopRTPDump(RTPDirections direction)
3420{
3421 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId, _channelId),
3422 "Channel::StopRTPDump()");
3423 if ((direction != kRtpIncoming) && (direction != kRtpOutgoing))
3424 {
3425 _engineStatisticsPtr->SetLastError(
3426 VE_INVALID_ARGUMENT, kTraceError,
3427 "StopRTPDump() invalid RTP direction");
3428 return -1;
3429 }
3430 RtpDump* rtpDumpPtr = (direction == kRtpIncoming) ?
3431 &_rtpDumpIn : &_rtpDumpOut;
3432 if (rtpDumpPtr == NULL)
3433 {
3434 assert(false);
3435 return -1;
3436 }
3437 if (!rtpDumpPtr->IsActive())
3438 {
3439 return 0;
3440 }
3441 return rtpDumpPtr->Stop();
3442}
3443
3444bool
3445Channel::RTPDumpIsActive(RTPDirections direction)
3446{
3447 if ((direction != kRtpIncoming) &&
3448 (direction != kRtpOutgoing))
3449 {
3450 _engineStatisticsPtr->SetLastError(
3451 VE_INVALID_ARGUMENT, kTraceError,
3452 "RTPDumpIsActive() invalid RTP direction");
3453 return false;
3454 }
3455 RtpDump* rtpDumpPtr = (direction == kRtpIncoming) ?
3456 &_rtpDumpIn : &_rtpDumpOut;
3457 return rtpDumpPtr->IsActive();
3458}
3459
solenberg@webrtc.orgb1f50102014-03-24 10:38:25 +00003460void Channel::SetVideoEngineBWETarget(ViENetwork* vie_network,
3461 int video_channel) {
3462 CriticalSectionScoped cs(&_callbackCritSect);
3463 if (vie_network_) {
3464 vie_network_->Release();
3465 vie_network_ = NULL;
3466 }
3467 video_channel_ = -1;
3468
3469 if (vie_network != NULL && video_channel != -1) {
3470 vie_network_ = vie_network;
3471 video_channel_ = video_channel;
3472 }
3473}
3474
pbos@webrtc.org6141e132013-04-09 10:09:10 +00003475uint32_t
andrew@webrtc.org755b04a2011-11-15 16:57:56 +00003476Channel::Demultiplex(const AudioFrame& audioFrame)
niklase@google.com470e71d2011-07-07 08:21:25 +00003477{
3478 WEBRTC_TRACE(kTraceStream, kTraceVoice, VoEId(_instanceId,_channelId),
andrew@webrtc.org755b04a2011-11-15 16:57:56 +00003479 "Channel::Demultiplex()");
andrew@webrtc.orgae1a58b2013-01-22 04:44:30 +00003480 _audioFrame.CopyFrom(audioFrame);
andrew@webrtc.org63a50982012-05-02 23:56:37 +00003481 _audioFrame.id_ = _channelId;
niklase@google.com470e71d2011-07-07 08:21:25 +00003482 return 0;
3483}
3484
xians@webrtc.org2f84afa2013-07-31 16:23:37 +00003485void Channel::Demultiplex(const int16_t* audio_data,
xians@webrtc.org8fff1f02013-07-31 16:27:42 +00003486 int sample_rate,
xians@webrtc.org2f84afa2013-07-31 16:23:37 +00003487 int number_of_frames,
xians@webrtc.org8fff1f02013-07-31 16:27:42 +00003488 int number_of_channels) {
xians@webrtc.org2f84afa2013-07-31 16:23:37 +00003489 CodecInst codec;
3490 GetSendCodec(codec);
xians@webrtc.org2f84afa2013-07-31 16:23:37 +00003491
andrew@webrtc.org40ee3d02014-04-03 21:56:01 +00003492 if (!mono_recording_audio_.get()) {
3493 // Temporary space for DownConvertToCodecFormat.
3494 mono_recording_audio_.reset(new int16_t[kMaxMonoDataSizeSamples]);
xians@webrtc.org2f84afa2013-07-31 16:23:37 +00003495 }
andrew@webrtc.org40ee3d02014-04-03 21:56:01 +00003496 DownConvertToCodecFormat(audio_data,
3497 number_of_frames,
3498 number_of_channels,
3499 sample_rate,
3500 codec.channels,
3501 codec.plfreq,
3502 mono_recording_audio_.get(),
3503 &input_resampler_,
3504 &_audioFrame);
xians@webrtc.org2f84afa2013-07-31 16:23:37 +00003505}
3506
pbos@webrtc.org6141e132013-04-09 10:09:10 +00003507uint32_t
xians@google.com0b0665a2011-08-08 08:18:44 +00003508Channel::PrepareEncodeAndSend(int mixingFrequency)
niklase@google.com470e71d2011-07-07 08:21:25 +00003509{
3510 WEBRTC_TRACE(kTraceStream, kTraceVoice, VoEId(_instanceId,_channelId),
3511 "Channel::PrepareEncodeAndSend()");
3512
andrew@webrtc.org63a50982012-05-02 23:56:37 +00003513 if (_audioFrame.samples_per_channel_ == 0)
niklase@google.com470e71d2011-07-07 08:21:25 +00003514 {
3515 WEBRTC_TRACE(kTraceWarning, kTraceVoice, VoEId(_instanceId,_channelId),
3516 "Channel::PrepareEncodeAndSend() invalid audio frame");
tommi@webrtc.orgeec6ecd2014-07-11 19:09:59 +00003517 return 0xFFFFFFFF;
niklase@google.com470e71d2011-07-07 08:21:25 +00003518 }
3519
henrika@webrtc.org944cbeb2014-03-18 10:32:33 +00003520 if (channel_state_.Get().input_file_playing)
niklase@google.com470e71d2011-07-07 08:21:25 +00003521 {
3522 MixOrReplaceAudioWithFile(mixingFrequency);
3523 }
3524
andrew@webrtc.org21299d42014-05-14 19:00:59 +00003525 bool is_muted = Mute(); // Cache locally as Mute() takes a lock.
3526 if (is_muted) {
3527 AudioFrameOperations::Mute(_audioFrame);
niklase@google.com470e71d2011-07-07 08:21:25 +00003528 }
3529
henrika@webrtc.org944cbeb2014-03-18 10:32:33 +00003530 if (channel_state_.Get().input_external_media)
niklase@google.com470e71d2011-07-07 08:21:25 +00003531 {
mflodman@webrtc.org9a065d12012-03-07 08:12:21 +00003532 CriticalSectionScoped cs(&_callbackCritSect);
andrew@webrtc.org63a50982012-05-02 23:56:37 +00003533 const bool isStereo = (_audioFrame.num_channels_ == 2);
niklase@google.com470e71d2011-07-07 08:21:25 +00003534 if (_inputExternalMediaCallbackPtr)
3535 {
3536 _inputExternalMediaCallbackPtr->Process(
3537 _channelId,
3538 kRecordingPerChannel,
pbos@webrtc.org6141e132013-04-09 10:09:10 +00003539 (int16_t*)_audioFrame.data_,
andrew@webrtc.org63a50982012-05-02 23:56:37 +00003540 _audioFrame.samples_per_channel_,
3541 _audioFrame.sample_rate_hz_,
niklase@google.com470e71d2011-07-07 08:21:25 +00003542 isStereo);
3543 }
3544 }
3545
3546 InsertInbandDtmfTone();
3547
andrew@webrtc.org60730cf2014-01-07 17:45:09 +00003548 if (_includeAudioLevelIndication) {
andrew@webrtc.org382c0c22014-05-05 18:22:21 +00003549 int length = _audioFrame.samples_per_channel_ * _audioFrame.num_channels_;
andrew@webrtc.org21299d42014-05-14 19:00:59 +00003550 if (is_muted) {
3551 rms_level_.ProcessMuted(length);
3552 } else {
3553 rms_level_.Process(_audioFrame.data_, length);
3554 }
andrew@webrtc.org755b04a2011-11-15 16:57:56 +00003555 }
3556
niklase@google.com470e71d2011-07-07 08:21:25 +00003557 return 0;
3558}
3559
pbos@webrtc.org6141e132013-04-09 10:09:10 +00003560uint32_t
niklase@google.com470e71d2011-07-07 08:21:25 +00003561Channel::EncodeAndSend()
3562{
3563 WEBRTC_TRACE(kTraceStream, kTraceVoice, VoEId(_instanceId,_channelId),
3564 "Channel::EncodeAndSend()");
3565
andrew@webrtc.org63a50982012-05-02 23:56:37 +00003566 assert(_audioFrame.num_channels_ <= 2);
3567 if (_audioFrame.samples_per_channel_ == 0)
niklase@google.com470e71d2011-07-07 08:21:25 +00003568 {
3569 WEBRTC_TRACE(kTraceWarning, kTraceVoice, VoEId(_instanceId,_channelId),
3570 "Channel::EncodeAndSend() invalid audio frame");
tommi@webrtc.orgeec6ecd2014-07-11 19:09:59 +00003571 return 0xFFFFFFFF;
niklase@google.com470e71d2011-07-07 08:21:25 +00003572 }
3573
andrew@webrtc.org63a50982012-05-02 23:56:37 +00003574 _audioFrame.id_ = _channelId;
niklase@google.com470e71d2011-07-07 08:21:25 +00003575
3576 // --- Add 10ms of raw (PCM) audio data to the encoder @ 32kHz.
3577
3578 // The ACM resamples internally.
andrew@webrtc.org63a50982012-05-02 23:56:37 +00003579 _audioFrame.timestamp_ = _timeStamp;
henrik.lundin@webrtc.orgf56c1622015-03-02 12:29:30 +00003580 // This call will trigger AudioPacketizationCallback::SendData if encoding
3581 // is done and payload is ready for packetization and transmission.
3582 // Otherwise, it will return without invoking the callback.
3583 if (audio_coding_->Add10MsData((AudioFrame&)_audioFrame) < 0)
niklase@google.com470e71d2011-07-07 08:21:25 +00003584 {
3585 WEBRTC_TRACE(kTraceError, kTraceVoice, VoEId(_instanceId,_channelId),
3586 "Channel::EncodeAndSend() ACM encoding failed");
tommi@webrtc.orgeec6ecd2014-07-11 19:09:59 +00003587 return 0xFFFFFFFF;
niklase@google.com470e71d2011-07-07 08:21:25 +00003588 }
3589
andrew@webrtc.org63a50982012-05-02 23:56:37 +00003590 _timeStamp += _audioFrame.samples_per_channel_;
henrik.lundin@webrtc.orgf56c1622015-03-02 12:29:30 +00003591 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +00003592}
3593
3594int Channel::RegisterExternalMediaProcessing(
3595 ProcessingTypes type,
3596 VoEMediaProcess& processObject)
3597{
3598 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId,_channelId),
3599 "Channel::RegisterExternalMediaProcessing()");
3600
mflodman@webrtc.org9a065d12012-03-07 08:12:21 +00003601 CriticalSectionScoped cs(&_callbackCritSect);
niklase@google.com470e71d2011-07-07 08:21:25 +00003602
3603 if (kPlaybackPerChannel == type)
3604 {
3605 if (_outputExternalMediaCallbackPtr)
3606 {
3607 _engineStatisticsPtr->SetLastError(
3608 VE_INVALID_OPERATION, kTraceError,
3609 "Channel::RegisterExternalMediaProcessing() "
3610 "output external media already enabled");
3611 return -1;
3612 }
3613 _outputExternalMediaCallbackPtr = &processObject;
3614 _outputExternalMedia = true;
3615 }
3616 else if (kRecordingPerChannel == type)
3617 {
3618 if (_inputExternalMediaCallbackPtr)
3619 {
3620 _engineStatisticsPtr->SetLastError(
3621 VE_INVALID_OPERATION, kTraceError,
3622 "Channel::RegisterExternalMediaProcessing() "
3623 "output external media already enabled");
3624 return -1;
3625 }
3626 _inputExternalMediaCallbackPtr = &processObject;
henrika@webrtc.org944cbeb2014-03-18 10:32:33 +00003627 channel_state_.SetInputExternalMedia(true);
niklase@google.com470e71d2011-07-07 08:21:25 +00003628 }
3629 return 0;
3630}
3631
3632int Channel::DeRegisterExternalMediaProcessing(ProcessingTypes type)
3633{
3634 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId,_channelId),
3635 "Channel::DeRegisterExternalMediaProcessing()");
3636
mflodman@webrtc.org9a065d12012-03-07 08:12:21 +00003637 CriticalSectionScoped cs(&_callbackCritSect);
niklase@google.com470e71d2011-07-07 08:21:25 +00003638
3639 if (kPlaybackPerChannel == type)
3640 {
3641 if (!_outputExternalMediaCallbackPtr)
3642 {
3643 _engineStatisticsPtr->SetLastError(
3644 VE_INVALID_OPERATION, kTraceWarning,
3645 "Channel::DeRegisterExternalMediaProcessing() "
3646 "output external media already disabled");
3647 return 0;
3648 }
3649 _outputExternalMedia = false;
3650 _outputExternalMediaCallbackPtr = NULL;
3651 }
3652 else if (kRecordingPerChannel == type)
3653 {
3654 if (!_inputExternalMediaCallbackPtr)
3655 {
3656 _engineStatisticsPtr->SetLastError(
3657 VE_INVALID_OPERATION, kTraceWarning,
3658 "Channel::DeRegisterExternalMediaProcessing() "
3659 "input external media already disabled");
3660 return 0;
3661 }
henrika@webrtc.org944cbeb2014-03-18 10:32:33 +00003662 channel_state_.SetInputExternalMedia(false);
niklase@google.com470e71d2011-07-07 08:21:25 +00003663 _inputExternalMediaCallbackPtr = NULL;
3664 }
3665
3666 return 0;
3667}
3668
roosa@google.com1b60ceb2012-12-12 23:00:29 +00003669int Channel::SetExternalMixing(bool enabled) {
3670 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId,_channelId),
3671 "Channel::SetExternalMixing(enabled=%d)", enabled);
3672
henrika@webrtc.org944cbeb2014-03-18 10:32:33 +00003673 if (channel_state_.Get().playing)
roosa@google.com1b60ceb2012-12-12 23:00:29 +00003674 {
3675 _engineStatisticsPtr->SetLastError(
3676 VE_INVALID_OPERATION, kTraceError,
3677 "Channel::SetExternalMixing() "
3678 "external mixing cannot be changed while playing.");
3679 return -1;
3680 }
3681
3682 _externalMixing = enabled;
3683
3684 return 0;
3685}
3686
niklase@google.com470e71d2011-07-07 08:21:25 +00003687int
niklase@google.com470e71d2011-07-07 08:21:25 +00003688Channel::GetNetworkStatistics(NetworkStatistics& stats)
3689{
3690 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId,_channelId),
3691 "Channel::GetNetworkStatistics()");
minyue@webrtc.orgc0bd7be2015-02-18 15:24:13 +00003692 return audio_coding_->GetNetworkStatistics(&stats);
niklase@google.com470e71d2011-07-07 08:21:25 +00003693}
3694
wu@webrtc.org24301a62013-12-13 19:17:43 +00003695void Channel::GetDecodingCallStatistics(AudioDecodingCallStats* stats) const {
3696 audio_coding_->GetDecodingCallStatistics(stats);
3697}
3698
pwestin@webrtc.org1de01352013-04-11 20:23:35 +00003699bool Channel::GetDelayEstimate(int* jitter_buffer_delay_ms,
3700 int* playout_buffer_delay_ms) const {
3701 if (_average_jitter_buffer_delay_us == 0) {
niklase@google.com470e71d2011-07-07 08:21:25 +00003702 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId,_channelId),
pwestin@webrtc.org1de01352013-04-11 20:23:35 +00003703 "Channel::GetDelayEstimate() no valid estimate.");
3704 return false;
3705 }
3706 *jitter_buffer_delay_ms = (_average_jitter_buffer_delay_us + 500) / 1000 +
3707 _recPacketDelayMs;
3708 *playout_buffer_delay_ms = playout_delay_ms_;
3709 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId,_channelId),
3710 "Channel::GetDelayEstimate()");
3711 return true;
niklase@google.com470e71d2011-07-07 08:21:25 +00003712}
3713
turaj@webrtc.org6388c3e2013-02-12 21:42:18 +00003714int Channel::SetInitialPlayoutDelay(int delay_ms)
3715{
3716 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId,_channelId),
3717 "Channel::SetInitialPlayoutDelay()");
3718 if ((delay_ms < kVoiceEngineMinMinPlayoutDelayMs) ||
3719 (delay_ms > kVoiceEngineMaxMinPlayoutDelayMs))
3720 {
3721 _engineStatisticsPtr->SetLastError(
3722 VE_INVALID_ARGUMENT, kTraceError,
3723 "SetInitialPlayoutDelay() invalid min delay");
3724 return -1;
3725 }
andrew@webrtc.orgeb524d92013-09-23 23:02:24 +00003726 if (audio_coding_->SetInitialPlayoutDelay(delay_ms) != 0)
turaj@webrtc.org6388c3e2013-02-12 21:42:18 +00003727 {
3728 _engineStatisticsPtr->SetLastError(
3729 VE_AUDIO_CODING_MODULE_ERROR, kTraceError,
3730 "SetInitialPlayoutDelay() failed to set min playout delay");
3731 return -1;
3732 }
3733 return 0;
3734}
3735
3736
niklase@google.com470e71d2011-07-07 08:21:25 +00003737int
3738Channel::SetMinimumPlayoutDelay(int delayMs)
3739{
3740 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId,_channelId),
3741 "Channel::SetMinimumPlayoutDelay()");
3742 if ((delayMs < kVoiceEngineMinMinPlayoutDelayMs) ||
3743 (delayMs > kVoiceEngineMaxMinPlayoutDelayMs))
3744 {
3745 _engineStatisticsPtr->SetLastError(
3746 VE_INVALID_ARGUMENT, kTraceError,
3747 "SetMinimumPlayoutDelay() invalid min delay");
3748 return -1;
3749 }
andrew@webrtc.orgeb524d92013-09-23 23:02:24 +00003750 if (audio_coding_->SetMinimumPlayoutDelay(delayMs) != 0)
niklase@google.com470e71d2011-07-07 08:21:25 +00003751 {
3752 _engineStatisticsPtr->SetLastError(
3753 VE_AUDIO_CODING_MODULE_ERROR, kTraceError,
3754 "SetMinimumPlayoutDelay() failed to set min playout delay");
3755 return -1;
3756 }
3757 return 0;
3758}
3759
pwestin@webrtc.org1de01352013-04-11 20:23:35 +00003760void Channel::UpdatePlayoutTimestamp(bool rtcp) {
3761 uint32_t playout_timestamp = 0;
3762
andrew@webrtc.orgeb524d92013-09-23 23:02:24 +00003763 if (audio_coding_->PlayoutTimestamp(&playout_timestamp) == -1) {
turaj@webrtc.org1ebd2e92014-07-25 17:50:10 +00003764 // This can happen if this channel has not been received any RTP packet. In
3765 // this case, NetEq is not capable of computing playout timestamp.
pwestin@webrtc.org1de01352013-04-11 20:23:35 +00003766 return;
3767 }
3768
3769 uint16_t delay_ms = 0;
3770 if (_audioDeviceModulePtr->PlayoutDelay(&delay_ms) == -1) {
3771 WEBRTC_TRACE(kTraceWarning, kTraceVoice, VoEId(_instanceId,_channelId),
3772 "Channel::UpdatePlayoutTimestamp() failed to read playout"
3773 " delay from the ADM");
3774 _engineStatisticsPtr->SetLastError(
3775 VE_CANNOT_RETRIEVE_VALUE, kTraceError,
3776 "UpdatePlayoutTimestamp() failed to retrieve playout delay");
3777 return;
3778 }
3779
turaj@webrtc.org167b6df2013-12-13 21:05:07 +00003780 jitter_buffer_playout_timestamp_ = playout_timestamp;
3781
pwestin@webrtc.org1de01352013-04-11 20:23:35 +00003782 // Remove the playout delay.
wu@webrtc.org94454b72014-06-05 20:34:08 +00003783 playout_timestamp -= (delay_ms * (GetPlayoutFrequency() / 1000));
pwestin@webrtc.org1de01352013-04-11 20:23:35 +00003784
3785 WEBRTC_TRACE(kTraceStream, kTraceVoice, VoEId(_instanceId,_channelId),
3786 "Channel::UpdatePlayoutTimestamp() => playoutTimestamp = %lu",
3787 playout_timestamp);
3788
3789 if (rtcp) {
3790 playout_timestamp_rtcp_ = playout_timestamp;
3791 } else {
3792 playout_timestamp_rtp_ = playout_timestamp;
3793 }
3794 playout_delay_ms_ = delay_ms;
3795}
3796
3797int Channel::GetPlayoutTimestamp(unsigned int& timestamp) {
3798 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId,_channelId),
3799 "Channel::GetPlayoutTimestamp()");
3800 if (playout_timestamp_rtp_ == 0) {
3801 _engineStatisticsPtr->SetLastError(
3802 VE_CANNOT_RETRIEVE_VALUE, kTraceError,
3803 "GetPlayoutTimestamp() failed to retrieve timestamp");
3804 return -1;
3805 }
3806 timestamp = playout_timestamp_rtp_;
3807 WEBRTC_TRACE(kTraceStateInfo, kTraceVoice,
3808 VoEId(_instanceId,_channelId),
3809 "GetPlayoutTimestamp() => timestamp=%u", timestamp);
3810 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +00003811}
3812
pbos@webrtc.orgd16e8392014-12-19 13:49:55 +00003813int Channel::SetInitTimestamp(unsigned int timestamp) {
3814 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId, _channelId),
niklase@google.com470e71d2011-07-07 08:21:25 +00003815 "Channel::SetInitTimestamp()");
pbos@webrtc.orgd16e8392014-12-19 13:49:55 +00003816 if (channel_state_.Get().sending) {
3817 _engineStatisticsPtr->SetLastError(VE_SENDING, kTraceError,
3818 "SetInitTimestamp() already sending");
3819 return -1;
3820 }
3821 _rtpRtcpModule->SetStartTimestamp(timestamp);
3822 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +00003823}
3824
pbos@webrtc.orgd16e8392014-12-19 13:49:55 +00003825int Channel::SetInitSequenceNumber(short sequenceNumber) {
3826 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId, _channelId),
3827 "Channel::SetInitSequenceNumber()");
3828 if (channel_state_.Get().sending) {
3829 _engineStatisticsPtr->SetLastError(
3830 VE_SENDING, kTraceError, "SetInitSequenceNumber() already sending");
3831 return -1;
3832 }
3833 _rtpRtcpModule->SetSequenceNumber(sequenceNumber);
3834 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +00003835}
3836
3837int
wu@webrtc.org822fbd82013-08-15 23:38:54 +00003838Channel::GetRtpRtcp(RtpRtcp** rtpRtcpModule, RtpReceiver** rtp_receiver) const
niklase@google.com470e71d2011-07-07 08:21:25 +00003839{
3840 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId,_channelId),
3841 "Channel::GetRtpRtcp()");
wu@webrtc.org822fbd82013-08-15 23:38:54 +00003842 *rtpRtcpModule = _rtpRtcpModule.get();
3843 *rtp_receiver = rtp_receiver_.get();
niklase@google.com470e71d2011-07-07 08:21:25 +00003844 return 0;
3845}
3846
andrew@webrtc.orge59a0ac2012-05-08 17:12:40 +00003847// TODO(andrew): refactor Mix functions here and in transmit_mixer.cc to use
3848// a shared helper.
pbos@webrtc.org6141e132013-04-09 10:09:10 +00003849int32_t
pbos@webrtc.org92135212013-05-14 08:31:39 +00003850Channel::MixOrReplaceAudioWithFile(int mixingFrequency)
niklase@google.com470e71d2011-07-07 08:21:25 +00003851{
kwiberg@webrtc.org00b8f6b2015-02-26 14:34:55 +00003852 rtc::scoped_ptr<int16_t[]> fileBuffer(new int16_t[640]);
andrew@webrtc.orge59a0ac2012-05-08 17:12:40 +00003853 int fileSamples(0);
niklase@google.com470e71d2011-07-07 08:21:25 +00003854
3855 {
mflodman@webrtc.org9a065d12012-03-07 08:12:21 +00003856 CriticalSectionScoped cs(&_fileCritSect);
niklase@google.com470e71d2011-07-07 08:21:25 +00003857
3858 if (_inputFilePlayerPtr == NULL)
3859 {
3860 WEBRTC_TRACE(kTraceWarning, kTraceVoice,
3861 VoEId(_instanceId, _channelId),
3862 "Channel::MixOrReplaceAudioWithFile() fileplayer"
3863 " doesnt exist");
3864 return -1;
3865 }
3866
braveyao@webrtc.orgd7131432012-03-29 10:39:44 +00003867 if (_inputFilePlayerPtr->Get10msAudioFromFile(fileBuffer.get(),
niklase@google.com470e71d2011-07-07 08:21:25 +00003868 fileSamples,
3869 mixingFrequency) == -1)
3870 {
3871 WEBRTC_TRACE(kTraceWarning, kTraceVoice,
3872 VoEId(_instanceId, _channelId),
3873 "Channel::MixOrReplaceAudioWithFile() file mixing "
3874 "failed");
3875 return -1;
3876 }
3877 if (fileSamples == 0)
3878 {
3879 WEBRTC_TRACE(kTraceWarning, kTraceVoice,
3880 VoEId(_instanceId, _channelId),
3881 "Channel::MixOrReplaceAudioWithFile() file is ended");
3882 return 0;
3883 }
3884 }
3885
andrew@webrtc.org63a50982012-05-02 23:56:37 +00003886 assert(_audioFrame.samples_per_channel_ == fileSamples);
niklase@google.com470e71d2011-07-07 08:21:25 +00003887
3888 if (_mixFileWithMicrophone)
3889 {
braveyao@webrtc.orgd7131432012-03-29 10:39:44 +00003890 // Currently file stream is always mono.
3891 // TODO(xians): Change the code when FilePlayer supports real stereo.
andrew@webrtc.org40ee3d02014-04-03 21:56:01 +00003892 MixWithSat(_audioFrame.data_,
3893 _audioFrame.num_channels_,
3894 fileBuffer.get(),
3895 1,
3896 fileSamples);
niklase@google.com470e71d2011-07-07 08:21:25 +00003897 }
3898 else
3899 {
braveyao@webrtc.orgd7131432012-03-29 10:39:44 +00003900 // Replace ACM audio with file.
3901 // Currently file stream is always mono.
3902 // TODO(xians): Change the code when FilePlayer supports real stereo.
niklase@google.com470e71d2011-07-07 08:21:25 +00003903 _audioFrame.UpdateFrame(_channelId,
tommi@webrtc.orgeec6ecd2014-07-11 19:09:59 +00003904 0xFFFFFFFF,
braveyao@webrtc.orgd7131432012-03-29 10:39:44 +00003905 fileBuffer.get(),
andrew@webrtc.orge59a0ac2012-05-08 17:12:40 +00003906 fileSamples,
niklase@google.com470e71d2011-07-07 08:21:25 +00003907 mixingFrequency,
3908 AudioFrame::kNormalSpeech,
3909 AudioFrame::kVadUnknown,
3910 1);
3911
3912 }
3913 return 0;
3914}
3915
pbos@webrtc.org6141e132013-04-09 10:09:10 +00003916int32_t
niklase@google.com470e71d2011-07-07 08:21:25 +00003917Channel::MixAudioWithFile(AudioFrame& audioFrame,
pbos@webrtc.org92135212013-05-14 08:31:39 +00003918 int mixingFrequency)
niklase@google.com470e71d2011-07-07 08:21:25 +00003919{
minyue@webrtc.org2a8df7c2014-08-06 10:05:19 +00003920 assert(mixingFrequency <= 48000);
niklase@google.com470e71d2011-07-07 08:21:25 +00003921
kwiberg@webrtc.org00b8f6b2015-02-26 14:34:55 +00003922 rtc::scoped_ptr<int16_t[]> fileBuffer(new int16_t[960]);
andrew@webrtc.orge59a0ac2012-05-08 17:12:40 +00003923 int fileSamples(0);
niklase@google.com470e71d2011-07-07 08:21:25 +00003924
3925 {
mflodman@webrtc.org9a065d12012-03-07 08:12:21 +00003926 CriticalSectionScoped cs(&_fileCritSect);
niklase@google.com470e71d2011-07-07 08:21:25 +00003927
3928 if (_outputFilePlayerPtr == NULL)
3929 {
3930 WEBRTC_TRACE(kTraceWarning, kTraceVoice,
3931 VoEId(_instanceId, _channelId),
3932 "Channel::MixAudioWithFile() file mixing failed");
3933 return -1;
3934 }
3935
3936 // We should get the frequency we ask for.
braveyao@webrtc.orgd7131432012-03-29 10:39:44 +00003937 if (_outputFilePlayerPtr->Get10msAudioFromFile(fileBuffer.get(),
niklase@google.com470e71d2011-07-07 08:21:25 +00003938 fileSamples,
3939 mixingFrequency) == -1)
3940 {
3941 WEBRTC_TRACE(kTraceWarning, kTraceVoice,
3942 VoEId(_instanceId, _channelId),
3943 "Channel::MixAudioWithFile() file mixing failed");
3944 return -1;
3945 }
3946 }
3947
andrew@webrtc.org63a50982012-05-02 23:56:37 +00003948 if (audioFrame.samples_per_channel_ == fileSamples)
niklase@google.com470e71d2011-07-07 08:21:25 +00003949 {
braveyao@webrtc.orgd7131432012-03-29 10:39:44 +00003950 // Currently file stream is always mono.
3951 // TODO(xians): Change the code when FilePlayer supports real stereo.
andrew@webrtc.org40ee3d02014-04-03 21:56:01 +00003952 MixWithSat(audioFrame.data_,
3953 audioFrame.num_channels_,
3954 fileBuffer.get(),
3955 1,
3956 fileSamples);
niklase@google.com470e71d2011-07-07 08:21:25 +00003957 }
3958 else
3959 {
3960 WEBRTC_TRACE(kTraceWarning, kTraceVoice, VoEId(_instanceId,_channelId),
andrew@webrtc.org63a50982012-05-02 23:56:37 +00003961 "Channel::MixAudioWithFile() samples_per_channel_(%d) != "
niklase@google.com470e71d2011-07-07 08:21:25 +00003962 "fileSamples(%d)",
andrew@webrtc.org63a50982012-05-02 23:56:37 +00003963 audioFrame.samples_per_channel_, fileSamples);
niklase@google.com470e71d2011-07-07 08:21:25 +00003964 return -1;
3965 }
3966
3967 return 0;
3968}
3969
3970int
3971Channel::InsertInbandDtmfTone()
3972{
niklas.enbom@webrtc.orgaf26f642011-11-16 12:41:36 +00003973 // Check if we should start a new tone.
niklase@google.com470e71d2011-07-07 08:21:25 +00003974 if (_inbandDtmfQueue.PendingDtmf() &&
3975 !_inbandDtmfGenerator.IsAddingTone() &&
3976 _inbandDtmfGenerator.DelaySinceLastTone() >
3977 kMinTelephoneEventSeparationMs)
3978 {
pbos@webrtc.org6141e132013-04-09 10:09:10 +00003979 int8_t eventCode(0);
3980 uint16_t lengthMs(0);
3981 uint8_t attenuationDb(0);
niklase@google.com470e71d2011-07-07 08:21:25 +00003982
3983 eventCode = _inbandDtmfQueue.NextDtmf(&lengthMs, &attenuationDb);
3984 _inbandDtmfGenerator.AddTone(eventCode, lengthMs, attenuationDb);
3985 if (_playInbandDtmfEvent)
3986 {
3987 // Add tone to output mixer using a reduced length to minimize
3988 // risk of echo.
3989 _outputMixerPtr->PlayDtmfTone(eventCode, lengthMs - 80,
3990 attenuationDb);
3991 }
3992 }
3993
3994 if (_inbandDtmfGenerator.IsAddingTone())
3995 {
pbos@webrtc.org6141e132013-04-09 10:09:10 +00003996 uint16_t frequency(0);
niklase@google.com470e71d2011-07-07 08:21:25 +00003997 _inbandDtmfGenerator.GetSampleRate(frequency);
3998
andrew@webrtc.org63a50982012-05-02 23:56:37 +00003999 if (frequency != _audioFrame.sample_rate_hz_)
niklase@google.com470e71d2011-07-07 08:21:25 +00004000 {
4001 // Update sample rate of Dtmf tone since the mixing frequency
4002 // has changed.
4003 _inbandDtmfGenerator.SetSampleRate(
pbos@webrtc.org6141e132013-04-09 10:09:10 +00004004 (uint16_t) (_audioFrame.sample_rate_hz_));
niklase@google.com470e71d2011-07-07 08:21:25 +00004005 // Reset the tone to be added taking the new sample rate into
4006 // account.
4007 _inbandDtmfGenerator.ResetTone();
4008 }
andrew@webrtc.orgae1a58b2013-01-22 04:44:30 +00004009
pbos@webrtc.org6141e132013-04-09 10:09:10 +00004010 int16_t toneBuffer[320];
4011 uint16_t toneSamples(0);
niklase@google.com470e71d2011-07-07 08:21:25 +00004012 // Get 10ms tone segment and set time since last tone to zero
4013 if (_inbandDtmfGenerator.Get10msTone(toneBuffer, toneSamples) == -1)
4014 {
4015 WEBRTC_TRACE(kTraceWarning, kTraceVoice,
4016 VoEId(_instanceId, _channelId),
4017 "Channel::EncodeAndSend() inserting Dtmf failed");
4018 return -1;
4019 }
4020
niklas.enbom@webrtc.orgaf26f642011-11-16 12:41:36 +00004021 // Replace mixed audio with DTMF tone.
andrew@webrtc.orgae1a58b2013-01-22 04:44:30 +00004022 for (int sample = 0;
andrew@webrtc.org63a50982012-05-02 23:56:37 +00004023 sample < _audioFrame.samples_per_channel_;
niklas.enbom@webrtc.orgaf26f642011-11-16 12:41:36 +00004024 sample++)
4025 {
andrew@webrtc.orgae1a58b2013-01-22 04:44:30 +00004026 for (int channel = 0;
4027 channel < _audioFrame.num_channels_;
niklas.enbom@webrtc.orgaf26f642011-11-16 12:41:36 +00004028 channel++)
4029 {
andrew@webrtc.orgae1a58b2013-01-22 04:44:30 +00004030 const int index = sample * _audioFrame.num_channels_ + channel;
4031 _audioFrame.data_[index] = toneBuffer[sample];
niklas.enbom@webrtc.orgaf26f642011-11-16 12:41:36 +00004032 }
4033 }
andrew@webrtc.orgae1a58b2013-01-22 04:44:30 +00004034
andrew@webrtc.org63a50982012-05-02 23:56:37 +00004035 assert(_audioFrame.samples_per_channel_ == toneSamples);
niklase@google.com470e71d2011-07-07 08:21:25 +00004036 } else
4037 {
4038 // Add 10ms to "delay-since-last-tone" counter
4039 _inbandDtmfGenerator.UpdateDelaySinceLastTone();
4040 }
4041 return 0;
4042}
4043
pbos@webrtc.org6141e132013-04-09 10:09:10 +00004044int32_t
pkasting@chromium.org4591fbd2014-11-20 22:28:14 +00004045Channel::SendPacketRaw(const void *data, size_t len, bool RTCP)
niklase@google.com470e71d2011-07-07 08:21:25 +00004046{
wu@webrtc.orgfb648da2013-10-18 21:10:51 +00004047 CriticalSectionScoped cs(&_callbackCritSect);
niklase@google.com470e71d2011-07-07 08:21:25 +00004048 if (_transportPtr == NULL)
4049 {
4050 return -1;
4051 }
4052 if (!RTCP)
4053 {
4054 return _transportPtr->SendPacket(_channelId, data, len);
4055 }
4056 else
4057 {
4058 return _transportPtr->SendRTCPPacket(_channelId, data, len);
4059 }
4060}
4061
pwestin@webrtc.org1de01352013-04-11 20:23:35 +00004062// Called for incoming RTP packets after successful RTP header parsing.
4063void Channel::UpdatePacketDelay(uint32_t rtp_timestamp,
4064 uint16_t sequence_number) {
4065 WEBRTC_TRACE(kTraceStream, kTraceVoice, VoEId(_instanceId,_channelId),
4066 "Channel::UpdatePacketDelay(timestamp=%lu, sequenceNumber=%u)",
4067 rtp_timestamp, sequence_number);
niklase@google.com470e71d2011-07-07 08:21:25 +00004068
pwestin@webrtc.org1de01352013-04-11 20:23:35 +00004069 // Get frequency of last received payload
wu@webrtc.org94454b72014-06-05 20:34:08 +00004070 int rtp_receive_frequency = GetPlayoutFrequency();
niklase@google.com470e71d2011-07-07 08:21:25 +00004071
turaj@webrtc.orge46c8d32013-05-22 20:39:43 +00004072 // Update the least required delay.
andrew@webrtc.orgeb524d92013-09-23 23:02:24 +00004073 least_required_delay_ms_ = audio_coding_->LeastRequiredDelayMs();
turaj@webrtc.orge46c8d32013-05-22 20:39:43 +00004074
turaj@webrtc.org167b6df2013-12-13 21:05:07 +00004075 // |jitter_buffer_playout_timestamp_| updated in UpdatePlayoutTimestamp for
4076 // every incoming packet.
4077 uint32_t timestamp_diff_ms = (rtp_timestamp -
4078 jitter_buffer_playout_timestamp_) / (rtp_receive_frequency / 1000);
henrik.lundin@webrtc.orgd6692992014-03-20 12:04:09 +00004079 if (!IsNewerTimestamp(rtp_timestamp, jitter_buffer_playout_timestamp_) ||
4080 timestamp_diff_ms > (2 * kVoiceEngineMaxMinPlayoutDelayMs)) {
4081 // If |jitter_buffer_playout_timestamp_| is newer than the incoming RTP
4082 // timestamp, the resulting difference is negative, but is set to zero.
4083 // This can happen when a network glitch causes a packet to arrive late,
4084 // and during long comfort noise periods with clock drift.
4085 timestamp_diff_ms = 0;
4086 }
niklase@google.com470e71d2011-07-07 08:21:25 +00004087
pwestin@webrtc.org1de01352013-04-11 20:23:35 +00004088 uint16_t packet_delay_ms = (rtp_timestamp - _previousTimestamp) /
4089 (rtp_receive_frequency / 1000);
niklase@google.com470e71d2011-07-07 08:21:25 +00004090
pwestin@webrtc.org1de01352013-04-11 20:23:35 +00004091 _previousTimestamp = rtp_timestamp;
niklase@google.com470e71d2011-07-07 08:21:25 +00004092
pwestin@webrtc.org1de01352013-04-11 20:23:35 +00004093 if (timestamp_diff_ms == 0) return;
niklase@google.com470e71d2011-07-07 08:21:25 +00004094
pwestin@webrtc.org1de01352013-04-11 20:23:35 +00004095 if (packet_delay_ms >= 10 && packet_delay_ms <= 60) {
4096 _recPacketDelayMs = packet_delay_ms;
4097 }
niklase@google.com470e71d2011-07-07 08:21:25 +00004098
pwestin@webrtc.org1de01352013-04-11 20:23:35 +00004099 if (_average_jitter_buffer_delay_us == 0) {
4100 _average_jitter_buffer_delay_us = timestamp_diff_ms * 1000;
4101 return;
4102 }
4103
4104 // Filter average delay value using exponential filter (alpha is
4105 // 7/8). We derive 1000 *_average_jitter_buffer_delay_us here (reduces
4106 // risk of rounding error) and compensate for it in GetDelayEstimate()
4107 // later.
4108 _average_jitter_buffer_delay_us = (_average_jitter_buffer_delay_us * 7 +
4109 1000 * timestamp_diff_ms + 500) / 8;
niklase@google.com470e71d2011-07-07 08:21:25 +00004110}
4111
4112void
4113Channel::RegisterReceiveCodecsToRTPModule()
4114{
4115 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId,_channelId),
4116 "Channel::RegisterReceiveCodecsToRTPModule()");
4117
4118
4119 CodecInst codec;
pbos@webrtc.org6141e132013-04-09 10:09:10 +00004120 const uint8_t nSupportedCodecs = AudioCodingModule::NumberOfCodecs();
niklase@google.com470e71d2011-07-07 08:21:25 +00004121
4122 for (int idx = 0; idx < nSupportedCodecs; idx++)
4123 {
4124 // Open up the RTP/RTCP receiver for all supported codecs
andrew@webrtc.orgeb524d92013-09-23 23:02:24 +00004125 if ((audio_coding_->Codec(idx, &codec) == -1) ||
wu@webrtc.org822fbd82013-08-15 23:38:54 +00004126 (rtp_receiver_->RegisterReceivePayload(
4127 codec.plname,
4128 codec.pltype,
4129 codec.plfreq,
4130 codec.channels,
4131 (codec.rate < 0) ? 0 : codec.rate) == -1))
niklase@google.com470e71d2011-07-07 08:21:25 +00004132 {
4133 WEBRTC_TRACE(
4134 kTraceWarning,
4135 kTraceVoice,
4136 VoEId(_instanceId, _channelId),
4137 "Channel::RegisterReceiveCodecsToRTPModule() unable"
4138 " to register %s (%d/%d/%d/%d) to RTP/RTCP receiver",
4139 codec.plname, codec.pltype, codec.plfreq,
4140 codec.channels, codec.rate);
4141 }
4142 else
4143 {
4144 WEBRTC_TRACE(
4145 kTraceInfo,
4146 kTraceVoice,
4147 VoEId(_instanceId, _channelId),
4148 "Channel::RegisterReceiveCodecsToRTPModule() %s "
wu@webrtc.orgfcd12b32011-09-15 20:49:50 +00004149 "(%d/%d/%d/%d) has been added to the RTP/RTCP "
niklase@google.com470e71d2011-07-07 08:21:25 +00004150 "receiver",
4151 codec.plname, codec.pltype, codec.plfreq,
4152 codec.channels, codec.rate);
4153 }
4154 }
4155}
4156
turaj@webrtc.org8c8ad852013-01-31 18:20:17 +00004157// Assuming this method is called with valid payload type.
turaj@webrtc.org42259e72012-12-11 02:15:12 +00004158int Channel::SetRedPayloadType(int red_payload_type) {
turaj@webrtc.org42259e72012-12-11 02:15:12 +00004159 CodecInst codec;
4160 bool found_red = false;
4161
4162 // Get default RED settings from the ACM database
4163 const int num_codecs = AudioCodingModule::NumberOfCodecs();
4164 for (int idx = 0; idx < num_codecs; idx++) {
andrew@webrtc.orgeb524d92013-09-23 23:02:24 +00004165 audio_coding_->Codec(idx, &codec);
turaj@webrtc.org42259e72012-12-11 02:15:12 +00004166 if (!STR_CASE_CMP(codec.plname, "RED")) {
4167 found_red = true;
4168 break;
4169 }
4170 }
4171
4172 if (!found_red) {
4173 _engineStatisticsPtr->SetLastError(
4174 VE_CODEC_ERROR, kTraceError,
4175 "SetRedPayloadType() RED is not supported");
4176 return -1;
4177 }
4178
turaj@webrtc.org9d532fd2013-01-31 18:34:19 +00004179 codec.pltype = red_payload_type;
andrew@webrtc.orgeb524d92013-09-23 23:02:24 +00004180 if (audio_coding_->RegisterSendCodec(codec) < 0) {
turaj@webrtc.org42259e72012-12-11 02:15:12 +00004181 _engineStatisticsPtr->SetLastError(
4182 VE_AUDIO_CODING_MODULE_ERROR, kTraceError,
4183 "SetRedPayloadType() RED registration in ACM module failed");
4184 return -1;
4185 }
4186
4187 if (_rtpRtcpModule->SetSendREDPayloadType(red_payload_type) != 0) {
4188 _engineStatisticsPtr->SetLastError(
4189 VE_RTP_RTCP_MODULE_ERROR, kTraceError,
4190 "SetRedPayloadType() RED registration in RTP/RTCP module failed");
4191 return -1;
4192 }
4193 return 0;
4194}
4195
wu@webrtc.orgebdb0e32014-03-06 23:49:08 +00004196int Channel::SetSendRtpHeaderExtension(bool enable, RTPExtensionType type,
4197 unsigned char id) {
4198 int error = 0;
4199 _rtpRtcpModule->DeregisterSendRtpHeaderExtension(type);
4200 if (enable) {
4201 error = _rtpRtcpModule->RegisterSendRtpHeaderExtension(type, id);
4202 }
4203 return error;
4204}
minyue@webrtc.orgc1a40a72014-05-28 09:52:06 +00004205
wu@webrtc.org94454b72014-06-05 20:34:08 +00004206int32_t Channel::GetPlayoutFrequency() {
4207 int32_t playout_frequency = audio_coding_->PlayoutFrequency();
4208 CodecInst current_recive_codec;
4209 if (audio_coding_->ReceiveCodec(&current_recive_codec) == 0) {
4210 if (STR_CASE_CMP("G722", current_recive_codec.plname) == 0) {
4211 // Even though the actual sampling rate for G.722 audio is
4212 // 16,000 Hz, the RTP clock rate for the G722 payload format is
4213 // 8,000 Hz because that value was erroneously assigned in
4214 // RFC 1890 and must remain unchanged for backward compatibility.
4215 playout_frequency = 8000;
4216 } else if (STR_CASE_CMP("opus", current_recive_codec.plname) == 0) {
4217 // We are resampling Opus internally to 32,000 Hz until all our
4218 // DSP routines can operate at 48,000 Hz, but the RTP clock
4219 // rate for the Opus payload format is standardized to 48,000 Hz,
4220 // because that is the maximum supported decoding sampling rate.
4221 playout_frequency = 48000;
4222 }
4223 }
4224 return playout_frequency;
4225}
4226
pkasting@chromium.org16825b12015-01-12 21:51:21 +00004227int64_t Channel::GetRTT() const {
minyue@webrtc.org2b58a442014-09-11 07:51:53 +00004228 RTCPMethod method = _rtpRtcpModule->RTCP();
4229 if (method == kRtcpOff) {
minyue@webrtc.org2b58a442014-09-11 07:51:53 +00004230 return 0;
4231 }
4232 std::vector<RTCPReportBlock> report_blocks;
4233 _rtpRtcpModule->RemoteRTCPStat(&report_blocks);
4234 if (report_blocks.empty()) {
minyue@webrtc.org2b58a442014-09-11 07:51:53 +00004235 return 0;
4236 }
4237
4238 uint32_t remoteSSRC = rtp_receiver_->SSRC();
4239 std::vector<RTCPReportBlock>::const_iterator it = report_blocks.begin();
4240 for (; it != report_blocks.end(); ++it) {
4241 if (it->remoteSSRC == remoteSSRC)
4242 break;
4243 }
4244 if (it == report_blocks.end()) {
4245 // We have not received packets with SSRC matching the report blocks.
4246 // To calculate RTT we try with the SSRC of the first report block.
4247 // This is very important for send-only channels where we don't know
4248 // the SSRC of the other end.
4249 remoteSSRC = report_blocks[0].remoteSSRC;
4250 }
pkasting@chromium.org16825b12015-01-12 21:51:21 +00004251 int64_t rtt = 0;
4252 int64_t avg_rtt = 0;
4253 int64_t max_rtt= 0;
4254 int64_t min_rtt = 0;
minyue@webrtc.org2b58a442014-09-11 07:51:53 +00004255 if (_rtpRtcpModule->RTT(remoteSSRC, &rtt, &avg_rtt, &min_rtt, &max_rtt)
4256 != 0) {
minyue@webrtc.org2b58a442014-09-11 07:51:53 +00004257 return 0;
4258 }
pkasting@chromium.org16825b12015-01-12 21:51:21 +00004259 return rtt;
minyue@webrtc.org2b58a442014-09-11 07:51:53 +00004260}
4261
pbos@webrtc.orgd900e8b2013-07-03 15:12:26 +00004262} // namespace voe
4263} // namespace webrtc