2025-01-11 22:34:36 +08:00
|
|
|
|
package service
|
2025-01-12 15:18:58 +08:00
|
|
|
|
|
|
|
|
|
|
import (
|
2025-01-14 15:00:40 +08:00
|
|
|
|
"VideoStream/dao"
|
2025-01-12 15:18:58 +08:00
|
|
|
|
"VideoStream/proto"
|
2025-01-14 15:47:28 +08:00
|
|
|
|
"VideoStream/worker"
|
2025-01-12 15:18:58 +08:00
|
|
|
|
"fmt"
|
|
|
|
|
|
"gocv.io/x/gocv"
|
|
|
|
|
|
"image"
|
|
|
|
|
|
"image/color"
|
|
|
|
|
|
"log"
|
|
|
|
|
|
"net/http"
|
|
|
|
|
|
"sync"
|
|
|
|
|
|
"time"
|
|
|
|
|
|
)
|
|
|
|
|
|
|
2025-01-15 00:25:10 +08:00
|
|
|
|
var DeviceRWMap = &sync.Map{}
|
|
|
|
|
|
var DeviceCurrentFrameMap = &sync.Map{}
|
2025-01-18 19:59:32 +08:00
|
|
|
|
var DeviceFrameCount map[int]int
|
2025-01-15 00:25:10 +08:00
|
|
|
|
var DeviceIsGettingFrame = &sync.Map{}
|
2025-01-18 14:21:04 +08:00
|
|
|
|
var Device1CurrentFrame gocv.Mat
|
|
|
|
|
|
var Device50CurrentFrame gocv.Mat
|
2025-01-18 19:59:32 +08:00
|
|
|
|
var Device73CurrentFrame gocv.Mat
|
2025-01-12 15:18:58 +08:00
|
|
|
|
|
2025-01-18 19:59:32 +08:00
|
|
|
|
func SetDeviceCurrentFrame(frame gocv.Mat, deviceId int) error {
|
2025-01-12 15:18:58 +08:00
|
|
|
|
//获取读写锁
|
2025-01-18 19:59:32 +08:00
|
|
|
|
mutex_, ok := DeviceRWMap.Load(deviceId)
|
2025-01-12 15:18:58 +08:00
|
|
|
|
if !ok {
|
2025-01-18 19:59:32 +08:00
|
|
|
|
return fmt.Errorf("设备:%s 读写锁不存在", deviceId)
|
2025-01-12 15:18:58 +08:00
|
|
|
|
}
|
2025-01-15 00:25:10 +08:00
|
|
|
|
mutex := mutex_.(*sync.RWMutex)
|
2025-01-12 15:18:58 +08:00
|
|
|
|
mutex.Lock()
|
|
|
|
|
|
defer mutex.Unlock()
|
2025-01-16 13:58:23 +08:00
|
|
|
|
//获取前一帧,将前一帧释放
|
2025-01-18 19:59:32 +08:00
|
|
|
|
framePrev, ok := DeviceCurrentFrameMap.Load(deviceId)
|
2025-01-16 13:58:23 +08:00
|
|
|
|
if ok {
|
|
|
|
|
|
frame_, ok2 := framePrev.(gocv.Mat)
|
|
|
|
|
|
if ok2 {
|
|
|
|
|
|
err2 := frame_.Close()
|
|
|
|
|
|
if err2 != nil {
|
2025-01-18 19:59:32 +08:00
|
|
|
|
log.Printf("设备:%d, 错误: 无法关闭帧\n", deviceId)
|
2025-01-16 13:58:23 +08:00
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
2025-01-12 15:18:58 +08:00
|
|
|
|
//设置当前帧
|
2025-01-18 19:59:32 +08:00
|
|
|
|
DeviceCurrentFrameMap.Store(deviceId, frame)
|
|
|
|
|
|
frameCount, ok := DeviceFrameCount[deviceId]
|
2025-01-15 00:25:10 +08:00
|
|
|
|
if !ok {
|
2025-01-18 19:59:32 +08:00
|
|
|
|
return fmt.Errorf("设备:%s 当前帧计数不存在", deviceId)
|
2025-01-15 00:25:10 +08:00
|
|
|
|
}
|
2025-01-18 19:59:32 +08:00
|
|
|
|
frameCount++
|
|
|
|
|
|
DeviceFrameCount[deviceId] = frameCount
|
2025-01-12 15:18:58 +08:00
|
|
|
|
return nil
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2025-01-18 19:59:32 +08:00
|
|
|
|
func SetDeviceCurrentFrameV2(frame *gocv.Mat, deviceId int) error {
|
2025-01-18 14:21:04 +08:00
|
|
|
|
//获取读写锁
|
2025-01-18 19:59:32 +08:00
|
|
|
|
mutex_, ok := DeviceRWMap.Load(deviceId)
|
2025-01-18 14:21:04 +08:00
|
|
|
|
if !ok {
|
2025-01-18 19:59:32 +08:00
|
|
|
|
return fmt.Errorf("设备:%s 读写锁不存在", deviceId)
|
2025-01-18 14:21:04 +08:00
|
|
|
|
}
|
|
|
|
|
|
mutex := mutex_.(*sync.RWMutex)
|
|
|
|
|
|
mutex.Lock()
|
|
|
|
|
|
defer mutex.Unlock()
|
|
|
|
|
|
//设置当前帧
|
2025-01-18 19:59:32 +08:00
|
|
|
|
switch deviceId {
|
2025-01-18 14:21:04 +08:00
|
|
|
|
case 1:
|
2025-01-18 16:01:42 +08:00
|
|
|
|
if Device1CurrentFrame.Empty() {
|
|
|
|
|
|
Device1CurrentFrame = gocv.NewMatWithSize((*frame).Rows(), (*frame).Cols(), (*frame).Type())
|
|
|
|
|
|
}
|
|
|
|
|
|
(*frame).CopyTo(&Device1CurrentFrame)
|
2025-01-18 14:21:04 +08:00
|
|
|
|
case 50:
|
2025-01-18 16:08:07 +08:00
|
|
|
|
if Device50CurrentFrame.Empty() {
|
|
|
|
|
|
Device50CurrentFrame = gocv.NewMatWithSize((*frame).Rows(), (*frame).Cols(), (*frame).Type())
|
2025-01-18 15:48:20 +08:00
|
|
|
|
}
|
2025-01-18 16:08:07 +08:00
|
|
|
|
(*frame).CopyTo(&Device50CurrentFrame)
|
2025-01-18 19:59:32 +08:00
|
|
|
|
case 73:
|
|
|
|
|
|
if Device73CurrentFrame.Empty() {
|
|
|
|
|
|
Device73CurrentFrame = gocv.NewMatWithSize((*frame).Rows(), (*frame).Cols(), (*frame).Type())
|
|
|
|
|
|
}
|
|
|
|
|
|
(*frame).CopyTo(&Device73CurrentFrame)
|
|
|
|
|
|
|
2025-01-18 14:21:04 +08:00
|
|
|
|
}
|
2025-01-18 19:59:32 +08:00
|
|
|
|
frameCount, ok := DeviceFrameCount[deviceId]
|
2025-01-18 14:21:04 +08:00
|
|
|
|
if !ok {
|
2025-01-18 19:59:32 +08:00
|
|
|
|
return fmt.Errorf("设备:%s 当前帧计数不存在", deviceId)
|
2025-01-18 14:21:04 +08:00
|
|
|
|
}
|
2025-01-22 14:46:23 +08:00
|
|
|
|
if frameCount%10 == 0 {
|
|
|
|
|
|
log.Printf("设备:%d 当前帧: %d 已更新\n", deviceId, frameCount)
|
|
|
|
|
|
}
|
2025-01-18 19:59:32 +08:00
|
|
|
|
frameCount++
|
|
|
|
|
|
DeviceFrameCount[deviceId] = frameCount
|
2025-01-18 14:21:04 +08:00
|
|
|
|
return nil
|
|
|
|
|
|
}
|
2025-03-10 14:39:15 +08:00
|
|
|
|
func GetDeviceCurrentFrameV5(deviceId int, curCount int, buf *[]byte) (cnt int) {
|
|
|
|
|
|
defer func() {
|
|
|
|
|
|
if err := recover(); err != nil {
|
|
|
|
|
|
log.Printf("设备:%d 错误: %v\n", deviceId, err)
|
|
|
|
|
|
// 异常时释放 buf
|
|
|
|
|
|
buf = nil
|
|
|
|
|
|
}
|
|
|
|
|
|
}()
|
|
|
|
|
|
//获取读写锁
|
|
|
|
|
|
mutex_, ok := DeviceRWMap.Load(deviceId)
|
|
|
|
|
|
if !ok {
|
|
|
|
|
|
log.Printf("DeviceRWMap 读写锁不存在,device_id: %d \n", deviceId)
|
|
|
|
|
|
return -1
|
|
|
|
|
|
}
|
|
|
|
|
|
mutex, ok := mutex_.(*sync.RWMutex)
|
|
|
|
|
|
if !ok {
|
|
|
|
|
|
log.Printf("DeviceRWMap 存储的不是 *sync.RWMutex 类型,device_id: %d \n", deviceId)
|
|
|
|
|
|
return -1
|
|
|
|
|
|
}
|
|
|
|
|
|
mutex.RLock()
|
|
|
|
|
|
defer mutex.RUnlock()
|
|
|
|
|
|
frameCount, ok := DeviceFrameCount[deviceId]
|
|
|
|
|
|
if !ok {
|
|
|
|
|
|
return -1
|
|
|
|
|
|
}
|
|
|
|
|
|
if frameCount == curCount {
|
|
|
|
|
|
return frameCount
|
|
|
|
|
|
}
|
|
|
|
|
|
//获取当前帧
|
|
|
|
|
|
var tempBuf []byte
|
|
|
|
|
|
switch deviceId {
|
|
|
|
|
|
case 1:
|
|
|
|
|
|
if Device1CurrentFrame.Empty() {
|
|
|
|
|
|
return -1
|
|
|
|
|
|
}
|
|
|
|
|
|
tempBuf = Device1CurrentFrame.ToBytes()
|
|
|
|
|
|
case 50:
|
|
|
|
|
|
if Device50CurrentFrame.Empty() {
|
|
|
|
|
|
return -1
|
|
|
|
|
|
}
|
|
|
|
|
|
tempBuf = Device50CurrentFrame.ToBytes()
|
|
|
|
|
|
case 73:
|
|
|
|
|
|
if Device73CurrentFrame.Empty() {
|
|
|
|
|
|
return -1
|
|
|
|
|
|
}
|
|
|
|
|
|
tempBuf = Device73CurrentFrame.ToBytes()
|
|
|
|
|
|
default:
|
|
|
|
|
|
return -1
|
|
|
|
|
|
}
|
|
|
|
|
|
// 避免直接返回大数组,可考虑复制必要的数据
|
|
|
|
|
|
if len(tempBuf) > 0 {
|
|
|
|
|
|
copy(*buf, tempBuf)
|
|
|
|
|
|
// 释放临时变量
|
|
|
|
|
|
tempBuf = nil
|
|
|
|
|
|
}
|
|
|
|
|
|
return frameCount
|
|
|
|
|
|
}
|
2025-01-18 14:21:04 +08:00
|
|
|
|
|
2025-01-23 22:31:04 +08:00
|
|
|
|
func GetDeviceCurrentFrameV4(deviceId int, curCount int) (buf []byte, cnt int) {
|
|
|
|
|
|
defer func() {
|
|
|
|
|
|
if err := recover(); err != nil {
|
|
|
|
|
|
log.Printf("设备:%d 错误: %v\n", deviceId, err)
|
2025-01-23 22:40:07 +08:00
|
|
|
|
// 异常时释放 buf
|
|
|
|
|
|
buf = nil
|
2025-01-23 22:31:04 +08:00
|
|
|
|
}
|
|
|
|
|
|
}()
|
|
|
|
|
|
//获取读写锁
|
|
|
|
|
|
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
|
|
|
|
|
|
}
|
|
|
|
|
|
//获取当前帧
|
2025-01-23 22:40:07 +08:00
|
|
|
|
var tempBuf []byte
|
2025-01-23 22:31:04 +08:00
|
|
|
|
switch deviceId {
|
|
|
|
|
|
case 1:
|
|
|
|
|
|
if Device1CurrentFrame.Empty() {
|
|
|
|
|
|
return nil, -1
|
|
|
|
|
|
}
|
2025-01-23 22:40:07 +08:00
|
|
|
|
tempBuf = Device1CurrentFrame.ToBytes()
|
2025-01-23 22:31:04 +08:00
|
|
|
|
case 50:
|
|
|
|
|
|
if Device50CurrentFrame.Empty() {
|
|
|
|
|
|
return nil, -1
|
|
|
|
|
|
}
|
2025-01-23 22:40:07 +08:00
|
|
|
|
tempBuf = Device50CurrentFrame.ToBytes()
|
2025-01-23 22:31:04 +08:00
|
|
|
|
case 73:
|
|
|
|
|
|
if Device73CurrentFrame.Empty() {
|
|
|
|
|
|
return nil, -1
|
|
|
|
|
|
}
|
2025-01-23 22:40:07 +08:00
|
|
|
|
tempBuf = Device73CurrentFrame.ToBytes()
|
|
|
|
|
|
default:
|
|
|
|
|
|
return nil, -1
|
|
|
|
|
|
}
|
|
|
|
|
|
// 避免直接返回大数组,可考虑复制必要的数据
|
|
|
|
|
|
if len(tempBuf) > 0 {
|
|
|
|
|
|
buf = make([]byte, len(tempBuf))
|
|
|
|
|
|
copy(buf, tempBuf)
|
|
|
|
|
|
// 释放临时变量
|
|
|
|
|
|
tempBuf = nil
|
2025-01-23 22:31:04 +08:00
|
|
|
|
}
|
|
|
|
|
|
return buf, frameCount
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2025-01-16 14:06:52 +08:00
|
|
|
|
func GetDeviceCurrentFrameV2(frame *gocv.Mat, deviceId int) 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 -1
|
|
|
|
|
|
}
|
|
|
|
|
|
mutex, ok := mutex_.(*sync.RWMutex)
|
|
|
|
|
|
if !ok {
|
|
|
|
|
|
log.Printf("DeviceRWMap 存储的不是 *sync.RWMutex 类型,device_id: %d \n", deviceId)
|
|
|
|
|
|
return -1
|
|
|
|
|
|
}
|
|
|
|
|
|
mutex.RLock()
|
|
|
|
|
|
defer mutex.RUnlock()
|
|
|
|
|
|
//获取当前帧
|
2025-01-18 20:50:33 +08:00
|
|
|
|
switch deviceId {
|
|
|
|
|
|
case 1:
|
|
|
|
|
|
if (*frame).Empty() {
|
|
|
|
|
|
*frame = gocv.NewMatWithSize(Device1CurrentFrame.Rows(), Device1CurrentFrame.Cols(), Device1CurrentFrame.Type())
|
|
|
|
|
|
}
|
2025-01-19 15:13:51 +08:00
|
|
|
|
if Device1CurrentFrame.Empty() {
|
|
|
|
|
|
return -1
|
|
|
|
|
|
}
|
2025-01-18 20:50:33 +08:00
|
|
|
|
Device1CurrentFrame.CopyTo(frame)
|
|
|
|
|
|
case 50:
|
|
|
|
|
|
if (*frame).Empty() {
|
|
|
|
|
|
*frame = gocv.NewMatWithSize(Device50CurrentFrame.Rows(), Device50CurrentFrame.Cols(), Device50CurrentFrame.Type())
|
|
|
|
|
|
}
|
2025-01-19 15:13:51 +08:00
|
|
|
|
if Device50CurrentFrame.Empty() {
|
|
|
|
|
|
return -1
|
|
|
|
|
|
}
|
2025-01-18 20:50:33 +08:00
|
|
|
|
Device50CurrentFrame.CopyTo(frame)
|
|
|
|
|
|
case 73:
|
|
|
|
|
|
if (*frame).Empty() {
|
|
|
|
|
|
*frame = gocv.NewMatWithSize(Device73CurrentFrame.Rows(), Device73CurrentFrame.Cols(), Device73CurrentFrame.Type())
|
|
|
|
|
|
}
|
2025-01-19 15:13:51 +08:00
|
|
|
|
if Device73CurrentFrame.Empty() {
|
|
|
|
|
|
return -1
|
|
|
|
|
|
}
|
2025-01-18 20:50:33 +08:00
|
|
|
|
Device73CurrentFrame.CopyTo(frame)
|
|
|
|
|
|
|
2025-01-16 14:06:52 +08:00
|
|
|
|
}
|
2025-01-18 19:59:32 +08:00
|
|
|
|
frameCount, ok := DeviceFrameCount[deviceId]
|
2025-01-16 14:06:52 +08:00
|
|
|
|
if !ok {
|
|
|
|
|
|
return -1
|
|
|
|
|
|
}
|
2025-01-18 19:59:32 +08:00
|
|
|
|
return frameCount
|
2025-01-16 14:06:52 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
2025-01-18 14:21:04 +08:00
|
|
|
|
func GetDeviceCurrentFrameV3(deviceId int) (gocv.Mat, 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 gocv.NewMat(), -1
|
|
|
|
|
|
}
|
|
|
|
|
|
mutex, ok := mutex_.(*sync.RWMutex)
|
|
|
|
|
|
if !ok {
|
|
|
|
|
|
log.Printf("DeviceRWMap 存储的不是 *sync.RWMutex 类型,device_id: %d \n", deviceId)
|
|
|
|
|
|
return gocv.NewMat(), -1
|
|
|
|
|
|
}
|
|
|
|
|
|
mutex.RLock()
|
|
|
|
|
|
defer mutex.RUnlock()
|
|
|
|
|
|
//获取当前帧
|
|
|
|
|
|
var frame gocv.Mat
|
|
|
|
|
|
switch deviceId {
|
|
|
|
|
|
case 1:
|
2025-01-18 16:04:54 +08:00
|
|
|
|
frame = gocv.NewMatWithSize(Device1CurrentFrame.Rows(), Device1CurrentFrame.Cols(), Device1CurrentFrame.Type())
|
|
|
|
|
|
Device1CurrentFrame.CopyTo(&frame)
|
2025-01-18 15:41:15 +08:00
|
|
|
|
//查看帧状态
|
2025-01-18 16:08:07 +08:00
|
|
|
|
//log.Printf("frame:%v,Device1CurrentFrame:%v \n", frame.Empty(), Device1CurrentFrame.Empty())
|
2025-01-18 15:41:15 +08:00
|
|
|
|
|
2025-01-18 14:21:04 +08:00
|
|
|
|
case 50:
|
2025-01-18 16:04:54 +08:00
|
|
|
|
frame = gocv.NewMatWithSize(Device50CurrentFrame.Rows(), Device50CurrentFrame.Cols(), Device50CurrentFrame.Type())
|
|
|
|
|
|
Device50CurrentFrame.CopyTo(&frame)
|
2025-01-18 15:41:15 +08:00
|
|
|
|
//查看帧状态
|
2025-01-18 16:08:07 +08:00
|
|
|
|
//log.Printf("frame:%v,Device50CurrentFrame:%v\n", frame.Empty(), Device50CurrentFrame.Empty())
|
2025-01-18 20:04:50 +08:00
|
|
|
|
case 73:
|
|
|
|
|
|
frame = gocv.NewMatWithSize(Device73CurrentFrame.Rows(), Device73CurrentFrame.Cols(), Device73CurrentFrame.Type())
|
|
|
|
|
|
Device73CurrentFrame.CopyTo(&frame)
|
2025-01-18 14:21:04 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
2025-01-18 19:59:32 +08:00
|
|
|
|
frameCount, ok := DeviceFrameCount[deviceId]
|
2025-01-18 14:21:04 +08:00
|
|
|
|
if !ok {
|
|
|
|
|
|
return gocv.NewMat(), -1
|
|
|
|
|
|
}
|
2025-01-18 19:59:32 +08:00
|
|
|
|
|
2025-01-18 15:26:04 +08:00
|
|
|
|
//查看地址
|
2025-01-18 16:08:07 +08:00
|
|
|
|
//log.Printf("frame:%p,Device1CurrentFrame:%p,Device50CurrentFrame:%p\n", &frame, &Device1CurrentFrame, &Device50CurrentFrame)
|
2025-01-18 19:59:32 +08:00
|
|
|
|
return frame, frameCount
|
2025-01-18 14:21:04 +08:00
|
|
|
|
}
|
2025-01-15 23:47:37 +08:00
|
|
|
|
func GetDeviceCurrentFrame(deviceId int) (gocv.Mat, int) {
|
|
|
|
|
|
defer func() {
|
|
|
|
|
|
if err := recover(); err != nil {
|
|
|
|
|
|
log.Printf("设备:%d 错误: %v\n", deviceId, err)
|
|
|
|
|
|
}
|
|
|
|
|
|
}()
|
|
|
|
|
|
|
2025-01-12 15:18:58 +08:00
|
|
|
|
//获取读写锁
|
2025-01-15 23:47:37 +08:00
|
|
|
|
mutex_, ok := DeviceRWMap.Load(deviceId)
|
2025-01-12 15:18:58 +08:00
|
|
|
|
if !ok {
|
2025-01-15 23:54:43 +08:00
|
|
|
|
log.Printf("DeviceRWMap 读写锁不存在,device_id: %d \n", deviceId)
|
2025-01-12 15:18:58 +08:00
|
|
|
|
return gocv.NewMat(), -1
|
|
|
|
|
|
}
|
2025-01-15 23:20:44 +08:00
|
|
|
|
mutex, ok := mutex_.(*sync.RWMutex)
|
|
|
|
|
|
if !ok {
|
2025-01-15 23:54:43 +08:00
|
|
|
|
log.Printf("DeviceRWMap 存储的不是 *sync.RWMutex 类型,device_id: %d \n", deviceId)
|
2025-01-15 23:41:01 +08:00
|
|
|
|
return gocv.NewMat(), -1
|
2025-01-15 23:20:44 +08:00
|
|
|
|
}
|
2025-01-12 15:18:58 +08:00
|
|
|
|
mutex.RLock()
|
|
|
|
|
|
defer mutex.RUnlock()
|
|
|
|
|
|
//获取当前帧
|
2025-01-15 23:47:37 +08:00
|
|
|
|
frameIface, ok := DeviceCurrentFrameMap.Load(deviceId)
|
2025-01-15 23:20:44 +08:00
|
|
|
|
if !ok {
|
|
|
|
|
|
return gocv.NewMat(), -1
|
|
|
|
|
|
}
|
|
|
|
|
|
frame, ok := frameIface.(gocv.Mat)
|
|
|
|
|
|
if !ok {
|
2025-01-15 23:54:43 +08:00
|
|
|
|
log.Printf("DeviceCurrentFrameMap 存储的不是 gocv.Mat 类型,device_id: %d \n", deviceId)
|
2025-01-15 23:20:44 +08:00
|
|
|
|
}
|
2025-01-18 19:59:32 +08:00
|
|
|
|
frameCount, ok := DeviceFrameCount[deviceId]
|
2025-01-15 23:20:44 +08:00
|
|
|
|
if !ok {
|
|
|
|
|
|
return gocv.NewMat(), -1
|
|
|
|
|
|
}
|
2025-01-18 19:59:32 +08:00
|
|
|
|
return frame, frameCount
|
2025-01-12 15:18:58 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
func getVideoFrame(device proto.DeviceInfo) {
|
2025-01-15 23:31:50 +08:00
|
|
|
|
//捕获异常
|
|
|
|
|
|
defer func() {
|
|
|
|
|
|
if err := recover(); err != nil {
|
2025-01-15 23:41:01 +08:00
|
|
|
|
log.Printf("设备:%d 错误: %v\n", device.ID, err)
|
2025-01-15 23:31:50 +08:00
|
|
|
|
}
|
|
|
|
|
|
}()
|
|
|
|
|
|
|
2025-01-12 15:18:58 +08:00
|
|
|
|
webcam, err := gocv.OpenVideoCapture(device.Stream)
|
|
|
|
|
|
if err != nil {
|
2025-01-16 23:40:08 +08:00
|
|
|
|
log.Printf("设备:%d 错误: 无法打开视频流,err: %v\n", device.ID, err)
|
2025-01-12 15:18:58 +08:00
|
|
|
|
return
|
|
|
|
|
|
}
|
2025-01-15 23:31:50 +08:00
|
|
|
|
defer func(webcam *gocv.VideoCapture) {
|
|
|
|
|
|
err2 := webcam.Close()
|
|
|
|
|
|
if err2 != nil {
|
2025-01-15 23:41:01 +08:00
|
|
|
|
log.Printf("设备:%d 错误: 无法关闭视频流,%s \n", device.ID, err2.Error())
|
2025-01-15 23:31:50 +08:00
|
|
|
|
}
|
|
|
|
|
|
}(webcam)
|
2025-01-12 15:18:58 +08:00
|
|
|
|
// 字体相关设置,对应OpenCV默认字体等,这里简化处理,实际可按需求调整
|
|
|
|
|
|
font := gocv.FontHersheySimplex
|
|
|
|
|
|
fontScale := 0.5
|
|
|
|
|
|
fontColor := color.RGBA{G: 255}
|
|
|
|
|
|
lineType := 2
|
|
|
|
|
|
z := 0
|
2025-01-18 14:55:02 +08:00
|
|
|
|
var frame gocv.Mat
|
2025-01-12 15:18:58 +08:00
|
|
|
|
for {
|
|
|
|
|
|
if device.LogFrame > 0 && z%device.LogFrame == 0 {
|
2025-01-15 23:41:01 +08:00
|
|
|
|
log.Printf("设备:%d 当前帧: %d\n", device.ID, z)
|
2025-01-12 15:18:58 +08:00
|
|
|
|
}
|
|
|
|
|
|
if device.NextStop {
|
|
|
|
|
|
break
|
|
|
|
|
|
}
|
2025-01-18 14:55:02 +08:00
|
|
|
|
frame = gocv.NewMat()
|
2025-01-12 15:18:58 +08:00
|
|
|
|
ok := webcam.Read(&frame)
|
|
|
|
|
|
if !ok {
|
2025-01-16 23:40:08 +08:00
|
|
|
|
log.Printf("设备:%v 错误: 无法从视频流中读取帧\n", device)
|
2025-01-12 15:18:58 +08:00
|
|
|
|
break
|
|
|
|
|
|
}
|
|
|
|
|
|
if frame.Empty() {
|
2025-01-15 23:41:01 +08:00
|
|
|
|
log.Printf("设备:%s 错误: 无法从视频流中读取帧\n", device)
|
2025-01-12 15:18:58 +08:00
|
|
|
|
//等待50ms
|
|
|
|
|
|
time.Sleep(50 * time.Millisecond)
|
|
|
|
|
|
continue
|
|
|
|
|
|
}
|
|
|
|
|
|
height := frame.Rows()
|
|
|
|
|
|
width := frame.Cols()
|
|
|
|
|
|
if height < device.CheckFrameHeight || width < device.CheckFrameWidth {
|
2025-01-15 23:41:01 +08:00
|
|
|
|
log.Printf("设备:%s 帧尺寸已改变\n", device)
|
2025-01-12 15:18:58 +08:00
|
|
|
|
break
|
|
|
|
|
|
}
|
|
|
|
|
|
currentTime := time.Now().Format("2006-01-02 15:04:05")
|
|
|
|
|
|
gocv.PutText(&frame, currentTime, image.Point{10, 20}, font, fontScale, fontColor, lineType)
|
|
|
|
|
|
//需要将帧付给全局变量
|
2025-01-18 14:55:02 +08:00
|
|
|
|
err3 := SetDeviceCurrentFrameV2(&frame, device.ID)
|
2025-01-15 00:25:10 +08:00
|
|
|
|
if err3 != nil {
|
2025-01-15 23:41:01 +08:00
|
|
|
|
log.Printf("设备:%d 错误: 无法设置当前帧,err:%s \n", device.ID, err3.Error())
|
2025-01-12 15:18:58 +08:00
|
|
|
|
}
|
|
|
|
|
|
z++
|
2025-01-18 15:13:51 +08:00
|
|
|
|
//关闭帧
|
|
|
|
|
|
err4 := frame.Close()
|
|
|
|
|
|
if err4 != nil {
|
|
|
|
|
|
log.Printf("设备:%d 错误: 无法关闭帧,err:%s \n", device.ID, err4.Error())
|
|
|
|
|
|
}
|
2025-01-18 14:55:02 +08:00
|
|
|
|
}
|
|
|
|
|
|
//关闭帧
|
|
|
|
|
|
err4 := frame.Close()
|
|
|
|
|
|
if err4 != nil {
|
|
|
|
|
|
log.Printf("设备:%d 错误: 无法关闭帧,err:%s \n", device.ID, err4.Error())
|
2025-01-12 15:18:58 +08:00
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// 发起get请求,返回响应状态码
|
|
|
|
|
|
func Get(url string) int {
|
|
|
|
|
|
req, err := http.NewRequest("GET", url, nil)
|
|
|
|
|
|
var client = &http.Client{}
|
|
|
|
|
|
if err != nil {
|
|
|
|
|
|
return 500
|
|
|
|
|
|
}
|
|
|
|
|
|
resp, err := client.Do(req)
|
|
|
|
|
|
if err != nil {
|
|
|
|
|
|
return 500
|
|
|
|
|
|
}
|
|
|
|
|
|
return resp.StatusCode
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
func GetVideoStream(id int) {
|
2025-01-15 23:48:10 +08:00
|
|
|
|
defer func() {
|
|
|
|
|
|
if err := recover(); err != nil {
|
|
|
|
|
|
log.Printf("设备:%d 错误: %v\n", id, err)
|
|
|
|
|
|
}
|
|
|
|
|
|
}()
|
2025-01-18 20:21:38 +08:00
|
|
|
|
isGetting := worker.GetRedis(fmt.Sprintf("device_%d_is_getting", id))
|
|
|
|
|
|
if isGetting == "true" || isGetting == "" {
|
|
|
|
|
|
log.Printf("设备:%d 正在运行,isGetting:%s", id, isGetting)
|
2025-01-14 22:53:42 +08:00
|
|
|
|
return
|
|
|
|
|
|
}
|
2025-01-12 15:18:58 +08:00
|
|
|
|
for {
|
|
|
|
|
|
var device proto.DeviceInfo
|
|
|
|
|
|
var index int
|
|
|
|
|
|
//获取设备信息
|
|
|
|
|
|
for i, device1 := range proto.Config.DeviceInfo {
|
|
|
|
|
|
if device1.ID == id {
|
|
|
|
|
|
device = device1
|
|
|
|
|
|
break
|
|
|
|
|
|
}
|
|
|
|
|
|
index = i
|
|
|
|
|
|
}
|
|
|
|
|
|
if index == len(proto.Config.DeviceInfo) {
|
|
|
|
|
|
//设备不存在
|
2025-01-15 23:41:01 +08:00
|
|
|
|
log.Printf("device: %d not found", id)
|
2025-01-12 15:18:58 +08:00
|
|
|
|
break
|
|
|
|
|
|
}
|
2025-01-18 20:21:38 +08:00
|
|
|
|
isGetting = worker.GetRedis(fmt.Sprintf("device_%d_is_getting", id))
|
|
|
|
|
|
if isGetting == "true" {
|
|
|
|
|
|
log.Printf("设备:%d 正在运行,isGetting:%s", id, isGetting)
|
2025-01-14 23:29:52 +08:00
|
|
|
|
break
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2025-01-12 15:18:58 +08:00
|
|
|
|
if device.NextStop {
|
2025-01-15 00:25:10 +08:00
|
|
|
|
DeviceIsGettingFrame.Store(id, false)
|
2025-01-12 15:18:58 +08:00
|
|
|
|
break
|
|
|
|
|
|
}
|
|
|
|
|
|
//设置设备控制信息
|
|
|
|
|
|
status := Get(device.Control)
|
2025-01-15 00:25:10 +08:00
|
|
|
|
DeviceIsGettingFrame.Store(id, true)
|
2025-01-18 20:21:38 +08:00
|
|
|
|
worker.SetRedis(fmt.Sprintf("device_%d_is_getting", id), "true")
|
2025-01-15 23:41:01 +08:00
|
|
|
|
log.Printf("device: %d set control info status: %d", device.ID, status)
|
2025-01-12 15:18:58 +08:00
|
|
|
|
getVideoFrame(device)
|
2025-01-15 00:25:10 +08:00
|
|
|
|
DeviceIsGettingFrame.Store(id, false)
|
2025-01-18 20:21:38 +08:00
|
|
|
|
worker.SetRedis(fmt.Sprintf("device_%d_is_getting", id), "false")
|
2025-01-14 23:29:52 +08:00
|
|
|
|
//等待1s
|
|
|
|
|
|
time.Sleep(1 * time.Second)
|
2025-01-12 15:18:58 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
func DoGetVideoStream() {
|
|
|
|
|
|
for _, device := range proto.Config.DeviceInfo {
|
|
|
|
|
|
go GetVideoStream(device.ID)
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
2025-01-14 15:00:40 +08:00
|
|
|
|
|
|
|
|
|
|
func GetDevice(id, auth_id int) dao.Device {
|
|
|
|
|
|
return dao.FindDeviceByID(id, auth_id)
|
|
|
|
|
|
}
|
2025-01-14 15:47:28 +08:00
|
|
|
|
|
|
|
|
|
|
func SetConfigRedis(key, config string) {
|
|
|
|
|
|
worker.SetRedis(key, config)
|
|
|
|
|
|
}
|