Skip to main content

Module file

Module file 

Source
Expand description

Recording NDI sources to disk and playing recordings back, through the NDI File library.

§Supported targets

Enabled by file-io, independently of the advanced Cargo feature. The File ABI is included, so desktop and Android builds need no Advanced headers or libclang. On Linux, macOS and Android, the 6.4.0.2 File library imports the Advanced runtime’s entry points and is refused before loading beside a Standard runtime. Select the runtime through RuntimeOptions::edition. The Windows File library ships for 64-bit Windows and supports either runtime edition. Apple mobile targets link the File archive at build time.

The File library is a separate native library that accompanies the NDI runtime. FileApi::new finds it when it is first needed; nothing else in this crate depends on it, so its absence never affects sending or receiving.

  • FilePlayer::probe reads a recording’s duration and track layout without advertising anything on the network.
  • FilePlayer::new advertises an NDI source and plays recordings through it; see FilePlayer.
  • FileRecorder::new records a source’s compressed essence exactly as it arrives; see FileRecorder. The recorder is source-driven: it subscribes to an NDI source and does not accept frames from Rust.

§Where the File library is loaded from

An explicit FileApiOptions::library_path selects a single file. The default search tries the selected NDI runtime’s directory first, followed by the executable’s directory, the absolute NDI_RUNTIME_DIR_V6 and the platform loader locations, in that order.

The runtime search policy also governs File library paths, platform locations, relative environment entries and dyld behavior. Use trusted libraries and dependencies.

When nothing is usable, Error::RuntimeLoadFailed carries a crate::LoadFailure for crate::LibraryKind::File, with each attempted location, origin and typed refusal. loaded_any() reports whether any native code loaded before being refused. Display retains the diagnostic text for each attempt, including the operating system’s reason. Like the NDI runtime, a loaded File library stays selected and loaded for the life of the process.

A library that loads but cannot be used stays loaded. If the platform loader’s own search then matches the library name to that module, it returns it again instead of looking further; the error names the file, and the remedy is to remove or fix it or to pass FileApiOptions::library_path.

The native borrowed source/path getters are deliberately not called: their strings can be invalidated by the SDK’s recording thread. Use FileRecorder::last_accepted_source for the last accepted source request and FileRecorder::reported_current_file for the owned event view of the file. These can lag or differ from the native state as their docs describe.

§What the File library provides

Its entry points come in FileFamily groups: playback, which also probes recordings, and recording. A library is selected when it provides at least one family completely. FileApi::status reports each family, and an operation that needs a family the selected library lacks fails with Error::UnsupportedCapability, naming the family and the first entry point the library does not export.

§Finishing a recording

FileRecorder::finish requests a stop and waits for closure events. See RecordingReport::closure_confirmed for what the SDK confirms and check the report’s error counters. Drop and async close follow FileRecorderOptions::drop_wait. When file tracking becomes incomplete, follow StateConflict::IncompleteFileTracking before restarting.

§Limits of the NDI 6.4 File library

These limits apply to the versions and platforms named below. They lie inside the library and this crate cannot correct them.

  • On Linux with the NDI 6.4 File library, a file reported closed is not proof that its data was written. A session’s report repeats what the library said: that it finalized each file. When the disk fills while a session is being stopped, a write comes up short, the library extends the file all the same and reports it closed without an error, and FilePlayer::probe then reports a final duration for a file whose data is not there. SessionEnd::Closed with no errors therefore means that the container was finalized, not that every frame reached the disk. A recording that must be complete has to be checked by reading it back.
  • On Linux with the NDI 6.4 File library, the process does not survive a full disk during recording. When a write fails while a session is recording, the library queues a write-failed and an error event and then crashes in its own writer thread, which ends the process. Nothing this crate does can prevent it. Record only where enough space is certain.
  • On Linux with the NDI 6.4 File library, a file can be closed with nothing in it. The library opens a file as soon as a session or a rollover starts, and reports it closed even if no frame arrived: after a stop before the source delivered anything, a switch to a source that is not there, or a rollover while the source is lost. RecordingReport::empty_files counts confirmed empty files: exactly under confirmed closure, and as a lower bound after incomplete tracking.
  • On Linux with the NDI 6.4 File library, a rollover the library cannot carry out ends the session. It accepts the request, fails to open the new file, closes the old one and stops. FileRecorder::rollover preflights the destination so a missing output directory, a directory-only path, an existing exact name or an indexed name that outgrows the filesystem limit can be refused while the session continues. See FileNaming for the naming rules.
  • On every platform with the NDI 6.4 File library, recording paths must be absolute and reportable exactly. Start and rollover preflight the expanded path sent to the SDK. It must fit in 4094 UTF-8 bytes with the current SDK, reserving at least 20 digits per index token. This keeps closure events unambiguous.

§Threads

FilePlayer and FileRecorder are neither Send nor Sync: the SDK documents no threading contract for them, so each stays on the thread that created it. Every native call can block, including destruction; timeouts bound event waits only. The values they return are owned and can be sent anywhere. AsyncFilePlayer and AsyncFileRecorder keep each native object on a dedicated thread and expose futures that work with any executor. Those futures submit work on their first poll; FileOperation::submit explicitly submits immediately. Submitted stop, finish, close and abandon operations survive cancellation of their futures. Dropping the last async handle can block; await its close() operation before dropping it on an executor.

§Examples

use std::time::{Duration, Instant};

use grafton_ndi::{
    file::{
        FileApi, FileApiOptions, FileRecorder, FileRecorderOptions, FinishStatus,
        RecordingTarget, RecordingTimecode, SessionEnd, SourceSpec,
    },
    Ndi,
};

let ndi = Ndi::new()?;
let file = FileApi::new(&ndi, &FileApiOptions::default())?;

let options = FileRecorderOptions::new(SourceSpec::new("STUDIO (Camera 1)")?);
let mut recorder = FileRecorder::new(&file, &options)?;
recorder.start(&RecordingTarget::new("take")?, RecordingTimecode::Preserve)?;
let duration = Duration::from_secs(10);
let start = Instant::now();
while start.elapsed() < duration {
    let wait = duration.saturating_sub(start.elapsed()).min(Duration::from_millis(250));
    if let Some(event) = recorder.next_event(wait)? {
        println!("{:?}", event.kind());
    }
}

match recorder.finish(Duration::from_secs(5))? {
    FinishStatus::Ended(SessionEnd::Closed(report)) => {
        if report.error_events() != 0 || report.write_failures() != 0
            || report.empty_files() != 0 || report.stopped_by_sdk()
        {
            return Err(format!("recording incomplete: {report:?}").into());
        }
        println!("closed {} file(s)", report.closed_files());
    }
    other => return Err(format!("closure unconfirmed: {other:?}").into()),
}

See file_record.rs for the complete recording workflow.

Structs§

AsyncFileOptions
The sizes of the queues between an async handle and its owner thread.
AsyncFilePlayer
A FilePlayer on a thread of its own, driven through futures.
AsyncFileRecorder
A FileRecorder on a thread of its own, driven through futures.
EventText
Original SDK event bytes, retaining invalid UTF-8 and truncation evidence.
FileApi
The File library selected for this process.
FileApiOptions
How the File library is located. Creating this value loads nothing.
FileDuration
A current duration that may still be growing.
FileEventFuture
The next event from a player or recorder, or None when none arrives within the timeout.
FileInfo
A non-advertising file probe result.
FileOperation
The result of a command sent to a player’s or recorder’s thread.
FilePlayer
An NDI source that plays recordings.
FilePlayerOptions
How a player advertises its NDI source. Creating this value advertises nothing.
FilePosition
Unsigned file-relative time in 100ns units, distinct from the editorial Timecode that frames carry.
FileRecorder
A recorder that writes one NDI source to disk.
FileRecorderOptions
Recorder configuration fixed when its native instance is created.
PlayerEvent
An owned player event. Inline SDK strings are copied within their bounds.
PlayerStats
Owned playback counters for the current or most recent file session.
RecorderEvent
An owned recorder event, including original bounded diagnostic text.
RecorderStats
Owned recorder telemetry. Counts refer to packets rather than decoded samples.
RecordingReport
What the recorder’s events have shown about a session so far.
RecordingTarget
An owned absolute output prefix/path interpreted by the recorder’s naming policy.
SessionTimecode
An explicit editorial timecode from which a recording session can advance.
SourceFormat
An owned layout snapshot containing no borrowed native memory.
SourceSpec
An owned source name or direct URL used by a recorder.
VideoTrack
An owned description of one video track, not a decoded pixel layout.

Enums§

FileFamily
A family of the NDI File library’s entry points, which a library provides completely or not at all.
FileNaming
How a recorder names the files of a session from its RecordingTarget.
FinishStatus
The outcome of finishing a recording session.
PlayerEventKind
Playback event kind; unknown native values remain observable.
RecorderEventKind
Recorder event kind; a write failure is not necessarily fatal.
RecorderState
Where a recorder is in its session.
RecordingTimecode
Timecode policy applied when a recording session starts.
SessionEnd
The final outcome of a recording session.
StateConflict
Why an operation is not permitted in the current state, carried by Error::InvalidState.
StopRequest
What FileRecorder::request_stop did.