nexus-ai / socket_handlers /camera_handler.py
Jeetandar N Silwani
fix: image gen response parsing, camera DISABLE_CAMERA, lazy MediaPipe init
f021ba1
Raw History Blame Contribute Delete
1.5 kB
import os
import logging
from threading import Thread, Event, Lock
log = logging.getLogger(__name__)
_camera_thread = None
_camera_stop = Event()
_socketio_instance = None
_camera_lock = Lock()
def _start_camera():
global _camera_thread, _camera_stop, _socketio_instance
if not _socketio_instance:
log.warning("Cannot start camera: no socketio instance")
return
if os.getenv("DISABLE_CAMERA", "").strip() in ("1", "true", "yes"):
return
from camera_service import start_camera
with _camera_lock:
if _camera_thread and _camera_thread.is_alive():
return
_camera_stop.clear()
_camera_thread = Thread(
target=start_camera, args=(_socketio_instance, _camera_stop), daemon=True
)
_camera_thread.start()
log.info("Camera started")
def _stop_camera():
global _camera_stop, _camera_thread
with _camera_lock:
if not _camera_thread:
return
_camera_stop.set()
thread = _camera_thread
_camera_thread = None
thread.join(timeout=2)
log.info("Camera stopped")
def register_camera_handlers(socketio):
global _socketio_instance
_socketio_instance = socketio
@socketio.on("request_camera")
def on_request_camera():
log.info("request_camera received")
_start_camera()
@socketio.on("release_camera")
def on_release_camera():
log.info("release_camera received")
_stop_camera()