diff --git a/gb28181/channel.go b/gb28181/channel.go index 2f3fa77..95b2749 100644 --- a/gb28181/channel.go +++ b/gb28181/channel.go @@ -1,6 +1,7 @@ package gb28181 import ( + "errors" "fmt" config "lalmax/conf" "net/http" @@ -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 @@ -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 } @@ -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 @@ -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 diff --git a/gb28181/device.go b/gb28181/device.go index f3a46cb..3197378 100644 --- a/gb28181/device.go +++ b/gb28181/device.go @@ -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) { diff --git a/gb28181/http_logic.go b/gb28181/http_logic.go index d718e5f..a9e9885 100644 --- a/gb28181/http_logic.go +++ b/gb28181/http_logic.go @@ -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()) + } + } } diff --git a/gb28181/server.go b/gb28181/server.go index 28aed9c..1b537f5 100644 --- a/gb28181/server.go +++ b/gb28181/server.go @@ -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 }) @@ -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 }) @@ -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) {