input/gvctrl: added Channel option function and started making changes

Added a field called ch to hold the channel. Ch can be set using Channel option function.
Created default channel const and setting ch field in newSettings. Started making changes
to other option functions, namely CodecOut, subsequently, wrote testing for it."
This commit is contained in:
Saxon 2019-10-23 21:47:15 +10:30
parent 392ed399d9
commit 710a245c89
4 changed files with 84 additions and 6 deletions

View File

@ -35,6 +35,7 @@ LICENSE
package gvctrl package gvctrl
import ( import (
"errors"
"fmt" "fmt"
"math/rand" "math/rand"
"net/http" "net/http"
@ -60,6 +61,7 @@ type Option func(s settings) (settings, error)
// vbr bitrate: 250 kbps // vbr bitrate: 250 kbps
// cbr bitrate: 512 kbps // cbr bitrate: 512 kbps
// refresh: 2 seconds // refresh: 2 seconds
// channel: 2
func Set(host string, options ...Option) error { func Set(host string, options ...Option) error {
// Randomly generate an ID our client will use. // Randomly generate an ID our client will use.
const ( const (
@ -109,6 +111,17 @@ func Set(host string, options ...Option) error {
return nil 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. // Codec is a video codec.
type Codec string type Codec string
@ -123,13 +136,11 @@ const (
// codec options are listed above as consts. // codec options are listed above as consts.
func CodecOut(c Codec) Option { func CodecOut(c Codec) Option {
return func(s settings) (settings, error) { return func(s settings) (settings, error) {
switch c { if (c != CodecH265 && c != CodecH264) && (s.ch == 1 || (s.ch == 2 && c != CodecMJPEG)) {
case CodecH265, CodecH264, CodecMJPEG:
s.codec = c
return s, nil
default:
return s, fmt.Errorf("unknown Codec: %v", c) return s, fmt.Errorf("unknown Codec: %v", c)
} }
s.codec = c
return s, nil
} }
} }

View File

@ -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) { func TestHeight(t *testing.T) {
tests := []struct { tests := []struct {
h int h int

View File

@ -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("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("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("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-Encoding", "gzip, deflate")
req.Header.Set("Accept-Language", "en-GB,en-US;q=0.9,en;q=0.8") req.Header.Set("Accept-Language", "en-GB,en-US;q=0.9,en;q=0.8")
req.Header.Set("Cookie", "CLIENT_ID="+id) req.Header.Set("Cookie", "CLIENT_ID="+id)

View File

@ -52,6 +52,7 @@ const (
defaultVBRBitrate = "250000" // 512 kbps (lowest with 360p) defaultVBRBitrate = "250000" // 512 kbps (lowest with 360p)
defaultCBRBitrate = "512000" defaultCBRBitrate = "512000"
defaultRefresh = "2000" // 2 seconds defaultRefresh = "2000" // 2 seconds
defaultChan = 2
) )
// settings holds string representations required by the settings form for each // settings holds string representations required by the settings form for each
@ -65,6 +66,7 @@ type settings struct {
vbrBitrate string vbrBitrate string
cbrBitrate string cbrBitrate string
refresh string refresh string
ch int
} }
// newSetting will return a settings with default values. // newSetting will return a settings with default values.
@ -78,6 +80,7 @@ func newSettings() settings {
vbrBitrate: defaultVBRBitrate, vbrBitrate: defaultVBRBitrate,
cbrBitrate: defaultCBRBitrate, cbrBitrate: defaultCBRBitrate,
refresh: defaultRefresh, refresh: defaultRefresh,
ch: defaultChan,
} }
} }