playlists

package
v2.22.0 Latest Latest
Warning

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

Go to latest
Published: Nov 9, 2026 License: GPL-3.0 Imports: 12 Imported by: 0

Documentation

Index

Constants

View Source
const (
	RepeatNone = "none"
	RepeatAll  = "all"
	RepeatOne  = "one"
)

Repeat modes of a playlist, as the API names them.

View Source
const DefaultCountdown = 10 * time.Second

DefaultCountdown is how long an automatic switch is announced when a playlist does not say.

Variables

View Source
var ErrInvalidRepeat = errors.New("invalid repeat mode")

ErrInvalidRepeat is returned for a repeat mode that is not one of the Repeat constants.

View Source
var ErrSnapshotVersion = errors.New("unsupported playlist snapshot version")

ErrSnapshotVersion is returned for a snapshot this build cannot read.

Functions

func EncodeSnapshot added in v2.21.0

func EncodeSnapshot(p *Playlist) (string, error)

EncodeSnapshot stores what a playlist is and where it is, so it can be opened again after a restart. Playback state is left out: a restored playlist never plays by itself.

func ItemScripts added in v2.21.0

func ItemScripts(items []PlaylistItem) []string

ItemScripts returns the ZapScript of each item, in order.

func ItemsHash added in v2.21.0

func ItemsHash(items []PlaylistItem) string

ItemsHash identifies the set of items in a playlist regardless of their order, so a shuffled copy of the same items hashes the same.

func LocateItem added in v2.21.0

func LocateItem(items []PlaylistItem, zapScript string, hint int) int

LocateItem finds the item with zapScript. hint, where the item was last time, settles which of several identical items it was; without a match there the item is only found when exactly one has that script. It returns -1 rather than guess.

func MemoryKey added in v2.21.0

func MemoryKey(p *Playlist) string

MemoryKey is what a playlist is remembered by: its deck when it has one, since that is the only identity this device vouches for, else the ID its source gave it, else its set of items. It is "" for a playlist that is not remembered.

func NormalizeRepeat added in v2.22.0

func NormalizeRepeat(mode string) (string, error)

NormalizeRepeat returns the repeat mode named by mode, accepting "off" as RepeatNone.

func ShuffleItems added in v2.22.0

func ShuffleItems(p *Playlist)

ShuffleItems puts the items of a playlist that was just loaded in a random order and marks it shuffled, keeping the order they loaded in so shuffle can be turned off again.

Types

type Intent added in v2.22.0

type Intent int

Intent is a request to play or pause whatever playlist is open in a slot, resolved by the queue worker against the playlist as it is at that moment.

const (
	// IntentNone marks an ordinary update that carries a whole playlist.
	IntentNone Intent = iota
	IntentPlay
	IntentPause
)

type Playlist

type Playlist struct {
	// HoldToken is internal runtime ownership for primary hold-mode playback.
	// It is not part of playlist persistence or API responses.
	HoldToken *tokens.Token
	// AllowedCommands is the bound carried by the script that opened this
	// playlist, and it travels to every item token. A playlist is an
	// indirection like a ZapLink: opening one must not let its items reach
	// commands the script that opened it could not run itself.
	AllowedCommands tokens.CommandPolicy
	// Repeat, set only on a ModeChange update, is the repeat mode to switch
	// the open playlist to: RepeatNone, RepeatAll or RepeatOne.
	Repeat *string `json:"-"`
	ID     string
	// DeckID names the deck on this device the playlist was opened from. It
	// is set only by the deck loader, never from a served playlist, so it is
	// what ties an open playlist to its deck; the ID is whatever its source
	// chose to call it.
	DeckID string
	// ProfileID is the profile that loaded the playlist, "" for none. Its
	// memory of where the playlist is belongs to that profile until the
	// playlist closes, whoever is active by then.
	ProfileID string
	Name      string
	Slot      string
	Items     []PlaylistItem
	// Original is the order the items loaded in, kept only while Shuffled so
	// turning shuffle off can put them back. It holds the same items as
	// Items.
	Original []PlaylistItem
	Index    int
	// Move identifies the move that stored the playlist, and so the launch
	// it started. In-place changes that launch nothing, such as shuffle and
	// repeat, keep it; any other update gets a new one. Zero is unstamped.
	Move uint64 `json:"-"`
	// Advance moves to the next item after this much playing time. Zero
	// means the playlist does not rotate on a timer.
	Advance time.Duration
	// Idle moves to the next item after this long with no user input, and
	// starts a loaded playlist the same way. Zero means it does not rotate on
	// idle. It is never set together with Advance.
	Idle time.Duration
	// Countdown is how long the notice before an automatic switch stays up.
	// CountdownSet distinguishes an explicit zero, which switches without a
	// notice, from no value, which uses DefaultCountdown.
	Countdown time.Duration
	// EngineFromIndex is the item the engine moved this update away from, so
	// the queue handler can drop a move the user has already overtaken.
	EngineFromIndex int `json:"-"`
	// Intent, when set, makes this an update that only plays or pauses the
	// open playlist; the other fields are then ignored.
	Intent Intent `json:"-"`
	// SkipRun counts the items in a row a background playlist has stepped
	// past because their launch failed. Any other move starts it over.
	SkipRun int `json:"-"`
	// StopSeq is the count of stops of the primary media when the update was
	// issued, valid when StopStamped is set. A stop after that cancels an
	// update that has not launched yet.
	StopSeq uint64 `json:"-"`
	// StopStamped says StopSeq was taken for this update.
	StopStamped bool `json:"-"`
	Playing     bool
	// Armed marks a loaded idle playlist that is waiting for the device to go
	// idle before it starts. Playing it, pausing it or stopping it disarms it.
	Armed bool
	// PausedByFailure marks a playlist that stopped because its current item
	// failed to launch, not because anyone paused it. Moving to another item
	// resumes such a playlist, where a paused one only moves its cursor.
	PausedByFailure bool `json:"-"`
	// Clear signals the queue handler to remove the active playlist for this slot.
	Clear bool
	// Loop and LoopOne control end-of-playlist behavior set at load time.
	// Loop wraps back to the start; LoopOne repeats the current track.
	Loop    bool
	LoopOne bool
	// ForceRelaunch bypasses playlistNeedsUpdate dedup so the same track can be
	// relaunched for LoopOne and single-item Loop.
	ForceRelaunch bool
	// FromPlaylistItem marks an update requested by a playlist item's own
	// run, such as a card that plays another playlist. It is part of that
	// item's launch rather than a second one competing with it.
	FromPlaylistItem bool `json:"-"`
	// EngineStarted marks an update the auto-advance engine queued on its own,
	// so the item it launches is known to be one nobody asked for. It is
	// part of that launch rather than of the playlist.
	EngineStarted bool `json:"-"`
	// Reconfigure applies only the rotation settings of this update — Advance,
	// Idle, Countdown and Armed — to the active playlist with the same ID,
	// keeping its position and playback and never launching anything.
	Reconfigure bool `json:"-"`
	// EngineFromArmed says the move was decided on a loaded playlist waiting
	// to start rather than a playing one.
	EngineFromArmed bool `json:"-"`
	// ModeChange applies only the shuffle and repeat settings of this update,
	// ShuffleSet with Shuffle and Repeat, to the playlist open in its slot at
	// the time it is handled. It keeps the current item and playback and
	// never launches anything.
	ModeChange bool `json:"-"`
	// ShuffleSet says this ModeChange update sets shuffle to Shuffle.
	ShuffleSet bool `json:"-"`
	// Shuffle is the shuffle state a ModeChange update with ShuffleSet asks for.
	Shuffle bool `json:"-"`
	// Refresh replaces the items and name of the active playlist with the
	// same ID in place, keeping its position and playback state and never
	// launching anything. It is ignored when no such playlist is active.
	Refresh bool
	// Unsafe is the playlist's effective trust. It is set either because the
	// items came from a source this device does not trust — a fetched
	// ZapLink, or a cached copy of somebody else's deck — or because the
	// script that opened it was already untrusted, which even the user's own
	// deck inherits. Every item token carries it, so commands that drive
	// input or run programs refuse, the same as the script that opened the
	// playlist.
	Unsafe       bool
	CountdownSet bool
	// Shuffled says the items were put in a random order when the playlist
	// loaded, so that order is part of where the playlist is.
	Shuffled bool
	// Ephemeral marks a playlist made up on the spot, such as a random
	// rotation, that has no identity to be remembered by.
	Ephemeral bool
	// Completed marks the Clear of a playlist that played to its natural
	// end, which starts it over the next time it loads.
	Completed bool `json:"-"`
}

