opus.go 1.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354
  1. package track
  2. import (
  3. "github.com/pkg/errors"
  4. "m7s.live/engine/v4/codec"
  5. . "m7s.live/engine/v4/common"
  6. "m7s.live/engine/v4/util"
  7. )
  8. var _ SpesificTrack = (*Opus)(nil)
  9. func NewOpus(puber IPuber, stuff ...any) (opus *Opus) {
  10. opus = &Opus{}
  11. opus.CodecID = codec.CodecID_OPUS
  12. opus.SampleSize = 16
  13. opus.Channels = 2
  14. opus.AVCCHead = []byte{(byte(opus.CodecID) << 4) | (1 << 1)}
  15. opus.SetStuff("opus", uint32(48000), byte(111), opus, stuff, puber)
  16. if opus.BytesPool == nil {
  17. opus.BytesPool = make(util.BytesPool, 17)
  18. }
  19. return
  20. }
  21. type Opus struct {
  22. Audio
  23. }
  24. func (opus *Opus) WriteAVCC(ts uint32, frame *util.BLL) error {
  25. return errors.New("opus not support WriteAVCC")
  26. }
  27. func (opus *Opus) WriteRTPFrame(rtpItem *util.ListItem[RTPFrame]) {
  28. frame := &rtpItem.Value
  29. opus.Value.RTP.Push(rtpItem)
  30. if opus.SampleRate != 90000 {
  31. opus.generateTimestamp(uint32(uint64(frame.Timestamp) * 90000 / uint64(opus.SampleRate)))
  32. }
  33. opus.AppendAuBytes(frame.Payload)
  34. opus.Flush()
  35. }
  36. func (opus *Opus) CompleteRTP(value *AVFrame) {
  37. if value.AUList.ByteLength > RTPMTU {
  38. var packets [][][]byte
  39. r := value.AUList.Next.Value.NewReader()
  40. for bufs := r.ReadN(RTPMTU); len(bufs) > 0; bufs = r.ReadN(RTPMTU) {
  41. packets = append(packets, bufs)
  42. }
  43. opus.PacketizeRTP(packets...)
  44. } else {
  45. opus.Audio.CompleteRTP(value)
  46. }
  47. }