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

playlist upload backoff #779

Merged
merged 3 commits into from
Sep 24, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 16 additions & 2 deletions pkg/pipeline/sink/segments.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ type SegmentSink struct {
conf *config.PipelineConfig
callbacks *gstreamer.Callbacks

segmentCount int
playlist m3u8.PlaylistWriter
livePlaylist m3u8.PlaylistWriter

Expand Down Expand Up @@ -185,9 +186,14 @@ func (s *SegmentSink) handlePlaylistUpdates(update SegmentUpdate) error {
if err := s.playlist.Append(segmentStartTime, duration, update.filename); err != nil {
return err
}
if err := s.uploadPlaylist(); err != nil {
s.callbacks.OnError(err)

s.segmentCount++
if s.shouldUploadPlaylist() {
if err := s.uploadPlaylist(); err != nil {
s.callbacks.OnError(err)
}
}

if s.livePlaylist != nil {
if err := s.livePlaylist.Append(segmentStartTime, duration, update.filename); err != nil {
return err
Expand All @@ -200,6 +206,14 @@ func (s *SegmentSink) handlePlaylistUpdates(update SegmentUpdate) error {
return nil
}

// Each segment adds about 100 bytes in the playlist, and long playlists can get very large.
// Uploads every N segments, where N is the number of hours, with a minimum frequency of once per minute
func (s *SegmentSink) shouldUploadPlaylist() bool {
segmentsPerHour := 3600 / s.SegmentDuration
frequency := min(s.segmentCount/segmentsPerHour, segmentsPerHour/60)
return s.segmentCount < segmentsPerHour || s.segmentCount%frequency == 0
}

func (s *SegmentSink) UpdateStartDate(t time.Time) {
s.segmentLock.Lock()
defer s.segmentLock.Unlock()
Expand Down
Loading