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

Drain output channel on EOS #183

Merged
merged 1 commit into from
Dec 20, 2023
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
30 changes: 25 additions & 5 deletions pkg/media/output.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,8 @@ type Output struct {
outputSync *utils.TrackOutputSynchronizer

samples chan *sample
fuse core.Fuse
closed core.Fuse
flushed core.Fuse
}

type sample struct {
Expand Down Expand Up @@ -351,7 +352,8 @@ func newOutput(outputSync *utils.TrackOutputSynchronizer) (*Output, error) {
sink: sink,
outputSync: outputSync,
samples: make(chan *sample, 15),
fuse: core.NewFuse(),
closed: core.NewFuse(),
flushed: core.NewFuse(),
}

return e, nil
Expand Down Expand Up @@ -388,9 +390,26 @@ func (e *Output) ForceKeyFrame() error {
func (e *Output) handleEOS(_ *app.Sink) {
e.logger.Infow("app sink EOS")

e.Flush()
e.Close()
}

func (e *Output) Flush() {
select {
case e.samples <- nil:
case <-e.closed.Watch():
case <-time.After(time.Second):
e.logger.Errorw("output pipeline flush timed out", errors.New("could not enqueue EOS marker"))
}

select {
case <-e.flushed.Watch():
case <-e.closed.Watch():
case <-time.After(5 * time.Second):
e.logger.Errorw("output pipeline flush timed out", errors.New("flush timed out"))
}
}

func (e *Output) writeSample(s *media.Sample, pts time.Duration) error {

// Synchronize the outputs before the network jitter buffer to avoid old samples stuck
Expand All @@ -407,7 +426,7 @@ func (e *Output) writeSample(s *media.Sample, pts time.Duration) error {
select {
case e.samples <- &sample{s, pts}:
return nil
case <-e.fuse.Watch():
case <-e.closed.Watch():
return io.EOF
default:
// drop the sample if the output queue is full. This is needed if we are reconnecting.
Expand All @@ -421,11 +440,12 @@ func (e *Output) NextSample(ctx context.Context) (media.Sample, error) {
for {
select {
case s = <-e.samples:
case <-e.fuse.Watch():
case <-e.closed.Watch():
case <-ctx.Done():
}

if s == nil {
e.flushed.Break()
return media.Sample{}, io.EOF
}

Expand All @@ -447,7 +467,7 @@ func (e *Output) OnUnbind() error {

func (e *Output) Close() error {

e.fuse.Break()
e.closed.Break()
e.outputSync.Close()

return nil
Expand Down
Loading