/*-------------------------------------------------------------------------
   RTP function copy right information

   Copyright (C) 2006-2008. Tang, Li <tangli@palmmicro.com>

   This program is free software; you can redistribute it and/or
   modify it under the terms of the GNU Lesser General Public
   License as published by the Free Software Foundation; either
   version 2.1 of the License, or (at your option) any later version.

   This software is distributed in the hope that it will be useful,
   but WITHOUT ANY WARRANTY; without even the implied warranty of
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
   Lesser General Public License for more details.

   You should have received a copy of the GNU Lesser General Public
   License along with this library; if not, write to the Free Software
   Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
-------------------------------------------------------------------------*/
#include "version.h"
#include "type.h"

#ifdef CALL_SIP

#include "ar168.h"
#include "core.h"
#include "apps.h"
#include "tcpip.h"
#include "bank5.h"

UCHAR RtpGetPayload(UCHAR iVoiceCoder, SIP_LCB_HANDLE pLcb)
{
	USHORT sIndex;

	if (iVoiceCoder == VOICE_CODER_ILBC || iVoiceCoder == VOICE_CODER_G726_32 || iVoiceCoder == VOICE_CODER_SPEEX)
	{
		if (pLcb->iSdpType == SDP_TYPE_OFFER)
		{
			if (iVoiceCoder == VOICE_CODER_ILBC)
			{
				sIndex = OPT_ILBC_PAYLOAD;
			}
			else if (iVoiceCoder == VOICE_CODER_G726_32)
			{
				sIndex = OPT_ADPCM32_PAYLOAD;
			}
			else
			{
				sIndex = OPT_SPEEX_PAYLOAD;
			}
			return OptionsGetByte(sIndex);
		}
		else
		{
			return pLcb->iCoderPayload;
		}
	}
	else
	{
		return Rtp_cPayLoad[iVoiceCoder-1];
	}
}

void RtpSendKeyPacket(SIP_LCB_HANDLE pLcb, BOOLEAN bHead, BOOLEAN bEnd)
{
	PCHAR pDst;

	pDst = Adapter_pUdpBuf;

	pDst[RTP_VERSION] = 0x80;
	memcpy4((PCHAR)(pDst+RTP_SSRC), pLcb->pRtpSSRC);
	USHORT2PCHAR(pLcb->sRtpSeq, (PCHAR)(pDst+RTP_SEQ));
	pLcb->sRtpSeq ++;
	ULONG2PCHAR(pLcb->lRtpTimeStamp, (PCHAR)(pDst+RTP_TIMESTAMP));
	pDst[RTP_PAYLOAD] = OptionsGetByte(OPT_DTMF_PAYLOAD);
	if (bHead)
	{
		pDst[RTP_PAYLOAD] |= 0x80;
	}

	Rtp_pData[0] = pLcb->iSendKey;
	Rtp_pData[1] = bEnd ? 0x80 : 0x00;
	USHORT2PCHAR(pLcb->sSendKeyLen, (PCHAR)(Rtp_pData + 2));

	UdpSendTo(pLcb->pRtpSocket, (USHORT)(RTP_DATA + 4), pLcb->pRtpDstIP, pLcb->sRtpDstPort);
}	

void RtpSendKey(SIP_LCB_HANDLE pLcb, UCHAR iKey)
{
	UCHAR i;

	// finish current DTMF key
	if (pLcb->bSendingKey)
	{
		for (i = 0; i < 3; i ++)
		{
			RtpSendKeyPacket(pLcb, FALSE, TRUE);
		}
	}

	pLcb->iSendKey = iKey;
	pLcb->sSendKeyLen = 0;
	pLcb->bSendingKey = TRUE;
}

USHORT rtp_add_voice(PCHAR pSendBuf, UCHAR iFrame, PCHAR pBuf)
{
	UCHAR i, iLen;
	USHORT sLen;

	sLen = 0;
	for (i = 0; i < iFrame; i ++)
	{
		iLen = *pBuf ++;
		if (!iLen)	break;
		memcpy((PCHAR)(pSendBuf + sLen), pBuf, iLen);
		pBuf += iLen;
		sLen += iLen;
	}

	return sLen;
}

void RtpSendVoice(SIP_LCB_HANDLE pLcb, UCHAR iLen)
{
	PCHAR pDst;

	pLcb->lRtpTimeStamp = rl_32x8((Sys_lTicker - pLcb->lRtpOffset), 3);

	pDst = Adapter_pUdpBuf;
	pDst[RTP_VERSION] = 0x80;
	memcpy4((PCHAR)(pDst+RTP_SSRC), pLcb->pRtpSSRC);
	USHORT2PCHAR(pLcb->sRtpSeq, (PCHAR)(pDst+RTP_SEQ));
	ULONG2PCHAR(pLcb->lRtpTimeStamp, (PCHAR)(pDst+RTP_TIMESTAMP));
	pDst[RTP_PAYLOAD] = RtpGetPayload(pLcb->iVoiceCoder, pLcb);
	if (pLcb->bVoiceStart)
	{
		pDst[RTP_PAYLOAD] |= 0x80;
		pLcb->bVoiceStart = FALSE;
	}

#ifdef OEM_IP20
#ifdef __ADSL
	if (pLcb->brtpIpPrivate == FALSE && pLcb->brtpSession == 1)
	{
		pLcb->brtpSession = 2;
	}
	if (pLcb->brtpIpPrivate == FALSE)
	{
		if (Sip_pCurLcb->brtpSession)//rtp packet received from destination
		{
			UdpSendTo(pLcb->pRtpSocket, iLen + RTP_DATA, pLcb->pRtpDstIP, pLcb->sRtpDstPort);
		}
	}
	else
	{
		UdpSendTo(pLcb->pRtpSocket, iLen + RTP_DATA, pLcb->pRtpDstIP, pLcb->sRtpDstPort);
	}
#else		
	UdpSendTo(pLcb->pRtpSocket, iLen + RTP_DATA, pLcb->pRtpDstIP, pLcb->sRtpDstPort);
#endif

#else
	UdpSendTo(pLcb->pRtpSocket, iLen + RTP_DATA, pLcb->pRtpDstIP, pLcb->sRtpDstPort);
#endif
	pLcb->sRtpSeq ++;
}

#endif

