Skip to content

Commit

Permalink
Change time.Sleep to time.Ticker in the examples
Browse files Browse the repository at this point in the history
  • Loading branch information
AfonsoVilalonga authored and Sean-Der committed Jul 29, 2024
1 parent 021c271 commit 35b3ae1
Showing 1 changed file with 6 additions and 3 deletions.
9 changes: 6 additions & 3 deletions examples/insertable-streams/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand All @@ -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)
}
Expand Down

0 comments on commit 35b3ae1

Please sign in to comment.