summaryrefslogtreecommitdiff
path: root/src/v4l2.rs
diff options
context:
space:
mode:
authordyknon dyknonr5fjp2025-02-24 18:58:32 +0900
committerdyknon dyknonr5fjp2025-02-24 18:58:32 +0900
commit6ecbf0d55695335f52d6fcf2b6a22ed45f5e4d99 (patch)
treee17bfc8a9b55d5afbfe2f884322a96a54340f25d /src/v4l2.rs
parent31b60ae28e6aff6d23b378cd77e288c96c7db148 (diff)
Remote camera capability.
Diffstat (limited to 'src/v4l2.rs')
-rw-r--r--src/v4l2.rs45
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)]