解决拷贝问题
This commit is contained in:
parent
15a78b10c5
commit
1e739c4be2
|
|
@ -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,9 @@ func subscribeAndHandleMessagesV3(ws *websocket.Conn, device_id int) {
|
||||||
count := 0
|
count := 0
|
||||||
//定时器,发送计数器
|
//定时器,发送计数器
|
||||||
t_count := 0
|
t_count := 0
|
||||||
|
//从service获取当前帧
|
||||||
|
var img gocv.Mat
|
||||||
for {
|
for {
|
||||||
//从service获取当前帧
|
|
||||||
var img gocv.Mat
|
|
||||||
c := service.GetDeviceCurrentFrameV2(&img, device_id)
|
c := service.GetDeviceCurrentFrameV2(&img, device_id)
|
||||||
if c != count {
|
if c != count {
|
||||||
//将img转[]byte
|
//将img转[]byte
|
||||||
|
|
@ -176,7 +187,7 @@ func subscribeAndHandleMessagesV3(ws *websocket.Conn, device_id int) {
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Printf("img encode err:%v", err)
|
log.Printf("img encode err:%v", err)
|
||||||
worker.SetRedisSetRemove(online_conn_key, con_id)
|
worker.SetRedisSetRemove(online_conn_key, con_id)
|
||||||
goto end
|
break
|
||||||
}
|
}
|
||||||
buf1 := buf.GetBytes()
|
buf1 := buf.GetBytes()
|
||||||
|
|
||||||
|
|
@ -184,7 +195,7 @@ func subscribeAndHandleMessagesV3(ws *websocket.Conn, device_id int) {
|
||||||
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)
|
||||||
goto end
|
break
|
||||||
}
|
}
|
||||||
c = count
|
c = count
|
||||||
err4 := img.Close()
|
err4 := img.Close()
|
||||||
|
|
@ -199,15 +210,18 @@ func subscribeAndHandleMessagesV3(ws *websocket.Conn, device_id int) {
|
||||||
if err != nil {
|
if err != nil {
|
||||||
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)
|
||||||
goto end
|
break
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
time.Sleep(100 * time.Millisecond)
|
time.Sleep(100 * time.Millisecond)
|
||||||
t_count++
|
t_count++
|
||||||
}
|
}
|
||||||
|
//关闭img
|
||||||
end:
|
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)
|
||||||
|
|
|
||||||
2
main.go
2
main.go
|
|
@ -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)
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -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{}
|
||||||
|
|
||||||
|
|
@ -28,19 +28,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()
|
frame.CopyTo(&mat)
|
||||||
if err2 != nil {
|
DeviceCurrentFrameMap[device_id] = mat
|
||||||
log.Printf("设备:%d, 错误: 无法关闭帧\n", device_id)
|
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)
|
||||||
|
|
@ -70,17 +68,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
|
||||||
|
|
@ -113,14 +112,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
|
||||||
|
|
@ -129,7 +121,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) {
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue