Exemplo n.º 1
0
void CSourceManager::DSRealDataManager(int nDevId, unsigned char *pBuffer, int nBufSize, 
										RealDataStreamType realDataType, /*RealDataStreamInfo*/void* realDataInfo)
{
	int nVideoWidth = 640;
	int nVideoHeight = 480;
	int nFps = 25;

	nVideoWidth = m_sDevConfigInfo.VideoInfo.nWidth ;
	nVideoHeight = m_sDevConfigInfo.VideoInfo.nHeight ;
	nFps = m_sDevConfigInfo.VideoInfo.nFrameRate;

	switch (realDataType)
	{
	case REALDATA_VIDEO:
		{
			if (m_bPushing)
			{
				//YUV格式转换
				int nWidhtHeightBuf=(nVideoWidth*nVideoHeight*3)>>1;
				CString strDataType = _T("");
				strDataType = m_sDevConfigInfo.VideoInfo.strDataType;
				BYTE* pDataBuffer = NULL;
				BYTE* pDesBuffer = pBuffer;
				if (strDataType == _T("YUY2"))
				{
					pDataBuffer=new unsigned char[nWidhtHeightBuf];
					YUY2toI420(nVideoWidth,nVideoHeight,pBuffer, pDataBuffer);
					pDesBuffer = pDataBuffer;
				}
				else //默认==RGB24
				{
					pDesBuffer = pBuffer;
					if (!m_bUseFFEncoder)//x264 Encoder just suppport i420 CSP 
					{
						pDataBuffer=new unsigned char[nWidhtHeightBuf];
						// rgb24->i420
						ConvertRGB2YUV(nVideoWidth,nVideoHeight,pBuffer, (unsigned char*)pDataBuffer);
						pDesBuffer = pDataBuffer;
					}
				}

				RTSP_FRAME_INFO	frameinfo;
				memset(&frameinfo, 0x00, sizeof(RTSP_FRAME_INFO));
				int datasize=0;
				bool keyframe=false;
				if (m_bUseFFEncoder)
				{			
					if (m_hFfeVideoHandle)
					{
						//FFEncoder编码
						int ret = FFE_EncodeVideo(m_hFfeVideoHandle, pDesBuffer, (unsigned char*)m_EncoderBuffer, &datasize, ++m_nFrameNum, 1);
						if (ret == 0x00 && datasize>0)
						{
							keyframe = ((unsigned char)m_EncoderBuffer[4]==0x67?true:false);
							frameinfo.length = datasize;
						}
					}
				}
				else//x264编码
				{
					byte*pdata = m_H264EncoderManager.Encoder(0, pDataBuffer,
						nWidhtHeightBuf, datasize, keyframe);
					if (datasize>0)
					{
						memset(m_EncoderBuffer, 0, 2073600);//1920*1080
						bool bKeyF = keyframe;
						byte btHeader[4];
						btHeader[0] = 0x00;
						btHeader[1] = 0x00;
							btHeader[2] = 0x00;
						btHeader[3] = 0x01;
						if (bKeyF)
						{
							frameinfo.length = datasize + 8 + m_spslen+m_ppslen;
							memcpy(m_EncoderBuffer, btHeader, 4);
							memcpy(m_EncoderBuffer+4, m_sps, m_spslen);
							memcpy(m_EncoderBuffer+4+m_spslen, btHeader, 4);
							memcpy(m_EncoderBuffer+4+m_spslen+4, m_pps, m_ppslen);
							memcpy(m_EncoderBuffer+4+m_spslen+4+m_ppslen, btHeader, 4);
							memcpy(m_EncoderBuffer+4+m_spslen+4+m_ppslen+4, pdata+4, datasize-4);
						} 
						else
						{
							frameinfo.length = datasize;
							memcpy(m_EncoderBuffer, btHeader, 4);
							memcpy(m_EncoderBuffer+4, pdata+4, datasize-4);
						}
					}
				}
				if (datasize>0&&m_EncoderBuffer)
				{
					frameinfo.codec = EASY_SDK_VIDEO_CODEC_H264;
					frameinfo.width  = nVideoWidth;
					frameinfo.height = nVideoHeight;
					frameinfo.fps    = nFps;
					frameinfo.type	 = ( keyframe ? EASY_SDK_VIDEO_FRAME_I : EASY_SDK_VIDEO_FRAME_P);
					long nTimeStamp = clock();
					frameinfo.timestamp_sec = nTimeStamp/1000;
					frameinfo.timestamp_usec = (nTimeStamp%1000)*1000;

					frameinfo.sample_rate = m_sDevConfigInfo.AudioInfo.nSampleRate;
					frameinfo.channels = m_sDevConfigInfo.AudioInfo.nChannaels;
					//推送回调管理
					//TRACE("frameinfo.length == %d \r\n", frameinfo.length);
					SourceManager(nDevId, (int*)&m_sSourceInfo, EASY_SDK_VIDEO_FRAME_FLAG, m_EncoderBuffer, &frameinfo);
				}

				if (pDataBuffer)
				{
					delete[] pDataBuffer;
					pDataBuffer = NULL;
				}			
			}
		}
		break;
	case REALDATA_AUDIO:
		{
			if (m_bPushing)
			{
				RTSP_FRAME_INFO	frameinfo;
				memset(&frameinfo, 0x00, sizeof(RTSP_FRAME_INFO));
				int datasize=0;
				unsigned char *pAACbuf= NULL;
				if (m_bUseFFEncoder)//FFEncoder 
				{
					if (m_hFfeAudioHandle)
					{
						//音频编码
						int ret = AAC_Encode(m_hFfeAudioHandle, (int*)pBuffer, nBufSize, &pAACbuf, &datasize);
						if (ret != 0x00 )
						{
							datasize = -1;
						}	
					}
				}
				else//Faac Encoder
				{
					pAACbuf=m_AACEncoderManager.Encoder(pBuffer,nBufSize,datasize);	
					if(pAACbuf == NULL)
					{
						datasize = -1;
					}	
				}
				if ( datasize>0&&pAACbuf)
				{
					frameinfo.codec = EASY_SDK_AUDIO_CODEC_AAC;
					frameinfo.length = datasize;
					frameinfo.sample_rate = m_sDevConfigInfo.AudioInfo.nSampleRate;
					frameinfo.channels = m_sDevConfigInfo.AudioInfo.nChannaels;
					frameinfo.width  = nVideoWidth;
					frameinfo.height = nVideoHeight;
					frameinfo.fps    = nFps;

					long nTimeStamp = clock();
					frameinfo.timestamp_sec = nTimeStamp/1000;
					frameinfo.timestamp_usec = (nTimeStamp%1000)*1000;
					SourceManager(nDevId, (int*)&m_sSourceInfo, EASY_SDK_AUDIO_FRAME_FLAG, (char*)pAACbuf, &frameinfo);
				}	
			}
		}
		break;
	}
}
Exemplo n.º 2
0
//实时音视频数据管理
// nDevId: 设备编号:音频 == -1,视频 == 0---N
// realDataType: 实时音视频数据类型;视频 == 1,音频 == 2
int CMaster::RealDataManager(int nDevId, unsigned char *pBuffer, int nBufSize, 
							 RealDataStreamType realDataType, /*RealDataStreamInfo*/void* realDataInfo)
{
	if (!pBuffer || nBufSize <= 0 )
	{
		return -1;
	}
	//获取实时数据流回调信息
	RealDataStreamInfo* callbackParam = (RealDataStreamInfo*)realDataInfo;
	// 	//录制设备型号
	// 	RecordDevFlag nDeviceType = (RecordDevFlag)(nDevId + 1);
	// 
	// 	BYTE* pWriteBuffer = NULL;//new BYTE[nBufSize];
	// 	BOOL bUseMutiThread = m_pEncConfigInfo->bUseMutiThread;

	//实时音视频数据处理
	switch (realDataType)
	{
	case REALDATA_VIDEO:
		{
			if (m_bStartPusher)
			{
				int nVideoWidth = 640;
				int	nVideoHeight = 480;
				int nWidhtHeightBuf=(nVideoWidth*nVideoHeight*3)>>1;
				BYTE* pDataBuffer=new unsigned char[nWidhtHeightBuf];
				YUY2toI420(nVideoWidth, nVideoHeight, pBuffer, pDataBuffer);
				
				//编码实现
				byte*pdata=NULL;
				int datasize=0;
				bool keyframe=false;
				pdata=m_H264EncoderManager.Encoder(0, pDataBuffer,
					nWidhtHeightBuf,datasize,keyframe);

				if (pDataBuffer)
				{
					delete pDataBuffer;
					pDataBuffer = NULL;
				}
				if (datasize>0&&m_bStartPusher)
				{
					memset(m_pFrameBuf, 0, 1024*512);
					EASY_AV_Frame   avFrame;
					bool bKeyF = keyframe;
					byte btHeader[4];
					btHeader[0] = 0x00;
					btHeader[1] = 0x00;
					btHeader[2] = 0x00;
					btHeader[3] = 0x01;
					memset(&avFrame, 0x00, sizeof(EASY_AV_Frame));
					if (bKeyF)
					{
						avFrame.u32AVFrameLen  =  datasize + 8 + m_spslen+m_ppslen;
						memcpy(m_pFrameBuf, btHeader, 4);
						memcpy(m_pFrameBuf+4, m_sps, m_spslen);
						memcpy(m_pFrameBuf+4+m_spslen, btHeader, 4);
						memcpy(m_pFrameBuf+4+m_spslen+4, m_pps, m_ppslen);
						memcpy(m_pFrameBuf+4+m_spslen+4+m_ppslen, btHeader, 4);
						memcpy(m_pFrameBuf+4+m_spslen+4+m_ppslen+4, pdata+4, datasize-4);
					} 
					else
					{
						avFrame.u32AVFrameLen = datasize;
						memcpy(m_pFrameBuf, btHeader, 4);
						memcpy(m_pFrameBuf+4, pdata+4, datasize-4);
					}
					avFrame.u32AVFrameFlag = EASY_SDK_VIDEO_FRAME_FLAG;
					avFrame.pBuffer = (unsigned char*)m_pFrameBuf;
					avFrame.u32VFrameType = (bKeyF)?EASY_SDK_VIDEO_FRAME_I:EASY_SDK_VIDEO_FRAME_P;
					long nTimeStamp = clock();
					avFrame.u32TimestampSec = nTimeStamp/1000;
					avFrame.u32TimestampUsec = (nTimeStamp%1000)*1000;

					EasyPusher_PushFrame(m_pusherId, &avFrame);
				}
			}

// 			if (m_pRecordModel->nRecFlag==RECFLAG_RUNNING&&(m_pmp4file[0]||m_pMP4Package[0]))//&&(m_nCountQueueVideoFrames<m_nMaxQueueVideoFrames))
// 			{	
// 				FrameInfo *pTmpinfo=NULL;
// 				pTmpinfo=new FrameInfo;
// 
// 				int nWidhtHeightBuf=(nVideoWidth*nVideoHeight*3)>>1;
// 				pTmpinfo->pDataBuffer=new unsigned char[nWidhtHeightBuf];
// 				YUY2toI420(nVideoWidth,nVideoHeight,pDataWriteWaterMask,pTmpinfo->pDataBuffer);
// 				pTmpinfo->nBufSize=nWidhtHeightBuf;
// 				//	memcpy(pTmpinfo->pDataBuffer,m_pRealWriteBuffer,nBufSize);			
// 				pTmpinfo->bIsVideo=TRUE;
// 				pTmpinfo->nID=0;	
// 
// 				EnterCriticalSection(&m_cs);					
// 				pTmpinfo->nTimestamp=clock()-m_unVPauseTime;
// 				m_queueframe.push(pTmpinfo);
// 				m_nCountQueueVideoFrames++;
// 				LeaveCriticalSection(&m_cs);
// 			}					

		}//case REALDATA_VIDEO
		break;//REALDATA_VIDEO

	case REALDATA_AUDIO://PCM等原始音频数据
		{
			if (m_bStartPusher)
			{
				byte*pdata=NULL;
				int datasize=0;
				pdata=m_AACEncoderManager.Encoder(pBuffer,nBufSize,datasize);	
				if(datasize>0&&m_bStartPusher)
				{

					EASY_AV_Frame   AudioFrame;
					AudioFrame.u32AVFrameFlag = EASY_SDK_AUDIO_FRAME_FLAG;
					AudioFrame.pBuffer = (unsigned char*)pdata;
					AudioFrame.u32VFrameType = FALSE;//(bKeyF)?EASY_SDK_VIDEO_FRAME_I:EASY_SDK_VIDEO_FRAME_P;
					AudioFrame.u32AVFrameLen = datasize;
					long nTimeStamp = clock();
					AudioFrame.u32TimestampSec = nTimeStamp/1000;
					AudioFrame.u32TimestampUsec = (nTimeStamp%1000)*1000;

					EasyPusher_PushFrame(m_pusherId, &AudioFrame);

// 					if(m_pRecordModel->nRecFlag==RECFLAG_RUNNING&&m_bIsAudioWriteInFile==TRUE)
// 					{
// 						FrameInfo *pTmpinfo;
// 						pTmpinfo=new FrameInfo;
// 						pTmpinfo->pDataBuffer=new unsigned char[datasize];
// 						memcpy(pTmpinfo->pDataBuffer,pdata,datasize);					
// 						pTmpinfo->nBufSize=datasize;
// 						pTmpinfo->bIsVideo=FALSE;
// 						pTmpinfo->nID=0;	
// 
// 						EnterCriticalSection(&m_cs);
// 						pTmpinfo->nTimestamp=clock()-m_unAPauseTime;
// 						m_queueframe.push(pTmpinfo);
// 						LeaveCriticalSection(&m_cs);
// 					}

				}
			}	
		}
		break;

	default:
		break;
	}

	return TRUE;//音视频文件录制
}