Files
operating-room-monitor-server/app/services/video/stream_worker.py
Kevin 3d7bd70355 feat: 手术视频消耗、待确认与持久化改造
- 新增 Alembic 初始迁移、领域明细模型及归档持久化与重试链路\n- 拆分视频会话注册表、分类处理、推理时间窗聚合与流处理\n- 消耗日志:TSV/Markdown 含 top2/top3;item_id 优先产品编码;待确认记「待确认」行,语音确认后落正式行并更新汇总\n- 待确认时内存/DB 明细为占位行,确认后替换;拒绝时移除占位\n- 分类 probs 先 detach/cpu 再转 NumPy,修复 MPS/CUDA 上推理被静默跳过\n- 补充集成测试、归档与设备张量等单测

Made-with: Cursor
2026-04-23 20:42:21 +08:00

122 lines
4.2 KiB
Python
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
"""单路 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.config import Settings
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,
*,
settings: Settings,
surgery_id: str,
camera_id: str,
url: str,
) -> None:
self._s = settings
self._surgery_id = surgery_id
self._camera_id = camera_id
self._url = url
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._s.video_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(self._s.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
>= self._s.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(self._s.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)