關於對H264碼流的PS的封裝的相關代碼實現

1、寫在開始之前: 

          最近因爲新工作要維護別人留下的GB模塊代碼,先熟悉了流程,然後也試着封裝了下ps流,結果也能通過測試正常預覽了,當然,其中開發讀文檔的頭疼,預覽花屏,卡幀的事情都有遇到,當時慢慢的看文檔,整理邏輯,也就都順利解決了,下面把大致的一些流程代碼貼出來分享下。既然是對接國標,自然少不了通讀它的標準文檔和相關的RFC文檔了!具體的我就不說了,可以用百度google下的。

注意:因爲是GB要求ps封裝後再加上rtp頭的格式來的, 所以下面代碼中我也加上了rtp頭,如果不需要的話,直接屏蔽代碼中的rtp即可。

2、封裝的重點

當我們從讀緩衝區中取得一幀音視頻數據的時候,封裝時其實每一幀數據有且只有一個ps頭和psm頭,如果是I幀的話,就還多一個system頭,一個或者多個pes頭和rtp頭,

像如果幀數據過長的話,就得進行分片,每片都會包含一個pes頭,rtp負載最好長度1460,所以會進行再分包操作!所以每一個包數據至少一個rtp+databuf,每一片數據,至少有個rtp+pes+databuf,每一幀數據至少有rtp+ps+psm+pes+databuf(關鍵幀的話:多一個system頭)

3、具體的各個封裝的代碼實現

首先給去一個整體的封裝rtp->ps->sys->psm->pes(如果只要ps的話,則爲ps->sys->psm->pes)的大致流程,

然後再一一羅列出各個部件的封裝接口

  1. /*** 
  2.  *@remark:  音視頻數據的打包成ps流,並封裝成rtp 
  3.  *@param :  pData      [in] 需要發送的音視頻數據 
  4.  *          nFrameLen  [in] 發送數據的長度 
  5.  *          pPacker    [in] 數據包的一些信息,包括時間戳,rtp數據buff,發送的socket相關信息 
  6.  *          stream_type[in] 數據類型 0 視頻 1 音頻 
  7.  *@return:  0 success others failed 
  8. */  
  9.   
  10. int gb28181_streampackageForH264(char *pData, int nFrameLen, Data_Info_s* pPacker, int stream_type)  
  11. {  
  12.     char szTempPacketHead[256];  
  13.     int  nSizePos = 0;  
  14.     int  nSize = 0;       
  15.     char *pBuff = NULL;  
  16.     memset(szTempPacketHead, 0, 256);  
  17.     // 1 package for ps header   
  18.     gb28181_make_ps_header(szTempPacketHead + nSizePos, pPacker->s64CurPts);  
  19.     nSizePos += PS_HDR_LEN;   
  20.     //2 system header   
  21.     if( pPacker->IFrame == 1 )  
  22.     {  
  23.         // 如果是I幀的話,則添加系統頭  
  24.         gb28181_make_sys_header(szTempPacketHead + nSizePos);  
  25.         nSizePos += SYS_HDR_LEN;  
  26.         //這個地方我是不管是I幀還是p幀都加上了map的,貌似只是I幀加也沒有問題  
  27. //      gb28181_make_psm_header(szTempPacketHead + nSizePos);  
  28. //      nSizePos += PSM_HDR_LEN;  
  29.   
  30.     }  
  31.     // psm頭 (也是map)  
  32.     gb28181_make_psm_header(szTempPacketHead + nSizePos);  
  33.     nSizePos += PSM_HDR_LEN;  
  34.   
  35.     //加上rtp發送出去,這樣的話,後面的數據就只要分片分包就只有加上pes頭和rtp頭了  
  36.     if(gb28181_send_rtp_pack(szTempPacketHead, nSizePos, 0, pPacker) != 0 )  
  37.         return -1;    
  38.   
  39.     // 這裏向後移動是爲了方便拷貝pes頭  
  40.     //這裏是爲了減少後面音視頻裸數據的大量拷貝浪費空間,所以這裏就向後移動,在實際處理的時候,要注意地址是否越界以及覆蓋等問題  
  41.     pBuff = pData - PES_HDR_LEN;  
  42.     while(nFrameLen > 0)  
  43.     {  
  44.         //每次幀的長度不要超過short類型,過了就得分片進循環行發送  
  45.         nSize = (nFrameLen > PS_PES_PAYLOAD_SIZE) ? PS_PES_PAYLOAD_SIZE : nFrameLen;  
  46.         // 添加pes頭  
  47.         gb28181_make_pes_header(pBuff, stream_type ? 0xC0:0xE0, nSize, (pPacker->s64CurPts / 100), (pPacker->s64CurPts/300));  
  48.   
  49.         //最後在添加rtp頭併發送數據  
  50.         if( gb28181_send_rtp_pack(pBuff, nSize + PES_HDR_LEN, ((nSize == nFrameLen)?1:0), pPacker) != 0 )  
  51.         {  
  52.             printf("gb28181_send_pack failed!\n");  
  53.             return -1;  
  54.         }  
  55.         //分片後每次發送的數據移動指針操作  
  56.         nFrameLen -= nSize;  
  57.         //這裏也只移動nSize,因爲在while向後移動的pes頭長度,正好重新填充pes頭數據  
  58.         pBuff     += nSize;  
  59.           
  60.     }  
  61.     return 0;  
  62. }  

