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
//! StdioStream — PES frames via stdin/stdout with FMKV metadata header.
//!
//! The FMKV header carries stream metadata (PIDs, codecs, languages, codec_privates)
//! so the receiving end can set up muxing without scanning the content.
use super::meta;
use crate::disc::DiscTitle;
use std::io::{self, Write};
/// Stdio stream — reads PES from stdin, writes PES to stdout.
/// FMKV metadata header is written/read automatically.
pub struct StdioStream {
disc_title: DiscTitle,
reader: Option<io::Stdin>,
writer: Option<io::BufWriter<io::Stdout>>,
header_written: bool,
header_read: bool,
/// True once an FMKV header was actually parsed on the read side
/// (set only inside the `Some(meta)` arm). Distinct from
/// `header_read`, which is true after the first read attempt even
/// when no header was present — `headers_ready()` must gate on the
/// metadata actually being available, not merely on having looked.
meta_parsed: bool,
}
impl StdioStream {
/// Create a stdio stream for reading (stdin).
pub fn input() -> Self {
Self {
disc_title: DiscTitle::empty(),
reader: Some(io::stdin()),
writer: None,
header_written: false,
header_read: false,
meta_parsed: false,
}
}
/// Create a stdio stream for writing (stdout).
pub fn output(title: &DiscTitle) -> Self {
Self {
disc_title: title.clone(),
reader: None,
writer: Some(io::BufWriter::new(io::stdout())),
header_written: false,
header_read: false,
meta_parsed: false,
}
}
/// Write the FMKV metadata header to stdout exactly once, before any
/// frames. Always writes (even when the title has no streams) so a
/// zero-frame output stream still emits the magic + metadata header,
/// keeping the wire protocol symmetric with the read side's read_header().
fn ensure_header_written(&mut self) -> io::Result<()> {
if let Some(w) = &mut self.writer {
if !self.header_written {
let m = meta::M2tsMeta::from_title(&self.disc_title);
meta::write_header(w, &m)?;
self.header_written = true;
}
}
Ok(())
}
/// Read the FMKV metadata header from stdin on first read.
fn ensure_header_read(&mut self) -> io::Result<()> {
if self.header_read {
return Ok(());
}
self.header_read = true;
if let Some(ref mut r) = self.reader {
// Propagate real header errors. read_header consumes bytes
// from the unbuffered stdin BEFORE it can fail (oversized
// length, bad JSON, partial read), so swallowing the Err
// would leave the stream misaligned and PesFrame::deserialize
// would then read garbage. `?` surfaces the true error;
// Ok(None) (genuine magic mismatch / clean EOF) stays a
// non-error and leaves the empty default title in place.
if let Some(m) = meta::read_header(r)? {
self.disc_title = m.to_title();
self.meta_parsed = true;
}
}
Ok(())
}
}
impl crate::pes::Stream for StdioStream {
fn read(&mut self) -> io::Result<Option<crate::pes::PesFrame>> {
self.ensure_header_read()?;
match &mut self.reader {
Some(r) => crate::pes::PesFrame::deserialize(r),
None => Err(crate::error::Error::StreamWriteOnly.into()),
}
}
fn write(&mut self, frame: &crate::pes::PesFrame) -> io::Result<()> {
if self.writer.is_none() {
return Err(crate::error::Error::StreamReadOnly.into());
}
self.ensure_header_written()?;
match &mut self.writer {
Some(w) => frame.serialize(w),
None => Err(crate::error::Error::StreamReadOnly.into()),
}
}
fn finish(&mut self) -> io::Result<()> {
// Emit the header even when write() was never called, so a zero-frame
// title still produces the FMKV magic + metadata header on stdout
// (symmetric with the read side's read_header()).
self.ensure_header_written()?;
if let Some(w) = &mut self.writer {
w.flush()?;
}
Ok(())
}
fn info(&self) -> &DiscTitle {
&self.disc_title
}
fn codec_private(&self, track: usize) -> Option<Vec<u8>> {
// Single source of truth: the title's own codec_privates. (The
// previous `stored_codec_privates` field was a redundant clone
// of exactly this, populated from the same header.)
self.disc_title
.codec_privates
.get(track)
.and_then(|c| c.clone())
}
fn headers_ready(&self) -> bool {
// Write side: caller supplied the title up front, so headers are
// always ready. Read side: ready only once an FMKV header was
// actually parsed — gating on `header_read` alone would claim
// readiness for a headerless stream whose codec_private() is None
// for every track, starving the downstream MKV writer of init
// data. A genuinely headerless stream never flips ready (the
// caller must then fall back to its own codec detection).
self.writer.is_some() || self.meta_parsed
}
}