videostream_sys/
lib.rs

1// SPDX-License-Identifier: Apache-2.0
2// Copyright 2025 Au-Zone Technologies
3
4#![allow(non_upper_case_globals)]
5#![allow(non_camel_case_types)]
6#![allow(non_snake_case)]
7#![allow(clippy::type_complexity)]
8#![allow(clippy::missing_safety_doc)]
9#![allow(clippy::too_many_arguments)]
10
11include!("ffi.rs");
12
13// Re-export libloading for error handling
14pub use libloading;
15
16use std::sync::{Mutex, OnceLock};
17
18static LIBRARY: OnceLock<VideoStreamLibrary> = OnceLock::new();
19static INIT_LOCK: Mutex<()> = Mutex::new(());
20
21/// Initialize the VideoStream library by loading libvideostream.so
22///
23/// This must be called before using any other VideoStream functions.
24/// Returns an error if the library cannot be loaded.
25pub fn init() -> Result<&'static VideoStreamLibrary, libloading::Error> {
26    if let Some(lib) = LIBRARY.get() {
27        return Ok(lib);
28    }
29
30    let _guard = INIT_LOCK.lock().unwrap();
31
32    // Double-check after acquiring lock
33    if let Some(lib) = LIBRARY.get() {
34        return Ok(lib);
35    }
36
37    let lib = unsafe { VideoStreamLibrary::new("libvideostream.so")? };
38
39    LIBRARY.set(lib).ok().expect("Failed to initialize library");
40
41    Ok(LIBRARY.get().unwrap())
42}
43
44/// Get a reference to the loaded library
45///
46/// Panics if init() has not been called successfully.
47pub fn library() -> &'static VideoStreamLibrary {
48    LIBRARY
49        .get()
50        .expect("VideoStream library not initialized - call videostream_sys::init() first")
51}
52
53/// Try to get a reference to the loaded library without panicking
54pub fn try_library() -> Option<&'static VideoStreamLibrary> {
55    LIBRARY.get()
56}