Skip to content

Refactor emulated_camera to use a shared video_capture_device library#2653

Open
bridadan wants to merge 3 commits into
google:mainfrom
bridadan:dedup_virtio_media_code
Open

Refactor emulated_camera to use a shared video_capture_device library#2653
bridadan wants to merge 3 commits into
google:mainfrom
bridadan:dedup_virtio_media_code

Conversation

@bridadan
Copy link
Copy Markdown
Collaborator

@bridadan bridadan commented Jun 3, 2026

Extract shared code from emulated_camera_mplane and emulated_camera_splane into a new shared library video_capture_device. This removes a large amount of copy-pasted code between the two emulated camera implementations.

Key changes:

  • Created video_capture_device library containing:
    • CaptureDeviceFormat trait defining format-specific constants and methods.
    • Generic EmulatedCamera and EmulatedCameraSession structs implementing VirtioMediaDevice and VirtioMediaIoctlHandler.
    • Common Buffer and BufferState implementations.
    • A default implementation of configure_planes in the trait, as it was identical for both devices.
  • Refactored emulated_camera_mplane and emulated_camera_splane to:
    • Depend on the new video_capture_device library.
    • Implement CaptureDeviceFormat for their respective format structs (MplaneFormat and SplaneFormat).
    • Use type aliases to map their local EmulatedCamera to the generic one.
  • Updated Cargo.toml and BUILD.bazel files to reflect new dependencies.

TAG=agy
CONV=b4f9ffcf-c393-4dbc-b8fb-5c38416291c2

Bug: 519646531
Test: build and launch with both device types, ensure v4l2-compliance
passes in the guest

@bridadan bridadan requested a review from ser-io June 3, 2026 19:32
Copy link
Copy Markdown
Member

@ser-io ser-io left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for this needed refactoring.

Can you split the PR into two commits (parts)

  1. Refactor code that's clearly duplicated among the two devices implemention without CaptureDeviceFormat abstraction.
  2. Introduce the new CaptureDeviceFormat abstraction and the refactored code around it.

I'd like to analyze the new abstraction of a commit of its own. Thanks.

@bridadan bridadan force-pushed the dedup_virtio_media_code branch from 774f514 to 3ba52ec Compare June 4, 2026 15:45
@bridadan
Copy link
Copy Markdown
Collaborator Author

bridadan commented Jun 4, 2026

@ser-io Here's an attempt at splitting up the changes. The impl<Q, HM, Reader, Writer> VirtioMediaDevice<Reader, Writer> for EmulatedCamera<Q, HM> block has some very minor dependencies on the plane type, so if the PR is still hard to read, I could insert a third commit that only deduplicates that implementation based on the plane type. Let me know if that would be useful!

}
}

pub fn enum_framesizes(
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This helper function is not extensible. If I were to add a new frame size to my device implementation, I cannot reuse this function.

Let's keep this responsibility on the device implementation side.




pub fn default_fmtdesc(queue: QueueType, pixelformat: u32) -> v4l2_fmtdesc {
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No need to introduce this new helper as it just return a struct. It's more clear to return the struct directly on the device implementation.

})
}

pub fn enum_frameintervals(
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

same as enum_framesizes.

@@ -0,0 +1,10 @@
[package]
name = "video_capture_device"
Copy link
Copy Markdown
Member

@ser-io ser-io Jun 4, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This should be a new module pub mod devices in lib.rs

The directory "vhu_media" is the library code containing the shared code to be used by the binaries. Eventually we need to split the "lib.rs" file into different files. Feel free to do this if you want in this PR, however it's not a requirement for this PR.

})
}

pub fn configure_planes(v4l2_buffer: &mut V4l2Buffer, offset: u32, buffer_size: u32) {
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We can be specific with the function name for readability in this case: set_plane_offset_and_length.

Brian Daniels added 2 commits June 5, 2026 15:58
Extract shared code from `emulated_camera_mplane` and `emulated_camera_splane`
into a new `devices` module of `vhu_media`. This version extracts only
obviously duplicated code (structs, enums, and helper functions).

Key changes:
- Created `devices` module in `vhu_media` library containing:
  - `Buffer` and `BufferState` implementations.
- Refactored `emulated_camera_mplane` and `emulated_camera_splane` to:
  - Depend on the new `devices` module.
  - Use the shared `Buffer` and `BufferState`.
- Updated Cargo.toml and BUILD.bazel files to reflect new dependencies.

TAG=agy
CONV=b4f9ffcf-c393-4dbc-b8fb-5c38416291c2

Bug: 519646531
Test: cd base/cvd && bazel build cuttlefish/package:cvd
…ptureDeviceFormat trait

Extract `EmulatedCamera` and `EmulatedCameraSession` structs and their
`VirtioMediaDevice` and `VirtioMediaIoctlHandler` implementations into the shared
`vhu_media` library to eliminate duplication.

Introduce the `CaptureDeviceFormat` trait in the shared library to abstract
format-specific differences between `emulated_camera_mplane` and
`emulated_camera_splane`.

Key changes:
- Created `CaptureDeviceFormat` trait in `vhu_media`.
- Moved `Buffer`, `BufferState`, `EmulatedCameraSession`, and `EmulatedCamera`
  to `vhu_media`, making the camera and session generic over
  `F: CaptureDeviceFormat`.
- Moved and unified `VirtioMediaDevice` and `VirtioMediaIoctlHandler`
  implementations to `vhu_media`.
- Refactored `emulated_camera_mplane` and `emulated_camera_splane` to:
  - Implement `CaptureDeviceFormat` for `MplaneFormat` and `SplaneFormat` respectively.
  - Implement `default_fmt` and `write_pattern` directly within the trait
    impl blocks.
  - Instantiate the generic `EmulatedCamera` in `main.rs`.

TAG=agy
CONV=b4f9ffcf-c393-4dbc-b8fb-5c38416291c2

Bug: 519646531
Test: cd base/cvd && bazel build cuttlefish/package:cvd
      # Repeat with v4l2_emulated_camera_mplane
      cvd create -media=type=v4l2_emulated_camera_splane
      # Ensure pixel format changes with each type
      v4l2-ctl -d1 --all
      # Ensure it passes with no failures
      v4l2-compliance -d1 -s
@bridadan bridadan force-pushed the dedup_virtio_media_code branch from 3ba52ec to 33297c9 Compare June 5, 2026 20:09
Bug: 519646531
Test: cd base/cvd && bazel build cuttlefish/package:cvd
      # Repeat with v4l2_emulated_camera_mplane
      cvd create -media=type=v4l2_emulated_camera_splane
      # Ensure pixel format changes with each type
      v4l2-ctl -d1 --all
      # Ensure it passes with no failures
      v4l2-compliance -d1 -s
@bridadan bridadan force-pushed the dedup_virtio_media_code branch from 33297c9 to 7c6c405 Compare June 5, 2026 20:11
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants