camera.py 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414
  1. """Camera streaming API endpoints for Bambu Lab printers."""
  2. import asyncio
  3. import logging
  4. from collections.abc import AsyncGenerator
  5. from fastapi import APIRouter, Depends, HTTPException, Request
  6. from fastapi.responses import Response, StreamingResponse
  7. from sqlalchemy import select
  8. from sqlalchemy.ext.asyncio import AsyncSession
  9. from backend.app.core.database import get_db
  10. from backend.app.models.printer import Printer
  11. from backend.app.services.camera import (
  12. capture_camera_frame,
  13. get_camera_port,
  14. get_ffmpeg_path,
  15. is_low_fps_model,
  16. test_camera_connection,
  17. )
  18. logger = logging.getLogger(__name__)
  19. router = APIRouter(prefix="/printers", tags=["camera"])
  20. # Track active ffmpeg processes for cleanup
  21. _active_streams: dict[str, asyncio.subprocess.Process] = {}
  22. # Store last frame for each printer (for photo capture from active stream)
  23. _last_frames: dict[int, bytes] = {}
  24. def get_buffered_frame(printer_id: int) -> bytes | None:
  25. """Get the last buffered frame for a printer from an active stream.
  26. Returns the JPEG frame data if available, or None if no active stream.
  27. """
  28. return _last_frames.get(printer_id)
  29. async def get_printer_or_404(printer_id: int, db: AsyncSession) -> Printer:
  30. """Get printer by ID or raise 404."""
  31. result = await db.execute(select(Printer).where(Printer.id == printer_id))
  32. printer = result.scalar_one_or_none()
  33. if not printer:
  34. raise HTTPException(status_code=404, detail="Printer not found")
  35. return printer
  36. async def generate_mjpeg_stream(
  37. ip_address: str,
  38. access_code: str,
  39. model: str | None,
  40. fps: int = 10,
  41. stream_id: str | None = None,
  42. disconnect_event: asyncio.Event | None = None,
  43. printer_id: int | None = None,
  44. ) -> AsyncGenerator[bytes, None]:
  45. """Generate MJPEG stream from printer camera using ffmpeg.
  46. This captures frames continuously and yields them in MJPEG format.
  47. """
  48. ffmpeg = get_ffmpeg_path()
  49. if not ffmpeg:
  50. logger.error("ffmpeg not found - camera streaming requires ffmpeg")
  51. yield (b"--frame\r\n" b"Content-Type: text/plain\r\n\r\n" b"Error: ffmpeg not installed\r\n")
  52. return
  53. port = get_camera_port(model)
  54. camera_url = f"rtsps://bblp:{access_code}@{ip_address}:{port}/streaming/live/1"
  55. low_fps = is_low_fps_model(model)
  56. # For A1/P1 models, use lower FPS and longer timeouts
  57. # These models have more limited camera streaming capability
  58. effective_fps = min(fps, 5) if low_fps else fps
  59. # ffmpeg command to output MJPEG stream to stdout
  60. # -rtsp_transport tcp: Use TCP for reliability
  61. # -rtsp_flags prefer_tcp: Prefer TCP for RTSP
  62. # -f mjpeg: Output as MJPEG
  63. # -q:v 5: Quality (lower = better, 2-10 is good range)
  64. # -r: Output framerate
  65. cmd = [
  66. ffmpeg,
  67. "-rtsp_transport",
  68. "tcp",
  69. "-rtsp_flags",
  70. "prefer_tcp",
  71. ]
  72. # Add longer timeouts for A1/P1 models which may be slower to respond
  73. if low_fps:
  74. cmd.extend(
  75. [
  76. "-timeout",
  77. "10000000", # 10 seconds in microseconds (replaces deprecated -stimeout)
  78. "-analyzeduration",
  79. "10000000", # Longer analysis time
  80. "-probesize",
  81. "5000000", # Larger probe size
  82. ]
  83. )
  84. cmd.extend(
  85. [
  86. "-i",
  87. camera_url,
  88. "-f",
  89. "mjpeg",
  90. "-q:v",
  91. "5",
  92. "-r",
  93. str(effective_fps),
  94. "-an", # No audio
  95. "-", # Output to stdout
  96. ]
  97. )
  98. logger.info(f"Starting camera stream for {ip_address} (stream_id={stream_id}, model={model}, fps={effective_fps})")
  99. if low_fps:
  100. logger.info(f"Using extended timeouts for {model} camera")
  101. logger.debug(f"ffmpeg command: {ffmpeg} ... (url hidden)")
  102. process = None
  103. try:
  104. process = await asyncio.create_subprocess_exec(
  105. *cmd,
  106. stdout=asyncio.subprocess.PIPE,
  107. stderr=asyncio.subprocess.PIPE,
  108. )
  109. # Track active process for cleanup
  110. if stream_id:
  111. _active_streams[stream_id] = process
  112. # Give ffmpeg a moment to start and check for immediate failures
  113. # A1/P1 models may need longer to establish connection
  114. startup_wait = 2.0 if low_fps else 0.5
  115. await asyncio.sleep(startup_wait)
  116. if process.returncode is not None:
  117. stderr = await process.stderr.read()
  118. logger.error(f"ffmpeg failed immediately: {stderr.decode()}")
  119. yield (
  120. b"--frame\r\n"
  121. b"Content-Type: text/plain\r\n\r\n"
  122. b"Error: Camera connection failed. Check printer is on and camera is enabled.\r\n"
  123. )
  124. return
  125. # Read JPEG frames from ffmpeg output
  126. # JPEG images start with 0xFFD8 and end with 0xFFD9
  127. buffer = b""
  128. jpeg_start = b"\xff\xd8"
  129. jpeg_end = b"\xff\xd9"
  130. while True:
  131. # Check if client disconnected
  132. if disconnect_event and disconnect_event.is_set():
  133. logger.info(f"Client disconnected, stopping stream {stream_id}")
  134. break
  135. try:
  136. # Read chunk from ffmpeg
  137. # A1/P1 models may have longer gaps between frames
  138. read_timeout = 30.0 if low_fps else 10.0
  139. chunk = await asyncio.wait_for(process.stdout.read(8192), timeout=read_timeout)
  140. if not chunk:
  141. logger.warning("Camera stream ended (no more data)")
  142. break
  143. buffer += chunk
  144. # Find complete JPEG frames in buffer
  145. while True:
  146. start_idx = buffer.find(jpeg_start)
  147. if start_idx == -1:
  148. # No start marker, clear buffer up to last 2 bytes
  149. buffer = buffer[-2:] if len(buffer) > 2 else buffer
  150. break
  151. # Trim anything before the start marker
  152. if start_idx > 0:
  153. buffer = buffer[start_idx:]
  154. end_idx = buffer.find(jpeg_end, 2) # Skip first 2 bytes
  155. if end_idx == -1:
  156. # No end marker yet, wait for more data
  157. break
  158. # Extract complete frame
  159. frame = buffer[: end_idx + 2]
  160. buffer = buffer[end_idx + 2 :]
  161. # Save frame to buffer for photo capture
  162. if printer_id is not None:
  163. _last_frames[printer_id] = frame
  164. # Yield frame in MJPEG format
  165. yield (
  166. b"--frame\r\n"
  167. b"Content-Type: image/jpeg\r\n"
  168. b"Content-Length: " + str(len(frame)).encode() + b"\r\n"
  169. b"\r\n" + frame + b"\r\n"
  170. )
  171. except TimeoutError:
  172. logger.warning("Camera stream read timeout")
  173. break
  174. except asyncio.CancelledError:
  175. logger.info(f"Camera stream cancelled (stream_id={stream_id})")
  176. break
  177. except GeneratorExit:
  178. logger.info(f"Camera stream generator exit (stream_id={stream_id})")
  179. break
  180. except FileNotFoundError:
  181. logger.error("ffmpeg not found - camera streaming requires ffmpeg")
  182. yield (b"--frame\r\n" b"Content-Type: text/plain\r\n\r\n" b"Error: ffmpeg not installed\r\n")
  183. except asyncio.CancelledError:
  184. logger.info(f"Camera stream task cancelled (stream_id={stream_id})")
  185. except GeneratorExit:
  186. logger.info(f"Camera stream generator closed (stream_id={stream_id})")
  187. except Exception as e:
  188. logger.exception(f"Camera stream error: {e}")
  189. finally:
  190. # Remove from active streams
  191. if stream_id and stream_id in _active_streams:
  192. del _active_streams[stream_id]
  193. # Clean up frame buffer
  194. if printer_id is not None and printer_id in _last_frames:
  195. del _last_frames[printer_id]
  196. if process and process.returncode is None:
  197. logger.info(f"Terminating ffmpeg process for stream {stream_id}")
  198. try:
  199. process.terminate()
  200. try:
  201. await asyncio.wait_for(process.wait(), timeout=2.0)
  202. except TimeoutError:
  203. logger.warning(f"ffmpeg didn't terminate gracefully, killing (stream_id={stream_id})")
  204. process.kill()
  205. await process.wait()
  206. except ProcessLookupError:
  207. pass # Process already dead
  208. except Exception as e:
  209. logger.warning(f"Error terminating ffmpeg: {e}")
  210. logger.info(f"Camera stream stopped for {ip_address} (stream_id={stream_id})")
  211. @router.get("/{printer_id}/camera/stream")
  212. async def camera_stream(
  213. printer_id: int,
  214. request: Request,
  215. fps: int = 10,
  216. db: AsyncSession = Depends(get_db),
  217. ):
  218. """Stream live video from printer camera as MJPEG.
  219. This endpoint returns a multipart MJPEG stream that can be used directly
  220. in an <img> tag or video player.
  221. Args:
  222. printer_id: Printer ID
  223. fps: Target frames per second (default: 10, max: 30)
  224. """
  225. import uuid
  226. printer = await get_printer_or_404(printer_id, db)
  227. # Validate FPS
  228. fps = min(max(fps, 1), 30)
  229. # Generate unique stream ID for tracking
  230. stream_id = f"{printer_id}-{uuid.uuid4().hex[:8]}"
  231. # Create disconnect event that will be set when client disconnects
  232. disconnect_event = asyncio.Event()
  233. async def stream_with_disconnect_check():
  234. """Wrapper generator that monitors for client disconnect."""
  235. try:
  236. async for chunk in generate_mjpeg_stream(
  237. ip_address=printer.ip_address,
  238. access_code=printer.access_code,
  239. model=printer.model,
  240. fps=fps,
  241. stream_id=stream_id,
  242. disconnect_event=disconnect_event,
  243. printer_id=printer_id,
  244. ):
  245. # Check if client is still connected
  246. if await request.is_disconnected():
  247. logger.info(f"Client disconnected detected for stream {stream_id}")
  248. disconnect_event.set()
  249. break
  250. yield chunk
  251. except asyncio.CancelledError:
  252. logger.info(f"Stream {stream_id} cancelled")
  253. disconnect_event.set()
  254. except GeneratorExit:
  255. logger.info(f"Stream {stream_id} generator closed")
  256. disconnect_event.set()
  257. finally:
  258. disconnect_event.set()
  259. # Give a moment for the inner generator to clean up
  260. await asyncio.sleep(0.1)
  261. return StreamingResponse(
  262. stream_with_disconnect_check(),
  263. media_type="multipart/x-mixed-replace; boundary=frame",
  264. headers={
  265. "Cache-Control": "no-cache, no-store, must-revalidate",
  266. "Pragma": "no-cache",
  267. "Expires": "0",
  268. },
  269. )
  270. @router.api_route("/{printer_id}/camera/stop", methods=["GET", "POST"])
  271. async def stop_camera_stream(printer_id: int):
  272. """Stop all active camera streams for a printer.
  273. This can be called by the frontend when the camera window is closed.
  274. Accepts both GET and POST (POST for sendBeacon compatibility).
  275. """
  276. stopped = 0
  277. to_remove = []
  278. for stream_id, process in list(_active_streams.items()):
  279. if stream_id.startswith(f"{printer_id}-"):
  280. to_remove.append(stream_id)
  281. if process.returncode is None:
  282. try:
  283. process.terminate()
  284. stopped += 1
  285. logger.info(f"Terminated ffmpeg process for stream {stream_id}")
  286. except Exception as e:
  287. logger.warning(f"Error stopping stream {stream_id}: {e}")
  288. for stream_id in to_remove:
  289. _active_streams.pop(stream_id, None)
  290. logger.info(
  291. f"Stopped {stopped} camera stream(s) for printer {printer_id}, active streams remaining: {list(_active_streams.keys())}"
  292. )
  293. return {"stopped": stopped}
  294. @router.get("/{printer_id}/camera/snapshot")
  295. async def camera_snapshot(
  296. printer_id: int,
  297. db: AsyncSession = Depends(get_db),
  298. ):
  299. """Capture a single frame from the printer camera.
  300. Returns a JPEG image.
  301. """
  302. import tempfile
  303. from pathlib import Path
  304. printer = await get_printer_or_404(printer_id, db)
  305. # Create temporary file for the snapshot
  306. with tempfile.NamedTemporaryFile(suffix=".jpg", delete=False) as f:
  307. temp_path = Path(f.name)
  308. try:
  309. success = await capture_camera_frame(
  310. ip_address=printer.ip_address,
  311. access_code=printer.access_code,
  312. model=printer.model,
  313. output_path=temp_path,
  314. timeout=15,
  315. )
  316. if not success:
  317. raise HTTPException(status_code=503, detail="Failed to capture camera frame. Is the printer powered on?")
  318. # Read and return the image
  319. with open(temp_path, "rb") as f:
  320. image_data = f.read()
  321. return Response(
  322. content=image_data,
  323. media_type="image/jpeg",
  324. headers={
  325. "Cache-Control": "no-cache, no-store, must-revalidate",
  326. "Content-Disposition": f'inline; filename="snapshot_{printer_id}.jpg"',
  327. },
  328. )
  329. finally:
  330. # Clean up temp file
  331. if temp_path.exists():
  332. temp_path.unlink()
  333. @router.get("/{printer_id}/camera/test")
  334. async def test_camera(
  335. printer_id: int,
  336. db: AsyncSession = Depends(get_db),
  337. ):
  338. """Test camera connection for a printer.
  339. Returns success status and any error message.
  340. """
  341. printer = await get_printer_or_404(printer_id, db)
  342. result = await test_camera_connection(
  343. ip_address=printer.ip_address,
  344. access_code=printer.access_code,
  345. model=printer.model,
  346. )
  347. return result