Compare commits
8 Commits
d69e02115f
...
1e6a0996a0
| Author | SHA1 | Date |
|---|---|---|
|
|
1e6a0996a0 | |
|
|
f86c1fd3a4 | |
|
|
8827fa0412 | |
|
|
cead913af7 | |
|
|
0906fce8cf | |
|
|
5552548a35 | |
|
|
de9b8d23ac | |
|
|
abea9bd374 |
|
|
@ -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", "*")
|
||||||
|
|
@ -65,48 +65,78 @@ type videoStreamReq struct {
|
||||||
}
|
}
|
||||||
|
|
||||||
func GetVideoStream(c *gin.Context) {
|
func GetVideoStream(c *gin.Context) {
|
||||||
var req videoStreamReq
|
|
||||||
id, _ := c.Get("id")
|
id, _ := c.Get("id")
|
||||||
id1 := id.(int)
|
id1 := id.(int)
|
||||||
|
deviceIDSTR := c.Query("id")
|
||||||
|
deviceID, err := strconv.Atoi(deviceIDSTR)
|
||||||
|
if err != nil {
|
||||||
|
c.JSON(400, gin.H{"error": "device_id error"})
|
||||||
|
return
|
||||||
|
}
|
||||||
|
key := c.Query("key")
|
||||||
//校验权限
|
//校验权限
|
||||||
device := service.GetDevice(req.ID, id1)
|
device := service.GetDevice(deviceID, id1)
|
||||||
if device.ID == 0 {
|
if device.ID == 0 {
|
||||||
c.JSON(400, gin.H{"error": "device not exist"})
|
c.JSON(400, gin.H{"error": "device not exist"})
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
rKey := worker.GetRedis("video_stream_get_stream_key")
|
||||||
|
if rKey == "" {
|
||||||
|
rKey = "123456"
|
||||||
|
}
|
||||||
|
|
||||||
if err := c.ShouldBind(&req); err != nil {
|
|
||||||
c.JSON(400, gin.H{"error": err.Error()})
|
|
||||||
return
|
|
||||||
} else {
|
|
||||||
//查看id是否存在
|
//查看id是否存在
|
||||||
index := -1
|
index := -1
|
||||||
for _, device := range proto.Config.DeviceInfo {
|
for _, device_ := range proto.Config.DeviceInfo {
|
||||||
if device.ID == req.ID {
|
if device_.ID == deviceID {
|
||||||
index = req.ID
|
index = deviceID
|
||||||
break
|
break
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
if index == -1 {
|
if index == -1 {
|
||||||
c.JSON(400, gin.H{"error": "id not exist"})
|
c.JSON(400, gin.H{"error": "id config not exist"})
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
//查看key是否正确
|
//查看key是否正确
|
||||||
if req.Key != "123456" {
|
if key != rKey {
|
||||||
c.JSON(400, gin.H{"error": "key error"})
|
c.JSON(400, gin.H{"error": "key error"})
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
//设备流
|
//设备流
|
||||||
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
|
||||||
for {
|
for {
|
||||||
frame, cnt := service.GetDeviceCurrentFrame(req.ID)
|
if errCount > 10 {
|
||||||
|
log.Printf("stream device:%d errCount > 10", deviceID)
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
cnt := service.GetDeviceCurrentFrameV2(&frame, deviceID)
|
||||||
if cnt == count {
|
if cnt == count {
|
||||||
time.Sleep(50 * time.Millisecond)
|
time.Sleep(50 * time.Millisecond)
|
||||||
|
errCount++
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
if frame.Empty() {
|
||||||
|
log.Printf("stream device:%d frame is empty", deviceID)
|
||||||
|
time.Sleep(100 * time.Millisecond)
|
||||||
|
errCount++
|
||||||
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("stream 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"))
|
||||||
|
|
@ -127,8 +157,6 @@ func GetVideoStream(c *gin.Context) {
|
||||||
time.Sleep(50 * time.Millisecond) // 控制帧率,模拟每秒约20帧,可按实际调整
|
time.Sleep(50 * time.Millisecond) // 控制帧率,模拟每秒约20帧,可按实际调整
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// 发送实时视频流
|
// 发送实时视频流
|
||||||
|
|
@ -143,13 +171,13 @@ func GetRealTimeImage(c *gin.Context) {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
//查看设备是否在获取
|
//查看设备是否在获取
|
||||||
is_get, ok := service.DeviceIsGettingFrame.Load(device.ID)
|
isGetting := worker.GetRedis(fmt.Sprintf("device_%d_is_getting", device.ID))
|
||||||
if !ok || is_get == false {
|
if isGetting != "true" {
|
||||||
//直接返回
|
c.JSON(http.StatusOK, gin.H{"code": 4, "message": "device is not getting or not exist"})
|
||||||
c.JSON(http.StatusOK, gin.H{"code": 4, "message": "device not getting frame or not exist"})
|
log.Printf("device_id:%d is not getting or not exist", deviceIdInt)
|
||||||
log.Printf("device:%d not found", device.ID)
|
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
ws, err := upgrader.Upgrade(c.Writer, c.Request, nil)
|
ws, err := upgrader.Upgrade(c.Writer, c.Request, nil)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Printf("connect wss err:%v", err)
|
log.Printf("connect wss err:%v", err)
|
||||||
|
|
@ -170,9 +198,10 @@ func subscribeAndHandleMessagesV3(ws *websocket.Conn, device_id int) {
|
||||||
count := 0
|
count := 0
|
||||||
//定时器,发送计数器
|
//定时器,发送计数器
|
||||||
t_count := 0
|
t_count := 0
|
||||||
|
img := gocv.NewMat()
|
||||||
for {
|
for {
|
||||||
//从service获取当前帧
|
//从service获取当前帧
|
||||||
img, c := service.GetDeviceCurrentFrameV3(device_id)
|
c := service.GetDeviceCurrentFrameV2(&img, device_id)
|
||||||
if c != count {
|
if c != count {
|
||||||
//将img转[]byte
|
//将img转[]byte
|
||||||
if img.Empty() {
|
if img.Empty() {
|
||||||
|
|
@ -194,10 +223,6 @@ func subscribeAndHandleMessagesV3(ws *websocket.Conn, device_id int) {
|
||||||
break
|
break
|
||||||
}
|
}
|
||||||
c = count
|
c = count
|
||||||
err4 := img.Close()
|
|
||||||
if err4 != nil {
|
|
||||||
log.Printf("close img err:%v", err)
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
//每秒发送一次心跳检测
|
//每秒发送一次心跳检测
|
||||||
|
|
@ -215,6 +240,11 @@ func subscribeAndHandleMessagesV3(ws *websocket.Conn, device_id int) {
|
||||||
time.Sleep(100 * time.Millisecond)
|
time.Sleep(100 * time.Millisecond)
|
||||||
t_count++
|
t_count++
|
||||||
}
|
}
|
||||||
|
// 关闭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 {
|
||||||
|
|
|
||||||
35
main.go
35
main.go
|
|
@ -19,10 +19,11 @@ import (
|
||||||
)
|
)
|
||||||
|
|
||||||
var configPath string
|
var configPath string
|
||||||
|
var logCount int
|
||||||
|
|
||||||
func main() {
|
func main() {
|
||||||
fmt.Println("start server")
|
fmt.Println("start server")
|
||||||
gin.SetMode(gin.DebugMode) //设置为debug模式
|
gin.SetMode(gin.ReleaseMode) //设置为debug模式
|
||||||
r := gin.Default()
|
r := gin.Default()
|
||||||
//数据库初始
|
//数据库初始
|
||||||
err0 := dao.Init()
|
err0 := dao.Init()
|
||||||
|
|
@ -35,7 +36,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()
|
||||||
|
|
@ -51,6 +52,7 @@ func main() {
|
||||||
}
|
}
|
||||||
c.Start()
|
c.Start()
|
||||||
fmt.Println("定时任务已启动")
|
fmt.Println("定时任务已启动")
|
||||||
|
initDeviceGettingStatus()
|
||||||
err3 := r.Run(":" + proto.Config.SERVER_PORT)
|
err3 := r.Run(":" + proto.Config.SERVER_PORT)
|
||||||
if err3 != nil {
|
if err3 != nil {
|
||||||
panic("failed to run server:" + err3.Error())
|
panic("failed to run server:" + err3.Error())
|
||||||
|
|
@ -66,6 +68,7 @@ func init() {
|
||||||
} else {
|
} else {
|
||||||
configPath = "/home/videoplayer/vp_stream.conf"
|
configPath = "/home/videoplayer/vp_stream.conf"
|
||||||
}
|
}
|
||||||
|
logCount = 0
|
||||||
//读取配置文件
|
//读取配置文件
|
||||||
err := proto.ReadConfig(configPath)
|
err := proto.ReadConfig(configPath)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
|
@ -87,7 +90,20 @@ func myTask() {
|
||||||
ReadConfigAndSetSystem()
|
ReadConfigAndSetSystem()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func initDeviceGettingStatus() {
|
||||||
|
for _, device := range proto.Config.DeviceInfo {
|
||||||
|
if device.NextStop == false {
|
||||||
|
worker.SetRedis(fmt.Sprintf("device_%d_is_getting", device.ID), "false")
|
||||||
|
}
|
||||||
|
if device.NextStop == true {
|
||||||
|
worker.DelRedis(fmt.Sprintf("device_%d_is_getting", device.ID))
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
func ReadConfigAndSetSystem() {
|
func ReadConfigAndSetSystem() {
|
||||||
|
logCount++
|
||||||
//configPath := "/home/videoplayer/vp_stream.conf"
|
//configPath := "/home/videoplayer/vp_stream.conf"
|
||||||
//读取配置文件
|
//读取配置文件
|
||||||
err := proto.ReadConfig(configPath)
|
err := proto.ReadConfig(configPath)
|
||||||
|
|
@ -96,26 +112,33 @@ func ReadConfigAndSetSystem() {
|
||||||
}
|
}
|
||||||
//检测是否需要获取设备流,如果需要则开启
|
//检测是否需要获取设备流,如果需要则开启
|
||||||
for _, device := range proto.Config.DeviceInfo {
|
for _, device := range proto.Config.DeviceInfo {
|
||||||
is_get, ok := service.DeviceIsGettingFrame.Load(device.ID)
|
//isGet, ok := service.DeviceIsGettingFrame.Load(device.ID)
|
||||||
if !ok {
|
//isGet_ := isGet.(bool)
|
||||||
|
isGetting := worker.GetRedis(fmt.Sprintf("device_%d_is_getting", device.ID))
|
||||||
|
if isGetting == "" {
|
||||||
//说明没有这个设备,需初始化添加
|
//说明没有这个设备,需初始化添加
|
||||||
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[device.ID] = 0
|
service.DeviceFrameCount[device.ID] = 0
|
||||||
service.DeviceIsGettingFrame.Store(device.ID, false)
|
service.DeviceIsGettingFrame.Store(device.ID, false)
|
||||||
|
worker.SetRedis(fmt.Sprintf("device_%d_is_getting", device.ID), "false")
|
||||||
}
|
}
|
||||||
if is_get == false && device.NextStop == false { //如果设备流已经停止且不暂停,则开启
|
if isGetting == "false" && device.NextStop == false { //如果设备流已经停止且不暂停,则开启
|
||||||
switch device.ID {
|
switch device.ID {
|
||||||
case 1:
|
case 1:
|
||||||
service.Device1CurrentFrame = gocv.NewMat()
|
service.Device1CurrentFrame = gocv.NewMat()
|
||||||
case 50:
|
case 50:
|
||||||
service.Device50CurrentFrame = gocv.NewMat()
|
service.Device50CurrentFrame = gocv.NewMat()
|
||||||
|
case 73:
|
||||||
|
service.Device73CurrentFrame = gocv.NewMat()
|
||||||
}
|
}
|
||||||
go service.GetVideoStream(device.ID)
|
go service.GetVideoStream(device.ID)
|
||||||
log.Printf("device:%d has started!\n", device.ID)
|
log.Printf("device:%d has started!\n", device.ID)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
log.Println("每10秒执行一次,当前设备:", proto.Config.DeviceInfo)
|
if logCount%3600 == 0 {
|
||||||
|
log.Printf("每%d秒执行一次,当前设备:%v", logCount*10, proto.Config.DeviceInfo)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func JWTAuthMiddleware() gin.HandlerFunc {
|
func JWTAuthMiddleware() gin.HandlerFunc {
|
||||||
|
|
|
||||||
|
|
@ -109,17 +109,25 @@ 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 {
|
||||||
if !ok {
|
case 1:
|
||||||
return -1
|
if (*frame).Empty() {
|
||||||
|
*frame = gocv.NewMatWithSize(Device1CurrentFrame.Rows(), Device1CurrentFrame.Cols(), Device1CurrentFrame.Type())
|
||||||
}
|
}
|
||||||
frame_, ok = frameIface.(gocv.Mat)
|
Device1CurrentFrame.CopyTo(frame)
|
||||||
if !ok {
|
case 50:
|
||||||
log.Printf("DeviceCurrentFrameMap 存储的不是 gocv.Mat 类型,device_id: %d \n", deviceId)
|
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)
|
||||||
|
|
||||||
}
|
}
|
||||||
*frame = frame_
|
|
||||||
frameCount, ok := DeviceFrameCount[deviceId]
|
frameCount, ok := DeviceFrameCount[deviceId]
|
||||||
if !ok {
|
if !ok {
|
||||||
return -1
|
return -1
|
||||||
|
|
@ -161,7 +169,9 @@ func GetDeviceCurrentFrameV3(deviceId int) (gocv.Mat, int) {
|
||||||
Device50CurrentFrame.CopyTo(&frame)
|
Device50CurrentFrame.CopyTo(&frame)
|
||||||
//查看帧状态
|
//查看帧状态
|
||||||
//log.Printf("frame:%v,Device50CurrentFrame:%v\n", frame.Empty(), Device50CurrentFrame.Empty())
|
//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]
|
frameCount, ok := DeviceFrameCount[deviceId]
|
||||||
|
|
@ -301,13 +311,9 @@ func GetVideoStream(id int) {
|
||||||
log.Printf("设备:%d 错误: %v\n", id, err)
|
log.Printf("设备:%d 错误: %v\n", id, err)
|
||||||
}
|
}
|
||||||
}()
|
}()
|
||||||
is_get, ok := DeviceIsGettingFrame.Load(id)
|
isGetting := worker.GetRedis(fmt.Sprintf("device_%d_is_getting", id))
|
||||||
if !ok {
|
if isGetting == "true" || isGetting == "" {
|
||||||
log.Printf("device: %d not found", id)
|
log.Printf("设备:%d 正在运行,isGetting:%s", id, isGetting)
|
||||||
return
|
|
||||||
}
|
|
||||||
if is_get == true {
|
|
||||||
log.Printf("device: %d is running!", id)
|
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
for {
|
for {
|
||||||
|
|
@ -326,9 +332,9 @@ func GetVideoStream(id int) {
|
||||||
log.Printf("device: %d not found", id)
|
log.Printf("device: %d not found", id)
|
||||||
break
|
break
|
||||||
}
|
}
|
||||||
is_get, ok = DeviceIsGettingFrame.Load(id)
|
isGetting = worker.GetRedis(fmt.Sprintf("device_%d_is_getting", id))
|
||||||
if is_get == true {
|
if isGetting == "true" {
|
||||||
log.Printf("for device:%d is running!", id)
|
log.Printf("设备:%d 正在运行,isGetting:%s", id, isGetting)
|
||||||
break
|
break
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -339,9 +345,11 @@ func GetVideoStream(id int) {
|
||||||
//设置设备控制信息
|
//设置设备控制信息
|
||||||
status := Get(device.Control)
|
status := Get(device.Control)
|
||||||
DeviceIsGettingFrame.Store(id, true)
|
DeviceIsGettingFrame.Store(id, true)
|
||||||
|
worker.SetRedis(fmt.Sprintf("device_%d_is_getting", id), "true")
|
||||||
log.Printf("device: %d set control info status: %d", device.ID, status)
|
log.Printf("device: %d set control info status: %d", device.ID, status)
|
||||||
getVideoFrame(device)
|
getVideoFrame(device)
|
||||||
DeviceIsGettingFrame.Store(id, false)
|
DeviceIsGettingFrame.Store(id, false)
|
||||||
|
worker.SetRedis(fmt.Sprintf("device_%d_is_getting", id), "false")
|
||||||
//等待1s
|
//等待1s
|
||||||
time.Sleep(1 * time.Second)
|
time.Sleep(1 * time.Second)
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue