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

Devices with no capture formats cause panic #8

Closed
laptou opened this issue Aug 11, 2020 · 3 comments
Closed

Devices with no capture formats cause panic #8

laptou opened this issue Aug 11, 2020 · 3 comments

Comments

@laptou
Copy link

laptou commented Aug 11, 2020

  • Trying to enumerate the capture formats on devices that have none causes a panic.

Minimal reproducible example:

extern crate clap;
extern crate v4l;

use clap::{App, Arg};
use v4l::prelude::*;

fn main() {
    let matches = App::new("v4l device")
        .version("0.2")
        .author("Christopher N. Hesse <raymanfx@gmail.com>")
        .about("Video4Linux device example")
        .arg(
            Arg::with_name("device")
                .short("d")
                .long("device")
                .value_name("INDEX or PATH")
                .help("Device node path or index (default: 0)")
                .takes_value(true),
        )
        .get_matches();

    // Determine which device to use
    let mut path: String = matches
        .value_of("device")
        .unwrap_or("/dev/video0")
        .to_string();

    if path.parse::<u64>().is_ok() {
        path = format!("/dev/video{}", path);
    }

    println!("Using device: {}\n", path);

    let device = CaptureDevice::with_path(path).unwrap();

    println!("Formats:");
    
    for fmt in device.enum_formats().unwrap() {
        println!("\t{:#?}", fmt.description);
        println!("\t\tflags: {:#}", fmt.flags);
        println!("\t\tformat: {:#}", fmt.fourcc);
    }
}

Output:

v4l-rs-bug-repro is 📦 v0.1.0 via 🦀 v1.45.2 
❯ RUST_BACKTRACE=1 cargo run -- --device=/dev/video2
   Compiling v4l-rs-bug-repro v0.1.0 (/home/ibiyemi/projects/scratch/v4l-rs-bug-repro)
    Finished dev [unoptimized + debuginfo] target(s) in 0.56s
     Running `target/debug/v4l-rs-bug-repro --device=/dev/video2`
Using device: /dev/video2

Formats:
thread 'main' panicked at 'called `Result::unwrap()` on an `Err` value: Os { code: 22, kind: InvalidInput, message: "Invalid argument" }', src/main.rs:38:16
stack backtrace:
   0: backtrace::backtrace::libunwind::trace
             at /cargo/registry/src/github.com-1ecc6299db9ec823/backtrace-0.3.46/src/backtrace/libunwind.rs:86
   1: backtrace::backtrace::trace_unsynchronized
             at /cargo/registry/src/github.com-1ecc6299db9ec823/backtrace-0.3.46/src/backtrace/mod.rs:66
   2: std::sys_common::backtrace::_print_fmt
             at src/libstd/sys_common/backtrace.rs:78
   3: <std::sys_common::backtrace::_print::DisplayBacktrace as core::fmt::Display>::fmt
             at src/libstd/sys_common/backtrace.rs:59
   4: core::fmt::write
             at src/libcore/fmt/mod.rs:1076
   5: std::io::Write::write_fmt
             at src/libstd/io/mod.rs:1537
   6: std::sys_common::backtrace::_print
             at src/libstd/sys_common/backtrace.rs:62
   7: std::sys_common::backtrace::print
             at src/libstd/sys_common/backtrace.rs:49
   8: std::panicking::default_hook::{{closure}}
             at src/libstd/panicking.rs:198
   9: std::panicking::default_hook
             at src/libstd/panicking.rs:218
  10: std::panicking::rust_panic_with_hook
             at src/libstd/panicking.rs:486
  11: rust_begin_unwind
             at src/libstd/panicking.rs:388
  12: core::panicking::panic_fmt
             at src/libcore/panicking.rs:101
  13: core::option::expect_none_failed
             at src/libcore/option.rs:1272
  14: core::result::Result<T,E>::unwrap
             at /home/ibiyemi/.rustup/toolchains/stable-x86_64-unknown-linux-gnu/lib/rustlib/src/rust/src/libcore/result.rs:1005
  15: v4l_rs_bug_repro::main
             at src/main.rs:38
  16: std::rt::lang_start::{{closure}}
             at /home/ibiyemi/.rustup/toolchains/stable-x86_64-unknown-linux-gnu/lib/rustlib/src/rust/src/libstd/rt.rs:67
  17: std::rt::lang_start_internal::{{closure}}
             at src/libstd/rt.rs:52
  18: std::panicking::try::do_call
             at src/libstd/panicking.rs:297
  19: std::panicking::try
             at src/libstd/panicking.rs:274
  20: std::panic::catch_unwind
             at src/libstd/panic.rs:394
  21: std::rt::lang_start_internal
             at src/libstd/rt.rs:51
  22: std::rt::lang_start
             at /home/ibiyemi/.rustup/toolchains/stable-x86_64-unknown-linux-gnu/lib/rustlib/src/rust/src/libstd/rt.rs:67
  23: main
  24: __libc_start_main
  25: _start

Where /dev/video2 is a v4l2loopback device with no capture formats.

@raymanfx
Copy link
Owner

raymanfx commented Aug 14, 2020

I'll take a look at this tomorrow.

raymanfx added a commit that referenced this issue Aug 14, 2020
This is a quick bandaid to fix #8.
For the next version, we'll remove the io::Result wrapper and just return the
vec, which is allowed to be empty.

Signed-off-by: Christopher N. Hesse <raymanfx@gmail.com>
@raymanfx
Copy link
Owner

Hmm, thinking about this some more, I believe the Linux v4l2 API design is a bit lacking here.
From [1]: EINVAL - The struct v4l2_fmtdesc type is not supported or the index is out of bounds.
It might be an option to keep the current behavior, because we can't know whether the type is not supported or whether there are no formats.

[1] https://www.kernel.org/doc/html/v4.14/media/uapi/v4l/vidioc-enum-fmt.html

raymanfx added a commit that referenced this issue Aug 26, 2020
This is a quick bandaid to fix #8.
For the next version, we'll remove the io::Result wrapper and just return the
vec, which is allowed to be empty.

Signed-off-by: Christopher N. Hesse <raymanfx@gmail.com>
@raymanfx
Copy link
Owner

Fixed in 176f88d (released as part of 0.9.2).

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

No branches or pull requests

2 participants