Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[RSDK-9538] - Throttle frame fetches by configured framerate #30

Merged
merged 2 commits into from
Dec 16, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
36 changes: 19 additions & 17 deletions cam/cam.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
}

Expand Down
Loading