Skip to content

Commit

Permalink
validating framerate
Browse files Browse the repository at this point in the history
  • Loading branch information
jckras committed Sep 26, 2024
1 parent 726e274 commit a005675
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 6 deletions.
7 changes: 6 additions & 1 deletion components/camera/videosource/webcam.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down Expand Up @@ -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
}
}
Expand Down
27 changes: 22 additions & 5 deletions components/camera/videosource/webcam_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}

0 comments on commit a005675

Please sign in to comment.