add synchronized playback attempt

This commit is contained in:
Dmitry Sergeev
2026-08-31 08:27:37 +03:00
parent 9b6607fc48
commit 05f356fa10
2 changed files with 297 additions and 0 deletions
+56
View File
@@ -0,0 +1,56 @@
package playback
import (
"context"
"errors"
"fmt"
)
func runSyncAttempt(
ctx context.Context,
factory SyncReaderFactory,
videoSink VideoSink,
audioSink AudioSink,
videoConfig FeedConfig,
audioConfig FeedConfig,
) (resultErr error) {
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(ctx, 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}
}
}
}