From f86c1fd3a4066b9a52b0ce0f2b2c05ed5f08d513 Mon Sep 17 00:00:00 2001 From: lijun Date: Sat, 18 Jan 2025 20:50:33 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BD=BF=E7=94=A8=E5=88=86=E7=A6=BB=E6=96=B9?= =?UTF-8?q?=E5=BC=8F=EF=BC=8C=E4=BF=AE=E6=94=B9=E8=A7=86=E9=A2=91=E6=B5=81?= =?UTF-8?q?=E8=BF=94=E5=9B=9E=E6=96=B9=E5=BC=8F,=E4=BD=BF=E7=94=A8redis?= =?UTF-8?q?=E5=AD=98=E6=98=AF=E5=90=A6=E5=9C=A8=E8=8E=B7=E5=8F=96?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- handler/tool.go | 19 ++++++++++++++----- service/tool.go | 26 +++++++++++++++++--------- 2 files changed, 31 insertions(+), 14 deletions(-) diff --git a/handler/tool.go b/handler/tool.go index e1f55ee..362c3f3 100644 --- a/handler/tool.go +++ b/handler/tool.go @@ -113,15 +113,22 @@ func GetVideoStream(c *gin.Context) { log.Printf("device:%d frame close err:%v", deviceID, err4) } }() + errCount := 0 for { + if errCount > 10 { + log.Printf("stream device:%d errCount > 10", deviceID) + return false + } cnt := service.GetDeviceCurrentFrameV2(&frame, deviceID) if cnt == count { time.Sleep(50 * time.Millisecond) + errCount++ continue } if frame.Empty() { log.Printf("stream device:%d frame is empty", deviceID) time.Sleep(100 * time.Millisecond) + errCount++ continue } //gocv.Matrix转为jpeg @@ -191,9 +198,10 @@ func subscribeAndHandleMessagesV3(ws *websocket.Conn, device_id int) { count := 0 //定时器,发送计数器 t_count := 0 + img := gocv.NewMat() for { //从service获取当前帧 - img, c := service.GetDeviceCurrentFrameV3(device_id) + c := service.GetDeviceCurrentFrameV2(&img, device_id) if c != count { //将img转[]byte if img.Empty() { @@ -215,10 +223,6 @@ func subscribeAndHandleMessagesV3(ws *websocket.Conn, device_id int) { break } c = count - err4 := img.Close() - if err4 != nil { - log.Printf("close img err:%v", err) - } } } else { //每秒发送一次心跳检测 @@ -236,6 +240,11 @@ func subscribeAndHandleMessagesV3(ws *websocket.Conn, device_id int) { time.Sleep(100 * time.Millisecond) t_count++ } + // 关闭img + err := img.Close() + if err != nil { + log.Printf("device:%d img close err:%v", device_id, err) + } // 查看是否还有其他连接,没有则设置 is_play 为 0 if worker.IsContainKey(online_conn_key) == false { diff --git a/service/tool.go b/service/tool.go index 25a264a..a2b2869 100644 --- a/service/tool.go +++ b/service/tool.go @@ -109,17 +109,25 @@ func GetDeviceCurrentFrameV2(frame *gocv.Mat, deviceId int) int { } mutex.RLock() defer mutex.RUnlock() - var frame_ gocv.Mat //获取当前帧 - frameIface, ok := DeviceCurrentFrameMap.Load(deviceId) - if !ok { - return -1 + switch deviceId { + case 1: + if (*frame).Empty() { + *frame = gocv.NewMatWithSize(Device1CurrentFrame.Rows(), Device1CurrentFrame.Cols(), Device1CurrentFrame.Type()) + } + Device1CurrentFrame.CopyTo(frame) + case 50: + if (*frame).Empty() { + *frame = gocv.NewMatWithSize(Device50CurrentFrame.Rows(), Device50CurrentFrame.Cols(), Device50CurrentFrame.Type()) + } + Device50CurrentFrame.CopyTo(frame) + case 73: + if (*frame).Empty() { + *frame = gocv.NewMatWithSize(Device73CurrentFrame.Rows(), Device73CurrentFrame.Cols(), Device73CurrentFrame.Type()) + } + Device73CurrentFrame.CopyTo(frame) + } - frame_, ok = frameIface.(gocv.Mat) - if !ok { - log.Printf("DeviceCurrentFrameMap 存储的不是 gocv.Mat 类型,device_id: %d \n", deviceId) - } - *frame = frame_ frameCount, ok := DeviceFrameCount[deviceId] if !ok { return -1