librtmp2
A modern, open-source Rust library for Legacy RTMP and Enhanced RTMP v1/v2.
Overview
librtmp2 implements the lowest protocol layer of RTMP: handshake, chunking, AMF, commands, audio/video tags, Enhanced RTMP extensions, state machine, and a clean callback API.
It is designed to be embedded into custom servers, clients, relay tools, OBS/FFmpeg integrations, or any future product that needs a solid RTMP foundation.
What it is:
- A complete Legacy RTMP implementation (handshake, chunk streams, message reassembly, commands, control messages)
- E-RTMP v1 support: ExVideoTagHeader, FourCC codecs (
hvc1,av01,vp09), HDR metadata - E-RTMP v2 support: capability negotiation (
capsEx,videoFourCcInfoMap), reconnect, multitrack, ModEx - An idiomatic Rust API plus an
extern "C"FFI layer for use from C, Go, Python, PHP, and others - RTMPS (RTMP over TLS) via the optional
tlsCargo feature (OpenSSL), enabled by default
What it is not:
- Not an HTTP server or web UI
- Not a media server with business logic
- Not a push-relay to third-party platforms
- Not an FFmpeg wrapper
Architecture
OBS / FFmpeg / App
│
▼
librtmp2 ← this library
├── Handshake
├── Chunking
├── AMF (AMF0 / AMF3)
├── RTMP Commands
├── E-RTMP v1
└── E-RTMP v2
The library processes bytes, frames, commands, and protocol states. What a host program does with them is decided entirely via callbacks and configuration structures.
State Machine
TCP_ACCEPTED
→ HANDSHAKE
→ CONNECTED
→ CAPS_NEGOTIATED (E-RTMP v2)
→ APP_CONNECTED
→ STREAM_CREATED
→ PUBLISHING | PLAYING
→ CLOSING
→ CLOSED
Build
# Debug build
# Release build
# Run tests
# Build without TLS (no OpenSSL dependency)
The crate uses crate-type = ["cdylib", "staticlib", "lib"] and produces:
librtmp2.so/librtmp2.dll— cdylib for FFI consumerslibrtmp2.a/librtmp2.lib— staticlib for FFI consumers- Rust
lib— for direct Cargo dependency
TLS / RTMPS
RTMPS (RTMP over TLS) is supported via OpenSSL and is enabled by default via the tls Cargo feature. To produce a plaintext-only build without the optional TLS/OpenSSL dependency:
Call lrtmp2_tls_supported() at runtime to check whether the library was built with TLS.
Using as a Rust Crate
Add to your Cargo.toml:
[]
= { = "../librtmp2" }
Without TLS:
[]
= { = "../librtmp2", = false }
Using via the extern "C" FFI
The crate also builds as a cdylib/staticlib and exposes a stable extern "C" API for use from C, Go, Python, PHP, and others. See src/lib.rs for the full FFI surface.
Server
lrtmp2_server_t *;
void ;
int ;
int ;
void ;
Client
lrtmp2_client_t *;
void ;
int ;
int ;
int ;
int ;
int ;
Utilities
int ;
const char *;
int ;
int ;
int ;
const char *;
Repository Structure
librtmp2/
├── src/
│ ├── lib.rs Rust API + extern "C" FFI layer
│ ├── alloc.rs Custom allocator hook
│ ├── amf.rs AMF0 + AMF3 encoding/decoding
│ ├── buffer.rs Growable byte buffers
│ ├── bytes.rs Big-endian byte helpers
│ ├── chunk.rs Chunk reader/writer/state (per-csid)
│ ├── client.rs Outbound client: connect → publish/play
│ ├── ertmp.rs E-RTMP v1/v2 extensions
│ ├── flv.rs FLV audio/video/script tag parsing
│ ├── handshake.rs C0/C1/C2 ↔ S0/S1/S2
│ ├── log.rs Logging
│ ├── message.rs Message reassembly, control, commands
│ ├── net.rs Network helpers
│ ├── server.rs Listening socket, accept loop, per-connection poll
│ ├── session.rs Connection state machine, stream bookkeeping
│ ├── transport.rs TLS/plaintext transport abstraction
│ └── types.rs Shared types
├── tests/
│ ├── interop/
│ └── server_client_loopback.rs
├── build.rs
├── Cargo.toml
└── docs/
Roadmap
| Feature | Status |
|---|---|
| Legacy RTMP server — minimal | Implemented |
| Legacy RTMP client — minimal | Implemented |
| E-RTMP v1 receive (HEVC/AV1 detection) | Implemented |
| E-RTMP v1 send | Implemented |
| E-RTMP v2 capability layer | Implemented |
| Multitrack / reconnect / ModEx | Implemented |
| RTMPS (TLS) | Implemented |
| End-to-end test suites | In progress |
| Performance benchmarks | Planned |
License
MIT — see LICENSE