input/gvctrl/gvctrl_test.go: added TestPopulateForm to test construction of settings form and fixed bugs

This commit is contained in:
Saxon 2019-10-13 12:42:31 +10:30
parent 7639c7bb74
commit afe136a51d
3 changed files with 127 additions and 60 deletions

View File

@ -26,6 +26,8 @@ package gvctrl
import (
"errors"
"net/url"
"reflect"
"testing"
)
@ -267,3 +269,51 @@ func TestRefresh(t *testing.T) {
}
}
}
func TestPopulateForm(t *testing.T) {
tests := []struct {
in settings
want string
}{
{
in: newSettings(),
want: "dwConnType=5&mpeg_type=10&dwflicker_hz=0&szResolution=6400360&dwFrameRate=25000&vbr_enable=0&max_bit_rate=512000&custom_rate_control_type=0&custom_bitrate=0&custom_qp_init=25&custom_qp_min=10&custom_qp_max=40&gop_N=2000&dwEncProfile=1&dwEncLevel=31&dwEntropy=0&u8PreAlarmBuf=1&u32PostAlarmBuf2Disk=1&u8SplitInterval=5&bEnableIO=1&bEbIoIn=1&bEbIoIn1=1&bOSDFontSize=0&bEnableOSDCameraName=1&bCamNamePos=2&bEnableOSDDate=1&bDatePos=2&bEnableOSDTime=1&bTimePos=2&szOsdCamName=Camera&u16PostAlarmBuf=1&dwCameraId=1&LangCode=undefined&Recflag=0&submit=Apply",
},
{
in: settings{
codec: codecH265,
res: defaultRes,
frameRate: defaultFrameRate,
vbr: defaultVBR,
quality: defaultQuality,
bitRate: defaultBitRate,
refresh: defaultRefresh,
},
want: "dwConnType=5&mpeg_type=28&dwflicker_hz=0&szResolution=6400360&dwFrameRate=25000&vbr_enable=0&max_bit_rate=512000&custom_rate_control_type=0&custom_bitrate=0&custom_qp_init=25&custom_qp_min=10&custom_qp_max=40&gop_N=2000&dwEncProfile=1&dwEncLevel=31&dwEntropy=0&u8PreAlarmBuf=1&u32PostAlarmBuf2Disk=1&u8SplitInterval=5&bEnableIO=1&bEbIoIn=1&bEbIoIn1=1&bOSDFontSize=0&bEnableOSDCameraName=1&bCamNamePos=2&bEnableOSDDate=1&bDatePos=2&bEnableOSDTime=1&bTimePos=2&szOsdCamName=Camera&u16PostAlarmBuf=1&dwCameraId=1&LangCode=undefined&Recflag=0&submit=Apply",
},
{
in: settings{
codec: codecMJPEG,
res: defaultRes,
frameRate: defaultFrameRate,
vbr: defaultVBR,
quality: defaultQuality,
bitRate: defaultBitRate,
refresh: defaultRefresh,
},
want: "dwConnType=5&mpeg_type=4&dwflicker_hz=0&szResolution=6400360&dwFrameRate=25000&vbr_enable=1&dwVbrQuality=2&vbrmaxbitrate=500000&custom_qp_init=25&gop_N=1500&u8PreAlarmBuf=1&u32PostAlarmBuf2Disk=1&u8SplitInterval=5&bEnableIO=1&bEbIoIn=1&bEbIoIn1=1&bOSDFontSize=0&bEnableOSDCameraName=1&bCamNamePos=2&bEnableOSDDate=1&bDatePos=2&bEnableOSDTime=1&bTimePos=2&szOsdCamName=Camera&u16PostAlarmBuf=1&dwCameraId=1&LangCode=undefined&Recflag=0&submit=Apply",
},
}
for i, test := range tests {
got := populateForm(test.in)
want, err := url.ParseQuery(test.want)
if err != nil {
t.Fatalf("should not have got error: %v parsing want string for test: %d", err, i)
}
if !reflect.DeepEqual(got, want) {
t.Errorf("did not get expected result for test: %d\nGot: %v\nWant: %v", i, got, want)
}
}
}

View File

@ -124,66 +124,7 @@ func logIn(c *http.Client, id, host, b string) error {
}
func submitSettings(c *http.Client, id, host string, s settings) error {
var f url.Values
f.Set("dwConnType", "5")
f.Set("mpeg_type", "10")
f.Set("dwflicker_hz", "0")
f.Set("szResolution", s.res)
f.Set("dwFrameRate", s.frameRate)
if s.codec == codecMJPEG {
f.Set("vbr_enable", "1")
f.Set("dwVbrQuality", s.vbr)
f.Set("vbrmaxbitrate", "750000")
} else {
switch s.vbr {
case "0":
f.Set("vbr_enable", "0")
f.Set("max_bit_rate", s.bitRate)
case "1":
f.Set("vbr_enable", "1")
f.Set("dwVbrQuality", s.vbr)
f.Set("vbrmaxbitrate", s.bitRate)
default:
panic("invalid vbrEnable parameter")
}
f.Set("custom_rate_control_type", "0")
f.Set("custom_bitrate", "512000")
f.Set("custom_qp_init", "25")
f.Set("custom_qp_min", "10")
f.Set("custom_qp_max", "40")
}
f.Set("gop_N", s.refresh)
if s.codec == codecH264 || s.codec == codecH265 {
f.Set("dwEncProfile", "3")
f.Set("dwEncLevel", "31")
f.Set("dwEntropy", "0")
}
f.Set("u8PreAlarmBuf", "1")
f.Set("u32PostAlarmBuf2Disk", "1")
f.Set("u8SplitInterval", "5")
f.Set("bEnableIO", "1")
f.Set("bEbIoIn", "1")
f.Set("bEbIoIn1", "1")
f.Set("bOSDFontSize", "0")
f.Set("bEnableOSDCameraName", "1")
f.Set("bCamNamePos", "2")
f.Set("bEnableOSDDate", "1")
f.Set("bDatePos", "2")
f.Set("bEnableOSDTime", "1")
f.Set("bTimePos", "2")
f.Set("szOsdCamName", "Camera")
f.Set("u16PostAlarmBuf", "1")
f.Set("dwCameraId", "1") // Channel=1 => cameraID=0 and chanel=2 => cameraID=1
f.Set("LangCode", "undefined")
f.Set("Recflag", "0")
f.Set("submit", "Apply")
fBytes := []byte(f.Encode())
fBytes := []byte(populateForm(s).Encode())
req, err := http.NewRequest("POST", settingsURL, bytes.NewReader(fBytes))
if err != nil {
return fmt.Errorf("could not create settings submit request: %v", err)

View File

@ -29,6 +29,7 @@ import (
"crypto/md5"
"encoding/hex"
"math"
"net/url"
"strconv"
"strings"
)
@ -52,3 +53,78 @@ func closestValIdx(v int, l []int) int {
func convRate(v int, l []int) string {
return strconv.Itoa(l[closestValIdx(v, l)] * 1000)
}
func populateForm(s settings) url.Values {
f := url.Values{}
f.Set("dwConnType", "5")
f.Set("mpeg_type", string(s.codec))
f.Set("dwflicker_hz", "0")
f.Set("szResolution", s.res)
f.Set("dwFrameRate", s.frameRate)
f.Set("custom_qp_init", "25")
if s.codec == codecMJPEG {
f.Set("vbr_enable", "1")
f.Set("dwVbrQuality", string(s.quality))
switch s.res {
case res256:
f.Set("vbrmaxbitrate", "250000")
case res360:
f.Set("vbrmaxbitrate", "500000")
case res720:
f.Set("vbrmaxbitrate", "750000")
default:
panic("invalid resolution")
}
} else {
switch s.vbr {
case "0":
f.Set("vbr_enable", "0")
f.Set("max_bit_rate", s.bitRate)
case "1":
f.Set("vbr_enable", "1")
f.Set("dwVbrQuality", s.vbr)
f.Set("vbrmaxbitrate", s.bitRate)
default:
panic("invalid vbrEnable parameter")
}
f.Set("custom_rate_control_type", "0")
f.Set("custom_bitrate", "0")
f.Set("custom_qp_min", "10")
f.Set("custom_qp_max", "40")
}
f.Set("gop_N", s.refresh)
if s.codec == codecMJPEG {
f.Set("gop_N", "1500")
}
if s.codec == codecH264 || s.codec == codecH265 {
f.Set("dwEncProfile", "1")
f.Set("dwEncLevel", "31")
f.Set("dwEntropy", "0")
}
f.Set("u8PreAlarmBuf", "1")
f.Set("u32PostAlarmBuf2Disk", "1")
f.Set("u8SplitInterval", "5")
f.Set("bEnableIO", "1")
f.Set("bEbIoIn", "1")
f.Set("bEbIoIn1", "1")
f.Set("bOSDFontSize", "0")
f.Set("bEnableOSDCameraName", "1")
f.Set("bCamNamePos", "2")
f.Set("bEnableOSDDate", "1")
f.Set("bDatePos", "2")
f.Set("bEnableOSDTime", "1")
f.Set("bTimePos", "2")
f.Set("szOsdCamName", "Camera")
f.Set("u16PostAlarmBuf", "1")
f.Set("dwCameraId", "1") // Channel=1 => cameraID=0 and chanel=2 => cameraID=1
f.Set("LangCode", "undefined")
f.Set("Recflag", "0")
f.Set("submit", "Apply")
return f
}