player

package
v0.6.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Oct 11, 2026 License: MIT Imports: 22 Imported by: 0 modules, 0 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

Documentation

Overview

Package player provides an mpv IPC wrapper for audio playback control.

Index

Constants

View Source
const DefaultMediaCacheLimit int64 = 2 << 30

DefaultMediaCacheLimit bounds temporary media storage to 2 GiB.

View Source
const MaxSubliminalTracks = 8

MaxSubliminalTracks prevents accidental unbounded mpv process creation.

Variables

View Source
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 LaunchGroupCmd(ctx context.Context, group *Group, paths []string, volume int, generation uint64) tea.Cmd

func MpvSocketDir

func MpvSocketDir() (string, error)

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 QuitCmd

func QuitCmd(p Player) tea.Cmd

QuitCmd stops mpv playback.

func QuitGroupCmd added in v0.6.0

func QuitGroupCmd(group *Group) tea.Cmd

func SeekCmd added in v0.6.0

func SeekCmd(p Player, target float64) tea.Cmd

SeekCmd sends an absolute seek request to mpv by target seconds.

func SeekRelativeCmd added in v0.6.0

func SeekRelativeCmd(p Player, offset float64) tea.Cmd

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 SetGroupPauseCmd(group *Group, paused bool, generation uint64) tea.Cmd

func SetGroupVolumeCmd added in v0.6.0

func SetGroupVolumeCmd(group *Group, volume int, generation uint64) tea.Cmd

func SetPauseCmd added in v0.6.0

func SetPauseCmd(p Player, paused bool) tea.Cmd

SetPauseCmd sets mpv's paused state.

func SetSpeedCmd

func SetSpeedCmd(p Player, speed float64) tea.Cmd

SetSpeedCmd sends a speed change to mpv.

func SetVolumeCmd

func SetVolumeCmd(p Player, vol int) tea.Cmd

SetVolumeCmd sends a volume change to mpv.

func TickCmd

func TickCmd(p Player, generation uint64) tea.Cmd

TickCmd returns a command that fires twice per second and polls mpv state. The generation parameter ties the tick to a specific play session so stale ticks from a previous session can be safely ignored.

func WatchGroupCmd added in v0.6.0

func WatchGroupCmd(ctx context.Context, group *Group, generation uint64) tea.Cmd

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.

func NewGroup added in v0.6.0

func NewGroup() *Group

func (*Group) Launch added in v0.6.0

func (g *Group) Launch(ctx context.Context, paths []string, volume int) error

Launch starts every path paused, then resumes the complete group together.

func (*Group) Quit added in v0.6.0

func (g *Group) Quit() error

func (*Group) SetPause added in v0.6.0

func (g *Group) SetPause(paused bool) error

func (*Group) SetVolume added in v0.6.0

func (g *Group) SetVolume(volume int) error

func (*Group) Wait added in v0.6.0

func (g *Group) Wait(ctx context.Context) error

Wait returns when a group member exits or ctx is cancelled.

type GroupControlMsg added in v0.6.0

type GroupControlMsg struct {
	Generation uint64
	Err        error
}

type GroupEndedMsg added in v0.6.0

type GroupEndedMsg struct {
	Generation uint64
	Err        error
}

type GroupReadyMsg added in v0.6.0

type GroupReadyMsg struct {
	Generation uint64
	Err        error
}

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

type MediaDownloadFunc func(context.Context, io.Writer) (int64, error)

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 NewModel

func NewModel(p Player, speed float64, keys Keys, styles ui.Styles) Model

NewModel creates a new player sub-model with the initial speed and the key names.

func (Model) HandlesKey

func (m Model) HandlesKey(msg tea.KeyMsg) bool

HandlesKey reports whether the player should consume the key while a session is active.

func (Model) Init

func (m Model) Init() tea.Cmd

Init returns nil; the player is inactive until playback starts.

func (Model) PlayPauseKey added in v0.6.0

func (m Model) PlayPauseKey() key.Binding

PlayPauseKey returns the play/pause key binding.

func (Model) SeekBackKey

func (m Model) SeekBackKey() key.Binding

SeekBackKey returns the seek backward key binding.

func (Model) SeekForwardKey

func (m Model) SeekForwardKey() key.Binding

SeekForwardKey returns the seek forward key binding.

func (*Model) SetWidth

func (m *Model) SetWidth(w int)

SetWidth sets the available width for rendering.

func (Model) Update

func (m Model) Update(msg tea.Msg) (Model, tea.Cmd)

Update handles messages for the player sub-model.

func (Model) View

func (m Model) View() string

View renders the player footer bar. Returns empty string if inactive.

func (Model) VolumeDownKey added in v0.6.0

func (m Model) VolumeDownKey() key.Binding

VolumeDownKey returns the volume-down key binding.

func (Model) VolumeUpKey added in v0.6.0

func (m Model) VolumeUpKey() key.Binding

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) Connect

func (m *Mpv) Connect() error

Connect opens the IPC connection to mpv.

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

func (m *Mpv) GetDuration() (float64, error)

GetDuration returns the total duration in seconds.

func (*Mpv) GetPaused

func (m *Mpv) GetPaused() (bool, error)

GetPaused returns whether playback is paused.

func (*Mpv) GetPosition

func (m *Mpv) GetPosition() (float64, error)

GetPosition returns the current playback position in seconds.

func (*Mpv) GetVolume

func (m *Mpv) GetVolume() (int, error)

GetVolume returns the current volume level.

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

func (m *Mpv) LaunchLoop(path, socketPath string, paused bool, volume int) error

LaunchLoop spawns mpv for an indefinitely looping local media file.

func (*Mpv) Quit

func (m *Mpv) Quit() error

Quit sends the quit command and cleans up.

func (*Mpv) Seek

func (m *Mpv) Seek(seconds float64) error

Seek seeks to an absolute position in seconds.

func (*Mpv) SeekRelative added in v0.6.0

func (m *Mpv) SeekRelative(seconds float64) error

SeekRelative seeks by a relative offset in seconds.

func (*Mpv) SetPause

func (m *Mpv) SetPause(paused bool) error

SetPause pauses or resumes playback.

func (*Mpv) SetSpeed

func (m *Mpv) SetSpeed(speed float64) error

SetSpeed sets the playback speed multiplier.

func (*Mpv) SetVolume

func (m *Mpv) SetVolume(vol int) error

SetVolume sets the playback volume (0-150).

func (*Mpv) WatchEvents added in v0.6.0

func (m *Mpv) WatchEvents(generation uint64) tea.Cmd

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

type PlayerLaunchErrMsg struct {
	Generation uint64
	Err        error
}

PlayerLaunchErrMsg signals that mpv failed to launch.

type PlayerQuitMsg

type PlayerQuitMsg struct{}

PlayerQuitMsg signals that mpv has been quit.

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

type ProcessStarter func(name string, args ...string) *exec.Cmd

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.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL