pub mod sys;
use std::{
fs::{
File,
OpenOptions,
},
io::Read,
os::{
fd::{
AsFd,
BorrowedFd,
},
unix::{
fs::OpenOptionsExt,
io::{
AsRawFd,
RawFd,
},
},
},
};
use self::sys::*;
use crate::error::Result;
#[derive(Debug)]
pub struct DmxDevice {
file: File,
}
impl AsRawFd for DmxDevice {
#[inline]
fn as_raw_fd(&self) -> RawFd {
self.file.as_raw_fd()
}
}
impl AsFd for DmxDevice {
#[inline]
fn as_fd(&self) -> BorrowedFd<'_> {
self.file.as_fd()
}
}
impl Read for DmxDevice {
#[inline]
fn read(&mut self, buf: &mut [u8]) -> std::io::Result<usize> {
(&self.file).read(buf)
}
}
impl DmxDevice {
pub fn open(adapter: u32, device: u32) -> Result<Self> {
let path = format!("/dev/dvb/adapter{}/demux{}", adapter, device);
let file = OpenOptions::new()
.read(true)
.write(true)
.custom_flags(::nix::libc::O_NONBLOCK)
.open(&path)?;
let dmx = DmxDevice { file };
Ok(dmx)
}
pub fn set_pes_filter(&self, filter: &DmxPesFilterParams) -> Result<()> {
nix::ioctl_write_ptr!(
#[inline]
ioctl_call,
b'o',
44,
DmxPesFilterParams
);
unsafe { ioctl_call(self.as_raw_fd(), filter) }?;
Ok(())
}
pub fn set_buffer_size(&self, buffer_size: u64) -> Result<()> {
nix::ioctl_write_int_bad!(
#[inline]
ioctl_call,
nix::request_code_none!(b'o', 45)
);
unsafe { ioctl_call(self.as_raw_fd(), buffer_size as _) }?;
Ok(())
}
pub fn start(&self) -> Result<()> {
nix::ioctl_none!(
#[inline]
ioctl_call,
b'o',
41
);
unsafe { ioctl_call(self.as_raw_fd()) }?;
Ok(())
}
pub fn stop(&self) -> Result<()> {
nix::ioctl_none!(
#[inline]
ioctl_call,
b'o',
42
);
unsafe { ioctl_call(self.as_raw_fd()) }?;
Ok(())
}
}