mirror of https://bitbucket.org/ausocean/av.git
244 lines
5.3 KiB
Go
244 lines
5.3 KiB
Go
/*
|
|
DESCRIPTION
|
|
gvctrl.go provides a programmatic interface for controlling the HTTP based
|
|
server hosted by GeoVision cameras for settings control.
|
|
|
|
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
|
|
option) any later version.
|
|
|
|
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.
|
|
*/
|
|
|
|
package gvctrl
|
|
|
|
import (
|
|
"fmt"
|
|
"math/rand"
|
|
"net/http"
|
|
"net/http/cookiejar"
|
|
"strconv"
|
|
"time"
|
|
)
|
|
|
|
type codec string
|
|
|
|
const (
|
|
codecH265 codec = "28"
|
|
codecH264 codec = "10"
|
|
codecMJPEG codec = "4"
|
|
)
|
|
|
|
type quality string
|
|
|
|
const (
|
|
qualityStandard quality = "0"
|
|
qualityFair quality = "1"
|
|
qualityGood quality = "2"
|
|
qualityGreat quality = "3"
|
|
qualityExcellent quality = "4"
|
|
)
|
|
|
|
const (
|
|
res256 = "4480256"
|
|
res360 = "6400360"
|
|
res720 = "12800720"
|
|
)
|
|
|
|
const (
|
|
defaultCodec = codecH264
|
|
defaultRes = "6400360" // 360p
|
|
defaultFrameRate = "25000" // 25 fps
|
|
defaultVBR = "0" // Variable bitrate off
|
|
defaultQuality = qualityGood
|
|
defaultBitRate = "512000" // 512 kbps (lowest with 360p)
|
|
defaultRefresh = "2000" // 2 seconds
|
|
)
|
|
|
|
type settings struct {
|
|
codec codec
|
|
res string
|
|
frameRate string
|
|
vbr string
|
|
quality quality
|
|
bitRate string
|
|
refresh string
|
|
}
|
|
|
|
func newSettings() settings {
|
|
return settings{
|
|
codec: defaultCodec,
|
|
res: defaultRes,
|
|
frameRate: defaultFrameRate,
|
|
vbr: defaultVBR,
|
|
quality: defaultQuality,
|
|
bitRate: defaultBitRate,
|
|
refresh: defaultRefresh,
|
|
}
|
|
}
|
|
|
|
type option func(s settings) error
|
|
|
|
func Set(host string, options ...option) error {
|
|
// 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 {
|
|
err = op(s)
|
|
if err != nil {
|
|
return fmt.Errorf("could not action option: %v", err)
|
|
}
|
|
}
|
|
|
|
// 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
|
|
}
|
|
|
|
func Codec(c codec) option {
|
|
return func(s settings) error {
|
|
switch c {
|
|
case codecH265, codecH264, codecMJPEG:
|
|
s.codec = c
|
|
return nil
|
|
default:
|
|
return fmt.Errorf("unknown codec: %v", c)
|
|
}
|
|
}
|
|
}
|
|
|
|
func Height(h int) option {
|
|
return func(s settings) error {
|
|
v, ok := map[int]string{256: res256, 360: res360, 720: res720}[h]
|
|
if !ok {
|
|
return fmt.Errorf("invalid display height: %d", h)
|
|
}
|
|
s.res = v
|
|
return nil
|
|
}
|
|
}
|
|
|
|
func FrameRate(f int) option {
|
|
return func(s settings) error {
|
|
if 1 > f || f > 30 {
|
|
return fmt.Errorf("invalid frame rate: %d", f)
|
|
}
|
|
s.frameRate = strconv.Itoa(f * 1000)
|
|
return nil
|
|
}
|
|
}
|
|
|
|
func VariableBitRate(b bool) option {
|
|
return func(s settings) error {
|
|
s.vbr = "0"
|
|
if b {
|
|
s.vbr = "1"
|
|
}
|
|
return nil
|
|
}
|
|
}
|
|
|
|
func Quality(q quality) option {
|
|
return func(s settings) error {
|
|
switch q {
|
|
case qualityStandard, qualityFair, qualityGood, qualityGreat, qualityExcellent:
|
|
s.quality = q
|
|
return nil
|
|
default:
|
|
return fmt.Errorf("invalid quality: %v", q)
|
|
}
|
|
}
|
|
}
|
|
|
|
// TODO: add check of r
|
|
func BitRate(r int) option {
|
|
return func(s settings) error {
|
|
var (
|
|
vbrRates = []int{2000, 4000, 6000, 8000, 10000, 12000, 14000, 16000, 18000, 20000}
|
|
cbrRates256 = []int{128, 256, 512, 1024}
|
|
cbrRates360 = []int{512, 1024, 2048, 3072}
|
|
cbrRates720 = []int{1024, 2048, 4096, 6144}
|
|
)
|
|
|
|
if s.vbr == "1" {
|
|
s.bitRate = closestValStr(r, vbrRates)
|
|
return nil
|
|
}
|
|
|
|
switch s.res {
|
|
case res720:
|
|
s.bitRate = closestValStr(r, cbrRates720)
|
|
case res360:
|
|
s.bitRate = closestValStr(r, cbrRates360)
|
|
case res256:
|
|
s.bitRate = closestValStr(r, cbrRates256)
|
|
default:
|
|
panic("bad resolution")
|
|
}
|
|
return nil
|
|
}
|
|
}
|
|
|
|
func Refresh(r float64) option {
|
|
return func(s settings) error {
|
|
const (
|
|
maxRefreshPeriod = 5
|
|
minRefreshPeriod = .25
|
|
)
|
|
|
|
if minRefreshPeriod > r || r > maxRefreshPeriod {
|
|
return fmt.Errorf("invalid refresh period: %g", r)
|
|
}
|
|
|
|
refOptions := []int{250, 500, 1000, 1500, 2000, 2500, 3000, 3500, 4000, 4500, 5000}
|
|
s.refresh = closestValStr(int(r*1000), refOptions)
|
|
return nil
|
|
}
|
|
}
|