Skip to content

Commit

Permalink
Merge pull request #59 from joestarzxh/master
Browse files Browse the repository at this point in the history
[fix]gb stop api错误返回,修正NotifyClose devices与channel的关系对应
  • Loading branch information
ZSC714725 authored Mar 12, 2024
2 parents 92f45cd + 1d93224 commit 930e513
Show file tree
Hide file tree
Showing 4 changed files with 46 additions and 24 deletions.
20 changes: 14 additions & 6 deletions gb28181/channel.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package gb28181

import (
"errors"
"fmt"
config "lalmax/conf"
"net/http"
Expand Down Expand Up @@ -41,6 +42,11 @@ type ChannelInfo struct {
Latitude string `xml:"Latitude"` // 纬度
StreamName string `xml:"-"`
serial string
mediaInfo struct {
IsInvite bool
Ssrc uint32
StreamName string
}
}

type ChannelStatus string
Expand All @@ -64,9 +70,8 @@ func (channel *Channel) CanInvite(streamName string) bool {
if channel.Parental != 0 {
return false
}
d := channel.device

if d.mediaInfo.IsInvite {
if channel.mediaInfo.IsInvite {
return false
}

Expand Down Expand Up @@ -210,9 +215,9 @@ func (channel *Channel) Invite(opt *InviteOptions, conf config.GB28181Config, st
}
}

d.mediaInfo.IsInvite = true
d.mediaInfo.Ssrc = opt.SSRC
d.mediaInfo.StreamName = streamName
channel.mediaInfo.IsInvite = true
channel.mediaInfo.Ssrc = opt.SSRC
channel.mediaInfo.StreamName = streamName

ackReq := sip.NewAckRequest("", invite, inviteRes, "", nil)
channel.ackReq = ackReq
Expand All @@ -231,8 +236,11 @@ func (channel *Channel) Bye(streamName string) (err error) {
if err == nil {
channel.ackReq = nil
}
return err
} else {
return errors.New("channel has been closed")
}
return err

}
func (channel *Channel) CreateRequst(Method sip.RequestMethod, conf config.GB28181Config) (req sip.Request) {
d := channel.device
Expand Down
5 changes: 0 additions & 5 deletions gb28181/device.go
Original file line number Diff line number Diff line change
Expand Up @@ -54,11 +54,6 @@ type Device struct {
GpsTime time.Time //gps时间
Longitude string //经度
Latitude string //纬度
mediaInfo struct {
IsInvite bool
Ssrc uint32
StreamName string
}
}

func (d *Device) syncChannels(conf config.GB28181Config) {
Expand Down
8 changes: 6 additions & 2 deletions gb28181/http_logic.go
Original file line number Diff line number Diff line change
Expand Up @@ -80,8 +80,12 @@ func (g *GbLogic) UpdateNotify(c *gin.Context) {
if err := c.ShouldBindJSON(&reqUpdateNotify); err != nil {
ResponseErrorWithMsg(c, CodeInvalidParam, CodeInvalidParam.Msg())
} else {
g.s.GetSyncChannels(reqUpdateNotify.DeviceId)
ResponseSuccess(c, nil)
if g.s.GetSyncChannels(reqUpdateNotify.DeviceId) {
ResponseSuccess(c, nil)
} else {
ResponseErrorWithMsg(c, CodeDeviceNotRegister, CodeDeviceNotRegister.Msg())
}

}

}
37 changes: 26 additions & 11 deletions gb28181/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -122,11 +122,15 @@ func (s *GB28181Server) CheckSsrc(ssrc uint32) (string, bool) {

Devices.Range(func(_, value any) bool {
d := value.(*Device)
if d.mediaInfo.Ssrc == ssrc {
isValidSsrc = true
streamName = d.mediaInfo.StreamName
}

d.channelMap.Range(func(key, value any) bool {
ch := value.(*Channel)
if ch.mediaInfo.Ssrc == ssrc {
isValidSsrc = true
streamName = ch.mediaInfo.StreamName
return false
}
return true
})
return true
})

Expand All @@ -140,11 +144,19 @@ func (s *GB28181Server) CheckSsrc(ssrc uint32) (string, bool) {
func (s *GB28181Server) NotifyClose(streamName string) {
Devices.Range(func(_, value any) bool {
d := value.(*Device)
if d.mediaInfo.StreamName == streamName {
d.mediaInfo.IsInvite = false
d.mediaInfo.Ssrc = 0
d.mediaInfo.StreamName = ""
}
d.channelMap.Range(func(key, value any) bool {
ch := value.(*Channel)
if ch.mediaInfo.StreamName == streamName {
if ch.mediaInfo.IsInvite {
ch.Bye(streamName)
}
ch.mediaInfo.IsInvite = false
ch.mediaInfo.Ssrc = 0
ch.mediaInfo.StreamName = ""
return false
}
return true
})

return true
})
Expand Down Expand Up @@ -236,10 +248,13 @@ func (s *GB28181Server) GetAllSyncChannels() {
return true
})
}
func (s *GB28181Server) GetSyncChannels(deviceId string) {
func (s *GB28181Server) GetSyncChannels(deviceId string) bool {
if v, ok := Devices.Load(deviceId); ok {
d := v.(*Device)
d.syncChannels(s.conf)
return true
} else {
return false
}
}
func (s *GB28181Server) FindChannel(deviceId string, channelId string) (channel *Channel) {
Expand Down

0 comments on commit 930e513

Please sign in to comment.