mirror of
https://github.com/cottongin/cardputerzero-camera.git
synced 2026-11-20 14:24:48 -06:00
fix: resolve final whole-branch review findings
This commit is contained in:
@@ -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:
|
||||
|
||||
@@ -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)
|
||||
|
||||
@@ -95,6 +95,7 @@ class FakePicamera2:
|
||||
self.pre_callback = None
|
||||
self.recording = False
|
||||
self.recording_attempts = []
|
||||
self.recording_argv_history = []
|
||||
self.captured_files = []
|
||||
self.direct_captures = []
|
||||
self.switched_captures = []
|
||||
@@ -170,8 +171,11 @@ class FakePicamera2:
|
||||
self.encoder = encoder
|
||||
self.output = output
|
||||
self.recording = True
|
||||
# Extract real path from output_filename string (after -f mp4 -movflags +faststart )
|
||||
path = output.output_filename.split()[-1].strip("'\"")
|
||||
# Match upstream picamera2 FfmpegOutput.start(), which splits
|
||||
# output_filename via plain str.split() without stripping shell quotes.
|
||||
tokens = output.output_filename.split()
|
||||
self.recording_argv_history.append(list(tokens))
|
||||
path = tokens[-1]
|
||||
with open(path, "wb") as f:
|
||||
f.write(b"\x00\x00\x00\x18ftypmp42MOCKMP4")
|
||||
|
||||
@@ -591,6 +595,53 @@ class Picam2WorkerTest(unittest.TestCase):
|
||||
self.assertEqual(payload[y_len : y_len + uv_len], b"\x80" * uv_len)
|
||||
self.assertEqual(payload[y_len + uv_len :], b"\x81" * uv_len)
|
||||
|
||||
def test_start_video_preserves_spaces_and_special_chars_in_path(self):
|
||||
worker, _, _ = self._make_worker()
|
||||
worker.start()
|
||||
worker.configure(
|
||||
mode="video",
|
||||
main_w=1280,
|
||||
main_h=720,
|
||||
preview_w=226,
|
||||
preview_h=128,
|
||||
fps=30,
|
||||
zoom=1.0,
|
||||
ev=0.0,
|
||||
transform="0",
|
||||
)
|
||||
picam = FakePicamera2.instances[-1]
|
||||
|
||||
with tempfile.TemporaryDirectory() as tmpdir:
|
||||
spaced_dir = os.path.join(tmpdir, "My Drive (1);test's")
|
||||
os.makedirs(spaced_dir, exist_ok=True)
|
||||
video_path = os.path.join(spaced_dir, ".VID_20261119_124000.mp4.part")
|
||||
|
||||
res = worker.start_video(
|
||||
tmp_path=video_path,
|
||||
width=1280,
|
||||
height=720,
|
||||
fps=30,
|
||||
bitrate=4000000,
|
||||
audio=False,
|
||||
)
|
||||
self.assertFalse(res["audio_recorded"])
|
||||
self.assertTrue(picam.recording)
|
||||
self.assertTrue(os.path.isfile(video_path))
|
||||
self.assertEqual(
|
||||
picam.recording_argv_history[-1],
|
||||
["-f", "mp4", "-movflags", "+faststart", video_path],
|
||||
)
|
||||
self.assertIsInstance(picam.output.output_filename, str)
|
||||
self.assertIn("-f mp4", picam.output.output_filename)
|
||||
self.assertIn("-movflags +faststart", picam.output.output_filename)
|
||||
self.assertIn(video_path, picam.output.output_filename)
|
||||
self.assertEqual(
|
||||
str(picam.output.output_filename),
|
||||
f"-f mp4 -movflags +faststart {video_path}",
|
||||
)
|
||||
stop_res = worker.stop_video()
|
||||
self.assertGreater(stop_res["bytes"], 0)
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
unittest.main()
|
||||
|
||||
@@ -1,3 +1,4 @@
|
||||
import io
|
||||
import json
|
||||
import os
|
||||
import select
|
||||
@@ -8,6 +9,7 @@ import tempfile
|
||||
import threading
|
||||
import time
|
||||
import unittest
|
||||
from unittest import mock
|
||||
|
||||
from czcamera_cam.protocol import (
|
||||
CZFR_HEADER_STRUCT,
|
||||
@@ -16,6 +18,7 @@ from czcamera_cam.protocol import (
|
||||
PIXFMT_RGB24,
|
||||
PIXFMT_YUV420,
|
||||
FramePipeWriter,
|
||||
JsonProtocolServer,
|
||||
pack_czfr_header,
|
||||
)
|
||||
|
||||
@@ -394,6 +397,120 @@ class ProtocolTest(unittest.TestCase):
|
||||
if proc.stderr:
|
||||
proc.stderr.close()
|
||||
|
||||
def test_frame_pipe_writer_validates_header_in_push_frame(self):
|
||||
r_fd, w_fd = os.pipe()
|
||||
thread_errors = []
|
||||
orig_hook = threading.excepthook
|
||||
threading.excepthook = lambda args: thread_errors.append(args)
|
||||
writer = FramePipeWriter(w_fd, owns_fd=False)
|
||||
writer.start()
|
||||
try:
|
||||
with self.assertRaises(ValueError):
|
||||
writer.push_frame(226, 170, 99, 0, b"\x00")
|
||||
with self.assertRaises(ValueError):
|
||||
writer.push_frame(70000, 170, PIXFMT_YUV420, 0, b"\x00")
|
||||
self.assertEqual(thread_errors, [])
|
||||
finally:
|
||||
writer.stop()
|
||||
threading.excepthook = orig_hook
|
||||
os.close(r_fd)
|
||||
os.close(w_fd)
|
||||
|
||||
def test_frame_pipe_writer_select_errors_and_safe_close(self):
|
||||
# 1. InterruptedError retries select, then OSError / ValueError exit cleanly without thread crash
|
||||
for terminal_exc in (OSError("EBADF"), ValueError("fd out of range")):
|
||||
r_fd, w_fd = os.pipe()
|
||||
thread_errors = []
|
||||
orig_hook = threading.excepthook
|
||||
threading.excepthook = lambda args: thread_errors.append(args)
|
||||
writer = FramePipeWriter(w_fd, owns_fd=False)
|
||||
select_calls = []
|
||||
|
||||
def fake_select(_r, wlist, _x, _timeout):
|
||||
select_calls.append(len(select_calls))
|
||||
if len(select_calls) == 1:
|
||||
raise InterruptedError("EINTR")
|
||||
raise terminal_exc
|
||||
|
||||
try:
|
||||
with mock.patch("czcamera_cam.protocol.os.write", side_effect=BlockingIOError), \
|
||||
mock.patch("czcamera_cam.protocol.select.select", side_effect=fake_select):
|
||||
writer.start()
|
||||
writer.push_frame(4, 4, PIXFMT_YUV420, 0, b"\x00" * 24)
|
||||
deadline = time.monotonic() + 2.0
|
||||
while len(select_calls) < 2 and time.monotonic() < deadline:
|
||||
time.sleep(0.01)
|
||||
time.sleep(0.05)
|
||||
self.assertEqual(len(select_calls), 2)
|
||||
self.assertEqual(thread_errors, [])
|
||||
finally:
|
||||
writer.stop()
|
||||
threading.excepthook = orig_hook
|
||||
os.close(r_fd)
|
||||
os.close(w_fd)
|
||||
|
||||
# 2. owns_fd=True must NOT close fd while writer thread is still alive on stop() timeout
|
||||
r_fd, w_fd = os.pipe()
|
||||
writer = FramePipeWriter(w_fd, owns_fd=True)
|
||||
write_started = threading.Event()
|
||||
release_write = threading.Event()
|
||||
closed_fds = []
|
||||
real_close = os.close
|
||||
|
||||
def slow_write(_fd, buf):
|
||||
write_started.set()
|
||||
release_write.wait(timeout=2.0)
|
||||
return len(buf)
|
||||
|
||||
def tracked_close(fd):
|
||||
closed_fds.append(fd)
|
||||
return real_close(fd)
|
||||
|
||||
try:
|
||||
with mock.patch("czcamera_cam.protocol.os.write", side_effect=slow_write), \
|
||||
mock.patch("czcamera_cam.protocol.os.close", side_effect=tracked_close):
|
||||
writer.start()
|
||||
writer.push_frame(4, 4, PIXFMT_YUV420, 0, b"\x00" * 24)
|
||||
self.assertTrue(write_started.wait(timeout=2.0))
|
||||
|
||||
# Timeout expires while slow_write is still blocked; fd must NOT be closed yet.
|
||||
writer.stop(timeout=0.02)
|
||||
self.assertNotIn(w_fd, closed_fds)
|
||||
|
||||
# Unblock writer thread; thread finishes and closes w_fd exactly once.
|
||||
release_write.set()
|
||||
deadline = time.monotonic() + 2.0
|
||||
while w_fd not in closed_fds and time.monotonic() < deadline:
|
||||
time.sleep(0.01)
|
||||
self.assertEqual(closed_fds.count(w_fd), 1)
|
||||
writer.stop(timeout=0.1)
|
||||
self.assertEqual(closed_fds.count(w_fd), 1)
|
||||
finally:
|
||||
release_write.set()
|
||||
real_close(r_fd)
|
||||
if w_fd not in closed_fds:
|
||||
real_close(w_fd)
|
||||
|
||||
def test_json_protocol_server_calls_shutdown_if_worker_start_raises(self):
|
||||
events = []
|
||||
|
||||
class FailingStartWorker:
|
||||
def start(self):
|
||||
events.append("start")
|
||||
raise RuntimeError("camera open failed")
|
||||
|
||||
def shutdown(self):
|
||||
events.append("shutdown")
|
||||
|
||||
server = JsonProtocolServer(
|
||||
worker_factory=lambda _emit: FailingStartWorker(),
|
||||
stdin=io.StringIO(""),
|
||||
stdout=io.StringIO(""),
|
||||
)
|
||||
with self.assertRaisesRegex(RuntimeError, "camera open failed"):
|
||||
server.run()
|
||||
self.assertEqual(events, ["start", "shutdown"])
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
unittest.main()
|
||||
|
||||
Reference in New Issue
Block a user