mirror of
https://github.com/bleackstorm/SteamFrame-FPV-Control-Center.git
synced 2026-12-03 07:31:57 +01:00
feat(radxa,android): autonomous Python usb0 DHCP server and Annex-B raw NAL framer for RubyFPV
This commit is contained in:
1 parent
5d4a9a40a4
commit
5b2b40d079
4 files changed
+283
-70
No files matched your search
@@ -352,6 +352,77 @@ impl RtpDepacketizer {
|
||||
}
|
||||
}
|
||||
|
||||
/// Réassemble un flux brut Annex-B découpé en morceaux UDP arbitraires (ex. RubyFPV `video_usb: 1 5600 1024`)
|
||||
/// en unités NAL complètes avec détection fiable de `is_keyframe` et `is_codec_config` (VPS/SPS/PPS).
|
||||
pub struct AnnexBFramer {
|
||||
codec: VideoCodec,
|
||||
buf: Vec<u8>,
|
||||
}
|
||||
|
||||
impl AnnexBFramer {
|
||||
const MAX_BUF_BYTES: usize = 512 * 1024;
|
||||
|
||||
pub fn new(codec: VideoCodec) -> Self {
|
||||
Self {
|
||||
codec,
|
||||
buf: Vec::with_capacity(64 * 1024),
|
||||
}
|
||||
}
|
||||
|
||||
fn find_start_code(data: &[u8], from: usize) -> Option<(usize, usize)> {
|
||||
let mut i = from;
|
||||
while i + 3 <= data.len() {
|
||||
if data[i] == 0 && data[i + 1] == 0 {
|
||||
if data[i + 2] == 1 {
|
||||
return Some((i, 3));
|
||||
}
|
||||
if i + 4 <= data.len() && data[i + 2] == 0 && data[i + 3] == 1 {
|
||||
return Some((i, 4));
|
||||
}
|
||||
}
|
||||
i += 1;
|
||||
}
|
||||
None
|
||||
}
|
||||
|
||||
pub fn push_chunk<F>(&mut self, chunk: &[u8], pts_us: u64, mut emit: F)
|
||||
where
|
||||
F: FnMut(VideoNalUnit),
|
||||
{
|
||||
if self.buf.len() + chunk.len() > Self::MAX_BUF_BYTES {
|
||||
self.buf.clear();
|
||||
}
|
||||
self.buf.extend_from_slice(chunk);
|
||||
|
||||
let Some((first_pos, first_sc_len)) = Self::find_start_code(&self.buf, 0) else {
|
||||
// Garde au maximum les 3 derniers octets au cas où un start code chevauche deux paquets
|
||||
if self.buf.len() > 3 {
|
||||
let keep = self.buf.len() - 3;
|
||||
self.buf.drain(..keep);
|
||||
}
|
||||
return;
|
||||
};
|
||||
if first_pos > 0 {
|
||||
self.buf.drain(..first_pos);
|
||||
}
|
||||
|
||||
let mut cur_sc_len = first_sc_len;
|
||||
while let Some((next_pos, next_sc_len)) = Self::find_start_code(&self.buf, cur_sc_len) {
|
||||
let nal_bytes = self.buf[..next_pos].to_vec();
|
||||
let nal_hdr = nal_bytes.get(cur_sc_len).copied().unwrap_or(0);
|
||||
let (is_keyframe, is_codec_config) = classify_nal(self.codec, nal_hdr);
|
||||
emit(VideoNalUnit {
|
||||
data: nal_bytes,
|
||||
pts_us,
|
||||
is_keyframe,
|
||||
is_codec_config,
|
||||
});
|
||||
self.buf.drain(..next_pos);
|
||||
cur_sc_len = next_sc_len;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// Récepteur vidéo UDP pur Rust (port `5600` par défaut) destiné au décodeur matériel Android (`MediaCodec`).
|
||||
pub struct UdpVideoReceiver {
|
||||
metrics: Arc<Mutex<UdpVideoMetrics>>,
|
||||
@@ -390,6 +461,7 @@ impl UdpVideoReceiver {
|
||||
.spawn(move || {
|
||||
let mut buf = [0u8; 65536];
|
||||
let mut depacketizer = RtpDepacketizer::new(codec);
|
||||
let mut annex_b_framer = AnnexBFramer::new(codec);
|
||||
let start_time = Instant::now();
|
||||
let mut last_packet_at: Option<Instant> = None;
|
||||
let mut last_frame_at: Option<Instant> = None;
|
||||
@@ -433,13 +505,11 @@ impl UdpVideoReceiver {
|
||||
);
|
||||
}
|
||||
StreamTransport::Raw => {
|
||||
let is_key = has_keyframe_annex_b(codec, &buf[..len]);
|
||||
emit_and_track(VideoNalUnit {
|
||||
data: buf[..len].to_vec(),
|
||||
annex_b_framer.push_chunk(
|
||||
&buf[..len],
|
||||
pts_us,
|
||||
is_keyframe: is_key,
|
||||
is_codec_config: false,
|
||||
});
|
||||
&mut emit_and_track,
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -494,29 +564,6 @@ impl Drop for UdpVideoReceiver {
|
||||
}
|
||||
}
|
||||
|
||||
fn has_keyframe_annex_b(codec: VideoCodec, bytes: &[u8]) -> bool {
|
||||
let mut i = 0usize;
|
||||
while i + 4 < bytes.len() {
|
||||
let sc = if bytes[i] == 0 && bytes[i + 1] == 0 && bytes[i + 2] == 1 {
|
||||
3
|
||||
} else if bytes[i] == 0 && bytes[i + 1] == 0 && bytes[i + 2] == 0 && bytes[i + 3] == 1 {
|
||||
4
|
||||
} else {
|
||||
i += 1;
|
||||
continue;
|
||||
};
|
||||
let nal_idx = i + sc;
|
||||
if nal_idx < bytes.len() {
|
||||
let (is_key, _) = classify_nal(codec, bytes[nal_idx]);
|
||||
if is_key {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
i = nal_idx + 1;
|
||||
}
|
||||
false
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
@@ -588,4 +635,21 @@ mod tests {
|
||||
assert!(out[0].is_keyframe);
|
||||
assert_eq!(out[0].data, vec![0, 0, 0, 1, 0x26, 0x01, 0x11, 0x22, 0x33, 0x44]);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_annex_b_framer_reassembles_chunked_ruby_stream() {
|
||||
let mut framer = AnnexBFramer::new(VideoCodec::H264);
|
||||
let mut out = Vec::new();
|
||||
// Flux brut coupé en deux morceaux au milieu d'un SPS et d'un IDR
|
||||
let chunk1 = [0x00, 0x00, 0x00, 0x01, 0x67, 0x42, 0x00];
|
||||
let chunk2 = [0x1E, 0x00, 0x00, 0x00, 0x01, 0x65, 0xAA, 0xBB, 0x00, 0x00, 0x00, 0x01, 0x41];
|
||||
framer.push_chunk(&chunk1, 1000, |n| out.push(n));
|
||||
assert!(out.is_empty());
|
||||
framer.push_chunk(&chunk2, 2000, |n| out.push(n));
|
||||
assert_eq!(out.len(), 2);
|
||||
assert!(out[0].is_codec_config);
|
||||
assert_eq!(out[0].data, vec![0, 0, 0, 1, 0x67, 0x42, 0x00, 0x1E]);
|
||||
assert!(out[1].is_keyframe);
|
||||
assert_eq!(out[1].data, vec![0, 0, 0, 1, 0x65, 0xAA, 0xBB]);
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user