input/gvctrl: moved unexported stuff from gvctrl.go to be more friendly to API users

This commit is contained in:
Saxon 2019-10-13 18:25:36 +10:30
parent cda7093c21
commit b3c3b842c8
3 changed files with 71 additions and 71 deletions

View File

@ -34,65 +34,6 @@ import (
"time"
)
type codec string
const (
codecH265 codec = "28"
codecH264 codec = "10"
codecMJPEG codec = "4"
)
type quality string
const (
qualityStandard quality = "0"
qualityFair quality = "1"
qualityGood quality = "2"
qualityGreat quality = "3"
qualityExcellent quality = "4"
)
const (
res256 = "4480256"
res360 = "6400360"
res720 = "12800720"
)
const (
defaultCodec = codecH264
defaultRes = "6400360" // 360p
defaultFrameRate = "25000" // 25 fps
defaultVBR = "0" // Variable bitrate off
defaultQuality = qualityGood
defaultVBRBitRate = "250000" // 512 kbps (lowest with 360p)
defaultCBRBitRate = "512000"
defaultRefresh = "2000" // 2 seconds
)
type settings struct {
codec codec
res string
frameRate string
vbr string
quality quality
vbrBitRate string
cbrBitRate string
refresh string
}
func newSettings() settings {
return settings{
codec: defaultCodec,
res: defaultRes,
frameRate: defaultFrameRate,
vbr: defaultVBR,
quality: defaultQuality,
vbrBitRate: defaultVBRBitRate,
cbrBitRate: defaultCBRBitRate,
refresh: defaultRefresh,
}
}
type option func(s settings) (settings, error)
func Set(host string, options ...option) error {
@ -144,14 +85,22 @@ func Set(host string, options ...option) error {
return nil
}
func Codec(c codec) option {
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:
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)
}
}
}
@ -187,14 +136,24 @@ func VariableBitRate(b bool) option {
}
}
func VBRQuality(q quality) option {
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:
case QualityStandard, QualityFair, QualityGood, QualityGreat, QualityExcellent:
s.quality = q
return s, nil
default:
return s, fmt.Errorf("invalid quality: %v", q)
return s, fmt.Errorf("invalid Quality: %v", q)
}
}
}

View File

@ -297,7 +297,7 @@ func TestPopulateForm(t *testing.T) {
},
{
in: settings{
codec: codecH265,
codec: CodecH265,
res: defaultRes,
frameRate: defaultFrameRate,
vbr: defaultVBR,
@ -310,7 +310,7 @@ func TestPopulateForm(t *testing.T) {
},
{
in: settings{
codec: codecMJPEG,
codec: CodecMJPEG,
res: defaultRes,
frameRate: defaultFrameRate,
vbr: defaultVBR,
@ -323,7 +323,7 @@ func TestPopulateForm(t *testing.T) {
},
{
in: settings{
codec: codecH264,
codec: CodecH264,
res: defaultRes,
frameRate: defaultFrameRate,
vbr: "1",

View File

@ -34,6 +34,47 @@ import (
"strings"
)
const (
res256 = "4480256"
res360 = "6400360"
res720 = "12800720"
)
const (
defaultCodec = CodecH264
defaultRes = "6400360" // 360p
defaultFrameRate = "25000" // 25 fps
defaultVBR = "0" // Variable bitrate off
defaultQuality = QualityGood
defaultVBRBitRate = "250000" // 512 kbps (lowest with 360p)
defaultCBRBitRate = "512000"
defaultRefresh = "2000" // 2 seconds
)
type settings struct {
codec Codec
res string
frameRate string
vbr string
quality Quality
vbrBitRate string
cbrBitRate string
refresh string
}
func newSettings() settings {
return settings{
codec: defaultCodec,
res: defaultRes,
frameRate: defaultFrameRate,
vbr: defaultVBR,
quality: defaultQuality,
vbrBitRate: defaultVBRBitRate,
cbrBitRate: defaultCBRBitRate,
refresh: defaultRefresh,
}
}
func md5Hex(s string) string {
h := md5.New()
h.Write([]byte(s))
@ -63,7 +104,7 @@ func populateForm(s settings) url.Values {
f.Set("dwFrameRate", s.frameRate)
f.Set("custom_qp_init", "25")
if s.codec == codecMJPEG {
if s.codec == CodecMJPEG {
f.Set("vbr_enable", "1")
f.Set("dwVbrQuality", string(s.quality))
@ -97,11 +138,11 @@ func populateForm(s settings) url.Values {
}
f.Set("gop_N", s.refresh)
if s.codec == codecMJPEG {
if s.codec == CodecMJPEG {
f.Set("gop_N", "1500")
}
if s.codec == codecH264 || s.codec == codecH265 {
if s.codec == CodecH264 || s.codec == CodecH265 {
f.Set("dwEncProfile", "1")
f.Set("dwEncLevel", "31")
f.Set("dwEntropy", "0")