RTP (Real-time Transport Protocol): C++98/STL parsing library

The Real-time Transport Protocol (RTP) is a widely used network protocol for transmitting audio or video. It usually works with the RTP Control Protocol (RTCP). The transmission can be based on Transmission Control Protocol (TCP) or User Datagram Protocol (UDP).

This page hosts a formal specification of RTP (Real-time Transport Protocol) 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.bin", 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:
    rtp_packet_t data(&ks);
    

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

data.data() // => Payload without padding.
data.len_padding_if_exists() // => If padding bit is enabled, last byte of data contains number of
bytes appended to the payload as padding.

C++98/STL source code to parse RTP (Real-time Transport Protocol)

rtp_packet.h

#ifndef RTP_PACKET_H_
#define RTP_PACKET_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>

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

/**
 * The Real-time Transport Protocol (RTP) is a widely used network
 * protocol for transmitting audio or video. It usually works with the
 * RTP Control Protocol (RTCP). The transmission can be based on
 * Transmission Control Protocol (TCP) or User Datagram Protocol (UDP).
 */

class rtp_packet_t : public kaitai::kstruct {

public:
    class header_extention_t;

    enum payload_type_enum_t {
        PAYLOAD_TYPE_ENUM_PCMU = 0,
        PAYLOAD_TYPE_ENUM_RESERVED1 = 1,
        PAYLOAD_TYPE_ENUM_RESERVED2 = 2,
        PAYLOAD_TYPE_ENUM_GSM = 3,
        PAYLOAD_TYPE_ENUM_G723 = 4,
        PAYLOAD_TYPE_ENUM_DVI4_1 = 5,
        PAYLOAD_TYPE_ENUM_DVI4_2 = 6,
        PAYLOAD_TYPE_ENUM_LPC = 7,
        PAYLOAD_TYPE_ENUM_PCMA = 8,
        PAYLOAD_TYPE_ENUM_G722 = 9,
        PAYLOAD_TYPE_ENUM_L16_1 = 10,
        PAYLOAD_TYPE_ENUM_L16_2 = 11,
        PAYLOAD_TYPE_ENUM_QCELP = 12,
        PAYLOAD_TYPE_ENUM_CN = 13,
        PAYLOAD_TYPE_ENUM_MPA = 14,
        PAYLOAD_TYPE_ENUM_G728 = 15,
        PAYLOAD_TYPE_ENUM_DVI4_3 = 16,
        PAYLOAD_TYPE_ENUM_DVI4_4 = 17,
        PAYLOAD_TYPE_ENUM_G729 = 18,
        PAYLOAD_TYPE_ENUM_RESERVED19 = 19,
        PAYLOAD_TYPE_ENUM_UNASSIGNED20 = 20,
        PAYLOAD_TYPE_ENUM_UNASSIGNED21 = 21,
        PAYLOAD_TYPE_ENUM_UNASSIGNED22 = 22,
        PAYLOAD_TYPE_ENUM_UNASSIGNED23 = 23,
        PAYLOAD_TYPE_ENUM_UNASSIGNED24 = 24,
        PAYLOAD_TYPE_ENUM_CELB = 25,
        PAYLOAD_TYPE_ENUM_JPEG = 26,
        PAYLOAD_TYPE_ENUM_UNASSIGNED27 = 27,
        PAYLOAD_TYPE_ENUM_NV = 28,
        PAYLOAD_TYPE_ENUM_UNASSIGNED29 = 29,
        PAYLOAD_TYPE_ENUM_UNASSIGNED30 = 30,
        PAYLOAD_TYPE_ENUM_H261 = 31,
        PAYLOAD_TYPE_ENUM_MPV = 32,
        PAYLOAD_TYPE_ENUM_MP2T = 33,
        PAYLOAD_TYPE_ENUM_H263 = 34,
        PAYLOAD_TYPE_ENUM_MPEG_PS = 96
    };

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

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

public:
    ~rtp_packet_t();

    class header_extention_t : public kaitai::kstruct {

    public:

        header_extention_t(kaitai::kstream* p__io, rtp_packet_t* p__parent = 0, rtp_packet_t* p__root = 0);

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

    public:
        ~header_extention_t();

    private:
        uint16_t m_id;
        uint16_t m_length;
        rtp_packet_t* m__root;
        rtp_packet_t* m__parent;

    public:
        uint16_t id() const { return m_id; }
        uint16_t length() const { return m_length; }
        rtp_packet_t* _root() const { return m__root; }
        rtp_packet_t* _parent() const { return m__parent; }
    };

private:
    bool f_len_padding_if_exists;
    uint8_t m_len_padding_if_exists;
    bool n_len_padding_if_exists;

public:
    bool _is_null_len_padding_if_exists() { len_padding_if_exists(); return n_len_padding_if_exists; };

private:

public:

