使用分离方式,修改视频流返回方式,使用redis存是否在获取
This commit is contained in:
parent
8827fa0412
commit
f86c1fd3a4
|
|
@ -113,15 +113,22 @@ func GetVideoStream(c *gin.Context) {
|
||||||
log.Printf("device:%d frame close err:%v", deviceID, err4)
|
log.Printf("device:%d frame close err:%v", deviceID, err4)
|
||||||
}
|
}
|
||||||
}()
|
}()
|
||||||
|
errCount := 0
|
||||||
for {
|
for {
|
||||||
|
if errCount > 10 {
|
||||||
|
log.Printf("stream device:%d errCount > 10", deviceID)
|
||||||
|
return false
|
||||||
|
}
|
||||||
cnt := service.GetDeviceCurrentFrameV2(&frame, deviceID)
|
cnt := service.GetDeviceCurrentFrameV2(&frame, deviceID)
|
||||||
if cnt == count {
|
if cnt == count {
|
||||||
time.Sleep(50 * time.Millisecond)
|
time.Sleep(50 * time.Millisecond)
|
||||||
|
errCount++
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
if frame.Empty() {
|
if frame.Empty() {
|
||||||
log.Printf("stream device:%d frame is empty", deviceID)
|
log.Printf("stream device:%d frame is empty", deviceID)
|
||||||
time.Sleep(100 * time.Millisecond)
|
time.Sleep(100 * time.Millisecond)
|
||||||
|
errCount++
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
//gocv.Matrix转为jpeg
|
//gocv.Matrix转为jpeg
|
||||||
|
|
@ -191,9 +198,10 @@ func subscribeAndHandleMessagesV3(ws *websocket.Conn, device_id int) {
|
||||||
count := 0
|
count := 0
|
||||||
//定时器,发送计数器
|
//定时器,发送计数器
|
||||||
t_count := 0
|
t_count := 0
|
||||||
|
img := gocv.NewMat()
|
||||||
for {
|
for {
|
||||||
//从service获取当前帧
|
//从service获取当前帧
|
||||||
img, c := service.GetDeviceCurrentFrameV3(device_id)
|
c := service.GetDeviceCurrentFrameV2(&img, device_id)
|
||||||
if c != count {
|
if c != count {
|
||||||
//将img转[]byte
|
//将img转[]byte
|
||||||
if img.Empty() {
|
if img.Empty() {
|
||||||
|
|
@ -215,10 +223,6 @@ func subscribeAndHandleMessagesV3(ws *websocket.Conn, device_id int) {
|
||||||
break
|
break
|
||||||
}
|
}
|
||||||
c = count
|
c = count
|
||||||
err4 := img.Close()
|
|
||||||
if err4 != nil {
|
|
||||||
log.Printf("close img err:%v", err)
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
//每秒发送一次心跳检测
|
//每秒发送一次心跳检测
|
||||||
|
|
@ -236,6 +240,11 @@ func subscribeAndHandleMessagesV3(ws *websocket.Conn, device_id int) {
|
||||||
time.Sleep(100 * time.Millisecond)
|
time.Sleep(100 * time.Millisecond)
|
||||||
t_count++
|
t_count++
|
||||||
}
|
}
|
||||||
|
// 关闭img
|
||||||
|
err := img.Close()
|
||||||
|
if err != nil {
|
||||||
|
log.Printf("device:%d img close err:%v", device_id, err)
|
||||||
|
}
|
||||||
|
|
||||||
// 查看是否还有其他连接,没有则设置 is_play 为 0
|
// 查看是否还有其他连接,没有则设置 is_play 为 0
|
||||||
if worker.IsContainKey(online_conn_key) == false {
|
if worker.IsContainKey(online_conn_key) == false {
|
||||||
|
|
|
||||||
|
|
@ -109,17 +109,25 @@ func GetDeviceCurrentFrameV2(frame *gocv.Mat, deviceId int) int {
|
||||||
}
|
}
|
||||||
mutex.RLock()
|
mutex.RLock()
|
||||||
defer mutex.RUnlock()
|
defer mutex.RUnlock()
|
||||||
var frame_ gocv.Mat
|
|
||||||
//获取当前帧
|
//获取当前帧
|
||||||
frameIface, ok := DeviceCurrentFrameMap.Load(deviceId)
|
switch deviceId {
|
||||||
if !ok {
|
case 1:
|
||||||
return -1
|
if (*frame).Empty() {
|
||||||
|
*frame = gocv.NewMatWithSize(Device1CurrentFrame.Rows(), Device1CurrentFrame.Cols(), Device1CurrentFrame.Type())
|
||||||
}
|
}
|
||||||
frame_, ok = frameIface.(gocv.Mat)
|
Device1CurrentFrame.CopyTo(frame)
|
||||||
if !ok {
|
case 50:
|
||||||
log.Printf("DeviceCurrentFrameMap 存储的不是 gocv.Mat 类型,device_id: %d \n", deviceId)
|
if (*frame).Empty() {
|
||||||
|
*frame = gocv.NewMatWithSize(Device50CurrentFrame.Rows(), Device50CurrentFrame.Cols(), Device50CurrentFrame.Type())
|
||||||
|
}
|
||||||
|
Device50CurrentFrame.CopyTo(frame)
|
||||||
|
case 73:
|
||||||
|
if (*frame).Empty() {
|
||||||
|
*frame = gocv.NewMatWithSize(Device73CurrentFrame.Rows(), Device73CurrentFrame.Cols(), Device73CurrentFrame.Type())
|
||||||
|
}
|
||||||
|
Device73CurrentFrame.CopyTo(frame)
|
||||||
|
|
||||||
}
|
}
|
||||||
*frame = frame_
|
|
||||||
frameCount, ok := DeviceFrameCount[deviceId]
|
frameCount, ok := DeviceFrameCount[deviceId]
|
||||||
if !ok {
|
if !ok {
|
||||||
return -1
|
return -1
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue