blob: 745ac9e90ef9df13c2d50b7117851340c5010135 [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,
pkasting@chromium.org16825b12015-01-12 21:51:21 +0000104 const int64_t rtt) OVERRIDE {
minyue@webrtc.orgc1a40a72014-05-28 09:52:06 +0000105 // |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
pkasting@chromium.org16825b12015-01-12 21:51:21 +0000413 int64_t round_trip_time = 0;
wu@webrtc.org822fbd82013-08-15 23:38:54 +0000414 _rtpRtcpModule->RTT(rtp_receiver_->SSRC(), &round_trip_time,
415 NULL, NULL, NULL);
pwestin@webrtc.orgd30859e2013-06-06 21:09:01 +0000416
andrew@webrtc.orgeb524d92013-09-23 23:02:24 +0000417 std::vector<uint16_t> nack_list = audio_coding_->GetNackList(
wu@webrtc.org822fbd82013-08-15 23:38:54 +0000418 round_trip_time);
419 if (!nack_list.empty()) {
420 // Can't use nack_list.data() since it's not supported by all
421 // compilers.
422 ResendPackets(&(nack_list[0]), static_cast<int>(nack_list.size()));
pwestin@webrtc.orgd30859e2013-06-06 21:09:01 +0000423 }
niklase@google.com470e71d2011-07-07 08:21:25 +0000424 return 0;
425}
426
stefan@webrtc.org7bb8f022013-09-06 13:40:11 +0000427bool Channel::OnRecoveredPacket(const uint8_t* rtp_packet,
pkasting@chromium.org4591fbd2014-11-20 22:28:14 +0000428 size_t rtp_packet_length) {
stefan@webrtc.org7bb8f022013-09-06 13:40:11 +0000429 RTPHeader header;
430 if (!rtp_header_parser_->Parse(rtp_packet, rtp_packet_length, &header)) {
431 WEBRTC_TRACE(kTraceDebug, webrtc::kTraceVoice, _channelId,
432 "IncomingPacket invalid RTP header");
433 return false;
434 }
435 header.payload_type_frequency =
436 rtp_payload_registry_->GetPayloadTypeFrequency(header.payloadType);
437 if (header.payload_type_frequency < 0)
438 return false;
439 return ReceivePacket(rtp_packet, rtp_packet_length, header, false);
440}
441
pbos@webrtc.org92135212013-05-14 08:31:39 +0000442int32_t Channel::GetAudioFrame(int32_t id, AudioFrame& audioFrame)
niklase@google.com470e71d2011-07-07 08:21:25 +0000443{
444 WEBRTC_TRACE(kTraceStream, kTraceVoice, VoEId(_instanceId,_channelId),
445 "Channel::GetAudioFrame(id=%d)", id);
446
447 // Get 10ms raw PCM data from the ACM (mixer limits output frequency)
andrew@webrtc.orgeb524d92013-09-23 23:02:24 +0000448 if (audio_coding_->PlayoutData10Ms(audioFrame.sample_rate_hz_,
449 &audioFrame) == -1)
niklase@google.com470e71d2011-07-07 08:21:25 +0000450 {
451 WEBRTC_TRACE(kTraceError, kTraceVoice,
452 VoEId(_instanceId,_channelId),
453 "Channel::GetAudioFrame() PlayoutData10Ms() failed!");
andrew@webrtc.org7859e102012-01-13 00:30:11 +0000454 // In all likelihood, the audio in this frame is garbage. We return an
455 // error so that the audio mixer module doesn't add it to the mix. As
456 // a result, it won't be played out and the actions skipped here are
457 // irrelevant.
458 return -1;
niklase@google.com470e71d2011-07-07 08:21:25 +0000459 }
460
461 if (_RxVadDetection)
462 {
463 UpdateRxVadDetection(audioFrame);
464 }
465
466 // Convert module ID to internal VoE channel ID
andrew@webrtc.org63a50982012-05-02 23:56:37 +0000467 audioFrame.id_ = VoEChannelId(audioFrame.id_);
niklase@google.com470e71d2011-07-07 08:21:25 +0000468 // Store speech type for dead-or-alive detection
andrew@webrtc.org63a50982012-05-02 23:56:37 +0000469 _outputSpeechType = audioFrame.speech_type_;
niklase@google.com470e71d2011-07-07 08:21:25 +0000470
henrika@webrtc.org944cbeb2014-03-18 10:32:33 +0000471 ChannelState::State state = channel_state_.Get();
472
473 if (state.rx_apm_is_enabled) {
andrew@webrtc.org60730cf2014-01-07 17:45:09 +0000474 int err = rx_audioproc_->ProcessStream(&audioFrame);
475 if (err) {
476 LOG(LS_ERROR) << "ProcessStream() error: " << err;
477 assert(false);
478 }
niklase@google.com470e71d2011-07-07 08:21:25 +0000479 }
480
wu@webrtc.org63420662013-10-17 18:28:55 +0000481 float output_gain = 1.0f;
482 float left_pan = 1.0f;
483 float right_pan = 1.0f;
niklase@google.com470e71d2011-07-07 08:21:25 +0000484 {
wu@webrtc.org63420662013-10-17 18:28:55 +0000485 CriticalSectionScoped cs(&volume_settings_critsect_);
486 output_gain = _outputGain;
487 left_pan = _panLeft;
488 right_pan= _panRight;
489 }
490
491 // Output volume scaling
492 if (output_gain < 0.99f || output_gain > 1.01f)
493 {
494 AudioFrameOperations::ScaleWithSat(output_gain, audioFrame);
niklase@google.com470e71d2011-07-07 08:21:25 +0000495 }
496
497 // Scale left and/or right channel(s) if stereo and master balance is
498 // active
499
wu@webrtc.org63420662013-10-17 18:28:55 +0000500 if (left_pan != 1.0f || right_pan != 1.0f)
niklase@google.com470e71d2011-07-07 08:21:25 +0000501 {
andrew@webrtc.org63a50982012-05-02 23:56:37 +0000502 if (audioFrame.num_channels_ == 1)
niklase@google.com470e71d2011-07-07 08:21:25 +0000503 {
504 // Emulate stereo mode since panning is active.
505 // The mono signal is copied to both left and right channels here.
andrew@webrtc.org4ecea3e2012-06-27 03:25:31 +0000506 AudioFrameOperations::MonoToStereo(&audioFrame);
niklase@google.com470e71d2011-07-07 08:21:25 +0000507 }
508 // For true stereo mode (when we are receiving a stereo signal), no
509 // action is needed.
510
511 // Do the panning operation (the audio frame contains stereo at this
512 // stage)
wu@webrtc.org63420662013-10-17 18:28:55 +0000513 AudioFrameOperations::Scale(left_pan, right_pan, audioFrame);
niklase@google.com470e71d2011-07-07 08:21:25 +0000514 }
515
516 // Mix decoded PCM output with file if file mixing is enabled
henrika@webrtc.org944cbeb2014-03-18 10:32:33 +0000517 if (state.output_file_playing)
niklase@google.com470e71d2011-07-07 08:21:25 +0000518 {
andrew@webrtc.org63a50982012-05-02 23:56:37 +0000519 MixAudioWithFile(audioFrame, audioFrame.sample_rate_hz_);
niklase@google.com470e71d2011-07-07 08:21:25 +0000520 }
521
niklase@google.com470e71d2011-07-07 08:21:25 +0000522 // External media
523 if (_outputExternalMedia)
524 {
mflodman@webrtc.org9a065d12012-03-07 08:12:21 +0000525 CriticalSectionScoped cs(&_callbackCritSect);
andrew@webrtc.org63a50982012-05-02 23:56:37 +0000526 const bool isStereo = (audioFrame.num_channels_ == 2);
niklase@google.com470e71d2011-07-07 08:21:25 +0000527 if (_outputExternalMediaCallbackPtr)
528 {
529 _outputExternalMediaCallbackPtr->Process(
530 _channelId,
531 kPlaybackPerChannel,
pbos@webrtc.org6141e132013-04-09 10:09:10 +0000532 (int16_t*)audioFrame.data_,
andrew@webrtc.org63a50982012-05-02 23:56:37 +0000533 audioFrame.samples_per_channel_,
534 audioFrame.sample_rate_hz_,
niklase@google.com470e71d2011-07-07 08:21:25 +0000535 isStereo);
536 }
537 }
538
539 // Record playout if enabled
540 {
mflodman@webrtc.org9a065d12012-03-07 08:12:21 +0000541 CriticalSectionScoped cs(&_fileCritSect);
niklase@google.com470e71d2011-07-07 08:21:25 +0000542
543 if (_outputFileRecording && _outputFileRecorderPtr)
544 {
niklas.enbom@webrtc.org5398d952012-03-26 08:11:25 +0000545 _outputFileRecorderPtr->RecordAudioToFile(audioFrame);
niklase@google.com470e71d2011-07-07 08:21:25 +0000546 }
547 }
548
549 // Measure audio level (0-9)
550 _outputAudioLevel.ComputeLevel(audioFrame);
551
wu@webrtc.org94454b72014-06-05 20:34:08 +0000552 if (capture_start_rtp_time_stamp_ < 0 && audioFrame.timestamp_ != 0) {
553 // The first frame with a valid rtp timestamp.
wu@webrtc.orgcb711f72014-05-19 17:39:11 +0000554 capture_start_rtp_time_stamp_ = audioFrame.timestamp_;
wu@webrtc.org94454b72014-06-05 20:34:08 +0000555 }
556
557 if (capture_start_rtp_time_stamp_ >= 0) {
558 // audioFrame.timestamp_ should be valid from now on.
559
560 // Compute elapsed time.
561 int64_t unwrap_timestamp =
562 rtp_ts_wraparound_handler_->Unwrap(audioFrame.timestamp_);
563 audioFrame.elapsed_time_ms_ =
564 (unwrap_timestamp - capture_start_rtp_time_stamp_) /
565 (GetPlayoutFrequency() / 1000);
566
stefan@webrtc.org8e24d872014-09-02 18:58:24 +0000567 {
wu@webrtc.orgcb711f72014-05-19 17:39:11 +0000568 CriticalSectionScoped lock(ts_stats_lock_.get());
stefan@webrtc.org8e24d872014-09-02 18:58:24 +0000569 // Compute ntp time.
570 audioFrame.ntp_time_ms_ = ntp_estimator_.Estimate(
571 audioFrame.timestamp_);
572 // |ntp_time_ms_| won't be valid until at least 2 RTCP SRs are received.
573 if (audioFrame.ntp_time_ms_ > 0) {
574 // Compute |capture_start_ntp_time_ms_| so that
575 // |capture_start_ntp_time_ms_| + |elapsed_time_ms_| == |ntp_time_ms_|
576 capture_start_ntp_time_ms_ =
577 audioFrame.ntp_time_ms_ - audioFrame.elapsed_time_ms_;
578 }
wu@webrtc.orgcb711f72014-05-19 17:39:11 +0000579 }
580 }
581
niklase@google.com470e71d2011-07-07 08:21:25 +0000582 return 0;
583}
584
pbos@webrtc.org6141e132013-04-09 10:09:10 +0000585int32_t
pbos@webrtc.org92135212013-05-14 08:31:39 +0000586Channel::NeededFrequency(int32_t id)
niklase@google.com470e71d2011-07-07 08:21:25 +0000587{
588 WEBRTC_TRACE(kTraceStream, kTraceVoice, VoEId(_instanceId,_channelId),
589 "Channel::NeededFrequency(id=%d)", id);
590
591 int highestNeeded = 0;
592
593 // Determine highest needed receive frequency
andrew@webrtc.orgeb524d92013-09-23 23:02:24 +0000594 int32_t receiveFrequency = audio_coding_->ReceiveFrequency();
niklase@google.com470e71d2011-07-07 08:21:25 +0000595
596 // Return the bigger of playout and receive frequency in the ACM.
andrew@webrtc.orgeb524d92013-09-23 23:02:24 +0000597 if (audio_coding_->PlayoutFrequency() > receiveFrequency)
niklase@google.com470e71d2011-07-07 08:21:25 +0000598 {
andrew@webrtc.orgeb524d92013-09-23 23:02:24 +0000599 highestNeeded = audio_coding_->PlayoutFrequency();
niklase@google.com470e71d2011-07-07 08:21:25 +0000600 }
601 else
602 {
603 highestNeeded = receiveFrequency;
604 }
605
606 // Special case, if we're playing a file on the playout side
607 // we take that frequency into consideration as well
608 // This is not needed on sending side, since the codec will
609 // limit the spectrum anyway.
henrika@webrtc.org944cbeb2014-03-18 10:32:33 +0000610 if (channel_state_.Get().output_file_playing)
niklase@google.com470e71d2011-07-07 08:21:25 +0000611 {
mflodman@webrtc.org9a065d12012-03-07 08:12:21 +0000612 CriticalSectionScoped cs(&_fileCritSect);
henrika@webrtc.org944cbeb2014-03-18 10:32:33 +0000613 if (_outputFilePlayerPtr)
niklase@google.com470e71d2011-07-07 08:21:25 +0000614 {
615 if(_outputFilePlayerPtr->Frequency()>highestNeeded)
616 {
617 highestNeeded=_outputFilePlayerPtr->Frequency();
618 }
619 }
620 }
621
622 return(highestNeeded);
623}
624
pbos@webrtc.org6141e132013-04-09 10:09:10 +0000625int32_t
niklase@google.com470e71d2011-07-07 08:21:25 +0000626Channel::CreateChannel(Channel*& channel,
pbos@webrtc.org92135212013-05-14 08:31:39 +0000627 int32_t channelId,
minyue@webrtc.orge509f942013-09-12 17:03:00 +0000628 uint32_t instanceId,
629 const Config& config)
niklase@google.com470e71d2011-07-07 08:21:25 +0000630{
631 WEBRTC_TRACE(kTraceMemory, kTraceVoice, VoEId(instanceId,channelId),
632 "Channel::CreateChannel(channelId=%d, instanceId=%d)",
633 channelId, instanceId);
634
minyue@webrtc.orge509f942013-09-12 17:03:00 +0000635 channel = new Channel(channelId, instanceId, config);
niklase@google.com470e71d2011-07-07 08:21:25 +0000636 if (channel == NULL)
637 {
638 WEBRTC_TRACE(kTraceMemory, kTraceVoice,
639 VoEId(instanceId,channelId),
640 "Channel::CreateChannel() unable to allocate memory for"
641 " channel");
642 return -1;
643 }
644 return 0;
645}
646
647void
pbos@webrtc.org92135212013-05-14 08:31:39 +0000648Channel::PlayNotification(int32_t id, uint32_t durationMs)
niklase@google.com470e71d2011-07-07 08:21:25 +0000649{
650 WEBRTC_TRACE(kTraceStream, kTraceVoice, VoEId(_instanceId,_channelId),
651 "Channel::PlayNotification(id=%d, durationMs=%d)",
652 id, durationMs);
653
654 // Not implement yet
655}
656
657void
pbos@webrtc.org92135212013-05-14 08:31:39 +0000658Channel::RecordNotification(int32_t id, uint32_t durationMs)
niklase@google.com470e71d2011-07-07 08:21:25 +0000659{
660 WEBRTC_TRACE(kTraceStream, kTraceVoice, VoEId(_instanceId,_channelId),
661 "Channel::RecordNotification(id=%d, durationMs=%d)",
662 id, durationMs);
663
664 // Not implement yet
665}
666
667void
pbos@webrtc.org92135212013-05-14 08:31:39 +0000668Channel::PlayFileEnded(int32_t id)
niklase@google.com470e71d2011-07-07 08:21:25 +0000669{
670 WEBRTC_TRACE(kTraceStream, kTraceVoice, VoEId(_instanceId,_channelId),
671 "Channel::PlayFileEnded(id=%d)", id);
672
673 if (id == _inputFilePlayerId)
674 {
henrika@webrtc.org944cbeb2014-03-18 10:32:33 +0000675 channel_state_.SetInputFilePlaying(false);
niklase@google.com470e71d2011-07-07 08:21:25 +0000676 WEBRTC_TRACE(kTraceStateInfo, kTraceVoice,
677 VoEId(_instanceId,_channelId),
678 "Channel::PlayFileEnded() => input file player module is"
679 " shutdown");
680 }
681 else if (id == _outputFilePlayerId)
682 {
henrika@webrtc.org944cbeb2014-03-18 10:32:33 +0000683 channel_state_.SetOutputFilePlaying(false);
niklase@google.com470e71d2011-07-07 08:21:25 +0000684 WEBRTC_TRACE(kTraceStateInfo, kTraceVoice,
685 VoEId(_instanceId,_channelId),
686 "Channel::PlayFileEnded() => output file player module is"
687 " shutdown");
688 }
689}
690
691void
pbos@webrtc.org92135212013-05-14 08:31:39 +0000692Channel::RecordFileEnded(int32_t id)
niklase@google.com470e71d2011-07-07 08:21:25 +0000693{
694 WEBRTC_TRACE(kTraceStream, kTraceVoice, VoEId(_instanceId,_channelId),
695 "Channel::RecordFileEnded(id=%d)", id);
696
697 assert(id == _outputFileRecorderId);
698
mflodman@webrtc.org9a065d12012-03-07 08:12:21 +0000699 CriticalSectionScoped cs(&_fileCritSect);
niklase@google.com470e71d2011-07-07 08:21:25 +0000700
701 _outputFileRecording = false;
702 WEBRTC_TRACE(kTraceStateInfo, kTraceVoice,
703 VoEId(_instanceId,_channelId),
704 "Channel::RecordFileEnded() => output file recorder module is"
705 " shutdown");
706}
707
pbos@webrtc.org92135212013-05-14 08:31:39 +0000708Channel::Channel(int32_t channelId,
minyue@webrtc.orge509f942013-09-12 17:03:00 +0000709 uint32_t instanceId,
710 const Config& config) :
niklase@google.com470e71d2011-07-07 08:21:25 +0000711 _fileCritSect(*CriticalSectionWrapper::CreateCriticalSection()),
712 _callbackCritSect(*CriticalSectionWrapper::CreateCriticalSection()),
wu@webrtc.org63420662013-10-17 18:28:55 +0000713 volume_settings_critsect_(*CriticalSectionWrapper::CreateCriticalSection()),
niklase@google.com470e71d2011-07-07 08:21:25 +0000714 _instanceId(instanceId),
xians@google.com22963ab2011-08-03 12:40:23 +0000715 _channelId(channelId),
stefan@webrtc.orga5cb98c2013-05-29 12:12:51 +0000716 rtp_header_parser_(RtpHeaderParser::Create()),
wu@webrtc.org822fbd82013-08-15 23:38:54 +0000717 rtp_payload_registry_(
andresp@webrtc.orgdc80bae2014-04-08 11:06:12 +0000718 new RTPPayloadRegistry(RTPPayloadStrategy::CreateStrategy(true))),
wu@webrtc.org822fbd82013-08-15 23:38:54 +0000719 rtp_receive_statistics_(ReceiveStatistics::Create(
720 Clock::GetRealTimeClock())),
721 rtp_receiver_(RtpReceiver::CreateAudioReceiver(
722 VoEModuleId(instanceId, channelId), Clock::GetRealTimeClock(), this,
723 this, this, rtp_payload_registry_.get())),
724 telephone_event_handler_(rtp_receiver_->GetTelephoneEventHandler()),
henrik.lundin@webrtc.org34fe0152014-04-22 19:04:34 +0000725 audio_coding_(AudioCodingModule::Create(
xians@google.com22963ab2011-08-03 12:40:23 +0000726 VoEModuleId(instanceId, channelId))),
niklase@google.com470e71d2011-07-07 08:21:25 +0000727 _rtpDumpIn(*RtpDump::CreateRtpDump()),
728 _rtpDumpOut(*RtpDump::CreateRtpDump()),
niklase@google.com470e71d2011-07-07 08:21:25 +0000729 _outputAudioLevel(),
niklase@google.com470e71d2011-07-07 08:21:25 +0000730 _externalTransport(false),
niklase@google.com470e71d2011-07-07 08:21:25 +0000731 _inputFilePlayerPtr(NULL),
732 _outputFilePlayerPtr(NULL),
733 _outputFileRecorderPtr(NULL),
734 // Avoid conflict with other channels by adding 1024 - 1026,
735 // won't use as much as 1024 channels.
736 _inputFilePlayerId(VoEModuleId(instanceId, channelId) + 1024),
737 _outputFilePlayerId(VoEModuleId(instanceId, channelId) + 1025),
738 _outputFileRecorderId(VoEModuleId(instanceId, channelId) + 1026),
niklase@google.com470e71d2011-07-07 08:21:25 +0000739 _outputFileRecording(false),
xians@google.com22963ab2011-08-03 12:40:23 +0000740 _inbandDtmfQueue(VoEModuleId(instanceId, channelId)),
741 _inbandDtmfGenerator(VoEModuleId(instanceId, channelId)),
xians@google.com22963ab2011-08-03 12:40:23 +0000742 _outputExternalMedia(false),
niklase@google.com470e71d2011-07-07 08:21:25 +0000743 _inputExternalMediaCallbackPtr(NULL),
744 _outputExternalMediaCallbackPtr(NULL),
xians@google.com22963ab2011-08-03 12:40:23 +0000745 _timeStamp(0), // This is just an offset, RTP module will add it's own random offset
746 _sendTelephoneEventPayloadType(106),
stefan@webrtc.org8e24d872014-09-02 18:58:24 +0000747 ntp_estimator_(Clock::GetRealTimeClock()),
turaj@webrtc.org167b6df2013-12-13 21:05:07 +0000748 jitter_buffer_playout_timestamp_(0),
pwestin@webrtc.org1de01352013-04-11 20:23:35 +0000749 playout_timestamp_rtp_(0),
750 playout_timestamp_rtcp_(0),
sprang@webrtc.org54ae4ff2013-12-19 13:26:02 +0000751 playout_delay_ms_(0),
xians@google.com22963ab2011-08-03 12:40:23 +0000752 _numberOfDiscardedPackets(0),
xians@webrtc.org09e8c472013-07-31 16:30:19 +0000753 send_sequence_number_(0),
wu@webrtc.orgcb711f72014-05-19 17:39:11 +0000754 ts_stats_lock_(CriticalSectionWrapper::CreateCriticalSection()),
wu@webrtc.org94454b72014-06-05 20:34:08 +0000755 rtp_ts_wraparound_handler_(new rtc::TimestampWrapAroundHandler()),
756 capture_start_rtp_time_stamp_(-1),
wu@webrtc.orgcb711f72014-05-19 17:39:11 +0000757 capture_start_ntp_time_ms_(-1),
xians@google.com22963ab2011-08-03 12:40:23 +0000758 _engineStatisticsPtr(NULL),
henrika@webrtc.org2919e952012-01-31 08:45:03 +0000759 _outputMixerPtr(NULL),
760 _transmitMixerPtr(NULL),
xians@google.com22963ab2011-08-03 12:40:23 +0000761 _moduleProcessThreadPtr(NULL),
762 _audioDeviceModulePtr(NULL),
763 _voiceEngineObserverPtr(NULL),
764 _callbackCritSectPtr(NULL),
765 _transportPtr(NULL),
xians@google.com22963ab2011-08-03 12:40:23 +0000766 _rxVadObserverPtr(NULL),
767 _oldVadDecision(-1),
768 _sendFrameType(0),
roosa@google.com1b60ceb2012-12-12 23:00:29 +0000769 _externalMixing(false),
xians@google.com22963ab2011-08-03 12:40:23 +0000770 _mixFileWithMicrophone(false),
niklase@google.com470e71d2011-07-07 08:21:25 +0000771 _mute(false),
772 _panLeft(1.0f),
773 _panRight(1.0f),
774 _outputGain(1.0f),
775 _playOutbandDtmfEvent(false),
776 _playInbandDtmfEvent(false),
niklase@google.com470e71d2011-07-07 08:21:25 +0000777 _lastLocalTimeStamp(0),
778 _lastPayloadType(0),
xians@google.com22963ab2011-08-03 12:40:23 +0000779 _includeAudioLevelIndication(false),
niklase@google.com470e71d2011-07-07 08:21:25 +0000780 _outputSpeechType(AudioFrame::kNormalSpeech),
solenberg@webrtc.orgb1f50102014-03-24 10:38:25 +0000781 vie_network_(NULL),
782 video_channel_(-1),
pwestin@webrtc.org1de01352013-04-11 20:23:35 +0000783 _average_jitter_buffer_delay_us(0),
turaj@webrtc.orge46c8d32013-05-22 20:39:43 +0000784 least_required_delay_ms_(0),
niklase@google.com470e71d2011-07-07 08:21:25 +0000785 _previousTimestamp(0),
786 _recPacketDelayMs(20),
787 _RxVadDetection(false),
niklase@google.com470e71d2011-07-07 08:21:25 +0000788 _rxAgcIsEnabled(false),
stefan@webrtc.org7bb8f022013-09-06 13:40:11 +0000789 _rxNsIsEnabled(false),
minyue@webrtc.orgc1a40a72014-05-28 09:52:06 +0000790 restored_packet_in_use_(false),
791 bitrate_controller_(
792 BitrateController::CreateBitrateController(Clock::GetRealTimeClock(),
793 true)),
794 rtcp_bandwidth_observer_(
795 bitrate_controller_->CreateRtcpBandwidthObserver()),
minyue@webrtc.org74aaf292014-07-16 21:28:26 +0000796 send_bitrate_observer_(new VoEBitrateObserver(this)),
797 network_predictor_(new NetworkPredictor(Clock::GetRealTimeClock()))
niklase@google.com470e71d2011-07-07 08:21:25 +0000798{
799 WEBRTC_TRACE(kTraceMemory, kTraceVoice, VoEId(_instanceId,_channelId),
800 "Channel::Channel() - ctor");
801 _inbandDtmfQueue.ResetDtmf();
802 _inbandDtmfGenerator.Init();
803 _outputAudioLevel.Clear();
804
pwestin@webrtc.org2853dde2012-05-11 11:08:54 +0000805 RtpRtcp::Configuration configuration;
806 configuration.id = VoEModuleId(instanceId, channelId);
807 configuration.audio = true;
pwestin@webrtc.org2853dde2012-05-11 11:08:54 +0000808 configuration.outgoing_transport = this;
pwestin@webrtc.org2853dde2012-05-11 11:08:54 +0000809 configuration.audio_messages = this;
wu@webrtc.org822fbd82013-08-15 23:38:54 +0000810 configuration.receive_statistics = rtp_receive_statistics_.get();
minyue@webrtc.orgc1a40a72014-05-28 09:52:06 +0000811 configuration.bandwidth_callback = rtcp_bandwidth_observer_.get();
pwestin@webrtc.org2853dde2012-05-11 11:08:54 +0000812
813 _rtpRtcpModule.reset(RtpRtcp::CreateRtpRtcp(configuration));
sprang@webrtc.org54ae4ff2013-12-19 13:26:02 +0000814
815 statistics_proxy_.reset(new StatisticsProxy(_rtpRtcpModule->SSRC()));
816 rtp_receive_statistics_->RegisterRtcpStatisticsCallback(
817 statistics_proxy_.get());
aluebs@webrtc.orgf927fd62014-04-16 11:58:18 +0000818
819 Config audioproc_config;
820 audioproc_config.Set<ExperimentalAgc>(new ExperimentalAgc(false));
821 rx_audioproc_.reset(AudioProcessing::Create(audioproc_config));
niklase@google.com470e71d2011-07-07 08:21:25 +0000822}
823
824Channel::~Channel()
825{
sprang@webrtc.org54ae4ff2013-12-19 13:26:02 +0000826 rtp_receive_statistics_->RegisterRtcpStatisticsCallback(NULL);
niklase@google.com470e71d2011-07-07 08:21:25 +0000827 WEBRTC_TRACE(kTraceMemory, kTraceVoice, VoEId(_instanceId,_channelId),
828 "Channel::~Channel() - dtor");
829
830 if (_outputExternalMedia)
831 {
832 DeRegisterExternalMediaProcessing(kPlaybackPerChannel);
833 }
henrika@webrtc.org944cbeb2014-03-18 10:32:33 +0000834 if (channel_state_.Get().input_external_media)
niklase@google.com470e71d2011-07-07 08:21:25 +0000835 {
836 DeRegisterExternalMediaProcessing(kRecordingPerChannel);
837 }
838 StopSend();
niklase@google.com470e71d2011-07-07 08:21:25 +0000839 StopPlayout();
840
841 {
mflodman@webrtc.org9a065d12012-03-07 08:12:21 +0000842 CriticalSectionScoped cs(&_fileCritSect);
niklase@google.com470e71d2011-07-07 08:21:25 +0000843 if (_inputFilePlayerPtr)
844 {
845 _inputFilePlayerPtr->RegisterModuleFileCallback(NULL);
846 _inputFilePlayerPtr->StopPlayingFile();
847 FilePlayer::DestroyFilePlayer(_inputFilePlayerPtr);
848 _inputFilePlayerPtr = NULL;
849 }
850 if (_outputFilePlayerPtr)
851 {
852 _outputFilePlayerPtr->RegisterModuleFileCallback(NULL);
853 _outputFilePlayerPtr->StopPlayingFile();
854 FilePlayer::DestroyFilePlayer(_outputFilePlayerPtr);
855 _outputFilePlayerPtr = NULL;
856 }
857 if (_outputFileRecorderPtr)
858 {
859 _outputFileRecorderPtr->RegisterModuleFileCallback(NULL);
860 _outputFileRecorderPtr->StopRecording();
861 FileRecorder::DestroyFileRecorder(_outputFileRecorderPtr);
862 _outputFileRecorderPtr = NULL;
863 }
864 }
865
866 // The order to safely shutdown modules in a channel is:
867 // 1. De-register callbacks in modules
868 // 2. De-register modules in process thread
869 // 3. Destroy modules
andrew@webrtc.orgeb524d92013-09-23 23:02:24 +0000870 if (audio_coding_->RegisterTransportCallback(NULL) == -1)
niklase@google.com470e71d2011-07-07 08:21:25 +0000871 {
872 WEBRTC_TRACE(kTraceWarning, kTraceVoice,
873 VoEId(_instanceId,_channelId),
874 "~Channel() failed to de-register transport callback"
875 " (Audio coding module)");
876 }
andrew@webrtc.orgeb524d92013-09-23 23:02:24 +0000877 if (audio_coding_->RegisterVADCallback(NULL) == -1)
niklase@google.com470e71d2011-07-07 08:21:25 +0000878 {
879 WEBRTC_TRACE(kTraceWarning, kTraceVoice,
880 VoEId(_instanceId,_channelId),
881 "~Channel() failed to de-register VAD callback"
882 " (Audio coding module)");
883 }
niklase@google.com470e71d2011-07-07 08:21:25 +0000884 // De-register modules in process thread
pwestin@webrtc.org2853dde2012-05-11 11:08:54 +0000885 if (_moduleProcessThreadPtr->DeRegisterModule(_rtpRtcpModule.get()) == -1)
niklase@google.com470e71d2011-07-07 08:21:25 +0000886 {
887 WEBRTC_TRACE(kTraceInfo, kTraceVoice,
888 VoEId(_instanceId,_channelId),
889 "~Channel() failed to deregister RTP/RTCP module");
890 }
niklase@google.com470e71d2011-07-07 08:21:25 +0000891 // End of modules shutdown
892
893 // Delete other objects
solenberg@webrtc.orgb1f50102014-03-24 10:38:25 +0000894 if (vie_network_) {
895 vie_network_->Release();
896 vie_network_ = NULL;
897 }
niklase@google.com470e71d2011-07-07 08:21:25 +0000898 RtpDump::DestroyRtpDump(&_rtpDumpIn);
899 RtpDump::DestroyRtpDump(&_rtpDumpOut);
niklase@google.com470e71d2011-07-07 08:21:25 +0000900 delete &_callbackCritSect;
niklase@google.com470e71d2011-07-07 08:21:25 +0000901 delete &_fileCritSect;
wu@webrtc.org63420662013-10-17 18:28:55 +0000902 delete &volume_settings_critsect_;
niklase@google.com470e71d2011-07-07 08:21:25 +0000903}
904
pbos@webrtc.org6141e132013-04-09 10:09:10 +0000905int32_t
niklase@google.com470e71d2011-07-07 08:21:25 +0000906Channel::Init()
907{
908 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId,_channelId),
909 "Channel::Init()");
910
henrika@webrtc.org944cbeb2014-03-18 10:32:33 +0000911 channel_state_.Reset();
912
niklase@google.com470e71d2011-07-07 08:21:25 +0000913 // --- Initial sanity
914
915 if ((_engineStatisticsPtr == NULL) ||
916 (_moduleProcessThreadPtr == NULL))
917 {
918 WEBRTC_TRACE(kTraceError, kTraceVoice,
919 VoEId(_instanceId,_channelId),
920 "Channel::Init() must call SetEngineInformation() first");
921 return -1;
922 }
923
924 // --- Add modules to process thread (for periodic schedulation)
925
926 const bool processThreadFail =
pwestin@webrtc.org2853dde2012-05-11 11:08:54 +0000927 ((_moduleProcessThreadPtr->RegisterModule(_rtpRtcpModule.get()) != 0) ||
niklase@google.com470e71d2011-07-07 08:21:25 +0000928 false);
niklase@google.com470e71d2011-07-07 08:21:25 +0000929 if (processThreadFail)
930 {
931 _engineStatisticsPtr->SetLastError(
932 VE_CANNOT_INIT_CHANNEL, kTraceError,
933 "Channel::Init() modules not registered");
934 return -1;
935 }
pwestin@webrtc.orgc450a192012-01-04 15:00:12 +0000936 // --- ACM initialization
niklase@google.com470e71d2011-07-07 08:21:25 +0000937
andrew@webrtc.orgeb524d92013-09-23 23:02:24 +0000938 if ((audio_coding_->InitializeReceiver() == -1) ||
niklase@google.com470e71d2011-07-07 08:21:25 +0000939#ifdef WEBRTC_CODEC_AVT
940 // out-of-band Dtmf tones are played out by default
andrew@webrtc.orgeb524d92013-09-23 23:02:24 +0000941 (audio_coding_->SetDtmfPlayoutStatus(true) == -1) ||
niklase@google.com470e71d2011-07-07 08:21:25 +0000942#endif
andrew@webrtc.orgeb524d92013-09-23 23:02:24 +0000943 (audio_coding_->InitializeSender() == -1))
niklase@google.com470e71d2011-07-07 08:21:25 +0000944 {
945 _engineStatisticsPtr->SetLastError(
946 VE_AUDIO_CODING_MODULE_ERROR, kTraceError,
947 "Channel::Init() unable to initialize the ACM - 1");
948 return -1;
949 }
950
951 // --- RTP/RTCP module initialization
952
953 // Ensure that RTCP is enabled by default for the created channel.
954 // Note that, the module will keep generating RTCP until it is explicitly
955 // disabled by the user.
956 // After StopListen (when no sockets exists), RTCP packets will no longer
957 // be transmitted since the Transport object will then be invalid.
wu@webrtc.org822fbd82013-08-15 23:38:54 +0000958 telephone_event_handler_->SetTelephoneEventForwardToDecoder(true);
959 // RTCP is enabled by default.
pbos@webrtc.orgd16e8392014-12-19 13:49:55 +0000960 _rtpRtcpModule->SetRTCPStatus(kRtcpCompound);
961 // --- Register all permanent callbacks
niklase@google.com470e71d2011-07-07 08:21:25 +0000962 const bool fail =
andrew@webrtc.orgeb524d92013-09-23 23:02:24 +0000963 (audio_coding_->RegisterTransportCallback(this) == -1) ||
964 (audio_coding_->RegisterVADCallback(this) == -1);
niklase@google.com470e71d2011-07-07 08:21:25 +0000965
966 if (fail)
967 {
968 _engineStatisticsPtr->SetLastError(
969 VE_CANNOT_INIT_CHANNEL, kTraceError,
970 "Channel::Init() callbacks not registered");
971 return -1;
972 }
973
974 // --- Register all supported codecs to the receiving side of the
975 // RTP/RTCP module
976
977 CodecInst codec;
pbos@webrtc.org6141e132013-04-09 10:09:10 +0000978 const uint8_t nSupportedCodecs = AudioCodingModule::NumberOfCodecs();
niklase@google.com470e71d2011-07-07 08:21:25 +0000979
980 for (int idx = 0; idx < nSupportedCodecs; idx++)
981 {
982 // Open up the RTP/RTCP receiver for all supported codecs
andrew@webrtc.orgeb524d92013-09-23 23:02:24 +0000983 if ((audio_coding_->Codec(idx, &codec) == -1) ||
wu@webrtc.org822fbd82013-08-15 23:38:54 +0000984 (rtp_receiver_->RegisterReceivePayload(
985 codec.plname,
986 codec.pltype,
987 codec.plfreq,
988 codec.channels,
989 (codec.rate < 0) ? 0 : codec.rate) == -1))
niklase@google.com470e71d2011-07-07 08:21:25 +0000990 {
991 WEBRTC_TRACE(kTraceWarning, kTraceVoice,
992 VoEId(_instanceId,_channelId),
993 "Channel::Init() unable to register %s (%d/%d/%d/%d) "
994 "to RTP/RTCP receiver",
995 codec.plname, codec.pltype, codec.plfreq,
996 codec.channels, codec.rate);
997 }
998 else
999 {
1000 WEBRTC_TRACE(kTraceInfo, kTraceVoice,
1001 VoEId(_instanceId,_channelId),
1002 "Channel::Init() %s (%d/%d/%d/%d) has been added to "
1003 "the RTP/RTCP receiver",
1004 codec.plname, codec.pltype, codec.plfreq,
1005 codec.channels, codec.rate);
1006 }
1007
1008 // Ensure that PCMU is used as default codec on the sending side
tina.legrand@webrtc.org45175852012-06-01 09:27:35 +00001009 if (!STR_CASE_CMP(codec.plname, "PCMU") && (codec.channels == 1))
niklase@google.com470e71d2011-07-07 08:21:25 +00001010 {
1011 SetSendCodec(codec);
1012 }
1013
1014 // Register default PT for outband 'telephone-event'
1015 if (!STR_CASE_CMP(codec.plname, "telephone-event"))
1016 {
pwestin@webrtc.org2853dde2012-05-11 11:08:54 +00001017 if ((_rtpRtcpModule->RegisterSendPayload(codec) == -1) ||
andrew@webrtc.orgeb524d92013-09-23 23:02:24 +00001018 (audio_coding_->RegisterReceiveCodec(codec) == -1))
niklase@google.com470e71d2011-07-07 08:21:25 +00001019 {
1020 WEBRTC_TRACE(kTraceWarning, kTraceVoice,
1021 VoEId(_instanceId,_channelId),
1022 "Channel::Init() failed to register outband "
1023 "'telephone-event' (%d/%d) correctly",
1024 codec.pltype, codec.plfreq);
1025 }
1026 }
1027
1028 if (!STR_CASE_CMP(codec.plname, "CN"))
1029 {
andrew@webrtc.orgeb524d92013-09-23 23:02:24 +00001030 if ((audio_coding_->RegisterSendCodec(codec) == -1) ||
1031 (audio_coding_->RegisterReceiveCodec(codec) == -1) ||
pwestin@webrtc.org2853dde2012-05-11 11:08:54 +00001032 (_rtpRtcpModule->RegisterSendPayload(codec) == -1))
niklase@google.com470e71d2011-07-07 08:21:25 +00001033 {
1034 WEBRTC_TRACE(kTraceWarning, kTraceVoice,
1035 VoEId(_instanceId,_channelId),
1036 "Channel::Init() failed to register CN (%d/%d) "
1037 "correctly - 1",
1038 codec.pltype, codec.plfreq);
1039 }
1040 }
1041#ifdef WEBRTC_CODEC_RED
1042 // Register RED to the receiving side of the ACM.
1043 // We will not receive an OnInitializeDecoder() callback for RED.
1044 if (!STR_CASE_CMP(codec.plname, "RED"))
1045 {
andrew@webrtc.orgeb524d92013-09-23 23:02:24 +00001046 if (audio_coding_->RegisterReceiveCodec(codec) == -1)
niklase@google.com470e71d2011-07-07 08:21:25 +00001047 {
1048 WEBRTC_TRACE(kTraceWarning, kTraceVoice,
1049 VoEId(_instanceId,_channelId),
1050 "Channel::Init() failed to register RED (%d/%d) "
1051 "correctly",
1052 codec.pltype, codec.plfreq);
1053 }
1054 }
1055#endif
1056 }
pwestin@webrtc.org684f0572013-03-13 23:20:57 +00001057
andrew@webrtc.org6c264cc2013-10-04 17:54:09 +00001058 if (rx_audioproc_->noise_suppression()->set_level(kDefaultNsMode) != 0) {
1059 LOG_FERR1(LS_ERROR, noise_suppression()->set_level, kDefaultNsMode);
1060 return -1;
niklase@google.com470e71d2011-07-07 08:21:25 +00001061 }
andrew@webrtc.org6c264cc2013-10-04 17:54:09 +00001062 if (rx_audioproc_->gain_control()->set_mode(kDefaultRxAgcMode) != 0) {
1063 LOG_FERR1(LS_ERROR, gain_control()->set_mode, kDefaultRxAgcMode);
1064 return -1;
niklase@google.com470e71d2011-07-07 08:21:25 +00001065 }
1066
1067 return 0;
1068}
1069
pbos@webrtc.org6141e132013-04-09 10:09:10 +00001070int32_t
niklase@google.com470e71d2011-07-07 08:21:25 +00001071Channel::SetEngineInformation(Statistics& engineStatistics,
1072 OutputMixer& outputMixer,
1073 voe::TransmitMixer& transmitMixer,
1074 ProcessThread& moduleProcessThread,
1075 AudioDeviceModule& audioDeviceModule,
1076 VoiceEngineObserver* voiceEngineObserver,
1077 CriticalSectionWrapper* callbackCritSect)
1078{
1079 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId,_channelId),
1080 "Channel::SetEngineInformation()");
1081 _engineStatisticsPtr = &engineStatistics;
1082 _outputMixerPtr = &outputMixer;
1083 _transmitMixerPtr = &transmitMixer,
1084 _moduleProcessThreadPtr = &moduleProcessThread;
1085 _audioDeviceModulePtr = &audioDeviceModule;
1086 _voiceEngineObserverPtr = voiceEngineObserver;
1087 _callbackCritSectPtr = callbackCritSect;
1088 return 0;
1089}
1090
pbos@webrtc.org6141e132013-04-09 10:09:10 +00001091int32_t
niklase@google.com470e71d2011-07-07 08:21:25 +00001092Channel::UpdateLocalTimeStamp()
1093{
1094
andrew@webrtc.org63a50982012-05-02 23:56:37 +00001095 _timeStamp += _audioFrame.samples_per_channel_;
niklase@google.com470e71d2011-07-07 08:21:25 +00001096 return 0;
1097}
1098
pbos@webrtc.org6141e132013-04-09 10:09:10 +00001099int32_t
niklase@google.com470e71d2011-07-07 08:21:25 +00001100Channel::StartPlayout()
1101{
1102 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId,_channelId),
1103 "Channel::StartPlayout()");
henrika@webrtc.org944cbeb2014-03-18 10:32:33 +00001104 if (channel_state_.Get().playing)
niklase@google.com470e71d2011-07-07 08:21:25 +00001105 {
1106 return 0;
1107 }
roosa@google.com1b60ceb2012-12-12 23:00:29 +00001108
1109 if (!_externalMixing) {
1110 // Add participant as candidates for mixing.
1111 if (_outputMixerPtr->SetMixabilityStatus(*this, true) != 0)
1112 {
1113 _engineStatisticsPtr->SetLastError(
1114 VE_AUDIO_CONF_MIX_MODULE_ERROR, kTraceError,
1115 "StartPlayout() failed to add participant to mixer");
1116 return -1;
1117 }
niklase@google.com470e71d2011-07-07 08:21:25 +00001118 }
1119
henrika@webrtc.org944cbeb2014-03-18 10:32:33 +00001120 channel_state_.SetPlaying(true);
braveyao@webrtc.orgab129902012-06-04 03:26:39 +00001121 if (RegisterFilePlayingToMixer() != 0)
1122 return -1;
1123
niklase@google.com470e71d2011-07-07 08:21:25 +00001124 return 0;
1125}
1126
pbos@webrtc.org6141e132013-04-09 10:09:10 +00001127int32_t
niklase@google.com470e71d2011-07-07 08:21:25 +00001128Channel::StopPlayout()
1129{
1130 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId,_channelId),
1131 "Channel::StopPlayout()");
henrika@webrtc.org944cbeb2014-03-18 10:32:33 +00001132 if (!channel_state_.Get().playing)
niklase@google.com470e71d2011-07-07 08:21:25 +00001133 {
1134 return 0;
1135 }
roosa@google.com1b60ceb2012-12-12 23:00:29 +00001136
1137 if (!_externalMixing) {
1138 // Remove participant as candidates for mixing
1139 if (_outputMixerPtr->SetMixabilityStatus(*this, false) != 0)
1140 {
1141 _engineStatisticsPtr->SetLastError(
1142 VE_AUDIO_CONF_MIX_MODULE_ERROR, kTraceError,
1143 "StopPlayout() failed to remove participant from mixer");
1144 return -1;
1145 }
niklase@google.com470e71d2011-07-07 08:21:25 +00001146 }
1147
henrika@webrtc.org944cbeb2014-03-18 10:32:33 +00001148 channel_state_.SetPlaying(false);
niklase@google.com470e71d2011-07-07 08:21:25 +00001149 _outputAudioLevel.Clear();
1150
1151 return 0;
1152}
1153
pbos@webrtc.org6141e132013-04-09 10:09:10 +00001154int32_t
niklase@google.com470e71d2011-07-07 08:21:25 +00001155Channel::StartSend()
1156{
1157 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId,_channelId),
1158 "Channel::StartSend()");
xians@webrtc.org09e8c472013-07-31 16:30:19 +00001159 // Resume the previous sequence number which was reset by StopSend().
henrika@webrtc.org944cbeb2014-03-18 10:32:33 +00001160 // This needs to be done before |sending| is set to true.
xians@webrtc.org09e8c472013-07-31 16:30:19 +00001161 if (send_sequence_number_)
1162 SetInitSequenceNumber(send_sequence_number_);
1163
henrika@webrtc.org944cbeb2014-03-18 10:32:33 +00001164 if (channel_state_.Get().sending)
niklase@google.com470e71d2011-07-07 08:21:25 +00001165 {
henrika@webrtc.org944cbeb2014-03-18 10:32:33 +00001166 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +00001167 }
henrika@webrtc.org944cbeb2014-03-18 10:32:33 +00001168 channel_state_.SetSending(true);
xians@webrtc.orge07247a2011-11-28 16:31:28 +00001169
pwestin@webrtc.org2853dde2012-05-11 11:08:54 +00001170 if (_rtpRtcpModule->SetSendingStatus(true) != 0)
niklase@google.com470e71d2011-07-07 08:21:25 +00001171 {
1172 _engineStatisticsPtr->SetLastError(
1173 VE_RTP_RTCP_MODULE_ERROR, kTraceError,
1174 "StartSend() RTP/RTCP failed to start sending");
mflodman@webrtc.org9a065d12012-03-07 08:12:21 +00001175 CriticalSectionScoped cs(&_callbackCritSect);
henrika@webrtc.org944cbeb2014-03-18 10:32:33 +00001176 channel_state_.SetSending(false);
niklase@google.com470e71d2011-07-07 08:21:25 +00001177 return -1;
1178 }
xians@webrtc.orge07247a2011-11-28 16:31:28 +00001179
niklase@google.com470e71d2011-07-07 08:21:25 +00001180 return 0;
1181}
1182
pbos@webrtc.org6141e132013-04-09 10:09:10 +00001183int32_t
niklase@google.com470e71d2011-07-07 08:21:25 +00001184Channel::StopSend()
1185{
1186 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId,_channelId),
1187 "Channel::StopSend()");
henrika@webrtc.org944cbeb2014-03-18 10:32:33 +00001188 if (!channel_state_.Get().sending)
niklase@google.com470e71d2011-07-07 08:21:25 +00001189 {
henrika@webrtc.org944cbeb2014-03-18 10:32:33 +00001190 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +00001191 }
henrika@webrtc.org944cbeb2014-03-18 10:32:33 +00001192 channel_state_.SetSending(false);
xians@webrtc.orge07247a2011-11-28 16:31:28 +00001193
xians@webrtc.org09e8c472013-07-31 16:30:19 +00001194 // Store the sequence number to be able to pick up the same sequence for
1195 // the next StartSend(). This is needed for restarting device, otherwise
1196 // it might cause libSRTP to complain about packets being replayed.
1197 // TODO(xians): Remove this workaround after RtpRtcpModule's refactoring
1198 // CL is landed. See issue
1199 // https://code.google.com/p/webrtc/issues/detail?id=2111 .
1200 send_sequence_number_ = _rtpRtcpModule->SequenceNumber();
1201
niklase@google.com470e71d2011-07-07 08:21:25 +00001202 // Reset sending SSRC and sequence number and triggers direct transmission
1203 // of RTCP BYE
pwestin@webrtc.org2853dde2012-05-11 11:08:54 +00001204 if (_rtpRtcpModule->SetSendingStatus(false) == -1 ||
1205 _rtpRtcpModule->ResetSendDataCountersRTP() == -1)
niklase@google.com470e71d2011-07-07 08:21:25 +00001206 {
1207 _engineStatisticsPtr->SetLastError(
1208 VE_RTP_RTCP_MODULE_ERROR, kTraceWarning,
1209 "StartSend() RTP/RTCP failed to stop sending");
1210 }
1211
niklase@google.com470e71d2011-07-07 08:21:25 +00001212 return 0;
1213}
1214
pbos@webrtc.org6141e132013-04-09 10:09:10 +00001215int32_t
niklase@google.com470e71d2011-07-07 08:21:25 +00001216Channel::StartReceiving()
1217{
1218 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId,_channelId),
1219 "Channel::StartReceiving()");
henrika@webrtc.org944cbeb2014-03-18 10:32:33 +00001220 if (channel_state_.Get().receiving)
niklase@google.com470e71d2011-07-07 08:21:25 +00001221 {
1222 return 0;
1223 }
henrika@webrtc.org944cbeb2014-03-18 10:32:33 +00001224 channel_state_.SetReceiving(true);
niklase@google.com470e71d2011-07-07 08:21:25 +00001225 _numberOfDiscardedPackets = 0;
1226 return 0;
1227}
1228
pbos@webrtc.org6141e132013-04-09 10:09:10 +00001229int32_t
niklase@google.com470e71d2011-07-07 08:21:25 +00001230Channel::StopReceiving()
1231{
1232 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId,_channelId),
1233 "Channel::StopReceiving()");
henrika@webrtc.org944cbeb2014-03-18 10:32:33 +00001234 if (!channel_state_.Get().receiving)
niklase@google.com470e71d2011-07-07 08:21:25 +00001235 {
1236 return 0;
1237 }
pwestin@webrtc.org684f0572013-03-13 23:20:57 +00001238
henrika@webrtc.org944cbeb2014-03-18 10:32:33 +00001239 channel_state_.SetReceiving(false);
niklase@google.com470e71d2011-07-07 08:21:25 +00001240 return 0;
1241}
1242
pbos@webrtc.org6141e132013-04-09 10:09:10 +00001243int32_t
niklase@google.com470e71d2011-07-07 08:21:25 +00001244Channel::RegisterVoiceEngineObserver(VoiceEngineObserver& observer)
1245{
1246 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId,_channelId),
1247 "Channel::RegisterVoiceEngineObserver()");
mflodman@webrtc.org9a065d12012-03-07 08:12:21 +00001248 CriticalSectionScoped cs(&_callbackCritSect);
niklase@google.com470e71d2011-07-07 08:21:25 +00001249
1250 if (_voiceEngineObserverPtr)
1251 {
1252 _engineStatisticsPtr->SetLastError(
1253 VE_INVALID_OPERATION, kTraceError,
1254 "RegisterVoiceEngineObserver() observer already enabled");
1255 return -1;
1256 }
1257 _voiceEngineObserverPtr = &observer;
1258 return 0;
1259}
1260
pbos@webrtc.org6141e132013-04-09 10:09:10 +00001261int32_t
niklase@google.com470e71d2011-07-07 08:21:25 +00001262Channel::DeRegisterVoiceEngineObserver()
1263{
1264 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId,_channelId),
1265 "Channel::DeRegisterVoiceEngineObserver()");
mflodman@webrtc.org9a065d12012-03-07 08:12:21 +00001266 CriticalSectionScoped cs(&_callbackCritSect);
niklase@google.com470e71d2011-07-07 08:21:25 +00001267
1268 if (!_voiceEngineObserverPtr)
1269 {
1270 _engineStatisticsPtr->SetLastError(
1271 VE_INVALID_OPERATION, kTraceWarning,
1272 "DeRegisterVoiceEngineObserver() observer already disabled");
1273 return 0;
1274 }
1275 _voiceEngineObserverPtr = NULL;
1276 return 0;
1277}
1278
pbos@webrtc.org6141e132013-04-09 10:09:10 +00001279int32_t
niklase@google.com470e71d2011-07-07 08:21:25 +00001280Channel::GetSendCodec(CodecInst& codec)
1281{
andrew@webrtc.orgeb524d92013-09-23 23:02:24 +00001282 return (audio_coding_->SendCodec(&codec));
niklase@google.com470e71d2011-07-07 08:21:25 +00001283}
1284
pbos@webrtc.org6141e132013-04-09 10:09:10 +00001285int32_t
niklase@google.com470e71d2011-07-07 08:21:25 +00001286Channel::GetRecCodec(CodecInst& codec)
1287{
andrew@webrtc.orgeb524d92013-09-23 23:02:24 +00001288 return (audio_coding_->ReceiveCodec(&codec));
niklase@google.com470e71d2011-07-07 08:21:25 +00001289}
1290
pbos@webrtc.org6141e132013-04-09 10:09:10 +00001291int32_t
niklase@google.com470e71d2011-07-07 08:21:25 +00001292Channel::SetSendCodec(const CodecInst& codec)
1293{
1294 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId,_channelId),
1295 "Channel::SetSendCodec()");
1296
andrew@webrtc.orgeb524d92013-09-23 23:02:24 +00001297 if (audio_coding_->RegisterSendCodec(codec) != 0)
niklase@google.com470e71d2011-07-07 08:21:25 +00001298 {
1299 WEBRTC_TRACE(kTraceError, kTraceVoice, VoEId(_instanceId,_channelId),
1300 "SetSendCodec() failed to register codec to ACM");
1301 return -1;
1302 }
1303
pwestin@webrtc.org2853dde2012-05-11 11:08:54 +00001304 if (_rtpRtcpModule->RegisterSendPayload(codec) != 0)
niklase@google.com470e71d2011-07-07 08:21:25 +00001305 {
pwestin@webrtc.org2853dde2012-05-11 11:08:54 +00001306 _rtpRtcpModule->DeRegisterSendPayload(codec.pltype);
1307 if (_rtpRtcpModule->RegisterSendPayload(codec) != 0)
niklase@google.com470e71d2011-07-07 08:21:25 +00001308 {
1309 WEBRTC_TRACE(
1310 kTraceError, kTraceVoice, VoEId(_instanceId,_channelId),
1311 "SetSendCodec() failed to register codec to"
1312 " RTP/RTCP module");
1313 return -1;
1314 }
1315 }
1316
pwestin@webrtc.org2853dde2012-05-11 11:08:54 +00001317 if (_rtpRtcpModule->SetAudioPacketSize(codec.pacsize) != 0)
niklase@google.com470e71d2011-07-07 08:21:25 +00001318 {
1319 WEBRTC_TRACE(kTraceError, kTraceVoice, VoEId(_instanceId,_channelId),
1320 "SetSendCodec() failed to set audio packet size");
1321 return -1;
1322 }
1323
minyue@webrtc.orgc1a40a72014-05-28 09:52:06 +00001324 bitrate_controller_->SetBitrateObserver(send_bitrate_observer_.get(),
1325 codec.rate, 0, 0);
1326
niklase@google.com470e71d2011-07-07 08:21:25 +00001327 return 0;
1328}
1329
minyue@webrtc.orgc1a40a72014-05-28 09:52:06 +00001330void
1331Channel::OnNetworkChanged(const uint32_t bitrate_bps,
1332 const uint8_t fraction_lost, // 0 - 255.
pkasting@chromium.org16825b12015-01-12 21:51:21 +00001333 const int64_t rtt) {
minyue@webrtc.orgc1a40a72014-05-28 09:52:06 +00001334 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId,_channelId),
pkasting@chromium.org16825b12015-01-12 21:51:21 +00001335 "Channel::OnNetworkChanged(bitrate_bps=%d, fration_lost=%d, rtt=%" PRId64
1336 ")", bitrate_bps, fraction_lost, rtt);
minyue@webrtc.org74aaf292014-07-16 21:28:26 +00001337 // |fraction_lost| from BitrateObserver is short time observation of packet
1338 // loss rate from past. We use network predictor to make a more reasonable
1339 // loss rate estimation.
1340 network_predictor_->UpdatePacketLossRate(fraction_lost);
1341 uint8_t loss_rate = network_predictor_->GetLossRate();
minyue@webrtc.orgc1a40a72014-05-28 09:52:06 +00001342 // Normalizes rate to 0 - 100.
minyue@webrtc.org74aaf292014-07-16 21:28:26 +00001343 if (audio_coding_->SetPacketLossRate(100 * loss_rate / 255) != 0) {
minyue@webrtc.orgc1a40a72014-05-28 09:52:06 +00001344 _engineStatisticsPtr->SetLastError(VE_AUDIO_CODING_MODULE_ERROR,
1345 kTraceError, "OnNetworkChanged() failed to set packet loss rate");
1346 assert(false); // This should not happen.
1347 }
1348}
1349
pbos@webrtc.org6141e132013-04-09 10:09:10 +00001350int32_t
niklase@google.com470e71d2011-07-07 08:21:25 +00001351Channel::SetVADStatus(bool enableVAD, ACMVADMode mode, bool disableDTX)
1352{
1353 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId,_channelId),
1354 "Channel::SetVADStatus(mode=%d)", mode);
henrik.lundin@webrtc.org664ccb72015-01-28 14:49:05 +00001355 assert(!(disableDTX && enableVAD)); // disableDTX mode is deprecated.
niklase@google.com470e71d2011-07-07 08:21:25 +00001356 // To disable VAD, DTX must be disabled too
1357 disableDTX = ((enableVAD == false) ? true : disableDTX);
andrew@webrtc.orgeb524d92013-09-23 23:02:24 +00001358 if (audio_coding_->SetVAD(!disableDTX, enableVAD, mode) != 0)
niklase@google.com470e71d2011-07-07 08:21:25 +00001359 {
1360 _engineStatisticsPtr->SetLastError(
1361 VE_AUDIO_CODING_MODULE_ERROR, kTraceError,
1362 "SetVADStatus() failed to set VAD");
1363 return -1;
1364 }
1365 return 0;
1366}
1367
pbos@webrtc.org6141e132013-04-09 10:09:10 +00001368int32_t
niklase@google.com470e71d2011-07-07 08:21:25 +00001369Channel::GetVADStatus(bool& enabledVAD, ACMVADMode& mode, bool& disabledDTX)
1370{
1371 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId,_channelId),
1372 "Channel::GetVADStatus");
andrew@webrtc.orgeb524d92013-09-23 23:02:24 +00001373 if (audio_coding_->VAD(&disabledDTX, &enabledVAD, &mode) != 0)
niklase@google.com470e71d2011-07-07 08:21:25 +00001374 {
1375 _engineStatisticsPtr->SetLastError(
1376 VE_AUDIO_CODING_MODULE_ERROR, kTraceError,
1377 "GetVADStatus() failed to get VAD status");
1378 return -1;
1379 }
1380 disabledDTX = !disabledDTX;
1381 return 0;
1382}
1383
pbos@webrtc.org6141e132013-04-09 10:09:10 +00001384int32_t
niklase@google.com470e71d2011-07-07 08:21:25 +00001385Channel::SetRecPayloadType(const CodecInst& codec)
1386{
1387 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId,_channelId),
1388 "Channel::SetRecPayloadType()");
1389
henrika@webrtc.org944cbeb2014-03-18 10:32:33 +00001390 if (channel_state_.Get().playing)
niklase@google.com470e71d2011-07-07 08:21:25 +00001391 {
1392 _engineStatisticsPtr->SetLastError(
1393 VE_ALREADY_PLAYING, kTraceError,
1394 "SetRecPayloadType() unable to set PT while playing");
1395 return -1;
1396 }
henrika@webrtc.org944cbeb2014-03-18 10:32:33 +00001397 if (channel_state_.Get().receiving)
niklase@google.com470e71d2011-07-07 08:21:25 +00001398 {
1399 _engineStatisticsPtr->SetLastError(
1400 VE_ALREADY_LISTENING, kTraceError,
1401 "SetRecPayloadType() unable to set PT while listening");
1402 return -1;
1403 }
1404
1405 if (codec.pltype == -1)
1406 {
1407 // De-register the selected codec (RTP/RTCP module and ACM)
1408
pbos@webrtc.org6141e132013-04-09 10:09:10 +00001409 int8_t pltype(-1);
niklase@google.com470e71d2011-07-07 08:21:25 +00001410 CodecInst rxCodec = codec;
1411
1412 // Get payload type for the given codec
wu@webrtc.org822fbd82013-08-15 23:38:54 +00001413 rtp_payload_registry_->ReceivePayloadType(
1414 rxCodec.plname,
1415 rxCodec.plfreq,
1416 rxCodec.channels,
1417 (rxCodec.rate < 0) ? 0 : rxCodec.rate,
1418 &pltype);
niklase@google.com470e71d2011-07-07 08:21:25 +00001419 rxCodec.pltype = pltype;
1420
wu@webrtc.org822fbd82013-08-15 23:38:54 +00001421 if (rtp_receiver_->DeRegisterReceivePayload(pltype) != 0)
niklase@google.com470e71d2011-07-07 08:21:25 +00001422 {
1423 _engineStatisticsPtr->SetLastError(
1424 VE_RTP_RTCP_MODULE_ERROR,
1425 kTraceError,
1426 "SetRecPayloadType() RTP/RTCP-module deregistration "
1427 "failed");
1428 return -1;
1429 }
andrew@webrtc.orgeb524d92013-09-23 23:02:24 +00001430 if (audio_coding_->UnregisterReceiveCodec(rxCodec.pltype) != 0)
niklase@google.com470e71d2011-07-07 08:21:25 +00001431 {
1432 _engineStatisticsPtr->SetLastError(
1433 VE_AUDIO_CODING_MODULE_ERROR, kTraceError,
1434 "SetRecPayloadType() ACM deregistration failed - 1");
1435 return -1;
1436 }
1437 return 0;
1438 }
1439
wu@webrtc.org822fbd82013-08-15 23:38:54 +00001440 if (rtp_receiver_->RegisterReceivePayload(
1441 codec.plname,
1442 codec.pltype,
1443 codec.plfreq,
1444 codec.channels,
1445 (codec.rate < 0) ? 0 : codec.rate) != 0)
niklase@google.com470e71d2011-07-07 08:21:25 +00001446 {
1447 // First attempt to register failed => de-register and try again
wu@webrtc.org822fbd82013-08-15 23:38:54 +00001448 rtp_receiver_->DeRegisterReceivePayload(codec.pltype);
1449 if (rtp_receiver_->RegisterReceivePayload(
1450 codec.plname,
1451 codec.pltype,
1452 codec.plfreq,
1453 codec.channels,
1454 (codec.rate < 0) ? 0 : codec.rate) != 0)
niklase@google.com470e71d2011-07-07 08:21:25 +00001455 {
1456 _engineStatisticsPtr->SetLastError(
1457 VE_RTP_RTCP_MODULE_ERROR, kTraceError,
1458 "SetRecPayloadType() RTP/RTCP-module registration failed");
1459 return -1;
1460 }
1461 }
andrew@webrtc.orgeb524d92013-09-23 23:02:24 +00001462 if (audio_coding_->RegisterReceiveCodec(codec) != 0)
niklase@google.com470e71d2011-07-07 08:21:25 +00001463 {
andrew@webrtc.orgeb524d92013-09-23 23:02:24 +00001464 audio_coding_->UnregisterReceiveCodec(codec.pltype);
1465 if (audio_coding_->RegisterReceiveCodec(codec) != 0)
niklase@google.com470e71d2011-07-07 08:21:25 +00001466 {
1467 _engineStatisticsPtr->SetLastError(
1468 VE_AUDIO_CODING_MODULE_ERROR, kTraceError,
1469 "SetRecPayloadType() ACM registration failed - 1");
1470 return -1;
1471 }
1472 }
1473 return 0;
1474}
1475
pbos@webrtc.org6141e132013-04-09 10:09:10 +00001476int32_t
niklase@google.com470e71d2011-07-07 08:21:25 +00001477Channel::GetRecPayloadType(CodecInst& codec)
1478{
1479 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId,_channelId),
1480 "Channel::GetRecPayloadType()");
pbos@webrtc.org6141e132013-04-09 10:09:10 +00001481 int8_t payloadType(-1);
wu@webrtc.org822fbd82013-08-15 23:38:54 +00001482 if (rtp_payload_registry_->ReceivePayloadType(
1483 codec.plname,
1484 codec.plfreq,
1485 codec.channels,
1486 (codec.rate < 0) ? 0 : codec.rate,
1487 &payloadType) != 0)
niklase@google.com470e71d2011-07-07 08:21:25 +00001488 {
1489 _engineStatisticsPtr->SetLastError(
henrika@webrtc.org37198002012-06-18 11:00:12 +00001490 VE_RTP_RTCP_MODULE_ERROR, kTraceWarning,
niklase@google.com470e71d2011-07-07 08:21:25 +00001491 "GetRecPayloadType() failed to retrieve RX payload type");
1492 return -1;
1493 }
1494 codec.pltype = payloadType;
1495 WEBRTC_TRACE(kTraceStateInfo, kTraceVoice, VoEId(_instanceId,_channelId),
1496 "Channel::GetRecPayloadType() => pltype=%u", codec.pltype);
1497 return 0;
1498}
1499
pbos@webrtc.org6141e132013-04-09 10:09:10 +00001500int32_t
niklase@google.com470e71d2011-07-07 08:21:25 +00001501Channel::SetSendCNPayloadType(int type, PayloadFrequencies frequency)
1502{
1503 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId,_channelId),
1504 "Channel::SetSendCNPayloadType()");
1505
1506 CodecInst codec;
pbos@webrtc.org6141e132013-04-09 10:09:10 +00001507 int32_t samplingFreqHz(-1);
tina.legrand@webrtc.org45175852012-06-01 09:27:35 +00001508 const int kMono = 1;
niklase@google.com470e71d2011-07-07 08:21:25 +00001509 if (frequency == kFreq32000Hz)
1510 samplingFreqHz = 32000;
1511 else if (frequency == kFreq16000Hz)
1512 samplingFreqHz = 16000;
1513
andrew@webrtc.orgeb524d92013-09-23 23:02:24 +00001514 if (audio_coding_->Codec("CN", &codec, samplingFreqHz, kMono) == -1)
niklase@google.com470e71d2011-07-07 08:21:25 +00001515 {
1516 _engineStatisticsPtr->SetLastError(
1517 VE_AUDIO_CODING_MODULE_ERROR, kTraceError,
1518 "SetSendCNPayloadType() failed to retrieve default CN codec "
1519 "settings");
1520 return -1;
1521 }
1522
1523 // Modify the payload type (must be set to dynamic range)
1524 codec.pltype = type;
1525
andrew@webrtc.orgeb524d92013-09-23 23:02:24 +00001526 if (audio_coding_->RegisterSendCodec(codec) != 0)
niklase@google.com470e71d2011-07-07 08:21:25 +00001527 {
1528 _engineStatisticsPtr->SetLastError(
1529 VE_AUDIO_CODING_MODULE_ERROR, kTraceError,
1530 "SetSendCNPayloadType() failed to register CN to ACM");
1531 return -1;
1532 }
1533
pwestin@webrtc.org2853dde2012-05-11 11:08:54 +00001534 if (_rtpRtcpModule->RegisterSendPayload(codec) != 0)
niklase@google.com470e71d2011-07-07 08:21:25 +00001535 {
pwestin@webrtc.org2853dde2012-05-11 11:08:54 +00001536 _rtpRtcpModule->DeRegisterSendPayload(codec.pltype);
1537 if (_rtpRtcpModule->RegisterSendPayload(codec) != 0)
niklase@google.com470e71d2011-07-07 08:21:25 +00001538 {
1539 _engineStatisticsPtr->SetLastError(
1540 VE_RTP_RTCP_MODULE_ERROR, kTraceError,
1541 "SetSendCNPayloadType() failed to register CN to RTP/RTCP "
1542 "module");
1543 return -1;
1544 }
1545 }
1546 return 0;
1547}
1548
minyue@webrtc.orgadee8f92014-09-03 12:28:06 +00001549int Channel::SetOpusMaxPlaybackRate(int frequency_hz) {
minyue@webrtc.org6aac93b2014-08-12 08:13:33 +00001550 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId, _channelId),
minyue@webrtc.orgadee8f92014-09-03 12:28:06 +00001551 "Channel::SetOpusMaxPlaybackRate()");
minyue@webrtc.org6aac93b2014-08-12 08:13:33 +00001552
minyue@webrtc.orgadee8f92014-09-03 12:28:06 +00001553 if (audio_coding_->SetOpusMaxPlaybackRate(frequency_hz) != 0) {
minyue@webrtc.org6aac93b2014-08-12 08:13:33 +00001554 _engineStatisticsPtr->SetLastError(
1555 VE_AUDIO_CODING_MODULE_ERROR, kTraceError,
minyue@webrtc.orgadee8f92014-09-03 12:28:06 +00001556 "SetOpusMaxPlaybackRate() failed to set maximum playback rate");
minyue@webrtc.org6aac93b2014-08-12 08:13:33 +00001557 return -1;
1558 }
1559 return 0;
1560}
1561
pbos@webrtc.org6141e132013-04-09 10:09:10 +00001562int32_t Channel::RegisterExternalTransport(Transport& transport)
niklase@google.com470e71d2011-07-07 08:21:25 +00001563{
1564 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId, _channelId),
1565 "Channel::RegisterExternalTransport()");
1566
mflodman@webrtc.org9a065d12012-03-07 08:12:21 +00001567 CriticalSectionScoped cs(&_callbackCritSect);
niklase@google.com470e71d2011-07-07 08:21:25 +00001568
niklase@google.com470e71d2011-07-07 08:21:25 +00001569 if (_externalTransport)
1570 {
1571 _engineStatisticsPtr->SetLastError(VE_INVALID_OPERATION,
1572 kTraceError,
1573 "RegisterExternalTransport() external transport already enabled");
1574 return -1;
1575 }
1576 _externalTransport = true;
1577 _transportPtr = &transport;
1578 return 0;
1579}
1580
pbos@webrtc.org6141e132013-04-09 10:09:10 +00001581int32_t
niklase@google.com470e71d2011-07-07 08:21:25 +00001582Channel::DeRegisterExternalTransport()
1583{
1584 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId,_channelId),
1585 "Channel::DeRegisterExternalTransport()");
1586
mflodman@webrtc.org9a065d12012-03-07 08:12:21 +00001587 CriticalSectionScoped cs(&_callbackCritSect);
xians@webrtc.org83661f52011-11-25 10:58:15 +00001588
niklase@google.com470e71d2011-07-07 08:21:25 +00001589 if (!_transportPtr)
1590 {
1591 _engineStatisticsPtr->SetLastError(
1592 VE_INVALID_OPERATION, kTraceWarning,
1593 "DeRegisterExternalTransport() external transport already "
1594 "disabled");
1595 return 0;
1596 }
1597 _externalTransport = false;
niklase@google.com470e71d2011-07-07 08:21:25 +00001598 _transportPtr = NULL;
1599 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId,_channelId),
1600 "DeRegisterExternalTransport() all transport is disabled");
niklase@google.com470e71d2011-07-07 08:21:25 +00001601 return 0;
1602}
1603
pkasting@chromium.org4591fbd2014-11-20 22:28:14 +00001604int32_t Channel::ReceivedRTPPacket(const int8_t* data, size_t length,
solenberg@webrtc.orgb1f50102014-03-24 10:38:25 +00001605 const PacketTime& packet_time) {
pwestin@webrtc.org0c459572013-04-03 15:43:57 +00001606 WEBRTC_TRACE(kTraceStream, kTraceVoice, VoEId(_instanceId,_channelId),
1607 "Channel::ReceivedRTPPacket()");
1608
1609 // Store playout timestamp for the received RTP packet
pwestin@webrtc.org1de01352013-04-11 20:23:35 +00001610 UpdatePlayoutTimestamp(false);
pwestin@webrtc.org0c459572013-04-03 15:43:57 +00001611
1612 // Dump the RTP packet to a file (if RTP dump is enabled).
pbos@webrtc.org6141e132013-04-09 10:09:10 +00001613 if (_rtpDumpIn.DumpPacket((const uint8_t*)data,
1614 (uint16_t)length) == -1) {
pwestin@webrtc.org0c459572013-04-03 15:43:57 +00001615 WEBRTC_TRACE(kTraceWarning, kTraceVoice,
1616 VoEId(_instanceId,_channelId),
1617 "Channel::SendPacket() RTP dump to input file failed");
1618 }
stefan@webrtc.org7bb8f022013-09-06 13:40:11 +00001619 const uint8_t* received_packet = reinterpret_cast<const uint8_t*>(data);
stefan@webrtc.orga5cb98c2013-05-29 12:12:51 +00001620 RTPHeader header;
stefan@webrtc.org7bb8f022013-09-06 13:40:11 +00001621 if (!rtp_header_parser_->Parse(received_packet, length, &header)) {
1622 WEBRTC_TRACE(webrtc::kTraceDebug, webrtc::kTraceVoice, _channelId,
1623 "Incoming packet: invalid RTP header");
stefan@webrtc.orga5cb98c2013-05-29 12:12:51 +00001624 return -1;
1625 }
wu@webrtc.org822fbd82013-08-15 23:38:54 +00001626 header.payload_type_frequency =
1627 rtp_payload_registry_->GetPayloadTypeFrequency(header.payloadType);
stefan@webrtc.org7bb8f022013-09-06 13:40:11 +00001628 if (header.payload_type_frequency < 0)
wu@webrtc.org822fbd82013-08-15 23:38:54 +00001629 return -1;
stefan@webrtc.org48df3812013-11-08 15:18:52 +00001630 bool in_order = IsPacketInOrder(header);
stefan@webrtc.org7bb8f022013-09-06 13:40:11 +00001631 rtp_receive_statistics_->IncomingPacket(header, length,
stefan@webrtc.org48df3812013-11-08 15:18:52 +00001632 IsPacketRetransmitted(header, in_order));
stefan@webrtc.org7bb8f022013-09-06 13:40:11 +00001633 rtp_payload_registry_->SetIncomingPayloadType(header);
solenberg@webrtc.orgb1f50102014-03-24 10:38:25 +00001634
1635 // Forward any packets to ViE bandwidth estimator, if enabled.
1636 {
1637 CriticalSectionScoped cs(&_callbackCritSect);
1638 if (vie_network_) {
1639 int64_t arrival_time_ms;
1640 if (packet_time.timestamp != -1) {
1641 arrival_time_ms = (packet_time.timestamp + 500) / 1000;
1642 } else {
1643 arrival_time_ms = TickTime::MillisecondTimestamp();
1644 }
pkasting@chromium.org4591fbd2014-11-20 22:28:14 +00001645 size_t payload_length = length - header.headerLength;
solenberg@webrtc.orgb1f50102014-03-24 10:38:25 +00001646 vie_network_->ReceivedBWEPacket(video_channel_, arrival_time_ms,
1647 payload_length, header);
1648 }
1649 }
1650
stefan@webrtc.org48df3812013-11-08 15:18:52 +00001651 return ReceivePacket(received_packet, length, header, in_order) ? 0 : -1;
stefan@webrtc.org7bb8f022013-09-06 13:40:11 +00001652}
1653
1654bool Channel::ReceivePacket(const uint8_t* packet,
pkasting@chromium.org4591fbd2014-11-20 22:28:14 +00001655 size_t packet_length,
stefan@webrtc.org7bb8f022013-09-06 13:40:11 +00001656 const RTPHeader& header,
1657 bool in_order) {
minyue@webrtc.org456f0142015-01-23 11:58:42 +00001658 if (rtp_payload_registry_->IsRtx(header)) {
1659 return HandleRtxPacket(packet, packet_length, header);
wu@webrtc.org822fbd82013-08-15 23:38:54 +00001660 }
stefan@webrtc.org7bb8f022013-09-06 13:40:11 +00001661 const uint8_t* payload = packet + header.headerLength;
pkasting@chromium.org4591fbd2014-11-20 22:28:14 +00001662 assert(packet_length >= header.headerLength);
1663 size_t payload_length = packet_length - header.headerLength;
wu@webrtc.org822fbd82013-08-15 23:38:54 +00001664 PayloadUnion payload_specific;
1665 if (!rtp_payload_registry_->GetPayloadSpecifics(header.payloadType,
stefan@webrtc.org7bb8f022013-09-06 13:40:11 +00001666 &payload_specific)) {
1667 return false;
wu@webrtc.org822fbd82013-08-15 23:38:54 +00001668 }
stefan@webrtc.org7bb8f022013-09-06 13:40:11 +00001669 return rtp_receiver_->IncomingRtpPacket(header, payload, payload_length,
1670 payload_specific, in_order);
1671}
1672
minyue@webrtc.org456f0142015-01-23 11:58:42 +00001673bool Channel::HandleRtxPacket(const uint8_t* packet,
1674 size_t packet_length,
1675 const RTPHeader& header) {
stefan@webrtc.org7bb8f022013-09-06 13:40:11 +00001676 if (!rtp_payload_registry_->IsRtx(header))
1677 return false;
1678
1679 // Remove the RTX header and parse the original RTP header.
1680 if (packet_length < header.headerLength)
1681 return false;
1682 if (packet_length > kVoiceEngineMaxIpPacketSizeBytes)
1683 return false;
1684 if (restored_packet_in_use_) {
1685 WEBRTC_TRACE(webrtc::kTraceDebug, webrtc::kTraceVoice, _channelId,
1686 "Multiple RTX headers detected, dropping packet");
1687 return false;
pwestin@webrtc.org0c459572013-04-03 15:43:57 +00001688 }
stefan@webrtc.org7bb8f022013-09-06 13:40:11 +00001689 uint8_t* restored_packet_ptr = restored_packet_;
1690 if (!rtp_payload_registry_->RestoreOriginalPacket(
1691 &restored_packet_ptr, packet, &packet_length, rtp_receiver_->SSRC(),
1692 header)) {
1693 WEBRTC_TRACE(webrtc::kTraceDebug, webrtc::kTraceVoice, _channelId,
1694 "Incoming RTX packet: invalid RTP header");
1695 return false;
1696 }
1697 restored_packet_in_use_ = true;
1698 bool ret = OnRecoveredPacket(restored_packet_ptr, packet_length);
1699 restored_packet_in_use_ = false;
1700 return ret;
1701}
1702
1703bool Channel::IsPacketInOrder(const RTPHeader& header) const {
1704 StreamStatistician* statistician =
1705 rtp_receive_statistics_->GetStatistician(header.ssrc);
1706 if (!statistician)
1707 return false;
1708 return statistician->IsPacketInOrder(header.sequenceNumber);
niklase@google.com470e71d2011-07-07 08:21:25 +00001709}
1710
stefan@webrtc.org48df3812013-11-08 15:18:52 +00001711bool Channel::IsPacketRetransmitted(const RTPHeader& header,
1712 bool in_order) const {
stefan@webrtc.org7bb8f022013-09-06 13:40:11 +00001713 // Retransmissions are handled separately if RTX is enabled.
1714 if (rtp_payload_registry_->RtxEnabled())
1715 return false;
1716 StreamStatistician* statistician =
1717 rtp_receive_statistics_->GetStatistician(header.ssrc);
1718 if (!statistician)
1719 return false;
1720 // Check if this is a retransmission.
pkasting@chromium.org16825b12015-01-12 21:51:21 +00001721 int64_t min_rtt = 0;
stefan@webrtc.org7bb8f022013-09-06 13:40:11 +00001722 _rtpRtcpModule->RTT(rtp_receiver_->SSRC(), NULL, NULL, &min_rtt, NULL);
stefan@webrtc.org48df3812013-11-08 15:18:52 +00001723 return !in_order &&
stefan@webrtc.org7bb8f022013-09-06 13:40:11 +00001724 statistician->IsRetransmitOfOldPacket(header, min_rtt);
wu@webrtc.org822fbd82013-08-15 23:38:54 +00001725}
1726
pkasting@chromium.org4591fbd2014-11-20 22:28:14 +00001727int32_t Channel::ReceivedRTCPPacket(const int8_t* data, size_t length) {
pwestin@webrtc.org0c459572013-04-03 15:43:57 +00001728 WEBRTC_TRACE(kTraceStream, kTraceVoice, VoEId(_instanceId,_channelId),
1729 "Channel::ReceivedRTCPPacket()");
1730 // Store playout timestamp for the received RTCP packet
pwestin@webrtc.org1de01352013-04-11 20:23:35 +00001731 UpdatePlayoutTimestamp(true);
pwestin@webrtc.org0c459572013-04-03 15:43:57 +00001732
1733 // Dump the RTCP packet to a file (if RTP dump is enabled).
pkasting@chromium.org4591fbd2014-11-20 22:28:14 +00001734 if (_rtpDumpIn.DumpPacket((const uint8_t*)data, length) == -1) {
pwestin@webrtc.org0c459572013-04-03 15:43:57 +00001735 WEBRTC_TRACE(kTraceWarning, kTraceVoice,
1736 VoEId(_instanceId,_channelId),
1737 "Channel::SendPacket() RTCP dump to input file failed");
1738 }
1739
1740 // Deliver RTCP packet to RTP/RTCP module for parsing
pkasting@chromium.org4591fbd2014-11-20 22:28:14 +00001741 if (_rtpRtcpModule->IncomingRtcpPacket((const uint8_t*)data, length) == -1) {
pwestin@webrtc.org0c459572013-04-03 15:43:57 +00001742 _engineStatisticsPtr->SetLastError(
1743 VE_SOCKET_TRANSPORT_MODULE_ERROR, kTraceWarning,
1744 "Channel::IncomingRTPPacket() RTCP packet is invalid");
1745 }
wu@webrtc.org82c4b852014-05-20 22:55:01 +00001746
stefan@webrtc.org8e24d872014-09-02 18:58:24 +00001747 {
1748 CriticalSectionScoped lock(ts_stats_lock_.get());
pkasting@chromium.org16825b12015-01-12 21:51:21 +00001749 int64_t rtt = GetRTT();
minyue@webrtc.org2c0cdbc2014-10-09 10:52:43 +00001750 if (rtt == 0) {
1751 // Waiting for valid RTT.
1752 return 0;
1753 }
1754 uint32_t ntp_secs = 0;
1755 uint32_t ntp_frac = 0;
1756 uint32_t rtp_timestamp = 0;
1757 if (0 != _rtpRtcpModule->RemoteNTP(&ntp_secs, &ntp_frac, NULL, NULL,
1758 &rtp_timestamp)) {
1759 // Waiting for RTCP.
1760 return 0;
1761 }
1762 ntp_estimator_.UpdateRtcpTimestamp(rtt, ntp_secs, ntp_frac, rtp_timestamp);
stefan@webrtc.org8e24d872014-09-02 18:58:24 +00001763 }
pwestin@webrtc.org0c459572013-04-03 15:43:57 +00001764 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +00001765}
1766
niklase@google.com470e71d2011-07-07 08:21:25 +00001767int Channel::StartPlayingFileLocally(const char* fileName,
pbos@webrtc.org92135212013-05-14 08:31:39 +00001768 bool loop,
1769 FileFormats format,
1770 int startPosition,
1771 float volumeScaling,
1772 int stopPosition,
niklase@google.com470e71d2011-07-07 08:21:25 +00001773 const CodecInst* codecInst)
1774{
1775 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId,_channelId),
1776 "Channel::StartPlayingFileLocally(fileNameUTF8[]=%s, loop=%d,"
1777 " format=%d, volumeScaling=%5.3f, startPosition=%d, "
1778 "stopPosition=%d)", fileName, loop, format, volumeScaling,
1779 startPosition, stopPosition);
1780
henrika@webrtc.org944cbeb2014-03-18 10:32:33 +00001781 if (channel_state_.Get().output_file_playing)
niklase@google.com470e71d2011-07-07 08:21:25 +00001782 {
1783 _engineStatisticsPtr->SetLastError(
1784 VE_ALREADY_PLAYING, kTraceError,
1785 "StartPlayingFileLocally() is already playing");
1786 return -1;
1787 }
1788
niklase@google.com470e71d2011-07-07 08:21:25 +00001789 {
mflodman@webrtc.org9a065d12012-03-07 08:12:21 +00001790 CriticalSectionScoped cs(&_fileCritSect);
henrike@webrtc.orgb37c6282011-10-31 23:53:04 +00001791
1792 if (_outputFilePlayerPtr)
1793 {
1794 _outputFilePlayerPtr->RegisterModuleFileCallback(NULL);
1795 FilePlayer::DestroyFilePlayer(_outputFilePlayerPtr);
1796 _outputFilePlayerPtr = NULL;
1797 }
1798
1799 _outputFilePlayerPtr = FilePlayer::CreateFilePlayer(
1800 _outputFilePlayerId, (const FileFormats)format);
1801
1802 if (_outputFilePlayerPtr == NULL)
1803 {
1804 _engineStatisticsPtr->SetLastError(
1805 VE_INVALID_ARGUMENT, kTraceError,
henrike@webrtc.org31d30702011-11-18 19:59:32 +00001806 "StartPlayingFileLocally() filePlayer format is not correct");
henrike@webrtc.orgb37c6282011-10-31 23:53:04 +00001807 return -1;
1808 }
1809
pbos@webrtc.org6141e132013-04-09 10:09:10 +00001810 const uint32_t notificationTime(0);
henrike@webrtc.orgb37c6282011-10-31 23:53:04 +00001811
1812 if (_outputFilePlayerPtr->StartPlayingFile(
1813 fileName,
1814 loop,
1815 startPosition,
1816 volumeScaling,
1817 notificationTime,
1818 stopPosition,
1819 (const CodecInst*)codecInst) != 0)
1820 {
1821 _engineStatisticsPtr->SetLastError(
1822 VE_BAD_FILE, kTraceError,
1823 "StartPlayingFile() failed to start file playout");
1824 _outputFilePlayerPtr->StopPlayingFile();
1825 FilePlayer::DestroyFilePlayer(_outputFilePlayerPtr);
1826 _outputFilePlayerPtr = NULL;
1827 return -1;
1828 }
1829 _outputFilePlayerPtr->RegisterModuleFileCallback(this);
henrika@webrtc.org944cbeb2014-03-18 10:32:33 +00001830 channel_state_.SetOutputFilePlaying(true);
niklase@google.com470e71d2011-07-07 08:21:25 +00001831 }
braveyao@webrtc.orgab129902012-06-04 03:26:39 +00001832
1833 if (RegisterFilePlayingToMixer() != 0)
henrike@webrtc.org066f9e52011-10-28 23:15:47 +00001834 return -1;
niklase@google.com470e71d2011-07-07 08:21:25 +00001835
1836 return 0;
1837}
1838
1839int Channel::StartPlayingFileLocally(InStream* stream,
pbos@webrtc.org92135212013-05-14 08:31:39 +00001840 FileFormats format,
1841 int startPosition,
1842 float volumeScaling,
1843 int stopPosition,
niklase@google.com470e71d2011-07-07 08:21:25 +00001844 const CodecInst* codecInst)
1845{
1846 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId,_channelId),
1847 "Channel::StartPlayingFileLocally(format=%d,"
1848 " volumeScaling=%5.3f, startPosition=%d, stopPosition=%d)",
1849 format, volumeScaling, startPosition, stopPosition);
1850
1851 if(stream == NULL)
1852 {
1853 _engineStatisticsPtr->SetLastError(
1854 VE_BAD_FILE, kTraceError,
1855 "StartPlayingFileLocally() NULL as input stream");
1856 return -1;
1857 }
1858
1859
henrika@webrtc.org944cbeb2014-03-18 10:32:33 +00001860 if (channel_state_.Get().output_file_playing)
niklase@google.com470e71d2011-07-07 08:21:25 +00001861 {
1862 _engineStatisticsPtr->SetLastError(
1863 VE_ALREADY_PLAYING, kTraceError,
1864 "StartPlayingFileLocally() is already playing");
1865 return -1;
1866 }
1867
niklase@google.com470e71d2011-07-07 08:21:25 +00001868 {
mflodman@webrtc.org9a065d12012-03-07 08:12:21 +00001869 CriticalSectionScoped cs(&_fileCritSect);
henrike@webrtc.orgb37c6282011-10-31 23:53:04 +00001870
1871 // Destroy the old instance
1872 if (_outputFilePlayerPtr)
1873 {
1874 _outputFilePlayerPtr->RegisterModuleFileCallback(NULL);
1875 FilePlayer::DestroyFilePlayer(_outputFilePlayerPtr);
1876 _outputFilePlayerPtr = NULL;
1877 }
1878
1879 // Create the instance
1880 _outputFilePlayerPtr = FilePlayer::CreateFilePlayer(
1881 _outputFilePlayerId,
1882 (const FileFormats)format);
1883
1884 if (_outputFilePlayerPtr == NULL)
1885 {
1886 _engineStatisticsPtr->SetLastError(
1887 VE_INVALID_ARGUMENT, kTraceError,
1888 "StartPlayingFileLocally() filePlayer format isnot correct");
1889 return -1;
1890 }
1891
pbos@webrtc.org6141e132013-04-09 10:09:10 +00001892 const uint32_t notificationTime(0);
henrike@webrtc.orgb37c6282011-10-31 23:53:04 +00001893
1894 if (_outputFilePlayerPtr->StartPlayingFile(*stream, startPosition,
1895 volumeScaling,
1896 notificationTime,
1897 stopPosition, codecInst) != 0)
1898 {
1899 _engineStatisticsPtr->SetLastError(VE_BAD_FILE, kTraceError,
1900 "StartPlayingFile() failed to "
1901 "start file playout");
1902 _outputFilePlayerPtr->StopPlayingFile();
1903 FilePlayer::DestroyFilePlayer(_outputFilePlayerPtr);
1904 _outputFilePlayerPtr = NULL;
1905 return -1;
1906 }
1907 _outputFilePlayerPtr->RegisterModuleFileCallback(this);
henrika@webrtc.org944cbeb2014-03-18 10:32:33 +00001908 channel_state_.SetOutputFilePlaying(true);
niklase@google.com470e71d2011-07-07 08:21:25 +00001909 }
braveyao@webrtc.orgab129902012-06-04 03:26:39 +00001910
1911 if (RegisterFilePlayingToMixer() != 0)
henrike@webrtc.org066f9e52011-10-28 23:15:47 +00001912 return -1;
niklase@google.com470e71d2011-07-07 08:21:25 +00001913
niklase@google.com470e71d2011-07-07 08:21:25 +00001914 return 0;
1915}
1916
1917int Channel::StopPlayingFileLocally()
1918{
1919 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId,_channelId),
1920 "Channel::StopPlayingFileLocally()");
1921
henrika@webrtc.org944cbeb2014-03-18 10:32:33 +00001922 if (!channel_state_.Get().output_file_playing)
niklase@google.com470e71d2011-07-07 08:21:25 +00001923 {
1924 _engineStatisticsPtr->SetLastError(
1925 VE_INVALID_OPERATION, kTraceWarning,
1926 "StopPlayingFileLocally() isnot playing");
1927 return 0;
1928 }
1929
niklase@google.com470e71d2011-07-07 08:21:25 +00001930 {
mflodman@webrtc.org9a065d12012-03-07 08:12:21 +00001931 CriticalSectionScoped cs(&_fileCritSect);
henrike@webrtc.orgb37c6282011-10-31 23:53:04 +00001932
1933 if (_outputFilePlayerPtr->StopPlayingFile() != 0)
1934 {
1935 _engineStatisticsPtr->SetLastError(
1936 VE_STOP_RECORDING_FAILED, kTraceError,
1937 "StopPlayingFile() could not stop playing");
1938 return -1;
1939 }
1940 _outputFilePlayerPtr->RegisterModuleFileCallback(NULL);
1941 FilePlayer::DestroyFilePlayer(_outputFilePlayerPtr);
1942 _outputFilePlayerPtr = NULL;
henrika@webrtc.org944cbeb2014-03-18 10:32:33 +00001943 channel_state_.SetOutputFilePlaying(false);
niklase@google.com470e71d2011-07-07 08:21:25 +00001944 }
henrike@webrtc.orgb37c6282011-10-31 23:53:04 +00001945 // _fileCritSect cannot be taken while calling
1946 // SetAnonymousMixibilityStatus. Refer to comments in
1947 // StartPlayingFileLocally(const char* ...) for more details.
henrike@webrtc.org066f9e52011-10-28 23:15:47 +00001948 if (_outputMixerPtr->SetAnonymousMixabilityStatus(*this, false) != 0)
1949 {
1950 _engineStatisticsPtr->SetLastError(
1951 VE_AUDIO_CONF_MIX_MODULE_ERROR, kTraceError,
henrike@webrtc.orgb37c6282011-10-31 23:53:04 +00001952 "StopPlayingFile() failed to stop participant from playing as"
1953 "file in the mixer");
henrike@webrtc.org066f9e52011-10-28 23:15:47 +00001954 return -1;
1955 }
niklase@google.com470e71d2011-07-07 08:21:25 +00001956
1957 return 0;
1958}
1959
1960int Channel::IsPlayingFileLocally() const
1961{
1962 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId,_channelId),
1963 "Channel::IsPlayingFileLocally()");
1964
henrika@webrtc.org944cbeb2014-03-18 10:32:33 +00001965 return channel_state_.Get().output_file_playing;
niklase@google.com470e71d2011-07-07 08:21:25 +00001966}
1967
braveyao@webrtc.orgab129902012-06-04 03:26:39 +00001968int Channel::RegisterFilePlayingToMixer()
1969{
1970 // Return success for not registering for file playing to mixer if:
1971 // 1. playing file before playout is started on that channel.
1972 // 2. starting playout without file playing on that channel.
henrika@webrtc.org944cbeb2014-03-18 10:32:33 +00001973 if (!channel_state_.Get().playing ||
1974 !channel_state_.Get().output_file_playing)
braveyao@webrtc.orgab129902012-06-04 03:26:39 +00001975 {
1976 return 0;
1977 }
1978
1979 // |_fileCritSect| cannot be taken while calling
1980 // SetAnonymousMixabilityStatus() since as soon as the participant is added
1981 // frames can be pulled by the mixer. Since the frames are generated from
1982 // the file, _fileCritSect will be taken. This would result in a deadlock.
1983 if (_outputMixerPtr->SetAnonymousMixabilityStatus(*this, true) != 0)
1984 {
henrika@webrtc.org944cbeb2014-03-18 10:32:33 +00001985 channel_state_.SetOutputFilePlaying(false);
braveyao@webrtc.orgab129902012-06-04 03:26:39 +00001986 CriticalSectionScoped cs(&_fileCritSect);
braveyao@webrtc.orgab129902012-06-04 03:26:39 +00001987 _engineStatisticsPtr->SetLastError(
1988 VE_AUDIO_CONF_MIX_MODULE_ERROR, kTraceError,
1989 "StartPlayingFile() failed to add participant as file to mixer");
1990 _outputFilePlayerPtr->StopPlayingFile();
1991 FilePlayer::DestroyFilePlayer(_outputFilePlayerPtr);
1992 _outputFilePlayerPtr = NULL;
1993 return -1;
1994 }
1995
1996 return 0;
1997}
1998
niklase@google.com470e71d2011-07-07 08:21:25 +00001999int Channel::StartPlayingFileAsMicrophone(const char* fileName,
pbos@webrtc.org92135212013-05-14 08:31:39 +00002000 bool loop,
2001 FileFormats format,
2002 int startPosition,
2003 float volumeScaling,
2004 int stopPosition,
niklase@google.com470e71d2011-07-07 08:21:25 +00002005 const CodecInst* codecInst)
2006{
2007 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId,_channelId),
2008 "Channel::StartPlayingFileAsMicrophone(fileNameUTF8[]=%s, "
2009 "loop=%d, format=%d, volumeScaling=%5.3f, startPosition=%d, "
2010 "stopPosition=%d)", fileName, loop, format, volumeScaling,
2011 startPosition, stopPosition);
2012
henrika@webrtc.org944cbeb2014-03-18 10:32:33 +00002013 CriticalSectionScoped cs(&_fileCritSect);
2014
2015 if (channel_state_.Get().input_file_playing)
niklase@google.com470e71d2011-07-07 08:21:25 +00002016 {
2017 _engineStatisticsPtr->SetLastError(
2018 VE_ALREADY_PLAYING, kTraceWarning,
2019 "StartPlayingFileAsMicrophone() filePlayer is playing");
2020 return 0;
2021 }
2022
niklase@google.com470e71d2011-07-07 08:21:25 +00002023 // Destroy the old instance
2024 if (_inputFilePlayerPtr)
2025 {
2026 _inputFilePlayerPtr->RegisterModuleFileCallback(NULL);
2027 FilePlayer::DestroyFilePlayer(_inputFilePlayerPtr);
2028 _inputFilePlayerPtr = NULL;
2029 }
2030
2031 // Create the instance
2032 _inputFilePlayerPtr = FilePlayer::CreateFilePlayer(
2033 _inputFilePlayerId, (const FileFormats)format);
2034
2035 if (_inputFilePlayerPtr == NULL)
2036 {
2037 _engineStatisticsPtr->SetLastError(
2038 VE_INVALID_ARGUMENT, kTraceError,
2039 "StartPlayingFileAsMicrophone() filePlayer format isnot correct");
2040 return -1;
2041 }
2042
pbos@webrtc.org6141e132013-04-09 10:09:10 +00002043 const uint32_t notificationTime(0);
niklase@google.com470e71d2011-07-07 08:21:25 +00002044
2045 if (_inputFilePlayerPtr->StartPlayingFile(
2046 fileName,
2047 loop,
2048 startPosition,
2049 volumeScaling,
2050 notificationTime,
2051 stopPosition,
2052 (const CodecInst*)codecInst) != 0)
2053 {
2054 _engineStatisticsPtr->SetLastError(
2055 VE_BAD_FILE, kTraceError,
2056 "StartPlayingFile() failed to start file playout");
2057 _inputFilePlayerPtr->StopPlayingFile();
2058 FilePlayer::DestroyFilePlayer(_inputFilePlayerPtr);
2059 _inputFilePlayerPtr = NULL;
2060 return -1;
2061 }
2062 _inputFilePlayerPtr->RegisterModuleFileCallback(this);
henrika@webrtc.org944cbeb2014-03-18 10:32:33 +00002063 channel_state_.SetInputFilePlaying(true);
niklase@google.com470e71d2011-07-07 08:21:25 +00002064
2065 return 0;
2066}
2067
2068int Channel::StartPlayingFileAsMicrophone(InStream* stream,
pbos@webrtc.org92135212013-05-14 08:31:39 +00002069 FileFormats format,
2070 int startPosition,
2071 float volumeScaling,
2072 int stopPosition,
niklase@google.com470e71d2011-07-07 08:21:25 +00002073 const CodecInst* codecInst)
2074{
2075 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId,_channelId),
2076 "Channel::StartPlayingFileAsMicrophone(format=%d, "
2077 "volumeScaling=%5.3f, startPosition=%d, stopPosition=%d)",
2078 format, volumeScaling, startPosition, stopPosition);
2079
2080 if(stream == NULL)
2081 {
2082 _engineStatisticsPtr->SetLastError(
2083 VE_BAD_FILE, kTraceError,
2084 "StartPlayingFileAsMicrophone NULL as input stream");
2085 return -1;
2086 }
2087
henrika@webrtc.org944cbeb2014-03-18 10:32:33 +00002088 CriticalSectionScoped cs(&_fileCritSect);
2089
2090 if (channel_state_.Get().input_file_playing)
niklase@google.com470e71d2011-07-07 08:21:25 +00002091 {
2092 _engineStatisticsPtr->SetLastError(
2093 VE_ALREADY_PLAYING, kTraceWarning,
2094 "StartPlayingFileAsMicrophone() is playing");
2095 return 0;
2096 }
2097
niklase@google.com470e71d2011-07-07 08:21:25 +00002098 // Destroy the old instance
2099 if (_inputFilePlayerPtr)
2100 {
2101 _inputFilePlayerPtr->RegisterModuleFileCallback(NULL);
2102 FilePlayer::DestroyFilePlayer(_inputFilePlayerPtr);
2103 _inputFilePlayerPtr = NULL;
2104 }
2105
2106 // Create the instance
2107 _inputFilePlayerPtr = FilePlayer::CreateFilePlayer(
2108 _inputFilePlayerId, (const FileFormats)format);
2109
2110 if (_inputFilePlayerPtr == NULL)
2111 {
2112 _engineStatisticsPtr->SetLastError(
2113 VE_INVALID_ARGUMENT, kTraceError,
2114 "StartPlayingInputFile() filePlayer format isnot correct");
2115 return -1;
2116 }
2117
pbos@webrtc.org6141e132013-04-09 10:09:10 +00002118 const uint32_t notificationTime(0);
niklase@google.com470e71d2011-07-07 08:21:25 +00002119
2120 if (_inputFilePlayerPtr->StartPlayingFile(*stream, startPosition,
2121 volumeScaling, notificationTime,
2122 stopPosition, codecInst) != 0)
2123 {
2124 _engineStatisticsPtr->SetLastError(VE_BAD_FILE, kTraceError,
2125 "StartPlayingFile() failed to start "
2126 "file playout");
2127 _inputFilePlayerPtr->StopPlayingFile();
2128 FilePlayer::DestroyFilePlayer(_inputFilePlayerPtr);
2129 _inputFilePlayerPtr = NULL;
2130 return -1;
2131 }
andrew@webrtc.orgae1a58b2013-01-22 04:44:30 +00002132
niklase@google.com470e71d2011-07-07 08:21:25 +00002133 _inputFilePlayerPtr->RegisterModuleFileCallback(this);
henrika@webrtc.org944cbeb2014-03-18 10:32:33 +00002134 channel_state_.SetInputFilePlaying(true);
niklase@google.com470e71d2011-07-07 08:21:25 +00002135
2136 return 0;
2137}
2138
2139int Channel::StopPlayingFileAsMicrophone()
2140{
2141 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId,_channelId),
2142 "Channel::StopPlayingFileAsMicrophone()");
2143
henrika@webrtc.org944cbeb2014-03-18 10:32:33 +00002144 CriticalSectionScoped cs(&_fileCritSect);
2145
2146 if (!channel_state_.Get().input_file_playing)
niklase@google.com470e71d2011-07-07 08:21:25 +00002147 {
2148 _engineStatisticsPtr->SetLastError(
2149 VE_INVALID_OPERATION, kTraceWarning,
2150 "StopPlayingFileAsMicrophone() isnot playing");
2151 return 0;
2152 }
2153
niklase@google.com470e71d2011-07-07 08:21:25 +00002154 if (_inputFilePlayerPtr->StopPlayingFile() != 0)
2155 {
2156 _engineStatisticsPtr->SetLastError(
2157 VE_STOP_RECORDING_FAILED, kTraceError,
2158 "StopPlayingFile() could not stop playing");
2159 return -1;
2160 }
2161 _inputFilePlayerPtr->RegisterModuleFileCallback(NULL);
2162 FilePlayer::DestroyFilePlayer(_inputFilePlayerPtr);
2163 _inputFilePlayerPtr = NULL;
henrika@webrtc.org944cbeb2014-03-18 10:32:33 +00002164 channel_state_.SetInputFilePlaying(false);
niklase@google.com470e71d2011-07-07 08:21:25 +00002165
2166 return 0;
2167}
2168
2169int Channel::IsPlayingFileAsMicrophone() const
2170{
2171 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId,_channelId),
2172 "Channel::IsPlayingFileAsMicrophone()");
henrika@webrtc.org944cbeb2014-03-18 10:32:33 +00002173 return channel_state_.Get().input_file_playing;
niklase@google.com470e71d2011-07-07 08:21:25 +00002174}
2175
leozwang@webrtc.org813e4b02012-03-01 18:34:25 +00002176int Channel::StartRecordingPlayout(const char* fileName,
niklase@google.com470e71d2011-07-07 08:21:25 +00002177 const CodecInst* codecInst)
2178{
2179 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId,_channelId),
2180 "Channel::StartRecordingPlayout(fileName=%s)", fileName);
2181
2182 if (_outputFileRecording)
2183 {
2184 WEBRTC_TRACE(kTraceWarning, kTraceVoice, VoEId(_instanceId,-1),
2185 "StartRecordingPlayout() is already recording");
2186 return 0;
2187 }
2188
2189 FileFormats format;
pbos@webrtc.org6141e132013-04-09 10:09:10 +00002190 const uint32_t notificationTime(0); // Not supported in VoE
niklase@google.com470e71d2011-07-07 08:21:25 +00002191 CodecInst dummyCodec={100,"L16",16000,320,1,320000};
2192
niklas.enbom@webrtc.org40197d72012-03-26 08:45:47 +00002193 if ((codecInst != NULL) &&
2194 ((codecInst->channels < 1) || (codecInst->channels > 2)))
niklase@google.com470e71d2011-07-07 08:21:25 +00002195 {
2196 _engineStatisticsPtr->SetLastError(
2197 VE_BAD_ARGUMENT, kTraceError,
2198 "StartRecordingPlayout() invalid compression");
2199 return(-1);
2200 }
2201 if(codecInst == NULL)
2202 {
2203 format = kFileFormatPcm16kHzFile;
2204 codecInst=&dummyCodec;
2205 }
2206 else if((STR_CASE_CMP(codecInst->plname,"L16") == 0) ||
2207 (STR_CASE_CMP(codecInst->plname,"PCMU") == 0) ||
2208 (STR_CASE_CMP(codecInst->plname,"PCMA") == 0))
2209 {
2210 format = kFileFormatWavFile;
2211 }
2212 else
2213 {
2214 format = kFileFormatCompressedFile;
2215 }
2216
mflodman@webrtc.org9a065d12012-03-07 08:12:21 +00002217 CriticalSectionScoped cs(&_fileCritSect);
niklase@google.com470e71d2011-07-07 08:21:25 +00002218
2219 // Destroy the old instance
2220 if (_outputFileRecorderPtr)
2221 {
2222 _outputFileRecorderPtr->RegisterModuleFileCallback(NULL);
2223 FileRecorder::DestroyFileRecorder(_outputFileRecorderPtr);
2224 _outputFileRecorderPtr = NULL;
2225 }
2226
2227 _outputFileRecorderPtr = FileRecorder::CreateFileRecorder(
2228 _outputFileRecorderId, (const FileFormats)format);
2229 if (_outputFileRecorderPtr == NULL)
2230 {
2231 _engineStatisticsPtr->SetLastError(
2232 VE_INVALID_ARGUMENT, kTraceError,
2233 "StartRecordingPlayout() fileRecorder format isnot correct");
2234 return -1;
2235 }
2236
2237 if (_outputFileRecorderPtr->StartRecordingAudioFile(
2238 fileName, (const CodecInst&)*codecInst, notificationTime) != 0)
2239 {
2240 _engineStatisticsPtr->SetLastError(
2241 VE_BAD_FILE, kTraceError,
2242 "StartRecordingAudioFile() failed to start file recording");
2243 _outputFileRecorderPtr->StopRecording();
2244 FileRecorder::DestroyFileRecorder(_outputFileRecorderPtr);
2245 _outputFileRecorderPtr = NULL;
2246 return -1;
2247 }
2248 _outputFileRecorderPtr->RegisterModuleFileCallback(this);
2249 _outputFileRecording = true;
2250
2251 return 0;
2252}
2253
2254int Channel::StartRecordingPlayout(OutStream* stream,
2255 const CodecInst* codecInst)
2256{
2257 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId,_channelId),
2258 "Channel::StartRecordingPlayout()");
2259
2260 if (_outputFileRecording)
2261 {
2262 WEBRTC_TRACE(kTraceWarning, kTraceVoice, VoEId(_instanceId,-1),
2263 "StartRecordingPlayout() is already recording");
2264 return 0;
2265 }
2266
2267 FileFormats format;
pbos@webrtc.org6141e132013-04-09 10:09:10 +00002268 const uint32_t notificationTime(0); // Not supported in VoE
niklase@google.com470e71d2011-07-07 08:21:25 +00002269 CodecInst dummyCodec={100,"L16",16000,320,1,320000};
2270
2271 if (codecInst != NULL && codecInst->channels != 1)
2272 {
2273 _engineStatisticsPtr->SetLastError(
2274 VE_BAD_ARGUMENT, kTraceError,
2275 "StartRecordingPlayout() invalid compression");
2276 return(-1);
2277 }
2278 if(codecInst == NULL)
2279 {
2280 format = kFileFormatPcm16kHzFile;
2281 codecInst=&dummyCodec;
2282 }
2283 else if((STR_CASE_CMP(codecInst->plname,"L16") == 0) ||
2284 (STR_CASE_CMP(codecInst->plname,"PCMU") == 0) ||
2285 (STR_CASE_CMP(codecInst->plname,"PCMA") == 0))
2286 {
2287 format = kFileFormatWavFile;
2288 }
2289 else
2290 {
2291 format = kFileFormatCompressedFile;
2292 }
2293
mflodman@webrtc.org9a065d12012-03-07 08:12:21 +00002294 CriticalSectionScoped cs(&_fileCritSect);
niklase@google.com470e71d2011-07-07 08:21:25 +00002295
2296 // Destroy the old instance
2297 if (_outputFileRecorderPtr)
2298 {
2299 _outputFileRecorderPtr->RegisterModuleFileCallback(NULL);
2300 FileRecorder::DestroyFileRecorder(_outputFileRecorderPtr);
2301 _outputFileRecorderPtr = NULL;
2302 }
2303
2304 _outputFileRecorderPtr = FileRecorder::CreateFileRecorder(
2305 _outputFileRecorderId, (const FileFormats)format);
2306 if (_outputFileRecorderPtr == NULL)
2307 {
2308 _engineStatisticsPtr->SetLastError(
2309 VE_INVALID_ARGUMENT, kTraceError,
2310 "StartRecordingPlayout() fileRecorder format isnot correct");
2311 return -1;
2312 }
2313
2314 if (_outputFileRecorderPtr->StartRecordingAudioFile(*stream, *codecInst,
2315 notificationTime) != 0)
2316 {
2317 _engineStatisticsPtr->SetLastError(VE_BAD_FILE, kTraceError,
2318 "StartRecordingPlayout() failed to "
2319 "start file recording");
2320 _outputFileRecorderPtr->StopRecording();
2321 FileRecorder::DestroyFileRecorder(_outputFileRecorderPtr);
2322 _outputFileRecorderPtr = NULL;
2323 return -1;
2324 }
andrew@webrtc.orgae1a58b2013-01-22 04:44:30 +00002325
niklase@google.com470e71d2011-07-07 08:21:25 +00002326 _outputFileRecorderPtr->RegisterModuleFileCallback(this);
2327 _outputFileRecording = true;
2328
2329 return 0;
2330}
2331
2332int Channel::StopRecordingPlayout()
2333{
2334 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId,-1),
2335 "Channel::StopRecordingPlayout()");
2336
2337 if (!_outputFileRecording)
2338 {
2339 WEBRTC_TRACE(kTraceError, kTraceVoice, VoEId(_instanceId,-1),
2340 "StopRecordingPlayout() isnot recording");
2341 return -1;
2342 }
2343
2344
mflodman@webrtc.org9a065d12012-03-07 08:12:21 +00002345 CriticalSectionScoped cs(&_fileCritSect);
niklase@google.com470e71d2011-07-07 08:21:25 +00002346
2347 if (_outputFileRecorderPtr->StopRecording() != 0)
2348 {
2349 _engineStatisticsPtr->SetLastError(
2350 VE_STOP_RECORDING_FAILED, kTraceError,
2351 "StopRecording() could not stop recording");
2352 return(-1);
2353 }
2354 _outputFileRecorderPtr->RegisterModuleFileCallback(NULL);
2355 FileRecorder::DestroyFileRecorder(_outputFileRecorderPtr);
2356 _outputFileRecorderPtr = NULL;
2357 _outputFileRecording = false;
2358
2359 return 0;
2360}
2361
2362void
2363Channel::SetMixWithMicStatus(bool mix)
2364{
henrika@webrtc.org944cbeb2014-03-18 10:32:33 +00002365 CriticalSectionScoped cs(&_fileCritSect);
niklase@google.com470e71d2011-07-07 08:21:25 +00002366 _mixFileWithMicrophone=mix;
2367}
2368
2369int
pbos@webrtc.org6141e132013-04-09 10:09:10 +00002370Channel::GetSpeechOutputLevel(uint32_t& level) const
niklase@google.com470e71d2011-07-07 08:21:25 +00002371{
pbos@webrtc.org6141e132013-04-09 10:09:10 +00002372 int8_t currentLevel = _outputAudioLevel.Level();
2373 level = static_cast<int32_t> (currentLevel);
niklase@google.com470e71d2011-07-07 08:21:25 +00002374 WEBRTC_TRACE(kTraceStateInfo, kTraceVoice,
2375 VoEId(_instanceId,_channelId),
2376 "GetSpeechOutputLevel() => level=%u", level);
2377 return 0;
2378}
2379
2380int
pbos@webrtc.org6141e132013-04-09 10:09:10 +00002381Channel::GetSpeechOutputLevelFullRange(uint32_t& level) const
niklase@google.com470e71d2011-07-07 08:21:25 +00002382{
pbos@webrtc.org6141e132013-04-09 10:09:10 +00002383 int16_t currentLevel = _outputAudioLevel.LevelFullRange();
2384 level = static_cast<int32_t> (currentLevel);
niklase@google.com470e71d2011-07-07 08:21:25 +00002385 WEBRTC_TRACE(kTraceStateInfo, kTraceVoice,
2386 VoEId(_instanceId,_channelId),
2387 "GetSpeechOutputLevelFullRange() => level=%u", level);
2388 return 0;
2389}
2390
2391int
2392Channel::SetMute(bool enable)
2393{
wu@webrtc.org63420662013-10-17 18:28:55 +00002394 CriticalSectionScoped cs(&volume_settings_critsect_);
niklase@google.com470e71d2011-07-07 08:21:25 +00002395 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId,_channelId),
2396 "Channel::SetMute(enable=%d)", enable);
2397 _mute = enable;
2398 return 0;
2399}
2400
2401bool
2402Channel::Mute() const
2403{
wu@webrtc.org63420662013-10-17 18:28:55 +00002404 CriticalSectionScoped cs(&volume_settings_critsect_);
niklase@google.com470e71d2011-07-07 08:21:25 +00002405 return _mute;
2406}
2407
2408int
2409Channel::SetOutputVolumePan(float left, float right)
2410{
wu@webrtc.org63420662013-10-17 18:28:55 +00002411 CriticalSectionScoped cs(&volume_settings_critsect_);
niklase@google.com470e71d2011-07-07 08:21:25 +00002412 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId,_channelId),
2413 "Channel::SetOutputVolumePan()");
2414 _panLeft = left;
2415 _panRight = right;
2416 return 0;
2417}
2418
2419int
2420Channel::GetOutputVolumePan(float& left, float& right) const
2421{
wu@webrtc.org63420662013-10-17 18:28:55 +00002422 CriticalSectionScoped cs(&volume_settings_critsect_);
niklase@google.com470e71d2011-07-07 08:21:25 +00002423 left = _panLeft;
2424 right = _panRight;
2425 WEBRTC_TRACE(kTraceStateInfo, kTraceVoice,
2426 VoEId(_instanceId,_channelId),
2427 "GetOutputVolumePan() => left=%3.2f, right=%3.2f", left, right);
2428 return 0;
2429}
2430
2431int
2432Channel::SetChannelOutputVolumeScaling(float scaling)
2433{
wu@webrtc.org63420662013-10-17 18:28:55 +00002434 CriticalSectionScoped cs(&volume_settings_critsect_);
niklase@google.com470e71d2011-07-07 08:21:25 +00002435 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId,_channelId),
2436 "Channel::SetChannelOutputVolumeScaling()");
2437 _outputGain = scaling;
2438 return 0;
2439}
2440
2441int
2442Channel::GetChannelOutputVolumeScaling(float& scaling) const
2443{
wu@webrtc.org63420662013-10-17 18:28:55 +00002444 CriticalSectionScoped cs(&volume_settings_critsect_);
niklase@google.com470e71d2011-07-07 08:21:25 +00002445 scaling = _outputGain;
2446 WEBRTC_TRACE(kTraceStateInfo, kTraceVoice,
2447 VoEId(_instanceId,_channelId),
2448 "GetChannelOutputVolumeScaling() => scaling=%3.2f", scaling);
2449 return 0;
2450}
2451
niklase@google.com470e71d2011-07-07 08:21:25 +00002452int Channel::SendTelephoneEventOutband(unsigned char eventCode,
wu@webrtc.org822fbd82013-08-15 23:38:54 +00002453 int lengthMs, int attenuationDb,
2454 bool playDtmfEvent)
niklase@google.com470e71d2011-07-07 08:21:25 +00002455{
2456 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId, _channelId),
2457 "Channel::SendTelephoneEventOutband(..., playDtmfEvent=%d)",
2458 playDtmfEvent);
2459
2460 _playOutbandDtmfEvent = playDtmfEvent;
2461
pwestin@webrtc.org2853dde2012-05-11 11:08:54 +00002462 if (_rtpRtcpModule->SendTelephoneEventOutband(eventCode, lengthMs,
niklase@google.com470e71d2011-07-07 08:21:25 +00002463 attenuationDb) != 0)
2464 {
2465 _engineStatisticsPtr->SetLastError(
2466 VE_SEND_DTMF_FAILED,
2467 kTraceWarning,
2468 "SendTelephoneEventOutband() failed to send event");
2469 return -1;
2470 }
2471 return 0;
2472}
2473
2474int Channel::SendTelephoneEventInband(unsigned char eventCode,
2475 int lengthMs,
2476 int attenuationDb,
2477 bool playDtmfEvent)
2478{
2479 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId, _channelId),
2480 "Channel::SendTelephoneEventInband(..., playDtmfEvent=%d)",
2481 playDtmfEvent);
2482
2483 _playInbandDtmfEvent = playDtmfEvent;
2484 _inbandDtmfQueue.AddDtmf(eventCode, lengthMs, attenuationDb);
2485
2486 return 0;
2487}
2488
2489int
niklase@google.com470e71d2011-07-07 08:21:25 +00002490Channel::SetSendTelephoneEventPayloadType(unsigned char type)
2491{
2492 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId,_channelId),
2493 "Channel::SetSendTelephoneEventPayloadType()");
andrew@webrtc.orgf81f9f82011-08-19 22:56:22 +00002494 if (type > 127)
niklase@google.com470e71d2011-07-07 08:21:25 +00002495 {
2496 _engineStatisticsPtr->SetLastError(
2497 VE_INVALID_ARGUMENT, kTraceError,
2498 "SetSendTelephoneEventPayloadType() invalid type");
2499 return -1;
2500 }
pbos@webrtc.org5b10d8f2013-07-11 15:50:07 +00002501 CodecInst codec = {};
pwestin@webrtc.org1da1ce02011-10-13 15:19:55 +00002502 codec.plfreq = 8000;
2503 codec.pltype = type;
2504 memcpy(codec.plname, "telephone-event", 16);
pwestin@webrtc.org2853dde2012-05-11 11:08:54 +00002505 if (_rtpRtcpModule->RegisterSendPayload(codec) != 0)
niklase@google.com470e71d2011-07-07 08:21:25 +00002506 {
henrika@webrtc.org4392d5f2013-04-17 07:34:25 +00002507 _rtpRtcpModule->DeRegisterSendPayload(codec.pltype);
2508 if (_rtpRtcpModule->RegisterSendPayload(codec) != 0) {
2509 _engineStatisticsPtr->SetLastError(
2510 VE_RTP_RTCP_MODULE_ERROR, kTraceError,
2511 "SetSendTelephoneEventPayloadType() failed to register send"
2512 "payload type");
2513 return -1;
2514 }
niklase@google.com470e71d2011-07-07 08:21:25 +00002515 }
2516 _sendTelephoneEventPayloadType = type;
2517 return 0;
2518}
2519
2520int
2521Channel::GetSendTelephoneEventPayloadType(unsigned char& type)
2522{
2523 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId,_channelId),
2524 "Channel::GetSendTelephoneEventPayloadType()");
2525 type = _sendTelephoneEventPayloadType;
2526 WEBRTC_TRACE(kTraceStateInfo, kTraceVoice,
2527 VoEId(_instanceId,_channelId),
2528 "GetSendTelephoneEventPayloadType() => type=%u", type);
2529 return 0;
2530}
2531
niklase@google.com470e71d2011-07-07 08:21:25 +00002532int
2533Channel::UpdateRxVadDetection(AudioFrame& audioFrame)
2534{
2535 WEBRTC_TRACE(kTraceStream, kTraceVoice, VoEId(_instanceId,_channelId),
2536 "Channel::UpdateRxVadDetection()");
2537
2538 int vadDecision = 1;
2539
andrew@webrtc.org63a50982012-05-02 23:56:37 +00002540 vadDecision = (audioFrame.vad_activity_ == AudioFrame::kVadActive)? 1 : 0;
niklase@google.com470e71d2011-07-07 08:21:25 +00002541
2542 if ((vadDecision != _oldVadDecision) && _rxVadObserverPtr)
2543 {
2544 OnRxVadDetected(vadDecision);
2545 _oldVadDecision = vadDecision;
2546 }
2547
2548 WEBRTC_TRACE(kTraceStream, kTraceVoice, VoEId(_instanceId,_channelId),
2549 "Channel::UpdateRxVadDetection() => vadDecision=%d",
2550 vadDecision);
2551 return 0;
2552}
2553
2554int
2555Channel::RegisterRxVadObserver(VoERxVadCallback &observer)
2556{
2557 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId,_channelId),
2558 "Channel::RegisterRxVadObserver()");
mflodman@webrtc.org9a065d12012-03-07 08:12:21 +00002559 CriticalSectionScoped cs(&_callbackCritSect);
niklase@google.com470e71d2011-07-07 08:21:25 +00002560
2561 if (_rxVadObserverPtr)
2562 {
2563 _engineStatisticsPtr->SetLastError(
2564 VE_INVALID_OPERATION, kTraceError,
2565 "RegisterRxVadObserver() observer already enabled");
2566 return -1;
2567 }
niklase@google.com470e71d2011-07-07 08:21:25 +00002568 _rxVadObserverPtr = &observer;
2569 _RxVadDetection = true;
2570 return 0;
2571}
2572
2573int
2574Channel::DeRegisterRxVadObserver()
2575{
2576 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId,_channelId),
2577 "Channel::DeRegisterRxVadObserver()");
mflodman@webrtc.org9a065d12012-03-07 08:12:21 +00002578 CriticalSectionScoped cs(&_callbackCritSect);
niklase@google.com470e71d2011-07-07 08:21:25 +00002579
2580 if (!_rxVadObserverPtr)
2581 {
2582 _engineStatisticsPtr->SetLastError(
2583 VE_INVALID_OPERATION, kTraceWarning,
2584 "DeRegisterRxVadObserver() observer already disabled");
2585 return 0;
2586 }
2587 _rxVadObserverPtr = NULL;
2588 _RxVadDetection = false;
2589 return 0;
2590}
2591
2592int
2593Channel::VoiceActivityIndicator(int &activity)
2594{
2595 activity = _sendFrameType;
2596
2597 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId,_channelId),
andrew@webrtc.org6c264cc2013-10-04 17:54:09 +00002598 "Channel::VoiceActivityIndicator(indicator=%d)", activity);
niklase@google.com470e71d2011-07-07 08:21:25 +00002599 return 0;
2600}
2601
2602#ifdef WEBRTC_VOICE_ENGINE_AGC
2603
2604int
pbos@webrtc.org92135212013-05-14 08:31:39 +00002605Channel::SetRxAgcStatus(bool enable, AgcModes mode)
niklase@google.com470e71d2011-07-07 08:21:25 +00002606{
2607 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId,_channelId),
2608 "Channel::SetRxAgcStatus(enable=%d, mode=%d)",
2609 (int)enable, (int)mode);
2610
andrew@webrtc.org6c264cc2013-10-04 17:54:09 +00002611 GainControl::Mode agcMode = kDefaultRxAgcMode;
niklase@google.com470e71d2011-07-07 08:21:25 +00002612 switch (mode)
2613 {
2614 case kAgcDefault:
niklase@google.com470e71d2011-07-07 08:21:25 +00002615 break;
2616 case kAgcUnchanged:
andrew@webrtc.orgf3930e92013-09-18 22:37:32 +00002617 agcMode = rx_audioproc_->gain_control()->mode();
niklase@google.com470e71d2011-07-07 08:21:25 +00002618 break;
2619 case kAgcFixedDigital:
2620 agcMode = GainControl::kFixedDigital;
2621 break;
2622 case kAgcAdaptiveDigital:
2623 agcMode =GainControl::kAdaptiveDigital;
2624 break;
2625 default:
2626 _engineStatisticsPtr->SetLastError(
2627 VE_INVALID_ARGUMENT, kTraceError,
2628 "SetRxAgcStatus() invalid Agc mode");
2629 return -1;
2630 }
2631
andrew@webrtc.orgf3930e92013-09-18 22:37:32 +00002632 if (rx_audioproc_->gain_control()->set_mode(agcMode) != 0)
niklase@google.com470e71d2011-07-07 08:21:25 +00002633 {
2634 _engineStatisticsPtr->SetLastError(
2635 VE_APM_ERROR, kTraceError,
2636 "SetRxAgcStatus() failed to set Agc mode");
2637 return -1;
2638 }
andrew@webrtc.orgf3930e92013-09-18 22:37:32 +00002639 if (rx_audioproc_->gain_control()->Enable(enable) != 0)
niklase@google.com470e71d2011-07-07 08:21:25 +00002640 {
2641 _engineStatisticsPtr->SetLastError(
2642 VE_APM_ERROR, kTraceError,
2643 "SetRxAgcStatus() failed to set Agc state");
2644 return -1;
2645 }
2646
2647 _rxAgcIsEnabled = enable;
henrika@webrtc.org944cbeb2014-03-18 10:32:33 +00002648 channel_state_.SetRxApmIsEnabled(_rxAgcIsEnabled || _rxNsIsEnabled);
niklase@google.com470e71d2011-07-07 08:21:25 +00002649
2650 return 0;
2651}
2652
2653int
2654Channel::GetRxAgcStatus(bool& enabled, AgcModes& mode)
2655{
2656 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId,_channelId),
2657 "Channel::GetRxAgcStatus(enable=?, mode=?)");
2658
andrew@webrtc.orgf3930e92013-09-18 22:37:32 +00002659 bool enable = rx_audioproc_->gain_control()->is_enabled();
niklase@google.com470e71d2011-07-07 08:21:25 +00002660 GainControl::Mode agcMode =
andrew@webrtc.orgf3930e92013-09-18 22:37:32 +00002661 rx_audioproc_->gain_control()->mode();
niklase@google.com470e71d2011-07-07 08:21:25 +00002662
2663 enabled = enable;
2664
2665 switch (agcMode)
2666 {
2667 case GainControl::kFixedDigital:
2668 mode = kAgcFixedDigital;
2669 break;
2670 case GainControl::kAdaptiveDigital:
2671 mode = kAgcAdaptiveDigital;
2672 break;
2673 default:
2674 _engineStatisticsPtr->SetLastError(
2675 VE_APM_ERROR, kTraceError,
2676 "GetRxAgcStatus() invalid Agc mode");
2677 return -1;
2678 }
2679
2680 return 0;
2681}
2682
2683int
pbos@webrtc.org92135212013-05-14 08:31:39 +00002684Channel::SetRxAgcConfig(AgcConfig config)
niklase@google.com470e71d2011-07-07 08:21:25 +00002685{
2686 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId,_channelId),
2687 "Channel::SetRxAgcConfig()");
2688
andrew@webrtc.orgf3930e92013-09-18 22:37:32 +00002689 if (rx_audioproc_->gain_control()->set_target_level_dbfs(
niklase@google.com470e71d2011-07-07 08:21:25 +00002690 config.targetLeveldBOv) != 0)
2691 {
2692 _engineStatisticsPtr->SetLastError(
2693 VE_APM_ERROR, kTraceError,
2694 "SetRxAgcConfig() failed to set target peak |level|"
2695 "(or envelope) of the Agc");
2696 return -1;
2697 }
andrew@webrtc.orgf3930e92013-09-18 22:37:32 +00002698 if (rx_audioproc_->gain_control()->set_compression_gain_db(
niklase@google.com470e71d2011-07-07 08:21:25 +00002699 config.digitalCompressionGaindB) != 0)
2700 {
2701 _engineStatisticsPtr->SetLastError(
2702 VE_APM_ERROR, kTraceError,
2703 "SetRxAgcConfig() failed to set the range in |gain| the"
2704 " digital compression stage may apply");
2705 return -1;
2706 }
andrew@webrtc.orgf3930e92013-09-18 22:37:32 +00002707 if (rx_audioproc_->gain_control()->enable_limiter(
niklase@google.com470e71d2011-07-07 08:21:25 +00002708 config.limiterEnable) != 0)
2709 {
2710 _engineStatisticsPtr->SetLastError(
2711 VE_APM_ERROR, kTraceError,
2712 "SetRxAgcConfig() failed to set hard limiter to the signal");
2713 return -1;
2714 }
2715
2716 return 0;
2717}
2718
2719int
2720Channel::GetRxAgcConfig(AgcConfig& config)
2721{
2722 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId,_channelId),
2723 "Channel::GetRxAgcConfig(config=%?)");
2724
2725 config.targetLeveldBOv =
andrew@webrtc.orgf3930e92013-09-18 22:37:32 +00002726 rx_audioproc_->gain_control()->target_level_dbfs();
niklase@google.com470e71d2011-07-07 08:21:25 +00002727 config.digitalCompressionGaindB =
andrew@webrtc.orgf3930e92013-09-18 22:37:32 +00002728 rx_audioproc_->gain_control()->compression_gain_db();
niklase@google.com470e71d2011-07-07 08:21:25 +00002729 config.limiterEnable =
andrew@webrtc.orgf3930e92013-09-18 22:37:32 +00002730 rx_audioproc_->gain_control()->is_limiter_enabled();
niklase@google.com470e71d2011-07-07 08:21:25 +00002731
2732 WEBRTC_TRACE(kTraceStateInfo, kTraceVoice,
2733 VoEId(_instanceId,_channelId), "GetRxAgcConfig() => "
2734 "targetLeveldBOv=%u, digitalCompressionGaindB=%u,"
2735 " limiterEnable=%d",
2736 config.targetLeveldBOv,
2737 config.digitalCompressionGaindB,
2738 config.limiterEnable);
2739
2740 return 0;
2741}
2742
2743#endif // #ifdef WEBRTC_VOICE_ENGINE_AGC
2744
2745#ifdef WEBRTC_VOICE_ENGINE_NR
2746
2747int
pbos@webrtc.org92135212013-05-14 08:31:39 +00002748Channel::SetRxNsStatus(bool enable, NsModes mode)
niklase@google.com470e71d2011-07-07 08:21:25 +00002749{
2750 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId,_channelId),
2751 "Channel::SetRxNsStatus(enable=%d, mode=%d)",
2752 (int)enable, (int)mode);
2753
andrew@webrtc.org6c264cc2013-10-04 17:54:09 +00002754 NoiseSuppression::Level nsLevel = kDefaultNsMode;
niklase@google.com470e71d2011-07-07 08:21:25 +00002755 switch (mode)
2756 {
2757
2758 case kNsDefault:
niklase@google.com470e71d2011-07-07 08:21:25 +00002759 break;
2760 case kNsUnchanged:
andrew@webrtc.orgf3930e92013-09-18 22:37:32 +00002761 nsLevel = rx_audioproc_->noise_suppression()->level();
niklase@google.com470e71d2011-07-07 08:21:25 +00002762 break;
2763 case kNsConference:
2764 nsLevel = NoiseSuppression::kHigh;
2765 break;
2766 case kNsLowSuppression:
2767 nsLevel = NoiseSuppression::kLow;
2768 break;
2769 case kNsModerateSuppression:
2770 nsLevel = NoiseSuppression::kModerate;
2771 break;
2772 case kNsHighSuppression:
2773 nsLevel = NoiseSuppression::kHigh;
2774 break;
2775 case kNsVeryHighSuppression:
2776 nsLevel = NoiseSuppression::kVeryHigh;
2777 break;
niklase@google.com470e71d2011-07-07 08:21:25 +00002778 }
2779
andrew@webrtc.orgf3930e92013-09-18 22:37:32 +00002780 if (rx_audioproc_->noise_suppression()->set_level(nsLevel)
niklase@google.com470e71d2011-07-07 08:21:25 +00002781 != 0)
2782 {
2783 _engineStatisticsPtr->SetLastError(
2784 VE_APM_ERROR, kTraceError,
andrew@webrtc.org6c264cc2013-10-04 17:54:09 +00002785 "SetRxNsStatus() failed to set NS level");
niklase@google.com470e71d2011-07-07 08:21:25 +00002786 return -1;
2787 }
andrew@webrtc.orgf3930e92013-09-18 22:37:32 +00002788 if (rx_audioproc_->noise_suppression()->Enable(enable) != 0)
niklase@google.com470e71d2011-07-07 08:21:25 +00002789 {
2790 _engineStatisticsPtr->SetLastError(
2791 VE_APM_ERROR, kTraceError,
andrew@webrtc.org6c264cc2013-10-04 17:54:09 +00002792 "SetRxNsStatus() failed to set NS state");
niklase@google.com470e71d2011-07-07 08:21:25 +00002793 return -1;
2794 }
2795
2796 _rxNsIsEnabled = enable;
henrika@webrtc.org944cbeb2014-03-18 10:32:33 +00002797 channel_state_.SetRxApmIsEnabled(_rxAgcIsEnabled || _rxNsIsEnabled);
niklase@google.com470e71d2011-07-07 08:21:25 +00002798
2799 return 0;
2800}
2801
2802int
2803Channel::GetRxNsStatus(bool& enabled, NsModes& mode)
2804{
2805 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId,_channelId),
2806 "Channel::GetRxNsStatus(enable=?, mode=?)");
2807
2808 bool enable =
andrew@webrtc.orgf3930e92013-09-18 22:37:32 +00002809 rx_audioproc_->noise_suppression()->is_enabled();
niklase@google.com470e71d2011-07-07 08:21:25 +00002810 NoiseSuppression::Level ncLevel =
andrew@webrtc.orgf3930e92013-09-18 22:37:32 +00002811 rx_audioproc_->noise_suppression()->level();
niklase@google.com470e71d2011-07-07 08:21:25 +00002812
2813 enabled = enable;
2814
2815 switch (ncLevel)
2816 {
2817 case NoiseSuppression::kLow:
2818 mode = kNsLowSuppression;
2819 break;
2820 case NoiseSuppression::kModerate:
2821 mode = kNsModerateSuppression;
2822 break;
2823 case NoiseSuppression::kHigh:
2824 mode = kNsHighSuppression;
2825 break;
2826 case NoiseSuppression::kVeryHigh:
2827 mode = kNsVeryHighSuppression;
2828 break;
niklase@google.com470e71d2011-07-07 08:21:25 +00002829 }
2830
2831 WEBRTC_TRACE(kTraceStateInfo, kTraceVoice,
2832 VoEId(_instanceId,_channelId),
2833 "GetRxNsStatus() => enabled=%d, mode=%d", enabled, mode);
2834 return 0;
2835}
2836
2837#endif // #ifdef WEBRTC_VOICE_ENGINE_NR
2838
2839int
niklase@google.com470e71d2011-07-07 08:21:25 +00002840Channel::SetLocalSSRC(unsigned int ssrc)
2841{
2842 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId, _channelId),
2843 "Channel::SetLocalSSRC()");
henrika@webrtc.org944cbeb2014-03-18 10:32:33 +00002844 if (channel_state_.Get().sending)
niklase@google.com470e71d2011-07-07 08:21:25 +00002845 {
2846 _engineStatisticsPtr->SetLastError(
2847 VE_ALREADY_SENDING, kTraceError,
2848 "SetLocalSSRC() already sending");
2849 return -1;
2850 }
stefan@webrtc.orgef927552014-06-05 08:25:29 +00002851 _rtpRtcpModule->SetSSRC(ssrc);
niklase@google.com470e71d2011-07-07 08:21:25 +00002852 return 0;
2853}
2854
2855int
2856Channel::GetLocalSSRC(unsigned int& ssrc)
2857{
pwestin@webrtc.org2853dde2012-05-11 11:08:54 +00002858 ssrc = _rtpRtcpModule->SSRC();
niklase@google.com470e71d2011-07-07 08:21:25 +00002859 WEBRTC_TRACE(kTraceStateInfo, kTraceVoice,
2860 VoEId(_instanceId,_channelId),
2861 "GetLocalSSRC() => ssrc=%lu", ssrc);
2862 return 0;
2863}
2864
2865int
2866Channel::GetRemoteSSRC(unsigned int& ssrc)
2867{
wu@webrtc.org822fbd82013-08-15 23:38:54 +00002868 ssrc = rtp_receiver_->SSRC();
niklase@google.com470e71d2011-07-07 08:21:25 +00002869 WEBRTC_TRACE(kTraceStateInfo, kTraceVoice,
2870 VoEId(_instanceId,_channelId),
2871 "GetRemoteSSRC() => ssrc=%lu", ssrc);
2872 return 0;
2873}
2874
wu@webrtc.orgebdb0e32014-03-06 23:49:08 +00002875int Channel::SetSendAudioLevelIndicationStatus(bool enable, unsigned char id) {
andrew@webrtc.orgf3930e92013-09-18 22:37:32 +00002876 _includeAudioLevelIndication = enable;
wu@webrtc.orgebdb0e32014-03-06 23:49:08 +00002877 return SetSendRtpHeaderExtension(enable, kRtpExtensionAudioLevel, id);
niklase@google.com470e71d2011-07-07 08:21:25 +00002878}
andrew@webrtc.orgf3930e92013-09-18 22:37:32 +00002879
wu@webrtc.org93fd25c2014-04-24 20:33:08 +00002880int Channel::SetReceiveAudioLevelIndicationStatus(bool enable,
2881 unsigned char id) {
2882 rtp_header_parser_->DeregisterRtpHeaderExtension(
2883 kRtpExtensionAudioLevel);
2884 if (enable && !rtp_header_parser_->RegisterRtpHeaderExtension(
2885 kRtpExtensionAudioLevel, id)) {
2886 return -1;
2887 }
2888 return 0;
2889}
2890
wu@webrtc.orgebdb0e32014-03-06 23:49:08 +00002891int Channel::SetSendAbsoluteSenderTimeStatus(bool enable, unsigned char id) {
2892 return SetSendRtpHeaderExtension(enable, kRtpExtensionAbsoluteSendTime, id);
2893}
2894
2895int Channel::SetReceiveAbsoluteSenderTimeStatus(bool enable, unsigned char id) {
2896 rtp_header_parser_->DeregisterRtpHeaderExtension(
2897 kRtpExtensionAbsoluteSendTime);
solenberg@webrtc.orgb1f50102014-03-24 10:38:25 +00002898 if (enable && !rtp_header_parser_->RegisterRtpHeaderExtension(
2899 kRtpExtensionAbsoluteSendTime, id)) {
2900 return -1;
wu@webrtc.orgebdb0e32014-03-06 23:49:08 +00002901 }
2902 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +00002903}
2904
pbos@webrtc.orgd16e8392014-12-19 13:49:55 +00002905void Channel::SetRTCPStatus(bool enable) {
2906 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId, _channelId),
2907 "Channel::SetRTCPStatus()");
2908 _rtpRtcpModule->SetRTCPStatus(enable ? kRtcpCompound : kRtcpOff);
niklase@google.com470e71d2011-07-07 08:21:25 +00002909}
2910
2911int
2912Channel::GetRTCPStatus(bool& enabled)
2913{
pwestin@webrtc.org2853dde2012-05-11 11:08:54 +00002914 RTCPMethod method = _rtpRtcpModule->RTCP();
niklase@google.com470e71d2011-07-07 08:21:25 +00002915 enabled = (method != kRtcpOff);
2916 WEBRTC_TRACE(kTraceStateInfo, kTraceVoice,
2917 VoEId(_instanceId,_channelId),
2918 "GetRTCPStatus() => enabled=%d", enabled);
2919 return 0;
2920}
2921
2922int
2923Channel::SetRTCP_CNAME(const char cName[256])
2924{
2925 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId, _channelId),
2926 "Channel::SetRTCP_CNAME()");
pwestin@webrtc.org2853dde2012-05-11 11:08:54 +00002927 if (_rtpRtcpModule->SetCNAME(cName) != 0)
niklase@google.com470e71d2011-07-07 08:21:25 +00002928 {
2929 _engineStatisticsPtr->SetLastError(
2930 VE_RTP_RTCP_MODULE_ERROR, kTraceError,
2931 "SetRTCP_CNAME() failed to set RTCP CNAME");
2932 return -1;
2933 }
2934 return 0;
2935}
2936
2937int
niklase@google.com470e71d2011-07-07 08:21:25 +00002938Channel::GetRemoteRTCP_CNAME(char cName[256])
2939{
2940 if (cName == NULL)
2941 {
2942 _engineStatisticsPtr->SetLastError(
2943 VE_INVALID_ARGUMENT, kTraceError,
2944 "GetRemoteRTCP_CNAME() invalid CNAME input buffer");
2945 return -1;
2946 }
leozwang@webrtc.org813e4b02012-03-01 18:34:25 +00002947 char cname[RTCP_CNAME_SIZE];
wu@webrtc.org822fbd82013-08-15 23:38:54 +00002948 const uint32_t remoteSSRC = rtp_receiver_->SSRC();
pwestin@webrtc.org2853dde2012-05-11 11:08:54 +00002949 if (_rtpRtcpModule->RemoteCNAME(remoteSSRC, cname) != 0)
niklase@google.com470e71d2011-07-07 08:21:25 +00002950 {
2951 _engineStatisticsPtr->SetLastError(
2952 VE_CANNOT_RETRIEVE_CNAME, kTraceError,
2953 "GetRemoteRTCP_CNAME() failed to retrieve remote RTCP CNAME");
2954 return -1;
2955 }
2956 strcpy(cName, cname);
2957 WEBRTC_TRACE(kTraceStateInfo, kTraceVoice,
2958 VoEId(_instanceId, _channelId),
2959 "GetRemoteRTCP_CNAME() => cName=%s", cName);
2960 return 0;
2961}
2962
2963int
2964Channel::GetRemoteRTCPData(
2965 unsigned int& NTPHigh,
2966 unsigned int& NTPLow,
2967 unsigned int& timestamp,
2968 unsigned int& playoutTimestamp,
2969 unsigned int* jitter,
2970 unsigned short* fractionLost)
2971{
2972 // --- Information from sender info in received Sender Reports
2973
2974 RTCPSenderInfo senderInfo;
pwestin@webrtc.org2853dde2012-05-11 11:08:54 +00002975 if (_rtpRtcpModule->RemoteRTCPStat(&senderInfo) != 0)
niklase@google.com470e71d2011-07-07 08:21:25 +00002976 {
2977 _engineStatisticsPtr->SetLastError(
2978 VE_RTP_RTCP_MODULE_ERROR, kTraceError,
wu@webrtc.orgfcd12b32011-09-15 20:49:50 +00002979 "GetRemoteRTCPData() failed to retrieve sender info for remote "
niklase@google.com470e71d2011-07-07 08:21:25 +00002980 "side");
2981 return -1;
2982 }
2983
2984 // We only utilize 12 out of 20 bytes in the sender info (ignores packet
2985 // and octet count)
2986 NTPHigh = senderInfo.NTPseconds;
2987 NTPLow = senderInfo.NTPfraction;
2988 timestamp = senderInfo.RTPtimeStamp;
2989
2990 WEBRTC_TRACE(kTraceStateInfo, kTraceVoice,
2991 VoEId(_instanceId, _channelId),
2992 "GetRemoteRTCPData() => NTPHigh=%lu, NTPLow=%lu, "
2993 "timestamp=%lu",
2994 NTPHigh, NTPLow, timestamp);
2995
2996 // --- Locally derived information
2997
2998 // This value is updated on each incoming RTCP packet (0 when no packet
2999 // has been received)
pwestin@webrtc.org1de01352013-04-11 20:23:35 +00003000 playoutTimestamp = playout_timestamp_rtcp_;
niklase@google.com470e71d2011-07-07 08:21:25 +00003001
3002 WEBRTC_TRACE(kTraceStateInfo, kTraceVoice,
3003 VoEId(_instanceId, _channelId),
3004 "GetRemoteRTCPData() => playoutTimestamp=%lu",
pwestin@webrtc.org1de01352013-04-11 20:23:35 +00003005 playout_timestamp_rtcp_);
niklase@google.com470e71d2011-07-07 08:21:25 +00003006
3007 if (NULL != jitter || NULL != fractionLost)
3008 {
perkj@webrtc.orgce5990c2012-01-11 13:00:08 +00003009 // Get all RTCP receiver report blocks that have been received on this
3010 // channel. If we receive RTP packets from a remote source we know the
3011 // remote SSRC and use the report block from him.
3012 // Otherwise use the first report block.
3013 std::vector<RTCPReportBlock> remote_stats;
pwestin@webrtc.org2853dde2012-05-11 11:08:54 +00003014 if (_rtpRtcpModule->RemoteRTCPStat(&remote_stats) != 0 ||
perkj@webrtc.orgce5990c2012-01-11 13:00:08 +00003015 remote_stats.empty()) {
3016 WEBRTC_TRACE(kTraceWarning, kTraceVoice,
3017 VoEId(_instanceId, _channelId),
3018 "GetRemoteRTCPData() failed to measure statistics due"
3019 " to lack of received RTP and/or RTCP packets");
3020 return -1;
niklase@google.com470e71d2011-07-07 08:21:25 +00003021 }
perkj@webrtc.orgce5990c2012-01-11 13:00:08 +00003022
wu@webrtc.org822fbd82013-08-15 23:38:54 +00003023 uint32_t remoteSSRC = rtp_receiver_->SSRC();
perkj@webrtc.orgce5990c2012-01-11 13:00:08 +00003024 std::vector<RTCPReportBlock>::const_iterator it = remote_stats.begin();
3025 for (; it != remote_stats.end(); ++it) {
3026 if (it->remoteSSRC == remoteSSRC)
3027 break;
niklase@google.com470e71d2011-07-07 08:21:25 +00003028 }
perkj@webrtc.orgce5990c2012-01-11 13:00:08 +00003029
3030 if (it == remote_stats.end()) {
3031 // If we have not received any RTCP packets from this SSRC it probably
3032 // means that we have not received any RTP packets.
3033 // Use the first received report block instead.
3034 it = remote_stats.begin();
3035 remoteSSRC = it->remoteSSRC;
niklase@google.com470e71d2011-07-07 08:21:25 +00003036 }
perkj@webrtc.orgce5990c2012-01-11 13:00:08 +00003037
xians@webrtc.org79af7342012-01-31 12:22:14 +00003038 if (jitter) {
3039 *jitter = it->jitter;
3040 WEBRTC_TRACE(kTraceStateInfo, kTraceVoice,
3041 VoEId(_instanceId, _channelId),
3042 "GetRemoteRTCPData() => jitter = %lu", *jitter);
3043 }
perkj@webrtc.orgce5990c2012-01-11 13:00:08 +00003044
xians@webrtc.org79af7342012-01-31 12:22:14 +00003045 if (fractionLost) {
3046 *fractionLost = it->fractionLost;
3047 WEBRTC_TRACE(kTraceStateInfo, kTraceVoice,
3048 VoEId(_instanceId, _channelId),
3049 "GetRemoteRTCPData() => fractionLost = %lu",
3050 *fractionLost);
3051 }
niklase@google.com470e71d2011-07-07 08:21:25 +00003052 }
3053 return 0;
3054}
3055
3056int
pbos@webrtc.org92135212013-05-14 08:31:39 +00003057Channel::SendApplicationDefinedRTCPPacket(unsigned char subType,
niklase@google.com470e71d2011-07-07 08:21:25 +00003058 unsigned int name,
3059 const char* data,
3060 unsigned short dataLengthInBytes)
3061{
3062 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId, _channelId),
3063 "Channel::SendApplicationDefinedRTCPPacket()");
henrika@webrtc.org944cbeb2014-03-18 10:32:33 +00003064 if (!channel_state_.Get().sending)
niklase@google.com470e71d2011-07-07 08:21:25 +00003065 {
3066 _engineStatisticsPtr->SetLastError(
3067 VE_NOT_SENDING, kTraceError,
3068 "SendApplicationDefinedRTCPPacket() not sending");
3069 return -1;
3070 }
3071 if (NULL == data)
3072 {
3073 _engineStatisticsPtr->SetLastError(
3074 VE_INVALID_ARGUMENT, kTraceError,
3075 "SendApplicationDefinedRTCPPacket() invalid data value");
3076 return -1;
3077 }
3078 if (dataLengthInBytes % 4 != 0)
3079 {
3080 _engineStatisticsPtr->SetLastError(
3081 VE_INVALID_ARGUMENT, kTraceError,
3082 "SendApplicationDefinedRTCPPacket() invalid length value");
3083 return -1;
3084 }
pwestin@webrtc.org2853dde2012-05-11 11:08:54 +00003085 RTCPMethod status = _rtpRtcpModule->RTCP();
niklase@google.com470e71d2011-07-07 08:21:25 +00003086 if (status == kRtcpOff)
3087 {
3088 _engineStatisticsPtr->SetLastError(
3089 VE_RTCP_ERROR, kTraceError,
3090 "SendApplicationDefinedRTCPPacket() RTCP is disabled");
3091 return -1;
3092 }
3093
3094 // Create and schedule the RTCP APP packet for transmission
pwestin@webrtc.org2853dde2012-05-11 11:08:54 +00003095 if (_rtpRtcpModule->SetRTCPApplicationSpecificData(
niklase@google.com470e71d2011-07-07 08:21:25 +00003096 subType,
3097 name,
3098 (const unsigned char*) data,
3099 dataLengthInBytes) != 0)
3100 {
3101 _engineStatisticsPtr->SetLastError(
3102 VE_SEND_ERROR, kTraceError,
3103 "SendApplicationDefinedRTCPPacket() failed to send RTCP packet");
3104 return -1;
3105 }
3106 return 0;
3107}
3108
3109int
3110Channel::GetRTPStatistics(
3111 unsigned int& averageJitterMs,
3112 unsigned int& maxJitterMs,
3113 unsigned int& discardedPackets)
3114{
niklase@google.com470e71d2011-07-07 08:21:25 +00003115 // The jitter statistics is updated for each received RTP packet and is
3116 // based on received packets.
sprang@webrtc.org54ae4ff2013-12-19 13:26:02 +00003117 if (_rtpRtcpModule->RTCP() == kRtcpOff) {
3118 // If RTCP is off, there is no timed thread in the RTCP module regularly
3119 // generating new stats, trigger the update manually here instead.
3120 StreamStatistician* statistician =
3121 rtp_receive_statistics_->GetStatistician(rtp_receiver_->SSRC());
3122 if (statistician) {
3123 // Don't use returned statistics, use data from proxy instead so that
3124 // max jitter can be fetched atomically.
3125 RtcpStatistics s;
3126 statistician->GetStatistics(&s, true);
3127 }
niklase@google.com470e71d2011-07-07 08:21:25 +00003128 }
3129
sprang@webrtc.org54ae4ff2013-12-19 13:26:02 +00003130 ChannelStatistics stats = statistics_proxy_->GetStats();
andrew@webrtc.orgeb524d92013-09-23 23:02:24 +00003131 const int32_t playoutFrequency = audio_coding_->PlayoutFrequency();
sprang@webrtc.org54ae4ff2013-12-19 13:26:02 +00003132 if (playoutFrequency > 0) {
3133 // Scale RTP statistics given the current playout frequency
3134 maxJitterMs = stats.max_jitter / (playoutFrequency / 1000);
3135 averageJitterMs = stats.rtcp.jitter / (playoutFrequency / 1000);
niklase@google.com470e71d2011-07-07 08:21:25 +00003136 }
3137
3138 discardedPackets = _numberOfDiscardedPackets;
3139
3140 WEBRTC_TRACE(kTraceStateInfo, kTraceVoice,
3141 VoEId(_instanceId, _channelId),
3142 "GetRTPStatistics() => averageJitterMs = %lu, maxJitterMs = %lu,"
wu@webrtc.orgfcd12b32011-09-15 20:49:50 +00003143 " discardedPackets = %lu)",
niklase@google.com470e71d2011-07-07 08:21:25 +00003144 averageJitterMs, maxJitterMs, discardedPackets);
3145 return 0;
3146}
3147
henrika@webrtc.org8a2fc882012-08-22 08:53:55 +00003148int Channel::GetRemoteRTCPReportBlocks(
3149 std::vector<ReportBlock>* report_blocks) {
3150 if (report_blocks == NULL) {
3151 _engineStatisticsPtr->SetLastError(VE_INVALID_ARGUMENT, kTraceError,
3152 "GetRemoteRTCPReportBlock()s invalid report_blocks.");
3153 return -1;
3154 }
3155
3156 // Get the report blocks from the latest received RTCP Sender or Receiver
3157 // Report. Each element in the vector contains the sender's SSRC and a
3158 // report block according to RFC 3550.
3159 std::vector<RTCPReportBlock> rtcp_report_blocks;
3160 if (_rtpRtcpModule->RemoteRTCPStat(&rtcp_report_blocks) != 0) {
3161 _engineStatisticsPtr->SetLastError(VE_RTP_RTCP_MODULE_ERROR, kTraceError,
3162 "GetRemoteRTCPReportBlocks() failed to read RTCP SR/RR report block.");
3163 return -1;
3164 }
3165
3166 if (rtcp_report_blocks.empty())
3167 return 0;
3168
3169 std::vector<RTCPReportBlock>::const_iterator it = rtcp_report_blocks.begin();
3170 for (; it != rtcp_report_blocks.end(); ++it) {
3171 ReportBlock report_block;
3172 report_block.sender_SSRC = it->remoteSSRC;
3173 report_block.source_SSRC = it->sourceSSRC;
3174 report_block.fraction_lost = it->fractionLost;
3175 report_block.cumulative_num_packets_lost = it->cumulativeLost;
3176 report_block.extended_highest_sequence_number = it->extendedHighSeqNum;
3177 report_block.interarrival_jitter = it->jitter;
3178 report_block.last_SR_timestamp = it->lastSR;
3179 report_block.delay_since_last_SR = it->delaySinceLastSR;
3180 report_blocks->push_back(report_block);
3181 }
3182 return 0;
3183}
3184
niklase@google.com470e71d2011-07-07 08:21:25 +00003185int
3186Channel::GetRTPStatistics(CallStatistics& stats)
3187{
wu@webrtc.orgcb711f72014-05-19 17:39:11 +00003188 // --- RtcpStatistics
niklase@google.com470e71d2011-07-07 08:21:25 +00003189
3190 // The jitter statistics is updated for each received RTP packet and is
3191 // based on received packets.
sprang@webrtc.org54ae4ff2013-12-19 13:26:02 +00003192 RtcpStatistics statistics;
stefan@webrtc.org286fe0b2013-08-21 20:58:21 +00003193 StreamStatistician* statistician =
3194 rtp_receive_statistics_->GetStatistician(rtp_receiver_->SSRC());
3195 if (!statistician || !statistician->GetStatistics(
wu@webrtc.org822fbd82013-08-15 23:38:54 +00003196 &statistics, _rtpRtcpModule->RTCP() == kRtcpOff)) {
3197 _engineStatisticsPtr->SetLastError(
3198 VE_CANNOT_RETRIEVE_RTP_STAT, kTraceWarning,
3199 "GetRTPStatistics() failed to read RTP statistics from the "
3200 "RTP/RTCP module");
niklase@google.com470e71d2011-07-07 08:21:25 +00003201 }
3202
wu@webrtc.org822fbd82013-08-15 23:38:54 +00003203 stats.fractionLost = statistics.fraction_lost;
3204 stats.cumulativeLost = statistics.cumulative_lost;
3205 stats.extendedMax = statistics.extended_max_sequence_number;
3206 stats.jitterSamples = statistics.jitter;
niklase@google.com470e71d2011-07-07 08:21:25 +00003207
3208 WEBRTC_TRACE(kTraceStateInfo, kTraceVoice,
3209 VoEId(_instanceId, _channelId),
3210 "GetRTPStatistics() => fractionLost=%lu, cumulativeLost=%lu,"
wu@webrtc.orgfcd12b32011-09-15 20:49:50 +00003211 " extendedMax=%lu, jitterSamples=%li)",
niklase@google.com470e71d2011-07-07 08:21:25 +00003212 stats.fractionLost, stats.cumulativeLost, stats.extendedMax,
3213 stats.jitterSamples);
3214
wu@webrtc.orgcb711f72014-05-19 17:39:11 +00003215 // --- RTT
minyue@webrtc.org2b58a442014-09-11 07:51:53 +00003216 stats.rttMs = GetRTT();
minyue@webrtc.org6fd93082014-12-15 14:56:44 +00003217 if (stats.rttMs == 0) {
3218 WEBRTC_TRACE(kTraceWarning, kTraceVoice, VoEId(_instanceId, _channelId),
3219 "GetRTPStatistics() failed to get RTT");
3220 } else {
3221 WEBRTC_TRACE(kTraceStateInfo, kTraceVoice, VoEId(_instanceId, _channelId),
pkasting@chromium.org16825b12015-01-12 21:51:21 +00003222 "GetRTPStatistics() => rttMs=%" PRId64, stats.rttMs);
minyue@webrtc.org6fd93082014-12-15 14:56:44 +00003223 }
niklase@google.com470e71d2011-07-07 08:21:25 +00003224
wu@webrtc.orgcb711f72014-05-19 17:39:11 +00003225 // --- Data counters
niklase@google.com470e71d2011-07-07 08:21:25 +00003226
pkasting@chromium.org4591fbd2014-11-20 22:28:14 +00003227 size_t bytesSent(0);
pbos@webrtc.org6141e132013-04-09 10:09:10 +00003228 uint32_t packetsSent(0);
pkasting@chromium.org4591fbd2014-11-20 22:28:14 +00003229 size_t bytesReceived(0);
pbos@webrtc.org6141e132013-04-09 10:09:10 +00003230 uint32_t packetsReceived(0);
niklase@google.com470e71d2011-07-07 08:21:25 +00003231
stefan@webrtc.org286fe0b2013-08-21 20:58:21 +00003232 if (statistician) {
3233 statistician->GetDataCounters(&bytesReceived, &packetsReceived);
3234 }
wu@webrtc.org822fbd82013-08-15 23:38:54 +00003235
pwestin@webrtc.org2853dde2012-05-11 11:08:54 +00003236 if (_rtpRtcpModule->DataCountersRTP(&bytesSent,
wu@webrtc.org822fbd82013-08-15 23:38:54 +00003237 &packetsSent) != 0)
niklase@google.com470e71d2011-07-07 08:21:25 +00003238 {
3239 WEBRTC_TRACE(kTraceWarning, kTraceVoice,
3240 VoEId(_instanceId, _channelId),
3241 "GetRTPStatistics() failed to retrieve RTP datacounters =>"
wu@webrtc.orgfcd12b32011-09-15 20:49:50 +00003242 " output will not be complete");
niklase@google.com470e71d2011-07-07 08:21:25 +00003243 }
3244
3245 stats.bytesSent = bytesSent;
3246 stats.packetsSent = packetsSent;
3247 stats.bytesReceived = bytesReceived;
3248 stats.packetsReceived = packetsReceived;
3249
3250 WEBRTC_TRACE(kTraceStateInfo, kTraceVoice,
3251 VoEId(_instanceId, _channelId),
pkasting@chromium.org4591fbd2014-11-20 22:28:14 +00003252 "GetRTPStatistics() => bytesSent=%" PRIuS ", packetsSent=%d,"
3253 " bytesReceived=%" PRIuS ", packetsReceived=%d)",
niklase@google.com470e71d2011-07-07 08:21:25 +00003254 stats.bytesSent, stats.packetsSent, stats.bytesReceived,
3255 stats.packetsReceived);
3256
wu@webrtc.orgcb711f72014-05-19 17:39:11 +00003257 // --- Timestamps
3258 {
3259 CriticalSectionScoped lock(ts_stats_lock_.get());
3260 stats.capture_start_ntp_time_ms_ = capture_start_ntp_time_ms_;
3261 }
niklase@google.com470e71d2011-07-07 08:21:25 +00003262 return 0;
3263}
3264
minyue@webrtc.orgc1a40a72014-05-28 09:52:06 +00003265int Channel::SetREDStatus(bool enable, int redPayloadtype) {
turaj@webrtc.org42259e72012-12-11 02:15:12 +00003266 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId, _channelId),
minyue@webrtc.orgc1a40a72014-05-28 09:52:06 +00003267 "Channel::SetREDStatus()");
niklase@google.com470e71d2011-07-07 08:21:25 +00003268
turaj@webrtc.org8c8ad852013-01-31 18:20:17 +00003269 if (enable) {
3270 if (redPayloadtype < 0 || redPayloadtype > 127) {
3271 _engineStatisticsPtr->SetLastError(
3272 VE_PLTYPE_ERROR, kTraceError,
minyue@webrtc.orgc1a40a72014-05-28 09:52:06 +00003273 "SetREDStatus() invalid RED payload type");
turaj@webrtc.org8c8ad852013-01-31 18:20:17 +00003274 return -1;
3275 }
3276
3277 if (SetRedPayloadType(redPayloadtype) < 0) {
3278 _engineStatisticsPtr->SetLastError(
3279 VE_CODEC_ERROR, kTraceError,
3280 "SetSecondarySendCodec() Failed to register RED ACM");
3281 return -1;
3282 }
turaj@webrtc.org42259e72012-12-11 02:15:12 +00003283 }
niklase@google.com470e71d2011-07-07 08:21:25 +00003284
minyue@webrtc.orgaa5ea1c2014-05-23 15:16:51 +00003285 if (audio_coding_->SetREDStatus(enable) != 0) {
turaj@webrtc.org42259e72012-12-11 02:15:12 +00003286 _engineStatisticsPtr->SetLastError(
3287 VE_AUDIO_CODING_MODULE_ERROR, kTraceError,
minyue@webrtc.orgaa5ea1c2014-05-23 15:16:51 +00003288 "SetREDStatus() failed to set RED state in the ACM");
turaj@webrtc.org42259e72012-12-11 02:15:12 +00003289 return -1;
3290 }
3291 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +00003292}
3293
3294int
minyue@webrtc.orgc1a40a72014-05-28 09:52:06 +00003295Channel::GetREDStatus(bool& enabled, int& redPayloadtype)
niklase@google.com470e71d2011-07-07 08:21:25 +00003296{
minyue@webrtc.orgaa5ea1c2014-05-23 15:16:51 +00003297 enabled = audio_coding_->REDStatus();
niklase@google.com470e71d2011-07-07 08:21:25 +00003298 if (enabled)
3299 {
pbos@webrtc.org6141e132013-04-09 10:09:10 +00003300 int8_t payloadType(0);
pwestin@webrtc.org2853dde2012-05-11 11:08:54 +00003301 if (_rtpRtcpModule->SendREDPayloadType(payloadType) != 0)
niklase@google.com470e71d2011-07-07 08:21:25 +00003302 {
3303 _engineStatisticsPtr->SetLastError(
3304 VE_RTP_RTCP_MODULE_ERROR, kTraceError,
minyue@webrtc.orgc1a40a72014-05-28 09:52:06 +00003305 "GetREDStatus() failed to retrieve RED PT from RTP/RTCP "
niklase@google.com470e71d2011-07-07 08:21:25 +00003306 "module");
3307 return -1;
3308 }
pkasting@chromium.orgdf9a41d2015-01-26 22:35:29 +00003309 redPayloadtype = payloadType;
niklase@google.com470e71d2011-07-07 08:21:25 +00003310 WEBRTC_TRACE(kTraceStateInfo, kTraceVoice,
3311 VoEId(_instanceId, _channelId),
minyue@webrtc.orgc1a40a72014-05-28 09:52:06 +00003312 "GetREDStatus() => enabled=%d, redPayloadtype=%d",
niklase@google.com470e71d2011-07-07 08:21:25 +00003313 enabled, redPayloadtype);
3314 return 0;
3315 }
3316 WEBRTC_TRACE(kTraceStateInfo, kTraceVoice,
3317 VoEId(_instanceId, _channelId),
minyue@webrtc.orgc1a40a72014-05-28 09:52:06 +00003318 "GetREDStatus() => enabled=%d", enabled);
niklase@google.com470e71d2011-07-07 08:21:25 +00003319 return 0;
3320}
3321
minyue@webrtc.orgc1a40a72014-05-28 09:52:06 +00003322int Channel::SetCodecFECStatus(bool enable) {
3323 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId, _channelId),
3324 "Channel::SetCodecFECStatus()");
3325
3326 if (audio_coding_->SetCodecFEC(enable) != 0) {
3327 _engineStatisticsPtr->SetLastError(
3328 VE_AUDIO_CODING_MODULE_ERROR, kTraceError,
3329 "SetCodecFECStatus() failed to set FEC state");
3330 return -1;
3331 }
3332 return 0;
3333}
3334
3335bool Channel::GetCodecFECStatus() {
3336 bool enabled = audio_coding_->CodecFEC();
3337 WEBRTC_TRACE(kTraceStateInfo, kTraceVoice,
3338 VoEId(_instanceId, _channelId),
3339 "GetCodecFECStatus() => enabled=%d", enabled);
3340 return enabled;
3341}
3342
pwestin@webrtc.orgdb249952013-06-05 15:33:20 +00003343void Channel::SetNACKStatus(bool enable, int maxNumberOfPackets) {
3344 // None of these functions can fail.
3345 _rtpRtcpModule->SetStorePacketsStatus(enable, maxNumberOfPackets);
stefan@webrtc.org7bb8f022013-09-06 13:40:11 +00003346 rtp_receive_statistics_->SetMaxReorderingThreshold(maxNumberOfPackets);
3347 rtp_receiver_->SetNACKStatus(enable ? kNackRtcp : kNackOff);
pwestin@webrtc.orgd30859e2013-06-06 21:09:01 +00003348 if (enable)
andrew@webrtc.orgeb524d92013-09-23 23:02:24 +00003349 audio_coding_->EnableNack(maxNumberOfPackets);
pwestin@webrtc.orgd30859e2013-06-06 21:09:01 +00003350 else
andrew@webrtc.orgeb524d92013-09-23 23:02:24 +00003351 audio_coding_->DisableNack();
pwestin@webrtc.orgdb249952013-06-05 15:33:20 +00003352}
3353
pwestin@webrtc.orgd30859e2013-06-06 21:09:01 +00003354// Called when we are missing one or more packets.
3355int Channel::ResendPackets(const uint16_t* sequence_numbers, int length) {
pwestin@webrtc.orgdb249952013-06-05 15:33:20 +00003356 return _rtpRtcpModule->SendNACK(sequence_numbers, length);
3357}
3358
niklase@google.com470e71d2011-07-07 08:21:25 +00003359int
niklase@google.com470e71d2011-07-07 08:21:25 +00003360Channel::StartRTPDump(const char fileNameUTF8[1024],
3361 RTPDirections direction)
3362{
3363 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId, _channelId),
3364 "Channel::StartRTPDump()");
3365 if ((direction != kRtpIncoming) && (direction != kRtpOutgoing))
3366 {
3367 _engineStatisticsPtr->SetLastError(
3368 VE_INVALID_ARGUMENT, kTraceError,
3369 "StartRTPDump() invalid RTP direction");
3370 return -1;
3371 }
3372 RtpDump* rtpDumpPtr = (direction == kRtpIncoming) ?
3373 &_rtpDumpIn : &_rtpDumpOut;
3374 if (rtpDumpPtr == NULL)
3375 {
3376 assert(false);
3377 return -1;
3378 }
3379 if (rtpDumpPtr->IsActive())
3380 {
3381 rtpDumpPtr->Stop();
3382 }
3383 if (rtpDumpPtr->Start(fileNameUTF8) != 0)
3384 {
3385 _engineStatisticsPtr->SetLastError(
3386 VE_BAD_FILE, kTraceError,
3387 "StartRTPDump() failed to create file");
3388 return -1;
3389 }
3390 return 0;
3391}
3392
3393int
3394Channel::StopRTPDump(RTPDirections direction)
3395{
3396 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId, _channelId),
3397 "Channel::StopRTPDump()");
3398 if ((direction != kRtpIncoming) && (direction != kRtpOutgoing))
3399 {
3400 _engineStatisticsPtr->SetLastError(
3401 VE_INVALID_ARGUMENT, kTraceError,
3402 "StopRTPDump() invalid RTP direction");
3403 return -1;
3404 }
3405 RtpDump* rtpDumpPtr = (direction == kRtpIncoming) ?
3406 &_rtpDumpIn : &_rtpDumpOut;
3407 if (rtpDumpPtr == NULL)
3408 {
3409 assert(false);
3410 return -1;
3411 }
3412 if (!rtpDumpPtr->IsActive())
3413 {
3414 return 0;
3415 }
3416 return rtpDumpPtr->Stop();
3417}
3418
3419bool
3420Channel::RTPDumpIsActive(RTPDirections direction)
3421{
3422 if ((direction != kRtpIncoming) &&
3423 (direction != kRtpOutgoing))
3424 {
3425 _engineStatisticsPtr->SetLastError(
3426 VE_INVALID_ARGUMENT, kTraceError,
3427 "RTPDumpIsActive() invalid RTP direction");
3428 return false;
3429 }
3430 RtpDump* rtpDumpPtr = (direction == kRtpIncoming) ?
3431 &_rtpDumpIn : &_rtpDumpOut;
3432 return rtpDumpPtr->IsActive();
3433}
3434
solenberg@webrtc.orgb1f50102014-03-24 10:38:25 +00003435void Channel::SetVideoEngineBWETarget(ViENetwork* vie_network,
3436 int video_channel) {
3437 CriticalSectionScoped cs(&_callbackCritSect);
3438 if (vie_network_) {
3439 vie_network_->Release();
3440 vie_network_ = NULL;
3441 }
3442 video_channel_ = -1;
3443
3444 if (vie_network != NULL && video_channel != -1) {
3445 vie_network_ = vie_network;
3446 video_channel_ = video_channel;
3447 }
3448}
3449
pbos@webrtc.org6141e132013-04-09 10:09:10 +00003450uint32_t
andrew@webrtc.org755b04a2011-11-15 16:57:56 +00003451Channel::Demultiplex(const AudioFrame& audioFrame)
niklase@google.com470e71d2011-07-07 08:21:25 +00003452{
3453 WEBRTC_TRACE(kTraceStream, kTraceVoice, VoEId(_instanceId,_channelId),
andrew@webrtc.org755b04a2011-11-15 16:57:56 +00003454 "Channel::Demultiplex()");
andrew@webrtc.orgae1a58b2013-01-22 04:44:30 +00003455 _audioFrame.CopyFrom(audioFrame);
andrew@webrtc.org63a50982012-05-02 23:56:37 +00003456 _audioFrame.id_ = _channelId;
niklase@google.com470e71d2011-07-07 08:21:25 +00003457 return 0;
3458}
3459
xians@webrtc.org2f84afa2013-07-31 16:23:37 +00003460void Channel::Demultiplex(const int16_t* audio_data,
xians@webrtc.org8fff1f02013-07-31 16:27:42 +00003461 int sample_rate,
xians@webrtc.org2f84afa2013-07-31 16:23:37 +00003462 int number_of_frames,
xians@webrtc.org8fff1f02013-07-31 16:27:42 +00003463 int number_of_channels) {
xians@webrtc.org2f84afa2013-07-31 16:23:37 +00003464 CodecInst codec;
3465 GetSendCodec(codec);
xians@webrtc.org2f84afa2013-07-31 16:23:37 +00003466
andrew@webrtc.org40ee3d02014-04-03 21:56:01 +00003467 if (!mono_recording_audio_.get()) {
3468 // Temporary space for DownConvertToCodecFormat.
3469 mono_recording_audio_.reset(new int16_t[kMaxMonoDataSizeSamples]);
xians@webrtc.org2f84afa2013-07-31 16:23:37 +00003470 }
andrew@webrtc.org40ee3d02014-04-03 21:56:01 +00003471 DownConvertToCodecFormat(audio_data,
3472 number_of_frames,
3473 number_of_channels,
3474 sample_rate,
3475 codec.channels,
3476 codec.plfreq,
3477 mono_recording_audio_.get(),
3478 &input_resampler_,
3479 &_audioFrame);
xians@webrtc.org2f84afa2013-07-31 16:23:37 +00003480}
3481
pbos@webrtc.org6141e132013-04-09 10:09:10 +00003482uint32_t
xians@google.com0b0665a2011-08-08 08:18:44 +00003483Channel::PrepareEncodeAndSend(int mixingFrequency)
niklase@google.com470e71d2011-07-07 08:21:25 +00003484{
3485 WEBRTC_TRACE(kTraceStream, kTraceVoice, VoEId(_instanceId,_channelId),
3486 "Channel::PrepareEncodeAndSend()");
3487
andrew@webrtc.org63a50982012-05-02 23:56:37 +00003488 if (_audioFrame.samples_per_channel_ == 0)
niklase@google.com470e71d2011-07-07 08:21:25 +00003489 {
3490 WEBRTC_TRACE(kTraceWarning, kTraceVoice, VoEId(_instanceId,_channelId),
3491 "Channel::PrepareEncodeAndSend() invalid audio frame");
tommi@webrtc.orgeec6ecd2014-07-11 19:09:59 +00003492 return 0xFFFFFFFF;
niklase@google.com470e71d2011-07-07 08:21:25 +00003493 }
3494
henrika@webrtc.org944cbeb2014-03-18 10:32:33 +00003495 if (channel_state_.Get().input_file_playing)
niklase@google.com470e71d2011-07-07 08:21:25 +00003496 {
3497 MixOrReplaceAudioWithFile(mixingFrequency);
3498 }
3499
andrew@webrtc.org21299d42014-05-14 19:00:59 +00003500 bool is_muted = Mute(); // Cache locally as Mute() takes a lock.
3501 if (is_muted) {
3502 AudioFrameOperations::Mute(_audioFrame);
niklase@google.com470e71d2011-07-07 08:21:25 +00003503 }
3504
henrika@webrtc.org944cbeb2014-03-18 10:32:33 +00003505 if (channel_state_.Get().input_external_media)
niklase@google.com470e71d2011-07-07 08:21:25 +00003506 {
mflodman@webrtc.org9a065d12012-03-07 08:12:21 +00003507 CriticalSectionScoped cs(&_callbackCritSect);
andrew@webrtc.org63a50982012-05-02 23:56:37 +00003508 const bool isStereo = (_audioFrame.num_channels_ == 2);
niklase@google.com470e71d2011-07-07 08:21:25 +00003509 if (_inputExternalMediaCallbackPtr)
3510 {
3511 _inputExternalMediaCallbackPtr->Process(
3512 _channelId,
3513 kRecordingPerChannel,
pbos@webrtc.org6141e132013-04-09 10:09:10 +00003514 (int16_t*)_audioFrame.data_,
andrew@webrtc.org63a50982012-05-02 23:56:37 +00003515 _audioFrame.samples_per_channel_,
3516 _audioFrame.sample_rate_hz_,
niklase@google.com470e71d2011-07-07 08:21:25 +00003517 isStereo);
3518 }
3519 }
3520
3521 InsertInbandDtmfTone();
3522
andrew@webrtc.org60730cf2014-01-07 17:45:09 +00003523 if (_includeAudioLevelIndication) {
andrew@webrtc.org382c0c22014-05-05 18:22:21 +00003524 int length = _audioFrame.samples_per_channel_ * _audioFrame.num_channels_;
andrew@webrtc.org21299d42014-05-14 19:00:59 +00003525 if (is_muted) {
3526 rms_level_.ProcessMuted(length);
3527 } else {
3528 rms_level_.Process(_audioFrame.data_, length);
3529 }
andrew@webrtc.org755b04a2011-11-15 16:57:56 +00003530 }
3531
niklase@google.com470e71d2011-07-07 08:21:25 +00003532 return 0;
3533}
3534
pbos@webrtc.org6141e132013-04-09 10:09:10 +00003535uint32_t
niklase@google.com470e71d2011-07-07 08:21:25 +00003536Channel::EncodeAndSend()
3537{
3538 WEBRTC_TRACE(kTraceStream, kTraceVoice, VoEId(_instanceId,_channelId),
3539 "Channel::EncodeAndSend()");
3540
andrew@webrtc.org63a50982012-05-02 23:56:37 +00003541 assert(_audioFrame.num_channels_ <= 2);
3542 if (_audioFrame.samples_per_channel_ == 0)
niklase@google.com470e71d2011-07-07 08:21:25 +00003543 {
3544 WEBRTC_TRACE(kTraceWarning, kTraceVoice, VoEId(_instanceId,_channelId),
3545 "Channel::EncodeAndSend() invalid audio frame");
tommi@webrtc.orgeec6ecd2014-07-11 19:09:59 +00003546 return 0xFFFFFFFF;
niklase@google.com470e71d2011-07-07 08:21:25 +00003547 }
3548
andrew@webrtc.org63a50982012-05-02 23:56:37 +00003549 _audioFrame.id_ = _channelId;
niklase@google.com470e71d2011-07-07 08:21:25 +00003550
3551 // --- Add 10ms of raw (PCM) audio data to the encoder @ 32kHz.
3552
3553 // The ACM resamples internally.
andrew@webrtc.org63a50982012-05-02 23:56:37 +00003554 _audioFrame.timestamp_ = _timeStamp;
andrew@webrtc.orgeb524d92013-09-23 23:02:24 +00003555 if (audio_coding_->Add10MsData((AudioFrame&)_audioFrame) != 0)
niklase@google.com470e71d2011-07-07 08:21:25 +00003556 {
3557 WEBRTC_TRACE(kTraceError, kTraceVoice, VoEId(_instanceId,_channelId),
3558 "Channel::EncodeAndSend() ACM encoding failed");
tommi@webrtc.orgeec6ecd2014-07-11 19:09:59 +00003559 return 0xFFFFFFFF;
niklase@google.com470e71d2011-07-07 08:21:25 +00003560 }
3561
andrew@webrtc.org63a50982012-05-02 23:56:37 +00003562 _timeStamp += _audioFrame.samples_per_channel_;
niklase@google.com470e71d2011-07-07 08:21:25 +00003563
3564 // --- Encode if complete frame is ready
3565
3566 // This call will trigger AudioPacketizationCallback::SendData if encoding
3567 // is done and payload is ready for packetization and transmission.
andrew@webrtc.orgeb524d92013-09-23 23:02:24 +00003568 return audio_coding_->Process();
niklase@google.com470e71d2011-07-07 08:21:25 +00003569}
3570
3571int Channel::RegisterExternalMediaProcessing(
3572 ProcessingTypes type,
3573 VoEMediaProcess& processObject)
3574{
3575 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId,_channelId),
3576 "Channel::RegisterExternalMediaProcessing()");
3577
mflodman@webrtc.org9a065d12012-03-07 08:12:21 +00003578 CriticalSectionScoped cs(&_callbackCritSect);
niklase@google.com470e71d2011-07-07 08:21:25 +00003579
3580 if (kPlaybackPerChannel == type)
3581 {
3582 if (_outputExternalMediaCallbackPtr)
3583 {
3584 _engineStatisticsPtr->SetLastError(
3585 VE_INVALID_OPERATION, kTraceError,
3586 "Channel::RegisterExternalMediaProcessing() "
3587 "output external media already enabled");
3588 return -1;
3589 }
3590 _outputExternalMediaCallbackPtr = &processObject;
3591 _outputExternalMedia = true;
3592 }
3593 else if (kRecordingPerChannel == type)
3594 {
3595 if (_inputExternalMediaCallbackPtr)
3596 {
3597 _engineStatisticsPtr->SetLastError(
3598 VE_INVALID_OPERATION, kTraceError,
3599 "Channel::RegisterExternalMediaProcessing() "
3600 "output external media already enabled");
3601 return -1;
3602 }
3603 _inputExternalMediaCallbackPtr = &processObject;
henrika@webrtc.org944cbeb2014-03-18 10:32:33 +00003604 channel_state_.SetInputExternalMedia(true);
niklase@google.com470e71d2011-07-07 08:21:25 +00003605 }
3606 return 0;
3607}
3608
3609int Channel::DeRegisterExternalMediaProcessing(ProcessingTypes type)
3610{
3611 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId,_channelId),
3612 "Channel::DeRegisterExternalMediaProcessing()");
3613
mflodman@webrtc.org9a065d12012-03-07 08:12:21 +00003614 CriticalSectionScoped cs(&_callbackCritSect);
niklase@google.com470e71d2011-07-07 08:21:25 +00003615
3616 if (kPlaybackPerChannel == type)
3617 {
3618 if (!_outputExternalMediaCallbackPtr)
3619 {
3620 _engineStatisticsPtr->SetLastError(
3621 VE_INVALID_OPERATION, kTraceWarning,
3622 "Channel::DeRegisterExternalMediaProcessing() "
3623 "output external media already disabled");
3624 return 0;
3625 }
3626 _outputExternalMedia = false;
3627 _outputExternalMediaCallbackPtr = NULL;
3628 }
3629 else if (kRecordingPerChannel == type)
3630 {
3631 if (!_inputExternalMediaCallbackPtr)
3632 {
3633 _engineStatisticsPtr->SetLastError(
3634 VE_INVALID_OPERATION, kTraceWarning,
3635 "Channel::DeRegisterExternalMediaProcessing() "
3636 "input external media already disabled");
3637 return 0;
3638 }
henrika@webrtc.org944cbeb2014-03-18 10:32:33 +00003639 channel_state_.SetInputExternalMedia(false);
niklase@google.com470e71d2011-07-07 08:21:25 +00003640 _inputExternalMediaCallbackPtr = NULL;
3641 }
3642
3643 return 0;
3644}
3645
roosa@google.com1b60ceb2012-12-12 23:00:29 +00003646int Channel::SetExternalMixing(bool enabled) {
3647 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId,_channelId),
3648 "Channel::SetExternalMixing(enabled=%d)", enabled);
3649
henrika@webrtc.org944cbeb2014-03-18 10:32:33 +00003650 if (channel_state_.Get().playing)
roosa@google.com1b60ceb2012-12-12 23:00:29 +00003651 {
3652 _engineStatisticsPtr->SetLastError(
3653 VE_INVALID_OPERATION, kTraceError,
3654 "Channel::SetExternalMixing() "
3655 "external mixing cannot be changed while playing.");
3656 return -1;
3657 }
3658
3659 _externalMixing = enabled;
3660
3661 return 0;
3662}
3663
niklase@google.com470e71d2011-07-07 08:21:25 +00003664int
niklase@google.com470e71d2011-07-07 08:21:25 +00003665Channel::GetNetworkStatistics(NetworkStatistics& stats)
3666{
3667 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId,_channelId),
3668 "Channel::GetNetworkStatistics()");
tina.legrand@webrtc.org7a7a0082013-02-21 10:27:48 +00003669 ACMNetworkStatistics acm_stats;
andrew@webrtc.orgeb524d92013-09-23 23:02:24 +00003670 int return_value = audio_coding_->NetworkStatistics(&acm_stats);
tina.legrand@webrtc.org7a7a0082013-02-21 10:27:48 +00003671 if (return_value >= 0) {
3672 memcpy(&stats, &acm_stats, sizeof(NetworkStatistics));
3673 }
3674 return return_value;
niklase@google.com470e71d2011-07-07 08:21:25 +00003675}
3676
wu@webrtc.org24301a62013-12-13 19:17:43 +00003677void Channel::GetDecodingCallStatistics(AudioDecodingCallStats* stats) const {
3678 audio_coding_->GetDecodingCallStatistics(stats);
3679}
3680
pwestin@webrtc.org1de01352013-04-11 20:23:35 +00003681bool Channel::GetDelayEstimate(int* jitter_buffer_delay_ms,
3682 int* playout_buffer_delay_ms) const {
3683 if (_average_jitter_buffer_delay_us == 0) {
niklase@google.com470e71d2011-07-07 08:21:25 +00003684 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId,_channelId),
pwestin@webrtc.org1de01352013-04-11 20:23:35 +00003685 "Channel::GetDelayEstimate() no valid estimate.");
3686 return false;
3687 }
3688 *jitter_buffer_delay_ms = (_average_jitter_buffer_delay_us + 500) / 1000 +
3689 _recPacketDelayMs;
3690 *playout_buffer_delay_ms = playout_delay_ms_;
3691 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId,_channelId),
3692 "Channel::GetDelayEstimate()");
3693 return true;
niklase@google.com470e71d2011-07-07 08:21:25 +00003694}
3695
turaj@webrtc.org6388c3e2013-02-12 21:42:18 +00003696int Channel::SetInitialPlayoutDelay(int delay_ms)
3697{
3698 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId,_channelId),
3699 "Channel::SetInitialPlayoutDelay()");
3700 if ((delay_ms < kVoiceEngineMinMinPlayoutDelayMs) ||
3701 (delay_ms > kVoiceEngineMaxMinPlayoutDelayMs))
3702 {
3703 _engineStatisticsPtr->SetLastError(
3704 VE_INVALID_ARGUMENT, kTraceError,
3705 "SetInitialPlayoutDelay() invalid min delay");
3706 return -1;
3707 }
andrew@webrtc.orgeb524d92013-09-23 23:02:24 +00003708 if (audio_coding_->SetInitialPlayoutDelay(delay_ms) != 0)
turaj@webrtc.org6388c3e2013-02-12 21:42:18 +00003709 {
3710 _engineStatisticsPtr->SetLastError(
3711 VE_AUDIO_CODING_MODULE_ERROR, kTraceError,
3712 "SetInitialPlayoutDelay() failed to set min playout delay");
3713 return -1;
3714 }
3715 return 0;
3716}
3717
3718
niklase@google.com470e71d2011-07-07 08:21:25 +00003719int
3720Channel::SetMinimumPlayoutDelay(int delayMs)
3721{
3722 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId,_channelId),
3723 "Channel::SetMinimumPlayoutDelay()");
3724 if ((delayMs < kVoiceEngineMinMinPlayoutDelayMs) ||
3725 (delayMs > kVoiceEngineMaxMinPlayoutDelayMs))
3726 {
3727 _engineStatisticsPtr->SetLastError(
3728 VE_INVALID_ARGUMENT, kTraceError,
3729 "SetMinimumPlayoutDelay() invalid min delay");
3730 return -1;
3731 }
andrew@webrtc.orgeb524d92013-09-23 23:02:24 +00003732 if (audio_coding_->SetMinimumPlayoutDelay(delayMs) != 0)
niklase@google.com470e71d2011-07-07 08:21:25 +00003733 {
3734 _engineStatisticsPtr->SetLastError(
3735 VE_AUDIO_CODING_MODULE_ERROR, kTraceError,
3736 "SetMinimumPlayoutDelay() failed to set min playout delay");
3737 return -1;
3738 }
3739 return 0;
3740}
3741
pwestin@webrtc.org1de01352013-04-11 20:23:35 +00003742void Channel::UpdatePlayoutTimestamp(bool rtcp) {
3743 uint32_t playout_timestamp = 0;
3744
andrew@webrtc.orgeb524d92013-09-23 23:02:24 +00003745 if (audio_coding_->PlayoutTimestamp(&playout_timestamp) == -1) {
turaj@webrtc.org1ebd2e92014-07-25 17:50:10 +00003746 // This can happen if this channel has not been received any RTP packet. In
3747 // this case, NetEq is not capable of computing playout timestamp.
pwestin@webrtc.org1de01352013-04-11 20:23:35 +00003748 return;
3749 }
3750
3751 uint16_t delay_ms = 0;
3752 if (_audioDeviceModulePtr->PlayoutDelay(&delay_ms) == -1) {
3753 WEBRTC_TRACE(kTraceWarning, kTraceVoice, VoEId(_instanceId,_channelId),
3754 "Channel::UpdatePlayoutTimestamp() failed to read playout"
3755 " delay from the ADM");
3756 _engineStatisticsPtr->SetLastError(
3757 VE_CANNOT_RETRIEVE_VALUE, kTraceError,
3758 "UpdatePlayoutTimestamp() failed to retrieve playout delay");
3759 return;
3760 }
3761
turaj@webrtc.org167b6df2013-12-13 21:05:07 +00003762 jitter_buffer_playout_timestamp_ = playout_timestamp;
3763
pwestin@webrtc.org1de01352013-04-11 20:23:35 +00003764 // Remove the playout delay.
wu@webrtc.org94454b72014-06-05 20:34:08 +00003765 playout_timestamp -= (delay_ms * (GetPlayoutFrequency() / 1000));
pwestin@webrtc.org1de01352013-04-11 20:23:35 +00003766
3767 WEBRTC_TRACE(kTraceStream, kTraceVoice, VoEId(_instanceId,_channelId),
3768 "Channel::UpdatePlayoutTimestamp() => playoutTimestamp = %lu",
3769 playout_timestamp);
3770
3771 if (rtcp) {
3772 playout_timestamp_rtcp_ = playout_timestamp;
3773 } else {
3774 playout_timestamp_rtp_ = playout_timestamp;
3775 }
3776 playout_delay_ms_ = delay_ms;
3777}
3778
3779int Channel::GetPlayoutTimestamp(unsigned int& timestamp) {
3780 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId,_channelId),
3781 "Channel::GetPlayoutTimestamp()");
3782 if (playout_timestamp_rtp_ == 0) {
3783 _engineStatisticsPtr->SetLastError(
3784 VE_CANNOT_RETRIEVE_VALUE, kTraceError,
3785 "GetPlayoutTimestamp() failed to retrieve timestamp");
3786 return -1;
3787 }
3788 timestamp = playout_timestamp_rtp_;
3789 WEBRTC_TRACE(kTraceStateInfo, kTraceVoice,
3790 VoEId(_instanceId,_channelId),
3791 "GetPlayoutTimestamp() => timestamp=%u", timestamp);
3792 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +00003793}
3794
pbos@webrtc.orgd16e8392014-12-19 13:49:55 +00003795int Channel::SetInitTimestamp(unsigned int timestamp) {
3796 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId, _channelId),
niklase@google.com470e71d2011-07-07 08:21:25 +00003797 "Channel::SetInitTimestamp()");
pbos@webrtc.orgd16e8392014-12-19 13:49:55 +00003798 if (channel_state_.Get().sending) {
3799 _engineStatisticsPtr->SetLastError(VE_SENDING, kTraceError,
3800 "SetInitTimestamp() already sending");
3801 return -1;
3802 }
3803 _rtpRtcpModule->SetStartTimestamp(timestamp);
3804 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +00003805}
3806
pbos@webrtc.orgd16e8392014-12-19 13:49:55 +00003807int Channel::SetInitSequenceNumber(short sequenceNumber) {
3808 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId, _channelId),
3809 "Channel::SetInitSequenceNumber()");
3810 if (channel_state_.Get().sending) {
3811 _engineStatisticsPtr->SetLastError(
3812 VE_SENDING, kTraceError, "SetInitSequenceNumber() already sending");
3813 return -1;
3814 }
3815 _rtpRtcpModule->SetSequenceNumber(sequenceNumber);
3816 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +00003817}
3818
3819int
wu@webrtc.org822fbd82013-08-15 23:38:54 +00003820Channel::GetRtpRtcp(RtpRtcp** rtpRtcpModule, RtpReceiver** rtp_receiver) const
niklase@google.com470e71d2011-07-07 08:21:25 +00003821{
3822 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId,_channelId),
3823 "Channel::GetRtpRtcp()");
wu@webrtc.org822fbd82013-08-15 23:38:54 +00003824 *rtpRtcpModule = _rtpRtcpModule.get();
3825 *rtp_receiver = rtp_receiver_.get();
niklase@google.com470e71d2011-07-07 08:21:25 +00003826 return 0;
3827}
3828
andrew@webrtc.orge59a0ac2012-05-08 17:12:40 +00003829// TODO(andrew): refactor Mix functions here and in transmit_mixer.cc to use
3830// a shared helper.
pbos@webrtc.org6141e132013-04-09 10:09:10 +00003831int32_t
pbos@webrtc.org92135212013-05-14 08:31:39 +00003832Channel::MixOrReplaceAudioWithFile(int mixingFrequency)
niklase@google.com470e71d2011-07-07 08:21:25 +00003833{
andrew@webrtc.org8f693302014-04-25 23:10:28 +00003834 scoped_ptr<int16_t[]> fileBuffer(new int16_t[640]);
andrew@webrtc.orge59a0ac2012-05-08 17:12:40 +00003835 int fileSamples(0);
niklase@google.com470e71d2011-07-07 08:21:25 +00003836
3837 {
mflodman@webrtc.org9a065d12012-03-07 08:12:21 +00003838 CriticalSectionScoped cs(&_fileCritSect);
niklase@google.com470e71d2011-07-07 08:21:25 +00003839
3840 if (_inputFilePlayerPtr == NULL)
3841 {
3842 WEBRTC_TRACE(kTraceWarning, kTraceVoice,
3843 VoEId(_instanceId, _channelId),
3844 "Channel::MixOrReplaceAudioWithFile() fileplayer"
3845 " doesnt exist");
3846 return -1;
3847 }
3848
braveyao@webrtc.orgd7131432012-03-29 10:39:44 +00003849 if (_inputFilePlayerPtr->Get10msAudioFromFile(fileBuffer.get(),
niklase@google.com470e71d2011-07-07 08:21:25 +00003850 fileSamples,
3851 mixingFrequency) == -1)
3852 {
3853 WEBRTC_TRACE(kTraceWarning, kTraceVoice,
3854 VoEId(_instanceId, _channelId),
3855 "Channel::MixOrReplaceAudioWithFile() file mixing "
3856 "failed");
3857 return -1;
3858 }
3859 if (fileSamples == 0)
3860 {
3861 WEBRTC_TRACE(kTraceWarning, kTraceVoice,
3862 VoEId(_instanceId, _channelId),
3863 "Channel::MixOrReplaceAudioWithFile() file is ended");
3864 return 0;
3865 }
3866 }
3867
andrew@webrtc.org63a50982012-05-02 23:56:37 +00003868 assert(_audioFrame.samples_per_channel_ == fileSamples);
niklase@google.com470e71d2011-07-07 08:21:25 +00003869
3870 if (_mixFileWithMicrophone)
3871 {
braveyao@webrtc.orgd7131432012-03-29 10:39:44 +00003872 // Currently file stream is always mono.
3873 // TODO(xians): Change the code when FilePlayer supports real stereo.
andrew@webrtc.org40ee3d02014-04-03 21:56:01 +00003874 MixWithSat(_audioFrame.data_,
3875 _audioFrame.num_channels_,
3876 fileBuffer.get(),
3877 1,
3878 fileSamples);
niklase@google.com470e71d2011-07-07 08:21:25 +00003879 }
3880 else
3881 {
braveyao@webrtc.orgd7131432012-03-29 10:39:44 +00003882 // Replace ACM audio with file.
3883 // Currently file stream is always mono.
3884 // TODO(xians): Change the code when FilePlayer supports real stereo.
niklase@google.com470e71d2011-07-07 08:21:25 +00003885 _audioFrame.UpdateFrame(_channelId,
tommi@webrtc.orgeec6ecd2014-07-11 19:09:59 +00003886 0xFFFFFFFF,
braveyao@webrtc.orgd7131432012-03-29 10:39:44 +00003887 fileBuffer.get(),
andrew@webrtc.orge59a0ac2012-05-08 17:12:40 +00003888 fileSamples,
niklase@google.com470e71d2011-07-07 08:21:25 +00003889 mixingFrequency,
3890 AudioFrame::kNormalSpeech,
3891 AudioFrame::kVadUnknown,
3892 1);
3893
3894 }
3895 return 0;
3896}
3897
pbos@webrtc.org6141e132013-04-09 10:09:10 +00003898int32_t
niklase@google.com470e71d2011-07-07 08:21:25 +00003899Channel::MixAudioWithFile(AudioFrame& audioFrame,
pbos@webrtc.org92135212013-05-14 08:31:39 +00003900 int mixingFrequency)
niklase@google.com470e71d2011-07-07 08:21:25 +00003901{
minyue@webrtc.org2a8df7c2014-08-06 10:05:19 +00003902 assert(mixingFrequency <= 48000);
niklase@google.com470e71d2011-07-07 08:21:25 +00003903
minyue@webrtc.org2a8df7c2014-08-06 10:05:19 +00003904 scoped_ptr<int16_t[]> fileBuffer(new int16_t[960]);
andrew@webrtc.orge59a0ac2012-05-08 17:12:40 +00003905 int fileSamples(0);
niklase@google.com470e71d2011-07-07 08:21:25 +00003906
3907 {
mflodman@webrtc.org9a065d12012-03-07 08:12:21 +00003908 CriticalSectionScoped cs(&_fileCritSect);
niklase@google.com470e71d2011-07-07 08:21:25 +00003909
3910 if (_outputFilePlayerPtr == NULL)
3911 {
3912 WEBRTC_TRACE(kTraceWarning, kTraceVoice,
3913 VoEId(_instanceId, _channelId),
3914 "Channel::MixAudioWithFile() file mixing failed");
3915 return -1;
3916 }
3917
3918 // We should get the frequency we ask for.
braveyao@webrtc.orgd7131432012-03-29 10:39:44 +00003919 if (_outputFilePlayerPtr->Get10msAudioFromFile(fileBuffer.get(),
niklase@google.com470e71d2011-07-07 08:21:25 +00003920 fileSamples,
3921 mixingFrequency) == -1)
3922 {
3923 WEBRTC_TRACE(kTraceWarning, kTraceVoice,
3924 VoEId(_instanceId, _channelId),
3925 "Channel::MixAudioWithFile() file mixing failed");
3926 return -1;
3927 }
3928 }
3929
andrew@webrtc.org63a50982012-05-02 23:56:37 +00003930 if (audioFrame.samples_per_channel_ == fileSamples)
niklase@google.com470e71d2011-07-07 08:21:25 +00003931 {
braveyao@webrtc.orgd7131432012-03-29 10:39:44 +00003932 // Currently file stream is always mono.
3933 // TODO(xians): Change the code when FilePlayer supports real stereo.
andrew@webrtc.org40ee3d02014-04-03 21:56:01 +00003934 MixWithSat(audioFrame.data_,
3935 audioFrame.num_channels_,
3936 fileBuffer.get(),
3937 1,
3938 fileSamples);
niklase@google.com470e71d2011-07-07 08:21:25 +00003939 }
3940 else
3941 {
3942 WEBRTC_TRACE(kTraceWarning, kTraceVoice, VoEId(_instanceId,_channelId),
andrew@webrtc.org63a50982012-05-02 23:56:37 +00003943 "Channel::MixAudioWithFile() samples_per_channel_(%d) != "
niklase@google.com470e71d2011-07-07 08:21:25 +00003944 "fileSamples(%d)",
andrew@webrtc.org63a50982012-05-02 23:56:37 +00003945 audioFrame.samples_per_channel_, fileSamples);
niklase@google.com470e71d2011-07-07 08:21:25 +00003946 return -1;
3947 }
3948
3949 return 0;
3950}
3951
3952int
3953Channel::InsertInbandDtmfTone()
3954{
niklas.enbom@webrtc.orgaf26f642011-11-16 12:41:36 +00003955 // Check if we should start a new tone.
niklase@google.com470e71d2011-07-07 08:21:25 +00003956 if (_inbandDtmfQueue.PendingDtmf() &&
3957 !_inbandDtmfGenerator.IsAddingTone() &&
3958 _inbandDtmfGenerator.DelaySinceLastTone() >
3959 kMinTelephoneEventSeparationMs)
3960 {
pbos@webrtc.org6141e132013-04-09 10:09:10 +00003961 int8_t eventCode(0);
3962 uint16_t lengthMs(0);
3963 uint8_t attenuationDb(0);
niklase@google.com470e71d2011-07-07 08:21:25 +00003964
3965 eventCode = _inbandDtmfQueue.NextDtmf(&lengthMs, &attenuationDb);
3966 _inbandDtmfGenerator.AddTone(eventCode, lengthMs, attenuationDb);
3967 if (_playInbandDtmfEvent)
3968 {
3969 // Add tone to output mixer using a reduced length to minimize
3970 // risk of echo.
3971 _outputMixerPtr->PlayDtmfTone(eventCode, lengthMs - 80,
3972 attenuationDb);
3973 }
3974 }
3975
3976 if (_inbandDtmfGenerator.IsAddingTone())
3977 {
pbos@webrtc.org6141e132013-04-09 10:09:10 +00003978 uint16_t frequency(0);
niklase@google.com470e71d2011-07-07 08:21:25 +00003979 _inbandDtmfGenerator.GetSampleRate(frequency);
3980
andrew@webrtc.org63a50982012-05-02 23:56:37 +00003981 if (frequency != _audioFrame.sample_rate_hz_)
niklase@google.com470e71d2011-07-07 08:21:25 +00003982 {
3983 // Update sample rate of Dtmf tone since the mixing frequency
3984 // has changed.
3985 _inbandDtmfGenerator.SetSampleRate(
pbos@webrtc.org6141e132013-04-09 10:09:10 +00003986 (uint16_t) (_audioFrame.sample_rate_hz_));
niklase@google.com470e71d2011-07-07 08:21:25 +00003987 // Reset the tone to be added taking the new sample rate into
3988 // account.
3989 _inbandDtmfGenerator.ResetTone();
3990 }
andrew@webrtc.orgae1a58b2013-01-22 04:44:30 +00003991
pbos@webrtc.org6141e132013-04-09 10:09:10 +00003992 int16_t toneBuffer[320];
3993 uint16_t toneSamples(0);
niklase@google.com470e71d2011-07-07 08:21:25 +00003994 // Get 10ms tone segment and set time since last tone to zero
3995 if (_inbandDtmfGenerator.Get10msTone(toneBuffer, toneSamples) == -1)
3996 {
3997 WEBRTC_TRACE(kTraceWarning, kTraceVoice,
3998 VoEId(_instanceId, _channelId),
3999 "Channel::EncodeAndSend() inserting Dtmf failed");
4000 return -1;
4001 }
4002
niklas.enbom@webrtc.orgaf26f642011-11-16 12:41:36 +00004003 // Replace mixed audio with DTMF tone.
andrew@webrtc.orgae1a58b2013-01-22 04:44:30 +00004004 for (int sample = 0;
andrew@webrtc.org63a50982012-05-02 23:56:37 +00004005 sample < _audioFrame.samples_per_channel_;
niklas.enbom@webrtc.orgaf26f642011-11-16 12:41:36 +00004006 sample++)
4007 {
andrew@webrtc.orgae1a58b2013-01-22 04:44:30 +00004008 for (int channel = 0;
4009 channel < _audioFrame.num_channels_;
niklas.enbom@webrtc.orgaf26f642011-11-16 12:41:36 +00004010 channel++)
4011 {
andrew@webrtc.orgae1a58b2013-01-22 04:44:30 +00004012 const int index = sample * _audioFrame.num_channels_ + channel;
4013 _audioFrame.data_[index] = toneBuffer[sample];
niklas.enbom@webrtc.orgaf26f642011-11-16 12:41:36 +00004014 }
4015 }
andrew@webrtc.orgae1a58b2013-01-22 04:44:30 +00004016
andrew@webrtc.org63a50982012-05-02 23:56:37 +00004017 assert(_audioFrame.samples_per_channel_ == toneSamples);
niklase@google.com470e71d2011-07-07 08:21:25 +00004018 } else
4019 {
4020 // Add 10ms to "delay-since-last-tone" counter
4021 _inbandDtmfGenerator.UpdateDelaySinceLastTone();
4022 }
4023 return 0;
4024}
4025
pbos@webrtc.org6141e132013-04-09 10:09:10 +00004026int32_t
pkasting@chromium.org4591fbd2014-11-20 22:28:14 +00004027Channel::SendPacketRaw(const void *data, size_t len, bool RTCP)
niklase@google.com470e71d2011-07-07 08:21:25 +00004028{
wu@webrtc.orgfb648da2013-10-18 21:10:51 +00004029 CriticalSectionScoped cs(&_callbackCritSect);
niklase@google.com470e71d2011-07-07 08:21:25 +00004030 if (_transportPtr == NULL)
4031 {
4032 return -1;
4033 }
4034 if (!RTCP)
4035 {
4036 return _transportPtr->SendPacket(_channelId, data, len);
4037 }
4038 else
4039 {
4040 return _transportPtr->SendRTCPPacket(_channelId, data, len);
4041 }
4042}
4043
pwestin@webrtc.org1de01352013-04-11 20:23:35 +00004044// Called for incoming RTP packets after successful RTP header parsing.
4045void Channel::UpdatePacketDelay(uint32_t rtp_timestamp,
4046 uint16_t sequence_number) {
4047 WEBRTC_TRACE(kTraceStream, kTraceVoice, VoEId(_instanceId,_channelId),
4048 "Channel::UpdatePacketDelay(timestamp=%lu, sequenceNumber=%u)",
4049 rtp_timestamp, sequence_number);
niklase@google.com470e71d2011-07-07 08:21:25 +00004050
pwestin@webrtc.org1de01352013-04-11 20:23:35 +00004051 // Get frequency of last received payload
wu@webrtc.org94454b72014-06-05 20:34:08 +00004052 int rtp_receive_frequency = GetPlayoutFrequency();
niklase@google.com470e71d2011-07-07 08:21:25 +00004053
turaj@webrtc.orge46c8d32013-05-22 20:39:43 +00004054 // Update the least required delay.
andrew@webrtc.orgeb524d92013-09-23 23:02:24 +00004055 least_required_delay_ms_ = audio_coding_->LeastRequiredDelayMs();
turaj@webrtc.orge46c8d32013-05-22 20:39:43 +00004056
turaj@webrtc.org167b6df2013-12-13 21:05:07 +00004057 // |jitter_buffer_playout_timestamp_| updated in UpdatePlayoutTimestamp for
4058 // every incoming packet.
4059 uint32_t timestamp_diff_ms = (rtp_timestamp -
4060 jitter_buffer_playout_timestamp_) / (rtp_receive_frequency / 1000);
henrik.lundin@webrtc.orgd6692992014-03-20 12:04:09 +00004061 if (!IsNewerTimestamp(rtp_timestamp, jitter_buffer_playout_timestamp_) ||
4062 timestamp_diff_ms > (2 * kVoiceEngineMaxMinPlayoutDelayMs)) {
4063 // If |jitter_buffer_playout_timestamp_| is newer than the incoming RTP
4064 // timestamp, the resulting difference is negative, but is set to zero.
4065 // This can happen when a network glitch causes a packet to arrive late,
4066 // and during long comfort noise periods with clock drift.
4067 timestamp_diff_ms = 0;
4068 }
niklase@google.com470e71d2011-07-07 08:21:25 +00004069
pwestin@webrtc.org1de01352013-04-11 20:23:35 +00004070 uint16_t packet_delay_ms = (rtp_timestamp - _previousTimestamp) /
4071 (rtp_receive_frequency / 1000);
niklase@google.com470e71d2011-07-07 08:21:25 +00004072
pwestin@webrtc.org1de01352013-04-11 20:23:35 +00004073 _previousTimestamp = rtp_timestamp;
niklase@google.com470e71d2011-07-07 08:21:25 +00004074
pwestin@webrtc.org1de01352013-04-11 20:23:35 +00004075 if (timestamp_diff_ms == 0) return;
niklase@google.com470e71d2011-07-07 08:21:25 +00004076
pwestin@webrtc.org1de01352013-04-11 20:23:35 +00004077 if (packet_delay_ms >= 10 && packet_delay_ms <= 60) {
4078 _recPacketDelayMs = packet_delay_ms;
4079 }
niklase@google.com470e71d2011-07-07 08:21:25 +00004080
pwestin@webrtc.org1de01352013-04-11 20:23:35 +00004081 if (_average_jitter_buffer_delay_us == 0) {
4082 _average_jitter_buffer_delay_us = timestamp_diff_ms * 1000;
4083 return;
4084 }
4085
4086 // Filter average delay value using exponential filter (alpha is
4087 // 7/8). We derive 1000 *_average_jitter_buffer_delay_us here (reduces
4088 // risk of rounding error) and compensate for it in GetDelayEstimate()
4089 // later.
4090 _average_jitter_buffer_delay_us = (_average_jitter_buffer_delay_us * 7 +
4091 1000 * timestamp_diff_ms + 500) / 8;
niklase@google.com470e71d2011-07-07 08:21:25 +00004092}
4093
4094void
4095Channel::RegisterReceiveCodecsToRTPModule()
4096{
4097 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId,_channelId),
4098 "Channel::RegisterReceiveCodecsToRTPModule()");
4099
4100
4101 CodecInst codec;
pbos@webrtc.org6141e132013-04-09 10:09:10 +00004102 const uint8_t nSupportedCodecs = AudioCodingModule::NumberOfCodecs();
niklase@google.com470e71d2011-07-07 08:21:25 +00004103
4104 for (int idx = 0; idx < nSupportedCodecs; idx++)
4105 {
4106 // Open up the RTP/RTCP receiver for all supported codecs
andrew@webrtc.orgeb524d92013-09-23 23:02:24 +00004107 if ((audio_coding_->Codec(idx, &codec) == -1) ||
wu@webrtc.org822fbd82013-08-15 23:38:54 +00004108 (rtp_receiver_->RegisterReceivePayload(
4109 codec.plname,
4110 codec.pltype,
4111 codec.plfreq,
4112 codec.channels,
4113 (codec.rate < 0) ? 0 : codec.rate) == -1))
niklase@google.com470e71d2011-07-07 08:21:25 +00004114 {
4115 WEBRTC_TRACE(
4116 kTraceWarning,
4117 kTraceVoice,
4118 VoEId(_instanceId, _channelId),
4119 "Channel::RegisterReceiveCodecsToRTPModule() unable"
4120 " to register %s (%d/%d/%d/%d) to RTP/RTCP receiver",
4121 codec.plname, codec.pltype, codec.plfreq,
4122 codec.channels, codec.rate);
4123 }
4124 else
4125 {
4126 WEBRTC_TRACE(
4127 kTraceInfo,
4128 kTraceVoice,
4129 VoEId(_instanceId, _channelId),
4130 "Channel::RegisterReceiveCodecsToRTPModule() %s "
wu@webrtc.orgfcd12b32011-09-15 20:49:50 +00004131 "(%d/%d/%d/%d) has been added to the RTP/RTCP "
niklase@google.com470e71d2011-07-07 08:21:25 +00004132 "receiver",
4133 codec.plname, codec.pltype, codec.plfreq,
4134 codec.channels, codec.rate);
4135 }
4136 }
4137}
4138
turaj@webrtc.org8c8ad852013-01-31 18:20:17 +00004139// Assuming this method is called with valid payload type.
turaj@webrtc.org42259e72012-12-11 02:15:12 +00004140int Channel::SetRedPayloadType(int red_payload_type) {
turaj@webrtc.org42259e72012-12-11 02:15:12 +00004141 CodecInst codec;
4142 bool found_red = false;
4143
4144 // Get default RED settings from the ACM database
4145 const int num_codecs = AudioCodingModule::NumberOfCodecs();
4146 for (int idx = 0; idx < num_codecs; idx++) {
andrew@webrtc.orgeb524d92013-09-23 23:02:24 +00004147 audio_coding_->Codec(idx, &codec);
turaj@webrtc.org42259e72012-12-11 02:15:12 +00004148 if (!STR_CASE_CMP(codec.plname, "RED")) {
4149 found_red = true;
4150 break;
4151 }
4152 }
4153
4154 if (!found_red) {
4155 _engineStatisticsPtr->SetLastError(
4156 VE_CODEC_ERROR, kTraceError,
4157 "SetRedPayloadType() RED is not supported");
4158 return -1;
4159 }
4160
turaj@webrtc.org9d532fd2013-01-31 18:34:19 +00004161 codec.pltype = red_payload_type;
andrew@webrtc.orgeb524d92013-09-23 23:02:24 +00004162 if (audio_coding_->RegisterSendCodec(codec) < 0) {
turaj@webrtc.org42259e72012-12-11 02:15:12 +00004163 _engineStatisticsPtr->SetLastError(
4164 VE_AUDIO_CODING_MODULE_ERROR, kTraceError,
4165 "SetRedPayloadType() RED registration in ACM module failed");
4166 return -1;
4167 }
4168
4169 if (_rtpRtcpModule->SetSendREDPayloadType(red_payload_type) != 0) {
4170 _engineStatisticsPtr->SetLastError(
4171 VE_RTP_RTCP_MODULE_ERROR, kTraceError,
4172 "SetRedPayloadType() RED registration in RTP/RTCP module failed");
4173 return -1;
4174 }
4175 return 0;
4176}
4177
wu@webrtc.orgebdb0e32014-03-06 23:49:08 +00004178int Channel::SetSendRtpHeaderExtension(bool enable, RTPExtensionType type,
4179 unsigned char id) {
4180 int error = 0;
4181 _rtpRtcpModule->DeregisterSendRtpHeaderExtension(type);
4182 if (enable) {
4183 error = _rtpRtcpModule->RegisterSendRtpHeaderExtension(type, id);
4184 }
4185 return error;
4186}
minyue@webrtc.orgc1a40a72014-05-28 09:52:06 +00004187
wu@webrtc.org94454b72014-06-05 20:34:08 +00004188int32_t Channel::GetPlayoutFrequency() {
4189 int32_t playout_frequency = audio_coding_->PlayoutFrequency();
4190 CodecInst current_recive_codec;
4191 if (audio_coding_->ReceiveCodec(&current_recive_codec) == 0) {
4192 if (STR_CASE_CMP("G722", current_recive_codec.plname) == 0) {
4193 // Even though the actual sampling rate for G.722 audio is
4194 // 16,000 Hz, the RTP clock rate for the G722 payload format is
4195 // 8,000 Hz because that value was erroneously assigned in
4196 // RFC 1890 and must remain unchanged for backward compatibility.
4197 playout_frequency = 8000;
4198 } else if (STR_CASE_CMP("opus", current_recive_codec.plname) == 0) {
4199 // We are resampling Opus internally to 32,000 Hz until all our
4200 // DSP routines can operate at 48,000 Hz, but the RTP clock
4201 // rate for the Opus payload format is standardized to 48,000 Hz,
4202 // because that is the maximum supported decoding sampling rate.
4203 playout_frequency = 48000;
4204 }
4205 }
4206 return playout_frequency;
4207}
4208
pkasting@chromium.org16825b12015-01-12 21:51:21 +00004209int64_t Channel::GetRTT() const {
minyue@webrtc.org2b58a442014-09-11 07:51:53 +00004210 RTCPMethod method = _rtpRtcpModule->RTCP();
4211 if (method == kRtcpOff) {
minyue@webrtc.org2b58a442014-09-11 07:51:53 +00004212 return 0;
4213 }
4214 std::vector<RTCPReportBlock> report_blocks;
4215 _rtpRtcpModule->RemoteRTCPStat(&report_blocks);
4216 if (report_blocks.empty()) {
minyue@webrtc.org2b58a442014-09-11 07:51:53 +00004217 return 0;
4218 }
4219
4220 uint32_t remoteSSRC = rtp_receiver_->SSRC();
4221 std::vector<RTCPReportBlock>::const_iterator it = report_blocks.begin();
4222 for (; it != report_blocks.end(); ++it) {
4223 if (it->remoteSSRC == remoteSSRC)
4224 break;
4225 }
4226 if (it == report_blocks.end()) {
4227 // We have not received packets with SSRC matching the report blocks.
4228 // To calculate RTT we try with the SSRC of the first report block.
4229 // This is very important for send-only channels where we don't know
4230 // the SSRC of the other end.
4231 remoteSSRC = report_blocks[0].remoteSSRC;
4232 }
pkasting@chromium.org16825b12015-01-12 21:51:21 +00004233 int64_t rtt = 0;
4234 int64_t avg_rtt = 0;
4235 int64_t max_rtt= 0;
4236 int64_t min_rtt = 0;
minyue@webrtc.org2b58a442014-09-11 07:51:53 +00004237 if (_rtpRtcpModule->RTT(remoteSSRC, &rtt, &avg_rtt, &min_rtt, &max_rtt)
4238 != 0) {
minyue@webrtc.org2b58a442014-09-11 07:51:53 +00004239 return 0;
4240 }
pkasting@chromium.org16825b12015-01-12 21:51:21 +00004241 return rtt;
minyue@webrtc.org2b58a442014-09-11 07:51:53 +00004242}
4243
pbos@webrtc.orgd900e8b2013-07-03 15:12:26 +00004244} // namespace voe
4245} // namespace webrtc