stream/mts: patch for revid.Start() no exit bug

This commit is contained in:
saxon 2019-01-31 15:03:50 +10:30
parent 197ca684a6
commit ed4d97f893
1 changed files with 38 additions and 13 deletions

View File

@ -120,6 +120,8 @@ type Revid struct {
// isRunning is a loaded and cocked foot-gun. // isRunning is a loaded and cocked foot-gun.
isRunning bool isRunning bool
err chan error
} }
// packer takes data segments and packs them into clips // packer takes data segments and packs them into clips
@ -176,13 +178,29 @@ func (p *packer) Write(frame []byte) (int, error) {
// New returns a pointer to a new Revid with the desired configuration, and/or // New returns a pointer to a new Revid with the desired configuration, and/or
// an error if construction of the new instance was not successful. // an error if construction of the new instance was not successful.
func New(c Config, ns *netsender.Sender) (*Revid, error) { func New(c Config, ns *netsender.Sender) (*Revid, error) {
r := Revid{ns: ns} r := Revid{ns: ns, err: make(chan error)}
r.buffer = ring.NewBuffer(ringBufferSize, ringBufferElementSize, writeTimeout) r.buffer = ring.NewBuffer(ringBufferSize, ringBufferElementSize, writeTimeout)
r.packer.owner = &r r.packer.owner = &r
err := r.reset(c) err := r.reset(c)
if err != nil { if err != nil {
return nil, err return nil, err
} }
go func() {
for {
err := <-r.err
if err != nil {
r.config.Logger.Log(logger.Error, pkg+"async error", "error", err.Error())
err = r.Stop()
if err != nil {
r.config.Logger.Log(logger.Fatal, pkg+"failed to stop", "error", err.Error())
}
r.Start()
if err != nil {
r.config.Logger.Log(logger.Fatal, pkg+"failed to restart", "error", err.Error())
}
}
}
}()
return &r, nil return &r, nil
} }
@ -312,8 +330,8 @@ func (r *Revid) Start() error {
r.config.Logger.Log(logger.Info, pkg+"starting output routine") r.config.Logger.Log(logger.Info, pkg+"starting output routine")
go r.outputClips() go r.outputClips()
r.config.Logger.Log(logger.Info, pkg+"setting up input and receiving content") r.config.Logger.Log(logger.Info, pkg+"setting up input and receiving content")
err := r.setupInput() go r.setupInput()
return err return nil
} }
// Stop halts any processing of video data from a camera or file // Stop halts any processing of video data from a camera or file
@ -474,11 +492,13 @@ func (r *Revid) startRaspivid() error {
r.config.Logger.Log(logger.Fatal, pkg+"cannot start raspivid", "error", err.Error()) r.config.Logger.Log(logger.Fatal, pkg+"cannot start raspivid", "error", err.Error())
} }
go func() {
r.config.Logger.Log(logger.Info, pkg+"reading camera data") r.config.Logger.Log(logger.Info, pkg+"reading camera data")
delay := time.Second / time.Duration(r.config.FrameRate) delay := time.Second / time.Duration(r.config.FrameRate)
err = r.lexTo(r.encoder, stdout, delay) r.err <- r.lexTo(r.encoder, stdout, delay)
r.config.Logger.Log(logger.Info, pkg+"finished reading camera data") r.config.Logger.Log(logger.Info, pkg+"finished reading camera data")
return err }()
return nil
} }
func (r *Revid) startV4L() error { func (r *Revid) startV4L() error {
@ -526,10 +546,12 @@ func (r *Revid) startV4L() error {
return err return err
} }
go func() {
r.config.Logger.Log(logger.Info, pkg+"reading camera data") r.config.Logger.Log(logger.Info, pkg+"reading camera data")
err = r.lexTo(r.encoder, stdout, delay) r.err <- r.lexTo(r.encoder, stdout, delay)
r.config.Logger.Log(logger.Info, pkg+"finished reading camera data") r.config.Logger.Log(logger.Info, pkg+"finished reading camera data")
return err }()
return nil
} }
// setupInputForFile sets things up for getting input from a file // setupInputForFile sets things up for getting input from a file
@ -544,5 +566,8 @@ func (r *Revid) setupInputForFile() error {
defer f.Close() defer f.Close()
// TODO(kortschak): Maybe we want a context.Context-aware parser that we can stop. // TODO(kortschak): Maybe we want a context.Context-aware parser that we can stop.
return r.lexTo(r.encoder, f, delay) go func() {
r.err <- r.lexTo(r.encoder, f, delay)
}()
return nil
} }