mirror of
https://github.com/pion/mediadevices.git
synced 2025-09-26 20:41:46 +08:00
discard buffered frames for < 1fps (#470)
This commit is contained in:
@@ -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 {
|
||||
|
@@ -9,7 +9,7 @@ import (
|
||||
"github.com/pion/mediadevices/pkg/frame"
|
||||
)
|
||||
|
||||
// MediaConstraints represents set of media propaty constraints.
|
||||
// MediaConstraints represents set of media property constraints.
|
||||
// Each field constrains property by min/ideal/max range, exact match, or oneof match.
|
||||
type MediaConstraints struct {
|
||||
DeviceID StringConstraint
|
||||
@@ -229,16 +229,18 @@ func (c *comparisons) fitnessDistance() (float64, bool) {
|
||||
|
||||
// VideoConstraints represents a video's constraints
|
||||
type VideoConstraints struct {
|
||||
Width, Height IntConstraint
|
||||
FrameRate FloatConstraint
|
||||
FrameFormat FrameFormatConstraint
|
||||
Width, Height IntConstraint
|
||||
FrameRate FloatConstraint
|
||||
FrameFormat FrameFormatConstraint
|
||||
DiscardFramesOlderThan time.Duration
|
||||
}
|
||||
|
||||
// Video represents a video's constraints
|
||||
type Video struct {
|
||||
Width, Height int
|
||||
FrameRate float32
|
||||
FrameFormat frame.Format
|
||||
Width, Height int
|
||||
FrameRate float32
|
||||
FrameFormat frame.Format
|
||||
DiscardFramesOlderThan time.Duration
|
||||
}
|
||||
|
||||
// AudioConstraints represents an audio's constraints
|
||||
|
Reference in New Issue
Block a user