Compare commits
31 Commits
| Author | SHA1 | Date |
|---|---|---|
|
|
17b9bfc6f5 | |
|
|
df0985bc28 | |
|
|
0cc6792be0 | |
|
|
795cc35396 | |
|
|
b27a7fb7cc | |
|
|
8ceea2bb55 | |
|
|
fd4e0c0495 | |
|
|
539ea9215b | |
|
|
8c382bbbff | |
|
|
6ab124b0cb | |
|
|
ea963f4076 | |
|
|
6bdaeaf017 | |
|
|
2040232735 | |
|
|
7c1d9559b9 | |
|
|
5f2b5b55f4 | |
|
|
2a120d85f4 | |
|
|
c6bc92c4de | |
|
|
9362523c39 | |
|
|
fdc3407b6d | |
|
|
02a7738c49 | |
|
|
1110ca1b9c | |
|
|
7e7cc07c4d | |
|
|
bf6546069b | |
|
|
c0219ce83c | |
|
|
4d7521e4b5 | |
|
|
f1401e8a9b | |
|
|
6486b8328b | |
|
|
07cb9515be | |
|
|
e969fc53a2 | |
|
|
90e64920c7 | |
|
|
b155ce2c1d |
2
go.mod
2
go.mod
|
|
@ -12,7 +12,7 @@ require (
|
||||||
github.com/gorilla/websocket v1.5.3
|
github.com/gorilla/websocket v1.5.3
|
||||||
github.com/robfig/cron/v3 v3.0.1
|
github.com/robfig/cron/v3 v3.0.1
|
||||||
github.com/ulule/deepcopier v0.0.0-20200430083143-45decc6639b6
|
github.com/ulule/deepcopier v0.0.0-20200430083143-45decc6639b6
|
||||||
gocv.io/x/gocv v0.39.0
|
gocv.io/x/gocv v0.40.0
|
||||||
gorm.io/driver/mysql v1.5.7
|
gorm.io/driver/mysql v1.5.7
|
||||||
gorm.io/driver/postgres v1.5.11
|
gorm.io/driver/postgres v1.5.11
|
||||||
gorm.io/gorm v1.25.12
|
gorm.io/gorm v1.25.12
|
||||||
|
|
|
||||||
2
go.sum
2
go.sum
|
|
@ -110,6 +110,8 @@ github.com/ulule/deepcopier v0.0.0-20200430083143-45decc6639b6 h1:TtyC78WMafNW8Q
|
||||||
github.com/ulule/deepcopier v0.0.0-20200430083143-45decc6639b6/go.mod h1:h8272+G2omSmi30fBXiZDMkmHuOgonplfKIKjQWzlfs=
|
github.com/ulule/deepcopier v0.0.0-20200430083143-45decc6639b6/go.mod h1:h8272+G2omSmi30fBXiZDMkmHuOgonplfKIKjQWzlfs=
|
||||||
gocv.io/x/gocv v0.39.0 h1:vWHupDE22LebZW6id2mVeT767j1YS8WqGt+ZiV7XJXE=
|
gocv.io/x/gocv v0.39.0 h1:vWHupDE22LebZW6id2mVeT767j1YS8WqGt+ZiV7XJXE=
|
||||||
gocv.io/x/gocv v0.39.0/go.mod h1:zYdWMj29WAEznM3Y8NsU3A0TRq/wR/cy75jeUypThqU=
|
gocv.io/x/gocv v0.39.0/go.mod h1:zYdWMj29WAEznM3Y8NsU3A0TRq/wR/cy75jeUypThqU=
|
||||||
|
gocv.io/x/gocv v0.40.0 h1:kGBu/UVj+dO6A9dhQmGOnCICSL7ke7b5YtX3R3azdXI=
|
||||||
|
gocv.io/x/gocv v0.40.0/go.mod h1:zYdWMj29WAEznM3Y8NsU3A0TRq/wR/cy75jeUypThqU=
|
||||||
golang.org/x/arch v0.0.0-20210923205945-b76863e36670/go.mod h1:5om86z9Hs0C8fWVUuoMHwpExlXzs5Tkyp9hOrfG7pp8=
|
golang.org/x/arch v0.0.0-20210923205945-b76863e36670/go.mod h1:5om86z9Hs0C8fWVUuoMHwpExlXzs5Tkyp9hOrfG7pp8=
|
||||||
golang.org/x/arch v0.8.0 h1:3wRIsP3pM4yUptoR96otTUOXI367OS0+c9eeRi9doIc=
|
golang.org/x/arch v0.8.0 h1:3wRIsP3pM4yUptoR96otTUOXI367OS0+c9eeRi9doIc=
|
||||||
golang.org/x/arch v0.8.0/go.mod h1:FEVrYAQjsQXMVJ1nsMoVVXPZg6p2JE2mx8psSWTDQys=
|
golang.org/x/arch v0.8.0/go.mod h1:FEVrYAQjsQXMVJ1nsMoVVXPZg6p2JE2mx8psSWTDQys=
|
||||||
|
|
|
||||||
|
|
@ -8,7 +8,6 @@ import (
|
||||||
"github.com/gin-gonic/gin"
|
"github.com/gin-gonic/gin"
|
||||||
"github.com/google/uuid"
|
"github.com/google/uuid"
|
||||||
"github.com/gorilla/websocket"
|
"github.com/gorilla/websocket"
|
||||||
"gocv.io/x/gocv"
|
|
||||||
"io"
|
"io"
|
||||||
"log"
|
"log"
|
||||||
"net/http"
|
"net/http"
|
||||||
|
|
@ -113,57 +112,41 @@ 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
|
||||||
frame := gocv.NewMat()
|
|
||||||
defer func() {
|
|
||||||
//关闭帧
|
|
||||||
err4 := frame.Close()
|
|
||||||
if err4 != nil {
|
|
||||||
log.Printf("device:%d frame close err:%v", deviceID, err4)
|
|
||||||
}
|
|
||||||
}()
|
|
||||||
errCount := 0
|
errCount := 0
|
||||||
for {
|
for {
|
||||||
if errCount > 10 {
|
if errCount > 10 {
|
||||||
log.Printf("stream device:%d errCount > 10", deviceID)
|
log.Printf("stream device:%d errCount > 10", deviceID)
|
||||||
return false
|
return false
|
||||||
}
|
}
|
||||||
cnt := service.GetDeviceCurrentFrameV2(&frame, deviceID)
|
buf := make([]byte, 1024*1024)
|
||||||
|
cnt := -1
|
||||||
if cnt == count || cnt == -1 {
|
if cnt == count || cnt == -1 {
|
||||||
time.Sleep(50 * time.Millisecond)
|
time.Sleep(50 * time.Millisecond)
|
||||||
log.Printf("stream device:%d ,cnt =%d,count=%d,errCount=%d", deviceID, cnt, count, errCount)
|
log.Printf("stream device:%d,cnt =%d,count=%d,errCount=%d", deviceID, cnt, count, errCount)
|
||||||
errCount++
|
errCount++
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
if frame.Empty() {
|
count = cnt
|
||||||
log.Printf("stream device:%d frame is empty", deviceID)
|
|
||||||
time.Sleep(100 * time.Millisecond)
|
|
||||||
errCount++
|
|
||||||
continue
|
|
||||||
}
|
|
||||||
//gocv.Matrix转为jpeg
|
|
||||||
img, err2 := gocv.IMEncode(".jpg", frame)
|
|
||||||
if err2 != nil {
|
|
||||||
log.Printf("stream img encode err:%v", err2)
|
|
||||||
return false
|
|
||||||
}
|
|
||||||
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"))
|
||||||
if err != nil {
|
if err != nil {
|
||||||
fmt.Printf("写入头部信息错误: %v\n", err)
|
fmt.Printf("写入头部信息错误: %v\n", err)
|
||||||
|
buf = nil
|
||||||
return false
|
return false
|
||||||
}
|
}
|
||||||
_, err = w.Write(frame_)
|
_, err = w.Write(buf)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
fmt.Printf("写入帧数据错误: %v\n", err)
|
fmt.Printf("写入帧数据错误: %v\n", err)
|
||||||
|
buf = nil
|
||||||
return false
|
return false
|
||||||
}
|
}
|
||||||
_, err = w.Write([]byte("\r\n"))
|
_, err = w.Write([]byte("\r\n"))
|
||||||
if err != nil {
|
if err != nil {
|
||||||
fmt.Printf("写入帧结束标记错误: %v\n", err)
|
fmt.Printf("写入帧结束标记错误: %v\n", err)
|
||||||
|
buf = nil
|
||||||
return false
|
return false
|
||||||
}
|
}
|
||||||
time.Sleep(50 * time.Millisecond) // 控制帧率,模拟每秒约20帧,可按实际调整
|
time.Sleep(50 * time.Millisecond) // 控制帧率,模拟每秒约20帧,可按实际调整
|
||||||
|
buf = nil
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
@ -193,60 +176,42 @@ func GetRealTimeImage(c *gin.Context) {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
worker.SetRedisWithExpire(strconv.Itoa(int(device.ID))+"_is_play", "1", time.Minute*5)
|
worker.SetRedisWithExpire(strconv.Itoa(int(device.ID))+"_is_play", "1", time.Minute*5)
|
||||||
log.Printf("device_id:%d has set is_play to 1", deviceIdInt)
|
|
||||||
isGetting = worker.GetRedis(fmt.Sprintf("device_%d_is_getting", device.ID))
|
isGetting = worker.GetRedis(fmt.Sprintf("device_%d_is_getting", device.ID))
|
||||||
if isGetting != "true" {
|
if isGetting != "true" {
|
||||||
c.JSON(http.StatusOK, gin.H{"code": 5, "message": "device is not getting or not exist"})
|
c.JSON(http.StatusOK, gin.H{"code": 5, "message": "device is not getting or not exist"})
|
||||||
log.Printf("device_id:%d is not getting or not exist", deviceIdInt)
|
log.Printf("device_id:%d is not getting or not exist", deviceIdInt)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
go subscribeAndHandleMessagesV3(ws, deviceIdInt)
|
subscribeAndHandleMessagesV6(ws, deviceIdInt)
|
||||||
}
|
}
|
||||||
|
|
||||||
func subscribeAndHandleMessagesV3(ws *websocket.Conn, device_id int) {
|
func subscribeAndHandleMessagesV6(ws *websocket.Conn, deviceId int) {
|
||||||
// 生成唯一连接 uuid
|
// 生成唯一连接 uuid
|
||||||
con_id := uuid.New().String()
|
con_id := uuid.New().String()
|
||||||
online_conn_key := "device_" + strconv.Itoa(device_id) + "_online_conn_ids"
|
online_conn_key := "device_" + strconv.Itoa(deviceId) + "_online_conn_ids"
|
||||||
// 加入设备在线连接集合
|
// 加入设备在线连接集合
|
||||||
worker.SetRedisSetAddWithExpire(online_conn_key, con_id, time.Minute*5)
|
worker.SetRedisSetAddWithExpire(online_conn_key, con_id, time.Minute*5)
|
||||||
//图片计数器
|
//图片计数器
|
||||||
count := 0
|
count := 0
|
||||||
//定时器,发送计数器
|
//定时器,发送计数器
|
||||||
t_count := 0
|
tCount := 0
|
||||||
img := gocv.NewMat()
|
//计算帧率
|
||||||
for {
|
for {
|
||||||
//从service获取当前帧
|
//从service获取当前帧
|
||||||
c := service.GetDeviceCurrentFrameV2(&img, device_id)
|
c := service.DeviceFrameCount[deviceId]
|
||||||
if c != count {
|
if c != count {
|
||||||
if c == -1 {
|
count = c
|
||||||
log.Printf("device:%d get frame err!", device_id)
|
buf := service.Device1CurrentFrame
|
||||||
worker.SetRedisSetRemove(online_conn_key, con_id)
|
err2 := ws.WriteMessage(websocket.BinaryMessage, buf)
|
||||||
break
|
|
||||||
}
|
|
||||||
//将img转[]byte
|
|
||||||
if img.Empty() {
|
|
||||||
log.Printf("device:%d img is empty! count = %d \n", device_id, 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
|
||||||
}
|
|
||||||
} 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)
|
||||||
|
|
@ -258,17 +223,12 @@ func subscribeAndHandleMessagesV3(ws *websocket.Conn, device_id int) {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
time.Sleep(100 * time.Millisecond)
|
time.Sleep(100 * time.Millisecond)
|
||||||
t_count++
|
tCount++
|
||||||
}
|
|
||||||
// 关闭img
|
|
||||||
err := img.Close()
|
|
||||||
if err != nil {
|
|
||||||
log.Printf("device:%d img close err:%v", device_id, 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(deviceId)+"_is_play", "1", time.Minute*5)
|
||||||
log.Printf("device_id: %d has set is_play to 0", device_id)
|
log.Printf("device_id: %d has set is_play to 0", deviceId)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
2
main.go
2
main.go
|
|
@ -125,8 +125,6 @@ func ReadConfigAndSetSystem() {
|
||||||
}
|
}
|
||||||
if isGetting == "false" && device.NextStop == false { //如果设备流已经停止且不暂停,则开启
|
if isGetting == "false" && device.NextStop == false { //如果设备流已经停止且不暂停,则开启
|
||||||
switch device.ID {
|
switch device.ID {
|
||||||
case 1:
|
|
||||||
service.Device1CurrentFrame = gocv.NewMat()
|
|
||||||
case 50:
|
case 50:
|
||||||
service.Device50CurrentFrame = gocv.NewMat()
|
service.Device50CurrentFrame = gocv.NewMat()
|
||||||
case 73:
|
case 73:
|
||||||
|
|
|
||||||
148
service/tool.go
148
service/tool.go
|
|
@ -18,7 +18,7 @@ var DeviceRWMap = &sync.Map{}
|
||||||
var DeviceCurrentFrameMap = &sync.Map{}
|
var DeviceCurrentFrameMap = &sync.Map{}
|
||||||
var DeviceFrameCount map[int]int
|
var DeviceFrameCount map[int]int
|
||||||
var DeviceIsGettingFrame = &sync.Map{}
|
var DeviceIsGettingFrame = &sync.Map{}
|
||||||
var Device1CurrentFrame gocv.Mat
|
var Device1CurrentFrame []byte
|
||||||
var Device50CurrentFrame gocv.Mat
|
var Device50CurrentFrame gocv.Mat
|
||||||
var Device73CurrentFrame gocv.Mat
|
var Device73CurrentFrame gocv.Mat
|
||||||
|
|
||||||
|
|
@ -65,10 +65,7 @@ func SetDeviceCurrentFrameV2(frame *gocv.Mat, deviceId int) error {
|
||||||
//设置当前帧
|
//设置当前帧
|
||||||
switch deviceId {
|
switch deviceId {
|
||||||
case 1:
|
case 1:
|
||||||
if Device1CurrentFrame.Empty() {
|
Device1CurrentFrame = (*frame).ToBytes()
|
||||||
Device1CurrentFrame = gocv.NewMatWithSize((*frame).Rows(), (*frame).Cols(), (*frame).Type())
|
|
||||||
}
|
|
||||||
(*frame).CopyTo(&Device1CurrentFrame)
|
|
||||||
case 50:
|
case 50:
|
||||||
if Device50CurrentFrame.Empty() {
|
if Device50CurrentFrame.Empty() {
|
||||||
Device50CurrentFrame = gocv.NewMatWithSize((*frame).Rows(), (*frame).Cols(), (*frame).Type())
|
Device50CurrentFrame = gocv.NewMatWithSize((*frame).Rows(), (*frame).Cols(), (*frame).Type())
|
||||||
|
|
@ -85,149 +82,14 @@ func SetDeviceCurrentFrameV2(frame *gocv.Mat, deviceId int) error {
|
||||||
if !ok {
|
if !ok {
|
||||||
return fmt.Errorf("设备:%s 当前帧计数不存在", deviceId)
|
return fmt.Errorf("设备:%s 当前帧计数不存在", deviceId)
|
||||||
}
|
}
|
||||||
|
if frameCount%10 == 0 {
|
||||||
|
log.Printf("设备:%d 当前帧: %d 已更新\n", deviceId, frameCount)
|
||||||
|
}
|
||||||
frameCount++
|
frameCount++
|
||||||
DeviceFrameCount[deviceId] = frameCount
|
DeviceFrameCount[deviceId] = frameCount
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
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()
|
|
||||||
//获取当前帧
|
|
||||||
switch deviceId {
|
|
||||||
case 1:
|
|
||||||
if (*frame).Empty() {
|
|
||||||
*frame = gocv.NewMatWithSize(Device1CurrentFrame.Rows(), Device1CurrentFrame.Cols(), Device1CurrentFrame.Type())
|
|
||||||
}
|
|
||||||
if Device1CurrentFrame.Empty() {
|
|
||||||
return -1
|
|
||||||
}
|
|
||||||
Device1CurrentFrame.CopyTo(frame)
|
|
||||||
case 50:
|
|
||||||
if (*frame).Empty() {
|
|
||||||
*frame = gocv.NewMatWithSize(Device50CurrentFrame.Rows(), Device50CurrentFrame.Cols(), Device50CurrentFrame.Type())
|
|
||||||
}
|
|
||||||
if Device50CurrentFrame.Empty() {
|
|
||||||
return -1
|
|
||||||
}
|
|
||||||
Device50CurrentFrame.CopyTo(frame)
|
|
||||||
case 73:
|
|
||||||
if (*frame).Empty() {
|
|
||||||
*frame = gocv.NewMatWithSize(Device73CurrentFrame.Rows(), Device73CurrentFrame.Cols(), Device73CurrentFrame.Type())
|
|
||||||
}
|
|
||||||
if Device73CurrentFrame.Empty() {
|
|
||||||
return -1
|
|
||||||
}
|
|
||||||
Device73CurrentFrame.CopyTo(frame)
|
|
||||||
|
|
||||||
}
|
|
||||||
frameCount, ok := DeviceFrameCount[deviceId]
|
|
||||||
if !ok {
|
|
||||||
return -1
|
|
||||||
}
|
|
||||||
return frameCount
|
|
||||||
}
|
|
||||||
|
|
||||||
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:
|
|
||||||
frame = gocv.NewMatWithSize(Device1CurrentFrame.Rows(), Device1CurrentFrame.Cols(), Device1CurrentFrame.Type())
|
|
||||||
Device1CurrentFrame.CopyTo(&frame)
|
|
||||||
//查看帧状态
|
|
||||||
//log.Printf("frame:%v,Device1CurrentFrame:%v \n", frame.Empty(), Device1CurrentFrame.Empty())
|
|
||||||
|
|
||||||
case 50:
|
|
||||||
frame = gocv.NewMatWithSize(Device50CurrentFrame.Rows(), Device50CurrentFrame.Cols(), Device50CurrentFrame.Type())
|
|
||||||
Device50CurrentFrame.CopyTo(&frame)
|
|
||||||
//查看帧状态
|
|
||||||
//log.Printf("frame:%v,Device50CurrentFrame:%v\n", frame.Empty(), Device50CurrentFrame.Empty())
|
|
||||||
case 73:
|
|
||||||
frame = gocv.NewMatWithSize(Device73CurrentFrame.Rows(), Device73CurrentFrame.Cols(), Device73CurrentFrame.Type())
|
|
||||||
Device73CurrentFrame.CopyTo(&frame)
|
|
||||||
}
|
|
||||||
|
|
||||||
frameCount, ok := DeviceFrameCount[deviceId]
|
|
||||||
if !ok {
|
|
||||||
return gocv.NewMat(), -1
|
|
||||||
}
|
|
||||||
|
|
||||||
//查看地址
|
|
||||||
//log.Printf("frame:%p,Device1CurrentFrame:%p,Device50CurrentFrame:%p\n", &frame, &Device1CurrentFrame, &Device50CurrentFrame)
|
|
||||||
return frame, frameCount
|
|
||||||
}
|
|
||||||
func GetDeviceCurrentFrame(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()
|
|
||||||
//获取当前帧
|
|
||||||
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)
|
|
||||||
}
|
|
||||||
frameCount, ok := DeviceFrameCount[deviceId]
|
|
||||||
if !ok {
|
|
||||||
return gocv.NewMat(), -1
|
|
||||||
}
|
|
||||||
return frame, frameCount
|
|
||||||
}
|
|
||||||
|
|
||||||
func getVideoFrame(device proto.DeviceInfo) {
|
func getVideoFrame(device proto.DeviceInfo) {
|
||||||
//捕获异常
|
//捕获异常
|
||||||
defer func() {
|
defer func() {
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue