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


    1. H264视频的RTP封包

    [c-sharp] view plaincopy
     
    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; 
    [c-sharp] view plaincopy
     
    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封包

    [c-sharp] view plaincopy
     
    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; 
    [c-sharp] view plaincopy
     
    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                      |

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

    */

     
  • 相关阅读:
    PHP生成xml 无法识别或是无法读取或是浏览器不识别等问题
    关于PHP 采集类
    Centos7 下安装Docke
    Git使用之设置SSH Key
    yii2.0中Rbac 怎么添加超加管理员
    Undefined index: HTTP_RAW_POST_DATA的解决办法
    window下phpstudy的nginx配置虚拟主机
    yii2.0中添加二维数组,多条数据。
    预防onion比特币勒索病毒,如何快速关闭135,137,138,139,445端口
    github与git之间怎么建立连接
  • 原文地址:https://www.cnblogs.com/lidabo/p/4245435.html
Copyright © 2020-2023  润新知