2019-10-11 12:46:21 +03:00
|
|
|
/*
|
|
|
|
DESCRIPTION
|
2019-10-14 02:26:43 +03:00
|
|
|
gvctrl.go provides exported functionality of a basic API to allow programmatic
|
|
|
|
control over the GeoVision camera (namely the GV-BX4700) through the HTTP
|
|
|
|
server used for settings control. See package documentation for further
|
|
|
|
information on the API.
|
2019-10-11 12:46:21 +03:00
|
|
|
|
|
|
|
AUTHORS
|
|
|
|
Saxon A. Nelson-Milton <saxon@ausocean.org>
|
|
|
|
|
|
|
|
LICENSE
|
|
|
|
Copyright (C) 2019 the Australian Ocean Lab (AusOcean)
|
|
|
|
|
|
|
|
It is free software: you can redistribute it and/or modify them
|
|
|
|
under the terms of the GNU General Public License as published by the
|
|
|
|
Free Software Foundation, either version 3 of the License, or (at your
|
2019-10-13 14:17:50 +03:00
|
|
|
Option) any later version.
|
2019-10-11 12:46:21 +03:00
|
|
|
|
|
|
|
It is distributed in the hope that it will be useful, but WITHOUT
|
|
|
|
ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
|
|
|
|
FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
|
|
|
|
for more details.
|
|
|
|
|
|
|
|
You should have received a copy of the GNU General Public License
|
|
|
|
in gpl.txt. If not, see http://www.gnu.org/licenses.
|
|
|
|
*/
|
|
|
|
|
2019-10-14 02:26:43 +03:00
|
|
|
// Package gvctrl provides a basic API for programmatic control of the
|
|
|
|
// web based interface provided by GeoVision cameras. This API has been
|
|
|
|
// developed and tested only with the GV-BX4700, and therefore may not work
|
|
|
|
// with other models without further evolution.
|
|
|
|
//
|
|
|
|
// Settings on a GeoVision camera are updated using the Set function. One or
|
|
|
|
// more option functions may be provided to control camera function.
|
2019-10-11 12:46:21 +03:00
|
|
|
package gvctrl
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"math/rand"
|
|
|
|
"net/http"
|
|
|
|
"net/http/cookiejar"
|
|
|
|
"strconv"
|
|
|
|
"time"
|
|
|
|
)
|
|
|
|
|
2019-10-14 02:26:43 +03:00
|
|
|
// Option describes a function that will apply an option to the passed s.
|
2019-10-13 14:17:50 +03:00
|
|
|
type Option func(s settings) (settings, error)
|
2019-10-11 12:46:21 +03:00
|
|
|
|
2019-10-14 02:26:43 +03:00
|
|
|
// Set will log in to the camera at host and submit a form of settings. The
|
|
|
|
// settings form is populated with values influenced by the optional options
|
|
|
|
// passed. Available options are defined below this function.
|
|
|
|
//
|
|
|
|
// The following defaults are applied to each configurable parameter if not
|
|
|
|
// influenced by the passed options:
|
|
|
|
// codec: H264
|
|
|
|
// resolution: 640x360
|
|
|
|
// framerate: 25
|
|
|
|
// variable bitrate: off
|
|
|
|
// variable bitrate quality: good
|
|
|
|
// vbr bitrate: 250 kbps
|
|
|
|
// cbr bitrate: 512 kbps
|
|
|
|
// refresh: 2 seconds
|
2019-10-13 14:17:50 +03:00
|
|
|
func Set(host string, options ...Option) error {
|
2019-10-11 12:46:21 +03:00
|
|
|
// Randomly generate an ID our client will use.
|
|
|
|
const (
|
|
|
|
minID = 10000
|
|
|
|
maxID = 99999
|
|
|
|
)
|
|
|
|
rand.Seed(time.Now().UTC().UnixNano())
|
|
|
|
id := strconv.Itoa(maxID + rand.Intn(maxID-minID))
|
|
|
|
|
|
|
|
// Create a client with a cookie jar.
|
|
|
|
jar, err := cookiejar.New(nil)
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("could not create cookie jar, failed with error: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
client := &http.Client{
|
|
|
|
Timeout: time.Duration(5 * time.Second),
|
|
|
|
Jar: jar,
|
|
|
|
}
|
|
|
|
|
|
|
|
// Get the request body required for log in.
|
|
|
|
body, err := genLogIn(client, id, host)
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("could not generate log in request data: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Log in using generated log in request body.
|
|
|
|
err = logIn(client, id, host, body)
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("could not logIn: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Apply the options to the settings specified by the user.
|
|
|
|
s := newSettings()
|
|
|
|
for _, op := range options {
|
2019-10-12 09:57:09 +03:00
|
|
|
s, err = op(s)
|
2019-10-11 12:46:21 +03:00
|
|
|
if err != nil {
|
2019-10-13 14:17:50 +03:00
|
|
|
return fmt.Errorf("could not action Option: %v", err)
|
2019-10-11 12:46:21 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Submit the settings to the server.
|
|
|
|
err = submitSettings(client, id, host, s)
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("could not submit settings: %v", err)
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2019-10-14 02:26:43 +03:00
|
|
|
// Codec is a video codec.
|
2019-10-13 10:55:36 +03:00
|
|
|
type Codec string
|
|
|
|
|
2019-10-14 02:26:43 +03:00
|
|
|
// The avilable codecs that may be selected using CodecOut below.
|
2019-10-13 10:55:36 +03:00
|
|
|
const (
|
|
|
|
CodecH265 Codec = "28"
|
|
|
|
CodecH264 Codec = "10"
|
|
|
|
CodecMJPEG Codec = "4"
|
|
|
|
)
|
|
|
|
|
2019-10-14 02:26:43 +03:00
|
|
|
// CodecOut will set the video codec outputted by the camera. The available
|
|
|
|
// codec options are listed above as consts.
|
2019-10-13 14:17:50 +03:00
|
|
|
func CodecOut(c Codec) Option {
|
2019-10-12 09:57:09 +03:00
|
|
|
return func(s settings) (settings, error) {
|
2019-10-11 12:46:21 +03:00
|
|
|
switch c {
|
2019-10-13 10:55:36 +03:00
|
|
|
case CodecH265, CodecH264, CodecMJPEG:
|
2019-10-11 12:46:21 +03:00
|
|
|
s.codec = c
|
2019-10-12 09:57:09 +03:00
|
|
|
return s, nil
|
2019-10-11 12:46:21 +03:00
|
|
|
default:
|
2019-10-13 10:55:36 +03:00
|
|
|
return s, fmt.Errorf("unknown Codec: %v", c)
|
2019-10-11 12:46:21 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-10-14 02:26:43 +03:00
|
|
|
// Height will set the height component of the video resolution. Available
|
|
|
|
// heights are 256, 360 and 720.
|
2019-10-13 14:17:50 +03:00
|
|
|
func Height(h int) Option {
|
2019-10-12 09:57:09 +03:00
|
|
|
return func(s settings) (settings, error) {
|
2019-10-12 08:22:35 +03:00
|
|
|
v, ok := map[int]string{256: res256, 360: res360, 720: res720}[h]
|
2019-10-11 12:46:21 +03:00
|
|
|
if !ok {
|
2019-10-12 09:57:09 +03:00
|
|
|
return s, fmt.Errorf("invalid display height: %d", h)
|
2019-10-11 12:46:21 +03:00
|
|
|
}
|
|
|
|
s.res = v
|
2019-10-12 09:57:09 +03:00
|
|
|
return s, nil
|
2019-10-11 12:46:21 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-10-14 02:26:43 +03:00
|
|
|
// FrameRate will set the frame rate of the video. This value is defined in
|
|
|
|
// units of frames per second, and must be between 1 and 30 inclusive.
|
2019-10-13 14:17:50 +03:00
|
|
|
func FrameRate(f int) Option {
|
2019-10-12 09:57:09 +03:00
|
|
|
return func(s settings) (settings, error) {
|
2019-10-11 12:46:21 +03:00
|
|
|
if 1 > f || f > 30 {
|
2019-10-12 09:57:09 +03:00
|
|
|
return s, fmt.Errorf("invalid frame rate: %d", f)
|
2019-10-11 12:46:21 +03:00
|
|
|
}
|
|
|
|
s.frameRate = strconv.Itoa(f * 1000)
|
2019-10-12 09:57:09 +03:00
|
|
|
return s, nil
|
2019-10-11 12:46:21 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-10-14 02:26:43 +03:00
|
|
|
// VariableBitrate with b set true will turn on variable bitrate video and
|
|
|
|
// with b set false will turn off variable bitrate (resulting in constant bitrate).
|
|
|
|
func VariableBitrate(b bool) Option {
|
2019-10-12 09:57:09 +03:00
|
|
|
return func(s settings) (settings, error) {
|
2019-10-11 12:46:21 +03:00
|
|
|
s.vbr = "0"
|
|
|
|
if b {
|
|
|
|
s.vbr = "1"
|
|
|
|
}
|
2019-10-12 09:57:09 +03:00
|
|
|
return s, nil
|
2019-10-11 12:46:21 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-10-14 02:26:43 +03:00
|
|
|
// Quality defines an average quality of video from the camera.
|
2019-10-13 10:55:36 +03:00
|
|
|
type Quality string
|
|
|
|
|
2019-10-14 02:26:43 +03:00
|
|
|
// The available video qualities under variable bitrate. NB: it is not known
|
|
|
|
// what bitrates these correspond to.
|
2019-10-13 10:55:36 +03:00
|
|
|
const (
|
2019-10-13 14:17:50 +03:00
|
|
|
QualityStandard Quality = "4"
|
|
|
|
QualityFair Quality = "3"
|
2019-10-13 10:55:36 +03:00
|
|
|
QualityGood Quality = "2"
|
2019-10-13 14:17:50 +03:00
|
|
|
QualityGreat Quality = "1"
|
|
|
|
QualityExcellent Quality = "0"
|
2019-10-13 10:55:36 +03:00
|
|
|
)
|
|
|
|
|
2019-10-14 02:26:43 +03:00
|
|
|
// VBRQuality will set the average quality of video under variable bitrate.
|
|
|
|
// The quality may be chosen from standard to excellent, as defined above.
|
2019-10-13 14:17:50 +03:00
|
|
|
func VBRQuality(q Quality) Option {
|
2019-10-12 09:57:09 +03:00
|
|
|
return func(s settings) (settings, error) {
|
2019-10-11 12:46:21 +03:00
|
|
|
switch q {
|
2019-10-13 10:55:36 +03:00
|
|
|
case QualityStandard, QualityFair, QualityGood, QualityGreat, QualityExcellent:
|
2019-10-11 12:46:21 +03:00
|
|
|
s.quality = q
|
2019-10-12 09:57:09 +03:00
|
|
|
return s, nil
|
2019-10-11 12:46:21 +03:00
|
|
|
default:
|
2019-10-13 10:55:36 +03:00
|
|
|
return s, fmt.Errorf("invalid Quality: %v", q)
|
2019-10-11 12:46:21 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-10-14 02:26:43 +03:00
|
|
|
// VBRBitrate will set the maximal bitrate when the camera is set to variable
|
|
|
|
// bitrate. The possible values of maximal bitrate in kbps are predefined (by
|
|
|
|
// the camera) as: 250, 500, 750, 1000, 1250, 1500, 1750, 2000, 2250 and 2500.
|
|
|
|
// If the passed rate does not match one of these values, the closest value is
|
|
|
|
// selected.
|
|
|
|
func VBRBitrate(r int) Option {
|
2019-10-13 10:41:49 +03:00
|
|
|
return func(s settings) (settings, error) {
|
|
|
|
var vbrRates = []int{250, 500, 750, 1000, 1250, 1500, 1750, 2000, 2250, 2500}
|
|
|
|
if s.vbr == "1" {
|
2019-10-14 02:26:43 +03:00
|
|
|
s.vbrBitrate = convRate(r, vbrRates)
|
2019-10-13 10:41:49 +03:00
|
|
|
return s, nil
|
|
|
|
}
|
|
|
|
return s, nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-10-14 02:26:43 +03:00
|
|
|
// CBRBitrate will select the bitrate when the camera is set to constant bitrate.
|
|
|
|
// The possible values of bitrate are predefined for each resolution as follows:
|
|
|
|
// 256p: 128, 256, 512, 1024
|
|
|
|
// 360p: 512, 1024, 2048, 3072
|
|
|
|
// 720p: 1024, 2048, 4096, 6144
|
|
|
|
// If the passed rate does not align with one of these values, the closest
|
|
|
|
// value is selected.
|
|
|
|
func CBRBitrate(r int) Option {
|
2019-10-12 09:57:09 +03:00
|
|
|
return func(s settings) (settings, error) {
|
2019-10-11 12:46:21 +03:00
|
|
|
var (
|
|
|
|
cbrRates256 = []int{128, 256, 512, 1024}
|
|
|
|
cbrRates360 = []int{512, 1024, 2048, 3072}
|
|
|
|
cbrRates720 = []int{1024, 2048, 4096, 6144}
|
|
|
|
)
|
|
|
|
|
|
|
|
switch s.res {
|
2019-10-12 08:22:35 +03:00
|
|
|
case res720:
|
2019-10-14 02:26:43 +03:00
|
|
|
s.cbrBitrate = convRate(r, cbrRates720)
|
2019-10-12 08:22:35 +03:00
|
|
|
case res360:
|
2019-10-14 02:26:43 +03:00
|
|
|
s.cbrBitrate = convRate(r, cbrRates360)
|
2019-10-12 08:22:35 +03:00
|
|
|
case res256:
|
2019-10-14 02:26:43 +03:00
|
|
|
s.cbrBitrate = convRate(r, cbrRates256)
|
2019-10-11 12:46:21 +03:00
|
|
|
default:
|
|
|
|
panic("bad resolution")
|
|
|
|
}
|
2019-10-12 09:57:09 +03:00
|
|
|
return s, nil
|
2019-10-11 12:46:21 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-10-14 02:26:43 +03:00
|
|
|
// Refresh will set the intra refresh period. The passed value is in seconds and
|
|
|
|
// must be between .25 and 5 inclusive. The value will be rounded to the nearest
|
|
|
|
// value divisible by .25 seconds.
|
2019-10-13 14:17:50 +03:00
|
|
|
func Refresh(r float64) Option {
|
2019-10-12 09:57:09 +03:00
|
|
|
return func(s settings) (settings, error) {
|
2019-10-12 08:09:27 +03:00
|
|
|
const (
|
|
|
|
maxRefreshPeriod = 5
|
|
|
|
minRefreshPeriod = .25
|
|
|
|
)
|
|
|
|
|
|
|
|
if minRefreshPeriod > r || r > maxRefreshPeriod {
|
2019-10-12 09:57:09 +03:00
|
|
|
return s, fmt.Errorf("invalid refresh period: %g", r)
|
2019-10-12 08:09:27 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
refOptions := []int{250, 500, 1000, 1500, 2000, 2500, 3000, 3500, 4000, 4500, 5000}
|
2019-10-12 09:18:35 +03:00
|
|
|
s.refresh = strconv.Itoa(refOptions[closestValIdx(int(r*1000), refOptions)])
|
2019-10-12 09:57:09 +03:00
|
|
|
return s, nil
|
2019-10-11 12:46:21 +03:00
|
|
|
}
|
|
|
|
}
|