librtmp2 0.5.0

librtmp2 — RTMP/RTMPS protocol library
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
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
//! RTMP command message encoder/decoder
//!
//! Mirrors `src/message/command.h` and `src/message/command.c`.

use crate::amf::amf0;
use crate::buffer::Buffer;
use crate::ertmp::connect_amf;
use crate::types::CAPS_EX_MASK_MULTITRACK;
use crate::types::ConnectInfo;
use crate::types::ErrorCode;
use crate::types::NegotiatedCaps;
use crate::types::Result;

/// Maximum key/value pairs in a connect object
const MAX_CONNECT_OBJECT_KEYS: usize = 256;

/* ── Encoder ── */

/// Build a "connect" command.
pub fn build_connect(
    buf: &mut Buffer,
    app: &str,
    tc_url: &str,
    page_url: &str,
    swf_url: &str,
    flash_ver: &str,
    audio_codecs: i32,
    video_codecs: i32,
    caps: Option<&NegotiatedCaps>,
) -> Result<()> {
    macro_rules! chk {
        ($expr:expr) => {
            $expr.map_err(|_| ErrorCode::Internal)?
        };
    }

    chk!(amf0::write_string(buf, "connect"));
    chk!(amf0::write_number(buf, 1.0));
    chk!(amf0::write_object_begin(buf));
    chk!(amf0::write_object_key(buf, "app"));
    chk!(amf0::write_string(buf, app));
    chk!(amf0::write_object_key(buf, "type"));
    chk!(amf0::write_string(buf, "nonprivate"));

    chk!(amf0::write_object_key(buf, "tcUrl"));
    chk!(amf0::write_string(buf, tc_url));
    if !page_url.is_empty() {
        chk!(amf0::write_object_key(buf, "pageUrl"));
        chk!(amf0::write_string(buf, page_url));
    }
    if !swf_url.is_empty() {
        chk!(amf0::write_object_key(buf, "swfUrl"));
        chk!(amf0::write_string(buf, swf_url));
    }
    if !flash_ver.is_empty() {
        chk!(amf0::write_object_key(buf, "flashVer"));
        chk!(amf0::write_string(buf, flash_ver));
    }

    chk!(amf0::write_object_key(buf, "audioCodecs"));
    chk!(amf0::write_number(buf, audio_codecs as f64));
    chk!(amf0::write_object_key(buf, "videoCodecs"));
    chk!(amf0::write_number(buf, video_codecs as f64));
    if let Some(caps) = caps {
        connect_amf::write_negotiated_caps(buf, caps)?;
    }
    chk!(amf0::write_object_end(buf));

    Ok(())
}

/// Build a NetConnection `_error` response.
pub fn build_error(
    buf: &mut Buffer,
    transaction_id: f64,
    code: &str,
    description: &str,
) -> Result<()> {
    amf0::write_string(buf, "_error")?;
    amf0::write_number(buf, transaction_id)?;
    amf0::write_null(buf)?;
    amf0::write_object_begin(buf)?;
    amf0::write_object_key(buf, "level")?;
    amf0::write_string(buf, "error")?;
    amf0::write_object_key(buf, "code")?;
    amf0::write_string(buf, code)?;
    amf0::write_object_key(buf, "description")?;
    amf0::write_string(buf, description)?;
    amf0::write_object_end(buf)?;
    Ok(())
}

/// Build a "releaseStream" command.
pub fn build_release_stream(buf: &mut Buffer, stream_name: &str) -> Result<()> {
    amf0::write_string(buf, "releaseStream")?;
    amf0::write_number(buf, 2.0)?;
    amf0::write_null(buf)?;
    amf0::write_string(buf, stream_name)?;
    Ok(())
}

/// Build a "createStream" command.
pub fn build_create_stream(buf: &mut Buffer, transaction_id: f64) -> Result<()> {
    amf0::write_string(buf, "createStream")?;
    amf0::write_number(buf, transaction_id)?;
    amf0::write_null(buf)?;
    Ok(())
}

/// Build a "publish" command.
pub fn build_publish(buf: &mut Buffer, stream_name: &str, publish_type: &str) -> Result<()> {
    amf0::write_string(buf, "publish")?;
    amf0::write_number(buf, 0.0)?;
    amf0::write_null(buf)?;
    amf0::write_string(buf, stream_name)?;
    if !publish_type.is_empty() {
        amf0::write_string(buf, publish_type)?;
    }
    Ok(())
}

