From cead913af7ac253bab7ef9cc7a46a51a58c710c3 Mon Sep 17 00:00:00 2001 From: lijun Date: Sat, 18 Jan 2025 20:38:35 +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 | 116 +++++++++++++++++++++++++----------------------- main.go | 13 ++++++ 2 files changed, 73 insertions(+), 56 deletions(-) diff --git a/handler/tool.go b/handler/tool.go index bc97577..cebb750 100644 --- a/handler/tool.go +++ b/handler/tool.go @@ -65,70 +65,74 @@ type videoStreamReq struct { } func GetVideoStream(c *gin.Context) { - var req videoStreamReq id, _ := c.Get("id") id1 := id.(int) + deviceIDSTR := c.Query("id") + deviceID, err := strconv.Atoi(deviceIDSTR) + if err != nil { + c.JSON(400, gin.H{"error": "device_id error"}) + return + } + key := c.Query("key") //校验权限 - device := service.GetDevice(req.ID, id1) + device := service.GetDevice(deviceID, id1) if device.ID == 0 { c.JSON(400, gin.H{"error": "device not exist"}) return } - - if err := c.ShouldBind(&req); err != nil { - c.JSON(400, gin.H{"error": err.Error()}) - return - } else { - //查看id是否存在 - index := -1 - for _, device := range proto.Config.DeviceInfo { - if device.ID == req.ID { - index = req.ID - break - } - } - if index == -1 { - c.JSON(400, gin.H{"error": "id not exist"}) - return - } - //查看key是否正确 - if req.Key != "123456" { - c.JSON(400, gin.H{"error": "key error"}) - return - } - //设备流 - c.Stream(func(w io.Writer) bool { - var count int - for { - frame, cnt := service.GetDeviceCurrentFrame(req.ID) - if cnt == count { - time.Sleep(50 * time.Millisecond) - continue - } - //gocv.Matrix转为jpeg - img, err := gocv.IMEncode(".jpg", frame) - frame_ := img.GetBytes() - - _, err = w.Write([]byte("--frame\r\nContent-Type: image/jpeg\r\n\r\n")) - if err != nil { - fmt.Printf("写入头部信息错误: %v\n", err) - return false - } - _, err = w.Write(frame_) - if err != nil { - fmt.Printf("写入帧数据错误: %v\n", err) - return false - } - _, err = w.Write([]byte("\r\n")) - if err != nil { - fmt.Printf("写入帧结束标记错误: %v\n", err) - return false - } - time.Sleep(50 * time.Millisecond) // 控制帧率,模拟每秒约20帧,可按实际调整 - } - }) - + rKey := worker.GetRedis("video_stream_get_stream_key") + if rKey == "" { + rKey = "123456" } + + //查看id是否存在 + index := -1 + for _, device_ := range proto.Config.DeviceInfo { + if device_.ID == deviceID { + index = deviceID + break + } + } + if index == -1 { + c.JSON(400, gin.H{"error": "id config not exist"}) + return + } + //查看key是否正确 + if key != rKey { + c.JSON(400, gin.H{"error": "key error"}) + return + } + //设备流 + c.Stream(func(w io.Writer) bool { + var count int + for { + frame, cnt := service.GetDeviceCurrentFrame(deviceID) + if cnt == count { + time.Sleep(50 * time.Millisecond) + continue + } + //gocv.Matrix转为jpeg + img, err := gocv.IMEncode(".jpg", frame) + frame_ := img.GetBytes() + + _, err = w.Write([]byte("--frame\r\nContent-Type: image/jpeg\r\n\r\n")) + if err != nil { + fmt.Printf("写入头部信息错误: %v\n", err) + return false + } + _, err = w.Write(frame_) + if err != nil { + fmt.Printf("写入帧数据错误: %v\n", err) + return false + } + _, err = w.Write([]byte("\r\n")) + if err != nil { + fmt.Printf("写入帧结束标记错误: %v\n", err) + return false + } + time.Sleep(50 * time.Millisecond) // 控制帧率,模拟每秒约20帧,可按实际调整 + } + }) } // 发送实时视频流 diff --git a/main.go b/main.go index 9765019..ec0aa5b 100644 --- a/main.go +++ b/main.go @@ -51,6 +51,7 @@ func main() { } c.Start() fmt.Println("定时任务已启动") + initDeviceGettingStatus() err3 := r.Run(":" + proto.Config.SERVER_PORT) if err3 != nil { panic("failed to run server:" + err3.Error()) @@ -87,6 +88,18 @@ func myTask() { ReadConfigAndSetSystem() } +func initDeviceGettingStatus() { + for _, device := range proto.Config.DeviceInfo { + if device.NextStop == false { + worker.SetRedis(fmt.Sprintf("device_%d_is_getting", device.ID), "false") + } + if device.NextStop == true { + worker.DelRedis(fmt.Sprintf("device_%d_is_getting", device.ID)) + } + } + +} + func ReadConfigAndSetSystem() { //configPath := "/home/videoplayer/vp_stream.conf" //读取配置文件