From a0056750ea2074d99a0bf6c75e54a3d8747669b2 Mon Sep 17 00:00:00 2001 From: Julie Krasnick Date: Thu, 26 Sep 2024 16:05:28 -0400 Subject: [PATCH] validating framerate --- components/camera/videosource/webcam.go | 7 ++++- components/camera/videosource/webcam_test.go | 27 ++++++++++++++++---- 2 files changed, 28 insertions(+), 6 deletions(-) diff --git a/components/camera/videosource/webcam.go b/components/camera/videosource/webcam.go index 741d2d6973c..a60d62f4e00 100644 --- a/components/camera/videosource/webcam.go +++ b/components/camera/videosource/webcam.go @@ -157,6 +157,11 @@ func (c WebcamConfig) Validate(path string) ([]string, error) { "got illegal negative dimensions for width_px and height_px (%d, %d) fields set for webcam camera", c.Height, c.Width) } + if c.FrameRate <= 0 { + return nil, fmt.Errorf( + "got illegal non-positive dimension for frame rate (%.2f) field set for webcam camera", + c.FrameRate) + } return []string{}, nil } @@ -640,7 +645,7 @@ func (c *monitoredWebcam) Properties(ctx context.Context) (camera.Properties, er } props.IntrinsicParams = &cameraIntrinsics - if c.conf.FrameRate != 0 { + if c.conf.FrameRate > 0 { props.FrameRate = c.conf.FrameRate } } diff --git a/components/camera/videosource/webcam_test.go b/components/camera/videosource/webcam_test.go index c3c9350bb22..e07c636a5ad 100644 --- a/components/camera/videosource/webcam_test.go +++ b/components/camera/videosource/webcam_test.go @@ -57,33 +57,50 @@ func TestDiscoveryWebcam(t *testing.T) { func TestWebcamValidation(t *testing.T) { webCfg := &videosource.WebcamConfig{ - Width: 1280, - Height: 640, + Width: 1280, + Height: 640, + FrameRate: 100, } - // no error with positive width and height + // no error with positive width, height, and frame rate deps, err := webCfg.Validate("path") test.That(t, err, test.ShouldBeNil) test.That(t, deps, test.ShouldResemble, []string{}) + // no error with 0 width and 0 height webCfg.Width = 0 webCfg.Height = 0 deps, err = webCfg.Validate("path") test.That(t, err, test.ShouldBeNil) test.That(t, deps, test.ShouldResemble, []string{}) - // error with a negative width and positive height + // error with a negative width webCfg.Width = -200 deps, err = webCfg.Validate("path") test.That(t, err.Error(), test.ShouldEqual, "got illegal negative dimensions for width_px and height_px (0, -200) fields set for webcam camera") test.That(t, deps, test.ShouldBeNil) - // error with a positive width and negative height + // error with a negative height webCfg.Width = 200 webCfg.Height = -200 deps, err = webCfg.Validate("path") test.That(t, err.Error(), test.ShouldEqual, "got illegal negative dimensions for width_px and height_px (-200, 200) fields set for webcam camera") test.That(t, deps, test.ShouldBeNil) + + // error with a negative frame rate + webCfg.Height = 200 + webCfg.FrameRate = -100 + deps, err = webCfg.Validate("path") + test.That(t, err.Error(), test.ShouldEqual, + "got illegal non-positive dimension for frame rate (-100.00) field set for webcam camera") + test.That(t, deps, test.ShouldBeNil) + + // error with a 0 frame rate + webCfg.FrameRate = 0 + deps, err = webCfg.Validate("path") + test.That(t, err.Error(), test.ShouldEqual, + "got illegal non-positive dimension for frame rate (0.00) field set for webcam camera") + test.That(t, deps, test.ShouldBeNil) }