// Manager组件测试代码及实用工具
#include "netsdk.h"
#include "StreamReader.h"
#include <stdlib.h>
#include <stdio.h>
#include <unistd.h>
#ifdef WIN32
#pragma comment (lib,"Bin\\x86\\netsdk")
#endif
#define trace printf
#define tracepoint() printf("FIEL: %s, LINE: %d\n", __FILE__, __LINE__);
static int g_iPlayhandle[16] = {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0};
static FILE *pFile[16];
static HANDLE hStreamParser[16];
static long g_lLogin = 0;
int StopRealPlay(int iPlayhandle);
int STDCALL RealDataCallBack(long lRealHandle,
long dwDataType, unsigned char *pBuffer,
long lbufsize,long dwUser)
{
int nChannel = dwUser;
//trace("nChannel: %d, lRealHandle: %ld, lbufsize: %ld\n", nChannel, lRealHandle, lbufsize);
if (pFile[nChannel] == NULL)
{
char szPath[64];
snprintf(szPath, sizeof(szPath), "ch%d_stream.h264", nChannel);
pFile[nChannel] = fopen(szPath, "w+");
if (NULL == pFile[nChannel])
{
//trace("fopen %s fail\n", szPath);
return -1;
}
}
if (hStreamParser[nChannel] == NULL)
{
hStreamParser[nChannel] = H264_PARSER_Create(streamTypeEmpty);
if (hStreamParser[nChannel] == NULL)
{
trace("chn: %d H264_PARSER_Create fail\n", nChannel);
}
}
if (hStreamParser[nChannel])
{
H264_FRAME_INFO *pFrameInfo;
H264_PARSER_InputData(hStreamParser[nChannel], pBuffer, lbufsize);
while (pFrameInfo = H264_PARSER_GetNextFrame(hStreamParser[nChannel]))
{
if(pFrameInfo->nFrameLength != fwrite(pFrameInfo->pContent, 1, pFrameInfo->nFrameLength, pFile[nChannel]))
{
trace("chn: %d write fail\n", nChannel);
}
//trace("nChannel: %d, len: %d, type: %d, subType: %d\n",
// nChannel, pFrameInfo->nFrameLength, pFrameInfo->nType, pFrameInfo->nSubType);
}
}
return 1;
}
void STDCALL DisConnectBackCallFunc(long lLoginID, char *pchDVRIP,
long nDVRPort, unsigned long dwUser)
{
trace("DisConnectBackCallFunc\n");
for(int i = 0; i < 1; i++)
{
if (g_iPlayhandle[i] != 0)
{
StopRealPlay(g_iPlayhandle[i]);
g_iPlayhandle[i] = 0;
}
if (hStreamParser[i])
{
H264_PARSER_Destroy(hStreamParser[i]);
hStreamParser[i] = NULL;
}
if (pFile[i] != NULL)
{
fclose(pFile[i]);
pFile[i] = NULL;
}
}
H264_DVR_Logout(lLoginID);
g_lLogin = 0;
}
bool STDCALL MessCallBack(long lLoginID, char *pBuf,
unsigned long dwBufLen, long dwUser)
{
trace("MessCallBack\n");
return true;
}
int StopRealPlay(int iPlayhandle)
{
H264_DVR_DelRealDataCallBack(iPlayhandle, RealDataCallBack, 0);
if(!H264_DVR_StopRealPlay(iPlayhandle))
{
trace("H264_DVR_StopRealPlay fail m_iPlayhandle = %d", iPlayhandle);
return -1;
}
return 0;
}
bool STDCALL CbAlarmInfo(long lLoginID, char *pBuf, unsigned long dwBufLen, long dwUser)
{
if (dwBufLen == sizeof(SDK_NetAlarmCenterMsg))
{
char szType[][32] = {"Alarm", "Log"};
char szStatus[][32] = {"Start", "Stop"};
SDK_NetAlarmCenterMsg *pMsg = (SDK_NetAlarmCenterMsg *)pBuf;
char szTime[32];
snprintf(szTime, sizeof(szTime), "%04d-%02d-%02d %02d:%02d:%02d", pMsg->Time.year,
pMsg->Time.month, pMsg->Time.day, pMsg->Time.hour,
pMsg->Time.minute, pMsg->Time.second);
char szIP[32];
snprintf(szIP, sizeof(szIP), "%d.%d.%d.%d", pMsg->HostIP.c[0], pMsg->HostIP.c[1], pMsg->HostIP.c[2], pMsg->HostIP.c[3]);
printf("Type: %s, IP: %s, Time: %s, SerialID: %s, Event: %s, Status: %s, Descrip: %s\n",
szType[pMsg->nType], szIP, szTime, pMsg->sSerialID, pMsg->sEvent, szStatus[pMsg->nStatus], pMsg->sDescrip);
return true;
}
return false;
}
int StartRealPlay(int lLoginID, int nChannel, int nStream)
{
H264_DVR_CLIENTINFO playstru;
playstru.nChannel = nChannel;
playstru.nStream = nStream;
playstru.nMode = 0;
int iPlayhandle = H264_DVR_RealPlay(lLoginID, &playstru);
if(iPlayhandle == 0 )
{
trace("Error H264_DVR_RealPlay: %ld\n", H264_DVR_GetLastError());
return -1;
}
else
{
//set callback to decode receiving data
H264_DVR_SetRealDataCallBack(iPlayhandle, RealDataCallBack, nChannel);
}
return iPlayhandle;
}
#if 0
int main(int argc, char **argv)
{
for (int i = 0; i < 16; i++)
{
hStreamParser[i] = NULL;
pFile[i] = NULL;
}
H264_DVR_Init((fDisConnect)DisConnectBackCallFunc, 0);
H264_DVR_SetDVRMessCallBack(MessCallBack, 0);
while (1)
{
if (g_lLogin == 0)
{
H264_DVR_DEVICEINFO OutDev;
int nError = 0;
g_lLogin = H264_DVR_Login("10.6.10.17", 34567, "admin", "", &OutDev, &nError);
if ( g_lLogin == 0 )
{
trace("Login Fail: %ld\n", H264_DVR_GetLastError());
}
else
{
H264_DVR_SetupAlarmChan(g_lLogin);
for(int i = 0; i < 1; i++)
{
g_iPlayhandle[i] = StartRealPlay(g_lLogin, i, 0);
if (g_iPlayhandle[i] == 0)
{
goto end;
}
}
}
}
sleep(20);
}
for(int i = 0; i < 1; i++)
{
if (g_iPlayhandle[i] != 0)
{
StopRealPlay(g_iPlayhandle[i]);
}
if (hStreamParser[i])
{
H264_PARSER_Destroy(hStreamParser[i]);
}
if (pFile[i] == NULL)
{
fclose(pFile[i]);
}
}
end:
H264_DVR_Cleanup();
return 0;
}
#else //alarm center
int main(int argc, char **argv)
{
H264_DVR_StartAlarmCenterListen(15002, CbAlarmInfo, 0);
while (1)
{
sleep(1);
}
H264_DVR_StopAlarmCenterListen();
}
#endif