82 lines
1.3 KiB
Go
82 lines
1.3 KiB
Go
package playback
|
|
|
|
import (
|
|
"context"
|
|
"fmt"
|
|
"time"
|
|
)
|
|
|
|
type Unit uint8
|
|
|
|
const (
|
|
UnitVideo Unit = iota
|
|
UnitAudio
|
|
UnitSync
|
|
)
|
|
|
|
type State uint8
|
|
|
|
const (
|
|
StateIdle State = iota
|
|
StateConnecting
|
|
StatePlaying
|
|
StateReconnecting
|
|
StateFailed
|
|
StateStopping
|
|
)
|
|
|
|
type Status struct {
|
|
Unit Unit
|
|
State State
|
|
Generation uint64
|
|
Attempt int
|
|
FailedAttempts int
|
|
RetryIn time.Duration
|
|
Err error
|
|
}
|
|
|
|
type StatusObserver func(Status)
|
|
|
|
type generationContextKey struct{}
|
|
|
|
func withGeneration(ctx context.Context, generation uint64) context.Context {
|
|
return context.WithValue(ctx, generationContextKey{}, generation)
|
|
}
|
|
|
|
func generationFromContext(ctx context.Context) uint64 {
|
|
generation, _ := ctx.Value(generationContextKey{}).(uint64)
|
|
return generation
|
|
}
|
|
|
|
func (u Unit) String() string {
|
|
switch u {
|
|
case UnitVideo:
|
|
return "video"
|
|
case UnitAudio:
|
|
return "audio"
|
|
case UnitSync:
|
|
return "sync"
|
|
default:
|
|
return fmt.Sprintf("Unit(%d)", uint8(u))
|
|
}
|
|
}
|
|
|
|
func (s State) String() string {
|
|
switch s {
|
|
case StateIdle:
|
|
return "idle"
|
|
case StateConnecting:
|
|
return "connecting"
|
|
case StatePlaying:
|
|
return "playing"
|
|
case StateReconnecting:
|
|
return "reconnecting"
|
|
case StateFailed:
|
|
return "failed"
|
|
case StateStopping:
|
|
return "stopping"
|
|
default:
|
|
return fmt.Sprintf("State(%d)", uint8(s))
|
|
}
|
|
}
|