mirror of
https://github.com/bleackstorm/SteamFrame-FPV-Control-Center.git
synced 2026-12-03 07:31:57 +01:00
perf(video): immediate Annex-B burst flush for Android and Condvar frame wake-up on PC
This commit is contained in:
1 parent
f75dd39bd5
commit
4fdffe3393
3 files changed
+98
-5
No files matched your search
@@ -421,6 +421,34 @@ impl AnnexBFramer {
|
|||||||
cur_sc_len = next_sc_len;
|
cur_sc_len = next_sc_len;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// Émet immédiatement le dernier NAL en attente (ex. paquet de fin de burst `< 1024` octets
|
||||||
|
/// sous RubyFPV ou silence UDP >= 2 ms) afin d'éliminer 1 frame entière (+16,7 ms à 60 fps)
|
||||||
|
/// de latence d'attente du start code suivant.
|
||||||
|
pub fn flush_pending<F>(&mut self, pts_us: u64, mut emit: F) -> bool
|
||||||
|
where
|
||||||
|
F: FnMut(VideoNalUnit),
|
||||||
|
{
|
||||||
|
let Some((pos, sc_len)) = Self::find_start_code(&self.buf, 0) else {
|
||||||
|
return false;
|
||||||
|
};
|
||||||
|
if pos > 0 {
|
||||||
|
self.buf.drain(..pos);
|
||||||
|
}
|
||||||
|
if self.buf.len() <= sc_len {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
let nal_hdr = self.buf[sc_len];
|
||||||
|
let (is_keyframe, is_codec_config) = classify_nal(self.codec, nal_hdr);
|
||||||
|
let data = std::mem::take(&mut self.buf);
|
||||||
|
emit(VideoNalUnit {
|
||||||
|
data,
|
||||||
|
pts_us,
|
||||||
|
is_keyframe,
|
||||||
|
is_codec_config,
|
||||||
|
});
|
||||||
|
true
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Récepteur vidéo UDP pur Rust (port `5600` par défaut) destiné au décodeur matériel Android (`MediaCodec`).
|
/// Récepteur vidéo UDP pur Rust (port `5600` par défaut) destiné au décodeur matériel Android (`MediaCodec`).
|
||||||
@@ -443,7 +471,13 @@ impl UdpVideoReceiver {
|
|||||||
{
|
{
|
||||||
let addr = format!("{}:{}", bind_ip, port);
|
let addr = format!("{}:{}", bind_ip, port);
|
||||||
let socket = UdpSocket::bind(&addr)?;
|
let socket = UdpSocket::bind(&addr)?;
|
||||||
socket.set_read_timeout(Some(Duration::from_millis(100)))?;
|
// En mode Raw Annex-B (RubyFPV), un timeout de 2 ms permet de flusher immédiatement
|
||||||
|
// la dernière trame dès la fin d'un burst UDP sans attendre le start code suivant.
|
||||||
|
let read_timeout = match transport {
|
||||||
|
StreamTransport::Raw => Duration::from_millis(2),
|
||||||
|
StreamTransport::Rtp => Duration::from_millis(50),
|
||||||
|
};
|
||||||
|
socket.set_read_timeout(Some(read_timeout))?;
|
||||||
log::info!(
|
log::info!(
|
||||||
"[UDP Video] En écoute sur {} ({:?}, {:?})",
|
"[UDP Video] En écoute sur {} ({:?}, {:?})",
|
||||||
addr,
|
addr,
|
||||||
@@ -510,6 +544,11 @@ impl UdpVideoReceiver {
|
|||||||
pts_us,
|
pts_us,
|
||||||
&mut emit_and_track,
|
&mut emit_and_track,
|
||||||
);
|
);
|
||||||
|
// RubyFPV découpe en paquets de 1024 octets (`video_usb: 1 5600 1024`) :
|
||||||
|
// un paquet plus court marque la fin immédiate de la trame.
|
||||||
|
if len < 1024 {
|
||||||
|
annex_b_framer.flush_pending(pts_us, &mut emit_and_track);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -518,7 +557,30 @@ impl UdpVideoReceiver {
|
|||||||
m.dropped_packets = depacketizer.dropped_packets;
|
m.dropped_packets = depacketizer.dropped_packets;
|
||||||
m.signal_live = true;
|
m.signal_live = true;
|
||||||
}
|
}
|
||||||
_ => {}
|
_ => {
|
||||||
|
if transport == StreamTransport::Raw {
|
||||||
|
let now = Instant::now();
|
||||||
|
let pts_us = start_time.elapsed().as_micros() as u64;
|
||||||
|
annex_b_framer.flush_pending(pts_us, |nal| {
|
||||||
|
if !nal.is_codec_config {
|
||||||
|
fps_frames = fps_frames.saturating_add(1);
|
||||||
|
if let Some(prev_f) = last_frame_at {
|
||||||
|
let dt_ms =
|
||||||
|
now.duration_since(prev_f).as_secs_f32() * 1000.0;
|
||||||
|
if dt_ms > 1.0 && dt_ms < 250.0 {
|
||||||
|
expected_interval_ms =
|
||||||
|
expected_interval_ms * 0.9 + dt_ms * 0.1;
|
||||||
|
let diff = (dt_ms - expected_interval_ms).abs();
|
||||||
|
jitter_ms_ewma =
|
||||||
|
jitter_ms_ewma * 0.85 + diff * 0.15;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
last_frame_at = Some(now);
|
||||||
|
}
|
||||||
|
on_nal(nal);
|
||||||
|
});
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
let win_elapsed = fps_window_start.elapsed();
|
let win_elapsed = fps_window_start.elapsed();
|
||||||
@@ -651,5 +713,10 @@ mod tests {
|
|||||||
assert_eq!(out[0].data, vec![0, 0, 0, 1, 0x67, 0x42, 0x00, 0x1E]);
|
assert_eq!(out[0].data, vec![0, 0, 0, 1, 0x67, 0x42, 0x00, 0x1E]);
|
||||||
assert!(out[1].is_keyframe);
|
assert!(out[1].is_keyframe);
|
||||||
assert_eq!(out[1].data, vec![0, 0, 0, 1, 0x65, 0xAA, 0xBB]);
|
assert_eq!(out[1].data, vec![0, 0, 0, 1, 0x65, 0xAA, 0xBB]);
|
||||||
|
// Le dernier NAL (P-frame 0x41) est émis immédiatement via flush_pending
|
||||||
|
assert!(framer.flush_pending(2000, |n| out.push(n)));
|
||||||
|
assert_eq!(out.len(), 3);
|
||||||
|
assert_eq!(out[2].data, vec![0, 0, 0, 1, 0x41]);
|
||||||
|
assert!(!framer.flush_pending(2000, |n| out.push(n)));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -178,9 +178,14 @@ pub fn run_desktop_fallback_loop(ctx: &mut EngineWorkerRefs<'_>) {
|
|||||||
);
|
);
|
||||||
|
|
||||||
let elapsed = loop_start.elapsed();
|
let elapsed = loop_start.elapsed();
|
||||||
let target_dt = Duration::from_millis(8); // Jusqu'à 120 Hz sur les flux 90/120 FPS
|
let target_dt = Duration::from_millis(8);
|
||||||
if elapsed < target_dt {
|
if elapsed < target_dt {
|
||||||
thread::sleep(target_dt - elapsed);
|
let remaining = target_dt - elapsed;
|
||||||
|
if let Some(rx) = ctx.video_rx_opt {
|
||||||
|
rx.wait_for_frame(last_frame_id, remaining);
|
||||||
|
} else {
|
||||||
|
thread::sleep(remaining);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
+22
-1
@@ -5,7 +5,7 @@ use gstreamer::prelude::*;
|
|||||||
use gstreamer_app as gst_app;
|
use gstreamer_app as gst_app;
|
||||||
use gstreamer_video as gst_video;
|
use gstreamer_video as gst_video;
|
||||||
use gstreamer_video::prelude::*;
|
use gstreamer_video::prelude::*;
|
||||||
use parking_lot::Mutex;
|
use parking_lot::{Condvar, Mutex};
|
||||||
use std::os::fd::{BorrowedFd, OwnedFd};
|
use std::os::fd::{BorrowedFd, OwnedFd};
|
||||||
use std::sync::atomic::{AtomicBool, AtomicU64, Ordering};
|
use std::sync::atomic::{AtomicBool, AtomicU64, Ordering};
|
||||||
use std::sync::Arc;
|
use std::sync::Arc;
|
||||||
@@ -101,6 +101,8 @@ pub struct FrameMailbox {
|
|||||||
/// Horodatage de la dernière trame publiée (conservé même après consommation par l'UI/VR
|
/// Horodatage de la dernière trame publiée (conservé même après consommation par l'UI/VR
|
||||||
/// pour éviter les faux positifs "Signal vidéo perdu" entre deux trames).
|
/// pour éviter les faux positifs "Signal vidéo perdu" entre deux trames).
|
||||||
last_received_at: Option<Instant>,
|
last_received_at: Option<Instant>,
|
||||||
|
/// Signal événementiel pour réveiller instantanément la boucle Bureau dès qu'une trame arrive.
|
||||||
|
frame_condvar: Arc<Condvar>,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl FrameMailbox {
|
impl FrameMailbox {
|
||||||
@@ -114,12 +116,14 @@ impl FrameMailbox {
|
|||||||
if self.spare_buf.capacity() > incoming.capacity() {
|
if self.spare_buf.capacity() > incoming.capacity() {
|
||||||
std::mem::swap(&mut self.spare_buf, incoming);
|
std::mem::swap(&mut self.spare_buf, incoming);
|
||||||
}
|
}
|
||||||
|
self.frame_condvar.notify_all();
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Publie une trame DMA-BUF zéro-copie (sans aucune copie pixel côté CPU).
|
/// Publie une trame DMA-BUF zéro-copie (sans aucune copie pixel côté CPU).
|
||||||
fn publish_dmabuf(&mut self, dmabuf: DmaBufFrame) {
|
fn publish_dmabuf(&mut self, dmabuf: DmaBufFrame) {
|
||||||
self.last_received_at = Some(dmabuf.received_at);
|
self.last_received_at = Some(dmabuf.received_at);
|
||||||
self.dmabuf_ready = Some(dmabuf);
|
self.dmabuf_ready = Some(dmabuf);
|
||||||
|
self.frame_condvar.notify_all();
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Récupère la dernière trame DMA-BUF si plus récente que `last_frame_id` et fraîche.
|
/// Récupère la dernière trame DMA-BUF si plus récente que `last_frame_id` et fraîche.
|
||||||
@@ -323,6 +327,23 @@ impl UsbVideoReceiver {
|
|||||||
.take_if_newer(consumer_buf, last_frame_id, max_age)
|
.take_if_newer(consumer_buf, last_frame_id, max_age)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// Attend de manière événementielle (`Condvar`) l'arrivée d'une trame plus récente que `last_frame_id`
|
||||||
|
/// (jusqu'à `timeout`), supprimant le délai de polling fixe dans la boucle Bureau.
|
||||||
|
pub fn wait_for_frame(&self, last_frame_id: u64, timeout: Duration) {
|
||||||
|
let mut guard = self.mailbox.lock();
|
||||||
|
let has_newer = guard
|
||||||
|
.ready_meta
|
||||||
|
.is_some_and(|m| m.frame_id != last_frame_id)
|
||||||
|
|| guard
|
||||||
|
.dmabuf_ready
|
||||||
|
.as_ref()
|
||||||
|
.is_some_and(|d| d.frame_id != last_frame_id);
|
||||||
|
if !has_newer {
|
||||||
|
let cv = Arc::clone(&guard.frame_condvar);
|
||||||
|
cv.wait_for(&mut guard, timeout);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
pub fn metrics(&self) -> VideoLinkMetrics {
|
pub fn metrics(&self) -> VideoLinkMetrics {
|
||||||
self.metrics.lock().clone()
|
self.metrics.lock().clone()
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in new issue
Block a user