rtmp-runtime 0.13.0

Sans-IO RTMP 1.0 (Adobe Real-Time Messaging Protocol) publish engine for both ends: a driveable ingest server state machine and a publish client over the handshake, chunk stream, AMF0, and message layers; optional tokio socket adapter.
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
//! New client adapter (SP6.1) against `AsyncRtmpServer` over real loopback sockets,
//! and the bounded failure modes over `duplex` + paused time.
#![cfg(feature = "tokio")]

use std::io::ErrorKind;
use std::time::Duration;

use broadcast_core::ParseWith;
use broadcast_net::timeouts::Timeouts;
use rtmp_runtime::amf0::{Amf0Value, Command};
use rtmp_runtime::chunk::ChunkAssembler;
use rtmp_runtime::handshake::Handshake;
use rtmp_runtime::io::{
    AcceptedRtmpConnection, AsyncRtmpClient, AsyncRtmpServer, RtmpClientConfig, RtmpConnection,
    RtmpServerConfig,
};
use rtmp_runtime::message::{ChunkSize, MessageTypeId};
use rtmp_runtime::server::ServerEvent;
use rtmp_runtime::target::RtmpTarget;
use tokio::io::{AsyncReadExt, AsyncWriteExt};

fn timeouts() -> Timeouts {
    Timeouts::new(
        Duration::from_secs(10),
        Duration::from_secs(10),
        Duration::from_secs(30),
        Duration::from_secs(10),
    )
}

/// The tag type, timestamp and body of one FLV tag (Adobe FLV v10.1 Annex
/// E.4.1), skipping the file header when the run starts with one.
fn flv_tag(flv: &[u8]) -> (u8, u32, &[u8]) {
    let start = if flv.starts_with(b"FLV") { 13 } else { 0 };
    let tag_type = flv[start];
    let data_size =
        u32::from_be_bytes([0, flv[start + 1], flv[start + 2], flv[start + 3]]) as usize;
    let low = u32::from_be_bytes([0, flv[start + 4], flv[start + 5], flv[start + 6]]);
    let timestamp = low | (u32::from(flv[start + 7]) << 24);
    (
        tag_type,
        timestamp,
        &flv[start + 11..start + 11 + data_size],
    )
}

async fn publish_roundtrip(bind: &str, url_host: &str) {
    let server = match AsyncRtmpServer::bind(
        bind,
        RtmpServerConfig::new(crate::support::server_config(), timeouts()),
    )
    .await
    {
        Ok(s) => s,
        // Only a host genuinely without IPv6 loopback may skip; anything else is a failure.
        Err(e) if e.kind() == ErrorKind::AddrNotAvailable || e.raw_os_error() == Some(97) => {
            eprintln!("skipping {bind}: {e}");
            return;
        }
        Err(e) => panic!("bind {bind}: {e}"),
    };
    let port = server.local_addr().unwrap().port();
    let accepted = tokio::spawn(async move {
        let mut conn = server.accept().await.unwrap().into_connection();
        let mut seen = Vec::new();
        while let Some(batch) = conn.next_events().await.unwrap() {
            seen.extend(batch);
            if seen.iter().any(|e| matches!(e, ServerEvent::Media { .. })) {
                break;
            }
        }
        seen
    });
    let target = RtmpTarget::parse(&format!("rtmp://{url_host}:{port}/live/testkey")).unwrap();
    let mut c = AsyncRtmpClient::connect(RtmpClientConfig::new(
        target,
        crate::support::client_config(),
        timeouts(),
    ))
    .await
    .unwrap();
    c.publish().await.unwrap();
    c.send_video(
        rtmp_runtime::ids::Timestamp::from_millis(0),
        &[0x17, 0x01, 0, 0, 0, 0xDE, 0xAD],
    )
    .await
    .unwrap();
    let seen = accepted.await.unwrap();
    assert!(
        seen.iter()
            .any(|e| matches!(e, ServerEvent::Connected { app } if app == "live"))
    );
    assert!(
        seen.iter().any(
            |e| matches!(e, ServerEvent::Publish { stream_key, .. } if stream_key == "testkey")
        )
    );
    assert!(seen.iter().any(|e| matches!(e, ServerEvent::Media { .. })));
}

#[tokio::test]
async fn publishes_over_ipv4_loopback() {
    publish_roundtrip("127.0.0.1:0", "127.0.0.1").await;
}

