camera.py 42 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073
  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 functools
  8. import logging
  9. import os
  10. import shutil
  11. import ssl
  12. import struct
  13. import subprocess
  14. import uuid
  15. import weakref
  16. from datetime import datetime
  17. from pathlib import Path
  18. from backend.app.utils.ffmpeg_output import (
  19. FFMPEG_RTSP_LOGLEVEL,
  20. NO_FFMPEG_OUTPUT,
  21. read_ffmpeg_log_until_open,
  22. reap_ffmpeg,
  23. summarize_ffmpeg_stderr,
  24. )
  25. logger = logging.getLogger(__name__)
  26. # JPEG markers
  27. JPEG_START = b"\xff\xd8"
  28. JPEG_END = b"\xff\xd9"
  29. # Cache the ffmpeg path after first lookup
  30. _ffmpeg_path: str | None = None
  31. # Cached result of rtsp_socket_timeout_flag(); see that function for context.
  32. _rtsp_socket_timeout_flag: str | None = None
  33. # Track PIDs of ffmpeg processes spawned for one-shot frame capture (snapshot).
  34. # The cleanup task in routes/camera.py checks this set to avoid killing active captures.
  35. _active_capture_pids: set[int] = set()
  36. # In-flight one-shot captures, keyed by printer IP (#2705).
  37. #
  38. # Bambu firmware allows exactly one camera connection, and the existing guards
  39. # (is_stream_active / try_get_active_buffered_frame, #1271 + #1348) only stop a
  40. # capturer from competing with the fan-out BROADCASTER. They do nothing for
  41. # capturer-vs-capturer with no viewer attached, where every consumer correctly
  42. # concludes it isn't competing with a viewer and then collides with the others.
  43. # Eight paths reach capture_camera_frame_bytes() independently — Obico polling,
  44. # /camera/snapshot, the finish-photo moment and its disk-writing sibling, plate
  45. # detection, the camera test and the diagnose tool — so the single-flight lives
  46. # at the bottom of the stack and needs no call-site changes.
  47. #
  48. # Keyed by IP rather than printer_id because IP is what the firmware's one-
  49. # connection limit applies to: two printer rows pointing at the same address
  50. # still share one camera. (This function never sees a printer_id anyway.) The
  51. # key deliberately excludes the timeout, or callers that disagree about it —
  52. # and they all do, from 10s to 30s — would never coalesce, which is exactly
  53. # the Obico-vs-snapshot pair from the report.
  54. _inflight_captures: dict[str, asyncio.Task[bytes | None]] = {}
  55. # In-flight connection handlers for each live TLS proxy server (#3001).
  56. #
  57. # This belongs on the server object, and for one release it lived there as an
  58. # instance attribute. That works on asyncio's own Server and raises
  59. # AttributeError on uvloop's, which is a Cython cdef class with no __dict__.
  60. # Every launch path this repo ships pins --loop asyncio (added for #1896), so
  61. # none of them could hit it -- but requirements.txt pins uvicorn[standard],
  62. # which installs uvloop, so anything launched without that flag gets uvloop
  63. # from --loop auto and loses every RTSP camera. That is real deployments: the
  64. # Proxmox VE Helper-Scripts LXC writes its own unit, and native installs
  65. # predating the #1896 pin never get it either, since update.sh does not rewrite
  66. # unit files. So the loop is not ours to assume, and this must not depend on it.
  67. # The test suite could not see it either: conftest builds the loop from the
  68. # default policy, so it only ever exercised the loop where the assignment is
  69. # legal.
  70. #
  71. # Keyed weakly so a server that is dropped without close_tls_proxy() -- an
  72. # exception between create and close -- takes its entry with it. Keying on
  73. # id(server) instead would leak those entries forever and, worse, hand a later
  74. # server a dead one's handler set once CPython recycles the address.
  75. _proxy_handlers: "weakref.WeakKeyDictionary[asyncio.Server, set[asyncio.Task]]" = weakref.WeakKeyDictionary()
  76. def get_ffmpeg_path() -> str | None:
  77. """Find the ffmpeg executable path.
  78. Uses shutil.which first, then checks common installation locations
  79. for systems where PATH may be limited (e.g., systemd services).
  80. """
  81. global _ffmpeg_path
  82. if _ffmpeg_path is not None:
  83. return _ffmpeg_path
  84. # Try PATH first
  85. ffmpeg_path = shutil.which("ffmpeg")
  86. # If not found via PATH, check common installation locations
  87. if ffmpeg_path is None:
  88. common_paths = [
  89. "/usr/bin/ffmpeg",
  90. "/usr/local/bin/ffmpeg",
  91. "/opt/homebrew/bin/ffmpeg", # macOS Homebrew
  92. "/snap/bin/ffmpeg", # Ubuntu Snap
  93. "C:\\ffmpeg\\bin\\ffmpeg.exe", # Windows common
  94. ]
  95. for path in common_paths:
  96. if Path(path).exists():
  97. ffmpeg_path = path
  98. break
  99. _ffmpeg_path = ffmpeg_path
  100. if ffmpeg_path:
  101. logger.info("Found ffmpeg at: %s", ffmpeg_path)
  102. else:
  103. logger.warning("ffmpeg not found in PATH or common locations")
  104. return ffmpeg_path
  105. def rtsp_socket_timeout_flag() -> str:
  106. """Return the ffmpeg argv flag (without the leading dash) that sets the
  107. RTSP demuxer's client-side TCP socket I/O timeout, in microseconds.
  108. ffmpeg has shipped three different option arrangements for this over
  109. time, and Bambuddy supports the full range:
  110. - **Modern ffmpeg (5.x / 6.x / 7.x)** — Debian 13, Ubuntu 24.04, current
  111. Homebrew, etc. ``-timeout`` is the socket I/O timeout (microseconds);
  112. ``-stimeout`` was REMOVED.
  113. - **Transitional ffmpeg (~late-4.x, some 5.x builds)** — Ubuntu 22.04's
  114. shipped version is one of these. ``-timeout`` was deprecated and
  115. *repurposed* to mean the RTSP listen-mode incoming-connection
  116. timeout — and any non-zero value implies ``-listen``, which makes
  117. ffmpeg bind the localhost proxy port and fail with EADDRINUSE
  118. (#1504). ``-stimeout`` was the replacement socket I/O timeout in
  119. that window.
  120. - **Old ffmpeg (early 4.x and earlier)** — ``-timeout`` is socket I/O
  121. timeout (the original meaning, before the deprecation churn).
  122. We probe ``-h demuxer=rtsp`` once and cache: if ``-stimeout`` is
  123. advertised, prefer it (covers the transitional window and stays
  124. correct on the older builds that still accept it as an alias); else
  125. fall back to ``-timeout`` (correct on modern and pre-deprecation
  126. ffmpeg). The result is cached for the process lifetime — ffmpeg
  127. isn't going to swap mid-run.
  128. Returns the option name without the leading dash, e.g. ``"timeout"``
  129. or ``"stimeout"``. Callers must prepend ``-`` themselves so a string
  130. formatting bug can't pass an empty flag.
  131. """
  132. global _rtsp_socket_timeout_flag
  133. if _rtsp_socket_timeout_flag is not None:
  134. return _rtsp_socket_timeout_flag
  135. ffmpeg = get_ffmpeg_path()
  136. chosen = "timeout" # safe default for modern ffmpeg
  137. if ffmpeg:
  138. try:
  139. result = subprocess.run(
  140. [ffmpeg, "-hide_banner", "-h", "demuxer=rtsp"],
  141. capture_output=True,
  142. text=True,
  143. timeout=5,
  144. check=False,
  145. )
  146. help_text = (result.stdout or "") + (result.stderr or "")
  147. # Help lines list each option as `-<name> ` (trailing space) — match
  148. # that exact form so we don't accidentally hit a substring elsewhere.
  149. if "-stimeout " in help_text:
  150. chosen = "stimeout"
  151. except (OSError, subprocess.SubprocessError) as exc:
  152. # If probing fails, keep the modern-ffmpeg default. Worst case
  153. # is the EADDRINUSE regression returns for transitional-ffmpeg
  154. # users — same as before this function existed.
  155. logger.warning("Could not probe ffmpeg RTSP timeout flag, defaulting to -timeout: %s", exc)
  156. _rtsp_socket_timeout_flag = chosen
  157. logger.info("RTSP socket I/O timeout flag: -%s", chosen)
  158. return chosen
  159. def supports_rtsp(model: str | None) -> bool:
  160. """Check if printer model supports RTSP camera streaming.
  161. RTSP supported: X1, X1C, X1E, X2D, H2C, H2D, H2DPRO, H2S, H3C, P2S
  162. Chamber image only: A1, A1MINI, P1P, P1S
  163. Note: Model can be either display name (e.g., "P2S") or internal code (e.g., "N7").
  164. Internal codes from MQTT/SSDP:
  165. - BL-P001: X1/X1C
  166. - C13: X1E
  167. - N6: X2D
  168. - O1D: H2D
  169. - O1C, O1C2: H2C
  170. - O1S: H2S
  171. - O1E, O2D: H2D Pro
  172. - N7: P2S
  173. - N10: H3C
  174. """
  175. if model:
  176. model_upper = model.upper()
  177. # Display names: X1, X1C, X1E, X2D, H2C, H2D, H2DPRO, H2S, H3C, P2S
  178. if model_upper.startswith(("X1", "X2", "H2", "H3", "P2")):
  179. return True
  180. # Internal codes for RTSP models
  181. if model_upper in ("BL-P001", "C13", "N6", "O1D", "O1C", "O1C2", "O1S", "O1E", "O2D", "N7", "N10"):
  182. return True
  183. # A1/P1 and unknown models use chamber image protocol
  184. return False
  185. def get_camera_port(model: str | None) -> int:
  186. """Get the camera port based on printer model.
  187. X1/X2/H2/H3/P2 series use RTSP on port 322.
  188. A1/P1 series use chamber image protocol on port 6000.
  189. """
  190. if supports_rtsp(model):
  191. return 322
  192. return 6000
  193. def rewrite_rtsp_request_url(data: bytes, proxy_url: bytes, real_url: bytes) -> bytes:
  194. """Rewrite RTSP request-line URLs, leaving other lines (e.g. Authorization) intact.
  195. RTSP request lines have the form ``METHOD <url> RTSP/1.0\\r\\n``.
  196. Only those lines are modified so that Digest auth headers (which embed
  197. the original URL and a cryptographic hash) are not broken.
  198. """
  199. rtsp_marker = b" RTSP/1.0"
  200. if rtsp_marker not in data:
  201. return data
  202. lines = data.split(b"\r\n")
  203. for i, line in enumerate(lines):
  204. if line.endswith(rtsp_marker):
  205. lines[i] = line.replace(proxy_url, real_url)
  206. break
  207. return b"\r\n".join(lines)
  208. async def create_tls_proxy(target_host: str, target_port: int) -> tuple[int, "asyncio.Server"]:
  209. """Create a local TCP→TLS proxy for RTSP streams.
  210. Bambu printers use RTSPS (RTSP over TLS) with self-signed certificates.
  211. The Debian ffmpeg package uses GnuTLS, whose hardened defaults reject
  212. certain TLS behaviors (renegotiation, legacy ciphers) that some printer
  213. firmwares (notably P2S) rely on. This causes streams to drop after a
  214. few seconds.
  215. This proxy terminates TLS using Python's ssl module (OpenSSL), which is
  216. more permissive, and exposes a plain TCP port that ffmpeg connects to
  217. with ``rtsp://`` instead of ``rtsps://``.
  218. RTSP embeds URLs in protocol messages (DESCRIBE, SETUP, PLAY). The proxy
  219. rewrites ``127.0.0.1:<proxy_port>`` → ``<target_host>:<target_port>`` in
  220. client→server data so the printer recognises the stream path.
  221. Returns ``(local_port, server)``. Caller must close it with
  222. :func:`close_tls_proxy` when done.
  223. """
  224. ssl_ctx = ssl.SSLContext(ssl.PROTOCOL_TLS_CLIENT)
  225. ssl_ctx.check_hostname = False
  226. ssl_ctx.verify_mode = ssl.CERT_NONE
  227. # Filled in after the server socket is created (handler only runs after).
  228. _local_port: list[int] = [0]
  229. # Strong references to the in-flight connection handlers (#2968).
  230. # ``asyncio.start_server`` wraps the callback in a task and keeps only a
  231. # weak reference to it, so a handler still awaiting its two forwarders can
  232. # be garbage-collected out from under itself — which is asyncio's
  233. # "Task was destroyed but it is pending!", logged at ERROR with a traceback
  234. # pointing here and no indication that it is a teardown race rather than a
  235. # camera fault. Holding the set also gives close_tls_proxy something to
  236. # cancel, so shutdown stops depending on ffmpeg having dropped its end.
  237. # The set is published in _proxy_handlers once the server exists; see the
  238. # note there for why it is not an attribute on the server itself.
  239. handlers: set[asyncio.Task] = set()
  240. async def _handle(client_reader: asyncio.StreamReader, client_writer: asyncio.StreamWriter):
  241. current = asyncio.current_task()
  242. if current is not None:
  243. handlers.add(current)
  244. tls_writer = None
  245. try:
  246. tls_reader, tls_writer = await asyncio.wait_for(
  247. asyncio.open_connection(target_host, target_port, ssl=ssl_ctx),
  248. timeout=10.0,
  249. )
  250. # URL patterns for RTSP request-line rewriting.
  251. proxy_url = f"rtsp://127.0.0.1:{_local_port[0]}".encode()
  252. real_url = f"rtsps://{target_host}:{target_port}".encode()
  253. # Note on the broad except below: dst.write() raises RuntimeError
  254. # under uvloop when the underlying handle has already been torn
  255. # down (uvloop.loop.UVHandle._ensure_alive). asyncio's default
  256. # selector loop reports the same situation as ConnectionResetError
  257. # / OSError, so a tuple that doesn't include RuntimeError leaks the
  258. # uvloop variant up to asyncio's unhandled-exception logger
  259. # ("Unhandled exception in client_connected_cb"). The forwarders
  260. # are intentionally fire-and-forget on tear-down — once either
  261. # peer drops, both halves of the proxy should exit quietly.
  262. async def _fwd_to_server(src: asyncio.StreamReader, dst: asyncio.StreamWriter):
  263. """Forward client→server, rewriting RTSP request-line URLs only."""
  264. try:
  265. while True:
  266. data = await src.read(65536)
  267. if not data:
  268. break
  269. data = rewrite_rtsp_request_url(data, proxy_url, real_url)
  270. dst.write(data)
  271. await dst.drain()
  272. except (ConnectionError, OSError, asyncio.CancelledError, RuntimeError):
  273. pass
  274. finally:
  275. if not dst.is_closing():
  276. try:
  277. dst.close()
  278. except OSError:
  279. pass
  280. async def _fwd_to_client(src: asyncio.StreamReader, dst: asyncio.StreamWriter):
  281. """Forward server→client unchanged."""
  282. try:
  283. while True:
  284. data = await src.read(65536)
  285. if not data:
  286. break
  287. dst.write(data)
  288. await dst.drain()
  289. except (ConnectionError, OSError, asyncio.CancelledError, RuntimeError):
  290. pass
  291. finally:
  292. if not dst.is_closing():
  293. try:
  294. dst.close()
  295. except OSError:
  296. pass
  297. await asyncio.gather(
  298. _fwd_to_server(client_reader, tls_writer),
  299. _fwd_to_client(tls_reader, client_writer),
  300. )
  301. except (ConnectionError, OSError, TimeoutError) as e:
  302. logger.debug("TLS proxy connection to %s:%s failed: %s", target_host, target_port, e)
  303. except asyncio.CancelledError:
  304. # close_tls_proxy cancelling us at shutdown, which is the only thing
  305. # that cancels this task. Swallowing a cancellation is normally
  306. # wrong because it hides the request from whoever made it; here we
  307. # *are* whoever made it, the cleanup it exists to trigger is in the
  308. # finally below, and nothing awaits this task's result. Asyncio's
  309. # own done-callback for a connection handler treats a cancelled task
  310. # differently from a completed one, so ending in the ordinary way
  311. # keeps the teardown on one path across Python versions.
  312. pass
  313. finally:
  314. if current is not None:
  315. handlers.discard(current)
  316. for w in (client_writer, tls_writer):
  317. if w and not w.is_closing():
  318. try:
  319. w.close()
  320. except OSError:
  321. pass
  322. server = await asyncio.start_server(_handle, "127.0.0.1", 0)
  323. _local_port[0] = server.sockets[0].getsockname()[1]
  324. _proxy_handlers[server] = handlers
  325. logger.debug("TLS proxy for %s:%s listening on 127.0.0.1:%s", target_host, target_port, _local_port[0])
  326. return _local_port[0], server
  327. async def close_tls_proxy(server: "asyncio.Server") -> None:
  328. """Shut a :func:`create_tls_proxy` server down without leaving tasks behind.
  329. ``server.close()`` stops the listener but leaves established connections
  330. running, and ``wait_closed()`` is only as deterministic as the peer: it
  331. waits for the handlers, and a handler waits for ffmpeg to drop its end of
  332. the socket. By the time this is called ffmpeg has already been reaped, so
  333. the connection is dead weight — cancelling it is both correct and the only
  334. way to guarantee no handler outlives the server that owns it.
  335. ``Server.close_clients()`` would do this natively, but it landed in Python
  336. 3.13 and Bambuddy supports 3.11, so the handler set is tracked by hand in
  337. ``_proxy_handlers``.
  338. Safe to call on any ``asyncio.Server`` from anywhere else: a server that
  339. was not created here is simply absent from the registry, and this degrades
  340. to the close/wait it replaces.
  341. """
  342. handlers: set[asyncio.Task] = _proxy_handlers.pop(server, set())
  343. server.close()
  344. for task in list(handlers):
  345. task.cancel()
  346. if handlers:
  347. await asyncio.gather(*list(handlers), return_exceptions=True)
  348. await server.wait_closed()
  349. def is_chamber_image_model(model: str | None) -> bool:
  350. """Check if printer uses chamber image protocol instead of RTSP.
  351. A1, A1MINI, P1P, P1S use the chamber image protocol on port 6000.
  352. """
  353. return not supports_rtsp(model)
  354. def _create_chamber_auth_payload(access_code: str) -> bytes:
  355. """Create the 80-byte authentication payload for chamber image protocol.
  356. Format:
  357. - Bytes 0-3: 0x40 0x00 0x00 0x00 (magic)
  358. - Bytes 4-7: 0x00 0x30 0x00 0x00 (command)
  359. - Bytes 8-15: zeros (padding)
  360. - Bytes 16-47: username "bblp" (32 bytes, null-padded)
  361. - Bytes 48-79: access code (32 bytes, null-padded)
  362. """
  363. username = b"bblp"
  364. access_code_bytes = access_code.encode("utf-8")
  365. # Build the 80-byte payload
  366. payload = struct.pack(
  367. "<II8s32s32s",
  368. 0x40, # Magic header
  369. 0x3000, # Command
  370. b"\x00" * 8, # Padding
  371. username.ljust(32, b"\x00"), # Username padded to 32 bytes
  372. access_code_bytes.ljust(32, b"\x00"), # Access code padded to 32 bytes
  373. )
  374. return payload
  375. def _create_ssl_context() -> ssl.SSLContext:
  376. """Create an SSL context for chamber image connection.
  377. Bambu printers use self-signed certificates, so we disable verification.
  378. """
  379. ctx = ssl.SSLContext(ssl.PROTOCOL_TLS_CLIENT)
  380. ctx.check_hostname = False
  381. ctx.verify_mode = ssl.CERT_NONE
  382. return ctx
  383. async def read_chamber_image_frame(
  384. ip_address: str,
  385. access_code: str,
  386. timeout: float = 10.0,
  387. ) -> bytes | None:
  388. """Read a single JPEG frame from the chamber image protocol.
  389. This is used by A1/P1 printers which don't support RTSP.
  390. Args:
  391. ip_address: Printer IP address
  392. access_code: Printer access code
  393. timeout: Connection timeout in seconds
  394. Returns:
  395. JPEG image data or None if failed
  396. """
  397. port = 6000
  398. ssl_context = _create_ssl_context()
  399. try:
  400. # Connect with SSL
  401. reader, writer = await asyncio.wait_for(
  402. asyncio.open_connection(ip_address, port, ssl=ssl_context),
  403. timeout=timeout,
  404. )
  405. try:
  406. # Send authentication payload
  407. auth_payload = _create_chamber_auth_payload(access_code)
  408. writer.write(auth_payload)
  409. await writer.drain()
  410. # Read the 16-byte header
  411. header = await asyncio.wait_for(reader.readexactly(16), timeout=timeout)
  412. if len(header) < 16:
  413. logger.error("Chamber image: incomplete header received")
  414. return None
  415. # Parse payload size from header (little-endian uint32 at offset 0)
  416. payload_size = struct.unpack("<I", header[0:4])[0]
  417. if payload_size == 0 or payload_size > 10_000_000: # Sanity check: max 10MB
  418. logger.error("Chamber image: invalid payload size %s", payload_size)
  419. return None
  420. # Read the JPEG data
  421. jpeg_data = await asyncio.wait_for(
  422. reader.readexactly(payload_size),
  423. timeout=timeout,
  424. )
  425. # Validate JPEG markers
  426. if not jpeg_data.startswith(JPEG_START):
  427. logger.error("Chamber image: data is not a valid JPEG (missing start marker)")
  428. return None
  429. if not jpeg_data.endswith(JPEG_END):
  430. logger.warning("Chamber image: JPEG missing end marker, may be truncated")
  431. logger.debug("Chamber image: received %s bytes", len(jpeg_data))
  432. return jpeg_data
  433. finally:
  434. writer.close()
  435. try:
  436. await writer.wait_closed()
  437. except OSError:
  438. pass # Socket already closed; cleanup is best-effort
  439. except TimeoutError:
  440. logger.error("Chamber image: connection timeout to %s:%s", ip_address, port)
  441. return None
  442. except ConnectionRefusedError:
  443. logger.error("Chamber image: connection refused by %s:%s", ip_address, port)
  444. return None
  445. except Exception as e:
  446. logger.exception("Chamber image: error connecting to %s:%s: %s", ip_address, port, e)
  447. return None
  448. async def generate_chamber_image_stream(
  449. ip_address: str,
  450. access_code: str,
  451. fps: int = 5,
  452. ) -> asyncio.StreamReader | None:
  453. """Create a persistent connection for streaming chamber images.
  454. Returns a connected reader or None if connection failed.
  455. """
  456. port = 6000
  457. ssl_context = _create_ssl_context()
  458. try:
  459. reader, writer = await asyncio.wait_for(
  460. asyncio.open_connection(ip_address, port, ssl=ssl_context),
  461. timeout=10.0,
  462. )
  463. # Send authentication payload
  464. auth_payload = _create_chamber_auth_payload(access_code)
  465. writer.write(auth_payload)
  466. await writer.drain()
  467. logger.info("Chamber image: connected to %s:%s", ip_address, port)
  468. return reader, writer
  469. except Exception as e:
  470. logger.error("Chamber image: failed to connect to %s:%s: %s", ip_address, port, e)
  471. return None
  472. async def read_next_chamber_frame(reader: asyncio.StreamReader, timeout: float = 10.0) -> bytes | None:
  473. """Read the next JPEG frame from an established chamber image connection."""
  474. try:
  475. # Read the 16-byte header
  476. header = await asyncio.wait_for(reader.readexactly(16), timeout=timeout)
  477. # Parse payload size from header (little-endian uint32 at offset 0)
  478. payload_size = struct.unpack("<I", header[0:4])[0]
  479. if payload_size == 0 or payload_size > 10_000_000:
  480. logger.error("Chamber image: invalid payload size %s", payload_size)
  481. return None
  482. # Read the JPEG data
  483. jpeg_data = await asyncio.wait_for(
  484. reader.readexactly(payload_size),
  485. timeout=timeout,
  486. )
  487. return jpeg_data
  488. except asyncio.IncompleteReadError:
  489. logger.warning("Chamber image: connection closed by printer")
  490. return None
  491. except TimeoutError:
  492. logger.warning("Chamber image: read timeout")
  493. return None
  494. except Exception as e:
  495. logger.error("Chamber image: error reading frame: %s", e)
  496. return None
  497. async def capture_camera_frame(
  498. ip_address: str,
  499. access_code: str,
  500. model: str | None,
  501. output_path: Path,
  502. timeout: int = 30,
  503. ) -> bool:
  504. """Capture a single frame from the printer's camera stream and save to disk.
  505. Uses capture_camera_frame_bytes() internally for protocol selection,
  506. then writes the result to the specified output path.
  507. Args:
  508. ip_address: Printer IP address
  509. access_code: Printer access code
  510. model: Printer model (X1, H2D, P1, A1, etc.)
  511. output_path: Path where to save the captured image
  512. timeout: Timeout in seconds for the capture operation
  513. Returns:
  514. True if capture was successful, False otherwise
  515. """
  516. output_path.parent.mkdir(parents=True, exist_ok=True)
  517. jpeg_data = await capture_camera_frame_bytes(ip_address, access_code, model, timeout)
  518. if jpeg_data:
  519. try:
  520. with open(output_path, "wb") as f:
  521. f.write(jpeg_data)
  522. logger.info("Saved camera frame to: %s", output_path)
  523. return True
  524. except OSError as e:
  525. logger.error("Failed to write camera frame: %s", e)
  526. return False
  527. return False
  528. def capture_in_flight(ip_address: str) -> bool:
  529. """Return True iff a one-shot capture for this IP is running right now.
  530. For callers that need to know whether they will JOIN someone else's
  531. capture rather than perform their own — currently only the diagnose tool,
  532. which reports on what it measured and so must not present a coalesced
  533. frame as proof that it opened its own connection (see camera_diagnose).
  534. Ordinary consumers should ignore this: they want "a recent frame", and
  535. capture_camera_frame_bytes() already does the right thing for them.
  536. """
  537. task = _inflight_captures.get(ip_address)
  538. return task is not None and not task.done()
  539. def _discard_inflight_capture(ip_address: str, task: asyncio.Task) -> None:
  540. """Done-callback: drop the finished task from the in-flight registry.
  541. Guarded on identity so a slow task that finishes after a newer capture
  542. has registered can't evict its successor.
  543. Also retrieves the exception, if any. The leader normally awaits the task
  544. and would surface it, but a leader whose own caller was cancelled leaves
  545. nobody to collect it — and an unretrieved task exception is logged by
  546. asyncio as a warning with a traceback at an arbitrary later point.
  547. """
  548. if _inflight_captures.get(ip_address) is task:
  549. del _inflight_captures[ip_address]
  550. if not task.cancelled() and task.exception() is not None:
  551. logger.debug("In-flight camera capture for %s ended in an exception", ip_address)
  552. async def capture_camera_frame_bytes(
  553. ip_address: str,
  554. access_code: str,
  555. model: str | None,
  556. timeout: int = 15,
  557. ) -> bytes | None:
  558. """Capture a single frame and return as JPEG bytes (no disk write).
  559. Concurrent callers for the same printer share one capture (#2705): the
  560. first opens the connection, everyone arriving while it is in flight awaits
  561. the same result. Every consumer here wants "a recent frame" rather than
  562. "a frame captured at exactly my timestamp", so handing identical bytes to
  563. simultaneous callers is correct — and it is the only way to honour the
  564. firmware's one-connection limit without serialising captures behind a lock
  565. (which would just turn a collision into a queue).
  566. This coalesces; it does not cache. A call that arrives after the previous
  567. capture finished always captures fresh. Two consumers of these frames —
  568. plate detection and the finish-photo path — decide things about a running
  569. print from them, and a stale frame there is worse than a slow one: the
  570. whole of #1397 was a finish photo taken seconds late showing the bed
  571. already lowered.
  572. Args:
  573. ip_address: Printer IP address
  574. access_code: Printer access code
  575. model: Printer model (X1, H2D, P1, A1, etc.)
  576. timeout: Timeout in seconds for the capture operation. Applies to this
  577. caller's own wait, including when it joins another caller's
  578. capture — the call sites disagree about the value (10s for plate
  579. detection, 20s for Obico), and a follower must not silently
  580. inherit the leader's deadline in either direction.
  581. Returns:
  582. JPEG bytes if capture was successful, None otherwise
  583. """
  584. # A follower whose leader fails takes a turn of its own rather than
  585. # inheriting a failure it never had a chance to avoid — by then the leader
  586. # has finished, so there is no socket left to compete with. Bounded at two
  587. # rounds: if the capture we joined AND its replacement both failed, a third
  588. # connection won't help, and this caller has already spent its patience.
  589. for _ in range(2):
  590. leader = _inflight_captures.get(ip_address)
  591. if leader is None or leader.done():
  592. break
  593. try:
  594. frame = await asyncio.wait_for(asyncio.shield(leader), timeout=timeout)
  595. except TimeoutError:
  596. # shield() keeps the capture running for whoever else is still
  597. # waiting on it — giving up is this caller's decision alone.
  598. logger.warning(
  599. "Gave up waiting %ss on the in-flight camera capture for %s",
  600. timeout,
  601. ip_address,
  602. )
  603. return None
  604. except asyncio.CancelledError:
  605. # Distinguish "the capture I joined was cancelled" from "I was
  606. # cancelled". Only the former is ours to recover from.
  607. if not leader.cancelled():
  608. raise
  609. logger.info("In-flight camera capture for %s was cancelled; capturing our own", ip_address)
  610. continue
  611. if frame is not None:
  612. logger.info(
  613. "Reusing in-flight camera capture for %s: %s bytes (no second connection opened)",
  614. ip_address,
  615. len(frame),
  616. )
  617. return frame
  618. logger.info("In-flight camera capture for %s failed; capturing our own", ip_address)
  619. else:
  620. return None
  621. task = asyncio.create_task(_capture_camera_frame_bytes_uncoalesced(ip_address, access_code, model, timeout))
  622. _inflight_captures[ip_address] = task
  623. task.add_done_callback(functools.partial(_discard_inflight_capture, ip_address))
  624. # No wait_for here: this caller IS the capture, and the implementation
  625. # already enforces `timeout` internally where it can also kill the ffmpeg
  626. # process. A second deadline on top would abandon the subprocess instead.
  627. # shield() so that a cancelled leader (a client navigating away mid-
  628. # snapshot is routine) doesn't take the capture down with it — the
  629. # followers already waiting on it still get their frame.
  630. return await asyncio.shield(task)
  631. async def _capture_camera_frame_bytes_uncoalesced(
  632. ip_address: str,
  633. access_code: str,
  634. model: str | None,
  635. timeout: int = 15,
  636. ) -> bytes | None:
  637. """Open a connection and capture one frame. See capture_camera_frame_bytes.
  638. Callers want that wrapper, not this: it opens a socket unconditionally,
  639. which is the collision #2705 is about.
  640. """
  641. # Chamber image models: A1/P1 - returns bytes directly
  642. if is_chamber_image_model(model):
  643. logger.info("Capturing camera frame bytes from %s using chamber image protocol (model: %s)", ip_address, model)
  644. return await read_chamber_image_frame(ip_address, access_code, timeout=float(timeout))
  645. # RTSP models: X1/H2/P2 - use ffmpeg piping to stdout
  646. # TLS proxy avoids GnuTLS compatibility issues with some printer firmwares
  647. port = get_camera_port(model)
  648. proxy_port, proxy_server = await create_tls_proxy(ip_address, port)
  649. camera_url = f"rtsp://bblp:{access_code}@127.0.0.1:{proxy_port}/streaming/live/1"
  650. ffmpeg = get_ffmpeg_path()
  651. if not ffmpeg:
  652. await close_tls_proxy(proxy_server)
  653. logger.error("ffmpeg not found for camera frame capture")
  654. return None
  655. cmd = [
  656. ffmpeg,
  657. "-y",
  658. *FFMPEG_RTSP_LOGLEVEL,
  659. "-rtsp_transport",
  660. "tcp",
  661. "-rtsp_flags",
  662. "prefer_tcp",
  663. "-i",
  664. camera_url,
  665. "-frames:v",
  666. "1",
  667. "-f",
  668. "image2pipe",
  669. "-vcodec",
  670. "mjpeg",
  671. "-q:v",
  672. "2",
  673. "-",
  674. ]
  675. logger.info("Capturing camera frame bytes from %s using RTSP (model: %s)", ip_address, model)
  676. process = None
  677. try:
  678. process = await asyncio.create_subprocess_exec(
  679. *cmd,
  680. stdout=asyncio.subprocess.PIPE,
  681. stderr=asyncio.subprocess.PIPE,
  682. )
  683. _active_capture_pids.add(process.pid)
  684. try:
  685. # The printer's answers reach ffmpeg through the proxy unchanged,
  686. # so a redirect is refused here as for an external camera.
  687. async with asyncio.timeout(timeout):
  688. state, log = await read_ffmpeg_log_until_open(process)
  689. stdout, stderr = await process.communicate()
  690. except TimeoutError:
  691. logger.error("Camera frame bytes capture timed out after %ss", timeout)
  692. return None
  693. stderr = log + stderr
  694. if state == "redirected":
  695. return None
  696. if state == "open" and process.returncode == 0 and stdout and len(stdout) >= 100:
  697. logger.info("Successfully captured camera frame bytes: %s bytes", len(stdout))
  698. return stdout
  699. else:
  700. # The summariser drops ffmpeg's banner and masks the access code
  701. # the RTSP input URL carries; without it this line was 200
  702. # characters of build configuration (#2968).
  703. stderr_text = summarize_ffmpeg_stderr(stderr) or NO_FFMPEG_OUTPUT
  704. logger.error("ffmpeg frame bytes capture failed (code %s): %s", process.returncode, stderr_text)
  705. return None
  706. except FileNotFoundError:
  707. logger.error("ffmpeg not found for camera frame capture")
  708. return None
  709. except Exception as e:
  710. logger.exception("Camera frame bytes capture failed: %s", e)
  711. return None
  712. finally:
  713. # Killed however the capture ends, not only on its deadline (#3292).
  714. await reap_ffmpeg(process)
  715. if process is not None:
  716. _active_capture_pids.discard(process.pid)
  717. await close_tls_proxy(proxy_server)
  718. async def extract_video_last_frame(video_path: Path, output_path: Path) -> bool:
  719. """Extract the last frame of `video_path` as JPEG at `output_path`.
  720. Used to source finish photos from a Bambu timelapse. The Bambu firmware
  721. stops timelapse recording AFTER the toolhead parks but BEFORE the bed-drop
  722. end-gcode runs, so the last frame frames the finished print correctly.
  723. A live camera grab at `gcode_state=FINISH` captures the bed already
  724. lowered (#1397).
  725. Implementation: ``-update 1`` writes each decoded frame to the same
  726. output file (overwriting), so the file left on disk after ffmpeg
  727. finishes is the LAST frame. This works regardless of how short the
  728. video is — a small print's timelapse can be sub-second / sub-30 frames
  729. (one frame per layer-change capture), and the earlier ``-sseof -1.0``
  730. approach failed there because the seek went before the start of the
  731. file and ffmpeg silently returned frame 0 (empty bed at print start).
  732. Decoding every frame is fine: Bambu timelapses are short by
  733. construction (<1 minute even on hours-long prints).
  734. Returns False on missing ffmpeg, missing video, subprocess failure or
  735. timeout. Never raises.
  736. """
  737. ffmpeg = get_ffmpeg_path()
  738. if not ffmpeg:
  739. logger.warning("Cannot extract video last frame: ffmpeg not available")
  740. return False
  741. if not video_path.exists() or video_path.stat().st_size == 0:
  742. logger.warning("Cannot extract last frame: %s missing or empty", video_path)
  743. return False
  744. output_path.parent.mkdir(parents=True, exist_ok=True)
  745. cmd = [
  746. ffmpeg,
  747. "-y",
  748. "-i",
  749. str(video_path),
  750. "-q:v",
  751. "2",
  752. "-update",
  753. "1",
  754. str(output_path),
  755. ]
  756. process = None
  757. try:
  758. process = await asyncio.create_subprocess_exec(
  759. *cmd,
  760. stdout=asyncio.subprocess.PIPE,
  761. stderr=asyncio.subprocess.PIPE,
  762. )
  763. _, stderr = await asyncio.wait_for(process.communicate(), timeout=15.0)
  764. if process.returncode != 0:
  765. logger.warning(
  766. "ffmpeg failed extracting last frame from %s: %s",
  767. video_path,
  768. summarize_ffmpeg_stderr(stderr) or NO_FFMPEG_OUTPUT,
  769. )
  770. return False
  771. if not output_path.exists() or output_path.stat().st_size == 0:
  772. logger.warning("ffmpeg produced no output for %s", video_path)
  773. return False
  774. return True
  775. except TimeoutError:
  776. logger.warning("ffmpeg timed out extracting last frame from %s", video_path)
  777. return False
  778. except OSError as e:
  779. logger.warning("ffmpeg subprocess error for %s: %s", video_path, e)
  780. return False
  781. finally:
  782. # Killed however this ends, not only on its deadline (#3292).
  783. await reap_ffmpeg(process)
  784. def apply_camera_rotation(image_data: bytes, rotation: int, logger: logging.Logger) -> bytes:
  785. """Apply a camera_rotation value (degrees clockwise) to a captured JPEG.
  786. Shared by every capture path that saves a still image (notification
  787. snapshots, finish photos, layer-timelapse frames) - previously only
  788. wired into the notification-snapshot path, which left finish photos
  789. and timelapse videos upside-down whenever camera_rotation was set.
  790. Returns *image_data* itself (identity, not a copy) when there is nothing
  791. to do or the rotate fails; callers that write to disk use that to skip a
  792. pointless rewrite.
  793. """
  794. if not rotation:
  795. return image_data
  796. try:
  797. from io import BytesIO
  798. from PIL import Image
  799. img = Image.open(BytesIO(image_data))
  800. # PIL rotate is counter-clockwise, so negate for clockwise rotation
  801. img = img.rotate(-rotation, expand=True)
  802. buf = BytesIO()
  803. img.save(buf, format="JPEG", quality=90)
  804. rotated = buf.getvalue()
  805. # Debug, not info: layer-timelapse calls this once per layer, so a tall
  806. # print would otherwise put hundreds of lines in the log for something
  807. # the surrounding capture already reports at debug level.
  808. logger.debug("Applied %d° camera rotation: %s → %s bytes", rotation, len(image_data), len(rotated))
  809. return rotated
  810. except Exception as e:
  811. logger.warning("Failed to apply camera rotation: %s", e)
  812. return image_data
  813. async def apply_camera_rotation_to_file(path: Path, rotation: int, logger: logging.Logger) -> None:
  814. """Rotate a JPEG that has already been written to disk, in place.
  815. Two finish-photo sources never hold the frame as bytes - ``ffmpeg`` writes
  816. the file for them, and they return only a filename - so they can't use
  817. ``apply_camera_rotation`` directly. Best-effort: any failure leaves the
  818. unrotated file in place, which is what the caller had before.
  819. """
  820. if not rotation:
  821. return
  822. try:
  823. data = await asyncio.to_thread(path.read_bytes)
  824. rotated = await asyncio.to_thread(apply_camera_rotation, data, rotation, logger)
  825. if rotated is data:
  826. # Nothing was done (the rotate failed and returned its input) -
  827. # rewriting the same bytes would only risk truncating a good file.
  828. return
  829. await asyncio.to_thread(path.write_bytes, rotated)
  830. except Exception as e:
  831. logger.warning("Failed to rotate %s in place: %s", path.name, e)
  832. async def capture_finish_photo(
  833. printer_id: int,
  834. ip_address: str,
  835. access_code: str,
  836. model: str | None,
  837. archive_dir: Path,
  838. rotation: int = 0,
  839. ) -> str | None:
  840. """Capture a finish photo and save it to the archive's photos folder.
  841. Args:
  842. printer_id: ID of the printer
  843. ip_address: Printer IP address
  844. access_code: Printer access code
  845. model: Printer model
  846. archive_dir: Directory of the archive (where the 3MF is stored)
  847. rotation: Printer's configured camera_rotation (degrees clockwise).
  848. ffmpeg writes the file directly here, so the rotation is applied
  849. to it afterwards rather than to bytes in hand.
  850. Returns:
  851. Filename of the captured photo, or None if capture failed
  852. """
  853. # Create photos subdirectory
  854. photos_dir = archive_dir / "photos"
  855. photos_dir.mkdir(parents=True, exist_ok=True)
  856. # Generate filename with timestamp
  857. timestamp = datetime.now().strftime("%Y%m%d_%H%M%S")
  858. filename = f"finish_{timestamp}_{uuid.uuid4().hex[:8]}.jpg"
  859. output_path = (
  860. photos_dir / filename
  861. ) # SEC-PATH-OK: filename = f"finish_{timestamp}_{uuid.uuid4().hex[:8]}.jpg" generated above
  862. success = await capture_camera_frame(
  863. ip_address=ip_address,
  864. access_code=access_code,
  865. model=model,
  866. output_path=output_path,
  867. timeout=30,
  868. )
  869. if success:
  870. await apply_camera_rotation_to_file(output_path, rotation, logger)
  871. logger.info("Finish photo saved: %s", filename)
  872. return filename
  873. else:
  874. logger.warning("Failed to capture finish photo for printer %s", printer_id)
  875. return None
  876. async def test_camera_connection(
  877. ip_address: str,
  878. access_code: str,
  879. model: str | None,
  880. ) -> dict:
  881. """Test if the camera stream is accessible.
  882. Returns dict with success status and any error message.
  883. """
  884. import tempfile
  885. fd, tmp_name = tempfile.mkstemp(suffix=".jpg")
  886. os.close(fd)
  887. test_path = Path(tmp_name)
  888. test_path.chmod(0o600)
  889. try:
  890. success = await capture_camera_frame(
  891. ip_address=ip_address,
  892. access_code=access_code,
  893. model=model,
  894. output_path=test_path,
  895. timeout=15,
  896. )
  897. if success:
  898. return {"success": True, "message": "Camera connection successful"}
  899. else:
  900. return {
  901. "success": False,
  902. "error": (
  903. "Failed to capture frame from camera. "
  904. "Ensure the printer is powered on, camera is enabled, and Developer Mode is active. "
  905. "If running in Docker, try 'network_mode: host' in docker-compose.yml."
  906. ),
  907. }
  908. finally:
  909. # Clean up test file
  910. if test_path.exists():
  911. test_path.unlink()