Skip to content

Commit

Permalink
ioctl: add support for G_ENC_IDX
Browse files Browse the repository at this point in the history
  • Loading branch information
Gnurou committed Nov 15, 2023
1 parent 1b5bbe3 commit 6832e45
Showing 1 changed file with 35 additions and 7 deletions.
42 changes: 35 additions & 7 deletions lib/src/ioctl/encoder_cmd.rs
Original file line number Diff line number Diff line change
@@ -1,20 +1,48 @@
use crate::bindings;
use crate::bindings::v4l2_encoder_cmd;
use std::convert::{Infallible, TryFrom};
use std::mem;
use std::os::unix::io::AsRawFd;

use nix::errno::Errno;
use std::{
convert::{Infallible, TryFrom},
mem,
os::unix::io::AsRawFd,
};
use thiserror::Error;

use crate::bindings;
use crate::bindings::v4l2_enc_idx;
use crate::bindings::v4l2_encoder_cmd;

#[doc(hidden)]
mod ioctl {
use crate::bindings::v4l2_enc_idx;
use crate::bindings::v4l2_encoder_cmd;

nix::ioctl_read!(vidioc_g_enc_index, b'V', 76, v4l2_enc_idx);
nix::ioctl_readwrite!(vidioc_encoder_cmd, b'V', 77, v4l2_encoder_cmd);
nix::ioctl_readwrite!(vidioc_try_encoder_cmd, b'V', 78, v4l2_encoder_cmd);
}

#[derive(Debug, Error)]
pub enum GEncIndexError {
#[error("ioctl error: {0}")]
IoctlError(Errno),
}

impl From<GEncIndexError> for Errno {
fn from(err: GEncIndexError) -> Self {
match err {
GEncIndexError::IoctlError(e) => e,
}
}
}

/// Safe wrapper around the `VIDIOC_G_ENC_INDEX` ioctl.
pub fn g_enc_index<O: From<v4l2_enc_idx>>(fd: &impl AsRawFd) -> Result<O, GEncIndexError> {
let mut enc_idx: v4l2_enc_idx = unsafe { std::mem::zeroed() };

match unsafe { ioctl::vidioc_g_enc_index(fd.as_raw_fd(), &mut enc_idx) } {
Ok(_) => Ok(O::from(enc_idx)),
Err(e) => Err(GEncIndexError::IoctlError(e)),
}
}

#[derive(Debug, Clone, Copy)]
pub enum EncoderCommand {
Start,
Expand Down

0 comments on commit 6832e45

Please sign in to comment.