H.264的RTP封包和解包

 

H264RTP封包方法

(2010-02-20 10:08:08) 转载

标签:

杂谈

分类: 多媒体

可以通过生成SDP文件给播放器在指定端口接收数据播放,如果你不用动态调整编码器什么的就不用考虑另外发送RTCP. 而且RTCP必须自己实现RTSP服务结合起来用,没有实现RTSP服务,就谈不上实现RTCP. 具体H264字节流拆包和RTP封包方法如下

UINT MediaStreamH264::TransportData(PBYTE pData, UINT dataSize, int pts)

{ PBYTE p_buffer = pData;

 inti_buffer = dataSize;

 UINT writeSize = 0;

while( i_buffer > 4 && ( p_buffer[0] != 0 || p_buffer[1] != 0 || p_buffer[2] != 1 ) )

{ i_buffer--; p_buffer++; }

 

while( i_buffer > 4 )

{ int i_offset; int i_size = i_buffer;

int i_skip = i_buffer; /

* search nal end */

 for( i_offset = 4; i_offset+2 < i_buffer ; i_offset++)

{ if( p_buffer[i_offset] == 0 && p_buffer[i_offset+1] == 0 && p_buffer[i_offset+2] == 1 )

 {

 

 i_size = i_offset - ( p_buffer[i_offset-1] == 0 ? 1 : 0);

i_skip = i_offset; break; }

}

 

UINT iWrite = TransportH264Nal(p_buffer, i_size, pts, (i_size >= i_buffer) );

 if (iWrite > 0 ) writeSize += iWrite;

i_buffer -= i_skip;

 p_buffer += i_skip;

 }

 return writeSize; }

UINT MediaStreamH264::TransportH264Nal(const PBYTE pNal, UINT nalSize, INT32 pts, BOOL isLast)

{ ATLock atlock(&m_tlockRun);

 if (m_bRun == FALSE)

 return 0;

if( nalSize < 5 )

 return 0;

 UINTmtu = m_nMTU;

const int i_max = mtu - RTP_HEADER_SIZE;

 

int i_nal_hdr;

int i_nal_type;

i_nal_hdr = pNal[3];

i_nal_type = i_nal_hdr&0x1f;

string sps;

string pps;

 if( i_nal_type == 7 || i_nal_type == 8 )

 {

return 0;

}

 

 PBYTE p_data = pNal;

 inti_data = nalSize;

p_data += 3;

i_data -= 3;

int writeSize = 0;

 if( i_data <= i_max )

{

 

writeSize = m_pRtpTransport->SetRtpData(p_data, i_data, pts, isLast); writeSize = m_pRtpTransport->Write(p_data, i_data, m_nRtpPayloadType, pts, 0, isLast);

if (writeSize <= 0)

return 0;

return writeSize;

}

else

 {

 

const int i_count = ( i_data-1 + i_max-2 - 1 ) / (i_max-2);

int i;

 p_data++;

i_data--;

 for( i = 0; i < i_count; i++ )

 { const int i_payload =  (i_data < (i_max-2)) ? i_data : (i_max-2);

 const int nalSize = 2 + i_payload;

m_Packet.ExtendBuffer(nalSize);

 

m_Packet.m_pData[0] = 0x00 | (i_nal_hdr & 0x60) | 28;

 

m_Packet.m_pData[1] = ( i == 0 ? 0x80 : 0x00 ) | ( (i == i_count-1) ? 0x40 : 0x00 )  | i_nal_type;

 

memcpy( &m_Packet.m_pData[2], p_data, i_payload );

 m_Packet.m_DataSize = nalSize;

//int iWrite = m_pRtpTransport->SetRtpData(m_Packet.m_pData,m_Packet.m_DataSize, pts, isLast && (i == i_count-1));

 int iWrite = m_pRtpTransport->Write(m_Packet.m_pData,m_Packet.m_DataSize, m_nRtpPayloadType, pts, 0,isLast && (i == i_count-1));

if (iWrite > 0)

 writeSize += iWrite;

 i_data -= i_payload;

p_data += i_payload; }

}

return writeSize; }

 

RTP拆包如下:

UINT MediaStreamH264::TransportH264Nal(const PBYTE pNal, UINT nalSize, INT32 pts, BOOL isLast)
{
 ATLock atlock(&m_tlockRun);

 if (m_bRun == FALSE)
  return 0;

 if( nalSize < 5 )
  return 0;

 UINT mtu = m_nMTU;

 const int i_max = mtu - RTP_HEADER_SIZE;
 int i_nal_hdr;
 int i_nal_type;

 i_nal_hdr = pNal[3];
 i_nal_type = i_nal_hdr&0x1f;
 
 string sps;
 string pps;

 if( i_nal_type == 7 || i_nal_type == 8 )
 {
  
  return 0;
 }

 
 PBYTE p_data = pNal;
 int i_data = nalSize;

 p_data += 3;
 i_data -= 3;

 int writeSize = 0;

 if( i_data <= i_max )
 {
  
  //writeSize = m_pRtpTransport->SetRtpData(p_data, i_data, pts, isLast);
  writeSize = m_pRtpTransport->Write(p_data, i_data, m_nRtpPayloadType, pts, 0, isLast);
  if (writeSize <= 0)
   return 0;
  return writeSize;
 }
 else
 {
  
  const int i_count = ( i_data-1 + i_max-2 - 1 ) / (i_max-2);
  int i;

  p_data++;
  i_data--;

  for( i = 0; i < i_count; i++ )
  {
   const int i_payload =  (i_data < (i_max-2)) ? i_data : (i_max-2);
   const int nalSize = 2 + i_payload;

   m_Packet.ExtendBuffer(nalSize);

   
   m_Packet.m_pData[0] = 0x00 | (i_nal_hdr & 0x60) | 28;
   
   m_Packet.m_pData[1] = ( i == 0 ? 0x80 : 0x00 ) | ( (i == i_count-1) ? 0x40 : 0x00 )  | i_nal_type;

   
   memcpy( &m_Packet.m_pData[2], p_data, i_payload );

   m_Packet.m_DataSize = nalSize;

   //int iWrite = m_pRtpTransport->SetRtpData(m_Packet.m_pData, m_Packet.m_DataSize, pts, isLast && (i == i_count-1));
   int iWrite = m_pRtpTransport->Write(m_Packet.m_pData, m_Packet.m_DataSize, m_nRtpPayloadType, pts, 0, isLast && (i == i_count-1));
   if (iWrite > 0)
    writeSize += iWrite;

   i_data -= i_payload;
   p_data += i_payload;
  }
 }
 return writeSize;
}
== 这是具体拆法

你可能感兴趣的:(专业大杂烩,h.264,buffer,header,search,2010)