Documentation
¶
Overview ¶
Package stream runs game sessions on the server: it starts a runner process, reads its raw frames, encodes them with FFmpeg and sends them to viewers over WebRTC or a WebSocket. docs/GAMING.md has the reasoning.
Index ¶
- Constants
- Variables
- func SlotName(slot int) (string, error)
- type AU
- type Config
- type Info
- type Manager
- type Mode
- type Options
- type Session
- func (s *Session) Answer(ctx context.Context, offer, prefer string) (string, error)
- func (s *Session) Done() <-chan struct{}
- func (s *Session) ID() string
- func (s *Session) Info() Info
- func (s *Session) Input(p []byte)
- func (s *Session) ItemID() int64
- func (s *Session) LoadState(slot int) error
- func (s *Session) Pause(on bool) error
- func (s *Session) Reset() error
- func (s *Session) SaveState(ctx context.Context, slot int) error
- func (s *Session) ServeWS(w http.ResponseWriter, r *http.Request, mode string)
- func (s *Session) Stop()
- func (s *Session) UserID() int64
Constants ¶
const ( ClientBrowser = "browser" ClientNative = "native" )
Client kinds a server can be asked about.
const MaxOfferSize = 64 << 10
MaxOfferSize bounds an SDP offer; a real one is a few kilobytes.
const MaxPlayers = 4
MaxPlayers is how many players a browser may drive.
Variables ¶
var ( ErrBusy = errors.New("this server is already running as many games as it allows; stop one first") ErrStopped = errors.New("the game has stopped") ErrNoSlot = errors.New("there is no such save slot") )
Errors a caller shows to people.
Functions ¶
Types ¶
type Config ¶
type Config struct {
Runner string // the wittyplay-host program, or another runner speaking ipc
RunnerArgs []string // extra arguments, before the ones added here
FFmpeg string
HWEncoders []string // hardware encoders known to work here, best first
HaveEncoder func(string) bool // FFmpeg was built with this encoder
MaxSessions int // 0 means 2
MemoryMB int64 // 0 means 2048
Log *slog.Logger
// WebRTC networking; all optional. A fixed UDP port is what a firewall or
// a container needs; the public address is what NAT needs.
UDPPort int
PublicIPs []string
STUN []string
}
Config is how the server runs games.
type Info ¶
type Info struct {
ID string `json:"id"`
ItemID int64 `json:"itemId"`
Title string `json:"title"`
Console string `json:"console"`
Core string `json:"core"`
Width int `json:"width"`
Height int `json:"height"`
Aspect float64 `json:"aspect"`
FPS float64 `json:"fps"`
SampleRate float64 `json:"sampleRate"`
Resumed bool `json:"resumed"`
Encoder string `json:"encoder,omitempty"`
Modes []Mode `json:"modes"` // what this client can connect with, best first
}
Info is what a browser needs to know about a session.
type Manager ¶
type Manager struct {
// contains filtered or unexported fields
}
Manager holds the running sessions.
func NewManager ¶
NewManager makes a manager; sessions start with Start.
type Mode ¶
type Mode struct {
ID string `json:"id"` // webrtc | ws-h264 | ws-jpeg
Video []string `json:"video"` // codecs, best first
Audio string `json:"audio"` // opus | pcm
Input string `json:"input"` // datachannel | websocket
Needs string `json:"needs,omitempty"` // what a client must have, in words
// BrowserOnly marks a mode only a web browser can use. None exists yet; in-browser
// emulation will be one, and is left out of the answer for native clients.
BrowserOnly bool `json:"browserOnly,omitempty"`
}
Mode is a way a client can receive a game. The server says which ones it has, so a client never has to guess, and new modes can appear without breaking old clients.
type Options ¶
type Options struct {
UserID, ItemID int64
Title string
Console string
Core string // a shared library path, or ipc.TestCore
ROM string
SystemDir string
SaveDir string // this account's folder for this game
Resume bool // continue from auto.state
Quality string // "" (auto) | low | high
CoreOptions map[string]string
Cleanup func() // runs when the session has ended
}
Options start one game.
type Session ¶
type Session struct {
// contains filtered or unexported fields
}
Session is one running game.
func (*Session) Answer ¶
Answer connects a browser over WebRTC: it takes the browser's offer and returns the answer. prefer is "" or "vp8". Which codec is used depends on what the browser offered and what FFmpeg here can encode.
func (*Session) Done ¶
func (s *Session) Done() <-chan struct{}
Done is closed when the game has ended.
func (*Session) Input ¶
Input applies an 11-byte pad message from a viewer. Malformed ones and players beyond what the player offers are ignored.
func (*Session) ServeWS ¶
ServeWS is the fallback transport. mode is "h264" (decoded with WebCodecs) or "jpeg". Origin checks are done by Accept, as for the rest of the API.