Working on more comments and getting ready for testing

This commit is contained in:
Saxon Milton 2018-01-31 10:21:53 +10:30
parent ccd2bd2bed
commit 3738659eae
1 changed files with 46 additions and 18 deletions

View File

@ -54,7 +54,7 @@ import (
"../../utils/smartLogger"
)
// defaults and networking consts
// Misc constants
const (
clipDuration = 1 // s
mp2tPacketSize = 188 // MPEG-TS packet size
@ -79,19 +79,8 @@ const (
Debug = "Debug"
)
// Config enums
const (
raspivid = 1
rtp = 2
h264Codec = 3
file = 4
httpOut = 5
h264 = 6
mjpeg = 7
none = 8
mpegts = 9
)
// Config provides parameters relevant to a revid instance. A new config must
// be passed to the constructor.
type Config struct {
Input uint8
InputCodec uint8
@ -109,6 +98,19 @@ type Config struct {
Logger smartLogger.LogInstance
}
// Enums for config struct
const (
raspivid = 1
rtp = 2
h264Codec = 3
file = 4
httpOut = 5
h264 = 6
mjpeg = 7
none = 8
mpegts = 9
)
// Default config settings
const (
defaultFrameRate = "25"
@ -117,6 +119,8 @@ const (
defaultIntraRefreshPeriod = "100"
)
// RevidInst provides methods to control a revidInst session; providing methods
// to start, stop and change the state of an instance using the Config struct.
type RevidInst interface {
Start()
Stop()
@ -126,9 +130,9 @@ type RevidInst interface {
IsRunning() bool
}
// The revidInst struct provides fields to describe the state of a RevidInst.
type revidInst struct {
dumpPCRBase uint64
conn net.Conn
ffmpegPath string
tempDir string
ringBuffer ringbuffer.RingBuffer
@ -143,10 +147,12 @@ type revidInst struct {
mjpegOutputChan chan []byte
}
// NewRevidInstance returns a pointer to a new revidInst with the desired
// configuration, and/or an error if construction of the new instant was not
// successful.
func NewRevidInstance(config Config) (r *revidInst, err error) {
r = new(revidInst)
r.ringBuffer = ringbuffer.NewRingBuffer(bufferSize, mp2tPacketSize*mp2tMaxPackets)
r.dumpPCRBase = 0
r.ChangeState(config)
switch r.config.Output {
case file:
@ -184,10 +190,13 @@ func NewRevidInstance(config Config) (r *revidInst, err error) {
return
}
// GetConfigRef returns a pointer to the revidInst's Config struct object
func (r *revidInst) GetConfigRef() *Config {
return &r.config
}
// ChangeState swaps the current config of a revidInst with the passed
// configuration; checking validity and returning errors if not valid.
func (r *revidInst) ChangeState(config Config) error {
switch config.Input {
case rtp:
@ -242,6 +251,9 @@ func (r *revidInst) ChangeState(config Config) error {
return nil
}
// Log takes a logtype and message and tries to send this information to the
// logger provided in the revidInst config, if there is one, otherwise an
// error is returned.
func (r *revidInst) Log(logType, m string) error {
if r.config.Logger != nil {
r.config.Logger.Log(logType, m)
@ -251,10 +263,13 @@ func (r *revidInst) Log(logType, m string) error {
}
}
// IsRunning returns true if the revidInst is currently running and false otherwise
func (r *revidInst) IsRunning() bool {
return r.isRunning
}
// Start invokes a revidInst to start processing video from a defined input
// and packetising to a defined output.
func (r *revidInst) Start() {
if r.isRunning {
r.Log(Warning, "revidInst.Start() called but revid already running!")
@ -314,6 +329,7 @@ func (r *revidInst) Start() {
go r.outputClips()
}
// readCamera reads data from the defined camera while the revidInst is running.
func (r *revidInst)readCamera() {
r.Log(Info, "Reading camera data!")
for r.isRunning {
@ -332,6 +348,7 @@ func (r *revidInst)readCamera() {
r.Log(Info, "Out of reading routine!")
}
// Stop
func (r *revidInst) Stop() {
if r.isRunning {
r.Log(Info, "Stopping revid!")
@ -342,6 +359,10 @@ func (r *revidInst) Stop() {
}
}
/*
New returns a pointer to a newly allocated RingBuffer with the parameters specified.
It initialises fields and allocates the required dataMemory.
*/
func (r *revidInst) packClips() {
clipSize := 0
packetCount := 0
@ -400,6 +421,10 @@ func (r *revidInst) packClips() {
}
}
/*
New returns a pointer to a newly allocated RingBuffer with the parameters specified.
It initialises fields and allocates the required dataMemory.
*/
func (r *revidInst) outputClips() {
now := time.Now()
prevTime := now
@ -444,7 +469,10 @@ func (r *revidInst) outputClips() {
}
}
// sendClipToHTPP posts a video clip via HTTP, using a new TCP connection each time.
/*
New returns a pointer to a newly allocated RingBuffer with the parameters specified.
It initialises fields and allocates the required dataMemory.
*/
func (r *revidInst) sendClipToHTTP(clip []byte, output string) error {
timeout := time.Duration(httpTimeOut * time.Second)
client := http.Client{