mirror of
https://github.com/Monibuca/engine.git
synced 2025-12-24 13:18:07 +08:00
refactor: type =
This commit is contained in:
@@ -21,7 +21,7 @@ func SplitAnnexB[T ~[]byte](frame T, process func(T), delimiter []byte) {
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
type LIRTP = util.ListItem[RTPFrame]
|
||||
type RTPFrame struct {
|
||||
*rtp.Packet
|
||||
Raw []byte
|
||||
@@ -122,11 +122,11 @@ type AVFrame struct {
|
||||
IFrame bool
|
||||
PTS time.Duration
|
||||
DTS time.Duration
|
||||
Timestamp time.Duration // 绝对时间戳
|
||||
ADTS *util.ListItem[util.Buffer] `json:"-" yaml:"-"` // ADTS头
|
||||
AVCC util.BLL `json:"-" yaml:"-"` // 打包好的AVCC格式(MPEG-4格式、Byte-Stream Format)
|
||||
RTP util.List[RTPFrame] `json:"-" yaml:"-"`
|
||||
AUList util.BLLs `json:"-" yaml:"-"` // 裸数据
|
||||
Timestamp time.Duration // 绝对时间戳
|
||||
ADTS util.LIBP `json:"-" yaml:"-"` // ADTS头
|
||||
AVCC util.BLL `json:"-" yaml:"-"` // 打包好的AVCC格式(MPEG-4格式、Byte-Stream Format)
|
||||
RTP util.List[RTPFrame] `json:"-" yaml:"-"`
|
||||
AUList util.BLLs `json:"-" yaml:"-"` // 裸数据
|
||||
}
|
||||
|
||||
func NewAVFrame() *AVFrame {
|
||||
@@ -164,7 +164,7 @@ func (av *AVFrame) Reset() {
|
||||
av.DataFrame.Reset()
|
||||
}
|
||||
|
||||
func (av *AVFrame) Assign(source *AVFrame) {
|
||||
func (av *AVFrame) Assign(source *AVFrame) {
|
||||
av.IFrame = source.IFrame
|
||||
av.PTS = source.PTS
|
||||
av.DTS = source.DTS
|
||||
|
||||
@@ -64,13 +64,13 @@ type AVTrack interface {
|
||||
Attach()
|
||||
Detach()
|
||||
WriteAVCC(ts uint32, frame *util.BLL) error //写入AVCC格式的数据
|
||||
WriteRTP(*util.ListItem[RTPFrame])
|
||||
WriteRTP(*LIRTP)
|
||||
WriteRTPPack(*rtp.Packet)
|
||||
WriteSequenceHead(sh []byte) error
|
||||
Flush()
|
||||
SetSpeedLimit(time.Duration)
|
||||
GetRTPFromPool() *util.ListItem[RTPFrame]
|
||||
GetFromPool(util.IBytes) *util.ListItem[util.Buffer]
|
||||
GetRTPFromPool() *LIRTP
|
||||
GetFromPool(util.IBytes) util.LIBP
|
||||
}
|
||||
type VideoTrack interface {
|
||||
AVTrack
|
||||
|
||||
@@ -69,7 +69,7 @@ func (aac *AAC) WriteADTS(ts uint32, b util.IBytes) {
|
||||
}
|
||||
|
||||
// https://datatracker.ietf.org/doc/html/rfc3640#section-3.2.1
|
||||
func (aac *AAC) WriteRTPFrame(rtpItem *util.ListItem[RTPFrame]) {
|
||||
func (aac *AAC) WriteRTPFrame(rtpItem *LIRTP) {
|
||||
aac.Value.RTP.Push(rtpItem)
|
||||
frame := &rtpItem.Value
|
||||
if len(frame.Payload) < 2 {
|
||||
|
||||
@@ -48,7 +48,7 @@ func (vt *AV1) WriteSequenceHead(head []byte) (err error) {
|
||||
return
|
||||
}
|
||||
|
||||
func (vt *AV1) WriteRTPFrame(rtpItem *util.ListItem[RTPFrame]) {
|
||||
func (vt *AV1) WriteRTPFrame(rtpItem *LIRTP) {
|
||||
defer func() {
|
||||
err := recover()
|
||||
if err != nil {
|
||||
|
||||
@@ -99,7 +99,7 @@ func NewRecycleDataTrack[T util.Recyclable](name string) (dt *RecycleData[T]) {
|
||||
}
|
||||
|
||||
type BytesData struct {
|
||||
RecycleData[*util.ListItem[util.Buffer]]
|
||||
RecycleData[util.LIBP]
|
||||
Pool util.BytesPool
|
||||
}
|
||||
|
||||
|
||||
@@ -57,7 +57,7 @@ func (g711 *G711) WriteAVCC(ts uint32, frame *util.BLL) error {
|
||||
return nil
|
||||
}
|
||||
|
||||
func (g711 *G711) WriteRTPFrame(rtpItem *util.ListItem[RTPFrame]) {
|
||||
func (g711 *G711) WriteRTPFrame(rtpItem *LIRTP) {
|
||||
frame := &rtpItem.Value
|
||||
g711.Value.RTP.Push(rtpItem)
|
||||
if g711.SampleRate != 90000 {
|
||||
|
||||
@@ -110,7 +110,7 @@ func (vt *H264) WriteSequenceHead(head []byte) (err error) {
|
||||
return
|
||||
}
|
||||
|
||||
func (vt *H264) WriteRTPFrame(rtpItem *util.ListItem[RTPFrame]) {
|
||||
func (vt *H264) WriteRTPFrame(rtpItem *LIRTP) {
|
||||
defer func() {
|
||||
err := recover()
|
||||
if err != nil {
|
||||
@@ -223,7 +223,7 @@ func (vt *H264) CompleteRTP(value *AVFrame) {
|
||||
vt.PacketizeRTP(out...)
|
||||
}
|
||||
|
||||
func (vt *H264) GetNALU_SEI() (item *util.ListItem[util.Buffer]) {
|
||||
func (vt *H264) GetNALU_SEI() (item util.LIBP) {
|
||||
item = vt.BytesPool.Get(1)
|
||||
item.Value[0] = byte(codec.NALU_SEI)
|
||||
return
|
||||
|
||||
@@ -97,7 +97,7 @@ func (vt *H265) WriteSequenceHead(head []byte) (err error) {
|
||||
return
|
||||
}
|
||||
|
||||
func (vt *H265) WriteRTPFrame(rtpItem *util.ListItem[RTPFrame]) {
|
||||
func (vt *H265) WriteRTPFrame(rtpItem *LIRTP) {
|
||||
defer func() {
|
||||
err := recover()
|
||||
if err != nil {
|
||||
@@ -218,7 +218,7 @@ func (vt *H265) CompleteRTP(value *AVFrame) {
|
||||
vt.PacketizeRTP(out...)
|
||||
}
|
||||
|
||||
func (vt *H265) GetNALU_SEI() (item *util.ListItem[util.Buffer]) {
|
||||
func (vt *H265) GetNALU_SEI() (item util.LIBP) {
|
||||
item = vt.BytesPool.Get(1)
|
||||
item.Value[0] = 0b10000000 | byte(codec.NAL_UNIT_SEI<<1)
|
||||
return
|
||||
|
||||
@@ -63,11 +63,11 @@ type SpesificTrack interface {
|
||||
CompleteRTP(*AVFrame)
|
||||
CompleteAVCC(*AVFrame)
|
||||
WriteSliceBytes([]byte)
|
||||
WriteRTPFrame(*util.ListItem[RTPFrame])
|
||||
WriteRTPFrame(*LIRTP)
|
||||
generateTimestamp(uint32)
|
||||
WriteSequenceHead([]byte) error
|
||||
writeAVCCFrame(uint32, *util.BLLReader, *util.BLL) error
|
||||
GetNALU_SEI() *util.ListItem[util.Buffer]
|
||||
GetNALU_SEI() util.LIBP
|
||||
Flush()
|
||||
}
|
||||
|
||||
@@ -106,7 +106,7 @@ type Media struct {
|
||||
流速控制
|
||||
}
|
||||
|
||||
func (av *Media) GetFromPool(b util.IBytes) (item *util.ListItem[util.Buffer]) {
|
||||
func (av *Media) GetFromPool(b util.IBytes) (item util.LIBP) {
|
||||
if b.Reuse() {
|
||||
item = av.BytesPool.Get(b.Len())
|
||||
copy(item.Value, b.Bytes())
|
||||
@@ -116,7 +116,7 @@ func (av *Media) GetFromPool(b util.IBytes) (item *util.ListItem[util.Buffer]) {
|
||||
return
|
||||
}
|
||||
|
||||
func (av *Media) GetRTPFromPool() (result *util.ListItem[RTPFrame]) {
|
||||
func (av *Media) GetRTPFromPool() (result *LIRTP) {
|
||||
result = av.RtpPool.Get()
|
||||
if result.Value.Packet == nil {
|
||||
result.Value.Packet = &rtp.Packet{}
|
||||
|
||||
@@ -30,7 +30,7 @@ func (opus *Opus) WriteAVCC(ts uint32, frame *util.BLL) error {
|
||||
return errors.New("opus not support WriteAVCC")
|
||||
}
|
||||
|
||||
func (opus *Opus) WriteRTPFrame(rtpItem *util.ListItem[RTPFrame]) {
|
||||
func (opus *Opus) WriteRTPFrame(rtpItem *LIRTP) {
|
||||
frame := &rtpItem.Value
|
||||
opus.Value.RTP.Push(rtpItem)
|
||||
if opus.SampleRate != 90000 {
|
||||
|
||||
10
track/rtp.go
10
track/rtp.go
@@ -28,7 +28,7 @@ func (av *Media) WriteRTPPack(p *rtp.Packet) {
|
||||
}
|
||||
|
||||
// WriteRTPFrame 写入未反序列化的RTP包, 未排序的
|
||||
func (av *Media) WriteRTP(raw *util.ListItem[RTPFrame]) {
|
||||
func (av *Media) WriteRTP(raw *LIRTP) {
|
||||
for frame := av.recorderRTP(raw); frame != nil; frame = av.nextRTPFrame() {
|
||||
frame.Value.SSRC = av.SSRC
|
||||
av.Value.BytesIn += len(frame.Value.Payload) + 12
|
||||
@@ -46,7 +46,7 @@ func (av *Media) WriteRTP(raw *util.ListItem[RTPFrame]) {
|
||||
// https://www.cnblogs.com/moonwalk/p/15903760.html
|
||||
// Packetize packetizes the payload of an RTP packet and returns one or more RTP packets
|
||||
func (av *Media) PacketizeRTP(payloads ...[][]byte) {
|
||||
var rtpItem *util.ListItem[RTPFrame]
|
||||
var rtpItem *LIRTP
|
||||
for _, pp := range payloads {
|
||||
rtpItem = av.GetRTPFromPool()
|
||||
packet := &rtpItem.Value
|
||||
@@ -78,11 +78,11 @@ func (av *Media) PacketizeRTP(payloads ...[][]byte) {
|
||||
type RTPDemuxer struct {
|
||||
lastSeq uint16 //上一个rtp包的序号
|
||||
lastSeq2 uint16 //上上一个rtp包的序号
|
||||
乱序重排 util.RTPReorder[*util.ListItem[RTPFrame]]
|
||||
乱序重排 util.RTPReorder[*LIRTP]
|
||||
}
|
||||
|
||||
// 获取缓存中下一个rtpFrame
|
||||
func (av *RTPDemuxer) nextRTPFrame() (frame *util.ListItem[RTPFrame]) {
|
||||
func (av *RTPDemuxer) nextRTPFrame() (frame *LIRTP) {
|
||||
frame = av.乱序重排.Pop()
|
||||
if frame == nil {
|
||||
return
|
||||
@@ -93,7 +93,7 @@ func (av *RTPDemuxer) nextRTPFrame() (frame *util.ListItem[RTPFrame]) {
|
||||
}
|
||||
|
||||
// 对RTP包乱序重排
|
||||
func (av *RTPDemuxer) recorderRTP(item *util.ListItem[RTPFrame]) (frame *util.ListItem[RTPFrame]) {
|
||||
func (av *RTPDemuxer) recorderRTP(item *LIRTP) (frame *LIRTP) {
|
||||
frame = av.乱序重排.Push(item.Value.SequenceNumber, item)
|
||||
if frame == nil {
|
||||
return
|
||||
|
||||
25
util/pool.go
25
util/pool.go
@@ -11,12 +11,12 @@ type Recyclable interface {
|
||||
Recycle()
|
||||
}
|
||||
type BLLReader struct {
|
||||
*ListItem[Buffer]
|
||||
LIBP
|
||||
pos int
|
||||
}
|
||||
|
||||
func (r *BLLReader) CanRead() bool {
|
||||
return r.ListItem != nil && !r.IsRoot()
|
||||
return r.LIBP != nil && !r.IsRoot()
|
||||
}
|
||||
|
||||
func (r *BLLReader) Skip(n int) (err error) {
|
||||
@@ -27,7 +27,7 @@ func (r *BLLReader) Skip(n int) (err error) {
|
||||
return
|
||||
}
|
||||
n -= l
|
||||
r.ListItem = r.Next
|
||||
r.LIBP = r.Next
|
||||
r.pos = 0
|
||||
}
|
||||
return io.EOF
|
||||
@@ -41,7 +41,7 @@ func (r *BLLReader) ReadByte() (b byte, err error) {
|
||||
r.pos++
|
||||
return
|
||||
}
|
||||
r.ListItem = r.Next
|
||||
r.LIBP = r.Next
|
||||
r.pos = 0
|
||||
}
|
||||
return 0, io.EOF
|
||||
@@ -88,7 +88,7 @@ func (r *BLLReader) ReadN(n int) (result net.Buffers) {
|
||||
}
|
||||
result = append(result, r.Value[r.pos:])
|
||||
n -= l
|
||||
r.ListItem = r.Next
|
||||
r.LIBP = r.Next
|
||||
r.pos = 0
|
||||
}
|
||||
return
|
||||
@@ -105,7 +105,7 @@ func (r *BLLReader) WriteNTo(n int, result *net.Buffers) (actual int) {
|
||||
}
|
||||
*result = append(*result, r.Value[r.pos:])
|
||||
n -= l
|
||||
r.ListItem = r.Next
|
||||
r.LIBP = r.Next
|
||||
r.pos = 0
|
||||
}
|
||||
return actual - n
|
||||
@@ -152,7 +152,7 @@ func (list *BLLs) PushValue(item *BLL) {
|
||||
list.ByteLength += item.ByteLength
|
||||
}
|
||||
|
||||
func (list *BLLs) Push(item *ListItem[Buffer]) {
|
||||
func (list *BLLs) Push(item LIBP) {
|
||||
if list == nil {
|
||||
return
|
||||
}
|
||||
@@ -220,7 +220,7 @@ func (list *BLL) NewReader() *BLLReader {
|
||||
// list.ByteLength += list2.ByteLength
|
||||
// }
|
||||
|
||||
func (list *BLL) Push(item *ListItem[Buffer]) {
|
||||
func (list *BLL) Push(item LIBP) {
|
||||
if list == nil {
|
||||
return
|
||||
}
|
||||
@@ -228,7 +228,7 @@ func (list *BLL) Push(item *ListItem[Buffer]) {
|
||||
list.ByteLength += item.Value.Len()
|
||||
}
|
||||
|
||||
func (list *BLL) Shift() (item *ListItem[Buffer]) {
|
||||
func (list *BLL) Shift() (item LIBP) {
|
||||
if list == nil || list.Length == 0 {
|
||||
return
|
||||
}
|
||||
@@ -304,7 +304,7 @@ func (list *BLL) GetUintN(index int, n int) (result uint32) {
|
||||
type BytesPool []List[Buffer]
|
||||
|
||||
// 获取来自真实内存的切片的——假内存块,即只回收外壳
|
||||
func (p BytesPool) GetShell(b []byte) (item *ListItem[Buffer]) {
|
||||
func (p BytesPool) GetShell(b []byte) (item LIBP) {
|
||||
if len(p) == 0 {
|
||||
return &ListItem[Buffer]{Value: b}
|
||||
}
|
||||
@@ -314,7 +314,7 @@ func (p BytesPool) GetShell(b []byte) (item *ListItem[Buffer]) {
|
||||
return
|
||||
}
|
||||
|
||||
func (p BytesPool) Get(size int) (item *ListItem[Buffer]) {
|
||||
func (p BytesPool) Get(size int) (item LIBP) {
|
||||
for i := 1; i < len(p); i++ {
|
||||
if level := 1 << i; level >= size {
|
||||
if item = p[i].PoolShift(); cap(item.Value) > 0 {
|
||||
@@ -340,3 +340,6 @@ type Pool[T any] List[T]
|
||||
func (p *Pool[T]) Get() (item *ListItem[T]) {
|
||||
return (*List[T])(p).PoolShift()
|
||||
}
|
||||
|
||||
type LIB = ListItem[Buffer]
|
||||
type LIBP = *LIB
|
||||
Reference in New Issue
Block a user