Rtpdump (rtptools): C++98/STL parsing library

rtpdump is a format used by rtptools to record and replay rtp data from network capture.

File extension

["rtp", "rtpdump"]

KS implementation details

License: Unlicense

This page hosts a formal specification of Rtpdump (rtptools) using Kaitai Struct. This specification can be automatically translated into a variety of programming languages to get a parsing library.

Usage

Runtime library

All parsing code for C++98/STL generated by Kaitai Struct depends on the C++/STL runtime library. You have to install it before you can parse data.

For C++, the easiest way is to clone the runtime library sources and build them along with your project.

Code

Using Kaitai Struct in C++/STL usually consists of 3 steps.

  1. We need to create an STL input stream (std::istream). One can open local file for that, or use existing std::string or char* buffer.
    #include <fstream>
    
    std::ifstream is("path/to/local/file.["rtp", "rtpdump"]", std::ifstream::binary);
    
    #include <sstream>
    
    std::istringstream is(str);
    
    #include <sstream>
    
    const char buf[] = { ... };
    std::string str(buf, sizeof buf);
    std::istringstream is(str);
    
  2. We need to wrap our input stream into Kaitai stream:
    #include "kaitai/kaitaistream.h"
    
    kaitai::kstream ks(&is);
    
  3. And finally, we can invoke the parsing:
    rtpdump_t data(&ks);
    

After that, one can get various attributes from the structure by invoking getter methods like:

data.file_header() // => get file header

C++98/STL source code to parse Rtpdump (rtptools)

rtpdump.h

#ifndef RTPDUMP_H_
#define RTPDUMP_H_

// This is a generated file! Please edit source .ksy file and use kaitai-struct-compiler to rebuild

#include "kaitai/kaitaistruct.h"
#include <stdint.h>
#include "rtp_packet.h"
#include <vector>

#if KAITAI_STRUCT_VERSION < 9000L
#error "Incompatible Kaitai Struct C++/STL API: version 0.9 or later is required"
#endif
class rtp_packet_t;

/**
 * rtpdump is a format used by rtptools to record and replay
 * rtp data from network capture.
 * \sa https://chromium.googlesource.com/external/webrtc/stable/talk/+/master/media/base/rtpdump.h Source
 */

class rtpdump_t : public kaitai::kstruct {

public:
    class header_t_t;
    class packet_t_t;

    rtpdump_t(kaitai::kstream* p__io, kaitai::kstruct* p__parent = 0, rtpdump_t* p__root = 0);

private:
    void _read();
    void _clean_up();

public:
    ~rtpdump_t();

    class header_t_t : public kaitai::kstruct {

    public:

        header_t_t(kaitai::kstream* p__io, rtpdump_t* p__parent = 0, rtpdump_t* p__root = 0);

    private:
        void _read();
        void _clean_up();

    public:
        ~header_t_t();

    private:
        std::string m_shebang;
        std::string m_space;
        std::string m_ip;
        std::string m_port;
        uint32_t m_start_sec;
        uint32_t m_start_usec;
        uint32_t m_ip2;
        uint16_t m_port2;
        uint16_t m_padding;
        rtpdump_t* m__root;
        rtpdump_t* m__parent;

    public:
        std::string shebang() const { return m_shebang; }
        std::string space() const { return m_space; }
        std::string ip() const { return m_ip; }
        std::string port() const { return m_port; }

        /**
         * start of recording, the seconds part.
         */
        uint32_t start_sec() const { return m_start_sec; }

        /**
         * start of recording, the microseconds part.
         */
        uint32_t start_usec() const { return m_start_usec; }

        /**
         * network source.
         */
        uint32_t ip2() const { return m_ip2; }

        /**
         * port.
         */
        uint16_t port2() const { return m_port2; }

        /**
         * 2 bytes padding.
         */
        uint16_t padding() const { return m_padding; }
        rtpdump_t* _root() const { return m__root; }
        rtpdump_t* _parent() const { return m__parent; }
    };

    class packet_t_t : public kaitai::kstruct {

    public:

        packet_t_t(kaitai::kstream* p__io, rtpdump_t* p__parent = 0, rtpdump_t* p__root = 0);

    private:
        void _read();
        void _clean_up();

    public:
        ~packet_t_t();

    private:
        uint16_t m_length;
        uint16_t m_len_body;
        uint32_t m_packet_usec;
        rtp_packet_t* m_body;
        rtpdump_t* m__root;
        rtpdump_t* m__parent;
        std::string m__raw_body;
        kaitai::kstream* m__io__raw_body;

    public:

        /**
         * packet length (including this header).
         */
        uint16_t length() const { return m_length; }

        /**
         * payload length.
         */
        uint16_t len_body() const { return m_len_body; }

