{"id":7691,"library":"rtp","title":"RTP Packet Decoder/Encoder","description":"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.","status":"active","version":"0.0.4","language":"en","source_language":"en","source_url":"https://github.com/bbc/rd-apmm-python-lib-rtp","tags":["networking","rtp","audio","video","real-time","packet-processing"],"install":[{"cmd":"pip install rtp","lang":"bash","label":"Install latest version"}],"dependencies":[],"imports":[{"symbol":"RTP","correct":"from rtp import RTP"},{"symbol":"Extension","correct":"from rtp import Extension"},{"symbol":"PayloadType","correct":"from rtp import PayloadType"}],"quickstart":{"code":"from rtp import RTP, Extension, PayloadType\nfrom copy import deepcopy\n\n# Placeholder functions for demonstration, as 'rtp' is payload-agnostic\ndef getExtStartBits(): return b'\\x10\\x00'\ndef getExtBody(): return b'\\x01\\x02\\x03\\x04'\ndef getCSRCList(): return [12345]\ndef getNextPayload(): return b'\\xDE\\xAD\\xBE\\xEF'\ndef transmit(rtp_packet): print(f\"Transmitting: {rtp_packet.toBytearray().hex()}\")\ndef getNextPacket(): return bytes.fromhex('80e000000000000000000000100001020304deadbeef')\ndef MyPayloadDecoder(payload_bytes): return f\"Decoded payload: {payload_bytes.hex()}\"\ndef render(decoded_data): print(f\"Rendering: {decoded_data}\")\n\n# Encoding example\nbaseRTP = RTP(\n    marker=True,\n    payloadType=PayloadType.DYNAMIC_96, # Using a common dynamic payload type for example\n    extension=Extension(\n        startBits=getExtStartBits(),\n        headerExtension=getExtBody()\n    ),\n    csrcList=getCSRCList()\n)\n\n# Initial packet\nthisRTPBitstream = baseRTP.toBytearray()\nprint(f\"Initial RTP Packet (hex): {thisRTPBitstream.hex()}\")\n\n# Example of sending subsequent packets (simplified loop)\nnextRTP = deepcopy(baseRTP)\nnextRTP.sequenceNumber += 1\nnextRTP.timestamp += 160 # Example increment for timestamp\nnextRTP.payload = getNextPayload()\ntransmit(nextRTP)\n\n# Decoding example\ndecoded_rtp_packet = RTP().fromBytearray(getNextPacket())\ndecodedPayload = MyPayloadDecoder(decoded_rtp_packet.payload)\nrender(decodedPayload)\n\nprint(f\"Decoded Sequence Number: {decoded_rtp_packet.sequenceNumber}\")\nprint(f\"Decoded Timestamp: {decoded_rtp_packet.timestamp}\")","lang":"python","description":"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."},"warnings":[{"fix":"Ensure you have separate components for network transport (e.g., `socket` module) and specific payload type encoding/decoding (e.g., `rtpPayload-ttml`).","message":"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.","severity":"gotcha","affected_versions":"0.0.1 - 0.0.4"},{"fix":"Consult the GitHub repository's release notes or changelog for specific breaking changes before upgrading. Pin exact versions in your `requirements.txt`.","message":"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.","severity":"breaking","affected_versions":"< 1.0.0"},{"fix":"Implement the necessary helper functions to generate extension headers, CSRC lists, and actual payload data according to your RTP stream specifications.","message":"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.","severity":"gotcha","affected_versions":"0.0.1 - 0.0.4"}],"env_vars":null,"last_verified":"2026-04-16T00:00:00.000Z","next_check":"2026-07-15T00:00:00.000Z","problems":[{"fix":"Run `pip install rtp` to install the library.","cause":"The 'rtp' library is not installed in the current Python environment.","error":"ModuleNotFoundError: No module named 'rtp'"},{"fix":"Use `from rtp import RTP` to directly import the class, or ensure you are not shadowing another module.","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).","error":"AttributeError: module 'rtp' has no attribute 'RTP'"},{"fix":"Ensure 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.","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.","error":"TypeError: 'bytes' object cannot be interpreted as an integer"}]}