use std::{error::Error, ffi::CStr};
use crate::bindings;
pub fn version() -> Option<&'static str> {
let version_ptr = unsafe { bindings::NDIlib_version() };
if version_ptr.is_null() {
return None;
}
let version = unsafe { CStr::from_ptr(version_ptr) };
version.to_str().ok()
}
#[cfg(test)]
#[test]
fn test_get_version() {
let version = version();
assert!(version.is_some(), "Failed to get NDI SDK version");
}
pub fn cpu_supported() -> bool {
unsafe { bindings::NDIlib_is_supported_CPU() }
}
#[cfg(test)]
#[test]
fn test_cpu_supported() {
assert!(
cpu_supported(),
"CPU is not supported by NDI SDK, further tests will fail"
);
}
pub fn initialize() -> Result<(), NDIInitError> {
if unsafe { bindings::NDIlib_initialize() } {
Ok(())
} else if !cpu_supported() {
Err(NDIInitError::UnsupportedCPU)
} else {
Err(NDIInitError::GenericError)
}
}
pub fn destroy() {
unsafe { bindings::NDIlib_destroy() }
}
#[non_exhaustive]
#[derive(Debug, Clone)]
pub enum NDIInitError {
UnsupportedCPU,
GenericError,
}
impl std::fmt::Display for NDIInitError {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
match self {
#[cfg(target_arch = "x86_64")]
Self::UnsupportedCPU => f.write_str("Unsupported CPU, NDI requires SSE4.2"),
#[cfg(not(target_arch = "x86_64"))]
Self::UnsupportedCPU => f.write_str("Unsupported CPU"),
Self::GenericError => f.write_str("Generic NDI SDK error"),
}
}
}
impl Error for NDIInitError {}