func ApplyIntent added in v2.22.0

func ApplyIntent(open, update *Playlist) *Playlist

ApplyIntent resolves an intent-only update against the open playlist, or returns nil when none is open.

func DecodeSnapshot added in v2.21.0

func DecodeSnapshot(data string) (*Playlist, error)

DecodeSnapshot opens a playlist stored by EncodeSnapshot, paused and not waiting to start.

func Goto

func Goto(p Playlist, idx int) *Playlist

func IntentUpdate added in v2.22.0

func IntentUpdate(slot string, playing bool) *Playlist

IntentUpdate returns an update that only plays or pauses the playlist open in slot. It carries no playlist of its own, so it cannot overwrite a move, shuffle or repeat change that lands before the worker reaches it.

func NewPlaylist

func NewPlaylist(id, name string, item []PlaylistItem) *Playlist

func Next

func Next(p Playlist) *Playlist

func Pause

func Pause(p Playlist) *Playlist

func Play

func Play(p Playlist) *Playlist

func Previous

func Previous(p Playlist) *Playlist

func Rearm added in v2.21.0

func Rearm(p Playlist) *Playlist

Rearm returns p loaded and waiting for the device to go idle: not playing, position kept. The hold token is dropped because whatever held the playlist open is gone.

func SetRepeat added in v2.22.0

func SetRepeat(p *Playlist, mode string) (*Playlist, error)

SetRepeat returns p switched to repeat mode, keeping its position and playback.

func SetShuffle added in v2.22.0

func SetShuffle(p *Playlist, on bool) *Playlist

SetShuffle returns p with shuffle turned on or off, keeping the current item current and the playback as it is. Turning it on moves the current item to the front and shuffles the rest behind it; turning it off puts the items back in the order they loaded in. A playlist already in the asked state is returned unchanged.

func SkipFailed added in v2.22.0

func SkipFailed(p Playlist) (*Playlist, bool)

SkipFailed returns the move past p's current item after its launch failed, or false when there is nowhere to go: the end of a playlist that does not loop, or every item having failed in a row. It keeps count, which any other move starts over.

func StopRotating added in v2.21.0

func StopRotating(p Playlist) *Playlist

StopRotating returns the reconfiguration that turns off p's advance and idle rotation and keeps everything else, including what is playing.

func (*Playlist) APIState added in v2.22.0

func (p *Playlist) APIState() models.PlaylistState

APIState returns the playlist as the API reports it.

func (*Playlist) Current

func (p *Playlist) Current() PlaylistItem

Current returns the item at the playlist's index, clamped into range. It only reads: the stored playlist is shared between goroutines, so a read that repaired an out-of-range index in place would be a data race.

func (*Playlist) EffectiveCountdown added in v2.21.0

func (p *Playlist) EffectiveCountdown() time.Duration

EffectiveCountdown returns the notice length for an automatic switch.

func (*Playlist) RepeatMode added in v2.22.0

func (p *Playlist) RepeatMode() string

RepeatMode returns the playlist's repeat mode.

type PlaylistController

type PlaylistController struct {
	Active     *Playlist
	Background *Playlist
	Current    *Playlist
	HoldToken  *tokens.Token
	Queue      chan<- *Playlist
	// LaunchResolved, when set, is told once the launcher of a launch is
	// known whether that launcher publishes the media itself later, outside
	// the launch call. Only the playlist worker sets it.
	LaunchResolved func(publishesLater bool)
}

type PlaylistItem

type PlaylistItem struct {
	ZapScript string
	Name      string
}

func ApplyOrder added in v2.21.0

func ApplyOrder(items []PlaylistItem, order []string) ([]PlaylistItem, bool)

ApplyOrder puts items in order, given as item scripts. It reports false and leaves items alone unless order holds exactly the same items.

Jump to

Keyboard shortcuts

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