Documentation
¶
Overview ¶
Package rtc sends the captured picture to browsers over WebRTC.
A Broadcaster owns one H.264 track and one encoder, which runs only while a viewer is connected or a tap is taking the encoded picture: the first of them taps the capture loop's raw frames, the last one leaving stops the encoder again. It also owns one speaker track, the phone's sound as G.722, encoded the same way, only while someone is connected. Every session is a peer connection with the two tracks, a "control" data channel for keyframe and bitrate requests and stats, a "hid" data channel carrying the viewer's input as the JSON /api/hid/ws takes, the viewer's microphone back as an audio track, and signaling over a WebSocket.
Index ¶
- type AudioSource
- type Broadcaster
- func (b *Broadcaster) Available() bool
- func (b *Broadcaster) Close()
- func (b *Broadcaster) EndHold(holdID string)
- func (b *Broadcaster) Frame(img image.Image, crop image.Rectangle, ts time.Duration)
- func (b *Broadcaster) OnViewerRecording(fn func())
- func (b *Broadcaster) ServeSignal(w http.ResponseWriter, r *http.Request)
- func (b *Broadcaster) SetAuto(on bool)
- func (b *Broadcaster) SetInput(hid func(ctx context.Context, msg []byte), mic func(pcm []byte) error)
- func (b *Broadcaster) Status() Status
- func (b *Broadcaster) Tap(sink SampleSink) (stop func())
- func (b *Broadcaster) ViewerRecording() bool
- type FrameSink
- type FrameSource
- type Message
- type Options
- type SampleSink
- type Session
- type Status
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type AudioSource ¶
type AudioSource interface {
Subscribe() (<-chan []byte, func())
AudioFormat() (rate, channels int)
AudioActive() bool
}
AudioSource is the phone's sound as the capture delivers it: chunks of interleaved little-endian float32 samples at AudioFormat's rate and channel count. The channel closes when the format changes or the source goes away; the reader subscribes again and reads the new format.
type Broadcaster ¶
type Broadcaster struct {
// contains filtered or unexported fields
}
Broadcaster encodes once and sends to every connected session.
func New ¶
func New(src FrameSource, opts Options) (*Broadcaster, error)
New makes a Broadcaster. It opens the UDP port now; the encoder waits for the first viewer.
func (*Broadcaster) Available ¶
func (b *Broadcaster) Available() bool
Available says whether an encoder was found, so WebRTC can be offered.
func (*Broadcaster) EndHold ¶
func (b *Broadcaster) EndHold(holdID string)
EndHold closes every session admitted on a hold: it was revoked or a newer hold took the device.
func (*Broadcaster) Frame ¶
Frame is the capture loop's hand-off: convert and queue this frame, opening the encoder at this crop's size when it is not running.
func (*Broadcaster) OnViewerRecording ¶
func (b *Broadcaster) OnViewerRecording(fn func())
OnViewerRecording has fn called each time a viewer says it is recording.
func (*Broadcaster) ServeSignal ¶
func (b *Broadcaster) ServeSignal(w http.ResponseWriter, r *http.Request)
ServeSignal is the signaling endpoint: a WebSocket whose first message is {"type":"hello"}; the Pi then sends its offer and ICE candidates, and the viewer answers with its own. The same messages travel through a station later, so nothing here depends on the WebSocket itself.
func (*Broadcaster) SetAuto ¶
func (b *Broadcaster) SetAuto(on bool)
SetAuto says whether Roadie chooses how many of the captured frames to send. It does until told otherwise; off, every frame goes out.
func (*Broadcaster) SetInput ¶
func (b *Broadcaster) SetInput(hid func(ctx context.Context, msg []byte), mic func(pcm []byte) error)
SetInput gives the broadcaster where input from viewers goes: hid takes each message of the "hid" data channel, the JSON /api/hid/ws accepts, with a context that ends when the viewer's session does, and mic takes the viewer's microphone as 16 kHz mono 16-bit samples, the way /api/mic does. Either may be nil.
func (*Broadcaster) Status ¶
func (b *Broadcaster) Status() Status
Status is the current state for /health and the control channel.
func (*Broadcaster) Tap ¶
func (b *Broadcaster) Tap(sink SampleSink) (stop func())
Tap sends the encoded picture to sink until stop is called. A tap keeps the encoder running as a viewer does, and its first unit is a keyframe as soon as the encoder can make one.
func (*Broadcaster) ViewerRecording ¶
func (b *Broadcaster) ViewerRecording() bool
ViewerRecording says whether a connected viewer is recording the stream.
type FrameSink ¶
FrameSink receives every captured frame with its crop. It must return quickly: it runs in the capture loop.
type FrameSource ¶
FrameSource is where frames come from: the capture loop.
type Message ¶
type Message struct {
Type string `json:"type"`
SDP string `json:"sdp,omitempty"`
Candidate *webrtc.ICECandidateInit `json:"candidate,omitempty"`
Error string `json:"error,omitempty"`
// Grant is the hold grant a viewer coming through a station puts in
// its hello: the rig library checks it.
Grant json.RawMessage `json:"grant,omitempty"`
}
Message is one signaling message, either way. Type is one of hello (the viewer's first message), offer, answer, candidate, bye and error.
type Options ¶
type Options struct {
// UDPPort is the one UDP port every peer connection uses (ICE on a
// mux), so a firewall needs one rule. 0 picks an ephemeral port per
// connection.
UDPPort int
// Encoder is the spec for encoder.Open: auto, v4l2m2m, ffmpeg[:codec].
Encoder string
FPS int
// Bitrate limits in bits per second; congestion control moves the
// target between them. Zero means the defaults: 300 kbps to 4 Mbps,
// starting at 1.5 Mbps.
MinBitrate, MaxBitrate, StartBitrate int
// Interfaces says which network interfaces to offer candidates on.
// nil means all but the phone's own link (usb0), docker and loopback.
Interfaces func(name string) bool
Logf func(format string, args ...any)
// OpenEncoder replaces encoder.Open, for tests.
OpenEncoder func(cfg encoder.Config) (encoder.Encoder, string, error)
// Loopback offers loopback candidates too, for tests on one machine.
Loopback bool
// Audio is the phone's sound for the speaker track; nil sends none.
Audio AudioSource
// HostTemp reads the host's temperature in °C; ok is false where it
// cannot be read. nil never holds frames back for heat.
HostTemp func() (celsius float64, ok bool)
}
Options configure a Broadcaster.
type SampleSink ¶
SampleSink takes every encoded access unit: Annex-B, with its parameter sets on keyframes, and the time its picture was captured. It runs in the encoder's pump, so it must return at once, and it must not keep data.
type Session ¶
type Session struct {
// contains filtered or unexported fields
}
Session is one viewer's peer connection.
type Status ¶
type Status struct {
Encoder string `json:"encoder"` // what Open would use, or did
Sessions int `json:"sessions"` // connected viewers
Bitrate int `json:"bitrate,omitempty"` // current target, bits per second
Width int `json:"width,omitempty"` // of the picture being encoded
Height int `json:"height,omitempty"`
FPS int `json:"fps,omitempty"` // encoded in the last second
// FPSLimit is the most frames a second being sent when some captured
// frames are held back, and LimitedBy why: link, host or floor.
FPSLimit int `json:"fps_limit,omitempty"`
LimitedBy string `json:"limited_by,omitempty"`
Dropped int `json:"dropped,omitempty"` // frames skipped by the encoder
// Audio is the speaker track's codec while sound is being sent, and
// AudioKbps its rate over the last second; Mic is the codec of the
// viewer's microphone being passed to the phone, if one is.
Audio string `json:"audio,omitempty"`
AudioKbps int `json:"audio_kbps,omitempty"`
Mic string `json:"mic,omitempty"`
MicKbps int `json:"mic_kbps,omitempty"`
}
Status is what /health reports.