Skip to content

programmatic video rendering framework that is actually fast

4.2 KB
Raw Blame
//! Platform smoke test: renders a short video with the CPU backend and decodes it back
//! with the fframes decoder. Unlike the visual regression test it needs no reference
//! frames or external tools (ffmpeg CLI, odiff), so it runs on every OS in CI, Windows
//! included.

use fframes::media::FFmpegDecoder;
use fframes::{AudioMap, Color, Duration, FFramesContext, Frame, RenderOptions, Svgr, Video};

const FPS: usize = 10;
const SECONDS: f32 = 2.0;
const SWITCH_AT_SECONDS: f32 = SECONDS / 2.0;

#[derive(Debug)]
struct SmokeVideo;

impl Video for SmokeVideo {
    const FPS: usize = FPS;
    const WIDTH: usize = 320;
    const HEIGHT: usize = 240;
    const BACKGROUND_COLOR: Color = Color::BLACK;

    fn duration(&self) -> Duration<'_> {
        Duration::Seconds(SECONDS)
    }

    fn audio(&self) -> AudioMap<'_> {
        AudioMap::none()
    }

    fn render_frame<'a>(&'a self, frame: Frame, _ctx: &FFramesContext<'a, '_>) -> Svgr<'a> {
        let fill = if frame.seconds() < SWITCH_AT_SECONDS {
            "#ff0000"
        } else {
            "#0000ff"
        };

        fframes::svgr!(
            <svg xmlns="http://www.w3.org/2000/svg" width={Self::WIDTH} height={Self::HEIGHT}>
                <rect x="0" y="0" width={Self::WIDTH} height={Self::HEIGHT} fill={fill} />
            </svg>
        )
    }
}

fn center_pixel(decoder: &mut FFmpegDecoder, frame: usize) -> [u8; 4] {
    unsafe {
        assert!(
            decoder.decode_up_to(frame as i64).unwrap(),
            "frame {frame} is missing from the rendered video"
        );

        let image = decoder
            .get_raw_frame()
            .convert_last_decoded_frame_into_svg_image(None)
            .unwrap();

        assert_eq!(image.width as usize, SmokeVideo::WIDTH);
        assert_eq!(image.height as usize, SmokeVideo::HEIGHT);

        let offset =
            ((image.height as usize / 2) * image.width as usize + image.width as usize / 2) * 4;
        image.data[offset..offset + 4].try_into().unwrap()
    }
}

fn assert_color_close(actual: [u8; 4], expected: [u8; 3], frame: usize) {
    // lossy yuv420p encoding shifts solid colors by a few units
    let close = actual[..3]
        .iter()
        .zip(expected)
        .all(|(a, e)| a.abs_diff(e) <= 24);

    assert!(
        close,
        "frame {frame}: expected ~{expected:?}, decoded {actual:?}"
    );
}

/// Renders `SmokeVideo` and checks the encoded frame rate and that the color switches
/// at `SWITCH_AT_SECONDS` when counted in that frame rate.
fn render_and_check(name: &str, override_fps: Option<usize>) {
    let expected_fps = override_fps.unwrap_or(FPS);
    let dir = std::env::temp_dir().join(format!("fframes-{name}-{}", std::process::id()));
    std::fs::create_dir_all(&dir).unwrap();
    let output = dir.join("smoke.mp4");

    fframes::render(
        &output,
        &SmokeVideo,
        fframes::cpu::CpuRenderingBackend {
            concurrency: 2,
            ..Default::default()
        },
        &RenderOptions {
            logger: fframes::fframes_logger::FFramesLoggerVariant::Silent,
            tmp_files_directory: Some(&dir.join("chunks")),
            override_fps,
            ..Default::default()
        },
    )
    .unwrap();

    assert!(std::fs::metadata(&output).unwrap().len() > 0);

    let mut decoder = unsafe { FFmpegDecoder::new(&output, expected_fps, 1) }.unwrap();
    assert_eq!(
        decoder.get_raw_frame().get_stream_fps(),
        expected_fps as f32
    );

    let after_switch = (SWITCH_AT_SECONDS * expected_fps as f32) as usize + 2;
    assert_color_close(center_pixel(&mut decoder, 0), [255, 0, 0], 0);
    assert_color_close(
        center_pixel(&mut decoder, after_switch),
        [0, 0, 255],
        after_switch,
    );

    let frames = (SECONDS * expected_fps as f32) as usize;
    assert_color_close(
        center_pixel(&mut decoder, frames - 1),
        [0, 0, 255],
        frames - 1,
    );
    assert!(
        !unsafe { decoder.decode_up_to(frames as i64) }.unwrap(),
        "the video has more than {frames} frames"
    );
    drop(decoder);

    let _ = std::fs::remove_dir_all(&dir);
}

#[test]
fn renders_and_decodes_video() {
    render_and_check("smoke", None);
}

#[test]
fn override_fps_sets_the_rendered_frame_rate() {
    render_and_check("override-fps", Some(FPS / 2));
}