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
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
#![warn(missing_docs)]
#![warn(unsafe_op_in_unsafe_fn)]
use internal::{bindings::*, OnDrop};
use std::{
convert::TryFrom,
ffi::{CStr, CString},
fmt::{Debug, Display},
ptr::{null, null_mut},
sync::Arc,
};
pub mod error;
pub mod find;
#[doc(hidden)]
pub mod internal;
pub mod recv;
pub mod send;
#[doc(hidden)]
pub use error::*;
#[doc(hidden)]
pub use find::*;
#[doc(hidden)]
pub use recv::*;
#[doc(hidden)]
pub use send::*;
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum FrameType {
None = NDIlib_frame_type_e_NDIlib_frame_type_none as _,
Video = NDIlib_frame_type_e_NDIlib_frame_type_video as _,
Audio = NDIlib_frame_type_e_NDIlib_frame_type_audio as _,
Metadata = NDIlib_frame_type_e_NDIlib_frame_type_metadata as _,
StatusChange = NDIlib_frame_type_e_NDIlib_frame_type_status_change as _,
ErrorFrame = NDIlib_frame_type_e_NDIlib_frame_type_error as _,
}
impl TryFrom<NDIlib_frame_type_e> for FrameType {
type Error = InvalidEnum;
fn try_from(value: NDIlib_frame_type_e) -> Result<Self, Self::Error> {
#[allow(non_upper_case_globals)]
match value {
NDIlib_frame_type_e_NDIlib_frame_type_audio => Ok(FrameType::Audio),
NDIlib_frame_type_e_NDIlib_frame_type_video => Ok(FrameType::Video),
NDIlib_frame_type_e_NDIlib_frame_type_none => Ok(FrameType::None),
NDIlib_frame_type_e_NDIlib_frame_type_status_change => Ok(FrameType::StatusChange),
NDIlib_frame_type_e_NDIlib_frame_type_error => Ok(FrameType::ErrorFrame),
NDIlib_frame_type_e_NDIlib_frame_type_metadata => Ok(FrameType::Metadata),
x => Err(InvalidEnum(x as _, "FrameType")),
}
}
}
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum FrameFormatType {
Progressive = NDIlib_frame_format_type_e_NDIlib_frame_format_type_progressive as _,
Interleaved = NDIlib_frame_format_type_e_NDIlib_frame_format_type_interleaved as _,
Field0 = NDIlib_frame_format_type_e_NDIlib_frame_format_type_field_0 as _,
Field1 = NDIlib_frame_format_type_e_NDIlib_frame_format_type_field_1 as _,
}
impl TryFrom<NDIlib_frame_format_type_e> for FrameFormatType {
type Error = InvalidEnum;
fn try_from(value: NDIlib_frame_format_type_e) -> Result<Self, Self::Error> {
#[allow(non_upper_case_globals)]
match value {
NDIlib_frame_format_type_e_NDIlib_frame_format_type_progressive => {
Ok(FrameFormatType::Progressive)
}
NDIlib_frame_format_type_e_NDIlib_frame_format_type_interleaved => {
Ok(FrameFormatType::Interleaved)
}
NDIlib_frame_format_type_e_NDIlib_frame_format_type_field_0 => {
Ok(FrameFormatType::Field0)
}
NDIlib_frame_format_type_e_NDIlib_frame_format_type_field_1 => {
Ok(FrameFormatType::Field1)
}
x => Err(InvalidEnum(x as _, "FrameFormatType")),
}
}
}
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum FourCCVideoType {
UYVY = NDIlib_FourCC_video_type_e_NDIlib_FourCC_type_UYVY as _,
UYVA = NDIlib_FourCC_video_type_e_NDIlib_FourCC_type_UYVA as _,
P216 = NDIlib_FourCC_video_type_e_NDIlib_FourCC_type_P216 as _,
PA16 = NDIlib_FourCC_video_type_e_NDIlib_FourCC_type_PA16 as _,
YV12 = NDIlib_FourCC_video_type_e_NDIlib_FourCC_type_YV12 as _,
I420 = NDIlib_FourCC_video_type_e_NDIlib_FourCC_type_I420 as _,
NV12 = NDIlib_FourCC_video_type_e_NDIlib_FourCC_type_NV12 as _,
BGRA = NDIlib_FourCC_video_type_e_NDIlib_FourCC_type_BGRA as _,
BGRX = NDIlib_FourCC_video_type_e_NDIlib_FourCC_type_BGRX as _,
RGBA = NDIlib_FourCC_video_type_e_NDIlib_FourCC_type_RGBA as _,
RGBX = NDIlib_FourCC_video_type_e_NDIlib_FourCC_type_RGBX as _,
}
impl TryFrom<NDIlib_FourCC_video_type_e> for FourCCVideoType {
type Error = InvalidEnum;
fn try_from(value: NDIlib_FourCC_video_type_e) -> Result<Self, Self::Error> {
#[allow(non_upper_case_globals)]
match value {
NDIlib_FourCC_video_type_e_NDIlib_FourCC_type_UYVY => Ok(FourCCVideoType::UYVY),
NDIlib_FourCC_video_type_e_NDIlib_FourCC_type_UYVA => Ok(FourCCVideoType::UYVA),
NDIlib_FourCC_video_type_e_NDIlib_FourCC_type_P216 => Ok(FourCCVideoType::P216),
NDIlib_FourCC_video_type_e_NDIlib_FourCC_type_PA16 => Ok(FourCCVideoType::PA16),
NDIlib_FourCC_video_type_e_NDIlib_FourCC_type_YV12 => Ok(FourCCVideoType::YV12),
NDIlib_FourCC_video_type_e_NDIlib_FourCC_type_I420 => Ok(FourCCVideoType::I420),
NDIlib_FourCC_video_type_e_NDIlib_FourCC_type_NV12 => Ok(FourCCVideoType::NV12),
NDIlib_FourCC_video_type_e_NDIlib_FourCC_type_BGRA => Ok(FourCCVideoType::BGRA),
NDIlib_FourCC_video_type_e_NDIlib_FourCC_type_RGBA => Ok(FourCCVideoType::RGBA),
NDIlib_FourCC_video_type_e_NDIlib_FourCC_type_BGRX => Ok(FourCCVideoType::BGRX),
NDIlib_FourCC_video_type_e_NDIlib_FourCC_type_RGBX => Ok(FourCCVideoType::RGBX),
x => Err(InvalidEnum(x as _, "FourCCVideoType")),
}
}
}
#[derive(Debug, Clone, Copy)]
pub enum FourCCAudioType {
FLTP = NDIlib_FourCC_audio_type_e_NDIlib_FourCC_type_FLTP as _,
}
impl TryFrom<NDIlib_FourCC_audio_type_e> for FourCCAudioType {
type Error = InvalidEnum;
fn try_from(value: NDIlib_FourCC_audio_type_e) -> Result<Self, Self::Error> {
#[allow(non_upper_case_globals)]
match value {
NDIlib_FourCC_audio_type_e_NDIlib_FourCC_type_FLTP => Ok(FourCCAudioType::FLTP),
x => Err(InvalidEnum(x as _, "FourCCAudioType")),
}
}
}
#[derive(Clone)]
enum SourceParent {
Find(Arc<OnDrop<NDIlib_find_instance_t>>),
Send(Arc<OnDrop<NDIlib_send_instance_t>>),
None,
}
#[derive(Clone)]
pub struct Source {
p_instance: NDIlib_source_t,
parent: SourceParent,
}
impl Debug for Source {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
f.debug_struct("ndi::Source")
.field("name", &self.get_name())
.finish()
}
}
impl Source {
fn from_binding(parent: SourceParent, source: NDIlib_source_t) -> Self {
Self {
parent,
p_instance: source,
}
}
fn new() -> Self {
let p_instance = NDIlib_source_t {
p_ndi_name: null(),
__bindgen_anon_1: NDIlib_source_t__bindgen_ty_1 {
p_ip_address: null(),
},
};
Self {
parent: SourceParent::None,
p_instance,
}
}
pub fn get_name(&self) -> String {
let name_char_ptr: *mut std::os::raw::c_char = self.p_instance.p_ndi_name as _;
if name_char_ptr.is_null() {
return String::new();
}
let name = unsafe {
CStr::from_ptr(name_char_ptr)
.to_owned()
.to_string_lossy()
.to_string()
};
name
}
}
unsafe impl core::marker::Send for Source {}
unsafe impl core::marker::Sync for Source {}
#[repr(C)]
#[derive(Debug, Copy, Clone)]
pub struct Tally {
pub on_program: bool,
pub on_preview: bool,
}
impl Tally {
pub fn new() -> Self {
Self {
on_program: false,
on_preview: false,
}
}
}
impl Default for Tally {
fn default() -> Self {
Self::new()
}
}
impl From<NDIlib_tally_t> for Tally {
fn from(x: NDIlib_tally_t) -> Self {
Self {
on_preview: x.on_preview,
on_program: x.on_program,
}
}
}
impl Into<NDIlib_tally_t> for Tally {
fn into(self) -> NDIlib_tally_t {
NDIlib_tally_t {
on_preview: self.on_preview,
on_program: self.on_program,
}
}
}
enum VideoParent {
Recv(Arc<OnDrop<NDIlib_recv_instance_t>>),
Owned,
}
pub struct VideoData {
p_instance: NDIlib_video_frame_v2_t,
parent: VideoParent,
}
unsafe impl core::marker::Send for VideoData {}
unsafe impl core::marker::Sync for VideoData {}
impl Debug for VideoData {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
f.debug_struct("VideoData")
.field("width", &self.width())
.field("height", &self.height())
.field("line_stride_in_bytes", &self.line_stride_in_bytes())
.field("data_size", &self.data_size_in_bytes())
.field("fourcc", &self.four_cc())
.field("frame_format_type", &self.frame_format_type())
.field(
"frame_rate",
&format!(
"{}/{} = {}",
self.frame_rate_n(),
self.frame_rate_d(),
self.frame_rate_n() as f32 / self.frame_rate_d() as f32
),
)
.field("timestamp", &self.timestamp())
.field("timecode", &self.timecode())
.field("metadata", &self.metadata())
.finish()
}
}
impl VideoData {
fn from_binding_recv(
recv: Arc<OnDrop<NDIlib_recv_instance_t>>,
p_instance: NDIlib_video_frame_v2_t,
) -> Self {
Self {
p_instance,
parent: VideoParent::Recv(recv),
}
}
pub fn new() -> Self {
Self {
p_instance: NDIlib_video_frame_v2_t {
xres: 0,
yres: 0,
FourCC: FourCCVideoType::UYVY as _,
frame_rate_N: 60,
frame_rate_D: 0,
picture_aspect_ratio: 0f32,
frame_format_type: FrameFormatType::Progressive as _,
timecode: 0,
p_data: null_mut(),
__bindgen_anon_1: NDIlib_video_frame_v2_t__bindgen_ty_1 {
line_stride_in_bytes: 0,
},
p_metadata: null(),
timestamp: 0,
},
parent: VideoParent::Owned,
}
}
pub fn width(&self) -> u32 {
self.p_instance.xres as _
}
pub fn height(&self) -> u32 {
self.p_instance.yres as _
}
pub fn four_cc(&self) -> FourCCVideoType {
FourCCVideoType::try_from(self.p_instance.FourCC).unwrap()
}
pub fn frame_rate_n(&self) -> u32 {
self.p_instance.frame_rate_N as _
}
pub fn frame_rate_d(&self) -> u32 {
self.p_instance.frame_rate_D as _
}
pub fn frame_rate(&self) -> f32 {
self.p_instance.frame_rate_N as f32 / self.p_instance.frame_rate_D as f32
}
pub fn picture_aspect_ratio(&self) -> f32 {
self.p_instance.picture_aspect_ratio
}
pub fn frame_format_type(&self) -> FrameFormatType {
FrameFormatType::try_from(self.p_instance.frame_format_type).unwrap()
}
pub fn timecode(&self) -> i64 {
self.p_instance.timecode
}
pub fn p_data(&self) -> *mut u8 {
self.p_instance.p_data
}
pub fn line_stride_in_bytes(&self) -> Option<u32> {
unsafe { Some(self.p_instance.__bindgen_anon_1.line_stride_in_bytes as _) }
}
pub fn data_size_in_bytes(&self) -> Option<u32> {
unsafe { Some(self.p_instance.__bindgen_anon_1.data_size_in_bytes as _) }
}
pub fn metadata(&self) -> String {
let metadata_char_ptr = self.p_instance.p_metadata;
if metadata_char_ptr.is_null() {
return String::new();
}
let metadata = unsafe { CStr::from_ptr(metadata_char_ptr) }
.to_owned()
.to_string_lossy()
.to_string();
metadata
}
pub fn timestamp(&self) -> Option<i64> {
let timestamp = self.p_instance.timestamp;
if timestamp == NDIlib_recv_timestamp_undefined {
None
} else {
Some(timestamp)
}
}
}
impl Drop for VideoData {
fn drop(&mut self) {
match &self.parent {
VideoParent::Recv(recv) => unsafe {
NDIlib_recv_free_video_v2(***recv, &mut self.p_instance);
},
VideoParent::Owned => {}
}
}
}
enum AudioParent {
Recv(Arc<OnDrop<NDIlib_recv_instance_t>>),
Owned,
}
pub struct AudioData {
p_instance: NDIlib_audio_frame_v3_t,
parent: AudioParent,
}
unsafe impl core::marker::Send for AudioData {}
unsafe impl core::marker::Sync for AudioData {}
impl Debug for AudioData {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
f.debug_struct("AudioData")
.field("sample_rate", &self.sample_rate())
.field("no_samples", &self.no_samples())
.field("no_channels", &self.no_channels())
.field("timestamp", &self.timestamp())
.field("timecode", &self.timecode())
.field("fourcc", &self.four_cc())
.field("metadata", &self.metadata())
.finish()
}
}
impl AudioData {
fn from_binding_recv(
recv: Arc<OnDrop<NDIlib_recv_instance_t>>,
p_instance: NDIlib_audio_frame_v3_t,
) -> Self {
Self {
p_instance,
parent: AudioParent::Recv(recv),
}
}
pub fn new() -> Self {
Self {
p_instance: NDIlib_audio_frame_v3_t {
sample_rate: 0,
no_channels: 0,
no_samples: 0,
timecode: 0,
FourCC: FourCCAudioType::FLTP as _,
p_data: null_mut(),
__bindgen_anon_1: NDIlib_audio_frame_v3_t__bindgen_ty_1 {
channel_stride_in_bytes: 0,
},
p_metadata: "".as_ptr() as _,
timestamp: 0,
},
parent: AudioParent::Owned,
}
}
pub fn sample_rate(&self) -> u32 {
self.p_instance.sample_rate as _
}
pub fn no_channels(&self) -> u32 {
self.p_instance.no_channels as _
}
pub fn no_samples(&self) -> u32 {
self.p_instance.no_samples as _
}
pub fn timecode(&self) -> i64 {
self.p_instance.timecode
}
pub fn timestamp(&self) -> Option<i64> {
let timestamp = self.p_instance.timestamp;
if timestamp == NDIlib_recv_timestamp_undefined {
None
} else {
Some(timestamp)
}
}
pub fn p_data(&self) -> *mut u8 {
self.p_instance.p_data
}
pub fn four_cc(&self) -> FourCCAudioType {
#[allow(non_upper_case_globals)]
match self.p_instance.FourCC {
NDIlib_FourCC_audio_type_e_NDIlib_FourCC_type_FLTP => FourCCAudioType::FLTP,
x => panic!("Unknown NDI FourCC Audio type encountered: {}", x),
}
}
pub fn channel_stride_in_bytes(&self) -> u32 {
match self.four_cc() {
FourCCAudioType::FLTP => unsafe {
self.p_instance.__bindgen_anon_1.channel_stride_in_bytes as _
},
}
}
pub fn metadata(&self) -> String {
let metadata_char_ptr = self.p_instance.p_metadata;
if metadata_char_ptr.is_null() {
return String::new();
}
let metadata = unsafe { CStr::from_ptr(metadata_char_ptr) }
.to_owned()
.to_string_lossy()
.to_string();
metadata
}
}
impl Drop for AudioData {
fn drop(&mut self) {
match &self.parent {
AudioParent::Recv(recv) => unsafe {
NDIlib_recv_free_audio_v3(***recv, &self.p_instance);
},
AudioParent::Owned => {}
}
}
}
enum MetaDataParent {
Recv(Arc<OnDrop<NDIlib_recv_instance_t>>),
Send(Arc<OnDrop<NDIlib_send_instance_t>>),
Owned,
}
pub struct MetaData {
p_instance: NDIlib_metadata_frame_t,
parent: MetaDataParent,
}
unsafe impl core::marker::Send for MetaData {}
unsafe impl core::marker::Sync for MetaData {}
impl Debug for MetaData {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
f.debug_struct("MetaData")
.field("length", &self.length())
.field("data", &self.data())
.field("timecode", &self.timecode())
.finish()
}
}
impl MetaData {
fn from_binding_recv(
recv: Arc<OnDrop<NDIlib_recv_instance_t>>,
p_instance: NDIlib_metadata_frame_t,
) -> Self {
Self {
p_instance,
parent: MetaDataParent::Recv(recv),
}
}
fn from_binding_send(
send: Arc<OnDrop<NDIlib_send_instance_t>>,
p_instance: NDIlib_metadata_frame_t,
) -> Self {
Self {
p_instance,
parent: MetaDataParent::Send(send),
}
}
pub fn new(length: u32, timecode: i64, data: String) -> Self {
let p_data = CString::new(data).unwrap().into_raw();
let p_instance = NDIlib_metadata_frame_t {
length: length as _,
timecode,
p_data,
};
Self {
p_instance,
parent: MetaDataParent::Owned,
}
}
pub fn length(&self) -> u32 {
self.p_instance.length as _
}
pub fn timecode(&self) -> i64 {
self.p_instance.timecode
}
pub fn data(&self) -> String {
let char_ptr = self.p_instance.p_data;
let data = unsafe { CStr::from_ptr(char_ptr).to_string_lossy().to_string() };
data
}
}
impl Drop for MetaData {
fn drop(&mut self) {
match &self.parent {
MetaDataParent::Recv(recv) => unsafe {
NDIlib_recv_free_metadata(***recv, &mut self.p_instance);
},
MetaDataParent::Send(send) => unsafe {
NDIlib_send_free_metadata(***send, &mut self.p_instance);
},
MetaDataParent::Owned => {}
}
}
}
pub fn initialize() -> Result<(), NotSupported> {
if !unsafe { NDIlib_initialize() } {
return Err(NotSupported);
};
Ok(())
}
pub unsafe fn cleanup() {
unsafe { NDIlib_destroy() };
}
#[allow(non_snake_case)]
pub fn is_supported_CPU() -> bool {
unsafe { NDIlib_is_supported_CPU() }
}