Skip to main content

Crate rtmp_runtime

Crate rtmp_runtime 

Source
Expand description

Sans-IO RTMP 1.0 (the Adobe RTMP specification): the simple handshake, chunk-stream assembly and writing, protocol control and user control messages, AMF0 commands, and the session engines for both ends of a live push. server::ServerSession is an ingest server that accepts a publish from OBS or ffmpeg (connect, createStream, publish) and hands the audio, video and data back as FLV bytes; client::ClientSession publishes to a remote server, or plays from one in client::ClientMode::Play. The crate also owns the FLV tag syntax an RTMP message carries (flv).

transmux’s streaming FLV demuxer reads the FLV the server emits; multimux builds its RTMP input and push output, and broadcast-client its RTMP play source, on this crate. The core is no_std + alloc, opens no socket and reads no clock: bytes go in, bytes to write and events come out. Every bound (chunk::ChunkLimits, amf0::Amf0Limits, the session limits) is the caller’s. A discarded or undecodable message is reported as damage in order with the events around it; only a stream that cannot be framed is an error. The optional tokio feature adds socket adapters and target the rtmp[s]:// URL grammar.

§Choosing limits

No limit struct here has a Default. Each one offers for_profile(broadcast_core::Profile), a complete preset whose every number is a documented constant, and a typed with_* setter per field:

ProfileSized for
Embeddedone program, a few hundred MB of memory, 32-bit or wasm32, few elementary streams (values checked against the 256 MiB budget where exercised, #4834)
Devicesingle-service playback on a phone, tablet, TV or desktop, about 30 s of read-ahead
Servera live origin or HLS server carrying several programs
Headenda whole multiplex with full SI/EPG, up to 2048 PIDs

The presets: chunk::ChunkLimits, amf0::Amf0Limits, client::ClientLimits and server::ServerLimits. A preset delivers a damaged unit flagged; dropping it is the caller’s opt-in.

§Quickstart

Replay a real ffmpeg publish capture through the ingest server and collect the FLV it produces. Every bound is the caller’s:

use rtmp_runtime::amf0::Amf0Limits;
use rtmp_runtime::chunk::ChunkLimits;
use rtmp_runtime::server::{RtmpServerConfig, ServerEvent, ServerLimits, ServerSession};

let limits = ServerLimits {
    chunk: ChunkLimits {
        max_chunk_size: 1 << 20,
        max_message_len: 1 << 20,
        max_csids: 64,
        max_in_progress_bytes: 1 << 22,
    },
    amf0: Amf0Limits { max_depth: 16, max_values: 1024 },
    max_command_len: 16 * 1024,
    max_failed_publish_attempts: 3,
};
let capture = test_fixtures::read("rtmp/obs-publish.bin");
let mut session = ServerSession::new(RtmpServerConfig::new(limits, 4096, 2_500_000, 2_500_000));
let mut flv = Vec::new();
let mut published = false;
for chunk in capture.chunks(1400) {
    let (_reply, events) = session.handle_data(chunk).unwrap();
    for ev in events {
        match ev {
            ServerEvent::Connected { app } => assert_eq!(app, "live"),
            ServerEvent::Publish { stream_key, .. } => {
                assert_eq!(stream_key, "testkey");
                published = true;
            }
            ServerEvent::Media { flv: bytes } => flv.extend_from_slice(&bytes),
            _ => {}
        }
    }
}
assert!(published && flv.starts_with(b"FLV"));

§How do I…

§Modules

Sessions: server (the ingest state machine), client (publish and play).

Wire layer: handshake (C0/C1/C2 and S0/S1/S2), chunk (chunk stream assembly and writing), message (message types, protocol and user control), amf0 (values and commands), ids (chunk stream, message stream, sequence and timestamp identities), flv (FLV file, tag and tag-body syntax).

Sockets and URLs: io (feature tokio, the async server and client), target (feature target, the rtmp[s]:// URL).

Errors: error.

§Feature flags

§Spec citations

Adobe RTMP specification 1.0 §5.2 (handshake), §5.3 (chunk stream), §5.4 (protocol control), §6 (message format), §7.1 and §7.2 (message types and commands), transcribed in private/specs-verified/rtmp/rtmp-runtime/rtmp.md; AMF0 as the RTMP specification references it; the Adobe FLV and F4V file format specification v10.1 for the FLV tags.

Re-exports§

pub use error::RtmpError;
pub use message::ChunkSize;

Modules§

amf0
AMF0 (Action Message Format 0) value encoding/decoding, used by RTMP command and data messages.
chunk
RTMP chunk stream — basic header, message header, extended timestamp (Adobe RTMP 1.0 §5.3).
client
RTMP client session engine — connect → createStream → publish (Adobe RTMP 1.0 §7.2, NetConnection/NetStream commands).
error
Error type for the RTMP ingest session engine.
flv
FLV (Flash Video) tag framing — Adobe Flash Video File Format Specification v10.1, Annex E: the file header of §E.2 and the tag of §E.4.1.
handshake
RTMP handshake — C0/C1/C2 and S0/S1/S2 (Adobe RTMP 1.0 §5.2).
ids
The RTMP wire identities — §5.1.2 chunk stream id, §5.1.3 message stream id and timestamp, §5.3.1.1’s basic-header encodability rule (reference private/specs-verified/rtmp/rtmp-runtime/rtmp.md).
iotokio
Async tokio socket adapter driving the sans-IO ingest server session over a real tokio::net::TcpStream — the Layer-2 adapter (see private/specs-verified/rtmp/rtmp-runtime/rtmp.md § Layer-2 adapter; mirrors the rtsp_runtime::io tokio adapter shape in this same workspace).
message
RTMP message assembly from chunks, protocol control messages, and the message type catalogue (Adobe RTMP 1.0 §5.4, §6, §7.1).
presets
The broadcast_core::Profile presets of this crate’s bounds. Every number is a documented constant used only by a for_profile body; the caller replaces any field through its typed with_* setter. No preset refuses a message a real encoder sends.
server
RTMP ingest server session state machine — connect → createStream → publish (Adobe RTMP 1.0 §7.2, NetConnection/NetStream commands).
targettarget
rtmp[s]://host[:port]/app/stream-key -> typed target, built with the url crate. Replaces format!("rtmp://{host}:{port}/{app}"), which produces rtmp://::1:1935/live for an IPv6 address (defect 8) and leaks userinfo/query into tcUrl.

Constants§

RTMP_VERSION
The Adobe RTMP specification version this engine implements.