Skip to content

Commit

Permalink
Use permalink to code example
Browse files Browse the repository at this point in the history
  • Loading branch information
GrantSparks committed Jul 23, 2024
1 parent e384137 commit cead8df
Showing 1 changed file with 1 addition and 75 deletions.
76 changes: 1 addition & 75 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -34,81 +34,7 @@ cargo run --example NDIlib_Find
The following example demonstrates how to use the `grafton-ndi` library to find NDI sources on the network and receive frames from a source.

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

use grafton_ndi::{Find, Finder, FrameType, Receiver, Recv, RecvBandwidth, RecvColorFormat, NDI};

fn main() -> Result<(), &'static str> {
// Initialize the NDI library and ensure it's properly cleaned up
if let Ok(_ndi) = NDI::new() {
// Create an NDI finder to locate sources on the network
let finder = Finder::default();
let ndi_find = Find::new(finder)?;

// Wait up to 5 seconds to check for new sources
if !ndi_find.wait_for_sources(5000) {
println!("No sources found.");
return Err("No sources found");
}

// Get the list of sources
let sources = ndi_find.get_sources(5000);
if sources.is_empty() {
println!("No sources found.");
return Err("No sources found");
}

// Display all the sources
println!("Network sources ({} found):", sources.len());
for (i, source) in sources.iter().enumerate() {
println!("{}. {}", i + 1, source.name);
}

// Create a receiver to connect to the first source
let source_to_connect_to = sources[0].clone();
let receiver = Receiver::new(
source_to_connect_to,
RecvColorFormat::UYVY_BGRA,
RecvBandwidth::Highest,
true,
Some("Example Receiver".to_string()),
);

let ndi_recv = Recv::new(receiver).expect("Failed to create NDI recv instance");

// Run for 5 seconds
let start = Instant::now();
while start.elapsed() < Duration::from_secs(5) {
// Receive something
match ndi_recv.capture(1000) {
Ok(FrameType::None) => {}
Ok(FrameType::Video(_)) => {
println!("Received a video frame");
// Handle video frame
}
Ok(FrameType::Audio(_)) => {
println!("Received an audio frame");
// Handle audio frame
}
Ok(FrameType::Metadata(_)) => {
println!("Received a metadata frame");
// Handle metadata frame
}
Err(_) => {
println!("Failed to receive frame");
}
}
}

// Destroy the receiver
drop(ndi_recv);
} else {
return Err("Failed to initialize NDI library");
}

// The Drop trait for NDI will take care of calling NDIlib_destroy()
Ok(())
}
https://github.com/GrantSparks/grafton-ndi/blob/e3841377cc2f26447b6239165c086e89aa11b2ad/examples/NDIlib_Find.rs
```

## Best Practices
Expand Down

0 comments on commit cead8df

Please sign in to comment.