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

1. H264视频的RTP封包

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

2. AAC的RTP封包

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

上面的变量 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                      |

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

*/

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值