解决拷贝问题
This commit is contained in:
parent
69f81addeb
commit
bfc7659ec0
|
|
@ -99,14 +99,25 @@ func GetVideoStream(c *gin.Context) {
|
|||
//设备流
|
||||
c.Stream(func(w io.Writer) bool {
|
||||
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 {
|
||||
frame, cnt := service.GetDeviceCurrentFrame(req.ID)
|
||||
cnt := service.GetDeviceCurrentFrameV2(&frame, req.ID)
|
||||
if cnt == count {
|
||||
time.Sleep(50 * time.Millisecond)
|
||||
continue
|
||||
}
|
||||
//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()
|
||||
|
||||
_, 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
|
||||
//定时器,发送计数器
|
||||
t_count := 0
|
||||
//从service获取当前帧
|
||||
var img gocv.Mat
|
||||
for {
|
||||
//从service获取当前帧
|
||||
img, c := service.GetDeviceCurrentFrameV3(device_id)
|
||||
c := service.GetDeviceCurrentFrameV2(&img, device_id)
|
||||
if c != count {
|
||||
//将img转[]byte
|
||||
if img.Empty() {
|
||||
|
|
@ -199,15 +211,17 @@ func subscribeAndHandleMessagesV3(ws *websocket.Conn, device_id int) {
|
|||
log.Printf("Connection check failed:%v", err)
|
||||
worker.SetRedisSetRemove(online_conn_key, con_id)
|
||||
break
|
||||
} else {
|
||||
log.Printf("Connection check success")
|
||||
}
|
||||
}
|
||||
}
|
||||
time.Sleep(100 * time.Millisecond)
|
||||
t_count++
|
||||
}
|
||||
|
||||
//关闭img
|
||||
err := img.Close()
|
||||
if err != nil {
|
||||
log.Printf("close img err:%v", err)
|
||||
}
|
||||
// 查看是否还有其他连接,没有则设置 is_play 为 0
|
||||
if worker.IsContainKey(online_conn_key) == false {
|
||||
worker.SetRedisWithExpire(strconv.Itoa(device_id)+"_is_play", "1", time.Minute*5)
|
||||
|
|
|
|||
2
main.go
2
main.go
|
|
@ -99,7 +99,7 @@ func ReadConfigAndSetSystem() {
|
|||
if !ok {
|
||||
//说明没有这个设备,需初始化添加
|
||||
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.DeviceIsGettingFrame.Store(device.ID, false)
|
||||
}
|
||||
|
|
|
|||
|
|
@ -15,7 +15,7 @@ import (
|
|||
)
|
||||
|
||||
var DeviceRWMap = &sync.Map{}
|
||||
var DeviceCurrentFrameMap = &sync.Map{}
|
||||
var DeviceCurrentFrameMap map[int]gocv.Mat
|
||||
var DeviceFrameCount = &sync.Map{}
|
||||
var DeviceIsGettingFrame = &sync.Map{}
|
||||
var Device1CurrentFrame gocv.Mat
|
||||
|
|
@ -30,19 +30,17 @@ func SetDeviceCurrentFrame(frame gocv.Mat, device_id int) error {
|
|||
mutex := mutex_.(*sync.RWMutex)
|
||||
mutex.Lock()
|
||||
defer mutex.Unlock()
|
||||
//获取前一帧,将前一帧释放
|
||||
framePrev, ok := DeviceCurrentFrameMap.Load(device_id)
|
||||
if ok {
|
||||
frame_, ok2 := framePrev.(gocv.Mat)
|
||||
if ok2 {
|
||||
err2 := frame_.Close()
|
||||
if err2 != nil {
|
||||
log.Printf("设备:%d, 错误: 无法关闭帧\n", device_id)
|
||||
}
|
||||
}
|
||||
|
||||
mat := DeviceCurrentFrameMap[device_id]
|
||||
if mat.Empty() {
|
||||
DeviceCurrentFrameMap[device_id] = gocv.NewMatWithSize(frame.Rows(), frame.Cols(), frame.Type())
|
||||
}
|
||||
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)
|
||||
if !ok {
|
||||
return fmt.Errorf("设备:%s 当前帧计数不存在", device_id)
|
||||
|
|
@ -115,17 +113,18 @@ func GetDeviceCurrentFrameV2(frame *gocv.Mat, deviceId int) int {
|
|||
}
|
||||
mutex.RLock()
|
||||
defer mutex.RUnlock()
|
||||
var frame_ gocv.Mat
|
||||
//获取当前帧
|
||||
frameIface, ok := DeviceCurrentFrameMap.Load(deviceId)
|
||||
if !ok {
|
||||
return -1
|
||||
mat := DeviceCurrentFrameMap[deviceId]
|
||||
if (*frame).Empty() {
|
||||
*frame = gocv.NewMatWithSize(mat.Rows(), mat.Cols(), mat.Type())
|
||||
}
|
||||
frame_, ok = frameIface.(gocv.Mat)
|
||||
if !ok {
|
||||
log.Printf("DeviceCurrentFrameMap 存储的不是 gocv.Mat 类型,device_id: %d \n", deviceId)
|
||||
mat.CopyTo(frame)
|
||||
//关闭
|
||||
err := mat.Close()
|
||||
if err != nil {
|
||||
log.Printf("设备:%d 错误: %v\n", deviceId, err)
|
||||
}
|
||||
*frame = frame_
|
||||
|
||||
frame_countIface, ok := DeviceFrameCount.Load(deviceId)
|
||||
if !ok {
|
||||
return -1
|
||||
|
|
@ -207,14 +206,7 @@ func GetDeviceCurrentFrame(deviceId int) (gocv.Mat, int) {
|
|||
mutex.RLock()
|
||||
defer mutex.RUnlock()
|
||||
//获取当前帧
|
||||
frameIface, ok := DeviceCurrentFrameMap.Load(deviceId)
|
||||
if !ok {
|
||||
return gocv.NewMat(), -1
|
||||
}
|
||||
frame, ok := frameIface.(gocv.Mat)
|
||||
if !ok {
|
||||
log.Printf("DeviceCurrentFrameMap 存储的不是 gocv.Mat 类型,device_id: %d \n", deviceId)
|
||||
}
|
||||
mat := DeviceCurrentFrameMap[deviceId]
|
||||
frame_countIface, ok := DeviceFrameCount.Load(deviceId)
|
||||
if !ok {
|
||||
return gocv.NewMat(), -1
|
||||
|
|
@ -223,7 +215,7 @@ func GetDeviceCurrentFrame(deviceId int) (gocv.Mat, int) {
|
|||
if !ok {
|
||||
log.Printf("DeviceFrameCount 存储的不是 int 类型,device_id: %d", deviceId)
|
||||
}
|
||||
return frame, frame_count
|
||||
return mat, frame_count
|
||||
}
|
||||
|
||||
func getVideoFrame(device proto.DeviceInfo) {
|
||||
|
|
|
|||
Loading…
Reference in New Issue