diff --git a/pkg/pipeline/sink/segments.go b/pkg/pipeline/sink/segments.go index 727f772a..d000f7d4 100644 --- a/pkg/pipeline/sink/segments.go +++ b/pkg/pipeline/sink/segments.go @@ -45,6 +45,7 @@ type SegmentSink struct { conf *config.PipelineConfig callbacks *gstreamer.Callbacks + segmentCount int playlist m3u8.PlaylistWriter livePlaylist m3u8.PlaylistWriter @@ -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 @@ -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()