diff --git a/cam/cam.go b/cam/cam.go index 752d2ee..c98d633 100644 --- a/cam/cam.go +++ b/cam/cam.go @@ -333,29 +333,31 @@ 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) + 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) } }