        /**
         * timestamp of packet since the start.
         */
        uint32_t packet_usec() const { return m_packet_usec; }
        rtp_packet_t* body() const { return m_body; }
        rtpdump_t* _root() const { return m__root; }
        rtpdump_t* _parent() const { return m__parent; }
        std::string _raw_body() const { return m__raw_body; }
        kaitai::kstream* _io__raw_body() const { return m__io__raw_body; }
    };

private:
    header_t_t* m_file_header;
    std::vector<packet_t_t*>* m_packets;
    rtpdump_t* m__root;
    kaitai::kstruct* m__parent;

public:
    header_t_t* file_header() const { return m_file_header; }
    std::vector<packet_t_t*>* packets() const { return m_packets; }
    rtpdump_t* _root() const { return m__root; }
    kaitai::kstruct* _parent() const { return m__parent; }
};

#endif  // RTPDUMP_H_

rtpdump.cpp

// This is a generated file! Please edit source .ksy file and use kaitai-struct-compiler to rebuild

#include "rtpdump.h"
#include "kaitai/exceptions.h"

rtpdump_t::rtpdump_t(kaitai::kstream* p__io, kaitai::kstruct* p__parent, rtpdump_t* p__root) : kaitai::kstruct(p__io) {
    m__parent = p__parent;
    m__root = this;
    m_file_header = 0;
    m_packets = 0;

    try {
        _read();
    } catch(...) {
        _clean_up();
        throw;
    }
}

void rtpdump_t::_read() {
    m_file_header = new header_t_t(m__io, this, m__root);
    m_packets = new std::vector<packet_t_t*>();
    {
        int i = 0;
        while (!m__io->is_eof()) {
            m_packets->push_back(new packet_t_t(m__io, this, m__root));
            i++;
        }
    }
}

rtpdump_t::~rtpdump_t() {
    _clean_up();
}

void rtpdump_t::_clean_up() {
    if (m_file_header) {
        delete m_file_header; m_file_header = 0;
    }
    if (m_packets) {
        for (std::vector<packet_t_t*>::iterator it = m_packets->begin(); it != m_packets->end(); ++it) {
            delete *it;
        }
        delete m_packets; m_packets = 0;
    }
}

rtpdump_t::header_t_t::header_t_t(kaitai::kstream* p__io, rtpdump_t* p__parent, rtpdump_t* p__root) : kaitai::kstruct(p__io) {
    m__parent = p__parent;
    m__root = p__root;

    try {
        _read();
    } catch(...) {
        _clean_up();
        throw;
    }
}

void rtpdump_t::header_t_t::_read() {
    m_shebang = m__io->read_bytes(12);
    if (!(shebang() == std::string("\x23\x21\x72\x74\x70\x70\x6C\x61\x79\x31\x2E\x30", 12))) {
        throw kaitai::validation_not_equal_error<std::string>(std::string("\x23\x21\x72\x74\x70\x70\x6C\x61\x79\x31\x2E\x30", 12), shebang(), _io(), std::string("/types/header_t/seq/0"));
    }
    m_space = m__io->read_bytes(1);
    if (!(space() == std::string("\x20", 1))) {
        throw kaitai::validation_not_equal_error<std::string>(std::string("\x20", 1), space(), _io(), std::string("/types/header_t/seq/1"));
    }
    m_ip = kaitai::kstream::bytes_to_str(m__io->read_bytes_term(47, false, true, true), std::string("ascii"));
    m_port = kaitai::kstream::bytes_to_str(m__io->read_bytes_term(10, false, true, true), std::string("ascii"));
    m_start_sec = m__io->read_u4be();
    m_start_usec = m__io->read_u4be();
    m_ip2 = m__io->read_u4be();
    m_port2 = m__io->read_u2be();
    m_padding = m__io->read_u2be();
}

rtpdump_t::header_t_t::~header_t_t() {
    _clean_up();
}

void rtpdump_t::header_t_t::_clean_up() {
}

rtpdump_t::packet_t_t::packet_t_t(kaitai::kstream* p__io, rtpdump_t* p__parent, rtpdump_t* p__root) : kaitai::kstruct(p__io) {
    m__parent = p__parent;
    m__root = p__root;
    m_body = 0;
    m__io__raw_body = 0;

    try {
        _read();
    } catch(...) {
        _clean_up();
        throw;
    }
}

void rtpdump_t::packet_t_t::_read() {
    m_length = m__io->read_u2be();
    m_len_body = m__io->read_u2be();
    m_packet_usec = m__io->read_u4be();
    m__raw_body = m__io->read_bytes(len_body());
    m__io__raw_body = new kaitai::kstream(m__raw_body);
    m_body = new rtp_packet_t(m__io__raw_body);
}

rtpdump_t::packet_t_t::~packet_t_t() {
    _clean_up();
}

void rtpdump_t::packet_t_t::_clean_up() {
    if (m__io__raw_body) {
        delete m__io__raw_body; m__io__raw_body = 0;
    }
    if (m_body) {
        delete m_body; m_body = 0;
    }
}