telemetry_parser/
lib.rs

1// SPDX-License-Identifier: MIT OR Apache-2.0
2// Copyright © 2021-2022 Adrian <adrian.eddy at gmail>
3
4mod sony;
5mod gopro;
6mod gyroflow;
7mod insta360;
8mod blackbox;
9mod runcam;
10mod witmotion;
11mod dji;
12mod phone_apps;
13mod ardupilot;
14mod blackmagic;
15mod red;
16
17pub mod tags_impl;
18pub mod util;
19
20use std::io::*;
21use std::sync::{ Arc, atomic::AtomicBool };
22use util::*;
23
24macro_rules! impl_formats {
25    ($($name:ident => $class:ty,)*) => {
26        pub enum SupportedFormats {
27            $($name($class),)*
28        }
29        pub struct Input {
30            inner: SupportedFormats,
31            pub samples: Option<Vec<SampleInfo>>
32        }
33        impl Input {
34            pub fn from_stream<T: Read + Seek, P: AsRef<std::path::Path>, F: Fn(f64)>(stream: &mut T, size: usize, filepath: P, progress_cb: F, cancel_flag: Arc<AtomicBool>) -> Result<Input> {
35                let read_mb = if size as u64 > 30u64*1024*1024*1024 { // If file is greater than 30 GB, read 30 MB header/footer
36                    30
37                } else if size as u64 > 5u64*1024*1024*1024 { // If file is greater than 5 GB, read 10 MB header/footer
38                    10
39                } else {
40                    2
41                };
42                let buf = util::read_beginning_and_end(stream, size, read_mb*1024*1024)?;
43                if buf.is_empty() {
44                    return Err(Error::new(ErrorKind::Other, "File is empty or there was an error trying to load it."));
45                }
46                $(
47                    if let Some(mut x) = <$class>::detect(&buf, &filepath) {
48                        return Ok(Input {
49                            samples: x.parse(stream, size, progress_cb, cancel_flag).ok(),
50                            inner: SupportedFormats::$name(x)
51                        });
52                    }
53                )*
54                return Err(Error::new(ErrorKind::Other, "Unsupported file format"));
55            }
56            pub fn camera_type(&self) -> String {
57                match &self.inner {
58                    $(SupportedFormats::$name(x) => x.camera_type(),)*
59                }
60            }
61            pub fn camera_model(&self) -> Option<&String> {
62                match &self.inner {
63                    $(SupportedFormats::$name(x) => x.model.as_ref(),)*
64                }
65            }
66            pub fn normalize_imu_orientation(&self, v: String) -> String {
67                match &self.inner {
68                    $(SupportedFormats::$name(_) => <$class>::normalize_imu_orientation(v),)*
69                }
70            }
71            pub fn frame_readout_time(&self) -> Option<f64> {
72                match &self.inner {
73                    $(SupportedFormats::$name(x) => x.frame_readout_time(),)*
74                }
75            }
76        }
77    };
78}
79
80impl_formats! {
81    GoPro     => gopro::GoPro,
82    Sony      => sony::Sony,
83    Gyroflow  => gyroflow::Gyroflow,
84    Insta360  => insta360::Insta360,
85    BlackBox  => blackbox::BlackBox,
86    Runcam    => runcam::Runcam,
87    WitMotion => witmotion::WitMotion,
88    Dji       => dji::Dji,
89    PhoneApps => phone_apps::PhoneApps,
90    ArduPilot => ardupilot::ArduPilot,
91    BlackmagicBraw => blackmagic::BlackmagicBraw,
92    RedR3d    => red::RedR3d,
93}