海康大華攝像頭高起播低延時RTSP網頁無插件流媒體播放器EasyPlayer-RTSP-Win錄像和抓圖實現線程優化方案分享


EasyPlayer-RTSP播放器是一套RTSP專用的播放器,包括有:Windows(支持IE插件,npapi插件)、Android、iOS三個平台,是由青犀TSINGSEE開放平台開發和維護的區別於市面上大部分的通用播放器,EasyPlayer-RTSP系列從2014年初發展至今得到了各行各業(尤其是安防行業)的廣泛應用,其主要原因是EasyPlayer-RTSP更加精煉、更加專注,具備低延時和高RTSP協議兼容性。

EasyPlayer-RTSP-Win錄像和抓圖實現線程優化

測試發現,通過EasyPlayer-RTSP-Win拉取網絡攝像機的流, 其音頻可能是G711,G726等,而寫MP4或者轉推RTMP等都不支持這些音頻格式,那么我們就需要將其音頻轉碼成AAC,可以使用libEasyAACEncoder庫進行轉碼,然后寫MP4或者推送;

然而,在實際應用中,我們發現轉碼過程其實還是比較耗時的,它甚至會導致解碼線程來不及從而使直播延時增大,所以,我們采用隊列緩存+線程的方式來優化錄像和抓圖。

實現如下:

1、錄像優化
1> 開啟錄像


			if (pThread->manuRecording == 0x01 && NULL==pThread->m_pMP4Writer && frameinfo.type==EASY_SDK_VIDEO_FRAME_I)//開啟錄制
			{
				//EnterCriticalSection(&pThread->critRecQueue);				
				if (!pThread->m_pMP4Writer)
				{
					pThread->m_pMP4Writer = new EasyMP4Writer();
				}
				unsigned int timestamp = (unsigned int)time(NULL);
				time_t tt = timestamp;
				struct tm *_time = localtime(&tt);
				char szTime[64] = {0,};
				strftime(szTime, 32, "%Y%m%d%H%M%S", _time);

				int nRecordPathLen = strlen(pThread->manuRecordingPath);
				if (nRecordPathLen==0 || (pThread->manuRecordingPath[nRecordPathLen-1] != '/' && pThread->manuRecordingPath[nRecordPathLen-1] != '\\') )
				{
					pThread->manuRecordingPath[nRecordPathLen] = '/';
				}
				
				char sFileName[512] = {0,};
				sprintf(sFileName, "%sch%d_%s.mp4", pThread->manuRecordingPath, pThread->channelId, szTime);
				 
				if (!pThread->m_pMP4Writer->CreateMP4File(sFileName, ZOUTFILE_FLAG_FULL))
				{
					delete pThread->m_pMP4Writer;
					pThread->m_pMP4Writer = NULL;
					//return -1;
				}		
				else
				{

				}
				//LeaveCriticalSection(&pThread->critRecQueue);
			}

2> 錄像數據寫緩存

					if (NULL != pThread->pRecAVQueue)
					{
						SSQ_AddData(pThread->pRecAVQueue, channelid, MEDIA_TYPE_VIDEO, (MEDIA_FRAME_INFO*)&frameinfo, pbuf);
					}

3> 錄像線程處理

