feat: rtc支持关键帧缓存

This commit is contained in:
ydajiang
2025-07-27 15:24:00 +08:00
parent c6aba06199
commit 4870830a6c

View File

@@ -28,20 +28,42 @@ var (
type transStream struct { type transStream struct {
stream.BaseTransStream stream.BaseTransStream
segments []stream.TransStreamSegment
} }
func (t *transStream) Input(packet *avformat.AVPacket, _ int) ([]*collections.ReferenceCounter[[]byte], int64, bool, error) { func (t *transStream) Input(packet *avformat.AVPacket, trackIndex int) ([]*collections.ReferenceCounter[[]byte], int64, bool, error) {
t.ClearOutStreamBuffer() t.ClearOutStreamBuffer()
var data *collections.ReferenceCounter[[]byte]
if utils.AVMediaTypeAudio == packet.MediaType { if utils.AVMediaTypeAudio == packet.MediaType {
t.AppendOutStreamBuffer(collections.NewReferenceCounter(packet.Data)) data = collections.NewReferenceCounter(packet.Data)
} else if utils.AVMediaTypeVideo == packet.MediaType { } else if utils.AVMediaTypeVideo == packet.MediaType {
avStream := t.FindTrackWithStreamIndex(packet.Index).Stream annexBData := avformat.AVCCPacket2AnnexB(t.FindTrackWithStreamIndex(packet.Index).Stream, packet)
data := avformat.AVCCPacket2AnnexB(avStream, packet) data = collections.NewReferenceCounter(annexBData)
t.AppendOutStreamBuffer(collections.NewReferenceCounter(data))
} }
return t.OutBuffer[:t.OutBufferSize], int64(uint32(packet.GetDuration(1000))), utils.AVMediaTypeVideo == packet.MediaType && packet.Key, nil duration := int64(uint32(packet.GetDuration(1000)))
key := utils.AVMediaTypeVideo == packet.MediaType && packet.Key
if t.HasVideo() && stream.AppConfig.GOPCache {
// 遇到视频关键帧, 丢弃前一帧缓存
if key {
t.segments = t.segments[:0]
}
t.segments = append(t.segments, stream.TransStreamSegment{
Data: []*collections.ReferenceCounter[[]byte]{data},
TS: duration,
Key: key,
Index: trackIndex,
})
}
t.AppendOutStreamBuffer(data)
return t.OutBuffer[:t.OutBufferSize], duration, key, nil
}
func (t *transStream) ReadKeyFrameBuffer() ([]stream.TransStreamSegment, error) {
return t.segments, nil
} }
func (t *transStream) WriteHeader() error { func (t *transStream) WriteHeader() error {