blob: 4fc1c8e3e1d88dac54b65156f59ca915a2f518ae [file] [log] [blame]
/*
* Copyright (c) 2012 The WebRTC project authors. All Rights Reserved.
*
* Use of this source code is governed by a BSD-style license
* that can be found in the LICENSE file in the root of the source
* tree. An additional intellectual property rights grant can be found
* in the file PATENTS. All contributing project authors may
* be found in the AUTHORS file in the root of the source tree.
*/
#include "channel.h"
#include "audio_device.h"
#include "audio_frame_operations.h"
#include "audio_processing.h"
#include "critical_section_wrapper.h"
#include "output_mixer.h"
#include "process_thread.h"
#include "rtp_dump.h"
#include "statistics.h"
#include "trace.h"
#include "transmit_mixer.h"
#include "utility.h"
#include "voe_base.h"
#include "voe_external_media.h"
#include "voe_rtp_rtcp.h"
#if defined(_WIN32)
#include <Qos.h>
#endif
namespace webrtc
{
namespace voe
{
WebRtc_Word32
Channel::SendData(FrameType frameType,
WebRtc_UWord8 payloadType,
WebRtc_UWord32 timeStamp,
const WebRtc_UWord8* payloadData,
WebRtc_UWord16 payloadSize,
const RTPFragmentationHeader* fragmentation)
{
WEBRTC_TRACE(kTraceStream, kTraceVoice, VoEId(_instanceId,_channelId),
"Channel::SendData(frameType=%u, payloadType=%u, timeStamp=%u,"
" payloadSize=%u, fragmentation=0x%x)",
frameType, payloadType, timeStamp, payloadSize, fragmentation);
if (_includeAudioLevelIndication)
{
assert(_rtpAudioProc.get() != NULL);
// Store current audio level in the RTP/RTCP module.
// The level will be used in combination with voice-activity state
// (frameType) to add an RTP header extension
_rtpRtcpModule.SetAudioLevel(_rtpAudioProc->level_estimator()->RMS());
}
// Push data from ACM to RTP/RTCP-module to deliver audio frame for
// packetization.
// This call will trigger Transport::SendPacket() from the RTP/RTCP module.
if (_rtpRtcpModule.SendOutgoingData((FrameType&)frameType,
payloadType,
timeStamp,
payloadData,
payloadSize,
fragmentation) == -1)
{
_engineStatisticsPtr->SetLastError(
VE_RTP_RTCP_MODULE_ERROR, kTraceWarning,
"Channel::SendData() failed to send data to RTP/RTCP module");
return -1;
}
_lastLocalTimeStamp = timeStamp;
_lastPayloadType = payloadType;
return 0;
}
WebRtc_Word32
Channel::InFrameType(WebRtc_Word16 frameType)
{
WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId,_channelId),
"Channel::InFrameType(frameType=%d)", frameType);
CriticalSectionScoped cs(_callbackCritSect);
// 1 indicates speech
_sendFrameType = (frameType == 1) ? 1 : 0;
return 0;
}
#ifdef WEBRTC_DTMF_DETECTION
int
Channel::IncomingDtmf(const WebRtc_UWord8 digitDtmf, const bool end)
{
WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId,_channelId),
"Channel::IncomingDtmf(digitDtmf=%u, end=%d)",
digitDtmf, end);
if (digitDtmf != 999)
{
CriticalSectionScoped cs(_callbackCritSect);
if (_telephoneEventDetectionPtr)
{
_telephoneEventDetectionPtr->OnReceivedTelephoneEventInband(
_channelId, digitDtmf, end);
}
}
return 0;
}
#endif
WebRtc_Word32
Channel::OnRxVadDetected(const int vadDecision)
{
WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId, _channelId),
"Channel::OnRxVadDetected(vadDecision=%d)", vadDecision);
CriticalSectionScoped cs(_callbackCritSect);
if (_rxVadObserverPtr)
{
_rxVadObserverPtr->OnRxVad(_channelId, vadDecision);
}
return 0;
}
int
Channel::SendPacket(int channel, const void *data, int len)
{
channel = VoEChannelId(channel);
assert(channel == _channelId);
WEBRTC_TRACE(kTraceStream, kTraceVoice, VoEId(_instanceId,_channelId),
"Channel::SendPacket(channel=%d, len=%d)", channel, len);
if (_transportPtr == NULL)
{
WEBRTC_TRACE(kTraceError, kTraceVoice, VoEId(_instanceId,_channelId),
"Channel::SendPacket() failed to send RTP packet due to"
" invalid transport object");
return -1;
}
// Insert extra RTP packet using if user has called the InsertExtraRTPPacket
// API
if (_insertExtraRTPPacket)
{
WebRtc_UWord8* rtpHdr = (WebRtc_UWord8*)data;
WebRtc_UWord8 M_PT(0);
if (_extraMarkerBit)
{
M_PT = 0x80; // set the M-bit
}
M_PT += _extraPayloadType; // set the payload type
*(++rtpHdr) = M_PT; // modify the M|PT-byte within the RTP header
_insertExtraRTPPacket = false; // insert one packet only
}
WebRtc_UWord8* bufferToSendPtr = (WebRtc_UWord8*)data;
WebRtc_Word32 bufferLength = len;
// Dump the RTP packet to a file (if RTP dump is enabled).
if (_rtpDumpOut.DumpPacket((const WebRtc_UWord8*)data, len) == -1)
{
WEBRTC_TRACE(kTraceWarning, kTraceVoice,
VoEId(_instanceId,_channelId),
"Channel::SendPacket() RTP dump to output file failed");
}
// SRTP or External encryption
if (_encrypting)
{
CriticalSectionScoped cs(_callbackCritSect);
if (_encryptionPtr)
{
if (!_encryptionRTPBufferPtr)
{
// Allocate memory for encryption buffer one time only
_encryptionRTPBufferPtr =
new WebRtc_UWord8[kVoiceEngineMaxIpPacketSizeBytes];
}
// Perform encryption (SRTP or external)
WebRtc_Word32 encryptedBufferLength = 0;
_encryptionPtr->encrypt(_channelId,
bufferToSendPtr,
_encryptionRTPBufferPtr,
bufferLength,
(int*)&encryptedBufferLength);
if (encryptedBufferLength <= 0)
{
_engineStatisticsPtr->SetLastError(
VE_ENCRYPTION_FAILED,
kTraceError, "Channel::SendPacket() encryption failed");
return -1;
}
// Replace default data buffer with encrypted buffer
bufferToSendPtr = _encryptionRTPBufferPtr;
bufferLength = encryptedBufferLength;
}
}
// Packet transmission using WebRtc socket transport
if (!_externalTransport)
{
int n = _transportPtr->SendPacket(channel, bufferToSendPtr,
bufferLength);
if (n < 0)
{
WEBRTC_TRACE(kTraceError, kTraceVoice,
VoEId(_instanceId,_channelId),
"Channel::SendPacket() RTP transmission using WebRtc"
" sockets failed");
return -1;
}
return n;
}
// Packet transmission using external transport transport
{
CriticalSectionScoped cs(_callbackCritSect);
int n = _transportPtr->SendPacket(channel,
bufferToSendPtr,
bufferLength);
if (n < 0)
{
WEBRTC_TRACE(kTraceError, kTraceVoice,
VoEId(_instanceId,_channelId),
"Channel::SendPacket() RTP transmission using external"
" transport failed");
return -1;
}
return n;
}
}
int
Channel::SendRTCPPacket(int channel, const void *data, int len)
{
channel = VoEChannelId(channel);
assert(channel == _channelId);
WEBRTC_TRACE(kTraceStream, kTraceVoice, VoEId(_instanceId,_channelId),
"Channel::SendRTCPPacket(channel=%d, len=%d)", channel, len);
{
CriticalSectionScoped cs(_callbackCritSect);
if (_transportPtr == NULL)
{
WEBRTC_TRACE(kTraceError, kTraceVoice,
VoEId(_instanceId,_channelId),
"Channel::SendRTCPPacket() failed to send RTCP packet"
" due to invalid transport object");
return -1;
}
}
WebRtc_UWord8* bufferToSendPtr = (WebRtc_UWord8*)data;
WebRtc_Word32 bufferLength = len;
// Dump the RTCP packet to a file (if RTP dump is enabled).
if (_rtpDumpOut.DumpPacket((const WebRtc_UWord8*)data, len) == -1)
{
WEBRTC_TRACE(kTraceWarning, kTraceVoice,
VoEId(_instanceId,_channelId),
"Channel::SendPacket() RTCP dump to output file failed");
}
// SRTP or External encryption
if (_encrypting)
{
CriticalSectionScoped cs(_callbackCritSect);
if (_encryptionPtr)
{
if (!_encryptionRTCPBufferPtr)
{
// Allocate memory for encryption buffer one time only
_encryptionRTCPBufferPtr =
new WebRtc_UWord8[kVoiceEngineMaxIpPacketSizeBytes];
}
// Perform encryption (SRTP or external).
WebRtc_Word32 encryptedBufferLength = 0;
_encryptionPtr->encrypt_rtcp(_channelId,
bufferToSendPtr,
_encryptionRTCPBufferPtr,
bufferLength,
(int*)&encryptedBufferLength);
if (encryptedBufferLength <= 0)
{
_engineStatisticsPtr->SetLastError(
VE_ENCRYPTION_FAILED, kTraceError,
"Channel::SendRTCPPacket() encryption failed");
return -1;
}
// Replace default data buffer with encrypted buffer
bufferToSendPtr = _encryptionRTCPBufferPtr;
bufferLength = encryptedBufferLength;
}
}
// Packet transmission using WebRtc socket transport
if (!_externalTransport)
{
int n = _transportPtr->SendRTCPPacket(channel,
bufferToSendPtr,
bufferLength);
if (n < 0)
{
WEBRTC_TRACE(kTraceInfo, kTraceVoice,
VoEId(_instanceId,_channelId),
"Channel::SendRTCPPacket() transmission using WebRtc"
" sockets failed");
return -1;
}
return n;
}
// Packet transmission using external transport transport
{
CriticalSectionScoped cs(_callbackCritSect);
int n = _transportPtr->SendRTCPPacket(channel,
bufferToSendPtr,
bufferLength);
if (n < 0)
{
WEBRTC_TRACE(kTraceInfo, kTraceVoice,
VoEId(_instanceId,_channelId),
"Channel::SendRTCPPacket() transmission using external"
" transport failed");
return -1;
}
return n;
}
return len;
}
void
Channel::IncomingRTPPacket(const WebRtc_Word8* incomingRtpPacket,
const WebRtc_Word32 rtpPacketLength,
const WebRtc_Word8* fromIP,
const WebRtc_UWord16 fromPort)
{
WEBRTC_TRACE(kTraceStream, kTraceVoice, VoEId(_instanceId,_channelId),
"Channel::IncomingRTPPacket(rtpPacketLength=%d,"
" fromIP=%s, fromPort=%u)",
rtpPacketLength, fromIP, fromPort);
// Store playout timestamp for the received RTP packet
// to be used for upcoming delay estimations
WebRtc_UWord32 playoutTimestamp(0);
if (GetPlayoutTimeStamp(playoutTimestamp) == 0)
{
_playoutTimeStampRTP = playoutTimestamp;
}
WebRtc_UWord8* rtpBufferPtr = (WebRtc_UWord8*)incomingRtpPacket;
WebRtc_Word32 rtpBufferLength = rtpPacketLength;
// SRTP or External decryption
if (_decrypting)
{
CriticalSectionScoped cs(_callbackCritSect);
if (_encryptionPtr)
{
if (!_decryptionRTPBufferPtr)
{
// Allocate memory for decryption buffer one time only
_decryptionRTPBufferPtr =
new WebRtc_UWord8[kVoiceEngineMaxIpPacketSizeBytes];
}
// Perform decryption (SRTP or external)
WebRtc_Word32 decryptedBufferLength = 0;
_encryptionPtr->decrypt(_channelId,
rtpBufferPtr,
_decryptionRTPBufferPtr,
rtpBufferLength,
(int*)&decryptedBufferLength);
if (decryptedBufferLength <= 0)
{
_engineStatisticsPtr->SetLastError(
VE_DECRYPTION_FAILED, kTraceError,
"Channel::IncomingRTPPacket() decryption failed");
return;
}
// Replace default data buffer with decrypted buffer
rtpBufferPtr = _decryptionRTPBufferPtr;
rtpBufferLength = decryptedBufferLength;
}
}
// Dump the RTP packet to a file (if RTP dump is enabled).
if (_rtpDumpIn.DumpPacket(rtpBufferPtr,
(WebRtc_UWord16)rtpBufferLength) == -1)
{
WEBRTC_TRACE(kTraceWarning, kTraceVoice,
VoEId(_instanceId,_channelId),
"Channel::SendPacket() RTP dump to input file failed");
}
// Deliver RTP packet to RTP/RTCP module for parsing
// The packet will be pushed back to the channel thru the
// OnReceivedPayloadData callback so we don't push it to the ACM here
if (_rtpRtcpModule.IncomingPacket((const WebRtc_UWord8*)rtpBufferPtr,
(WebRtc_UWord16)rtpBufferLength) == -1)
{
_engineStatisticsPtr->SetLastError(
VE_SOCKET_TRANSPORT_MODULE_ERROR, kTraceWarning,
"Channel::IncomingRTPPacket() RTP packet is invalid");
return;
}
}
void
Channel::IncomingRTCPPacket(const WebRtc_Word8* incomingRtcpPacket,
const WebRtc_Word32 rtcpPacketLength,
const WebRtc_Word8* fromIP,
const WebRtc_UWord16 fromPort)
{
WEBRTC_TRACE(kTraceStream, kTraceVoice, VoEId(_instanceId,_channelId),
"Channel::IncomingRTCPPacket(rtcpPacketLength=%d, fromIP=%s,"
" fromPort=%u)",
rtcpPacketLength, fromIP, fromPort);
// Temporary buffer pointer and size for decryption
WebRtc_UWord8* rtcpBufferPtr = (WebRtc_UWord8*)incomingRtcpPacket;
WebRtc_Word32 rtcpBufferLength = rtcpPacketLength;
// Store playout timestamp for the received RTCP packet
// which will be read by the GetRemoteRTCPData API
WebRtc_UWord32 playoutTimestamp(0);
if (GetPlayoutTimeStamp(playoutTimestamp) == 0)
{
_playoutTimeStampRTCP = playoutTimestamp;
}
// SRTP or External decryption
if (_decrypting)
{
CriticalSectionScoped cs(_callbackCritSect);
if (_encryptionPtr)
{
if (!_decryptionRTCPBufferPtr)
{
// Allocate memory for decryption buffer one time only
_decryptionRTCPBufferPtr =
new WebRtc_UWord8[kVoiceEngineMaxIpPacketSizeBytes];
}
// Perform decryption (SRTP or external).
WebRtc_Word32 decryptedBufferLength = 0;
_encryptionPtr->decrypt_rtcp(_channelId,
rtcpBufferPtr,
_decryptionRTCPBufferPtr,
rtcpBufferLength,
(int*)&decryptedBufferLength);
if (decryptedBufferLength <= 0)
{
_engineStatisticsPtr->SetLastError(
VE_DECRYPTION_FAILED, kTraceError,
"Channel::IncomingRTCPPacket() decryption failed");
return;
}
// Replace default data buffer with decrypted buffer
rtcpBufferPtr = _decryptionRTCPBufferPtr;
rtcpBufferLength = decryptedBufferLength;
}
}
// Dump the RTCP packet to a file (if RTP dump is enabled).
if (_rtpDumpIn.DumpPacket(rtcpBufferPtr,
(WebRtc_UWord16)rtcpBufferLength) == -1)
{
WEBRTC_TRACE(kTraceWarning, kTraceVoice,
VoEId(_instanceId,_channelId),
"Channel::SendPacket() RTCP dump to input file failed");
}
// Deliver RTCP packet to RTP/RTCP module for parsing
if (_rtpRtcpModule.IncomingPacket((const WebRtc_UWord8*)rtcpBufferPtr,
(WebRtc_UWord16)rtcpBufferLength) == -1)
{
_engineStatisticsPtr->SetLastError(
VE_SOCKET_TRANSPORT_MODULE_ERROR, kTraceWarning,
"Channel::IncomingRTPPacket() RTCP packet is invalid");
return;
}
}
void
Channel::OnReceivedTelephoneEvent(const WebRtc_Word32 id,
const WebRtc_UWord8 event,
const bool endOfEvent)
{
WEBRTC_TRACE(kTraceStream, kTraceVoice, VoEId(_instanceId,_channelId),
"Channel::OnReceivedTelephoneEvent(id=%d, event=%u,"
" endOfEvent=%d)", id, event, endOfEvent);
#ifdef WEBRTC_DTMF_DETECTION
if (_outOfBandTelephoneEventDetecion)
{
CriticalSectionScoped cs(_callbackCritSect);
if (_telephoneEventDetectionPtr)
{
_telephoneEventDetectionPtr->OnReceivedTelephoneEventOutOfBand(
_channelId, event, endOfEvent);
}
}
#endif
}
void
Channel::OnPlayTelephoneEvent(const WebRtc_Word32 id,
const WebRtc_UWord8 event,
const WebRtc_UWord16 lengthMs,
const WebRtc_UWord8 volume)
{
WEBRTC_TRACE(kTraceStream, kTraceVoice, VoEId(_instanceId,_channelId),
"Channel::OnPlayTelephoneEvent(id=%d, event=%u, lengthMs=%u,"
" volume=%u)", id, event, lengthMs, volume);
if (!_playOutbandDtmfEvent || (event > 15))
{
// Ignore callback since feedback is disabled or event is not a
// Dtmf tone event.
return;
}
assert(_outputMixerPtr != NULL);
// Start playing out the Dtmf tone (if playout is enabled).
// Reduce length of tone with 80ms to the reduce risk of echo.
_outputMixerPtr->PlayDtmfTone(event, lengthMs - 80, volume);
}
void
Channel::OnIncomingSSRCChanged(const WebRtc_Word32 id,
const WebRtc_UWord32 SSRC)
{
WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId,_channelId),
"Channel::OnIncomingSSRCChanged(id=%d, SSRC=%d)",
id, SSRC);
WebRtc_Word32 channel = VoEChannelId(id);
assert(channel == _channelId);
// Reset RTP-module counters since a new incoming RTP stream is detected
_rtpRtcpModule.ResetReceiveDataCountersRTP();
_rtpRtcpModule.ResetStatisticsRTP();
if (_rtpObserver)
{
CriticalSectionScoped cs(_callbackCritSect);
if (_rtpObserverPtr)
{
// Send new SSRC to registered observer using callback
_rtpObserverPtr->OnIncomingSSRCChanged(channel, SSRC);
}
}
}
void Channel::OnIncomingCSRCChanged(const WebRtc_Word32 id,
const WebRtc_UWord32 CSRC,
const bool added)
{
WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId,_channelId),
"Channel::OnIncomingCSRCChanged(id=%d, CSRC=%d, added=%d)",
id, CSRC, added);
WebRtc_Word32 channel = VoEChannelId(id);
assert(channel == _channelId);
if (_rtpObserver)
{
CriticalSectionScoped cs(_callbackCritSect);
if (_rtpObserverPtr)
{
_rtpObserverPtr->OnIncomingCSRCChanged(channel, CSRC, added);
}
}
}
void
Channel::OnApplicationDataReceived(const WebRtc_Word32 id,
const WebRtc_UWord8 subType,
const WebRtc_UWord32 name,
const WebRtc_UWord16 length,
const WebRtc_UWord8* data)
{
WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId,_channelId),
"Channel::OnApplicationDataReceived(id=%d, subType=%u,"
" name=%u, length=%u)",
id, subType, name, length);
WebRtc_Word32 channel = VoEChannelId(id);
assert(channel == _channelId);
if (_rtcpObserver)
{
CriticalSectionScoped cs(_callbackCritSect);
if (_rtcpObserverPtr)
{
_rtcpObserverPtr->OnApplicationDataReceived(channel,
subType,
name,
data,
length);
}
}
}
WebRtc_Word32
Channel::OnInitializeDecoder(
const WebRtc_Word32 id,
const WebRtc_Word8 payloadType,
const WebRtc_Word8 payloadName[RTP_PAYLOAD_NAME_SIZE],
const int frequency,
const WebRtc_UWord8 channels,
const WebRtc_UWord32 rate)
{
WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId,_channelId),
"Channel::OnInitializeDecoder(id=%d, payloadType=%d, "
"payloadName=%s, frequency=%u, channels=%u, rate=%u)",
id, payloadType, payloadName, frequency, channels, rate);
assert(VoEChannelId(id) == _channelId);
CodecInst receiveCodec = {0};
CodecInst dummyCodec = {0};
receiveCodec.pltype = payloadType;
receiveCodec.plfreq = frequency;
receiveCodec.channels = channels;
receiveCodec.rate = rate;
strncpy(receiveCodec.plname, payloadName, RTP_PAYLOAD_NAME_SIZE - 1);
_audioCodingModule.Codec(payloadName, dummyCodec, frequency);
receiveCodec.pacsize = dummyCodec.pacsize;
// Register the new codec to the ACM
if (_audioCodingModule.RegisterReceiveCodec(receiveCodec) == -1)
{
WEBRTC_TRACE(kTraceWarning, kTraceVoice,
VoEId(_instanceId, _channelId),
"Channel::OnInitializeDecoder() invalid codec ("
"pt=%d, name=%s) received - 1", payloadType, payloadName);
_engineStatisticsPtr->SetLastError(VE_AUDIO_CODING_MODULE_ERROR);
return -1;
}
return 0;
}
void
Channel::OnPacketTimeout(const WebRtc_Word32 id)
{
WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId,_channelId),
"Channel::OnPacketTimeout(id=%d)", id);
CriticalSectionScoped cs(*_callbackCritSectPtr);
if (_voiceEngineObserverPtr)
{
if (_receiving || _externalTransport)
{
WebRtc_Word32 channel = VoEChannelId(id);
assert(channel == _channelId);
// Ensure that next OnReceivedPacket() callback will trigger
// a VE_PACKET_RECEIPT_RESTARTED callback.
_rtpPacketTimedOut = true;
// Deliver callback to the observer
WEBRTC_TRACE(kTraceInfo, kTraceVoice,
VoEId(_instanceId,_channelId),
"Channel::OnPacketTimeout() => "
"CallbackOnError(VE_RECEIVE_PACKET_TIMEOUT)");
_voiceEngineObserverPtr->CallbackOnError(channel,
VE_RECEIVE_PACKET_TIMEOUT);
}
}
}
void
Channel::OnReceivedPacket(const WebRtc_Word32 id,
const RtpRtcpPacketType packetType)
{
WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId,_channelId),
"Channel::OnReceivedPacket(id=%d, packetType=%d)",
id, packetType);
assert(VoEChannelId(id) == _channelId);
// Notify only for the case when we have restarted an RTP session.
if (_rtpPacketTimedOut && (kPacketRtp == packetType))
{
CriticalSectionScoped cs(*_callbackCritSectPtr);
if (_voiceEngineObserverPtr)
{
WebRtc_Word32 channel = VoEChannelId(id);
assert(channel == _channelId);
// Reset timeout mechanism
_rtpPacketTimedOut = false;
// Deliver callback to the observer
WEBRTC_TRACE(kTraceInfo, kTraceVoice,
VoEId(_instanceId,_channelId),
"Channel::OnPacketTimeout() =>"
" CallbackOnError(VE_PACKET_RECEIPT_RESTARTED)");
_voiceEngineObserverPtr->CallbackOnError(
channel,
VE_PACKET_RECEIPT_RESTARTED);
}
}
}
void
Channel::OnPeriodicDeadOrAlive(const WebRtc_Word32 id,
const RTPAliveType alive)
{
WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId,_channelId),
"Channel::OnPeriodicDeadOrAlive(id=%d, alive=%d)", id, alive);
if (!_connectionObserver)
return;
WebRtc_Word32 channel = VoEChannelId(id);
assert(channel == _channelId);
// Use Alive as default to limit risk of false Dead detections
bool isAlive(true);
// Always mark the connection as Dead when the module reports kRtpDead
if (kRtpDead == alive)
{
isAlive = false;
}
// It is possible that the connection is alive even if no RTP packet has
// been received for a long time since the other side might use VAD/DTX
// and a low SID-packet update rate.
if ((kRtpNoRtp == alive) && _playing)
{
// Detect Alive for all NetEQ states except for the case when we are
// in PLC_CNG state.
// PLC_CNG <=> background noise only due to long expand or error.
// Note that, the case where the other side stops sending during CNG
// state will be detected as Alive. Dead is is not set until after
// missing RTCP packets for at least twelve seconds (handled
// internally by the RTP/RTCP module).
isAlive = (_outputSpeechType != AudioFrame::kPLCCNG);
}
UpdateDeadOrAliveCounters(isAlive);
// Send callback to the registered observer
if (_connectionObserver)
{
CriticalSectionScoped cs(_callbackCritSect);
if (_connectionObserverPtr)
{
_connectionObserverPtr->OnPeriodicDeadOrAlive(channel, isAlive);
}
}
}
WebRtc_Word32
Channel::OnReceivedPayloadData(const WebRtc_UWord8* payloadData,
const WebRtc_UWord16 payloadSize,
const WebRtcRTPHeader* rtpHeader)
{
WEBRTC_TRACE(kTraceStream, kTraceVoice, VoEId(_instanceId,_channelId),
"Channel::OnReceivedPayloadData(payloadSize=%d,"
" payloadType=%u, audioChannel=%u)",
payloadSize,
rtpHeader->header.payloadType,
rtpHeader->type.Audio.channel);
if (!_playing)
{
// Avoid inserting into NetEQ when we are not playing. Count the
// packet as discarded.
WEBRTC_TRACE(kTraceStream, kTraceVoice,
VoEId(_instanceId, _channelId),
"received packet is discarded since playing is not"
" activated");
_numberOfDiscardedPackets++;
return 0;
}
// Push the incoming payload (parsed and ready for decoding) into the ACM
if (_audioCodingModule.IncomingPacket((const WebRtc_Word8*) payloadData,
payloadSize,
*rtpHeader) != 0)
{
_engineStatisticsPtr->SetLastError(
VE_AUDIO_CODING_MODULE_ERROR, kTraceWarning,
"Channel::OnReceivedPayloadData() unable to push data to the ACM");
return -1;
}
// Update the packet delay
UpdatePacketDelay(rtpHeader->header.timestamp,
rtpHeader->header.sequenceNumber);
return 0;
}
WebRtc_Word32 Channel::GetAudioFrame(const WebRtc_Word32 id,
AudioFrame& audioFrame)
{
WEBRTC_TRACE(kTraceStream, kTraceVoice, VoEId(_instanceId,_channelId),
"Channel::GetAudioFrame(id=%d)", id);
// Get 10ms raw PCM data from the ACM (mixer limits output frequency)
if (_audioCodingModule.PlayoutData10Ms(audioFrame._frequencyInHz,
audioFrame) == -1)
{
WEBRTC_TRACE(kTraceError, kTraceVoice,
VoEId(_instanceId,_channelId),
"Channel::GetAudioFrame() PlayoutData10Ms() failed!");
// In all likelihood, the audio in this frame is garbage. We return an
// error so that the audio mixer module doesn't add it to the mix. As
// a result, it won't be played out and the actions skipped here are
// irrelevant.
return -1;
}
if (_RxVadDetection)
{
UpdateRxVadDetection(audioFrame);
}
// Convert module ID to internal VoE channel ID
audioFrame._id = VoEChannelId(audioFrame._id);
// Store speech type for dead-or-alive detection
_outputSpeechType = audioFrame._speechType;
// Perform far-end AudioProcessing module processing on the received signal
if (_rxApmIsEnabled)
{
ApmProcessRx(audioFrame);
}
// Output volume scaling
if (_outputGain < 0.99f || _outputGain > 1.01f)
{
AudioFrameOperations::ScaleWithSat(_outputGain, audioFrame);
}
// Scale left and/or right channel(s) if stereo and master balance is
// active
if (_panLeft != 1.0f || _panRight != 1.0f)
{
if (audioFrame._audioChannel == 1)
{
// Emulate stereo mode since panning is active.
// The mono signal is copied to both left and right channels here.
AudioFrameOperations::MonoToStereo(audioFrame);
}
// For true stereo mode (when we are receiving a stereo signal), no
// action is needed.
// Do the panning operation (the audio frame contains stereo at this
// stage)
AudioFrameOperations::Scale(_panLeft, _panRight, audioFrame);
}
// Mix decoded PCM output with file if file mixing is enabled
if (_outputFilePlaying)
{
MixAudioWithFile(audioFrame, audioFrame._frequencyInHz);
}
// Place channel in on-hold state (~muted) if on-hold is activated
if (_outputIsOnHold)
{
AudioFrameOperations::Mute(audioFrame);
}
// External media
if (_outputExternalMedia)
{
CriticalSectionScoped cs(_callbackCritSect);
const bool isStereo = (audioFrame._audioChannel == 2);
if (_outputExternalMediaCallbackPtr)
{
_outputExternalMediaCallbackPtr->Process(
_channelId,
kPlaybackPerChannel,
(WebRtc_Word16*)audioFrame._payloadData,
audioFrame._payloadDataLengthInSamples,
audioFrame._frequencyInHz,
isStereo);
}
}
// Record playout if enabled
{
CriticalSectionScoped cs(_fileCritSect);
if (_outputFileRecording && _outputFileRecorderPtr)
{
if(audioFrame._audioChannel == 2)
{
AudioFrame temp = audioFrame;
AudioFrameOperations::StereoToMono (temp);
_outputFileRecorderPtr->RecordAudioToFile(temp);
}
else if(audioFrame._audioChannel == 1)
{
_outputFileRecorderPtr->RecordAudioToFile(audioFrame);
}
else
{
assert(false);
}
}
}
// Measure audio level (0-9)
_outputAudioLevel.ComputeLevel(audioFrame);
return 0;
}
WebRtc_Word32
Channel::NeededFrequency(const WebRtc_Word32 id)
{
WEBRTC_TRACE(kTraceStream, kTraceVoice, VoEId(_instanceId,_channelId),
"Channel::NeededFrequency(id=%d)", id);
int highestNeeded = 0;
// Determine highest needed receive frequency
WebRtc_Word32 receiveFrequency = _audioCodingModule.ReceiveFrequency();
// Return the bigger of playout and receive frequency in the ACM.
if (_audioCodingModule.PlayoutFrequency() > receiveFrequency)
{
highestNeeded = _audioCodingModule.PlayoutFrequency();
}
else
{
highestNeeded = receiveFrequency;
}
// Special case, if we're playing a file on the playout side
// we take that frequency into consideration as well
// This is not needed on sending side, since the codec will
// limit the spectrum anyway.
if (_outputFilePlaying)
{
CriticalSectionScoped cs(_fileCritSect);
if (_outputFilePlayerPtr && _outputFilePlaying)
{
if(_outputFilePlayerPtr->Frequency()>highestNeeded)
{
highestNeeded=_outputFilePlayerPtr->Frequency();
}
}
}
return(highestNeeded);
}
WebRtc_Word32
Channel::CreateChannel(Channel*& channel,
const WebRtc_Word32 channelId,
const WebRtc_UWord32 instanceId)
{
WEBRTC_TRACE(kTraceMemory, kTraceVoice, VoEId(instanceId,channelId),
"Channel::CreateChannel(channelId=%d, instanceId=%d)",
channelId, instanceId);
channel = new Channel(channelId, instanceId);
if (channel == NULL)
{
WEBRTC_TRACE(kTraceMemory, kTraceVoice,
VoEId(instanceId,channelId),
"Channel::CreateChannel() unable to allocate memory for"
" channel");
return -1;
}
return 0;
}
void
Channel::PlayNotification(const WebRtc_Word32 id,
const WebRtc_UWord32 durationMs)
{
WEBRTC_TRACE(kTraceStream, kTraceVoice, VoEId(_instanceId,_channelId),
"Channel::PlayNotification(id=%d, durationMs=%d)",
id, durationMs);
// Not implement yet
}
void
Channel::RecordNotification(const WebRtc_Word32 id,
const WebRtc_UWord32 durationMs)
{
WEBRTC_TRACE(kTraceStream, kTraceVoice, VoEId(_instanceId,_channelId),
"Channel::RecordNotification(id=%d, durationMs=%d)",
id, durationMs);
// Not implement yet
}
void
Channel::PlayFileEnded(const WebRtc_Word32 id)
{
WEBRTC_TRACE(kTraceStream, kTraceVoice, VoEId(_instanceId,_channelId),
"Channel::PlayFileEnded(id=%d)", id);
if (id == _inputFilePlayerId)
{
CriticalSectionScoped cs(_fileCritSect);
_inputFilePlaying = false;
WEBRTC_TRACE(kTraceStateInfo, kTraceVoice,
VoEId(_instanceId,_channelId),
"Channel::PlayFileEnded() => input file player module is"
" shutdown");
}
else if (id == _outputFilePlayerId)
{
CriticalSectionScoped cs(_fileCritSect);
_outputFilePlaying = false;
WEBRTC_TRACE(kTraceStateInfo, kTraceVoice,
VoEId(_instanceId,_channelId),
"Channel::PlayFileEnded() => output file player module is"
" shutdown");
}
}
void
Channel::RecordFileEnded(const WebRtc_Word32 id)
{
WEBRTC_TRACE(kTraceStream, kTraceVoice, VoEId(_instanceId,_channelId),
"Channel::RecordFileEnded(id=%d)", id);
assert(id == _outputFileRecorderId);
CriticalSectionScoped cs(_fileCritSect);
_outputFileRecording = false;
WEBRTC_TRACE(kTraceStateInfo, kTraceVoice,
VoEId(_instanceId,_channelId),
"Channel::RecordFileEnded() => output file recorder module is"
" shutdown");
}
Channel::Channel(const WebRtc_Word32 channelId,
const WebRtc_UWord32 instanceId) :
_fileCritSect(*CriticalSectionWrapper::CreateCriticalSection()),
_callbackCritSect(*CriticalSectionWrapper::CreateCriticalSection()),
_transmitCritSect(*CriticalSectionWrapper::CreateCriticalSection()),
_instanceId(instanceId),
_channelId(channelId),
_rtpRtcpModule(*RtpRtcp::CreateRtpRtcp(VoEModuleId(
instanceId, channelId), true)),
_audioCodingModule(*AudioCodingModule::Create(
VoEModuleId(instanceId, channelId))),
#ifndef WEBRTC_EXTERNAL_TRANSPORT
_numSocketThreads(KNumSocketThreads),
_socketTransportModule(*UdpTransport::Create(
VoEModuleId(instanceId, channelId), _numSocketThreads)),
#endif
#ifdef WEBRTC_SRTP
_srtpModule(*SrtpModule::CreateSrtpModule(VoEModuleId(instanceId,
channelId))),
#endif
_rtpDumpIn(*RtpDump::CreateRtpDump()),
_rtpDumpOut(*RtpDump::CreateRtpDump()),
_outputAudioLevel(),
_externalTransport(false),
_inputFilePlayerPtr(NULL),
_outputFilePlayerPtr(NULL),
_outputFileRecorderPtr(NULL),
// Avoid conflict with other channels by adding 1024 - 1026,
// won't use as much as 1024 channels.
_inputFilePlayerId(VoEModuleId(instanceId, channelId) + 1024),
_outputFilePlayerId(VoEModuleId(instanceId, channelId) + 1025),
_outputFileRecorderId(VoEModuleId(instanceId, channelId) + 1026),
_inputFilePlaying(false),
_outputFilePlaying(false),
_outputFileRecording(false),
_inbandDtmfQueue(VoEModuleId(instanceId, channelId)),
_inbandDtmfGenerator(VoEModuleId(instanceId, channelId)),
_inputExternalMedia(false),
_outputExternalMedia(false),
_inputExternalMediaCallbackPtr(NULL),
_outputExternalMediaCallbackPtr(NULL),
_encryptionRTPBufferPtr(NULL),
_decryptionRTPBufferPtr(NULL),
_encryptionRTCPBufferPtr(NULL),
_decryptionRTCPBufferPtr(NULL),
_timeStamp(0), // This is just an offset, RTP module will add it's own random offset
_sendTelephoneEventPayloadType(106),
_playoutTimeStampRTP(0),
_playoutTimeStampRTCP(0),
_numberOfDiscardedPackets(0),
_engineStatisticsPtr(NULL),
_outputMixerPtr(NULL),
_transmitMixerPtr(NULL),
_moduleProcessThreadPtr(NULL),
_audioDeviceModulePtr(NULL),
_voiceEngineObserverPtr(NULL),
_callbackCritSectPtr(NULL),
_transportPtr(NULL),
_encryptionPtr(NULL),
_rtpAudioProc(NULL),
_rxAudioProcessingModulePtr(NULL),
#ifdef WEBRTC_DTMF_DETECTION
_telephoneEventDetectionPtr(NULL),
#endif
_rxVadObserverPtr(NULL),
_oldVadDecision(-1),
_sendFrameType(0),
_rtpObserverPtr(NULL),
_rtcpObserverPtr(NULL),
_outputIsOnHold(false),
_externalPlayout(false),
_inputIsOnHold(false),
_playing(false),
_sending(false),
_receiving(false),
_mixFileWithMicrophone(false),
_rtpObserver(false),
_rtcpObserver(false),
_mute(false),
_panLeft(1.0f),
_panRight(1.0f),
_outputGain(1.0f),
_encrypting(false),
_decrypting(false),
_playOutbandDtmfEvent(false),
_playInbandDtmfEvent(false),
_inbandTelephoneEventDetection(false),
_outOfBandTelephoneEventDetecion(false),
_extraPayloadType(0),
_insertExtraRTPPacket(false),
_extraMarkerBit(false),
_lastLocalTimeStamp(0),
_lastPayloadType(0),
_includeAudioLevelIndication(false),
_rtpPacketTimedOut(false),
_rtpPacketTimeOutIsEnabled(false),
_rtpTimeOutSeconds(0),
_connectionObserver(false),
_connectionObserverPtr(NULL),
_countAliveDetections(0),
_countDeadDetections(0),
_outputSpeechType(AudioFrame::kNormalSpeech),
_averageDelayMs(0),
_previousSequenceNumber(0),
_previousTimestamp(0),
_recPacketDelayMs(20),
_RxVadDetection(false),
_rxApmIsEnabled(false),
_rxAgcIsEnabled(false),
_rxNsIsEnabled(false)
{
WEBRTC_TRACE(kTraceMemory, kTraceVoice, VoEId(_instanceId,_channelId),
"Channel::Channel() - ctor");
_inbandDtmfQueue.ResetDtmf();
_inbandDtmfGenerator.Init();
_outputAudioLevel.Clear();
// Create far end AudioProcessing Module
_rxAudioProcessingModulePtr = AudioProcessing::Create(
VoEModuleId(instanceId, channelId));
}
Channel::~Channel()
{
WEBRTC_TRACE(kTraceMemory, kTraceVoice, VoEId(_instanceId,_channelId),
"Channel::~Channel() - dtor");
if (_outputExternalMedia)
{
DeRegisterExternalMediaProcessing(kPlaybackPerChannel);
}
if (_inputExternalMedia)
{
DeRegisterExternalMediaProcessing(kRecordingPerChannel);
}
StopSend();
#ifndef WEBRTC_EXTERNAL_TRANSPORT
StopReceiving();
// De-register packet callback to ensure we're not in a callback when
// deleting channel state, avoids race condition and deadlock.
if (_socketTransportModule.InitializeReceiveSockets(NULL, 0, NULL, NULL, 0)
!= 0)
{
WEBRTC_TRACE(kTraceWarning, kTraceVoice,
VoEId(_instanceId, _channelId),
"~Channel() failed to de-register receive callback");
}
#endif
StopPlayout();
{
CriticalSectionScoped cs(_fileCritSect);
if (_inputFilePlayerPtr)
{
_inputFilePlayerPtr->RegisterModuleFileCallback(NULL);
_inputFilePlayerPtr->StopPlayingFile();
FilePlayer::DestroyFilePlayer(_inputFilePlayerPtr);
_inputFilePlayerPtr = NULL;
}
if (_outputFilePlayerPtr)
{
_outputFilePlayerPtr->RegisterModuleFileCallback(NULL);
_outputFilePlayerPtr->StopPlayingFile();
FilePlayer::DestroyFilePlayer(_outputFilePlayerPtr);
_outputFilePlayerPtr = NULL;
}
if (_outputFileRecorderPtr)
{
_outputFileRecorderPtr->RegisterModuleFileCallback(NULL);
_outputFileRecorderPtr->StopRecording();
FileRecorder::DestroyFileRecorder(_outputFileRecorderPtr);
_outputFileRecorderPtr = NULL;
}
}
// The order to safely shutdown modules in a channel is:
// 1. De-register callbacks in modules
// 2. De-register modules in process thread
// 3. Destroy modules
// De-register all RTP module callbacks to ensure geting no callbacks
// (Receive socket callback was de-registered above)
if (_rtpRtcpModule.RegisterIncomingDataCallback(NULL) == -1)
{
WEBRTC_TRACE(kTraceWarning, kTraceVoice,
VoEId(_instanceId,_channelId),
"~Channel() failed to de-register incoming data callback"
" (RTP module)");
}
if (_rtpRtcpModule.RegisterSendTransport(NULL) == -1)
{
WEBRTC_TRACE(kTraceWarning, kTraceVoice,
VoEId(_instanceId,_channelId),
"~Channel() failed to de-register send transport "
"(RTP module)");
}
if (_rtpRtcpModule.RegisterIncomingRTPCallback(NULL) == -1)
{
WEBRTC_TRACE(kTraceWarning, kTraceVoice,
VoEId(_instanceId,_channelId),
"~Channel() failed to de-register incoming RTP"
" callback (RTP module)");
}
if (_rtpRtcpModule.RegisterIncomingRTCPCallback(NULL) == -1)
{
WEBRTC_TRACE(kTraceWarning, kTraceVoice,
VoEId(_instanceId,_channelId),
"~Channel() failed to de-register incoming RTCP "
"callback (RTP module)");
}
if (_rtpRtcpModule.RegisterAudioCallback(NULL) == -1)
{
WEBRTC_TRACE(kTraceWarning, kTraceVoice,
VoEId(_instanceId,_channelId),
"~Channel() failed to de-register audio callback "
"(RTP module)");
}
if (_audioCodingModule.RegisterTransportCallback(NULL) == -1)
{
WEBRTC_TRACE(kTraceWarning, kTraceVoice,
VoEId(_instanceId,_channelId),
"~Channel() failed to de-register transport callback"
" (Audio coding module)");
}
if (_audioCodingModule.RegisterVADCallback(NULL) == -1)
{
WEBRTC_TRACE(kTraceWarning, kTraceVoice,
VoEId(_instanceId,_channelId),
"~Channel() failed to de-register VAD callback"
" (Audio coding module)");
}
#ifdef WEBRTC_DTMF_DETECTION
if (_audioCodingModule.RegisterIncomingMessagesCallback(NULL) == -1)
{
WEBRTC_TRACE(kTraceWarning, kTraceVoice,
VoEId(_instanceId,_channelId),
"~Channel() failed to de-register incoming messages "
"callback (Audio coding module)");
}
#endif
// De-register modules in process thread
#ifndef WEBRTC_EXTERNAL_TRANSPORT
if (_moduleProcessThreadPtr->DeRegisterModule(&_socketTransportModule)
== -1)
{
WEBRTC_TRACE(kTraceInfo, kTraceVoice,
VoEId(_instanceId,_channelId),
"~Channel() failed to deregister socket module");
}
#endif
if (_moduleProcessThreadPtr->DeRegisterModule(&_rtpRtcpModule) == -1)
{
WEBRTC_TRACE(kTraceInfo, kTraceVoice,
VoEId(_instanceId,_channelId),
"~Channel() failed to deregister RTP/RTCP module");
}
// Destroy modules
#ifndef WEBRTC_EXTERNAL_TRANSPORT
UdpTransport::Destroy(
&_socketTransportModule);
#endif
RtpRtcp::DestroyRtpRtcp(&_rtpRtcpModule);
AudioCodingModule::Destroy(&_audioCodingModule);
#ifdef WEBRTC_SRTP
SrtpModule::DestroySrtpModule(&_srtpModule);
#endif
if (_rxAudioProcessingModulePtr != NULL)
{
AudioProcessing::Destroy(_rxAudioProcessingModulePtr); // far end APM
_rxAudioProcessingModulePtr = NULL;
}
// End of modules shutdown
// Delete other objects
RtpDump::DestroyRtpDump(&_rtpDumpIn);
RtpDump::DestroyRtpDump(&_rtpDumpOut);
delete [] _encryptionRTPBufferPtr;
delete [] _decryptionRTPBufferPtr;
delete [] _encryptionRTCPBufferPtr;
delete [] _decryptionRTCPBufferPtr;
delete &_callbackCritSect;
delete &_transmitCritSect;
delete &_fileCritSect;
}
WebRtc_Word32
Channel::Init()
{
WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId,_channelId),
"Channel::Init()");
// --- Initial sanity
if ((_engineStatisticsPtr == NULL) ||
(_moduleProcessThreadPtr == NULL))
{
WEBRTC_TRACE(kTraceError, kTraceVoice,
VoEId(_instanceId,_channelId),
"Channel::Init() must call SetEngineInformation() first");
return -1;
}
// --- Add modules to process thread (for periodic schedulation)
const bool processThreadFail =
((_moduleProcessThreadPtr->RegisterModule(&_rtpRtcpModule) != 0) ||
#ifndef WEBRTC_EXTERNAL_TRANSPORT
(_moduleProcessThreadPtr->RegisterModule(
&_socketTransportModule) != 0));
#else
false);
#endif
if (processThreadFail)
{
_engineStatisticsPtr->SetLastError(
VE_CANNOT_INIT_CHANNEL, kTraceError,
"Channel::Init() modules not registered");
return -1;
}
// --- ACM initialization
if ((_audioCodingModule.InitializeReceiver() == -1) ||
#ifdef WEBRTC_CODEC_AVT
// out-of-band Dtmf tones are played out by default
(_audioCodingModule.SetDtmfPlayoutStatus(true) == -1) ||
#endif
(_audioCodingModule.InitializeSender() == -1))
{
_engineStatisticsPtr->SetLastError(
VE_AUDIO_CODING_MODULE_ERROR, kTraceError,
"Channel::Init() unable to initialize the ACM - 1");
return -1;
}
// --- RTP/RTCP module initialization
// Ensure that RTCP is enabled by default for the created channel.
// Note that, the module will keep generating RTCP until it is explicitly
// disabled by the user.
// After StopListen (when no sockets exists), RTCP packets will no longer
// be transmitted since the Transport object will then be invalid.
const bool rtpRtcpFail =
((_rtpRtcpModule.InitReceiver() == -1) ||
(_rtpRtcpModule.InitSender() == -1) ||
(_rtpRtcpModule.SetTelephoneEventStatus(false, true, true) == -1) ||
// RTCP is enabled by default
(_rtpRtcpModule.SetRTCPStatus(kRtcpCompound) == -1));
if (rtpRtcpFail)
{
_engineStatisticsPtr->SetLastError(
VE_RTP_RTCP_MODULE_ERROR, kTraceError,
"Channel::Init() RTP/RTCP module not initialized");
return -1;
}
// --- Register all permanent callbacks
const bool fail =
(_rtpRtcpModule.RegisterIncomingDataCallback(this) == -1) ||
(_rtpRtcpModule.RegisterIncomingRTPCallback(this) == -1) ||
(_rtpRtcpModule.RegisterIncomingRTCPCallback(this) == -1) ||
(_rtpRtcpModule.RegisterSendTransport(this) == -1) ||
(_rtpRtcpModule.RegisterAudioCallback(this) == -1) ||
(_audioCodingModule.RegisterTransportCallback(this) == -1) ||
(_audioCodingModule.RegisterVADCallback(this) == -1);
if (fail)
{
_engineStatisticsPtr->SetLastError(
VE_CANNOT_INIT_CHANNEL, kTraceError,
"Channel::Init() callbacks not registered");
return -1;
}
// --- Register all supported codecs to the receiving side of the
// RTP/RTCP module
CodecInst codec;
const WebRtc_UWord8 nSupportedCodecs = AudioCodingModule::NumberOfCodecs();
for (int idx = 0; idx < nSupportedCodecs; idx++)
{
// Open up the RTP/RTCP receiver for all supported codecs
if ((_audioCodingModule.Codec(idx, codec) == -1) ||
(_rtpRtcpModule.RegisterReceivePayload(codec) == -1))
{
WEBRTC_TRACE(kTraceWarning, kTraceVoice,
VoEId(_instanceId,_channelId),
"Channel::Init() unable to register %s (%d/%d/%d/%d) "
"to RTP/RTCP receiver",
codec.plname, codec.pltype, codec.plfreq,
codec.channels, codec.rate);
}
else
{
WEBRTC_TRACE(kTraceInfo, kTraceVoice,
VoEId(_instanceId,_channelId),
"Channel::Init() %s (%d/%d/%d/%d) has been added to "
"the RTP/RTCP receiver",
codec.plname, codec.pltype, codec.plfreq,
codec.channels, codec.rate);
}
// Ensure that PCMU is used as default codec on the sending side
if (!STR_CASE_CMP(codec.plname, "PCMU"))
{
SetSendCodec(codec);
}
// Register default PT for outband 'telephone-event'
if (!STR_CASE_CMP(codec.plname, "telephone-event"))
{
if ((_rtpRtcpModule.RegisterSendPayload(codec) == -1) ||
(_audioCodingModule.RegisterReceiveCodec(codec) == -1))
{
WEBRTC_TRACE(kTraceWarning, kTraceVoice,
VoEId(_instanceId,_channelId),
"Channel::Init() failed to register outband "
"'telephone-event' (%d/%d) correctly",
codec.pltype, codec.plfreq);
}
}
if (!STR_CASE_CMP(codec.plname, "CN"))
{
if ((_audioCodingModule.RegisterSendCodec(codec) == -1) ||
(_audioCodingModule.RegisterReceiveCodec(codec) == -1) ||
(_rtpRtcpModule.RegisterSendPayload(codec) == -1))
{
WEBRTC_TRACE(kTraceWarning, kTraceVoice,
VoEId(_instanceId,_channelId),
"Channel::Init() failed to register CN (%d/%d) "
"correctly - 1",
codec.pltype, codec.plfreq);
}
}
#ifdef WEBRTC_CODEC_RED
// Register RED to the receiving side of the ACM.
// We will not receive an OnInitializeDecoder() callback for RED.
if (!STR_CASE_CMP(codec.plname, "RED"))
{
if (_audioCodingModule.RegisterReceiveCodec(codec) == -1)
{
WEBRTC_TRACE(kTraceWarning, kTraceVoice,
VoEId(_instanceId,_channelId),
"Channel::Init() failed to register RED (%d/%d) "
"correctly",
codec.pltype, codec.plfreq);
}
}
#endif
}
#ifndef WEBRTC_EXTERNAL_TRANSPORT
// Ensure that the WebRtcSocketTransport implementation is used as
// Transport on the sending side
{
// A lock is needed here since users can call
// RegisterExternalTransport() at the same time.
CriticalSectionScoped cs(_callbackCritSect);
_transportPtr = &_socketTransportModule;
}
#endif
// Initialize the far end AP module
// Using 8 kHz as initial Fs, the same as in transmission. Might be
// changed at the first receiving audio.
if (_rxAudioProcessingModulePtr == NULL)
{
_engineStatisticsPtr->SetLastError(
VE_NO_MEMORY, kTraceCritical,
"Channel::Init() failed to create the far-end AudioProcessing"
" module");
return -1;
}
if (_rxAudioProcessingModulePtr->set_sample_rate_hz(8000))
{
_engineStatisticsPtr->SetLastError(
VE_APM_ERROR, kTraceWarning,
"Channel::Init() failed to set the sample rate to 8K for"
" far-end AP module");
}
if (_rxAudioProcessingModulePtr->set_num_channels(1, 1) != 0)
{
_engineStatisticsPtr->SetLastError(
VE_SOUNDCARD_ERROR, kTraceWarning,
"Init() failed to set channels for the primary audio stream");
}
if (_rxAudioProcessingModulePtr->high_pass_filter()->Enable(
WEBRTC_VOICE_ENGINE_RX_HP_DEFAULT_STATE) != 0)
{
_engineStatisticsPtr->SetLastError(
VE_APM_ERROR, kTraceWarning,
"Channel::Init() failed to set the high-pass filter for"
" far-end AP module");
}
if (_rxAudioProcessingModulePtr->noise_suppression()->set_level(
(NoiseSuppression::Level)WEBRTC_VOICE_ENGINE_RX_NS_DEFAULT_MODE) != 0)
{
_engineStatisticsPtr->SetLastError(
VE_APM_ERROR, kTraceWarning,
"Init() failed to set noise reduction level for far-end"
" AP module");
}
if (_rxAudioProcessingModulePtr->noise_suppression()->Enable(
WEBRTC_VOICE_ENGINE_RX_NS_DEFAULT_STATE) != 0)
{
_engineStatisticsPtr->SetLastError(
VE_APM_ERROR, kTraceWarning,
"Init() failed to set noise reduction state for far-end"
" AP module");
}
if (_rxAudioProcessingModulePtr->gain_control()->set_mode(
(GainControl::Mode)WEBRTC_VOICE_ENGINE_RX_AGC_DEFAULT_MODE) != 0)
{
_engineStatisticsPtr->SetLastError(
VE_APM_ERROR, kTraceWarning,
"Init() failed to set AGC mode for far-end AP module");
}
if (_rxAudioProcessingModulePtr->gain_control()->Enable(
WEBRTC_VOICE_ENGINE_RX_AGC_DEFAULT_STATE) != 0)
{
_engineStatisticsPtr->SetLastError(
VE_APM_ERROR, kTraceWarning,
"Init() failed to set AGC state for far-end AP module");
}
return 0;
}
WebRtc_Word32
Channel::SetEngineInformation(Statistics& engineStatistics,
OutputMixer& outputMixer,
voe::TransmitMixer& transmitMixer,
ProcessThread& moduleProcessThread,
AudioDeviceModule& audioDeviceModule,
VoiceEngineObserver* voiceEngineObserver,
CriticalSectionWrapper* callbackCritSect)
{
WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId,_channelId),
"Channel::SetEngineInformation()");
_engineStatisticsPtr = &engineStatistics;
_outputMixerPtr = &outputMixer;
_transmitMixerPtr = &transmitMixer,
_moduleProcessThreadPtr = &moduleProcessThread;
_audioDeviceModulePtr = &audioDeviceModule;
_voiceEngineObserverPtr = voiceEngineObserver;
_callbackCritSectPtr = callbackCritSect;
return 0;
}
WebRtc_Word32
Channel::UpdateLocalTimeStamp()
{
_timeStamp += _audioFrame._payloadDataLengthInSamples;
return 0;
}
WebRtc_Word32
Channel::StartPlayout()
{
WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId,_channelId),
"Channel::StartPlayout()");
if (_playing)
{
return 0;
}
// Add participant as candidates for mixing.
if (_outputMixerPtr->SetMixabilityStatus(*this, true) != 0)
{
_engineStatisticsPtr->SetLastError(
VE_AUDIO_CONF_MIX_MODULE_ERROR, kTraceError,
"StartPlayout() failed to add participant to mixer");
return -1;
}
_playing = true;
return 0;
}
WebRtc_Word32
Channel::StopPlayout()
{
WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId,_channelId),
"Channel::StopPlayout()");
if (!_playing)
{
return 0;
}
// Remove participant as candidates for mixing
if (_outputMixerPtr->SetMixabilityStatus(*this, false) != 0)
{
_engineStatisticsPtr->SetLastError(
VE_AUDIO_CONF_MIX_MODULE_ERROR, kTraceError,
"StartPlayout() failed to remove participant from mixer");
return -1;
}
_playing = false;
_outputAudioLevel.Clear();
return 0;
}
WebRtc_Word32
Channel::StartSend()
{
WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId,_channelId),
"Channel::StartSend()");
{
// A lock is needed because |_sending| can be accessed or modified by
// another thread at the same time.
CriticalSectionScoped cs(_callbackCritSect);
if (_sending)
{
return 0;
}
_sending = true;
}
if (_rtpRtcpModule.SetSendingStatus(true) != 0)
{
_engineStatisticsPtr->SetLastError(
VE_RTP_RTCP_MODULE_ERROR, kTraceError,
"StartSend() RTP/RTCP failed to start sending");
CriticalSectionScoped cs(_callbackCritSect);
_sending = false;
return -1;
}
return 0;
}
WebRtc_Word32
Channel::StopSend()
{
WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId,_channelId),
"Channel::StopSend()");
{
// A lock is needed because |_sending| can be accessed or modified by
// another thread at the same time.
CriticalSectionScoped cs(_callbackCritSect);
if (!_sending)
{
return 0;
}
_sending = false;
}
// Reset sending SSRC and sequence number and triggers direct transmission
// of RTCP BYE
if (_rtpRtcpModule.SetSendingStatus(false) == -1 ||
_rtpRtcpModule.ResetSendDataCountersRTP() == -1)
{
_engineStatisticsPtr->SetLastError(
VE_RTP_RTCP_MODULE_ERROR, kTraceWarning,
"StartSend() RTP/RTCP failed to stop sending");
}
return 0;
}
WebRtc_Word32
Channel::StartReceiving()
{
WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId,_channelId),
"Channel::StartReceiving()");
if (_receiving)
{
return 0;
}
// If external transport is used, we will only initialize/set the variables
// after this section, since we are not using the WebRtc transport but
// still need to keep track of e.g. if we are receiving.
#ifndef WEBRTC_EXTERNAL_TRANSPORT
if (!_externalTransport)
{
if (!_socketTransportModule.ReceiveSocketsInitialized())
{
_engineStatisticsPtr->SetLastError(
VE_SOCKETS_NOT_INITED, kTraceError,
"StartReceive() must set local receiver first");
return -1;
}
if (_socketTransportModule.StartReceiving(KNumberOfSocketBuffers) != 0)
{
_engineStatisticsPtr->SetLastError(
VE_SOCKET_TRANSPORT_MODULE_ERROR, kTraceError,
"StartReceiving() failed to start receiving");
return -1;
}
}
#endif
_receiving = true;
_numberOfDiscardedPackets = 0;
return 0;
}
WebRtc_Word32
Channel::StopReceiving()
{
WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId,_channelId),
"Channel::StopReceiving()");
if (!_receiving)
{
return 0;
}
#ifndef WEBRTC_EXTERNAL_TRANSPORT
if (!_externalTransport &&
_socketTransportModule.ReceiveSocketsInitialized())
{
if (_socketTransportModule.StopReceiving() != 0)
{
_engineStatisticsPtr->SetLastError(
VE_SOCKET_TRANSPORT_MODULE_ERROR, kTraceError,
"StopReceiving() failed to stop receiving.");
return -1;
}
}
#endif
bool dtmfDetection = _rtpRtcpModule.TelephoneEvent();
WebRtc_Word32 ret = _rtpRtcpModule.InitReceiver();
if (ret != 0) {
_engineStatisticsPtr->SetLastError(
VE_RTP_RTCP_MODULE_ERROR, kTraceError,
"StopReceiving() failed to reinitialize the RTP receiver.");
return -1;
}
// Recover DTMF detection status.
ret = _rtpRtcpModule.SetTelephoneEventStatus(dtmfDetection, true, true);
if (ret != 0) {
_engineStatisticsPtr->SetLastError(
VE_INVALID_OPERATION, kTraceWarning,
"StopReceiving() failed to restore telephone-event status.");
}
RegisterReceiveCodecsToRTPModule();
_receiving = false;
return 0;
}
#ifndef WEBRTC_EXTERNAL_TRANSPORT
WebRtc_Word32
Channel::SetLocalReceiver(const WebRtc_UWord16 rtpPort,
const WebRtc_UWord16 rtcpPort,
const WebRtc_Word8 ipAddr[64],
const WebRtc_Word8 multicastIpAddr[64])
{
WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId,_channelId),
"Channel::SetLocalReceiver()");
if (_externalTransport)
{
_engineStatisticsPtr->SetLastError(
VE_EXTERNAL_TRANSPORT_ENABLED, kTraceError,
"SetLocalReceiver() conflict with external transport");
return -1;
}
if (_sending)
{
_engineStatisticsPtr->SetLastError(
VE_ALREADY_SENDING, kTraceError,
"SetLocalReceiver() already sending");
return -1;
}
if (_receiving)
{
_engineStatisticsPtr->SetLastError(
VE_ALREADY_LISTENING, kTraceError,
"SetLocalReceiver() already receiving");
return -1;
}
if (_socketTransportModule.InitializeReceiveSockets(this,
rtpPort,
ipAddr,
multicastIpAddr,
rtcpPort) != 0)
{
UdpTransport::ErrorCode lastSockError(
_socketTransportModule.LastError());
switch (lastSockError)
{
case UdpTransport::kIpAddressInvalid:
_engineStatisticsPtr->SetLastError(
VE_INVALID_IP_ADDRESS, kTraceError,
"SetLocalReceiver() invalid IP address");
break;
case UdpTransport::kSocketInvalid:
_engineStatisticsPtr->SetLastError(
VE_SOCKET_ERROR, kTraceError,
"SetLocalReceiver() invalid socket");
break;
case UdpTransport::kPortInvalid:
_engineStatisticsPtr->SetLastError(
VE_INVALID_PORT_NMBR, kTraceError,
"SetLocalReceiver() invalid port");
break;
case UdpTransport::kFailedToBindPort:
_engineStatisticsPtr->SetLastError(
VE_BINDING_SOCKET_TO_LOCAL_ADDRESS_FAILED, kTraceError,
"SetLocalReceiver() binding failed");
break;
default:
_engineStatisticsPtr->SetLastError(
VE_SOCKET_ERROR, kTraceError,
"SetLocalReceiver() undefined socket error");
break;
}
return -1;
}
return 0;
}
#endif
#ifndef WEBRTC_EXTERNAL_TRANSPORT
WebRtc_Word32
Channel::GetLocalReceiver(int& port, int& RTCPport, char ipAddr[64])
{
WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId,_channelId),
"Channel::GetLocalReceiver()");
if (_externalTransport)
{
_engineStatisticsPtr->SetLastError(
VE_EXTERNAL_TRANSPORT_ENABLED, kTraceError,
"SetLocalReceiver() conflict with external transport");
return -1;
}
WebRtc_Word8 ipAddrTmp[UdpTransport::
kIpAddressVersion6Length] = {0};
WebRtc_UWord16 rtpPort(0);
WebRtc_UWord16 rtcpPort(0);
WebRtc_Word8 multicastIpAddr[UdpTransport::
kIpAddressVersion6Length] = {0};
// Acquire socket information from the socket module
if (_socketTransportModule.ReceiveSocketInformation(ipAddrTmp,
rtpPort,
rtcpPort,
multicastIpAddr) != 0)
{
_engineStatisticsPtr->SetLastError(
VE_CANNOT_GET_SOCKET_INFO, kTraceError,
"GetLocalReceiver() unable to retrieve socket information");
return -1;
}
// Deliver valid results to the user
port = static_cast<int> (rtpPort);
RTCPport = static_cast<int> (rtcpPort);
if (ipAddr != NULL)
{
strcpy(ipAddr, ipAddrTmp);
}
return 0;
}
#endif
#ifndef WEBRTC_EXTERNAL_TRANSPORT
WebRtc_Word32
Channel::SetSendDestination(const WebRtc_UWord16 rtpPort,
const WebRtc_Word8 ipAddr[64],
const int sourcePort,
const WebRtc_UWord16 rtcpPort)
{
WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId,_channelId),
"Channel::SetSendDestination()");
if (_externalTransport)
{
_engineStatisticsPtr->SetLastError(
VE_EXTERNAL_TRANSPORT_ENABLED, kTraceError,
"SetSendDestination() conflict with external transport");
return -1;
}
// Initialize ports and IP address for the remote (destination) side.
// By default, the sockets used for receiving are used for transmission as
// well, hence the source ports for outgoing packets are the same as the
// receiving ports specified in SetLocalReceiver.
// If an extra send socket has been created, it will be utilized until a
// new source port is specified or until the channel has been deleted and
// recreated. If no socket exists, sockets will be created when the first
// RTP and RTCP packets shall be transmitted (see e.g.
// UdpTransportImpl::SendPacket()).
//
// NOTE: this function does not require that sockets exists; all it does is
// to build send structures to be used with the sockets when they exist.
// It is therefore possible to call this method before SetLocalReceiver.
// However, sockets must exist if a multi-cast address is given as input.
// Build send structures and enable QoS (if enabled and supported)
if (_socketTransportModule.InitializeSendSockets(
ipAddr, rtpPort, rtcpPort) != UdpTransport::kNoSocketError)
{
UdpTransport::ErrorCode lastSockError(
_socketTransportModule.LastError());
switch (lastSockError)
{
case UdpTransport::kIpAddressInvalid:
_engineStatisticsPtr->SetLastError(
VE_INVALID_IP_ADDRESS, kTraceError,
"SetSendDestination() invalid IP address 1");
break;
case UdpTransport::kSocketInvalid:
_engineStatisticsPtr->SetLastError(
VE_SOCKET_ERROR, kTraceError,
"SetSendDestination() invalid socket 1");
break;
case UdpTransport::kQosError:
_engineStatisticsPtr->SetLastError(
VE_GQOS_ERROR, kTraceError,
"SetSendDestination() failed to set QoS");
break;
case UdpTransport::kMulticastAddressInvalid:
_engineStatisticsPtr->SetLastError(
VE_INVALID_MULTICAST_ADDRESS, kTraceError,
"SetSendDestination() invalid multicast address");
break;
default:
_engineStatisticsPtr->SetLastError(
VE_SOCKET_ERROR, kTraceError,
"SetSendDestination() undefined socket error 1");
break;
}
return -1;
}
// Check if the user has specified a non-default source port different from
// the local receive port.
// If so, an extra local socket will be created unless the source port is
// not unique.
if (sourcePort != kVoEDefault)
{
WebRtc_UWord16 receiverRtpPort(0);
WebRtc_UWord16 rtcpNA(0);
if (_socketTransportModule.ReceiveSocketInformation(NULL,
receiverRtpPort,
rtcpNA,
NULL) != 0)
{
_engineStatisticsPtr->SetLastError(
VE_CANNOT_GET_SOCKET_INFO, kTraceError,
"SetSendDestination() failed to retrieve socket information");
return -1;
}
WebRtc_UWord16 sourcePortUW16 =
static_cast<WebRtc_UWord16> (sourcePort);
// An extra socket will only be created if the specified source port
// differs from the local receive port.
if (sourcePortUW16 != receiverRtpPort)
{
// Initialize extra local socket to get a different source port
// than the local
// receiver port. Always use default source for RTCP.
// Note that, this calls UdpTransport::CloseSendSockets().
if (_socketTransportModule.InitializeSourcePorts(
sourcePortUW16,
sourcePortUW16+1) != 0)
{
UdpTransport::ErrorCode lastSockError(
_socketTransportModule.LastError());
switch (lastSockError)
{
case UdpTransport::kIpAddressInvalid:
_engineStatisticsPtr->SetLastError(
VE_INVALID_IP_ADDRESS, kTraceError,
"SetSendDestination() invalid IP address 2");
break;
case UdpTransport::kSocketInvalid:
_engineStatisticsPtr->SetLastError(
VE_SOCKET_ERROR, kTraceError,
"SetSendDestination() invalid socket 2");
break;
default:
_engineStatisticsPtr->SetLastError(
VE_SOCKET_ERROR, kTraceError,
"SetSendDestination() undefined socket error 2");
break;
}
return -1;
}
WEBRTC_TRACE(kTraceInfo, kTraceVoice,
VoEId(_instanceId,_channelId),
"SetSendDestination() extra local socket is created"
" to facilitate unique source port");
}
else
{
WEBRTC_TRACE(kTraceInfo, kTraceVoice,
VoEId(_instanceId,_channelId),
"SetSendDestination() sourcePort equals the local"
" receive port => no extra socket is created");
}
}
return 0;
}
#endif
#ifndef WEBRTC_EXTERNAL_TRANSPORT
WebRtc_Word32
Channel::GetSendDestination(int& port,
char ipAddr[64],
int& sourcePort,
int& RTCPport)
{
WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId,_channelId),
"Channel::GetSendDestination()");
if (_externalTransport)
{
_engineStatisticsPtr->SetLastError(
VE_EXTERNAL_TRANSPORT_ENABLED, kTraceError,
"GetSendDestination() conflict with external transport");
return -1;
}
WebRtc_Word8 ipAddrTmp[UdpTransport::kIpAddressVersion6Length] = {0};
WebRtc_UWord16 rtpPort(0);
WebRtc_UWord16 rtcpPort(0);
WebRtc_UWord16 rtpSourcePort(0);
WebRtc_UWord16 rtcpSourcePort(0);
// Acquire sending socket information from the socket module
_socketTransportModule.SendSocketInformation(ipAddrTmp, rtpPort, rtcpPort);
_socketTransportModule.SourcePorts(rtpSourcePort, rtcpSourcePort);
// Deliver valid results to the user
port = static_cast<int> (rtpPort);
RTCPport = static_cast<int> (rtcpPort);
sourcePort = static_cast<int> (rtpSourcePort);
if (ipAddr != NULL)
{
strcpy(ipAddr, ipAddrTmp);
}
return 0;
}
#endif
WebRtc_Word32
Channel::SetNetEQPlayoutMode(NetEqModes mode)
{
WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId,_channelId),
"Channel::SetNetEQPlayoutMode()");
AudioPlayoutMode playoutMode(voice);
switch (mode)
{
case kNetEqDefault:
playoutMode = voice;
break;
case kNetEqStreaming:
playoutMode = streaming;
break;
case kNetEqFax:
playoutMode = fax;
break;
}
if (_audioCodingModule.SetPlayoutMode(playoutMode) != 0)
{
_engineStatisticsPtr->SetLastError(
VE_AUDIO_CODING_MODULE_ERROR, kTraceError,
"SetNetEQPlayoutMode() failed to set playout mode");
return -1;
}
return 0;
}
WebRtc_Word32
Channel::GetNetEQPlayoutMode(NetEqModes& mode)
{
const AudioPlayoutMode playoutMode = _audioCodingModule.PlayoutMode();
switch (playoutMode)
{
case voice:
mode = kNetEqDefault;
break;
case streaming:
mode = kNetEqStreaming;
break;
case fax:
mode = kNetEqFax;
break;
}
WEBRTC_TRACE(kTraceStateInfo, kTraceVoice,
VoEId(_instanceId,_channelId),
"Channel::GetNetEQPlayoutMode() => mode=%u", mode);
return 0;
}
WebRtc_Word32
Channel::SetNetEQBGNMode(NetEqBgnModes mode)
{
WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId,_channelId),
"Channel::SetNetEQPlayoutMode()");
ACMBackgroundNoiseMode noiseMode(On);
switch (mode)
{
case kBgnOn:
noiseMode = On;
break;
case kBgnFade:
noiseMode = Fade;
break;
case kBgnOff:
noiseMode = Off;
break;
}
if (_audioCodingModule.SetBackgroundNoiseMode(noiseMode) != 0)
{
_engineStatisticsPtr->SetLastError(
VE_AUDIO_CODING_MODULE_ERROR, kTraceError,
"SetBackgroundNoiseMode() failed to set noise mode");
return -1;
}
return 0;
}
WebRtc_Word32
Channel::SetOnHoldStatus(bool enable, OnHoldModes mode)
{
WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId,_channelId),
"Channel::SetOnHoldStatus()");
if (mode == kHoldSendAndPlay)
{
_outputIsOnHold = enable;
_inputIsOnHold = enable;
}
else if (mode == kHoldPlayOnly)
{
_outputIsOnHold = enable;
}
if (mode == kHoldSendOnly)
{
_inputIsOnHold = enable;
}
return 0;
}
WebRtc_Word32
Channel::GetOnHoldStatus(bool& enabled, OnHoldModes& mode)
{
WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId,_channelId),
"Channel::GetOnHoldStatus()");
enabled = (_outputIsOnHold || _inputIsOnHold);
if (_outputIsOnHold && _inputIsOnHold)
{
mode = kHoldSendAndPlay;
}
else if (_outputIsOnHold && !_inputIsOnHold)
{
mode = kHoldPlayOnly;
}
else if (!_outputIsOnHold && _inputIsOnHold)
{
mode = kHoldSendOnly;
}
WEBRTC_TRACE(kTraceStateInfo, kTraceVoice, VoEId(_instanceId,_channelId),
"Channel::GetOnHoldStatus() => enabled=%d, mode=%d",
enabled, mode);
return 0;
}
WebRtc_Word32
Channel::RegisterVoiceEngineObserver(VoiceEngineObserver& observer)
{
WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId,_channelId),
"Channel::RegisterVoiceEngineObserver()");
CriticalSectionScoped cs(_callbackCritSect);
if (_voiceEngineObserverPtr)
{
_engineStatisticsPtr->SetLastError(
VE_INVALID_OPERATION, kTraceError,
"RegisterVoiceEngineObserver() observer already enabled");
return -1;
}
_voiceEngineObserverPtr = &observer;
return 0;
}
WebRtc_Word32
Channel::DeRegisterVoiceEngineObserver()
{
WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId,_channelId),
"Channel::DeRegisterVoiceEngineObserver()");
CriticalSectionScoped cs(_callbackCritSect);
if (!_voiceEngineObserverPtr)
{
_engineStatisticsPtr->SetLastError(
VE_INVALID_OPERATION, kTraceWarning,
"DeRegisterVoiceEngineObserver() observer already disabled");
return 0;
}
_voiceEngineObserverPtr = NULL;
return 0;
}
WebRtc_Word32
Channel::GetNetEQBGNMode(NetEqBgnModes& mode)
{
ACMBackgroundNoiseMode noiseMode(On);
_audioCodingModule.BackgroundNoiseMode(noiseMode);
switch (noiseMode)
{
case On:
mode = kBgnOn;
break;
case Fade:
mode = kBgnFade;
break;
case Off:
mode = kBgnOff;
break;
}
WEBRTC_TRACE(kTraceStateInfo, kTraceVoice, VoEId(_instanceId,_channelId),
"Channel::GetNetEQBGNMode() => mode=%u", mode);
return 0;
}
WebRtc_Word32
Channel::GetSendCodec(CodecInst& codec)
{
return (_audioCodingModule.SendCodec(codec));
}
WebRtc_Word32
Channel::GetRecCodec(CodecInst& codec)
{
return (_audioCodingModule.ReceiveCodec(codec));
}
WebRtc_Word32
Channel::SetSendCodec(const CodecInst& codec)
{
WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId,_channelId),
"Channel::SetSendCodec()");
if (_audioCodingModule.RegisterSendCodec(codec) != 0)
{
WEBRTC_TRACE(kTraceError, kTraceVoice, VoEId(_instanceId,_channelId),
"SetSendCodec() failed to register codec to ACM");
return -1;
}
if (_rtpRtcpModule.RegisterSendPayload(codec) != 0)
{
_rtpRtcpModule.DeRegisterSendPayload(codec.pltype);
if (_rtpRtcpModule.RegisterSendPayload(codec) != 0)
{
WEBRTC_TRACE(
kTraceError, kTraceVoice, VoEId(_instanceId,_channelId),
"SetSendCodec() failed to register codec to"
" RTP/RTCP module");
return -1;
}
}
if (_rtpRtcpModule.SetAudioPacketSize(codec.pacsize) != 0)
{
WEBRTC_TRACE(kTraceError, kTraceVoice, VoEId(_instanceId,_channelId),
"SetSendCodec() failed to set audio packet size");
return -1;
}
return 0;
}
WebRtc_Word32
Channel::SetVADStatus(bool enableVAD, ACMVADMode mode, bool disableDTX)
{
WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId,_channelId),
"Channel::SetVADStatus(mode=%d)", mode);
// To disable VAD, DTX must be disabled too
disableDTX = ((enableVAD == false) ? true : disableDTX);
if (_audioCodingModule.SetVAD(!disableDTX, enableVAD, mode) != 0)
{
_engineStatisticsPtr->SetLastError(
VE_AUDIO_CODING_MODULE_ERROR, kTraceError,
"SetVADStatus() failed to set VAD");
return -1;
}
return 0;
}
WebRtc_Word32
Channel::GetVADStatus(bool& enabledVAD, ACMVADMode& mode, bool& disabledDTX)
{
WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId,_channelId),
"Channel::GetVADStatus");
if (_audioCodingModule.VAD(disabledDTX, enabledVAD, mode) != 0)
{
_engineStatisticsPtr->SetLastError(
VE_AUDIO_CODING_MODULE_ERROR, kTraceError,
"GetVADStatus() failed to get VAD status");
return -1;
}
disabledDTX = !disabledDTX;
return 0;
}
WebRtc_Word32
Channel::SetRecPayloadType(const CodecInst& codec)
{
WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId,_channelId),
"Channel::SetRecPayloadType()");
if (_playing)
{
_engineStatisticsPtr->SetLastError(
VE_ALREADY_PLAYING, kTraceError,
"SetRecPayloadType() unable to set PT while playing");
return -1;
}
if (_receiving)
{
_engineStatisticsPtr->SetLastError(
VE_ALREADY_LISTENING, kTraceError,
"SetRecPayloadType() unable to set PT while listening");
return -1;
}
if (codec.pltype == -1)
{
// De-register the selected codec (RTP/RTCP module and ACM)
WebRtc_Word8 pltype(-1);
CodecInst rxCodec = codec;
// Get payload type for the given codec
_rtpRtcpModule.ReceivePayloadType(rxCodec, &pltype);
rxCodec.pltype = pltype;
if (_rtpRtcpModule.DeRegisterReceivePayload(pltype) != 0)
{
_engineStatisticsPtr->SetLastError(
VE_RTP_RTCP_MODULE_ERROR,
kTraceError,
"SetRecPayloadType() RTP/RTCP-module deregistration "
"failed");
return -1;
}
if (_audioCodingModule.UnregisterReceiveCodec(rxCodec.pltype) != 0)
{
_engineStatisticsPtr->SetLastError(
VE_AUDIO_CODING_MODULE_ERROR, kTraceError,
"SetRecPayloadType() ACM deregistration failed - 1");
return -1;
}
return 0;
}
if (_rtpRtcpModule.RegisterReceivePayload(codec) != 0)
{
// First attempt to register failed => de-register and try again
_rtpRtcpModule.DeRegisterReceivePayload(codec.pltype);
if (_rtpRtcpModule.RegisterReceivePayload(codec) != 0)
{
_engineStatisticsPtr->SetLastError(
VE_RTP_RTCP_MODULE_ERROR, kTraceError,
"SetRecPayloadType() RTP/RTCP-module registration failed");
return -1;
}
}
if (_audioCodingModule.RegisterReceiveCodec(codec) != 0)
{
_audioCodingModule.UnregisterReceiveCodec(codec.pltype);
if (_audioCodingModule.RegisterReceiveCodec(codec) != 0)
{
_engineStatisticsPtr->SetLastError(
VE_AUDIO_CODING_MODULE_ERROR, kTraceError,
"SetRecPayloadType() ACM registration failed - 1");
return -1;
}
}
return 0;
}
WebRtc_Word32
Channel::GetRecPayloadType(CodecInst& codec)
{
WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId,_channelId),
"Channel::GetRecPayloadType()");
WebRtc_Word8 payloadType(-1);
if (_rtpRtcpModule.ReceivePayloadType(codec, &payloadType) != 0)
{
_engineStatisticsPtr->SetLastError(
VE_RTP_RTCP_MODULE_ERROR, kTraceError,
"GetRecPayloadType() failed to retrieve RX payload type");
return -1;
}
codec.pltype = payloadType;
WEBRTC_TRACE(kTraceStateInfo, kTraceVoice, VoEId(_instanceId,_channelId),
"Channel::GetRecPayloadType() => pltype=%u", codec.pltype);
return 0;
}
WebRtc_Word32
Channel::SetAMREncFormat(AmrMode mode)
{
WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId,_channelId),
"Channel::SetAMREncFormat()");
// ACM doesn't support AMR
return -1;
}
WebRtc_Word32
Channel::SetAMRDecFormat(AmrMode mode)
{
WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId,_channelId),
"Channel::SetAMRDecFormat()");
// ACM doesn't support AMR
return -1;
}
WebRtc_Word32
Channel::SetAMRWbEncFormat(AmrMode mode)
{
WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId,_channelId),
"Channel::SetAMRWbEncFormat()");
// ACM doesn't support AMR
return -1;
}
WebRtc_Word32
Channel::SetAMRWbDecFormat(AmrMode mode)
{
WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId,_channelId),
"Channel::SetAMRWbDecFormat()");
// ACM doesn't support AMR
return -1;
}
WebRtc_Word32
Channel::SetSendCNPayloadType(int type, PayloadFrequencies frequency)
{
WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId,_channelId),
"Channel::SetSendCNPayloadType()");
CodecInst codec;
WebRtc_Word32 samplingFreqHz(-1);
if (frequency == kFreq32000Hz)
samplingFreqHz = 32000;
else if (frequency == kFreq16000Hz)
samplingFreqHz = 16000;
if (_audioCodingModule.Codec("CN", codec, samplingFreqHz) == -1)
{
_engineStatisticsPtr->SetLastError(
VE_AUDIO_CODING_MODULE_ERROR, kTraceError,
"SetSendCNPayloadType() failed to retrieve default CN codec "
"settings");
return -1;
}
// Modify the payload type (must be set to dynamic range)
codec.pltype = type;
if (_audioCodingModule.RegisterSendCodec(codec) != 0)
{
_engineStatisticsPtr->SetLastError(
VE_AUDIO_CODING_MODULE_ERROR, kTraceError,
"SetSendCNPayloadType() failed to register CN to ACM");
return -1;
}
if (_rtpRtcpModule.RegisterSendPayload(codec) != 0)
{
_rtpRtcpModule.DeRegisterSendPayload(codec.pltype);
if (_rtpRtcpModule.RegisterSendPayload(codec) != 0)
{
_engineStatisticsPtr->SetLastError(
VE_RTP_RTCP_MODULE_ERROR, kTraceError,
"SetSendCNPayloadType() failed to register CN to RTP/RTCP "
"module");
return -1;
}
}
return 0;
}
WebRtc_Word32
Channel::SetISACInitTargetRate(int rateBps, bool useFixedFrameSize)
{
WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId,_channelId),
"Channel::SetISACInitTargetRate()");
CodecInst sendCodec;
if (_audioCodingModule.SendCodec(sendCodec) == -1)
{
_engineStatisticsPtr->SetLastError(
VE_CODEC_ERROR, kTraceError,
"SetISACInitTargetRate() failed to retrieve send codec");
return -1;
}
if (STR_CASE_CMP(sendCodec.plname, "ISAC") != 0)
{
// This API is only valid if iSAC is setup to run in channel-adaptive
// mode.
// We do not validate the adaptive mode here. It is done later in the
// ConfigISACBandwidthEstimator() API.
_engineStatisticsPtr->SetLastError(
VE_CODEC_ERROR, kTraceError,
"SetISACInitTargetRate() send codec is not iSAC");
return -1;
}
WebRtc_UWord8 initFrameSizeMsec(0);
if (16000 == sendCodec.plfreq)
{
// Note that 0 is a valid and corresponds to "use default
if ((rateBps != 0 &&
rateBps < kVoiceEngineMinIsacInitTargetRateBpsWb) ||
(rateBps > kVoiceEngineMaxIsacInitTargetRateBpsWb))
{
_engineStatisticsPtr->SetLastError(
VE_INVALID_ARGUMENT, kTraceError,
"SetISACInitTargetRate() invalid target rate - 1");
return -1;
}
// 30 or 60ms
initFrameSizeMsec = (WebRtc_UWord8)(sendCodec.pacsize / 16);
}
else if (32000 == sendCodec.plfreq)
{
if ((rateBps != 0 &&
rateBps < kVoiceEngineMinIsacInitTargetRateBpsSwb) ||
(rateBps > kVoiceEngineMaxIsacInitTargetRateBpsSwb))
{
_engineStatisticsPtr->SetLastError(
VE_INVALID_ARGUMENT, kTraceError,
"SetISACInitTargetRate() invalid target rate - 2");
return -1;
}
initFrameSizeMsec = (WebRtc_UWord8)(sendCodec.pacsize / 32); // 30ms
}
if (_audioCodingModule.ConfigISACBandwidthEstimator(
initFrameSizeMsec, rateBps, useFixedFrameSize) == -1)
{
_engineStatisticsPtr->SetLastError(
VE_AUDIO_CODING_MODULE_ERROR, kTraceError,
"SetISACInitTargetRate() iSAC BWE config failed");
return -1;
}
return 0;
}
WebRtc_Word32
Channel::SetISACMaxRate(int rateBps)
{
WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId,_channelId),
"Channel::SetISACMaxRate()");
CodecInst sendCodec;
if (_audioCodingModule.SendCodec(sendCodec) == -1)
{
_engineStatisticsPtr->SetLastError(
VE_CODEC_ERROR, kTraceError,
"SetISACMaxRate() failed to retrieve send codec");
return -1;
}
if (STR_CASE_CMP(sendCodec.plname, "ISAC") != 0)
{
// This API is only valid if iSAC is selected as sending codec.
_engineStatisticsPtr->SetLastError(
VE_CODEC_ERROR, kTraceError,
"SetISACMaxRate() send codec is not iSAC");
return -1;
}
if (16000 == sendCodec.plfreq)
{
if ((rateBps < kVoiceEngineMinIsacMaxRateBpsWb) ||
(rateBps > kVoiceEngineMaxIsacMaxRateBpsWb))
{
_engineStatisticsPtr->SetLastError(
VE_INVALID_ARGUMENT, kTraceError,
"SetISACMaxRate() invalid max rate - 1");
return -1;
}
}
else if (32000 == sendCodec.plfreq)
{
if ((rateBps < kVoiceEngineMinIsacMaxRateBpsSwb) ||
(rateBps > kVoiceEngineMaxIsacMaxRateBpsSwb))
{
_engineStatisticsPtr->SetLastError(
VE_INVALID_ARGUMENT, kTraceError,
"SetISACMaxRate() invalid max rate - 2");
return -1;
}
}
if (_sending)
{
_engineStatisticsPtr->SetLastError(
VE_SENDING, kTraceError,
"SetISACMaxRate() unable to set max rate while sending");
return -1;
}
// Set the maximum instantaneous rate of iSAC (works for both adaptive
// and non-adaptive mode)
if (_audioCodingModule.SetISACMaxRate(rateBps) == -1)
{
_engineStatisticsPtr->SetLastError(
VE_AUDIO_CODING_MODULE_ERROR, kTraceError,
"SetISACMaxRate() failed to set max rate");
return -1;
}
return 0;
}
WebRtc_Word32
Channel::SetISACMaxPayloadSize(int sizeBytes)
{
WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId,_channelId),
"Channel::SetISACMaxPayloadSize()");
CodecInst sendCodec;
if (_audioCodingModule.SendCodec(sendCodec) == -1)
{
_engineStatisticsPtr->SetLastError(
VE_CODEC_ERROR, kTraceError,
"SetISACMaxPayloadSize() failed to retrieve send codec");
return -1;
}
if (STR_CASE_CMP(sendCodec.plname, "ISAC") != 0)
{
_engineStatisticsPtr->SetLastError(
VE_CODEC_ERROR, kTraceError,
"SetISACMaxPayloadSize() send codec is not iSAC");
return -1;
}
if (16000 == sendCodec.plfreq)
{
if ((sizeBytes < kVoiceEngineMinIsacMaxPayloadSizeBytesWb) ||
(sizeBytes > kVoiceEngineMaxIsacMaxPayloadSizeBytesWb))
{
_engineStatisticsPtr->SetLastError(
VE_INVALID_ARGUMENT, kTraceError,
"SetISACMaxPayloadSize() invalid max payload - 1");
return -1;
}
}
else if (32000 == sendCodec.plfreq)
{
if ((sizeBytes < kVoiceEngineMinIsacMaxPayloadSizeBytesSwb) ||
(sizeBytes > kVoiceEngineMaxIsacMaxPayloadSizeBytesSwb))
{
_engineStatisticsPtr->SetLastError(
VE_INVALID_ARGUMENT, kTraceError,
"SetISACMaxPayloadSize() invalid max payload - 2");
return -1;
}
}
if (_sending)
{
_engineStatisticsPtr->SetLastError(
VE_SENDING, kTraceError,
"SetISACMaxPayloadSize() unable to set max rate while sending");
return -1;
}
if (_audioCodingModule.SetISACMaxPayloadSize(sizeBytes) == -1)
{
_engineStatisticsPtr->SetLastError(
VE_AUDIO_CODING_MODULE_ERROR, kTraceError,
"SetISACMaxPayloadSize() failed to set max payload size");
return -1;
}
return 0;
}
WebRtc_Word32 Channel::RegisterExternalTransport(Transport& transport)
{
WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId, _channelId),
"Channel::RegisterExternalTransport()");
CriticalSectionScoped cs(_callbackCritSect);
#ifndef WEBRTC_EXTERNAL_TRANSPORT
// Sanity checks for default (non external transport) to avoid conflict with
// WebRtc sockets.
if (_socketTransportModule.SendSocketsInitialized())
{
_engineStatisticsPtr->SetLastError(VE_SEND_SOCKETS_CONFLICT,
kTraceError,
"RegisterExternalTransport() send sockets already initialized");
return -1;
}
if (_socketTransportModule.ReceiveSocketsInitialized())
{
_engineStatisticsPtr->SetLastError(VE_RECEIVE_SOCKETS_CONFLICT,
kTraceError,
"RegisterExternalTransport() receive sockets already initialized");
return -1;
}
#endif
if (_externalTransport)
{
_engineStatisticsPtr->SetLastError(VE_INVALID_OPERATION,
kTraceError,
"RegisterExternalTransport() external transport already enabled");
return -1;
}
_externalTransport = true;
_transportPtr = &transport;
return 0;
}
WebRtc_Word32
Channel::DeRegisterExternalTransport()
{
WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId,_channelId),
"Channel::DeRegisterExternalTransport()");
CriticalSectionScoped cs(_callbackCritSect);
if (!_transportPtr)
{
_engineStatisticsPtr->SetLastError(
VE_INVALID_OPERATION, kTraceWarning,
"DeRegisterExternalTransport() external transport already "
"disabled");
return 0;
}
_externalTransport = false;
#ifdef WEBRTC_EXTERNAL_TRANSPORT
_transportPtr = NULL;
WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId,_channelId),
"DeRegisterExternalTransport() all transport is disabled");
#else
_transportPtr = &_socketTransportModule;
WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId,_channelId),
"DeRegisterExternalTransport() internal Transport is enabled");
#endif
return 0;
}
WebRtc_Word32
Channel::ReceivedRTPPacket(const WebRtc_Word8* data, WebRtc_Word32 length)
{
WEBRTC_TRACE(kTraceStream, kTraceVoice, VoEId(_instanceId,_channelId),
"Channel::ReceivedRTPPacket()");
const WebRtc_Word8 dummyIP[] = "127.0.0.1";
IncomingRTPPacket(data, length, dummyIP, 0);
return 0;
}
WebRtc_Word32
Channel::ReceivedRTCPPacket(const WebRtc_Word8* data, WebRtc_Word32 length)
{
WEBRTC_TRACE(kTraceStream, kTraceVoice, VoEId(_instanceId,_channelId),
"Channel::ReceivedRTCPPacket()");
const WebRtc_Word8 dummyIP[] = "127.0.0.1";
IncomingRTCPPacket(data, length, dummyIP, 0);
return 0;
}
#ifndef WEBRTC_EXTERNAL_TRANSPORT
WebRtc_Word32
Channel::GetSourceInfo(int& rtpPort, int& rtcpPort, char ipAddr[64])
{
WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId,_channelId),
"Channel::GetSourceInfo()");
WebRtc_UWord16 rtpPortModule;
WebRtc_UWord16 rtcpPortModule;
WebRtc_Word8 ipaddr[UdpTransport::kIpAddressVersion6Length] = {0};
if (_socketTransportModule.RemoteSocketInformation(ipaddr,
rtpPortModule,
rtcpPortModule) != 0)
{
_engineStatisticsPtr->SetLastError(
VE_SOCKET_TRANSPORT_MODULE_ERROR, kTraceError,
"GetSourceInfo() failed to retrieve remote socket information");
return -1;
}
strcpy(ipAddr, ipaddr);
rtpPort = rtpPortModule;
rtcpPort = rtcpPortModule;
WEBRTC_TRACE(kTraceStateInfo, kTraceVoice, VoEId(_instanceId,_channelId),
"GetSourceInfo() => rtpPort=%d, rtcpPort=%d, ipAddr=%s",
rtpPort, rtcpPort, ipAddr);
return 0;
}
WebRtc_Word32
Channel::EnableIPv6()
{
WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId,_channelId),
"Channel::EnableIPv6()");
if (_socketTransportModule.ReceiveSocketsInitialized() ||
_socketTransportModule.SendSocketsInitialized())
{
_engineStatisticsPtr->SetLastError(
VE_INVALID_OPERATION, kTraceError,
"EnableIPv6() socket layer is already initialized");
return -1;
}
if (_socketTransportModule.EnableIpV6() != 0)
{
_engineStatisticsPtr->SetLastError(
VE_SOCKET_ERROR, kTraceError,
"EnableIPv6() failed to enable IPv6");
const UdpTransport::ErrorCode lastError =
_socketTransportModule.LastError();
WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId,_channelId),
"UdpTransport::LastError() => %d", lastError);
return -1;
}
return 0;
}
bool
Channel::IPv6IsEnabled() const
{
bool isEnabled = _socketTransportModule.IpV6Enabled();
WEBRTC_TRACE(kTraceStateInfo, kTraceVoice, VoEId(_instanceId,_channelId),
"IPv6IsEnabled() => %d", isEnabled);
return isEnabled;
}
WebRtc_Word32
Channel::SetSourceFilter(int rtpPort, int rtcpPort, const char ipAddr[64])
{
WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId,_channelId),
"Channel::SetSourceFilter()");
if (_socketTransportModule.SetFilterPorts(
static_cast<WebRtc_UWord16>(rtpPort),
static_cast<WebRtc_UWord16>(rtcpPort)) != 0)
{
_engineStatisticsPtr->SetLastError(
VE_SOCKET_TRANSPORT_MODULE_ERROR, kTraceError,
"SetSourceFilter() failed to set filter ports");
const UdpTransport::ErrorCode lastError =
_socketTransportModule.LastError();
WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId,_channelId),
"UdpTransport::LastError() => %d",
lastError);
return -1;
}
const WebRtc_Word8* filterIpAddress =
static_cast<const WebRtc_Word8*> (ipAddr);
if (_socketTransportModule.SetFilterIP(filterIpAddress) != 0)
{
_engineStatisticsPtr->SetLastError(
VE_INVALID_IP_ADDRESS, kTraceError,
"SetSourceFilter() failed to set filter IP address");
const UdpTransport::ErrorCode lastError =
_socketTransportModule.LastError();
WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId,_channelId),
"UdpTransport::LastError() => %d", lastError);
return -1;
}
return 0;
}
WebRtc_Word32
Channel::GetSourceFilter(int& rtpPort, int& rtcpPort, char ipAddr[64])
{
WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId,_channelId),
"Channel::GetSourceFilter()");
WebRtc_UWord16 rtpFilterPort(0);
WebRtc_UWord16 rtcpFilterPort(0);
if (_socketTransportModule.FilterPorts(rtpFilterPort, rtcpFilterPort) != 0)
{
_engineStatisticsPtr->SetLastError(
VE_SOCKET_TRANSPORT_MODULE_ERROR, kTraceWarning,
"GetSourceFilter() failed to retrieve filter ports");
}
WebRtc_Word8 ipAddrTmp[UdpTransport::kIpAddressVersion6Length] = {0};
if (_socketTransportModule.FilterIP(ipAddrTmp) != 0)
{
// no filter has been configured (not seen as an error)
memset(ipAddrTmp,
0, UdpTransport::kIpAddressVersion6Length);
}
rtpPort = static_cast<int> (rtpFilterPort);
rtcpPort = static_cast<int> (rtcpFilterPort);
strcpy(ipAddr, ipAddrTmp);
WEBRTC_TRACE(kTraceStateInfo, kTraceVoice, VoEId(_instanceId,_channelId),
"GetSourceFilter() => rtpPort=%d, rtcpPort=%d, ipAddr=%s",
rtpPort, rtcpPort, ipAddr);
return 0;
}
WebRtc_Word32
Channel::SetSendTOS(int DSCP, int priority, bool useSetSockopt)
{
WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId,_channelId),
"Channel::SetSendTOS(DSCP=%d, useSetSockopt=%d)",
DSCP, (int)useSetSockopt);
// Set TOS value and possibly try to force usage of setsockopt()
if (_socketTransportModule.SetToS(DSCP, useSetSockopt) != 0)
{
UdpTransport::ErrorCode lastSockError(
_socketTransportModule.LastError());
switch (lastSockError)
{
case UdpTransport::kTosError:
_engineStatisticsPtr->SetLastError(VE_TOS_ERROR, kTraceError,
"SetSendTOS() TOS error");
break;
case UdpTransport::kQosError:
_engineStatisticsPtr->SetLastError(
VE_TOS_GQOS_CONFLICT, kTraceError,
"SetSendTOS() GQOS error");
break;
case UdpTransport::kTosInvalid:
// can't switch SetSockOpt method without disabling TOS first, or
// SetSockopt() call failed
_engineStatisticsPtr->SetLastError(VE_TOS_INVALID, kTraceError,
"SetSendTOS() invalid TOS");
break;
case UdpTransport::kSocketInvalid:
_engineStatisticsPtr->SetLastError(VE_SOCKET_ERROR, kTraceError,
"SetSendTOS() invalid Socket");
break;
default:
_engineStatisticsPtr->SetLastError(VE_TOS_ERROR, kTraceError,
"SetSendTOS() TOS error");
break;
}
WEBRTC_TRACE(kTraceError, kTraceVoice, VoEId(_instanceId,_channelId),
"UdpTransport => lastError = %d",
lastSockError);
return -1;
}
// Set priority (PCP) value, -1 means don't change
if (-1 != priority)
{
if (_socketTransportModule.SetPCP(priority) != 0)
{
UdpTransport::ErrorCode lastSockError(
_socketTransportModule.LastError());
switch (lastSockError)
{
case UdpTransport::kPcpError:
_engineStatisticsPtr->SetLastError(VE_TOS_ERROR, kTraceError,
"SetSendTOS() PCP error");
break;
case UdpTransport::kQosError:
_engineStatisticsPtr->SetLastError(
VE_TOS_GQOS_CONFLICT, kTraceError,
"SetSendTOS() GQOS conflict");
break;
case UdpTransport::kSocketInvalid:
_engineStatisticsPtr->SetLastError(
VE_SOCKET_ERROR, kTraceError,
"SetSendTOS() invalid Socket");
break;
default:
_engineStatisticsPtr->SetLastError(VE_TOS_ERROR, kTraceError,
"SetSendTOS() PCP error");
break;
}
WEBRTC_TRACE(kTraceError, kTraceVoice,
VoEId(_instanceId,_channelId),
"UdpTransport => lastError = %d",
lastSockError);
return -1;
}
}
return 0;
}
WebRtc_Word32
Channel::GetSendTOS(int &DSCP, int& priority, bool &useSetSockopt)
{
WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId,_channelId),
"Channel::GetSendTOS(DSCP=?, useSetSockopt=?)");
WebRtc_Word32 dscp(0), prio(0);
bool setSockopt(false);
if (_socketTransportModule.ToS(dscp, setSockopt) != 0)
{
_engineStatisticsPtr->SetLastError(
VE_SOCKET_TRANSPORT_MODULE_ERROR, kTraceError,
"GetSendTOS() failed to get TOS info");
return -1;
}
if (_socketTransportModule.PCP(prio) != 0)
{
_engineStatisticsPtr->SetLastError(
VE_SOCKET_TRANSPORT_MODULE_ERROR, kTraceError,
"GetSendTOS() failed to get PCP info");
return -1;
}
DSCP = static_cast<int> (dscp);
priority = static_cast<int> (prio);
useSetSockopt = setSockopt;
WEBRTC_TRACE(kTraceStateInfo, kTraceVoice, VoEId(_instanceId,-1),
"GetSendTOS() => DSCP=%d, priority=%d, useSetSockopt=%d",
DSCP, priority, (int)useSetSockopt);
return 0;
}
#if defined(_WIN32)
WebRtc_Word32
Channel::SetSendGQoS(bool enable, int serviceType, int overrideDSCP)
{
WEBRTC_TRACE(kTraceInfo, kTraceVoice, VoEId(_instanceId,_channelId),
"Channel::SetSendGQoS(enable=%d, serviceType=%d, "
"overrideDSCP=%d)",
(int)enable, serviceType, overrideDSCP);
if(!_socketTransportModule.ReceiveSocketsInitialized())
{
_engineStatisticsPtr->SetLastError(
VE_SOCKETS_NOT_INITED, kTraceError,
"SetSendGQoS() GQoS state must be set after sockets are created");
return -1;
}
if(!_socketTransportModule.SendSocketsInitialized())
{
_engineStatisticsPtr->SetLastError(
VE_DESTINATION_NOT_INITED, kTraceError,
"SetSendGQoS() GQoS state must be set after sending side is "
"initialized");
return -1;
}
if (enable &&
(serviceType != SERVICETYPE_BESTEFFORT) &&
(serviceType != SERVICETYPE_CONTROLLEDLOAD) &&
(serviceType != SERVICETYPE_GUARANTEED) &&
(serviceType != SERVICETYPE_QUALITATIVE))
{
_engineStatisticsPtr->SetLastError(
VE_INVALID_ARGUMENT, kTraceError,
"SetSendGQoS() Invalid service type");
return -1;
}
if (enable && ((overrideDSCP < 0) || (overrideDSCP > 63)))
{
_engineStatisticsPtr->SetLastError(
VE_INVALID_ARGUMENT, kTraceError,
"SetSendGQoS() Invalid overrideDSCP value");
return -1;
}
// Avoid GQoS/ToS conflict when user wants to override the default DSCP
// mapping
bool QoS(false);
WebRtc_Word32 sType(0);
WebRtc_Word32 ovrDSCP(0);
if (_socketTransportModule.QoS(QoS, sType, ovrDSCP))
{
_engineStatisticsPtr->SetLastError(
VE_SOCKET_TRANSPORT_MODULE_ERROR, kTraceError,
"SetSendGQoS() failed to get QOS info");
return -1;
}
if (QoS && ovrDSCP == 0 && overrideDSCP != 0)
{
_engineStatisticsPtr->SetLastError(
VE_TOS_GQOS_CONFLICT, kTraceError,
"SetSendGQoS() QOS is already enabled and overrideDSCP differs,"
" not allowed");
return -1;
}
const WebRtc_Word32 maxBitrate(0);
if (_socketTransportModule.SetQoS(enable,
static_cast<WebRtc_Word32>(serviceType),
maxBitrate,
static_cast<WebRtc_Word32>(overrideDSCP),
true))
{
UdpTransport::ErrorCode lastSockError(