/* 
 * Copyright (C) 2012 Yee Young Han <websearch@naver.com> (http://blog.naver.com/websearch)
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program 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 General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA 
 */

#include "SipClient.h"
#include <time.h>
#include "Log.h"
#include "RtpThread.h"
#include "MemoryDebug.h"

std::string	gstrInviteId;

/**
 * @ingroup SipClient
 * @brief SIP REGISTER ÀÀ´ä ¸Þ½ÃÁö ¼ö½Å ÀÌº¥Æ® ÇÚµé·¯
 * @param pclsInfo	SIP REGISTER ÀÀ´ä ¸Þ½ÃÁö¸¦ Àü¼ÛÇÑ IP-PBX Á¤º¸ ÀúÀå °´Ã¼
 * @param iStatus		SIP REGISTER ÀÀ´ä ÄÚµå
 */
void CSipClient::EventRegister( CSipServerInfo * pclsInfo, int iStatus )
{
	CLog::Print( LOG_DEBUG, "%s(%d)", __FUNCTION__, iStatus );
	printf( "EventRegister(%s) : %d\n", pclsInfo->m_strUserId.c_str(), iStatus );
}

/**
 * @ingroup SipClient
 * @brief SIP ÅëÈ­ ¿äÃ» ¼ö½Å ÀÌº¥Æ® ÇÚµé·¯
 * @param	pszCallId	SIP Call-ID
 * @param pszFrom		SIP From »ç¿ëÀÚ ¾ÆÀÌµð
 * @param pszTo			SIP To »ç¿ëÀÚ ¾ÆÀÌµð
 * @param pclsRtp		RTP Á¤º¸ ÀúÀå °´Ã¼
 */
void CSipClient::EventIncomingCall( const char * pszCallId, const char * pszFrom, const char * pszTo, CSipCallRtp * pclsRtp )
{
	printf( "EventIncomingCall(%s,%s)\n", pszCallId, pszFrom );

	gstrInviteId = pszCallId;

	if( pclsRtp )
	{
		printf( "=> RTP(%s:%d) codec(%d)\n", pclsRtp->m_strIp.c_str(), pclsRtp->m_iPort, pclsRtp->m_iCodec );

		m_clsDestRtp = *pclsRtp;
	}
}

/**
 * @ingroup SipClient
 * @brief SIP Ring / Session Progress ¼ö½Å ÀÌº¥Æ® ÇÚµé·¯
 * @param	pszCallId		SIP Call-ID
 * @param iSipStatus	SIP ÀÀ´ä ÄÚµå
 * @param pclsRtp			RTP Á¤º¸ ÀúÀå °´Ã¼
 */
void CSipClient::EventCallRing( const char * pszCallId, int iSipStatus, CSipCallRtp * pclsRtp )
{
	printf( "EventCallRing(%s,%d)\n", pszCallId, iSipStatus );

	if( pclsRtp )
	{
		printf( "=> RTP(%s:%d) codec(%d)\n", pclsRtp->m_strIp.c_str(), pclsRtp->m_iPort, pclsRtp->m_iCodec );
	}
}

/**
 * @ingroup SipClient
 * @brief SIP ÅëÈ­ ¿¬°á ÀÌº¥Æ® ÇÚµé·¯
 * @param	pszCallId	SIP Call-ID
 * @param pclsRtp		RTP Á¤º¸ ÀúÀå °´Ã¼
 */
void CSipClient::EventCallStart( const char * pszCallId, CSipCallRtp * pclsRtp )
{
	printf( "EventCallStart(%s)\n", pszCallId );

	if( pclsRtp )
	{
		printf( "=> RTP(%s:%d) codec(%d)\n", pclsRtp->m_strIp.c_str(), pclsRtp->m_iPort, pclsRtp->m_iCodec );

		gclsRtpThread.Start( pclsRtp->m_strIp.c_str(), pclsRtp->m_iPort );
	}
}

/**
 * @ingroup SipClient
 * @brief SIP ÅëÈ­ Á¾·á ÀÌº¥Æ® ÇÚµé·¯
 * @param	pszCallId		SIP Call-ID
 * @param iSipStatus	SIP ÀÀ´ä ÄÚµå. INVITE ¿¡ ´ëÇÑ ¿À·ù ÀÀ´äÀ¸·Î ÀüÈ­°¡ Á¾·áµÈ °æ¿ì, INVITE ÀÇ ÀÀ´ä ÄÚµå¸¦ ÀúÀåÇÑ´Ù.
 */
