discard buffered frames for < 1fps (#470)

This commit is contained in:
Clyde Bazile
2023-03-06 16:52:59 -05:00
committed by GitHub
parent dbd37689e4
commit 62009a882b
2 changed files with 29 additions and 9 deletions
+20 -2
View File
@@ -12,6 +12,7 @@ import (
"path/filepath"
"strconv"
"sync"
"time"
"github.com/blackjack/webcam"
"github.com/pion/mediadevices/pkg/driver"
@@ -70,6 +71,7 @@ type camera struct {
started bool
mutex sync.Mutex
cancel func()
prevFrameTime time.Time
}
func init() {
@@ -158,8 +160,13 @@ func (c *camera) Open() error {
return err
}
// Late frames should be discarded. Buffering should be handled in higher level.
cam.SetBufferCount(1)
// Buffering should be handled in higher level.
err = cam.SetBufferCount(1)
if err != nil {
return err
}
c.prevFrameTime = time.Now()
c.cam = cam
return nil
}
@@ -228,6 +235,13 @@ func (c *camera) VideoRecord(p prop.Media) (video.Reader, error) {
return nil, func() {}, io.EOF
}
if p.DiscardFramesOlderThan != 0 {
if time.Now().Sub(c.prevFrameTime) >= p.DiscardFramesOlderThan {
_ = cam.WaitForFrame(readTimeoutSec)
_, _ = cam.ReadFrame()
}
}
err := cam.WaitForFrame(readTimeoutSec)
switch err.(type) {
case nil:
@@ -244,6 +258,10 @@ func (c *camera) VideoRecord(p prop.Media) (video.Reader, error) {
return nil, func() {}, err
}
if p.DiscardFramesOlderThan != 0 {
c.prevFrameTime = time.Now()
}
// Frame is empty.
// Retry reading and return errEmptyFrame if it exceeds maxEmptyFrameCount.
if len(b) == 0 {