index.go 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  1. package common
  2. import (
  3. "sync/atomic"
  4. "time"
  5. "github.com/pion/rtp"
  6. "go.uber.org/zap"
  7. "m7s.live/engine/v4/log"
  8. "m7s.live/engine/v4/util"
  9. )
  10. type TimelineData[T any] struct {
  11. Timestamp time.Time
  12. Value T
  13. }
  14. type TrackState byte
  15. const (
  16. TrackStateOnline TrackState = iota // 上线
  17. TrackStateOffline // 下线
  18. )
  19. // Base 基础Track类
  20. type Base[T any, F IDataFrame[T]] struct {
  21. RingWriter[T, F]
  22. Name string
  23. log.Zap `json:"-" yaml:"-"`
  24. Stream IStream `json:"-" yaml:"-"`
  25. Attached atomic.Bool `json:"-" yaml:"-"`
  26. State TrackState
  27. ts time.Time
  28. bytes int
  29. frames int
  30. DropCount int `json:"-" yaml:"-"` //丢帧数
  31. BPS int
  32. FPS int
  33. Drops int // 丢帧率
  34. RawSize int // 裸数据长度
  35. RawPart []int // 裸数据片段用于UI上显示
  36. }
  37. func (bt *Base[T, F]) ComputeBPS(bytes int) {
  38. bt.bytes += bytes
  39. bt.frames++
  40. if elapse := time.Since(bt.ts).Seconds(); elapse > 1 {
  41. bt.BPS = int(float64(bt.bytes) / elapse)
  42. bt.FPS = int(float64(bt.frames) / elapse)
  43. bt.Drops = int(float64(bt.DropCount) / elapse)
  44. bt.bytes = 0
  45. bt.frames = 0
  46. bt.DropCount = 0
  47. bt.ts = time.Now()
  48. }
  49. }
  50. func (bt *Base[T, F]) GetName() string {
  51. return bt.Name
  52. }
  53. func (bt *Base[T, F]) GetBPS() int {
  54. return bt.BPS
  55. }
  56. func (bt *Base[T, F]) GetFPS() int {
  57. return bt.FPS
  58. }
  59. func (bt *Base[T, F]) GetDrops() int {
  60. return bt.Drops
  61. }
  62. // GetRBSize 获取缓冲区大小
  63. func (bt *Base[T, F]) GetRBSize() int {
  64. return bt.RingWriter.Size
  65. }
  66. func (bt *Base[T, F]) SnapForJson() {
  67. }
  68. func (bt *Base[T, F]) SetStuff(stuff ...any) {
  69. for _, s := range stuff {
  70. switch v := s.(type) {
  71. case IStream:
  72. bt.Stream = v
  73. bt.Zap = v.With(zap.String("track", bt.Name))
  74. case TrackState:
  75. bt.State = v
  76. case string:
  77. bt.Name = v
  78. }
  79. }
  80. }
  81. func (bt *Base[T, F]) Dispose() {
  82. bt.Value.Broadcast()
  83. }
  84. type Track interface {
  85. GetReaderCount() int32
  86. GetName() string
  87. GetBPS() int
  88. GetFPS() int
  89. GetDrops() int
  90. LastWriteTime() time.Time
  91. SnapForJson()
  92. SetStuff(stuff ...any)
  93. GetRBSize() int
  94. Dispose()
  95. }
  96. type AVTrack interface {
  97. Track
  98. PreFrame() *AVFrame
  99. CurrentFrame() *AVFrame
  100. Attach()
  101. Detach()
  102. WriteAVCC(ts uint32, frame *util.BLL) error //写入AVCC格式的数据
  103. WriteRTP(*util.ListItem[RTPFrame])
  104. WriteRTPPack(*rtp.Packet)
  105. WriteSequenceHead(sh []byte) error
  106. Flush()
  107. SetSpeedLimit(time.Duration)
  108. GetRTPFromPool() *util.ListItem[RTPFrame]
  109. GetFromPool(util.IBytes) *util.ListItem[util.Buffer]
  110. }
  111. type VideoTrack interface {
  112. AVTrack
  113. WriteSliceBytes(slice []byte)
  114. WriteNalu(uint32, uint32, []byte)
  115. WriteAnnexB(uint32, uint32, []byte)
  116. SetLostFlag()
  117. }
  118. type AudioTrack interface {
  119. AVTrack
  120. WriteADTS(uint32, util.IBytes)
  121. WriteRawBytes(uint32, util.IBytes)
  122. }