Skip to content

Commit

Permalink
Save-to-webm: handle timestamp wrap
Browse files Browse the repository at this point in the history
In the event that the packet timestamp wraps past uint32max, cast to int64
and add uint32_max so that the delta (and thus the webm timestamp) stays
positive.
  • Loading branch information
tmatth committed Nov 12, 2022
1 parent 5bab82e commit ac7d2b9
Showing 1 changed file with 13 additions and 2 deletions.
15 changes: 13 additions & 2 deletions save-to-webm/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package main
import (
"fmt"
"io"
"math"
"os"
"os/signal"
"time"
Expand Down Expand Up @@ -71,7 +72,12 @@ func (s *webmSaver) PushOpus(rtpPacket *rtp.Packet) {
s.startAudioOffset = sample.PacketTimestamp
s.hasStartAudioOffset = true
}
if _, err := s.audioWriter.Write(true, int64((sample.PacketTimestamp-s.startAudioOffset)/48), sample.Data); err != nil {
timestampSinceStart := int64(sample.PacketTimestamp) - int64(s.startAudioOffset)
// handle range where PacketTimestamp has wrapped past uint32 by operating in int64 range until the timestamp has caught up to the offset
if timestampSinceStart < 0 {
timestampSinceStart = int64(sample.PacketTimestamp+math.MaxUint32) - int64(s.startAudioOffset)
}
if _, err := s.audioWriter.Write(true, int64(timestampSinceStart/48), sample.Data); err != nil {
panic(err)
}
}
Expand Down Expand Up @@ -103,7 +109,12 @@ func (s *webmSaver) PushVP8(rtpPacket *rtp.Packet) {
s.startVideoOffset = sample.PacketTimestamp
s.hasStartVideoOffset = true
}
if _, err := s.videoWriter.Write(videoKeyframe, int64((sample.PacketTimestamp-s.startVideoOffset)/90), sample.Data); err != nil {
timestampSinceStart := int64(sample.PacketTimestamp) - int64(s.startVideoOffset)
// handle range where PacketTimestamp has wrapped past uint32 by operating in int64 range until the timestamp has caught up to the offset
if timestampSinceStart < 0 {
timestampSinceStart = int64(sample.PacketTimestamp+math.MaxUint32) - int64(s.startVideoOffset)
}
if _, err := s.videoWriter.Write(videoKeyframe, int64(timestampSinceStart/90), sample.Data); err != nil {
panic(err)
}
}
Expand Down

0 comments on commit ac7d2b9

Please sign in to comment.