blob: a570922aca4f33e5b795a204056f32d23fb76bd4 [file] [log] [blame]
niklase@google.com470e71d2011-07-07 08:21:25 +00001/*
henrika@webrtc.org2919e952012-01-31 08:45:03 +00002 * Copyright (c) 2012 The WebRTC project authors. All Rights Reserved.
niklase@google.com470e71d2011-07-07 08:21:25 +00003 *
4 * Use of this source code is governed by a BSD-style license
5 * that can be found in the LICENSE file in the root of the source
6 * tree. An additional intellectual property rights grant can be found
7 * in the file PATENTS. All contributing project authors may
8 * be found in the AUTHORS file in the root of the source tree.
9 */
10
turaj@webrtc.org6388c3e2013-02-12 21:42:18 +000011#include "webrtc/voice_engine/channel.h"
niklase@google.com470e71d2011-07-07 08:21:25 +000012
pkasting@chromium.org4591fbd2014-11-20 22:28:14 +000013#include "webrtc/base/format_macros.h"
wu@webrtc.org94454b72014-06-05 20:34:08 +000014#include "webrtc/base/timeutils.h"
minyue@webrtc.orge509f942013-09-12 17:03:00 +000015#include "webrtc/common.h"
turaj@webrtc.org6388c3e2013-02-12 21:42:18 +000016#include "webrtc/modules/audio_device/include/audio_device.h"
17#include "webrtc/modules/audio_processing/include/audio_processing.h"
henrik.lundin@webrtc.orgd6692992014-03-20 12:04:09 +000018#include "webrtc/modules/interface/module_common_types.h"
wu@webrtc.org822fbd82013-08-15 23:38:54 +000019#include "webrtc/modules/rtp_rtcp/interface/receive_statistics.h"
20#include "webrtc/modules/rtp_rtcp/interface/rtp_payload_registry.h"
21#include "webrtc/modules/rtp_rtcp/interface/rtp_receiver.h"
22#include "webrtc/modules/rtp_rtcp/source/rtp_receiver_strategy.h"
turaj@webrtc.org6388c3e2013-02-12 21:42:18 +000023#include "webrtc/modules/utility/interface/audio_frame_operations.h"
24#include "webrtc/modules/utility/interface/process_thread.h"
25#include "webrtc/modules/utility/interface/rtp_dump.h"
26#include "webrtc/system_wrappers/interface/critical_section_wrapper.h"
27#include "webrtc/system_wrappers/interface/logging.h"
28#include "webrtc/system_wrappers/interface/trace.h"
solenberg@webrtc.orgb1f50102014-03-24 10:38:25 +000029#include "webrtc/video_engine/include/vie_network.h"
turaj@webrtc.org6388c3e2013-02-12 21:42:18 +000030#include "webrtc/voice_engine/include/voe_base.h"
31#include "webrtc/voice_engine/include/voe_external_media.h"
32#include "webrtc/voice_engine/include/voe_rtp_rtcp.h"
33#include "webrtc/voice_engine/output_mixer.h"
34#include "webrtc/voice_engine/statistics.h"
35#include "webrtc/voice_engine/transmit_mixer.h"
36#include "webrtc/voice_engine/utility.h"
niklase@google.com470e71d2011-07-07 08:21:25 +000037
38#if defined(_WIN32)
39#include <Qos.h>
40#endif
41
andrew@webrtc.org50419b02012-11-14 19:07:54 +000042namespace webrtc {
43namespace voe {
niklase@google.com470e71d2011-07-07 08:21:25 +000044
sprang@webrtc.org54ae4ff2013-12-19 13:26:02 +000045// Extend the default RTCP statistics struct with max_jitter, defined as the
46// maximum jitter value seen in an RTCP report block.
47struct ChannelStatistics : public RtcpStatistics {
48 ChannelStatistics() : rtcp(), max_jitter(0) {}
49
50 RtcpStatistics rtcp;
51 uint32_t max_jitter;
52};
53
54// Statistics callback, called at each generation of a new RTCP report block.
55class StatisticsProxy : public RtcpStatisticsCallback {
56 public:
57 StatisticsProxy(uint32_t ssrc)
58 : stats_lock_(CriticalSectionWrapper::CreateCriticalSection()),
59 ssrc_(ssrc) {}
60 virtual ~StatisticsProxy() {}
61
62 virtual void StatisticsUpdated(const RtcpStatistics& statistics,
63 uint32_t ssrc) OVERRIDE {
64 if (ssrc != ssrc_)
65 return;
66
67 CriticalSectionScoped cs(stats_lock_.get());
68 stats_.rtcp = statistics;
69 if (statistics.jitter > stats_.max_jitter) {
70 stats_.max_jitter = statistics.jitter;
71 }
72 }
73
pbos@webrtc.orgce4e9a32014-12-18 13:50:16 +000074 virtual void CNameChanged(const char* cname, uint32_t ssrc) OVERRIDE {}
75
sprang@webrtc.org54ae4ff2013-12-19 13:26:02 +000076 void ResetStatistics() {
77 CriticalSectionScoped cs(stats_lock_.get());
78 stats_ = ChannelStatistics();
79 }
80
81 ChannelStatistics GetStats() {
82 CriticalSectionScoped cs(stats_lock_.get());
83 return stats_;
84 }
85
86 private:
87 // StatisticsUpdated calls are triggered from threads in the RTP module,
88 // while GetStats calls can be triggered from the public voice engine API,
89 // hence synchronization is needed.
90 scoped_ptr<CriticalSectionWrapper> stats_lock_;
91 const uint32_t ssrc_;
92 ChannelStatistics stats_;
93};
94
minyue@webrtc.orgc1a40a72014-05-28 09:52:06 +000095class VoEBitrateObserver : public BitrateObserver {
96 public:
97 explicit VoEBitrateObserver(Channel* owner)
98 : owner_(owner) {}
99 virtual ~VoEBitrateObserver() {}
100
101 // Implements BitrateObserver.
102 virtual void OnNetworkChanged(const uint32_t bitrate_bps,
103 const uint8_t fraction_lost,
104 const uint32_t rtt) OVERRIDE {
105 // |fraction_lost| has a scale of 0 - 255.
106 owner_->OnNetworkChanged(bitrate_bps, fraction_lost, rtt);
107 }
108
109 private:
110 Channel* owner_;
111};
112
pbos@webrtc.org6141e132013-04-09 10:09:10 +0000113int32_t
niklase@google.com470e71d2011-07-07 08:21:25 +0000114Channel::SendData(FrameType frameType,
pbos@webrtc.org6141e132013-04-09 10:09:10 +0000115 uint8_t payloadType,
116 uint32_t timeStamp,
117 const uint8_t* payloadData,
pkasting@chromium.org4591fbd2014-11-20 22:28:14 +0000118 size_t payloadSize,
niklase@google.com470e71d2011-07-07 08:21:25 +0000119 const RTPFragmentationHeader* fragmentation)
120{
121 WEBRTC_TRACE(kTraceStream, kTraceVoice, VoEId(_instanceId,_channelId),
122 "Channel::SendData(frameType=%u, payloadType=%u, timeStamp=%u,"
pkasting@chromium.org4591fbd2014-11-20 22:28:14 +0000123 " payloadSize=%" PRIuS ", fragmentation=0x%x)",
124 frameType, payloadType, timeStamp,
125 payloadSize, fragmentation);
niklase@google.com470e71d2011-07-07 08:21:25 +0000126
127 if (_includeAudioLevelIndication)
128 {
129 // Store current audio level in the RTP/RTCP module.
130 // The level will be used in combination with voice-activity state
131 // (frameType) to add an RTP header extension
andrew@webrtc.org382c0c22014-05-05 18:22:21 +0000132 _rtpRtcpModule->SetAudioLevel(rms_level_.RMS());
niklase@google.com470e71d2011-07-07 08:21:25 +0000133 }
134
135 // Push data from ACM to RTP/RTCP-module to deliver audio frame for
136 // packetization.
137 // This call will trigger Transport::SendPacket() from the RTP/RTCP module.
pwestin@webrtc.org2853dde2012-05-11 11:08:54 +0000138 if (_rtpRtcpModule->SendOutgoingData((FrameType&)frameType,
niklase@google.com470e71d2011-07-07 08:21:25 +0000139 payloadType,
140 timeStamp,
stefan@webrtc.orgddfdfed2012-07-03 13:21:22 +0000141 // Leaving the time when this frame was
142 // received from the capture device as
143 // undefined for voice for now.
144 -1,
niklase@google.com470e71d2011-07-07 08:21:25 +0000145 payloadData,
146 payloadSize,
147 fragmentation) == -1)
148 {
149 _engineStatisticsPtr->SetLastError(
150 VE_RTP_RTCP_MODULE_ERROR, kTraceWarning,
151 "Channel::SendData() failed to send data to RTP/RTCP module");
152 return -1;
153 }
154
155 _lastLocalTimeStamp = timeStamp;
156 _lastPayloadType = payloadType;
157
158 return 0;
159}
160
pbos@webrtc.org6141e132013-04-09 10:09:10 +0000161int32_t
162Channel::InFrameType(int16_t frameType)
niklase@google.com470e71d2011-07-07 08:21:25 +0000163{
164 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId,_channelId),
165 "Channel::InFrameType(frameType=%d)", frameType);
166
mflodman@webrtc.org9a065d12012-03-07 08:12:21 +0000167 CriticalSectionScoped cs(&_callbackCritSect);
niklase@google.com470e71d2011-07-07 08:21:25 +0000168 // 1 indicates speech
169 _sendFrameType = (frameType == 1) ? 1 : 0;
170 return 0;
171}
172
pbos@webrtc.org6141e132013-04-09 10:09:10 +0000173int32_t
pbos@webrtc.org92135212013-05-14 08:31:39 +0000174Channel::OnRxVadDetected(int vadDecision)
niklase@google.com470e71d2011-07-07 08:21:25 +0000175{
176 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId, _channelId),
177 "Channel::OnRxVadDetected(vadDecision=%d)", vadDecision);
178
mflodman@webrtc.org9a065d12012-03-07 08:12:21 +0000179 CriticalSectionScoped cs(&_callbackCritSect);
niklase@google.com470e71d2011-07-07 08:21:25 +0000180 if (_rxVadObserverPtr)
181 {
182 _rxVadObserverPtr->OnRxVad(_channelId, vadDecision);
183 }
184
185 return 0;
186}
187
188int
pkasting@chromium.org4591fbd2014-11-20 22:28:14 +0000189Channel::SendPacket(int channel, const void *data, size_t len)
niklase@google.com470e71d2011-07-07 08:21:25 +0000190{
191 channel = VoEChannelId(channel);
192 assert(channel == _channelId);
193
194 WEBRTC_TRACE(kTraceStream, kTraceVoice, VoEId(_instanceId,_channelId),
pkasting@chromium.org4591fbd2014-11-20 22:28:14 +0000195 "Channel::SendPacket(channel=%d, len=%" PRIuS ")", channel,
196 len);
niklase@google.com470e71d2011-07-07 08:21:25 +0000197
wu@webrtc.orgfb648da2013-10-18 21:10:51 +0000198 CriticalSectionScoped cs(&_callbackCritSect);
199
niklase@google.com470e71d2011-07-07 08:21:25 +0000200 if (_transportPtr == NULL)
201 {
202 WEBRTC_TRACE(kTraceError, kTraceVoice, VoEId(_instanceId,_channelId),
203 "Channel::SendPacket() failed to send RTP packet due to"
204 " invalid transport object");
205 return -1;
206 }
207
pbos@webrtc.org6141e132013-04-09 10:09:10 +0000208 uint8_t* bufferToSendPtr = (uint8_t*)data;
pkasting@chromium.org4591fbd2014-11-20 22:28:14 +0000209 size_t bufferLength = len;
niklase@google.com470e71d2011-07-07 08:21:25 +0000210
211 // Dump the RTP packet to a file (if RTP dump is enabled).
pbos@webrtc.org6141e132013-04-09 10:09:10 +0000212 if (_rtpDumpOut.DumpPacket((const uint8_t*)data, len) == -1)
niklase@google.com470e71d2011-07-07 08:21:25 +0000213 {
214 WEBRTC_TRACE(kTraceWarning, kTraceVoice,
215 VoEId(_instanceId,_channelId),
216 "Channel::SendPacket() RTP dump to output file failed");
217 }
218
wu@webrtc.orgfb648da2013-10-18 21:10:51 +0000219 int n = _transportPtr->SendPacket(channel, bufferToSendPtr,
220 bufferLength);
221 if (n < 0) {
222 std::string transport_name =
223 _externalTransport ? "external transport" : "WebRtc sockets";
224 WEBRTC_TRACE(kTraceError, kTraceVoice,
225 VoEId(_instanceId,_channelId),
226 "Channel::SendPacket() RTP transmission using %s failed",
227 transport_name.c_str());
228 return -1;
niklase@google.com470e71d2011-07-07 08:21:25 +0000229 }
wu@webrtc.orgfb648da2013-10-18 21:10:51 +0000230 return n;
niklase@google.com470e71d2011-07-07 08:21:25 +0000231}
232
233int
pkasting@chromium.org4591fbd2014-11-20 22:28:14 +0000234Channel::SendRTCPPacket(int channel, const void *data, size_t len)
niklase@google.com470e71d2011-07-07 08:21:25 +0000235{
236 channel = VoEChannelId(channel);
237 assert(channel == _channelId);
238
239 WEBRTC_TRACE(kTraceStream, kTraceVoice, VoEId(_instanceId,_channelId),
pkasting@chromium.org4591fbd2014-11-20 22:28:14 +0000240 "Channel::SendRTCPPacket(channel=%d, len=%" PRIuS ")", channel,
241 len);
niklase@google.com470e71d2011-07-07 08:21:25 +0000242
wu@webrtc.orgfb648da2013-10-18 21:10:51 +0000243 CriticalSectionScoped cs(&_callbackCritSect);
244 if (_transportPtr == NULL)
niklase@google.com470e71d2011-07-07 08:21:25 +0000245 {
wu@webrtc.orgfb648da2013-10-18 21:10:51 +0000246 WEBRTC_TRACE(kTraceError, kTraceVoice,
247 VoEId(_instanceId,_channelId),
248 "Channel::SendRTCPPacket() failed to send RTCP packet"
249 " due to invalid transport object");
250 return -1;
niklase@google.com470e71d2011-07-07 08:21:25 +0000251 }
252
pbos@webrtc.org6141e132013-04-09 10:09:10 +0000253 uint8_t* bufferToSendPtr = (uint8_t*)data;
pkasting@chromium.org4591fbd2014-11-20 22:28:14 +0000254 size_t bufferLength = len;
niklase@google.com470e71d2011-07-07 08:21:25 +0000255
256 // Dump the RTCP packet to a file (if RTP dump is enabled).
pbos@webrtc.org6141e132013-04-09 10:09:10 +0000257 if (_rtpDumpOut.DumpPacket((const uint8_t*)data, len) == -1)
niklase@google.com470e71d2011-07-07 08:21:25 +0000258 {
259 WEBRTC_TRACE(kTraceWarning, kTraceVoice,
260 VoEId(_instanceId,_channelId),
261 "Channel::SendPacket() RTCP dump to output file failed");
262 }
263
wu@webrtc.orgfb648da2013-10-18 21:10:51 +0000264 int n = _transportPtr->SendRTCPPacket(channel,
265 bufferToSendPtr,
266 bufferLength);
267 if (n < 0) {
268 std::string transport_name =
269 _externalTransport ? "external transport" : "WebRtc sockets";
270 WEBRTC_TRACE(kTraceInfo, kTraceVoice,
271 VoEId(_instanceId,_channelId),
272 "Channel::SendRTCPPacket() transmission using %s failed",
273 transport_name.c_str());
274 return -1;
niklase@google.com470e71d2011-07-07 08:21:25 +0000275 }
wu@webrtc.orgfb648da2013-10-18 21:10:51 +0000276 return n;
niklase@google.com470e71d2011-07-07 08:21:25 +0000277}
278
279void
pbos@webrtc.org92135212013-05-14 08:31:39 +0000280Channel::OnPlayTelephoneEvent(int32_t id,
281 uint8_t event,
282 uint16_t lengthMs,
283 uint8_t volume)
niklase@google.com470e71d2011-07-07 08:21:25 +0000284{
285 WEBRTC_TRACE(kTraceStream, kTraceVoice, VoEId(_instanceId,_channelId),
286 "Channel::OnPlayTelephoneEvent(id=%d, event=%u, lengthMs=%u,"
wu@webrtc.orgfcd12b32011-09-15 20:49:50 +0000287 " volume=%u)", id, event, lengthMs, volume);
niklase@google.com470e71d2011-07-07 08:21:25 +0000288
289 if (!_playOutbandDtmfEvent || (event > 15))
290 {
291 // Ignore callback since feedback is disabled or event is not a
292 // Dtmf tone event.
293 return;
294 }
295
296 assert(_outputMixerPtr != NULL);
297
298 // Start playing out the Dtmf tone (if playout is enabled).
299 // Reduce length of tone with 80ms to the reduce risk of echo.
300 _outputMixerPtr->PlayDtmfTone(event, lengthMs - 80, volume);
301}
302
303void
stefan@webrtc.org286fe0b2013-08-21 20:58:21 +0000304Channel::OnIncomingSSRCChanged(int32_t id, uint32_t ssrc)
niklase@google.com470e71d2011-07-07 08:21:25 +0000305{
306 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId,_channelId),
307 "Channel::OnIncomingSSRCChanged(id=%d, SSRC=%d)",
stefan@webrtc.org286fe0b2013-08-21 20:58:21 +0000308 id, ssrc);
niklase@google.com470e71d2011-07-07 08:21:25 +0000309
dwkang@webrtc.orgb295a3f2013-08-29 07:34:12 +0000310 // Update ssrc so that NTP for AV sync can be updated.
311 _rtpRtcpModule->SetRemoteSSRC(ssrc);
niklase@google.com470e71d2011-07-07 08:21:25 +0000312}
313
pbos@webrtc.org92135212013-05-14 08:31:39 +0000314void Channel::OnIncomingCSRCChanged(int32_t id,
315 uint32_t CSRC,
316 bool added)
niklase@google.com470e71d2011-07-07 08:21:25 +0000317{
318 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId,_channelId),
319 "Channel::OnIncomingCSRCChanged(id=%d, CSRC=%d, added=%d)",
320 id, CSRC, added);
niklase@google.com470e71d2011-07-07 08:21:25 +0000321}
322
stefan@webrtc.org286fe0b2013-08-21 20:58:21 +0000323void Channel::ResetStatistics(uint32_t ssrc) {
324 StreamStatistician* statistician =
325 rtp_receive_statistics_->GetStatistician(ssrc);
326 if (statistician) {
327 statistician->ResetStatistics();
328 }
sprang@webrtc.org54ae4ff2013-12-19 13:26:02 +0000329 statistics_proxy_->ResetStatistics();
wu@webrtc.org822fbd82013-08-15 23:38:54 +0000330}
331
pbos@webrtc.org6141e132013-04-09 10:09:10 +0000332int32_t
niklase@google.com470e71d2011-07-07 08:21:25 +0000333Channel::OnInitializeDecoder(
pbos@webrtc.org92135212013-05-14 08:31:39 +0000334 int32_t id,
335 int8_t payloadType,
leozwang@webrtc.org813e4b02012-03-01 18:34:25 +0000336 const char payloadName[RTP_PAYLOAD_NAME_SIZE],
pbos@webrtc.org92135212013-05-14 08:31:39 +0000337 int frequency,
338 uint8_t channels,
339 uint32_t rate)
niklase@google.com470e71d2011-07-07 08:21:25 +0000340{
341 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId,_channelId),
342 "Channel::OnInitializeDecoder(id=%d, payloadType=%d, "
343 "payloadName=%s, frequency=%u, channels=%u, rate=%u)",
344 id, payloadType, payloadName, frequency, channels, rate);
345
andrew@webrtc.orgceb148c2011-08-23 17:53:54 +0000346 assert(VoEChannelId(id) == _channelId);
niklase@google.com470e71d2011-07-07 08:21:25 +0000347
henrika@webrtc.orgf75901f2012-01-16 08:45:42 +0000348 CodecInst receiveCodec = {0};
349 CodecInst dummyCodec = {0};
niklase@google.com470e71d2011-07-07 08:21:25 +0000350
351 receiveCodec.pltype = payloadType;
niklase@google.com470e71d2011-07-07 08:21:25 +0000352 receiveCodec.plfreq = frequency;
353 receiveCodec.channels = channels;
354 receiveCodec.rate = rate;
henrika@webrtc.orgf75901f2012-01-16 08:45:42 +0000355 strncpy(receiveCodec.plname, payloadName, RTP_PAYLOAD_NAME_SIZE - 1);
andrew@webrtc.orgae1a58b2013-01-22 04:44:30 +0000356
andrew@webrtc.orgeb524d92013-09-23 23:02:24 +0000357 audio_coding_->Codec(payloadName, &dummyCodec, frequency, channels);
niklase@google.com470e71d2011-07-07 08:21:25 +0000358 receiveCodec.pacsize = dummyCodec.pacsize;
359
360 // Register the new codec to the ACM
andrew@webrtc.orgeb524d92013-09-23 23:02:24 +0000361 if (audio_coding_->RegisterReceiveCodec(receiveCodec) == -1)
niklase@google.com470e71d2011-07-07 08:21:25 +0000362 {
363 WEBRTC_TRACE(kTraceWarning, kTraceVoice,
andrew@webrtc.orgceb148c2011-08-23 17:53:54 +0000364 VoEId(_instanceId, _channelId),
niklase@google.com470e71d2011-07-07 08:21:25 +0000365 "Channel::OnInitializeDecoder() invalid codec ("
366 "pt=%d, name=%s) received - 1", payloadType, payloadName);
367 _engineStatisticsPtr->SetLastError(VE_AUDIO_CODING_MODULE_ERROR);
368 return -1;
369 }
370
371 return 0;
372}
373
pbos@webrtc.org6141e132013-04-09 10:09:10 +0000374int32_t
375Channel::OnReceivedPayloadData(const uint8_t* payloadData,
pkasting@chromium.org4591fbd2014-11-20 22:28:14 +0000376 size_t payloadSize,
niklase@google.com470e71d2011-07-07 08:21:25 +0000377 const WebRtcRTPHeader* rtpHeader)
378{
379 WEBRTC_TRACE(kTraceStream, kTraceVoice, VoEId(_instanceId,_channelId),
pkasting@chromium.org4591fbd2014-11-20 22:28:14 +0000380 "Channel::OnReceivedPayloadData(payloadSize=%" PRIuS ","
niklase@google.com470e71d2011-07-07 08:21:25 +0000381 " payloadType=%u, audioChannel=%u)",
382 payloadSize,
383 rtpHeader->header.payloadType,
384 rtpHeader->type.Audio.channel);
385
henrika@webrtc.org944cbeb2014-03-18 10:32:33 +0000386 if (!channel_state_.Get().playing)
niklase@google.com470e71d2011-07-07 08:21:25 +0000387 {
388 // Avoid inserting into NetEQ when we are not playing. Count the
389 // packet as discarded.
390 WEBRTC_TRACE(kTraceStream, kTraceVoice,
391 VoEId(_instanceId, _channelId),
392 "received packet is discarded since playing is not"
393 " activated");
394 _numberOfDiscardedPackets++;
395 return 0;
396 }
397
398 // Push the incoming payload (parsed and ready for decoding) into the ACM
andrew@webrtc.orgeb524d92013-09-23 23:02:24 +0000399 if (audio_coding_->IncomingPacket(payloadData,
400 payloadSize,
401 *rtpHeader) != 0)
niklase@google.com470e71d2011-07-07 08:21:25 +0000402 {
403 _engineStatisticsPtr->SetLastError(
404 VE_AUDIO_CODING_MODULE_ERROR, kTraceWarning,
405 "Channel::OnReceivedPayloadData() unable to push data to the ACM");
406 return -1;
407 }
408
pwestin@webrtc.orgd30859e2013-06-06 21:09:01 +0000409 // Update the packet delay.
niklase@google.com470e71d2011-07-07 08:21:25 +0000410 UpdatePacketDelay(rtpHeader->header.timestamp,
411 rtpHeader->header.sequenceNumber);
pwestin@webrtc.orgd30859e2013-06-06 21:09:01 +0000412
wu@webrtc.org822fbd82013-08-15 23:38:54 +0000413 uint16_t round_trip_time = 0;
414 _rtpRtcpModule->RTT(rtp_receiver_->SSRC(), &round_trip_time,
415 NULL, NULL, NULL);
pwestin@webrtc.orgd30859e2013-06-06 21:09:01 +0000416
andrew@webrtc.orgeb524d92013-09-23 23:02:24 +0000417 std::vector<uint16_t> nack_list = audio_coding_->GetNackList(
wu@webrtc.org822fbd82013-08-15 23:38:54 +0000418 round_trip_time);
419 if (!nack_list.empty()) {
420 // Can't use nack_list.data() since it's not supported by all
421 // compilers.
422 ResendPackets(&(nack_list[0]), static_cast<int>(nack_list.size()));
pwestin@webrtc.orgd30859e2013-06-06 21:09:01 +0000423 }
niklase@google.com470e71d2011-07-07 08:21:25 +0000424 return 0;
425}
426
stefan@webrtc.org7bb8f022013-09-06 13:40:11 +0000427bool Channel::OnRecoveredPacket(const uint8_t* rtp_packet,
pkasting@chromium.org4591fbd2014-11-20 22:28:14 +0000428 size_t rtp_packet_length) {
stefan@webrtc.org7bb8f022013-09-06 13:40:11 +0000429 RTPHeader header;
430 if (!rtp_header_parser_->Parse(rtp_packet, rtp_packet_length, &header)) {
431 WEBRTC_TRACE(kTraceDebug, webrtc::kTraceVoice, _channelId,
432 "IncomingPacket invalid RTP header");
433 return false;
434 }
435 header.payload_type_frequency =
436 rtp_payload_registry_->GetPayloadTypeFrequency(header.payloadType);
437 if (header.payload_type_frequency < 0)
438 return false;
439 return ReceivePacket(rtp_packet, rtp_packet_length, header, false);
440}
441
pbos@webrtc.org92135212013-05-14 08:31:39 +0000442int32_t Channel::GetAudioFrame(int32_t id, AudioFrame& audioFrame)
niklase@google.com470e71d2011-07-07 08:21:25 +0000443{
444 WEBRTC_TRACE(kTraceStream, kTraceVoice, VoEId(_instanceId,_channelId),
445 "Channel::GetAudioFrame(id=%d)", id);
446
447 // Get 10ms raw PCM data from the ACM (mixer limits output frequency)
andrew@webrtc.orgeb524d92013-09-23 23:02:24 +0000448 if (audio_coding_->PlayoutData10Ms(audioFrame.sample_rate_hz_,
449 &audioFrame) == -1)
niklase@google.com470e71d2011-07-07 08:21:25 +0000450 {
451 WEBRTC_TRACE(kTraceError, kTraceVoice,
452 VoEId(_instanceId,_channelId),
453 "Channel::GetAudioFrame() PlayoutData10Ms() failed!");
andrew@webrtc.org7859e102012-01-13 00:30:11 +0000454 // In all likelihood, the audio in this frame is garbage. We return an
455 // error so that the audio mixer module doesn't add it to the mix. As
456 // a result, it won't be played out and the actions skipped here are
457 // irrelevant.
458 return -1;
niklase@google.com470e71d2011-07-07 08:21:25 +0000459 }
460
461 if (_RxVadDetection)
462 {
463 UpdateRxVadDetection(audioFrame);
464 }
465
466 // Convert module ID to internal VoE channel ID
andrew@webrtc.org63a50982012-05-02 23:56:37 +0000467 audioFrame.id_ = VoEChannelId(audioFrame.id_);
niklase@google.com470e71d2011-07-07 08:21:25 +0000468 // Store speech type for dead-or-alive detection
andrew@webrtc.org63a50982012-05-02 23:56:37 +0000469 _outputSpeechType = audioFrame.speech_type_;
niklase@google.com470e71d2011-07-07 08:21:25 +0000470
henrika@webrtc.org944cbeb2014-03-18 10:32:33 +0000471 ChannelState::State state = channel_state_.Get();
472
473 if (state.rx_apm_is_enabled) {
andrew@webrtc.org60730cf2014-01-07 17:45:09 +0000474 int err = rx_audioproc_->ProcessStream(&audioFrame);
475 if (err) {
476 LOG(LS_ERROR) << "ProcessStream() error: " << err;
477 assert(false);
478 }
niklase@google.com470e71d2011-07-07 08:21:25 +0000479 }
480
wu@webrtc.org63420662013-10-17 18:28:55 +0000481 float output_gain = 1.0f;
482 float left_pan = 1.0f;
483 float right_pan = 1.0f;
niklase@google.com470e71d2011-07-07 08:21:25 +0000484 {
wu@webrtc.org63420662013-10-17 18:28:55 +0000485 CriticalSectionScoped cs(&volume_settings_critsect_);
486 output_gain = _outputGain;
487 left_pan = _panLeft;
488 right_pan= _panRight;
489 }
490
491 // Output volume scaling
492 if (output_gain < 0.99f || output_gain > 1.01f)
493 {
494 AudioFrameOperations::ScaleWithSat(output_gain, audioFrame);
niklase@google.com470e71d2011-07-07 08:21:25 +0000495 }
496
497 // Scale left and/or right channel(s) if stereo and master balance is
498 // active
499
wu@webrtc.org63420662013-10-17 18:28:55 +0000500 if (left_pan != 1.0f || right_pan != 1.0f)
niklase@google.com470e71d2011-07-07 08:21:25 +0000501 {
andrew@webrtc.org63a50982012-05-02 23:56:37 +0000502 if (audioFrame.num_channels_ == 1)
niklase@google.com470e71d2011-07-07 08:21:25 +0000503 {
504 // Emulate stereo mode since panning is active.
505 // The mono signal is copied to both left and right channels here.
andrew@webrtc.org4ecea3e2012-06-27 03:25:31 +0000506 AudioFrameOperations::MonoToStereo(&audioFrame);
niklase@google.com470e71d2011-07-07 08:21:25 +0000507 }
508 // For true stereo mode (when we are receiving a stereo signal), no
509 // action is needed.
510
511 // Do the panning operation (the audio frame contains stereo at this
512 // stage)
wu@webrtc.org63420662013-10-17 18:28:55 +0000513 AudioFrameOperations::Scale(left_pan, right_pan, audioFrame);
niklase@google.com470e71d2011-07-07 08:21:25 +0000514 }
515
516 // Mix decoded PCM output with file if file mixing is enabled
henrika@webrtc.org944cbeb2014-03-18 10:32:33 +0000517 if (state.output_file_playing)
niklase@google.com470e71d2011-07-07 08:21:25 +0000518 {
andrew@webrtc.org63a50982012-05-02 23:56:37 +0000519 MixAudioWithFile(audioFrame, audioFrame.sample_rate_hz_);
niklase@google.com470e71d2011-07-07 08:21:25 +0000520 }
521
niklase@google.com470e71d2011-07-07 08:21:25 +0000522 // External media
523 if (_outputExternalMedia)
524 {
mflodman@webrtc.org9a065d12012-03-07 08:12:21 +0000525 CriticalSectionScoped cs(&_callbackCritSect);
andrew@webrtc.org63a50982012-05-02 23:56:37 +0000526 const bool isStereo = (audioFrame.num_channels_ == 2);
niklase@google.com470e71d2011-07-07 08:21:25 +0000527 if (_outputExternalMediaCallbackPtr)
528 {
529 _outputExternalMediaCallbackPtr->Process(
530 _channelId,
531 kPlaybackPerChannel,
pbos@webrtc.org6141e132013-04-09 10:09:10 +0000532 (int16_t*)audioFrame.data_,
andrew@webrtc.org63a50982012-05-02 23:56:37 +0000533 audioFrame.samples_per_channel_,
534 audioFrame.sample_rate_hz_,
niklase@google.com470e71d2011-07-07 08:21:25 +0000535 isStereo);
536 }
537 }
538
539 // Record playout if enabled
540 {
mflodman@webrtc.org9a065d12012-03-07 08:12:21 +0000541 CriticalSectionScoped cs(&_fileCritSect);
niklase@google.com470e71d2011-07-07 08:21:25 +0000542
543 if (_outputFileRecording && _outputFileRecorderPtr)
544 {
niklas.enbom@webrtc.org5398d952012-03-26 08:11:25 +0000545 _outputFileRecorderPtr->RecordAudioToFile(audioFrame);
niklase@google.com470e71d2011-07-07 08:21:25 +0000546 }
547 }
548
549 // Measure audio level (0-9)
550 _outputAudioLevel.ComputeLevel(audioFrame);
551
wu@webrtc.org94454b72014-06-05 20:34:08 +0000552 if (capture_start_rtp_time_stamp_ < 0 && audioFrame.timestamp_ != 0) {
553 // The first frame with a valid rtp timestamp.
wu@webrtc.orgcb711f72014-05-19 17:39:11 +0000554 capture_start_rtp_time_stamp_ = audioFrame.timestamp_;
wu@webrtc.org94454b72014-06-05 20:34:08 +0000555 }
556
557 if (capture_start_rtp_time_stamp_ >= 0) {
558 // audioFrame.timestamp_ should be valid from now on.
559
560 // Compute elapsed time.
561 int64_t unwrap_timestamp =
562 rtp_ts_wraparound_handler_->Unwrap(audioFrame.timestamp_);
563 audioFrame.elapsed_time_ms_ =
564 (unwrap_timestamp - capture_start_rtp_time_stamp_) /
565 (GetPlayoutFrequency() / 1000);
566
stefan@webrtc.org8e24d872014-09-02 18:58:24 +0000567 {
wu@webrtc.orgcb711f72014-05-19 17:39:11 +0000568 CriticalSectionScoped lock(ts_stats_lock_.get());
stefan@webrtc.org8e24d872014-09-02 18:58:24 +0000569 // Compute ntp time.
570 audioFrame.ntp_time_ms_ = ntp_estimator_.Estimate(
571 audioFrame.timestamp_);
572 // |ntp_time_ms_| won't be valid until at least 2 RTCP SRs are received.
573 if (audioFrame.ntp_time_ms_ > 0) {
574 // Compute |capture_start_ntp_time_ms_| so that
575 // |capture_start_ntp_time_ms_| + |elapsed_time_ms_| == |ntp_time_ms_|
576 capture_start_ntp_time_ms_ =
577 audioFrame.ntp_time_ms_ - audioFrame.elapsed_time_ms_;
578 }
wu@webrtc.orgcb711f72014-05-19 17:39:11 +0000579 }
580 }
581
niklase@google.com470e71d2011-07-07 08:21:25 +0000582 return 0;
583}
584
pbos@webrtc.org6141e132013-04-09 10:09:10 +0000585int32_t
pbos@webrtc.org92135212013-05-14 08:31:39 +0000586Channel::NeededFrequency(int32_t id)
niklase@google.com470e71d2011-07-07 08:21:25 +0000587{
588 WEBRTC_TRACE(kTraceStream, kTraceVoice, VoEId(_instanceId,_channelId),
589 "Channel::NeededFrequency(id=%d)", id);
590
591 int highestNeeded = 0;
592
593 // Determine highest needed receive frequency
andrew@webrtc.orgeb524d92013-09-23 23:02:24 +0000594 int32_t receiveFrequency = audio_coding_->ReceiveFrequency();
niklase@google.com470e71d2011-07-07 08:21:25 +0000595
596 // Return the bigger of playout and receive frequency in the ACM.
andrew@webrtc.orgeb524d92013-09-23 23:02:24 +0000597 if (audio_coding_->PlayoutFrequency() > receiveFrequency)
niklase@google.com470e71d2011-07-07 08:21:25 +0000598 {
andrew@webrtc.orgeb524d92013-09-23 23:02:24 +0000599 highestNeeded = audio_coding_->PlayoutFrequency();
niklase@google.com470e71d2011-07-07 08:21:25 +0000600 }
601 else
602 {
603 highestNeeded = receiveFrequency;
604 }
605
606 // Special case, if we're playing a file on the playout side
607 // we take that frequency into consideration as well
608 // This is not needed on sending side, since the codec will
609 // limit the spectrum anyway.
henrika@webrtc.org944cbeb2014-03-18 10:32:33 +0000610 if (channel_state_.Get().output_file_playing)
niklase@google.com470e71d2011-07-07 08:21:25 +0000611 {
mflodman@webrtc.org9a065d12012-03-07 08:12:21 +0000612 CriticalSectionScoped cs(&_fileCritSect);
henrika@webrtc.org944cbeb2014-03-18 10:32:33 +0000613 if (_outputFilePlayerPtr)
niklase@google.com470e71d2011-07-07 08:21:25 +0000614 {
615 if(_outputFilePlayerPtr->Frequency()>highestNeeded)
616 {
617 highestNeeded=_outputFilePlayerPtr->Frequency();
618 }
619 }
620 }
621
622 return(highestNeeded);
623}
624
pbos@webrtc.org6141e132013-04-09 10:09:10 +0000625int32_t
niklase@google.com470e71d2011-07-07 08:21:25 +0000626Channel::CreateChannel(Channel*& channel,
pbos@webrtc.org92135212013-05-14 08:31:39 +0000627 int32_t channelId,
minyue@webrtc.orge509f942013-09-12 17:03:00 +0000628 uint32_t instanceId,
629 const Config& config)
niklase@google.com470e71d2011-07-07 08:21:25 +0000630{
631 WEBRTC_TRACE(kTraceMemory, kTraceVoice, VoEId(instanceId,channelId),
632 "Channel::CreateChannel(channelId=%d, instanceId=%d)",
633 channelId, instanceId);
634
minyue@webrtc.orge509f942013-09-12 17:03:00 +0000635 channel = new Channel(channelId, instanceId, config);
niklase@google.com470e71d2011-07-07 08:21:25 +0000636 if (channel == NULL)
637 {
638 WEBRTC_TRACE(kTraceMemory, kTraceVoice,
639 VoEId(instanceId,channelId),
640 "Channel::CreateChannel() unable to allocate memory for"
641 " channel");
642 return -1;
643 }
644 return 0;
645}
646
647void
pbos@webrtc.org92135212013-05-14 08:31:39 +0000648Channel::PlayNotification(int32_t id, uint32_t durationMs)
niklase@google.com470e71d2011-07-07 08:21:25 +0000649{
650 WEBRTC_TRACE(kTraceStream, kTraceVoice, VoEId(_instanceId,_channelId),
651 "Channel::PlayNotification(id=%d, durationMs=%d)",
652 id, durationMs);
653
654 // Not implement yet
655}
656
657void
pbos@webrtc.org92135212013-05-14 08:31:39 +0000658Channel::RecordNotification(int32_t id, uint32_t durationMs)
niklase@google.com470e71d2011-07-07 08:21:25 +0000659{
660 WEBRTC_TRACE(kTraceStream, kTraceVoice, VoEId(_instanceId,_channelId),
661 "Channel::RecordNotification(id=%d, durationMs=%d)",
662 id, durationMs);
663
664 // Not implement yet
665}
666
667void
pbos@webrtc.org92135212013-05-14 08:31:39 +0000668Channel::PlayFileEnded(int32_t id)
niklase@google.com470e71d2011-07-07 08:21:25 +0000669{
670 WEBRTC_TRACE(kTraceStream, kTraceVoice, VoEId(_instanceId,_channelId),
671 "Channel::PlayFileEnded(id=%d)", id);
672
673 if (id == _inputFilePlayerId)
674 {
henrika@webrtc.org944cbeb2014-03-18 10:32:33 +0000675 channel_state_.SetInputFilePlaying(false);
niklase@google.com470e71d2011-07-07 08:21:25 +0000676 WEBRTC_TRACE(kTraceStateInfo, kTraceVoice,
677 VoEId(_instanceId,_channelId),
678 "Channel::PlayFileEnded() => input file player module is"
679 " shutdown");
680 }
681 else if (id == _outputFilePlayerId)
682 {
henrika@webrtc.org944cbeb2014-03-18 10:32:33 +0000683 channel_state_.SetOutputFilePlaying(false);
niklase@google.com470e71d2011-07-07 08:21:25 +0000684 WEBRTC_TRACE(kTraceStateInfo, kTraceVoice,
685 VoEId(_instanceId,_channelId),
686 "Channel::PlayFileEnded() => output file player module is"
687 " shutdown");
688 }
689}
690
691void
pbos@webrtc.org92135212013-05-14 08:31:39 +0000692Channel::RecordFileEnded(int32_t id)
niklase@google.com470e71d2011-07-07 08:21:25 +0000693{
694 WEBRTC_TRACE(kTraceStream, kTraceVoice, VoEId(_instanceId,_channelId),
695 "Channel::RecordFileEnded(id=%d)", id);
696
697 assert(id == _outputFileRecorderId);
698
mflodman@webrtc.org9a065d12012-03-07 08:12:21 +0000699 CriticalSectionScoped cs(&_fileCritSect);
niklase@google.com470e71d2011-07-07 08:21:25 +0000700
701 _outputFileRecording = false;
702 WEBRTC_TRACE(kTraceStateInfo, kTraceVoice,
703 VoEId(_instanceId,_channelId),
704 "Channel::RecordFileEnded() => output file recorder module is"
705 " shutdown");
706}
707
pbos@webrtc.org92135212013-05-14 08:31:39 +0000708Channel::Channel(int32_t channelId,
minyue@webrtc.orge509f942013-09-12 17:03:00 +0000709 uint32_t instanceId,
710 const Config& config) :
niklase@google.com470e71d2011-07-07 08:21:25 +0000711 _fileCritSect(*CriticalSectionWrapper::CreateCriticalSection()),
712 _callbackCritSect(*CriticalSectionWrapper::CreateCriticalSection()),
wu@webrtc.org63420662013-10-17 18:28:55 +0000713 volume_settings_critsect_(*CriticalSectionWrapper::CreateCriticalSection()),
niklase@google.com470e71d2011-07-07 08:21:25 +0000714 _instanceId(instanceId),
xians@google.com22963ab2011-08-03 12:40:23 +0000715 _channelId(channelId),
stefan@webrtc.orga5cb98c2013-05-29 12:12:51 +0000716 rtp_header_parser_(RtpHeaderParser::Create()),
wu@webrtc.org822fbd82013-08-15 23:38:54 +0000717 rtp_payload_registry_(
andresp@webrtc.orgdc80bae2014-04-08 11:06:12 +0000718 new RTPPayloadRegistry(RTPPayloadStrategy::CreateStrategy(true))),
wu@webrtc.org822fbd82013-08-15 23:38:54 +0000719 rtp_receive_statistics_(ReceiveStatistics::Create(
720 Clock::GetRealTimeClock())),
721 rtp_receiver_(RtpReceiver::CreateAudioReceiver(
722 VoEModuleId(instanceId, channelId), Clock::GetRealTimeClock(), this,
723 this, this, rtp_payload_registry_.get())),
724 telephone_event_handler_(rtp_receiver_->GetTelephoneEventHandler()),
henrik.lundin@webrtc.org34fe0152014-04-22 19:04:34 +0000725 audio_coding_(AudioCodingModule::Create(
xians@google.com22963ab2011-08-03 12:40:23 +0000726 VoEModuleId(instanceId, channelId))),
niklase@google.com470e71d2011-07-07 08:21:25 +0000727 _rtpDumpIn(*RtpDump::CreateRtpDump()),
728 _rtpDumpOut(*RtpDump::CreateRtpDump()),
niklase@google.com470e71d2011-07-07 08:21:25 +0000729 _outputAudioLevel(),
niklase@google.com470e71d2011-07-07 08:21:25 +0000730 _externalTransport(false),
niklase@google.com470e71d2011-07-07 08:21:25 +0000731 _inputFilePlayerPtr(NULL),
732 _outputFilePlayerPtr(NULL),
733 _outputFileRecorderPtr(NULL),
734 // Avoid conflict with other channels by adding 1024 - 1026,
735 // won't use as much as 1024 channels.
736 _inputFilePlayerId(VoEModuleId(instanceId, channelId) + 1024),
737 _outputFilePlayerId(VoEModuleId(instanceId, channelId) + 1025),
738 _outputFileRecorderId(VoEModuleId(instanceId, channelId) + 1026),
niklase@google.com470e71d2011-07-07 08:21:25 +0000739 _outputFileRecording(false),
xians@google.com22963ab2011-08-03 12:40:23 +0000740 _inbandDtmfQueue(VoEModuleId(instanceId, channelId)),
741 _inbandDtmfGenerator(VoEModuleId(instanceId, channelId)),
xians@google.com22963ab2011-08-03 12:40:23 +0000742 _outputExternalMedia(false),
niklase@google.com470e71d2011-07-07 08:21:25 +0000743 _inputExternalMediaCallbackPtr(NULL),
744 _outputExternalMediaCallbackPtr(NULL),
xians@google.com22963ab2011-08-03 12:40:23 +0000745 _timeStamp(0), // This is just an offset, RTP module will add it's own random offset
746 _sendTelephoneEventPayloadType(106),
stefan@webrtc.org8e24d872014-09-02 18:58:24 +0000747 ntp_estimator_(Clock::GetRealTimeClock()),
turaj@webrtc.org167b6df2013-12-13 21:05:07 +0000748 jitter_buffer_playout_timestamp_(0),
pwestin@webrtc.org1de01352013-04-11 20:23:35 +0000749 playout_timestamp_rtp_(0),
750 playout_timestamp_rtcp_(0),
sprang@webrtc.org54ae4ff2013-12-19 13:26:02 +0000751 playout_delay_ms_(0),
xians@google.com22963ab2011-08-03 12:40:23 +0000752 _numberOfDiscardedPackets(0),
xians@webrtc.org09e8c472013-07-31 16:30:19 +0000753 send_sequence_number_(0),
wu@webrtc.orgcb711f72014-05-19 17:39:11 +0000754 ts_stats_lock_(CriticalSectionWrapper::CreateCriticalSection()),
wu@webrtc.org94454b72014-06-05 20:34:08 +0000755 rtp_ts_wraparound_handler_(new rtc::TimestampWrapAroundHandler()),
756 capture_start_rtp_time_stamp_(-1),
wu@webrtc.orgcb711f72014-05-19 17:39:11 +0000757 capture_start_ntp_time_ms_(-1),
xians@google.com22963ab2011-08-03 12:40:23 +0000758 _engineStatisticsPtr(NULL),
henrika@webrtc.org2919e952012-01-31 08:45:03 +0000759 _outputMixerPtr(NULL),
760 _transmitMixerPtr(NULL),
xians@google.com22963ab2011-08-03 12:40:23 +0000761 _moduleProcessThreadPtr(NULL),
762 _audioDeviceModulePtr(NULL),
763 _voiceEngineObserverPtr(NULL),
764 _callbackCritSectPtr(NULL),
765 _transportPtr(NULL),
xians@google.com22963ab2011-08-03 12:40:23 +0000766 _rxVadObserverPtr(NULL),
767 _oldVadDecision(-1),
768 _sendFrameType(0),
roosa@google.com1b60ceb2012-12-12 23:00:29 +0000769 _externalMixing(false),
xians@google.com22963ab2011-08-03 12:40:23 +0000770 _mixFileWithMicrophone(false),
niklase@google.com470e71d2011-07-07 08:21:25 +0000771 _mute(false),
772 _panLeft(1.0f),
773 _panRight(1.0f),
774 _outputGain(1.0f),
775 _playOutbandDtmfEvent(false),
776 _playInbandDtmfEvent(false),
niklase@google.com470e71d2011-07-07 08:21:25 +0000777 _lastLocalTimeStamp(0),
778 _lastPayloadType(0),
xians@google.com22963ab2011-08-03 12:40:23 +0000779 _includeAudioLevelIndication(false),
niklase@google.com470e71d2011-07-07 08:21:25 +0000780 _outputSpeechType(AudioFrame::kNormalSpeech),
solenberg@webrtc.orgb1f50102014-03-24 10:38:25 +0000781 vie_network_(NULL),
782 video_channel_(-1),
pwestin@webrtc.org1de01352013-04-11 20:23:35 +0000783 _average_jitter_buffer_delay_us(0),
turaj@webrtc.orge46c8d32013-05-22 20:39:43 +0000784 least_required_delay_ms_(0),
niklase@google.com470e71d2011-07-07 08:21:25 +0000785 _previousTimestamp(0),
786 _recPacketDelayMs(20),
787 _RxVadDetection(false),
niklase@google.com470e71d2011-07-07 08:21:25 +0000788 _rxAgcIsEnabled(false),
stefan@webrtc.org7bb8f022013-09-06 13:40:11 +0000789 _rxNsIsEnabled(false),
minyue@webrtc.orgc1a40a72014-05-28 09:52:06 +0000790 restored_packet_in_use_(false),
791 bitrate_controller_(
792 BitrateController::CreateBitrateController(Clock::GetRealTimeClock(),
793 true)),
794 rtcp_bandwidth_observer_(
795 bitrate_controller_->CreateRtcpBandwidthObserver()),
minyue@webrtc.org74aaf292014-07-16 21:28:26 +0000796 send_bitrate_observer_(new VoEBitrateObserver(this)),
797 network_predictor_(new NetworkPredictor(Clock::GetRealTimeClock()))
niklase@google.com470e71d2011-07-07 08:21:25 +0000798{
799 WEBRTC_TRACE(kTraceMemory, kTraceVoice, VoEId(_instanceId,_channelId),
800 "Channel::Channel() - ctor");
801 _inbandDtmfQueue.ResetDtmf();
802 _inbandDtmfGenerator.Init();
803 _outputAudioLevel.Clear();
804
pwestin@webrtc.org2853dde2012-05-11 11:08:54 +0000805 RtpRtcp::Configuration configuration;
806 configuration.id = VoEModuleId(instanceId, channelId);
807 configuration.audio = true;
pwestin@webrtc.org2853dde2012-05-11 11:08:54 +0000808 configuration.outgoing_transport = this;
pwestin@webrtc.org2853dde2012-05-11 11:08:54 +0000809 configuration.audio_messages = this;
wu@webrtc.org822fbd82013-08-15 23:38:54 +0000810 configuration.receive_statistics = rtp_receive_statistics_.get();
minyue@webrtc.orgc1a40a72014-05-28 09:52:06 +0000811 configuration.bandwidth_callback = rtcp_bandwidth_observer_.get();
pwestin@webrtc.org2853dde2012-05-11 11:08:54 +0000812
813 _rtpRtcpModule.reset(RtpRtcp::CreateRtpRtcp(configuration));
sprang@webrtc.org54ae4ff2013-12-19 13:26:02 +0000814
815 statistics_proxy_.reset(new StatisticsProxy(_rtpRtcpModule->SSRC()));
816 rtp_receive_statistics_->RegisterRtcpStatisticsCallback(
817 statistics_proxy_.get());
aluebs@webrtc.orgf927fd62014-04-16 11:58:18 +0000818
819 Config audioproc_config;
820 audioproc_config.Set<ExperimentalAgc>(new ExperimentalAgc(false));
821 rx_audioproc_.reset(AudioProcessing::Create(audioproc_config));
niklase@google.com470e71d2011-07-07 08:21:25 +0000822}
823
824Channel::~Channel()
825{
sprang@webrtc.org54ae4ff2013-12-19 13:26:02 +0000826 rtp_receive_statistics_->RegisterRtcpStatisticsCallback(NULL);
niklase@google.com470e71d2011-07-07 08:21:25 +0000827 WEBRTC_TRACE(kTraceMemory, kTraceVoice, VoEId(_instanceId,_channelId),
828 "Channel::~Channel() - dtor");
829
830 if (_outputExternalMedia)
831 {
832 DeRegisterExternalMediaProcessing(kPlaybackPerChannel);
833 }
henrika@webrtc.org944cbeb2014-03-18 10:32:33 +0000834 if (channel_state_.Get().input_external_media)
niklase@google.com470e71d2011-07-07 08:21:25 +0000835 {
836 DeRegisterExternalMediaProcessing(kRecordingPerChannel);
837 }
838 StopSend();
niklase@google.com470e71d2011-07-07 08:21:25 +0000839 StopPlayout();
840
841 {
mflodman@webrtc.org9a065d12012-03-07 08:12:21 +0000842 CriticalSectionScoped cs(&_fileCritSect);
niklase@google.com470e71d2011-07-07 08:21:25 +0000843 if (_inputFilePlayerPtr)
844 {
845 _inputFilePlayerPtr->RegisterModuleFileCallback(NULL);
846 _inputFilePlayerPtr->StopPlayingFile();
847 FilePlayer::DestroyFilePlayer(_inputFilePlayerPtr);
848 _inputFilePlayerPtr = NULL;
849 }
850 if (_outputFilePlayerPtr)
851 {
852 _outputFilePlayerPtr->RegisterModuleFileCallback(NULL);
853 _outputFilePlayerPtr->StopPlayingFile();
854 FilePlayer::DestroyFilePlayer(_outputFilePlayerPtr);
855 _outputFilePlayerPtr = NULL;
856 }
857 if (_outputFileRecorderPtr)
858 {
859 _outputFileRecorderPtr->RegisterModuleFileCallback(NULL);
860 _outputFileRecorderPtr->StopRecording();
861 FileRecorder::DestroyFileRecorder(_outputFileRecorderPtr);
862 _outputFileRecorderPtr = NULL;
863 }
864 }
865
866 // The order to safely shutdown modules in a channel is:
867 // 1. De-register callbacks in modules
868 // 2. De-register modules in process thread
869 // 3. Destroy modules
andrew@webrtc.orgeb524d92013-09-23 23:02:24 +0000870 if (audio_coding_->RegisterTransportCallback(NULL) == -1)
niklase@google.com470e71d2011-07-07 08:21:25 +0000871 {
872 WEBRTC_TRACE(kTraceWarning, kTraceVoice,
873 VoEId(_instanceId,_channelId),
874 "~Channel() failed to de-register transport callback"
875 " (Audio coding module)");
876 }
andrew@webrtc.orgeb524d92013-09-23 23:02:24 +0000877 if (audio_coding_->RegisterVADCallback(NULL) == -1)
niklase@google.com470e71d2011-07-07 08:21:25 +0000878 {
879 WEBRTC_TRACE(kTraceWarning, kTraceVoice,
880 VoEId(_instanceId,_channelId),
881 "~Channel() failed to de-register VAD callback"
882 " (Audio coding module)");
883 }
niklase@google.com470e71d2011-07-07 08:21:25 +0000884 // De-register modules in process thread
pwestin@webrtc.org2853dde2012-05-11 11:08:54 +0000885 if (_moduleProcessThreadPtr->DeRegisterModule(_rtpRtcpModule.get()) == -1)
niklase@google.com470e71d2011-07-07 08:21:25 +0000886 {
887 WEBRTC_TRACE(kTraceInfo, kTraceVoice,
888 VoEId(_instanceId,_channelId),
889 "~Channel() failed to deregister RTP/RTCP module");
890 }
niklase@google.com470e71d2011-07-07 08:21:25 +0000891 // End of modules shutdown
892
893 // Delete other objects
solenberg@webrtc.orgb1f50102014-03-24 10:38:25 +0000894 if (vie_network_) {
895 vie_network_->Release();
896 vie_network_ = NULL;
897 }
niklase@google.com470e71d2011-07-07 08:21:25 +0000898 RtpDump::DestroyRtpDump(&_rtpDumpIn);
899 RtpDump::DestroyRtpDump(&_rtpDumpOut);
niklase@google.com470e71d2011-07-07 08:21:25 +0000900 delete &_callbackCritSect;
niklase@google.com470e71d2011-07-07 08:21:25 +0000901 delete &_fileCritSect;
wu@webrtc.org63420662013-10-17 18:28:55 +0000902 delete &volume_settings_critsect_;
niklase@google.com470e71d2011-07-07 08:21:25 +0000903}
904
pbos@webrtc.org6141e132013-04-09 10:09:10 +0000905int32_t
niklase@google.com470e71d2011-07-07 08:21:25 +0000906Channel::Init()
907{
908 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId,_channelId),
909 "Channel::Init()");
910
henrika@webrtc.org944cbeb2014-03-18 10:32:33 +0000911 channel_state_.Reset();
912
niklase@google.com470e71d2011-07-07 08:21:25 +0000913 // --- Initial sanity
914
915 if ((_engineStatisticsPtr == NULL) ||
916 (_moduleProcessThreadPtr == NULL))
917 {
918 WEBRTC_TRACE(kTraceError, kTraceVoice,
919 VoEId(_instanceId,_channelId),
920 "Channel::Init() must call SetEngineInformation() first");
921 return -1;
922 }
923
924 // --- Add modules to process thread (for periodic schedulation)
925
926 const bool processThreadFail =
pwestin@webrtc.org2853dde2012-05-11 11:08:54 +0000927 ((_moduleProcessThreadPtr->RegisterModule(_rtpRtcpModule.get()) != 0) ||
niklase@google.com470e71d2011-07-07 08:21:25 +0000928 false);
niklase@google.com470e71d2011-07-07 08:21:25 +0000929 if (processThreadFail)
930 {
931 _engineStatisticsPtr->SetLastError(
932 VE_CANNOT_INIT_CHANNEL, kTraceError,
933 "Channel::Init() modules not registered");
934 return -1;
935 }
pwestin@webrtc.orgc450a192012-01-04 15:00:12 +0000936 // --- ACM initialization
niklase@google.com470e71d2011-07-07 08:21:25 +0000937
andrew@webrtc.orgeb524d92013-09-23 23:02:24 +0000938 if ((audio_coding_->InitializeReceiver() == -1) ||
niklase@google.com470e71d2011-07-07 08:21:25 +0000939#ifdef WEBRTC_CODEC_AVT
940 // out-of-band Dtmf tones are played out by default
andrew@webrtc.orgeb524d92013-09-23 23:02:24 +0000941 (audio_coding_->SetDtmfPlayoutStatus(true) == -1) ||
niklase@google.com470e71d2011-07-07 08:21:25 +0000942#endif
andrew@webrtc.orgeb524d92013-09-23 23:02:24 +0000943 (audio_coding_->InitializeSender() == -1))
niklase@google.com470e71d2011-07-07 08:21:25 +0000944 {
945 _engineStatisticsPtr->SetLastError(
946 VE_AUDIO_CODING_MODULE_ERROR, kTraceError,
947 "Channel::Init() unable to initialize the ACM - 1");
948 return -1;
949 }
950
951 // --- RTP/RTCP module initialization
952
953 // Ensure that RTCP is enabled by default for the created channel.
954 // Note that, the module will keep generating RTCP until it is explicitly
955 // disabled by the user.
956 // After StopListen (when no sockets exists), RTCP packets will no longer
957 // be transmitted since the Transport object will then be invalid.
wu@webrtc.org822fbd82013-08-15 23:38:54 +0000958 telephone_event_handler_->SetTelephoneEventForwardToDecoder(true);
959 // RTCP is enabled by default.
960 if (_rtpRtcpModule->SetRTCPStatus(kRtcpCompound) == -1)
niklase@google.com470e71d2011-07-07 08:21:25 +0000961 {
962 _engineStatisticsPtr->SetLastError(
963 VE_RTP_RTCP_MODULE_ERROR, kTraceError,
964 "Channel::Init() RTP/RTCP module not initialized");
965 return -1;
966 }
967
968 // --- Register all permanent callbacks
niklase@google.com470e71d2011-07-07 08:21:25 +0000969 const bool fail =
andrew@webrtc.orgeb524d92013-09-23 23:02:24 +0000970 (audio_coding_->RegisterTransportCallback(this) == -1) ||
971 (audio_coding_->RegisterVADCallback(this) == -1);
niklase@google.com470e71d2011-07-07 08:21:25 +0000972
973 if (fail)
974 {
975 _engineStatisticsPtr->SetLastError(
976 VE_CANNOT_INIT_CHANNEL, kTraceError,
977 "Channel::Init() callbacks not registered");
978 return -1;
979 }
980
981 // --- Register all supported codecs to the receiving side of the
982 // RTP/RTCP module
983
984 CodecInst codec;
pbos@webrtc.org6141e132013-04-09 10:09:10 +0000985 const uint8_t nSupportedCodecs = AudioCodingModule::NumberOfCodecs();
niklase@google.com470e71d2011-07-07 08:21:25 +0000986
987 for (int idx = 0; idx < nSupportedCodecs; idx++)
988 {
989 // Open up the RTP/RTCP receiver for all supported codecs
andrew@webrtc.orgeb524d92013-09-23 23:02:24 +0000990 if ((audio_coding_->Codec(idx, &codec) == -1) ||
wu@webrtc.org822fbd82013-08-15 23:38:54 +0000991 (rtp_receiver_->RegisterReceivePayload(
992 codec.plname,
993 codec.pltype,
994 codec.plfreq,
995 codec.channels,
996 (codec.rate < 0) ? 0 : codec.rate) == -1))
niklase@google.com470e71d2011-07-07 08:21:25 +0000997 {
998 WEBRTC_TRACE(kTraceWarning, kTraceVoice,
999 VoEId(_instanceId,_channelId),
1000 "Channel::Init() unable to register %s (%d/%d/%d/%d) "
1001 "to RTP/RTCP receiver",
1002 codec.plname, codec.pltype, codec.plfreq,
1003 codec.channels, codec.rate);
1004 }
1005 else
1006 {
1007 WEBRTC_TRACE(kTraceInfo, kTraceVoice,
1008 VoEId(_instanceId,_channelId),
1009 "Channel::Init() %s (%d/%d/%d/%d) has been added to "
1010 "the RTP/RTCP receiver",
1011 codec.plname, codec.pltype, codec.plfreq,
1012 codec.channels, codec.rate);
1013 }
1014
1015 // Ensure that PCMU is used as default codec on the sending side
tina.legrand@webrtc.org45175852012-06-01 09:27:35 +00001016 if (!STR_CASE_CMP(codec.plname, "PCMU") && (codec.channels == 1))
niklase@google.com470e71d2011-07-07 08:21:25 +00001017 {
1018 SetSendCodec(codec);
1019 }
1020
1021 // Register default PT for outband 'telephone-event'
1022 if (!STR_CASE_CMP(codec.plname, "telephone-event"))
1023 {
pwestin@webrtc.org2853dde2012-05-11 11:08:54 +00001024 if ((_rtpRtcpModule->RegisterSendPayload(codec) == -1) ||
andrew@webrtc.orgeb524d92013-09-23 23:02:24 +00001025 (audio_coding_->RegisterReceiveCodec(codec) == -1))
niklase@google.com470e71d2011-07-07 08:21:25 +00001026 {
1027 WEBRTC_TRACE(kTraceWarning, kTraceVoice,
1028 VoEId(_instanceId,_channelId),
1029 "Channel::Init() failed to register outband "
1030 "'telephone-event' (%d/%d) correctly",
1031 codec.pltype, codec.plfreq);
1032 }
1033 }
1034
1035 if (!STR_CASE_CMP(codec.plname, "CN"))
1036 {
andrew@webrtc.orgeb524d92013-09-23 23:02:24 +00001037 if ((audio_coding_->RegisterSendCodec(codec) == -1) ||
1038 (audio_coding_->RegisterReceiveCodec(codec) == -1) ||
pwestin@webrtc.org2853dde2012-05-11 11:08:54 +00001039 (_rtpRtcpModule->RegisterSendPayload(codec) == -1))
niklase@google.com470e71d2011-07-07 08:21:25 +00001040 {
1041 WEBRTC_TRACE(kTraceWarning, kTraceVoice,
1042 VoEId(_instanceId,_channelId),
1043 "Channel::Init() failed to register CN (%d/%d) "
1044 "correctly - 1",
1045 codec.pltype, codec.plfreq);
1046 }
1047 }
1048#ifdef WEBRTC_CODEC_RED
1049 // Register RED to the receiving side of the ACM.
1050 // We will not receive an OnInitializeDecoder() callback for RED.
1051 if (!STR_CASE_CMP(codec.plname, "RED"))
1052 {
andrew@webrtc.orgeb524d92013-09-23 23:02:24 +00001053 if (audio_coding_->RegisterReceiveCodec(codec) == -1)
niklase@google.com470e71d2011-07-07 08:21:25 +00001054 {
1055 WEBRTC_TRACE(kTraceWarning, kTraceVoice,
1056 VoEId(_instanceId,_channelId),
1057 "Channel::Init() failed to register RED (%d/%d) "
1058 "correctly",
1059 codec.pltype, codec.plfreq);
1060 }
1061 }
1062#endif
1063 }
pwestin@webrtc.org684f0572013-03-13 23:20:57 +00001064
andrew@webrtc.org6c264cc2013-10-04 17:54:09 +00001065 if (rx_audioproc_->noise_suppression()->set_level(kDefaultNsMode) != 0) {
1066 LOG_FERR1(LS_ERROR, noise_suppression()->set_level, kDefaultNsMode);
1067 return -1;
niklase@google.com470e71d2011-07-07 08:21:25 +00001068 }
andrew@webrtc.org6c264cc2013-10-04 17:54:09 +00001069 if (rx_audioproc_->gain_control()->set_mode(kDefaultRxAgcMode) != 0) {
1070 LOG_FERR1(LS_ERROR, gain_control()->set_mode, kDefaultRxAgcMode);
1071 return -1;
niklase@google.com470e71d2011-07-07 08:21:25 +00001072 }
1073
1074 return 0;
1075}
1076
pbos@webrtc.org6141e132013-04-09 10:09:10 +00001077int32_t
niklase@google.com470e71d2011-07-07 08:21:25 +00001078Channel::SetEngineInformation(Statistics& engineStatistics,
1079 OutputMixer& outputMixer,
1080 voe::TransmitMixer& transmitMixer,
1081 ProcessThread& moduleProcessThread,
1082 AudioDeviceModule& audioDeviceModule,
1083 VoiceEngineObserver* voiceEngineObserver,
1084 CriticalSectionWrapper* callbackCritSect)
1085{
1086 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId,_channelId),
1087 "Channel::SetEngineInformation()");
1088 _engineStatisticsPtr = &engineStatistics;
1089 _outputMixerPtr = &outputMixer;
1090 _transmitMixerPtr = &transmitMixer,
1091 _moduleProcessThreadPtr = &moduleProcessThread;
1092 _audioDeviceModulePtr = &audioDeviceModule;
1093 _voiceEngineObserverPtr = voiceEngineObserver;
1094 _callbackCritSectPtr = callbackCritSect;
1095 return 0;
1096}
1097
pbos@webrtc.org6141e132013-04-09 10:09:10 +00001098int32_t
niklase@google.com470e71d2011-07-07 08:21:25 +00001099Channel::UpdateLocalTimeStamp()
1100{
1101
andrew@webrtc.org63a50982012-05-02 23:56:37 +00001102 _timeStamp += _audioFrame.samples_per_channel_;
niklase@google.com470e71d2011-07-07 08:21:25 +00001103 return 0;
1104}
1105
pbos@webrtc.org6141e132013-04-09 10:09:10 +00001106int32_t
niklase@google.com470e71d2011-07-07 08:21:25 +00001107Channel::StartPlayout()
1108{
1109 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId,_channelId),
1110 "Channel::StartPlayout()");
henrika@webrtc.org944cbeb2014-03-18 10:32:33 +00001111 if (channel_state_.Get().playing)
niklase@google.com470e71d2011-07-07 08:21:25 +00001112 {
1113 return 0;
1114 }
roosa@google.com1b60ceb2012-12-12 23:00:29 +00001115
1116 if (!_externalMixing) {
1117 // Add participant as candidates for mixing.
1118 if (_outputMixerPtr->SetMixabilityStatus(*this, true) != 0)
1119 {
1120 _engineStatisticsPtr->SetLastError(
1121 VE_AUDIO_CONF_MIX_MODULE_ERROR, kTraceError,
1122 "StartPlayout() failed to add participant to mixer");
1123 return -1;
1124 }
niklase@google.com470e71d2011-07-07 08:21:25 +00001125 }
1126
henrika@webrtc.org944cbeb2014-03-18 10:32:33 +00001127 channel_state_.SetPlaying(true);
braveyao@webrtc.orgab129902012-06-04 03:26:39 +00001128 if (RegisterFilePlayingToMixer() != 0)
1129 return -1;
1130
niklase@google.com470e71d2011-07-07 08:21:25 +00001131 return 0;
1132}
1133
pbos@webrtc.org6141e132013-04-09 10:09:10 +00001134int32_t
niklase@google.com470e71d2011-07-07 08:21:25 +00001135Channel::StopPlayout()
1136{
1137 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId,_channelId),
1138 "Channel::StopPlayout()");
henrika@webrtc.org944cbeb2014-03-18 10:32:33 +00001139 if (!channel_state_.Get().playing)
niklase@google.com470e71d2011-07-07 08:21:25 +00001140 {
1141 return 0;
1142 }
roosa@google.com1b60ceb2012-12-12 23:00:29 +00001143
1144 if (!_externalMixing) {
1145 // Remove participant as candidates for mixing
1146 if (_outputMixerPtr->SetMixabilityStatus(*this, false) != 0)
1147 {
1148 _engineStatisticsPtr->SetLastError(
1149 VE_AUDIO_CONF_MIX_MODULE_ERROR, kTraceError,
1150 "StopPlayout() failed to remove participant from mixer");
1151 return -1;
1152 }
niklase@google.com470e71d2011-07-07 08:21:25 +00001153 }
1154
henrika@webrtc.org944cbeb2014-03-18 10:32:33 +00001155 channel_state_.SetPlaying(false);
niklase@google.com470e71d2011-07-07 08:21:25 +00001156 _outputAudioLevel.Clear();
1157
1158 return 0;
1159}
1160
pbos@webrtc.org6141e132013-04-09 10:09:10 +00001161int32_t
niklase@google.com470e71d2011-07-07 08:21:25 +00001162Channel::StartSend()
1163{
1164 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId,_channelId),
1165 "Channel::StartSend()");
xians@webrtc.org09e8c472013-07-31 16:30:19 +00001166 // Resume the previous sequence number which was reset by StopSend().
henrika@webrtc.org944cbeb2014-03-18 10:32:33 +00001167 // This needs to be done before |sending| is set to true.
xians@webrtc.org09e8c472013-07-31 16:30:19 +00001168 if (send_sequence_number_)
1169 SetInitSequenceNumber(send_sequence_number_);
1170
henrika@webrtc.org944cbeb2014-03-18 10:32:33 +00001171 if (channel_state_.Get().sending)
niklase@google.com470e71d2011-07-07 08:21:25 +00001172 {
henrika@webrtc.org944cbeb2014-03-18 10:32:33 +00001173 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +00001174 }
henrika@webrtc.org944cbeb2014-03-18 10:32:33 +00001175 channel_state_.SetSending(true);
xians@webrtc.orge07247a2011-11-28 16:31:28 +00001176
pwestin@webrtc.org2853dde2012-05-11 11:08:54 +00001177 if (_rtpRtcpModule->SetSendingStatus(true) != 0)
niklase@google.com470e71d2011-07-07 08:21:25 +00001178 {
1179 _engineStatisticsPtr->SetLastError(
1180 VE_RTP_RTCP_MODULE_ERROR, kTraceError,
1181 "StartSend() RTP/RTCP failed to start sending");
mflodman@webrtc.org9a065d12012-03-07 08:12:21 +00001182 CriticalSectionScoped cs(&_callbackCritSect);
henrika@webrtc.org944cbeb2014-03-18 10:32:33 +00001183 channel_state_.SetSending(false);
niklase@google.com470e71d2011-07-07 08:21:25 +00001184 return -1;
1185 }
xians@webrtc.orge07247a2011-11-28 16:31:28 +00001186
niklase@google.com470e71d2011-07-07 08:21:25 +00001187 return 0;
1188}
1189
pbos@webrtc.org6141e132013-04-09 10:09:10 +00001190int32_t
niklase@google.com470e71d2011-07-07 08:21:25 +00001191Channel::StopSend()
1192{
1193 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId,_channelId),
1194 "Channel::StopSend()");
henrika@webrtc.org944cbeb2014-03-18 10:32:33 +00001195 if (!channel_state_.Get().sending)
niklase@google.com470e71d2011-07-07 08:21:25 +00001196 {
henrika@webrtc.org944cbeb2014-03-18 10:32:33 +00001197 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +00001198 }
henrika@webrtc.org944cbeb2014-03-18 10:32:33 +00001199 channel_state_.SetSending(false);
xians@webrtc.orge07247a2011-11-28 16:31:28 +00001200
xians@webrtc.org09e8c472013-07-31 16:30:19 +00001201 // Store the sequence number to be able to pick up the same sequence for
1202 // the next StartSend(). This is needed for restarting device, otherwise
1203 // it might cause libSRTP to complain about packets being replayed.
1204 // TODO(xians): Remove this workaround after RtpRtcpModule's refactoring
1205 // CL is landed. See issue
1206 // https://code.google.com/p/webrtc/issues/detail?id=2111 .
1207 send_sequence_number_ = _rtpRtcpModule->SequenceNumber();
1208
niklase@google.com470e71d2011-07-07 08:21:25 +00001209 // Reset sending SSRC and sequence number and triggers direct transmission
1210 // of RTCP BYE
pwestin@webrtc.org2853dde2012-05-11 11:08:54 +00001211 if (_rtpRtcpModule->SetSendingStatus(false) == -1 ||
1212 _rtpRtcpModule->ResetSendDataCountersRTP() == -1)
niklase@google.com470e71d2011-07-07 08:21:25 +00001213 {
1214 _engineStatisticsPtr->SetLastError(
1215 VE_RTP_RTCP_MODULE_ERROR, kTraceWarning,
1216 "StartSend() RTP/RTCP failed to stop sending");
1217 }
1218
niklase@google.com470e71d2011-07-07 08:21:25 +00001219 return 0;
1220}
1221
pbos@webrtc.org6141e132013-04-09 10:09:10 +00001222int32_t
niklase@google.com470e71d2011-07-07 08:21:25 +00001223Channel::StartReceiving()
1224{
1225 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId,_channelId),
1226 "Channel::StartReceiving()");
henrika@webrtc.org944cbeb2014-03-18 10:32:33 +00001227 if (channel_state_.Get().receiving)
niklase@google.com470e71d2011-07-07 08:21:25 +00001228 {
1229 return 0;
1230 }
henrika@webrtc.org944cbeb2014-03-18 10:32:33 +00001231 channel_state_.SetReceiving(true);
niklase@google.com470e71d2011-07-07 08:21:25 +00001232 _numberOfDiscardedPackets = 0;
1233 return 0;
1234}
1235
pbos@webrtc.org6141e132013-04-09 10:09:10 +00001236int32_t
niklase@google.com470e71d2011-07-07 08:21:25 +00001237Channel::StopReceiving()
1238{
1239 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId,_channelId),
1240 "Channel::StopReceiving()");
henrika@webrtc.org944cbeb2014-03-18 10:32:33 +00001241 if (!channel_state_.Get().receiving)
niklase@google.com470e71d2011-07-07 08:21:25 +00001242 {
1243 return 0;
1244 }
pwestin@webrtc.org684f0572013-03-13 23:20:57 +00001245
henrika@webrtc.org944cbeb2014-03-18 10:32:33 +00001246 channel_state_.SetReceiving(false);
niklase@google.com470e71d2011-07-07 08:21:25 +00001247 return 0;
1248}
1249
pbos@webrtc.org6141e132013-04-09 10:09:10 +00001250int32_t
niklase@google.com470e71d2011-07-07 08:21:25 +00001251Channel::RegisterVoiceEngineObserver(VoiceEngineObserver& observer)
1252{
1253 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId,_channelId),
1254 "Channel::RegisterVoiceEngineObserver()");
mflodman@webrtc.org9a065d12012-03-07 08:12:21 +00001255 CriticalSectionScoped cs(&_callbackCritSect);
niklase@google.com470e71d2011-07-07 08:21:25 +00001256
1257 if (_voiceEngineObserverPtr)
1258 {
1259 _engineStatisticsPtr->SetLastError(
1260 VE_INVALID_OPERATION, kTraceError,
1261 "RegisterVoiceEngineObserver() observer already enabled");
1262 return -1;
1263 }
1264 _voiceEngineObserverPtr = &observer;
1265 return 0;
1266}
1267
pbos@webrtc.org6141e132013-04-09 10:09:10 +00001268int32_t
niklase@google.com470e71d2011-07-07 08:21:25 +00001269Channel::DeRegisterVoiceEngineObserver()
1270{
1271 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId,_channelId),
1272 "Channel::DeRegisterVoiceEngineObserver()");
mflodman@webrtc.org9a065d12012-03-07 08:12:21 +00001273 CriticalSectionScoped cs(&_callbackCritSect);
niklase@google.com470e71d2011-07-07 08:21:25 +00001274
1275 if (!_voiceEngineObserverPtr)
1276 {
1277 _engineStatisticsPtr->SetLastError(
1278 VE_INVALID_OPERATION, kTraceWarning,
1279 "DeRegisterVoiceEngineObserver() observer already disabled");
1280 return 0;
1281 }
1282 _voiceEngineObserverPtr = NULL;
1283 return 0;
1284}
1285
pbos@webrtc.org6141e132013-04-09 10:09:10 +00001286int32_t
niklase@google.com470e71d2011-07-07 08:21:25 +00001287Channel::GetSendCodec(CodecInst& codec)
1288{
andrew@webrtc.orgeb524d92013-09-23 23:02:24 +00001289 return (audio_coding_->SendCodec(&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::GetRecCodec(CodecInst& codec)
1294{
andrew@webrtc.orgeb524d92013-09-23 23:02:24 +00001295 return (audio_coding_->ReceiveCodec(&codec));
niklase@google.com470e71d2011-07-07 08:21:25 +00001296}
1297
pbos@webrtc.org6141e132013-04-09 10:09:10 +00001298int32_t
niklase@google.com470e71d2011-07-07 08:21:25 +00001299Channel::SetSendCodec(const CodecInst& codec)
1300{
1301 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId,_channelId),
1302 "Channel::SetSendCodec()");
1303
andrew@webrtc.orgeb524d92013-09-23 23:02:24 +00001304 if (audio_coding_->RegisterSendCodec(codec) != 0)
niklase@google.com470e71d2011-07-07 08:21:25 +00001305 {
1306 WEBRTC_TRACE(kTraceError, kTraceVoice, VoEId(_instanceId,_channelId),
1307 "SetSendCodec() failed to register codec to ACM");
1308 return -1;
1309 }
1310
pwestin@webrtc.org2853dde2012-05-11 11:08:54 +00001311 if (_rtpRtcpModule->RegisterSendPayload(codec) != 0)
niklase@google.com470e71d2011-07-07 08:21:25 +00001312 {
pwestin@webrtc.org2853dde2012-05-11 11:08:54 +00001313 _rtpRtcpModule->DeRegisterSendPayload(codec.pltype);
1314 if (_rtpRtcpModule->RegisterSendPayload(codec) != 0)
niklase@google.com470e71d2011-07-07 08:21:25 +00001315 {
1316 WEBRTC_TRACE(
1317 kTraceError, kTraceVoice, VoEId(_instanceId,_channelId),
1318 "SetSendCodec() failed to register codec to"
1319 " RTP/RTCP module");
1320 return -1;
1321 }
1322 }
1323
pwestin@webrtc.org2853dde2012-05-11 11:08:54 +00001324 if (_rtpRtcpModule->SetAudioPacketSize(codec.pacsize) != 0)
niklase@google.com470e71d2011-07-07 08:21:25 +00001325 {
1326 WEBRTC_TRACE(kTraceError, kTraceVoice, VoEId(_instanceId,_channelId),
1327 "SetSendCodec() failed to set audio packet size");
1328 return -1;
1329 }
1330
minyue@webrtc.orgc1a40a72014-05-28 09:52:06 +00001331 bitrate_controller_->SetBitrateObserver(send_bitrate_observer_.get(),
1332 codec.rate, 0, 0);
1333
niklase@google.com470e71d2011-07-07 08:21:25 +00001334 return 0;
1335}
1336
minyue@webrtc.orgc1a40a72014-05-28 09:52:06 +00001337void
1338Channel::OnNetworkChanged(const uint32_t bitrate_bps,
1339 const uint8_t fraction_lost, // 0 - 255.
1340 const uint32_t rtt) {
1341 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId,_channelId),
1342 "Channel::OnNetworkChanged(bitrate_bps=%d, fration_lost=%d, rtt=%d)",
1343 bitrate_bps, fraction_lost, rtt);
minyue@webrtc.org74aaf292014-07-16 21:28:26 +00001344 // |fraction_lost| from BitrateObserver is short time observation of packet
1345 // loss rate from past. We use network predictor to make a more reasonable
1346 // loss rate estimation.
1347 network_predictor_->UpdatePacketLossRate(fraction_lost);
1348 uint8_t loss_rate = network_predictor_->GetLossRate();
minyue@webrtc.orgc1a40a72014-05-28 09:52:06 +00001349 // Normalizes rate to 0 - 100.
minyue@webrtc.org74aaf292014-07-16 21:28:26 +00001350 if (audio_coding_->SetPacketLossRate(100 * loss_rate / 255) != 0) {
minyue@webrtc.orgc1a40a72014-05-28 09:52:06 +00001351 _engineStatisticsPtr->SetLastError(VE_AUDIO_CODING_MODULE_ERROR,
1352 kTraceError, "OnNetworkChanged() failed to set packet loss rate");
1353 assert(false); // This should not happen.
1354 }
1355}
1356
pbos@webrtc.org6141e132013-04-09 10:09:10 +00001357int32_t
niklase@google.com470e71d2011-07-07 08:21:25 +00001358Channel::SetVADStatus(bool enableVAD, ACMVADMode mode, bool disableDTX)
1359{
1360 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId,_channelId),
1361 "Channel::SetVADStatus(mode=%d)", mode);
1362 // To disable VAD, DTX must be disabled too
1363 disableDTX = ((enableVAD == false) ? true : disableDTX);
andrew@webrtc.orgeb524d92013-09-23 23:02:24 +00001364 if (audio_coding_->SetVAD(!disableDTX, enableVAD, mode) != 0)
niklase@google.com470e71d2011-07-07 08:21:25 +00001365 {
1366 _engineStatisticsPtr->SetLastError(
1367 VE_AUDIO_CODING_MODULE_ERROR, kTraceError,
1368 "SetVADStatus() failed to set VAD");
1369 return -1;
1370 }
1371 return 0;
1372}
1373
pbos@webrtc.org6141e132013-04-09 10:09:10 +00001374int32_t
niklase@google.com470e71d2011-07-07 08:21:25 +00001375Channel::GetVADStatus(bool& enabledVAD, ACMVADMode& mode, bool& disabledDTX)
1376{
1377 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId,_channelId),
1378 "Channel::GetVADStatus");
andrew@webrtc.orgeb524d92013-09-23 23:02:24 +00001379 if (audio_coding_->VAD(&disabledDTX, &enabledVAD, &mode) != 0)
niklase@google.com470e71d2011-07-07 08:21:25 +00001380 {
1381 _engineStatisticsPtr->SetLastError(
1382 VE_AUDIO_CODING_MODULE_ERROR, kTraceError,
1383 "GetVADStatus() failed to get VAD status");
1384 return -1;
1385 }
1386 disabledDTX = !disabledDTX;
1387 return 0;
1388}
1389
pbos@webrtc.org6141e132013-04-09 10:09:10 +00001390int32_t
niklase@google.com470e71d2011-07-07 08:21:25 +00001391Channel::SetRecPayloadType(const CodecInst& codec)
1392{
1393 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId,_channelId),
1394 "Channel::SetRecPayloadType()");
1395
henrika@webrtc.org944cbeb2014-03-18 10:32:33 +00001396 if (channel_state_.Get().playing)
niklase@google.com470e71d2011-07-07 08:21:25 +00001397 {
1398 _engineStatisticsPtr->SetLastError(
1399 VE_ALREADY_PLAYING, kTraceError,
1400 "SetRecPayloadType() unable to set PT while playing");
1401 return -1;
1402 }
henrika@webrtc.org944cbeb2014-03-18 10:32:33 +00001403 if (channel_state_.Get().receiving)
niklase@google.com470e71d2011-07-07 08:21:25 +00001404 {
1405 _engineStatisticsPtr->SetLastError(
1406 VE_ALREADY_LISTENING, kTraceError,
1407 "SetRecPayloadType() unable to set PT while listening");
1408 return -1;
1409 }
1410
1411 if (codec.pltype == -1)
1412 {
1413 // De-register the selected codec (RTP/RTCP module and ACM)
1414
pbos@webrtc.org6141e132013-04-09 10:09:10 +00001415 int8_t pltype(-1);
niklase@google.com470e71d2011-07-07 08:21:25 +00001416 CodecInst rxCodec = codec;
1417
1418 // Get payload type for the given codec
wu@webrtc.org822fbd82013-08-15 23:38:54 +00001419 rtp_payload_registry_->ReceivePayloadType(
1420 rxCodec.plname,
1421 rxCodec.plfreq,
1422 rxCodec.channels,
1423 (rxCodec.rate < 0) ? 0 : rxCodec.rate,
1424 &pltype);
niklase@google.com470e71d2011-07-07 08:21:25 +00001425 rxCodec.pltype = pltype;
1426
wu@webrtc.org822fbd82013-08-15 23:38:54 +00001427 if (rtp_receiver_->DeRegisterReceivePayload(pltype) != 0)
niklase@google.com470e71d2011-07-07 08:21:25 +00001428 {
1429 _engineStatisticsPtr->SetLastError(
1430 VE_RTP_RTCP_MODULE_ERROR,
1431 kTraceError,
1432 "SetRecPayloadType() RTP/RTCP-module deregistration "
1433 "failed");
1434 return -1;
1435 }
andrew@webrtc.orgeb524d92013-09-23 23:02:24 +00001436 if (audio_coding_->UnregisterReceiveCodec(rxCodec.pltype) != 0)
niklase@google.com470e71d2011-07-07 08:21:25 +00001437 {
1438 _engineStatisticsPtr->SetLastError(
1439 VE_AUDIO_CODING_MODULE_ERROR, kTraceError,
1440 "SetRecPayloadType() ACM deregistration failed - 1");
1441 return -1;
1442 }
1443 return 0;
1444 }
1445
wu@webrtc.org822fbd82013-08-15 23:38:54 +00001446 if (rtp_receiver_->RegisterReceivePayload(
1447 codec.plname,
1448 codec.pltype,
1449 codec.plfreq,
1450 codec.channels,
1451 (codec.rate < 0) ? 0 : codec.rate) != 0)
niklase@google.com470e71d2011-07-07 08:21:25 +00001452 {
1453 // First attempt to register failed => de-register and try again
wu@webrtc.org822fbd82013-08-15 23:38:54 +00001454 rtp_receiver_->DeRegisterReceivePayload(codec.pltype);
1455 if (rtp_receiver_->RegisterReceivePayload(
1456 codec.plname,
1457 codec.pltype,
1458 codec.plfreq,
1459 codec.channels,
1460 (codec.rate < 0) ? 0 : codec.rate) != 0)
niklase@google.com470e71d2011-07-07 08:21:25 +00001461 {
1462 _engineStatisticsPtr->SetLastError(
1463 VE_RTP_RTCP_MODULE_ERROR, kTraceError,
1464 "SetRecPayloadType() RTP/RTCP-module registration failed");
1465 return -1;
1466 }
1467 }
andrew@webrtc.orgeb524d92013-09-23 23:02:24 +00001468 if (audio_coding_->RegisterReceiveCodec(codec) != 0)
niklase@google.com470e71d2011-07-07 08:21:25 +00001469 {
andrew@webrtc.orgeb524d92013-09-23 23:02:24 +00001470 audio_coding_->UnregisterReceiveCodec(codec.pltype);
1471 if (audio_coding_->RegisterReceiveCodec(codec) != 0)
niklase@google.com470e71d2011-07-07 08:21:25 +00001472 {
1473 _engineStatisticsPtr->SetLastError(
1474 VE_AUDIO_CODING_MODULE_ERROR, kTraceError,
1475 "SetRecPayloadType() ACM registration failed - 1");
1476 return -1;
1477 }
1478 }
1479 return 0;
1480}
1481
pbos@webrtc.org6141e132013-04-09 10:09:10 +00001482int32_t
niklase@google.com470e71d2011-07-07 08:21:25 +00001483Channel::GetRecPayloadType(CodecInst& codec)
1484{
1485 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId,_channelId),
1486 "Channel::GetRecPayloadType()");
pbos@webrtc.org6141e132013-04-09 10:09:10 +00001487 int8_t payloadType(-1);
wu@webrtc.org822fbd82013-08-15 23:38:54 +00001488 if (rtp_payload_registry_->ReceivePayloadType(
1489 codec.plname,
1490 codec.plfreq,
1491 codec.channels,
1492 (codec.rate < 0) ? 0 : codec.rate,
1493 &payloadType) != 0)
niklase@google.com470e71d2011-07-07 08:21:25 +00001494 {
1495 _engineStatisticsPtr->SetLastError(
henrika@webrtc.org37198002012-06-18 11:00:12 +00001496 VE_RTP_RTCP_MODULE_ERROR, kTraceWarning,
niklase@google.com470e71d2011-07-07 08:21:25 +00001497 "GetRecPayloadType() failed to retrieve RX payload type");
1498 return -1;
1499 }
1500 codec.pltype = payloadType;
1501 WEBRTC_TRACE(kTraceStateInfo, kTraceVoice, VoEId(_instanceId,_channelId),
1502 "Channel::GetRecPayloadType() => pltype=%u", codec.pltype);
1503 return 0;
1504}
1505
pbos@webrtc.org6141e132013-04-09 10:09:10 +00001506int32_t
niklase@google.com470e71d2011-07-07 08:21:25 +00001507Channel::SetSendCNPayloadType(int type, PayloadFrequencies frequency)
1508{
1509 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId,_channelId),
1510 "Channel::SetSendCNPayloadType()");
1511
1512 CodecInst codec;
pbos@webrtc.org6141e132013-04-09 10:09:10 +00001513 int32_t samplingFreqHz(-1);
tina.legrand@webrtc.org45175852012-06-01 09:27:35 +00001514 const int kMono = 1;
niklase@google.com470e71d2011-07-07 08:21:25 +00001515 if (frequency == kFreq32000Hz)
1516 samplingFreqHz = 32000;
1517 else if (frequency == kFreq16000Hz)
1518 samplingFreqHz = 16000;
1519
andrew@webrtc.orgeb524d92013-09-23 23:02:24 +00001520 if (audio_coding_->Codec("CN", &codec, samplingFreqHz, kMono) == -1)
niklase@google.com470e71d2011-07-07 08:21:25 +00001521 {
1522 _engineStatisticsPtr->SetLastError(
1523 VE_AUDIO_CODING_MODULE_ERROR, kTraceError,
1524 "SetSendCNPayloadType() failed to retrieve default CN codec "
1525 "settings");
1526 return -1;
1527 }
1528
1529 // Modify the payload type (must be set to dynamic range)
1530 codec.pltype = type;
1531
andrew@webrtc.orgeb524d92013-09-23 23:02:24 +00001532 if (audio_coding_->RegisterSendCodec(codec) != 0)
niklase@google.com470e71d2011-07-07 08:21:25 +00001533 {
1534 _engineStatisticsPtr->SetLastError(
1535 VE_AUDIO_CODING_MODULE_ERROR, kTraceError,
1536 "SetSendCNPayloadType() failed to register CN to ACM");
1537 return -1;
1538 }
1539
pwestin@webrtc.org2853dde2012-05-11 11:08:54 +00001540 if (_rtpRtcpModule->RegisterSendPayload(codec) != 0)
niklase@google.com470e71d2011-07-07 08:21:25 +00001541 {
pwestin@webrtc.org2853dde2012-05-11 11:08:54 +00001542 _rtpRtcpModule->DeRegisterSendPayload(codec.pltype);
1543 if (_rtpRtcpModule->RegisterSendPayload(codec) != 0)
niklase@google.com470e71d2011-07-07 08:21:25 +00001544 {
1545 _engineStatisticsPtr->SetLastError(
1546 VE_RTP_RTCP_MODULE_ERROR, kTraceError,
1547 "SetSendCNPayloadType() failed to register CN to RTP/RTCP "
1548 "module");
1549 return -1;
1550 }
1551 }
1552 return 0;
1553}
1554
minyue@webrtc.orgadee8f92014-09-03 12:28:06 +00001555int Channel::SetOpusMaxPlaybackRate(int frequency_hz) {
minyue@webrtc.org6aac93b2014-08-12 08:13:33 +00001556 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId, _channelId),
minyue@webrtc.orgadee8f92014-09-03 12:28:06 +00001557 "Channel::SetOpusMaxPlaybackRate()");
minyue@webrtc.org6aac93b2014-08-12 08:13:33 +00001558
minyue@webrtc.orgadee8f92014-09-03 12:28:06 +00001559 if (audio_coding_->SetOpusMaxPlaybackRate(frequency_hz) != 0) {
minyue@webrtc.org6aac93b2014-08-12 08:13:33 +00001560 _engineStatisticsPtr->SetLastError(
1561 VE_AUDIO_CODING_MODULE_ERROR, kTraceError,
minyue@webrtc.orgadee8f92014-09-03 12:28:06 +00001562 "SetOpusMaxPlaybackRate() failed to set maximum playback rate");
minyue@webrtc.org6aac93b2014-08-12 08:13:33 +00001563 return -1;
1564 }
1565 return 0;
1566}
1567
pbos@webrtc.org6141e132013-04-09 10:09:10 +00001568int32_t Channel::RegisterExternalTransport(Transport& transport)
niklase@google.com470e71d2011-07-07 08:21:25 +00001569{
1570 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId, _channelId),
1571 "Channel::RegisterExternalTransport()");
1572
mflodman@webrtc.org9a065d12012-03-07 08:12:21 +00001573 CriticalSectionScoped cs(&_callbackCritSect);
niklase@google.com470e71d2011-07-07 08:21:25 +00001574
niklase@google.com470e71d2011-07-07 08:21:25 +00001575 if (_externalTransport)
1576 {
1577 _engineStatisticsPtr->SetLastError(VE_INVALID_OPERATION,
1578 kTraceError,
1579 "RegisterExternalTransport() external transport already enabled");
1580 return -1;
1581 }
1582 _externalTransport = true;
1583 _transportPtr = &transport;
1584 return 0;
1585}
1586
pbos@webrtc.org6141e132013-04-09 10:09:10 +00001587int32_t
niklase@google.com470e71d2011-07-07 08:21:25 +00001588Channel::DeRegisterExternalTransport()
1589{
1590 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId,_channelId),
1591 "Channel::DeRegisterExternalTransport()");
1592
mflodman@webrtc.org9a065d12012-03-07 08:12:21 +00001593 CriticalSectionScoped cs(&_callbackCritSect);
xians@webrtc.org83661f52011-11-25 10:58:15 +00001594
niklase@google.com470e71d2011-07-07 08:21:25 +00001595 if (!_transportPtr)
1596 {
1597 _engineStatisticsPtr->SetLastError(
1598 VE_INVALID_OPERATION, kTraceWarning,
1599 "DeRegisterExternalTransport() external transport already "
1600 "disabled");
1601 return 0;
1602 }
1603 _externalTransport = false;
niklase@google.com470e71d2011-07-07 08:21:25 +00001604 _transportPtr = NULL;
1605 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId,_channelId),
1606 "DeRegisterExternalTransport() all transport is disabled");
niklase@google.com470e71d2011-07-07 08:21:25 +00001607 return 0;
1608}
1609
pkasting@chromium.org4591fbd2014-11-20 22:28:14 +00001610int32_t Channel::ReceivedRTPPacket(const int8_t* data, size_t length,
solenberg@webrtc.orgb1f50102014-03-24 10:38:25 +00001611 const PacketTime& packet_time) {
pwestin@webrtc.org0c459572013-04-03 15:43:57 +00001612 WEBRTC_TRACE(kTraceStream, kTraceVoice, VoEId(_instanceId,_channelId),
1613 "Channel::ReceivedRTPPacket()");
1614
1615 // Store playout timestamp for the received RTP packet
pwestin@webrtc.org1de01352013-04-11 20:23:35 +00001616 UpdatePlayoutTimestamp(false);
pwestin@webrtc.org0c459572013-04-03 15:43:57 +00001617
1618 // Dump the RTP packet to a file (if RTP dump is enabled).
pbos@webrtc.org6141e132013-04-09 10:09:10 +00001619 if (_rtpDumpIn.DumpPacket((const uint8_t*)data,
1620 (uint16_t)length) == -1) {
pwestin@webrtc.org0c459572013-04-03 15:43:57 +00001621 WEBRTC_TRACE(kTraceWarning, kTraceVoice,
1622 VoEId(_instanceId,_channelId),
1623 "Channel::SendPacket() RTP dump to input file failed");
1624 }
stefan@webrtc.org7bb8f022013-09-06 13:40:11 +00001625 const uint8_t* received_packet = reinterpret_cast<const uint8_t*>(data);
stefan@webrtc.orga5cb98c2013-05-29 12:12:51 +00001626 RTPHeader header;
stefan@webrtc.org7bb8f022013-09-06 13:40:11 +00001627 if (!rtp_header_parser_->Parse(received_packet, length, &header)) {
1628 WEBRTC_TRACE(webrtc::kTraceDebug, webrtc::kTraceVoice, _channelId,
1629 "Incoming packet: invalid RTP header");
stefan@webrtc.orga5cb98c2013-05-29 12:12:51 +00001630 return -1;
1631 }
wu@webrtc.org822fbd82013-08-15 23:38:54 +00001632 header.payload_type_frequency =
1633 rtp_payload_registry_->GetPayloadTypeFrequency(header.payloadType);
stefan@webrtc.org7bb8f022013-09-06 13:40:11 +00001634 if (header.payload_type_frequency < 0)
wu@webrtc.org822fbd82013-08-15 23:38:54 +00001635 return -1;
stefan@webrtc.org48df3812013-11-08 15:18:52 +00001636 bool in_order = IsPacketInOrder(header);
stefan@webrtc.org7bb8f022013-09-06 13:40:11 +00001637 rtp_receive_statistics_->IncomingPacket(header, length,
stefan@webrtc.org48df3812013-11-08 15:18:52 +00001638 IsPacketRetransmitted(header, in_order));
stefan@webrtc.org7bb8f022013-09-06 13:40:11 +00001639 rtp_payload_registry_->SetIncomingPayloadType(header);
solenberg@webrtc.orgb1f50102014-03-24 10:38:25 +00001640
1641 // Forward any packets to ViE bandwidth estimator, if enabled.
1642 {
1643 CriticalSectionScoped cs(&_callbackCritSect);
1644 if (vie_network_) {
1645 int64_t arrival_time_ms;
1646 if (packet_time.timestamp != -1) {
1647 arrival_time_ms = (packet_time.timestamp + 500) / 1000;
1648 } else {
1649 arrival_time_ms = TickTime::MillisecondTimestamp();
1650 }
pkasting@chromium.org4591fbd2014-11-20 22:28:14 +00001651 size_t payload_length = length - header.headerLength;
solenberg@webrtc.orgb1f50102014-03-24 10:38:25 +00001652 vie_network_->ReceivedBWEPacket(video_channel_, arrival_time_ms,
1653 payload_length, header);
1654 }
1655 }
1656
stefan@webrtc.org48df3812013-11-08 15:18:52 +00001657 return ReceivePacket(received_packet, length, header, in_order) ? 0 : -1;
stefan@webrtc.org7bb8f022013-09-06 13:40:11 +00001658}
1659
1660bool Channel::ReceivePacket(const uint8_t* packet,
pkasting@chromium.org4591fbd2014-11-20 22:28:14 +00001661 size_t packet_length,
stefan@webrtc.org7bb8f022013-09-06 13:40:11 +00001662 const RTPHeader& header,
1663 bool in_order) {
1664 if (rtp_payload_registry_->IsEncapsulated(header)) {
1665 return HandleEncapsulation(packet, packet_length, header);
wu@webrtc.org822fbd82013-08-15 23:38:54 +00001666 }
stefan@webrtc.org7bb8f022013-09-06 13:40:11 +00001667 const uint8_t* payload = packet + header.headerLength;
pkasting@chromium.org4591fbd2014-11-20 22:28:14 +00001668 assert(packet_length >= header.headerLength);
1669 size_t payload_length = packet_length - header.headerLength;
wu@webrtc.org822fbd82013-08-15 23:38:54 +00001670 PayloadUnion payload_specific;
1671 if (!rtp_payload_registry_->GetPayloadSpecifics(header.payloadType,
stefan@webrtc.org7bb8f022013-09-06 13:40:11 +00001672 &payload_specific)) {
1673 return false;
wu@webrtc.org822fbd82013-08-15 23:38:54 +00001674 }
stefan@webrtc.org7bb8f022013-09-06 13:40:11 +00001675 return rtp_receiver_->IncomingRtpPacket(header, payload, payload_length,
1676 payload_specific, in_order);
1677}
1678
1679bool Channel::HandleEncapsulation(const uint8_t* packet,
pkasting@chromium.org4591fbd2014-11-20 22:28:14 +00001680 size_t packet_length,
stefan@webrtc.org7bb8f022013-09-06 13:40:11 +00001681 const RTPHeader& header) {
1682 if (!rtp_payload_registry_->IsRtx(header))
1683 return false;
1684
1685 // Remove the RTX header and parse the original RTP header.
1686 if (packet_length < header.headerLength)
1687 return false;
1688 if (packet_length > kVoiceEngineMaxIpPacketSizeBytes)
1689 return false;
1690 if (restored_packet_in_use_) {
1691 WEBRTC_TRACE(webrtc::kTraceDebug, webrtc::kTraceVoice, _channelId,
1692 "Multiple RTX headers detected, dropping packet");
1693 return false;
pwestin@webrtc.org0c459572013-04-03 15:43:57 +00001694 }
stefan@webrtc.org7bb8f022013-09-06 13:40:11 +00001695 uint8_t* restored_packet_ptr = restored_packet_;
1696 if (!rtp_payload_registry_->RestoreOriginalPacket(
1697 &restored_packet_ptr, packet, &packet_length, rtp_receiver_->SSRC(),
1698 header)) {
1699 WEBRTC_TRACE(webrtc::kTraceDebug, webrtc::kTraceVoice, _channelId,
1700 "Incoming RTX packet: invalid RTP header");
1701 return false;
1702 }
1703 restored_packet_in_use_ = true;
1704 bool ret = OnRecoveredPacket(restored_packet_ptr, packet_length);
1705 restored_packet_in_use_ = false;
1706 return ret;
1707}
1708
1709bool Channel::IsPacketInOrder(const RTPHeader& header) const {
1710 StreamStatistician* statistician =
1711 rtp_receive_statistics_->GetStatistician(header.ssrc);
1712 if (!statistician)
1713 return false;
1714 return statistician->IsPacketInOrder(header.sequenceNumber);
niklase@google.com470e71d2011-07-07 08:21:25 +00001715}
1716
stefan@webrtc.org48df3812013-11-08 15:18:52 +00001717bool Channel::IsPacketRetransmitted(const RTPHeader& header,
1718 bool in_order) const {
stefan@webrtc.org7bb8f022013-09-06 13:40:11 +00001719 // Retransmissions are handled separately if RTX is enabled.
1720 if (rtp_payload_registry_->RtxEnabled())
1721 return false;
1722 StreamStatistician* statistician =
1723 rtp_receive_statistics_->GetStatistician(header.ssrc);
1724 if (!statistician)
1725 return false;
1726 // Check if this is a retransmission.
1727 uint16_t min_rtt = 0;
1728 _rtpRtcpModule->RTT(rtp_receiver_->SSRC(), NULL, NULL, &min_rtt, NULL);
stefan@webrtc.org48df3812013-11-08 15:18:52 +00001729 return !in_order &&
stefan@webrtc.org7bb8f022013-09-06 13:40:11 +00001730 statistician->IsRetransmitOfOldPacket(header, min_rtt);
wu@webrtc.org822fbd82013-08-15 23:38:54 +00001731}
1732
pkasting@chromium.org4591fbd2014-11-20 22:28:14 +00001733int32_t Channel::ReceivedRTCPPacket(const int8_t* data, size_t length) {
pwestin@webrtc.org0c459572013-04-03 15:43:57 +00001734 WEBRTC_TRACE(kTraceStream, kTraceVoice, VoEId(_instanceId,_channelId),
1735 "Channel::ReceivedRTCPPacket()");
1736 // Store playout timestamp for the received RTCP packet
pwestin@webrtc.org1de01352013-04-11 20:23:35 +00001737 UpdatePlayoutTimestamp(true);
pwestin@webrtc.org0c459572013-04-03 15:43:57 +00001738
1739 // Dump the RTCP packet to a file (if RTP dump is enabled).
pkasting@chromium.org4591fbd2014-11-20 22:28:14 +00001740 if (_rtpDumpIn.DumpPacket((const uint8_t*)data, length) == -1) {
pwestin@webrtc.org0c459572013-04-03 15:43:57 +00001741 WEBRTC_TRACE(kTraceWarning, kTraceVoice,
1742 VoEId(_instanceId,_channelId),
1743 "Channel::SendPacket() RTCP dump to input file failed");
1744 }
1745
1746 // Deliver RTCP packet to RTP/RTCP module for parsing
pkasting@chromium.org4591fbd2014-11-20 22:28:14 +00001747 if (_rtpRtcpModule->IncomingRtcpPacket((const uint8_t*)data, length) == -1) {
pwestin@webrtc.org0c459572013-04-03 15:43:57 +00001748 _engineStatisticsPtr->SetLastError(
1749 VE_SOCKET_TRANSPORT_MODULE_ERROR, kTraceWarning,
1750 "Channel::IncomingRTPPacket() RTCP packet is invalid");
1751 }
wu@webrtc.org82c4b852014-05-20 22:55:01 +00001752
stefan@webrtc.org8e24d872014-09-02 18:58:24 +00001753 {
1754 CriticalSectionScoped lock(ts_stats_lock_.get());
minyue@webrtc.org2c0cdbc2014-10-09 10:52:43 +00001755 uint16_t rtt = GetRTT();
1756 if (rtt == 0) {
1757 // Waiting for valid RTT.
1758 return 0;
1759 }
1760 uint32_t ntp_secs = 0;
1761 uint32_t ntp_frac = 0;
1762 uint32_t rtp_timestamp = 0;
1763 if (0 != _rtpRtcpModule->RemoteNTP(&ntp_secs, &ntp_frac, NULL, NULL,
1764 &rtp_timestamp)) {
1765 // Waiting for RTCP.
1766 return 0;
1767 }
1768 ntp_estimator_.UpdateRtcpTimestamp(rtt, ntp_secs, ntp_frac, rtp_timestamp);
stefan@webrtc.org8e24d872014-09-02 18:58:24 +00001769 }
pwestin@webrtc.org0c459572013-04-03 15:43:57 +00001770 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +00001771}
1772
niklase@google.com470e71d2011-07-07 08:21:25 +00001773int Channel::StartPlayingFileLocally(const char* fileName,
pbos@webrtc.org92135212013-05-14 08:31:39 +00001774 bool loop,
1775 FileFormats format,
1776 int startPosition,
1777 float volumeScaling,
1778 int stopPosition,
niklase@google.com470e71d2011-07-07 08:21:25 +00001779 const CodecInst* codecInst)
1780{
1781 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId,_channelId),
1782 "Channel::StartPlayingFileLocally(fileNameUTF8[]=%s, loop=%d,"
1783 " format=%d, volumeScaling=%5.3f, startPosition=%d, "
1784 "stopPosition=%d)", fileName, loop, format, volumeScaling,
1785 startPosition, stopPosition);
1786
henrika@webrtc.org944cbeb2014-03-18 10:32:33 +00001787 if (channel_state_.Get().output_file_playing)
niklase@google.com470e71d2011-07-07 08:21:25 +00001788 {
1789 _engineStatisticsPtr->SetLastError(
1790 VE_ALREADY_PLAYING, kTraceError,
1791 "StartPlayingFileLocally() is already playing");
1792 return -1;
1793 }
1794
niklase@google.com470e71d2011-07-07 08:21:25 +00001795 {
mflodman@webrtc.org9a065d12012-03-07 08:12:21 +00001796 CriticalSectionScoped cs(&_fileCritSect);
henrike@webrtc.orgb37c6282011-10-31 23:53:04 +00001797
1798 if (_outputFilePlayerPtr)
1799 {
1800 _outputFilePlayerPtr->RegisterModuleFileCallback(NULL);
1801 FilePlayer::DestroyFilePlayer(_outputFilePlayerPtr);
1802 _outputFilePlayerPtr = NULL;
1803 }
1804
1805 _outputFilePlayerPtr = FilePlayer::CreateFilePlayer(
1806 _outputFilePlayerId, (const FileFormats)format);
1807
1808 if (_outputFilePlayerPtr == NULL)
1809 {
1810 _engineStatisticsPtr->SetLastError(
1811 VE_INVALID_ARGUMENT, kTraceError,
henrike@webrtc.org31d30702011-11-18 19:59:32 +00001812 "StartPlayingFileLocally() filePlayer format is not correct");
henrike@webrtc.orgb37c6282011-10-31 23:53:04 +00001813 return -1;
1814 }
1815
pbos@webrtc.org6141e132013-04-09 10:09:10 +00001816 const uint32_t notificationTime(0);
henrike@webrtc.orgb37c6282011-10-31 23:53:04 +00001817
1818 if (_outputFilePlayerPtr->StartPlayingFile(
1819 fileName,
1820 loop,
1821 startPosition,
1822 volumeScaling,
1823 notificationTime,
1824 stopPosition,
1825 (const CodecInst*)codecInst) != 0)
1826 {
1827 _engineStatisticsPtr->SetLastError(
1828 VE_BAD_FILE, kTraceError,
1829 "StartPlayingFile() failed to start file playout");
1830 _outputFilePlayerPtr->StopPlayingFile();
1831 FilePlayer::DestroyFilePlayer(_outputFilePlayerPtr);
1832 _outputFilePlayerPtr = NULL;
1833 return -1;
1834 }
1835 _outputFilePlayerPtr->RegisterModuleFileCallback(this);
henrika@webrtc.org944cbeb2014-03-18 10:32:33 +00001836 channel_state_.SetOutputFilePlaying(true);
niklase@google.com470e71d2011-07-07 08:21:25 +00001837 }
braveyao@webrtc.orgab129902012-06-04 03:26:39 +00001838
1839 if (RegisterFilePlayingToMixer() != 0)
henrike@webrtc.org066f9e52011-10-28 23:15:47 +00001840 return -1;
niklase@google.com470e71d2011-07-07 08:21:25 +00001841
1842 return 0;
1843}
1844
1845int Channel::StartPlayingFileLocally(InStream* stream,
pbos@webrtc.org92135212013-05-14 08:31:39 +00001846 FileFormats format,
1847 int startPosition,
1848 float volumeScaling,
1849 int stopPosition,
niklase@google.com470e71d2011-07-07 08:21:25 +00001850 const CodecInst* codecInst)
1851{
1852 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId,_channelId),
1853 "Channel::StartPlayingFileLocally(format=%d,"
1854 " volumeScaling=%5.3f, startPosition=%d, stopPosition=%d)",
1855 format, volumeScaling, startPosition, stopPosition);
1856
1857 if(stream == NULL)
1858 {
1859 _engineStatisticsPtr->SetLastError(
1860 VE_BAD_FILE, kTraceError,
1861 "StartPlayingFileLocally() NULL as input stream");
1862 return -1;
1863 }
1864
1865
henrika@webrtc.org944cbeb2014-03-18 10:32:33 +00001866 if (channel_state_.Get().output_file_playing)
niklase@google.com470e71d2011-07-07 08:21:25 +00001867 {
1868 _engineStatisticsPtr->SetLastError(
1869 VE_ALREADY_PLAYING, kTraceError,
1870 "StartPlayingFileLocally() is already playing");
1871 return -1;
1872 }
1873
niklase@google.com470e71d2011-07-07 08:21:25 +00001874 {
mflodman@webrtc.org9a065d12012-03-07 08:12:21 +00001875 CriticalSectionScoped cs(&_fileCritSect);
henrike@webrtc.orgb37c6282011-10-31 23:53:04 +00001876
1877 // Destroy the old instance
1878 if (_outputFilePlayerPtr)
1879 {
1880 _outputFilePlayerPtr->RegisterModuleFileCallback(NULL);
1881 FilePlayer::DestroyFilePlayer(_outputFilePlayerPtr);
1882 _outputFilePlayerPtr = NULL;
1883 }
1884
1885 // Create the instance
1886 _outputFilePlayerPtr = FilePlayer::CreateFilePlayer(
1887 _outputFilePlayerId,
1888 (const FileFormats)format);
1889
1890 if (_outputFilePlayerPtr == NULL)
1891 {
1892 _engineStatisticsPtr->SetLastError(
1893 VE_INVALID_ARGUMENT, kTraceError,
1894 "StartPlayingFileLocally() filePlayer format isnot correct");
1895 return -1;
1896 }
1897
pbos@webrtc.org6141e132013-04-09 10:09:10 +00001898 const uint32_t notificationTime(0);
henrike@webrtc.orgb37c6282011-10-31 23:53:04 +00001899
1900 if (_outputFilePlayerPtr->StartPlayingFile(*stream, startPosition,
1901 volumeScaling,
1902 notificationTime,
1903 stopPosition, codecInst) != 0)
1904 {
1905 _engineStatisticsPtr->SetLastError(VE_BAD_FILE, kTraceError,
1906 "StartPlayingFile() failed to "
1907 "start file playout");
1908 _outputFilePlayerPtr->StopPlayingFile();
1909 FilePlayer::DestroyFilePlayer(_outputFilePlayerPtr);
1910 _outputFilePlayerPtr = NULL;
1911 return -1;
1912 }
1913 _outputFilePlayerPtr->RegisterModuleFileCallback(this);
henrika@webrtc.org944cbeb2014-03-18 10:32:33 +00001914 channel_state_.SetOutputFilePlaying(true);
niklase@google.com470e71d2011-07-07 08:21:25 +00001915 }
braveyao@webrtc.orgab129902012-06-04 03:26:39 +00001916
1917 if (RegisterFilePlayingToMixer() != 0)
henrike@webrtc.org066f9e52011-10-28 23:15:47 +00001918 return -1;
niklase@google.com470e71d2011-07-07 08:21:25 +00001919
niklase@google.com470e71d2011-07-07 08:21:25 +00001920 return 0;
1921}
1922
1923int Channel::StopPlayingFileLocally()
1924{
1925 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId,_channelId),
1926 "Channel::StopPlayingFileLocally()");
1927
henrika@webrtc.org944cbeb2014-03-18 10:32:33 +00001928 if (!channel_state_.Get().output_file_playing)
niklase@google.com470e71d2011-07-07 08:21:25 +00001929 {
1930 _engineStatisticsPtr->SetLastError(
1931 VE_INVALID_OPERATION, kTraceWarning,
1932 "StopPlayingFileLocally() isnot playing");
1933 return 0;
1934 }
1935
niklase@google.com470e71d2011-07-07 08:21:25 +00001936 {
mflodman@webrtc.org9a065d12012-03-07 08:12:21 +00001937 CriticalSectionScoped cs(&_fileCritSect);
henrike@webrtc.orgb37c6282011-10-31 23:53:04 +00001938
1939 if (_outputFilePlayerPtr->StopPlayingFile() != 0)
1940 {
1941 _engineStatisticsPtr->SetLastError(
1942 VE_STOP_RECORDING_FAILED, kTraceError,
1943 "StopPlayingFile() could not stop playing");
1944 return -1;
1945 }
1946 _outputFilePlayerPtr->RegisterModuleFileCallback(NULL);
1947 FilePlayer::DestroyFilePlayer(_outputFilePlayerPtr);
1948 _outputFilePlayerPtr = NULL;
henrika@webrtc.org944cbeb2014-03-18 10:32:33 +00001949 channel_state_.SetOutputFilePlaying(false);
niklase@google.com470e71d2011-07-07 08:21:25 +00001950 }
henrike@webrtc.orgb37c6282011-10-31 23:53:04 +00001951 // _fileCritSect cannot be taken while calling
1952 // SetAnonymousMixibilityStatus. Refer to comments in
1953 // StartPlayingFileLocally(const char* ...) for more details.
henrike@webrtc.org066f9e52011-10-28 23:15:47 +00001954 if (_outputMixerPtr->SetAnonymousMixabilityStatus(*this, false) != 0)
1955 {
1956 _engineStatisticsPtr->SetLastError(
1957 VE_AUDIO_CONF_MIX_MODULE_ERROR, kTraceError,
henrike@webrtc.orgb37c6282011-10-31 23:53:04 +00001958 "StopPlayingFile() failed to stop participant from playing as"
1959 "file in the mixer");
henrike@webrtc.org066f9e52011-10-28 23:15:47 +00001960 return -1;
1961 }
niklase@google.com470e71d2011-07-07 08:21:25 +00001962
1963 return 0;
1964}
1965
1966int Channel::IsPlayingFileLocally() const
1967{
1968 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId,_channelId),
1969 "Channel::IsPlayingFileLocally()");
1970
henrika@webrtc.org944cbeb2014-03-18 10:32:33 +00001971 return channel_state_.Get().output_file_playing;
niklase@google.com470e71d2011-07-07 08:21:25 +00001972}
1973
braveyao@webrtc.orgab129902012-06-04 03:26:39 +00001974int Channel::RegisterFilePlayingToMixer()
1975{
1976 // Return success for not registering for file playing to mixer if:
1977 // 1. playing file before playout is started on that channel.
1978 // 2. starting playout without file playing on that channel.
henrika@webrtc.org944cbeb2014-03-18 10:32:33 +00001979 if (!channel_state_.Get().playing ||
1980 !channel_state_.Get().output_file_playing)
braveyao@webrtc.orgab129902012-06-04 03:26:39 +00001981 {
1982 return 0;
1983 }
1984
1985 // |_fileCritSect| cannot be taken while calling
1986 // SetAnonymousMixabilityStatus() since as soon as the participant is added
1987 // frames can be pulled by the mixer. Since the frames are generated from
1988 // the file, _fileCritSect will be taken. This would result in a deadlock.
1989 if (_outputMixerPtr->SetAnonymousMixabilityStatus(*this, true) != 0)
1990 {
henrika@webrtc.org944cbeb2014-03-18 10:32:33 +00001991 channel_state_.SetOutputFilePlaying(false);
braveyao@webrtc.orgab129902012-06-04 03:26:39 +00001992 CriticalSectionScoped cs(&_fileCritSect);
braveyao@webrtc.orgab129902012-06-04 03:26:39 +00001993 _engineStatisticsPtr->SetLastError(
1994 VE_AUDIO_CONF_MIX_MODULE_ERROR, kTraceError,
1995 "StartPlayingFile() failed to add participant as file to mixer");
1996 _outputFilePlayerPtr->StopPlayingFile();
1997 FilePlayer::DestroyFilePlayer(_outputFilePlayerPtr);
1998 _outputFilePlayerPtr = NULL;
1999 return -1;
2000 }
2001
2002 return 0;
2003}
2004
niklase@google.com470e71d2011-07-07 08:21:25 +00002005int Channel::StartPlayingFileAsMicrophone(const char* fileName,
pbos@webrtc.org92135212013-05-14 08:31:39 +00002006 bool loop,
2007 FileFormats format,
2008 int startPosition,
2009 float volumeScaling,
2010 int stopPosition,
niklase@google.com470e71d2011-07-07 08:21:25 +00002011 const CodecInst* codecInst)
2012{
2013 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId,_channelId),
2014 "Channel::StartPlayingFileAsMicrophone(fileNameUTF8[]=%s, "
2015 "loop=%d, format=%d, volumeScaling=%5.3f, startPosition=%d, "
2016 "stopPosition=%d)", fileName, loop, format, volumeScaling,
2017 startPosition, stopPosition);
2018
henrika@webrtc.org944cbeb2014-03-18 10:32:33 +00002019 CriticalSectionScoped cs(&_fileCritSect);
2020
2021 if (channel_state_.Get().input_file_playing)
niklase@google.com470e71d2011-07-07 08:21:25 +00002022 {
2023 _engineStatisticsPtr->SetLastError(
2024 VE_ALREADY_PLAYING, kTraceWarning,
2025 "StartPlayingFileAsMicrophone() filePlayer is playing");
2026 return 0;
2027 }
2028
niklase@google.com470e71d2011-07-07 08:21:25 +00002029 // Destroy the old instance
2030 if (_inputFilePlayerPtr)
2031 {
2032 _inputFilePlayerPtr->RegisterModuleFileCallback(NULL);
2033 FilePlayer::DestroyFilePlayer(_inputFilePlayerPtr);
2034 _inputFilePlayerPtr = NULL;
2035 }
2036
2037 // Create the instance
2038 _inputFilePlayerPtr = FilePlayer::CreateFilePlayer(
2039 _inputFilePlayerId, (const FileFormats)format);
2040
2041 if (_inputFilePlayerPtr == NULL)
2042 {
2043 _engineStatisticsPtr->SetLastError(
2044 VE_INVALID_ARGUMENT, kTraceError,
2045 "StartPlayingFileAsMicrophone() filePlayer format isnot correct");
2046 return -1;
2047 }
2048
pbos@webrtc.org6141e132013-04-09 10:09:10 +00002049 const uint32_t notificationTime(0);
niklase@google.com470e71d2011-07-07 08:21:25 +00002050
2051 if (_inputFilePlayerPtr->StartPlayingFile(
2052 fileName,
2053 loop,
2054 startPosition,
2055 volumeScaling,
2056 notificationTime,
2057 stopPosition,
2058 (const CodecInst*)codecInst) != 0)
2059 {
2060 _engineStatisticsPtr->SetLastError(
2061 VE_BAD_FILE, kTraceError,
2062 "StartPlayingFile() failed to start file playout");
2063 _inputFilePlayerPtr->StopPlayingFile();
2064 FilePlayer::DestroyFilePlayer(_inputFilePlayerPtr);
2065 _inputFilePlayerPtr = NULL;
2066 return -1;
2067 }
2068 _inputFilePlayerPtr->RegisterModuleFileCallback(this);
henrika@webrtc.org944cbeb2014-03-18 10:32:33 +00002069 channel_state_.SetInputFilePlaying(true);
niklase@google.com470e71d2011-07-07 08:21:25 +00002070
2071 return 0;
2072}
2073
2074int Channel::StartPlayingFileAsMicrophone(InStream* stream,
pbos@webrtc.org92135212013-05-14 08:31:39 +00002075 FileFormats format,
2076 int startPosition,
2077 float volumeScaling,
2078 int stopPosition,
niklase@google.com470e71d2011-07-07 08:21:25 +00002079 const CodecInst* codecInst)
2080{
2081 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId,_channelId),
2082 "Channel::StartPlayingFileAsMicrophone(format=%d, "
2083 "volumeScaling=%5.3f, startPosition=%d, stopPosition=%d)",
2084 format, volumeScaling, startPosition, stopPosition);
2085
2086 if(stream == NULL)
2087 {
2088 _engineStatisticsPtr->SetLastError(
2089 VE_BAD_FILE, kTraceError,
2090 "StartPlayingFileAsMicrophone NULL as input stream");
2091 return -1;
2092 }
2093
henrika@webrtc.org944cbeb2014-03-18 10:32:33 +00002094 CriticalSectionScoped cs(&_fileCritSect);
2095
2096 if (channel_state_.Get().input_file_playing)
niklase@google.com470e71d2011-07-07 08:21:25 +00002097 {
2098 _engineStatisticsPtr->SetLastError(
2099 VE_ALREADY_PLAYING, kTraceWarning,
2100 "StartPlayingFileAsMicrophone() is playing");
2101 return 0;
2102 }
2103
niklase@google.com470e71d2011-07-07 08:21:25 +00002104 // Destroy the old instance
2105 if (_inputFilePlayerPtr)
2106 {
2107 _inputFilePlayerPtr->RegisterModuleFileCallback(NULL);
2108 FilePlayer::DestroyFilePlayer(_inputFilePlayerPtr);
2109 _inputFilePlayerPtr = NULL;
2110 }
2111
2112 // Create the instance
2113 _inputFilePlayerPtr = FilePlayer::CreateFilePlayer(
2114 _inputFilePlayerId, (const FileFormats)format);
2115
2116 if (_inputFilePlayerPtr == NULL)
2117 {
2118 _engineStatisticsPtr->SetLastError(
2119 VE_INVALID_ARGUMENT, kTraceError,
2120 "StartPlayingInputFile() filePlayer format isnot correct");
2121 return -1;
2122 }
2123
pbos@webrtc.org6141e132013-04-09 10:09:10 +00002124 const uint32_t notificationTime(0);
niklase@google.com470e71d2011-07-07 08:21:25 +00002125
2126 if (_inputFilePlayerPtr->StartPlayingFile(*stream, startPosition,
2127 volumeScaling, notificationTime,
2128 stopPosition, codecInst) != 0)
2129 {
2130 _engineStatisticsPtr->SetLastError(VE_BAD_FILE, kTraceError,
2131 "StartPlayingFile() failed to start "
2132 "file playout");
2133 _inputFilePlayerPtr->StopPlayingFile();
2134 FilePlayer::DestroyFilePlayer(_inputFilePlayerPtr);
2135 _inputFilePlayerPtr = NULL;
2136 return -1;
2137 }
andrew@webrtc.orgae1a58b2013-01-22 04:44:30 +00002138
niklase@google.com470e71d2011-07-07 08:21:25 +00002139 _inputFilePlayerPtr->RegisterModuleFileCallback(this);
henrika@webrtc.org944cbeb2014-03-18 10:32:33 +00002140 channel_state_.SetInputFilePlaying(true);
niklase@google.com470e71d2011-07-07 08:21:25 +00002141
2142 return 0;
2143}
2144
2145int Channel::StopPlayingFileAsMicrophone()
2146{
2147 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId,_channelId),
2148 "Channel::StopPlayingFileAsMicrophone()");
2149
henrika@webrtc.org944cbeb2014-03-18 10:32:33 +00002150 CriticalSectionScoped cs(&_fileCritSect);
2151
2152 if (!channel_state_.Get().input_file_playing)
niklase@google.com470e71d2011-07-07 08:21:25 +00002153 {
2154 _engineStatisticsPtr->SetLastError(
2155 VE_INVALID_OPERATION, kTraceWarning,
2156 "StopPlayingFileAsMicrophone() isnot playing");
2157 return 0;
2158 }
2159
niklase@google.com470e71d2011-07-07 08:21:25 +00002160 if (_inputFilePlayerPtr->StopPlayingFile() != 0)
2161 {
2162 _engineStatisticsPtr->SetLastError(
2163 VE_STOP_RECORDING_FAILED, kTraceError,
2164 "StopPlayingFile() could not stop playing");
2165 return -1;
2166 }
2167 _inputFilePlayerPtr->RegisterModuleFileCallback(NULL);
2168 FilePlayer::DestroyFilePlayer(_inputFilePlayerPtr);
2169 _inputFilePlayerPtr = NULL;
henrika@webrtc.org944cbeb2014-03-18 10:32:33 +00002170 channel_state_.SetInputFilePlaying(false);
niklase@google.com470e71d2011-07-07 08:21:25 +00002171
2172 return 0;
2173}
2174
2175int Channel::IsPlayingFileAsMicrophone() const
2176{
2177 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId,_channelId),
2178 "Channel::IsPlayingFileAsMicrophone()");
henrika@webrtc.org944cbeb2014-03-18 10:32:33 +00002179 return channel_state_.Get().input_file_playing;
niklase@google.com470e71d2011-07-07 08:21:25 +00002180}
2181
leozwang@webrtc.org813e4b02012-03-01 18:34:25 +00002182int Channel::StartRecordingPlayout(const char* fileName,
niklase@google.com470e71d2011-07-07 08:21:25 +00002183 const CodecInst* codecInst)
2184{
2185 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId,_channelId),
2186 "Channel::StartRecordingPlayout(fileName=%s)", fileName);
2187
2188 if (_outputFileRecording)
2189 {
2190 WEBRTC_TRACE(kTraceWarning, kTraceVoice, VoEId(_instanceId,-1),
2191 "StartRecordingPlayout() is already recording");
2192 return 0;
2193 }
2194
2195 FileFormats format;
pbos@webrtc.org6141e132013-04-09 10:09:10 +00002196 const uint32_t notificationTime(0); // Not supported in VoE
niklase@google.com470e71d2011-07-07 08:21:25 +00002197 CodecInst dummyCodec={100,"L16",16000,320,1,320000};
2198
niklas.enbom@webrtc.org40197d72012-03-26 08:45:47 +00002199 if ((codecInst != NULL) &&
2200 ((codecInst->channels < 1) || (codecInst->channels > 2)))
niklase@google.com470e71d2011-07-07 08:21:25 +00002201 {
2202 _engineStatisticsPtr->SetLastError(
2203 VE_BAD_ARGUMENT, kTraceError,
2204 "StartRecordingPlayout() invalid compression");
2205 return(-1);
2206 }
2207 if(codecInst == NULL)
2208 {
2209 format = kFileFormatPcm16kHzFile;
2210 codecInst=&dummyCodec;
2211 }
2212 else if((STR_CASE_CMP(codecInst->plname,"L16") == 0) ||
2213 (STR_CASE_CMP(codecInst->plname,"PCMU") == 0) ||
2214 (STR_CASE_CMP(codecInst->plname,"PCMA") == 0))
2215 {
2216 format = kFileFormatWavFile;
2217 }
2218 else
2219 {
2220 format = kFileFormatCompressedFile;
2221 }
2222
mflodman@webrtc.org9a065d12012-03-07 08:12:21 +00002223 CriticalSectionScoped cs(&_fileCritSect);
niklase@google.com470e71d2011-07-07 08:21:25 +00002224
2225 // Destroy the old instance
2226 if (_outputFileRecorderPtr)
2227 {
2228 _outputFileRecorderPtr->RegisterModuleFileCallback(NULL);
2229 FileRecorder::DestroyFileRecorder(_outputFileRecorderPtr);
2230 _outputFileRecorderPtr = NULL;
2231 }
2232
2233 _outputFileRecorderPtr = FileRecorder::CreateFileRecorder(
2234 _outputFileRecorderId, (const FileFormats)format);
2235 if (_outputFileRecorderPtr == NULL)
2236 {
2237 _engineStatisticsPtr->SetLastError(
2238 VE_INVALID_ARGUMENT, kTraceError,
2239 "StartRecordingPlayout() fileRecorder format isnot correct");
2240 return -1;
2241 }
2242
2243 if (_outputFileRecorderPtr->StartRecordingAudioFile(
2244 fileName, (const CodecInst&)*codecInst, notificationTime) != 0)
2245 {
2246 _engineStatisticsPtr->SetLastError(
2247 VE_BAD_FILE, kTraceError,
2248 "StartRecordingAudioFile() failed to start file recording");
2249 _outputFileRecorderPtr->StopRecording();
2250 FileRecorder::DestroyFileRecorder(_outputFileRecorderPtr);
2251 _outputFileRecorderPtr = NULL;
2252 return -1;
2253 }
2254 _outputFileRecorderPtr->RegisterModuleFileCallback(this);
2255 _outputFileRecording = true;
2256
2257 return 0;
2258}
2259
2260int Channel::StartRecordingPlayout(OutStream* stream,
2261 const CodecInst* codecInst)
2262{
2263 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId,_channelId),
2264 "Channel::StartRecordingPlayout()");
2265
2266 if (_outputFileRecording)
2267 {
2268 WEBRTC_TRACE(kTraceWarning, kTraceVoice, VoEId(_instanceId,-1),
2269 "StartRecordingPlayout() is already recording");
2270 return 0;
2271 }
2272
2273 FileFormats format;
pbos@webrtc.org6141e132013-04-09 10:09:10 +00002274 const uint32_t notificationTime(0); // Not supported in VoE
niklase@google.com470e71d2011-07-07 08:21:25 +00002275 CodecInst dummyCodec={100,"L16",16000,320,1,320000};
2276
2277 if (codecInst != NULL && codecInst->channels != 1)
2278 {
2279 _engineStatisticsPtr->SetLastError(
2280 VE_BAD_ARGUMENT, kTraceError,
2281 "StartRecordingPlayout() invalid compression");
2282 return(-1);
2283 }
2284 if(codecInst == NULL)
2285 {
2286 format = kFileFormatPcm16kHzFile;
2287 codecInst=&dummyCodec;
2288 }
2289 else if((STR_CASE_CMP(codecInst->plname,"L16") == 0) ||
2290 (STR_CASE_CMP(codecInst->plname,"PCMU") == 0) ||
2291 (STR_CASE_CMP(codecInst->plname,"PCMA") == 0))
2292 {
2293 format = kFileFormatWavFile;
2294 }
2295 else
2296 {
2297 format = kFileFormatCompressedFile;
2298 }
2299
mflodman@webrtc.org9a065d12012-03-07 08:12:21 +00002300 CriticalSectionScoped cs(&_fileCritSect);
niklase@google.com470e71d2011-07-07 08:21:25 +00002301
2302 // Destroy the old instance
2303 if (_outputFileRecorderPtr)
2304 {
2305 _outputFileRecorderPtr->RegisterModuleFileCallback(NULL);
2306 FileRecorder::DestroyFileRecorder(_outputFileRecorderPtr);
2307 _outputFileRecorderPtr = NULL;
2308 }
2309
2310 _outputFileRecorderPtr = FileRecorder::CreateFileRecorder(
2311 _outputFileRecorderId, (const FileFormats)format);
2312 if (_outputFileRecorderPtr == NULL)
2313 {
2314 _engineStatisticsPtr->SetLastError(
2315 VE_INVALID_ARGUMENT, kTraceError,
2316 "StartRecordingPlayout() fileRecorder format isnot correct");
2317 return -1;
2318 }
2319
2320 if (_outputFileRecorderPtr->StartRecordingAudioFile(*stream, *codecInst,
2321 notificationTime) != 0)
2322 {
2323 _engineStatisticsPtr->SetLastError(VE_BAD_FILE, kTraceError,
2324 "StartRecordingPlayout() failed to "
2325 "start file recording");
2326 _outputFileRecorderPtr->StopRecording();
2327 FileRecorder::DestroyFileRecorder(_outputFileRecorderPtr);
2328 _outputFileRecorderPtr = NULL;
2329 return -1;
2330 }
andrew@webrtc.orgae1a58b2013-01-22 04:44:30 +00002331
niklase@google.com470e71d2011-07-07 08:21:25 +00002332 _outputFileRecorderPtr->RegisterModuleFileCallback(this);
2333 _outputFileRecording = true;
2334
2335 return 0;
2336}
2337
2338int Channel::StopRecordingPlayout()
2339{
2340 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId,-1),
2341 "Channel::StopRecordingPlayout()");
2342
2343 if (!_outputFileRecording)
2344 {
2345 WEBRTC_TRACE(kTraceError, kTraceVoice, VoEId(_instanceId,-1),
2346 "StopRecordingPlayout() isnot recording");
2347 return -1;
2348 }
2349
2350
mflodman@webrtc.org9a065d12012-03-07 08:12:21 +00002351 CriticalSectionScoped cs(&_fileCritSect);
niklase@google.com470e71d2011-07-07 08:21:25 +00002352
2353 if (_outputFileRecorderPtr->StopRecording() != 0)
2354 {
2355 _engineStatisticsPtr->SetLastError(
2356 VE_STOP_RECORDING_FAILED, kTraceError,
2357 "StopRecording() could not stop recording");
2358 return(-1);
2359 }
2360 _outputFileRecorderPtr->RegisterModuleFileCallback(NULL);
2361 FileRecorder::DestroyFileRecorder(_outputFileRecorderPtr);
2362 _outputFileRecorderPtr = NULL;
2363 _outputFileRecording = false;
2364
2365 return 0;
2366}
2367
2368void
2369Channel::SetMixWithMicStatus(bool mix)
2370{
henrika@webrtc.org944cbeb2014-03-18 10:32:33 +00002371 CriticalSectionScoped cs(&_fileCritSect);
niklase@google.com470e71d2011-07-07 08:21:25 +00002372 _mixFileWithMicrophone=mix;
2373}
2374
2375int
pbos@webrtc.org6141e132013-04-09 10:09:10 +00002376Channel::GetSpeechOutputLevel(uint32_t& level) const
niklase@google.com470e71d2011-07-07 08:21:25 +00002377{
pbos@webrtc.org6141e132013-04-09 10:09:10 +00002378 int8_t currentLevel = _outputAudioLevel.Level();
2379 level = static_cast<int32_t> (currentLevel);
niklase@google.com470e71d2011-07-07 08:21:25 +00002380 WEBRTC_TRACE(kTraceStateInfo, kTraceVoice,
2381 VoEId(_instanceId,_channelId),
2382 "GetSpeechOutputLevel() => level=%u", level);
2383 return 0;
2384}
2385
2386int
pbos@webrtc.org6141e132013-04-09 10:09:10 +00002387Channel::GetSpeechOutputLevelFullRange(uint32_t& level) const
niklase@google.com470e71d2011-07-07 08:21:25 +00002388{
pbos@webrtc.org6141e132013-04-09 10:09:10 +00002389 int16_t currentLevel = _outputAudioLevel.LevelFullRange();
2390 level = static_cast<int32_t> (currentLevel);
niklase@google.com470e71d2011-07-07 08:21:25 +00002391 WEBRTC_TRACE(kTraceStateInfo, kTraceVoice,
2392 VoEId(_instanceId,_channelId),
2393 "GetSpeechOutputLevelFullRange() => level=%u", level);
2394 return 0;
2395}
2396
2397int
2398Channel::SetMute(bool enable)
2399{
wu@webrtc.org63420662013-10-17 18:28:55 +00002400 CriticalSectionScoped cs(&volume_settings_critsect_);
niklase@google.com470e71d2011-07-07 08:21:25 +00002401 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId,_channelId),
2402 "Channel::SetMute(enable=%d)", enable);
2403 _mute = enable;
2404 return 0;
2405}
2406
2407bool
2408Channel::Mute() const
2409{
wu@webrtc.org63420662013-10-17 18:28:55 +00002410 CriticalSectionScoped cs(&volume_settings_critsect_);
niklase@google.com470e71d2011-07-07 08:21:25 +00002411 return _mute;
2412}
2413
2414int
2415Channel::SetOutputVolumePan(float left, float right)
2416{
wu@webrtc.org63420662013-10-17 18:28:55 +00002417 CriticalSectionScoped cs(&volume_settings_critsect_);
niklase@google.com470e71d2011-07-07 08:21:25 +00002418 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId,_channelId),
2419 "Channel::SetOutputVolumePan()");
2420 _panLeft = left;
2421 _panRight = right;
2422 return 0;
2423}
2424
2425int
2426Channel::GetOutputVolumePan(float& left, float& right) const
2427{
wu@webrtc.org63420662013-10-17 18:28:55 +00002428 CriticalSectionScoped cs(&volume_settings_critsect_);
niklase@google.com470e71d2011-07-07 08:21:25 +00002429 left = _panLeft;
2430 right = _panRight;
2431 WEBRTC_TRACE(kTraceStateInfo, kTraceVoice,
2432 VoEId(_instanceId,_channelId),
2433 "GetOutputVolumePan() => left=%3.2f, right=%3.2f", left, right);
2434 return 0;
2435}
2436
2437int
2438Channel::SetChannelOutputVolumeScaling(float scaling)
2439{
wu@webrtc.org63420662013-10-17 18:28:55 +00002440 CriticalSectionScoped cs(&volume_settings_critsect_);
niklase@google.com470e71d2011-07-07 08:21:25 +00002441 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId,_channelId),
2442 "Channel::SetChannelOutputVolumeScaling()");
2443 _outputGain = scaling;
2444 return 0;
2445}
2446
2447int
2448Channel::GetChannelOutputVolumeScaling(float& scaling) const
2449{
wu@webrtc.org63420662013-10-17 18:28:55 +00002450 CriticalSectionScoped cs(&volume_settings_critsect_);
niklase@google.com470e71d2011-07-07 08:21:25 +00002451 scaling = _outputGain;
2452 WEBRTC_TRACE(kTraceStateInfo, kTraceVoice,
2453 VoEId(_instanceId,_channelId),
2454 "GetChannelOutputVolumeScaling() => scaling=%3.2f", scaling);
2455 return 0;
2456}
2457
niklase@google.com470e71d2011-07-07 08:21:25 +00002458int Channel::SendTelephoneEventOutband(unsigned char eventCode,
wu@webrtc.org822fbd82013-08-15 23:38:54 +00002459 int lengthMs, int attenuationDb,
2460 bool playDtmfEvent)
niklase@google.com470e71d2011-07-07 08:21:25 +00002461{
2462 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId, _channelId),
2463 "Channel::SendTelephoneEventOutband(..., playDtmfEvent=%d)",
2464 playDtmfEvent);
2465
2466 _playOutbandDtmfEvent = playDtmfEvent;
2467
pwestin@webrtc.org2853dde2012-05-11 11:08:54 +00002468 if (_rtpRtcpModule->SendTelephoneEventOutband(eventCode, lengthMs,
niklase@google.com470e71d2011-07-07 08:21:25 +00002469 attenuationDb) != 0)
2470 {
2471 _engineStatisticsPtr->SetLastError(
2472 VE_SEND_DTMF_FAILED,
2473 kTraceWarning,
2474 "SendTelephoneEventOutband() failed to send event");
2475 return -1;
2476 }
2477 return 0;
2478}
2479
2480int Channel::SendTelephoneEventInband(unsigned char eventCode,
2481 int lengthMs,
2482 int attenuationDb,
2483 bool playDtmfEvent)
2484{
2485 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId, _channelId),
2486 "Channel::SendTelephoneEventInband(..., playDtmfEvent=%d)",
2487 playDtmfEvent);
2488
2489 _playInbandDtmfEvent = playDtmfEvent;
2490 _inbandDtmfQueue.AddDtmf(eventCode, lengthMs, attenuationDb);
2491
2492 return 0;
2493}
2494
2495int
niklase@google.com470e71d2011-07-07 08:21:25 +00002496Channel::SetSendTelephoneEventPayloadType(unsigned char type)
2497{
2498 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId,_channelId),
2499 "Channel::SetSendTelephoneEventPayloadType()");
andrew@webrtc.orgf81f9f82011-08-19 22:56:22 +00002500 if (type > 127)
niklase@google.com470e71d2011-07-07 08:21:25 +00002501 {
2502 _engineStatisticsPtr->SetLastError(
2503 VE_INVALID_ARGUMENT, kTraceError,
2504 "SetSendTelephoneEventPayloadType() invalid type");
2505 return -1;
2506 }
pbos@webrtc.org5b10d8f2013-07-11 15:50:07 +00002507 CodecInst codec = {};
pwestin@webrtc.org1da1ce02011-10-13 15:19:55 +00002508 codec.plfreq = 8000;
2509 codec.pltype = type;
2510 memcpy(codec.plname, "telephone-event", 16);
pwestin@webrtc.org2853dde2012-05-11 11:08:54 +00002511 if (_rtpRtcpModule->RegisterSendPayload(codec) != 0)
niklase@google.com470e71d2011-07-07 08:21:25 +00002512 {
henrika@webrtc.org4392d5f2013-04-17 07:34:25 +00002513 _rtpRtcpModule->DeRegisterSendPayload(codec.pltype);
2514 if (_rtpRtcpModule->RegisterSendPayload(codec) != 0) {
2515 _engineStatisticsPtr->SetLastError(
2516 VE_RTP_RTCP_MODULE_ERROR, kTraceError,
2517 "SetSendTelephoneEventPayloadType() failed to register send"
2518 "payload type");
2519 return -1;
2520 }
niklase@google.com470e71d2011-07-07 08:21:25 +00002521 }
2522 _sendTelephoneEventPayloadType = type;
2523 return 0;
2524}
2525
2526int
2527Channel::GetSendTelephoneEventPayloadType(unsigned char& type)
2528{
2529 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId,_channelId),
2530 "Channel::GetSendTelephoneEventPayloadType()");
2531 type = _sendTelephoneEventPayloadType;
2532 WEBRTC_TRACE(kTraceStateInfo, kTraceVoice,
2533 VoEId(_instanceId,_channelId),
2534 "GetSendTelephoneEventPayloadType() => type=%u", type);
2535 return 0;
2536}
2537
niklase@google.com470e71d2011-07-07 08:21:25 +00002538int
2539Channel::UpdateRxVadDetection(AudioFrame& audioFrame)
2540{
2541 WEBRTC_TRACE(kTraceStream, kTraceVoice, VoEId(_instanceId,_channelId),
2542 "Channel::UpdateRxVadDetection()");
2543
2544 int vadDecision = 1;
2545
andrew@webrtc.org63a50982012-05-02 23:56:37 +00002546 vadDecision = (audioFrame.vad_activity_ == AudioFrame::kVadActive)? 1 : 0;
niklase@google.com470e71d2011-07-07 08:21:25 +00002547
2548 if ((vadDecision != _oldVadDecision) && _rxVadObserverPtr)
2549 {
2550 OnRxVadDetected(vadDecision);
2551 _oldVadDecision = vadDecision;
2552 }
2553
2554 WEBRTC_TRACE(kTraceStream, kTraceVoice, VoEId(_instanceId,_channelId),
2555 "Channel::UpdateRxVadDetection() => vadDecision=%d",
2556 vadDecision);
2557 return 0;
2558}
2559
2560int
2561Channel::RegisterRxVadObserver(VoERxVadCallback &observer)
2562{
2563 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId,_channelId),
2564 "Channel::RegisterRxVadObserver()");
mflodman@webrtc.org9a065d12012-03-07 08:12:21 +00002565 CriticalSectionScoped cs(&_callbackCritSect);
niklase@google.com470e71d2011-07-07 08:21:25 +00002566
2567 if (_rxVadObserverPtr)
2568 {
2569 _engineStatisticsPtr->SetLastError(
2570 VE_INVALID_OPERATION, kTraceError,
2571 "RegisterRxVadObserver() observer already enabled");
2572 return -1;
2573 }
niklase@google.com470e71d2011-07-07 08:21:25 +00002574 _rxVadObserverPtr = &observer;
2575 _RxVadDetection = true;
2576 return 0;
2577}
2578
2579int
2580Channel::DeRegisterRxVadObserver()
2581{
2582 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId,_channelId),
2583 "Channel::DeRegisterRxVadObserver()");
mflodman@webrtc.org9a065d12012-03-07 08:12:21 +00002584 CriticalSectionScoped cs(&_callbackCritSect);
niklase@google.com470e71d2011-07-07 08:21:25 +00002585
2586 if (!_rxVadObserverPtr)
2587 {
2588 _engineStatisticsPtr->SetLastError(
2589 VE_INVALID_OPERATION, kTraceWarning,
2590 "DeRegisterRxVadObserver() observer already disabled");
2591 return 0;
2592 }
2593 _rxVadObserverPtr = NULL;
2594 _RxVadDetection = false;
2595 return 0;
2596}
2597
2598int
2599Channel::VoiceActivityIndicator(int &activity)
2600{
2601 activity = _sendFrameType;
2602
2603 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId,_channelId),
andrew@webrtc.org6c264cc2013-10-04 17:54:09 +00002604 "Channel::VoiceActivityIndicator(indicator=%d)", activity);
niklase@google.com470e71d2011-07-07 08:21:25 +00002605 return 0;
2606}
2607
2608#ifdef WEBRTC_VOICE_ENGINE_AGC
2609
2610int
pbos@webrtc.org92135212013-05-14 08:31:39 +00002611Channel::SetRxAgcStatus(bool enable, AgcModes mode)
niklase@google.com470e71d2011-07-07 08:21:25 +00002612{
2613 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId,_channelId),
2614 "Channel::SetRxAgcStatus(enable=%d, mode=%d)",
2615 (int)enable, (int)mode);
2616
andrew@webrtc.org6c264cc2013-10-04 17:54:09 +00002617 GainControl::Mode agcMode = kDefaultRxAgcMode;
niklase@google.com470e71d2011-07-07 08:21:25 +00002618 switch (mode)
2619 {
2620 case kAgcDefault:
niklase@google.com470e71d2011-07-07 08:21:25 +00002621 break;
2622 case kAgcUnchanged:
andrew@webrtc.orgf3930e92013-09-18 22:37:32 +00002623 agcMode = rx_audioproc_->gain_control()->mode();
niklase@google.com470e71d2011-07-07 08:21:25 +00002624 break;
2625 case kAgcFixedDigital:
2626 agcMode = GainControl::kFixedDigital;
2627 break;
2628 case kAgcAdaptiveDigital:
2629 agcMode =GainControl::kAdaptiveDigital;
2630 break;
2631 default:
2632 _engineStatisticsPtr->SetLastError(
2633 VE_INVALID_ARGUMENT, kTraceError,
2634 "SetRxAgcStatus() invalid Agc mode");
2635 return -1;
2636 }
2637
andrew@webrtc.orgf3930e92013-09-18 22:37:32 +00002638 if (rx_audioproc_->gain_control()->set_mode(agcMode) != 0)
niklase@google.com470e71d2011-07-07 08:21:25 +00002639 {
2640 _engineStatisticsPtr->SetLastError(
2641 VE_APM_ERROR, kTraceError,
2642 "SetRxAgcStatus() failed to set Agc mode");
2643 return -1;
2644 }
andrew@webrtc.orgf3930e92013-09-18 22:37:32 +00002645 if (rx_audioproc_->gain_control()->Enable(enable) != 0)
niklase@google.com470e71d2011-07-07 08:21:25 +00002646 {
2647 _engineStatisticsPtr->SetLastError(
2648 VE_APM_ERROR, kTraceError,
2649 "SetRxAgcStatus() failed to set Agc state");
2650 return -1;
2651 }
2652
2653 _rxAgcIsEnabled = enable;
henrika@webrtc.org944cbeb2014-03-18 10:32:33 +00002654 channel_state_.SetRxApmIsEnabled(_rxAgcIsEnabled || _rxNsIsEnabled);
niklase@google.com470e71d2011-07-07 08:21:25 +00002655
2656 return 0;
2657}
2658
2659int
2660Channel::GetRxAgcStatus(bool& enabled, AgcModes& mode)
2661{
2662 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId,_channelId),
2663 "Channel::GetRxAgcStatus(enable=?, mode=?)");
2664
andrew@webrtc.orgf3930e92013-09-18 22:37:32 +00002665 bool enable = rx_audioproc_->gain_control()->is_enabled();
niklase@google.com470e71d2011-07-07 08:21:25 +00002666 GainControl::Mode agcMode =
andrew@webrtc.orgf3930e92013-09-18 22:37:32 +00002667 rx_audioproc_->gain_control()->mode();
niklase@google.com470e71d2011-07-07 08:21:25 +00002668
2669 enabled = enable;
2670
2671 switch (agcMode)
2672 {
2673 case GainControl::kFixedDigital:
2674 mode = kAgcFixedDigital;
2675 break;
2676 case GainControl::kAdaptiveDigital:
2677 mode = kAgcAdaptiveDigital;
2678 break;
2679 default:
2680 _engineStatisticsPtr->SetLastError(
2681 VE_APM_ERROR, kTraceError,
2682 "GetRxAgcStatus() invalid Agc mode");
2683 return -1;
2684 }
2685
2686 return 0;
2687}
2688
2689int
pbos@webrtc.org92135212013-05-14 08:31:39 +00002690Channel::SetRxAgcConfig(AgcConfig config)
niklase@google.com470e71d2011-07-07 08:21:25 +00002691{
2692 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId,_channelId),
2693 "Channel::SetRxAgcConfig()");
2694
andrew@webrtc.orgf3930e92013-09-18 22:37:32 +00002695 if (rx_audioproc_->gain_control()->set_target_level_dbfs(
niklase@google.com470e71d2011-07-07 08:21:25 +00002696 config.targetLeveldBOv) != 0)
2697 {
2698 _engineStatisticsPtr->SetLastError(
2699 VE_APM_ERROR, kTraceError,
2700 "SetRxAgcConfig() failed to set target peak |level|"
2701 "(or envelope) of the Agc");
2702 return -1;
2703 }
andrew@webrtc.orgf3930e92013-09-18 22:37:32 +00002704 if (rx_audioproc_->gain_control()->set_compression_gain_db(
niklase@google.com470e71d2011-07-07 08:21:25 +00002705 config.digitalCompressionGaindB) != 0)
2706 {
2707 _engineStatisticsPtr->SetLastError(
2708 VE_APM_ERROR, kTraceError,
2709 "SetRxAgcConfig() failed to set the range in |gain| the"
2710 " digital compression stage may apply");
2711 return -1;
2712 }
andrew@webrtc.orgf3930e92013-09-18 22:37:32 +00002713 if (rx_audioproc_->gain_control()->enable_limiter(
niklase@google.com470e71d2011-07-07 08:21:25 +00002714 config.limiterEnable) != 0)
2715 {
2716 _engineStatisticsPtr->SetLastError(
2717 VE_APM_ERROR, kTraceError,
2718 "SetRxAgcConfig() failed to set hard limiter to the signal");
2719 return -1;
2720 }
2721
2722 return 0;
2723}
2724
2725int
2726Channel::GetRxAgcConfig(AgcConfig& config)
2727{
2728 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId,_channelId),
2729 "Channel::GetRxAgcConfig(config=%?)");
2730
2731 config.targetLeveldBOv =
andrew@webrtc.orgf3930e92013-09-18 22:37:32 +00002732 rx_audioproc_->gain_control()->target_level_dbfs();
niklase@google.com470e71d2011-07-07 08:21:25 +00002733 config.digitalCompressionGaindB =
andrew@webrtc.orgf3930e92013-09-18 22:37:32 +00002734 rx_audioproc_->gain_control()->compression_gain_db();
niklase@google.com470e71d2011-07-07 08:21:25 +00002735 config.limiterEnable =
andrew@webrtc.orgf3930e92013-09-18 22:37:32 +00002736 rx_audioproc_->gain_control()->is_limiter_enabled();
niklase@google.com470e71d2011-07-07 08:21:25 +00002737
2738 WEBRTC_TRACE(kTraceStateInfo, kTraceVoice,
2739 VoEId(_instanceId,_channelId), "GetRxAgcConfig() => "
2740 "targetLeveldBOv=%u, digitalCompressionGaindB=%u,"
2741 " limiterEnable=%d",
2742 config.targetLeveldBOv,
2743 config.digitalCompressionGaindB,
2744 config.limiterEnable);
2745
2746 return 0;
2747}
2748
2749#endif // #ifdef WEBRTC_VOICE_ENGINE_AGC
2750
2751#ifdef WEBRTC_VOICE_ENGINE_NR
2752
2753int
pbos@webrtc.org92135212013-05-14 08:31:39 +00002754Channel::SetRxNsStatus(bool enable, NsModes mode)
niklase@google.com470e71d2011-07-07 08:21:25 +00002755{
2756 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId,_channelId),
2757 "Channel::SetRxNsStatus(enable=%d, mode=%d)",
2758 (int)enable, (int)mode);
2759
andrew@webrtc.org6c264cc2013-10-04 17:54:09 +00002760 NoiseSuppression::Level nsLevel = kDefaultNsMode;
niklase@google.com470e71d2011-07-07 08:21:25 +00002761 switch (mode)
2762 {
2763
2764 case kNsDefault:
niklase@google.com470e71d2011-07-07 08:21:25 +00002765 break;
2766 case kNsUnchanged:
andrew@webrtc.orgf3930e92013-09-18 22:37:32 +00002767 nsLevel = rx_audioproc_->noise_suppression()->level();
niklase@google.com470e71d2011-07-07 08:21:25 +00002768 break;
2769 case kNsConference:
2770 nsLevel = NoiseSuppression::kHigh;
2771 break;
2772 case kNsLowSuppression:
2773 nsLevel = NoiseSuppression::kLow;
2774 break;
2775 case kNsModerateSuppression:
2776 nsLevel = NoiseSuppression::kModerate;
2777 break;
2778 case kNsHighSuppression:
2779 nsLevel = NoiseSuppression::kHigh;
2780 break;
2781 case kNsVeryHighSuppression:
2782 nsLevel = NoiseSuppression::kVeryHigh;
2783 break;
niklase@google.com470e71d2011-07-07 08:21:25 +00002784 }
2785
andrew@webrtc.orgf3930e92013-09-18 22:37:32 +00002786 if (rx_audioproc_->noise_suppression()->set_level(nsLevel)
niklase@google.com470e71d2011-07-07 08:21:25 +00002787 != 0)
2788 {
2789 _engineStatisticsPtr->SetLastError(
2790 VE_APM_ERROR, kTraceError,
andrew@webrtc.org6c264cc2013-10-04 17:54:09 +00002791 "SetRxNsStatus() failed to set NS level");
niklase@google.com470e71d2011-07-07 08:21:25 +00002792 return -1;
2793 }
andrew@webrtc.orgf3930e92013-09-18 22:37:32 +00002794 if (rx_audioproc_->noise_suppression()->Enable(enable) != 0)
niklase@google.com470e71d2011-07-07 08:21:25 +00002795 {
2796 _engineStatisticsPtr->SetLastError(
2797 VE_APM_ERROR, kTraceError,
andrew@webrtc.org6c264cc2013-10-04 17:54:09 +00002798 "SetRxNsStatus() failed to set NS state");
niklase@google.com470e71d2011-07-07 08:21:25 +00002799 return -1;
2800 }
2801
2802 _rxNsIsEnabled = enable;
henrika@webrtc.org944cbeb2014-03-18 10:32:33 +00002803 channel_state_.SetRxApmIsEnabled(_rxAgcIsEnabled || _rxNsIsEnabled);
niklase@google.com470e71d2011-07-07 08:21:25 +00002804
2805 return 0;
2806}
2807
2808int
2809Channel::GetRxNsStatus(bool& enabled, NsModes& mode)
2810{
2811 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId,_channelId),
2812 "Channel::GetRxNsStatus(enable=?, mode=?)");
2813
2814 bool enable =
andrew@webrtc.orgf3930e92013-09-18 22:37:32 +00002815 rx_audioproc_->noise_suppression()->is_enabled();
niklase@google.com470e71d2011-07-07 08:21:25 +00002816 NoiseSuppression::Level ncLevel =
andrew@webrtc.orgf3930e92013-09-18 22:37:32 +00002817 rx_audioproc_->noise_suppression()->level();
niklase@google.com470e71d2011-07-07 08:21:25 +00002818
2819 enabled = enable;
2820
2821 switch (ncLevel)
2822 {
2823 case NoiseSuppression::kLow:
2824 mode = kNsLowSuppression;
2825 break;
2826 case NoiseSuppression::kModerate:
2827 mode = kNsModerateSuppression;
2828 break;
2829 case NoiseSuppression::kHigh:
2830 mode = kNsHighSuppression;
2831 break;
2832 case NoiseSuppression::kVeryHigh:
2833 mode = kNsVeryHighSuppression;
2834 break;
niklase@google.com470e71d2011-07-07 08:21:25 +00002835 }
2836
2837 WEBRTC_TRACE(kTraceStateInfo, kTraceVoice,
2838 VoEId(_instanceId,_channelId),
2839 "GetRxNsStatus() => enabled=%d, mode=%d", enabled, mode);
2840 return 0;
2841}
2842
2843#endif // #ifdef WEBRTC_VOICE_ENGINE_NR
2844
2845int
niklase@google.com470e71d2011-07-07 08:21:25 +00002846Channel::SetLocalSSRC(unsigned int ssrc)
2847{
2848 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId, _channelId),
2849 "Channel::SetLocalSSRC()");
henrika@webrtc.org944cbeb2014-03-18 10:32:33 +00002850 if (channel_state_.Get().sending)
niklase@google.com470e71d2011-07-07 08:21:25 +00002851 {
2852 _engineStatisticsPtr->SetLastError(
2853 VE_ALREADY_SENDING, kTraceError,
2854 "SetLocalSSRC() already sending");
2855 return -1;
2856 }
stefan@webrtc.orgef927552014-06-05 08:25:29 +00002857 _rtpRtcpModule->SetSSRC(ssrc);
niklase@google.com470e71d2011-07-07 08:21:25 +00002858 return 0;
2859}
2860
2861int
2862Channel::GetLocalSSRC(unsigned int& ssrc)
2863{
pwestin@webrtc.org2853dde2012-05-11 11:08:54 +00002864 ssrc = _rtpRtcpModule->SSRC();
niklase@google.com470e71d2011-07-07 08:21:25 +00002865 WEBRTC_TRACE(kTraceStateInfo, kTraceVoice,
2866 VoEId(_instanceId,_channelId),
2867 "GetLocalSSRC() => ssrc=%lu", ssrc);
2868 return 0;
2869}
2870
2871int
2872Channel::GetRemoteSSRC(unsigned int& ssrc)
2873{
wu@webrtc.org822fbd82013-08-15 23:38:54 +00002874 ssrc = rtp_receiver_->SSRC();
niklase@google.com470e71d2011-07-07 08:21:25 +00002875 WEBRTC_TRACE(kTraceStateInfo, kTraceVoice,
2876 VoEId(_instanceId,_channelId),
2877 "GetRemoteSSRC() => ssrc=%lu", ssrc);
2878 return 0;
2879}
2880
wu@webrtc.orgebdb0e32014-03-06 23:49:08 +00002881int Channel::SetSendAudioLevelIndicationStatus(bool enable, unsigned char id) {
andrew@webrtc.orgf3930e92013-09-18 22:37:32 +00002882 _includeAudioLevelIndication = enable;
wu@webrtc.orgebdb0e32014-03-06 23:49:08 +00002883 return SetSendRtpHeaderExtension(enable, kRtpExtensionAudioLevel, id);
niklase@google.com470e71d2011-07-07 08:21:25 +00002884}
andrew@webrtc.orgf3930e92013-09-18 22:37:32 +00002885
wu@webrtc.org93fd25c2014-04-24 20:33:08 +00002886int Channel::SetReceiveAudioLevelIndicationStatus(bool enable,
2887 unsigned char id) {
2888 rtp_header_parser_->DeregisterRtpHeaderExtension(
2889 kRtpExtensionAudioLevel);
2890 if (enable && !rtp_header_parser_->RegisterRtpHeaderExtension(
2891 kRtpExtensionAudioLevel, id)) {
2892 return -1;
2893 }
2894 return 0;
2895}
2896
wu@webrtc.orgebdb0e32014-03-06 23:49:08 +00002897int Channel::SetSendAbsoluteSenderTimeStatus(bool enable, unsigned char id) {
2898 return SetSendRtpHeaderExtension(enable, kRtpExtensionAbsoluteSendTime, id);
2899}
2900
2901int Channel::SetReceiveAbsoluteSenderTimeStatus(bool enable, unsigned char id) {
2902 rtp_header_parser_->DeregisterRtpHeaderExtension(
2903 kRtpExtensionAbsoluteSendTime);
solenberg@webrtc.orgb1f50102014-03-24 10:38:25 +00002904 if (enable && !rtp_header_parser_->RegisterRtpHeaderExtension(
2905 kRtpExtensionAbsoluteSendTime, id)) {
2906 return -1;
wu@webrtc.orgebdb0e32014-03-06 23:49:08 +00002907 }
2908 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +00002909}
2910
2911int
2912Channel::SetRTCPStatus(bool enable)
2913{
2914 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId,_channelId),
2915 "Channel::SetRTCPStatus()");
pwestin@webrtc.org2853dde2012-05-11 11:08:54 +00002916 if (_rtpRtcpModule->SetRTCPStatus(enable ?
niklase@google.com470e71d2011-07-07 08:21:25 +00002917 kRtcpCompound : kRtcpOff) != 0)
2918 {
2919 _engineStatisticsPtr->SetLastError(
2920 VE_RTP_RTCP_MODULE_ERROR, kTraceError,
2921 "SetRTCPStatus() failed to set RTCP status");
2922 return -1;
2923 }
2924 return 0;
2925}
2926
2927int
2928Channel::GetRTCPStatus(bool& enabled)
2929{
pwestin@webrtc.org2853dde2012-05-11 11:08:54 +00002930 RTCPMethod method = _rtpRtcpModule->RTCP();
niklase@google.com470e71d2011-07-07 08:21:25 +00002931 enabled = (method != kRtcpOff);
2932 WEBRTC_TRACE(kTraceStateInfo, kTraceVoice,
2933 VoEId(_instanceId,_channelId),
2934 "GetRTCPStatus() => enabled=%d", enabled);
2935 return 0;
2936}
2937
2938int
2939Channel::SetRTCP_CNAME(const char cName[256])
2940{
2941 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId, _channelId),
2942 "Channel::SetRTCP_CNAME()");
pwestin@webrtc.org2853dde2012-05-11 11:08:54 +00002943 if (_rtpRtcpModule->SetCNAME(cName) != 0)
niklase@google.com470e71d2011-07-07 08:21:25 +00002944 {
2945 _engineStatisticsPtr->SetLastError(
2946 VE_RTP_RTCP_MODULE_ERROR, kTraceError,
2947 "SetRTCP_CNAME() failed to set RTCP CNAME");
2948 return -1;
2949 }
2950 return 0;
2951}
2952
2953int
niklase@google.com470e71d2011-07-07 08:21:25 +00002954Channel::GetRemoteRTCP_CNAME(char cName[256])
2955{
2956 if (cName == NULL)
2957 {
2958 _engineStatisticsPtr->SetLastError(
2959 VE_INVALID_ARGUMENT, kTraceError,
2960 "GetRemoteRTCP_CNAME() invalid CNAME input buffer");
2961 return -1;
2962 }
leozwang@webrtc.org813e4b02012-03-01 18:34:25 +00002963 char cname[RTCP_CNAME_SIZE];
wu@webrtc.org822fbd82013-08-15 23:38:54 +00002964 const uint32_t remoteSSRC = rtp_receiver_->SSRC();
pwestin@webrtc.org2853dde2012-05-11 11:08:54 +00002965 if (_rtpRtcpModule->RemoteCNAME(remoteSSRC, cname) != 0)
niklase@google.com470e71d2011-07-07 08:21:25 +00002966 {
2967 _engineStatisticsPtr->SetLastError(
2968 VE_CANNOT_RETRIEVE_CNAME, kTraceError,
2969 "GetRemoteRTCP_CNAME() failed to retrieve remote RTCP CNAME");
2970 return -1;
2971 }
2972 strcpy(cName, cname);
2973 WEBRTC_TRACE(kTraceStateInfo, kTraceVoice,
2974 VoEId(_instanceId, _channelId),
2975 "GetRemoteRTCP_CNAME() => cName=%s", cName);
2976 return 0;
2977}
2978
2979int
2980Channel::GetRemoteRTCPData(
2981 unsigned int& NTPHigh,
2982 unsigned int& NTPLow,
2983 unsigned int& timestamp,
2984 unsigned int& playoutTimestamp,
2985 unsigned int* jitter,
2986 unsigned short* fractionLost)
2987{
2988 // --- Information from sender info in received Sender Reports
2989
2990 RTCPSenderInfo senderInfo;
pwestin@webrtc.org2853dde2012-05-11 11:08:54 +00002991 if (_rtpRtcpModule->RemoteRTCPStat(&senderInfo) != 0)
niklase@google.com470e71d2011-07-07 08:21:25 +00002992 {
2993 _engineStatisticsPtr->SetLastError(
2994 VE_RTP_RTCP_MODULE_ERROR, kTraceError,
wu@webrtc.orgfcd12b32011-09-15 20:49:50 +00002995 "GetRemoteRTCPData() failed to retrieve sender info for remote "
niklase@google.com470e71d2011-07-07 08:21:25 +00002996 "side");
2997 return -1;
2998 }
2999
3000 // We only utilize 12 out of 20 bytes in the sender info (ignores packet
3001 // and octet count)
3002 NTPHigh = senderInfo.NTPseconds;
3003 NTPLow = senderInfo.NTPfraction;
3004 timestamp = senderInfo.RTPtimeStamp;
3005
3006 WEBRTC_TRACE(kTraceStateInfo, kTraceVoice,
3007 VoEId(_instanceId, _channelId),
3008 "GetRemoteRTCPData() => NTPHigh=%lu, NTPLow=%lu, "
3009 "timestamp=%lu",
3010 NTPHigh, NTPLow, timestamp);
3011
3012 // --- Locally derived information
3013
3014 // This value is updated on each incoming RTCP packet (0 when no packet
3015 // has been received)
pwestin@webrtc.org1de01352013-04-11 20:23:35 +00003016 playoutTimestamp = playout_timestamp_rtcp_;
niklase@google.com470e71d2011-07-07 08:21:25 +00003017
3018 WEBRTC_TRACE(kTraceStateInfo, kTraceVoice,
3019 VoEId(_instanceId, _channelId),
3020 "GetRemoteRTCPData() => playoutTimestamp=%lu",
pwestin@webrtc.org1de01352013-04-11 20:23:35 +00003021 playout_timestamp_rtcp_);
niklase@google.com470e71d2011-07-07 08:21:25 +00003022
3023 if (NULL != jitter || NULL != fractionLost)
3024 {
perkj@webrtc.orgce5990c2012-01-11 13:00:08 +00003025 // Get all RTCP receiver report blocks that have been received on this
3026 // channel. If we receive RTP packets from a remote source we know the
3027 // remote SSRC and use the report block from him.
3028 // Otherwise use the first report block.
3029 std::vector<RTCPReportBlock> remote_stats;
pwestin@webrtc.org2853dde2012-05-11 11:08:54 +00003030 if (_rtpRtcpModule->RemoteRTCPStat(&remote_stats) != 0 ||
perkj@webrtc.orgce5990c2012-01-11 13:00:08 +00003031 remote_stats.empty()) {
3032 WEBRTC_TRACE(kTraceWarning, kTraceVoice,
3033 VoEId(_instanceId, _channelId),
3034 "GetRemoteRTCPData() failed to measure statistics due"
3035 " to lack of received RTP and/or RTCP packets");
3036 return -1;
niklase@google.com470e71d2011-07-07 08:21:25 +00003037 }
perkj@webrtc.orgce5990c2012-01-11 13:00:08 +00003038
wu@webrtc.org822fbd82013-08-15 23:38:54 +00003039 uint32_t remoteSSRC = rtp_receiver_->SSRC();
perkj@webrtc.orgce5990c2012-01-11 13:00:08 +00003040 std::vector<RTCPReportBlock>::const_iterator it = remote_stats.begin();
3041 for (; it != remote_stats.end(); ++it) {
3042 if (it->remoteSSRC == remoteSSRC)
3043 break;
niklase@google.com470e71d2011-07-07 08:21:25 +00003044 }
perkj@webrtc.orgce5990c2012-01-11 13:00:08 +00003045
3046 if (it == remote_stats.end()) {
3047 // If we have not received any RTCP packets from this SSRC it probably
3048 // means that we have not received any RTP packets.
3049 // Use the first received report block instead.
3050 it = remote_stats.begin();
3051 remoteSSRC = it->remoteSSRC;
niklase@google.com470e71d2011-07-07 08:21:25 +00003052 }
perkj@webrtc.orgce5990c2012-01-11 13:00:08 +00003053
xians@webrtc.org79af7342012-01-31 12:22:14 +00003054 if (jitter) {
3055 *jitter = it->jitter;
3056 WEBRTC_TRACE(kTraceStateInfo, kTraceVoice,
3057 VoEId(_instanceId, _channelId),
3058 "GetRemoteRTCPData() => jitter = %lu", *jitter);
3059 }
perkj@webrtc.orgce5990c2012-01-11 13:00:08 +00003060
xians@webrtc.org79af7342012-01-31 12:22:14 +00003061 if (fractionLost) {
3062 *fractionLost = it->fractionLost;
3063 WEBRTC_TRACE(kTraceStateInfo, kTraceVoice,
3064 VoEId(_instanceId, _channelId),
3065 "GetRemoteRTCPData() => fractionLost = %lu",
3066 *fractionLost);
3067 }
niklase@google.com470e71d2011-07-07 08:21:25 +00003068 }
3069 return 0;
3070}
3071
3072int
pbos@webrtc.org92135212013-05-14 08:31:39 +00003073Channel::SendApplicationDefinedRTCPPacket(unsigned char subType,
niklase@google.com470e71d2011-07-07 08:21:25 +00003074 unsigned int name,
3075 const char* data,
3076 unsigned short dataLengthInBytes)
3077{
3078 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId, _channelId),
3079 "Channel::SendApplicationDefinedRTCPPacket()");
henrika@webrtc.org944cbeb2014-03-18 10:32:33 +00003080 if (!channel_state_.Get().sending)
niklase@google.com470e71d2011-07-07 08:21:25 +00003081 {
3082 _engineStatisticsPtr->SetLastError(
3083 VE_NOT_SENDING, kTraceError,
3084 "SendApplicationDefinedRTCPPacket() not sending");
3085 return -1;
3086 }
3087 if (NULL == data)
3088 {
3089 _engineStatisticsPtr->SetLastError(
3090 VE_INVALID_ARGUMENT, kTraceError,
3091 "SendApplicationDefinedRTCPPacket() invalid data value");
3092 return -1;
3093 }
3094 if (dataLengthInBytes % 4 != 0)
3095 {
3096 _engineStatisticsPtr->SetLastError(
3097 VE_INVALID_ARGUMENT, kTraceError,
3098 "SendApplicationDefinedRTCPPacket() invalid length value");
3099 return -1;
3100 }
pwestin@webrtc.org2853dde2012-05-11 11:08:54 +00003101 RTCPMethod status = _rtpRtcpModule->RTCP();
niklase@google.com470e71d2011-07-07 08:21:25 +00003102 if (status == kRtcpOff)
3103 {
3104 _engineStatisticsPtr->SetLastError(
3105 VE_RTCP_ERROR, kTraceError,
3106 "SendApplicationDefinedRTCPPacket() RTCP is disabled");
3107 return -1;
3108 }
3109
3110 // Create and schedule the RTCP APP packet for transmission
pwestin@webrtc.org2853dde2012-05-11 11:08:54 +00003111 if (_rtpRtcpModule->SetRTCPApplicationSpecificData(
niklase@google.com470e71d2011-07-07 08:21:25 +00003112 subType,
3113 name,
3114 (const unsigned char*) data,
3115 dataLengthInBytes) != 0)
3116 {
3117 _engineStatisticsPtr->SetLastError(
3118 VE_SEND_ERROR, kTraceError,
3119 "SendApplicationDefinedRTCPPacket() failed to send RTCP packet");
3120 return -1;
3121 }
3122 return 0;
3123}
3124
3125int
3126Channel::GetRTPStatistics(
3127 unsigned int& averageJitterMs,
3128 unsigned int& maxJitterMs,
3129 unsigned int& discardedPackets)
3130{
niklase@google.com470e71d2011-07-07 08:21:25 +00003131 // The jitter statistics is updated for each received RTP packet and is
3132 // based on received packets.
sprang@webrtc.org54ae4ff2013-12-19 13:26:02 +00003133 if (_rtpRtcpModule->RTCP() == kRtcpOff) {
3134 // If RTCP is off, there is no timed thread in the RTCP module regularly
3135 // generating new stats, trigger the update manually here instead.
3136 StreamStatistician* statistician =
3137 rtp_receive_statistics_->GetStatistician(rtp_receiver_->SSRC());
3138 if (statistician) {
3139 // Don't use returned statistics, use data from proxy instead so that
3140 // max jitter can be fetched atomically.
3141 RtcpStatistics s;
3142 statistician->GetStatistics(&s, true);
3143 }
niklase@google.com470e71d2011-07-07 08:21:25 +00003144 }
3145
sprang@webrtc.org54ae4ff2013-12-19 13:26:02 +00003146 ChannelStatistics stats = statistics_proxy_->GetStats();
andrew@webrtc.orgeb524d92013-09-23 23:02:24 +00003147 const int32_t playoutFrequency = audio_coding_->PlayoutFrequency();
sprang@webrtc.org54ae4ff2013-12-19 13:26:02 +00003148 if (playoutFrequency > 0) {
3149 // Scale RTP statistics given the current playout frequency
3150 maxJitterMs = stats.max_jitter / (playoutFrequency / 1000);
3151 averageJitterMs = stats.rtcp.jitter / (playoutFrequency / 1000);
niklase@google.com470e71d2011-07-07 08:21:25 +00003152 }
3153
3154 discardedPackets = _numberOfDiscardedPackets;
3155
3156 WEBRTC_TRACE(kTraceStateInfo, kTraceVoice,
3157 VoEId(_instanceId, _channelId),
3158 "GetRTPStatistics() => averageJitterMs = %lu, maxJitterMs = %lu,"
wu@webrtc.orgfcd12b32011-09-15 20:49:50 +00003159 " discardedPackets = %lu)",
niklase@google.com470e71d2011-07-07 08:21:25 +00003160 averageJitterMs, maxJitterMs, discardedPackets);
3161 return 0;
3162}
3163
henrika@webrtc.org8a2fc882012-08-22 08:53:55 +00003164int Channel::GetRemoteRTCPReportBlocks(
3165 std::vector<ReportBlock>* report_blocks) {
3166 if (report_blocks == NULL) {
3167 _engineStatisticsPtr->SetLastError(VE_INVALID_ARGUMENT, kTraceError,
3168 "GetRemoteRTCPReportBlock()s invalid report_blocks.");
3169 return -1;
3170 }
3171
3172 // Get the report blocks from the latest received RTCP Sender or Receiver
3173 // Report. Each element in the vector contains the sender's SSRC and a
3174 // report block according to RFC 3550.
3175 std::vector<RTCPReportBlock> rtcp_report_blocks;
3176 if (_rtpRtcpModule->RemoteRTCPStat(&rtcp_report_blocks) != 0) {
3177 _engineStatisticsPtr->SetLastError(VE_RTP_RTCP_MODULE_ERROR, kTraceError,
3178 "GetRemoteRTCPReportBlocks() failed to read RTCP SR/RR report block.");
3179 return -1;
3180 }
3181
3182 if (rtcp_report_blocks.empty())
3183 return 0;
3184
3185 std::vector<RTCPReportBlock>::const_iterator it = rtcp_report_blocks.begin();
3186 for (; it != rtcp_report_blocks.end(); ++it) {
3187 ReportBlock report_block;
3188 report_block.sender_SSRC = it->remoteSSRC;
3189 report_block.source_SSRC = it->sourceSSRC;
3190 report_block.fraction_lost = it->fractionLost;
3191 report_block.cumulative_num_packets_lost = it->cumulativeLost;
3192 report_block.extended_highest_sequence_number = it->extendedHighSeqNum;
3193 report_block.interarrival_jitter = it->jitter;
3194 report_block.last_SR_timestamp = it->lastSR;
3195 report_block.delay_since_last_SR = it->delaySinceLastSR;
3196 report_blocks->push_back(report_block);
3197 }
3198 return 0;
3199}
3200
niklase@google.com470e71d2011-07-07 08:21:25 +00003201int
3202Channel::GetRTPStatistics(CallStatistics& stats)
3203{
wu@webrtc.orgcb711f72014-05-19 17:39:11 +00003204 // --- RtcpStatistics
niklase@google.com470e71d2011-07-07 08:21:25 +00003205
3206 // The jitter statistics is updated for each received RTP packet and is
3207 // based on received packets.
sprang@webrtc.org54ae4ff2013-12-19 13:26:02 +00003208 RtcpStatistics statistics;
stefan@webrtc.org286fe0b2013-08-21 20:58:21 +00003209 StreamStatistician* statistician =
3210 rtp_receive_statistics_->GetStatistician(rtp_receiver_->SSRC());
3211 if (!statistician || !statistician->GetStatistics(
wu@webrtc.org822fbd82013-08-15 23:38:54 +00003212 &statistics, _rtpRtcpModule->RTCP() == kRtcpOff)) {
3213 _engineStatisticsPtr->SetLastError(
3214 VE_CANNOT_RETRIEVE_RTP_STAT, kTraceWarning,
3215 "GetRTPStatistics() failed to read RTP statistics from the "
3216 "RTP/RTCP module");
niklase@google.com470e71d2011-07-07 08:21:25 +00003217 }
3218
wu@webrtc.org822fbd82013-08-15 23:38:54 +00003219 stats.fractionLost = statistics.fraction_lost;
3220 stats.cumulativeLost = statistics.cumulative_lost;
3221 stats.extendedMax = statistics.extended_max_sequence_number;
3222 stats.jitterSamples = statistics.jitter;
niklase@google.com470e71d2011-07-07 08:21:25 +00003223
3224 WEBRTC_TRACE(kTraceStateInfo, kTraceVoice,
3225 VoEId(_instanceId, _channelId),
3226 "GetRTPStatistics() => fractionLost=%lu, cumulativeLost=%lu,"
wu@webrtc.orgfcd12b32011-09-15 20:49:50 +00003227 " extendedMax=%lu, jitterSamples=%li)",
niklase@google.com470e71d2011-07-07 08:21:25 +00003228 stats.fractionLost, stats.cumulativeLost, stats.extendedMax,
3229 stats.jitterSamples);
3230
wu@webrtc.orgcb711f72014-05-19 17:39:11 +00003231 // --- RTT
minyue@webrtc.org2b58a442014-09-11 07:51:53 +00003232 stats.rttMs = GetRTT();
minyue@webrtc.org6fd93082014-12-15 14:56:44 +00003233 if (stats.rttMs == 0) {
3234 WEBRTC_TRACE(kTraceWarning, kTraceVoice, VoEId(_instanceId, _channelId),
3235 "GetRTPStatistics() failed to get RTT");
3236 } else {
3237 WEBRTC_TRACE(kTraceStateInfo, kTraceVoice, VoEId(_instanceId, _channelId),
3238 "GetRTPStatistics() => rttMs=%d", stats.rttMs);
3239 }
niklase@google.com470e71d2011-07-07 08:21:25 +00003240
wu@webrtc.orgcb711f72014-05-19 17:39:11 +00003241 // --- Data counters
niklase@google.com470e71d2011-07-07 08:21:25 +00003242
pkasting@chromium.org4591fbd2014-11-20 22:28:14 +00003243 size_t bytesSent(0);
pbos@webrtc.org6141e132013-04-09 10:09:10 +00003244 uint32_t packetsSent(0);
pkasting@chromium.org4591fbd2014-11-20 22:28:14 +00003245 size_t bytesReceived(0);
pbos@webrtc.org6141e132013-04-09 10:09:10 +00003246 uint32_t packetsReceived(0);
niklase@google.com470e71d2011-07-07 08:21:25 +00003247
stefan@webrtc.org286fe0b2013-08-21 20:58:21 +00003248 if (statistician) {
3249 statistician->GetDataCounters(&bytesReceived, &packetsReceived);
3250 }
wu@webrtc.org822fbd82013-08-15 23:38:54 +00003251
pwestin@webrtc.org2853dde2012-05-11 11:08:54 +00003252 if (_rtpRtcpModule->DataCountersRTP(&bytesSent,
wu@webrtc.org822fbd82013-08-15 23:38:54 +00003253 &packetsSent) != 0)
niklase@google.com470e71d2011-07-07 08:21:25 +00003254 {
3255 WEBRTC_TRACE(kTraceWarning, kTraceVoice,
3256 VoEId(_instanceId, _channelId),
3257 "GetRTPStatistics() failed to retrieve RTP datacounters =>"
wu@webrtc.orgfcd12b32011-09-15 20:49:50 +00003258 " output will not be complete");
niklase@google.com470e71d2011-07-07 08:21:25 +00003259 }
3260
3261 stats.bytesSent = bytesSent;
3262 stats.packetsSent = packetsSent;
3263 stats.bytesReceived = bytesReceived;
3264 stats.packetsReceived = packetsReceived;
3265
3266 WEBRTC_TRACE(kTraceStateInfo, kTraceVoice,
3267 VoEId(_instanceId, _channelId),
pkasting@chromium.org4591fbd2014-11-20 22:28:14 +00003268 "GetRTPStatistics() => bytesSent=%" PRIuS ", packetsSent=%d,"
3269 " bytesReceived=%" PRIuS ", packetsReceived=%d)",
niklase@google.com470e71d2011-07-07 08:21:25 +00003270 stats.bytesSent, stats.packetsSent, stats.bytesReceived,
3271 stats.packetsReceived);
3272
wu@webrtc.orgcb711f72014-05-19 17:39:11 +00003273 // --- Timestamps
3274 {
3275 CriticalSectionScoped lock(ts_stats_lock_.get());
3276 stats.capture_start_ntp_time_ms_ = capture_start_ntp_time_ms_;
3277 }
niklase@google.com470e71d2011-07-07 08:21:25 +00003278 return 0;
3279}
3280
minyue@webrtc.orgc1a40a72014-05-28 09:52:06 +00003281int Channel::SetREDStatus(bool enable, int redPayloadtype) {
turaj@webrtc.org42259e72012-12-11 02:15:12 +00003282 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId, _channelId),
minyue@webrtc.orgc1a40a72014-05-28 09:52:06 +00003283 "Channel::SetREDStatus()");
niklase@google.com470e71d2011-07-07 08:21:25 +00003284
turaj@webrtc.org8c8ad852013-01-31 18:20:17 +00003285 if (enable) {
3286 if (redPayloadtype < 0 || redPayloadtype > 127) {
3287 _engineStatisticsPtr->SetLastError(
3288 VE_PLTYPE_ERROR, kTraceError,
minyue@webrtc.orgc1a40a72014-05-28 09:52:06 +00003289 "SetREDStatus() invalid RED payload type");
turaj@webrtc.org8c8ad852013-01-31 18:20:17 +00003290 return -1;
3291 }
3292
3293 if (SetRedPayloadType(redPayloadtype) < 0) {
3294 _engineStatisticsPtr->SetLastError(
3295 VE_CODEC_ERROR, kTraceError,
3296 "SetSecondarySendCodec() Failed to register RED ACM");
3297 return -1;
3298 }
turaj@webrtc.org42259e72012-12-11 02:15:12 +00003299 }
niklase@google.com470e71d2011-07-07 08:21:25 +00003300
minyue@webrtc.orgaa5ea1c2014-05-23 15:16:51 +00003301 if (audio_coding_->SetREDStatus(enable) != 0) {
turaj@webrtc.org42259e72012-12-11 02:15:12 +00003302 _engineStatisticsPtr->SetLastError(
3303 VE_AUDIO_CODING_MODULE_ERROR, kTraceError,
minyue@webrtc.orgaa5ea1c2014-05-23 15:16:51 +00003304 "SetREDStatus() failed to set RED state in the ACM");
turaj@webrtc.org42259e72012-12-11 02:15:12 +00003305 return -1;
3306 }
3307 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +00003308}
3309
3310int
minyue@webrtc.orgc1a40a72014-05-28 09:52:06 +00003311Channel::GetREDStatus(bool& enabled, int& redPayloadtype)
niklase@google.com470e71d2011-07-07 08:21:25 +00003312{
minyue@webrtc.orgaa5ea1c2014-05-23 15:16:51 +00003313 enabled = audio_coding_->REDStatus();
niklase@google.com470e71d2011-07-07 08:21:25 +00003314 if (enabled)
3315 {
pbos@webrtc.org6141e132013-04-09 10:09:10 +00003316 int8_t payloadType(0);
pwestin@webrtc.org2853dde2012-05-11 11:08:54 +00003317 if (_rtpRtcpModule->SendREDPayloadType(payloadType) != 0)
niklase@google.com470e71d2011-07-07 08:21:25 +00003318 {
3319 _engineStatisticsPtr->SetLastError(
3320 VE_RTP_RTCP_MODULE_ERROR, kTraceError,
minyue@webrtc.orgc1a40a72014-05-28 09:52:06 +00003321 "GetREDStatus() failed to retrieve RED PT from RTP/RTCP "
niklase@google.com470e71d2011-07-07 08:21:25 +00003322 "module");
3323 return -1;
3324 }
3325 WEBRTC_TRACE(kTraceStateInfo, kTraceVoice,
3326 VoEId(_instanceId, _channelId),
minyue@webrtc.orgc1a40a72014-05-28 09:52:06 +00003327 "GetREDStatus() => enabled=%d, redPayloadtype=%d",
niklase@google.com470e71d2011-07-07 08:21:25 +00003328 enabled, redPayloadtype);
3329 return 0;
3330 }
3331 WEBRTC_TRACE(kTraceStateInfo, kTraceVoice,
3332 VoEId(_instanceId, _channelId),
minyue@webrtc.orgc1a40a72014-05-28 09:52:06 +00003333 "GetREDStatus() => enabled=%d", enabled);
niklase@google.com470e71d2011-07-07 08:21:25 +00003334 return 0;
3335}
3336
minyue@webrtc.orgc1a40a72014-05-28 09:52:06 +00003337int Channel::SetCodecFECStatus(bool enable) {
3338 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId, _channelId),
3339 "Channel::SetCodecFECStatus()");
3340
3341 if (audio_coding_->SetCodecFEC(enable) != 0) {
3342 _engineStatisticsPtr->SetLastError(
3343 VE_AUDIO_CODING_MODULE_ERROR, kTraceError,
3344 "SetCodecFECStatus() failed to set FEC state");
3345 return -1;
3346 }
3347 return 0;
3348}
3349
3350bool Channel::GetCodecFECStatus() {
3351 bool enabled = audio_coding_->CodecFEC();
3352 WEBRTC_TRACE(kTraceStateInfo, kTraceVoice,
3353 VoEId(_instanceId, _channelId),
3354 "GetCodecFECStatus() => enabled=%d", enabled);
3355 return enabled;
3356}
3357
pwestin@webrtc.orgdb249952013-06-05 15:33:20 +00003358void Channel::SetNACKStatus(bool enable, int maxNumberOfPackets) {
3359 // None of these functions can fail.
3360 _rtpRtcpModule->SetStorePacketsStatus(enable, maxNumberOfPackets);
stefan@webrtc.org7bb8f022013-09-06 13:40:11 +00003361 rtp_receive_statistics_->SetMaxReorderingThreshold(maxNumberOfPackets);
3362 rtp_receiver_->SetNACKStatus(enable ? kNackRtcp : kNackOff);
pwestin@webrtc.orgd30859e2013-06-06 21:09:01 +00003363 if (enable)
andrew@webrtc.orgeb524d92013-09-23 23:02:24 +00003364 audio_coding_->EnableNack(maxNumberOfPackets);
pwestin@webrtc.orgd30859e2013-06-06 21:09:01 +00003365 else
andrew@webrtc.orgeb524d92013-09-23 23:02:24 +00003366 audio_coding_->DisableNack();
pwestin@webrtc.orgdb249952013-06-05 15:33:20 +00003367}
3368
pwestin@webrtc.orgd30859e2013-06-06 21:09:01 +00003369// Called when we are missing one or more packets.
3370int Channel::ResendPackets(const uint16_t* sequence_numbers, int length) {
pwestin@webrtc.orgdb249952013-06-05 15:33:20 +00003371 return _rtpRtcpModule->SendNACK(sequence_numbers, length);
3372}
3373
niklase@google.com470e71d2011-07-07 08:21:25 +00003374int
niklase@google.com470e71d2011-07-07 08:21:25 +00003375Channel::StartRTPDump(const char fileNameUTF8[1024],
3376 RTPDirections direction)
3377{
3378 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId, _channelId),
3379 "Channel::StartRTPDump()");
3380 if ((direction != kRtpIncoming) && (direction != kRtpOutgoing))
3381 {
3382 _engineStatisticsPtr->SetLastError(
3383 VE_INVALID_ARGUMENT, kTraceError,
3384 "StartRTPDump() invalid RTP direction");
3385 return -1;
3386 }
3387 RtpDump* rtpDumpPtr = (direction == kRtpIncoming) ?
3388 &_rtpDumpIn : &_rtpDumpOut;
3389 if (rtpDumpPtr == NULL)
3390 {
3391 assert(false);
3392 return -1;
3393 }
3394 if (rtpDumpPtr->IsActive())
3395 {
3396 rtpDumpPtr->Stop();
3397 }
3398 if (rtpDumpPtr->Start(fileNameUTF8) != 0)
3399 {
3400 _engineStatisticsPtr->SetLastError(
3401 VE_BAD_FILE, kTraceError,
3402 "StartRTPDump() failed to create file");
3403 return -1;
3404 }
3405 return 0;
3406}
3407
3408int
3409Channel::StopRTPDump(RTPDirections direction)
3410{
3411 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId, _channelId),
3412 "Channel::StopRTPDump()");
3413 if ((direction != kRtpIncoming) && (direction != kRtpOutgoing))
3414 {
3415 _engineStatisticsPtr->SetLastError(
3416 VE_INVALID_ARGUMENT, kTraceError,
3417 "StopRTPDump() invalid RTP direction");
3418 return -1;
3419 }
3420 RtpDump* rtpDumpPtr = (direction == kRtpIncoming) ?
3421 &_rtpDumpIn : &_rtpDumpOut;
3422 if (rtpDumpPtr == NULL)
3423 {
3424 assert(false);
3425 return -1;
3426 }
3427 if (!rtpDumpPtr->IsActive())
3428 {
3429 return 0;
3430 }
3431 return rtpDumpPtr->Stop();
3432}
3433
3434bool
3435Channel::RTPDumpIsActive(RTPDirections direction)
3436{
3437 if ((direction != kRtpIncoming) &&
3438 (direction != kRtpOutgoing))
3439 {
3440 _engineStatisticsPtr->SetLastError(
3441 VE_INVALID_ARGUMENT, kTraceError,
3442 "RTPDumpIsActive() invalid RTP direction");
3443 return false;
3444 }
3445 RtpDump* rtpDumpPtr = (direction == kRtpIncoming) ?
3446 &_rtpDumpIn : &_rtpDumpOut;
3447 return rtpDumpPtr->IsActive();
3448}
3449
solenberg@webrtc.orgb1f50102014-03-24 10:38:25 +00003450void Channel::SetVideoEngineBWETarget(ViENetwork* vie_network,
3451 int video_channel) {
3452 CriticalSectionScoped cs(&_callbackCritSect);
3453 if (vie_network_) {
3454 vie_network_->Release();
3455 vie_network_ = NULL;
3456 }
3457 video_channel_ = -1;
3458
3459 if (vie_network != NULL && video_channel != -1) {
3460 vie_network_ = vie_network;
3461 video_channel_ = video_channel;
3462 }
3463}
3464
pbos@webrtc.org6141e132013-04-09 10:09:10 +00003465uint32_t
andrew@webrtc.org755b04a2011-11-15 16:57:56 +00003466Channel::Demultiplex(const AudioFrame& audioFrame)
niklase@google.com470e71d2011-07-07 08:21:25 +00003467{
3468 WEBRTC_TRACE(kTraceStream, kTraceVoice, VoEId(_instanceId,_channelId),
andrew@webrtc.org755b04a2011-11-15 16:57:56 +00003469 "Channel::Demultiplex()");
andrew@webrtc.orgae1a58b2013-01-22 04:44:30 +00003470 _audioFrame.CopyFrom(audioFrame);
andrew@webrtc.org63a50982012-05-02 23:56:37 +00003471 _audioFrame.id_ = _channelId;
niklase@google.com470e71d2011-07-07 08:21:25 +00003472 return 0;
3473}
3474
xians@webrtc.org2f84afa2013-07-31 16:23:37 +00003475void Channel::Demultiplex(const int16_t* audio_data,
xians@webrtc.org8fff1f02013-07-31 16:27:42 +00003476 int sample_rate,
xians@webrtc.org2f84afa2013-07-31 16:23:37 +00003477 int number_of_frames,
xians@webrtc.org8fff1f02013-07-31 16:27:42 +00003478 int number_of_channels) {
xians@webrtc.org2f84afa2013-07-31 16:23:37 +00003479 CodecInst codec;
3480 GetSendCodec(codec);
xians@webrtc.org2f84afa2013-07-31 16:23:37 +00003481
andrew@webrtc.org40ee3d02014-04-03 21:56:01 +00003482 if (!mono_recording_audio_.get()) {
3483 // Temporary space for DownConvertToCodecFormat.
3484 mono_recording_audio_.reset(new int16_t[kMaxMonoDataSizeSamples]);
xians@webrtc.org2f84afa2013-07-31 16:23:37 +00003485 }
andrew@webrtc.org40ee3d02014-04-03 21:56:01 +00003486 DownConvertToCodecFormat(audio_data,
3487 number_of_frames,
3488 number_of_channels,
3489 sample_rate,
3490 codec.channels,
3491 codec.plfreq,
3492 mono_recording_audio_.get(),
3493 &input_resampler_,
3494 &_audioFrame);
xians@webrtc.org2f84afa2013-07-31 16:23:37 +00003495}
3496
pbos@webrtc.org6141e132013-04-09 10:09:10 +00003497uint32_t
xians@google.com0b0665a2011-08-08 08:18:44 +00003498Channel::PrepareEncodeAndSend(int mixingFrequency)
niklase@google.com470e71d2011-07-07 08:21:25 +00003499{
3500 WEBRTC_TRACE(kTraceStream, kTraceVoice, VoEId(_instanceId,_channelId),
3501 "Channel::PrepareEncodeAndSend()");
3502
andrew@webrtc.org63a50982012-05-02 23:56:37 +00003503 if (_audioFrame.samples_per_channel_ == 0)
niklase@google.com470e71d2011-07-07 08:21:25 +00003504 {
3505 WEBRTC_TRACE(kTraceWarning, kTraceVoice, VoEId(_instanceId,_channelId),
3506 "Channel::PrepareEncodeAndSend() invalid audio frame");
tommi@webrtc.orgeec6ecd2014-07-11 19:09:59 +00003507 return 0xFFFFFFFF;
niklase@google.com470e71d2011-07-07 08:21:25 +00003508 }
3509
henrika@webrtc.org944cbeb2014-03-18 10:32:33 +00003510 if (channel_state_.Get().input_file_playing)
niklase@google.com470e71d2011-07-07 08:21:25 +00003511 {
3512 MixOrReplaceAudioWithFile(mixingFrequency);
3513 }
3514
andrew@webrtc.org21299d42014-05-14 19:00:59 +00003515 bool is_muted = Mute(); // Cache locally as Mute() takes a lock.
3516 if (is_muted) {
3517 AudioFrameOperations::Mute(_audioFrame);
niklase@google.com470e71d2011-07-07 08:21:25 +00003518 }
3519
henrika@webrtc.org944cbeb2014-03-18 10:32:33 +00003520 if (channel_state_.Get().input_external_media)
niklase@google.com470e71d2011-07-07 08:21:25 +00003521 {
mflodman@webrtc.org9a065d12012-03-07 08:12:21 +00003522 CriticalSectionScoped cs(&_callbackCritSect);
andrew@webrtc.org63a50982012-05-02 23:56:37 +00003523 const bool isStereo = (_audioFrame.num_channels_ == 2);
niklase@google.com470e71d2011-07-07 08:21:25 +00003524 if (_inputExternalMediaCallbackPtr)
3525 {
3526 _inputExternalMediaCallbackPtr->Process(
3527 _channelId,
3528 kRecordingPerChannel,
pbos@webrtc.org6141e132013-04-09 10:09:10 +00003529 (int16_t*)_audioFrame.data_,
andrew@webrtc.org63a50982012-05-02 23:56:37 +00003530 _audioFrame.samples_per_channel_,
3531 _audioFrame.sample_rate_hz_,
niklase@google.com470e71d2011-07-07 08:21:25 +00003532 isStereo);
3533 }
3534 }
3535
3536 InsertInbandDtmfTone();
3537
andrew@webrtc.org60730cf2014-01-07 17:45:09 +00003538 if (_includeAudioLevelIndication) {
andrew@webrtc.org382c0c22014-05-05 18:22:21 +00003539 int length = _audioFrame.samples_per_channel_ * _audioFrame.num_channels_;
andrew@webrtc.org21299d42014-05-14 19:00:59 +00003540 if (is_muted) {
3541 rms_level_.ProcessMuted(length);
3542 } else {
3543 rms_level_.Process(_audioFrame.data_, length);
3544 }
andrew@webrtc.org755b04a2011-11-15 16:57:56 +00003545 }
3546
niklase@google.com470e71d2011-07-07 08:21:25 +00003547 return 0;
3548}
3549
pbos@webrtc.org6141e132013-04-09 10:09:10 +00003550uint32_t
niklase@google.com470e71d2011-07-07 08:21:25 +00003551Channel::EncodeAndSend()
3552{
3553 WEBRTC_TRACE(kTraceStream, kTraceVoice, VoEId(_instanceId,_channelId),
3554 "Channel::EncodeAndSend()");
3555
andrew@webrtc.org63a50982012-05-02 23:56:37 +00003556 assert(_audioFrame.num_channels_ <= 2);
3557 if (_audioFrame.samples_per_channel_ == 0)
niklase@google.com470e71d2011-07-07 08:21:25 +00003558 {
3559 WEBRTC_TRACE(kTraceWarning, kTraceVoice, VoEId(_instanceId,_channelId),
3560 "Channel::EncodeAndSend() invalid audio frame");
tommi@webrtc.orgeec6ecd2014-07-11 19:09:59 +00003561 return 0xFFFFFFFF;
niklase@google.com470e71d2011-07-07 08:21:25 +00003562 }
3563
andrew@webrtc.org63a50982012-05-02 23:56:37 +00003564 _audioFrame.id_ = _channelId;
niklase@google.com470e71d2011-07-07 08:21:25 +00003565
3566 // --- Add 10ms of raw (PCM) audio data to the encoder @ 32kHz.
3567
3568 // The ACM resamples internally.
andrew@webrtc.org63a50982012-05-02 23:56:37 +00003569 _audioFrame.timestamp_ = _timeStamp;
andrew@webrtc.orgeb524d92013-09-23 23:02:24 +00003570 if (audio_coding_->Add10MsData((AudioFrame&)_audioFrame) != 0)
niklase@google.com470e71d2011-07-07 08:21:25 +00003571 {
3572 WEBRTC_TRACE(kTraceError, kTraceVoice, VoEId(_instanceId,_channelId),
3573 "Channel::EncodeAndSend() ACM encoding failed");
tommi@webrtc.orgeec6ecd2014-07-11 19:09:59 +00003574 return 0xFFFFFFFF;
niklase@google.com470e71d2011-07-07 08:21:25 +00003575 }
3576
andrew@webrtc.org63a50982012-05-02 23:56:37 +00003577 _timeStamp += _audioFrame.samples_per_channel_;
niklase@google.com470e71d2011-07-07 08:21:25 +00003578
3579 // --- Encode if complete frame is ready
3580
3581 // This call will trigger AudioPacketizationCallback::SendData if encoding
3582 // is done and payload is ready for packetization and transmission.
andrew@webrtc.orgeb524d92013-09-23 23:02:24 +00003583 return audio_coding_->Process();
niklase@google.com470e71d2011-07-07 08:21:25 +00003584}
3585
3586int Channel::RegisterExternalMediaProcessing(
3587 ProcessingTypes type,
3588 VoEMediaProcess& processObject)
3589{
3590 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId,_channelId),
3591 "Channel::RegisterExternalMediaProcessing()");
3592
mflodman@webrtc.org9a065d12012-03-07 08:12:21 +00003593 CriticalSectionScoped cs(&_callbackCritSect);
niklase@google.com470e71d2011-07-07 08:21:25 +00003594
3595 if (kPlaybackPerChannel == type)
3596 {
3597 if (_outputExternalMediaCallbackPtr)
3598 {
3599 _engineStatisticsPtr->SetLastError(
3600 VE_INVALID_OPERATION, kTraceError,
3601 "Channel::RegisterExternalMediaProcessing() "
3602 "output external media already enabled");
3603 return -1;
3604 }
3605 _outputExternalMediaCallbackPtr = &processObject;
3606 _outputExternalMedia = true;
3607 }
3608 else if (kRecordingPerChannel == type)
3609 {
3610 if (_inputExternalMediaCallbackPtr)
3611 {
3612 _engineStatisticsPtr->SetLastError(
3613 VE_INVALID_OPERATION, kTraceError,
3614 "Channel::RegisterExternalMediaProcessing() "
3615 "output external media already enabled");
3616 return -1;
3617 }
3618 _inputExternalMediaCallbackPtr = &processObject;
henrika@webrtc.org944cbeb2014-03-18 10:32:33 +00003619 channel_state_.SetInputExternalMedia(true);
niklase@google.com470e71d2011-07-07 08:21:25 +00003620 }
3621 return 0;
3622}
3623
3624int Channel::DeRegisterExternalMediaProcessing(ProcessingTypes type)
3625{
3626 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId,_channelId),
3627 "Channel::DeRegisterExternalMediaProcessing()");
3628
mflodman@webrtc.org9a065d12012-03-07 08:12:21 +00003629 CriticalSectionScoped cs(&_callbackCritSect);
niklase@google.com470e71d2011-07-07 08:21:25 +00003630
3631 if (kPlaybackPerChannel == type)
3632 {
3633 if (!_outputExternalMediaCallbackPtr)
3634 {
3635 _engineStatisticsPtr->SetLastError(
3636 VE_INVALID_OPERATION, kTraceWarning,
3637 "Channel::DeRegisterExternalMediaProcessing() "
3638 "output external media already disabled");
3639 return 0;
3640 }
3641 _outputExternalMedia = false;
3642 _outputExternalMediaCallbackPtr = NULL;
3643 }
3644 else if (kRecordingPerChannel == type)
3645 {
3646 if (!_inputExternalMediaCallbackPtr)
3647 {
3648 _engineStatisticsPtr->SetLastError(
3649 VE_INVALID_OPERATION, kTraceWarning,
3650 "Channel::DeRegisterExternalMediaProcessing() "
3651 "input external media already disabled");
3652 return 0;
3653 }
henrika@webrtc.org944cbeb2014-03-18 10:32:33 +00003654 channel_state_.SetInputExternalMedia(false);
niklase@google.com470e71d2011-07-07 08:21:25 +00003655 _inputExternalMediaCallbackPtr = NULL;
3656 }
3657
3658 return 0;
3659}
3660
roosa@google.com1b60ceb2012-12-12 23:00:29 +00003661int Channel::SetExternalMixing(bool enabled) {
3662 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId,_channelId),
3663 "Channel::SetExternalMixing(enabled=%d)", enabled);
3664
henrika@webrtc.org944cbeb2014-03-18 10:32:33 +00003665 if (channel_state_.Get().playing)
roosa@google.com1b60ceb2012-12-12 23:00:29 +00003666 {
3667 _engineStatisticsPtr->SetLastError(
3668 VE_INVALID_OPERATION, kTraceError,
3669 "Channel::SetExternalMixing() "
3670 "external mixing cannot be changed while playing.");
3671 return -1;
3672 }
3673
3674 _externalMixing = enabled;
3675
3676 return 0;
3677}
3678
niklase@google.com470e71d2011-07-07 08:21:25 +00003679int
niklase@google.com470e71d2011-07-07 08:21:25 +00003680Channel::GetNetworkStatistics(NetworkStatistics& stats)
3681{
3682 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId,_channelId),
3683 "Channel::GetNetworkStatistics()");
tina.legrand@webrtc.org7a7a0082013-02-21 10:27:48 +00003684 ACMNetworkStatistics acm_stats;
andrew@webrtc.orgeb524d92013-09-23 23:02:24 +00003685 int return_value = audio_coding_->NetworkStatistics(&acm_stats);
tina.legrand@webrtc.org7a7a0082013-02-21 10:27:48 +00003686 if (return_value >= 0) {
3687 memcpy(&stats, &acm_stats, sizeof(NetworkStatistics));
3688 }
3689 return return_value;
niklase@google.com470e71d2011-07-07 08:21:25 +00003690}
3691
wu@webrtc.org24301a62013-12-13 19:17:43 +00003692void Channel::GetDecodingCallStatistics(AudioDecodingCallStats* stats) const {
3693 audio_coding_->GetDecodingCallStatistics(stats);
3694}
3695
pwestin@webrtc.org1de01352013-04-11 20:23:35 +00003696bool Channel::GetDelayEstimate(int* jitter_buffer_delay_ms,
3697 int* playout_buffer_delay_ms) const {
3698 if (_average_jitter_buffer_delay_us == 0) {
niklase@google.com470e71d2011-07-07 08:21:25 +00003699 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId,_channelId),
pwestin@webrtc.org1de01352013-04-11 20:23:35 +00003700 "Channel::GetDelayEstimate() no valid estimate.");
3701 return false;
3702 }
3703 *jitter_buffer_delay_ms = (_average_jitter_buffer_delay_us + 500) / 1000 +
3704 _recPacketDelayMs;
3705 *playout_buffer_delay_ms = playout_delay_ms_;
3706 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId,_channelId),
3707 "Channel::GetDelayEstimate()");
3708 return true;
niklase@google.com470e71d2011-07-07 08:21:25 +00003709}
3710
turaj@webrtc.org6388c3e2013-02-12 21:42:18 +00003711int Channel::SetInitialPlayoutDelay(int delay_ms)
3712{
3713 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId,_channelId),
3714 "Channel::SetInitialPlayoutDelay()");
3715 if ((delay_ms < kVoiceEngineMinMinPlayoutDelayMs) ||
3716 (delay_ms > kVoiceEngineMaxMinPlayoutDelayMs))
3717 {
3718 _engineStatisticsPtr->SetLastError(
3719 VE_INVALID_ARGUMENT, kTraceError,
3720 "SetInitialPlayoutDelay() invalid min delay");
3721 return -1;
3722 }
andrew@webrtc.orgeb524d92013-09-23 23:02:24 +00003723 if (audio_coding_->SetInitialPlayoutDelay(delay_ms) != 0)
turaj@webrtc.org6388c3e2013-02-12 21:42:18 +00003724 {
3725 _engineStatisticsPtr->SetLastError(
3726 VE_AUDIO_CODING_MODULE_ERROR, kTraceError,
3727 "SetInitialPlayoutDelay() failed to set min playout delay");
3728 return -1;
3729 }
3730 return 0;
3731}
3732
3733
niklase@google.com470e71d2011-07-07 08:21:25 +00003734int
3735Channel::SetMinimumPlayoutDelay(int delayMs)
3736{
3737 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId,_channelId),
3738 "Channel::SetMinimumPlayoutDelay()");
3739 if ((delayMs < kVoiceEngineMinMinPlayoutDelayMs) ||
3740 (delayMs > kVoiceEngineMaxMinPlayoutDelayMs))
3741 {
3742 _engineStatisticsPtr->SetLastError(
3743 VE_INVALID_ARGUMENT, kTraceError,
3744 "SetMinimumPlayoutDelay() invalid min delay");
3745 return -1;
3746 }
andrew@webrtc.orgeb524d92013-09-23 23:02:24 +00003747 if (audio_coding_->SetMinimumPlayoutDelay(delayMs) != 0)
niklase@google.com470e71d2011-07-07 08:21:25 +00003748 {
3749 _engineStatisticsPtr->SetLastError(
3750 VE_AUDIO_CODING_MODULE_ERROR, kTraceError,
3751 "SetMinimumPlayoutDelay() failed to set min playout delay");
3752 return -1;
3753 }
3754 return 0;
3755}
3756
pwestin@webrtc.org1de01352013-04-11 20:23:35 +00003757void Channel::UpdatePlayoutTimestamp(bool rtcp) {
3758 uint32_t playout_timestamp = 0;
3759
andrew@webrtc.orgeb524d92013-09-23 23:02:24 +00003760 if (audio_coding_->PlayoutTimestamp(&playout_timestamp) == -1) {
turaj@webrtc.org1ebd2e92014-07-25 17:50:10 +00003761 // This can happen if this channel has not been received any RTP packet. In
3762 // this case, NetEq is not capable of computing playout timestamp.
pwestin@webrtc.org1de01352013-04-11 20:23:35 +00003763 return;
3764 }
3765
3766 uint16_t delay_ms = 0;
3767 if (_audioDeviceModulePtr->PlayoutDelay(&delay_ms) == -1) {
3768 WEBRTC_TRACE(kTraceWarning, kTraceVoice, VoEId(_instanceId,_channelId),
3769 "Channel::UpdatePlayoutTimestamp() failed to read playout"
3770 " delay from the ADM");
3771 _engineStatisticsPtr->SetLastError(
3772 VE_CANNOT_RETRIEVE_VALUE, kTraceError,
3773 "UpdatePlayoutTimestamp() failed to retrieve playout delay");
3774 return;
3775 }
3776
turaj@webrtc.org167b6df2013-12-13 21:05:07 +00003777 jitter_buffer_playout_timestamp_ = playout_timestamp;
3778
pwestin@webrtc.org1de01352013-04-11 20:23:35 +00003779 // Remove the playout delay.
wu@webrtc.org94454b72014-06-05 20:34:08 +00003780 playout_timestamp -= (delay_ms * (GetPlayoutFrequency() / 1000));
pwestin@webrtc.org1de01352013-04-11 20:23:35 +00003781
3782 WEBRTC_TRACE(kTraceStream, kTraceVoice, VoEId(_instanceId,_channelId),
3783 "Channel::UpdatePlayoutTimestamp() => playoutTimestamp = %lu",
3784 playout_timestamp);
3785
3786 if (rtcp) {
3787 playout_timestamp_rtcp_ = playout_timestamp;
3788 } else {
3789 playout_timestamp_rtp_ = playout_timestamp;
3790 }
3791 playout_delay_ms_ = delay_ms;
3792}
3793
3794int Channel::GetPlayoutTimestamp(unsigned int& timestamp) {
3795 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId,_channelId),
3796 "Channel::GetPlayoutTimestamp()");
3797 if (playout_timestamp_rtp_ == 0) {
3798 _engineStatisticsPtr->SetLastError(
3799 VE_CANNOT_RETRIEVE_VALUE, kTraceError,
3800 "GetPlayoutTimestamp() failed to retrieve timestamp");
3801 return -1;
3802 }
3803 timestamp = playout_timestamp_rtp_;
3804 WEBRTC_TRACE(kTraceStateInfo, kTraceVoice,
3805 VoEId(_instanceId,_channelId),
3806 "GetPlayoutTimestamp() => timestamp=%u", timestamp);
3807 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +00003808}
3809
3810int
3811Channel::SetInitTimestamp(unsigned int timestamp)
3812{
3813 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId,_channelId),
3814 "Channel::SetInitTimestamp()");
henrika@webrtc.org944cbeb2014-03-18 10:32:33 +00003815 if (channel_state_.Get().sending)
niklase@google.com470e71d2011-07-07 08:21:25 +00003816 {
3817 _engineStatisticsPtr->SetLastError(
3818 VE_SENDING, kTraceError, "SetInitTimestamp() already sending");
3819 return -1;
3820 }
pwestin@webrtc.org2853dde2012-05-11 11:08:54 +00003821 if (_rtpRtcpModule->SetStartTimestamp(timestamp) != 0)
niklase@google.com470e71d2011-07-07 08:21:25 +00003822 {
3823 _engineStatisticsPtr->SetLastError(
3824 VE_RTP_RTCP_MODULE_ERROR, kTraceError,
3825 "SetInitTimestamp() failed to set timestamp");
3826 return -1;
3827 }
3828 return 0;
3829}
3830
3831int
3832Channel::SetInitSequenceNumber(short sequenceNumber)
3833{
3834 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId,_channelId),
3835 "Channel::SetInitSequenceNumber()");
henrika@webrtc.org944cbeb2014-03-18 10:32:33 +00003836 if (channel_state_.Get().sending)
niklase@google.com470e71d2011-07-07 08:21:25 +00003837 {
3838 _engineStatisticsPtr->SetLastError(
3839 VE_SENDING, kTraceError,
3840 "SetInitSequenceNumber() already sending");
3841 return -1;
3842 }
pwestin@webrtc.org2853dde2012-05-11 11:08:54 +00003843 if (_rtpRtcpModule->SetSequenceNumber(sequenceNumber) != 0)
niklase@google.com470e71d2011-07-07 08:21:25 +00003844 {
3845 _engineStatisticsPtr->SetLastError(
3846 VE_RTP_RTCP_MODULE_ERROR, kTraceError,
3847 "SetInitSequenceNumber() failed to set sequence number");
3848 return -1;
3849 }
3850 return 0;
3851}
3852
3853int
wu@webrtc.org822fbd82013-08-15 23:38:54 +00003854Channel::GetRtpRtcp(RtpRtcp** rtpRtcpModule, RtpReceiver** rtp_receiver) const
niklase@google.com470e71d2011-07-07 08:21:25 +00003855{
3856 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId,_channelId),
3857 "Channel::GetRtpRtcp()");
wu@webrtc.org822fbd82013-08-15 23:38:54 +00003858 *rtpRtcpModule = _rtpRtcpModule.get();
3859 *rtp_receiver = rtp_receiver_.get();
niklase@google.com470e71d2011-07-07 08:21:25 +00003860 return 0;
3861}
3862
andrew@webrtc.orge59a0ac2012-05-08 17:12:40 +00003863// TODO(andrew): refactor Mix functions here and in transmit_mixer.cc to use
3864// a shared helper.
pbos@webrtc.org6141e132013-04-09 10:09:10 +00003865int32_t
pbos@webrtc.org92135212013-05-14 08:31:39 +00003866Channel::MixOrReplaceAudioWithFile(int mixingFrequency)
niklase@google.com470e71d2011-07-07 08:21:25 +00003867{
andrew@webrtc.org8f693302014-04-25 23:10:28 +00003868 scoped_ptr<int16_t[]> fileBuffer(new int16_t[640]);
andrew@webrtc.orge59a0ac2012-05-08 17:12:40 +00003869 int fileSamples(0);
niklase@google.com470e71d2011-07-07 08:21:25 +00003870
3871 {
mflodman@webrtc.org9a065d12012-03-07 08:12:21 +00003872 CriticalSectionScoped cs(&_fileCritSect);
niklase@google.com470e71d2011-07-07 08:21:25 +00003873
3874 if (_inputFilePlayerPtr == NULL)
3875 {
3876 WEBRTC_TRACE(kTraceWarning, kTraceVoice,
3877 VoEId(_instanceId, _channelId),
3878 "Channel::MixOrReplaceAudioWithFile() fileplayer"
3879 " doesnt exist");
3880 return -1;
3881 }
3882
braveyao@webrtc.orgd7131432012-03-29 10:39:44 +00003883 if (_inputFilePlayerPtr->Get10msAudioFromFile(fileBuffer.get(),
niklase@google.com470e71d2011-07-07 08:21:25 +00003884 fileSamples,
3885 mixingFrequency) == -1)
3886 {
3887 WEBRTC_TRACE(kTraceWarning, kTraceVoice,
3888 VoEId(_instanceId, _channelId),
3889 "Channel::MixOrReplaceAudioWithFile() file mixing "
3890 "failed");
3891 return -1;
3892 }
3893 if (fileSamples == 0)
3894 {
3895 WEBRTC_TRACE(kTraceWarning, kTraceVoice,
3896 VoEId(_instanceId, _channelId),
3897 "Channel::MixOrReplaceAudioWithFile() file is ended");
3898 return 0;
3899 }
3900 }
3901
andrew@webrtc.org63a50982012-05-02 23:56:37 +00003902 assert(_audioFrame.samples_per_channel_ == fileSamples);
niklase@google.com470e71d2011-07-07 08:21:25 +00003903
3904 if (_mixFileWithMicrophone)
3905 {
braveyao@webrtc.orgd7131432012-03-29 10:39:44 +00003906 // Currently file stream is always mono.
3907 // TODO(xians): Change the code when FilePlayer supports real stereo.
andrew@webrtc.org40ee3d02014-04-03 21:56:01 +00003908 MixWithSat(_audioFrame.data_,
3909 _audioFrame.num_channels_,
3910 fileBuffer.get(),
3911 1,
3912 fileSamples);
niklase@google.com470e71d2011-07-07 08:21:25 +00003913 }
3914 else
3915 {
braveyao@webrtc.orgd7131432012-03-29 10:39:44 +00003916 // Replace ACM audio with file.
3917 // Currently file stream is always mono.
3918 // TODO(xians): Change the code when FilePlayer supports real stereo.
niklase@google.com470e71d2011-07-07 08:21:25 +00003919 _audioFrame.UpdateFrame(_channelId,
tommi@webrtc.orgeec6ecd2014-07-11 19:09:59 +00003920 0xFFFFFFFF,
braveyao@webrtc.orgd7131432012-03-29 10:39:44 +00003921 fileBuffer.get(),
andrew@webrtc.orge59a0ac2012-05-08 17:12:40 +00003922 fileSamples,
niklase@google.com470e71d2011-07-07 08:21:25 +00003923 mixingFrequency,
3924 AudioFrame::kNormalSpeech,
3925 AudioFrame::kVadUnknown,
3926 1);
3927
3928 }
3929 return 0;
3930}
3931
pbos@webrtc.org6141e132013-04-09 10:09:10 +00003932int32_t
niklase@google.com470e71d2011-07-07 08:21:25 +00003933Channel::MixAudioWithFile(AudioFrame& audioFrame,
pbos@webrtc.org92135212013-05-14 08:31:39 +00003934 int mixingFrequency)
niklase@google.com470e71d2011-07-07 08:21:25 +00003935{
minyue@webrtc.org2a8df7c2014-08-06 10:05:19 +00003936 assert(mixingFrequency <= 48000);
niklase@google.com470e71d2011-07-07 08:21:25 +00003937
minyue@webrtc.org2a8df7c2014-08-06 10:05:19 +00003938 scoped_ptr<int16_t[]> fileBuffer(new int16_t[960]);
andrew@webrtc.orge59a0ac2012-05-08 17:12:40 +00003939 int fileSamples(0);
niklase@google.com470e71d2011-07-07 08:21:25 +00003940
3941 {
mflodman@webrtc.org9a065d12012-03-07 08:12:21 +00003942 CriticalSectionScoped cs(&_fileCritSect);
niklase@google.com470e71d2011-07-07 08:21:25 +00003943
3944 if (_outputFilePlayerPtr == NULL)
3945 {
3946 WEBRTC_TRACE(kTraceWarning, kTraceVoice,
3947 VoEId(_instanceId, _channelId),
3948 "Channel::MixAudioWithFile() file mixing failed");
3949 return -1;
3950 }
3951
3952 // We should get the frequency we ask for.
braveyao@webrtc.orgd7131432012-03-29 10:39:44 +00003953 if (_outputFilePlayerPtr->Get10msAudioFromFile(fileBuffer.get(),
niklase@google.com470e71d2011-07-07 08:21:25 +00003954 fileSamples,
3955 mixingFrequency) == -1)
3956 {
3957 WEBRTC_TRACE(kTraceWarning, kTraceVoice,
3958 VoEId(_instanceId, _channelId),
3959 "Channel::MixAudioWithFile() file mixing failed");
3960 return -1;
3961 }
3962 }
3963
andrew@webrtc.org63a50982012-05-02 23:56:37 +00003964 if (audioFrame.samples_per_channel_ == fileSamples)
niklase@google.com470e71d2011-07-07 08:21:25 +00003965 {
braveyao@webrtc.orgd7131432012-03-29 10:39:44 +00003966 // Currently file stream is always mono.
3967 // TODO(xians): Change the code when FilePlayer supports real stereo.
andrew@webrtc.org40ee3d02014-04-03 21:56:01 +00003968 MixWithSat(audioFrame.data_,
3969 audioFrame.num_channels_,
3970 fileBuffer.get(),
3971 1,
3972 fileSamples);
niklase@google.com470e71d2011-07-07 08:21:25 +00003973 }
3974 else
3975 {
3976 WEBRTC_TRACE(kTraceWarning, kTraceVoice, VoEId(_instanceId,_channelId),
andrew@webrtc.org63a50982012-05-02 23:56:37 +00003977 "Channel::MixAudioWithFile() samples_per_channel_(%d) != "
niklase@google.com470e71d2011-07-07 08:21:25 +00003978 "fileSamples(%d)",
andrew@webrtc.org63a50982012-05-02 23:56:37 +00003979 audioFrame.samples_per_channel_, fileSamples);
niklase@google.com470e71d2011-07-07 08:21:25 +00003980 return -1;
3981 }
3982
3983 return 0;
3984}
3985
3986int
3987Channel::InsertInbandDtmfTone()
3988{
niklas.enbom@webrtc.orgaf26f642011-11-16 12:41:36 +00003989 // Check if we should start a new tone.
niklase@google.com470e71d2011-07-07 08:21:25 +00003990 if (_inbandDtmfQueue.PendingDtmf() &&
3991 !_inbandDtmfGenerator.IsAddingTone() &&
3992 _inbandDtmfGenerator.DelaySinceLastTone() >
3993 kMinTelephoneEventSeparationMs)
3994 {
pbos@webrtc.org6141e132013-04-09 10:09:10 +00003995 int8_t eventCode(0);
3996 uint16_t lengthMs(0);
3997 uint8_t attenuationDb(0);
niklase@google.com470e71d2011-07-07 08:21:25 +00003998
3999 eventCode = _inbandDtmfQueue.NextDtmf(&lengthMs, &attenuationDb);
4000 _inbandDtmfGenerator.AddTone(eventCode, lengthMs, attenuationDb);
4001 if (_playInbandDtmfEvent)
4002 {
4003 // Add tone to output mixer using a reduced length to minimize
4004 // risk of echo.
4005 _outputMixerPtr->PlayDtmfTone(eventCode, lengthMs - 80,
4006 attenuationDb);
4007 }
4008 }
4009
4010 if (_inbandDtmfGenerator.IsAddingTone())
4011 {
pbos@webrtc.org6141e132013-04-09 10:09:10 +00004012 uint16_t frequency(0);
niklase@google.com470e71d2011-07-07 08:21:25 +00004013 _inbandDtmfGenerator.GetSampleRate(frequency);
4014
andrew@webrtc.org63a50982012-05-02 23:56:37 +00004015 if (frequency != _audioFrame.sample_rate_hz_)
niklase@google.com470e71d2011-07-07 08:21:25 +00004016 {
4017 // Update sample rate of Dtmf tone since the mixing frequency
4018 // has changed.
4019 _inbandDtmfGenerator.SetSampleRate(
pbos@webrtc.org6141e132013-04-09 10:09:10 +00004020 (uint16_t) (_audioFrame.sample_rate_hz_));
niklase@google.com470e71d2011-07-07 08:21:25 +00004021 // Reset the tone to be added taking the new sample rate into
4022 // account.
4023 _inbandDtmfGenerator.ResetTone();
4024 }
andrew@webrtc.orgae1a58b2013-01-22 04:44:30 +00004025
pbos@webrtc.org6141e132013-04-09 10:09:10 +00004026 int16_t toneBuffer[320];
4027 uint16_t toneSamples(0);
niklase@google.com470e71d2011-07-07 08:21:25 +00004028 // Get 10ms tone segment and set time since last tone to zero
4029 if (_inbandDtmfGenerator.Get10msTone(toneBuffer, toneSamples) == -1)
4030 {
4031 WEBRTC_TRACE(kTraceWarning, kTraceVoice,
4032 VoEId(_instanceId, _channelId),
4033 "Channel::EncodeAndSend() inserting Dtmf failed");
4034 return -1;
4035 }
4036
niklas.enbom@webrtc.orgaf26f642011-11-16 12:41:36 +00004037 // Replace mixed audio with DTMF tone.
andrew@webrtc.orgae1a58b2013-01-22 04:44:30 +00004038 for (int sample = 0;
andrew@webrtc.org63a50982012-05-02 23:56:37 +00004039 sample < _audioFrame.samples_per_channel_;
niklas.enbom@webrtc.orgaf26f642011-11-16 12:41:36 +00004040 sample++)
4041 {
andrew@webrtc.orgae1a58b2013-01-22 04:44:30 +00004042 for (int channel = 0;
4043 channel < _audioFrame.num_channels_;
niklas.enbom@webrtc.orgaf26f642011-11-16 12:41:36 +00004044 channel++)
4045 {
andrew@webrtc.orgae1a58b2013-01-22 04:44:30 +00004046 const int index = sample * _audioFrame.num_channels_ + channel;
4047 _audioFrame.data_[index] = toneBuffer[sample];
niklas.enbom@webrtc.orgaf26f642011-11-16 12:41:36 +00004048 }
4049 }
andrew@webrtc.orgae1a58b2013-01-22 04:44:30 +00004050
andrew@webrtc.org63a50982012-05-02 23:56:37 +00004051 assert(_audioFrame.samples_per_channel_ == toneSamples);
niklase@google.com470e71d2011-07-07 08:21:25 +00004052 } else
4053 {
4054 // Add 10ms to "delay-since-last-tone" counter
4055 _inbandDtmfGenerator.UpdateDelaySinceLastTone();
4056 }
4057 return 0;
4058}
4059
pbos@webrtc.org6141e132013-04-09 10:09:10 +00004060int32_t
pkasting@chromium.org4591fbd2014-11-20 22:28:14 +00004061Channel::SendPacketRaw(const void *data, size_t len, bool RTCP)
niklase@google.com470e71d2011-07-07 08:21:25 +00004062{
wu@webrtc.orgfb648da2013-10-18 21:10:51 +00004063 CriticalSectionScoped cs(&_callbackCritSect);
niklase@google.com470e71d2011-07-07 08:21:25 +00004064 if (_transportPtr == NULL)
4065 {
4066 return -1;
4067 }
4068 if (!RTCP)
4069 {
4070 return _transportPtr->SendPacket(_channelId, data, len);
4071 }
4072 else
4073 {
4074 return _transportPtr->SendRTCPPacket(_channelId, data, len);
4075 }
4076}
4077
pwestin@webrtc.org1de01352013-04-11 20:23:35 +00004078// Called for incoming RTP packets after successful RTP header parsing.
4079void Channel::UpdatePacketDelay(uint32_t rtp_timestamp,
4080 uint16_t sequence_number) {
4081 WEBRTC_TRACE(kTraceStream, kTraceVoice, VoEId(_instanceId,_channelId),
4082 "Channel::UpdatePacketDelay(timestamp=%lu, sequenceNumber=%u)",
4083 rtp_timestamp, sequence_number);
niklase@google.com470e71d2011-07-07 08:21:25 +00004084
pwestin@webrtc.org1de01352013-04-11 20:23:35 +00004085 // Get frequency of last received payload
wu@webrtc.org94454b72014-06-05 20:34:08 +00004086 int rtp_receive_frequency = GetPlayoutFrequency();
niklase@google.com470e71d2011-07-07 08:21:25 +00004087
turaj@webrtc.orge46c8d32013-05-22 20:39:43 +00004088 // Update the least required delay.
andrew@webrtc.orgeb524d92013-09-23 23:02:24 +00004089 least_required_delay_ms_ = audio_coding_->LeastRequiredDelayMs();
turaj@webrtc.orge46c8d32013-05-22 20:39:43 +00004090
turaj@webrtc.org167b6df2013-12-13 21:05:07 +00004091 // |jitter_buffer_playout_timestamp_| updated in UpdatePlayoutTimestamp for
4092 // every incoming packet.
4093 uint32_t timestamp_diff_ms = (rtp_timestamp -
4094 jitter_buffer_playout_timestamp_) / (rtp_receive_frequency / 1000);
henrik.lundin@webrtc.orgd6692992014-03-20 12:04:09 +00004095 if (!IsNewerTimestamp(rtp_timestamp, jitter_buffer_playout_timestamp_) ||
4096 timestamp_diff_ms > (2 * kVoiceEngineMaxMinPlayoutDelayMs)) {
4097 // If |jitter_buffer_playout_timestamp_| is newer than the incoming RTP
4098 // timestamp, the resulting difference is negative, but is set to zero.
4099 // This can happen when a network glitch causes a packet to arrive late,
4100 // and during long comfort noise periods with clock drift.
4101 timestamp_diff_ms = 0;
4102 }
niklase@google.com470e71d2011-07-07 08:21:25 +00004103
pwestin@webrtc.org1de01352013-04-11 20:23:35 +00004104 uint16_t packet_delay_ms = (rtp_timestamp - _previousTimestamp) /
4105 (rtp_receive_frequency / 1000);
niklase@google.com470e71d2011-07-07 08:21:25 +00004106
pwestin@webrtc.org1de01352013-04-11 20:23:35 +00004107 _previousTimestamp = rtp_timestamp;
niklase@google.com470e71d2011-07-07 08:21:25 +00004108
pwestin@webrtc.org1de01352013-04-11 20:23:35 +00004109 if (timestamp_diff_ms == 0) return;
niklase@google.com470e71d2011-07-07 08:21:25 +00004110
pwestin@webrtc.org1de01352013-04-11 20:23:35 +00004111 if (packet_delay_ms >= 10 && packet_delay_ms <= 60) {
4112 _recPacketDelayMs = packet_delay_ms;
4113 }
niklase@google.com470e71d2011-07-07 08:21:25 +00004114
pwestin@webrtc.org1de01352013-04-11 20:23:35 +00004115 if (_average_jitter_buffer_delay_us == 0) {
4116 _average_jitter_buffer_delay_us = timestamp_diff_ms * 1000;
4117 return;
4118 }
4119
4120 // Filter average delay value using exponential filter (alpha is
4121 // 7/8). We derive 1000 *_average_jitter_buffer_delay_us here (reduces
4122 // risk of rounding error) and compensate for it in GetDelayEstimate()
4123 // later.
4124 _average_jitter_buffer_delay_us = (_average_jitter_buffer_delay_us * 7 +
4125 1000 * timestamp_diff_ms + 500) / 8;
niklase@google.com470e71d2011-07-07 08:21:25 +00004126}
4127
4128void
4129Channel::RegisterReceiveCodecsToRTPModule()
4130{
4131 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId,_channelId),
4132 "Channel::RegisterReceiveCodecsToRTPModule()");
4133
4134
4135 CodecInst codec;
pbos@webrtc.org6141e132013-04-09 10:09:10 +00004136 const uint8_t nSupportedCodecs = AudioCodingModule::NumberOfCodecs();
niklase@google.com470e71d2011-07-07 08:21:25 +00004137
4138 for (int idx = 0; idx < nSupportedCodecs; idx++)
4139 {
4140 // Open up the RTP/RTCP receiver for all supported codecs
andrew@webrtc.orgeb524d92013-09-23 23:02:24 +00004141 if ((audio_coding_->Codec(idx, &codec) == -1) ||
wu@webrtc.org822fbd82013-08-15 23:38:54 +00004142 (rtp_receiver_->RegisterReceivePayload(
4143 codec.plname,
4144 codec.pltype,
4145 codec.plfreq,
4146 codec.channels,
4147 (codec.rate < 0) ? 0 : codec.rate) == -1))
niklase@google.com470e71d2011-07-07 08:21:25 +00004148 {
4149 WEBRTC_TRACE(
4150 kTraceWarning,
4151 kTraceVoice,
4152 VoEId(_instanceId, _channelId),
4153 "Channel::RegisterReceiveCodecsToRTPModule() unable"
4154 " to register %s (%d/%d/%d/%d) to RTP/RTCP receiver",
4155 codec.plname, codec.pltype, codec.plfreq,
4156 codec.channels, codec.rate);
4157 }
4158 else
4159 {
4160 WEBRTC_TRACE(
4161 kTraceInfo,
4162 kTraceVoice,
4163 VoEId(_instanceId, _channelId),
4164 "Channel::RegisterReceiveCodecsToRTPModule() %s "
wu@webrtc.orgfcd12b32011-09-15 20:49:50 +00004165 "(%d/%d/%d/%d) has been added to the RTP/RTCP "
niklase@google.com470e71d2011-07-07 08:21:25 +00004166 "receiver",
4167 codec.plname, codec.pltype, codec.plfreq,
4168 codec.channels, codec.rate);
4169 }
4170 }
4171}
4172
turaj@webrtc.org42259e72012-12-11 02:15:12 +00004173int Channel::SetSecondarySendCodec(const CodecInst& codec,
4174 int red_payload_type) {
turaj@webrtc.org8c8ad852013-01-31 18:20:17 +00004175 // Sanity check for payload type.
4176 if (red_payload_type < 0 || red_payload_type > 127) {
4177 _engineStatisticsPtr->SetLastError(
4178 VE_PLTYPE_ERROR, kTraceError,
4179 "SetRedPayloadType() invalid RED payload type");
4180 return -1;
4181 }
4182
turaj@webrtc.org42259e72012-12-11 02:15:12 +00004183 if (SetRedPayloadType(red_payload_type) < 0) {
4184 _engineStatisticsPtr->SetLastError(
4185 VE_AUDIO_CODING_MODULE_ERROR, kTraceError,
4186 "SetSecondarySendCodec() Failed to register RED ACM");
4187 return -1;
4188 }
andrew@webrtc.orgeb524d92013-09-23 23:02:24 +00004189 if (audio_coding_->RegisterSecondarySendCodec(codec) < 0) {
turaj@webrtc.org42259e72012-12-11 02:15:12 +00004190 _engineStatisticsPtr->SetLastError(
4191 VE_AUDIO_CODING_MODULE_ERROR, kTraceError,
4192 "SetSecondarySendCodec() Failed to register secondary send codec in "
4193 "ACM");
4194 return -1;
4195 }
4196
4197 return 0;
4198}
4199
4200void Channel::RemoveSecondarySendCodec() {
andrew@webrtc.orgeb524d92013-09-23 23:02:24 +00004201 audio_coding_->UnregisterSecondarySendCodec();
turaj@webrtc.org42259e72012-12-11 02:15:12 +00004202}
4203
4204int Channel::GetSecondarySendCodec(CodecInst* codec) {
andrew@webrtc.orgeb524d92013-09-23 23:02:24 +00004205 if (audio_coding_->SecondarySendCodec(codec) < 0) {
turaj@webrtc.org42259e72012-12-11 02:15:12 +00004206 _engineStatisticsPtr->SetLastError(
4207 VE_AUDIO_CODING_MODULE_ERROR, kTraceError,
4208 "GetSecondarySendCodec() Failed to get secondary sent codec from ACM");
4209 return -1;
4210 }
4211 return 0;
4212}
4213
turaj@webrtc.org8c8ad852013-01-31 18:20:17 +00004214// Assuming this method is called with valid payload type.
turaj@webrtc.org42259e72012-12-11 02:15:12 +00004215int Channel::SetRedPayloadType(int red_payload_type) {
turaj@webrtc.org42259e72012-12-11 02:15:12 +00004216 CodecInst codec;
4217 bool found_red = false;
4218
4219 // Get default RED settings from the ACM database
4220 const int num_codecs = AudioCodingModule::NumberOfCodecs();
4221 for (int idx = 0; idx < num_codecs; idx++) {
andrew@webrtc.orgeb524d92013-09-23 23:02:24 +00004222 audio_coding_->Codec(idx, &codec);
turaj@webrtc.org42259e72012-12-11 02:15:12 +00004223 if (!STR_CASE_CMP(codec.plname, "RED")) {
4224 found_red = true;
4225 break;
4226 }
4227 }
4228
4229 if (!found_red) {
4230 _engineStatisticsPtr->SetLastError(
4231 VE_CODEC_ERROR, kTraceError,
4232 "SetRedPayloadType() RED is not supported");
4233 return -1;
4234 }
4235
turaj@webrtc.org9d532fd2013-01-31 18:34:19 +00004236 codec.pltype = red_payload_type;
andrew@webrtc.orgeb524d92013-09-23 23:02:24 +00004237 if (audio_coding_->RegisterSendCodec(codec) < 0) {
turaj@webrtc.org42259e72012-12-11 02:15:12 +00004238 _engineStatisticsPtr->SetLastError(
4239 VE_AUDIO_CODING_MODULE_ERROR, kTraceError,
4240 "SetRedPayloadType() RED registration in ACM module failed");
4241 return -1;
4242 }
4243
4244 if (_rtpRtcpModule->SetSendREDPayloadType(red_payload_type) != 0) {
4245 _engineStatisticsPtr->SetLastError(
4246 VE_RTP_RTCP_MODULE_ERROR, kTraceError,
4247 "SetRedPayloadType() RED registration in RTP/RTCP module failed");
4248 return -1;
4249 }
4250 return 0;
4251}
4252
wu@webrtc.orgebdb0e32014-03-06 23:49:08 +00004253int Channel::SetSendRtpHeaderExtension(bool enable, RTPExtensionType type,
4254 unsigned char id) {
4255 int error = 0;
4256 _rtpRtcpModule->DeregisterSendRtpHeaderExtension(type);
4257 if (enable) {
4258 error = _rtpRtcpModule->RegisterSendRtpHeaderExtension(type, id);
4259 }
4260 return error;
4261}
minyue@webrtc.orgc1a40a72014-05-28 09:52:06 +00004262
wu@webrtc.org94454b72014-06-05 20:34:08 +00004263int32_t Channel::GetPlayoutFrequency() {
4264 int32_t playout_frequency = audio_coding_->PlayoutFrequency();
4265 CodecInst current_recive_codec;
4266 if (audio_coding_->ReceiveCodec(&current_recive_codec) == 0) {
4267 if (STR_CASE_CMP("G722", current_recive_codec.plname) == 0) {
4268 // Even though the actual sampling rate for G.722 audio is
4269 // 16,000 Hz, the RTP clock rate for the G722 payload format is
4270 // 8,000 Hz because that value was erroneously assigned in
4271 // RFC 1890 and must remain unchanged for backward compatibility.
4272 playout_frequency = 8000;
4273 } else if (STR_CASE_CMP("opus", current_recive_codec.plname) == 0) {
4274 // We are resampling Opus internally to 32,000 Hz until all our
4275 // DSP routines can operate at 48,000 Hz, but the RTP clock
4276 // rate for the Opus payload format is standardized to 48,000 Hz,
4277 // because that is the maximum supported decoding sampling rate.
4278 playout_frequency = 48000;
4279 }
4280 }
4281 return playout_frequency;
4282}
4283
minyue@webrtc.org2b58a442014-09-11 07:51:53 +00004284int Channel::GetRTT() const {
4285 RTCPMethod method = _rtpRtcpModule->RTCP();
4286 if (method == kRtcpOff) {
minyue@webrtc.org2b58a442014-09-11 07:51:53 +00004287 return 0;
4288 }
4289 std::vector<RTCPReportBlock> report_blocks;
4290 _rtpRtcpModule->RemoteRTCPStat(&report_blocks);
4291 if (report_blocks.empty()) {
minyue@webrtc.org2b58a442014-09-11 07:51:53 +00004292 return 0;
4293 }
4294
4295 uint32_t remoteSSRC = rtp_receiver_->SSRC();
4296 std::vector<RTCPReportBlock>::const_iterator it = report_blocks.begin();
4297 for (; it != report_blocks.end(); ++it) {
4298 if (it->remoteSSRC == remoteSSRC)
4299 break;
4300 }
4301 if (it == report_blocks.end()) {
4302 // We have not received packets with SSRC matching the report blocks.
4303 // To calculate RTT we try with the SSRC of the first report block.
4304 // This is very important for send-only channels where we don't know
4305 // the SSRC of the other end.
4306 remoteSSRC = report_blocks[0].remoteSSRC;
4307 }
4308 uint16_t rtt = 0;
4309 uint16_t avg_rtt = 0;
4310 uint16_t max_rtt= 0;
4311 uint16_t min_rtt = 0;
4312 if (_rtpRtcpModule->RTT(remoteSSRC, &rtt, &avg_rtt, &min_rtt, &max_rtt)
4313 != 0) {
minyue@webrtc.org2b58a442014-09-11 07:51:53 +00004314 return 0;
4315 }
4316 return static_cast<int>(rtt);
4317}
4318
pbos@webrtc.orgd900e8b2013-07-03 15:12:26 +00004319} // namespace voe
4320} // namespace webrtc