Skip to main content

Asyncio RTP/RTCP audio library for Python

Project description

aiortp

Asyncio RTP/RTCP audio library for Python.

Plain RTP/RTCP for audio — no WebRTC, no ICE, no DTLS. Built for telephony and VoIP applications where you need direct control over RTP streams.

Portions derived from aiortc by Jeremy Lainé (BSD-3-Clause).

Features

  • Pure Python — zero required dependencies, Python >=3.11
  • AsyncIO native — built on asyncio.DatagramProtocol
  • G.711 codecs — µ-law (PCMU) and A-law (PCMA) with precomputed lookup tables
  • L16 codec — linear 16-bit PCM (s16le ↔ s16be)
  • Optional Opus — via opuslib (pip install aiortp[opus])
  • RTCP — Sender Reports, SDES, BYE with RFC 3550 randomized intervals
  • DTMF — RFC 4733 telephone-event send/receive with redundant end packets
  • Jitter buffer — extracted from aiortc, with configurable capacity and prefetch
  • Fully typed — PEP 561 py.typed marker included

Installation

pip install aiortp

With Opus support:

pip install aiortp[opus]

Quick Start

import asyncio
from aiortp import RTPSession, PayloadType

async def main():
    # Create two sessions on localhost
    session_a = await RTPSession.create(
        local_addr=("127.0.0.1", 10000),
        remote_addr=("127.0.0.1", 10002),
        payload_type=PayloadType.PCMU,
    )

    session_b = await RTPSession.create(
        local_addr=("127.0.0.1", 10002),
        remote_addr=("127.0.0.1", 10000),
        payload_type=PayloadType.PCMU,
    )

    # Receive callback
    def on_audio(data: bytes, timestamp: int) -> None:
        print(f"Received {len(data)} bytes, ts={timestamp}")

    session_b.on_audio = on_audio

    # Send PCM audio (auto-encoded to µ-law)
    pcm = b"\x00" * 320  # 160 samples of silence (20ms at 8kHz)
    for i in range(10):
        session_a.send_audio_pcm(pcm, timestamp=i * 160)

    await asyncio.sleep(1)

    await session_a.close()
    await session_b.close()

asyncio.run(main())

DTMF

# Send
session.send_dtmf("1", duration_ms=160, timestamp=0)

# Receive
def on_dtmf(digit: str, duration: int) -> None:
    print(f"Got DTMF: {digit}")

session.on_dtmf = on_dtmf

Codec Registry

from aiortp import get_codec, PayloadType

codec = get_codec(PayloadType.PCMU)  # or PCMA, L16
encoded = codec.encode(pcm_bytes)
decoded = codec.decode(encoded)

Low-Level Packets

from aiortp import RtpPacket, RtcpPacket, is_rtcp

# Parse
packet = RtpPacket.parse(data)
print(packet.sequence_number, packet.timestamp, packet.payload_type)

# Build
packet = RtpPacket(
    payload_type=0,
    sequence_number=1000,
    timestamp=8000,
    ssrc=0xDEADBEEF,
    payload=b"\x80" * 160,
)
data = packet.serialize()

# Demux RTP vs RTCP
if is_rtcp(data):
    rtcp_packets = RtcpPacket.parse(data)

Examples

See the examples/ directory:

  • loopback.py — two sessions exchanging G.711 audio on localhost
  • dtmf.py — sending and receiving DTMF digits
  • codec_roundtrip.py — encode/decode with each built-in codec
  • raw_packets.py — low-level RTP/RTCP packet construction

License

BSD-3-Clause. See LICENSE for details.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

aiortp-0.1.1.tar.gz (32.0 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

aiortp-0.1.1-py3-none-any.whl (24.0 kB view details)

Uploaded Python 3

File details

Details for the file aiortp-0.1.1.tar.gz.

File metadata

  • Download URL: aiortp-0.1.1.tar.gz
  • Upload date:
  • Size: 32.0 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.13.3

File hashes

Hashes for aiortp-0.1.1.tar.gz
Algorithm Hash digest
SHA256 d8749dc4a3376c9dd2d6ebcb7231e3398a9a1cb1a0c9a1b79534054e76db549a
MD5 29ecea29d57bc60bbd20448d26a404fe
BLAKE2b-256 93a4e1a5c149a8171b504e1fb4312b006b49797cc003906a4e706af6fafdfb8c

See more details on using hashes here.

File details

Details for the file aiortp-0.1.1-py3-none-any.whl.

File metadata

  • Download URL: aiortp-0.1.1-py3-none-any.whl
  • Upload date:
  • Size: 24.0 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.13.3

File hashes

Hashes for aiortp-0.1.1-py3-none-any.whl
Algorithm Hash digest
SHA256 22f646c67f9e23eeeb670346248462d75ef8afcf20b746adea7c882723531038
MD5 cb9cdc5bc2602e4e9a48ff4449a356f9
BLAKE2b-256 4cdbd01d3600851e8f5c94a4351842ce7809a68afbedf70f95a8484773135c3b

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page