Skip to content

Commit

Permalink
Merge pull request #129 from Naostage/fix-arena-buffer-usage-v4l
Browse files Browse the repository at this point in the history
🐛 v4l: fix perf issue introduce by disabled arena buffer
  • Loading branch information
l1npengtul authored Jun 15, 2023
2 parents 57e56ac + eff92a5 commit ea97124
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 0 deletions.
5 changes: 5 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,11 @@ decoding = ["nokhwa-core/mjpeg"]
input-avfoundation = ["nokhwa-bindings-macos"]
input-msmf = ["nokhwa-bindings-windows"]
input-v4l = ["nokhwa-bindings-linux"]
# Disable arena buffer on v4l
# This should fix crash on Raspberry Pi that have faulty v4l driver.
# WARNING: This create a performance regression, half of the frames will be dropped
# You shouldn't enable this unless you have to.
input-v4l-no-arena-buffer = ["nokhwa-bindings-linux/no-arena-buffer"]
input-native = ["input-avfoundation", "input-v4l", "input-msmf"]
# Re-enable it once soundness has been proven + mozjpeg is updated to 0.9.x
# input-uvc = ["uvc", "uvc/vendor", "usb_enumeration", "lazy_static"]
Expand Down
5 changes: 5 additions & 0 deletions nokhwa-bindings-linux/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,11 @@ description = "The V4L2 bindings crate for `nokhwa`"
keywords = ["v4l", "v4l2", "linux", "capture", "webcam"]
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html

[features]
# Disable arena buffer of v4l2 for the crate to work on raspberry pi with faulty v4l2 driver
# https://github.com/l1npengtul/nokhwa/pull/121
no-arena-buffer = []

[dependencies]

[dependencies.nokhwa-core]
Expand Down
5 changes: 5 additions & 0 deletions nokhwa-bindings-linux/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -853,12 +853,17 @@ mod internal {
}

fn open_stream(&mut self) -> Result<(), NokhwaError> {
// Disable mut warning, since mut is only required when not using arena buffers
#[allow(unused_mut)]
let mut stream =
match MmapStream::new(&*self.lock_device()?, v4l::buffer::Type::VideoCapture) {
Ok(s) => s,
Err(why) => return Err(NokhwaError::OpenStreamError(why.to_string())),
};

// Explicitly start now, or won't work with the RPi. As a consequence, buffers will only be used as required.
// WARNING: This will cause drop of half of the frames
#[cfg(feature = "no-arena-buffer")]
match stream.start() {
Ok(s) => s,
Err(why) => return Err(NokhwaError::OpenStreamError(why.to_string())),
Expand Down

0 comments on commit ea97124

Please sign in to comment.