VC 发送帧数据给SDK服务器
变量定义 :
typedef struct { //byte 0 unsigned char TYPE : 5; unsigned char NRI : 2; unsigned char F : 1; } NALU_HEADER; #define H264_START_CODE_LENGTH 0x04 #define H264_START_CODE 0x00000001 #define H264_NALU_TYPE_SILCE 1 #define H264_NALU_TYPE_IDR 5 #define EIVS_MAX_IPADDR_LEN 32
线程声明 :
DWORD WINAPI ThreadProc( LPVOID lpParam ) { DecSendData *pDlg = (DecSendData *)lpParam; ASSERT(pDlg); pDlg->SendData(); return 1; }
事件调用 :
// 发送数据 void DecSendData::OnBnClickedOk() { // TODO: 在此添加控件通知处理程序代码 UpdateData(TRUE); g_pMainDlg->fedDecInfo.ulChannelId = g_pMainDlg->m_pDecodeMainInfo->ulChannelId; m_bThreadFlage = true; CreateThread( NULL, 0,ThreadProc,(void*)this,0,NULL); OnOK(); }
发送数据 :
void DecSendData::SendData() { FILE *pFileHandle = NULL; pFileHandle = fopen("OneStdFrame3.264", "rb"); char *pBuff = new char[1024 * 1024]; char *pOneFrame = new char[1024 * 1024]; int iOneLength = 0; int iReadLength = 1; ULONG ulthreadID = m_threadID; bool bIsFindStartCode = false; char cType = 0;; while ((0 < iReadLength) && m_bThreadFlage) { iReadLength = fread(pBuff, 1, 1024 * 1024, pFileHandle); if (iReadLength < 0) { printf("Error 1\n"); break; } char *pTempBuff = pBuff; int iLeftLength = iReadLength; while ((iLeftLength > 0) && m_bThreadFlage) { if (1 == getOneFrame(pTempBuff, iLeftLength, pOneFrame, iOneLength, cType, bIsFindStartCode)) { memcpy(pOneFrame, pTempBuff, iLeftLength); iOneLength = iLeftLength; break; } else { //fwrite(pOneFrame, 1, iOneLength, pnFileHandle); IVS_PU_MatrixSendData(ulthreadID, pOneFrame, iOneLength, ENC_H264); iOneLength = 0; if ((H264_NALU_TYPE_SILCE == cType) || (H264_NALU_TYPE_IDR == cType)) { //fwrite("Hello world.", 1,sizeof("Hello world."), pnFileHandle); Sleep(40); } //fflush(pnFileHandle); } } } Sleep(1000); delete [] pBuff; pBuff = NULL; delete [] pOneFrame; pOneFrame = NULL; fclose(pFileHandle); }
获取一帧数据 :
int DecSendData::getOneFrame(char *&pstrbuff, int &iLength, char *pOneFrame, int &iOneLength, char &cType, bool &bIsFindStartCode) { int iCopyLength = 0; while (H264_START_CODE_LENGTH <= iLength) { if (H264_START_CODE == ntohl((unsigned long)*(int *)pstrbuff)) //lint !e826 { if (!bIsFindStartCode) { bIsFindStartCode = true; pstrbuff += H264_START_CODE_LENGTH; iLength -= H264_START_CODE_LENGTH; iCopyLength += H264_START_CODE_LENGTH; NALU_HEADER naluheader = *(NALU_HEADER*)pstrbuff; cType = naluheader.TYPE; } else { if (iOneLength) { memcpy(pOneFrame + iOneLength, (pstrbuff - iCopyLength), iCopyLength); } else { memcpy(pOneFrame, (pstrbuff - iCopyLength), iCopyLength); } iOneLength += iCopyLength; bIsFindStartCode = false; return 0; } } if (bIsFindStartCode) { ++iCopyLength; } ++pstrbuff; --iLength; } pstrbuff -= iCopyLength; iLength += iCopyLength; return 1; }
结束......
posted on 2012-10-26 14:32 xuejianhui 阅读(250) 评论(0) 收藏 举报
浙公网安备 33010602011771号