Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ci: add rust binding build #701

Merged
merged 4 commits into from
Jan 12, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 9 additions & 1 deletion .github/workflows/ecosystem.yml
Original file line number Diff line number Diff line change
Expand Up @@ -83,4 +83,12 @@ jobs:

- name: Build python binding
run: |
./script/build_python.sh
./script/build_python.sh

- name: Build Rust binding
run: |
cd rust/
cargo build --verbose
cd imtl-sys
cargo build --verbose
cargo test --verbose
46 changes: 19 additions & 27 deletions rust/examples/video-rx.rs
Original file line number Diff line number Diff line change
@@ -1,11 +1,10 @@
use anyhow::{Context, Result};
use anyhow::{bail, Context, Result};
use clap::Parser;
use sdl2::pixels::PixelFormatEnum;
use sdl2::render::{Canvas, Texture};
use sdl2::video::Window;
use std::io::Write;
use std::net::Ipv4Addr;
use std::rc::Rc;
use std::str::FromStr;
use std::sync::atomic::{AtomicBool, Ordering};
use std::sync::Arc;
Expand Down Expand Up @@ -66,6 +65,11 @@ struct Args {

fn main() -> Result<()> {
let args = Args::parse();
let display = args.display;
let save_yuv = args.yuv.is_some();
if (save_yuv && display) || (!save_yuv && !display) {
bail!("Only one of --yuv or --display should be set");
}

let running = Arc::new(AtomicBool::new(true));
let r = running.clone();
Expand Down Expand Up @@ -100,10 +104,10 @@ fn main() -> Result<()> {
.init()
.context("Failed to init mtl")?;

let net_dev0 = Rc::new(mtl.net_devs()[0].clone());
let net_dev0 = mtl.net_devs()[0].clone();

let session = RtpSessionBuilder::default()
.net_dev(net_dev0.clone())
.net_dev(net_dev0)
.ip(args.ip)
.port(args.port)
.payload_type(112u8)
Expand All @@ -128,31 +132,21 @@ fn main() -> Result<()> {
let texture_creator;
let mut canvas: Option<Canvas<Window>> = None;
let mut texture: Option<Texture> = None;
if args.display {
if display {
sdl_context = sdl2::init().unwrap();
video_subsystem = sdl_context.video().unwrap();
window = video_subsystem
.window("IMTL RX Video", args.width / 4, args.height / 4)
.position_centered()
.opengl()
.build()
.map_err(|e| e.to_string())
.unwrap();

canvas = Some(
window
.into_canvas()
.build()
.map_err(|e| e.to_string())
.unwrap(),
);
.build()?;
canvas = Some(window.into_canvas().build()?);
texture_creator = canvas.as_ref().unwrap().texture_creator();
texture = Some(
texture_creator
.create_texture_streaming(PixelFormatEnum::UYVY, args.width, args.height)
.map_err(|e| e.to_string())
.unwrap(),
);
texture = Some(texture_creator.create_texture_streaming(
PixelFormatEnum::UYVY,
args.width,
args.height,
)?);
}

let frame = vec![0u8; video_rx.frame_size()];
Expand All @@ -161,10 +155,7 @@ fn main() -> Result<()> {
match video_rx.fill_new_frame(&frame) {
Ok(_) => {
if let (Some(ref mut texture), Some(ref mut canvas)) = (&mut texture, &mut canvas) {
texture
.update(None, &frame, args.width as usize * 2)
.map_err(|e| e.to_string())
.unwrap();
texture.update(None, &frame, args.width as usize * 2)?;
canvas.clear();
canvas.copy(&texture, None, None).unwrap();
canvas.present();
Expand All @@ -177,7 +168,8 @@ fn main() -> Result<()> {
}

// create a yuv file and save the frame to it
if let Some(file_name) = args.yuv {
if save_yuv {
let file_name = args.yuv.unwrap();
let mut yuv_file = std::fs::File::create(&file_name)?;
yuv_file.write_all(&frame)?;
println!("Wrote frame to yuv file {}", file_name);
Expand Down
35 changes: 11 additions & 24 deletions rust/examples/video-tx.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ use sdl2::pixels::PixelFormatEnum;
use sdl2::render::{Canvas, Texture};
use sdl2::video::Window;
use std::net::Ipv4Addr;
use std::rc::Rc;
use std::str::FromStr;
use std::sync::atomic::{AtomicBool, Ordering};
use std::sync::Arc;
Expand Down Expand Up @@ -103,10 +102,10 @@ fn main() -> Result<()> {
.init()
.context("Failed to init mtl")?;

let net_dev0 = Rc::new(mtl.net_devs()[0].clone());
let net_dev0 = mtl.net_devs()[0].clone();

let session = RtpSessionBuilder::default()
.net_dev(net_dev0.clone())
.net_dev(net_dev0)
.ip(args.ip)
.port(args.port)
.payload_type(112u8)
Expand Down Expand Up @@ -138,24 +137,15 @@ fn main() -> Result<()> {
.window("IMTL TX Video", args.width / 4, args.height / 4)
.position_centered()
.opengl()
.build()
.map_err(|e| e.to_string())
.unwrap();

canvas = Some(
window
.into_canvas()
.build()
.map_err(|e| e.to_string())
.unwrap(),
);
.build()?;

canvas = Some(window.into_canvas().build()?);
texture_creator = canvas.as_ref().unwrap().texture_creator();
texture = Some(
texture_creator
.create_texture_streaming(PixelFormatEnum::UYVY, args.width, args.height)
.map_err(|e| e.to_string())
.unwrap(),
);
texture = Some(texture_creator.create_texture_streaming(
PixelFormatEnum::UYVY,
args.width,
args.height,
)?);
}

let frames = yuv_file.chunks_exact(video_tx.frame_size());
Expand All @@ -169,10 +159,7 @@ fn main() -> Result<()> {
match video_tx.fill_next_frame(frame) {
Ok(_) => {
if let (Some(ref mut texture), Some(ref mut canvas)) = (&mut texture, &mut canvas) {
texture
.update(None, &frame, args.width as usize * 2)
.map_err(|e| e.to_string())
.unwrap();
texture.update(None, &frame, args.width as usize * 2)?;
canvas.clear();
canvas.copy(&texture, None, None).unwrap();
canvas.present();
Expand Down
1 change: 1 addition & 0 deletions rust/imtl-sys/examples/no_std.rs
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ fn main() {
arp_timeout_s: 0,
ptp_sync_notify: None,
rss_sch_nb: [0; 8],
memzone_max: 0,
};

/* initialize dev handle */
Expand Down
12 changes: 8 additions & 4 deletions rust/src/imtl/video.rs
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,10 @@ impl Fps {
Fps::P23_98 => 24000.0 / 1001.0,
}
}

pub fn duration(self, frame_count: u32) -> std::time::Duration {
std::time::Duration::from_secs_f32(frame_count as f32 / self.to_float())
}
}

/// Different transport formats for raw video data.
Expand Down Expand Up @@ -319,11 +323,11 @@ impl VideoTx {
self.frame_size
}

/// Wait until free frame available
/// Wait until free frame available, default timeout is 1 frame interval
pub fn wait_free_frame(&mut self) {
let frame_idx = self.producer_idx;
if !self.is_frame_free(frame_idx) {
self.parker.park();
self.parker.park_timeout(self.fps.duration(1));
}
}

Expand Down Expand Up @@ -507,10 +511,10 @@ impl VideoRx {
self.frame_size
}

/// Wait until new frame available
/// Wait until new frame available, default timeout is 1 frame interval
pub fn wait_new_frame(&mut self) {
if self.frames[self.consumer_idx as usize].is_null() {
self.parker.park();
self.parker.park_timeout(self.fps.duration(1));
}
}

Expand Down
Loading