cgi

package
v0.2.16 Latest Latest
Warning

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

Go to latest
Published: Dec 1, 2026 License: AGPL-3.0 Imports: 10 Imported by: 0 modules, 0 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

Documentation

Overview

Package cgi talks to a Reolink camera's HTTP API.

Baichuan is the better transport and carries everything reostream needs for streaming, but a handful of camera settings have no Baichuan message id we have recovered: the fisheye view modes and the dual lens stitch parameters are both reachable only here for now. The message ids exist, since the official NVR sets both over Baichuan; finding them needs a capture of an NVR talking to a camera, or disassembly of the dispatch code, and neither is done. This package is what makes those features usable meanwhile.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func AudioAlarmEnabled added in v0.1.4

func AudioAlarmEnabled(doc map[string]any) bool

AudioAlarmEnabled reads the on/off out of that document.

func Unreachable added in v0.2.9

func Unreachable(err error) bool

Unreachable reports whether err is Dial finding no web API on the camera, as opposed to one that answered and refused the login.

Types

type AudioCfg added in v0.1.4

type AudioCfg struct {
	Volume int `json:"volume"`
}

AudioCfg is the camera's speaker volume.

type Client

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

Client is a logged in session against one camera.

The credentials are kept because a camera will intermittently forget a session it has just issued, and the only way through that is to log in again.

func Dial

func Dial(host, user, password string) (*Client, error)

Dial logs in and returns a session, with no deadline of its own beyond the 20 second per-call timeout every Client carries. DialContext is the same call with a caller-supplied bound; this is that call with context.Background(), for the many call sites that predate ctx threading through this package and have no deadline of their own to hand down.

The password goes in the request body. Passing it in the query string instead fails with "password wrong", which reads like a credential problem and is not.

func DialContext

func DialContext(ctx context.Context, host, user, password string) (*Client, error)

