|
|
@@ -121,11 +121,20 @@ class PanoramaCamera:
|
|
|
rtsp_url = self.config.get('rtsp_url') or f"rtsp://{self.config['username']}:{self.config['password']}@{self.config['ip']}:{self.config.get('rtsp_port', 554)}/h264/ch{self.config['channel']}/main/av_stream"
|
|
|
|
|
|
try:
|
|
|
- self.rtsp_cap = cv2.VideoCapture(rtsp_url)
|
|
|
+ # 使用 FFmpeg 单线程模式避免线程安全崩溃
|
|
|
+ # 设置环境变量禁用多线程解码
|
|
|
+ import os
|
|
|
+ os.environ['OPENCV_FFMPEG_CAPTURE_OPTIONS'] = 'threads;1'
|
|
|
+
|
|
|
+ # 使用 CAP_FFMPEG 后端并设置单线程
|
|
|
+ self.rtsp_cap = cv2.VideoCapture(rtsp_url, cv2.CAP_FFMPEG)
|
|
|
if not self.rtsp_cap.isOpened():
|
|
|
print(f"无法打开RTSP流: {rtsp_url}")
|
|
|
return False
|
|
|
|
|
|
+ # 设置缓冲区大小为1,减少延迟
|
|
|
+ self.rtsp_cap.set(cv2.CAP_PROP_BUFFERSIZE, 1)
|
|
|
+
|
|
|
self.running = True
|
|
|
self.stream_thread = threading.Thread(target=self._rtsp_stream_worker, daemon=True)
|
|
|
self.stream_thread.start()
|
|
|
@@ -174,7 +183,7 @@ class PanoramaCamera:
|
|
|
rtsp_url = self._build_rtsp_url()
|
|
|
try:
|
|
|
if self.rtsp_cap is None:
|
|
|
- self.rtsp_cap = cv2.VideoCapture(rtsp_url)
|
|
|
+ self.rtsp_cap = cv2.VideoCapture(rtsp_url, cv2.CAP_FFMPEG)
|
|
|
self.rtsp_cap.set(cv2.CAP_PROP_BUFFERSIZE, 1) # 减少缓冲延迟
|
|
|
|
|
|
if self.rtsp_cap.isOpened():
|