上面列出來了整個打包發包的過程,接下來一個一個接口的看

  1. /*** 
  2.  *@remark:   ps頭的封裝,裏面的具體數據的填寫已經佔位,可以參考標準 
  3.  *@param :   pData  [in] 填充ps頭數據的地址 
  4.  *           s64Src [in] 時間戳 
  5.  *@return:   0 success, others failed 
  6. */  
  7. int gb28181_make_ps_header(char *pData, unsigned long long s64Scr)  
  8. {  
  9.     unsigned long long lScrExt = (s64Scr) % 100;      
  10.     s64Scr = s64Scr / 100;  
  11.     // 這裏除以100是由於sdp協議返回的video的頻率是90000,幀率是25幀/s,所以每次遞增的量是3600,  
  12.     // 所以實際你應該根據你自己編碼裏的時間戳來處理以保證時間戳的增量爲3600即可,  
  13.     //如果這裏不對的話,就可能導致卡頓現象了  
  14.     bits_buffer_s   bitsBuffer;  
  15.     bitsBuffer.i_size = PS_HDR_LEN;   
  16.     bitsBuffer.i_data = 0;  
  17.     bitsBuffer.i_mask = 0x80; // 二進制:10000000 這裏是爲了後面對一個字節的每一位進行操作,避免大小端誇字節字序錯亂  
  18.     bitsBuffer.p_data = (unsigned char *)(pData);  
  19.     memset(bitsBuffer.p_data, 0, PS_HDR_LEN);  
  20.     bits_write(&bitsBuffer, 32, 0x000001BA);            /*start codes*/  
  21.     bits_write(&bitsBuffer, 2,  1);                     /*marker bits '01b'*/  
  22.     bits_write(&bitsBuffer, 3,  (s64Scr>>30)&0x07);     /*System clock [32..30]*/  
  23.     bits_write(&bitsBuffer, 1,  1);                     /*marker bit*/  
  24.     bits_write(&bitsBuffer, 15, (s64Scr>>15)&0x7FFF);   /*System clock [29..15]*/  
  25.     bits_write(&bitsBuffer, 1,  1);                     /*marker bit*/  
  26.     bits_write(&bitsBuffer, 15, s64Scr&0x7fff);         /*System clock [29..15]*/  
  27.     bits_write(&bitsBuffer, 1,  1);                     /*marker bit*/  
  28.     bits_write(&bitsBuffer, 9,  lScrExt&0x01ff);        /*System clock [14..0]*/  
  29.     bits_write(&bitsBuffer, 1,  1);                     /*marker bit*/  
  30.     bits_write(&bitsBuffer, 22, (255)&0x3fffff);        /*bit rate(n units of 50 bytes per second.)*/  
  31.     bits_write(&bitsBuffer, 2,  3);                     /*marker bits '11'*/  
  32.     bits_write(&bitsBuffer, 5,  0x1f);                  /*reserved(reserved for future use)*/  
  33.     bits_write(&bitsBuffer, 3,  0);                     /*stuffing length*/  
  34.     return 0;  
  35. }  
  1. /*** 
  2.  *@remark:   sys頭的封裝,裏面的具體數據的填寫已經佔位,可以參考標準 
  3.  *@param :   pData  [in] 填充ps頭數據的地址 
  4.  *@return:   0 success, others failed 
  5. */  
  6. int gb28181_make_sys_header(char *pData)  
  7. {  
  8.       
  9.     bits_buffer_s   bitsBuffer;  
  10.     bitsBuffer.i_size = SYS_HDR_LEN;  
  11.     bitsBuffer.i_data = 0;  
  12.     bitsBuffer.i_mask = 0x80;  
  13.     bitsBuffer.p_data = (unsigned char *)(pData);  
  14.     memset(bitsBuffer.p_data, 0, SYS_HDR_LEN);  
  15.     /*system header*/  
  16.     bits_write( &bitsBuffer, 32, 0x000001BB);   /*start code*/  
  17.     bits_write( &bitsBuffer, 16, SYS_HDR_LEN-6);/*header_length 表示次字節後面的長度,後面的相關頭也是次意思*/  
  18.     bits_write( &bitsBuffer, 1,  1);            /*marker_bit*/  
  19.     bits_write( &bitsBuffer, 22, 50000);        /*rate_bound*/  
  20.     bits_write( &bitsBuffer, 1,  1);            /*marker_bit*/  
  21.     bits_write( &bitsBuffer, 6,  1);            /*audio_bound*/  
  22.     bits_write( &bitsBuffer, 1,  0);            /*fixed_flag */  
  23.     bits_write( &bitsBuffer, 1,  1);            /*CSPS_flag */  
  24.     bits_write( &bitsBuffer, 1,  1);            /*system_audio_lock_flag*/  
  25.     bits_write( &bitsBuffer, 1,  1);            /*system_video_lock_flag*/  
  26.     bits_write( &bitsBuffer, 1,  1);            /*marker_bit*/  
  27.     bits_write( &bitsBuffer, 5,  1);            /*video_bound*/  
  28.     bits_write( &bitsBuffer, 1,  0);            /*dif from mpeg1*/  
  29.     bits_write( &bitsBuffer, 7,  0x7F);         /*reserver*/  
  30.     /*audio stream bound*/  
  31.     bits_write( &bitsBuffer, 8,  0xC0);         /*stream_id*/  
  32.     bits_write( &bitsBuffer, 2,  3);            /*marker_bit */  
  33.     bits_write( &bitsBuffer, 1,  0);            /*PSTD_buffer_bound_scale*/  
  34.     bits_write( &bitsBuffer, 13, 512);          /*PSTD_buffer_size_bound*/  
  35.     /*video stream bound*/  
  36.     bits_write( &bitsBuffer, 8,  0xE0);         /*stream_id*/  
  37.     bits_write( &bitsBuffer, 2,  3);            /*marker_bit */  
  38.     bits_write( &bitsBuffer, 1,  1);            /*PSTD_buffer_bound_scale*/  
  39.     bits_write( &bitsBuffer, 13, 2048);         /*PSTD_buffer_size_bound*/  
  40.     return 0;  
  41. }  
  1. /*** 
  2.  *@remark:   psm頭的封裝,裏面的具體數據的填寫已經佔位,可以參考標準 
  3.  *@param :   pData  [in] 填充ps頭數據的地址 
  4.  *@return:   0 success, others failed 
  5. */  
  6. int gb28181_make_psm_header(char *pData)  
  7. {  
  8.       
  9.     bits_buffer_s   bitsBuffer;  
  10.     bitsBuffer.i_size = PSM_HDR_LEN;   
  11.     bitsBuffer.i_data = 0;  
  12.     bitsBuffer.i_mask = 0x80;  
  13.     bitsBuffer.p_data = (unsigned char *)(pData);  
  14.     memset(bitsBuffer.p_data, 0, PS_SYS_MAP_SIZE);  
  15.     bits_write(&bitsBuffer, 24,0x000001);   /*start code*/  
  16.     bits_write(&bitsBuffer, 8, 0xBC);       /*map stream id*/  
  17.     bits_write(&bitsBuffer, 16,18);         /*program stream map length*/   
  18.     bits_write(&bitsBuffer, 1, 1);          /*current next indicator */  
  19.     bits_write(&bitsBuffer, 2, 3);          /*reserved*/  
  20.     bits_write(&bitsBuffer, 5, 0);          /*program stream map version*/  
  21.     bits_write(&bitsBuffer, 7, 0x7F);       /*reserved */  
  22.     bits_write(&bitsBuffer, 1, 1);          /*marker bit */  
  23.     bits_write(&bitsBuffer, 16,0);          /*programe stream info length*/  
  24.     bits_write(&bitsBuffer, 16, 8);         /*elementary stream map length  is*/  
  25.     /*audio*/  
  26.     bits_write(&bitsBuffer, 8, 0x90);       /*stream_type*/  
  27.     bits_write(&bitsBuffer, 8, 0xC0);       /*elementary_stream_id*/  
  28.     bits_write(&bitsBuffer, 16, 0);         /*elementary_stream_info_length is*/  
  29.     /*video*/  
  30.     bits_write(&bitsBuffer, 8, 0x1B);       /*stream_type*/  
  31.     bits_write(&bitsBuffer, 8, 0xE0);       /*elementary_stream_id*/  
  32.     bits_write(&bitsBuffer, 16, 0);         /*elementary_stream_info_length */  
  33.     /*crc (2e b9 0f 3d)*/  
  34.     bits_write(&bitsBuffer, 8, 0x45);       /*crc (24~31) bits*/  
  35.     bits_write(&bitsBuffer, 8, 0xBD);       /*crc (16~23) bits*/  
  36.     bits_write(&bitsBuffer, 8, 0xDC);       /*crc (8~15) bits*/  
  37.     bits_write(&bitsBuffer, 8, 0xF4);       /*crc (0~7) bits*/  
  38.     return 0;  
  39. }  
  1. /*** 
  2.  *@remark:   pes頭的封裝,裏面的具體數據的填寫已經佔位,可以參考標準 
  3.  *@param :   pData      [in] 填充ps頭數據的地址 
  4.  *           stream_id  [in] 碼流類型 
  5.  *           paylaod_len[in] 負載長度 
  6.  *           pts        [in] 時間戳 
  7.  *           dts        [in] 
  8.  *@return:   0 success, others failed 
  9. */  
  10. int gb28181_make_pes_header(char *pData, int stream_id, int payload_len, unsigned long long pts, unsigned long long dts)  
  11. {  
  12.       
  13.     bits_buffer_s   bitsBuffer;  
  14.     bitsBuffer.i_size = PES_HDR_LEN;  
  15.     bitsBuffer.i_data = 0;  
  16.     bitsBuffer.i_mask = 0x80;  
  17.     bitsBuffer.p_data = (unsigned char *)(pData);  
  18.     memset(bitsBuffer.p_data, 0, PES_HDR_LEN);  
  19.     /*system header*/  
  20.     bits_write( &bitsBuffer, 24,0x000001);  /*start code*/  
  21.     bits_write( &bitsBuffer, 8, (stream_id));   /*streamID*/  
  22.     bits_write( &bitsBuffer, 16,(payload_len)+13);  /*packet_len*/ //指出pes分組中數據長度和該字節後的長度和  
  23.     bits_write( &bitsBuffer, 2, 2 );        /*'10'*/  
  24.     bits_write( &bitsBuffer, 2, 0 );        /*scrambling_control*/  
  25.     bits_write( &bitsBuffer, 1, 0 );        /*priority*/  
  26.     bits_write( &bitsBuffer, 1, 0 );        /*data_alignment_indicator*/  
  27.     bits_write( &bitsBuffer, 1, 0 );        /*copyright*/  
  28.     bits_write( &bitsBuffer, 1, 0 );        /*original_or_copy*/  
  29.     bits_write( &bitsBuffer, 1, 1 );        /*PTS_flag*/  
  30.     bits_write( &bitsBuffer, 1, 1 );        /*DTS_flag*/  
  31.     bits_write( &bitsBuffer, 1, 0 );        /*ESCR_flag*/  
  32.     bits_write( &bitsBuffer, 1, 0 );        /*ES_rate_flag*/  
  33.     bits_write( &bitsBuffer, 1, 0 );        /*DSM_trick_mode_flag*/  
  34.     bits_write( &bitsBuffer, 1, 0 );        /*additional_copy_info_flag*/  
  35.     bits_write( &bitsBuffer, 1, 0 );        /*PES_CRC_flag*/  
  36.     bits_write( &bitsBuffer, 1, 0 );        /*PES_extension_flag*/  
  37.     bits_write( &bitsBuffer, 8, 10);        /*header_data_length*/   
  38.     // 指出包含在 PES 分組標題中的可選字段和任何填充字節所佔用的總字節數。該字段之前  
  39.     //的字節指出了有無可選字段。  
  40.       
  41.     /*PTS,DTS*/   
  42.     bits_write( &bitsBuffer, 4, 3 );                    /*'0011'*/  
  43.     bits_write( &bitsBuffer, 3, ((pts)>>30)&0x07 );     /*PTS[32..30]*/  
  44.     bits_write( &bitsBuffer, 1, 1 );  
  45.     bits_write( &bitsBuffer, 15,((pts)>>15)&0x7FFF);    /*PTS[29..15]*/  
  46.     bits_write( &bitsBuffer, 1, 1 );  
  47.     bits_write( &bitsBuffer, 15,(pts)&0x7FFF);          /*PTS[14..0]*/  
  48.     bits_write( &bitsBuffer, 1, 1 );  
  49.     bits_write( &bitsBuffer, 4, 1 );                    /*'0001'*/  
  50.     bits_write( &bitsBuffer, 3, ((dts)>>30)&0x07 );     /*DTS[32..30]*/  
  51.     bits_write( &bitsBuffer, 1, 1 );  
  52.     bits_write( &bitsBuffer, 15,((dts)>>15)&0x7FFF);    /*DTS[29..15]*/  
  53.     bits_write( &bitsBuffer, 1, 1 );  
  54.     bits_write( &bitsBuffer, 15,(dts)&0x7FFF);          /*DTS[14..0]*/  
  55.     bits_write( &bitsBuffer, 1, 1 );  
  56.     return 0;  
  57. }  
  1. /*** 
  2.  *@remark:   rtp頭的打包,並循環發送數據 
  3.  *@param :   pData      [in] 發送的數據地址 
  4.  *           nDatalen   [in] 發送數據的長度 
  5.  *           mark_flag  [in] mark標誌位 
  6.  *           curpts     [in] 時間戳 
  7.  *           pPacker    [in] 數據包的基本信息 
  8.  *@return:   0 success, others failed 
  9. */  
  10.   
  11. int gb28181_send_rtp_pack(char *databuff, int nDataLen, int mark_flag, Data_Info_s* pPacker)  
  12. {  
  13.     int nRes = 0;  
  14.     int nPlayLoadLen = 0;  
  15.     int nSendSize    = 0;  
  16.     char szRtpHdr[RTP_HDR_LEN];  
  17.     memset(szRtpHdr, 0, RTP_HDR_LEN);  
  18.       
  19.     if(nDataLen + RTP_HDR_LEN <= RTP_MAX_PACKET_BUFF)// 1460 pPacker指針本來有一個1460大小的buffer數據緩存  
  20.     {  
  21.         // 一幀數據發送完後,給mark標誌位置1  
  22.         gb28181_make_rtp_header(szRtpHdr, ((mark_flag == 1 )? 1 : 0 ), ++pPacker->u16CSeq, (pPacker->s64CurPts /300), pPacker->u32Ssrc);  
  23.         memcpy(pPacker->szBuff, szRtpHdr, RTP_HDR_LEN);  
  24.         memcpy(pPacker->szBuff + RTP_HDR_LEN, databuff, nDataLen);  
  25.         nRet = SendDataBuff(databuff, RTP_HDR_LEN + nSendSize, pPacker);  
  26.         if (nRes != (RTP_HDR_LEN + nDataLen))  
  27.         {  
  28.             printf(" udp send error !\n");  
  29.             return -1;  
  30.         }  
  31.           
  32.     }  
  33.     else   
  34.     {  
  35.         nPlayLoadLen = RTP_MAX_PACKET_BUFF - RTP_HDR_LEN; // 每次只能發送的數據長度 除去rtp頭  
  36.         gb28181_make_rtp_header(pPacker->szBuff, 0, ++pPacker->u16CSeq, (pPacker->s64CurPts/100), pPacker->u32Ssrc);  
  37.         memcpy(pPacker->szBuff + RTP_HDR_LEN, databuff, nPlayLoadLen);  
  38.                 nRet = SendDataBuff(databuff, RTP_HDR_LEN + nSendSize, pPacker);  
  39.         if (nRes != (RTP_HDR_LEN + nPlayLoadLen))  
  40.         {  
  41.             printf(" udp send error !\n");  
  42.             return -1;  
  43.         }  
  44.           
  45.         nDataLen -= nPlayLoadLen;  
  46.         // databuff += (nPlayLoadLen - RTP_HDR_LEN);  
  47.         databuff += nPlayLoadLen; // 表明前面到數據已經發送出去        
  48.         databuff -= RTP_HDR_LEN; // 用來存放rtp頭  
  49.         while(nDataLen > 0)  
  50.         {  
  51.             if(nDataLen <= nPlayLoadLen)  
  52.             {  
  53.                 //一幀數據發送完,置mark標誌位  
  54.                 gb28181_make_rtp_header(databuff, mark_flag, ++pPacker->u16CSeq, (pPacker->s64CurPts/100), pPacker->u32Ssrc);  
  55.                 nSendSize = nDataLen;  
  56.             }  
  57.             else   
  58.             {  
  59.                 gb28181_make_rtp_header(databuff, 0, ++pPacker->u16CSeq, (pPacker->s64CurPts/100), pPacker->u32Ssrc);  
  60.                 nSendSize = nPlayLoadLen;  
  61.             }  
  62.                         nRet = SendDataBuff(databuff, RTP_HDR_LEN + nSendSize, pPacker);  
  63.             if (nRes != (RTP_HDR_LEN + nSendSize))  
  64.             {  
  65.                 printf(" udp send error !\n");  
  66.                 return -1;  
  67.             }  
  68.             nDataLen -= nSendSize;  
  69.             databuff += nSendSize;   
  70.             //因爲buffer指針已經向後移動一次rtp頭長度後,  
  71.             //所以每次循環發送rtp包時,只要向前移動裸數據到長度即可,這是buffer指針實際指向到位置是  
  72.             //databuff向後重複的rtp長度的裸數據到位置上   
  73.   
  74.         }  
  75.   
  76.     }  
  77.     return 0;  
  78. }  

