Documentation
¶
Overview ¶
Package player provides an mpv IPC wrapper for audio playback control.
Index ¶
- Constants
- Variables
- func LaunchCmd(p Player, url string, startTime, duration float64, paused bool, ...) tea.Cmd
- func LaunchGroupCmd(ctx context.Context, group *Group, paths []string, volume int, ...) tea.Cmd
- func MpvSocketDir() (string, error)
- func QuitCmd(p Player) tea.Cmd
- func QuitGroupCmd(group *Group) tea.Cmd
- func SeekCmd(p Player, target float64) tea.Cmd
- func SeekRelativeCmd(p Player, offset float64) tea.Cmd
- func SetAppName(name string)
- func SetGroupPauseCmd(group *Group, paused bool, generation uint64) tea.Cmd
- func SetGroupVolumeCmd(group *Group, volume int, generation uint64) tea.Cmd
- func SetPauseCmd(p Player, paused bool) tea.Cmd
- func SetSpeedCmd(p Player, speed float64) tea.Cmd
- func SetVolumeCmd(p Player, vol int) tea.Cmd
- func TickCmd(p Player, generation uint64) tea.Cmd
- func WatchGroupCmd(ctx context.Context, group *Group, generation uint64) tea.Cmd
- type Group
- type GroupControlMsg
- type GroupEndedMsg
- type GroupReadyMsg
- type GroupStoppedMsg
- type IPCConnection
- type Keys
- type MediaDownloadFunc
- type Model
- func (m Model) HandlesKey(msg tea.KeyMsg) bool
- func (m Model) Init() tea.Cmd
- func (m Model) PlayPauseKey() key.Binding
- func (m Model) SeekBackKey() key.Binding
- func (m Model) SeekForwardKey() key.Binding
- func (m *Model) SetWidth(w int)
- func (m Model) Update(msg tea.Msg) (Model, tea.Cmd)
- func (m Model) View() string
- func (m Model) VolumeDownKey() key.Binding
- func (m Model) VolumeUpKey() key.Binding
- type Mpv
- func (m *Mpv) Connect() error
- func (m *Mpv) Done() <-chan struct{}
- func (m *Mpv) GetDuration() (float64, error)
- func (m *Mpv) GetPaused() (bool, error)
- func (m *Mpv) GetPosition() (float64, error)
- func (m *Mpv) GetVolume() (int, error)
- func (m *Mpv) Launch(url, startTime, socketPath string, paused bool, httpHeaders []string, ...) error
- func (m *Mpv) LaunchLoop(path, socketPath string, paused bool, volume int) error
- func (m *Mpv) Quit() error
- func (m *Mpv) Seek(seconds float64) error
- func (m *Mpv) SeekRelative(seconds float64) error
- func (m *Mpv) SetPause(paused bool) error
- func (m *Mpv) SetSpeed(speed float64) error
- func (m *Mpv) SetVolume(vol int) error
- func (m *Mpv) WatchEvents(generation uint64) tea.Cmd
- type Player
- type PlayerEndMsg
- type PlayerKeyMap
- type PlayerLaunchErrMsg
- type PlayerQuitMsg
- type PlayerReadyMsg
- type PositionMsg
- type ProcessStarter
- type SessionCache
- type StartPlayMsg
Constants ¶
const DefaultMediaCacheLimit int64 = 2 << 30
DefaultMediaCacheLimit bounds temporary media storage to 2 GiB.
const MaxSubliminalTracks = 8
MaxSubliminalTracks prevents accidental unbounded mpv process creation.
Variables ¶
var ErrMediaCacheFull = errors.New("media cache size limit reached")
ErrMediaCacheFull reports that a download would exceed the session limit.
Functions ¶
func LaunchCmd ¶
func LaunchCmd(p Player, url string, startTime, duration float64, paused bool, httpHeaders []string, volume int, generation uint64) tea.Cmd
LaunchCmd spawns mpv and connects via IPC. If paused is true, mpv starts paused. httpHeaders are passed to mpv via --http-header-fields. duration is the current track length in seconds; it is used to start at a percent offset when resuming into a long HTTP stream. Pass 0 to start at startTime in seconds. Returns PlayerReadyMsg on success.
func LaunchGroupCmd ¶ added in v0.6.0
func MpvSocketDir ¶
MpvSocketDir returns a private directory where mpv can create IPC sockets. For snap-packaged mpv, this is ~/snap/mpv/common/<app name>/ since snap's /tmp is isolated. For native mpv, this is /tmp/<app name>-runtime-<uid> with 0700 permissions.
func QuitGroupCmd ¶ added in v0.6.0
func SeekRelativeCmd ¶ added in v0.6.0
SeekRelativeCmd sends a relative seek request to mpv by offset seconds.
func SetAppName ¶ added in v0.6.0
func SetAppName(name string)
SetAppName sets the app name used for runtime file names. Call it once at startup, before playback starts.
func SetGroupPauseCmd ¶ added in v0.6.0
func SetGroupVolumeCmd ¶ added in v0.6.0
func SetPauseCmd ¶ added in v0.6.0
SetPauseCmd sets mpv's paused state.
func SetSpeedCmd ¶
SetSpeedCmd sends a speed change to mpv.
func SetVolumeCmd ¶
SetVolumeCmd sends a volume change to mpv.
Types ¶
type Group ¶ added in v0.6.0
type Group struct {
// contains filtered or unexported fields
}
Group manages independently looping mpv processes as one playback unit.
type GroupControlMsg ¶ added in v0.6.0
type GroupEndedMsg ¶ added in v0.6.0
type GroupReadyMsg ¶ added in v0.6.0
type GroupStoppedMsg ¶ added in v0.6.0
type GroupStoppedMsg struct {
Group *Group
}
type IPCConnection ¶
type IPCConnection interface {
Open() error
Get(property string) (interface{}, error)
Set(property string, value interface{}) error
Call(arguments ...interface{}) (interface{}, error)
Close() error
IsClosed() bool
NewEventListener() (chan *mpvipc.Event, chan struct{})
}
IPCConnection abstracts the mpvipc.Connection for testability.
type Keys ¶ added in v0.6.0
type Keys struct {
PlayPause string
SeekForward string
SeekBackward string
SpeedUp string
SpeedDown string
VolumeUp string
VolumeDown string
}
Keys holds the key names for the player keybindings.
type MediaDownloadFunc ¶ added in v0.6.0
MediaDownloadFunc writes one media item and returns its byte count.
type Model ¶
type Model struct {
Playing bool
Title string
Position float64
Duration float64
Speed float64
Volume int
SleepRemaining string
RepeatStatus string
SubliminalCount int
// contains filtered or unexported fields
}
Model is the bubbletea sub-model for the player footer bar.
func (Model) HandlesKey ¶
HandlesKey reports whether the player should consume the key while a session is active.
func (Model) PlayPauseKey ¶ added in v0.6.0
PlayPauseKey returns the play/pause key binding.
func (Model) SeekBackKey ¶
SeekBackKey returns the seek backward key binding.
func (Model) SeekForwardKey ¶
SeekForwardKey returns the seek forward key binding.
func (Model) VolumeDownKey ¶ added in v0.6.0
VolumeDownKey returns the volume-down key binding.
func (Model) VolumeUpKey ¶ added in v0.6.0
VolumeUpKey returns the volume-up key binding.
type Mpv ¶
type Mpv struct {
// ForceSeekable adds --force-seekable=yes at launch, so that mpv can start
// a long HTTP stream at a percent offset.
ForceSeekable bool
// contains filtered or unexported fields
}
Mpv wraps mpvipc to control an mpv subprocess via IPC.
func NewMpv ¶
func NewMpv() *Mpv
NewMpv creates an Mpv player with default process and connection factories.
func (*Mpv) Done ¶ added in v0.6.0
func (m *Mpv) Done() <-chan struct{}
Done returns a channel that closes when the current mpv process exits.
func (*Mpv) GetDuration ¶
GetDuration returns the total duration in seconds.
func (*Mpv) GetPosition ¶
GetPosition returns the current playback position in seconds.
func (*Mpv) Launch ¶
func (m *Mpv) Launch(url, startTime, socketPath string, paused bool, httpHeaders []string, volume int) error
Launch spawns mpv in audio-only mode with the given IPC socket. If paused is true, mpv starts paused and the user must press play to resume. If a previous mpv process is still running, it is killed first.
func (*Mpv) LaunchLoop ¶ added in v0.6.0
LaunchLoop spawns mpv for an indefinitely looping local media file.
func (*Mpv) SeekRelative ¶ added in v0.6.0
SeekRelative seeks by a relative offset in seconds.
func (*Mpv) WatchEvents ¶ added in v0.6.0
WatchEvents subscribes to mpv's IPC event stream and returns a PlayerEndMsg when mpv broadcasts end-file (normal EOF, load/decode error, or redirect). mpv auto-broadcasts end-file; no observe command is required.
A side goroutine polls conn.IsClosed so that if mpv is killed without emitting end-file (e.g. SIGKILL during stopPlayback), the event listener is unregistered and this cmd returns a fatal PlayerEndMsg instead of leaking.
type Player ¶
type Player interface {
Launch(url, startTime, socketPath string, paused bool, httpHeaders []string, volume int) error
Connect() error
GetPosition() (float64, error)
GetDuration() (float64, error)
GetPaused() (bool, error)
SetPause(paused bool) error
Seek(seconds float64) error
SeekRelative(seconds float64) error
SetSpeed(speed float64) error
SetVolume(vol int) error
GetVolume() (int, error)
Quit() error
}
Player defines the interface for media playback control.
type PlayerEndMsg ¶ added in v0.6.0
type PlayerEndMsg struct {
Generation uint64
Reason string // "eof" | "error" | "redirect"
Err error
}
PlayerEndMsg is emitted when mpv broadcasts an end-file event, indicating the current file finished (eof) or failed to load/decode (error/redirect). The app uses the Reason to decide whether to auto-advance the queue.
type PlayerKeyMap ¶
type PlayerKeyMap struct {
PlayPause key.Binding
SeekForward key.Binding
SeekBack key.Binding
SpeedUp key.Binding
SpeedDown key.Binding
VolumeUp key.Binding
VolumeDown key.Binding
}
PlayerKeyMap defines keybindings active when the player is playing.
type PlayerLaunchErrMsg ¶
PlayerLaunchErrMsg signals that mpv failed to launch.
type PlayerReadyMsg ¶
type PlayerReadyMsg struct {
Generation uint64
}
PlayerReadyMsg signals that mpv has been launched and connected.
type PositionMsg ¶
type PositionMsg struct {
Position float64
Duration float64
Paused bool
Err error
Generation uint64 // ties this tick to a specific play session
}
PositionMsg carries the current playback position polled from mpv.
type ProcessStarter ¶
ProcessStarter abstracts process spawning for testability.
type SessionCache ¶ added in v0.6.0
type SessionCache struct {
// contains filtered or unexported fields
}
SessionCache stores media files for the lifetime of one app process. Downloads are serialized so the configured size limit cannot be exceeded by concurrent cache requests.
func NewSessionCache ¶ added in v0.6.0
func NewSessionCache(maxBytes int64) (*SessionCache, error)
NewSessionCache creates a private cache directory that mpv can access.
func (*SessionCache) Close ¶ added in v0.6.0
func (c *SessionCache) Close() error
Close removes every cached file.
func (*SessionCache) Path ¶ added in v0.6.0
func (c *SessionCache) Path(itemID string) (string, bool)
Path returns a complete cached file for itemID.
func (*SessionCache) Store ¶ added in v0.6.0
func (c *SessionCache) Store(ctx context.Context, itemID string, download MediaDownloadFunc) (string, error)
Store downloads itemID once and returns its local path.
type StartPlayMsg ¶
type StartPlayMsg struct {
Title string
}
StartPlayMsg signals that playback should begin.