linux_video_core/impls/
streamparm.rs

1use crate::{calls, types::*, Internal, Result};
2use std::os::unix::io::RawFd;
3
4impl Internal<&mut StreamParm> {
5    pub fn get(&mut self, fd: RawFd) -> Result<()> {
6        unsafe_call!(calls::g_parm(fd, *self.as_mut() as *mut _)).map(|_| ())
7    }
8}
9
10impl Internal<&mut StreamParm> {
11    pub fn set(&self, fd: RawFd) -> Result<()> {
12        unsafe_call!(calls::s_parm(fd, *self.as_ref() as *const _ as *mut _)).map(|_| ())
13    }
14}
15
16unsafe impl Send for StreamParm {}
17
18impl From<BufferType> for StreamParm {
19    fn from(type_: BufferType) -> Self {
20        Self {
21            type_,
22            parm: if type_.is_capture() {
23                StreamParmUnion {
24                    capture: CaptureParm::default(),
25                }
26            } else {
27                StreamParmUnion {
28                    output: OutputParm::default(),
29                }
30            },
31        }
32    }
33}
34
35impl StreamParm {
36    /// Create from value
37    pub fn new<T: IsStreamParm>(type_: BufferType, data: T) -> Option<Self> {
38        if T::OUTPUT == type_.is_output() {
39            let mut this = Self::from(type_);
40            *this.try_mut().unwrap() = data;
41            Some(this)
42        } else {
43            None
44        }
45    }
46
47    /// Get reference to value
48    pub fn try_ref<T: IsStreamParm>(&self) -> Option<&T> {
49        if T::OUTPUT == self.type_.is_output() {
50            Some(unsafe { &*(&self.parm as *const _ as *const T) })
51        } else {
52            None
53        }
54    }
55
56    /// Get mutable reference to value
57    pub fn try_mut<T: IsStreamParm>(&mut self) -> Option<&mut T> {
58        if T::OUTPUT == self.type_.is_output() {
59            Some(unsafe { &mut *(&mut self.parm as *mut _ as *mut T) })
60        } else {
61            None
62        }
63    }
64}
65
66/// Stream parameter types
67pub trait IsStreamParm {
68    /// Stream direction
69    const OUTPUT: bool;
70}
71
72impl IsStreamParm for CaptureParm {
73    const OUTPUT: bool = false;
74}
75
76impl IsStreamParm for OutputParm {
77    const OUTPUT: bool = true;
78}
79
80impl core::fmt::Display for StreamParm {
81    fn fmt(&self, f: &mut core::fmt::Formatter) -> core::fmt::Result {
82        self.type_.fmt(f)?;
83        ": ".fmt(f)?;
84        self.try_ref::<CaptureParm>()
85            .map(|parm| parm.fmt(f))
86            .or_else(|| self.try_ref::<OutputParm>().map(|parm| parm.fmt(f)))
87            .unwrap()
88    }
89}
90
91impl core::fmt::Display for CaptureParm {
92    fn fmt(&self, f: &mut core::fmt::Formatter) -> core::fmt::Result {
93        self.time_per_frame.fmt(f)?;
94        " #".fmt(f)?;
95        self.read_buffers.fmt(f)?;
96        ' '.fmt(f)?;
97        self.capability.fmt(f)?;
98        ','.fmt(f)?;
99        self.capture_mode.fmt(f)?;
100        " *".fmt(f)?;
101        self.extended_mode.fmt(f)
102    }
103}
104
105impl core::fmt::Display for OutputParm {
106    fn fmt(&self, f: &mut core::fmt::Formatter) -> core::fmt::Result {
107        self.time_per_frame.fmt(f)?;
108        " #".fmt(f)?;
109        self.write_buffers.fmt(f)?;
110        ' '.fmt(f)?;
111        self.capability.fmt(f)?;
112        ','.fmt(f)?;
113        self.output_mode.fmt(f)?;
114        " *".fmt(f)?;
115        self.extended_mode.fmt(f)
116    }
117}