blob: 46b95ed948989ad04042500d8e9d0d8465c00167 [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
pbos@webrtc.org92135212013-05-14 08:31:39 +000097Channel::OnRxVadDetected(int vadDecision)
niklase@google.com470e71d2011-07-07 08:21:25 +000098{
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.org92135212013-05-14 08:31:39 +0000335Channel::OnPlayTelephoneEvent(int32_t id,
336 uint8_t event,
337 uint16_t lengthMs,
338 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.org92135212013-05-14 08:31:39 +0000359Channel::OnIncomingSSRCChanged(int32_t id,
360 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.org92135212013-05-14 08:31:39 +0000385void Channel::OnIncomingCSRCChanged(int32_t id,
386 uint32_t CSRC,
387 bool added)
niklase@google.com470e71d2011-07-07 08:21:25 +0000388{
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.org92135212013-05-14 08:31:39 +0000408Channel::OnApplicationDataReceived(int32_t id,
409 uint8_t subType,
410 uint32_t name,
411 uint16_t length,
pbos@webrtc.org6141e132013-04-09 10:09:10 +0000412 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.org92135212013-05-14 08:31:39 +0000439 int32_t id,
440 int8_t payloadType,
leozwang@webrtc.org813e4b02012-03-01 18:34:25 +0000441 const char payloadName[RTP_PAYLOAD_NAME_SIZE],
pbos@webrtc.org92135212013-05-14 08:31:39 +0000442 int frequency,
443 uint8_t channels,
444 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.org92135212013-05-14 08:31:39 +0000480Channel::OnPacketTimeout(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.org92135212013-05-14 08:31:39 +0000507Channel::OnReceivedPacket(int32_t id,
508 RtpRtcpPacketType packetType)
niklase@google.com470e71d2011-07-07 08:21:25 +0000509{
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.org92135212013-05-14 08:31:39 +0000539Channel::OnPeriodicDeadOrAlive(int32_t id,
540 RTPAliveType alive)
niklase@google.com470e71d2011-07-07 08:21:25 +0000541{
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,
pbos@webrtc.org92135212013-05-14 08:31:39 +0000593 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.org92135212013-05-14 08:31:39 +0000634int32_t Channel::GetAudioFrame(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
pbos@webrtc.org92135212013-05-14 08:31:39 +0000740Channel::NeededFrequency(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.org92135212013-05-14 08:31:39 +0000781 int32_t channelId,
782 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.org92135212013-05-14 08:31:39 +0000801Channel::PlayNotification(int32_t id, 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.org92135212013-05-14 08:31:39 +0000811Channel::RecordNotification(int32_t id, 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.org92135212013-05-14 08:31:39 +0000821Channel::PlayFileEnded(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.org92135212013-05-14 08:31:39 +0000849Channel::RecordFileEnded(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.org92135212013-05-14 08:31:39 +0000865Channel::Channel(int32_t channelId,
866 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),
stefan@webrtc.orga5cb98c2013-05-29 12:12:51 +0000871 rtp_header_parser_(RtpHeaderParser::Create()),
niklase@google.com470e71d2011-07-07 08:21:25 +0000872 _audioCodingModule(*AudioCodingModule::Create(
xians@google.com22963ab2011-08-03 12:40:23 +0000873 VoEModuleId(instanceId, channelId))),
niklase@google.com470e71d2011-07-07 08:21:25 +0000874 _rtpDumpIn(*RtpDump::CreateRtpDump()),
875 _rtpDumpOut(*RtpDump::CreateRtpDump()),
niklase@google.com470e71d2011-07-07 08:21:25 +0000876 _outputAudioLevel(),
niklase@google.com470e71d2011-07-07 08:21:25 +0000877 _externalTransport(false),
niklase@google.com470e71d2011-07-07 08:21:25 +0000878 _inputFilePlayerPtr(NULL),
879 _outputFilePlayerPtr(NULL),
880 _outputFileRecorderPtr(NULL),
881 // Avoid conflict with other channels by adding 1024 - 1026,
882 // won't use as much as 1024 channels.
883 _inputFilePlayerId(VoEModuleId(instanceId, channelId) + 1024),
884 _outputFilePlayerId(VoEModuleId(instanceId, channelId) + 1025),
885 _outputFileRecorderId(VoEModuleId(instanceId, channelId) + 1026),
886 _inputFilePlaying(false),
887 _outputFilePlaying(false),
888 _outputFileRecording(false),
xians@google.com22963ab2011-08-03 12:40:23 +0000889 _inbandDtmfQueue(VoEModuleId(instanceId, channelId)),
890 _inbandDtmfGenerator(VoEModuleId(instanceId, channelId)),
niklase@google.com470e71d2011-07-07 08:21:25 +0000891 _inputExternalMedia(false),
xians@google.com22963ab2011-08-03 12:40:23 +0000892 _outputExternalMedia(false),
niklase@google.com470e71d2011-07-07 08:21:25 +0000893 _inputExternalMediaCallbackPtr(NULL),
894 _outputExternalMediaCallbackPtr(NULL),
xians@google.com22963ab2011-08-03 12:40:23 +0000895 _encryptionRTPBufferPtr(NULL),
896 _decryptionRTPBufferPtr(NULL),
897 _encryptionRTCPBufferPtr(NULL),
898 _decryptionRTCPBufferPtr(NULL),
899 _timeStamp(0), // This is just an offset, RTP module will add it's own random offset
900 _sendTelephoneEventPayloadType(106),
pwestin@webrtc.org1de01352013-04-11 20:23:35 +0000901 playout_timestamp_rtp_(0),
902 playout_timestamp_rtcp_(0),
xians@google.com22963ab2011-08-03 12:40:23 +0000903 _numberOfDiscardedPackets(0),
904 _engineStatisticsPtr(NULL),
henrika@webrtc.org2919e952012-01-31 08:45:03 +0000905 _outputMixerPtr(NULL),
906 _transmitMixerPtr(NULL),
xians@google.com22963ab2011-08-03 12:40:23 +0000907 _moduleProcessThreadPtr(NULL),
908 _audioDeviceModulePtr(NULL),
909 _voiceEngineObserverPtr(NULL),
910 _callbackCritSectPtr(NULL),
911 _transportPtr(NULL),
912 _encryptionPtr(NULL),
andrew@webrtc.org755b04a2011-11-15 16:57:56 +0000913 _rtpAudioProc(NULL),
xians@google.com22963ab2011-08-03 12:40:23 +0000914 _rxAudioProcessingModulePtr(NULL),
xians@google.com22963ab2011-08-03 12:40:23 +0000915 _rxVadObserverPtr(NULL),
916 _oldVadDecision(-1),
917 _sendFrameType(0),
niklase@google.com470e71d2011-07-07 08:21:25 +0000918 _rtpObserverPtr(NULL),
919 _rtcpObserverPtr(NULL),
xians@google.com22963ab2011-08-03 12:40:23 +0000920 _outputIsOnHold(false),
921 _externalPlayout(false),
roosa@google.com1b60ceb2012-12-12 23:00:29 +0000922 _externalMixing(false),
xians@google.com22963ab2011-08-03 12:40:23 +0000923 _inputIsOnHold(false),
924 _playing(false),
925 _sending(false),
926 _receiving(false),
927 _mixFileWithMicrophone(false),
928 _rtpObserver(false),
929 _rtcpObserver(false),
niklase@google.com470e71d2011-07-07 08:21:25 +0000930 _mute(false),
931 _panLeft(1.0f),
932 _panRight(1.0f),
933 _outputGain(1.0f),
xians@google.com22963ab2011-08-03 12:40:23 +0000934 _encrypting(false),
935 _decrypting(false),
niklase@google.com470e71d2011-07-07 08:21:25 +0000936 _playOutbandDtmfEvent(false),
937 _playInbandDtmfEvent(false),
niklase@google.com470e71d2011-07-07 08:21:25 +0000938 _extraPayloadType(0),
939 _insertExtraRTPPacket(false),
940 _extraMarkerBit(false),
941 _lastLocalTimeStamp(0),
roosa@google.com0870f022012-12-12 21:31:41 +0000942 _lastRemoteTimeStamp(0),
niklase@google.com470e71d2011-07-07 08:21:25 +0000943 _lastPayloadType(0),
xians@google.com22963ab2011-08-03 12:40:23 +0000944 _includeAudioLevelIndication(false),
niklase@google.com470e71d2011-07-07 08:21:25 +0000945 _rtpPacketTimedOut(false),
946 _rtpPacketTimeOutIsEnabled(false),
947 _rtpTimeOutSeconds(0),
948 _connectionObserver(false),
949 _connectionObserverPtr(NULL),
950 _countAliveDetections(0),
951 _countDeadDetections(0),
952 _outputSpeechType(AudioFrame::kNormalSpeech),
pwestin@webrtc.org1de01352013-04-11 20:23:35 +0000953 _average_jitter_buffer_delay_us(0),
turaj@webrtc.orge46c8d32013-05-22 20:39:43 +0000954 least_required_delay_ms_(0),
niklase@google.com470e71d2011-07-07 08:21:25 +0000955 _previousTimestamp(0),
956 _recPacketDelayMs(20),
957 _RxVadDetection(false),
958 _rxApmIsEnabled(false),
959 _rxAgcIsEnabled(false),
xians@google.com22963ab2011-08-03 12:40:23 +0000960 _rxNsIsEnabled(false)
niklase@google.com470e71d2011-07-07 08:21:25 +0000961{
962 WEBRTC_TRACE(kTraceMemory, kTraceVoice, VoEId(_instanceId,_channelId),
963 "Channel::Channel() - ctor");
964 _inbandDtmfQueue.ResetDtmf();
965 _inbandDtmfGenerator.Init();
966 _outputAudioLevel.Clear();
967
pwestin@webrtc.org2853dde2012-05-11 11:08:54 +0000968 RtpRtcp::Configuration configuration;
969 configuration.id = VoEModuleId(instanceId, channelId);
970 configuration.audio = true;
971 configuration.incoming_data = this;
972 configuration.incoming_messages = this;
973 configuration.outgoing_transport = this;
974 configuration.rtcp_feedback = this;
975 configuration.audio_messages = this;
976
977 _rtpRtcpModule.reset(RtpRtcp::CreateRtpRtcp(configuration));
978
niklase@google.com470e71d2011-07-07 08:21:25 +0000979 // Create far end AudioProcessing Module
980 _rxAudioProcessingModulePtr = AudioProcessing::Create(
981 VoEModuleId(instanceId, channelId));
982}
983
984Channel::~Channel()
985{
986 WEBRTC_TRACE(kTraceMemory, kTraceVoice, VoEId(_instanceId,_channelId),
987 "Channel::~Channel() - dtor");
988
989 if (_outputExternalMedia)
990 {
991 DeRegisterExternalMediaProcessing(kPlaybackPerChannel);
992 }
993 if (_inputExternalMedia)
994 {
995 DeRegisterExternalMediaProcessing(kRecordingPerChannel);
996 }
997 StopSend();
niklase@google.com470e71d2011-07-07 08:21:25 +0000998 StopPlayout();
999
1000 {
mflodman@webrtc.org9a065d12012-03-07 08:12:21 +00001001 CriticalSectionScoped cs(&_fileCritSect);
niklase@google.com470e71d2011-07-07 08:21:25 +00001002 if (_inputFilePlayerPtr)
1003 {
1004 _inputFilePlayerPtr->RegisterModuleFileCallback(NULL);
1005 _inputFilePlayerPtr->StopPlayingFile();
1006 FilePlayer::DestroyFilePlayer(_inputFilePlayerPtr);
1007 _inputFilePlayerPtr = NULL;
1008 }
1009 if (_outputFilePlayerPtr)
1010 {
1011 _outputFilePlayerPtr->RegisterModuleFileCallback(NULL);
1012 _outputFilePlayerPtr->StopPlayingFile();
1013 FilePlayer::DestroyFilePlayer(_outputFilePlayerPtr);
1014 _outputFilePlayerPtr = NULL;
1015 }
1016 if (_outputFileRecorderPtr)
1017 {
1018 _outputFileRecorderPtr->RegisterModuleFileCallback(NULL);
1019 _outputFileRecorderPtr->StopRecording();
1020 FileRecorder::DestroyFileRecorder(_outputFileRecorderPtr);
1021 _outputFileRecorderPtr = NULL;
1022 }
1023 }
1024
1025 // The order to safely shutdown modules in a channel is:
1026 // 1. De-register callbacks in modules
1027 // 2. De-register modules in process thread
1028 // 3. Destroy modules
niklase@google.com470e71d2011-07-07 08:21:25 +00001029 if (_audioCodingModule.RegisterTransportCallback(NULL) == -1)
1030 {
1031 WEBRTC_TRACE(kTraceWarning, kTraceVoice,
1032 VoEId(_instanceId,_channelId),
1033 "~Channel() failed to de-register transport callback"
1034 " (Audio coding module)");
1035 }
1036 if (_audioCodingModule.RegisterVADCallback(NULL) == -1)
1037 {
1038 WEBRTC_TRACE(kTraceWarning, kTraceVoice,
1039 VoEId(_instanceId,_channelId),
1040 "~Channel() failed to de-register VAD callback"
1041 " (Audio coding module)");
1042 }
niklase@google.com470e71d2011-07-07 08:21:25 +00001043 // De-register modules in process thread
pwestin@webrtc.org2853dde2012-05-11 11:08:54 +00001044 if (_moduleProcessThreadPtr->DeRegisterModule(_rtpRtcpModule.get()) == -1)
niklase@google.com470e71d2011-07-07 08:21:25 +00001045 {
1046 WEBRTC_TRACE(kTraceInfo, kTraceVoice,
1047 VoEId(_instanceId,_channelId),
1048 "~Channel() failed to deregister RTP/RTCP module");
1049 }
1050
1051 // Destroy modules
niklase@google.com470e71d2011-07-07 08:21:25 +00001052 AudioCodingModule::Destroy(&_audioCodingModule);
niklase@google.com470e71d2011-07-07 08:21:25 +00001053 if (_rxAudioProcessingModulePtr != NULL)
1054 {
1055 AudioProcessing::Destroy(_rxAudioProcessingModulePtr); // far end APM
1056 _rxAudioProcessingModulePtr = NULL;
1057 }
1058
1059 // End of modules shutdown
1060
1061 // Delete other objects
1062 RtpDump::DestroyRtpDump(&_rtpDumpIn);
1063 RtpDump::DestroyRtpDump(&_rtpDumpOut);
1064 delete [] _encryptionRTPBufferPtr;
1065 delete [] _decryptionRTPBufferPtr;
1066 delete [] _encryptionRTCPBufferPtr;
1067 delete [] _decryptionRTCPBufferPtr;
1068 delete &_callbackCritSect;
niklase@google.com470e71d2011-07-07 08:21:25 +00001069 delete &_fileCritSect;
1070}
1071
pbos@webrtc.org6141e132013-04-09 10:09:10 +00001072int32_t
niklase@google.com470e71d2011-07-07 08:21:25 +00001073Channel::Init()
1074{
1075 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId,_channelId),
1076 "Channel::Init()");
1077
1078 // --- Initial sanity
1079
1080 if ((_engineStatisticsPtr == NULL) ||
1081 (_moduleProcessThreadPtr == NULL))
1082 {
1083 WEBRTC_TRACE(kTraceError, kTraceVoice,
1084 VoEId(_instanceId,_channelId),
1085 "Channel::Init() must call SetEngineInformation() first");
1086 return -1;
1087 }
1088
1089 // --- Add modules to process thread (for periodic schedulation)
1090
1091 const bool processThreadFail =
pwestin@webrtc.org2853dde2012-05-11 11:08:54 +00001092 ((_moduleProcessThreadPtr->RegisterModule(_rtpRtcpModule.get()) != 0) ||
niklase@google.com470e71d2011-07-07 08:21:25 +00001093 false);
niklase@google.com470e71d2011-07-07 08:21:25 +00001094 if (processThreadFail)
1095 {
1096 _engineStatisticsPtr->SetLastError(
1097 VE_CANNOT_INIT_CHANNEL, kTraceError,
1098 "Channel::Init() modules not registered");
1099 return -1;
1100 }
pwestin@webrtc.orgc450a192012-01-04 15:00:12 +00001101 // --- ACM initialization
niklase@google.com470e71d2011-07-07 08:21:25 +00001102
1103 if ((_audioCodingModule.InitializeReceiver() == -1) ||
1104#ifdef WEBRTC_CODEC_AVT
1105 // out-of-band Dtmf tones are played out by default
1106 (_audioCodingModule.SetDtmfPlayoutStatus(true) == -1) ||
1107#endif
niklase@google.com470e71d2011-07-07 08:21:25 +00001108 (_audioCodingModule.InitializeSender() == -1))
1109 {
1110 _engineStatisticsPtr->SetLastError(
1111 VE_AUDIO_CODING_MODULE_ERROR, kTraceError,
1112 "Channel::Init() unable to initialize the ACM - 1");
1113 return -1;
1114 }
1115
1116 // --- RTP/RTCP module initialization
1117
1118 // Ensure that RTCP is enabled by default for the created channel.
1119 // Note that, the module will keep generating RTCP until it is explicitly
1120 // disabled by the user.
1121 // After StopListen (when no sockets exists), RTCP packets will no longer
1122 // be transmitted since the Transport object will then be invalid.
1123
1124 const bool rtpRtcpFail =
turaj@webrtc.orgb7edd062013-03-12 22:27:27 +00001125 ((_rtpRtcpModule->SetTelephoneEventForwardToDecoder(true) == -1) ||
niklase@google.com470e71d2011-07-07 08:21:25 +00001126 // RTCP is enabled by default
pwestin@webrtc.org2853dde2012-05-11 11:08:54 +00001127 (_rtpRtcpModule->SetRTCPStatus(kRtcpCompound) == -1));
niklase@google.com470e71d2011-07-07 08:21:25 +00001128 if (rtpRtcpFail)
1129 {
1130 _engineStatisticsPtr->SetLastError(
1131 VE_RTP_RTCP_MODULE_ERROR, kTraceError,
1132 "Channel::Init() RTP/RTCP module not initialized");
1133 return -1;
1134 }
1135
1136 // --- Register all permanent callbacks
niklase@google.com470e71d2011-07-07 08:21:25 +00001137 const bool fail =
niklase@google.com470e71d2011-07-07 08:21:25 +00001138 (_audioCodingModule.RegisterTransportCallback(this) == -1) ||
1139 (_audioCodingModule.RegisterVADCallback(this) == -1);
1140
1141 if (fail)
1142 {
1143 _engineStatisticsPtr->SetLastError(
1144 VE_CANNOT_INIT_CHANNEL, kTraceError,
1145 "Channel::Init() callbacks not registered");
1146 return -1;
1147 }
1148
1149 // --- Register all supported codecs to the receiving side of the
1150 // RTP/RTCP module
1151
1152 CodecInst codec;
pbos@webrtc.org6141e132013-04-09 10:09:10 +00001153 const uint8_t nSupportedCodecs = AudioCodingModule::NumberOfCodecs();
niklase@google.com470e71d2011-07-07 08:21:25 +00001154
1155 for (int idx = 0; idx < nSupportedCodecs; idx++)
1156 {
1157 // Open up the RTP/RTCP receiver for all supported codecs
tina.legrand@webrtc.org7a7a0082013-02-21 10:27:48 +00001158 if ((_audioCodingModule.Codec(idx, &codec) == -1) ||
pwestin@webrtc.org2853dde2012-05-11 11:08:54 +00001159 (_rtpRtcpModule->RegisterReceivePayload(codec) == -1))
niklase@google.com470e71d2011-07-07 08:21:25 +00001160 {
1161 WEBRTC_TRACE(kTraceWarning, kTraceVoice,
1162 VoEId(_instanceId,_channelId),
1163 "Channel::Init() unable to register %s (%d/%d/%d/%d) "
1164 "to RTP/RTCP receiver",
1165 codec.plname, codec.pltype, codec.plfreq,
1166 codec.channels, codec.rate);
1167 }
1168 else
1169 {
1170 WEBRTC_TRACE(kTraceInfo, kTraceVoice,
1171 VoEId(_instanceId,_channelId),
1172 "Channel::Init() %s (%d/%d/%d/%d) has been added to "
1173 "the RTP/RTCP receiver",
1174 codec.plname, codec.pltype, codec.plfreq,
1175 codec.channels, codec.rate);
1176 }
1177
1178 // Ensure that PCMU is used as default codec on the sending side
tina.legrand@webrtc.org45175852012-06-01 09:27:35 +00001179 if (!STR_CASE_CMP(codec.plname, "PCMU") && (codec.channels == 1))
niklase@google.com470e71d2011-07-07 08:21:25 +00001180 {
1181 SetSendCodec(codec);
1182 }
1183
1184 // Register default PT for outband 'telephone-event'
1185 if (!STR_CASE_CMP(codec.plname, "telephone-event"))
1186 {
pwestin@webrtc.org2853dde2012-05-11 11:08:54 +00001187 if ((_rtpRtcpModule->RegisterSendPayload(codec) == -1) ||
niklase@google.com470e71d2011-07-07 08:21:25 +00001188 (_audioCodingModule.RegisterReceiveCodec(codec) == -1))
1189 {
1190 WEBRTC_TRACE(kTraceWarning, kTraceVoice,
1191 VoEId(_instanceId,_channelId),
1192 "Channel::Init() failed to register outband "
1193 "'telephone-event' (%d/%d) correctly",
1194 codec.pltype, codec.plfreq);
1195 }
1196 }
1197
1198 if (!STR_CASE_CMP(codec.plname, "CN"))
1199 {
1200 if ((_audioCodingModule.RegisterSendCodec(codec) == -1) ||
1201 (_audioCodingModule.RegisterReceiveCodec(codec) == -1) ||
pwestin@webrtc.org2853dde2012-05-11 11:08:54 +00001202 (_rtpRtcpModule->RegisterSendPayload(codec) == -1))
niklase@google.com470e71d2011-07-07 08:21:25 +00001203 {
1204 WEBRTC_TRACE(kTraceWarning, kTraceVoice,
1205 VoEId(_instanceId,_channelId),
1206 "Channel::Init() failed to register CN (%d/%d) "
1207 "correctly - 1",
1208 codec.pltype, codec.plfreq);
1209 }
1210 }
1211#ifdef WEBRTC_CODEC_RED
1212 // Register RED to the receiving side of the ACM.
1213 // We will not receive an OnInitializeDecoder() callback for RED.
1214 if (!STR_CASE_CMP(codec.plname, "RED"))
1215 {
1216 if (_audioCodingModule.RegisterReceiveCodec(codec) == -1)
1217 {
1218 WEBRTC_TRACE(kTraceWarning, kTraceVoice,
1219 VoEId(_instanceId,_channelId),
1220 "Channel::Init() failed to register RED (%d/%d) "
1221 "correctly",
1222 codec.pltype, codec.plfreq);
1223 }
1224 }
1225#endif
1226 }
pwestin@webrtc.org684f0572013-03-13 23:20:57 +00001227
niklase@google.com470e71d2011-07-07 08:21:25 +00001228 // Initialize the far end AP module
1229 // Using 8 kHz as initial Fs, the same as in transmission. Might be
1230 // changed at the first receiving audio.
1231 if (_rxAudioProcessingModulePtr == NULL)
1232 {
1233 _engineStatisticsPtr->SetLastError(
1234 VE_NO_MEMORY, kTraceCritical,
1235 "Channel::Init() failed to create the far-end AudioProcessing"
1236 " module");
1237 return -1;
1238 }
1239
niklase@google.com470e71d2011-07-07 08:21:25 +00001240 if (_rxAudioProcessingModulePtr->set_sample_rate_hz(8000))
1241 {
1242 _engineStatisticsPtr->SetLastError(
1243 VE_APM_ERROR, kTraceWarning,
1244 "Channel::Init() failed to set the sample rate to 8K for"
1245 " far-end AP module");
1246 }
1247
1248 if (_rxAudioProcessingModulePtr->set_num_channels(1, 1) != 0)
1249 {
1250 _engineStatisticsPtr->SetLastError(
1251 VE_SOUNDCARD_ERROR, kTraceWarning,
andrew@webrtc.org755b04a2011-11-15 16:57:56 +00001252 "Init() failed to set channels for the primary audio stream");
niklase@google.com470e71d2011-07-07 08:21:25 +00001253 }
1254
1255 if (_rxAudioProcessingModulePtr->high_pass_filter()->Enable(
1256 WEBRTC_VOICE_ENGINE_RX_HP_DEFAULT_STATE) != 0)
1257 {
1258 _engineStatisticsPtr->SetLastError(
1259 VE_APM_ERROR, kTraceWarning,
1260 "Channel::Init() failed to set the high-pass filter for"
wu@webrtc.orgfcd12b32011-09-15 20:49:50 +00001261 " far-end AP module");
niklase@google.com470e71d2011-07-07 08:21:25 +00001262 }
1263
1264 if (_rxAudioProcessingModulePtr->noise_suppression()->set_level(
1265 (NoiseSuppression::Level)WEBRTC_VOICE_ENGINE_RX_NS_DEFAULT_MODE) != 0)
1266 {
1267 _engineStatisticsPtr->SetLastError(
1268 VE_APM_ERROR, kTraceWarning,
1269 "Init() failed to set noise reduction level for far-end"
wu@webrtc.orgfcd12b32011-09-15 20:49:50 +00001270 " AP module");
niklase@google.com470e71d2011-07-07 08:21:25 +00001271 }
1272 if (_rxAudioProcessingModulePtr->noise_suppression()->Enable(
1273 WEBRTC_VOICE_ENGINE_RX_NS_DEFAULT_STATE) != 0)
1274 {
1275 _engineStatisticsPtr->SetLastError(
1276 VE_APM_ERROR, kTraceWarning,
1277 "Init() failed to set noise reduction state for far-end"
wu@webrtc.orgfcd12b32011-09-15 20:49:50 +00001278 " AP module");
niklase@google.com470e71d2011-07-07 08:21:25 +00001279 }
1280
1281 if (_rxAudioProcessingModulePtr->gain_control()->set_mode(
1282 (GainControl::Mode)WEBRTC_VOICE_ENGINE_RX_AGC_DEFAULT_MODE) != 0)
1283 {
1284 _engineStatisticsPtr->SetLastError(
1285 VE_APM_ERROR, kTraceWarning,
1286 "Init() failed to set AGC mode for far-end AP module");
1287 }
1288 if (_rxAudioProcessingModulePtr->gain_control()->Enable(
1289 WEBRTC_VOICE_ENGINE_RX_AGC_DEFAULT_STATE) != 0)
1290 {
1291 _engineStatisticsPtr->SetLastError(
1292 VE_APM_ERROR, kTraceWarning,
1293 "Init() failed to set AGC state for far-end AP module");
1294 }
1295
1296 return 0;
1297}
1298
pbos@webrtc.org6141e132013-04-09 10:09:10 +00001299int32_t
niklase@google.com470e71d2011-07-07 08:21:25 +00001300Channel::SetEngineInformation(Statistics& engineStatistics,
1301 OutputMixer& outputMixer,
1302 voe::TransmitMixer& transmitMixer,
1303 ProcessThread& moduleProcessThread,
1304 AudioDeviceModule& audioDeviceModule,
1305 VoiceEngineObserver* voiceEngineObserver,
1306 CriticalSectionWrapper* callbackCritSect)
1307{
1308 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId,_channelId),
1309 "Channel::SetEngineInformation()");
1310 _engineStatisticsPtr = &engineStatistics;
1311 _outputMixerPtr = &outputMixer;
1312 _transmitMixerPtr = &transmitMixer,
1313 _moduleProcessThreadPtr = &moduleProcessThread;
1314 _audioDeviceModulePtr = &audioDeviceModule;
1315 _voiceEngineObserverPtr = voiceEngineObserver;
1316 _callbackCritSectPtr = callbackCritSect;
1317 return 0;
1318}
1319
pbos@webrtc.org6141e132013-04-09 10:09:10 +00001320int32_t
niklase@google.com470e71d2011-07-07 08:21:25 +00001321Channel::UpdateLocalTimeStamp()
1322{
1323
andrew@webrtc.org63a50982012-05-02 23:56:37 +00001324 _timeStamp += _audioFrame.samples_per_channel_;
niklase@google.com470e71d2011-07-07 08:21:25 +00001325 return 0;
1326}
1327
pbos@webrtc.org6141e132013-04-09 10:09:10 +00001328int32_t
niklase@google.com470e71d2011-07-07 08:21:25 +00001329Channel::StartPlayout()
1330{
1331 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId,_channelId),
1332 "Channel::StartPlayout()");
1333 if (_playing)
1334 {
1335 return 0;
1336 }
roosa@google.com1b60ceb2012-12-12 23:00:29 +00001337
1338 if (!_externalMixing) {
1339 // Add participant as candidates for mixing.
1340 if (_outputMixerPtr->SetMixabilityStatus(*this, true) != 0)
1341 {
1342 _engineStatisticsPtr->SetLastError(
1343 VE_AUDIO_CONF_MIX_MODULE_ERROR, kTraceError,
1344 "StartPlayout() failed to add participant to mixer");
1345 return -1;
1346 }
niklase@google.com470e71d2011-07-07 08:21:25 +00001347 }
1348
1349 _playing = true;
braveyao@webrtc.orgab129902012-06-04 03:26:39 +00001350
1351 if (RegisterFilePlayingToMixer() != 0)
1352 return -1;
1353
niklase@google.com470e71d2011-07-07 08:21:25 +00001354 return 0;
1355}
1356
pbos@webrtc.org6141e132013-04-09 10:09:10 +00001357int32_t
niklase@google.com470e71d2011-07-07 08:21:25 +00001358Channel::StopPlayout()
1359{
1360 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId,_channelId),
1361 "Channel::StopPlayout()");
1362 if (!_playing)
1363 {
1364 return 0;
1365 }
roosa@google.com1b60ceb2012-12-12 23:00:29 +00001366
1367 if (!_externalMixing) {
1368 // Remove participant as candidates for mixing
1369 if (_outputMixerPtr->SetMixabilityStatus(*this, false) != 0)
1370 {
1371 _engineStatisticsPtr->SetLastError(
1372 VE_AUDIO_CONF_MIX_MODULE_ERROR, kTraceError,
1373 "StopPlayout() failed to remove participant from mixer");
1374 return -1;
1375 }
niklase@google.com470e71d2011-07-07 08:21:25 +00001376 }
1377
1378 _playing = false;
1379 _outputAudioLevel.Clear();
1380
1381 return 0;
1382}
1383
pbos@webrtc.org6141e132013-04-09 10:09:10 +00001384int32_t
niklase@google.com470e71d2011-07-07 08:21:25 +00001385Channel::StartSend()
1386{
1387 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId,_channelId),
1388 "Channel::StartSend()");
niklase@google.com470e71d2011-07-07 08:21:25 +00001389 {
xians@webrtc.orge07247a2011-11-28 16:31:28 +00001390 // A lock is needed because |_sending| can be accessed or modified by
1391 // another thread at the same time.
mflodman@webrtc.org9a065d12012-03-07 08:12:21 +00001392 CriticalSectionScoped cs(&_callbackCritSect);
xians@webrtc.orge07247a2011-11-28 16:31:28 +00001393
1394 if (_sending)
1395 {
1396 return 0;
1397 }
1398 _sending = true;
niklase@google.com470e71d2011-07-07 08:21:25 +00001399 }
xians@webrtc.orge07247a2011-11-28 16:31:28 +00001400
pwestin@webrtc.org2853dde2012-05-11 11:08:54 +00001401 if (_rtpRtcpModule->SetSendingStatus(true) != 0)
niklase@google.com470e71d2011-07-07 08:21:25 +00001402 {
1403 _engineStatisticsPtr->SetLastError(
1404 VE_RTP_RTCP_MODULE_ERROR, kTraceError,
1405 "StartSend() RTP/RTCP failed to start sending");
mflodman@webrtc.org9a065d12012-03-07 08:12:21 +00001406 CriticalSectionScoped cs(&_callbackCritSect);
xians@webrtc.orge07247a2011-11-28 16:31:28 +00001407 _sending = false;
niklase@google.com470e71d2011-07-07 08:21:25 +00001408 return -1;
1409 }
xians@webrtc.orge07247a2011-11-28 16:31:28 +00001410
niklase@google.com470e71d2011-07-07 08:21:25 +00001411 return 0;
1412}
1413
pbos@webrtc.org6141e132013-04-09 10:09:10 +00001414int32_t
niklase@google.com470e71d2011-07-07 08:21:25 +00001415Channel::StopSend()
1416{
1417 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId,_channelId),
1418 "Channel::StopSend()");
niklase@google.com470e71d2011-07-07 08:21:25 +00001419 {
xians@webrtc.orge07247a2011-11-28 16:31:28 +00001420 // A lock is needed because |_sending| can be accessed or modified by
1421 // another thread at the same time.
mflodman@webrtc.org9a065d12012-03-07 08:12:21 +00001422 CriticalSectionScoped cs(&_callbackCritSect);
xians@webrtc.orge07247a2011-11-28 16:31:28 +00001423
1424 if (!_sending)
1425 {
1426 return 0;
1427 }
1428 _sending = false;
niklase@google.com470e71d2011-07-07 08:21:25 +00001429 }
xians@webrtc.orge07247a2011-11-28 16:31:28 +00001430
niklase@google.com470e71d2011-07-07 08:21:25 +00001431 // Reset sending SSRC and sequence number and triggers direct transmission
1432 // of RTCP BYE
pwestin@webrtc.org2853dde2012-05-11 11:08:54 +00001433 if (_rtpRtcpModule->SetSendingStatus(false) == -1 ||
1434 _rtpRtcpModule->ResetSendDataCountersRTP() == -1)
niklase@google.com470e71d2011-07-07 08:21:25 +00001435 {
1436 _engineStatisticsPtr->SetLastError(
1437 VE_RTP_RTCP_MODULE_ERROR, kTraceWarning,
1438 "StartSend() RTP/RTCP failed to stop sending");
1439 }
1440
niklase@google.com470e71d2011-07-07 08:21:25 +00001441 return 0;
1442}
1443
pbos@webrtc.org6141e132013-04-09 10:09:10 +00001444int32_t
niklase@google.com470e71d2011-07-07 08:21:25 +00001445Channel::StartReceiving()
1446{
1447 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId,_channelId),
1448 "Channel::StartReceiving()");
1449 if (_receiving)
1450 {
1451 return 0;
1452 }
niklase@google.com470e71d2011-07-07 08:21:25 +00001453 _receiving = true;
1454 _numberOfDiscardedPackets = 0;
1455 return 0;
1456}
1457
pbos@webrtc.org6141e132013-04-09 10:09:10 +00001458int32_t
niklase@google.com470e71d2011-07-07 08:21:25 +00001459Channel::StopReceiving()
1460{
1461 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId,_channelId),
1462 "Channel::StopReceiving()");
1463 if (!_receiving)
1464 {
1465 return 0;
1466 }
pwestin@webrtc.org684f0572013-03-13 23:20:57 +00001467
henrika@webrtc.orgaf71f0e2011-12-05 07:02:22 +00001468 // Recover DTMF detection status.
pbos@webrtc.org6141e132013-04-09 10:09:10 +00001469 int32_t ret = _rtpRtcpModule->SetTelephoneEventForwardToDecoder(true);
henrika@webrtc.orgaf71f0e2011-12-05 07:02:22 +00001470 if (ret != 0) {
1471 _engineStatisticsPtr->SetLastError(
1472 VE_INVALID_OPERATION, kTraceWarning,
1473 "StopReceiving() failed to restore telephone-event status.");
1474 }
niklase@google.com470e71d2011-07-07 08:21:25 +00001475 RegisterReceiveCodecsToRTPModule();
1476 _receiving = false;
1477 return 0;
1478}
1479
pbos@webrtc.org6141e132013-04-09 10:09:10 +00001480int32_t
niklase@google.com470e71d2011-07-07 08:21:25 +00001481Channel::SetNetEQPlayoutMode(NetEqModes mode)
1482{
1483 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId,_channelId),
1484 "Channel::SetNetEQPlayoutMode()");
1485 AudioPlayoutMode playoutMode(voice);
1486 switch (mode)
1487 {
1488 case kNetEqDefault:
1489 playoutMode = voice;
1490 break;
1491 case kNetEqStreaming:
1492 playoutMode = streaming;
1493 break;
1494 case kNetEqFax:
1495 playoutMode = fax;
1496 break;
roosa@google.comb7186192012-12-12 21:59:14 +00001497 case kNetEqOff:
1498 playoutMode = off;
1499 break;
niklase@google.com470e71d2011-07-07 08:21:25 +00001500 }
1501 if (_audioCodingModule.SetPlayoutMode(playoutMode) != 0)
1502 {
1503 _engineStatisticsPtr->SetLastError(
1504 VE_AUDIO_CODING_MODULE_ERROR, kTraceError,
1505 "SetNetEQPlayoutMode() failed to set playout mode");
1506 return -1;
1507 }
1508 return 0;
1509}
1510
pbos@webrtc.org6141e132013-04-09 10:09:10 +00001511int32_t
niklase@google.com470e71d2011-07-07 08:21:25 +00001512Channel::GetNetEQPlayoutMode(NetEqModes& mode)
1513{
1514 const AudioPlayoutMode playoutMode = _audioCodingModule.PlayoutMode();
1515 switch (playoutMode)
1516 {
1517 case voice:
1518 mode = kNetEqDefault;
1519 break;
1520 case streaming:
1521 mode = kNetEqStreaming;
1522 break;
1523 case fax:
1524 mode = kNetEqFax;
1525 break;
roosa@google.comb7186192012-12-12 21:59:14 +00001526 case off:
1527 mode = kNetEqOff;
niklase@google.com470e71d2011-07-07 08:21:25 +00001528 }
1529 WEBRTC_TRACE(kTraceStateInfo, kTraceVoice,
1530 VoEId(_instanceId,_channelId),
1531 "Channel::GetNetEQPlayoutMode() => mode=%u", mode);
1532 return 0;
1533}
1534
pbos@webrtc.org6141e132013-04-09 10:09:10 +00001535int32_t
niklase@google.com470e71d2011-07-07 08:21:25 +00001536Channel::SetOnHoldStatus(bool enable, OnHoldModes mode)
1537{
1538 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId,_channelId),
1539 "Channel::SetOnHoldStatus()");
1540 if (mode == kHoldSendAndPlay)
1541 {
1542 _outputIsOnHold = enable;
1543 _inputIsOnHold = enable;
1544 }
1545 else if (mode == kHoldPlayOnly)
1546 {
1547 _outputIsOnHold = enable;
1548 }
1549 if (mode == kHoldSendOnly)
1550 {
1551 _inputIsOnHold = enable;
1552 }
1553 return 0;
1554}
1555
pbos@webrtc.org6141e132013-04-09 10:09:10 +00001556int32_t
niklase@google.com470e71d2011-07-07 08:21:25 +00001557Channel::GetOnHoldStatus(bool& enabled, OnHoldModes& mode)
1558{
1559 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId,_channelId),
1560 "Channel::GetOnHoldStatus()");
1561 enabled = (_outputIsOnHold || _inputIsOnHold);
1562 if (_outputIsOnHold && _inputIsOnHold)
1563 {
1564 mode = kHoldSendAndPlay;
1565 }
1566 else if (_outputIsOnHold && !_inputIsOnHold)
1567 {
1568 mode = kHoldPlayOnly;
1569 }
1570 else if (!_outputIsOnHold && _inputIsOnHold)
1571 {
1572 mode = kHoldSendOnly;
1573 }
1574 WEBRTC_TRACE(kTraceStateInfo, kTraceVoice, VoEId(_instanceId,_channelId),
1575 "Channel::GetOnHoldStatus() => enabled=%d, mode=%d",
1576 enabled, mode);
1577 return 0;
1578}
1579
pbos@webrtc.org6141e132013-04-09 10:09:10 +00001580int32_t
niklase@google.com470e71d2011-07-07 08:21:25 +00001581Channel::RegisterVoiceEngineObserver(VoiceEngineObserver& observer)
1582{
1583 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId,_channelId),
1584 "Channel::RegisterVoiceEngineObserver()");
mflodman@webrtc.org9a065d12012-03-07 08:12:21 +00001585 CriticalSectionScoped cs(&_callbackCritSect);
niklase@google.com470e71d2011-07-07 08:21:25 +00001586
1587 if (_voiceEngineObserverPtr)
1588 {
1589 _engineStatisticsPtr->SetLastError(
1590 VE_INVALID_OPERATION, kTraceError,
1591 "RegisterVoiceEngineObserver() observer already enabled");
1592 return -1;
1593 }
1594 _voiceEngineObserverPtr = &observer;
1595 return 0;
1596}
1597
pbos@webrtc.org6141e132013-04-09 10:09:10 +00001598int32_t
niklase@google.com470e71d2011-07-07 08:21:25 +00001599Channel::DeRegisterVoiceEngineObserver()
1600{
1601 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId,_channelId),
1602 "Channel::DeRegisterVoiceEngineObserver()");
mflodman@webrtc.org9a065d12012-03-07 08:12:21 +00001603 CriticalSectionScoped cs(&_callbackCritSect);
niklase@google.com470e71d2011-07-07 08:21:25 +00001604
1605 if (!_voiceEngineObserverPtr)
1606 {
1607 _engineStatisticsPtr->SetLastError(
1608 VE_INVALID_OPERATION, kTraceWarning,
1609 "DeRegisterVoiceEngineObserver() observer already disabled");
1610 return 0;
1611 }
1612 _voiceEngineObserverPtr = NULL;
1613 return 0;
1614}
1615
pbos@webrtc.org6141e132013-04-09 10:09:10 +00001616int32_t
niklase@google.com470e71d2011-07-07 08:21:25 +00001617Channel::GetSendCodec(CodecInst& codec)
1618{
tina.legrand@webrtc.org7a7a0082013-02-21 10:27:48 +00001619 return (_audioCodingModule.SendCodec(&codec));
niklase@google.com470e71d2011-07-07 08:21:25 +00001620}
1621
pbos@webrtc.org6141e132013-04-09 10:09:10 +00001622int32_t
niklase@google.com470e71d2011-07-07 08:21:25 +00001623Channel::GetRecCodec(CodecInst& codec)
1624{
tina.legrand@webrtc.org7a7a0082013-02-21 10:27:48 +00001625 return (_audioCodingModule.ReceiveCodec(&codec));
niklase@google.com470e71d2011-07-07 08:21:25 +00001626}
1627
pbos@webrtc.org6141e132013-04-09 10:09:10 +00001628int32_t
niklase@google.com470e71d2011-07-07 08:21:25 +00001629Channel::SetSendCodec(const CodecInst& codec)
1630{
1631 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId,_channelId),
1632 "Channel::SetSendCodec()");
1633
1634 if (_audioCodingModule.RegisterSendCodec(codec) != 0)
1635 {
1636 WEBRTC_TRACE(kTraceError, kTraceVoice, VoEId(_instanceId,_channelId),
1637 "SetSendCodec() failed to register codec to ACM");
1638 return -1;
1639 }
1640
pwestin@webrtc.org2853dde2012-05-11 11:08:54 +00001641 if (_rtpRtcpModule->RegisterSendPayload(codec) != 0)
niklase@google.com470e71d2011-07-07 08:21:25 +00001642 {
pwestin@webrtc.org2853dde2012-05-11 11:08:54 +00001643 _rtpRtcpModule->DeRegisterSendPayload(codec.pltype);
1644 if (_rtpRtcpModule->RegisterSendPayload(codec) != 0)
niklase@google.com470e71d2011-07-07 08:21:25 +00001645 {
1646 WEBRTC_TRACE(
1647 kTraceError, kTraceVoice, VoEId(_instanceId,_channelId),
1648 "SetSendCodec() failed to register codec to"
1649 " RTP/RTCP module");
1650 return -1;
1651 }
1652 }
1653
pwestin@webrtc.org2853dde2012-05-11 11:08:54 +00001654 if (_rtpRtcpModule->SetAudioPacketSize(codec.pacsize) != 0)
niklase@google.com470e71d2011-07-07 08:21:25 +00001655 {
1656 WEBRTC_TRACE(kTraceError, kTraceVoice, VoEId(_instanceId,_channelId),
1657 "SetSendCodec() failed to set audio packet size");
1658 return -1;
1659 }
1660
1661 return 0;
1662}
1663
pbos@webrtc.org6141e132013-04-09 10:09:10 +00001664int32_t
niklase@google.com470e71d2011-07-07 08:21:25 +00001665Channel::SetVADStatus(bool enableVAD, ACMVADMode mode, bool disableDTX)
1666{
1667 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId,_channelId),
1668 "Channel::SetVADStatus(mode=%d)", mode);
1669 // To disable VAD, DTX must be disabled too
1670 disableDTX = ((enableVAD == false) ? true : disableDTX);
1671 if (_audioCodingModule.SetVAD(!disableDTX, enableVAD, mode) != 0)
1672 {
1673 _engineStatisticsPtr->SetLastError(
1674 VE_AUDIO_CODING_MODULE_ERROR, kTraceError,
1675 "SetVADStatus() failed to set VAD");
1676 return -1;
1677 }
1678 return 0;
1679}
1680
pbos@webrtc.org6141e132013-04-09 10:09:10 +00001681int32_t
niklase@google.com470e71d2011-07-07 08:21:25 +00001682Channel::GetVADStatus(bool& enabledVAD, ACMVADMode& mode, bool& disabledDTX)
1683{
1684 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId,_channelId),
1685 "Channel::GetVADStatus");
tina.legrand@webrtc.org7a7a0082013-02-21 10:27:48 +00001686 if (_audioCodingModule.VAD(&disabledDTX, &enabledVAD, &mode) != 0)
niklase@google.com470e71d2011-07-07 08:21:25 +00001687 {
1688 _engineStatisticsPtr->SetLastError(
1689 VE_AUDIO_CODING_MODULE_ERROR, kTraceError,
1690 "GetVADStatus() failed to get VAD status");
1691 return -1;
1692 }
1693 disabledDTX = !disabledDTX;
1694 return 0;
1695}
1696
pbos@webrtc.org6141e132013-04-09 10:09:10 +00001697int32_t
niklase@google.com470e71d2011-07-07 08:21:25 +00001698Channel::SetRecPayloadType(const CodecInst& codec)
1699{
1700 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId,_channelId),
1701 "Channel::SetRecPayloadType()");
1702
1703 if (_playing)
1704 {
1705 _engineStatisticsPtr->SetLastError(
1706 VE_ALREADY_PLAYING, kTraceError,
1707 "SetRecPayloadType() unable to set PT while playing");
1708 return -1;
1709 }
1710 if (_receiving)
1711 {
1712 _engineStatisticsPtr->SetLastError(
1713 VE_ALREADY_LISTENING, kTraceError,
1714 "SetRecPayloadType() unable to set PT while listening");
1715 return -1;
1716 }
1717
1718 if (codec.pltype == -1)
1719 {
1720 // De-register the selected codec (RTP/RTCP module and ACM)
1721
pbos@webrtc.org6141e132013-04-09 10:09:10 +00001722 int8_t pltype(-1);
niklase@google.com470e71d2011-07-07 08:21:25 +00001723 CodecInst rxCodec = codec;
1724
1725 // Get payload type for the given codec
pwestin@webrtc.org2853dde2012-05-11 11:08:54 +00001726 _rtpRtcpModule->ReceivePayloadType(rxCodec, &pltype);
niklase@google.com470e71d2011-07-07 08:21:25 +00001727 rxCodec.pltype = pltype;
1728
pwestin@webrtc.org2853dde2012-05-11 11:08:54 +00001729 if (_rtpRtcpModule->DeRegisterReceivePayload(pltype) != 0)
niklase@google.com470e71d2011-07-07 08:21:25 +00001730 {
1731 _engineStatisticsPtr->SetLastError(
1732 VE_RTP_RTCP_MODULE_ERROR,
1733 kTraceError,
1734 "SetRecPayloadType() RTP/RTCP-module deregistration "
1735 "failed");
1736 return -1;
1737 }
1738 if (_audioCodingModule.UnregisterReceiveCodec(rxCodec.pltype) != 0)
1739 {
1740 _engineStatisticsPtr->SetLastError(
1741 VE_AUDIO_CODING_MODULE_ERROR, kTraceError,
1742 "SetRecPayloadType() ACM deregistration failed - 1");
1743 return -1;
1744 }
1745 return 0;
1746 }
1747
pwestin@webrtc.org2853dde2012-05-11 11:08:54 +00001748 if (_rtpRtcpModule->RegisterReceivePayload(codec) != 0)
niklase@google.com470e71d2011-07-07 08:21:25 +00001749 {
1750 // First attempt to register failed => de-register and try again
pwestin@webrtc.org2853dde2012-05-11 11:08:54 +00001751 _rtpRtcpModule->DeRegisterReceivePayload(codec.pltype);
1752 if (_rtpRtcpModule->RegisterReceivePayload(codec) != 0)
niklase@google.com470e71d2011-07-07 08:21:25 +00001753 {
1754 _engineStatisticsPtr->SetLastError(
1755 VE_RTP_RTCP_MODULE_ERROR, kTraceError,
1756 "SetRecPayloadType() RTP/RTCP-module registration failed");
1757 return -1;
1758 }
1759 }
1760 if (_audioCodingModule.RegisterReceiveCodec(codec) != 0)
1761 {
1762 _audioCodingModule.UnregisterReceiveCodec(codec.pltype);
1763 if (_audioCodingModule.RegisterReceiveCodec(codec) != 0)
1764 {
1765 _engineStatisticsPtr->SetLastError(
1766 VE_AUDIO_CODING_MODULE_ERROR, kTraceError,
1767 "SetRecPayloadType() ACM registration failed - 1");
1768 return -1;
1769 }
1770 }
1771 return 0;
1772}
1773
pbos@webrtc.org6141e132013-04-09 10:09:10 +00001774int32_t
niklase@google.com470e71d2011-07-07 08:21:25 +00001775Channel::GetRecPayloadType(CodecInst& codec)
1776{
1777 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId,_channelId),
1778 "Channel::GetRecPayloadType()");
pbos@webrtc.org6141e132013-04-09 10:09:10 +00001779 int8_t payloadType(-1);
pwestin@webrtc.org2853dde2012-05-11 11:08:54 +00001780 if (_rtpRtcpModule->ReceivePayloadType(codec, &payloadType) != 0)
niklase@google.com470e71d2011-07-07 08:21:25 +00001781 {
1782 _engineStatisticsPtr->SetLastError(
henrika@webrtc.org37198002012-06-18 11:00:12 +00001783 VE_RTP_RTCP_MODULE_ERROR, kTraceWarning,
niklase@google.com470e71d2011-07-07 08:21:25 +00001784 "GetRecPayloadType() failed to retrieve RX payload type");
1785 return -1;
1786 }
1787 codec.pltype = payloadType;
1788 WEBRTC_TRACE(kTraceStateInfo, kTraceVoice, VoEId(_instanceId,_channelId),
1789 "Channel::GetRecPayloadType() => pltype=%u", codec.pltype);
1790 return 0;
1791}
1792
pbos@webrtc.org6141e132013-04-09 10:09:10 +00001793int32_t
niklase@google.com470e71d2011-07-07 08:21:25 +00001794Channel::SetAMREncFormat(AmrMode mode)
1795{
1796 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId,_channelId),
1797 "Channel::SetAMREncFormat()");
1798
1799 // ACM doesn't support AMR
1800 return -1;
1801}
1802
pbos@webrtc.org6141e132013-04-09 10:09:10 +00001803int32_t
niklase@google.com470e71d2011-07-07 08:21:25 +00001804Channel::SetAMRDecFormat(AmrMode mode)
1805{
1806 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId,_channelId),
1807 "Channel::SetAMRDecFormat()");
1808
1809 // ACM doesn't support AMR
1810 return -1;
1811}
1812
pbos@webrtc.org6141e132013-04-09 10:09:10 +00001813int32_t
niklase@google.com470e71d2011-07-07 08:21:25 +00001814Channel::SetAMRWbEncFormat(AmrMode mode)
1815{
1816 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId,_channelId),
1817 "Channel::SetAMRWbEncFormat()");
1818
1819 // ACM doesn't support AMR
1820 return -1;
1821
1822}
1823
pbos@webrtc.org6141e132013-04-09 10:09:10 +00001824int32_t
niklase@google.com470e71d2011-07-07 08:21:25 +00001825Channel::SetAMRWbDecFormat(AmrMode mode)
1826{
1827 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId,_channelId),
1828 "Channel::SetAMRWbDecFormat()");
1829
1830 // ACM doesn't support AMR
1831 return -1;
1832}
1833
pbos@webrtc.org6141e132013-04-09 10:09:10 +00001834int32_t
niklase@google.com470e71d2011-07-07 08:21:25 +00001835Channel::SetSendCNPayloadType(int type, PayloadFrequencies frequency)
1836{
1837 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId,_channelId),
1838 "Channel::SetSendCNPayloadType()");
1839
1840 CodecInst codec;
pbos@webrtc.org6141e132013-04-09 10:09:10 +00001841 int32_t samplingFreqHz(-1);
tina.legrand@webrtc.org45175852012-06-01 09:27:35 +00001842 const int kMono = 1;
niklase@google.com470e71d2011-07-07 08:21:25 +00001843 if (frequency == kFreq32000Hz)
1844 samplingFreqHz = 32000;
1845 else if (frequency == kFreq16000Hz)
1846 samplingFreqHz = 16000;
1847
tina.legrand@webrtc.org7a7a0082013-02-21 10:27:48 +00001848 if (_audioCodingModule.Codec("CN", &codec, samplingFreqHz, kMono) == -1)
niklase@google.com470e71d2011-07-07 08:21:25 +00001849 {
1850 _engineStatisticsPtr->SetLastError(
1851 VE_AUDIO_CODING_MODULE_ERROR, kTraceError,
1852 "SetSendCNPayloadType() failed to retrieve default CN codec "
1853 "settings");
1854 return -1;
1855 }
1856
1857 // Modify the payload type (must be set to dynamic range)
1858 codec.pltype = type;
1859
1860 if (_audioCodingModule.RegisterSendCodec(codec) != 0)
1861 {
1862 _engineStatisticsPtr->SetLastError(
1863 VE_AUDIO_CODING_MODULE_ERROR, kTraceError,
1864 "SetSendCNPayloadType() failed to register CN to ACM");
1865 return -1;
1866 }
1867
pwestin@webrtc.org2853dde2012-05-11 11:08:54 +00001868 if (_rtpRtcpModule->RegisterSendPayload(codec) != 0)
niklase@google.com470e71d2011-07-07 08:21:25 +00001869 {
pwestin@webrtc.org2853dde2012-05-11 11:08:54 +00001870 _rtpRtcpModule->DeRegisterSendPayload(codec.pltype);
1871 if (_rtpRtcpModule->RegisterSendPayload(codec) != 0)
niklase@google.com470e71d2011-07-07 08:21:25 +00001872 {
1873 _engineStatisticsPtr->SetLastError(
1874 VE_RTP_RTCP_MODULE_ERROR, kTraceError,
1875 "SetSendCNPayloadType() failed to register CN to RTP/RTCP "
1876 "module");
1877 return -1;
1878 }
1879 }
1880 return 0;
1881}
1882
pbos@webrtc.org6141e132013-04-09 10:09:10 +00001883int32_t
niklase@google.com470e71d2011-07-07 08:21:25 +00001884Channel::SetISACInitTargetRate(int rateBps, bool useFixedFrameSize)
1885{
1886 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId,_channelId),
1887 "Channel::SetISACInitTargetRate()");
1888
1889 CodecInst sendCodec;
tina.legrand@webrtc.org7a7a0082013-02-21 10:27:48 +00001890 if (_audioCodingModule.SendCodec(&sendCodec) == -1)
niklase@google.com470e71d2011-07-07 08:21:25 +00001891 {
1892 _engineStatisticsPtr->SetLastError(
1893 VE_CODEC_ERROR, kTraceError,
1894 "SetISACInitTargetRate() failed to retrieve send codec");
1895 return -1;
1896 }
1897 if (STR_CASE_CMP(sendCodec.plname, "ISAC") != 0)
1898 {
1899 // This API is only valid if iSAC is setup to run in channel-adaptive
1900 // mode.
1901 // We do not validate the adaptive mode here. It is done later in the
1902 // ConfigISACBandwidthEstimator() API.
1903 _engineStatisticsPtr->SetLastError(
1904 VE_CODEC_ERROR, kTraceError,
1905 "SetISACInitTargetRate() send codec is not iSAC");
1906 return -1;
1907 }
1908
pbos@webrtc.org6141e132013-04-09 10:09:10 +00001909 uint8_t initFrameSizeMsec(0);
niklase@google.com470e71d2011-07-07 08:21:25 +00001910 if (16000 == sendCodec.plfreq)
1911 {
1912 // Note that 0 is a valid and corresponds to "use default
1913 if ((rateBps != 0 &&
1914 rateBps < kVoiceEngineMinIsacInitTargetRateBpsWb) ||
1915 (rateBps > kVoiceEngineMaxIsacInitTargetRateBpsWb))
1916 {
1917 _engineStatisticsPtr->SetLastError(
1918 VE_INVALID_ARGUMENT, kTraceError,
1919 "SetISACInitTargetRate() invalid target rate - 1");
1920 return -1;
1921 }
1922 // 30 or 60ms
pbos@webrtc.org6141e132013-04-09 10:09:10 +00001923 initFrameSizeMsec = (uint8_t)(sendCodec.pacsize / 16);
niklase@google.com470e71d2011-07-07 08:21:25 +00001924 }
1925 else if (32000 == sendCodec.plfreq)
1926 {
1927 if ((rateBps != 0 &&
1928 rateBps < kVoiceEngineMinIsacInitTargetRateBpsSwb) ||
1929 (rateBps > kVoiceEngineMaxIsacInitTargetRateBpsSwb))
1930 {
1931 _engineStatisticsPtr->SetLastError(
1932 VE_INVALID_ARGUMENT, kTraceError,
1933 "SetISACInitTargetRate() invalid target rate - 2");
1934 return -1;
1935 }
pbos@webrtc.org6141e132013-04-09 10:09:10 +00001936 initFrameSizeMsec = (uint8_t)(sendCodec.pacsize / 32); // 30ms
niklase@google.com470e71d2011-07-07 08:21:25 +00001937 }
1938
1939 if (_audioCodingModule.ConfigISACBandwidthEstimator(
1940 initFrameSizeMsec, rateBps, useFixedFrameSize) == -1)
1941 {
1942 _engineStatisticsPtr->SetLastError(
1943 VE_AUDIO_CODING_MODULE_ERROR, kTraceError,
1944 "SetISACInitTargetRate() iSAC BWE config failed");
1945 return -1;
1946 }
1947
1948 return 0;
1949}
1950
pbos@webrtc.org6141e132013-04-09 10:09:10 +00001951int32_t
niklase@google.com470e71d2011-07-07 08:21:25 +00001952Channel::SetISACMaxRate(int rateBps)
1953{
1954 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId,_channelId),
1955 "Channel::SetISACMaxRate()");
1956
1957 CodecInst sendCodec;
tina.legrand@webrtc.org7a7a0082013-02-21 10:27:48 +00001958 if (_audioCodingModule.SendCodec(&sendCodec) == -1)
niklase@google.com470e71d2011-07-07 08:21:25 +00001959 {
1960 _engineStatisticsPtr->SetLastError(
1961 VE_CODEC_ERROR, kTraceError,
1962 "SetISACMaxRate() failed to retrieve send codec");
1963 return -1;
1964 }
1965 if (STR_CASE_CMP(sendCodec.plname, "ISAC") != 0)
1966 {
1967 // This API is only valid if iSAC is selected as sending codec.
1968 _engineStatisticsPtr->SetLastError(
1969 VE_CODEC_ERROR, kTraceError,
1970 "SetISACMaxRate() send codec is not iSAC");
1971 return -1;
1972 }
1973 if (16000 == sendCodec.plfreq)
1974 {
1975 if ((rateBps < kVoiceEngineMinIsacMaxRateBpsWb) ||
1976 (rateBps > kVoiceEngineMaxIsacMaxRateBpsWb))
1977 {
1978 _engineStatisticsPtr->SetLastError(
1979 VE_INVALID_ARGUMENT, kTraceError,
1980 "SetISACMaxRate() invalid max rate - 1");
1981 return -1;
1982 }
1983 }
1984 else if (32000 == sendCodec.plfreq)
1985 {
1986 if ((rateBps < kVoiceEngineMinIsacMaxRateBpsSwb) ||
1987 (rateBps > kVoiceEngineMaxIsacMaxRateBpsSwb))
1988 {
1989 _engineStatisticsPtr->SetLastError(
1990 VE_INVALID_ARGUMENT, kTraceError,
1991 "SetISACMaxRate() invalid max rate - 2");
1992 return -1;
1993 }
1994 }
1995 if (_sending)
1996 {
1997 _engineStatisticsPtr->SetLastError(
1998 VE_SENDING, kTraceError,
1999 "SetISACMaxRate() unable to set max rate while sending");
2000 return -1;
2001 }
2002
2003 // Set the maximum instantaneous rate of iSAC (works for both adaptive
2004 // and non-adaptive mode)
2005 if (_audioCodingModule.SetISACMaxRate(rateBps) == -1)
2006 {
2007 _engineStatisticsPtr->SetLastError(
2008 VE_AUDIO_CODING_MODULE_ERROR, kTraceError,
2009 "SetISACMaxRate() failed to set max rate");
2010 return -1;
2011 }
2012
2013 return 0;
2014}
2015
pbos@webrtc.org6141e132013-04-09 10:09:10 +00002016int32_t
niklase@google.com470e71d2011-07-07 08:21:25 +00002017Channel::SetISACMaxPayloadSize(int sizeBytes)
2018{
2019 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId,_channelId),
2020 "Channel::SetISACMaxPayloadSize()");
2021 CodecInst sendCodec;
tina.legrand@webrtc.org7a7a0082013-02-21 10:27:48 +00002022 if (_audioCodingModule.SendCodec(&sendCodec) == -1)
niklase@google.com470e71d2011-07-07 08:21:25 +00002023 {
2024 _engineStatisticsPtr->SetLastError(
2025 VE_CODEC_ERROR, kTraceError,
2026 "SetISACMaxPayloadSize() failed to retrieve send codec");
2027 return -1;
2028 }
2029 if (STR_CASE_CMP(sendCodec.plname, "ISAC") != 0)
2030 {
2031 _engineStatisticsPtr->SetLastError(
2032 VE_CODEC_ERROR, kTraceError,
2033 "SetISACMaxPayloadSize() send codec is not iSAC");
2034 return -1;
2035 }
2036 if (16000 == sendCodec.plfreq)
2037 {
2038 if ((sizeBytes < kVoiceEngineMinIsacMaxPayloadSizeBytesWb) ||
2039 (sizeBytes > kVoiceEngineMaxIsacMaxPayloadSizeBytesWb))
2040 {
2041 _engineStatisticsPtr->SetLastError(
2042 VE_INVALID_ARGUMENT, kTraceError,
2043 "SetISACMaxPayloadSize() invalid max payload - 1");
2044 return -1;
2045 }
2046 }
2047 else if (32000 == sendCodec.plfreq)
2048 {
2049 if ((sizeBytes < kVoiceEngineMinIsacMaxPayloadSizeBytesSwb) ||
2050 (sizeBytes > kVoiceEngineMaxIsacMaxPayloadSizeBytesSwb))
2051 {
2052 _engineStatisticsPtr->SetLastError(
2053 VE_INVALID_ARGUMENT, kTraceError,
2054 "SetISACMaxPayloadSize() invalid max payload - 2");
2055 return -1;
2056 }
2057 }
2058 if (_sending)
2059 {
2060 _engineStatisticsPtr->SetLastError(
2061 VE_SENDING, kTraceError,
2062 "SetISACMaxPayloadSize() unable to set max rate while sending");
2063 return -1;
2064 }
2065
2066 if (_audioCodingModule.SetISACMaxPayloadSize(sizeBytes) == -1)
2067 {
2068 _engineStatisticsPtr->SetLastError(
2069 VE_AUDIO_CODING_MODULE_ERROR, kTraceError,
2070 "SetISACMaxPayloadSize() failed to set max payload size");
2071 return -1;
2072 }
2073 return 0;
2074}
2075
pbos@webrtc.org6141e132013-04-09 10:09:10 +00002076int32_t Channel::RegisterExternalTransport(Transport& transport)
niklase@google.com470e71d2011-07-07 08:21:25 +00002077{
2078 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId, _channelId),
2079 "Channel::RegisterExternalTransport()");
2080
mflodman@webrtc.org9a065d12012-03-07 08:12:21 +00002081 CriticalSectionScoped cs(&_callbackCritSect);
niklase@google.com470e71d2011-07-07 08:21:25 +00002082
niklase@google.com470e71d2011-07-07 08:21:25 +00002083 if (_externalTransport)
2084 {
2085 _engineStatisticsPtr->SetLastError(VE_INVALID_OPERATION,
2086 kTraceError,
2087 "RegisterExternalTransport() external transport already enabled");
2088 return -1;
2089 }
2090 _externalTransport = true;
2091 _transportPtr = &transport;
2092 return 0;
2093}
2094
pbos@webrtc.org6141e132013-04-09 10:09:10 +00002095int32_t
niklase@google.com470e71d2011-07-07 08:21:25 +00002096Channel::DeRegisterExternalTransport()
2097{
2098 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId,_channelId),
2099 "Channel::DeRegisterExternalTransport()");
2100
mflodman@webrtc.org9a065d12012-03-07 08:12:21 +00002101 CriticalSectionScoped cs(&_callbackCritSect);
xians@webrtc.org83661f52011-11-25 10:58:15 +00002102
niklase@google.com470e71d2011-07-07 08:21:25 +00002103 if (!_transportPtr)
2104 {
2105 _engineStatisticsPtr->SetLastError(
2106 VE_INVALID_OPERATION, kTraceWarning,
2107 "DeRegisterExternalTransport() external transport already "
2108 "disabled");
2109 return 0;
2110 }
2111 _externalTransport = false;
niklase@google.com470e71d2011-07-07 08:21:25 +00002112 _transportPtr = NULL;
2113 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId,_channelId),
2114 "DeRegisterExternalTransport() all transport is disabled");
niklase@google.com470e71d2011-07-07 08:21:25 +00002115 return 0;
2116}
2117
pbos@webrtc.org6141e132013-04-09 10:09:10 +00002118int32_t Channel::ReceivedRTPPacket(const int8_t* data, int32_t length) {
pwestin@webrtc.org0c459572013-04-03 15:43:57 +00002119 WEBRTC_TRACE(kTraceStream, kTraceVoice, VoEId(_instanceId,_channelId),
2120 "Channel::ReceivedRTPPacket()");
2121
2122 // Store playout timestamp for the received RTP packet
pwestin@webrtc.org1de01352013-04-11 20:23:35 +00002123 UpdatePlayoutTimestamp(false);
pwestin@webrtc.org0c459572013-04-03 15:43:57 +00002124
2125 // Dump the RTP packet to a file (if RTP dump is enabled).
pbos@webrtc.org6141e132013-04-09 10:09:10 +00002126 if (_rtpDumpIn.DumpPacket((const uint8_t*)data,
2127 (uint16_t)length) == -1) {
pwestin@webrtc.org0c459572013-04-03 15:43:57 +00002128 WEBRTC_TRACE(kTraceWarning, kTraceVoice,
2129 VoEId(_instanceId,_channelId),
2130 "Channel::SendPacket() RTP dump to input file failed");
2131 }
stefan@webrtc.orga5cb98c2013-05-29 12:12:51 +00002132 RTPHeader header;
2133 if (!rtp_header_parser_->Parse(reinterpret_cast<const uint8_t*>(data),
2134 static_cast<uint16_t>(length), &header)) {
2135 WEBRTC_TRACE(webrtc::kTraceDebug, webrtc::kTraceVideo,
2136 VoEId(_instanceId,_channelId),
2137 "IncomingPacket invalid RTP header");
2138 return -1;
2139 }
pwestin@webrtc.org0c459572013-04-03 15:43:57 +00002140 // Deliver RTP packet to RTP/RTCP module for parsing
2141 // The packet will be pushed back to the channel thru the
2142 // OnReceivedPayloadData callback so we don't push it to the ACM here
stefan@webrtc.orga5cb98c2013-05-29 12:12:51 +00002143 if (_rtpRtcpModule->IncomingRtpPacket(reinterpret_cast<const uint8_t*>(data),
2144 static_cast<uint16_t>(length),
2145 header) == -1) {
pwestin@webrtc.org0c459572013-04-03 15:43:57 +00002146 _engineStatisticsPtr->SetLastError(
2147 VE_SOCKET_TRANSPORT_MODULE_ERROR, kTraceWarning,
2148 "Channel::IncomingRTPPacket() RTP packet is invalid");
2149 }
2150 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +00002151}
2152
pbos@webrtc.org6141e132013-04-09 10:09:10 +00002153int32_t Channel::ReceivedRTCPPacket(const int8_t* data, int32_t length) {
pwestin@webrtc.org0c459572013-04-03 15:43:57 +00002154 WEBRTC_TRACE(kTraceStream, kTraceVoice, VoEId(_instanceId,_channelId),
2155 "Channel::ReceivedRTCPPacket()");
2156 // Store playout timestamp for the received RTCP packet
pwestin@webrtc.org1de01352013-04-11 20:23:35 +00002157 UpdatePlayoutTimestamp(true);
pwestin@webrtc.org0c459572013-04-03 15:43:57 +00002158
2159 // Dump the RTCP packet to a file (if RTP dump is enabled).
pbos@webrtc.org6141e132013-04-09 10:09:10 +00002160 if (_rtpDumpIn.DumpPacket((const uint8_t*)data,
2161 (uint16_t)length) == -1) {
pwestin@webrtc.org0c459572013-04-03 15:43:57 +00002162 WEBRTC_TRACE(kTraceWarning, kTraceVoice,
2163 VoEId(_instanceId,_channelId),
2164 "Channel::SendPacket() RTCP dump to input file failed");
2165 }
2166
2167 // Deliver RTCP packet to RTP/RTCP module for parsing
stefan@webrtc.orga5cb98c2013-05-29 12:12:51 +00002168 if (_rtpRtcpModule->IncomingRtcpPacket((const uint8_t*)data,
2169 (uint16_t)length) == -1) {
pwestin@webrtc.org0c459572013-04-03 15:43:57 +00002170 _engineStatisticsPtr->SetLastError(
2171 VE_SOCKET_TRANSPORT_MODULE_ERROR, kTraceWarning,
2172 "Channel::IncomingRTPPacket() RTCP packet is invalid");
2173 }
2174 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +00002175}
2176
pbos@webrtc.org6141e132013-04-09 10:09:10 +00002177int32_t
niklase@google.com470e71d2011-07-07 08:21:25 +00002178Channel::SetPacketTimeoutNotification(bool enable, int timeoutSeconds)
2179{
2180 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId,_channelId),
2181 "Channel::SetPacketTimeoutNotification()");
2182 if (enable)
2183 {
pbos@webrtc.org6141e132013-04-09 10:09:10 +00002184 const uint32_t RTPtimeoutMS = 1000*timeoutSeconds;
2185 const uint32_t RTCPtimeoutMS = 0;
pwestin@webrtc.org2853dde2012-05-11 11:08:54 +00002186 _rtpRtcpModule->SetPacketTimeout(RTPtimeoutMS, RTCPtimeoutMS);
niklase@google.com470e71d2011-07-07 08:21:25 +00002187 _rtpPacketTimeOutIsEnabled = true;
2188 _rtpTimeOutSeconds = timeoutSeconds;
2189 }
2190 else
2191 {
pwestin@webrtc.org2853dde2012-05-11 11:08:54 +00002192 _rtpRtcpModule->SetPacketTimeout(0, 0);
niklase@google.com470e71d2011-07-07 08:21:25 +00002193 _rtpPacketTimeOutIsEnabled = false;
2194 _rtpTimeOutSeconds = 0;
2195 }
2196 return 0;
2197}
2198
pbos@webrtc.org6141e132013-04-09 10:09:10 +00002199int32_t
niklase@google.com470e71d2011-07-07 08:21:25 +00002200Channel::GetPacketTimeoutNotification(bool& enabled, int& timeoutSeconds)
2201{
2202 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId,_channelId),
2203 "Channel::GetPacketTimeoutNotification()");
2204 enabled = _rtpPacketTimeOutIsEnabled;
2205 if (enabled)
2206 {
2207 timeoutSeconds = _rtpTimeOutSeconds;
2208 }
2209 WEBRTC_TRACE(kTraceStateInfo, kTraceVoice, VoEId(_instanceId,-1),
2210 "GetPacketTimeoutNotification() => enabled=%d,"
2211 " timeoutSeconds=%d",
2212 enabled, timeoutSeconds);
2213 return 0;
2214}
2215
pbos@webrtc.org6141e132013-04-09 10:09:10 +00002216int32_t
niklase@google.com470e71d2011-07-07 08:21:25 +00002217Channel::RegisterDeadOrAliveObserver(VoEConnectionObserver& observer)
2218{
2219 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId,_channelId),
2220 "Channel::RegisterDeadOrAliveObserver()");
mflodman@webrtc.org9a065d12012-03-07 08:12:21 +00002221 CriticalSectionScoped cs(&_callbackCritSect);
niklase@google.com470e71d2011-07-07 08:21:25 +00002222
2223 if (_connectionObserverPtr)
2224 {
2225 _engineStatisticsPtr->SetLastError(VE_INVALID_OPERATION, kTraceError,
2226 "RegisterDeadOrAliveObserver() observer already enabled");
2227 return -1;
2228 }
2229
2230 _connectionObserverPtr = &observer;
2231 _connectionObserver = true;
2232
2233 return 0;
2234}
2235
pbos@webrtc.org6141e132013-04-09 10:09:10 +00002236int32_t
niklase@google.com470e71d2011-07-07 08:21:25 +00002237Channel::DeRegisterDeadOrAliveObserver()
2238{
2239 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId,_channelId),
2240 "Channel::DeRegisterDeadOrAliveObserver()");
mflodman@webrtc.org9a065d12012-03-07 08:12:21 +00002241 CriticalSectionScoped cs(&_callbackCritSect);
niklase@google.com470e71d2011-07-07 08:21:25 +00002242
2243 if (!_connectionObserverPtr)
2244 {
2245 _engineStatisticsPtr->SetLastError(
2246 VE_INVALID_OPERATION, kTraceWarning,
2247 "DeRegisterDeadOrAliveObserver() observer already disabled");
2248 return 0;
2249 }
2250
2251 _connectionObserver = false;
2252 _connectionObserverPtr = NULL;
2253
2254 return 0;
2255}
2256
pbos@webrtc.org6141e132013-04-09 10:09:10 +00002257int32_t
niklase@google.com470e71d2011-07-07 08:21:25 +00002258Channel::SetPeriodicDeadOrAliveStatus(bool enable, int sampleTimeSeconds)
2259{
2260 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId,_channelId),
2261 "Channel::SetPeriodicDeadOrAliveStatus()");
2262 if (!_connectionObserverPtr)
2263 {
2264 WEBRTC_TRACE(kTraceWarning, kTraceVoice, VoEId(_instanceId,_channelId),
2265 "SetPeriodicDeadOrAliveStatus() connection observer has"
2266 " not been registered");
2267 }
2268 if (enable)
2269 {
2270 ResetDeadOrAliveCounters();
2271 }
2272 bool enabled(false);
pbos@webrtc.org6141e132013-04-09 10:09:10 +00002273 uint8_t currentSampleTimeSec(0);
niklase@google.com470e71d2011-07-07 08:21:25 +00002274 // Store last state (will be used later if dead-or-alive is disabled).
pwestin@webrtc.org2853dde2012-05-11 11:08:54 +00002275 _rtpRtcpModule->PeriodicDeadOrAliveStatus(enabled, currentSampleTimeSec);
niklase@google.com470e71d2011-07-07 08:21:25 +00002276 // Update the dead-or-alive state.
pwestin@webrtc.org2853dde2012-05-11 11:08:54 +00002277 if (_rtpRtcpModule->SetPeriodicDeadOrAliveStatus(
pbos@webrtc.org6141e132013-04-09 10:09:10 +00002278 enable, (uint8_t)sampleTimeSeconds) != 0)
niklase@google.com470e71d2011-07-07 08:21:25 +00002279 {
2280 _engineStatisticsPtr->SetLastError(
2281 VE_RTP_RTCP_MODULE_ERROR,
2282 kTraceError,
2283 "SetPeriodicDeadOrAliveStatus() failed to set dead-or-alive "
2284 "status");
2285 return -1;
2286 }
2287 if (!enable)
2288 {
2289 // Restore last utilized sample time.
2290 // Without this, the sample time would always be reset to default
2291 // (2 sec), each time dead-or-alived was disabled without sample-time
2292 // parameter.
pwestin@webrtc.org2853dde2012-05-11 11:08:54 +00002293 _rtpRtcpModule->SetPeriodicDeadOrAliveStatus(enable,
niklase@google.com470e71d2011-07-07 08:21:25 +00002294 currentSampleTimeSec);
2295 }
2296 return 0;
2297}
2298
pbos@webrtc.org6141e132013-04-09 10:09:10 +00002299int32_t
niklase@google.com470e71d2011-07-07 08:21:25 +00002300Channel::GetPeriodicDeadOrAliveStatus(bool& enabled, int& sampleTimeSeconds)
2301{
pwestin@webrtc.org2853dde2012-05-11 11:08:54 +00002302 _rtpRtcpModule->PeriodicDeadOrAliveStatus(
niklase@google.com470e71d2011-07-07 08:21:25 +00002303 enabled,
pbos@webrtc.org6141e132013-04-09 10:09:10 +00002304 (uint8_t&)sampleTimeSeconds);
niklase@google.com470e71d2011-07-07 08:21:25 +00002305 WEBRTC_TRACE(kTraceStateInfo, kTraceVoice, VoEId(_instanceId,-1),
2306 "GetPeriodicDeadOrAliveStatus() => enabled=%d,"
2307 " sampleTimeSeconds=%d",
2308 enabled, sampleTimeSeconds);
2309 return 0;
2310}
2311
niklase@google.com470e71d2011-07-07 08:21:25 +00002312int Channel::StartPlayingFileLocally(const char* fileName,
pbos@webrtc.org92135212013-05-14 08:31:39 +00002313 bool loop,
2314 FileFormats format,
2315 int startPosition,
2316 float volumeScaling,
2317 int stopPosition,
niklase@google.com470e71d2011-07-07 08:21:25 +00002318 const CodecInst* codecInst)
2319{
2320 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId,_channelId),
2321 "Channel::StartPlayingFileLocally(fileNameUTF8[]=%s, loop=%d,"
2322 " format=%d, volumeScaling=%5.3f, startPosition=%d, "
2323 "stopPosition=%d)", fileName, loop, format, volumeScaling,
2324 startPosition, stopPosition);
2325
2326 if (_outputFilePlaying)
2327 {
2328 _engineStatisticsPtr->SetLastError(
2329 VE_ALREADY_PLAYING, kTraceError,
2330 "StartPlayingFileLocally() is already playing");
2331 return -1;
2332 }
2333
niklase@google.com470e71d2011-07-07 08:21:25 +00002334 {
mflodman@webrtc.org9a065d12012-03-07 08:12:21 +00002335 CriticalSectionScoped cs(&_fileCritSect);
henrike@webrtc.orgb37c6282011-10-31 23:53:04 +00002336
2337 if (_outputFilePlayerPtr)
2338 {
2339 _outputFilePlayerPtr->RegisterModuleFileCallback(NULL);
2340 FilePlayer::DestroyFilePlayer(_outputFilePlayerPtr);
2341 _outputFilePlayerPtr = NULL;
2342 }
2343
2344 _outputFilePlayerPtr = FilePlayer::CreateFilePlayer(
2345 _outputFilePlayerId, (const FileFormats)format);
2346
2347 if (_outputFilePlayerPtr == NULL)
2348 {
2349 _engineStatisticsPtr->SetLastError(
2350 VE_INVALID_ARGUMENT, kTraceError,
henrike@webrtc.org31d30702011-11-18 19:59:32 +00002351 "StartPlayingFileLocally() filePlayer format is not correct");
henrike@webrtc.orgb37c6282011-10-31 23:53:04 +00002352 return -1;
2353 }
2354
pbos@webrtc.org6141e132013-04-09 10:09:10 +00002355 const uint32_t notificationTime(0);
henrike@webrtc.orgb37c6282011-10-31 23:53:04 +00002356
2357 if (_outputFilePlayerPtr->StartPlayingFile(
2358 fileName,
2359 loop,
2360 startPosition,
2361 volumeScaling,
2362 notificationTime,
2363 stopPosition,
2364 (const CodecInst*)codecInst) != 0)
2365 {
2366 _engineStatisticsPtr->SetLastError(
2367 VE_BAD_FILE, kTraceError,
2368 "StartPlayingFile() failed to start file playout");
2369 _outputFilePlayerPtr->StopPlayingFile();
2370 FilePlayer::DestroyFilePlayer(_outputFilePlayerPtr);
2371 _outputFilePlayerPtr = NULL;
2372 return -1;
2373 }
2374 _outputFilePlayerPtr->RegisterModuleFileCallback(this);
2375 _outputFilePlaying = true;
niklase@google.com470e71d2011-07-07 08:21:25 +00002376 }
braveyao@webrtc.orgab129902012-06-04 03:26:39 +00002377
2378 if (RegisterFilePlayingToMixer() != 0)
henrike@webrtc.org066f9e52011-10-28 23:15:47 +00002379 return -1;
niklase@google.com470e71d2011-07-07 08:21:25 +00002380
2381 return 0;
2382}
2383
2384int Channel::StartPlayingFileLocally(InStream* stream,
pbos@webrtc.org92135212013-05-14 08:31:39 +00002385 FileFormats format,
2386 int startPosition,
2387 float volumeScaling,
2388 int stopPosition,
niklase@google.com470e71d2011-07-07 08:21:25 +00002389 const CodecInst* codecInst)
2390{
2391 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId,_channelId),
2392 "Channel::StartPlayingFileLocally(format=%d,"
2393 " volumeScaling=%5.3f, startPosition=%d, stopPosition=%d)",
2394 format, volumeScaling, startPosition, stopPosition);
2395
2396 if(stream == NULL)
2397 {
2398 _engineStatisticsPtr->SetLastError(
2399 VE_BAD_FILE, kTraceError,
2400 "StartPlayingFileLocally() NULL as input stream");
2401 return -1;
2402 }
2403
2404
2405 if (_outputFilePlaying)
2406 {
2407 _engineStatisticsPtr->SetLastError(
2408 VE_ALREADY_PLAYING, kTraceError,
2409 "StartPlayingFileLocally() is already playing");
2410 return -1;
2411 }
2412
niklase@google.com470e71d2011-07-07 08:21:25 +00002413 {
mflodman@webrtc.org9a065d12012-03-07 08:12:21 +00002414 CriticalSectionScoped cs(&_fileCritSect);
henrike@webrtc.orgb37c6282011-10-31 23:53:04 +00002415
2416 // Destroy the old instance
2417 if (_outputFilePlayerPtr)
2418 {
2419 _outputFilePlayerPtr->RegisterModuleFileCallback(NULL);
2420 FilePlayer::DestroyFilePlayer(_outputFilePlayerPtr);
2421 _outputFilePlayerPtr = NULL;
2422 }
2423
2424 // Create the instance
2425 _outputFilePlayerPtr = FilePlayer::CreateFilePlayer(
2426 _outputFilePlayerId,
2427 (const FileFormats)format);
2428
2429 if (_outputFilePlayerPtr == NULL)
2430 {
2431 _engineStatisticsPtr->SetLastError(
2432 VE_INVALID_ARGUMENT, kTraceError,
2433 "StartPlayingFileLocally() filePlayer format isnot correct");
2434 return -1;
2435 }
2436
pbos@webrtc.org6141e132013-04-09 10:09:10 +00002437 const uint32_t notificationTime(0);
henrike@webrtc.orgb37c6282011-10-31 23:53:04 +00002438
2439 if (_outputFilePlayerPtr->StartPlayingFile(*stream, startPosition,
2440 volumeScaling,
2441 notificationTime,
2442 stopPosition, codecInst) != 0)
2443 {
2444 _engineStatisticsPtr->SetLastError(VE_BAD_FILE, kTraceError,
2445 "StartPlayingFile() failed to "
2446 "start file playout");
2447 _outputFilePlayerPtr->StopPlayingFile();
2448 FilePlayer::DestroyFilePlayer(_outputFilePlayerPtr);
2449 _outputFilePlayerPtr = NULL;
2450 return -1;
2451 }
2452 _outputFilePlayerPtr->RegisterModuleFileCallback(this);
2453 _outputFilePlaying = true;
niklase@google.com470e71d2011-07-07 08:21:25 +00002454 }
braveyao@webrtc.orgab129902012-06-04 03:26:39 +00002455
2456 if (RegisterFilePlayingToMixer() != 0)
henrike@webrtc.org066f9e52011-10-28 23:15:47 +00002457 return -1;
niklase@google.com470e71d2011-07-07 08:21:25 +00002458
niklase@google.com470e71d2011-07-07 08:21:25 +00002459 return 0;
2460}
2461
2462int Channel::StopPlayingFileLocally()
2463{
2464 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId,_channelId),
2465 "Channel::StopPlayingFileLocally()");
2466
2467 if (!_outputFilePlaying)
2468 {
2469 _engineStatisticsPtr->SetLastError(
2470 VE_INVALID_OPERATION, kTraceWarning,
2471 "StopPlayingFileLocally() isnot playing");
2472 return 0;
2473 }
2474
niklase@google.com470e71d2011-07-07 08:21:25 +00002475 {
mflodman@webrtc.org9a065d12012-03-07 08:12:21 +00002476 CriticalSectionScoped cs(&_fileCritSect);
henrike@webrtc.orgb37c6282011-10-31 23:53:04 +00002477
2478 if (_outputFilePlayerPtr->StopPlayingFile() != 0)
2479 {
2480 _engineStatisticsPtr->SetLastError(
2481 VE_STOP_RECORDING_FAILED, kTraceError,
2482 "StopPlayingFile() could not stop playing");
2483 return -1;
2484 }
2485 _outputFilePlayerPtr->RegisterModuleFileCallback(NULL);
2486 FilePlayer::DestroyFilePlayer(_outputFilePlayerPtr);
2487 _outputFilePlayerPtr = NULL;
2488 _outputFilePlaying = false;
niklase@google.com470e71d2011-07-07 08:21:25 +00002489 }
henrike@webrtc.orgb37c6282011-10-31 23:53:04 +00002490 // _fileCritSect cannot be taken while calling
2491 // SetAnonymousMixibilityStatus. Refer to comments in
2492 // StartPlayingFileLocally(const char* ...) for more details.
henrike@webrtc.org066f9e52011-10-28 23:15:47 +00002493 if (_outputMixerPtr->SetAnonymousMixabilityStatus(*this, false) != 0)
2494 {
2495 _engineStatisticsPtr->SetLastError(
2496 VE_AUDIO_CONF_MIX_MODULE_ERROR, kTraceError,
henrike@webrtc.orgb37c6282011-10-31 23:53:04 +00002497 "StopPlayingFile() failed to stop participant from playing as"
2498 "file in the mixer");
henrike@webrtc.org066f9e52011-10-28 23:15:47 +00002499 return -1;
2500 }
niklase@google.com470e71d2011-07-07 08:21:25 +00002501
2502 return 0;
2503}
2504
2505int Channel::IsPlayingFileLocally() const
2506{
2507 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId,_channelId),
2508 "Channel::IsPlayingFileLocally()");
2509
pbos@webrtc.org6141e132013-04-09 10:09:10 +00002510 return (int32_t)_outputFilePlaying;
niklase@google.com470e71d2011-07-07 08:21:25 +00002511}
2512
braveyao@webrtc.orgab129902012-06-04 03:26:39 +00002513int Channel::RegisterFilePlayingToMixer()
2514{
2515 // Return success for not registering for file playing to mixer if:
2516 // 1. playing file before playout is started on that channel.
2517 // 2. starting playout without file playing on that channel.
2518 if (!_playing || !_outputFilePlaying)
2519 {
2520 return 0;
2521 }
2522
2523 // |_fileCritSect| cannot be taken while calling
2524 // SetAnonymousMixabilityStatus() since as soon as the participant is added
2525 // frames can be pulled by the mixer. Since the frames are generated from
2526 // the file, _fileCritSect will be taken. This would result in a deadlock.
2527 if (_outputMixerPtr->SetAnonymousMixabilityStatus(*this, true) != 0)
2528 {
2529 CriticalSectionScoped cs(&_fileCritSect);
2530 _outputFilePlaying = false;
2531 _engineStatisticsPtr->SetLastError(
2532 VE_AUDIO_CONF_MIX_MODULE_ERROR, kTraceError,
2533 "StartPlayingFile() failed to add participant as file to mixer");
2534 _outputFilePlayerPtr->StopPlayingFile();
2535 FilePlayer::DestroyFilePlayer(_outputFilePlayerPtr);
2536 _outputFilePlayerPtr = NULL;
2537 return -1;
2538 }
2539
2540 return 0;
2541}
2542
pbos@webrtc.org92135212013-05-14 08:31:39 +00002543int Channel::ScaleLocalFilePlayout(float scale)
niklase@google.com470e71d2011-07-07 08:21:25 +00002544{
2545 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId,_channelId),
2546 "Channel::ScaleLocalFilePlayout(scale=%5.3f)", scale);
2547
mflodman@webrtc.org9a065d12012-03-07 08:12:21 +00002548 CriticalSectionScoped cs(&_fileCritSect);
niklase@google.com470e71d2011-07-07 08:21:25 +00002549
2550 if (!_outputFilePlaying)
2551 {
2552 _engineStatisticsPtr->SetLastError(
2553 VE_INVALID_OPERATION, kTraceError,
2554 "ScaleLocalFilePlayout() isnot playing");
2555 return -1;
2556 }
2557 if ((_outputFilePlayerPtr == NULL) ||
2558 (_outputFilePlayerPtr->SetAudioScaling(scale) != 0))
2559 {
2560 _engineStatisticsPtr->SetLastError(
2561 VE_BAD_ARGUMENT, kTraceError,
2562 "SetAudioScaling() failed to scale the playout");
2563 return -1;
2564 }
2565
2566 return 0;
2567}
2568
2569int Channel::GetLocalPlayoutPosition(int& positionMs)
2570{
2571 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId,_channelId),
2572 "Channel::GetLocalPlayoutPosition(position=?)");
2573
pbos@webrtc.org6141e132013-04-09 10:09:10 +00002574 uint32_t position;
niklase@google.com470e71d2011-07-07 08:21:25 +00002575
mflodman@webrtc.org9a065d12012-03-07 08:12:21 +00002576 CriticalSectionScoped cs(&_fileCritSect);
niklase@google.com470e71d2011-07-07 08:21:25 +00002577
2578 if (_outputFilePlayerPtr == NULL)
2579 {
2580 _engineStatisticsPtr->SetLastError(
2581 VE_INVALID_OPERATION, kTraceError,
2582 "GetLocalPlayoutPosition() filePlayer instance doesnot exist");
2583 return -1;
2584 }
2585
2586 if (_outputFilePlayerPtr->GetPlayoutPosition(position) != 0)
2587 {
2588 _engineStatisticsPtr->SetLastError(
2589 VE_BAD_FILE, kTraceError,
2590 "GetLocalPlayoutPosition() failed");
2591 return -1;
2592 }
2593 positionMs = position;
2594
2595 return 0;
2596}
2597
2598int Channel::StartPlayingFileAsMicrophone(const char* fileName,
pbos@webrtc.org92135212013-05-14 08:31:39 +00002599 bool loop,
2600 FileFormats format,
2601 int startPosition,
2602 float volumeScaling,
2603 int stopPosition,
niklase@google.com470e71d2011-07-07 08:21:25 +00002604 const CodecInst* codecInst)
2605{
2606 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId,_channelId),
2607 "Channel::StartPlayingFileAsMicrophone(fileNameUTF8[]=%s, "
2608 "loop=%d, format=%d, volumeScaling=%5.3f, startPosition=%d, "
2609 "stopPosition=%d)", fileName, loop, format, volumeScaling,
2610 startPosition, stopPosition);
2611
2612 if (_inputFilePlaying)
2613 {
2614 _engineStatisticsPtr->SetLastError(
2615 VE_ALREADY_PLAYING, kTraceWarning,
2616 "StartPlayingFileAsMicrophone() filePlayer is playing");
2617 return 0;
2618 }
2619
mflodman@webrtc.org9a065d12012-03-07 08:12:21 +00002620 CriticalSectionScoped cs(&_fileCritSect);
niklase@google.com470e71d2011-07-07 08:21:25 +00002621
2622 // Destroy the old instance
2623 if (_inputFilePlayerPtr)
2624 {
2625 _inputFilePlayerPtr->RegisterModuleFileCallback(NULL);
2626 FilePlayer::DestroyFilePlayer(_inputFilePlayerPtr);
2627 _inputFilePlayerPtr = NULL;
2628 }
2629
2630 // Create the instance
2631 _inputFilePlayerPtr = FilePlayer::CreateFilePlayer(
2632 _inputFilePlayerId, (const FileFormats)format);
2633
2634 if (_inputFilePlayerPtr == NULL)
2635 {
2636 _engineStatisticsPtr->SetLastError(
2637 VE_INVALID_ARGUMENT, kTraceError,
2638 "StartPlayingFileAsMicrophone() filePlayer format isnot correct");
2639 return -1;
2640 }
2641
pbos@webrtc.org6141e132013-04-09 10:09:10 +00002642 const uint32_t notificationTime(0);
niklase@google.com470e71d2011-07-07 08:21:25 +00002643
2644 if (_inputFilePlayerPtr->StartPlayingFile(
2645 fileName,
2646 loop,
2647 startPosition,
2648 volumeScaling,
2649 notificationTime,
2650 stopPosition,
2651 (const CodecInst*)codecInst) != 0)
2652 {
2653 _engineStatisticsPtr->SetLastError(
2654 VE_BAD_FILE, kTraceError,
2655 "StartPlayingFile() failed to start file playout");
2656 _inputFilePlayerPtr->StopPlayingFile();
2657 FilePlayer::DestroyFilePlayer(_inputFilePlayerPtr);
2658 _inputFilePlayerPtr = NULL;
2659 return -1;
2660 }
2661 _inputFilePlayerPtr->RegisterModuleFileCallback(this);
2662 _inputFilePlaying = true;
2663
2664 return 0;
2665}
2666
2667int Channel::StartPlayingFileAsMicrophone(InStream* stream,
pbos@webrtc.org92135212013-05-14 08:31:39 +00002668 FileFormats format,
2669 int startPosition,
2670 float volumeScaling,
2671 int stopPosition,
niklase@google.com470e71d2011-07-07 08:21:25 +00002672 const CodecInst* codecInst)
2673{
2674 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId,_channelId),
2675 "Channel::StartPlayingFileAsMicrophone(format=%d, "
2676 "volumeScaling=%5.3f, startPosition=%d, stopPosition=%d)",
2677 format, volumeScaling, startPosition, stopPosition);
2678
2679 if(stream == NULL)
2680 {
2681 _engineStatisticsPtr->SetLastError(
2682 VE_BAD_FILE, kTraceError,
2683 "StartPlayingFileAsMicrophone NULL as input stream");
2684 return -1;
2685 }
2686
2687 if (_inputFilePlaying)
2688 {
2689 _engineStatisticsPtr->SetLastError(
2690 VE_ALREADY_PLAYING, kTraceWarning,
2691 "StartPlayingFileAsMicrophone() is playing");
2692 return 0;
2693 }
2694
mflodman@webrtc.org9a065d12012-03-07 08:12:21 +00002695 CriticalSectionScoped cs(&_fileCritSect);
niklase@google.com470e71d2011-07-07 08:21:25 +00002696
2697 // Destroy the old instance
2698 if (_inputFilePlayerPtr)
2699 {
2700 _inputFilePlayerPtr->RegisterModuleFileCallback(NULL);
2701 FilePlayer::DestroyFilePlayer(_inputFilePlayerPtr);
2702 _inputFilePlayerPtr = NULL;
2703 }
2704
2705 // Create the instance
2706 _inputFilePlayerPtr = FilePlayer::CreateFilePlayer(
2707 _inputFilePlayerId, (const FileFormats)format);
2708
2709 if (_inputFilePlayerPtr == NULL)
2710 {
2711 _engineStatisticsPtr->SetLastError(
2712 VE_INVALID_ARGUMENT, kTraceError,
2713 "StartPlayingInputFile() filePlayer format isnot correct");
2714 return -1;
2715 }
2716
pbos@webrtc.org6141e132013-04-09 10:09:10 +00002717 const uint32_t notificationTime(0);
niklase@google.com470e71d2011-07-07 08:21:25 +00002718
2719 if (_inputFilePlayerPtr->StartPlayingFile(*stream, startPosition,
2720 volumeScaling, notificationTime,
2721 stopPosition, codecInst) != 0)
2722 {
2723 _engineStatisticsPtr->SetLastError(VE_BAD_FILE, kTraceError,
2724 "StartPlayingFile() failed to start "
2725 "file playout");
2726 _inputFilePlayerPtr->StopPlayingFile();
2727 FilePlayer::DestroyFilePlayer(_inputFilePlayerPtr);
2728 _inputFilePlayerPtr = NULL;
2729 return -1;
2730 }
andrew@webrtc.orgae1a58b2013-01-22 04:44:30 +00002731
niklase@google.com470e71d2011-07-07 08:21:25 +00002732 _inputFilePlayerPtr->RegisterModuleFileCallback(this);
2733 _inputFilePlaying = true;
2734
2735 return 0;
2736}
2737
2738int Channel::StopPlayingFileAsMicrophone()
2739{
2740 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId,_channelId),
2741 "Channel::StopPlayingFileAsMicrophone()");
2742
2743 if (!_inputFilePlaying)
2744 {
2745 _engineStatisticsPtr->SetLastError(
2746 VE_INVALID_OPERATION, kTraceWarning,
2747 "StopPlayingFileAsMicrophone() isnot playing");
2748 return 0;
2749 }
2750
mflodman@webrtc.org9a065d12012-03-07 08:12:21 +00002751 CriticalSectionScoped cs(&_fileCritSect);
niklase@google.com470e71d2011-07-07 08:21:25 +00002752 if (_inputFilePlayerPtr->StopPlayingFile() != 0)
2753 {
2754 _engineStatisticsPtr->SetLastError(
2755 VE_STOP_RECORDING_FAILED, kTraceError,
2756 "StopPlayingFile() could not stop playing");
2757 return -1;
2758 }
2759 _inputFilePlayerPtr->RegisterModuleFileCallback(NULL);
2760 FilePlayer::DestroyFilePlayer(_inputFilePlayerPtr);
2761 _inputFilePlayerPtr = NULL;
2762 _inputFilePlaying = false;
2763
2764 return 0;
2765}
2766
2767int Channel::IsPlayingFileAsMicrophone() const
2768{
2769 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId,_channelId),
2770 "Channel::IsPlayingFileAsMicrophone()");
2771
2772 return _inputFilePlaying;
2773}
2774
pbos@webrtc.org92135212013-05-14 08:31:39 +00002775int Channel::ScaleFileAsMicrophonePlayout(float scale)
niklase@google.com470e71d2011-07-07 08:21:25 +00002776{
2777 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId,_channelId),
2778 "Channel::ScaleFileAsMicrophonePlayout(scale=%5.3f)", scale);
2779
mflodman@webrtc.org9a065d12012-03-07 08:12:21 +00002780 CriticalSectionScoped cs(&_fileCritSect);
niklase@google.com470e71d2011-07-07 08:21:25 +00002781
2782 if (!_inputFilePlaying)
2783 {
2784 _engineStatisticsPtr->SetLastError(
2785 VE_INVALID_OPERATION, kTraceError,
2786 "ScaleFileAsMicrophonePlayout() isnot playing");
2787 return -1;
2788 }
2789
2790 if ((_inputFilePlayerPtr == NULL) ||
2791 (_inputFilePlayerPtr->SetAudioScaling(scale) != 0))
2792 {
2793 _engineStatisticsPtr->SetLastError(
2794 VE_BAD_ARGUMENT, kTraceError,
2795 "SetAudioScaling() failed to scale playout");
2796 return -1;
2797 }
2798
2799 return 0;
2800}
2801
leozwang@webrtc.org813e4b02012-03-01 18:34:25 +00002802int Channel::StartRecordingPlayout(const char* fileName,
niklase@google.com470e71d2011-07-07 08:21:25 +00002803 const CodecInst* codecInst)
2804{
2805 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId,_channelId),
2806 "Channel::StartRecordingPlayout(fileName=%s)", fileName);
2807
2808 if (_outputFileRecording)
2809 {
2810 WEBRTC_TRACE(kTraceWarning, kTraceVoice, VoEId(_instanceId,-1),
2811 "StartRecordingPlayout() is already recording");
2812 return 0;
2813 }
2814
2815 FileFormats format;
pbos@webrtc.org6141e132013-04-09 10:09:10 +00002816 const uint32_t notificationTime(0); // Not supported in VoE
niklase@google.com470e71d2011-07-07 08:21:25 +00002817 CodecInst dummyCodec={100,"L16",16000,320,1,320000};
2818
niklas.enbom@webrtc.org40197d72012-03-26 08:45:47 +00002819 if ((codecInst != NULL) &&
2820 ((codecInst->channels < 1) || (codecInst->channels > 2)))
niklase@google.com470e71d2011-07-07 08:21:25 +00002821 {
2822 _engineStatisticsPtr->SetLastError(
2823 VE_BAD_ARGUMENT, kTraceError,
2824 "StartRecordingPlayout() invalid compression");
2825 return(-1);
2826 }
2827 if(codecInst == NULL)
2828 {
2829 format = kFileFormatPcm16kHzFile;
2830 codecInst=&dummyCodec;
2831 }
2832 else if((STR_CASE_CMP(codecInst->plname,"L16") == 0) ||
2833 (STR_CASE_CMP(codecInst->plname,"PCMU") == 0) ||
2834 (STR_CASE_CMP(codecInst->plname,"PCMA") == 0))
2835 {
2836 format = kFileFormatWavFile;
2837 }
2838 else
2839 {
2840 format = kFileFormatCompressedFile;
2841 }
2842
mflodman@webrtc.org9a065d12012-03-07 08:12:21 +00002843 CriticalSectionScoped cs(&_fileCritSect);
niklase@google.com470e71d2011-07-07 08:21:25 +00002844
2845 // Destroy the old instance
2846 if (_outputFileRecorderPtr)
2847 {
2848 _outputFileRecorderPtr->RegisterModuleFileCallback(NULL);
2849 FileRecorder::DestroyFileRecorder(_outputFileRecorderPtr);
2850 _outputFileRecorderPtr = NULL;
2851 }
2852
2853 _outputFileRecorderPtr = FileRecorder::CreateFileRecorder(
2854 _outputFileRecorderId, (const FileFormats)format);
2855 if (_outputFileRecorderPtr == NULL)
2856 {
2857 _engineStatisticsPtr->SetLastError(
2858 VE_INVALID_ARGUMENT, kTraceError,
2859 "StartRecordingPlayout() fileRecorder format isnot correct");
2860 return -1;
2861 }
2862
2863 if (_outputFileRecorderPtr->StartRecordingAudioFile(
2864 fileName, (const CodecInst&)*codecInst, notificationTime) != 0)
2865 {
2866 _engineStatisticsPtr->SetLastError(
2867 VE_BAD_FILE, kTraceError,
2868 "StartRecordingAudioFile() failed to start file recording");
2869 _outputFileRecorderPtr->StopRecording();
2870 FileRecorder::DestroyFileRecorder(_outputFileRecorderPtr);
2871 _outputFileRecorderPtr = NULL;
2872 return -1;
2873 }
2874 _outputFileRecorderPtr->RegisterModuleFileCallback(this);
2875 _outputFileRecording = true;
2876
2877 return 0;
2878}
2879
2880int Channel::StartRecordingPlayout(OutStream* stream,
2881 const CodecInst* codecInst)
2882{
2883 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId,_channelId),
2884 "Channel::StartRecordingPlayout()");
2885
2886 if (_outputFileRecording)
2887 {
2888 WEBRTC_TRACE(kTraceWarning, kTraceVoice, VoEId(_instanceId,-1),
2889 "StartRecordingPlayout() is already recording");
2890 return 0;
2891 }
2892
2893 FileFormats format;
pbos@webrtc.org6141e132013-04-09 10:09:10 +00002894 const uint32_t notificationTime(0); // Not supported in VoE
niklase@google.com470e71d2011-07-07 08:21:25 +00002895 CodecInst dummyCodec={100,"L16",16000,320,1,320000};
2896
2897 if (codecInst != NULL && codecInst->channels != 1)
2898 {
2899 _engineStatisticsPtr->SetLastError(
2900 VE_BAD_ARGUMENT, kTraceError,
2901 "StartRecordingPlayout() invalid compression");
2902 return(-1);
2903 }
2904 if(codecInst == NULL)
2905 {
2906 format = kFileFormatPcm16kHzFile;
2907 codecInst=&dummyCodec;
2908 }
2909 else if((STR_CASE_CMP(codecInst->plname,"L16") == 0) ||
2910 (STR_CASE_CMP(codecInst->plname,"PCMU") == 0) ||
2911 (STR_CASE_CMP(codecInst->plname,"PCMA") == 0))
2912 {
2913 format = kFileFormatWavFile;
2914 }
2915 else
2916 {
2917 format = kFileFormatCompressedFile;
2918 }
2919
mflodman@webrtc.org9a065d12012-03-07 08:12:21 +00002920 CriticalSectionScoped cs(&_fileCritSect);
niklase@google.com470e71d2011-07-07 08:21:25 +00002921
2922 // Destroy the old instance
2923 if (_outputFileRecorderPtr)
2924 {
2925 _outputFileRecorderPtr->RegisterModuleFileCallback(NULL);
2926 FileRecorder::DestroyFileRecorder(_outputFileRecorderPtr);
2927 _outputFileRecorderPtr = NULL;
2928 }
2929
2930 _outputFileRecorderPtr = FileRecorder::CreateFileRecorder(
2931 _outputFileRecorderId, (const FileFormats)format);
2932 if (_outputFileRecorderPtr == NULL)
2933 {
2934 _engineStatisticsPtr->SetLastError(
2935 VE_INVALID_ARGUMENT, kTraceError,
2936 "StartRecordingPlayout() fileRecorder format isnot correct");
2937 return -1;
2938 }
2939
2940 if (_outputFileRecorderPtr->StartRecordingAudioFile(*stream, *codecInst,
2941 notificationTime) != 0)
2942 {
2943 _engineStatisticsPtr->SetLastError(VE_BAD_FILE, kTraceError,
2944 "StartRecordingPlayout() failed to "
2945 "start file recording");
2946 _outputFileRecorderPtr->StopRecording();
2947 FileRecorder::DestroyFileRecorder(_outputFileRecorderPtr);
2948 _outputFileRecorderPtr = NULL;
2949 return -1;
2950 }
andrew@webrtc.orgae1a58b2013-01-22 04:44:30 +00002951
niklase@google.com470e71d2011-07-07 08:21:25 +00002952 _outputFileRecorderPtr->RegisterModuleFileCallback(this);
2953 _outputFileRecording = true;
2954
2955 return 0;
2956}
2957
2958int Channel::StopRecordingPlayout()
2959{
2960 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId,-1),
2961 "Channel::StopRecordingPlayout()");
2962
2963 if (!_outputFileRecording)
2964 {
2965 WEBRTC_TRACE(kTraceError, kTraceVoice, VoEId(_instanceId,-1),
2966 "StopRecordingPlayout() isnot recording");
2967 return -1;
2968 }
2969
2970
mflodman@webrtc.org9a065d12012-03-07 08:12:21 +00002971 CriticalSectionScoped cs(&_fileCritSect);
niklase@google.com470e71d2011-07-07 08:21:25 +00002972
2973 if (_outputFileRecorderPtr->StopRecording() != 0)
2974 {
2975 _engineStatisticsPtr->SetLastError(
2976 VE_STOP_RECORDING_FAILED, kTraceError,
2977 "StopRecording() could not stop recording");
2978 return(-1);
2979 }
2980 _outputFileRecorderPtr->RegisterModuleFileCallback(NULL);
2981 FileRecorder::DestroyFileRecorder(_outputFileRecorderPtr);
2982 _outputFileRecorderPtr = NULL;
2983 _outputFileRecording = false;
2984
2985 return 0;
2986}
2987
2988void
2989Channel::SetMixWithMicStatus(bool mix)
2990{
2991 _mixFileWithMicrophone=mix;
2992}
2993
2994int
pbos@webrtc.org6141e132013-04-09 10:09:10 +00002995Channel::GetSpeechOutputLevel(uint32_t& level) const
niklase@google.com470e71d2011-07-07 08:21:25 +00002996{
pbos@webrtc.org6141e132013-04-09 10:09:10 +00002997 int8_t currentLevel = _outputAudioLevel.Level();
2998 level = static_cast<int32_t> (currentLevel);
niklase@google.com470e71d2011-07-07 08:21:25 +00002999 WEBRTC_TRACE(kTraceStateInfo, kTraceVoice,
3000 VoEId(_instanceId,_channelId),
3001 "GetSpeechOutputLevel() => level=%u", level);
3002 return 0;
3003}
3004
3005int
pbos@webrtc.org6141e132013-04-09 10:09:10 +00003006Channel::GetSpeechOutputLevelFullRange(uint32_t& level) const
niklase@google.com470e71d2011-07-07 08:21:25 +00003007{
pbos@webrtc.org6141e132013-04-09 10:09:10 +00003008 int16_t currentLevel = _outputAudioLevel.LevelFullRange();
3009 level = static_cast<int32_t> (currentLevel);
niklase@google.com470e71d2011-07-07 08:21:25 +00003010 WEBRTC_TRACE(kTraceStateInfo, kTraceVoice,
3011 VoEId(_instanceId,_channelId),
3012 "GetSpeechOutputLevelFullRange() => level=%u", level);
3013 return 0;
3014}
3015
3016int
3017Channel::SetMute(bool enable)
3018{
3019 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId,_channelId),
3020 "Channel::SetMute(enable=%d)", enable);
3021 _mute = enable;
3022 return 0;
3023}
3024
3025bool
3026Channel::Mute() const
3027{
3028 return _mute;
3029}
3030
3031int
3032Channel::SetOutputVolumePan(float left, float right)
3033{
3034 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId,_channelId),
3035 "Channel::SetOutputVolumePan()");
3036 _panLeft = left;
3037 _panRight = right;
3038 return 0;
3039}
3040
3041int
3042Channel::GetOutputVolumePan(float& left, float& right) const
3043{
3044 left = _panLeft;
3045 right = _panRight;
3046 WEBRTC_TRACE(kTraceStateInfo, kTraceVoice,
3047 VoEId(_instanceId,_channelId),
3048 "GetOutputVolumePan() => left=%3.2f, right=%3.2f", left, right);
3049 return 0;
3050}
3051
3052int
3053Channel::SetChannelOutputVolumeScaling(float scaling)
3054{
3055 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId,_channelId),
3056 "Channel::SetChannelOutputVolumeScaling()");
3057 _outputGain = scaling;
3058 return 0;
3059}
3060
3061int
3062Channel::GetChannelOutputVolumeScaling(float& scaling) const
3063{
3064 scaling = _outputGain;
3065 WEBRTC_TRACE(kTraceStateInfo, kTraceVoice,
3066 VoEId(_instanceId,_channelId),
3067 "GetChannelOutputVolumeScaling() => scaling=%3.2f", scaling);
3068 return 0;
3069}
3070
niklase@google.com470e71d2011-07-07 08:21:25 +00003071int
3072Channel::RegisterExternalEncryption(Encryption& encryption)
3073{
3074 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId,_channelId),
3075 "Channel::RegisterExternalEncryption()");
3076
mflodman@webrtc.org9a065d12012-03-07 08:12:21 +00003077 CriticalSectionScoped cs(&_callbackCritSect);
niklase@google.com470e71d2011-07-07 08:21:25 +00003078
3079 if (_encryptionPtr)
3080 {
3081 _engineStatisticsPtr->SetLastError(
3082 VE_INVALID_OPERATION, kTraceError,
3083 "RegisterExternalEncryption() encryption already enabled");
3084 return -1;
3085 }
3086
3087 _encryptionPtr = &encryption;
3088
3089 _decrypting = true;
3090 _encrypting = true;
3091
3092 return 0;
3093}
3094
3095int
3096Channel::DeRegisterExternalEncryption()
3097{
3098 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId,_channelId),
3099 "Channel::DeRegisterExternalEncryption()");
3100
mflodman@webrtc.org9a065d12012-03-07 08:12:21 +00003101 CriticalSectionScoped cs(&_callbackCritSect);
niklase@google.com470e71d2011-07-07 08:21:25 +00003102
3103 if (!_encryptionPtr)
3104 {
3105 _engineStatisticsPtr->SetLastError(
3106 VE_INVALID_OPERATION, kTraceWarning,
3107 "DeRegisterExternalEncryption() encryption already disabled");
3108 return 0;
3109 }
3110
3111 _decrypting = false;
3112 _encrypting = false;
3113
3114 _encryptionPtr = NULL;
3115
3116 return 0;
3117}
3118
3119int Channel::SendTelephoneEventOutband(unsigned char eventCode,
3120 int lengthMs, int attenuationDb,
3121 bool playDtmfEvent)
3122{
3123 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId, _channelId),
3124 "Channel::SendTelephoneEventOutband(..., playDtmfEvent=%d)",
3125 playDtmfEvent);
3126
3127 _playOutbandDtmfEvent = playDtmfEvent;
3128
pwestin@webrtc.org2853dde2012-05-11 11:08:54 +00003129 if (_rtpRtcpModule->SendTelephoneEventOutband(eventCode, lengthMs,
niklase@google.com470e71d2011-07-07 08:21:25 +00003130 attenuationDb) != 0)
3131 {
3132 _engineStatisticsPtr->SetLastError(
3133 VE_SEND_DTMF_FAILED,
3134 kTraceWarning,
3135 "SendTelephoneEventOutband() failed to send event");
3136 return -1;
3137 }
3138 return 0;
3139}
3140
3141int Channel::SendTelephoneEventInband(unsigned char eventCode,
3142 int lengthMs,
3143 int attenuationDb,
3144 bool playDtmfEvent)
3145{
3146 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId, _channelId),
3147 "Channel::SendTelephoneEventInband(..., playDtmfEvent=%d)",
3148 playDtmfEvent);
3149
3150 _playInbandDtmfEvent = playDtmfEvent;
3151 _inbandDtmfQueue.AddDtmf(eventCode, lengthMs, attenuationDb);
3152
3153 return 0;
3154}
3155
3156int
3157Channel::SetDtmfPlayoutStatus(bool enable)
3158{
3159 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId,_channelId),
3160 "Channel::SetDtmfPlayoutStatus()");
3161 if (_audioCodingModule.SetDtmfPlayoutStatus(enable) != 0)
3162 {
3163 _engineStatisticsPtr->SetLastError(
3164 VE_AUDIO_CODING_MODULE_ERROR, kTraceWarning,
3165 "SetDtmfPlayoutStatus() failed to set Dtmf playout");
3166 return -1;
3167 }
3168 return 0;
3169}
3170
3171bool
3172Channel::DtmfPlayoutStatus() const
3173{
3174 return _audioCodingModule.DtmfPlayoutStatus();
3175}
3176
3177int
3178Channel::SetSendTelephoneEventPayloadType(unsigned char type)
3179{
3180 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId,_channelId),
3181 "Channel::SetSendTelephoneEventPayloadType()");
andrew@webrtc.orgf81f9f82011-08-19 22:56:22 +00003182 if (type > 127)
niklase@google.com470e71d2011-07-07 08:21:25 +00003183 {
3184 _engineStatisticsPtr->SetLastError(
3185 VE_INVALID_ARGUMENT, kTraceError,
3186 "SetSendTelephoneEventPayloadType() invalid type");
3187 return -1;
3188 }
pwestin@webrtc.org1da1ce02011-10-13 15:19:55 +00003189 CodecInst codec;
3190 codec.plfreq = 8000;
3191 codec.pltype = type;
3192 memcpy(codec.plname, "telephone-event", 16);
pwestin@webrtc.org2853dde2012-05-11 11:08:54 +00003193 if (_rtpRtcpModule->RegisterSendPayload(codec) != 0)
niklase@google.com470e71d2011-07-07 08:21:25 +00003194 {
henrika@webrtc.org4392d5f2013-04-17 07:34:25 +00003195 _rtpRtcpModule->DeRegisterSendPayload(codec.pltype);
3196 if (_rtpRtcpModule->RegisterSendPayload(codec) != 0) {
3197 _engineStatisticsPtr->SetLastError(
3198 VE_RTP_RTCP_MODULE_ERROR, kTraceError,
3199 "SetSendTelephoneEventPayloadType() failed to register send"
3200 "payload type");
3201 return -1;
3202 }
niklase@google.com470e71d2011-07-07 08:21:25 +00003203 }
3204 _sendTelephoneEventPayloadType = type;
3205 return 0;
3206}
3207
3208int
3209Channel::GetSendTelephoneEventPayloadType(unsigned char& type)
3210{
3211 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId,_channelId),
3212 "Channel::GetSendTelephoneEventPayloadType()");
3213 type = _sendTelephoneEventPayloadType;
3214 WEBRTC_TRACE(kTraceStateInfo, kTraceVoice,
3215 VoEId(_instanceId,_channelId),
3216 "GetSendTelephoneEventPayloadType() => type=%u", type);
3217 return 0;
3218}
3219
niklase@google.com470e71d2011-07-07 08:21:25 +00003220int
3221Channel::UpdateRxVadDetection(AudioFrame& audioFrame)
3222{
3223 WEBRTC_TRACE(kTraceStream, kTraceVoice, VoEId(_instanceId,_channelId),
3224 "Channel::UpdateRxVadDetection()");
3225
3226 int vadDecision = 1;
3227
andrew@webrtc.org63a50982012-05-02 23:56:37 +00003228 vadDecision = (audioFrame.vad_activity_ == AudioFrame::kVadActive)? 1 : 0;
niklase@google.com470e71d2011-07-07 08:21:25 +00003229
3230 if ((vadDecision != _oldVadDecision) && _rxVadObserverPtr)
3231 {
3232 OnRxVadDetected(vadDecision);
3233 _oldVadDecision = vadDecision;
3234 }
3235
3236 WEBRTC_TRACE(kTraceStream, kTraceVoice, VoEId(_instanceId,_channelId),
3237 "Channel::UpdateRxVadDetection() => vadDecision=%d",
3238 vadDecision);
3239 return 0;
3240}
3241
3242int
3243Channel::RegisterRxVadObserver(VoERxVadCallback &observer)
3244{
3245 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId,_channelId),
3246 "Channel::RegisterRxVadObserver()");
mflodman@webrtc.org9a065d12012-03-07 08:12:21 +00003247 CriticalSectionScoped cs(&_callbackCritSect);
niklase@google.com470e71d2011-07-07 08:21:25 +00003248
3249 if (_rxVadObserverPtr)
3250 {
3251 _engineStatisticsPtr->SetLastError(
3252 VE_INVALID_OPERATION, kTraceError,
3253 "RegisterRxVadObserver() observer already enabled");
3254 return -1;
3255 }
niklase@google.com470e71d2011-07-07 08:21:25 +00003256 _rxVadObserverPtr = &observer;
3257 _RxVadDetection = true;
3258 return 0;
3259}
3260
3261int
3262Channel::DeRegisterRxVadObserver()
3263{
3264 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId,_channelId),
3265 "Channel::DeRegisterRxVadObserver()");
mflodman@webrtc.org9a065d12012-03-07 08:12:21 +00003266 CriticalSectionScoped cs(&_callbackCritSect);
niklase@google.com470e71d2011-07-07 08:21:25 +00003267
3268 if (!_rxVadObserverPtr)
3269 {
3270 _engineStatisticsPtr->SetLastError(
3271 VE_INVALID_OPERATION, kTraceWarning,
3272 "DeRegisterRxVadObserver() observer already disabled");
3273 return 0;
3274 }
3275 _rxVadObserverPtr = NULL;
3276 _RxVadDetection = false;
3277 return 0;
3278}
3279
3280int
3281Channel::VoiceActivityIndicator(int &activity)
3282{
3283 activity = _sendFrameType;
3284
3285 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId,_channelId),
3286 "Channel::VoiceActivityIndicator(indicator=%d)", activity);
3287 return 0;
3288}
3289
3290#ifdef WEBRTC_VOICE_ENGINE_AGC
3291
3292int
pbos@webrtc.org92135212013-05-14 08:31:39 +00003293Channel::SetRxAgcStatus(bool enable, AgcModes mode)
niklase@google.com470e71d2011-07-07 08:21:25 +00003294{
3295 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId,_channelId),
3296 "Channel::SetRxAgcStatus(enable=%d, mode=%d)",
3297 (int)enable, (int)mode);
3298
3299 GainControl::Mode agcMode(GainControl::kFixedDigital);
3300 switch (mode)
3301 {
3302 case kAgcDefault:
3303 agcMode = GainControl::kAdaptiveDigital;
3304 break;
3305 case kAgcUnchanged:
3306 agcMode = _rxAudioProcessingModulePtr->gain_control()->mode();
3307 break;
3308 case kAgcFixedDigital:
3309 agcMode = GainControl::kFixedDigital;
3310 break;
3311 case kAgcAdaptiveDigital:
3312 agcMode =GainControl::kAdaptiveDigital;
3313 break;
3314 default:
3315 _engineStatisticsPtr->SetLastError(
3316 VE_INVALID_ARGUMENT, kTraceError,
3317 "SetRxAgcStatus() invalid Agc mode");
3318 return -1;
3319 }
3320
3321 if (_rxAudioProcessingModulePtr->gain_control()->set_mode(agcMode) != 0)
3322 {
3323 _engineStatisticsPtr->SetLastError(
3324 VE_APM_ERROR, kTraceError,
3325 "SetRxAgcStatus() failed to set Agc mode");
3326 return -1;
3327 }
3328 if (_rxAudioProcessingModulePtr->gain_control()->Enable(enable) != 0)
3329 {
3330 _engineStatisticsPtr->SetLastError(
3331 VE_APM_ERROR, kTraceError,
3332 "SetRxAgcStatus() failed to set Agc state");
3333 return -1;
3334 }
3335
3336 _rxAgcIsEnabled = enable;
niklase@google.com470e71d2011-07-07 08:21:25 +00003337 _rxApmIsEnabled = ((_rxAgcIsEnabled == true) || (_rxNsIsEnabled == true));
3338
3339 return 0;
3340}
3341
3342int
3343Channel::GetRxAgcStatus(bool& enabled, AgcModes& mode)
3344{
3345 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId,_channelId),
3346 "Channel::GetRxAgcStatus(enable=?, mode=?)");
3347
3348 bool enable = _rxAudioProcessingModulePtr->gain_control()->is_enabled();
3349 GainControl::Mode agcMode =
3350 _rxAudioProcessingModulePtr->gain_control()->mode();
3351
3352 enabled = enable;
3353
3354 switch (agcMode)
3355 {
3356 case GainControl::kFixedDigital:
3357 mode = kAgcFixedDigital;
3358 break;
3359 case GainControl::kAdaptiveDigital:
3360 mode = kAgcAdaptiveDigital;
3361 break;
3362 default:
3363 _engineStatisticsPtr->SetLastError(
3364 VE_APM_ERROR, kTraceError,
3365 "GetRxAgcStatus() invalid Agc mode");
3366 return -1;
3367 }
3368
3369 return 0;
3370}
3371
3372int
pbos@webrtc.org92135212013-05-14 08:31:39 +00003373Channel::SetRxAgcConfig(AgcConfig config)
niklase@google.com470e71d2011-07-07 08:21:25 +00003374{
3375 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId,_channelId),
3376 "Channel::SetRxAgcConfig()");
3377
3378 if (_rxAudioProcessingModulePtr->gain_control()->set_target_level_dbfs(
3379 config.targetLeveldBOv) != 0)
3380 {
3381 _engineStatisticsPtr->SetLastError(
3382 VE_APM_ERROR, kTraceError,
3383 "SetRxAgcConfig() failed to set target peak |level|"
3384 "(or envelope) of the Agc");
3385 return -1;
3386 }
3387 if (_rxAudioProcessingModulePtr->gain_control()->set_compression_gain_db(
3388 config.digitalCompressionGaindB) != 0)
3389 {
3390 _engineStatisticsPtr->SetLastError(
3391 VE_APM_ERROR, kTraceError,
3392 "SetRxAgcConfig() failed to set the range in |gain| the"
3393 " digital compression stage may apply");
3394 return -1;
3395 }
3396 if (_rxAudioProcessingModulePtr->gain_control()->enable_limiter(
3397 config.limiterEnable) != 0)
3398 {
3399 _engineStatisticsPtr->SetLastError(
3400 VE_APM_ERROR, kTraceError,
3401 "SetRxAgcConfig() failed to set hard limiter to the signal");
3402 return -1;
3403 }
3404
3405 return 0;
3406}
3407
3408int
3409Channel::GetRxAgcConfig(AgcConfig& config)
3410{
3411 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId,_channelId),
3412 "Channel::GetRxAgcConfig(config=%?)");
3413
3414 config.targetLeveldBOv =
3415 _rxAudioProcessingModulePtr->gain_control()->target_level_dbfs();
3416 config.digitalCompressionGaindB =
3417 _rxAudioProcessingModulePtr->gain_control()->compression_gain_db();
3418 config.limiterEnable =
3419 _rxAudioProcessingModulePtr->gain_control()->is_limiter_enabled();
3420
3421 WEBRTC_TRACE(kTraceStateInfo, kTraceVoice,
3422 VoEId(_instanceId,_channelId), "GetRxAgcConfig() => "
3423 "targetLeveldBOv=%u, digitalCompressionGaindB=%u,"
3424 " limiterEnable=%d",
3425 config.targetLeveldBOv,
3426 config.digitalCompressionGaindB,
3427 config.limiterEnable);
3428
3429 return 0;
3430}
3431
3432#endif // #ifdef WEBRTC_VOICE_ENGINE_AGC
3433
3434#ifdef WEBRTC_VOICE_ENGINE_NR
3435
3436int
pbos@webrtc.org92135212013-05-14 08:31:39 +00003437Channel::SetRxNsStatus(bool enable, NsModes mode)
niklase@google.com470e71d2011-07-07 08:21:25 +00003438{
3439 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId,_channelId),
3440 "Channel::SetRxNsStatus(enable=%d, mode=%d)",
3441 (int)enable, (int)mode);
3442
3443 NoiseSuppression::Level nsLevel(
3444 (NoiseSuppression::Level)WEBRTC_VOICE_ENGINE_RX_NS_DEFAULT_MODE);
3445 switch (mode)
3446 {
3447
3448 case kNsDefault:
3449 nsLevel = (NoiseSuppression::Level)
3450 WEBRTC_VOICE_ENGINE_RX_NS_DEFAULT_MODE;
3451 break;
3452 case kNsUnchanged:
3453 nsLevel = _rxAudioProcessingModulePtr->noise_suppression()->level();
3454 break;
3455 case kNsConference:
3456 nsLevel = NoiseSuppression::kHigh;
3457 break;
3458 case kNsLowSuppression:
3459 nsLevel = NoiseSuppression::kLow;
3460 break;
3461 case kNsModerateSuppression:
3462 nsLevel = NoiseSuppression::kModerate;
3463 break;
3464 case kNsHighSuppression:
3465 nsLevel = NoiseSuppression::kHigh;
3466 break;
3467 case kNsVeryHighSuppression:
3468 nsLevel = NoiseSuppression::kVeryHigh;
3469 break;
niklase@google.com470e71d2011-07-07 08:21:25 +00003470 }
3471
3472 if (_rxAudioProcessingModulePtr->noise_suppression()->set_level(nsLevel)
3473 != 0)
3474 {
3475 _engineStatisticsPtr->SetLastError(
3476 VE_APM_ERROR, kTraceError,
3477 "SetRxAgcStatus() failed to set Ns level");
3478 return -1;
3479 }
3480 if (_rxAudioProcessingModulePtr->noise_suppression()->Enable(enable) != 0)
3481 {
3482 _engineStatisticsPtr->SetLastError(
3483 VE_APM_ERROR, kTraceError,
3484 "SetRxAgcStatus() failed to set Agc state");
3485 return -1;
3486 }
3487
3488 _rxNsIsEnabled = enable;
3489 _rxApmIsEnabled = ((_rxAgcIsEnabled == true) || (_rxNsIsEnabled == true));
3490
3491 return 0;
3492}
3493
3494int
3495Channel::GetRxNsStatus(bool& enabled, NsModes& mode)
3496{
3497 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId,_channelId),
3498 "Channel::GetRxNsStatus(enable=?, mode=?)");
3499
3500 bool enable =
3501 _rxAudioProcessingModulePtr->noise_suppression()->is_enabled();
3502 NoiseSuppression::Level ncLevel =
3503 _rxAudioProcessingModulePtr->noise_suppression()->level();
3504
3505 enabled = enable;
3506
3507 switch (ncLevel)
3508 {
3509 case NoiseSuppression::kLow:
3510 mode = kNsLowSuppression;
3511 break;
3512 case NoiseSuppression::kModerate:
3513 mode = kNsModerateSuppression;
3514 break;
3515 case NoiseSuppression::kHigh:
3516 mode = kNsHighSuppression;
3517 break;
3518 case NoiseSuppression::kVeryHigh:
3519 mode = kNsVeryHighSuppression;
3520 break;
niklase@google.com470e71d2011-07-07 08:21:25 +00003521 }
3522
3523 WEBRTC_TRACE(kTraceStateInfo, kTraceVoice,
3524 VoEId(_instanceId,_channelId),
3525 "GetRxNsStatus() => enabled=%d, mode=%d", enabled, mode);
3526 return 0;
3527}
3528
3529#endif // #ifdef WEBRTC_VOICE_ENGINE_NR
3530
3531int
3532Channel::RegisterRTPObserver(VoERTPObserver& observer)
3533{
3534 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId, _channelId),
3535 "Channel::RegisterRTPObserver()");
mflodman@webrtc.org9a065d12012-03-07 08:12:21 +00003536 CriticalSectionScoped cs(&_callbackCritSect);
niklase@google.com470e71d2011-07-07 08:21:25 +00003537
3538 if (_rtpObserverPtr)
3539 {
3540 _engineStatisticsPtr->SetLastError(
3541 VE_INVALID_OPERATION, kTraceError,
3542 "RegisterRTPObserver() observer already enabled");
3543 return -1;
3544 }
3545
3546 _rtpObserverPtr = &observer;
3547 _rtpObserver = true;
3548
3549 return 0;
3550}
3551
3552int
3553Channel::DeRegisterRTPObserver()
3554{
3555 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId,_channelId),
3556 "Channel::DeRegisterRTPObserver()");
mflodman@webrtc.org9a065d12012-03-07 08:12:21 +00003557 CriticalSectionScoped cs(&_callbackCritSect);
niklase@google.com470e71d2011-07-07 08:21:25 +00003558
3559 if (!_rtpObserverPtr)
3560 {
3561 _engineStatisticsPtr->SetLastError(
3562 VE_INVALID_OPERATION, kTraceWarning,
3563 "DeRegisterRTPObserver() observer already disabled");
3564 return 0;
3565 }
3566
3567 _rtpObserver = false;
3568 _rtpObserverPtr = NULL;
3569
3570 return 0;
3571}
3572
3573int
3574Channel::RegisterRTCPObserver(VoERTCPObserver& observer)
3575{
3576 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId,_channelId),
3577 "Channel::RegisterRTCPObserver()");
mflodman@webrtc.org9a065d12012-03-07 08:12:21 +00003578 CriticalSectionScoped cs(&_callbackCritSect);
niklase@google.com470e71d2011-07-07 08:21:25 +00003579
3580 if (_rtcpObserverPtr)
3581 {
3582 _engineStatisticsPtr->SetLastError(
3583 VE_INVALID_OPERATION, kTraceError,
3584 "RegisterRTCPObserver() observer already enabled");
3585 return -1;
3586 }
3587
3588 _rtcpObserverPtr = &observer;
3589 _rtcpObserver = true;
3590
3591 return 0;
3592}
3593
3594int
3595Channel::DeRegisterRTCPObserver()
3596{
3597 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId, _channelId),
3598 "Channel::DeRegisterRTCPObserver()");
mflodman@webrtc.org9a065d12012-03-07 08:12:21 +00003599 CriticalSectionScoped cs(&_callbackCritSect);
niklase@google.com470e71d2011-07-07 08:21:25 +00003600
3601 if (!_rtcpObserverPtr)
3602 {
3603 _engineStatisticsPtr->SetLastError(
3604 VE_INVALID_OPERATION, kTraceWarning,
3605 "DeRegisterRTCPObserver() observer already disabled");
3606 return 0;
3607 }
3608
3609 _rtcpObserver = false;
3610 _rtcpObserverPtr = NULL;
3611
3612 return 0;
3613}
3614
3615int
3616Channel::SetLocalSSRC(unsigned int ssrc)
3617{
3618 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId, _channelId),
3619 "Channel::SetLocalSSRC()");
3620 if (_sending)
3621 {
3622 _engineStatisticsPtr->SetLastError(
3623 VE_ALREADY_SENDING, kTraceError,
3624 "SetLocalSSRC() already sending");
3625 return -1;
3626 }
pwestin@webrtc.org2853dde2012-05-11 11:08:54 +00003627 if (_rtpRtcpModule->SetSSRC(ssrc) != 0)
niklase@google.com470e71d2011-07-07 08:21:25 +00003628 {
3629 _engineStatisticsPtr->SetLastError(
3630 VE_RTP_RTCP_MODULE_ERROR, kTraceError,
3631 "SetLocalSSRC() failed to set SSRC");
3632 return -1;
3633 }
3634 return 0;
3635}
3636
3637int
3638Channel::GetLocalSSRC(unsigned int& ssrc)
3639{
pwestin@webrtc.org2853dde2012-05-11 11:08:54 +00003640 ssrc = _rtpRtcpModule->SSRC();
niklase@google.com470e71d2011-07-07 08:21:25 +00003641 WEBRTC_TRACE(kTraceStateInfo, kTraceVoice,
3642 VoEId(_instanceId,_channelId),
3643 "GetLocalSSRC() => ssrc=%lu", ssrc);
3644 return 0;
3645}
3646
3647int
3648Channel::GetRemoteSSRC(unsigned int& ssrc)
3649{
pwestin@webrtc.org2853dde2012-05-11 11:08:54 +00003650 ssrc = _rtpRtcpModule->RemoteSSRC();
niklase@google.com470e71d2011-07-07 08:21:25 +00003651 WEBRTC_TRACE(kTraceStateInfo, kTraceVoice,
3652 VoEId(_instanceId,_channelId),
3653 "GetRemoteSSRC() => ssrc=%lu", ssrc);
3654 return 0;
3655}
3656
3657int
3658Channel::GetRemoteCSRCs(unsigned int arrCSRC[15])
3659{
3660 if (arrCSRC == NULL)
3661 {
3662 _engineStatisticsPtr->SetLastError(
3663 VE_INVALID_ARGUMENT, kTraceError,
3664 "GetRemoteCSRCs() invalid array argument");
3665 return -1;
3666 }
pbos@webrtc.org6141e132013-04-09 10:09:10 +00003667 uint32_t arrOfCSRC[kRtpCsrcSize];
3668 int32_t CSRCs(0);
pwestin@webrtc.org2853dde2012-05-11 11:08:54 +00003669 CSRCs = _rtpRtcpModule->CSRCs(arrOfCSRC);
niklase@google.com470e71d2011-07-07 08:21:25 +00003670 if (CSRCs > 0)
3671 {
pbos@webrtc.org6141e132013-04-09 10:09:10 +00003672 memcpy(arrCSRC, arrOfCSRC, CSRCs * sizeof(uint32_t));
niklase@google.com470e71d2011-07-07 08:21:25 +00003673 for (int i = 0; i < (int) CSRCs; i++)
3674 {
3675 WEBRTC_TRACE(kTraceStateInfo, kTraceVoice,
3676 VoEId(_instanceId, _channelId),
3677 "GetRemoteCSRCs() => arrCSRC[%d]=%lu", i, arrCSRC[i]);
3678 }
3679 } else
3680 {
3681 WEBRTC_TRACE(kTraceStateInfo, kTraceVoice,
3682 VoEId(_instanceId, _channelId),
3683 "GetRemoteCSRCs() => list is empty!");
3684 }
3685 return CSRCs;
3686}
3687
3688int
3689Channel::SetRTPAudioLevelIndicationStatus(bool enable, unsigned char ID)
3690{
andrew@webrtc.org755b04a2011-11-15 16:57:56 +00003691 if (_rtpAudioProc.get() == NULL)
3692 {
3693 _rtpAudioProc.reset(AudioProcessing::Create(VoEModuleId(_instanceId,
3694 _channelId)));
3695 if (_rtpAudioProc.get() == NULL)
3696 {
3697 _engineStatisticsPtr->SetLastError(VE_NO_MEMORY, kTraceCritical,
3698 "Failed to create AudioProcessing");
3699 return -1;
3700 }
3701 }
3702
3703 if (_rtpAudioProc->level_estimator()->Enable(enable) !=
3704 AudioProcessing::kNoError)
3705 {
3706 _engineStatisticsPtr->SetLastError(VE_APM_ERROR, kTraceWarning,
3707 "Failed to enable AudioProcessing::level_estimator()");
3708 }
3709
niklase@google.com470e71d2011-07-07 08:21:25 +00003710 _includeAudioLevelIndication = enable;
stefan@webrtc.orga5cb98c2013-05-29 12:12:51 +00003711 if (enable) {
3712 rtp_header_parser_->RegisterRtpHeaderExtension(kRtpExtensionAudioLevel,
3713 ID);
3714 } else {
3715 rtp_header_parser_->DeregisterRtpHeaderExtension(kRtpExtensionAudioLevel);
3716 }
pwestin@webrtc.org2853dde2012-05-11 11:08:54 +00003717 return _rtpRtcpModule->SetRTPAudioLevelIndicationStatus(enable, ID);
niklase@google.com470e71d2011-07-07 08:21:25 +00003718}
3719int
3720Channel::GetRTPAudioLevelIndicationStatus(bool& enabled, unsigned char& ID)
3721{
3722 WEBRTC_TRACE(kTraceStateInfo, kTraceVoice,
3723 VoEId(_instanceId,_channelId),
3724 "GetRTPAudioLevelIndicationStatus() => enabled=%d, ID=%u",
3725 enabled, ID);
pwestin@webrtc.org2853dde2012-05-11 11:08:54 +00003726 return _rtpRtcpModule->GetRTPAudioLevelIndicationStatus(enabled, ID);
niklase@google.com470e71d2011-07-07 08:21:25 +00003727}
3728
3729int
3730Channel::SetRTCPStatus(bool enable)
3731{
3732 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId,_channelId),
3733 "Channel::SetRTCPStatus()");
pwestin@webrtc.org2853dde2012-05-11 11:08:54 +00003734 if (_rtpRtcpModule->SetRTCPStatus(enable ?
niklase@google.com470e71d2011-07-07 08:21:25 +00003735 kRtcpCompound : kRtcpOff) != 0)
3736 {
3737 _engineStatisticsPtr->SetLastError(
3738 VE_RTP_RTCP_MODULE_ERROR, kTraceError,
3739 "SetRTCPStatus() failed to set RTCP status");
3740 return -1;
3741 }
3742 return 0;
3743}
3744
3745int
3746Channel::GetRTCPStatus(bool& enabled)
3747{
pwestin@webrtc.org2853dde2012-05-11 11:08:54 +00003748 RTCPMethod method = _rtpRtcpModule->RTCP();
niklase@google.com470e71d2011-07-07 08:21:25 +00003749 enabled = (method != kRtcpOff);
3750 WEBRTC_TRACE(kTraceStateInfo, kTraceVoice,
3751 VoEId(_instanceId,_channelId),
3752 "GetRTCPStatus() => enabled=%d", enabled);
3753 return 0;
3754}
3755
3756int
3757Channel::SetRTCP_CNAME(const char cName[256])
3758{
3759 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId, _channelId),
3760 "Channel::SetRTCP_CNAME()");
pwestin@webrtc.org2853dde2012-05-11 11:08:54 +00003761 if (_rtpRtcpModule->SetCNAME(cName) != 0)
niklase@google.com470e71d2011-07-07 08:21:25 +00003762 {
3763 _engineStatisticsPtr->SetLastError(
3764 VE_RTP_RTCP_MODULE_ERROR, kTraceError,
3765 "SetRTCP_CNAME() failed to set RTCP CNAME");
3766 return -1;
3767 }
3768 return 0;
3769}
3770
3771int
3772Channel::GetRTCP_CNAME(char cName[256])
3773{
pwestin@webrtc.org2853dde2012-05-11 11:08:54 +00003774 if (_rtpRtcpModule->CNAME(cName) != 0)
niklase@google.com470e71d2011-07-07 08:21:25 +00003775 {
3776 _engineStatisticsPtr->SetLastError(
3777 VE_RTP_RTCP_MODULE_ERROR, kTraceError,
3778 "GetRTCP_CNAME() failed to retrieve RTCP CNAME");
3779 return -1;
3780 }
3781 WEBRTC_TRACE(kTraceStateInfo, kTraceVoice,
3782 VoEId(_instanceId, _channelId),
3783 "GetRTCP_CNAME() => cName=%s", cName);
3784 return 0;
3785}
3786
3787int
3788Channel::GetRemoteRTCP_CNAME(char cName[256])
3789{
3790 if (cName == NULL)
3791 {
3792 _engineStatisticsPtr->SetLastError(
3793 VE_INVALID_ARGUMENT, kTraceError,
3794 "GetRemoteRTCP_CNAME() invalid CNAME input buffer");
3795 return -1;
3796 }
leozwang@webrtc.org813e4b02012-03-01 18:34:25 +00003797 char cname[RTCP_CNAME_SIZE];
pbos@webrtc.org6141e132013-04-09 10:09:10 +00003798 const uint32_t remoteSSRC = _rtpRtcpModule->RemoteSSRC();
pwestin@webrtc.org2853dde2012-05-11 11:08:54 +00003799 if (_rtpRtcpModule->RemoteCNAME(remoteSSRC, cname) != 0)
niklase@google.com470e71d2011-07-07 08:21:25 +00003800 {
3801 _engineStatisticsPtr->SetLastError(
3802 VE_CANNOT_RETRIEVE_CNAME, kTraceError,
3803 "GetRemoteRTCP_CNAME() failed to retrieve remote RTCP CNAME");
3804 return -1;
3805 }
3806 strcpy(cName, cname);
3807 WEBRTC_TRACE(kTraceStateInfo, kTraceVoice,
3808 VoEId(_instanceId, _channelId),
3809 "GetRemoteRTCP_CNAME() => cName=%s", cName);
3810 return 0;
3811}
3812
3813int
3814Channel::GetRemoteRTCPData(
3815 unsigned int& NTPHigh,
3816 unsigned int& NTPLow,
3817 unsigned int& timestamp,
3818 unsigned int& playoutTimestamp,
3819 unsigned int* jitter,
3820 unsigned short* fractionLost)
3821{
3822 // --- Information from sender info in received Sender Reports
3823
3824 RTCPSenderInfo senderInfo;
pwestin@webrtc.org2853dde2012-05-11 11:08:54 +00003825 if (_rtpRtcpModule->RemoteRTCPStat(&senderInfo) != 0)
niklase@google.com470e71d2011-07-07 08:21:25 +00003826 {
3827 _engineStatisticsPtr->SetLastError(
3828 VE_RTP_RTCP_MODULE_ERROR, kTraceError,
wu@webrtc.orgfcd12b32011-09-15 20:49:50 +00003829 "GetRemoteRTCPData() failed to retrieve sender info for remote "
niklase@google.com470e71d2011-07-07 08:21:25 +00003830 "side");
3831 return -1;
3832 }
3833
3834 // We only utilize 12 out of 20 bytes in the sender info (ignores packet
3835 // and octet count)
3836 NTPHigh = senderInfo.NTPseconds;
3837 NTPLow = senderInfo.NTPfraction;
3838 timestamp = senderInfo.RTPtimeStamp;
3839
3840 WEBRTC_TRACE(kTraceStateInfo, kTraceVoice,
3841 VoEId(_instanceId, _channelId),
3842 "GetRemoteRTCPData() => NTPHigh=%lu, NTPLow=%lu, "
3843 "timestamp=%lu",
3844 NTPHigh, NTPLow, timestamp);
3845
3846 // --- Locally derived information
3847
3848 // This value is updated on each incoming RTCP packet (0 when no packet
3849 // has been received)
pwestin@webrtc.org1de01352013-04-11 20:23:35 +00003850 playoutTimestamp = playout_timestamp_rtcp_;
niklase@google.com470e71d2011-07-07 08:21:25 +00003851
3852 WEBRTC_TRACE(kTraceStateInfo, kTraceVoice,
3853 VoEId(_instanceId, _channelId),
3854 "GetRemoteRTCPData() => playoutTimestamp=%lu",
pwestin@webrtc.org1de01352013-04-11 20:23:35 +00003855 playout_timestamp_rtcp_);
niklase@google.com470e71d2011-07-07 08:21:25 +00003856
3857 if (NULL != jitter || NULL != fractionLost)
3858 {
perkj@webrtc.orgce5990c2012-01-11 13:00:08 +00003859 // Get all RTCP receiver report blocks that have been received on this
3860 // channel. If we receive RTP packets from a remote source we know the
3861 // remote SSRC and use the report block from him.
3862 // Otherwise use the first report block.
3863 std::vector<RTCPReportBlock> remote_stats;
pwestin@webrtc.org2853dde2012-05-11 11:08:54 +00003864 if (_rtpRtcpModule->RemoteRTCPStat(&remote_stats) != 0 ||
perkj@webrtc.orgce5990c2012-01-11 13:00:08 +00003865 remote_stats.empty()) {
3866 WEBRTC_TRACE(kTraceWarning, kTraceVoice,
3867 VoEId(_instanceId, _channelId),
3868 "GetRemoteRTCPData() failed to measure statistics due"
3869 " to lack of received RTP and/or RTCP packets");
3870 return -1;
niklase@google.com470e71d2011-07-07 08:21:25 +00003871 }
perkj@webrtc.orgce5990c2012-01-11 13:00:08 +00003872
pbos@webrtc.org6141e132013-04-09 10:09:10 +00003873 uint32_t remoteSSRC = _rtpRtcpModule->RemoteSSRC();
perkj@webrtc.orgce5990c2012-01-11 13:00:08 +00003874 std::vector<RTCPReportBlock>::const_iterator it = remote_stats.begin();
3875 for (; it != remote_stats.end(); ++it) {
3876 if (it->remoteSSRC == remoteSSRC)
3877 break;
niklase@google.com470e71d2011-07-07 08:21:25 +00003878 }
perkj@webrtc.orgce5990c2012-01-11 13:00:08 +00003879
3880 if (it == remote_stats.end()) {
3881 // If we have not received any RTCP packets from this SSRC it probably
3882 // means that we have not received any RTP packets.
3883 // Use the first received report block instead.
3884 it = remote_stats.begin();
3885 remoteSSRC = it->remoteSSRC;
niklase@google.com470e71d2011-07-07 08:21:25 +00003886 }
perkj@webrtc.orgce5990c2012-01-11 13:00:08 +00003887
xians@webrtc.org79af7342012-01-31 12:22:14 +00003888 if (jitter) {
3889 *jitter = it->jitter;
3890 WEBRTC_TRACE(kTraceStateInfo, kTraceVoice,
3891 VoEId(_instanceId, _channelId),
3892 "GetRemoteRTCPData() => jitter = %lu", *jitter);
3893 }
perkj@webrtc.orgce5990c2012-01-11 13:00:08 +00003894
xians@webrtc.org79af7342012-01-31 12:22:14 +00003895 if (fractionLost) {
3896 *fractionLost = it->fractionLost;
3897 WEBRTC_TRACE(kTraceStateInfo, kTraceVoice,
3898 VoEId(_instanceId, _channelId),
3899 "GetRemoteRTCPData() => fractionLost = %lu",
3900 *fractionLost);
3901 }
niklase@google.com470e71d2011-07-07 08:21:25 +00003902 }
3903 return 0;
3904}
3905
3906int
pbos@webrtc.org92135212013-05-14 08:31:39 +00003907Channel::SendApplicationDefinedRTCPPacket(unsigned char subType,
niklase@google.com470e71d2011-07-07 08:21:25 +00003908 unsigned int name,
3909 const char* data,
3910 unsigned short dataLengthInBytes)
3911{
3912 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId, _channelId),
3913 "Channel::SendApplicationDefinedRTCPPacket()");
3914 if (!_sending)
3915 {
3916 _engineStatisticsPtr->SetLastError(
3917 VE_NOT_SENDING, kTraceError,
3918 "SendApplicationDefinedRTCPPacket() not sending");
3919 return -1;
3920 }
3921 if (NULL == data)
3922 {
3923 _engineStatisticsPtr->SetLastError(
3924 VE_INVALID_ARGUMENT, kTraceError,
3925 "SendApplicationDefinedRTCPPacket() invalid data value");
3926 return -1;
3927 }
3928 if (dataLengthInBytes % 4 != 0)
3929 {
3930 _engineStatisticsPtr->SetLastError(
3931 VE_INVALID_ARGUMENT, kTraceError,
3932 "SendApplicationDefinedRTCPPacket() invalid length value");
3933 return -1;
3934 }
pwestin@webrtc.org2853dde2012-05-11 11:08:54 +00003935 RTCPMethod status = _rtpRtcpModule->RTCP();
niklase@google.com470e71d2011-07-07 08:21:25 +00003936 if (status == kRtcpOff)
3937 {
3938 _engineStatisticsPtr->SetLastError(
3939 VE_RTCP_ERROR, kTraceError,
3940 "SendApplicationDefinedRTCPPacket() RTCP is disabled");
3941 return -1;
3942 }
3943
3944 // Create and schedule the RTCP APP packet for transmission
pwestin@webrtc.org2853dde2012-05-11 11:08:54 +00003945 if (_rtpRtcpModule->SetRTCPApplicationSpecificData(
niklase@google.com470e71d2011-07-07 08:21:25 +00003946 subType,
3947 name,
3948 (const unsigned char*) data,
3949 dataLengthInBytes) != 0)
3950 {
3951 _engineStatisticsPtr->SetLastError(
3952 VE_SEND_ERROR, kTraceError,
3953 "SendApplicationDefinedRTCPPacket() failed to send RTCP packet");
3954 return -1;
3955 }
3956 return 0;
3957}
3958
3959int
3960Channel::GetRTPStatistics(
3961 unsigned int& averageJitterMs,
3962 unsigned int& maxJitterMs,
3963 unsigned int& discardedPackets)
3964{
pbos@webrtc.org6141e132013-04-09 10:09:10 +00003965 uint8_t fraction_lost(0);
3966 uint32_t cum_lost(0);
3967 uint32_t ext_max(0);
3968 uint32_t jitter(0);
3969 uint32_t max_jitter(0);
niklase@google.com470e71d2011-07-07 08:21:25 +00003970
3971 // The jitter statistics is updated for each received RTP packet and is
3972 // based on received packets.
pwestin@webrtc.org2853dde2012-05-11 11:08:54 +00003973 if (_rtpRtcpModule->StatisticsRTP(&fraction_lost,
niklase@google.com470e71d2011-07-07 08:21:25 +00003974 &cum_lost,
3975 &ext_max,
3976 &jitter,
3977 &max_jitter) != 0)
3978 {
3979 _engineStatisticsPtr->SetLastError(
3980 VE_CANNOT_RETRIEVE_RTP_STAT, kTraceWarning,
wu@webrtc.orgfcd12b32011-09-15 20:49:50 +00003981 "GetRTPStatistics() failed to read RTP statistics from the "
niklase@google.com470e71d2011-07-07 08:21:25 +00003982 "RTP/RTCP module");
3983 }
3984
pbos@webrtc.org6141e132013-04-09 10:09:10 +00003985 const int32_t playoutFrequency =
niklase@google.com470e71d2011-07-07 08:21:25 +00003986 _audioCodingModule.PlayoutFrequency();
3987 if (playoutFrequency > 0)
3988 {
3989 // Scale RTP statistics given the current playout frequency
3990 maxJitterMs = max_jitter / (playoutFrequency / 1000);
3991 averageJitterMs = jitter / (playoutFrequency / 1000);
3992 }
3993
3994 discardedPackets = _numberOfDiscardedPackets;
3995
3996 WEBRTC_TRACE(kTraceStateInfo, kTraceVoice,
3997 VoEId(_instanceId, _channelId),
3998 "GetRTPStatistics() => averageJitterMs = %lu, maxJitterMs = %lu,"
wu@webrtc.orgfcd12b32011-09-15 20:49:50 +00003999 " discardedPackets = %lu)",
niklase@google.com470e71d2011-07-07 08:21:25 +00004000 averageJitterMs, maxJitterMs, discardedPackets);
4001 return 0;
4002}
4003
henrika@webrtc.org8a2fc882012-08-22 08:53:55 +00004004int Channel::GetRemoteRTCPSenderInfo(SenderInfo* sender_info) {
4005 if (sender_info == NULL) {
4006 _engineStatisticsPtr->SetLastError(VE_INVALID_ARGUMENT, kTraceError,
4007 "GetRemoteRTCPSenderInfo() invalid sender_info.");
4008 return -1;
4009 }
4010
4011 // Get the sender info from the latest received RTCP Sender Report.
4012 RTCPSenderInfo rtcp_sender_info;
4013 if (_rtpRtcpModule->RemoteRTCPStat(&rtcp_sender_info) != 0) {
4014 _engineStatisticsPtr->SetLastError(VE_RTP_RTCP_MODULE_ERROR, kTraceError,
4015 "GetRemoteRTCPSenderInfo() failed to read RTCP SR sender info.");
4016 return -1;
4017 }
4018
4019 sender_info->NTP_timestamp_high = rtcp_sender_info.NTPseconds;
4020 sender_info->NTP_timestamp_low = rtcp_sender_info.NTPfraction;
4021 sender_info->RTP_timestamp = rtcp_sender_info.RTPtimeStamp;
4022 sender_info->sender_packet_count = rtcp_sender_info.sendPacketCount;
4023 sender_info->sender_octet_count = rtcp_sender_info.sendOctetCount;
4024 return 0;
4025}
4026
4027int Channel::GetRemoteRTCPReportBlocks(
4028 std::vector<ReportBlock>* report_blocks) {
4029 if (report_blocks == NULL) {
4030 _engineStatisticsPtr->SetLastError(VE_INVALID_ARGUMENT, kTraceError,
4031 "GetRemoteRTCPReportBlock()s invalid report_blocks.");
4032 return -1;
4033 }
4034
4035 // Get the report blocks from the latest received RTCP Sender or Receiver
4036 // Report. Each element in the vector contains the sender's SSRC and a
4037 // report block according to RFC 3550.
4038 std::vector<RTCPReportBlock> rtcp_report_blocks;
4039 if (_rtpRtcpModule->RemoteRTCPStat(&rtcp_report_blocks) != 0) {
4040 _engineStatisticsPtr->SetLastError(VE_RTP_RTCP_MODULE_ERROR, kTraceError,
4041 "GetRemoteRTCPReportBlocks() failed to read RTCP SR/RR report block.");
4042 return -1;
4043 }
4044
4045 if (rtcp_report_blocks.empty())
4046 return 0;
4047
4048 std::vector<RTCPReportBlock>::const_iterator it = rtcp_report_blocks.begin();
4049 for (; it != rtcp_report_blocks.end(); ++it) {
4050 ReportBlock report_block;
4051 report_block.sender_SSRC = it->remoteSSRC;
4052 report_block.source_SSRC = it->sourceSSRC;
4053 report_block.fraction_lost = it->fractionLost;
4054 report_block.cumulative_num_packets_lost = it->cumulativeLost;
4055 report_block.extended_highest_sequence_number = it->extendedHighSeqNum;
4056 report_block.interarrival_jitter = it->jitter;
4057 report_block.last_SR_timestamp = it->lastSR;
4058 report_block.delay_since_last_SR = it->delaySinceLastSR;
4059 report_blocks->push_back(report_block);
4060 }
4061 return 0;
4062}
4063
niklase@google.com470e71d2011-07-07 08:21:25 +00004064int
4065Channel::GetRTPStatistics(CallStatistics& stats)
4066{
pbos@webrtc.org6141e132013-04-09 10:09:10 +00004067 uint8_t fraction_lost(0);
4068 uint32_t cum_lost(0);
4069 uint32_t ext_max(0);
4070 uint32_t jitter(0);
4071 uint32_t max_jitter(0);
niklase@google.com470e71d2011-07-07 08:21:25 +00004072
4073 // --- Part one of the final structure (four values)
4074
4075 // The jitter statistics is updated for each received RTP packet and is
4076 // based on received packets.
pwestin@webrtc.org2853dde2012-05-11 11:08:54 +00004077 if (_rtpRtcpModule->StatisticsRTP(&fraction_lost,
niklase@google.com470e71d2011-07-07 08:21:25 +00004078 &cum_lost,
4079 &ext_max,
4080 &jitter,
4081 &max_jitter) != 0)
4082 {
4083 _engineStatisticsPtr->SetLastError(
4084 VE_CANNOT_RETRIEVE_RTP_STAT, kTraceWarning,
4085 "GetRTPStatistics() failed to read RTP statistics from the "
4086 "RTP/RTCP module");
4087 }
4088
4089 stats.fractionLost = fraction_lost;
4090 stats.cumulativeLost = cum_lost;
4091 stats.extendedMax = ext_max;
4092 stats.jitterSamples = jitter;
4093
4094 WEBRTC_TRACE(kTraceStateInfo, kTraceVoice,
4095 VoEId(_instanceId, _channelId),
4096 "GetRTPStatistics() => fractionLost=%lu, cumulativeLost=%lu,"
wu@webrtc.orgfcd12b32011-09-15 20:49:50 +00004097 " extendedMax=%lu, jitterSamples=%li)",
niklase@google.com470e71d2011-07-07 08:21:25 +00004098 stats.fractionLost, stats.cumulativeLost, stats.extendedMax,
4099 stats.jitterSamples);
4100
4101 // --- Part two of the final structure (one value)
4102
pbos@webrtc.org6141e132013-04-09 10:09:10 +00004103 uint16_t RTT(0);
pwestin@webrtc.org2853dde2012-05-11 11:08:54 +00004104 RTCPMethod method = _rtpRtcpModule->RTCP();
niklase@google.com470e71d2011-07-07 08:21:25 +00004105 if (method == kRtcpOff)
4106 {
4107 WEBRTC_TRACE(kTraceWarning, kTraceVoice,
4108 VoEId(_instanceId, _channelId),
wu@webrtc.orgfcd12b32011-09-15 20:49:50 +00004109 "GetRTPStatistics() RTCP is disabled => valid RTT "
niklase@google.com470e71d2011-07-07 08:21:25 +00004110 "measurements cannot be retrieved");
4111 } else
4112 {
4113 // The remote SSRC will be zero if no RTP packet has been received.
pbos@webrtc.org6141e132013-04-09 10:09:10 +00004114 uint32_t remoteSSRC = _rtpRtcpModule->RemoteSSRC();
niklase@google.com470e71d2011-07-07 08:21:25 +00004115 if (remoteSSRC > 0)
4116 {
pbos@webrtc.org6141e132013-04-09 10:09:10 +00004117 uint16_t avgRTT(0);
4118 uint16_t maxRTT(0);
4119 uint16_t minRTT(0);
niklase@google.com470e71d2011-07-07 08:21:25 +00004120
pwestin@webrtc.org2853dde2012-05-11 11:08:54 +00004121 if (_rtpRtcpModule->RTT(remoteSSRC, &RTT, &avgRTT, &minRTT, &maxRTT)
niklase@google.com470e71d2011-07-07 08:21:25 +00004122 != 0)
4123 {
4124 WEBRTC_TRACE(kTraceWarning, kTraceVoice,
4125 VoEId(_instanceId, _channelId),
wu@webrtc.orgfcd12b32011-09-15 20:49:50 +00004126 "GetRTPStatistics() failed to retrieve RTT from "
niklase@google.com470e71d2011-07-07 08:21:25 +00004127 "the RTP/RTCP module");
4128 }
4129 } else
4130 {
4131 WEBRTC_TRACE(kTraceWarning, kTraceVoice,
4132 VoEId(_instanceId, _channelId),
wu@webrtc.orgfcd12b32011-09-15 20:49:50 +00004133 "GetRTPStatistics() failed to measure RTT since no "
niklase@google.com470e71d2011-07-07 08:21:25 +00004134 "RTP packets have been received yet");
4135 }
4136 }
4137
4138 stats.rttMs = static_cast<int> (RTT);
4139
4140 WEBRTC_TRACE(kTraceStateInfo, kTraceVoice,
4141 VoEId(_instanceId, _channelId),
4142 "GetRTPStatistics() => rttMs=%d", stats.rttMs);
4143
4144 // --- Part three of the final structure (four values)
4145
pbos@webrtc.org6141e132013-04-09 10:09:10 +00004146 uint32_t bytesSent(0);
4147 uint32_t packetsSent(0);
4148 uint32_t bytesReceived(0);
4149 uint32_t packetsReceived(0);
niklase@google.com470e71d2011-07-07 08:21:25 +00004150
pwestin@webrtc.org2853dde2012-05-11 11:08:54 +00004151 if (_rtpRtcpModule->DataCountersRTP(&bytesSent,
niklase@google.com470e71d2011-07-07 08:21:25 +00004152 &packetsSent,
4153 &bytesReceived,
4154 &packetsReceived) != 0)
4155 {
4156 WEBRTC_TRACE(kTraceWarning, kTraceVoice,
4157 VoEId(_instanceId, _channelId),
4158 "GetRTPStatistics() failed to retrieve RTP datacounters =>"
wu@webrtc.orgfcd12b32011-09-15 20:49:50 +00004159 " output will not be complete");
niklase@google.com470e71d2011-07-07 08:21:25 +00004160 }
4161
4162 stats.bytesSent = bytesSent;
4163 stats.packetsSent = packetsSent;
4164 stats.bytesReceived = bytesReceived;
4165 stats.packetsReceived = packetsReceived;
4166
4167 WEBRTC_TRACE(kTraceStateInfo, kTraceVoice,
4168 VoEId(_instanceId, _channelId),
4169 "GetRTPStatistics() => bytesSent=%d, packetsSent=%d,"
wu@webrtc.orgfcd12b32011-09-15 20:49:50 +00004170 " bytesReceived=%d, packetsReceived=%d)",
niklase@google.com470e71d2011-07-07 08:21:25 +00004171 stats.bytesSent, stats.packetsSent, stats.bytesReceived,
4172 stats.packetsReceived);
4173
4174 return 0;
4175}
4176
turaj@webrtc.org42259e72012-12-11 02:15:12 +00004177int Channel::SetFECStatus(bool enable, int redPayloadtype) {
4178 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId, _channelId),
4179 "Channel::SetFECStatus()");
niklase@google.com470e71d2011-07-07 08:21:25 +00004180
turaj@webrtc.org8c8ad852013-01-31 18:20:17 +00004181 if (enable) {
4182 if (redPayloadtype < 0 || redPayloadtype > 127) {
4183 _engineStatisticsPtr->SetLastError(
4184 VE_PLTYPE_ERROR, kTraceError,
4185 "SetFECStatus() invalid RED payload type");
4186 return -1;
4187 }
4188
4189 if (SetRedPayloadType(redPayloadtype) < 0) {
4190 _engineStatisticsPtr->SetLastError(
4191 VE_CODEC_ERROR, kTraceError,
4192 "SetSecondarySendCodec() Failed to register RED ACM");
4193 return -1;
4194 }
turaj@webrtc.org42259e72012-12-11 02:15:12 +00004195 }
niklase@google.com470e71d2011-07-07 08:21:25 +00004196
turaj@webrtc.org42259e72012-12-11 02:15:12 +00004197 if (_audioCodingModule.SetFECStatus(enable) != 0) {
4198 _engineStatisticsPtr->SetLastError(
4199 VE_AUDIO_CODING_MODULE_ERROR, kTraceError,
4200 "SetFECStatus() failed to set FEC state in the ACM");
4201 return -1;
4202 }
4203 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +00004204}
4205
4206int
4207Channel::GetFECStatus(bool& enabled, int& redPayloadtype)
4208{
4209 enabled = _audioCodingModule.FECStatus();
4210 if (enabled)
4211 {
pbos@webrtc.org6141e132013-04-09 10:09:10 +00004212 int8_t payloadType(0);
pwestin@webrtc.org2853dde2012-05-11 11:08:54 +00004213 if (_rtpRtcpModule->SendREDPayloadType(payloadType) != 0)
niklase@google.com470e71d2011-07-07 08:21:25 +00004214 {
4215 _engineStatisticsPtr->SetLastError(
4216 VE_RTP_RTCP_MODULE_ERROR, kTraceError,
4217 "GetFECStatus() failed to retrieve RED PT from RTP/RTCP "
4218 "module");
4219 return -1;
4220 }
4221 WEBRTC_TRACE(kTraceStateInfo, kTraceVoice,
4222 VoEId(_instanceId, _channelId),
4223 "GetFECStatus() => enabled=%d, redPayloadtype=%d",
4224 enabled, redPayloadtype);
4225 return 0;
4226 }
4227 WEBRTC_TRACE(kTraceStateInfo, kTraceVoice,
4228 VoEId(_instanceId, _channelId),
4229 "GetFECStatus() => enabled=%d", enabled);
4230 return 0;
4231}
4232
pwestin@webrtc.orgdb249952013-06-05 15:33:20 +00004233void Channel::SetNACKStatus(bool enable, int maxNumberOfPackets) {
4234 // None of these functions can fail.
4235 _rtpRtcpModule->SetStorePacketsStatus(enable, maxNumberOfPackets);
4236 _rtpRtcpModule->SetNACKStatus(enable ? kNackRtcp : kNackOff,
4237 maxNumberOfPackets);
4238}
4239
4240// Called by the ACM when it's missing one or more packets.
4241int Channel::ResendPackets(const uint16_t* sequence_numbers,
4242 int length) {
4243 return _rtpRtcpModule->SendNACK(sequence_numbers, length);
4244}
4245
niklase@google.com470e71d2011-07-07 08:21:25 +00004246int
niklase@google.com470e71d2011-07-07 08:21:25 +00004247Channel::StartRTPDump(const char fileNameUTF8[1024],
4248 RTPDirections direction)
4249{
4250 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId, _channelId),
4251 "Channel::StartRTPDump()");
4252 if ((direction != kRtpIncoming) && (direction != kRtpOutgoing))
4253 {
4254 _engineStatisticsPtr->SetLastError(
4255 VE_INVALID_ARGUMENT, kTraceError,
4256 "StartRTPDump() invalid RTP direction");
4257 return -1;
4258 }
4259 RtpDump* rtpDumpPtr = (direction == kRtpIncoming) ?
4260 &_rtpDumpIn : &_rtpDumpOut;
4261 if (rtpDumpPtr == NULL)
4262 {
4263 assert(false);
4264 return -1;
4265 }
4266 if (rtpDumpPtr->IsActive())
4267 {
4268 rtpDumpPtr->Stop();
4269 }
4270 if (rtpDumpPtr->Start(fileNameUTF8) != 0)
4271 {
4272 _engineStatisticsPtr->SetLastError(
4273 VE_BAD_FILE, kTraceError,
4274 "StartRTPDump() failed to create file");
4275 return -1;
4276 }
4277 return 0;
4278}
4279
4280int
4281Channel::StopRTPDump(RTPDirections direction)
4282{
4283 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId, _channelId),
4284 "Channel::StopRTPDump()");
4285 if ((direction != kRtpIncoming) && (direction != kRtpOutgoing))
4286 {
4287 _engineStatisticsPtr->SetLastError(
4288 VE_INVALID_ARGUMENT, kTraceError,
4289 "StopRTPDump() invalid RTP direction");
4290 return -1;
4291 }
4292 RtpDump* rtpDumpPtr = (direction == kRtpIncoming) ?
4293 &_rtpDumpIn : &_rtpDumpOut;
4294 if (rtpDumpPtr == NULL)
4295 {
4296 assert(false);
4297 return -1;
4298 }
4299 if (!rtpDumpPtr->IsActive())
4300 {
4301 return 0;
4302 }
4303 return rtpDumpPtr->Stop();
4304}
4305
4306bool
4307Channel::RTPDumpIsActive(RTPDirections direction)
4308{
4309 if ((direction != kRtpIncoming) &&
4310 (direction != kRtpOutgoing))
4311 {
4312 _engineStatisticsPtr->SetLastError(
4313 VE_INVALID_ARGUMENT, kTraceError,
4314 "RTPDumpIsActive() invalid RTP direction");
4315 return false;
4316 }
4317 RtpDump* rtpDumpPtr = (direction == kRtpIncoming) ?
4318 &_rtpDumpIn : &_rtpDumpOut;
4319 return rtpDumpPtr->IsActive();
4320}
4321
4322int
4323Channel::InsertExtraRTPPacket(unsigned char payloadType,
4324 bool markerBit,
4325 const char* payloadData,
4326 unsigned short payloadSize)
4327{
4328 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId, _channelId),
4329 "Channel::InsertExtraRTPPacket()");
4330 if (payloadType > 127)
4331 {
4332 _engineStatisticsPtr->SetLastError(
4333 VE_INVALID_PLTYPE, kTraceError,
4334 "InsertExtraRTPPacket() invalid payload type");
4335 return -1;
4336 }
4337 if (payloadData == NULL)
4338 {
4339 _engineStatisticsPtr->SetLastError(
4340 VE_INVALID_ARGUMENT, kTraceError,
4341 "InsertExtraRTPPacket() invalid payload data");
4342 return -1;
4343 }
pwestin@webrtc.org2853dde2012-05-11 11:08:54 +00004344 if (payloadSize > _rtpRtcpModule->MaxDataPayloadLength())
niklase@google.com470e71d2011-07-07 08:21:25 +00004345 {
4346 _engineStatisticsPtr->SetLastError(
4347 VE_INVALID_ARGUMENT, kTraceError,
4348 "InsertExtraRTPPacket() invalid payload size");
4349 return -1;
4350 }
4351 if (!_sending)
4352 {
4353 _engineStatisticsPtr->SetLastError(
4354 VE_NOT_SENDING, kTraceError,
4355 "InsertExtraRTPPacket() not sending");
4356 return -1;
4357 }
4358
4359 // Create extra RTP packet by calling RtpRtcp::SendOutgoingData().
4360 // Transport::SendPacket() will be called by the module when the RTP packet
4361 // is created.
4362 // The call to SendOutgoingData() does *not* modify the timestamp and
4363 // payloadtype to ensure that the RTP module generates a valid RTP packet
4364 // (user might utilize a non-registered payload type).
4365 // The marker bit and payload type will be replaced just before the actual
4366 // transmission, i.e., the actual modification is done *after* the RTP
4367 // module has delivered its RTP packet back to the VoE.
4368 // We will use the stored values above when the packet is modified
4369 // (see Channel::SendPacket()).
4370
4371 _extraPayloadType = payloadType;
4372 _extraMarkerBit = markerBit;
4373 _insertExtraRTPPacket = true;
4374
pwestin@webrtc.org2853dde2012-05-11 11:08:54 +00004375 if (_rtpRtcpModule->SendOutgoingData(kAudioFrameSpeech,
niklase@google.com470e71d2011-07-07 08:21:25 +00004376 _lastPayloadType,
4377 _lastLocalTimeStamp,
stefan@webrtc.orgddfdfed2012-07-03 13:21:22 +00004378 // Leaving the time when this frame was
4379 // received from the capture device as
4380 // undefined for voice for now.
4381 -1,
pbos@webrtc.org6141e132013-04-09 10:09:10 +00004382 (const uint8_t*) payloadData,
niklase@google.com470e71d2011-07-07 08:21:25 +00004383 payloadSize) != 0)
4384 {
4385 _engineStatisticsPtr->SetLastError(
4386 VE_RTP_RTCP_MODULE_ERROR, kTraceError,
4387 "InsertExtraRTPPacket() failed to send extra RTP packet");
4388 return -1;
4389 }
4390
4391 return 0;
4392}
4393
pbos@webrtc.org6141e132013-04-09 10:09:10 +00004394uint32_t
andrew@webrtc.org755b04a2011-11-15 16:57:56 +00004395Channel::Demultiplex(const AudioFrame& audioFrame)
niklase@google.com470e71d2011-07-07 08:21:25 +00004396{
4397 WEBRTC_TRACE(kTraceStream, kTraceVoice, VoEId(_instanceId,_channelId),
andrew@webrtc.org755b04a2011-11-15 16:57:56 +00004398 "Channel::Demultiplex()");
andrew@webrtc.orgae1a58b2013-01-22 04:44:30 +00004399 _audioFrame.CopyFrom(audioFrame);
andrew@webrtc.org63a50982012-05-02 23:56:37 +00004400 _audioFrame.id_ = _channelId;
niklase@google.com470e71d2011-07-07 08:21:25 +00004401 return 0;
4402}
4403
pbos@webrtc.org6141e132013-04-09 10:09:10 +00004404uint32_t
xians@google.com0b0665a2011-08-08 08:18:44 +00004405Channel::PrepareEncodeAndSend(int mixingFrequency)
niklase@google.com470e71d2011-07-07 08:21:25 +00004406{
4407 WEBRTC_TRACE(kTraceStream, kTraceVoice, VoEId(_instanceId,_channelId),
4408 "Channel::PrepareEncodeAndSend()");
4409
andrew@webrtc.org63a50982012-05-02 23:56:37 +00004410 if (_audioFrame.samples_per_channel_ == 0)
niklase@google.com470e71d2011-07-07 08:21:25 +00004411 {
4412 WEBRTC_TRACE(kTraceWarning, kTraceVoice, VoEId(_instanceId,_channelId),
4413 "Channel::PrepareEncodeAndSend() invalid audio frame");
4414 return -1;
4415 }
4416
4417 if (_inputFilePlaying)
4418 {
4419 MixOrReplaceAudioWithFile(mixingFrequency);
4420 }
4421
4422 if (_mute)
4423 {
4424 AudioFrameOperations::Mute(_audioFrame);
4425 }
4426
4427 if (_inputExternalMedia)
4428 {
mflodman@webrtc.org9a065d12012-03-07 08:12:21 +00004429 CriticalSectionScoped cs(&_callbackCritSect);
andrew@webrtc.org63a50982012-05-02 23:56:37 +00004430 const bool isStereo = (_audioFrame.num_channels_ == 2);
niklase@google.com470e71d2011-07-07 08:21:25 +00004431 if (_inputExternalMediaCallbackPtr)
4432 {
4433 _inputExternalMediaCallbackPtr->Process(
4434 _channelId,
4435 kRecordingPerChannel,
pbos@webrtc.org6141e132013-04-09 10:09:10 +00004436 (int16_t*)_audioFrame.data_,
andrew@webrtc.org63a50982012-05-02 23:56:37 +00004437 _audioFrame.samples_per_channel_,
4438 _audioFrame.sample_rate_hz_,
niklase@google.com470e71d2011-07-07 08:21:25 +00004439 isStereo);
4440 }
4441 }
4442
4443 InsertInbandDtmfTone();
4444
andrew@webrtc.org755b04a2011-11-15 16:57:56 +00004445 if (_includeAudioLevelIndication)
4446 {
4447 assert(_rtpAudioProc.get() != NULL);
4448
4449 // Check if settings need to be updated.
andrew@webrtc.org63a50982012-05-02 23:56:37 +00004450 if (_rtpAudioProc->sample_rate_hz() != _audioFrame.sample_rate_hz_)
andrew@webrtc.org755b04a2011-11-15 16:57:56 +00004451 {
andrew@webrtc.org63a50982012-05-02 23:56:37 +00004452 if (_rtpAudioProc->set_sample_rate_hz(_audioFrame.sample_rate_hz_) !=
andrew@webrtc.org755b04a2011-11-15 16:57:56 +00004453 AudioProcessing::kNoError)
4454 {
4455 WEBRTC_TRACE(kTraceWarning, kTraceVoice,
4456 VoEId(_instanceId, _channelId),
4457 "Error setting AudioProcessing sample rate");
4458 return -1;
4459 }
4460 }
4461
andrew@webrtc.org63a50982012-05-02 23:56:37 +00004462 if (_rtpAudioProc->num_input_channels() != _audioFrame.num_channels_)
andrew@webrtc.org755b04a2011-11-15 16:57:56 +00004463 {
andrew@webrtc.org63a50982012-05-02 23:56:37 +00004464 if (_rtpAudioProc->set_num_channels(_audioFrame.num_channels_,
4465 _audioFrame.num_channels_)
andrew@webrtc.org755b04a2011-11-15 16:57:56 +00004466 != AudioProcessing::kNoError)
4467 {
4468 WEBRTC_TRACE(kTraceWarning, kTraceVoice,
4469 VoEId(_instanceId, _channelId),
4470 "Error setting AudioProcessing channels");
4471 return -1;
4472 }
4473 }
4474
4475 // Performs level analysis only; does not affect the signal.
4476 _rtpAudioProc->ProcessStream(&_audioFrame);
4477 }
4478
niklase@google.com470e71d2011-07-07 08:21:25 +00004479 return 0;
4480}
4481
pbos@webrtc.org6141e132013-04-09 10:09:10 +00004482uint32_t
niklase@google.com470e71d2011-07-07 08:21:25 +00004483Channel::EncodeAndSend()
4484{
4485 WEBRTC_TRACE(kTraceStream, kTraceVoice, VoEId(_instanceId,_channelId),
4486 "Channel::EncodeAndSend()");
4487
andrew@webrtc.org63a50982012-05-02 23:56:37 +00004488 assert(_audioFrame.num_channels_ <= 2);
4489 if (_audioFrame.samples_per_channel_ == 0)
niklase@google.com470e71d2011-07-07 08:21:25 +00004490 {
4491 WEBRTC_TRACE(kTraceWarning, kTraceVoice, VoEId(_instanceId,_channelId),
4492 "Channel::EncodeAndSend() invalid audio frame");
4493 return -1;
4494 }
4495
andrew@webrtc.org63a50982012-05-02 23:56:37 +00004496 _audioFrame.id_ = _channelId;
niklase@google.com470e71d2011-07-07 08:21:25 +00004497
4498 // --- Add 10ms of raw (PCM) audio data to the encoder @ 32kHz.
4499
4500 // The ACM resamples internally.
andrew@webrtc.org63a50982012-05-02 23:56:37 +00004501 _audioFrame.timestamp_ = _timeStamp;
niklase@google.com470e71d2011-07-07 08:21:25 +00004502 if (_audioCodingModule.Add10MsData((AudioFrame&)_audioFrame) != 0)
4503 {
4504 WEBRTC_TRACE(kTraceError, kTraceVoice, VoEId(_instanceId,_channelId),
4505 "Channel::EncodeAndSend() ACM encoding failed");
4506 return -1;
4507 }
4508
andrew@webrtc.org63a50982012-05-02 23:56:37 +00004509 _timeStamp += _audioFrame.samples_per_channel_;
niklase@google.com470e71d2011-07-07 08:21:25 +00004510
4511 // --- Encode if complete frame is ready
4512
4513 // This call will trigger AudioPacketizationCallback::SendData if encoding
4514 // is done and payload is ready for packetization and transmission.
4515 return _audioCodingModule.Process();
4516}
4517
4518int Channel::RegisterExternalMediaProcessing(
4519 ProcessingTypes type,
4520 VoEMediaProcess& processObject)
4521{
4522 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId,_channelId),
4523 "Channel::RegisterExternalMediaProcessing()");
4524
mflodman@webrtc.org9a065d12012-03-07 08:12:21 +00004525 CriticalSectionScoped cs(&_callbackCritSect);
niklase@google.com470e71d2011-07-07 08:21:25 +00004526
4527 if (kPlaybackPerChannel == type)
4528 {
4529 if (_outputExternalMediaCallbackPtr)
4530 {
4531 _engineStatisticsPtr->SetLastError(
4532 VE_INVALID_OPERATION, kTraceError,
4533 "Channel::RegisterExternalMediaProcessing() "
4534 "output external media already enabled");
4535 return -1;
4536 }
4537 _outputExternalMediaCallbackPtr = &processObject;
4538 _outputExternalMedia = true;
4539 }
4540 else if (kRecordingPerChannel == type)
4541 {
4542 if (_inputExternalMediaCallbackPtr)
4543 {
4544 _engineStatisticsPtr->SetLastError(
4545 VE_INVALID_OPERATION, kTraceError,
4546 "Channel::RegisterExternalMediaProcessing() "
4547 "output external media already enabled");
4548 return -1;
4549 }
4550 _inputExternalMediaCallbackPtr = &processObject;
4551 _inputExternalMedia = true;
4552 }
4553 return 0;
4554}
4555
4556int Channel::DeRegisterExternalMediaProcessing(ProcessingTypes type)
4557{
4558 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId,_channelId),
4559 "Channel::DeRegisterExternalMediaProcessing()");
4560
mflodman@webrtc.org9a065d12012-03-07 08:12:21 +00004561 CriticalSectionScoped cs(&_callbackCritSect);
niklase@google.com470e71d2011-07-07 08:21:25 +00004562
4563 if (kPlaybackPerChannel == type)
4564 {
4565 if (!_outputExternalMediaCallbackPtr)
4566 {
4567 _engineStatisticsPtr->SetLastError(
4568 VE_INVALID_OPERATION, kTraceWarning,
4569 "Channel::DeRegisterExternalMediaProcessing() "
4570 "output external media already disabled");
4571 return 0;
4572 }
4573 _outputExternalMedia = false;
4574 _outputExternalMediaCallbackPtr = NULL;
4575 }
4576 else if (kRecordingPerChannel == type)
4577 {
4578 if (!_inputExternalMediaCallbackPtr)
4579 {
4580 _engineStatisticsPtr->SetLastError(
4581 VE_INVALID_OPERATION, kTraceWarning,
4582 "Channel::DeRegisterExternalMediaProcessing() "
4583 "input external media already disabled");
4584 return 0;
4585 }
4586 _inputExternalMedia = false;
4587 _inputExternalMediaCallbackPtr = NULL;
4588 }
4589
4590 return 0;
4591}
4592
roosa@google.com1b60ceb2012-12-12 23:00:29 +00004593int Channel::SetExternalMixing(bool enabled) {
4594 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId,_channelId),
4595 "Channel::SetExternalMixing(enabled=%d)", enabled);
4596
4597 if (_playing)
4598 {
4599 _engineStatisticsPtr->SetLastError(
4600 VE_INVALID_OPERATION, kTraceError,
4601 "Channel::SetExternalMixing() "
4602 "external mixing cannot be changed while playing.");
4603 return -1;
4604 }
4605
4606 _externalMixing = enabled;
4607
4608 return 0;
4609}
4610
niklase@google.com470e71d2011-07-07 08:21:25 +00004611int
4612Channel::ResetRTCPStatistics()
4613{
4614 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId,_channelId),
4615 "Channel::ResetRTCPStatistics()");
pbos@webrtc.org6141e132013-04-09 10:09:10 +00004616 uint32_t remoteSSRC(0);
pwestin@webrtc.org2853dde2012-05-11 11:08:54 +00004617 remoteSSRC = _rtpRtcpModule->RemoteSSRC();
4618 return _rtpRtcpModule->ResetRTT(remoteSSRC);
niklase@google.com470e71d2011-07-07 08:21:25 +00004619}
4620
4621int
4622Channel::GetRoundTripTimeSummary(StatVal& delaysMs) const
4623{
4624 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId,_channelId),
4625 "Channel::GetRoundTripTimeSummary()");
4626 // Override default module outputs for the case when RTCP is disabled.
4627 // This is done to ensure that we are backward compatible with the
4628 // VoiceEngine where we did not use RTP/RTCP module.
pwestin@webrtc.org2853dde2012-05-11 11:08:54 +00004629 if (!_rtpRtcpModule->RTCP())
niklase@google.com470e71d2011-07-07 08:21:25 +00004630 {
4631 delaysMs.min = -1;
4632 delaysMs.max = -1;
4633 delaysMs.average = -1;
4634 WEBRTC_TRACE(kTraceWarning, kTraceVoice, VoEId(_instanceId,_channelId),
4635 "Channel::GetRoundTripTimeSummary() RTCP is disabled =>"
4636 " valid RTT measurements cannot be retrieved");
4637 return 0;
4638 }
4639
pbos@webrtc.org6141e132013-04-09 10:09:10 +00004640 uint32_t remoteSSRC;
4641 uint16_t RTT;
4642 uint16_t avgRTT;
4643 uint16_t maxRTT;
4644 uint16_t minRTT;
niklase@google.com470e71d2011-07-07 08:21:25 +00004645 // The remote SSRC will be zero if no RTP packet has been received.
pwestin@webrtc.org2853dde2012-05-11 11:08:54 +00004646 remoteSSRC = _rtpRtcpModule->RemoteSSRC();
niklase@google.com470e71d2011-07-07 08:21:25 +00004647 if (remoteSSRC == 0)
4648 {
4649 WEBRTC_TRACE(kTraceWarning, kTraceVoice, VoEId(_instanceId,_channelId),
4650 "Channel::GetRoundTripTimeSummary() unable to measure RTT"
4651 " since no RTP packet has been received yet");
4652 }
4653
4654 // Retrieve RTT statistics from the RTP/RTCP module for the specified
4655 // channel and SSRC. The SSRC is required to parse out the correct source
4656 // in conference scenarios.
pwestin@webrtc.org2853dde2012-05-11 11:08:54 +00004657 if (_rtpRtcpModule->RTT(remoteSSRC, &RTT, &avgRTT, &minRTT,&maxRTT) != 0)
niklase@google.com470e71d2011-07-07 08:21:25 +00004658 {
4659 WEBRTC_TRACE(kTraceWarning, kTraceVoice, VoEId(_instanceId,_channelId),
4660 "GetRoundTripTimeSummary unable to retrieve RTT values"
4661 " from the RTCP layer");
4662 delaysMs.min = -1; delaysMs.max = -1; delaysMs.average = -1;
4663 }
4664 else
4665 {
4666 delaysMs.min = minRTT;
4667 delaysMs.max = maxRTT;
4668 delaysMs.average = avgRTT;
4669 }
4670 return 0;
4671}
4672
4673int
4674Channel::GetNetworkStatistics(NetworkStatistics& stats)
4675{
4676 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId,_channelId),
4677 "Channel::GetNetworkStatistics()");
tina.legrand@webrtc.org7a7a0082013-02-21 10:27:48 +00004678 ACMNetworkStatistics acm_stats;
4679 int return_value = _audioCodingModule.NetworkStatistics(&acm_stats);
4680 if (return_value >= 0) {
4681 memcpy(&stats, &acm_stats, sizeof(NetworkStatistics));
4682 }
4683 return return_value;
niklase@google.com470e71d2011-07-07 08:21:25 +00004684}
4685
pwestin@webrtc.org1de01352013-04-11 20:23:35 +00004686bool Channel::GetDelayEstimate(int* jitter_buffer_delay_ms,
4687 int* playout_buffer_delay_ms) const {
4688 if (_average_jitter_buffer_delay_us == 0) {
niklase@google.com470e71d2011-07-07 08:21:25 +00004689 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId,_channelId),
pwestin@webrtc.org1de01352013-04-11 20:23:35 +00004690 "Channel::GetDelayEstimate() no valid estimate.");
4691 return false;
4692 }
4693 *jitter_buffer_delay_ms = (_average_jitter_buffer_delay_us + 500) / 1000 +
4694 _recPacketDelayMs;
4695 *playout_buffer_delay_ms = playout_delay_ms_;
4696 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId,_channelId),
4697 "Channel::GetDelayEstimate()");
4698 return true;
niklase@google.com470e71d2011-07-07 08:21:25 +00004699}
4700
turaj@webrtc.org6388c3e2013-02-12 21:42:18 +00004701int Channel::SetInitialPlayoutDelay(int delay_ms)
4702{
4703 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId,_channelId),
4704 "Channel::SetInitialPlayoutDelay()");
4705 if ((delay_ms < kVoiceEngineMinMinPlayoutDelayMs) ||
4706 (delay_ms > kVoiceEngineMaxMinPlayoutDelayMs))
4707 {
4708 _engineStatisticsPtr->SetLastError(
4709 VE_INVALID_ARGUMENT, kTraceError,
4710 "SetInitialPlayoutDelay() invalid min delay");
4711 return -1;
4712 }
4713 if (_audioCodingModule.SetInitialPlayoutDelay(delay_ms) != 0)
4714 {
4715 _engineStatisticsPtr->SetLastError(
4716 VE_AUDIO_CODING_MODULE_ERROR, kTraceError,
4717 "SetInitialPlayoutDelay() failed to set min playout delay");
4718 return -1;
4719 }
4720 return 0;
4721}
4722
4723
niklase@google.com470e71d2011-07-07 08:21:25 +00004724int
4725Channel::SetMinimumPlayoutDelay(int delayMs)
4726{
4727 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId,_channelId),
4728 "Channel::SetMinimumPlayoutDelay()");
4729 if ((delayMs < kVoiceEngineMinMinPlayoutDelayMs) ||
4730 (delayMs > kVoiceEngineMaxMinPlayoutDelayMs))
4731 {
4732 _engineStatisticsPtr->SetLastError(
4733 VE_INVALID_ARGUMENT, kTraceError,
4734 "SetMinimumPlayoutDelay() invalid min delay");
4735 return -1;
4736 }
4737 if (_audioCodingModule.SetMinimumPlayoutDelay(delayMs) != 0)
4738 {
4739 _engineStatisticsPtr->SetLastError(
4740 VE_AUDIO_CODING_MODULE_ERROR, kTraceError,
4741 "SetMinimumPlayoutDelay() failed to set min playout delay");
4742 return -1;
4743 }
4744 return 0;
4745}
4746
pwestin@webrtc.org1de01352013-04-11 20:23:35 +00004747void Channel::UpdatePlayoutTimestamp(bool rtcp) {
4748 uint32_t playout_timestamp = 0;
4749
4750 if (_audioCodingModule.PlayoutTimestamp(&playout_timestamp) == -1) {
4751 WEBRTC_TRACE(kTraceWarning, kTraceVoice, VoEId(_instanceId,_channelId),
4752 "Channel::UpdatePlayoutTimestamp() failed to read playout"
4753 " timestamp from the ACM");
4754 _engineStatisticsPtr->SetLastError(
4755 VE_CANNOT_RETRIEVE_VALUE, kTraceError,
4756 "UpdatePlayoutTimestamp() failed to retrieve timestamp");
4757 return;
4758 }
4759
4760 uint16_t delay_ms = 0;
4761 if (_audioDeviceModulePtr->PlayoutDelay(&delay_ms) == -1) {
4762 WEBRTC_TRACE(kTraceWarning, kTraceVoice, VoEId(_instanceId,_channelId),
4763 "Channel::UpdatePlayoutTimestamp() failed to read playout"
4764 " delay from the ADM");
4765 _engineStatisticsPtr->SetLastError(
4766 VE_CANNOT_RETRIEVE_VALUE, kTraceError,
4767 "UpdatePlayoutTimestamp() failed to retrieve playout delay");
4768 return;
4769 }
4770
4771 int32_t playout_frequency = _audioCodingModule.PlayoutFrequency();
4772 CodecInst current_recive_codec;
4773 if (_audioCodingModule.ReceiveCodec(&current_recive_codec) == 0) {
4774 if (STR_CASE_CMP("G722", current_recive_codec.plname) == 0) {
4775 playout_frequency = 8000;
4776 } else if (STR_CASE_CMP("opus", current_recive_codec.plname) == 0) {
4777 playout_frequency = 48000;
niklase@google.com470e71d2011-07-07 08:21:25 +00004778 }
pwestin@webrtc.org1de01352013-04-11 20:23:35 +00004779 }
4780
4781 // Remove the playout delay.
4782 playout_timestamp -= (delay_ms * (playout_frequency / 1000));
4783
4784 WEBRTC_TRACE(kTraceStream, kTraceVoice, VoEId(_instanceId,_channelId),
4785 "Channel::UpdatePlayoutTimestamp() => playoutTimestamp = %lu",
4786 playout_timestamp);
4787
4788 if (rtcp) {
4789 playout_timestamp_rtcp_ = playout_timestamp;
4790 } else {
4791 playout_timestamp_rtp_ = playout_timestamp;
4792 }
4793 playout_delay_ms_ = delay_ms;
4794}
4795
4796int Channel::GetPlayoutTimestamp(unsigned int& timestamp) {
4797 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId,_channelId),
4798 "Channel::GetPlayoutTimestamp()");
4799 if (playout_timestamp_rtp_ == 0) {
4800 _engineStatisticsPtr->SetLastError(
4801 VE_CANNOT_RETRIEVE_VALUE, kTraceError,
4802 "GetPlayoutTimestamp() failed to retrieve timestamp");
4803 return -1;
4804 }
4805 timestamp = playout_timestamp_rtp_;
4806 WEBRTC_TRACE(kTraceStateInfo, kTraceVoice,
4807 VoEId(_instanceId,_channelId),
4808 "GetPlayoutTimestamp() => timestamp=%u", timestamp);
4809 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +00004810}
4811
4812int
4813Channel::SetInitTimestamp(unsigned int timestamp)
4814{
4815 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId,_channelId),
4816 "Channel::SetInitTimestamp()");
4817 if (_sending)
4818 {
4819 _engineStatisticsPtr->SetLastError(
4820 VE_SENDING, kTraceError, "SetInitTimestamp() already sending");
4821 return -1;
4822 }
pwestin@webrtc.org2853dde2012-05-11 11:08:54 +00004823 if (_rtpRtcpModule->SetStartTimestamp(timestamp) != 0)
niklase@google.com470e71d2011-07-07 08:21:25 +00004824 {
4825 _engineStatisticsPtr->SetLastError(
4826 VE_RTP_RTCP_MODULE_ERROR, kTraceError,
4827 "SetInitTimestamp() failed to set timestamp");
4828 return -1;
4829 }
4830 return 0;
4831}
4832
4833int
4834Channel::SetInitSequenceNumber(short sequenceNumber)
4835{
4836 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId,_channelId),
4837 "Channel::SetInitSequenceNumber()");
4838 if (_sending)
4839 {
4840 _engineStatisticsPtr->SetLastError(
4841 VE_SENDING, kTraceError,
4842 "SetInitSequenceNumber() already sending");
4843 return -1;
4844 }
pwestin@webrtc.org2853dde2012-05-11 11:08:54 +00004845 if (_rtpRtcpModule->SetSequenceNumber(sequenceNumber) != 0)
niklase@google.com470e71d2011-07-07 08:21:25 +00004846 {
4847 _engineStatisticsPtr->SetLastError(
4848 VE_RTP_RTCP_MODULE_ERROR, kTraceError,
4849 "SetInitSequenceNumber() failed to set sequence number");
4850 return -1;
4851 }
4852 return 0;
4853}
4854
4855int
4856Channel::GetRtpRtcp(RtpRtcp* &rtpRtcpModule) const
4857{
4858 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId,_channelId),
4859 "Channel::GetRtpRtcp()");
pwestin@webrtc.org2853dde2012-05-11 11:08:54 +00004860 rtpRtcpModule = _rtpRtcpModule.get();
niklase@google.com470e71d2011-07-07 08:21:25 +00004861 return 0;
4862}
4863
andrew@webrtc.orge59a0ac2012-05-08 17:12:40 +00004864// TODO(andrew): refactor Mix functions here and in transmit_mixer.cc to use
4865// a shared helper.
pbos@webrtc.org6141e132013-04-09 10:09:10 +00004866int32_t
pbos@webrtc.org92135212013-05-14 08:31:39 +00004867Channel::MixOrReplaceAudioWithFile(int mixingFrequency)
niklase@google.com470e71d2011-07-07 08:21:25 +00004868{
pbos@webrtc.org6141e132013-04-09 10:09:10 +00004869 scoped_array<int16_t> fileBuffer(new int16_t[640]);
andrew@webrtc.orge59a0ac2012-05-08 17:12:40 +00004870 int fileSamples(0);
niklase@google.com470e71d2011-07-07 08:21:25 +00004871
4872 {
mflodman@webrtc.org9a065d12012-03-07 08:12:21 +00004873 CriticalSectionScoped cs(&_fileCritSect);
niklase@google.com470e71d2011-07-07 08:21:25 +00004874
4875 if (_inputFilePlayerPtr == NULL)
4876 {
4877 WEBRTC_TRACE(kTraceWarning, kTraceVoice,
4878 VoEId(_instanceId, _channelId),
4879 "Channel::MixOrReplaceAudioWithFile() fileplayer"
4880 " doesnt exist");
4881 return -1;
4882 }
4883
braveyao@webrtc.orgd7131432012-03-29 10:39:44 +00004884 if (_inputFilePlayerPtr->Get10msAudioFromFile(fileBuffer.get(),
niklase@google.com470e71d2011-07-07 08:21:25 +00004885 fileSamples,
4886 mixingFrequency) == -1)
4887 {
4888 WEBRTC_TRACE(kTraceWarning, kTraceVoice,
4889 VoEId(_instanceId, _channelId),
4890 "Channel::MixOrReplaceAudioWithFile() file mixing "
4891 "failed");
4892 return -1;
4893 }
4894 if (fileSamples == 0)
4895 {
4896 WEBRTC_TRACE(kTraceWarning, kTraceVoice,
4897 VoEId(_instanceId, _channelId),
4898 "Channel::MixOrReplaceAudioWithFile() file is ended");
4899 return 0;
4900 }
4901 }
4902
andrew@webrtc.org63a50982012-05-02 23:56:37 +00004903 assert(_audioFrame.samples_per_channel_ == fileSamples);
niklase@google.com470e71d2011-07-07 08:21:25 +00004904
4905 if (_mixFileWithMicrophone)
4906 {
braveyao@webrtc.orgd7131432012-03-29 10:39:44 +00004907 // Currently file stream is always mono.
4908 // TODO(xians): Change the code when FilePlayer supports real stereo.
andrew@webrtc.org63a50982012-05-02 23:56:37 +00004909 Utility::MixWithSat(_audioFrame.data_,
andrew@webrtc.orge59a0ac2012-05-08 17:12:40 +00004910 _audioFrame.num_channels_,
braveyao@webrtc.orgd7131432012-03-29 10:39:44 +00004911 fileBuffer.get(),
4912 1,
andrew@webrtc.orge59a0ac2012-05-08 17:12:40 +00004913 fileSamples);
niklase@google.com470e71d2011-07-07 08:21:25 +00004914 }
4915 else
4916 {
braveyao@webrtc.orgd7131432012-03-29 10:39:44 +00004917 // Replace ACM audio with file.
4918 // Currently file stream is always mono.
4919 // TODO(xians): Change the code when FilePlayer supports real stereo.
niklase@google.com470e71d2011-07-07 08:21:25 +00004920 _audioFrame.UpdateFrame(_channelId,
4921 -1,
braveyao@webrtc.orgd7131432012-03-29 10:39:44 +00004922 fileBuffer.get(),
andrew@webrtc.orge59a0ac2012-05-08 17:12:40 +00004923 fileSamples,
niklase@google.com470e71d2011-07-07 08:21:25 +00004924 mixingFrequency,
4925 AudioFrame::kNormalSpeech,
4926 AudioFrame::kVadUnknown,
4927 1);
4928
4929 }
4930 return 0;
4931}
4932
pbos@webrtc.org6141e132013-04-09 10:09:10 +00004933int32_t
niklase@google.com470e71d2011-07-07 08:21:25 +00004934Channel::MixAudioWithFile(AudioFrame& audioFrame,
pbos@webrtc.org92135212013-05-14 08:31:39 +00004935 int mixingFrequency)
niklase@google.com470e71d2011-07-07 08:21:25 +00004936{
4937 assert(mixingFrequency <= 32000);
4938
pbos@webrtc.org6141e132013-04-09 10:09:10 +00004939 scoped_array<int16_t> fileBuffer(new int16_t[640]);
andrew@webrtc.orge59a0ac2012-05-08 17:12:40 +00004940 int fileSamples(0);
niklase@google.com470e71d2011-07-07 08:21:25 +00004941
4942 {
mflodman@webrtc.org9a065d12012-03-07 08:12:21 +00004943 CriticalSectionScoped cs(&_fileCritSect);
niklase@google.com470e71d2011-07-07 08:21:25 +00004944
4945 if (_outputFilePlayerPtr == NULL)
4946 {
4947 WEBRTC_TRACE(kTraceWarning, kTraceVoice,
4948 VoEId(_instanceId, _channelId),
4949 "Channel::MixAudioWithFile() file mixing failed");
4950 return -1;
4951 }
4952
4953 // We should get the frequency we ask for.
braveyao@webrtc.orgd7131432012-03-29 10:39:44 +00004954 if (_outputFilePlayerPtr->Get10msAudioFromFile(fileBuffer.get(),
niklase@google.com470e71d2011-07-07 08:21:25 +00004955 fileSamples,
4956 mixingFrequency) == -1)
4957 {
4958 WEBRTC_TRACE(kTraceWarning, kTraceVoice,
4959 VoEId(_instanceId, _channelId),
4960 "Channel::MixAudioWithFile() file mixing failed");
4961 return -1;
4962 }
4963 }
4964
andrew@webrtc.org63a50982012-05-02 23:56:37 +00004965 if (audioFrame.samples_per_channel_ == fileSamples)
niklase@google.com470e71d2011-07-07 08:21:25 +00004966 {
braveyao@webrtc.orgd7131432012-03-29 10:39:44 +00004967 // Currently file stream is always mono.
4968 // TODO(xians): Change the code when FilePlayer supports real stereo.
andrew@webrtc.org63a50982012-05-02 23:56:37 +00004969 Utility::MixWithSat(audioFrame.data_,
andrew@webrtc.orge59a0ac2012-05-08 17:12:40 +00004970 audioFrame.num_channels_,
braveyao@webrtc.orgd7131432012-03-29 10:39:44 +00004971 fileBuffer.get(),
4972 1,
andrew@webrtc.orge59a0ac2012-05-08 17:12:40 +00004973 fileSamples);
niklase@google.com470e71d2011-07-07 08:21:25 +00004974 }
4975 else
4976 {
4977 WEBRTC_TRACE(kTraceWarning, kTraceVoice, VoEId(_instanceId,_channelId),
andrew@webrtc.org63a50982012-05-02 23:56:37 +00004978 "Channel::MixAudioWithFile() samples_per_channel_(%d) != "
niklase@google.com470e71d2011-07-07 08:21:25 +00004979 "fileSamples(%d)",
andrew@webrtc.org63a50982012-05-02 23:56:37 +00004980 audioFrame.samples_per_channel_, fileSamples);
niklase@google.com470e71d2011-07-07 08:21:25 +00004981 return -1;
4982 }
4983
4984 return 0;
4985}
4986
4987int
4988Channel::InsertInbandDtmfTone()
4989{
niklas.enbom@webrtc.orgaf26f642011-11-16 12:41:36 +00004990 // Check if we should start a new tone.
niklase@google.com470e71d2011-07-07 08:21:25 +00004991 if (_inbandDtmfQueue.PendingDtmf() &&
4992 !_inbandDtmfGenerator.IsAddingTone() &&
4993 _inbandDtmfGenerator.DelaySinceLastTone() >
4994 kMinTelephoneEventSeparationMs)
4995 {
pbos@webrtc.org6141e132013-04-09 10:09:10 +00004996 int8_t eventCode(0);
4997 uint16_t lengthMs(0);
4998 uint8_t attenuationDb(0);
niklase@google.com470e71d2011-07-07 08:21:25 +00004999
5000 eventCode = _inbandDtmfQueue.NextDtmf(&lengthMs, &attenuationDb);
5001 _inbandDtmfGenerator.AddTone(eventCode, lengthMs, attenuationDb);
5002 if (_playInbandDtmfEvent)
5003 {
5004 // Add tone to output mixer using a reduced length to minimize
5005 // risk of echo.
5006 _outputMixerPtr->PlayDtmfTone(eventCode, lengthMs - 80,
5007 attenuationDb);
5008 }
5009 }
5010
5011 if (_inbandDtmfGenerator.IsAddingTone())
5012 {
pbos@webrtc.org6141e132013-04-09 10:09:10 +00005013 uint16_t frequency(0);
niklase@google.com470e71d2011-07-07 08:21:25 +00005014 _inbandDtmfGenerator.GetSampleRate(frequency);
5015
andrew@webrtc.org63a50982012-05-02 23:56:37 +00005016 if (frequency != _audioFrame.sample_rate_hz_)
niklase@google.com470e71d2011-07-07 08:21:25 +00005017 {
5018 // Update sample rate of Dtmf tone since the mixing frequency
5019 // has changed.
5020 _inbandDtmfGenerator.SetSampleRate(
pbos@webrtc.org6141e132013-04-09 10:09:10 +00005021 (uint16_t) (_audioFrame.sample_rate_hz_));
niklase@google.com470e71d2011-07-07 08:21:25 +00005022 // Reset the tone to be added taking the new sample rate into
5023 // account.
5024 _inbandDtmfGenerator.ResetTone();
5025 }
andrew@webrtc.orgae1a58b2013-01-22 04:44:30 +00005026
pbos@webrtc.org6141e132013-04-09 10:09:10 +00005027 int16_t toneBuffer[320];
5028 uint16_t toneSamples(0);
niklase@google.com470e71d2011-07-07 08:21:25 +00005029 // Get 10ms tone segment and set time since last tone to zero
5030 if (_inbandDtmfGenerator.Get10msTone(toneBuffer, toneSamples) == -1)
5031 {
5032 WEBRTC_TRACE(kTraceWarning, kTraceVoice,
5033 VoEId(_instanceId, _channelId),
5034 "Channel::EncodeAndSend() inserting Dtmf failed");
5035 return -1;
5036 }
5037
niklas.enbom@webrtc.orgaf26f642011-11-16 12:41:36 +00005038 // Replace mixed audio with DTMF tone.
andrew@webrtc.orgae1a58b2013-01-22 04:44:30 +00005039 for (int sample = 0;
andrew@webrtc.org63a50982012-05-02 23:56:37 +00005040 sample < _audioFrame.samples_per_channel_;
niklas.enbom@webrtc.orgaf26f642011-11-16 12:41:36 +00005041 sample++)
5042 {
andrew@webrtc.orgae1a58b2013-01-22 04:44:30 +00005043 for (int channel = 0;
5044 channel < _audioFrame.num_channels_;
niklas.enbom@webrtc.orgaf26f642011-11-16 12:41:36 +00005045 channel++)
5046 {
andrew@webrtc.orgae1a58b2013-01-22 04:44:30 +00005047 const int index = sample * _audioFrame.num_channels_ + channel;
5048 _audioFrame.data_[index] = toneBuffer[sample];
niklas.enbom@webrtc.orgaf26f642011-11-16 12:41:36 +00005049 }
5050 }
andrew@webrtc.orgae1a58b2013-01-22 04:44:30 +00005051
andrew@webrtc.org63a50982012-05-02 23:56:37 +00005052 assert(_audioFrame.samples_per_channel_ == toneSamples);
niklase@google.com470e71d2011-07-07 08:21:25 +00005053 } else
5054 {
5055 // Add 10ms to "delay-since-last-tone" counter
5056 _inbandDtmfGenerator.UpdateDelaySinceLastTone();
5057 }
5058 return 0;
5059}
5060
niklase@google.com470e71d2011-07-07 08:21:25 +00005061void
5062Channel::ResetDeadOrAliveCounters()
5063{
5064 _countDeadDetections = 0;
5065 _countAliveDetections = 0;
5066}
5067
5068void
5069Channel::UpdateDeadOrAliveCounters(bool alive)
5070{
5071 if (alive)
5072 _countAliveDetections++;
5073 else
5074 _countDeadDetections++;
5075}
5076
5077int
5078Channel::GetDeadOrAliveCounters(int& countDead, int& countAlive) const
5079{
5080 bool enabled;
pbos@webrtc.org6141e132013-04-09 10:09:10 +00005081 uint8_t timeSec;
niklase@google.com470e71d2011-07-07 08:21:25 +00005082
pwestin@webrtc.org2853dde2012-05-11 11:08:54 +00005083 _rtpRtcpModule->PeriodicDeadOrAliveStatus(enabled, timeSec);
niklase@google.com470e71d2011-07-07 08:21:25 +00005084 if (!enabled)
5085 return (-1);
5086
5087 countDead = static_cast<int> (_countDeadDetections);
5088 countAlive = static_cast<int> (_countAliveDetections);
5089 return 0;
5090}
5091
pbos@webrtc.org6141e132013-04-09 10:09:10 +00005092int32_t
niklase@google.com470e71d2011-07-07 08:21:25 +00005093Channel::SendPacketRaw(const void *data, int len, bool RTCP)
5094{
5095 if (_transportPtr == NULL)
5096 {
5097 return -1;
5098 }
5099 if (!RTCP)
5100 {
5101 return _transportPtr->SendPacket(_channelId, data, len);
5102 }
5103 else
5104 {
5105 return _transportPtr->SendRTCPPacket(_channelId, data, len);
5106 }
5107}
5108
pwestin@webrtc.org1de01352013-04-11 20:23:35 +00005109// Called for incoming RTP packets after successful RTP header parsing.
5110void Channel::UpdatePacketDelay(uint32_t rtp_timestamp,
5111 uint16_t sequence_number) {
5112 WEBRTC_TRACE(kTraceStream, kTraceVoice, VoEId(_instanceId,_channelId),
5113 "Channel::UpdatePacketDelay(timestamp=%lu, sequenceNumber=%u)",
5114 rtp_timestamp, sequence_number);
niklase@google.com470e71d2011-07-07 08:21:25 +00005115
pwestin@webrtc.org1de01352013-04-11 20:23:35 +00005116 // Get frequency of last received payload
5117 int rtp_receive_frequency = _audioCodingModule.ReceiveFrequency();
niklase@google.com470e71d2011-07-07 08:21:25 +00005118
pwestin@webrtc.org1de01352013-04-11 20:23:35 +00005119 CodecInst current_receive_codec;
5120 if (_audioCodingModule.ReceiveCodec(&current_receive_codec) != 0) {
5121 return;
5122 }
niklase@google.com470e71d2011-07-07 08:21:25 +00005123
turaj@webrtc.orge46c8d32013-05-22 20:39:43 +00005124 // Update the least required delay.
5125 least_required_delay_ms_ = _audioCodingModule.LeastRequiredDelayMs();
5126
pwestin@webrtc.org1de01352013-04-11 20:23:35 +00005127 if (STR_CASE_CMP("G722", current_receive_codec.plname) == 0) {
5128 // Even though the actual sampling rate for G.722 audio is
5129 // 16,000 Hz, the RTP clock rate for the G722 payload format is
5130 // 8,000 Hz because that value was erroneously assigned in
5131 // RFC 1890 and must remain unchanged for backward compatibility.
5132 rtp_receive_frequency = 8000;
5133 } else if (STR_CASE_CMP("opus", current_receive_codec.plname) == 0) {
5134 // We are resampling Opus internally to 32,000 Hz until all our
5135 // DSP routines can operate at 48,000 Hz, but the RTP clock
5136 // rate for the Opus payload format is standardized to 48,000 Hz,
5137 // because that is the maximum supported decoding sampling rate.
5138 rtp_receive_frequency = 48000;
5139 }
niklase@google.com470e71d2011-07-07 08:21:25 +00005140
pwestin@webrtc.org1de01352013-04-11 20:23:35 +00005141 // playout_timestamp_rtp_ updated in UpdatePlayoutTimestamp for every incoming
5142 // packet.
5143 uint32_t timestamp_diff_ms = (rtp_timestamp - playout_timestamp_rtp_) /
5144 (rtp_receive_frequency / 1000);
niklase@google.com470e71d2011-07-07 08:21:25 +00005145
pwestin@webrtc.org1de01352013-04-11 20:23:35 +00005146 uint16_t packet_delay_ms = (rtp_timestamp - _previousTimestamp) /
5147 (rtp_receive_frequency / 1000);
niklase@google.com470e71d2011-07-07 08:21:25 +00005148
pwestin@webrtc.org1de01352013-04-11 20:23:35 +00005149 _previousTimestamp = rtp_timestamp;
niklase@google.com470e71d2011-07-07 08:21:25 +00005150
pwestin@webrtc.org1de01352013-04-11 20:23:35 +00005151 if (timestamp_diff_ms > (2 * kVoiceEngineMaxMinPlayoutDelayMs)) {
5152 timestamp_diff_ms = 0;
5153 }
niklase@google.com470e71d2011-07-07 08:21:25 +00005154
pwestin@webrtc.org1de01352013-04-11 20:23:35 +00005155 if (timestamp_diff_ms == 0) return;
niklase@google.com470e71d2011-07-07 08:21:25 +00005156
pwestin@webrtc.org1de01352013-04-11 20:23:35 +00005157 if (packet_delay_ms >= 10 && packet_delay_ms <= 60) {
5158 _recPacketDelayMs = packet_delay_ms;
5159 }
niklase@google.com470e71d2011-07-07 08:21:25 +00005160
pwestin@webrtc.org1de01352013-04-11 20:23:35 +00005161 if (_average_jitter_buffer_delay_us == 0) {
5162 _average_jitter_buffer_delay_us = timestamp_diff_ms * 1000;
5163 return;
5164 }
5165
5166 // Filter average delay value using exponential filter (alpha is
5167 // 7/8). We derive 1000 *_average_jitter_buffer_delay_us here (reduces
5168 // risk of rounding error) and compensate for it in GetDelayEstimate()
5169 // later.
5170 _average_jitter_buffer_delay_us = (_average_jitter_buffer_delay_us * 7 +
5171 1000 * timestamp_diff_ms + 500) / 8;
niklase@google.com470e71d2011-07-07 08:21:25 +00005172}
5173
5174void
5175Channel::RegisterReceiveCodecsToRTPModule()
5176{
5177 WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId,_channelId),
5178 "Channel::RegisterReceiveCodecsToRTPModule()");
5179
5180
5181 CodecInst codec;
pbos@webrtc.org6141e132013-04-09 10:09:10 +00005182 const uint8_t nSupportedCodecs = AudioCodingModule::NumberOfCodecs();
niklase@google.com470e71d2011-07-07 08:21:25 +00005183
5184 for (int idx = 0; idx < nSupportedCodecs; idx++)
5185 {
5186 // Open up the RTP/RTCP receiver for all supported codecs
tina.legrand@webrtc.org7a7a0082013-02-21 10:27:48 +00005187 if ((_audioCodingModule.Codec(idx, &codec) == -1) ||
pwestin@webrtc.org2853dde2012-05-11 11:08:54 +00005188 (_rtpRtcpModule->RegisterReceivePayload(codec) == -1))
niklase@google.com470e71d2011-07-07 08:21:25 +00005189 {
5190 WEBRTC_TRACE(
5191 kTraceWarning,
5192 kTraceVoice,
5193 VoEId(_instanceId, _channelId),
5194 "Channel::RegisterReceiveCodecsToRTPModule() unable"
5195 " to register %s (%d/%d/%d/%d) to RTP/RTCP receiver",
5196 codec.plname, codec.pltype, codec.plfreq,
5197 codec.channels, codec.rate);
5198 }
5199 else
5200 {
5201 WEBRTC_TRACE(
5202 kTraceInfo,
5203 kTraceVoice,
5204 VoEId(_instanceId, _channelId),
5205 "Channel::RegisterReceiveCodecsToRTPModule() %s "
wu@webrtc.orgfcd12b32011-09-15 20:49:50 +00005206 "(%d/%d/%d/%d) has been added to the RTP/RTCP "
niklase@google.com470e71d2011-07-07 08:21:25 +00005207 "receiver",
5208 codec.plname, codec.pltype, codec.plfreq,
5209 codec.channels, codec.rate);
5210 }
5211 }
5212}
5213
andrew@webrtc.org50419b02012-11-14 19:07:54 +00005214int Channel::ApmProcessRx(AudioFrame& frame) {
5215 AudioProcessing* audioproc = _rxAudioProcessingModulePtr;
5216 // Register the (possibly new) frame parameters.
5217 if (audioproc->set_sample_rate_hz(frame.sample_rate_hz_) != 0) {
andrew@webrtc.org655d8f52012-11-20 07:34:45 +00005218 LOG_FERR1(LS_WARNING, set_sample_rate_hz, frame.sample_rate_hz_);
andrew@webrtc.org50419b02012-11-14 19:07:54 +00005219 }
5220 if (audioproc->set_num_channels(frame.num_channels_,
5221 frame.num_channels_) != 0) {
andrew@webrtc.org655d8f52012-11-20 07:34:45 +00005222 LOG_FERR1(LS_WARNING, set_num_channels, frame.num_channels_);
andrew@webrtc.org50419b02012-11-14 19:07:54 +00005223 }
5224 if (audioproc->ProcessStream(&frame) != 0) {
andrew@webrtc.org655d8f52012-11-20 07:34:45 +00005225 LOG_FERR0(LS_WARNING, ProcessStream);
andrew@webrtc.org50419b02012-11-14 19:07:54 +00005226 }
5227 return 0;
niklase@google.com470e71d2011-07-07 08:21:25 +00005228}
5229
turaj@webrtc.org42259e72012-12-11 02:15:12 +00005230int Channel::SetSecondarySendCodec(const CodecInst& codec,
5231 int red_payload_type) {
turaj@webrtc.org8c8ad852013-01-31 18:20:17 +00005232 // Sanity check for payload type.
5233 if (red_payload_type < 0 || red_payload_type > 127) {
5234 _engineStatisticsPtr->SetLastError(
5235 VE_PLTYPE_ERROR, kTraceError,
5236 "SetRedPayloadType() invalid RED payload type");
5237 return -1;
5238 }
5239
turaj@webrtc.org42259e72012-12-11 02:15:12 +00005240 if (SetRedPayloadType(red_payload_type) < 0) {
5241 _engineStatisticsPtr->SetLastError(
5242 VE_AUDIO_CODING_MODULE_ERROR, kTraceError,
5243 "SetSecondarySendCodec() Failed to register RED ACM");
5244 return -1;
5245 }
5246 if (_audioCodingModule.RegisterSecondarySendCodec(codec) < 0) {
5247 _engineStatisticsPtr->SetLastError(
5248 VE_AUDIO_CODING_MODULE_ERROR, kTraceError,
5249 "SetSecondarySendCodec() Failed to register secondary send codec in "
5250 "ACM");
5251 return -1;
5252 }
5253
5254 return 0;
5255}
5256
5257void Channel::RemoveSecondarySendCodec() {
5258 _audioCodingModule.UnregisterSecondarySendCodec();
5259}
5260
5261int Channel::GetSecondarySendCodec(CodecInst* codec) {
5262 if (_audioCodingModule.SecondarySendCodec(codec) < 0) {
5263 _engineStatisticsPtr->SetLastError(
5264 VE_AUDIO_CODING_MODULE_ERROR, kTraceError,
5265 "GetSecondarySendCodec() Failed to get secondary sent codec from ACM");
5266 return -1;
5267 }
5268 return 0;
5269}
5270
turaj@webrtc.org8c8ad852013-01-31 18:20:17 +00005271// Assuming this method is called with valid payload type.
turaj@webrtc.org42259e72012-12-11 02:15:12 +00005272int Channel::SetRedPayloadType(int red_payload_type) {
turaj@webrtc.org42259e72012-12-11 02:15:12 +00005273 CodecInst codec;
5274 bool found_red = false;
5275
5276 // Get default RED settings from the ACM database
5277 const int num_codecs = AudioCodingModule::NumberOfCodecs();
5278 for (int idx = 0; idx < num_codecs; idx++) {
tina.legrand@webrtc.org7a7a0082013-02-21 10:27:48 +00005279 _audioCodingModule.Codec(idx, &codec);
turaj@webrtc.org42259e72012-12-11 02:15:12 +00005280 if (!STR_CASE_CMP(codec.plname, "RED")) {
5281 found_red = true;
5282 break;
5283 }
5284 }
5285
5286 if (!found_red) {
5287 _engineStatisticsPtr->SetLastError(
5288 VE_CODEC_ERROR, kTraceError,
5289 "SetRedPayloadType() RED is not supported");
5290 return -1;
5291 }
5292
turaj@webrtc.org9d532fd2013-01-31 18:34:19 +00005293 codec.pltype = red_payload_type;
turaj@webrtc.org42259e72012-12-11 02:15:12 +00005294 if (_audioCodingModule.RegisterSendCodec(codec) < 0) {
5295 _engineStatisticsPtr->SetLastError(
5296 VE_AUDIO_CODING_MODULE_ERROR, kTraceError,
5297 "SetRedPayloadType() RED registration in ACM module failed");
5298 return -1;
5299 }
5300
5301 if (_rtpRtcpModule->SetSendREDPayloadType(red_payload_type) != 0) {
5302 _engineStatisticsPtr->SetLastError(
5303 VE_RTP_RTCP_MODULE_ERROR, kTraceError,
5304 "SetRedPayloadType() RED registration in RTP/RTCP module failed");
5305 return -1;
5306 }
5307 return 0;
5308}
5309
niklase@google.com470e71d2011-07-07 08:21:25 +00005310} // namespace voe
niklase@google.com470e71d2011-07-07 08:21:25 +00005311} // namespace webrtc