调取大华SDK获取IPC音视频流并得到H264帧

本文为博主原创文章,未经博主允许不得转载。(合作洽谈请联系QQ:1010316426)

通过调取大华摄像机SDK,获取的音视频码流,其格式为大华的私有格式。需通过大华的码流分析库StreamAnalyzer.dll将私有头和负载分离,从而提取出纯H264及音频帧数据。

第1步:调用sdk获取实况码流

LLONG lRet = CLIENT_RealPlayEx(m_LoginID,iChannel,0);
if(0 != lRet)
{
	m_DispHanle[iDispNum-1]=lRet;
	SetPlayVideoInfo(iDispNum,iChannel,ServerMode);
	CLIENT_SetRealDataCallBackEx2(lRet, RealDataCallBackEx, (LDWORD)this, 0x0f);
	CPlayWnd* pWnd = (CPlayWnd*)FromHandle(hWnd);
	pWnd->SetWndPlaying(true); 
}

第2步:在实况流回调函数中将码流送入帧分析库
在这里需要引入分析库StreamAnalyzer.dll,并声明一个处理句柄:ANA_HANDLE m_hParser。后续对私有流的解析都要用到这个句柄。

void CALLBACK RealDataCallBackEx(LLONG lRealHandle, DWORD dwDataType, BYTE *pBuffer,DWORD dwBufSize, LLONG lParam, LDWORD dwUser)
{
	LONG lRealPort=GetStreamPort(lRealHandle);
	if (0 == lRealPort)
	{
		return;
	}
	//Input the stream data getting from the card
	BOOL bInput=FALSE;
	switch(dwDataType) 
	{
		case 0:
			//Original data 
			ANA_InputData(m_hParser, pBuffer, dwBufSize);
			ANA_FRAME_INFO frame;
			memset(&frame, 0, sizeof(frame));
			//int ret = 0;
			while ((/*ret = */ANA_GetMediaFrame(m_hParser, &frame)) == 0)
			{
				if (((frame.bValid & (1 << E_STREAM_LENGTH)) != 0)
					|| ((frame.bValid & (1 << E_STREAM_HEADER)) != 0)
					|| ((frame.bValid & (1 << E_STREAM_VERIFY)) != 0)
					|| (frame.bValid & (1 << E_STREAM_WATERMARK))
					)
					continue;

				AanlyData(&frame);
			}

			break;
		case 1:
			//data with frame info 
	
			break;
		case 2:
			//yuv data 

			break;
		case 3:
			//pcm audio data 

			break;		
		default:
			break;
	}	
}

第3步:获取H264裸流

void AanlyData(const ANA_FRAME_INFO* frame)
{
	if (!frame || !frame->pHeader || frame->nLength <= 0)
		return;

	BOOL isKeyFrame = false;

#if 0
	printf("nType=%d nSubType=%d nStreamType=%d nEncodeType=%2d nLength=%6d nBodyLength=%6d "
		, frame->nType, frame->nSubType, frame->nStreamType, frame->nEncodeType
		, frame->nLength, frame->nBodyLength
	);

	u_char* payload = frame->pFrameBody;//H264 NALU
	for (int i = 0; i < 16*3; i++)
	{
		printf("%.2x ", payload[i]);
	}
	printf("\r\n");
#endif

	if (frame->nType == FRAME_TYPE_VIDEO)
	{
		if(frame->nEncodeType == ENCODE_VIDEO_DH_H264)
		{
			uint32_t streamType = STREAM_TYPE_VIDEO;
			uint32_t codecType = VIDEO_TYPE_H264;
			/* 进行后续操作,如打包PS等 */
		}
	}
	else if (frame->nType == FRAME_TYPE_AUDIO && m_v_streamid>0)
	{
		if (frame->nEncodeType == ENCODE_AUDIO_G711A)
		{
			uint32_t streamType = STREAM_TYPE_AUDIO;
			uint32_t codecType = AUDIO_TYPE_G711A;
		}
	}
	else
	{
		return;
	}
}

你可能感兴趣的:(音视频流媒体,28181国标,安防行业)