分享一段H264视频和AAC音频的RTP封包代码

转自http://blog.csdn.net/moruite/archive/2010/12/28/6102758.aspx

 

1. H264视频的RTP封包

 

view plain copy to clipboard print ?
  1. static int h264_parse(Track *tr, uint8_t *data, size_t len)  
  2. {  
  3.     h264_priv *priv = tr->private_data;  
  4. //    double nal_time; // see page 9 and 7.4.1.2  
  5.     size_t nalsize = 0, index = 0;  
  6.     uint8_t *p, *q;  
  7.     if (priv->is_avc) {  
  8.         while (1) {  
  9.             unsigned int i;  
  10.             if(index >= len) break;  
  11.             //get the nal size  
  12.             nalsize = 0;  
  13.             for(i = 0; i < priv->nal_length_size; i++)  
  14.                 nalsize = (nalsize << 8) | data[index++];  
  15.             if(nalsize <= 1 || nalsize > len) {  
  16.                 if(nalsize == 1) {  
  17.                     index++;  
  18.                     continue;  
  19.                 } else {  
  20.                     fnc_log(FNC_LOG_VERBOSE, "[h264] AVC: nal size %d", nalsize);  
  21.                     break;  
  22.                 }  
  23.             }  
  24.             if (DEFAULT_MTU >= nalsize) {  
  25.                 mparser_buffer_write(tr,  
  26.                                      tr->properties.pts,  
  27.                                      tr->properties.dts,  
  28.                                      tr->properties.frame_duration,  
  29.                                      1,  
  30.                                      data + index, nalsize);  
  31.                 fnc_log(FNC_LOG_VERBOSE, "[h264] single NAL");  
  32.             } else {  
  33.             // single NAL, to be fragmented, FU-A;  
  34.                 frag_fu_a(data + index, nalsize, DEFAULT_MTU, tr);  
  35.             }  
  36.             index += nalsize;  
  37.         }  
  38.     } else {  
  39.         //seek to the first startcode  
  40.         for (p = data; p<data + len - 3; p++) {  
  41.             if (p[0] == 0 && p[1] == 0 && p[2] == 1) {  
  42.                 break;  
  43.             }  
  44.         }  
  45.         if (p >= data + len) return ERR_PARSE;  
  46.         while (1) {  
  47.         //seek to the next startcode [0 0 1]  
  48.             for (q = p; q<data+len-3;q++) {  
  49.                 if (q[0] == 0 && q[1] == 0 && q[2] == 1) {  
  50.                     break;  
  51.                 }  
  52.             }  
  53.             if (q >= data + len) break;  
  54.             if (DEFAULT_MTU >= q - p) {  
  55.                 fnc_log(FNC_LOG_VERBOSE, "[h264] Sending NAL %d",p[0]&0x1f);  
  56.                 mparser_buffer_write(tr,  
  57.                                      tr->properties.pts,  
  58.                                      tr->properties.dts,  
  59.                                      tr->properties.frame_duration,  
  60.                                      1,  
  61.                                      p, q - p);  
  62.                 fnc_log(FNC_LOG_VERBOSE, "[h264] single NAL");  
  63.             } else {  
  64.                 //FU-A  
  65.                 fnc_log(FNC_LOG_VERBOSE, "[h264] frags");  
  66.                 frag_fu_a(p, q - p, DEFAULT_MTU, tr);  
  67.             }  
  68.             p = q;  
  69.         }  
  70.         // last NAL  
  71.         fnc_log(FNC_LOG_VERBOSE, "[h264] last NAL %d",p[0]&0x1f);  
  72.         if (DEFAULT_MTU >= len - (p - data)) {  
  73.             fnc_log(FNC_LOG_VERBOSE, "[h264] no frags");  
  74.             mparser_buffer_write(tr,  
  75.                                  tr->properties.pts,  
  76.                                  tr->properties.dts,  
  77.                                  tr->properties.frame_duration,  
  78.                                  1,  
  79.                                  p, len - (p - data));  
  80.         } else {  
  81.             //FU-A  
  82.             fnc_log(FNC_LOG_VERBOSE, "[h264] frags");  
  83.             frag_fu_a(p, len - (p - data), DEFAULT_MTU, tr);  
  84.         }  
  85.     }  
  86.     fnc_log(FNC_LOG_VERBOSE, "[h264] Frame completed");  
  87.     return ERR_NOERROR;  
  88. }  

 

2. AAC的RTP封包

 

view plain copy to clipboard print ?
  1. static int aac_parse(Track *tr, uint8_t *data, size_t len)  
  2. {  
  3.     //XXX handle the last packet on EOF  
  4.     int off = 0;  
  5.     uint32_t payload = DEFAULT_MTU - AU_HEADER_SIZE;  
  6.     uint8_t *packet = g_malloc0(DEFAULT_MTU);  
  7.     if(!packet) return ERR_ALLOC;  
  8. // trim away extradata  
  9. //    data += AAC_EXTRA;  
  10. //    len -= AAC_EXTRA;  
  11.     packet[0] = 0x00;  
  12.     packet[1] = 0x10;  
  13.     packet[2] = (len & 0x1fe0) >> 5;  
  14.     packet[3] = (len & 0x1f) << 3;  
  15.     if (len > payload) {  
  16.         while (len > payload) {  
  17.             memcpy(packet + AU_HEADER_SIZE, data + off, payload);  
  18.             mparser_buffer_write(tr,  
  19.                                  tr->properties.pts,  
  20.                                  tr->properties.dts,  
  21.                                  tr->properties.frame_duration,  
  22.                                  0,  
  23.                                  packet, DEFAULT_MTU);  
  24.             len -= payload;  
  25.             off += payload;  
  26.         }  
  27.     }  
  28.     memcpy(packet + AU_HEADER_SIZE, data + off, len);  
  29.     mparser_buffer_write(tr,  
  30.                          tr->properties.pts,  
  31.                          tr->properties.dts,  
  32.                          tr->properties.frame_duration,  
  33.                          1,  
  34.                          packet, len + AU_HEADER_SIZE);  
  35.     g_free(packet);  
  36.     return ERR_NOERROR;  
  37. }  

 

上面的变量 AU_HEADER_SIZE=4

/* au header

      +---------------------------------------+

      |     AU-size                           |

      +---------------------------------------+

      |     AU-Index / AU-Index-delta         |

      +---------------------------------------+

      |     CTS-flag                          |

      +---------------------------------------+

      |     CTS-delta                         |

      +---------------------------------------+

      |     DTS-flag                          |

      +---------------------------------------+

      |     DTS-delta                         |

      +---------------------------------------+

      |     RAP-flag                          |

      +---------------------------------------+

      |     Stream-state                      |

      +---------------------------------------+

*/

你可能感兴趣的:(分享一段H264视频和AAC音频的RTP封包代码)