/// Build a "play" command.
pub fn build_play(buf: &mut Buffer, stream_name: &str) -> Result<()> {
    amf0::write_string(buf, "play")?;
    amf0::write_number(buf, 0.0)?;
    amf0::write_null(buf)?;
    amf0::write_string(buf, stream_name)?;
    Ok(())
}

/// Build a "FCPublish" command.
pub fn build_fcpublish(buf: &mut Buffer, stream_name: &str) -> Result<()> {
    amf0::write_string(buf, "FCPublish")?;
    amf0::write_number(buf, 0.0)?;
    amf0::write_null(buf)?;
    amf0::write_string(buf, stream_name)?;
    Ok(())
}

/// Build a "FCUnpublish" command.
pub fn build_fcunpublish(buf: &mut Buffer, stream_name: &str) -> Result<()> {
    amf0::write_string(buf, "FCUnpublish")?;
    amf0::write_number(buf, 0.0)?;
    amf0::write_null(buf)?;
    amf0::write_string(buf, stream_name)?;
    Ok(())
}

/// Build a "deleteStream" command.
pub fn build_deletestream(buf: &mut Buffer, transaction_id: f64, stream_id: u32) -> Result<()> {
    amf0::write_string(buf, "deleteStream")?;
    amf0::write_number(buf, transaction_id)?;
    amf0::write_null(buf)?;
    amf0::write_number(buf, stream_id as f64)?;
    Ok(())
}

/// Build a createStream _result response.
pub fn build_create_stream_result(
    buf: &mut Buffer,
    transaction_id: f64,
    stream_id: f64,
) -> Result<()> {
    amf0::write_string(buf, "_result")?;
    amf0::write_number(buf, transaction_id)?;
    amf0::write_null(buf)?;
    amf0::write_number(buf, stream_id)?;
    Ok(())
}

/// Build an onStatus command.
pub fn build_onstatus(buf: &mut Buffer, level: &str, code: &str, description: &str) -> Result<()> {
    amf0::write_string(buf, "onStatus")?;
    amf0::write_number(buf, 0.0)?;
    amf0::write_null(buf)?;
    amf0::write_object_begin(buf)?;
    amf0::write_object_key(buf, "level")?;
    amf0::write_string(buf, level)?;
    amf0::write_object_key(buf, "code")?;
    amf0::write_string(buf, code)?;
    amf0::write_object_key(buf, "description")?;
    amf0::write_string(buf, description)?;
    amf0::write_object_end(buf)?;
    Ok(())
}

/* ── Decoder ── */

/// Peek at the command name without consuming it.
pub fn peek_name(buf: &mut Buffer, out: &mut [u8]) -> Result<usize> {
    let saved_pos = buf.read_pos();
    let result = amf0::read_string(buf, out);
    buf.set_read_pos(saved_pos);
    result
}