/// Defect 8 end to end: the bracketed IPv6 URL both resolves and connects.
#[tokio::test]
async fn publishes_over_ipv6_loopback() {
    publish_roundtrip("[::1]:0", "[::1]").await;
}

#[tokio::test(start_paused = true)]
async fn publish_to_a_peer_that_never_answers_times_out_at_handshake() {
    let (client_io, _server_io) = tokio::io::duplex(8192);
    let target = RtmpTarget::parse("rtmp://h/live/k").unwrap();
    let config = RtmpClientConfig::new(target, crate::support::client_config(), timeouts())
        .with_timeouts(timeouts().with_handshake(Duration::from_secs(4)));
    assert_eq!(
        config.timeouts.handshake,
        Duration::from_secs(4),
        "the config carries the handshake bound"
    );
    let mut c = AsyncRtmpClient::from_stream(client_io, config);
    let t0 = tokio::time::Instant::now();
    let err = c.publish().await.expect_err("handshake deadline");
    assert_eq!(err.kind(), ErrorKind::TimedOut, "{err}");
    assert_eq!(t0.elapsed(), Duration::from_secs(4));
}

#[tokio::test]
async fn a_server_that_rejects_publish_surfaces_connection_refused() {
    let server = AsyncRtmpServer::bind(
        "127.0.0.1:0",
        RtmpServerConfig::new(crate::support::server_config(), timeouts()).with_session(
            crate::support::server_config().with_expected_stream_key(Some("right".into())),
        ),
    )
    .await
    .unwrap();
    let port = server.local_addr().unwrap().port();
    tokio::spawn(async move {
        let mut conn = server.accept().await.unwrap().into_connection();
        while let Ok(Some(_)) = conn.next_events().await {}
    });
    let target = RtmpTarget::parse(&format!("rtmp://127.0.0.1:{port}/live/wrong")).unwrap();
    let mut c = AsyncRtmpClient::connect(RtmpClientConfig::new(
        target,
        crate::support::client_config(),
        timeouts(),
    ))
    .await
    .unwrap();
    let err = c.publish().await.expect_err("BadName");
    assert_eq!(err.kind(), ErrorKind::ConnectionRefused, "{err}");
}

/// A client already publishing against a server that stops reading: `send_video` must hit the
/// write bound (review: the `queue` flush was unbounded and untested). Virtual-time hang guard so a
/// missing bound fails instead of hanging.
#[tokio::test(start_paused = true)]
async fn send_video_to_a_peer_that_stops_reading_times_out_the_write() {
    let (client_io, server_io) = tokio::io::duplex(4096);
    tokio::spawn(async move {
        let mut conn = RtmpConnection::from_stream(
            server_io,
            RtmpServerConfig::new(crate::support::server_config(), timeouts()),
        );
        while let Ok(Some(batch)) = conn.next_events().await {
            if batch
                .iter()
                .any(|e| matches!(e, ServerEvent::Publish { .. }))
            {
                std::future::pending::<()>().await; // stop reading, keep the pipe open
            }
        }
    });
    let target = RtmpTarget::parse("rtmp://h/live/k").unwrap();
    let mut c = AsyncRtmpClient::from_stream(
        client_io,
        RtmpClientConfig::new(target, crate::support::client_config(), timeouts())
            .with_timeouts(timeouts().with_write(Duration::from_secs(4))),
    );
    c.publish().await.unwrap();
    let t0 = tokio::time::Instant::now();
    let big = vec![0x42u8; 256 * 1024];
    let err = tokio::time::timeout(
        Duration::from_secs(60),
        c.send_video(rtmp_runtime::ids::Timestamp::from_millis(0), &big),
    )
    .await
    .expect("send_video must be bounded by `write`, not hang")
    .expect_err("peer is not reading");
    assert_eq!(err.kind(), ErrorKind::TimedOut, "{err}");
    assert_eq!(t0.elapsed(), Duration::from_secs(4));
}

