fix random timestamp offset in examples (#628)

This commit is contained in:
Alessandro Ros
2024-10-07 17:01:27 +02:00
committed by GitHub
parent 2ca0bffa20
commit 53b2d2c6bf
2 changed files with 32 additions and 2 deletions

View File

@@ -2,6 +2,7 @@ package main
import (
"bytes"
"crypto/rand"
"image"
"image/color"
"image/jpeg"
@@ -25,6 +26,15 @@ func multiplyAndDivide(v, m, d int64) int64 {
return (secs*m + dec*m/d)
}
func randUint32() (uint32, error) {
var b [4]byte
_, err := rand.Read(b[:])
if err != nil {
return 0, err
}
return uint32(b[0])<<24 | uint32(b[1])<<16 | uint32(b[2])<<8 | uint32(b[3]), nil
}
func createRandomImage(i int) *image.RGBA {
img := image.NewRGBA(image.Rect(0, 0, 640, 480))
@@ -73,6 +83,11 @@ func main() {
start := time.Now()
randomStart, err := randUint32()
if err != nil {
panic(err)
}
// setup a ticker to sleep between frames
ticker := time.NewTicker(200 * time.Millisecond)
defer ticker.Stop()
@@ -102,7 +117,7 @@ func main() {
// write packets to the server
for _, pkt := range pkts {
pkt.Timestamp = pts
pkt.Timestamp = randomStart + pts
err = c.WritePacketRTP(desc.Medias[0], pkt)
if err != nil {