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.2.0.tar.gz (33.9 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.2.0-py3-none-any.whl (25.1 kB view details)

Uploaded Python 3

File details

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

File metadata

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

File hashes

Hashes for aiortp-0.2.0.tar.gz
Algorithm Hash digest
SHA256 18385c94a2e28b2fce604ec329ebdd0fe1da8ff895896e0a0da29bdafd375336
MD5 2fdd3f17fc3084c2a89a90b574be131c
BLAKE2b-256 3791648ede29d55de4178bb05a93618b00d6e09bd548374025c0f28d4bf40eec

See more details on using hashes here.

File details

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

File metadata

  • Download URL: aiortp-0.2.0-py3-none-any.whl
  • Upload date:
  • Size: 25.1 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.2.0-py3-none-any.whl
Algorithm Hash digest
SHA256 feaf7c1b3df4a56d39d420d87c37424e7a1636ee3e8f8fb538887c26608de767
MD5 2a3f46f8a15c960641d7bb5342af6043
BLAKE2b-256 acbe284c91a55a7a651186afcc48ae478419d56b90648b7c9f20d74b827187ac

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