blob: 472ed8b7fe452ff507fb26415fad6a279ba0dd5a [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
mflodman@webrtc.org0a7d4ee2015-02-17 12:57:14 +00001345void Channel::OnIncomingFractionLoss(int fraction_lost) {
minyue@webrtc.org74aaf292014-07-16 21:28:26 +00001346 network_predictor_->UpdatePacketLossRate(fraction_lost);
mflodman@webrtc.org0a7d4ee2015-02-17 12:57:14 +00001347 uint8_t average_fraction_loss = network_predictor_->GetLossRate();
1348
minyue@webrtc.orgc1a40a72014-05-28 09:52:06 +00001349 // Normalizes rate to 0 - 100.
mflodman@webrtc.org0a7d4ee2015-02-17 12:57:14 +00001350 if (audio_coding_->SetPacketLossRate(
1351 100 * average_fraction_loss / 255) != 0) {
minyue@webrtc.orgc1a40a72014-05-28 09:52:06 +00001352 assert(false); // This should not happen.
1353 }
1354}
1355
pbos@webrtc.org6141e132013-04-09 10:09:10 +00001356int32_t
niklase@google.com470e71d2011-07-07 08:21:25 +00001357Channel::SetVADStatus(bool enableVAD, ACMVADMode mode, bool disableDTX)
1358{
1359 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId,_channelId),
1360 "Channel::SetVADStatus(mode=%d)", mode);
henrik.lundin@webrtc.org664ccb72015-01-28 14:49:05 +00001361 assert(!(disableDTX && enableVAD)); // disableDTX mode is deprecated.
niklase@google.com470e71d2011-07-07 08:21:25 +00001362 // To disable VAD, DTX must be disabled too
1363 disableDTX = ((enableVAD == false) ? true : disableDTX);
andrew@webrtc.orgeb524d92013-09-23 23:02:24 +00001364 if (audio_coding_->SetVAD(!disableDTX, enableVAD, mode) != 0)
niklase@google.com470e71d2011-07-07 08:21:25 +00001365 {
1366 _engineStatisticsPtr->SetLastError(
1367 VE_AUDIO_CODING_MODULE_ERROR, kTraceError,
1368 "SetVADStatus() failed to set VAD");
1369 return -1;
1370 }
1371 return 0;
1372}
1373
pbos@webrtc.org6141e132013-04-09 10:09:10 +00001374int32_t
niklase@google.com470e71d2011-07-07 08:21:25 +00001375Channel::GetVADStatus(bool& enabledVAD, ACMVADMode& mode, bool& disabledDTX)
1376{
1377 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId,_channelId),
1378 "Channel::GetVADStatus");
andrew@webrtc.orgeb524d92013-09-23 23:02:24 +00001379 if (audio_coding_->VAD(&disabledDTX, &enabledVAD, &mode) != 0)
niklase@google.com470e71d2011-07-07 08:21:25 +00001380 {
1381 _engineStatisticsPtr->SetLastError(
1382 VE_AUDIO_CODING_MODULE_ERROR, kTraceError,
1383 "GetVADStatus() failed to get VAD status");
1384 return -1;
1385 }
1386 disabledDTX = !disabledDTX;
1387 return 0;
1388}
1389
pbos@webrtc.org6141e132013-04-09 10:09:10 +00001390int32_t
niklase@google.com470e71d2011-07-07 08:21:25 +00001391Channel::SetRecPayloadType(const CodecInst& codec)
1392{
1393 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId,_channelId),
1394 "Channel::SetRecPayloadType()");
1395
henrika@webrtc.org944cbeb2014-03-18 10:32:33 +00001396 if (channel_state_.Get().playing)
niklase@google.com470e71d2011-07-07 08:21:25 +00001397 {
1398 _engineStatisticsPtr->SetLastError(
1399 VE_ALREADY_PLAYING, kTraceError,
1400 "SetRecPayloadType() unable to set PT while playing");
1401 return -1;
1402 }
henrika@webrtc.org944cbeb2014-03-18 10:32:33 +00001403 if (channel_state_.Get().receiving)
niklase@google.com470e71d2011-07-07 08:21:25 +00001404 {
1405 _engineStatisticsPtr->SetLastError(
1406 VE_ALREADY_LISTENING, kTraceError,
1407 "SetRecPayloadType() unable to set PT while listening");
1408 return -1;
1409 }
1410
1411 if (codec.pltype == -1)
1412 {
1413 // De-register the selected codec (RTP/RTCP module and ACM)
1414
pbos@webrtc.org6141e132013-04-09 10:09:10 +00001415 int8_t pltype(-1);
niklase@google.com470e71d2011-07-07 08:21:25 +00001416 CodecInst rxCodec = codec;
1417
1418 // Get payload type for the given codec
wu@webrtc.org822fbd82013-08-15 23:38:54 +00001419 rtp_payload_registry_->ReceivePayloadType(
1420 rxCodec.plname,
1421 rxCodec.plfreq,
1422 rxCodec.channels,
1423 (rxCodec.rate < 0) ? 0 : rxCodec.rate,
1424 &pltype);
niklase@google.com470e71d2011-07-07 08:21:25 +00001425 rxCodec.pltype = pltype;
1426
wu@webrtc.org822fbd82013-08-15 23:38:54 +00001427 if (rtp_receiver_->DeRegisterReceivePayload(pltype) != 0)
niklase@google.com470e71d2011-07-07 08:21:25 +00001428 {
1429 _engineStatisticsPtr->SetLastError(
1430 VE_RTP_RTCP_MODULE_ERROR,
1431 kTraceError,
1432 "SetRecPayloadType() RTP/RTCP-module deregistration "
1433 "failed");
1434 return -1;
1435 }
andrew@webrtc.orgeb524d92013-09-23 23:02:24 +00001436 if (audio_coding_->UnregisterReceiveCodec(rxCodec.pltype) != 0)
niklase@google.com470e71d2011-07-07 08:21:25 +00001437 {
1438 _engineStatisticsPtr->SetLastError(
1439 VE_AUDIO_CODING_MODULE_ERROR, kTraceError,
1440 "SetRecPayloadType() ACM deregistration failed - 1");
1441 return -1;
1442 }
1443 return 0;
1444 }
1445
wu@webrtc.org822fbd82013-08-15 23:38:54 +00001446 if (rtp_receiver_->RegisterReceivePayload(
1447 codec.plname,
1448 codec.pltype,
1449 codec.plfreq,
1450 codec.channels,
1451 (codec.rate < 0) ? 0 : codec.rate) != 0)
niklase@google.com470e71d2011-07-07 08:21:25 +00001452 {
1453 // First attempt to register failed => de-register and try again
wu@webrtc.org822fbd82013-08-15 23:38:54 +00001454 rtp_receiver_->DeRegisterReceivePayload(codec.pltype);
1455 if (rtp_receiver_->RegisterReceivePayload(
1456 codec.plname,
1457 codec.pltype,
1458 codec.plfreq,
1459 codec.channels,
1460 (codec.rate < 0) ? 0 : codec.rate) != 0)
niklase@google.com470e71d2011-07-07 08:21:25 +00001461 {
1462 _engineStatisticsPtr->SetLastError(
1463 VE_RTP_RTCP_MODULE_ERROR, kTraceError,
1464 "SetRecPayloadType() RTP/RTCP-module registration failed");
1465 return -1;
1466 }
1467 }
andrew@webrtc.orgeb524d92013-09-23 23:02:24 +00001468 if (audio_coding_->RegisterReceiveCodec(codec) != 0)
niklase@google.com470e71d2011-07-07 08:21:25 +00001469 {
andrew@webrtc.orgeb524d92013-09-23 23:02:24 +00001470 audio_coding_->UnregisterReceiveCodec(codec.pltype);
1471 if (audio_coding_->RegisterReceiveCodec(codec) != 0)
niklase@google.com470e71d2011-07-07 08:21:25 +00001472 {
1473 _engineStatisticsPtr->SetLastError(
1474 VE_AUDIO_CODING_MODULE_ERROR, kTraceError,
1475 "SetRecPayloadType() ACM registration failed - 1");
1476 return -1;
1477 }
1478 }
1479 return 0;
1480}
1481
pbos@webrtc.org6141e132013-04-09 10:09:10 +00001482int32_t
niklase@google.com470e71d2011-07-07 08:21:25 +00001483Channel::GetRecPayloadType(CodecInst& codec)
1484{
1485 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId,_channelId),
1486 "Channel::GetRecPayloadType()");
pbos@webrtc.org6141e132013-04-09 10:09:10 +00001487 int8_t payloadType(-1);
wu@webrtc.org822fbd82013-08-15 23:38:54 +00001488 if (rtp_payload_registry_->ReceivePayloadType(
1489 codec.plname,
1490 codec.plfreq,
1491 codec.channels,
1492 (codec.rate < 0) ? 0 : codec.rate,
1493 &payloadType) != 0)
niklase@google.com470e71d2011-07-07 08:21:25 +00001494 {
1495 _engineStatisticsPtr->SetLastError(
henrika@webrtc.org37198002012-06-18 11:00:12 +00001496 VE_RTP_RTCP_MODULE_ERROR, kTraceWarning,
niklase@google.com470e71d2011-07-07 08:21:25 +00001497 "GetRecPayloadType() failed to retrieve RX payload type");
1498 return -1;
1499 }
1500 codec.pltype = payloadType;
1501 WEBRTC_TRACE(kTraceStateInfo, kTraceVoice, VoEId(_instanceId,_channelId),
pkasting@chromium.orgd3245462015-02-23 21:28:22 +00001502 "Channel::GetRecPayloadType() => pltype=%d", codec.pltype);
niklase@google.com470e71d2011-07-07 08:21:25 +00001503 return 0;
1504}
1505
pbos@webrtc.org6141e132013-04-09 10:09:10 +00001506int32_t
niklase@google.com470e71d2011-07-07 08:21:25 +00001507Channel::SetSendCNPayloadType(int type, PayloadFrequencies frequency)
1508{
1509 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId,_channelId),
1510 "Channel::SetSendCNPayloadType()");
1511
1512 CodecInst codec;
pbos@webrtc.org6141e132013-04-09 10:09:10 +00001513 int32_t samplingFreqHz(-1);
tina.legrand@webrtc.org45175852012-06-01 09:27:35 +00001514 const int kMono = 1;
niklase@google.com470e71d2011-07-07 08:21:25 +00001515 if (frequency == kFreq32000Hz)
1516 samplingFreqHz = 32000;
1517 else if (frequency == kFreq16000Hz)
1518 samplingFreqHz = 16000;
1519
andrew@webrtc.orgeb524d92013-09-23 23:02:24 +00001520 if (audio_coding_->Codec("CN", &codec, samplingFreqHz, kMono) == -1)
niklase@google.com470e71d2011-07-07 08:21:25 +00001521 {
1522 _engineStatisticsPtr->SetLastError(
1523 VE_AUDIO_CODING_MODULE_ERROR, kTraceError,
1524 "SetSendCNPayloadType() failed to retrieve default CN codec "
1525 "settings");
1526 return -1;
1527 }
1528
1529 // Modify the payload type (must be set to dynamic range)
1530 codec.pltype = type;
1531
andrew@webrtc.orgeb524d92013-09-23 23:02:24 +00001532 if (audio_coding_->RegisterSendCodec(codec) != 0)
niklase@google.com470e71d2011-07-07 08:21:25 +00001533 {
1534 _engineStatisticsPtr->SetLastError(
1535 VE_AUDIO_CODING_MODULE_ERROR, kTraceError,
1536 "SetSendCNPayloadType() failed to register CN to ACM");
1537 return -1;
1538 }
1539
pwestin@webrtc.org2853dde2012-05-11 11:08:54 +00001540 if (_rtpRtcpModule->RegisterSendPayload(codec) != 0)
niklase@google.com470e71d2011-07-07 08:21:25 +00001541 {
pwestin@webrtc.org2853dde2012-05-11 11:08:54 +00001542 _rtpRtcpModule->DeRegisterSendPayload(codec.pltype);
1543 if (_rtpRtcpModule->RegisterSendPayload(codec) != 0)
niklase@google.com470e71d2011-07-07 08:21:25 +00001544 {
1545 _engineStatisticsPtr->SetLastError(
1546 VE_RTP_RTCP_MODULE_ERROR, kTraceError,
1547 "SetSendCNPayloadType() failed to register CN to RTP/RTCP "
1548 "module");
1549 return -1;
1550 }
1551 }
1552 return 0;
1553}
1554
minyue@webrtc.orgadee8f92014-09-03 12:28:06 +00001555int Channel::SetOpusMaxPlaybackRate(int frequency_hz) {
minyue@webrtc.org6aac93b2014-08-12 08:13:33 +00001556 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId, _channelId),
minyue@webrtc.orgadee8f92014-09-03 12:28:06 +00001557 "Channel::SetOpusMaxPlaybackRate()");
minyue@webrtc.org6aac93b2014-08-12 08:13:33 +00001558
minyue@webrtc.orgadee8f92014-09-03 12:28:06 +00001559 if (audio_coding_->SetOpusMaxPlaybackRate(frequency_hz) != 0) {
minyue@webrtc.org6aac93b2014-08-12 08:13:33 +00001560 _engineStatisticsPtr->SetLastError(
1561 VE_AUDIO_CODING_MODULE_ERROR, kTraceError,
minyue@webrtc.orgadee8f92014-09-03 12:28:06 +00001562 "SetOpusMaxPlaybackRate() failed to set maximum playback rate");
minyue@webrtc.org6aac93b2014-08-12 08:13:33 +00001563 return -1;
1564 }
1565 return 0;
1566}
1567
minyue@webrtc.org9b2e1142015-03-13 09:38:07 +00001568int Channel::SetOpusDtx(bool enable_dtx) {
1569 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId, _channelId),
1570 "Channel::SetOpusDtx(%d)", enable_dtx);
1571 int ret = enable_dtx ? audio_coding_->EnableOpusDtx(true)
1572 : audio_coding_->DisableOpusDtx();
1573 if (ret != 0) {
1574 _engineStatisticsPtr->SetLastError(
1575 VE_AUDIO_CODING_MODULE_ERROR, kTraceError, "SetOpusDtx() failed");
1576 return -1;
1577 }
1578 return 0;
1579}
1580
pbos@webrtc.org6141e132013-04-09 10:09:10 +00001581int32_t Channel::RegisterExternalTransport(Transport& transport)
niklase@google.com470e71d2011-07-07 08:21:25 +00001582{
1583 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId, _channelId),
1584 "Channel::RegisterExternalTransport()");
1585
mflodman@webrtc.org9a065d12012-03-07 08:12:21 +00001586 CriticalSectionScoped cs(&_callbackCritSect);
niklase@google.com470e71d2011-07-07 08:21:25 +00001587
niklase@google.com470e71d2011-07-07 08:21:25 +00001588 if (_externalTransport)
1589 {
1590 _engineStatisticsPtr->SetLastError(VE_INVALID_OPERATION,
1591 kTraceError,
1592 "RegisterExternalTransport() external transport already enabled");
1593 return -1;
1594 }
1595 _externalTransport = true;
1596 _transportPtr = &transport;
1597 return 0;
1598}
1599
pbos@webrtc.org6141e132013-04-09 10:09:10 +00001600int32_t
niklase@google.com470e71d2011-07-07 08:21:25 +00001601Channel::DeRegisterExternalTransport()
1602{
1603 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId,_channelId),
1604 "Channel::DeRegisterExternalTransport()");
1605
mflodman@webrtc.org9a065d12012-03-07 08:12:21 +00001606 CriticalSectionScoped cs(&_callbackCritSect);
xians@webrtc.org83661f52011-11-25 10:58:15 +00001607
niklase@google.com470e71d2011-07-07 08:21:25 +00001608 if (!_transportPtr)
1609 {
1610 _engineStatisticsPtr->SetLastError(
1611 VE_INVALID_OPERATION, kTraceWarning,
1612 "DeRegisterExternalTransport() external transport already "
1613 "disabled");
1614 return 0;
1615 }
1616 _externalTransport = false;
niklase@google.com470e71d2011-07-07 08:21:25 +00001617 _transportPtr = NULL;
1618 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId,_channelId),
1619 "DeRegisterExternalTransport() all transport is disabled");
niklase@google.com470e71d2011-07-07 08:21:25 +00001620 return 0;
1621}
1622
pkasting@chromium.org4591fbd2014-11-20 22:28:14 +00001623int32_t Channel::ReceivedRTPPacket(const int8_t* data, size_t length,
solenberg@webrtc.orgb1f50102014-03-24 10:38:25 +00001624 const PacketTime& packet_time) {
pwestin@webrtc.org0c459572013-04-03 15:43:57 +00001625 WEBRTC_TRACE(kTraceStream, kTraceVoice, VoEId(_instanceId,_channelId),
1626 "Channel::ReceivedRTPPacket()");
1627
1628 // Store playout timestamp for the received RTP packet
pwestin@webrtc.org1de01352013-04-11 20:23:35 +00001629 UpdatePlayoutTimestamp(false);
pwestin@webrtc.org0c459572013-04-03 15:43:57 +00001630
1631 // Dump the RTP packet to a file (if RTP dump is enabled).
pbos@webrtc.org6141e132013-04-09 10:09:10 +00001632 if (_rtpDumpIn.DumpPacket((const uint8_t*)data,
1633 (uint16_t)length) == -1) {
pwestin@webrtc.org0c459572013-04-03 15:43:57 +00001634 WEBRTC_TRACE(kTraceWarning, kTraceVoice,
1635 VoEId(_instanceId,_channelId),
1636 "Channel::SendPacket() RTP dump to input file failed");
1637 }
stefan@webrtc.org7bb8f022013-09-06 13:40:11 +00001638 const uint8_t* received_packet = reinterpret_cast<const uint8_t*>(data);
stefan@webrtc.orga5cb98c2013-05-29 12:12:51 +00001639 RTPHeader header;
stefan@webrtc.org7bb8f022013-09-06 13:40:11 +00001640 if (!rtp_header_parser_->Parse(received_packet, length, &header)) {
1641 WEBRTC_TRACE(webrtc::kTraceDebug, webrtc::kTraceVoice, _channelId,
1642 "Incoming packet: invalid RTP header");
stefan@webrtc.orga5cb98c2013-05-29 12:12:51 +00001643 return -1;
1644 }
wu@webrtc.org822fbd82013-08-15 23:38:54 +00001645 header.payload_type_frequency =
1646 rtp_payload_registry_->GetPayloadTypeFrequency(header.payloadType);
stefan@webrtc.org7bb8f022013-09-06 13:40:11 +00001647 if (header.payload_type_frequency < 0)
wu@webrtc.org822fbd82013-08-15 23:38:54 +00001648 return -1;
stefan@webrtc.org48df3812013-11-08 15:18:52 +00001649 bool in_order = IsPacketInOrder(header);
stefan@webrtc.org7bb8f022013-09-06 13:40:11 +00001650 rtp_receive_statistics_->IncomingPacket(header, length,
stefan@webrtc.org48df3812013-11-08 15:18:52 +00001651 IsPacketRetransmitted(header, in_order));
stefan@webrtc.org7bb8f022013-09-06 13:40:11 +00001652 rtp_payload_registry_->SetIncomingPayloadType(header);
solenberg@webrtc.orgb1f50102014-03-24 10:38:25 +00001653
1654 // Forward any packets to ViE bandwidth estimator, if enabled.
1655 {
1656 CriticalSectionScoped cs(&_callbackCritSect);
1657 if (vie_network_) {
1658 int64_t arrival_time_ms;
1659 if (packet_time.timestamp != -1) {
1660 arrival_time_ms = (packet_time.timestamp + 500) / 1000;
1661 } else {
1662 arrival_time_ms = TickTime::MillisecondTimestamp();
1663 }
pkasting@chromium.org4591fbd2014-11-20 22:28:14 +00001664 size_t payload_length = length - header.headerLength;
solenberg@webrtc.orgb1f50102014-03-24 10:38:25 +00001665 vie_network_->ReceivedBWEPacket(video_channel_, arrival_time_ms,
1666 payload_length, header);
1667 }
1668 }
1669
stefan@webrtc.org48df3812013-11-08 15:18:52 +00001670 return ReceivePacket(received_packet, length, header, in_order) ? 0 : -1;
stefan@webrtc.org7bb8f022013-09-06 13:40:11 +00001671}
1672
1673bool Channel::ReceivePacket(const uint8_t* packet,
pkasting@chromium.org4591fbd2014-11-20 22:28:14 +00001674 size_t packet_length,
stefan@webrtc.org7bb8f022013-09-06 13:40:11 +00001675 const RTPHeader& header,
1676 bool in_order) {
minyue@webrtc.org456f0142015-01-23 11:58:42 +00001677 if (rtp_payload_registry_->IsRtx(header)) {
1678 return HandleRtxPacket(packet, packet_length, header);
wu@webrtc.org822fbd82013-08-15 23:38:54 +00001679 }
stefan@webrtc.org7bb8f022013-09-06 13:40:11 +00001680 const uint8_t* payload = packet + header.headerLength;
pkasting@chromium.org4591fbd2014-11-20 22:28:14 +00001681 assert(packet_length >= header.headerLength);
1682 size_t payload_length = packet_length - header.headerLength;
wu@webrtc.org822fbd82013-08-15 23:38:54 +00001683 PayloadUnion payload_specific;
1684 if (!rtp_payload_registry_->GetPayloadSpecifics(header.payloadType,
stefan@webrtc.org7bb8f022013-09-06 13:40:11 +00001685 &payload_specific)) {
1686 return false;
wu@webrtc.org822fbd82013-08-15 23:38:54 +00001687 }
stefan@webrtc.org7bb8f022013-09-06 13:40:11 +00001688 return rtp_receiver_->IncomingRtpPacket(header, payload, payload_length,
1689 payload_specific, in_order);
1690}
1691
minyue@webrtc.org456f0142015-01-23 11:58:42 +00001692bool Channel::HandleRtxPacket(const uint8_t* packet,
1693 size_t packet_length,
1694 const RTPHeader& header) {
stefan@webrtc.org7bb8f022013-09-06 13:40:11 +00001695 if (!rtp_payload_registry_->IsRtx(header))
1696 return false;
1697
1698 // Remove the RTX header and parse the original RTP header.
1699 if (packet_length < header.headerLength)
1700 return false;
1701 if (packet_length > kVoiceEngineMaxIpPacketSizeBytes)
1702 return false;
1703 if (restored_packet_in_use_) {
1704 WEBRTC_TRACE(webrtc::kTraceDebug, webrtc::kTraceVoice, _channelId,
1705 "Multiple RTX headers detected, dropping packet");
1706 return false;
pwestin@webrtc.org0c459572013-04-03 15:43:57 +00001707 }
stefan@webrtc.org7bb8f022013-09-06 13:40:11 +00001708 uint8_t* restored_packet_ptr = restored_packet_;
1709 if (!rtp_payload_registry_->RestoreOriginalPacket(
1710 &restored_packet_ptr, packet, &packet_length, rtp_receiver_->SSRC(),
1711 header)) {
1712 WEBRTC_TRACE(webrtc::kTraceDebug, webrtc::kTraceVoice, _channelId,
1713 "Incoming RTX packet: invalid RTP header");
1714 return false;
1715 }
1716 restored_packet_in_use_ = true;
1717 bool ret = OnRecoveredPacket(restored_packet_ptr, packet_length);
1718 restored_packet_in_use_ = false;
1719 return ret;
1720}
1721
1722bool Channel::IsPacketInOrder(const RTPHeader& header) const {
1723 StreamStatistician* statistician =
1724 rtp_receive_statistics_->GetStatistician(header.ssrc);
1725 if (!statistician)
1726 return false;
1727 return statistician->IsPacketInOrder(header.sequenceNumber);
niklase@google.com470e71d2011-07-07 08:21:25 +00001728}
1729
stefan@webrtc.org48df3812013-11-08 15:18:52 +00001730bool Channel::IsPacketRetransmitted(const RTPHeader& header,
1731 bool in_order) const {
stefan@webrtc.org7bb8f022013-09-06 13:40:11 +00001732 // Retransmissions are handled separately if RTX is enabled.
1733 if (rtp_payload_registry_->RtxEnabled())
1734 return false;
1735 StreamStatistician* statistician =
1736 rtp_receive_statistics_->GetStatistician(header.ssrc);
1737 if (!statistician)
1738 return false;
1739 // Check if this is a retransmission.
pkasting@chromium.org16825b12015-01-12 21:51:21 +00001740 int64_t min_rtt = 0;
stefan@webrtc.org7bb8f022013-09-06 13:40:11 +00001741 _rtpRtcpModule->RTT(rtp_receiver_->SSRC(), NULL, NULL, &min_rtt, NULL);
stefan@webrtc.org48df3812013-11-08 15:18:52 +00001742 return !in_order &&
stefan@webrtc.org7bb8f022013-09-06 13:40:11 +00001743 statistician->IsRetransmitOfOldPacket(header, min_rtt);
wu@webrtc.org822fbd82013-08-15 23:38:54 +00001744}
1745
pkasting@chromium.org4591fbd2014-11-20 22:28:14 +00001746int32_t Channel::ReceivedRTCPPacket(const int8_t* data, size_t length) {
pwestin@webrtc.org0c459572013-04-03 15:43:57 +00001747 WEBRTC_TRACE(kTraceStream, kTraceVoice, VoEId(_instanceId,_channelId),
1748 "Channel::ReceivedRTCPPacket()");
1749 // Store playout timestamp for the received RTCP packet
pwestin@webrtc.org1de01352013-04-11 20:23:35 +00001750 UpdatePlayoutTimestamp(true);
pwestin@webrtc.org0c459572013-04-03 15:43:57 +00001751
1752 // Dump the RTCP packet to a file (if RTP dump is enabled).
pkasting@chromium.org4591fbd2014-11-20 22:28:14 +00001753 if (_rtpDumpIn.DumpPacket((const uint8_t*)data, length) == -1) {
pwestin@webrtc.org0c459572013-04-03 15:43:57 +00001754 WEBRTC_TRACE(kTraceWarning, kTraceVoice,
1755 VoEId(_instanceId,_channelId),
1756 "Channel::SendPacket() RTCP dump to input file failed");
1757 }
1758
1759 // Deliver RTCP packet to RTP/RTCP module for parsing
pkasting@chromium.org4591fbd2014-11-20 22:28:14 +00001760 if (_rtpRtcpModule->IncomingRtcpPacket((const uint8_t*)data, length) == -1) {
pwestin@webrtc.org0c459572013-04-03 15:43:57 +00001761 _engineStatisticsPtr->SetLastError(
1762 VE_SOCKET_TRANSPORT_MODULE_ERROR, kTraceWarning,
1763 "Channel::IncomingRTPPacket() RTCP packet is invalid");
1764 }
wu@webrtc.org82c4b852014-05-20 22:55:01 +00001765
stefan@webrtc.org8e24d872014-09-02 18:58:24 +00001766 {
1767 CriticalSectionScoped lock(ts_stats_lock_.get());
pkasting@chromium.org16825b12015-01-12 21:51:21 +00001768 int64_t rtt = GetRTT();
minyue@webrtc.org2c0cdbc2014-10-09 10:52:43 +00001769 if (rtt == 0) {
1770 // Waiting for valid RTT.
1771 return 0;
1772 }
1773 uint32_t ntp_secs = 0;
1774 uint32_t ntp_frac = 0;
1775 uint32_t rtp_timestamp = 0;
1776 if (0 != _rtpRtcpModule->RemoteNTP(&ntp_secs, &ntp_frac, NULL, NULL,
1777 &rtp_timestamp)) {
1778 // Waiting for RTCP.
1779 return 0;
1780 }
1781 ntp_estimator_.UpdateRtcpTimestamp(rtt, ntp_secs, ntp_frac, rtp_timestamp);
stefan@webrtc.org8e24d872014-09-02 18:58:24 +00001782 }
pwestin@webrtc.org0c459572013-04-03 15:43:57 +00001783 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +00001784}
1785
niklase@google.com470e71d2011-07-07 08:21:25 +00001786int Channel::StartPlayingFileLocally(const char* fileName,
pbos@webrtc.org92135212013-05-14 08:31:39 +00001787 bool loop,
1788 FileFormats format,
1789 int startPosition,
1790 float volumeScaling,
1791 int stopPosition,
niklase@google.com470e71d2011-07-07 08:21:25 +00001792 const CodecInst* codecInst)
1793{
1794 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId,_channelId),
1795 "Channel::StartPlayingFileLocally(fileNameUTF8[]=%s, loop=%d,"
1796 " format=%d, volumeScaling=%5.3f, startPosition=%d, "
1797 "stopPosition=%d)", fileName, loop, format, volumeScaling,
1798 startPosition, stopPosition);
1799
henrika@webrtc.org944cbeb2014-03-18 10:32:33 +00001800 if (channel_state_.Get().output_file_playing)
niklase@google.com470e71d2011-07-07 08:21:25 +00001801 {
1802 _engineStatisticsPtr->SetLastError(
1803 VE_ALREADY_PLAYING, kTraceError,
1804 "StartPlayingFileLocally() is already playing");
1805 return -1;
1806 }
1807
niklase@google.com470e71d2011-07-07 08:21:25 +00001808 {
mflodman@webrtc.org9a065d12012-03-07 08:12:21 +00001809 CriticalSectionScoped cs(&_fileCritSect);
henrike@webrtc.orgb37c6282011-10-31 23:53:04 +00001810
1811 if (_outputFilePlayerPtr)
1812 {
1813 _outputFilePlayerPtr->RegisterModuleFileCallback(NULL);
1814 FilePlayer::DestroyFilePlayer(_outputFilePlayerPtr);
1815 _outputFilePlayerPtr = NULL;
1816 }
1817
1818 _outputFilePlayerPtr = FilePlayer::CreateFilePlayer(
1819 _outputFilePlayerId, (const FileFormats)format);
1820
1821 if (_outputFilePlayerPtr == NULL)
1822 {
1823 _engineStatisticsPtr->SetLastError(
1824 VE_INVALID_ARGUMENT, kTraceError,
henrike@webrtc.org31d30702011-11-18 19:59:32 +00001825 "StartPlayingFileLocally() filePlayer format is not correct");
henrike@webrtc.orgb37c6282011-10-31 23:53:04 +00001826 return -1;
1827 }
1828
pbos@webrtc.org6141e132013-04-09 10:09:10 +00001829 const uint32_t notificationTime(0);
henrike@webrtc.orgb37c6282011-10-31 23:53:04 +00001830
1831 if (_outputFilePlayerPtr->StartPlayingFile(
1832 fileName,
1833 loop,
1834 startPosition,
1835 volumeScaling,
1836 notificationTime,
1837 stopPosition,
1838 (const CodecInst*)codecInst) != 0)
1839 {
1840 _engineStatisticsPtr->SetLastError(
1841 VE_BAD_FILE, kTraceError,
1842 "StartPlayingFile() failed to start file playout");
1843 _outputFilePlayerPtr->StopPlayingFile();
1844 FilePlayer::DestroyFilePlayer(_outputFilePlayerPtr);
1845 _outputFilePlayerPtr = NULL;
1846 return -1;
1847 }
1848 _outputFilePlayerPtr->RegisterModuleFileCallback(this);
henrika@webrtc.org944cbeb2014-03-18 10:32:33 +00001849 channel_state_.SetOutputFilePlaying(true);
niklase@google.com470e71d2011-07-07 08:21:25 +00001850 }
braveyao@webrtc.orgab129902012-06-04 03:26:39 +00001851
1852 if (RegisterFilePlayingToMixer() != 0)
henrike@webrtc.org066f9e52011-10-28 23:15:47 +00001853 return -1;
niklase@google.com470e71d2011-07-07 08:21:25 +00001854
1855 return 0;
1856}
1857
1858int Channel::StartPlayingFileLocally(InStream* stream,
pbos@webrtc.org92135212013-05-14 08:31:39 +00001859 FileFormats format,
1860 int startPosition,
1861 float volumeScaling,
1862 int stopPosition,
niklase@google.com470e71d2011-07-07 08:21:25 +00001863 const CodecInst* codecInst)
1864{
1865 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId,_channelId),
1866 "Channel::StartPlayingFileLocally(format=%d,"
1867 " volumeScaling=%5.3f, startPosition=%d, stopPosition=%d)",
1868 format, volumeScaling, startPosition, stopPosition);
1869
1870 if(stream == NULL)
1871 {
1872 _engineStatisticsPtr->SetLastError(
1873 VE_BAD_FILE, kTraceError,
1874 "StartPlayingFileLocally() NULL as input stream");
1875 return -1;
1876 }
1877
1878
henrika@webrtc.org944cbeb2014-03-18 10:32:33 +00001879 if (channel_state_.Get().output_file_playing)
niklase@google.com470e71d2011-07-07 08:21:25 +00001880 {
1881 _engineStatisticsPtr->SetLastError(
1882 VE_ALREADY_PLAYING, kTraceError,
1883 "StartPlayingFileLocally() is already playing");
1884 return -1;
1885 }
1886
niklase@google.com470e71d2011-07-07 08:21:25 +00001887 {
mflodman@webrtc.org9a065d12012-03-07 08:12:21 +00001888 CriticalSectionScoped cs(&_fileCritSect);
henrike@webrtc.orgb37c6282011-10-31 23:53:04 +00001889
1890 // Destroy the old instance
1891 if (_outputFilePlayerPtr)
1892 {
1893 _outputFilePlayerPtr->RegisterModuleFileCallback(NULL);
1894 FilePlayer::DestroyFilePlayer(_outputFilePlayerPtr);
1895 _outputFilePlayerPtr = NULL;
1896 }
1897
1898 // Create the instance
1899 _outputFilePlayerPtr = FilePlayer::CreateFilePlayer(
1900 _outputFilePlayerId,
1901 (const FileFormats)format);
1902
1903 if (_outputFilePlayerPtr == NULL)
1904 {
1905 _engineStatisticsPtr->SetLastError(
1906 VE_INVALID_ARGUMENT, kTraceError,
1907 "StartPlayingFileLocally() filePlayer format isnot correct");
1908 return -1;
1909 }
1910
pbos@webrtc.org6141e132013-04-09 10:09:10 +00001911 const uint32_t notificationTime(0);
henrike@webrtc.orgb37c6282011-10-31 23:53:04 +00001912
1913 if (_outputFilePlayerPtr->StartPlayingFile(*stream, startPosition,
1914 volumeScaling,
1915 notificationTime,
1916 stopPosition, codecInst) != 0)
1917 {
1918 _engineStatisticsPtr->SetLastError(VE_BAD_FILE, kTraceError,
1919 "StartPlayingFile() failed to "
1920 "start file playout");
1921 _outputFilePlayerPtr->StopPlayingFile();
1922 FilePlayer::DestroyFilePlayer(_outputFilePlayerPtr);
1923 _outputFilePlayerPtr = NULL;
1924 return -1;
1925 }
1926 _outputFilePlayerPtr->RegisterModuleFileCallback(this);
henrika@webrtc.org944cbeb2014-03-18 10:32:33 +00001927 channel_state_.SetOutputFilePlaying(true);
niklase@google.com470e71d2011-07-07 08:21:25 +00001928 }
braveyao@webrtc.orgab129902012-06-04 03:26:39 +00001929
1930 if (RegisterFilePlayingToMixer() != 0)
henrike@webrtc.org066f9e52011-10-28 23:15:47 +00001931 return -1;
niklase@google.com470e71d2011-07-07 08:21:25 +00001932
niklase@google.com470e71d2011-07-07 08:21:25 +00001933 return 0;
1934}
1935
1936int Channel::StopPlayingFileLocally()
1937{
1938 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId,_channelId),
1939 "Channel::StopPlayingFileLocally()");
1940
henrika@webrtc.org944cbeb2014-03-18 10:32:33 +00001941 if (!channel_state_.Get().output_file_playing)
niklase@google.com470e71d2011-07-07 08:21:25 +00001942 {
1943 _engineStatisticsPtr->SetLastError(
1944 VE_INVALID_OPERATION, kTraceWarning,
1945 "StopPlayingFileLocally() isnot playing");
1946 return 0;
1947 }
1948
niklase@google.com470e71d2011-07-07 08:21:25 +00001949 {
mflodman@webrtc.org9a065d12012-03-07 08:12:21 +00001950 CriticalSectionScoped cs(&_fileCritSect);
henrike@webrtc.orgb37c6282011-10-31 23:53:04 +00001951
1952 if (_outputFilePlayerPtr->StopPlayingFile() != 0)
1953 {
1954 _engineStatisticsPtr->SetLastError(
1955 VE_STOP_RECORDING_FAILED, kTraceError,
1956 "StopPlayingFile() could not stop playing");
1957 return -1;
1958 }
1959 _outputFilePlayerPtr->RegisterModuleFileCallback(NULL);
1960 FilePlayer::DestroyFilePlayer(_outputFilePlayerPtr);
1961 _outputFilePlayerPtr = NULL;
henrika@webrtc.org944cbeb2014-03-18 10:32:33 +00001962 channel_state_.SetOutputFilePlaying(false);
niklase@google.com470e71d2011-07-07 08:21:25 +00001963 }
henrike@webrtc.orgb37c6282011-10-31 23:53:04 +00001964 // _fileCritSect cannot be taken while calling
1965 // SetAnonymousMixibilityStatus. Refer to comments in
1966 // StartPlayingFileLocally(const char* ...) for more details.
henrike@webrtc.org066f9e52011-10-28 23:15:47 +00001967 if (_outputMixerPtr->SetAnonymousMixabilityStatus(*this, false) != 0)
1968 {
1969 _engineStatisticsPtr->SetLastError(
1970 VE_AUDIO_CONF_MIX_MODULE_ERROR, kTraceError,
henrike@webrtc.orgb37c6282011-10-31 23:53:04 +00001971 "StopPlayingFile() failed to stop participant from playing as"
1972 "file in the mixer");
henrike@webrtc.org066f9e52011-10-28 23:15:47 +00001973 return -1;
1974 }
niklase@google.com470e71d2011-07-07 08:21:25 +00001975
1976 return 0;
1977}
1978
1979int Channel::IsPlayingFileLocally() const
1980{
1981 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId,_channelId),
1982 "Channel::IsPlayingFileLocally()");
1983
henrika@webrtc.org944cbeb2014-03-18 10:32:33 +00001984 return channel_state_.Get().output_file_playing;
niklase@google.com470e71d2011-07-07 08:21:25 +00001985}
1986
braveyao@webrtc.orgab129902012-06-04 03:26:39 +00001987int Channel::RegisterFilePlayingToMixer()
1988{
1989 // Return success for not registering for file playing to mixer if:
1990 // 1. playing file before playout is started on that channel.
1991 // 2. starting playout without file playing on that channel.
henrika@webrtc.org944cbeb2014-03-18 10:32:33 +00001992 if (!channel_state_.Get().playing ||
1993 !channel_state_.Get().output_file_playing)
braveyao@webrtc.orgab129902012-06-04 03:26:39 +00001994 {
1995 return 0;
1996 }
1997
1998 // |_fileCritSect| cannot be taken while calling
1999 // SetAnonymousMixabilityStatus() since as soon as the participant is added
2000 // frames can be pulled by the mixer. Since the frames are generated from
2001 // the file, _fileCritSect will be taken. This would result in a deadlock.
2002 if (_outputMixerPtr->SetAnonymousMixabilityStatus(*this, true) != 0)
2003 {
henrika@webrtc.org944cbeb2014-03-18 10:32:33 +00002004 channel_state_.SetOutputFilePlaying(false);
braveyao@webrtc.orgab129902012-06-04 03:26:39 +00002005 CriticalSectionScoped cs(&_fileCritSect);
braveyao@webrtc.orgab129902012-06-04 03:26:39 +00002006 _engineStatisticsPtr->SetLastError(
2007 VE_AUDIO_CONF_MIX_MODULE_ERROR, kTraceError,
2008 "StartPlayingFile() failed to add participant as file to mixer");
2009 _outputFilePlayerPtr->StopPlayingFile();
2010 FilePlayer::DestroyFilePlayer(_outputFilePlayerPtr);
2011 _outputFilePlayerPtr = NULL;
2012 return -1;
2013 }
2014
2015 return 0;
2016}
2017
niklase@google.com470e71d2011-07-07 08:21:25 +00002018int Channel::StartPlayingFileAsMicrophone(const char* fileName,
pbos@webrtc.org92135212013-05-14 08:31:39 +00002019 bool loop,
2020 FileFormats format,
2021 int startPosition,
2022 float volumeScaling,
2023 int stopPosition,
niklase@google.com470e71d2011-07-07 08:21:25 +00002024 const CodecInst* codecInst)
2025{
2026 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId,_channelId),
2027 "Channel::StartPlayingFileAsMicrophone(fileNameUTF8[]=%s, "
2028 "loop=%d, format=%d, volumeScaling=%5.3f, startPosition=%d, "
2029 "stopPosition=%d)", fileName, loop, format, volumeScaling,
2030 startPosition, stopPosition);
2031
henrika@webrtc.org944cbeb2014-03-18 10:32:33 +00002032 CriticalSectionScoped cs(&_fileCritSect);
2033
2034 if (channel_state_.Get().input_file_playing)
niklase@google.com470e71d2011-07-07 08:21:25 +00002035 {
2036 _engineStatisticsPtr->SetLastError(
2037 VE_ALREADY_PLAYING, kTraceWarning,
2038 "StartPlayingFileAsMicrophone() filePlayer is playing");
2039 return 0;
2040 }
2041
niklase@google.com470e71d2011-07-07 08:21:25 +00002042 // Destroy the old instance
2043 if (_inputFilePlayerPtr)
2044 {
2045 _inputFilePlayerPtr->RegisterModuleFileCallback(NULL);
2046 FilePlayer::DestroyFilePlayer(_inputFilePlayerPtr);
2047 _inputFilePlayerPtr = NULL;
2048 }
2049
2050 // Create the instance
2051 _inputFilePlayerPtr = FilePlayer::CreateFilePlayer(
2052 _inputFilePlayerId, (const FileFormats)format);
2053
2054 if (_inputFilePlayerPtr == NULL)
2055 {
2056 _engineStatisticsPtr->SetLastError(
2057 VE_INVALID_ARGUMENT, kTraceError,
2058 "StartPlayingFileAsMicrophone() filePlayer format isnot correct");
2059 return -1;
2060 }
2061
pbos@webrtc.org6141e132013-04-09 10:09:10 +00002062 const uint32_t notificationTime(0);
niklase@google.com470e71d2011-07-07 08:21:25 +00002063
2064 if (_inputFilePlayerPtr->StartPlayingFile(
2065 fileName,
2066 loop,
2067 startPosition,
2068 volumeScaling,
2069 notificationTime,
2070 stopPosition,
2071 (const CodecInst*)codecInst) != 0)
2072 {
2073 _engineStatisticsPtr->SetLastError(
2074 VE_BAD_FILE, kTraceError,
2075 "StartPlayingFile() failed to start file playout");
2076 _inputFilePlayerPtr->StopPlayingFile();
2077 FilePlayer::DestroyFilePlayer(_inputFilePlayerPtr);
2078 _inputFilePlayerPtr = NULL;
2079 return -1;
2080 }
2081 _inputFilePlayerPtr->RegisterModuleFileCallback(this);
henrika@webrtc.org944cbeb2014-03-18 10:32:33 +00002082 channel_state_.SetInputFilePlaying(true);
niklase@google.com470e71d2011-07-07 08:21:25 +00002083
2084 return 0;
2085}
2086
2087int Channel::StartPlayingFileAsMicrophone(InStream* stream,
pbos@webrtc.org92135212013-05-14 08:31:39 +00002088 FileFormats format,
2089 int startPosition,
2090 float volumeScaling,
2091 int stopPosition,
niklase@google.com470e71d2011-07-07 08:21:25 +00002092 const CodecInst* codecInst)
2093{
2094 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId,_channelId),
2095 "Channel::StartPlayingFileAsMicrophone(format=%d, "
2096 "volumeScaling=%5.3f, startPosition=%d, stopPosition=%d)",
2097 format, volumeScaling, startPosition, stopPosition);
2098
2099 if(stream == NULL)
2100 {
2101 _engineStatisticsPtr->SetLastError(
2102 VE_BAD_FILE, kTraceError,
2103 "StartPlayingFileAsMicrophone NULL as input stream");
2104 return -1;
2105 }
2106
henrika@webrtc.org944cbeb2014-03-18 10:32:33 +00002107 CriticalSectionScoped cs(&_fileCritSect);
2108
2109 if (channel_state_.Get().input_file_playing)
niklase@google.com470e71d2011-07-07 08:21:25 +00002110 {
2111 _engineStatisticsPtr->SetLastError(
2112 VE_ALREADY_PLAYING, kTraceWarning,
2113 "StartPlayingFileAsMicrophone() is playing");
2114 return 0;
2115 }
2116
niklase@google.com470e71d2011-07-07 08:21:25 +00002117 // Destroy the old instance
2118 if (_inputFilePlayerPtr)
2119 {
2120 _inputFilePlayerPtr->RegisterModuleFileCallback(NULL);
2121 FilePlayer::DestroyFilePlayer(_inputFilePlayerPtr);
2122 _inputFilePlayerPtr = NULL;
2123 }
2124
2125 // Create the instance
2126 _inputFilePlayerPtr = FilePlayer::CreateFilePlayer(
2127 _inputFilePlayerId, (const FileFormats)format);
2128
2129 if (_inputFilePlayerPtr == NULL)
2130 {
2131 _engineStatisticsPtr->SetLastError(
2132 VE_INVALID_ARGUMENT, kTraceError,
2133 "StartPlayingInputFile() filePlayer format isnot correct");
2134 return -1;
2135 }
2136
pbos@webrtc.org6141e132013-04-09 10:09:10 +00002137 const uint32_t notificationTime(0);
niklase@google.com470e71d2011-07-07 08:21:25 +00002138
2139 if (_inputFilePlayerPtr->StartPlayingFile(*stream, startPosition,
2140 volumeScaling, notificationTime,
2141 stopPosition, codecInst) != 0)
2142 {
2143 _engineStatisticsPtr->SetLastError(VE_BAD_FILE, kTraceError,
2144 "StartPlayingFile() failed to start "
2145 "file playout");
2146 _inputFilePlayerPtr->StopPlayingFile();
2147 FilePlayer::DestroyFilePlayer(_inputFilePlayerPtr);
2148 _inputFilePlayerPtr = NULL;
2149 return -1;
2150 }
andrew@webrtc.orgae1a58b2013-01-22 04:44:30 +00002151
niklase@google.com470e71d2011-07-07 08:21:25 +00002152 _inputFilePlayerPtr->RegisterModuleFileCallback(this);
henrika@webrtc.org944cbeb2014-03-18 10:32:33 +00002153 channel_state_.SetInputFilePlaying(true);
niklase@google.com470e71d2011-07-07 08:21:25 +00002154
2155 return 0;
2156}
2157
2158int Channel::StopPlayingFileAsMicrophone()
2159{
2160 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId,_channelId),
2161 "Channel::StopPlayingFileAsMicrophone()");
2162
henrika@webrtc.org944cbeb2014-03-18 10:32:33 +00002163 CriticalSectionScoped cs(&_fileCritSect);
2164
2165 if (!channel_state_.Get().input_file_playing)
niklase@google.com470e71d2011-07-07 08:21:25 +00002166 {
2167 _engineStatisticsPtr->SetLastError(
2168 VE_INVALID_OPERATION, kTraceWarning,
2169 "StopPlayingFileAsMicrophone() isnot playing");
2170 return 0;
2171 }
2172
niklase@google.com470e71d2011-07-07 08:21:25 +00002173 if (_inputFilePlayerPtr->StopPlayingFile() != 0)
2174 {
2175 _engineStatisticsPtr->SetLastError(
2176 VE_STOP_RECORDING_FAILED, kTraceError,
2177 "StopPlayingFile() could not stop playing");
2178 return -1;
2179 }
2180 _inputFilePlayerPtr->RegisterModuleFileCallback(NULL);
2181 FilePlayer::DestroyFilePlayer(_inputFilePlayerPtr);
2182 _inputFilePlayerPtr = NULL;
henrika@webrtc.org944cbeb2014-03-18 10:32:33 +00002183 channel_state_.SetInputFilePlaying(false);
niklase@google.com470e71d2011-07-07 08:21:25 +00002184
2185 return 0;
2186}
2187
2188int Channel::IsPlayingFileAsMicrophone() const
2189{
2190 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId,_channelId),
2191 "Channel::IsPlayingFileAsMicrophone()");
henrika@webrtc.org944cbeb2014-03-18 10:32:33 +00002192 return channel_state_.Get().input_file_playing;
niklase@google.com470e71d2011-07-07 08:21:25 +00002193}
2194
leozwang@webrtc.org813e4b02012-03-01 18:34:25 +00002195int Channel::StartRecordingPlayout(const char* fileName,
niklase@google.com470e71d2011-07-07 08:21:25 +00002196 const CodecInst* codecInst)
2197{
2198 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId,_channelId),
2199 "Channel::StartRecordingPlayout(fileName=%s)", fileName);
2200
2201 if (_outputFileRecording)
2202 {
2203 WEBRTC_TRACE(kTraceWarning, kTraceVoice, VoEId(_instanceId,-1),
2204 "StartRecordingPlayout() is already recording");
2205 return 0;
2206 }
2207
2208 FileFormats format;
pbos@webrtc.org6141e132013-04-09 10:09:10 +00002209 const uint32_t notificationTime(0); // Not supported in VoE
niklase@google.com470e71d2011-07-07 08:21:25 +00002210 CodecInst dummyCodec={100,"L16",16000,320,1,320000};
2211
niklas.enbom@webrtc.org40197d72012-03-26 08:45:47 +00002212 if ((codecInst != NULL) &&
2213 ((codecInst->channels < 1) || (codecInst->channels > 2)))
niklase@google.com470e71d2011-07-07 08:21:25 +00002214 {
2215 _engineStatisticsPtr->SetLastError(
2216 VE_BAD_ARGUMENT, kTraceError,
2217 "StartRecordingPlayout() invalid compression");
2218 return(-1);
2219 }
2220 if(codecInst == NULL)
2221 {
2222 format = kFileFormatPcm16kHzFile;
2223 codecInst=&dummyCodec;
2224 }
2225 else if((STR_CASE_CMP(codecInst->plname,"L16") == 0) ||
2226 (STR_CASE_CMP(codecInst->plname,"PCMU") == 0) ||
2227 (STR_CASE_CMP(codecInst->plname,"PCMA") == 0))
2228 {
2229 format = kFileFormatWavFile;
2230 }
2231 else
2232 {
2233 format = kFileFormatCompressedFile;
2234 }
2235
mflodman@webrtc.org9a065d12012-03-07 08:12:21 +00002236 CriticalSectionScoped cs(&_fileCritSect);
niklase@google.com470e71d2011-07-07 08:21:25 +00002237
2238 // Destroy the old instance
2239 if (_outputFileRecorderPtr)
2240 {
2241 _outputFileRecorderPtr->RegisterModuleFileCallback(NULL);
2242 FileRecorder::DestroyFileRecorder(_outputFileRecorderPtr);
2243 _outputFileRecorderPtr = NULL;
2244 }
2245
2246 _outputFileRecorderPtr = FileRecorder::CreateFileRecorder(
2247 _outputFileRecorderId, (const FileFormats)format);
2248 if (_outputFileRecorderPtr == NULL)
2249 {
2250 _engineStatisticsPtr->SetLastError(
2251 VE_INVALID_ARGUMENT, kTraceError,
2252 "StartRecordingPlayout() fileRecorder format isnot correct");
2253 return -1;
2254 }
2255
2256 if (_outputFileRecorderPtr->StartRecordingAudioFile(
2257 fileName, (const CodecInst&)*codecInst, notificationTime) != 0)
2258 {
2259 _engineStatisticsPtr->SetLastError(
2260 VE_BAD_FILE, kTraceError,
2261 "StartRecordingAudioFile() failed to start file recording");
2262 _outputFileRecorderPtr->StopRecording();
2263 FileRecorder::DestroyFileRecorder(_outputFileRecorderPtr);
2264 _outputFileRecorderPtr = NULL;
2265 return -1;
2266 }
2267 _outputFileRecorderPtr->RegisterModuleFileCallback(this);
2268 _outputFileRecording = true;
2269
2270 return 0;
2271}
2272
2273int Channel::StartRecordingPlayout(OutStream* stream,
2274 const CodecInst* codecInst)
2275{
2276 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId,_channelId),
2277 "Channel::StartRecordingPlayout()");
2278
2279 if (_outputFileRecording)
2280 {
2281 WEBRTC_TRACE(kTraceWarning, kTraceVoice, VoEId(_instanceId,-1),
2282 "StartRecordingPlayout() is already recording");
2283 return 0;
2284 }
2285
2286 FileFormats format;
pbos@webrtc.org6141e132013-04-09 10:09:10 +00002287 const uint32_t notificationTime(0); // Not supported in VoE
niklase@google.com470e71d2011-07-07 08:21:25 +00002288 CodecInst dummyCodec={100,"L16",16000,320,1,320000};
2289
2290 if (codecInst != NULL && codecInst->channels != 1)
2291 {
2292 _engineStatisticsPtr->SetLastError(
2293 VE_BAD_ARGUMENT, kTraceError,
2294 "StartRecordingPlayout() invalid compression");
2295 return(-1);
2296 }
2297 if(codecInst == NULL)
2298 {
2299 format = kFileFormatPcm16kHzFile;
2300 codecInst=&dummyCodec;
2301 }
2302 else if((STR_CASE_CMP(codecInst->plname,"L16") == 0) ||
2303 (STR_CASE_CMP(codecInst->plname,"PCMU") == 0) ||
2304 (STR_CASE_CMP(codecInst->plname,"PCMA") == 0))
2305 {
2306 format = kFileFormatWavFile;
2307 }
2308 else
2309 {
2310 format = kFileFormatCompressedFile;
2311 }
2312
mflodman@webrtc.org9a065d12012-03-07 08:12:21 +00002313 CriticalSectionScoped cs(&_fileCritSect);
niklase@google.com470e71d2011-07-07 08:21:25 +00002314
2315 // Destroy the old instance
2316 if (_outputFileRecorderPtr)
2317 {
2318 _outputFileRecorderPtr->RegisterModuleFileCallback(NULL);
2319 FileRecorder::DestroyFileRecorder(_outputFileRecorderPtr);
2320 _outputFileRecorderPtr = NULL;
2321 }
2322
2323 _outputFileRecorderPtr = FileRecorder::CreateFileRecorder(
2324 _outputFileRecorderId, (const FileFormats)format);
2325 if (_outputFileRecorderPtr == NULL)
2326 {
2327 _engineStatisticsPtr->SetLastError(
2328 VE_INVALID_ARGUMENT, kTraceError,
2329 "StartRecordingPlayout() fileRecorder format isnot correct");
2330 return -1;
2331 }
2332
2333 if (_outputFileRecorderPtr->StartRecordingAudioFile(*stream, *codecInst,
2334 notificationTime) != 0)
2335 {
2336 _engineStatisticsPtr->SetLastError(VE_BAD_FILE, kTraceError,
2337 "StartRecordingPlayout() failed to "
2338 "start file recording");
2339 _outputFileRecorderPtr->StopRecording();
2340 FileRecorder::DestroyFileRecorder(_outputFileRecorderPtr);
2341 _outputFileRecorderPtr = NULL;
2342 return -1;
2343 }
andrew@webrtc.orgae1a58b2013-01-22 04:44:30 +00002344
niklase@google.com470e71d2011-07-07 08:21:25 +00002345 _outputFileRecorderPtr->RegisterModuleFileCallback(this);
2346 _outputFileRecording = true;
2347
2348 return 0;
2349}
2350
2351int Channel::StopRecordingPlayout()
2352{
2353 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId,-1),
2354 "Channel::StopRecordingPlayout()");
2355
2356 if (!_outputFileRecording)
2357 {
2358 WEBRTC_TRACE(kTraceError, kTraceVoice, VoEId(_instanceId,-1),
2359 "StopRecordingPlayout() isnot recording");
2360 return -1;
2361 }
2362
2363
mflodman@webrtc.org9a065d12012-03-07 08:12:21 +00002364 CriticalSectionScoped cs(&_fileCritSect);
niklase@google.com470e71d2011-07-07 08:21:25 +00002365
2366 if (_outputFileRecorderPtr->StopRecording() != 0)
2367 {
2368 _engineStatisticsPtr->SetLastError(
2369 VE_STOP_RECORDING_FAILED, kTraceError,
2370 "StopRecording() could not stop recording");
2371 return(-1);
2372 }
2373 _outputFileRecorderPtr->RegisterModuleFileCallback(NULL);
2374 FileRecorder::DestroyFileRecorder(_outputFileRecorderPtr);
2375 _outputFileRecorderPtr = NULL;
2376 _outputFileRecording = false;
2377
2378 return 0;
2379}
2380
2381void
2382Channel::SetMixWithMicStatus(bool mix)
2383{
henrika@webrtc.org944cbeb2014-03-18 10:32:33 +00002384 CriticalSectionScoped cs(&_fileCritSect);
niklase@google.com470e71d2011-07-07 08:21:25 +00002385 _mixFileWithMicrophone=mix;
2386}
2387
2388int
pbos@webrtc.org6141e132013-04-09 10:09:10 +00002389Channel::GetSpeechOutputLevel(uint32_t& level) const
niklase@google.com470e71d2011-07-07 08:21:25 +00002390{
pbos@webrtc.org6141e132013-04-09 10:09:10 +00002391 int8_t currentLevel = _outputAudioLevel.Level();
2392 level = static_cast<int32_t> (currentLevel);
niklase@google.com470e71d2011-07-07 08:21:25 +00002393 WEBRTC_TRACE(kTraceStateInfo, kTraceVoice,
2394 VoEId(_instanceId,_channelId),
2395 "GetSpeechOutputLevel() => level=%u", level);
2396 return 0;
2397}
2398
2399int
pbos@webrtc.org6141e132013-04-09 10:09:10 +00002400Channel::GetSpeechOutputLevelFullRange(uint32_t& level) const
niklase@google.com470e71d2011-07-07 08:21:25 +00002401{
pbos@webrtc.org6141e132013-04-09 10:09:10 +00002402 int16_t currentLevel = _outputAudioLevel.LevelFullRange();
2403 level = static_cast<int32_t> (currentLevel);
niklase@google.com470e71d2011-07-07 08:21:25 +00002404 WEBRTC_TRACE(kTraceStateInfo, kTraceVoice,
2405 VoEId(_instanceId,_channelId),
2406 "GetSpeechOutputLevelFullRange() => level=%u", level);
2407 return 0;
2408}
2409
2410int
2411Channel::SetMute(bool enable)
2412{
wu@webrtc.org63420662013-10-17 18:28:55 +00002413 CriticalSectionScoped cs(&volume_settings_critsect_);
niklase@google.com470e71d2011-07-07 08:21:25 +00002414 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId,_channelId),
2415 "Channel::SetMute(enable=%d)", enable);
2416 _mute = enable;
2417 return 0;
2418}
2419
2420bool
2421Channel::Mute() const
2422{
wu@webrtc.org63420662013-10-17 18:28:55 +00002423 CriticalSectionScoped cs(&volume_settings_critsect_);
niklase@google.com470e71d2011-07-07 08:21:25 +00002424 return _mute;
2425}
2426
2427int
2428Channel::SetOutputVolumePan(float left, float right)
2429{
wu@webrtc.org63420662013-10-17 18:28:55 +00002430 CriticalSectionScoped cs(&volume_settings_critsect_);
niklase@google.com470e71d2011-07-07 08:21:25 +00002431 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId,_channelId),
2432 "Channel::SetOutputVolumePan()");
2433 _panLeft = left;
2434 _panRight = right;
2435 return 0;
2436}
2437
2438int
2439Channel::GetOutputVolumePan(float& left, float& right) const
2440{
wu@webrtc.org63420662013-10-17 18:28:55 +00002441 CriticalSectionScoped cs(&volume_settings_critsect_);
niklase@google.com470e71d2011-07-07 08:21:25 +00002442 left = _panLeft;
2443 right = _panRight;
2444 WEBRTC_TRACE(kTraceStateInfo, kTraceVoice,
2445 VoEId(_instanceId,_channelId),
2446 "GetOutputVolumePan() => left=%3.2f, right=%3.2f", left, right);
2447 return 0;
2448}
2449
2450int
2451Channel::SetChannelOutputVolumeScaling(float scaling)
2452{
wu@webrtc.org63420662013-10-17 18:28:55 +00002453 CriticalSectionScoped cs(&volume_settings_critsect_);
niklase@google.com470e71d2011-07-07 08:21:25 +00002454 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId,_channelId),
2455 "Channel::SetChannelOutputVolumeScaling()");
2456 _outputGain = scaling;
2457 return 0;
2458}
2459
2460int
2461Channel::GetChannelOutputVolumeScaling(float& scaling) const
2462{
wu@webrtc.org63420662013-10-17 18:28:55 +00002463 CriticalSectionScoped cs(&volume_settings_critsect_);
niklase@google.com470e71d2011-07-07 08:21:25 +00002464 scaling = _outputGain;
2465 WEBRTC_TRACE(kTraceStateInfo, kTraceVoice,
2466 VoEId(_instanceId,_channelId),
2467 "GetChannelOutputVolumeScaling() => scaling=%3.2f", scaling);
2468 return 0;
2469}
2470
niklase@google.com470e71d2011-07-07 08:21:25 +00002471int Channel::SendTelephoneEventOutband(unsigned char eventCode,
wu@webrtc.org822fbd82013-08-15 23:38:54 +00002472 int lengthMs, int attenuationDb,
2473 bool playDtmfEvent)
niklase@google.com470e71d2011-07-07 08:21:25 +00002474{
2475 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId, _channelId),
2476 "Channel::SendTelephoneEventOutband(..., playDtmfEvent=%d)",
2477 playDtmfEvent);
2478
2479 _playOutbandDtmfEvent = playDtmfEvent;
2480
pwestin@webrtc.org2853dde2012-05-11 11:08:54 +00002481 if (_rtpRtcpModule->SendTelephoneEventOutband(eventCode, lengthMs,
niklase@google.com470e71d2011-07-07 08:21:25 +00002482 attenuationDb) != 0)
2483 {
2484 _engineStatisticsPtr->SetLastError(
2485 VE_SEND_DTMF_FAILED,
2486 kTraceWarning,
2487 "SendTelephoneEventOutband() failed to send event");
2488 return -1;
2489 }
2490 return 0;
2491}
2492
2493int Channel::SendTelephoneEventInband(unsigned char eventCode,
2494 int lengthMs,
2495 int attenuationDb,
2496 bool playDtmfEvent)
2497{
2498 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId, _channelId),
2499 "Channel::SendTelephoneEventInband(..., playDtmfEvent=%d)",
2500 playDtmfEvent);
2501
2502 _playInbandDtmfEvent = playDtmfEvent;
2503 _inbandDtmfQueue.AddDtmf(eventCode, lengthMs, attenuationDb);
2504
2505 return 0;
2506}
2507
2508int
niklase@google.com470e71d2011-07-07 08:21:25 +00002509Channel::SetSendTelephoneEventPayloadType(unsigned char type)
2510{
2511 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId,_channelId),
2512 "Channel::SetSendTelephoneEventPayloadType()");
andrew@webrtc.orgf81f9f82011-08-19 22:56:22 +00002513 if (type > 127)
niklase@google.com470e71d2011-07-07 08:21:25 +00002514 {
2515 _engineStatisticsPtr->SetLastError(
2516 VE_INVALID_ARGUMENT, kTraceError,
2517 "SetSendTelephoneEventPayloadType() invalid type");
2518 return -1;
2519 }
pbos@webrtc.org5b10d8f2013-07-11 15:50:07 +00002520 CodecInst codec = {};
pwestin@webrtc.org1da1ce02011-10-13 15:19:55 +00002521 codec.plfreq = 8000;
2522 codec.pltype = type;
2523 memcpy(codec.plname, "telephone-event", 16);
pwestin@webrtc.org2853dde2012-05-11 11:08:54 +00002524 if (_rtpRtcpModule->RegisterSendPayload(codec) != 0)
niklase@google.com470e71d2011-07-07 08:21:25 +00002525 {
henrika@webrtc.org4392d5f2013-04-17 07:34:25 +00002526 _rtpRtcpModule->DeRegisterSendPayload(codec.pltype);
2527 if (_rtpRtcpModule->RegisterSendPayload(codec) != 0) {
2528 _engineStatisticsPtr->SetLastError(
2529 VE_RTP_RTCP_MODULE_ERROR, kTraceError,
2530 "SetSendTelephoneEventPayloadType() failed to register send"
2531 "payload type");
2532 return -1;
2533 }
niklase@google.com470e71d2011-07-07 08:21:25 +00002534 }
2535 _sendTelephoneEventPayloadType = type;
2536 return 0;
2537}
2538
2539int
2540Channel::GetSendTelephoneEventPayloadType(unsigned char& type)
2541{
2542 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId,_channelId),
2543 "Channel::GetSendTelephoneEventPayloadType()");
2544 type = _sendTelephoneEventPayloadType;
2545 WEBRTC_TRACE(kTraceStateInfo, kTraceVoice,
2546 VoEId(_instanceId,_channelId),
2547 "GetSendTelephoneEventPayloadType() => type=%u", type);
2548 return 0;
2549}
2550
niklase@google.com470e71d2011-07-07 08:21:25 +00002551int
2552Channel::UpdateRxVadDetection(AudioFrame& audioFrame)
2553{
2554 WEBRTC_TRACE(kTraceStream, kTraceVoice, VoEId(_instanceId,_channelId),
2555 "Channel::UpdateRxVadDetection()");
2556
2557 int vadDecision = 1;
2558
andrew@webrtc.org63a50982012-05-02 23:56:37 +00002559 vadDecision = (audioFrame.vad_activity_ == AudioFrame::kVadActive)? 1 : 0;
niklase@google.com470e71d2011-07-07 08:21:25 +00002560
2561 if ((vadDecision != _oldVadDecision) && _rxVadObserverPtr)
2562 {
2563 OnRxVadDetected(vadDecision);
2564 _oldVadDecision = vadDecision;
2565 }
2566
2567 WEBRTC_TRACE(kTraceStream, kTraceVoice, VoEId(_instanceId,_channelId),
2568 "Channel::UpdateRxVadDetection() => vadDecision=%d",
2569 vadDecision);
2570 return 0;
2571}
2572
2573int
2574Channel::RegisterRxVadObserver(VoERxVadCallback &observer)
2575{
2576 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId,_channelId),
2577 "Channel::RegisterRxVadObserver()");
mflodman@webrtc.org9a065d12012-03-07 08:12:21 +00002578 CriticalSectionScoped cs(&_callbackCritSect);
niklase@google.com470e71d2011-07-07 08:21:25 +00002579
2580 if (_rxVadObserverPtr)
2581 {
2582 _engineStatisticsPtr->SetLastError(
2583 VE_INVALID_OPERATION, kTraceError,
2584 "RegisterRxVadObserver() observer already enabled");
2585 return -1;
2586 }
niklase@google.com470e71d2011-07-07 08:21:25 +00002587 _rxVadObserverPtr = &observer;
2588 _RxVadDetection = true;
2589 return 0;
2590}
2591
2592int
2593Channel::DeRegisterRxVadObserver()
2594{
2595 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId,_channelId),
2596 "Channel::DeRegisterRxVadObserver()");
mflodman@webrtc.org9a065d12012-03-07 08:12:21 +00002597 CriticalSectionScoped cs(&_callbackCritSect);
niklase@google.com470e71d2011-07-07 08:21:25 +00002598
2599 if (!_rxVadObserverPtr)
2600 {
2601 _engineStatisticsPtr->SetLastError(
2602 VE_INVALID_OPERATION, kTraceWarning,
2603 "DeRegisterRxVadObserver() observer already disabled");
2604 return 0;
2605 }
2606 _rxVadObserverPtr = NULL;
2607 _RxVadDetection = false;
2608 return 0;
2609}
2610
2611int
2612Channel::VoiceActivityIndicator(int &activity)
2613{
2614 activity = _sendFrameType;
2615
2616 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId,_channelId),
andrew@webrtc.org6c264cc2013-10-04 17:54:09 +00002617 "Channel::VoiceActivityIndicator(indicator=%d)", activity);
niklase@google.com470e71d2011-07-07 08:21:25 +00002618 return 0;
2619}
2620
2621#ifdef WEBRTC_VOICE_ENGINE_AGC
2622
2623int
pbos@webrtc.org92135212013-05-14 08:31:39 +00002624Channel::SetRxAgcStatus(bool enable, AgcModes mode)
niklase@google.com470e71d2011-07-07 08:21:25 +00002625{
2626 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId,_channelId),
2627 "Channel::SetRxAgcStatus(enable=%d, mode=%d)",
2628 (int)enable, (int)mode);
2629
andrew@webrtc.org6c264cc2013-10-04 17:54:09 +00002630 GainControl::Mode agcMode = kDefaultRxAgcMode;
niklase@google.com470e71d2011-07-07 08:21:25 +00002631 switch (mode)
2632 {
2633 case kAgcDefault:
niklase@google.com470e71d2011-07-07 08:21:25 +00002634 break;
2635 case kAgcUnchanged:
andrew@webrtc.orgf3930e92013-09-18 22:37:32 +00002636 agcMode = rx_audioproc_->gain_control()->mode();
niklase@google.com470e71d2011-07-07 08:21:25 +00002637 break;
2638 case kAgcFixedDigital:
2639 agcMode = GainControl::kFixedDigital;
2640 break;
2641 case kAgcAdaptiveDigital:
2642 agcMode =GainControl::kAdaptiveDigital;
2643 break;
2644 default:
2645 _engineStatisticsPtr->SetLastError(
2646 VE_INVALID_ARGUMENT, kTraceError,
2647 "SetRxAgcStatus() invalid Agc mode");
2648 return -1;
2649 }
2650
andrew@webrtc.orgf3930e92013-09-18 22:37:32 +00002651 if (rx_audioproc_->gain_control()->set_mode(agcMode) != 0)
niklase@google.com470e71d2011-07-07 08:21:25 +00002652 {
2653 _engineStatisticsPtr->SetLastError(
2654 VE_APM_ERROR, kTraceError,
2655 "SetRxAgcStatus() failed to set Agc mode");
2656 return -1;
2657 }
andrew@webrtc.orgf3930e92013-09-18 22:37:32 +00002658 if (rx_audioproc_->gain_control()->Enable(enable) != 0)
niklase@google.com470e71d2011-07-07 08:21:25 +00002659 {
2660 _engineStatisticsPtr->SetLastError(
2661 VE_APM_ERROR, kTraceError,
2662 "SetRxAgcStatus() failed to set Agc state");
2663 return -1;
2664 }
2665
2666 _rxAgcIsEnabled = enable;
henrika@webrtc.org944cbeb2014-03-18 10:32:33 +00002667 channel_state_.SetRxApmIsEnabled(_rxAgcIsEnabled || _rxNsIsEnabled);
niklase@google.com470e71d2011-07-07 08:21:25 +00002668
2669 return 0;
2670}
2671
2672int
2673Channel::GetRxAgcStatus(bool& enabled, AgcModes& mode)
2674{
2675 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId,_channelId),
2676 "Channel::GetRxAgcStatus(enable=?, mode=?)");
2677
andrew@webrtc.orgf3930e92013-09-18 22:37:32 +00002678 bool enable = rx_audioproc_->gain_control()->is_enabled();
niklase@google.com470e71d2011-07-07 08:21:25 +00002679 GainControl::Mode agcMode =
andrew@webrtc.orgf3930e92013-09-18 22:37:32 +00002680 rx_audioproc_->gain_control()->mode();
niklase@google.com470e71d2011-07-07 08:21:25 +00002681
2682 enabled = enable;
2683
2684 switch (agcMode)
2685 {
2686 case GainControl::kFixedDigital:
2687 mode = kAgcFixedDigital;
2688 break;
2689 case GainControl::kAdaptiveDigital:
2690 mode = kAgcAdaptiveDigital;
2691 break;
2692 default:
2693 _engineStatisticsPtr->SetLastError(
2694 VE_APM_ERROR, kTraceError,
2695 "GetRxAgcStatus() invalid Agc mode");
2696 return -1;
2697 }
2698
2699 return 0;
2700}
2701
2702int
pbos@webrtc.org92135212013-05-14 08:31:39 +00002703Channel::SetRxAgcConfig(AgcConfig config)
niklase@google.com470e71d2011-07-07 08:21:25 +00002704{
2705 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId,_channelId),
2706 "Channel::SetRxAgcConfig()");
2707
andrew@webrtc.orgf3930e92013-09-18 22:37:32 +00002708 if (rx_audioproc_->gain_control()->set_target_level_dbfs(
niklase@google.com470e71d2011-07-07 08:21:25 +00002709 config.targetLeveldBOv) != 0)
2710 {
2711 _engineStatisticsPtr->SetLastError(
2712 VE_APM_ERROR, kTraceError,
2713 "SetRxAgcConfig() failed to set target peak |level|"
2714 "(or envelope) of the Agc");
2715 return -1;
2716 }
andrew@webrtc.orgf3930e92013-09-18 22:37:32 +00002717 if (rx_audioproc_->gain_control()->set_compression_gain_db(
niklase@google.com470e71d2011-07-07 08:21:25 +00002718 config.digitalCompressionGaindB) != 0)
2719 {
2720 _engineStatisticsPtr->SetLastError(
2721 VE_APM_ERROR, kTraceError,
2722 "SetRxAgcConfig() failed to set the range in |gain| the"
2723 " digital compression stage may apply");
2724 return -1;
2725 }
andrew@webrtc.orgf3930e92013-09-18 22:37:32 +00002726 if (rx_audioproc_->gain_control()->enable_limiter(
niklase@google.com470e71d2011-07-07 08:21:25 +00002727 config.limiterEnable) != 0)
2728 {
2729 _engineStatisticsPtr->SetLastError(
2730 VE_APM_ERROR, kTraceError,
2731 "SetRxAgcConfig() failed to set hard limiter to the signal");
2732 return -1;
2733 }
2734
2735 return 0;
2736}
2737
2738int
2739Channel::GetRxAgcConfig(AgcConfig& config)
2740{
2741 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId,_channelId),
2742 "Channel::GetRxAgcConfig(config=%?)");
2743
2744 config.targetLeveldBOv =
andrew@webrtc.orgf3930e92013-09-18 22:37:32 +00002745 rx_audioproc_->gain_control()->target_level_dbfs();
niklase@google.com470e71d2011-07-07 08:21:25 +00002746 config.digitalCompressionGaindB =
andrew@webrtc.orgf3930e92013-09-18 22:37:32 +00002747 rx_audioproc_->gain_control()->compression_gain_db();
niklase@google.com470e71d2011-07-07 08:21:25 +00002748 config.limiterEnable =
andrew@webrtc.orgf3930e92013-09-18 22:37:32 +00002749 rx_audioproc_->gain_control()->is_limiter_enabled();
niklase@google.com470e71d2011-07-07 08:21:25 +00002750
2751 WEBRTC_TRACE(kTraceStateInfo, kTraceVoice,
2752 VoEId(_instanceId,_channelId), "GetRxAgcConfig() => "
2753 "targetLeveldBOv=%u, digitalCompressionGaindB=%u,"
2754 " limiterEnable=%d",
2755 config.targetLeveldBOv,
2756 config.digitalCompressionGaindB,
2757 config.limiterEnable);
2758
2759 return 0;
2760}
2761
2762#endif // #ifdef WEBRTC_VOICE_ENGINE_AGC
2763
2764#ifdef WEBRTC_VOICE_ENGINE_NR
2765
2766int
pbos@webrtc.org92135212013-05-14 08:31:39 +00002767Channel::SetRxNsStatus(bool enable, NsModes mode)
niklase@google.com470e71d2011-07-07 08:21:25 +00002768{
2769 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId,_channelId),
2770 "Channel::SetRxNsStatus(enable=%d, mode=%d)",
2771 (int)enable, (int)mode);
2772
andrew@webrtc.org6c264cc2013-10-04 17:54:09 +00002773 NoiseSuppression::Level nsLevel = kDefaultNsMode;
niklase@google.com470e71d2011-07-07 08:21:25 +00002774 switch (mode)
2775 {
2776
2777 case kNsDefault:
niklase@google.com470e71d2011-07-07 08:21:25 +00002778 break;
2779 case kNsUnchanged:
andrew@webrtc.orgf3930e92013-09-18 22:37:32 +00002780 nsLevel = rx_audioproc_->noise_suppression()->level();
niklase@google.com470e71d2011-07-07 08:21:25 +00002781 break;
2782 case kNsConference:
2783 nsLevel = NoiseSuppression::kHigh;
2784 break;
2785 case kNsLowSuppression:
2786 nsLevel = NoiseSuppression::kLow;
2787 break;
2788 case kNsModerateSuppression:
2789 nsLevel = NoiseSuppression::kModerate;
2790 break;
2791 case kNsHighSuppression:
2792 nsLevel = NoiseSuppression::kHigh;
2793 break;
2794 case kNsVeryHighSuppression:
2795 nsLevel = NoiseSuppression::kVeryHigh;
2796 break;
niklase@google.com470e71d2011-07-07 08:21:25 +00002797 }
2798
andrew@webrtc.orgf3930e92013-09-18 22:37:32 +00002799 if (rx_audioproc_->noise_suppression()->set_level(nsLevel)
niklase@google.com470e71d2011-07-07 08:21:25 +00002800 != 0)
2801 {
2802 _engineStatisticsPtr->SetLastError(
2803 VE_APM_ERROR, kTraceError,
andrew@webrtc.org6c264cc2013-10-04 17:54:09 +00002804 "SetRxNsStatus() failed to set NS level");
niklase@google.com470e71d2011-07-07 08:21:25 +00002805 return -1;
2806 }
andrew@webrtc.orgf3930e92013-09-18 22:37:32 +00002807 if (rx_audioproc_->noise_suppression()->Enable(enable) != 0)
niklase@google.com470e71d2011-07-07 08:21:25 +00002808 {
2809 _engineStatisticsPtr->SetLastError(
2810 VE_APM_ERROR, kTraceError,
andrew@webrtc.org6c264cc2013-10-04 17:54:09 +00002811 "SetRxNsStatus() failed to set NS state");
niklase@google.com470e71d2011-07-07 08:21:25 +00002812 return -1;
2813 }
2814
2815 _rxNsIsEnabled = enable;
henrika@webrtc.org944cbeb2014-03-18 10:32:33 +00002816 channel_state_.SetRxApmIsEnabled(_rxAgcIsEnabled || _rxNsIsEnabled);
niklase@google.com470e71d2011-07-07 08:21:25 +00002817
2818 return 0;
2819}
2820
2821int
2822Channel::GetRxNsStatus(bool& enabled, NsModes& mode)
2823{
2824 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId,_channelId),
2825 "Channel::GetRxNsStatus(enable=?, mode=?)");
2826
2827 bool enable =
andrew@webrtc.orgf3930e92013-09-18 22:37:32 +00002828 rx_audioproc_->noise_suppression()->is_enabled();
niklase@google.com470e71d2011-07-07 08:21:25 +00002829 NoiseSuppression::Level ncLevel =
andrew@webrtc.orgf3930e92013-09-18 22:37:32 +00002830 rx_audioproc_->noise_suppression()->level();
niklase@google.com470e71d2011-07-07 08:21:25 +00002831
2832 enabled = enable;
2833
2834 switch (ncLevel)
2835 {
2836 case NoiseSuppression::kLow:
2837 mode = kNsLowSuppression;
2838 break;
2839 case NoiseSuppression::kModerate:
2840 mode = kNsModerateSuppression;
2841 break;
2842 case NoiseSuppression::kHigh:
2843 mode = kNsHighSuppression;
2844 break;
2845 case NoiseSuppression::kVeryHigh:
2846 mode = kNsVeryHighSuppression;
2847 break;
niklase@google.com470e71d2011-07-07 08:21:25 +00002848 }
2849
2850 WEBRTC_TRACE(kTraceStateInfo, kTraceVoice,
2851 VoEId(_instanceId,_channelId),
2852 "GetRxNsStatus() => enabled=%d, mode=%d", enabled, mode);
2853 return 0;
2854}
2855
2856#endif // #ifdef WEBRTC_VOICE_ENGINE_NR
2857
2858int
niklase@google.com470e71d2011-07-07 08:21:25 +00002859Channel::SetLocalSSRC(unsigned int ssrc)
2860{
2861 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId, _channelId),
2862 "Channel::SetLocalSSRC()");
henrika@webrtc.org944cbeb2014-03-18 10:32:33 +00002863 if (channel_state_.Get().sending)
niklase@google.com470e71d2011-07-07 08:21:25 +00002864 {
2865 _engineStatisticsPtr->SetLastError(
2866 VE_ALREADY_SENDING, kTraceError,
2867 "SetLocalSSRC() already sending");
2868 return -1;
2869 }
stefan@webrtc.orgef927552014-06-05 08:25:29 +00002870 _rtpRtcpModule->SetSSRC(ssrc);
niklase@google.com470e71d2011-07-07 08:21:25 +00002871 return 0;
2872}
2873
2874int
2875Channel::GetLocalSSRC(unsigned int& ssrc)
2876{
pwestin@webrtc.org2853dde2012-05-11 11:08:54 +00002877 ssrc = _rtpRtcpModule->SSRC();
niklase@google.com470e71d2011-07-07 08:21:25 +00002878 WEBRTC_TRACE(kTraceStateInfo, kTraceVoice,
2879 VoEId(_instanceId,_channelId),
2880 "GetLocalSSRC() => ssrc=%lu", ssrc);
2881 return 0;
2882}
2883
2884int
2885Channel::GetRemoteSSRC(unsigned int& ssrc)
2886{
wu@webrtc.org822fbd82013-08-15 23:38:54 +00002887 ssrc = rtp_receiver_->SSRC();
niklase@google.com470e71d2011-07-07 08:21:25 +00002888 WEBRTC_TRACE(kTraceStateInfo, kTraceVoice,
2889 VoEId(_instanceId,_channelId),
2890 "GetRemoteSSRC() => ssrc=%lu", ssrc);
2891 return 0;
2892}
2893
wu@webrtc.orgebdb0e32014-03-06 23:49:08 +00002894int Channel::SetSendAudioLevelIndicationStatus(bool enable, unsigned char id) {
andrew@webrtc.orgf3930e92013-09-18 22:37:32 +00002895 _includeAudioLevelIndication = enable;
wu@webrtc.orgebdb0e32014-03-06 23:49:08 +00002896 return SetSendRtpHeaderExtension(enable, kRtpExtensionAudioLevel, id);
niklase@google.com470e71d2011-07-07 08:21:25 +00002897}
andrew@webrtc.orgf3930e92013-09-18 22:37:32 +00002898
wu@webrtc.org93fd25c2014-04-24 20:33:08 +00002899int Channel::SetReceiveAudioLevelIndicationStatus(bool enable,
2900 unsigned char id) {
2901 rtp_header_parser_->DeregisterRtpHeaderExtension(
2902 kRtpExtensionAudioLevel);
2903 if (enable && !rtp_header_parser_->RegisterRtpHeaderExtension(
2904 kRtpExtensionAudioLevel, id)) {
2905 return -1;
2906 }
2907 return 0;
2908}
2909
wu@webrtc.orgebdb0e32014-03-06 23:49:08 +00002910int Channel::SetSendAbsoluteSenderTimeStatus(bool enable, unsigned char id) {
2911 return SetSendRtpHeaderExtension(enable, kRtpExtensionAbsoluteSendTime, id);
2912}
2913
2914int Channel::SetReceiveAbsoluteSenderTimeStatus(bool enable, unsigned char id) {
2915 rtp_header_parser_->DeregisterRtpHeaderExtension(
2916 kRtpExtensionAbsoluteSendTime);
solenberg@webrtc.orgb1f50102014-03-24 10:38:25 +00002917 if (enable && !rtp_header_parser_->RegisterRtpHeaderExtension(
2918 kRtpExtensionAbsoluteSendTime, id)) {
2919 return -1;
wu@webrtc.orgebdb0e32014-03-06 23:49:08 +00002920 }
2921 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +00002922}
2923
pbos@webrtc.orgd16e8392014-12-19 13:49:55 +00002924void Channel::SetRTCPStatus(bool enable) {
2925 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId, _channelId),
2926 "Channel::SetRTCPStatus()");
2927 _rtpRtcpModule->SetRTCPStatus(enable ? kRtcpCompound : kRtcpOff);
niklase@google.com470e71d2011-07-07 08:21:25 +00002928}
2929
2930int
2931Channel::GetRTCPStatus(bool& enabled)
2932{
pwestin@webrtc.org2853dde2012-05-11 11:08:54 +00002933 RTCPMethod method = _rtpRtcpModule->RTCP();
niklase@google.com470e71d2011-07-07 08:21:25 +00002934 enabled = (method != kRtcpOff);
2935 WEBRTC_TRACE(kTraceStateInfo, kTraceVoice,
2936 VoEId(_instanceId,_channelId),
2937 "GetRTCPStatus() => enabled=%d", enabled);
2938 return 0;
2939}
2940
2941int
2942Channel::SetRTCP_CNAME(const char cName[256])
2943{
2944 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId, _channelId),
2945 "Channel::SetRTCP_CNAME()");
pwestin@webrtc.org2853dde2012-05-11 11:08:54 +00002946 if (_rtpRtcpModule->SetCNAME(cName) != 0)
niklase@google.com470e71d2011-07-07 08:21:25 +00002947 {
2948 _engineStatisticsPtr->SetLastError(
2949 VE_RTP_RTCP_MODULE_ERROR, kTraceError,
2950 "SetRTCP_CNAME() failed to set RTCP CNAME");
2951 return -1;
2952 }
2953 return 0;
2954}
2955
2956int
niklase@google.com470e71d2011-07-07 08:21:25 +00002957Channel::GetRemoteRTCP_CNAME(char cName[256])
2958{
2959 if (cName == NULL)
2960 {
2961 _engineStatisticsPtr->SetLastError(
2962 VE_INVALID_ARGUMENT, kTraceError,
2963 "GetRemoteRTCP_CNAME() invalid CNAME input buffer");
2964 return -1;
2965 }
leozwang@webrtc.org813e4b02012-03-01 18:34:25 +00002966 char cname[RTCP_CNAME_SIZE];
wu@webrtc.org822fbd82013-08-15 23:38:54 +00002967 const uint32_t remoteSSRC = rtp_receiver_->SSRC();
pwestin@webrtc.org2853dde2012-05-11 11:08:54 +00002968 if (_rtpRtcpModule->RemoteCNAME(remoteSSRC, cname) != 0)
niklase@google.com470e71d2011-07-07 08:21:25 +00002969 {
2970 _engineStatisticsPtr->SetLastError(
2971 VE_CANNOT_RETRIEVE_CNAME, kTraceError,
2972 "GetRemoteRTCP_CNAME() failed to retrieve remote RTCP CNAME");
2973 return -1;
2974 }
2975 strcpy(cName, cname);
2976 WEBRTC_TRACE(kTraceStateInfo, kTraceVoice,
2977 VoEId(_instanceId, _channelId),
2978 "GetRemoteRTCP_CNAME() => cName=%s", cName);
2979 return 0;
2980}
2981
2982int
2983Channel::GetRemoteRTCPData(
2984 unsigned int& NTPHigh,
2985 unsigned int& NTPLow,
2986 unsigned int& timestamp,
2987 unsigned int& playoutTimestamp,
2988 unsigned int* jitter,
2989 unsigned short* fractionLost)
2990{
2991 // --- Information from sender info in received Sender Reports
2992
2993 RTCPSenderInfo senderInfo;
pwestin@webrtc.org2853dde2012-05-11 11:08:54 +00002994 if (_rtpRtcpModule->RemoteRTCPStat(&senderInfo) != 0)
niklase@google.com470e71d2011-07-07 08:21:25 +00002995 {
2996 _engineStatisticsPtr->SetLastError(
2997 VE_RTP_RTCP_MODULE_ERROR, kTraceError,
wu@webrtc.orgfcd12b32011-09-15 20:49:50 +00002998 "GetRemoteRTCPData() failed to retrieve sender info for remote "
niklase@google.com470e71d2011-07-07 08:21:25 +00002999 "side");
3000 return -1;
3001 }
3002
3003 // We only utilize 12 out of 20 bytes in the sender info (ignores packet
3004 // and octet count)
3005 NTPHigh = senderInfo.NTPseconds;
3006 NTPLow = senderInfo.NTPfraction;
3007 timestamp = senderInfo.RTPtimeStamp;
3008
3009 WEBRTC_TRACE(kTraceStateInfo, kTraceVoice,
3010 VoEId(_instanceId, _channelId),
3011 "GetRemoteRTCPData() => NTPHigh=%lu, NTPLow=%lu, "
3012 "timestamp=%lu",
3013 NTPHigh, NTPLow, timestamp);
3014
3015 // --- Locally derived information
3016
3017 // This value is updated on each incoming RTCP packet (0 when no packet
3018 // has been received)
pwestin@webrtc.org1de01352013-04-11 20:23:35 +00003019 playoutTimestamp = playout_timestamp_rtcp_;
niklase@google.com470e71d2011-07-07 08:21:25 +00003020
3021 WEBRTC_TRACE(kTraceStateInfo, kTraceVoice,
3022 VoEId(_instanceId, _channelId),
3023 "GetRemoteRTCPData() => playoutTimestamp=%lu",
pwestin@webrtc.org1de01352013-04-11 20:23:35 +00003024 playout_timestamp_rtcp_);
niklase@google.com470e71d2011-07-07 08:21:25 +00003025
3026 if (NULL != jitter || NULL != fractionLost)
3027 {
perkj@webrtc.orgce5990c2012-01-11 13:00:08 +00003028 // Get all RTCP receiver report blocks that have been received on this
3029 // channel. If we receive RTP packets from a remote source we know the
3030 // remote SSRC and use the report block from him.
3031 // Otherwise use the first report block.
3032 std::vector<RTCPReportBlock> remote_stats;
pwestin@webrtc.org2853dde2012-05-11 11:08:54 +00003033 if (_rtpRtcpModule->RemoteRTCPStat(&remote_stats) != 0 ||
perkj@webrtc.orgce5990c2012-01-11 13:00:08 +00003034 remote_stats.empty()) {
3035 WEBRTC_TRACE(kTraceWarning, kTraceVoice,
3036 VoEId(_instanceId, _channelId),
3037 "GetRemoteRTCPData() failed to measure statistics due"
3038 " to lack of received RTP and/or RTCP packets");
3039 return -1;
niklase@google.com470e71d2011-07-07 08:21:25 +00003040 }
perkj@webrtc.orgce5990c2012-01-11 13:00:08 +00003041
wu@webrtc.org822fbd82013-08-15 23:38:54 +00003042 uint32_t remoteSSRC = rtp_receiver_->SSRC();
perkj@webrtc.orgce5990c2012-01-11 13:00:08 +00003043 std::vector<RTCPReportBlock>::const_iterator it = remote_stats.begin();
3044 for (; it != remote_stats.end(); ++it) {
3045 if (it->remoteSSRC == remoteSSRC)
3046 break;
niklase@google.com470e71d2011-07-07 08:21:25 +00003047 }
perkj@webrtc.orgce5990c2012-01-11 13:00:08 +00003048
3049 if (it == remote_stats.end()) {
3050 // If we have not received any RTCP packets from this SSRC it probably
3051 // means that we have not received any RTP packets.
3052 // Use the first received report block instead.
3053 it = remote_stats.begin();
3054 remoteSSRC = it->remoteSSRC;
niklase@google.com470e71d2011-07-07 08:21:25 +00003055 }
perkj@webrtc.orgce5990c2012-01-11 13:00:08 +00003056
xians@webrtc.org79af7342012-01-31 12:22:14 +00003057 if (jitter) {
3058 *jitter = it->jitter;
3059 WEBRTC_TRACE(kTraceStateInfo, kTraceVoice,
3060 VoEId(_instanceId, _channelId),
3061 "GetRemoteRTCPData() => jitter = %lu", *jitter);
3062 }
perkj@webrtc.orgce5990c2012-01-11 13:00:08 +00003063
xians@webrtc.org79af7342012-01-31 12:22:14 +00003064 if (fractionLost) {
3065 *fractionLost = it->fractionLost;
3066 WEBRTC_TRACE(kTraceStateInfo, kTraceVoice,
3067 VoEId(_instanceId, _channelId),
3068 "GetRemoteRTCPData() => fractionLost = %lu",
3069 *fractionLost);
3070 }
niklase@google.com470e71d2011-07-07 08:21:25 +00003071 }
3072 return 0;
3073}
3074
3075int
pbos@webrtc.org92135212013-05-14 08:31:39 +00003076Channel::SendApplicationDefinedRTCPPacket(unsigned char subType,
niklase@google.com470e71d2011-07-07 08:21:25 +00003077 unsigned int name,
3078 const char* data,
3079 unsigned short dataLengthInBytes)
3080{
3081 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId, _channelId),
3082 "Channel::SendApplicationDefinedRTCPPacket()");
henrika@webrtc.org944cbeb2014-03-18 10:32:33 +00003083 if (!channel_state_.Get().sending)
niklase@google.com470e71d2011-07-07 08:21:25 +00003084 {
3085 _engineStatisticsPtr->SetLastError(
3086 VE_NOT_SENDING, kTraceError,
3087 "SendApplicationDefinedRTCPPacket() not sending");
3088 return -1;
3089 }
3090 if (NULL == data)
3091 {
3092 _engineStatisticsPtr->SetLastError(
3093 VE_INVALID_ARGUMENT, kTraceError,
3094 "SendApplicationDefinedRTCPPacket() invalid data value");
3095 return -1;
3096 }
3097 if (dataLengthInBytes % 4 != 0)
3098 {
3099 _engineStatisticsPtr->SetLastError(
3100 VE_INVALID_ARGUMENT, kTraceError,
3101 "SendApplicationDefinedRTCPPacket() invalid length value");
3102 return -1;
3103 }
pwestin@webrtc.org2853dde2012-05-11 11:08:54 +00003104 RTCPMethod status = _rtpRtcpModule->RTCP();
niklase@google.com470e71d2011-07-07 08:21:25 +00003105 if (status == kRtcpOff)
3106 {
3107 _engineStatisticsPtr->SetLastError(
3108 VE_RTCP_ERROR, kTraceError,
3109 "SendApplicationDefinedRTCPPacket() RTCP is disabled");
3110 return -1;
3111 }
3112
3113 // Create and schedule the RTCP APP packet for transmission
pwestin@webrtc.org2853dde2012-05-11 11:08:54 +00003114 if (_rtpRtcpModule->SetRTCPApplicationSpecificData(
niklase@google.com470e71d2011-07-07 08:21:25 +00003115 subType,
3116 name,
3117 (const unsigned char*) data,
3118 dataLengthInBytes) != 0)
3119 {
3120 _engineStatisticsPtr->SetLastError(
3121 VE_SEND_ERROR, kTraceError,
3122 "SendApplicationDefinedRTCPPacket() failed to send RTCP packet");
3123 return -1;
3124 }
3125 return 0;
3126}
3127
3128int
3129Channel::GetRTPStatistics(
3130 unsigned int& averageJitterMs,
3131 unsigned int& maxJitterMs,
3132 unsigned int& discardedPackets)
3133{
niklase@google.com470e71d2011-07-07 08:21:25 +00003134 // The jitter statistics is updated for each received RTP packet and is
3135 // based on received packets.
sprang@webrtc.org54ae4ff2013-12-19 13:26:02 +00003136 if (_rtpRtcpModule->RTCP() == kRtcpOff) {
3137 // If RTCP is off, there is no timed thread in the RTCP module regularly
3138 // generating new stats, trigger the update manually here instead.
3139 StreamStatistician* statistician =
3140 rtp_receive_statistics_->GetStatistician(rtp_receiver_->SSRC());
3141 if (statistician) {
3142 // Don't use returned statistics, use data from proxy instead so that
3143 // max jitter can be fetched atomically.
3144 RtcpStatistics s;
3145 statistician->GetStatistics(&s, true);
3146 }
niklase@google.com470e71d2011-07-07 08:21:25 +00003147 }
3148
sprang@webrtc.org54ae4ff2013-12-19 13:26:02 +00003149 ChannelStatistics stats = statistics_proxy_->GetStats();
andrew@webrtc.orgeb524d92013-09-23 23:02:24 +00003150 const int32_t playoutFrequency = audio_coding_->PlayoutFrequency();
sprang@webrtc.org54ae4ff2013-12-19 13:26:02 +00003151 if (playoutFrequency > 0) {
3152 // Scale RTP statistics given the current playout frequency
3153 maxJitterMs = stats.max_jitter / (playoutFrequency / 1000);
3154 averageJitterMs = stats.rtcp.jitter / (playoutFrequency / 1000);
niklase@google.com470e71d2011-07-07 08:21:25 +00003155 }
3156
3157 discardedPackets = _numberOfDiscardedPackets;
3158
3159 WEBRTC_TRACE(kTraceStateInfo, kTraceVoice,
3160 VoEId(_instanceId, _channelId),
3161 "GetRTPStatistics() => averageJitterMs = %lu, maxJitterMs = %lu,"
wu@webrtc.orgfcd12b32011-09-15 20:49:50 +00003162 " discardedPackets = %lu)",
niklase@google.com470e71d2011-07-07 08:21:25 +00003163 averageJitterMs, maxJitterMs, discardedPackets);
3164 return 0;
3165}
3166
henrika@webrtc.org8a2fc882012-08-22 08:53:55 +00003167int Channel::GetRemoteRTCPReportBlocks(
3168 std::vector<ReportBlock>* report_blocks) {
3169 if (report_blocks == NULL) {
3170 _engineStatisticsPtr->SetLastError(VE_INVALID_ARGUMENT, kTraceError,
3171 "GetRemoteRTCPReportBlock()s invalid report_blocks.");
3172 return -1;
3173 }
3174
3175 // Get the report blocks from the latest received RTCP Sender or Receiver
3176 // Report. Each element in the vector contains the sender's SSRC and a
3177 // report block according to RFC 3550.
3178 std::vector<RTCPReportBlock> rtcp_report_blocks;
3179 if (_rtpRtcpModule->RemoteRTCPStat(&rtcp_report_blocks) != 0) {
3180 _engineStatisticsPtr->SetLastError(VE_RTP_RTCP_MODULE_ERROR, kTraceError,
3181 "GetRemoteRTCPReportBlocks() failed to read RTCP SR/RR report block.");
3182 return -1;
3183 }
3184
3185 if (rtcp_report_blocks.empty())
3186 return 0;
3187
3188 std::vector<RTCPReportBlock>::const_iterator it = rtcp_report_blocks.begin();
3189 for (; it != rtcp_report_blocks.end(); ++it) {
3190 ReportBlock report_block;
3191 report_block.sender_SSRC = it->remoteSSRC;
3192 report_block.source_SSRC = it->sourceSSRC;
3193 report_block.fraction_lost = it->fractionLost;
3194 report_block.cumulative_num_packets_lost = it->cumulativeLost;
3195 report_block.extended_highest_sequence_number = it->extendedHighSeqNum;
3196 report_block.interarrival_jitter = it->jitter;
3197 report_block.last_SR_timestamp = it->lastSR;
3198 report_block.delay_since_last_SR = it->delaySinceLastSR;
3199 report_blocks->push_back(report_block);
3200 }
3201 return 0;
3202}
3203
niklase@google.com470e71d2011-07-07 08:21:25 +00003204int
3205Channel::GetRTPStatistics(CallStatistics& stats)
3206{
wu@webrtc.orgcb711f72014-05-19 17:39:11 +00003207 // --- RtcpStatistics
niklase@google.com470e71d2011-07-07 08:21:25 +00003208
3209 // The jitter statistics is updated for each received RTP packet and is
3210 // based on received packets.
sprang@webrtc.org54ae4ff2013-12-19 13:26:02 +00003211 RtcpStatistics statistics;
stefan@webrtc.org286fe0b2013-08-21 20:58:21 +00003212 StreamStatistician* statistician =
3213 rtp_receive_statistics_->GetStatistician(rtp_receiver_->SSRC());
3214 if (!statistician || !statistician->GetStatistics(
wu@webrtc.org822fbd82013-08-15 23:38:54 +00003215 &statistics, _rtpRtcpModule->RTCP() == kRtcpOff)) {
3216 _engineStatisticsPtr->SetLastError(
3217 VE_CANNOT_RETRIEVE_RTP_STAT, kTraceWarning,
3218 "GetRTPStatistics() failed to read RTP statistics from the "
3219 "RTP/RTCP module");
niklase@google.com470e71d2011-07-07 08:21:25 +00003220 }
3221
wu@webrtc.org822fbd82013-08-15 23:38:54 +00003222 stats.fractionLost = statistics.fraction_lost;
3223 stats.cumulativeLost = statistics.cumulative_lost;
3224 stats.extendedMax = statistics.extended_max_sequence_number;
3225 stats.jitterSamples = statistics.jitter;
niklase@google.com470e71d2011-07-07 08:21:25 +00003226
3227 WEBRTC_TRACE(kTraceStateInfo, kTraceVoice,
3228 VoEId(_instanceId, _channelId),
3229 "GetRTPStatistics() => fractionLost=%lu, cumulativeLost=%lu,"
wu@webrtc.orgfcd12b32011-09-15 20:49:50 +00003230 " extendedMax=%lu, jitterSamples=%li)",
niklase@google.com470e71d2011-07-07 08:21:25 +00003231 stats.fractionLost, stats.cumulativeLost, stats.extendedMax,
3232 stats.jitterSamples);
3233
wu@webrtc.orgcb711f72014-05-19 17:39:11 +00003234 // --- RTT
minyue@webrtc.org2b58a442014-09-11 07:51:53 +00003235 stats.rttMs = GetRTT();
minyue@webrtc.org6fd93082014-12-15 14:56:44 +00003236 if (stats.rttMs == 0) {
3237 WEBRTC_TRACE(kTraceWarning, kTraceVoice, VoEId(_instanceId, _channelId),
3238 "GetRTPStatistics() failed to get RTT");
3239 } else {
3240 WEBRTC_TRACE(kTraceStateInfo, kTraceVoice, VoEId(_instanceId, _channelId),
pkasting@chromium.org16825b12015-01-12 21:51:21 +00003241 "GetRTPStatistics() => rttMs=%" PRId64, stats.rttMs);
minyue@webrtc.org6fd93082014-12-15 14:56:44 +00003242 }
niklase@google.com470e71d2011-07-07 08:21:25 +00003243
wu@webrtc.orgcb711f72014-05-19 17:39:11 +00003244 // --- Data counters
niklase@google.com470e71d2011-07-07 08:21:25 +00003245
pkasting@chromium.org4591fbd2014-11-20 22:28:14 +00003246 size_t bytesSent(0);
pbos@webrtc.org6141e132013-04-09 10:09:10 +00003247 uint32_t packetsSent(0);
pkasting@chromium.org4591fbd2014-11-20 22:28:14 +00003248 size_t bytesReceived(0);
pbos@webrtc.org6141e132013-04-09 10:09:10 +00003249 uint32_t packetsReceived(0);
niklase@google.com470e71d2011-07-07 08:21:25 +00003250
stefan@webrtc.org286fe0b2013-08-21 20:58:21 +00003251 if (statistician) {
3252 statistician->GetDataCounters(&bytesReceived, &packetsReceived);
3253 }
wu@webrtc.org822fbd82013-08-15 23:38:54 +00003254
pwestin@webrtc.org2853dde2012-05-11 11:08:54 +00003255 if (_rtpRtcpModule->DataCountersRTP(&bytesSent,
wu@webrtc.org822fbd82013-08-15 23:38:54 +00003256 &packetsSent) != 0)
niklase@google.com470e71d2011-07-07 08:21:25 +00003257 {
3258 WEBRTC_TRACE(kTraceWarning, kTraceVoice,
3259 VoEId(_instanceId, _channelId),
3260 "GetRTPStatistics() failed to retrieve RTP datacounters =>"
wu@webrtc.orgfcd12b32011-09-15 20:49:50 +00003261 " output will not be complete");
niklase@google.com470e71d2011-07-07 08:21:25 +00003262 }
3263
3264 stats.bytesSent = bytesSent;
3265 stats.packetsSent = packetsSent;
3266 stats.bytesReceived = bytesReceived;
3267 stats.packetsReceived = packetsReceived;
3268
3269 WEBRTC_TRACE(kTraceStateInfo, kTraceVoice,
3270 VoEId(_instanceId, _channelId),
pkasting@chromium.org4591fbd2014-11-20 22:28:14 +00003271 "GetRTPStatistics() => bytesSent=%" PRIuS ", packetsSent=%d,"
3272 " bytesReceived=%" PRIuS ", packetsReceived=%d)",
niklase@google.com470e71d2011-07-07 08:21:25 +00003273 stats.bytesSent, stats.packetsSent, stats.bytesReceived,
3274 stats.packetsReceived);
3275
wu@webrtc.orgcb711f72014-05-19 17:39:11 +00003276 // --- Timestamps
3277 {
3278 CriticalSectionScoped lock(ts_stats_lock_.get());
3279 stats.capture_start_ntp_time_ms_ = capture_start_ntp_time_ms_;
3280 }
niklase@google.com470e71d2011-07-07 08:21:25 +00003281 return 0;
3282}
3283
minyue@webrtc.orgc1a40a72014-05-28 09:52:06 +00003284int Channel::SetREDStatus(bool enable, int redPayloadtype) {
turaj@webrtc.org42259e72012-12-11 02:15:12 +00003285 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId, _channelId),
minyue@webrtc.orgc1a40a72014-05-28 09:52:06 +00003286 "Channel::SetREDStatus()");
niklase@google.com470e71d2011-07-07 08:21:25 +00003287
turaj@webrtc.org8c8ad852013-01-31 18:20:17 +00003288 if (enable) {
3289 if (redPayloadtype < 0 || redPayloadtype > 127) {
3290 _engineStatisticsPtr->SetLastError(
3291 VE_PLTYPE_ERROR, kTraceError,
minyue@webrtc.orgc1a40a72014-05-28 09:52:06 +00003292 "SetREDStatus() invalid RED payload type");
turaj@webrtc.org8c8ad852013-01-31 18:20:17 +00003293 return -1;
3294 }
3295
3296 if (SetRedPayloadType(redPayloadtype) < 0) {
3297 _engineStatisticsPtr->SetLastError(
3298 VE_CODEC_ERROR, kTraceError,
3299 "SetSecondarySendCodec() Failed to register RED ACM");
3300 return -1;
3301 }
turaj@webrtc.org42259e72012-12-11 02:15:12 +00003302 }
niklase@google.com470e71d2011-07-07 08:21:25 +00003303
minyue@webrtc.orgaa5ea1c2014-05-23 15:16:51 +00003304 if (audio_coding_->SetREDStatus(enable) != 0) {
turaj@webrtc.org42259e72012-12-11 02:15:12 +00003305 _engineStatisticsPtr->SetLastError(
3306 VE_AUDIO_CODING_MODULE_ERROR, kTraceError,
minyue@webrtc.orgaa5ea1c2014-05-23 15:16:51 +00003307 "SetREDStatus() failed to set RED state in the ACM");
turaj@webrtc.org42259e72012-12-11 02:15:12 +00003308 return -1;
3309 }
3310 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +00003311}
3312
3313int
minyue@webrtc.orgc1a40a72014-05-28 09:52:06 +00003314Channel::GetREDStatus(bool& enabled, int& redPayloadtype)
niklase@google.com470e71d2011-07-07 08:21:25 +00003315{
minyue@webrtc.orgaa5ea1c2014-05-23 15:16:51 +00003316 enabled = audio_coding_->REDStatus();
niklase@google.com470e71d2011-07-07 08:21:25 +00003317 if (enabled)
3318 {
pbos@webrtc.org6141e132013-04-09 10:09:10 +00003319 int8_t payloadType(0);
pwestin@webrtc.org2853dde2012-05-11 11:08:54 +00003320 if (_rtpRtcpModule->SendREDPayloadType(payloadType) != 0)
niklase@google.com470e71d2011-07-07 08:21:25 +00003321 {
3322 _engineStatisticsPtr->SetLastError(
3323 VE_RTP_RTCP_MODULE_ERROR, kTraceError,
minyue@webrtc.orgc1a40a72014-05-28 09:52:06 +00003324 "GetREDStatus() failed to retrieve RED PT from RTP/RTCP "
niklase@google.com470e71d2011-07-07 08:21:25 +00003325 "module");
3326 return -1;
3327 }
pkasting@chromium.orgdf9a41d2015-01-26 22:35:29 +00003328 redPayloadtype = payloadType;
niklase@google.com470e71d2011-07-07 08:21:25 +00003329 WEBRTC_TRACE(kTraceStateInfo, kTraceVoice,
3330 VoEId(_instanceId, _channelId),
minyue@webrtc.orgc1a40a72014-05-28 09:52:06 +00003331 "GetREDStatus() => enabled=%d, redPayloadtype=%d",
niklase@google.com470e71d2011-07-07 08:21:25 +00003332 enabled, redPayloadtype);
3333 return 0;
3334 }
3335 WEBRTC_TRACE(kTraceStateInfo, kTraceVoice,
3336 VoEId(_instanceId, _channelId),
minyue@webrtc.orgc1a40a72014-05-28 09:52:06 +00003337 "GetREDStatus() => enabled=%d", enabled);
niklase@google.com470e71d2011-07-07 08:21:25 +00003338 return 0;
3339}
3340
minyue@webrtc.orgc1a40a72014-05-28 09:52:06 +00003341int Channel::SetCodecFECStatus(bool enable) {
3342 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId, _channelId),
3343 "Channel::SetCodecFECStatus()");
3344
3345 if (audio_coding_->SetCodecFEC(enable) != 0) {
3346 _engineStatisticsPtr->SetLastError(
3347 VE_AUDIO_CODING_MODULE_ERROR, kTraceError,
3348 "SetCodecFECStatus() failed to set FEC state");
3349 return -1;
3350 }
3351 return 0;
3352}
3353
3354bool Channel::GetCodecFECStatus() {
3355 bool enabled = audio_coding_->CodecFEC();
3356 WEBRTC_TRACE(kTraceStateInfo, kTraceVoice,
3357 VoEId(_instanceId, _channelId),
3358 "GetCodecFECStatus() => enabled=%d", enabled);
3359 return enabled;
3360}
3361
pwestin@webrtc.orgdb249952013-06-05 15:33:20 +00003362void Channel::SetNACKStatus(bool enable, int maxNumberOfPackets) {
3363 // None of these functions can fail.
3364 _rtpRtcpModule->SetStorePacketsStatus(enable, maxNumberOfPackets);
stefan@webrtc.org7bb8f022013-09-06 13:40:11 +00003365 rtp_receive_statistics_->SetMaxReorderingThreshold(maxNumberOfPackets);
3366 rtp_receiver_->SetNACKStatus(enable ? kNackRtcp : kNackOff);
pwestin@webrtc.orgd30859e2013-06-06 21:09:01 +00003367 if (enable)
andrew@webrtc.orgeb524d92013-09-23 23:02:24 +00003368 audio_coding_->EnableNack(maxNumberOfPackets);
pwestin@webrtc.orgd30859e2013-06-06 21:09:01 +00003369 else
andrew@webrtc.orgeb524d92013-09-23 23:02:24 +00003370 audio_coding_->DisableNack();
pwestin@webrtc.orgdb249952013-06-05 15:33:20 +00003371}
3372
pwestin@webrtc.orgd30859e2013-06-06 21:09:01 +00003373// Called when we are missing one or more packets.
3374int Channel::ResendPackets(const uint16_t* sequence_numbers, int length) {
pwestin@webrtc.orgdb249952013-06-05 15:33:20 +00003375 return _rtpRtcpModule->SendNACK(sequence_numbers, length);
3376}
3377
niklase@google.com470e71d2011-07-07 08:21:25 +00003378int
niklase@google.com470e71d2011-07-07 08:21:25 +00003379Channel::StartRTPDump(const char fileNameUTF8[1024],
3380 RTPDirections direction)
3381{
3382 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId, _channelId),
3383 "Channel::StartRTPDump()");
3384 if ((direction != kRtpIncoming) && (direction != kRtpOutgoing))
3385 {
3386 _engineStatisticsPtr->SetLastError(
3387 VE_INVALID_ARGUMENT, kTraceError,
3388 "StartRTPDump() invalid RTP direction");
3389 return -1;
3390 }
3391 RtpDump* rtpDumpPtr = (direction == kRtpIncoming) ?
3392 &_rtpDumpIn : &_rtpDumpOut;
3393 if (rtpDumpPtr == NULL)
3394 {
3395 assert(false);
3396 return -1;
3397 }
3398 if (rtpDumpPtr->IsActive())
3399 {
3400 rtpDumpPtr->Stop();
3401 }
3402 if (rtpDumpPtr->Start(fileNameUTF8) != 0)
3403 {
3404 _engineStatisticsPtr->SetLastError(
3405 VE_BAD_FILE, kTraceError,
3406 "StartRTPDump() failed to create file");
3407 return -1;
3408 }
3409 return 0;
3410}
3411
3412int
3413Channel::StopRTPDump(RTPDirections direction)
3414{
3415 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId, _channelId),
3416 "Channel::StopRTPDump()");
3417 if ((direction != kRtpIncoming) && (direction != kRtpOutgoing))
3418 {
3419 _engineStatisticsPtr->SetLastError(
3420 VE_INVALID_ARGUMENT, kTraceError,
3421 "StopRTPDump() invalid RTP direction");
3422 return -1;
3423 }
3424 RtpDump* rtpDumpPtr = (direction == kRtpIncoming) ?
3425 &_rtpDumpIn : &_rtpDumpOut;
3426 if (rtpDumpPtr == NULL)
3427 {
3428 assert(false);
3429 return -1;
3430 }
3431 if (!rtpDumpPtr->IsActive())
3432 {
3433 return 0;
3434 }
3435 return rtpDumpPtr->Stop();
3436}
3437
3438bool
3439Channel::RTPDumpIsActive(RTPDirections direction)
3440{
3441 if ((direction != kRtpIncoming) &&
3442 (direction != kRtpOutgoing))
3443 {
3444 _engineStatisticsPtr->SetLastError(
3445 VE_INVALID_ARGUMENT, kTraceError,
3446 "RTPDumpIsActive() invalid RTP direction");
3447 return false;
3448 }
3449 RtpDump* rtpDumpPtr = (direction == kRtpIncoming) ?
3450 &_rtpDumpIn : &_rtpDumpOut;
3451 return rtpDumpPtr->IsActive();
3452}
3453
solenberg@webrtc.orgb1f50102014-03-24 10:38:25 +00003454void Channel::SetVideoEngineBWETarget(ViENetwork* vie_network,
3455 int video_channel) {
3456 CriticalSectionScoped cs(&_callbackCritSect);
3457 if (vie_network_) {
3458 vie_network_->Release();
3459 vie_network_ = NULL;
3460 }
3461 video_channel_ = -1;
3462
3463 if (vie_network != NULL && video_channel != -1) {
3464 vie_network_ = vie_network;
3465 video_channel_ = video_channel;
3466 }
3467}
3468
pbos@webrtc.org6141e132013-04-09 10:09:10 +00003469uint32_t
andrew@webrtc.org755b04a2011-11-15 16:57:56 +00003470Channel::Demultiplex(const AudioFrame& audioFrame)
niklase@google.com470e71d2011-07-07 08:21:25 +00003471{
3472 WEBRTC_TRACE(kTraceStream, kTraceVoice, VoEId(_instanceId,_channelId),
andrew@webrtc.org755b04a2011-11-15 16:57:56 +00003473 "Channel::Demultiplex()");
andrew@webrtc.orgae1a58b2013-01-22 04:44:30 +00003474 _audioFrame.CopyFrom(audioFrame);
andrew@webrtc.org63a50982012-05-02 23:56:37 +00003475 _audioFrame.id_ = _channelId;
niklase@google.com470e71d2011-07-07 08:21:25 +00003476 return 0;
3477}
3478
xians@webrtc.org2f84afa2013-07-31 16:23:37 +00003479void Channel::Demultiplex(const int16_t* audio_data,
xians@webrtc.org8fff1f02013-07-31 16:27:42 +00003480 int sample_rate,
xians@webrtc.org2f84afa2013-07-31 16:23:37 +00003481 int number_of_frames,
xians@webrtc.org8fff1f02013-07-31 16:27:42 +00003482 int number_of_channels) {
xians@webrtc.org2f84afa2013-07-31 16:23:37 +00003483 CodecInst codec;
3484 GetSendCodec(codec);
xians@webrtc.org2f84afa2013-07-31 16:23:37 +00003485
andrew@webrtc.org40ee3d02014-04-03 21:56:01 +00003486 if (!mono_recording_audio_.get()) {
3487 // Temporary space for DownConvertToCodecFormat.
3488 mono_recording_audio_.reset(new int16_t[kMaxMonoDataSizeSamples]);
xians@webrtc.org2f84afa2013-07-31 16:23:37 +00003489 }
andrew@webrtc.org40ee3d02014-04-03 21:56:01 +00003490 DownConvertToCodecFormat(audio_data,
3491 number_of_frames,
3492 number_of_channels,
3493 sample_rate,
3494 codec.channels,
3495 codec.plfreq,
3496 mono_recording_audio_.get(),
3497 &input_resampler_,
3498 &_audioFrame);
xians@webrtc.org2f84afa2013-07-31 16:23:37 +00003499}
3500
pbos@webrtc.org6141e132013-04-09 10:09:10 +00003501uint32_t
xians@google.com0b0665a2011-08-08 08:18:44 +00003502Channel::PrepareEncodeAndSend(int mixingFrequency)
niklase@google.com470e71d2011-07-07 08:21:25 +00003503{
3504 WEBRTC_TRACE(kTraceStream, kTraceVoice, VoEId(_instanceId,_channelId),
3505 "Channel::PrepareEncodeAndSend()");
3506
andrew@webrtc.org63a50982012-05-02 23:56:37 +00003507 if (_audioFrame.samples_per_channel_ == 0)
niklase@google.com470e71d2011-07-07 08:21:25 +00003508 {
3509 WEBRTC_TRACE(kTraceWarning, kTraceVoice, VoEId(_instanceId,_channelId),
3510 "Channel::PrepareEncodeAndSend() invalid audio frame");
tommi@webrtc.orgeec6ecd2014-07-11 19:09:59 +00003511 return 0xFFFFFFFF;
niklase@google.com470e71d2011-07-07 08:21:25 +00003512 }
3513
henrika@webrtc.org944cbeb2014-03-18 10:32:33 +00003514 if (channel_state_.Get().input_file_playing)
niklase@google.com470e71d2011-07-07 08:21:25 +00003515 {
3516 MixOrReplaceAudioWithFile(mixingFrequency);
3517 }
3518
andrew@webrtc.org21299d42014-05-14 19:00:59 +00003519 bool is_muted = Mute(); // Cache locally as Mute() takes a lock.
3520 if (is_muted) {
3521 AudioFrameOperations::Mute(_audioFrame);
niklase@google.com470e71d2011-07-07 08:21:25 +00003522 }
3523
henrika@webrtc.org944cbeb2014-03-18 10:32:33 +00003524 if (channel_state_.Get().input_external_media)
niklase@google.com470e71d2011-07-07 08:21:25 +00003525 {
mflodman@webrtc.org9a065d12012-03-07 08:12:21 +00003526 CriticalSectionScoped cs(&_callbackCritSect);
andrew@webrtc.org63a50982012-05-02 23:56:37 +00003527 const bool isStereo = (_audioFrame.num_channels_ == 2);
niklase@google.com470e71d2011-07-07 08:21:25 +00003528 if (_inputExternalMediaCallbackPtr)
3529 {
3530 _inputExternalMediaCallbackPtr->Process(
3531 _channelId,
3532 kRecordingPerChannel,
pbos@webrtc.org6141e132013-04-09 10:09:10 +00003533 (int16_t*)_audioFrame.data_,
andrew@webrtc.org63a50982012-05-02 23:56:37 +00003534 _audioFrame.samples_per_channel_,
3535 _audioFrame.sample_rate_hz_,
niklase@google.com470e71d2011-07-07 08:21:25 +00003536 isStereo);
3537 }
3538 }
3539
3540 InsertInbandDtmfTone();
3541
andrew@webrtc.org60730cf2014-01-07 17:45:09 +00003542 if (_includeAudioLevelIndication) {
andrew@webrtc.org382c0c22014-05-05 18:22:21 +00003543 int length = _audioFrame.samples_per_channel_ * _audioFrame.num_channels_;
andrew@webrtc.org21299d42014-05-14 19:00:59 +00003544 if (is_muted) {
3545 rms_level_.ProcessMuted(length);
3546 } else {
3547 rms_level_.Process(_audioFrame.data_, length);
3548 }
andrew@webrtc.org755b04a2011-11-15 16:57:56 +00003549 }
3550
niklase@google.com470e71d2011-07-07 08:21:25 +00003551 return 0;
3552}
3553
pbos@webrtc.org6141e132013-04-09 10:09:10 +00003554uint32_t
niklase@google.com470e71d2011-07-07 08:21:25 +00003555Channel::EncodeAndSend()
3556{
3557 WEBRTC_TRACE(kTraceStream, kTraceVoice, VoEId(_instanceId,_channelId),
3558 "Channel::EncodeAndSend()");
3559
andrew@webrtc.org63a50982012-05-02 23:56:37 +00003560 assert(_audioFrame.num_channels_ <= 2);
3561 if (_audioFrame.samples_per_channel_ == 0)
niklase@google.com470e71d2011-07-07 08:21:25 +00003562 {
3563 WEBRTC_TRACE(kTraceWarning, kTraceVoice, VoEId(_instanceId,_channelId),
3564 "Channel::EncodeAndSend() invalid audio frame");
tommi@webrtc.orgeec6ecd2014-07-11 19:09:59 +00003565 return 0xFFFFFFFF;
niklase@google.com470e71d2011-07-07 08:21:25 +00003566 }
3567
andrew@webrtc.org63a50982012-05-02 23:56:37 +00003568 _audioFrame.id_ = _channelId;
niklase@google.com470e71d2011-07-07 08:21:25 +00003569
3570 // --- Add 10ms of raw (PCM) audio data to the encoder @ 32kHz.
3571
3572 // The ACM resamples internally.
andrew@webrtc.org63a50982012-05-02 23:56:37 +00003573 _audioFrame.timestamp_ = _timeStamp;
henrik.lundin@webrtc.orgf56c1622015-03-02 12:29:30 +00003574 // This call will trigger AudioPacketizationCallback::SendData if encoding
3575 // is done and payload is ready for packetization and transmission.
3576 // Otherwise, it will return without invoking the callback.
3577 if (audio_coding_->Add10MsData((AudioFrame&)_audioFrame) < 0)
niklase@google.com470e71d2011-07-07 08:21:25 +00003578 {
3579 WEBRTC_TRACE(kTraceError, kTraceVoice, VoEId(_instanceId,_channelId),
3580 "Channel::EncodeAndSend() ACM encoding failed");
tommi@webrtc.orgeec6ecd2014-07-11 19:09:59 +00003581 return 0xFFFFFFFF;
niklase@google.com470e71d2011-07-07 08:21:25 +00003582 }
3583
andrew@webrtc.org63a50982012-05-02 23:56:37 +00003584 _timeStamp += _audioFrame.samples_per_channel_;
henrik.lundin@webrtc.orgf56c1622015-03-02 12:29:30 +00003585 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +00003586}
3587
3588int Channel::RegisterExternalMediaProcessing(
3589 ProcessingTypes type,
3590 VoEMediaProcess& processObject)
3591{
3592 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId,_channelId),
3593 "Channel::RegisterExternalMediaProcessing()");
3594
mflodman@webrtc.org9a065d12012-03-07 08:12:21 +00003595 CriticalSectionScoped cs(&_callbackCritSect);
niklase@google.com470e71d2011-07-07 08:21:25 +00003596
3597 if (kPlaybackPerChannel == type)
3598 {
3599 if (_outputExternalMediaCallbackPtr)
3600 {
3601 _engineStatisticsPtr->SetLastError(
3602 VE_INVALID_OPERATION, kTraceError,
3603 "Channel::RegisterExternalMediaProcessing() "
3604 "output external media already enabled");
3605 return -1;
3606 }
3607 _outputExternalMediaCallbackPtr = &processObject;
3608 _outputExternalMedia = true;
3609 }
3610 else if (kRecordingPerChannel == type)
3611 {
3612 if (_inputExternalMediaCallbackPtr)
3613 {
3614 _engineStatisticsPtr->SetLastError(
3615 VE_INVALID_OPERATION, kTraceError,
3616 "Channel::RegisterExternalMediaProcessing() "
3617 "output external media already enabled");
3618 return -1;
3619 }
3620 _inputExternalMediaCallbackPtr = &processObject;
henrika@webrtc.org944cbeb2014-03-18 10:32:33 +00003621 channel_state_.SetInputExternalMedia(true);
niklase@google.com470e71d2011-07-07 08:21:25 +00003622 }
3623 return 0;
3624}
3625
3626int Channel::DeRegisterExternalMediaProcessing(ProcessingTypes type)
3627{
3628 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId,_channelId),
3629 "Channel::DeRegisterExternalMediaProcessing()");
3630
mflodman@webrtc.org9a065d12012-03-07 08:12:21 +00003631 CriticalSectionScoped cs(&_callbackCritSect);
niklase@google.com470e71d2011-07-07 08:21:25 +00003632
3633 if (kPlaybackPerChannel == type)
3634 {
3635 if (!_outputExternalMediaCallbackPtr)
3636 {
3637 _engineStatisticsPtr->SetLastError(
3638 VE_INVALID_OPERATION, kTraceWarning,
3639 "Channel::DeRegisterExternalMediaProcessing() "
3640 "output external media already disabled");
3641 return 0;
3642 }
3643 _outputExternalMedia = false;
3644 _outputExternalMediaCallbackPtr = NULL;
3645 }
3646 else if (kRecordingPerChannel == type)
3647 {
3648 if (!_inputExternalMediaCallbackPtr)
3649 {
3650 _engineStatisticsPtr->SetLastError(
3651 VE_INVALID_OPERATION, kTraceWarning,
3652 "Channel::DeRegisterExternalMediaProcessing() "
3653 "input external media already disabled");
3654 return 0;
3655 }
henrika@webrtc.org944cbeb2014-03-18 10:32:33 +00003656 channel_state_.SetInputExternalMedia(false);
niklase@google.com470e71d2011-07-07 08:21:25 +00003657 _inputExternalMediaCallbackPtr = NULL;
3658 }
3659
3660 return 0;
3661}
3662
roosa@google.com1b60ceb2012-12-12 23:00:29 +00003663int Channel::SetExternalMixing(bool enabled) {
3664 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId,_channelId),
3665 "Channel::SetExternalMixing(enabled=%d)", enabled);
3666
henrika@webrtc.org944cbeb2014-03-18 10:32:33 +00003667 if (channel_state_.Get().playing)
roosa@google.com1b60ceb2012-12-12 23:00:29 +00003668 {
3669 _engineStatisticsPtr->SetLastError(
3670 VE_INVALID_OPERATION, kTraceError,
3671 "Channel::SetExternalMixing() "
3672 "external mixing cannot be changed while playing.");
3673 return -1;
3674 }
3675
3676 _externalMixing = enabled;
3677
3678 return 0;
3679}
3680
niklase@google.com470e71d2011-07-07 08:21:25 +00003681int
niklase@google.com470e71d2011-07-07 08:21:25 +00003682Channel::GetNetworkStatistics(NetworkStatistics& stats)
3683{
3684 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId,_channelId),
3685 "Channel::GetNetworkStatistics()");
minyue@webrtc.orgc0bd7be2015-02-18 15:24:13 +00003686 return audio_coding_->GetNetworkStatistics(&stats);
niklase@google.com470e71d2011-07-07 08:21:25 +00003687}
3688
wu@webrtc.org24301a62013-12-13 19:17:43 +00003689void Channel::GetDecodingCallStatistics(AudioDecodingCallStats* stats) const {
3690 audio_coding_->GetDecodingCallStatistics(stats);
3691}
3692
pwestin@webrtc.org1de01352013-04-11 20:23:35 +00003693bool Channel::GetDelayEstimate(int* jitter_buffer_delay_ms,
3694 int* playout_buffer_delay_ms) const {
3695 if (_average_jitter_buffer_delay_us == 0) {
niklase@google.com470e71d2011-07-07 08:21:25 +00003696 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId,_channelId),
pwestin@webrtc.org1de01352013-04-11 20:23:35 +00003697 "Channel::GetDelayEstimate() no valid estimate.");
3698 return false;
3699 }
3700 *jitter_buffer_delay_ms = (_average_jitter_buffer_delay_us + 500) / 1000 +
3701 _recPacketDelayMs;
3702 *playout_buffer_delay_ms = playout_delay_ms_;
3703 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId,_channelId),
3704 "Channel::GetDelayEstimate()");
3705 return true;
niklase@google.com470e71d2011-07-07 08:21:25 +00003706}
3707
turaj@webrtc.org6388c3e2013-02-12 21:42:18 +00003708int Channel::SetInitialPlayoutDelay(int delay_ms)
3709{
3710 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId,_channelId),
3711 "Channel::SetInitialPlayoutDelay()");
3712 if ((delay_ms < kVoiceEngineMinMinPlayoutDelayMs) ||
3713 (delay_ms > kVoiceEngineMaxMinPlayoutDelayMs))
3714 {
3715 _engineStatisticsPtr->SetLastError(
3716 VE_INVALID_ARGUMENT, kTraceError,
3717 "SetInitialPlayoutDelay() invalid min delay");
3718 return -1;
3719 }
andrew@webrtc.orgeb524d92013-09-23 23:02:24 +00003720 if (audio_coding_->SetInitialPlayoutDelay(delay_ms) != 0)
turaj@webrtc.org6388c3e2013-02-12 21:42:18 +00003721 {
3722 _engineStatisticsPtr->SetLastError(
3723 VE_AUDIO_CODING_MODULE_ERROR, kTraceError,
3724 "SetInitialPlayoutDelay() failed to set min playout delay");
3725 return -1;
3726 }
3727 return 0;
3728}
3729
3730
niklase@google.com470e71d2011-07-07 08:21:25 +00003731int
3732Channel::SetMinimumPlayoutDelay(int delayMs)
3733{
3734 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId,_channelId),
3735 "Channel::SetMinimumPlayoutDelay()");
3736 if ((delayMs < kVoiceEngineMinMinPlayoutDelayMs) ||
3737 (delayMs > kVoiceEngineMaxMinPlayoutDelayMs))
3738 {
3739 _engineStatisticsPtr->SetLastError(
3740 VE_INVALID_ARGUMENT, kTraceError,
3741 "SetMinimumPlayoutDelay() invalid min delay");
3742 return -1;
3743 }
andrew@webrtc.orgeb524d92013-09-23 23:02:24 +00003744 if (audio_coding_->SetMinimumPlayoutDelay(delayMs) != 0)
niklase@google.com470e71d2011-07-07 08:21:25 +00003745 {
3746 _engineStatisticsPtr->SetLastError(
3747 VE_AUDIO_CODING_MODULE_ERROR, kTraceError,
3748 "SetMinimumPlayoutDelay() failed to set min playout delay");
3749 return -1;
3750 }
3751 return 0;
3752}
3753
pwestin@webrtc.org1de01352013-04-11 20:23:35 +00003754void Channel::UpdatePlayoutTimestamp(bool rtcp) {
3755 uint32_t playout_timestamp = 0;
3756
andrew@webrtc.orgeb524d92013-09-23 23:02:24 +00003757 if (audio_coding_->PlayoutTimestamp(&playout_timestamp) == -1) {
turaj@webrtc.org1ebd2e92014-07-25 17:50:10 +00003758 // This can happen if this channel has not been received any RTP packet. In
3759 // this case, NetEq is not capable of computing playout timestamp.
pwestin@webrtc.org1de01352013-04-11 20:23:35 +00003760 return;
3761 }
3762
3763 uint16_t delay_ms = 0;
3764 if (_audioDeviceModulePtr->PlayoutDelay(&delay_ms) == -1) {
3765 WEBRTC_TRACE(kTraceWarning, kTraceVoice, VoEId(_instanceId,_channelId),
3766 "Channel::UpdatePlayoutTimestamp() failed to read playout"
3767 " delay from the ADM");
3768 _engineStatisticsPtr->SetLastError(
3769 VE_CANNOT_RETRIEVE_VALUE, kTraceError,
3770 "UpdatePlayoutTimestamp() failed to retrieve playout delay");
3771 return;
3772 }
3773
turaj@webrtc.org167b6df2013-12-13 21:05:07 +00003774 jitter_buffer_playout_timestamp_ = playout_timestamp;
3775
pwestin@webrtc.org1de01352013-04-11 20:23:35 +00003776 // Remove the playout delay.
wu@webrtc.org94454b72014-06-05 20:34:08 +00003777 playout_timestamp -= (delay_ms * (GetPlayoutFrequency() / 1000));
pwestin@webrtc.org1de01352013-04-11 20:23:35 +00003778
3779 WEBRTC_TRACE(kTraceStream, kTraceVoice, VoEId(_instanceId,_channelId),
3780 "Channel::UpdatePlayoutTimestamp() => playoutTimestamp = %lu",
3781 playout_timestamp);
3782
3783 if (rtcp) {
3784 playout_timestamp_rtcp_ = playout_timestamp;
3785 } else {
3786 playout_timestamp_rtp_ = playout_timestamp;
3787 }
3788 playout_delay_ms_ = delay_ms;
3789}
3790
3791int Channel::GetPlayoutTimestamp(unsigned int& timestamp) {
3792 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId,_channelId),
3793 "Channel::GetPlayoutTimestamp()");
3794 if (playout_timestamp_rtp_ == 0) {
3795 _engineStatisticsPtr->SetLastError(
3796 VE_CANNOT_RETRIEVE_VALUE, kTraceError,
3797 "GetPlayoutTimestamp() failed to retrieve timestamp");
3798 return -1;
3799 }
3800 timestamp = playout_timestamp_rtp_;
3801 WEBRTC_TRACE(kTraceStateInfo, kTraceVoice,
3802 VoEId(_instanceId,_channelId),
3803 "GetPlayoutTimestamp() => timestamp=%u", timestamp);
3804 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +00003805}
3806
pbos@webrtc.orgd16e8392014-12-19 13:49:55 +00003807int Channel::SetInitTimestamp(unsigned int timestamp) {
3808 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId, _channelId),
niklase@google.com470e71d2011-07-07 08:21:25 +00003809 "Channel::SetInitTimestamp()");
pbos@webrtc.orgd16e8392014-12-19 13:49:55 +00003810 if (channel_state_.Get().sending) {
3811 _engineStatisticsPtr->SetLastError(VE_SENDING, kTraceError,
3812 "SetInitTimestamp() already sending");
3813 return -1;
3814 }
3815 _rtpRtcpModule->SetStartTimestamp(timestamp);
3816 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +00003817}
3818
pbos@webrtc.orgd16e8392014-12-19 13:49:55 +00003819int Channel::SetInitSequenceNumber(short sequenceNumber) {
3820 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId, _channelId),
3821 "Channel::SetInitSequenceNumber()");
3822 if (channel_state_.Get().sending) {
3823 _engineStatisticsPtr->SetLastError(
3824 VE_SENDING, kTraceError, "SetInitSequenceNumber() already sending");
3825 return -1;
3826 }
3827 _rtpRtcpModule->SetSequenceNumber(sequenceNumber);
3828 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +00003829}
3830
3831int
wu@webrtc.org822fbd82013-08-15 23:38:54 +00003832Channel::GetRtpRtcp(RtpRtcp** rtpRtcpModule, RtpReceiver** rtp_receiver) const
niklase@google.com470e71d2011-07-07 08:21:25 +00003833{
3834 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId,_channelId),
3835 "Channel::GetRtpRtcp()");
wu@webrtc.org822fbd82013-08-15 23:38:54 +00003836 *rtpRtcpModule = _rtpRtcpModule.get();
3837 *rtp_receiver = rtp_receiver_.get();
niklase@google.com470e71d2011-07-07 08:21:25 +00003838 return 0;
3839}
3840
andrew@webrtc.orge59a0ac2012-05-08 17:12:40 +00003841// TODO(andrew): refactor Mix functions here and in transmit_mixer.cc to use
3842// a shared helper.
pbos@webrtc.org6141e132013-04-09 10:09:10 +00003843int32_t
pbos@webrtc.org92135212013-05-14 08:31:39 +00003844Channel::MixOrReplaceAudioWithFile(int mixingFrequency)
niklase@google.com470e71d2011-07-07 08:21:25 +00003845{
kwiberg@webrtc.org00b8f6b2015-02-26 14:34:55 +00003846 rtc::scoped_ptr<int16_t[]> fileBuffer(new int16_t[640]);
andrew@webrtc.orge59a0ac2012-05-08 17:12:40 +00003847 int fileSamples(0);
niklase@google.com470e71d2011-07-07 08:21:25 +00003848
3849 {
mflodman@webrtc.org9a065d12012-03-07 08:12:21 +00003850 CriticalSectionScoped cs(&_fileCritSect);
niklase@google.com470e71d2011-07-07 08:21:25 +00003851
3852 if (_inputFilePlayerPtr == NULL)
3853 {
3854 WEBRTC_TRACE(kTraceWarning, kTraceVoice,
3855 VoEId(_instanceId, _channelId),
3856 "Channel::MixOrReplaceAudioWithFile() fileplayer"
3857 " doesnt exist");
3858 return -1;
3859 }
3860
braveyao@webrtc.orgd7131432012-03-29 10:39:44 +00003861 if (_inputFilePlayerPtr->Get10msAudioFromFile(fileBuffer.get(),
niklase@google.com470e71d2011-07-07 08:21:25 +00003862 fileSamples,
3863 mixingFrequency) == -1)
3864 {
3865 WEBRTC_TRACE(kTraceWarning, kTraceVoice,
3866 VoEId(_instanceId, _channelId),
3867 "Channel::MixOrReplaceAudioWithFile() file mixing "
3868 "failed");
3869 return -1;
3870 }
3871 if (fileSamples == 0)
3872 {
3873 WEBRTC_TRACE(kTraceWarning, kTraceVoice,
3874 VoEId(_instanceId, _channelId),
3875 "Channel::MixOrReplaceAudioWithFile() file is ended");
3876 return 0;
3877 }
3878 }
3879
andrew@webrtc.org63a50982012-05-02 23:56:37 +00003880 assert(_audioFrame.samples_per_channel_ == fileSamples);
niklase@google.com470e71d2011-07-07 08:21:25 +00003881
3882 if (_mixFileWithMicrophone)
3883 {
braveyao@webrtc.orgd7131432012-03-29 10:39:44 +00003884 // Currently file stream is always mono.
3885 // TODO(xians): Change the code when FilePlayer supports real stereo.
andrew@webrtc.org40ee3d02014-04-03 21:56:01 +00003886 MixWithSat(_audioFrame.data_,
3887 _audioFrame.num_channels_,
3888 fileBuffer.get(),
3889 1,
3890 fileSamples);
niklase@google.com470e71d2011-07-07 08:21:25 +00003891 }
3892 else
3893 {
braveyao@webrtc.orgd7131432012-03-29 10:39:44 +00003894 // Replace ACM audio with file.
3895 // Currently file stream is always mono.
3896 // TODO(xians): Change the code when FilePlayer supports real stereo.
niklase@google.com470e71d2011-07-07 08:21:25 +00003897 _audioFrame.UpdateFrame(_channelId,
tommi@webrtc.orgeec6ecd2014-07-11 19:09:59 +00003898 0xFFFFFFFF,
braveyao@webrtc.orgd7131432012-03-29 10:39:44 +00003899 fileBuffer.get(),
andrew@webrtc.orge59a0ac2012-05-08 17:12:40 +00003900 fileSamples,
niklase@google.com470e71d2011-07-07 08:21:25 +00003901 mixingFrequency,
3902 AudioFrame::kNormalSpeech,
3903 AudioFrame::kVadUnknown,
3904 1);
3905
3906 }
3907 return 0;
3908}
3909
pbos@webrtc.org6141e132013-04-09 10:09:10 +00003910int32_t
niklase@google.com470e71d2011-07-07 08:21:25 +00003911Channel::MixAudioWithFile(AudioFrame& audioFrame,
pbos@webrtc.org92135212013-05-14 08:31:39 +00003912 int mixingFrequency)
niklase@google.com470e71d2011-07-07 08:21:25 +00003913{
minyue@webrtc.org2a8df7c2014-08-06 10:05:19 +00003914 assert(mixingFrequency <= 48000);
niklase@google.com470e71d2011-07-07 08:21:25 +00003915
kwiberg@webrtc.org00b8f6b2015-02-26 14:34:55 +00003916 rtc::scoped_ptr<int16_t[]> fileBuffer(new int16_t[960]);
andrew@webrtc.orge59a0ac2012-05-08 17:12:40 +00003917 int fileSamples(0);
niklase@google.com470e71d2011-07-07 08:21:25 +00003918
3919 {
mflodman@webrtc.org9a065d12012-03-07 08:12:21 +00003920 CriticalSectionScoped cs(&_fileCritSect);
niklase@google.com470e71d2011-07-07 08:21:25 +00003921
3922 if (_outputFilePlayerPtr == NULL)
3923 {
3924 WEBRTC_TRACE(kTraceWarning, kTraceVoice,
3925 VoEId(_instanceId, _channelId),
3926 "Channel::MixAudioWithFile() file mixing failed");
3927 return -1;
3928 }
3929
3930 // We should get the frequency we ask for.
braveyao@webrtc.orgd7131432012-03-29 10:39:44 +00003931 if (_outputFilePlayerPtr->Get10msAudioFromFile(fileBuffer.get(),
niklase@google.com470e71d2011-07-07 08:21:25 +00003932 fileSamples,
3933 mixingFrequency) == -1)
3934 {
3935 WEBRTC_TRACE(kTraceWarning, kTraceVoice,
3936 VoEId(_instanceId, _channelId),
3937 "Channel::MixAudioWithFile() file mixing failed");
3938 return -1;
3939 }
3940 }
3941
andrew@webrtc.org63a50982012-05-02 23:56:37 +00003942 if (audioFrame.samples_per_channel_ == fileSamples)
niklase@google.com470e71d2011-07-07 08:21:25 +00003943 {
braveyao@webrtc.orgd7131432012-03-29 10:39:44 +00003944 // Currently file stream is always mono.
3945 // TODO(xians): Change the code when FilePlayer supports real stereo.
andrew@webrtc.org40ee3d02014-04-03 21:56:01 +00003946 MixWithSat(audioFrame.data_,
3947 audioFrame.num_channels_,
3948 fileBuffer.get(),
3949 1,
3950 fileSamples);
niklase@google.com470e71d2011-07-07 08:21:25 +00003951 }
3952 else
3953 {
3954 WEBRTC_TRACE(kTraceWarning, kTraceVoice, VoEId(_instanceId,_channelId),
andrew@webrtc.org63a50982012-05-02 23:56:37 +00003955 "Channel::MixAudioWithFile() samples_per_channel_(%d) != "
niklase@google.com470e71d2011-07-07 08:21:25 +00003956 "fileSamples(%d)",
andrew@webrtc.org63a50982012-05-02 23:56:37 +00003957 audioFrame.samples_per_channel_, fileSamples);
niklase@google.com470e71d2011-07-07 08:21:25 +00003958 return -1;
3959 }
3960
3961 return 0;
3962}
3963
3964int
3965Channel::InsertInbandDtmfTone()
3966{
niklas.enbom@webrtc.orgaf26f642011-11-16 12:41:36 +00003967 // Check if we should start a new tone.
niklase@google.com470e71d2011-07-07 08:21:25 +00003968 if (_inbandDtmfQueue.PendingDtmf() &&
3969 !_inbandDtmfGenerator.IsAddingTone() &&
3970 _inbandDtmfGenerator.DelaySinceLastTone() >
3971 kMinTelephoneEventSeparationMs)
3972 {
pbos@webrtc.org6141e132013-04-09 10:09:10 +00003973 int8_t eventCode(0);
3974 uint16_t lengthMs(0);
3975 uint8_t attenuationDb(0);
niklase@google.com470e71d2011-07-07 08:21:25 +00003976
3977 eventCode = _inbandDtmfQueue.NextDtmf(&lengthMs, &attenuationDb);
3978 _inbandDtmfGenerator.AddTone(eventCode, lengthMs, attenuationDb);
3979 if (_playInbandDtmfEvent)
3980 {
3981 // Add tone to output mixer using a reduced length to minimize
3982 // risk of echo.
3983 _outputMixerPtr->PlayDtmfTone(eventCode, lengthMs - 80,
3984 attenuationDb);
3985 }
3986 }
3987
3988 if (_inbandDtmfGenerator.IsAddingTone())
3989 {
pbos@webrtc.org6141e132013-04-09 10:09:10 +00003990 uint16_t frequency(0);
niklase@google.com470e71d2011-07-07 08:21:25 +00003991 _inbandDtmfGenerator.GetSampleRate(frequency);
3992
andrew@webrtc.org63a50982012-05-02 23:56:37 +00003993 if (frequency != _audioFrame.sample_rate_hz_)
niklase@google.com470e71d2011-07-07 08:21:25 +00003994 {
3995 // Update sample rate of Dtmf tone since the mixing frequency
3996 // has changed.
3997 _inbandDtmfGenerator.SetSampleRate(
pbos@webrtc.org6141e132013-04-09 10:09:10 +00003998 (uint16_t) (_audioFrame.sample_rate_hz_));
niklase@google.com470e71d2011-07-07 08:21:25 +00003999 // Reset the tone to be added taking the new sample rate into
4000 // account.
4001 _inbandDtmfGenerator.ResetTone();
4002 }
andrew@webrtc.orgae1a58b2013-01-22 04:44:30 +00004003
pbos@webrtc.org6141e132013-04-09 10:09:10 +00004004 int16_t toneBuffer[320];
4005 uint16_t toneSamples(0);
niklase@google.com470e71d2011-07-07 08:21:25 +00004006 // Get 10ms tone segment and set time since last tone to zero
4007 if (_inbandDtmfGenerator.Get10msTone(toneBuffer, toneSamples) == -1)
4008 {
4009 WEBRTC_TRACE(kTraceWarning, kTraceVoice,
4010 VoEId(_instanceId, _channelId),
4011 "Channel::EncodeAndSend() inserting Dtmf failed");
4012 return -1;
4013 }
4014
niklas.enbom@webrtc.orgaf26f642011-11-16 12:41:36 +00004015 // Replace mixed audio with DTMF tone.
andrew@webrtc.orgae1a58b2013-01-22 04:44:30 +00004016 for (int sample = 0;
andrew@webrtc.org63a50982012-05-02 23:56:37 +00004017 sample < _audioFrame.samples_per_channel_;
niklas.enbom@webrtc.orgaf26f642011-11-16 12:41:36 +00004018 sample++)
4019 {
andrew@webrtc.orgae1a58b2013-01-22 04:44:30 +00004020 for (int channel = 0;
4021 channel < _audioFrame.num_channels_;
niklas.enbom@webrtc.orgaf26f642011-11-16 12:41:36 +00004022 channel++)
4023 {
andrew@webrtc.orgae1a58b2013-01-22 04:44:30 +00004024 const int index = sample * _audioFrame.num_channels_ + channel;
4025 _audioFrame.data_[index] = toneBuffer[sample];
niklas.enbom@webrtc.orgaf26f642011-11-16 12:41:36 +00004026 }
4027 }
andrew@webrtc.orgae1a58b2013-01-22 04:44:30 +00004028
andrew@webrtc.org63a50982012-05-02 23:56:37 +00004029 assert(_audioFrame.samples_per_channel_ == toneSamples);
niklase@google.com470e71d2011-07-07 08:21:25 +00004030 } else
4031 {
4032 // Add 10ms to "delay-since-last-tone" counter
4033 _inbandDtmfGenerator.UpdateDelaySinceLastTone();
4034 }
4035 return 0;
4036}
4037
pbos@webrtc.org6141e132013-04-09 10:09:10 +00004038int32_t
pkasting@chromium.org4591fbd2014-11-20 22:28:14 +00004039Channel::SendPacketRaw(const void *data, size_t len, bool RTCP)
niklase@google.com470e71d2011-07-07 08:21:25 +00004040{
wu@webrtc.orgfb648da2013-10-18 21:10:51 +00004041 CriticalSectionScoped cs(&_callbackCritSect);
niklase@google.com470e71d2011-07-07 08:21:25 +00004042 if (_transportPtr == NULL)
4043 {
4044 return -1;
4045 }
4046 if (!RTCP)
4047 {
4048 return _transportPtr->SendPacket(_channelId, data, len);
4049 }
4050 else
4051 {
4052 return _transportPtr->SendRTCPPacket(_channelId, data, len);
4053 }
4054}
4055
pwestin@webrtc.org1de01352013-04-11 20:23:35 +00004056// Called for incoming RTP packets after successful RTP header parsing.
4057void Channel::UpdatePacketDelay(uint32_t rtp_timestamp,
4058 uint16_t sequence_number) {
4059 WEBRTC_TRACE(kTraceStream, kTraceVoice, VoEId(_instanceId,_channelId),
4060 "Channel::UpdatePacketDelay(timestamp=%lu, sequenceNumber=%u)",
4061 rtp_timestamp, sequence_number);
niklase@google.com470e71d2011-07-07 08:21:25 +00004062
pwestin@webrtc.org1de01352013-04-11 20:23:35 +00004063 // Get frequency of last received payload
wu@webrtc.org94454b72014-06-05 20:34:08 +00004064 int rtp_receive_frequency = GetPlayoutFrequency();
niklase@google.com470e71d2011-07-07 08:21:25 +00004065
turaj@webrtc.orge46c8d32013-05-22 20:39:43 +00004066 // Update the least required delay.
andrew@webrtc.orgeb524d92013-09-23 23:02:24 +00004067 least_required_delay_ms_ = audio_coding_->LeastRequiredDelayMs();
turaj@webrtc.orge46c8d32013-05-22 20:39:43 +00004068
turaj@webrtc.org167b6df2013-12-13 21:05:07 +00004069 // |jitter_buffer_playout_timestamp_| updated in UpdatePlayoutTimestamp for
4070 // every incoming packet.
4071 uint32_t timestamp_diff_ms = (rtp_timestamp -
4072 jitter_buffer_playout_timestamp_) / (rtp_receive_frequency / 1000);
henrik.lundin@webrtc.orgd6692992014-03-20 12:04:09 +00004073 if (!IsNewerTimestamp(rtp_timestamp, jitter_buffer_playout_timestamp_) ||
4074 timestamp_diff_ms > (2 * kVoiceEngineMaxMinPlayoutDelayMs)) {
4075 // If |jitter_buffer_playout_timestamp_| is newer than the incoming RTP
4076 // timestamp, the resulting difference is negative, but is set to zero.
4077 // This can happen when a network glitch causes a packet to arrive late,
4078 // and during long comfort noise periods with clock drift.
4079 timestamp_diff_ms = 0;
4080 }
niklase@google.com470e71d2011-07-07 08:21:25 +00004081
pwestin@webrtc.org1de01352013-04-11 20:23:35 +00004082 uint16_t packet_delay_ms = (rtp_timestamp - _previousTimestamp) /
4083 (rtp_receive_frequency / 1000);
niklase@google.com470e71d2011-07-07 08:21:25 +00004084
pwestin@webrtc.org1de01352013-04-11 20:23:35 +00004085 _previousTimestamp = rtp_timestamp;
niklase@google.com470e71d2011-07-07 08:21:25 +00004086
pwestin@webrtc.org1de01352013-04-11 20:23:35 +00004087 if (timestamp_diff_ms == 0) return;
niklase@google.com470e71d2011-07-07 08:21:25 +00004088
pwestin@webrtc.org1de01352013-04-11 20:23:35 +00004089 if (packet_delay_ms >= 10 && packet_delay_ms <= 60) {
4090 _recPacketDelayMs = packet_delay_ms;
4091 }
niklase@google.com470e71d2011-07-07 08:21:25 +00004092
pwestin@webrtc.org1de01352013-04-11 20:23:35 +00004093 if (_average_jitter_buffer_delay_us == 0) {
4094 _average_jitter_buffer_delay_us = timestamp_diff_ms * 1000;
4095 return;
4096 }
4097
4098 // Filter average delay value using exponential filter (alpha is
4099 // 7/8). We derive 1000 *_average_jitter_buffer_delay_us here (reduces
4100 // risk of rounding error) and compensate for it in GetDelayEstimate()
4101 // later.
4102 _average_jitter_buffer_delay_us = (_average_jitter_buffer_delay_us * 7 +
4103 1000 * timestamp_diff_ms + 500) / 8;
niklase@google.com470e71d2011-07-07 08:21:25 +00004104}
4105
4106void
4107Channel::RegisterReceiveCodecsToRTPModule()
4108{
4109 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId,_channelId),
4110 "Channel::RegisterReceiveCodecsToRTPModule()");
4111
4112
4113 CodecInst codec;
pbos@webrtc.org6141e132013-04-09 10:09:10 +00004114 const uint8_t nSupportedCodecs = AudioCodingModule::NumberOfCodecs();
niklase@google.com470e71d2011-07-07 08:21:25 +00004115
4116 for (int idx = 0; idx < nSupportedCodecs; idx++)
4117 {
4118 // Open up the RTP/RTCP receiver for all supported codecs
andrew@webrtc.orgeb524d92013-09-23 23:02:24 +00004119 if ((audio_coding_->Codec(idx, &codec) == -1) ||
wu@webrtc.org822fbd82013-08-15 23:38:54 +00004120 (rtp_receiver_->RegisterReceivePayload(
4121 codec.plname,
4122 codec.pltype,
4123 codec.plfreq,
4124 codec.channels,
4125 (codec.rate < 0) ? 0 : codec.rate) == -1))
niklase@google.com470e71d2011-07-07 08:21:25 +00004126 {
4127 WEBRTC_TRACE(
4128 kTraceWarning,
4129 kTraceVoice,
4130 VoEId(_instanceId, _channelId),
4131 "Channel::RegisterReceiveCodecsToRTPModule() unable"
4132 " to register %s (%d/%d/%d/%d) to RTP/RTCP receiver",
4133 codec.plname, codec.pltype, codec.plfreq,
4134 codec.channels, codec.rate);
4135 }
4136 else
4137 {
4138 WEBRTC_TRACE(
4139 kTraceInfo,
4140 kTraceVoice,
4141 VoEId(_instanceId, _channelId),
4142 "Channel::RegisterReceiveCodecsToRTPModule() %s "
wu@webrtc.orgfcd12b32011-09-15 20:49:50 +00004143 "(%d/%d/%d/%d) has been added to the RTP/RTCP "
niklase@google.com470e71d2011-07-07 08:21:25 +00004144 "receiver",
4145 codec.plname, codec.pltype, codec.plfreq,
4146 codec.channels, codec.rate);
4147 }
4148 }
4149}
4150
turaj@webrtc.org8c8ad852013-01-31 18:20:17 +00004151// Assuming this method is called with valid payload type.
turaj@webrtc.org42259e72012-12-11 02:15:12 +00004152int Channel::SetRedPayloadType(int red_payload_type) {
turaj@webrtc.org42259e72012-12-11 02:15:12 +00004153 CodecInst codec;
4154 bool found_red = false;
4155
4156 // Get default RED settings from the ACM database
4157 const int num_codecs = AudioCodingModule::NumberOfCodecs();
4158 for (int idx = 0; idx < num_codecs; idx++) {
andrew@webrtc.orgeb524d92013-09-23 23:02:24 +00004159 audio_coding_->Codec(idx, &codec);
turaj@webrtc.org42259e72012-12-11 02:15:12 +00004160 if (!STR_CASE_CMP(codec.plname, "RED")) {
4161 found_red = true;
4162 break;
4163 }
4164 }
4165
4166 if (!found_red) {
4167 _engineStatisticsPtr->SetLastError(
4168 VE_CODEC_ERROR, kTraceError,
4169 "SetRedPayloadType() RED is not supported");
4170 return -1;
4171 }
4172
turaj@webrtc.org9d532fd2013-01-31 18:34:19 +00004173 codec.pltype = red_payload_type;
andrew@webrtc.orgeb524d92013-09-23 23:02:24 +00004174 if (audio_coding_->RegisterSendCodec(codec) < 0) {
turaj@webrtc.org42259e72012-12-11 02:15:12 +00004175 _engineStatisticsPtr->SetLastError(
4176 VE_AUDIO_CODING_MODULE_ERROR, kTraceError,
4177 "SetRedPayloadType() RED registration in ACM module failed");
4178 return -1;
4179 }
4180
4181 if (_rtpRtcpModule->SetSendREDPayloadType(red_payload_type) != 0) {
4182 _engineStatisticsPtr->SetLastError(
4183 VE_RTP_RTCP_MODULE_ERROR, kTraceError,
4184 "SetRedPayloadType() RED registration in RTP/RTCP module failed");
4185 return -1;
4186 }
4187 return 0;
4188}
4189
wu@webrtc.orgebdb0e32014-03-06 23:49:08 +00004190int Channel::SetSendRtpHeaderExtension(bool enable, RTPExtensionType type,
4191 unsigned char id) {
4192 int error = 0;
4193 _rtpRtcpModule->DeregisterSendRtpHeaderExtension(type);
4194 if (enable) {
4195 error = _rtpRtcpModule->RegisterSendRtpHeaderExtension(type, id);
4196 }
4197 return error;
4198}
minyue@webrtc.orgc1a40a72014-05-28 09:52:06 +00004199
wu@webrtc.org94454b72014-06-05 20:34:08 +00004200int32_t Channel::GetPlayoutFrequency() {
4201 int32_t playout_frequency = audio_coding_->PlayoutFrequency();
4202 CodecInst current_recive_codec;
4203 if (audio_coding_->ReceiveCodec(&current_recive_codec) == 0) {
4204 if (STR_CASE_CMP("G722", current_recive_codec.plname) == 0) {
4205 // Even though the actual sampling rate for G.722 audio is
4206 // 16,000 Hz, the RTP clock rate for the G722 payload format is
4207 // 8,000 Hz because that value was erroneously assigned in
4208 // RFC 1890 and must remain unchanged for backward compatibility.
4209 playout_frequency = 8000;
4210 } else if (STR_CASE_CMP("opus", current_recive_codec.plname) == 0) {
4211 // We are resampling Opus internally to 32,000 Hz until all our
4212 // DSP routines can operate at 48,000 Hz, but the RTP clock
4213 // rate for the Opus payload format is standardized to 48,000 Hz,
4214 // because that is the maximum supported decoding sampling rate.
4215 playout_frequency = 48000;
4216 }
4217 }
4218 return playout_frequency;
4219}
4220
pkasting@chromium.org16825b12015-01-12 21:51:21 +00004221int64_t Channel::GetRTT() const {
minyue@webrtc.org2b58a442014-09-11 07:51:53 +00004222 RTCPMethod method = _rtpRtcpModule->RTCP();
4223 if (method == kRtcpOff) {
minyue@webrtc.org2b58a442014-09-11 07:51:53 +00004224 return 0;
4225 }
4226 std::vector<RTCPReportBlock> report_blocks;
4227 _rtpRtcpModule->RemoteRTCPStat(&report_blocks);
4228 if (report_blocks.empty()) {
minyue@webrtc.org2b58a442014-09-11 07:51:53 +00004229 return 0;
4230 }
4231
4232 uint32_t remoteSSRC = rtp_receiver_->SSRC();
4233 std::vector<RTCPReportBlock>::const_iterator it = report_blocks.begin();
4234 for (; it != report_blocks.end(); ++it) {
4235 if (it->remoteSSRC == remoteSSRC)
4236 break;
4237 }
4238 if (it == report_blocks.end()) {
4239 // We have not received packets with SSRC matching the report blocks.
4240 // To calculate RTT we try with the SSRC of the first report block.
4241 // This is very important for send-only channels where we don't know
4242 // the SSRC of the other end.
4243 remoteSSRC = report_blocks[0].remoteSSRC;
4244 }
pkasting@chromium.org16825b12015-01-12 21:51:21 +00004245 int64_t rtt = 0;
4246 int64_t avg_rtt = 0;
4247 int64_t max_rtt= 0;
4248 int64_t min_rtt = 0;
minyue@webrtc.org2b58a442014-09-11 07:51:53 +00004249 if (_rtpRtcpModule->RTT(remoteSSRC, &rtt, &avg_rtt, &min_rtt, &max_rtt)
4250 != 0) {
minyue@webrtc.org2b58a442014-09-11 07:51:53 +00004251 return 0;
4252 }
pkasting@chromium.org16825b12015-01-12 21:51:21 +00004253 return rtt;
minyue@webrtc.org2b58a442014-09-11 07:51:53 +00004254}
4255
pbos@webrtc.orgd900e8b2013-07-03 15:12:26 +00004256} // namespace voe
4257} // namespace webrtc