RTP Packet Decoder/Encoder
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
-
ModuleNotFoundError: No module named 'rtp'
cause The 'rtp' library is not installed in the current Python environment.fixRun `pip install rtp` to install the library. -
AttributeError: module 'rtp' has no attribute 'RTP'
cause Attempting to access the `RTP` class directly after a simple `import rtp` instead of `from rtp import RTP`, or a conflict with another library named 'rtp' (less likely for this specific library).fixUse `from rtp import RTP` to directly import the class, or ensure you are not shadowing another module. -
TypeError: 'bytes' object cannot be interpreted as an integer
cause This error or similar `TypeError` can occur when passing malformed byte data to `RTP.fromBytearray()`, or when attempting to use an incorrect type for a parameter during packet creation.fixEnsure that the byte array passed to `fromBytearray()` represents a valid RTP packet, and that all parameters for `RTP()` constructor are of the expected types and formats.
Warnings
- gotcha This library explicitly handles only RTP packet encoding/decoding and does NOT provide any network functionality (like sending over UDP) or specific payload handling. Users need to implement or integrate with other libraries for these aspects.
- breaking As a library in early development (version 0.0.4), expect potential breaking changes in minor versions (0.x.x) as the API matures. Always review release notes when upgrading.
- gotcha The quickstart examples and common usage patterns require helper functions (e.g., `getExtStartBits`, `getExtBody`, `getCSRCList`, `getNextPayload`) that are not part of the `rtp` library itself. Users must provide these based on their specific RTP implementation needs.
Install
-
pip install rtp
Imports
- RTP
from rtp import RTP
- Extension
from rtp import Extension
- PayloadType
from rtp import PayloadType
Quickstart
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}")