/// The client's `next_events` honours `read_idle` once publishing (a silent server).
#[tokio::test(start_paused = true)]
async fn next_events_on_a_silent_server_times_out_at_read_idle() {
    let (client_io, server_io) = tokio::io::duplex(64 * 1024);
    tokio::spawn(async move {
        let mut conn = RtmpConnection::from_stream(
            server_io,
            RtmpServerConfig::new(crate::support::server_config(), timeouts()),
        );
        while let Ok(Some(batch)) = conn.next_events().await {
            if batch
                .iter()
                .any(|e| matches!(e, ServerEvent::Publish { .. }))
            {
                std::future::pending::<()>().await;
            }
        }
    });
    let target = RtmpTarget::parse("rtmp://h/live/k").unwrap();
    let mut c = AsyncRtmpClient::from_stream(
        client_io,
        RtmpClientConfig::new(target, crate::support::client_config(), timeouts())
            .with_timeouts(timeouts().with_read_idle(Duration::from_secs(5))),
    );
    c.publish().await.unwrap();
    let t0 = tokio::time::Instant::now();
    let err = loop {
        match tokio::time::timeout(Duration::from_secs(60), c.next_events())
            .await
            .expect("bounded by read_idle")
        {
            Ok(Some(_)) => continue,
            Ok(None) => panic!("closed"),
            Err(e) => break e,
        }
    };
    assert_eq!(err.kind(), ErrorKind::TimedOut, "{err}");
    assert!(t0.elapsed() <= Duration::from_secs(5));
}
/// Audit 22 (finding 6): the audio and metadata halves of the adapter — both
/// the sans-IO `encode_*` frames and the flushing `send_*` calls — reach a
/// real server with their payload and timestamp intact.
#[tokio::test]
async fn audio_and_metadata_traverse_the_adapter() {
    let server = AsyncRtmpServer::bind(
        "127.0.0.1:0",
        RtmpServerConfig::new(crate::support::server_config(), timeouts()),
    )
    .await
    .expect("bind loopback");
    let port = server.local_addr().expect("addr").port();
    let server_task = tokio::spawn(async move {
        let mut conn = server.accept().await.expect("accept").into_connection();
        let mut events = Vec::new();
        let mut media = 0usize;
        while let Ok(Ok(Some(batch))) =
            tokio::time::timeout(Duration::from_secs(5), conn.next_events()).await
        {
            media += batch
                .iter()
                .filter(|e| matches!(e, ServerEvent::Media { .. }))
                .count();
            events.extend(batch);
            if media >= 2 {
                break;
            }
        }
        events
    });
    let target = RtmpTarget::parse(&format!("rtmp://127.0.0.1:{port}/live/testkey")).unwrap();
    let mut client = AsyncRtmpClient::connect(RtmpClientConfig::new(
        target,
        crate::support::client_config(),
        timeouts(),
    ))
    .await
    .expect("connect");
    client.publish().await.expect("publish");

    let audio = client
        .encode_audio(rtmp_runtime::ids::Timestamp::from_millis(7), &[0xAF, 0x02])
        .expect("encode audio");
    let mut assembler = ChunkAssembler::new(crate::support::CHUNK_LIMITS);
    assembler.set_chunk_size(ChunkSize::new(4096).unwrap());
    let decoded = assembler.push(&audio).expect("decode audio frame");
    assert_eq!(decoded.len(), 1);
    assert_eq!(decoded[0].message_type_id, MessageTypeId::Audio);
    assert_eq!(decoded[0].payload, vec![0xAF, 0x02]);
    assert_eq!(decoded[0].timestamp.millis(), 7);

    let metadata = client
        .encode_metadata(&[
            ("width".to_string(), Amf0Value::Number(1280.0)),
            ("height".to_string(), Amf0Value::Number(720.0)),
        ])
        .expect("encode metadata");
    let mut assembler = ChunkAssembler::new(crate::support::CHUNK_LIMITS);
    assembler.set_chunk_size(ChunkSize::new(4096).unwrap());
    let decoded = assembler.push(&metadata).expect("decode metadata frame");
    assert_eq!(decoded.len(), 1);
    assert_eq!(decoded[0].message_type_id, MessageTypeId::DataAmf0);
    let mut expected = vec![0x02, 0x00, 0x0D];
    expected.extend_from_slice(b"@setDataFrame");
    expected.extend_from_slice(&[0x02, 0x00, 0x0A]);
    expected.extend_from_slice(b"onMetaData");
    assert!(
        decoded[0].payload.starts_with(&expected),
        "{:?}",
        decoded[0].payload
    );

    client
        .send_audio(rtmp_runtime::ids::Timestamp::from_millis(21), &[0xAF, 0x03])
        .await
        .expect("send audio");
    client
        .send_metadata(&[("width".to_string(), Amf0Value::Number(640.0))])
        .await
        .expect("send metadata");

    let seen = tokio::time::timeout(Duration::from_secs(30), server_task)
        .await
        .expect("server must finish")
        .expect("server task");
    let medias: Vec<&Vec<u8>> = seen
        .iter()
        .filter_map(|e| match e {
            ServerEvent::Media { flv } => Some(flv),
            _ => None,
        })
        .collect();
    assert_eq!(medias.len(), 2, "{seen:?}");

    let audio_tag = medias
        .iter()
        .find(|flv| flv_tag(flv).0 == 8)
        .expect("audio tag");
    assert_eq!(flv_tag(audio_tag), (8, 21, &[0xAF, 0x03][..]));

    let meta_tag = medias
        .iter()
        .find(|flv| flv_tag(flv).0 == 18)
        .expect("metadata tag");
    let (_, _, body) = flv_tag(meta_tag);
    assert!(body.windows(5).any(|w| w == b"width"), "{body:?}");
    assert!(!body.windows(6).any(|w| w == b"height"), "{body:?}");
}

