frame.go 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208
  1. package common
  2. import (
  3. "bytes"
  4. "io"
  5. "net"
  6. "sync"
  7. "sync/atomic"
  8. "time"
  9. "github.com/pion/rtp"
  10. "m7s.live/engine/v4/codec"
  11. "m7s.live/engine/v4/log"
  12. "m7s.live/engine/v4/util"
  13. )
  14. func SplitAnnexB[T ~[]byte](frame T, process func(T), delimiter []byte) {
  15. for after := frame; len(frame) > 0; frame = after {
  16. if frame, after, _ = bytes.Cut(frame, delimiter); len(frame) > 0 {
  17. process(frame)
  18. }
  19. }
  20. }
  21. type RTPFrame struct {
  22. *rtp.Packet
  23. Raw []byte
  24. }
  25. func (r *RTPFrame) H264Type() (naluType codec.H264NALUType) {
  26. return naluType.Parse(r.Payload[0])
  27. }
  28. func (r *RTPFrame) H265Type() (naluType codec.H265NALUType) {
  29. return naluType.Parse(r.Payload[0])
  30. }
  31. func (r *RTPFrame) Unmarshal(raw []byte) *RTPFrame {
  32. if r.Packet == nil {
  33. r.Packet = &rtp.Packet{}
  34. }
  35. if err := r.Packet.Unmarshal(raw); err != nil {
  36. log.Error(err)
  37. return nil
  38. }
  39. return r
  40. }
  41. type DataFrame[T any] struct {
  42. DeltaTime uint32 // 相对上一帧时间戳,毫秒
  43. WriteTime time.Time // 写入时间,可用于比较两个帧的先后
  44. Sequence uint32 // 在一个Track中的序号
  45. BytesIn int // 输入字节数用于计算BPS
  46. CanRead bool `json:"-" yaml:"-"` // 是否可读取
  47. readerCount atomic.Int32 `json:"-" yaml:"-"` // 读取者数量
  48. Data T `json:"-" yaml:"-"`
  49. sync.Cond `json:"-" yaml:"-"`
  50. }
  51. func NewDataFrame[T any]() *DataFrame[T] {
  52. return &DataFrame[T]{}
  53. }
  54. func (df *DataFrame[T]) IsWriting() bool {
  55. return !df.CanRead
  56. }
  57. func (df *DataFrame[T]) IsDiscarded() bool {
  58. return df.L == nil
  59. }
  60. func (df *DataFrame[T]) Discard() int32 {
  61. df.L = nil //标记为废弃
  62. return df.readerCount.Load()
  63. }
  64. func (df *DataFrame[T]) SetSequence(sequence uint32) {
  65. df.Sequence = sequence
  66. }
  67. func (df *DataFrame[T]) GetSequence() uint32 {
  68. return df.Sequence
  69. }
  70. func (df *DataFrame[T]) ReaderEnter() int32 {
  71. return df.readerCount.Add(1)
  72. }
  73. func (df *DataFrame[T]) ReaderCount() int32 {
  74. return df.readerCount.Load()
  75. }
  76. func (df *DataFrame[T]) ReaderLeave() int32 {
  77. return df.readerCount.Add(-1)
  78. }
  79. func (df *DataFrame[T]) StartWrite() bool {
  80. if df.readerCount.Load() > 0 {
  81. df.Discard() //标记为废弃
  82. return false
  83. } else {
  84. df.CanRead = false //标记为正在写入
  85. return true
  86. }
  87. }
  88. func (df *DataFrame[T]) Ready() {
  89. df.WriteTime = time.Now()
  90. df.CanRead = true //标记为可读取
  91. df.Broadcast()
  92. }
  93. func (df *DataFrame[T]) Init() {
  94. df.L = util.EmptyLocker
  95. }
  96. func (df *DataFrame[T]) Reset() {
  97. df.BytesIn = 0
  98. df.DeltaTime = 0
  99. }
  100. type AVFrame struct {
  101. DataFrame[any]
  102. IFrame bool
  103. PTS time.Duration
  104. DTS time.Duration
  105. Timestamp time.Duration // 绝对时间戳
  106. ADTS *util.ListItem[util.Buffer] `json:"-" yaml:"-"` // ADTS头
  107. AVCC util.BLL `json:"-" yaml:"-"` // 打包好的AVCC格式(MPEG-4格式、Byte-Stream Format)
  108. RTP util.List[RTPFrame] `json:"-" yaml:"-"`
  109. AUList util.BLLs `json:"-" yaml:"-"` // 裸数据
  110. }
  111. func NewAVFrame() *AVFrame {
  112. return &AVFrame{}
  113. }
  114. func (av *AVFrame) WriteAVCC(ts uint32, frame *util.BLL) {
  115. if ts == 0 {
  116. ts = 1
  117. }
  118. av.Timestamp = time.Duration(ts) * time.Millisecond
  119. av.BytesIn += frame.ByteLength
  120. for {
  121. item := frame.Shift()
  122. if item == nil {
  123. break
  124. }
  125. av.AVCC.Push(item)
  126. }
  127. // frame.Transfer(&av.AVCC)
  128. // frame.ByteLength = 0
  129. }
  130. // Reset 重置数据,复用内存
  131. func (av *AVFrame) Reset() {
  132. av.RTP.Recycle()
  133. av.AVCC.Recycle()
  134. av.AUList.Recycle()
  135. if av.ADTS != nil {
  136. av.ADTS.Recycle()
  137. av.ADTS = nil
  138. }
  139. av.Timestamp = 0
  140. av.IFrame = false
  141. av.DataFrame.Reset()
  142. }
  143. func (av *AVFrame) Assign(source *AVFrame) {
  144. av.IFrame = source.IFrame
  145. av.PTS = source.PTS
  146. av.DTS = source.DTS
  147. av.Timestamp = source.Timestamp
  148. av.BytesIn = source.BytesIn
  149. av.DeltaTime = source.DeltaTime
  150. source.AUList.Range(func(au *util.BLL) bool {
  151. var nau util.BLL
  152. au.Range(func(b util.Buffer) bool {
  153. nau.PushValue(b)
  154. return true
  155. })
  156. nau.ByteLength = au.ByteLength
  157. av.AUList.PushValue(&nau)
  158. return true
  159. })
  160. }
  161. type ParamaterSets [][]byte
  162. func (v ParamaterSets) GetAnnexB() (r net.Buffers) {
  163. for _, v := range v {
  164. r = append(r, codec.NALU_Delimiter2, v)
  165. }
  166. return
  167. }
  168. func (v ParamaterSets) WriteAnnexBTo(w io.Writer) (n int, err error) {
  169. var n1, n2 int
  170. for _, v := range v {
  171. if n1, err = w.Write(codec.NALU_Delimiter2); err != nil {
  172. return
  173. }
  174. n += n1
  175. if n2, err = w.Write(v); err != nil {
  176. return
  177. }
  178. n += n2
  179. }
  180. return
  181. }