From 3e8484ec874043d7891aa11855d0f33ed13dbe28 Mon Sep 17 00:00:00 2001 From: seanavery Date: Fri, 13 Dec 2024 16:36:48 -0500 Subject: [PATCH 1/2] Throttle frame fetches by configured framerate --- cam/cam.go | 31 +++++++++++++++++-------------- 1 file changed, 17 insertions(+), 14 deletions(-) diff --git a/cam/cam.go b/cam/cam.go index 752d2ee..7eb685c 100644 --- a/cam/cam.go +++ b/cam/cam.go @@ -337,25 +337,28 @@ func (vs *videostore) Properties(_ context.Context) (camera.Properties, error) { // in the latestFrame atomic pointer. This routine runs as fast as possible // to keep the latest frame up to date. func (vs *videostore) fetchFrames(ctx context.Context) { + frameInterval := time.Second / time.Duration(vs.conf.Properties.Framerate) + ticker := time.NewTicker(frameInterval) + defer ticker.Stop() for { select { case <-ctx.Done(): return - default: - } - frame, err := camera.DecodeImageFromCamera(ctx, rutils.MimeTypeJPEG, nil, vs.cam) - if err != nil { - vs.logger.Warn("failed to get frame from camera", err) - time.Sleep(retryInterval * time.Second) - continue - } - lazyImage, ok := frame.(*rimage.LazyEncodedImage) - if !ok { - vs.logger.Error("frame is not of type *rimage.LazyEncodedImage") - return + case <-ticker.C: + frame, err := camera.DecodeImageFromCamera(ctx, rutils.MimeTypeJPEG, nil, vs.cam) + if err != nil { + vs.logger.Warn("failed to get frame from camera", err) + time.Sleep(retryInterval * time.Second) + continue + } + lazyImage, ok := frame.(*rimage.LazyEncodedImage) + if !ok { + vs.logger.Error("frame is not of type *rimage.LazyEncodedImage") + return + } + decodedImage := lazyImage.DecodedImage() + vs.latestFrame.Store(&decodedImage) } - decodedImage := lazyImage.DecodedImage() - vs.latestFrame.Store(&decodedImage) } } From ec7dee621c85b4634c16a0845480653e5e51f0d9 Mon Sep 17 00:00:00 2001 From: seanavery Date: Mon, 16 Dec 2024 13:34:21 -0500 Subject: [PATCH 2/2] Update comment --- cam/cam.go | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/cam/cam.go b/cam/cam.go index 7eb685c..c98d633 100644 --- a/cam/cam.go +++ b/cam/cam.go @@ -333,9 +333,8 @@ func (vs *videostore) Properties(_ context.Context) (camera.Properties, error) { return camera.Properties{}, nil } -// fetchFrames reads frames from the camera and stores the decoded image -// in the latestFrame atomic pointer. This routine runs as fast as possible -// to keep the latest frame up to date. +// fetchFrames reads frames from the camera at the framerate interval +// and stores the decoded image in the latestFrame atomic pointer. func (vs *videostore) fetchFrames(ctx context.Context) { frameInterval := time.Second / time.Duration(vs.conf.Properties.Framerate) ticker := time.NewTicker(frameInterval)