blob: d651d8f76de60989effe045dd2dc58399376d485 [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
wu@webrtc.org94454b72014-06-05 20:34:08 +000013#include "webrtc/base/timeutils.h"
minyue@webrtc.orge509f942013-09-12 17:03:00 +000014#include "webrtc/common.h"
turaj@webrtc.org6388c3e2013-02-12 21:42:18 +000015#include "webrtc/modules/audio_device/include/audio_device.h"
16#include "webrtc/modules/audio_processing/include/audio_processing.h"
henrik.lundin@webrtc.orgd6692992014-03-20 12:04:09 +000017#include "webrtc/modules/interface/module_common_types.h"
wu@webrtc.org822fbd82013-08-15 23:38:54 +000018#include "webrtc/modules/rtp_rtcp/interface/receive_statistics.h"
19#include "webrtc/modules/rtp_rtcp/interface/rtp_payload_registry.h"
20#include "webrtc/modules/rtp_rtcp/interface/rtp_receiver.h"
21#include "webrtc/modules/rtp_rtcp/source/rtp_receiver_strategy.h"
turaj@webrtc.org6388c3e2013-02-12 21:42:18 +000022#include "webrtc/modules/utility/interface/audio_frame_operations.h"
23#include "webrtc/modules/utility/interface/process_thread.h"
24#include "webrtc/modules/utility/interface/rtp_dump.h"
25#include "webrtc/system_wrappers/interface/critical_section_wrapper.h"
26#include "webrtc/system_wrappers/interface/logging.h"
27#include "webrtc/system_wrappers/interface/trace.h"
solenberg@webrtc.orgb1f50102014-03-24 10:38:25 +000028#include "webrtc/video_engine/include/vie_network.h"
turaj@webrtc.org6388c3e2013-02-12 21:42:18 +000029#include "webrtc/voice_engine/include/voe_base.h"
30#include "webrtc/voice_engine/include/voe_external_media.h"
31#include "webrtc/voice_engine/include/voe_rtp_rtcp.h"
32#include "webrtc/voice_engine/output_mixer.h"
33#include "webrtc/voice_engine/statistics.h"
34#include "webrtc/voice_engine/transmit_mixer.h"
35#include "webrtc/voice_engine/utility.h"
niklase@google.com470e71d2011-07-07 08:21:25 +000036
37#if defined(_WIN32)
38#include <Qos.h>
39#endif
40
andrew@webrtc.org50419b02012-11-14 19:07:54 +000041namespace webrtc {
42namespace voe {
niklase@google.com470e71d2011-07-07 08:21:25 +000043
sprang@webrtc.org54ae4ff2013-12-19 13:26:02 +000044// Extend the default RTCP statistics struct with max_jitter, defined as the
45// maximum jitter value seen in an RTCP report block.
46struct ChannelStatistics : public RtcpStatistics {
47 ChannelStatistics() : rtcp(), max_jitter(0) {}
48
49 RtcpStatistics rtcp;
50 uint32_t max_jitter;
51};
52
53// Statistics callback, called at each generation of a new RTCP report block.
54class StatisticsProxy : public RtcpStatisticsCallback {
55 public:
56 StatisticsProxy(uint32_t ssrc)
57 : stats_lock_(CriticalSectionWrapper::CreateCriticalSection()),
58 ssrc_(ssrc) {}
59 virtual ~StatisticsProxy() {}
60
61 virtual void StatisticsUpdated(const RtcpStatistics& statistics,
62 uint32_t ssrc) OVERRIDE {
63 if (ssrc != ssrc_)
64 return;
65
66 CriticalSectionScoped cs(stats_lock_.get());
67 stats_.rtcp = statistics;
68 if (statistics.jitter > stats_.max_jitter) {
69 stats_.max_jitter = statistics.jitter;
70 }
71 }
72
73 void ResetStatistics() {
74 CriticalSectionScoped cs(stats_lock_.get());
75 stats_ = ChannelStatistics();
76 }
77
78 ChannelStatistics GetStats() {
79 CriticalSectionScoped cs(stats_lock_.get());
80 return stats_;
81 }
82
83 private:
84 // StatisticsUpdated calls are triggered from threads in the RTP module,
85 // while GetStats calls can be triggered from the public voice engine API,
86 // hence synchronization is needed.
87 scoped_ptr<CriticalSectionWrapper> stats_lock_;
88 const uint32_t ssrc_;
89 ChannelStatistics stats_;
90};
91
minyue@webrtc.orgc1a40a72014-05-28 09:52:06 +000092class VoEBitrateObserver : public BitrateObserver {
93 public:
94 explicit VoEBitrateObserver(Channel* owner)
95 : owner_(owner) {}
96 virtual ~VoEBitrateObserver() {}
97
98 // Implements BitrateObserver.
99 virtual void OnNetworkChanged(const uint32_t bitrate_bps,
100 const uint8_t fraction_lost,
101 const uint32_t rtt) OVERRIDE {
102 // |fraction_lost| has a scale of 0 - 255.
103 owner_->OnNetworkChanged(bitrate_bps, fraction_lost, rtt);
104 }
105
106 private:
107 Channel* owner_;
108};
109
pbos@webrtc.org6141e132013-04-09 10:09:10 +0000110int32_t
niklase@google.com470e71d2011-07-07 08:21:25 +0000111Channel::SendData(FrameType frameType,
pbos@webrtc.org6141e132013-04-09 10:09:10 +0000112 uint8_t payloadType,
113 uint32_t timeStamp,
114 const uint8_t* payloadData,
115 uint16_t payloadSize,
niklase@google.com470e71d2011-07-07 08:21:25 +0000116 const RTPFragmentationHeader* fragmentation)
117{
118 WEBRTC_TRACE(kTraceStream, kTraceVoice, VoEId(_instanceId,_channelId),
119 "Channel::SendData(frameType=%u, payloadType=%u, timeStamp=%u,"
120 " payloadSize=%u, fragmentation=0x%x)",
121 frameType, payloadType, timeStamp, payloadSize, fragmentation);
122
123 if (_includeAudioLevelIndication)
124 {
125 // Store current audio level in the RTP/RTCP module.
126 // The level will be used in combination with voice-activity state
127 // (frameType) to add an RTP header extension
andrew@webrtc.org382c0c22014-05-05 18:22:21 +0000128 _rtpRtcpModule->SetAudioLevel(rms_level_.RMS());
niklase@google.com470e71d2011-07-07 08:21:25 +0000129 }
130
131 // Push data from ACM to RTP/RTCP-module to deliver audio frame for
132 // packetization.
133 // This call will trigger Transport::SendPacket() from the RTP/RTCP module.
pwestin@webrtc.org2853dde2012-05-11 11:08:54 +0000134 if (_rtpRtcpModule->SendOutgoingData((FrameType&)frameType,
niklase@google.com470e71d2011-07-07 08:21:25 +0000135 payloadType,
136 timeStamp,
stefan@webrtc.orgddfdfed2012-07-03 13:21:22 +0000137 // Leaving the time when this frame was
138 // received from the capture device as
139 // undefined for voice for now.
140 -1,
niklase@google.com470e71d2011-07-07 08:21:25 +0000141 payloadData,
142 payloadSize,
143 fragmentation) == -1)
144 {
145 _engineStatisticsPtr->SetLastError(
146 VE_RTP_RTCP_MODULE_ERROR, kTraceWarning,
147 "Channel::SendData() failed to send data to RTP/RTCP module");
148 return -1;
149 }
150
151 _lastLocalTimeStamp = timeStamp;
152 _lastPayloadType = payloadType;
153
154 return 0;
155}
156
pbos@webrtc.org6141e132013-04-09 10:09:10 +0000157int32_t
158Channel::InFrameType(int16_t frameType)
niklase@google.com470e71d2011-07-07 08:21:25 +0000159{
160 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId,_channelId),
161 "Channel::InFrameType(frameType=%d)", frameType);
162
mflodman@webrtc.org9a065d12012-03-07 08:12:21 +0000163 CriticalSectionScoped cs(&_callbackCritSect);
niklase@google.com470e71d2011-07-07 08:21:25 +0000164 // 1 indicates speech
165 _sendFrameType = (frameType == 1) ? 1 : 0;
166 return 0;
167}
168
pbos@webrtc.org6141e132013-04-09 10:09:10 +0000169int32_t
pbos@webrtc.org92135212013-05-14 08:31:39 +0000170Channel::OnRxVadDetected(int vadDecision)
niklase@google.com470e71d2011-07-07 08:21:25 +0000171{
172 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId, _channelId),
173 "Channel::OnRxVadDetected(vadDecision=%d)", vadDecision);
174
mflodman@webrtc.org9a065d12012-03-07 08:12:21 +0000175 CriticalSectionScoped cs(&_callbackCritSect);
niklase@google.com470e71d2011-07-07 08:21:25 +0000176 if (_rxVadObserverPtr)
177 {
178 _rxVadObserverPtr->OnRxVad(_channelId, vadDecision);
179 }
180
181 return 0;
182}
183
184int
185Channel::SendPacket(int channel, const void *data, int len)
186{
187 channel = VoEChannelId(channel);
188 assert(channel == _channelId);
189
190 WEBRTC_TRACE(kTraceStream, kTraceVoice, VoEId(_instanceId,_channelId),
191 "Channel::SendPacket(channel=%d, len=%d)", channel, len);
192
wu@webrtc.orgfb648da2013-10-18 21:10:51 +0000193 CriticalSectionScoped cs(&_callbackCritSect);
194
niklase@google.com470e71d2011-07-07 08:21:25 +0000195 if (_transportPtr == NULL)
196 {
197 WEBRTC_TRACE(kTraceError, kTraceVoice, VoEId(_instanceId,_channelId),
198 "Channel::SendPacket() failed to send RTP packet due to"
199 " invalid transport object");
200 return -1;
201 }
202
pbos@webrtc.org6141e132013-04-09 10:09:10 +0000203 uint8_t* bufferToSendPtr = (uint8_t*)data;
204 int32_t bufferLength = len;
niklase@google.com470e71d2011-07-07 08:21:25 +0000205
206 // Dump the RTP packet to a file (if RTP dump is enabled).
pbos@webrtc.org6141e132013-04-09 10:09:10 +0000207 if (_rtpDumpOut.DumpPacket((const uint8_t*)data, len) == -1)
niklase@google.com470e71d2011-07-07 08:21:25 +0000208 {
209 WEBRTC_TRACE(kTraceWarning, kTraceVoice,
210 VoEId(_instanceId,_channelId),
211 "Channel::SendPacket() RTP dump to output file failed");
212 }
213
wu@webrtc.orgfb648da2013-10-18 21:10:51 +0000214 int n = _transportPtr->SendPacket(channel, bufferToSendPtr,
215 bufferLength);
216 if (n < 0) {
217 std::string transport_name =
218 _externalTransport ? "external transport" : "WebRtc sockets";
219 WEBRTC_TRACE(kTraceError, kTraceVoice,
220 VoEId(_instanceId,_channelId),
221 "Channel::SendPacket() RTP transmission using %s failed",
222 transport_name.c_str());
223 return -1;
niklase@google.com470e71d2011-07-07 08:21:25 +0000224 }
wu@webrtc.orgfb648da2013-10-18 21:10:51 +0000225 return n;
niklase@google.com470e71d2011-07-07 08:21:25 +0000226}
227
228int
229Channel::SendRTCPPacket(int channel, const void *data, int len)
230{
231 channel = VoEChannelId(channel);
232 assert(channel == _channelId);
233
234 WEBRTC_TRACE(kTraceStream, kTraceVoice, VoEId(_instanceId,_channelId),
235 "Channel::SendRTCPPacket(channel=%d, len=%d)", channel, len);
236
wu@webrtc.orgfb648da2013-10-18 21:10:51 +0000237 CriticalSectionScoped cs(&_callbackCritSect);
238 if (_transportPtr == NULL)
niklase@google.com470e71d2011-07-07 08:21:25 +0000239 {
wu@webrtc.orgfb648da2013-10-18 21:10:51 +0000240 WEBRTC_TRACE(kTraceError, kTraceVoice,
241 VoEId(_instanceId,_channelId),
242 "Channel::SendRTCPPacket() failed to send RTCP packet"
243 " due to invalid transport object");
244 return -1;
niklase@google.com470e71d2011-07-07 08:21:25 +0000245 }
246
pbos@webrtc.org6141e132013-04-09 10:09:10 +0000247 uint8_t* bufferToSendPtr = (uint8_t*)data;
248 int32_t bufferLength = len;
niklase@google.com470e71d2011-07-07 08:21:25 +0000249
250 // Dump the RTCP packet to a file (if RTP dump is enabled).
pbos@webrtc.org6141e132013-04-09 10:09:10 +0000251 if (_rtpDumpOut.DumpPacket((const uint8_t*)data, len) == -1)
niklase@google.com470e71d2011-07-07 08:21:25 +0000252 {
253 WEBRTC_TRACE(kTraceWarning, kTraceVoice,
254 VoEId(_instanceId,_channelId),
255 "Channel::SendPacket() RTCP dump to output file failed");
256 }
257
wu@webrtc.orgfb648da2013-10-18 21:10:51 +0000258 int n = _transportPtr->SendRTCPPacket(channel,
259 bufferToSendPtr,
260 bufferLength);
261 if (n < 0) {
262 std::string transport_name =
263 _externalTransport ? "external transport" : "WebRtc sockets";
264 WEBRTC_TRACE(kTraceInfo, kTraceVoice,
265 VoEId(_instanceId,_channelId),
266 "Channel::SendRTCPPacket() transmission using %s failed",
267 transport_name.c_str());
268 return -1;
niklase@google.com470e71d2011-07-07 08:21:25 +0000269 }
wu@webrtc.orgfb648da2013-10-18 21:10:51 +0000270 return n;
niklase@google.com470e71d2011-07-07 08:21:25 +0000271}
272
273void
pbos@webrtc.org92135212013-05-14 08:31:39 +0000274Channel::OnPlayTelephoneEvent(int32_t id,
275 uint8_t event,
276 uint16_t lengthMs,
277 uint8_t volume)
niklase@google.com470e71d2011-07-07 08:21:25 +0000278{
279 WEBRTC_TRACE(kTraceStream, kTraceVoice, VoEId(_instanceId,_channelId),
280 "Channel::OnPlayTelephoneEvent(id=%d, event=%u, lengthMs=%u,"
wu@webrtc.orgfcd12b32011-09-15 20:49:50 +0000281 " volume=%u)", id, event, lengthMs, volume);
niklase@google.com470e71d2011-07-07 08:21:25 +0000282
283 if (!_playOutbandDtmfEvent || (event > 15))
284 {
285 // Ignore callback since feedback is disabled or event is not a
286 // Dtmf tone event.
287 return;
288 }
289
290 assert(_outputMixerPtr != NULL);
291
292 // Start playing out the Dtmf tone (if playout is enabled).
293 // Reduce length of tone with 80ms to the reduce risk of echo.
294 _outputMixerPtr->PlayDtmfTone(event, lengthMs - 80, volume);
295}
296
297void
stefan@webrtc.org286fe0b2013-08-21 20:58:21 +0000298Channel::OnIncomingSSRCChanged(int32_t id, uint32_t ssrc)
niklase@google.com470e71d2011-07-07 08:21:25 +0000299{
300 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId,_channelId),
301 "Channel::OnIncomingSSRCChanged(id=%d, SSRC=%d)",
stefan@webrtc.org286fe0b2013-08-21 20:58:21 +0000302 id, ssrc);
niklase@google.com470e71d2011-07-07 08:21:25 +0000303
dwkang@webrtc.orgb295a3f2013-08-29 07:34:12 +0000304 // Update ssrc so that NTP for AV sync can be updated.
305 _rtpRtcpModule->SetRemoteSSRC(ssrc);
niklase@google.com470e71d2011-07-07 08:21:25 +0000306}
307
pbos@webrtc.org92135212013-05-14 08:31:39 +0000308void Channel::OnIncomingCSRCChanged(int32_t id,
309 uint32_t CSRC,
310 bool added)
niklase@google.com470e71d2011-07-07 08:21:25 +0000311{
312 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId,_channelId),
313 "Channel::OnIncomingCSRCChanged(id=%d, CSRC=%d, added=%d)",
314 id, CSRC, added);
niklase@google.com470e71d2011-07-07 08:21:25 +0000315}
316
stefan@webrtc.org286fe0b2013-08-21 20:58:21 +0000317void Channel::ResetStatistics(uint32_t ssrc) {
318 StreamStatistician* statistician =
319 rtp_receive_statistics_->GetStatistician(ssrc);
320 if (statistician) {
321 statistician->ResetStatistics();
322 }
sprang@webrtc.org54ae4ff2013-12-19 13:26:02 +0000323 statistics_proxy_->ResetStatistics();
wu@webrtc.org822fbd82013-08-15 23:38:54 +0000324}
325
pbos@webrtc.org6141e132013-04-09 10:09:10 +0000326int32_t
niklase@google.com470e71d2011-07-07 08:21:25 +0000327Channel::OnInitializeDecoder(
pbos@webrtc.org92135212013-05-14 08:31:39 +0000328 int32_t id,
329 int8_t payloadType,
leozwang@webrtc.org813e4b02012-03-01 18:34:25 +0000330 const char payloadName[RTP_PAYLOAD_NAME_SIZE],
pbos@webrtc.org92135212013-05-14 08:31:39 +0000331 int frequency,
332 uint8_t channels,
333 uint32_t rate)
niklase@google.com470e71d2011-07-07 08:21:25 +0000334{
335 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId,_channelId),
336 "Channel::OnInitializeDecoder(id=%d, payloadType=%d, "
337 "payloadName=%s, frequency=%u, channels=%u, rate=%u)",
338 id, payloadType, payloadName, frequency, channels, rate);
339
andrew@webrtc.orgceb148c2011-08-23 17:53:54 +0000340 assert(VoEChannelId(id) == _channelId);
niklase@google.com470e71d2011-07-07 08:21:25 +0000341
henrika@webrtc.orgf75901f2012-01-16 08:45:42 +0000342 CodecInst receiveCodec = {0};
343 CodecInst dummyCodec = {0};
niklase@google.com470e71d2011-07-07 08:21:25 +0000344
345 receiveCodec.pltype = payloadType;
niklase@google.com470e71d2011-07-07 08:21:25 +0000346 receiveCodec.plfreq = frequency;
347 receiveCodec.channels = channels;
348 receiveCodec.rate = rate;
henrika@webrtc.orgf75901f2012-01-16 08:45:42 +0000349 strncpy(receiveCodec.plname, payloadName, RTP_PAYLOAD_NAME_SIZE - 1);
andrew@webrtc.orgae1a58b2013-01-22 04:44:30 +0000350
andrew@webrtc.orgeb524d92013-09-23 23:02:24 +0000351 audio_coding_->Codec(payloadName, &dummyCodec, frequency, channels);
niklase@google.com470e71d2011-07-07 08:21:25 +0000352 receiveCodec.pacsize = dummyCodec.pacsize;
353
354 // Register the new codec to the ACM
andrew@webrtc.orgeb524d92013-09-23 23:02:24 +0000355 if (audio_coding_->RegisterReceiveCodec(receiveCodec) == -1)
niklase@google.com470e71d2011-07-07 08:21:25 +0000356 {
357 WEBRTC_TRACE(kTraceWarning, kTraceVoice,
andrew@webrtc.orgceb148c2011-08-23 17:53:54 +0000358 VoEId(_instanceId, _channelId),
niklase@google.com470e71d2011-07-07 08:21:25 +0000359 "Channel::OnInitializeDecoder() invalid codec ("
360 "pt=%d, name=%s) received - 1", payloadType, payloadName);
361 _engineStatisticsPtr->SetLastError(VE_AUDIO_CODING_MODULE_ERROR);
362 return -1;
363 }
364
365 return 0;
366}
367
pbos@webrtc.org6141e132013-04-09 10:09:10 +0000368int32_t
369Channel::OnReceivedPayloadData(const uint8_t* payloadData,
pbos@webrtc.org92135212013-05-14 08:31:39 +0000370 uint16_t payloadSize,
niklase@google.com470e71d2011-07-07 08:21:25 +0000371 const WebRtcRTPHeader* rtpHeader)
372{
373 WEBRTC_TRACE(kTraceStream, kTraceVoice, VoEId(_instanceId,_channelId),
374 "Channel::OnReceivedPayloadData(payloadSize=%d,"
375 " payloadType=%u, audioChannel=%u)",
376 payloadSize,
377 rtpHeader->header.payloadType,
378 rtpHeader->type.Audio.channel);
379
henrika@webrtc.org944cbeb2014-03-18 10:32:33 +0000380 if (!channel_state_.Get().playing)
niklase@google.com470e71d2011-07-07 08:21:25 +0000381 {
382 // Avoid inserting into NetEQ when we are not playing. Count the
383 // packet as discarded.
384 WEBRTC_TRACE(kTraceStream, kTraceVoice,
385 VoEId(_instanceId, _channelId),
386 "received packet is discarded since playing is not"
387 " activated");
388 _numberOfDiscardedPackets++;
389 return 0;
390 }
391
392 // Push the incoming payload (parsed and ready for decoding) into the ACM
andrew@webrtc.orgeb524d92013-09-23 23:02:24 +0000393 if (audio_coding_->IncomingPacket(payloadData,
394 payloadSize,
395 *rtpHeader) != 0)
niklase@google.com470e71d2011-07-07 08:21:25 +0000396 {
397 _engineStatisticsPtr->SetLastError(
398 VE_AUDIO_CODING_MODULE_ERROR, kTraceWarning,
399 "Channel::OnReceivedPayloadData() unable to push data to the ACM");
400 return -1;
401 }
402
pwestin@webrtc.orgd30859e2013-06-06 21:09:01 +0000403 // Update the packet delay.
niklase@google.com470e71d2011-07-07 08:21:25 +0000404 UpdatePacketDelay(rtpHeader->header.timestamp,
405 rtpHeader->header.sequenceNumber);
pwestin@webrtc.orgd30859e2013-06-06 21:09:01 +0000406
wu@webrtc.org822fbd82013-08-15 23:38:54 +0000407 uint16_t round_trip_time = 0;
408 _rtpRtcpModule->RTT(rtp_receiver_->SSRC(), &round_trip_time,
409 NULL, NULL, NULL);
pwestin@webrtc.orgd30859e2013-06-06 21:09:01 +0000410
andrew@webrtc.orgeb524d92013-09-23 23:02:24 +0000411 std::vector<uint16_t> nack_list = audio_coding_->GetNackList(
wu@webrtc.org822fbd82013-08-15 23:38:54 +0000412 round_trip_time);
413 if (!nack_list.empty()) {
414 // Can't use nack_list.data() since it's not supported by all
415 // compilers.
416 ResendPackets(&(nack_list[0]), static_cast<int>(nack_list.size()));
pwestin@webrtc.orgd30859e2013-06-06 21:09:01 +0000417 }
niklase@google.com470e71d2011-07-07 08:21:25 +0000418 return 0;
419}
420
stefan@webrtc.org7bb8f022013-09-06 13:40:11 +0000421bool Channel::OnRecoveredPacket(const uint8_t* rtp_packet,
422 int rtp_packet_length) {
423 RTPHeader header;
424 if (!rtp_header_parser_->Parse(rtp_packet, rtp_packet_length, &header)) {
425 WEBRTC_TRACE(kTraceDebug, webrtc::kTraceVoice, _channelId,
426 "IncomingPacket invalid RTP header");
427 return false;
428 }
429 header.payload_type_frequency =
430 rtp_payload_registry_->GetPayloadTypeFrequency(header.payloadType);
431 if (header.payload_type_frequency < 0)
432 return false;
433 return ReceivePacket(rtp_packet, rtp_packet_length, header, false);
434}
435
pbos@webrtc.org92135212013-05-14 08:31:39 +0000436int32_t Channel::GetAudioFrame(int32_t id, AudioFrame& audioFrame)
niklase@google.com470e71d2011-07-07 08:21:25 +0000437{
438 WEBRTC_TRACE(kTraceStream, kTraceVoice, VoEId(_instanceId,_channelId),
439 "Channel::GetAudioFrame(id=%d)", id);
440
441 // Get 10ms raw PCM data from the ACM (mixer limits output frequency)
andrew@webrtc.orgeb524d92013-09-23 23:02:24 +0000442 if (audio_coding_->PlayoutData10Ms(audioFrame.sample_rate_hz_,
443 &audioFrame) == -1)
niklase@google.com470e71d2011-07-07 08:21:25 +0000444 {
445 WEBRTC_TRACE(kTraceError, kTraceVoice,
446 VoEId(_instanceId,_channelId),
447 "Channel::GetAudioFrame() PlayoutData10Ms() failed!");
andrew@webrtc.org7859e102012-01-13 00:30:11 +0000448 // In all likelihood, the audio in this frame is garbage. We return an
449 // error so that the audio mixer module doesn't add it to the mix. As
450 // a result, it won't be played out and the actions skipped here are
451 // irrelevant.
452 return -1;
niklase@google.com470e71d2011-07-07 08:21:25 +0000453 }
454
455 if (_RxVadDetection)
456 {
457 UpdateRxVadDetection(audioFrame);
458 }
459
460 // Convert module ID to internal VoE channel ID
andrew@webrtc.org63a50982012-05-02 23:56:37 +0000461 audioFrame.id_ = VoEChannelId(audioFrame.id_);
niklase@google.com470e71d2011-07-07 08:21:25 +0000462 // Store speech type for dead-or-alive detection
andrew@webrtc.org63a50982012-05-02 23:56:37 +0000463 _outputSpeechType = audioFrame.speech_type_;
niklase@google.com470e71d2011-07-07 08:21:25 +0000464
henrika@webrtc.org944cbeb2014-03-18 10:32:33 +0000465 ChannelState::State state = channel_state_.Get();
466
467 if (state.rx_apm_is_enabled) {
andrew@webrtc.org60730cf2014-01-07 17:45:09 +0000468 int err = rx_audioproc_->ProcessStream(&audioFrame);
469 if (err) {
470 LOG(LS_ERROR) << "ProcessStream() error: " << err;
471 assert(false);
472 }
niklase@google.com470e71d2011-07-07 08:21:25 +0000473 }
474
wu@webrtc.org63420662013-10-17 18:28:55 +0000475 float output_gain = 1.0f;
476 float left_pan = 1.0f;
477 float right_pan = 1.0f;
niklase@google.com470e71d2011-07-07 08:21:25 +0000478 {
wu@webrtc.org63420662013-10-17 18:28:55 +0000479 CriticalSectionScoped cs(&volume_settings_critsect_);
480 output_gain = _outputGain;
481 left_pan = _panLeft;
482 right_pan= _panRight;
483 }
484
485 // Output volume scaling
486 if (output_gain < 0.99f || output_gain > 1.01f)
487 {
488 AudioFrameOperations::ScaleWithSat(output_gain, audioFrame);
niklase@google.com470e71d2011-07-07 08:21:25 +0000489 }
490
491 // Scale left and/or right channel(s) if stereo and master balance is
492 // active
493
wu@webrtc.org63420662013-10-17 18:28:55 +0000494 if (left_pan != 1.0f || right_pan != 1.0f)
niklase@google.com470e71d2011-07-07 08:21:25 +0000495 {
andrew@webrtc.org63a50982012-05-02 23:56:37 +0000496 if (audioFrame.num_channels_ == 1)
niklase@google.com470e71d2011-07-07 08:21:25 +0000497 {
498 // Emulate stereo mode since panning is active.
499 // The mono signal is copied to both left and right channels here.
andrew@webrtc.org4ecea3e2012-06-27 03:25:31 +0000500 AudioFrameOperations::MonoToStereo(&audioFrame);
niklase@google.com470e71d2011-07-07 08:21:25 +0000501 }
502 // For true stereo mode (when we are receiving a stereo signal), no
503 // action is needed.
504
505 // Do the panning operation (the audio frame contains stereo at this
506 // stage)
wu@webrtc.org63420662013-10-17 18:28:55 +0000507 AudioFrameOperations::Scale(left_pan, right_pan, audioFrame);
niklase@google.com470e71d2011-07-07 08:21:25 +0000508 }
509
510 // Mix decoded PCM output with file if file mixing is enabled
henrika@webrtc.org944cbeb2014-03-18 10:32:33 +0000511 if (state.output_file_playing)
niklase@google.com470e71d2011-07-07 08:21:25 +0000512 {
andrew@webrtc.org63a50982012-05-02 23:56:37 +0000513 MixAudioWithFile(audioFrame, audioFrame.sample_rate_hz_);
niklase@google.com470e71d2011-07-07 08:21:25 +0000514 }
515
niklase@google.com470e71d2011-07-07 08:21:25 +0000516 // External media
517 if (_outputExternalMedia)
518 {
mflodman@webrtc.org9a065d12012-03-07 08:12:21 +0000519 CriticalSectionScoped cs(&_callbackCritSect);
andrew@webrtc.org63a50982012-05-02 23:56:37 +0000520 const bool isStereo = (audioFrame.num_channels_ == 2);
niklase@google.com470e71d2011-07-07 08:21:25 +0000521 if (_outputExternalMediaCallbackPtr)
522 {
523 _outputExternalMediaCallbackPtr->Process(
524 _channelId,
525 kPlaybackPerChannel,
pbos@webrtc.org6141e132013-04-09 10:09:10 +0000526 (int16_t*)audioFrame.data_,
andrew@webrtc.org63a50982012-05-02 23:56:37 +0000527 audioFrame.samples_per_channel_,
528 audioFrame.sample_rate_hz_,
niklase@google.com470e71d2011-07-07 08:21:25 +0000529 isStereo);
530 }
531 }
532
533 // Record playout if enabled
534 {
mflodman@webrtc.org9a065d12012-03-07 08:12:21 +0000535 CriticalSectionScoped cs(&_fileCritSect);
niklase@google.com470e71d2011-07-07 08:21:25 +0000536
537 if (_outputFileRecording && _outputFileRecorderPtr)
538 {
niklas.enbom@webrtc.org5398d952012-03-26 08:11:25 +0000539 _outputFileRecorderPtr->RecordAudioToFile(audioFrame);
niklase@google.com470e71d2011-07-07 08:21:25 +0000540 }
541 }
542
543 // Measure audio level (0-9)
544 _outputAudioLevel.ComputeLevel(audioFrame);
545
wu@webrtc.org94454b72014-06-05 20:34:08 +0000546 if (capture_start_rtp_time_stamp_ < 0 && audioFrame.timestamp_ != 0) {
547 // The first frame with a valid rtp timestamp.
wu@webrtc.orgcb711f72014-05-19 17:39:11 +0000548 capture_start_rtp_time_stamp_ = audioFrame.timestamp_;
wu@webrtc.org94454b72014-06-05 20:34:08 +0000549 }
550
551 if (capture_start_rtp_time_stamp_ >= 0) {
552 // audioFrame.timestamp_ should be valid from now on.
553
554 // Compute elapsed time.
555 int64_t unwrap_timestamp =
556 rtp_ts_wraparound_handler_->Unwrap(audioFrame.timestamp_);
557 audioFrame.elapsed_time_ms_ =
558 (unwrap_timestamp - capture_start_rtp_time_stamp_) /
559 (GetPlayoutFrequency() / 1000);
560
stefan@webrtc.org8e24d872014-09-02 18:58:24 +0000561 {
wu@webrtc.orgcb711f72014-05-19 17:39:11 +0000562 CriticalSectionScoped lock(ts_stats_lock_.get());
stefan@webrtc.org8e24d872014-09-02 18:58:24 +0000563 // Compute ntp time.
564 audioFrame.ntp_time_ms_ = ntp_estimator_.Estimate(
565 audioFrame.timestamp_);
566 // |ntp_time_ms_| won't be valid until at least 2 RTCP SRs are received.
567 if (audioFrame.ntp_time_ms_ > 0) {
568 // Compute |capture_start_ntp_time_ms_| so that
569 // |capture_start_ntp_time_ms_| + |elapsed_time_ms_| == |ntp_time_ms_|
570 capture_start_ntp_time_ms_ =
571 audioFrame.ntp_time_ms_ - audioFrame.elapsed_time_ms_;
572 }
wu@webrtc.orgcb711f72014-05-19 17:39:11 +0000573 }
574 }
575
niklase@google.com470e71d2011-07-07 08:21:25 +0000576 return 0;
577}
578
pbos@webrtc.org6141e132013-04-09 10:09:10 +0000579int32_t
pbos@webrtc.org92135212013-05-14 08:31:39 +0000580Channel::NeededFrequency(int32_t id)
niklase@google.com470e71d2011-07-07 08:21:25 +0000581{
582 WEBRTC_TRACE(kTraceStream, kTraceVoice, VoEId(_instanceId,_channelId),
583 "Channel::NeededFrequency(id=%d)", id);
584
585 int highestNeeded = 0;
586
587 // Determine highest needed receive frequency
andrew@webrtc.orgeb524d92013-09-23 23:02:24 +0000588 int32_t receiveFrequency = audio_coding_->ReceiveFrequency();
niklase@google.com470e71d2011-07-07 08:21:25 +0000589
590 // Return the bigger of playout and receive frequency in the ACM.
andrew@webrtc.orgeb524d92013-09-23 23:02:24 +0000591 if (audio_coding_->PlayoutFrequency() > receiveFrequency)
niklase@google.com470e71d2011-07-07 08:21:25 +0000592 {
andrew@webrtc.orgeb524d92013-09-23 23:02:24 +0000593 highestNeeded = audio_coding_->PlayoutFrequency();
niklase@google.com470e71d2011-07-07 08:21:25 +0000594 }
595 else
596 {
597 highestNeeded = receiveFrequency;
598 }
599
600 // Special case, if we're playing a file on the playout side
601 // we take that frequency into consideration as well
602 // This is not needed on sending side, since the codec will
603 // limit the spectrum anyway.
henrika@webrtc.org944cbeb2014-03-18 10:32:33 +0000604 if (channel_state_.Get().output_file_playing)
niklase@google.com470e71d2011-07-07 08:21:25 +0000605 {
mflodman@webrtc.org9a065d12012-03-07 08:12:21 +0000606 CriticalSectionScoped cs(&_fileCritSect);
henrika@webrtc.org944cbeb2014-03-18 10:32:33 +0000607 if (_outputFilePlayerPtr)
niklase@google.com470e71d2011-07-07 08:21:25 +0000608 {
609 if(_outputFilePlayerPtr->Frequency()>highestNeeded)
610 {
611 highestNeeded=_outputFilePlayerPtr->Frequency();
612 }
613 }
614 }
615
616 return(highestNeeded);
617}
618
pbos@webrtc.org6141e132013-04-09 10:09:10 +0000619int32_t
niklase@google.com470e71d2011-07-07 08:21:25 +0000620Channel::CreateChannel(Channel*& channel,
pbos@webrtc.org92135212013-05-14 08:31:39 +0000621 int32_t channelId,
minyue@webrtc.orge509f942013-09-12 17:03:00 +0000622 uint32_t instanceId,
623 const Config& config)
niklase@google.com470e71d2011-07-07 08:21:25 +0000624{
625 WEBRTC_TRACE(kTraceMemory, kTraceVoice, VoEId(instanceId,channelId),
626 "Channel::CreateChannel(channelId=%d, instanceId=%d)",
627 channelId, instanceId);
628
minyue@webrtc.orge509f942013-09-12 17:03:00 +0000629 channel = new Channel(channelId, instanceId, config);
niklase@google.com470e71d2011-07-07 08:21:25 +0000630 if (channel == NULL)
631 {
632 WEBRTC_TRACE(kTraceMemory, kTraceVoice,
633 VoEId(instanceId,channelId),
634 "Channel::CreateChannel() unable to allocate memory for"
635 " channel");
636 return -1;
637 }
638 return 0;
639}
640
641void
pbos@webrtc.org92135212013-05-14 08:31:39 +0000642Channel::PlayNotification(int32_t id, uint32_t durationMs)
niklase@google.com470e71d2011-07-07 08:21:25 +0000643{
644 WEBRTC_TRACE(kTraceStream, kTraceVoice, VoEId(_instanceId,_channelId),
645 "Channel::PlayNotification(id=%d, durationMs=%d)",
646 id, durationMs);
647
648 // Not implement yet
649}
650
651void
pbos@webrtc.org92135212013-05-14 08:31:39 +0000652Channel::RecordNotification(int32_t id, uint32_t durationMs)
niklase@google.com470e71d2011-07-07 08:21:25 +0000653{
654 WEBRTC_TRACE(kTraceStream, kTraceVoice, VoEId(_instanceId,_channelId),
655 "Channel::RecordNotification(id=%d, durationMs=%d)",
656 id, durationMs);
657
658 // Not implement yet
659}
660
661void
pbos@webrtc.org92135212013-05-14 08:31:39 +0000662Channel::PlayFileEnded(int32_t id)
niklase@google.com470e71d2011-07-07 08:21:25 +0000663{
664 WEBRTC_TRACE(kTraceStream, kTraceVoice, VoEId(_instanceId,_channelId),
665 "Channel::PlayFileEnded(id=%d)", id);
666
667 if (id == _inputFilePlayerId)
668 {
henrika@webrtc.org944cbeb2014-03-18 10:32:33 +0000669 channel_state_.SetInputFilePlaying(false);
niklase@google.com470e71d2011-07-07 08:21:25 +0000670 WEBRTC_TRACE(kTraceStateInfo, kTraceVoice,
671 VoEId(_instanceId,_channelId),
672 "Channel::PlayFileEnded() => input file player module is"
673 " shutdown");
674 }
675 else if (id == _outputFilePlayerId)
676 {
henrika@webrtc.org944cbeb2014-03-18 10:32:33 +0000677 channel_state_.SetOutputFilePlaying(false);
niklase@google.com470e71d2011-07-07 08:21:25 +0000678 WEBRTC_TRACE(kTraceStateInfo, kTraceVoice,
679 VoEId(_instanceId,_channelId),
680 "Channel::PlayFileEnded() => output file player module is"
681 " shutdown");
682 }
683}
684
685void
pbos@webrtc.org92135212013-05-14 08:31:39 +0000686Channel::RecordFileEnded(int32_t id)
niklase@google.com470e71d2011-07-07 08:21:25 +0000687{
688 WEBRTC_TRACE(kTraceStream, kTraceVoice, VoEId(_instanceId,_channelId),
689 "Channel::RecordFileEnded(id=%d)", id);
690
691 assert(id == _outputFileRecorderId);
692
mflodman@webrtc.org9a065d12012-03-07 08:12:21 +0000693 CriticalSectionScoped cs(&_fileCritSect);
niklase@google.com470e71d2011-07-07 08:21:25 +0000694
695 _outputFileRecording = false;
696 WEBRTC_TRACE(kTraceStateInfo, kTraceVoice,
697 VoEId(_instanceId,_channelId),
698 "Channel::RecordFileEnded() => output file recorder module is"
699 " shutdown");
700}
701
pbos@webrtc.org92135212013-05-14 08:31:39 +0000702Channel::Channel(int32_t channelId,
minyue@webrtc.orge509f942013-09-12 17:03:00 +0000703 uint32_t instanceId,
704 const Config& config) :
niklase@google.com470e71d2011-07-07 08:21:25 +0000705 _fileCritSect(*CriticalSectionWrapper::CreateCriticalSection()),
706 _callbackCritSect(*CriticalSectionWrapper::CreateCriticalSection()),
wu@webrtc.org63420662013-10-17 18:28:55 +0000707 volume_settings_critsect_(*CriticalSectionWrapper::CreateCriticalSection()),
niklase@google.com470e71d2011-07-07 08:21:25 +0000708 _instanceId(instanceId),
xians@google.com22963ab2011-08-03 12:40:23 +0000709 _channelId(channelId),
stefan@webrtc.orga5cb98c2013-05-29 12:12:51 +0000710 rtp_header_parser_(RtpHeaderParser::Create()),
wu@webrtc.org822fbd82013-08-15 23:38:54 +0000711 rtp_payload_registry_(
andresp@webrtc.orgdc80bae2014-04-08 11:06:12 +0000712 new RTPPayloadRegistry(RTPPayloadStrategy::CreateStrategy(true))),
wu@webrtc.org822fbd82013-08-15 23:38:54 +0000713 rtp_receive_statistics_(ReceiveStatistics::Create(
714 Clock::GetRealTimeClock())),
715 rtp_receiver_(RtpReceiver::CreateAudioReceiver(
716 VoEModuleId(instanceId, channelId), Clock::GetRealTimeClock(), this,
717 this, this, rtp_payload_registry_.get())),
718 telephone_event_handler_(rtp_receiver_->GetTelephoneEventHandler()),
henrik.lundin@webrtc.org34fe0152014-04-22 19:04:34 +0000719 audio_coding_(AudioCodingModule::Create(
xians@google.com22963ab2011-08-03 12:40:23 +0000720 VoEModuleId(instanceId, channelId))),
niklase@google.com470e71d2011-07-07 08:21:25 +0000721 _rtpDumpIn(*RtpDump::CreateRtpDump()),
722 _rtpDumpOut(*RtpDump::CreateRtpDump()),
niklase@google.com470e71d2011-07-07 08:21:25 +0000723 _outputAudioLevel(),
niklase@google.com470e71d2011-07-07 08:21:25 +0000724 _externalTransport(false),
niklase@google.com470e71d2011-07-07 08:21:25 +0000725 _inputFilePlayerPtr(NULL),
726 _outputFilePlayerPtr(NULL),
727 _outputFileRecorderPtr(NULL),
728 // Avoid conflict with other channels by adding 1024 - 1026,
729 // won't use as much as 1024 channels.
730 _inputFilePlayerId(VoEModuleId(instanceId, channelId) + 1024),
731 _outputFilePlayerId(VoEModuleId(instanceId, channelId) + 1025),
732 _outputFileRecorderId(VoEModuleId(instanceId, channelId) + 1026),
niklase@google.com470e71d2011-07-07 08:21:25 +0000733 _outputFileRecording(false),
xians@google.com22963ab2011-08-03 12:40:23 +0000734 _inbandDtmfQueue(VoEModuleId(instanceId, channelId)),
735 _inbandDtmfGenerator(VoEModuleId(instanceId, channelId)),
xians@google.com22963ab2011-08-03 12:40:23 +0000736 _outputExternalMedia(false),
niklase@google.com470e71d2011-07-07 08:21:25 +0000737 _inputExternalMediaCallbackPtr(NULL),
738 _outputExternalMediaCallbackPtr(NULL),
xians@google.com22963ab2011-08-03 12:40:23 +0000739 _timeStamp(0), // This is just an offset, RTP module will add it's own random offset
740 _sendTelephoneEventPayloadType(106),
stefan@webrtc.org8e24d872014-09-02 18:58:24 +0000741 ntp_estimator_(Clock::GetRealTimeClock()),
turaj@webrtc.org167b6df2013-12-13 21:05:07 +0000742 jitter_buffer_playout_timestamp_(0),
pwestin@webrtc.org1de01352013-04-11 20:23:35 +0000743 playout_timestamp_rtp_(0),
744 playout_timestamp_rtcp_(0),
sprang@webrtc.org54ae4ff2013-12-19 13:26:02 +0000745 playout_delay_ms_(0),
xians@google.com22963ab2011-08-03 12:40:23 +0000746 _numberOfDiscardedPackets(0),
xians@webrtc.org09e8c472013-07-31 16:30:19 +0000747 send_sequence_number_(0),
wu@webrtc.orgcb711f72014-05-19 17:39:11 +0000748 ts_stats_lock_(CriticalSectionWrapper::CreateCriticalSection()),
wu@webrtc.org94454b72014-06-05 20:34:08 +0000749 rtp_ts_wraparound_handler_(new rtc::TimestampWrapAroundHandler()),
750 capture_start_rtp_time_stamp_(-1),
wu@webrtc.orgcb711f72014-05-19 17:39:11 +0000751 capture_start_ntp_time_ms_(-1),
xians@google.com22963ab2011-08-03 12:40:23 +0000752 _engineStatisticsPtr(NULL),
henrika@webrtc.org2919e952012-01-31 08:45:03 +0000753 _outputMixerPtr(NULL),
754 _transmitMixerPtr(NULL),
xians@google.com22963ab2011-08-03 12:40:23 +0000755 _moduleProcessThreadPtr(NULL),
756 _audioDeviceModulePtr(NULL),
757 _voiceEngineObserverPtr(NULL),
758 _callbackCritSectPtr(NULL),
759 _transportPtr(NULL),
xians@google.com22963ab2011-08-03 12:40:23 +0000760 _rxVadObserverPtr(NULL),
761 _oldVadDecision(-1),
762 _sendFrameType(0),
roosa@google.com1b60ceb2012-12-12 23:00:29 +0000763 _externalMixing(false),
xians@google.com22963ab2011-08-03 12:40:23 +0000764 _mixFileWithMicrophone(false),
niklase@google.com470e71d2011-07-07 08:21:25 +0000765 _mute(false),
766 _panLeft(1.0f),
767 _panRight(1.0f),
768 _outputGain(1.0f),
769 _playOutbandDtmfEvent(false),
770 _playInbandDtmfEvent(false),
niklase@google.com470e71d2011-07-07 08:21:25 +0000771 _lastLocalTimeStamp(0),
772 _lastPayloadType(0),
xians@google.com22963ab2011-08-03 12:40:23 +0000773 _includeAudioLevelIndication(false),
niklase@google.com470e71d2011-07-07 08:21:25 +0000774 _outputSpeechType(AudioFrame::kNormalSpeech),
solenberg@webrtc.orgb1f50102014-03-24 10:38:25 +0000775 vie_network_(NULL),
776 video_channel_(-1),
pwestin@webrtc.org1de01352013-04-11 20:23:35 +0000777 _average_jitter_buffer_delay_us(0),
turaj@webrtc.orge46c8d32013-05-22 20:39:43 +0000778 least_required_delay_ms_(0),
niklase@google.com470e71d2011-07-07 08:21:25 +0000779 _previousTimestamp(0),
780 _recPacketDelayMs(20),
781 _RxVadDetection(false),
niklase@google.com470e71d2011-07-07 08:21:25 +0000782 _rxAgcIsEnabled(false),
stefan@webrtc.org7bb8f022013-09-06 13:40:11 +0000783 _rxNsIsEnabled(false),
minyue@webrtc.orgc1a40a72014-05-28 09:52:06 +0000784 restored_packet_in_use_(false),
785 bitrate_controller_(
786 BitrateController::CreateBitrateController(Clock::GetRealTimeClock(),
787 true)),
788 rtcp_bandwidth_observer_(
789 bitrate_controller_->CreateRtcpBandwidthObserver()),
minyue@webrtc.org74aaf292014-07-16 21:28:26 +0000790 send_bitrate_observer_(new VoEBitrateObserver(this)),
791 network_predictor_(new NetworkPredictor(Clock::GetRealTimeClock()))
niklase@google.com470e71d2011-07-07 08:21:25 +0000792{
793 WEBRTC_TRACE(kTraceMemory, kTraceVoice, VoEId(_instanceId,_channelId),
794 "Channel::Channel() - ctor");
795 _inbandDtmfQueue.ResetDtmf();
796 _inbandDtmfGenerator.Init();
797 _outputAudioLevel.Clear();
798
pwestin@webrtc.org2853dde2012-05-11 11:08:54 +0000799 RtpRtcp::Configuration configuration;
800 configuration.id = VoEModuleId(instanceId, channelId);
801 configuration.audio = true;
pwestin@webrtc.org2853dde2012-05-11 11:08:54 +0000802 configuration.outgoing_transport = this;
pwestin@webrtc.org2853dde2012-05-11 11:08:54 +0000803 configuration.audio_messages = this;
wu@webrtc.org822fbd82013-08-15 23:38:54 +0000804 configuration.receive_statistics = rtp_receive_statistics_.get();
minyue@webrtc.orgc1a40a72014-05-28 09:52:06 +0000805 configuration.bandwidth_callback = rtcp_bandwidth_observer_.get();
pwestin@webrtc.org2853dde2012-05-11 11:08:54 +0000806
807 _rtpRtcpModule.reset(RtpRtcp::CreateRtpRtcp(configuration));
sprang@webrtc.org54ae4ff2013-12-19 13:26:02 +0000808
809 statistics_proxy_.reset(new StatisticsProxy(_rtpRtcpModule->SSRC()));
810 rtp_receive_statistics_->RegisterRtcpStatisticsCallback(
811 statistics_proxy_.get());
aluebs@webrtc.orgf927fd62014-04-16 11:58:18 +0000812
813 Config audioproc_config;
814 audioproc_config.Set<ExperimentalAgc>(new ExperimentalAgc(false));
815 rx_audioproc_.reset(AudioProcessing::Create(audioproc_config));
niklase@google.com470e71d2011-07-07 08:21:25 +0000816}
817
818Channel::~Channel()
819{
sprang@webrtc.org54ae4ff2013-12-19 13:26:02 +0000820 rtp_receive_statistics_->RegisterRtcpStatisticsCallback(NULL);
niklase@google.com470e71d2011-07-07 08:21:25 +0000821 WEBRTC_TRACE(kTraceMemory, kTraceVoice, VoEId(_instanceId,_channelId),
822 "Channel::~Channel() - dtor");
823
824 if (_outputExternalMedia)
825 {
826 DeRegisterExternalMediaProcessing(kPlaybackPerChannel);
827 }
henrika@webrtc.org944cbeb2014-03-18 10:32:33 +0000828 if (channel_state_.Get().input_external_media)
niklase@google.com470e71d2011-07-07 08:21:25 +0000829 {
830 DeRegisterExternalMediaProcessing(kRecordingPerChannel);
831 }
832 StopSend();
niklase@google.com470e71d2011-07-07 08:21:25 +0000833 StopPlayout();
834
835 {
mflodman@webrtc.org9a065d12012-03-07 08:12:21 +0000836 CriticalSectionScoped cs(&_fileCritSect);
niklase@google.com470e71d2011-07-07 08:21:25 +0000837 if (_inputFilePlayerPtr)
838 {
839 _inputFilePlayerPtr->RegisterModuleFileCallback(NULL);
840 _inputFilePlayerPtr->StopPlayingFile();
841 FilePlayer::DestroyFilePlayer(_inputFilePlayerPtr);
842 _inputFilePlayerPtr = NULL;
843 }
844 if (_outputFilePlayerPtr)
845 {
846 _outputFilePlayerPtr->RegisterModuleFileCallback(NULL);
847 _outputFilePlayerPtr->StopPlayingFile();
848 FilePlayer::DestroyFilePlayer(_outputFilePlayerPtr);
849 _outputFilePlayerPtr = NULL;
850 }
851 if (_outputFileRecorderPtr)
852 {
853 _outputFileRecorderPtr->RegisterModuleFileCallback(NULL);
854 _outputFileRecorderPtr->StopRecording();
855 FileRecorder::DestroyFileRecorder(_outputFileRecorderPtr);
856 _outputFileRecorderPtr = NULL;
857 }
858 }
859
860 // The order to safely shutdown modules in a channel is:
861 // 1. De-register callbacks in modules
862 // 2. De-register modules in process thread
863 // 3. Destroy modules
andrew@webrtc.orgeb524d92013-09-23 23:02:24 +0000864 if (audio_coding_->RegisterTransportCallback(NULL) == -1)
niklase@google.com470e71d2011-07-07 08:21:25 +0000865 {
866 WEBRTC_TRACE(kTraceWarning, kTraceVoice,
867 VoEId(_instanceId,_channelId),
868 "~Channel() failed to de-register transport callback"
869 " (Audio coding module)");
870 }
andrew@webrtc.orgeb524d92013-09-23 23:02:24 +0000871 if (audio_coding_->RegisterVADCallback(NULL) == -1)
niklase@google.com470e71d2011-07-07 08:21:25 +0000872 {
873 WEBRTC_TRACE(kTraceWarning, kTraceVoice,
874 VoEId(_instanceId,_channelId),
875 "~Channel() failed to de-register VAD callback"
876 " (Audio coding module)");
877 }
niklase@google.com470e71d2011-07-07 08:21:25 +0000878 // De-register modules in process thread
pwestin@webrtc.org2853dde2012-05-11 11:08:54 +0000879 if (_moduleProcessThreadPtr->DeRegisterModule(_rtpRtcpModule.get()) == -1)
niklase@google.com470e71d2011-07-07 08:21:25 +0000880 {
881 WEBRTC_TRACE(kTraceInfo, kTraceVoice,
882 VoEId(_instanceId,_channelId),
883 "~Channel() failed to deregister RTP/RTCP module");
884 }
niklase@google.com470e71d2011-07-07 08:21:25 +0000885 // End of modules shutdown
886
887 // Delete other objects
solenberg@webrtc.orgb1f50102014-03-24 10:38:25 +0000888 if (vie_network_) {
889 vie_network_->Release();
890 vie_network_ = NULL;
891 }
niklase@google.com470e71d2011-07-07 08:21:25 +0000892 RtpDump::DestroyRtpDump(&_rtpDumpIn);
893 RtpDump::DestroyRtpDump(&_rtpDumpOut);
niklase@google.com470e71d2011-07-07 08:21:25 +0000894 delete &_callbackCritSect;
niklase@google.com470e71d2011-07-07 08:21:25 +0000895 delete &_fileCritSect;
wu@webrtc.org63420662013-10-17 18:28:55 +0000896 delete &volume_settings_critsect_;
niklase@google.com470e71d2011-07-07 08:21:25 +0000897}
898
pbos@webrtc.org6141e132013-04-09 10:09:10 +0000899int32_t
niklase@google.com470e71d2011-07-07 08:21:25 +0000900Channel::Init()
901{
902 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId,_channelId),
903 "Channel::Init()");
904
henrika@webrtc.org944cbeb2014-03-18 10:32:33 +0000905 channel_state_.Reset();
906
niklase@google.com470e71d2011-07-07 08:21:25 +0000907 // --- Initial sanity
908
909 if ((_engineStatisticsPtr == NULL) ||
910 (_moduleProcessThreadPtr == NULL))
911 {
912 WEBRTC_TRACE(kTraceError, kTraceVoice,
913 VoEId(_instanceId,_channelId),
914 "Channel::Init() must call SetEngineInformation() first");
915 return -1;
916 }
917
918 // --- Add modules to process thread (for periodic schedulation)
919
920 const bool processThreadFail =
pwestin@webrtc.org2853dde2012-05-11 11:08:54 +0000921 ((_moduleProcessThreadPtr->RegisterModule(_rtpRtcpModule.get()) != 0) ||
niklase@google.com470e71d2011-07-07 08:21:25 +0000922 false);
niklase@google.com470e71d2011-07-07 08:21:25 +0000923 if (processThreadFail)
924 {
925 _engineStatisticsPtr->SetLastError(
926 VE_CANNOT_INIT_CHANNEL, kTraceError,
927 "Channel::Init() modules not registered");
928 return -1;
929 }
pwestin@webrtc.orgc450a192012-01-04 15:00:12 +0000930 // --- ACM initialization
niklase@google.com470e71d2011-07-07 08:21:25 +0000931
andrew@webrtc.orgeb524d92013-09-23 23:02:24 +0000932 if ((audio_coding_->InitializeReceiver() == -1) ||
niklase@google.com470e71d2011-07-07 08:21:25 +0000933#ifdef WEBRTC_CODEC_AVT
934 // out-of-band Dtmf tones are played out by default
andrew@webrtc.orgeb524d92013-09-23 23:02:24 +0000935 (audio_coding_->SetDtmfPlayoutStatus(true) == -1) ||
niklase@google.com470e71d2011-07-07 08:21:25 +0000936#endif
andrew@webrtc.orgeb524d92013-09-23 23:02:24 +0000937 (audio_coding_->InitializeSender() == -1))
niklase@google.com470e71d2011-07-07 08:21:25 +0000938 {
939 _engineStatisticsPtr->SetLastError(
940 VE_AUDIO_CODING_MODULE_ERROR, kTraceError,
941 "Channel::Init() unable to initialize the ACM - 1");
942 return -1;
943 }
944
945 // --- RTP/RTCP module initialization
946
947 // Ensure that RTCP is enabled by default for the created channel.
948 // Note that, the module will keep generating RTCP until it is explicitly
949 // disabled by the user.
950 // After StopListen (when no sockets exists), RTCP packets will no longer
951 // be transmitted since the Transport object will then be invalid.
wu@webrtc.org822fbd82013-08-15 23:38:54 +0000952 telephone_event_handler_->SetTelephoneEventForwardToDecoder(true);
953 // RTCP is enabled by default.
954 if (_rtpRtcpModule->SetRTCPStatus(kRtcpCompound) == -1)
niklase@google.com470e71d2011-07-07 08:21:25 +0000955 {
956 _engineStatisticsPtr->SetLastError(
957 VE_RTP_RTCP_MODULE_ERROR, kTraceError,
958 "Channel::Init() RTP/RTCP module not initialized");
959 return -1;
960 }
961
962 // --- Register all permanent callbacks
niklase@google.com470e71d2011-07-07 08:21:25 +0000963 const bool fail =
andrew@webrtc.orgeb524d92013-09-23 23:02:24 +0000964 (audio_coding_->RegisterTransportCallback(this) == -1) ||
965 (audio_coding_->RegisterVADCallback(this) == -1);
niklase@google.com470e71d2011-07-07 08:21:25 +0000966
967 if (fail)
968 {
969 _engineStatisticsPtr->SetLastError(
970 VE_CANNOT_INIT_CHANNEL, kTraceError,
971 "Channel::Init() callbacks not registered");
972 return -1;
973 }
974
975 // --- Register all supported codecs to the receiving side of the
976 // RTP/RTCP module
977
978 CodecInst codec;
pbos@webrtc.org6141e132013-04-09 10:09:10 +0000979 const uint8_t nSupportedCodecs = AudioCodingModule::NumberOfCodecs();
niklase@google.com470e71d2011-07-07 08:21:25 +0000980
981 for (int idx = 0; idx < nSupportedCodecs; idx++)
982 {
983 // Open up the RTP/RTCP receiver for all supported codecs
andrew@webrtc.orgeb524d92013-09-23 23:02:24 +0000984 if ((audio_coding_->Codec(idx, &codec) == -1) ||
wu@webrtc.org822fbd82013-08-15 23:38:54 +0000985 (rtp_receiver_->RegisterReceivePayload(
986 codec.plname,
987 codec.pltype,
988 codec.plfreq,
989 codec.channels,
990 (codec.rate < 0) ? 0 : codec.rate) == -1))
niklase@google.com470e71d2011-07-07 08:21:25 +0000991 {
992 WEBRTC_TRACE(kTraceWarning, kTraceVoice,
993 VoEId(_instanceId,_channelId),
994 "Channel::Init() unable to register %s (%d/%d/%d/%d) "
995 "to RTP/RTCP receiver",
996 codec.plname, codec.pltype, codec.plfreq,
997 codec.channels, codec.rate);
998 }
999 else
1000 {
1001 WEBRTC_TRACE(kTraceInfo, kTraceVoice,
1002 VoEId(_instanceId,_channelId),
1003 "Channel::Init() %s (%d/%d/%d/%d) has been added to "
1004 "the RTP/RTCP receiver",
1005 codec.plname, codec.pltype, codec.plfreq,
1006 codec.channels, codec.rate);
1007 }
1008
1009 // Ensure that PCMU is used as default codec on the sending side
tina.legrand@webrtc.org45175852012-06-01 09:27:35 +00001010 if (!STR_CASE_CMP(codec.plname, "PCMU") && (codec.channels == 1))
niklase@google.com470e71d2011-07-07 08:21:25 +00001011 {
1012 SetSendCodec(codec);
1013 }
1014
1015 // Register default PT for outband 'telephone-event'
1016 if (!STR_CASE_CMP(codec.plname, "telephone-event"))
1017 {
pwestin@webrtc.org2853dde2012-05-11 11:08:54 +00001018 if ((_rtpRtcpModule->RegisterSendPayload(codec) == -1) ||
andrew@webrtc.orgeb524d92013-09-23 23:02:24 +00001019 (audio_coding_->RegisterReceiveCodec(codec) == -1))
niklase@google.com470e71d2011-07-07 08:21:25 +00001020 {
1021 WEBRTC_TRACE(kTraceWarning, kTraceVoice,
1022 VoEId(_instanceId,_channelId),
1023 "Channel::Init() failed to register outband "
1024 "'telephone-event' (%d/%d) correctly",
1025 codec.pltype, codec.plfreq);
1026 }
1027 }
1028
1029 if (!STR_CASE_CMP(codec.plname, "CN"))
1030 {
andrew@webrtc.orgeb524d92013-09-23 23:02:24 +00001031 if ((audio_coding_->RegisterSendCodec(codec) == -1) ||
1032 (audio_coding_->RegisterReceiveCodec(codec) == -1) ||
pwestin@webrtc.org2853dde2012-05-11 11:08:54 +00001033 (_rtpRtcpModule->RegisterSendPayload(codec) == -1))
niklase@google.com470e71d2011-07-07 08:21:25 +00001034 {
1035 WEBRTC_TRACE(kTraceWarning, kTraceVoice,
1036 VoEId(_instanceId,_channelId),
1037 "Channel::Init() failed to register CN (%d/%d) "
1038 "correctly - 1",
1039 codec.pltype, codec.plfreq);
1040 }
1041 }
1042#ifdef WEBRTC_CODEC_RED
1043 // Register RED to the receiving side of the ACM.
1044 // We will not receive an OnInitializeDecoder() callback for RED.
1045 if (!STR_CASE_CMP(codec.plname, "RED"))
1046 {
andrew@webrtc.orgeb524d92013-09-23 23:02:24 +00001047 if (audio_coding_->RegisterReceiveCodec(codec) == -1)
niklase@google.com470e71d2011-07-07 08:21:25 +00001048 {
1049 WEBRTC_TRACE(kTraceWarning, kTraceVoice,
1050 VoEId(_instanceId,_channelId),
1051 "Channel::Init() failed to register RED (%d/%d) "
1052 "correctly",
1053 codec.pltype, codec.plfreq);
1054 }
1055 }
1056#endif
1057 }
pwestin@webrtc.org684f0572013-03-13 23:20:57 +00001058
andrew@webrtc.org6c264cc2013-10-04 17:54:09 +00001059 if (rx_audioproc_->noise_suppression()->set_level(kDefaultNsMode) != 0) {
1060 LOG_FERR1(LS_ERROR, noise_suppression()->set_level, kDefaultNsMode);
1061 return -1;
niklase@google.com470e71d2011-07-07 08:21:25 +00001062 }
andrew@webrtc.org6c264cc2013-10-04 17:54:09 +00001063 if (rx_audioproc_->gain_control()->set_mode(kDefaultRxAgcMode) != 0) {
1064 LOG_FERR1(LS_ERROR, gain_control()->set_mode, kDefaultRxAgcMode);
1065 return -1;
niklase@google.com470e71d2011-07-07 08:21:25 +00001066 }
1067
1068 return 0;
1069}
1070
pbos@webrtc.org6141e132013-04-09 10:09:10 +00001071int32_t
niklase@google.com470e71d2011-07-07 08:21:25 +00001072Channel::SetEngineInformation(Statistics& engineStatistics,
1073 OutputMixer& outputMixer,
1074 voe::TransmitMixer& transmitMixer,
1075 ProcessThread& moduleProcessThread,
1076 AudioDeviceModule& audioDeviceModule,
1077 VoiceEngineObserver* voiceEngineObserver,
1078 CriticalSectionWrapper* callbackCritSect)
1079{
1080 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId,_channelId),
1081 "Channel::SetEngineInformation()");
1082 _engineStatisticsPtr = &engineStatistics;
1083 _outputMixerPtr = &outputMixer;
1084 _transmitMixerPtr = &transmitMixer,
1085 _moduleProcessThreadPtr = &moduleProcessThread;
1086 _audioDeviceModulePtr = &audioDeviceModule;
1087 _voiceEngineObserverPtr = voiceEngineObserver;
1088 _callbackCritSectPtr = callbackCritSect;
1089 return 0;
1090}
1091
pbos@webrtc.org6141e132013-04-09 10:09:10 +00001092int32_t
niklase@google.com470e71d2011-07-07 08:21:25 +00001093Channel::UpdateLocalTimeStamp()
1094{
1095
andrew@webrtc.org63a50982012-05-02 23:56:37 +00001096 _timeStamp += _audioFrame.samples_per_channel_;
niklase@google.com470e71d2011-07-07 08:21:25 +00001097 return 0;
1098}
1099
pbos@webrtc.org6141e132013-04-09 10:09:10 +00001100int32_t
niklase@google.com470e71d2011-07-07 08:21:25 +00001101Channel::StartPlayout()
1102{
1103 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId,_channelId),
1104 "Channel::StartPlayout()");
henrika@webrtc.org944cbeb2014-03-18 10:32:33 +00001105 if (channel_state_.Get().playing)
niklase@google.com470e71d2011-07-07 08:21:25 +00001106 {
1107 return 0;
1108 }
roosa@google.com1b60ceb2012-12-12 23:00:29 +00001109
1110 if (!_externalMixing) {
1111 // Add participant as candidates for mixing.
1112 if (_outputMixerPtr->SetMixabilityStatus(*this, true) != 0)
1113 {
1114 _engineStatisticsPtr->SetLastError(
1115 VE_AUDIO_CONF_MIX_MODULE_ERROR, kTraceError,
1116 "StartPlayout() failed to add participant to mixer");
1117 return -1;
1118 }
niklase@google.com470e71d2011-07-07 08:21:25 +00001119 }
1120
henrika@webrtc.org944cbeb2014-03-18 10:32:33 +00001121 channel_state_.SetPlaying(true);
braveyao@webrtc.orgab129902012-06-04 03:26:39 +00001122 if (RegisterFilePlayingToMixer() != 0)
1123 return -1;
1124
niklase@google.com470e71d2011-07-07 08:21:25 +00001125 return 0;
1126}
1127
pbos@webrtc.org6141e132013-04-09 10:09:10 +00001128int32_t
niklase@google.com470e71d2011-07-07 08:21:25 +00001129Channel::StopPlayout()
1130{
1131 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId,_channelId),
1132 "Channel::StopPlayout()");
henrika@webrtc.org944cbeb2014-03-18 10:32:33 +00001133 if (!channel_state_.Get().playing)
niklase@google.com470e71d2011-07-07 08:21:25 +00001134 {
1135 return 0;
1136 }
roosa@google.com1b60ceb2012-12-12 23:00:29 +00001137
1138 if (!_externalMixing) {
1139 // Remove participant as candidates for mixing
1140 if (_outputMixerPtr->SetMixabilityStatus(*this, false) != 0)
1141 {
1142 _engineStatisticsPtr->SetLastError(
1143 VE_AUDIO_CONF_MIX_MODULE_ERROR, kTraceError,
1144 "StopPlayout() failed to remove participant from mixer");
1145 return -1;
1146 }
niklase@google.com470e71d2011-07-07 08:21:25 +00001147 }
1148
henrika@webrtc.org944cbeb2014-03-18 10:32:33 +00001149 channel_state_.SetPlaying(false);
niklase@google.com470e71d2011-07-07 08:21:25 +00001150 _outputAudioLevel.Clear();
1151
1152 return 0;
1153}
1154
pbos@webrtc.org6141e132013-04-09 10:09:10 +00001155int32_t
niklase@google.com470e71d2011-07-07 08:21:25 +00001156Channel::StartSend()
1157{
1158 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId,_channelId),
1159 "Channel::StartSend()");
xians@webrtc.org09e8c472013-07-31 16:30:19 +00001160 // Resume the previous sequence number which was reset by StopSend().
henrika@webrtc.org944cbeb2014-03-18 10:32:33 +00001161 // This needs to be done before |sending| is set to true.
xians@webrtc.org09e8c472013-07-31 16:30:19 +00001162 if (send_sequence_number_)
1163 SetInitSequenceNumber(send_sequence_number_);
1164
henrika@webrtc.org944cbeb2014-03-18 10:32:33 +00001165 if (channel_state_.Get().sending)
niklase@google.com470e71d2011-07-07 08:21:25 +00001166 {
henrika@webrtc.org944cbeb2014-03-18 10:32:33 +00001167 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +00001168 }
henrika@webrtc.org944cbeb2014-03-18 10:32:33 +00001169 channel_state_.SetSending(true);
xians@webrtc.orge07247a2011-11-28 16:31:28 +00001170
pwestin@webrtc.org2853dde2012-05-11 11:08:54 +00001171 if (_rtpRtcpModule->SetSendingStatus(true) != 0)
niklase@google.com470e71d2011-07-07 08:21:25 +00001172 {
1173 _engineStatisticsPtr->SetLastError(
1174 VE_RTP_RTCP_MODULE_ERROR, kTraceError,
1175 "StartSend() RTP/RTCP failed to start sending");
mflodman@webrtc.org9a065d12012-03-07 08:12:21 +00001176 CriticalSectionScoped cs(&_callbackCritSect);
henrika@webrtc.org944cbeb2014-03-18 10:32:33 +00001177 channel_state_.SetSending(false);
niklase@google.com470e71d2011-07-07 08:21:25 +00001178 return -1;
1179 }
xians@webrtc.orge07247a2011-11-28 16:31:28 +00001180
niklase@google.com470e71d2011-07-07 08:21:25 +00001181 return 0;
1182}
1183
pbos@webrtc.org6141e132013-04-09 10:09:10 +00001184int32_t
niklase@google.com470e71d2011-07-07 08:21:25 +00001185Channel::StopSend()
1186{
1187 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId,_channelId),
1188 "Channel::StopSend()");
henrika@webrtc.org944cbeb2014-03-18 10:32:33 +00001189 if (!channel_state_.Get().sending)
niklase@google.com470e71d2011-07-07 08:21:25 +00001190 {
henrika@webrtc.org944cbeb2014-03-18 10:32:33 +00001191 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +00001192 }
henrika@webrtc.org944cbeb2014-03-18 10:32:33 +00001193 channel_state_.SetSending(false);
xians@webrtc.orge07247a2011-11-28 16:31:28 +00001194
xians@webrtc.org09e8c472013-07-31 16:30:19 +00001195 // Store the sequence number to be able to pick up the same sequence for
1196 // the next StartSend(). This is needed for restarting device, otherwise
1197 // it might cause libSRTP to complain about packets being replayed.
1198 // TODO(xians): Remove this workaround after RtpRtcpModule's refactoring
1199 // CL is landed. See issue
1200 // https://code.google.com/p/webrtc/issues/detail?id=2111 .
1201 send_sequence_number_ = _rtpRtcpModule->SequenceNumber();
1202
niklase@google.com470e71d2011-07-07 08:21:25 +00001203 // Reset sending SSRC and sequence number and triggers direct transmission
1204 // of RTCP BYE
pwestin@webrtc.org2853dde2012-05-11 11:08:54 +00001205 if (_rtpRtcpModule->SetSendingStatus(false) == -1 ||
1206 _rtpRtcpModule->ResetSendDataCountersRTP() == -1)
niklase@google.com470e71d2011-07-07 08:21:25 +00001207 {
1208 _engineStatisticsPtr->SetLastError(
1209 VE_RTP_RTCP_MODULE_ERROR, kTraceWarning,
1210 "StartSend() RTP/RTCP failed to stop sending");
1211 }
1212
niklase@google.com470e71d2011-07-07 08:21:25 +00001213 return 0;
1214}
1215
pbos@webrtc.org6141e132013-04-09 10:09:10 +00001216int32_t
niklase@google.com470e71d2011-07-07 08:21:25 +00001217Channel::StartReceiving()
1218{
1219 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId,_channelId),
1220 "Channel::StartReceiving()");
henrika@webrtc.org944cbeb2014-03-18 10:32:33 +00001221 if (channel_state_.Get().receiving)
niklase@google.com470e71d2011-07-07 08:21:25 +00001222 {
1223 return 0;
1224 }
henrika@webrtc.org944cbeb2014-03-18 10:32:33 +00001225 channel_state_.SetReceiving(true);
niklase@google.com470e71d2011-07-07 08:21:25 +00001226 _numberOfDiscardedPackets = 0;
1227 return 0;
1228}
1229
pbos@webrtc.org6141e132013-04-09 10:09:10 +00001230int32_t
niklase@google.com470e71d2011-07-07 08:21:25 +00001231Channel::StopReceiving()
1232{
1233 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId,_channelId),
1234 "Channel::StopReceiving()");
henrika@webrtc.org944cbeb2014-03-18 10:32:33 +00001235 if (!channel_state_.Get().receiving)
niklase@google.com470e71d2011-07-07 08:21:25 +00001236 {
1237 return 0;
1238 }
pwestin@webrtc.org684f0572013-03-13 23:20:57 +00001239
henrika@webrtc.org944cbeb2014-03-18 10:32:33 +00001240 channel_state_.SetReceiving(false);
niklase@google.com470e71d2011-07-07 08:21:25 +00001241 return 0;
1242}
1243
pbos@webrtc.org6141e132013-04-09 10:09:10 +00001244int32_t
niklase@google.com470e71d2011-07-07 08:21:25 +00001245Channel::RegisterVoiceEngineObserver(VoiceEngineObserver& observer)
1246{
1247 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId,_channelId),
1248 "Channel::RegisterVoiceEngineObserver()");
mflodman@webrtc.org9a065d12012-03-07 08:12:21 +00001249 CriticalSectionScoped cs(&_callbackCritSect);
niklase@google.com470e71d2011-07-07 08:21:25 +00001250
1251 if (_voiceEngineObserverPtr)
1252 {
1253 _engineStatisticsPtr->SetLastError(
1254 VE_INVALID_OPERATION, kTraceError,
1255 "RegisterVoiceEngineObserver() observer already enabled");
1256 return -1;
1257 }
1258 _voiceEngineObserverPtr = &observer;
1259 return 0;
1260}
1261
pbos@webrtc.org6141e132013-04-09 10:09:10 +00001262int32_t
niklase@google.com470e71d2011-07-07 08:21:25 +00001263Channel::DeRegisterVoiceEngineObserver()
1264{
1265 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId,_channelId),
1266 "Channel::DeRegisterVoiceEngineObserver()");
mflodman@webrtc.org9a065d12012-03-07 08:12:21 +00001267 CriticalSectionScoped cs(&_callbackCritSect);
niklase@google.com470e71d2011-07-07 08:21:25 +00001268
1269 if (!_voiceEngineObserverPtr)
1270 {
1271 _engineStatisticsPtr->SetLastError(
1272 VE_INVALID_OPERATION, kTraceWarning,
1273 "DeRegisterVoiceEngineObserver() observer already disabled");
1274 return 0;
1275 }
1276 _voiceEngineObserverPtr = NULL;
1277 return 0;
1278}
1279
pbos@webrtc.org6141e132013-04-09 10:09:10 +00001280int32_t
niklase@google.com470e71d2011-07-07 08:21:25 +00001281Channel::GetSendCodec(CodecInst& codec)
1282{
andrew@webrtc.orgeb524d92013-09-23 23:02:24 +00001283 return (audio_coding_->SendCodec(&codec));
niklase@google.com470e71d2011-07-07 08:21:25 +00001284}
1285
pbos@webrtc.org6141e132013-04-09 10:09:10 +00001286int32_t
niklase@google.com470e71d2011-07-07 08:21:25 +00001287Channel::GetRecCodec(CodecInst& codec)
1288{
andrew@webrtc.orgeb524d92013-09-23 23:02:24 +00001289 return (audio_coding_->ReceiveCodec(&codec));
niklase@google.com470e71d2011-07-07 08:21:25 +00001290}
1291
pbos@webrtc.org6141e132013-04-09 10:09:10 +00001292int32_t
niklase@google.com470e71d2011-07-07 08:21:25 +00001293Channel::SetSendCodec(const CodecInst& codec)
1294{
1295 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId,_channelId),
1296 "Channel::SetSendCodec()");
1297
andrew@webrtc.orgeb524d92013-09-23 23:02:24 +00001298 if (audio_coding_->RegisterSendCodec(codec) != 0)
niklase@google.com470e71d2011-07-07 08:21:25 +00001299 {
1300 WEBRTC_TRACE(kTraceError, kTraceVoice, VoEId(_instanceId,_channelId),
1301 "SetSendCodec() failed to register codec to ACM");
1302 return -1;
1303 }
1304
pwestin@webrtc.org2853dde2012-05-11 11:08:54 +00001305 if (_rtpRtcpModule->RegisterSendPayload(codec) != 0)
niklase@google.com470e71d2011-07-07 08:21:25 +00001306 {
pwestin@webrtc.org2853dde2012-05-11 11:08:54 +00001307 _rtpRtcpModule->DeRegisterSendPayload(codec.pltype);
1308 if (_rtpRtcpModule->RegisterSendPayload(codec) != 0)
niklase@google.com470e71d2011-07-07 08:21:25 +00001309 {
1310 WEBRTC_TRACE(
1311 kTraceError, kTraceVoice, VoEId(_instanceId,_channelId),
1312 "SetSendCodec() failed to register codec to"
1313 " RTP/RTCP module");
1314 return -1;
1315 }
1316 }
1317
pwestin@webrtc.org2853dde2012-05-11 11:08:54 +00001318 if (_rtpRtcpModule->SetAudioPacketSize(codec.pacsize) != 0)
niklase@google.com470e71d2011-07-07 08:21:25 +00001319 {
1320 WEBRTC_TRACE(kTraceError, kTraceVoice, VoEId(_instanceId,_channelId),
1321 "SetSendCodec() failed to set audio packet size");
1322 return -1;
1323 }
1324
minyue@webrtc.orgc1a40a72014-05-28 09:52:06 +00001325 bitrate_controller_->SetBitrateObserver(send_bitrate_observer_.get(),
1326 codec.rate, 0, 0);
1327
niklase@google.com470e71d2011-07-07 08:21:25 +00001328 return 0;
1329}
1330
minyue@webrtc.orgc1a40a72014-05-28 09:52:06 +00001331void
1332Channel::OnNetworkChanged(const uint32_t bitrate_bps,
1333 const uint8_t fraction_lost, // 0 - 255.
1334 const uint32_t rtt) {
1335 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId,_channelId),
1336 "Channel::OnNetworkChanged(bitrate_bps=%d, fration_lost=%d, rtt=%d)",
1337 bitrate_bps, fraction_lost, rtt);
minyue@webrtc.org74aaf292014-07-16 21:28:26 +00001338 // |fraction_lost| from BitrateObserver is short time observation of packet
1339 // loss rate from past. We use network predictor to make a more reasonable
1340 // loss rate estimation.
1341 network_predictor_->UpdatePacketLossRate(fraction_lost);
1342 uint8_t loss_rate = network_predictor_->GetLossRate();
minyue@webrtc.orgc1a40a72014-05-28 09:52:06 +00001343 // Normalizes rate to 0 - 100.
minyue@webrtc.org74aaf292014-07-16 21:28:26 +00001344 if (audio_coding_->SetPacketLossRate(100 * loss_rate / 255) != 0) {
minyue@webrtc.orgc1a40a72014-05-28 09:52:06 +00001345 _engineStatisticsPtr->SetLastError(VE_AUDIO_CODING_MODULE_ERROR,
1346 kTraceError, "OnNetworkChanged() failed to set packet loss rate");
1347 assert(false); // This should not happen.
1348 }
1349}
1350
pbos@webrtc.org6141e132013-04-09 10:09:10 +00001351int32_t
niklase@google.com470e71d2011-07-07 08:21:25 +00001352Channel::SetVADStatus(bool enableVAD, ACMVADMode mode, bool disableDTX)
1353{
1354 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId,_channelId),
1355 "Channel::SetVADStatus(mode=%d)", mode);
1356 // To disable VAD, DTX must be disabled too
1357 disableDTX = ((enableVAD == false) ? true : disableDTX);
andrew@webrtc.orgeb524d92013-09-23 23:02:24 +00001358 if (audio_coding_->SetVAD(!disableDTX, enableVAD, mode) != 0)
niklase@google.com470e71d2011-07-07 08:21:25 +00001359 {
1360 _engineStatisticsPtr->SetLastError(
1361 VE_AUDIO_CODING_MODULE_ERROR, kTraceError,
1362 "SetVADStatus() failed to set VAD");
1363 return -1;
1364 }
1365 return 0;
1366}
1367
pbos@webrtc.org6141e132013-04-09 10:09:10 +00001368int32_t
niklase@google.com470e71d2011-07-07 08:21:25 +00001369Channel::GetVADStatus(bool& enabledVAD, ACMVADMode& mode, bool& disabledDTX)
1370{
1371 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId,_channelId),
1372 "Channel::GetVADStatus");
andrew@webrtc.orgeb524d92013-09-23 23:02:24 +00001373 if (audio_coding_->VAD(&disabledDTX, &enabledVAD, &mode) != 0)
niklase@google.com470e71d2011-07-07 08:21:25 +00001374 {
1375 _engineStatisticsPtr->SetLastError(
1376 VE_AUDIO_CODING_MODULE_ERROR, kTraceError,
1377 "GetVADStatus() failed to get VAD status");
1378 return -1;
1379 }
1380 disabledDTX = !disabledDTX;
1381 return 0;
1382}
1383
pbos@webrtc.org6141e132013-04-09 10:09:10 +00001384int32_t
niklase@google.com470e71d2011-07-07 08:21:25 +00001385Channel::SetRecPayloadType(const CodecInst& codec)
1386{
1387 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId,_channelId),
1388 "Channel::SetRecPayloadType()");
1389
henrika@webrtc.org944cbeb2014-03-18 10:32:33 +00001390 if (channel_state_.Get().playing)
niklase@google.com470e71d2011-07-07 08:21:25 +00001391 {
1392 _engineStatisticsPtr->SetLastError(
1393 VE_ALREADY_PLAYING, kTraceError,
1394 "SetRecPayloadType() unable to set PT while playing");
1395 return -1;
1396 }
henrika@webrtc.org944cbeb2014-03-18 10:32:33 +00001397 if (channel_state_.Get().receiving)
niklase@google.com470e71d2011-07-07 08:21:25 +00001398 {
1399 _engineStatisticsPtr->SetLastError(
1400 VE_ALREADY_LISTENING, kTraceError,
1401 "SetRecPayloadType() unable to set PT while listening");
1402 return -1;
1403 }
1404
1405 if (codec.pltype == -1)
1406 {
1407 // De-register the selected codec (RTP/RTCP module and ACM)
1408
pbos@webrtc.org6141e132013-04-09 10:09:10 +00001409 int8_t pltype(-1);
niklase@google.com470e71d2011-07-07 08:21:25 +00001410 CodecInst rxCodec = codec;
1411
1412 // Get payload type for the given codec
wu@webrtc.org822fbd82013-08-15 23:38:54 +00001413 rtp_payload_registry_->ReceivePayloadType(
1414 rxCodec.plname,
1415 rxCodec.plfreq,
1416 rxCodec.channels,
1417 (rxCodec.rate < 0) ? 0 : rxCodec.rate,
1418 &pltype);
niklase@google.com470e71d2011-07-07 08:21:25 +00001419 rxCodec.pltype = pltype;
1420
wu@webrtc.org822fbd82013-08-15 23:38:54 +00001421 if (rtp_receiver_->DeRegisterReceivePayload(pltype) != 0)
niklase@google.com470e71d2011-07-07 08:21:25 +00001422 {
1423 _engineStatisticsPtr->SetLastError(
1424 VE_RTP_RTCP_MODULE_ERROR,
1425 kTraceError,
1426 "SetRecPayloadType() RTP/RTCP-module deregistration "
1427 "failed");
1428 return -1;
1429 }
andrew@webrtc.orgeb524d92013-09-23 23:02:24 +00001430 if (audio_coding_->UnregisterReceiveCodec(rxCodec.pltype) != 0)
niklase@google.com470e71d2011-07-07 08:21:25 +00001431 {
1432 _engineStatisticsPtr->SetLastError(
1433 VE_AUDIO_CODING_MODULE_ERROR, kTraceError,
1434 "SetRecPayloadType() ACM deregistration failed - 1");
1435 return -1;
1436 }
1437 return 0;
1438 }
1439
wu@webrtc.org822fbd82013-08-15 23:38:54 +00001440 if (rtp_receiver_->RegisterReceivePayload(
1441 codec.plname,
1442 codec.pltype,
1443 codec.plfreq,
1444 codec.channels,
1445 (codec.rate < 0) ? 0 : codec.rate) != 0)
niklase@google.com470e71d2011-07-07 08:21:25 +00001446 {
1447 // First attempt to register failed => de-register and try again
wu@webrtc.org822fbd82013-08-15 23:38:54 +00001448 rtp_receiver_->DeRegisterReceivePayload(codec.pltype);
1449 if (rtp_receiver_->RegisterReceivePayload(
1450 codec.plname,
1451 codec.pltype,
1452 codec.plfreq,
1453 codec.channels,
1454 (codec.rate < 0) ? 0 : codec.rate) != 0)
niklase@google.com470e71d2011-07-07 08:21:25 +00001455 {
1456 _engineStatisticsPtr->SetLastError(
1457 VE_RTP_RTCP_MODULE_ERROR, kTraceError,
1458 "SetRecPayloadType() RTP/RTCP-module registration failed");
1459 return -1;
1460 }
1461 }
andrew@webrtc.orgeb524d92013-09-23 23:02:24 +00001462 if (audio_coding_->RegisterReceiveCodec(codec) != 0)
niklase@google.com470e71d2011-07-07 08:21:25 +00001463 {
andrew@webrtc.orgeb524d92013-09-23 23:02:24 +00001464 audio_coding_->UnregisterReceiveCodec(codec.pltype);
1465 if (audio_coding_->RegisterReceiveCodec(codec) != 0)
niklase@google.com470e71d2011-07-07 08:21:25 +00001466 {
1467 _engineStatisticsPtr->SetLastError(
1468 VE_AUDIO_CODING_MODULE_ERROR, kTraceError,
1469 "SetRecPayloadType() ACM registration failed - 1");
1470 return -1;
1471 }
1472 }
1473 return 0;
1474}
1475
pbos@webrtc.org6141e132013-04-09 10:09:10 +00001476int32_t
niklase@google.com470e71d2011-07-07 08:21:25 +00001477Channel::GetRecPayloadType(CodecInst& codec)
1478{
1479 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId,_channelId),
1480 "Channel::GetRecPayloadType()");
pbos@webrtc.org6141e132013-04-09 10:09:10 +00001481 int8_t payloadType(-1);
wu@webrtc.org822fbd82013-08-15 23:38:54 +00001482 if (rtp_payload_registry_->ReceivePayloadType(
1483 codec.plname,
1484 codec.plfreq,
1485 codec.channels,
1486 (codec.rate < 0) ? 0 : codec.rate,
1487 &payloadType) != 0)
niklase@google.com470e71d2011-07-07 08:21:25 +00001488 {
1489 _engineStatisticsPtr->SetLastError(
henrika@webrtc.org37198002012-06-18 11:00:12 +00001490 VE_RTP_RTCP_MODULE_ERROR, kTraceWarning,
niklase@google.com470e71d2011-07-07 08:21:25 +00001491 "GetRecPayloadType() failed to retrieve RX payload type");
1492 return -1;
1493 }
1494 codec.pltype = payloadType;
1495 WEBRTC_TRACE(kTraceStateInfo, kTraceVoice, VoEId(_instanceId,_channelId),
1496 "Channel::GetRecPayloadType() => pltype=%u", codec.pltype);
1497 return 0;
1498}
1499
pbos@webrtc.org6141e132013-04-09 10:09:10 +00001500int32_t
niklase@google.com470e71d2011-07-07 08:21:25 +00001501Channel::SetSendCNPayloadType(int type, PayloadFrequencies frequency)
1502{
1503 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId,_channelId),
1504 "Channel::SetSendCNPayloadType()");
1505
1506 CodecInst codec;
pbos@webrtc.org6141e132013-04-09 10:09:10 +00001507 int32_t samplingFreqHz(-1);
tina.legrand@webrtc.org45175852012-06-01 09:27:35 +00001508 const int kMono = 1;
niklase@google.com470e71d2011-07-07 08:21:25 +00001509 if (frequency == kFreq32000Hz)
1510 samplingFreqHz = 32000;
1511 else if (frequency == kFreq16000Hz)
1512 samplingFreqHz = 16000;
1513
andrew@webrtc.orgeb524d92013-09-23 23:02:24 +00001514 if (audio_coding_->Codec("CN", &codec, samplingFreqHz, kMono) == -1)
niklase@google.com470e71d2011-07-07 08:21:25 +00001515 {
1516 _engineStatisticsPtr->SetLastError(
1517 VE_AUDIO_CODING_MODULE_ERROR, kTraceError,
1518 "SetSendCNPayloadType() failed to retrieve default CN codec "
1519 "settings");
1520 return -1;
1521 }
1522
1523 // Modify the payload type (must be set to dynamic range)
1524 codec.pltype = type;
1525
andrew@webrtc.orgeb524d92013-09-23 23:02:24 +00001526 if (audio_coding_->RegisterSendCodec(codec) != 0)
niklase@google.com470e71d2011-07-07 08:21:25 +00001527 {
1528 _engineStatisticsPtr->SetLastError(
1529 VE_AUDIO_CODING_MODULE_ERROR, kTraceError,
1530 "SetSendCNPayloadType() failed to register CN to ACM");
1531 return -1;
1532 }
1533
pwestin@webrtc.org2853dde2012-05-11 11:08:54 +00001534 if (_rtpRtcpModule->RegisterSendPayload(codec) != 0)
niklase@google.com470e71d2011-07-07 08:21:25 +00001535 {
pwestin@webrtc.org2853dde2012-05-11 11:08:54 +00001536 _rtpRtcpModule->DeRegisterSendPayload(codec.pltype);
1537 if (_rtpRtcpModule->RegisterSendPayload(codec) != 0)
niklase@google.com470e71d2011-07-07 08:21:25 +00001538 {
1539 _engineStatisticsPtr->SetLastError(
1540 VE_RTP_RTCP_MODULE_ERROR, kTraceError,
1541 "SetSendCNPayloadType() failed to register CN to RTP/RTCP "
1542 "module");
1543 return -1;
1544 }
1545 }
1546 return 0;
1547}
1548
minyue@webrtc.orgadee8f92014-09-03 12:28:06 +00001549int Channel::SetOpusMaxPlaybackRate(int frequency_hz) {
minyue@webrtc.org6aac93b2014-08-12 08:13:33 +00001550 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId, _channelId),
minyue@webrtc.orgadee8f92014-09-03 12:28:06 +00001551 "Channel::SetOpusMaxPlaybackRate()");
minyue@webrtc.org6aac93b2014-08-12 08:13:33 +00001552
minyue@webrtc.orgadee8f92014-09-03 12:28:06 +00001553 if (audio_coding_->SetOpusMaxPlaybackRate(frequency_hz) != 0) {
minyue@webrtc.org6aac93b2014-08-12 08:13:33 +00001554 _engineStatisticsPtr->SetLastError(
1555 VE_AUDIO_CODING_MODULE_ERROR, kTraceError,
minyue@webrtc.orgadee8f92014-09-03 12:28:06 +00001556 "SetOpusMaxPlaybackRate() failed to set maximum playback rate");
minyue@webrtc.org6aac93b2014-08-12 08:13:33 +00001557 return -1;
1558 }
1559 return 0;
1560}
1561
pbos@webrtc.org6141e132013-04-09 10:09:10 +00001562int32_t Channel::RegisterExternalTransport(Transport& transport)
niklase@google.com470e71d2011-07-07 08:21:25 +00001563{
1564 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId, _channelId),
1565 "Channel::RegisterExternalTransport()");
1566
mflodman@webrtc.org9a065d12012-03-07 08:12:21 +00001567 CriticalSectionScoped cs(&_callbackCritSect);
niklase@google.com470e71d2011-07-07 08:21:25 +00001568
niklase@google.com470e71d2011-07-07 08:21:25 +00001569 if (_externalTransport)
1570 {
1571 _engineStatisticsPtr->SetLastError(VE_INVALID_OPERATION,
1572 kTraceError,
1573 "RegisterExternalTransport() external transport already enabled");
1574 return -1;
1575 }
1576 _externalTransport = true;
1577 _transportPtr = &transport;
1578 return 0;
1579}
1580
pbos@webrtc.org6141e132013-04-09 10:09:10 +00001581int32_t
niklase@google.com470e71d2011-07-07 08:21:25 +00001582Channel::DeRegisterExternalTransport()
1583{
1584 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId,_channelId),
1585 "Channel::DeRegisterExternalTransport()");
1586
mflodman@webrtc.org9a065d12012-03-07 08:12:21 +00001587 CriticalSectionScoped cs(&_callbackCritSect);
xians@webrtc.org83661f52011-11-25 10:58:15 +00001588
niklase@google.com470e71d2011-07-07 08:21:25 +00001589 if (!_transportPtr)
1590 {
1591 _engineStatisticsPtr->SetLastError(
1592 VE_INVALID_OPERATION, kTraceWarning,
1593 "DeRegisterExternalTransport() external transport already "
1594 "disabled");
1595 return 0;
1596 }
1597 _externalTransport = false;
niklase@google.com470e71d2011-07-07 08:21:25 +00001598 _transportPtr = NULL;
1599 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId,_channelId),
1600 "DeRegisterExternalTransport() all transport is disabled");
niklase@google.com470e71d2011-07-07 08:21:25 +00001601 return 0;
1602}
1603
solenberg@webrtc.orgb1f50102014-03-24 10:38:25 +00001604int32_t Channel::ReceivedRTPPacket(const int8_t* data, int32_t length,
1605 const PacketTime& packet_time) {
pwestin@webrtc.org0c459572013-04-03 15:43:57 +00001606 WEBRTC_TRACE(kTraceStream, kTraceVoice, VoEId(_instanceId,_channelId),
1607 "Channel::ReceivedRTPPacket()");
1608
1609 // Store playout timestamp for the received RTP packet
pwestin@webrtc.org1de01352013-04-11 20:23:35 +00001610 UpdatePlayoutTimestamp(false);
pwestin@webrtc.org0c459572013-04-03 15:43:57 +00001611
1612 // Dump the RTP packet to a file (if RTP dump is enabled).
pbos@webrtc.org6141e132013-04-09 10:09:10 +00001613 if (_rtpDumpIn.DumpPacket((const uint8_t*)data,
1614 (uint16_t)length) == -1) {
pwestin@webrtc.org0c459572013-04-03 15:43:57 +00001615 WEBRTC_TRACE(kTraceWarning, kTraceVoice,
1616 VoEId(_instanceId,_channelId),
1617 "Channel::SendPacket() RTP dump to input file failed");
1618 }
stefan@webrtc.org7bb8f022013-09-06 13:40:11 +00001619 const uint8_t* received_packet = reinterpret_cast<const uint8_t*>(data);
stefan@webrtc.orga5cb98c2013-05-29 12:12:51 +00001620 RTPHeader header;
stefan@webrtc.org7bb8f022013-09-06 13:40:11 +00001621 if (!rtp_header_parser_->Parse(received_packet, length, &header)) {
1622 WEBRTC_TRACE(webrtc::kTraceDebug, webrtc::kTraceVoice, _channelId,
1623 "Incoming packet: invalid RTP header");
stefan@webrtc.orga5cb98c2013-05-29 12:12:51 +00001624 return -1;
1625 }
wu@webrtc.org822fbd82013-08-15 23:38:54 +00001626 header.payload_type_frequency =
1627 rtp_payload_registry_->GetPayloadTypeFrequency(header.payloadType);
stefan@webrtc.org7bb8f022013-09-06 13:40:11 +00001628 if (header.payload_type_frequency < 0)
wu@webrtc.org822fbd82013-08-15 23:38:54 +00001629 return -1;
stefan@webrtc.org48df3812013-11-08 15:18:52 +00001630 bool in_order = IsPacketInOrder(header);
stefan@webrtc.org7bb8f022013-09-06 13:40:11 +00001631 rtp_receive_statistics_->IncomingPacket(header, length,
stefan@webrtc.org48df3812013-11-08 15:18:52 +00001632 IsPacketRetransmitted(header, in_order));
stefan@webrtc.org7bb8f022013-09-06 13:40:11 +00001633 rtp_payload_registry_->SetIncomingPayloadType(header);
solenberg@webrtc.orgb1f50102014-03-24 10:38:25 +00001634
1635 // Forward any packets to ViE bandwidth estimator, if enabled.
1636 {
1637 CriticalSectionScoped cs(&_callbackCritSect);
1638 if (vie_network_) {
1639 int64_t arrival_time_ms;
1640 if (packet_time.timestamp != -1) {
1641 arrival_time_ms = (packet_time.timestamp + 500) / 1000;
1642 } else {
1643 arrival_time_ms = TickTime::MillisecondTimestamp();
1644 }
1645 int payload_length = length - header.headerLength;
1646 vie_network_->ReceivedBWEPacket(video_channel_, arrival_time_ms,
1647 payload_length, header);
1648 }
1649 }
1650
stefan@webrtc.org48df3812013-11-08 15:18:52 +00001651 return ReceivePacket(received_packet, length, header, in_order) ? 0 : -1;
stefan@webrtc.org7bb8f022013-09-06 13:40:11 +00001652}
1653
1654bool Channel::ReceivePacket(const uint8_t* packet,
1655 int packet_length,
1656 const RTPHeader& header,
1657 bool in_order) {
1658 if (rtp_payload_registry_->IsEncapsulated(header)) {
1659 return HandleEncapsulation(packet, packet_length, header);
wu@webrtc.org822fbd82013-08-15 23:38:54 +00001660 }
stefan@webrtc.org7bb8f022013-09-06 13:40:11 +00001661 const uint8_t* payload = packet + header.headerLength;
1662 int payload_length = packet_length - header.headerLength;
1663 assert(payload_length >= 0);
wu@webrtc.org822fbd82013-08-15 23:38:54 +00001664 PayloadUnion payload_specific;
1665 if (!rtp_payload_registry_->GetPayloadSpecifics(header.payloadType,
stefan@webrtc.org7bb8f022013-09-06 13:40:11 +00001666 &payload_specific)) {
1667 return false;
wu@webrtc.org822fbd82013-08-15 23:38:54 +00001668 }
stefan@webrtc.org7bb8f022013-09-06 13:40:11 +00001669 return rtp_receiver_->IncomingRtpPacket(header, payload, payload_length,
1670 payload_specific, in_order);
1671}
1672
1673bool Channel::HandleEncapsulation(const uint8_t* packet,
1674 int packet_length,
1675 const RTPHeader& header) {
1676 if (!rtp_payload_registry_->IsRtx(header))
1677 return false;
1678
1679 // Remove the RTX header and parse the original RTP header.
1680 if (packet_length < header.headerLength)
1681 return false;
1682 if (packet_length > kVoiceEngineMaxIpPacketSizeBytes)
1683 return false;
1684 if (restored_packet_in_use_) {
1685 WEBRTC_TRACE(webrtc::kTraceDebug, webrtc::kTraceVoice, _channelId,
1686 "Multiple RTX headers detected, dropping packet");
1687 return false;
pwestin@webrtc.org0c459572013-04-03 15:43:57 +00001688 }
stefan@webrtc.org7bb8f022013-09-06 13:40:11 +00001689 uint8_t* restored_packet_ptr = restored_packet_;
1690 if (!rtp_payload_registry_->RestoreOriginalPacket(
1691 &restored_packet_ptr, packet, &packet_length, rtp_receiver_->SSRC(),
1692 header)) {
1693 WEBRTC_TRACE(webrtc::kTraceDebug, webrtc::kTraceVoice, _channelId,
1694 "Incoming RTX packet: invalid RTP header");
1695 return false;
1696 }
1697 restored_packet_in_use_ = true;
1698 bool ret = OnRecoveredPacket(restored_packet_ptr, packet_length);
1699 restored_packet_in_use_ = false;
1700 return ret;
1701}
1702
1703bool Channel::IsPacketInOrder(const RTPHeader& header) const {
1704 StreamStatistician* statistician =
1705 rtp_receive_statistics_->GetStatistician(header.ssrc);
1706 if (!statistician)
1707 return false;
1708 return statistician->IsPacketInOrder(header.sequenceNumber);
niklase@google.com470e71d2011-07-07 08:21:25 +00001709}
1710
stefan@webrtc.org48df3812013-11-08 15:18:52 +00001711bool Channel::IsPacketRetransmitted(const RTPHeader& header,
1712 bool in_order) const {
stefan@webrtc.org7bb8f022013-09-06 13:40:11 +00001713 // Retransmissions are handled separately if RTX is enabled.
1714 if (rtp_payload_registry_->RtxEnabled())
1715 return false;
1716 StreamStatistician* statistician =
1717 rtp_receive_statistics_->GetStatistician(header.ssrc);
1718 if (!statistician)
1719 return false;
1720 // Check if this is a retransmission.
1721 uint16_t min_rtt = 0;
1722 _rtpRtcpModule->RTT(rtp_receiver_->SSRC(), NULL, NULL, &min_rtt, NULL);
stefan@webrtc.org48df3812013-11-08 15:18:52 +00001723 return !in_order &&
stefan@webrtc.org7bb8f022013-09-06 13:40:11 +00001724 statistician->IsRetransmitOfOldPacket(header, min_rtt);
wu@webrtc.org822fbd82013-08-15 23:38:54 +00001725}
1726
pbos@webrtc.org6141e132013-04-09 10:09:10 +00001727int32_t Channel::ReceivedRTCPPacket(const int8_t* data, int32_t length) {
pwestin@webrtc.org0c459572013-04-03 15:43:57 +00001728 WEBRTC_TRACE(kTraceStream, kTraceVoice, VoEId(_instanceId,_channelId),
1729 "Channel::ReceivedRTCPPacket()");
1730 // Store playout timestamp for the received RTCP packet
pwestin@webrtc.org1de01352013-04-11 20:23:35 +00001731 UpdatePlayoutTimestamp(true);
pwestin@webrtc.org0c459572013-04-03 15:43:57 +00001732
1733 // Dump the RTCP packet to a file (if RTP dump is enabled).
pbos@webrtc.org6141e132013-04-09 10:09:10 +00001734 if (_rtpDumpIn.DumpPacket((const uint8_t*)data,
1735 (uint16_t)length) == -1) {
pwestin@webrtc.org0c459572013-04-03 15:43:57 +00001736 WEBRTC_TRACE(kTraceWarning, kTraceVoice,
1737 VoEId(_instanceId,_channelId),
1738 "Channel::SendPacket() RTCP dump to input file failed");
1739 }
1740
1741 // Deliver RTCP packet to RTP/RTCP module for parsing
stefan@webrtc.orga5cb98c2013-05-29 12:12:51 +00001742 if (_rtpRtcpModule->IncomingRtcpPacket((const uint8_t*)data,
1743 (uint16_t)length) == -1) {
pwestin@webrtc.org0c459572013-04-03 15:43:57 +00001744 _engineStatisticsPtr->SetLastError(
1745 VE_SOCKET_TRANSPORT_MODULE_ERROR, kTraceWarning,
1746 "Channel::IncomingRTPPacket() RTCP packet is invalid");
1747 }
wu@webrtc.org82c4b852014-05-20 22:55:01 +00001748
stefan@webrtc.org8e24d872014-09-02 18:58:24 +00001749 {
1750 CriticalSectionScoped lock(ts_stats_lock_.get());
minyue@webrtc.org2c0cdbc2014-10-09 10:52:43 +00001751 uint16_t rtt = GetRTT();
1752 if (rtt == 0) {
1753 // Waiting for valid RTT.
1754 return 0;
1755 }
1756 uint32_t ntp_secs = 0;
1757 uint32_t ntp_frac = 0;
1758 uint32_t rtp_timestamp = 0;
1759 if (0 != _rtpRtcpModule->RemoteNTP(&ntp_secs, &ntp_frac, NULL, NULL,
1760 &rtp_timestamp)) {
1761 // Waiting for RTCP.
1762 return 0;
1763 }
1764 ntp_estimator_.UpdateRtcpTimestamp(rtt, ntp_secs, ntp_frac, rtp_timestamp);
stefan@webrtc.org8e24d872014-09-02 18:58:24 +00001765 }
pwestin@webrtc.org0c459572013-04-03 15:43:57 +00001766 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +00001767}
1768
niklase@google.com470e71d2011-07-07 08:21:25 +00001769int Channel::StartPlayingFileLocally(const char* fileName,
pbos@webrtc.org92135212013-05-14 08:31:39 +00001770 bool loop,
1771 FileFormats format,
1772 int startPosition,
1773 float volumeScaling,
1774 int stopPosition,
niklase@google.com470e71d2011-07-07 08:21:25 +00001775 const CodecInst* codecInst)
1776{
1777 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId,_channelId),
1778 "Channel::StartPlayingFileLocally(fileNameUTF8[]=%s, loop=%d,"
1779 " format=%d, volumeScaling=%5.3f, startPosition=%d, "
1780 "stopPosition=%d)", fileName, loop, format, volumeScaling,
1781 startPosition, stopPosition);
1782
henrika@webrtc.org944cbeb2014-03-18 10:32:33 +00001783 if (channel_state_.Get().output_file_playing)
niklase@google.com470e71d2011-07-07 08:21:25 +00001784 {
1785 _engineStatisticsPtr->SetLastError(
1786 VE_ALREADY_PLAYING, kTraceError,
1787 "StartPlayingFileLocally() is already playing");
1788 return -1;
1789 }
1790
niklase@google.com470e71d2011-07-07 08:21:25 +00001791 {
mflodman@webrtc.org9a065d12012-03-07 08:12:21 +00001792 CriticalSectionScoped cs(&_fileCritSect);
henrike@webrtc.orgb37c6282011-10-31 23:53:04 +00001793
1794 if (_outputFilePlayerPtr)
1795 {
1796 _outputFilePlayerPtr->RegisterModuleFileCallback(NULL);
1797 FilePlayer::DestroyFilePlayer(_outputFilePlayerPtr);
1798 _outputFilePlayerPtr = NULL;
1799 }
1800
1801 _outputFilePlayerPtr = FilePlayer::CreateFilePlayer(
1802 _outputFilePlayerId, (const FileFormats)format);
1803
1804 if (_outputFilePlayerPtr == NULL)
1805 {
1806 _engineStatisticsPtr->SetLastError(
1807 VE_INVALID_ARGUMENT, kTraceError,
henrike@webrtc.org31d30702011-11-18 19:59:32 +00001808 "StartPlayingFileLocally() filePlayer format is not correct");
henrike@webrtc.orgb37c6282011-10-31 23:53:04 +00001809 return -1;
1810 }
1811
pbos@webrtc.org6141e132013-04-09 10:09:10 +00001812 const uint32_t notificationTime(0);
henrike@webrtc.orgb37c6282011-10-31 23:53:04 +00001813
1814 if (_outputFilePlayerPtr->StartPlayingFile(
1815 fileName,
1816 loop,
1817 startPosition,
1818 volumeScaling,
1819 notificationTime,
1820 stopPosition,
1821 (const CodecInst*)codecInst) != 0)
1822 {
1823 _engineStatisticsPtr->SetLastError(
1824 VE_BAD_FILE, kTraceError,
1825 "StartPlayingFile() failed to start file playout");
1826 _outputFilePlayerPtr->StopPlayingFile();
1827 FilePlayer::DestroyFilePlayer(_outputFilePlayerPtr);
1828 _outputFilePlayerPtr = NULL;
1829 return -1;
1830 }
1831 _outputFilePlayerPtr->RegisterModuleFileCallback(this);
henrika@webrtc.org944cbeb2014-03-18 10:32:33 +00001832 channel_state_.SetOutputFilePlaying(true);
niklase@google.com470e71d2011-07-07 08:21:25 +00001833 }
braveyao@webrtc.orgab129902012-06-04 03:26:39 +00001834
1835 if (RegisterFilePlayingToMixer() != 0)
henrike@webrtc.org066f9e52011-10-28 23:15:47 +00001836 return -1;
niklase@google.com470e71d2011-07-07 08:21:25 +00001837
1838 return 0;
1839}
1840
1841int Channel::StartPlayingFileLocally(InStream* stream,
pbos@webrtc.org92135212013-05-14 08:31:39 +00001842 FileFormats format,
1843 int startPosition,
1844 float volumeScaling,
1845 int stopPosition,
niklase@google.com470e71d2011-07-07 08:21:25 +00001846 const CodecInst* codecInst)
1847{
1848 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId,_channelId),
1849 "Channel::StartPlayingFileLocally(format=%d,"
1850 " volumeScaling=%5.3f, startPosition=%d, stopPosition=%d)",
1851 format, volumeScaling, startPosition, stopPosition);
1852
1853 if(stream == NULL)
1854 {
1855 _engineStatisticsPtr->SetLastError(
1856 VE_BAD_FILE, kTraceError,
1857 "StartPlayingFileLocally() NULL as input stream");
1858 return -1;
1859 }
1860
1861
henrika@webrtc.org944cbeb2014-03-18 10:32:33 +00001862 if (channel_state_.Get().output_file_playing)
niklase@google.com470e71d2011-07-07 08:21:25 +00001863 {
1864 _engineStatisticsPtr->SetLastError(
1865 VE_ALREADY_PLAYING, kTraceError,
1866 "StartPlayingFileLocally() is already playing");
1867 return -1;
1868 }
1869
niklase@google.com470e71d2011-07-07 08:21:25 +00001870 {
mflodman@webrtc.org9a065d12012-03-07 08:12:21 +00001871 CriticalSectionScoped cs(&_fileCritSect);
henrike@webrtc.orgb37c6282011-10-31 23:53:04 +00001872
1873 // Destroy the old instance
1874 if (_outputFilePlayerPtr)
1875 {
1876 _outputFilePlayerPtr->RegisterModuleFileCallback(NULL);
1877 FilePlayer::DestroyFilePlayer(_outputFilePlayerPtr);
1878 _outputFilePlayerPtr = NULL;
1879 }
1880
1881 // Create the instance
1882 _outputFilePlayerPtr = FilePlayer::CreateFilePlayer(
1883 _outputFilePlayerId,
1884 (const FileFormats)format);
1885
1886 if (_outputFilePlayerPtr == NULL)
1887 {
1888 _engineStatisticsPtr->SetLastError(
1889 VE_INVALID_ARGUMENT, kTraceError,
1890 "StartPlayingFileLocally() filePlayer format isnot correct");
1891 return -1;
1892 }
1893
pbos@webrtc.org6141e132013-04-09 10:09:10 +00001894 const uint32_t notificationTime(0);
henrike@webrtc.orgb37c6282011-10-31 23:53:04 +00001895
1896 if (_outputFilePlayerPtr->StartPlayingFile(*stream, startPosition,
1897 volumeScaling,
1898 notificationTime,
1899 stopPosition, codecInst) != 0)
1900 {
1901 _engineStatisticsPtr->SetLastError(VE_BAD_FILE, kTraceError,
1902 "StartPlayingFile() failed to "
1903 "start file playout");
1904 _outputFilePlayerPtr->StopPlayingFile();
1905 FilePlayer::DestroyFilePlayer(_outputFilePlayerPtr);
1906 _outputFilePlayerPtr = NULL;
1907 return -1;
1908 }
1909 _outputFilePlayerPtr->RegisterModuleFileCallback(this);
henrika@webrtc.org944cbeb2014-03-18 10:32:33 +00001910 channel_state_.SetOutputFilePlaying(true);
niklase@google.com470e71d2011-07-07 08:21:25 +00001911 }
braveyao@webrtc.orgab129902012-06-04 03:26:39 +00001912
1913 if (RegisterFilePlayingToMixer() != 0)
henrike@webrtc.org066f9e52011-10-28 23:15:47 +00001914 return -1;
niklase@google.com470e71d2011-07-07 08:21:25 +00001915
niklase@google.com470e71d2011-07-07 08:21:25 +00001916 return 0;
1917}
1918
1919int Channel::StopPlayingFileLocally()
1920{
1921 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId,_channelId),
1922 "Channel::StopPlayingFileLocally()");
1923
henrika@webrtc.org944cbeb2014-03-18 10:32:33 +00001924 if (!channel_state_.Get().output_file_playing)
niklase@google.com470e71d2011-07-07 08:21:25 +00001925 {
1926 _engineStatisticsPtr->SetLastError(
1927 VE_INVALID_OPERATION, kTraceWarning,
1928 "StopPlayingFileLocally() isnot playing");
1929 return 0;
1930 }
1931
niklase@google.com470e71d2011-07-07 08:21:25 +00001932 {
mflodman@webrtc.org9a065d12012-03-07 08:12:21 +00001933 CriticalSectionScoped cs(&_fileCritSect);
henrike@webrtc.orgb37c6282011-10-31 23:53:04 +00001934
1935 if (_outputFilePlayerPtr->StopPlayingFile() != 0)
1936 {
1937 _engineStatisticsPtr->SetLastError(
1938 VE_STOP_RECORDING_FAILED, kTraceError,
1939 "StopPlayingFile() could not stop playing");
1940 return -1;
1941 }
1942 _outputFilePlayerPtr->RegisterModuleFileCallback(NULL);
1943 FilePlayer::DestroyFilePlayer(_outputFilePlayerPtr);
1944 _outputFilePlayerPtr = NULL;
henrika@webrtc.org944cbeb2014-03-18 10:32:33 +00001945 channel_state_.SetOutputFilePlaying(false);
niklase@google.com470e71d2011-07-07 08:21:25 +00001946 }
henrike@webrtc.orgb37c6282011-10-31 23:53:04 +00001947 // _fileCritSect cannot be taken while calling
1948 // SetAnonymousMixibilityStatus. Refer to comments in
1949 // StartPlayingFileLocally(const char* ...) for more details.
henrike@webrtc.org066f9e52011-10-28 23:15:47 +00001950 if (_outputMixerPtr->SetAnonymousMixabilityStatus(*this, false) != 0)
1951 {
1952 _engineStatisticsPtr->SetLastError(
1953 VE_AUDIO_CONF_MIX_MODULE_ERROR, kTraceError,
henrike@webrtc.orgb37c6282011-10-31 23:53:04 +00001954 "StopPlayingFile() failed to stop participant from playing as"
1955 "file in the mixer");
henrike@webrtc.org066f9e52011-10-28 23:15:47 +00001956 return -1;
1957 }
niklase@google.com470e71d2011-07-07 08:21:25 +00001958
1959 return 0;
1960}
1961
1962int Channel::IsPlayingFileLocally() const
1963{
1964 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId,_channelId),
1965 "Channel::IsPlayingFileLocally()");
1966
henrika@webrtc.org944cbeb2014-03-18 10:32:33 +00001967 return channel_state_.Get().output_file_playing;
niklase@google.com470e71d2011-07-07 08:21:25 +00001968}
1969
braveyao@webrtc.orgab129902012-06-04 03:26:39 +00001970int Channel::RegisterFilePlayingToMixer()
1971{
1972 // Return success for not registering for file playing to mixer if:
1973 // 1. playing file before playout is started on that channel.
1974 // 2. starting playout without file playing on that channel.
henrika@webrtc.org944cbeb2014-03-18 10:32:33 +00001975 if (!channel_state_.Get().playing ||
1976 !channel_state_.Get().output_file_playing)
braveyao@webrtc.orgab129902012-06-04 03:26:39 +00001977 {
1978 return 0;
1979 }
1980
1981 // |_fileCritSect| cannot be taken while calling
1982 // SetAnonymousMixabilityStatus() since as soon as the participant is added
1983 // frames can be pulled by the mixer. Since the frames are generated from
1984 // the file, _fileCritSect will be taken. This would result in a deadlock.
1985 if (_outputMixerPtr->SetAnonymousMixabilityStatus(*this, true) != 0)
1986 {
henrika@webrtc.org944cbeb2014-03-18 10:32:33 +00001987 channel_state_.SetOutputFilePlaying(false);
braveyao@webrtc.orgab129902012-06-04 03:26:39 +00001988 CriticalSectionScoped cs(&_fileCritSect);
braveyao@webrtc.orgab129902012-06-04 03:26:39 +00001989 _engineStatisticsPtr->SetLastError(
1990 VE_AUDIO_CONF_MIX_MODULE_ERROR, kTraceError,
1991 "StartPlayingFile() failed to add participant as file to mixer");
1992 _outputFilePlayerPtr->StopPlayingFile();
1993 FilePlayer::DestroyFilePlayer(_outputFilePlayerPtr);
1994 _outputFilePlayerPtr = NULL;
1995 return -1;
1996 }
1997
1998 return 0;
1999}
2000
niklase@google.com470e71d2011-07-07 08:21:25 +00002001int Channel::StartPlayingFileAsMicrophone(const char* fileName,
pbos@webrtc.org92135212013-05-14 08:31:39 +00002002 bool loop,
2003 FileFormats format,
2004 int startPosition,
2005 float volumeScaling,
2006 int stopPosition,
niklase@google.com470e71d2011-07-07 08:21:25 +00002007 const CodecInst* codecInst)
2008{
2009 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId,_channelId),
2010 "Channel::StartPlayingFileAsMicrophone(fileNameUTF8[]=%s, "
2011 "loop=%d, format=%d, volumeScaling=%5.3f, startPosition=%d, "
2012 "stopPosition=%d)", fileName, loop, format, volumeScaling,
2013 startPosition, stopPosition);
2014
henrika@webrtc.org944cbeb2014-03-18 10:32:33 +00002015 CriticalSectionScoped cs(&_fileCritSect);
2016
2017 if (channel_state_.Get().input_file_playing)
niklase@google.com470e71d2011-07-07 08:21:25 +00002018 {
2019 _engineStatisticsPtr->SetLastError(
2020 VE_ALREADY_PLAYING, kTraceWarning,
2021 "StartPlayingFileAsMicrophone() filePlayer is playing");
2022 return 0;
2023 }
2024
niklase@google.com470e71d2011-07-07 08:21:25 +00002025 // Destroy the old instance
2026 if (_inputFilePlayerPtr)
2027 {
2028 _inputFilePlayerPtr->RegisterModuleFileCallback(NULL);
2029 FilePlayer::DestroyFilePlayer(_inputFilePlayerPtr);
2030 _inputFilePlayerPtr = NULL;
2031 }
2032
2033 // Create the instance
2034 _inputFilePlayerPtr = FilePlayer::CreateFilePlayer(
2035 _inputFilePlayerId, (const FileFormats)format);
2036
2037 if (_inputFilePlayerPtr == NULL)
2038 {
2039 _engineStatisticsPtr->SetLastError(
2040 VE_INVALID_ARGUMENT, kTraceError,
2041 "StartPlayingFileAsMicrophone() filePlayer format isnot correct");
2042 return -1;
2043 }
2044
pbos@webrtc.org6141e132013-04-09 10:09:10 +00002045 const uint32_t notificationTime(0);
niklase@google.com470e71d2011-07-07 08:21:25 +00002046
2047 if (_inputFilePlayerPtr->StartPlayingFile(
2048 fileName,
2049 loop,
2050 startPosition,
2051 volumeScaling,
2052 notificationTime,
2053 stopPosition,
2054 (const CodecInst*)codecInst) != 0)
2055 {
2056 _engineStatisticsPtr->SetLastError(
2057 VE_BAD_FILE, kTraceError,
2058 "StartPlayingFile() failed to start file playout");
2059 _inputFilePlayerPtr->StopPlayingFile();
2060 FilePlayer::DestroyFilePlayer(_inputFilePlayerPtr);
2061 _inputFilePlayerPtr = NULL;
2062 return -1;
2063 }
2064 _inputFilePlayerPtr->RegisterModuleFileCallback(this);
henrika@webrtc.org944cbeb2014-03-18 10:32:33 +00002065 channel_state_.SetInputFilePlaying(true);
niklase@google.com470e71d2011-07-07 08:21:25 +00002066
2067 return 0;
2068}
2069
2070int Channel::StartPlayingFileAsMicrophone(InStream* stream,
pbos@webrtc.org92135212013-05-14 08:31:39 +00002071 FileFormats format,
2072 int startPosition,
2073 float volumeScaling,
2074 int stopPosition,
niklase@google.com470e71d2011-07-07 08:21:25 +00002075 const CodecInst* codecInst)
2076{
2077 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId,_channelId),
2078 "Channel::StartPlayingFileAsMicrophone(format=%d, "
2079 "volumeScaling=%5.3f, startPosition=%d, stopPosition=%d)",
2080 format, volumeScaling, startPosition, stopPosition);
2081
2082 if(stream == NULL)
2083 {
2084 _engineStatisticsPtr->SetLastError(
2085 VE_BAD_FILE, kTraceError,
2086 "StartPlayingFileAsMicrophone NULL as input stream");
2087 return -1;
2088 }
2089
henrika@webrtc.org944cbeb2014-03-18 10:32:33 +00002090 CriticalSectionScoped cs(&_fileCritSect);
2091
2092 if (channel_state_.Get().input_file_playing)
niklase@google.com470e71d2011-07-07 08:21:25 +00002093 {
2094 _engineStatisticsPtr->SetLastError(
2095 VE_ALREADY_PLAYING, kTraceWarning,
2096 "StartPlayingFileAsMicrophone() is playing");
2097 return 0;
2098 }
2099
niklase@google.com470e71d2011-07-07 08:21:25 +00002100 // Destroy the old instance
2101 if (_inputFilePlayerPtr)
2102 {
2103 _inputFilePlayerPtr->RegisterModuleFileCallback(NULL);
2104 FilePlayer::DestroyFilePlayer(_inputFilePlayerPtr);
2105 _inputFilePlayerPtr = NULL;
2106 }
2107
2108 // Create the instance
2109 _inputFilePlayerPtr = FilePlayer::CreateFilePlayer(
2110 _inputFilePlayerId, (const FileFormats)format);
2111
2112 if (_inputFilePlayerPtr == NULL)
2113 {
2114 _engineStatisticsPtr->SetLastError(
2115 VE_INVALID_ARGUMENT, kTraceError,
2116 "StartPlayingInputFile() filePlayer format isnot correct");
2117 return -1;
2118 }
2119
pbos@webrtc.org6141e132013-04-09 10:09:10 +00002120 const uint32_t notificationTime(0);
niklase@google.com470e71d2011-07-07 08:21:25 +00002121
2122 if (_inputFilePlayerPtr->StartPlayingFile(*stream, startPosition,
2123 volumeScaling, notificationTime,
2124 stopPosition, codecInst) != 0)
2125 {
2126 _engineStatisticsPtr->SetLastError(VE_BAD_FILE, kTraceError,
2127 "StartPlayingFile() failed to start "
2128 "file playout");
2129 _inputFilePlayerPtr->StopPlayingFile();
2130 FilePlayer::DestroyFilePlayer(_inputFilePlayerPtr);
2131 _inputFilePlayerPtr = NULL;
2132 return -1;
2133 }
andrew@webrtc.orgae1a58b2013-01-22 04:44:30 +00002134
niklase@google.com470e71d2011-07-07 08:21:25 +00002135 _inputFilePlayerPtr->RegisterModuleFileCallback(this);
henrika@webrtc.org944cbeb2014-03-18 10:32:33 +00002136 channel_state_.SetInputFilePlaying(true);
niklase@google.com470e71d2011-07-07 08:21:25 +00002137
2138 return 0;
2139}
2140
2141int Channel::StopPlayingFileAsMicrophone()
2142{
2143 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId,_channelId),
2144 "Channel::StopPlayingFileAsMicrophone()");
2145
henrika@webrtc.org944cbeb2014-03-18 10:32:33 +00002146 CriticalSectionScoped cs(&_fileCritSect);
2147
2148 if (!channel_state_.Get().input_file_playing)
niklase@google.com470e71d2011-07-07 08:21:25 +00002149 {
2150 _engineStatisticsPtr->SetLastError(
2151 VE_INVALID_OPERATION, kTraceWarning,
2152 "StopPlayingFileAsMicrophone() isnot playing");
2153 return 0;
2154 }
2155
niklase@google.com470e71d2011-07-07 08:21:25 +00002156 if (_inputFilePlayerPtr->StopPlayingFile() != 0)
2157 {
2158 _engineStatisticsPtr->SetLastError(
2159 VE_STOP_RECORDING_FAILED, kTraceError,
2160 "StopPlayingFile() could not stop playing");
2161 return -1;
2162 }
2163 _inputFilePlayerPtr->RegisterModuleFileCallback(NULL);
2164 FilePlayer::DestroyFilePlayer(_inputFilePlayerPtr);
2165 _inputFilePlayerPtr = NULL;
henrika@webrtc.org944cbeb2014-03-18 10:32:33 +00002166 channel_state_.SetInputFilePlaying(false);
niklase@google.com470e71d2011-07-07 08:21:25 +00002167
2168 return 0;
2169}
2170
2171int Channel::IsPlayingFileAsMicrophone() const
2172{
2173 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId,_channelId),
2174 "Channel::IsPlayingFileAsMicrophone()");
henrika@webrtc.org944cbeb2014-03-18 10:32:33 +00002175 return channel_state_.Get().input_file_playing;
niklase@google.com470e71d2011-07-07 08:21:25 +00002176}
2177
leozwang@webrtc.org813e4b02012-03-01 18:34:25 +00002178int Channel::StartRecordingPlayout(const char* fileName,
niklase@google.com470e71d2011-07-07 08:21:25 +00002179 const CodecInst* codecInst)
2180{
2181 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId,_channelId),
2182 "Channel::StartRecordingPlayout(fileName=%s)", fileName);
2183
2184 if (_outputFileRecording)
2185 {
2186 WEBRTC_TRACE(kTraceWarning, kTraceVoice, VoEId(_instanceId,-1),
2187 "StartRecordingPlayout() is already recording");
2188 return 0;
2189 }
2190
2191 FileFormats format;
pbos@webrtc.org6141e132013-04-09 10:09:10 +00002192 const uint32_t notificationTime(0); // Not supported in VoE
niklase@google.com470e71d2011-07-07 08:21:25 +00002193 CodecInst dummyCodec={100,"L16",16000,320,1,320000};
2194
niklas.enbom@webrtc.org40197d72012-03-26 08:45:47 +00002195 if ((codecInst != NULL) &&
2196 ((codecInst->channels < 1) || (codecInst->channels > 2)))
niklase@google.com470e71d2011-07-07 08:21:25 +00002197 {
2198 _engineStatisticsPtr->SetLastError(
2199 VE_BAD_ARGUMENT, kTraceError,
2200 "StartRecordingPlayout() invalid compression");
2201 return(-1);
2202 }
2203 if(codecInst == NULL)
2204 {
2205 format = kFileFormatPcm16kHzFile;
2206 codecInst=&dummyCodec;
2207 }
2208 else if((STR_CASE_CMP(codecInst->plname,"L16") == 0) ||
2209 (STR_CASE_CMP(codecInst->plname,"PCMU") == 0) ||
2210 (STR_CASE_CMP(codecInst->plname,"PCMA") == 0))
2211 {
2212 format = kFileFormatWavFile;
2213 }
2214 else
2215 {
2216 format = kFileFormatCompressedFile;
2217 }
2218
mflodman@webrtc.org9a065d12012-03-07 08:12:21 +00002219 CriticalSectionScoped cs(&_fileCritSect);
niklase@google.com470e71d2011-07-07 08:21:25 +00002220
2221 // Destroy the old instance
2222 if (_outputFileRecorderPtr)
2223 {
2224 _outputFileRecorderPtr->RegisterModuleFileCallback(NULL);
2225 FileRecorder::DestroyFileRecorder(_outputFileRecorderPtr);
2226 _outputFileRecorderPtr = NULL;
2227 }
2228
2229 _outputFileRecorderPtr = FileRecorder::CreateFileRecorder(
2230 _outputFileRecorderId, (const FileFormats)format);
2231 if (_outputFileRecorderPtr == NULL)
2232 {
2233 _engineStatisticsPtr->SetLastError(
2234 VE_INVALID_ARGUMENT, kTraceError,
2235 "StartRecordingPlayout() fileRecorder format isnot correct");
2236 return -1;
2237 }
2238
2239 if (_outputFileRecorderPtr->StartRecordingAudioFile(
2240 fileName, (const CodecInst&)*codecInst, notificationTime) != 0)
2241 {
2242 _engineStatisticsPtr->SetLastError(
2243 VE_BAD_FILE, kTraceError,
2244 "StartRecordingAudioFile() failed to start file recording");
2245 _outputFileRecorderPtr->StopRecording();
2246 FileRecorder::DestroyFileRecorder(_outputFileRecorderPtr);
2247 _outputFileRecorderPtr = NULL;
2248 return -1;
2249 }
2250 _outputFileRecorderPtr->RegisterModuleFileCallback(this);
2251 _outputFileRecording = true;
2252
2253 return 0;
2254}
2255
2256int Channel::StartRecordingPlayout(OutStream* stream,
2257 const CodecInst* codecInst)
2258{
2259 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId,_channelId),
2260 "Channel::StartRecordingPlayout()");
2261
2262 if (_outputFileRecording)
2263 {
2264 WEBRTC_TRACE(kTraceWarning, kTraceVoice, VoEId(_instanceId,-1),
2265 "StartRecordingPlayout() is already recording");
2266 return 0;
2267 }
2268
2269 FileFormats format;
pbos@webrtc.org6141e132013-04-09 10:09:10 +00002270 const uint32_t notificationTime(0); // Not supported in VoE
niklase@google.com470e71d2011-07-07 08:21:25 +00002271 CodecInst dummyCodec={100,"L16",16000,320,1,320000};
2272
2273 if (codecInst != NULL && codecInst->channels != 1)
2274 {
2275 _engineStatisticsPtr->SetLastError(
2276 VE_BAD_ARGUMENT, kTraceError,
2277 "StartRecordingPlayout() invalid compression");
2278 return(-1);
2279 }
2280 if(codecInst == NULL)
2281 {
2282 format = kFileFormatPcm16kHzFile;
2283 codecInst=&dummyCodec;
2284 }
2285 else if((STR_CASE_CMP(codecInst->plname,"L16") == 0) ||
2286 (STR_CASE_CMP(codecInst->plname,"PCMU") == 0) ||
2287 (STR_CASE_CMP(codecInst->plname,"PCMA") == 0))
2288 {
2289 format = kFileFormatWavFile;
2290 }
2291 else
2292 {
2293 format = kFileFormatCompressedFile;
2294 }
2295
mflodman@webrtc.org9a065d12012-03-07 08:12:21 +00002296 CriticalSectionScoped cs(&_fileCritSect);
niklase@google.com470e71d2011-07-07 08:21:25 +00002297
2298 // Destroy the old instance
2299 if (_outputFileRecorderPtr)
2300 {
2301 _outputFileRecorderPtr->RegisterModuleFileCallback(NULL);
2302 FileRecorder::DestroyFileRecorder(_outputFileRecorderPtr);
2303 _outputFileRecorderPtr = NULL;
2304 }
2305
2306 _outputFileRecorderPtr = FileRecorder::CreateFileRecorder(
2307 _outputFileRecorderId, (const FileFormats)format);
2308 if (_outputFileRecorderPtr == NULL)
2309 {
2310 _engineStatisticsPtr->SetLastError(
2311 VE_INVALID_ARGUMENT, kTraceError,
2312 "StartRecordingPlayout() fileRecorder format isnot correct");
2313 return -1;
2314 }
2315
2316 if (_outputFileRecorderPtr->StartRecordingAudioFile(*stream, *codecInst,
2317 notificationTime) != 0)
2318 {
2319 _engineStatisticsPtr->SetLastError(VE_BAD_FILE, kTraceError,
2320 "StartRecordingPlayout() failed to "
2321 "start file recording");
2322 _outputFileRecorderPtr->StopRecording();
2323 FileRecorder::DestroyFileRecorder(_outputFileRecorderPtr);
2324 _outputFileRecorderPtr = NULL;
2325 return -1;
2326 }
andrew@webrtc.orgae1a58b2013-01-22 04:44:30 +00002327
niklase@google.com470e71d2011-07-07 08:21:25 +00002328 _outputFileRecorderPtr->RegisterModuleFileCallback(this);
2329 _outputFileRecording = true;
2330
2331 return 0;
2332}
2333
2334int Channel::StopRecordingPlayout()
2335{
2336 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId,-1),
2337 "Channel::StopRecordingPlayout()");
2338
2339 if (!_outputFileRecording)
2340 {
2341 WEBRTC_TRACE(kTraceError, kTraceVoice, VoEId(_instanceId,-1),
2342 "StopRecordingPlayout() isnot recording");
2343 return -1;
2344 }
2345
2346
mflodman@webrtc.org9a065d12012-03-07 08:12:21 +00002347 CriticalSectionScoped cs(&_fileCritSect);
niklase@google.com470e71d2011-07-07 08:21:25 +00002348
2349 if (_outputFileRecorderPtr->StopRecording() != 0)
2350 {
2351 _engineStatisticsPtr->SetLastError(
2352 VE_STOP_RECORDING_FAILED, kTraceError,
2353 "StopRecording() could not stop recording");
2354 return(-1);
2355 }
2356 _outputFileRecorderPtr->RegisterModuleFileCallback(NULL);
2357 FileRecorder::DestroyFileRecorder(_outputFileRecorderPtr);
2358 _outputFileRecorderPtr = NULL;
2359 _outputFileRecording = false;
2360
2361 return 0;
2362}
2363
2364void
2365Channel::SetMixWithMicStatus(bool mix)
2366{
henrika@webrtc.org944cbeb2014-03-18 10:32:33 +00002367 CriticalSectionScoped cs(&_fileCritSect);
niklase@google.com470e71d2011-07-07 08:21:25 +00002368 _mixFileWithMicrophone=mix;
2369}
2370
2371int
pbos@webrtc.org6141e132013-04-09 10:09:10 +00002372Channel::GetSpeechOutputLevel(uint32_t& level) const
niklase@google.com470e71d2011-07-07 08:21:25 +00002373{
pbos@webrtc.org6141e132013-04-09 10:09:10 +00002374 int8_t currentLevel = _outputAudioLevel.Level();
2375 level = static_cast<int32_t> (currentLevel);
niklase@google.com470e71d2011-07-07 08:21:25 +00002376 WEBRTC_TRACE(kTraceStateInfo, kTraceVoice,
2377 VoEId(_instanceId,_channelId),
2378 "GetSpeechOutputLevel() => level=%u", level);
2379 return 0;
2380}
2381
2382int
pbos@webrtc.org6141e132013-04-09 10:09:10 +00002383Channel::GetSpeechOutputLevelFullRange(uint32_t& level) const
niklase@google.com470e71d2011-07-07 08:21:25 +00002384{
pbos@webrtc.org6141e132013-04-09 10:09:10 +00002385 int16_t currentLevel = _outputAudioLevel.LevelFullRange();
2386 level = static_cast<int32_t> (currentLevel);
niklase@google.com470e71d2011-07-07 08:21:25 +00002387 WEBRTC_TRACE(kTraceStateInfo, kTraceVoice,
2388 VoEId(_instanceId,_channelId),
2389 "GetSpeechOutputLevelFullRange() => level=%u", level);
2390 return 0;
2391}
2392
2393int
2394Channel::SetMute(bool enable)
2395{
wu@webrtc.org63420662013-10-17 18:28:55 +00002396 CriticalSectionScoped cs(&volume_settings_critsect_);
niklase@google.com470e71d2011-07-07 08:21:25 +00002397 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId,_channelId),
2398 "Channel::SetMute(enable=%d)", enable);
2399 _mute = enable;
2400 return 0;
2401}
2402
2403bool
2404Channel::Mute() const
2405{
wu@webrtc.org63420662013-10-17 18:28:55 +00002406 CriticalSectionScoped cs(&volume_settings_critsect_);
niklase@google.com470e71d2011-07-07 08:21:25 +00002407 return _mute;
2408}
2409
2410int
2411Channel::SetOutputVolumePan(float left, float right)
2412{
wu@webrtc.org63420662013-10-17 18:28:55 +00002413 CriticalSectionScoped cs(&volume_settings_critsect_);
niklase@google.com470e71d2011-07-07 08:21:25 +00002414 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId,_channelId),
2415 "Channel::SetOutputVolumePan()");
2416 _panLeft = left;
2417 _panRight = right;
2418 return 0;
2419}
2420
2421int
2422Channel::GetOutputVolumePan(float& left, float& right) const
2423{
wu@webrtc.org63420662013-10-17 18:28:55 +00002424 CriticalSectionScoped cs(&volume_settings_critsect_);
niklase@google.com470e71d2011-07-07 08:21:25 +00002425 left = _panLeft;
2426 right = _panRight;
2427 WEBRTC_TRACE(kTraceStateInfo, kTraceVoice,
2428 VoEId(_instanceId,_channelId),
2429 "GetOutputVolumePan() => left=%3.2f, right=%3.2f", left, right);
2430 return 0;
2431}
2432
2433int
2434Channel::SetChannelOutputVolumeScaling(float scaling)
2435{
wu@webrtc.org63420662013-10-17 18:28:55 +00002436 CriticalSectionScoped cs(&volume_settings_critsect_);
niklase@google.com470e71d2011-07-07 08:21:25 +00002437 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId,_channelId),
2438 "Channel::SetChannelOutputVolumeScaling()");
2439 _outputGain = scaling;
2440 return 0;
2441}
2442
2443int
2444Channel::GetChannelOutputVolumeScaling(float& scaling) const
2445{
wu@webrtc.org63420662013-10-17 18:28:55 +00002446 CriticalSectionScoped cs(&volume_settings_critsect_);
niklase@google.com470e71d2011-07-07 08:21:25 +00002447 scaling = _outputGain;
2448 WEBRTC_TRACE(kTraceStateInfo, kTraceVoice,
2449 VoEId(_instanceId,_channelId),
2450 "GetChannelOutputVolumeScaling() => scaling=%3.2f", scaling);
2451 return 0;
2452}
2453
niklase@google.com470e71d2011-07-07 08:21:25 +00002454int Channel::SendTelephoneEventOutband(unsigned char eventCode,
wu@webrtc.org822fbd82013-08-15 23:38:54 +00002455 int lengthMs, int attenuationDb,
2456 bool playDtmfEvent)
niklase@google.com470e71d2011-07-07 08:21:25 +00002457{
2458 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId, _channelId),
2459 "Channel::SendTelephoneEventOutband(..., playDtmfEvent=%d)",
2460 playDtmfEvent);
2461
2462 _playOutbandDtmfEvent = playDtmfEvent;
2463
pwestin@webrtc.org2853dde2012-05-11 11:08:54 +00002464 if (_rtpRtcpModule->SendTelephoneEventOutband(eventCode, lengthMs,
niklase@google.com470e71d2011-07-07 08:21:25 +00002465 attenuationDb) != 0)
2466 {
2467 _engineStatisticsPtr->SetLastError(
2468 VE_SEND_DTMF_FAILED,
2469 kTraceWarning,
2470 "SendTelephoneEventOutband() failed to send event");
2471 return -1;
2472 }
2473 return 0;
2474}
2475
2476int Channel::SendTelephoneEventInband(unsigned char eventCode,
2477 int lengthMs,
2478 int attenuationDb,
2479 bool playDtmfEvent)
2480{
2481 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId, _channelId),
2482 "Channel::SendTelephoneEventInband(..., playDtmfEvent=%d)",
2483 playDtmfEvent);
2484
2485 _playInbandDtmfEvent = playDtmfEvent;
2486 _inbandDtmfQueue.AddDtmf(eventCode, lengthMs, attenuationDb);
2487
2488 return 0;
2489}
2490
2491int
niklase@google.com470e71d2011-07-07 08:21:25 +00002492Channel::SetSendTelephoneEventPayloadType(unsigned char type)
2493{
2494 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId,_channelId),
2495 "Channel::SetSendTelephoneEventPayloadType()");
andrew@webrtc.orgf81f9f82011-08-19 22:56:22 +00002496 if (type > 127)
niklase@google.com470e71d2011-07-07 08:21:25 +00002497 {
2498 _engineStatisticsPtr->SetLastError(
2499 VE_INVALID_ARGUMENT, kTraceError,
2500 "SetSendTelephoneEventPayloadType() invalid type");
2501 return -1;
2502 }
pbos@webrtc.org5b10d8f2013-07-11 15:50:07 +00002503 CodecInst codec = {};
pwestin@webrtc.org1da1ce02011-10-13 15:19:55 +00002504 codec.plfreq = 8000;
2505 codec.pltype = type;
2506 memcpy(codec.plname, "telephone-event", 16);
pwestin@webrtc.org2853dde2012-05-11 11:08:54 +00002507 if (_rtpRtcpModule->RegisterSendPayload(codec) != 0)
niklase@google.com470e71d2011-07-07 08:21:25 +00002508 {
henrika@webrtc.org4392d5f2013-04-17 07:34:25 +00002509 _rtpRtcpModule->DeRegisterSendPayload(codec.pltype);
2510 if (_rtpRtcpModule->RegisterSendPayload(codec) != 0) {
2511 _engineStatisticsPtr->SetLastError(
2512 VE_RTP_RTCP_MODULE_ERROR, kTraceError,
2513 "SetSendTelephoneEventPayloadType() failed to register send"
2514 "payload type");
2515 return -1;
2516 }
niklase@google.com470e71d2011-07-07 08:21:25 +00002517 }
2518 _sendTelephoneEventPayloadType = type;
2519 return 0;
2520}
2521
2522int
2523Channel::GetSendTelephoneEventPayloadType(unsigned char& type)
2524{
2525 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId,_channelId),
2526 "Channel::GetSendTelephoneEventPayloadType()");
2527 type = _sendTelephoneEventPayloadType;
2528 WEBRTC_TRACE(kTraceStateInfo, kTraceVoice,
2529 VoEId(_instanceId,_channelId),
2530 "GetSendTelephoneEventPayloadType() => type=%u", type);
2531 return 0;
2532}
2533
niklase@google.com470e71d2011-07-07 08:21:25 +00002534int
2535Channel::UpdateRxVadDetection(AudioFrame& audioFrame)
2536{
2537 WEBRTC_TRACE(kTraceStream, kTraceVoice, VoEId(_instanceId,_channelId),
2538 "Channel::UpdateRxVadDetection()");
2539
2540 int vadDecision = 1;
2541
andrew@webrtc.org63a50982012-05-02 23:56:37 +00002542 vadDecision = (audioFrame.vad_activity_ == AudioFrame::kVadActive)? 1 : 0;
niklase@google.com470e71d2011-07-07 08:21:25 +00002543
2544 if ((vadDecision != _oldVadDecision) && _rxVadObserverPtr)
2545 {
2546 OnRxVadDetected(vadDecision);
2547 _oldVadDecision = vadDecision;
2548 }
2549
2550 WEBRTC_TRACE(kTraceStream, kTraceVoice, VoEId(_instanceId,_channelId),
2551 "Channel::UpdateRxVadDetection() => vadDecision=%d",
2552 vadDecision);
2553 return 0;
2554}
2555
2556int
2557Channel::RegisterRxVadObserver(VoERxVadCallback &observer)
2558{
2559 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId,_channelId),
2560 "Channel::RegisterRxVadObserver()");
mflodman@webrtc.org9a065d12012-03-07 08:12:21 +00002561 CriticalSectionScoped cs(&_callbackCritSect);
niklase@google.com470e71d2011-07-07 08:21:25 +00002562
2563 if (_rxVadObserverPtr)
2564 {
2565 _engineStatisticsPtr->SetLastError(
2566 VE_INVALID_OPERATION, kTraceError,
2567 "RegisterRxVadObserver() observer already enabled");
2568 return -1;
2569 }
niklase@google.com470e71d2011-07-07 08:21:25 +00002570 _rxVadObserverPtr = &observer;
2571 _RxVadDetection = true;
2572 return 0;
2573}
2574
2575int
2576Channel::DeRegisterRxVadObserver()
2577{
2578 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId,_channelId),
2579 "Channel::DeRegisterRxVadObserver()");
mflodman@webrtc.org9a065d12012-03-07 08:12:21 +00002580 CriticalSectionScoped cs(&_callbackCritSect);
niklase@google.com470e71d2011-07-07 08:21:25 +00002581
2582 if (!_rxVadObserverPtr)
2583 {
2584 _engineStatisticsPtr->SetLastError(
2585 VE_INVALID_OPERATION, kTraceWarning,
2586 "DeRegisterRxVadObserver() observer already disabled");
2587 return 0;
2588 }
2589 _rxVadObserverPtr = NULL;
2590 _RxVadDetection = false;
2591 return 0;
2592}
2593
2594int
2595Channel::VoiceActivityIndicator(int &activity)
2596{
2597 activity = _sendFrameType;
2598
2599 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId,_channelId),
andrew@webrtc.org6c264cc2013-10-04 17:54:09 +00002600 "Channel::VoiceActivityIndicator(indicator=%d)", activity);
niklase@google.com470e71d2011-07-07 08:21:25 +00002601 return 0;
2602}
2603
2604#ifdef WEBRTC_VOICE_ENGINE_AGC
2605
2606int
pbos@webrtc.org92135212013-05-14 08:31:39 +00002607Channel::SetRxAgcStatus(bool enable, AgcModes mode)
niklase@google.com470e71d2011-07-07 08:21:25 +00002608{
2609 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId,_channelId),
2610 "Channel::SetRxAgcStatus(enable=%d, mode=%d)",
2611 (int)enable, (int)mode);
2612
andrew@webrtc.org6c264cc2013-10-04 17:54:09 +00002613 GainControl::Mode agcMode = kDefaultRxAgcMode;
niklase@google.com470e71d2011-07-07 08:21:25 +00002614 switch (mode)
2615 {
2616 case kAgcDefault:
niklase@google.com470e71d2011-07-07 08:21:25 +00002617 break;
2618 case kAgcUnchanged:
andrew@webrtc.orgf3930e92013-09-18 22:37:32 +00002619 agcMode = rx_audioproc_->gain_control()->mode();
niklase@google.com470e71d2011-07-07 08:21:25 +00002620 break;
2621 case kAgcFixedDigital:
2622 agcMode = GainControl::kFixedDigital;
2623 break;
2624 case kAgcAdaptiveDigital:
2625 agcMode =GainControl::kAdaptiveDigital;
2626 break;
2627 default:
2628 _engineStatisticsPtr->SetLastError(
2629 VE_INVALID_ARGUMENT, kTraceError,
2630 "SetRxAgcStatus() invalid Agc mode");
2631 return -1;
2632 }
2633
andrew@webrtc.orgf3930e92013-09-18 22:37:32 +00002634 if (rx_audioproc_->gain_control()->set_mode(agcMode) != 0)
niklase@google.com470e71d2011-07-07 08:21:25 +00002635 {
2636 _engineStatisticsPtr->SetLastError(
2637 VE_APM_ERROR, kTraceError,
2638 "SetRxAgcStatus() failed to set Agc mode");
2639 return -1;
2640 }
andrew@webrtc.orgf3930e92013-09-18 22:37:32 +00002641 if (rx_audioproc_->gain_control()->Enable(enable) != 0)
niklase@google.com470e71d2011-07-07 08:21:25 +00002642 {
2643 _engineStatisticsPtr->SetLastError(
2644 VE_APM_ERROR, kTraceError,
2645 "SetRxAgcStatus() failed to set Agc state");
2646 return -1;
2647 }
2648
2649 _rxAgcIsEnabled = enable;
henrika@webrtc.org944cbeb2014-03-18 10:32:33 +00002650 channel_state_.SetRxApmIsEnabled(_rxAgcIsEnabled || _rxNsIsEnabled);
niklase@google.com470e71d2011-07-07 08:21:25 +00002651
2652 return 0;
2653}
2654
2655int
2656Channel::GetRxAgcStatus(bool& enabled, AgcModes& mode)
2657{
2658 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId,_channelId),
2659 "Channel::GetRxAgcStatus(enable=?, mode=?)");
2660
andrew@webrtc.orgf3930e92013-09-18 22:37:32 +00002661 bool enable = rx_audioproc_->gain_control()->is_enabled();
niklase@google.com470e71d2011-07-07 08:21:25 +00002662 GainControl::Mode agcMode =
andrew@webrtc.orgf3930e92013-09-18 22:37:32 +00002663 rx_audioproc_->gain_control()->mode();
niklase@google.com470e71d2011-07-07 08:21:25 +00002664
2665 enabled = enable;
2666
2667 switch (agcMode)
2668 {
2669 case GainControl::kFixedDigital:
2670 mode = kAgcFixedDigital;
2671 break;
2672 case GainControl::kAdaptiveDigital:
2673 mode = kAgcAdaptiveDigital;
2674 break;
2675 default:
2676 _engineStatisticsPtr->SetLastError(
2677 VE_APM_ERROR, kTraceError,
2678 "GetRxAgcStatus() invalid Agc mode");
2679 return -1;
2680 }
2681
2682 return 0;
2683}
2684
2685int
pbos@webrtc.org92135212013-05-14 08:31:39 +00002686Channel::SetRxAgcConfig(AgcConfig config)
niklase@google.com470e71d2011-07-07 08:21:25 +00002687{
2688 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId,_channelId),
2689 "Channel::SetRxAgcConfig()");
2690
andrew@webrtc.orgf3930e92013-09-18 22:37:32 +00002691 if (rx_audioproc_->gain_control()->set_target_level_dbfs(
niklase@google.com470e71d2011-07-07 08:21:25 +00002692 config.targetLeveldBOv) != 0)
2693 {
2694 _engineStatisticsPtr->SetLastError(
2695 VE_APM_ERROR, kTraceError,
2696 "SetRxAgcConfig() failed to set target peak |level|"
2697 "(or envelope) of the Agc");
2698 return -1;
2699 }
andrew@webrtc.orgf3930e92013-09-18 22:37:32 +00002700 if (rx_audioproc_->gain_control()->set_compression_gain_db(
niklase@google.com470e71d2011-07-07 08:21:25 +00002701 config.digitalCompressionGaindB) != 0)
2702 {
2703 _engineStatisticsPtr->SetLastError(
2704 VE_APM_ERROR, kTraceError,
2705 "SetRxAgcConfig() failed to set the range in |gain| the"
2706 " digital compression stage may apply");
2707 return -1;
2708 }
andrew@webrtc.orgf3930e92013-09-18 22:37:32 +00002709 if (rx_audioproc_->gain_control()->enable_limiter(
niklase@google.com470e71d2011-07-07 08:21:25 +00002710 config.limiterEnable) != 0)
2711 {
2712 _engineStatisticsPtr->SetLastError(
2713 VE_APM_ERROR, kTraceError,
2714 "SetRxAgcConfig() failed to set hard limiter to the signal");
2715 return -1;
2716 }
2717
2718 return 0;
2719}
2720
2721int
2722Channel::GetRxAgcConfig(AgcConfig& config)
2723{
2724 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId,_channelId),
2725 "Channel::GetRxAgcConfig(config=%?)");
2726
2727 config.targetLeveldBOv =
andrew@webrtc.orgf3930e92013-09-18 22:37:32 +00002728 rx_audioproc_->gain_control()->target_level_dbfs();
niklase@google.com470e71d2011-07-07 08:21:25 +00002729 config.digitalCompressionGaindB =
andrew@webrtc.orgf3930e92013-09-18 22:37:32 +00002730 rx_audioproc_->gain_control()->compression_gain_db();
niklase@google.com470e71d2011-07-07 08:21:25 +00002731 config.limiterEnable =
andrew@webrtc.orgf3930e92013-09-18 22:37:32 +00002732 rx_audioproc_->gain_control()->is_limiter_enabled();
niklase@google.com470e71d2011-07-07 08:21:25 +00002733
2734 WEBRTC_TRACE(kTraceStateInfo, kTraceVoice,
2735 VoEId(_instanceId,_channelId), "GetRxAgcConfig() => "
2736 "targetLeveldBOv=%u, digitalCompressionGaindB=%u,"
2737 " limiterEnable=%d",
2738 config.targetLeveldBOv,
2739 config.digitalCompressionGaindB,
2740 config.limiterEnable);
2741
2742 return 0;
2743}
2744
2745#endif // #ifdef WEBRTC_VOICE_ENGINE_AGC
2746
2747#ifdef WEBRTC_VOICE_ENGINE_NR
2748
2749int
pbos@webrtc.org92135212013-05-14 08:31:39 +00002750Channel::SetRxNsStatus(bool enable, NsModes mode)
niklase@google.com470e71d2011-07-07 08:21:25 +00002751{
2752 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId,_channelId),
2753 "Channel::SetRxNsStatus(enable=%d, mode=%d)",
2754 (int)enable, (int)mode);
2755
andrew@webrtc.org6c264cc2013-10-04 17:54:09 +00002756 NoiseSuppression::Level nsLevel = kDefaultNsMode;
niklase@google.com470e71d2011-07-07 08:21:25 +00002757 switch (mode)
2758 {
2759
2760 case kNsDefault:
niklase@google.com470e71d2011-07-07 08:21:25 +00002761 break;
2762 case kNsUnchanged:
andrew@webrtc.orgf3930e92013-09-18 22:37:32 +00002763 nsLevel = rx_audioproc_->noise_suppression()->level();
niklase@google.com470e71d2011-07-07 08:21:25 +00002764 break;
2765 case kNsConference:
2766 nsLevel = NoiseSuppression::kHigh;
2767 break;
2768 case kNsLowSuppression:
2769 nsLevel = NoiseSuppression::kLow;
2770 break;
2771 case kNsModerateSuppression:
2772 nsLevel = NoiseSuppression::kModerate;
2773 break;
2774 case kNsHighSuppression:
2775 nsLevel = NoiseSuppression::kHigh;
2776 break;
2777 case kNsVeryHighSuppression:
2778 nsLevel = NoiseSuppression::kVeryHigh;
2779 break;
niklase@google.com470e71d2011-07-07 08:21:25 +00002780 }
2781
andrew@webrtc.orgf3930e92013-09-18 22:37:32 +00002782 if (rx_audioproc_->noise_suppression()->set_level(nsLevel)
niklase@google.com470e71d2011-07-07 08:21:25 +00002783 != 0)
2784 {
2785 _engineStatisticsPtr->SetLastError(
2786 VE_APM_ERROR, kTraceError,
andrew@webrtc.org6c264cc2013-10-04 17:54:09 +00002787 "SetRxNsStatus() failed to set NS level");
niklase@google.com470e71d2011-07-07 08:21:25 +00002788 return -1;
2789 }
andrew@webrtc.orgf3930e92013-09-18 22:37:32 +00002790 if (rx_audioproc_->noise_suppression()->Enable(enable) != 0)
niklase@google.com470e71d2011-07-07 08:21:25 +00002791 {
2792 _engineStatisticsPtr->SetLastError(
2793 VE_APM_ERROR, kTraceError,
andrew@webrtc.org6c264cc2013-10-04 17:54:09 +00002794 "SetRxNsStatus() failed to set NS state");
niklase@google.com470e71d2011-07-07 08:21:25 +00002795 return -1;
2796 }
2797
2798 _rxNsIsEnabled = enable;
henrika@webrtc.org944cbeb2014-03-18 10:32:33 +00002799 channel_state_.SetRxApmIsEnabled(_rxAgcIsEnabled || _rxNsIsEnabled);
niklase@google.com470e71d2011-07-07 08:21:25 +00002800
2801 return 0;
2802}
2803
2804int
2805Channel::GetRxNsStatus(bool& enabled, NsModes& mode)
2806{
2807 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId,_channelId),
2808 "Channel::GetRxNsStatus(enable=?, mode=?)");
2809
2810 bool enable =
andrew@webrtc.orgf3930e92013-09-18 22:37:32 +00002811 rx_audioproc_->noise_suppression()->is_enabled();
niklase@google.com470e71d2011-07-07 08:21:25 +00002812 NoiseSuppression::Level ncLevel =
andrew@webrtc.orgf3930e92013-09-18 22:37:32 +00002813 rx_audioproc_->noise_suppression()->level();
niklase@google.com470e71d2011-07-07 08:21:25 +00002814
2815 enabled = enable;
2816
2817 switch (ncLevel)
2818 {
2819 case NoiseSuppression::kLow:
2820 mode = kNsLowSuppression;
2821 break;
2822 case NoiseSuppression::kModerate:
2823 mode = kNsModerateSuppression;
2824 break;
2825 case NoiseSuppression::kHigh:
2826 mode = kNsHighSuppression;
2827 break;
2828 case NoiseSuppression::kVeryHigh:
2829 mode = kNsVeryHighSuppression;
2830 break;
niklase@google.com470e71d2011-07-07 08:21:25 +00002831 }
2832
2833 WEBRTC_TRACE(kTraceStateInfo, kTraceVoice,
2834 VoEId(_instanceId,_channelId),
2835 "GetRxNsStatus() => enabled=%d, mode=%d", enabled, mode);
2836 return 0;
2837}
2838
2839#endif // #ifdef WEBRTC_VOICE_ENGINE_NR
2840
2841int
niklase@google.com470e71d2011-07-07 08:21:25 +00002842Channel::SetLocalSSRC(unsigned int ssrc)
2843{
2844 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId, _channelId),
2845 "Channel::SetLocalSSRC()");
henrika@webrtc.org944cbeb2014-03-18 10:32:33 +00002846 if (channel_state_.Get().sending)
niklase@google.com470e71d2011-07-07 08:21:25 +00002847 {
2848 _engineStatisticsPtr->SetLastError(
2849 VE_ALREADY_SENDING, kTraceError,
2850 "SetLocalSSRC() already sending");
2851 return -1;
2852 }
stefan@webrtc.orgef927552014-06-05 08:25:29 +00002853 _rtpRtcpModule->SetSSRC(ssrc);
niklase@google.com470e71d2011-07-07 08:21:25 +00002854 return 0;
2855}
2856
2857int
2858Channel::GetLocalSSRC(unsigned int& ssrc)
2859{
pwestin@webrtc.org2853dde2012-05-11 11:08:54 +00002860 ssrc = _rtpRtcpModule->SSRC();
niklase@google.com470e71d2011-07-07 08:21:25 +00002861 WEBRTC_TRACE(kTraceStateInfo, kTraceVoice,
2862 VoEId(_instanceId,_channelId),
2863 "GetLocalSSRC() => ssrc=%lu", ssrc);
2864 return 0;
2865}
2866
2867int
2868Channel::GetRemoteSSRC(unsigned int& ssrc)
2869{
wu@webrtc.org822fbd82013-08-15 23:38:54 +00002870 ssrc = rtp_receiver_->SSRC();
niklase@google.com470e71d2011-07-07 08:21:25 +00002871 WEBRTC_TRACE(kTraceStateInfo, kTraceVoice,
2872 VoEId(_instanceId,_channelId),
2873 "GetRemoteSSRC() => ssrc=%lu", ssrc);
2874 return 0;
2875}
2876
wu@webrtc.orgebdb0e32014-03-06 23:49:08 +00002877int Channel::SetSendAudioLevelIndicationStatus(bool enable, unsigned char id) {
andrew@webrtc.orgf3930e92013-09-18 22:37:32 +00002878 _includeAudioLevelIndication = enable;
wu@webrtc.orgebdb0e32014-03-06 23:49:08 +00002879 return SetSendRtpHeaderExtension(enable, kRtpExtensionAudioLevel, id);
niklase@google.com470e71d2011-07-07 08:21:25 +00002880}
andrew@webrtc.orgf3930e92013-09-18 22:37:32 +00002881
wu@webrtc.org93fd25c2014-04-24 20:33:08 +00002882int Channel::SetReceiveAudioLevelIndicationStatus(bool enable,
2883 unsigned char id) {
2884 rtp_header_parser_->DeregisterRtpHeaderExtension(
2885 kRtpExtensionAudioLevel);
2886 if (enable && !rtp_header_parser_->RegisterRtpHeaderExtension(
2887 kRtpExtensionAudioLevel, id)) {
2888 return -1;
2889 }
2890 return 0;
2891}
2892
wu@webrtc.orgebdb0e32014-03-06 23:49:08 +00002893int Channel::SetSendAbsoluteSenderTimeStatus(bool enable, unsigned char id) {
2894 return SetSendRtpHeaderExtension(enable, kRtpExtensionAbsoluteSendTime, id);
2895}
2896
2897int Channel::SetReceiveAbsoluteSenderTimeStatus(bool enable, unsigned char id) {
2898 rtp_header_parser_->DeregisterRtpHeaderExtension(
2899 kRtpExtensionAbsoluteSendTime);
solenberg@webrtc.orgb1f50102014-03-24 10:38:25 +00002900 if (enable && !rtp_header_parser_->RegisterRtpHeaderExtension(
2901 kRtpExtensionAbsoluteSendTime, id)) {
2902 return -1;
wu@webrtc.orgebdb0e32014-03-06 23:49:08 +00002903 }
2904 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +00002905}
2906
2907int
2908Channel::SetRTCPStatus(bool enable)
2909{
2910 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId,_channelId),
2911 "Channel::SetRTCPStatus()");
pwestin@webrtc.org2853dde2012-05-11 11:08:54 +00002912 if (_rtpRtcpModule->SetRTCPStatus(enable ?
niklase@google.com470e71d2011-07-07 08:21:25 +00002913 kRtcpCompound : kRtcpOff) != 0)
2914 {
2915 _engineStatisticsPtr->SetLastError(
2916 VE_RTP_RTCP_MODULE_ERROR, kTraceError,
2917 "SetRTCPStatus() failed to set RTCP status");
2918 return -1;
2919 }
2920 return 0;
2921}
2922
2923int
2924Channel::GetRTCPStatus(bool& enabled)
2925{
pwestin@webrtc.org2853dde2012-05-11 11:08:54 +00002926 RTCPMethod method = _rtpRtcpModule->RTCP();
niklase@google.com470e71d2011-07-07 08:21:25 +00002927 enabled = (method != kRtcpOff);
2928 WEBRTC_TRACE(kTraceStateInfo, kTraceVoice,
2929 VoEId(_instanceId,_channelId),
2930 "GetRTCPStatus() => enabled=%d", enabled);
2931 return 0;
2932}
2933
2934int
2935Channel::SetRTCP_CNAME(const char cName[256])
2936{
2937 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId, _channelId),
2938 "Channel::SetRTCP_CNAME()");
pwestin@webrtc.org2853dde2012-05-11 11:08:54 +00002939 if (_rtpRtcpModule->SetCNAME(cName) != 0)
niklase@google.com470e71d2011-07-07 08:21:25 +00002940 {
2941 _engineStatisticsPtr->SetLastError(
2942 VE_RTP_RTCP_MODULE_ERROR, kTraceError,
2943 "SetRTCP_CNAME() failed to set RTCP CNAME");
2944 return -1;
2945 }
2946 return 0;
2947}
2948
2949int
niklase@google.com470e71d2011-07-07 08:21:25 +00002950Channel::GetRemoteRTCP_CNAME(char cName[256])
2951{
2952 if (cName == NULL)
2953 {
2954 _engineStatisticsPtr->SetLastError(
2955 VE_INVALID_ARGUMENT, kTraceError,
2956 "GetRemoteRTCP_CNAME() invalid CNAME input buffer");
2957 return -1;
2958 }
leozwang@webrtc.org813e4b02012-03-01 18:34:25 +00002959 char cname[RTCP_CNAME_SIZE];
wu@webrtc.org822fbd82013-08-15 23:38:54 +00002960 const uint32_t remoteSSRC = rtp_receiver_->SSRC();
pwestin@webrtc.org2853dde2012-05-11 11:08:54 +00002961 if (_rtpRtcpModule->RemoteCNAME(remoteSSRC, cname) != 0)
niklase@google.com470e71d2011-07-07 08:21:25 +00002962 {
2963 _engineStatisticsPtr->SetLastError(
2964 VE_CANNOT_RETRIEVE_CNAME, kTraceError,
2965 "GetRemoteRTCP_CNAME() failed to retrieve remote RTCP CNAME");
2966 return -1;
2967 }
2968 strcpy(cName, cname);
2969 WEBRTC_TRACE(kTraceStateInfo, kTraceVoice,
2970 VoEId(_instanceId, _channelId),
2971 "GetRemoteRTCP_CNAME() => cName=%s", cName);
2972 return 0;
2973}
2974
2975int
2976Channel::GetRemoteRTCPData(
2977 unsigned int& NTPHigh,
2978 unsigned int& NTPLow,
2979 unsigned int& timestamp,
2980 unsigned int& playoutTimestamp,
2981 unsigned int* jitter,
2982 unsigned short* fractionLost)
2983{
2984 // --- Information from sender info in received Sender Reports
2985
2986 RTCPSenderInfo senderInfo;
pwestin@webrtc.org2853dde2012-05-11 11:08:54 +00002987 if (_rtpRtcpModule->RemoteRTCPStat(&senderInfo) != 0)
niklase@google.com470e71d2011-07-07 08:21:25 +00002988 {
2989 _engineStatisticsPtr->SetLastError(
2990 VE_RTP_RTCP_MODULE_ERROR, kTraceError,
wu@webrtc.orgfcd12b32011-09-15 20:49:50 +00002991 "GetRemoteRTCPData() failed to retrieve sender info for remote "
niklase@google.com470e71d2011-07-07 08:21:25 +00002992 "side");
2993 return -1;
2994 }
2995
2996 // We only utilize 12 out of 20 bytes in the sender info (ignores packet
2997 // and octet count)
2998 NTPHigh = senderInfo.NTPseconds;
2999 NTPLow = senderInfo.NTPfraction;
3000 timestamp = senderInfo.RTPtimeStamp;
3001
3002 WEBRTC_TRACE(kTraceStateInfo, kTraceVoice,
3003 VoEId(_instanceId, _channelId),
3004 "GetRemoteRTCPData() => NTPHigh=%lu, NTPLow=%lu, "
3005 "timestamp=%lu",
3006 NTPHigh, NTPLow, timestamp);
3007
3008 // --- Locally derived information
3009
3010 // This value is updated on each incoming RTCP packet (0 when no packet
3011 // has been received)
pwestin@webrtc.org1de01352013-04-11 20:23:35 +00003012 playoutTimestamp = playout_timestamp_rtcp_;
niklase@google.com470e71d2011-07-07 08:21:25 +00003013
3014 WEBRTC_TRACE(kTraceStateInfo, kTraceVoice,
3015 VoEId(_instanceId, _channelId),
3016 "GetRemoteRTCPData() => playoutTimestamp=%lu",
pwestin@webrtc.org1de01352013-04-11 20:23:35 +00003017 playout_timestamp_rtcp_);
niklase@google.com470e71d2011-07-07 08:21:25 +00003018
3019 if (NULL != jitter || NULL != fractionLost)
3020 {
perkj@webrtc.orgce5990c2012-01-11 13:00:08 +00003021 // Get all RTCP receiver report blocks that have been received on this
3022 // channel. If we receive RTP packets from a remote source we know the
3023 // remote SSRC and use the report block from him.
3024 // Otherwise use the first report block.
3025 std::vector<RTCPReportBlock> remote_stats;
pwestin@webrtc.org2853dde2012-05-11 11:08:54 +00003026 if (_rtpRtcpModule->RemoteRTCPStat(&remote_stats) != 0 ||
perkj@webrtc.orgce5990c2012-01-11 13:00:08 +00003027 remote_stats.empty()) {
3028 WEBRTC_TRACE(kTraceWarning, kTraceVoice,
3029 VoEId(_instanceId, _channelId),
3030 "GetRemoteRTCPData() failed to measure statistics due"
3031 " to lack of received RTP and/or RTCP packets");
3032 return -1;
niklase@google.com470e71d2011-07-07 08:21:25 +00003033 }
perkj@webrtc.orgce5990c2012-01-11 13:00:08 +00003034
wu@webrtc.org822fbd82013-08-15 23:38:54 +00003035 uint32_t remoteSSRC = rtp_receiver_->SSRC();
perkj@webrtc.orgce5990c2012-01-11 13:00:08 +00003036 std::vector<RTCPReportBlock>::const_iterator it = remote_stats.begin();
3037 for (; it != remote_stats.end(); ++it) {
3038 if (it->remoteSSRC == remoteSSRC)
3039 break;
niklase@google.com470e71d2011-07-07 08:21:25 +00003040 }
perkj@webrtc.orgce5990c2012-01-11 13:00:08 +00003041
3042 if (it == remote_stats.end()) {
3043 // If we have not received any RTCP packets from this SSRC it probably
3044 // means that we have not received any RTP packets.
3045 // Use the first received report block instead.
3046 it = remote_stats.begin();
3047 remoteSSRC = it->remoteSSRC;
niklase@google.com470e71d2011-07-07 08:21:25 +00003048 }
perkj@webrtc.orgce5990c2012-01-11 13:00:08 +00003049
xians@webrtc.org79af7342012-01-31 12:22:14 +00003050 if (jitter) {
3051 *jitter = it->jitter;
3052 WEBRTC_TRACE(kTraceStateInfo, kTraceVoice,
3053 VoEId(_instanceId, _channelId),
3054 "GetRemoteRTCPData() => jitter = %lu", *jitter);
3055 }
perkj@webrtc.orgce5990c2012-01-11 13:00:08 +00003056
xians@webrtc.org79af7342012-01-31 12:22:14 +00003057 if (fractionLost) {
3058 *fractionLost = it->fractionLost;
3059 WEBRTC_TRACE(kTraceStateInfo, kTraceVoice,
3060 VoEId(_instanceId, _channelId),
3061 "GetRemoteRTCPData() => fractionLost = %lu",
3062 *fractionLost);
3063 }
niklase@google.com470e71d2011-07-07 08:21:25 +00003064 }
3065 return 0;
3066}
3067
3068int
pbos@webrtc.org92135212013-05-14 08:31:39 +00003069Channel::SendApplicationDefinedRTCPPacket(unsigned char subType,
niklase@google.com470e71d2011-07-07 08:21:25 +00003070 unsigned int name,
3071 const char* data,
3072 unsigned short dataLengthInBytes)
3073{
3074 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId, _channelId),
3075 "Channel::SendApplicationDefinedRTCPPacket()");
henrika@webrtc.org944cbeb2014-03-18 10:32:33 +00003076 if (!channel_state_.Get().sending)
niklase@google.com470e71d2011-07-07 08:21:25 +00003077 {
3078 _engineStatisticsPtr->SetLastError(
3079 VE_NOT_SENDING, kTraceError,
3080 "SendApplicationDefinedRTCPPacket() not sending");
3081 return -1;
3082 }
3083 if (NULL == data)
3084 {
3085 _engineStatisticsPtr->SetLastError(
3086 VE_INVALID_ARGUMENT, kTraceError,
3087 "SendApplicationDefinedRTCPPacket() invalid data value");
3088 return -1;
3089 }
3090 if (dataLengthInBytes % 4 != 0)
3091 {
3092 _engineStatisticsPtr->SetLastError(
3093 VE_INVALID_ARGUMENT, kTraceError,
3094 "SendApplicationDefinedRTCPPacket() invalid length value");
3095 return -1;
3096 }
pwestin@webrtc.org2853dde2012-05-11 11:08:54 +00003097 RTCPMethod status = _rtpRtcpModule->RTCP();
niklase@google.com470e71d2011-07-07 08:21:25 +00003098 if (status == kRtcpOff)
3099 {
3100 _engineStatisticsPtr->SetLastError(
3101 VE_RTCP_ERROR, kTraceError,
3102 "SendApplicationDefinedRTCPPacket() RTCP is disabled");
3103 return -1;
3104 }
3105
3106 // Create and schedule the RTCP APP packet for transmission
pwestin@webrtc.org2853dde2012-05-11 11:08:54 +00003107 if (_rtpRtcpModule->SetRTCPApplicationSpecificData(
niklase@google.com470e71d2011-07-07 08:21:25 +00003108 subType,
3109 name,
3110 (const unsigned char*) data,
3111 dataLengthInBytes) != 0)
3112 {
3113 _engineStatisticsPtr->SetLastError(
3114 VE_SEND_ERROR, kTraceError,
3115 "SendApplicationDefinedRTCPPacket() failed to send RTCP packet");
3116 return -1;
3117 }
3118 return 0;
3119}
3120
3121int
3122Channel::GetRTPStatistics(
3123 unsigned int& averageJitterMs,
3124 unsigned int& maxJitterMs,
3125 unsigned int& discardedPackets)
3126{
niklase@google.com470e71d2011-07-07 08:21:25 +00003127 // The jitter statistics is updated for each received RTP packet and is
3128 // based on received packets.
sprang@webrtc.org54ae4ff2013-12-19 13:26:02 +00003129 if (_rtpRtcpModule->RTCP() == kRtcpOff) {
3130 // If RTCP is off, there is no timed thread in the RTCP module regularly
3131 // generating new stats, trigger the update manually here instead.
3132 StreamStatistician* statistician =
3133 rtp_receive_statistics_->GetStatistician(rtp_receiver_->SSRC());
3134 if (statistician) {
3135 // Don't use returned statistics, use data from proxy instead so that
3136 // max jitter can be fetched atomically.
3137 RtcpStatistics s;
3138 statistician->GetStatistics(&s, true);
3139 }
niklase@google.com470e71d2011-07-07 08:21:25 +00003140 }
3141
sprang@webrtc.org54ae4ff2013-12-19 13:26:02 +00003142 ChannelStatistics stats = statistics_proxy_->GetStats();
andrew@webrtc.orgeb524d92013-09-23 23:02:24 +00003143 const int32_t playoutFrequency = audio_coding_->PlayoutFrequency();
sprang@webrtc.org54ae4ff2013-12-19 13:26:02 +00003144 if (playoutFrequency > 0) {
3145 // Scale RTP statistics given the current playout frequency
3146 maxJitterMs = stats.max_jitter / (playoutFrequency / 1000);
3147 averageJitterMs = stats.rtcp.jitter / (playoutFrequency / 1000);
niklase@google.com470e71d2011-07-07 08:21:25 +00003148 }
3149
3150 discardedPackets = _numberOfDiscardedPackets;
3151
3152 WEBRTC_TRACE(kTraceStateInfo, kTraceVoice,
3153 VoEId(_instanceId, _channelId),
3154 "GetRTPStatistics() => averageJitterMs = %lu, maxJitterMs = %lu,"
wu@webrtc.orgfcd12b32011-09-15 20:49:50 +00003155 " discardedPackets = %lu)",
niklase@google.com470e71d2011-07-07 08:21:25 +00003156 averageJitterMs, maxJitterMs, discardedPackets);
3157 return 0;
3158}
3159
henrika@webrtc.org8a2fc882012-08-22 08:53:55 +00003160int Channel::GetRemoteRTCPReportBlocks(
3161 std::vector<ReportBlock>* report_blocks) {
3162 if (report_blocks == NULL) {
3163 _engineStatisticsPtr->SetLastError(VE_INVALID_ARGUMENT, kTraceError,
3164 "GetRemoteRTCPReportBlock()s invalid report_blocks.");
3165 return -1;
3166 }
3167
3168 // Get the report blocks from the latest received RTCP Sender or Receiver
3169 // Report. Each element in the vector contains the sender's SSRC and a
3170 // report block according to RFC 3550.
3171 std::vector<RTCPReportBlock> rtcp_report_blocks;
3172 if (_rtpRtcpModule->RemoteRTCPStat(&rtcp_report_blocks) != 0) {
3173 _engineStatisticsPtr->SetLastError(VE_RTP_RTCP_MODULE_ERROR, kTraceError,
3174 "GetRemoteRTCPReportBlocks() failed to read RTCP SR/RR report block.");
3175 return -1;
3176 }
3177
3178 if (rtcp_report_blocks.empty())
3179 return 0;
3180
3181 std::vector<RTCPReportBlock>::const_iterator it = rtcp_report_blocks.begin();
3182 for (; it != rtcp_report_blocks.end(); ++it) {
3183 ReportBlock report_block;
3184 report_block.sender_SSRC = it->remoteSSRC;
3185 report_block.source_SSRC = it->sourceSSRC;
3186 report_block.fraction_lost = it->fractionLost;
3187 report_block.cumulative_num_packets_lost = it->cumulativeLost;
3188 report_block.extended_highest_sequence_number = it->extendedHighSeqNum;
3189 report_block.interarrival_jitter = it->jitter;
3190 report_block.last_SR_timestamp = it->lastSR;
3191 report_block.delay_since_last_SR = it->delaySinceLastSR;
3192 report_blocks->push_back(report_block);
3193 }
3194 return 0;
3195}
3196
niklase@google.com470e71d2011-07-07 08:21:25 +00003197int
3198Channel::GetRTPStatistics(CallStatistics& stats)
3199{
wu@webrtc.orgcb711f72014-05-19 17:39:11 +00003200 // --- RtcpStatistics
niklase@google.com470e71d2011-07-07 08:21:25 +00003201
3202 // The jitter statistics is updated for each received RTP packet and is
3203 // based on received packets.
sprang@webrtc.org54ae4ff2013-12-19 13:26:02 +00003204 RtcpStatistics statistics;
stefan@webrtc.org286fe0b2013-08-21 20:58:21 +00003205 StreamStatistician* statistician =
3206 rtp_receive_statistics_->GetStatistician(rtp_receiver_->SSRC());
3207 if (!statistician || !statistician->GetStatistics(
wu@webrtc.org822fbd82013-08-15 23:38:54 +00003208 &statistics, _rtpRtcpModule->RTCP() == kRtcpOff)) {
3209 _engineStatisticsPtr->SetLastError(
3210 VE_CANNOT_RETRIEVE_RTP_STAT, kTraceWarning,
3211 "GetRTPStatistics() failed to read RTP statistics from the "
3212 "RTP/RTCP module");
niklase@google.com470e71d2011-07-07 08:21:25 +00003213 }
3214
wu@webrtc.org822fbd82013-08-15 23:38:54 +00003215 stats.fractionLost = statistics.fraction_lost;
3216 stats.cumulativeLost = statistics.cumulative_lost;
3217 stats.extendedMax = statistics.extended_max_sequence_number;
3218 stats.jitterSamples = statistics.jitter;
niklase@google.com470e71d2011-07-07 08:21:25 +00003219
3220 WEBRTC_TRACE(kTraceStateInfo, kTraceVoice,
3221 VoEId(_instanceId, _channelId),
3222 "GetRTPStatistics() => fractionLost=%lu, cumulativeLost=%lu,"
wu@webrtc.orgfcd12b32011-09-15 20:49:50 +00003223 " extendedMax=%lu, jitterSamples=%li)",
niklase@google.com470e71d2011-07-07 08:21:25 +00003224 stats.fractionLost, stats.cumulativeLost, stats.extendedMax,
3225 stats.jitterSamples);
3226
wu@webrtc.orgcb711f72014-05-19 17:39:11 +00003227 // --- RTT
minyue@webrtc.org2b58a442014-09-11 07:51:53 +00003228 stats.rttMs = GetRTT();
niklase@google.com470e71d2011-07-07 08:21:25 +00003229
3230 WEBRTC_TRACE(kTraceStateInfo, kTraceVoice,
3231 VoEId(_instanceId, _channelId),
3232 "GetRTPStatistics() => rttMs=%d", stats.rttMs);
3233
wu@webrtc.orgcb711f72014-05-19 17:39:11 +00003234 // --- Data counters
niklase@google.com470e71d2011-07-07 08:21:25 +00003235
pbos@webrtc.org6141e132013-04-09 10:09:10 +00003236 uint32_t bytesSent(0);
3237 uint32_t packetsSent(0);
3238 uint32_t bytesReceived(0);
3239 uint32_t packetsReceived(0);
niklase@google.com470e71d2011-07-07 08:21:25 +00003240
stefan@webrtc.org286fe0b2013-08-21 20:58:21 +00003241 if (statistician) {
3242 statistician->GetDataCounters(&bytesReceived, &packetsReceived);
3243 }
wu@webrtc.org822fbd82013-08-15 23:38:54 +00003244
pwestin@webrtc.org2853dde2012-05-11 11:08:54 +00003245 if (_rtpRtcpModule->DataCountersRTP(&bytesSent,
wu@webrtc.org822fbd82013-08-15 23:38:54 +00003246 &packetsSent) != 0)
niklase@google.com470e71d2011-07-07 08:21:25 +00003247 {
3248 WEBRTC_TRACE(kTraceWarning, kTraceVoice,
3249 VoEId(_instanceId, _channelId),
3250 "GetRTPStatistics() failed to retrieve RTP datacounters =>"
wu@webrtc.orgfcd12b32011-09-15 20:49:50 +00003251 " output will not be complete");
niklase@google.com470e71d2011-07-07 08:21:25 +00003252 }
3253
3254 stats.bytesSent = bytesSent;
3255 stats.packetsSent = packetsSent;
3256 stats.bytesReceived = bytesReceived;
3257 stats.packetsReceived = packetsReceived;
3258
3259 WEBRTC_TRACE(kTraceStateInfo, kTraceVoice,
3260 VoEId(_instanceId, _channelId),
3261 "GetRTPStatistics() => bytesSent=%d, packetsSent=%d,"
wu@webrtc.orgfcd12b32011-09-15 20:49:50 +00003262 " bytesReceived=%d, packetsReceived=%d)",
niklase@google.com470e71d2011-07-07 08:21:25 +00003263 stats.bytesSent, stats.packetsSent, stats.bytesReceived,
3264 stats.packetsReceived);
3265
wu@webrtc.orgcb711f72014-05-19 17:39:11 +00003266 // --- Timestamps
3267 {
3268 CriticalSectionScoped lock(ts_stats_lock_.get());
3269 stats.capture_start_ntp_time_ms_ = capture_start_ntp_time_ms_;
3270 }
niklase@google.com470e71d2011-07-07 08:21:25 +00003271 return 0;
3272}
3273
minyue@webrtc.orgc1a40a72014-05-28 09:52:06 +00003274int Channel::SetREDStatus(bool enable, int redPayloadtype) {
turaj@webrtc.org42259e72012-12-11 02:15:12 +00003275 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId, _channelId),
minyue@webrtc.orgc1a40a72014-05-28 09:52:06 +00003276 "Channel::SetREDStatus()");
niklase@google.com470e71d2011-07-07 08:21:25 +00003277
turaj@webrtc.org8c8ad852013-01-31 18:20:17 +00003278 if (enable) {
3279 if (redPayloadtype < 0 || redPayloadtype > 127) {
3280 _engineStatisticsPtr->SetLastError(
3281 VE_PLTYPE_ERROR, kTraceError,
minyue@webrtc.orgc1a40a72014-05-28 09:52:06 +00003282 "SetREDStatus() invalid RED payload type");
turaj@webrtc.org8c8ad852013-01-31 18:20:17 +00003283 return -1;
3284 }
3285
3286 if (SetRedPayloadType(redPayloadtype) < 0) {
3287 _engineStatisticsPtr->SetLastError(
3288 VE_CODEC_ERROR, kTraceError,
3289 "SetSecondarySendCodec() Failed to register RED ACM");
3290 return -1;
3291 }
turaj@webrtc.org42259e72012-12-11 02:15:12 +00003292 }
niklase@google.com470e71d2011-07-07 08:21:25 +00003293
minyue@webrtc.orgaa5ea1c2014-05-23 15:16:51 +00003294 if (audio_coding_->SetREDStatus(enable) != 0) {
turaj@webrtc.org42259e72012-12-11 02:15:12 +00003295 _engineStatisticsPtr->SetLastError(
3296 VE_AUDIO_CODING_MODULE_ERROR, kTraceError,
minyue@webrtc.orgaa5ea1c2014-05-23 15:16:51 +00003297 "SetREDStatus() failed to set RED state in the ACM");
turaj@webrtc.org42259e72012-12-11 02:15:12 +00003298 return -1;
3299 }
3300 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +00003301}
3302
3303int
minyue@webrtc.orgc1a40a72014-05-28 09:52:06 +00003304Channel::GetREDStatus(bool& enabled, int& redPayloadtype)
niklase@google.com470e71d2011-07-07 08:21:25 +00003305{
minyue@webrtc.orgaa5ea1c2014-05-23 15:16:51 +00003306 enabled = audio_coding_->REDStatus();
niklase@google.com470e71d2011-07-07 08:21:25 +00003307 if (enabled)
3308 {
pbos@webrtc.org6141e132013-04-09 10:09:10 +00003309 int8_t payloadType(0);
pwestin@webrtc.org2853dde2012-05-11 11:08:54 +00003310 if (_rtpRtcpModule->SendREDPayloadType(payloadType) != 0)
niklase@google.com470e71d2011-07-07 08:21:25 +00003311 {
3312 _engineStatisticsPtr->SetLastError(
3313 VE_RTP_RTCP_MODULE_ERROR, kTraceError,
minyue@webrtc.orgc1a40a72014-05-28 09:52:06 +00003314 "GetREDStatus() failed to retrieve RED PT from RTP/RTCP "
niklase@google.com470e71d2011-07-07 08:21:25 +00003315 "module");
3316 return -1;
3317 }
3318 WEBRTC_TRACE(kTraceStateInfo, kTraceVoice,
3319 VoEId(_instanceId, _channelId),
minyue@webrtc.orgc1a40a72014-05-28 09:52:06 +00003320 "GetREDStatus() => enabled=%d, redPayloadtype=%d",
niklase@google.com470e71d2011-07-07 08:21:25 +00003321 enabled, redPayloadtype);
3322 return 0;
3323 }
3324 WEBRTC_TRACE(kTraceStateInfo, kTraceVoice,
3325 VoEId(_instanceId, _channelId),
minyue@webrtc.orgc1a40a72014-05-28 09:52:06 +00003326 "GetREDStatus() => enabled=%d", enabled);
niklase@google.com470e71d2011-07-07 08:21:25 +00003327 return 0;
3328}
3329
minyue@webrtc.orgc1a40a72014-05-28 09:52:06 +00003330int Channel::SetCodecFECStatus(bool enable) {
3331 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId, _channelId),
3332 "Channel::SetCodecFECStatus()");
3333
3334 if (audio_coding_->SetCodecFEC(enable) != 0) {
3335 _engineStatisticsPtr->SetLastError(
3336 VE_AUDIO_CODING_MODULE_ERROR, kTraceError,
3337 "SetCodecFECStatus() failed to set FEC state");
3338 return -1;
3339 }
3340 return 0;
3341}
3342
3343bool Channel::GetCodecFECStatus() {
3344 bool enabled = audio_coding_->CodecFEC();
3345 WEBRTC_TRACE(kTraceStateInfo, kTraceVoice,
3346 VoEId(_instanceId, _channelId),
3347 "GetCodecFECStatus() => enabled=%d", enabled);
3348 return enabled;
3349}
3350
pwestin@webrtc.orgdb249952013-06-05 15:33:20 +00003351void Channel::SetNACKStatus(bool enable, int maxNumberOfPackets) {
3352 // None of these functions can fail.
3353 _rtpRtcpModule->SetStorePacketsStatus(enable, maxNumberOfPackets);
stefan@webrtc.org7bb8f022013-09-06 13:40:11 +00003354 rtp_receive_statistics_->SetMaxReorderingThreshold(maxNumberOfPackets);
3355 rtp_receiver_->SetNACKStatus(enable ? kNackRtcp : kNackOff);
pwestin@webrtc.orgd30859e2013-06-06 21:09:01 +00003356 if (enable)
andrew@webrtc.orgeb524d92013-09-23 23:02:24 +00003357 audio_coding_->EnableNack(maxNumberOfPackets);
pwestin@webrtc.orgd30859e2013-06-06 21:09:01 +00003358 else
andrew@webrtc.orgeb524d92013-09-23 23:02:24 +00003359 audio_coding_->DisableNack();
pwestin@webrtc.orgdb249952013-06-05 15:33:20 +00003360}
3361
pwestin@webrtc.orgd30859e2013-06-06 21:09:01 +00003362// Called when we are missing one or more packets.
3363int Channel::ResendPackets(const uint16_t* sequence_numbers, int length) {
pwestin@webrtc.orgdb249952013-06-05 15:33:20 +00003364 return _rtpRtcpModule->SendNACK(sequence_numbers, length);
3365}
3366
niklase@google.com470e71d2011-07-07 08:21:25 +00003367int
niklase@google.com470e71d2011-07-07 08:21:25 +00003368Channel::StartRTPDump(const char fileNameUTF8[1024],
3369 RTPDirections direction)
3370{
3371 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId, _channelId),
3372 "Channel::StartRTPDump()");
3373 if ((direction != kRtpIncoming) && (direction != kRtpOutgoing))
3374 {
3375 _engineStatisticsPtr->SetLastError(
3376 VE_INVALID_ARGUMENT, kTraceError,
3377 "StartRTPDump() invalid RTP direction");
3378 return -1;
3379 }
3380 RtpDump* rtpDumpPtr = (direction == kRtpIncoming) ?
3381 &_rtpDumpIn : &_rtpDumpOut;
3382 if (rtpDumpPtr == NULL)
3383 {
3384 assert(false);
3385 return -1;
3386 }
3387 if (rtpDumpPtr->IsActive())
3388 {
3389 rtpDumpPtr->Stop();
3390 }
3391 if (rtpDumpPtr->Start(fileNameUTF8) != 0)
3392 {
3393 _engineStatisticsPtr->SetLastError(
3394 VE_BAD_FILE, kTraceError,
3395 "StartRTPDump() failed to create file");
3396 return -1;
3397 }
3398 return 0;
3399}
3400
3401int
3402Channel::StopRTPDump(RTPDirections direction)
3403{
3404 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId, _channelId),
3405 "Channel::StopRTPDump()");
3406 if ((direction != kRtpIncoming) && (direction != kRtpOutgoing))
3407 {
3408 _engineStatisticsPtr->SetLastError(
3409 VE_INVALID_ARGUMENT, kTraceError,
3410 "StopRTPDump() invalid RTP direction");
3411 return -1;
3412 }
3413 RtpDump* rtpDumpPtr = (direction == kRtpIncoming) ?
3414 &_rtpDumpIn : &_rtpDumpOut;
3415 if (rtpDumpPtr == NULL)
3416 {
3417 assert(false);
3418 return -1;
3419 }
3420 if (!rtpDumpPtr->IsActive())
3421 {
3422 return 0;
3423 }
3424 return rtpDumpPtr->Stop();
3425}
3426
3427bool
3428Channel::RTPDumpIsActive(RTPDirections direction)
3429{
3430 if ((direction != kRtpIncoming) &&
3431 (direction != kRtpOutgoing))
3432 {
3433 _engineStatisticsPtr->SetLastError(
3434 VE_INVALID_ARGUMENT, kTraceError,
3435 "RTPDumpIsActive() invalid RTP direction");
3436 return false;
3437 }
3438 RtpDump* rtpDumpPtr = (direction == kRtpIncoming) ?
3439 &_rtpDumpIn : &_rtpDumpOut;
3440 return rtpDumpPtr->IsActive();
3441}
3442
solenberg@webrtc.orgb1f50102014-03-24 10:38:25 +00003443void Channel::SetVideoEngineBWETarget(ViENetwork* vie_network,
3444 int video_channel) {
3445 CriticalSectionScoped cs(&_callbackCritSect);
3446 if (vie_network_) {
3447 vie_network_->Release();
3448 vie_network_ = NULL;
3449 }
3450 video_channel_ = -1;
3451
3452 if (vie_network != NULL && video_channel != -1) {
3453 vie_network_ = vie_network;
3454 video_channel_ = video_channel;
3455 }
3456}
3457
pbos@webrtc.org6141e132013-04-09 10:09:10 +00003458uint32_t
andrew@webrtc.org755b04a2011-11-15 16:57:56 +00003459Channel::Demultiplex(const AudioFrame& audioFrame)
niklase@google.com470e71d2011-07-07 08:21:25 +00003460{
3461 WEBRTC_TRACE(kTraceStream, kTraceVoice, VoEId(_instanceId,_channelId),
andrew@webrtc.org755b04a2011-11-15 16:57:56 +00003462 "Channel::Demultiplex()");
andrew@webrtc.orgae1a58b2013-01-22 04:44:30 +00003463 _audioFrame.CopyFrom(audioFrame);
andrew@webrtc.org63a50982012-05-02 23:56:37 +00003464 _audioFrame.id_ = _channelId;
niklase@google.com470e71d2011-07-07 08:21:25 +00003465 return 0;
3466}
3467
xians@webrtc.org2f84afa2013-07-31 16:23:37 +00003468void Channel::Demultiplex(const int16_t* audio_data,
xians@webrtc.org8fff1f02013-07-31 16:27:42 +00003469 int sample_rate,
xians@webrtc.org2f84afa2013-07-31 16:23:37 +00003470 int number_of_frames,
xians@webrtc.org8fff1f02013-07-31 16:27:42 +00003471 int number_of_channels) {
xians@webrtc.org2f84afa2013-07-31 16:23:37 +00003472 CodecInst codec;
3473 GetSendCodec(codec);
xians@webrtc.org2f84afa2013-07-31 16:23:37 +00003474
andrew@webrtc.org40ee3d02014-04-03 21:56:01 +00003475 if (!mono_recording_audio_.get()) {
3476 // Temporary space for DownConvertToCodecFormat.
3477 mono_recording_audio_.reset(new int16_t[kMaxMonoDataSizeSamples]);
xians@webrtc.org2f84afa2013-07-31 16:23:37 +00003478 }
andrew@webrtc.org40ee3d02014-04-03 21:56:01 +00003479 DownConvertToCodecFormat(audio_data,
3480 number_of_frames,
3481 number_of_channels,
3482 sample_rate,
3483 codec.channels,
3484 codec.plfreq,
3485 mono_recording_audio_.get(),
3486 &input_resampler_,
3487 &_audioFrame);
xians@webrtc.org2f84afa2013-07-31 16:23:37 +00003488}
3489
pbos@webrtc.org6141e132013-04-09 10:09:10 +00003490uint32_t
xians@google.com0b0665a2011-08-08 08:18:44 +00003491Channel::PrepareEncodeAndSend(int mixingFrequency)
niklase@google.com470e71d2011-07-07 08:21:25 +00003492{
3493 WEBRTC_TRACE(kTraceStream, kTraceVoice, VoEId(_instanceId,_channelId),
3494 "Channel::PrepareEncodeAndSend()");
3495
andrew@webrtc.org63a50982012-05-02 23:56:37 +00003496 if (_audioFrame.samples_per_channel_ == 0)
niklase@google.com470e71d2011-07-07 08:21:25 +00003497 {
3498 WEBRTC_TRACE(kTraceWarning, kTraceVoice, VoEId(_instanceId,_channelId),
3499 "Channel::PrepareEncodeAndSend() invalid audio frame");
tommi@webrtc.orgeec6ecd2014-07-11 19:09:59 +00003500 return 0xFFFFFFFF;
niklase@google.com470e71d2011-07-07 08:21:25 +00003501 }
3502
henrika@webrtc.org944cbeb2014-03-18 10:32:33 +00003503 if (channel_state_.Get().input_file_playing)
niklase@google.com470e71d2011-07-07 08:21:25 +00003504 {
3505 MixOrReplaceAudioWithFile(mixingFrequency);
3506 }
3507
andrew@webrtc.org21299d42014-05-14 19:00:59 +00003508 bool is_muted = Mute(); // Cache locally as Mute() takes a lock.
3509 if (is_muted) {
3510 AudioFrameOperations::Mute(_audioFrame);
niklase@google.com470e71d2011-07-07 08:21:25 +00003511 }
3512
henrika@webrtc.org944cbeb2014-03-18 10:32:33 +00003513 if (channel_state_.Get().input_external_media)
niklase@google.com470e71d2011-07-07 08:21:25 +00003514 {
mflodman@webrtc.org9a065d12012-03-07 08:12:21 +00003515 CriticalSectionScoped cs(&_callbackCritSect);
andrew@webrtc.org63a50982012-05-02 23:56:37 +00003516 const bool isStereo = (_audioFrame.num_channels_ == 2);
niklase@google.com470e71d2011-07-07 08:21:25 +00003517 if (_inputExternalMediaCallbackPtr)
3518 {
3519 _inputExternalMediaCallbackPtr->Process(
3520 _channelId,
3521 kRecordingPerChannel,
pbos@webrtc.org6141e132013-04-09 10:09:10 +00003522 (int16_t*)_audioFrame.data_,
andrew@webrtc.org63a50982012-05-02 23:56:37 +00003523 _audioFrame.samples_per_channel_,
3524 _audioFrame.sample_rate_hz_,
niklase@google.com470e71d2011-07-07 08:21:25 +00003525 isStereo);
3526 }
3527 }
3528
3529 InsertInbandDtmfTone();
3530
andrew@webrtc.org60730cf2014-01-07 17:45:09 +00003531 if (_includeAudioLevelIndication) {
andrew@webrtc.org382c0c22014-05-05 18:22:21 +00003532 int length = _audioFrame.samples_per_channel_ * _audioFrame.num_channels_;
andrew@webrtc.org21299d42014-05-14 19:00:59 +00003533 if (is_muted) {
3534 rms_level_.ProcessMuted(length);
3535 } else {
3536 rms_level_.Process(_audioFrame.data_, length);
3537 }
andrew@webrtc.org755b04a2011-11-15 16:57:56 +00003538 }
3539
niklase@google.com470e71d2011-07-07 08:21:25 +00003540 return 0;
3541}
3542
pbos@webrtc.org6141e132013-04-09 10:09:10 +00003543uint32_t
niklase@google.com470e71d2011-07-07 08:21:25 +00003544Channel::EncodeAndSend()
3545{
3546 WEBRTC_TRACE(kTraceStream, kTraceVoice, VoEId(_instanceId,_channelId),
3547 "Channel::EncodeAndSend()");
3548
andrew@webrtc.org63a50982012-05-02 23:56:37 +00003549 assert(_audioFrame.num_channels_ <= 2);
3550 if (_audioFrame.samples_per_channel_ == 0)
niklase@google.com470e71d2011-07-07 08:21:25 +00003551 {
3552 WEBRTC_TRACE(kTraceWarning, kTraceVoice, VoEId(_instanceId,_channelId),
3553 "Channel::EncodeAndSend() invalid audio frame");
tommi@webrtc.orgeec6ecd2014-07-11 19:09:59 +00003554 return 0xFFFFFFFF;
niklase@google.com470e71d2011-07-07 08:21:25 +00003555 }
3556
andrew@webrtc.org63a50982012-05-02 23:56:37 +00003557 _audioFrame.id_ = _channelId;
niklase@google.com470e71d2011-07-07 08:21:25 +00003558
3559 // --- Add 10ms of raw (PCM) audio data to the encoder @ 32kHz.
3560
3561 // The ACM resamples internally.
andrew@webrtc.org63a50982012-05-02 23:56:37 +00003562 _audioFrame.timestamp_ = _timeStamp;
andrew@webrtc.orgeb524d92013-09-23 23:02:24 +00003563 if (audio_coding_->Add10MsData((AudioFrame&)_audioFrame) != 0)
niklase@google.com470e71d2011-07-07 08:21:25 +00003564 {
3565 WEBRTC_TRACE(kTraceError, kTraceVoice, VoEId(_instanceId,_channelId),
3566 "Channel::EncodeAndSend() ACM encoding failed");
tommi@webrtc.orgeec6ecd2014-07-11 19:09:59 +00003567 return 0xFFFFFFFF;
niklase@google.com470e71d2011-07-07 08:21:25 +00003568 }
3569
andrew@webrtc.org63a50982012-05-02 23:56:37 +00003570 _timeStamp += _audioFrame.samples_per_channel_;
niklase@google.com470e71d2011-07-07 08:21:25 +00003571
3572 // --- Encode if complete frame is ready
3573
3574 // This call will trigger AudioPacketizationCallback::SendData if encoding
3575 // is done and payload is ready for packetization and transmission.
andrew@webrtc.orgeb524d92013-09-23 23:02:24 +00003576 return audio_coding_->Process();
niklase@google.com470e71d2011-07-07 08:21:25 +00003577}
3578
3579int Channel::RegisterExternalMediaProcessing(
3580 ProcessingTypes type,
3581 VoEMediaProcess& processObject)
3582{
3583 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId,_channelId),
3584 "Channel::RegisterExternalMediaProcessing()");
3585
mflodman@webrtc.org9a065d12012-03-07 08:12:21 +00003586 CriticalSectionScoped cs(&_callbackCritSect);
niklase@google.com470e71d2011-07-07 08:21:25 +00003587
3588 if (kPlaybackPerChannel == type)
3589 {
3590 if (_outputExternalMediaCallbackPtr)
3591 {
3592 _engineStatisticsPtr->SetLastError(
3593 VE_INVALID_OPERATION, kTraceError,
3594 "Channel::RegisterExternalMediaProcessing() "
3595 "output external media already enabled");
3596 return -1;
3597 }
3598 _outputExternalMediaCallbackPtr = &processObject;
3599 _outputExternalMedia = true;
3600 }
3601 else if (kRecordingPerChannel == type)
3602 {
3603 if (_inputExternalMediaCallbackPtr)
3604 {
3605 _engineStatisticsPtr->SetLastError(
3606 VE_INVALID_OPERATION, kTraceError,
3607 "Channel::RegisterExternalMediaProcessing() "
3608 "output external media already enabled");
3609 return -1;
3610 }
3611 _inputExternalMediaCallbackPtr = &processObject;
henrika@webrtc.org944cbeb2014-03-18 10:32:33 +00003612 channel_state_.SetInputExternalMedia(true);
niklase@google.com470e71d2011-07-07 08:21:25 +00003613 }
3614 return 0;
3615}
3616
3617int Channel::DeRegisterExternalMediaProcessing(ProcessingTypes type)
3618{
3619 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId,_channelId),
3620 "Channel::DeRegisterExternalMediaProcessing()");
3621
mflodman@webrtc.org9a065d12012-03-07 08:12:21 +00003622 CriticalSectionScoped cs(&_callbackCritSect);
niklase@google.com470e71d2011-07-07 08:21:25 +00003623
3624 if (kPlaybackPerChannel == type)
3625 {
3626 if (!_outputExternalMediaCallbackPtr)
3627 {
3628 _engineStatisticsPtr->SetLastError(
3629 VE_INVALID_OPERATION, kTraceWarning,
3630 "Channel::DeRegisterExternalMediaProcessing() "
3631 "output external media already disabled");
3632 return 0;
3633 }
3634 _outputExternalMedia = false;
3635 _outputExternalMediaCallbackPtr = NULL;
3636 }
3637 else if (kRecordingPerChannel == type)
3638 {
3639 if (!_inputExternalMediaCallbackPtr)
3640 {
3641 _engineStatisticsPtr->SetLastError(
3642 VE_INVALID_OPERATION, kTraceWarning,
3643 "Channel::DeRegisterExternalMediaProcessing() "
3644 "input external media already disabled");
3645 return 0;
3646 }
henrika@webrtc.org944cbeb2014-03-18 10:32:33 +00003647 channel_state_.SetInputExternalMedia(false);
niklase@google.com470e71d2011-07-07 08:21:25 +00003648 _inputExternalMediaCallbackPtr = NULL;
3649 }
3650
3651 return 0;
3652}
3653
roosa@google.com1b60ceb2012-12-12 23:00:29 +00003654int Channel::SetExternalMixing(bool enabled) {
3655 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId,_channelId),
3656 "Channel::SetExternalMixing(enabled=%d)", enabled);
3657
henrika@webrtc.org944cbeb2014-03-18 10:32:33 +00003658 if (channel_state_.Get().playing)
roosa@google.com1b60ceb2012-12-12 23:00:29 +00003659 {
3660 _engineStatisticsPtr->SetLastError(
3661 VE_INVALID_OPERATION, kTraceError,
3662 "Channel::SetExternalMixing() "
3663 "external mixing cannot be changed while playing.");
3664 return -1;
3665 }
3666
3667 _externalMixing = enabled;
3668
3669 return 0;
3670}
3671
niklase@google.com470e71d2011-07-07 08:21:25 +00003672int
niklase@google.com470e71d2011-07-07 08:21:25 +00003673Channel::GetNetworkStatistics(NetworkStatistics& stats)
3674{
3675 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId,_channelId),
3676 "Channel::GetNetworkStatistics()");
tina.legrand@webrtc.org7a7a0082013-02-21 10:27:48 +00003677 ACMNetworkStatistics acm_stats;
andrew@webrtc.orgeb524d92013-09-23 23:02:24 +00003678 int return_value = audio_coding_->NetworkStatistics(&acm_stats);
tina.legrand@webrtc.org7a7a0082013-02-21 10:27:48 +00003679 if (return_value >= 0) {
3680 memcpy(&stats, &acm_stats, sizeof(NetworkStatistics));
3681 }
3682 return return_value;
niklase@google.com470e71d2011-07-07 08:21:25 +00003683}
3684
wu@webrtc.org24301a62013-12-13 19:17:43 +00003685void Channel::GetDecodingCallStatistics(AudioDecodingCallStats* stats) const {
3686 audio_coding_->GetDecodingCallStatistics(stats);
3687}
3688
pwestin@webrtc.org1de01352013-04-11 20:23:35 +00003689bool Channel::GetDelayEstimate(int* jitter_buffer_delay_ms,
3690 int* playout_buffer_delay_ms) const {
3691 if (_average_jitter_buffer_delay_us == 0) {
niklase@google.com470e71d2011-07-07 08:21:25 +00003692 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId,_channelId),
pwestin@webrtc.org1de01352013-04-11 20:23:35 +00003693 "Channel::GetDelayEstimate() no valid estimate.");
3694 return false;
3695 }
3696 *jitter_buffer_delay_ms = (_average_jitter_buffer_delay_us + 500) / 1000 +
3697 _recPacketDelayMs;
3698 *playout_buffer_delay_ms = playout_delay_ms_;
3699 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId,_channelId),
3700 "Channel::GetDelayEstimate()");
3701 return true;
niklase@google.com470e71d2011-07-07 08:21:25 +00003702}
3703
turaj@webrtc.org6388c3e2013-02-12 21:42:18 +00003704int Channel::SetInitialPlayoutDelay(int delay_ms)
3705{
3706 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId,_channelId),
3707 "Channel::SetInitialPlayoutDelay()");
3708 if ((delay_ms < kVoiceEngineMinMinPlayoutDelayMs) ||
3709 (delay_ms > kVoiceEngineMaxMinPlayoutDelayMs))
3710 {
3711 _engineStatisticsPtr->SetLastError(
3712 VE_INVALID_ARGUMENT, kTraceError,
3713 "SetInitialPlayoutDelay() invalid min delay");
3714 return -1;
3715 }
andrew@webrtc.orgeb524d92013-09-23 23:02:24 +00003716 if (audio_coding_->SetInitialPlayoutDelay(delay_ms) != 0)
turaj@webrtc.org6388c3e2013-02-12 21:42:18 +00003717 {
3718 _engineStatisticsPtr->SetLastError(
3719 VE_AUDIO_CODING_MODULE_ERROR, kTraceError,
3720 "SetInitialPlayoutDelay() failed to set min playout delay");
3721 return -1;
3722 }
3723 return 0;
3724}
3725
3726
niklase@google.com470e71d2011-07-07 08:21:25 +00003727int
3728Channel::SetMinimumPlayoutDelay(int delayMs)
3729{
3730 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId,_channelId),
3731 "Channel::SetMinimumPlayoutDelay()");
3732 if ((delayMs < kVoiceEngineMinMinPlayoutDelayMs) ||
3733 (delayMs > kVoiceEngineMaxMinPlayoutDelayMs))
3734 {
3735 _engineStatisticsPtr->SetLastError(
3736 VE_INVALID_ARGUMENT, kTraceError,
3737 "SetMinimumPlayoutDelay() invalid min delay");
3738 return -1;
3739 }
andrew@webrtc.orgeb524d92013-09-23 23:02:24 +00003740 if (audio_coding_->SetMinimumPlayoutDelay(delayMs) != 0)
niklase@google.com470e71d2011-07-07 08:21:25 +00003741 {
3742 _engineStatisticsPtr->SetLastError(
3743 VE_AUDIO_CODING_MODULE_ERROR, kTraceError,
3744 "SetMinimumPlayoutDelay() failed to set min playout delay");
3745 return -1;
3746 }
3747 return 0;
3748}
3749
pwestin@webrtc.org1de01352013-04-11 20:23:35 +00003750void Channel::UpdatePlayoutTimestamp(bool rtcp) {
3751 uint32_t playout_timestamp = 0;
3752
andrew@webrtc.orgeb524d92013-09-23 23:02:24 +00003753 if (audio_coding_->PlayoutTimestamp(&playout_timestamp) == -1) {
turaj@webrtc.org1ebd2e92014-07-25 17:50:10 +00003754 // This can happen if this channel has not been received any RTP packet. In
3755 // this case, NetEq is not capable of computing playout timestamp.
pwestin@webrtc.org1de01352013-04-11 20:23:35 +00003756 return;
3757 }
3758
3759 uint16_t delay_ms = 0;
3760 if (_audioDeviceModulePtr->PlayoutDelay(&delay_ms) == -1) {
3761 WEBRTC_TRACE(kTraceWarning, kTraceVoice, VoEId(_instanceId,_channelId),
3762 "Channel::UpdatePlayoutTimestamp() failed to read playout"
3763 " delay from the ADM");
3764 _engineStatisticsPtr->SetLastError(
3765 VE_CANNOT_RETRIEVE_VALUE, kTraceError,
3766 "UpdatePlayoutTimestamp() failed to retrieve playout delay");
3767 return;
3768 }
3769
turaj@webrtc.org167b6df2013-12-13 21:05:07 +00003770 jitter_buffer_playout_timestamp_ = playout_timestamp;
3771
pwestin@webrtc.org1de01352013-04-11 20:23:35 +00003772 // Remove the playout delay.
wu@webrtc.org94454b72014-06-05 20:34:08 +00003773 playout_timestamp -= (delay_ms * (GetPlayoutFrequency() / 1000));
pwestin@webrtc.org1de01352013-04-11 20:23:35 +00003774
3775 WEBRTC_TRACE(kTraceStream, kTraceVoice, VoEId(_instanceId,_channelId),
3776 "Channel::UpdatePlayoutTimestamp() => playoutTimestamp = %lu",
3777 playout_timestamp);
3778
3779 if (rtcp) {
3780 playout_timestamp_rtcp_ = playout_timestamp;
3781 } else {
3782 playout_timestamp_rtp_ = playout_timestamp;
3783 }
3784 playout_delay_ms_ = delay_ms;
3785}
3786
3787int Channel::GetPlayoutTimestamp(unsigned int& timestamp) {
3788 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId,_channelId),
3789 "Channel::GetPlayoutTimestamp()");
3790 if (playout_timestamp_rtp_ == 0) {
3791 _engineStatisticsPtr->SetLastError(
3792 VE_CANNOT_RETRIEVE_VALUE, kTraceError,
3793 "GetPlayoutTimestamp() failed to retrieve timestamp");
3794 return -1;
3795 }
3796 timestamp = playout_timestamp_rtp_;
3797 WEBRTC_TRACE(kTraceStateInfo, kTraceVoice,
3798 VoEId(_instanceId,_channelId),
3799 "GetPlayoutTimestamp() => timestamp=%u", timestamp);
3800 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +00003801}
3802
3803int
3804Channel::SetInitTimestamp(unsigned int timestamp)
3805{
3806 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId,_channelId),
3807 "Channel::SetInitTimestamp()");
henrika@webrtc.org944cbeb2014-03-18 10:32:33 +00003808 if (channel_state_.Get().sending)
niklase@google.com470e71d2011-07-07 08:21:25 +00003809 {
3810 _engineStatisticsPtr->SetLastError(
3811 VE_SENDING, kTraceError, "SetInitTimestamp() already sending");
3812 return -1;
3813 }
pwestin@webrtc.org2853dde2012-05-11 11:08:54 +00003814 if (_rtpRtcpModule->SetStartTimestamp(timestamp) != 0)
niklase@google.com470e71d2011-07-07 08:21:25 +00003815 {
3816 _engineStatisticsPtr->SetLastError(
3817 VE_RTP_RTCP_MODULE_ERROR, kTraceError,
3818 "SetInitTimestamp() failed to set timestamp");
3819 return -1;
3820 }
3821 return 0;
3822}
3823
3824int
3825Channel::SetInitSequenceNumber(short sequenceNumber)
3826{
3827 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId,_channelId),
3828 "Channel::SetInitSequenceNumber()");
henrika@webrtc.org944cbeb2014-03-18 10:32:33 +00003829 if (channel_state_.Get().sending)
niklase@google.com470e71d2011-07-07 08:21:25 +00003830 {
3831 _engineStatisticsPtr->SetLastError(
3832 VE_SENDING, kTraceError,
3833 "SetInitSequenceNumber() already sending");
3834 return -1;
3835 }
pwestin@webrtc.org2853dde2012-05-11 11:08:54 +00003836 if (_rtpRtcpModule->SetSequenceNumber(sequenceNumber) != 0)
niklase@google.com470e71d2011-07-07 08:21:25 +00003837 {
3838 _engineStatisticsPtr->SetLastError(
3839 VE_RTP_RTCP_MODULE_ERROR, kTraceError,
3840 "SetInitSequenceNumber() failed to set sequence number");
3841 return -1;
3842 }
3843 return 0;
3844}
3845
3846int
wu@webrtc.org822fbd82013-08-15 23:38:54 +00003847Channel::GetRtpRtcp(RtpRtcp** rtpRtcpModule, RtpReceiver** rtp_receiver) const
niklase@google.com470e71d2011-07-07 08:21:25 +00003848{
3849 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId,_channelId),
3850 "Channel::GetRtpRtcp()");
wu@webrtc.org822fbd82013-08-15 23:38:54 +00003851 *rtpRtcpModule = _rtpRtcpModule.get();
3852 *rtp_receiver = rtp_receiver_.get();
niklase@google.com470e71d2011-07-07 08:21:25 +00003853 return 0;
3854}
3855
andrew@webrtc.orge59a0ac2012-05-08 17:12:40 +00003856// TODO(andrew): refactor Mix functions here and in transmit_mixer.cc to use
3857// a shared helper.
pbos@webrtc.org6141e132013-04-09 10:09:10 +00003858int32_t
pbos@webrtc.org92135212013-05-14 08:31:39 +00003859Channel::MixOrReplaceAudioWithFile(int mixingFrequency)
niklase@google.com470e71d2011-07-07 08:21:25 +00003860{
andrew@webrtc.org8f693302014-04-25 23:10:28 +00003861 scoped_ptr<int16_t[]> fileBuffer(new int16_t[640]);
andrew@webrtc.orge59a0ac2012-05-08 17:12:40 +00003862 int fileSamples(0);
niklase@google.com470e71d2011-07-07 08:21:25 +00003863
3864 {
mflodman@webrtc.org9a065d12012-03-07 08:12:21 +00003865 CriticalSectionScoped cs(&_fileCritSect);
niklase@google.com470e71d2011-07-07 08:21:25 +00003866
3867 if (_inputFilePlayerPtr == NULL)
3868 {
3869 WEBRTC_TRACE(kTraceWarning, kTraceVoice,
3870 VoEId(_instanceId, _channelId),
3871 "Channel::MixOrReplaceAudioWithFile() fileplayer"
3872 " doesnt exist");
3873 return -1;
3874 }
3875
braveyao@webrtc.orgd7131432012-03-29 10:39:44 +00003876 if (_inputFilePlayerPtr->Get10msAudioFromFile(fileBuffer.get(),
niklase@google.com470e71d2011-07-07 08:21:25 +00003877 fileSamples,
3878 mixingFrequency) == -1)
3879 {
3880 WEBRTC_TRACE(kTraceWarning, kTraceVoice,
3881 VoEId(_instanceId, _channelId),
3882 "Channel::MixOrReplaceAudioWithFile() file mixing "
3883 "failed");
3884 return -1;
3885 }
3886 if (fileSamples == 0)
3887 {
3888 WEBRTC_TRACE(kTraceWarning, kTraceVoice,
3889 VoEId(_instanceId, _channelId),
3890 "Channel::MixOrReplaceAudioWithFile() file is ended");
3891 return 0;
3892 }
3893 }
3894
andrew@webrtc.org63a50982012-05-02 23:56:37 +00003895 assert(_audioFrame.samples_per_channel_ == fileSamples);
niklase@google.com470e71d2011-07-07 08:21:25 +00003896
3897 if (_mixFileWithMicrophone)
3898 {
braveyao@webrtc.orgd7131432012-03-29 10:39:44 +00003899 // Currently file stream is always mono.
3900 // TODO(xians): Change the code when FilePlayer supports real stereo.
andrew@webrtc.org40ee3d02014-04-03 21:56:01 +00003901 MixWithSat(_audioFrame.data_,
3902 _audioFrame.num_channels_,
3903 fileBuffer.get(),
3904 1,
3905 fileSamples);
niklase@google.com470e71d2011-07-07 08:21:25 +00003906 }
3907 else
3908 {
braveyao@webrtc.orgd7131432012-03-29 10:39:44 +00003909 // Replace ACM audio with file.
3910 // Currently file stream is always mono.
3911 // TODO(xians): Change the code when FilePlayer supports real stereo.
niklase@google.com470e71d2011-07-07 08:21:25 +00003912 _audioFrame.UpdateFrame(_channelId,
tommi@webrtc.orgeec6ecd2014-07-11 19:09:59 +00003913 0xFFFFFFFF,
braveyao@webrtc.orgd7131432012-03-29 10:39:44 +00003914 fileBuffer.get(),
andrew@webrtc.orge59a0ac2012-05-08 17:12:40 +00003915 fileSamples,
niklase@google.com470e71d2011-07-07 08:21:25 +00003916 mixingFrequency,
3917 AudioFrame::kNormalSpeech,
3918 AudioFrame::kVadUnknown,
3919 1);
3920
3921 }
3922 return 0;
3923}
3924
pbos@webrtc.org6141e132013-04-09 10:09:10 +00003925int32_t
niklase@google.com470e71d2011-07-07 08:21:25 +00003926Channel::MixAudioWithFile(AudioFrame& audioFrame,
pbos@webrtc.org92135212013-05-14 08:31:39 +00003927 int mixingFrequency)
niklase@google.com470e71d2011-07-07 08:21:25 +00003928{
minyue@webrtc.org2a8df7c2014-08-06 10:05:19 +00003929 assert(mixingFrequency <= 48000);
niklase@google.com470e71d2011-07-07 08:21:25 +00003930
minyue@webrtc.org2a8df7c2014-08-06 10:05:19 +00003931 scoped_ptr<int16_t[]> fileBuffer(new int16_t[960]);
andrew@webrtc.orge59a0ac2012-05-08 17:12:40 +00003932 int fileSamples(0);
niklase@google.com470e71d2011-07-07 08:21:25 +00003933
3934 {
mflodman@webrtc.org9a065d12012-03-07 08:12:21 +00003935 CriticalSectionScoped cs(&_fileCritSect);
niklase@google.com470e71d2011-07-07 08:21:25 +00003936
3937 if (_outputFilePlayerPtr == NULL)
3938 {
3939 WEBRTC_TRACE(kTraceWarning, kTraceVoice,
3940 VoEId(_instanceId, _channelId),
3941 "Channel::MixAudioWithFile() file mixing failed");
3942 return -1;
3943 }
3944
3945 // We should get the frequency we ask for.
braveyao@webrtc.orgd7131432012-03-29 10:39:44 +00003946 if (_outputFilePlayerPtr->Get10msAudioFromFile(fileBuffer.get(),
niklase@google.com470e71d2011-07-07 08:21:25 +00003947 fileSamples,
3948 mixingFrequency) == -1)
3949 {
3950 WEBRTC_TRACE(kTraceWarning, kTraceVoice,
3951 VoEId(_instanceId, _channelId),
3952 "Channel::MixAudioWithFile() file mixing failed");
3953 return -1;
3954 }
3955 }
3956
andrew@webrtc.org63a50982012-05-02 23:56:37 +00003957 if (audioFrame.samples_per_channel_ == fileSamples)
niklase@google.com470e71d2011-07-07 08:21:25 +00003958 {
braveyao@webrtc.orgd7131432012-03-29 10:39:44 +00003959 // Currently file stream is always mono.
3960 // TODO(xians): Change the code when FilePlayer supports real stereo.
andrew@webrtc.org40ee3d02014-04-03 21:56:01 +00003961 MixWithSat(audioFrame.data_,
3962 audioFrame.num_channels_,
3963 fileBuffer.get(),
3964 1,
3965 fileSamples);
niklase@google.com470e71d2011-07-07 08:21:25 +00003966 }
3967 else
3968 {
3969 WEBRTC_TRACE(kTraceWarning, kTraceVoice, VoEId(_instanceId,_channelId),
andrew@webrtc.org63a50982012-05-02 23:56:37 +00003970 "Channel::MixAudioWithFile() samples_per_channel_(%d) != "
niklase@google.com470e71d2011-07-07 08:21:25 +00003971 "fileSamples(%d)",
andrew@webrtc.org63a50982012-05-02 23:56:37 +00003972 audioFrame.samples_per_channel_, fileSamples);
niklase@google.com470e71d2011-07-07 08:21:25 +00003973 return -1;
3974 }
3975
3976 return 0;
3977}
3978
3979int
3980Channel::InsertInbandDtmfTone()
3981{
niklas.enbom@webrtc.orgaf26f642011-11-16 12:41:36 +00003982 // Check if we should start a new tone.
niklase@google.com470e71d2011-07-07 08:21:25 +00003983 if (_inbandDtmfQueue.PendingDtmf() &&
3984 !_inbandDtmfGenerator.IsAddingTone() &&
3985 _inbandDtmfGenerator.DelaySinceLastTone() >
3986 kMinTelephoneEventSeparationMs)
3987 {
pbos@webrtc.org6141e132013-04-09 10:09:10 +00003988 int8_t eventCode(0);
3989 uint16_t lengthMs(0);
3990 uint8_t attenuationDb(0);
niklase@google.com470e71d2011-07-07 08:21:25 +00003991
3992 eventCode = _inbandDtmfQueue.NextDtmf(&lengthMs, &attenuationDb);
3993 _inbandDtmfGenerator.AddTone(eventCode, lengthMs, attenuationDb);
3994 if (_playInbandDtmfEvent)
3995 {
3996 // Add tone to output mixer using a reduced length to minimize
3997 // risk of echo.
3998 _outputMixerPtr->PlayDtmfTone(eventCode, lengthMs - 80,
3999 attenuationDb);
4000 }
4001 }
4002
4003 if (_inbandDtmfGenerator.IsAddingTone())
4004 {
pbos@webrtc.org6141e132013-04-09 10:09:10 +00004005 uint16_t frequency(0);
niklase@google.com470e71d2011-07-07 08:21:25 +00004006 _inbandDtmfGenerator.GetSampleRate(frequency);
4007
andrew@webrtc.org63a50982012-05-02 23:56:37 +00004008 if (frequency != _audioFrame.sample_rate_hz_)
niklase@google.com470e71d2011-07-07 08:21:25 +00004009 {
4010 // Update sample rate of Dtmf tone since the mixing frequency
4011 // has changed.
4012 _inbandDtmfGenerator.SetSampleRate(
pbos@webrtc.org6141e132013-04-09 10:09:10 +00004013 (uint16_t) (_audioFrame.sample_rate_hz_));
niklase@google.com470e71d2011-07-07 08:21:25 +00004014 // Reset the tone to be added taking the new sample rate into
4015 // account.
4016 _inbandDtmfGenerator.ResetTone();
4017 }
andrew@webrtc.orgae1a58b2013-01-22 04:44:30 +00004018
pbos@webrtc.org6141e132013-04-09 10:09:10 +00004019 int16_t toneBuffer[320];
4020 uint16_t toneSamples(0);
niklase@google.com470e71d2011-07-07 08:21:25 +00004021 // Get 10ms tone segment and set time since last tone to zero
4022 if (_inbandDtmfGenerator.Get10msTone(toneBuffer, toneSamples) == -1)
4023 {
4024 WEBRTC_TRACE(kTraceWarning, kTraceVoice,
4025 VoEId(_instanceId, _channelId),
4026 "Channel::EncodeAndSend() inserting Dtmf failed");
4027 return -1;
4028 }
4029
niklas.enbom@webrtc.orgaf26f642011-11-16 12:41:36 +00004030 // Replace mixed audio with DTMF tone.
andrew@webrtc.orgae1a58b2013-01-22 04:44:30 +00004031 for (int sample = 0;
andrew@webrtc.org63a50982012-05-02 23:56:37 +00004032 sample < _audioFrame.samples_per_channel_;
niklas.enbom@webrtc.orgaf26f642011-11-16 12:41:36 +00004033 sample++)
4034 {
andrew@webrtc.orgae1a58b2013-01-22 04:44:30 +00004035 for (int channel = 0;
4036 channel < _audioFrame.num_channels_;
niklas.enbom@webrtc.orgaf26f642011-11-16 12:41:36 +00004037 channel++)
4038 {
andrew@webrtc.orgae1a58b2013-01-22 04:44:30 +00004039 const int index = sample * _audioFrame.num_channels_ + channel;
4040 _audioFrame.data_[index] = toneBuffer[sample];
niklas.enbom@webrtc.orgaf26f642011-11-16 12:41:36 +00004041 }
4042 }
andrew@webrtc.orgae1a58b2013-01-22 04:44:30 +00004043
andrew@webrtc.org63a50982012-05-02 23:56:37 +00004044 assert(_audioFrame.samples_per_channel_ == toneSamples);
niklase@google.com470e71d2011-07-07 08:21:25 +00004045 } else
4046 {
4047 // Add 10ms to "delay-since-last-tone" counter
4048 _inbandDtmfGenerator.UpdateDelaySinceLastTone();
4049 }
4050 return 0;
4051}
4052
pbos@webrtc.org6141e132013-04-09 10:09:10 +00004053int32_t
niklase@google.com470e71d2011-07-07 08:21:25 +00004054Channel::SendPacketRaw(const void *data, int len, bool RTCP)
4055{
wu@webrtc.orgfb648da2013-10-18 21:10:51 +00004056 CriticalSectionScoped cs(&_callbackCritSect);
niklase@google.com470e71d2011-07-07 08:21:25 +00004057 if (_transportPtr == NULL)
4058 {
4059 return -1;
4060 }
4061 if (!RTCP)
4062 {
4063 return _transportPtr->SendPacket(_channelId, data, len);
4064 }
4065 else
4066 {
4067 return _transportPtr->SendRTCPPacket(_channelId, data, len);
4068 }
4069}
4070
pwestin@webrtc.org1de01352013-04-11 20:23:35 +00004071// Called for incoming RTP packets after successful RTP header parsing.
4072void Channel::UpdatePacketDelay(uint32_t rtp_timestamp,
4073 uint16_t sequence_number) {
4074 WEBRTC_TRACE(kTraceStream, kTraceVoice, VoEId(_instanceId,_channelId),
4075 "Channel::UpdatePacketDelay(timestamp=%lu, sequenceNumber=%u)",
4076 rtp_timestamp, sequence_number);
niklase@google.com470e71d2011-07-07 08:21:25 +00004077
pwestin@webrtc.org1de01352013-04-11 20:23:35 +00004078 // Get frequency of last received payload
wu@webrtc.org94454b72014-06-05 20:34:08 +00004079 int rtp_receive_frequency = GetPlayoutFrequency();
niklase@google.com470e71d2011-07-07 08:21:25 +00004080
turaj@webrtc.orge46c8d32013-05-22 20:39:43 +00004081 // Update the least required delay.
andrew@webrtc.orgeb524d92013-09-23 23:02:24 +00004082 least_required_delay_ms_ = audio_coding_->LeastRequiredDelayMs();
turaj@webrtc.orge46c8d32013-05-22 20:39:43 +00004083
turaj@webrtc.org167b6df2013-12-13 21:05:07 +00004084 // |jitter_buffer_playout_timestamp_| updated in UpdatePlayoutTimestamp for
4085 // every incoming packet.
4086 uint32_t timestamp_diff_ms = (rtp_timestamp -
4087 jitter_buffer_playout_timestamp_) / (rtp_receive_frequency / 1000);
henrik.lundin@webrtc.orgd6692992014-03-20 12:04:09 +00004088 if (!IsNewerTimestamp(rtp_timestamp, jitter_buffer_playout_timestamp_) ||
4089 timestamp_diff_ms > (2 * kVoiceEngineMaxMinPlayoutDelayMs)) {
4090 // If |jitter_buffer_playout_timestamp_| is newer than the incoming RTP
4091 // timestamp, the resulting difference is negative, but is set to zero.
4092 // This can happen when a network glitch causes a packet to arrive late,
4093 // and during long comfort noise periods with clock drift.
4094 timestamp_diff_ms = 0;
4095 }
niklase@google.com470e71d2011-07-07 08:21:25 +00004096
pwestin@webrtc.org1de01352013-04-11 20:23:35 +00004097 uint16_t packet_delay_ms = (rtp_timestamp - _previousTimestamp) /
4098 (rtp_receive_frequency / 1000);
niklase@google.com470e71d2011-07-07 08:21:25 +00004099
pwestin@webrtc.org1de01352013-04-11 20:23:35 +00004100 _previousTimestamp = rtp_timestamp;
niklase@google.com470e71d2011-07-07 08:21:25 +00004101
pwestin@webrtc.org1de01352013-04-11 20:23:35 +00004102 if (timestamp_diff_ms == 0) return;
niklase@google.com470e71d2011-07-07 08:21:25 +00004103
pwestin@webrtc.org1de01352013-04-11 20:23:35 +00004104 if (packet_delay_ms >= 10 && packet_delay_ms <= 60) {
4105 _recPacketDelayMs = packet_delay_ms;
4106 }
niklase@google.com470e71d2011-07-07 08:21:25 +00004107
pwestin@webrtc.org1de01352013-04-11 20:23:35 +00004108 if (_average_jitter_buffer_delay_us == 0) {
4109 _average_jitter_buffer_delay_us = timestamp_diff_ms * 1000;
4110 return;
4111 }
4112
4113 // Filter average delay value using exponential filter (alpha is
4114 // 7/8). We derive 1000 *_average_jitter_buffer_delay_us here (reduces
4115 // risk of rounding error) and compensate for it in GetDelayEstimate()
4116 // later.
4117 _average_jitter_buffer_delay_us = (_average_jitter_buffer_delay_us * 7 +
4118 1000 * timestamp_diff_ms + 500) / 8;
niklase@google.com470e71d2011-07-07 08:21:25 +00004119}
4120
4121void
4122Channel::RegisterReceiveCodecsToRTPModule()
4123{
4124 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId,_channelId),
4125 "Channel::RegisterReceiveCodecsToRTPModule()");
4126
4127
4128 CodecInst codec;
pbos@webrtc.org6141e132013-04-09 10:09:10 +00004129 const uint8_t nSupportedCodecs = AudioCodingModule::NumberOfCodecs();
niklase@google.com470e71d2011-07-07 08:21:25 +00004130
4131 for (int idx = 0; idx < nSupportedCodecs; idx++)
4132 {
4133 // Open up the RTP/RTCP receiver for all supported codecs
andrew@webrtc.orgeb524d92013-09-23 23:02:24 +00004134 if ((audio_coding_->Codec(idx, &codec) == -1) ||
wu@webrtc.org822fbd82013-08-15 23:38:54 +00004135 (rtp_receiver_->RegisterReceivePayload(
4136 codec.plname,
4137 codec.pltype,
4138 codec.plfreq,
4139 codec.channels,
4140 (codec.rate < 0) ? 0 : codec.rate) == -1))
niklase@google.com470e71d2011-07-07 08:21:25 +00004141 {
4142 WEBRTC_TRACE(
4143 kTraceWarning,
4144 kTraceVoice,
4145 VoEId(_instanceId, _channelId),
4146 "Channel::RegisterReceiveCodecsToRTPModule() unable"
4147 " to register %s (%d/%d/%d/%d) to RTP/RTCP receiver",
4148 codec.plname, codec.pltype, codec.plfreq,
4149 codec.channels, codec.rate);
4150 }
4151 else
4152 {
4153 WEBRTC_TRACE(
4154 kTraceInfo,
4155 kTraceVoice,
4156 VoEId(_instanceId, _channelId),
4157 "Channel::RegisterReceiveCodecsToRTPModule() %s "
wu@webrtc.orgfcd12b32011-09-15 20:49:50 +00004158 "(%d/%d/%d/%d) has been added to the RTP/RTCP "
niklase@google.com470e71d2011-07-07 08:21:25 +00004159 "receiver",
4160 codec.plname, codec.pltype, codec.plfreq,
4161 codec.channels, codec.rate);
4162 }
4163 }
4164}
4165
turaj@webrtc.org42259e72012-12-11 02:15:12 +00004166int Channel::SetSecondarySendCodec(const CodecInst& codec,
4167 int red_payload_type) {
turaj@webrtc.org8c8ad852013-01-31 18:20:17 +00004168 // Sanity check for payload type.
4169 if (red_payload_type < 0 || red_payload_type > 127) {
4170 _engineStatisticsPtr->SetLastError(
4171 VE_PLTYPE_ERROR, kTraceError,
4172 "SetRedPayloadType() invalid RED payload type");
4173 return -1;
4174 }
4175
turaj@webrtc.org42259e72012-12-11 02:15:12 +00004176 if (SetRedPayloadType(red_payload_type) < 0) {
4177 _engineStatisticsPtr->SetLastError(
4178 VE_AUDIO_CODING_MODULE_ERROR, kTraceError,
4179 "SetSecondarySendCodec() Failed to register RED ACM");
4180 return -1;
4181 }
andrew@webrtc.orgeb524d92013-09-23 23:02:24 +00004182 if (audio_coding_->RegisterSecondarySendCodec(codec) < 0) {
turaj@webrtc.org42259e72012-12-11 02:15:12 +00004183 _engineStatisticsPtr->SetLastError(
4184 VE_AUDIO_CODING_MODULE_ERROR, kTraceError,
4185 "SetSecondarySendCodec() Failed to register secondary send codec in "
4186 "ACM");
4187 return -1;
4188 }
4189
4190 return 0;
4191}
4192
4193void Channel::RemoveSecondarySendCodec() {
andrew@webrtc.orgeb524d92013-09-23 23:02:24 +00004194 audio_coding_->UnregisterSecondarySendCodec();
turaj@webrtc.org42259e72012-12-11 02:15:12 +00004195}
4196
4197int Channel::GetSecondarySendCodec(CodecInst* codec) {
andrew@webrtc.orgeb524d92013-09-23 23:02:24 +00004198 if (audio_coding_->SecondarySendCodec(codec) < 0) {
turaj@webrtc.org42259e72012-12-11 02:15:12 +00004199 _engineStatisticsPtr->SetLastError(
4200 VE_AUDIO_CODING_MODULE_ERROR, kTraceError,
4201 "GetSecondarySendCodec() Failed to get secondary sent codec from ACM");
4202 return -1;
4203 }
4204 return 0;
4205}
4206
turaj@webrtc.org8c8ad852013-01-31 18:20:17 +00004207// Assuming this method is called with valid payload type.
turaj@webrtc.org42259e72012-12-11 02:15:12 +00004208int Channel::SetRedPayloadType(int red_payload_type) {
turaj@webrtc.org42259e72012-12-11 02:15:12 +00004209 CodecInst codec;
4210 bool found_red = false;
4211
4212 // Get default RED settings from the ACM database
4213 const int num_codecs = AudioCodingModule::NumberOfCodecs();
4214 for (int idx = 0; idx < num_codecs; idx++) {
andrew@webrtc.orgeb524d92013-09-23 23:02:24 +00004215 audio_coding_->Codec(idx, &codec);
turaj@webrtc.org42259e72012-12-11 02:15:12 +00004216 if (!STR_CASE_CMP(codec.plname, "RED")) {
4217 found_red = true;
4218 break;
4219 }
4220 }
4221
4222 if (!found_red) {
4223 _engineStatisticsPtr->SetLastError(
4224 VE_CODEC_ERROR, kTraceError,
4225 "SetRedPayloadType() RED is not supported");
4226 return -1;
4227 }
4228
turaj@webrtc.org9d532fd2013-01-31 18:34:19 +00004229 codec.pltype = red_payload_type;
andrew@webrtc.orgeb524d92013-09-23 23:02:24 +00004230 if (audio_coding_->RegisterSendCodec(codec) < 0) {
turaj@webrtc.org42259e72012-12-11 02:15:12 +00004231 _engineStatisticsPtr->SetLastError(
4232 VE_AUDIO_CODING_MODULE_ERROR, kTraceError,
4233 "SetRedPayloadType() RED registration in ACM module failed");
4234 return -1;
4235 }
4236
4237 if (_rtpRtcpModule->SetSendREDPayloadType(red_payload_type) != 0) {
4238 _engineStatisticsPtr->SetLastError(
4239 VE_RTP_RTCP_MODULE_ERROR, kTraceError,
4240 "SetRedPayloadType() RED registration in RTP/RTCP module failed");
4241 return -1;
4242 }
4243 return 0;
4244}
4245
wu@webrtc.orgebdb0e32014-03-06 23:49:08 +00004246int Channel::SetSendRtpHeaderExtension(bool enable, RTPExtensionType type,
4247 unsigned char id) {
4248 int error = 0;
4249 _rtpRtcpModule->DeregisterSendRtpHeaderExtension(type);
4250 if (enable) {
4251 error = _rtpRtcpModule->RegisterSendRtpHeaderExtension(type, id);
4252 }
4253 return error;
4254}
minyue@webrtc.orgc1a40a72014-05-28 09:52:06 +00004255
wu@webrtc.org94454b72014-06-05 20:34:08 +00004256int32_t Channel::GetPlayoutFrequency() {
4257 int32_t playout_frequency = audio_coding_->PlayoutFrequency();
4258 CodecInst current_recive_codec;
4259 if (audio_coding_->ReceiveCodec(&current_recive_codec) == 0) {
4260 if (STR_CASE_CMP("G722", current_recive_codec.plname) == 0) {
4261 // Even though the actual sampling rate for G.722 audio is
4262 // 16,000 Hz, the RTP clock rate for the G722 payload format is
4263 // 8,000 Hz because that value was erroneously assigned in
4264 // RFC 1890 and must remain unchanged for backward compatibility.
4265 playout_frequency = 8000;
4266 } else if (STR_CASE_CMP("opus", current_recive_codec.plname) == 0) {
4267 // We are resampling Opus internally to 32,000 Hz until all our
4268 // DSP routines can operate at 48,000 Hz, but the RTP clock
4269 // rate for the Opus payload format is standardized to 48,000 Hz,
4270 // because that is the maximum supported decoding sampling rate.
4271 playout_frequency = 48000;
4272 }
4273 }
4274 return playout_frequency;
4275}
4276
minyue@webrtc.org2b58a442014-09-11 07:51:53 +00004277int Channel::GetRTT() const {
4278 RTCPMethod method = _rtpRtcpModule->RTCP();
4279 if (method == kRtcpOff) {
4280 WEBRTC_TRACE(kTraceWarning, kTraceVoice,
4281 VoEId(_instanceId, _channelId),
4282 "GetRTPStatistics() RTCP is disabled => valid RTT "
4283 "measurements cannot be retrieved");
4284 return 0;
4285 }
4286 std::vector<RTCPReportBlock> report_blocks;
4287 _rtpRtcpModule->RemoteRTCPStat(&report_blocks);
4288 if (report_blocks.empty()) {
4289 WEBRTC_TRACE(kTraceWarning, kTraceVoice,
4290 VoEId(_instanceId, _channelId),
4291 "GetRTPStatistics() failed to measure RTT since no "
4292 "RTCP packets have been received yet");
4293 return 0;
4294 }
4295
4296 uint32_t remoteSSRC = rtp_receiver_->SSRC();
4297 std::vector<RTCPReportBlock>::const_iterator it = report_blocks.begin();
4298 for (; it != report_blocks.end(); ++it) {
4299 if (it->remoteSSRC == remoteSSRC)
4300 break;
4301 }
4302 if (it == report_blocks.end()) {
4303 // We have not received packets with SSRC matching the report blocks.
4304 // To calculate RTT we try with the SSRC of the first report block.
4305 // This is very important for send-only channels where we don't know
4306 // the SSRC of the other end.
4307 remoteSSRC = report_blocks[0].remoteSSRC;
4308 }
4309 uint16_t rtt = 0;
4310 uint16_t avg_rtt = 0;
4311 uint16_t max_rtt= 0;
4312 uint16_t min_rtt = 0;
4313 if (_rtpRtcpModule->RTT(remoteSSRC, &rtt, &avg_rtt, &min_rtt, &max_rtt)
4314 != 0) {
4315 WEBRTC_TRACE(kTraceWarning, kTraceVoice,
4316 VoEId(_instanceId, _channelId),
4317 "GetRTPStatistics() failed to retrieve RTT from "
4318 "the RTP/RTCP module");
4319 return 0;
4320 }
4321 return static_cast<int>(rtt);
4322}
4323
pbos@webrtc.orgd900e8b2013-07-03 15:12:26 +00004324} // namespace voe
4325} // namespace webrtc