DialContext is Dial with a context that bounds the login call itself, so a caller with its own deadline (a fleet apply's per-camera timeout, for one) can have it actually interrupt a hung login rather than only bound the gap between calls.

It tries HTTP first, then HTTPS. Newer firmware ships with HTTP turned off, and HTTPS uses a self-signed certificate, so it is not verified. Only a failure to reach the API falls through to HTTPS; a camera that answers and refuses the login is reported as it is.

func (*Client) Close added in v0.1.4

func (c *Client) Close() error

Close hands the session back to the camera.

This is not housekeeping, it is the difference between a UI that works and one that stops: a Reolink camera allows only a handful of concurrent CGI logins, and it holds an abandoned one until its own timeout, which is minutes. Every page that reads a setting dials a client, so without this a few minutes of ordinary browsing exhausts the camera and every subsequent read fails with "max session" -- at which point the camera also refuses the Reolink app and its own web UI, not just this one.

Failures are returned but rarely worth acting on: the session is being dropped either way, and a Logout that did not arrive is exactly the leak that would have happened anyway. Safe on a nil or already-closed client so it can sit under an unconditional defer.

func (*Client) Get

func (c *Client) Get(ctx context.Context, cmd string, channel int) (value, initial, rng json.RawMessage, err error)

Get runs a read command. action 1 asks the camera to return the valid range and the factory default alongside the current value, which is what makes a setting adjustable without guessing at bounds.

ctx bounds this call and, if the session needs a retry, the login that follows it: canceling ctx aborts whichever of those is in flight, rather than only being honored between round trips the way a caller-side context.WithTimeout that this method never looked at would be.

func (*Client) GetAudioAlarm added in v0.1.4

func (c *Client) GetAudioAlarm(ctx context.Context, channel int) (map[string]any, error)

GetAudioAlarm reads the alarm sound's whole document.

A map rather than a struct, the same discipline readFloodlightDoc uses: the document carries a schedule of 168 characters per detection type -- one per hour of the week -- and the types differ by model. The fisheye sends AI_PEOPLE, AI_VEHICLE and MD; the pano sends crossline and intrusion entries instead. Nothing here models that, and nothing here may drop it, so the caller changes one field on top of what the camera sent.

func (*Client) GetAudioCfg added in v0.1.4

func (c *Client) GetAudioCfg(ctx context.Context, channel int) (AudioCfg, VolumeRange, error)

GetAudioCfg reads the speaker volume and the range it may take.

func (*Client) GetEnc added in v0.1.4

func (c *Client) GetEnc(ctx context.Context, channel int) (current Enc, options []EncOptions, err error)

GetEnc reads the encoder settings and every combination the camera says it will accept.

The range is the point of this call. Without it a form has to offer free number boxes and learn what the camera refuses by refusing to work, and the fields are not independent -- see EncOptions.

func (*Client) GetFishEye

func (c *Client) GetFishEye(ctx context.Context, channel int) (FishEye, error)

GetFishEye reads the fisheye view configuration.

func (*Client) GetStitch

func (c *Client) GetStitch(ctx context.Context, channel int) (current, factory Stitch, limits StitchLimits, err error)

GetStitch reads the stitch settings along with the factory defaults and the valid range, which is everything needed to adjust it without guessing.

func (*Client) Set

func (c *Client) Set(ctx context.Context, cmd string, param any) error

Set runs a write command, bounded by ctx the same way Get is.

func (*Client) SetAudioAlarmEnabled added in v0.1.4

func (c *Client) SetAudioAlarmEnabled(ctx context.Context, channel int, doc map[string]any, on bool) error

SetAudioAlarmEnabled turns the alarm sound on or off, writing the camera's own document back with that one field changed -- never a composed one, which would drop the schedule the camera keeps in it.

func (*Client) SetAudioCfg added in v0.1.4

func (c *Client) SetAudioCfg(ctx context.Context, channel, volume int) error

SetAudioCfg writes the speaker volume.

func (*Client) SetEnc added in v0.1.4

func (c *Client) SetEnc(ctx context.Context, channel int, e Enc) error

SetEnc writes the encoder settings.

This re-encodes what the camera records and what every reader of it receives. It is not a reboot like a fisheye mode change, but a recorder holding the stream will see it stop and come back with different parameters, which for an H.264 or H.265 consumer means waiting for the next keyframe at best and a reconnect at worst.

Every field is sent, not only the changed one. The camera takes the whole block, and a partial write is how a stream ends up at a resolution with a bitrate that belongs to a different one.

func (*Client) SetFishEye

func (c *Client) SetFishEye(ctx context.Context, channel int, f FishEye) error

SetFishEye changes the view mode.

**This reboots the camera.** That is normal for this setting rather than a fault, and it has consequences worth planning for: the HTTP API returns 502 or nothing for roughly 15 to 20 seconds, the auth token is invalidated so anything afterwards needs a fresh login, and any recorder consuming the camera loses the stream and reconnects. A fixed sleep is the wrong tool for waiting it out; poll until the camera answers.

It also invalidates any motion mask or detection zone drawn against the previous geometry, because those coordinates are normalized to the frame. A fisheye with zones drawn on the circular view will have them pointing at the wrong places after a switch to quad or dual.

func (*Client) SetStitch

func (c *Client) SetStitch(ctx context.Context, channel int, s Stitch) error

SetStitch writes the stitch settings. Unlike a fisheye mode change this is a numeric adjustment and does not reboot the camera.

type Enc added in v0.1.4

type Enc struct {
	Audio  int       `json:"audio"`
	Main   EncStream `json:"mainStream"`
	Sub    EncStream `json:"subStream"`
	Extern EncStream `json:"extStream"`
}

Enc is a channel's encoder settings: one block per stream, plus whether audio is encoded at all.

type EncOptions added in v0.1.4

type EncOptions struct {
	Main EncStreamOptions
	Sub  EncStreamOptions
}

EncOptions is one whole combination the camera offers.

The camera answers GetEnc's range with an ARRAY of these, one per resolution it supports, and the bitrates and frame rates inside each are only valid for that resolution. On the pano, 7680x2160 offers bitrates up to 12288 and 4096x1152 stops at 10240. So "what bitrates may I choose" has no answer until a resolution is chosen, and a form that presents the two as independent lists will offer combinations the camera refuses.

type EncStream added in v0.1.4

type EncStream struct {
	BitRate   int    `json:"bitRate"`
	FrameRate int    `json:"frameRate"`
	GOP       int    `json:"gop"`
	Width     int    `json:"width"`
	Height    int    `json:"height"`
	Size      string `json:"size"`
	Profile   string `json:"profile"`
	VType     string `json:"vType"`
}

EncStream is one stream's encoder settings.

Size is the camera's own spelling of the resolution, "4096*1152", with a star rather than an x. Width and Height repeat it as numbers; the camera sends all three and SetEnc is given all three back, because a size string that disagrees with its own numbers is a value no camera was ever asked to interpret.

GOP is the interframe space, which is the vendor's own label for it: how many frames pass between keyframes. The camera reports it in frames and the range is small -- 1 to 2 on a main stream here -- so this is not the dozens-of-frames GOP of a streaming encoder.

type EncStreamOptions added in v0.1.4

type EncStreamOptions struct {
	Size       string
	Width      int
	Height     int
	BitRates   []int
	FrameRates []int
	GOPMin     int
	GOPMax     int
	Profiles   []string
	VTypes     []string

	// Default is what the camera calls the default for this size, which is
	// not the same as the value in force.
	DefaultBitRate   int
	DefaultFrameRate int
	DefaultGOP       int
}

EncStreamOptions is what one stream will accept, as the camera reports it rather than as anything here assumes.

BitRates and FrameRates are the exact stops offered; a value not in the list is refused by the camera. Profiles and VTypes likewise. GOPMin and GOPMax bound the interframe space.

Size is the resolution these options belong to, which is the subtlety of this whole structure: see EncOptions.

type FishEye

type FishEye struct {
	ImageType     FishEyeMode `json:"imageType"`
	InstallType   int         `json:"installType"`
	RotationAngle int         `json:"rotationAngle"`
}

FishEye is the camera's current view configuration.

type FishEyeMode

type FishEyeMode int

FishEyeMode is what the camera does to the circular image before encoding it. All four produce the same frame size; modes 1 to 3 are dewarped by the camera, so a client cropping regions out of the raw circle and correcting them itself is redoing work the camera will do properly.

const (
	FishEyeRaw      FishEyeMode = 0 // the circle, uncorrected
	FishEyePanorama FishEyeMode = 1 // the circle unwrapped into a flat band
	FishEyeQuad     FishEyeMode = 2 // four dewarped sub views in a 2x2 grid
	FishEyeDual     FishEyeMode = 3 // two dewarped halves, stacked
)

func (FishEyeMode) String

func (m FishEyeMode) String() string

type Stitch

type Stitch struct {
	Distance float64 `json:"distance"`
	XMove    int     `json:"stitchXMove"`
	YMove    int     `json:"stitchYMove"`
}

Stitch is the dual lens alignment on a two lens camera.

The two fields are not two flavours of the same control. Distance is a physical measurement and the moves are offsets, and they correct different errors:

The lenses sit a few centimeters apart, so in the region where their views overlap an object appears in a different place in each image by an amount that depends on how far away it is. Near objects shift a lot, distant ones barely. No single alignment is therefore correct for a whole scene: a seam can only be made to disappear at one depth, and Distance chooses which. Objects much nearer than it duplicate across the seam, objects much further are clipped out of it.

XMove and YMove are a fixed nudge of one image against the other, correcting mechanical tolerance in how the lens modules are mounted. That error is the same whatever the camera is looking at, which is why it is a constant rather than a function of depth.

So: a seam misaligned the same way everywhere is XMove and YMove. A seam that lines up at one depth and splits at another is Distance, and no value fixes every depth at once.

type StitchLimits

type StitchLimits struct {
	Distance struct{ Min, Max float64 }
	XMove    struct{ Min, Max int }
	YMove    struct{ Min, Max int }
}

StitchLimits is the valid range for each field, as the camera reports it.

type VolumeRange added in v0.1.4

type VolumeRange struct {
	Min int `json:"min"`
	Max int `json:"max"`
}

VolumeRange is what the camera says a volume may be. It is read rather than assumed for the same reason the encoder's stops are: 0 to 100 is what these two cameras answer, and a third camera has not been asked.

Jump to

Keyboard shortcuts

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