aac.go 7.3 KB

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