diff --git a/examples/client-record-format-h264-from-disk/main.go b/examples/client-record-format-h264-from-disk/main.go index 54c9d920..9ee202fa 100644 --- a/examples/client-record-format-h264-from-disk/main.go +++ b/examples/client-record-format-h264-from-disk/main.go @@ -1,6 +1,7 @@ package main import ( + "crypto/rand" "fmt" "log" "os" @@ -27,6 +28,15 @@ func findTrack(r *mpegts.Reader) (*mpegts.Track, error) { return nil, fmt.Errorf("H264 track not found") } +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 main() { // open a file in MPEG-TS format f, err := os.Open("myvideo.ts") @@ -73,6 +83,11 @@ func main() { panic(err) } + randomStart, err := randUint32() + if err != nil { + panic(err) + } + timeDecoder := mpegts.NewTimeDecoder2() var firstDTS *int64 var startTime time.Time @@ -105,7 +120,7 @@ func main() { // we don't have to perform any conversion // since H264 clock rate is the same in both MPEG-TS and RTSP for _, packet := range packets { - packet.Timestamp = uint32(pts) + packet.Timestamp = randomStart + uint32(pts) } // write packets to the server diff --git a/examples/client-record-format-mjpeg-from-image/main.go b/examples/client-record-format-mjpeg-from-image/main.go index b7133a09..528f88ee 100644 --- a/examples/client-record-format-mjpeg-from-image/main.go +++ b/examples/client-record-format-mjpeg-from-image/main.go @@ -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 {