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.0.tar.gz (29.2 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.0-py3-none-any.whl (22.9 kB view details)

Uploaded Python 3

File details

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

File metadata

  • Download URL: aiortp-0.1.0.tar.gz
  • Upload date:
  • Size: 29.2 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.0.tar.gz
Algorithm Hash digest
SHA256 c9bf0521480282d801be33f5203efb561e5cd007a276261973ca72d7cfa6eae2
MD5 fc35487e6d046847b05a8eabd54c3735
BLAKE2b-256 0d98145289b75ab0a6de9a0aac3ff0983bb92c2bf05acdb05d678c3a7e23a94f

See more details on using hashes here.

File details

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

File metadata

  • Download URL: aiortp-0.1.0-py3-none-any.whl
  • Upload date:
  • Size: 22.9 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.0-py3-none-any.whl
Algorithm Hash digest
SHA256 1e519645a17e6c90ae7c94397ac63eded19fbf6f12d3d7f7b31d9fe851e9fc25
MD5 643a96f16d3a08623ab6a27cc9cf8c63
BLAKE2b-256 abcc7cd7be189fd87f87a684431d89d988eb4618e508d602ced9e833ba56a5d4

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