device/file & revid/pipeline.go: add looping functionality to file device and removing from processFrom

This commit is contained in:
Saxon Nelson-Milton 2020-12-19 12:52:49 +10:30
parent 528733c559
commit 6506a3021f
2 changed files with 45 additions and 35 deletions

View File

@ -82,7 +82,26 @@ func (m *AVFile) Stop() error {
// called and Stop has since been called, an error is returned.
func (m *AVFile) Read(p []byte) (int, error) {
if m.f != nil {
return m.f.Read(p)
n, err := m.f.Read()
if err != nil {
// In the case that we reach end of file but loop is true, we want to
// seek to start and keep reading from there.
if err == io.EOF && m.cfg.Loop {
_, err = m.f.Seek(0,io.SeekStart)
if err != nil {
return 0, fmt.Errorf("could not seek to start of file for input loop: %w",err)
}
// Now that we've seeked to start, let's try reading again.
n, err = m.f.Read()
if err != nil {
return n, fmt.Errorf("could not read after start seek: %w",err)
}
return n, nil
}
return n, err
}
return n, nil
}
return 0, errors.New("AV file is closed")
}

View File

@ -332,7 +332,6 @@ func (r *Revid) setLexer(c uint8, isRTSP bool) error {
func (r *Revid) processFrom(in device.AVDevice, delay time.Duration) {
defer r.wg.Done()
for l := true; l; l = r.cfg.Loop {
err := in.Start()
if err != nil {
r.err <- fmt.Errorf("could not start input device: %w", err)
@ -361,12 +360,4 @@ func (r *Revid) processFrom(in device.AVDevice, delay time.Duration) {
} else {
r.cfg.Logger.Log(logger.Info, "input stopped")
}
// If we're looping and we get a stop signal we return.
select {
case <-r.stop:
return
default:
}
}
}