blob: 12e66ef3cb12992b8be72768020cf069c82ede7e [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
minyue@webrtc.orge509f942013-09-12 17:03:00 +000013#include "webrtc/common.h"
turaj@webrtc.org6388c3e2013-02-12 21:42:18 +000014#include "webrtc/modules/audio_device/include/audio_device.h"
15#include "webrtc/modules/audio_processing/include/audio_processing.h"
henrik.lundin@webrtc.orgd6692992014-03-20 12:04:09 +000016#include "webrtc/modules/interface/module_common_types.h"
wu@webrtc.org822fbd82013-08-15 23:38:54 +000017#include "webrtc/modules/rtp_rtcp/interface/receive_statistics.h"
wu@webrtc.org82c4b852014-05-20 22:55:01 +000018#include "webrtc/modules/rtp_rtcp/interface/remote_ntp_time_estimator.h"
wu@webrtc.org822fbd82013-08-15 23:38:54 +000019#include "webrtc/modules/rtp_rtcp/interface/rtp_payload_registry.h"
20#include "webrtc/modules/rtp_rtcp/interface/rtp_receiver.h"
21#include "webrtc/modules/rtp_rtcp/source/rtp_receiver_strategy.h"
turaj@webrtc.org6388c3e2013-02-12 21:42:18 +000022#include "webrtc/modules/utility/interface/audio_frame_operations.h"
23#include "webrtc/modules/utility/interface/process_thread.h"
24#include "webrtc/modules/utility/interface/rtp_dump.h"
25#include "webrtc/system_wrappers/interface/critical_section_wrapper.h"
26#include "webrtc/system_wrappers/interface/logging.h"
27#include "webrtc/system_wrappers/interface/trace.h"
solenberg@webrtc.orgb1f50102014-03-24 10:38:25 +000028#include "webrtc/video_engine/include/vie_network.h"
turaj@webrtc.org6388c3e2013-02-12 21:42:18 +000029#include "webrtc/voice_engine/include/voe_base.h"
30#include "webrtc/voice_engine/include/voe_external_media.h"
31#include "webrtc/voice_engine/include/voe_rtp_rtcp.h"
32#include "webrtc/voice_engine/output_mixer.h"
33#include "webrtc/voice_engine/statistics.h"
34#include "webrtc/voice_engine/transmit_mixer.h"
35#include "webrtc/voice_engine/utility.h"
niklase@google.com470e71d2011-07-07 08:21:25 +000036
37#if defined(_WIN32)
38#include <Qos.h>
39#endif
40
andrew@webrtc.org50419b02012-11-14 19:07:54 +000041namespace webrtc {
42namespace voe {
niklase@google.com470e71d2011-07-07 08:21:25 +000043
sprang@webrtc.org54ae4ff2013-12-19 13:26:02 +000044// Extend the default RTCP statistics struct with max_jitter, defined as the
45// maximum jitter value seen in an RTCP report block.
46struct ChannelStatistics : public RtcpStatistics {
47 ChannelStatistics() : rtcp(), max_jitter(0) {}
48
49 RtcpStatistics rtcp;
50 uint32_t max_jitter;
51};
52
53// Statistics callback, called at each generation of a new RTCP report block.
54class StatisticsProxy : public RtcpStatisticsCallback {
55 public:
56 StatisticsProxy(uint32_t ssrc)
57 : stats_lock_(CriticalSectionWrapper::CreateCriticalSection()),
58 ssrc_(ssrc) {}
59 virtual ~StatisticsProxy() {}
60
61 virtual void StatisticsUpdated(const RtcpStatistics& statistics,
62 uint32_t ssrc) OVERRIDE {
63 if (ssrc != ssrc_)
64 return;
65
66 CriticalSectionScoped cs(stats_lock_.get());
67 stats_.rtcp = statistics;
68 if (statistics.jitter > stats_.max_jitter) {
69 stats_.max_jitter = statistics.jitter;
70 }
71 }
72
73 void ResetStatistics() {
74 CriticalSectionScoped cs(stats_lock_.get());
75 stats_ = ChannelStatistics();
76 }
77
78 ChannelStatistics GetStats() {
79 CriticalSectionScoped cs(stats_lock_.get());
80 return stats_;
81 }
82
83 private:
84 // StatisticsUpdated calls are triggered from threads in the RTP module,
85 // while GetStats calls can be triggered from the public voice engine API,
86 // hence synchronization is needed.
87 scoped_ptr<CriticalSectionWrapper> stats_lock_;
88 const uint32_t ssrc_;
89 ChannelStatistics stats_;
90};
91
minyue@webrtc.orgc1a40a72014-05-28 09:52:06 +000092class VoEBitrateObserver : public BitrateObserver {
93 public:
94 explicit VoEBitrateObserver(Channel* owner)
95 : owner_(owner) {}
96 virtual ~VoEBitrateObserver() {}
97
98 // Implements BitrateObserver.
99 virtual void OnNetworkChanged(const uint32_t bitrate_bps,
100 const uint8_t fraction_lost,
101 const uint32_t rtt) OVERRIDE {
102 // |fraction_lost| has a scale of 0 - 255.
103 owner_->OnNetworkChanged(bitrate_bps, fraction_lost, rtt);
104 }
105
106 private:
107 Channel* owner_;
108};
109
pbos@webrtc.org6141e132013-04-09 10:09:10 +0000110int32_t
niklase@google.com470e71d2011-07-07 08:21:25 +0000111Channel::SendData(FrameType frameType,
pbos@webrtc.org6141e132013-04-09 10:09:10 +0000112 uint8_t payloadType,
113 uint32_t timeStamp,
114 const uint8_t* payloadData,
115 uint16_t payloadSize,
niklase@google.com470e71d2011-07-07 08:21:25 +0000116 const RTPFragmentationHeader* fragmentation)
117{
118 WEBRTC_TRACE(kTraceStream, kTraceVoice, VoEId(_instanceId,_channelId),
119 "Channel::SendData(frameType=%u, payloadType=%u, timeStamp=%u,"
120 " payloadSize=%u, fragmentation=0x%x)",
121 frameType, payloadType, timeStamp, payloadSize, fragmentation);
122
123 if (_includeAudioLevelIndication)
124 {
125 // Store current audio level in the RTP/RTCP module.
126 // The level will be used in combination with voice-activity state
127 // (frameType) to add an RTP header extension
andrew@webrtc.org382c0c22014-05-05 18:22:21 +0000128 _rtpRtcpModule->SetAudioLevel(rms_level_.RMS());
niklase@google.com470e71d2011-07-07 08:21:25 +0000129 }
130
131 // Push data from ACM to RTP/RTCP-module to deliver audio frame for
132 // packetization.
133 // This call will trigger Transport::SendPacket() from the RTP/RTCP module.
pwestin@webrtc.org2853dde2012-05-11 11:08:54 +0000134 if (_rtpRtcpModule->SendOutgoingData((FrameType&)frameType,
niklase@google.com470e71d2011-07-07 08:21:25 +0000135 payloadType,
136 timeStamp,
stefan@webrtc.orgddfdfed2012-07-03 13:21:22 +0000137 // Leaving the time when this frame was
138 // received from the capture device as
139 // undefined for voice for now.
140 -1,
niklase@google.com470e71d2011-07-07 08:21:25 +0000141 payloadData,
142 payloadSize,
143 fragmentation) == -1)
144 {
145 _engineStatisticsPtr->SetLastError(
146 VE_RTP_RTCP_MODULE_ERROR, kTraceWarning,
147 "Channel::SendData() failed to send data to RTP/RTCP module");
148 return -1;
149 }
150
151 _lastLocalTimeStamp = timeStamp;
152 _lastPayloadType = payloadType;
153
154 return 0;
155}
156
pbos@webrtc.org6141e132013-04-09 10:09:10 +0000157int32_t
158Channel::InFrameType(int16_t frameType)
niklase@google.com470e71d2011-07-07 08:21:25 +0000159{
160 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId,_channelId),
161 "Channel::InFrameType(frameType=%d)", frameType);
162
mflodman@webrtc.org9a065d12012-03-07 08:12:21 +0000163 CriticalSectionScoped cs(&_callbackCritSect);
niklase@google.com470e71d2011-07-07 08:21:25 +0000164 // 1 indicates speech
165 _sendFrameType = (frameType == 1) ? 1 : 0;
166 return 0;
167}
168
pbos@webrtc.org6141e132013-04-09 10:09:10 +0000169int32_t
pbos@webrtc.org92135212013-05-14 08:31:39 +0000170Channel::OnRxVadDetected(int vadDecision)
niklase@google.com470e71d2011-07-07 08:21:25 +0000171{
172 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId, _channelId),
173 "Channel::OnRxVadDetected(vadDecision=%d)", vadDecision);
174
mflodman@webrtc.org9a065d12012-03-07 08:12:21 +0000175 CriticalSectionScoped cs(&_callbackCritSect);
niklase@google.com470e71d2011-07-07 08:21:25 +0000176 if (_rxVadObserverPtr)
177 {
178 _rxVadObserverPtr->OnRxVad(_channelId, vadDecision);
179 }
180
181 return 0;
182}
183
184int
185Channel::SendPacket(int channel, const void *data, int len)
186{
187 channel = VoEChannelId(channel);
188 assert(channel == _channelId);
189
190 WEBRTC_TRACE(kTraceStream, kTraceVoice, VoEId(_instanceId,_channelId),
191 "Channel::SendPacket(channel=%d, len=%d)", channel, len);
192
wu@webrtc.orgfb648da2013-10-18 21:10:51 +0000193 CriticalSectionScoped cs(&_callbackCritSect);
194
niklase@google.com470e71d2011-07-07 08:21:25 +0000195 if (_transportPtr == NULL)
196 {
197 WEBRTC_TRACE(kTraceError, kTraceVoice, VoEId(_instanceId,_channelId),
198 "Channel::SendPacket() failed to send RTP packet due to"
199 " invalid transport object");
200 return -1;
201 }
202
pbos@webrtc.org6141e132013-04-09 10:09:10 +0000203 uint8_t* bufferToSendPtr = (uint8_t*)data;
204 int32_t bufferLength = len;
niklase@google.com470e71d2011-07-07 08:21:25 +0000205
206 // Dump the RTP packet to a file (if RTP dump is enabled).
pbos@webrtc.org6141e132013-04-09 10:09:10 +0000207 if (_rtpDumpOut.DumpPacket((const uint8_t*)data, len) == -1)
niklase@google.com470e71d2011-07-07 08:21:25 +0000208 {
209 WEBRTC_TRACE(kTraceWarning, kTraceVoice,
210 VoEId(_instanceId,_channelId),
211 "Channel::SendPacket() RTP dump to output file failed");
212 }
213
wu@webrtc.orgfb648da2013-10-18 21:10:51 +0000214 int n = _transportPtr->SendPacket(channel, bufferToSendPtr,
215 bufferLength);
216 if (n < 0) {
217 std::string transport_name =
218 _externalTransport ? "external transport" : "WebRtc sockets";
219 WEBRTC_TRACE(kTraceError, kTraceVoice,
220 VoEId(_instanceId,_channelId),
221 "Channel::SendPacket() RTP transmission using %s failed",
222 transport_name.c_str());
223 return -1;
niklase@google.com470e71d2011-07-07 08:21:25 +0000224 }
wu@webrtc.orgfb648da2013-10-18 21:10:51 +0000225 return n;
niklase@google.com470e71d2011-07-07 08:21:25 +0000226}
227
228int
229Channel::SendRTCPPacket(int channel, const void *data, int len)
230{
231 channel = VoEChannelId(channel);
232 assert(channel == _channelId);
233
234 WEBRTC_TRACE(kTraceStream, kTraceVoice, VoEId(_instanceId,_channelId),
235 "Channel::SendRTCPPacket(channel=%d, len=%d)", channel, len);
236
wu@webrtc.orgfb648da2013-10-18 21:10:51 +0000237 CriticalSectionScoped cs(&_callbackCritSect);
238 if (_transportPtr == NULL)
niklase@google.com470e71d2011-07-07 08:21:25 +0000239 {
wu@webrtc.orgfb648da2013-10-18 21:10:51 +0000240 WEBRTC_TRACE(kTraceError, kTraceVoice,
241 VoEId(_instanceId,_channelId),
242 "Channel::SendRTCPPacket() failed to send RTCP packet"
243 " due to invalid transport object");
244 return -1;
niklase@google.com470e71d2011-07-07 08:21:25 +0000245 }
246
pbos@webrtc.org6141e132013-04-09 10:09:10 +0000247 uint8_t* bufferToSendPtr = (uint8_t*)data;
248 int32_t bufferLength = len;
niklase@google.com470e71d2011-07-07 08:21:25 +0000249
250 // Dump the RTCP packet to a file (if RTP dump is enabled).
pbos@webrtc.org6141e132013-04-09 10:09:10 +0000251 if (_rtpDumpOut.DumpPacket((const uint8_t*)data, len) == -1)
niklase@google.com470e71d2011-07-07 08:21:25 +0000252 {
253 WEBRTC_TRACE(kTraceWarning, kTraceVoice,
254 VoEId(_instanceId,_channelId),
255 "Channel::SendPacket() RTCP dump to output file failed");
256 }
257
wu@webrtc.orgfb648da2013-10-18 21:10:51 +0000258 int n = _transportPtr->SendRTCPPacket(channel,
259 bufferToSendPtr,
260 bufferLength);
261 if (n < 0) {
262 std::string transport_name =
263 _externalTransport ? "external transport" : "WebRtc sockets";
264 WEBRTC_TRACE(kTraceInfo, kTraceVoice,
265 VoEId(_instanceId,_channelId),
266 "Channel::SendRTCPPacket() transmission using %s failed",
267 transport_name.c_str());
268 return -1;
niklase@google.com470e71d2011-07-07 08:21:25 +0000269 }
wu@webrtc.orgfb648da2013-10-18 21:10:51 +0000270 return n;
niklase@google.com470e71d2011-07-07 08:21:25 +0000271}
272
273void
pbos@webrtc.org92135212013-05-14 08:31:39 +0000274Channel::OnPlayTelephoneEvent(int32_t id,
275 uint8_t event,
276 uint16_t lengthMs,
277 uint8_t volume)
niklase@google.com470e71d2011-07-07 08:21:25 +0000278{
279 WEBRTC_TRACE(kTraceStream, kTraceVoice, VoEId(_instanceId,_channelId),
280 "Channel::OnPlayTelephoneEvent(id=%d, event=%u, lengthMs=%u,"
wu@webrtc.orgfcd12b32011-09-15 20:49:50 +0000281 " volume=%u)", id, event, lengthMs, volume);
niklase@google.com470e71d2011-07-07 08:21:25 +0000282
283 if (!_playOutbandDtmfEvent || (event > 15))
284 {
285 // Ignore callback since feedback is disabled or event is not a
286 // Dtmf tone event.
287 return;
288 }
289
290 assert(_outputMixerPtr != NULL);
291
292 // Start playing out the Dtmf tone (if playout is enabled).
293 // Reduce length of tone with 80ms to the reduce risk of echo.
294 _outputMixerPtr->PlayDtmfTone(event, lengthMs - 80, volume);
295}
296
297void
stefan@webrtc.org286fe0b2013-08-21 20:58:21 +0000298Channel::OnIncomingSSRCChanged(int32_t id, uint32_t ssrc)
niklase@google.com470e71d2011-07-07 08:21:25 +0000299{
300 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId,_channelId),
301 "Channel::OnIncomingSSRCChanged(id=%d, SSRC=%d)",
stefan@webrtc.org286fe0b2013-08-21 20:58:21 +0000302 id, ssrc);
niklase@google.com470e71d2011-07-07 08:21:25 +0000303
dwkang@webrtc.orgb295a3f2013-08-29 07:34:12 +0000304 // Update ssrc so that NTP for AV sync can be updated.
305 _rtpRtcpModule->SetRemoteSSRC(ssrc);
niklase@google.com470e71d2011-07-07 08:21:25 +0000306}
307
pbos@webrtc.org92135212013-05-14 08:31:39 +0000308void Channel::OnIncomingCSRCChanged(int32_t id,
309 uint32_t CSRC,
310 bool added)
niklase@google.com470e71d2011-07-07 08:21:25 +0000311{
312 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId,_channelId),
313 "Channel::OnIncomingCSRCChanged(id=%d, CSRC=%d, added=%d)",
314 id, CSRC, added);
niklase@google.com470e71d2011-07-07 08:21:25 +0000315}
316
stefan@webrtc.org286fe0b2013-08-21 20:58:21 +0000317void Channel::ResetStatistics(uint32_t ssrc) {
318 StreamStatistician* statistician =
319 rtp_receive_statistics_->GetStatistician(ssrc);
320 if (statistician) {
321 statistician->ResetStatistics();
322 }
sprang@webrtc.org54ae4ff2013-12-19 13:26:02 +0000323 statistics_proxy_->ResetStatistics();
wu@webrtc.org822fbd82013-08-15 23:38:54 +0000324}
325
niklase@google.com470e71d2011-07-07 08:21:25 +0000326void
pbos@webrtc.org92135212013-05-14 08:31:39 +0000327Channel::OnApplicationDataReceived(int32_t id,
328 uint8_t subType,
329 uint32_t name,
330 uint16_t length,
pbos@webrtc.org6141e132013-04-09 10:09:10 +0000331 const uint8_t* data)
niklase@google.com470e71d2011-07-07 08:21:25 +0000332{
333 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId,_channelId),
334 "Channel::OnApplicationDataReceived(id=%d, subType=%u,"
335 " name=%u, length=%u)",
336 id, subType, name, length);
337
pbos@webrtc.org6141e132013-04-09 10:09:10 +0000338 int32_t channel = VoEChannelId(id);
niklase@google.com470e71d2011-07-07 08:21:25 +0000339 assert(channel == _channelId);
340
341 if (_rtcpObserver)
342 {
mflodman@webrtc.org9a065d12012-03-07 08:12:21 +0000343 CriticalSectionScoped cs(&_callbackCritSect);
niklase@google.com470e71d2011-07-07 08:21:25 +0000344
345 if (_rtcpObserverPtr)
346 {
347 _rtcpObserverPtr->OnApplicationDataReceived(channel,
348 subType,
349 name,
350 data,
351 length);
352 }
353 }
354}
355
pbos@webrtc.org6141e132013-04-09 10:09:10 +0000356int32_t
niklase@google.com470e71d2011-07-07 08:21:25 +0000357Channel::OnInitializeDecoder(
pbos@webrtc.org92135212013-05-14 08:31:39 +0000358 int32_t id,
359 int8_t payloadType,
leozwang@webrtc.org813e4b02012-03-01 18:34:25 +0000360 const char payloadName[RTP_PAYLOAD_NAME_SIZE],
pbos@webrtc.org92135212013-05-14 08:31:39 +0000361 int frequency,
362 uint8_t channels,
363 uint32_t rate)
niklase@google.com470e71d2011-07-07 08:21:25 +0000364{
365 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId,_channelId),
366 "Channel::OnInitializeDecoder(id=%d, payloadType=%d, "
367 "payloadName=%s, frequency=%u, channels=%u, rate=%u)",
368 id, payloadType, payloadName, frequency, channels, rate);
369
andrew@webrtc.orgceb148c2011-08-23 17:53:54 +0000370 assert(VoEChannelId(id) == _channelId);
niklase@google.com470e71d2011-07-07 08:21:25 +0000371
henrika@webrtc.orgf75901f2012-01-16 08:45:42 +0000372 CodecInst receiveCodec = {0};
373 CodecInst dummyCodec = {0};
niklase@google.com470e71d2011-07-07 08:21:25 +0000374
375 receiveCodec.pltype = payloadType;
niklase@google.com470e71d2011-07-07 08:21:25 +0000376 receiveCodec.plfreq = frequency;
377 receiveCodec.channels = channels;
378 receiveCodec.rate = rate;
henrika@webrtc.orgf75901f2012-01-16 08:45:42 +0000379 strncpy(receiveCodec.plname, payloadName, RTP_PAYLOAD_NAME_SIZE - 1);
andrew@webrtc.orgae1a58b2013-01-22 04:44:30 +0000380
andrew@webrtc.orgeb524d92013-09-23 23:02:24 +0000381 audio_coding_->Codec(payloadName, &dummyCodec, frequency, channels);
niklase@google.com470e71d2011-07-07 08:21:25 +0000382 receiveCodec.pacsize = dummyCodec.pacsize;
383
384 // Register the new codec to the ACM
andrew@webrtc.orgeb524d92013-09-23 23:02:24 +0000385 if (audio_coding_->RegisterReceiveCodec(receiveCodec) == -1)
niklase@google.com470e71d2011-07-07 08:21:25 +0000386 {
387 WEBRTC_TRACE(kTraceWarning, kTraceVoice,
andrew@webrtc.orgceb148c2011-08-23 17:53:54 +0000388 VoEId(_instanceId, _channelId),
niklase@google.com470e71d2011-07-07 08:21:25 +0000389 "Channel::OnInitializeDecoder() invalid codec ("
390 "pt=%d, name=%s) received - 1", payloadType, payloadName);
391 _engineStatisticsPtr->SetLastError(VE_AUDIO_CODING_MODULE_ERROR);
392 return -1;
393 }
394
395 return 0;
396}
397
398void
pbos@webrtc.org92135212013-05-14 08:31:39 +0000399Channel::OnPacketTimeout(int32_t id)
niklase@google.com470e71d2011-07-07 08:21:25 +0000400{
401 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId,_channelId),
402 "Channel::OnPacketTimeout(id=%d)", id);
403
mflodman@webrtc.org9a065d12012-03-07 08:12:21 +0000404 CriticalSectionScoped cs(_callbackCritSectPtr);
niklase@google.com470e71d2011-07-07 08:21:25 +0000405 if (_voiceEngineObserverPtr)
406 {
henrika@webrtc.org944cbeb2014-03-18 10:32:33 +0000407 if (channel_state_.Get().receiving || _externalTransport)
niklase@google.com470e71d2011-07-07 08:21:25 +0000408 {
pbos@webrtc.org6141e132013-04-09 10:09:10 +0000409 int32_t channel = VoEChannelId(id);
niklase@google.com470e71d2011-07-07 08:21:25 +0000410 assert(channel == _channelId);
411 // Ensure that next OnReceivedPacket() callback will trigger
412 // a VE_PACKET_RECEIPT_RESTARTED callback.
413 _rtpPacketTimedOut = true;
414 // Deliver callback to the observer
415 WEBRTC_TRACE(kTraceInfo, kTraceVoice,
416 VoEId(_instanceId,_channelId),
417 "Channel::OnPacketTimeout() => "
418 "CallbackOnError(VE_RECEIVE_PACKET_TIMEOUT)");
419 _voiceEngineObserverPtr->CallbackOnError(channel,
420 VE_RECEIVE_PACKET_TIMEOUT);
421 }
422 }
423}
424
425void
pbos@webrtc.org92135212013-05-14 08:31:39 +0000426Channel::OnReceivedPacket(int32_t id,
427 RtpRtcpPacketType packetType)
niklase@google.com470e71d2011-07-07 08:21:25 +0000428{
429 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId,_channelId),
430 "Channel::OnReceivedPacket(id=%d, packetType=%d)",
431 id, packetType);
432
andrew@webrtc.orgceb148c2011-08-23 17:53:54 +0000433 assert(VoEChannelId(id) == _channelId);
niklase@google.com470e71d2011-07-07 08:21:25 +0000434
435 // Notify only for the case when we have restarted an RTP session.
436 if (_rtpPacketTimedOut && (kPacketRtp == packetType))
437 {
mflodman@webrtc.org9a065d12012-03-07 08:12:21 +0000438 CriticalSectionScoped cs(_callbackCritSectPtr);
niklase@google.com470e71d2011-07-07 08:21:25 +0000439 if (_voiceEngineObserverPtr)
440 {
pbos@webrtc.org6141e132013-04-09 10:09:10 +0000441 int32_t channel = VoEChannelId(id);
niklase@google.com470e71d2011-07-07 08:21:25 +0000442 assert(channel == _channelId);
443 // Reset timeout mechanism
444 _rtpPacketTimedOut = false;
445 // Deliver callback to the observer
446 WEBRTC_TRACE(kTraceInfo, kTraceVoice,
447 VoEId(_instanceId,_channelId),
448 "Channel::OnPacketTimeout() =>"
449 " CallbackOnError(VE_PACKET_RECEIPT_RESTARTED)");
450 _voiceEngineObserverPtr->CallbackOnError(
451 channel,
452 VE_PACKET_RECEIPT_RESTARTED);
453 }
454 }
455}
456
457void
pbos@webrtc.org92135212013-05-14 08:31:39 +0000458Channel::OnPeriodicDeadOrAlive(int32_t id,
459 RTPAliveType alive)
niklase@google.com470e71d2011-07-07 08:21:25 +0000460{
461 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId,_channelId),
462 "Channel::OnPeriodicDeadOrAlive(id=%d, alive=%d)", id, alive);
463
henrika@webrtc.org19da7192013-04-05 14:34:57 +0000464 {
465 CriticalSectionScoped cs(&_callbackCritSect);
466 if (!_connectionObserver)
467 return;
468 }
niklase@google.com470e71d2011-07-07 08:21:25 +0000469
pbos@webrtc.org6141e132013-04-09 10:09:10 +0000470 int32_t channel = VoEChannelId(id);
niklase@google.com470e71d2011-07-07 08:21:25 +0000471 assert(channel == _channelId);
472
473 // Use Alive as default to limit risk of false Dead detections
474 bool isAlive(true);
475
476 // Always mark the connection as Dead when the module reports kRtpDead
477 if (kRtpDead == alive)
478 {
479 isAlive = false;
480 }
481
482 // It is possible that the connection is alive even if no RTP packet has
483 // been received for a long time since the other side might use VAD/DTX
484 // and a low SID-packet update rate.
henrika@webrtc.org944cbeb2014-03-18 10:32:33 +0000485 if ((kRtpNoRtp == alive) && channel_state_.Get().playing)
niklase@google.com470e71d2011-07-07 08:21:25 +0000486 {
487 // Detect Alive for all NetEQ states except for the case when we are
488 // in PLC_CNG state.
489 // PLC_CNG <=> background noise only due to long expand or error.
490 // Note that, the case where the other side stops sending during CNG
491 // state will be detected as Alive. Dead is is not set until after
492 // missing RTCP packets for at least twelve seconds (handled
493 // internally by the RTP/RTCP module).
494 isAlive = (_outputSpeechType != AudioFrame::kPLCCNG);
495 }
496
niklase@google.com470e71d2011-07-07 08:21:25 +0000497 // Send callback to the registered observer
498 if (_connectionObserver)
499 {
mflodman@webrtc.org9a065d12012-03-07 08:12:21 +0000500 CriticalSectionScoped cs(&_callbackCritSect);
niklase@google.com470e71d2011-07-07 08:21:25 +0000501 if (_connectionObserverPtr)
502 {
503 _connectionObserverPtr->OnPeriodicDeadOrAlive(channel, isAlive);
504 }
505 }
506}
507
pbos@webrtc.org6141e132013-04-09 10:09:10 +0000508int32_t
509Channel::OnReceivedPayloadData(const uint8_t* payloadData,
pbos@webrtc.org92135212013-05-14 08:31:39 +0000510 uint16_t payloadSize,
niklase@google.com470e71d2011-07-07 08:21:25 +0000511 const WebRtcRTPHeader* rtpHeader)
512{
513 WEBRTC_TRACE(kTraceStream, kTraceVoice, VoEId(_instanceId,_channelId),
514 "Channel::OnReceivedPayloadData(payloadSize=%d,"
515 " payloadType=%u, audioChannel=%u)",
516 payloadSize,
517 rtpHeader->header.payloadType,
518 rtpHeader->type.Audio.channel);
519
henrika@webrtc.org944cbeb2014-03-18 10:32:33 +0000520 if (!channel_state_.Get().playing)
niklase@google.com470e71d2011-07-07 08:21:25 +0000521 {
522 // Avoid inserting into NetEQ when we are not playing. Count the
523 // packet as discarded.
524 WEBRTC_TRACE(kTraceStream, kTraceVoice,
525 VoEId(_instanceId, _channelId),
526 "received packet is discarded since playing is not"
527 " activated");
528 _numberOfDiscardedPackets++;
529 return 0;
530 }
531
532 // Push the incoming payload (parsed and ready for decoding) into the ACM
andrew@webrtc.orgeb524d92013-09-23 23:02:24 +0000533 if (audio_coding_->IncomingPacket(payloadData,
534 payloadSize,
535 *rtpHeader) != 0)
niklase@google.com470e71d2011-07-07 08:21:25 +0000536 {
537 _engineStatisticsPtr->SetLastError(
538 VE_AUDIO_CODING_MODULE_ERROR, kTraceWarning,
539 "Channel::OnReceivedPayloadData() unable to push data to the ACM");
540 return -1;
541 }
542
pwestin@webrtc.orgd30859e2013-06-06 21:09:01 +0000543 // Update the packet delay.
niklase@google.com470e71d2011-07-07 08:21:25 +0000544 UpdatePacketDelay(rtpHeader->header.timestamp,
545 rtpHeader->header.sequenceNumber);
pwestin@webrtc.orgd30859e2013-06-06 21:09:01 +0000546
wu@webrtc.org822fbd82013-08-15 23:38:54 +0000547 uint16_t round_trip_time = 0;
548 _rtpRtcpModule->RTT(rtp_receiver_->SSRC(), &round_trip_time,
549 NULL, NULL, NULL);
pwestin@webrtc.orgd30859e2013-06-06 21:09:01 +0000550
andrew@webrtc.orgeb524d92013-09-23 23:02:24 +0000551 std::vector<uint16_t> nack_list = audio_coding_->GetNackList(
wu@webrtc.org822fbd82013-08-15 23:38:54 +0000552 round_trip_time);
553 if (!nack_list.empty()) {
554 // Can't use nack_list.data() since it's not supported by all
555 // compilers.
556 ResendPackets(&(nack_list[0]), static_cast<int>(nack_list.size()));
pwestin@webrtc.orgd30859e2013-06-06 21:09:01 +0000557 }
niklase@google.com470e71d2011-07-07 08:21:25 +0000558 return 0;
559}
560
stefan@webrtc.org7bb8f022013-09-06 13:40:11 +0000561bool Channel::OnRecoveredPacket(const uint8_t* rtp_packet,
562 int rtp_packet_length) {
563 RTPHeader header;
564 if (!rtp_header_parser_->Parse(rtp_packet, rtp_packet_length, &header)) {
565 WEBRTC_TRACE(kTraceDebug, webrtc::kTraceVoice, _channelId,
566 "IncomingPacket invalid RTP header");
567 return false;
568 }
569 header.payload_type_frequency =
570 rtp_payload_registry_->GetPayloadTypeFrequency(header.payloadType);
571 if (header.payload_type_frequency < 0)
572 return false;
573 return ReceivePacket(rtp_packet, rtp_packet_length, header, false);
574}
575
pbos@webrtc.org92135212013-05-14 08:31:39 +0000576int32_t Channel::GetAudioFrame(int32_t id, AudioFrame& audioFrame)
niklase@google.com470e71d2011-07-07 08:21:25 +0000577{
578 WEBRTC_TRACE(kTraceStream, kTraceVoice, VoEId(_instanceId,_channelId),
579 "Channel::GetAudioFrame(id=%d)", id);
580
581 // Get 10ms raw PCM data from the ACM (mixer limits output frequency)
andrew@webrtc.orgeb524d92013-09-23 23:02:24 +0000582 if (audio_coding_->PlayoutData10Ms(audioFrame.sample_rate_hz_,
583 &audioFrame) == -1)
niklase@google.com470e71d2011-07-07 08:21:25 +0000584 {
585 WEBRTC_TRACE(kTraceError, kTraceVoice,
586 VoEId(_instanceId,_channelId),
587 "Channel::GetAudioFrame() PlayoutData10Ms() failed!");
andrew@webrtc.org7859e102012-01-13 00:30:11 +0000588 // In all likelihood, the audio in this frame is garbage. We return an
589 // error so that the audio mixer module doesn't add it to the mix. As
590 // a result, it won't be played out and the actions skipped here are
591 // irrelevant.
592 return -1;
niklase@google.com470e71d2011-07-07 08:21:25 +0000593 }
594
595 if (_RxVadDetection)
596 {
597 UpdateRxVadDetection(audioFrame);
598 }
599
600 // Convert module ID to internal VoE channel ID
andrew@webrtc.org63a50982012-05-02 23:56:37 +0000601 audioFrame.id_ = VoEChannelId(audioFrame.id_);
niklase@google.com470e71d2011-07-07 08:21:25 +0000602 // Store speech type for dead-or-alive detection
andrew@webrtc.org63a50982012-05-02 23:56:37 +0000603 _outputSpeechType = audioFrame.speech_type_;
niklase@google.com470e71d2011-07-07 08:21:25 +0000604
henrika@webrtc.org944cbeb2014-03-18 10:32:33 +0000605 ChannelState::State state = channel_state_.Get();
606
607 if (state.rx_apm_is_enabled) {
andrew@webrtc.org60730cf2014-01-07 17:45:09 +0000608 int err = rx_audioproc_->ProcessStream(&audioFrame);
609 if (err) {
610 LOG(LS_ERROR) << "ProcessStream() error: " << err;
611 assert(false);
612 }
niklase@google.com470e71d2011-07-07 08:21:25 +0000613 }
614
wu@webrtc.org63420662013-10-17 18:28:55 +0000615 float output_gain = 1.0f;
616 float left_pan = 1.0f;
617 float right_pan = 1.0f;
niklase@google.com470e71d2011-07-07 08:21:25 +0000618 {
wu@webrtc.org63420662013-10-17 18:28:55 +0000619 CriticalSectionScoped cs(&volume_settings_critsect_);
620 output_gain = _outputGain;
621 left_pan = _panLeft;
622 right_pan= _panRight;
623 }
624
625 // Output volume scaling
626 if (output_gain < 0.99f || output_gain > 1.01f)
627 {
628 AudioFrameOperations::ScaleWithSat(output_gain, audioFrame);
niklase@google.com470e71d2011-07-07 08:21:25 +0000629 }
630
631 // Scale left and/or right channel(s) if stereo and master balance is
632 // active
633
wu@webrtc.org63420662013-10-17 18:28:55 +0000634 if (left_pan != 1.0f || right_pan != 1.0f)
niklase@google.com470e71d2011-07-07 08:21:25 +0000635 {
andrew@webrtc.org63a50982012-05-02 23:56:37 +0000636 if (audioFrame.num_channels_ == 1)
niklase@google.com470e71d2011-07-07 08:21:25 +0000637 {
638 // Emulate stereo mode since panning is active.
639 // The mono signal is copied to both left and right channels here.
andrew@webrtc.org4ecea3e2012-06-27 03:25:31 +0000640 AudioFrameOperations::MonoToStereo(&audioFrame);
niklase@google.com470e71d2011-07-07 08:21:25 +0000641 }
642 // For true stereo mode (when we are receiving a stereo signal), no
643 // action is needed.
644
645 // Do the panning operation (the audio frame contains stereo at this
646 // stage)
wu@webrtc.org63420662013-10-17 18:28:55 +0000647 AudioFrameOperations::Scale(left_pan, right_pan, audioFrame);
niklase@google.com470e71d2011-07-07 08:21:25 +0000648 }
649
650 // Mix decoded PCM output with file if file mixing is enabled
henrika@webrtc.org944cbeb2014-03-18 10:32:33 +0000651 if (state.output_file_playing)
niklase@google.com470e71d2011-07-07 08:21:25 +0000652 {
andrew@webrtc.org63a50982012-05-02 23:56:37 +0000653 MixAudioWithFile(audioFrame, audioFrame.sample_rate_hz_);
niklase@google.com470e71d2011-07-07 08:21:25 +0000654 }
655
niklase@google.com470e71d2011-07-07 08:21:25 +0000656 // External media
657 if (_outputExternalMedia)
658 {
mflodman@webrtc.org9a065d12012-03-07 08:12:21 +0000659 CriticalSectionScoped cs(&_callbackCritSect);
andrew@webrtc.org63a50982012-05-02 23:56:37 +0000660 const bool isStereo = (audioFrame.num_channels_ == 2);
niklase@google.com470e71d2011-07-07 08:21:25 +0000661 if (_outputExternalMediaCallbackPtr)
662 {
663 _outputExternalMediaCallbackPtr->Process(
664 _channelId,
665 kPlaybackPerChannel,
pbos@webrtc.org6141e132013-04-09 10:09:10 +0000666 (int16_t*)audioFrame.data_,
andrew@webrtc.org63a50982012-05-02 23:56:37 +0000667 audioFrame.samples_per_channel_,
668 audioFrame.sample_rate_hz_,
niklase@google.com470e71d2011-07-07 08:21:25 +0000669 isStereo);
670 }
671 }
672
673 // Record playout if enabled
674 {
mflodman@webrtc.org9a065d12012-03-07 08:12:21 +0000675 CriticalSectionScoped cs(&_fileCritSect);
niklase@google.com470e71d2011-07-07 08:21:25 +0000676
677 if (_outputFileRecording && _outputFileRecorderPtr)
678 {
niklas.enbom@webrtc.org5398d952012-03-26 08:11:25 +0000679 _outputFileRecorderPtr->RecordAudioToFile(audioFrame);
niklase@google.com470e71d2011-07-07 08:21:25 +0000680 }
681 }
682
683 // Measure audio level (0-9)
684 _outputAudioLevel.ComputeLevel(audioFrame);
685
wu@webrtc.org82c4b852014-05-20 22:55:01 +0000686 audioFrame.ntp_time_ms_ = ntp_estimator_->Estimate(audioFrame.timestamp_);
wu@webrtc.orgcb711f72014-05-19 17:39:11 +0000687
688 if (!first_frame_arrived_) {
689 first_frame_arrived_ = true;
690 capture_start_rtp_time_stamp_ = audioFrame.timestamp_;
691 } else {
692 // |ntp_time_ms_| won't be valid until at least 2 RTCP SRs are received.
693 if (audioFrame.ntp_time_ms_ > 0) {
694 // Compute |capture_start_ntp_time_ms_| so that
695 // |capture_start_ntp_time_ms_| + |elapsed_time_ms| == |ntp_time_ms_|
696 CriticalSectionScoped lock(ts_stats_lock_.get());
697 uint32_t elapsed_time_ms =
698 (audioFrame.timestamp_ - capture_start_rtp_time_stamp_) /
699 (audioFrame.sample_rate_hz_ * 1000);
700 capture_start_ntp_time_ms_ = audioFrame.ntp_time_ms_ - elapsed_time_ms;
701 }
702 }
703
niklase@google.com470e71d2011-07-07 08:21:25 +0000704 return 0;
705}
706
pbos@webrtc.org6141e132013-04-09 10:09:10 +0000707int32_t
pbos@webrtc.org92135212013-05-14 08:31:39 +0000708Channel::NeededFrequency(int32_t id)
niklase@google.com470e71d2011-07-07 08:21:25 +0000709{
710 WEBRTC_TRACE(kTraceStream, kTraceVoice, VoEId(_instanceId,_channelId),
711 "Channel::NeededFrequency(id=%d)", id);
712
713 int highestNeeded = 0;
714
715 // Determine highest needed receive frequency
andrew@webrtc.orgeb524d92013-09-23 23:02:24 +0000716 int32_t receiveFrequency = audio_coding_->ReceiveFrequency();
niklase@google.com470e71d2011-07-07 08:21:25 +0000717
718 // Return the bigger of playout and receive frequency in the ACM.
andrew@webrtc.orgeb524d92013-09-23 23:02:24 +0000719 if (audio_coding_->PlayoutFrequency() > receiveFrequency)
niklase@google.com470e71d2011-07-07 08:21:25 +0000720 {
andrew@webrtc.orgeb524d92013-09-23 23:02:24 +0000721 highestNeeded = audio_coding_->PlayoutFrequency();
niklase@google.com470e71d2011-07-07 08:21:25 +0000722 }
723 else
724 {
725 highestNeeded = receiveFrequency;
726 }
727
728 // Special case, if we're playing a file on the playout side
729 // we take that frequency into consideration as well
730 // This is not needed on sending side, since the codec will
731 // limit the spectrum anyway.
henrika@webrtc.org944cbeb2014-03-18 10:32:33 +0000732 if (channel_state_.Get().output_file_playing)
niklase@google.com470e71d2011-07-07 08:21:25 +0000733 {
mflodman@webrtc.org9a065d12012-03-07 08:12:21 +0000734 CriticalSectionScoped cs(&_fileCritSect);
henrika@webrtc.org944cbeb2014-03-18 10:32:33 +0000735 if (_outputFilePlayerPtr)
niklase@google.com470e71d2011-07-07 08:21:25 +0000736 {
737 if(_outputFilePlayerPtr->Frequency()>highestNeeded)
738 {
739 highestNeeded=_outputFilePlayerPtr->Frequency();
740 }
741 }
742 }
743
744 return(highestNeeded);
745}
746
pbos@webrtc.org6141e132013-04-09 10:09:10 +0000747int32_t
niklase@google.com470e71d2011-07-07 08:21:25 +0000748Channel::CreateChannel(Channel*& channel,
pbos@webrtc.org92135212013-05-14 08:31:39 +0000749 int32_t channelId,
minyue@webrtc.orge509f942013-09-12 17:03:00 +0000750 uint32_t instanceId,
751 const Config& config)
niklase@google.com470e71d2011-07-07 08:21:25 +0000752{
753 WEBRTC_TRACE(kTraceMemory, kTraceVoice, VoEId(instanceId,channelId),
754 "Channel::CreateChannel(channelId=%d, instanceId=%d)",
755 channelId, instanceId);
756
minyue@webrtc.orge509f942013-09-12 17:03:00 +0000757 channel = new Channel(channelId, instanceId, config);
niklase@google.com470e71d2011-07-07 08:21:25 +0000758 if (channel == NULL)
759 {
760 WEBRTC_TRACE(kTraceMemory, kTraceVoice,
761 VoEId(instanceId,channelId),
762 "Channel::CreateChannel() unable to allocate memory for"
763 " channel");
764 return -1;
765 }
766 return 0;
767}
768
769void
pbos@webrtc.org92135212013-05-14 08:31:39 +0000770Channel::PlayNotification(int32_t id, uint32_t durationMs)
niklase@google.com470e71d2011-07-07 08:21:25 +0000771{
772 WEBRTC_TRACE(kTraceStream, kTraceVoice, VoEId(_instanceId,_channelId),
773 "Channel::PlayNotification(id=%d, durationMs=%d)",
774 id, durationMs);
775
776 // Not implement yet
777}
778
779void
pbos@webrtc.org92135212013-05-14 08:31:39 +0000780Channel::RecordNotification(int32_t id, uint32_t durationMs)
niklase@google.com470e71d2011-07-07 08:21:25 +0000781{
782 WEBRTC_TRACE(kTraceStream, kTraceVoice, VoEId(_instanceId,_channelId),
783 "Channel::RecordNotification(id=%d, durationMs=%d)",
784 id, durationMs);
785
786 // Not implement yet
787}
788
789void
pbos@webrtc.org92135212013-05-14 08:31:39 +0000790Channel::PlayFileEnded(int32_t id)
niklase@google.com470e71d2011-07-07 08:21:25 +0000791{
792 WEBRTC_TRACE(kTraceStream, kTraceVoice, VoEId(_instanceId,_channelId),
793 "Channel::PlayFileEnded(id=%d)", id);
794
795 if (id == _inputFilePlayerId)
796 {
henrika@webrtc.org944cbeb2014-03-18 10:32:33 +0000797 channel_state_.SetInputFilePlaying(false);
niklase@google.com470e71d2011-07-07 08:21:25 +0000798 WEBRTC_TRACE(kTraceStateInfo, kTraceVoice,
799 VoEId(_instanceId,_channelId),
800 "Channel::PlayFileEnded() => input file player module is"
801 " shutdown");
802 }
803 else if (id == _outputFilePlayerId)
804 {
henrika@webrtc.org944cbeb2014-03-18 10:32:33 +0000805 channel_state_.SetOutputFilePlaying(false);
niklase@google.com470e71d2011-07-07 08:21:25 +0000806 WEBRTC_TRACE(kTraceStateInfo, kTraceVoice,
807 VoEId(_instanceId,_channelId),
808 "Channel::PlayFileEnded() => output file player module is"
809 " shutdown");
810 }
811}
812
813void
pbos@webrtc.org92135212013-05-14 08:31:39 +0000814Channel::RecordFileEnded(int32_t id)
niklase@google.com470e71d2011-07-07 08:21:25 +0000815{
816 WEBRTC_TRACE(kTraceStream, kTraceVoice, VoEId(_instanceId,_channelId),
817 "Channel::RecordFileEnded(id=%d)", id);
818
819 assert(id == _outputFileRecorderId);
820
mflodman@webrtc.org9a065d12012-03-07 08:12:21 +0000821 CriticalSectionScoped cs(&_fileCritSect);
niklase@google.com470e71d2011-07-07 08:21:25 +0000822
823 _outputFileRecording = false;
824 WEBRTC_TRACE(kTraceStateInfo, kTraceVoice,
825 VoEId(_instanceId,_channelId),
826 "Channel::RecordFileEnded() => output file recorder module is"
827 " shutdown");
828}
829
pbos@webrtc.org92135212013-05-14 08:31:39 +0000830Channel::Channel(int32_t channelId,
minyue@webrtc.orge509f942013-09-12 17:03:00 +0000831 uint32_t instanceId,
832 const Config& config) :
niklase@google.com470e71d2011-07-07 08:21:25 +0000833 _fileCritSect(*CriticalSectionWrapper::CreateCriticalSection()),
834 _callbackCritSect(*CriticalSectionWrapper::CreateCriticalSection()),
wu@webrtc.org63420662013-10-17 18:28:55 +0000835 volume_settings_critsect_(*CriticalSectionWrapper::CreateCriticalSection()),
niklase@google.com470e71d2011-07-07 08:21:25 +0000836 _instanceId(instanceId),
xians@google.com22963ab2011-08-03 12:40:23 +0000837 _channelId(channelId),
stefan@webrtc.orga5cb98c2013-05-29 12:12:51 +0000838 rtp_header_parser_(RtpHeaderParser::Create()),
wu@webrtc.org822fbd82013-08-15 23:38:54 +0000839 rtp_payload_registry_(
andresp@webrtc.orgdc80bae2014-04-08 11:06:12 +0000840 new RTPPayloadRegistry(RTPPayloadStrategy::CreateStrategy(true))),
wu@webrtc.org822fbd82013-08-15 23:38:54 +0000841 rtp_receive_statistics_(ReceiveStatistics::Create(
842 Clock::GetRealTimeClock())),
843 rtp_receiver_(RtpReceiver::CreateAudioReceiver(
844 VoEModuleId(instanceId, channelId), Clock::GetRealTimeClock(), this,
845 this, this, rtp_payload_registry_.get())),
846 telephone_event_handler_(rtp_receiver_->GetTelephoneEventHandler()),
henrik.lundin@webrtc.org34fe0152014-04-22 19:04:34 +0000847 audio_coding_(AudioCodingModule::Create(
xians@google.com22963ab2011-08-03 12:40:23 +0000848 VoEModuleId(instanceId, channelId))),
niklase@google.com470e71d2011-07-07 08:21:25 +0000849 _rtpDumpIn(*RtpDump::CreateRtpDump()),
850 _rtpDumpOut(*RtpDump::CreateRtpDump()),
niklase@google.com470e71d2011-07-07 08:21:25 +0000851 _outputAudioLevel(),
niklase@google.com470e71d2011-07-07 08:21:25 +0000852 _externalTransport(false),
sprang@webrtc.org54ae4ff2013-12-19 13:26:02 +0000853 _audioLevel_dBov(0),
niklase@google.com470e71d2011-07-07 08:21:25 +0000854 _inputFilePlayerPtr(NULL),
855 _outputFilePlayerPtr(NULL),
856 _outputFileRecorderPtr(NULL),
857 // Avoid conflict with other channels by adding 1024 - 1026,
858 // won't use as much as 1024 channels.
859 _inputFilePlayerId(VoEModuleId(instanceId, channelId) + 1024),
860 _outputFilePlayerId(VoEModuleId(instanceId, channelId) + 1025),
861 _outputFileRecorderId(VoEModuleId(instanceId, channelId) + 1026),
niklase@google.com470e71d2011-07-07 08:21:25 +0000862 _outputFileRecording(false),
xians@google.com22963ab2011-08-03 12:40:23 +0000863 _inbandDtmfQueue(VoEModuleId(instanceId, channelId)),
864 _inbandDtmfGenerator(VoEModuleId(instanceId, channelId)),
xians@google.com22963ab2011-08-03 12:40:23 +0000865 _outputExternalMedia(false),
niklase@google.com470e71d2011-07-07 08:21:25 +0000866 _inputExternalMediaCallbackPtr(NULL),
867 _outputExternalMediaCallbackPtr(NULL),
xians@google.com22963ab2011-08-03 12:40:23 +0000868 _timeStamp(0), // This is just an offset, RTP module will add it's own random offset
869 _sendTelephoneEventPayloadType(106),
wu@webrtc.org82c4b852014-05-20 22:55:01 +0000870 ntp_estimator_(new RemoteNtpTimeEstimator(Clock::GetRealTimeClock())),
turaj@webrtc.org167b6df2013-12-13 21:05:07 +0000871 jitter_buffer_playout_timestamp_(0),
pwestin@webrtc.org1de01352013-04-11 20:23:35 +0000872 playout_timestamp_rtp_(0),
873 playout_timestamp_rtcp_(0),
sprang@webrtc.org54ae4ff2013-12-19 13:26:02 +0000874 playout_delay_ms_(0),
xians@google.com22963ab2011-08-03 12:40:23 +0000875 _numberOfDiscardedPackets(0),
xians@webrtc.org09e8c472013-07-31 16:30:19 +0000876 send_sequence_number_(0),
wu@webrtc.orgcb711f72014-05-19 17:39:11 +0000877 ts_stats_lock_(CriticalSectionWrapper::CreateCriticalSection()),
878 first_frame_arrived_(false),
879 capture_start_rtp_time_stamp_(0),
880 capture_start_ntp_time_ms_(-1),
xians@google.com22963ab2011-08-03 12:40:23 +0000881 _engineStatisticsPtr(NULL),
henrika@webrtc.org2919e952012-01-31 08:45:03 +0000882 _outputMixerPtr(NULL),
883 _transmitMixerPtr(NULL),
xians@google.com22963ab2011-08-03 12:40:23 +0000884 _moduleProcessThreadPtr(NULL),
885 _audioDeviceModulePtr(NULL),
886 _voiceEngineObserverPtr(NULL),
887 _callbackCritSectPtr(NULL),
888 _transportPtr(NULL),
xians@google.com22963ab2011-08-03 12:40:23 +0000889 _rxVadObserverPtr(NULL),
890 _oldVadDecision(-1),
891 _sendFrameType(0),
niklase@google.com470e71d2011-07-07 08:21:25 +0000892 _rtcpObserverPtr(NULL),
xians@google.com22963ab2011-08-03 12:40:23 +0000893 _externalPlayout(false),
roosa@google.com1b60ceb2012-12-12 23:00:29 +0000894 _externalMixing(false),
xians@google.com22963ab2011-08-03 12:40:23 +0000895 _mixFileWithMicrophone(false),
xians@google.com22963ab2011-08-03 12:40:23 +0000896 _rtcpObserver(false),
niklase@google.com470e71d2011-07-07 08:21:25 +0000897 _mute(false),
898 _panLeft(1.0f),
899 _panRight(1.0f),
900 _outputGain(1.0f),
901 _playOutbandDtmfEvent(false),
902 _playInbandDtmfEvent(false),
niklase@google.com470e71d2011-07-07 08:21:25 +0000903 _lastLocalTimeStamp(0),
904 _lastPayloadType(0),
xians@google.com22963ab2011-08-03 12:40:23 +0000905 _includeAudioLevelIndication(false),
niklase@google.com470e71d2011-07-07 08:21:25 +0000906 _rtpPacketTimedOut(false),
907 _rtpPacketTimeOutIsEnabled(false),
908 _rtpTimeOutSeconds(0),
909 _connectionObserver(false),
910 _connectionObserverPtr(NULL),
niklase@google.com470e71d2011-07-07 08:21:25 +0000911 _outputSpeechType(AudioFrame::kNormalSpeech),
solenberg@webrtc.orgb1f50102014-03-24 10:38:25 +0000912 vie_network_(NULL),
913 video_channel_(-1),
pwestin@webrtc.org1de01352013-04-11 20:23:35 +0000914 _average_jitter_buffer_delay_us(0),
turaj@webrtc.orge46c8d32013-05-22 20:39:43 +0000915 least_required_delay_ms_(0),
niklase@google.com470e71d2011-07-07 08:21:25 +0000916 _previousTimestamp(0),
917 _recPacketDelayMs(20),
918 _RxVadDetection(false),
niklase@google.com470e71d2011-07-07 08:21:25 +0000919 _rxAgcIsEnabled(false),
stefan@webrtc.org7bb8f022013-09-06 13:40:11 +0000920 _rxNsIsEnabled(false),
minyue@webrtc.orgc1a40a72014-05-28 09:52:06 +0000921 restored_packet_in_use_(false),
922 bitrate_controller_(
923 BitrateController::CreateBitrateController(Clock::GetRealTimeClock(),
924 true)),
925 rtcp_bandwidth_observer_(
926 bitrate_controller_->CreateRtcpBandwidthObserver()),
927 send_bitrate_observer_(new VoEBitrateObserver(this))
niklase@google.com470e71d2011-07-07 08:21:25 +0000928{
929 WEBRTC_TRACE(kTraceMemory, kTraceVoice, VoEId(_instanceId,_channelId),
930 "Channel::Channel() - ctor");
931 _inbandDtmfQueue.ResetDtmf();
932 _inbandDtmfGenerator.Init();
933 _outputAudioLevel.Clear();
934
pwestin@webrtc.org2853dde2012-05-11 11:08:54 +0000935 RtpRtcp::Configuration configuration;
936 configuration.id = VoEModuleId(instanceId, channelId);
937 configuration.audio = true;
pwestin@webrtc.org2853dde2012-05-11 11:08:54 +0000938 configuration.outgoing_transport = this;
939 configuration.rtcp_feedback = this;
940 configuration.audio_messages = this;
wu@webrtc.org822fbd82013-08-15 23:38:54 +0000941 configuration.receive_statistics = rtp_receive_statistics_.get();
minyue@webrtc.orgc1a40a72014-05-28 09:52:06 +0000942 configuration.bandwidth_callback = rtcp_bandwidth_observer_.get();
pwestin@webrtc.org2853dde2012-05-11 11:08:54 +0000943
944 _rtpRtcpModule.reset(RtpRtcp::CreateRtpRtcp(configuration));
sprang@webrtc.org54ae4ff2013-12-19 13:26:02 +0000945
946 statistics_proxy_.reset(new StatisticsProxy(_rtpRtcpModule->SSRC()));
947 rtp_receive_statistics_->RegisterRtcpStatisticsCallback(
948 statistics_proxy_.get());
aluebs@webrtc.orgf927fd62014-04-16 11:58:18 +0000949
950 Config audioproc_config;
951 audioproc_config.Set<ExperimentalAgc>(new ExperimentalAgc(false));
952 rx_audioproc_.reset(AudioProcessing::Create(audioproc_config));
niklase@google.com470e71d2011-07-07 08:21:25 +0000953}
954
955Channel::~Channel()
956{
sprang@webrtc.org54ae4ff2013-12-19 13:26:02 +0000957 rtp_receive_statistics_->RegisterRtcpStatisticsCallback(NULL);
niklase@google.com470e71d2011-07-07 08:21:25 +0000958 WEBRTC_TRACE(kTraceMemory, kTraceVoice, VoEId(_instanceId,_channelId),
959 "Channel::~Channel() - dtor");
960
961 if (_outputExternalMedia)
962 {
963 DeRegisterExternalMediaProcessing(kPlaybackPerChannel);
964 }
henrika@webrtc.org944cbeb2014-03-18 10:32:33 +0000965 if (channel_state_.Get().input_external_media)
niklase@google.com470e71d2011-07-07 08:21:25 +0000966 {
967 DeRegisterExternalMediaProcessing(kRecordingPerChannel);
968 }
969 StopSend();
niklase@google.com470e71d2011-07-07 08:21:25 +0000970 StopPlayout();
971
972 {
mflodman@webrtc.org9a065d12012-03-07 08:12:21 +0000973 CriticalSectionScoped cs(&_fileCritSect);
niklase@google.com470e71d2011-07-07 08:21:25 +0000974 if (_inputFilePlayerPtr)
975 {
976 _inputFilePlayerPtr->RegisterModuleFileCallback(NULL);
977 _inputFilePlayerPtr->StopPlayingFile();
978 FilePlayer::DestroyFilePlayer(_inputFilePlayerPtr);
979 _inputFilePlayerPtr = NULL;
980 }
981 if (_outputFilePlayerPtr)
982 {
983 _outputFilePlayerPtr->RegisterModuleFileCallback(NULL);
984 _outputFilePlayerPtr->StopPlayingFile();
985 FilePlayer::DestroyFilePlayer(_outputFilePlayerPtr);
986 _outputFilePlayerPtr = NULL;
987 }
988 if (_outputFileRecorderPtr)
989 {
990 _outputFileRecorderPtr->RegisterModuleFileCallback(NULL);
991 _outputFileRecorderPtr->StopRecording();
992 FileRecorder::DestroyFileRecorder(_outputFileRecorderPtr);
993 _outputFileRecorderPtr = NULL;
994 }
995 }
996
997 // The order to safely shutdown modules in a channel is:
998 // 1. De-register callbacks in modules
999 // 2. De-register modules in process thread
1000 // 3. Destroy modules
andrew@webrtc.orgeb524d92013-09-23 23:02:24 +00001001 if (audio_coding_->RegisterTransportCallback(NULL) == -1)
niklase@google.com470e71d2011-07-07 08:21:25 +00001002 {
1003 WEBRTC_TRACE(kTraceWarning, kTraceVoice,
1004 VoEId(_instanceId,_channelId),
1005 "~Channel() failed to de-register transport callback"
1006 " (Audio coding module)");
1007 }
andrew@webrtc.orgeb524d92013-09-23 23:02:24 +00001008 if (audio_coding_->RegisterVADCallback(NULL) == -1)
niklase@google.com470e71d2011-07-07 08:21:25 +00001009 {
1010 WEBRTC_TRACE(kTraceWarning, kTraceVoice,
1011 VoEId(_instanceId,_channelId),
1012 "~Channel() failed to de-register VAD callback"
1013 " (Audio coding module)");
1014 }
niklase@google.com470e71d2011-07-07 08:21:25 +00001015 // De-register modules in process thread
pwestin@webrtc.org2853dde2012-05-11 11:08:54 +00001016 if (_moduleProcessThreadPtr->DeRegisterModule(_rtpRtcpModule.get()) == -1)
niklase@google.com470e71d2011-07-07 08:21:25 +00001017 {
1018 WEBRTC_TRACE(kTraceInfo, kTraceVoice,
1019 VoEId(_instanceId,_channelId),
1020 "~Channel() failed to deregister RTP/RTCP module");
1021 }
niklase@google.com470e71d2011-07-07 08:21:25 +00001022 // End of modules shutdown
1023
1024 // Delete other objects
solenberg@webrtc.orgb1f50102014-03-24 10:38:25 +00001025 if (vie_network_) {
1026 vie_network_->Release();
1027 vie_network_ = NULL;
1028 }
niklase@google.com470e71d2011-07-07 08:21:25 +00001029 RtpDump::DestroyRtpDump(&_rtpDumpIn);
1030 RtpDump::DestroyRtpDump(&_rtpDumpOut);
niklase@google.com470e71d2011-07-07 08:21:25 +00001031 delete &_callbackCritSect;
niklase@google.com470e71d2011-07-07 08:21:25 +00001032 delete &_fileCritSect;
wu@webrtc.org63420662013-10-17 18:28:55 +00001033 delete &volume_settings_critsect_;
niklase@google.com470e71d2011-07-07 08:21:25 +00001034}
1035
pbos@webrtc.org6141e132013-04-09 10:09:10 +00001036int32_t
niklase@google.com470e71d2011-07-07 08:21:25 +00001037Channel::Init()
1038{
1039 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId,_channelId),
1040 "Channel::Init()");
1041
henrika@webrtc.org944cbeb2014-03-18 10:32:33 +00001042 channel_state_.Reset();
1043
niklase@google.com470e71d2011-07-07 08:21:25 +00001044 // --- Initial sanity
1045
1046 if ((_engineStatisticsPtr == NULL) ||
1047 (_moduleProcessThreadPtr == NULL))
1048 {
1049 WEBRTC_TRACE(kTraceError, kTraceVoice,
1050 VoEId(_instanceId,_channelId),
1051 "Channel::Init() must call SetEngineInformation() first");
1052 return -1;
1053 }
1054
1055 // --- Add modules to process thread (for periodic schedulation)
1056
1057 const bool processThreadFail =
pwestin@webrtc.org2853dde2012-05-11 11:08:54 +00001058 ((_moduleProcessThreadPtr->RegisterModule(_rtpRtcpModule.get()) != 0) ||
niklase@google.com470e71d2011-07-07 08:21:25 +00001059 false);
niklase@google.com470e71d2011-07-07 08:21:25 +00001060 if (processThreadFail)
1061 {
1062 _engineStatisticsPtr->SetLastError(
1063 VE_CANNOT_INIT_CHANNEL, kTraceError,
1064 "Channel::Init() modules not registered");
1065 return -1;
1066 }
pwestin@webrtc.orgc450a192012-01-04 15:00:12 +00001067 // --- ACM initialization
niklase@google.com470e71d2011-07-07 08:21:25 +00001068
andrew@webrtc.orgeb524d92013-09-23 23:02:24 +00001069 if ((audio_coding_->InitializeReceiver() == -1) ||
niklase@google.com470e71d2011-07-07 08:21:25 +00001070#ifdef WEBRTC_CODEC_AVT
1071 // out-of-band Dtmf tones are played out by default
andrew@webrtc.orgeb524d92013-09-23 23:02:24 +00001072 (audio_coding_->SetDtmfPlayoutStatus(true) == -1) ||
niklase@google.com470e71d2011-07-07 08:21:25 +00001073#endif
andrew@webrtc.orgeb524d92013-09-23 23:02:24 +00001074 (audio_coding_->InitializeSender() == -1))
niklase@google.com470e71d2011-07-07 08:21:25 +00001075 {
1076 _engineStatisticsPtr->SetLastError(
1077 VE_AUDIO_CODING_MODULE_ERROR, kTraceError,
1078 "Channel::Init() unable to initialize the ACM - 1");
1079 return -1;
1080 }
1081
1082 // --- RTP/RTCP module initialization
1083
1084 // Ensure that RTCP is enabled by default for the created channel.
1085 // Note that, the module will keep generating RTCP until it is explicitly
1086 // disabled by the user.
1087 // After StopListen (when no sockets exists), RTCP packets will no longer
1088 // be transmitted since the Transport object will then be invalid.
wu@webrtc.org822fbd82013-08-15 23:38:54 +00001089 telephone_event_handler_->SetTelephoneEventForwardToDecoder(true);
1090 // RTCP is enabled by default.
1091 if (_rtpRtcpModule->SetRTCPStatus(kRtcpCompound) == -1)
niklase@google.com470e71d2011-07-07 08:21:25 +00001092 {
1093 _engineStatisticsPtr->SetLastError(
1094 VE_RTP_RTCP_MODULE_ERROR, kTraceError,
1095 "Channel::Init() RTP/RTCP module not initialized");
1096 return -1;
1097 }
1098
1099 // --- Register all permanent callbacks
niklase@google.com470e71d2011-07-07 08:21:25 +00001100 const bool fail =
andrew@webrtc.orgeb524d92013-09-23 23:02:24 +00001101 (audio_coding_->RegisterTransportCallback(this) == -1) ||
1102 (audio_coding_->RegisterVADCallback(this) == -1);
niklase@google.com470e71d2011-07-07 08:21:25 +00001103
1104 if (fail)
1105 {
1106 _engineStatisticsPtr->SetLastError(
1107 VE_CANNOT_INIT_CHANNEL, kTraceError,
1108 "Channel::Init() callbacks not registered");
1109 return -1;
1110 }
1111
1112 // --- Register all supported codecs to the receiving side of the
1113 // RTP/RTCP module
1114
1115 CodecInst codec;
pbos@webrtc.org6141e132013-04-09 10:09:10 +00001116 const uint8_t nSupportedCodecs = AudioCodingModule::NumberOfCodecs();
niklase@google.com470e71d2011-07-07 08:21:25 +00001117
1118 for (int idx = 0; idx < nSupportedCodecs; idx++)
1119 {
1120 // Open up the RTP/RTCP receiver for all supported codecs
andrew@webrtc.orgeb524d92013-09-23 23:02:24 +00001121 if ((audio_coding_->Codec(idx, &codec) == -1) ||
wu@webrtc.org822fbd82013-08-15 23:38:54 +00001122 (rtp_receiver_->RegisterReceivePayload(
1123 codec.plname,
1124 codec.pltype,
1125 codec.plfreq,
1126 codec.channels,
1127 (codec.rate < 0) ? 0 : codec.rate) == -1))
niklase@google.com470e71d2011-07-07 08:21:25 +00001128 {
1129 WEBRTC_TRACE(kTraceWarning, kTraceVoice,
1130 VoEId(_instanceId,_channelId),
1131 "Channel::Init() unable to register %s (%d/%d/%d/%d) "
1132 "to RTP/RTCP receiver",
1133 codec.plname, codec.pltype, codec.plfreq,
1134 codec.channels, codec.rate);
1135 }
1136 else
1137 {
1138 WEBRTC_TRACE(kTraceInfo, kTraceVoice,
1139 VoEId(_instanceId,_channelId),
1140 "Channel::Init() %s (%d/%d/%d/%d) has been added to "
1141 "the RTP/RTCP receiver",
1142 codec.plname, codec.pltype, codec.plfreq,
1143 codec.channels, codec.rate);
1144 }
1145
1146 // Ensure that PCMU is used as default codec on the sending side
tina.legrand@webrtc.org45175852012-06-01 09:27:35 +00001147 if (!STR_CASE_CMP(codec.plname, "PCMU") && (codec.channels == 1))
niklase@google.com470e71d2011-07-07 08:21:25 +00001148 {
1149 SetSendCodec(codec);
1150 }
1151
1152 // Register default PT for outband 'telephone-event'
1153 if (!STR_CASE_CMP(codec.plname, "telephone-event"))
1154 {
pwestin@webrtc.org2853dde2012-05-11 11:08:54 +00001155 if ((_rtpRtcpModule->RegisterSendPayload(codec) == -1) ||
andrew@webrtc.orgeb524d92013-09-23 23:02:24 +00001156 (audio_coding_->RegisterReceiveCodec(codec) == -1))
niklase@google.com470e71d2011-07-07 08:21:25 +00001157 {
1158 WEBRTC_TRACE(kTraceWarning, kTraceVoice,
1159 VoEId(_instanceId,_channelId),
1160 "Channel::Init() failed to register outband "
1161 "'telephone-event' (%d/%d) correctly",
1162 codec.pltype, codec.plfreq);
1163 }
1164 }
1165
1166 if (!STR_CASE_CMP(codec.plname, "CN"))
1167 {
andrew@webrtc.orgeb524d92013-09-23 23:02:24 +00001168 if ((audio_coding_->RegisterSendCodec(codec) == -1) ||
1169 (audio_coding_->RegisterReceiveCodec(codec) == -1) ||
pwestin@webrtc.org2853dde2012-05-11 11:08:54 +00001170 (_rtpRtcpModule->RegisterSendPayload(codec) == -1))
niklase@google.com470e71d2011-07-07 08:21:25 +00001171 {
1172 WEBRTC_TRACE(kTraceWarning, kTraceVoice,
1173 VoEId(_instanceId,_channelId),
1174 "Channel::Init() failed to register CN (%d/%d) "
1175 "correctly - 1",
1176 codec.pltype, codec.plfreq);
1177 }
1178 }
1179#ifdef WEBRTC_CODEC_RED
1180 // Register RED to the receiving side of the ACM.
1181 // We will not receive an OnInitializeDecoder() callback for RED.
1182 if (!STR_CASE_CMP(codec.plname, "RED"))
1183 {
andrew@webrtc.orgeb524d92013-09-23 23:02:24 +00001184 if (audio_coding_->RegisterReceiveCodec(codec) == -1)
niklase@google.com470e71d2011-07-07 08:21:25 +00001185 {
1186 WEBRTC_TRACE(kTraceWarning, kTraceVoice,
1187 VoEId(_instanceId,_channelId),
1188 "Channel::Init() failed to register RED (%d/%d) "
1189 "correctly",
1190 codec.pltype, codec.plfreq);
1191 }
1192 }
1193#endif
1194 }
pwestin@webrtc.org684f0572013-03-13 23:20:57 +00001195
andrew@webrtc.org6c264cc2013-10-04 17:54:09 +00001196 if (rx_audioproc_->noise_suppression()->set_level(kDefaultNsMode) != 0) {
1197 LOG_FERR1(LS_ERROR, noise_suppression()->set_level, kDefaultNsMode);
1198 return -1;
niklase@google.com470e71d2011-07-07 08:21:25 +00001199 }
andrew@webrtc.org6c264cc2013-10-04 17:54:09 +00001200 if (rx_audioproc_->gain_control()->set_mode(kDefaultRxAgcMode) != 0) {
1201 LOG_FERR1(LS_ERROR, gain_control()->set_mode, kDefaultRxAgcMode);
1202 return -1;
niklase@google.com470e71d2011-07-07 08:21:25 +00001203 }
1204
1205 return 0;
1206}
1207
pbos@webrtc.org6141e132013-04-09 10:09:10 +00001208int32_t
niklase@google.com470e71d2011-07-07 08:21:25 +00001209Channel::SetEngineInformation(Statistics& engineStatistics,
1210 OutputMixer& outputMixer,
1211 voe::TransmitMixer& transmitMixer,
1212 ProcessThread& moduleProcessThread,
1213 AudioDeviceModule& audioDeviceModule,
1214 VoiceEngineObserver* voiceEngineObserver,
1215 CriticalSectionWrapper* callbackCritSect)
1216{
1217 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId,_channelId),
1218 "Channel::SetEngineInformation()");
1219 _engineStatisticsPtr = &engineStatistics;
1220 _outputMixerPtr = &outputMixer;
1221 _transmitMixerPtr = &transmitMixer,
1222 _moduleProcessThreadPtr = &moduleProcessThread;
1223 _audioDeviceModulePtr = &audioDeviceModule;
1224 _voiceEngineObserverPtr = voiceEngineObserver;
1225 _callbackCritSectPtr = callbackCritSect;
1226 return 0;
1227}
1228
pbos@webrtc.org6141e132013-04-09 10:09:10 +00001229int32_t
niklase@google.com470e71d2011-07-07 08:21:25 +00001230Channel::UpdateLocalTimeStamp()
1231{
1232
andrew@webrtc.org63a50982012-05-02 23:56:37 +00001233 _timeStamp += _audioFrame.samples_per_channel_;
niklase@google.com470e71d2011-07-07 08:21:25 +00001234 return 0;
1235}
1236
pbos@webrtc.org6141e132013-04-09 10:09:10 +00001237int32_t
niklase@google.com470e71d2011-07-07 08:21:25 +00001238Channel::StartPlayout()
1239{
1240 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId,_channelId),
1241 "Channel::StartPlayout()");
henrika@webrtc.org944cbeb2014-03-18 10:32:33 +00001242 if (channel_state_.Get().playing)
niklase@google.com470e71d2011-07-07 08:21:25 +00001243 {
1244 return 0;
1245 }
roosa@google.com1b60ceb2012-12-12 23:00:29 +00001246
1247 if (!_externalMixing) {
1248 // Add participant as candidates for mixing.
1249 if (_outputMixerPtr->SetMixabilityStatus(*this, true) != 0)
1250 {
1251 _engineStatisticsPtr->SetLastError(
1252 VE_AUDIO_CONF_MIX_MODULE_ERROR, kTraceError,
1253 "StartPlayout() failed to add participant to mixer");
1254 return -1;
1255 }
niklase@google.com470e71d2011-07-07 08:21:25 +00001256 }
1257
henrika@webrtc.org944cbeb2014-03-18 10:32:33 +00001258 channel_state_.SetPlaying(true);
braveyao@webrtc.orgab129902012-06-04 03:26:39 +00001259 if (RegisterFilePlayingToMixer() != 0)
1260 return -1;
1261
niklase@google.com470e71d2011-07-07 08:21:25 +00001262 return 0;
1263}
1264
pbos@webrtc.org6141e132013-04-09 10:09:10 +00001265int32_t
niklase@google.com470e71d2011-07-07 08:21:25 +00001266Channel::StopPlayout()
1267{
1268 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId,_channelId),
1269 "Channel::StopPlayout()");
henrika@webrtc.org944cbeb2014-03-18 10:32:33 +00001270 if (!channel_state_.Get().playing)
niklase@google.com470e71d2011-07-07 08:21:25 +00001271 {
1272 return 0;
1273 }
roosa@google.com1b60ceb2012-12-12 23:00:29 +00001274
1275 if (!_externalMixing) {
1276 // Remove participant as candidates for mixing
1277 if (_outputMixerPtr->SetMixabilityStatus(*this, false) != 0)
1278 {
1279 _engineStatisticsPtr->SetLastError(
1280 VE_AUDIO_CONF_MIX_MODULE_ERROR, kTraceError,
1281 "StopPlayout() failed to remove participant from mixer");
1282 return -1;
1283 }
niklase@google.com470e71d2011-07-07 08:21:25 +00001284 }
1285
henrika@webrtc.org944cbeb2014-03-18 10:32:33 +00001286 channel_state_.SetPlaying(false);
niklase@google.com470e71d2011-07-07 08:21:25 +00001287 _outputAudioLevel.Clear();
1288
1289 return 0;
1290}
1291
pbos@webrtc.org6141e132013-04-09 10:09:10 +00001292int32_t
niklase@google.com470e71d2011-07-07 08:21:25 +00001293Channel::StartSend()
1294{
1295 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId,_channelId),
1296 "Channel::StartSend()");
xians@webrtc.org09e8c472013-07-31 16:30:19 +00001297 // Resume the previous sequence number which was reset by StopSend().
henrika@webrtc.org944cbeb2014-03-18 10:32:33 +00001298 // This needs to be done before |sending| is set to true.
xians@webrtc.org09e8c472013-07-31 16:30:19 +00001299 if (send_sequence_number_)
1300 SetInitSequenceNumber(send_sequence_number_);
1301
henrika@webrtc.org944cbeb2014-03-18 10:32:33 +00001302 if (channel_state_.Get().sending)
niklase@google.com470e71d2011-07-07 08:21:25 +00001303 {
henrika@webrtc.org944cbeb2014-03-18 10:32:33 +00001304 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +00001305 }
henrika@webrtc.org944cbeb2014-03-18 10:32:33 +00001306 channel_state_.SetSending(true);
xians@webrtc.orge07247a2011-11-28 16:31:28 +00001307
pwestin@webrtc.org2853dde2012-05-11 11:08:54 +00001308 if (_rtpRtcpModule->SetSendingStatus(true) != 0)
niklase@google.com470e71d2011-07-07 08:21:25 +00001309 {
1310 _engineStatisticsPtr->SetLastError(
1311 VE_RTP_RTCP_MODULE_ERROR, kTraceError,
1312 "StartSend() RTP/RTCP failed to start sending");
mflodman@webrtc.org9a065d12012-03-07 08:12:21 +00001313 CriticalSectionScoped cs(&_callbackCritSect);
henrika@webrtc.org944cbeb2014-03-18 10:32:33 +00001314 channel_state_.SetSending(false);
niklase@google.com470e71d2011-07-07 08:21:25 +00001315 return -1;
1316 }
xians@webrtc.orge07247a2011-11-28 16:31:28 +00001317
niklase@google.com470e71d2011-07-07 08:21:25 +00001318 return 0;
1319}
1320
pbos@webrtc.org6141e132013-04-09 10:09:10 +00001321int32_t
niklase@google.com470e71d2011-07-07 08:21:25 +00001322Channel::StopSend()
1323{
1324 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId,_channelId),
1325 "Channel::StopSend()");
henrika@webrtc.org944cbeb2014-03-18 10:32:33 +00001326 if (!channel_state_.Get().sending)
niklase@google.com470e71d2011-07-07 08:21:25 +00001327 {
henrika@webrtc.org944cbeb2014-03-18 10:32:33 +00001328 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +00001329 }
henrika@webrtc.org944cbeb2014-03-18 10:32:33 +00001330 channel_state_.SetSending(false);
xians@webrtc.orge07247a2011-11-28 16:31:28 +00001331
xians@webrtc.org09e8c472013-07-31 16:30:19 +00001332 // Store the sequence number to be able to pick up the same sequence for
1333 // the next StartSend(). This is needed for restarting device, otherwise
1334 // it might cause libSRTP to complain about packets being replayed.
1335 // TODO(xians): Remove this workaround after RtpRtcpModule's refactoring
1336 // CL is landed. See issue
1337 // https://code.google.com/p/webrtc/issues/detail?id=2111 .
1338 send_sequence_number_ = _rtpRtcpModule->SequenceNumber();
1339
niklase@google.com470e71d2011-07-07 08:21:25 +00001340 // Reset sending SSRC and sequence number and triggers direct transmission
1341 // of RTCP BYE
pwestin@webrtc.org2853dde2012-05-11 11:08:54 +00001342 if (_rtpRtcpModule->SetSendingStatus(false) == -1 ||
1343 _rtpRtcpModule->ResetSendDataCountersRTP() == -1)
niklase@google.com470e71d2011-07-07 08:21:25 +00001344 {
1345 _engineStatisticsPtr->SetLastError(
1346 VE_RTP_RTCP_MODULE_ERROR, kTraceWarning,
1347 "StartSend() RTP/RTCP failed to stop sending");
1348 }
1349
niklase@google.com470e71d2011-07-07 08:21:25 +00001350 return 0;
1351}
1352
pbos@webrtc.org6141e132013-04-09 10:09:10 +00001353int32_t
niklase@google.com470e71d2011-07-07 08:21:25 +00001354Channel::StartReceiving()
1355{
1356 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId,_channelId),
1357 "Channel::StartReceiving()");
henrika@webrtc.org944cbeb2014-03-18 10:32:33 +00001358 if (channel_state_.Get().receiving)
niklase@google.com470e71d2011-07-07 08:21:25 +00001359 {
1360 return 0;
1361 }
henrika@webrtc.org944cbeb2014-03-18 10:32:33 +00001362 channel_state_.SetReceiving(true);
niklase@google.com470e71d2011-07-07 08:21:25 +00001363 _numberOfDiscardedPackets = 0;
1364 return 0;
1365}
1366
pbos@webrtc.org6141e132013-04-09 10:09:10 +00001367int32_t
niklase@google.com470e71d2011-07-07 08:21:25 +00001368Channel::StopReceiving()
1369{
1370 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId,_channelId),
1371 "Channel::StopReceiving()");
henrika@webrtc.org944cbeb2014-03-18 10:32:33 +00001372 if (!channel_state_.Get().receiving)
niklase@google.com470e71d2011-07-07 08:21:25 +00001373 {
1374 return 0;
1375 }
pwestin@webrtc.org684f0572013-03-13 23:20:57 +00001376
henrika@webrtc.org944cbeb2014-03-18 10:32:33 +00001377 channel_state_.SetReceiving(false);
niklase@google.com470e71d2011-07-07 08:21:25 +00001378 return 0;
1379}
1380
pbos@webrtc.org6141e132013-04-09 10:09:10 +00001381int32_t
niklase@google.com470e71d2011-07-07 08:21:25 +00001382Channel::SetNetEQPlayoutMode(NetEqModes mode)
1383{
1384 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId,_channelId),
1385 "Channel::SetNetEQPlayoutMode()");
1386 AudioPlayoutMode playoutMode(voice);
1387 switch (mode)
1388 {
1389 case kNetEqDefault:
1390 playoutMode = voice;
1391 break;
1392 case kNetEqStreaming:
1393 playoutMode = streaming;
1394 break;
1395 case kNetEqFax:
1396 playoutMode = fax;
1397 break;
roosa@google.comb7186192012-12-12 21:59:14 +00001398 case kNetEqOff:
1399 playoutMode = off;
1400 break;
niklase@google.com470e71d2011-07-07 08:21:25 +00001401 }
andrew@webrtc.orgeb524d92013-09-23 23:02:24 +00001402 if (audio_coding_->SetPlayoutMode(playoutMode) != 0)
niklase@google.com470e71d2011-07-07 08:21:25 +00001403 {
1404 _engineStatisticsPtr->SetLastError(
1405 VE_AUDIO_CODING_MODULE_ERROR, kTraceError,
1406 "SetNetEQPlayoutMode() failed to set playout mode");
1407 return -1;
1408 }
1409 return 0;
1410}
1411
pbos@webrtc.org6141e132013-04-09 10:09:10 +00001412int32_t
niklase@google.com470e71d2011-07-07 08:21:25 +00001413Channel::GetNetEQPlayoutMode(NetEqModes& mode)
1414{
andrew@webrtc.orgeb524d92013-09-23 23:02:24 +00001415 const AudioPlayoutMode playoutMode = audio_coding_->PlayoutMode();
niklase@google.com470e71d2011-07-07 08:21:25 +00001416 switch (playoutMode)
1417 {
1418 case voice:
1419 mode = kNetEqDefault;
1420 break;
1421 case streaming:
1422 mode = kNetEqStreaming;
1423 break;
1424 case fax:
1425 mode = kNetEqFax;
1426 break;
roosa@google.comb7186192012-12-12 21:59:14 +00001427 case off:
1428 mode = kNetEqOff;
niklase@google.com470e71d2011-07-07 08:21:25 +00001429 }
1430 WEBRTC_TRACE(kTraceStateInfo, kTraceVoice,
1431 VoEId(_instanceId,_channelId),
1432 "Channel::GetNetEQPlayoutMode() => mode=%u", mode);
1433 return 0;
1434}
1435
pbos@webrtc.org6141e132013-04-09 10:09:10 +00001436int32_t
niklase@google.com470e71d2011-07-07 08:21:25 +00001437Channel::RegisterVoiceEngineObserver(VoiceEngineObserver& observer)
1438{
1439 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId,_channelId),
1440 "Channel::RegisterVoiceEngineObserver()");
mflodman@webrtc.org9a065d12012-03-07 08:12:21 +00001441 CriticalSectionScoped cs(&_callbackCritSect);
niklase@google.com470e71d2011-07-07 08:21:25 +00001442
1443 if (_voiceEngineObserverPtr)
1444 {
1445 _engineStatisticsPtr->SetLastError(
1446 VE_INVALID_OPERATION, kTraceError,
1447 "RegisterVoiceEngineObserver() observer already enabled");
1448 return -1;
1449 }
1450 _voiceEngineObserverPtr = &observer;
1451 return 0;
1452}
1453
pbos@webrtc.org6141e132013-04-09 10:09:10 +00001454int32_t
niklase@google.com470e71d2011-07-07 08:21:25 +00001455Channel::DeRegisterVoiceEngineObserver()
1456{
1457 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId,_channelId),
1458 "Channel::DeRegisterVoiceEngineObserver()");
mflodman@webrtc.org9a065d12012-03-07 08:12:21 +00001459 CriticalSectionScoped cs(&_callbackCritSect);
niklase@google.com470e71d2011-07-07 08:21:25 +00001460
1461 if (!_voiceEngineObserverPtr)
1462 {
1463 _engineStatisticsPtr->SetLastError(
1464 VE_INVALID_OPERATION, kTraceWarning,
1465 "DeRegisterVoiceEngineObserver() observer already disabled");
1466 return 0;
1467 }
1468 _voiceEngineObserverPtr = NULL;
1469 return 0;
1470}
1471
pbos@webrtc.org6141e132013-04-09 10:09:10 +00001472int32_t
niklase@google.com470e71d2011-07-07 08:21:25 +00001473Channel::GetSendCodec(CodecInst& codec)
1474{
andrew@webrtc.orgeb524d92013-09-23 23:02:24 +00001475 return (audio_coding_->SendCodec(&codec));
niklase@google.com470e71d2011-07-07 08:21:25 +00001476}
1477
pbos@webrtc.org6141e132013-04-09 10:09:10 +00001478int32_t
niklase@google.com470e71d2011-07-07 08:21:25 +00001479Channel::GetRecCodec(CodecInst& codec)
1480{
andrew@webrtc.orgeb524d92013-09-23 23:02:24 +00001481 return (audio_coding_->ReceiveCodec(&codec));
niklase@google.com470e71d2011-07-07 08:21:25 +00001482}
1483
pbos@webrtc.org6141e132013-04-09 10:09:10 +00001484int32_t
niklase@google.com470e71d2011-07-07 08:21:25 +00001485Channel::SetSendCodec(const CodecInst& codec)
1486{
1487 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId,_channelId),
1488 "Channel::SetSendCodec()");
1489
andrew@webrtc.orgeb524d92013-09-23 23:02:24 +00001490 if (audio_coding_->RegisterSendCodec(codec) != 0)
niklase@google.com470e71d2011-07-07 08:21:25 +00001491 {
1492 WEBRTC_TRACE(kTraceError, kTraceVoice, VoEId(_instanceId,_channelId),
1493 "SetSendCodec() failed to register codec to ACM");
1494 return -1;
1495 }
1496
pwestin@webrtc.org2853dde2012-05-11 11:08:54 +00001497 if (_rtpRtcpModule->RegisterSendPayload(codec) != 0)
niklase@google.com470e71d2011-07-07 08:21:25 +00001498 {
pwestin@webrtc.org2853dde2012-05-11 11:08:54 +00001499 _rtpRtcpModule->DeRegisterSendPayload(codec.pltype);
1500 if (_rtpRtcpModule->RegisterSendPayload(codec) != 0)
niklase@google.com470e71d2011-07-07 08:21:25 +00001501 {
1502 WEBRTC_TRACE(
1503 kTraceError, kTraceVoice, VoEId(_instanceId,_channelId),
1504 "SetSendCodec() failed to register codec to"
1505 " RTP/RTCP module");
1506 return -1;
1507 }
1508 }
1509
pwestin@webrtc.org2853dde2012-05-11 11:08:54 +00001510 if (_rtpRtcpModule->SetAudioPacketSize(codec.pacsize) != 0)
niklase@google.com470e71d2011-07-07 08:21:25 +00001511 {
1512 WEBRTC_TRACE(kTraceError, kTraceVoice, VoEId(_instanceId,_channelId),
1513 "SetSendCodec() failed to set audio packet size");
1514 return -1;
1515 }
1516
minyue@webrtc.orgc1a40a72014-05-28 09:52:06 +00001517 bitrate_controller_->SetBitrateObserver(send_bitrate_observer_.get(),
1518 codec.rate, 0, 0);
1519
niklase@google.com470e71d2011-07-07 08:21:25 +00001520 return 0;
1521}
1522
minyue@webrtc.orgc1a40a72014-05-28 09:52:06 +00001523void
1524Channel::OnNetworkChanged(const uint32_t bitrate_bps,
1525 const uint8_t fraction_lost, // 0 - 255.
1526 const uint32_t rtt) {
1527 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId,_channelId),
1528 "Channel::OnNetworkChanged(bitrate_bps=%d, fration_lost=%d, rtt=%d)",
1529 bitrate_bps, fraction_lost, rtt);
1530 // Normalizes rate to 0 - 100.
1531 if (audio_coding_->SetPacketLossRate(100 * fraction_lost / 255) != 0) {
1532 _engineStatisticsPtr->SetLastError(VE_AUDIO_CODING_MODULE_ERROR,
1533 kTraceError, "OnNetworkChanged() failed to set packet loss rate");
1534 assert(false); // This should not happen.
1535 }
1536}
1537
pbos@webrtc.org6141e132013-04-09 10:09:10 +00001538int32_t
niklase@google.com470e71d2011-07-07 08:21:25 +00001539Channel::SetVADStatus(bool enableVAD, ACMVADMode mode, bool disableDTX)
1540{
1541 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId,_channelId),
1542 "Channel::SetVADStatus(mode=%d)", mode);
1543 // To disable VAD, DTX must be disabled too
1544 disableDTX = ((enableVAD == false) ? true : disableDTX);
andrew@webrtc.orgeb524d92013-09-23 23:02:24 +00001545 if (audio_coding_->SetVAD(!disableDTX, enableVAD, mode) != 0)
niklase@google.com470e71d2011-07-07 08:21:25 +00001546 {
1547 _engineStatisticsPtr->SetLastError(
1548 VE_AUDIO_CODING_MODULE_ERROR, kTraceError,
1549 "SetVADStatus() failed to set VAD");
1550 return -1;
1551 }
1552 return 0;
1553}
1554
pbos@webrtc.org6141e132013-04-09 10:09:10 +00001555int32_t
niklase@google.com470e71d2011-07-07 08:21:25 +00001556Channel::GetVADStatus(bool& enabledVAD, ACMVADMode& mode, bool& disabledDTX)
1557{
1558 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId,_channelId),
1559 "Channel::GetVADStatus");
andrew@webrtc.orgeb524d92013-09-23 23:02:24 +00001560 if (audio_coding_->VAD(&disabledDTX, &enabledVAD, &mode) != 0)
niklase@google.com470e71d2011-07-07 08:21:25 +00001561 {
1562 _engineStatisticsPtr->SetLastError(
1563 VE_AUDIO_CODING_MODULE_ERROR, kTraceError,
1564 "GetVADStatus() failed to get VAD status");
1565 return -1;
1566 }
1567 disabledDTX = !disabledDTX;
1568 return 0;
1569}
1570
pbos@webrtc.org6141e132013-04-09 10:09:10 +00001571int32_t
niklase@google.com470e71d2011-07-07 08:21:25 +00001572Channel::SetRecPayloadType(const CodecInst& codec)
1573{
1574 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId,_channelId),
1575 "Channel::SetRecPayloadType()");
1576
henrika@webrtc.org944cbeb2014-03-18 10:32:33 +00001577 if (channel_state_.Get().playing)
niklase@google.com470e71d2011-07-07 08:21:25 +00001578 {
1579 _engineStatisticsPtr->SetLastError(
1580 VE_ALREADY_PLAYING, kTraceError,
1581 "SetRecPayloadType() unable to set PT while playing");
1582 return -1;
1583 }
henrika@webrtc.org944cbeb2014-03-18 10:32:33 +00001584 if (channel_state_.Get().receiving)
niklase@google.com470e71d2011-07-07 08:21:25 +00001585 {
1586 _engineStatisticsPtr->SetLastError(
1587 VE_ALREADY_LISTENING, kTraceError,
1588 "SetRecPayloadType() unable to set PT while listening");
1589 return -1;
1590 }
1591
1592 if (codec.pltype == -1)
1593 {
1594 // De-register the selected codec (RTP/RTCP module and ACM)
1595
pbos@webrtc.org6141e132013-04-09 10:09:10 +00001596 int8_t pltype(-1);
niklase@google.com470e71d2011-07-07 08:21:25 +00001597 CodecInst rxCodec = codec;
1598
1599 // Get payload type for the given codec
wu@webrtc.org822fbd82013-08-15 23:38:54 +00001600 rtp_payload_registry_->ReceivePayloadType(
1601 rxCodec.plname,
1602 rxCodec.plfreq,
1603 rxCodec.channels,
1604 (rxCodec.rate < 0) ? 0 : rxCodec.rate,
1605 &pltype);
niklase@google.com470e71d2011-07-07 08:21:25 +00001606 rxCodec.pltype = pltype;
1607
wu@webrtc.org822fbd82013-08-15 23:38:54 +00001608 if (rtp_receiver_->DeRegisterReceivePayload(pltype) != 0)
niklase@google.com470e71d2011-07-07 08:21:25 +00001609 {
1610 _engineStatisticsPtr->SetLastError(
1611 VE_RTP_RTCP_MODULE_ERROR,
1612 kTraceError,
1613 "SetRecPayloadType() RTP/RTCP-module deregistration "
1614 "failed");
1615 return -1;
1616 }
andrew@webrtc.orgeb524d92013-09-23 23:02:24 +00001617 if (audio_coding_->UnregisterReceiveCodec(rxCodec.pltype) != 0)
niklase@google.com470e71d2011-07-07 08:21:25 +00001618 {
1619 _engineStatisticsPtr->SetLastError(
1620 VE_AUDIO_CODING_MODULE_ERROR, kTraceError,
1621 "SetRecPayloadType() ACM deregistration failed - 1");
1622 return -1;
1623 }
1624 return 0;
1625 }
1626
wu@webrtc.org822fbd82013-08-15 23:38:54 +00001627 if (rtp_receiver_->RegisterReceivePayload(
1628 codec.plname,
1629 codec.pltype,
1630 codec.plfreq,
1631 codec.channels,
1632 (codec.rate < 0) ? 0 : codec.rate) != 0)
niklase@google.com470e71d2011-07-07 08:21:25 +00001633 {
1634 // First attempt to register failed => de-register and try again
wu@webrtc.org822fbd82013-08-15 23:38:54 +00001635 rtp_receiver_->DeRegisterReceivePayload(codec.pltype);
1636 if (rtp_receiver_->RegisterReceivePayload(
1637 codec.plname,
1638 codec.pltype,
1639 codec.plfreq,
1640 codec.channels,
1641 (codec.rate < 0) ? 0 : codec.rate) != 0)
niklase@google.com470e71d2011-07-07 08:21:25 +00001642 {
1643 _engineStatisticsPtr->SetLastError(
1644 VE_RTP_RTCP_MODULE_ERROR, kTraceError,
1645 "SetRecPayloadType() RTP/RTCP-module registration failed");
1646 return -1;
1647 }
1648 }
andrew@webrtc.orgeb524d92013-09-23 23:02:24 +00001649 if (audio_coding_->RegisterReceiveCodec(codec) != 0)
niklase@google.com470e71d2011-07-07 08:21:25 +00001650 {
andrew@webrtc.orgeb524d92013-09-23 23:02:24 +00001651 audio_coding_->UnregisterReceiveCodec(codec.pltype);
1652 if (audio_coding_->RegisterReceiveCodec(codec) != 0)
niklase@google.com470e71d2011-07-07 08:21:25 +00001653 {
1654 _engineStatisticsPtr->SetLastError(
1655 VE_AUDIO_CODING_MODULE_ERROR, kTraceError,
1656 "SetRecPayloadType() ACM registration failed - 1");
1657 return -1;
1658 }
1659 }
1660 return 0;
1661}
1662
pbos@webrtc.org6141e132013-04-09 10:09:10 +00001663int32_t
niklase@google.com470e71d2011-07-07 08:21:25 +00001664Channel::GetRecPayloadType(CodecInst& codec)
1665{
1666 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId,_channelId),
1667 "Channel::GetRecPayloadType()");
pbos@webrtc.org6141e132013-04-09 10:09:10 +00001668 int8_t payloadType(-1);
wu@webrtc.org822fbd82013-08-15 23:38:54 +00001669 if (rtp_payload_registry_->ReceivePayloadType(
1670 codec.plname,
1671 codec.plfreq,
1672 codec.channels,
1673 (codec.rate < 0) ? 0 : codec.rate,
1674 &payloadType) != 0)
niklase@google.com470e71d2011-07-07 08:21:25 +00001675 {
1676 _engineStatisticsPtr->SetLastError(
henrika@webrtc.org37198002012-06-18 11:00:12 +00001677 VE_RTP_RTCP_MODULE_ERROR, kTraceWarning,
niklase@google.com470e71d2011-07-07 08:21:25 +00001678 "GetRecPayloadType() failed to retrieve RX payload type");
1679 return -1;
1680 }
1681 codec.pltype = payloadType;
1682 WEBRTC_TRACE(kTraceStateInfo, kTraceVoice, VoEId(_instanceId,_channelId),
1683 "Channel::GetRecPayloadType() => pltype=%u", codec.pltype);
1684 return 0;
1685}
1686
pbos@webrtc.org6141e132013-04-09 10:09:10 +00001687int32_t
niklase@google.com470e71d2011-07-07 08:21:25 +00001688Channel::SetSendCNPayloadType(int type, PayloadFrequencies frequency)
1689{
1690 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId,_channelId),
1691 "Channel::SetSendCNPayloadType()");
1692
1693 CodecInst codec;
pbos@webrtc.org6141e132013-04-09 10:09:10 +00001694 int32_t samplingFreqHz(-1);
tina.legrand@webrtc.org45175852012-06-01 09:27:35 +00001695 const int kMono = 1;
niklase@google.com470e71d2011-07-07 08:21:25 +00001696 if (frequency == kFreq32000Hz)
1697 samplingFreqHz = 32000;
1698 else if (frequency == kFreq16000Hz)
1699 samplingFreqHz = 16000;
1700
andrew@webrtc.orgeb524d92013-09-23 23:02:24 +00001701 if (audio_coding_->Codec("CN", &codec, samplingFreqHz, kMono) == -1)
niklase@google.com470e71d2011-07-07 08:21:25 +00001702 {
1703 _engineStatisticsPtr->SetLastError(
1704 VE_AUDIO_CODING_MODULE_ERROR, kTraceError,
1705 "SetSendCNPayloadType() failed to retrieve default CN codec "
1706 "settings");
1707 return -1;
1708 }
1709
1710 // Modify the payload type (must be set to dynamic range)
1711 codec.pltype = type;
1712
andrew@webrtc.orgeb524d92013-09-23 23:02:24 +00001713 if (audio_coding_->RegisterSendCodec(codec) != 0)
niklase@google.com470e71d2011-07-07 08:21:25 +00001714 {
1715 _engineStatisticsPtr->SetLastError(
1716 VE_AUDIO_CODING_MODULE_ERROR, kTraceError,
1717 "SetSendCNPayloadType() failed to register CN to ACM");
1718 return -1;
1719 }
1720
pwestin@webrtc.org2853dde2012-05-11 11:08:54 +00001721 if (_rtpRtcpModule->RegisterSendPayload(codec) != 0)
niklase@google.com470e71d2011-07-07 08:21:25 +00001722 {
pwestin@webrtc.org2853dde2012-05-11 11:08:54 +00001723 _rtpRtcpModule->DeRegisterSendPayload(codec.pltype);
1724 if (_rtpRtcpModule->RegisterSendPayload(codec) != 0)
niklase@google.com470e71d2011-07-07 08:21:25 +00001725 {
1726 _engineStatisticsPtr->SetLastError(
1727 VE_RTP_RTCP_MODULE_ERROR, kTraceError,
1728 "SetSendCNPayloadType() failed to register CN to RTP/RTCP "
1729 "module");
1730 return -1;
1731 }
1732 }
1733 return 0;
1734}
1735
pbos@webrtc.org6141e132013-04-09 10:09:10 +00001736int32_t Channel::RegisterExternalTransport(Transport& transport)
niklase@google.com470e71d2011-07-07 08:21:25 +00001737{
1738 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId, _channelId),
1739 "Channel::RegisterExternalTransport()");
1740
mflodman@webrtc.org9a065d12012-03-07 08:12:21 +00001741 CriticalSectionScoped cs(&_callbackCritSect);
niklase@google.com470e71d2011-07-07 08:21:25 +00001742
niklase@google.com470e71d2011-07-07 08:21:25 +00001743 if (_externalTransport)
1744 {
1745 _engineStatisticsPtr->SetLastError(VE_INVALID_OPERATION,
1746 kTraceError,
1747 "RegisterExternalTransport() external transport already enabled");
1748 return -1;
1749 }
1750 _externalTransport = true;
1751 _transportPtr = &transport;
1752 return 0;
1753}
1754
pbos@webrtc.org6141e132013-04-09 10:09:10 +00001755int32_t
niklase@google.com470e71d2011-07-07 08:21:25 +00001756Channel::DeRegisterExternalTransport()
1757{
1758 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId,_channelId),
1759 "Channel::DeRegisterExternalTransport()");
1760
mflodman@webrtc.org9a065d12012-03-07 08:12:21 +00001761 CriticalSectionScoped cs(&_callbackCritSect);
xians@webrtc.org83661f52011-11-25 10:58:15 +00001762
niklase@google.com470e71d2011-07-07 08:21:25 +00001763 if (!_transportPtr)
1764 {
1765 _engineStatisticsPtr->SetLastError(
1766 VE_INVALID_OPERATION, kTraceWarning,
1767 "DeRegisterExternalTransport() external transport already "
1768 "disabled");
1769 return 0;
1770 }
1771 _externalTransport = false;
niklase@google.com470e71d2011-07-07 08:21:25 +00001772 _transportPtr = NULL;
1773 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId,_channelId),
1774 "DeRegisterExternalTransport() all transport is disabled");
niklase@google.com470e71d2011-07-07 08:21:25 +00001775 return 0;
1776}
1777
solenberg@webrtc.orgb1f50102014-03-24 10:38:25 +00001778int32_t Channel::ReceivedRTPPacket(const int8_t* data, int32_t length,
1779 const PacketTime& packet_time) {
pwestin@webrtc.org0c459572013-04-03 15:43:57 +00001780 WEBRTC_TRACE(kTraceStream, kTraceVoice, VoEId(_instanceId,_channelId),
1781 "Channel::ReceivedRTPPacket()");
1782
1783 // Store playout timestamp for the received RTP packet
pwestin@webrtc.org1de01352013-04-11 20:23:35 +00001784 UpdatePlayoutTimestamp(false);
pwestin@webrtc.org0c459572013-04-03 15:43:57 +00001785
1786 // Dump the RTP packet to a file (if RTP dump is enabled).
pbos@webrtc.org6141e132013-04-09 10:09:10 +00001787 if (_rtpDumpIn.DumpPacket((const uint8_t*)data,
1788 (uint16_t)length) == -1) {
pwestin@webrtc.org0c459572013-04-03 15:43:57 +00001789 WEBRTC_TRACE(kTraceWarning, kTraceVoice,
1790 VoEId(_instanceId,_channelId),
1791 "Channel::SendPacket() RTP dump to input file failed");
1792 }
stefan@webrtc.org7bb8f022013-09-06 13:40:11 +00001793 const uint8_t* received_packet = reinterpret_cast<const uint8_t*>(data);
stefan@webrtc.orga5cb98c2013-05-29 12:12:51 +00001794 RTPHeader header;
stefan@webrtc.org7bb8f022013-09-06 13:40:11 +00001795 if (!rtp_header_parser_->Parse(received_packet, length, &header)) {
1796 WEBRTC_TRACE(webrtc::kTraceDebug, webrtc::kTraceVoice, _channelId,
1797 "Incoming packet: invalid RTP header");
stefan@webrtc.orga5cb98c2013-05-29 12:12:51 +00001798 return -1;
1799 }
wu@webrtc.org822fbd82013-08-15 23:38:54 +00001800 header.payload_type_frequency =
1801 rtp_payload_registry_->GetPayloadTypeFrequency(header.payloadType);
stefan@webrtc.org7bb8f022013-09-06 13:40:11 +00001802 if (header.payload_type_frequency < 0)
wu@webrtc.org822fbd82013-08-15 23:38:54 +00001803 return -1;
stefan@webrtc.org48df3812013-11-08 15:18:52 +00001804 bool in_order = IsPacketInOrder(header);
stefan@webrtc.org7bb8f022013-09-06 13:40:11 +00001805 rtp_receive_statistics_->IncomingPacket(header, length,
stefan@webrtc.org48df3812013-11-08 15:18:52 +00001806 IsPacketRetransmitted(header, in_order));
stefan@webrtc.org7bb8f022013-09-06 13:40:11 +00001807 rtp_payload_registry_->SetIncomingPayloadType(header);
solenberg@webrtc.orgb1f50102014-03-24 10:38:25 +00001808
1809 // Forward any packets to ViE bandwidth estimator, if enabled.
1810 {
1811 CriticalSectionScoped cs(&_callbackCritSect);
1812 if (vie_network_) {
1813 int64_t arrival_time_ms;
1814 if (packet_time.timestamp != -1) {
1815 arrival_time_ms = (packet_time.timestamp + 500) / 1000;
1816 } else {
1817 arrival_time_ms = TickTime::MillisecondTimestamp();
1818 }
1819 int payload_length = length - header.headerLength;
1820 vie_network_->ReceivedBWEPacket(video_channel_, arrival_time_ms,
1821 payload_length, header);
1822 }
1823 }
1824
stefan@webrtc.org48df3812013-11-08 15:18:52 +00001825 return ReceivePacket(received_packet, length, header, in_order) ? 0 : -1;
stefan@webrtc.org7bb8f022013-09-06 13:40:11 +00001826}
1827
1828bool Channel::ReceivePacket(const uint8_t* packet,
1829 int packet_length,
1830 const RTPHeader& header,
1831 bool in_order) {
1832 if (rtp_payload_registry_->IsEncapsulated(header)) {
1833 return HandleEncapsulation(packet, packet_length, header);
wu@webrtc.org822fbd82013-08-15 23:38:54 +00001834 }
stefan@webrtc.org7bb8f022013-09-06 13:40:11 +00001835 const uint8_t* payload = packet + header.headerLength;
1836 int payload_length = packet_length - header.headerLength;
1837 assert(payload_length >= 0);
wu@webrtc.org822fbd82013-08-15 23:38:54 +00001838 PayloadUnion payload_specific;
1839 if (!rtp_payload_registry_->GetPayloadSpecifics(header.payloadType,
stefan@webrtc.org7bb8f022013-09-06 13:40:11 +00001840 &payload_specific)) {
1841 return false;
wu@webrtc.org822fbd82013-08-15 23:38:54 +00001842 }
stefan@webrtc.org7bb8f022013-09-06 13:40:11 +00001843 return rtp_receiver_->IncomingRtpPacket(header, payload, payload_length,
1844 payload_specific, in_order);
1845}
1846
1847bool Channel::HandleEncapsulation(const uint8_t* packet,
1848 int packet_length,
1849 const RTPHeader& header) {
1850 if (!rtp_payload_registry_->IsRtx(header))
1851 return false;
1852
1853 // Remove the RTX header and parse the original RTP header.
1854 if (packet_length < header.headerLength)
1855 return false;
1856 if (packet_length > kVoiceEngineMaxIpPacketSizeBytes)
1857 return false;
1858 if (restored_packet_in_use_) {
1859 WEBRTC_TRACE(webrtc::kTraceDebug, webrtc::kTraceVoice, _channelId,
1860 "Multiple RTX headers detected, dropping packet");
1861 return false;
pwestin@webrtc.org0c459572013-04-03 15:43:57 +00001862 }
stefan@webrtc.org7bb8f022013-09-06 13:40:11 +00001863 uint8_t* restored_packet_ptr = restored_packet_;
1864 if (!rtp_payload_registry_->RestoreOriginalPacket(
1865 &restored_packet_ptr, packet, &packet_length, rtp_receiver_->SSRC(),
1866 header)) {
1867 WEBRTC_TRACE(webrtc::kTraceDebug, webrtc::kTraceVoice, _channelId,
1868 "Incoming RTX packet: invalid RTP header");
1869 return false;
1870 }
1871 restored_packet_in_use_ = true;
1872 bool ret = OnRecoveredPacket(restored_packet_ptr, packet_length);
1873 restored_packet_in_use_ = false;
1874 return ret;
1875}
1876
1877bool Channel::IsPacketInOrder(const RTPHeader& header) const {
1878 StreamStatistician* statistician =
1879 rtp_receive_statistics_->GetStatistician(header.ssrc);
1880 if (!statistician)
1881 return false;
1882 return statistician->IsPacketInOrder(header.sequenceNumber);
niklase@google.com470e71d2011-07-07 08:21:25 +00001883}
1884
stefan@webrtc.org48df3812013-11-08 15:18:52 +00001885bool Channel::IsPacketRetransmitted(const RTPHeader& header,
1886 bool in_order) const {
stefan@webrtc.org7bb8f022013-09-06 13:40:11 +00001887 // Retransmissions are handled separately if RTX is enabled.
1888 if (rtp_payload_registry_->RtxEnabled())
1889 return false;
1890 StreamStatistician* statistician =
1891 rtp_receive_statistics_->GetStatistician(header.ssrc);
1892 if (!statistician)
1893 return false;
1894 // Check if this is a retransmission.
1895 uint16_t min_rtt = 0;
1896 _rtpRtcpModule->RTT(rtp_receiver_->SSRC(), NULL, NULL, &min_rtt, NULL);
stefan@webrtc.org48df3812013-11-08 15:18:52 +00001897 return !in_order &&
stefan@webrtc.org7bb8f022013-09-06 13:40:11 +00001898 statistician->IsRetransmitOfOldPacket(header, min_rtt);
wu@webrtc.org822fbd82013-08-15 23:38:54 +00001899}
1900
pbos@webrtc.org6141e132013-04-09 10:09:10 +00001901int32_t Channel::ReceivedRTCPPacket(const int8_t* data, int32_t length) {
pwestin@webrtc.org0c459572013-04-03 15:43:57 +00001902 WEBRTC_TRACE(kTraceStream, kTraceVoice, VoEId(_instanceId,_channelId),
1903 "Channel::ReceivedRTCPPacket()");
1904 // Store playout timestamp for the received RTCP packet
pwestin@webrtc.org1de01352013-04-11 20:23:35 +00001905 UpdatePlayoutTimestamp(true);
pwestin@webrtc.org0c459572013-04-03 15:43:57 +00001906
1907 // Dump the RTCP packet to a file (if RTP dump is enabled).
pbos@webrtc.org6141e132013-04-09 10:09:10 +00001908 if (_rtpDumpIn.DumpPacket((const uint8_t*)data,
1909 (uint16_t)length) == -1) {
pwestin@webrtc.org0c459572013-04-03 15:43:57 +00001910 WEBRTC_TRACE(kTraceWarning, kTraceVoice,
1911 VoEId(_instanceId,_channelId),
1912 "Channel::SendPacket() RTCP dump to input file failed");
1913 }
1914
1915 // Deliver RTCP packet to RTP/RTCP module for parsing
stefan@webrtc.orga5cb98c2013-05-29 12:12:51 +00001916 if (_rtpRtcpModule->IncomingRtcpPacket((const uint8_t*)data,
1917 (uint16_t)length) == -1) {
pwestin@webrtc.org0c459572013-04-03 15:43:57 +00001918 _engineStatisticsPtr->SetLastError(
1919 VE_SOCKET_TRANSPORT_MODULE_ERROR, kTraceWarning,
1920 "Channel::IncomingRTPPacket() RTCP packet is invalid");
1921 }
wu@webrtc.org82c4b852014-05-20 22:55:01 +00001922
1923 ntp_estimator_->UpdateRtcpTimestamp(rtp_receiver_->SSRC(),
1924 _rtpRtcpModule.get());
pwestin@webrtc.org0c459572013-04-03 15:43:57 +00001925 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +00001926}
1927
niklase@google.com470e71d2011-07-07 08:21:25 +00001928int Channel::StartPlayingFileLocally(const char* fileName,
pbos@webrtc.org92135212013-05-14 08:31:39 +00001929 bool loop,
1930 FileFormats format,
1931 int startPosition,
1932 float volumeScaling,
1933 int stopPosition,
niklase@google.com470e71d2011-07-07 08:21:25 +00001934 const CodecInst* codecInst)
1935{
1936 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId,_channelId),
1937 "Channel::StartPlayingFileLocally(fileNameUTF8[]=%s, loop=%d,"
1938 " format=%d, volumeScaling=%5.3f, startPosition=%d, "
1939 "stopPosition=%d)", fileName, loop, format, volumeScaling,
1940 startPosition, stopPosition);
1941
henrika@webrtc.org944cbeb2014-03-18 10:32:33 +00001942 if (channel_state_.Get().output_file_playing)
niklase@google.com470e71d2011-07-07 08:21:25 +00001943 {
1944 _engineStatisticsPtr->SetLastError(
1945 VE_ALREADY_PLAYING, kTraceError,
1946 "StartPlayingFileLocally() is already playing");
1947 return -1;
1948 }
1949
niklase@google.com470e71d2011-07-07 08:21:25 +00001950 {
mflodman@webrtc.org9a065d12012-03-07 08:12:21 +00001951 CriticalSectionScoped cs(&_fileCritSect);
henrike@webrtc.orgb37c6282011-10-31 23:53:04 +00001952
1953 if (_outputFilePlayerPtr)
1954 {
1955 _outputFilePlayerPtr->RegisterModuleFileCallback(NULL);
1956 FilePlayer::DestroyFilePlayer(_outputFilePlayerPtr);
1957 _outputFilePlayerPtr = NULL;
1958 }
1959
1960 _outputFilePlayerPtr = FilePlayer::CreateFilePlayer(
1961 _outputFilePlayerId, (const FileFormats)format);
1962
1963 if (_outputFilePlayerPtr == NULL)
1964 {
1965 _engineStatisticsPtr->SetLastError(
1966 VE_INVALID_ARGUMENT, kTraceError,
henrike@webrtc.org31d30702011-11-18 19:59:32 +00001967 "StartPlayingFileLocally() filePlayer format is not correct");
henrike@webrtc.orgb37c6282011-10-31 23:53:04 +00001968 return -1;
1969 }
1970
pbos@webrtc.org6141e132013-04-09 10:09:10 +00001971 const uint32_t notificationTime(0);
henrike@webrtc.orgb37c6282011-10-31 23:53:04 +00001972
1973 if (_outputFilePlayerPtr->StartPlayingFile(
1974 fileName,
1975 loop,
1976 startPosition,
1977 volumeScaling,
1978 notificationTime,
1979 stopPosition,
1980 (const CodecInst*)codecInst) != 0)
1981 {
1982 _engineStatisticsPtr->SetLastError(
1983 VE_BAD_FILE, kTraceError,
1984 "StartPlayingFile() failed to start file playout");
1985 _outputFilePlayerPtr->StopPlayingFile();
1986 FilePlayer::DestroyFilePlayer(_outputFilePlayerPtr);
1987 _outputFilePlayerPtr = NULL;
1988 return -1;
1989 }
1990 _outputFilePlayerPtr->RegisterModuleFileCallback(this);
henrika@webrtc.org944cbeb2014-03-18 10:32:33 +00001991 channel_state_.SetOutputFilePlaying(true);
niklase@google.com470e71d2011-07-07 08:21:25 +00001992 }
braveyao@webrtc.orgab129902012-06-04 03:26:39 +00001993
1994 if (RegisterFilePlayingToMixer() != 0)
henrike@webrtc.org066f9e52011-10-28 23:15:47 +00001995 return -1;
niklase@google.com470e71d2011-07-07 08:21:25 +00001996
1997 return 0;
1998}
1999
2000int Channel::StartPlayingFileLocally(InStream* stream,
pbos@webrtc.org92135212013-05-14 08:31:39 +00002001 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::StartPlayingFileLocally(format=%d,"
2009 " volumeScaling=%5.3f, startPosition=%d, stopPosition=%d)",
2010 format, volumeScaling, startPosition, stopPosition);
2011
2012 if(stream == NULL)
2013 {
2014 _engineStatisticsPtr->SetLastError(
2015 VE_BAD_FILE, kTraceError,
2016 "StartPlayingFileLocally() NULL as input stream");
2017 return -1;
2018 }
2019
2020
henrika@webrtc.org944cbeb2014-03-18 10:32:33 +00002021 if (channel_state_.Get().output_file_playing)
niklase@google.com470e71d2011-07-07 08:21:25 +00002022 {
2023 _engineStatisticsPtr->SetLastError(
2024 VE_ALREADY_PLAYING, kTraceError,
2025 "StartPlayingFileLocally() is already playing");
2026 return -1;
2027 }
2028
niklase@google.com470e71d2011-07-07 08:21:25 +00002029 {
mflodman@webrtc.org9a065d12012-03-07 08:12:21 +00002030 CriticalSectionScoped cs(&_fileCritSect);
henrike@webrtc.orgb37c6282011-10-31 23:53:04 +00002031
2032 // Destroy the old instance
2033 if (_outputFilePlayerPtr)
2034 {
2035 _outputFilePlayerPtr->RegisterModuleFileCallback(NULL);
2036 FilePlayer::DestroyFilePlayer(_outputFilePlayerPtr);
2037 _outputFilePlayerPtr = NULL;
2038 }
2039
2040 // Create the instance
2041 _outputFilePlayerPtr = FilePlayer::CreateFilePlayer(
2042 _outputFilePlayerId,
2043 (const FileFormats)format);
2044
2045 if (_outputFilePlayerPtr == NULL)
2046 {
2047 _engineStatisticsPtr->SetLastError(
2048 VE_INVALID_ARGUMENT, kTraceError,
2049 "StartPlayingFileLocally() filePlayer format isnot correct");
2050 return -1;
2051 }
2052
pbos@webrtc.org6141e132013-04-09 10:09:10 +00002053 const uint32_t notificationTime(0);
henrike@webrtc.orgb37c6282011-10-31 23:53:04 +00002054
2055 if (_outputFilePlayerPtr->StartPlayingFile(*stream, startPosition,
2056 volumeScaling,
2057 notificationTime,
2058 stopPosition, codecInst) != 0)
2059 {
2060 _engineStatisticsPtr->SetLastError(VE_BAD_FILE, kTraceError,
2061 "StartPlayingFile() failed to "
2062 "start file playout");
2063 _outputFilePlayerPtr->StopPlayingFile();
2064 FilePlayer::DestroyFilePlayer(_outputFilePlayerPtr);
2065 _outputFilePlayerPtr = NULL;
2066 return -1;
2067 }
2068 _outputFilePlayerPtr->RegisterModuleFileCallback(this);
henrika@webrtc.org944cbeb2014-03-18 10:32:33 +00002069 channel_state_.SetOutputFilePlaying(true);
niklase@google.com470e71d2011-07-07 08:21:25 +00002070 }
braveyao@webrtc.orgab129902012-06-04 03:26:39 +00002071
2072 if (RegisterFilePlayingToMixer() != 0)
henrike@webrtc.org066f9e52011-10-28 23:15:47 +00002073 return -1;
niklase@google.com470e71d2011-07-07 08:21:25 +00002074
niklase@google.com470e71d2011-07-07 08:21:25 +00002075 return 0;
2076}
2077
2078int Channel::StopPlayingFileLocally()
2079{
2080 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId,_channelId),
2081 "Channel::StopPlayingFileLocally()");
2082
henrika@webrtc.org944cbeb2014-03-18 10:32:33 +00002083 if (!channel_state_.Get().output_file_playing)
niklase@google.com470e71d2011-07-07 08:21:25 +00002084 {
2085 _engineStatisticsPtr->SetLastError(
2086 VE_INVALID_OPERATION, kTraceWarning,
2087 "StopPlayingFileLocally() isnot playing");
2088 return 0;
2089 }
2090
niklase@google.com470e71d2011-07-07 08:21:25 +00002091 {
mflodman@webrtc.org9a065d12012-03-07 08:12:21 +00002092 CriticalSectionScoped cs(&_fileCritSect);
henrike@webrtc.orgb37c6282011-10-31 23:53:04 +00002093
2094 if (_outputFilePlayerPtr->StopPlayingFile() != 0)
2095 {
2096 _engineStatisticsPtr->SetLastError(
2097 VE_STOP_RECORDING_FAILED, kTraceError,
2098 "StopPlayingFile() could not stop playing");
2099 return -1;
2100 }
2101 _outputFilePlayerPtr->RegisterModuleFileCallback(NULL);
2102 FilePlayer::DestroyFilePlayer(_outputFilePlayerPtr);
2103 _outputFilePlayerPtr = NULL;
henrika@webrtc.org944cbeb2014-03-18 10:32:33 +00002104 channel_state_.SetOutputFilePlaying(false);
niklase@google.com470e71d2011-07-07 08:21:25 +00002105 }
henrike@webrtc.orgb37c6282011-10-31 23:53:04 +00002106 // _fileCritSect cannot be taken while calling
2107 // SetAnonymousMixibilityStatus. Refer to comments in
2108 // StartPlayingFileLocally(const char* ...) for more details.
henrike@webrtc.org066f9e52011-10-28 23:15:47 +00002109 if (_outputMixerPtr->SetAnonymousMixabilityStatus(*this, false) != 0)
2110 {
2111 _engineStatisticsPtr->SetLastError(
2112 VE_AUDIO_CONF_MIX_MODULE_ERROR, kTraceError,
henrike@webrtc.orgb37c6282011-10-31 23:53:04 +00002113 "StopPlayingFile() failed to stop participant from playing as"
2114 "file in the mixer");
henrike@webrtc.org066f9e52011-10-28 23:15:47 +00002115 return -1;
2116 }
niklase@google.com470e71d2011-07-07 08:21:25 +00002117
2118 return 0;
2119}
2120
2121int Channel::IsPlayingFileLocally() const
2122{
2123 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId,_channelId),
2124 "Channel::IsPlayingFileLocally()");
2125
henrika@webrtc.org944cbeb2014-03-18 10:32:33 +00002126 return channel_state_.Get().output_file_playing;
niklase@google.com470e71d2011-07-07 08:21:25 +00002127}
2128
braveyao@webrtc.orgab129902012-06-04 03:26:39 +00002129int Channel::RegisterFilePlayingToMixer()
2130{
2131 // Return success for not registering for file playing to mixer if:
2132 // 1. playing file before playout is started on that channel.
2133 // 2. starting playout without file playing on that channel.
henrika@webrtc.org944cbeb2014-03-18 10:32:33 +00002134 if (!channel_state_.Get().playing ||
2135 !channel_state_.Get().output_file_playing)
braveyao@webrtc.orgab129902012-06-04 03:26:39 +00002136 {
2137 return 0;
2138 }
2139
2140 // |_fileCritSect| cannot be taken while calling
2141 // SetAnonymousMixabilityStatus() since as soon as the participant is added
2142 // frames can be pulled by the mixer. Since the frames are generated from
2143 // the file, _fileCritSect will be taken. This would result in a deadlock.
2144 if (_outputMixerPtr->SetAnonymousMixabilityStatus(*this, true) != 0)
2145 {
henrika@webrtc.org944cbeb2014-03-18 10:32:33 +00002146 channel_state_.SetOutputFilePlaying(false);
braveyao@webrtc.orgab129902012-06-04 03:26:39 +00002147 CriticalSectionScoped cs(&_fileCritSect);
braveyao@webrtc.orgab129902012-06-04 03:26:39 +00002148 _engineStatisticsPtr->SetLastError(
2149 VE_AUDIO_CONF_MIX_MODULE_ERROR, kTraceError,
2150 "StartPlayingFile() failed to add participant as file to mixer");
2151 _outputFilePlayerPtr->StopPlayingFile();
2152 FilePlayer::DestroyFilePlayer(_outputFilePlayerPtr);
2153 _outputFilePlayerPtr = NULL;
2154 return -1;
2155 }
2156
2157 return 0;
2158}
2159
niklase@google.com470e71d2011-07-07 08:21:25 +00002160int Channel::StartPlayingFileAsMicrophone(const char* fileName,
pbos@webrtc.org92135212013-05-14 08:31:39 +00002161 bool loop,
2162 FileFormats format,
2163 int startPosition,
2164 float volumeScaling,
2165 int stopPosition,
niklase@google.com470e71d2011-07-07 08:21:25 +00002166 const CodecInst* codecInst)
2167{
2168 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId,_channelId),
2169 "Channel::StartPlayingFileAsMicrophone(fileNameUTF8[]=%s, "
2170 "loop=%d, format=%d, volumeScaling=%5.3f, startPosition=%d, "
2171 "stopPosition=%d)", fileName, loop, format, volumeScaling,
2172 startPosition, stopPosition);
2173
henrika@webrtc.org944cbeb2014-03-18 10:32:33 +00002174 CriticalSectionScoped cs(&_fileCritSect);
2175
2176 if (channel_state_.Get().input_file_playing)
niklase@google.com470e71d2011-07-07 08:21:25 +00002177 {
2178 _engineStatisticsPtr->SetLastError(
2179 VE_ALREADY_PLAYING, kTraceWarning,
2180 "StartPlayingFileAsMicrophone() filePlayer is playing");
2181 return 0;
2182 }
2183
niklase@google.com470e71d2011-07-07 08:21:25 +00002184 // Destroy the old instance
2185 if (_inputFilePlayerPtr)
2186 {
2187 _inputFilePlayerPtr->RegisterModuleFileCallback(NULL);
2188 FilePlayer::DestroyFilePlayer(_inputFilePlayerPtr);
2189 _inputFilePlayerPtr = NULL;
2190 }
2191
2192 // Create the instance
2193 _inputFilePlayerPtr = FilePlayer::CreateFilePlayer(
2194 _inputFilePlayerId, (const FileFormats)format);
2195
2196 if (_inputFilePlayerPtr == NULL)
2197 {
2198 _engineStatisticsPtr->SetLastError(
2199 VE_INVALID_ARGUMENT, kTraceError,
2200 "StartPlayingFileAsMicrophone() filePlayer format isnot correct");
2201 return -1;
2202 }
2203
pbos@webrtc.org6141e132013-04-09 10:09:10 +00002204 const uint32_t notificationTime(0);
niklase@google.com470e71d2011-07-07 08:21:25 +00002205
2206 if (_inputFilePlayerPtr->StartPlayingFile(
2207 fileName,
2208 loop,
2209 startPosition,
2210 volumeScaling,
2211 notificationTime,
2212 stopPosition,
2213 (const CodecInst*)codecInst) != 0)
2214 {
2215 _engineStatisticsPtr->SetLastError(
2216 VE_BAD_FILE, kTraceError,
2217 "StartPlayingFile() failed to start file playout");
2218 _inputFilePlayerPtr->StopPlayingFile();
2219 FilePlayer::DestroyFilePlayer(_inputFilePlayerPtr);
2220 _inputFilePlayerPtr = NULL;
2221 return -1;
2222 }
2223 _inputFilePlayerPtr->RegisterModuleFileCallback(this);
henrika@webrtc.org944cbeb2014-03-18 10:32:33 +00002224 channel_state_.SetInputFilePlaying(true);
niklase@google.com470e71d2011-07-07 08:21:25 +00002225
2226 return 0;
2227}
2228
2229int Channel::StartPlayingFileAsMicrophone(InStream* stream,
pbos@webrtc.org92135212013-05-14 08:31:39 +00002230 FileFormats format,
2231 int startPosition,
2232 float volumeScaling,
2233 int stopPosition,
niklase@google.com470e71d2011-07-07 08:21:25 +00002234 const CodecInst* codecInst)
2235{
2236 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId,_channelId),
2237 "Channel::StartPlayingFileAsMicrophone(format=%d, "
2238 "volumeScaling=%5.3f, startPosition=%d, stopPosition=%d)",
2239 format, volumeScaling, startPosition, stopPosition);
2240
2241 if(stream == NULL)
2242 {
2243 _engineStatisticsPtr->SetLastError(
2244 VE_BAD_FILE, kTraceError,
2245 "StartPlayingFileAsMicrophone NULL as input stream");
2246 return -1;
2247 }
2248
henrika@webrtc.org944cbeb2014-03-18 10:32:33 +00002249 CriticalSectionScoped cs(&_fileCritSect);
2250
2251 if (channel_state_.Get().input_file_playing)
niklase@google.com470e71d2011-07-07 08:21:25 +00002252 {
2253 _engineStatisticsPtr->SetLastError(
2254 VE_ALREADY_PLAYING, kTraceWarning,
2255 "StartPlayingFileAsMicrophone() is playing");
2256 return 0;
2257 }
2258
niklase@google.com470e71d2011-07-07 08:21:25 +00002259 // Destroy the old instance
2260 if (_inputFilePlayerPtr)
2261 {
2262 _inputFilePlayerPtr->RegisterModuleFileCallback(NULL);
2263 FilePlayer::DestroyFilePlayer(_inputFilePlayerPtr);
2264 _inputFilePlayerPtr = NULL;
2265 }
2266
2267 // Create the instance
2268 _inputFilePlayerPtr = FilePlayer::CreateFilePlayer(
2269 _inputFilePlayerId, (const FileFormats)format);
2270
2271 if (_inputFilePlayerPtr == NULL)
2272 {
2273 _engineStatisticsPtr->SetLastError(
2274 VE_INVALID_ARGUMENT, kTraceError,
2275 "StartPlayingInputFile() filePlayer format isnot correct");
2276 return -1;
2277 }
2278
pbos@webrtc.org6141e132013-04-09 10:09:10 +00002279 const uint32_t notificationTime(0);
niklase@google.com470e71d2011-07-07 08:21:25 +00002280
2281 if (_inputFilePlayerPtr->StartPlayingFile(*stream, startPosition,
2282 volumeScaling, notificationTime,
2283 stopPosition, codecInst) != 0)
2284 {
2285 _engineStatisticsPtr->SetLastError(VE_BAD_FILE, kTraceError,
2286 "StartPlayingFile() failed to start "
2287 "file playout");
2288 _inputFilePlayerPtr->StopPlayingFile();
2289 FilePlayer::DestroyFilePlayer(_inputFilePlayerPtr);
2290 _inputFilePlayerPtr = NULL;
2291 return -1;
2292 }
andrew@webrtc.orgae1a58b2013-01-22 04:44:30 +00002293
niklase@google.com470e71d2011-07-07 08:21:25 +00002294 _inputFilePlayerPtr->RegisterModuleFileCallback(this);
henrika@webrtc.org944cbeb2014-03-18 10:32:33 +00002295 channel_state_.SetInputFilePlaying(true);
niklase@google.com470e71d2011-07-07 08:21:25 +00002296
2297 return 0;
2298}
2299
2300int Channel::StopPlayingFileAsMicrophone()
2301{
2302 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId,_channelId),
2303 "Channel::StopPlayingFileAsMicrophone()");
2304
henrika@webrtc.org944cbeb2014-03-18 10:32:33 +00002305 CriticalSectionScoped cs(&_fileCritSect);
2306
2307 if (!channel_state_.Get().input_file_playing)
niklase@google.com470e71d2011-07-07 08:21:25 +00002308 {
2309 _engineStatisticsPtr->SetLastError(
2310 VE_INVALID_OPERATION, kTraceWarning,
2311 "StopPlayingFileAsMicrophone() isnot playing");
2312 return 0;
2313 }
2314
niklase@google.com470e71d2011-07-07 08:21:25 +00002315 if (_inputFilePlayerPtr->StopPlayingFile() != 0)
2316 {
2317 _engineStatisticsPtr->SetLastError(
2318 VE_STOP_RECORDING_FAILED, kTraceError,
2319 "StopPlayingFile() could not stop playing");
2320 return -1;
2321 }
2322 _inputFilePlayerPtr->RegisterModuleFileCallback(NULL);
2323 FilePlayer::DestroyFilePlayer(_inputFilePlayerPtr);
2324 _inputFilePlayerPtr = NULL;
henrika@webrtc.org944cbeb2014-03-18 10:32:33 +00002325 channel_state_.SetInputFilePlaying(false);
niklase@google.com470e71d2011-07-07 08:21:25 +00002326
2327 return 0;
2328}
2329
2330int Channel::IsPlayingFileAsMicrophone() const
2331{
2332 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId,_channelId),
2333 "Channel::IsPlayingFileAsMicrophone()");
henrika@webrtc.org944cbeb2014-03-18 10:32:33 +00002334 return channel_state_.Get().input_file_playing;
niklase@google.com470e71d2011-07-07 08:21:25 +00002335}
2336
leozwang@webrtc.org813e4b02012-03-01 18:34:25 +00002337int Channel::StartRecordingPlayout(const char* fileName,
niklase@google.com470e71d2011-07-07 08:21:25 +00002338 const CodecInst* codecInst)
2339{
2340 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId,_channelId),
2341 "Channel::StartRecordingPlayout(fileName=%s)", fileName);
2342
2343 if (_outputFileRecording)
2344 {
2345 WEBRTC_TRACE(kTraceWarning, kTraceVoice, VoEId(_instanceId,-1),
2346 "StartRecordingPlayout() is already recording");
2347 return 0;
2348 }
2349
2350 FileFormats format;
pbos@webrtc.org6141e132013-04-09 10:09:10 +00002351 const uint32_t notificationTime(0); // Not supported in VoE
niklase@google.com470e71d2011-07-07 08:21:25 +00002352 CodecInst dummyCodec={100,"L16",16000,320,1,320000};
2353
niklas.enbom@webrtc.org40197d72012-03-26 08:45:47 +00002354 if ((codecInst != NULL) &&
2355 ((codecInst->channels < 1) || (codecInst->channels > 2)))
niklase@google.com470e71d2011-07-07 08:21:25 +00002356 {
2357 _engineStatisticsPtr->SetLastError(
2358 VE_BAD_ARGUMENT, kTraceError,
2359 "StartRecordingPlayout() invalid compression");
2360 return(-1);
2361 }
2362 if(codecInst == NULL)
2363 {
2364 format = kFileFormatPcm16kHzFile;
2365 codecInst=&dummyCodec;
2366 }
2367 else if((STR_CASE_CMP(codecInst->plname,"L16") == 0) ||
2368 (STR_CASE_CMP(codecInst->plname,"PCMU") == 0) ||
2369 (STR_CASE_CMP(codecInst->plname,"PCMA") == 0))
2370 {
2371 format = kFileFormatWavFile;
2372 }
2373 else
2374 {
2375 format = kFileFormatCompressedFile;
2376 }
2377
mflodman@webrtc.org9a065d12012-03-07 08:12:21 +00002378 CriticalSectionScoped cs(&_fileCritSect);
niklase@google.com470e71d2011-07-07 08:21:25 +00002379
2380 // Destroy the old instance
2381 if (_outputFileRecorderPtr)
2382 {
2383 _outputFileRecorderPtr->RegisterModuleFileCallback(NULL);
2384 FileRecorder::DestroyFileRecorder(_outputFileRecorderPtr);
2385 _outputFileRecorderPtr = NULL;
2386 }
2387
2388 _outputFileRecorderPtr = FileRecorder::CreateFileRecorder(
2389 _outputFileRecorderId, (const FileFormats)format);
2390 if (_outputFileRecorderPtr == NULL)
2391 {
2392 _engineStatisticsPtr->SetLastError(
2393 VE_INVALID_ARGUMENT, kTraceError,
2394 "StartRecordingPlayout() fileRecorder format isnot correct");
2395 return -1;
2396 }
2397
2398 if (_outputFileRecorderPtr->StartRecordingAudioFile(
2399 fileName, (const CodecInst&)*codecInst, notificationTime) != 0)
2400 {
2401 _engineStatisticsPtr->SetLastError(
2402 VE_BAD_FILE, kTraceError,
2403 "StartRecordingAudioFile() failed to start file recording");
2404 _outputFileRecorderPtr->StopRecording();
2405 FileRecorder::DestroyFileRecorder(_outputFileRecorderPtr);
2406 _outputFileRecorderPtr = NULL;
2407 return -1;
2408 }
2409 _outputFileRecorderPtr->RegisterModuleFileCallback(this);
2410 _outputFileRecording = true;
2411
2412 return 0;
2413}
2414
2415int Channel::StartRecordingPlayout(OutStream* stream,
2416 const CodecInst* codecInst)
2417{
2418 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId,_channelId),
2419 "Channel::StartRecordingPlayout()");
2420
2421 if (_outputFileRecording)
2422 {
2423 WEBRTC_TRACE(kTraceWarning, kTraceVoice, VoEId(_instanceId,-1),
2424 "StartRecordingPlayout() is already recording");
2425 return 0;
2426 }
2427
2428 FileFormats format;
pbos@webrtc.org6141e132013-04-09 10:09:10 +00002429 const uint32_t notificationTime(0); // Not supported in VoE
niklase@google.com470e71d2011-07-07 08:21:25 +00002430 CodecInst dummyCodec={100,"L16",16000,320,1,320000};
2431
2432 if (codecInst != NULL && codecInst->channels != 1)
2433 {
2434 _engineStatisticsPtr->SetLastError(
2435 VE_BAD_ARGUMENT, kTraceError,
2436 "StartRecordingPlayout() invalid compression");
2437 return(-1);
2438 }
2439 if(codecInst == NULL)
2440 {
2441 format = kFileFormatPcm16kHzFile;
2442 codecInst=&dummyCodec;
2443 }
2444 else if((STR_CASE_CMP(codecInst->plname,"L16") == 0) ||
2445 (STR_CASE_CMP(codecInst->plname,"PCMU") == 0) ||
2446 (STR_CASE_CMP(codecInst->plname,"PCMA") == 0))
2447 {
2448 format = kFileFormatWavFile;
2449 }
2450 else
2451 {
2452 format = kFileFormatCompressedFile;
2453 }
2454
mflodman@webrtc.org9a065d12012-03-07 08:12:21 +00002455 CriticalSectionScoped cs(&_fileCritSect);
niklase@google.com470e71d2011-07-07 08:21:25 +00002456
2457 // Destroy the old instance
2458 if (_outputFileRecorderPtr)
2459 {
2460 _outputFileRecorderPtr->RegisterModuleFileCallback(NULL);
2461 FileRecorder::DestroyFileRecorder(_outputFileRecorderPtr);
2462 _outputFileRecorderPtr = NULL;
2463 }
2464
2465 _outputFileRecorderPtr = FileRecorder::CreateFileRecorder(
2466 _outputFileRecorderId, (const FileFormats)format);
2467 if (_outputFileRecorderPtr == NULL)
2468 {
2469 _engineStatisticsPtr->SetLastError(
2470 VE_INVALID_ARGUMENT, kTraceError,
2471 "StartRecordingPlayout() fileRecorder format isnot correct");
2472 return -1;
2473 }
2474
2475 if (_outputFileRecorderPtr->StartRecordingAudioFile(*stream, *codecInst,
2476 notificationTime) != 0)
2477 {
2478 _engineStatisticsPtr->SetLastError(VE_BAD_FILE, kTraceError,
2479 "StartRecordingPlayout() failed to "
2480 "start file recording");
2481 _outputFileRecorderPtr->StopRecording();
2482 FileRecorder::DestroyFileRecorder(_outputFileRecorderPtr);
2483 _outputFileRecorderPtr = NULL;
2484 return -1;
2485 }
andrew@webrtc.orgae1a58b2013-01-22 04:44:30 +00002486
niklase@google.com470e71d2011-07-07 08:21:25 +00002487 _outputFileRecorderPtr->RegisterModuleFileCallback(this);
2488 _outputFileRecording = true;
2489
2490 return 0;
2491}
2492
2493int Channel::StopRecordingPlayout()
2494{
2495 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId,-1),
2496 "Channel::StopRecordingPlayout()");
2497
2498 if (!_outputFileRecording)
2499 {
2500 WEBRTC_TRACE(kTraceError, kTraceVoice, VoEId(_instanceId,-1),
2501 "StopRecordingPlayout() isnot recording");
2502 return -1;
2503 }
2504
2505
mflodman@webrtc.org9a065d12012-03-07 08:12:21 +00002506 CriticalSectionScoped cs(&_fileCritSect);
niklase@google.com470e71d2011-07-07 08:21:25 +00002507
2508 if (_outputFileRecorderPtr->StopRecording() != 0)
2509 {
2510 _engineStatisticsPtr->SetLastError(
2511 VE_STOP_RECORDING_FAILED, kTraceError,
2512 "StopRecording() could not stop recording");
2513 return(-1);
2514 }
2515 _outputFileRecorderPtr->RegisterModuleFileCallback(NULL);
2516 FileRecorder::DestroyFileRecorder(_outputFileRecorderPtr);
2517 _outputFileRecorderPtr = NULL;
2518 _outputFileRecording = false;
2519
2520 return 0;
2521}
2522
2523void
2524Channel::SetMixWithMicStatus(bool mix)
2525{
henrika@webrtc.org944cbeb2014-03-18 10:32:33 +00002526 CriticalSectionScoped cs(&_fileCritSect);
niklase@google.com470e71d2011-07-07 08:21:25 +00002527 _mixFileWithMicrophone=mix;
2528}
2529
2530int
pbos@webrtc.org6141e132013-04-09 10:09:10 +00002531Channel::GetSpeechOutputLevel(uint32_t& level) const
niklase@google.com470e71d2011-07-07 08:21:25 +00002532{
pbos@webrtc.org6141e132013-04-09 10:09:10 +00002533 int8_t currentLevel = _outputAudioLevel.Level();
2534 level = static_cast<int32_t> (currentLevel);
niklase@google.com470e71d2011-07-07 08:21:25 +00002535 WEBRTC_TRACE(kTraceStateInfo, kTraceVoice,
2536 VoEId(_instanceId,_channelId),
2537 "GetSpeechOutputLevel() => level=%u", level);
2538 return 0;
2539}
2540
2541int
pbos@webrtc.org6141e132013-04-09 10:09:10 +00002542Channel::GetSpeechOutputLevelFullRange(uint32_t& level) const
niklase@google.com470e71d2011-07-07 08:21:25 +00002543{
pbos@webrtc.org6141e132013-04-09 10:09:10 +00002544 int16_t currentLevel = _outputAudioLevel.LevelFullRange();
2545 level = static_cast<int32_t> (currentLevel);
niklase@google.com470e71d2011-07-07 08:21:25 +00002546 WEBRTC_TRACE(kTraceStateInfo, kTraceVoice,
2547 VoEId(_instanceId,_channelId),
2548 "GetSpeechOutputLevelFullRange() => level=%u", level);
2549 return 0;
2550}
2551
2552int
2553Channel::SetMute(bool enable)
2554{
wu@webrtc.org63420662013-10-17 18:28:55 +00002555 CriticalSectionScoped cs(&volume_settings_critsect_);
niklase@google.com470e71d2011-07-07 08:21:25 +00002556 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId,_channelId),
2557 "Channel::SetMute(enable=%d)", enable);
2558 _mute = enable;
2559 return 0;
2560}
2561
2562bool
2563Channel::Mute() const
2564{
wu@webrtc.org63420662013-10-17 18:28:55 +00002565 CriticalSectionScoped cs(&volume_settings_critsect_);
niklase@google.com470e71d2011-07-07 08:21:25 +00002566 return _mute;
2567}
2568
2569int
2570Channel::SetOutputVolumePan(float left, float right)
2571{
wu@webrtc.org63420662013-10-17 18:28:55 +00002572 CriticalSectionScoped cs(&volume_settings_critsect_);
niklase@google.com470e71d2011-07-07 08:21:25 +00002573 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId,_channelId),
2574 "Channel::SetOutputVolumePan()");
2575 _panLeft = left;
2576 _panRight = right;
2577 return 0;
2578}
2579
2580int
2581Channel::GetOutputVolumePan(float& left, float& right) const
2582{
wu@webrtc.org63420662013-10-17 18:28:55 +00002583 CriticalSectionScoped cs(&volume_settings_critsect_);
niklase@google.com470e71d2011-07-07 08:21:25 +00002584 left = _panLeft;
2585 right = _panRight;
2586 WEBRTC_TRACE(kTraceStateInfo, kTraceVoice,
2587 VoEId(_instanceId,_channelId),
2588 "GetOutputVolumePan() => left=%3.2f, right=%3.2f", left, right);
2589 return 0;
2590}
2591
2592int
2593Channel::SetChannelOutputVolumeScaling(float scaling)
2594{
wu@webrtc.org63420662013-10-17 18:28:55 +00002595 CriticalSectionScoped cs(&volume_settings_critsect_);
niklase@google.com470e71d2011-07-07 08:21:25 +00002596 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId,_channelId),
2597 "Channel::SetChannelOutputVolumeScaling()");
2598 _outputGain = scaling;
2599 return 0;
2600}
2601
2602int
2603Channel::GetChannelOutputVolumeScaling(float& scaling) const
2604{
wu@webrtc.org63420662013-10-17 18:28:55 +00002605 CriticalSectionScoped cs(&volume_settings_critsect_);
niklase@google.com470e71d2011-07-07 08:21:25 +00002606 scaling = _outputGain;
2607 WEBRTC_TRACE(kTraceStateInfo, kTraceVoice,
2608 VoEId(_instanceId,_channelId),
2609 "GetChannelOutputVolumeScaling() => scaling=%3.2f", scaling);
2610 return 0;
2611}
2612
niklase@google.com470e71d2011-07-07 08:21:25 +00002613int Channel::SendTelephoneEventOutband(unsigned char eventCode,
wu@webrtc.org822fbd82013-08-15 23:38:54 +00002614 int lengthMs, int attenuationDb,
2615 bool playDtmfEvent)
niklase@google.com470e71d2011-07-07 08:21:25 +00002616{
2617 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId, _channelId),
2618 "Channel::SendTelephoneEventOutband(..., playDtmfEvent=%d)",
2619 playDtmfEvent);
2620
2621 _playOutbandDtmfEvent = playDtmfEvent;
2622
pwestin@webrtc.org2853dde2012-05-11 11:08:54 +00002623 if (_rtpRtcpModule->SendTelephoneEventOutband(eventCode, lengthMs,
niklase@google.com470e71d2011-07-07 08:21:25 +00002624 attenuationDb) != 0)
2625 {
2626 _engineStatisticsPtr->SetLastError(
2627 VE_SEND_DTMF_FAILED,
2628 kTraceWarning,
2629 "SendTelephoneEventOutband() failed to send event");
2630 return -1;
2631 }
2632 return 0;
2633}
2634
2635int Channel::SendTelephoneEventInband(unsigned char eventCode,
2636 int lengthMs,
2637 int attenuationDb,
2638 bool playDtmfEvent)
2639{
2640 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId, _channelId),
2641 "Channel::SendTelephoneEventInband(..., playDtmfEvent=%d)",
2642 playDtmfEvent);
2643
2644 _playInbandDtmfEvent = playDtmfEvent;
2645 _inbandDtmfQueue.AddDtmf(eventCode, lengthMs, attenuationDb);
2646
2647 return 0;
2648}
2649
2650int
2651Channel::SetDtmfPlayoutStatus(bool enable)
2652{
2653 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId,_channelId),
2654 "Channel::SetDtmfPlayoutStatus()");
andrew@webrtc.orgeb524d92013-09-23 23:02:24 +00002655 if (audio_coding_->SetDtmfPlayoutStatus(enable) != 0)
niklase@google.com470e71d2011-07-07 08:21:25 +00002656 {
2657 _engineStatisticsPtr->SetLastError(
2658 VE_AUDIO_CODING_MODULE_ERROR, kTraceWarning,
2659 "SetDtmfPlayoutStatus() failed to set Dtmf playout");
2660 return -1;
2661 }
2662 return 0;
2663}
2664
2665bool
2666Channel::DtmfPlayoutStatus() const
2667{
andrew@webrtc.orgeb524d92013-09-23 23:02:24 +00002668 return audio_coding_->DtmfPlayoutStatus();
niklase@google.com470e71d2011-07-07 08:21:25 +00002669}
2670
2671int
2672Channel::SetSendTelephoneEventPayloadType(unsigned char type)
2673{
2674 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId,_channelId),
2675 "Channel::SetSendTelephoneEventPayloadType()");
andrew@webrtc.orgf81f9f82011-08-19 22:56:22 +00002676 if (type > 127)
niklase@google.com470e71d2011-07-07 08:21:25 +00002677 {
2678 _engineStatisticsPtr->SetLastError(
2679 VE_INVALID_ARGUMENT, kTraceError,
2680 "SetSendTelephoneEventPayloadType() invalid type");
2681 return -1;
2682 }
pbos@webrtc.org5b10d8f2013-07-11 15:50:07 +00002683 CodecInst codec = {};
pwestin@webrtc.org1da1ce02011-10-13 15:19:55 +00002684 codec.plfreq = 8000;
2685 codec.pltype = type;
2686 memcpy(codec.plname, "telephone-event", 16);
pwestin@webrtc.org2853dde2012-05-11 11:08:54 +00002687 if (_rtpRtcpModule->RegisterSendPayload(codec) != 0)
niklase@google.com470e71d2011-07-07 08:21:25 +00002688 {
henrika@webrtc.org4392d5f2013-04-17 07:34:25 +00002689 _rtpRtcpModule->DeRegisterSendPayload(codec.pltype);
2690 if (_rtpRtcpModule->RegisterSendPayload(codec) != 0) {
2691 _engineStatisticsPtr->SetLastError(
2692 VE_RTP_RTCP_MODULE_ERROR, kTraceError,
2693 "SetSendTelephoneEventPayloadType() failed to register send"
2694 "payload type");
2695 return -1;
2696 }
niklase@google.com470e71d2011-07-07 08:21:25 +00002697 }
2698 _sendTelephoneEventPayloadType = type;
2699 return 0;
2700}
2701
2702int
2703Channel::GetSendTelephoneEventPayloadType(unsigned char& type)
2704{
2705 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId,_channelId),
2706 "Channel::GetSendTelephoneEventPayloadType()");
2707 type = _sendTelephoneEventPayloadType;
2708 WEBRTC_TRACE(kTraceStateInfo, kTraceVoice,
2709 VoEId(_instanceId,_channelId),
2710 "GetSendTelephoneEventPayloadType() => type=%u", type);
2711 return 0;
2712}
2713
niklase@google.com470e71d2011-07-07 08:21:25 +00002714int
2715Channel::UpdateRxVadDetection(AudioFrame& audioFrame)
2716{
2717 WEBRTC_TRACE(kTraceStream, kTraceVoice, VoEId(_instanceId,_channelId),
2718 "Channel::UpdateRxVadDetection()");
2719
2720 int vadDecision = 1;
2721
andrew@webrtc.org63a50982012-05-02 23:56:37 +00002722 vadDecision = (audioFrame.vad_activity_ == AudioFrame::kVadActive)? 1 : 0;
niklase@google.com470e71d2011-07-07 08:21:25 +00002723
2724 if ((vadDecision != _oldVadDecision) && _rxVadObserverPtr)
2725 {
2726 OnRxVadDetected(vadDecision);
2727 _oldVadDecision = vadDecision;
2728 }
2729
2730 WEBRTC_TRACE(kTraceStream, kTraceVoice, VoEId(_instanceId,_channelId),
2731 "Channel::UpdateRxVadDetection() => vadDecision=%d",
2732 vadDecision);
2733 return 0;
2734}
2735
2736int
2737Channel::RegisterRxVadObserver(VoERxVadCallback &observer)
2738{
2739 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId,_channelId),
2740 "Channel::RegisterRxVadObserver()");
mflodman@webrtc.org9a065d12012-03-07 08:12:21 +00002741 CriticalSectionScoped cs(&_callbackCritSect);
niklase@google.com470e71d2011-07-07 08:21:25 +00002742
2743 if (_rxVadObserverPtr)
2744 {
2745 _engineStatisticsPtr->SetLastError(
2746 VE_INVALID_OPERATION, kTraceError,
2747 "RegisterRxVadObserver() observer already enabled");
2748 return -1;
2749 }
niklase@google.com470e71d2011-07-07 08:21:25 +00002750 _rxVadObserverPtr = &observer;
2751 _RxVadDetection = true;
2752 return 0;
2753}
2754
2755int
2756Channel::DeRegisterRxVadObserver()
2757{
2758 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId,_channelId),
2759 "Channel::DeRegisterRxVadObserver()");
mflodman@webrtc.org9a065d12012-03-07 08:12:21 +00002760 CriticalSectionScoped cs(&_callbackCritSect);
niklase@google.com470e71d2011-07-07 08:21:25 +00002761
2762 if (!_rxVadObserverPtr)
2763 {
2764 _engineStatisticsPtr->SetLastError(
2765 VE_INVALID_OPERATION, kTraceWarning,
2766 "DeRegisterRxVadObserver() observer already disabled");
2767 return 0;
2768 }
2769 _rxVadObserverPtr = NULL;
2770 _RxVadDetection = false;
2771 return 0;
2772}
2773
2774int
2775Channel::VoiceActivityIndicator(int &activity)
2776{
2777 activity = _sendFrameType;
2778
2779 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId,_channelId),
andrew@webrtc.org6c264cc2013-10-04 17:54:09 +00002780 "Channel::VoiceActivityIndicator(indicator=%d)", activity);
niklase@google.com470e71d2011-07-07 08:21:25 +00002781 return 0;
2782}
2783
2784#ifdef WEBRTC_VOICE_ENGINE_AGC
2785
2786int
pbos@webrtc.org92135212013-05-14 08:31:39 +00002787Channel::SetRxAgcStatus(bool enable, AgcModes mode)
niklase@google.com470e71d2011-07-07 08:21:25 +00002788{
2789 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId,_channelId),
2790 "Channel::SetRxAgcStatus(enable=%d, mode=%d)",
2791 (int)enable, (int)mode);
2792
andrew@webrtc.org6c264cc2013-10-04 17:54:09 +00002793 GainControl::Mode agcMode = kDefaultRxAgcMode;
niklase@google.com470e71d2011-07-07 08:21:25 +00002794 switch (mode)
2795 {
2796 case kAgcDefault:
niklase@google.com470e71d2011-07-07 08:21:25 +00002797 break;
2798 case kAgcUnchanged:
andrew@webrtc.orgf3930e92013-09-18 22:37:32 +00002799 agcMode = rx_audioproc_->gain_control()->mode();
niklase@google.com470e71d2011-07-07 08:21:25 +00002800 break;
2801 case kAgcFixedDigital:
2802 agcMode = GainControl::kFixedDigital;
2803 break;
2804 case kAgcAdaptiveDigital:
2805 agcMode =GainControl::kAdaptiveDigital;
2806 break;
2807 default:
2808 _engineStatisticsPtr->SetLastError(
2809 VE_INVALID_ARGUMENT, kTraceError,
2810 "SetRxAgcStatus() invalid Agc mode");
2811 return -1;
2812 }
2813
andrew@webrtc.orgf3930e92013-09-18 22:37:32 +00002814 if (rx_audioproc_->gain_control()->set_mode(agcMode) != 0)
niklase@google.com470e71d2011-07-07 08:21:25 +00002815 {
2816 _engineStatisticsPtr->SetLastError(
2817 VE_APM_ERROR, kTraceError,
2818 "SetRxAgcStatus() failed to set Agc mode");
2819 return -1;
2820 }
andrew@webrtc.orgf3930e92013-09-18 22:37:32 +00002821 if (rx_audioproc_->gain_control()->Enable(enable) != 0)
niklase@google.com470e71d2011-07-07 08:21:25 +00002822 {
2823 _engineStatisticsPtr->SetLastError(
2824 VE_APM_ERROR, kTraceError,
2825 "SetRxAgcStatus() failed to set Agc state");
2826 return -1;
2827 }
2828
2829 _rxAgcIsEnabled = enable;
henrika@webrtc.org944cbeb2014-03-18 10:32:33 +00002830 channel_state_.SetRxApmIsEnabled(_rxAgcIsEnabled || _rxNsIsEnabled);
niklase@google.com470e71d2011-07-07 08:21:25 +00002831
2832 return 0;
2833}
2834
2835int
2836Channel::GetRxAgcStatus(bool& enabled, AgcModes& mode)
2837{
2838 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId,_channelId),
2839 "Channel::GetRxAgcStatus(enable=?, mode=?)");
2840
andrew@webrtc.orgf3930e92013-09-18 22:37:32 +00002841 bool enable = rx_audioproc_->gain_control()->is_enabled();
niklase@google.com470e71d2011-07-07 08:21:25 +00002842 GainControl::Mode agcMode =
andrew@webrtc.orgf3930e92013-09-18 22:37:32 +00002843 rx_audioproc_->gain_control()->mode();
niklase@google.com470e71d2011-07-07 08:21:25 +00002844
2845 enabled = enable;
2846
2847 switch (agcMode)
2848 {
2849 case GainControl::kFixedDigital:
2850 mode = kAgcFixedDigital;
2851 break;
2852 case GainControl::kAdaptiveDigital:
2853 mode = kAgcAdaptiveDigital;
2854 break;
2855 default:
2856 _engineStatisticsPtr->SetLastError(
2857 VE_APM_ERROR, kTraceError,
2858 "GetRxAgcStatus() invalid Agc mode");
2859 return -1;
2860 }
2861
2862 return 0;
2863}
2864
2865int
pbos@webrtc.org92135212013-05-14 08:31:39 +00002866Channel::SetRxAgcConfig(AgcConfig config)
niklase@google.com470e71d2011-07-07 08:21:25 +00002867{
2868 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId,_channelId),
2869 "Channel::SetRxAgcConfig()");
2870
andrew@webrtc.orgf3930e92013-09-18 22:37:32 +00002871 if (rx_audioproc_->gain_control()->set_target_level_dbfs(
niklase@google.com470e71d2011-07-07 08:21:25 +00002872 config.targetLeveldBOv) != 0)
2873 {
2874 _engineStatisticsPtr->SetLastError(
2875 VE_APM_ERROR, kTraceError,
2876 "SetRxAgcConfig() failed to set target peak |level|"
2877 "(or envelope) of the Agc");
2878 return -1;
2879 }
andrew@webrtc.orgf3930e92013-09-18 22:37:32 +00002880 if (rx_audioproc_->gain_control()->set_compression_gain_db(
niklase@google.com470e71d2011-07-07 08:21:25 +00002881 config.digitalCompressionGaindB) != 0)
2882 {
2883 _engineStatisticsPtr->SetLastError(
2884 VE_APM_ERROR, kTraceError,
2885 "SetRxAgcConfig() failed to set the range in |gain| the"
2886 " digital compression stage may apply");
2887 return -1;
2888 }
andrew@webrtc.orgf3930e92013-09-18 22:37:32 +00002889 if (rx_audioproc_->gain_control()->enable_limiter(
niklase@google.com470e71d2011-07-07 08:21:25 +00002890 config.limiterEnable) != 0)
2891 {
2892 _engineStatisticsPtr->SetLastError(
2893 VE_APM_ERROR, kTraceError,
2894 "SetRxAgcConfig() failed to set hard limiter to the signal");
2895 return -1;
2896 }
2897
2898 return 0;
2899}
2900
2901int
2902Channel::GetRxAgcConfig(AgcConfig& config)
2903{
2904 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId,_channelId),
2905 "Channel::GetRxAgcConfig(config=%?)");
2906
2907 config.targetLeveldBOv =
andrew@webrtc.orgf3930e92013-09-18 22:37:32 +00002908 rx_audioproc_->gain_control()->target_level_dbfs();
niklase@google.com470e71d2011-07-07 08:21:25 +00002909 config.digitalCompressionGaindB =
andrew@webrtc.orgf3930e92013-09-18 22:37:32 +00002910 rx_audioproc_->gain_control()->compression_gain_db();
niklase@google.com470e71d2011-07-07 08:21:25 +00002911 config.limiterEnable =
andrew@webrtc.orgf3930e92013-09-18 22:37:32 +00002912 rx_audioproc_->gain_control()->is_limiter_enabled();
niklase@google.com470e71d2011-07-07 08:21:25 +00002913
2914 WEBRTC_TRACE(kTraceStateInfo, kTraceVoice,
2915 VoEId(_instanceId,_channelId), "GetRxAgcConfig() => "
2916 "targetLeveldBOv=%u, digitalCompressionGaindB=%u,"
2917 " limiterEnable=%d",
2918 config.targetLeveldBOv,
2919 config.digitalCompressionGaindB,
2920 config.limiterEnable);
2921
2922 return 0;
2923}
2924
2925#endif // #ifdef WEBRTC_VOICE_ENGINE_AGC
2926
2927#ifdef WEBRTC_VOICE_ENGINE_NR
2928
2929int
pbos@webrtc.org92135212013-05-14 08:31:39 +00002930Channel::SetRxNsStatus(bool enable, NsModes mode)
niklase@google.com470e71d2011-07-07 08:21:25 +00002931{
2932 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId,_channelId),
2933 "Channel::SetRxNsStatus(enable=%d, mode=%d)",
2934 (int)enable, (int)mode);
2935
andrew@webrtc.org6c264cc2013-10-04 17:54:09 +00002936 NoiseSuppression::Level nsLevel = kDefaultNsMode;
niklase@google.com470e71d2011-07-07 08:21:25 +00002937 switch (mode)
2938 {
2939
2940 case kNsDefault:
niklase@google.com470e71d2011-07-07 08:21:25 +00002941 break;
2942 case kNsUnchanged:
andrew@webrtc.orgf3930e92013-09-18 22:37:32 +00002943 nsLevel = rx_audioproc_->noise_suppression()->level();
niklase@google.com470e71d2011-07-07 08:21:25 +00002944 break;
2945 case kNsConference:
2946 nsLevel = NoiseSuppression::kHigh;
2947 break;
2948 case kNsLowSuppression:
2949 nsLevel = NoiseSuppression::kLow;
2950 break;
2951 case kNsModerateSuppression:
2952 nsLevel = NoiseSuppression::kModerate;
2953 break;
2954 case kNsHighSuppression:
2955 nsLevel = NoiseSuppression::kHigh;
2956 break;
2957 case kNsVeryHighSuppression:
2958 nsLevel = NoiseSuppression::kVeryHigh;
2959 break;
niklase@google.com470e71d2011-07-07 08:21:25 +00002960 }
2961
andrew@webrtc.orgf3930e92013-09-18 22:37:32 +00002962 if (rx_audioproc_->noise_suppression()->set_level(nsLevel)
niklase@google.com470e71d2011-07-07 08:21:25 +00002963 != 0)
2964 {
2965 _engineStatisticsPtr->SetLastError(
2966 VE_APM_ERROR, kTraceError,
andrew@webrtc.org6c264cc2013-10-04 17:54:09 +00002967 "SetRxNsStatus() failed to set NS level");
niklase@google.com470e71d2011-07-07 08:21:25 +00002968 return -1;
2969 }
andrew@webrtc.orgf3930e92013-09-18 22:37:32 +00002970 if (rx_audioproc_->noise_suppression()->Enable(enable) != 0)
niklase@google.com470e71d2011-07-07 08:21:25 +00002971 {
2972 _engineStatisticsPtr->SetLastError(
2973 VE_APM_ERROR, kTraceError,
andrew@webrtc.org6c264cc2013-10-04 17:54:09 +00002974 "SetRxNsStatus() failed to set NS state");
niklase@google.com470e71d2011-07-07 08:21:25 +00002975 return -1;
2976 }
2977
2978 _rxNsIsEnabled = enable;
henrika@webrtc.org944cbeb2014-03-18 10:32:33 +00002979 channel_state_.SetRxApmIsEnabled(_rxAgcIsEnabled || _rxNsIsEnabled);
niklase@google.com470e71d2011-07-07 08:21:25 +00002980
2981 return 0;
2982}
2983
2984int
2985Channel::GetRxNsStatus(bool& enabled, NsModes& mode)
2986{
2987 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId,_channelId),
2988 "Channel::GetRxNsStatus(enable=?, mode=?)");
2989
2990 bool enable =
andrew@webrtc.orgf3930e92013-09-18 22:37:32 +00002991 rx_audioproc_->noise_suppression()->is_enabled();
niklase@google.com470e71d2011-07-07 08:21:25 +00002992 NoiseSuppression::Level ncLevel =
andrew@webrtc.orgf3930e92013-09-18 22:37:32 +00002993 rx_audioproc_->noise_suppression()->level();
niklase@google.com470e71d2011-07-07 08:21:25 +00002994
2995 enabled = enable;
2996
2997 switch (ncLevel)
2998 {
2999 case NoiseSuppression::kLow:
3000 mode = kNsLowSuppression;
3001 break;
3002 case NoiseSuppression::kModerate:
3003 mode = kNsModerateSuppression;
3004 break;
3005 case NoiseSuppression::kHigh:
3006 mode = kNsHighSuppression;
3007 break;
3008 case NoiseSuppression::kVeryHigh:
3009 mode = kNsVeryHighSuppression;
3010 break;
niklase@google.com470e71d2011-07-07 08:21:25 +00003011 }
3012
3013 WEBRTC_TRACE(kTraceStateInfo, kTraceVoice,
3014 VoEId(_instanceId,_channelId),
3015 "GetRxNsStatus() => enabled=%d, mode=%d", enabled, mode);
3016 return 0;
3017}
3018
3019#endif // #ifdef WEBRTC_VOICE_ENGINE_NR
3020
3021int
niklase@google.com470e71d2011-07-07 08:21:25 +00003022Channel::RegisterRTCPObserver(VoERTCPObserver& observer)
3023{
3024 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId,_channelId),
3025 "Channel::RegisterRTCPObserver()");
mflodman@webrtc.org9a065d12012-03-07 08:12:21 +00003026 CriticalSectionScoped cs(&_callbackCritSect);
niklase@google.com470e71d2011-07-07 08:21:25 +00003027
3028 if (_rtcpObserverPtr)
3029 {
3030 _engineStatisticsPtr->SetLastError(
3031 VE_INVALID_OPERATION, kTraceError,
3032 "RegisterRTCPObserver() observer already enabled");
3033 return -1;
3034 }
3035
3036 _rtcpObserverPtr = &observer;
3037 _rtcpObserver = true;
3038
3039 return 0;
3040}
3041
3042int
3043Channel::DeRegisterRTCPObserver()
3044{
3045 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId, _channelId),
3046 "Channel::DeRegisterRTCPObserver()");
mflodman@webrtc.org9a065d12012-03-07 08:12:21 +00003047 CriticalSectionScoped cs(&_callbackCritSect);
niklase@google.com470e71d2011-07-07 08:21:25 +00003048
3049 if (!_rtcpObserverPtr)
3050 {
3051 _engineStatisticsPtr->SetLastError(
3052 VE_INVALID_OPERATION, kTraceWarning,
3053 "DeRegisterRTCPObserver() observer already disabled");
3054 return 0;
3055 }
3056
3057 _rtcpObserver = false;
3058 _rtcpObserverPtr = NULL;
3059
3060 return 0;
3061}
3062
3063int
3064Channel::SetLocalSSRC(unsigned int ssrc)
3065{
3066 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId, _channelId),
3067 "Channel::SetLocalSSRC()");
henrika@webrtc.org944cbeb2014-03-18 10:32:33 +00003068 if (channel_state_.Get().sending)
niklase@google.com470e71d2011-07-07 08:21:25 +00003069 {
3070 _engineStatisticsPtr->SetLastError(
3071 VE_ALREADY_SENDING, kTraceError,
3072 "SetLocalSSRC() already sending");
3073 return -1;
3074 }
stefan@webrtc.orgef927552014-06-05 08:25:29 +00003075 _rtpRtcpModule->SetSSRC(ssrc);
niklase@google.com470e71d2011-07-07 08:21:25 +00003076 return 0;
3077}
3078
3079int
3080Channel::GetLocalSSRC(unsigned int& ssrc)
3081{
pwestin@webrtc.org2853dde2012-05-11 11:08:54 +00003082 ssrc = _rtpRtcpModule->SSRC();
niklase@google.com470e71d2011-07-07 08:21:25 +00003083 WEBRTC_TRACE(kTraceStateInfo, kTraceVoice,
3084 VoEId(_instanceId,_channelId),
3085 "GetLocalSSRC() => ssrc=%lu", ssrc);
3086 return 0;
3087}
3088
3089int
3090Channel::GetRemoteSSRC(unsigned int& ssrc)
3091{
wu@webrtc.org822fbd82013-08-15 23:38:54 +00003092 ssrc = rtp_receiver_->SSRC();
niklase@google.com470e71d2011-07-07 08:21:25 +00003093 WEBRTC_TRACE(kTraceStateInfo, kTraceVoice,
3094 VoEId(_instanceId,_channelId),
3095 "GetRemoteSSRC() => ssrc=%lu", ssrc);
3096 return 0;
3097}
3098
wu@webrtc.orgebdb0e32014-03-06 23:49:08 +00003099int Channel::SetSendAudioLevelIndicationStatus(bool enable, unsigned char id) {
andrew@webrtc.orgf3930e92013-09-18 22:37:32 +00003100 _includeAudioLevelIndication = enable;
wu@webrtc.orgebdb0e32014-03-06 23:49:08 +00003101 return SetSendRtpHeaderExtension(enable, kRtpExtensionAudioLevel, id);
niklase@google.com470e71d2011-07-07 08:21:25 +00003102}
andrew@webrtc.orgf3930e92013-09-18 22:37:32 +00003103
wu@webrtc.org93fd25c2014-04-24 20:33:08 +00003104int Channel::SetReceiveAudioLevelIndicationStatus(bool enable,
3105 unsigned char id) {
3106 rtp_header_parser_->DeregisterRtpHeaderExtension(
3107 kRtpExtensionAudioLevel);
3108 if (enable && !rtp_header_parser_->RegisterRtpHeaderExtension(
3109 kRtpExtensionAudioLevel, id)) {
3110 return -1;
3111 }
3112 return 0;
3113}
3114
wu@webrtc.orgebdb0e32014-03-06 23:49:08 +00003115int Channel::SetSendAbsoluteSenderTimeStatus(bool enable, unsigned char id) {
3116 return SetSendRtpHeaderExtension(enable, kRtpExtensionAbsoluteSendTime, id);
3117}
3118
3119int Channel::SetReceiveAbsoluteSenderTimeStatus(bool enable, unsigned char id) {
3120 rtp_header_parser_->DeregisterRtpHeaderExtension(
3121 kRtpExtensionAbsoluteSendTime);
solenberg@webrtc.orgb1f50102014-03-24 10:38:25 +00003122 if (enable && !rtp_header_parser_->RegisterRtpHeaderExtension(
3123 kRtpExtensionAbsoluteSendTime, id)) {
3124 return -1;
wu@webrtc.orgebdb0e32014-03-06 23:49:08 +00003125 }
3126 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +00003127}
3128
3129int
3130Channel::SetRTCPStatus(bool enable)
3131{
3132 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId,_channelId),
3133 "Channel::SetRTCPStatus()");
pwestin@webrtc.org2853dde2012-05-11 11:08:54 +00003134 if (_rtpRtcpModule->SetRTCPStatus(enable ?
niklase@google.com470e71d2011-07-07 08:21:25 +00003135 kRtcpCompound : kRtcpOff) != 0)
3136 {
3137 _engineStatisticsPtr->SetLastError(
3138 VE_RTP_RTCP_MODULE_ERROR, kTraceError,
3139 "SetRTCPStatus() failed to set RTCP status");
3140 return -1;
3141 }
3142 return 0;
3143}
3144
3145int
3146Channel::GetRTCPStatus(bool& enabled)
3147{
pwestin@webrtc.org2853dde2012-05-11 11:08:54 +00003148 RTCPMethod method = _rtpRtcpModule->RTCP();
niklase@google.com470e71d2011-07-07 08:21:25 +00003149 enabled = (method != kRtcpOff);
3150 WEBRTC_TRACE(kTraceStateInfo, kTraceVoice,
3151 VoEId(_instanceId,_channelId),
3152 "GetRTCPStatus() => enabled=%d", enabled);
3153 return 0;
3154}
3155
3156int
3157Channel::SetRTCP_CNAME(const char cName[256])
3158{
3159 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId, _channelId),
3160 "Channel::SetRTCP_CNAME()");
pwestin@webrtc.org2853dde2012-05-11 11:08:54 +00003161 if (_rtpRtcpModule->SetCNAME(cName) != 0)
niklase@google.com470e71d2011-07-07 08:21:25 +00003162 {
3163 _engineStatisticsPtr->SetLastError(
3164 VE_RTP_RTCP_MODULE_ERROR, kTraceError,
3165 "SetRTCP_CNAME() failed to set RTCP CNAME");
3166 return -1;
3167 }
3168 return 0;
3169}
3170
3171int
3172Channel::GetRTCP_CNAME(char cName[256])
3173{
pwestin@webrtc.org2853dde2012-05-11 11:08:54 +00003174 if (_rtpRtcpModule->CNAME(cName) != 0)
niklase@google.com470e71d2011-07-07 08:21:25 +00003175 {
3176 _engineStatisticsPtr->SetLastError(
3177 VE_RTP_RTCP_MODULE_ERROR, kTraceError,
3178 "GetRTCP_CNAME() failed to retrieve RTCP CNAME");
3179 return -1;
3180 }
3181 WEBRTC_TRACE(kTraceStateInfo, kTraceVoice,
3182 VoEId(_instanceId, _channelId),
3183 "GetRTCP_CNAME() => cName=%s", cName);
3184 return 0;
3185}
3186
3187int
3188Channel::GetRemoteRTCP_CNAME(char cName[256])
3189{
3190 if (cName == NULL)
3191 {
3192 _engineStatisticsPtr->SetLastError(
3193 VE_INVALID_ARGUMENT, kTraceError,
3194 "GetRemoteRTCP_CNAME() invalid CNAME input buffer");
3195 return -1;
3196 }
leozwang@webrtc.org813e4b02012-03-01 18:34:25 +00003197 char cname[RTCP_CNAME_SIZE];
wu@webrtc.org822fbd82013-08-15 23:38:54 +00003198 const uint32_t remoteSSRC = rtp_receiver_->SSRC();
pwestin@webrtc.org2853dde2012-05-11 11:08:54 +00003199 if (_rtpRtcpModule->RemoteCNAME(remoteSSRC, cname) != 0)
niklase@google.com470e71d2011-07-07 08:21:25 +00003200 {
3201 _engineStatisticsPtr->SetLastError(
3202 VE_CANNOT_RETRIEVE_CNAME, kTraceError,
3203 "GetRemoteRTCP_CNAME() failed to retrieve remote RTCP CNAME");
3204 return -1;
3205 }
3206 strcpy(cName, cname);
3207 WEBRTC_TRACE(kTraceStateInfo, kTraceVoice,
3208 VoEId(_instanceId, _channelId),
3209 "GetRemoteRTCP_CNAME() => cName=%s", cName);
3210 return 0;
3211}
3212
3213int
3214Channel::GetRemoteRTCPData(
3215 unsigned int& NTPHigh,
3216 unsigned int& NTPLow,
3217 unsigned int& timestamp,
3218 unsigned int& playoutTimestamp,
3219 unsigned int* jitter,
3220 unsigned short* fractionLost)
3221{
3222 // --- Information from sender info in received Sender Reports
3223
3224 RTCPSenderInfo senderInfo;
pwestin@webrtc.org2853dde2012-05-11 11:08:54 +00003225 if (_rtpRtcpModule->RemoteRTCPStat(&senderInfo) != 0)
niklase@google.com470e71d2011-07-07 08:21:25 +00003226 {
3227 _engineStatisticsPtr->SetLastError(
3228 VE_RTP_RTCP_MODULE_ERROR, kTraceError,
wu@webrtc.orgfcd12b32011-09-15 20:49:50 +00003229 "GetRemoteRTCPData() failed to retrieve sender info for remote "
niklase@google.com470e71d2011-07-07 08:21:25 +00003230 "side");
3231 return -1;
3232 }
3233
3234 // We only utilize 12 out of 20 bytes in the sender info (ignores packet
3235 // and octet count)
3236 NTPHigh = senderInfo.NTPseconds;
3237 NTPLow = senderInfo.NTPfraction;
3238 timestamp = senderInfo.RTPtimeStamp;
3239
3240 WEBRTC_TRACE(kTraceStateInfo, kTraceVoice,
3241 VoEId(_instanceId, _channelId),
3242 "GetRemoteRTCPData() => NTPHigh=%lu, NTPLow=%lu, "
3243 "timestamp=%lu",
3244 NTPHigh, NTPLow, timestamp);
3245
3246 // --- Locally derived information
3247
3248 // This value is updated on each incoming RTCP packet (0 when no packet
3249 // has been received)
pwestin@webrtc.org1de01352013-04-11 20:23:35 +00003250 playoutTimestamp = playout_timestamp_rtcp_;
niklase@google.com470e71d2011-07-07 08:21:25 +00003251
3252 WEBRTC_TRACE(kTraceStateInfo, kTraceVoice,
3253 VoEId(_instanceId, _channelId),
3254 "GetRemoteRTCPData() => playoutTimestamp=%lu",
pwestin@webrtc.org1de01352013-04-11 20:23:35 +00003255 playout_timestamp_rtcp_);
niklase@google.com470e71d2011-07-07 08:21:25 +00003256
3257 if (NULL != jitter || NULL != fractionLost)
3258 {
perkj@webrtc.orgce5990c2012-01-11 13:00:08 +00003259 // Get all RTCP receiver report blocks that have been received on this
3260 // channel. If we receive RTP packets from a remote source we know the
3261 // remote SSRC and use the report block from him.
3262 // Otherwise use the first report block.
3263 std::vector<RTCPReportBlock> remote_stats;
pwestin@webrtc.org2853dde2012-05-11 11:08:54 +00003264 if (_rtpRtcpModule->RemoteRTCPStat(&remote_stats) != 0 ||
perkj@webrtc.orgce5990c2012-01-11 13:00:08 +00003265 remote_stats.empty()) {
3266 WEBRTC_TRACE(kTraceWarning, kTraceVoice,
3267 VoEId(_instanceId, _channelId),
3268 "GetRemoteRTCPData() failed to measure statistics due"
3269 " to lack of received RTP and/or RTCP packets");
3270 return -1;
niklase@google.com470e71d2011-07-07 08:21:25 +00003271 }
perkj@webrtc.orgce5990c2012-01-11 13:00:08 +00003272
wu@webrtc.org822fbd82013-08-15 23:38:54 +00003273 uint32_t remoteSSRC = rtp_receiver_->SSRC();
perkj@webrtc.orgce5990c2012-01-11 13:00:08 +00003274 std::vector<RTCPReportBlock>::const_iterator it = remote_stats.begin();
3275 for (; it != remote_stats.end(); ++it) {
3276 if (it->remoteSSRC == remoteSSRC)
3277 break;
niklase@google.com470e71d2011-07-07 08:21:25 +00003278 }
perkj@webrtc.orgce5990c2012-01-11 13:00:08 +00003279
3280 if (it == remote_stats.end()) {
3281 // If we have not received any RTCP packets from this SSRC it probably
3282 // means that we have not received any RTP packets.
3283 // Use the first received report block instead.
3284 it = remote_stats.begin();
3285 remoteSSRC = it->remoteSSRC;
niklase@google.com470e71d2011-07-07 08:21:25 +00003286 }
perkj@webrtc.orgce5990c2012-01-11 13:00:08 +00003287
xians@webrtc.org79af7342012-01-31 12:22:14 +00003288 if (jitter) {
3289 *jitter = it->jitter;
3290 WEBRTC_TRACE(kTraceStateInfo, kTraceVoice,
3291 VoEId(_instanceId, _channelId),
3292 "GetRemoteRTCPData() => jitter = %lu", *jitter);
3293 }
perkj@webrtc.orgce5990c2012-01-11 13:00:08 +00003294
xians@webrtc.org79af7342012-01-31 12:22:14 +00003295 if (fractionLost) {
3296 *fractionLost = it->fractionLost;
3297 WEBRTC_TRACE(kTraceStateInfo, kTraceVoice,
3298 VoEId(_instanceId, _channelId),
3299 "GetRemoteRTCPData() => fractionLost = %lu",
3300 *fractionLost);
3301 }
niklase@google.com470e71d2011-07-07 08:21:25 +00003302 }
3303 return 0;
3304}
3305
3306int
pbos@webrtc.org92135212013-05-14 08:31:39 +00003307Channel::SendApplicationDefinedRTCPPacket(unsigned char subType,
niklase@google.com470e71d2011-07-07 08:21:25 +00003308 unsigned int name,
3309 const char* data,
3310 unsigned short dataLengthInBytes)
3311{
3312 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId, _channelId),
3313 "Channel::SendApplicationDefinedRTCPPacket()");
henrika@webrtc.org944cbeb2014-03-18 10:32:33 +00003314 if (!channel_state_.Get().sending)
niklase@google.com470e71d2011-07-07 08:21:25 +00003315 {
3316 _engineStatisticsPtr->SetLastError(
3317 VE_NOT_SENDING, kTraceError,
3318 "SendApplicationDefinedRTCPPacket() not sending");
3319 return -1;
3320 }
3321 if (NULL == data)
3322 {
3323 _engineStatisticsPtr->SetLastError(
3324 VE_INVALID_ARGUMENT, kTraceError,
3325 "SendApplicationDefinedRTCPPacket() invalid data value");
3326 return -1;
3327 }
3328 if (dataLengthInBytes % 4 != 0)
3329 {
3330 _engineStatisticsPtr->SetLastError(
3331 VE_INVALID_ARGUMENT, kTraceError,
3332 "SendApplicationDefinedRTCPPacket() invalid length value");
3333 return -1;
3334 }
pwestin@webrtc.org2853dde2012-05-11 11:08:54 +00003335 RTCPMethod status = _rtpRtcpModule->RTCP();
niklase@google.com470e71d2011-07-07 08:21:25 +00003336 if (status == kRtcpOff)
3337 {
3338 _engineStatisticsPtr->SetLastError(
3339 VE_RTCP_ERROR, kTraceError,
3340 "SendApplicationDefinedRTCPPacket() RTCP is disabled");
3341 return -1;
3342 }
3343
3344 // Create and schedule the RTCP APP packet for transmission
pwestin@webrtc.org2853dde2012-05-11 11:08:54 +00003345 if (_rtpRtcpModule->SetRTCPApplicationSpecificData(
niklase@google.com470e71d2011-07-07 08:21:25 +00003346 subType,
3347 name,
3348 (const unsigned char*) data,
3349 dataLengthInBytes) != 0)
3350 {
3351 _engineStatisticsPtr->SetLastError(
3352 VE_SEND_ERROR, kTraceError,
3353 "SendApplicationDefinedRTCPPacket() failed to send RTCP packet");
3354 return -1;
3355 }
3356 return 0;
3357}
3358
3359int
3360Channel::GetRTPStatistics(
3361 unsigned int& averageJitterMs,
3362 unsigned int& maxJitterMs,
3363 unsigned int& discardedPackets)
3364{
niklase@google.com470e71d2011-07-07 08:21:25 +00003365 // The jitter statistics is updated for each received RTP packet and is
3366 // based on received packets.
sprang@webrtc.org54ae4ff2013-12-19 13:26:02 +00003367 if (_rtpRtcpModule->RTCP() == kRtcpOff) {
3368 // If RTCP is off, there is no timed thread in the RTCP module regularly
3369 // generating new stats, trigger the update manually here instead.
3370 StreamStatistician* statistician =
3371 rtp_receive_statistics_->GetStatistician(rtp_receiver_->SSRC());
3372 if (statistician) {
3373 // Don't use returned statistics, use data from proxy instead so that
3374 // max jitter can be fetched atomically.
3375 RtcpStatistics s;
3376 statistician->GetStatistics(&s, true);
3377 }
niklase@google.com470e71d2011-07-07 08:21:25 +00003378 }
3379
sprang@webrtc.org54ae4ff2013-12-19 13:26:02 +00003380 ChannelStatistics stats = statistics_proxy_->GetStats();
andrew@webrtc.orgeb524d92013-09-23 23:02:24 +00003381 const int32_t playoutFrequency = audio_coding_->PlayoutFrequency();
sprang@webrtc.org54ae4ff2013-12-19 13:26:02 +00003382 if (playoutFrequency > 0) {
3383 // Scale RTP statistics given the current playout frequency
3384 maxJitterMs = stats.max_jitter / (playoutFrequency / 1000);
3385 averageJitterMs = stats.rtcp.jitter / (playoutFrequency / 1000);
niklase@google.com470e71d2011-07-07 08:21:25 +00003386 }
3387
3388 discardedPackets = _numberOfDiscardedPackets;
3389
3390 WEBRTC_TRACE(kTraceStateInfo, kTraceVoice,
3391 VoEId(_instanceId, _channelId),
3392 "GetRTPStatistics() => averageJitterMs = %lu, maxJitterMs = %lu,"
wu@webrtc.orgfcd12b32011-09-15 20:49:50 +00003393 " discardedPackets = %lu)",
niklase@google.com470e71d2011-07-07 08:21:25 +00003394 averageJitterMs, maxJitterMs, discardedPackets);
3395 return 0;
3396}
3397
henrika@webrtc.org8a2fc882012-08-22 08:53:55 +00003398int Channel::GetRemoteRTCPReportBlocks(
3399 std::vector<ReportBlock>* report_blocks) {
3400 if (report_blocks == NULL) {
3401 _engineStatisticsPtr->SetLastError(VE_INVALID_ARGUMENT, kTraceError,
3402 "GetRemoteRTCPReportBlock()s invalid report_blocks.");
3403 return -1;
3404 }
3405
3406 // Get the report blocks from the latest received RTCP Sender or Receiver
3407 // Report. Each element in the vector contains the sender's SSRC and a
3408 // report block according to RFC 3550.
3409 std::vector<RTCPReportBlock> rtcp_report_blocks;
3410 if (_rtpRtcpModule->RemoteRTCPStat(&rtcp_report_blocks) != 0) {
3411 _engineStatisticsPtr->SetLastError(VE_RTP_RTCP_MODULE_ERROR, kTraceError,
3412 "GetRemoteRTCPReportBlocks() failed to read RTCP SR/RR report block.");
3413 return -1;
3414 }
3415
3416 if (rtcp_report_blocks.empty())
3417 return 0;
3418
3419 std::vector<RTCPReportBlock>::const_iterator it = rtcp_report_blocks.begin();
3420 for (; it != rtcp_report_blocks.end(); ++it) {
3421 ReportBlock report_block;
3422 report_block.sender_SSRC = it->remoteSSRC;
3423 report_block.source_SSRC = it->sourceSSRC;
3424 report_block.fraction_lost = it->fractionLost;
3425 report_block.cumulative_num_packets_lost = it->cumulativeLost;
3426 report_block.extended_highest_sequence_number = it->extendedHighSeqNum;
3427 report_block.interarrival_jitter = it->jitter;
3428 report_block.last_SR_timestamp = it->lastSR;
3429 report_block.delay_since_last_SR = it->delaySinceLastSR;
3430 report_blocks->push_back(report_block);
3431 }
3432 return 0;
3433}
3434
niklase@google.com470e71d2011-07-07 08:21:25 +00003435int
3436Channel::GetRTPStatistics(CallStatistics& stats)
3437{
wu@webrtc.orgcb711f72014-05-19 17:39:11 +00003438 // --- RtcpStatistics
niklase@google.com470e71d2011-07-07 08:21:25 +00003439
3440 // The jitter statistics is updated for each received RTP packet and is
3441 // based on received packets.
sprang@webrtc.org54ae4ff2013-12-19 13:26:02 +00003442 RtcpStatistics statistics;
stefan@webrtc.org286fe0b2013-08-21 20:58:21 +00003443 StreamStatistician* statistician =
3444 rtp_receive_statistics_->GetStatistician(rtp_receiver_->SSRC());
3445 if (!statistician || !statistician->GetStatistics(
wu@webrtc.org822fbd82013-08-15 23:38:54 +00003446 &statistics, _rtpRtcpModule->RTCP() == kRtcpOff)) {
3447 _engineStatisticsPtr->SetLastError(
3448 VE_CANNOT_RETRIEVE_RTP_STAT, kTraceWarning,
3449 "GetRTPStatistics() failed to read RTP statistics from the "
3450 "RTP/RTCP module");
niklase@google.com470e71d2011-07-07 08:21:25 +00003451 }
3452
wu@webrtc.org822fbd82013-08-15 23:38:54 +00003453 stats.fractionLost = statistics.fraction_lost;
3454 stats.cumulativeLost = statistics.cumulative_lost;
3455 stats.extendedMax = statistics.extended_max_sequence_number;
3456 stats.jitterSamples = statistics.jitter;
niklase@google.com470e71d2011-07-07 08:21:25 +00003457
3458 WEBRTC_TRACE(kTraceStateInfo, kTraceVoice,
3459 VoEId(_instanceId, _channelId),
3460 "GetRTPStatistics() => fractionLost=%lu, cumulativeLost=%lu,"
wu@webrtc.orgfcd12b32011-09-15 20:49:50 +00003461 " extendedMax=%lu, jitterSamples=%li)",
niklase@google.com470e71d2011-07-07 08:21:25 +00003462 stats.fractionLost, stats.cumulativeLost, stats.extendedMax,
3463 stats.jitterSamples);
3464
wu@webrtc.orgcb711f72014-05-19 17:39:11 +00003465 // --- RTT
niklase@google.com470e71d2011-07-07 08:21:25 +00003466
pbos@webrtc.org6141e132013-04-09 10:09:10 +00003467 uint16_t RTT(0);
pwestin@webrtc.org2853dde2012-05-11 11:08:54 +00003468 RTCPMethod method = _rtpRtcpModule->RTCP();
niklase@google.com470e71d2011-07-07 08:21:25 +00003469 if (method == kRtcpOff)
3470 {
3471 WEBRTC_TRACE(kTraceWarning, kTraceVoice,
3472 VoEId(_instanceId, _channelId),
wu@webrtc.orgfcd12b32011-09-15 20:49:50 +00003473 "GetRTPStatistics() RTCP is disabled => valid RTT "
niklase@google.com470e71d2011-07-07 08:21:25 +00003474 "measurements cannot be retrieved");
3475 } else
3476 {
3477 // The remote SSRC will be zero if no RTP packet has been received.
wu@webrtc.org822fbd82013-08-15 23:38:54 +00003478 uint32_t remoteSSRC = rtp_receiver_->SSRC();
niklase@google.com470e71d2011-07-07 08:21:25 +00003479 if (remoteSSRC > 0)
3480 {
pbos@webrtc.org6141e132013-04-09 10:09:10 +00003481 uint16_t avgRTT(0);
3482 uint16_t maxRTT(0);
3483 uint16_t minRTT(0);
niklase@google.com470e71d2011-07-07 08:21:25 +00003484
pwestin@webrtc.org2853dde2012-05-11 11:08:54 +00003485 if (_rtpRtcpModule->RTT(remoteSSRC, &RTT, &avgRTT, &minRTT, &maxRTT)
niklase@google.com470e71d2011-07-07 08:21:25 +00003486 != 0)
3487 {
3488 WEBRTC_TRACE(kTraceWarning, kTraceVoice,
3489 VoEId(_instanceId, _channelId),
wu@webrtc.orgfcd12b32011-09-15 20:49:50 +00003490 "GetRTPStatistics() failed to retrieve RTT from "
niklase@google.com470e71d2011-07-07 08:21:25 +00003491 "the RTP/RTCP module");
3492 }
3493 } else
3494 {
3495 WEBRTC_TRACE(kTraceWarning, kTraceVoice,
3496 VoEId(_instanceId, _channelId),
wu@webrtc.orgfcd12b32011-09-15 20:49:50 +00003497 "GetRTPStatistics() failed to measure RTT since no "
niklase@google.com470e71d2011-07-07 08:21:25 +00003498 "RTP packets have been received yet");
3499 }
3500 }
3501
3502 stats.rttMs = static_cast<int> (RTT);
3503
3504 WEBRTC_TRACE(kTraceStateInfo, kTraceVoice,
3505 VoEId(_instanceId, _channelId),
3506 "GetRTPStatistics() => rttMs=%d", stats.rttMs);
3507
wu@webrtc.orgcb711f72014-05-19 17:39:11 +00003508 // --- Data counters
niklase@google.com470e71d2011-07-07 08:21:25 +00003509
pbos@webrtc.org6141e132013-04-09 10:09:10 +00003510 uint32_t bytesSent(0);
3511 uint32_t packetsSent(0);
3512 uint32_t bytesReceived(0);
3513 uint32_t packetsReceived(0);
niklase@google.com470e71d2011-07-07 08:21:25 +00003514
stefan@webrtc.org286fe0b2013-08-21 20:58:21 +00003515 if (statistician) {
3516 statistician->GetDataCounters(&bytesReceived, &packetsReceived);
3517 }
wu@webrtc.org822fbd82013-08-15 23:38:54 +00003518
pwestin@webrtc.org2853dde2012-05-11 11:08:54 +00003519 if (_rtpRtcpModule->DataCountersRTP(&bytesSent,
wu@webrtc.org822fbd82013-08-15 23:38:54 +00003520 &packetsSent) != 0)
niklase@google.com470e71d2011-07-07 08:21:25 +00003521 {
3522 WEBRTC_TRACE(kTraceWarning, kTraceVoice,
3523 VoEId(_instanceId, _channelId),
3524 "GetRTPStatistics() failed to retrieve RTP datacounters =>"
wu@webrtc.orgfcd12b32011-09-15 20:49:50 +00003525 " output will not be complete");
niklase@google.com470e71d2011-07-07 08:21:25 +00003526 }
3527
3528 stats.bytesSent = bytesSent;
3529 stats.packetsSent = packetsSent;
3530 stats.bytesReceived = bytesReceived;
3531 stats.packetsReceived = packetsReceived;
3532
3533 WEBRTC_TRACE(kTraceStateInfo, kTraceVoice,
3534 VoEId(_instanceId, _channelId),
3535 "GetRTPStatistics() => bytesSent=%d, packetsSent=%d,"
wu@webrtc.orgfcd12b32011-09-15 20:49:50 +00003536 " bytesReceived=%d, packetsReceived=%d)",
niklase@google.com470e71d2011-07-07 08:21:25 +00003537 stats.bytesSent, stats.packetsSent, stats.bytesReceived,
3538 stats.packetsReceived);
3539
wu@webrtc.orgcb711f72014-05-19 17:39:11 +00003540 // --- Timestamps
3541 {
3542 CriticalSectionScoped lock(ts_stats_lock_.get());
3543 stats.capture_start_ntp_time_ms_ = capture_start_ntp_time_ms_;
3544 }
niklase@google.com470e71d2011-07-07 08:21:25 +00003545 return 0;
3546}
3547
minyue@webrtc.orgc1a40a72014-05-28 09:52:06 +00003548int Channel::SetREDStatus(bool enable, int redPayloadtype) {
turaj@webrtc.org42259e72012-12-11 02:15:12 +00003549 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId, _channelId),
minyue@webrtc.orgc1a40a72014-05-28 09:52:06 +00003550 "Channel::SetREDStatus()");
niklase@google.com470e71d2011-07-07 08:21:25 +00003551
turaj@webrtc.org8c8ad852013-01-31 18:20:17 +00003552 if (enable) {
3553 if (redPayloadtype < 0 || redPayloadtype > 127) {
3554 _engineStatisticsPtr->SetLastError(
3555 VE_PLTYPE_ERROR, kTraceError,
minyue@webrtc.orgc1a40a72014-05-28 09:52:06 +00003556 "SetREDStatus() invalid RED payload type");
turaj@webrtc.org8c8ad852013-01-31 18:20:17 +00003557 return -1;
3558 }
3559
3560 if (SetRedPayloadType(redPayloadtype) < 0) {
3561 _engineStatisticsPtr->SetLastError(
3562 VE_CODEC_ERROR, kTraceError,
3563 "SetSecondarySendCodec() Failed to register RED ACM");
3564 return -1;
3565 }
turaj@webrtc.org42259e72012-12-11 02:15:12 +00003566 }
niklase@google.com470e71d2011-07-07 08:21:25 +00003567
minyue@webrtc.orgaa5ea1c2014-05-23 15:16:51 +00003568 if (audio_coding_->SetREDStatus(enable) != 0) {
turaj@webrtc.org42259e72012-12-11 02:15:12 +00003569 _engineStatisticsPtr->SetLastError(
3570 VE_AUDIO_CODING_MODULE_ERROR, kTraceError,
minyue@webrtc.orgaa5ea1c2014-05-23 15:16:51 +00003571 "SetREDStatus() failed to set RED state in the ACM");
turaj@webrtc.org42259e72012-12-11 02:15:12 +00003572 return -1;
3573 }
3574 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +00003575}
3576
3577int
minyue@webrtc.orgc1a40a72014-05-28 09:52:06 +00003578Channel::GetREDStatus(bool& enabled, int& redPayloadtype)
niklase@google.com470e71d2011-07-07 08:21:25 +00003579{
minyue@webrtc.orgaa5ea1c2014-05-23 15:16:51 +00003580 enabled = audio_coding_->REDStatus();
niklase@google.com470e71d2011-07-07 08:21:25 +00003581 if (enabled)
3582 {
pbos@webrtc.org6141e132013-04-09 10:09:10 +00003583 int8_t payloadType(0);
pwestin@webrtc.org2853dde2012-05-11 11:08:54 +00003584 if (_rtpRtcpModule->SendREDPayloadType(payloadType) != 0)
niklase@google.com470e71d2011-07-07 08:21:25 +00003585 {
3586 _engineStatisticsPtr->SetLastError(
3587 VE_RTP_RTCP_MODULE_ERROR, kTraceError,
minyue@webrtc.orgc1a40a72014-05-28 09:52:06 +00003588 "GetREDStatus() failed to retrieve RED PT from RTP/RTCP "
niklase@google.com470e71d2011-07-07 08:21:25 +00003589 "module");
3590 return -1;
3591 }
3592 WEBRTC_TRACE(kTraceStateInfo, kTraceVoice,
3593 VoEId(_instanceId, _channelId),
minyue@webrtc.orgc1a40a72014-05-28 09:52:06 +00003594 "GetREDStatus() => enabled=%d, redPayloadtype=%d",
niklase@google.com470e71d2011-07-07 08:21:25 +00003595 enabled, redPayloadtype);
3596 return 0;
3597 }
3598 WEBRTC_TRACE(kTraceStateInfo, kTraceVoice,
3599 VoEId(_instanceId, _channelId),
minyue@webrtc.orgc1a40a72014-05-28 09:52:06 +00003600 "GetREDStatus() => enabled=%d", enabled);
niklase@google.com470e71d2011-07-07 08:21:25 +00003601 return 0;
3602}
3603
minyue@webrtc.orgc1a40a72014-05-28 09:52:06 +00003604int Channel::SetCodecFECStatus(bool enable) {
3605 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId, _channelId),
3606 "Channel::SetCodecFECStatus()");
3607
3608 if (audio_coding_->SetCodecFEC(enable) != 0) {
3609 _engineStatisticsPtr->SetLastError(
3610 VE_AUDIO_CODING_MODULE_ERROR, kTraceError,
3611 "SetCodecFECStatus() failed to set FEC state");
3612 return -1;
3613 }
3614 return 0;
3615}
3616
3617bool Channel::GetCodecFECStatus() {
3618 bool enabled = audio_coding_->CodecFEC();
3619 WEBRTC_TRACE(kTraceStateInfo, kTraceVoice,
3620 VoEId(_instanceId, _channelId),
3621 "GetCodecFECStatus() => enabled=%d", enabled);
3622 return enabled;
3623}
3624
pwestin@webrtc.orgdb249952013-06-05 15:33:20 +00003625void Channel::SetNACKStatus(bool enable, int maxNumberOfPackets) {
3626 // None of these functions can fail.
3627 _rtpRtcpModule->SetStorePacketsStatus(enable, maxNumberOfPackets);
stefan@webrtc.org7bb8f022013-09-06 13:40:11 +00003628 rtp_receive_statistics_->SetMaxReorderingThreshold(maxNumberOfPackets);
3629 rtp_receiver_->SetNACKStatus(enable ? kNackRtcp : kNackOff);
pwestin@webrtc.orgd30859e2013-06-06 21:09:01 +00003630 if (enable)
andrew@webrtc.orgeb524d92013-09-23 23:02:24 +00003631 audio_coding_->EnableNack(maxNumberOfPackets);
pwestin@webrtc.orgd30859e2013-06-06 21:09:01 +00003632 else
andrew@webrtc.orgeb524d92013-09-23 23:02:24 +00003633 audio_coding_->DisableNack();
pwestin@webrtc.orgdb249952013-06-05 15:33:20 +00003634}
3635
pwestin@webrtc.orgd30859e2013-06-06 21:09:01 +00003636// Called when we are missing one or more packets.
3637int Channel::ResendPackets(const uint16_t* sequence_numbers, int length) {
pwestin@webrtc.orgdb249952013-06-05 15:33:20 +00003638 return _rtpRtcpModule->SendNACK(sequence_numbers, length);
3639}
3640
niklase@google.com470e71d2011-07-07 08:21:25 +00003641int
niklase@google.com470e71d2011-07-07 08:21:25 +00003642Channel::StartRTPDump(const char fileNameUTF8[1024],
3643 RTPDirections direction)
3644{
3645 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId, _channelId),
3646 "Channel::StartRTPDump()");
3647 if ((direction != kRtpIncoming) && (direction != kRtpOutgoing))
3648 {
3649 _engineStatisticsPtr->SetLastError(
3650 VE_INVALID_ARGUMENT, kTraceError,
3651 "StartRTPDump() invalid RTP direction");
3652 return -1;
3653 }
3654 RtpDump* rtpDumpPtr = (direction == kRtpIncoming) ?
3655 &_rtpDumpIn : &_rtpDumpOut;
3656 if (rtpDumpPtr == NULL)
3657 {
3658 assert(false);
3659 return -1;
3660 }
3661 if (rtpDumpPtr->IsActive())
3662 {
3663 rtpDumpPtr->Stop();
3664 }
3665 if (rtpDumpPtr->Start(fileNameUTF8) != 0)
3666 {
3667 _engineStatisticsPtr->SetLastError(
3668 VE_BAD_FILE, kTraceError,
3669 "StartRTPDump() failed to create file");
3670 return -1;
3671 }
3672 return 0;
3673}
3674
3675int
3676Channel::StopRTPDump(RTPDirections direction)
3677{
3678 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId, _channelId),
3679 "Channel::StopRTPDump()");
3680 if ((direction != kRtpIncoming) && (direction != kRtpOutgoing))
3681 {
3682 _engineStatisticsPtr->SetLastError(
3683 VE_INVALID_ARGUMENT, kTraceError,
3684 "StopRTPDump() invalid RTP direction");
3685 return -1;
3686 }
3687 RtpDump* rtpDumpPtr = (direction == kRtpIncoming) ?
3688 &_rtpDumpIn : &_rtpDumpOut;
3689 if (rtpDumpPtr == NULL)
3690 {
3691 assert(false);
3692 return -1;
3693 }
3694 if (!rtpDumpPtr->IsActive())
3695 {
3696 return 0;
3697 }
3698 return rtpDumpPtr->Stop();
3699}
3700
3701bool
3702Channel::RTPDumpIsActive(RTPDirections direction)
3703{
3704 if ((direction != kRtpIncoming) &&
3705 (direction != kRtpOutgoing))
3706 {
3707 _engineStatisticsPtr->SetLastError(
3708 VE_INVALID_ARGUMENT, kTraceError,
3709 "RTPDumpIsActive() invalid RTP direction");
3710 return false;
3711 }
3712 RtpDump* rtpDumpPtr = (direction == kRtpIncoming) ?
3713 &_rtpDumpIn : &_rtpDumpOut;
3714 return rtpDumpPtr->IsActive();
3715}
3716
solenberg@webrtc.orgb1f50102014-03-24 10:38:25 +00003717void Channel::SetVideoEngineBWETarget(ViENetwork* vie_network,
3718 int video_channel) {
3719 CriticalSectionScoped cs(&_callbackCritSect);
3720 if (vie_network_) {
3721 vie_network_->Release();
3722 vie_network_ = NULL;
3723 }
3724 video_channel_ = -1;
3725
3726 if (vie_network != NULL && video_channel != -1) {
3727 vie_network_ = vie_network;
3728 video_channel_ = video_channel;
3729 }
3730}
3731
pbos@webrtc.org6141e132013-04-09 10:09:10 +00003732uint32_t
andrew@webrtc.org755b04a2011-11-15 16:57:56 +00003733Channel::Demultiplex(const AudioFrame& audioFrame)
niklase@google.com470e71d2011-07-07 08:21:25 +00003734{
3735 WEBRTC_TRACE(kTraceStream, kTraceVoice, VoEId(_instanceId,_channelId),
andrew@webrtc.org755b04a2011-11-15 16:57:56 +00003736 "Channel::Demultiplex()");
andrew@webrtc.orgae1a58b2013-01-22 04:44:30 +00003737 _audioFrame.CopyFrom(audioFrame);
andrew@webrtc.org63a50982012-05-02 23:56:37 +00003738 _audioFrame.id_ = _channelId;
niklase@google.com470e71d2011-07-07 08:21:25 +00003739 return 0;
3740}
3741
xians@webrtc.org2f84afa2013-07-31 16:23:37 +00003742void Channel::Demultiplex(const int16_t* audio_data,
xians@webrtc.org8fff1f02013-07-31 16:27:42 +00003743 int sample_rate,
xians@webrtc.org2f84afa2013-07-31 16:23:37 +00003744 int number_of_frames,
xians@webrtc.org8fff1f02013-07-31 16:27:42 +00003745 int number_of_channels) {
xians@webrtc.org2f84afa2013-07-31 16:23:37 +00003746 CodecInst codec;
3747 GetSendCodec(codec);
xians@webrtc.org2f84afa2013-07-31 16:23:37 +00003748
andrew@webrtc.org40ee3d02014-04-03 21:56:01 +00003749 if (!mono_recording_audio_.get()) {
3750 // Temporary space for DownConvertToCodecFormat.
3751 mono_recording_audio_.reset(new int16_t[kMaxMonoDataSizeSamples]);
xians@webrtc.org2f84afa2013-07-31 16:23:37 +00003752 }
andrew@webrtc.org40ee3d02014-04-03 21:56:01 +00003753 DownConvertToCodecFormat(audio_data,
3754 number_of_frames,
3755 number_of_channels,
3756 sample_rate,
3757 codec.channels,
3758 codec.plfreq,
3759 mono_recording_audio_.get(),
3760 &input_resampler_,
3761 &_audioFrame);
xians@webrtc.org2f84afa2013-07-31 16:23:37 +00003762}
3763
pbos@webrtc.org6141e132013-04-09 10:09:10 +00003764uint32_t
xians@google.com0b0665a2011-08-08 08:18:44 +00003765Channel::PrepareEncodeAndSend(int mixingFrequency)
niklase@google.com470e71d2011-07-07 08:21:25 +00003766{
3767 WEBRTC_TRACE(kTraceStream, kTraceVoice, VoEId(_instanceId,_channelId),
3768 "Channel::PrepareEncodeAndSend()");
3769
andrew@webrtc.org63a50982012-05-02 23:56:37 +00003770 if (_audioFrame.samples_per_channel_ == 0)
niklase@google.com470e71d2011-07-07 08:21:25 +00003771 {
3772 WEBRTC_TRACE(kTraceWarning, kTraceVoice, VoEId(_instanceId,_channelId),
3773 "Channel::PrepareEncodeAndSend() invalid audio frame");
3774 return -1;
3775 }
3776
henrika@webrtc.org944cbeb2014-03-18 10:32:33 +00003777 if (channel_state_.Get().input_file_playing)
niklase@google.com470e71d2011-07-07 08:21:25 +00003778 {
3779 MixOrReplaceAudioWithFile(mixingFrequency);
3780 }
3781
andrew@webrtc.org21299d42014-05-14 19:00:59 +00003782 bool is_muted = Mute(); // Cache locally as Mute() takes a lock.
3783 if (is_muted) {
3784 AudioFrameOperations::Mute(_audioFrame);
niklase@google.com470e71d2011-07-07 08:21:25 +00003785 }
3786
henrika@webrtc.org944cbeb2014-03-18 10:32:33 +00003787 if (channel_state_.Get().input_external_media)
niklase@google.com470e71d2011-07-07 08:21:25 +00003788 {
mflodman@webrtc.org9a065d12012-03-07 08:12:21 +00003789 CriticalSectionScoped cs(&_callbackCritSect);
andrew@webrtc.org63a50982012-05-02 23:56:37 +00003790 const bool isStereo = (_audioFrame.num_channels_ == 2);
niklase@google.com470e71d2011-07-07 08:21:25 +00003791 if (_inputExternalMediaCallbackPtr)
3792 {
3793 _inputExternalMediaCallbackPtr->Process(
3794 _channelId,
3795 kRecordingPerChannel,
pbos@webrtc.org6141e132013-04-09 10:09:10 +00003796 (int16_t*)_audioFrame.data_,
andrew@webrtc.org63a50982012-05-02 23:56:37 +00003797 _audioFrame.samples_per_channel_,
3798 _audioFrame.sample_rate_hz_,
niklase@google.com470e71d2011-07-07 08:21:25 +00003799 isStereo);
3800 }
3801 }
3802
3803 InsertInbandDtmfTone();
3804
andrew@webrtc.org60730cf2014-01-07 17:45:09 +00003805 if (_includeAudioLevelIndication) {
andrew@webrtc.org382c0c22014-05-05 18:22:21 +00003806 int length = _audioFrame.samples_per_channel_ * _audioFrame.num_channels_;
andrew@webrtc.org21299d42014-05-14 19:00:59 +00003807 if (is_muted) {
3808 rms_level_.ProcessMuted(length);
3809 } else {
3810 rms_level_.Process(_audioFrame.data_, length);
3811 }
andrew@webrtc.org755b04a2011-11-15 16:57:56 +00003812 }
3813
niklase@google.com470e71d2011-07-07 08:21:25 +00003814 return 0;
3815}
3816
pbos@webrtc.org6141e132013-04-09 10:09:10 +00003817uint32_t
niklase@google.com470e71d2011-07-07 08:21:25 +00003818Channel::EncodeAndSend()
3819{
3820 WEBRTC_TRACE(kTraceStream, kTraceVoice, VoEId(_instanceId,_channelId),
3821 "Channel::EncodeAndSend()");
3822
andrew@webrtc.org63a50982012-05-02 23:56:37 +00003823 assert(_audioFrame.num_channels_ <= 2);
3824 if (_audioFrame.samples_per_channel_ == 0)
niklase@google.com470e71d2011-07-07 08:21:25 +00003825 {
3826 WEBRTC_TRACE(kTraceWarning, kTraceVoice, VoEId(_instanceId,_channelId),
3827 "Channel::EncodeAndSend() invalid audio frame");
3828 return -1;
3829 }
3830
andrew@webrtc.org63a50982012-05-02 23:56:37 +00003831 _audioFrame.id_ = _channelId;
niklase@google.com470e71d2011-07-07 08:21:25 +00003832
3833 // --- Add 10ms of raw (PCM) audio data to the encoder @ 32kHz.
3834
3835 // The ACM resamples internally.
andrew@webrtc.org63a50982012-05-02 23:56:37 +00003836 _audioFrame.timestamp_ = _timeStamp;
andrew@webrtc.orgeb524d92013-09-23 23:02:24 +00003837 if (audio_coding_->Add10MsData((AudioFrame&)_audioFrame) != 0)
niklase@google.com470e71d2011-07-07 08:21:25 +00003838 {
3839 WEBRTC_TRACE(kTraceError, kTraceVoice, VoEId(_instanceId,_channelId),
3840 "Channel::EncodeAndSend() ACM encoding failed");
3841 return -1;
3842 }
3843
andrew@webrtc.org63a50982012-05-02 23:56:37 +00003844 _timeStamp += _audioFrame.samples_per_channel_;
niklase@google.com470e71d2011-07-07 08:21:25 +00003845
3846 // --- Encode if complete frame is ready
3847
3848 // This call will trigger AudioPacketizationCallback::SendData if encoding
3849 // is done and payload is ready for packetization and transmission.
andrew@webrtc.orgeb524d92013-09-23 23:02:24 +00003850 return audio_coding_->Process();
niklase@google.com470e71d2011-07-07 08:21:25 +00003851}
3852
3853int Channel::RegisterExternalMediaProcessing(
3854 ProcessingTypes type,
3855 VoEMediaProcess& processObject)
3856{
3857 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId,_channelId),
3858 "Channel::RegisterExternalMediaProcessing()");
3859
mflodman@webrtc.org9a065d12012-03-07 08:12:21 +00003860 CriticalSectionScoped cs(&_callbackCritSect);
niklase@google.com470e71d2011-07-07 08:21:25 +00003861
3862 if (kPlaybackPerChannel == type)
3863 {
3864 if (_outputExternalMediaCallbackPtr)
3865 {
3866 _engineStatisticsPtr->SetLastError(
3867 VE_INVALID_OPERATION, kTraceError,
3868 "Channel::RegisterExternalMediaProcessing() "
3869 "output external media already enabled");
3870 return -1;
3871 }
3872 _outputExternalMediaCallbackPtr = &processObject;
3873 _outputExternalMedia = true;
3874 }
3875 else if (kRecordingPerChannel == type)
3876 {
3877 if (_inputExternalMediaCallbackPtr)
3878 {
3879 _engineStatisticsPtr->SetLastError(
3880 VE_INVALID_OPERATION, kTraceError,
3881 "Channel::RegisterExternalMediaProcessing() "
3882 "output external media already enabled");
3883 return -1;
3884 }
3885 _inputExternalMediaCallbackPtr = &processObject;
henrika@webrtc.org944cbeb2014-03-18 10:32:33 +00003886 channel_state_.SetInputExternalMedia(true);
niklase@google.com470e71d2011-07-07 08:21:25 +00003887 }
3888 return 0;
3889}
3890
3891int Channel::DeRegisterExternalMediaProcessing(ProcessingTypes type)
3892{
3893 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId,_channelId),
3894 "Channel::DeRegisterExternalMediaProcessing()");
3895
mflodman@webrtc.org9a065d12012-03-07 08:12:21 +00003896 CriticalSectionScoped cs(&_callbackCritSect);
niklase@google.com470e71d2011-07-07 08:21:25 +00003897
3898 if (kPlaybackPerChannel == type)
3899 {
3900 if (!_outputExternalMediaCallbackPtr)
3901 {
3902 _engineStatisticsPtr->SetLastError(
3903 VE_INVALID_OPERATION, kTraceWarning,
3904 "Channel::DeRegisterExternalMediaProcessing() "
3905 "output external media already disabled");
3906 return 0;
3907 }
3908 _outputExternalMedia = false;
3909 _outputExternalMediaCallbackPtr = NULL;
3910 }
3911 else if (kRecordingPerChannel == type)
3912 {
3913 if (!_inputExternalMediaCallbackPtr)
3914 {
3915 _engineStatisticsPtr->SetLastError(
3916 VE_INVALID_OPERATION, kTraceWarning,
3917 "Channel::DeRegisterExternalMediaProcessing() "
3918 "input external media already disabled");
3919 return 0;
3920 }
henrika@webrtc.org944cbeb2014-03-18 10:32:33 +00003921 channel_state_.SetInputExternalMedia(false);
niklase@google.com470e71d2011-07-07 08:21:25 +00003922 _inputExternalMediaCallbackPtr = NULL;
3923 }
3924
3925 return 0;
3926}
3927
roosa@google.com1b60ceb2012-12-12 23:00:29 +00003928int Channel::SetExternalMixing(bool enabled) {
3929 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId,_channelId),
3930 "Channel::SetExternalMixing(enabled=%d)", enabled);
3931
henrika@webrtc.org944cbeb2014-03-18 10:32:33 +00003932 if (channel_state_.Get().playing)
roosa@google.com1b60ceb2012-12-12 23:00:29 +00003933 {
3934 _engineStatisticsPtr->SetLastError(
3935 VE_INVALID_OPERATION, kTraceError,
3936 "Channel::SetExternalMixing() "
3937 "external mixing cannot be changed while playing.");
3938 return -1;
3939 }
3940
3941 _externalMixing = enabled;
3942
3943 return 0;
3944}
3945
niklase@google.com470e71d2011-07-07 08:21:25 +00003946int
niklase@google.com470e71d2011-07-07 08:21:25 +00003947Channel::GetNetworkStatistics(NetworkStatistics& stats)
3948{
3949 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId,_channelId),
3950 "Channel::GetNetworkStatistics()");
tina.legrand@webrtc.org7a7a0082013-02-21 10:27:48 +00003951 ACMNetworkStatistics acm_stats;
andrew@webrtc.orgeb524d92013-09-23 23:02:24 +00003952 int return_value = audio_coding_->NetworkStatistics(&acm_stats);
tina.legrand@webrtc.org7a7a0082013-02-21 10:27:48 +00003953 if (return_value >= 0) {
3954 memcpy(&stats, &acm_stats, sizeof(NetworkStatistics));
3955 }
3956 return return_value;
niklase@google.com470e71d2011-07-07 08:21:25 +00003957}
3958
wu@webrtc.org24301a62013-12-13 19:17:43 +00003959void Channel::GetDecodingCallStatistics(AudioDecodingCallStats* stats) const {
3960 audio_coding_->GetDecodingCallStatistics(stats);
3961}
3962
pwestin@webrtc.org1de01352013-04-11 20:23:35 +00003963bool Channel::GetDelayEstimate(int* jitter_buffer_delay_ms,
3964 int* playout_buffer_delay_ms) const {
3965 if (_average_jitter_buffer_delay_us == 0) {
niklase@google.com470e71d2011-07-07 08:21:25 +00003966 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId,_channelId),
pwestin@webrtc.org1de01352013-04-11 20:23:35 +00003967 "Channel::GetDelayEstimate() no valid estimate.");
3968 return false;
3969 }
3970 *jitter_buffer_delay_ms = (_average_jitter_buffer_delay_us + 500) / 1000 +
3971 _recPacketDelayMs;
3972 *playout_buffer_delay_ms = playout_delay_ms_;
3973 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId,_channelId),
3974 "Channel::GetDelayEstimate()");
3975 return true;
niklase@google.com470e71d2011-07-07 08:21:25 +00003976}
3977
turaj@webrtc.org6388c3e2013-02-12 21:42:18 +00003978int Channel::SetInitialPlayoutDelay(int delay_ms)
3979{
3980 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId,_channelId),
3981 "Channel::SetInitialPlayoutDelay()");
3982 if ((delay_ms < kVoiceEngineMinMinPlayoutDelayMs) ||
3983 (delay_ms > kVoiceEngineMaxMinPlayoutDelayMs))
3984 {
3985 _engineStatisticsPtr->SetLastError(
3986 VE_INVALID_ARGUMENT, kTraceError,
3987 "SetInitialPlayoutDelay() invalid min delay");
3988 return -1;
3989 }
andrew@webrtc.orgeb524d92013-09-23 23:02:24 +00003990 if (audio_coding_->SetInitialPlayoutDelay(delay_ms) != 0)
turaj@webrtc.org6388c3e2013-02-12 21:42:18 +00003991 {
3992 _engineStatisticsPtr->SetLastError(
3993 VE_AUDIO_CODING_MODULE_ERROR, kTraceError,
3994 "SetInitialPlayoutDelay() failed to set min playout delay");
3995 return -1;
3996 }
3997 return 0;
3998}
3999
4000
niklase@google.com470e71d2011-07-07 08:21:25 +00004001int
4002Channel::SetMinimumPlayoutDelay(int delayMs)
4003{
4004 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId,_channelId),
4005 "Channel::SetMinimumPlayoutDelay()");
4006 if ((delayMs < kVoiceEngineMinMinPlayoutDelayMs) ||
4007 (delayMs > kVoiceEngineMaxMinPlayoutDelayMs))
4008 {
4009 _engineStatisticsPtr->SetLastError(
4010 VE_INVALID_ARGUMENT, kTraceError,
4011 "SetMinimumPlayoutDelay() invalid min delay");
4012 return -1;
4013 }
andrew@webrtc.orgeb524d92013-09-23 23:02:24 +00004014 if (audio_coding_->SetMinimumPlayoutDelay(delayMs) != 0)
niklase@google.com470e71d2011-07-07 08:21:25 +00004015 {
4016 _engineStatisticsPtr->SetLastError(
4017 VE_AUDIO_CODING_MODULE_ERROR, kTraceError,
4018 "SetMinimumPlayoutDelay() failed to set min playout delay");
4019 return -1;
4020 }
4021 return 0;
4022}
4023
pwestin@webrtc.org1de01352013-04-11 20:23:35 +00004024void Channel::UpdatePlayoutTimestamp(bool rtcp) {
4025 uint32_t playout_timestamp = 0;
4026
andrew@webrtc.orgeb524d92013-09-23 23:02:24 +00004027 if (audio_coding_->PlayoutTimestamp(&playout_timestamp) == -1) {
pwestin@webrtc.org1de01352013-04-11 20:23:35 +00004028 WEBRTC_TRACE(kTraceWarning, kTraceVoice, VoEId(_instanceId,_channelId),
4029 "Channel::UpdatePlayoutTimestamp() failed to read playout"
4030 " timestamp from the ACM");
4031 _engineStatisticsPtr->SetLastError(
4032 VE_CANNOT_RETRIEVE_VALUE, kTraceError,
4033 "UpdatePlayoutTimestamp() failed to retrieve timestamp");
4034 return;
4035 }
4036
4037 uint16_t delay_ms = 0;
4038 if (_audioDeviceModulePtr->PlayoutDelay(&delay_ms) == -1) {
4039 WEBRTC_TRACE(kTraceWarning, kTraceVoice, VoEId(_instanceId,_channelId),
4040 "Channel::UpdatePlayoutTimestamp() failed to read playout"
4041 " delay from the ADM");
4042 _engineStatisticsPtr->SetLastError(
4043 VE_CANNOT_RETRIEVE_VALUE, kTraceError,
4044 "UpdatePlayoutTimestamp() failed to retrieve playout delay");
4045 return;
4046 }
4047
andrew@webrtc.orgeb524d92013-09-23 23:02:24 +00004048 int32_t playout_frequency = audio_coding_->PlayoutFrequency();
pwestin@webrtc.org1de01352013-04-11 20:23:35 +00004049 CodecInst current_recive_codec;
andrew@webrtc.orgeb524d92013-09-23 23:02:24 +00004050 if (audio_coding_->ReceiveCodec(&current_recive_codec) == 0) {
pwestin@webrtc.org1de01352013-04-11 20:23:35 +00004051 if (STR_CASE_CMP("G722", current_recive_codec.plname) == 0) {
4052 playout_frequency = 8000;
4053 } else if (STR_CASE_CMP("opus", current_recive_codec.plname) == 0) {
4054 playout_frequency = 48000;
niklase@google.com470e71d2011-07-07 08:21:25 +00004055 }
pwestin@webrtc.org1de01352013-04-11 20:23:35 +00004056 }
4057
turaj@webrtc.org167b6df2013-12-13 21:05:07 +00004058 jitter_buffer_playout_timestamp_ = playout_timestamp;
4059
pwestin@webrtc.org1de01352013-04-11 20:23:35 +00004060 // Remove the playout delay.
4061 playout_timestamp -= (delay_ms * (playout_frequency / 1000));
4062
4063 WEBRTC_TRACE(kTraceStream, kTraceVoice, VoEId(_instanceId,_channelId),
4064 "Channel::UpdatePlayoutTimestamp() => playoutTimestamp = %lu",
4065 playout_timestamp);
4066
4067 if (rtcp) {
4068 playout_timestamp_rtcp_ = playout_timestamp;
4069 } else {
4070 playout_timestamp_rtp_ = playout_timestamp;
4071 }
4072 playout_delay_ms_ = delay_ms;
4073}
4074
4075int Channel::GetPlayoutTimestamp(unsigned int& timestamp) {
4076 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId,_channelId),
4077 "Channel::GetPlayoutTimestamp()");
4078 if (playout_timestamp_rtp_ == 0) {
4079 _engineStatisticsPtr->SetLastError(
4080 VE_CANNOT_RETRIEVE_VALUE, kTraceError,
4081 "GetPlayoutTimestamp() failed to retrieve timestamp");
4082 return -1;
4083 }
4084 timestamp = playout_timestamp_rtp_;
4085 WEBRTC_TRACE(kTraceStateInfo, kTraceVoice,
4086 VoEId(_instanceId,_channelId),
4087 "GetPlayoutTimestamp() => timestamp=%u", timestamp);
4088 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +00004089}
4090
4091int
4092Channel::SetInitTimestamp(unsigned int timestamp)
4093{
4094 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId,_channelId),
4095 "Channel::SetInitTimestamp()");
henrika@webrtc.org944cbeb2014-03-18 10:32:33 +00004096 if (channel_state_.Get().sending)
niklase@google.com470e71d2011-07-07 08:21:25 +00004097 {
4098 _engineStatisticsPtr->SetLastError(
4099 VE_SENDING, kTraceError, "SetInitTimestamp() already sending");
4100 return -1;
4101 }
pwestin@webrtc.org2853dde2012-05-11 11:08:54 +00004102 if (_rtpRtcpModule->SetStartTimestamp(timestamp) != 0)
niklase@google.com470e71d2011-07-07 08:21:25 +00004103 {
4104 _engineStatisticsPtr->SetLastError(
4105 VE_RTP_RTCP_MODULE_ERROR, kTraceError,
4106 "SetInitTimestamp() failed to set timestamp");
4107 return -1;
4108 }
4109 return 0;
4110}
4111
4112int
4113Channel::SetInitSequenceNumber(short sequenceNumber)
4114{
4115 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId,_channelId),
4116 "Channel::SetInitSequenceNumber()");
henrika@webrtc.org944cbeb2014-03-18 10:32:33 +00004117 if (channel_state_.Get().sending)
niklase@google.com470e71d2011-07-07 08:21:25 +00004118 {
4119 _engineStatisticsPtr->SetLastError(
4120 VE_SENDING, kTraceError,
4121 "SetInitSequenceNumber() already sending");
4122 return -1;
4123 }
pwestin@webrtc.org2853dde2012-05-11 11:08:54 +00004124 if (_rtpRtcpModule->SetSequenceNumber(sequenceNumber) != 0)
niklase@google.com470e71d2011-07-07 08:21:25 +00004125 {
4126 _engineStatisticsPtr->SetLastError(
4127 VE_RTP_RTCP_MODULE_ERROR, kTraceError,
4128 "SetInitSequenceNumber() failed to set sequence number");
4129 return -1;
4130 }
4131 return 0;
4132}
4133
4134int
wu@webrtc.org822fbd82013-08-15 23:38:54 +00004135Channel::GetRtpRtcp(RtpRtcp** rtpRtcpModule, RtpReceiver** rtp_receiver) const
niklase@google.com470e71d2011-07-07 08:21:25 +00004136{
4137 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId,_channelId),
4138 "Channel::GetRtpRtcp()");
wu@webrtc.org822fbd82013-08-15 23:38:54 +00004139 *rtpRtcpModule = _rtpRtcpModule.get();
4140 *rtp_receiver = rtp_receiver_.get();
niklase@google.com470e71d2011-07-07 08:21:25 +00004141 return 0;
4142}
4143
andrew@webrtc.orge59a0ac2012-05-08 17:12:40 +00004144// TODO(andrew): refactor Mix functions here and in transmit_mixer.cc to use
4145// a shared helper.
pbos@webrtc.org6141e132013-04-09 10:09:10 +00004146int32_t
pbos@webrtc.org92135212013-05-14 08:31:39 +00004147Channel::MixOrReplaceAudioWithFile(int mixingFrequency)
niklase@google.com470e71d2011-07-07 08:21:25 +00004148{
andrew@webrtc.org8f693302014-04-25 23:10:28 +00004149 scoped_ptr<int16_t[]> fileBuffer(new int16_t[640]);
andrew@webrtc.orge59a0ac2012-05-08 17:12:40 +00004150 int fileSamples(0);
niklase@google.com470e71d2011-07-07 08:21:25 +00004151
4152 {
mflodman@webrtc.org9a065d12012-03-07 08:12:21 +00004153 CriticalSectionScoped cs(&_fileCritSect);
niklase@google.com470e71d2011-07-07 08:21:25 +00004154
4155 if (_inputFilePlayerPtr == NULL)
4156 {
4157 WEBRTC_TRACE(kTraceWarning, kTraceVoice,
4158 VoEId(_instanceId, _channelId),
4159 "Channel::MixOrReplaceAudioWithFile() fileplayer"
4160 " doesnt exist");
4161 return -1;
4162 }
4163
braveyao@webrtc.orgd7131432012-03-29 10:39:44 +00004164 if (_inputFilePlayerPtr->Get10msAudioFromFile(fileBuffer.get(),
niklase@google.com470e71d2011-07-07 08:21:25 +00004165 fileSamples,
4166 mixingFrequency) == -1)
4167 {
4168 WEBRTC_TRACE(kTraceWarning, kTraceVoice,
4169 VoEId(_instanceId, _channelId),
4170 "Channel::MixOrReplaceAudioWithFile() file mixing "
4171 "failed");
4172 return -1;
4173 }
4174 if (fileSamples == 0)
4175 {
4176 WEBRTC_TRACE(kTraceWarning, kTraceVoice,
4177 VoEId(_instanceId, _channelId),
4178 "Channel::MixOrReplaceAudioWithFile() file is ended");
4179 return 0;
4180 }
4181 }
4182
andrew@webrtc.org63a50982012-05-02 23:56:37 +00004183 assert(_audioFrame.samples_per_channel_ == fileSamples);
niklase@google.com470e71d2011-07-07 08:21:25 +00004184
4185 if (_mixFileWithMicrophone)
4186 {
braveyao@webrtc.orgd7131432012-03-29 10:39:44 +00004187 // Currently file stream is always mono.
4188 // TODO(xians): Change the code when FilePlayer supports real stereo.
andrew@webrtc.org40ee3d02014-04-03 21:56:01 +00004189 MixWithSat(_audioFrame.data_,
4190 _audioFrame.num_channels_,
4191 fileBuffer.get(),
4192 1,
4193 fileSamples);
niklase@google.com470e71d2011-07-07 08:21:25 +00004194 }
4195 else
4196 {
braveyao@webrtc.orgd7131432012-03-29 10:39:44 +00004197 // Replace ACM audio with file.
4198 // Currently file stream is always mono.
4199 // TODO(xians): Change the code when FilePlayer supports real stereo.
niklase@google.com470e71d2011-07-07 08:21:25 +00004200 _audioFrame.UpdateFrame(_channelId,
4201 -1,
braveyao@webrtc.orgd7131432012-03-29 10:39:44 +00004202 fileBuffer.get(),
andrew@webrtc.orge59a0ac2012-05-08 17:12:40 +00004203 fileSamples,
niklase@google.com470e71d2011-07-07 08:21:25 +00004204 mixingFrequency,
4205 AudioFrame::kNormalSpeech,
4206 AudioFrame::kVadUnknown,
4207 1);
4208
4209 }
4210 return 0;
4211}
4212
pbos@webrtc.org6141e132013-04-09 10:09:10 +00004213int32_t
niklase@google.com470e71d2011-07-07 08:21:25 +00004214Channel::MixAudioWithFile(AudioFrame& audioFrame,
pbos@webrtc.org92135212013-05-14 08:31:39 +00004215 int mixingFrequency)
niklase@google.com470e71d2011-07-07 08:21:25 +00004216{
4217 assert(mixingFrequency <= 32000);
4218
andrew@webrtc.org8f693302014-04-25 23:10:28 +00004219 scoped_ptr<int16_t[]> fileBuffer(new int16_t[640]);
andrew@webrtc.orge59a0ac2012-05-08 17:12:40 +00004220 int fileSamples(0);
niklase@google.com470e71d2011-07-07 08:21:25 +00004221
4222 {
mflodman@webrtc.org9a065d12012-03-07 08:12:21 +00004223 CriticalSectionScoped cs(&_fileCritSect);
niklase@google.com470e71d2011-07-07 08:21:25 +00004224
4225 if (_outputFilePlayerPtr == NULL)
4226 {
4227 WEBRTC_TRACE(kTraceWarning, kTraceVoice,
4228 VoEId(_instanceId, _channelId),
4229 "Channel::MixAudioWithFile() file mixing failed");
4230 return -1;
4231 }
4232
4233 // We should get the frequency we ask for.
braveyao@webrtc.orgd7131432012-03-29 10:39:44 +00004234 if (_outputFilePlayerPtr->Get10msAudioFromFile(fileBuffer.get(),
niklase@google.com470e71d2011-07-07 08:21:25 +00004235 fileSamples,
4236 mixingFrequency) == -1)
4237 {
4238 WEBRTC_TRACE(kTraceWarning, kTraceVoice,
4239 VoEId(_instanceId, _channelId),
4240 "Channel::MixAudioWithFile() file mixing failed");
4241 return -1;
4242 }
4243 }
4244
andrew@webrtc.org63a50982012-05-02 23:56:37 +00004245 if (audioFrame.samples_per_channel_ == fileSamples)
niklase@google.com470e71d2011-07-07 08:21:25 +00004246 {
braveyao@webrtc.orgd7131432012-03-29 10:39:44 +00004247 // Currently file stream is always mono.
4248 // TODO(xians): Change the code when FilePlayer supports real stereo.
andrew@webrtc.org40ee3d02014-04-03 21:56:01 +00004249 MixWithSat(audioFrame.data_,
4250 audioFrame.num_channels_,
4251 fileBuffer.get(),
4252 1,
4253 fileSamples);
niklase@google.com470e71d2011-07-07 08:21:25 +00004254 }
4255 else
4256 {
4257 WEBRTC_TRACE(kTraceWarning, kTraceVoice, VoEId(_instanceId,_channelId),
andrew@webrtc.org63a50982012-05-02 23:56:37 +00004258 "Channel::MixAudioWithFile() samples_per_channel_(%d) != "
niklase@google.com470e71d2011-07-07 08:21:25 +00004259 "fileSamples(%d)",
andrew@webrtc.org63a50982012-05-02 23:56:37 +00004260 audioFrame.samples_per_channel_, fileSamples);
niklase@google.com470e71d2011-07-07 08:21:25 +00004261 return -1;
4262 }
4263
4264 return 0;
4265}
4266
4267int
4268Channel::InsertInbandDtmfTone()
4269{
niklas.enbom@webrtc.orgaf26f642011-11-16 12:41:36 +00004270 // Check if we should start a new tone.
niklase@google.com470e71d2011-07-07 08:21:25 +00004271 if (_inbandDtmfQueue.PendingDtmf() &&
4272 !_inbandDtmfGenerator.IsAddingTone() &&
4273 _inbandDtmfGenerator.DelaySinceLastTone() >
4274 kMinTelephoneEventSeparationMs)
4275 {
pbos@webrtc.org6141e132013-04-09 10:09:10 +00004276 int8_t eventCode(0);
4277 uint16_t lengthMs(0);
4278 uint8_t attenuationDb(0);
niklase@google.com470e71d2011-07-07 08:21:25 +00004279
4280 eventCode = _inbandDtmfQueue.NextDtmf(&lengthMs, &attenuationDb);
4281 _inbandDtmfGenerator.AddTone(eventCode, lengthMs, attenuationDb);
4282 if (_playInbandDtmfEvent)
4283 {
4284 // Add tone to output mixer using a reduced length to minimize
4285 // risk of echo.
4286 _outputMixerPtr->PlayDtmfTone(eventCode, lengthMs - 80,
4287 attenuationDb);
4288 }
4289 }
4290
4291 if (_inbandDtmfGenerator.IsAddingTone())
4292 {
pbos@webrtc.org6141e132013-04-09 10:09:10 +00004293 uint16_t frequency(0);
niklase@google.com470e71d2011-07-07 08:21:25 +00004294 _inbandDtmfGenerator.GetSampleRate(frequency);
4295
andrew@webrtc.org63a50982012-05-02 23:56:37 +00004296 if (frequency != _audioFrame.sample_rate_hz_)
niklase@google.com470e71d2011-07-07 08:21:25 +00004297 {
4298 // Update sample rate of Dtmf tone since the mixing frequency
4299 // has changed.
4300 _inbandDtmfGenerator.SetSampleRate(
pbos@webrtc.org6141e132013-04-09 10:09:10 +00004301 (uint16_t) (_audioFrame.sample_rate_hz_));
niklase@google.com470e71d2011-07-07 08:21:25 +00004302 // Reset the tone to be added taking the new sample rate into
4303 // account.
4304 _inbandDtmfGenerator.ResetTone();
4305 }
andrew@webrtc.orgae1a58b2013-01-22 04:44:30 +00004306
pbos@webrtc.org6141e132013-04-09 10:09:10 +00004307 int16_t toneBuffer[320];
4308 uint16_t toneSamples(0);
niklase@google.com470e71d2011-07-07 08:21:25 +00004309 // Get 10ms tone segment and set time since last tone to zero
4310 if (_inbandDtmfGenerator.Get10msTone(toneBuffer, toneSamples) == -1)
4311 {
4312 WEBRTC_TRACE(kTraceWarning, kTraceVoice,
4313 VoEId(_instanceId, _channelId),
4314 "Channel::EncodeAndSend() inserting Dtmf failed");
4315 return -1;
4316 }
4317
niklas.enbom@webrtc.orgaf26f642011-11-16 12:41:36 +00004318 // Replace mixed audio with DTMF tone.
andrew@webrtc.orgae1a58b2013-01-22 04:44:30 +00004319 for (int sample = 0;
andrew@webrtc.org63a50982012-05-02 23:56:37 +00004320 sample < _audioFrame.samples_per_channel_;
niklas.enbom@webrtc.orgaf26f642011-11-16 12:41:36 +00004321 sample++)
4322 {
andrew@webrtc.orgae1a58b2013-01-22 04:44:30 +00004323 for (int channel = 0;
4324 channel < _audioFrame.num_channels_;
niklas.enbom@webrtc.orgaf26f642011-11-16 12:41:36 +00004325 channel++)
4326 {
andrew@webrtc.orgae1a58b2013-01-22 04:44:30 +00004327 const int index = sample * _audioFrame.num_channels_ + channel;
4328 _audioFrame.data_[index] = toneBuffer[sample];
niklas.enbom@webrtc.orgaf26f642011-11-16 12:41:36 +00004329 }
4330 }
andrew@webrtc.orgae1a58b2013-01-22 04:44:30 +00004331
andrew@webrtc.org63a50982012-05-02 23:56:37 +00004332 assert(_audioFrame.samples_per_channel_ == toneSamples);
niklase@google.com470e71d2011-07-07 08:21:25 +00004333 } else
4334 {
4335 // Add 10ms to "delay-since-last-tone" counter
4336 _inbandDtmfGenerator.UpdateDelaySinceLastTone();
4337 }
4338 return 0;
4339}
4340
pbos@webrtc.org6141e132013-04-09 10:09:10 +00004341int32_t
niklase@google.com470e71d2011-07-07 08:21:25 +00004342Channel::SendPacketRaw(const void *data, int len, bool RTCP)
4343{
wu@webrtc.orgfb648da2013-10-18 21:10:51 +00004344 CriticalSectionScoped cs(&_callbackCritSect);
niklase@google.com470e71d2011-07-07 08:21:25 +00004345 if (_transportPtr == NULL)
4346 {
4347 return -1;
4348 }
4349 if (!RTCP)
4350 {
4351 return _transportPtr->SendPacket(_channelId, data, len);
4352 }
4353 else
4354 {
4355 return _transportPtr->SendRTCPPacket(_channelId, data, len);
4356 }
4357}
4358
pwestin@webrtc.org1de01352013-04-11 20:23:35 +00004359// Called for incoming RTP packets after successful RTP header parsing.
4360void Channel::UpdatePacketDelay(uint32_t rtp_timestamp,
4361 uint16_t sequence_number) {
4362 WEBRTC_TRACE(kTraceStream, kTraceVoice, VoEId(_instanceId,_channelId),
4363 "Channel::UpdatePacketDelay(timestamp=%lu, sequenceNumber=%u)",
4364 rtp_timestamp, sequence_number);
niklase@google.com470e71d2011-07-07 08:21:25 +00004365
pwestin@webrtc.org1de01352013-04-11 20:23:35 +00004366 // Get frequency of last received payload
andrew@webrtc.orgeb524d92013-09-23 23:02:24 +00004367 int rtp_receive_frequency = audio_coding_->ReceiveFrequency();
niklase@google.com470e71d2011-07-07 08:21:25 +00004368
pwestin@webrtc.org1de01352013-04-11 20:23:35 +00004369 CodecInst current_receive_codec;
andrew@webrtc.orgeb524d92013-09-23 23:02:24 +00004370 if (audio_coding_->ReceiveCodec(&current_receive_codec) != 0) {
pwestin@webrtc.org1de01352013-04-11 20:23:35 +00004371 return;
4372 }
niklase@google.com470e71d2011-07-07 08:21:25 +00004373
turaj@webrtc.orge46c8d32013-05-22 20:39:43 +00004374 // Update the least required delay.
andrew@webrtc.orgeb524d92013-09-23 23:02:24 +00004375 least_required_delay_ms_ = audio_coding_->LeastRequiredDelayMs();
turaj@webrtc.orge46c8d32013-05-22 20:39:43 +00004376
pwestin@webrtc.org1de01352013-04-11 20:23:35 +00004377 if (STR_CASE_CMP("G722", current_receive_codec.plname) == 0) {
4378 // Even though the actual sampling rate for G.722 audio is
4379 // 16,000 Hz, the RTP clock rate for the G722 payload format is
4380 // 8,000 Hz because that value was erroneously assigned in
4381 // RFC 1890 and must remain unchanged for backward compatibility.
4382 rtp_receive_frequency = 8000;
4383 } else if (STR_CASE_CMP("opus", current_receive_codec.plname) == 0) {
4384 // We are resampling Opus internally to 32,000 Hz until all our
4385 // DSP routines can operate at 48,000 Hz, but the RTP clock
4386 // rate for the Opus payload format is standardized to 48,000 Hz,
4387 // because that is the maximum supported decoding sampling rate.
4388 rtp_receive_frequency = 48000;
4389 }
niklase@google.com470e71d2011-07-07 08:21:25 +00004390
turaj@webrtc.org167b6df2013-12-13 21:05:07 +00004391 // |jitter_buffer_playout_timestamp_| updated in UpdatePlayoutTimestamp for
4392 // every incoming packet.
4393 uint32_t timestamp_diff_ms = (rtp_timestamp -
4394 jitter_buffer_playout_timestamp_) / (rtp_receive_frequency / 1000);
henrik.lundin@webrtc.orgd6692992014-03-20 12:04:09 +00004395 if (!IsNewerTimestamp(rtp_timestamp, jitter_buffer_playout_timestamp_) ||
4396 timestamp_diff_ms > (2 * kVoiceEngineMaxMinPlayoutDelayMs)) {
4397 // If |jitter_buffer_playout_timestamp_| is newer than the incoming RTP
4398 // timestamp, the resulting difference is negative, but is set to zero.
4399 // This can happen when a network glitch causes a packet to arrive late,
4400 // and during long comfort noise periods with clock drift.
4401 timestamp_diff_ms = 0;
4402 }
niklase@google.com470e71d2011-07-07 08:21:25 +00004403
pwestin@webrtc.org1de01352013-04-11 20:23:35 +00004404 uint16_t packet_delay_ms = (rtp_timestamp - _previousTimestamp) /
4405 (rtp_receive_frequency / 1000);
niklase@google.com470e71d2011-07-07 08:21:25 +00004406
pwestin@webrtc.org1de01352013-04-11 20:23:35 +00004407 _previousTimestamp = rtp_timestamp;
niklase@google.com470e71d2011-07-07 08:21:25 +00004408
pwestin@webrtc.org1de01352013-04-11 20:23:35 +00004409 if (timestamp_diff_ms == 0) return;
niklase@google.com470e71d2011-07-07 08:21:25 +00004410
pwestin@webrtc.org1de01352013-04-11 20:23:35 +00004411 if (packet_delay_ms >= 10 && packet_delay_ms <= 60) {
4412 _recPacketDelayMs = packet_delay_ms;
4413 }
niklase@google.com470e71d2011-07-07 08:21:25 +00004414
pwestin@webrtc.org1de01352013-04-11 20:23:35 +00004415 if (_average_jitter_buffer_delay_us == 0) {
4416 _average_jitter_buffer_delay_us = timestamp_diff_ms * 1000;
4417 return;
4418 }
4419
4420 // Filter average delay value using exponential filter (alpha is
4421 // 7/8). We derive 1000 *_average_jitter_buffer_delay_us here (reduces
4422 // risk of rounding error) and compensate for it in GetDelayEstimate()
4423 // later.
4424 _average_jitter_buffer_delay_us = (_average_jitter_buffer_delay_us * 7 +
4425 1000 * timestamp_diff_ms + 500) / 8;
niklase@google.com470e71d2011-07-07 08:21:25 +00004426}
4427
4428void
4429Channel::RegisterReceiveCodecsToRTPModule()
4430{
4431 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId,_channelId),
4432 "Channel::RegisterReceiveCodecsToRTPModule()");
4433
4434
4435 CodecInst codec;
pbos@webrtc.org6141e132013-04-09 10:09:10 +00004436 const uint8_t nSupportedCodecs = AudioCodingModule::NumberOfCodecs();
niklase@google.com470e71d2011-07-07 08:21:25 +00004437
4438 for (int idx = 0; idx < nSupportedCodecs; idx++)
4439 {
4440 // Open up the RTP/RTCP receiver for all supported codecs
andrew@webrtc.orgeb524d92013-09-23 23:02:24 +00004441 if ((audio_coding_->Codec(idx, &codec) == -1) ||
wu@webrtc.org822fbd82013-08-15 23:38:54 +00004442 (rtp_receiver_->RegisterReceivePayload(
4443 codec.plname,
4444 codec.pltype,
4445 codec.plfreq,
4446 codec.channels,
4447 (codec.rate < 0) ? 0 : codec.rate) == -1))
niklase@google.com470e71d2011-07-07 08:21:25 +00004448 {
4449 WEBRTC_TRACE(
4450 kTraceWarning,
4451 kTraceVoice,
4452 VoEId(_instanceId, _channelId),
4453 "Channel::RegisterReceiveCodecsToRTPModule() unable"
4454 " to register %s (%d/%d/%d/%d) to RTP/RTCP receiver",
4455 codec.plname, codec.pltype, codec.plfreq,
4456 codec.channels, codec.rate);
4457 }
4458 else
4459 {
4460 WEBRTC_TRACE(
4461 kTraceInfo,
4462 kTraceVoice,
4463 VoEId(_instanceId, _channelId),
4464 "Channel::RegisterReceiveCodecsToRTPModule() %s "
wu@webrtc.orgfcd12b32011-09-15 20:49:50 +00004465 "(%d/%d/%d/%d) has been added to the RTP/RTCP "
niklase@google.com470e71d2011-07-07 08:21:25 +00004466 "receiver",
4467 codec.plname, codec.pltype, codec.plfreq,
4468 codec.channels, codec.rate);
4469 }
4470 }
4471}
4472
turaj@webrtc.org42259e72012-12-11 02:15:12 +00004473int Channel::SetSecondarySendCodec(const CodecInst& codec,
4474 int red_payload_type) {
turaj@webrtc.org8c8ad852013-01-31 18:20:17 +00004475 // Sanity check for payload type.
4476 if (red_payload_type < 0 || red_payload_type > 127) {
4477 _engineStatisticsPtr->SetLastError(
4478 VE_PLTYPE_ERROR, kTraceError,
4479 "SetRedPayloadType() invalid RED payload type");
4480 return -1;
4481 }
4482
turaj@webrtc.org42259e72012-12-11 02:15:12 +00004483 if (SetRedPayloadType(red_payload_type) < 0) {
4484 _engineStatisticsPtr->SetLastError(
4485 VE_AUDIO_CODING_MODULE_ERROR, kTraceError,
4486 "SetSecondarySendCodec() Failed to register RED ACM");
4487 return -1;
4488 }
andrew@webrtc.orgeb524d92013-09-23 23:02:24 +00004489 if (audio_coding_->RegisterSecondarySendCodec(codec) < 0) {
turaj@webrtc.org42259e72012-12-11 02:15:12 +00004490 _engineStatisticsPtr->SetLastError(
4491 VE_AUDIO_CODING_MODULE_ERROR, kTraceError,
4492 "SetSecondarySendCodec() Failed to register secondary send codec in "
4493 "ACM");
4494 return -1;
4495 }
4496
4497 return 0;
4498}
4499
4500void Channel::RemoveSecondarySendCodec() {
andrew@webrtc.orgeb524d92013-09-23 23:02:24 +00004501 audio_coding_->UnregisterSecondarySendCodec();
turaj@webrtc.org42259e72012-12-11 02:15:12 +00004502}
4503
4504int Channel::GetSecondarySendCodec(CodecInst* codec) {
andrew@webrtc.orgeb524d92013-09-23 23:02:24 +00004505 if (audio_coding_->SecondarySendCodec(codec) < 0) {
turaj@webrtc.org42259e72012-12-11 02:15:12 +00004506 _engineStatisticsPtr->SetLastError(
4507 VE_AUDIO_CODING_MODULE_ERROR, kTraceError,
4508 "GetSecondarySendCodec() Failed to get secondary sent codec from ACM");
4509 return -1;
4510 }
4511 return 0;
4512}
4513
turaj@webrtc.org8c8ad852013-01-31 18:20:17 +00004514// Assuming this method is called with valid payload type.
turaj@webrtc.org42259e72012-12-11 02:15:12 +00004515int Channel::SetRedPayloadType(int red_payload_type) {
turaj@webrtc.org42259e72012-12-11 02:15:12 +00004516 CodecInst codec;
4517 bool found_red = false;
4518
4519 // Get default RED settings from the ACM database
4520 const int num_codecs = AudioCodingModule::NumberOfCodecs();
4521 for (int idx = 0; idx < num_codecs; idx++) {
andrew@webrtc.orgeb524d92013-09-23 23:02:24 +00004522 audio_coding_->Codec(idx, &codec);
turaj@webrtc.org42259e72012-12-11 02:15:12 +00004523 if (!STR_CASE_CMP(codec.plname, "RED")) {
4524 found_red = true;
4525 break;
4526 }
4527 }
4528
4529 if (!found_red) {
4530 _engineStatisticsPtr->SetLastError(
4531 VE_CODEC_ERROR, kTraceError,
4532 "SetRedPayloadType() RED is not supported");
4533 return -1;
4534 }
4535
turaj@webrtc.org9d532fd2013-01-31 18:34:19 +00004536 codec.pltype = red_payload_type;
andrew@webrtc.orgeb524d92013-09-23 23:02:24 +00004537 if (audio_coding_->RegisterSendCodec(codec) < 0) {
turaj@webrtc.org42259e72012-12-11 02:15:12 +00004538 _engineStatisticsPtr->SetLastError(
4539 VE_AUDIO_CODING_MODULE_ERROR, kTraceError,
4540 "SetRedPayloadType() RED registration in ACM module failed");
4541 return -1;
4542 }
4543
4544 if (_rtpRtcpModule->SetSendREDPayloadType(red_payload_type) != 0) {
4545 _engineStatisticsPtr->SetLastError(
4546 VE_RTP_RTCP_MODULE_ERROR, kTraceError,
4547 "SetRedPayloadType() RED registration in RTP/RTCP module failed");
4548 return -1;
4549 }
4550 return 0;
4551}
4552
wu@webrtc.orgebdb0e32014-03-06 23:49:08 +00004553int Channel::SetSendRtpHeaderExtension(bool enable, RTPExtensionType type,
4554 unsigned char id) {
4555 int error = 0;
4556 _rtpRtcpModule->DeregisterSendRtpHeaderExtension(type);
4557 if (enable) {
4558 error = _rtpRtcpModule->RegisterSendRtpHeaderExtension(type, id);
4559 }
4560 return error;
4561}
minyue@webrtc.orgc1a40a72014-05-28 09:52:06 +00004562
pbos@webrtc.orgd900e8b2013-07-03 15:12:26 +00004563} // namespace voe
4564} // namespace webrtc