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
- Variables
- type Client
- type Command
- type Event
- type Hello
- type Hub
- func (h *Hub) Client(id string, allowed func(Client) bool) (Client, bool)
- func (h *Hub) Clients(allowed func(Client) bool) []Client
- func (h *Hub) Close()
- func (h *Hub) Closing() bool
- func (h *Hub) Leave(id string, account int64)
- func (h *Hub) Notify(allowed func(Client) bool, notification any) int
- func (h *Hub) Poll(ctx context.Context, id string, account int64, wait time.Duration) ([]Command, error)
- func (h *Hub) Publish(e Event)
- func (h *Hub) Report(id string, account int64, user User, hello Hello, playing *Playing, ...) error
- func (h *Hub) Run(ctx context.Context)
- func (h *Hub) Send(id string, allowed func(Client) bool, c Command) (Command, error)
- func (h *Hub) Subscribe() (events <-chan Event, cancel func())
- type Item
- type Library
- type Playing
- type User
Constants ¶
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.
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.
const CommandNotify = "notify"
CommandNotify tells the person using a player something (Notification), such as a title they asked for being ready. Only the server sends it, to players that say they take notifications (Hello.Notifications).
const ( // MaxWait is the longest a player's request for commands is held open. MaxWait = 30 * time.Second )
Variables ¶
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") )
var Commands = []string{CommandPlay, CommandPause, CommandStop, CommandSeek, CommandNext, CommandPrevious, CommandPlayItem, CommandVolume, CommandMute, CommandSleep, CommandSubtitles}
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.
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"`
// Notification is what to show, for notify.
Notification any `json:"notification,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
// Notifications says it shows notifications (see Hub.Notify), which come
// as a "notify" command. They aren't in Commands, since nobody else
// can send one.
Notifications bool `json:"notifications,omitempty"`
}
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 (*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) Notify ¶ added in v0.2.3
Notify sends a notification to every online player allowed picks that shows notifications, and says how many it went to. A player that's slow to ask misses it; it's in the person's list of notifications anyway.
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) 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.
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 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.