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