Skip to content

Commit

Permalink
Save-to-webm: do not force starting ts to 0
Browse files Browse the repository at this point in the history
Instead we use the sample.Duration (as before) for the first packet
only, subsequent packets will be computed as a delta.
  • Loading branch information
tmatth committed Nov 18, 2022
1 parent aca9a01 commit c827071
Showing 1 changed file with 20 additions and 12 deletions.
32 changes: 20 additions & 12 deletions save-to-webm/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -68,16 +68,20 @@ func (s *webmSaver) PushOpus(rtpPacket *rtp.Packet) {
return
}
if s.audioWriter != nil {
var ts int64
if !s.hasStartAudioOffset {
s.startAudioOffset = sample.PacketTimestamp
s.hasStartAudioOffset = true
ts = int64(sample.Duration / time.Millisecond)
} else {
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)
}
ts = timestampSinceStart / 48 // convert from RTPTime to sample time
}
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, timestampSinceStart/48, sample.Data); err != nil {
if _, err := s.audioWriter.Write(true, ts, sample.Data); err != nil {
panic(err)
}
}
Expand Down Expand Up @@ -105,16 +109,20 @@ func (s *webmSaver) PushVP8(rtpPacket *rtp.Packet) {
}
}
if s.videoWriter != nil {
var ts int64
if !s.hasStartVideoOffset {
s.startVideoOffset = sample.PacketTimestamp
s.hasStartVideoOffset = true
ts = int64(sample.Duration / time.Millisecond)
} else {
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)
}
ts = timestampSinceStart / 90 // convert from RTP time to sample time
}
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, timestampSinceStart/90, sample.Data); err != nil {
if _, err := s.videoWriter.Write(videoKeyframe, ts, sample.Data); err != nil {
panic(err)
}
}
Expand Down

0 comments on commit c827071

Please sign in to comment.