/// Audit 22 (finding 6): the adapter's `connect` carries the `tcUrl` derived
/// from the target, not the client's `rtmp://localhost/<app>` fallback.
#[tokio::test]
async fn adapter_sends_the_configured_tc_url_in_connect() {
    let (client_io, mut server_io) = tokio::io::duplex(64 * 1024);
    let target = RtmpTarget::parse("rtmp://ingest.example/live/key").unwrap();
    let mut client = AsyncRtmpClient::from_stream(
        client_io,
        RtmpClientConfig::new(target, crate::support::client_config(), timeouts()),
    );

    let server = tokio::spawn(async move {
        let mut c0_c1 = vec![0u8; 1 + rtmp_runtime::handshake::HANDSHAKE_PACKET_LEN];
        server_io.read_exact(&mut c0_c1).await.expect("C0+C1");
        let mut handshake = Handshake::new();
        let (s0_s1_s2, _, _) = handshake.read(&c0_c1).expect("S0+S1+S2");
        server_io
            .write_all(&s0_s1_s2)
            .await
            .expect("write S0+S1+S2");
        let mut c2 = vec![0u8; rtmp_runtime::handshake::HANDSHAKE_PACKET_LEN];
        server_io.read_exact(&mut c2).await.expect("C2");

        let mut assembler = ChunkAssembler::new(crate::support::CHUNK_LIMITS);
        assembler.set_chunk_size(ChunkSize::new(4096).unwrap());
        let mut buf = [0u8; 4096];
        loop {
            let n = server_io.read(&mut buf).await.expect("read");
            if n == 0 {
                return None;
            }
            assembler.feed(&buf[..n]);
            while let Some(message) = assembler.next_message().expect("message") {
                if message.message_type_id != MessageTypeId::CommandAmf0 {
                    continue;
                }
                let command = Command::parse_with(&message.payload, &crate::support::AMF0_LIMITS)
                    .expect("command");
                if command.name == "connect" {
                    return Some(command);
                }
            }
        }
    });

    let client_task = tokio::spawn(async move { client.publish().await });
    let command = tokio::time::timeout(Duration::from_secs(30), server)
        .await
        .expect("server must finish")
        .expect("server task")
        .expect("connect command");
    let _ = tokio::time::timeout(Duration::from_secs(30), client_task).await;

    let tc_url = command
        .arguments
        .first()
        .and_then(|argument| match argument {
            Amf0Value::Object(pairs) => pairs
                .iter()
                .find(|(key, _)| key == "tcUrl")
                .map(|(_, value)| value.clone()),
            _ => None,
        });
    assert_eq!(
        tc_url,
        Some(Amf0Value::String("rtmp://ingest.example/live".to_string()))
    );
}

/// Audit 22 (finding 8): the debug output names the connection's peer.
#[test]
fn accepted_connection_debug_names_the_peer() {
    let (client_io, _server_io) = tokio::io::duplex(64);
    let conn = RtmpConnection::from_stream(
        client_io,
        RtmpServerConfig::new(crate::support::server_config(), timeouts()),
    );
    let accepted = AcceptedRtmpConnection::new(conn, "127.0.0.1:1935".parse().unwrap());
    let text = format!("{accepted:?}");
    assert!(text.contains("peer"), "{text}");
    assert!(text.contains("127.0.0.1:1935"), "{text}");
}