diff --git a/c_engine/x11_engine.c b/c_engine/x11_engine.c index ccf5472..dcb0937 100644 --- a/c_engine/x11_engine.c +++ b/c_engine/x11_engine.c @@ -331,12 +331,30 @@ static int class_is_terminal(const char *value) { return 0; } -/* Returns 1 when the focused window belongs to a terminal emulator, else 0. */ +/* Returns 1 when the focused window belongs to a terminal emulator, else 0. + Cached by focused Window with a 1000ms TTL, so key bursts on the same window + avoid redundant 12-level XQueryTree walks, while focus changes invalidate immediately. */ int x11_terminal_shortcut_state(void) { + static Window cached_window = 0; + static int cached_terminal = 0; + static struct timespec last_query = {0, 0}; + if (!g_probe && !(g_probe = XOpenDisplay(NULL))) return 0; Window focus = 0; int revert = 0; XGetInputFocus(g_probe, &focus, &revert); - if (focus == None || focus == PointerRoot) return 0; + if (focus == None || focus == PointerRoot) { + cached_window = 0; + cached_terminal = 0; + return 0; + } + + struct timespec now; + clock_gettime(CLOCK_MONOTONIC, &now); + long diff_ms = (now.tv_sec - last_query.tv_sec) * 1000 + (now.tv_nsec - last_query.tv_nsec) / 1000000; + if (focus == cached_window && last_query.tv_sec != 0 && diff_ms >= 0 && diff_ms < 1000) { + return cached_terminal; + } + Window cur = focus; for (int depth = 0; depth < 12 && cur; depth++) { XClassHint hint = {0}; @@ -344,14 +362,28 @@ int x11_terminal_shortcut_state(void) { int terminal = class_is_terminal(hint.res_name) || class_is_terminal(hint.res_class); if (hint.res_name) XFree(hint.res_name); if (hint.res_class) XFree(hint.res_class); + cached_window = focus; + cached_terminal = terminal; + last_query = now; return terminal; } Window root, parent, *children = NULL; unsigned int n = 0; - if (!XQueryTree(g_probe, cur, &root, &parent, &children, &n)) return 0; + if (!XQueryTree(g_probe, cur, &root, &parent, &children, &n)) { + cached_window = 0; + cached_terminal = 0; + return 0; + } if (children) XFree(children); - if (parent == root) return 0; + if (parent == root) { + cached_window = 0; + cached_terminal = 0; + return 0; + } cur = parent; } + cached_window = focus; + cached_terminal = 0; + last_query = now; return 0; } diff --git a/src/hevc.rs b/src/hevc.rs index fad3a33..b862b7b 100644 --- a/src/hevc.rs +++ b/src/hevc.rs @@ -36,6 +36,46 @@ pub fn payload(mtu: usize, annex_b: &[u8]) -> Result, String> { Ok(out) } +/// Payload HEVC Annex-B bitstream with zero-copy sub-slicing for single NAL units <= MTU. +pub fn payload_bytes(mtu: usize, annex_b: &Bytes) -> Result, String> { + if mtu < 4 { return Err("HEVC payload MTU too small".into()); } + let mut starts = Vec::new(); + let mut i = 0; + let bytes = annex_b.as_ref(); + while i + 3 <= bytes.len() { + let prefix = if bytes[i..].starts_with(&[0, 0, 0, 1]) { 4 } + else if bytes[i..].starts_with(&[0, 0, 1]) { 3 } else { i += 1; continue; }; + starts.push((i, i + prefix)); i += prefix; + } + if starts.is_empty() { return Err("HEVC access unit has no Annex-B start code".into()); } + let mut out = Vec::new(); + for (index, &(_, start)) in starts.iter().enumerate() { + let mut end = starts.get(index + 1).map_or(bytes.len(), |s| s.0); + while end > start && bytes[end - 1] == 0 { end -= 1; } // trailing_zero_8bits + let nal_slice = &bytes[start..end]; + if nal_slice.len() < 2 || nal_slice[0] & 0x80 != 0 || nal_slice[1] & 7 == 0 { + return Err("Invalid HEVC NAL header".into()); + } + let kind = (nal_slice[0] >> 1) & 63; + if kind >= 48 { return Err("HEVC Annex-B contains RTP packet type".into()); } + if nal_slice.len() <= mtu { + // Zero-copy reference-counted sub-slice: + out.push(annex_b.slice(start..end)); + continue; + } + let chunks = nal_slice[2..].chunks(mtu - 3); + let last = chunks.len() - 1; + for (n, chunk) in chunks.enumerate() { + let mut fu = Vec::with_capacity(3 + chunk.len()); + // Keep F, LayerId and TID, replacing only the NAL type with FU=49. + fu.extend_from_slice(&[(nal_slice[0] & 0x81) | (49 << 1), nal_slice[1], + kind | if n == 0 { 0x80 } else { 0 } | if n == last { 0x40 } else { 0 }]); + fu.extend_from_slice(chunk); out.push(fu.into()); + } + } + Ok(out) +} + #[cfg(test)] mod tests { use super::*; diff --git a/src/video_track.rs b/src/video_track.rs index cda4ddf..501add9 100644 --- a/src/video_track.rs +++ b/src/video_track.rs @@ -57,7 +57,7 @@ impl VideoTrack { // Leave room for the RTP header and negotiated UU extensions below the 1200 byte MTU. let payloads = match codec { VideoCodec::H264 => state.h264.payload(1160, &sample.data).map_err(Error::Rtp)?, - VideoCodec::H265 => crate::hevc::payload(1160, &sample.data).map_err(Error::new)?, + VideoCodec::H265 => crate::hevc::payload_bytes(1160, &sample.data).map_err(Error::new)?, }; let count = payloads.len(); let mut complete = count > 0;