- Settings 增加 video_open_timeout_sec(VIDEO_OPEN_TIMEOUT_SEC),默认 45s - SessionManager 全部就绪等待为该值 + 5s;StreamWorker 传入单路超时 - baked pipeline 回退默认值与配置对齐;.env.example 补充说明 - or_site_config.sample.json 扩展穿透摄像头与术间绑定示例 - 同步 uv.lock
124 lines
4.3 KiB
Python
124 lines
4.3 KiB
Python
"""单路 RTSP 拉流 worker:负责打开、重连、读帧分发。
|
||
|
||
从 ``CameraSessionManager._camera_worker`` 抽出,保持同样的行为:
|
||
- 打开失败 → 退避 → 重试。
|
||
- 连续读帧失败达到阈值 → 释放连接 → 退避 → 重试。
|
||
- 读到可用帧后交给上游 ``frame_handler``,由其决定是否推理 / 跳帧。
|
||
|
||
不知道手术会话、推理结果或数据库。日志中出现 RTSP URL 时会脱敏 user:password。
|
||
"""
|
||
|
||
from __future__ import annotations
|
||
|
||
import asyncio
|
||
import re
|
||
from typing import Awaitable, Callable
|
||
|
||
from loguru import logger
|
||
|
||
from app.baked import pipeline as bp
|
||
from app.services.video.rtsp_capture import RtspCapture
|
||
|
||
|
||
FrameHandler = Callable[[object], Awaitable[None]]
|
||
|
||
_RTSP_CRED_RE = re.compile(r"(?P<scheme>rtsp://)(?P<userinfo>[^@/\s]+@)")
|
||
|
||
|
||
def redact_rtsp_url(url: str | None) -> str:
|
||
"""把 ``rtsp://user:pwd@host/...`` 脱敏为 ``rtsp://***@host/...``。"""
|
||
if not url:
|
||
return ""
|
||
return _RTSP_CRED_RE.sub(r"\g<scheme>***@", url)
|
||
|
||
|
||
class CameraStreamWorker:
|
||
"""以 async 循环封装单路 RTSP 的重连/读帧,交由 handler 处理帧。"""
|
||
|
||
def __init__(
|
||
self,
|
||
*,
|
||
surgery_id: str,
|
||
camera_id: str,
|
||
url: str,
|
||
open_timeout_sec: float | None = None,
|
||
) -> None:
|
||
self._surgery_id = surgery_id
|
||
self._camera_id = camera_id
|
||
self._url = url
|
||
self._open_timeout_sec = (
|
||
float(open_timeout_sec) if open_timeout_sec is not None else bp.VIDEO_OPEN_TIMEOUT_SEC
|
||
)
|
||
|
||
async def run(
|
||
self,
|
||
*,
|
||
stream_ready: asyncio.Event,
|
||
stop_event: asyncio.Event,
|
||
frame_handler: FrameHandler,
|
||
) -> None:
|
||
cap: RtspCapture | None = None
|
||
consecutive_failures = 0
|
||
first_ready = True
|
||
safe_url = redact_rtsp_url(self._url)
|
||
|
||
try:
|
||
while not stop_event.is_set():
|
||
if cap is None:
|
||
try:
|
||
cap = RtspCapture(
|
||
self._url, open_timeout_sec=self._open_timeout_sec
|
||
)
|
||
await asyncio.to_thread(cap.open)
|
||
consecutive_failures = 0
|
||
if first_ready:
|
||
stream_ready.set()
|
||
first_ready = False
|
||
logger.info(
|
||
"RTSP stream opened camera={} surgery={} url={}",
|
||
self._camera_id,
|
||
self._surgery_id,
|
||
safe_url,
|
||
)
|
||
except Exception as exc:
|
||
logger.warning(
|
||
"RTSP open failed camera={} surgery={} url={}: {}",
|
||
self._camera_id,
|
||
self._surgery_id,
|
||
safe_url,
|
||
exc,
|
||
)
|
||
if cap is not None:
|
||
await asyncio.to_thread(cap.release)
|
||
cap = None
|
||
await asyncio.sleep(bp.VIDEO_RECONNECT_BACKOFF_SECONDS)
|
||
continue
|
||
|
||
ok, frame = await asyncio.to_thread(cap.read)
|
||
if not ok or frame is None:
|
||
consecutive_failures += 1
|
||
if (
|
||
consecutive_failures
|
||
>= bp.VIDEO_READ_FAILURE_RECONNECT_THRESHOLD
|
||
):
|
||
logger.warning(
|
||
"RTSP reconnect camera={} surgery={} url={} after {} read failures",
|
||
self._camera_id,
|
||
self._surgery_id,
|
||
safe_url,
|
||
consecutive_failures,
|
||
)
|
||
await asyncio.to_thread(cap.release)
|
||
cap = None
|
||
consecutive_failures = 0
|
||
await asyncio.sleep(bp.VIDEO_RECONNECT_BACKOFF_SECONDS)
|
||
else:
|
||
await asyncio.sleep(0.05)
|
||
continue
|
||
|
||
consecutive_failures = 0
|
||
await frame_handler(frame)
|
||
finally:
|
||
if cap is not None:
|
||
await asyncio.to_thread(cap.release)
|