From 8827fa0412ea660470f0dd27452e00fb68e9b710 Mon Sep 17 00:00:00 2001 From: lijun Date: Sat, 18 Jan 2025 20:43:28 +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 | 23 ++++++++++++++++++++--- main.go | 2 +- 2 files changed, 21 insertions(+), 4 deletions(-) diff --git a/handler/tool.go b/handler/tool.go index cebb750..e1f55ee 100644 --- a/handler/tool.go +++ b/handler/tool.go @@ -34,7 +34,7 @@ func SetUpToolGroup(router *gin.Engine) { } // 跨域访问:cross origin resource share -func CrosHandler() gin.HandlerFunc { +func CorsHandler() gin.HandlerFunc { return func(context *gin.Context) { //method := context.Request.Method context.Writer.Header().Set("Access-Control-Allow-Origin", "*") @@ -105,14 +105,31 @@ func GetVideoStream(c *gin.Context) { //设备流 c.Stream(func(w io.Writer) bool { var count int + frame := gocv.NewMat() + defer func() { + //关闭帧 + err4 := frame.Close() + if err4 != nil { + log.Printf("device:%d frame close err:%v", deviceID, err4) + } + }() for { - frame, cnt := service.GetDeviceCurrentFrame(deviceID) + cnt := service.GetDeviceCurrentFrameV2(&frame, deviceID) if cnt == count { time.Sleep(50 * time.Millisecond) continue } + if frame.Empty() { + log.Printf("stream device:%d frame is empty", deviceID) + time.Sleep(100 * time.Millisecond) + continue + } //gocv.Matrix转为jpeg - img, err := gocv.IMEncode(".jpg", frame) + img, err2 := gocv.IMEncode(".jpg", frame) + if err2 != nil { + log.Printf("stream img encode err:%v", err2) + return false + } frame_ := img.GetBytes() _, err = w.Write([]byte("--frame\r\nContent-Type: image/jpeg\r\n\r\n")) diff --git a/main.go b/main.go index ec0aa5b..0636f90 100644 --- a/main.go +++ b/main.go @@ -35,7 +35,7 @@ func main() { if err0 != nil { panic("failed to connect redis:" + err0.Error()) } - r.Use(handler.CrosHandler()) + r.Use(handler.CorsHandler()) r.Use(JWTAuthMiddleware()) // 使用 JWT 认证中间件 handler.SetUpToolGroup(r) // Tool err := worker.InitRedis()