From 632aa57a209f15a93dd114a9306e2909d767fb94 Mon Sep 17 00:00:00 2001 From: AfonsoVi Date: Sat, 27 Jul 2024 10:58:45 +0100 Subject: [PATCH] Change time.Sleep to time.Ticker in the examples --- examples/insertable-streams/main.go | 9 ++++++--- 1 file changed, 6 insertions(+), 3 deletions(-) diff --git a/examples/insertable-streams/main.go b/examples/insertable-streams/main.go index 97d3eb2f17f..65a43272925 100644 --- a/examples/insertable-streams/main.go +++ b/examples/insertable-streams/main.go @@ -84,8 +84,12 @@ func main() { // Send our video file frame at a time. Pace our sending so we send it at the same speed it should be played back as. // This isn't required since the video is timestamped, but we will such much higher loss if we send all at once. - sleepTime := time.Millisecond * time.Duration((float32(header.TimebaseNumerator)/float32(header.TimebaseDenominator))*1000) - for { + // + // It is important to use a time.Ticker instead of time.Sleep because + // * avoids accumulating skew, just calling time.Sleep didn't compensate for the time spent parsing the data + // * works around latency issues with Sleep (see https://github.com/golang/go/issues/44343) + ticker := time.NewTicker(time.Millisecond * time.Duration((float32(header.TimebaseNumerator)/float32(header.TimebaseDenominator))*1000)) + for ; true; <-ticker.C { frame, _, ivfErr := ivf.ParseNextFrame() if errors.Is(ivfErr, io.EOF) { fmt.Printf("All frames parsed and sent") @@ -101,7 +105,6 @@ func main() { frame[i] ^= cipherKey } - time.Sleep(sleepTime) if ivfErr = videoTrack.WriteSample(media.Sample{Data: frame, Duration: time.Second}); ivfErr != nil { panic(ivfErr) }