stream

package
v0.1.0 Latest Latest
Warning

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

Go to latest
Published: Oct 30, 2026 License: Apache-2.0 Imports: 32 Imported by: 0

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

View Source
const (
	ClientBrowser = "browser"
	ClientNative  = "native"
)

Client kinds a server can be asked about.

View Source
const MaxOfferSize = 64 << 10

MaxOfferSize bounds an SDP offer; a real one is a few kilobytes.

View Source
const MaxPlayers = 4

MaxPlayers is how many players a browser may drive.

Variables

View Source
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

func SlotName

func SlotName(slot int) (string, error)

SlotName is the state file for a slot: 0 is the automatic save, 1-9 the manual ones. Paths are built only from this number.

Types

type AU

type AU struct {
	Data []byte
	Key  bool
}

AU is one encoded frame in Annex B form (H.264) or as a VP8 frame.

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

func NewManager(cfg Config) *Manager

NewManager makes a manager; sessions start with Start.

func (*Manager) Configure

func (m *Manager) Configure(update func(*Config))

Configure replaces the settings new sessions use.

func (*Manager) Count

func (m *Manager) Count() int

Count says how many games are running.

func (*Manager) Get

func (m *Manager) Get(id string) *Session

Get finds a session.

func (*Manager) Modes

func (m *Manager) Modes(client string) []Mode

Modes lists what this server can do for a kind of client, best first.

func (*Manager) Start

func (m *Manager) Start(opt Options) (*Session, error)

Start launches a game. An account plays one game at a time: starting another saves and ends the first, on any device.

func (*Manager) StopAll

func (m *Manager) StopAll()

StopAll ends every session, saving each one. Used at shutdown.

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

func (s *Session) Answer(ctx context.Context, offer, prefer string) (string, error)

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

func (s *Session) ID() string

ID is the session's unguessable name.

func (*Session) Info

func (s *Session) Info() Info

Info describes the session for a browser.

func (*Session) Input

func (s *Session) Input(p []byte)

Input applies an 11-byte pad message from a viewer. Malformed ones and players beyond what the player offers are ignored.

func (*Session) ItemID

func (s *Session) ItemID() int64

ItemID is the game being played.

func (*Session) LoadState

func (s *Session) LoadState(slot int) error

LoadState restores a save slot.

func (*Session) Pause

func (s *Session) Pause(on bool) error

Pause stops or restarts the game, as a menu does.

func (*Session) Reset

func (s *Session) Reset() error

Reset is the console's reset button.

func (*Session) SaveState

func (s *Session) SaveState(ctx context.Context, slot int) error

SaveState writes a save slot, and a picture of the game beside it.

func (*Session) ServeWS

func (s *Session) ServeWS(w http.ResponseWriter, r *http.Request, mode string)

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.

func (*Session) Stop

func (s *Session) Stop()

Stop saves the game and ends it. It returns once everything is written.

func (*Session) UserID

func (s *Session) UserID() int64

UserID is the account that started it.

Jump to

Keyboard shortcuts

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