From c6bc92c4de3c08dfcbe5b8fad4ff6c439bf6501f Mon Sep 17 00:00:00 2001 From: lijun Date: Thu, 23 Jan 2025 22:31:04 +0800 Subject: [PATCH] =?UTF-8?q?=E8=8E=B7=E5=8F=96=E5=BD=93=E5=89=8D=E5=B8=A7by?= =?UTF-8?q?tes?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- handler/tool.go | 47 +++++++++-------------------------------------- service/tool.go | 46 ++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 55 insertions(+), 38 deletions(-) diff --git a/handler/tool.go b/handler/tool.go index 5639bce..c1f2d91 100644 --- a/handler/tool.go +++ b/handler/tool.go @@ -213,52 +213,23 @@ func subscribeAndHandleMessagesV3(ws *websocket.Conn, deviceId int) { //图片计数器 count := 0 //定时器,发送计数器 - t_count := 0 - t := 0 - img := gocv.NewMat() + tCount := 0 //计算帧率 for { //从service获取当前帧 - c := service.GetDeviceCurrentFrameV2(&img, deviceId) + buf, c := service.GetDeviceCurrentFrameV4(deviceId, count) if c != count { - if c == -1 { - log.Printf("device:%d get frame err!", deviceId) + count = c + err2 := ws.WriteMessage(websocket.BinaryMessage, buf) + if err2 != nil { + log.Printf("send message to client err:%v", err2) worker.SetRedisSetRemove(online_conn_key, con_id) break } - //将img转[]byte - if img.Empty() { - log.Printf("device:%d img is empty! count = %d \n", deviceId, c) - } else { - //gocv.Matrix转为jpeg - buf, err := gocv.IMEncode(".jpg", img) - if err != nil { - log.Printf("img encode err:%v", err) - worker.SetRedisSetRemove(online_conn_key, con_id) - break - } - buf1 := buf.GetBytes() - - err2 := ws.WriteMessage(websocket.BinaryMessage, buf1) - if err2 != nil { - log.Printf("send message to client err:%v", err2) - worker.SetRedisSetRemove(online_conn_key, con_id) - break - } - c = count - err5 := img.Close() - if err5 != nil { - log.Printf("img close err:%v", err) - } - buf.Close() - t++ - if t%50 == 0 { - log.Printf("device:%d send frame count:%d,img and buf had been closed", deviceId, c) - } - } + c = count } else { //每秒发送一次心跳检测 - if t_count%10 == 0 { + if tCount%10 == 0 { err := ws.WriteControl(websocket.PingMessage, []byte{}, time.Now().Add(time.Second)) if err != nil { log.Printf("Connection check failed:%v", err) @@ -270,7 +241,7 @@ func subscribeAndHandleMessagesV3(ws *websocket.Conn, deviceId int) { } } time.Sleep(100 * time.Millisecond) - t_count++ + tCount++ } // 查看是否还有其他连接,没有则设置 is_play 为 0 diff --git a/service/tool.go b/service/tool.go index d48ee56..3f2dd2d 100644 --- a/service/tool.go +++ b/service/tool.go @@ -93,6 +93,52 @@ func SetDeviceCurrentFrameV2(frame *gocv.Mat, deviceId int) error { return nil } +func GetDeviceCurrentFrameV4(deviceId int, curCount int) (buf []byte, cnt int) { + defer func() { + if err := recover(); err != nil { + log.Printf("设备:%d 错误: %v\n", deviceId, err) + } + }() + //获取读写锁 + mutex_, ok := DeviceRWMap.Load(deviceId) + if !ok { + log.Printf("DeviceRWMap 读写锁不存在,device_id: %d \n", deviceId) + return nil, -1 + } + mutex, ok := mutex_.(*sync.RWMutex) + if !ok { + log.Printf("DeviceRWMap 存储的不是 *sync.RWMutex 类型,device_id: %d \n", deviceId) + return nil, -1 + } + mutex.RLock() + defer mutex.RUnlock() + frameCount, ok := DeviceFrameCount[deviceId] + if !ok { + return buf, -1 + } + if frameCount == curCount { + return nil, frameCount + } + //获取当前帧 + switch deviceId { + case 1: + if Device1CurrentFrame.Empty() { + return nil, -1 + } + buf = Device1CurrentFrame.ToBytes() + case 50: + if Device50CurrentFrame.Empty() { + return nil, -1 + } + buf = Device50CurrentFrame.ToBytes() + case 73: + if Device73CurrentFrame.Empty() { + return nil, -1 + } + } + return buf, frameCount +} + func GetDeviceCurrentFrameV2(frame *gocv.Mat, deviceId int) int { defer func() { if err := recover(); err != nil {