-
Notifications
You must be signed in to change notification settings - Fork 41
Add /api/camera/stream endpoint for MJPEG video streaming #405
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: develop
Are you sure you want to change the base?
Changes from 2 commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,57 @@ | ||
| """Camera streaming API routes.""" | ||
|
|
||
| import asyncio | ||
|
|
||
| import cv2 | ||
| from fastapi import APIRouter, Depends | ||
| from fastapi.responses import StreamingResponse | ||
|
|
||
| from ...backend.mujoco.backend import MujocoBackend | ||
| from ...daemon import Daemon | ||
| from reachy_mini.media.camera_constants import CameraResolution | ||
| from reachy_mini.media.camera_opencv import OpenCVCamera | ||
| from ..dependencies import get_backend, get_daemon | ||
|
Check failure on line 13 in src/reachy_mini/daemon/app/routers/camera.py
|
||
|
|
||
| router = APIRouter(prefix="/camera") | ||
|
|
||
| _shared_camera = None | ||
| _camera_refs = 0 | ||
|
|
||
| async def _get_shared_camera(is_sim: bool): | ||
| global _shared_camera, _camera_refs | ||
| if _shared_camera is None: | ||
| _shared_camera = OpenCVCamera(log_level="WARNING", resolution=CameraResolution.R1280x720) | ||
| _shared_camera.open(udp_camera="udp://@127.0.0.1:5005" if is_sim else None) | ||
| _camera_refs += 1 | ||
| return _shared_camera | ||
|
|
||
| def _release_camera(): | ||
| global _shared_camera, _camera_refs | ||
| _camera_refs -= 1 | ||
|
Comment on lines
29
to
37
|
||
| if _camera_refs <= 0 and _shared_camera is not None: | ||
| _shared_camera.close() | ||
| _shared_camera = None | ||
| _camera_refs = 0 | ||
|
|
||
|
||
| @router.get("/stream") | ||
| async def stream_camera( | ||
| backend=Depends(get_backend), | ||
| daemon: Daemon=Depends(get_daemon), | ||
| ) -> StreamingResponse: | ||
| """Stream camera feed as MJPEG.""" | ||
| async def _stream(): | ||
| is_sim = daemon.status().simulation_enabled and isinstance(backend, MujocoBackend) | ||
| cam = await _get_shared_camera(is_sim) | ||
|
Comment on lines
22
to
55
|
||
| try: | ||
| while True: | ||
| f = cam.read() | ||
| if f is not None: | ||
|
Comment on lines
49
to
59
|
||
| f = cv2.resize(f, (640, 480)) | ||
| _, j = cv2.imencode(".jpg", f, [cv2.IMWRITE_JPEG_QUALITY, 80]) | ||
| if _: | ||
| yield b"--frame\r\nContent-Type: image/jpeg\r\n\r\n" + j.tobytes() + b"\r\n" | ||
| await asyncio.sleep(0.04) | ||
| finally: | ||
| _release_camera() | ||
| return StreamingResponse(_stream(), media_type="multipart/x-mixed-replace; boundary=frame") | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Global state management without thread safety. The
_shared_cameraand_camera_refsvariables are accessed and modified by multiple concurrent requests without synchronization (e.g., locks). This can lead to race conditions where:_shared_camera is Noneand create multiple camera instancesConsider using
asyncio.Lockorthreading.Lockto protect these shared variables.