From 7d112175f61f822a01bbc8a6dc887a18308b522d Mon Sep 17 00:00:00 2001 From: Saxon Milton Date: Mon, 16 Apr 2018 14:42:16 +0930 Subject: [PATCH] Changed RevidInst to revid --- revid/Config.go | 2 +- revid/{RevidInstance.go => Revid.go} | 82 ++++++++++++++-------------- 2 files changed, 42 insertions(+), 42 deletions(-) rename revid/{RevidInstance.go => Revid.go} (87%) diff --git a/revid/Config.go b/revid/Config.go index 67e7ff39..06b99c1a 100644 --- a/revid/Config.go +++ b/revid/Config.go @@ -99,7 +99,7 @@ const ( // Validate checks for any errors in the config fields and defaults settings // if particular parameters have not been defined. -func (config *Config) Validate(r *revidInst) error { +func (config *Config) Validate(r *revid) error { switch config.Verbosity { case Yes: case No: diff --git a/revid/RevidInstance.go b/revid/Revid.go similarity index 87% rename from revid/RevidInstance.go rename to revid/Revid.go index bf8644eb..71e77236 100644 --- a/revid/RevidInstance.go +++ b/revid/Revid.go @@ -1,6 +1,6 @@ /* NAME - RevidInstance.go + Revid.go DESCRIPTION See Readme.md @@ -80,9 +80,9 @@ const ( Debug = "Debug" ) -// RevidInst provides methods to control a revidInst session; providing methods +// Revid provides methods to control a revid session; providing methods // to start, stop and change the state of an instance using the Config struct. -type RevidInst interface { +type Revid interface { Start() Stop() changeState(newconfig Config) error @@ -91,8 +91,8 @@ type RevidInst interface { IsRunning() bool } -// The revidInst struct provides fields to describe the state of a RevidInst. -type revidInst struct { +// The revid struct provides fields to describe the state of a Revid. +type revid struct { ffmpegPath string tempDir string ringBuffer ringbuffer.RingBuffer @@ -115,11 +115,11 @@ type revidInst struct { mutex sync.Mutex } -// NewRevidInstance returns a pointer to a new revidInst with the desired +// NewRevidInstance returns a pointer to a new revid 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) +func NewRevidInstance(config Config) (r *revid, err error) { + r = new(revid) r.mutex = sync.Mutex{} r.ringBuffer = ringbuffer.NewRingBuffer(ringBufferSize, ringBufferElementSize) err = r.changeState(config) @@ -136,13 +136,13 @@ func NewRevidInstance(config Config) (r *revidInst, err error) { } // GetConfigRef returns a pointer to the revidInst's Config struct object -func (r *revidInst) GetConfigRef() *Config { +func (r *revid) GetConfigRef() *Config { return &r.config } -// changeState swaps the current config of a revidInst with the passed +// changeState swaps the current config of a revid with the passed // configuration; checking validity and returning errors if not valid. -func (r *revidInst) changeState(config Config) error { +func (r *revid) changeState(config Config) error { r.config.Logger = config.Logger err := config.Validate(r) if err != nil { @@ -209,7 +209,7 @@ noPacketizationSetup: } // ChangeConfig changes the current configuration of the revid instance. -func (r *revidInst) ChangeConfig(config Config) (err error) { +func (r *revid) ChangeConfig(config Config) (err error) { r.Stop() r, err = NewRevidInstance(config) if err != nil { @@ -220,9 +220,9 @@ func (r *revidInst) ChangeConfig(config Config) (err error) { } // 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 the message +// logger provided in the revid config - if there is one, otherwise the message // is sent to stdout -func (r *revidInst) Log(logType, m string) { +func (r *revid) Log(logType, m string) { if r.config.Logger != nil { r.config.Logger.Log(logType, m) } else { @@ -232,18 +232,18 @@ func (r *revidInst) Log(logType, m string) { } } -// IsRunning returns true if the revidInst is currently running and false otherwise -func (r *revidInst) IsRunning() bool { +// IsRunning returns true if the revid is currently running and false otherwise +func (r *revid) IsRunning() bool { return r.isRunning } -// Start invokes a revidInst to start processing video from a defined input +// Start invokes a revid to start processing video from a defined input // and packetising (if theres packetization) to a defined output. -func (r *revidInst) Start() { +func (r *revid) Start() { r.mutex.Lock() defer r.mutex.Unlock() if r.isRunning { - r.Log(Warning, "revidInst.Start() called but revid already running!") + r.Log(Warning, "revid.Start() called but revid already running!") return } r.Log(Info, "Starting Revid!") @@ -260,11 +260,11 @@ func (r *revidInst) Start() { } // Stop halts any processing of video data from a camera or file -func (r *revidInst) Stop() { +func (r *revid) Stop() { r.mutex.Lock() defer r.mutex.Unlock() if !r.isRunning { - r.Log(Warning, "revidInst.Stop() called but revid not running!") + r.Log(Warning, "revid.Stop() called but revid not running!") return } r.Log(Info, "Stopping revid!") @@ -277,18 +277,18 @@ func (r *revidInst) Stop() { // getFrameNoPacketization gets a frame directly from the revid output chan // as we don't need to go through the generator with no packetization settings -func (r *revidInst) getFrameNoPacketization() []byte { +func (r *revid) getFrameNoPacketization() []byte { return <-r.outputChan } // getFramePacketization gets a frame from the generators output chan - the // the generator being an mpegts or flv generator depending on the config -func (r *revidInst) getFramePacketization() []byte { +func (r *revid) getFramePacketization() []byte { return <-(r.generator.GetOutputChan()) } // flushDataPacketization removes data from the revid inst's coutput chan -func (r *revidInst) flushData() { +func (r *revid) flushData() { for len(r.outputChan) > 0 { <-(r.outputChan) } @@ -296,7 +296,7 @@ func (r *revidInst) flushData() { // packClips takes data segments; whether that be tsPackets or mjpeg frames and // packs them into clips consisting of the amount frames specified in the config -func (r *revidInst) packClips() { +func (r *revid) packClips() { clipSize := 0 packetCount := 0 for { @@ -335,8 +335,8 @@ func reboot() { } // outputClips takes the clips produced in the packClips method and outputs them -// to the desired output defined in the revidInst config -func (r *revidInst) outputClips() { +// to the desired output defined in the revid config +func (r *revid) outputClips() { now := time.Now() prevTime := now bytes := 0 @@ -392,7 +392,7 @@ func (r *revidInst) outputClips() { } // senClipToFile writes the passed clip to a file -func (r *revidInst) sendClipToFile(clip []byte) error { +func (r *revid) sendClipToFile(clip []byte) error { _, err := r.outputFile.Write(clip) if err != nil { return err @@ -401,7 +401,7 @@ func (r *revidInst) sendClipToFile(clip []byte) error { } // sendClipToHTTP takes a clip and an output url and posts through http. -func (r *revidInst) sendClipToHTTP(clip []byte) error { +func (r *revid) sendClipToHTTP(clip []byte) error { timeout := time.Duration(httpTimeOut * time.Second) client := http.Client{Timeout: timeout} url := r.config.HttpAddress + strconv.Itoa(len(clip)) @@ -422,20 +422,20 @@ func (r *revidInst) sendClipToHTTP(clip []byte) error { // sendClipToFfmpegRtmp sends the clip over the current rtmp connection using // an ffmpeg process. -func (r *revidInst) sendClipToFfmpegRtmp(clip []byte) (err error) { +func (r *revid) sendClipToFfmpegRtmp(clip []byte) (err error) { _, err = r.ffmpegStdin.Write(clip) return } // sendClipToLibRtmp send the clip over the current rtmp connection using the // c based librtmp library -func (r *revidInst) sendClipToLibRtmp(clip []byte) (err error) { +func (r *revid) sendClipToLibRtmp(clip []byte) (err error) { err = r.rtmpInst.WriteFrame(clip, uint(len(clip))) return } // setupOutputForFfmpegRtmp sets up output to rtmp using an ffmpeg process -func (r *revidInst) setupOutputForFfmpegRtmp() error { +func (r *revid) setupOutputForFfmpegRtmp() error { r.ffmpegCmd = exec.Command(ffmpegPath, "-f", "h264", "-r", r.config.FrameRate, @@ -469,7 +469,7 @@ func (r *revidInst) setupOutputForFfmpegRtmp() error { // setupOutputForLibRtmp sets up rtmp output using the wrapper for the c based // librtmp library - makes connection and starts comms etc. -func (r *revidInst) setupOutputForLibRtmp() (err error) { +func (r *revid) setupOutputForLibRtmp() (err error) { r.rtmpInst = rtmp.NewRTMPSession(r.config.RtmpUrl, rtmpConnectionTimout) err = r.rtmpInst.StartSession() // go r.testRtmp(5000) @@ -477,14 +477,14 @@ func (r *revidInst) setupOutputForLibRtmp() (err error) { } // setupOutputForFile sets up an output file to output data to -func (r *revidInst) setupOutputForFile() (err error) { +func (r *revid) setupOutputForFile() (err error) { r.outputFile, err = os.Create(r.config.OutputFileName) return } // setupInputForRaspivid sets up things for input from raspivid i.e. starts // a raspivid process and pipes it's data output. -func (r *revidInst) setupInputForRaspivid() error { +func (r *revid) setupInputForRaspivid() error { r.Log(Info, "Starting raspivid!") switch r.config.InputCodec { case H264: @@ -539,7 +539,7 @@ func (r *revidInst) setupInputForRaspivid() error { } // setupInputForFile sets things up for getting input from a file -func (r *revidInst) setupInputForFile() error { +func (r *revid) setupInputForFile() error { fps, _ := strconv.Atoi(r.config.FrameRate) r.parser.SetDelay(uint(float64(1000) / float64(fps))) r.readFile() @@ -549,7 +549,7 @@ func (r *revidInst) setupInputForFile() error { // testRtmp is useful to check robustness of connections. Intended to be run as // goroutine. After every 'delayTime' the rtmp connection is ended and then // restarted -func (r *revidInst) testRtmp(delayTime uint) { +func (r *revid) testRtmp(delayTime uint) { for { time.Sleep(time.Duration(delayTime) * time.Millisecond) r.rtmpInst.EndSession() @@ -557,9 +557,9 @@ func (r *revidInst) testRtmp(delayTime uint) { } } -// readCamera reads data from the defined camera while the revidInst is running. +// readCamera reads data from the defined camera while the revid is running. // TODO: use ringbuffer here instead of allocating mem every time! -func (r *revidInst) readCamera() { +func (r *revid) readCamera() { r.Log(Info, "Reading camera data!") for r.isRunning { data := make([]byte, 1) @@ -581,8 +581,8 @@ func (r *revidInst) readCamera() { r.Log(Info, "Not trying to read from camera anymore!") } -// readFile reads data from the defined file while the revidInst is running. -func (r *revidInst) readFile() error { +// readFile reads data from the defined file while the revid is running. +func (r *revid) readFile() error { var err error r.inputFile, err = os.Open(r.config.InputFileName) if err != nil {