#include "XNetSDKTest.h"

int CALLBACK Test_TalkCallBack(XSDK_HANDLE hMedia, const unsigned char *pData, int nDataLen, int nDataType, void *pDataInfo, int nDataInfoSize, void *pUserData)
{
	if (EXSDK_DATA_FORMATE_FRAME == nDataType && nDataInfoSize > 0)
	{
		SXSDK_FRAME_INFO *pFrame = (SXSDK_FRAME_INFO *)pDataInfo;
		printf("Media[hMedia:%ld][Len:%d][Type:%d/%d/%d][%d-%d]\r\n", hMedia, nDataLen, pFrame->nType, pFrame->nSubType, pFrame->nEncodeType, pFrame->nBitsPerSample, pFrame->nSamplesPerSecond);
	}
	else if (EXSDK_DATA_MEDIA_ON_PLAY_STATE == nDataType)
	{
		printf("EXSDK_DATA_MEDIA_ON_PLAY_STATE[%d][%d]\r\n", hMedia, nDataLen);
	}
	else if (ESXSDK_MEDIA_START_REAL_PLAY == nDataType)
	{
		printf("OnRealPlayResult[%d][%s]\r\n", hMedia, nDataLen >= 0 ? "Success" : "Failed");
	}
	else 
	{
		printf("Media[hMedia:%ld][Len:%d][Type:%d]\r\n", hMedia, nDataLen, nDataType);
	}
	return 0;
}

int TestDevTalk()
{
	printf("%s\r\n", __FUNCTION__);
	XSDK_HANDLE hPlayer = 0;
	char c = 0;
	while((c = getchar()) != 'q')
	{
		switch(c)
		{
		case 's':
			{
				if (hPlayer)
				{
					XSDK_MediaStop(hPlayer);
					hPlayer = 0;
				}

				SXMediaTalkReq param = {0};
				param.nBitRate = 128;
				param.nSampleBit = 8;
				param.nSampleRate = 8000;
				strcpy((char *)param.nEncodeType, "G711_ALAW");
				param.nRequestType = EXSDK_DATA_FORMATE_FRAME;
				param.pMediaCallback = Test_TalkCallBack;
				hPlayer = XSDK_StartTalk(g_hDevice, &param);
			}
			break;
		case 't':
			{
				if (hPlayer)
				{
					XSDK_MediaStop(hPlayer);
					hPlayer = 0;
				}
			}
			break;
		default:
			{
				if (hPlayer)
				{
					char pcmData[320] = {0};
					memset(pcmData, 100, 320);
					XSDK_SendTalkData(hPlayer, pcmData, 320);
				}
			}
		}
	}

	if (hPlayer)
	{
		XSDK_MediaStop(hPlayer);
		hPlayer = 0;
	}
	return 0;
}