/* 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 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 defaultVBRBitRate = "250000" // 512 kbps (lowest with 360p) defaultCBRBitRate = "512000" defaultRefresh = "2000" // 2 seconds ) type settings struct { codec codec res string frameRate string vbr string quality quality vbrBitRate string cbrBitRate string refresh string } func newSettings() settings { return settings{ codec: defaultCodec, res: defaultRes, frameRate: defaultFrameRate, vbr: defaultVBR, quality: defaultQuality, vbrBitRate: defaultVBRBitRate, cbrBitRate: defaultCBRBitRate, refresh: defaultRefresh, } } type option func(s settings) (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 { s, 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) (settings, error) { switch c { case codecH265, codecH264, codecMJPEG: s.codec = c return s, nil default: return s, fmt.Errorf("unknown codec: %v", c) } } } func Height(h int) option { return func(s settings) (settings, error) { v, ok := map[int]string{256: res256, 360: res360, 720: res720}[h] if !ok { return s, fmt.Errorf("invalid display height: %d", h) } s.res = v return s, nil } } func FrameRate(f int) option { return func(s settings) (settings, error) { if 1 > f || f > 30 { return s, fmt.Errorf("invalid frame rate: %d", f) } s.frameRate = strconv.Itoa(f * 1000) return s, nil } } func VariableBitRate(b bool) option { return func(s settings) (settings, error) { s.vbr = "0" if b { s.vbr = "1" } return s, nil } } func VBRQuality(q quality) option { return func(s settings) (settings, error) { switch q { case qualityStandard, qualityFair, qualityGood, qualityGreat, qualityExcellent: s.quality = q return s, nil default: return s, fmt.Errorf("invalid quality: %v", q) } } } func VBRBitRate(r int) option { return func(s settings) (settings, error) { var vbrRates = []int{250, 500, 750, 1000, 1250, 1500, 1750, 2000, 2250, 2500} if s.vbr == "1" { s.vbrBitRate = convRate(r, vbrRates) return s, nil } return s, nil } } func CBRBitRate(r int) option { return func(s settings) (settings, error) { var ( cbrRates256 = []int{128, 256, 512, 1024} cbrRates360 = []int{512, 1024, 2048, 3072} cbrRates720 = []int{1024, 2048, 4096, 6144} ) switch s.res { case res720: s.cbrBitRate = convRate(r, cbrRates720) case res360: s.cbrBitRate = convRate(r, cbrRates360) case res256: s.cbrBitRate = convRate(r, cbrRates256) default: panic("bad resolution") } return s, nil } } func Refresh(r float64) option { return func(s settings) (settings, error) { const ( maxRefreshPeriod = 5 minRefreshPeriod = .25 ) if minRefreshPeriod > r || r > maxRefreshPeriod { return s, fmt.Errorf("invalid refresh period: %g", r) } refOptions := []int{250, 500, 1000, 1500, 2000, 2500, 3000, 3500, 4000, 4500, 5000} s.refresh = strconv.Itoa(refOptions[closestValIdx(int(r*1000), refOptions)]) return s, nil } }