#![allow(dead_code)]
use ff_format::{PixelFormat, PooledBuffer, Timestamp, VideoFrame};
use std::path::PathBuf;
pub fn test_output_dir() -> PathBuf {
let manifest_dir = env!("CARGO_MANIFEST_DIR");
PathBuf::from(format!("{}/target/test-output", manifest_dir))
}
pub fn test_output_path(filename: &str) -> PathBuf {
let output_dir = test_output_dir();
std::fs::create_dir_all(&output_dir).ok();
output_dir.join(filename)
}
pub fn create_black_frame(width: u32, height: u32) -> VideoFrame {
create_yuv420p_frame(width, height, 0, 128, 128)
}
pub fn create_white_frame(width: u32, height: u32) -> VideoFrame {
create_yuv420p_frame(width, height, 255, 128, 128)
}
fn create_yuv420p_frame(width: u32, height: u32, y: u8, u: u8, v: u8) -> VideoFrame {
let y_size = (width * height) as usize;
let uv_size = ((width / 2) * (height / 2)) as usize;
let y_plane = PooledBuffer::standalone(vec![y; y_size]);
let u_plane = PooledBuffer::standalone(vec![u; uv_size]);
let v_plane = PooledBuffer::standalone(vec![v; uv_size]);
let strides = vec![width as usize, (width / 2) as usize, (width / 2) as usize];
VideoFrame::new(
vec![y_plane, u_plane, v_plane],
strides,
width,
height,
PixelFormat::Yuv420p,
Timestamp::default(),
true,
)
.expect("Failed to create video frame")
}
pub struct FileGuard {
path: PathBuf,
}
impl FileGuard {
pub fn new(path: PathBuf) -> Self {
Self { path }
}
pub fn path(&self) -> &PathBuf {
&self.path
}
}
impl Drop for FileGuard {
fn drop(&mut self) {
if self.path.exists() {
let _ = std::fs::remove_file(&self.path);
}
}
}
pub fn assert_valid_output_file(path: &PathBuf) {
assert!(path.exists(), "Output file does not exist: {:?}", path);
let metadata = std::fs::metadata(path).expect("Failed to read file metadata");
assert!(metadata.len() > 0, "Output file is empty: {:?}", path);
}
pub fn get_file_size(path: &PathBuf) -> u64 {
std::fs::metadata(path)
.expect("Failed to read file metadata")
.len()
}