package playback import ( "context" "errors" "fmt" ) func runSyncAttempt( ctx context.Context, factory SyncReaderFactory, videoSink VideoSink, audioSink AudioSink, videoConfig FeedConfig, audioConfig FeedConfig, ) (resultErr error) { videoCtx := withVideoSource(ctx, videoConfig) reader, err := factory.OpenSync( ctx, videoConfig, audioConfig, ) if err != nil { return fmt.Errorf("open sync group: %w", err) } defer func() { if closeErr := reader.Close(); closeErr != nil { closeErr = fmt.Errorf("close sync group: %w", closeErr) resultErr = errors.Join(resultErr, closeErr) } }() for { frame, err := reader.ReadSync(ctx) if err != nil { if ctx.Err() != nil { return ctx.Err() } return fmt.Errorf("read sync group: %w", err) } if err := videoSink.ConsumeVideo(videoCtx, frame.Video); err != nil { if ctx.Err() != nil { return ctx.Err() } return &videoSinkError{err: err} } if err := audioSink.ConsumeAudio(ctx, frame.Audio); err != nil { if ctx.Err() != nil { return ctx.Err() } return &audioSinkError{err: err} } } }