解决拷贝问题

This commit is contained in:
lijun 2025-01-18 16:51:54 +08:00
parent 69f81addeb
commit bfc7659ec0
3 changed files with 44 additions and 38 deletions

View File

@ -99,14 +99,25 @@ 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
var frame gocv.Mat
defer func(frame *gocv.Mat) {
err3 := frame.Close()
if err3 != nil {
log.Printf("流,设备:%d 错误: 无法关闭当前帧,err:%s \n", req.ID, err3.Error())
}
}(&frame)
for { for {
frame, cnt := service.GetDeviceCurrentFrame(req.ID) cnt := service.GetDeviceCurrentFrameV2(&frame, req.ID)
if cnt == count { if cnt == count {
time.Sleep(50 * time.Millisecond) time.Sleep(50 * time.Millisecond)
continue continue
} }
//gocv.Matrix转为jpeg //gocv.Matrix转为jpeg
img, err := gocv.IMEncode(".jpg", frame) img, err2 := gocv.IMEncode(".jpg", frame)
if err2 != nil {
log.Printf("img encode err:%v", err2)
return false
}
frame_ := img.GetBytes() frame_ := img.GetBytes()
_, err = w.Write([]byte("--frame\r\nContent-Type: image/jpeg\r\n\r\n")) _, err = w.Write([]byte("--frame\r\nContent-Type: image/jpeg\r\n\r\n"))
@ -162,9 +173,10 @@ func subscribeAndHandleMessagesV3(ws *websocket.Conn, device_id int) {
count := 0 count := 0
//定时器,发送计数器 //定时器,发送计数器
t_count := 0 t_count := 0
for {
//从service获取当前帧 //从service获取当前帧
img, c := service.GetDeviceCurrentFrameV3(device_id) var img gocv.Mat
for {
c := service.GetDeviceCurrentFrameV2(&img, device_id)
if c != count { if c != count {
//将img转[]byte //将img转[]byte
if img.Empty() { if img.Empty() {
@ -199,15 +211,17 @@ func subscribeAndHandleMessagesV3(ws *websocket.Conn, device_id int) {
log.Printf("Connection check failed:%v", err) log.Printf("Connection check failed:%v", err)
worker.SetRedisSetRemove(online_conn_key, con_id) worker.SetRedisSetRemove(online_conn_key, con_id)
break break
} else {
log.Printf("Connection check success")
} }
} }
} }
time.Sleep(100 * time.Millisecond) time.Sleep(100 * time.Millisecond)
t_count++ t_count++
} }
//关闭img
err := img.Close()
if err != nil {
log.Printf("close img err:%v", err)
}
// 查看是否还有其他连接,没有则设置 is_play 为 0 // 查看是否还有其他连接,没有则设置 is_play 为 0
if worker.IsContainKey(online_conn_key) == false { if worker.IsContainKey(online_conn_key) == false {
worker.SetRedisWithExpire(strconv.Itoa(device_id)+"_is_play", "1", time.Minute*5) worker.SetRedisWithExpire(strconv.Itoa(device_id)+"_is_play", "1", time.Minute*5)

View File

@ -99,7 +99,7 @@ func ReadConfigAndSetSystem() {
if !ok { if !ok {
//说明没有这个设备,需初始化添加 //说明没有这个设备,需初始化添加
service.DeviceRWMap.Store(device.ID, &sync.RWMutex{}) service.DeviceRWMap.Store(device.ID, &sync.RWMutex{})
service.DeviceCurrentFrameMap.Store(device.ID, gocv.NewMat()) service.DeviceCurrentFrameMap = map[int]gocv.Mat{}
service.DeviceFrameCount.Store(device.ID, 0) service.DeviceFrameCount.Store(device.ID, 0)
service.DeviceIsGettingFrame.Store(device.ID, false) service.DeviceIsGettingFrame.Store(device.ID, false)
} }

View File

@ -15,7 +15,7 @@ import (
) )
var DeviceRWMap = &sync.Map{} var DeviceRWMap = &sync.Map{}
var DeviceCurrentFrameMap = &sync.Map{} var DeviceCurrentFrameMap map[int]gocv.Mat
var DeviceFrameCount = &sync.Map{} var DeviceFrameCount = &sync.Map{}
var DeviceIsGettingFrame = &sync.Map{} var DeviceIsGettingFrame = &sync.Map{}
var Device1CurrentFrame gocv.Mat var Device1CurrentFrame gocv.Mat
@ -30,19 +30,17 @@ func SetDeviceCurrentFrame(frame gocv.Mat, device_id int) error {
mutex := mutex_.(*sync.RWMutex) mutex := mutex_.(*sync.RWMutex)
mutex.Lock() mutex.Lock()
defer mutex.Unlock() defer mutex.Unlock()
//获取前一帧,将前一帧释放
framePrev, ok := DeviceCurrentFrameMap.Load(device_id) mat := DeviceCurrentFrameMap[device_id]
if ok { if mat.Empty() {
frame_, ok2 := framePrev.(gocv.Mat) DeviceCurrentFrameMap[device_id] = gocv.NewMatWithSize(frame.Rows(), frame.Cols(), frame.Type())
if ok2 {
err2 := frame_.Close()
if err2 != nil {
log.Printf("设备:%d, 错误: 无法关闭帧\n", device_id)
} }
frame.CopyTo(&mat)
DeviceCurrentFrameMap[device_id] = mat
err := mat.Close()
if err != nil {
return err
} }
}
//设置当前帧
DeviceCurrentFrameMap.Store(device_id, frame)
frame_count, ok := DeviceFrameCount.Load(device_id) frame_count, ok := DeviceFrameCount.Load(device_id)
if !ok { if !ok {
return fmt.Errorf("设备:%s 当前帧计数不存在", device_id) return fmt.Errorf("设备:%s 当前帧计数不存在", device_id)
@ -115,17 +113,18 @@ 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) mat := DeviceCurrentFrameMap[deviceId]
if !ok { if (*frame).Empty() {
return -1 *frame = gocv.NewMatWithSize(mat.Rows(), mat.Cols(), mat.Type())
} }
frame_, ok = frameIface.(gocv.Mat) mat.CopyTo(frame)
if !ok { //关闭
log.Printf("DeviceCurrentFrameMap 存储的不是 gocv.Mat 类型device_id: %d \n", deviceId) err := mat.Close()
if err != nil {
log.Printf("设备:%d 错误: %v\n", deviceId, err)
} }
*frame = frame_
frame_countIface, ok := DeviceFrameCount.Load(deviceId) frame_countIface, ok := DeviceFrameCount.Load(deviceId)
if !ok { if !ok {
return -1 return -1
@ -207,14 +206,7 @@ func GetDeviceCurrentFrame(deviceId int) (gocv.Mat, int) {
mutex.RLock() mutex.RLock()
defer mutex.RUnlock() defer mutex.RUnlock()
//获取当前帧 //获取当前帧
frameIface, ok := DeviceCurrentFrameMap.Load(deviceId) mat := DeviceCurrentFrameMap[deviceId]
if !ok {
return gocv.NewMat(), -1
}
frame, ok := frameIface.(gocv.Mat)
if !ok {
log.Printf("DeviceCurrentFrameMap 存储的不是 gocv.Mat 类型device_id: %d \n", deviceId)
}
frame_countIface, ok := DeviceFrameCount.Load(deviceId) frame_countIface, ok := DeviceFrameCount.Load(deviceId)
if !ok { if !ok {
return gocv.NewMat(), -1 return gocv.NewMat(), -1
@ -223,7 +215,7 @@ func GetDeviceCurrentFrame(deviceId int) (gocv.Mat, int) {
if !ok { if !ok {
log.Printf("DeviceFrameCount 存储的不是 int 类型device_id: %d", deviceId) log.Printf("DeviceFrameCount 存储的不是 int 类型device_id: %d", deviceId)
} }
return frame, frame_count return mat, frame_count
} }
func getVideoFrame(device proto.DeviceInfo) { func getVideoFrame(device proto.DeviceInfo) {