main.go 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  1. package gb28181
  2. import (
  3. "os"
  4. "strings"
  5. "sync"
  6. "time"
  7. "github.com/ghettovoice/gosip/sip"
  8. myip "github.com/husanpao/ip"
  9. "go.uber.org/zap"
  10. . "m7s.live/engine/v4"
  11. "m7s.live/engine/v4/util"
  12. )
  13. type GB28181PositionConfig struct {
  14. AutosubPosition bool `desc:"是否自动订阅定位"` //是否自动订阅定位
  15. Expires time.Duration `default:"3600s" desc:"订阅周期"` //订阅周期
  16. Interval time.Duration `default:"6s" desc:"订阅间隔"` //订阅间隔
  17. }
  18. type GB28181Config struct {
  19. InviteMode int `default:"1" desc:"拉流模式" enum:"0:手动拉流,1:预拉流,2:按需拉流"` //邀请模式,0:手动拉流,1:预拉流,2:按需拉流
  20. InviteIDs string `default:"131,132" desc:"允许邀请的设备类型( 11~13位是设备类型编码),逗号分割"` //按照国标gb28181协议允许邀请的设备类型:132 摄像机 NVR
  21. ListenAddr string `default:"0.0.0.0" desc:"监听IP地址"` //监听地址
  22. //sip服务器的配置
  23. SipNetwork string `default:"udp" desc:"废弃,请使用 Port"` //传输协议,默认UDP,可选TCP
  24. SipIP string `desc:"sip 服务IP地址"` //sip 服务器公网IP
  25. SipPort sip.Port `default:"5060" desc:"废弃,请使用 Port"` //sip 服务器端口,默认 5060
  26. Serial string `default:"34020000002000000001" desc:"sip 服务 id"` //sip 服务器 id, 默认 34020000002000000001
  27. Realm string `default:"3402000000" desc:"sip 服务域"` //sip 服务器域,默认 3402000000
  28. Username string `desc:"sip 服务账号"` //sip 服务器账号
  29. Password string `desc:"sip 服务密码"` //sip 服务器密码
  30. Port struct { // 新配置方式
  31. Sip string `default:"udp:5060" desc:"sip服务端口号"`
  32. Media string `default:"tcp:58200-59200" desc:"媒体服务端口号"`
  33. }
  34. RegisterValidity time.Duration `default:"3600s" desc:"注册有效期"` //注册有效期,单位秒,默认 3600
  35. HeartbeatInterval time.Duration `default:"60s" desc:"心跳间隔"` //心跳间隔,单位秒,默认 60
  36. //媒体服务器配置
  37. MediaIP string `desc:"媒体服务IP地址"` //媒体服务器地址
  38. MediaPort uint16 `default:"58200" desc:"废弃,请使用 Port"` //媒体服务器端口
  39. MediaNetwork string `default:"tcp" desc:"废弃,请使用 Port"` //媒体传输协议,默认UDP,可选TCP
  40. MediaPortMin uint16 `default:"58200" desc:"废弃,请使用 Port"`
  41. MediaPortMax uint16 `default:"59200" desc:"废弃,请使用 Port"`
  42. RemoveBanInterval time.Duration `default:"600s" desc:"移除禁止设备间隔"` //移除禁止设备间隔
  43. routes map[string]string
  44. DumpPath string `desc:"dump PS流本地文件路径"` //dump PS流本地文件路径
  45. Ignores []string `desc:"忽略的设备ID"` //忽略的设备ID
  46. ignores map[string]struct{}
  47. tcpPorts PortManager
  48. udpPorts PortManager
  49. Position GB28181PositionConfig //关于定位的配置参数
  50. }
  51. var SipUri *sip.SipUri
  52. func (c *GB28181Config) initRoutes() {
  53. c.routes = make(map[string]string)
  54. tempIps := myip.LocalAndInternalIPs()
  55. for k, v := range tempIps {
  56. c.routes[k] = v
  57. if lastdot := strings.LastIndex(k, "."); lastdot >= 0 {
  58. c.routes[k[0:lastdot]] = k
  59. }
  60. }
  61. GB28181Plugin.Info("LocalAndInternalIPs", zap.Any("routes", c.routes))
  62. }
  63. func (c *GB28181Config) OnEvent(event any) {
  64. switch e := event.(type) {
  65. case FirstConfig:
  66. if c.Port.Sip != "udp:5060" {
  67. protocol, ports := util.Conf2Listener(c.Port.Sip)
  68. c.SipNetwork = protocol
  69. c.SipPort = sip.Port(ports[0])
  70. }
  71. if c.Port.Media != "tcp:58200-59200" {
  72. protocol, ports := util.Conf2Listener(c.Port.Media)
  73. c.MediaNetwork = protocol
  74. if len(ports) > 1 {
  75. c.MediaPortMin = ports[0]
  76. c.MediaPortMax = ports[1]
  77. } else {
  78. c.MediaPortMin = 0
  79. c.MediaPortMax = 0
  80. c.MediaPort = ports[0]
  81. }
  82. }
  83. if len(c.Ignores) > 0 {
  84. c.ignores = make(map[string]struct{})
  85. for _, v := range c.Ignores {
  86. c.ignores[v] = util.Null
  87. }
  88. }
  89. os.MkdirAll(c.DumpPath, 0766)
  90. c.ReadDevices()
  91. SipUri = &sip.SipUri{
  92. FUser: sip.String{Str: c.Serial},
  93. FHost: c.SipIP,
  94. FPort: &conf.SipPort,
  95. }
  96. go c.initRoutes()
  97. c.startServer()
  98. case InvitePublish:
  99. if c.InviteMode == INVIDE_MODE_ONSUBSCRIBE {
  100. //流可能是回放流,stream path是device/channel/start-end形式
  101. streamNames := strings.Split(e.Target, "/")
  102. if channel := FindChannel(streamNames[0], streamNames[1]); channel != nil {
  103. opt := InviteOptions{}
  104. if len(streamNames) > 2 {
  105. last := len(streamNames) - 1
  106. timestr := streamNames[last]
  107. trange := strings.Split(timestr, "-")
  108. if len(trange) == 2 {
  109. startTime := trange[0]
  110. endTime := trange[1]
  111. opt.Validate(startTime, endTime)
  112. }
  113. }
  114. channel.TryAutoInvite(&opt)
  115. }
  116. }
  117. case SEpublish:
  118. if channel := FindChannel(e.Target.AppName, strings.TrimSuffix(e.Target.StreamName, "/rtsp")); channel != nil {
  119. channel.LiveSubSP = e.Target.Path
  120. }
  121. case SEclose:
  122. if channel := FindChannel(e.Target.AppName, strings.TrimSuffix(e.Target.StreamName, "/rtsp")); channel != nil {
  123. channel.LiveSubSP = ""
  124. }
  125. if v, ok := PullStreams.LoadAndDelete(e.Target.Path); ok {
  126. go v.(*PullStream).Bye()
  127. }
  128. }
  129. }
  130. func (c *GB28181Config) IsMediaNetworkTCP() bool {
  131. return strings.ToLower(c.MediaNetwork) == "tcp"
  132. }
  133. var conf GB28181Config
  134. var GB28181Plugin = InstallPlugin(&conf)
  135. var PullStreams sync.Map //拉流