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" "../../utils/smartLogger"
) )
// defaults and networking consts // Misc constants
const ( const (
clipDuration = 1 // s clipDuration = 1 // s
mp2tPacketSize = 188 // MPEG-TS packet size mp2tPacketSize = 188 // MPEG-TS packet size
@ -79,19 +79,8 @@ const (
Debug = "Debug" Debug = "Debug"
) )
// Config enums // Config provides parameters relevant to a revid instance. A new config must
const ( // be passed to the constructor.
raspivid = 1
rtp = 2
h264Codec = 3
file = 4
httpOut = 5
h264 = 6
mjpeg = 7
none = 8
mpegts = 9
)
type Config struct { type Config struct {
Input uint8 Input uint8
InputCodec uint8 InputCodec uint8
@ -109,6 +98,19 @@ type Config struct {
Logger smartLogger.LogInstance 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 // Default config settings
const ( const (
defaultFrameRate = "25" defaultFrameRate = "25"
@ -117,6 +119,8 @@ const (
defaultIntraRefreshPeriod = "100" 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 { type RevidInst interface {
Start() Start()
Stop() Stop()
@ -126,9 +130,9 @@ type RevidInst interface {
IsRunning() bool IsRunning() bool
} }
// The revidInst struct provides fields to describe the state of a RevidInst.
type revidInst struct { type revidInst struct {
dumpPCRBase uint64
conn net.Conn
ffmpegPath string ffmpegPath string
tempDir string tempDir string
ringBuffer ringbuffer.RingBuffer ringBuffer ringbuffer.RingBuffer
@ -143,10 +147,12 @@ type revidInst struct {
mjpegOutputChan chan []byte 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) { 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.dumpPCRBase = 0
r.ChangeState(config) r.ChangeState(config)
switch r.config.Output { switch r.config.Output {
case file: case file:
@ -184,10 +190,13 @@ func NewRevidInstance(config Config) (r *revidInst, err error) {
return return
} }
// GetConfigRef returns a pointer to the revidInst's Config struct object
func (r *revidInst) GetConfigRef() *Config { func (r *revidInst) GetConfigRef() *Config {
return &r.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 { func (r *revidInst) ChangeState(config Config) error {
switch config.Input { switch config.Input {
case rtp: case rtp:
@ -242,6 +251,9 @@ func (r *revidInst) ChangeState(config Config) error {
return nil 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 { func (r *revidInst) Log(logType, m string) error {
if r.config.Logger != nil { if r.config.Logger != nil {
r.config.Logger.Log(logType, m) 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 { func (r *revidInst) IsRunning() bool {
return r.isRunning 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() { func (r *revidInst) Start() {
if r.isRunning { if r.isRunning {
r.Log(Warning, "revidInst.Start() called but revid already running!") r.Log(Warning, "revidInst.Start() called but revid already running!")
@ -314,6 +329,7 @@ func (r *revidInst) Start() {
go r.outputClips() go r.outputClips()
} }
// readCamera reads data from the defined camera while the revidInst is running.
func (r *revidInst)readCamera() { func (r *revidInst)readCamera() {
r.Log(Info, "Reading camera data!") r.Log(Info, "Reading camera data!")
for r.isRunning { for r.isRunning {
@ -332,6 +348,7 @@ func (r *revidInst)readCamera() {
r.Log(Info, "Out of reading routine!") r.Log(Info, "Out of reading routine!")
} }
// Stop
func (r *revidInst) Stop() { func (r *revidInst) Stop() {
if r.isRunning { if r.isRunning {
r.Log(Info, "Stopping revid!") 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() { func (r *revidInst) packClips() {
clipSize := 0 clipSize := 0
packetCount := 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() { func (r *revidInst) outputClips() {
now := time.Now() now := time.Now()
prevTime := 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 { func (r *revidInst) sendClipToHTTP(clip []byte, output string) error {
timeout := time.Duration(httpTimeOut * time.Second) timeout := time.Duration(httpTimeOut * time.Second)
client := http.Client{ client := http.Client{