Skip to main content

dynlink_nvidia_encode/
load.rs

1// Copyright (C) The Strand-Braid Authors
2// SPDX-License-Identifier: MIT OR Apache-2.0
3
4use std::path::{Path, PathBuf};
5
6use crate::NvencError;
7
8// The dynamic loading aspects here were inspired by clang-sys.
9
10// Due to the thread local stuff here, it is somewhat complex to abstract this
11// into a standalone library.
12
13pub struct SharedLibrary {
14    pub(crate) library: libloading::Library,
15    path: PathBuf,
16}
17
18impl SharedLibrary {
19    fn new(library: libloading::Library, path: PathBuf) -> Self {
20        Self { library, path }
21    }
22
23    pub fn path(&self) -> &Path {
24        &self.path
25    }
26}
27
28pub fn load_manually() -> Result<SharedLibrary, NvencError> {
29    #[cfg(target_os = "windows")]
30    let path = PathBuf::from("nvEncodeAPI64.dll");
31    #[cfg(not(target_os = "windows"))]
32    let path = PathBuf::from("libnvidia-encode.so.1");
33    let library = unsafe { libloading::Library::new(&path) }.map_err(|source| {
34        NvencError::DynLibLoadError {
35            dynlib: path.display().to_string(),
36            source,
37        }
38    })?;
39
40    let library = SharedLibrary::new(library, path);
41
42    Ok(library)
43}
44
45pub fn load() -> Result<SharedLibrary, NvencError> {
46    let library = load_manually()?;
47    Ok(library)
48}
49
50#[cfg(test)]
51mod tests {
52    use crate::load::*;
53    #[ignore = "requires nv encode shared library to be present at runtime"]
54    #[test]
55    fn test_load_unload() {
56        load().expect("load");
57    }
58}