RTP Packet Decoder/Encoder

0.0.4 · active · verified Thu Apr 16

The `rtp` library is a Python library for decoding, encoding, and interacting with Real-time Transport Protocol (RTP) packets. Currently at version 0.0.4, it is under active development by BBC R&D. This library is designed to be payload-agnostic and does not provide network functionality or handle specific payload bitstreams; it is intended to be used in conjunction with other libraries for those purposes.

Common errors

Warnings

Install

Imports

Quickstart

This quickstart demonstrates how to create and manipulate RTP packets. It includes examples for both encoding and decoding. Note that placeholder functions (e.g., `getExtStartBits`, `getNextPayload`) are used as the `rtp` library focuses solely on RTP packet structure, not on network transmission or specific payload content.

from rtp import RTP, Extension, PayloadType
from copy import deepcopy

# Placeholder functions for demonstration, as 'rtp' is payload-agnostic
def getExtStartBits(): return b'\x10\x00'
def getExtBody(): return b'\x01\x02\x03\x04'
def getCSRCList(): return [12345]
def getNextPayload(): return b'\xDE\xAD\xBE\xEF'
def transmit(rtp_packet): print(f"Transmitting: {rtp_packet.toBytearray().hex()}")
def getNextPacket(): return bytes.fromhex('80e000000000000000000000100001020304deadbeef')
def MyPayloadDecoder(payload_bytes): return f"Decoded payload: {payload_bytes.hex()}"
def render(decoded_data): print(f"Rendering: {decoded_data}")

# Encoding example
baseRTP = RTP(
    marker=True,
    payloadType=PayloadType.DYNAMIC_96, # Using a common dynamic payload type for example
    extension=Extension(
        startBits=getExtStartBits(),
        headerExtension=getExtBody()
    ),
    csrcList=getCSRCList()
)

# Initial packet
thisRTPBitstream = baseRTP.toBytearray()
print(f"Initial RTP Packet (hex): {thisRTPBitstream.hex()}")

# Example of sending subsequent packets (simplified loop)
nextRTP = deepcopy(baseRTP)
nextRTP.sequenceNumber += 1
nextRTP.timestamp += 160 # Example increment for timestamp
nextRTP.payload = getNextPayload()
transmit(nextRTP)

# Decoding example
decoded_rtp_packet = RTP().fromBytearray(getNextPacket())
decodedPayload = MyPayloadDecoder(decoded_rtp_packet.payload)
render(decodedPayload)

print(f"Decoded Sequence Number: {decoded_rtp_packet.sequenceNumber}")
print(f"Decoded Timestamp: {decoded_rtp_packet.timestamp}")

view raw JSON →