live

package
v0.2.1 Latest Latest
Warning

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

Go to latest
Published: Nov 4, 2026 License: AGPL-3.0 Imports: 6 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 live keeps track of what's happening on the server right now: which players are open, what each one is playing, and a feed of events for home automation (see the Home Assistant guide). Players that can take commands get them from here too, so another device can pause or start something on them.

Players say hello and report what they play with Report, and ask for commands with Poll, which waits until one comes or the wait runs out. A player that stops asking goes offline.

Index

Constants

View Source
const (
	CommandPlay      = "play"      // carry on playing
	CommandPause     = "pause"     // pause
	CommandStop      = "stop"      // stop and close the player
	CommandSeek      = "seek"      // jump to PositionMs
	CommandNext      = "next"      // the next episode or track
	CommandPrevious  = "previous"  // the previous one, or back to the start
	CommandPlayItem  = "playItem"  // open ItemID and play it
	CommandVolume    = "volume"    // set the volume to Volume, 0 to 1
	CommandMute      = "mute"      // mute or unmute (Muted)
	CommandSleep     = "sleep"     // stop after Minutes, or at the end (EndOfItem); 0 cancels
	CommandSubtitles = "subtitles" // show subtitle Track, or none for ""
)

Things a player can be asked to do. Players list the ones they can in Hello.Commands.

View Source
const (
	EventClientUpdated   = "client.updated"   // a player appeared, changed, or went offline
	EventClientRemoved   = "client.removed"   // an offline player was forgotten
	EventPlaybackStarted = "playback.started" // a player started something
	EventPlaybackPaused  = "playback.paused"
	EventPlaybackResumed = "playback.resumed"
	EventPlaybackStopped = "playback.stopped" // Finished says whether it reached the end
	EventItemAdded       = "item.added"       // a scan found something new
	EventLibraryScanned  = "library.scanned"  // a library finished scanning
)

Event types in the feed.

View Source
const (

	// MaxWait is the longest a player's request for commands is held open.
	MaxWait = 30 * time.Second
)

Variables

View Source
var (
	ErrNoClient    = errors.New("no such player")
	ErrOffline     = errors.New("player is offline")
	ErrUnsupported = errors.New("player can't do that")
	ErrBusy        = errors.New("player isn't keeping up")
)

Commands are every command there is.

Functions

This section is empty.

Types

type Client

type Client struct {
	ID string `json:"id"`
	Hello
	User     User     `json:"user"`
	Online   bool     `json:"online"`
	LastSeen int64    `json:"lastSeen"` // milliseconds
	Playing  *Playing `json:"playing,omitempty"`
	// Remote says the player is away from home, so it has no address on the
	// home network.
	Remote bool `json:"remote,omitempty"`
	// contains filtered or unexported fields
}

Client is an open web player or app.

func (Client) Account

func (p Client) Account() int64

Account is the account a player reports as.

type Command

type Command struct {
	ID         string   `json:"id"`
	Type       string   `json:"type"`
	PositionMs *int64   `json:"positionMs,omitempty"` // seek; where to start, for playItem
	ItemID     int64    `json:"itemId,omitempty"`     // playItem
	Item       *Item    `json:"item,omitempty"`       // playItem: what ItemID is, for the player to show
	Volume     *float64 `json:"volume,omitempty"`
	Muted      *bool    `json:"muted,omitempty"`
	Minutes    *int     `json:"minutes,omitempty"`
	EndOfItem  bool     `json:"endOfItem,omitempty"`
	Track      *string  `json:"track,omitempty"`
	// From names who sent it, such as "Home Assistant", so a player can say so.
	From string `json:"from,omitempty"`
}

Command is something a player is asked to do.

type Event

type Event struct {
	Type     string   `json:"type"`
	Time     int64    `json:"time"`               // milliseconds
	Client   *Client  `json:"client,omitempty"`   // client.updated, and the client for playback events
	ClientID string   `json:"clientId,omitempty"` // client.removed
	Item     *Item    `json:"item,omitempty"`
	User     *User    `json:"user,omitempty"`
	Library  *Library `json:"library,omitempty"`
	// PositionMs is where playback was, for playback events.
	PositionMs int64 `json:"positionMs,omitempty"`
	Finished   bool  `json:"finished,omitempty"`
}

Event is one entry in the feed.

type Hello

