ndi-sdk-sys 0.1.1

Rust bindings for the NDI SDK
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
//! NDI Receiver
//!
//! <https://docs.ndi.video/all/developing-with-ndi/sdk/ndi-recv>

use std::{
    error::Error,
    ffi::{CStr, CString},
    fmt::Debug,
    ptr::NonNull,
    sync::Arc,
    time::Duration,
};

use static_assertions::assert_impl_all;

use crate::{
    bindings::{self},
    enums::{NDIBandwidthMode, NDIPreferredColorFormat, NDIRecvError},
    frame::{
        audio::AudioFrame,
        generic::{AsFFIReadable, AsFFIWritable},
        metadata::MetadataFrame,
        video::VideoFrame,
    },
    source::NDISourceLike,
    tally::Tally,
    util::duration_to_ms,
};

pub use crate::enums::NDIRecvType;

/// Builder for [NDIReceiver]
#[non_exhaustive]
#[derive(Debug, Clone)]
pub struct NDIReceiverBuilder<Source: NDISourceLike> {
    pub source: Option<Source>,
    pub name: Option<CString>,
    pub color_format: NDIPreferredColorFormat,
    pub bandwidth: NDIBandwidthMode,
    pub allow_fielded_video: bool,
}

impl<Source: NDISourceLike> Default for NDIReceiverBuilder<Source> {
    fn default() -> Self {
        Self {
            source: None,
            name: None,
            color_format: NDIPreferredColorFormat::default(),
            bandwidth: NDIBandwidthMode::default(),
            allow_fielded_video: false,
        }
    }
}

impl<Source: NDISourceLike> NDIReceiverBuilder<Source> {
    pub fn new() -> Self {
        Self::default()
    }

    /// Sets the source to connect to. Can be left out to select the source later.
    pub fn source(mut self, source: Source) -> Self {
        self.source = Some(source);
        self
    }

    /// Sets the receiver name. This will be used in future versions of NDI.
    pub fn name(mut self, name: &str) -> Self {
        self.name = Some(CString::new(name).unwrap());
        self
    }

    /// Sets the preferred color format for the receiver.
    /// Unless you need a specific color format, always use [NDIPreferredColorFormat::Fastest]
    pub fn color_format(mut self, color_format: NDIPreferredColorFormat) -> Self {
        self.color_format = color_format;
        self
    }

    /// Sets the bandwidth mode for the receiver.
    /// Can be used to use reduced bandwidth for multi-views or metadata-only
    pub fn bandwidth(mut self, bandwidth: NDIBandwidthMode) -> Self {
        self.bandwidth = bandwidth;
        self
    }

    /// Sets whether the receiver should allow fielded video.
    /// Default is false, only progressive video will be received.
    pub fn allow_fielded_video(mut self, allow: bool) -> Self {
        self.allow_fielded_video = allow;
        self
    }

    pub fn build(self) -> Result<NDIReceiver, NDIReceiverBuilderError> {
        self.source.with_descriptor(|src_ptr| {
            let options = bindings::NDIlib_recv_create_v3_t {
                p_ndi_recv_name: self
                    .name
                    .as_ref()
                    .map(|s| s.as_ptr())
                    .unwrap_or(std::ptr::null()),

                source_to_connect_to: if src_ptr.is_null() {
                    bindings::NDIlib_source_t {
                        p_ndi_name: std::ptr::null(),
                        __bindgen_anon_1: bindings::NDIlib_source_t__bindgen_ty_1 {
                            p_url_address: std::ptr::null(),
                        },
                    }
                } else {
                    unsafe { *src_ptr }
                },
                color_format: self.color_format.to_ffi(),
                bandwidth: self.bandwidth.to_ffi(),
                allow_video_fields: self.allow_fielded_video,
            };

            let handle = unsafe { bindings::NDIlib_recv_create_v3(&options) };

            if let Some(handle) = NonNull::new(handle) {
                Ok(NDIReceiver {
                    handle: Arc::new(RawReceiver { handle }),
                })
            } else {
                Err(NDIReceiverBuilderError::CreationFailed)
            }
        })
    }
}

#[non_exhaustive]
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum NDIReceiverBuilderError {
    CreationFailed,
}

impl std::fmt::Display for NDIReceiverBuilderError {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        match self {
            Self::CreationFailed => f.write_str("Creating NDI receiver failed"),
        }
    }
}

impl Error for NDIReceiverBuilderError {}

#[derive(PartialEq, Eq)]
pub(crate) struct RawReceiver {
    handle: NonNull<bindings::NDIlib_recv_instance_type>,
}

impl RawReceiver {
    pub(crate) fn raw_ptr(&self) -> bindings::NDIlib_recv_instance_t {
        self.handle.as_ptr()
    }
}

impl Drop for RawReceiver {
    fn drop(&mut self) {
        unsafe { bindings::NDIlib_recv_destroy(self.raw_ptr()) };
    }
}

impl Debug for RawReceiver {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        f.debug_struct("RawReceiver")
            .field("raw_ptr", &self.raw_ptr())
            .finish()
    }
}

unsafe impl Send for RawReceiver {}
unsafe impl Sync for RawReceiver {}

/// A NDI receiver that can receive frames from a source.
///
/// Please note that the receiver handle (from the SDK) will not be dropped until all
/// frames that were received from it are dropped or have their buffers deallocated.[^note]
///
/// [^note]: The inner receiver is [Arc]ed because all frames received need to be dropped on the receiver handle and therefore need a valid reference to it
pub struct NDIReceiver {
    handle: Arc<RawReceiver>,
}

assert_impl_all!(NDIReceiver: Send, Sync);

impl NDIReceiver {
    /// Switches the receiver to the given source.
    pub fn set_source(&self, source: &impl NDISourceLike) {
        source.with_descriptor(|src_ptr| {
            unsafe { bindings::NDIlib_recv_connect(self.handle.raw_ptr(), src_ptr) };
        });
    }

    /// Tries to read into the given buffers and returns which of these has been written to.
    pub fn recv(
        &self,
        mut video: Option<&mut VideoFrame>,
        mut audio: Option<&mut AudioFrame>,
        mut meta: Option<&mut MetadataFrame>,
        timeout: Duration,
    ) -> Result<NDIRecvType, NDIRecvError> {
        let video_ptr = video.to_ffi_recv_frame_ptr();
        if video_ptr.is_null() {
            video = None; // make sure NullPtr's are consistent with the option
        }

        let audio_ptr = audio.to_ffi_recv_frame_ptr();
        if audio_ptr.is_null() {
            audio = None; // make sure NullPtr's are consistent with the option
        }

        let meta_ptr = meta.to_ffi_recv_frame_ptr();
        if meta_ptr.is_null() {
            meta = None; // make sure NullPtr's are consistent with the option
        }

        let timeout: u32 = duration_to_ms(timeout);

        let recv_type = unsafe {
            bindings::NDIlib_recv_capture_v3(
                self.handle.raw_ptr(),
                video_ptr,
                audio_ptr,
                meta_ptr,
                timeout,
            )
        };

        match recv_type {
            bindings::NDIlib_frame_type_e_NDIlib_frame_type_video => {
                video
                    .expect(
                        "[Fatal FFI Error] SDK indicated that a video frame was received, but there is no buffer it could have been written to",
                    )
                    .alloc.update_from_receiver(self.handle.clone());

                #[cfg(any(debug_assertions, feature = "strict_assertions"))]
                {
                    if let Some(audio) = audio {
                        audio.assert_unwritten();
                    }
                    if let Some(meta) = meta {
                        meta.assert_unwritten();
                    }
                }

                Ok(NDIRecvType::Video)
            }
            bindings::NDIlib_frame_type_e_NDIlib_frame_type_audio => {
                audio
                    .expect(
                        "[Fatal FFI Error] SDK indicated that an audio frame was received, but there is no buffer it could have been written to",
                    )
                    .alloc.update_from_receiver(self.handle.clone());

                #[cfg(any(debug_assertions, feature = "strict_assertions"))]
                {
                    if let Some(video) = video {
                        video.assert_unwritten();
                    }
                    if let Some(meta) = meta {
                        meta.assert_unwritten();
                    }
                }

                Ok(NDIRecvType::Audio)
            }
            bindings::NDIlib_frame_type_e_NDIlib_frame_type_metadata => {
                meta.expect(
                    "[Fatal FFI Error] SDK indicated that a metadata frame was received, but there is no buffer it could have been written to",
                )
                .alloc.update_from_receiver(self.handle.clone());

                #[cfg(any(debug_assertions, feature = "strict_assertions"))]
                {
                    if let Some(video) = video {
                        video.assert_unwritten();
                    }
                    if let Some(audio) = audio {
                        audio.assert_unwritten();
                    }
                }

                Ok(NDIRecvType::Metadata)
            }
            bindings::NDIlib_frame_type_e_NDIlib_frame_type_status_change => {
                #[cfg(any(debug_assertions, feature = "strict_assertions"))]
                {
                    if let Some(video) = video {
                        video.assert_unwritten();
                    }
                    if let Some(audio) = audio {
                        audio.assert_unwritten();
                    }
                    if let Some(meta) = meta {
                        meta.assert_unwritten();
                    }
                }

                Ok(NDIRecvType::StatusChange)
            }
            bindings::NDIlib_frame_type_e_NDIlib_frame_type_source_change => {
                #[cfg(any(debug_assertions, feature = "strict_assertions"))]
                {
                    if let Some(video) = video {
                        video.assert_unwritten();
                    }
                    if let Some(audio) = audio {
                        audio.assert_unwritten();
                    }
                    if let Some(meta) = meta {
                        meta.assert_unwritten();
                    }
                }

                Ok(NDIRecvType::SourceChange)
            }
            bindings::NDIlib_frame_type_e_NDIlib_frame_type_none => {
                #[cfg(any(debug_assertions, feature = "strict_assertions"))]
                {
                    if let Some(video) = video {
                        video.assert_unwritten();
                    }
                    if let Some(audio) = audio {
                        audio.assert_unwritten();
                    }
                    if let Some(meta) = meta {
                        meta.assert_unwritten();
                    }
                }

                Ok(NDIRecvType::None)
            }
            #[cfg(any(debug_assertions, feature = "strict_assertions"))]
            discriminant => {
                eprintln!("NDI SDK returned an unknown frame type: {:?}", discriminant);

                if let Some(video) = video {
                    video.assert_unwritten();
                }
                if let Some(audio) = audio {
                    audio.assert_unwritten();
                }
                if let Some(meta) = meta {
                    meta.assert_unwritten();
                }

                Err(NDIRecvError::UnknownType)
            }
            #[cfg(not(any(debug_assertions, feature = "strict_assertions")))]
            _ => Ok(NDIRecvType::None),
        }
    }

    unsafe fn free_string(&self, ptr: *const std::os::raw::c_char) {
        if !ptr.is_null() {
            unsafe { bindings::NDIlib_recv_free_string(self.handle.raw_ptr(), ptr) };
        }
    }

    /// Sends a metadata frame over the current connection.
    pub fn send_metadata(&self, frame: &MetadataFrame) -> Result<(), SendMetadataError> {
        let ptr = frame.to_ffi_send_frame_ptr().map_err(|err| match err {
            crate::frame::generic::FFIReadablePtrError::NotReadable(desc) => {
                SendMetadataError::NotSendable(desc)
            }
        })?;

        let result = unsafe { bindings::NDIlib_recv_send_metadata(self.handle.raw_ptr(), ptr) };

        if result {
            Ok(())
        } else {
            Err(SendMetadataError::NotConnected)
        }
    }

    /// Adds connection metadata which will be sent to every connection in the future.
    pub fn add_connection_metadata(&self, frame: &MetadataFrame) -> Result<(), SendMetadataError> {
        let ptr = frame.to_ffi_send_frame_ptr().map_err(|err| match err {
            crate::frame::generic::FFIReadablePtrError::NotReadable(desc) => {
                SendMetadataError::NotSendable(desc)
            }
        })?;

        unsafe { bindings::NDIlib_recv_add_connection_metadata(self.handle.raw_ptr(), ptr) };

        Ok(())
    }

    /// Removes all connection metadata that was previously added.
    pub fn clear_connection_metadata(&self) {
        unsafe { bindings::NDIlib_recv_clear_connection_metadata(self.handle.raw_ptr()) };
    }

    /// Sets the tally status for the receiver. This will be merged from all receivers on the same
    /// source.
    pub fn set_tally(&self, tally: Tally) {
        let tally = tally.to_ffi();

        unsafe { bindings::NDIlib_recv_set_tally(self.handle.raw_ptr(), &tally) };
    }

    /// Returns the current number of connections to the receiver.
    pub fn get_num_connections(&self) -> usize {
        let num_connections =
            unsafe { bindings::NDIlib_recv_get_no_connections(self.handle.raw_ptr()) };
        num_connections
            .try_into()
            .expect("[Fatal FFI Error] NDI SDK returned a invalid number of connections")
    }

    /// Get the web control URL for the receiver.
    pub fn get_web_control(&self) -> Option<NDIWebControlInfo<'_>> {
        let ptr = unsafe { bindings::NDIlib_recv_get_web_control(self.handle.raw_ptr()) };

        if ptr.is_null() {
            None?;
        }

        let str = unsafe { CStr::from_ptr(ptr) };
        Some(NDIWebControlInfo {
            url: str,
            recv: self,
        })
    }
}

#[non_exhaustive]
#[derive(Debug, Clone, Copy, Hash, PartialEq, Eq)]
pub enum SendMetadataError {
    /// The metadata could not be sent because it is not readable
    NotSendable(&'static str),
    /// The metadata could not be sent because the receiver is not connected
    NotConnected,
}

impl std::fmt::Display for SendMetadataError {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        match self {
            Self::NotSendable(message) => write!(f, "Metadata is not readable: {message}"),
            Self::NotConnected => f.write_str("Receiver is not connected, cannot send metadata"),
        }
    }
}

impl Error for SendMetadataError {}

pub struct NDIWebControlInfo<'a> {
    url: &'a CStr,
    recv: &'a NDIReceiver,
}

impl<'a> Drop for NDIWebControlInfo<'a> {
    fn drop(&mut self) {
        unsafe { self.recv.free_string(self.url.as_ptr()) };
    }
}

impl<'a> NDIWebControlInfo<'a> {
    pub fn as_cstr(&self) -> &'a CStr {
        self.url
    }
}