Skip to content

Commit

Permalink
Merge branch 'braid-process-video-braidz-output'
Browse files Browse the repository at this point in the history
  • Loading branch information
astraw committed Jan 11, 2025
2 parents ad094f1 + b54ca45 commit 7c81077
Show file tree
Hide file tree
Showing 5 changed files with 87 additions and 40 deletions.
18 changes: 12 additions & 6 deletions braid-process-video/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,8 @@ mod synced_iter;
mod config;
pub(crate) use config::FeatureDetectionMethod;
pub use config::{
BraidRetrackVideoConfig, OutputConfig, Valid, Validate, VideoOutputConfig, VideoSourceConfig,
BraidRetrackVideoConfig, BraidzOutputConfig, OutputConfig, Valid, Validate, VideoOutputConfig,
VideoSourceConfig,
};

mod auto_config_generator;
Expand Down Expand Up @@ -777,7 +778,7 @@ pub async fn run_config(cfg: &Valid<BraidRetrackVideoConfig>) -> Result<Vec<std:
fd: std::fs::File::create(&output_filename)?,
})),
OutputConfig::Braidz(b) => {
let (braidz_storage, coord_proc_fut) = output_braidz::BraidStorage::new(
let braidz_storage = output_braidz::BraidStorage::new(
cfg,
&b,
tracking_parameters.clone(),
Expand All @@ -788,8 +789,6 @@ pub async fn run_config(cfg: &Valid<BraidRetrackVideoConfig>) -> Result<Vec<std:
)
.await?;

coord_proc_fut.await?.await??;

Ok(OutputStorage::Braid(braidz_storage))
}
}
Expand Down Expand Up @@ -849,10 +848,17 @@ pub async fn run_config(cfg: &Valid<BraidRetrackVideoConfig>) -> Result<Vec<std:

pb.finish_and_clear();

Ok(output_storage
// collect output filenames
let outputs = output_storage
.iter()
.map(|d| d.path().to_path_buf())
.collect())
.collect();

for output in output_storage.into_iter() {
output.close().await?;
}

Ok(outputs)
}

fn gather_frame_data<'a>(
Expand Down
52 changes: 32 additions & 20 deletions braid-process-video/src/output_braidz.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,5 @@
use eyre::{self as anyhow, Result};
use std::{
collections::{BTreeMap, BTreeSet},
future::Future,
};
use std::collections::{BTreeMap, BTreeSet};

use flydra_types::{PerCamSaveData, RawCamName};

Expand All @@ -15,6 +12,7 @@ pub(crate) struct BraidStorage {
pub(crate) cam_manager: flydra2::ConnectedCamerasManager,
pub(crate) frame_data_tx: tokio::sync::mpsc::Sender<flydra2::StreamItem>,
pub(crate) output_braidz_path: std::path::PathBuf,
pub(crate) coord_proc_jh: tokio::task::JoinHandle<Result<()>>,
}

impl BraidStorage {
Expand All @@ -26,12 +24,7 @@ impl BraidStorage {
all_expected_cameras: BTreeSet<RawCamName>,
expected_framerate: Option<f32>,
braidz_calibration: Option<braidz_types::CalibrationInfo>,
) -> Result<(
Self,
impl Future<
Output = Result<tokio::task::JoinHandle<Result<(), flydra2::Error>>, flydra2::Error>,
>,
)> {
) -> Result<Self> {
let output_braidz_path = std::path::PathBuf::from(&b.filename);
let output_dirname =
if output_braidz_path.extension() == Some(std::ffi::OsStr::new("braidz")) {
Expand Down Expand Up @@ -141,17 +134,20 @@ impl BraidStorage {
.await
.unwrap();

let coord_proc_fut = coord_processor.consume_stream(frame_data_rx, expected_framerate);

Ok((
Self {
cam_manager,
frame_data_tx,
output_braidz_path,
},
coord_proc_fut,
))
let coord_proc_jh = tokio::spawn(async move {
let coord_proc_fut = coord_processor.consume_stream(frame_data_rx, expected_framerate);
coord_proc_fut.await?.await??;
Ok(())
});

Ok(Self {
cam_manager,
frame_data_tx,
output_braidz_path,
coord_proc_jh,
})
}

pub(crate) async fn render_frame(
&mut self,
out_fno: usize,
Expand Down Expand Up @@ -211,4 +207,20 @@ impl BraidStorage {
}
Ok(())
}

pub(crate) async fn close(self) -> Result<()> {
let BraidStorage {
cam_manager: _,
frame_data_tx,
output_braidz_path: _,
coord_proc_jh,
} = self;

// Stop transmitting frames, which causes coord processor to end.
std::mem::drop(frame_data_tx);

// Wait for coord processor to finish.
coord_proc_jh.await??;
Ok(())
}
}
12 changes: 12 additions & 0 deletions braid-process-video/src/output_types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,14 @@ impl<'lib> OutputStorage<'lib> {
OutputStorage::Video(v) => &v.path,
}
}

pub(crate) async fn close(self) -> Result<()> {
match self {
OutputStorage::Debug(d) => d.close().await,
OutputStorage::Braid(b) => b.close().await,
OutputStorage::Video(v) => v.close().await,
}
}
}

