pylibsrtp
pylibsrtp is a Python wrapper around the native libsrtp library, enabling Python applications to encrypt and decrypt Secure Real-time Transport Protocol (SRTP) packets. SRTP, as defined by RFC 3711, provides essential confidentiality, message authentication, and replay protection for RTP streams. The library is currently at version 1.0.0, released on October 13, 2025, and maintains a periodic release schedule to incorporate updates and fixes.
Warnings
- breaking The underlying libsrtp library, which pylibsrtp wraps, introduced breaking changes in versions 2.x, specifically concerning initialization vectors for AES 256 ICM and AES GCM ciphers when used with OpenSSL for RTCP. Ensure your libsrtp version is compatible with your usage patterns to avoid unexpected decryption failures or security issues.
- gotcha pylibsrtp requires the native libsrtp library (version 2.0 or better) to be installed on your system. `pip install pylibsrtp` only installs the Python bindings; it does not install the underlying C library. Failure to install libsrtp beforehand will result in build errors during pip installation or runtime errors due to missing shared libraries.
- gotcha When building pylibsrtp on macOS after installing `srtp` via Homebrew, you might need to set specific environment variables for the build process to correctly link against the installed libsrtp and OpenSSL libraries.
Install
-
pip install pylibsrtp
Imports
- Policy
from pylibsrtp import Policy
- Session
from pylibsrtp import Session
- Error
from pylibsrtp import Error
Quickstart
from pylibsrtp import Policy, Session
key = (b'\x00' * 30)
rtp_packet = b'\x80\x08\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00' + (b'\xd4' * 160)
# Create an outbound policy and session for protection
tx_policy = Policy(key=key, ssrc_type=Policy.SSRC_ANY_OUTBOUND)
tx_session = Session(policy=tx_policy)
srtp_packet = tx_session.protect(rtp_packet)
# Create an inbound policy and session for unprotection
rx_policy = Policy(key=key, ssrc_type=Policy.SSRC_ANY_INBOUND)
rx_session = Session(policy=rx_policy)
unprotected_rtp_packet = rx_session.unprotect(srtp_packet)
# Verify roundtrip
assert unprotected_rtp_packet == rtp_packet
print("SRTP protection and unprotection successful!")