使用分离方式,修改视频流返回方式,使用redis存是否在获取
This commit is contained in:
parent
0906fce8cf
commit
cead913af7
116
handler/tool.go
116
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帧,可按实际调整
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
// 发送实时视频流
|
||||
|
|
|
|||
13
main.go
13
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"
|
||||
//读取配置文件
|
||||
|
|
|
|||
Loading…
Reference in New Issue