/// Read a connect command.
pub fn read_connect(buf: &mut Buffer, info: &mut ConnectInfo) -> Result<()> {
    // Read command name
    let mut name = [0u8; 64];
    let name_len = amf0::read_string(buf, &mut name)?;
    info.name[..name_len].copy_from_slice(&name[..name_len]);

    // Read transaction ID
    info.transaction_id = read_number_value(buf)?;

    // Read command object
    amf0::read_object_begin(buf)?;

    // Parse key-value pairs
    let mut keys = 0;
    while !amf0::is_object_end(buf) {
        if keys >= MAX_CONNECT_OBJECT_KEYS {
            return Err(ErrorCode::Amf);
        }
        keys += 1;

        let mut key = [0u8; 256];
        let key_len = amf0::read_object_key(buf, &mut key)?;
        let key_str = std::str::from_utf8(&key[..key_len]).unwrap_or("");

        if key_str == "fourCcList" {
            if connect_amf::read_four_cc_list_amf(buf, &mut info.four_cc_list).is_ok() {
                info.has_four_cc_list = true;
            } else {
                let type_pos = buf.read_pos();
                let _ = amf0::read_type(buf);
                buf.set_read_pos(type_pos);
                amf0::skip_value(buf)?;
            }
            continue;
        }
        if key_str == "capsEx" {
            if connect_amf::read_caps_ex_amf(buf, &mut info.caps_ex, &mut info.caps_ex_mask).is_ok()
            {
                info.has_caps_ex = true;
            } else {
                let type_pos = buf.read_pos();
                let _ = amf0::read_type(buf);
                buf.set_read_pos(type_pos);
                amf0::skip_value(buf)?;
            }
            continue;
        }
        if key_str == "videoFourCcInfoMap" {
            if connect_amf::read_video_fourcc_info_map_amf(buf, &mut info.video_four_cc_info_map)
                .is_ok()
            {
                info.has_video_four_cc_info_map = true;
            } else {
                let type_pos = buf.read_pos();
                let _ = amf0::read_type(buf);
                buf.set_read_pos(type_pos);
                amf0::skip_value(buf)?;
            }
            continue;
        }
        if key_str == "reconnect" {
            if connect_amf::read_reconnect_amf(buf, &mut info.reconnect).is_ok() {
                info.has_reconnect = true;
            } else {
                let type_pos = buf.read_pos();
                let _ = amf0::read_type(buf);
                buf.set_read_pos(type_pos);
                amf0::skip_value(buf)?;
            }
            continue;
        }

        // Peek value type
        let type_pos = buf.read_pos();
        let value_type = amf0::read_type(buf)?;
        buf.set_read_pos(type_pos); // restore

        match value_type {
            amf0::Amf0Type::String => match key_str {
                "app" => read_string_checked(buf, &mut info.app)?,
                "tcUrl" => read_string_checked(buf, &mut info.tc_url)?,
                "pageUrl" => read_string_checked(buf, &mut info.page_url)?,
                "swfUrl" => read_string_checked(buf, &mut info.swf_url)?,
                "flashVer" => read_string_checked(buf, &mut info.flash_ver)?,
                _ => {
                    amf0::skip_value(buf)?;
                }
            },
            amf0::Amf0Type::Number => {
                let value = read_number_value(buf)?;
                match key_str {
                    "audioCodecs" => info.audio_codecs = value as i32,
                    "videoCodecs" => info.video_codecs = value as i32,
                    _ => {}
                }
            }
            _ => {
                amf0::skip_value(buf)?;
            }
        }
    }

    // Consume object end marker
    let mut end = [0u8; 3];
    buf.read(&mut end).map_err(|_| ErrorCode::Amf)?;

    Ok(())
}

/// Read a createStream command.
pub fn read_create_stream(buf: &mut Buffer) -> Result<f64> {
    let mut name = [0u8; 64];
    amf0::read_string(buf, &mut name)?;
    let txn = read_number_value(buf)?;
    amf0::skip_value(buf)?;
    Ok(txn)
}

/// Read a publish command.
pub fn read_publish(
    buf: &mut Buffer,
    stream_name: &mut [u8],
    publish_type: &mut [u8],
) -> Result<()> {
    let mut name = [0u8; 64];
    amf0::read_string(buf, &mut name)?;
    read_number_value(buf)?; // skip txn
    amf0::skip_value(buf)?;
    read_string_checked(buf, stream_name)?;

    // The publish type argument is optional in practice. Decode it only when a
    // client actually sent more AMF data; otherwise keep the output buffer empty.
    if buf.available() > 0 {
        let _ = read_string_checked(buf, publish_type);
    }
    Ok(())
}

/// Read a play command.
pub fn read_play(buf: &mut Buffer, stream_name: &mut [u8]) -> Result<()> {
    let mut name = [0u8; 64];
    amf0::read_string(buf, &mut name)?;
    read_number_value(buf)?; // skip txn
    amf0::skip_value(buf)?;
    read_string_checked(buf, stream_name)?;
    Ok(())
}

/// Read a pause command. Returns the pause flag (true = pause, false = unpause).
pub fn read_pause(buf: &mut Buffer) -> Result<bool> {
    let mut name = [0u8; 64];
    amf0::read_string(buf, &mut name)?;
    read_number_value(buf)?;
    amf0::skip_value(buf)?;
    read_bool_value(buf)
}

/// Read a seek command. Returns the target time in milliseconds.
pub fn read_seek(buf: &mut Buffer) -> Result<f64> {
    let mut name = [0u8; 64];
    amf0::read_string(buf, &mut name)?;
    read_number_value(buf)?;
    amf0::skip_value(buf)?;
    read_number_value(buf)
}

/// Read receiveAudio / receiveVideo: the boolean enable flag.
pub fn read_bool_command(buf: &mut Buffer) -> Result<bool> {
    let mut name = [0u8; 64];
    amf0::read_string(buf, &mut name)?;
    read_number_value(buf)?;
    amf0::skip_value(buf)?;
    read_bool_value(buf)
}

