input/gvctrl/utils.go: wrote documentation for utils.go

This commit is contained in:
Saxon 2019-10-14 10:37:41 +10:30
parent b86ff77996
commit 5b17613489
1 changed files with 18 additions and 5 deletions

View File

@ -1,7 +1,7 @@
/* /*
DESCRIPTION DESCRIPTION
utils.go provides helper functions for functionality found in both gvctrl.go utils.go provides general constants, structs and helper functions for use in
request.go. this package.
AUTHORS AUTHORS
Saxon A. Nelson-Milton <saxon@ausocean.org> Saxon A. Nelson-Milton <saxon@ausocean.org>
@ -34,12 +34,15 @@ import (
"strings" "strings"
) )
// The strings used in encoding the settings form to indicate resultion.
const ( const (
res256 = "4480256" res256 = "4480256" // 480x256
res360 = "6400360" res360 = "6400360" // 640x360
res720 = "12800720" res720 = "12800720" // 1280x720
) )
// Default values for fields in the settings struct when the newSettings
// constructor is used.
const ( const (
defaultCodec = CodecH264 defaultCodec = CodecH264
defaultRes = "6400360" // 360p defaultRes = "6400360" // 360p
@ -51,6 +54,8 @@ const (
defaultRefresh = "2000" // 2 seconds defaultRefresh = "2000" // 2 seconds
) )
// settings holds string representations required by the settings form for each
// of the parameters configurable through this API.
type settings struct { type settings struct {
codec Codec codec Codec
res string res string
@ -62,6 +67,7 @@ type settings struct {
refresh string refresh string
} }
// newSetting will return a settings with default values.
func newSettings() settings { func newSettings() settings {
return settings{ return settings{
codec: defaultCodec, codec: defaultCodec,
@ -75,12 +81,15 @@ func newSettings() settings {
} }
} }
// md5Hex returns the md5 hex string of s with alphanumerics as upper case.
func md5Hex(s string) string { func md5Hex(s string) string {
h := md5.New() h := md5.New()
h.Write([]byte(s)) h.Write([]byte(s))
return strings.ToUpper(hex.EncodeToString(h.Sum(nil))) return strings.ToUpper(hex.EncodeToString(h.Sum(nil)))
} }
// closestValIdx will return the index of the value in l that is closest to the
// value v.
func closestValIdx(v int, l []int) int { func closestValIdx(v int, l []int) int {
var idx int var idx int
for i := range l { for i := range l {
@ -91,10 +100,14 @@ func closestValIdx(v int, l []int) int {
return idx return idx
} }
// convRate is used to firstly find a value in l closest to the bitrate v (in
// kbps), convert from kbps to bps, and the convert to string.
func convRate(v int, l []int) string { func convRate(v int, l []int) string {
return strconv.Itoa(l[closestValIdx(v, l)] * 1000) return strconv.Itoa(l[closestValIdx(v, l)] * 1000)
} }
// populateForm will populate the settings form using the passed settings struct
// s and return as a url.Values.
func populateForm(s settings) url.Values { func populateForm(s settings) url.Values {
f := url.Values{} f := url.Values{}
f.Set("dwConnType", "5") f.Set("dwConnType", "5")