camera.py 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711
  1. """Camera capture service for Bambu Lab printers.
  2. Supports two camera protocols:
  3. - RTSP: Used by X1, X1C, X1E, X2D, H2C, H2D, H2DPRO, H2S, P2S (port 322)
  4. - Chamber Image: Used by A1, A1MINI, P1P, P1S (port 6000, custom binary protocol)
  5. """
  6. import asyncio
  7. import logging
  8. import os
  9. import shutil
  10. import ssl
  11. import struct
  12. import subprocess
  13. import uuid
  14. from datetime import datetime
  15. from pathlib import Path
  16. logger = logging.getLogger(__name__)
  17. # JPEG markers
  18. JPEG_START = b"\xff\xd8"
  19. JPEG_END = b"\xff\xd9"
  20. # Cache the ffmpeg path after first lookup
  21. _ffmpeg_path: str | None = None
  22. # Cached result of rtsp_socket_timeout_flag(); see that function for context.
  23. _rtsp_socket_timeout_flag: str | None = None
  24. # Track PIDs of ffmpeg processes spawned for one-shot frame capture (snapshot).
  25. # The cleanup task in routes/camera.py checks this set to avoid killing active captures.
  26. _active_capture_pids: set[int] = set()
  27. def get_ffmpeg_path() -> str | None:
  28. """Find the ffmpeg executable path.
  29. Uses shutil.which first, then checks common installation locations
  30. for systems where PATH may be limited (e.g., systemd services).
  31. """
  32. global _ffmpeg_path
  33. if _ffmpeg_path is not None:
  34. return _ffmpeg_path
  35. # Try PATH first
  36. ffmpeg_path = shutil.which("ffmpeg")
  37. # If not found via PATH, check common installation locations
  38. if ffmpeg_path is None:
  39. common_paths = [
  40. "/usr/bin/ffmpeg",
  41. "/usr/local/bin/ffmpeg",
  42. "/opt/homebrew/bin/ffmpeg", # macOS Homebrew
  43. "/snap/bin/ffmpeg", # Ubuntu Snap
  44. "C:\\ffmpeg\\bin\\ffmpeg.exe", # Windows common
  45. ]
  46. for path in common_paths:
  47. if Path(path).exists():
  48. ffmpeg_path = path
  49. break
  50. _ffmpeg_path = ffmpeg_path
  51. if ffmpeg_path:
  52. logger.info("Found ffmpeg at: %s", ffmpeg_path)
  53. else:
  54. logger.warning("ffmpeg not found in PATH or common locations")
  55. return ffmpeg_path
  56. def rtsp_socket_timeout_flag() -> str:
  57. """Return the ffmpeg argv flag (without the leading dash) that sets the
  58. RTSP demuxer's client-side TCP socket I/O timeout, in microseconds.
  59. ffmpeg has shipped three different option arrangements for this over
  60. time, and Bambuddy supports the full range:
  61. - **Modern ffmpeg (5.x / 6.x / 7.x)** — Debian 13, Ubuntu 24.04, current
  62. Homebrew, etc. ``-timeout`` is the socket I/O timeout (microseconds);
  63. ``-stimeout`` was REMOVED.
  64. - **Transitional ffmpeg (~late-4.x, some 5.x builds)** — Ubuntu 22.04's
  65. shipped version is one of these. ``-timeout`` was deprecated and
  66. *repurposed* to mean the RTSP listen-mode incoming-connection
  67. timeout — and any non-zero value implies ``-listen``, which makes
  68. ffmpeg bind the localhost proxy port and fail with EADDRINUSE
  69. (#1504). ``-stimeout`` was the replacement socket I/O timeout in
  70. that window.
  71. - **Old ffmpeg (early 4.x and earlier)** — ``-timeout`` is socket I/O
  72. timeout (the original meaning, before the deprecation churn).
  73. We probe ``-h demuxer=rtsp`` once and cache: if ``-stimeout`` is
  74. advertised, prefer it (covers the transitional window and stays
  75. correct on the older builds that still accept it as an alias); else
  76. fall back to ``-timeout`` (correct on modern and pre-deprecation
  77. ffmpeg). The result is cached for the process lifetime — ffmpeg
  78. isn't going to swap mid-run.
  79. Returns the option name without the leading dash, e.g. ``"timeout"``
  80. or ``"stimeout"``. Callers must prepend ``-`` themselves so a string
  81. formatting bug can't pass an empty flag.
  82. """
  83. global _rtsp_socket_timeout_flag
  84. if _rtsp_socket_timeout_flag is not None:
  85. return _rtsp_socket_timeout_flag
  86. ffmpeg = get_ffmpeg_path()
  87. chosen = "timeout" # safe default for modern ffmpeg
  88. if ffmpeg:
  89. try:
  90. result = subprocess.run(
  91. [ffmpeg, "-hide_banner", "-h", "demuxer=rtsp"],
  92. capture_output=True,
  93. text=True,
  94. timeout=5,
  95. check=False,
  96. )
  97. help_text = (result.stdout or "") + (result.stderr or "")
  98. # Help lines list each option as `-<name> ` (trailing space) — match
  99. # that exact form so we don't accidentally hit a substring elsewhere.
  100. if "-stimeout " in help_text:
  101. chosen = "stimeout"
  102. except (OSError, subprocess.SubprocessError) as exc:
  103. # If probing fails, keep the modern-ffmpeg default. Worst case
  104. # is the EADDRINUSE regression returns for transitional-ffmpeg
  105. # users — same as before this function existed.
  106. logger.warning("Could not probe ffmpeg RTSP timeout flag, defaulting to -timeout: %s", exc)
  107. _rtsp_socket_timeout_flag = chosen
  108. logger.info("RTSP socket I/O timeout flag: -%s", chosen)
  109. return chosen
  110. def supports_rtsp(model: str | None) -> bool:
  111. """Check if printer model supports RTSP camera streaming.
  112. RTSP supported: X1, X1C, X1E, X2D, H2C, H2D, H2DPRO, H2S, P2S
  113. Chamber image only: A1, A1MINI, P1P, P1S
  114. Note: Model can be either display name (e.g., "P2S") or internal code (e.g., "N7").
  115. Internal codes from MQTT/SSDP:
  116. - BL-P001: X1/X1C
  117. - C13: X1E
  118. - N6: X2D
  119. - O1D: H2D
  120. - O1C, O1C2: H2C
  121. - O1S: H2S
  122. - O1E, O2D: H2D Pro
  123. - N7: P2S
  124. """
  125. if model:
  126. model_upper = model.upper()
  127. # Display names: X1, X1C, X1E, X2D, H2C, H2D, H2DPRO, H2S, P2S
  128. if model_upper.startswith(("X1", "X2", "H2", "P2")):
  129. return True
  130. # Internal codes for RTSP models
  131. if model_upper in ("BL-P001", "C13", "N6", "O1D", "O1C", "O1C2", "O1S", "O1E", "O2D", "N7"):
  132. return True
  133. # A1/P1 and unknown models use chamber image protocol
  134. return False
  135. def get_camera_port(model: str | None) -> int:
  136. """Get the camera port based on printer model.
  137. X1/X2/H2/P2 series use RTSP on port 322.
  138. A1/P1 series use chamber image protocol on port 6000.
  139. """
  140. if supports_rtsp(model):
  141. return 322
  142. return 6000
  143. def rewrite_rtsp_request_url(data: bytes, proxy_url: bytes, real_url: bytes) -> bytes:
  144. """Rewrite RTSP request-line URLs, leaving other lines (e.g. Authorization) intact.
  145. RTSP request lines have the form ``METHOD <url> RTSP/1.0\\r\\n``.
  146. Only those lines are modified so that Digest auth headers (which embed
  147. the original URL and a cryptographic hash) are not broken.
  148. """
  149. rtsp_marker = b" RTSP/1.0"
  150. if rtsp_marker not in data:
  151. return data
  152. lines = data.split(b"\r\n")
  153. for i, line in enumerate(lines):
  154. if line.endswith(rtsp_marker):
  155. lines[i] = line.replace(proxy_url, real_url)
  156. break
  157. return b"\r\n".join(lines)
  158. async def create_tls_proxy(target_host: str, target_port: int) -> tuple[int, "asyncio.Server"]:
  159. """Create a local TCP→TLS proxy for RTSP streams.
  160. Bambu printers use RTSPS (RTSP over TLS) with self-signed certificates.
  161. The Debian ffmpeg package uses GnuTLS, whose hardened defaults reject
  162. certain TLS behaviors (renegotiation, legacy ciphers) that some printer
  163. firmwares (notably P2S) rely on. This causes streams to drop after a
  164. few seconds.
  165. This proxy terminates TLS using Python's ssl module (OpenSSL), which is
  166. more permissive, and exposes a plain TCP port that ffmpeg connects to
  167. with ``rtsp://`` instead of ``rtsps://``.
  168. RTSP embeds URLs in protocol messages (DESCRIBE, SETUP, PLAY). The proxy
  169. rewrites ``127.0.0.1:<proxy_port>`` → ``<target_host>:<target_port>`` in
  170. client→server data so the printer recognises the stream path.
  171. Returns ``(local_port, server)``. Caller must close the server when done.
  172. """
  173. ssl_ctx = ssl.SSLContext(ssl.PROTOCOL_TLS_CLIENT)
  174. ssl_ctx.check_hostname = False
  175. ssl_ctx.verify_mode = ssl.CERT_NONE
  176. # Filled in after the server socket is created (handler only runs after).
  177. _local_port: list[int] = [0]
  178. async def _handle(client_reader: asyncio.StreamReader, client_writer: asyncio.StreamWriter):
  179. tls_writer = None
  180. try:
  181. tls_reader, tls_writer = await asyncio.wait_for(
  182. asyncio.open_connection(target_host, target_port, ssl=ssl_ctx),
  183. timeout=10.0,
  184. )
  185. # URL patterns for RTSP request-line rewriting.
  186. proxy_url = f"rtsp://127.0.0.1:{_local_port[0]}".encode()
  187. real_url = f"rtsps://{target_host}:{target_port}".encode()
  188. # Note on the broad except below: dst.write() raises RuntimeError
  189. # under uvloop when the underlying handle has already been torn
  190. # down (uvloop.loop.UVHandle._ensure_alive). asyncio's default
  191. # selector loop reports the same situation as ConnectionResetError
  192. # / OSError, so a tuple that doesn't include RuntimeError leaks the
  193. # uvloop variant up to asyncio's unhandled-exception logger
  194. # ("Unhandled exception in client_connected_cb"). The forwarders
  195. # are intentionally fire-and-forget on tear-down — once either
  196. # peer drops, both halves of the proxy should exit quietly.
  197. async def _fwd_to_server(src: asyncio.StreamReader, dst: asyncio.StreamWriter):
  198. """Forward client→server, rewriting RTSP request-line URLs only."""
  199. try:
  200. while True:
  201. data = await src.read(65536)
  202. if not data:
  203. break
  204. data = rewrite_rtsp_request_url(data, proxy_url, real_url)
  205. dst.write(data)
  206. await dst.drain()
  207. except (ConnectionError, OSError, asyncio.CancelledError, RuntimeError):
  208. pass
  209. finally:
  210. if not dst.is_closing():
  211. try:
  212. dst.close()
  213. except OSError:
  214. pass
  215. async def _fwd_to_client(src: asyncio.StreamReader, dst: asyncio.StreamWriter):
  216. """Forward server→client unchanged."""
  217. try:
  218. while True:
  219. data = await src.read(65536)
  220. if not data:
  221. break
  222. dst.write(data)
  223. await dst.drain()
  224. except (ConnectionError, OSError, asyncio.CancelledError, RuntimeError):
  225. pass
  226. finally:
  227. if not dst.is_closing():
  228. try:
  229. dst.close()
  230. except OSError:
  231. pass
  232. await asyncio.gather(
  233. _fwd_to_server(client_reader, tls_writer),
  234. _fwd_to_client(tls_reader, client_writer),
  235. )
  236. except (ConnectionError, OSError, TimeoutError) as e:
  237. logger.debug("TLS proxy connection to %s:%s failed: %s", target_host, target_port, e)
  238. finally:
  239. for w in (client_writer, tls_writer):
  240. if w and not w.is_closing():
  241. try:
  242. w.close()
  243. except OSError:
  244. pass
  245. server = await asyncio.start_server(_handle, "127.0.0.1", 0)
  246. _local_port[0] = server.sockets[0].getsockname()[1]
  247. logger.debug("TLS proxy for %s:%s listening on 127.0.0.1:%s", target_host, target_port, _local_port[0])
  248. return _local_port[0], server
  249. def is_chamber_image_model(model: str | None) -> bool:
  250. """Check if printer uses chamber image protocol instead of RTSP.
  251. A1, A1MINI, P1P, P1S use the chamber image protocol on port 6000.
  252. """
  253. return not supports_rtsp(model)
  254. def build_camera_url(ip_address: str, access_code: str, model: str | None) -> str:
  255. """Build the RTSPS URL for the printer camera (RTSP models only)."""
  256. port = get_camera_port(model)
  257. return f"rtsps://bblp:{access_code}@{ip_address}:{port}/streaming/live/1"
  258. def _create_chamber_auth_payload(access_code: str) -> bytes:
  259. """Create the 80-byte authentication payload for chamber image protocol.
  260. Format:
  261. - Bytes 0-3: 0x40 0x00 0x00 0x00 (magic)
  262. - Bytes 4-7: 0x00 0x30 0x00 0x00 (command)
  263. - Bytes 8-15: zeros (padding)
  264. - Bytes 16-47: username "bblp" (32 bytes, null-padded)
  265. - Bytes 48-79: access code (32 bytes, null-padded)
  266. """
  267. username = b"bblp"
  268. access_code_bytes = access_code.encode("utf-8")
  269. # Build the 80-byte payload
  270. payload = struct.pack(
  271. "<II8s32s32s",
  272. 0x40, # Magic header
  273. 0x3000, # Command
  274. b"\x00" * 8, # Padding
  275. username.ljust(32, b"\x00"), # Username padded to 32 bytes
  276. access_code_bytes.ljust(32, b"\x00"), # Access code padded to 32 bytes
  277. )
  278. return payload
  279. def _create_ssl_context() -> ssl.SSLContext:
  280. """Create an SSL context for chamber image connection.
  281. Bambu printers use self-signed certificates, so we disable verification.
  282. """
  283. ctx = ssl.SSLContext(ssl.PROTOCOL_TLS_CLIENT)
  284. ctx.check_hostname = False
  285. ctx.verify_mode = ssl.CERT_NONE
  286. return ctx
  287. async def read_chamber_image_frame(
  288. ip_address: str,
  289. access_code: str,
  290. timeout: float = 10.0,
  291. ) -> bytes | None:
  292. """Read a single JPEG frame from the chamber image protocol.
  293. This is used by A1/P1 printers which don't support RTSP.
  294. Args:
  295. ip_address: Printer IP address
  296. access_code: Printer access code
  297. timeout: Connection timeout in seconds
  298. Returns:
  299. JPEG image data or None if failed
  300. """
  301. port = 6000
  302. ssl_context = _create_ssl_context()
  303. try:
  304. # Connect with SSL
  305. reader, writer = await asyncio.wait_for(
  306. asyncio.open_connection(ip_address, port, ssl=ssl_context),
  307. timeout=timeout,
  308. )
  309. try:
  310. # Send authentication payload
  311. auth_payload = _create_chamber_auth_payload(access_code)
  312. writer.write(auth_payload)
  313. await writer.drain()
  314. # Read the 16-byte header
  315. header = await asyncio.wait_for(reader.readexactly(16), timeout=timeout)
  316. if len(header) < 16:
  317. logger.error("Chamber image: incomplete header received")
  318. return None
  319. # Parse payload size from header (little-endian uint32 at offset 0)
  320. payload_size = struct.unpack("<I", header[0:4])[0]
  321. if payload_size == 0 or payload_size > 10_000_000: # Sanity check: max 10MB
  322. logger.error("Chamber image: invalid payload size %s", payload_size)
  323. return None
  324. # Read the JPEG data
  325. jpeg_data = await asyncio.wait_for(
  326. reader.readexactly(payload_size),
  327. timeout=timeout,
  328. )
  329. # Validate JPEG markers
  330. if not jpeg_data.startswith(JPEG_START):
  331. logger.error("Chamber image: data is not a valid JPEG (missing start marker)")
  332. return None
  333. if not jpeg_data.endswith(JPEG_END):
  334. logger.warning("Chamber image: JPEG missing end marker, may be truncated")
  335. logger.debug("Chamber image: received %s bytes", len(jpeg_data))
  336. return jpeg_data
  337. finally:
  338. writer.close()
  339. try:
  340. await writer.wait_closed()
  341. except OSError:
  342. pass # Socket already closed; cleanup is best-effort
  343. except TimeoutError:
  344. logger.error("Chamber image: connection timeout to %s:%s", ip_address, port)
  345. return None
  346. except ConnectionRefusedError:
  347. logger.error("Chamber image: connection refused by %s:%s", ip_address, port)
  348. return None
  349. except Exception as e:
  350. logger.exception("Chamber image: error connecting to %s:%s: %s", ip_address, port, e)
  351. return None
  352. async def generate_chamber_image_stream(
  353. ip_address: str,
  354. access_code: str,
  355. fps: int = 5,
  356. ) -> asyncio.StreamReader | None:
  357. """Create a persistent connection for streaming chamber images.
  358. Returns a connected reader or None if connection failed.
  359. """
  360. port = 6000
  361. ssl_context = _create_ssl_context()
  362. try:
  363. reader, writer = await asyncio.wait_for(
  364. asyncio.open_connection(ip_address, port, ssl=ssl_context),
  365. timeout=10.0,
  366. )
  367. # Send authentication payload
  368. auth_payload = _create_chamber_auth_payload(access_code)
  369. writer.write(auth_payload)
  370. await writer.drain()
  371. logger.info("Chamber image: connected to %s:%s", ip_address, port)
  372. return reader, writer
  373. except Exception as e:
  374. logger.error("Chamber image: failed to connect to %s:%s: %s", ip_address, port, e)
  375. return None
  376. async def read_next_chamber_frame(reader: asyncio.StreamReader, timeout: float = 10.0) -> bytes | None:
  377. """Read the next JPEG frame from an established chamber image connection."""
  378. try:
  379. # Read the 16-byte header
  380. header = await asyncio.wait_for(reader.readexactly(16), timeout=timeout)
  381. # Parse payload size from header (little-endian uint32 at offset 0)
  382. payload_size = struct.unpack("<I", header[0:4])[0]
  383. if payload_size == 0 or payload_size > 10_000_000:
  384. logger.error("Chamber image: invalid payload size %s", payload_size)
  385. return None
  386. # Read the JPEG data
  387. jpeg_data = await asyncio.wait_for(
  388. reader.readexactly(payload_size),
  389. timeout=timeout,
  390. )
  391. return jpeg_data
  392. except asyncio.IncompleteReadError:
  393. logger.warning("Chamber image: connection closed by printer")
  394. return None
  395. except TimeoutError:
  396. logger.warning("Chamber image: read timeout")
  397. return None
  398. except Exception as e:
  399. logger.error("Chamber image: error reading frame: %s", e)
  400. return None
  401. async def capture_camera_frame(
  402. ip_address: str,
  403. access_code: str,
  404. model: str | None,
  405. output_path: Path,
  406. timeout: int = 30,
  407. ) -> bool:
  408. """Capture a single frame from the printer's camera stream and save to disk.
  409. Uses capture_camera_frame_bytes() internally for protocol selection,
  410. then writes the result to the specified output path.
  411. Args:
  412. ip_address: Printer IP address
  413. access_code: Printer access code
  414. model: Printer model (X1, H2D, P1, A1, etc.)
  415. output_path: Path where to save the captured image
  416. timeout: Timeout in seconds for the capture operation
  417. Returns:
  418. True if capture was successful, False otherwise
  419. """
  420. output_path.parent.mkdir(parents=True, exist_ok=True)
  421. jpeg_data = await capture_camera_frame_bytes(ip_address, access_code, model, timeout)
  422. if jpeg_data:
  423. try:
  424. with open(output_path, "wb") as f:
  425. f.write(jpeg_data)
  426. logger.info("Saved camera frame to: %s", output_path)
  427. return True
  428. except OSError as e:
  429. logger.error("Failed to write camera frame: %s", e)
  430. return False
  431. return False
  432. async def capture_camera_frame_bytes(
  433. ip_address: str,
  434. access_code: str,
  435. model: str | None,
  436. timeout: int = 15,
  437. ) -> bytes | None:
  438. """Capture a single frame and return as JPEG bytes (no disk write).
  439. Uses the same protocol selection as capture_camera_frame but returns
  440. bytes directly instead of writing to disk.
  441. Args:
  442. ip_address: Printer IP address
  443. access_code: Printer access code
  444. model: Printer model (X1, H2D, P1, A1, etc.)
  445. timeout: Timeout in seconds for the capture operation
  446. Returns:
  447. JPEG bytes if capture was successful, None otherwise
  448. """
  449. # Chamber image models: A1/P1 - returns bytes directly
  450. if is_chamber_image_model(model):
  451. logger.info("Capturing camera frame bytes from %s using chamber image protocol (model: %s)", ip_address, model)
  452. return await read_chamber_image_frame(ip_address, access_code, timeout=float(timeout))
  453. # RTSP models: X1/H2/P2 - use ffmpeg piping to stdout
  454. # TLS proxy avoids GnuTLS compatibility issues with some printer firmwares
  455. port = get_camera_port(model)
  456. proxy_port, proxy_server = await create_tls_proxy(ip_address, port)
  457. camera_url = f"rtsp://bblp:{access_code}@127.0.0.1:{proxy_port}/streaming/live/1"
  458. ffmpeg = get_ffmpeg_path()
  459. if not ffmpeg:
  460. proxy_server.close()
  461. await proxy_server.wait_closed()
  462. logger.error("ffmpeg not found for camera frame capture")
  463. return None
  464. cmd = [
  465. ffmpeg,
  466. "-y",
  467. "-rtsp_transport",
  468. "tcp",
  469. "-rtsp_flags",
  470. "prefer_tcp",
  471. "-i",
  472. camera_url,
  473. "-frames:v",
  474. "1",
  475. "-f",
  476. "image2pipe",
  477. "-vcodec",
  478. "mjpeg",
  479. "-q:v",
  480. "2",
  481. "-",
  482. ]
  483. logger.info("Capturing camera frame bytes from %s using RTSP (model: %s)", ip_address, model)
  484. process = None
  485. try:
  486. process = await asyncio.create_subprocess_exec(
  487. *cmd,
  488. stdout=asyncio.subprocess.PIPE,
  489. stderr=asyncio.subprocess.PIPE,
  490. )
  491. _active_capture_pids.add(process.pid)
  492. try:
  493. stdout, stderr = await asyncio.wait_for(process.communicate(), timeout=timeout)
  494. except TimeoutError:
  495. process.kill()
  496. await process.wait()
  497. logger.error("Camera frame bytes capture timed out after %ss", timeout)
  498. return None
  499. if process.returncode == 0 and stdout and len(stdout) >= 100:
  500. logger.info("Successfully captured camera frame bytes: %s bytes", len(stdout))
  501. return stdout
  502. else:
  503. stderr_text = stderr.decode() if stderr else "Unknown error"
  504. logger.error("ffmpeg frame bytes capture failed (code %s): %s", process.returncode, stderr_text[:200])
  505. return None
  506. except FileNotFoundError:
  507. logger.error("ffmpeg not found for camera frame capture")
  508. return None
  509. except Exception as e:
  510. logger.exception("Camera frame bytes capture failed: %s", e)
  511. return None
  512. finally:
  513. if process is not None:
  514. _active_capture_pids.discard(process.pid)
  515. proxy_server.close()
  516. await proxy_server.wait_closed()
  517. async def capture_finish_photo(
  518. printer_id: int,
  519. ip_address: str,
  520. access_code: str,
  521. model: str | None,
  522. archive_dir: Path,
  523. ) -> str | None:
  524. """Capture a finish photo and save it to the archive's photos folder.
  525. Args:
  526. printer_id: ID of the printer
  527. ip_address: Printer IP address
  528. access_code: Printer access code
  529. model: Printer model
  530. archive_dir: Directory of the archive (where the 3MF is stored)
  531. Returns:
  532. Filename of the captured photo, or None if capture failed
  533. """
  534. # Create photos subdirectory
  535. photos_dir = archive_dir / "photos"
  536. photos_dir.mkdir(parents=True, exist_ok=True)
  537. # Generate filename with timestamp
  538. timestamp = datetime.now().strftime("%Y%m%d_%H%M%S")
  539. filename = f"finish_{timestamp}_{uuid.uuid4().hex[:8]}.jpg"
  540. output_path = photos_dir / filename
  541. success = await capture_camera_frame(
  542. ip_address=ip_address,
  543. access_code=access_code,
  544. model=model,
  545. output_path=output_path,
  546. timeout=30,
  547. )
  548. if success:
  549. logger.info("Finish photo saved: %s", filename)
  550. return filename
  551. else:
  552. logger.warning("Failed to capture finish photo for printer %s", printer_id)
  553. return None
  554. async def test_camera_connection(
  555. ip_address: str,
  556. access_code: str,
  557. model: str | None,
  558. ) -> dict:
  559. """Test if the camera stream is accessible.
  560. Returns dict with success status and any error message.
  561. """
  562. import tempfile
  563. fd, tmp_name = tempfile.mkstemp(suffix=".jpg")
  564. os.close(fd)
  565. test_path = Path(tmp_name)
  566. test_path.chmod(0o600)
  567. try:
  568. success = await capture_camera_frame(
  569. ip_address=ip_address,
  570. access_code=access_code,
  571. model=model,
  572. output_path=test_path,
  573. timeout=15,
  574. )
  575. if success:
  576. return {"success": True, "message": "Camera connection successful"}
  577. else:
  578. return {
  579. "success": False,
  580. "error": (
  581. "Failed to capture frame from camera. "
  582. "Ensure the printer is powered on, camera is enabled, and Developer Mode is active. "
  583. "If running in Docker, try 'network_mode: host' in docker-compose.yml."
  584. ),
  585. }
  586. finally:
  587. # Clean up test file
  588. if test_path.exists():
  589. test_path.unlink()