Creating config getter

This commit is contained in:
Unknown 2018-01-23 13:45:06 +10:30
parent 6663656a63
commit 62f7f5ba9d
1 changed files with 29 additions and 24 deletions

View File

@ -102,7 +102,8 @@ type Config struct {
type RevidInst interface { type RevidInst interface {
Start() Start()
Stop() Stop()
ChangeState(newConfiguration Config) error ChangeState(newconfig Config) error
GetConfigRef() *Config
} }
type revidInst struct { type revidInst struct {
@ -113,7 +114,7 @@ type revidInst struct {
ffmpegPath string ffmpegPath string
tempDir string tempDir string
ringBuffer ringbuffer.RingBuffer ringBuffer ringbuffer.RingBuffer
Configuration Config config Config
isRunning bool isRunning bool
Error *log.Logger Error *log.Logger
outputFile *os.File outputFile *os.File
@ -122,24 +123,28 @@ type revidInst struct {
h264Parser h264.H264Parser h264Parser h264.H264Parser
} }
func NewRevidInstance(Configuration Config) (r *revidInst, err error) { func (r *revidInst) GetConfigRef(){
return &r.config
}
func NewRevidInstance(config Config) (r *revidInst, err error) {
r = new(revidInst) r = new(revidInst)
r.ringBuffer = ringbuffer.NewRingBuffer(bufferSize, mp2tPacketSize*mp2tMaxPackets) r.ringBuffer = ringbuffer.NewRingBuffer(bufferSize, mp2tPacketSize*mp2tMaxPackets)
r.Error = log.New(os.Stderr, "ERROR: ", log.Ldate|log.Ltime|log.Lshortfile) r.Error = log.New(os.Stderr, "ERROR: ", log.Ldate|log.Ltime|log.Lshortfile)
r.expectCC = -1 r.expectCC = -1
r.dumpCC = -1 r.dumpCC = -1
r.dumpPCRBase = 0 r.dumpPCRBase = 0
r.ChangeState(Configuration) r.ChangeState(config)
switch r.Configuration.Output { switch r.config.Output {
case File: case File:
r.outputFile, err = os.Create(r.Configuration.OutputFileName) r.outputFile, err = os.Create(r.config.OutputFileName)
if err != nil { if err != nil {
return nil, err return nil, err
} }
} }
switch r.Configuration.Input { switch r.config.Input {
case File: case File:
r.inputFile, err = os.Open(r.Configuration.InputFileName) r.inputFile, err = os.Open(r.config.InputFileName)
if err != nil { if err != nil {
return nil, err return nil, err
} }
@ -152,29 +157,29 @@ func NewRevidInstance(Configuration Config) (r *revidInst, err error) {
go r.h264Parser.Parse() go r.h264Parser.Parse()
go r.input() go r.input()
go r.generator.Generate() go r.generator.Generate()
r.ErrOut("New revid instance created! Configuration is:") r.ErrOut("New revid instance created! config is:")
r.ErrOut(fmt.Sprintf("%v",r.Configuration)) r.ErrOut(fmt.Sprintf("%v",r.config))
return return
} }
func (r *revidInst) ChangeState(newConfiguration Config) error { func (r *revidInst) ChangeState(newconfig Config) error {
// TODO: check that the Configuration is G // TODO: check that the config is G
r.Configuration = newConfiguration r.config = newconfig
return nil return nil
} }
func (r *revidInst) ErrOut(m string){ func (r *revidInst) ErrOut(m string){
if r.Configuration.Debug { if r.config.Debug {
r.Configuration.Logger.Log("Debug",m) r.config.Logger.Log("Debug",m)
} }
} }
func (r *revidInst) Start() { func (r *revidInst) Start() {
switch r.Configuration.Input { switch r.config.Input {
case Raspivid: case Raspivid:
r.ErrOut("Starting raspivid!") r.ErrOut("Starting raspivid!")
cmd = exec.Command("raspivid", "-o", "-", "-n", "-t", "0", "-b", cmd = exec.Command("raspivid", "-o", "-", "-n", "-t", "0", "-b",
r.Configuration.Bitrate,"-qp", r.Configuration.Quantization, "-w", r.Configuration.Width, "-h", r.Configuration.Height, "-fps", r.Configuration.FrameRate, "-ih", "-g", "100") r.config.Bitrate,"-qp", r.config.Quantization, "-w", r.config.Width, "-h", r.config.Height, "-fps", r.config.FrameRate, "-ih", "-g", "100")
stdout, _ := cmd.StdoutPipe() stdout, _ := cmd.StdoutPipe()
err := cmd.Start() err := cmd.Start()
inputReader = bufio.NewReader(stdout) inputReader = bufio.NewReader(stdout)
@ -188,7 +193,7 @@ func (r *revidInst) Start() {
} }
r.isRunning = true r.isRunning = true
var h264Data []byte var h264Data []byte
switch r.Configuration.Input { switch r.config.Input {
case Raspivid: case Raspivid:
go func() { go func() {
r.ErrOut("Reading camera data!") r.ErrOut("Reading camera data!")
@ -289,14 +294,14 @@ func (r *revidInst) output() {
if clip, err := r.ringBuffer.Read(); err == nil { if clip, err := r.ringBuffer.Read(); err == nil {
r.ErrOut(fmt.Sprintf("Delay is: %v\n", delay)) r.ErrOut(fmt.Sprintf("Delay is: %v\n", delay))
r.ErrOut(fmt.Sprintf("Ring buffer size: %v\n", r.ringBuffer.GetNoOfElements())) r.ErrOut(fmt.Sprintf("Ring buffer size: %v\n", r.ringBuffer.GetNoOfElements()))
switch r.Configuration.Output { switch r.config.Output {
case File: case File:
r.outputFile.Write(clip) r.outputFile.Write(clip)
case HttpOut: case HttpOut:
bytes += len(clip) bytes += len(clip)
for err := r.sendClipToHTTP(clip, r.Configuration.HttpAddress); err != nil; { for err := r.sendClipToHTTP(clip, r.config.HttpAddress); err != nil; {
r.ErrOut("Post failed trying again!") r.ErrOut("Post failed trying again!")
err = r.sendClipToHTTP(clip, r.Configuration.HttpAddress) err = r.sendClipToHTTP(clip, r.config.HttpAddress)
} }
default: default:
r.ErrOut("No output defined!") r.ErrOut("No output defined!")
@ -306,7 +311,7 @@ func (r *revidInst) output() {
} }
now = time.Now() now = time.Now()
deltaTime := now.Sub(prevTime) deltaTime := now.Sub(prevTime)
if r.Configuration.Debug && deltaTime > time.Duration(bitrateTime)*time.Second { if r.config.Debug && deltaTime > time.Duration(bitrateTime)*time.Second {
fmt.Printf("Bitrate: %v bytes/s\n", int64(float64(bytes) / float64(deltaTime/1e9))) fmt.Printf("Bitrate: %v bytes/s\n", int64(float64(bytes) / float64(deltaTime/1e9)))
prevTime = now prevTime = now
bytes = 0 bytes = 0
@ -322,7 +327,7 @@ func (r *revidInst)sendClipToHTTP(clip []byte, output string) error {
Timeout: timeout, Timeout: timeout,
} }
url := output + strconv.Itoa(len(clip)) url := output + strconv.Itoa(len(clip))
if r.Configuration.Debug { if r.config.Debug {
fmt.Printf("Posting %s (%d bytes)\n", url, len(clip)) fmt.Printf("Posting %s (%d bytes)\n", url, len(clip))
} }
resp, err := client.Post(url, "video/mp2t", bytes.NewReader(clip)) // lighter than NewBuffer resp, err := client.Post(url, "video/mp2t", bytes.NewReader(clip)) // lighter than NewBuffer
@ -331,7 +336,7 @@ func (r *revidInst)sendClipToHTTP(clip []byte, output string) error {
} }
defer resp.Body.Close() defer resp.Body.Close()
body, err := ioutil.ReadAll(resp.Body) body, err := ioutil.ReadAll(resp.Body)
if err == nil && r.Configuration.Debug { if err == nil && r.config.Debug {
fmt.Printf("%s\n", body) fmt.Printf("%s\n", body)
} }
return nil return nil