Skip to content

Commit 851c8b0

Browse files
asticodeSean-Der
authored andcommitted
Fixed ffmpeg-send memory leak + glitches
1 parent f07d02b commit 851c8b0

File tree

1 file changed

+10
-3
lines changed

1 file changed

+10
-3
lines changed

ffmpeg-send/main.go

+10-3
Original file line numberDiff line numberDiff line change
@@ -106,16 +106,18 @@ var (
106106
err error
107107
)
108108

109-
const h264FrameDuration = time.Millisecond * 20
110-
111109
func writeH264ToTrack(track *webrtc.TrackLocalStaticSample) {
112110
astiav.RegisterAllDevices()
113111

114112
initTestSrc()
115113
defer freeVideoCoding()
116114

115+
h264FrameDuration := time.Duration(float64(time.Second) / videoStream.AvgFrameRate().Float64())
116+
117117
ticker := time.NewTicker(h264FrameDuration)
118118
for ; true; <-ticker.C {
119+
decodePacket.Unref()
120+
119121
// Read frame from lavfi
120122
if err = inputFormatContext.ReadFrame(decodePacket); err != nil {
121123
if errors.Is(err, astiav.ErrEof) {
@@ -236,7 +238,12 @@ func initVideoEncoding() {
236238
encodeCodecContext.SetWidth(decodeCodecContext.Width())
237239
encodeCodecContext.SetHeight(decodeCodecContext.Height())
238240

239-
if err = encodeCodecContext.Open(h264Encoder, nil); err != nil {
241+
encodeCodecContextDictionary := astiav.NewDictionary()
242+
if err = encodeCodecContextDictionary.Set("bf", "0", astiav.NewDictionaryFlags()); err != nil {
243+
panic(err)
244+
}
245+
246+
if err = encodeCodecContext.Open(h264Encoder, encodeCodecContextDictionary); err != nil {
240247
panic(err)
241248
}
242249

0 commit comments

Comments
 (0)