#include "XNetSDKTest.h"
#include "XNetSDKSyn.h"
#include "RecordCfg.h"

USE_NS_NETSDK_CFG

int CALLBACK Test_RecordMediaCallBack(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][%04d-%02d-%02d %02d:%02d:%02d-%03d]\r\n", hMedia, nDataLen, pFrame->nType, pFrame->nSubType, pFrame->nYear, pFrame->nMonth, pFrame->nDay, pFrame->nHour, pFrame->nMinute, pFrame->nSecond, (int)(pFrame->nTimeStamp % 1000));
		
		//char name[200] = {0};
		char name[200] = "123.264";
		sprintf(name, "%d.h264", 1);
		FILE *file = fopen(name, "ab+");
		if (file)
		{
			fwrite(pFrame->pContent, 1, pFrame->nFrameLength, file);
			fclose(file);
		}
	}
	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_RECORD_PLAY == nDataType)
	{
		printf("OnRecordPlayResult[%d][%s]\r\n", hMedia, nDataLen >= 0 ? "Success" : "Failed");
	}
	else if (ESXSDK_MEDIA_DOWN_RECORD_FILE == nDataType)
	{
		printf("OnRecordDownloadResult[%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 TestMediaRecordPlay()
{
	printf("%s\r\n", __FUNCTION__);
	XSDK_HANDLE hPlayer = 0;
	bool bPause = false;
	int nSpeed = 0;
	char c = 0;
	do
	{
		switch(c)
		{
			////开始录像回放
		case 'a':
			{
				if (hPlayer)
				{
					XSDK_MediaStop(hPlayer);
					hPlayer = 0;
				}

				SXMediaRecordReq param = {0};
				param.nChannel = 1;
				param.nStreamType = 0;
				param.nRequestType = EXSDK_DATA_FORMATE_FRAME;
				param.pMediaCallback = Test_RecordMediaCallBack;
#if 1
				time_t tNow = time(NULL);
				struct tm tmNow = *localtime(&tNow);
				sprintf(param.sBeginTime, "%04d-%02d-%02d 00:00:00", tmNow.tm_year + 1900, tmNow.tm_mon + 1, tmNow.tm_mday);
				sprintf(param.sEndTime, "%04d-%02d-%02d 00:59:59", tmNow.tm_year + 1900, tmNow.tm_mon + 1, tmNow.tm_mday);
#else
				strcpy(param.sBeginTime, "2018-09-12 00:00:00");
				strcpy(param.sEndTime, "2018-09-12 23:59:59");
#endif
				hPlayer = XSDK_MediaRecordPlay(g_hDevice, &param, 0, 4000);
				//hPlayer = XSDK_Lib_MediaRecordDownload(g_hDevice, &param, 0, 4000);
				bPause = false;
				nSpeed = 0;
			}
			break;
			////关闭录像回放
		case 'b':
			{
				XSDK_MediaStop(hPlayer);
				hPlayer = 0;
			}
			break;
			////暂停录像回放
		case 'c':
			{
				bPause = !bPause;
				XSDK_MediaPause(hPlayer, bPause);
			}
			break;
			///回放控制
		case 'd':
			{
				XSDK_MediaSetSpeed(hPlayer, nSpeed);
				nSpeed++;
				if (nSpeed == 4)
				{
					nSpeed = -4;
				}
			}
			break;
			///录像回放跳转
		case 'e':
			{
				XSDK_MediaSeekToTime(hPlayer, "2018-08-04 13:00:00");
			}
			break;
			///录像相关配置
		case 'f':
			{
				///获取录像相关参数
				char szOutBuffer[4096] = {0};
				char outbuffer[100] = {0};
				int len = 0;
				int nInOutSize = sizeof(szOutBuffer);
				int nRet = 0;

				nRet =XSDK_DevGetChnConfigSyn(g_hDevice, 0, JK_Record, szOutBuffer, &nInOutSize, 4000, EXCMD_CONFIG_GET);

				if(nRet >= 0)
				{
					RecordCfg se;
					se.SetName("Record.[0]");
					se.Parse(szOutBuffer);

					se.PreRecord.SetValue(0);

					const char *pCfg = se.ToString();
					nRet = XSDK_DevSetChnConfigSyn(g_hDevice, 0, JK_Record, pCfg, strlen(pCfg), outbuffer, &len, 5000, EXCMD_CONFIG_SET);

					if(nRet >= 0)
					{
						printf("Set Success!\r\n");
					}
				}
			}
			break;
		}
	}while((c = getchar()) != 'q');

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