获取当前帧bytes
This commit is contained in:
parent
9362523c39
commit
c6bc92c4de
|
|
@ -213,52 +213,23 @@ func subscribeAndHandleMessagesV3(ws *websocket.Conn, deviceId int) {
|
||||||
//图片计数器
|
//图片计数器
|
||||||
count := 0
|
count := 0
|
||||||
//定时器,发送计数器
|
//定时器,发送计数器
|
||||||
t_count := 0
|
tCount := 0
|
||||||
t := 0
|
|
||||||
img := gocv.NewMat()
|
|
||||||
//计算帧率
|
//计算帧率
|
||||||
for {
|
for {
|
||||||
//从service获取当前帧
|
//从service获取当前帧
|
||||||
c := service.GetDeviceCurrentFrameV2(&img, deviceId)
|
buf, c := service.GetDeviceCurrentFrameV4(deviceId, count)
|
||||||
if c != count {
|
if c != count {
|
||||||
if c == -1 {
|
count = c
|
||||||
log.Printf("device:%d get frame err!", deviceId)
|
err2 := ws.WriteMessage(websocket.BinaryMessage, buf)
|
||||||
worker.SetRedisSetRemove(online_conn_key, con_id)
|
|
||||||
break
|
|
||||||
}
|
|
||||||
//将img转[]byte
|
|
||||||
if img.Empty() {
|
|
||||||
log.Printf("device:%d img is empty! count = %d \n", deviceId, c)
|
|
||||||
} else {
|
|
||||||
//gocv.Matrix转为jpeg
|
|
||||||
buf, err := gocv.IMEncode(".jpg", img)
|
|
||||||
if err != nil {
|
|
||||||
log.Printf("img encode err:%v", err)
|
|
||||||
worker.SetRedisSetRemove(online_conn_key, con_id)
|
|
||||||
break
|
|
||||||
}
|
|
||||||
buf1 := buf.GetBytes()
|
|
||||||
|
|
||||||
err2 := ws.WriteMessage(websocket.BinaryMessage, buf1)
|
|
||||||
if err2 != nil {
|
if err2 != nil {
|
||||||
log.Printf("send message to client err:%v", err2)
|
log.Printf("send message to client err:%v", err2)
|
||||||
worker.SetRedisSetRemove(online_conn_key, con_id)
|
worker.SetRedisSetRemove(online_conn_key, con_id)
|
||||||
break
|
break
|
||||||
}
|
}
|
||||||
c = count
|
c = count
|
||||||
err5 := img.Close()
|
|
||||||
if err5 != nil {
|
|
||||||
log.Printf("img close err:%v", err)
|
|
||||||
}
|
|
||||||
buf.Close()
|
|
||||||
t++
|
|
||||||
if t%50 == 0 {
|
|
||||||
log.Printf("device:%d send frame count:%d,img and buf had been closed", deviceId, c)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
} else {
|
} else {
|
||||||
//每秒发送一次心跳检测
|
//每秒发送一次心跳检测
|
||||||
if t_count%10 == 0 {
|
if tCount%10 == 0 {
|
||||||
err := ws.WriteControl(websocket.PingMessage, []byte{}, time.Now().Add(time.Second))
|
err := ws.WriteControl(websocket.PingMessage, []byte{}, time.Now().Add(time.Second))
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Printf("Connection check failed:%v", err)
|
log.Printf("Connection check failed:%v", err)
|
||||||
|
|
@ -270,7 +241,7 @@ func subscribeAndHandleMessagesV3(ws *websocket.Conn, deviceId int) {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
time.Sleep(100 * time.Millisecond)
|
time.Sleep(100 * time.Millisecond)
|
||||||
t_count++
|
tCount++
|
||||||
}
|
}
|
||||||
|
|
||||||
// 查看是否还有其他连接,没有则设置 is_play 为 0
|
// 查看是否还有其他连接,没有则设置 is_play 为 0
|
||||||
|
|
|
||||||
|
|
@ -93,6 +93,52 @@ func SetDeviceCurrentFrameV2(frame *gocv.Mat, deviceId int) error {
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func GetDeviceCurrentFrameV4(deviceId int, curCount int) (buf []byte, cnt int) {
|
||||||
|
defer func() {
|
||||||
|
if err := recover(); err != nil {
|
||||||
|
log.Printf("设备:%d 错误: %v\n", deviceId, err)
|
||||||
|
}
|
||||||
|
}()
|
||||||
|
//获取读写锁
|
||||||
|
mutex_, ok := DeviceRWMap.Load(deviceId)
|
||||||
|
if !ok {
|
||||||
|
log.Printf("DeviceRWMap 读写锁不存在,device_id: %d \n", deviceId)
|
||||||
|
return nil, -1
|
||||||
|
}
|
||||||
|
mutex, ok := mutex_.(*sync.RWMutex)
|
||||||
|
if !ok {
|
||||||
|
log.Printf("DeviceRWMap 存储的不是 *sync.RWMutex 类型,device_id: %d \n", deviceId)
|
||||||
|
return nil, -1
|
||||||
|
}
|
||||||
|
mutex.RLock()
|
||||||
|
defer mutex.RUnlock()
|
||||||
|
frameCount, ok := DeviceFrameCount[deviceId]
|
||||||
|
if !ok {
|
||||||
|
return buf, -1
|
||||||
|
}
|
||||||
|
if frameCount == curCount {
|
||||||
|
return nil, frameCount
|
||||||
|
}
|
||||||
|
//获取当前帧
|
||||||
|
switch deviceId {
|
||||||
|
case 1:
|
||||||
|
if Device1CurrentFrame.Empty() {
|
||||||
|
return nil, -1
|
||||||
|
}
|
||||||
|
buf = Device1CurrentFrame.ToBytes()
|
||||||
|
case 50:
|
||||||
|
if Device50CurrentFrame.Empty() {
|
||||||
|
return nil, -1
|
||||||
|
}
|
||||||
|
buf = Device50CurrentFrame.ToBytes()
|
||||||
|
case 73:
|
||||||
|
if Device73CurrentFrame.Empty() {
|
||||||
|
return nil, -1
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return buf, frameCount
|
||||||
|
}
|
||||||
|
|
||||||
func GetDeviceCurrentFrameV2(frame *gocv.Mat, deviceId int) int {
|
func GetDeviceCurrentFrameV2(frame *gocv.Mat, deviceId int) int {
|
||||||
defer func() {
|
defer func() {
|
||||||
if err := recover(); err != nil {
|
if err := recover(); err != nil {
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue