Skip to main content

FileRecorder

Struct FileRecorder 

Source
pub struct FileRecorder { /* private fields */ }
Expand description

A recorder that writes one NDI source to disk.

The recorder subscribes to the source named in its options and stores the compressed essence as it arrives; it takes no frames from Rust. A session runs from start until its last file is closed, and may span several files through rollover, set_source and the SDK’s own rollovers.

§Finishing

finish requests a stop and waits for closure events. Check RecordingReport::closure_confirmed and the report’s error counters. Drop follows FileRecorderOptions::drop_wait; abandon skips that wait. See StateConflict::IncompleteFileTracking for recovery.

§What a closed file means

A report of closure repeats what the SDK said: that it finalized the file. A closed file is not proof its data was written. Inspect the report and read back recordings whose completeness matters. See the module’s limits.

§Events

Events are taken with next_event, and finish takes them itself while it waits; their effect on the session is kept in the report either way. The SDK discards events that are not taken promptly, and a session that lost events can no longer be confirmed closed, so poll throughout a recording. File bookkeeping retains at most 16,384 entries and 16 MiB, accounting for entry storage and path capacity. Exceeding either private limit or failing to reserve storage makes tracking incomplete without dropping delivered events or changing the native dropped-event count.

A recorder belongs to the thread that created it, so it is neither Send nor Sync: the File API documents no thread on which a recorder may be used or destroyed, nor whether two calls on one recorder may overlap. Methods that call into the SDK may block; cached state and report snapshots do not call it. AsyncFileRecorder offers the same operations as futures.

Implementations§

Source§

impl FileRecorder

Source

pub fn new(api: &FileApi, options: &FileRecorderOptions) -> Result<Self>

Create a recorder for the source named in options.

Nothing is written until FileRecorder::start. The options are only read, so they stay with the caller whatever the outcome; the recorder keeps its own copy of the source they name.

§Errors

Error::InvalidInput when the options are malformed or conflict: an empty track selection, a drop wait beyond crate::MAX_TIMEOUT, a malformed FileNaming::Pattern, or an exact file name (FileNaming::Exact, or a pattern without {index}) with format-change rollover switched on or a nonzero rollover size. Error::InvalidCString when the filename pattern contains a NUL. Error::NativeOperationFailed when the SDK cannot create the recorder and Error::UnsupportedCapability when the library does not provide FileFamily::Recording.

Source

pub fn state(&self) -> RecorderState

Where the recorder is in its session.

The state follows the events taken so far. A source that disconnects leaves a session RecorderState::Recording; recording resumes when it returns.

Source

pub fn report(&self) -> RecordingReport

What the events taken so far have shown about the current session.

Source

pub fn start( &mut self, target: &RecordingTarget, timecode: RecordingTimecode, ) -> Result<u64>

Start a session writing to target, and return its number.

The recorder’s FileNaming turns the target into file names. With the default indexed names the lowest unused index is chosen; with an exact file name a file that already exists is refused. The directory the naming leads to must exist. The check does not reserve the file: a collision created afterwards reaches the SDK and can cause a native open failure. Linux File SDK 6.4.0.2 uses exclusive creation and preserves the colliding file; other platforms and SDK releases have not been verified. Use a recorder-controlled directory for uninterrupted recording and destination containment. Parent directories are not pinned against replacement.

Queued events from an earlier session, including unreadable events, are consumed and discarded before the native start. Poll them before starting if their contents are needed. The asynchronous recorder instead forwards these events through its next_event operation. A restart checks for SDK event loss after draining the queue. A loss visible at this check can require a reset without changing the previous session’s final report. If the restart succeeds, the new session accounts for loss after this check.

The target is only read, so it stays with the caller whatever the outcome, for example to start again once a missing directory exists. RecordingTimecode::GenerateFrom takes a validated SessionTimecode, so the native start always receives an explicit timecode rather than the send synthesize sentinel.

§Errors
  • Error::InvalidState with StateConflict::PreviousSessionOpen while an earlier session is recording, stopping, awaiting a known file’s closure under complete tracking; finish or poll it first. StateConflict::IncompleteFileTracking after a session ends with incomplete tracking. StateConflict::EarlierSessionEvents when the SDK keeps reporting events of an earlier session.
  • Error::InvalidInput when the expanded output path is relative, has no file name, or its longest form exceeds the recorder event’s exact path limit (4094 UTF-8 bytes with the current SDK), reserving at least 20 digits per index token. The SDK is not called.
  • Error::Io when the directory of the file the pattern names does not exist or cannot be written to, the filesystem rejects the candidate path, or an exact file name exists (kind AlreadyExists). The SDK is not called. On Windows, also reports security queries that could not be completed.
  • Error::NativeOperationFailed when the SDK cannot start the source or open the first file. Processing.NDI.Lib.File.Recorder.h specifies an open-failed event for an output-file open failure; source-start failure need not queue one. next_event can return that event, counted in no session’s report. Retrying start discards queued events before asking the SDK to start again.
Source

pub fn request_stop(&mut self) -> StopRequest

Ask the SDK to stop the session. Idempotent.

The SDK then finalizes the open file and reports it closed; this call alone does not show that it has. Use finish or wait_for_finish to find out.

Source

pub fn next_event(&mut self, timeout: Duration) -> Result<Option<RecorderEvent>>

Take the next queued event, waiting up to timeout for one, or None when none arrives in time.

The event’s effect on the session is recorded before it is returned, including when the event itself is malformed.

§Errors

Error::InvalidInput when timeout exceeds crate::MAX_TIMEOUT, and Error::UnexpectedSdkResult when the SDK’s event does not match this crate’s structure layout or its returned event type. An unreadable format is withheld through RecorderEvent::source_format while the rest of the event is delivered.

Source

pub fn finish(&mut self, timeout: Duration) -> Result<FinishStatus>

Stop the session and wait up to timeout for its last file to be closed.

Events that arrive during the wait are consumed; their effect is in the returned report. A malformed event is consumed too: what it showed about the session is recorded as next_event records it, and the wait goes on. One that could not be read at all makes the outcome SessionEnd::Indeterminate rather than an error.

The wait starts once the stop has returned, and covers taking the closure’s event, not the stop itself, which the SDK may carry out before it returns. Events already queued are taken even when the wait is over, so finish(Duration::ZERO) reports SessionEnd::Closed when the SDK finalized the file during the stop.

§Errors

Error::InvalidInput when timeout exceeds crate::MAX_TIMEOUT, without requesting the stop, and Error::InvalidState with StateConflict::StopNotRequested when no session was started. Neither is returned once the wait has begun: whatever the SDK’s events hold, the outcome is a FinishStatus.

Source

pub fn wait_for_finish( &mut self, timeout: Duration, ) -> Result<Option<SessionEnd>>

Wait up to timeout for a stopping session to end.

Returns None when the timeout expires, and Some with SessionEnd::Closed or SessionEnd::Indeterminate when the session ends. A timeout retains the session’s report in report, and another wait can continue observing it.

Use this to keep waiting after finish returns FinishStatus::TimedOut. Events already queued are taken even when timeout is over, so a closure that has been reported is never missed. Malformed events are taken and recorded as finish describes, without ending the wait.

§Errors

Error::InvalidInput when timeout exceeds crate::MAX_TIMEOUT, and Error::InvalidState with StateConflict::StopNotRequested when no session was started or the session has not been asked to stop. Neither is returned once the wait has begun.

Source

pub fn abandon(self) -> RecordingReport

Stop and destroy the recorder at once, without waiting for the SDK to finalize an open file, and return what was known about the session.

The file of a session that had not ended may be left truncated.

Source

pub fn reset( &mut self, options: &FileRecorderOptions, ) -> Result<RecordingReport>

Abandon this native recorder and replace it with a new idle recorder.

Use this after a finalization timeout when the SDK stopped producing events, or to recover from StateConflict::IncompleteFileTracking. The returned report preserves what was known about the old session. An unfinished file can be left truncated. The replacement has its own event queue, so delayed events from the old session cannot affect a new recording. The last report is retained and session numbers continue increasing across resets.

The replacement is created first. If creation fails, this recorder and its report are kept and can still be polled or finalized. The options are only read, as by FileRecorder::new.

§Errors

The errors of FileRecorder::new.

Source

pub fn is_recording(&self) -> bool

Whether the SDK is recording or waiting for frames. False once a stop has taken effect; this alone does not show that a file was finalized.

Source

pub fn dropped_event_count(&self) -> u64

How many events the SDK has discarded since the recorder was created because they were not taken in time.

Source

pub fn set_source(&mut self, source: &SourceSpec) -> Result<()>

Switch to recording another source.

During a session the SDK starts the new source at once and rolls over to a new file, named from the session’s current target. Before a session, the source is used by the next start.

A recorder with an exact file name (FileNaming::Exact, or a FileNaming::Pattern without {index}) cannot roll over to a file of the same name, and the SDK would end the session instead, so it refuses a change of source during a session and the session goes on. Before a session the change works as for any recorder.

The source stays with the caller; the recorder keeps a copy of it when the SDK accepts it, for last_accepted_source.

§Errors
Source

pub fn last_accepted_source(&self) -> Option<&SourceSpec>

The source the SDK accepted last: the one given at creation, or by the last successful set_source.

None after a rejected change, because the SDK does not say which source is then in effect. This is what was requested, not a statement that the source is connected.

Source

pub fn reported_current_file(&self) -> Option<&EventText>

The most recently opened file still retained in the file tracker.

The path is as the SDK gave it, and can lag events not yet taken. Once tracking is incomplete, later opens are not retained; consume next_event for their paths. None does not prove that no files remain open.

Source

pub fn rollover(&mut self, target: &RecordingTarget) -> Result<()>

Close the current file and continue the session in a new one named from target.

For compressed video the SDK makes the cut at the next keyframe, so the new file starts decodable. A rollover that has not happened by the time the session stops is dropped.

The SDK accepts a rollover it then cannot carry out, and ends the session when the new file cannot be opened: it reports open-failed, closes the old file and reports an error. The file the pattern names for the target is therefore checked first. Its directory must exist and be writable, the filesystem must accept the candidate path, and an exact file name must be available. The usual mistakes then leave the session running, and the target, which is only read, stays with the caller to correct and try again. The preflight is not a reservation: a path created after it can make the later native open fail and end the session even after this method returns Ok(()). Observe open-failed/error events and the final RecordingReport. The exclusive-creation behavior tested on Linux File SDK 6.4.0.2 preserves colliding regular files and symlinks; other platforms and SDK releases have not been verified. A recorder-controlled output directory avoids this race with other writers.

§Errors
  • Error::InvalidState with StateConflict::NotRecording unless a session is recording.
  • Error::InvalidInput when the expanded output path is relative, has no file name, or its longest form exceeds the recorder event’s exact path limit (4094 UTF-8 bytes with the current SDK), reserving at least 20 digits per index token. The SDK is not called and the session goes on.
  • Error::Io when the directory of the file the pattern names does not exist or cannot be written to, the filesystem rejects the candidate path, or an exact file name exists (kind AlreadyExists). The SDK is not called and the session goes on. On Windows, also reports security queries that could not be completed.
  • Error::NativeOperationFailed when the SDK refuses the target.
Source

pub fn inject_metadata<S: MetadataSource + ?Sized>( &mut self, metadata: &S, ) -> Result<()>

Write a metadata frame into the recording at its current position.

The frame’s timecode is stored and re-published on playback; it does not move the frame on the file’s timeline. The frame’s text is handed to the SDK as it is, without a wrapper copy. Owned and captured metadata are accepted, and need only outlive this synchronous call.

§Errors

Error::InvalidState with StateConflict::NotRecording unless a session is recording, and Error::NativeOperationFailed when the SDK refuses it, for example because the metadata track is not being recorded.

Source

pub fn stats(&self) -> Result<Option<RecorderStats>>

The recorder’s running counters, when the SDK has any. Interpret these with the recorder session state and events; they do not use receiver drop-percentage semantics.

§Errors

Error::UnexpectedSdkResult when the SDK’s structure does not match this crate’s layout.

Source

pub fn source_format(&self) -> Result<Option<SourceFormat>>

The format of the source, once the recorder has seen audio or video.

§Errors

Error::UnexpectedSdkResult when the SDK’s structure is malformed.

Trait Implementations§

Source§

impl Debug for FileRecorder

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl Drop for FileRecorder

Source§

fn drop(&mut self)

Executes the destructor for this type. Read more
Source§

fn pin_drop(self: Pin<&mut Self>)

🔬This is a nightly-only experimental API. (pin_ergonomics)
Execute the destructor for this type, but different to Drop::drop, it requires self to be pinned. Read more

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = !

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, !>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.