Refactor MediaMTX configuration and enhance RTSP streaming logging

- Removed outdated timeout settings from MediaMTX configuration for improved stream handling.
- Updated CameraStreamer class to include detailed logging for RTSP streaming state and frame dimension checks.
- Added environment variable support for configuring MediaMTX host, enhancing flexibility in deployment.
This commit is contained in:
salirezav
2025-11-01 12:58:30 -04:00
parent b7adc3788a
commit 3cb6a8e76e
4 changed files with 163 additions and 7 deletions

View File

@@ -86,6 +86,9 @@ class CameraStreamer:
# RTSP settings
self.rtsp_fps = 15.0 # RTSP FPS (can be higher than MJPEG preview)
# Use MEDIAMTX_HOST env var if set, otherwise default to localhost
# Note: If API uses network_mode: host, MediaMTX container ports are exposed to host
# So localhost should work, but MediaMTX must be accessible on that port
self.rtsp_host = os.getenv("MEDIAMTX_HOST", "localhost")
self.rtsp_port = int(os.getenv("MEDIAMTX_RTSP_PORT", "8554"))
self._rtsp_process: Optional[subprocess.Popen] = None
@@ -525,15 +528,27 @@ class CameraStreamer:
# Wait for frame dimensions to be set
timeout = 10.0 # Wait up to 10 seconds for first frame
start_time = time.time()
self.logger.info(f"Waiting for frame dimensions (current: {self._rtsp_frame_width}x{self._rtsp_frame_height})...")
while self._rtsp_frame_width == 0 and (time.time() - start_time) < timeout:
if self._stop_rtsp_event.is_set():
self.logger.info("RTSP streaming stopped before frame dimensions were available")
return
time.sleep(0.1)
# Check if streaming is actually producing frames
if not self.streaming:
self.logger.error("Camera streaming stopped, cannot start RTSP")
self.rtsp_streaming = False
return
time.sleep(0.5)
elapsed = time.time() - start_time
if elapsed > 2 and int(elapsed) % 2 == 0: # Log every 2 seconds
self.logger.debug(f"Still waiting for frame dimensions... ({elapsed:.1f}s elapsed, queue size: {self._rtsp_frame_queue.qsize()})")
if self._rtsp_frame_width == 0:
self.logger.error("Could not determine frame dimensions for RTSP streaming")
self.logger.error(f"Could not determine frame dimensions for RTSP streaming after {timeout}s. Stream active: {self.streaming}, Queue size: {self._rtsp_frame_queue.qsize()}")
self.rtsp_streaming = False
return
self.logger.info(f"Frame dimensions available: {self._rtsp_frame_width}x{self._rtsp_frame_height}")
rtsp_url = f"rtsp://{self.rtsp_host}:{self.rtsp_port}/{self.camera_config.name}"
self.logger.info(f"Publishing RTSP stream to {rtsp_url} with dimensions {self._rtsp_frame_width}x{self._rtsp_frame_height} @ {self.rtsp_fps}fps")