Documentation
¶
Overview ¶
Package supervisor keeps every configured camera stream running, one goroutine per stream, restarting it with backoff when it fails.
A camera's main, sub and extern streams are independent Baichuan connections, so they run on separate goroutines rather than being serialized behind one lock per camera; a stall on one must never delay the others.
Index ¶
- Variables
- type ReloadResult
- type Runner
- type StreamStat
- type Supervisor
- func (s *Supervisor) AttachPresence(presenceFor func(camera string, h *hub.Hub) stream.FrameSink)
- func (s *Supervisor) AttachSinks(sinkFor func(camera, stream string) stream.FrameSink)
- func (s *Supervisor) CameraConfig(name string) (stream.Config, bool)
- func (s *Supervisor) Hub(name string) (*hub.Hub, bool)
- func (s *Supervisor) HubNames() []string
- func (s *Supervisor) Motion(camera string, on bool) bool
- func (s *Supervisor) PrivacyOff(camera string)
- func (s *Supervisor) Reload(cams []config.Camera) (ReloadResult, error)
- func (s *Supervisor) Run(ctx context.Context) error
- func (s *Supervisor) SetMotionHook(f func(camera string, on bool))
- func (s *Supervisor) SetStillDir(dir string)
- func (s *Supervisor) Stats() []StreamStat
- func (s *Supervisor) Validate(cams []config.Camera) error
Constants ¶
This section is empty.
Variables ¶
var ErrAlreadyRunning = errors.New("supervisor: already running")
ErrAlreadyRunning is returned by a second call to Run on the same Supervisor. Two concurrent Run calls would spawn two goroutines per entry, which is exactly the two-connections-to-one-stream overlap this package exists to prevent, so a second call is refused rather than silently duplicating the fleet.
var ErrNotRunning = errors.New("supervisor: not running")
ErrNotRunning is returned by Reload before Run has started. There is no context to start a stream under yet, and silently deferring the change until Run would make a reload that appeared to succeed do nothing.
var ErrShuttingDown = errors.New("supervisor: shutting down")
ErrShuttingDown is returned by Reload once Run's context has been canceled. Run's final wait (see Run) only waits for entries still in the map at the moment it checks; a Reload allowed to start after ctx is already done could still be mid-diff or mid-stop when Run returns and the process exits out from under it, which is exactly what leaves a stream-stop message unwaited. Refusing outright, rather than racing it to completion, closes that window for any Reload that has not already begun; see Run's own reloadMu wait for the one that has.
Functions ¶
This section is empty.
Types ¶
type ReloadResult ¶
ReloadResult reports what a Reload did, for the operator page to show and for a test to assert on.
type Runner ¶
Runner runs one camera stream until ctx is canceled or it fails. It is a parameter rather than a hard call to stream.Run so the supervisor can be tested without a camera.
type StreamStat ¶
type StreamStat struct {
Camera string
Stream string
Running bool
LastError string
LastErrorAt time.Time
Restarts int
// OnDemand marks a battery camera's stream, which is asleep rather
// than down when it is not running.
OnDemand bool
// Privacy marks a stream whose camera is in privacy mode
// (stream.ErrPrivacy): off on purpose, not failing.
Privacy bool
// contains filtered or unexported fields
}
StreamStat reports one stream's current state for status endpoints.
LastError is history, not a diagnosis: it is the last error this stream ever hit and is never cleared, because a stream that failed and recovered is exactly what someone reading a status page needs to know about. That makes it dangerous on its own, which is what LastErrorAt is for. On 2026-10-16 a fisheye substream carried "camera is answering but not sending media (held session)" from a single restart hours earlier while streaming at 9.8 fps with a frame 0.1s old, and the stale sentence was read as a live fault and reported as a dead feed.
type Supervisor ¶
type Supervisor struct {
// contains filtered or unexported fields
}
Supervisor runs every stream from a camera list and restarts failed ones.
func New ¶
func New(cams []config.Camera, run Runner) *Supervisor
New builds a Supervisor for cams, using run to drive each stream. Nothing starts until Run is called.
func (*Supervisor) AttachPresence ¶ added in v0.2.0
AttachPresence gives each battery camera's main stream the sink built by presenceFor. Call before Run, like AttachSinks.
func (*Supervisor) AttachSinks ¶
func (s *Supervisor) AttachSinks(sinkFor func(camera, stream string) stream.FrameSink)
AttachSinks gives every stream named in a camera's rtsp list a frame sink, built by sinkFor.
It takes a factory rather than an RTSP server so this package does not depend on that one: the supervisor's job is running camera connections, and which outputs consume them is not its concern. A camera with no rtsp entry keeps a nil sink, which is the state the HTTP path has always run in.
Call before Run. It panics afterwards rather than racing a live fleet, matching setBackoff.
func (*Supervisor) CameraConfig ¶ added in v0.1.9
func (s *Supervisor) CameraConfig(name string) (stream.Config, bool)
CameraConfig returns the connection details the supervisor currently holds for camera name, reflecting any Reload since startup.
func (*Supervisor) Hub ¶
func (s *Supervisor) Hub(name string) (*hub.Hub, bool)
Hub returns one stream's hub by its "<camera>/<stream>" key. Locked because Reload adds and removes entries while the HTTP server is serving.
func (*Supervisor) HubNames ¶
func (s *Supervisor) HubNames() []string
HubNames lists every stream currently configured.
func (*Supervisor) Motion ¶ added in v0.2.3
func (s *Supervisor) Motion(camera string, on bool) bool
Motion tells a battery camera's main stream that the camera reports motion (on) or that the motion is over (off). While it lasts the stream wakes with nobody watching, so the presence stream, and whatever records it, carries the event live instead of the last still; wakeMax still caps the session. It reports whether camera is a battery camera with a main stream; a wired camera is never asleep, so it has nothing to do there.
func (*Supervisor) PrivacyOff ¶ added in v0.2.3
func (s *Supervisor) PrivacyOff(camera string)
PrivacyOff tells camera's streams that privacy mode was just turned off, so they try again now rather than at the next check.
func (*Supervisor) Reload ¶
func (s *Supervisor) Reload(cams []config.Camera) (ReloadResult, error)
Reload brings the running fleet in line with cams, touching only what changed. A stream whose camera is unchanged keeps its connection, its hub and its subscribers: adding a ninth camera must not interrupt the other eight, which is the entire reason this exists rather than a process restart.
A stream that is being replaced is stopped to completion before its replacement starts. Overlapping them gets the replacement a session the camera refuses, because the old one has not been released yet.
func (*Supervisor) Run ¶
func (s *Supervisor) Run(ctx context.Context) error
Run starts one goroutine per configured stream and blocks until ctx is canceled and every one of them has finished closing.
Run may only be called once. A second concurrent call would spawn a second goroutine per entry, which is a second connection to every stream this Supervisor owns, the exact fault this package exists to prevent, so it returns ErrAlreadyRunning instead.
func (*Supervisor) SetMotionHook ¶ added in v0.2.4
func (s *Supervisor) SetMotionHook(f func(camera string, on bool))
SetMotionHook has f told when a battery camera's wake turns motion on (on) and when that motion ends, so it can go out like a wired camera's own alerts. f must not block.
func (*Supervisor) SetStillDir ¶ added in v0.2.0
func (s *Supervisor) SetStillDir(dir string)
SetStillDir keeps each battery camera's last picture in dir, so a restart does not leave its tile and its presence stream with nothing to show. Call before Run.
func (*Supervisor) Stats ¶
func (s *Supervisor) Stats() []StreamStat
Stats returns a snapshot of every stream's current state.
func (*Supervisor) Validate ¶
func (s *Supervisor) Validate(cams []config.Camera) error
Validate reports whether cams would be accepted by Reload, without applying anything or requiring Run to have started.
This exists so a caller writing a config file to disk, such as the control page's config editor, can check it against the running fleet's own constraints BEFORE the write, not just against config.Config's context-free rules. The two disagree in one real case: an [rtsp] section added to a daemon that booted without one passes config.Load (the file is self-consistent) but fails this exact check, because RTSP was never wired up on this Supervisor and cfg.RTSP stays nil here regardless of what the file says. Catching that before the file is written, rather than after, is what stops "saved, but not applied" from leaving a config on disk that the daemon's next restart cannot even boot from.