blob: 7900ac911cc14d0b2b31876df49a96092251c611 [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
62 virtual void StatisticsUpdated(const RtcpStatistics& statistics,
63 uint32_t ssrc) OVERRIDE {
64 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
pbos@webrtc.orgce4e9a32014-12-18 13:50:16 +000074 virtual void CNameChanged(const char* cname, uint32_t ssrc) OVERRIDE {}
75
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.
90 scoped_ptr<CriticalSectionWrapper> stats_lock_;
91 const uint32_t ssrc_;
92 ChannelStatistics stats_;
93};
94
minyue@webrtc.orgc1a40a72014-05-28 09:52:06 +000095class VoEBitrateObserver : public BitrateObserver {
96 public:
97 explicit VoEBitrateObserver(Channel* owner)
98 : owner_(owner) {}
99 virtual ~VoEBitrateObserver() {}
100
101 // Implements BitrateObserver.
102 virtual void OnNetworkChanged(const uint32_t bitrate_bps,
103 const uint8_t fraction_lost,
104 const uint32_t rtt) OVERRIDE {
105 // |fraction_lost| has a scale of 0 - 255.
106 owner_->OnNetworkChanged(bitrate_bps, fraction_lost, rtt);
107 }
108
109 private:
110 Channel* owner_;
111};
112
pbos@webrtc.org6141e132013-04-09 10:09:10 +0000113int32_t
niklase@google.com470e71d2011-07-07 08:21:25 +0000114Channel::SendData(FrameType frameType,
pbos@webrtc.org6141e132013-04-09 10:09:10 +0000115 uint8_t payloadType,
116 uint32_t timeStamp,
117 const uint8_t* payloadData,
pkasting@chromium.org4591fbd2014-11-20 22:28:14 +0000118 size_t payloadSize,
niklase@google.com470e71d2011-07-07 08:21:25 +0000119 const RTPFragmentationHeader* fragmentation)
120{
121 WEBRTC_TRACE(kTraceStream, kTraceVoice, VoEId(_instanceId,_channelId),
122 "Channel::SendData(frameType=%u, payloadType=%u, timeStamp=%u,"
pkasting@chromium.org4591fbd2014-11-20 22:28:14 +0000123 " payloadSize=%" PRIuS ", fragmentation=0x%x)",
124 frameType, payloadType, timeStamp,
125 payloadSize, fragmentation);
niklase@google.com470e71d2011-07-07 08:21:25 +0000126
127 if (_includeAudioLevelIndication)
128 {
129 // Store current audio level in the RTP/RTCP module.
130 // The level will be used in combination with voice-activity state
131 // (frameType) to add an RTP header extension
andrew@webrtc.org382c0c22014-05-05 18:22:21 +0000132 _rtpRtcpModule->SetAudioLevel(rms_level_.RMS());
niklase@google.com470e71d2011-07-07 08:21:25 +0000133 }
134
135 // Push data from ACM to RTP/RTCP-module to deliver audio frame for
136 // packetization.
137 // This call will trigger Transport::SendPacket() from the RTP/RTCP module.
pwestin@webrtc.org2853dde2012-05-11 11:08:54 +0000138 if (_rtpRtcpModule->SendOutgoingData((FrameType&)frameType,
niklase@google.com470e71d2011-07-07 08:21:25 +0000139 payloadType,
140 timeStamp,
stefan@webrtc.orgddfdfed2012-07-03 13:21:22 +0000141 // Leaving the time when this frame was
142 // received from the capture device as
143 // undefined for voice for now.
144 -1,
niklase@google.com470e71d2011-07-07 08:21:25 +0000145 payloadData,
146 payloadSize,
147 fragmentation) == -1)
148 {
149 _engineStatisticsPtr->SetLastError(
150 VE_RTP_RTCP_MODULE_ERROR, kTraceWarning,
151 "Channel::SendData() failed to send data to RTP/RTCP module");
152 return -1;
153 }
154
155 _lastLocalTimeStamp = timeStamp;
156 _lastPayloadType = payloadType;
157
158 return 0;
159}
160
pbos@webrtc.org6141e132013-04-09 10:09:10 +0000161int32_t
162Channel::InFrameType(int16_t frameType)
niklase@google.com470e71d2011-07-07 08:21:25 +0000163{
164 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId,_channelId),
165 "Channel::InFrameType(frameType=%d)", frameType);
166
mflodman@webrtc.org9a065d12012-03-07 08:12:21 +0000167 CriticalSectionScoped cs(&_callbackCritSect);
niklase@google.com470e71d2011-07-07 08:21:25 +0000168 // 1 indicates speech
169 _sendFrameType = (frameType == 1) ? 1 : 0;
170 return 0;
171}
172
pbos@webrtc.org6141e132013-04-09 10:09:10 +0000173int32_t
pbos@webrtc.org92135212013-05-14 08:31:39 +0000174Channel::OnRxVadDetected(int vadDecision)
niklase@google.com470e71d2011-07-07 08:21:25 +0000175{
176 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId, _channelId),
177 "Channel::OnRxVadDetected(vadDecision=%d)", vadDecision);
178
mflodman@webrtc.org9a065d12012-03-07 08:12:21 +0000179 CriticalSectionScoped cs(&_callbackCritSect);
niklase@google.com470e71d2011-07-07 08:21:25 +0000180 if (_rxVadObserverPtr)
181 {
182 _rxVadObserverPtr->OnRxVad(_channelId, vadDecision);
183 }
184
185 return 0;
186}
187
188int
pkasting@chromium.org4591fbd2014-11-20 22:28:14 +0000189Channel::SendPacket(int channel, const void *data, size_t len)
niklase@google.com470e71d2011-07-07 08:21:25 +0000190{
191 channel = VoEChannelId(channel);
192 assert(channel == _channelId);
193
194 WEBRTC_TRACE(kTraceStream, kTraceVoice, VoEId(_instanceId,_channelId),
pkasting@chromium.org4591fbd2014-11-20 22:28:14 +0000195 "Channel::SendPacket(channel=%d, len=%" PRIuS ")", channel,
196 len);
niklase@google.com470e71d2011-07-07 08:21:25 +0000197
wu@webrtc.orgfb648da2013-10-18 21:10:51 +0000198 CriticalSectionScoped cs(&_callbackCritSect);
199
niklase@google.com470e71d2011-07-07 08:21:25 +0000200 if (_transportPtr == NULL)
201 {
202 WEBRTC_TRACE(kTraceError, kTraceVoice, VoEId(_instanceId,_channelId),
203 "Channel::SendPacket() failed to send RTP packet due to"
204 " invalid transport object");
205 return -1;
206 }
207
pbos@webrtc.org6141e132013-04-09 10:09:10 +0000208 uint8_t* bufferToSendPtr = (uint8_t*)data;
pkasting@chromium.org4591fbd2014-11-20 22:28:14 +0000209 size_t bufferLength = len;
niklase@google.com470e71d2011-07-07 08:21:25 +0000210
211 // Dump the RTP packet to a file (if RTP dump is enabled).
pbos@webrtc.org6141e132013-04-09 10:09:10 +0000212 if (_rtpDumpOut.DumpPacket((const uint8_t*)data, len) == -1)
niklase@google.com470e71d2011-07-07 08:21:25 +0000213 {
214 WEBRTC_TRACE(kTraceWarning, kTraceVoice,
215 VoEId(_instanceId,_channelId),
216 "Channel::SendPacket() RTP dump to output file failed");
217 }
218
wu@webrtc.orgfb648da2013-10-18 21:10:51 +0000219 int n = _transportPtr->SendPacket(channel, bufferToSendPtr,
220 bufferLength);
221 if (n < 0) {
222 std::string transport_name =
223 _externalTransport ? "external transport" : "WebRtc sockets";
224 WEBRTC_TRACE(kTraceError, kTraceVoice,
225 VoEId(_instanceId,_channelId),
226 "Channel::SendPacket() RTP transmission using %s failed",
227 transport_name.c_str());
228 return -1;
niklase@google.com470e71d2011-07-07 08:21:25 +0000229 }
wu@webrtc.orgfb648da2013-10-18 21:10:51 +0000230 return n;
niklase@google.com470e71d2011-07-07 08:21:25 +0000231}
232
233int
pkasting@chromium.org4591fbd2014-11-20 22:28:14 +0000234Channel::SendRTCPPacket(int channel, const void *data, size_t len)
niklase@google.com470e71d2011-07-07 08:21:25 +0000235{
236 channel = VoEChannelId(channel);
237 assert(channel == _channelId);
238
239 WEBRTC_TRACE(kTraceStream, kTraceVoice, VoEId(_instanceId,_channelId),
pkasting@chromium.org4591fbd2014-11-20 22:28:14 +0000240 "Channel::SendRTCPPacket(channel=%d, len=%" PRIuS ")", channel,
241 len);
niklase@google.com470e71d2011-07-07 08:21:25 +0000242
wu@webrtc.orgfb648da2013-10-18 21:10:51 +0000243 CriticalSectionScoped cs(&_callbackCritSect);
244 if (_transportPtr == NULL)
niklase@google.com470e71d2011-07-07 08:21:25 +0000245 {
wu@webrtc.orgfb648da2013-10-18 21:10:51 +0000246 WEBRTC_TRACE(kTraceError, kTraceVoice,
247 VoEId(_instanceId,_channelId),
248 "Channel::SendRTCPPacket() failed to send RTCP packet"
249 " due to invalid transport object");
250 return -1;
niklase@google.com470e71d2011-07-07 08:21:25 +0000251 }
252
pbos@webrtc.org6141e132013-04-09 10:09:10 +0000253 uint8_t* bufferToSendPtr = (uint8_t*)data;
pkasting@chromium.org4591fbd2014-11-20 22:28:14 +0000254 size_t bufferLength = len;
niklase@google.com470e71d2011-07-07 08:21:25 +0000255
256 // Dump the RTCP packet to a file (if RTP dump is enabled).
pbos@webrtc.org6141e132013-04-09 10:09:10 +0000257 if (_rtpDumpOut.DumpPacket((const uint8_t*)data, len) == -1)
niklase@google.com470e71d2011-07-07 08:21:25 +0000258 {
259 WEBRTC_TRACE(kTraceWarning, kTraceVoice,
260 VoEId(_instanceId,_channelId),
261 "Channel::SendPacket() RTCP dump to output file failed");
262 }
263
wu@webrtc.orgfb648da2013-10-18 21:10:51 +0000264 int n = _transportPtr->SendRTCPPacket(channel,
265 bufferToSendPtr,
266 bufferLength);
267 if (n < 0) {
268 std::string transport_name =
269 _externalTransport ? "external transport" : "WebRtc sockets";
270 WEBRTC_TRACE(kTraceInfo, kTraceVoice,
271 VoEId(_instanceId,_channelId),
272 "Channel::SendRTCPPacket() transmission using %s failed",
273 transport_name.c_str());
274 return -1;
niklase@google.com470e71d2011-07-07 08:21:25 +0000275 }
wu@webrtc.orgfb648da2013-10-18 21:10:51 +0000276 return n;
niklase@google.com470e71d2011-07-07 08:21:25 +0000277}
278
279void
pbos@webrtc.org92135212013-05-14 08:31:39 +0000280Channel::OnPlayTelephoneEvent(int32_t id,
281 uint8_t event,
282 uint16_t lengthMs,
283 uint8_t volume)
niklase@google.com470e71d2011-07-07 08:21:25 +0000284{
285 WEBRTC_TRACE(kTraceStream, kTraceVoice, VoEId(_instanceId,_channelId),
286 "Channel::OnPlayTelephoneEvent(id=%d, event=%u, lengthMs=%u,"
wu@webrtc.orgfcd12b32011-09-15 20:49:50 +0000287 " volume=%u)", id, event, lengthMs, volume);
niklase@google.com470e71d2011-07-07 08:21:25 +0000288
289 if (!_playOutbandDtmfEvent || (event > 15))
290 {
291 // Ignore callback since feedback is disabled or event is not a
292 // Dtmf tone event.
293 return;
294 }
295
296 assert(_outputMixerPtr != NULL);
297
298 // Start playing out the Dtmf tone (if playout is enabled).
299 // Reduce length of tone with 80ms to the reduce risk of echo.
300 _outputMixerPtr->PlayDtmfTone(event, lengthMs - 80, volume);
301}
302
303void
stefan@webrtc.org286fe0b2013-08-21 20:58:21 +0000304Channel::OnIncomingSSRCChanged(int32_t id, uint32_t ssrc)
niklase@google.com470e71d2011-07-07 08:21:25 +0000305{
306 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId,_channelId),
307 "Channel::OnIncomingSSRCChanged(id=%d, SSRC=%d)",
stefan@webrtc.org286fe0b2013-08-21 20:58:21 +0000308 id, ssrc);
niklase@google.com470e71d2011-07-07 08:21:25 +0000309
dwkang@webrtc.orgb295a3f2013-08-29 07:34:12 +0000310 // Update ssrc so that NTP for AV sync can be updated.
311 _rtpRtcpModule->SetRemoteSSRC(ssrc);
niklase@google.com470e71d2011-07-07 08:21:25 +0000312}
313
pbos@webrtc.org92135212013-05-14 08:31:39 +0000314void Channel::OnIncomingCSRCChanged(int32_t id,
315 uint32_t CSRC,
316 bool added)
niklase@google.com470e71d2011-07-07 08:21:25 +0000317{
318 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId,_channelId),
319 "Channel::OnIncomingCSRCChanged(id=%d, CSRC=%d, added=%d)",
320 id, CSRC, added);
niklase@google.com470e71d2011-07-07 08:21:25 +0000321}
322
stefan@webrtc.org286fe0b2013-08-21 20:58:21 +0000323void Channel::ResetStatistics(uint32_t ssrc) {
324 StreamStatistician* statistician =
325 rtp_receive_statistics_->GetStatistician(ssrc);
326 if (statistician) {
327 statistician->ResetStatistics();
328 }
sprang@webrtc.org54ae4ff2013-12-19 13:26:02 +0000329 statistics_proxy_->ResetStatistics();
wu@webrtc.org822fbd82013-08-15 23:38:54 +0000330}
331
pbos@webrtc.org6141e132013-04-09 10:09:10 +0000332int32_t
niklase@google.com470e71d2011-07-07 08:21:25 +0000333Channel::OnInitializeDecoder(
pbos@webrtc.org92135212013-05-14 08:31:39 +0000334 int32_t id,
335 int8_t payloadType,
leozwang@webrtc.org813e4b02012-03-01 18:34:25 +0000336 const char payloadName[RTP_PAYLOAD_NAME_SIZE],
pbos@webrtc.org92135212013-05-14 08:31:39 +0000337 int frequency,
338 uint8_t channels,
339 uint32_t rate)
niklase@google.com470e71d2011-07-07 08:21:25 +0000340{
341 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId,_channelId),
342 "Channel::OnInitializeDecoder(id=%d, payloadType=%d, "
343 "payloadName=%s, frequency=%u, channels=%u, rate=%u)",
344 id, payloadType, payloadName, frequency, channels, rate);
345
andrew@webrtc.orgceb148c2011-08-23 17:53:54 +0000346 assert(VoEChannelId(id) == _channelId);
niklase@google.com470e71d2011-07-07 08:21:25 +0000347
henrika@webrtc.orgf75901f2012-01-16 08:45:42 +0000348 CodecInst receiveCodec = {0};
349 CodecInst dummyCodec = {0};
niklase@google.com470e71d2011-07-07 08:21:25 +0000350
351 receiveCodec.pltype = payloadType;
niklase@google.com470e71d2011-07-07 08:21:25 +0000352 receiveCodec.plfreq = frequency;
353 receiveCodec.channels = channels;
354 receiveCodec.rate = rate;
henrika@webrtc.orgf75901f2012-01-16 08:45:42 +0000355 strncpy(receiveCodec.plname, payloadName, RTP_PAYLOAD_NAME_SIZE - 1);
andrew@webrtc.orgae1a58b2013-01-22 04:44:30 +0000356
andrew@webrtc.orgeb524d92013-09-23 23:02:24 +0000357 audio_coding_->Codec(payloadName, &dummyCodec, frequency, channels);
niklase@google.com470e71d2011-07-07 08:21:25 +0000358 receiveCodec.pacsize = dummyCodec.pacsize;
359
360 // Register the new codec to the ACM
andrew@webrtc.orgeb524d92013-09-23 23:02:24 +0000361 if (audio_coding_->RegisterReceiveCodec(receiveCodec) == -1)
niklase@google.com470e71d2011-07-07 08:21:25 +0000362 {
363 WEBRTC_TRACE(kTraceWarning, kTraceVoice,
andrew@webrtc.orgceb148c2011-08-23 17:53:54 +0000364 VoEId(_instanceId, _channelId),
niklase@google.com470e71d2011-07-07 08:21:25 +0000365 "Channel::OnInitializeDecoder() invalid codec ("
366 "pt=%d, name=%s) received - 1", payloadType, payloadName);
367 _engineStatisticsPtr->SetLastError(VE_AUDIO_CODING_MODULE_ERROR);
368 return -1;
369 }
370
371 return 0;
372}
373
pbos@webrtc.org6141e132013-04-09 10:09:10 +0000374int32_t
375Channel::OnReceivedPayloadData(const uint8_t* payloadData,
pkasting@chromium.org4591fbd2014-11-20 22:28:14 +0000376 size_t payloadSize,
niklase@google.com470e71d2011-07-07 08:21:25 +0000377 const WebRtcRTPHeader* rtpHeader)
378{
379 WEBRTC_TRACE(kTraceStream, kTraceVoice, VoEId(_instanceId,_channelId),
pkasting@chromium.org4591fbd2014-11-20 22:28:14 +0000380 "Channel::OnReceivedPayloadData(payloadSize=%" PRIuS ","
niklase@google.com470e71d2011-07-07 08:21:25 +0000381 " payloadType=%u, audioChannel=%u)",
382 payloadSize,
383 rtpHeader->header.payloadType,
384 rtpHeader->type.Audio.channel);
385
henrika@webrtc.org944cbeb2014-03-18 10:32:33 +0000386 if (!channel_state_.Get().playing)
niklase@google.com470e71d2011-07-07 08:21:25 +0000387 {
388 // Avoid inserting into NetEQ when we are not playing. Count the
389 // packet as discarded.
390 WEBRTC_TRACE(kTraceStream, kTraceVoice,
391 VoEId(_instanceId, _channelId),
392 "received packet is discarded since playing is not"
393 " activated");
394 _numberOfDiscardedPackets++;
395 return 0;
396 }
397
398 // Push the incoming payload (parsed and ready for decoding) into the ACM
andrew@webrtc.orgeb524d92013-09-23 23:02:24 +0000399 if (audio_coding_->IncomingPacket(payloadData,
400 payloadSize,
401 *rtpHeader) != 0)
niklase@google.com470e71d2011-07-07 08:21:25 +0000402 {
403 _engineStatisticsPtr->SetLastError(
404 VE_AUDIO_CODING_MODULE_ERROR, kTraceWarning,
405 "Channel::OnReceivedPayloadData() unable to push data to the ACM");
406 return -1;
407 }
408
pwestin@webrtc.orgd30859e2013-06-06 21:09:01 +0000409 // Update the packet delay.
niklase@google.com470e71d2011-07-07 08:21:25 +0000410 UpdatePacketDelay(rtpHeader->header.timestamp,
411 rtpHeader->header.sequenceNumber);
pwestin@webrtc.orgd30859e2013-06-06 21:09:01 +0000412
wu@webrtc.org822fbd82013-08-15 23:38:54 +0000413 uint16_t round_trip_time = 0;
414 _rtpRtcpModule->RTT(rtp_receiver_->SSRC(), &round_trip_time,
415 NULL, NULL, NULL);
pwestin@webrtc.orgd30859e2013-06-06 21:09:01 +0000416
andrew@webrtc.orgeb524d92013-09-23 23:02:24 +0000417 std::vector<uint16_t> nack_list = audio_coding_->GetNackList(
wu@webrtc.org822fbd82013-08-15 23:38:54 +0000418 round_trip_time);
419 if (!nack_list.empty()) {
420 // Can't use nack_list.data() since it's not supported by all
421 // compilers.
422 ResendPackets(&(nack_list[0]), static_cast<int>(nack_list.size()));
pwestin@webrtc.orgd30859e2013-06-06 21:09:01 +0000423 }
niklase@google.com470e71d2011-07-07 08:21:25 +0000424 return 0;
425}
426
stefan@webrtc.org7bb8f022013-09-06 13:40:11 +0000427bool Channel::OnRecoveredPacket(const uint8_t* rtp_packet,
pkasting@chromium.org4591fbd2014-11-20 22:28:14 +0000428 size_t rtp_packet_length) {
stefan@webrtc.org7bb8f022013-09-06 13:40:11 +0000429 RTPHeader header;
430 if (!rtp_header_parser_->Parse(rtp_packet, rtp_packet_length, &header)) {
431 WEBRTC_TRACE(kTraceDebug, webrtc::kTraceVoice, _channelId,
432 "IncomingPacket invalid RTP header");
433 return false;
434 }
435 header.payload_type_frequency =
436 rtp_payload_registry_->GetPayloadTypeFrequency(header.payloadType);
437 if (header.payload_type_frequency < 0)
438 return false;
439 return ReceivePacket(rtp_packet, rtp_packet_length, header, false);
440}
441
pbos@webrtc.org92135212013-05-14 08:31:39 +0000442int32_t Channel::GetAudioFrame(int32_t id, AudioFrame& audioFrame)
niklase@google.com470e71d2011-07-07 08:21:25 +0000443{
444 WEBRTC_TRACE(kTraceStream, kTraceVoice, VoEId(_instanceId,_channelId),
445 "Channel::GetAudioFrame(id=%d)", id);
446
447 // Get 10ms raw PCM data from the ACM (mixer limits output frequency)
andrew@webrtc.orgeb524d92013-09-23 23:02:24 +0000448 if (audio_coding_->PlayoutData10Ms(audioFrame.sample_rate_hz_,
449 &audioFrame) == -1)
niklase@google.com470e71d2011-07-07 08:21:25 +0000450 {
451 WEBRTC_TRACE(kTraceError, kTraceVoice,
452 VoEId(_instanceId,_channelId),
453 "Channel::GetAudioFrame() PlayoutData10Ms() failed!");
andrew@webrtc.org7859e102012-01-13 00:30:11 +0000454 // In all likelihood, the audio in this frame is garbage. We return an
455 // error so that the audio mixer module doesn't add it to the mix. As
456 // a result, it won't be played out and the actions skipped here are
457 // irrelevant.
458 return -1;
niklase@google.com470e71d2011-07-07 08:21:25 +0000459 }
460
461 if (_RxVadDetection)
462 {
463 UpdateRxVadDetection(audioFrame);
464 }
465
466 // Convert module ID to internal VoE channel ID
andrew@webrtc.org63a50982012-05-02 23:56:37 +0000467 audioFrame.id_ = VoEChannelId(audioFrame.id_);
niklase@google.com470e71d2011-07-07 08:21:25 +0000468 // Store speech type for dead-or-alive detection
andrew@webrtc.org63a50982012-05-02 23:56:37 +0000469 _outputSpeechType = audioFrame.speech_type_;
niklase@google.com470e71d2011-07-07 08:21:25 +0000470
henrika@webrtc.org944cbeb2014-03-18 10:32:33 +0000471 ChannelState::State state = channel_state_.Get();
472
473 if (state.rx_apm_is_enabled) {
andrew@webrtc.org60730cf2014-01-07 17:45:09 +0000474 int err = rx_audioproc_->ProcessStream(&audioFrame);
475 if (err) {
476 LOG(LS_ERROR) << "ProcessStream() error: " << err;
477 assert(false);
478 }
niklase@google.com470e71d2011-07-07 08:21:25 +0000479 }
480
wu@webrtc.org63420662013-10-17 18:28:55 +0000481 float output_gain = 1.0f;
482 float left_pan = 1.0f;
483 float right_pan = 1.0f;
niklase@google.com470e71d2011-07-07 08:21:25 +0000484 {
wu@webrtc.org63420662013-10-17 18:28:55 +0000485 CriticalSectionScoped cs(&volume_settings_critsect_);
486 output_gain = _outputGain;
487 left_pan = _panLeft;
488 right_pan= _panRight;
489 }
490
491 // Output volume scaling
492 if (output_gain < 0.99f || output_gain > 1.01f)
493 {
494 AudioFrameOperations::ScaleWithSat(output_gain, audioFrame);
niklase@google.com470e71d2011-07-07 08:21:25 +0000495 }
496
497 // Scale left and/or right channel(s) if stereo and master balance is
498 // active
499
wu@webrtc.org63420662013-10-17 18:28:55 +0000500 if (left_pan != 1.0f || right_pan != 1.0f)
niklase@google.com470e71d2011-07-07 08:21:25 +0000501 {
andrew@webrtc.org63a50982012-05-02 23:56:37 +0000502 if (audioFrame.num_channels_ == 1)
niklase@google.com470e71d2011-07-07 08:21:25 +0000503 {
504 // Emulate stereo mode since panning is active.
505 // The mono signal is copied to both left and right channels here.
andrew@webrtc.org4ecea3e2012-06-27 03:25:31 +0000506 AudioFrameOperations::MonoToStereo(&audioFrame);
niklase@google.com470e71d2011-07-07 08:21:25 +0000507 }
508 // For true stereo mode (when we are receiving a stereo signal), no
509 // action is needed.
510
511 // Do the panning operation (the audio frame contains stereo at this
512 // stage)
wu@webrtc.org63420662013-10-17 18:28:55 +0000513 AudioFrameOperations::Scale(left_pan, right_pan, audioFrame);
niklase@google.com470e71d2011-07-07 08:21:25 +0000514 }
515
516 // Mix decoded PCM output with file if file mixing is enabled
henrika@webrtc.org944cbeb2014-03-18 10:32:33 +0000517 if (state.output_file_playing)
niklase@google.com470e71d2011-07-07 08:21:25 +0000518 {
andrew@webrtc.org63a50982012-05-02 23:56:37 +0000519 MixAudioWithFile(audioFrame, audioFrame.sample_rate_hz_);
niklase@google.com470e71d2011-07-07 08:21:25 +0000520 }
521
niklase@google.com470e71d2011-07-07 08:21:25 +0000522 // External media
523 if (_outputExternalMedia)
524 {
mflodman@webrtc.org9a065d12012-03-07 08:12:21 +0000525 CriticalSectionScoped cs(&_callbackCritSect);
andrew@webrtc.org63a50982012-05-02 23:56:37 +0000526 const bool isStereo = (audioFrame.num_channels_ == 2);
niklase@google.com470e71d2011-07-07 08:21:25 +0000527 if (_outputExternalMediaCallbackPtr)
528 {
529 _outputExternalMediaCallbackPtr->Process(
530 _channelId,
531 kPlaybackPerChannel,
pbos@webrtc.org6141e132013-04-09 10:09:10 +0000532 (int16_t*)audioFrame.data_,
andrew@webrtc.org63a50982012-05-02 23:56:37 +0000533 audioFrame.samples_per_channel_,
534 audioFrame.sample_rate_hz_,
niklase@google.com470e71d2011-07-07 08:21:25 +0000535 isStereo);
536 }
537 }
538
539 // Record playout if enabled
540 {
mflodman@webrtc.org9a065d12012-03-07 08:12:21 +0000541 CriticalSectionScoped cs(&_fileCritSect);
niklase@google.com470e71d2011-07-07 08:21:25 +0000542
543 if (_outputFileRecording && _outputFileRecorderPtr)
544 {
niklas.enbom@webrtc.org5398d952012-03-26 08:11:25 +0000545 _outputFileRecorderPtr->RecordAudioToFile(audioFrame);
niklase@google.com470e71d2011-07-07 08:21:25 +0000546 }
547 }
548
549 // Measure audio level (0-9)
550 _outputAudioLevel.ComputeLevel(audioFrame);
551
wu@webrtc.org94454b72014-06-05 20:34:08 +0000552 if (capture_start_rtp_time_stamp_ < 0 && audioFrame.timestamp_ != 0) {
553 // The first frame with a valid rtp timestamp.
wu@webrtc.orgcb711f72014-05-19 17:39:11 +0000554 capture_start_rtp_time_stamp_ = audioFrame.timestamp_;
wu@webrtc.org94454b72014-06-05 20:34:08 +0000555 }
556
557 if (capture_start_rtp_time_stamp_ >= 0) {
558 // audioFrame.timestamp_ should be valid from now on.
559
560 // Compute elapsed time.
561 int64_t unwrap_timestamp =
562 rtp_ts_wraparound_handler_->Unwrap(audioFrame.timestamp_);
563 audioFrame.elapsed_time_ms_ =
564 (unwrap_timestamp - capture_start_rtp_time_stamp_) /
565 (GetPlayoutFrequency() / 1000);
566
stefan@webrtc.org8e24d872014-09-02 18:58:24 +0000567 {
wu@webrtc.orgcb711f72014-05-19 17:39:11 +0000568 CriticalSectionScoped lock(ts_stats_lock_.get());
stefan@webrtc.org8e24d872014-09-02 18:58:24 +0000569 // Compute ntp time.
570 audioFrame.ntp_time_ms_ = ntp_estimator_.Estimate(
571 audioFrame.timestamp_);
572 // |ntp_time_ms_| won't be valid until at least 2 RTCP SRs are received.
573 if (audioFrame.ntp_time_ms_ > 0) {
574 // Compute |capture_start_ntp_time_ms_| so that
575 // |capture_start_ntp_time_ms_| + |elapsed_time_ms_| == |ntp_time_ms_|
576 capture_start_ntp_time_ms_ =
577 audioFrame.ntp_time_ms_ - audioFrame.elapsed_time_ms_;
578 }
wu@webrtc.orgcb711f72014-05-19 17:39:11 +0000579 }
580 }
581
niklase@google.com470e71d2011-07-07 08:21:25 +0000582 return 0;
583}
584
pbos@webrtc.org6141e132013-04-09 10:09:10 +0000585int32_t
pbos@webrtc.org92135212013-05-14 08:31:39 +0000586Channel::NeededFrequency(int32_t id)
niklase@google.com470e71d2011-07-07 08:21:25 +0000587{
588 WEBRTC_TRACE(kTraceStream, kTraceVoice, VoEId(_instanceId,_channelId),
589 "Channel::NeededFrequency(id=%d)", id);
590
591 int highestNeeded = 0;
592
593 // Determine highest needed receive frequency
andrew@webrtc.orgeb524d92013-09-23 23:02:24 +0000594 int32_t receiveFrequency = audio_coding_->ReceiveFrequency();
niklase@google.com470e71d2011-07-07 08:21:25 +0000595
596 // Return the bigger of playout and receive frequency in the ACM.
andrew@webrtc.orgeb524d92013-09-23 23:02:24 +0000597 if (audio_coding_->PlayoutFrequency() > receiveFrequency)
niklase@google.com470e71d2011-07-07 08:21:25 +0000598 {
andrew@webrtc.orgeb524d92013-09-23 23:02:24 +0000599 highestNeeded = audio_coding_->PlayoutFrequency();
niklase@google.com470e71d2011-07-07 08:21:25 +0000600 }
601 else
602 {
603 highestNeeded = receiveFrequency;
604 }
605
606 // Special case, if we're playing a file on the playout side
607 // we take that frequency into consideration as well
608 // This is not needed on sending side, since the codec will
609 // limit the spectrum anyway.
henrika@webrtc.org944cbeb2014-03-18 10:32:33 +0000610 if (channel_state_.Get().output_file_playing)
niklase@google.com470e71d2011-07-07 08:21:25 +0000611 {
mflodman@webrtc.org9a065d12012-03-07 08:12:21 +0000612 CriticalSectionScoped cs(&_fileCritSect);
henrika@webrtc.org944cbeb2014-03-18 10:32:33 +0000613 if (_outputFilePlayerPtr)
niklase@google.com470e71d2011-07-07 08:21:25 +0000614 {
615 if(_outputFilePlayerPtr->Frequency()>highestNeeded)
616 {
617 highestNeeded=_outputFilePlayerPtr->Frequency();
618 }
619 }
620 }
621
622 return(highestNeeded);
623}
624
pbos@webrtc.org6141e132013-04-09 10:09:10 +0000625int32_t
niklase@google.com470e71d2011-07-07 08:21:25 +0000626Channel::CreateChannel(Channel*& channel,
pbos@webrtc.org92135212013-05-14 08:31:39 +0000627 int32_t channelId,
minyue@webrtc.orge509f942013-09-12 17:03:00 +0000628 uint32_t instanceId,
629 const Config& config)
niklase@google.com470e71d2011-07-07 08:21:25 +0000630{
631 WEBRTC_TRACE(kTraceMemory, kTraceVoice, VoEId(instanceId,channelId),
632 "Channel::CreateChannel(channelId=%d, instanceId=%d)",
633 channelId, instanceId);
634
minyue@webrtc.orge509f942013-09-12 17:03:00 +0000635 channel = new Channel(channelId, instanceId, config);
niklase@google.com470e71d2011-07-07 08:21:25 +0000636 if (channel == NULL)
637 {
638 WEBRTC_TRACE(kTraceMemory, kTraceVoice,
639 VoEId(instanceId,channelId),
640 "Channel::CreateChannel() unable to allocate memory for"
641 " channel");
642 return -1;
643 }
644 return 0;
645}
646
647void
pbos@webrtc.org92135212013-05-14 08:31:39 +0000648Channel::PlayNotification(int32_t id, uint32_t durationMs)
niklase@google.com470e71d2011-07-07 08:21:25 +0000649{
650 WEBRTC_TRACE(kTraceStream, kTraceVoice, VoEId(_instanceId,_channelId),
651 "Channel::PlayNotification(id=%d, durationMs=%d)",
652 id, durationMs);
653
654 // Not implement yet
655}
656
657void
pbos@webrtc.org92135212013-05-14 08:31:39 +0000658Channel::RecordNotification(int32_t id, uint32_t durationMs)
niklase@google.com470e71d2011-07-07 08:21:25 +0000659{
660 WEBRTC_TRACE(kTraceStream, kTraceVoice, VoEId(_instanceId,_channelId),
661 "Channel::RecordNotification(id=%d, durationMs=%d)",
662 id, durationMs);
663
664 // Not implement yet
665}
666
667void
pbos@webrtc.org92135212013-05-14 08:31:39 +0000668Channel::PlayFileEnded(int32_t id)
niklase@google.com470e71d2011-07-07 08:21:25 +0000669{
670 WEBRTC_TRACE(kTraceStream, kTraceVoice, VoEId(_instanceId,_channelId),
671 "Channel::PlayFileEnded(id=%d)", id);
672
673 if (id == _inputFilePlayerId)
674 {
henrika@webrtc.org944cbeb2014-03-18 10:32:33 +0000675 channel_state_.SetInputFilePlaying(false);
niklase@google.com470e71d2011-07-07 08:21:25 +0000676 WEBRTC_TRACE(kTraceStateInfo, kTraceVoice,
677 VoEId(_instanceId,_channelId),
678 "Channel::PlayFileEnded() => input file player module is"
679 " shutdown");
680 }
681 else if (id == _outputFilePlayerId)
682 {
henrika@webrtc.org944cbeb2014-03-18 10:32:33 +0000683 channel_state_.SetOutputFilePlaying(false);
niklase@google.com470e71d2011-07-07 08:21:25 +0000684 WEBRTC_TRACE(kTraceStateInfo, kTraceVoice,
685 VoEId(_instanceId,_channelId),
686 "Channel::PlayFileEnded() => output file player module is"
687 " shutdown");
688 }
689}
690
691void
pbos@webrtc.org92135212013-05-14 08:31:39 +0000692Channel::RecordFileEnded(int32_t id)
niklase@google.com470e71d2011-07-07 08:21:25 +0000693{
694 WEBRTC_TRACE(kTraceStream, kTraceVoice, VoEId(_instanceId,_channelId),
695 "Channel::RecordFileEnded(id=%d)", id);
696
697 assert(id == _outputFileRecorderId);
698
mflodman@webrtc.org9a065d12012-03-07 08:12:21 +0000699 CriticalSectionScoped cs(&_fileCritSect);
niklase@google.com470e71d2011-07-07 08:21:25 +0000700
701 _outputFileRecording = false;
702 WEBRTC_TRACE(kTraceStateInfo, kTraceVoice,
703 VoEId(_instanceId,_channelId),
704 "Channel::RecordFileEnded() => output file recorder module is"
705 " shutdown");
706}
707
pbos@webrtc.org92135212013-05-14 08:31:39 +0000708Channel::Channel(int32_t channelId,
minyue@webrtc.orge509f942013-09-12 17:03:00 +0000709 uint32_t instanceId,
710 const Config& config) :
niklase@google.com470e71d2011-07-07 08:21:25 +0000711 _fileCritSect(*CriticalSectionWrapper::CreateCriticalSection()),
712 _callbackCritSect(*CriticalSectionWrapper::CreateCriticalSection()),
wu@webrtc.org63420662013-10-17 18:28:55 +0000713 volume_settings_critsect_(*CriticalSectionWrapper::CreateCriticalSection()),
niklase@google.com470e71d2011-07-07 08:21:25 +0000714 _instanceId(instanceId),
xians@google.com22963ab2011-08-03 12:40:23 +0000715 _channelId(channelId),
stefan@webrtc.orga5cb98c2013-05-29 12:12:51 +0000716 rtp_header_parser_(RtpHeaderParser::Create()),
wu@webrtc.org822fbd82013-08-15 23:38:54 +0000717 rtp_payload_registry_(
andresp@webrtc.orgdc80bae2014-04-08 11:06:12 +0000718 new RTPPayloadRegistry(RTPPayloadStrategy::CreateStrategy(true))),
wu@webrtc.org822fbd82013-08-15 23:38:54 +0000719 rtp_receive_statistics_(ReceiveStatistics::Create(
720 Clock::GetRealTimeClock())),
721 rtp_receiver_(RtpReceiver::CreateAudioReceiver(
722 VoEModuleId(instanceId, channelId), Clock::GetRealTimeClock(), this,
723 this, this, rtp_payload_registry_.get())),
724 telephone_event_handler_(rtp_receiver_->GetTelephoneEventHandler()),
henrik.lundin@webrtc.org34fe0152014-04-22 19:04:34 +0000725 audio_coding_(AudioCodingModule::Create(
xians@google.com22963ab2011-08-03 12:40:23 +0000726 VoEModuleId(instanceId, channelId))),
niklase@google.com470e71d2011-07-07 08:21:25 +0000727 _rtpDumpIn(*RtpDump::CreateRtpDump()),
728 _rtpDumpOut(*RtpDump::CreateRtpDump()),
niklase@google.com470e71d2011-07-07 08:21:25 +0000729 _outputAudioLevel(),
niklase@google.com470e71d2011-07-07 08:21:25 +0000730 _externalTransport(false),
niklase@google.com470e71d2011-07-07 08:21:25 +0000731 _inputFilePlayerPtr(NULL),
732 _outputFilePlayerPtr(NULL),
733 _outputFileRecorderPtr(NULL),
734 // Avoid conflict with other channels by adding 1024 - 1026,
735 // won't use as much as 1024 channels.
736 _inputFilePlayerId(VoEModuleId(instanceId, channelId) + 1024),
737 _outputFilePlayerId(VoEModuleId(instanceId, channelId) + 1025),
738 _outputFileRecorderId(VoEModuleId(instanceId, channelId) + 1026),
niklase@google.com470e71d2011-07-07 08:21:25 +0000739 _outputFileRecording(false),
xians@google.com22963ab2011-08-03 12:40:23 +0000740 _inbandDtmfQueue(VoEModuleId(instanceId, channelId)),
741 _inbandDtmfGenerator(VoEModuleId(instanceId, channelId)),
xians@google.com22963ab2011-08-03 12:40:23 +0000742 _outputExternalMedia(false),
niklase@google.com470e71d2011-07-07 08:21:25 +0000743 _inputExternalMediaCallbackPtr(NULL),
744 _outputExternalMediaCallbackPtr(NULL),
xians@google.com22963ab2011-08-03 12:40:23 +0000745 _timeStamp(0), // This is just an offset, RTP module will add it's own random offset
746 _sendTelephoneEventPayloadType(106),
stefan@webrtc.org8e24d872014-09-02 18:58:24 +0000747 ntp_estimator_(Clock::GetRealTimeClock()),
turaj@webrtc.org167b6df2013-12-13 21:05:07 +0000748 jitter_buffer_playout_timestamp_(0),
pwestin@webrtc.org1de01352013-04-11 20:23:35 +0000749 playout_timestamp_rtp_(0),
750 playout_timestamp_rtcp_(0),
sprang@webrtc.org54ae4ff2013-12-19 13:26:02 +0000751 playout_delay_ms_(0),
xians@google.com22963ab2011-08-03 12:40:23 +0000752 _numberOfDiscardedPackets(0),
xians@webrtc.org09e8c472013-07-31 16:30:19 +0000753 send_sequence_number_(0),
wu@webrtc.orgcb711f72014-05-19 17:39:11 +0000754 ts_stats_lock_(CriticalSectionWrapper::CreateCriticalSection()),
wu@webrtc.org94454b72014-06-05 20:34:08 +0000755 rtp_ts_wraparound_handler_(new rtc::TimestampWrapAroundHandler()),
756 capture_start_rtp_time_stamp_(-1),
wu@webrtc.orgcb711f72014-05-19 17:39:11 +0000757 capture_start_ntp_time_ms_(-1),
xians@google.com22963ab2011-08-03 12:40:23 +0000758 _engineStatisticsPtr(NULL),
henrika@webrtc.org2919e952012-01-31 08:45:03 +0000759 _outputMixerPtr(NULL),
760 _transmitMixerPtr(NULL),
xians@google.com22963ab2011-08-03 12:40:23 +0000761 _moduleProcessThreadPtr(NULL),
762 _audioDeviceModulePtr(NULL),
763 _voiceEngineObserverPtr(NULL),
764 _callbackCritSectPtr(NULL),
765 _transportPtr(NULL),
xians@google.com22963ab2011-08-03 12:40:23 +0000766 _rxVadObserverPtr(NULL),
767 _oldVadDecision(-1),
768 _sendFrameType(0),
roosa@google.com1b60ceb2012-12-12 23:00:29 +0000769 _externalMixing(false),
xians@google.com22963ab2011-08-03 12:40:23 +0000770 _mixFileWithMicrophone(false),
niklase@google.com470e71d2011-07-07 08:21:25 +0000771 _mute(false),
772 _panLeft(1.0f),
773 _panRight(1.0f),
774 _outputGain(1.0f),
775 _playOutbandDtmfEvent(false),
776 _playInbandDtmfEvent(false),
niklase@google.com470e71d2011-07-07 08:21:25 +0000777 _lastLocalTimeStamp(0),
778 _lastPayloadType(0),
xians@google.com22963ab2011-08-03 12:40:23 +0000779 _includeAudioLevelIndication(false),
niklase@google.com470e71d2011-07-07 08:21:25 +0000780 _outputSpeechType(AudioFrame::kNormalSpeech),
solenberg@webrtc.orgb1f50102014-03-24 10:38:25 +0000781 vie_network_(NULL),
782 video_channel_(-1),
pwestin@webrtc.org1de01352013-04-11 20:23:35 +0000783 _average_jitter_buffer_delay_us(0),
turaj@webrtc.orge46c8d32013-05-22 20:39:43 +0000784 least_required_delay_ms_(0),
niklase@google.com470e71d2011-07-07 08:21:25 +0000785 _previousTimestamp(0),
786 _recPacketDelayMs(20),
787 _RxVadDetection(false),
niklase@google.com470e71d2011-07-07 08:21:25 +0000788 _rxAgcIsEnabled(false),
stefan@webrtc.org7bb8f022013-09-06 13:40:11 +0000789 _rxNsIsEnabled(false),
minyue@webrtc.orgc1a40a72014-05-28 09:52:06 +0000790 restored_packet_in_use_(false),
791 bitrate_controller_(
792 BitrateController::CreateBitrateController(Clock::GetRealTimeClock(),
793 true)),
794 rtcp_bandwidth_observer_(
795 bitrate_controller_->CreateRtcpBandwidthObserver()),
minyue@webrtc.org74aaf292014-07-16 21:28:26 +0000796 send_bitrate_observer_(new VoEBitrateObserver(this)),
797 network_predictor_(new NetworkPredictor(Clock::GetRealTimeClock()))
niklase@google.com470e71d2011-07-07 08:21:25 +0000798{
799 WEBRTC_TRACE(kTraceMemory, kTraceVoice, VoEId(_instanceId,_channelId),
800 "Channel::Channel() - ctor");
801 _inbandDtmfQueue.ResetDtmf();
802 _inbandDtmfGenerator.Init();
803 _outputAudioLevel.Clear();
804
pwestin@webrtc.org2853dde2012-05-11 11:08:54 +0000805 RtpRtcp::Configuration configuration;
806 configuration.id = VoEModuleId(instanceId, channelId);
807 configuration.audio = true;
pwestin@webrtc.org2853dde2012-05-11 11:08:54 +0000808 configuration.outgoing_transport = this;
pwestin@webrtc.org2853dde2012-05-11 11:08:54 +0000809 configuration.audio_messages = this;
wu@webrtc.org822fbd82013-08-15 23:38:54 +0000810 configuration.receive_statistics = rtp_receive_statistics_.get();
minyue@webrtc.orgc1a40a72014-05-28 09:52:06 +0000811 configuration.bandwidth_callback = rtcp_bandwidth_observer_.get();
pwestin@webrtc.org2853dde2012-05-11 11:08:54 +0000812
813 _rtpRtcpModule.reset(RtpRtcp::CreateRtpRtcp(configuration));
sprang@webrtc.org54ae4ff2013-12-19 13:26:02 +0000814
815 statistics_proxy_.reset(new StatisticsProxy(_rtpRtcpModule->SSRC()));
816 rtp_receive_statistics_->RegisterRtcpStatisticsCallback(
817 statistics_proxy_.get());
aluebs@webrtc.orgf927fd62014-04-16 11:58:18 +0000818
819 Config audioproc_config;
820 audioproc_config.Set<ExperimentalAgc>(new ExperimentalAgc(false));
821 rx_audioproc_.reset(AudioProcessing::Create(audioproc_config));
niklase@google.com470e71d2011-07-07 08:21:25 +0000822}
823
824Channel::~Channel()
825{
sprang@webrtc.org54ae4ff2013-12-19 13:26:02 +0000826 rtp_receive_statistics_->RegisterRtcpStatisticsCallback(NULL);
niklase@google.com470e71d2011-07-07 08:21:25 +0000827 WEBRTC_TRACE(kTraceMemory, kTraceVoice, VoEId(_instanceId,_channelId),
828 "Channel::~Channel() - dtor");
829
830 if (_outputExternalMedia)
831 {
832 DeRegisterExternalMediaProcessing(kPlaybackPerChannel);
833 }
henrika@webrtc.org944cbeb2014-03-18 10:32:33 +0000834 if (channel_state_.Get().input_external_media)
niklase@google.com470e71d2011-07-07 08:21:25 +0000835 {
836 DeRegisterExternalMediaProcessing(kRecordingPerChannel);
837 }
838 StopSend();
niklase@google.com470e71d2011-07-07 08:21:25 +0000839 StopPlayout();
840
841 {
mflodman@webrtc.org9a065d12012-03-07 08:12:21 +0000842 CriticalSectionScoped cs(&_fileCritSect);
niklase@google.com470e71d2011-07-07 08:21:25 +0000843 if (_inputFilePlayerPtr)
844 {
845 _inputFilePlayerPtr->RegisterModuleFileCallback(NULL);
846 _inputFilePlayerPtr->StopPlayingFile();
847 FilePlayer::DestroyFilePlayer(_inputFilePlayerPtr);
848 _inputFilePlayerPtr = NULL;
849 }
850 if (_outputFilePlayerPtr)
851 {
852 _outputFilePlayerPtr->RegisterModuleFileCallback(NULL);
853 _outputFilePlayerPtr->StopPlayingFile();
854 FilePlayer::DestroyFilePlayer(_outputFilePlayerPtr);
855 _outputFilePlayerPtr = NULL;
856 }
857 if (_outputFileRecorderPtr)
858 {
859 _outputFileRecorderPtr->RegisterModuleFileCallback(NULL);
860 _outputFileRecorderPtr->StopRecording();
861 FileRecorder::DestroyFileRecorder(_outputFileRecorderPtr);
862 _outputFileRecorderPtr = NULL;
863 }
864 }
865
866 // The order to safely shutdown modules in a channel is:
867 // 1. De-register callbacks in modules
868 // 2. De-register modules in process thread
869 // 3. Destroy modules
andrew@webrtc.orgeb524d92013-09-23 23:02:24 +0000870 if (audio_coding_->RegisterTransportCallback(NULL) == -1)
niklase@google.com470e71d2011-07-07 08:21:25 +0000871 {
872 WEBRTC_TRACE(kTraceWarning, kTraceVoice,
873 VoEId(_instanceId,_channelId),
874 "~Channel() failed to de-register transport callback"
875 " (Audio coding module)");
876 }
andrew@webrtc.orgeb524d92013-09-23 23:02:24 +0000877 if (audio_coding_->RegisterVADCallback(NULL) == -1)
niklase@google.com470e71d2011-07-07 08:21:25 +0000878 {
879 WEBRTC_TRACE(kTraceWarning, kTraceVoice,
880 VoEId(_instanceId,_channelId),
881 "~Channel() failed to de-register VAD callback"
882 " (Audio coding module)");
883 }
niklase@google.com470e71d2011-07-07 08:21:25 +0000884 // De-register modules in process thread
pwestin@webrtc.org2853dde2012-05-11 11:08:54 +0000885 if (_moduleProcessThreadPtr->DeRegisterModule(_rtpRtcpModule.get()) == -1)
niklase@google.com470e71d2011-07-07 08:21:25 +0000886 {
887 WEBRTC_TRACE(kTraceInfo, kTraceVoice,
888 VoEId(_instanceId,_channelId),
889 "~Channel() failed to deregister RTP/RTCP module");
890 }
niklase@google.com470e71d2011-07-07 08:21:25 +0000891 // End of modules shutdown
892
893 // Delete other objects
solenberg@webrtc.orgb1f50102014-03-24 10:38:25 +0000894 if (vie_network_) {
895 vie_network_->Release();
896 vie_network_ = NULL;
897 }
niklase@google.com470e71d2011-07-07 08:21:25 +0000898 RtpDump::DestroyRtpDump(&_rtpDumpIn);
899 RtpDump::DestroyRtpDump(&_rtpDumpOut);
niklase@google.com470e71d2011-07-07 08:21:25 +0000900 delete &_callbackCritSect;
niklase@google.com470e71d2011-07-07 08:21:25 +0000901 delete &_fileCritSect;
wu@webrtc.org63420662013-10-17 18:28:55 +0000902 delete &volume_settings_critsect_;
niklase@google.com470e71d2011-07-07 08:21:25 +0000903}
904
pbos@webrtc.org6141e132013-04-09 10:09:10 +0000905int32_t
niklase@google.com470e71d2011-07-07 08:21:25 +0000906Channel::Init()
907{
908 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId,_channelId),
909 "Channel::Init()");
910
henrika@webrtc.org944cbeb2014-03-18 10:32:33 +0000911 channel_state_.Reset();
912
niklase@google.com470e71d2011-07-07 08:21:25 +0000913 // --- Initial sanity
914
915 if ((_engineStatisticsPtr == NULL) ||
916 (_moduleProcessThreadPtr == NULL))
917 {
918 WEBRTC_TRACE(kTraceError, kTraceVoice,
919 VoEId(_instanceId,_channelId),
920 "Channel::Init() must call SetEngineInformation() first");
921 return -1;
922 }
923
924 // --- Add modules to process thread (for periodic schedulation)
925
926 const bool processThreadFail =
pwestin@webrtc.org2853dde2012-05-11 11:08:54 +0000927 ((_moduleProcessThreadPtr->RegisterModule(_rtpRtcpModule.get()) != 0) ||
niklase@google.com470e71d2011-07-07 08:21:25 +0000928 false);
niklase@google.com470e71d2011-07-07 08:21:25 +0000929 if (processThreadFail)
930 {
931 _engineStatisticsPtr->SetLastError(
932 VE_CANNOT_INIT_CHANNEL, kTraceError,
933 "Channel::Init() modules not registered");
934 return -1;
935 }
pwestin@webrtc.orgc450a192012-01-04 15:00:12 +0000936 // --- ACM initialization
niklase@google.com470e71d2011-07-07 08:21:25 +0000937
andrew@webrtc.orgeb524d92013-09-23 23:02:24 +0000938 if ((audio_coding_->InitializeReceiver() == -1) ||
niklase@google.com470e71d2011-07-07 08:21:25 +0000939#ifdef WEBRTC_CODEC_AVT
940 // out-of-band Dtmf tones are played out by default
andrew@webrtc.orgeb524d92013-09-23 23:02:24 +0000941 (audio_coding_->SetDtmfPlayoutStatus(true) == -1) ||
niklase@google.com470e71d2011-07-07 08:21:25 +0000942#endif
andrew@webrtc.orgeb524d92013-09-23 23:02:24 +0000943 (audio_coding_->InitializeSender() == -1))
niklase@google.com470e71d2011-07-07 08:21:25 +0000944 {
945 _engineStatisticsPtr->SetLastError(
946 VE_AUDIO_CODING_MODULE_ERROR, kTraceError,
947 "Channel::Init() unable to initialize the ACM - 1");
948 return -1;
949 }
950
951 // --- RTP/RTCP module initialization
952
953 // Ensure that RTCP is enabled by default for the created channel.
954 // Note that, the module will keep generating RTCP until it is explicitly
955 // disabled by the user.
956 // After StopListen (when no sockets exists), RTCP packets will no longer
957 // be transmitted since the Transport object will then be invalid.
wu@webrtc.org822fbd82013-08-15 23:38:54 +0000958 telephone_event_handler_->SetTelephoneEventForwardToDecoder(true);
959 // RTCP is enabled by default.
pbos@webrtc.orgd16e8392014-12-19 13:49:55 +0000960 _rtpRtcpModule->SetRTCPStatus(kRtcpCompound);
961 // --- Register all permanent callbacks
niklase@google.com470e71d2011-07-07 08:21:25 +0000962 const bool fail =
andrew@webrtc.orgeb524d92013-09-23 23:02:24 +0000963 (audio_coding_->RegisterTransportCallback(this) == -1) ||
964 (audio_coding_->RegisterVADCallback(this) == -1);
niklase@google.com470e71d2011-07-07 08:21:25 +0000965
966 if (fail)
967 {
968 _engineStatisticsPtr->SetLastError(
969 VE_CANNOT_INIT_CHANNEL, kTraceError,
970 "Channel::Init() callbacks not registered");
971 return -1;
972 }
973
974 // --- Register all supported codecs to the receiving side of the
975 // RTP/RTCP module
976
977 CodecInst codec;
pbos@webrtc.org6141e132013-04-09 10:09:10 +0000978 const uint8_t nSupportedCodecs = AudioCodingModule::NumberOfCodecs();
niklase@google.com470e71d2011-07-07 08:21:25 +0000979
980 for (int idx = 0; idx < nSupportedCodecs; idx++)
981 {
982 // Open up the RTP/RTCP receiver for all supported codecs
andrew@webrtc.orgeb524d92013-09-23 23:02:24 +0000983 if ((audio_coding_->Codec(idx, &codec) == -1) ||
wu@webrtc.org822fbd82013-08-15 23:38:54 +0000984 (rtp_receiver_->RegisterReceivePayload(
985 codec.plname,
986 codec.pltype,
987 codec.plfreq,
988 codec.channels,
989 (codec.rate < 0) ? 0 : codec.rate) == -1))
niklase@google.com470e71d2011-07-07 08:21:25 +0000990 {
991 WEBRTC_TRACE(kTraceWarning, kTraceVoice,
992 VoEId(_instanceId,_channelId),
993 "Channel::Init() unable to register %s (%d/%d/%d/%d) "
994 "to RTP/RTCP receiver",
995 codec.plname, codec.pltype, codec.plfreq,
996 codec.channels, codec.rate);
997 }
998 else
999 {
1000 WEBRTC_TRACE(kTraceInfo, kTraceVoice,
1001 VoEId(_instanceId,_channelId),
1002 "Channel::Init() %s (%d/%d/%d/%d) has been added to "
1003 "the RTP/RTCP receiver",
1004 codec.plname, codec.pltype, codec.plfreq,
1005 codec.channels, codec.rate);
1006 }
1007
1008 // Ensure that PCMU is used as default codec on the sending side
tina.legrand@webrtc.org45175852012-06-01 09:27:35 +00001009 if (!STR_CASE_CMP(codec.plname, "PCMU") && (codec.channels == 1))
niklase@google.com470e71d2011-07-07 08:21:25 +00001010 {
1011 SetSendCodec(codec);
1012 }
1013
1014 // Register default PT for outband 'telephone-event'
1015 if (!STR_CASE_CMP(codec.plname, "telephone-event"))
1016 {
pwestin@webrtc.org2853dde2012-05-11 11:08:54 +00001017 if ((_rtpRtcpModule->RegisterSendPayload(codec) == -1) ||
andrew@webrtc.orgeb524d92013-09-23 23:02:24 +00001018 (audio_coding_->RegisterReceiveCodec(codec) == -1))
niklase@google.com470e71d2011-07-07 08:21:25 +00001019 {
1020 WEBRTC_TRACE(kTraceWarning, kTraceVoice,
1021 VoEId(_instanceId,_channelId),
1022 "Channel::Init() failed to register outband "
1023 "'telephone-event' (%d/%d) correctly",
1024 codec.pltype, codec.plfreq);
1025 }
1026 }
1027
1028 if (!STR_CASE_CMP(codec.plname, "CN"))
1029 {
andrew@webrtc.orgeb524d92013-09-23 23:02:24 +00001030 if ((audio_coding_->RegisterSendCodec(codec) == -1) ||
1031 (audio_coding_->RegisterReceiveCodec(codec) == -1) ||
pwestin@webrtc.org2853dde2012-05-11 11:08:54 +00001032 (_rtpRtcpModule->RegisterSendPayload(codec) == -1))
niklase@google.com470e71d2011-07-07 08:21:25 +00001033 {
1034 WEBRTC_TRACE(kTraceWarning, kTraceVoice,
1035 VoEId(_instanceId,_channelId),
1036 "Channel::Init() failed to register CN (%d/%d) "
1037 "correctly - 1",
1038 codec.pltype, codec.plfreq);
1039 }
1040 }
1041#ifdef WEBRTC_CODEC_RED
1042 // Register RED to the receiving side of the ACM.
1043 // We will not receive an OnInitializeDecoder() callback for RED.
1044 if (!STR_CASE_CMP(codec.plname, "RED"))
1045 {
andrew@webrtc.orgeb524d92013-09-23 23:02:24 +00001046 if (audio_coding_->RegisterReceiveCodec(codec) == -1)
niklase@google.com470e71d2011-07-07 08:21:25 +00001047 {
1048 WEBRTC_TRACE(kTraceWarning, kTraceVoice,
1049 VoEId(_instanceId,_channelId),
1050 "Channel::Init() failed to register RED (%d/%d) "
1051 "correctly",
1052 codec.pltype, codec.plfreq);
1053 }
1054 }
1055#endif
1056 }
pwestin@webrtc.org684f0572013-03-13 23:20:57 +00001057
andrew@webrtc.org6c264cc2013-10-04 17:54:09 +00001058 if (rx_audioproc_->noise_suppression()->set_level(kDefaultNsMode) != 0) {
1059 LOG_FERR1(LS_ERROR, noise_suppression()->set_level, kDefaultNsMode);
1060 return -1;
niklase@google.com470e71d2011-07-07 08:21:25 +00001061 }
andrew@webrtc.org6c264cc2013-10-04 17:54:09 +00001062 if (rx_audioproc_->gain_control()->set_mode(kDefaultRxAgcMode) != 0) {
1063 LOG_FERR1(LS_ERROR, gain_control()->set_mode, kDefaultRxAgcMode);
1064 return -1;
niklase@google.com470e71d2011-07-07 08:21:25 +00001065 }
1066
1067 return 0;
1068}
1069
pbos@webrtc.org6141e132013-04-09 10:09:10 +00001070int32_t
niklase@google.com470e71d2011-07-07 08:21:25 +00001071Channel::SetEngineInformation(Statistics& engineStatistics,
1072 OutputMixer& outputMixer,
1073 voe::TransmitMixer& transmitMixer,
1074 ProcessThread& moduleProcessThread,
1075 AudioDeviceModule& audioDeviceModule,
1076 VoiceEngineObserver* voiceEngineObserver,
1077 CriticalSectionWrapper* callbackCritSect)
1078{
1079 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId,_channelId),
1080 "Channel::SetEngineInformation()");
1081 _engineStatisticsPtr = &engineStatistics;
1082 _outputMixerPtr = &outputMixer;
1083 _transmitMixerPtr = &transmitMixer,
1084 _moduleProcessThreadPtr = &moduleProcessThread;
1085 _audioDeviceModulePtr = &audioDeviceModule;
1086 _voiceEngineObserverPtr = voiceEngineObserver;
1087 _callbackCritSectPtr = callbackCritSect;
1088 return 0;
1089}
1090
pbos@webrtc.org6141e132013-04-09 10:09:10 +00001091int32_t
niklase@google.com470e71d2011-07-07 08:21:25 +00001092Channel::UpdateLocalTimeStamp()
1093{
1094
andrew@webrtc.org63a50982012-05-02 23:56:37 +00001095 _timeStamp += _audioFrame.samples_per_channel_;
niklase@google.com470e71d2011-07-07 08:21:25 +00001096 return 0;
1097}
1098
pbos@webrtc.org6141e132013-04-09 10:09:10 +00001099int32_t
niklase@google.com470e71d2011-07-07 08:21:25 +00001100Channel::StartPlayout()
1101{
1102 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId,_channelId),
1103 "Channel::StartPlayout()");
henrika@webrtc.org944cbeb2014-03-18 10:32:33 +00001104 if (channel_state_.Get().playing)
niklase@google.com470e71d2011-07-07 08:21:25 +00001105 {
1106 return 0;
1107 }
roosa@google.com1b60ceb2012-12-12 23:00:29 +00001108
1109 if (!_externalMixing) {
1110 // Add participant as candidates for mixing.
1111 if (_outputMixerPtr->SetMixabilityStatus(*this, true) != 0)
1112 {
1113 _engineStatisticsPtr->SetLastError(
1114 VE_AUDIO_CONF_MIX_MODULE_ERROR, kTraceError,
1115 "StartPlayout() failed to add participant to mixer");
1116 return -1;
1117 }
niklase@google.com470e71d2011-07-07 08:21:25 +00001118 }
1119
henrika@webrtc.org944cbeb2014-03-18 10:32:33 +00001120 channel_state_.SetPlaying(true);
braveyao@webrtc.orgab129902012-06-04 03:26:39 +00001121 if (RegisterFilePlayingToMixer() != 0)
1122 return -1;
1123
niklase@google.com470e71d2011-07-07 08:21:25 +00001124 return 0;
1125}
1126
pbos@webrtc.org6141e132013-04-09 10:09:10 +00001127int32_t
niklase@google.com470e71d2011-07-07 08:21:25 +00001128Channel::StopPlayout()
1129{
1130 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId,_channelId),
1131 "Channel::StopPlayout()");
henrika@webrtc.org944cbeb2014-03-18 10:32:33 +00001132 if (!channel_state_.Get().playing)
niklase@google.com470e71d2011-07-07 08:21:25 +00001133 {
1134 return 0;
1135 }
roosa@google.com1b60ceb2012-12-12 23:00:29 +00001136
1137 if (!_externalMixing) {
1138 // Remove participant as candidates for mixing
1139 if (_outputMixerPtr->SetMixabilityStatus(*this, false) != 0)
1140 {
1141 _engineStatisticsPtr->SetLastError(
1142 VE_AUDIO_CONF_MIX_MODULE_ERROR, kTraceError,
1143 "StopPlayout() failed to remove participant from mixer");
1144 return -1;
1145 }
niklase@google.com470e71d2011-07-07 08:21:25 +00001146 }
1147
henrika@webrtc.org944cbeb2014-03-18 10:32:33 +00001148 channel_state_.SetPlaying(false);
niklase@google.com470e71d2011-07-07 08:21:25 +00001149 _outputAudioLevel.Clear();
1150
1151 return 0;
1152}
1153
pbos@webrtc.org6141e132013-04-09 10:09:10 +00001154int32_t
niklase@google.com470e71d2011-07-07 08:21:25 +00001155Channel::StartSend()
1156{
1157 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId,_channelId),
1158 "Channel::StartSend()");
xians@webrtc.org09e8c472013-07-31 16:30:19 +00001159 // Resume the previous sequence number which was reset by StopSend().
henrika@webrtc.org944cbeb2014-03-18 10:32:33 +00001160 // This needs to be done before |sending| is set to true.
xians@webrtc.org09e8c472013-07-31 16:30:19 +00001161 if (send_sequence_number_)
1162 SetInitSequenceNumber(send_sequence_number_);
1163
henrika@webrtc.org944cbeb2014-03-18 10:32:33 +00001164 if (channel_state_.Get().sending)
niklase@google.com470e71d2011-07-07 08:21:25 +00001165 {
henrika@webrtc.org944cbeb2014-03-18 10:32:33 +00001166 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +00001167 }
henrika@webrtc.org944cbeb2014-03-18 10:32:33 +00001168 channel_state_.SetSending(true);
xians@webrtc.orge07247a2011-11-28 16:31:28 +00001169
pwestin@webrtc.org2853dde2012-05-11 11:08:54 +00001170 if (_rtpRtcpModule->SetSendingStatus(true) != 0)
niklase@google.com470e71d2011-07-07 08:21:25 +00001171 {
1172 _engineStatisticsPtr->SetLastError(
1173 VE_RTP_RTCP_MODULE_ERROR, kTraceError,
1174 "StartSend() RTP/RTCP failed to start sending");
mflodman@webrtc.org9a065d12012-03-07 08:12:21 +00001175 CriticalSectionScoped cs(&_callbackCritSect);
henrika@webrtc.org944cbeb2014-03-18 10:32:33 +00001176 channel_state_.SetSending(false);
niklase@google.com470e71d2011-07-07 08:21:25 +00001177 return -1;
1178 }
xians@webrtc.orge07247a2011-11-28 16:31:28 +00001179
niklase@google.com470e71d2011-07-07 08:21:25 +00001180 return 0;
1181}
1182
pbos@webrtc.org6141e132013-04-09 10:09:10 +00001183int32_t
niklase@google.com470e71d2011-07-07 08:21:25 +00001184Channel::StopSend()
1185{
1186 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId,_channelId),
1187 "Channel::StopSend()");
henrika@webrtc.org944cbeb2014-03-18 10:32:33 +00001188 if (!channel_state_.Get().sending)
niklase@google.com470e71d2011-07-07 08:21:25 +00001189 {
henrika@webrtc.org944cbeb2014-03-18 10:32:33 +00001190 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +00001191 }
henrika@webrtc.org944cbeb2014-03-18 10:32:33 +00001192 channel_state_.SetSending(false);
xians@webrtc.orge07247a2011-11-28 16:31:28 +00001193
xians@webrtc.org09e8c472013-07-31 16:30:19 +00001194 // Store the sequence number to be able to pick up the same sequence for
1195 // the next StartSend(). This is needed for restarting device, otherwise
1196 // it might cause libSRTP to complain about packets being replayed.
1197 // TODO(xians): Remove this workaround after RtpRtcpModule's refactoring
1198 // CL is landed. See issue
1199 // https://code.google.com/p/webrtc/issues/detail?id=2111 .
1200 send_sequence_number_ = _rtpRtcpModule->SequenceNumber();
1201
niklase@google.com470e71d2011-07-07 08:21:25 +00001202 // Reset sending SSRC and sequence number and triggers direct transmission
1203 // of RTCP BYE
pwestin@webrtc.org2853dde2012-05-11 11:08:54 +00001204 if (_rtpRtcpModule->SetSendingStatus(false) == -1 ||
1205 _rtpRtcpModule->ResetSendDataCountersRTP() == -1)
niklase@google.com470e71d2011-07-07 08:21:25 +00001206 {
1207 _engineStatisticsPtr->SetLastError(
1208 VE_RTP_RTCP_MODULE_ERROR, kTraceWarning,
1209 "StartSend() RTP/RTCP failed to stop sending");
1210 }
1211
niklase@google.com470e71d2011-07-07 08:21:25 +00001212 return 0;
1213}
1214
pbos@webrtc.org6141e132013-04-09 10:09:10 +00001215int32_t
niklase@google.com470e71d2011-07-07 08:21:25 +00001216Channel::StartReceiving()
1217{
1218 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId,_channelId),
1219 "Channel::StartReceiving()");
henrika@webrtc.org944cbeb2014-03-18 10:32:33 +00001220 if (channel_state_.Get().receiving)
niklase@google.com470e71d2011-07-07 08:21:25 +00001221 {
1222 return 0;
1223 }
henrika@webrtc.org944cbeb2014-03-18 10:32:33 +00001224 channel_state_.SetReceiving(true);
niklase@google.com470e71d2011-07-07 08:21:25 +00001225 _numberOfDiscardedPackets = 0;
1226 return 0;
1227}
1228
pbos@webrtc.org6141e132013-04-09 10:09:10 +00001229int32_t
niklase@google.com470e71d2011-07-07 08:21:25 +00001230Channel::StopReceiving()
1231{
1232 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId,_channelId),
1233 "Channel::StopReceiving()");
henrika@webrtc.org944cbeb2014-03-18 10:32:33 +00001234 if (!channel_state_.Get().receiving)
niklase@google.com470e71d2011-07-07 08:21:25 +00001235 {
1236 return 0;
1237 }
pwestin@webrtc.org684f0572013-03-13 23:20:57 +00001238
henrika@webrtc.org944cbeb2014-03-18 10:32:33 +00001239 channel_state_.SetReceiving(false);
niklase@google.com470e71d2011-07-07 08:21:25 +00001240 return 0;
1241}
1242
pbos@webrtc.org6141e132013-04-09 10:09:10 +00001243int32_t
niklase@google.com470e71d2011-07-07 08:21:25 +00001244Channel::RegisterVoiceEngineObserver(VoiceEngineObserver& observer)
1245{
1246 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId,_channelId),
1247 "Channel::RegisterVoiceEngineObserver()");
mflodman@webrtc.org9a065d12012-03-07 08:12:21 +00001248 CriticalSectionScoped cs(&_callbackCritSect);
niklase@google.com470e71d2011-07-07 08:21:25 +00001249
1250 if (_voiceEngineObserverPtr)
1251 {
1252 _engineStatisticsPtr->SetLastError(
1253 VE_INVALID_OPERATION, kTraceError,
1254 "RegisterVoiceEngineObserver() observer already enabled");
1255 return -1;
1256 }
1257 _voiceEngineObserverPtr = &observer;
1258 return 0;
1259}
1260
pbos@webrtc.org6141e132013-04-09 10:09:10 +00001261int32_t
niklase@google.com470e71d2011-07-07 08:21:25 +00001262Channel::DeRegisterVoiceEngineObserver()
1263{
1264 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId,_channelId),
1265 "Channel::DeRegisterVoiceEngineObserver()");
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, kTraceWarning,
1272 "DeRegisterVoiceEngineObserver() observer already disabled");
1273 return 0;
1274 }
1275 _voiceEngineObserverPtr = NULL;
1276 return 0;
1277}
1278
pbos@webrtc.org6141e132013-04-09 10:09:10 +00001279int32_t
niklase@google.com470e71d2011-07-07 08:21:25 +00001280Channel::GetSendCodec(CodecInst& codec)
1281{
andrew@webrtc.orgeb524d92013-09-23 23:02:24 +00001282 return (audio_coding_->SendCodec(&codec));
niklase@google.com470e71d2011-07-07 08:21:25 +00001283}
1284
pbos@webrtc.org6141e132013-04-09 10:09:10 +00001285int32_t
niklase@google.com470e71d2011-07-07 08:21:25 +00001286Channel::GetRecCodec(CodecInst& codec)
1287{
andrew@webrtc.orgeb524d92013-09-23 23:02:24 +00001288 return (audio_coding_->ReceiveCodec(&codec));
niklase@google.com470e71d2011-07-07 08:21:25 +00001289}
1290
pbos@webrtc.org6141e132013-04-09 10:09:10 +00001291int32_t
niklase@google.com470e71d2011-07-07 08:21:25 +00001292Channel::SetSendCodec(const CodecInst& codec)
1293{
1294 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId,_channelId),
1295 "Channel::SetSendCodec()");
1296
andrew@webrtc.orgeb524d92013-09-23 23:02:24 +00001297 if (audio_coding_->RegisterSendCodec(codec) != 0)
niklase@google.com470e71d2011-07-07 08:21:25 +00001298 {
1299 WEBRTC_TRACE(kTraceError, kTraceVoice, VoEId(_instanceId,_channelId),
1300 "SetSendCodec() failed to register codec to ACM");
1301 return -1;
1302 }
1303
pwestin@webrtc.org2853dde2012-05-11 11:08:54 +00001304 if (_rtpRtcpModule->RegisterSendPayload(codec) != 0)
niklase@google.com470e71d2011-07-07 08:21:25 +00001305 {
pwestin@webrtc.org2853dde2012-05-11 11:08:54 +00001306 _rtpRtcpModule->DeRegisterSendPayload(codec.pltype);
1307 if (_rtpRtcpModule->RegisterSendPayload(codec) != 0)
niklase@google.com470e71d2011-07-07 08:21:25 +00001308 {
1309 WEBRTC_TRACE(
1310 kTraceError, kTraceVoice, VoEId(_instanceId,_channelId),
1311 "SetSendCodec() failed to register codec to"
1312 " RTP/RTCP module");
1313 return -1;
1314 }
1315 }
1316
pwestin@webrtc.org2853dde2012-05-11 11:08:54 +00001317 if (_rtpRtcpModule->SetAudioPacketSize(codec.pacsize) != 0)
niklase@google.com470e71d2011-07-07 08:21:25 +00001318 {
1319 WEBRTC_TRACE(kTraceError, kTraceVoice, VoEId(_instanceId,_channelId),
1320 "SetSendCodec() failed to set audio packet size");
1321 return -1;
1322 }
1323
minyue@webrtc.orgc1a40a72014-05-28 09:52:06 +00001324 bitrate_controller_->SetBitrateObserver(send_bitrate_observer_.get(),
1325 codec.rate, 0, 0);
1326
niklase@google.com470e71d2011-07-07 08:21:25 +00001327 return 0;
1328}
1329
minyue@webrtc.orgc1a40a72014-05-28 09:52:06 +00001330void
1331Channel::OnNetworkChanged(const uint32_t bitrate_bps,
1332 const uint8_t fraction_lost, // 0 - 255.
1333 const uint32_t rtt) {
1334 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId,_channelId),
1335 "Channel::OnNetworkChanged(bitrate_bps=%d, fration_lost=%d, rtt=%d)",
1336 bitrate_bps, fraction_lost, rtt);
minyue@webrtc.org74aaf292014-07-16 21:28:26 +00001337 // |fraction_lost| from BitrateObserver is short time observation of packet
1338 // loss rate from past. We use network predictor to make a more reasonable
1339 // loss rate estimation.
1340 network_predictor_->UpdatePacketLossRate(fraction_lost);
1341 uint8_t loss_rate = network_predictor_->GetLossRate();
minyue@webrtc.orgc1a40a72014-05-28 09:52:06 +00001342 // Normalizes rate to 0 - 100.
minyue@webrtc.org74aaf292014-07-16 21:28:26 +00001343 if (audio_coding_->SetPacketLossRate(100 * loss_rate / 255) != 0) {
minyue@webrtc.orgc1a40a72014-05-28 09:52:06 +00001344 _engineStatisticsPtr->SetLastError(VE_AUDIO_CODING_MODULE_ERROR,
1345 kTraceError, "OnNetworkChanged() failed to set packet loss rate");
1346 assert(false); // This should not happen.
1347 }
1348}
1349
pbos@webrtc.org6141e132013-04-09 10:09:10 +00001350int32_t
niklase@google.com470e71d2011-07-07 08:21:25 +00001351Channel::SetVADStatus(bool enableVAD, ACMVADMode mode, bool disableDTX)
1352{
1353 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId,_channelId),
1354 "Channel::SetVADStatus(mode=%d)", mode);
1355 // To disable VAD, DTX must be disabled too
1356 disableDTX = ((enableVAD == false) ? true : disableDTX);
andrew@webrtc.orgeb524d92013-09-23 23:02:24 +00001357 if (audio_coding_->SetVAD(!disableDTX, enableVAD, mode) != 0)
niklase@google.com470e71d2011-07-07 08:21:25 +00001358 {
1359 _engineStatisticsPtr->SetLastError(
1360 VE_AUDIO_CODING_MODULE_ERROR, kTraceError,
1361 "SetVADStatus() failed to set VAD");
1362 return -1;
1363 }
1364 return 0;
1365}
1366
pbos@webrtc.org6141e132013-04-09 10:09:10 +00001367int32_t
niklase@google.com470e71d2011-07-07 08:21:25 +00001368Channel::GetVADStatus(bool& enabledVAD, ACMVADMode& mode, bool& disabledDTX)
1369{
1370 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId,_channelId),
1371 "Channel::GetVADStatus");
andrew@webrtc.orgeb524d92013-09-23 23:02:24 +00001372 if (audio_coding_->VAD(&disabledDTX, &enabledVAD, &mode) != 0)
niklase@google.com470e71d2011-07-07 08:21:25 +00001373 {
1374 _engineStatisticsPtr->SetLastError(
1375 VE_AUDIO_CODING_MODULE_ERROR, kTraceError,
1376 "GetVADStatus() failed to get VAD status");
1377 return -1;
1378 }
1379 disabledDTX = !disabledDTX;
1380 return 0;
1381}
1382
pbos@webrtc.org6141e132013-04-09 10:09:10 +00001383int32_t
niklase@google.com470e71d2011-07-07 08:21:25 +00001384Channel::SetRecPayloadType(const CodecInst& codec)
1385{
1386 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId,_channelId),
1387 "Channel::SetRecPayloadType()");
1388
henrika@webrtc.org944cbeb2014-03-18 10:32:33 +00001389 if (channel_state_.Get().playing)
niklase@google.com470e71d2011-07-07 08:21:25 +00001390 {
1391 _engineStatisticsPtr->SetLastError(
1392 VE_ALREADY_PLAYING, kTraceError,
1393 "SetRecPayloadType() unable to set PT while playing");
1394 return -1;
1395 }
henrika@webrtc.org944cbeb2014-03-18 10:32:33 +00001396 if (channel_state_.Get().receiving)
niklase@google.com470e71d2011-07-07 08:21:25 +00001397 {
1398 _engineStatisticsPtr->SetLastError(
1399 VE_ALREADY_LISTENING, kTraceError,
1400 "SetRecPayloadType() unable to set PT while listening");
1401 return -1;
1402 }
1403
1404 if (codec.pltype == -1)
1405 {
1406 // De-register the selected codec (RTP/RTCP module and ACM)
1407
pbos@webrtc.org6141e132013-04-09 10:09:10 +00001408 int8_t pltype(-1);
niklase@google.com470e71d2011-07-07 08:21:25 +00001409 CodecInst rxCodec = codec;
1410
1411 // Get payload type for the given codec
wu@webrtc.org822fbd82013-08-15 23:38:54 +00001412 rtp_payload_registry_->ReceivePayloadType(
1413 rxCodec.plname,
1414 rxCodec.plfreq,
1415 rxCodec.channels,
1416 (rxCodec.rate < 0) ? 0 : rxCodec.rate,
1417 &pltype);
niklase@google.com470e71d2011-07-07 08:21:25 +00001418 rxCodec.pltype = pltype;
1419
wu@webrtc.org822fbd82013-08-15 23:38:54 +00001420 if (rtp_receiver_->DeRegisterReceivePayload(pltype) != 0)
niklase@google.com470e71d2011-07-07 08:21:25 +00001421 {
1422 _engineStatisticsPtr->SetLastError(
1423 VE_RTP_RTCP_MODULE_ERROR,
1424 kTraceError,
1425 "SetRecPayloadType() RTP/RTCP-module deregistration "
1426 "failed");
1427 return -1;
1428 }
andrew@webrtc.orgeb524d92013-09-23 23:02:24 +00001429 if (audio_coding_->UnregisterReceiveCodec(rxCodec.pltype) != 0)
niklase@google.com470e71d2011-07-07 08:21:25 +00001430 {
1431 _engineStatisticsPtr->SetLastError(
1432 VE_AUDIO_CODING_MODULE_ERROR, kTraceError,
1433 "SetRecPayloadType() ACM deregistration failed - 1");
1434 return -1;
1435 }
1436 return 0;
1437 }
1438
wu@webrtc.org822fbd82013-08-15 23:38:54 +00001439 if (rtp_receiver_->RegisterReceivePayload(
1440 codec.plname,
1441 codec.pltype,
1442 codec.plfreq,
1443 codec.channels,
1444 (codec.rate < 0) ? 0 : codec.rate) != 0)
niklase@google.com470e71d2011-07-07 08:21:25 +00001445 {
1446 // First attempt to register failed => de-register and try again
wu@webrtc.org822fbd82013-08-15 23:38:54 +00001447 rtp_receiver_->DeRegisterReceivePayload(codec.pltype);
1448 if (rtp_receiver_->RegisterReceivePayload(
1449 codec.plname,
1450 codec.pltype,
1451 codec.plfreq,
1452 codec.channels,
1453 (codec.rate < 0) ? 0 : codec.rate) != 0)
niklase@google.com470e71d2011-07-07 08:21:25 +00001454 {
1455 _engineStatisticsPtr->SetLastError(
1456 VE_RTP_RTCP_MODULE_ERROR, kTraceError,
1457 "SetRecPayloadType() RTP/RTCP-module registration failed");
1458 return -1;
1459 }
1460 }
andrew@webrtc.orgeb524d92013-09-23 23:02:24 +00001461 if (audio_coding_->RegisterReceiveCodec(codec) != 0)
niklase@google.com470e71d2011-07-07 08:21:25 +00001462 {
andrew@webrtc.orgeb524d92013-09-23 23:02:24 +00001463 audio_coding_->UnregisterReceiveCodec(codec.pltype);
1464 if (audio_coding_->RegisterReceiveCodec(codec) != 0)
niklase@google.com470e71d2011-07-07 08:21:25 +00001465 {
1466 _engineStatisticsPtr->SetLastError(
1467 VE_AUDIO_CODING_MODULE_ERROR, kTraceError,
1468 "SetRecPayloadType() ACM registration failed - 1");
1469 return -1;
1470 }
1471 }
1472 return 0;
1473}
1474
pbos@webrtc.org6141e132013-04-09 10:09:10 +00001475int32_t
niklase@google.com470e71d2011-07-07 08:21:25 +00001476Channel::GetRecPayloadType(CodecInst& codec)
1477{
1478 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId,_channelId),
1479 "Channel::GetRecPayloadType()");
pbos@webrtc.org6141e132013-04-09 10:09:10 +00001480 int8_t payloadType(-1);
wu@webrtc.org822fbd82013-08-15 23:38:54 +00001481 if (rtp_payload_registry_->ReceivePayloadType(
1482 codec.plname,
1483 codec.plfreq,
1484 codec.channels,
1485 (codec.rate < 0) ? 0 : codec.rate,
1486 &payloadType) != 0)
niklase@google.com470e71d2011-07-07 08:21:25 +00001487 {
1488 _engineStatisticsPtr->SetLastError(
henrika@webrtc.org37198002012-06-18 11:00:12 +00001489 VE_RTP_RTCP_MODULE_ERROR, kTraceWarning,
niklase@google.com470e71d2011-07-07 08:21:25 +00001490 "GetRecPayloadType() failed to retrieve RX payload type");
1491 return -1;
1492 }
1493 codec.pltype = payloadType;
1494 WEBRTC_TRACE(kTraceStateInfo, kTraceVoice, VoEId(_instanceId,_channelId),
1495 "Channel::GetRecPayloadType() => pltype=%u", codec.pltype);
1496 return 0;
1497}
1498
pbos@webrtc.org6141e132013-04-09 10:09:10 +00001499int32_t
niklase@google.com470e71d2011-07-07 08:21:25 +00001500Channel::SetSendCNPayloadType(int type, PayloadFrequencies frequency)
1501{
1502 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId,_channelId),
1503 "Channel::SetSendCNPayloadType()");
1504
1505 CodecInst codec;
pbos@webrtc.org6141e132013-04-09 10:09:10 +00001506 int32_t samplingFreqHz(-1);
tina.legrand@webrtc.org45175852012-06-01 09:27:35 +00001507 const int kMono = 1;
niklase@google.com470e71d2011-07-07 08:21:25 +00001508 if (frequency == kFreq32000Hz)
1509 samplingFreqHz = 32000;
1510 else if (frequency == kFreq16000Hz)
1511 samplingFreqHz = 16000;
1512
andrew@webrtc.orgeb524d92013-09-23 23:02:24 +00001513 if (audio_coding_->Codec("CN", &codec, samplingFreqHz, kMono) == -1)
niklase@google.com470e71d2011-07-07 08:21:25 +00001514 {
1515 _engineStatisticsPtr->SetLastError(
1516 VE_AUDIO_CODING_MODULE_ERROR, kTraceError,
1517 "SetSendCNPayloadType() failed to retrieve default CN codec "
1518 "settings");
1519 return -1;
1520 }
1521
1522 // Modify the payload type (must be set to dynamic range)
1523 codec.pltype = type;
1524
andrew@webrtc.orgeb524d92013-09-23 23:02:24 +00001525 if (audio_coding_->RegisterSendCodec(codec) != 0)
niklase@google.com470e71d2011-07-07 08:21:25 +00001526 {
1527 _engineStatisticsPtr->SetLastError(
1528 VE_AUDIO_CODING_MODULE_ERROR, kTraceError,
1529 "SetSendCNPayloadType() failed to register CN to ACM");
1530 return -1;
1531 }
1532
pwestin@webrtc.org2853dde2012-05-11 11:08:54 +00001533 if (_rtpRtcpModule->RegisterSendPayload(codec) != 0)
niklase@google.com470e71d2011-07-07 08:21:25 +00001534 {
pwestin@webrtc.org2853dde2012-05-11 11:08:54 +00001535 _rtpRtcpModule->DeRegisterSendPayload(codec.pltype);
1536 if (_rtpRtcpModule->RegisterSendPayload(codec) != 0)
niklase@google.com470e71d2011-07-07 08:21:25 +00001537 {
1538 _engineStatisticsPtr->SetLastError(
1539 VE_RTP_RTCP_MODULE_ERROR, kTraceError,
1540 "SetSendCNPayloadType() failed to register CN to RTP/RTCP "
1541 "module");
1542 return -1;
1543 }
1544 }
1545 return 0;
1546}
1547
minyue@webrtc.orgadee8f92014-09-03 12:28:06 +00001548int Channel::SetOpusMaxPlaybackRate(int frequency_hz) {
minyue@webrtc.org6aac93b2014-08-12 08:13:33 +00001549 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId, _channelId),
minyue@webrtc.orgadee8f92014-09-03 12:28:06 +00001550 "Channel::SetOpusMaxPlaybackRate()");
minyue@webrtc.org6aac93b2014-08-12 08:13:33 +00001551
minyue@webrtc.orgadee8f92014-09-03 12:28:06 +00001552 if (audio_coding_->SetOpusMaxPlaybackRate(frequency_hz) != 0) {
minyue@webrtc.org6aac93b2014-08-12 08:13:33 +00001553 _engineStatisticsPtr->SetLastError(
1554 VE_AUDIO_CODING_MODULE_ERROR, kTraceError,
minyue@webrtc.orgadee8f92014-09-03 12:28:06 +00001555 "SetOpusMaxPlaybackRate() failed to set maximum playback rate");
minyue@webrtc.org6aac93b2014-08-12 08:13:33 +00001556 return -1;
1557 }
1558 return 0;
1559}
1560
pbos@webrtc.org6141e132013-04-09 10:09:10 +00001561int32_t Channel::RegisterExternalTransport(Transport& transport)
niklase@google.com470e71d2011-07-07 08:21:25 +00001562{
1563 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId, _channelId),
1564 "Channel::RegisterExternalTransport()");
1565
mflodman@webrtc.org9a065d12012-03-07 08:12:21 +00001566 CriticalSectionScoped cs(&_callbackCritSect);
niklase@google.com470e71d2011-07-07 08:21:25 +00001567
niklase@google.com470e71d2011-07-07 08:21:25 +00001568 if (_externalTransport)
1569 {
1570 _engineStatisticsPtr->SetLastError(VE_INVALID_OPERATION,
1571 kTraceError,
1572 "RegisterExternalTransport() external transport already enabled");
1573 return -1;
1574 }
1575 _externalTransport = true;
1576 _transportPtr = &transport;
1577 return 0;
1578}
1579
pbos@webrtc.org6141e132013-04-09 10:09:10 +00001580int32_t
niklase@google.com470e71d2011-07-07 08:21:25 +00001581Channel::DeRegisterExternalTransport()
1582{
1583 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId,_channelId),
1584 "Channel::DeRegisterExternalTransport()");
1585
mflodman@webrtc.org9a065d12012-03-07 08:12:21 +00001586 CriticalSectionScoped cs(&_callbackCritSect);
xians@webrtc.org83661f52011-11-25 10:58:15 +00001587
niklase@google.com470e71d2011-07-07 08:21:25 +00001588 if (!_transportPtr)
1589 {
1590 _engineStatisticsPtr->SetLastError(
1591 VE_INVALID_OPERATION, kTraceWarning,
1592 "DeRegisterExternalTransport() external transport already "
1593 "disabled");
1594 return 0;
1595 }
1596 _externalTransport = false;
niklase@google.com470e71d2011-07-07 08:21:25 +00001597 _transportPtr = NULL;
1598 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId,_channelId),
1599 "DeRegisterExternalTransport() all transport is disabled");
niklase@google.com470e71d2011-07-07 08:21:25 +00001600 return 0;
1601}
1602
pkasting@chromium.org4591fbd2014-11-20 22:28:14 +00001603int32_t Channel::ReceivedRTPPacket(const int8_t* data, size_t length,
solenberg@webrtc.orgb1f50102014-03-24 10:38:25 +00001604 const PacketTime& packet_time) {
pwestin@webrtc.org0c459572013-04-03 15:43:57 +00001605 WEBRTC_TRACE(kTraceStream, kTraceVoice, VoEId(_instanceId,_channelId),
1606 "Channel::ReceivedRTPPacket()");
1607
1608 // Store playout timestamp for the received RTP packet
pwestin@webrtc.org1de01352013-04-11 20:23:35 +00001609 UpdatePlayoutTimestamp(false);
pwestin@webrtc.org0c459572013-04-03 15:43:57 +00001610
1611 // Dump the RTP packet to a file (if RTP dump is enabled).
pbos@webrtc.org6141e132013-04-09 10:09:10 +00001612 if (_rtpDumpIn.DumpPacket((const uint8_t*)data,
1613 (uint16_t)length) == -1) {
pwestin@webrtc.org0c459572013-04-03 15:43:57 +00001614 WEBRTC_TRACE(kTraceWarning, kTraceVoice,
1615 VoEId(_instanceId,_channelId),
1616 "Channel::SendPacket() RTP dump to input file failed");
1617 }
stefan@webrtc.org7bb8f022013-09-06 13:40:11 +00001618 const uint8_t* received_packet = reinterpret_cast<const uint8_t*>(data);
stefan@webrtc.orga5cb98c2013-05-29 12:12:51 +00001619 RTPHeader header;
stefan@webrtc.org7bb8f022013-09-06 13:40:11 +00001620 if (!rtp_header_parser_->Parse(received_packet, length, &header)) {
1621 WEBRTC_TRACE(webrtc::kTraceDebug, webrtc::kTraceVoice, _channelId,
1622 "Incoming packet: invalid RTP header");
stefan@webrtc.orga5cb98c2013-05-29 12:12:51 +00001623 return -1;
1624 }
wu@webrtc.org822fbd82013-08-15 23:38:54 +00001625 header.payload_type_frequency =
1626 rtp_payload_registry_->GetPayloadTypeFrequency(header.payloadType);
stefan@webrtc.org7bb8f022013-09-06 13:40:11 +00001627 if (header.payload_type_frequency < 0)
wu@webrtc.org822fbd82013-08-15 23:38:54 +00001628 return -1;
stefan@webrtc.org48df3812013-11-08 15:18:52 +00001629 bool in_order = IsPacketInOrder(header);
stefan@webrtc.org7bb8f022013-09-06 13:40:11 +00001630 rtp_receive_statistics_->IncomingPacket(header, length,
stefan@webrtc.org48df3812013-11-08 15:18:52 +00001631 IsPacketRetransmitted(header, in_order));
stefan@webrtc.org7bb8f022013-09-06 13:40:11 +00001632 rtp_payload_registry_->SetIncomingPayloadType(header);
solenberg@webrtc.orgb1f50102014-03-24 10:38:25 +00001633
1634 // Forward any packets to ViE bandwidth estimator, if enabled.
1635 {
1636 CriticalSectionScoped cs(&_callbackCritSect);
1637 if (vie_network_) {
1638 int64_t arrival_time_ms;
1639 if (packet_time.timestamp != -1) {
1640 arrival_time_ms = (packet_time.timestamp + 500) / 1000;
1641 } else {
1642 arrival_time_ms = TickTime::MillisecondTimestamp();
1643 }
pkasting@chromium.org4591fbd2014-11-20 22:28:14 +00001644 size_t payload_length = length - header.headerLength;
solenberg@webrtc.orgb1f50102014-03-24 10:38:25 +00001645 vie_network_->ReceivedBWEPacket(video_channel_, arrival_time_ms,
1646 payload_length, header);
1647 }
1648 }
1649
stefan@webrtc.org48df3812013-11-08 15:18:52 +00001650 return ReceivePacket(received_packet, length, header, in_order) ? 0 : -1;
stefan@webrtc.org7bb8f022013-09-06 13:40:11 +00001651}
1652
1653bool Channel::ReceivePacket(const uint8_t* packet,
pkasting@chromium.org4591fbd2014-11-20 22:28:14 +00001654 size_t packet_length,
stefan@webrtc.org7bb8f022013-09-06 13:40:11 +00001655 const RTPHeader& header,
1656 bool in_order) {
1657 if (rtp_payload_registry_->IsEncapsulated(header)) {
1658 return HandleEncapsulation(packet, packet_length, header);
wu@webrtc.org822fbd82013-08-15 23:38:54 +00001659 }
stefan@webrtc.org7bb8f022013-09-06 13:40:11 +00001660 const uint8_t* payload = packet + header.headerLength;
pkasting@chromium.org4591fbd2014-11-20 22:28:14 +00001661 assert(packet_length >= header.headerLength);
1662 size_t payload_length = packet_length - header.headerLength;
wu@webrtc.org822fbd82013-08-15 23:38:54 +00001663 PayloadUnion payload_specific;
1664 if (!rtp_payload_registry_->GetPayloadSpecifics(header.payloadType,
stefan@webrtc.org7bb8f022013-09-06 13:40:11 +00001665 &payload_specific)) {
1666 return false;
wu@webrtc.org822fbd82013-08-15 23:38:54 +00001667 }
stefan@webrtc.org7bb8f022013-09-06 13:40:11 +00001668 return rtp_receiver_->IncomingRtpPacket(header, payload, payload_length,
1669 payload_specific, in_order);
1670}
1671
1672bool Channel::HandleEncapsulation(const uint8_t* packet,
pkasting@chromium.org4591fbd2014-11-20 22:28:14 +00001673 size_t packet_length,
stefan@webrtc.org7bb8f022013-09-06 13:40:11 +00001674 const RTPHeader& header) {
1675 if (!rtp_payload_registry_->IsRtx(header))
1676 return false;
1677
1678 // Remove the RTX header and parse the original RTP header.
1679 if (packet_length < header.headerLength)
1680 return false;
1681 if (packet_length > kVoiceEngineMaxIpPacketSizeBytes)
1682 return false;
1683 if (restored_packet_in_use_) {
1684 WEBRTC_TRACE(webrtc::kTraceDebug, webrtc::kTraceVoice, _channelId,
1685 "Multiple RTX headers detected, dropping packet");
1686 return false;
pwestin@webrtc.org0c459572013-04-03 15:43:57 +00001687 }
stefan@webrtc.org7bb8f022013-09-06 13:40:11 +00001688 uint8_t* restored_packet_ptr = restored_packet_;
1689 if (!rtp_payload_registry_->RestoreOriginalPacket(
1690 &restored_packet_ptr, packet, &packet_length, rtp_receiver_->SSRC(),
1691 header)) {
1692 WEBRTC_TRACE(webrtc::kTraceDebug, webrtc::kTraceVoice, _channelId,
1693 "Incoming RTX packet: invalid RTP header");
1694 return false;
1695 }
1696 restored_packet_in_use_ = true;
1697 bool ret = OnRecoveredPacket(restored_packet_ptr, packet_length);
1698 restored_packet_in_use_ = false;
1699 return ret;
1700}
1701
1702bool Channel::IsPacketInOrder(const RTPHeader& header) const {
1703 StreamStatistician* statistician =
1704 rtp_receive_statistics_->GetStatistician(header.ssrc);
1705 if (!statistician)
1706 return false;
1707 return statistician->IsPacketInOrder(header.sequenceNumber);
niklase@google.com470e71d2011-07-07 08:21:25 +00001708}
1709
stefan@webrtc.org48df3812013-11-08 15:18:52 +00001710bool Channel::IsPacketRetransmitted(const RTPHeader& header,
1711 bool in_order) const {
stefan@webrtc.org7bb8f022013-09-06 13:40:11 +00001712 // Retransmissions are handled separately if RTX is enabled.
1713 if (rtp_payload_registry_->RtxEnabled())
1714 return false;
1715 StreamStatistician* statistician =
1716 rtp_receive_statistics_->GetStatistician(header.ssrc);
1717 if (!statistician)
1718 return false;
1719 // Check if this is a retransmission.
1720 uint16_t min_rtt = 0;
1721 _rtpRtcpModule->RTT(rtp_receiver_->SSRC(), NULL, NULL, &min_rtt, NULL);
stefan@webrtc.org48df3812013-11-08 15:18:52 +00001722 return !in_order &&
stefan@webrtc.org7bb8f022013-09-06 13:40:11 +00001723 statistician->IsRetransmitOfOldPacket(header, min_rtt);
wu@webrtc.org822fbd82013-08-15 23:38:54 +00001724}
1725
pkasting@chromium.org4591fbd2014-11-20 22:28:14 +00001726int32_t Channel::ReceivedRTCPPacket(const int8_t* data, size_t length) {
pwestin@webrtc.org0c459572013-04-03 15:43:57 +00001727 WEBRTC_TRACE(kTraceStream, kTraceVoice, VoEId(_instanceId,_channelId),
1728 "Channel::ReceivedRTCPPacket()");
1729 // Store playout timestamp for the received RTCP packet
pwestin@webrtc.org1de01352013-04-11 20:23:35 +00001730 UpdatePlayoutTimestamp(true);
pwestin@webrtc.org0c459572013-04-03 15:43:57 +00001731
1732 // Dump the RTCP packet to a file (if RTP dump is enabled).
pkasting@chromium.org4591fbd2014-11-20 22:28:14 +00001733 if (_rtpDumpIn.DumpPacket((const uint8_t*)data, length) == -1) {
pwestin@webrtc.org0c459572013-04-03 15:43:57 +00001734 WEBRTC_TRACE(kTraceWarning, kTraceVoice,
1735 VoEId(_instanceId,_channelId),
1736 "Channel::SendPacket() RTCP dump to input file failed");
1737 }
1738
1739 // Deliver RTCP packet to RTP/RTCP module for parsing
pkasting@chromium.org4591fbd2014-11-20 22:28:14 +00001740 if (_rtpRtcpModule->IncomingRtcpPacket((const uint8_t*)data, length) == -1) {
pwestin@webrtc.org0c459572013-04-03 15:43:57 +00001741 _engineStatisticsPtr->SetLastError(
1742 VE_SOCKET_TRANSPORT_MODULE_ERROR, kTraceWarning,
1743 "Channel::IncomingRTPPacket() RTCP packet is invalid");
1744 }
wu@webrtc.org82c4b852014-05-20 22:55:01 +00001745
stefan@webrtc.org8e24d872014-09-02 18:58:24 +00001746 {
1747 CriticalSectionScoped lock(ts_stats_lock_.get());
minyue@webrtc.org2c0cdbc2014-10-09 10:52:43 +00001748 uint16_t rtt = GetRTT();
1749 if (rtt == 0) {
1750 // Waiting for valid RTT.
1751 return 0;
1752 }
1753 uint32_t ntp_secs = 0;
1754 uint32_t ntp_frac = 0;
1755 uint32_t rtp_timestamp = 0;
1756 if (0 != _rtpRtcpModule->RemoteNTP(&ntp_secs, &ntp_frac, NULL, NULL,
1757 &rtp_timestamp)) {
1758 // Waiting for RTCP.
1759 return 0;
1760 }
1761 ntp_estimator_.UpdateRtcpTimestamp(rtt, ntp_secs, ntp_frac, rtp_timestamp);
stefan@webrtc.org8e24d872014-09-02 18:58:24 +00001762 }
pwestin@webrtc.org0c459572013-04-03 15:43:57 +00001763 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +00001764}
1765
niklase@google.com470e71d2011-07-07 08:21:25 +00001766int Channel::StartPlayingFileLocally(const char* fileName,
pbos@webrtc.org92135212013-05-14 08:31:39 +00001767 bool loop,
1768 FileFormats format,
1769 int startPosition,
1770 float volumeScaling,
1771 int stopPosition,
niklase@google.com470e71d2011-07-07 08:21:25 +00001772 const CodecInst* codecInst)
1773{
1774 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId,_channelId),
1775 "Channel::StartPlayingFileLocally(fileNameUTF8[]=%s, loop=%d,"
1776 " format=%d, volumeScaling=%5.3f, startPosition=%d, "
1777 "stopPosition=%d)", fileName, loop, format, volumeScaling,
1778 startPosition, stopPosition);
1779
henrika@webrtc.org944cbeb2014-03-18 10:32:33 +00001780 if (channel_state_.Get().output_file_playing)
niklase@google.com470e71d2011-07-07 08:21:25 +00001781 {
1782 _engineStatisticsPtr->SetLastError(
1783 VE_ALREADY_PLAYING, kTraceError,
1784 "StartPlayingFileLocally() is already playing");
1785 return -1;
1786 }
1787
niklase@google.com470e71d2011-07-07 08:21:25 +00001788 {
mflodman@webrtc.org9a065d12012-03-07 08:12:21 +00001789 CriticalSectionScoped cs(&_fileCritSect);
henrike@webrtc.orgb37c6282011-10-31 23:53:04 +00001790
1791 if (_outputFilePlayerPtr)
1792 {
1793 _outputFilePlayerPtr->RegisterModuleFileCallback(NULL);
1794 FilePlayer::DestroyFilePlayer(_outputFilePlayerPtr);
1795 _outputFilePlayerPtr = NULL;
1796 }
1797
1798 _outputFilePlayerPtr = FilePlayer::CreateFilePlayer(
1799 _outputFilePlayerId, (const FileFormats)format);
1800
1801 if (_outputFilePlayerPtr == NULL)
1802 {
1803 _engineStatisticsPtr->SetLastError(
1804 VE_INVALID_ARGUMENT, kTraceError,
henrike@webrtc.org31d30702011-11-18 19:59:32 +00001805 "StartPlayingFileLocally() filePlayer format is not correct");
henrike@webrtc.orgb37c6282011-10-31 23:53:04 +00001806 return -1;
1807 }
1808
pbos@webrtc.org6141e132013-04-09 10:09:10 +00001809 const uint32_t notificationTime(0);
henrike@webrtc.orgb37c6282011-10-31 23:53:04 +00001810
1811 if (_outputFilePlayerPtr->StartPlayingFile(
1812 fileName,
1813 loop,
1814 startPosition,
1815 volumeScaling,
1816 notificationTime,
1817 stopPosition,
1818 (const CodecInst*)codecInst) != 0)
1819 {
1820 _engineStatisticsPtr->SetLastError(
1821 VE_BAD_FILE, kTraceError,
1822 "StartPlayingFile() failed to start file playout");
1823 _outputFilePlayerPtr->StopPlayingFile();
1824 FilePlayer::DestroyFilePlayer(_outputFilePlayerPtr);
1825 _outputFilePlayerPtr = NULL;
1826 return -1;
1827 }
1828 _outputFilePlayerPtr->RegisterModuleFileCallback(this);
henrika@webrtc.org944cbeb2014-03-18 10:32:33 +00001829 channel_state_.SetOutputFilePlaying(true);
niklase@google.com470e71d2011-07-07 08:21:25 +00001830 }
braveyao@webrtc.orgab129902012-06-04 03:26:39 +00001831
1832 if (RegisterFilePlayingToMixer() != 0)
henrike@webrtc.org066f9e52011-10-28 23:15:47 +00001833 return -1;
niklase@google.com470e71d2011-07-07 08:21:25 +00001834
1835 return 0;
1836}
1837
1838int Channel::StartPlayingFileLocally(InStream* stream,
pbos@webrtc.org92135212013-05-14 08:31:39 +00001839 FileFormats format,
1840 int startPosition,
1841 float volumeScaling,
1842 int stopPosition,
niklase@google.com470e71d2011-07-07 08:21:25 +00001843 const CodecInst* codecInst)
1844{
1845 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId,_channelId),
1846 "Channel::StartPlayingFileLocally(format=%d,"
1847 " volumeScaling=%5.3f, startPosition=%d, stopPosition=%d)",
1848 format, volumeScaling, startPosition, stopPosition);
1849
1850 if(stream == NULL)
1851 {
1852 _engineStatisticsPtr->SetLastError(
1853 VE_BAD_FILE, kTraceError,
1854 "StartPlayingFileLocally() NULL as input stream");
1855 return -1;
1856 }
1857
1858
henrika@webrtc.org944cbeb2014-03-18 10:32:33 +00001859 if (channel_state_.Get().output_file_playing)
niklase@google.com470e71d2011-07-07 08:21:25 +00001860 {
1861 _engineStatisticsPtr->SetLastError(
1862 VE_ALREADY_PLAYING, kTraceError,
1863 "StartPlayingFileLocally() is already playing");
1864 return -1;
1865 }
1866
niklase@google.com470e71d2011-07-07 08:21:25 +00001867 {
mflodman@webrtc.org9a065d12012-03-07 08:12:21 +00001868 CriticalSectionScoped cs(&_fileCritSect);
henrike@webrtc.orgb37c6282011-10-31 23:53:04 +00001869
1870 // Destroy the old instance
1871 if (_outputFilePlayerPtr)
1872 {
1873 _outputFilePlayerPtr->RegisterModuleFileCallback(NULL);
1874 FilePlayer::DestroyFilePlayer(_outputFilePlayerPtr);
1875 _outputFilePlayerPtr = NULL;
1876 }
1877
1878 // Create the instance
1879 _outputFilePlayerPtr = FilePlayer::CreateFilePlayer(
1880 _outputFilePlayerId,
1881 (const FileFormats)format);
1882
1883 if (_outputFilePlayerPtr == NULL)
1884 {
1885 _engineStatisticsPtr->SetLastError(
1886 VE_INVALID_ARGUMENT, kTraceError,
1887 "StartPlayingFileLocally() filePlayer format isnot correct");
1888 return -1;
1889 }
1890
pbos@webrtc.org6141e132013-04-09 10:09:10 +00001891 const uint32_t notificationTime(0);
henrike@webrtc.orgb37c6282011-10-31 23:53:04 +00001892
1893 if (_outputFilePlayerPtr->StartPlayingFile(*stream, startPosition,
1894 volumeScaling,
1895 notificationTime,
1896 stopPosition, codecInst) != 0)
1897 {
1898 _engineStatisticsPtr->SetLastError(VE_BAD_FILE, kTraceError,
1899 "StartPlayingFile() failed to "
1900 "start file playout");
1901 _outputFilePlayerPtr->StopPlayingFile();
1902 FilePlayer::DestroyFilePlayer(_outputFilePlayerPtr);
1903 _outputFilePlayerPtr = NULL;
1904 return -1;
1905 }
1906 _outputFilePlayerPtr->RegisterModuleFileCallback(this);
henrika@webrtc.org944cbeb2014-03-18 10:32:33 +00001907 channel_state_.SetOutputFilePlaying(true);
niklase@google.com470e71d2011-07-07 08:21:25 +00001908 }
braveyao@webrtc.orgab129902012-06-04 03:26:39 +00001909
1910 if (RegisterFilePlayingToMixer() != 0)
henrike@webrtc.org066f9e52011-10-28 23:15:47 +00001911 return -1;
niklase@google.com470e71d2011-07-07 08:21:25 +00001912
niklase@google.com470e71d2011-07-07 08:21:25 +00001913 return 0;
1914}
1915
1916int Channel::StopPlayingFileLocally()
1917{
1918 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId,_channelId),
1919 "Channel::StopPlayingFileLocally()");
1920
henrika@webrtc.org944cbeb2014-03-18 10:32:33 +00001921 if (!channel_state_.Get().output_file_playing)
niklase@google.com470e71d2011-07-07 08:21:25 +00001922 {
1923 _engineStatisticsPtr->SetLastError(
1924 VE_INVALID_OPERATION, kTraceWarning,
1925 "StopPlayingFileLocally() isnot playing");
1926 return 0;
1927 }
1928
niklase@google.com470e71d2011-07-07 08:21:25 +00001929 {
mflodman@webrtc.org9a065d12012-03-07 08:12:21 +00001930 CriticalSectionScoped cs(&_fileCritSect);
henrike@webrtc.orgb37c6282011-10-31 23:53:04 +00001931
1932 if (_outputFilePlayerPtr->StopPlayingFile() != 0)
1933 {
1934 _engineStatisticsPtr->SetLastError(
1935 VE_STOP_RECORDING_FAILED, kTraceError,
1936 "StopPlayingFile() could not stop playing");
1937 return -1;
1938 }
1939 _outputFilePlayerPtr->RegisterModuleFileCallback(NULL);
1940 FilePlayer::DestroyFilePlayer(_outputFilePlayerPtr);
1941 _outputFilePlayerPtr = NULL;
henrika@webrtc.org944cbeb2014-03-18 10:32:33 +00001942 channel_state_.SetOutputFilePlaying(false);
niklase@google.com470e71d2011-07-07 08:21:25 +00001943 }
henrike@webrtc.orgb37c6282011-10-31 23:53:04 +00001944 // _fileCritSect cannot be taken while calling
1945 // SetAnonymousMixibilityStatus. Refer to comments in
1946 // StartPlayingFileLocally(const char* ...) for more details.
henrike@webrtc.org066f9e52011-10-28 23:15:47 +00001947 if (_outputMixerPtr->SetAnonymousMixabilityStatus(*this, false) != 0)
1948 {
1949 _engineStatisticsPtr->SetLastError(
1950 VE_AUDIO_CONF_MIX_MODULE_ERROR, kTraceError,
henrike@webrtc.orgb37c6282011-10-31 23:53:04 +00001951 "StopPlayingFile() failed to stop participant from playing as"
1952 "file in the mixer");
henrike@webrtc.org066f9e52011-10-28 23:15:47 +00001953 return -1;
1954 }
niklase@google.com470e71d2011-07-07 08:21:25 +00001955
1956 return 0;
1957}
1958
1959int Channel::IsPlayingFileLocally() const
1960{
1961 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId,_channelId),
1962 "Channel::IsPlayingFileLocally()");
1963
henrika@webrtc.org944cbeb2014-03-18 10:32:33 +00001964 return channel_state_.Get().output_file_playing;
niklase@google.com470e71d2011-07-07 08:21:25 +00001965}
1966
braveyao@webrtc.orgab129902012-06-04 03:26:39 +00001967int Channel::RegisterFilePlayingToMixer()
1968{
1969 // Return success for not registering for file playing to mixer if:
1970 // 1. playing file before playout is started on that channel.
1971 // 2. starting playout without file playing on that channel.
henrika@webrtc.org944cbeb2014-03-18 10:32:33 +00001972 if (!channel_state_.Get().playing ||
1973 !channel_state_.Get().output_file_playing)
braveyao@webrtc.orgab129902012-06-04 03:26:39 +00001974 {
1975 return 0;
1976 }
1977
1978 // |_fileCritSect| cannot be taken while calling
1979 // SetAnonymousMixabilityStatus() since as soon as the participant is added
1980 // frames can be pulled by the mixer. Since the frames are generated from
1981 // the file, _fileCritSect will be taken. This would result in a deadlock.
1982 if (_outputMixerPtr->SetAnonymousMixabilityStatus(*this, true) != 0)
1983 {
henrika@webrtc.org944cbeb2014-03-18 10:32:33 +00001984 channel_state_.SetOutputFilePlaying(false);
braveyao@webrtc.orgab129902012-06-04 03:26:39 +00001985 CriticalSectionScoped cs(&_fileCritSect);
braveyao@webrtc.orgab129902012-06-04 03:26:39 +00001986 _engineStatisticsPtr->SetLastError(
1987 VE_AUDIO_CONF_MIX_MODULE_ERROR, kTraceError,
1988 "StartPlayingFile() failed to add participant as file to mixer");
1989 _outputFilePlayerPtr->StopPlayingFile();
1990 FilePlayer::DestroyFilePlayer(_outputFilePlayerPtr);
1991 _outputFilePlayerPtr = NULL;
1992 return -1;
1993 }
1994
1995 return 0;
1996}
1997
niklase@google.com470e71d2011-07-07 08:21:25 +00001998int Channel::StartPlayingFileAsMicrophone(const char* fileName,
pbos@webrtc.org92135212013-05-14 08:31:39 +00001999 bool loop,
2000 FileFormats format,
2001 int startPosition,
2002 float volumeScaling,
2003 int stopPosition,
niklase@google.com470e71d2011-07-07 08:21:25 +00002004 const CodecInst* codecInst)
2005{
2006 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId,_channelId),
2007 "Channel::StartPlayingFileAsMicrophone(fileNameUTF8[]=%s, "
2008 "loop=%d, format=%d, volumeScaling=%5.3f, startPosition=%d, "
2009 "stopPosition=%d)", fileName, loop, format, volumeScaling,
2010 startPosition, stopPosition);
2011
henrika@webrtc.org944cbeb2014-03-18 10:32:33 +00002012 CriticalSectionScoped cs(&_fileCritSect);
2013
2014 if (channel_state_.Get().input_file_playing)
niklase@google.com470e71d2011-07-07 08:21:25 +00002015 {
2016 _engineStatisticsPtr->SetLastError(
2017 VE_ALREADY_PLAYING, kTraceWarning,
2018 "StartPlayingFileAsMicrophone() filePlayer is playing");
2019 return 0;
2020 }
2021
niklase@google.com470e71d2011-07-07 08:21:25 +00002022 // Destroy the old instance
2023 if (_inputFilePlayerPtr)
2024 {
2025 _inputFilePlayerPtr->RegisterModuleFileCallback(NULL);
2026 FilePlayer::DestroyFilePlayer(_inputFilePlayerPtr);
2027 _inputFilePlayerPtr = NULL;
2028 }
2029
2030 // Create the instance
2031 _inputFilePlayerPtr = FilePlayer::CreateFilePlayer(
2032 _inputFilePlayerId, (const FileFormats)format);
2033
2034 if (_inputFilePlayerPtr == NULL)
2035 {
2036 _engineStatisticsPtr->SetLastError(
2037 VE_INVALID_ARGUMENT, kTraceError,
2038 "StartPlayingFileAsMicrophone() filePlayer format isnot correct");
2039 return -1;
2040 }
2041
pbos@webrtc.org6141e132013-04-09 10:09:10 +00002042 const uint32_t notificationTime(0);
niklase@google.com470e71d2011-07-07 08:21:25 +00002043
2044 if (_inputFilePlayerPtr->StartPlayingFile(
2045 fileName,
2046 loop,
2047 startPosition,
2048 volumeScaling,
2049 notificationTime,
2050 stopPosition,
2051 (const CodecInst*)codecInst) != 0)
2052 {
2053 _engineStatisticsPtr->SetLastError(
2054 VE_BAD_FILE, kTraceError,
2055 "StartPlayingFile() failed to start file playout");
2056 _inputFilePlayerPtr->StopPlayingFile();
2057 FilePlayer::DestroyFilePlayer(_inputFilePlayerPtr);
2058 _inputFilePlayerPtr = NULL;
2059 return -1;
2060 }
2061 _inputFilePlayerPtr->RegisterModuleFileCallback(this);
henrika@webrtc.org944cbeb2014-03-18 10:32:33 +00002062 channel_state_.SetInputFilePlaying(true);
niklase@google.com470e71d2011-07-07 08:21:25 +00002063
2064 return 0;
2065}
2066
2067int Channel::StartPlayingFileAsMicrophone(InStream* stream,
pbos@webrtc.org92135212013-05-14 08:31:39 +00002068 FileFormats format,
2069 int startPosition,
2070 float volumeScaling,
2071 int stopPosition,
niklase@google.com470e71d2011-07-07 08:21:25 +00002072 const CodecInst* codecInst)
2073{
2074 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId,_channelId),
2075 "Channel::StartPlayingFileAsMicrophone(format=%d, "
2076 "volumeScaling=%5.3f, startPosition=%d, stopPosition=%d)",
2077 format, volumeScaling, startPosition, stopPosition);
2078
2079 if(stream == NULL)
2080 {
2081 _engineStatisticsPtr->SetLastError(
2082 VE_BAD_FILE, kTraceError,
2083 "StartPlayingFileAsMicrophone NULL as input stream");
2084 return -1;
2085 }
2086
henrika@webrtc.org944cbeb2014-03-18 10:32:33 +00002087 CriticalSectionScoped cs(&_fileCritSect);
2088
2089 if (channel_state_.Get().input_file_playing)
niklase@google.com470e71d2011-07-07 08:21:25 +00002090 {
2091 _engineStatisticsPtr->SetLastError(
2092 VE_ALREADY_PLAYING, kTraceWarning,
2093 "StartPlayingFileAsMicrophone() is playing");
2094 return 0;
2095 }
2096
niklase@google.com470e71d2011-07-07 08:21:25 +00002097 // Destroy the old instance
2098 if (_inputFilePlayerPtr)
2099 {
2100 _inputFilePlayerPtr->RegisterModuleFileCallback(NULL);
2101 FilePlayer::DestroyFilePlayer(_inputFilePlayerPtr);
2102 _inputFilePlayerPtr = NULL;
2103 }
2104
2105 // Create the instance
2106 _inputFilePlayerPtr = FilePlayer::CreateFilePlayer(
2107 _inputFilePlayerId, (const FileFormats)format);
2108
2109 if (_inputFilePlayerPtr == NULL)
2110 {
2111 _engineStatisticsPtr->SetLastError(
2112 VE_INVALID_ARGUMENT, kTraceError,
2113 "StartPlayingInputFile() filePlayer format isnot correct");
2114 return -1;
2115 }
2116
pbos@webrtc.org6141e132013-04-09 10:09:10 +00002117 const uint32_t notificationTime(0);
niklase@google.com470e71d2011-07-07 08:21:25 +00002118
2119 if (_inputFilePlayerPtr->StartPlayingFile(*stream, startPosition,
2120 volumeScaling, notificationTime,
2121 stopPosition, codecInst) != 0)
2122 {
2123 _engineStatisticsPtr->SetLastError(VE_BAD_FILE, kTraceError,
2124 "StartPlayingFile() failed to start "
2125 "file playout");
2126 _inputFilePlayerPtr->StopPlayingFile();
2127 FilePlayer::DestroyFilePlayer(_inputFilePlayerPtr);
2128 _inputFilePlayerPtr = NULL;
2129 return -1;
2130 }
andrew@webrtc.orgae1a58b2013-01-22 04:44:30 +00002131
niklase@google.com470e71d2011-07-07 08:21:25 +00002132 _inputFilePlayerPtr->RegisterModuleFileCallback(this);
henrika@webrtc.org944cbeb2014-03-18 10:32:33 +00002133 channel_state_.SetInputFilePlaying(true);
niklase@google.com470e71d2011-07-07 08:21:25 +00002134
2135 return 0;
2136}
2137
2138int Channel::StopPlayingFileAsMicrophone()
2139{
2140 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId,_channelId),
2141 "Channel::StopPlayingFileAsMicrophone()");
2142
henrika@webrtc.org944cbeb2014-03-18 10:32:33 +00002143 CriticalSectionScoped cs(&_fileCritSect);
2144
2145 if (!channel_state_.Get().input_file_playing)
niklase@google.com470e71d2011-07-07 08:21:25 +00002146 {
2147 _engineStatisticsPtr->SetLastError(
2148 VE_INVALID_OPERATION, kTraceWarning,
2149 "StopPlayingFileAsMicrophone() isnot playing");
2150 return 0;
2151 }
2152
niklase@google.com470e71d2011-07-07 08:21:25 +00002153 if (_inputFilePlayerPtr->StopPlayingFile() != 0)
2154 {
2155 _engineStatisticsPtr->SetLastError(
2156 VE_STOP_RECORDING_FAILED, kTraceError,
2157 "StopPlayingFile() could not stop playing");
2158 return -1;
2159 }
2160 _inputFilePlayerPtr->RegisterModuleFileCallback(NULL);
2161 FilePlayer::DestroyFilePlayer(_inputFilePlayerPtr);
2162 _inputFilePlayerPtr = NULL;
henrika@webrtc.org944cbeb2014-03-18 10:32:33 +00002163 channel_state_.SetInputFilePlaying(false);
niklase@google.com470e71d2011-07-07 08:21:25 +00002164
2165 return 0;
2166}
2167
2168int Channel::IsPlayingFileAsMicrophone() const
2169{
2170 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId,_channelId),
2171 "Channel::IsPlayingFileAsMicrophone()");
henrika@webrtc.org944cbeb2014-03-18 10:32:33 +00002172 return channel_state_.Get().input_file_playing;
niklase@google.com470e71d2011-07-07 08:21:25 +00002173}
2174
leozwang@webrtc.org813e4b02012-03-01 18:34:25 +00002175int Channel::StartRecordingPlayout(const char* fileName,
niklase@google.com470e71d2011-07-07 08:21:25 +00002176 const CodecInst* codecInst)
2177{
2178 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId,_channelId),
2179 "Channel::StartRecordingPlayout(fileName=%s)", fileName);
2180
2181 if (_outputFileRecording)
2182 {
2183 WEBRTC_TRACE(kTraceWarning, kTraceVoice, VoEId(_instanceId,-1),
2184 "StartRecordingPlayout() is already recording");
2185 return 0;
2186 }
2187
2188 FileFormats format;
pbos@webrtc.org6141e132013-04-09 10:09:10 +00002189 const uint32_t notificationTime(0); // Not supported in VoE
niklase@google.com470e71d2011-07-07 08:21:25 +00002190 CodecInst dummyCodec={100,"L16",16000,320,1,320000};
2191
niklas.enbom@webrtc.org40197d72012-03-26 08:45:47 +00002192 if ((codecInst != NULL) &&
2193 ((codecInst->channels < 1) || (codecInst->channels > 2)))
niklase@google.com470e71d2011-07-07 08:21:25 +00002194 {
2195 _engineStatisticsPtr->SetLastError(
2196 VE_BAD_ARGUMENT, kTraceError,
2197 "StartRecordingPlayout() invalid compression");
2198 return(-1);
2199 }
2200 if(codecInst == NULL)
2201 {
2202 format = kFileFormatPcm16kHzFile;
2203 codecInst=&dummyCodec;
2204 }
2205 else if((STR_CASE_CMP(codecInst->plname,"L16") == 0) ||
2206 (STR_CASE_CMP(codecInst->plname,"PCMU") == 0) ||
2207 (STR_CASE_CMP(codecInst->plname,"PCMA") == 0))
2208 {
2209 format = kFileFormatWavFile;
2210 }
2211 else
2212 {
2213 format = kFileFormatCompressedFile;
2214 }
2215
mflodman@webrtc.org9a065d12012-03-07 08:12:21 +00002216 CriticalSectionScoped cs(&_fileCritSect);
niklase@google.com470e71d2011-07-07 08:21:25 +00002217
2218 // Destroy the old instance
2219 if (_outputFileRecorderPtr)
2220 {
2221 _outputFileRecorderPtr->RegisterModuleFileCallback(NULL);
2222 FileRecorder::DestroyFileRecorder(_outputFileRecorderPtr);
2223 _outputFileRecorderPtr = NULL;
2224 }
2225
2226 _outputFileRecorderPtr = FileRecorder::CreateFileRecorder(
2227 _outputFileRecorderId, (const FileFormats)format);
2228 if (_outputFileRecorderPtr == NULL)
2229 {
2230 _engineStatisticsPtr->SetLastError(
2231 VE_INVALID_ARGUMENT, kTraceError,
2232 "StartRecordingPlayout() fileRecorder format isnot correct");
2233 return -1;
2234 }
2235
2236 if (_outputFileRecorderPtr->StartRecordingAudioFile(
2237 fileName, (const CodecInst&)*codecInst, notificationTime) != 0)
2238 {
2239 _engineStatisticsPtr->SetLastError(
2240 VE_BAD_FILE, kTraceError,
2241 "StartRecordingAudioFile() failed to start file recording");
2242 _outputFileRecorderPtr->StopRecording();
2243 FileRecorder::DestroyFileRecorder(_outputFileRecorderPtr);
2244 _outputFileRecorderPtr = NULL;
2245 return -1;
2246 }
2247 _outputFileRecorderPtr->RegisterModuleFileCallback(this);
2248 _outputFileRecording = true;
2249
2250 return 0;
2251}
2252
2253int Channel::StartRecordingPlayout(OutStream* stream,
2254 const CodecInst* codecInst)
2255{
2256 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId,_channelId),
2257 "Channel::StartRecordingPlayout()");
2258
2259 if (_outputFileRecording)
2260 {
2261 WEBRTC_TRACE(kTraceWarning, kTraceVoice, VoEId(_instanceId,-1),
2262 "StartRecordingPlayout() is already recording");
2263 return 0;
2264 }
2265
2266 FileFormats format;
pbos@webrtc.org6141e132013-04-09 10:09:10 +00002267 const uint32_t notificationTime(0); // Not supported in VoE
niklase@google.com470e71d2011-07-07 08:21:25 +00002268 CodecInst dummyCodec={100,"L16",16000,320,1,320000};
2269
2270 if (codecInst != NULL && codecInst->channels != 1)
2271 {
2272 _engineStatisticsPtr->SetLastError(
2273 VE_BAD_ARGUMENT, kTraceError,
2274 "StartRecordingPlayout() invalid compression");
2275 return(-1);
2276 }
2277 if(codecInst == NULL)
2278 {
2279 format = kFileFormatPcm16kHzFile;
2280 codecInst=&dummyCodec;
2281 }
2282 else if((STR_CASE_CMP(codecInst->plname,"L16") == 0) ||
2283 (STR_CASE_CMP(codecInst->plname,"PCMU") == 0) ||
2284 (STR_CASE_CMP(codecInst->plname,"PCMA") == 0))
2285 {
2286 format = kFileFormatWavFile;
2287 }
2288 else
2289 {
2290 format = kFileFormatCompressedFile;
2291 }
2292
mflodman@webrtc.org9a065d12012-03-07 08:12:21 +00002293 CriticalSectionScoped cs(&_fileCritSect);
niklase@google.com470e71d2011-07-07 08:21:25 +00002294
2295 // Destroy the old instance
2296 if (_outputFileRecorderPtr)
2297 {
2298 _outputFileRecorderPtr->RegisterModuleFileCallback(NULL);
2299 FileRecorder::DestroyFileRecorder(_outputFileRecorderPtr);
2300 _outputFileRecorderPtr = NULL;
2301 }
2302
2303 _outputFileRecorderPtr = FileRecorder::CreateFileRecorder(
2304 _outputFileRecorderId, (const FileFormats)format);
2305 if (_outputFileRecorderPtr == NULL)
2306 {
2307 _engineStatisticsPtr->SetLastError(
2308 VE_INVALID_ARGUMENT, kTraceError,
2309 "StartRecordingPlayout() fileRecorder format isnot correct");
2310 return -1;
2311 }
2312
2313 if (_outputFileRecorderPtr->StartRecordingAudioFile(*stream, *codecInst,
2314 notificationTime) != 0)
2315 {
2316 _engineStatisticsPtr->SetLastError(VE_BAD_FILE, kTraceError,
2317 "StartRecordingPlayout() failed to "
2318 "start file recording");
2319 _outputFileRecorderPtr->StopRecording();
2320 FileRecorder::DestroyFileRecorder(_outputFileRecorderPtr);
2321 _outputFileRecorderPtr = NULL;
2322 return -1;
2323 }
andrew@webrtc.orgae1a58b2013-01-22 04:44:30 +00002324
niklase@google.com470e71d2011-07-07 08:21:25 +00002325 _outputFileRecorderPtr->RegisterModuleFileCallback(this);
2326 _outputFileRecording = true;
2327
2328 return 0;
2329}
2330
2331int Channel::StopRecordingPlayout()
2332{
2333 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId,-1),
2334 "Channel::StopRecordingPlayout()");
2335
2336 if (!_outputFileRecording)
2337 {
2338 WEBRTC_TRACE(kTraceError, kTraceVoice, VoEId(_instanceId,-1),
2339 "StopRecordingPlayout() isnot recording");
2340 return -1;
2341 }
2342
2343
mflodman@webrtc.org9a065d12012-03-07 08:12:21 +00002344 CriticalSectionScoped cs(&_fileCritSect);
niklase@google.com470e71d2011-07-07 08:21:25 +00002345
2346 if (_outputFileRecorderPtr->StopRecording() != 0)
2347 {
2348 _engineStatisticsPtr->SetLastError(
2349 VE_STOP_RECORDING_FAILED, kTraceError,
2350 "StopRecording() could not stop recording");
2351 return(-1);
2352 }
2353 _outputFileRecorderPtr->RegisterModuleFileCallback(NULL);
2354 FileRecorder::DestroyFileRecorder(_outputFileRecorderPtr);
2355 _outputFileRecorderPtr = NULL;
2356 _outputFileRecording = false;
2357
2358 return 0;
2359}
2360
2361void
2362Channel::SetMixWithMicStatus(bool mix)
2363{
henrika@webrtc.org944cbeb2014-03-18 10:32:33 +00002364 CriticalSectionScoped cs(&_fileCritSect);
niklase@google.com470e71d2011-07-07 08:21:25 +00002365 _mixFileWithMicrophone=mix;
2366}
2367
2368int
pbos@webrtc.org6141e132013-04-09 10:09:10 +00002369Channel::GetSpeechOutputLevel(uint32_t& level) const
niklase@google.com470e71d2011-07-07 08:21:25 +00002370{
pbos@webrtc.org6141e132013-04-09 10:09:10 +00002371 int8_t currentLevel = _outputAudioLevel.Level();
2372 level = static_cast<int32_t> (currentLevel);
niklase@google.com470e71d2011-07-07 08:21:25 +00002373 WEBRTC_TRACE(kTraceStateInfo, kTraceVoice,
2374 VoEId(_instanceId,_channelId),
2375 "GetSpeechOutputLevel() => level=%u", level);
2376 return 0;
2377}
2378
2379int
pbos@webrtc.org6141e132013-04-09 10:09:10 +00002380Channel::GetSpeechOutputLevelFullRange(uint32_t& level) const
niklase@google.com470e71d2011-07-07 08:21:25 +00002381{
pbos@webrtc.org6141e132013-04-09 10:09:10 +00002382 int16_t currentLevel = _outputAudioLevel.LevelFullRange();
2383 level = static_cast<int32_t> (currentLevel);
niklase@google.com470e71d2011-07-07 08:21:25 +00002384 WEBRTC_TRACE(kTraceStateInfo, kTraceVoice,
2385 VoEId(_instanceId,_channelId),
2386 "GetSpeechOutputLevelFullRange() => level=%u", level);
2387 return 0;
2388}
2389
2390int
2391Channel::SetMute(bool enable)
2392{
wu@webrtc.org63420662013-10-17 18:28:55 +00002393 CriticalSectionScoped cs(&volume_settings_critsect_);
niklase@google.com470e71d2011-07-07 08:21:25 +00002394 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId,_channelId),
2395 "Channel::SetMute(enable=%d)", enable);
2396 _mute = enable;
2397 return 0;
2398}
2399
2400bool
2401Channel::Mute() const
2402{
wu@webrtc.org63420662013-10-17 18:28:55 +00002403 CriticalSectionScoped cs(&volume_settings_critsect_);
niklase@google.com470e71d2011-07-07 08:21:25 +00002404 return _mute;
2405}
2406
2407int
2408Channel::SetOutputVolumePan(float left, float right)
2409{
wu@webrtc.org63420662013-10-17 18:28:55 +00002410 CriticalSectionScoped cs(&volume_settings_critsect_);
niklase@google.com470e71d2011-07-07 08:21:25 +00002411 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId,_channelId),
2412 "Channel::SetOutputVolumePan()");
2413 _panLeft = left;
2414 _panRight = right;
2415 return 0;
2416}
2417
2418int
2419Channel::GetOutputVolumePan(float& left, float& right) const
2420{
wu@webrtc.org63420662013-10-17 18:28:55 +00002421 CriticalSectionScoped cs(&volume_settings_critsect_);
niklase@google.com470e71d2011-07-07 08:21:25 +00002422 left = _panLeft;
2423 right = _panRight;
2424 WEBRTC_TRACE(kTraceStateInfo, kTraceVoice,
2425 VoEId(_instanceId,_channelId),
2426 "GetOutputVolumePan() => left=%3.2f, right=%3.2f", left, right);
2427 return 0;
2428}
2429
2430int
2431Channel::SetChannelOutputVolumeScaling(float scaling)
2432{
wu@webrtc.org63420662013-10-17 18:28:55 +00002433 CriticalSectionScoped cs(&volume_settings_critsect_);
niklase@google.com470e71d2011-07-07 08:21:25 +00002434 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId,_channelId),
2435 "Channel::SetChannelOutputVolumeScaling()");
2436 _outputGain = scaling;
2437 return 0;
2438}
2439
2440int
2441Channel::GetChannelOutputVolumeScaling(float& scaling) const
2442{
wu@webrtc.org63420662013-10-17 18:28:55 +00002443 CriticalSectionScoped cs(&volume_settings_critsect_);
niklase@google.com470e71d2011-07-07 08:21:25 +00002444 scaling = _outputGain;
2445 WEBRTC_TRACE(kTraceStateInfo, kTraceVoice,
2446 VoEId(_instanceId,_channelId),
2447 "GetChannelOutputVolumeScaling() => scaling=%3.2f", scaling);
2448 return 0;
2449}
2450
niklase@google.com470e71d2011-07-07 08:21:25 +00002451int Channel::SendTelephoneEventOutband(unsigned char eventCode,
wu@webrtc.org822fbd82013-08-15 23:38:54 +00002452 int lengthMs, int attenuationDb,
2453 bool playDtmfEvent)
niklase@google.com470e71d2011-07-07 08:21:25 +00002454{
2455 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId, _channelId),
2456 "Channel::SendTelephoneEventOutband(..., playDtmfEvent=%d)",
2457 playDtmfEvent);
2458
2459 _playOutbandDtmfEvent = playDtmfEvent;
2460
pwestin@webrtc.org2853dde2012-05-11 11:08:54 +00002461 if (_rtpRtcpModule->SendTelephoneEventOutband(eventCode, lengthMs,
niklase@google.com470e71d2011-07-07 08:21:25 +00002462 attenuationDb) != 0)
2463 {
2464 _engineStatisticsPtr->SetLastError(
2465 VE_SEND_DTMF_FAILED,
2466 kTraceWarning,
2467 "SendTelephoneEventOutband() failed to send event");
2468 return -1;
2469 }
2470 return 0;
2471}
2472
2473int Channel::SendTelephoneEventInband(unsigned char eventCode,
2474 int lengthMs,
2475 int attenuationDb,
2476 bool playDtmfEvent)
2477{
2478 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId, _channelId),
2479 "Channel::SendTelephoneEventInband(..., playDtmfEvent=%d)",
2480 playDtmfEvent);
2481
2482 _playInbandDtmfEvent = playDtmfEvent;
2483 _inbandDtmfQueue.AddDtmf(eventCode, lengthMs, attenuationDb);
2484
2485 return 0;
2486}
2487
2488int
niklase@google.com470e71d2011-07-07 08:21:25 +00002489Channel::SetSendTelephoneEventPayloadType(unsigned char type)
2490{
2491 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId,_channelId),
2492 "Channel::SetSendTelephoneEventPayloadType()");
andrew@webrtc.orgf81f9f82011-08-19 22:56:22 +00002493 if (type > 127)
niklase@google.com470e71d2011-07-07 08:21:25 +00002494 {
2495 _engineStatisticsPtr->SetLastError(
2496 VE_INVALID_ARGUMENT, kTraceError,
2497 "SetSendTelephoneEventPayloadType() invalid type");
2498 return -1;
2499 }
pbos@webrtc.org5b10d8f2013-07-11 15:50:07 +00002500 CodecInst codec = {};
pwestin@webrtc.org1da1ce02011-10-13 15:19:55 +00002501 codec.plfreq = 8000;
2502 codec.pltype = type;
2503 memcpy(codec.plname, "telephone-event", 16);
pwestin@webrtc.org2853dde2012-05-11 11:08:54 +00002504 if (_rtpRtcpModule->RegisterSendPayload(codec) != 0)
niklase@google.com470e71d2011-07-07 08:21:25 +00002505 {
henrika@webrtc.org4392d5f2013-04-17 07:34:25 +00002506 _rtpRtcpModule->DeRegisterSendPayload(codec.pltype);
2507 if (_rtpRtcpModule->RegisterSendPayload(codec) != 0) {
2508 _engineStatisticsPtr->SetLastError(
2509 VE_RTP_RTCP_MODULE_ERROR, kTraceError,
2510 "SetSendTelephoneEventPayloadType() failed to register send"
2511 "payload type");
2512 return -1;
2513 }
niklase@google.com470e71d2011-07-07 08:21:25 +00002514 }
2515 _sendTelephoneEventPayloadType = type;
2516 return 0;
2517}
2518
2519int
2520Channel::GetSendTelephoneEventPayloadType(unsigned char& type)
2521{
2522 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId,_channelId),
2523 "Channel::GetSendTelephoneEventPayloadType()");
2524 type = _sendTelephoneEventPayloadType;
2525 WEBRTC_TRACE(kTraceStateInfo, kTraceVoice,
2526 VoEId(_instanceId,_channelId),
2527 "GetSendTelephoneEventPayloadType() => type=%u", type);
2528 return 0;
2529}
2530
niklase@google.com470e71d2011-07-07 08:21:25 +00002531int
2532Channel::UpdateRxVadDetection(AudioFrame& audioFrame)
2533{
2534 WEBRTC_TRACE(kTraceStream, kTraceVoice, VoEId(_instanceId,_channelId),
2535 "Channel::UpdateRxVadDetection()");
2536
2537 int vadDecision = 1;
2538
andrew@webrtc.org63a50982012-05-02 23:56:37 +00002539 vadDecision = (audioFrame.vad_activity_ == AudioFrame::kVadActive)? 1 : 0;
niklase@google.com470e71d2011-07-07 08:21:25 +00002540
2541 if ((vadDecision != _oldVadDecision) && _rxVadObserverPtr)
2542 {
2543 OnRxVadDetected(vadDecision);
2544 _oldVadDecision = vadDecision;
2545 }
2546
2547 WEBRTC_TRACE(kTraceStream, kTraceVoice, VoEId(_instanceId,_channelId),
2548 "Channel::UpdateRxVadDetection() => vadDecision=%d",
2549 vadDecision);
2550 return 0;
2551}
2552
2553int
2554Channel::RegisterRxVadObserver(VoERxVadCallback &observer)
2555{
2556 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId,_channelId),
2557 "Channel::RegisterRxVadObserver()");
mflodman@webrtc.org9a065d12012-03-07 08:12:21 +00002558 CriticalSectionScoped cs(&_callbackCritSect);
niklase@google.com470e71d2011-07-07 08:21:25 +00002559
2560 if (_rxVadObserverPtr)
2561 {
2562 _engineStatisticsPtr->SetLastError(
2563 VE_INVALID_OPERATION, kTraceError,
2564 "RegisterRxVadObserver() observer already enabled");
2565 return -1;
2566 }
niklase@google.com470e71d2011-07-07 08:21:25 +00002567 _rxVadObserverPtr = &observer;
2568 _RxVadDetection = true;
2569 return 0;
2570}
2571
2572int
2573Channel::DeRegisterRxVadObserver()
2574{
2575 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId,_channelId),
2576 "Channel::DeRegisterRxVadObserver()");
mflodman@webrtc.org9a065d12012-03-07 08:12:21 +00002577 CriticalSectionScoped cs(&_callbackCritSect);
niklase@google.com470e71d2011-07-07 08:21:25 +00002578
2579 if (!_rxVadObserverPtr)
2580 {
2581 _engineStatisticsPtr->SetLastError(
2582 VE_INVALID_OPERATION, kTraceWarning,
2583 "DeRegisterRxVadObserver() observer already disabled");
2584 return 0;
2585 }
2586 _rxVadObserverPtr = NULL;
2587 _RxVadDetection = false;
2588 return 0;
2589}
2590
2591int
2592Channel::VoiceActivityIndicator(int &activity)
2593{
2594 activity = _sendFrameType;
2595
2596 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId,_channelId),
andrew@webrtc.org6c264cc2013-10-04 17:54:09 +00002597 "Channel::VoiceActivityIndicator(indicator=%d)", activity);
niklase@google.com470e71d2011-07-07 08:21:25 +00002598 return 0;
2599}
2600
2601#ifdef WEBRTC_VOICE_ENGINE_AGC
2602
2603int
pbos@webrtc.org92135212013-05-14 08:31:39 +00002604Channel::SetRxAgcStatus(bool enable, AgcModes mode)
niklase@google.com470e71d2011-07-07 08:21:25 +00002605{
2606 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId,_channelId),
2607 "Channel::SetRxAgcStatus(enable=%d, mode=%d)",
2608 (int)enable, (int)mode);
2609
andrew@webrtc.org6c264cc2013-10-04 17:54:09 +00002610 GainControl::Mode agcMode = kDefaultRxAgcMode;
niklase@google.com470e71d2011-07-07 08:21:25 +00002611 switch (mode)
2612 {
2613 case kAgcDefault:
niklase@google.com470e71d2011-07-07 08:21:25 +00002614 break;
2615 case kAgcUnchanged:
andrew@webrtc.orgf3930e92013-09-18 22:37:32 +00002616 agcMode = rx_audioproc_->gain_control()->mode();
niklase@google.com470e71d2011-07-07 08:21:25 +00002617 break;
2618 case kAgcFixedDigital:
2619 agcMode = GainControl::kFixedDigital;
2620 break;
2621 case kAgcAdaptiveDigital:
2622 agcMode =GainControl::kAdaptiveDigital;
2623 break;
2624 default:
2625 _engineStatisticsPtr->SetLastError(
2626 VE_INVALID_ARGUMENT, kTraceError,
2627 "SetRxAgcStatus() invalid Agc mode");
2628 return -1;
2629 }
2630
andrew@webrtc.orgf3930e92013-09-18 22:37:32 +00002631 if (rx_audioproc_->gain_control()->set_mode(agcMode) != 0)
niklase@google.com470e71d2011-07-07 08:21:25 +00002632 {
2633 _engineStatisticsPtr->SetLastError(
2634 VE_APM_ERROR, kTraceError,
2635 "SetRxAgcStatus() failed to set Agc mode");
2636 return -1;
2637 }
andrew@webrtc.orgf3930e92013-09-18 22:37:32 +00002638 if (rx_audioproc_->gain_control()->Enable(enable) != 0)
niklase@google.com470e71d2011-07-07 08:21:25 +00002639 {
2640 _engineStatisticsPtr->SetLastError(
2641 VE_APM_ERROR, kTraceError,
2642 "SetRxAgcStatus() failed to set Agc state");
2643 return -1;
2644 }
2645
2646 _rxAgcIsEnabled = enable;
henrika@webrtc.org944cbeb2014-03-18 10:32:33 +00002647 channel_state_.SetRxApmIsEnabled(_rxAgcIsEnabled || _rxNsIsEnabled);
niklase@google.com470e71d2011-07-07 08:21:25 +00002648
2649 return 0;
2650}
2651
2652int
2653Channel::GetRxAgcStatus(bool& enabled, AgcModes& mode)
2654{
2655 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId,_channelId),
2656 "Channel::GetRxAgcStatus(enable=?, mode=?)");
2657
andrew@webrtc.orgf3930e92013-09-18 22:37:32 +00002658 bool enable = rx_audioproc_->gain_control()->is_enabled();
niklase@google.com470e71d2011-07-07 08:21:25 +00002659 GainControl::Mode agcMode =
andrew@webrtc.orgf3930e92013-09-18 22:37:32 +00002660 rx_audioproc_->gain_control()->mode();
niklase@google.com470e71d2011-07-07 08:21:25 +00002661
2662 enabled = enable;
2663
2664 switch (agcMode)
2665 {
2666 case GainControl::kFixedDigital:
2667 mode = kAgcFixedDigital;
2668 break;
2669 case GainControl::kAdaptiveDigital:
2670 mode = kAgcAdaptiveDigital;
2671 break;
2672 default:
2673 _engineStatisticsPtr->SetLastError(
2674 VE_APM_ERROR, kTraceError,
2675 "GetRxAgcStatus() invalid Agc mode");
2676 return -1;
2677 }
2678
2679 return 0;
2680}
2681
2682int
pbos@webrtc.org92135212013-05-14 08:31:39 +00002683Channel::SetRxAgcConfig(AgcConfig config)
niklase@google.com470e71d2011-07-07 08:21:25 +00002684{
2685 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId,_channelId),
2686 "Channel::SetRxAgcConfig()");
2687
andrew@webrtc.orgf3930e92013-09-18 22:37:32 +00002688 if (rx_audioproc_->gain_control()->set_target_level_dbfs(
niklase@google.com470e71d2011-07-07 08:21:25 +00002689 config.targetLeveldBOv) != 0)
2690 {
2691 _engineStatisticsPtr->SetLastError(
2692 VE_APM_ERROR, kTraceError,
2693 "SetRxAgcConfig() failed to set target peak |level|"
2694 "(or envelope) of the Agc");
2695 return -1;
2696 }
andrew@webrtc.orgf3930e92013-09-18 22:37:32 +00002697 if (rx_audioproc_->gain_control()->set_compression_gain_db(
niklase@google.com470e71d2011-07-07 08:21:25 +00002698 config.digitalCompressionGaindB) != 0)
2699 {
2700 _engineStatisticsPtr->SetLastError(
2701 VE_APM_ERROR, kTraceError,
2702 "SetRxAgcConfig() failed to set the range in |gain| the"
2703 " digital compression stage may apply");
2704 return -1;
2705 }
andrew@webrtc.orgf3930e92013-09-18 22:37:32 +00002706 if (rx_audioproc_->gain_control()->enable_limiter(
niklase@google.com470e71d2011-07-07 08:21:25 +00002707 config.limiterEnable) != 0)
2708 {
2709 _engineStatisticsPtr->SetLastError(
2710 VE_APM_ERROR, kTraceError,
2711 "SetRxAgcConfig() failed to set hard limiter to the signal");
2712 return -1;
2713 }
2714
2715 return 0;
2716}
2717
2718int
2719Channel::GetRxAgcConfig(AgcConfig& config)
2720{
2721 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId,_channelId),
2722 "Channel::GetRxAgcConfig(config=%?)");
2723
2724 config.targetLeveldBOv =
andrew@webrtc.orgf3930e92013-09-18 22:37:32 +00002725 rx_audioproc_->gain_control()->target_level_dbfs();
niklase@google.com470e71d2011-07-07 08:21:25 +00002726 config.digitalCompressionGaindB =
andrew@webrtc.orgf3930e92013-09-18 22:37:32 +00002727 rx_audioproc_->gain_control()->compression_gain_db();
niklase@google.com470e71d2011-07-07 08:21:25 +00002728 config.limiterEnable =
andrew@webrtc.orgf3930e92013-09-18 22:37:32 +00002729 rx_audioproc_->gain_control()->is_limiter_enabled();
niklase@google.com470e71d2011-07-07 08:21:25 +00002730
2731 WEBRTC_TRACE(kTraceStateInfo, kTraceVoice,
2732 VoEId(_instanceId,_channelId), "GetRxAgcConfig() => "
2733 "targetLeveldBOv=%u, digitalCompressionGaindB=%u,"
2734 " limiterEnable=%d",
2735 config.targetLeveldBOv,
2736 config.digitalCompressionGaindB,
2737 config.limiterEnable);
2738
2739 return 0;
2740}
2741
2742#endif // #ifdef WEBRTC_VOICE_ENGINE_AGC
2743
2744#ifdef WEBRTC_VOICE_ENGINE_NR
2745
2746int
pbos@webrtc.org92135212013-05-14 08:31:39 +00002747Channel::SetRxNsStatus(bool enable, NsModes mode)
niklase@google.com470e71d2011-07-07 08:21:25 +00002748{
2749 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId,_channelId),
2750 "Channel::SetRxNsStatus(enable=%d, mode=%d)",
2751 (int)enable, (int)mode);
2752
andrew@webrtc.org6c264cc2013-10-04 17:54:09 +00002753 NoiseSuppression::Level nsLevel = kDefaultNsMode;
niklase@google.com470e71d2011-07-07 08:21:25 +00002754 switch (mode)
2755 {
2756
2757 case kNsDefault:
niklase@google.com470e71d2011-07-07 08:21:25 +00002758 break;
2759 case kNsUnchanged:
andrew@webrtc.orgf3930e92013-09-18 22:37:32 +00002760 nsLevel = rx_audioproc_->noise_suppression()->level();
niklase@google.com470e71d2011-07-07 08:21:25 +00002761 break;
2762 case kNsConference:
2763 nsLevel = NoiseSuppression::kHigh;
2764 break;
2765 case kNsLowSuppression:
2766 nsLevel = NoiseSuppression::kLow;
2767 break;
2768 case kNsModerateSuppression:
2769 nsLevel = NoiseSuppression::kModerate;
2770 break;
2771 case kNsHighSuppression:
2772 nsLevel = NoiseSuppression::kHigh;
2773 break;
2774 case kNsVeryHighSuppression:
2775 nsLevel = NoiseSuppression::kVeryHigh;
2776 break;
niklase@google.com470e71d2011-07-07 08:21:25 +00002777 }
2778
andrew@webrtc.orgf3930e92013-09-18 22:37:32 +00002779 if (rx_audioproc_->noise_suppression()->set_level(nsLevel)
niklase@google.com470e71d2011-07-07 08:21:25 +00002780 != 0)
2781 {
2782 _engineStatisticsPtr->SetLastError(
2783 VE_APM_ERROR, kTraceError,
andrew@webrtc.org6c264cc2013-10-04 17:54:09 +00002784 "SetRxNsStatus() failed to set NS level");
niklase@google.com470e71d2011-07-07 08:21:25 +00002785 return -1;
2786 }
andrew@webrtc.orgf3930e92013-09-18 22:37:32 +00002787 if (rx_audioproc_->noise_suppression()->Enable(enable) != 0)
niklase@google.com470e71d2011-07-07 08:21:25 +00002788 {
2789 _engineStatisticsPtr->SetLastError(
2790 VE_APM_ERROR, kTraceError,
andrew@webrtc.org6c264cc2013-10-04 17:54:09 +00002791 "SetRxNsStatus() failed to set NS state");
niklase@google.com470e71d2011-07-07 08:21:25 +00002792 return -1;
2793 }
2794
2795 _rxNsIsEnabled = enable;
henrika@webrtc.org944cbeb2014-03-18 10:32:33 +00002796 channel_state_.SetRxApmIsEnabled(_rxAgcIsEnabled || _rxNsIsEnabled);
niklase@google.com470e71d2011-07-07 08:21:25 +00002797
2798 return 0;
2799}
2800
2801int
2802Channel::GetRxNsStatus(bool& enabled, NsModes& mode)
2803{
2804 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId,_channelId),
2805 "Channel::GetRxNsStatus(enable=?, mode=?)");
2806
2807 bool enable =
andrew@webrtc.orgf3930e92013-09-18 22:37:32 +00002808 rx_audioproc_->noise_suppression()->is_enabled();
niklase@google.com470e71d2011-07-07 08:21:25 +00002809 NoiseSuppression::Level ncLevel =
andrew@webrtc.orgf3930e92013-09-18 22:37:32 +00002810 rx_audioproc_->noise_suppression()->level();
niklase@google.com470e71d2011-07-07 08:21:25 +00002811
2812 enabled = enable;
2813
2814 switch (ncLevel)
2815 {
2816 case NoiseSuppression::kLow:
2817 mode = kNsLowSuppression;
2818 break;
2819 case NoiseSuppression::kModerate:
2820 mode = kNsModerateSuppression;
2821 break;
2822 case NoiseSuppression::kHigh:
2823 mode = kNsHighSuppression;
2824 break;
2825 case NoiseSuppression::kVeryHigh:
2826 mode = kNsVeryHighSuppression;
2827 break;
niklase@google.com470e71d2011-07-07 08:21:25 +00002828 }
2829
2830 WEBRTC_TRACE(kTraceStateInfo, kTraceVoice,
2831 VoEId(_instanceId,_channelId),
2832 "GetRxNsStatus() => enabled=%d, mode=%d", enabled, mode);
2833 return 0;
2834}
2835
2836#endif // #ifdef WEBRTC_VOICE_ENGINE_NR
2837
2838int
niklase@google.com470e71d2011-07-07 08:21:25 +00002839Channel::SetLocalSSRC(unsigned int ssrc)
2840{
2841 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId, _channelId),
2842 "Channel::SetLocalSSRC()");
henrika@webrtc.org944cbeb2014-03-18 10:32:33 +00002843 if (channel_state_.Get().sending)
niklase@google.com470e71d2011-07-07 08:21:25 +00002844 {
2845 _engineStatisticsPtr->SetLastError(
2846 VE_ALREADY_SENDING, kTraceError,
2847 "SetLocalSSRC() already sending");
2848 return -1;
2849 }
stefan@webrtc.orgef927552014-06-05 08:25:29 +00002850 _rtpRtcpModule->SetSSRC(ssrc);
niklase@google.com470e71d2011-07-07 08:21:25 +00002851 return 0;
2852}
2853
2854int
2855Channel::GetLocalSSRC(unsigned int& ssrc)
2856{
pwestin@webrtc.org2853dde2012-05-11 11:08:54 +00002857 ssrc = _rtpRtcpModule->SSRC();
niklase@google.com470e71d2011-07-07 08:21:25 +00002858 WEBRTC_TRACE(kTraceStateInfo, kTraceVoice,
2859 VoEId(_instanceId,_channelId),
2860 "GetLocalSSRC() => ssrc=%lu", ssrc);
2861 return 0;
2862}
2863
2864int
2865Channel::GetRemoteSSRC(unsigned int& ssrc)
2866{
wu@webrtc.org822fbd82013-08-15 23:38:54 +00002867 ssrc = rtp_receiver_->SSRC();
niklase@google.com470e71d2011-07-07 08:21:25 +00002868 WEBRTC_TRACE(kTraceStateInfo, kTraceVoice,
2869 VoEId(_instanceId,_channelId),
2870 "GetRemoteSSRC() => ssrc=%lu", ssrc);
2871 return 0;
2872}
2873
wu@webrtc.orgebdb0e32014-03-06 23:49:08 +00002874int Channel::SetSendAudioLevelIndicationStatus(bool enable, unsigned char id) {
andrew@webrtc.orgf3930e92013-09-18 22:37:32 +00002875 _includeAudioLevelIndication = enable;
wu@webrtc.orgebdb0e32014-03-06 23:49:08 +00002876 return SetSendRtpHeaderExtension(enable, kRtpExtensionAudioLevel, id);
niklase@google.com470e71d2011-07-07 08:21:25 +00002877}
andrew@webrtc.orgf3930e92013-09-18 22:37:32 +00002878
wu@webrtc.org93fd25c2014-04-24 20:33:08 +00002879int Channel::SetReceiveAudioLevelIndicationStatus(bool enable,
2880 unsigned char id) {
2881 rtp_header_parser_->DeregisterRtpHeaderExtension(
2882 kRtpExtensionAudioLevel);
2883 if (enable && !rtp_header_parser_->RegisterRtpHeaderExtension(
2884 kRtpExtensionAudioLevel, id)) {
2885 return -1;
2886 }
2887 return 0;
2888}
2889
wu@webrtc.orgebdb0e32014-03-06 23:49:08 +00002890int Channel::SetSendAbsoluteSenderTimeStatus(bool enable, unsigned char id) {
2891 return SetSendRtpHeaderExtension(enable, kRtpExtensionAbsoluteSendTime, id);
2892}
2893
2894int Channel::SetReceiveAbsoluteSenderTimeStatus(bool enable, unsigned char id) {
2895 rtp_header_parser_->DeregisterRtpHeaderExtension(
2896 kRtpExtensionAbsoluteSendTime);
solenberg@webrtc.orgb1f50102014-03-24 10:38:25 +00002897 if (enable && !rtp_header_parser_->RegisterRtpHeaderExtension(
2898 kRtpExtensionAbsoluteSendTime, id)) {
2899 return -1;
wu@webrtc.orgebdb0e32014-03-06 23:49:08 +00002900 }
2901 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +00002902}
2903
pbos@webrtc.orgd16e8392014-12-19 13:49:55 +00002904void Channel::SetRTCPStatus(bool enable) {
2905 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId, _channelId),
2906 "Channel::SetRTCPStatus()");
2907 _rtpRtcpModule->SetRTCPStatus(enable ? kRtcpCompound : kRtcpOff);
niklase@google.com470e71d2011-07-07 08:21:25 +00002908}
2909
2910int
2911Channel::GetRTCPStatus(bool& enabled)
2912{
pwestin@webrtc.org2853dde2012-05-11 11:08:54 +00002913 RTCPMethod method = _rtpRtcpModule->RTCP();
niklase@google.com470e71d2011-07-07 08:21:25 +00002914 enabled = (method != kRtcpOff);
2915 WEBRTC_TRACE(kTraceStateInfo, kTraceVoice,
2916 VoEId(_instanceId,_channelId),
2917 "GetRTCPStatus() => enabled=%d", enabled);
2918 return 0;
2919}
2920
2921int
2922Channel::SetRTCP_CNAME(const char cName[256])
2923{
2924 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId, _channelId),
2925 "Channel::SetRTCP_CNAME()");
pwestin@webrtc.org2853dde2012-05-11 11:08:54 +00002926 if (_rtpRtcpModule->SetCNAME(cName) != 0)
niklase@google.com470e71d2011-07-07 08:21:25 +00002927 {
2928 _engineStatisticsPtr->SetLastError(
2929 VE_RTP_RTCP_MODULE_ERROR, kTraceError,
2930 "SetRTCP_CNAME() failed to set RTCP CNAME");
2931 return -1;
2932 }
2933 return 0;
2934}
2935
2936int
niklase@google.com470e71d2011-07-07 08:21:25 +00002937Channel::GetRemoteRTCP_CNAME(char cName[256])
2938{
2939 if (cName == NULL)
2940 {
2941 _engineStatisticsPtr->SetLastError(
2942 VE_INVALID_ARGUMENT, kTraceError,
2943 "GetRemoteRTCP_CNAME() invalid CNAME input buffer");
2944 return -1;
2945 }
leozwang@webrtc.org813e4b02012-03-01 18:34:25 +00002946 char cname[RTCP_CNAME_SIZE];
wu@webrtc.org822fbd82013-08-15 23:38:54 +00002947 const uint32_t remoteSSRC = rtp_receiver_->SSRC();
pwestin@webrtc.org2853dde2012-05-11 11:08:54 +00002948 if (_rtpRtcpModule->RemoteCNAME(remoteSSRC, cname) != 0)
niklase@google.com470e71d2011-07-07 08:21:25 +00002949 {
2950 _engineStatisticsPtr->SetLastError(
2951 VE_CANNOT_RETRIEVE_CNAME, kTraceError,
2952 "GetRemoteRTCP_CNAME() failed to retrieve remote RTCP CNAME");
2953 return -1;
2954 }
2955 strcpy(cName, cname);
2956 WEBRTC_TRACE(kTraceStateInfo, kTraceVoice,
2957 VoEId(_instanceId, _channelId),
2958 "GetRemoteRTCP_CNAME() => cName=%s", cName);
2959 return 0;
2960}
2961
2962int
2963Channel::GetRemoteRTCPData(
2964 unsigned int& NTPHigh,
2965 unsigned int& NTPLow,
2966 unsigned int& timestamp,
2967 unsigned int& playoutTimestamp,
2968 unsigned int* jitter,
2969 unsigned short* fractionLost)
2970{
2971 // --- Information from sender info in received Sender Reports
2972
2973 RTCPSenderInfo senderInfo;
pwestin@webrtc.org2853dde2012-05-11 11:08:54 +00002974 if (_rtpRtcpModule->RemoteRTCPStat(&senderInfo) != 0)
niklase@google.com470e71d2011-07-07 08:21:25 +00002975 {
2976 _engineStatisticsPtr->SetLastError(
2977 VE_RTP_RTCP_MODULE_ERROR, kTraceError,
wu@webrtc.orgfcd12b32011-09-15 20:49:50 +00002978 "GetRemoteRTCPData() failed to retrieve sender info for remote "
niklase@google.com470e71d2011-07-07 08:21:25 +00002979 "side");
2980 return -1;
2981 }
2982
2983 // We only utilize 12 out of 20 bytes in the sender info (ignores packet
2984 // and octet count)
2985 NTPHigh = senderInfo.NTPseconds;
2986 NTPLow = senderInfo.NTPfraction;
2987 timestamp = senderInfo.RTPtimeStamp;
2988
2989 WEBRTC_TRACE(kTraceStateInfo, kTraceVoice,
2990 VoEId(_instanceId, _channelId),
2991 "GetRemoteRTCPData() => NTPHigh=%lu, NTPLow=%lu, "
2992 "timestamp=%lu",
2993 NTPHigh, NTPLow, timestamp);
2994
2995 // --- Locally derived information
2996
2997 // This value is updated on each incoming RTCP packet (0 when no packet
2998 // has been received)
pwestin@webrtc.org1de01352013-04-11 20:23:35 +00002999 playoutTimestamp = playout_timestamp_rtcp_;
niklase@google.com470e71d2011-07-07 08:21:25 +00003000
3001 WEBRTC_TRACE(kTraceStateInfo, kTraceVoice,
3002 VoEId(_instanceId, _channelId),
3003 "GetRemoteRTCPData() => playoutTimestamp=%lu",
pwestin@webrtc.org1de01352013-04-11 20:23:35 +00003004 playout_timestamp_rtcp_);
niklase@google.com470e71d2011-07-07 08:21:25 +00003005
3006 if (NULL != jitter || NULL != fractionLost)
3007 {
perkj@webrtc.orgce5990c2012-01-11 13:00:08 +00003008 // Get all RTCP receiver report blocks that have been received on this
3009 // channel. If we receive RTP packets from a remote source we know the
3010 // remote SSRC and use the report block from him.
3011 // Otherwise use the first report block.
3012 std::vector<RTCPReportBlock> remote_stats;
pwestin@webrtc.org2853dde2012-05-11 11:08:54 +00003013 if (_rtpRtcpModule->RemoteRTCPStat(&remote_stats) != 0 ||
perkj@webrtc.orgce5990c2012-01-11 13:00:08 +00003014 remote_stats.empty()) {
3015 WEBRTC_TRACE(kTraceWarning, kTraceVoice,
3016 VoEId(_instanceId, _channelId),
3017 "GetRemoteRTCPData() failed to measure statistics due"
3018 " to lack of received RTP and/or RTCP packets");
3019 return -1;
niklase@google.com470e71d2011-07-07 08:21:25 +00003020 }
perkj@webrtc.orgce5990c2012-01-11 13:00:08 +00003021
wu@webrtc.org822fbd82013-08-15 23:38:54 +00003022 uint32_t remoteSSRC = rtp_receiver_->SSRC();
perkj@webrtc.orgce5990c2012-01-11 13:00:08 +00003023 std::vector<RTCPReportBlock>::const_iterator it = remote_stats.begin();
3024 for (; it != remote_stats.end(); ++it) {
3025 if (it->remoteSSRC == remoteSSRC)
3026 break;
niklase@google.com470e71d2011-07-07 08:21:25 +00003027 }
perkj@webrtc.orgce5990c2012-01-11 13:00:08 +00003028
3029 if (it == remote_stats.end()) {
3030 // If we have not received any RTCP packets from this SSRC it probably
3031 // means that we have not received any RTP packets.
3032 // Use the first received report block instead.
3033 it = remote_stats.begin();
3034 remoteSSRC = it->remoteSSRC;
niklase@google.com470e71d2011-07-07 08:21:25 +00003035 }
perkj@webrtc.orgce5990c2012-01-11 13:00:08 +00003036
xians@webrtc.org79af7342012-01-31 12:22:14 +00003037 if (jitter) {
3038 *jitter = it->jitter;
3039 WEBRTC_TRACE(kTraceStateInfo, kTraceVoice,
3040 VoEId(_instanceId, _channelId),
3041 "GetRemoteRTCPData() => jitter = %lu", *jitter);
3042 }
perkj@webrtc.orgce5990c2012-01-11 13:00:08 +00003043
xians@webrtc.org79af7342012-01-31 12:22:14 +00003044 if (fractionLost) {
3045 *fractionLost = it->fractionLost;
3046 WEBRTC_TRACE(kTraceStateInfo, kTraceVoice,
3047 VoEId(_instanceId, _channelId),
3048 "GetRemoteRTCPData() => fractionLost = %lu",
3049 *fractionLost);
3050 }
niklase@google.com470e71d2011-07-07 08:21:25 +00003051 }
3052 return 0;
3053}
3054
3055int
pbos@webrtc.org92135212013-05-14 08:31:39 +00003056Channel::SendApplicationDefinedRTCPPacket(unsigned char subType,
niklase@google.com470e71d2011-07-07 08:21:25 +00003057 unsigned int name,
3058 const char* data,
3059 unsigned short dataLengthInBytes)
3060{
3061 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId, _channelId),
3062 "Channel::SendApplicationDefinedRTCPPacket()");
henrika@webrtc.org944cbeb2014-03-18 10:32:33 +00003063 if (!channel_state_.Get().sending)
niklase@google.com470e71d2011-07-07 08:21:25 +00003064 {
3065 _engineStatisticsPtr->SetLastError(
3066 VE_NOT_SENDING, kTraceError,
3067 "SendApplicationDefinedRTCPPacket() not sending");
3068 return -1;
3069 }
3070 if (NULL == data)
3071 {
3072 _engineStatisticsPtr->SetLastError(
3073 VE_INVALID_ARGUMENT, kTraceError,
3074 "SendApplicationDefinedRTCPPacket() invalid data value");
3075 return -1;
3076 }
3077 if (dataLengthInBytes % 4 != 0)
3078 {
3079 _engineStatisticsPtr->SetLastError(
3080 VE_INVALID_ARGUMENT, kTraceError,
3081 "SendApplicationDefinedRTCPPacket() invalid length value");
3082 return -1;
3083 }
pwestin@webrtc.org2853dde2012-05-11 11:08:54 +00003084 RTCPMethod status = _rtpRtcpModule->RTCP();
niklase@google.com470e71d2011-07-07 08:21:25 +00003085 if (status == kRtcpOff)
3086 {
3087 _engineStatisticsPtr->SetLastError(
3088 VE_RTCP_ERROR, kTraceError,
3089 "SendApplicationDefinedRTCPPacket() RTCP is disabled");
3090 return -1;
3091 }
3092
3093 // Create and schedule the RTCP APP packet for transmission
pwestin@webrtc.org2853dde2012-05-11 11:08:54 +00003094 if (_rtpRtcpModule->SetRTCPApplicationSpecificData(
niklase@google.com470e71d2011-07-07 08:21:25 +00003095 subType,
3096 name,
3097 (const unsigned char*) data,
3098 dataLengthInBytes) != 0)
3099 {
3100 _engineStatisticsPtr->SetLastError(
3101 VE_SEND_ERROR, kTraceError,
3102 "SendApplicationDefinedRTCPPacket() failed to send RTCP packet");
3103 return -1;
3104 }
3105 return 0;
3106}
3107
3108int
3109Channel::GetRTPStatistics(
3110 unsigned int& averageJitterMs,
3111 unsigned int& maxJitterMs,
3112 unsigned int& discardedPackets)
3113{
niklase@google.com470e71d2011-07-07 08:21:25 +00003114 // The jitter statistics is updated for each received RTP packet and is
3115 // based on received packets.
sprang@webrtc.org54ae4ff2013-12-19 13:26:02 +00003116 if (_rtpRtcpModule->RTCP() == kRtcpOff) {
3117 // If RTCP is off, there is no timed thread in the RTCP module regularly
3118 // generating new stats, trigger the update manually here instead.
3119 StreamStatistician* statistician =
3120 rtp_receive_statistics_->GetStatistician(rtp_receiver_->SSRC());
3121 if (statistician) {
3122 // Don't use returned statistics, use data from proxy instead so that
3123 // max jitter can be fetched atomically.
3124 RtcpStatistics s;
3125 statistician->GetStatistics(&s, true);
3126 }
niklase@google.com470e71d2011-07-07 08:21:25 +00003127 }
3128
sprang@webrtc.org54ae4ff2013-12-19 13:26:02 +00003129 ChannelStatistics stats = statistics_proxy_->GetStats();
andrew@webrtc.orgeb524d92013-09-23 23:02:24 +00003130 const int32_t playoutFrequency = audio_coding_->PlayoutFrequency();
sprang@webrtc.org54ae4ff2013-12-19 13:26:02 +00003131 if (playoutFrequency > 0) {
3132 // Scale RTP statistics given the current playout frequency
3133 maxJitterMs = stats.max_jitter / (playoutFrequency / 1000);
3134 averageJitterMs = stats.rtcp.jitter / (playoutFrequency / 1000);
niklase@google.com470e71d2011-07-07 08:21:25 +00003135 }
3136
3137 discardedPackets = _numberOfDiscardedPackets;
3138
3139 WEBRTC_TRACE(kTraceStateInfo, kTraceVoice,
3140 VoEId(_instanceId, _channelId),
3141 "GetRTPStatistics() => averageJitterMs = %lu, maxJitterMs = %lu,"
wu@webrtc.orgfcd12b32011-09-15 20:49:50 +00003142 " discardedPackets = %lu)",
niklase@google.com470e71d2011-07-07 08:21:25 +00003143 averageJitterMs, maxJitterMs, discardedPackets);
3144 return 0;
3145}
3146
henrika@webrtc.org8a2fc882012-08-22 08:53:55 +00003147int Channel::GetRemoteRTCPReportBlocks(
3148 std::vector<ReportBlock>* report_blocks) {
3149 if (report_blocks == NULL) {
3150 _engineStatisticsPtr->SetLastError(VE_INVALID_ARGUMENT, kTraceError,
3151 "GetRemoteRTCPReportBlock()s invalid report_blocks.");
3152 return -1;
3153 }
3154
3155 // Get the report blocks from the latest received RTCP Sender or Receiver
3156 // Report. Each element in the vector contains the sender's SSRC and a
3157 // report block according to RFC 3550.
3158 std::vector<RTCPReportBlock> rtcp_report_blocks;
3159 if (_rtpRtcpModule->RemoteRTCPStat(&rtcp_report_blocks) != 0) {
3160 _engineStatisticsPtr->SetLastError(VE_RTP_RTCP_MODULE_ERROR, kTraceError,
3161 "GetRemoteRTCPReportBlocks() failed to read RTCP SR/RR report block.");
3162 return -1;
3163 }
3164
3165 if (rtcp_report_blocks.empty())
3166 return 0;
3167
3168 std::vector<RTCPReportBlock>::const_iterator it = rtcp_report_blocks.begin();
3169 for (; it != rtcp_report_blocks.end(); ++it) {
3170 ReportBlock report_block;
3171 report_block.sender_SSRC = it->remoteSSRC;
3172 report_block.source_SSRC = it->sourceSSRC;
3173 report_block.fraction_lost = it->fractionLost;
3174 report_block.cumulative_num_packets_lost = it->cumulativeLost;
3175 report_block.extended_highest_sequence_number = it->extendedHighSeqNum;
3176 report_block.interarrival_jitter = it->jitter;
3177 report_block.last_SR_timestamp = it->lastSR;
3178 report_block.delay_since_last_SR = it->delaySinceLastSR;
3179 report_blocks->push_back(report_block);
3180 }
3181 return 0;
3182}
3183
niklase@google.com470e71d2011-07-07 08:21:25 +00003184int
3185Channel::GetRTPStatistics(CallStatistics& stats)
3186{
wu@webrtc.orgcb711f72014-05-19 17:39:11 +00003187 // --- RtcpStatistics
niklase@google.com470e71d2011-07-07 08:21:25 +00003188
3189 // The jitter statistics is updated for each received RTP packet and is
3190 // based on received packets.
sprang@webrtc.org54ae4ff2013-12-19 13:26:02 +00003191 RtcpStatistics statistics;
stefan@webrtc.org286fe0b2013-08-21 20:58:21 +00003192 StreamStatistician* statistician =
3193 rtp_receive_statistics_->GetStatistician(rtp_receiver_->SSRC());
3194 if (!statistician || !statistician->GetStatistics(
wu@webrtc.org822fbd82013-08-15 23:38:54 +00003195 &statistics, _rtpRtcpModule->RTCP() == kRtcpOff)) {
3196 _engineStatisticsPtr->SetLastError(
3197 VE_CANNOT_RETRIEVE_RTP_STAT, kTraceWarning,
3198 "GetRTPStatistics() failed to read RTP statistics from the "
3199 "RTP/RTCP module");
niklase@google.com470e71d2011-07-07 08:21:25 +00003200 }
3201
wu@webrtc.org822fbd82013-08-15 23:38:54 +00003202 stats.fractionLost = statistics.fraction_lost;
3203 stats.cumulativeLost = statistics.cumulative_lost;
3204 stats.extendedMax = statistics.extended_max_sequence_number;
3205 stats.jitterSamples = statistics.jitter;
niklase@google.com470e71d2011-07-07 08:21:25 +00003206
3207 WEBRTC_TRACE(kTraceStateInfo, kTraceVoice,
3208 VoEId(_instanceId, _channelId),
3209 "GetRTPStatistics() => fractionLost=%lu, cumulativeLost=%lu,"
wu@webrtc.orgfcd12b32011-09-15 20:49:50 +00003210 " extendedMax=%lu, jitterSamples=%li)",
niklase@google.com470e71d2011-07-07 08:21:25 +00003211 stats.fractionLost, stats.cumulativeLost, stats.extendedMax,
3212 stats.jitterSamples);
3213
wu@webrtc.orgcb711f72014-05-19 17:39:11 +00003214 // --- RTT
minyue@webrtc.org2b58a442014-09-11 07:51:53 +00003215 stats.rttMs = GetRTT();
minyue@webrtc.org6fd93082014-12-15 14:56:44 +00003216 if (stats.rttMs == 0) {
3217 WEBRTC_TRACE(kTraceWarning, kTraceVoice, VoEId(_instanceId, _channelId),
3218 "GetRTPStatistics() failed to get RTT");
3219 } else {
3220 WEBRTC_TRACE(kTraceStateInfo, kTraceVoice, VoEId(_instanceId, _channelId),
3221 "GetRTPStatistics() => rttMs=%d", stats.rttMs);
3222 }
niklase@google.com470e71d2011-07-07 08:21:25 +00003223
wu@webrtc.orgcb711f72014-05-19 17:39:11 +00003224 // --- Data counters
niklase@google.com470e71d2011-07-07 08:21:25 +00003225
pkasting@chromium.org4591fbd2014-11-20 22:28:14 +00003226 size_t bytesSent(0);
pbos@webrtc.org6141e132013-04-09 10:09:10 +00003227 uint32_t packetsSent(0);
pkasting@chromium.org4591fbd2014-11-20 22:28:14 +00003228 size_t bytesReceived(0);
pbos@webrtc.org6141e132013-04-09 10:09:10 +00003229 uint32_t packetsReceived(0);
niklase@google.com470e71d2011-07-07 08:21:25 +00003230
stefan@webrtc.org286fe0b2013-08-21 20:58:21 +00003231 if (statistician) {
3232 statistician->GetDataCounters(&bytesReceived, &packetsReceived);
3233 }
wu@webrtc.org822fbd82013-08-15 23:38:54 +00003234
pwestin@webrtc.org2853dde2012-05-11 11:08:54 +00003235 if (_rtpRtcpModule->DataCountersRTP(&bytesSent,
wu@webrtc.org822fbd82013-08-15 23:38:54 +00003236 &packetsSent) != 0)
niklase@google.com470e71d2011-07-07 08:21:25 +00003237 {
3238 WEBRTC_TRACE(kTraceWarning, kTraceVoice,
3239 VoEId(_instanceId, _channelId),
3240 "GetRTPStatistics() failed to retrieve RTP datacounters =>"
wu@webrtc.orgfcd12b32011-09-15 20:49:50 +00003241 " output will not be complete");
niklase@google.com470e71d2011-07-07 08:21:25 +00003242 }
3243
3244 stats.bytesSent = bytesSent;
3245 stats.packetsSent = packetsSent;
3246 stats.bytesReceived = bytesReceived;
3247 stats.packetsReceived = packetsReceived;
3248
3249 WEBRTC_TRACE(kTraceStateInfo, kTraceVoice,
3250 VoEId(_instanceId, _channelId),
pkasting@chromium.org4591fbd2014-11-20 22:28:14 +00003251 "GetRTPStatistics() => bytesSent=%" PRIuS ", packetsSent=%d,"
3252 " bytesReceived=%" PRIuS ", packetsReceived=%d)",
niklase@google.com470e71d2011-07-07 08:21:25 +00003253 stats.bytesSent, stats.packetsSent, stats.bytesReceived,
3254 stats.packetsReceived);
3255
wu@webrtc.orgcb711f72014-05-19 17:39:11 +00003256 // --- Timestamps
3257 {
3258 CriticalSectionScoped lock(ts_stats_lock_.get());
3259 stats.capture_start_ntp_time_ms_ = capture_start_ntp_time_ms_;
3260 }
niklase@google.com470e71d2011-07-07 08:21:25 +00003261 return 0;
3262}
3263
minyue@webrtc.orgc1a40a72014-05-28 09:52:06 +00003264int Channel::SetREDStatus(bool enable, int redPayloadtype) {
turaj@webrtc.org42259e72012-12-11 02:15:12 +00003265 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId, _channelId),
minyue@webrtc.orgc1a40a72014-05-28 09:52:06 +00003266 "Channel::SetREDStatus()");
niklase@google.com470e71d2011-07-07 08:21:25 +00003267
turaj@webrtc.org8c8ad852013-01-31 18:20:17 +00003268 if (enable) {
3269 if (redPayloadtype < 0 || redPayloadtype > 127) {
3270 _engineStatisticsPtr->SetLastError(
3271 VE_PLTYPE_ERROR, kTraceError,
minyue@webrtc.orgc1a40a72014-05-28 09:52:06 +00003272 "SetREDStatus() invalid RED payload type");
turaj@webrtc.org8c8ad852013-01-31 18:20:17 +00003273 return -1;
3274 }
3275
3276 if (SetRedPayloadType(redPayloadtype) < 0) {
3277 _engineStatisticsPtr->SetLastError(
3278 VE_CODEC_ERROR, kTraceError,
3279 "SetSecondarySendCodec() Failed to register RED ACM");
3280 return -1;
3281 }
turaj@webrtc.org42259e72012-12-11 02:15:12 +00003282 }
niklase@google.com470e71d2011-07-07 08:21:25 +00003283
minyue@webrtc.orgaa5ea1c2014-05-23 15:16:51 +00003284 if (audio_coding_->SetREDStatus(enable) != 0) {
turaj@webrtc.org42259e72012-12-11 02:15:12 +00003285 _engineStatisticsPtr->SetLastError(
3286 VE_AUDIO_CODING_MODULE_ERROR, kTraceError,
minyue@webrtc.orgaa5ea1c2014-05-23 15:16:51 +00003287 "SetREDStatus() failed to set RED state in the ACM");
turaj@webrtc.org42259e72012-12-11 02:15:12 +00003288 return -1;
3289 }
3290 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +00003291}
3292
3293int
minyue@webrtc.orgc1a40a72014-05-28 09:52:06 +00003294Channel::GetREDStatus(bool& enabled, int& redPayloadtype)
niklase@google.com470e71d2011-07-07 08:21:25 +00003295{
minyue@webrtc.orgaa5ea1c2014-05-23 15:16:51 +00003296 enabled = audio_coding_->REDStatus();
niklase@google.com470e71d2011-07-07 08:21:25 +00003297 if (enabled)
3298 {
pbos@webrtc.org6141e132013-04-09 10:09:10 +00003299 int8_t payloadType(0);
pwestin@webrtc.org2853dde2012-05-11 11:08:54 +00003300 if (_rtpRtcpModule->SendREDPayloadType(payloadType) != 0)
niklase@google.com470e71d2011-07-07 08:21:25 +00003301 {
3302 _engineStatisticsPtr->SetLastError(
3303 VE_RTP_RTCP_MODULE_ERROR, kTraceError,
minyue@webrtc.orgc1a40a72014-05-28 09:52:06 +00003304 "GetREDStatus() failed to retrieve RED PT from RTP/RTCP "
niklase@google.com470e71d2011-07-07 08:21:25 +00003305 "module");
3306 return -1;
3307 }
3308 WEBRTC_TRACE(kTraceStateInfo, kTraceVoice,
3309 VoEId(_instanceId, _channelId),
minyue@webrtc.orgc1a40a72014-05-28 09:52:06 +00003310 "GetREDStatus() => enabled=%d, redPayloadtype=%d",
niklase@google.com470e71d2011-07-07 08:21:25 +00003311 enabled, redPayloadtype);
3312 return 0;
3313 }
3314 WEBRTC_TRACE(kTraceStateInfo, kTraceVoice,
3315 VoEId(_instanceId, _channelId),
minyue@webrtc.orgc1a40a72014-05-28 09:52:06 +00003316 "GetREDStatus() => enabled=%d", enabled);
niklase@google.com470e71d2011-07-07 08:21:25 +00003317 return 0;
3318}
3319
minyue@webrtc.orgc1a40a72014-05-28 09:52:06 +00003320int Channel::SetCodecFECStatus(bool enable) {
3321 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId, _channelId),
3322 "Channel::SetCodecFECStatus()");
3323
3324 if (audio_coding_->SetCodecFEC(enable) != 0) {
3325 _engineStatisticsPtr->SetLastError(
3326 VE_AUDIO_CODING_MODULE_ERROR, kTraceError,
3327 "SetCodecFECStatus() failed to set FEC state");
3328 return -1;
3329 }
3330 return 0;
3331}
3332
3333bool Channel::GetCodecFECStatus() {
3334 bool enabled = audio_coding_->CodecFEC();
3335 WEBRTC_TRACE(kTraceStateInfo, kTraceVoice,
3336 VoEId(_instanceId, _channelId),
3337 "GetCodecFECStatus() => enabled=%d", enabled);
3338 return enabled;
3339}
3340
pwestin@webrtc.orgdb249952013-06-05 15:33:20 +00003341void Channel::SetNACKStatus(bool enable, int maxNumberOfPackets) {
3342 // None of these functions can fail.
3343 _rtpRtcpModule->SetStorePacketsStatus(enable, maxNumberOfPackets);
stefan@webrtc.org7bb8f022013-09-06 13:40:11 +00003344 rtp_receive_statistics_->SetMaxReorderingThreshold(maxNumberOfPackets);
3345 rtp_receiver_->SetNACKStatus(enable ? kNackRtcp : kNackOff);
pwestin@webrtc.orgd30859e2013-06-06 21:09:01 +00003346 if (enable)
andrew@webrtc.orgeb524d92013-09-23 23:02:24 +00003347 audio_coding_->EnableNack(maxNumberOfPackets);
pwestin@webrtc.orgd30859e2013-06-06 21:09:01 +00003348 else
andrew@webrtc.orgeb524d92013-09-23 23:02:24 +00003349 audio_coding_->DisableNack();
pwestin@webrtc.orgdb249952013-06-05 15:33:20 +00003350}
3351
pwestin@webrtc.orgd30859e2013-06-06 21:09:01 +00003352// Called when we are missing one or more packets.
3353int Channel::ResendPackets(const uint16_t* sequence_numbers, int length) {
pwestin@webrtc.orgdb249952013-06-05 15:33:20 +00003354 return _rtpRtcpModule->SendNACK(sequence_numbers, length);
3355}
3356
niklase@google.com470e71d2011-07-07 08:21:25 +00003357int
niklase@google.com470e71d2011-07-07 08:21:25 +00003358Channel::StartRTPDump(const char fileNameUTF8[1024],
3359 RTPDirections direction)
3360{
3361 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId, _channelId),
3362 "Channel::StartRTPDump()");
3363 if ((direction != kRtpIncoming) && (direction != kRtpOutgoing))
3364 {
3365 _engineStatisticsPtr->SetLastError(
3366 VE_INVALID_ARGUMENT, kTraceError,
3367 "StartRTPDump() invalid RTP direction");
3368 return -1;
3369 }
3370 RtpDump* rtpDumpPtr = (direction == kRtpIncoming) ?
3371 &_rtpDumpIn : &_rtpDumpOut;
3372 if (rtpDumpPtr == NULL)
3373 {
3374 assert(false);
3375 return -1;
3376 }
3377 if (rtpDumpPtr->IsActive())
3378 {
3379 rtpDumpPtr->Stop();
3380 }
3381 if (rtpDumpPtr->Start(fileNameUTF8) != 0)
3382 {
3383 _engineStatisticsPtr->SetLastError(
3384 VE_BAD_FILE, kTraceError,
3385 "StartRTPDump() failed to create file");
3386 return -1;
3387 }
3388 return 0;
3389}
3390
3391int
3392Channel::StopRTPDump(RTPDirections direction)
3393{
3394 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId, _channelId),
3395 "Channel::StopRTPDump()");
3396 if ((direction != kRtpIncoming) && (direction != kRtpOutgoing))
3397 {
3398 _engineStatisticsPtr->SetLastError(
3399 VE_INVALID_ARGUMENT, kTraceError,
3400 "StopRTPDump() invalid RTP direction");
3401 return -1;
3402 }
3403 RtpDump* rtpDumpPtr = (direction == kRtpIncoming) ?
3404 &_rtpDumpIn : &_rtpDumpOut;
3405 if (rtpDumpPtr == NULL)
3406 {
3407 assert(false);
3408 return -1;
3409 }
3410 if (!rtpDumpPtr->IsActive())
3411 {
3412 return 0;
3413 }
3414 return rtpDumpPtr->Stop();
3415}
3416
3417bool
3418Channel::RTPDumpIsActive(RTPDirections direction)
3419{
3420 if ((direction != kRtpIncoming) &&
3421 (direction != kRtpOutgoing))
3422 {
3423 _engineStatisticsPtr->SetLastError(
3424 VE_INVALID_ARGUMENT, kTraceError,
3425 "RTPDumpIsActive() invalid RTP direction");
3426 return false;
3427 }
3428 RtpDump* rtpDumpPtr = (direction == kRtpIncoming) ?
3429 &_rtpDumpIn : &_rtpDumpOut;
3430 return rtpDumpPtr->IsActive();
3431}
3432
solenberg@webrtc.orgb1f50102014-03-24 10:38:25 +00003433void Channel::SetVideoEngineBWETarget(ViENetwork* vie_network,
3434 int video_channel) {
3435 CriticalSectionScoped cs(&_callbackCritSect);
3436 if (vie_network_) {
3437 vie_network_->Release();
3438 vie_network_ = NULL;
3439 }
3440 video_channel_ = -1;
3441
3442 if (vie_network != NULL && video_channel != -1) {
3443 vie_network_ = vie_network;
3444 video_channel_ = video_channel;
3445 }
3446}
3447
pbos@webrtc.org6141e132013-04-09 10:09:10 +00003448uint32_t
andrew@webrtc.org755b04a2011-11-15 16:57:56 +00003449Channel::Demultiplex(const AudioFrame& audioFrame)
niklase@google.com470e71d2011-07-07 08:21:25 +00003450{
3451 WEBRTC_TRACE(kTraceStream, kTraceVoice, VoEId(_instanceId,_channelId),
andrew@webrtc.org755b04a2011-11-15 16:57:56 +00003452 "Channel::Demultiplex()");
andrew@webrtc.orgae1a58b2013-01-22 04:44:30 +00003453 _audioFrame.CopyFrom(audioFrame);
andrew@webrtc.org63a50982012-05-02 23:56:37 +00003454 _audioFrame.id_ = _channelId;
niklase@google.com470e71d2011-07-07 08:21:25 +00003455 return 0;
3456}
3457
xians@webrtc.org2f84afa2013-07-31 16:23:37 +00003458void Channel::Demultiplex(const int16_t* audio_data,
xians@webrtc.org8fff1f02013-07-31 16:27:42 +00003459 int sample_rate,
xians@webrtc.org2f84afa2013-07-31 16:23:37 +00003460 int number_of_frames,
xians@webrtc.org8fff1f02013-07-31 16:27:42 +00003461 int number_of_channels) {
xians@webrtc.org2f84afa2013-07-31 16:23:37 +00003462 CodecInst codec;
3463 GetSendCodec(codec);
xians@webrtc.org2f84afa2013-07-31 16:23:37 +00003464
andrew@webrtc.org40ee3d02014-04-03 21:56:01 +00003465 if (!mono_recording_audio_.get()) {
3466 // Temporary space for DownConvertToCodecFormat.
3467 mono_recording_audio_.reset(new int16_t[kMaxMonoDataSizeSamples]);
xians@webrtc.org2f84afa2013-07-31 16:23:37 +00003468 }
andrew@webrtc.org40ee3d02014-04-03 21:56:01 +00003469 DownConvertToCodecFormat(audio_data,
3470 number_of_frames,
3471 number_of_channels,
3472 sample_rate,
3473 codec.channels,
3474 codec.plfreq,
3475 mono_recording_audio_.get(),
3476 &input_resampler_,
3477 &_audioFrame);
xians@webrtc.org2f84afa2013-07-31 16:23:37 +00003478}
3479
pbos@webrtc.org6141e132013-04-09 10:09:10 +00003480uint32_t
xians@google.com0b0665a2011-08-08 08:18:44 +00003481Channel::PrepareEncodeAndSend(int mixingFrequency)
niklase@google.com470e71d2011-07-07 08:21:25 +00003482{
3483 WEBRTC_TRACE(kTraceStream, kTraceVoice, VoEId(_instanceId,_channelId),
3484 "Channel::PrepareEncodeAndSend()");
3485
andrew@webrtc.org63a50982012-05-02 23:56:37 +00003486 if (_audioFrame.samples_per_channel_ == 0)
niklase@google.com470e71d2011-07-07 08:21:25 +00003487 {
3488 WEBRTC_TRACE(kTraceWarning, kTraceVoice, VoEId(_instanceId,_channelId),
3489 "Channel::PrepareEncodeAndSend() invalid audio frame");
tommi@webrtc.orgeec6ecd2014-07-11 19:09:59 +00003490 return 0xFFFFFFFF;
niklase@google.com470e71d2011-07-07 08:21:25 +00003491 }
3492
henrika@webrtc.org944cbeb2014-03-18 10:32:33 +00003493 if (channel_state_.Get().input_file_playing)
niklase@google.com470e71d2011-07-07 08:21:25 +00003494 {
3495 MixOrReplaceAudioWithFile(mixingFrequency);
3496 }
3497
andrew@webrtc.org21299d42014-05-14 19:00:59 +00003498 bool is_muted = Mute(); // Cache locally as Mute() takes a lock.
3499 if (is_muted) {
3500 AudioFrameOperations::Mute(_audioFrame);
niklase@google.com470e71d2011-07-07 08:21:25 +00003501 }
3502
henrika@webrtc.org944cbeb2014-03-18 10:32:33 +00003503 if (channel_state_.Get().input_external_media)
niklase@google.com470e71d2011-07-07 08:21:25 +00003504 {
mflodman@webrtc.org9a065d12012-03-07 08:12:21 +00003505 CriticalSectionScoped cs(&_callbackCritSect);
andrew@webrtc.org63a50982012-05-02 23:56:37 +00003506 const bool isStereo = (_audioFrame.num_channels_ == 2);
niklase@google.com470e71d2011-07-07 08:21:25 +00003507 if (_inputExternalMediaCallbackPtr)
3508 {
3509 _inputExternalMediaCallbackPtr->Process(
3510 _channelId,
3511 kRecordingPerChannel,
pbos@webrtc.org6141e132013-04-09 10:09:10 +00003512 (int16_t*)_audioFrame.data_,
andrew@webrtc.org63a50982012-05-02 23:56:37 +00003513 _audioFrame.samples_per_channel_,
3514 _audioFrame.sample_rate_hz_,
niklase@google.com470e71d2011-07-07 08:21:25 +00003515 isStereo);
3516 }
3517 }
3518
3519 InsertInbandDtmfTone();
3520
andrew@webrtc.org60730cf2014-01-07 17:45:09 +00003521 if (_includeAudioLevelIndication) {
andrew@webrtc.org382c0c22014-05-05 18:22:21 +00003522 int length = _audioFrame.samples_per_channel_ * _audioFrame.num_channels_;
andrew@webrtc.org21299d42014-05-14 19:00:59 +00003523 if (is_muted) {
3524 rms_level_.ProcessMuted(length);
3525 } else {
3526 rms_level_.Process(_audioFrame.data_, length);
3527 }
andrew@webrtc.org755b04a2011-11-15 16:57:56 +00003528 }
3529
niklase@google.com470e71d2011-07-07 08:21:25 +00003530 return 0;
3531}
3532
pbos@webrtc.org6141e132013-04-09 10:09:10 +00003533uint32_t
niklase@google.com470e71d2011-07-07 08:21:25 +00003534Channel::EncodeAndSend()
3535{
3536 WEBRTC_TRACE(kTraceStream, kTraceVoice, VoEId(_instanceId,_channelId),
3537 "Channel::EncodeAndSend()");
3538
andrew@webrtc.org63a50982012-05-02 23:56:37 +00003539 assert(_audioFrame.num_channels_ <= 2);
3540 if (_audioFrame.samples_per_channel_ == 0)
niklase@google.com470e71d2011-07-07 08:21:25 +00003541 {
3542 WEBRTC_TRACE(kTraceWarning, kTraceVoice, VoEId(_instanceId,_channelId),
3543 "Channel::EncodeAndSend() invalid audio frame");
tommi@webrtc.orgeec6ecd2014-07-11 19:09:59 +00003544 return 0xFFFFFFFF;
niklase@google.com470e71d2011-07-07 08:21:25 +00003545 }
3546
andrew@webrtc.org63a50982012-05-02 23:56:37 +00003547 _audioFrame.id_ = _channelId;
niklase@google.com470e71d2011-07-07 08:21:25 +00003548
3549 // --- Add 10ms of raw (PCM) audio data to the encoder @ 32kHz.
3550
3551 // The ACM resamples internally.
andrew@webrtc.org63a50982012-05-02 23:56:37 +00003552 _audioFrame.timestamp_ = _timeStamp;
andrew@webrtc.orgeb524d92013-09-23 23:02:24 +00003553 if (audio_coding_->Add10MsData((AudioFrame&)_audioFrame) != 0)
niklase@google.com470e71d2011-07-07 08:21:25 +00003554 {
3555 WEBRTC_TRACE(kTraceError, kTraceVoice, VoEId(_instanceId,_channelId),
3556 "Channel::EncodeAndSend() ACM encoding failed");
tommi@webrtc.orgeec6ecd2014-07-11 19:09:59 +00003557 return 0xFFFFFFFF;
niklase@google.com470e71d2011-07-07 08:21:25 +00003558 }
3559
andrew@webrtc.org63a50982012-05-02 23:56:37 +00003560 _timeStamp += _audioFrame.samples_per_channel_;
niklase@google.com470e71d2011-07-07 08:21:25 +00003561
3562 // --- Encode if complete frame is ready
3563
3564 // This call will trigger AudioPacketizationCallback::SendData if encoding
3565 // is done and payload is ready for packetization and transmission.
andrew@webrtc.orgeb524d92013-09-23 23:02:24 +00003566 return audio_coding_->Process();
niklase@google.com470e71d2011-07-07 08:21:25 +00003567}
3568
3569int Channel::RegisterExternalMediaProcessing(
3570 ProcessingTypes type,
3571 VoEMediaProcess& processObject)
3572{
3573 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId,_channelId),
3574 "Channel::RegisterExternalMediaProcessing()");
3575
mflodman@webrtc.org9a065d12012-03-07 08:12:21 +00003576 CriticalSectionScoped cs(&_callbackCritSect);
niklase@google.com470e71d2011-07-07 08:21:25 +00003577
3578 if (kPlaybackPerChannel == type)
3579 {
3580 if (_outputExternalMediaCallbackPtr)
3581 {
3582 _engineStatisticsPtr->SetLastError(
3583 VE_INVALID_OPERATION, kTraceError,
3584 "Channel::RegisterExternalMediaProcessing() "
3585 "output external media already enabled");
3586 return -1;
3587 }
3588 _outputExternalMediaCallbackPtr = &processObject;
3589 _outputExternalMedia = true;
3590 }
3591 else if (kRecordingPerChannel == type)
3592 {
3593 if (_inputExternalMediaCallbackPtr)
3594 {
3595 _engineStatisticsPtr->SetLastError(
3596 VE_INVALID_OPERATION, kTraceError,
3597 "Channel::RegisterExternalMediaProcessing() "
3598 "output external media already enabled");
3599 return -1;
3600 }
3601 _inputExternalMediaCallbackPtr = &processObject;
henrika@webrtc.org944cbeb2014-03-18 10:32:33 +00003602 channel_state_.SetInputExternalMedia(true);
niklase@google.com470e71d2011-07-07 08:21:25 +00003603 }
3604 return 0;
3605}
3606
3607int Channel::DeRegisterExternalMediaProcessing(ProcessingTypes type)
3608{
3609 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId,_channelId),
3610 "Channel::DeRegisterExternalMediaProcessing()");
3611
mflodman@webrtc.org9a065d12012-03-07 08:12:21 +00003612 CriticalSectionScoped cs(&_callbackCritSect);
niklase@google.com470e71d2011-07-07 08:21:25 +00003613
3614 if (kPlaybackPerChannel == type)
3615 {
3616 if (!_outputExternalMediaCallbackPtr)
3617 {
3618 _engineStatisticsPtr->SetLastError(
3619 VE_INVALID_OPERATION, kTraceWarning,
3620 "Channel::DeRegisterExternalMediaProcessing() "
3621 "output external media already disabled");
3622 return 0;
3623 }
3624 _outputExternalMedia = false;
3625 _outputExternalMediaCallbackPtr = NULL;
3626 }
3627 else if (kRecordingPerChannel == type)
3628 {
3629 if (!_inputExternalMediaCallbackPtr)
3630 {
3631 _engineStatisticsPtr->SetLastError(
3632 VE_INVALID_OPERATION, kTraceWarning,
3633 "Channel::DeRegisterExternalMediaProcessing() "
3634 "input external media already disabled");
3635 return 0;
3636 }
henrika@webrtc.org944cbeb2014-03-18 10:32:33 +00003637 channel_state_.SetInputExternalMedia(false);
niklase@google.com470e71d2011-07-07 08:21:25 +00003638 _inputExternalMediaCallbackPtr = NULL;
3639 }
3640
3641 return 0;
3642}
3643
roosa@google.com1b60ceb2012-12-12 23:00:29 +00003644int Channel::SetExternalMixing(bool enabled) {
3645 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId,_channelId),
3646 "Channel::SetExternalMixing(enabled=%d)", enabled);
3647
henrika@webrtc.org944cbeb2014-03-18 10:32:33 +00003648 if (channel_state_.Get().playing)
roosa@google.com1b60ceb2012-12-12 23:00:29 +00003649 {
3650 _engineStatisticsPtr->SetLastError(
3651 VE_INVALID_OPERATION, kTraceError,
3652 "Channel::SetExternalMixing() "
3653 "external mixing cannot be changed while playing.");
3654 return -1;
3655 }
3656
3657 _externalMixing = enabled;
3658
3659 return 0;
3660}
3661
niklase@google.com470e71d2011-07-07 08:21:25 +00003662int
niklase@google.com470e71d2011-07-07 08:21:25 +00003663Channel::GetNetworkStatistics(NetworkStatistics& stats)
3664{
3665 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId,_channelId),
3666 "Channel::GetNetworkStatistics()");
tina.legrand@webrtc.org7a7a0082013-02-21 10:27:48 +00003667 ACMNetworkStatistics acm_stats;
andrew@webrtc.orgeb524d92013-09-23 23:02:24 +00003668 int return_value = audio_coding_->NetworkStatistics(&acm_stats);
tina.legrand@webrtc.org7a7a0082013-02-21 10:27:48 +00003669 if (return_value >= 0) {
3670 memcpy(&stats, &acm_stats, sizeof(NetworkStatistics));
3671 }
3672 return return_value;
niklase@google.com470e71d2011-07-07 08:21:25 +00003673}
3674
wu@webrtc.org24301a62013-12-13 19:17:43 +00003675void Channel::GetDecodingCallStatistics(AudioDecodingCallStats* stats) const {
3676 audio_coding_->GetDecodingCallStatistics(stats);
3677}
3678
pwestin@webrtc.org1de01352013-04-11 20:23:35 +00003679bool Channel::GetDelayEstimate(int* jitter_buffer_delay_ms,
3680 int* playout_buffer_delay_ms) const {
3681 if (_average_jitter_buffer_delay_us == 0) {
niklase@google.com470e71d2011-07-07 08:21:25 +00003682 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId,_channelId),
pwestin@webrtc.org1de01352013-04-11 20:23:35 +00003683 "Channel::GetDelayEstimate() no valid estimate.");
3684 return false;
3685 }
3686 *jitter_buffer_delay_ms = (_average_jitter_buffer_delay_us + 500) / 1000 +
3687 _recPacketDelayMs;
3688 *playout_buffer_delay_ms = playout_delay_ms_;
3689 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId,_channelId),
3690 "Channel::GetDelayEstimate()");
3691 return true;
niklase@google.com470e71d2011-07-07 08:21:25 +00003692}
3693
turaj@webrtc.org6388c3e2013-02-12 21:42:18 +00003694int Channel::SetInitialPlayoutDelay(int delay_ms)
3695{
3696 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId,_channelId),
3697 "Channel::SetInitialPlayoutDelay()");
3698 if ((delay_ms < kVoiceEngineMinMinPlayoutDelayMs) ||
3699 (delay_ms > kVoiceEngineMaxMinPlayoutDelayMs))
3700 {
3701 _engineStatisticsPtr->SetLastError(
3702 VE_INVALID_ARGUMENT, kTraceError,
3703 "SetInitialPlayoutDelay() invalid min delay");
3704 return -1;
3705 }
andrew@webrtc.orgeb524d92013-09-23 23:02:24 +00003706 if (audio_coding_->SetInitialPlayoutDelay(delay_ms) != 0)
turaj@webrtc.org6388c3e2013-02-12 21:42:18 +00003707 {
3708 _engineStatisticsPtr->SetLastError(
3709 VE_AUDIO_CODING_MODULE_ERROR, kTraceError,
3710 "SetInitialPlayoutDelay() failed to set min playout delay");
3711 return -1;
3712 }
3713 return 0;
3714}
3715
3716
niklase@google.com470e71d2011-07-07 08:21:25 +00003717int
3718Channel::SetMinimumPlayoutDelay(int delayMs)
3719{
3720 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId,_channelId),
3721 "Channel::SetMinimumPlayoutDelay()");
3722 if ((delayMs < kVoiceEngineMinMinPlayoutDelayMs) ||
3723 (delayMs > kVoiceEngineMaxMinPlayoutDelayMs))
3724 {
3725 _engineStatisticsPtr->SetLastError(
3726 VE_INVALID_ARGUMENT, kTraceError,
3727 "SetMinimumPlayoutDelay() invalid min delay");
3728 return -1;
3729 }
andrew@webrtc.orgeb524d92013-09-23 23:02:24 +00003730 if (audio_coding_->SetMinimumPlayoutDelay(delayMs) != 0)
niklase@google.com470e71d2011-07-07 08:21:25 +00003731 {
3732 _engineStatisticsPtr->SetLastError(
3733 VE_AUDIO_CODING_MODULE_ERROR, kTraceError,
3734 "SetMinimumPlayoutDelay() failed to set min playout delay");
3735 return -1;
3736 }
3737 return 0;
3738}
3739
pwestin@webrtc.org1de01352013-04-11 20:23:35 +00003740void Channel::UpdatePlayoutTimestamp(bool rtcp) {
3741 uint32_t playout_timestamp = 0;
3742
andrew@webrtc.orgeb524d92013-09-23 23:02:24 +00003743 if (audio_coding_->PlayoutTimestamp(&playout_timestamp) == -1) {
turaj@webrtc.org1ebd2e92014-07-25 17:50:10 +00003744 // This can happen if this channel has not been received any RTP packet. In
3745 // this case, NetEq is not capable of computing playout timestamp.
pwestin@webrtc.org1de01352013-04-11 20:23:35 +00003746 return;
3747 }
3748
3749 uint16_t delay_ms = 0;
3750 if (_audioDeviceModulePtr->PlayoutDelay(&delay_ms) == -1) {
3751 WEBRTC_TRACE(kTraceWarning, kTraceVoice, VoEId(_instanceId,_channelId),
3752 "Channel::UpdatePlayoutTimestamp() failed to read playout"
3753 " delay from the ADM");
3754 _engineStatisticsPtr->SetLastError(
3755 VE_CANNOT_RETRIEVE_VALUE, kTraceError,
3756 "UpdatePlayoutTimestamp() failed to retrieve playout delay");
3757 return;
3758 }
3759
turaj@webrtc.org167b6df2013-12-13 21:05:07 +00003760 jitter_buffer_playout_timestamp_ = playout_timestamp;
3761
pwestin@webrtc.org1de01352013-04-11 20:23:35 +00003762 // Remove the playout delay.
wu@webrtc.org94454b72014-06-05 20:34:08 +00003763 playout_timestamp -= (delay_ms * (GetPlayoutFrequency() / 1000));
pwestin@webrtc.org1de01352013-04-11 20:23:35 +00003764
3765 WEBRTC_TRACE(kTraceStream, kTraceVoice, VoEId(_instanceId,_channelId),
3766 "Channel::UpdatePlayoutTimestamp() => playoutTimestamp = %lu",
3767 playout_timestamp);
3768
3769 if (rtcp) {
3770 playout_timestamp_rtcp_ = playout_timestamp;
3771 } else {
3772 playout_timestamp_rtp_ = playout_timestamp;
3773 }
3774 playout_delay_ms_ = delay_ms;
3775}
3776
3777int Channel::GetPlayoutTimestamp(unsigned int& timestamp) {
3778 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId,_channelId),
3779 "Channel::GetPlayoutTimestamp()");
3780 if (playout_timestamp_rtp_ == 0) {
3781 _engineStatisticsPtr->SetLastError(
3782 VE_CANNOT_RETRIEVE_VALUE, kTraceError,
3783 "GetPlayoutTimestamp() failed to retrieve timestamp");
3784 return -1;
3785 }
3786 timestamp = playout_timestamp_rtp_;
3787 WEBRTC_TRACE(kTraceStateInfo, kTraceVoice,
3788 VoEId(_instanceId,_channelId),
3789 "GetPlayoutTimestamp() => timestamp=%u", timestamp);
3790 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +00003791}
3792
pbos@webrtc.orgd16e8392014-12-19 13:49:55 +00003793int Channel::SetInitTimestamp(unsigned int timestamp) {
3794 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId, _channelId),
niklase@google.com470e71d2011-07-07 08:21:25 +00003795 "Channel::SetInitTimestamp()");
pbos@webrtc.orgd16e8392014-12-19 13:49:55 +00003796 if (channel_state_.Get().sending) {
3797 _engineStatisticsPtr->SetLastError(VE_SENDING, kTraceError,
3798 "SetInitTimestamp() already sending");
3799 return -1;
3800 }
3801 _rtpRtcpModule->SetStartTimestamp(timestamp);
3802 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +00003803}
3804
pbos@webrtc.orgd16e8392014-12-19 13:49:55 +00003805int Channel::SetInitSequenceNumber(short sequenceNumber) {
3806 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId, _channelId),
3807 "Channel::SetInitSequenceNumber()");
3808 if (channel_state_.Get().sending) {
3809 _engineStatisticsPtr->SetLastError(
3810 VE_SENDING, kTraceError, "SetInitSequenceNumber() already sending");
3811 return -1;
3812 }
3813 _rtpRtcpModule->SetSequenceNumber(sequenceNumber);
3814 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +00003815}
3816
3817int
wu@webrtc.org822fbd82013-08-15 23:38:54 +00003818Channel::GetRtpRtcp(RtpRtcp** rtpRtcpModule, RtpReceiver** rtp_receiver) const
niklase@google.com470e71d2011-07-07 08:21:25 +00003819{
3820 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId,_channelId),
3821 "Channel::GetRtpRtcp()");
wu@webrtc.org822fbd82013-08-15 23:38:54 +00003822 *rtpRtcpModule = _rtpRtcpModule.get();
3823 *rtp_receiver = rtp_receiver_.get();
niklase@google.com470e71d2011-07-07 08:21:25 +00003824 return 0;
3825}
3826
andrew@webrtc.orge59a0ac2012-05-08 17:12:40 +00003827// TODO(andrew): refactor Mix functions here and in transmit_mixer.cc to use
3828// a shared helper.
pbos@webrtc.org6141e132013-04-09 10:09:10 +00003829int32_t
pbos@webrtc.org92135212013-05-14 08:31:39 +00003830Channel::MixOrReplaceAudioWithFile(int mixingFrequency)
niklase@google.com470e71d2011-07-07 08:21:25 +00003831{
andrew@webrtc.org8f693302014-04-25 23:10:28 +00003832 scoped_ptr<int16_t[]> fileBuffer(new int16_t[640]);
andrew@webrtc.orge59a0ac2012-05-08 17:12:40 +00003833 int fileSamples(0);
niklase@google.com470e71d2011-07-07 08:21:25 +00003834
3835 {
mflodman@webrtc.org9a065d12012-03-07 08:12:21 +00003836 CriticalSectionScoped cs(&_fileCritSect);
niklase@google.com470e71d2011-07-07 08:21:25 +00003837
3838 if (_inputFilePlayerPtr == NULL)
3839 {
3840 WEBRTC_TRACE(kTraceWarning, kTraceVoice,
3841 VoEId(_instanceId, _channelId),
3842 "Channel::MixOrReplaceAudioWithFile() fileplayer"
3843 " doesnt exist");
3844 return -1;
3845 }
3846
braveyao@webrtc.orgd7131432012-03-29 10:39:44 +00003847 if (_inputFilePlayerPtr->Get10msAudioFromFile(fileBuffer.get(),
niklase@google.com470e71d2011-07-07 08:21:25 +00003848 fileSamples,
3849 mixingFrequency) == -1)
3850 {
3851 WEBRTC_TRACE(kTraceWarning, kTraceVoice,
3852 VoEId(_instanceId, _channelId),
3853 "Channel::MixOrReplaceAudioWithFile() file mixing "
3854 "failed");
3855 return -1;
3856 }
3857 if (fileSamples == 0)
3858 {
3859 WEBRTC_TRACE(kTraceWarning, kTraceVoice,
3860 VoEId(_instanceId, _channelId),
3861 "Channel::MixOrReplaceAudioWithFile() file is ended");
3862 return 0;
3863 }
3864 }
3865
andrew@webrtc.org63a50982012-05-02 23:56:37 +00003866 assert(_audioFrame.samples_per_channel_ == fileSamples);
niklase@google.com470e71d2011-07-07 08:21:25 +00003867
3868 if (_mixFileWithMicrophone)
3869 {
braveyao@webrtc.orgd7131432012-03-29 10:39:44 +00003870 // Currently file stream is always mono.
3871 // TODO(xians): Change the code when FilePlayer supports real stereo.
andrew@webrtc.org40ee3d02014-04-03 21:56:01 +00003872 MixWithSat(_audioFrame.data_,
3873 _audioFrame.num_channels_,
3874 fileBuffer.get(),
3875 1,
3876 fileSamples);
niklase@google.com470e71d2011-07-07 08:21:25 +00003877 }
3878 else
3879 {
braveyao@webrtc.orgd7131432012-03-29 10:39:44 +00003880 // Replace ACM audio with file.
3881 // Currently file stream is always mono.
3882 // TODO(xians): Change the code when FilePlayer supports real stereo.
niklase@google.com470e71d2011-07-07 08:21:25 +00003883 _audioFrame.UpdateFrame(_channelId,
tommi@webrtc.orgeec6ecd2014-07-11 19:09:59 +00003884 0xFFFFFFFF,
braveyao@webrtc.orgd7131432012-03-29 10:39:44 +00003885 fileBuffer.get(),
andrew@webrtc.orge59a0ac2012-05-08 17:12:40 +00003886 fileSamples,
niklase@google.com470e71d2011-07-07 08:21:25 +00003887 mixingFrequency,
3888 AudioFrame::kNormalSpeech,
3889 AudioFrame::kVadUnknown,
3890 1);
3891
3892 }
3893 return 0;
3894}
3895
pbos@webrtc.org6141e132013-04-09 10:09:10 +00003896int32_t
niklase@google.com470e71d2011-07-07 08:21:25 +00003897Channel::MixAudioWithFile(AudioFrame& audioFrame,
pbos@webrtc.org92135212013-05-14 08:31:39 +00003898 int mixingFrequency)
niklase@google.com470e71d2011-07-07 08:21:25 +00003899{
minyue@webrtc.org2a8df7c2014-08-06 10:05:19 +00003900 assert(mixingFrequency <= 48000);
niklase@google.com470e71d2011-07-07 08:21:25 +00003901
minyue@webrtc.org2a8df7c2014-08-06 10:05:19 +00003902 scoped_ptr<int16_t[]> fileBuffer(new int16_t[960]);
andrew@webrtc.orge59a0ac2012-05-08 17:12:40 +00003903 int fileSamples(0);
niklase@google.com470e71d2011-07-07 08:21:25 +00003904
3905 {
mflodman@webrtc.org9a065d12012-03-07 08:12:21 +00003906 CriticalSectionScoped cs(&_fileCritSect);
niklase@google.com470e71d2011-07-07 08:21:25 +00003907
3908 if (_outputFilePlayerPtr == NULL)
3909 {
3910 WEBRTC_TRACE(kTraceWarning, kTraceVoice,
3911 VoEId(_instanceId, _channelId),
3912 "Channel::MixAudioWithFile() file mixing failed");
3913 return -1;
3914 }
3915
3916 // We should get the frequency we ask for.
braveyao@webrtc.orgd7131432012-03-29 10:39:44 +00003917 if (_outputFilePlayerPtr->Get10msAudioFromFile(fileBuffer.get(),
niklase@google.com470e71d2011-07-07 08:21:25 +00003918 fileSamples,
3919 mixingFrequency) == -1)
3920 {
3921 WEBRTC_TRACE(kTraceWarning, kTraceVoice,
3922 VoEId(_instanceId, _channelId),
3923 "Channel::MixAudioWithFile() file mixing failed");
3924 return -1;
3925 }
3926 }
3927
andrew@webrtc.org63a50982012-05-02 23:56:37 +00003928 if (audioFrame.samples_per_channel_ == fileSamples)
niklase@google.com470e71d2011-07-07 08:21:25 +00003929 {
braveyao@webrtc.orgd7131432012-03-29 10:39:44 +00003930 // Currently file stream is always mono.
3931 // TODO(xians): Change the code when FilePlayer supports real stereo.
andrew@webrtc.org40ee3d02014-04-03 21:56:01 +00003932 MixWithSat(audioFrame.data_,
3933 audioFrame.num_channels_,
3934 fileBuffer.get(),
3935 1,
3936 fileSamples);
niklase@google.com470e71d2011-07-07 08:21:25 +00003937 }
3938 else
3939 {
3940 WEBRTC_TRACE(kTraceWarning, kTraceVoice, VoEId(_instanceId,_channelId),
andrew@webrtc.org63a50982012-05-02 23:56:37 +00003941 "Channel::MixAudioWithFile() samples_per_channel_(%d) != "
niklase@google.com470e71d2011-07-07 08:21:25 +00003942 "fileSamples(%d)",
andrew@webrtc.org63a50982012-05-02 23:56:37 +00003943 audioFrame.samples_per_channel_, fileSamples);
niklase@google.com470e71d2011-07-07 08:21:25 +00003944 return -1;
3945 }
3946
3947 return 0;
3948}
3949
3950int
3951Channel::InsertInbandDtmfTone()
3952{
niklas.enbom@webrtc.orgaf26f642011-11-16 12:41:36 +00003953 // Check if we should start a new tone.
niklase@google.com470e71d2011-07-07 08:21:25 +00003954 if (_inbandDtmfQueue.PendingDtmf() &&
3955 !_inbandDtmfGenerator.IsAddingTone() &&
3956 _inbandDtmfGenerator.DelaySinceLastTone() >
3957 kMinTelephoneEventSeparationMs)
3958 {
pbos@webrtc.org6141e132013-04-09 10:09:10 +00003959 int8_t eventCode(0);
3960 uint16_t lengthMs(0);
3961 uint8_t attenuationDb(0);
niklase@google.com470e71d2011-07-07 08:21:25 +00003962
3963 eventCode = _inbandDtmfQueue.NextDtmf(&lengthMs, &attenuationDb);
3964 _inbandDtmfGenerator.AddTone(eventCode, lengthMs, attenuationDb);
3965 if (_playInbandDtmfEvent)
3966 {
3967 // Add tone to output mixer using a reduced length to minimize
3968 // risk of echo.
3969 _outputMixerPtr->PlayDtmfTone(eventCode, lengthMs - 80,
3970 attenuationDb);
3971 }
3972 }
3973
3974 if (_inbandDtmfGenerator.IsAddingTone())
3975 {
pbos@webrtc.org6141e132013-04-09 10:09:10 +00003976 uint16_t frequency(0);
niklase@google.com470e71d2011-07-07 08:21:25 +00003977 _inbandDtmfGenerator.GetSampleRate(frequency);
3978
andrew@webrtc.org63a50982012-05-02 23:56:37 +00003979 if (frequency != _audioFrame.sample_rate_hz_)
niklase@google.com470e71d2011-07-07 08:21:25 +00003980 {
3981 // Update sample rate of Dtmf tone since the mixing frequency
3982 // has changed.
3983 _inbandDtmfGenerator.SetSampleRate(
pbos@webrtc.org6141e132013-04-09 10:09:10 +00003984 (uint16_t) (_audioFrame.sample_rate_hz_));
niklase@google.com470e71d2011-07-07 08:21:25 +00003985 // Reset the tone to be added taking the new sample rate into
3986 // account.
3987 _inbandDtmfGenerator.ResetTone();
3988 }
andrew@webrtc.orgae1a58b2013-01-22 04:44:30 +00003989
pbos@webrtc.org6141e132013-04-09 10:09:10 +00003990 int16_t toneBuffer[320];
3991 uint16_t toneSamples(0);
niklase@google.com470e71d2011-07-07 08:21:25 +00003992 // Get 10ms tone segment and set time since last tone to zero
3993 if (_inbandDtmfGenerator.Get10msTone(toneBuffer, toneSamples) == -1)
3994 {
3995 WEBRTC_TRACE(kTraceWarning, kTraceVoice,
3996 VoEId(_instanceId, _channelId),
3997 "Channel::EncodeAndSend() inserting Dtmf failed");
3998 return -1;
3999 }
4000
niklas.enbom@webrtc.orgaf26f642011-11-16 12:41:36 +00004001 // Replace mixed audio with DTMF tone.
andrew@webrtc.orgae1a58b2013-01-22 04:44:30 +00004002 for (int sample = 0;
andrew@webrtc.org63a50982012-05-02 23:56:37 +00004003 sample < _audioFrame.samples_per_channel_;
niklas.enbom@webrtc.orgaf26f642011-11-16 12:41:36 +00004004 sample++)
4005 {
andrew@webrtc.orgae1a58b2013-01-22 04:44:30 +00004006 for (int channel = 0;
4007 channel < _audioFrame.num_channels_;
niklas.enbom@webrtc.orgaf26f642011-11-16 12:41:36 +00004008 channel++)
4009 {
andrew@webrtc.orgae1a58b2013-01-22 04:44:30 +00004010 const int index = sample * _audioFrame.num_channels_ + channel;
4011 _audioFrame.data_[index] = toneBuffer[sample];
niklas.enbom@webrtc.orgaf26f642011-11-16 12:41:36 +00004012 }
4013 }
andrew@webrtc.orgae1a58b2013-01-22 04:44:30 +00004014
andrew@webrtc.org63a50982012-05-02 23:56:37 +00004015 assert(_audioFrame.samples_per_channel_ == toneSamples);
niklase@google.com470e71d2011-07-07 08:21:25 +00004016 } else
4017 {
4018 // Add 10ms to "delay-since-last-tone" counter
4019 _inbandDtmfGenerator.UpdateDelaySinceLastTone();
4020 }
4021 return 0;
4022}
4023
pbos@webrtc.org6141e132013-04-09 10:09:10 +00004024int32_t
pkasting@chromium.org4591fbd2014-11-20 22:28:14 +00004025Channel::SendPacketRaw(const void *data, size_t len, bool RTCP)
niklase@google.com470e71d2011-07-07 08:21:25 +00004026{
wu@webrtc.orgfb648da2013-10-18 21:10:51 +00004027 CriticalSectionScoped cs(&_callbackCritSect);
niklase@google.com470e71d2011-07-07 08:21:25 +00004028 if (_transportPtr == NULL)
4029 {
4030 return -1;
4031 }
4032 if (!RTCP)
4033 {
4034 return _transportPtr->SendPacket(_channelId, data, len);
4035 }
4036 else
4037 {
4038 return _transportPtr->SendRTCPPacket(_channelId, data, len);
4039 }
4040}
4041
pwestin@webrtc.org1de01352013-04-11 20:23:35 +00004042// Called for incoming RTP packets after successful RTP header parsing.
4043void Channel::UpdatePacketDelay(uint32_t rtp_timestamp,
4044 uint16_t sequence_number) {
4045 WEBRTC_TRACE(kTraceStream, kTraceVoice, VoEId(_instanceId,_channelId),
4046 "Channel::UpdatePacketDelay(timestamp=%lu, sequenceNumber=%u)",
4047 rtp_timestamp, sequence_number);
niklase@google.com470e71d2011-07-07 08:21:25 +00004048
pwestin@webrtc.org1de01352013-04-11 20:23:35 +00004049 // Get frequency of last received payload
wu@webrtc.org94454b72014-06-05 20:34:08 +00004050 int rtp_receive_frequency = GetPlayoutFrequency();
niklase@google.com470e71d2011-07-07 08:21:25 +00004051
turaj@webrtc.orge46c8d32013-05-22 20:39:43 +00004052 // Update the least required delay.
andrew@webrtc.orgeb524d92013-09-23 23:02:24 +00004053 least_required_delay_ms_ = audio_coding_->LeastRequiredDelayMs();
turaj@webrtc.orge46c8d32013-05-22 20:39:43 +00004054
turaj@webrtc.org167b6df2013-12-13 21:05:07 +00004055 // |jitter_buffer_playout_timestamp_| updated in UpdatePlayoutTimestamp for
4056 // every incoming packet.
4057 uint32_t timestamp_diff_ms = (rtp_timestamp -
4058 jitter_buffer_playout_timestamp_) / (rtp_receive_frequency / 1000);
henrik.lundin@webrtc.orgd6692992014-03-20 12:04:09 +00004059 if (!IsNewerTimestamp(rtp_timestamp, jitter_buffer_playout_timestamp_) ||
4060 timestamp_diff_ms > (2 * kVoiceEngineMaxMinPlayoutDelayMs)) {
4061 // If |jitter_buffer_playout_timestamp_| is newer than the incoming RTP
4062 // timestamp, the resulting difference is negative, but is set to zero.
4063 // This can happen when a network glitch causes a packet to arrive late,
4064 // and during long comfort noise periods with clock drift.
4065 timestamp_diff_ms = 0;
4066 }
niklase@google.com470e71d2011-07-07 08:21:25 +00004067
pwestin@webrtc.org1de01352013-04-11 20:23:35 +00004068 uint16_t packet_delay_ms = (rtp_timestamp - _previousTimestamp) /
4069 (rtp_receive_frequency / 1000);
niklase@google.com470e71d2011-07-07 08:21:25 +00004070
pwestin@webrtc.org1de01352013-04-11 20:23:35 +00004071 _previousTimestamp = rtp_timestamp;
niklase@google.com470e71d2011-07-07 08:21:25 +00004072
pwestin@webrtc.org1de01352013-04-11 20:23:35 +00004073 if (timestamp_diff_ms == 0) return;
niklase@google.com470e71d2011-07-07 08:21:25 +00004074
pwestin@webrtc.org1de01352013-04-11 20:23:35 +00004075 if (packet_delay_ms >= 10 && packet_delay_ms <= 60) {
4076 _recPacketDelayMs = packet_delay_ms;
4077 }
niklase@google.com470e71d2011-07-07 08:21:25 +00004078
pwestin@webrtc.org1de01352013-04-11 20:23:35 +00004079 if (_average_jitter_buffer_delay_us == 0) {
4080 _average_jitter_buffer_delay_us = timestamp_diff_ms * 1000;
4081 return;
4082 }
4083
4084 // Filter average delay value using exponential filter (alpha is
4085 // 7/8). We derive 1000 *_average_jitter_buffer_delay_us here (reduces
4086 // risk of rounding error) and compensate for it in GetDelayEstimate()
4087 // later.
4088 _average_jitter_buffer_delay_us = (_average_jitter_buffer_delay_us * 7 +
4089 1000 * timestamp_diff_ms + 500) / 8;
niklase@google.com470e71d2011-07-07 08:21:25 +00004090}
4091
4092void
4093Channel::RegisterReceiveCodecsToRTPModule()
4094{
4095 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId,_channelId),
4096 "Channel::RegisterReceiveCodecsToRTPModule()");
4097
4098
4099 CodecInst codec;
pbos@webrtc.org6141e132013-04-09 10:09:10 +00004100 const uint8_t nSupportedCodecs = AudioCodingModule::NumberOfCodecs();
niklase@google.com470e71d2011-07-07 08:21:25 +00004101
4102 for (int idx = 0; idx < nSupportedCodecs; idx++)
4103 {
4104 // Open up the RTP/RTCP receiver for all supported codecs
andrew@webrtc.orgeb524d92013-09-23 23:02:24 +00004105 if ((audio_coding_->Codec(idx, &codec) == -1) ||
wu@webrtc.org822fbd82013-08-15 23:38:54 +00004106 (rtp_receiver_->RegisterReceivePayload(
4107 codec.plname,
4108 codec.pltype,
4109 codec.plfreq,
4110 codec.channels,
4111 (codec.rate < 0) ? 0 : codec.rate) == -1))
niklase@google.com470e71d2011-07-07 08:21:25 +00004112 {
4113 WEBRTC_TRACE(
4114 kTraceWarning,
4115 kTraceVoice,
4116 VoEId(_instanceId, _channelId),
4117 "Channel::RegisterReceiveCodecsToRTPModule() unable"
4118 " to register %s (%d/%d/%d/%d) to RTP/RTCP receiver",
4119 codec.plname, codec.pltype, codec.plfreq,
4120 codec.channels, codec.rate);
4121 }
4122 else
4123 {
4124 WEBRTC_TRACE(
4125 kTraceInfo,
4126 kTraceVoice,
4127 VoEId(_instanceId, _channelId),
4128 "Channel::RegisterReceiveCodecsToRTPModule() %s "
wu@webrtc.orgfcd12b32011-09-15 20:49:50 +00004129 "(%d/%d/%d/%d) has been added to the RTP/RTCP "
niklase@google.com470e71d2011-07-07 08:21:25 +00004130 "receiver",
4131 codec.plname, codec.pltype, codec.plfreq,
4132 codec.channels, codec.rate);
4133 }
4134 }
4135}
4136
turaj@webrtc.org42259e72012-12-11 02:15:12 +00004137int Channel::SetSecondarySendCodec(const CodecInst& codec,
4138 int red_payload_type) {
turaj@webrtc.org8c8ad852013-01-31 18:20:17 +00004139 // Sanity check for payload type.
4140 if (red_payload_type < 0 || red_payload_type > 127) {
4141 _engineStatisticsPtr->SetLastError(
4142 VE_PLTYPE_ERROR, kTraceError,
4143 "SetRedPayloadType() invalid RED payload type");
4144 return -1;
4145 }
4146
turaj@webrtc.org42259e72012-12-11 02:15:12 +00004147 if (SetRedPayloadType(red_payload_type) < 0) {
4148 _engineStatisticsPtr->SetLastError(
4149 VE_AUDIO_CODING_MODULE_ERROR, kTraceError,
4150 "SetSecondarySendCodec() Failed to register RED ACM");
4151 return -1;
4152 }
andrew@webrtc.orgeb524d92013-09-23 23:02:24 +00004153 if (audio_coding_->RegisterSecondarySendCodec(codec) < 0) {
turaj@webrtc.org42259e72012-12-11 02:15:12 +00004154 _engineStatisticsPtr->SetLastError(
4155 VE_AUDIO_CODING_MODULE_ERROR, kTraceError,
4156 "SetSecondarySendCodec() Failed to register secondary send codec in "
4157 "ACM");
4158 return -1;
4159 }
4160
4161 return 0;
4162}
4163
4164void Channel::RemoveSecondarySendCodec() {
andrew@webrtc.orgeb524d92013-09-23 23:02:24 +00004165 audio_coding_->UnregisterSecondarySendCodec();
turaj@webrtc.org42259e72012-12-11 02:15:12 +00004166}
4167
4168int Channel::GetSecondarySendCodec(CodecInst* codec) {
andrew@webrtc.orgeb524d92013-09-23 23:02:24 +00004169 if (audio_coding_->SecondarySendCodec(codec) < 0) {
turaj@webrtc.org42259e72012-12-11 02:15:12 +00004170 _engineStatisticsPtr->SetLastError(
4171 VE_AUDIO_CODING_MODULE_ERROR, kTraceError,
4172 "GetSecondarySendCodec() Failed to get secondary sent codec from ACM");
4173 return -1;
4174 }
4175 return 0;
4176}
4177
turaj@webrtc.org8c8ad852013-01-31 18:20:17 +00004178// Assuming this method is called with valid payload type.
turaj@webrtc.org42259e72012-12-11 02:15:12 +00004179int Channel::SetRedPayloadType(int red_payload_type) {
turaj@webrtc.org42259e72012-12-11 02:15:12 +00004180 CodecInst codec;
4181 bool found_red = false;
4182
4183 // Get default RED settings from the ACM database
4184 const int num_codecs = AudioCodingModule::NumberOfCodecs();
4185 for (int idx = 0; idx < num_codecs; idx++) {
andrew@webrtc.orgeb524d92013-09-23 23:02:24 +00004186 audio_coding_->Codec(idx, &codec);
turaj@webrtc.org42259e72012-12-11 02:15:12 +00004187 if (!STR_CASE_CMP(codec.plname, "RED")) {
4188 found_red = true;
4189 break;
4190 }
4191 }
4192
4193 if (!found_red) {
4194 _engineStatisticsPtr->SetLastError(
4195 VE_CODEC_ERROR, kTraceError,
4196 "SetRedPayloadType() RED is not supported");
4197 return -1;
4198 }
4199
turaj@webrtc.org9d532fd2013-01-31 18:34:19 +00004200 codec.pltype = red_payload_type;
andrew@webrtc.orgeb524d92013-09-23 23:02:24 +00004201 if (audio_coding_->RegisterSendCodec(codec) < 0) {
turaj@webrtc.org42259e72012-12-11 02:15:12 +00004202 _engineStatisticsPtr->SetLastError(
4203 VE_AUDIO_CODING_MODULE_ERROR, kTraceError,
4204 "SetRedPayloadType() RED registration in ACM module failed");
4205 return -1;
4206 }
4207
4208 if (_rtpRtcpModule->SetSendREDPayloadType(red_payload_type) != 0) {
4209 _engineStatisticsPtr->SetLastError(
4210 VE_RTP_RTCP_MODULE_ERROR, kTraceError,
4211 "SetRedPayloadType() RED registration in RTP/RTCP module failed");
4212 return -1;
4213 }
4214 return 0;
4215}
4216
wu@webrtc.orgebdb0e32014-03-06 23:49:08 +00004217int Channel::SetSendRtpHeaderExtension(bool enable, RTPExtensionType type,
4218 unsigned char id) {
4219 int error = 0;
4220 _rtpRtcpModule->DeregisterSendRtpHeaderExtension(type);
4221 if (enable) {
4222 error = _rtpRtcpModule->RegisterSendRtpHeaderExtension(type, id);
4223 }
4224 return error;
4225}
minyue@webrtc.orgc1a40a72014-05-28 09:52:06 +00004226
wu@webrtc.org94454b72014-06-05 20:34:08 +00004227int32_t Channel::GetPlayoutFrequency() {
4228 int32_t playout_frequency = audio_coding_->PlayoutFrequency();
4229 CodecInst current_recive_codec;
4230 if (audio_coding_->ReceiveCodec(&current_recive_codec) == 0) {
4231 if (STR_CASE_CMP("G722", current_recive_codec.plname) == 0) {
4232 // Even though the actual sampling rate for G.722 audio is
4233 // 16,000 Hz, the RTP clock rate for the G722 payload format is
4234 // 8,000 Hz because that value was erroneously assigned in
4235 // RFC 1890 and must remain unchanged for backward compatibility.
4236 playout_frequency = 8000;
4237 } else if (STR_CASE_CMP("opus", current_recive_codec.plname) == 0) {
4238 // We are resampling Opus internally to 32,000 Hz until all our
4239 // DSP routines can operate at 48,000 Hz, but the RTP clock
4240 // rate for the Opus payload format is standardized to 48,000 Hz,
4241 // because that is the maximum supported decoding sampling rate.
4242 playout_frequency = 48000;
4243 }
4244 }
4245 return playout_frequency;
4246}
4247
minyue@webrtc.org2b58a442014-09-11 07:51:53 +00004248int Channel::GetRTT() const {
4249 RTCPMethod method = _rtpRtcpModule->RTCP();
4250 if (method == kRtcpOff) {
minyue@webrtc.org2b58a442014-09-11 07:51:53 +00004251 return 0;
4252 }
4253 std::vector<RTCPReportBlock> report_blocks;
4254 _rtpRtcpModule->RemoteRTCPStat(&report_blocks);
4255 if (report_blocks.empty()) {
minyue@webrtc.org2b58a442014-09-11 07:51:53 +00004256 return 0;
4257 }
4258
4259 uint32_t remoteSSRC = rtp_receiver_->SSRC();
4260 std::vector<RTCPReportBlock>::const_iterator it = report_blocks.begin();
4261 for (; it != report_blocks.end(); ++it) {
4262 if (it->remoteSSRC == remoteSSRC)
4263 break;
4264 }
4265 if (it == report_blocks.end()) {
4266 // We have not received packets with SSRC matching the report blocks.
4267 // To calculate RTT we try with the SSRC of the first report block.
4268 // This is very important for send-only channels where we don't know
4269 // the SSRC of the other end.
4270 remoteSSRC = report_blocks[0].remoteSSRC;
4271 }
4272 uint16_t rtt = 0;
4273 uint16_t avg_rtt = 0;
4274 uint16_t max_rtt= 0;
4275 uint16_t min_rtt = 0;
4276 if (_rtpRtcpModule->RTT(remoteSSRC, &rtt, &avg_rtt, &min_rtt, &max_rtt)
4277 != 0) {
minyue@webrtc.org2b58a442014-09-11 07:51:53 +00004278 return 0;
4279 }
4280 return static_cast<int>(rtt);
4281}
4282
pbos@webrtc.orgd900e8b2013-07-03 15:12:26 +00004283} // namespace voe
4284} // namespace webrtc