/// Read a closeStream command. Returns `None` when the peer sends the usual
/// three-argument form (stream id comes from the RTMP message stream id).
pub fn read_close_stream(buf: &mut Buffer) -> Result<Option<u32>> {
    let mut name = [0u8; 64];
    amf0::read_string(buf, &mut name)?;
    read_number_value(buf)?;
    amf0::skip_value(buf)?;
    if buf.available() == 0 {
        return Ok(None);
    }
    let type_pos = buf.read_pos();
    let ty = amf0::read_type(buf)?;
    if ty == amf0::Amf0Type::Number {
        Ok(Some(amf0::read_number(buf)? as u32))
    } else {
        buf.set_read_pos(type_pos);
        Ok(None)
    }
}

/// Build a pause command.
pub fn build_pause(buf: &mut Buffer, pause: bool) -> Result<()> {
    amf0::write_string(buf, "pause")?;
    amf0::write_number(buf, 0.0)?;
    amf0::write_null(buf)?;
    amf0::write_boolean(buf, pause)?;
    Ok(())
}

/// Build a seek command.
pub fn build_seek(buf: &mut Buffer, millis: f64) -> Result<()> {
    amf0::write_string(buf, "seek")?;
    amf0::write_number(buf, 0.0)?;
    amf0::write_null(buf)?;
    amf0::write_number(buf, millis)?;
    Ok(())
}

/// Read a connect _result response.
pub fn read_connect_result(buf: &mut Buffer) -> Result<f64> {
    read_connect_result_with_caps(buf, None)
}

/// Read a connect `_result` and optionally capture negotiated E-RTMP caps.
pub fn read_connect_result_with_caps(
    buf: &mut Buffer,
    caps: Option<&mut NegotiatedCaps>,
) -> Result<f64> {
    let mut name = [0u8; 64];
    amf0::read_string(buf, &mut name)?;
    let txn = read_number_value(buf)?;
    amf0::skip_value(buf)?;
    if let Some(caps) = caps {
        parse_connect_result_caps(buf, caps)?;
    } else {
        amf0::skip_value(buf)?;
    }
    Ok(txn)
}

fn parse_connect_result_caps(buf: &mut Buffer, caps: &mut NegotiatedCaps) -> Result<()> {
    let type_pos = buf.read_pos();
    let ty = amf0::read_type(buf)?;
    if ty != amf0::Amf0Type::Object {
        buf.set_read_pos(type_pos);
        amf0::skip_value(buf)?;
        return Ok(());
    }
    let mut keys = 0usize;
    while !amf0::is_object_end(buf) {
        keys += 1;
        if keys > amf0::MAX_OBJECT_KEYS {
            return Err(ErrorCode::Amf);
        }
        let mut key = [0u8; 256];
        let key_len = amf0::read_object_key(buf, &mut key)?;
        let key_str = std::str::from_utf8(&key[..key_len]).unwrap_or("");
        let type_pos = buf.read_pos();
        match key_str {
            "fourCcList" => {
                if connect_amf::read_four_cc_list_amf(buf, &mut caps.four_cc_list).is_ok() {
                    caps.has_four_cc_list = true;
                } else {
                    buf.set_read_pos(type_pos);
                    amf0::skip_value(buf)?;
                }
            }
            "capsEx" => {
                if connect_amf::read_caps_ex_amf(buf, &mut caps.caps_ex, &mut caps.caps_ex_mask)
                    .is_ok()
                {
                    caps.has_caps_ex = true;
                    caps.multitrack_enabled = (caps.caps_ex_mask & CAPS_EX_MASK_MULTITRACK) != 0;
                } else {
                    buf.set_read_pos(type_pos);
                    amf0::skip_value(buf)?;
                }
            }
            "videoFourCcInfoMap" => {
                if connect_amf::read_video_fourcc_info_map_amf(
                    buf,
                    &mut caps.video_four_cc_info_map,
                )
                .is_ok()
                {
                    caps.has_video_four_cc_info_map = true;
                } else {
                    buf.set_read_pos(type_pos);
                    amf0::skip_value(buf)?;
                }
            }
            "reconnect" => {
                if connect_amf::read_reconnect_amf(buf, &mut caps.reconnect).is_ok() {
                    caps.has_reconnect = true;
                } else {
                    buf.set_read_pos(type_pos);
                    amf0::skip_value(buf)?;
                }
            }
            _ => {
                amf0::skip_value(buf)?;
            }
        }
    }
    let mut end = [0u8; 3];
    buf.read(&mut end).map_err(|_| ErrorCode::Amf)?;
    Ok(())
}

