Compare commits

...

7 Commits

3 changed files with 123 additions and 83 deletions

View File

@ -34,7 +34,7 @@ func SetUpToolGroup(router *gin.Engine) {
} }
// 跨域访问cross origin resource share // 跨域访问cross origin resource share
func CrosHandler() gin.HandlerFunc { func CorsHandler() gin.HandlerFunc {
return func(context *gin.Context) { return func(context *gin.Context) {
//method := context.Request.Method //method := context.Request.Method
context.Writer.Header().Set("Access-Control-Allow-Origin", "*") context.Writer.Header().Set("Access-Control-Allow-Origin", "*")
@ -68,17 +68,17 @@ func GetVideoStream(c *gin.Context) {
var req videoStreamReq var req videoStreamReq
id, _ := c.Get("id") id, _ := c.Get("id")
id1 := id.(int) id1 := id.(int)
//校验权限 //将query参数绑定到结构体
device := service.GetDevice(req.ID, id1)
if device.ID == 0 {
c.JSON(400, gin.H{"error": "device not exist"})
return
}
if err := c.ShouldBind(&req); err != nil { if err := c.ShouldBind(&req); err != nil {
c.JSON(400, gin.H{"error": err.Error()}) c.JSON(400, gin.H{"error": err.Error()})
return return
} else { } else {
//校验权限
device := service.GetDevice(req.ID, id1)
if device.ID == 0 {
c.JSON(400, gin.H{"error": "device not exist"})
return
}
//查看id是否存在 //查看id是否存在
index := -1 index := -1
for _, device := range proto.Config.DeviceInfo { for _, device := range proto.Config.DeviceInfo {
@ -99,14 +99,20 @@ 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
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 {
fmt.Printf("img encode err:%v", err2)
time.Sleep(50 * time.Millisecond)
continue
}
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"))
@ -161,22 +167,22 @@ func subscribeAndHandleMessagesV3(ws *websocket.Conn, device_id int) {
//图片计数器 //图片计数器
count := 0 count := 0
//定时器,发送计数器 //定时器,发送计数器
t_count := 0 tCount := 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
if img.Empty() { if img.Empty() {
log.Printf("device:%d img is empty!", device_id) log.Printf("device:%d img is empty! count=%d", device_id, c)
} else { } else {
//gocv.Matrix转为jpeg //gocv.Matrix转为jpeg
buf, err := gocv.IMEncode(".jpg", img) buf, err := gocv.IMEncode(".jpg", img)
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 +190,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()
@ -194,20 +200,25 @@ func subscribeAndHandleMessagesV3(ws *websocket.Conn, device_id int) {
} }
} 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)
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++ tCount++
}
//关闭img
err := img.Close()
if err != nil {
log.Printf("img close err:%v", err)
} }
end:
// 查看是否还有其他连接,没有则设置 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)

12
main.go
View File

@ -10,7 +10,6 @@ import (
"github.com/gin-gonic/gin" "github.com/gin-gonic/gin"
"github.com/golang-jwt/jwt" "github.com/golang-jwt/jwt"
"github.com/robfig/cron/v3" "github.com/robfig/cron/v3"
"gocv.io/x/gocv"
"log" "log"
"runtime" "runtime"
"strconv" "strconv"
@ -35,7 +34,7 @@ func main() {
if err0 != nil { if err0 != nil {
panic("failed to connect redis:" + err0.Error()) panic("failed to connect redis:" + err0.Error())
} }
r.Use(handler.CrosHandler()) r.Use(handler.CorsHandler())
r.Use(JWTAuthMiddleware()) // 使用 JWT 认证中间件 r.Use(JWTAuthMiddleware()) // 使用 JWT 认证中间件
handler.SetUpToolGroup(r) // Tool handler.SetUpToolGroup(r) // Tool
err := worker.InitRedis() err := worker.InitRedis()
@ -71,13 +70,14 @@ func init() {
if err != nil { if err != nil {
panic("failed to read config file:" + err.Error()) panic("failed to read config file:" + err.Error())
} }
service.DeviceFrameCount = make(map[int]int)
for _, device := range proto.Config.DeviceInfo { for _, device := range proto.Config.DeviceInfo {
//service.DeviceRWMap[device.ID] = sync.RWMutex{} //service.DeviceRWMap[device.ID] = sync.RWMutex{}
//service.DeviceCurrentFrameMap[device.ID] = gocv.NewMat() //service.DeviceCurrentFrameMap[device.ID] = gocv.NewMat()
//service.DeviceIsGettingFrame[device.ID] = false //service.DeviceIsGettingFrame[device.ID] = false
service.DeviceRWMap.Store(device.ID, &sync.RWMutex{}) service.DeviceRWMap.Store(device.ID, &sync.RWMutex{})
service.DeviceCurrentFrameMap.Store(device.ID, gocv.NewMat()) //service.DeviceCurrentFrameMap.Store(device.ID, gocv.NewMat())
service.DeviceFrameCount.Store(device.ID, 0) service.DeviceFrameCount[device.ID] = 0
service.DeviceIsGettingFrame.Store(device.ID, false) service.DeviceIsGettingFrame.Store(device.ID, false)
} }
} }
@ -99,8 +99,8 @@ 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.Store(device.ID, gocv.NewMat())
service.DeviceFrameCount.Store(device.ID, 0) service.DeviceFrameCount[device.ID] = 0
service.DeviceIsGettingFrame.Store(device.ID, false) service.DeviceIsGettingFrame.Store(device.ID, false)
} }
if is_get == false && device.NextStop == false { //如果设备流已经停止且不暂停,则开启 if is_get == false && device.NextStop == false { //如果设备流已经停止且不暂停,则开启

View File

@ -15,39 +15,48 @@ import (
) )
var DeviceRWMap = &sync.Map{} var DeviceRWMap = &sync.Map{}
var DeviceCurrentFrameMap = &sync.Map{}
var DeviceFrameCount = &sync.Map{}
var DeviceIsGettingFrame = &sync.Map{}
func SetDeviceCurrentFrame(frame gocv.Mat, device_id int) error { // var DeviceCurrentFrameMap = &sync.Map{}
var DeviceFrameCount map[int]int
var DeviceIsGettingFrame = &sync.Map{}
var Device1CurrentFrame gocv.Mat
var Device50CurrentFrame gocv.Mat
var Device73CurrentFrame gocv.Mat
func SetDeviceCurrentFrame(frame *gocv.Mat, deviceId int) error {
//获取读写锁 //获取读写锁
mutex_, ok := DeviceRWMap.Load(device_id) mutex_, ok := DeviceRWMap.Load(deviceId)
if !ok { if !ok {
return fmt.Errorf("设备:%s 读写锁不存在", device_id) return fmt.Errorf("设备:%s 读写锁不存在", deviceId)
} }
mutex := mutex_.(*sync.RWMutex) mutex := mutex_.(*sync.RWMutex)
mutex.Lock() mutex.Lock()
defer mutex.Unlock() 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)
}
}
}
//设置当前帧 //设置当前帧
DeviceCurrentFrameMap.Store(device_id, frame) switch deviceId {
frame_count, ok := DeviceFrameCount.Load(device_id) case 1:
if !ok { if Device1CurrentFrame.Empty() {
return fmt.Errorf("设备:%s 当前帧计数不存在", device_id) Device1CurrentFrame = gocv.NewMatWithSize((*frame).Rows(), (*frame).Cols(), (*frame).Type())
}
(*frame).CopyTo(&Device1CurrentFrame)
case 50:
if Device50CurrentFrame.Empty() {
Device50CurrentFrame = gocv.NewMatWithSize((*frame).Rows(), (*frame).Cols(), (*frame).Type())
}
(*frame).CopyTo(&Device50CurrentFrame)
case 73:
if Device73CurrentFrame.Empty() {
Device73CurrentFrame = gocv.NewMatWithSize((*frame).Rows(), (*frame).Cols(), (*frame).Type())
}
(*frame).CopyTo(&Device73CurrentFrame)
} }
frame_count_ := frame_count.(int)
frame_count_++ frameCount, ok := DeviceFrameCount[deviceId]
DeviceFrameCount.Store(device_id, frame_count_) if !ok {
return fmt.Errorf("设备:%s 当前帧计数不存在", deviceId)
}
frameCount++
DeviceFrameCount[deviceId] = frameCount
return nil return nil
} }
@ -70,26 +79,31 @@ 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) switch deviceId {
case 1:
if frame.Empty() {
*frame = gocv.NewMatWithSize(Device1CurrentFrame.Rows(), Device1CurrentFrame.Cols(), Device1CurrentFrame.Type())
}
Device1CurrentFrame.CopyTo(frame)
case 50:
if frame.Empty() {
*frame = gocv.NewMatWithSize(Device50CurrentFrame.Rows(), Device50CurrentFrame.Cols(), Device50CurrentFrame.Type())
}
Device50CurrentFrame.CopyTo(frame)
case 73:
if frame.Empty() {
*frame = gocv.NewMatWithSize(Device73CurrentFrame.Rows(), Device73CurrentFrame.Cols(), Device73CurrentFrame.Type())
}
Device73CurrentFrame.CopyTo(frame)
}
frameCount, ok := DeviceFrameCount[deviceId]
if !ok { if !ok {
return -1 return -1
} }
frame_, ok = frameIface.(gocv.Mat) return frameCount
if !ok {
log.Printf("DeviceCurrentFrameMap 存储的不是 gocv.Mat 类型device_id: %d \n", deviceId)
}
*frame = frame_
frame_countIface, ok := DeviceFrameCount.Load(deviceId)
if !ok {
return -1
}
frame_count, ok := frame_countIface.(int)
if !ok {
log.Printf("DeviceFrameCount 存储的不是 int 类型device_id: %d", deviceId)
}
return frame_count
} }
func GetDeviceCurrentFrame(deviceId int) (gocv.Mat, int) { func GetDeviceCurrentFrame(deviceId int) (gocv.Mat, int) {
@ -112,24 +126,34 @@ func GetDeviceCurrentFrame(deviceId int) (gocv.Mat, int) {
} }
mutex.RLock() mutex.RLock()
defer mutex.RUnlock() defer mutex.RUnlock()
var ret gocv.Mat
if ret.Empty() {
}
//获取当前帧 //获取当前帧
frameIface, ok := DeviceCurrentFrameMap.Load(deviceId) switch deviceId {
if !ok { case 1:
return gocv.NewMat(), -1 if ret.Empty() {
ret = gocv.NewMatWithSize(Device1CurrentFrame.Rows(), Device1CurrentFrame.Cols(), Device1CurrentFrame.Type())
}
Device1CurrentFrame.CopyTo(&ret)
case 50:
if ret.Empty() {
ret = gocv.NewMatWithSize(Device50CurrentFrame.Rows(), Device50CurrentFrame.Cols(), Device50CurrentFrame.Type())
}
Device50CurrentFrame.CopyTo(&ret)
case 73:
if ret.Empty() {
ret = gocv.NewMatWithSize(Device73CurrentFrame.Rows(), Device73CurrentFrame.Cols(), Device73CurrentFrame.Type())
}
Device73CurrentFrame.CopyTo(&ret)
} }
frame, ok := frameIface.(gocv.Mat) frameCount, ok := DeviceFrameCount[deviceId]
if !ok { if !ok {
log.Printf("DeviceCurrentFrameMap 存储的不是 gocv.Mat 类型device_id: %d \n", deviceId) log.Printf("设备:%d 当前帧计数不存在\n", deviceId)
frameCount = -1
} }
frame_countIface, ok := DeviceFrameCount.Load(deviceId) return ret, frameCount
if !ok {
return gocv.NewMat(), -1
}
frame_count, ok := frame_countIface.(int)
if !ok {
log.Printf("DeviceFrameCount 存储的不是 int 类型device_id: %d", deviceId)
}
return frame, frame_count
} }
func getVideoFrame(device proto.DeviceInfo) { func getVideoFrame(device proto.DeviceInfo) {
@ -157,6 +181,8 @@ func getVideoFrame(device proto.DeviceInfo) {
fontColor := color.RGBA{G: 255} fontColor := color.RGBA{G: 255}
lineType := 2 lineType := 2
z := 0 z := 0
var frame gocv.Mat
frame = gocv.NewMat()
for { for {
if device.LogFrame > 0 && z%device.LogFrame == 0 { if device.LogFrame > 0 && z%device.LogFrame == 0 {
log.Printf("设备:%d 当前帧: %d\n", device.ID, z) log.Printf("设备:%d 当前帧: %d\n", device.ID, z)
@ -164,7 +190,6 @@ func getVideoFrame(device proto.DeviceInfo) {
if device.NextStop { if device.NextStop {
break break
} }
frame := gocv.NewMat()
ok := webcam.Read(&frame) ok := webcam.Read(&frame)
if !ok { if !ok {
log.Printf("设备:%v 错误: 无法从视频流中读取帧\n", device) log.Printf("设备:%v 错误: 无法从视频流中读取帧\n", device)
@ -183,14 +208,18 @@ func getVideoFrame(device proto.DeviceInfo) {
break break
} }
currentTime := time.Now().Format("2006-01-02 15:04:05") currentTime := time.Now().Format("2006-01-02 15:04:05")
gocv.PutText(&frame, currentTime, image.Point{10, 20}, font, fontScale, fontColor, lineType) gocv.PutText(&frame, currentTime, image.Point{X: 10, Y: 20}, font, fontScale, fontColor, lineType)
//需要将帧付给全局变量 //需要将帧付给全局变量
err3 := SetDeviceCurrentFrame(frame, device.ID) err3 := SetDeviceCurrentFrame(&frame, device.ID)
if err3 != nil { if err3 != nil {
log.Printf("设备:%d 错误: 无法设置当前帧,err:%s \n", device.ID, err3.Error()) log.Printf("设备:%d 错误: 无法设置当前帧,err:%s \n", device.ID, err3.Error())
} }
z++ z++
} }
err2 := frame.Close()
if err2 != nil {
log.Printf("设备:%d 错误: 无法释放帧资源,err:%s \n", device.ID, err.Error())
}
} }
// 发起get请求,返回响应状态码 // 发起get请求,返回响应状态码