還有一個很重要的宏定義,之所以把它定義成宏,是因爲會頻繁調用,其功能是循環將一個字節的8位按位一個一個的壓入數據,防止出現誇字節的導致字序出錯問題具體實現如下,其實是挪用了vlc源碼中的實現過來的,這也是讀源碼的一個好處,能很好的利用裏面比較高級而又方便的功能代碼模塊

  1. #define PS_HDR_LEN  14  
  2. #define SYS_HDR_LEN 18  
  3. #define PSM_HDR_LEN 24  
  4. #define PES_HDR_LEN 19  
  5. #define RTP_HDR_LEN 12  
  6. /*** 
  7.  *@remark:  講傳入的數據按地位一個一個的壓入數據 
  8.  *@param :  buffer   [in]  壓入數據的buffer 
  9.  *          count    [in]  需要壓入數據佔的位數 
  10.  *          bits     [in]  壓入的數值 
  11.  */  
  12. #define bits_write(buffer, count, bits)\  
  13. {\  
  14.     bits_buffer_s *p_buffer = (buffer);\  
  15.     int i_count = (count);\  
  16.     uint64_t i_bits = (bits);\  
  17.     while( i_count > 0 )\  
  18.     {\  
  19.         i_count--;\  
  20.         if( ( i_bits >> i_count )&0x01 )\  
  21.         {\  
  22.             p_buffer->p_data[p_buffer->i_data] |= p_buffer->i_mask;\  
  23.         }\  
  24.         else\  
  25.         {\  
  26.             p_buffer->p_data[p_buffer->i_data] &= ~p_buffer->i_mask;\  
  27.         }\  
  28.         p_buffer->i_mask >>= 1;         /*操作完一個字節第一位後,操作第二位*/\  
  29.         if( p_buffer->i_mask == 0 )     /*循環完一個字節的8位後,重新開始下一位*/\  
  30.         {\  
  31.             p_buffer->i_data++;\  
  32.             p_buffer->i_mask = 0x80;\  
  33.         }\  
  34.     }\  
  35. }  

上面忘記貼出rtp封裝頭了,這次補充,如果在實際不需要rtp的話,可以直接在gb28181_send_rtp_pack函數接口中屏蔽gb28181_make_rtp_header函數接口即可,當然需要注意一點問題,就是對應的buffer指針的位置就不需要移動rtp頭的長度了!

  1. int gb28181_make_rtp_header(char *pData, int marker_flag, unsigned short cseq, long long curpts, unsigned int ssrc)  
  2. {  
  3.     bits_buffer_s   bitsBuffer;  
  4.     if (pData == NULL)  
  5.         return -1;  
  6.     bitsBuffer.i_size = RTP_HDR_LEN;  
  7.     bitsBuffer.i_data = 0;  
  8.     bitsBuffer.i_mask = 0x80;  
  9.     bitsBuffer.p_data = (unsigned char *)(pData);  
  10.     memset(bitsBuffer.p_data, 0, RTP_HDR_SIZE);  
  11.     bits_write(&bitsBuffer, 2, RTP_VERSION);    /* rtp version  */  
  12.     bits_write(&bitsBuffer, 1, 0);              /* rtp padding  */  
  13.     bits_write(&bitsBuffer, 1, 0);              /* rtp extension    */  
  14.     bits_write(&bitsBuffer, 4, 0);              /* rtp CSRC count */  
  15.     bits_write(&bitsBuffer, 1, (marker_flag));          /* rtp marker   */  
  16.     bits_write(&bitsBuffer, 7, 96);         /* rtp payload type*/  
  17.     bits_write(&bitsBuffer, 16, (cseq));            /* rtp sequence      */  
  18.     bits_write(&bitsBuffer, 32, (curpts));      /* rtp timestamp     */  
  19.     bits_write(&bitsBuffer, 32, (ssrc));        /* rtp SSRC      */  
  20.     return 0;  
  21. }  

4、封裝相關心得

        博主已經分步驗證過rtp或者ps又或者rtp+ps封裝都能正常預覽。其實這個封裝真心沒什麼理論知識說的,看看標準都知道了,只要仔細看標準一步一步的向下走,分析各個封裝的各個字段,就沒有什麼問題了,當然在實際中也有很多小問題苦惱我很久,但是無不是因爲標準沒注意或者理解有誤。現在我也只是簡單的把自己實現的代碼大致貼出來分享了,希望相關開發的少走一些彎路而已。有問題或者有更好的方法,大家可以相互交流。

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章