/// Read a createStream _result response.
pub fn read_create_stream_result(buf: &mut Buffer) -> Result<(f64, f64)> {
    let mut name = [0u8; 64];
    amf0::read_string(buf, &mut name)?;
    let txn = read_number_value(buf)?;
    amf0::skip_value(buf)?;
    let stream_id = read_number_value(buf)?;
    Ok((txn, stream_id))
}

/// Read an onStatus command. Returns [`ErrorCode::Auth`] when level is `error`.
pub fn read_onstatus(buf: &mut Buffer) -> Result<()> {
    let mut name = [0u8; 64];
    amf0::read_string(buf, &mut name)?;
    read_number_value(buf)?;
    amf0::skip_value(buf)?;

    amf0::read_object_begin(buf)?;
    let mut level = [0u8; 32];
    let mut level_len = 0usize;
    let mut keys = 0usize;
    while !amf0::is_object_end(buf) {
        keys += 1;
        if keys > amf0::MAX_OBJECT_KEYS {
            return Err(ErrorCode::Amf);
        }
        let mut key = [0u8; 256];
        let key_len = amf0::read_object_key(buf, &mut key)?;
        let key_str = std::str::from_utf8(&key[..key_len]).unwrap_or("");
        if key_str == "level" {
            level_len = amf0::read_string(buf, &mut level)?;
        } else {
            amf0::skip_value(buf)?;
        }
    }

    let mut end = [0u8; 3];
    buf.read(&mut end).map_err(|_| ErrorCode::Amf)?;

    let level_str = std::str::from_utf8(&level[..level_len]).unwrap_or("");
    if level_str == "error" {
        return Err(ErrorCode::Auth);
    }
    Ok(())
}

/* ── Helpers ── */

fn read_number_value(buf: &mut Buffer) -> Result<f64> {
    let ty = amf0::read_type(buf)?;
    if ty != amf0::Amf0Type::Number {
        return Err(ErrorCode::Amf);
    }
    amf0::read_number(buf)
}

fn read_bool_value(buf: &mut Buffer) -> Result<bool> {
    let ty = amf0::read_type(buf)?;
    match ty {
        amf0::Amf0Type::Boolean => amf0::read_boolean(buf),
        amf0::Amf0Type::Number => Ok(amf0::read_number(buf)? != 0.0),
        _ => Err(ErrorCode::Amf),
    }
}

fn read_string_checked(buf: &mut Buffer, out: &mut [u8]) -> Result<()> {
    let mut byte = [0u8; 1];
    buf.read(&mut byte).map_err(|_| ErrorCode::Amf)?;
    if byte[0] != amf0::Amf0Type::String as u8 {
        return Err(ErrorCode::Amf);
    }
    let mut lb = [0u8; 2];
    buf.read(&mut lb).map_err(|_| ErrorCode::Amf)?;
    let slen = ((lb[0] as usize) << 8) | (lb[1] as usize);

    if buf.available() < slen {
        return Err(ErrorCode::Io);
    }

    if out.is_empty() {
        buf.drain(slen);
        return Ok(());
    }

    // Routing keys (app, stream name) and connect metadata must be stored
    // losslessly. Silently truncating long AMF strings lets two distinct peer
    // values collide on the same relay namespace.
    if slen >= out.len() {
        buf.drain(slen);
        return Err(ErrorCode::Amf);
    }

    buf.read(&mut out[..slen]).map_err(|_| ErrorCode::Amf)?;

    // The NUL byte at `out[slen]` is used as a sentinel by callers (and by
    // `decode_route_amf_string`) to find the end of the value. An embedded
    // NUL within the string content itself would let that sentinel scan
    // stop early, silently truncating the routing key and colliding two
    // distinct wire values (e.g. "live\0\xff..." and "live") onto the same
    // relay namespace. Reject embedded NULs outright.
    if out[..slen].contains(&0) {
        return Err(ErrorCode::Amf);
    }

    out[slen] = 0;
    Ok(())
}

