subscribers.go 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175
  1. package engine
  2. import (
  3. "encoding/json"
  4. "go.uber.org/zap"
  5. "m7s.live/engine/v4/common"
  6. "m7s.live/engine/v4/config"
  7. )
  8. type Subscribers struct {
  9. public map[ISubscriber]*waitTracks
  10. internal map[ISubscriber]*waitTracks
  11. waits map[*waitTracks]ISubscriber
  12. waitAborted bool // 不再等待了
  13. }
  14. func (s *Subscribers) Init() {
  15. s.public = make(map[ISubscriber]*waitTracks)
  16. s.internal = make(map[ISubscriber]*waitTracks)
  17. s.waits = make(map[*waitTracks]ISubscriber)
  18. }
  19. func (s *Subscribers) MarshalJSON() ([]byte, error) {
  20. var subers []ISubscriber
  21. for suber := range s.public {
  22. subers = append(subers, suber)
  23. }
  24. return json.Marshal(subers)
  25. }
  26. func (s *Subscribers) Broadcast(event any) {
  27. for sub := range s.internal {
  28. sub.OnEvent(event)
  29. }
  30. for sub := range s.public {
  31. sub.OnEvent(event)
  32. }
  33. }
  34. func (s *Subscribers) Pick() ISubscriber {
  35. for sub := range s.public {
  36. return sub
  37. }
  38. return nil
  39. }
  40. func (s *Subscribers) Len() int {
  41. return len(s.public)
  42. }
  43. func (s *Subscribers) RangeAll(f func(sub ISubscriber)) {
  44. s.rangeAll(func(sub ISubscriber, wait *waitTracks) {
  45. f(sub)
  46. })
  47. }
  48. func (s *Subscribers) rangeAll(f func(sub ISubscriber, wait *waitTracks)) {
  49. for sub, wait := range s.internal {
  50. f(sub, wait)
  51. }
  52. for sub, wait := range s.public {
  53. f(sub, wait)
  54. }
  55. }
  56. func (s *Subscribers) OnTrack(track common.Track) {
  57. s.rangeAll(func(sub ISubscriber, wait *waitTracks) {
  58. if _, ok := s.waits[wait]; ok {
  59. if wait.Accept(track) {
  60. delete(s.waits, wait)
  61. }
  62. } else {
  63. sub.OnEvent(track)
  64. }
  65. })
  66. }
  67. func (s *Subscribers) OnPublisherLost(event StateEvent) {
  68. s.rangeAll(func(sub ISubscriber, wait *waitTracks) {
  69. if _, ok := s.waits[wait]; ok {
  70. wait.Reject(ErrPublisherLost)
  71. delete(s.waits, wait)
  72. }
  73. sub.OnEvent(event)
  74. })
  75. }
  76. // SendInviteTrack 广播需要的 Track(转码插件可以用到)
  77. func (s *Subscribers) SendInviteTrack(stream *Stream) {
  78. var video = map[string]ISubscriber{}
  79. var audio = map[string]ISubscriber{}
  80. for wait, suber := range s.waits {
  81. for _, name := range wait.video {
  82. video[name] = suber
  83. }
  84. for _, name := range wait.audio {
  85. audio[name] = suber
  86. }
  87. }
  88. for v, suber := range video {
  89. InviteTrack(v, suber)
  90. }
  91. for a, suber := range audio {
  92. InviteTrack(a, suber)
  93. }
  94. }
  95. func (s *Subscribers) AbortWait() {
  96. s.waitAborted = true
  97. for wait := range s.waits {
  98. wait.Resolve()
  99. delete(s.waits, wait)
  100. }
  101. }
  102. func (s *Subscribers) Find(id string) ISubscriber {
  103. for sub := range s.public {
  104. if sub.GetSubscriber().ID == id {
  105. return sub
  106. }
  107. }
  108. return nil
  109. }
  110. func (s *Subscribers) Delete(suber ISubscriber) {
  111. io := suber.GetSubscriber()
  112. io.TrackPlayer.Audio = nil
  113. io.TrackPlayer.AudioReader = nil
  114. io.TrackPlayer.Video = nil
  115. io.TrackPlayer.VideoReader = nil
  116. for _, reader := range io.readers {
  117. reader.Track.Debug("reader -1", zap.Int32("count", reader.Track.ReaderCount.Add(-1)))
  118. }
  119. io.readers = nil
  120. if _, ok := s.public[suber]; ok {
  121. delete(s.public, suber)
  122. io.Info("suber -1", zap.Int("remains", s.Len()))
  123. }
  124. if _, ok := s.internal[suber]; ok {
  125. delete(s.internal, suber)
  126. io.Info("innersuber -1", zap.Int("remains", len(s.internal)))
  127. }
  128. if config.Global.EnableSubEvent {
  129. EventBus <- UnsubscribeEvent{CreateEvent(suber)}
  130. }
  131. }
  132. func (s *Subscribers) Add(suber ISubscriber, wait *waitTracks) {
  133. io := suber.GetSubscriber()
  134. if io.Config.Internal {
  135. s.internal[suber] = wait
  136. io.Info("innersuber +1", zap.Int("remains", len(s.internal)))
  137. } else {
  138. s.public[suber] = wait
  139. io.Info("suber +1", zap.Int("remains", s.Len()))
  140. if config.Global.EnableSubEvent {
  141. EventBus <- suber
  142. }
  143. }
  144. if wait.NeedWait() {
  145. s.waits[wait] = suber
  146. } else {
  147. wait.Resolve()
  148. }
  149. }
  150. func (s *Subscribers) Dispose() {
  151. for w := range s.waits {
  152. w.Reject(ErrStreamIsClosed)
  153. }
  154. s.waits = nil
  155. s.public = nil
  156. s.internal = nil
  157. }