2025-01-11 22:34:36 +08:00
|
|
|
|
package service
|
2025-01-12 15:18:58 +08:00
|
|
|
|
|
|
|
|
|
|
import (
|
2025-01-14 15:00:40 +08:00
|
|
|
|
"VideoStream/dao"
|
2025-01-12 15:18:58 +08:00
|
|
|
|
"VideoStream/proto"
|
2025-01-14 15:47:28 +08:00
|
|
|
|
"VideoStream/worker"
|
2025-01-12 15:18:58 +08:00
|
|
|
|
"fmt"
|
|
|
|
|
|
"gocv.io/x/gocv"
|
|
|
|
|
|
"image"
|
|
|
|
|
|
"image/color"
|
|
|
|
|
|
"log"
|
|
|
|
|
|
"net/http"
|
|
|
|
|
|
"sync"
|
|
|
|
|
|
"time"
|
|
|
|
|
|
)
|
|
|
|
|
|
|
2025-01-15 00:25:10 +08:00
|
|
|
|
var DeviceRWMap = &sync.Map{}
|
|
|
|
|
|
var DeviceCurrentFrameMap = &sync.Map{}
|
|
|
|
|
|
var DeviceFrameCount = &sync.Map{}
|
|
|
|
|
|
var DeviceIsGettingFrame = &sync.Map{}
|
2025-01-12 15:18:58 +08:00
|
|
|
|
|
|
|
|
|
|
func SetDeviceCurrentFrame(frame gocv.Mat, device_id int) error {
|
|
|
|
|
|
//获取读写锁
|
2025-01-15 00:25:10 +08:00
|
|
|
|
mutex_, ok := DeviceRWMap.Load(device_id)
|
2025-01-12 15:18:58 +08:00
|
|
|
|
if !ok {
|
|
|
|
|
|
return fmt.Errorf("设备:%s 读写锁不存在", device_id)
|
|
|
|
|
|
}
|
2025-01-15 00:25:10 +08:00
|
|
|
|
mutex := mutex_.(*sync.RWMutex)
|
2025-01-12 15:18:58 +08:00
|
|
|
|
mutex.Lock()
|
|
|
|
|
|
defer mutex.Unlock()
|
|
|
|
|
|
//设置当前帧
|
2025-01-15 00:25:10 +08:00
|
|
|
|
DeviceCurrentFrameMap.Store(device_id, frame)
|
|
|
|
|
|
frame_count, ok := DeviceFrameCount.Load(device_id)
|
|
|
|
|
|
if !ok {
|
|
|
|
|
|
return fmt.Errorf("设备:%s 当前帧计数不存在", device_id)
|
|
|
|
|
|
}
|
|
|
|
|
|
frame_count_ := frame_count.(int)
|
|
|
|
|
|
frame_count_++
|
|
|
|
|
|
DeviceFrameCount.Store(device_id, frame_count_)
|
2025-01-12 15:18:58 +08:00
|
|
|
|
return nil
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
func GetDeviceCurrentFrame(device_id int) (gocv.Mat, int) {
|
|
|
|
|
|
//获取读写锁
|
2025-01-15 00:25:10 +08:00
|
|
|
|
mutex_, ok := DeviceRWMap.Load(device_id)
|
2025-01-12 15:18:58 +08:00
|
|
|
|
if !ok {
|
|
|
|
|
|
return gocv.NewMat(), -1
|
|
|
|
|
|
}
|
2025-01-15 23:20:44 +08:00
|
|
|
|
mutex, ok := mutex_.(*sync.RWMutex)
|
|
|
|
|
|
if !ok {
|
2025-01-15 23:41:01 +08:00
|
|
|
|
log.Printf("DeviceRWMap 存储的不是 *sync.RWMutex 类型,device_id: %d", device_id)
|
|
|
|
|
|
return gocv.NewMat(), -1
|
2025-01-15 23:20:44 +08:00
|
|
|
|
}
|
2025-01-12 15:18:58 +08:00
|
|
|
|
mutex.RLock()
|
|
|
|
|
|
defer mutex.RUnlock()
|
|
|
|
|
|
//获取当前帧
|
2025-01-15 23:20:44 +08:00
|
|
|
|
frameIface, ok := DeviceCurrentFrameMap.Load(device_id)
|
|
|
|
|
|
if !ok {
|
|
|
|
|
|
return gocv.NewMat(), -1
|
|
|
|
|
|
}
|
|
|
|
|
|
frame, ok := frameIface.(gocv.Mat)
|
|
|
|
|
|
if !ok {
|
2025-01-15 23:41:01 +08:00
|
|
|
|
log.Printf("DeviceCurrentFrameMap 存储的不是 gocv.Mat 类型,device_id: %d", device_id)
|
2025-01-15 23:20:44 +08:00
|
|
|
|
}
|
|
|
|
|
|
frame_countIface, ok := DeviceFrameCount.Load(device_id)
|
|
|
|
|
|
if !ok {
|
|
|
|
|
|
return gocv.NewMat(), -1
|
|
|
|
|
|
}
|
|
|
|
|
|
frame_count, ok := frame_countIface.(int)
|
|
|
|
|
|
if !ok {
|
2025-01-15 23:41:01 +08:00
|
|
|
|
log.Printf("DeviceFrameCount 存储的不是 int 类型,device_id: %d", device_id)
|
2025-01-15 23:20:44 +08:00
|
|
|
|
}
|
|
|
|
|
|
return frame, frame_count
|
2025-01-12 15:18:58 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
func getVideoFrame(device proto.DeviceInfo) {
|
2025-01-15 23:31:50 +08:00
|
|
|
|
//捕获异常
|
|
|
|
|
|
defer func() {
|
|
|
|
|
|
if err := recover(); err != nil {
|
2025-01-15 23:41:01 +08:00
|
|
|
|
log.Printf("设备:%d 错误: %v\n", device.ID, err)
|
2025-01-15 23:31:50 +08:00
|
|
|
|
}
|
|
|
|
|
|
}()
|
|
|
|
|
|
|
2025-01-12 15:18:58 +08:00
|
|
|
|
webcam, err := gocv.OpenVideoCapture(device.Stream)
|
|
|
|
|
|
if err != nil {
|
2025-01-15 23:41:01 +08:00
|
|
|
|
log.Printf("设备:%s 错误: 无法打开视频流,err: %v\n", device.ID, err)
|
2025-01-12 15:18:58 +08:00
|
|
|
|
return
|
|
|
|
|
|
}
|
2025-01-15 23:31:50 +08:00
|
|
|
|
defer func(webcam *gocv.VideoCapture) {
|
|
|
|
|
|
err2 := webcam.Close()
|
|
|
|
|
|
if err2 != nil {
|
2025-01-15 23:41:01 +08:00
|
|
|
|
log.Printf("设备:%d 错误: 无法关闭视频流,%s \n", device.ID, err2.Error())
|
2025-01-15 23:31:50 +08:00
|
|
|
|
}
|
|
|
|
|
|
}(webcam)
|
2025-01-12 15:18:58 +08:00
|
|
|
|
// 字体相关设置,对应OpenCV默认字体等,这里简化处理,实际可按需求调整
|
|
|
|
|
|
font := gocv.FontHersheySimplex
|
|
|
|
|
|
fontScale := 0.5
|
|
|
|
|
|
fontColor := color.RGBA{G: 255}
|
|
|
|
|
|
lineType := 2
|
|
|
|
|
|
z := 0
|
|
|
|
|
|
for {
|
|
|
|
|
|
if device.LogFrame > 0 && z%device.LogFrame == 0 {
|
2025-01-15 23:41:01 +08:00
|
|
|
|
log.Printf("设备:%d 当前帧: %d\n", device.ID, z)
|
2025-01-12 15:18:58 +08:00
|
|
|
|
}
|
|
|
|
|
|
if device.NextStop {
|
|
|
|
|
|
break
|
|
|
|
|
|
}
|
2025-01-14 23:51:53 +08:00
|
|
|
|
frame := gocv.NewMat()
|
2025-01-12 15:18:58 +08:00
|
|
|
|
ok := webcam.Read(&frame)
|
|
|
|
|
|
if !ok {
|
2025-01-15 23:41:01 +08:00
|
|
|
|
log.Printf("设备 错误: 无法从视频流中读取帧:", device, "\n")
|
2025-01-12 15:18:58 +08:00
|
|
|
|
break
|
|
|
|
|
|
}
|
|
|
|
|
|
if frame.Empty() {
|
2025-01-15 23:41:01 +08:00
|
|
|
|
log.Printf("设备:%s 错误: 无法从视频流中读取帧\n", device)
|
2025-01-12 15:18:58 +08:00
|
|
|
|
//等待50ms
|
|
|
|
|
|
time.Sleep(50 * time.Millisecond)
|
|
|
|
|
|
continue
|
|
|
|
|
|
}
|
|
|
|
|
|
height := frame.Rows()
|
|
|
|
|
|
width := frame.Cols()
|
|
|
|
|
|
if height < device.CheckFrameHeight || width < device.CheckFrameWidth {
|
2025-01-15 23:41:01 +08:00
|
|
|
|
log.Printf("设备:%s 帧尺寸已改变\n", device)
|
2025-01-12 15:18:58 +08:00
|
|
|
|
break
|
|
|
|
|
|
}
|
|
|
|
|
|
currentTime := time.Now().Format("2006-01-02 15:04:05")
|
|
|
|
|
|
gocv.PutText(&frame, currentTime, image.Point{10, 20}, font, fontScale, fontColor, lineType)
|
|
|
|
|
|
//需要将帧付给全局变量
|
2025-01-15 00:25:10 +08:00
|
|
|
|
err3 := SetDeviceCurrentFrame(frame, device.ID)
|
|
|
|
|
|
if err3 != nil {
|
2025-01-15 23:41:01 +08:00
|
|
|
|
log.Printf("设备:%d 错误: 无法设置当前帧,err:%s \n", device.ID, err3.Error())
|
2025-01-12 15:18:58 +08:00
|
|
|
|
}
|
|
|
|
|
|
z++
|
2025-01-14 23:51:53 +08:00
|
|
|
|
err2 := frame.Close()
|
|
|
|
|
|
if err2 != nil {
|
2025-01-15 23:41:01 +08:00
|
|
|
|
log.Printf("设备:%d,计数z=%d, 错误: 无法关闭帧\n", device.ID, z)
|
2025-01-14 23:51:53 +08:00
|
|
|
|
}
|
2025-01-12 15:18:58 +08:00
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// 发起get请求,返回响应状态码
|
|
|
|
|
|
func Get(url string) int {
|
|
|
|
|
|
req, err := http.NewRequest("GET", url, nil)
|
|
|
|
|
|
var client = &http.Client{}
|
|
|
|
|
|
if err != nil {
|
|
|
|
|
|
return 500
|
|
|
|
|
|
}
|
|
|
|
|
|
resp, err := client.Do(req)
|
|
|
|
|
|
if err != nil {
|
|
|
|
|
|
return 500
|
|
|
|
|
|
}
|
|
|
|
|
|
return resp.StatusCode
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
func GetVideoStream(id int) {
|
2025-01-15 00:25:10 +08:00
|
|
|
|
is_get, ok := DeviceIsGettingFrame.Load(id)
|
|
|
|
|
|
if !ok {
|
2025-01-15 23:41:01 +08:00
|
|
|
|
log.Printf("device: %d not found", id)
|
2025-01-15 00:25:10 +08:00
|
|
|
|
return
|
|
|
|
|
|
}
|
|
|
|
|
|
if is_get == true {
|
2025-01-15 23:41:01 +08:00
|
|
|
|
log.Printf("device: %d is running!", id)
|
2025-01-14 22:53:42 +08:00
|
|
|
|
return
|
|
|
|
|
|
}
|
2025-01-12 15:18:58 +08:00
|
|
|
|
for {
|
|
|
|
|
|
var device proto.DeviceInfo
|
|
|
|
|
|
var index int
|
|
|
|
|
|
//获取设备信息
|
|
|
|
|
|
for i, device1 := range proto.Config.DeviceInfo {
|
|
|
|
|
|
if device1.ID == id {
|
|
|
|
|
|
device = device1
|
|
|
|
|
|
break
|
|
|
|
|
|
}
|
|
|
|
|
|
index = i
|
|
|
|
|
|
}
|
|
|
|
|
|
if index == len(proto.Config.DeviceInfo) {
|
|
|
|
|
|
//设备不存在
|
2025-01-15 23:41:01 +08:00
|
|
|
|
log.Printf("device: %d not found", id)
|
2025-01-12 15:18:58 +08:00
|
|
|
|
break
|
|
|
|
|
|
}
|
2025-01-15 00:25:10 +08:00
|
|
|
|
is_get, ok = DeviceIsGettingFrame.Load(id)
|
|
|
|
|
|
if is_get == true {
|
2025-01-15 23:41:01 +08:00
|
|
|
|
log.Printf("for device:%d is running!", id)
|
2025-01-14 23:29:52 +08:00
|
|
|
|
break
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2025-01-12 15:18:58 +08:00
|
|
|
|
if device.NextStop {
|
2025-01-15 00:25:10 +08:00
|
|
|
|
DeviceIsGettingFrame.Store(id, false)
|
2025-01-12 15:18:58 +08:00
|
|
|
|
break
|
|
|
|
|
|
}
|
|
|
|
|
|
//设置设备控制信息
|
|
|
|
|
|
status := Get(device.Control)
|
2025-01-15 00:25:10 +08:00
|
|
|
|
DeviceIsGettingFrame.Store(id, true)
|
2025-01-15 23:41:01 +08:00
|
|
|
|
log.Printf("device: %d set control info status: %d", device.ID, status)
|
2025-01-12 15:18:58 +08:00
|
|
|
|
getVideoFrame(device)
|
2025-01-15 00:25:10 +08:00
|
|
|
|
DeviceIsGettingFrame.Store(id, false)
|
2025-01-14 23:29:52 +08:00
|
|
|
|
//等待1s
|
|
|
|
|
|
time.Sleep(1 * time.Second)
|
2025-01-12 15:18:58 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
func DoGetVideoStream() {
|
|
|
|
|
|
for _, device := range proto.Config.DeviceInfo {
|
|
|
|
|
|
go GetVideoStream(device.ID)
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
2025-01-14 15:00:40 +08:00
|
|
|
|
|
|
|
|
|
|
func GetDevice(id, auth_id int) dao.Device {
|
|
|
|
|
|
return dao.FindDeviceByID(id, auth_id)
|
|
|
|
|
|
}
|
2025-01-14 15:47:28 +08:00
|
|
|
|
|
|
|
|
|
|
func SetConfigRedis(key, config string) {
|
|
|
|
|
|
worker.SetRedis(key, config)
|
|
|
|
|
|
}
|