/// Decode a NUL-terminated AMF string buffer into a relay routing key.
///
/// Routing keys must be lossless: invalid UTF-8 must not silently collapse
/// distinct wire values to the same empty namespace.
pub(crate) fn decode_route_amf_string(out: &[u8]) -> Result<String> {
    let len = out.iter().position(|&b| b == 0).unwrap_or(out.len());
    let value = std::str::from_utf8(&out[..len]).map_err(|_| ErrorCode::Amf)?;
    Ok(value.to_string())
}

#[cfg(test)]
mod tests {
    use super::*;

    fn cstr(buf: &[u8]) -> &str {
        let len = buf.iter().position(|&b| b == 0).unwrap_or(buf.len());
        std::str::from_utf8(&buf[..len]).unwrap()
    }

    #[test]
    fn read_connect_parses_four_cc_list_strict_array() {
        let mut buf = Buffer::new();
        amf0::write_string(&mut buf, "connect").unwrap();
        amf0::write_number(&mut buf, 1.0).unwrap();
        amf0::write_object_begin(&mut buf).unwrap();
        amf0::write_object_key(&mut buf, "fourCcList").unwrap();
        buf.write(&[0x0A, 0x00, 0x00, 0x00, 0x01]).unwrap();
        amf0::write_string(&mut buf, "av01").unwrap();
        amf0::write_object_key(&mut buf, "app").unwrap();
        amf0::write_string(&mut buf, "live").unwrap();
        amf0::write_object_end(&mut buf).unwrap();

        let mut info = ConnectInfo::default();
        read_connect(&mut buf, &mut info).unwrap();
        let app_len = info.app.iter().position(|&b| b == 0).unwrap_or(0);
        assert_eq!(std::str::from_utf8(&info.app[..app_len]).unwrap(), "live");
        assert!(info.has_four_cc_list);
        assert_eq!(info.four_cc_list.count, 1);
        assert_eq!(&info.four_cc_list.entries[0].cc[..4], b"av01");
    }

    #[test]
    fn read_string_checked_rejects_values_that_do_not_fit_output_buffer() {
        let mut buf = Buffer::new();
        let long = "x".repeat(256);
        amf0::write_string(&mut buf, &long).unwrap();

        let mut out = [0u8; 256];
        assert_eq!(read_string_checked(&mut buf, &mut out), Err(ErrorCode::Amf));
        assert_eq!(buf.available(), 0);
    }

    #[test]
    fn read_string_checked_accepts_values_up_to_buffer_capacity_minus_nul() {
        let mut buf = Buffer::new();
        let exact = "y".repeat(255);
        amf0::write_string(&mut buf, &exact).unwrap();

        let mut out = [0u8; 256];
        read_string_checked(&mut buf, &mut out).unwrap();
        assert_eq!(cstr(&out), exact);
    }

    #[test]
    fn read_publish_rejects_stream_names_longer_than_routing_buffer() {
        let mut buf = Buffer::new();
        build_publish(&mut buf, &"z".repeat(256), "live").unwrap();

        let mut stream_name = [0u8; 256];
        let mut publish_type = [0u8; 64];
        assert_eq!(
            read_publish(&mut buf, &mut stream_name, &mut publish_type),
            Err(ErrorCode::Amf)
        );
    }

    #[test]
    fn read_string_checked_with_empty_output_buffer_does_not_panic() {
        let mut buf = Buffer::new();
        amf0::write_string(&mut buf, "hello").unwrap();

        let mut out: [u8; 0] = [];
        assert!(read_string_checked(&mut buf, &mut out).is_ok());
        // The string bytes must still be fully consumed from the buffer.
        assert_eq!(buf.available(), 0);
    }

    #[test]
    fn decode_route_amf_string_rejects_invalid_utf8() {
        let mut buf = [0u8; 8];
        buf[0] = 0xFF;
        buf[1] = 0xFE;
        assert_eq!(decode_route_amf_string(&buf), Err(ErrorCode::Amf));
    }

    #[test]
    fn decode_route_amf_string_accepts_valid_utf8() {
        let mut buf = [0u8; 16];
        buf[..5].copy_from_slice(b"live\0");
        assert_eq!(decode_route_amf_string(&buf).unwrap(), "live");
    }

