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-9702] - Do not return odd resolutions from GetStreamOptions Response #4693

Merged
merged 3 commits into from
Jan 9, 2025
Merged
Show file tree
Hide file tree
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
16 changes: 15 additions & 1 deletion robot/web/stream/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -422,6 +422,13 @@ func validateSetStreamOptionsRequest(req *streampb.SetStreamOptionsRequest) (int
req.Name, req.Resolution.Width, req.Resolution.Height,
)
}
if req.Resolution.Width%2 != 0 || req.Resolution.Height%2 != 0 {
return optionsCommandUnknown,
fmt.Errorf(
"invalid resolution to resize stream %q: width (%d) and height (%d) must be even",
req.Name, req.Resolution.Width, req.Resolution.Height,
)
}
return optionsCommandResize, nil
}

Expand Down Expand Up @@ -731,11 +738,18 @@ func GenerateResolutions(width, height int32, logger logging.Logger) []Resolutio
// original aspect ratio exactly if source dimensions are odd.
for i := 0; i < 4; i++ {
// Break if the next scaled resolution would be too small.
if width <= 1 || height <= 1 {
if width <= 2 || height <= 2 {
break
}
width /= 2
height /= 2
// Ensure width and height are even
if width%2 != 0 {
width--
}
if height%2 != 0 {
height--
}
resolutions = append(resolutions, Resolution{Width: width, Height: height})
logger.Debugf("scaled resolution %d: %dx%d", i, width, height)
}
Expand Down
36 changes: 35 additions & 1 deletion robot/web/stream/stream_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -286,6 +286,27 @@ func TestGetStreamOptions(t *testing.T) {
Model: true,
},
},
// Odd resolutions gets rounded to nearest even resolution
// 100x100 downscaled to 25*25 turns into 24*24
{
Name: "fake-cam-4-0",
API: resource.NewAPI("rdk", "component", "camera"),
Model: resource.DefaultModelFamily.WithModel("fake"),
ConvertedAttributes: &fake.Config{
Width: 100,
Height: 100,
},
},
{
Name: "fake-cam-4-1",
API: resource.NewAPI("rdk", "component", "camera"),
Model: resource.DefaultModelFamily.WithModel("fake"),
ConvertedAttributes: &fake.Config{
Width: 100,
Height: 100,
Model: true,
},
},
}}

ctx, robot, addr, webSvc := setupRealRobot(t, origCfg, logger)
Expand All @@ -298,7 +319,7 @@ func TestGetStreamOptions(t *testing.T) {
livestreamClient := streampb.NewStreamServiceClient(conn)
listResp, err := livestreamClient.ListStreams(ctx, &streampb.ListStreamsRequest{})
test.That(t, err, test.ShouldBeNil)
test.That(t, len(listResp.Names), test.ShouldEqual, 8)
test.That(t, len(listResp.Names), test.ShouldEqual, 10)

streamOptionsResp, err := livestreamClient.GetStreamOptions(ctx, &streampb.GetStreamOptionsRequest{})
test.That(t, err, test.ShouldNotBeNil)
Expand Down Expand Up @@ -349,6 +370,8 @@ func TestGetStreamOptions(t *testing.T) {
"fake-cam-2-1": webstream.GenerateResolutions(1920, 1080, logger),
"fake-cam-3-0": webstream.GenerateResolutions(2, 2, logger),
"fake-cam-3-1": webstream.GenerateResolutions(2, 2, logger),
"fake-cam-4-0": webstream.GenerateResolutions(100, 100, logger),
"fake-cam-4-1": webstream.GenerateResolutions(100, 100, logger),
}

// Test each camera
Expand Down Expand Up @@ -431,6 +454,17 @@ func TestSetStreamOptions(t *testing.T) {
test.That(t, err.Error(), test.ShouldContainSubstring, "invalid resolution")
})

// Test setting stream options with an odd resolution
t.Run("SetStreamOptions with odd resolution", func(t *testing.T) {
setStreamOptionsResp, err := livestreamClient.SetStreamOptions(ctx, &streampb.SetStreamOptionsRequest{
Name: "fake-cam-0-0",
Resolution: &streampb.Resolution{Width: 25, Height: 25},
})
test.That(t, err, test.ShouldNotBeNil)
test.That(t, setStreamOptionsResp, test.ShouldBeNil)
test.That(t, err.Error(), test.ShouldContainSubstring, "invalid resolution")
})

t.Run("AddStream creates video track", func(t *testing.T) {
res, err := livestreamClient.AddStream(ctx, &streampb.AddStreamRequest{
Name: "fake-cam-0-0",
Expand Down
Loading