Skip to main content

Module runtime

Module runtime 

Source
Expand description

Select an NDI implementation and manage its initialization lifetime.

Using NDI takes two steps, and each is explicit:

  1. Select. Runtime::select loads the NDI runtime library and pins it for the rest of the process. Nothing is started. The Runtime it returns describes the library: where it was loaded from, its edition and version (Runtime::info), what it can do (Runtime::capabilities) and whether it can run on this CPU (Runtime::is_supported_cpu).
  2. Initialize. Runtime::initialize starts NDI and returns an Ndi handle, from which every finder, sender and receiver is created. NDI stays started while any handle exists, and stops when the last one is dropped unless Ndi::process_lifetime was chosen.

Ndi::new performs both steps with the default options, and Ndi::with_options with the options it is given. Nothing else in the crate selects or starts a runtime: every operation that needs NDI takes a handle, and a question such as Ndi::is_running only reports the current state.

use grafton_ndi::{Runtime, RuntimeOptions};

let runtime = Runtime::select(&RuntimeOptions::default())?;
println!("NDI {}", runtime.info().raw_version());
if !runtime.is_supported_cpu() {
    eprintln!("this CPU cannot run the selected NDI runtime");
}
let ndi = runtime.initialize()?;

Default builds on dynamically loaded targets need no NDI SDK. A selected shared library stays loaded for the life of the process, including after the last Ndi handle is dropped. Statically linked targets use the SDK archive linked at build time, and selecting it loads nothing.

§Where the runtime is loaded from

Deployment trust boundary: Ndi::new uses a packaging-oriented search. Every searched directory must be controlled by a trusted owner. Loading a native library executes its constructors before its symbols or edition can be checked, including libraries later rejected during fallback. For services and elevated processes, make the very first selection use an absolute path in an application-owner-controlled directory:

use grafton_ndi::{Runtime, RuntimeOptions};
let runtime = Runtime::select(&RuntimeOptions::new()
    .library_path("/opt/my-service/ndi/libndi.so.6"))?;
println!("Selected runtime: {:?}", runtime.info().library_path());
let ndi = runtime.initialize()?;

An explicit path must be absolute and name an existing file. A relative path returns Error::InvalidInput. An absent file returns an absent-path refusal before calling the platform loader. The crate makes no executable, environment or platform search for an explicit path. A later explicit selection cannot undo code already run by an earlier load. Native dependencies still use platform loader rules and must also come from trusted locations. With the File API, explicitly select FileApiOptions::library_path too, or establish that its companion and other search directories are trusted; inspect the selection through FileApi::library_path.

Unless RuntimeOptions::library_path names an exact file, the first usable library in this order is selected:

  1. the directory containing the running executable;
  2. the absolute directory named by NDI_RUNTIME_DIR_V6; relative values are recorded as refused locations and never searched;
  3. platform-specific locations: on Windows, absolute PATH directories followed by a System32-only name lookup; on macOS, @rpath/<name> and absolute DYLD_LIBRARY_PATH directories; on Linux and Android, the native loader search (rpath, LD_LIBRARY_PATH and system locations). Explicitly configured relative or empty LD_LIBRARY_PATH entries can still name the working directory on Linux and Android.

Windows and macOS never add the working directory to this search. Relative or empty PATH/DYLD_LIBRARY_PATH entries are recorded as refused locations. On macOS, a relative or empty entry in either DYLD_LIBRARY_PATH or DYLD_FALLBACK_LIBRARY_PATH refuses the entire default search before opening a library. The check reads the process environment at search time; dyld uses its launch-time values, so changing DYLD_* after launch is unsupported. On unrestricted macOS processes dyld can use DYLD_LIBRARY_PATH to substitute an existing explicit file. Such substitutions are refused with crate::LoadRefusal::ResolvedToAnotherFile after loading the candidate, when its initializers have already run. Configure trusted absolute DYLD_* entries before launching.

Each location is tried for the file names of both SDK editions, for example libndi.so.6 and libndi_advanced.so.6 on Linux, unless RuntimeOptions::edition asks for one. A build with the advanced feature, or with file-io on a platform where the File library needs the Advanced runtime, tries the Advanced name first at each location, and other builds the Standard name; where a library is installed matters more than which edition it is. Nothing installed on the build machine changes the search.

A file that is not there is skipped. A library that cannot be loaded, or that loads but is rejected because it lacks a required entry point or is not the requested edition, is recorded and the search moves on. When nothing is usable, Error::RuntimeLoadFailed carries crate::LoadFailure with every location, crate::LoadOrigin and typed crate::LoadRefusal. Its loaded_any() distinguishes code that loaded and was refused from locations that could not be opened; Display retains the diagnostic text, including the operating system’s own. Error::RuntimeConflict carries a crate::SelectionFailure with a typed kind and selected, requested or foreign module paths, alongside its diagnostic text.

On dynamically loaded, non-Apple ELF targets, a runtime already linked or preloaded into the process’s global symbol scope must also be the selected runtime. Other candidates are refused before opening them, because that global runtime would capture their native calls. Search failures record crate::LoadRefusal::ForeignRuntimeInScope; an explicit different file returns crate::SelectionConflict::ForeignRuntimeInScope. Select the module named in the diagnostic, or stop linking or preloading it.

One limit applies to a library that loads but is rejected: it stays loaded, because a native library can never be unloaded safely. If the platform loader’s own search then matches the library name to that module, it returns the module again instead of looking further, so no other library of that name can be reached by name in this process. The error names the rejected file. Remove or fix that file, or name the wanted library with RuntimeOptions::library_path.

Structs§

Ndi
A handle to the started NDI runtime.
ProcessLifetimeRuntime
An explicit promise that the selected NDI runtime stays initialized until exit.
Runtime
The NDI implementation selected for this process.
RuntimeOptions
Which NDI runtime to select; see Runtime::select.