aac.go 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265
  1. package track
  2. import (
  3. "fmt"
  4. "io"
  5. "net"
  6. "github.com/bluenviron/mediacommon/pkg/bits"
  7. "go.uber.org/zap"
  8. "m7s.live/engine/v4/codec"
  9. . "m7s.live/engine/v4/common"
  10. "m7s.live/engine/v4/util"
  11. )
  12. var _ SpesificTrack = (*AAC)(nil)
  13. func NewAAC(stream IStream, stuff ...any) (aac *AAC) {
  14. aac = &AAC{
  15. Mode: 2,
  16. }
  17. aac.SizeLength = 13
  18. aac.IndexLength = 3
  19. aac.IndexDeltaLength = 3
  20. aac.CodecID = codec.CodecID_AAC
  21. aac.Channels = 2
  22. aac.SampleSize = 16
  23. aac.SetStuff("aac", byte(97), aac, stuff, stream)
  24. if aac.BytesPool == nil {
  25. aac.BytesPool = make(util.BytesPool, 17)
  26. }
  27. aac.AVCCHead = []byte{0xAF, 1}
  28. return
  29. }
  30. type AAC struct {
  31. Audio
  32. Mode int // 1为lbr,2为hbr
  33. fragments *util.BLL // 用于处理不完整的AU,缺少的字节数
  34. }
  35. func (aac *AAC) WriteADTS(ts uint32, b util.IBytes) {
  36. adts := b.Bytes()
  37. if aac.SequenceHead == nil {
  38. profile := ((adts[2] & 0xc0) >> 6) + 1
  39. sampleRate := (adts[2] & 0x3c) >> 2
  40. channel := ((adts[2] & 0x1) << 2) | ((adts[3] & 0xc0) >> 6)
  41. config1 := (profile << 3) | ((sampleRate & 0xe) >> 1)
  42. config2 := ((sampleRate & 0x1) << 7) | (channel << 3)
  43. aac.Media.WriteSequenceHead([]byte{0xAF, 0x00, config1, config2})
  44. aac.SampleRate = uint32(codec.SamplingFrequencies[sampleRate])
  45. aac.Channels = channel
  46. aac.Parse(aac.SequenceHead[2:])
  47. aac.Attach()
  48. }
  49. aac.generateTimestamp(ts)
  50. frameLen := (int(adts[3]&3) << 11) | (int(adts[4]) << 3) | (int(adts[5]) >> 5)
  51. for len(adts) >= frameLen {
  52. aac.Value.AUList.Push(aac.BytesPool.GetShell(adts[7:frameLen]))
  53. adts = adts[frameLen:]
  54. if len(adts) < 7 {
  55. break
  56. }
  57. frameLen = (int(adts[3]&3) << 11) | (int(adts[4]) << 3) | (int(adts[5]) >> 5)
  58. }
  59. aac.Value.ADTS = aac.GetFromPool(b)
  60. aac.Flush()
  61. }
  62. // https://datatracker.ietf.org/doc/html/rfc3640#section-3.2.1
  63. func (aac *AAC) WriteRTPFrame(rtpItem *util.ListItem[RTPFrame]) {
  64. aac.Value.RTP.Push(rtpItem)
  65. frame := &rtpItem.Value
  66. if len(frame.Payload) < 2 {
  67. // aac.fragments = aac.fragments[:0]
  68. return
  69. }
  70. if aac.SampleRate != 90000 {
  71. aac.generateTimestamp(uint32(uint64(frame.Timestamp) * 90000 / uint64(aac.SampleRate)))
  72. }
  73. auHeaderLen := util.ReadBE[int](frame.Payload[:2]) //通常为16,即一个AU Header的长度
  74. if auHeaderLen == 0 {
  75. aac.Value.AUList.Push(aac.BytesPool.GetShell(frame.Payload[:2]))
  76. aac.Flush()
  77. } else {
  78. payload := frame.Payload[2:]
  79. // AU-headers
  80. dataLens, err := aac.readAUHeaders(payload, auHeaderLen)
  81. if err != nil {
  82. // discard pending fragmented packets
  83. return
  84. }
  85. pos := (auHeaderLen >> 3)
  86. if (auHeaderLen % 8) != 0 {
  87. pos++
  88. }
  89. payload = payload[pos:]
  90. if aac.fragments == nil {
  91. if frame.Header.Marker {
  92. // AUs
  93. for _, dataLen := range dataLens {
  94. if len(payload) < int(dataLen) {
  95. aac.fragments = &util.BLL{}
  96. aac.fragments.Push(aac.BytesPool.GetShell(payload))
  97. // aac.fragments = aac.fragments[:0]
  98. // aac.Error("payload is too short 1", zap.Int("dataLen", int(dataLen)), zap.Int("len", len(payload)))
  99. return
  100. }
  101. aac.AppendAuBytes(payload[:dataLen])
  102. payload = payload[dataLen:]
  103. }
  104. } else {
  105. if len(dataLens) != 1 {
  106. // aac.fragments = aac.fragments[:0]
  107. aac.Error("a fragmented packet can only contain one AU")
  108. return
  109. }
  110. aac.fragments = &util.BLL{}
  111. // if len(payload) < int(dataLens[0]) {
  112. // aac.fragments = aac.fragments[:0]
  113. // aac.Error("payload is too short 2", zap.Int("dataLen", int(dataLens[0])), zap.Int("len", len(payload)))
  114. // return
  115. // }
  116. aac.fragments.Push(aac.BytesPool.GetShell(payload))
  117. // aac.fragments = append(aac.fragments, payload[:dataLens[0]])
  118. return
  119. }
  120. } else {
  121. // we are decoding a fragmented AU
  122. if len(dataLens) != 1 {
  123. aac.fragments.Recycle()
  124. aac.fragments = nil
  125. // aac.fragments = aac.fragments[:0]
  126. aac.Error("a fragmented packet can only contain one AU")
  127. return
  128. }
  129. // if len(payload) < int(dataLens[0]) {
  130. // aac.fragments = aac.fragments[:0]
  131. // aac.Error("payload is too short 3", zap.Int("dataLen", int(dataLens[0])), zap.Int("len", len(payload)))
  132. // return
  133. // }
  134. // if fragmentedSize := util.SizeOfBuffers(aac.fragments) + int(dataLens[0]); fragmentedSize > 5*1024 {
  135. // aac.fragments = aac.fragments[:0] // discard pending fragmented packets
  136. // aac.Error(fmt.Sprintf("AU size (%d) is too big (maximum is %d)", fragmentedSize, 5*1024))
  137. // return
  138. // }
  139. // aac.fragments = append(aac.fragments, payload[:dataLens[0]])
  140. aac.fragments.Push(aac.BytesPool.GetShell(payload))
  141. if !frame.Header.Marker {
  142. return
  143. }
  144. if uint64(aac.fragments.ByteLength) != dataLens[0] {
  145. aac.Error("fragmented AU size is not correct", zap.Uint64("dataLen", dataLens[0]), zap.Int("len", aac.fragments.ByteLength))
  146. }
  147. aac.Value.AUList.PushValue(aac.fragments)
  148. // aac.AppendAuBytes(aac.fragments...)
  149. aac.fragments = nil
  150. }
  151. aac.Flush()
  152. }
  153. }
  154. func (aac *AAC) WriteSequenceHead(sh []byte) error {
  155. aac.Media.WriteSequenceHead(sh)
  156. config1, config2 := aac.SequenceHead[2], aac.SequenceHead[3]
  157. aac.Channels = ((config2 >> 3) & 0x0F) //声道
  158. aac.SampleRate = uint32(codec.SamplingFrequencies[((config1&0x7)<<1)|(config2>>7)])
  159. aac.Parse(aac.SequenceHead[2:])
  160. go aac.Attach()
  161. return nil
  162. }
  163. func (aac *AAC) WriteAVCC(ts uint32, frame *util.BLL) error {
  164. if l := frame.ByteLength; l < 4 {
  165. aac.Error("AVCC data too short", zap.Int("len", l))
  166. return io.ErrShortWrite
  167. }
  168. if frame.GetByte(1) == 0 {
  169. aac.WriteSequenceHead(frame.ToBytes())
  170. frame.Recycle()
  171. } else {
  172. au := frame.ToBuffers()
  173. au[0] = au[0][2:]
  174. aac.AppendAuBytes(au...)
  175. aac.Audio.WriteAVCC(ts, frame)
  176. }
  177. return nil
  178. }
  179. func (aac *AAC) CompleteRTP(value *AVFrame) {
  180. l := value.AUList.ByteLength
  181. //AU_HEADER_LENGTH,因为单位是bit, 除以8就是auHeader的字节长度;又因为单个auheader字节长度2字节,所以再除以2就是auheader的个数。
  182. auHeaderLen := []byte{0x00, 0x10, (byte)((l & 0x1fe0) >> 5), (byte)((l & 0x1f) << 3)} // 3 = 16-13, 5 = 8-3
  183. var packets [][][]byte
  184. r := value.AUList.Next.Value.NewReader()
  185. for bufs := r.ReadN(RTPMTU); len(bufs) > 0; bufs = r.ReadN(RTPMTU) {
  186. packets = append(packets, append(net.Buffers{auHeaderLen}, bufs...))
  187. }
  188. aac.PacketizeRTP(packets...)
  189. }
  190. func (aac *AAC) readAUHeaders(buf []byte, headersLen int) ([]uint64, error) {
  191. firstRead := false
  192. count := 0
  193. for i := 0; i < headersLen; {
  194. if i == 0 {
  195. i += aac.SizeLength
  196. i += aac.IndexLength
  197. } else {
  198. i += aac.SizeLength
  199. i += aac.IndexDeltaLength
  200. }
  201. count++
  202. }
  203. dataLens := make([]uint64, count)
  204. pos := 0
  205. i := 0
  206. for headersLen > 0 {
  207. dataLen, err := bits.ReadBits(buf, &pos, aac.SizeLength)
  208. if err != nil {
  209. return nil, err
  210. }
  211. headersLen -= aac.SizeLength
  212. if !firstRead {
  213. firstRead = true
  214. if aac.IndexLength > 0 {
  215. auIndex, err := bits.ReadBits(buf, &pos, aac.IndexLength)
  216. if err != nil {
  217. return nil, err
  218. }
  219. headersLen -= aac.IndexLength
  220. if auIndex != 0 {
  221. return nil, fmt.Errorf("AU-index different than zero is not supported")
  222. }
  223. }
  224. } else if aac.IndexDeltaLength > 0 {
  225. auIndexDelta, err := bits.ReadBits(buf, &pos, aac.IndexDeltaLength)
  226. if err != nil {
  227. return nil, err
  228. }
  229. headersLen -= aac.IndexDeltaLength
  230. if auIndexDelta != 0 {
  231. return nil, fmt.Errorf("AU-index-delta different than zero is not supported")
  232. }
  233. }
  234. dataLens[i] = dataLen
  235. i++
  236. }
  237. return dataLens, nil
  238. }