fix: resolve final whole-branch review findings

This commit is contained in:
2026-11-19 18:30:26 -05:00
parent a127c338d4
commit e3bd5bf615
12 changed files with 744 additions and 137 deletions

View File

@@ -3,7 +3,6 @@
from __future__ import annotations
import os
import shlex
import struct
import threading
import time
@@ -143,6 +142,26 @@ def _is_16_9_mode(mode: str, main_w: int, main_h: int) -> bool:
return False
class _FfmpegOutputTarget(str):
"""str subclass for picamera2.outputs.FfmpegOutput preserving path tokens.
Upstream FfmpegOutput.start() parses output_filename with plain str.split()
and no shell, so paths containing spaces or shlex quotes would break.
"""
_tokens: tuple[str, ...]
def __new__(cls, *tokens: str) -> "_FfmpegOutputTarget":
obj = super().__new__(cls, " ".join(tokens))
obj._tokens = tuple(tokens)
return obj
def split(self, sep: Optional[str] = None, maxsplit: int = -1) -> list[str]:
if sep is None and maxsplit == -1:
return list(self._tokens)
return super().split(sep, maxsplit)
def _probe_alsa_available() -> bool:
cards_path = "/proc/asound/cards"
if not os.path.exists(cards_path):
@@ -652,7 +671,7 @@ class Picam2Worker:
self._ensure_picamera2_imports()
# Ruling 3: staging files end in .mp4.part so ffmpeg needs -f mp4 explicitly.
output_target = f"-f mp4 -movflags +faststart {shlex.quote(tmp_path)}"
output_target = _FfmpegOutputTarget("-f", "mp4", "-movflags", "+faststart", tmp_path)
audio_recorded = False
if audio:

View File

@@ -59,7 +59,7 @@ class FramePipeWriter:
self._fd = frame_fd
self._owns_fd = owns_fd
self._cond = threading.Condition()
self._pending: Optional[tuple[int, int, int, int, bytes]] = None
self._pending: Optional[bytes] = None
self._stop = False
self._started = False
self._thread: Optional[threading.Thread] = None
@@ -110,15 +110,26 @@ class FramePipeWriter:
) -> None:
"""Queue the latest frame without blocking; drop any stale queued frame."""
data = bytes(payload)
header = pack_czfr_header(width, height, pixfmt, flags, len(data))
packet = header + data
with self._cond:
if self._stop:
return
if self._pending is not None:
self._frames_dropped += 1
self._pending = (width, height, pixfmt, flags, data)
self._pending = packet
self._frames_pushed += 1
self._cond.notify()
def _close_fd_locked(self) -> None:
if self._owns_fd and self._fd >= 0:
fd = self._fd
self._fd = -1
try:
os.close(fd)
except OSError:
pass
def stop(self, timeout: float = 1.0) -> None:
with self._cond:
self._stop = True
@@ -128,14 +139,10 @@ class FramePipeWriter:
if thread is not None and thread.is_alive():
thread.join(timeout=timeout)
with self._cond:
self._thread = None
self._started = False
if self._owns_fd and self._fd >= 0:
try:
os.close(self._fd)
except OSError:
pass
self._fd = -1
if thread is None or not thread.is_alive():
self._thread = None
self._started = False
self._close_fd_locked()
def _write_packet(self, packet: bytes) -> bool:
mv = memoryview(packet)
@@ -144,12 +151,24 @@ class FramePipeWriter:
try:
while offset < total:
with self._cond:
if self._stop:
if self._stop or self._fd < 0:
return False
fd = self._fd
try:
written = os.write(self._fd, mv[offset:])
written = os.write(fd, mv[offset:])
except BlockingIOError:
select.select([], [self._fd], [], 0.05)
while True:
with self._cond:
if self._stop or self._fd < 0:
return False
fd = self._fd
try:
select.select([], [fd], [], 0.05)
break
except InterruptedError:
continue
except (OSError, ValueError):
return False
continue
except (BrokenPipeError, OSError):
return False
@@ -161,24 +180,29 @@ class FramePipeWriter:
mv.release()
def _writer_loop(self) -> None:
while True:
with self._cond:
while self._pending is None and not self._stop:
self._cond.wait()
if self._stop:
return
frame = self._pending
self._pending = None
assert frame is not None
width, height, pixfmt, flags, payload = frame
header = pack_czfr_header(width, height, pixfmt, flags, len(payload))
if not self._write_packet(header + payload):
try:
while True:
with self._cond:
self._stop = True
return
while self._pending is None and not self._stop:
self._cond.wait()
if self._stop:
return
packet = self._pending
self._pending = None
assert packet is not None
if not self._write_packet(packet):
with self._cond:
self._stop = True
return
with self._cond:
self._frames_written += 1
finally:
with self._cond:
self._frames_written += 1
self._started = False
self._thread = None
if self._stop:
self._close_fd_locked()
class JsonProtocolServer:
@@ -338,10 +362,10 @@ class JsonProtocolServer:
buf.extend(chunk)
def run(self) -> int:
self._worker.start()
stdin_fd = self._get_stdin_fd() if self._wakeup_fd is not None else None
fd_buf = bytearray()
try:
self._worker.start()
while not self._shutdown_requested:
if stdin_fd is not None and self._wakeup_fd is not None:
raw_line = self._read_line_fd(stdin_fd, self._wakeup_fd, fd_buf)