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 2 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
32 changes: 25 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,16 @@ 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 {
if !s.hasStartAudioOffset {
s.startAudioOffset = sample.PacketTimestamp
tmatth marked this conversation as resolved.
Show resolved Hide resolved
s.hasStartAudioOffset = true
}
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 {
panic(err)
}
}
Expand Down Expand Up @@ -95,8 +105,16 @@ 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 {
if !s.hasStartVideoOffset {
s.startVideoOffset = sample.PacketTimestamp
s.hasStartVideoOffset = true
}
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 {
panic(err)
}
}
Expand Down