/////from the parsed imgbuf, we can get three parts buffers, pHdr, pBody, pExtra.
/////

enum
{
	DB_TYPE_FAKE_DATA =0,//fake data
	DB_TYPE_MP4_I=1,//I-frame
	DB_TYPE_MP4_P,//B-frame
	DB_TYPE_MP4_B,//B-frame
}; // total 8 ,is FIXED!!, can't add it



int sn=0;
int findfirst_I=false;

///we use a queue to save all parsed images.
DWORD CALLBACK ShowImgMultiThreadFunc( void* pArguments )
{
	while(!pHttp->CloseTrd)
	{			
		if(!pHttp->m_qImg[ch_id].empty() && !pHttp->CloseTrd)
		{	
			hdr=(tHeader *)pHttp->m_qImg[ch_id].front().pHdr;
			
			if(hdr->Basic.Type == DB_TYPE_FAKE_DATA){
					/////if fake data type, direct to remove the imgbuf
					goto mv_mem;
			}
			else
			{
				if(hdr->Basic.Type ==DB_TYPE_FAKE_DATA||hdr->Basic.Type==DB_TYPE_MP4_I){
					
					bool sendmessage=false;
					if(hdr->Basic.Source) // IP
					{
						for(int t=pHttp->m_iMaxanalogcamera;t<pHttp->m_iMaxChannel;t++){//only identify 16 cameras becuase header only has 16 cameras' status.
							
							pHttp->m_bIsLoss[t]=((*(short*)hdr->Extra.Vloss)>>(t-pHttp->m_iMaxanalogcamera))&0x1;
						    
							if(!hdr->Basic.ImgMode)
							{
								//Live
								pHttp->m_bIsAlarm[t]=((*(short*)hdr->Extra.AlmIn)>>(t-pHttp->m_iMaxanalogcamera))&0x1;	
							}
							
							if(pHttp->m_bIsLoss[t]){
								pHttp->m_bAvailableCH[t]=false;
								if(pHttp->m_iGPSHostCH==t/*&&(pHttp->GPS_HOSTNAME.GetLength()||pHttp->GPS_STOPNAME.GetLength())*/){
									memset(pHttp->pYUY2[t],0,pHttp->YUVFmt[t].biSizeImage);
									if(!pHttp->m_bForceShowinfo[t])pHttp->m_bForceShowinfo[t]=true;
									PulseEvent(pHttp->m_hImgEvent[t]);
								}
								else{
									sendmessage=true;
								}
							}
						}
					}
					else // Analog
					{
						for(int t=0;t<pHttp->m_iMaxanalogcamera;t++)//only identify 16 cameras becuase header only has 16 cameras' status.
						{	
							pHttp->m_bIsLoss[t]=((*(short*)hdr->Extra.Vloss)>>t)&0x1;
							
							if(!hdr->Basic.ImgMode)
							{
								//Live
								pHttp->m_bIsAlarm[t]=((*(short*)hdr->Extra.AlmIn)>>t)&0x1;
							}
							
							if(pHttp->m_bIsLoss[t])
							{	
								pHttp->m_bAvailableCH[t]=false;
								if(pHttp->m_iGPSHostCH==t/*&&(pHttp->GPS_HOSTNAME.GetLength()||pHttp->GPS_STOPNAME.GetLength())*/){
									memset(pHttp->pYUY2[t],0,pHttp->YUVFmt[t].biSizeImage);
									if(!pHttp->m_bForceShowinfo[t])pHttp->m_bForceShowinfo[t]=true;
									PulseEvent(pHttp->m_hImgEvent[t]);
								}
								else{
									sendmessage=true;
								}
							}
						}
					}
					if(sendmessage)pHttp->m_bSendmessage=true;						
				}
			}
			
			////(previos serno)+1 =skipPicCnt+(running serno)// if it is unequal, findfirst_I will be false to re-find I-frame.
			////if skipPicCnt >3 means loss to many frames, force to re-find I-frame also.
			if((sn+1) != (hdr->Extra.Serno-hdr->Extra.SkipPicCnt) ||hdr->Extra.SkipPicCnt>3)
			{	
				findfirst_I= false;
			}
				
			sn=hdr->Extra.Serno;
				if(hdr->Basic.Audio==0)/////to make sure the data is video
				{
					/////
					////the image format can be identified as the following
					////
					unsigned char Imagechtype=*(pHttp->m_qImg[ch_id].front().pBody+3);
					unsigned char Imagetype=Imagechtype>>4;
					
					if(Imagetype==0xf){//we use ffmpeg to decode H.264
						if(hdr->Basic.Type ==DB_TYPE_MP4_I ||  findfirst_I)
						{ 
							
							//after finding a I-frmae, set findfirst_I to be true;
							if(!findfirst_I&&hdr->Basic.Type ==DB_TYPE_MP4_I){
								findfirst_I= true;
							}	
							if((hdr->Basic.Type ==DB_TYPE_MP4_I		   )||
									(hdr->Basic.Type ==DB_TYPE_MP4_B ) ||
									(hdr->Basic.Type ==DB_TYPE_MP4_P )  )
							{
							
										unsigned char *prawdata=pHttp->m_qImg[ch_id].front().pBody+32;
										////////
										/////there is a PES Header , please shift 32 bytes to do decode.
										/////////
							}
						}
					}
					else if(Imagetype==0xd){
						
										unsigned char *prawdata=pHttp->m_qImg[ch_id].front().pBody+32;
										////////
										/////there is a PES Header , please shift 32 bytes to do decode.
										/////////
					}
					else if(Imagetype==0xe){
						//after finding a I-frmae, set findfirst_I to be true;
						if(!findfirst_I&&hdr->Basic.Type ==DB_TYPE_MP4_I){
										findfirst_I= true;
						}	
						if(hdr->Basic.Type ==DB_TYPE_MP4_I ||  findfirst_I )
						{
							if((hdr->Basic.Type ==DB_TYPE_MP4_I )||
									(hdr->Basic.Type ==DB_TYPE_MP4_B) ||
									(hdr->Basic.Type ==DB_TYPE_MP4_P) )
							{
								if(hdr->Basic.Source)///this variable can check the camera is IP or analog.
								{
										//////////we use XVID to decode IP camera
										////////
										/////there is a PES Header , please shift 32 bytes to do decode.
										/////////
								}
								else{
										//////////we use the library from the company selling the encoder chips.
										unsigned char *prawdata=pHttp->m_qImg[ch_id].front().pBody+32;
										////////
										/////there is a PES Header , please shift 32 bytes to do decode.
										/////////
								}
							}
						}
					}
					else{
						findfirst_I=false;
						OutputDebugStringA("not a correct video type");
						goto mv_mem;
					
					}
				}
mv_mem:
			SAFE_DELETE(pHttp->m_qImg[ch_id].front().pBody);
			SAFE_DELETE(pHttp->m_qImg[ch_id].front().pSuperExtra);
			SAFE_DELETE(pHttp->m_qImg[ch_id].front().pHdr);
			pHttp->m_qImg[ch_id].pop();	
		}	
	}
}