    #[test]
    fn read_publish_preserves_invalid_utf8_stream_name_bytes() {
        let mut buf = Buffer::with_capacity(128);
        amf0::write_string(&mut buf, "publish").unwrap();
        amf0::write_number(&mut buf, 1.0).unwrap();
        amf0::write_null(&mut buf).unwrap();
        buf.write(&[amf0::Amf0Type::String as u8, 0x00, 0x02, 0x80, 0x81])
            .unwrap();
        amf0::write_string(&mut buf, "live").unwrap();

        let mut stream_name = [0u8; 256];
        let mut publish_type = [0u8; 64];
        read_publish(&mut buf, &mut stream_name, &mut publish_type).unwrap();
        assert_eq!(stream_name[0], 0x80);
        assert_eq!(stream_name[1], 0x81);
        assert_eq!(decode_route_amf_string(&stream_name), Err(ErrorCode::Amf));
    }

    #[test]
    fn read_publish_rejects_stream_names_with_embedded_nul() {
        // "live\0\xff" must be rejected outright, not silently truncated to
        // "live" by the NUL sentinel used to mark the end of the value.
        let mut buf = Buffer::with_capacity(128);
        amf0::write_string(&mut buf, "publish").unwrap();
        amf0::write_number(&mut buf, 1.0).unwrap();
        amf0::write_null(&mut buf).unwrap();
        buf.write(&[amf0::Amf0Type::String as u8, 0x00, 0x06])
            .unwrap();
        buf.write(b"live\0\xff").unwrap();
        amf0::write_string(&mut buf, "live").unwrap();

        let mut stream_name = [0u8; 256];
        let mut publish_type = [0u8; 64];
        assert_eq!(
            read_publish(&mut buf, &mut stream_name, &mut publish_type),
            Err(ErrorCode::Amf)
        );
    }

    #[test]
    fn publish_command_round_trips_publish_type() {
        let mut buf = Buffer::new();
        build_publish(&mut buf, "stream_key", "record").unwrap();

        let mut stream_name = [0u8; 256];
        let mut publish_type = [0u8; 64];
        read_publish(&mut buf, &mut stream_name, &mut publish_type).unwrap();

        assert_eq!(cstr(&stream_name), "stream_key");
        assert_eq!(cstr(&publish_type), "record");
    }

    #[test]
    fn read_onstatus_rejects_error_level() {
        let mut buf = Buffer::new();
        build_onstatus(
            &mut buf,
            "error",
            "NetStream.Publish.BadName",
            "Publish not authorized",
        )
        .unwrap();

        assert_eq!(read_onstatus(&mut buf), Err(ErrorCode::Auth));
    }

    #[test]
    fn read_onstatus_accepts_status_level() {
        let mut buf = Buffer::new();
        build_onstatus(&mut buf, "status", "NetStream.Publish.Start", "Publishing").unwrap();

        assert!(read_onstatus(&mut buf).is_ok());
    }

    #[test]
    fn read_onstatus_rejects_excessive_object_keys() {
        let mut buf = Buffer::new();
        amf0::write_string(&mut buf, "onStatus").unwrap();
        amf0::write_number(&mut buf, 1.0).unwrap();
        amf0::write_null(&mut buf).unwrap();
        amf0::write_object_begin(&mut buf).unwrap();
        for i in 0..=amf0::MAX_OBJECT_KEYS {
            let key = format!("k{i}");
            amf0::write_object_key(&mut buf, &key).unwrap();
            amf0::write_boolean(&mut buf, false).unwrap();
        }
        amf0::write_object_key(&mut buf, "level").unwrap();
        amf0::write_string(&mut buf, "status").unwrap();
        amf0::write_object_end(&mut buf).unwrap();

        assert_eq!(read_onstatus(&mut buf), Err(ErrorCode::Amf));
    }

    #[test]
    fn read_close_stream_accepts_three_argument_form() {
        let mut buf = Buffer::new();
        amf0::write_string(&mut buf, "closeStream").unwrap();
        amf0::write_number(&mut buf, 2.0).unwrap();
        amf0::write_null(&mut buf).unwrap();

        assert_eq!(read_close_stream(&mut buf).unwrap(), None);
    }

    #[test]
    fn read_close_stream_accepts_explicit_stream_id() {
        let mut buf = Buffer::new();
        amf0::write_string(&mut buf, "closeStream").unwrap();
        amf0::write_number(&mut buf, 2.0).unwrap();
        amf0::write_null(&mut buf).unwrap();
        amf0::write_number(&mut buf, 7.0).unwrap();

        assert_eq!(read_close_stream(&mut buf).unwrap(), Some(7));
    }
}