Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Save-to-webm: use timestamp from samplebuilder #130

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
40 changes: 33 additions & 7 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 @@ -32,9 +33,10 @@ func main() {
}

type webmSaver struct {
audioWriter, videoWriter webm.BlockWriteCloser
audioBuilder, videoBuilder *samplebuilder.SampleBuilder
audioTimestamp, videoTimestamp time.Duration
audioWriter, videoWriter webm.BlockWriteCloser
audioBuilder, videoBuilder *samplebuilder.SampleBuilder
hasStartAudioOffset, hasStartVideoOffset bool
startAudioOffset, startVideoOffset uint32
}

func newWebmSaver() *webmSaver {
Expand Down Expand Up @@ -66,8 +68,20 @@ func (s *webmSaver) PushOpus(rtpPacket *rtp.Packet) {
return
}
if s.audioWriter != nil {
s.audioTimestamp += sample.Duration
if _, err := s.audioWriter.Write(true, int64(s.audioTimestamp/time.Millisecond), sample.Data); err != nil {
var ts int64
if !s.hasStartAudioOffset {
s.startAudioOffset = sample.PacketTimestamp
tmatth marked this conversation as resolved.
Show resolved Hide resolved
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
Comment on lines +77 to +82
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Does it work after the second overflow of packet timestamp?

}
if _, err := s.audioWriter.Write(true, ts, sample.Data); err != nil {
panic(err)
}
}
Expand Down Expand Up @@ -95,8 +109,20 @@ func (s *webmSaver) PushVP8(rtpPacket *rtp.Packet) {
}
}
if s.videoWriter != nil {
s.videoTimestamp += sample.Duration
if _, err := s.videoWriter.Write(videoKeyframe, int64(s.videoTimestamp/time.Millisecond), sample.Data); err != 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
}
if _, err := s.videoWriter.Write(videoKeyframe, ts, sample.Data); err != nil {
panic(err)
}
}
Expand Down