Documentation
¶
Overview ¶
Package ctlserver is goav's control plane: a running task's capabilities, served to clients over a socket or HTTP. A host serves its task; an operator, a dashboard, or goav ctl connects and inspects it, attaches and detaches outputs written in the pipeline language, and drives its controls.
server, err := ctlserver.New(task, ctlserver.Files(dir))
go server.ListenAndServe(ctx, "unix:///run/goav.sock")
client, err := ctlserver.Dial(ctx, "unix:///run/goav.sock")
stats, err := client.Call[pipeline.GraphStats](ctx, "stats", nil)
_, err = client.Call[ctlserver.Attached](ctx, "attach", map[string]string{
"program": "tap live ! resize 640x360 ! encode vp8 ! as preview ! file preview.webm",
})
Protocol ¶
JSON-RPC 2.0 over one connection (unix:///path or tcp://host:port), one JSON value per message: requests carry ids and run concurrently (past 32 in flight the server stops reading, backpressuring the client), a batch array runs its requests in order, a {"method":"$/cancelRequest","params": {"id":7}} notification cancels call 7, and a watch pushes {"method":"event"} notifications on the same connection. Server is also an http.Handler: POST a request, GET .../events for server-sent events. capabilities reports the protocol version, which changes only when the wire breaks. A refusal is an Error whose Data carries goav's structured detail: a BuildError's code, node, details, and fixes, or a program's line, column, and hint.
Methods ¶
Inspect: describe [format=json|text|dot|mermaid], explain, snapshot, stats, taps, capabilities. Watch: watch [types= stream=], unwatch subscription=. Controls: bitrate stream= bitrate=, keyframe stream=, select active=, seek position= [source=], rate value=, segment start= end= [source=], pause, resume. Change the graph: attach program=, rebranch attachment= program= [switch=now|next-frame|next-keyframe|<position>] [old=close|drain|abort], detach attachment= [outcome=close|drain|abort]. Stop: stop. Method adds a host's own, typed. detach and rebranch address the attachments made through the server; the host's own stay the host's.
An attached program starts at a tap (tap live ! ...) and may use the elements Elements adds; it reads and writes files only through the FileSystem Files grants, so a server for untrusted clients grants a launch.Dir or none. A tcp server authenticates its clients with a Token.
Cost ¶
Serving costs the task's data plane nothing: calls run on connection goroutines, stats are atomic reads, and a watch is an inspect subscription that sheds events when its client falls behind instead of blocking media.
Index ¶
Constants ¶
const ( CodeParse = -32700 CodeInvalidRequest = -32600 CodeMethodNotFound = -32601 CodeInvalidParams = -32602 CodeRefused = -32000 // authenticate with its token. CodeUnauthorized = -32001 )
JSON-RPC error codes; Refused carries goav's own refusal in Data.
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type Attached ¶
type Attached struct {
Attachment string `json:"attachment"`
}
Attached is what attach and rebranch return: the attachment's name, for detach and rebranch.
type Capabilities ¶
type Capabilities struct {
Protocol int `json:"protocol"`
Methods []MethodInfo `json:"methods"`
Elements []launch.ElementInfo `json:"elements"`
}
Capabilities is what capabilities returns: the wire protocol's version, which changes only when the wire breaks, the server's methods, and the elements attached programs may use.
type Client ¶
type Client struct {
// contains filtered or unexported fields
}
Client calls a ctl server over one connection; calls may run concurrently, and watches share the connection.
func Dial ¶
Dial connects to a server at unix:///path or tcp://host:port, and authenticates with the token a tcp://TOKEN@host:port address carries (see Token).
func (*Client) Call ¶
Call calls method with params (a struct or map, or nil) and returns its result as a T; json.RawMessage keeps it raw:
stats, err := client.Call[pipeline.GraphStats](ctx, "stats", nil)
_, err = client.Call[ctlserver.Attached](ctx, "attach", map[string]string{"program": "tap live ! null"})
A refusal is a *Error, its Data carrying goav's structured detail.
type Error ¶
type Error struct {
Code int `json:"code"`
Message string `json:"message"`
Data json.RawMessage `json:"data,omitempty"`
}
Error is a refused call: a JSON-RPC code and message, and for goav's own refusals the structured detail — a *goav.BuildError's code, node, details, and fixes, or a program's line, column, and hint.
type Event ¶
type Event struct {
Type av.EventType `json:"type"`
Stream av.StreamID `json:"stream,omitempty"`
Epoch av.Epoch `json:"epoch,omitempty"`
At time.Time `json:"at,omitzero"`
Position string `json:"position,omitempty"`
Track *Track `json:"track,omitempty"`
Reason string `json:"reason,omitempty"`
Cause string `json:"cause,omitempty"`
Metadata av.Metadata `json:"metadata,omitempty"`
}
Event is a task event as the wire carries it. Track describes the stream a stream_added or codec_changed event announces.
type MethodInfo ¶
type MethodInfo struct {
Name string `json:"name"`
Summary string `json:"summary"`
Params []Param `json:"params,omitempty"`
}
MethodInfo describes one method.
type Option ¶
Option configures a Server.
func Files ¶
func Files(fs launch.FileSystem) Option
Files lets programs attached through the server read and write files through fs. Without it, attached programs touch no files; a host serving untrusted clients grants a launch.Dir.
func Method ¶
func Method[T any](name, summary string, fn func(context.Context, goav.Task, T) (any, error)) Option
Method adds a host method. Its params are T's fields, named by their json tags; `ctl:"required"` marks a required one and `help:"..."` describes it. A client's call runs fn and returns its result as JSON.
func Token ¶
Token makes every client authenticate with token before its first call: a connection whose first request is not {"method":"$/authenticate", "params":{"token":...}} with it is answered CodeUnauthorized and closed, and an HTTP request needs "Authorization: Bearer <token>". Dial sends it from the address (tcp://TOKEN@host:port). ListenAndServe refuses tcp without a Token: a port is reachable by every local user, and more.
type Param ¶
type Param struct {
Name string `json:"name"`
Type string `json:"type"`
Required bool `json:"required,omitempty"`
Help string `json:"help,omitempty"`
}
Param describes one parameter of a method, for capabilities and help.
type Server ¶
type Server struct {
// contains filtered or unexported fields
}
Server serves one running task's control methods to clients. It does no work on the task's data plane: a call runs on its connection's goroutine, and a watch is an inspect subscription that sheds events rather than block media when its client falls behind.
func (*Server) ListenAndServe ¶
ListenAndServe serves clients at address — unix:///path/to.sock or tcp://host:port — until ctx ends. A unix socket is owner-only from the moment it exists; ListenAndServe replaces a stale one, and refuses a path another server is serving or that is not a socket.
func (*Server) Serve ¶
Serve serves clients on l until ctx ends, then closes l and waits for each connection's calls in flight to answer.
func (*Server) ServeHTTP ¶
func (s *Server) ServeHTTP(w http.ResponseWriter, r *http.Request)
ServeHTTP serves the methods over HTTP, for mounting the control plane in an existing service (behind its own authentication, or a Token): POST a JSON-RPC request and receive its response; GET .../events streams the task's events as server-sent events, filtered by ?types=a,b and ?stream=id.
type Track ¶
type Track struct {
Index int `json:"index"`
Type av.MediaType `json:"type"`
Codec av.CodecID `json:"codec"`
Profile string `json:"profile,omitempty"`
Width int `json:"width,omitempty"`
Height int `json:"height,omitempty"`
SampleRate int `json:"sampleRate,omitempty"`
Channels int `json:"channels,omitempty"`
Language string `json:"language,omitempty"`
Name string `json:"name,omitempty"`
}
Track is a stream's identity and shape, as an event announces it.