/* 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 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 } type Codec string const ( CodecH265 Codec = "28" CodecH264 Codec = "10" CodecMJPEG Codec = "4" ) func CodecOut(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 } } type Quality string const ( QualityStandard Quality = "0" QualityFair Quality = "1" QualityGood Quality = "2" QualityGreat Quality = "3" QualityExcellent Quality = "4" ) 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 } }