diff options
| author | dyknon dyknonr5fjp | 2025-02-24 18:58:32 +0900 |
|---|---|---|
| committer | dyknon dyknonr5fjp | 2025-02-24 18:58:32 +0900 |
| commit | 6ecbf0d55695335f52d6fcf2b6a22ed45f5e4d99 (patch) | |
| tree | e17bfc8a9b55d5afbfe2f884322a96a54340f25d /src/v4l2.rs | |
| parent | 31b60ae28e6aff6d23b378cd77e288c96c7db148 (diff) | |
Remote camera capability.
Diffstat (limited to 'src/v4l2.rs')
| -rw-r--r-- | src/v4l2.rs | 45 |
1 files changed, 44 insertions, 1 deletions
diff --git a/src/v4l2.rs b/src/v4l2.rs index 0f22aba..a8aff0c 100644 --- a/src/v4l2.rs +++ b/src/v4l2.rs @@ -9,11 +9,13 @@ use std::io::Error as IoError; use std::fmt::{Display, Debug, Formatter, Error as FmtError}; use std::error::Error as ErrorTrait; use std::{str, iter, array}; +use std::time::Duration; +use chrono::{DateTime, Local}; macro_rules! define_flagset{ ($tname:ident: $ctype:ty; $($name:ident = $mask:expr),+) => { #[derive(Copy, Clone, Debug, PartialEq, Eq)] - pub struct $tname{ $($name: bool),+ } + pub struct $tname{ $(pub $name: bool),+ } impl $tname{ pub fn zero() -> Self{ Self{ $($name: false),+ } @@ -166,6 +168,47 @@ pub struct BufAttrs{ pub timestamp: c::timeval, pub sequence: u32, } +impl BufAttrs{ + // XXX: It is correct only when V4L2_BUF_FLAG_TIMESTAMP_MONOTONIC + pub fn get_datetime(&self) -> Option<DateTime<Local>>{ + let now_monotonic = unsafe{ + let mut buf = MaybeUninit::uninit(); + assert!(c::clock_gettime(c::CLOCK_MONOTONIC, buf.as_mut_ptr()) + == 0); + buf.assume_init() + }; + let now_wallclock = Local::now(); + let mut timestamp = c::timespec{ + tv_sec: self.timestamp.tv_sec as _, + tv_nsec: self.timestamp.tv_usec as _, + }; + timestamp.tv_nsec *= 1000; + let (mut so, mut nso, future); + if now_monotonic.tv_sec < timestamp.tv_sec + || now_monotonic.tv_sec == timestamp.tv_sec + && now_monotonic.tv_nsec < timestamp.tv_nsec + { + future = true; + so = timestamp.tv_sec - now_monotonic.tv_sec; + nso = timestamp.tv_nsec - now_monotonic.tv_nsec; + }else{ + future = false; + so = now_monotonic.tv_sec - timestamp.tv_sec; + nso = now_monotonic.tv_nsec - timestamp.tv_nsec; + } + if nso < 0{ + nso += 1000_000_000; + so -= 1; + } + assert!(0 <= nso && nso < 1000_000_000); + let to = Duration::new(so as u64, nso as u32); + if future{ + Some(now_wallclock + to) + }else{ + Some(now_wallclock - to) + } + } +} impl Debug for BufAttrs{ fn fmt(&self, f: &mut Formatter<'_>) -> Result<(), FmtError>{ #[derive(Debug)] |