void CSipClient::EventCallEnd( const char * pszCallId, int iSipStatus )
{
	printf( "EventCallEnd(%s,%d)\n", pszCallId, iSipStatus );

	gclsRtpThread.Stop( );
}

/**
 * @ingroup SipClient
 * @brief SIP ReINVITE ¼ö½Å ÀÌº¥Æ® ÇÚµé·¯
 * @param	pszCallId	SIP Call-ID
 * @param pclsRemoteRtp		»ó´ë¹æ RTP Á¤º¸ ÀúÀå °´Ã¼
 * @param pclsLocalRtp		³» RTP Á¤º¸ ÀúÀå °´Ã¼
 */
void CSipClient::EventReInvite( const char * pszCallId, CSipCallRtp * pclsRemoteRtp, CSipCallRtp * pclsLocalRtp )
{
	printf( "EventReInvite(%s)\n", pszCallId );

	if( pclsRemoteRtp )
	{
		printf( "=> RTP(%s:%d) codec(%d)\n", pclsRemoteRtp->m_strIp.c_str(), pclsRemoteRtp->m_iPort, pclsRemoteRtp->m_iCodec );
		gclsRtpThread.Start( pclsRemoteRtp->m_strIp.c_str(), pclsRemoteRtp->m_iPort );
	}
}

/**
 * @ingroup SipClient
 * @brief Screened / Unscreened Transfer ¿äÃ» ¼ö½Å ÀÌº¥Æ® ÇÚµé·¯
 * @param pszCallId					SIP Call-ID
 * @param pszReferToCallId	ÀüÈ­°¡ Àü´ÞµÉ SIP Call-ID
 * @param bScreenedTransfer Screened Transfer ÀÌ¸é true °¡ ÀÔ·ÂµÇ°í Unscreened Transfer ÀÌ¸é false °¡ ÀÔ·ÂµÈ´Ù.
 * @returns ¿äÃ»À» ¼ö¶ôÇÏ¸é true ¸¦ ¸®ÅÏÇÏ°í ±×·¸Áö ¾ÊÀ¸¸é false ¸¦ ¸®ÅÏÇÑ´Ù.
 */
bool CSipClient::EventTransfer( const char * pszCallId, const char * pszReferToCallId, bool bScreenedTransfer )
{
	printf( "EventTransfer(%s,%s)\n", pszCallId, pszReferToCallId );

	return false;
}

/**
 * @ingroup SipClient
 * @brief Blind Transfer ¿äÃ» ¼ö½Å ÀÌº¥Æ® ÇÚµé·¯
 * @param pszCallId			SIP Call-ID
 * @param pszReferToId	ÀüÈ­°¡ Àü´ÞµÉ »ç¿ëÀÚ ¾ÆÀÌµð
 * @returns ¿äÃ»À» ¼ö¶ôÇÏ¸é true ¸¦ ¸®ÅÏÇÏ°í ±×·¸Áö ¾ÊÀ¸¸é false ¸¦ ¸®ÅÏÇÑ´Ù.
 */
bool CSipClient::EventBlindTransfer( const char * pszCallId, const char * pszReferToId )
{
	printf( "EventBlindTransfer(%s,%s)\n", pszCallId, pszReferToId );

	return false;
}

/**
 * @ingroup SipClient
 * @brief SIP MESSAGE ¼ö½Å ÀÌº¥Æ® ÇÚµé·¯
 * @param pszFrom			SIP ¸Þ½ÃÁö Àü¼Û ¾ÆÀÌµð
 * @param pszTo				SIP ¸Þ½ÃÁö ¼ö½Å ¾ÆÀÌµð
 * @param pclsMessage SIP ¸Þ½ÃÁö
 * @returns ¿äÃ»À» ¼ö¶ôÇÏ¸é true ¸¦ ¸®ÅÏÇÏ°í ±×·¸Áö ¾ÊÀ¸¸é false ¸¦ ¸®ÅÏÇÑ´Ù.
 */
bool CSipClient::EventMessage( const char * pszFrom, const char * pszTo, CSipMessage * pclsMessage )
{
	char	szContentType[255];

	memset( szContentType, 0, sizeof(szContentType) );
	pclsMessage->m_clsContentType.ToString( szContentType, sizeof(szContentType) );

	printf( "EventMessage(%s,%s)\n", pszFrom, pszTo );
	printf( "content-type[%s]\n", szContentType );
	printf( "body[%s]\n", pclsMessage->m_strBody.c_str() );

	return true;
}
