使用分离方式,修改视频流返回方式,使用redis存是否在获取

This commit is contained in:
lijun 2025-01-18 20:38:35 +08:00
parent 0906fce8cf
commit cead913af7
2 changed files with 73 additions and 56 deletions

View File

@ -65,34 +65,40 @@ type videoStreamReq struct {
} }
func GetVideoStream(c *gin.Context) { func GetVideoStream(c *gin.Context) {
var req videoStreamReq
id, _ := c.Get("id") id, _ := c.Get("id")
id1 := id.(int) 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 { if device.ID == 0 {
c.JSON(400, gin.H{"error": "device not exist"}) c.JSON(400, gin.H{"error": "device not exist"})
return return
} }
rKey := worker.GetRedis("video_stream_get_stream_key")
if rKey == "" {
rKey = "123456"
}
if err := c.ShouldBind(&req); err != nil {
c.JSON(400, gin.H{"error": err.Error()})
return
} else {
//查看id是否存在 //查看id是否存在
index := -1 index := -1
for _, device := range proto.Config.DeviceInfo { for _, device_ := range proto.Config.DeviceInfo {
if device.ID == req.ID { if device_.ID == deviceID {
index = req.ID index = deviceID
break break
} }
} }
if index == -1 { if index == -1 {
c.JSON(400, gin.H{"error": "id not exist"}) c.JSON(400, gin.H{"error": "id config not exist"})
return return
} }
//查看key是否正确 //查看key是否正确
if req.Key != "123456" { if key != rKey {
c.JSON(400, gin.H{"error": "key error"}) c.JSON(400, gin.H{"error": "key error"})
return return
} }
@ -100,7 +106,7 @@ func GetVideoStream(c *gin.Context) {
c.Stream(func(w io.Writer) bool { c.Stream(func(w io.Writer) bool {
var count int var count int
for { for {
frame, cnt := service.GetDeviceCurrentFrame(req.ID) frame, cnt := service.GetDeviceCurrentFrame(deviceID)
if cnt == count { if cnt == count {
time.Sleep(50 * time.Millisecond) time.Sleep(50 * time.Millisecond)
continue continue
@ -127,8 +133,6 @@ func GetVideoStream(c *gin.Context) {
time.Sleep(50 * time.Millisecond) // 控制帧率模拟每秒约20帧可按实际调整 time.Sleep(50 * time.Millisecond) // 控制帧率模拟每秒约20帧可按实际调整
} }
}) })
}
} }
// 发送实时视频流 // 发送实时视频流

13
main.go
View File

@ -51,6 +51,7 @@ func main() {
} }
c.Start() c.Start()
fmt.Println("定时任务已启动") fmt.Println("定时任务已启动")
initDeviceGettingStatus()
err3 := r.Run(":" + proto.Config.SERVER_PORT) err3 := r.Run(":" + proto.Config.SERVER_PORT)
if err3 != nil { if err3 != nil {
panic("failed to run server:" + err3.Error()) panic("failed to run server:" + err3.Error())
@ -87,6 +88,18 @@ func myTask() {
ReadConfigAndSetSystem() 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() { func ReadConfigAndSetSystem() {
//configPath := "/home/videoplayer/vp_stream.conf" //configPath := "/home/videoplayer/vp_stream.conf"
//读取配置文件 //读取配置文件