    /**
     * If padding bit is enabled, last byte of data contains number of
     * bytes appended to the payload as padding.
     */
    uint8_t len_padding_if_exists();

private:
    bool f_len_padding;
    uint8_t m_len_padding;

public:

    /**
     * Always returns number of padding bytes to in the payload.
     */
    uint8_t len_padding();

private:
    uint64_t m_version;
    bool m_has_padding;
    bool m_has_extension;
    uint64_t m_csrc_count;
    bool m_marker;
    payload_type_enum_t m_payload_type;
    uint16_t m_sequence_number;
    uint32_t m_timestamp;
    uint32_t m_ssrc;
    header_extention_t* m_header_extension;
    bool n_header_extension;

public:
    bool _is_null_header_extension() { header_extension(); return n_header_extension; };

private:
    std::string m_data;
    std::string m_padding;
    rtp_packet_t* m__root;
    kaitai::kstruct* m__parent;

public:
    uint64_t version() const { return m_version; }
    bool has_padding() const { return m_has_padding; }
    bool has_extension() const { return m_has_extension; }
    uint64_t csrc_count() const { return m_csrc_count; }
    bool marker() const { return m_marker; }
    payload_type_enum_t payload_type() const { return m_payload_type; }
    uint16_t sequence_number() const { return m_sequence_number; }
    uint32_t timestamp() const { return m_timestamp; }
    uint32_t ssrc() const { return m_ssrc; }
    header_extention_t* header_extension() const { return m_header_extension; }

    /**
     * Payload without padding.
     */
    std::string data() const { return m_data; }
    std::string padding() const { return m_padding; }
    rtp_packet_t* _root() const { return m__root; }
    kaitai::kstruct* _parent() const { return m__parent; }
};

#endif  // RTP_PACKET_H_

rtp_packet.cpp

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

#include "rtp_packet.h"

rtp_packet_t::rtp_packet_t(kaitai::kstream* p__io, kaitai::kstruct* p__parent, rtp_packet_t* p__root) : kaitai::kstruct(p__io) {
    m__parent = p__parent;
    m__root = this;
    m_header_extension = 0;
    f_len_padding_if_exists = false;
    f_len_padding = false;

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

void rtp_packet_t::_read() {
    m_version = m__io->read_bits_int_be(2);
    m_has_padding = m__io->read_bits_int_be(1);
    m_has_extension = m__io->read_bits_int_be(1);
    m_csrc_count = m__io->read_bits_int_be(4);
    m_marker = m__io->read_bits_int_be(1);
    m_payload_type = static_cast<rtp_packet_t::payload_type_enum_t>(m__io->read_bits_int_be(7));
    m__io->align_to_byte();
    m_sequence_number = m__io->read_u2be();
    m_timestamp = m__io->read_u4be();
    m_ssrc = m__io->read_u4be();
    n_header_extension = true;
    if (has_extension()) {
        n_header_extension = false;
        m_header_extension = new header_extention_t(m__io, this, m__root);
    }
    m_data = m__io->read_bytes(((_io()->size() - _io()->pos()) - len_padding()));
    m_padding = m__io->read_bytes(len_padding());
}

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

void rtp_packet_t::_clean_up() {
    if (!n_header_extension) {
        if (m_header_extension) {
            delete m_header_extension; m_header_extension = 0;
        }
    }
    if (f_len_padding_if_exists && !n_len_padding_if_exists) {
    }
}

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

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

void rtp_packet_t::header_extention_t::_read() {
    m_id = m__io->read_u2be();
    m_length = m__io->read_u2be();
}

rtp_packet_t::header_extention_t::~header_extention_t() {
    _clean_up();
}

void rtp_packet_t::header_extention_t::_clean_up() {
}

uint8_t rtp_packet_t::len_padding_if_exists() {
    if (f_len_padding_if_exists)
        return m_len_padding_if_exists;
    n_len_padding_if_exists = true;
    if (has_padding()) {
        n_len_padding_if_exists = false;
        std::streampos _pos = m__io->pos();
        m__io->seek((_io()->size() - 1));
        m_len_padding_if_exists = m__io->read_u1();
        m__io->seek(_pos);
        f_len_padding_if_exists = true;
    }
    return m_len_padding_if_exists;
}

uint8_t rtp_packet_t::len_padding() {
    if (f_len_padding)
        return m_len_padding;
    m_len_padding = ((has_padding()) ? (len_padding_if_exists()) : (0));
    f_len_padding = true;
    return m_len_padding;
}