pub(crate) struct DebugStorage {
Expand Down Expand Up @@ -100,4 +108,8 @@ impl DebugStorage {
}
Ok(())
}

pub(crate) async fn close(self) -> Result<()> {
Ok(())
}
}
19 changes: 14 additions & 5 deletions braid-process-video/src/output_video.rs
Original file line number Diff line number Diff line change
Expand Up @@ -76,25 +76,30 @@ impl<'lib> VideoStorage<'lib> {

let feature_radius = v
.video_options
.feature_radius.clone()
.feature_radius
.clone()
.unwrap_or_else(|| crate::DEFAULT_FEATURE_RADIUS.to_string());
let feature_style = v
.video_options
.feature_style.clone()
.feature_style
.clone()
.unwrap_or_else(|| crate::DEFAULT_FEATURE_STYLE.to_string());

let reprojected_radius = v
.video_options
.reprojected_radius.clone()
.reprojected_radius
.clone()
.unwrap_or_else(|| crate::DEFAULT_REPROJECTED_RADIUS.to_string());
let reprojected_style = v
.video_options
.reprojected_style.clone()
.reprojected_style
.clone()
.unwrap_or_else(|| crate::DEFAULT_REPROJECTED_STYLE.to_string());

let cam_text_style = v
.video_options
.cam_text_style.clone()
.cam_text_style
.clone()
.unwrap_or_else(|| crate::DEFAULT_CAMERA_TEXT_STYLE.to_string());

let mut usvg_opt = usvg::Options::default();
Expand Down Expand Up @@ -309,4 +314,8 @@ impl<'lib> VideoStorage<'lib> {

Ok(())
}

pub(crate) async fn close(self) -> Result<()> {
Ok(())
}
}
26 changes: 17 additions & 9 deletions braid-process-video/tests/test-montage.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
use eyre::{self as anyhow};

use braid_process_video::{
BraidRetrackVideoConfig, OutputConfig, Valid, Validate, VideoOutputConfig, VideoSourceConfig,
BraidRetrackVideoConfig, BraidzOutputConfig, OutputConfig, Valid, Validate, VideoOutputConfig,
VideoSourceConfig,
};

const BASE_URL: &str = "https://strawlab-cdn.com/assets/flycube6-videos";
Expand All @@ -10,14 +11,16 @@ const SOURCE_JSON: &str = include_str!("source.json");
async fn do_config(cfg: &Valid<BraidRetrackVideoConfig>) -> anyhow::Result<()> {
// generate the output
let output_fnames = braid_process_video::run_config(cfg).await?;
assert_eq!(output_fnames.len(), 1);
let output_fname = output_fnames[0].clone();
assert_eq!(output_fnames.len(), 2);
let output_mp4 = output_fnames[0].clone();

// start parsing output
let do_decode_h264 = false;
let _src = frame_source::from_path(&output_fname, do_decode_h264)?;
let _src = frame_source::from_path(&output_mp4, do_decode_h264)?;

// TODO: check output. How?
// TODO: check output mp4. How?

// TODO: check output braidz

Ok(())
}
Expand Down Expand Up @@ -71,10 +74,15 @@ fn get_files(
}

let input_braidz = input_braidz.map(Into::into);
let output = vec![OutputConfig::Video(VideoOutputConfig {
filename: format!("tests/rendered/{}.mp4", dirname),
video_options: Default::default(),
})];
let output = vec![
OutputConfig::Video(VideoOutputConfig {
filename: format!("tests/rendered/{}.mp4", dirname),
video_options: Default::default(),
}),
OutputConfig::Braidz(BraidzOutputConfig {
filename: format!("tests/rendered/{dirname}.braidz"),
}),
];

let cfg = BraidRetrackVideoConfig {
input_braidz,
Expand Down

0 comments on commit 7c81077

Please sign in to comment.