3个版本 (破坏性更新)

0.3.0 2023年5月7日
0.2.0 2021年1月7日
0.1.0 2020年5月3日

#239 in 视频

Download history 2371/week @ 2024-03-14 3016/week @ 2024-03-21 1970/week @ 2024-03-28 2636/week @ 2024-04-04 2507/week @ 2024-04-11 2514/week @ 2024-04-18 2387/week @ 2024-04-25 2579/week @ 2024-05-02 2102/week @ 2024-05-09 2411/week @ 2024-05-16 1965/week @ 2024-05-23 2132/week @ 2024-05-30 2191/week @ 2024-06-06 2015/week @ 2024-06-13 2076/week @ 2024-06-20 2559/week @ 2024-06-27

9,315 每月下载量
4 个包中使用了(直接使用2个)

MIT 许可

2KB

安全视频4Linux (v4l) 绑定

crates.io license Build Status

此crate提供了对Video for Linux (V4L)堆栈的安全绑定。现代设备驱动程序通常会实现v4l2API,而较旧的驱动程序可能依赖于传统的v4lAPI。可以通过选择此crate的libv4l功能来使用此crate来使用此类传统设备。

目标

此crate应提供v4l-sys包,以启用对libv4l*的完全(但不安全)访问。在此基础上,还将提供更高级、更符合习惯的API来在Linux中使用视频捕获设备。

将提供简单的实用程序应用程序来列出设备和捕获帧。计划未来提供一个最小化的OpenGL/Vulkan查看器来显示帧。

变更日志

请参阅CHANGELOG.md

依赖项

您可以选择两种依赖项(均由此crate内部提供)

  • libv4l-sys

    链接到包括libv4l1、libv4l2、libv4lconvert在内的libv4l*堆栈。这具有通过libv4lconvert等在用户空间中模拟常见捕获格式(如RGB3)的优点。但是,libv4l可能不支持某些功能,如userptr缓冲区。

  • v4l2-sys

    仅使用Linux内核提供的由videodev2.h提供的v4l2 API。您将获得所有v4l2功能的支持,例如userptr缓冲区,但可能需要自行进行格式转换,例如需要RGB/BGR缓冲区,而此类缓冲区可能不受如网络摄像头等通用设备的支持。

通过选择作为此crate功能的libv4lv4l2后端来启用。

使用方法

以下提供了此crate的快速使用示例。它介绍了从流设备(例如网络摄像头)捕获帧所需的基本知识。

use v4l::buffer::Type;
use v4l::io::mmap::Stream;
use v4l::io::traits::CaptureStream;
use v4l::video::Capture;
use v4l::Device;
use v4l::FourCC;

fn main() {
    // Create a new capture device with a few extra parameters
    let mut dev = Device::new(0).expect("Failed to open device");

    // Let's say we want to explicitly request another format
    let mut fmt = dev.format().expect("Failed to read format");
    fmt.width = 1280;
    fmt.height = 720;
    fmt.fourcc = FourCC::new(b"YUYV");
    let fmt = dev.set_format(&fmt).expect("Failed to write format");

    // The actual format chosen by the device driver may differ from what we
    // requested! Print it out to get an idea of what is actually used now.
    println!("Format in use:\n{}", fmt);

    // Now we'd like to capture some frames!
    // First, we need to create a stream to read buffers from. We choose a
    // mapped buffer stream, which uses mmap to directly access the device
    // frame buffer. No buffers are copied nor allocated, so this is actually
    // a zero-copy operation.

    // To achieve the best possible performance, you may want to use a
    // UserBufferStream instance, but this is not supported on all devices,
    // so we stick to the mapped case for this example.
    // Please refer to the rustdoc docs for a more detailed explanation about
    // buffer transfers.

    // Create the stream, which will internally 'allocate' (as in map) the
    // number of requested buffers for us.
    let mut stream = Stream::with_buffers(&mut dev, Type::VideoCapture, 4)
        .expect("Failed to create buffer stream");

    // At this point, the stream is ready and all buffers are setup.
    // We can now read frames (represented as buffers) by iterating through
    // the stream. Once an error condition occurs, the iterator will return
    // None.
    loop {
        let (buf, meta) = stream.next().unwrap();
        println!(
            "Buffer size: {}, seq: {}, timestamp: {}",
            buf.len(),
            meta.sequence,
            meta.timestamp
        );

        // To process the captured data, you can pass it somewhere else.
        // If you want to modify the data or extend its lifetime, you have to
        // copy it. This is a best-effort tradeoff solution that allows for
        // zero-copy readers while enforcing a full clone of the data for
        // writers.
    }
}

请查看提供的示例以获取更多示例应用。

无运行时依赖

~0–1.8MB
~36K SLoC