wireshark 抓包rtp over tcp分析PS/H264负载的过程


wireshark抓包之后

/*
 * describe: analyze rtp over tcp.
 * create time: 2021年11月16日 星期二 19时14分24秒 CST
 * author: yushimeng@baidu.com
 */
#include <iostream>
#include <string>
#include <fstream>
#include <stdint.h>
using namespace std;

streamsize Read(istream &stream, char* buffer, streamsize count)
{
    streamsize reads = stream.rdbuf()->sgetn(buffer, count);
    stream.rdstate();
    stream.peek();
    return reads;
}

int main(int argc, char* argv[])
{
    if (argc < 2) {
        cerr << "usage: " << argv[0] << " [inputfile]" << endl;
        exit(1);
    }
    string input_file = argv[1];
    // 以读模式打开文件
    ifstream infile; 
    infile.open(input_file); 
    if (!infile.is_open()) {
        cerr << "Canot open " << input_file << endl;
        exit(-1);
    }
    // 以写模式打开文件
    ofstream outfile;
    outfile.open("output.ps");
    if (!outfile.is_open()) {
        cerr << "Canot open " << "ourput.ps" << endl;
        exit(-1);
    }
    long count = 0;
    cout << "start parse rtp/tcp stream file " << input_file << endl;
    while (!infile.eof())
    {
        // 读tcp header len
        uint16_t tcp_header_len = 0;
        char* tcp_header = (char*)&tcp_header_len;
        size_t read = Read(infile, tcp_header, 2);
        if (infile.eof()) {
            cerr << "end of file after read tcp header len" << endl;
            break;
        }
        count += read;
        uint16_t tmp_len = tcp_header_len;
        tcp_header_len = (tcp_header_len << 8) | (tmp_len >> 8);
        if (read != 2 || tcp_header_len <= 2) {
            cerr << "read tcp_header len failed!" << endl;
            break;
        }
        cout << " tcp payload len = " << tcp_header_len << endl;
        if (tcp_header_len >= 1500) {
            cout << "tcp_header_len may failed from here. count num=" << count << endl;
        }

        // 读rtpheader
        char rtp_header[12]= {0};
        read = Read(infile, rtp_header, 12);
        if (infile.eof()) {
            cout << "end of file after read tcp header len" << endl;
            break;
        }
        if (read != 12) {
            cout << "read tcp_header len failed!" << endl;
            break;
        }
        count += read;
        for (unsigned char n : rtp_header) {
            cout << "0x" << std::hex << static_cast<unsigned short>(n) << " ";
        }
        cout << endl;

        // 读ps
        char *buffer = new char(tcp_header_len - 12);
        if (buffer == NULL) {
            cerr << "failed new buffer" << endl;
            break;
        }
        read = Read(infile, buffer, tcp_header_len - 12);
        if (read != tcp_header_len - 12) {
            cerr << "read tcp_header len failed! " << endl;
            break;
        }
        count += read;

        // 写文件
        outfile.write(buffer, tcp_header_len - 12);
        outfile.flush();

    }

    cout << "end of parse rtp/tcp stream." << endl;
    // 关闭打开的文件
    infile.close();
    outfile.close();
}

 


免责声明!

本站转载的文章为个人学习借鉴使用,本站对版权不负任何法律责任。如果侵犯了您的隐私权益,请联系本站邮箱yoyou2525@163.com删除。



 
粤ICP备18138465号  © 2018-2025 CODEPRJ.COM