diff --git a/input/gvctrl/gvctrl.go b/input/gvctrl/gvctrl.go index 79d260f1..ce99e51e 100644 --- a/input/gvctrl/gvctrl.go +++ b/input/gvctrl/gvctrl.go @@ -35,6 +35,7 @@ LICENSE package gvctrl import ( + "errors" "fmt" "math/rand" "net/http" @@ -60,6 +61,7 @@ type Option func(s settings) (settings, error) // vbr bitrate: 250 kbps // cbr bitrate: 512 kbps // refresh: 2 seconds +// channel: 2 func Set(host string, options ...Option) error { // Randomly generate an ID our client will use. const ( @@ -109,6 +111,17 @@ func Set(host string, options ...Option) error { return nil } +// Channel will set the GeoVision channel we will be using. +func Channel(c int) Option { + return func(s settings) (settings, error) { + if c != 1 && c != 2 { + return s, errors.New("invalid channel") + } + s.ch = c + return s, nil + } +} + // Codec is a video codec. type Codec string @@ -123,13 +136,11 @@ const ( // codec options are listed above as consts. func CodecOut(c Codec) Option { return func(s settings) (settings, error) { - switch c { - case CodecH265, CodecH264, CodecMJPEG: - s.codec = c - return s, nil - default: + if (c != CodecH265 && c != CodecH264) && (s.ch == 1 || (s.ch == 2 && c != CodecMJPEG)) { return s, fmt.Errorf("unknown Codec: %v", c) } + s.codec = c + return s, nil } } diff --git a/input/gvctrl/gvctrl_test.go b/input/gvctrl/gvctrl_test.go index d15ed9be..4a258da1 100644 --- a/input/gvctrl/gvctrl_test.go +++ b/input/gvctrl/gvctrl_test.go @@ -113,6 +113,70 @@ func TestConvRate(t *testing.T) { } } +func TestCodecOut(t *testing.T) { + tests := []struct { + s settings + c Codec + want settings + err bool + }{ + { + s: settings{ch: 1}, + c: CodecH264, + want: settings{ch: 1, codec: CodecH264}, + }, + { + s: settings{ch: 1}, + c: CodecH265, + want: settings{ch: 1, codec: CodecH265}, + }, + { + s: settings{ch: 1}, + c: CodecMJPEG, + want: settings{ch: 1}, + err: true, + }, + { + s: settings{ch: 2}, + c: CodecH264, + want: settings{ch: 2, codec: CodecH264}, + }, + { + s: settings{ch: 2}, + c: CodecH265, + want: settings{ch: 2, codec: CodecH265}, + }, + { + s: settings{ch: 2}, + c: CodecMJPEG, + want: settings{ch: 2, codec: CodecMJPEG}, + }, + { + s: settings{ch: 1}, + c: Codec(500), + want: settings{ch: 1}, + err: true, + }, + { + s: settings{ch: 2}, + c: Codec(500), + want: settings{ch: 2}, + err: true, + }, + } + + for i, test := range tests { + got, err := CodecOut(test.c)(test.s) + if err != nil && test.err != true { + t.Errorf("did not expect error: %v for test: %d", err, i) + } + + if got != test.want { + t.Errorf("did not get expected result for test: %d\nGot: %v\nWant: %v\n", i, got, test.want) + } + } +} + func TestHeight(t *testing.T) { tests := []struct { h int diff --git a/input/gvctrl/request.go b/input/gvctrl/request.go index 2fa5d9cb..df33dd3c 100644 --- a/input/gvctrl/request.go +++ b/input/gvctrl/request.go @@ -145,7 +145,7 @@ func submitSettings(c *http.Client, id, host string, s settings) error { req.Header.Set("User-Agent", "Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/77.0.3865.90 Safari/537.36") req.Header.Set("Content-Type", "application/x-www-form-urlencoded") req.Header.Set("Accept", "text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3") - req.Header.Set("Referer", "http://"+host+"/ssi.cgi/VideoSettingSub.htm?cam=2") + req.Header.Set("Referer", "http://"+host+"/ssi.cgi/VideoSettingSub.htm?cam="+strconv.Itoa(s.ch)) req.Header.Set("Accept-Encoding", "gzip, deflate") req.Header.Set("Accept-Language", "en-GB,en-US;q=0.9,en;q=0.8") req.Header.Set("Cookie", "CLIENT_ID="+id) diff --git a/input/gvctrl/utils.go b/input/gvctrl/utils.go index c33cc919..703f41bd 100644 --- a/input/gvctrl/utils.go +++ b/input/gvctrl/utils.go @@ -52,6 +52,7 @@ const ( defaultVBRBitrate = "250000" // 512 kbps (lowest with 360p) defaultCBRBitrate = "512000" defaultRefresh = "2000" // 2 seconds + defaultChan = 2 ) // settings holds string representations required by the settings form for each @@ -65,6 +66,7 @@ type settings struct { vbrBitrate string cbrBitrate string refresh string + ch int } // newSetting will return a settings with default values. @@ -78,6 +80,7 @@ func newSettings() settings { vbrBitrate: defaultVBRBitrate, cbrBitrate: defaultCBRBitrate, refresh: defaultRefresh, + ch: defaultChan, } }