From 4870830a6c07473f24f970223e5427ff6c0168f1 Mon Sep 17 00:00:00 2001 From: ydajiang Date: Sun, 27 Jul 2025 15:24:00 +0800 Subject: [PATCH] =?UTF-8?q?feat:=20rtc=E6=94=AF=E6=8C=81=E5=85=B3=E9=94=AE?= =?UTF-8?q?=E5=B8=A7=E7=BC=93=E5=AD=98?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- rtc/rtc_stream.go | 34 ++++++++++++++++++++++++++++------ 1 file changed, 28 insertions(+), 6 deletions(-) diff --git a/rtc/rtc_stream.go b/rtc/rtc_stream.go index 00ed118..db8c60e 100644 --- a/rtc/rtc_stream.go +++ b/rtc/rtc_stream.go @@ -28,20 +28,42 @@ var ( type transStream struct { 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() + var data *collections.ReferenceCounter[[]byte] if utils.AVMediaTypeAudio == packet.MediaType { - t.AppendOutStreamBuffer(collections.NewReferenceCounter(packet.Data)) + data = collections.NewReferenceCounter(packet.Data) } else if utils.AVMediaTypeVideo == packet.MediaType { - avStream := t.FindTrackWithStreamIndex(packet.Index).Stream - data := avformat.AVCCPacket2AnnexB(avStream, packet) - t.AppendOutStreamBuffer(collections.NewReferenceCounter(data)) + annexBData := avformat.AVCCPacket2AnnexB(t.FindTrackWithStreamIndex(packet.Index).Stream, packet) + data = collections.NewReferenceCounter(annexBData) } - 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 {