Documentation
¶
Overview ¶
Package stream joins one camera to one hub: dial, depacketize, mux, publish, on repeat until the context is canceled or something fails.
Index ¶
- Constants
- Variables
- func NativeInfo(ctx context.Context, address, username, password, stream string) (codec string, width, height int, err error)
- func Run(ctx context.Context, cfg Config, h *hub.Hub) (err error)
- func SetRecorderSharing(perConnection int)
- func ZoomInfo(ctx context.Context, address, username, password string) (codec string, width, height int, err error)
- type Config
- type ConnectionSink
- type FrameSink
- type SpeakerSink
Constants ¶
const DefaultMediaTimeout = 30 * time.Second
DefaultMediaTimeout bounds how long Run tolerates zero decoded video frames before giving up on the connection and returning an error, so the supervisor tears it down and reconnects.
This exists because internal/baichuan's idle-read timeout does not catch the failure that actually happened in production on 2026-09-08: a camera holding a stale session from a client that died without sending stream-stop still answers a new login, hands out a valid nonce, and keeps the connection responsive at the byte level (it acks other protocol traffic), while never sending a single media frame. Bytes kept arriving, so the idle timeout's read deadline kept getting pushed out, and two streams sat reporting connected:true, 0 fps, restarts:0 for over 25 seconds with no reconnection attempt before this was caught by hand. Byte liveness and media liveness are different facts about a connection, and only media liveness is what this package's job depends on.
30 seconds is chosen from the fleet measurements taken the same day, against the whole running fleet, not a lab bench: every healthy stream observed ran between 9.9 and 25.1 fps with last_frame_age_seconds consistently under 0.1, including a 4K main stream whose keyframes span several messages and a substream under load. None of that comes anywhere close to even a single second of genuine gap between decoded frames. 30s is a full order of magnitude above the worst observed gap, not a value tuned close to it, so it will not fire on a camera stuttering under load, a keyframe taking longer to reassemble, or ordinary network jitter. It is also double DefaultIdleTimeout: a fully silent camera (no bytes at all, not even keepalive traffic) is always caught by the idle-read timeout first, since that fires at 15s: this timeout exists specifically to catch the case the idle timeout structurally cannot, a connection with traffic but no media, so it does not need to race it and can afford to be patient. The production incident this responds to ran for minutes before anyone noticed; catching it in 30s instead is a large improvement without risking a reconnect storm on a healthy fleet.
Variables ¶
var ErrPrivacy = errors.New("the camera is in privacy mode")
ErrPrivacy is a stream that cannot run because the camera is in privacy mode (E1 series): it turns down the preview with status 425, or reports sleep 1 on its own (sleep status report, 623) and stops sending video. Seen in a reocapture log of an E1 Pro (issue #13, 2026-10-25).
ErrZoomUnavailable is a zoom stream that sent no video over Baichuan and that the camera's RTSP did not serve either. Usually the camera's main stream slots (two, device-wide) are taken by other clients and its RTSP is off, which retrying every few seconds will not change, so the supervisor waits longer before the next attempt (see retryAfter).
Functions ¶
func NativeInfo ¶ added in v0.2.0
func NativeInfo(ctx context.Context, address, username, password, stream string) (codec string, width, height int, err error)
NativeInfo asks the camera's own RTSP server what one native stream is, without playing it: its codec and picture size from the DESCRIBE answer. An error means the camera does not serve it (or RTSP is off).
func Run ¶
Run dials cfg's camera, starts its video stream, and publishes muxed MPEG-TS to h until ctx is canceled or an error occurs. It never retries: retry and backoff belong to the supervisor that runs many of these, since a runner that retries internally would hold the camera's one connection for that stream and block its own replacement from ever connecting.
Run returns promptly on an already canceled context without dialling, so a supervisor can cancel a Config before Run has had a chance to start it.
func SetRecorderSharing ¶ added in v0.2.5
func SetRecorderSharing(perConnection int)
SetRecorderSharing sets how many streams one connection to an NVR or Home Hub carries (baichuan.Pool.MaxPreviews). Zero gives every stream a connection of its own, as every release before this one did; negative leaves the default. Call it before any stream starts.
func ZoomInfo ¶ added in v0.2.2
func ZoomInfo(ctx context.Context, address, username, password string) (codec string, width, height int, err error)
ZoomInfo reports a camera's zoom lens: its codec and picture size, from the first video frame the camera sends for it over Baichuan, falling back to the camera's own RTSP server (NativeInfo). An error means the camera has no zoom lens reostream can reach. ctx bounds the whole attempt.
Types ¶
type Config ¶
type Config struct {
Name string
Address string
Username string
Password string
Stream string // "main", "sub" or "extern"
Channel int // the camera's channel on a hub or NVR; 0 on its own
UID string // a battery camera's UID; empty for one on TCP
// Sleeps is a battery camera, with a UID or behind a Home Hub (see
// config.Camera.Sleeps): woken only on demand.
Sleeps bool
// HoldPicture is the camera's hold_picture setting (see config.Camera).
HoldPicture bool
// Rotate is the camera's rotate setting (see config.Camera); nil
// leaves it to what the camera says at login.
Rotate *int
// IdleTimeout overrides baichuan.DefaultIdleTimeout. A battery camera
// that is awake sends within a couple of seconds, so one that does not
// is holding a session and is quicker to retry than to wait out.
IdleTimeout time.Duration
// Sink, when non-nil, receives every decoded frame before muxing. Nil
// is the default and the only state the HTTP output has ever run in.
Sink FrameSink
// MediaTimeout overrides DefaultMediaTimeout. Zero means the default.
//
// This is a Config field, not a TOML setting: unlike a per-camera value
// such as a stream name or credentials, the right timeout here is a
// property of the protocol's own frame cadence (see
// DefaultMediaTimeout's derivation from fleet-wide fps measurements),
// the same reasoning that kept baichuan.Options.IdleTimeout out of the
// TOML config. Exposing it to operators would invite "fixing" a camera
// that is actually failing by loosening the timeout that exists to
// catch exactly that, which defeats the point of the watchdog. It stays
// a Go field so tests can shorten it without a live camera or a config
// file.
MediaTimeout time.Duration
}
type ConnectionSink ¶ added in v0.1.4
type ConnectionSink interface {
FrameSink
Connected()
}
ConnectionSink is a FrameSink that also wants to know when the camera connection feeding it has been established, which it is told once per connection, before any frame from that connection.
A sink whose state outlives one connection needs this. The RTSP output is one: its session, and the timestamp clock behind it, stay up while the camera reconnects, and after a camera reboot the camera's microsecond counter has started over. A clock that is not told treats that as either a stall or a wrap, and a reader sees timestamps that freeze for up to 35 minutes or jump forward by up to 71. The HTTP output keeps its muxer, and so its clock, across connections too, and resumes it itself (tsOutput).
type FrameSink ¶
Config names one camera stream to run. FrameSink receives every frame a stream decodes, before it is muxed.
It exists so an output that needs frames rather than TS bytes, such as RTSP, can have them without the hub carrying frames and without demuxing TS back into them. Implementations must not block: this runs on the camera goroutine, and anything slow here stalls the read loop that the media watchdog is watching.
type SpeakerSink ¶ added in v0.1.9
type SpeakerSink interface {
SetSpeaker(bool)
}
SpeakerSink is a sink that offers two-way audio and so needs to know, before the first frame, whether the camera has a speaker.