modify: h5 插件系统更新
This commit is contained in:
@@ -18,25 +18,25 @@ class VideoStream(QObject):
|
||||
self.timer.timeout.connect(self.update_frame)
|
||||
self.retry_count = 0
|
||||
self.max_retry = 5
|
||||
|
||||
|
||||
self.open_stream()
|
||||
|
||||
|
||||
def open_stream(self):
|
||||
"""尝试打开RTSP视频流"""
|
||||
if self.cap:
|
||||
self.cap.release()
|
||||
|
||||
|
||||
# 创建VideoCapture对象并设置RTSP参数
|
||||
self.cap = cv2.VideoCapture(self.stream_url)
|
||||
|
||||
|
||||
# 关键设置:使用TCP传输(避免UDP丢包问题),设置超时和缓冲区
|
||||
# self.cap.set(cv2.CAP_PROP_RTSP_TRANSPORT, 1) # 1 = TCP传输模式
|
||||
self.cap.set(cv2.CAP_PROP_OPEN_TIMEOUT_MSEC, 5000) # 5秒超时
|
||||
self.cap.set(cv2.CAP_PROP_BUFFERSIZE, 1) # 最小化缓冲区
|
||||
|
||||
|
||||
# 打开RTSP流
|
||||
self.cap.open(self.stream_url)
|
||||
|
||||
|
||||
if not self.cap.isOpened():
|
||||
self.retry_count += 1
|
||||
if self.retry_count <= self.max_retry:
|
||||
@@ -46,11 +46,11 @@ class VideoStream(QObject):
|
||||
else:
|
||||
self.retry_count = 0
|
||||
self.timer.start(30) # 约30fps
|
||||
|
||||
|
||||
def update_frame(self):
|
||||
if not self.cap or not self.cap.isOpened():
|
||||
return
|
||||
|
||||
|
||||
ret, frame = self.cap.read()
|
||||
if ret:
|
||||
# 转换为黑白图像
|
||||
@@ -67,24 +67,25 @@ class VideoStream(QObject):
|
||||
else:
|
||||
# 读取失败时重新连接
|
||||
self.open_stream()
|
||||
|
||||
|
||||
def stop(self):
|
||||
self.timer.stop()
|
||||
if self.cap:
|
||||
self.cap.release()
|
||||
|
||||
|
||||
class CameraInterface(QWidget):
|
||||
def __init__(self, rtsp_url="rtsp://127.0.0.1:8554/stream"):
|
||||
super().__init__()
|
||||
self.setObjectName("camera_interface")
|
||||
self.setWindowTitle("RTSP视频流接收器")
|
||||
self.resize(800, 600)
|
||||
|
||||
|
||||
# 创建UI
|
||||
self.label = QLabel("等待RTSP视频流...")
|
||||
self.label.setAlignment(Qt.AlignmentFlag.AlignCenter)
|
||||
self.label.setStyleSheet("font-size: 20px; color: #888;")
|
||||
|
||||
|
||||
# 创建按钮
|
||||
self.listen_button = PushButton("开始监听RTSP")
|
||||
self.listen_button.clicked.connect(lambda: self.start_listen(rtsp_url))
|
||||
@@ -94,7 +95,7 @@ class CameraInterface(QWidget):
|
||||
layout.addWidget(self.label)
|
||||
layout.addWidget(self.listen_button)
|
||||
self.setLayout(layout)
|
||||
|
||||
|
||||
def start_listen(self, rtsp_url):
|
||||
self.listen_button.setEnabled(False)
|
||||
self.label.setText(f"正在连接: {rtsp_url}...")
|
||||
@@ -102,35 +103,37 @@ class CameraInterface(QWidget):
|
||||
self.stream = VideoStream(rtsp_url)
|
||||
self.stream.new_frame.connect(self.display_frame)
|
||||
self.stream.error_occurred.connect(self.display_error)
|
||||
|
||||
|
||||
def display_frame(self, image):
|
||||
pixmap = QPixmap.fromImage(image)
|
||||
self.label.setPixmap(pixmap.scaled(
|
||||
self.label.width(),
|
||||
self.label.height(),
|
||||
Qt.AspectRatioMode.KeepAspectRatio,
|
||||
Qt.TransformationMode.SmoothTransformation
|
||||
))
|
||||
|
||||
self.label.setPixmap(
|
||||
pixmap.scaled(
|
||||
self.label.width(),
|
||||
self.label.height(),
|
||||
Qt.AspectRatioMode.KeepAspectRatio,
|
||||
Qt.TransformationMode.SmoothTransformation,
|
||||
)
|
||||
)
|
||||
|
||||
def display_error(self, message):
|
||||
QMessageBox.critical(self, "RTSP视频流错误", message)
|
||||
self.label.setText(f"错误: {message}")
|
||||
self.listen_button.setEnabled(True) # 出错时重新启用按钮
|
||||
|
||||
|
||||
def closeEvent(self, event):
|
||||
if hasattr(self, 'stream') and self.stream:
|
||||
if hasattr(self, "stream") and self.stream:
|
||||
self.stream.stop()
|
||||
super().closeEvent(event)
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
app = QApplication(sys.argv)
|
||||
|
||||
|
||||
# 使用命令行参数指定RTSP URL
|
||||
rtsp_url = "rtsp://127.0.0.1:8554/stream" # 默认URL
|
||||
if len(sys.argv) > 1:
|
||||
rtsp_url = sys.argv[1]
|
||||
|
||||
|
||||
player = CameraInterface(rtsp_url)
|
||||
player.show()
|
||||
sys.exit(app.exec())
|
||||
|
||||
Reference in New Issue
Block a user