type Hello struct {
	Name     string   `json:"name"`     // what people call the device, such as "Living room TV"
	Platform string   `json:"platform"` // web, ios, android, tvos, androidtv
	App      string   `json:"app,omitempty"`
	Version  string   `json:"version,omitempty"`
	Commands []string `json:"commands"` // the commands it takes; none means it can't be controlled
}

Hello is what a player says about itself every time it reports.

type Hub

type Hub struct {
	// contains filtered or unexported fields
}

Hub is the server's live state. The zero value is not usable; call New.

func New

func New() *Hub

New makes a hub. Run it so players go offline when they leave.

func (*Hub) Client

func (h *Hub) Client(id string, allowed func(Client) bool) (Client, bool)

Client returns one player, if allowed may see it.

func (*Hub) Clients

func (h *Hub) Clients(allowed func(Client) bool) []Client

Players lists the players allowed may see, online ones first.

func (*Hub) Close added in v0.1.4

func (h *Hub) Close()

Close is the server shutting down: every held Poll returns at once, so players hear about it (see Closing) and the server isn't kept waiting.

func (*Hub) Closing added in v0.1.4

func (h *Hub) Closing() bool

Closing says whether the server is shutting down.

func (*Hub) Leave

func (h *Hub) Leave(id string, account int64)

Leave is a player closing: it goes offline at once.

func (*Hub) Poll

func (h *Hub) Poll(ctx context.Context, id string, account int64, wait time.Duration) ([]Command, error)

Poll waits up to wait for commands for player id, and counts as the player being there. ErrNoClient means it should report first.

func (*Hub) Publish

func (h *Hub) Publish(e Event)

Publish adds an event to the feed.

func (*Hub) Report

func (h *Hub) Report(id string, account int64, user User, hello Hello, playing *Playing, remote bool) error

Report is a player saying hello and what it's playing (nil for nothing). account is the signed-in account (or the home guest) the player uses; another account can't report for the same player.

func (*Hub) Run

func (h *Hub) Run(ctx context.Context)

Run marks quiet players offline, until ctx ends.

func (*Hub) Send

func (h *Hub) Send(id string, allowed func(Client) bool, c Command) (Command, error)

Send queues a command for a player. allowed says whether the sender may control this player.

func (*Hub) Subscribe

func (h *Hub) Subscribe() (events <-chan Event, cancel func())

Subscribe follows the feed until cancel is called. The channel closes if the subscriber falls too far behind.

type Item

type Item struct {
	ID        int64  `json:"id"`
	LibraryID int64  `json:"libraryId"`
	Kind      string `json:"kind"`
	Title     string `json:"title"`
	Year      int    `json:"year,omitempty"`
	ShowTitle string `json:"showTitle,omitempty"`
	Season    int    `json:"season,omitempty"`
	Episode   int    `json:"episode,omitempty"`
}

Item is what's playing, or what an event is about.

type Library

type Library struct {
	ID   int64  `json:"id"`
	Name string `json:"name"`
	Kind string `json:"kind"`
}

Library is a library an event is about.

type Playing

type Playing struct {
	Item       Item     `json:"item"`
	PositionMs int64    `json:"positionMs"`
	DurationMs int64    `json:"durationMs,omitempty"`
	Paused     bool     `json:"paused"`
	Buffering  bool     `json:"buffering,omitempty"`
	Volume     *float64 `json:"volume,omitempty"` // 0 to 1, when the player has its own volume
	Muted      bool     `json:"muted,omitempty"`
	// Subtitles is the subtitle track showing, empty for none.
	Subtitles string `json:"subtitles,omitempty"`
	// Transcoding says the server converts the stream; false is direct play.
	Transcoding bool `json:"transcoding,omitempty"`
	// SleepAt is when the sleep timer stops playback (milliseconds), or 0.
	SleepAt int64 `json:"sleepAt,omitempty"`
	// SleepAtEnd means the sleep timer stops at the end of this item.
	SleepAtEnd bool  `json:"sleepAtEnd,omitempty"`
	UpdatedAt  int64 `json:"updatedAt"` // milliseconds
}

Playing is what a player has open. PositionMs was true at UpdatedAt and moves on from there unless Paused.

type User

type User struct {
	ID    int64  `json:"id"`
	Name  string `json:"name"`
	Guest bool   `json:"guest,omitempty"` // the home network's guest account or one of its profiles
}

User is whose player it is: an account or a profile.

Jump to

Keyboard shortcuts

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