relay

package
v0.4.1 Latest Latest
Warning

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

Go to latest
Published: Nov 14, 2026 License: AGPL-3.0 Imports: 22 Imported by: 0

Documentation

Overview

Package relay carries a game stream between the Moonlight host program and a browser. The host program holds Moonlight's GPL-3.0 code and is a separate program, reached only by the messages in WittyCouchHost's PROTOCOL.md: this package contains none of it.

Index

Constants

View Source
const (
	CmdPad         = 65
	CmdMouseMove   = 66
	CmdMouseButton = 67
	CmdKey         = 68
	CmdScroll      = 69
	CmdIDR         = 70

	CmdMousePosition = 72
	CmdText          = 73
	CmdHScroll       = 74
)

What it reads. The numbers from CmdPad up are also what a browser sends.

View Source
const (

	// ProtocolVersion is the host program this package speaks to.
	ProtocolVersion = 1
)

Variables

View Source
var ErrNoProgram = errors.New("the Moonlight host program isn't installed on this server")

ErrNoProgram: the program isn't installed here.

Functions

func CheckProgram

func CheckProgram(ctx context.Context, program string, prepare func(*exec.Cmd)) error

CheckProgram runs the program once to see that it can run here and speaks this protocol, given as little as it's given to play a game.

func CheckProgramWith added in v0.4.0

func CheckProgramWith(ctx context.Context, program string, args []string, prepare func(*exec.Cmd)) error

CheckProgramWith is CheckProgram for a program run with arguments before its own.

func ValidInput

func ValidInput(m []byte) bool

ValidInput checks a message from a browser: its type byte, then its body.

Types

type Event

type Event struct {
	Type     string `json:"type"`
	Stage    string `json:"stage,omitempty"`
	Code     int    `json:"code,omitempty"`
	Poor     bool   `json:"poor,omitempty"`
	Format   int    `json:"format,omitempty"`
	Width    int    `json:"width,omitempty"`
	Height   int    `json:"height,omitempty"`
	FPS      int    `json:"fps,omitempty"`
	Rate     int    `json:"rate,omitempty"`
	Channels int    `json:"channels,omitempty"`
	Samples  int    `json:"samples,omitempty"`
	Streams  int    `json:"streams,omitempty"`
	Coupled  int    `json:"coupled,omitempty"`
	Protocol int    `json:"protocol,omitempty"`
}

Event is something the host program reports.

type Host

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

Host is one running host program, for one stream.

func StartHost

func StartHost(program string, prepare func(*exec.Cmd), log *slog.Logger, sink Sink) (*Host, error)

StartHost runs the program. It holds nothing and reads nothing it's not sent: no address, key or user name ever goes on its command line, where other programs could read it.

func StartHostWith added in v0.4.0

func StartHostWith(program string, args []string, prepare func(*exec.Cmd), log *slog.Logger, sink Sink) (*Host, error)

StartHostWith is StartHost for a program that is run with arguments before its own, such as a sandbox's launcher (the server's answer to where a module's program is: path and args).

func (*Host) Begin

func (h *Host) Begin(s Start) error

Begin connects to the PC.

func (*Host) Close

func (h *Host) Close()

Close ends the stream, and the program a moment later if it hasn't gone by itself.

func (*Host) Done

func (h *Host) Done() <-chan struct{}

Done closes when the program has ended.

func (*Host) Input

func (h *Host) Input(m []byte) error

Input passes on one message a browser sent, if it's a valid one.

type Options

type Options struct {
	Program  string
	Args     []string // run before the program's own, when the program is a launcher's (a sandbox)
	Prepare  func(*exec.Cmd)
	Start    Start
	Log      *slog.Logger
	STUN     []string
	PublicIP []string
	UDPPort  int // one port for every stream, when set, for routers and containers
	// OnEnd is told once, when the stream has ended for any reason.
	OnEnd func(reason string)
}

Options set up a session.

type Session

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

Session is one stream from one PC, to one viewer at a time.

func New

func New(ctx context.Context, opts Options) (*Session, error)

New starts the host program and connects it to the PC. It returns when the stream is flowing, or says why it isn't.

func (*Session) Audio

func (s *Session) Audio(opus []byte)

Audio is the host program's next Opus packet.

func (*Session) Close

func (s *Session) Close()

Close ends the stream.

func (*Session) Counts

func (s *Session) Counts() (video, audio uint64)

Counts says how many frames and audio packets have come through.

func (*Session) Ended

func (s *Session) Ended() <-chan struct{}

Ended closes when the stream has ended.

func (*Session) Event

func (s *Session) Event(e Event)

Event is something from the host program.

func (*Session) Info

func (s *Session) Info() Event

Info is the latest thing known about the picture.

func (*Session) Input

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

Input passes on a message from the viewer.

func (*Session) Offer

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

Offer takes a browser's WebRTC offer and answers it. A new one replaces any earlier viewer, so a reload or another device simply carries on.

func (*Session) Rumble

func (s *Session) Rumble(controller, low, high uint16)

Rumble goes on to the viewer's controller.

func (*Session) Video

func (s *Session) Video(keyframe bool, ts uint32, annexB []byte)

Video is the host program's next frame.

type Sink

type Sink interface {
	Video(keyframe bool, timestamp uint32, annexB []byte)
	Audio(opus []byte)
	Rumble(controller, low, high uint16)
	Event(e Event)
}

Sink receives what the host program produces. Calls come from one goroutine.

type Start

type Start struct {
	Address     string // an IP, resolved and checked by the caller
	AppVersion  string
	GfeVersion  string
	RTSP        string
	CodecModes  int
	Width       int
	Height      int
	FPS         int
	BitrateKbps int
	PacketSize  int
	Remote      bool
	Key, IV     [16]byte
}

Start is what a host program needs to connect. The PC has already been told to start the game, over its own client protocol, by the time this is used.

Directories

Path Synopsis
Package fakehost stands in for the Moonlight host program, in tests.
Package fakehost stands in for the Moonlight host program, in tests.

Jump to

Keyboard shortcuts

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