blob: 7abf7d4ff2b421f3c608495de4dc780b19241dc2 [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
turaj@webrtc.org6388c3e2013-02-12 21:42:18 +000013#include "webrtc/modules/audio_device/include/audio_device.h"
14#include "webrtc/modules/audio_processing/include/audio_processing.h"
15#include "webrtc/modules/utility/interface/audio_frame_operations.h"
16#include "webrtc/modules/utility/interface/process_thread.h"
17#include "webrtc/modules/utility/interface/rtp_dump.h"
18#include "webrtc/system_wrappers/interface/critical_section_wrapper.h"
19#include "webrtc/system_wrappers/interface/logging.h"
20#include "webrtc/system_wrappers/interface/trace.h"
21#include "webrtc/voice_engine/include/voe_base.h"
22#include "webrtc/voice_engine/include/voe_external_media.h"
23#include "webrtc/voice_engine/include/voe_rtp_rtcp.h"
24#include "webrtc/voice_engine/output_mixer.h"
25#include "webrtc/voice_engine/statistics.h"
26#include "webrtc/voice_engine/transmit_mixer.h"
27#include "webrtc/voice_engine/utility.h"
niklase@google.com470e71d2011-07-07 08:21:25 +000028
29#if defined(_WIN32)
30#include <Qos.h>
31#endif
32
andrew@webrtc.org50419b02012-11-14 19:07:54 +000033namespace webrtc {
34namespace voe {
niklase@google.com470e71d2011-07-07 08:21:25 +000035
pbos@webrtc.org6141e132013-04-09 10:09:10 +000036int32_t
niklase@google.com470e71d2011-07-07 08:21:25 +000037Channel::SendData(FrameType frameType,
pbos@webrtc.org6141e132013-04-09 10:09:10 +000038 uint8_t payloadType,
39 uint32_t timeStamp,
40 const uint8_t* payloadData,
41 uint16_t payloadSize,
niklase@google.com470e71d2011-07-07 08:21:25 +000042 const RTPFragmentationHeader* fragmentation)
43{
44 WEBRTC_TRACE(kTraceStream, kTraceVoice, VoEId(_instanceId,_channelId),
45 "Channel::SendData(frameType=%u, payloadType=%u, timeStamp=%u,"
46 " payloadSize=%u, fragmentation=0x%x)",
47 frameType, payloadType, timeStamp, payloadSize, fragmentation);
48
49 if (_includeAudioLevelIndication)
50 {
andrew@webrtc.org755b04a2011-11-15 16:57:56 +000051 assert(_rtpAudioProc.get() != NULL);
niklase@google.com470e71d2011-07-07 08:21:25 +000052 // Store current audio level in the RTP/RTCP module.
53 // The level will be used in combination with voice-activity state
54 // (frameType) to add an RTP header extension
pwestin@webrtc.org2853dde2012-05-11 11:08:54 +000055 _rtpRtcpModule->SetAudioLevel(_rtpAudioProc->level_estimator()->RMS());
niklase@google.com470e71d2011-07-07 08:21:25 +000056 }
57
58 // Push data from ACM to RTP/RTCP-module to deliver audio frame for
59 // packetization.
60 // This call will trigger Transport::SendPacket() from the RTP/RTCP module.
pwestin@webrtc.org2853dde2012-05-11 11:08:54 +000061 if (_rtpRtcpModule->SendOutgoingData((FrameType&)frameType,
niklase@google.com470e71d2011-07-07 08:21:25 +000062 payloadType,
63 timeStamp,
stefan@webrtc.orgddfdfed2012-07-03 13:21:22 +000064 // Leaving the time when this frame was
65 // received from the capture device as
66 // undefined for voice for now.
67 -1,
niklase@google.com470e71d2011-07-07 08:21:25 +000068 payloadData,
69 payloadSize,
70 fragmentation) == -1)
71 {
72 _engineStatisticsPtr->SetLastError(
73 VE_RTP_RTCP_MODULE_ERROR, kTraceWarning,
74 "Channel::SendData() failed to send data to RTP/RTCP module");
75 return -1;
76 }
77
78 _lastLocalTimeStamp = timeStamp;
79 _lastPayloadType = payloadType;
80
81 return 0;
82}
83
pbos@webrtc.org6141e132013-04-09 10:09:10 +000084int32_t
85Channel::InFrameType(int16_t frameType)
niklase@google.com470e71d2011-07-07 08:21:25 +000086{
87 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId,_channelId),
88 "Channel::InFrameType(frameType=%d)", frameType);
89
mflodman@webrtc.org9a065d12012-03-07 08:12:21 +000090 CriticalSectionScoped cs(&_callbackCritSect);
niklase@google.com470e71d2011-07-07 08:21:25 +000091 // 1 indicates speech
92 _sendFrameType = (frameType == 1) ? 1 : 0;
93 return 0;
94}
95
pbos@webrtc.org6141e132013-04-09 10:09:10 +000096int32_t
niklase@google.com470e71d2011-07-07 08:21:25 +000097Channel::OnRxVadDetected(const int vadDecision)
98{
99 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId, _channelId),
100 "Channel::OnRxVadDetected(vadDecision=%d)", vadDecision);
101
mflodman@webrtc.org9a065d12012-03-07 08:12:21 +0000102 CriticalSectionScoped cs(&_callbackCritSect);
niklase@google.com470e71d2011-07-07 08:21:25 +0000103 if (_rxVadObserverPtr)
104 {
105 _rxVadObserverPtr->OnRxVad(_channelId, vadDecision);
106 }
107
108 return 0;
109}
110
111int
112Channel::SendPacket(int channel, const void *data, int len)
113{
114 channel = VoEChannelId(channel);
115 assert(channel == _channelId);
116
117 WEBRTC_TRACE(kTraceStream, kTraceVoice, VoEId(_instanceId,_channelId),
118 "Channel::SendPacket(channel=%d, len=%d)", channel, len);
119
120 if (_transportPtr == NULL)
121 {
122 WEBRTC_TRACE(kTraceError, kTraceVoice, VoEId(_instanceId,_channelId),
123 "Channel::SendPacket() failed to send RTP packet due to"
124 " invalid transport object");
125 return -1;
126 }
127
128 // Insert extra RTP packet using if user has called the InsertExtraRTPPacket
129 // API
130 if (_insertExtraRTPPacket)
131 {
pbos@webrtc.org6141e132013-04-09 10:09:10 +0000132 uint8_t* rtpHdr = (uint8_t*)data;
133 uint8_t M_PT(0);
niklase@google.com470e71d2011-07-07 08:21:25 +0000134 if (_extraMarkerBit)
135 {
136 M_PT = 0x80; // set the M-bit
137 }
138 M_PT += _extraPayloadType; // set the payload type
139 *(++rtpHdr) = M_PT; // modify the M|PT-byte within the RTP header
140 _insertExtraRTPPacket = false; // insert one packet only
141 }
142
pbos@webrtc.org6141e132013-04-09 10:09:10 +0000143 uint8_t* bufferToSendPtr = (uint8_t*)data;
144 int32_t bufferLength = len;
niklase@google.com470e71d2011-07-07 08:21:25 +0000145
146 // Dump the RTP packet to a file (if RTP dump is enabled).
pbos@webrtc.org6141e132013-04-09 10:09:10 +0000147 if (_rtpDumpOut.DumpPacket((const uint8_t*)data, len) == -1)
niklase@google.com470e71d2011-07-07 08:21:25 +0000148 {
149 WEBRTC_TRACE(kTraceWarning, kTraceVoice,
150 VoEId(_instanceId,_channelId),
151 "Channel::SendPacket() RTP dump to output file failed");
152 }
153
154 // SRTP or External encryption
155 if (_encrypting)
156 {
mflodman@webrtc.org9a065d12012-03-07 08:12:21 +0000157 CriticalSectionScoped cs(&_callbackCritSect);
niklase@google.com470e71d2011-07-07 08:21:25 +0000158
159 if (_encryptionPtr)
160 {
161 if (!_encryptionRTPBufferPtr)
162 {
163 // Allocate memory for encryption buffer one time only
164 _encryptionRTPBufferPtr =
pbos@webrtc.org6141e132013-04-09 10:09:10 +0000165 new uint8_t[kVoiceEngineMaxIpPacketSizeBytes];
xians@webrtc.org51253502012-10-25 13:58:02 +0000166 memset(_encryptionRTPBufferPtr, 0,
167 kVoiceEngineMaxIpPacketSizeBytes);
niklase@google.com470e71d2011-07-07 08:21:25 +0000168 }
169
170 // Perform encryption (SRTP or external)
pbos@webrtc.org6141e132013-04-09 10:09:10 +0000171 int32_t encryptedBufferLength = 0;
niklase@google.com470e71d2011-07-07 08:21:25 +0000172 _encryptionPtr->encrypt(_channelId,
173 bufferToSendPtr,
174 _encryptionRTPBufferPtr,
175 bufferLength,
176 (int*)&encryptedBufferLength);
177 if (encryptedBufferLength <= 0)
178 {
179 _engineStatisticsPtr->SetLastError(
180 VE_ENCRYPTION_FAILED,
181 kTraceError, "Channel::SendPacket() encryption failed");
182 return -1;
183 }
184
185 // Replace default data buffer with encrypted buffer
186 bufferToSendPtr = _encryptionRTPBufferPtr;
187 bufferLength = encryptedBufferLength;
188 }
189 }
190
191 // Packet transmission using WebRtc socket transport
192 if (!_externalTransport)
193 {
194 int n = _transportPtr->SendPacket(channel, bufferToSendPtr,
195 bufferLength);
196 if (n < 0)
197 {
198 WEBRTC_TRACE(kTraceError, kTraceVoice,
199 VoEId(_instanceId,_channelId),
200 "Channel::SendPacket() RTP transmission using WebRtc"
201 " sockets failed");
202 return -1;
203 }
204 return n;
205 }
206
207 // Packet transmission using external transport transport
208 {
mflodman@webrtc.org9a065d12012-03-07 08:12:21 +0000209 CriticalSectionScoped cs(&_callbackCritSect);
niklase@google.com470e71d2011-07-07 08:21:25 +0000210
211 int n = _transportPtr->SendPacket(channel,
212 bufferToSendPtr,
213 bufferLength);
214 if (n < 0)
215 {
216 WEBRTC_TRACE(kTraceError, kTraceVoice,
217 VoEId(_instanceId,_channelId),
218 "Channel::SendPacket() RTP transmission using external"
219 " transport failed");
220 return -1;
221 }
222 return n;
223 }
224}
225
226int
227Channel::SendRTCPPacket(int channel, const void *data, int len)
228{
229 channel = VoEChannelId(channel);
230 assert(channel == _channelId);
231
232 WEBRTC_TRACE(kTraceStream, kTraceVoice, VoEId(_instanceId,_channelId),
233 "Channel::SendRTCPPacket(channel=%d, len=%d)", channel, len);
234
niklase@google.com470e71d2011-07-07 08:21:25 +0000235 {
mflodman@webrtc.org9a065d12012-03-07 08:12:21 +0000236 CriticalSectionScoped cs(&_callbackCritSect);
xians@webrtc.org83661f52011-11-25 10:58:15 +0000237 if (_transportPtr == NULL)
238 {
239 WEBRTC_TRACE(kTraceError, kTraceVoice,
240 VoEId(_instanceId,_channelId),
241 "Channel::SendRTCPPacket() failed to send RTCP packet"
242 " due to invalid transport object");
243 return -1;
244 }
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
258 // SRTP or External encryption
259 if (_encrypting)
260 {
mflodman@webrtc.org9a065d12012-03-07 08:12:21 +0000261 CriticalSectionScoped cs(&_callbackCritSect);
niklase@google.com470e71d2011-07-07 08:21:25 +0000262
263 if (_encryptionPtr)
264 {
265 if (!_encryptionRTCPBufferPtr)
266 {
267 // Allocate memory for encryption buffer one time only
268 _encryptionRTCPBufferPtr =
pbos@webrtc.org6141e132013-04-09 10:09:10 +0000269 new uint8_t[kVoiceEngineMaxIpPacketSizeBytes];
niklase@google.com470e71d2011-07-07 08:21:25 +0000270 }
271
272 // Perform encryption (SRTP or external).
pbos@webrtc.org6141e132013-04-09 10:09:10 +0000273 int32_t encryptedBufferLength = 0;
niklase@google.com470e71d2011-07-07 08:21:25 +0000274 _encryptionPtr->encrypt_rtcp(_channelId,
275 bufferToSendPtr,
276 _encryptionRTCPBufferPtr,
277 bufferLength,
278 (int*)&encryptedBufferLength);
279 if (encryptedBufferLength <= 0)
280 {
281 _engineStatisticsPtr->SetLastError(
282 VE_ENCRYPTION_FAILED, kTraceError,
283 "Channel::SendRTCPPacket() encryption failed");
284 return -1;
285 }
286
287 // Replace default data buffer with encrypted buffer
288 bufferToSendPtr = _encryptionRTCPBufferPtr;
289 bufferLength = encryptedBufferLength;
290 }
291 }
292
293 // Packet transmission using WebRtc socket transport
294 if (!_externalTransport)
295 {
296 int n = _transportPtr->SendRTCPPacket(channel,
297 bufferToSendPtr,
298 bufferLength);
299 if (n < 0)
300 {
301 WEBRTC_TRACE(kTraceInfo, kTraceVoice,
302 VoEId(_instanceId,_channelId),
303 "Channel::SendRTCPPacket() transmission using WebRtc"
304 " sockets failed");
305 return -1;
306 }
307 return n;
308 }
309
310 // Packet transmission using external transport transport
311 {
mflodman@webrtc.org9a065d12012-03-07 08:12:21 +0000312 CriticalSectionScoped cs(&_callbackCritSect);
henrike@webrtc.orgde727ab2012-11-18 18:49:13 +0000313 if (_transportPtr == NULL)
314 {
315 return -1;
316 }
niklase@google.com470e71d2011-07-07 08:21:25 +0000317 int n = _transportPtr->SendRTCPPacket(channel,
318 bufferToSendPtr,
319 bufferLength);
320 if (n < 0)
321 {
322 WEBRTC_TRACE(kTraceInfo, kTraceVoice,
323 VoEId(_instanceId,_channelId),
324 "Channel::SendRTCPPacket() transmission using external"
325 " transport failed");
326 return -1;
327 }
328 return n;
329 }
330
331 return len;
332}
333
334void
pbos@webrtc.org6141e132013-04-09 10:09:10 +0000335Channel::OnPlayTelephoneEvent(const int32_t id,
336 const uint8_t event,
337 const uint16_t lengthMs,
338 const uint8_t volume)
niklase@google.com470e71d2011-07-07 08:21:25 +0000339{
340 WEBRTC_TRACE(kTraceStream, kTraceVoice, VoEId(_instanceId,_channelId),
341 "Channel::OnPlayTelephoneEvent(id=%d, event=%u, lengthMs=%u,"
wu@webrtc.orgfcd12b32011-09-15 20:49:50 +0000342 " volume=%u)", id, event, lengthMs, volume);
niklase@google.com470e71d2011-07-07 08:21:25 +0000343
344 if (!_playOutbandDtmfEvent || (event > 15))
345 {
346 // Ignore callback since feedback is disabled or event is not a
347 // Dtmf tone event.
348 return;
349 }
350
351 assert(_outputMixerPtr != NULL);
352
353 // Start playing out the Dtmf tone (if playout is enabled).
354 // Reduce length of tone with 80ms to the reduce risk of echo.
355 _outputMixerPtr->PlayDtmfTone(event, lengthMs - 80, volume);
356}
357
358void
pbos@webrtc.org6141e132013-04-09 10:09:10 +0000359Channel::OnIncomingSSRCChanged(const int32_t id,
360 const uint32_t SSRC)
niklase@google.com470e71d2011-07-07 08:21:25 +0000361{
362 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId,_channelId),
363 "Channel::OnIncomingSSRCChanged(id=%d, SSRC=%d)",
364 id, SSRC);
365
pbos@webrtc.org6141e132013-04-09 10:09:10 +0000366 int32_t channel = VoEChannelId(id);
niklase@google.com470e71d2011-07-07 08:21:25 +0000367 assert(channel == _channelId);
368
369 // Reset RTP-module counters since a new incoming RTP stream is detected
pwestin@webrtc.org2853dde2012-05-11 11:08:54 +0000370 _rtpRtcpModule->ResetReceiveDataCountersRTP();
371 _rtpRtcpModule->ResetStatisticsRTP();
niklase@google.com470e71d2011-07-07 08:21:25 +0000372
373 if (_rtpObserver)
374 {
mflodman@webrtc.org9a065d12012-03-07 08:12:21 +0000375 CriticalSectionScoped cs(&_callbackCritSect);
niklase@google.com470e71d2011-07-07 08:21:25 +0000376
377 if (_rtpObserverPtr)
378 {
379 // Send new SSRC to registered observer using callback
380 _rtpObserverPtr->OnIncomingSSRCChanged(channel, SSRC);
381 }
382 }
383}
384
pbos@webrtc.org6141e132013-04-09 10:09:10 +0000385void Channel::OnIncomingCSRCChanged(const int32_t id,
386 const uint32_t CSRC,
niklase@google.com470e71d2011-07-07 08:21:25 +0000387 const bool added)
388{
389 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId,_channelId),
390 "Channel::OnIncomingCSRCChanged(id=%d, CSRC=%d, added=%d)",
391 id, CSRC, added);
392
pbos@webrtc.org6141e132013-04-09 10:09:10 +0000393 int32_t channel = VoEChannelId(id);
niklase@google.com470e71d2011-07-07 08:21:25 +0000394 assert(channel == _channelId);
395
396 if (_rtpObserver)
397 {
mflodman@webrtc.org9a065d12012-03-07 08:12:21 +0000398 CriticalSectionScoped cs(&_callbackCritSect);
niklase@google.com470e71d2011-07-07 08:21:25 +0000399
400 if (_rtpObserverPtr)
401 {
402 _rtpObserverPtr->OnIncomingCSRCChanged(channel, CSRC, added);
403 }
404 }
405}
406
407void
pbos@webrtc.org6141e132013-04-09 10:09:10 +0000408Channel::OnApplicationDataReceived(const int32_t id,
409 const uint8_t subType,
410 const uint32_t name,
411 const uint16_t length,
412 const uint8_t* data)
niklase@google.com470e71d2011-07-07 08:21:25 +0000413{
414 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId,_channelId),
415 "Channel::OnApplicationDataReceived(id=%d, subType=%u,"
416 " name=%u, length=%u)",
417 id, subType, name, length);
418
pbos@webrtc.org6141e132013-04-09 10:09:10 +0000419 int32_t channel = VoEChannelId(id);
niklase@google.com470e71d2011-07-07 08:21:25 +0000420 assert(channel == _channelId);
421
422 if (_rtcpObserver)
423 {
mflodman@webrtc.org9a065d12012-03-07 08:12:21 +0000424 CriticalSectionScoped cs(&_callbackCritSect);
niklase@google.com470e71d2011-07-07 08:21:25 +0000425
426 if (_rtcpObserverPtr)
427 {
428 _rtcpObserverPtr->OnApplicationDataReceived(channel,
429 subType,
430 name,
431 data,
432 length);
433 }
434 }
435}
436
pbos@webrtc.org6141e132013-04-09 10:09:10 +0000437int32_t
niklase@google.com470e71d2011-07-07 08:21:25 +0000438Channel::OnInitializeDecoder(
pbos@webrtc.org6141e132013-04-09 10:09:10 +0000439 const int32_t id,
440 const int8_t payloadType,
leozwang@webrtc.org813e4b02012-03-01 18:34:25 +0000441 const char payloadName[RTP_PAYLOAD_NAME_SIZE],
xians@google.com0b0665a2011-08-08 08:18:44 +0000442 const int frequency,
pbos@webrtc.org6141e132013-04-09 10:09:10 +0000443 const uint8_t channels,
444 const uint32_t rate)
niklase@google.com470e71d2011-07-07 08:21:25 +0000445{
446 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId,_channelId),
447 "Channel::OnInitializeDecoder(id=%d, payloadType=%d, "
448 "payloadName=%s, frequency=%u, channels=%u, rate=%u)",
449 id, payloadType, payloadName, frequency, channels, rate);
450
andrew@webrtc.orgceb148c2011-08-23 17:53:54 +0000451 assert(VoEChannelId(id) == _channelId);
niklase@google.com470e71d2011-07-07 08:21:25 +0000452
henrika@webrtc.orgf75901f2012-01-16 08:45:42 +0000453 CodecInst receiveCodec = {0};
454 CodecInst dummyCodec = {0};
niklase@google.com470e71d2011-07-07 08:21:25 +0000455
456 receiveCodec.pltype = payloadType;
niklase@google.com470e71d2011-07-07 08:21:25 +0000457 receiveCodec.plfreq = frequency;
458 receiveCodec.channels = channels;
459 receiveCodec.rate = rate;
henrika@webrtc.orgf75901f2012-01-16 08:45:42 +0000460 strncpy(receiveCodec.plname, payloadName, RTP_PAYLOAD_NAME_SIZE - 1);
andrew@webrtc.orgae1a58b2013-01-22 04:44:30 +0000461
tina.legrand@webrtc.org7a7a0082013-02-21 10:27:48 +0000462 _audioCodingModule.Codec(payloadName, &dummyCodec, frequency, channels);
niklase@google.com470e71d2011-07-07 08:21:25 +0000463 receiveCodec.pacsize = dummyCodec.pacsize;
464
465 // Register the new codec to the ACM
466 if (_audioCodingModule.RegisterReceiveCodec(receiveCodec) == -1)
467 {
468 WEBRTC_TRACE(kTraceWarning, kTraceVoice,
andrew@webrtc.orgceb148c2011-08-23 17:53:54 +0000469 VoEId(_instanceId, _channelId),
niklase@google.com470e71d2011-07-07 08:21:25 +0000470 "Channel::OnInitializeDecoder() invalid codec ("
471 "pt=%d, name=%s) received - 1", payloadType, payloadName);
472 _engineStatisticsPtr->SetLastError(VE_AUDIO_CODING_MODULE_ERROR);
473 return -1;
474 }
475
476 return 0;
477}
478
479void
pbos@webrtc.org6141e132013-04-09 10:09:10 +0000480Channel::OnPacketTimeout(const int32_t id)
niklase@google.com470e71d2011-07-07 08:21:25 +0000481{
482 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId,_channelId),
483 "Channel::OnPacketTimeout(id=%d)", id);
484
mflodman@webrtc.org9a065d12012-03-07 08:12:21 +0000485 CriticalSectionScoped cs(_callbackCritSectPtr);
niklase@google.com470e71d2011-07-07 08:21:25 +0000486 if (_voiceEngineObserverPtr)
487 {
488 if (_receiving || _externalTransport)
489 {
pbos@webrtc.org6141e132013-04-09 10:09:10 +0000490 int32_t channel = VoEChannelId(id);
niklase@google.com470e71d2011-07-07 08:21:25 +0000491 assert(channel == _channelId);
492 // Ensure that next OnReceivedPacket() callback will trigger
493 // a VE_PACKET_RECEIPT_RESTARTED callback.
494 _rtpPacketTimedOut = true;
495 // Deliver callback to the observer
496 WEBRTC_TRACE(kTraceInfo, kTraceVoice,
497 VoEId(_instanceId,_channelId),
498 "Channel::OnPacketTimeout() => "
499 "CallbackOnError(VE_RECEIVE_PACKET_TIMEOUT)");
500 _voiceEngineObserverPtr->CallbackOnError(channel,
501 VE_RECEIVE_PACKET_TIMEOUT);
502 }
503 }
504}
505
506void
pbos@webrtc.org6141e132013-04-09 10:09:10 +0000507Channel::OnReceivedPacket(const int32_t id,
niklase@google.com470e71d2011-07-07 08:21:25 +0000508 const RtpRtcpPacketType packetType)
509{
510 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId,_channelId),
511 "Channel::OnReceivedPacket(id=%d, packetType=%d)",
512 id, packetType);
513
andrew@webrtc.orgceb148c2011-08-23 17:53:54 +0000514 assert(VoEChannelId(id) == _channelId);
niklase@google.com470e71d2011-07-07 08:21:25 +0000515
516 // Notify only for the case when we have restarted an RTP session.
517 if (_rtpPacketTimedOut && (kPacketRtp == packetType))
518 {
mflodman@webrtc.org9a065d12012-03-07 08:12:21 +0000519 CriticalSectionScoped cs(_callbackCritSectPtr);
niklase@google.com470e71d2011-07-07 08:21:25 +0000520 if (_voiceEngineObserverPtr)
521 {
pbos@webrtc.org6141e132013-04-09 10:09:10 +0000522 int32_t channel = VoEChannelId(id);
niklase@google.com470e71d2011-07-07 08:21:25 +0000523 assert(channel == _channelId);
524 // Reset timeout mechanism
525 _rtpPacketTimedOut = false;
526 // Deliver callback to the observer
527 WEBRTC_TRACE(kTraceInfo, kTraceVoice,
528 VoEId(_instanceId,_channelId),
529 "Channel::OnPacketTimeout() =>"
530 " CallbackOnError(VE_PACKET_RECEIPT_RESTARTED)");
531 _voiceEngineObserverPtr->CallbackOnError(
532 channel,
533 VE_PACKET_RECEIPT_RESTARTED);
534 }
535 }
536}
537
538void
pbos@webrtc.org6141e132013-04-09 10:09:10 +0000539Channel::OnPeriodicDeadOrAlive(const int32_t id,
niklase@google.com470e71d2011-07-07 08:21:25 +0000540 const RTPAliveType alive)
541{
542 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId,_channelId),
543 "Channel::OnPeriodicDeadOrAlive(id=%d, alive=%d)", id, alive);
544
henrika@webrtc.org19da7192013-04-05 14:34:57 +0000545 {
546 CriticalSectionScoped cs(&_callbackCritSect);
547 if (!_connectionObserver)
548 return;
549 }
niklase@google.com470e71d2011-07-07 08:21:25 +0000550
pbos@webrtc.org6141e132013-04-09 10:09:10 +0000551 int32_t channel = VoEChannelId(id);
niklase@google.com470e71d2011-07-07 08:21:25 +0000552 assert(channel == _channelId);
553
554 // Use Alive as default to limit risk of false Dead detections
555 bool isAlive(true);
556
557 // Always mark the connection as Dead when the module reports kRtpDead
558 if (kRtpDead == alive)
559 {
560 isAlive = false;
561 }
562
563 // It is possible that the connection is alive even if no RTP packet has
564 // been received for a long time since the other side might use VAD/DTX
565 // and a low SID-packet update rate.
566 if ((kRtpNoRtp == alive) && _playing)
567 {
568 // Detect Alive for all NetEQ states except for the case when we are
569 // in PLC_CNG state.
570 // PLC_CNG <=> background noise only due to long expand or error.
571 // Note that, the case where the other side stops sending during CNG
572 // state will be detected as Alive. Dead is is not set until after
573 // missing RTCP packets for at least twelve seconds (handled
574 // internally by the RTP/RTCP module).
575 isAlive = (_outputSpeechType != AudioFrame::kPLCCNG);
576 }
577
578 UpdateDeadOrAliveCounters(isAlive);
579
580 // Send callback to the registered observer
581 if (_connectionObserver)
582 {
mflodman@webrtc.org9a065d12012-03-07 08:12:21 +0000583 CriticalSectionScoped cs(&_callbackCritSect);
niklase@google.com470e71d2011-07-07 08:21:25 +0000584 if (_connectionObserverPtr)
585 {
586 _connectionObserverPtr->OnPeriodicDeadOrAlive(channel, isAlive);
587 }
588 }
589}
590
pbos@webrtc.org6141e132013-04-09 10:09:10 +0000591int32_t
592Channel::OnReceivedPayloadData(const uint8_t* payloadData,
593 const uint16_t payloadSize,
niklase@google.com470e71d2011-07-07 08:21:25 +0000594 const WebRtcRTPHeader* rtpHeader)
595{
596 WEBRTC_TRACE(kTraceStream, kTraceVoice, VoEId(_instanceId,_channelId),
597 "Channel::OnReceivedPayloadData(payloadSize=%d,"
598 " payloadType=%u, audioChannel=%u)",
599 payloadSize,
600 rtpHeader->header.payloadType,
601 rtpHeader->type.Audio.channel);
602
roosa@google.com0870f022012-12-12 21:31:41 +0000603 _lastRemoteTimeStamp = rtpHeader->header.timestamp;
604
niklase@google.com470e71d2011-07-07 08:21:25 +0000605 if (!_playing)
606 {
607 // Avoid inserting into NetEQ when we are not playing. Count the
608 // packet as discarded.
609 WEBRTC_TRACE(kTraceStream, kTraceVoice,
610 VoEId(_instanceId, _channelId),
611 "received packet is discarded since playing is not"
612 " activated");
613 _numberOfDiscardedPackets++;
614 return 0;
615 }
616
617 // Push the incoming payload (parsed and ready for decoding) into the ACM
tina.legrand@webrtc.org16b6b902012-04-12 11:02:38 +0000618 if (_audioCodingModule.IncomingPacket(payloadData,
niklase@google.com470e71d2011-07-07 08:21:25 +0000619 payloadSize,
620 *rtpHeader) != 0)
621 {
622 _engineStatisticsPtr->SetLastError(
623 VE_AUDIO_CODING_MODULE_ERROR, kTraceWarning,
624 "Channel::OnReceivedPayloadData() unable to push data to the ACM");
625 return -1;
626 }
627
628 // Update the packet delay
629 UpdatePacketDelay(rtpHeader->header.timestamp,
630 rtpHeader->header.sequenceNumber);
niklase@google.com470e71d2011-07-07 08:21:25 +0000631 return 0;
632}
633
pbos@webrtc.org6141e132013-04-09 10:09:10 +0000634int32_t Channel::GetAudioFrame(const int32_t id, AudioFrame& audioFrame)
niklase@google.com470e71d2011-07-07 08:21:25 +0000635{
636 WEBRTC_TRACE(kTraceStream, kTraceVoice, VoEId(_instanceId,_channelId),
637 "Channel::GetAudioFrame(id=%d)", id);
638
639 // Get 10ms raw PCM data from the ACM (mixer limits output frequency)
andrew@webrtc.org63a50982012-05-02 23:56:37 +0000640 if (_audioCodingModule.PlayoutData10Ms(audioFrame.sample_rate_hz_,
tina.legrand@webrtc.org7a7a0082013-02-21 10:27:48 +0000641 &audioFrame) == -1)
niklase@google.com470e71d2011-07-07 08:21:25 +0000642 {
643 WEBRTC_TRACE(kTraceError, kTraceVoice,
644 VoEId(_instanceId,_channelId),
645 "Channel::GetAudioFrame() PlayoutData10Ms() failed!");
andrew@webrtc.org7859e102012-01-13 00:30:11 +0000646 // In all likelihood, the audio in this frame is garbage. We return an
647 // error so that the audio mixer module doesn't add it to the mix. As
648 // a result, it won't be played out and the actions skipped here are
649 // irrelevant.
650 return -1;
niklase@google.com470e71d2011-07-07 08:21:25 +0000651 }
652
653 if (_RxVadDetection)
654 {
655 UpdateRxVadDetection(audioFrame);
656 }
657
658 // Convert module ID to internal VoE channel ID
andrew@webrtc.org63a50982012-05-02 23:56:37 +0000659 audioFrame.id_ = VoEChannelId(audioFrame.id_);
niklase@google.com470e71d2011-07-07 08:21:25 +0000660 // Store speech type for dead-or-alive detection
andrew@webrtc.org63a50982012-05-02 23:56:37 +0000661 _outputSpeechType = audioFrame.speech_type_;
niklase@google.com470e71d2011-07-07 08:21:25 +0000662
663 // Perform far-end AudioProcessing module processing on the received signal
664 if (_rxApmIsEnabled)
665 {
666 ApmProcessRx(audioFrame);
667 }
668
669 // Output volume scaling
670 if (_outputGain < 0.99f || _outputGain > 1.01f)
671 {
672 AudioFrameOperations::ScaleWithSat(_outputGain, audioFrame);
673 }
674
675 // Scale left and/or right channel(s) if stereo and master balance is
676 // active
677
678 if (_panLeft != 1.0f || _panRight != 1.0f)
679 {
andrew@webrtc.org63a50982012-05-02 23:56:37 +0000680 if (audioFrame.num_channels_ == 1)
niklase@google.com470e71d2011-07-07 08:21:25 +0000681 {
682 // Emulate stereo mode since panning is active.
683 // The mono signal is copied to both left and right channels here.
andrew@webrtc.org4ecea3e2012-06-27 03:25:31 +0000684 AudioFrameOperations::MonoToStereo(&audioFrame);
niklase@google.com470e71d2011-07-07 08:21:25 +0000685 }
686 // For true stereo mode (when we are receiving a stereo signal), no
687 // action is needed.
688
689 // Do the panning operation (the audio frame contains stereo at this
690 // stage)
691 AudioFrameOperations::Scale(_panLeft, _panRight, audioFrame);
692 }
693
694 // Mix decoded PCM output with file if file mixing is enabled
695 if (_outputFilePlaying)
696 {
andrew@webrtc.org63a50982012-05-02 23:56:37 +0000697 MixAudioWithFile(audioFrame, audioFrame.sample_rate_hz_);
niklase@google.com470e71d2011-07-07 08:21:25 +0000698 }
699
700 // Place channel in on-hold state (~muted) if on-hold is activated
701 if (_outputIsOnHold)
702 {
703 AudioFrameOperations::Mute(audioFrame);
704 }
705
706 // External media
707 if (_outputExternalMedia)
708 {
mflodman@webrtc.org9a065d12012-03-07 08:12:21 +0000709 CriticalSectionScoped cs(&_callbackCritSect);
andrew@webrtc.org63a50982012-05-02 23:56:37 +0000710 const bool isStereo = (audioFrame.num_channels_ == 2);
niklase@google.com470e71d2011-07-07 08:21:25 +0000711 if (_outputExternalMediaCallbackPtr)
712 {
713 _outputExternalMediaCallbackPtr->Process(
714 _channelId,
715 kPlaybackPerChannel,
pbos@webrtc.org6141e132013-04-09 10:09:10 +0000716 (int16_t*)audioFrame.data_,
andrew@webrtc.org63a50982012-05-02 23:56:37 +0000717 audioFrame.samples_per_channel_,
718 audioFrame.sample_rate_hz_,
niklase@google.com470e71d2011-07-07 08:21:25 +0000719 isStereo);
720 }
721 }
722
723 // Record playout if enabled
724 {
mflodman@webrtc.org9a065d12012-03-07 08:12:21 +0000725 CriticalSectionScoped cs(&_fileCritSect);
niklase@google.com470e71d2011-07-07 08:21:25 +0000726
727 if (_outputFileRecording && _outputFileRecorderPtr)
728 {
niklas.enbom@webrtc.org5398d952012-03-26 08:11:25 +0000729 _outputFileRecorderPtr->RecordAudioToFile(audioFrame);
niklase@google.com470e71d2011-07-07 08:21:25 +0000730 }
731 }
732
733 // Measure audio level (0-9)
734 _outputAudioLevel.ComputeLevel(audioFrame);
735
736 return 0;
737}
738
pbos@webrtc.org6141e132013-04-09 10:09:10 +0000739int32_t
740Channel::NeededFrequency(const int32_t id)
niklase@google.com470e71d2011-07-07 08:21:25 +0000741{
742 WEBRTC_TRACE(kTraceStream, kTraceVoice, VoEId(_instanceId,_channelId),
743 "Channel::NeededFrequency(id=%d)", id);
744
745 int highestNeeded = 0;
746
747 // Determine highest needed receive frequency
pbos@webrtc.org6141e132013-04-09 10:09:10 +0000748 int32_t receiveFrequency = _audioCodingModule.ReceiveFrequency();
niklase@google.com470e71d2011-07-07 08:21:25 +0000749
750 // Return the bigger of playout and receive frequency in the ACM.
751 if (_audioCodingModule.PlayoutFrequency() > receiveFrequency)
752 {
753 highestNeeded = _audioCodingModule.PlayoutFrequency();
754 }
755 else
756 {
757 highestNeeded = receiveFrequency;
758 }
759
760 // Special case, if we're playing a file on the playout side
761 // we take that frequency into consideration as well
762 // This is not needed on sending side, since the codec will
763 // limit the spectrum anyway.
764 if (_outputFilePlaying)
765 {
mflodman@webrtc.org9a065d12012-03-07 08:12:21 +0000766 CriticalSectionScoped cs(&_fileCritSect);
niklase@google.com470e71d2011-07-07 08:21:25 +0000767 if (_outputFilePlayerPtr && _outputFilePlaying)
768 {
769 if(_outputFilePlayerPtr->Frequency()>highestNeeded)
770 {
771 highestNeeded=_outputFilePlayerPtr->Frequency();
772 }
773 }
774 }
775
776 return(highestNeeded);
777}
778
pbos@webrtc.org6141e132013-04-09 10:09:10 +0000779int32_t
niklase@google.com470e71d2011-07-07 08:21:25 +0000780Channel::CreateChannel(Channel*& channel,
pbos@webrtc.org6141e132013-04-09 10:09:10 +0000781 const int32_t channelId,
782 const uint32_t instanceId)
niklase@google.com470e71d2011-07-07 08:21:25 +0000783{
784 WEBRTC_TRACE(kTraceMemory, kTraceVoice, VoEId(instanceId,channelId),
785 "Channel::CreateChannel(channelId=%d, instanceId=%d)",
786 channelId, instanceId);
787
788 channel = new Channel(channelId, instanceId);
789 if (channel == NULL)
790 {
791 WEBRTC_TRACE(kTraceMemory, kTraceVoice,
792 VoEId(instanceId,channelId),
793 "Channel::CreateChannel() unable to allocate memory for"
794 " channel");
795 return -1;
796 }
797 return 0;
798}
799
800void
pbos@webrtc.org6141e132013-04-09 10:09:10 +0000801Channel::PlayNotification(const int32_t id, const uint32_t durationMs)
niklase@google.com470e71d2011-07-07 08:21:25 +0000802{
803 WEBRTC_TRACE(kTraceStream, kTraceVoice, VoEId(_instanceId,_channelId),
804 "Channel::PlayNotification(id=%d, durationMs=%d)",
805 id, durationMs);
806
807 // Not implement yet
808}
809
810void
pbos@webrtc.org6141e132013-04-09 10:09:10 +0000811Channel::RecordNotification(const int32_t id, const uint32_t durationMs)
niklase@google.com470e71d2011-07-07 08:21:25 +0000812{
813 WEBRTC_TRACE(kTraceStream, kTraceVoice, VoEId(_instanceId,_channelId),
814 "Channel::RecordNotification(id=%d, durationMs=%d)",
815 id, durationMs);
816
817 // Not implement yet
818}
819
820void
pbos@webrtc.org6141e132013-04-09 10:09:10 +0000821Channel::PlayFileEnded(const int32_t id)
niklase@google.com470e71d2011-07-07 08:21:25 +0000822{
823 WEBRTC_TRACE(kTraceStream, kTraceVoice, VoEId(_instanceId,_channelId),
824 "Channel::PlayFileEnded(id=%d)", id);
825
826 if (id == _inputFilePlayerId)
827 {
mflodman@webrtc.org9a065d12012-03-07 08:12:21 +0000828 CriticalSectionScoped cs(&_fileCritSect);
niklase@google.com470e71d2011-07-07 08:21:25 +0000829
830 _inputFilePlaying = false;
831 WEBRTC_TRACE(kTraceStateInfo, kTraceVoice,
832 VoEId(_instanceId,_channelId),
833 "Channel::PlayFileEnded() => input file player module is"
834 " shutdown");
835 }
836 else if (id == _outputFilePlayerId)
837 {
mflodman@webrtc.org9a065d12012-03-07 08:12:21 +0000838 CriticalSectionScoped cs(&_fileCritSect);
niklase@google.com470e71d2011-07-07 08:21:25 +0000839
840 _outputFilePlaying = false;
841 WEBRTC_TRACE(kTraceStateInfo, kTraceVoice,
842 VoEId(_instanceId,_channelId),
843 "Channel::PlayFileEnded() => output file player module is"
844 " shutdown");
845 }
846}
847
848void
pbos@webrtc.org6141e132013-04-09 10:09:10 +0000849Channel::RecordFileEnded(const int32_t id)
niklase@google.com470e71d2011-07-07 08:21:25 +0000850{
851 WEBRTC_TRACE(kTraceStream, kTraceVoice, VoEId(_instanceId,_channelId),
852 "Channel::RecordFileEnded(id=%d)", id);
853
854 assert(id == _outputFileRecorderId);
855
mflodman@webrtc.org9a065d12012-03-07 08:12:21 +0000856 CriticalSectionScoped cs(&_fileCritSect);
niklase@google.com470e71d2011-07-07 08:21:25 +0000857
858 _outputFileRecording = false;
859 WEBRTC_TRACE(kTraceStateInfo, kTraceVoice,
860 VoEId(_instanceId,_channelId),
861 "Channel::RecordFileEnded() => output file recorder module is"
862 " shutdown");
863}
864
pbos@webrtc.org6141e132013-04-09 10:09:10 +0000865Channel::Channel(const int32_t channelId,
866 const uint32_t instanceId) :
niklase@google.com470e71d2011-07-07 08:21:25 +0000867 _fileCritSect(*CriticalSectionWrapper::CreateCriticalSection()),
868 _callbackCritSect(*CriticalSectionWrapper::CreateCriticalSection()),
niklase@google.com470e71d2011-07-07 08:21:25 +0000869 _instanceId(instanceId),
xians@google.com22963ab2011-08-03 12:40:23 +0000870 _channelId(channelId),
niklase@google.com470e71d2011-07-07 08:21:25 +0000871 _audioCodingModule(*AudioCodingModule::Create(
xians@google.com22963ab2011-08-03 12:40:23 +0000872 VoEModuleId(instanceId, channelId))),
niklase@google.com470e71d2011-07-07 08:21:25 +0000873 _rtpDumpIn(*RtpDump::CreateRtpDump()),
874 _rtpDumpOut(*RtpDump::CreateRtpDump()),
niklase@google.com470e71d2011-07-07 08:21:25 +0000875 _outputAudioLevel(),
niklase@google.com470e71d2011-07-07 08:21:25 +0000876 _externalTransport(false),
niklase@google.com470e71d2011-07-07 08:21:25 +0000877 _inputFilePlayerPtr(NULL),
878 _outputFilePlayerPtr(NULL),
879 _outputFileRecorderPtr(NULL),
880 // Avoid conflict with other channels by adding 1024 - 1026,
881 // won't use as much as 1024 channels.
882 _inputFilePlayerId(VoEModuleId(instanceId, channelId) + 1024),
883 _outputFilePlayerId(VoEModuleId(instanceId, channelId) + 1025),
884 _outputFileRecorderId(VoEModuleId(instanceId, channelId) + 1026),
885 _inputFilePlaying(false),
886 _outputFilePlaying(false),
887 _outputFileRecording(false),
xians@google.com22963ab2011-08-03 12:40:23 +0000888 _inbandDtmfQueue(VoEModuleId(instanceId, channelId)),
889 _inbandDtmfGenerator(VoEModuleId(instanceId, channelId)),
niklase@google.com470e71d2011-07-07 08:21:25 +0000890 _inputExternalMedia(false),
xians@google.com22963ab2011-08-03 12:40:23 +0000891 _outputExternalMedia(false),
niklase@google.com470e71d2011-07-07 08:21:25 +0000892 _inputExternalMediaCallbackPtr(NULL),
893 _outputExternalMediaCallbackPtr(NULL),
xians@google.com22963ab2011-08-03 12:40:23 +0000894 _encryptionRTPBufferPtr(NULL),
895 _decryptionRTPBufferPtr(NULL),
896 _encryptionRTCPBufferPtr(NULL),
897 _decryptionRTCPBufferPtr(NULL),
898 _timeStamp(0), // This is just an offset, RTP module will add it's own random offset
899 _sendTelephoneEventPayloadType(106),
pwestin@webrtc.org1de01352013-04-11 20:23:35 +0000900 playout_timestamp_rtp_(0),
901 playout_timestamp_rtcp_(0),
xians@google.com22963ab2011-08-03 12:40:23 +0000902 _numberOfDiscardedPackets(0),
903 _engineStatisticsPtr(NULL),
henrika@webrtc.org2919e952012-01-31 08:45:03 +0000904 _outputMixerPtr(NULL),
905 _transmitMixerPtr(NULL),
xians@google.com22963ab2011-08-03 12:40:23 +0000906 _moduleProcessThreadPtr(NULL),
907 _audioDeviceModulePtr(NULL),
908 _voiceEngineObserverPtr(NULL),
909 _callbackCritSectPtr(NULL),
910 _transportPtr(NULL),
911 _encryptionPtr(NULL),
andrew@webrtc.org755b04a2011-11-15 16:57:56 +0000912 _rtpAudioProc(NULL),
xians@google.com22963ab2011-08-03 12:40:23 +0000913 _rxAudioProcessingModulePtr(NULL),
xians@google.com22963ab2011-08-03 12:40:23 +0000914 _rxVadObserverPtr(NULL),
915 _oldVadDecision(-1),
916 _sendFrameType(0),
niklase@google.com470e71d2011-07-07 08:21:25 +0000917 _rtpObserverPtr(NULL),
918 _rtcpObserverPtr(NULL),
xians@google.com22963ab2011-08-03 12:40:23 +0000919 _outputIsOnHold(false),
920 _externalPlayout(false),
roosa@google.com1b60ceb2012-12-12 23:00:29 +0000921 _externalMixing(false),
xians@google.com22963ab2011-08-03 12:40:23 +0000922 _inputIsOnHold(false),
923 _playing(false),
924 _sending(false),
925 _receiving(false),
926 _mixFileWithMicrophone(false),
927 _rtpObserver(false),
928 _rtcpObserver(false),
niklase@google.com470e71d2011-07-07 08:21:25 +0000929 _mute(false),
930 _panLeft(1.0f),
931 _panRight(1.0f),
932 _outputGain(1.0f),
xians@google.com22963ab2011-08-03 12:40:23 +0000933 _encrypting(false),
934 _decrypting(false),
niklase@google.com470e71d2011-07-07 08:21:25 +0000935 _playOutbandDtmfEvent(false),
936 _playInbandDtmfEvent(false),
niklase@google.com470e71d2011-07-07 08:21:25 +0000937 _extraPayloadType(0),
938 _insertExtraRTPPacket(false),
939 _extraMarkerBit(false),
940 _lastLocalTimeStamp(0),
roosa@google.com0870f022012-12-12 21:31:41 +0000941 _lastRemoteTimeStamp(0),
niklase@google.com470e71d2011-07-07 08:21:25 +0000942 _lastPayloadType(0),
xians@google.com22963ab2011-08-03 12:40:23 +0000943 _includeAudioLevelIndication(false),
niklase@google.com470e71d2011-07-07 08:21:25 +0000944 _rtpPacketTimedOut(false),
945 _rtpPacketTimeOutIsEnabled(false),
946 _rtpTimeOutSeconds(0),
947 _connectionObserver(false),
948 _connectionObserverPtr(NULL),
949 _countAliveDetections(0),
950 _countDeadDetections(0),
951 _outputSpeechType(AudioFrame::kNormalSpeech),
pwestin@webrtc.org1de01352013-04-11 20:23:35 +0000952 _average_jitter_buffer_delay_us(0),
niklase@google.com470e71d2011-07-07 08:21:25 +0000953 _previousTimestamp(0),
954 _recPacketDelayMs(20),
955 _RxVadDetection(false),
956 _rxApmIsEnabled(false),
957 _rxAgcIsEnabled(false),
xians@google.com22963ab2011-08-03 12:40:23 +0000958 _rxNsIsEnabled(false)
niklase@google.com470e71d2011-07-07 08:21:25 +0000959{
960 WEBRTC_TRACE(kTraceMemory, kTraceVoice, VoEId(_instanceId,_channelId),
961 "Channel::Channel() - ctor");
962 _inbandDtmfQueue.ResetDtmf();
963 _inbandDtmfGenerator.Init();
964 _outputAudioLevel.Clear();
965
pwestin@webrtc.org2853dde2012-05-11 11:08:54 +0000966 RtpRtcp::Configuration configuration;
967 configuration.id = VoEModuleId(instanceId, channelId);
968 configuration.audio = true;
969 configuration.incoming_data = this;
970 configuration.incoming_messages = this;
971 configuration.outgoing_transport = this;
972 configuration.rtcp_feedback = this;
973 configuration.audio_messages = this;
974
975 _rtpRtcpModule.reset(RtpRtcp::CreateRtpRtcp(configuration));
976
niklase@google.com470e71d2011-07-07 08:21:25 +0000977 // Create far end AudioProcessing Module
978 _rxAudioProcessingModulePtr = AudioProcessing::Create(
979 VoEModuleId(instanceId, channelId));
980}
981
982Channel::~Channel()
983{
984 WEBRTC_TRACE(kTraceMemory, kTraceVoice, VoEId(_instanceId,_channelId),
985 "Channel::~Channel() - dtor");
986
987 if (_outputExternalMedia)
988 {
989 DeRegisterExternalMediaProcessing(kPlaybackPerChannel);
990 }
991 if (_inputExternalMedia)
992 {
993 DeRegisterExternalMediaProcessing(kRecordingPerChannel);
994 }
995 StopSend();
niklase@google.com470e71d2011-07-07 08:21:25 +0000996 StopPlayout();
997
998 {
mflodman@webrtc.org9a065d12012-03-07 08:12:21 +0000999 CriticalSectionScoped cs(&_fileCritSect);
niklase@google.com470e71d2011-07-07 08:21:25 +00001000 if (_inputFilePlayerPtr)
1001 {
1002 _inputFilePlayerPtr->RegisterModuleFileCallback(NULL);
1003 _inputFilePlayerPtr->StopPlayingFile();
1004 FilePlayer::DestroyFilePlayer(_inputFilePlayerPtr);
1005 _inputFilePlayerPtr = NULL;
1006 }
1007 if (_outputFilePlayerPtr)
1008 {
1009 _outputFilePlayerPtr->RegisterModuleFileCallback(NULL);
1010 _outputFilePlayerPtr->StopPlayingFile();
1011 FilePlayer::DestroyFilePlayer(_outputFilePlayerPtr);
1012 _outputFilePlayerPtr = NULL;
1013 }
1014 if (_outputFileRecorderPtr)
1015 {
1016 _outputFileRecorderPtr->RegisterModuleFileCallback(NULL);
1017 _outputFileRecorderPtr->StopRecording();
1018 FileRecorder::DestroyFileRecorder(_outputFileRecorderPtr);
1019 _outputFileRecorderPtr = NULL;
1020 }
1021 }
1022
1023 // The order to safely shutdown modules in a channel is:
1024 // 1. De-register callbacks in modules
1025 // 2. De-register modules in process thread
1026 // 3. Destroy modules
niklase@google.com470e71d2011-07-07 08:21:25 +00001027 if (_audioCodingModule.RegisterTransportCallback(NULL) == -1)
1028 {
1029 WEBRTC_TRACE(kTraceWarning, kTraceVoice,
1030 VoEId(_instanceId,_channelId),
1031 "~Channel() failed to de-register transport callback"
1032 " (Audio coding module)");
1033 }
1034 if (_audioCodingModule.RegisterVADCallback(NULL) == -1)
1035 {
1036 WEBRTC_TRACE(kTraceWarning, kTraceVoice,
1037 VoEId(_instanceId,_channelId),
1038 "~Channel() failed to de-register VAD callback"
1039 " (Audio coding module)");
1040 }
niklase@google.com470e71d2011-07-07 08:21:25 +00001041 // De-register modules in process thread
pwestin@webrtc.org2853dde2012-05-11 11:08:54 +00001042 if (_moduleProcessThreadPtr->DeRegisterModule(_rtpRtcpModule.get()) == -1)
niklase@google.com470e71d2011-07-07 08:21:25 +00001043 {
1044 WEBRTC_TRACE(kTraceInfo, kTraceVoice,
1045 VoEId(_instanceId,_channelId),
1046 "~Channel() failed to deregister RTP/RTCP module");
1047 }
1048
1049 // Destroy modules
niklase@google.com470e71d2011-07-07 08:21:25 +00001050 AudioCodingModule::Destroy(&_audioCodingModule);
niklase@google.com470e71d2011-07-07 08:21:25 +00001051 if (_rxAudioProcessingModulePtr != NULL)
1052 {
1053 AudioProcessing::Destroy(_rxAudioProcessingModulePtr); // far end APM
1054 _rxAudioProcessingModulePtr = NULL;
1055 }
1056
1057 // End of modules shutdown
1058
1059 // Delete other objects
1060 RtpDump::DestroyRtpDump(&_rtpDumpIn);
1061 RtpDump::DestroyRtpDump(&_rtpDumpOut);
1062 delete [] _encryptionRTPBufferPtr;
1063 delete [] _decryptionRTPBufferPtr;
1064 delete [] _encryptionRTCPBufferPtr;
1065 delete [] _decryptionRTCPBufferPtr;
1066 delete &_callbackCritSect;
niklase@google.com470e71d2011-07-07 08:21:25 +00001067 delete &_fileCritSect;
1068}
1069
pbos@webrtc.org6141e132013-04-09 10:09:10 +00001070int32_t
niklase@google.com470e71d2011-07-07 08:21:25 +00001071Channel::Init()
1072{
1073 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId,_channelId),
1074 "Channel::Init()");
1075
1076 // --- Initial sanity
1077
1078 if ((_engineStatisticsPtr == NULL) ||
1079 (_moduleProcessThreadPtr == NULL))
1080 {
1081 WEBRTC_TRACE(kTraceError, kTraceVoice,
1082 VoEId(_instanceId,_channelId),
1083 "Channel::Init() must call SetEngineInformation() first");
1084 return -1;
1085 }
1086
1087 // --- Add modules to process thread (for periodic schedulation)
1088
1089 const bool processThreadFail =
pwestin@webrtc.org2853dde2012-05-11 11:08:54 +00001090 ((_moduleProcessThreadPtr->RegisterModule(_rtpRtcpModule.get()) != 0) ||
niklase@google.com470e71d2011-07-07 08:21:25 +00001091 false);
niklase@google.com470e71d2011-07-07 08:21:25 +00001092 if (processThreadFail)
1093 {
1094 _engineStatisticsPtr->SetLastError(
1095 VE_CANNOT_INIT_CHANNEL, kTraceError,
1096 "Channel::Init() modules not registered");
1097 return -1;
1098 }
pwestin@webrtc.orgc450a192012-01-04 15:00:12 +00001099 // --- ACM initialization
niklase@google.com470e71d2011-07-07 08:21:25 +00001100
1101 if ((_audioCodingModule.InitializeReceiver() == -1) ||
1102#ifdef WEBRTC_CODEC_AVT
1103 // out-of-band Dtmf tones are played out by default
1104 (_audioCodingModule.SetDtmfPlayoutStatus(true) == -1) ||
1105#endif
niklase@google.com470e71d2011-07-07 08:21:25 +00001106 (_audioCodingModule.InitializeSender() == -1))
1107 {
1108 _engineStatisticsPtr->SetLastError(
1109 VE_AUDIO_CODING_MODULE_ERROR, kTraceError,
1110 "Channel::Init() unable to initialize the ACM - 1");
1111 return -1;
1112 }
1113
1114 // --- RTP/RTCP module initialization
1115
1116 // Ensure that RTCP is enabled by default for the created channel.
1117 // Note that, the module will keep generating RTCP until it is explicitly
1118 // disabled by the user.
1119 // After StopListen (when no sockets exists), RTCP packets will no longer
1120 // be transmitted since the Transport object will then be invalid.
1121
1122 const bool rtpRtcpFail =
turaj@webrtc.orgb7edd062013-03-12 22:27:27 +00001123 ((_rtpRtcpModule->SetTelephoneEventForwardToDecoder(true) == -1) ||
niklase@google.com470e71d2011-07-07 08:21:25 +00001124 // RTCP is enabled by default
pwestin@webrtc.org2853dde2012-05-11 11:08:54 +00001125 (_rtpRtcpModule->SetRTCPStatus(kRtcpCompound) == -1));
niklase@google.com470e71d2011-07-07 08:21:25 +00001126 if (rtpRtcpFail)
1127 {
1128 _engineStatisticsPtr->SetLastError(
1129 VE_RTP_RTCP_MODULE_ERROR, kTraceError,
1130 "Channel::Init() RTP/RTCP module not initialized");
1131 return -1;
1132 }
1133
1134 // --- Register all permanent callbacks
niklase@google.com470e71d2011-07-07 08:21:25 +00001135 const bool fail =
niklase@google.com470e71d2011-07-07 08:21:25 +00001136 (_audioCodingModule.RegisterTransportCallback(this) == -1) ||
1137 (_audioCodingModule.RegisterVADCallback(this) == -1);
1138
1139 if (fail)
1140 {
1141 _engineStatisticsPtr->SetLastError(
1142 VE_CANNOT_INIT_CHANNEL, kTraceError,
1143 "Channel::Init() callbacks not registered");
1144 return -1;
1145 }
1146
1147 // --- Register all supported codecs to the receiving side of the
1148 // RTP/RTCP module
1149
1150 CodecInst codec;
pbos@webrtc.org6141e132013-04-09 10:09:10 +00001151 const uint8_t nSupportedCodecs = AudioCodingModule::NumberOfCodecs();
niklase@google.com470e71d2011-07-07 08:21:25 +00001152
1153 for (int idx = 0; idx < nSupportedCodecs; idx++)
1154 {
1155 // Open up the RTP/RTCP receiver for all supported codecs
tina.legrand@webrtc.org7a7a0082013-02-21 10:27:48 +00001156 if ((_audioCodingModule.Codec(idx, &codec) == -1) ||
pwestin@webrtc.org2853dde2012-05-11 11:08:54 +00001157 (_rtpRtcpModule->RegisterReceivePayload(codec) == -1))
niklase@google.com470e71d2011-07-07 08:21:25 +00001158 {
1159 WEBRTC_TRACE(kTraceWarning, kTraceVoice,
1160 VoEId(_instanceId,_channelId),
1161 "Channel::Init() unable to register %s (%d/%d/%d/%d) "
1162 "to RTP/RTCP receiver",
1163 codec.plname, codec.pltype, codec.plfreq,
1164 codec.channels, codec.rate);
1165 }
1166 else
1167 {
1168 WEBRTC_TRACE(kTraceInfo, kTraceVoice,
1169 VoEId(_instanceId,_channelId),
1170 "Channel::Init() %s (%d/%d/%d/%d) has been added to "
1171 "the RTP/RTCP receiver",
1172 codec.plname, codec.pltype, codec.plfreq,
1173 codec.channels, codec.rate);
1174 }
1175
1176 // Ensure that PCMU is used as default codec on the sending side
tina.legrand@webrtc.org45175852012-06-01 09:27:35 +00001177 if (!STR_CASE_CMP(codec.plname, "PCMU") && (codec.channels == 1))
niklase@google.com470e71d2011-07-07 08:21:25 +00001178 {
1179 SetSendCodec(codec);
1180 }
1181
1182 // Register default PT for outband 'telephone-event'
1183 if (!STR_CASE_CMP(codec.plname, "telephone-event"))
1184 {
pwestin@webrtc.org2853dde2012-05-11 11:08:54 +00001185 if ((_rtpRtcpModule->RegisterSendPayload(codec) == -1) ||
niklase@google.com470e71d2011-07-07 08:21:25 +00001186 (_audioCodingModule.RegisterReceiveCodec(codec) == -1))
1187 {
1188 WEBRTC_TRACE(kTraceWarning, kTraceVoice,
1189 VoEId(_instanceId,_channelId),
1190 "Channel::Init() failed to register outband "
1191 "'telephone-event' (%d/%d) correctly",
1192 codec.pltype, codec.plfreq);
1193 }
1194 }
1195
1196 if (!STR_CASE_CMP(codec.plname, "CN"))
1197 {
1198 if ((_audioCodingModule.RegisterSendCodec(codec) == -1) ||
1199 (_audioCodingModule.RegisterReceiveCodec(codec) == -1) ||
pwestin@webrtc.org2853dde2012-05-11 11:08:54 +00001200 (_rtpRtcpModule->RegisterSendPayload(codec) == -1))
niklase@google.com470e71d2011-07-07 08:21:25 +00001201 {
1202 WEBRTC_TRACE(kTraceWarning, kTraceVoice,
1203 VoEId(_instanceId,_channelId),
1204 "Channel::Init() failed to register CN (%d/%d) "
1205 "correctly - 1",
1206 codec.pltype, codec.plfreq);
1207 }
1208 }
1209#ifdef WEBRTC_CODEC_RED
1210 // Register RED to the receiving side of the ACM.
1211 // We will not receive an OnInitializeDecoder() callback for RED.
1212 if (!STR_CASE_CMP(codec.plname, "RED"))
1213 {
1214 if (_audioCodingModule.RegisterReceiveCodec(codec) == -1)
1215 {
1216 WEBRTC_TRACE(kTraceWarning, kTraceVoice,
1217 VoEId(_instanceId,_channelId),
1218 "Channel::Init() failed to register RED (%d/%d) "
1219 "correctly",
1220 codec.pltype, codec.plfreq);
1221 }
1222 }
1223#endif
1224 }
pwestin@webrtc.org684f0572013-03-13 23:20:57 +00001225
niklase@google.com470e71d2011-07-07 08:21:25 +00001226 // Initialize the far end AP module
1227 // Using 8 kHz as initial Fs, the same as in transmission. Might be
1228 // changed at the first receiving audio.
1229 if (_rxAudioProcessingModulePtr == NULL)
1230 {
1231 _engineStatisticsPtr->SetLastError(
1232 VE_NO_MEMORY, kTraceCritical,
1233 "Channel::Init() failed to create the far-end AudioProcessing"
1234 " module");
1235 return -1;
1236 }
1237
niklase@google.com470e71d2011-07-07 08:21:25 +00001238 if (_rxAudioProcessingModulePtr->set_sample_rate_hz(8000))
1239 {
1240 _engineStatisticsPtr->SetLastError(
1241 VE_APM_ERROR, kTraceWarning,
1242 "Channel::Init() failed to set the sample rate to 8K for"
1243 " far-end AP module");
1244 }
1245
1246 if (_rxAudioProcessingModulePtr->set_num_channels(1, 1) != 0)
1247 {
1248 _engineStatisticsPtr->SetLastError(
1249 VE_SOUNDCARD_ERROR, kTraceWarning,
andrew@webrtc.org755b04a2011-11-15 16:57:56 +00001250 "Init() failed to set channels for the primary audio stream");
niklase@google.com470e71d2011-07-07 08:21:25 +00001251 }
1252
1253 if (_rxAudioProcessingModulePtr->high_pass_filter()->Enable(
1254 WEBRTC_VOICE_ENGINE_RX_HP_DEFAULT_STATE) != 0)
1255 {
1256 _engineStatisticsPtr->SetLastError(
1257 VE_APM_ERROR, kTraceWarning,
1258 "Channel::Init() failed to set the high-pass filter for"
wu@webrtc.orgfcd12b32011-09-15 20:49:50 +00001259 " far-end AP module");
niklase@google.com470e71d2011-07-07 08:21:25 +00001260 }
1261
1262 if (_rxAudioProcessingModulePtr->noise_suppression()->set_level(
1263 (NoiseSuppression::Level)WEBRTC_VOICE_ENGINE_RX_NS_DEFAULT_MODE) != 0)
1264 {
1265 _engineStatisticsPtr->SetLastError(
1266 VE_APM_ERROR, kTraceWarning,
1267 "Init() failed to set noise reduction level for far-end"
wu@webrtc.orgfcd12b32011-09-15 20:49:50 +00001268 " AP module");
niklase@google.com470e71d2011-07-07 08:21:25 +00001269 }
1270 if (_rxAudioProcessingModulePtr->noise_suppression()->Enable(
1271 WEBRTC_VOICE_ENGINE_RX_NS_DEFAULT_STATE) != 0)
1272 {
1273 _engineStatisticsPtr->SetLastError(
1274 VE_APM_ERROR, kTraceWarning,
1275 "Init() failed to set noise reduction state for far-end"
wu@webrtc.orgfcd12b32011-09-15 20:49:50 +00001276 " AP module");
niklase@google.com470e71d2011-07-07 08:21:25 +00001277 }
1278
1279 if (_rxAudioProcessingModulePtr->gain_control()->set_mode(
1280 (GainControl::Mode)WEBRTC_VOICE_ENGINE_RX_AGC_DEFAULT_MODE) != 0)
1281 {
1282 _engineStatisticsPtr->SetLastError(
1283 VE_APM_ERROR, kTraceWarning,
1284 "Init() failed to set AGC mode for far-end AP module");
1285 }
1286 if (_rxAudioProcessingModulePtr->gain_control()->Enable(
1287 WEBRTC_VOICE_ENGINE_RX_AGC_DEFAULT_STATE) != 0)
1288 {
1289 _engineStatisticsPtr->SetLastError(
1290 VE_APM_ERROR, kTraceWarning,
1291 "Init() failed to set AGC state for far-end AP module");
1292 }
1293
1294 return 0;
1295}
1296
pbos@webrtc.org6141e132013-04-09 10:09:10 +00001297int32_t
niklase@google.com470e71d2011-07-07 08:21:25 +00001298Channel::SetEngineInformation(Statistics& engineStatistics,
1299 OutputMixer& outputMixer,
1300 voe::TransmitMixer& transmitMixer,
1301 ProcessThread& moduleProcessThread,
1302 AudioDeviceModule& audioDeviceModule,
1303 VoiceEngineObserver* voiceEngineObserver,
1304 CriticalSectionWrapper* callbackCritSect)
1305{
1306 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId,_channelId),
1307 "Channel::SetEngineInformation()");
1308 _engineStatisticsPtr = &engineStatistics;
1309 _outputMixerPtr = &outputMixer;
1310 _transmitMixerPtr = &transmitMixer,
1311 _moduleProcessThreadPtr = &moduleProcessThread;
1312 _audioDeviceModulePtr = &audioDeviceModule;
1313 _voiceEngineObserverPtr = voiceEngineObserver;
1314 _callbackCritSectPtr = callbackCritSect;
1315 return 0;
1316}
1317
pbos@webrtc.org6141e132013-04-09 10:09:10 +00001318int32_t
niklase@google.com470e71d2011-07-07 08:21:25 +00001319Channel::UpdateLocalTimeStamp()
1320{
1321
andrew@webrtc.org63a50982012-05-02 23:56:37 +00001322 _timeStamp += _audioFrame.samples_per_channel_;
niklase@google.com470e71d2011-07-07 08:21:25 +00001323 return 0;
1324}
1325
pbos@webrtc.org6141e132013-04-09 10:09:10 +00001326int32_t
niklase@google.com470e71d2011-07-07 08:21:25 +00001327Channel::StartPlayout()
1328{
1329 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId,_channelId),
1330 "Channel::StartPlayout()");
1331 if (_playing)
1332 {
1333 return 0;
1334 }
roosa@google.com1b60ceb2012-12-12 23:00:29 +00001335
1336 if (!_externalMixing) {
1337 // Add participant as candidates for mixing.
1338 if (_outputMixerPtr->SetMixabilityStatus(*this, true) != 0)
1339 {
1340 _engineStatisticsPtr->SetLastError(
1341 VE_AUDIO_CONF_MIX_MODULE_ERROR, kTraceError,
1342 "StartPlayout() failed to add participant to mixer");
1343 return -1;
1344 }
niklase@google.com470e71d2011-07-07 08:21:25 +00001345 }
1346
1347 _playing = true;
braveyao@webrtc.orgab129902012-06-04 03:26:39 +00001348
1349 if (RegisterFilePlayingToMixer() != 0)
1350 return -1;
1351
niklase@google.com470e71d2011-07-07 08:21:25 +00001352 return 0;
1353}
1354
pbos@webrtc.org6141e132013-04-09 10:09:10 +00001355int32_t
niklase@google.com470e71d2011-07-07 08:21:25 +00001356Channel::StopPlayout()
1357{
1358 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId,_channelId),
1359 "Channel::StopPlayout()");
1360 if (!_playing)
1361 {
1362 return 0;
1363 }
roosa@google.com1b60ceb2012-12-12 23:00:29 +00001364
1365 if (!_externalMixing) {
1366 // Remove participant as candidates for mixing
1367 if (_outputMixerPtr->SetMixabilityStatus(*this, false) != 0)
1368 {
1369 _engineStatisticsPtr->SetLastError(
1370 VE_AUDIO_CONF_MIX_MODULE_ERROR, kTraceError,
1371 "StopPlayout() failed to remove participant from mixer");
1372 return -1;
1373 }
niklase@google.com470e71d2011-07-07 08:21:25 +00001374 }
1375
1376 _playing = false;
1377 _outputAudioLevel.Clear();
1378
1379 return 0;
1380}
1381
pbos@webrtc.org6141e132013-04-09 10:09:10 +00001382int32_t
niklase@google.com470e71d2011-07-07 08:21:25 +00001383Channel::StartSend()
1384{
1385 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId,_channelId),
1386 "Channel::StartSend()");
niklase@google.com470e71d2011-07-07 08:21:25 +00001387 {
xians@webrtc.orge07247a2011-11-28 16:31:28 +00001388 // A lock is needed because |_sending| can be accessed or modified by
1389 // another thread at the same time.
mflodman@webrtc.org9a065d12012-03-07 08:12:21 +00001390 CriticalSectionScoped cs(&_callbackCritSect);
xians@webrtc.orge07247a2011-11-28 16:31:28 +00001391
1392 if (_sending)
1393 {
1394 return 0;
1395 }
1396 _sending = true;
niklase@google.com470e71d2011-07-07 08:21:25 +00001397 }
xians@webrtc.orge07247a2011-11-28 16:31:28 +00001398
pwestin@webrtc.org2853dde2012-05-11 11:08:54 +00001399 if (_rtpRtcpModule->SetSendingStatus(true) != 0)
niklase@google.com470e71d2011-07-07 08:21:25 +00001400 {
1401 _engineStatisticsPtr->SetLastError(
1402 VE_RTP_RTCP_MODULE_ERROR, kTraceError,
1403 "StartSend() RTP/RTCP failed to start sending");
mflodman@webrtc.org9a065d12012-03-07 08:12:21 +00001404 CriticalSectionScoped cs(&_callbackCritSect);
xians@webrtc.orge07247a2011-11-28 16:31:28 +00001405 _sending = false;
niklase@google.com470e71d2011-07-07 08:21:25 +00001406 return -1;
1407 }
xians@webrtc.orge07247a2011-11-28 16:31:28 +00001408
niklase@google.com470e71d2011-07-07 08:21:25 +00001409 return 0;
1410}
1411
pbos@webrtc.org6141e132013-04-09 10:09:10 +00001412int32_t
niklase@google.com470e71d2011-07-07 08:21:25 +00001413Channel::StopSend()
1414{
1415 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId,_channelId),
1416 "Channel::StopSend()");
niklase@google.com470e71d2011-07-07 08:21:25 +00001417 {
xians@webrtc.orge07247a2011-11-28 16:31:28 +00001418 // A lock is needed because |_sending| can be accessed or modified by
1419 // another thread at the same time.
mflodman@webrtc.org9a065d12012-03-07 08:12:21 +00001420 CriticalSectionScoped cs(&_callbackCritSect);
xians@webrtc.orge07247a2011-11-28 16:31:28 +00001421
1422 if (!_sending)
1423 {
1424 return 0;
1425 }
1426 _sending = false;
niklase@google.com470e71d2011-07-07 08:21:25 +00001427 }
xians@webrtc.orge07247a2011-11-28 16:31:28 +00001428
niklase@google.com470e71d2011-07-07 08:21:25 +00001429 // Reset sending SSRC and sequence number and triggers direct transmission
1430 // of RTCP BYE
pwestin@webrtc.org2853dde2012-05-11 11:08:54 +00001431 if (_rtpRtcpModule->SetSendingStatus(false) == -1 ||
1432 _rtpRtcpModule->ResetSendDataCountersRTP() == -1)
niklase@google.com470e71d2011-07-07 08:21:25 +00001433 {
1434 _engineStatisticsPtr->SetLastError(
1435 VE_RTP_RTCP_MODULE_ERROR, kTraceWarning,
1436 "StartSend() RTP/RTCP failed to stop sending");
1437 }
1438
niklase@google.com470e71d2011-07-07 08:21:25 +00001439 return 0;
1440}
1441
pbos@webrtc.org6141e132013-04-09 10:09:10 +00001442int32_t
niklase@google.com470e71d2011-07-07 08:21:25 +00001443Channel::StartReceiving()
1444{
1445 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId,_channelId),
1446 "Channel::StartReceiving()");
1447 if (_receiving)
1448 {
1449 return 0;
1450 }
niklase@google.com470e71d2011-07-07 08:21:25 +00001451 _receiving = true;
1452 _numberOfDiscardedPackets = 0;
1453 return 0;
1454}
1455
pbos@webrtc.org6141e132013-04-09 10:09:10 +00001456int32_t
niklase@google.com470e71d2011-07-07 08:21:25 +00001457Channel::StopReceiving()
1458{
1459 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId,_channelId),
1460 "Channel::StopReceiving()");
1461 if (!_receiving)
1462 {
1463 return 0;
1464 }
pwestin@webrtc.org684f0572013-03-13 23:20:57 +00001465
henrika@webrtc.orgaf71f0e2011-12-05 07:02:22 +00001466 // Recover DTMF detection status.
pbos@webrtc.org6141e132013-04-09 10:09:10 +00001467 int32_t ret = _rtpRtcpModule->SetTelephoneEventForwardToDecoder(true);
henrika@webrtc.orgaf71f0e2011-12-05 07:02:22 +00001468 if (ret != 0) {
1469 _engineStatisticsPtr->SetLastError(
1470 VE_INVALID_OPERATION, kTraceWarning,
1471 "StopReceiving() failed to restore telephone-event status.");
1472 }
niklase@google.com470e71d2011-07-07 08:21:25 +00001473 RegisterReceiveCodecsToRTPModule();
1474 _receiving = false;
1475 return 0;
1476}
1477
pbos@webrtc.org6141e132013-04-09 10:09:10 +00001478int32_t
niklase@google.com470e71d2011-07-07 08:21:25 +00001479Channel::SetNetEQPlayoutMode(NetEqModes mode)
1480{
1481 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId,_channelId),
1482 "Channel::SetNetEQPlayoutMode()");
1483 AudioPlayoutMode playoutMode(voice);
1484 switch (mode)
1485 {
1486 case kNetEqDefault:
1487 playoutMode = voice;
1488 break;
1489 case kNetEqStreaming:
1490 playoutMode = streaming;
1491 break;
1492 case kNetEqFax:
1493 playoutMode = fax;
1494 break;
roosa@google.comb7186192012-12-12 21:59:14 +00001495 case kNetEqOff:
1496 playoutMode = off;
1497 break;
niklase@google.com470e71d2011-07-07 08:21:25 +00001498 }
1499 if (_audioCodingModule.SetPlayoutMode(playoutMode) != 0)
1500 {
1501 _engineStatisticsPtr->SetLastError(
1502 VE_AUDIO_CODING_MODULE_ERROR, kTraceError,
1503 "SetNetEQPlayoutMode() failed to set playout mode");
1504 return -1;
1505 }
1506 return 0;
1507}
1508
pbos@webrtc.org6141e132013-04-09 10:09:10 +00001509int32_t
niklase@google.com470e71d2011-07-07 08:21:25 +00001510Channel::GetNetEQPlayoutMode(NetEqModes& mode)
1511{
1512 const AudioPlayoutMode playoutMode = _audioCodingModule.PlayoutMode();
1513 switch (playoutMode)
1514 {
1515 case voice:
1516 mode = kNetEqDefault;
1517 break;
1518 case streaming:
1519 mode = kNetEqStreaming;
1520 break;
1521 case fax:
1522 mode = kNetEqFax;
1523 break;
roosa@google.comb7186192012-12-12 21:59:14 +00001524 case off:
1525 mode = kNetEqOff;
niklase@google.com470e71d2011-07-07 08:21:25 +00001526 }
1527 WEBRTC_TRACE(kTraceStateInfo, kTraceVoice,
1528 VoEId(_instanceId,_channelId),
1529 "Channel::GetNetEQPlayoutMode() => mode=%u", mode);
1530 return 0;
1531}
1532
pbos@webrtc.org6141e132013-04-09 10:09:10 +00001533int32_t
niklase@google.com470e71d2011-07-07 08:21:25 +00001534Channel::SetOnHoldStatus(bool enable, OnHoldModes mode)
1535{
1536 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId,_channelId),
1537 "Channel::SetOnHoldStatus()");
1538 if (mode == kHoldSendAndPlay)
1539 {
1540 _outputIsOnHold = enable;
1541 _inputIsOnHold = enable;
1542 }
1543 else if (mode == kHoldPlayOnly)
1544 {
1545 _outputIsOnHold = enable;
1546 }
1547 if (mode == kHoldSendOnly)
1548 {
1549 _inputIsOnHold = enable;
1550 }
1551 return 0;
1552}
1553
pbos@webrtc.org6141e132013-04-09 10:09:10 +00001554int32_t
niklase@google.com470e71d2011-07-07 08:21:25 +00001555Channel::GetOnHoldStatus(bool& enabled, OnHoldModes& mode)
1556{
1557 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId,_channelId),
1558 "Channel::GetOnHoldStatus()");
1559 enabled = (_outputIsOnHold || _inputIsOnHold);
1560 if (_outputIsOnHold && _inputIsOnHold)
1561 {
1562 mode = kHoldSendAndPlay;
1563 }
1564 else if (_outputIsOnHold && !_inputIsOnHold)
1565 {
1566 mode = kHoldPlayOnly;
1567 }
1568 else if (!_outputIsOnHold && _inputIsOnHold)
1569 {
1570 mode = kHoldSendOnly;
1571 }
1572 WEBRTC_TRACE(kTraceStateInfo, kTraceVoice, VoEId(_instanceId,_channelId),
1573 "Channel::GetOnHoldStatus() => enabled=%d, mode=%d",
1574 enabled, mode);
1575 return 0;
1576}
1577
pbos@webrtc.org6141e132013-04-09 10:09:10 +00001578int32_t
niklase@google.com470e71d2011-07-07 08:21:25 +00001579Channel::RegisterVoiceEngineObserver(VoiceEngineObserver& observer)
1580{
1581 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId,_channelId),
1582 "Channel::RegisterVoiceEngineObserver()");
mflodman@webrtc.org9a065d12012-03-07 08:12:21 +00001583 CriticalSectionScoped cs(&_callbackCritSect);
niklase@google.com470e71d2011-07-07 08:21:25 +00001584
1585 if (_voiceEngineObserverPtr)
1586 {
1587 _engineStatisticsPtr->SetLastError(
1588 VE_INVALID_OPERATION, kTraceError,
1589 "RegisterVoiceEngineObserver() observer already enabled");
1590 return -1;
1591 }
1592 _voiceEngineObserverPtr = &observer;
1593 return 0;
1594}
1595
pbos@webrtc.org6141e132013-04-09 10:09:10 +00001596int32_t
niklase@google.com470e71d2011-07-07 08:21:25 +00001597Channel::DeRegisterVoiceEngineObserver()
1598{
1599 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId,_channelId),
1600 "Channel::DeRegisterVoiceEngineObserver()");
mflodman@webrtc.org9a065d12012-03-07 08:12:21 +00001601 CriticalSectionScoped cs(&_callbackCritSect);
niklase@google.com470e71d2011-07-07 08:21:25 +00001602
1603 if (!_voiceEngineObserverPtr)
1604 {
1605 _engineStatisticsPtr->SetLastError(
1606 VE_INVALID_OPERATION, kTraceWarning,
1607 "DeRegisterVoiceEngineObserver() observer already disabled");
1608 return 0;
1609 }
1610 _voiceEngineObserverPtr = NULL;
1611 return 0;
1612}
1613
pbos@webrtc.org6141e132013-04-09 10:09:10 +00001614int32_t
niklase@google.com470e71d2011-07-07 08:21:25 +00001615Channel::GetSendCodec(CodecInst& codec)
1616{
tina.legrand@webrtc.org7a7a0082013-02-21 10:27:48 +00001617 return (_audioCodingModule.SendCodec(&codec));
niklase@google.com470e71d2011-07-07 08:21:25 +00001618}
1619
pbos@webrtc.org6141e132013-04-09 10:09:10 +00001620int32_t
niklase@google.com470e71d2011-07-07 08:21:25 +00001621Channel::GetRecCodec(CodecInst& codec)
1622{
tina.legrand@webrtc.org7a7a0082013-02-21 10:27:48 +00001623 return (_audioCodingModule.ReceiveCodec(&codec));
niklase@google.com470e71d2011-07-07 08:21:25 +00001624}
1625
pbos@webrtc.org6141e132013-04-09 10:09:10 +00001626int32_t
niklase@google.com470e71d2011-07-07 08:21:25 +00001627Channel::SetSendCodec(const CodecInst& codec)
1628{
1629 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId,_channelId),
1630 "Channel::SetSendCodec()");
1631
1632 if (_audioCodingModule.RegisterSendCodec(codec) != 0)
1633 {
1634 WEBRTC_TRACE(kTraceError, kTraceVoice, VoEId(_instanceId,_channelId),
1635 "SetSendCodec() failed to register codec to ACM");
1636 return -1;
1637 }
1638
pwestin@webrtc.org2853dde2012-05-11 11:08:54 +00001639 if (_rtpRtcpModule->RegisterSendPayload(codec) != 0)
niklase@google.com470e71d2011-07-07 08:21:25 +00001640 {
pwestin@webrtc.org2853dde2012-05-11 11:08:54 +00001641 _rtpRtcpModule->DeRegisterSendPayload(codec.pltype);
1642 if (_rtpRtcpModule->RegisterSendPayload(codec) != 0)
niklase@google.com470e71d2011-07-07 08:21:25 +00001643 {
1644 WEBRTC_TRACE(
1645 kTraceError, kTraceVoice, VoEId(_instanceId,_channelId),
1646 "SetSendCodec() failed to register codec to"
1647 " RTP/RTCP module");
1648 return -1;
1649 }
1650 }
1651
pwestin@webrtc.org2853dde2012-05-11 11:08:54 +00001652 if (_rtpRtcpModule->SetAudioPacketSize(codec.pacsize) != 0)
niklase@google.com470e71d2011-07-07 08:21:25 +00001653 {
1654 WEBRTC_TRACE(kTraceError, kTraceVoice, VoEId(_instanceId,_channelId),
1655 "SetSendCodec() failed to set audio packet size");
1656 return -1;
1657 }
1658
1659 return 0;
1660}
1661
pbos@webrtc.org6141e132013-04-09 10:09:10 +00001662int32_t
niklase@google.com470e71d2011-07-07 08:21:25 +00001663Channel::SetVADStatus(bool enableVAD, ACMVADMode mode, bool disableDTX)
1664{
1665 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId,_channelId),
1666 "Channel::SetVADStatus(mode=%d)", mode);
1667 // To disable VAD, DTX must be disabled too
1668 disableDTX = ((enableVAD == false) ? true : disableDTX);
1669 if (_audioCodingModule.SetVAD(!disableDTX, enableVAD, mode) != 0)
1670 {
1671 _engineStatisticsPtr->SetLastError(
1672 VE_AUDIO_CODING_MODULE_ERROR, kTraceError,
1673 "SetVADStatus() failed to set VAD");
1674 return -1;
1675 }
1676 return 0;
1677}
1678
pbos@webrtc.org6141e132013-04-09 10:09:10 +00001679int32_t
niklase@google.com470e71d2011-07-07 08:21:25 +00001680Channel::GetVADStatus(bool& enabledVAD, ACMVADMode& mode, bool& disabledDTX)
1681{
1682 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId,_channelId),
1683 "Channel::GetVADStatus");
tina.legrand@webrtc.org7a7a0082013-02-21 10:27:48 +00001684 if (_audioCodingModule.VAD(&disabledDTX, &enabledVAD, &mode) != 0)
niklase@google.com470e71d2011-07-07 08:21:25 +00001685 {
1686 _engineStatisticsPtr->SetLastError(
1687 VE_AUDIO_CODING_MODULE_ERROR, kTraceError,
1688 "GetVADStatus() failed to get VAD status");
1689 return -1;
1690 }
1691 disabledDTX = !disabledDTX;
1692 return 0;
1693}
1694
pbos@webrtc.org6141e132013-04-09 10:09:10 +00001695int32_t
niklase@google.com470e71d2011-07-07 08:21:25 +00001696Channel::SetRecPayloadType(const CodecInst& codec)
1697{
1698 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId,_channelId),
1699 "Channel::SetRecPayloadType()");
1700
1701 if (_playing)
1702 {
1703 _engineStatisticsPtr->SetLastError(
1704 VE_ALREADY_PLAYING, kTraceError,
1705 "SetRecPayloadType() unable to set PT while playing");
1706 return -1;
1707 }
1708 if (_receiving)
1709 {
1710 _engineStatisticsPtr->SetLastError(
1711 VE_ALREADY_LISTENING, kTraceError,
1712 "SetRecPayloadType() unable to set PT while listening");
1713 return -1;
1714 }
1715
1716 if (codec.pltype == -1)
1717 {
1718 // De-register the selected codec (RTP/RTCP module and ACM)
1719
pbos@webrtc.org6141e132013-04-09 10:09:10 +00001720 int8_t pltype(-1);
niklase@google.com470e71d2011-07-07 08:21:25 +00001721 CodecInst rxCodec = codec;
1722
1723 // Get payload type for the given codec
pwestin@webrtc.org2853dde2012-05-11 11:08:54 +00001724 _rtpRtcpModule->ReceivePayloadType(rxCodec, &pltype);
niklase@google.com470e71d2011-07-07 08:21:25 +00001725 rxCodec.pltype = pltype;
1726
pwestin@webrtc.org2853dde2012-05-11 11:08:54 +00001727 if (_rtpRtcpModule->DeRegisterReceivePayload(pltype) != 0)
niklase@google.com470e71d2011-07-07 08:21:25 +00001728 {
1729 _engineStatisticsPtr->SetLastError(
1730 VE_RTP_RTCP_MODULE_ERROR,
1731 kTraceError,
1732 "SetRecPayloadType() RTP/RTCP-module deregistration "
1733 "failed");
1734 return -1;
1735 }
1736 if (_audioCodingModule.UnregisterReceiveCodec(rxCodec.pltype) != 0)
1737 {
1738 _engineStatisticsPtr->SetLastError(
1739 VE_AUDIO_CODING_MODULE_ERROR, kTraceError,
1740 "SetRecPayloadType() ACM deregistration failed - 1");
1741 return -1;
1742 }
1743 return 0;
1744 }
1745
pwestin@webrtc.org2853dde2012-05-11 11:08:54 +00001746 if (_rtpRtcpModule->RegisterReceivePayload(codec) != 0)
niklase@google.com470e71d2011-07-07 08:21:25 +00001747 {
1748 // First attempt to register failed => de-register and try again
pwestin@webrtc.org2853dde2012-05-11 11:08:54 +00001749 _rtpRtcpModule->DeRegisterReceivePayload(codec.pltype);
1750 if (_rtpRtcpModule->RegisterReceivePayload(codec) != 0)
niklase@google.com470e71d2011-07-07 08:21:25 +00001751 {
1752 _engineStatisticsPtr->SetLastError(
1753 VE_RTP_RTCP_MODULE_ERROR, kTraceError,
1754 "SetRecPayloadType() RTP/RTCP-module registration failed");
1755 return -1;
1756 }
1757 }
1758 if (_audioCodingModule.RegisterReceiveCodec(codec) != 0)
1759 {
1760 _audioCodingModule.UnregisterReceiveCodec(codec.pltype);
1761 if (_audioCodingModule.RegisterReceiveCodec(codec) != 0)
1762 {
1763 _engineStatisticsPtr->SetLastError(
1764 VE_AUDIO_CODING_MODULE_ERROR, kTraceError,
1765 "SetRecPayloadType() ACM registration failed - 1");
1766 return -1;
1767 }
1768 }
1769 return 0;
1770}
1771
pbos@webrtc.org6141e132013-04-09 10:09:10 +00001772int32_t
niklase@google.com470e71d2011-07-07 08:21:25 +00001773Channel::GetRecPayloadType(CodecInst& codec)
1774{
1775 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId,_channelId),
1776 "Channel::GetRecPayloadType()");
pbos@webrtc.org6141e132013-04-09 10:09:10 +00001777 int8_t payloadType(-1);
pwestin@webrtc.org2853dde2012-05-11 11:08:54 +00001778 if (_rtpRtcpModule->ReceivePayloadType(codec, &payloadType) != 0)
niklase@google.com470e71d2011-07-07 08:21:25 +00001779 {
1780 _engineStatisticsPtr->SetLastError(
henrika@webrtc.org37198002012-06-18 11:00:12 +00001781 VE_RTP_RTCP_MODULE_ERROR, kTraceWarning,
niklase@google.com470e71d2011-07-07 08:21:25 +00001782 "GetRecPayloadType() failed to retrieve RX payload type");
1783 return -1;
1784 }
1785 codec.pltype = payloadType;
1786 WEBRTC_TRACE(kTraceStateInfo, kTraceVoice, VoEId(_instanceId,_channelId),
1787 "Channel::GetRecPayloadType() => pltype=%u", codec.pltype);
1788 return 0;
1789}
1790
pbos@webrtc.org6141e132013-04-09 10:09:10 +00001791int32_t
niklase@google.com470e71d2011-07-07 08:21:25 +00001792Channel::SetAMREncFormat(AmrMode mode)
1793{
1794 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId,_channelId),
1795 "Channel::SetAMREncFormat()");
1796
1797 // ACM doesn't support AMR
1798 return -1;
1799}
1800
pbos@webrtc.org6141e132013-04-09 10:09:10 +00001801int32_t
niklase@google.com470e71d2011-07-07 08:21:25 +00001802Channel::SetAMRDecFormat(AmrMode mode)
1803{
1804 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId,_channelId),
1805 "Channel::SetAMRDecFormat()");
1806
1807 // ACM doesn't support AMR
1808 return -1;
1809}
1810
pbos@webrtc.org6141e132013-04-09 10:09:10 +00001811int32_t
niklase@google.com470e71d2011-07-07 08:21:25 +00001812Channel::SetAMRWbEncFormat(AmrMode mode)
1813{
1814 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId,_channelId),
1815 "Channel::SetAMRWbEncFormat()");
1816
1817 // ACM doesn't support AMR
1818 return -1;
1819
1820}
1821
pbos@webrtc.org6141e132013-04-09 10:09:10 +00001822int32_t
niklase@google.com470e71d2011-07-07 08:21:25 +00001823Channel::SetAMRWbDecFormat(AmrMode mode)
1824{
1825 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId,_channelId),
1826 "Channel::SetAMRWbDecFormat()");
1827
1828 // ACM doesn't support AMR
1829 return -1;
1830}
1831
pbos@webrtc.org6141e132013-04-09 10:09:10 +00001832int32_t
niklase@google.com470e71d2011-07-07 08:21:25 +00001833Channel::SetSendCNPayloadType(int type, PayloadFrequencies frequency)
1834{
1835 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId,_channelId),
1836 "Channel::SetSendCNPayloadType()");
1837
1838 CodecInst codec;
pbos@webrtc.org6141e132013-04-09 10:09:10 +00001839 int32_t samplingFreqHz(-1);
tina.legrand@webrtc.org45175852012-06-01 09:27:35 +00001840 const int kMono = 1;
niklase@google.com470e71d2011-07-07 08:21:25 +00001841 if (frequency == kFreq32000Hz)
1842 samplingFreqHz = 32000;
1843 else if (frequency == kFreq16000Hz)
1844 samplingFreqHz = 16000;
1845
tina.legrand@webrtc.org7a7a0082013-02-21 10:27:48 +00001846 if (_audioCodingModule.Codec("CN", &codec, samplingFreqHz, kMono) == -1)
niklase@google.com470e71d2011-07-07 08:21:25 +00001847 {
1848 _engineStatisticsPtr->SetLastError(
1849 VE_AUDIO_CODING_MODULE_ERROR, kTraceError,
1850 "SetSendCNPayloadType() failed to retrieve default CN codec "
1851 "settings");
1852 return -1;
1853 }
1854
1855 // Modify the payload type (must be set to dynamic range)
1856 codec.pltype = type;
1857
1858 if (_audioCodingModule.RegisterSendCodec(codec) != 0)
1859 {
1860 _engineStatisticsPtr->SetLastError(
1861 VE_AUDIO_CODING_MODULE_ERROR, kTraceError,
1862 "SetSendCNPayloadType() failed to register CN to ACM");
1863 return -1;
1864 }
1865
pwestin@webrtc.org2853dde2012-05-11 11:08:54 +00001866 if (_rtpRtcpModule->RegisterSendPayload(codec) != 0)
niklase@google.com470e71d2011-07-07 08:21:25 +00001867 {
pwestin@webrtc.org2853dde2012-05-11 11:08:54 +00001868 _rtpRtcpModule->DeRegisterSendPayload(codec.pltype);
1869 if (_rtpRtcpModule->RegisterSendPayload(codec) != 0)
niklase@google.com470e71d2011-07-07 08:21:25 +00001870 {
1871 _engineStatisticsPtr->SetLastError(
1872 VE_RTP_RTCP_MODULE_ERROR, kTraceError,
1873 "SetSendCNPayloadType() failed to register CN to RTP/RTCP "
1874 "module");
1875 return -1;
1876 }
1877 }
1878 return 0;
1879}
1880
pbos@webrtc.org6141e132013-04-09 10:09:10 +00001881int32_t
niklase@google.com470e71d2011-07-07 08:21:25 +00001882Channel::SetISACInitTargetRate(int rateBps, bool useFixedFrameSize)
1883{
1884 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId,_channelId),
1885 "Channel::SetISACInitTargetRate()");
1886
1887 CodecInst sendCodec;
tina.legrand@webrtc.org7a7a0082013-02-21 10:27:48 +00001888 if (_audioCodingModule.SendCodec(&sendCodec) == -1)
niklase@google.com470e71d2011-07-07 08:21:25 +00001889 {
1890 _engineStatisticsPtr->SetLastError(
1891 VE_CODEC_ERROR, kTraceError,
1892 "SetISACInitTargetRate() failed to retrieve send codec");
1893 return -1;
1894 }
1895 if (STR_CASE_CMP(sendCodec.plname, "ISAC") != 0)
1896 {
1897 // This API is only valid if iSAC is setup to run in channel-adaptive
1898 // mode.
1899 // We do not validate the adaptive mode here. It is done later in the
1900 // ConfigISACBandwidthEstimator() API.
1901 _engineStatisticsPtr->SetLastError(
1902 VE_CODEC_ERROR, kTraceError,
1903 "SetISACInitTargetRate() send codec is not iSAC");
1904 return -1;
1905 }
1906
pbos@webrtc.org6141e132013-04-09 10:09:10 +00001907 uint8_t initFrameSizeMsec(0);
niklase@google.com470e71d2011-07-07 08:21:25 +00001908 if (16000 == sendCodec.plfreq)
1909 {
1910 // Note that 0 is a valid and corresponds to "use default
1911 if ((rateBps != 0 &&
1912 rateBps < kVoiceEngineMinIsacInitTargetRateBpsWb) ||
1913 (rateBps > kVoiceEngineMaxIsacInitTargetRateBpsWb))
1914 {
1915 _engineStatisticsPtr->SetLastError(
1916 VE_INVALID_ARGUMENT, kTraceError,
1917 "SetISACInitTargetRate() invalid target rate - 1");
1918 return -1;
1919 }
1920 // 30 or 60ms
pbos@webrtc.org6141e132013-04-09 10:09:10 +00001921 initFrameSizeMsec = (uint8_t)(sendCodec.pacsize / 16);
niklase@google.com470e71d2011-07-07 08:21:25 +00001922 }
1923 else if (32000 == sendCodec.plfreq)
1924 {
1925 if ((rateBps != 0 &&
1926 rateBps < kVoiceEngineMinIsacInitTargetRateBpsSwb) ||
1927 (rateBps > kVoiceEngineMaxIsacInitTargetRateBpsSwb))
1928 {
1929 _engineStatisticsPtr->SetLastError(
1930 VE_INVALID_ARGUMENT, kTraceError,
1931 "SetISACInitTargetRate() invalid target rate - 2");
1932 return -1;
1933 }
pbos@webrtc.org6141e132013-04-09 10:09:10 +00001934 initFrameSizeMsec = (uint8_t)(sendCodec.pacsize / 32); // 30ms
niklase@google.com470e71d2011-07-07 08:21:25 +00001935 }
1936
1937 if (_audioCodingModule.ConfigISACBandwidthEstimator(
1938 initFrameSizeMsec, rateBps, useFixedFrameSize) == -1)
1939 {
1940 _engineStatisticsPtr->SetLastError(
1941 VE_AUDIO_CODING_MODULE_ERROR, kTraceError,
1942 "SetISACInitTargetRate() iSAC BWE config failed");
1943 return -1;
1944 }
1945
1946 return 0;
1947}
1948
pbos@webrtc.org6141e132013-04-09 10:09:10 +00001949int32_t
niklase@google.com470e71d2011-07-07 08:21:25 +00001950Channel::SetISACMaxRate(int rateBps)
1951{
1952 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId,_channelId),
1953 "Channel::SetISACMaxRate()");
1954
1955 CodecInst sendCodec;
tina.legrand@webrtc.org7a7a0082013-02-21 10:27:48 +00001956 if (_audioCodingModule.SendCodec(&sendCodec) == -1)
niklase@google.com470e71d2011-07-07 08:21:25 +00001957 {
1958 _engineStatisticsPtr->SetLastError(
1959 VE_CODEC_ERROR, kTraceError,
1960 "SetISACMaxRate() failed to retrieve send codec");
1961 return -1;
1962 }
1963 if (STR_CASE_CMP(sendCodec.plname, "ISAC") != 0)
1964 {
1965 // This API is only valid if iSAC is selected as sending codec.
1966 _engineStatisticsPtr->SetLastError(
1967 VE_CODEC_ERROR, kTraceError,
1968 "SetISACMaxRate() send codec is not iSAC");
1969 return -1;
1970 }
1971 if (16000 == sendCodec.plfreq)
1972 {
1973 if ((rateBps < kVoiceEngineMinIsacMaxRateBpsWb) ||
1974 (rateBps > kVoiceEngineMaxIsacMaxRateBpsWb))
1975 {
1976 _engineStatisticsPtr->SetLastError(
1977 VE_INVALID_ARGUMENT, kTraceError,
1978 "SetISACMaxRate() invalid max rate - 1");
1979 return -1;
1980 }
1981 }
1982 else if (32000 == sendCodec.plfreq)
1983 {
1984 if ((rateBps < kVoiceEngineMinIsacMaxRateBpsSwb) ||
1985 (rateBps > kVoiceEngineMaxIsacMaxRateBpsSwb))
1986 {
1987 _engineStatisticsPtr->SetLastError(
1988 VE_INVALID_ARGUMENT, kTraceError,
1989 "SetISACMaxRate() invalid max rate - 2");
1990 return -1;
1991 }
1992 }
1993 if (_sending)
1994 {
1995 _engineStatisticsPtr->SetLastError(
1996 VE_SENDING, kTraceError,
1997 "SetISACMaxRate() unable to set max rate while sending");
1998 return -1;
1999 }
2000
2001 // Set the maximum instantaneous rate of iSAC (works for both adaptive
2002 // and non-adaptive mode)
2003 if (_audioCodingModule.SetISACMaxRate(rateBps) == -1)
2004 {
2005 _engineStatisticsPtr->SetLastError(
2006 VE_AUDIO_CODING_MODULE_ERROR, kTraceError,
2007 "SetISACMaxRate() failed to set max rate");
2008 return -1;
2009 }
2010
2011 return 0;
2012}
2013
pbos@webrtc.org6141e132013-04-09 10:09:10 +00002014int32_t
niklase@google.com470e71d2011-07-07 08:21:25 +00002015Channel::SetISACMaxPayloadSize(int sizeBytes)
2016{
2017 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId,_channelId),
2018 "Channel::SetISACMaxPayloadSize()");
2019 CodecInst sendCodec;
tina.legrand@webrtc.org7a7a0082013-02-21 10:27:48 +00002020 if (_audioCodingModule.SendCodec(&sendCodec) == -1)
niklase@google.com470e71d2011-07-07 08:21:25 +00002021 {
2022 _engineStatisticsPtr->SetLastError(
2023 VE_CODEC_ERROR, kTraceError,
2024 "SetISACMaxPayloadSize() failed to retrieve send codec");
2025 return -1;
2026 }
2027 if (STR_CASE_CMP(sendCodec.plname, "ISAC") != 0)
2028 {
2029 _engineStatisticsPtr->SetLastError(
2030 VE_CODEC_ERROR, kTraceError,
2031 "SetISACMaxPayloadSize() send codec is not iSAC");
2032 return -1;
2033 }
2034 if (16000 == sendCodec.plfreq)
2035 {
2036 if ((sizeBytes < kVoiceEngineMinIsacMaxPayloadSizeBytesWb) ||
2037 (sizeBytes > kVoiceEngineMaxIsacMaxPayloadSizeBytesWb))
2038 {
2039 _engineStatisticsPtr->SetLastError(
2040 VE_INVALID_ARGUMENT, kTraceError,
2041 "SetISACMaxPayloadSize() invalid max payload - 1");
2042 return -1;
2043 }
2044 }
2045 else if (32000 == sendCodec.plfreq)
2046 {
2047 if ((sizeBytes < kVoiceEngineMinIsacMaxPayloadSizeBytesSwb) ||
2048 (sizeBytes > kVoiceEngineMaxIsacMaxPayloadSizeBytesSwb))
2049 {
2050 _engineStatisticsPtr->SetLastError(
2051 VE_INVALID_ARGUMENT, kTraceError,
2052 "SetISACMaxPayloadSize() invalid max payload - 2");
2053 return -1;
2054 }
2055 }
2056 if (_sending)
2057 {
2058 _engineStatisticsPtr->SetLastError(
2059 VE_SENDING, kTraceError,
2060 "SetISACMaxPayloadSize() unable to set max rate while sending");
2061 return -1;
2062 }
2063
2064 if (_audioCodingModule.SetISACMaxPayloadSize(sizeBytes) == -1)
2065 {
2066 _engineStatisticsPtr->SetLastError(
2067 VE_AUDIO_CODING_MODULE_ERROR, kTraceError,
2068 "SetISACMaxPayloadSize() failed to set max payload size");
2069 return -1;
2070 }
2071 return 0;
2072}
2073
pbos@webrtc.org6141e132013-04-09 10:09:10 +00002074int32_t Channel::RegisterExternalTransport(Transport& transport)
niklase@google.com470e71d2011-07-07 08:21:25 +00002075{
2076 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId, _channelId),
2077 "Channel::RegisterExternalTransport()");
2078
mflodman@webrtc.org9a065d12012-03-07 08:12:21 +00002079 CriticalSectionScoped cs(&_callbackCritSect);
niklase@google.com470e71d2011-07-07 08:21:25 +00002080
niklase@google.com470e71d2011-07-07 08:21:25 +00002081 if (_externalTransport)
2082 {
2083 _engineStatisticsPtr->SetLastError(VE_INVALID_OPERATION,
2084 kTraceError,
2085 "RegisterExternalTransport() external transport already enabled");
2086 return -1;
2087 }
2088 _externalTransport = true;
2089 _transportPtr = &transport;
2090 return 0;
2091}
2092
pbos@webrtc.org6141e132013-04-09 10:09:10 +00002093int32_t
niklase@google.com470e71d2011-07-07 08:21:25 +00002094Channel::DeRegisterExternalTransport()
2095{
2096 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId,_channelId),
2097 "Channel::DeRegisterExternalTransport()");
2098
mflodman@webrtc.org9a065d12012-03-07 08:12:21 +00002099 CriticalSectionScoped cs(&_callbackCritSect);
xians@webrtc.org83661f52011-11-25 10:58:15 +00002100
niklase@google.com470e71d2011-07-07 08:21:25 +00002101 if (!_transportPtr)
2102 {
2103 _engineStatisticsPtr->SetLastError(
2104 VE_INVALID_OPERATION, kTraceWarning,
2105 "DeRegisterExternalTransport() external transport already "
2106 "disabled");
2107 return 0;
2108 }
2109 _externalTransport = false;
niklase@google.com470e71d2011-07-07 08:21:25 +00002110 _transportPtr = NULL;
2111 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId,_channelId),
2112 "DeRegisterExternalTransport() all transport is disabled");
niklase@google.com470e71d2011-07-07 08:21:25 +00002113 return 0;
2114}
2115
pbos@webrtc.org6141e132013-04-09 10:09:10 +00002116int32_t Channel::ReceivedRTPPacket(const int8_t* data, int32_t length) {
pwestin@webrtc.org0c459572013-04-03 15:43:57 +00002117 WEBRTC_TRACE(kTraceStream, kTraceVoice, VoEId(_instanceId,_channelId),
2118 "Channel::ReceivedRTPPacket()");
2119
2120 // Store playout timestamp for the received RTP packet
pwestin@webrtc.org1de01352013-04-11 20:23:35 +00002121 UpdatePlayoutTimestamp(false);
pwestin@webrtc.org0c459572013-04-03 15:43:57 +00002122
2123 // Dump the RTP packet to a file (if RTP dump is enabled).
pbos@webrtc.org6141e132013-04-09 10:09:10 +00002124 if (_rtpDumpIn.DumpPacket((const uint8_t*)data,
2125 (uint16_t)length) == -1) {
pwestin@webrtc.org0c459572013-04-03 15:43:57 +00002126 WEBRTC_TRACE(kTraceWarning, kTraceVoice,
2127 VoEId(_instanceId,_channelId),
2128 "Channel::SendPacket() RTP dump to input file failed");
2129 }
2130
2131 // Deliver RTP packet to RTP/RTCP module for parsing
2132 // The packet will be pushed back to the channel thru the
2133 // OnReceivedPayloadData callback so we don't push it to the ACM here
pbos@webrtc.org6141e132013-04-09 10:09:10 +00002134 if (_rtpRtcpModule->IncomingPacket((const uint8_t*)data,
2135 (uint16_t)length) == -1) {
pwestin@webrtc.org0c459572013-04-03 15:43:57 +00002136 _engineStatisticsPtr->SetLastError(
2137 VE_SOCKET_TRANSPORT_MODULE_ERROR, kTraceWarning,
2138 "Channel::IncomingRTPPacket() RTP packet is invalid");
2139 }
2140 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +00002141}
2142
pbos@webrtc.org6141e132013-04-09 10:09:10 +00002143int32_t Channel::ReceivedRTCPPacket(const int8_t* data, int32_t length) {
pwestin@webrtc.org0c459572013-04-03 15:43:57 +00002144 WEBRTC_TRACE(kTraceStream, kTraceVoice, VoEId(_instanceId,_channelId),
2145 "Channel::ReceivedRTCPPacket()");
2146 // Store playout timestamp for the received RTCP packet
pwestin@webrtc.org1de01352013-04-11 20:23:35 +00002147 UpdatePlayoutTimestamp(true);
pwestin@webrtc.org0c459572013-04-03 15:43:57 +00002148
2149 // Dump the RTCP packet to a file (if RTP dump is enabled).
pbos@webrtc.org6141e132013-04-09 10:09:10 +00002150 if (_rtpDumpIn.DumpPacket((const uint8_t*)data,
2151 (uint16_t)length) == -1) {
pwestin@webrtc.org0c459572013-04-03 15:43:57 +00002152 WEBRTC_TRACE(kTraceWarning, kTraceVoice,
2153 VoEId(_instanceId,_channelId),
2154 "Channel::SendPacket() RTCP dump to input file failed");
2155 }
2156
2157 // Deliver RTCP packet to RTP/RTCP module for parsing
pbos@webrtc.org6141e132013-04-09 10:09:10 +00002158 if (_rtpRtcpModule->IncomingPacket((const uint8_t*)data,
2159 (uint16_t)length) == -1) {
pwestin@webrtc.org0c459572013-04-03 15:43:57 +00002160 _engineStatisticsPtr->SetLastError(
2161 VE_SOCKET_TRANSPORT_MODULE_ERROR, kTraceWarning,
2162 "Channel::IncomingRTPPacket() RTCP packet is invalid");
2163 }
2164 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +00002165}
2166
pbos@webrtc.org6141e132013-04-09 10:09:10 +00002167int32_t
niklase@google.com470e71d2011-07-07 08:21:25 +00002168Channel::SetPacketTimeoutNotification(bool enable, int timeoutSeconds)
2169{
2170 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId,_channelId),
2171 "Channel::SetPacketTimeoutNotification()");
2172 if (enable)
2173 {
pbos@webrtc.org6141e132013-04-09 10:09:10 +00002174 const uint32_t RTPtimeoutMS = 1000*timeoutSeconds;
2175 const uint32_t RTCPtimeoutMS = 0;
pwestin@webrtc.org2853dde2012-05-11 11:08:54 +00002176 _rtpRtcpModule->SetPacketTimeout(RTPtimeoutMS, RTCPtimeoutMS);
niklase@google.com470e71d2011-07-07 08:21:25 +00002177 _rtpPacketTimeOutIsEnabled = true;
2178 _rtpTimeOutSeconds = timeoutSeconds;
2179 }
2180 else
2181 {
pwestin@webrtc.org2853dde2012-05-11 11:08:54 +00002182 _rtpRtcpModule->SetPacketTimeout(0, 0);
niklase@google.com470e71d2011-07-07 08:21:25 +00002183 _rtpPacketTimeOutIsEnabled = false;
2184 _rtpTimeOutSeconds = 0;
2185 }
2186 return 0;
2187}
2188
pbos@webrtc.org6141e132013-04-09 10:09:10 +00002189int32_t
niklase@google.com470e71d2011-07-07 08:21:25 +00002190Channel::GetPacketTimeoutNotification(bool& enabled, int& timeoutSeconds)
2191{
2192 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId,_channelId),
2193 "Channel::GetPacketTimeoutNotification()");
2194 enabled = _rtpPacketTimeOutIsEnabled;
2195 if (enabled)
2196 {
2197 timeoutSeconds = _rtpTimeOutSeconds;
2198 }
2199 WEBRTC_TRACE(kTraceStateInfo, kTraceVoice, VoEId(_instanceId,-1),
2200 "GetPacketTimeoutNotification() => enabled=%d,"
2201 " timeoutSeconds=%d",
2202 enabled, timeoutSeconds);
2203 return 0;
2204}
2205
pbos@webrtc.org6141e132013-04-09 10:09:10 +00002206int32_t
niklase@google.com470e71d2011-07-07 08:21:25 +00002207Channel::RegisterDeadOrAliveObserver(VoEConnectionObserver& observer)
2208{
2209 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId,_channelId),
2210 "Channel::RegisterDeadOrAliveObserver()");
mflodman@webrtc.org9a065d12012-03-07 08:12:21 +00002211 CriticalSectionScoped cs(&_callbackCritSect);
niklase@google.com470e71d2011-07-07 08:21:25 +00002212
2213 if (_connectionObserverPtr)
2214 {
2215 _engineStatisticsPtr->SetLastError(VE_INVALID_OPERATION, kTraceError,
2216 "RegisterDeadOrAliveObserver() observer already enabled");
2217 return -1;
2218 }
2219
2220 _connectionObserverPtr = &observer;
2221 _connectionObserver = true;
2222
2223 return 0;
2224}
2225
pbos@webrtc.org6141e132013-04-09 10:09:10 +00002226int32_t
niklase@google.com470e71d2011-07-07 08:21:25 +00002227Channel::DeRegisterDeadOrAliveObserver()
2228{
2229 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId,_channelId),
2230 "Channel::DeRegisterDeadOrAliveObserver()");
mflodman@webrtc.org9a065d12012-03-07 08:12:21 +00002231 CriticalSectionScoped cs(&_callbackCritSect);
niklase@google.com470e71d2011-07-07 08:21:25 +00002232
2233 if (!_connectionObserverPtr)
2234 {
2235 _engineStatisticsPtr->SetLastError(
2236 VE_INVALID_OPERATION, kTraceWarning,
2237 "DeRegisterDeadOrAliveObserver() observer already disabled");
2238 return 0;
2239 }
2240
2241 _connectionObserver = false;
2242 _connectionObserverPtr = NULL;
2243
2244 return 0;
2245}
2246
pbos@webrtc.org6141e132013-04-09 10:09:10 +00002247int32_t
niklase@google.com470e71d2011-07-07 08:21:25 +00002248Channel::SetPeriodicDeadOrAliveStatus(bool enable, int sampleTimeSeconds)
2249{
2250 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId,_channelId),
2251 "Channel::SetPeriodicDeadOrAliveStatus()");
2252 if (!_connectionObserverPtr)
2253 {
2254 WEBRTC_TRACE(kTraceWarning, kTraceVoice, VoEId(_instanceId,_channelId),
2255 "SetPeriodicDeadOrAliveStatus() connection observer has"
2256 " not been registered");
2257 }
2258 if (enable)
2259 {
2260 ResetDeadOrAliveCounters();
2261 }
2262 bool enabled(false);
pbos@webrtc.org6141e132013-04-09 10:09:10 +00002263 uint8_t currentSampleTimeSec(0);
niklase@google.com470e71d2011-07-07 08:21:25 +00002264 // Store last state (will be used later if dead-or-alive is disabled).
pwestin@webrtc.org2853dde2012-05-11 11:08:54 +00002265 _rtpRtcpModule->PeriodicDeadOrAliveStatus(enabled, currentSampleTimeSec);
niklase@google.com470e71d2011-07-07 08:21:25 +00002266 // Update the dead-or-alive state.
pwestin@webrtc.org2853dde2012-05-11 11:08:54 +00002267 if (_rtpRtcpModule->SetPeriodicDeadOrAliveStatus(
pbos@webrtc.org6141e132013-04-09 10:09:10 +00002268 enable, (uint8_t)sampleTimeSeconds) != 0)
niklase@google.com470e71d2011-07-07 08:21:25 +00002269 {
2270 _engineStatisticsPtr->SetLastError(
2271 VE_RTP_RTCP_MODULE_ERROR,
2272 kTraceError,
2273 "SetPeriodicDeadOrAliveStatus() failed to set dead-or-alive "
2274 "status");
2275 return -1;
2276 }
2277 if (!enable)
2278 {
2279 // Restore last utilized sample time.
2280 // Without this, the sample time would always be reset to default
2281 // (2 sec), each time dead-or-alived was disabled without sample-time
2282 // parameter.
pwestin@webrtc.org2853dde2012-05-11 11:08:54 +00002283 _rtpRtcpModule->SetPeriodicDeadOrAliveStatus(enable,
niklase@google.com470e71d2011-07-07 08:21:25 +00002284 currentSampleTimeSec);
2285 }
2286 return 0;
2287}
2288
pbos@webrtc.org6141e132013-04-09 10:09:10 +00002289int32_t
niklase@google.com470e71d2011-07-07 08:21:25 +00002290Channel::GetPeriodicDeadOrAliveStatus(bool& enabled, int& sampleTimeSeconds)
2291{
pwestin@webrtc.org2853dde2012-05-11 11:08:54 +00002292 _rtpRtcpModule->PeriodicDeadOrAliveStatus(
niklase@google.com470e71d2011-07-07 08:21:25 +00002293 enabled,
pbos@webrtc.org6141e132013-04-09 10:09:10 +00002294 (uint8_t&)sampleTimeSeconds);
niklase@google.com470e71d2011-07-07 08:21:25 +00002295 WEBRTC_TRACE(kTraceStateInfo, kTraceVoice, VoEId(_instanceId,-1),
2296 "GetPeriodicDeadOrAliveStatus() => enabled=%d,"
2297 " sampleTimeSeconds=%d",
2298 enabled, sampleTimeSeconds);
2299 return 0;
2300}
2301
niklase@google.com470e71d2011-07-07 08:21:25 +00002302int Channel::StartPlayingFileLocally(const char* fileName,
2303 const bool loop,
2304 const FileFormats format,
2305 const int startPosition,
2306 const float volumeScaling,
2307 const int stopPosition,
2308 const CodecInst* codecInst)
2309{
2310 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId,_channelId),
2311 "Channel::StartPlayingFileLocally(fileNameUTF8[]=%s, loop=%d,"
2312 " format=%d, volumeScaling=%5.3f, startPosition=%d, "
2313 "stopPosition=%d)", fileName, loop, format, volumeScaling,
2314 startPosition, stopPosition);
2315
2316 if (_outputFilePlaying)
2317 {
2318 _engineStatisticsPtr->SetLastError(
2319 VE_ALREADY_PLAYING, kTraceError,
2320 "StartPlayingFileLocally() is already playing");
2321 return -1;
2322 }
2323
niklase@google.com470e71d2011-07-07 08:21:25 +00002324 {
mflodman@webrtc.org9a065d12012-03-07 08:12:21 +00002325 CriticalSectionScoped cs(&_fileCritSect);
henrike@webrtc.orgb37c6282011-10-31 23:53:04 +00002326
2327 if (_outputFilePlayerPtr)
2328 {
2329 _outputFilePlayerPtr->RegisterModuleFileCallback(NULL);
2330 FilePlayer::DestroyFilePlayer(_outputFilePlayerPtr);
2331 _outputFilePlayerPtr = NULL;
2332 }
2333
2334 _outputFilePlayerPtr = FilePlayer::CreateFilePlayer(
2335 _outputFilePlayerId, (const FileFormats)format);
2336
2337 if (_outputFilePlayerPtr == NULL)
2338 {
2339 _engineStatisticsPtr->SetLastError(
2340 VE_INVALID_ARGUMENT, kTraceError,
henrike@webrtc.org31d30702011-11-18 19:59:32 +00002341 "StartPlayingFileLocally() filePlayer format is not correct");
henrike@webrtc.orgb37c6282011-10-31 23:53:04 +00002342 return -1;
2343 }
2344
pbos@webrtc.org6141e132013-04-09 10:09:10 +00002345 const uint32_t notificationTime(0);
henrike@webrtc.orgb37c6282011-10-31 23:53:04 +00002346
2347 if (_outputFilePlayerPtr->StartPlayingFile(
2348 fileName,
2349 loop,
2350 startPosition,
2351 volumeScaling,
2352 notificationTime,
2353 stopPosition,
2354 (const CodecInst*)codecInst) != 0)
2355 {
2356 _engineStatisticsPtr->SetLastError(
2357 VE_BAD_FILE, kTraceError,
2358 "StartPlayingFile() failed to start file playout");
2359 _outputFilePlayerPtr->StopPlayingFile();
2360 FilePlayer::DestroyFilePlayer(_outputFilePlayerPtr);
2361 _outputFilePlayerPtr = NULL;
2362 return -1;
2363 }
2364 _outputFilePlayerPtr->RegisterModuleFileCallback(this);
2365 _outputFilePlaying = true;
niklase@google.com470e71d2011-07-07 08:21:25 +00002366 }
braveyao@webrtc.orgab129902012-06-04 03:26:39 +00002367
2368 if (RegisterFilePlayingToMixer() != 0)
henrike@webrtc.org066f9e52011-10-28 23:15:47 +00002369 return -1;
niklase@google.com470e71d2011-07-07 08:21:25 +00002370
2371 return 0;
2372}
2373
2374int Channel::StartPlayingFileLocally(InStream* stream,
2375 const FileFormats format,
2376 const int startPosition,
2377 const float volumeScaling,
2378 const int stopPosition,
2379 const CodecInst* codecInst)
2380{
2381 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId,_channelId),
2382 "Channel::StartPlayingFileLocally(format=%d,"
2383 " volumeScaling=%5.3f, startPosition=%d, stopPosition=%d)",
2384 format, volumeScaling, startPosition, stopPosition);
2385
2386 if(stream == NULL)
2387 {
2388 _engineStatisticsPtr->SetLastError(
2389 VE_BAD_FILE, kTraceError,
2390 "StartPlayingFileLocally() NULL as input stream");
2391 return -1;
2392 }
2393
2394
2395 if (_outputFilePlaying)
2396 {
2397 _engineStatisticsPtr->SetLastError(
2398 VE_ALREADY_PLAYING, kTraceError,
2399 "StartPlayingFileLocally() is already playing");
2400 return -1;
2401 }
2402
niklase@google.com470e71d2011-07-07 08:21:25 +00002403 {
mflodman@webrtc.org9a065d12012-03-07 08:12:21 +00002404 CriticalSectionScoped cs(&_fileCritSect);
henrike@webrtc.orgb37c6282011-10-31 23:53:04 +00002405
2406 // Destroy the old instance
2407 if (_outputFilePlayerPtr)
2408 {
2409 _outputFilePlayerPtr->RegisterModuleFileCallback(NULL);
2410 FilePlayer::DestroyFilePlayer(_outputFilePlayerPtr);
2411 _outputFilePlayerPtr = NULL;
2412 }
2413
2414 // Create the instance
2415 _outputFilePlayerPtr = FilePlayer::CreateFilePlayer(
2416 _outputFilePlayerId,
2417 (const FileFormats)format);
2418
2419 if (_outputFilePlayerPtr == NULL)
2420 {
2421 _engineStatisticsPtr->SetLastError(
2422 VE_INVALID_ARGUMENT, kTraceError,
2423 "StartPlayingFileLocally() filePlayer format isnot correct");
2424 return -1;
2425 }
2426
pbos@webrtc.org6141e132013-04-09 10:09:10 +00002427 const uint32_t notificationTime(0);
henrike@webrtc.orgb37c6282011-10-31 23:53:04 +00002428
2429 if (_outputFilePlayerPtr->StartPlayingFile(*stream, startPosition,
2430 volumeScaling,
2431 notificationTime,
2432 stopPosition, codecInst) != 0)
2433 {
2434 _engineStatisticsPtr->SetLastError(VE_BAD_FILE, kTraceError,
2435 "StartPlayingFile() failed to "
2436 "start file playout");
2437 _outputFilePlayerPtr->StopPlayingFile();
2438 FilePlayer::DestroyFilePlayer(_outputFilePlayerPtr);
2439 _outputFilePlayerPtr = NULL;
2440 return -1;
2441 }
2442 _outputFilePlayerPtr->RegisterModuleFileCallback(this);
2443 _outputFilePlaying = true;
niklase@google.com470e71d2011-07-07 08:21:25 +00002444 }
braveyao@webrtc.orgab129902012-06-04 03:26:39 +00002445
2446 if (RegisterFilePlayingToMixer() != 0)
henrike@webrtc.org066f9e52011-10-28 23:15:47 +00002447 return -1;
niklase@google.com470e71d2011-07-07 08:21:25 +00002448
niklase@google.com470e71d2011-07-07 08:21:25 +00002449 return 0;
2450}
2451
2452int Channel::StopPlayingFileLocally()
2453{
2454 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId,_channelId),
2455 "Channel::StopPlayingFileLocally()");
2456
2457 if (!_outputFilePlaying)
2458 {
2459 _engineStatisticsPtr->SetLastError(
2460 VE_INVALID_OPERATION, kTraceWarning,
2461 "StopPlayingFileLocally() isnot playing");
2462 return 0;
2463 }
2464
niklase@google.com470e71d2011-07-07 08:21:25 +00002465 {
mflodman@webrtc.org9a065d12012-03-07 08:12:21 +00002466 CriticalSectionScoped cs(&_fileCritSect);
henrike@webrtc.orgb37c6282011-10-31 23:53:04 +00002467
2468 if (_outputFilePlayerPtr->StopPlayingFile() != 0)
2469 {
2470 _engineStatisticsPtr->SetLastError(
2471 VE_STOP_RECORDING_FAILED, kTraceError,
2472 "StopPlayingFile() could not stop playing");
2473 return -1;
2474 }
2475 _outputFilePlayerPtr->RegisterModuleFileCallback(NULL);
2476 FilePlayer::DestroyFilePlayer(_outputFilePlayerPtr);
2477 _outputFilePlayerPtr = NULL;
2478 _outputFilePlaying = false;
niklase@google.com470e71d2011-07-07 08:21:25 +00002479 }
henrike@webrtc.orgb37c6282011-10-31 23:53:04 +00002480 // _fileCritSect cannot be taken while calling
2481 // SetAnonymousMixibilityStatus. Refer to comments in
2482 // StartPlayingFileLocally(const char* ...) for more details.
henrike@webrtc.org066f9e52011-10-28 23:15:47 +00002483 if (_outputMixerPtr->SetAnonymousMixabilityStatus(*this, false) != 0)
2484 {
2485 _engineStatisticsPtr->SetLastError(
2486 VE_AUDIO_CONF_MIX_MODULE_ERROR, kTraceError,
henrike@webrtc.orgb37c6282011-10-31 23:53:04 +00002487 "StopPlayingFile() failed to stop participant from playing as"
2488 "file in the mixer");
henrike@webrtc.org066f9e52011-10-28 23:15:47 +00002489 return -1;
2490 }
niklase@google.com470e71d2011-07-07 08:21:25 +00002491
2492 return 0;
2493}
2494
2495int Channel::IsPlayingFileLocally() const
2496{
2497 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId,_channelId),
2498 "Channel::IsPlayingFileLocally()");
2499
pbos@webrtc.org6141e132013-04-09 10:09:10 +00002500 return (int32_t)_outputFilePlaying;
niklase@google.com470e71d2011-07-07 08:21:25 +00002501}
2502
braveyao@webrtc.orgab129902012-06-04 03:26:39 +00002503int Channel::RegisterFilePlayingToMixer()
2504{
2505 // Return success for not registering for file playing to mixer if:
2506 // 1. playing file before playout is started on that channel.
2507 // 2. starting playout without file playing on that channel.
2508 if (!_playing || !_outputFilePlaying)
2509 {
2510 return 0;
2511 }
2512
2513 // |_fileCritSect| cannot be taken while calling
2514 // SetAnonymousMixabilityStatus() since as soon as the participant is added
2515 // frames can be pulled by the mixer. Since the frames are generated from
2516 // the file, _fileCritSect will be taken. This would result in a deadlock.
2517 if (_outputMixerPtr->SetAnonymousMixabilityStatus(*this, true) != 0)
2518 {
2519 CriticalSectionScoped cs(&_fileCritSect);
2520 _outputFilePlaying = false;
2521 _engineStatisticsPtr->SetLastError(
2522 VE_AUDIO_CONF_MIX_MODULE_ERROR, kTraceError,
2523 "StartPlayingFile() failed to add participant as file to mixer");
2524 _outputFilePlayerPtr->StopPlayingFile();
2525 FilePlayer::DestroyFilePlayer(_outputFilePlayerPtr);
2526 _outputFilePlayerPtr = NULL;
2527 return -1;
2528 }
2529
2530 return 0;
2531}
2532
niklase@google.com470e71d2011-07-07 08:21:25 +00002533int Channel::ScaleLocalFilePlayout(const float scale)
2534{
2535 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId,_channelId),
2536 "Channel::ScaleLocalFilePlayout(scale=%5.3f)", scale);
2537
mflodman@webrtc.org9a065d12012-03-07 08:12:21 +00002538 CriticalSectionScoped cs(&_fileCritSect);
niklase@google.com470e71d2011-07-07 08:21:25 +00002539
2540 if (!_outputFilePlaying)
2541 {
2542 _engineStatisticsPtr->SetLastError(
2543 VE_INVALID_OPERATION, kTraceError,
2544 "ScaleLocalFilePlayout() isnot playing");
2545 return -1;
2546 }
2547 if ((_outputFilePlayerPtr == NULL) ||
2548 (_outputFilePlayerPtr->SetAudioScaling(scale) != 0))
2549 {
2550 _engineStatisticsPtr->SetLastError(
2551 VE_BAD_ARGUMENT, kTraceError,
2552 "SetAudioScaling() failed to scale the playout");
2553 return -1;
2554 }
2555
2556 return 0;
2557}
2558
2559int Channel::GetLocalPlayoutPosition(int& positionMs)
2560{
2561 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId,_channelId),
2562 "Channel::GetLocalPlayoutPosition(position=?)");
2563
pbos@webrtc.org6141e132013-04-09 10:09:10 +00002564 uint32_t position;
niklase@google.com470e71d2011-07-07 08:21:25 +00002565
mflodman@webrtc.org9a065d12012-03-07 08:12:21 +00002566 CriticalSectionScoped cs(&_fileCritSect);
niklase@google.com470e71d2011-07-07 08:21:25 +00002567
2568 if (_outputFilePlayerPtr == NULL)
2569 {
2570 _engineStatisticsPtr->SetLastError(
2571 VE_INVALID_OPERATION, kTraceError,
2572 "GetLocalPlayoutPosition() filePlayer instance doesnot exist");
2573 return -1;
2574 }
2575
2576 if (_outputFilePlayerPtr->GetPlayoutPosition(position) != 0)
2577 {
2578 _engineStatisticsPtr->SetLastError(
2579 VE_BAD_FILE, kTraceError,
2580 "GetLocalPlayoutPosition() failed");
2581 return -1;
2582 }
2583 positionMs = position;
2584
2585 return 0;
2586}
2587
2588int Channel::StartPlayingFileAsMicrophone(const char* fileName,
2589 const bool loop,
2590 const FileFormats format,
2591 const int startPosition,
2592 const float volumeScaling,
2593 const int stopPosition,
2594 const CodecInst* codecInst)
2595{
2596 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId,_channelId),
2597 "Channel::StartPlayingFileAsMicrophone(fileNameUTF8[]=%s, "
2598 "loop=%d, format=%d, volumeScaling=%5.3f, startPosition=%d, "
2599 "stopPosition=%d)", fileName, loop, format, volumeScaling,
2600 startPosition, stopPosition);
2601
2602 if (_inputFilePlaying)
2603 {
2604 _engineStatisticsPtr->SetLastError(
2605 VE_ALREADY_PLAYING, kTraceWarning,
2606 "StartPlayingFileAsMicrophone() filePlayer is playing");
2607 return 0;
2608 }
2609
mflodman@webrtc.org9a065d12012-03-07 08:12:21 +00002610 CriticalSectionScoped cs(&_fileCritSect);
niklase@google.com470e71d2011-07-07 08:21:25 +00002611
2612 // Destroy the old instance
2613 if (_inputFilePlayerPtr)
2614 {
2615 _inputFilePlayerPtr->RegisterModuleFileCallback(NULL);
2616 FilePlayer::DestroyFilePlayer(_inputFilePlayerPtr);
2617 _inputFilePlayerPtr = NULL;
2618 }
2619
2620 // Create the instance
2621 _inputFilePlayerPtr = FilePlayer::CreateFilePlayer(
2622 _inputFilePlayerId, (const FileFormats)format);
2623
2624 if (_inputFilePlayerPtr == NULL)
2625 {
2626 _engineStatisticsPtr->SetLastError(
2627 VE_INVALID_ARGUMENT, kTraceError,
2628 "StartPlayingFileAsMicrophone() filePlayer format isnot correct");
2629 return -1;
2630 }
2631
pbos@webrtc.org6141e132013-04-09 10:09:10 +00002632 const uint32_t notificationTime(0);
niklase@google.com470e71d2011-07-07 08:21:25 +00002633
2634 if (_inputFilePlayerPtr->StartPlayingFile(
2635 fileName,
2636 loop,
2637 startPosition,
2638 volumeScaling,
2639 notificationTime,
2640 stopPosition,
2641 (const CodecInst*)codecInst) != 0)
2642 {
2643 _engineStatisticsPtr->SetLastError(
2644 VE_BAD_FILE, kTraceError,
2645 "StartPlayingFile() failed to start file playout");
2646 _inputFilePlayerPtr->StopPlayingFile();
2647 FilePlayer::DestroyFilePlayer(_inputFilePlayerPtr);
2648 _inputFilePlayerPtr = NULL;
2649 return -1;
2650 }
2651 _inputFilePlayerPtr->RegisterModuleFileCallback(this);
2652 _inputFilePlaying = true;
2653
2654 return 0;
2655}
2656
2657int Channel::StartPlayingFileAsMicrophone(InStream* stream,
2658 const FileFormats format,
2659 const int startPosition,
2660 const float volumeScaling,
2661 const int stopPosition,
2662 const CodecInst* codecInst)
2663{
2664 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId,_channelId),
2665 "Channel::StartPlayingFileAsMicrophone(format=%d, "
2666 "volumeScaling=%5.3f, startPosition=%d, stopPosition=%d)",
2667 format, volumeScaling, startPosition, stopPosition);
2668
2669 if(stream == NULL)
2670 {
2671 _engineStatisticsPtr->SetLastError(
2672 VE_BAD_FILE, kTraceError,
2673 "StartPlayingFileAsMicrophone NULL as input stream");
2674 return -1;
2675 }
2676
2677 if (_inputFilePlaying)
2678 {
2679 _engineStatisticsPtr->SetLastError(
2680 VE_ALREADY_PLAYING, kTraceWarning,
2681 "StartPlayingFileAsMicrophone() is playing");
2682 return 0;
2683 }
2684
mflodman@webrtc.org9a065d12012-03-07 08:12:21 +00002685 CriticalSectionScoped cs(&_fileCritSect);
niklase@google.com470e71d2011-07-07 08:21:25 +00002686
2687 // Destroy the old instance
2688 if (_inputFilePlayerPtr)
2689 {
2690 _inputFilePlayerPtr->RegisterModuleFileCallback(NULL);
2691 FilePlayer::DestroyFilePlayer(_inputFilePlayerPtr);
2692 _inputFilePlayerPtr = NULL;
2693 }
2694
2695 // Create the instance
2696 _inputFilePlayerPtr = FilePlayer::CreateFilePlayer(
2697 _inputFilePlayerId, (const FileFormats)format);
2698
2699 if (_inputFilePlayerPtr == NULL)
2700 {
2701 _engineStatisticsPtr->SetLastError(
2702 VE_INVALID_ARGUMENT, kTraceError,
2703 "StartPlayingInputFile() filePlayer format isnot correct");
2704 return -1;
2705 }
2706
pbos@webrtc.org6141e132013-04-09 10:09:10 +00002707 const uint32_t notificationTime(0);
niklase@google.com470e71d2011-07-07 08:21:25 +00002708
2709 if (_inputFilePlayerPtr->StartPlayingFile(*stream, startPosition,
2710 volumeScaling, notificationTime,
2711 stopPosition, codecInst) != 0)
2712 {
2713 _engineStatisticsPtr->SetLastError(VE_BAD_FILE, kTraceError,
2714 "StartPlayingFile() failed to start "
2715 "file playout");
2716 _inputFilePlayerPtr->StopPlayingFile();
2717 FilePlayer::DestroyFilePlayer(_inputFilePlayerPtr);
2718 _inputFilePlayerPtr = NULL;
2719 return -1;
2720 }
andrew@webrtc.orgae1a58b2013-01-22 04:44:30 +00002721
niklase@google.com470e71d2011-07-07 08:21:25 +00002722 _inputFilePlayerPtr->RegisterModuleFileCallback(this);
2723 _inputFilePlaying = true;
2724
2725 return 0;
2726}
2727
2728int Channel::StopPlayingFileAsMicrophone()
2729{
2730 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId,_channelId),
2731 "Channel::StopPlayingFileAsMicrophone()");
2732
2733 if (!_inputFilePlaying)
2734 {
2735 _engineStatisticsPtr->SetLastError(
2736 VE_INVALID_OPERATION, kTraceWarning,
2737 "StopPlayingFileAsMicrophone() isnot playing");
2738 return 0;
2739 }
2740
mflodman@webrtc.org9a065d12012-03-07 08:12:21 +00002741 CriticalSectionScoped cs(&_fileCritSect);
niklase@google.com470e71d2011-07-07 08:21:25 +00002742 if (_inputFilePlayerPtr->StopPlayingFile() != 0)
2743 {
2744 _engineStatisticsPtr->SetLastError(
2745 VE_STOP_RECORDING_FAILED, kTraceError,
2746 "StopPlayingFile() could not stop playing");
2747 return -1;
2748 }
2749 _inputFilePlayerPtr->RegisterModuleFileCallback(NULL);
2750 FilePlayer::DestroyFilePlayer(_inputFilePlayerPtr);
2751 _inputFilePlayerPtr = NULL;
2752 _inputFilePlaying = false;
2753
2754 return 0;
2755}
2756
2757int Channel::IsPlayingFileAsMicrophone() const
2758{
2759 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId,_channelId),
2760 "Channel::IsPlayingFileAsMicrophone()");
2761
2762 return _inputFilePlaying;
2763}
2764
2765int Channel::ScaleFileAsMicrophonePlayout(const float scale)
2766{
2767 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId,_channelId),
2768 "Channel::ScaleFileAsMicrophonePlayout(scale=%5.3f)", scale);
2769
mflodman@webrtc.org9a065d12012-03-07 08:12:21 +00002770 CriticalSectionScoped cs(&_fileCritSect);
niklase@google.com470e71d2011-07-07 08:21:25 +00002771
2772 if (!_inputFilePlaying)
2773 {
2774 _engineStatisticsPtr->SetLastError(
2775 VE_INVALID_OPERATION, kTraceError,
2776 "ScaleFileAsMicrophonePlayout() isnot playing");
2777 return -1;
2778 }
2779
2780 if ((_inputFilePlayerPtr == NULL) ||
2781 (_inputFilePlayerPtr->SetAudioScaling(scale) != 0))
2782 {
2783 _engineStatisticsPtr->SetLastError(
2784 VE_BAD_ARGUMENT, kTraceError,
2785 "SetAudioScaling() failed to scale playout");
2786 return -1;
2787 }
2788
2789 return 0;
2790}
2791
leozwang@webrtc.org813e4b02012-03-01 18:34:25 +00002792int Channel::StartRecordingPlayout(const char* fileName,
niklase@google.com470e71d2011-07-07 08:21:25 +00002793 const CodecInst* codecInst)
2794{
2795 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId,_channelId),
2796 "Channel::StartRecordingPlayout(fileName=%s)", fileName);
2797
2798 if (_outputFileRecording)
2799 {
2800 WEBRTC_TRACE(kTraceWarning, kTraceVoice, VoEId(_instanceId,-1),
2801 "StartRecordingPlayout() is already recording");
2802 return 0;
2803 }
2804
2805 FileFormats format;
pbos@webrtc.org6141e132013-04-09 10:09:10 +00002806 const uint32_t notificationTime(0); // Not supported in VoE
niklase@google.com470e71d2011-07-07 08:21:25 +00002807 CodecInst dummyCodec={100,"L16",16000,320,1,320000};
2808
niklas.enbom@webrtc.org40197d72012-03-26 08:45:47 +00002809 if ((codecInst != NULL) &&
2810 ((codecInst->channels < 1) || (codecInst->channels > 2)))
niklase@google.com470e71d2011-07-07 08:21:25 +00002811 {
2812 _engineStatisticsPtr->SetLastError(
2813 VE_BAD_ARGUMENT, kTraceError,
2814 "StartRecordingPlayout() invalid compression");
2815 return(-1);
2816 }
2817 if(codecInst == NULL)
2818 {
2819 format = kFileFormatPcm16kHzFile;
2820 codecInst=&dummyCodec;
2821 }
2822 else if((STR_CASE_CMP(codecInst->plname,"L16") == 0) ||
2823 (STR_CASE_CMP(codecInst->plname,"PCMU") == 0) ||
2824 (STR_CASE_CMP(codecInst->plname,"PCMA") == 0))
2825 {
2826 format = kFileFormatWavFile;
2827 }
2828 else
2829 {
2830 format = kFileFormatCompressedFile;
2831 }
2832
mflodman@webrtc.org9a065d12012-03-07 08:12:21 +00002833 CriticalSectionScoped cs(&_fileCritSect);
niklase@google.com470e71d2011-07-07 08:21:25 +00002834
2835 // Destroy the old instance
2836 if (_outputFileRecorderPtr)
2837 {
2838 _outputFileRecorderPtr->RegisterModuleFileCallback(NULL);
2839 FileRecorder::DestroyFileRecorder(_outputFileRecorderPtr);
2840 _outputFileRecorderPtr = NULL;
2841 }
2842
2843 _outputFileRecorderPtr = FileRecorder::CreateFileRecorder(
2844 _outputFileRecorderId, (const FileFormats)format);
2845 if (_outputFileRecorderPtr == NULL)
2846 {
2847 _engineStatisticsPtr->SetLastError(
2848 VE_INVALID_ARGUMENT, kTraceError,
2849 "StartRecordingPlayout() fileRecorder format isnot correct");
2850 return -1;
2851 }
2852
2853 if (_outputFileRecorderPtr->StartRecordingAudioFile(
2854 fileName, (const CodecInst&)*codecInst, notificationTime) != 0)
2855 {
2856 _engineStatisticsPtr->SetLastError(
2857 VE_BAD_FILE, kTraceError,
2858 "StartRecordingAudioFile() failed to start file recording");
2859 _outputFileRecorderPtr->StopRecording();
2860 FileRecorder::DestroyFileRecorder(_outputFileRecorderPtr);
2861 _outputFileRecorderPtr = NULL;
2862 return -1;
2863 }
2864 _outputFileRecorderPtr->RegisterModuleFileCallback(this);
2865 _outputFileRecording = true;
2866
2867 return 0;
2868}
2869
2870int Channel::StartRecordingPlayout(OutStream* stream,
2871 const CodecInst* codecInst)
2872{
2873 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId,_channelId),
2874 "Channel::StartRecordingPlayout()");
2875
2876 if (_outputFileRecording)
2877 {
2878 WEBRTC_TRACE(kTraceWarning, kTraceVoice, VoEId(_instanceId,-1),
2879 "StartRecordingPlayout() is already recording");
2880 return 0;
2881 }
2882
2883 FileFormats format;
pbos@webrtc.org6141e132013-04-09 10:09:10 +00002884 const uint32_t notificationTime(0); // Not supported in VoE
niklase@google.com470e71d2011-07-07 08:21:25 +00002885 CodecInst dummyCodec={100,"L16",16000,320,1,320000};
2886
2887 if (codecInst != NULL && codecInst->channels != 1)
2888 {
2889 _engineStatisticsPtr->SetLastError(
2890 VE_BAD_ARGUMENT, kTraceError,
2891 "StartRecordingPlayout() invalid compression");
2892 return(-1);
2893 }
2894 if(codecInst == NULL)
2895 {
2896 format = kFileFormatPcm16kHzFile;
2897 codecInst=&dummyCodec;
2898 }
2899 else if((STR_CASE_CMP(codecInst->plname,"L16") == 0) ||
2900 (STR_CASE_CMP(codecInst->plname,"PCMU") == 0) ||
2901 (STR_CASE_CMP(codecInst->plname,"PCMA") == 0))
2902 {
2903 format = kFileFormatWavFile;
2904 }
2905 else
2906 {
2907 format = kFileFormatCompressedFile;
2908 }
2909
mflodman@webrtc.org9a065d12012-03-07 08:12:21 +00002910 CriticalSectionScoped cs(&_fileCritSect);
niklase@google.com470e71d2011-07-07 08:21:25 +00002911
2912 // Destroy the old instance
2913 if (_outputFileRecorderPtr)
2914 {
2915 _outputFileRecorderPtr->RegisterModuleFileCallback(NULL);
2916 FileRecorder::DestroyFileRecorder(_outputFileRecorderPtr);
2917 _outputFileRecorderPtr = NULL;
2918 }
2919
2920 _outputFileRecorderPtr = FileRecorder::CreateFileRecorder(
2921 _outputFileRecorderId, (const FileFormats)format);
2922 if (_outputFileRecorderPtr == NULL)
2923 {
2924 _engineStatisticsPtr->SetLastError(
2925 VE_INVALID_ARGUMENT, kTraceError,
2926 "StartRecordingPlayout() fileRecorder format isnot correct");
2927 return -1;
2928 }
2929
2930 if (_outputFileRecorderPtr->StartRecordingAudioFile(*stream, *codecInst,
2931 notificationTime) != 0)
2932 {
2933 _engineStatisticsPtr->SetLastError(VE_BAD_FILE, kTraceError,
2934 "StartRecordingPlayout() failed to "
2935 "start file recording");
2936 _outputFileRecorderPtr->StopRecording();
2937 FileRecorder::DestroyFileRecorder(_outputFileRecorderPtr);
2938 _outputFileRecorderPtr = NULL;
2939 return -1;
2940 }
andrew@webrtc.orgae1a58b2013-01-22 04:44:30 +00002941
niklase@google.com470e71d2011-07-07 08:21:25 +00002942 _outputFileRecorderPtr->RegisterModuleFileCallback(this);
2943 _outputFileRecording = true;
2944
2945 return 0;
2946}
2947
2948int Channel::StopRecordingPlayout()
2949{
2950 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId,-1),
2951 "Channel::StopRecordingPlayout()");
2952
2953 if (!_outputFileRecording)
2954 {
2955 WEBRTC_TRACE(kTraceError, kTraceVoice, VoEId(_instanceId,-1),
2956 "StopRecordingPlayout() isnot recording");
2957 return -1;
2958 }
2959
2960
mflodman@webrtc.org9a065d12012-03-07 08:12:21 +00002961 CriticalSectionScoped cs(&_fileCritSect);
niklase@google.com470e71d2011-07-07 08:21:25 +00002962
2963 if (_outputFileRecorderPtr->StopRecording() != 0)
2964 {
2965 _engineStatisticsPtr->SetLastError(
2966 VE_STOP_RECORDING_FAILED, kTraceError,
2967 "StopRecording() could not stop recording");
2968 return(-1);
2969 }
2970 _outputFileRecorderPtr->RegisterModuleFileCallback(NULL);
2971 FileRecorder::DestroyFileRecorder(_outputFileRecorderPtr);
2972 _outputFileRecorderPtr = NULL;
2973 _outputFileRecording = false;
2974
2975 return 0;
2976}
2977
2978void
2979Channel::SetMixWithMicStatus(bool mix)
2980{
2981 _mixFileWithMicrophone=mix;
2982}
2983
2984int
pbos@webrtc.org6141e132013-04-09 10:09:10 +00002985Channel::GetSpeechOutputLevel(uint32_t& level) const
niklase@google.com470e71d2011-07-07 08:21:25 +00002986{
pbos@webrtc.org6141e132013-04-09 10:09:10 +00002987 int8_t currentLevel = _outputAudioLevel.Level();
2988 level = static_cast<int32_t> (currentLevel);
niklase@google.com470e71d2011-07-07 08:21:25 +00002989 WEBRTC_TRACE(kTraceStateInfo, kTraceVoice,
2990 VoEId(_instanceId,_channelId),
2991 "GetSpeechOutputLevel() => level=%u", level);
2992 return 0;
2993}
2994
2995int
pbos@webrtc.org6141e132013-04-09 10:09:10 +00002996Channel::GetSpeechOutputLevelFullRange(uint32_t& level) const
niklase@google.com470e71d2011-07-07 08:21:25 +00002997{
pbos@webrtc.org6141e132013-04-09 10:09:10 +00002998 int16_t currentLevel = _outputAudioLevel.LevelFullRange();
2999 level = static_cast<int32_t> (currentLevel);
niklase@google.com470e71d2011-07-07 08:21:25 +00003000 WEBRTC_TRACE(kTraceStateInfo, kTraceVoice,
3001 VoEId(_instanceId,_channelId),
3002 "GetSpeechOutputLevelFullRange() => level=%u", level);
3003 return 0;
3004}
3005
3006int
3007Channel::SetMute(bool enable)
3008{
3009 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId,_channelId),
3010 "Channel::SetMute(enable=%d)", enable);
3011 _mute = enable;
3012 return 0;
3013}
3014
3015bool
3016Channel::Mute() const
3017{
3018 return _mute;
3019}
3020
3021int
3022Channel::SetOutputVolumePan(float left, float right)
3023{
3024 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId,_channelId),
3025 "Channel::SetOutputVolumePan()");
3026 _panLeft = left;
3027 _panRight = right;
3028 return 0;
3029}
3030
3031int
3032Channel::GetOutputVolumePan(float& left, float& right) const
3033{
3034 left = _panLeft;
3035 right = _panRight;
3036 WEBRTC_TRACE(kTraceStateInfo, kTraceVoice,
3037 VoEId(_instanceId,_channelId),
3038 "GetOutputVolumePan() => left=%3.2f, right=%3.2f", left, right);
3039 return 0;
3040}
3041
3042int
3043Channel::SetChannelOutputVolumeScaling(float scaling)
3044{
3045 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId,_channelId),
3046 "Channel::SetChannelOutputVolumeScaling()");
3047 _outputGain = scaling;
3048 return 0;
3049}
3050
3051int
3052Channel::GetChannelOutputVolumeScaling(float& scaling) const
3053{
3054 scaling = _outputGain;
3055 WEBRTC_TRACE(kTraceStateInfo, kTraceVoice,
3056 VoEId(_instanceId,_channelId),
3057 "GetChannelOutputVolumeScaling() => scaling=%3.2f", scaling);
3058 return 0;
3059}
3060
niklase@google.com470e71d2011-07-07 08:21:25 +00003061int
3062Channel::RegisterExternalEncryption(Encryption& encryption)
3063{
3064 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId,_channelId),
3065 "Channel::RegisterExternalEncryption()");
3066
mflodman@webrtc.org9a065d12012-03-07 08:12:21 +00003067 CriticalSectionScoped cs(&_callbackCritSect);
niklase@google.com470e71d2011-07-07 08:21:25 +00003068
3069 if (_encryptionPtr)
3070 {
3071 _engineStatisticsPtr->SetLastError(
3072 VE_INVALID_OPERATION, kTraceError,
3073 "RegisterExternalEncryption() encryption already enabled");
3074 return -1;
3075 }
3076
3077 _encryptionPtr = &encryption;
3078
3079 _decrypting = true;
3080 _encrypting = true;
3081
3082 return 0;
3083}
3084
3085int
3086Channel::DeRegisterExternalEncryption()
3087{
3088 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId,_channelId),
3089 "Channel::DeRegisterExternalEncryption()");
3090
mflodman@webrtc.org9a065d12012-03-07 08:12:21 +00003091 CriticalSectionScoped cs(&_callbackCritSect);
niklase@google.com470e71d2011-07-07 08:21:25 +00003092
3093 if (!_encryptionPtr)
3094 {
3095 _engineStatisticsPtr->SetLastError(
3096 VE_INVALID_OPERATION, kTraceWarning,
3097 "DeRegisterExternalEncryption() encryption already disabled");
3098 return 0;
3099 }
3100
3101 _decrypting = false;
3102 _encrypting = false;
3103
3104 _encryptionPtr = NULL;
3105
3106 return 0;
3107}
3108
3109int Channel::SendTelephoneEventOutband(unsigned char eventCode,
3110 int lengthMs, int attenuationDb,
3111 bool playDtmfEvent)
3112{
3113 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId, _channelId),
3114 "Channel::SendTelephoneEventOutband(..., playDtmfEvent=%d)",
3115 playDtmfEvent);
3116
3117 _playOutbandDtmfEvent = playDtmfEvent;
3118
pwestin@webrtc.org2853dde2012-05-11 11:08:54 +00003119 if (_rtpRtcpModule->SendTelephoneEventOutband(eventCode, lengthMs,
niklase@google.com470e71d2011-07-07 08:21:25 +00003120 attenuationDb) != 0)
3121 {
3122 _engineStatisticsPtr->SetLastError(
3123 VE_SEND_DTMF_FAILED,
3124 kTraceWarning,
3125 "SendTelephoneEventOutband() failed to send event");
3126 return -1;
3127 }
3128 return 0;
3129}
3130
3131int Channel::SendTelephoneEventInband(unsigned char eventCode,
3132 int lengthMs,
3133 int attenuationDb,
3134 bool playDtmfEvent)
3135{
3136 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId, _channelId),
3137 "Channel::SendTelephoneEventInband(..., playDtmfEvent=%d)",
3138 playDtmfEvent);
3139
3140 _playInbandDtmfEvent = playDtmfEvent;
3141 _inbandDtmfQueue.AddDtmf(eventCode, lengthMs, attenuationDb);
3142
3143 return 0;
3144}
3145
3146int
3147Channel::SetDtmfPlayoutStatus(bool enable)
3148{
3149 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId,_channelId),
3150 "Channel::SetDtmfPlayoutStatus()");
3151 if (_audioCodingModule.SetDtmfPlayoutStatus(enable) != 0)
3152 {
3153 _engineStatisticsPtr->SetLastError(
3154 VE_AUDIO_CODING_MODULE_ERROR, kTraceWarning,
3155 "SetDtmfPlayoutStatus() failed to set Dtmf playout");
3156 return -1;
3157 }
3158 return 0;
3159}
3160
3161bool
3162Channel::DtmfPlayoutStatus() const
3163{
3164 return _audioCodingModule.DtmfPlayoutStatus();
3165}
3166
3167int
3168Channel::SetSendTelephoneEventPayloadType(unsigned char type)
3169{
3170 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId,_channelId),
3171 "Channel::SetSendTelephoneEventPayloadType()");
andrew@webrtc.orgf81f9f82011-08-19 22:56:22 +00003172 if (type > 127)
niklase@google.com470e71d2011-07-07 08:21:25 +00003173 {
3174 _engineStatisticsPtr->SetLastError(
3175 VE_INVALID_ARGUMENT, kTraceError,
3176 "SetSendTelephoneEventPayloadType() invalid type");
3177 return -1;
3178 }
pwestin@webrtc.org1da1ce02011-10-13 15:19:55 +00003179 CodecInst codec;
3180 codec.plfreq = 8000;
3181 codec.pltype = type;
3182 memcpy(codec.plname, "telephone-event", 16);
pwestin@webrtc.org2853dde2012-05-11 11:08:54 +00003183 if (_rtpRtcpModule->RegisterSendPayload(codec) != 0)
niklase@google.com470e71d2011-07-07 08:21:25 +00003184 {
3185 _engineStatisticsPtr->SetLastError(
3186 VE_RTP_RTCP_MODULE_ERROR, kTraceError,
3187 "SetSendTelephoneEventPayloadType() failed to register send"
3188 "payload type");
3189 return -1;
3190 }
3191 _sendTelephoneEventPayloadType = type;
3192 return 0;
3193}
3194
3195int
3196Channel::GetSendTelephoneEventPayloadType(unsigned char& type)
3197{
3198 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId,_channelId),
3199 "Channel::GetSendTelephoneEventPayloadType()");
3200 type = _sendTelephoneEventPayloadType;
3201 WEBRTC_TRACE(kTraceStateInfo, kTraceVoice,
3202 VoEId(_instanceId,_channelId),
3203 "GetSendTelephoneEventPayloadType() => type=%u", type);
3204 return 0;
3205}
3206
niklase@google.com470e71d2011-07-07 08:21:25 +00003207int
3208Channel::UpdateRxVadDetection(AudioFrame& audioFrame)
3209{
3210 WEBRTC_TRACE(kTraceStream, kTraceVoice, VoEId(_instanceId,_channelId),
3211 "Channel::UpdateRxVadDetection()");
3212
3213 int vadDecision = 1;
3214
andrew@webrtc.org63a50982012-05-02 23:56:37 +00003215 vadDecision = (audioFrame.vad_activity_ == AudioFrame::kVadActive)? 1 : 0;
niklase@google.com470e71d2011-07-07 08:21:25 +00003216
3217 if ((vadDecision != _oldVadDecision) && _rxVadObserverPtr)
3218 {
3219 OnRxVadDetected(vadDecision);
3220 _oldVadDecision = vadDecision;
3221 }
3222
3223 WEBRTC_TRACE(kTraceStream, kTraceVoice, VoEId(_instanceId,_channelId),
3224 "Channel::UpdateRxVadDetection() => vadDecision=%d",
3225 vadDecision);
3226 return 0;
3227}
3228
3229int
3230Channel::RegisterRxVadObserver(VoERxVadCallback &observer)
3231{
3232 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId,_channelId),
3233 "Channel::RegisterRxVadObserver()");
mflodman@webrtc.org9a065d12012-03-07 08:12:21 +00003234 CriticalSectionScoped cs(&_callbackCritSect);
niklase@google.com470e71d2011-07-07 08:21:25 +00003235
3236 if (_rxVadObserverPtr)
3237 {
3238 _engineStatisticsPtr->SetLastError(
3239 VE_INVALID_OPERATION, kTraceError,
3240 "RegisterRxVadObserver() observer already enabled");
3241 return -1;
3242 }
niklase@google.com470e71d2011-07-07 08:21:25 +00003243 _rxVadObserverPtr = &observer;
3244 _RxVadDetection = true;
3245 return 0;
3246}
3247
3248int
3249Channel::DeRegisterRxVadObserver()
3250{
3251 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId,_channelId),
3252 "Channel::DeRegisterRxVadObserver()");
mflodman@webrtc.org9a065d12012-03-07 08:12:21 +00003253 CriticalSectionScoped cs(&_callbackCritSect);
niklase@google.com470e71d2011-07-07 08:21:25 +00003254
3255 if (!_rxVadObserverPtr)
3256 {
3257 _engineStatisticsPtr->SetLastError(
3258 VE_INVALID_OPERATION, kTraceWarning,
3259 "DeRegisterRxVadObserver() observer already disabled");
3260 return 0;
3261 }
3262 _rxVadObserverPtr = NULL;
3263 _RxVadDetection = false;
3264 return 0;
3265}
3266
3267int
3268Channel::VoiceActivityIndicator(int &activity)
3269{
3270 activity = _sendFrameType;
3271
3272 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId,_channelId),
3273 "Channel::VoiceActivityIndicator(indicator=%d)", activity);
3274 return 0;
3275}
3276
3277#ifdef WEBRTC_VOICE_ENGINE_AGC
3278
3279int
3280Channel::SetRxAgcStatus(const bool enable, const AgcModes mode)
3281{
3282 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId,_channelId),
3283 "Channel::SetRxAgcStatus(enable=%d, mode=%d)",
3284 (int)enable, (int)mode);
3285
3286 GainControl::Mode agcMode(GainControl::kFixedDigital);
3287 switch (mode)
3288 {
3289 case kAgcDefault:
3290 agcMode = GainControl::kAdaptiveDigital;
3291 break;
3292 case kAgcUnchanged:
3293 agcMode = _rxAudioProcessingModulePtr->gain_control()->mode();
3294 break;
3295 case kAgcFixedDigital:
3296 agcMode = GainControl::kFixedDigital;
3297 break;
3298 case kAgcAdaptiveDigital:
3299 agcMode =GainControl::kAdaptiveDigital;
3300 break;
3301 default:
3302 _engineStatisticsPtr->SetLastError(
3303 VE_INVALID_ARGUMENT, kTraceError,
3304 "SetRxAgcStatus() invalid Agc mode");
3305 return -1;
3306 }
3307
3308 if (_rxAudioProcessingModulePtr->gain_control()->set_mode(agcMode) != 0)
3309 {
3310 _engineStatisticsPtr->SetLastError(
3311 VE_APM_ERROR, kTraceError,
3312 "SetRxAgcStatus() failed to set Agc mode");
3313 return -1;
3314 }
3315 if (_rxAudioProcessingModulePtr->gain_control()->Enable(enable) != 0)
3316 {
3317 _engineStatisticsPtr->SetLastError(
3318 VE_APM_ERROR, kTraceError,
3319 "SetRxAgcStatus() failed to set Agc state");
3320 return -1;
3321 }
3322
3323 _rxAgcIsEnabled = enable;
niklase@google.com470e71d2011-07-07 08:21:25 +00003324 _rxApmIsEnabled = ((_rxAgcIsEnabled == true) || (_rxNsIsEnabled == true));
3325
3326 return 0;
3327}
3328
3329int
3330Channel::GetRxAgcStatus(bool& enabled, AgcModes& mode)
3331{
3332 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId,_channelId),
3333 "Channel::GetRxAgcStatus(enable=?, mode=?)");
3334
3335 bool enable = _rxAudioProcessingModulePtr->gain_control()->is_enabled();
3336 GainControl::Mode agcMode =
3337 _rxAudioProcessingModulePtr->gain_control()->mode();
3338
3339 enabled = enable;
3340
3341 switch (agcMode)
3342 {
3343 case GainControl::kFixedDigital:
3344 mode = kAgcFixedDigital;
3345 break;
3346 case GainControl::kAdaptiveDigital:
3347 mode = kAgcAdaptiveDigital;
3348 break;
3349 default:
3350 _engineStatisticsPtr->SetLastError(
3351 VE_APM_ERROR, kTraceError,
3352 "GetRxAgcStatus() invalid Agc mode");
3353 return -1;
3354 }
3355
3356 return 0;
3357}
3358
3359int
3360Channel::SetRxAgcConfig(const AgcConfig config)
3361{
3362 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId,_channelId),
3363 "Channel::SetRxAgcConfig()");
3364
3365 if (_rxAudioProcessingModulePtr->gain_control()->set_target_level_dbfs(
3366 config.targetLeveldBOv) != 0)
3367 {
3368 _engineStatisticsPtr->SetLastError(
3369 VE_APM_ERROR, kTraceError,
3370 "SetRxAgcConfig() failed to set target peak |level|"
3371 "(or envelope) of the Agc");
3372 return -1;
3373 }
3374 if (_rxAudioProcessingModulePtr->gain_control()->set_compression_gain_db(
3375 config.digitalCompressionGaindB) != 0)
3376 {
3377 _engineStatisticsPtr->SetLastError(
3378 VE_APM_ERROR, kTraceError,
3379 "SetRxAgcConfig() failed to set the range in |gain| the"
3380 " digital compression stage may apply");
3381 return -1;
3382 }
3383 if (_rxAudioProcessingModulePtr->gain_control()->enable_limiter(
3384 config.limiterEnable) != 0)
3385 {
3386 _engineStatisticsPtr->SetLastError(
3387 VE_APM_ERROR, kTraceError,
3388 "SetRxAgcConfig() failed to set hard limiter to the signal");
3389 return -1;
3390 }
3391
3392 return 0;
3393}
3394
3395int
3396Channel::GetRxAgcConfig(AgcConfig& config)
3397{
3398 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId,_channelId),
3399 "Channel::GetRxAgcConfig(config=%?)");
3400
3401 config.targetLeveldBOv =
3402 _rxAudioProcessingModulePtr->gain_control()->target_level_dbfs();
3403 config.digitalCompressionGaindB =
3404 _rxAudioProcessingModulePtr->gain_control()->compression_gain_db();
3405 config.limiterEnable =
3406 _rxAudioProcessingModulePtr->gain_control()->is_limiter_enabled();
3407
3408 WEBRTC_TRACE(kTraceStateInfo, kTraceVoice,
3409 VoEId(_instanceId,_channelId), "GetRxAgcConfig() => "
3410 "targetLeveldBOv=%u, digitalCompressionGaindB=%u,"
3411 " limiterEnable=%d",
3412 config.targetLeveldBOv,
3413 config.digitalCompressionGaindB,
3414 config.limiterEnable);
3415
3416 return 0;
3417}
3418
3419#endif // #ifdef WEBRTC_VOICE_ENGINE_AGC
3420
3421#ifdef WEBRTC_VOICE_ENGINE_NR
3422
3423int
3424Channel::SetRxNsStatus(const bool enable, const NsModes mode)
3425{
3426 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId,_channelId),
3427 "Channel::SetRxNsStatus(enable=%d, mode=%d)",
3428 (int)enable, (int)mode);
3429
3430 NoiseSuppression::Level nsLevel(
3431 (NoiseSuppression::Level)WEBRTC_VOICE_ENGINE_RX_NS_DEFAULT_MODE);
3432 switch (mode)
3433 {
3434
3435 case kNsDefault:
3436 nsLevel = (NoiseSuppression::Level)
3437 WEBRTC_VOICE_ENGINE_RX_NS_DEFAULT_MODE;
3438 break;
3439 case kNsUnchanged:
3440 nsLevel = _rxAudioProcessingModulePtr->noise_suppression()->level();
3441 break;
3442 case kNsConference:
3443 nsLevel = NoiseSuppression::kHigh;
3444 break;
3445 case kNsLowSuppression:
3446 nsLevel = NoiseSuppression::kLow;
3447 break;
3448 case kNsModerateSuppression:
3449 nsLevel = NoiseSuppression::kModerate;
3450 break;
3451 case kNsHighSuppression:
3452 nsLevel = NoiseSuppression::kHigh;
3453 break;
3454 case kNsVeryHighSuppression:
3455 nsLevel = NoiseSuppression::kVeryHigh;
3456 break;
niklase@google.com470e71d2011-07-07 08:21:25 +00003457 }
3458
3459 if (_rxAudioProcessingModulePtr->noise_suppression()->set_level(nsLevel)
3460 != 0)
3461 {
3462 _engineStatisticsPtr->SetLastError(
3463 VE_APM_ERROR, kTraceError,
3464 "SetRxAgcStatus() failed to set Ns level");
3465 return -1;
3466 }
3467 if (_rxAudioProcessingModulePtr->noise_suppression()->Enable(enable) != 0)
3468 {
3469 _engineStatisticsPtr->SetLastError(
3470 VE_APM_ERROR, kTraceError,
3471 "SetRxAgcStatus() failed to set Agc state");
3472 return -1;
3473 }
3474
3475 _rxNsIsEnabled = enable;
3476 _rxApmIsEnabled = ((_rxAgcIsEnabled == true) || (_rxNsIsEnabled == true));
3477
3478 return 0;
3479}
3480
3481int
3482Channel::GetRxNsStatus(bool& enabled, NsModes& mode)
3483{
3484 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId,_channelId),
3485 "Channel::GetRxNsStatus(enable=?, mode=?)");
3486
3487 bool enable =
3488 _rxAudioProcessingModulePtr->noise_suppression()->is_enabled();
3489 NoiseSuppression::Level ncLevel =
3490 _rxAudioProcessingModulePtr->noise_suppression()->level();
3491
3492 enabled = enable;
3493
3494 switch (ncLevel)
3495 {
3496 case NoiseSuppression::kLow:
3497 mode = kNsLowSuppression;
3498 break;
3499 case NoiseSuppression::kModerate:
3500 mode = kNsModerateSuppression;
3501 break;
3502 case NoiseSuppression::kHigh:
3503 mode = kNsHighSuppression;
3504 break;
3505 case NoiseSuppression::kVeryHigh:
3506 mode = kNsVeryHighSuppression;
3507 break;
niklase@google.com470e71d2011-07-07 08:21:25 +00003508 }
3509
3510 WEBRTC_TRACE(kTraceStateInfo, kTraceVoice,
3511 VoEId(_instanceId,_channelId),
3512 "GetRxNsStatus() => enabled=%d, mode=%d", enabled, mode);
3513 return 0;
3514}
3515
3516#endif // #ifdef WEBRTC_VOICE_ENGINE_NR
3517
3518int
3519Channel::RegisterRTPObserver(VoERTPObserver& observer)
3520{
3521 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId, _channelId),
3522 "Channel::RegisterRTPObserver()");
mflodman@webrtc.org9a065d12012-03-07 08:12:21 +00003523 CriticalSectionScoped cs(&_callbackCritSect);
niklase@google.com470e71d2011-07-07 08:21:25 +00003524
3525 if (_rtpObserverPtr)
3526 {
3527 _engineStatisticsPtr->SetLastError(
3528 VE_INVALID_OPERATION, kTraceError,
3529 "RegisterRTPObserver() observer already enabled");
3530 return -1;
3531 }
3532
3533 _rtpObserverPtr = &observer;
3534 _rtpObserver = true;
3535
3536 return 0;
3537}
3538
3539int
3540Channel::DeRegisterRTPObserver()
3541{
3542 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId,_channelId),
3543 "Channel::DeRegisterRTPObserver()");
mflodman@webrtc.org9a065d12012-03-07 08:12:21 +00003544 CriticalSectionScoped cs(&_callbackCritSect);
niklase@google.com470e71d2011-07-07 08:21:25 +00003545
3546 if (!_rtpObserverPtr)
3547 {
3548 _engineStatisticsPtr->SetLastError(
3549 VE_INVALID_OPERATION, kTraceWarning,
3550 "DeRegisterRTPObserver() observer already disabled");
3551 return 0;
3552 }
3553
3554 _rtpObserver = false;
3555 _rtpObserverPtr = NULL;
3556
3557 return 0;
3558}
3559
3560int
3561Channel::RegisterRTCPObserver(VoERTCPObserver& observer)
3562{
3563 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId,_channelId),
3564 "Channel::RegisterRTCPObserver()");
mflodman@webrtc.org9a065d12012-03-07 08:12:21 +00003565 CriticalSectionScoped cs(&_callbackCritSect);
niklase@google.com470e71d2011-07-07 08:21:25 +00003566
3567 if (_rtcpObserverPtr)
3568 {
3569 _engineStatisticsPtr->SetLastError(
3570 VE_INVALID_OPERATION, kTraceError,
3571 "RegisterRTCPObserver() observer already enabled");
3572 return -1;
3573 }
3574
3575 _rtcpObserverPtr = &observer;
3576 _rtcpObserver = true;
3577
3578 return 0;
3579}
3580
3581int
3582Channel::DeRegisterRTCPObserver()
3583{
3584 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId, _channelId),
3585 "Channel::DeRegisterRTCPObserver()");
mflodman@webrtc.org9a065d12012-03-07 08:12:21 +00003586 CriticalSectionScoped cs(&_callbackCritSect);
niklase@google.com470e71d2011-07-07 08:21:25 +00003587
3588 if (!_rtcpObserverPtr)
3589 {
3590 _engineStatisticsPtr->SetLastError(
3591 VE_INVALID_OPERATION, kTraceWarning,
3592 "DeRegisterRTCPObserver() observer already disabled");
3593 return 0;
3594 }
3595
3596 _rtcpObserver = false;
3597 _rtcpObserverPtr = NULL;
3598
3599 return 0;
3600}
3601
3602int
3603Channel::SetLocalSSRC(unsigned int ssrc)
3604{
3605 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId, _channelId),
3606 "Channel::SetLocalSSRC()");
3607 if (_sending)
3608 {
3609 _engineStatisticsPtr->SetLastError(
3610 VE_ALREADY_SENDING, kTraceError,
3611 "SetLocalSSRC() already sending");
3612 return -1;
3613 }
pwestin@webrtc.org2853dde2012-05-11 11:08:54 +00003614 if (_rtpRtcpModule->SetSSRC(ssrc) != 0)
niklase@google.com470e71d2011-07-07 08:21:25 +00003615 {
3616 _engineStatisticsPtr->SetLastError(
3617 VE_RTP_RTCP_MODULE_ERROR, kTraceError,
3618 "SetLocalSSRC() failed to set SSRC");
3619 return -1;
3620 }
3621 return 0;
3622}
3623
3624int
3625Channel::GetLocalSSRC(unsigned int& ssrc)
3626{
pwestin@webrtc.org2853dde2012-05-11 11:08:54 +00003627 ssrc = _rtpRtcpModule->SSRC();
niklase@google.com470e71d2011-07-07 08:21:25 +00003628 WEBRTC_TRACE(kTraceStateInfo, kTraceVoice,
3629 VoEId(_instanceId,_channelId),
3630 "GetLocalSSRC() => ssrc=%lu", ssrc);
3631 return 0;
3632}
3633
3634int
3635Channel::GetRemoteSSRC(unsigned int& ssrc)
3636{
pwestin@webrtc.org2853dde2012-05-11 11:08:54 +00003637 ssrc = _rtpRtcpModule->RemoteSSRC();
niklase@google.com470e71d2011-07-07 08:21:25 +00003638 WEBRTC_TRACE(kTraceStateInfo, kTraceVoice,
3639 VoEId(_instanceId,_channelId),
3640 "GetRemoteSSRC() => ssrc=%lu", ssrc);
3641 return 0;
3642}
3643
3644int
3645Channel::GetRemoteCSRCs(unsigned int arrCSRC[15])
3646{
3647 if (arrCSRC == NULL)
3648 {
3649 _engineStatisticsPtr->SetLastError(
3650 VE_INVALID_ARGUMENT, kTraceError,
3651 "GetRemoteCSRCs() invalid array argument");
3652 return -1;
3653 }
pbos@webrtc.org6141e132013-04-09 10:09:10 +00003654 uint32_t arrOfCSRC[kRtpCsrcSize];
3655 int32_t CSRCs(0);
pwestin@webrtc.org2853dde2012-05-11 11:08:54 +00003656 CSRCs = _rtpRtcpModule->CSRCs(arrOfCSRC);
niklase@google.com470e71d2011-07-07 08:21:25 +00003657 if (CSRCs > 0)
3658 {
pbos@webrtc.org6141e132013-04-09 10:09:10 +00003659 memcpy(arrCSRC, arrOfCSRC, CSRCs * sizeof(uint32_t));
niklase@google.com470e71d2011-07-07 08:21:25 +00003660 for (int i = 0; i < (int) CSRCs; i++)
3661 {
3662 WEBRTC_TRACE(kTraceStateInfo, kTraceVoice,
3663 VoEId(_instanceId, _channelId),
3664 "GetRemoteCSRCs() => arrCSRC[%d]=%lu", i, arrCSRC[i]);
3665 }
3666 } else
3667 {
3668 WEBRTC_TRACE(kTraceStateInfo, kTraceVoice,
3669 VoEId(_instanceId, _channelId),
3670 "GetRemoteCSRCs() => list is empty!");
3671 }
3672 return CSRCs;
3673}
3674
3675int
3676Channel::SetRTPAudioLevelIndicationStatus(bool enable, unsigned char ID)
3677{
andrew@webrtc.org755b04a2011-11-15 16:57:56 +00003678 if (_rtpAudioProc.get() == NULL)
3679 {
3680 _rtpAudioProc.reset(AudioProcessing::Create(VoEModuleId(_instanceId,
3681 _channelId)));
3682 if (_rtpAudioProc.get() == NULL)
3683 {
3684 _engineStatisticsPtr->SetLastError(VE_NO_MEMORY, kTraceCritical,
3685 "Failed to create AudioProcessing");
3686 return -1;
3687 }
3688 }
3689
3690 if (_rtpAudioProc->level_estimator()->Enable(enable) !=
3691 AudioProcessing::kNoError)
3692 {
3693 _engineStatisticsPtr->SetLastError(VE_APM_ERROR, kTraceWarning,
3694 "Failed to enable AudioProcessing::level_estimator()");
3695 }
3696
niklase@google.com470e71d2011-07-07 08:21:25 +00003697 _includeAudioLevelIndication = enable;
pwestin@webrtc.org2853dde2012-05-11 11:08:54 +00003698 return _rtpRtcpModule->SetRTPAudioLevelIndicationStatus(enable, ID);
niklase@google.com470e71d2011-07-07 08:21:25 +00003699}
3700int
3701Channel::GetRTPAudioLevelIndicationStatus(bool& enabled, unsigned char& ID)
3702{
3703 WEBRTC_TRACE(kTraceStateInfo, kTraceVoice,
3704 VoEId(_instanceId,_channelId),
3705 "GetRTPAudioLevelIndicationStatus() => enabled=%d, ID=%u",
3706 enabled, ID);
pwestin@webrtc.org2853dde2012-05-11 11:08:54 +00003707 return _rtpRtcpModule->GetRTPAudioLevelIndicationStatus(enabled, ID);
niklase@google.com470e71d2011-07-07 08:21:25 +00003708}
3709
3710int
3711Channel::SetRTCPStatus(bool enable)
3712{
3713 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId,_channelId),
3714 "Channel::SetRTCPStatus()");
pwestin@webrtc.org2853dde2012-05-11 11:08:54 +00003715 if (_rtpRtcpModule->SetRTCPStatus(enable ?
niklase@google.com470e71d2011-07-07 08:21:25 +00003716 kRtcpCompound : kRtcpOff) != 0)
3717 {
3718 _engineStatisticsPtr->SetLastError(
3719 VE_RTP_RTCP_MODULE_ERROR, kTraceError,
3720 "SetRTCPStatus() failed to set RTCP status");
3721 return -1;
3722 }
3723 return 0;
3724}
3725
3726int
3727Channel::GetRTCPStatus(bool& enabled)
3728{
pwestin@webrtc.org2853dde2012-05-11 11:08:54 +00003729 RTCPMethod method = _rtpRtcpModule->RTCP();
niklase@google.com470e71d2011-07-07 08:21:25 +00003730 enabled = (method != kRtcpOff);
3731 WEBRTC_TRACE(kTraceStateInfo, kTraceVoice,
3732 VoEId(_instanceId,_channelId),
3733 "GetRTCPStatus() => enabled=%d", enabled);
3734 return 0;
3735}
3736
3737int
3738Channel::SetRTCP_CNAME(const char cName[256])
3739{
3740 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId, _channelId),
3741 "Channel::SetRTCP_CNAME()");
pwestin@webrtc.org2853dde2012-05-11 11:08:54 +00003742 if (_rtpRtcpModule->SetCNAME(cName) != 0)
niklase@google.com470e71d2011-07-07 08:21:25 +00003743 {
3744 _engineStatisticsPtr->SetLastError(
3745 VE_RTP_RTCP_MODULE_ERROR, kTraceError,
3746 "SetRTCP_CNAME() failed to set RTCP CNAME");
3747 return -1;
3748 }
3749 return 0;
3750}
3751
3752int
3753Channel::GetRTCP_CNAME(char cName[256])
3754{
pwestin@webrtc.org2853dde2012-05-11 11:08:54 +00003755 if (_rtpRtcpModule->CNAME(cName) != 0)
niklase@google.com470e71d2011-07-07 08:21:25 +00003756 {
3757 _engineStatisticsPtr->SetLastError(
3758 VE_RTP_RTCP_MODULE_ERROR, kTraceError,
3759 "GetRTCP_CNAME() failed to retrieve RTCP CNAME");
3760 return -1;
3761 }
3762 WEBRTC_TRACE(kTraceStateInfo, kTraceVoice,
3763 VoEId(_instanceId, _channelId),
3764 "GetRTCP_CNAME() => cName=%s", cName);
3765 return 0;
3766}
3767
3768int
3769Channel::GetRemoteRTCP_CNAME(char cName[256])
3770{
3771 if (cName == NULL)
3772 {
3773 _engineStatisticsPtr->SetLastError(
3774 VE_INVALID_ARGUMENT, kTraceError,
3775 "GetRemoteRTCP_CNAME() invalid CNAME input buffer");
3776 return -1;
3777 }
leozwang@webrtc.org813e4b02012-03-01 18:34:25 +00003778 char cname[RTCP_CNAME_SIZE];
pbos@webrtc.org6141e132013-04-09 10:09:10 +00003779 const uint32_t remoteSSRC = _rtpRtcpModule->RemoteSSRC();
pwestin@webrtc.org2853dde2012-05-11 11:08:54 +00003780 if (_rtpRtcpModule->RemoteCNAME(remoteSSRC, cname) != 0)
niklase@google.com470e71d2011-07-07 08:21:25 +00003781 {
3782 _engineStatisticsPtr->SetLastError(
3783 VE_CANNOT_RETRIEVE_CNAME, kTraceError,
3784 "GetRemoteRTCP_CNAME() failed to retrieve remote RTCP CNAME");
3785 return -1;
3786 }
3787 strcpy(cName, cname);
3788 WEBRTC_TRACE(kTraceStateInfo, kTraceVoice,
3789 VoEId(_instanceId, _channelId),
3790 "GetRemoteRTCP_CNAME() => cName=%s", cName);
3791 return 0;
3792}
3793
3794int
3795Channel::GetRemoteRTCPData(
3796 unsigned int& NTPHigh,
3797 unsigned int& NTPLow,
3798 unsigned int& timestamp,
3799 unsigned int& playoutTimestamp,
3800 unsigned int* jitter,
3801 unsigned short* fractionLost)
3802{
3803 // --- Information from sender info in received Sender Reports
3804
3805 RTCPSenderInfo senderInfo;
pwestin@webrtc.org2853dde2012-05-11 11:08:54 +00003806 if (_rtpRtcpModule->RemoteRTCPStat(&senderInfo) != 0)
niklase@google.com470e71d2011-07-07 08:21:25 +00003807 {
3808 _engineStatisticsPtr->SetLastError(
3809 VE_RTP_RTCP_MODULE_ERROR, kTraceError,
wu@webrtc.orgfcd12b32011-09-15 20:49:50 +00003810 "GetRemoteRTCPData() failed to retrieve sender info for remote "
niklase@google.com470e71d2011-07-07 08:21:25 +00003811 "side");
3812 return -1;
3813 }
3814
3815 // We only utilize 12 out of 20 bytes in the sender info (ignores packet
3816 // and octet count)
3817 NTPHigh = senderInfo.NTPseconds;
3818 NTPLow = senderInfo.NTPfraction;
3819 timestamp = senderInfo.RTPtimeStamp;
3820
3821 WEBRTC_TRACE(kTraceStateInfo, kTraceVoice,
3822 VoEId(_instanceId, _channelId),
3823 "GetRemoteRTCPData() => NTPHigh=%lu, NTPLow=%lu, "
3824 "timestamp=%lu",
3825 NTPHigh, NTPLow, timestamp);
3826
3827 // --- Locally derived information
3828
3829 // This value is updated on each incoming RTCP packet (0 when no packet
3830 // has been received)
pwestin@webrtc.org1de01352013-04-11 20:23:35 +00003831 playoutTimestamp = playout_timestamp_rtcp_;
niklase@google.com470e71d2011-07-07 08:21:25 +00003832
3833 WEBRTC_TRACE(kTraceStateInfo, kTraceVoice,
3834 VoEId(_instanceId, _channelId),
3835 "GetRemoteRTCPData() => playoutTimestamp=%lu",
pwestin@webrtc.org1de01352013-04-11 20:23:35 +00003836 playout_timestamp_rtcp_);
niklase@google.com470e71d2011-07-07 08:21:25 +00003837
3838 if (NULL != jitter || NULL != fractionLost)
3839 {
perkj@webrtc.orgce5990c2012-01-11 13:00:08 +00003840 // Get all RTCP receiver report blocks that have been received on this
3841 // channel. If we receive RTP packets from a remote source we know the
3842 // remote SSRC and use the report block from him.
3843 // Otherwise use the first report block.
3844 std::vector<RTCPReportBlock> remote_stats;
pwestin@webrtc.org2853dde2012-05-11 11:08:54 +00003845 if (_rtpRtcpModule->RemoteRTCPStat(&remote_stats) != 0 ||
perkj@webrtc.orgce5990c2012-01-11 13:00:08 +00003846 remote_stats.empty()) {
3847 WEBRTC_TRACE(kTraceWarning, kTraceVoice,
3848 VoEId(_instanceId, _channelId),
3849 "GetRemoteRTCPData() failed to measure statistics due"
3850 " to lack of received RTP and/or RTCP packets");
3851 return -1;
niklase@google.com470e71d2011-07-07 08:21:25 +00003852 }
perkj@webrtc.orgce5990c2012-01-11 13:00:08 +00003853
pbos@webrtc.org6141e132013-04-09 10:09:10 +00003854 uint32_t remoteSSRC = _rtpRtcpModule->RemoteSSRC();
perkj@webrtc.orgce5990c2012-01-11 13:00:08 +00003855 std::vector<RTCPReportBlock>::const_iterator it = remote_stats.begin();
3856 for (; it != remote_stats.end(); ++it) {
3857 if (it->remoteSSRC == remoteSSRC)
3858 break;
niklase@google.com470e71d2011-07-07 08:21:25 +00003859 }
perkj@webrtc.orgce5990c2012-01-11 13:00:08 +00003860
3861 if (it == remote_stats.end()) {
3862 // If we have not received any RTCP packets from this SSRC it probably
3863 // means that we have not received any RTP packets.
3864 // Use the first received report block instead.
3865 it = remote_stats.begin();
3866 remoteSSRC = it->remoteSSRC;
niklase@google.com470e71d2011-07-07 08:21:25 +00003867 }
perkj@webrtc.orgce5990c2012-01-11 13:00:08 +00003868
xians@webrtc.org79af7342012-01-31 12:22:14 +00003869 if (jitter) {
3870 *jitter = it->jitter;
3871 WEBRTC_TRACE(kTraceStateInfo, kTraceVoice,
3872 VoEId(_instanceId, _channelId),
3873 "GetRemoteRTCPData() => jitter = %lu", *jitter);
3874 }
perkj@webrtc.orgce5990c2012-01-11 13:00:08 +00003875
xians@webrtc.org79af7342012-01-31 12:22:14 +00003876 if (fractionLost) {
3877 *fractionLost = it->fractionLost;
3878 WEBRTC_TRACE(kTraceStateInfo, kTraceVoice,
3879 VoEId(_instanceId, _channelId),
3880 "GetRemoteRTCPData() => fractionLost = %lu",
3881 *fractionLost);
3882 }
niklase@google.com470e71d2011-07-07 08:21:25 +00003883 }
3884 return 0;
3885}
3886
3887int
3888Channel::SendApplicationDefinedRTCPPacket(const unsigned char subType,
3889 unsigned int name,
3890 const char* data,
3891 unsigned short dataLengthInBytes)
3892{
3893 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId, _channelId),
3894 "Channel::SendApplicationDefinedRTCPPacket()");
3895 if (!_sending)
3896 {
3897 _engineStatisticsPtr->SetLastError(
3898 VE_NOT_SENDING, kTraceError,
3899 "SendApplicationDefinedRTCPPacket() not sending");
3900 return -1;
3901 }
3902 if (NULL == data)
3903 {
3904 _engineStatisticsPtr->SetLastError(
3905 VE_INVALID_ARGUMENT, kTraceError,
3906 "SendApplicationDefinedRTCPPacket() invalid data value");
3907 return -1;
3908 }
3909 if (dataLengthInBytes % 4 != 0)
3910 {
3911 _engineStatisticsPtr->SetLastError(
3912 VE_INVALID_ARGUMENT, kTraceError,
3913 "SendApplicationDefinedRTCPPacket() invalid length value");
3914 return -1;
3915 }
pwestin@webrtc.org2853dde2012-05-11 11:08:54 +00003916 RTCPMethod status = _rtpRtcpModule->RTCP();
niklase@google.com470e71d2011-07-07 08:21:25 +00003917 if (status == kRtcpOff)
3918 {
3919 _engineStatisticsPtr->SetLastError(
3920 VE_RTCP_ERROR, kTraceError,
3921 "SendApplicationDefinedRTCPPacket() RTCP is disabled");
3922 return -1;
3923 }
3924
3925 // Create and schedule the RTCP APP packet for transmission
pwestin@webrtc.org2853dde2012-05-11 11:08:54 +00003926 if (_rtpRtcpModule->SetRTCPApplicationSpecificData(
niklase@google.com470e71d2011-07-07 08:21:25 +00003927 subType,
3928 name,
3929 (const unsigned char*) data,
3930 dataLengthInBytes) != 0)
3931 {
3932 _engineStatisticsPtr->SetLastError(
3933 VE_SEND_ERROR, kTraceError,
3934 "SendApplicationDefinedRTCPPacket() failed to send RTCP packet");
3935 return -1;
3936 }
3937 return 0;
3938}
3939
3940int
3941Channel::GetRTPStatistics(
3942 unsigned int& averageJitterMs,
3943 unsigned int& maxJitterMs,
3944 unsigned int& discardedPackets)
3945{
pbos@webrtc.org6141e132013-04-09 10:09:10 +00003946 uint8_t fraction_lost(0);
3947 uint32_t cum_lost(0);
3948 uint32_t ext_max(0);
3949 uint32_t jitter(0);
3950 uint32_t max_jitter(0);
niklase@google.com470e71d2011-07-07 08:21:25 +00003951
3952 // The jitter statistics is updated for each received RTP packet and is
3953 // based on received packets.
pwestin@webrtc.org2853dde2012-05-11 11:08:54 +00003954 if (_rtpRtcpModule->StatisticsRTP(&fraction_lost,
niklase@google.com470e71d2011-07-07 08:21:25 +00003955 &cum_lost,
3956 &ext_max,
3957 &jitter,
3958 &max_jitter) != 0)
3959 {
3960 _engineStatisticsPtr->SetLastError(
3961 VE_CANNOT_RETRIEVE_RTP_STAT, kTraceWarning,
wu@webrtc.orgfcd12b32011-09-15 20:49:50 +00003962 "GetRTPStatistics() failed to read RTP statistics from the "
niklase@google.com470e71d2011-07-07 08:21:25 +00003963 "RTP/RTCP module");
3964 }
3965
pbos@webrtc.org6141e132013-04-09 10:09:10 +00003966 const int32_t playoutFrequency =
niklase@google.com470e71d2011-07-07 08:21:25 +00003967 _audioCodingModule.PlayoutFrequency();
3968 if (playoutFrequency > 0)
3969 {
3970 // Scale RTP statistics given the current playout frequency
3971 maxJitterMs = max_jitter / (playoutFrequency / 1000);
3972 averageJitterMs = jitter / (playoutFrequency / 1000);
3973 }
3974
3975 discardedPackets = _numberOfDiscardedPackets;
3976
3977 WEBRTC_TRACE(kTraceStateInfo, kTraceVoice,
3978 VoEId(_instanceId, _channelId),
3979 "GetRTPStatistics() => averageJitterMs = %lu, maxJitterMs = %lu,"
wu@webrtc.orgfcd12b32011-09-15 20:49:50 +00003980 " discardedPackets = %lu)",
niklase@google.com470e71d2011-07-07 08:21:25 +00003981 averageJitterMs, maxJitterMs, discardedPackets);
3982 return 0;
3983}
3984
henrika@webrtc.org8a2fc882012-08-22 08:53:55 +00003985int Channel::GetRemoteRTCPSenderInfo(SenderInfo* sender_info) {
3986 if (sender_info == NULL) {
3987 _engineStatisticsPtr->SetLastError(VE_INVALID_ARGUMENT, kTraceError,
3988 "GetRemoteRTCPSenderInfo() invalid sender_info.");
3989 return -1;
3990 }
3991
3992 // Get the sender info from the latest received RTCP Sender Report.
3993 RTCPSenderInfo rtcp_sender_info;
3994 if (_rtpRtcpModule->RemoteRTCPStat(&rtcp_sender_info) != 0) {
3995 _engineStatisticsPtr->SetLastError(VE_RTP_RTCP_MODULE_ERROR, kTraceError,
3996 "GetRemoteRTCPSenderInfo() failed to read RTCP SR sender info.");
3997 return -1;
3998 }
3999
4000 sender_info->NTP_timestamp_high = rtcp_sender_info.NTPseconds;
4001 sender_info->NTP_timestamp_low = rtcp_sender_info.NTPfraction;
4002 sender_info->RTP_timestamp = rtcp_sender_info.RTPtimeStamp;
4003 sender_info->sender_packet_count = rtcp_sender_info.sendPacketCount;
4004 sender_info->sender_octet_count = rtcp_sender_info.sendOctetCount;
4005 return 0;
4006}
4007
4008int Channel::GetRemoteRTCPReportBlocks(
4009 std::vector<ReportBlock>* report_blocks) {
4010 if (report_blocks == NULL) {
4011 _engineStatisticsPtr->SetLastError(VE_INVALID_ARGUMENT, kTraceError,
4012 "GetRemoteRTCPReportBlock()s invalid report_blocks.");
4013 return -1;
4014 }
4015
4016 // Get the report blocks from the latest received RTCP Sender or Receiver
4017 // Report. Each element in the vector contains the sender's SSRC and a
4018 // report block according to RFC 3550.
4019 std::vector<RTCPReportBlock> rtcp_report_blocks;
4020 if (_rtpRtcpModule->RemoteRTCPStat(&rtcp_report_blocks) != 0) {
4021 _engineStatisticsPtr->SetLastError(VE_RTP_RTCP_MODULE_ERROR, kTraceError,
4022 "GetRemoteRTCPReportBlocks() failed to read RTCP SR/RR report block.");
4023 return -1;
4024 }
4025
4026 if (rtcp_report_blocks.empty())
4027 return 0;
4028
4029 std::vector<RTCPReportBlock>::const_iterator it = rtcp_report_blocks.begin();
4030 for (; it != rtcp_report_blocks.end(); ++it) {
4031 ReportBlock report_block;
4032 report_block.sender_SSRC = it->remoteSSRC;
4033 report_block.source_SSRC = it->sourceSSRC;
4034 report_block.fraction_lost = it->fractionLost;
4035 report_block.cumulative_num_packets_lost = it->cumulativeLost;
4036 report_block.extended_highest_sequence_number = it->extendedHighSeqNum;
4037 report_block.interarrival_jitter = it->jitter;
4038 report_block.last_SR_timestamp = it->lastSR;
4039 report_block.delay_since_last_SR = it->delaySinceLastSR;
4040 report_blocks->push_back(report_block);
4041 }
4042 return 0;
4043}
4044
niklase@google.com470e71d2011-07-07 08:21:25 +00004045int
4046Channel::GetRTPStatistics(CallStatistics& stats)
4047{
pbos@webrtc.org6141e132013-04-09 10:09:10 +00004048 uint8_t fraction_lost(0);
4049 uint32_t cum_lost(0);
4050 uint32_t ext_max(0);
4051 uint32_t jitter(0);
4052 uint32_t max_jitter(0);
niklase@google.com470e71d2011-07-07 08:21:25 +00004053
4054 // --- Part one of the final structure (four values)
4055
4056 // The jitter statistics is updated for each received RTP packet and is
4057 // based on received packets.
pwestin@webrtc.org2853dde2012-05-11 11:08:54 +00004058 if (_rtpRtcpModule->StatisticsRTP(&fraction_lost,
niklase@google.com470e71d2011-07-07 08:21:25 +00004059 &cum_lost,
4060 &ext_max,
4061 &jitter,
4062 &max_jitter) != 0)
4063 {
4064 _engineStatisticsPtr->SetLastError(
4065 VE_CANNOT_RETRIEVE_RTP_STAT, kTraceWarning,
4066 "GetRTPStatistics() failed to read RTP statistics from the "
4067 "RTP/RTCP module");
4068 }
4069
4070 stats.fractionLost = fraction_lost;
4071 stats.cumulativeLost = cum_lost;
4072 stats.extendedMax = ext_max;
4073 stats.jitterSamples = jitter;
4074
4075 WEBRTC_TRACE(kTraceStateInfo, kTraceVoice,
4076 VoEId(_instanceId, _channelId),
4077 "GetRTPStatistics() => fractionLost=%lu, cumulativeLost=%lu,"
wu@webrtc.orgfcd12b32011-09-15 20:49:50 +00004078 " extendedMax=%lu, jitterSamples=%li)",
niklase@google.com470e71d2011-07-07 08:21:25 +00004079 stats.fractionLost, stats.cumulativeLost, stats.extendedMax,
4080 stats.jitterSamples);
4081
4082 // --- Part two of the final structure (one value)
4083
pbos@webrtc.org6141e132013-04-09 10:09:10 +00004084 uint16_t RTT(0);
pwestin@webrtc.org2853dde2012-05-11 11:08:54 +00004085 RTCPMethod method = _rtpRtcpModule->RTCP();
niklase@google.com470e71d2011-07-07 08:21:25 +00004086 if (method == kRtcpOff)
4087 {
4088 WEBRTC_TRACE(kTraceWarning, kTraceVoice,
4089 VoEId(_instanceId, _channelId),
wu@webrtc.orgfcd12b32011-09-15 20:49:50 +00004090 "GetRTPStatistics() RTCP is disabled => valid RTT "
niklase@google.com470e71d2011-07-07 08:21:25 +00004091 "measurements cannot be retrieved");
4092 } else
4093 {
4094 // The remote SSRC will be zero if no RTP packet has been received.
pbos@webrtc.org6141e132013-04-09 10:09:10 +00004095 uint32_t remoteSSRC = _rtpRtcpModule->RemoteSSRC();
niklase@google.com470e71d2011-07-07 08:21:25 +00004096 if (remoteSSRC > 0)
4097 {
pbos@webrtc.org6141e132013-04-09 10:09:10 +00004098 uint16_t avgRTT(0);
4099 uint16_t maxRTT(0);
4100 uint16_t minRTT(0);
niklase@google.com470e71d2011-07-07 08:21:25 +00004101
pwestin@webrtc.org2853dde2012-05-11 11:08:54 +00004102 if (_rtpRtcpModule->RTT(remoteSSRC, &RTT, &avgRTT, &minRTT, &maxRTT)
niklase@google.com470e71d2011-07-07 08:21:25 +00004103 != 0)
4104 {
4105 WEBRTC_TRACE(kTraceWarning, kTraceVoice,
4106 VoEId(_instanceId, _channelId),
wu@webrtc.orgfcd12b32011-09-15 20:49:50 +00004107 "GetRTPStatistics() failed to retrieve RTT from "
niklase@google.com470e71d2011-07-07 08:21:25 +00004108 "the RTP/RTCP module");
4109 }
4110 } else
4111 {
4112 WEBRTC_TRACE(kTraceWarning, kTraceVoice,
4113 VoEId(_instanceId, _channelId),
wu@webrtc.orgfcd12b32011-09-15 20:49:50 +00004114 "GetRTPStatistics() failed to measure RTT since no "
niklase@google.com470e71d2011-07-07 08:21:25 +00004115 "RTP packets have been received yet");
4116 }
4117 }
4118
4119 stats.rttMs = static_cast<int> (RTT);
4120
4121 WEBRTC_TRACE(kTraceStateInfo, kTraceVoice,
4122 VoEId(_instanceId, _channelId),
4123 "GetRTPStatistics() => rttMs=%d", stats.rttMs);
4124
4125 // --- Part three of the final structure (four values)
4126
pbos@webrtc.org6141e132013-04-09 10:09:10 +00004127 uint32_t bytesSent(0);
4128 uint32_t packetsSent(0);
4129 uint32_t bytesReceived(0);
4130 uint32_t packetsReceived(0);
niklase@google.com470e71d2011-07-07 08:21:25 +00004131
pwestin@webrtc.org2853dde2012-05-11 11:08:54 +00004132 if (_rtpRtcpModule->DataCountersRTP(&bytesSent,
niklase@google.com470e71d2011-07-07 08:21:25 +00004133 &packetsSent,
4134 &bytesReceived,
4135 &packetsReceived) != 0)
4136 {
4137 WEBRTC_TRACE(kTraceWarning, kTraceVoice,
4138 VoEId(_instanceId, _channelId),
4139 "GetRTPStatistics() failed to retrieve RTP datacounters =>"
wu@webrtc.orgfcd12b32011-09-15 20:49:50 +00004140 " output will not be complete");
niklase@google.com470e71d2011-07-07 08:21:25 +00004141 }
4142
4143 stats.bytesSent = bytesSent;
4144 stats.packetsSent = packetsSent;
4145 stats.bytesReceived = bytesReceived;
4146 stats.packetsReceived = packetsReceived;
4147
4148 WEBRTC_TRACE(kTraceStateInfo, kTraceVoice,
4149 VoEId(_instanceId, _channelId),
4150 "GetRTPStatistics() => bytesSent=%d, packetsSent=%d,"
wu@webrtc.orgfcd12b32011-09-15 20:49:50 +00004151 " bytesReceived=%d, packetsReceived=%d)",
niklase@google.com470e71d2011-07-07 08:21:25 +00004152 stats.bytesSent, stats.packetsSent, stats.bytesReceived,
4153 stats.packetsReceived);
4154
4155 return 0;
4156}
4157
turaj@webrtc.org42259e72012-12-11 02:15:12 +00004158int Channel::SetFECStatus(bool enable, int redPayloadtype) {
4159 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId, _channelId),
4160 "Channel::SetFECStatus()");
niklase@google.com470e71d2011-07-07 08:21:25 +00004161
turaj@webrtc.org8c8ad852013-01-31 18:20:17 +00004162 if (enable) {
4163 if (redPayloadtype < 0 || redPayloadtype > 127) {
4164 _engineStatisticsPtr->SetLastError(
4165 VE_PLTYPE_ERROR, kTraceError,
4166 "SetFECStatus() invalid RED payload type");
4167 return -1;
4168 }
4169
4170 if (SetRedPayloadType(redPayloadtype) < 0) {
4171 _engineStatisticsPtr->SetLastError(
4172 VE_CODEC_ERROR, kTraceError,
4173 "SetSecondarySendCodec() Failed to register RED ACM");
4174 return -1;
4175 }
turaj@webrtc.org42259e72012-12-11 02:15:12 +00004176 }
niklase@google.com470e71d2011-07-07 08:21:25 +00004177
turaj@webrtc.org42259e72012-12-11 02:15:12 +00004178 if (_audioCodingModule.SetFECStatus(enable) != 0) {
4179 _engineStatisticsPtr->SetLastError(
4180 VE_AUDIO_CODING_MODULE_ERROR, kTraceError,
4181 "SetFECStatus() failed to set FEC state in the ACM");
4182 return -1;
4183 }
4184 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +00004185}
4186
4187int
4188Channel::GetFECStatus(bool& enabled, int& redPayloadtype)
4189{
4190 enabled = _audioCodingModule.FECStatus();
4191 if (enabled)
4192 {
pbos@webrtc.org6141e132013-04-09 10:09:10 +00004193 int8_t payloadType(0);
pwestin@webrtc.org2853dde2012-05-11 11:08:54 +00004194 if (_rtpRtcpModule->SendREDPayloadType(payloadType) != 0)
niklase@google.com470e71d2011-07-07 08:21:25 +00004195 {
4196 _engineStatisticsPtr->SetLastError(
4197 VE_RTP_RTCP_MODULE_ERROR, kTraceError,
4198 "GetFECStatus() failed to retrieve RED PT from RTP/RTCP "
4199 "module");
4200 return -1;
4201 }
4202 WEBRTC_TRACE(kTraceStateInfo, kTraceVoice,
4203 VoEId(_instanceId, _channelId),
4204 "GetFECStatus() => enabled=%d, redPayloadtype=%d",
4205 enabled, redPayloadtype);
4206 return 0;
4207 }
4208 WEBRTC_TRACE(kTraceStateInfo, kTraceVoice,
4209 VoEId(_instanceId, _channelId),
4210 "GetFECStatus() => enabled=%d", enabled);
4211 return 0;
4212}
4213
4214int
niklase@google.com470e71d2011-07-07 08:21:25 +00004215Channel::StartRTPDump(const char fileNameUTF8[1024],
4216 RTPDirections direction)
4217{
4218 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId, _channelId),
4219 "Channel::StartRTPDump()");
4220 if ((direction != kRtpIncoming) && (direction != kRtpOutgoing))
4221 {
4222 _engineStatisticsPtr->SetLastError(
4223 VE_INVALID_ARGUMENT, kTraceError,
4224 "StartRTPDump() invalid RTP direction");
4225 return -1;
4226 }
4227 RtpDump* rtpDumpPtr = (direction == kRtpIncoming) ?
4228 &_rtpDumpIn : &_rtpDumpOut;
4229 if (rtpDumpPtr == NULL)
4230 {
4231 assert(false);
4232 return -1;
4233 }
4234 if (rtpDumpPtr->IsActive())
4235 {
4236 rtpDumpPtr->Stop();
4237 }
4238 if (rtpDumpPtr->Start(fileNameUTF8) != 0)
4239 {
4240 _engineStatisticsPtr->SetLastError(
4241 VE_BAD_FILE, kTraceError,
4242 "StartRTPDump() failed to create file");
4243 return -1;
4244 }
4245 return 0;
4246}
4247
4248int
4249Channel::StopRTPDump(RTPDirections direction)
4250{
4251 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId, _channelId),
4252 "Channel::StopRTPDump()");
4253 if ((direction != kRtpIncoming) && (direction != kRtpOutgoing))
4254 {
4255 _engineStatisticsPtr->SetLastError(
4256 VE_INVALID_ARGUMENT, kTraceError,
4257 "StopRTPDump() invalid RTP direction");
4258 return -1;
4259 }
4260 RtpDump* rtpDumpPtr = (direction == kRtpIncoming) ?
4261 &_rtpDumpIn : &_rtpDumpOut;
4262 if (rtpDumpPtr == NULL)
4263 {
4264 assert(false);
4265 return -1;
4266 }
4267 if (!rtpDumpPtr->IsActive())
4268 {
4269 return 0;
4270 }
4271 return rtpDumpPtr->Stop();
4272}
4273
4274bool
4275Channel::RTPDumpIsActive(RTPDirections direction)
4276{
4277 if ((direction != kRtpIncoming) &&
4278 (direction != kRtpOutgoing))
4279 {
4280 _engineStatisticsPtr->SetLastError(
4281 VE_INVALID_ARGUMENT, kTraceError,
4282 "RTPDumpIsActive() invalid RTP direction");
4283 return false;
4284 }
4285 RtpDump* rtpDumpPtr = (direction == kRtpIncoming) ?
4286 &_rtpDumpIn : &_rtpDumpOut;
4287 return rtpDumpPtr->IsActive();
4288}
4289
4290int
4291Channel::InsertExtraRTPPacket(unsigned char payloadType,
4292 bool markerBit,
4293 const char* payloadData,
4294 unsigned short payloadSize)
4295{
4296 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId, _channelId),
4297 "Channel::InsertExtraRTPPacket()");
4298 if (payloadType > 127)
4299 {
4300 _engineStatisticsPtr->SetLastError(
4301 VE_INVALID_PLTYPE, kTraceError,
4302 "InsertExtraRTPPacket() invalid payload type");
4303 return -1;
4304 }
4305 if (payloadData == NULL)
4306 {
4307 _engineStatisticsPtr->SetLastError(
4308 VE_INVALID_ARGUMENT, kTraceError,
4309 "InsertExtraRTPPacket() invalid payload data");
4310 return -1;
4311 }
pwestin@webrtc.org2853dde2012-05-11 11:08:54 +00004312 if (payloadSize > _rtpRtcpModule->MaxDataPayloadLength())
niklase@google.com470e71d2011-07-07 08:21:25 +00004313 {
4314 _engineStatisticsPtr->SetLastError(
4315 VE_INVALID_ARGUMENT, kTraceError,
4316 "InsertExtraRTPPacket() invalid payload size");
4317 return -1;
4318 }
4319 if (!_sending)
4320 {
4321 _engineStatisticsPtr->SetLastError(
4322 VE_NOT_SENDING, kTraceError,
4323 "InsertExtraRTPPacket() not sending");
4324 return -1;
4325 }
4326
4327 // Create extra RTP packet by calling RtpRtcp::SendOutgoingData().
4328 // Transport::SendPacket() will be called by the module when the RTP packet
4329 // is created.
4330 // The call to SendOutgoingData() does *not* modify the timestamp and
4331 // payloadtype to ensure that the RTP module generates a valid RTP packet
4332 // (user might utilize a non-registered payload type).
4333 // The marker bit and payload type will be replaced just before the actual
4334 // transmission, i.e., the actual modification is done *after* the RTP
4335 // module has delivered its RTP packet back to the VoE.
4336 // We will use the stored values above when the packet is modified
4337 // (see Channel::SendPacket()).
4338
4339 _extraPayloadType = payloadType;
4340 _extraMarkerBit = markerBit;
4341 _insertExtraRTPPacket = true;
4342
pwestin@webrtc.org2853dde2012-05-11 11:08:54 +00004343 if (_rtpRtcpModule->SendOutgoingData(kAudioFrameSpeech,
niklase@google.com470e71d2011-07-07 08:21:25 +00004344 _lastPayloadType,
4345 _lastLocalTimeStamp,
stefan@webrtc.orgddfdfed2012-07-03 13:21:22 +00004346 // Leaving the time when this frame was
4347 // received from the capture device as
4348 // undefined for voice for now.
4349 -1,
pbos@webrtc.org6141e132013-04-09 10:09:10 +00004350 (const uint8_t*) payloadData,
niklase@google.com470e71d2011-07-07 08:21:25 +00004351 payloadSize) != 0)
4352 {
4353 _engineStatisticsPtr->SetLastError(
4354 VE_RTP_RTCP_MODULE_ERROR, kTraceError,
4355 "InsertExtraRTPPacket() failed to send extra RTP packet");
4356 return -1;
4357 }
4358
4359 return 0;
4360}
4361
pbos@webrtc.org6141e132013-04-09 10:09:10 +00004362uint32_t
andrew@webrtc.org755b04a2011-11-15 16:57:56 +00004363Channel::Demultiplex(const AudioFrame& audioFrame)
niklase@google.com470e71d2011-07-07 08:21:25 +00004364{
4365 WEBRTC_TRACE(kTraceStream, kTraceVoice, VoEId(_instanceId,_channelId),
andrew@webrtc.org755b04a2011-11-15 16:57:56 +00004366 "Channel::Demultiplex()");
andrew@webrtc.orgae1a58b2013-01-22 04:44:30 +00004367 _audioFrame.CopyFrom(audioFrame);
andrew@webrtc.org63a50982012-05-02 23:56:37 +00004368 _audioFrame.id_ = _channelId;
niklase@google.com470e71d2011-07-07 08:21:25 +00004369 return 0;
4370}
4371
pbos@webrtc.org6141e132013-04-09 10:09:10 +00004372uint32_t
xians@google.com0b0665a2011-08-08 08:18:44 +00004373Channel::PrepareEncodeAndSend(int mixingFrequency)
niklase@google.com470e71d2011-07-07 08:21:25 +00004374{
4375 WEBRTC_TRACE(kTraceStream, kTraceVoice, VoEId(_instanceId,_channelId),
4376 "Channel::PrepareEncodeAndSend()");
4377
andrew@webrtc.org63a50982012-05-02 23:56:37 +00004378 if (_audioFrame.samples_per_channel_ == 0)
niklase@google.com470e71d2011-07-07 08:21:25 +00004379 {
4380 WEBRTC_TRACE(kTraceWarning, kTraceVoice, VoEId(_instanceId,_channelId),
4381 "Channel::PrepareEncodeAndSend() invalid audio frame");
4382 return -1;
4383 }
4384
4385 if (_inputFilePlaying)
4386 {
4387 MixOrReplaceAudioWithFile(mixingFrequency);
4388 }
4389
4390 if (_mute)
4391 {
4392 AudioFrameOperations::Mute(_audioFrame);
4393 }
4394
4395 if (_inputExternalMedia)
4396 {
mflodman@webrtc.org9a065d12012-03-07 08:12:21 +00004397 CriticalSectionScoped cs(&_callbackCritSect);
andrew@webrtc.org63a50982012-05-02 23:56:37 +00004398 const bool isStereo = (_audioFrame.num_channels_ == 2);
niklase@google.com470e71d2011-07-07 08:21:25 +00004399 if (_inputExternalMediaCallbackPtr)
4400 {
4401 _inputExternalMediaCallbackPtr->Process(
4402 _channelId,
4403 kRecordingPerChannel,
pbos@webrtc.org6141e132013-04-09 10:09:10 +00004404 (int16_t*)_audioFrame.data_,
andrew@webrtc.org63a50982012-05-02 23:56:37 +00004405 _audioFrame.samples_per_channel_,
4406 _audioFrame.sample_rate_hz_,
niklase@google.com470e71d2011-07-07 08:21:25 +00004407 isStereo);
4408 }
4409 }
4410
4411 InsertInbandDtmfTone();
4412
andrew@webrtc.org755b04a2011-11-15 16:57:56 +00004413 if (_includeAudioLevelIndication)
4414 {
4415 assert(_rtpAudioProc.get() != NULL);
4416
4417 // Check if settings need to be updated.
andrew@webrtc.org63a50982012-05-02 23:56:37 +00004418 if (_rtpAudioProc->sample_rate_hz() != _audioFrame.sample_rate_hz_)
andrew@webrtc.org755b04a2011-11-15 16:57:56 +00004419 {
andrew@webrtc.org63a50982012-05-02 23:56:37 +00004420 if (_rtpAudioProc->set_sample_rate_hz(_audioFrame.sample_rate_hz_) !=
andrew@webrtc.org755b04a2011-11-15 16:57:56 +00004421 AudioProcessing::kNoError)
4422 {
4423 WEBRTC_TRACE(kTraceWarning, kTraceVoice,
4424 VoEId(_instanceId, _channelId),
4425 "Error setting AudioProcessing sample rate");
4426 return -1;
4427 }
4428 }
4429
andrew@webrtc.org63a50982012-05-02 23:56:37 +00004430 if (_rtpAudioProc->num_input_channels() != _audioFrame.num_channels_)
andrew@webrtc.org755b04a2011-11-15 16:57:56 +00004431 {
andrew@webrtc.org63a50982012-05-02 23:56:37 +00004432 if (_rtpAudioProc->set_num_channels(_audioFrame.num_channels_,
4433 _audioFrame.num_channels_)
andrew@webrtc.org755b04a2011-11-15 16:57:56 +00004434 != AudioProcessing::kNoError)
4435 {
4436 WEBRTC_TRACE(kTraceWarning, kTraceVoice,
4437 VoEId(_instanceId, _channelId),
4438 "Error setting AudioProcessing channels");
4439 return -1;
4440 }
4441 }
4442
4443 // Performs level analysis only; does not affect the signal.
4444 _rtpAudioProc->ProcessStream(&_audioFrame);
4445 }
4446
niklase@google.com470e71d2011-07-07 08:21:25 +00004447 return 0;
4448}
4449
pbos@webrtc.org6141e132013-04-09 10:09:10 +00004450uint32_t
niklase@google.com470e71d2011-07-07 08:21:25 +00004451Channel::EncodeAndSend()
4452{
4453 WEBRTC_TRACE(kTraceStream, kTraceVoice, VoEId(_instanceId,_channelId),
4454 "Channel::EncodeAndSend()");
4455
andrew@webrtc.org63a50982012-05-02 23:56:37 +00004456 assert(_audioFrame.num_channels_ <= 2);
4457 if (_audioFrame.samples_per_channel_ == 0)
niklase@google.com470e71d2011-07-07 08:21:25 +00004458 {
4459 WEBRTC_TRACE(kTraceWarning, kTraceVoice, VoEId(_instanceId,_channelId),
4460 "Channel::EncodeAndSend() invalid audio frame");
4461 return -1;
4462 }
4463
andrew@webrtc.org63a50982012-05-02 23:56:37 +00004464 _audioFrame.id_ = _channelId;
niklase@google.com470e71d2011-07-07 08:21:25 +00004465
4466 // --- Add 10ms of raw (PCM) audio data to the encoder @ 32kHz.
4467
4468 // The ACM resamples internally.
andrew@webrtc.org63a50982012-05-02 23:56:37 +00004469 _audioFrame.timestamp_ = _timeStamp;
niklase@google.com470e71d2011-07-07 08:21:25 +00004470 if (_audioCodingModule.Add10MsData((AudioFrame&)_audioFrame) != 0)
4471 {
4472 WEBRTC_TRACE(kTraceError, kTraceVoice, VoEId(_instanceId,_channelId),
4473 "Channel::EncodeAndSend() ACM encoding failed");
4474 return -1;
4475 }
4476
andrew@webrtc.org63a50982012-05-02 23:56:37 +00004477 _timeStamp += _audioFrame.samples_per_channel_;
niklase@google.com470e71d2011-07-07 08:21:25 +00004478
4479 // --- Encode if complete frame is ready
4480
4481 // This call will trigger AudioPacketizationCallback::SendData if encoding
4482 // is done and payload is ready for packetization and transmission.
4483 return _audioCodingModule.Process();
4484}
4485
4486int Channel::RegisterExternalMediaProcessing(
4487 ProcessingTypes type,
4488 VoEMediaProcess& processObject)
4489{
4490 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId,_channelId),
4491 "Channel::RegisterExternalMediaProcessing()");
4492
mflodman@webrtc.org9a065d12012-03-07 08:12:21 +00004493 CriticalSectionScoped cs(&_callbackCritSect);
niklase@google.com470e71d2011-07-07 08:21:25 +00004494
4495 if (kPlaybackPerChannel == type)
4496 {
4497 if (_outputExternalMediaCallbackPtr)
4498 {
4499 _engineStatisticsPtr->SetLastError(
4500 VE_INVALID_OPERATION, kTraceError,
4501 "Channel::RegisterExternalMediaProcessing() "
4502 "output external media already enabled");
4503 return -1;
4504 }
4505 _outputExternalMediaCallbackPtr = &processObject;
4506 _outputExternalMedia = true;
4507 }
4508 else if (kRecordingPerChannel == type)
4509 {
4510 if (_inputExternalMediaCallbackPtr)
4511 {
4512 _engineStatisticsPtr->SetLastError(
4513 VE_INVALID_OPERATION, kTraceError,
4514 "Channel::RegisterExternalMediaProcessing() "
4515 "output external media already enabled");
4516 return -1;
4517 }
4518 _inputExternalMediaCallbackPtr = &processObject;
4519 _inputExternalMedia = true;
4520 }
4521 return 0;
4522}
4523
4524int Channel::DeRegisterExternalMediaProcessing(ProcessingTypes type)
4525{
4526 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId,_channelId),
4527 "Channel::DeRegisterExternalMediaProcessing()");
4528
mflodman@webrtc.org9a065d12012-03-07 08:12:21 +00004529 CriticalSectionScoped cs(&_callbackCritSect);
niklase@google.com470e71d2011-07-07 08:21:25 +00004530
4531 if (kPlaybackPerChannel == type)
4532 {
4533 if (!_outputExternalMediaCallbackPtr)
4534 {
4535 _engineStatisticsPtr->SetLastError(
4536 VE_INVALID_OPERATION, kTraceWarning,
4537 "Channel::DeRegisterExternalMediaProcessing() "
4538 "output external media already disabled");
4539 return 0;
4540 }
4541 _outputExternalMedia = false;
4542 _outputExternalMediaCallbackPtr = NULL;
4543 }
4544 else if (kRecordingPerChannel == type)
4545 {
4546 if (!_inputExternalMediaCallbackPtr)
4547 {
4548 _engineStatisticsPtr->SetLastError(
4549 VE_INVALID_OPERATION, kTraceWarning,
4550 "Channel::DeRegisterExternalMediaProcessing() "
4551 "input external media already disabled");
4552 return 0;
4553 }
4554 _inputExternalMedia = false;
4555 _inputExternalMediaCallbackPtr = NULL;
4556 }
4557
4558 return 0;
4559}
4560
roosa@google.com1b60ceb2012-12-12 23:00:29 +00004561int Channel::SetExternalMixing(bool enabled) {
4562 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId,_channelId),
4563 "Channel::SetExternalMixing(enabled=%d)", enabled);
4564
4565 if (_playing)
4566 {
4567 _engineStatisticsPtr->SetLastError(
4568 VE_INVALID_OPERATION, kTraceError,
4569 "Channel::SetExternalMixing() "
4570 "external mixing cannot be changed while playing.");
4571 return -1;
4572 }
4573
4574 _externalMixing = enabled;
4575
4576 return 0;
4577}
4578
niklase@google.com470e71d2011-07-07 08:21:25 +00004579int
4580Channel::ResetRTCPStatistics()
4581{
4582 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId,_channelId),
4583 "Channel::ResetRTCPStatistics()");
pbos@webrtc.org6141e132013-04-09 10:09:10 +00004584 uint32_t remoteSSRC(0);
pwestin@webrtc.org2853dde2012-05-11 11:08:54 +00004585 remoteSSRC = _rtpRtcpModule->RemoteSSRC();
4586 return _rtpRtcpModule->ResetRTT(remoteSSRC);
niklase@google.com470e71d2011-07-07 08:21:25 +00004587}
4588
4589int
4590Channel::GetRoundTripTimeSummary(StatVal& delaysMs) const
4591{
4592 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId,_channelId),
4593 "Channel::GetRoundTripTimeSummary()");
4594 // Override default module outputs for the case when RTCP is disabled.
4595 // This is done to ensure that we are backward compatible with the
4596 // VoiceEngine where we did not use RTP/RTCP module.
pwestin@webrtc.org2853dde2012-05-11 11:08:54 +00004597 if (!_rtpRtcpModule->RTCP())
niklase@google.com470e71d2011-07-07 08:21:25 +00004598 {
4599 delaysMs.min = -1;
4600 delaysMs.max = -1;
4601 delaysMs.average = -1;
4602 WEBRTC_TRACE(kTraceWarning, kTraceVoice, VoEId(_instanceId,_channelId),
4603 "Channel::GetRoundTripTimeSummary() RTCP is disabled =>"
4604 " valid RTT measurements cannot be retrieved");
4605 return 0;
4606 }
4607
pbos@webrtc.org6141e132013-04-09 10:09:10 +00004608 uint32_t remoteSSRC;
4609 uint16_t RTT;
4610 uint16_t avgRTT;
4611 uint16_t maxRTT;
4612 uint16_t minRTT;
niklase@google.com470e71d2011-07-07 08:21:25 +00004613 // The remote SSRC will be zero if no RTP packet has been received.
pwestin@webrtc.org2853dde2012-05-11 11:08:54 +00004614 remoteSSRC = _rtpRtcpModule->RemoteSSRC();
niklase@google.com470e71d2011-07-07 08:21:25 +00004615 if (remoteSSRC == 0)
4616 {
4617 WEBRTC_TRACE(kTraceWarning, kTraceVoice, VoEId(_instanceId,_channelId),
4618 "Channel::GetRoundTripTimeSummary() unable to measure RTT"
4619 " since no RTP packet has been received yet");
4620 }
4621
4622 // Retrieve RTT statistics from the RTP/RTCP module for the specified
4623 // channel and SSRC. The SSRC is required to parse out the correct source
4624 // in conference scenarios.
pwestin@webrtc.org2853dde2012-05-11 11:08:54 +00004625 if (_rtpRtcpModule->RTT(remoteSSRC, &RTT, &avgRTT, &minRTT,&maxRTT) != 0)
niklase@google.com470e71d2011-07-07 08:21:25 +00004626 {
4627 WEBRTC_TRACE(kTraceWarning, kTraceVoice, VoEId(_instanceId,_channelId),
4628 "GetRoundTripTimeSummary unable to retrieve RTT values"
4629 " from the RTCP layer");
4630 delaysMs.min = -1; delaysMs.max = -1; delaysMs.average = -1;
4631 }
4632 else
4633 {
4634 delaysMs.min = minRTT;
4635 delaysMs.max = maxRTT;
4636 delaysMs.average = avgRTT;
4637 }
4638 return 0;
4639}
4640
4641int
4642Channel::GetNetworkStatistics(NetworkStatistics& stats)
4643{
4644 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId,_channelId),
4645 "Channel::GetNetworkStatistics()");
tina.legrand@webrtc.org7a7a0082013-02-21 10:27:48 +00004646 ACMNetworkStatistics acm_stats;
4647 int return_value = _audioCodingModule.NetworkStatistics(&acm_stats);
4648 if (return_value >= 0) {
4649 memcpy(&stats, &acm_stats, sizeof(NetworkStatistics));
4650 }
4651 return return_value;
niklase@google.com470e71d2011-07-07 08:21:25 +00004652}
4653
pwestin@webrtc.org1de01352013-04-11 20:23:35 +00004654bool Channel::GetDelayEstimate(int* jitter_buffer_delay_ms,
4655 int* playout_buffer_delay_ms) const {
4656 if (_average_jitter_buffer_delay_us == 0) {
niklase@google.com470e71d2011-07-07 08:21:25 +00004657 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId,_channelId),
pwestin@webrtc.org1de01352013-04-11 20:23:35 +00004658 "Channel::GetDelayEstimate() no valid estimate.");
4659 return false;
4660 }
4661 *jitter_buffer_delay_ms = (_average_jitter_buffer_delay_us + 500) / 1000 +
4662 _recPacketDelayMs;
4663 *playout_buffer_delay_ms = playout_delay_ms_;
4664 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId,_channelId),
4665 "Channel::GetDelayEstimate()");
4666 return true;
niklase@google.com470e71d2011-07-07 08:21:25 +00004667}
4668
turaj@webrtc.org6388c3e2013-02-12 21:42:18 +00004669int Channel::SetInitialPlayoutDelay(int delay_ms)
4670{
4671 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId,_channelId),
4672 "Channel::SetInitialPlayoutDelay()");
4673 if ((delay_ms < kVoiceEngineMinMinPlayoutDelayMs) ||
4674 (delay_ms > kVoiceEngineMaxMinPlayoutDelayMs))
4675 {
4676 _engineStatisticsPtr->SetLastError(
4677 VE_INVALID_ARGUMENT, kTraceError,
4678 "SetInitialPlayoutDelay() invalid min delay");
4679 return -1;
4680 }
4681 if (_audioCodingModule.SetInitialPlayoutDelay(delay_ms) != 0)
4682 {
4683 _engineStatisticsPtr->SetLastError(
4684 VE_AUDIO_CODING_MODULE_ERROR, kTraceError,
4685 "SetInitialPlayoutDelay() failed to set min playout delay");
4686 return -1;
4687 }
4688 return 0;
4689}
4690
4691
niklase@google.com470e71d2011-07-07 08:21:25 +00004692int
4693Channel::SetMinimumPlayoutDelay(int delayMs)
4694{
4695 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId,_channelId),
4696 "Channel::SetMinimumPlayoutDelay()");
4697 if ((delayMs < kVoiceEngineMinMinPlayoutDelayMs) ||
4698 (delayMs > kVoiceEngineMaxMinPlayoutDelayMs))
4699 {
4700 _engineStatisticsPtr->SetLastError(
4701 VE_INVALID_ARGUMENT, kTraceError,
4702 "SetMinimumPlayoutDelay() invalid min delay");
4703 return -1;
4704 }
4705 if (_audioCodingModule.SetMinimumPlayoutDelay(delayMs) != 0)
4706 {
4707 _engineStatisticsPtr->SetLastError(
4708 VE_AUDIO_CODING_MODULE_ERROR, kTraceError,
4709 "SetMinimumPlayoutDelay() failed to set min playout delay");
4710 return -1;
4711 }
4712 return 0;
4713}
4714
pwestin@webrtc.org1de01352013-04-11 20:23:35 +00004715void Channel::UpdatePlayoutTimestamp(bool rtcp) {
4716 uint32_t playout_timestamp = 0;
4717
4718 if (_audioCodingModule.PlayoutTimestamp(&playout_timestamp) == -1) {
4719 WEBRTC_TRACE(kTraceWarning, kTraceVoice, VoEId(_instanceId,_channelId),
4720 "Channel::UpdatePlayoutTimestamp() failed to read playout"
4721 " timestamp from the ACM");
4722 _engineStatisticsPtr->SetLastError(
4723 VE_CANNOT_RETRIEVE_VALUE, kTraceError,
4724 "UpdatePlayoutTimestamp() failed to retrieve timestamp");
4725 return;
4726 }
4727
4728 uint16_t delay_ms = 0;
4729 if (_audioDeviceModulePtr->PlayoutDelay(&delay_ms) == -1) {
4730 WEBRTC_TRACE(kTraceWarning, kTraceVoice, VoEId(_instanceId,_channelId),
4731 "Channel::UpdatePlayoutTimestamp() failed to read playout"
4732 " delay from the ADM");
4733 _engineStatisticsPtr->SetLastError(
4734 VE_CANNOT_RETRIEVE_VALUE, kTraceError,
4735 "UpdatePlayoutTimestamp() failed to retrieve playout delay");
4736 return;
4737 }
4738
4739 int32_t playout_frequency = _audioCodingModule.PlayoutFrequency();
4740 CodecInst current_recive_codec;
4741 if (_audioCodingModule.ReceiveCodec(&current_recive_codec) == 0) {
4742 if (STR_CASE_CMP("G722", current_recive_codec.plname) == 0) {
4743 playout_frequency = 8000;
4744 } else if (STR_CASE_CMP("opus", current_recive_codec.plname) == 0) {
4745 playout_frequency = 48000;
niklase@google.com470e71d2011-07-07 08:21:25 +00004746 }
pwestin@webrtc.org1de01352013-04-11 20:23:35 +00004747 }
4748
4749 // Remove the playout delay.
4750 playout_timestamp -= (delay_ms * (playout_frequency / 1000));
4751
4752 WEBRTC_TRACE(kTraceStream, kTraceVoice, VoEId(_instanceId,_channelId),
4753 "Channel::UpdatePlayoutTimestamp() => playoutTimestamp = %lu",
4754 playout_timestamp);
4755
4756 if (rtcp) {
4757 playout_timestamp_rtcp_ = playout_timestamp;
4758 } else {
4759 playout_timestamp_rtp_ = playout_timestamp;
4760 }
4761 playout_delay_ms_ = delay_ms;
4762}
4763
4764int Channel::GetPlayoutTimestamp(unsigned int& timestamp) {
4765 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId,_channelId),
4766 "Channel::GetPlayoutTimestamp()");
4767 if (playout_timestamp_rtp_ == 0) {
4768 _engineStatisticsPtr->SetLastError(
4769 VE_CANNOT_RETRIEVE_VALUE, kTraceError,
4770 "GetPlayoutTimestamp() failed to retrieve timestamp");
4771 return -1;
4772 }
4773 timestamp = playout_timestamp_rtp_;
4774 WEBRTC_TRACE(kTraceStateInfo, kTraceVoice,
4775 VoEId(_instanceId,_channelId),
4776 "GetPlayoutTimestamp() => timestamp=%u", timestamp);
4777 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +00004778}
4779
4780int
4781Channel::SetInitTimestamp(unsigned int timestamp)
4782{
4783 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId,_channelId),
4784 "Channel::SetInitTimestamp()");
4785 if (_sending)
4786 {
4787 _engineStatisticsPtr->SetLastError(
4788 VE_SENDING, kTraceError, "SetInitTimestamp() already sending");
4789 return -1;
4790 }
pwestin@webrtc.org2853dde2012-05-11 11:08:54 +00004791 if (_rtpRtcpModule->SetStartTimestamp(timestamp) != 0)
niklase@google.com470e71d2011-07-07 08:21:25 +00004792 {
4793 _engineStatisticsPtr->SetLastError(
4794 VE_RTP_RTCP_MODULE_ERROR, kTraceError,
4795 "SetInitTimestamp() failed to set timestamp");
4796 return -1;
4797 }
4798 return 0;
4799}
4800
4801int
4802Channel::SetInitSequenceNumber(short sequenceNumber)
4803{
4804 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId,_channelId),
4805 "Channel::SetInitSequenceNumber()");
4806 if (_sending)
4807 {
4808 _engineStatisticsPtr->SetLastError(
4809 VE_SENDING, kTraceError,
4810 "SetInitSequenceNumber() already sending");
4811 return -1;
4812 }
pwestin@webrtc.org2853dde2012-05-11 11:08:54 +00004813 if (_rtpRtcpModule->SetSequenceNumber(sequenceNumber) != 0)
niklase@google.com470e71d2011-07-07 08:21:25 +00004814 {
4815 _engineStatisticsPtr->SetLastError(
4816 VE_RTP_RTCP_MODULE_ERROR, kTraceError,
4817 "SetInitSequenceNumber() failed to set sequence number");
4818 return -1;
4819 }
4820 return 0;
4821}
4822
4823int
4824Channel::GetRtpRtcp(RtpRtcp* &rtpRtcpModule) const
4825{
4826 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId,_channelId),
4827 "Channel::GetRtpRtcp()");
pwestin@webrtc.org2853dde2012-05-11 11:08:54 +00004828 rtpRtcpModule = _rtpRtcpModule.get();
niklase@google.com470e71d2011-07-07 08:21:25 +00004829 return 0;
4830}
4831
andrew@webrtc.orge59a0ac2012-05-08 17:12:40 +00004832// TODO(andrew): refactor Mix functions here and in transmit_mixer.cc to use
4833// a shared helper.
pbos@webrtc.org6141e132013-04-09 10:09:10 +00004834int32_t
xians@google.com0b0665a2011-08-08 08:18:44 +00004835Channel::MixOrReplaceAudioWithFile(const int mixingFrequency)
niklase@google.com470e71d2011-07-07 08:21:25 +00004836{
pbos@webrtc.org6141e132013-04-09 10:09:10 +00004837 scoped_array<int16_t> fileBuffer(new int16_t[640]);
andrew@webrtc.orge59a0ac2012-05-08 17:12:40 +00004838 int fileSamples(0);
niklase@google.com470e71d2011-07-07 08:21:25 +00004839
4840 {
mflodman@webrtc.org9a065d12012-03-07 08:12:21 +00004841 CriticalSectionScoped cs(&_fileCritSect);
niklase@google.com470e71d2011-07-07 08:21:25 +00004842
4843 if (_inputFilePlayerPtr == NULL)
4844 {
4845 WEBRTC_TRACE(kTraceWarning, kTraceVoice,
4846 VoEId(_instanceId, _channelId),
4847 "Channel::MixOrReplaceAudioWithFile() fileplayer"
4848 " doesnt exist");
4849 return -1;
4850 }
4851
braveyao@webrtc.orgd7131432012-03-29 10:39:44 +00004852 if (_inputFilePlayerPtr->Get10msAudioFromFile(fileBuffer.get(),
niklase@google.com470e71d2011-07-07 08:21:25 +00004853 fileSamples,
4854 mixingFrequency) == -1)
4855 {
4856 WEBRTC_TRACE(kTraceWarning, kTraceVoice,
4857 VoEId(_instanceId, _channelId),
4858 "Channel::MixOrReplaceAudioWithFile() file mixing "
4859 "failed");
4860 return -1;
4861 }
4862 if (fileSamples == 0)
4863 {
4864 WEBRTC_TRACE(kTraceWarning, kTraceVoice,
4865 VoEId(_instanceId, _channelId),
4866 "Channel::MixOrReplaceAudioWithFile() file is ended");
4867 return 0;
4868 }
4869 }
4870
andrew@webrtc.org63a50982012-05-02 23:56:37 +00004871 assert(_audioFrame.samples_per_channel_ == fileSamples);
niklase@google.com470e71d2011-07-07 08:21:25 +00004872
4873 if (_mixFileWithMicrophone)
4874 {
braveyao@webrtc.orgd7131432012-03-29 10:39:44 +00004875 // Currently file stream is always mono.
4876 // TODO(xians): Change the code when FilePlayer supports real stereo.
andrew@webrtc.org63a50982012-05-02 23:56:37 +00004877 Utility::MixWithSat(_audioFrame.data_,
andrew@webrtc.orge59a0ac2012-05-08 17:12:40 +00004878 _audioFrame.num_channels_,
braveyao@webrtc.orgd7131432012-03-29 10:39:44 +00004879 fileBuffer.get(),
4880 1,
andrew@webrtc.orge59a0ac2012-05-08 17:12:40 +00004881 fileSamples);
niklase@google.com470e71d2011-07-07 08:21:25 +00004882 }
4883 else
4884 {
braveyao@webrtc.orgd7131432012-03-29 10:39:44 +00004885 // Replace ACM audio with file.
4886 // Currently file stream is always mono.
4887 // TODO(xians): Change the code when FilePlayer supports real stereo.
niklase@google.com470e71d2011-07-07 08:21:25 +00004888 _audioFrame.UpdateFrame(_channelId,
4889 -1,
braveyao@webrtc.orgd7131432012-03-29 10:39:44 +00004890 fileBuffer.get(),
andrew@webrtc.orge59a0ac2012-05-08 17:12:40 +00004891 fileSamples,
niklase@google.com470e71d2011-07-07 08:21:25 +00004892 mixingFrequency,
4893 AudioFrame::kNormalSpeech,
4894 AudioFrame::kVadUnknown,
4895 1);
4896
4897 }
4898 return 0;
4899}
4900
pbos@webrtc.org6141e132013-04-09 10:09:10 +00004901int32_t
niklase@google.com470e71d2011-07-07 08:21:25 +00004902Channel::MixAudioWithFile(AudioFrame& audioFrame,
xians@google.com0b0665a2011-08-08 08:18:44 +00004903 const int mixingFrequency)
niklase@google.com470e71d2011-07-07 08:21:25 +00004904{
4905 assert(mixingFrequency <= 32000);
4906
pbos@webrtc.org6141e132013-04-09 10:09:10 +00004907 scoped_array<int16_t> fileBuffer(new int16_t[640]);
andrew@webrtc.orge59a0ac2012-05-08 17:12:40 +00004908 int fileSamples(0);
niklase@google.com470e71d2011-07-07 08:21:25 +00004909
4910 {
mflodman@webrtc.org9a065d12012-03-07 08:12:21 +00004911 CriticalSectionScoped cs(&_fileCritSect);
niklase@google.com470e71d2011-07-07 08:21:25 +00004912
4913 if (_outputFilePlayerPtr == NULL)
4914 {
4915 WEBRTC_TRACE(kTraceWarning, kTraceVoice,
4916 VoEId(_instanceId, _channelId),
4917 "Channel::MixAudioWithFile() file mixing failed");
4918 return -1;
4919 }
4920
4921 // We should get the frequency we ask for.
braveyao@webrtc.orgd7131432012-03-29 10:39:44 +00004922 if (_outputFilePlayerPtr->Get10msAudioFromFile(fileBuffer.get(),
niklase@google.com470e71d2011-07-07 08:21:25 +00004923 fileSamples,
4924 mixingFrequency) == -1)
4925 {
4926 WEBRTC_TRACE(kTraceWarning, kTraceVoice,
4927 VoEId(_instanceId, _channelId),
4928 "Channel::MixAudioWithFile() file mixing failed");
4929 return -1;
4930 }
4931 }
4932
andrew@webrtc.org63a50982012-05-02 23:56:37 +00004933 if (audioFrame.samples_per_channel_ == fileSamples)
niklase@google.com470e71d2011-07-07 08:21:25 +00004934 {
braveyao@webrtc.orgd7131432012-03-29 10:39:44 +00004935 // Currently file stream is always mono.
4936 // TODO(xians): Change the code when FilePlayer supports real stereo.
andrew@webrtc.org63a50982012-05-02 23:56:37 +00004937 Utility::MixWithSat(audioFrame.data_,
andrew@webrtc.orge59a0ac2012-05-08 17:12:40 +00004938 audioFrame.num_channels_,
braveyao@webrtc.orgd7131432012-03-29 10:39:44 +00004939 fileBuffer.get(),
4940 1,
andrew@webrtc.orge59a0ac2012-05-08 17:12:40 +00004941 fileSamples);
niklase@google.com470e71d2011-07-07 08:21:25 +00004942 }
4943 else
4944 {
4945 WEBRTC_TRACE(kTraceWarning, kTraceVoice, VoEId(_instanceId,_channelId),
andrew@webrtc.org63a50982012-05-02 23:56:37 +00004946 "Channel::MixAudioWithFile() samples_per_channel_(%d) != "
niklase@google.com470e71d2011-07-07 08:21:25 +00004947 "fileSamples(%d)",
andrew@webrtc.org63a50982012-05-02 23:56:37 +00004948 audioFrame.samples_per_channel_, fileSamples);
niklase@google.com470e71d2011-07-07 08:21:25 +00004949 return -1;
4950 }
4951
4952 return 0;
4953}
4954
4955int
4956Channel::InsertInbandDtmfTone()
4957{
niklas.enbom@webrtc.orgaf26f642011-11-16 12:41:36 +00004958 // Check if we should start a new tone.
niklase@google.com470e71d2011-07-07 08:21:25 +00004959 if (_inbandDtmfQueue.PendingDtmf() &&
4960 !_inbandDtmfGenerator.IsAddingTone() &&
4961 _inbandDtmfGenerator.DelaySinceLastTone() >
4962 kMinTelephoneEventSeparationMs)
4963 {
pbos@webrtc.org6141e132013-04-09 10:09:10 +00004964 int8_t eventCode(0);
4965 uint16_t lengthMs(0);
4966 uint8_t attenuationDb(0);
niklase@google.com470e71d2011-07-07 08:21:25 +00004967
4968 eventCode = _inbandDtmfQueue.NextDtmf(&lengthMs, &attenuationDb);
4969 _inbandDtmfGenerator.AddTone(eventCode, lengthMs, attenuationDb);
4970 if (_playInbandDtmfEvent)
4971 {
4972 // Add tone to output mixer using a reduced length to minimize
4973 // risk of echo.
4974 _outputMixerPtr->PlayDtmfTone(eventCode, lengthMs - 80,
4975 attenuationDb);
4976 }
4977 }
4978
4979 if (_inbandDtmfGenerator.IsAddingTone())
4980 {
pbos@webrtc.org6141e132013-04-09 10:09:10 +00004981 uint16_t frequency(0);
niklase@google.com470e71d2011-07-07 08:21:25 +00004982 _inbandDtmfGenerator.GetSampleRate(frequency);
4983
andrew@webrtc.org63a50982012-05-02 23:56:37 +00004984 if (frequency != _audioFrame.sample_rate_hz_)
niklase@google.com470e71d2011-07-07 08:21:25 +00004985 {
4986 // Update sample rate of Dtmf tone since the mixing frequency
4987 // has changed.
4988 _inbandDtmfGenerator.SetSampleRate(
pbos@webrtc.org6141e132013-04-09 10:09:10 +00004989 (uint16_t) (_audioFrame.sample_rate_hz_));
niklase@google.com470e71d2011-07-07 08:21:25 +00004990 // Reset the tone to be added taking the new sample rate into
4991 // account.
4992 _inbandDtmfGenerator.ResetTone();
4993 }
andrew@webrtc.orgae1a58b2013-01-22 04:44:30 +00004994
pbos@webrtc.org6141e132013-04-09 10:09:10 +00004995 int16_t toneBuffer[320];
4996 uint16_t toneSamples(0);
niklase@google.com470e71d2011-07-07 08:21:25 +00004997 // Get 10ms tone segment and set time since last tone to zero
4998 if (_inbandDtmfGenerator.Get10msTone(toneBuffer, toneSamples) == -1)
4999 {
5000 WEBRTC_TRACE(kTraceWarning, kTraceVoice,
5001 VoEId(_instanceId, _channelId),
5002 "Channel::EncodeAndSend() inserting Dtmf failed");
5003 return -1;
5004 }
5005
niklas.enbom@webrtc.orgaf26f642011-11-16 12:41:36 +00005006 // Replace mixed audio with DTMF tone.
andrew@webrtc.orgae1a58b2013-01-22 04:44:30 +00005007 for (int sample = 0;
andrew@webrtc.org63a50982012-05-02 23:56:37 +00005008 sample < _audioFrame.samples_per_channel_;
niklas.enbom@webrtc.orgaf26f642011-11-16 12:41:36 +00005009 sample++)
5010 {
andrew@webrtc.orgae1a58b2013-01-22 04:44:30 +00005011 for (int channel = 0;
5012 channel < _audioFrame.num_channels_;
niklas.enbom@webrtc.orgaf26f642011-11-16 12:41:36 +00005013 channel++)
5014 {
andrew@webrtc.orgae1a58b2013-01-22 04:44:30 +00005015 const int index = sample * _audioFrame.num_channels_ + channel;
5016 _audioFrame.data_[index] = toneBuffer[sample];
niklas.enbom@webrtc.orgaf26f642011-11-16 12:41:36 +00005017 }
5018 }
andrew@webrtc.orgae1a58b2013-01-22 04:44:30 +00005019
andrew@webrtc.org63a50982012-05-02 23:56:37 +00005020 assert(_audioFrame.samples_per_channel_ == toneSamples);
niklase@google.com470e71d2011-07-07 08:21:25 +00005021 } else
5022 {
5023 // Add 10ms to "delay-since-last-tone" counter
5024 _inbandDtmfGenerator.UpdateDelaySinceLastTone();
5025 }
5026 return 0;
5027}
5028
niklase@google.com470e71d2011-07-07 08:21:25 +00005029void
5030Channel::ResetDeadOrAliveCounters()
5031{
5032 _countDeadDetections = 0;
5033 _countAliveDetections = 0;
5034}
5035
5036void
5037Channel::UpdateDeadOrAliveCounters(bool alive)
5038{
5039 if (alive)
5040 _countAliveDetections++;
5041 else
5042 _countDeadDetections++;
5043}
5044
5045int
5046Channel::GetDeadOrAliveCounters(int& countDead, int& countAlive) const
5047{
5048 bool enabled;
pbos@webrtc.org6141e132013-04-09 10:09:10 +00005049 uint8_t timeSec;
niklase@google.com470e71d2011-07-07 08:21:25 +00005050
pwestin@webrtc.org2853dde2012-05-11 11:08:54 +00005051 _rtpRtcpModule->PeriodicDeadOrAliveStatus(enabled, timeSec);
niklase@google.com470e71d2011-07-07 08:21:25 +00005052 if (!enabled)
5053 return (-1);
5054
5055 countDead = static_cast<int> (_countDeadDetections);
5056 countAlive = static_cast<int> (_countAliveDetections);
5057 return 0;
5058}
5059
pbos@webrtc.org6141e132013-04-09 10:09:10 +00005060int32_t
niklase@google.com470e71d2011-07-07 08:21:25 +00005061Channel::SendPacketRaw(const void *data, int len, bool RTCP)
5062{
5063 if (_transportPtr == NULL)
5064 {
5065 return -1;
5066 }
5067 if (!RTCP)
5068 {
5069 return _transportPtr->SendPacket(_channelId, data, len);
5070 }
5071 else
5072 {
5073 return _transportPtr->SendRTCPPacket(_channelId, data, len);
5074 }
5075}
5076
pwestin@webrtc.org1de01352013-04-11 20:23:35 +00005077// Called for incoming RTP packets after successful RTP header parsing.
5078void Channel::UpdatePacketDelay(uint32_t rtp_timestamp,
5079 uint16_t sequence_number) {
5080 WEBRTC_TRACE(kTraceStream, kTraceVoice, VoEId(_instanceId,_channelId),
5081 "Channel::UpdatePacketDelay(timestamp=%lu, sequenceNumber=%u)",
5082 rtp_timestamp, sequence_number);
niklase@google.com470e71d2011-07-07 08:21:25 +00005083
pwestin@webrtc.org1de01352013-04-11 20:23:35 +00005084 // Get frequency of last received payload
5085 int rtp_receive_frequency = _audioCodingModule.ReceiveFrequency();
niklase@google.com470e71d2011-07-07 08:21:25 +00005086
pwestin@webrtc.org1de01352013-04-11 20:23:35 +00005087 CodecInst current_receive_codec;
5088 if (_audioCodingModule.ReceiveCodec(&current_receive_codec) != 0) {
5089 return;
5090 }
niklase@google.com470e71d2011-07-07 08:21:25 +00005091
pwestin@webrtc.org1de01352013-04-11 20:23:35 +00005092 if (STR_CASE_CMP("G722", current_receive_codec.plname) == 0) {
5093 // Even though the actual sampling rate for G.722 audio is
5094 // 16,000 Hz, the RTP clock rate for the G722 payload format is
5095 // 8,000 Hz because that value was erroneously assigned in
5096 // RFC 1890 and must remain unchanged for backward compatibility.
5097 rtp_receive_frequency = 8000;
5098 } else if (STR_CASE_CMP("opus", current_receive_codec.plname) == 0) {
5099 // We are resampling Opus internally to 32,000 Hz until all our
5100 // DSP routines can operate at 48,000 Hz, but the RTP clock
5101 // rate for the Opus payload format is standardized to 48,000 Hz,
5102 // because that is the maximum supported decoding sampling rate.
5103 rtp_receive_frequency = 48000;
5104 }
niklase@google.com470e71d2011-07-07 08:21:25 +00005105
pwestin@webrtc.org1de01352013-04-11 20:23:35 +00005106 // playout_timestamp_rtp_ updated in UpdatePlayoutTimestamp for every incoming
5107 // packet.
5108 uint32_t timestamp_diff_ms = (rtp_timestamp - playout_timestamp_rtp_) /
5109 (rtp_receive_frequency / 1000);
niklase@google.com470e71d2011-07-07 08:21:25 +00005110
pwestin@webrtc.org1de01352013-04-11 20:23:35 +00005111 uint16_t packet_delay_ms = (rtp_timestamp - _previousTimestamp) /
5112 (rtp_receive_frequency / 1000);
niklase@google.com470e71d2011-07-07 08:21:25 +00005113
pwestin@webrtc.org1de01352013-04-11 20:23:35 +00005114 _previousTimestamp = rtp_timestamp;
niklase@google.com470e71d2011-07-07 08:21:25 +00005115
pwestin@webrtc.org1de01352013-04-11 20:23:35 +00005116 if (timestamp_diff_ms > (2 * kVoiceEngineMaxMinPlayoutDelayMs)) {
5117 timestamp_diff_ms = 0;
5118 }
niklase@google.com470e71d2011-07-07 08:21:25 +00005119
pwestin@webrtc.org1de01352013-04-11 20:23:35 +00005120 if (timestamp_diff_ms == 0) return;
niklase@google.com470e71d2011-07-07 08:21:25 +00005121
pwestin@webrtc.org1de01352013-04-11 20:23:35 +00005122 if (packet_delay_ms >= 10 && packet_delay_ms <= 60) {
5123 _recPacketDelayMs = packet_delay_ms;
5124 }
niklase@google.com470e71d2011-07-07 08:21:25 +00005125
pwestin@webrtc.org1de01352013-04-11 20:23:35 +00005126 if (_average_jitter_buffer_delay_us == 0) {
5127 _average_jitter_buffer_delay_us = timestamp_diff_ms * 1000;
5128 return;
5129 }
5130
5131 // Filter average delay value using exponential filter (alpha is
5132 // 7/8). We derive 1000 *_average_jitter_buffer_delay_us here (reduces
5133 // risk of rounding error) and compensate for it in GetDelayEstimate()
5134 // later.
5135 _average_jitter_buffer_delay_us = (_average_jitter_buffer_delay_us * 7 +
5136 1000 * timestamp_diff_ms + 500) / 8;
niklase@google.com470e71d2011-07-07 08:21:25 +00005137}
5138
5139void
5140Channel::RegisterReceiveCodecsToRTPModule()
5141{
5142 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId,_channelId),
5143 "Channel::RegisterReceiveCodecsToRTPModule()");
5144
5145
5146 CodecInst codec;
pbos@webrtc.org6141e132013-04-09 10:09:10 +00005147 const uint8_t nSupportedCodecs = AudioCodingModule::NumberOfCodecs();
niklase@google.com470e71d2011-07-07 08:21:25 +00005148
5149 for (int idx = 0; idx < nSupportedCodecs; idx++)
5150 {
5151 // Open up the RTP/RTCP receiver for all supported codecs
tina.legrand@webrtc.org7a7a0082013-02-21 10:27:48 +00005152 if ((_audioCodingModule.Codec(idx, &codec) == -1) ||
pwestin@webrtc.org2853dde2012-05-11 11:08:54 +00005153 (_rtpRtcpModule->RegisterReceivePayload(codec) == -1))
niklase@google.com470e71d2011-07-07 08:21:25 +00005154 {
5155 WEBRTC_TRACE(
5156 kTraceWarning,
5157 kTraceVoice,
5158 VoEId(_instanceId, _channelId),
5159 "Channel::RegisterReceiveCodecsToRTPModule() unable"
5160 " to register %s (%d/%d/%d/%d) to RTP/RTCP receiver",
5161 codec.plname, codec.pltype, codec.plfreq,
5162 codec.channels, codec.rate);
5163 }
5164 else
5165 {
5166 WEBRTC_TRACE(
5167 kTraceInfo,
5168 kTraceVoice,
5169 VoEId(_instanceId, _channelId),
5170 "Channel::RegisterReceiveCodecsToRTPModule() %s "
wu@webrtc.orgfcd12b32011-09-15 20:49:50 +00005171 "(%d/%d/%d/%d) has been added to the RTP/RTCP "
niklase@google.com470e71d2011-07-07 08:21:25 +00005172 "receiver",
5173 codec.plname, codec.pltype, codec.plfreq,
5174 codec.channels, codec.rate);
5175 }
5176 }
5177}
5178
andrew@webrtc.org50419b02012-11-14 19:07:54 +00005179int Channel::ApmProcessRx(AudioFrame& frame) {
5180 AudioProcessing* audioproc = _rxAudioProcessingModulePtr;
5181 // Register the (possibly new) frame parameters.
5182 if (audioproc->set_sample_rate_hz(frame.sample_rate_hz_) != 0) {
andrew@webrtc.org655d8f52012-11-20 07:34:45 +00005183 LOG_FERR1(LS_WARNING, set_sample_rate_hz, frame.sample_rate_hz_);
andrew@webrtc.org50419b02012-11-14 19:07:54 +00005184 }
5185 if (audioproc->set_num_channels(frame.num_channels_,
5186 frame.num_channels_) != 0) {
andrew@webrtc.org655d8f52012-11-20 07:34:45 +00005187 LOG_FERR1(LS_WARNING, set_num_channels, frame.num_channels_);
andrew@webrtc.org50419b02012-11-14 19:07:54 +00005188 }
5189 if (audioproc->ProcessStream(&frame) != 0) {
andrew@webrtc.org655d8f52012-11-20 07:34:45 +00005190 LOG_FERR0(LS_WARNING, ProcessStream);
andrew@webrtc.org50419b02012-11-14 19:07:54 +00005191 }
5192 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +00005193}
5194
turaj@webrtc.org42259e72012-12-11 02:15:12 +00005195int Channel::SetSecondarySendCodec(const CodecInst& codec,
5196 int red_payload_type) {
turaj@webrtc.org8c8ad852013-01-31 18:20:17 +00005197 // Sanity check for payload type.
5198 if (red_payload_type < 0 || red_payload_type > 127) {
5199 _engineStatisticsPtr->SetLastError(
5200 VE_PLTYPE_ERROR, kTraceError,
5201 "SetRedPayloadType() invalid RED payload type");
5202 return -1;
5203 }
5204
turaj@webrtc.org42259e72012-12-11 02:15:12 +00005205 if (SetRedPayloadType(red_payload_type) < 0) {
5206 _engineStatisticsPtr->SetLastError(
5207 VE_AUDIO_CODING_MODULE_ERROR, kTraceError,
5208 "SetSecondarySendCodec() Failed to register RED ACM");
5209 return -1;
5210 }
5211 if (_audioCodingModule.RegisterSecondarySendCodec(codec) < 0) {
5212 _engineStatisticsPtr->SetLastError(
5213 VE_AUDIO_CODING_MODULE_ERROR, kTraceError,
5214 "SetSecondarySendCodec() Failed to register secondary send codec in "
5215 "ACM");
5216 return -1;
5217 }
5218
5219 return 0;
5220}
5221
5222void Channel::RemoveSecondarySendCodec() {
5223 _audioCodingModule.UnregisterSecondarySendCodec();
5224}
5225
5226int Channel::GetSecondarySendCodec(CodecInst* codec) {
5227 if (_audioCodingModule.SecondarySendCodec(codec) < 0) {
5228 _engineStatisticsPtr->SetLastError(
5229 VE_AUDIO_CODING_MODULE_ERROR, kTraceError,
5230 "GetSecondarySendCodec() Failed to get secondary sent codec from ACM");
5231 return -1;
5232 }
5233 return 0;
5234}
5235
turaj@webrtc.org8c8ad852013-01-31 18:20:17 +00005236// Assuming this method is called with valid payload type.
turaj@webrtc.org42259e72012-12-11 02:15:12 +00005237int Channel::SetRedPayloadType(int red_payload_type) {
turaj@webrtc.org42259e72012-12-11 02:15:12 +00005238 CodecInst codec;
5239 bool found_red = false;
5240
5241 // Get default RED settings from the ACM database
5242 const int num_codecs = AudioCodingModule::NumberOfCodecs();
5243 for (int idx = 0; idx < num_codecs; idx++) {
tina.legrand@webrtc.org7a7a0082013-02-21 10:27:48 +00005244 _audioCodingModule.Codec(idx, &codec);
turaj@webrtc.org42259e72012-12-11 02:15:12 +00005245 if (!STR_CASE_CMP(codec.plname, "RED")) {
5246 found_red = true;
5247 break;
5248 }
5249 }
5250
5251 if (!found_red) {
5252 _engineStatisticsPtr->SetLastError(
5253 VE_CODEC_ERROR, kTraceError,
5254 "SetRedPayloadType() RED is not supported");
5255 return -1;
5256 }
5257
turaj@webrtc.org9d532fd2013-01-31 18:34:19 +00005258 codec.pltype = red_payload_type;
turaj@webrtc.org42259e72012-12-11 02:15:12 +00005259 if (_audioCodingModule.RegisterSendCodec(codec) < 0) {
5260 _engineStatisticsPtr->SetLastError(
5261 VE_AUDIO_CODING_MODULE_ERROR, kTraceError,
5262 "SetRedPayloadType() RED registration in ACM module failed");
5263 return -1;
5264 }
5265
5266 if (_rtpRtcpModule->SetSendREDPayloadType(red_payload_type) != 0) {
5267 _engineStatisticsPtr->SetLastError(
5268 VE_RTP_RTCP_MODULE_ERROR, kTraceError,
5269 "SetRedPayloadType() RED registration in RTP/RTCP module failed");
5270 return -1;
5271 }
5272 return 0;
5273}
5274
niklase@google.com470e71d2011-07-07 08:21:25 +00005275} // namespace voe
niklase@google.com470e71d2011-07-07 08:21:25 +00005276} // namespace webrtc