uvgRTP Documentation

repository·master·Indexed 19 days ago

https://github.com/ultravideo/uvgrtp

A high-performance C++ library for Real-Time Transport Protocol (RTP) media delivery optimized for low latency and CPU usage. It supports HEVC, VVC, and various audio formats, with secure delivery via SRTP and ZRTP. The library provides a C-style Core API for shared builds and a C++ Extended API for static builds, featuring a hierarchical architecture of context, session, and media_stream objects.

Tokens
7.6K
Snippets
26
Records
39
Agent score
64%

What's inside uvgRTP

  1. Overview of uvgRTP

    master

    uvgRTP is a C++ library designed for high-efficiency, real-time media delivery over the internet using the Real-Time Transport Protocol (RTP). It provides an intuitive API for transporting various video and audio codecs and supports End-to-End Encryption (E2EE) through SRTP and ZRTP.

    Supported Codecs

    • Video: Versatile Video Coding (VVC), High Efficiency Video Coding (HEVC), and Advanced Video Coding (AVC/H.264).
    • Audio: Opus.

    Supported Specifications

    • RTP Core: RFC 3350 (RTP Transport), RFC 3551 (Audio/Video Conferences).
    • Payload Formats: RFC 7798 (HEVC), RFC 6184 (H.264), RFC 7587 (Opus), and Draft VVC payload format.
    • Security: RFC 3711 (SRTP) and RFC 6189 (ZRTP).
  2. Understand the uvgRTP object hierarchy

    master

    The uvgRTP architecture is organized into three main hierarchical levels:

    1. uvgrtp::context: The top-level object. It manages IP address bindings and CNAME namespace isolation. Typically, an application only needs one context.
    2. uvgrtp::session: Allocated from a context. A session represents communication with a specific peer (local/remote address info) and handles ZRTP synchronization. The number of sessions usually matches the number of unique peers.
    3. uvgrtp::media_stream: Contained within a session. Each stream corresponds to a single bi-directional or uni-directional media stream (e.g., one audio stream and one video stream).

    If RTCP is enabled on a stream, you can access a uvgrtp::rtcp object from the media stream to send and receive RTCP packets.

  3. Configure Encryption (SRTP/ZRTP)

    master

    To enable End-to-End Encryption (E2EE), use the following flag combinations in create_stream:

    • RCE_SRTP | RCE_SRTP_KMNGMNT_ZRTP: Uses ZRTP for key negotiation.
    • RCE_SRTP | RCE_SRTP_KMNGMNT_USER: Uses user-provided keys. If using this mode, you must call add_srtp_ctx(key, salt) on the created stream.
    // Example for user-managed keys
    uvgrtp::media_stream *strm = sess->create_stream(8888, 8888, RTP_FORMAT_GENERIC, RCE_SRTP | RCE_SRTP_KMNGMNT_USER);
    strm->add_srtp_ctx(key, salt);
  4. Adjust MTU size for network compatibility

    master

    The default MTU size in uvgRTP is 1492 bytes. This accounts for UDP, IP, and RTP headers.

    If your traffic passes through tunnels (like VPNs) or uses IPv6 to IPv4 translation, which add extra header overhead, you may need to lower the MTU size to prevent IP-level fragmentation. Conversely, if your network supports larger packets, you can increase it.

  5. Detect network congestion using RTCP

    master

    When RTCP is enabled (via RCE_RTCP), you can monitor network health using the rtcp_report_block structure.

    By inspecting the fraction, lost, and jitter fields within the report block, your application can implement custom congestion control algorithms. Report blocks are generated by all media_stream entities receiving data and are included in both Sender Reports and Receiver Reports.

  6. Choose between Core API and Extended API

    master

    uvgRTP provides two API levels depending on your build type:

    • Core API: A C-style, ABI-stable interface. This is the only API available in shared/dynamic builds (.dll and .so). It is the recommended API for most users.
    • Extended API: A C++ interface using features like std::string and std::unique_ptr. This is only available in static builds of uvgRTP.
  7. Getting started with uvgRTP

    master

    To begin using uvgRTP, follow these resources:

    1. Tutorials: Follow the step-by-step guide in the USAGE.md file.
    2. Practical Examples: Explore the examples/ directory for working code illustrating various usage scenarios.
    3. API Reference: For a detailed description of all uvgRTP flags and advanced topics, consult the docs/README.md or the official Doxygen documentation.
  8. Configure User-managed SRTP encryption

    master

    If you prefer to manage SRTP keys and salts outside of uvgRTP, follow these steps:

    1. Enable user management by passing RCE_SRTP | RCE_SRTP_KMNGMNT_USER to create_stream().
    2. Immediately after create_stream() is called, you must provide the 128-bit keys and 112-bit salts to the uvgrtp::media_stream object using add_srtp_ctx().

    Warning: All other calls to the media_stream object will fail if they are made before add_srtp_ctx() has been called.

    // Example workflow for user-managed SRTP
    auto stream = create_stream(RCE_SRTP | RCE_SRTP_KMNGMNT_USER);
    stream.add_srtp_ctx(keys, salts);
    // Subsequent calls are now valid
  9. Cleanup uvgRTP resources

    master

    To prevent memory leaks and properly close connections, you must destroy resources in the reverse order of creation:

    1. Destroy the stream using sess->destroy_stream(strm).
    2. Destroy the session using ctx.destroy_session(sess).
    sess->destroy_stream(strm);
    ctx.destroy_session(sess);
  10. Enable SRTP encryption

    master

    Encryption can be enabled by passing specific flags to create_stream.

    There are two modes:

    1. ZRTP: Use RCE_SRTP | RCE_SRTP_KMNGMNT_ZRTP.
    2. User-managed: Use RCE_SRTP | RCE_SRTP_KMNGMNT_USER. This mode requires you to manually provide the key and salt using add_srtp_ctx(key, salt) on the created stream object.
    // Using ZRTP
    uvgrtp::media_stream *strm = sess->create_stream(8888, 8888, RTP_FORMAT_GENERIC, RCE_SRTP | RCE_SRTP_KMNGMNT_ZRTP);
    
    // Using User-managed keys
    uvgrtp::media_stream *strm = sess->create_stream(8888, 8888, RTP_FORMAT_GENERIC, RCE_SRTP | RCE_SRTP_KMNGMNT_USER);
    strm->add_srtp_ctx(key, salt);
  11. Configure ZRTP-based SRTP encryption

    master

    uvgRTP can automatically negotiate encryption keys using ZRTP. To enable this, you must provide a specific combination of flags to create_stream().

    Automatic Negotiation: Provide RCE_SRTP | RCE_SRTP_KMNGMNT_ZRTP to create_stream(). If using Multistream mode, also include RCE_ZRTP_MULTISTREAM_MODE for all relevant streams.

    Manual Negotiation: If you want to control when negotiation starts (rather than it happening automatically upon stream creation):

    1. Provide RCE_SRTP | RCE_ZRTP_DIFFIE_HELLMAN_MODE or RCE_SRTP | RCE_ZRTP_MULTISTREAM_MODE to create_stream().
    2. Configure media stream values (like SSRC) using configure_ctx().
    3. Trigger the negotiation manually using start_zrtp().
    // Example flag combination for automatic ZRTP
    create_stream(RCE_SRTP | RCE_SRTP_KMNGMNT_ZRTP | RCE_ZRTP_MULTISTREAM_MODE);
  12. Create and configure a media_stream

    master

    A uvgrtp::media_stream is used to send or receive actual media data.

    Creating a stream

    There are two constructor patterns:

    1. Full configuration: create_stream(local_port, remote_port, payload_format, flags)
    2. Single port configuration: create_stream(port, payload_format, flags). This is used in conjunction with RCE_SEND_ONLY or RCE_RECEIVE_ONLY flags.

    Configuration Flags

    Flags start with the RCE_ prefix and can be combined using bitwise OR (|).

    • RCE_RTCP: Enables RTCP.
    • RCE_NO_FLAGS: Default behavior.
    • RCE_SEND_ONLY / RCE_RECEIVE_ONLY: Used with the single-port constructor.

    Post-creation configuration

    You can modify stream behavior after creation using configure_ctx(key, value). Configuration keys start with the RCC_ prefix.

    // Full configuration (local port 8888, remote port 8888)
    uvgrtp::media_stream *strm = sess->create_stream(8888, 8888, RTP_FORMAT_GENERIC, RCE_NO_FLAGS);
    
    // Single port configuration (for receive-only)
    uvgrtp::media_stream *strm = sess->create_stream(8888, RTP_FORMAT_GENERIC, RCE_RECEIVE_ONLY);
    
    // Post-creation configuration
    strm->configure_ctx(RCC_MTU_SIZE, 2312);