LPTHREAD_START_ROUTINE CChannelManager::_lpRecordThread( LPVOID _pParam )
{
	PLAY_THREAD_OBJ *pThread = (PLAY_THREAD_OBJ*)_pParam;
	if (NULL == pThread)			return 0;

	pThread->recordThread.flag	=	0x02;

#ifdef _DEBUG
	_TRACE("錄像線程[%d]已啟動. ThreadId:%d ...\n", pThread->channelId, GetCurrentThreadId());
#endif

	EasyAACEncoder_Handle m_pAACEncoderHandle = NULL;
	int buf_size = 1024*1024;

	char *pbuf = new char[buf_size];
	if (NULL == pbuf)
	{
		pThread->recordThread.flag	=	0x00;
		return 0;
	}

	char* m_pAACEncBufer = new char[buf_size];
	memset(m_pAACEncBufer, 0x00, buf_size);

	//#define AVCODEC_MAX_AUDIO_FRAME_SIZE	(192000)
#define AVCODEC_MAX_AUDIO_FRAME_SIZE	(64000)
	int audbuf_len = (AVCODEC_MAX_AUDIO_FRAME_SIZE * 3) / 2;
	unsigned char *audio_buf = new unsigned char[audbuf_len+1];
	memset(audio_buf, 0x00, audbuf_len);

	MEDIA_FRAME_INFO frameinfo;
	unsigned int channelid = 0;
	unsigned int mediatype = 0;

	while (1)
	{
		if (pThread->recordThread.flag == 0x03)		break;

		int ret = SSQ_GetData(pThread->pRecAVQueue, &channelid, &mediatype, &frameinfo, pbuf);
		if (ret < 0)
		{
			_VS_BEGIN_TIME_PERIOD(1);
			__VS_Delay(1);
			_VS_END_TIME_PERIOD(1);
			continue;
		}

		long long nTimeStamp = frameinfo.timestamp_sec*1000+frameinfo.timestamp_usec/1000;
		byte*pdata=NULL;
		int datasize=0;
		bool keyframe=false;
		try
		{	
			if (mediatype == MEDIA_TYPE_VIDEO)
			{
				pdata = (byte*)pbuf;//獲取到的編碼數據
				datasize = frameinfo.length;
				int nVideoWidth     = frameinfo.width;
				int nVideoHeight    = frameinfo.height;
				keyframe = frameinfo.type==EASY_SDK_VIDEO_FRAME_I?true:false;
				if (pThread->m_pMP4Writer)
				{
					pThread->m_pMP4Writer->WriteMp4File((unsigned char*)pdata, datasize, keyframe,  nTimeStamp, nVideoWidth, nVideoHeight);
				}
			}
			else //音頻
			{
				pdata = (byte*)pbuf;//獲取到的編碼數據
				datasize = frameinfo.length;
				int bits_per_sample = frameinfo.bits_per_sample;
				int channels = frameinfo.channels;
				int sampleRate = frameinfo.sample_rate;

				if (EASY_SDK_AUDIO_CODEC_G711U == frameinfo.codec
					|| EASY_SDK_AUDIO_CODEC_G726 == frameinfo.codec 
					|| EASY_SDK_AUDIO_CODEC_G711A == frameinfo.codec ) 
				{
					if (!m_pAACEncoderHandle)
					{
						InitParam initParam;
						initParam.u32AudioSamplerate=frameinfo.sample_rate;
						initParam.ucAudioChannel=frameinfo.channels;
						initParam.u32PCMBitSize=frameinfo.bits_per_sample;
						if (frameinfo.codec == EASY_SDK_AUDIO_CODEC_G711U)
						{
							initParam.ucAudioCodec = Law_ULaw;
						} 
						else if (frameinfo.codec == EASY_SDK_AUDIO_CODEC_G726)
						{
							initParam.ucAudioCodec = Law_G726;
						}
						else if (frameinfo.codec == EASY_SDK_AUDIO_CODEC_G711A)
						{
							initParam.ucAudioCodec = Law_ALaw;
						}
						m_pAACEncoderHandle = Easy_AACEncoder_Init( initParam);
					}
					unsigned int out_len = 0;
					int nRet = Easy_AACEncoder_Encode(m_pAACEncoderHandle, 
						(unsigned char*)pbuf, frameinfo.length, (unsigned char*)m_pAACEncBufer, &out_len) ;
					if (nRet>0&&out_len>0)
					{
						pdata = (byte*)m_pAACEncBufer;
						datasize = out_len;
						frameinfo.codec = EASY_SDK_AUDIO_CODEC_AAC;
					} 
					else
					{
						continue;
					}
				}

				if (pThread->m_pMP4Writer)
				{
					if (pThread->m_pMP4Writer->CanWrite())
					{
						pThread->m_pMP4Writer->WriteAACToMp4File((unsigned char*)pdata, 
							datasize, nTimeStamp, sampleRate, channels, bits_per_sample);
					}
				}
			}
		}
		catch (...)
		{
			continue;
		}
	}

		pThread->recordThread.flag	=	0x00;

#ifdef _DEBUG
	_TRACE("錄像線程[%d]已退出 ThreadId:%d.\n", pThread->channelId, GetCurrentThreadId());
#endif

	return 0;
}

2、抓圖原理同錄像,唯一區別是直接數據傳入線程,進行jpg編碼存文件,詳見EasyPlayer-RTSP的代碼(這個代碼大部分是開源的)。


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM