3个版本 (破坏性更新)
0.3.0 | 2023年5月7日 |
---|---|
0.2.0 | 2021年1月7日 |
0.1.0 | 2020年5月3日 |
#239 in 视频
9,315 每月下载量
在 4 个包中使用了(直接使用2个)
2KB
安全视频4Linux (v4l) 绑定
此crate提供了对Video for Linux (V4L)堆栈的安全绑定。现代设备驱动程序通常会实现v4l2
API,而较旧的驱动程序可能依赖于传统的v4l
API。可以通过选择此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功能的libv4l
或v4l2
后端来启用。
使用方法
以下提供了此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