Open source Loom alternative. Beautiful, shareable screen recordings.
Rust appcapcossloommacnextjsnextjs15open-sourceossreactrecordrustscreen-capturescreen-recorderscreenshotsolidjstauritauri-apptypescriptvite
fix(enc-ffmpeg): keep recording audio benchmark macOS-only and assert the opened encoder
Hridya Agrawalaf0c6bd2b301
diff --git a/crates/enc-ffmpeg/Cargo.toml b/crates/enc-ffmpeg/Cargo.tomlindex d5180ef5a..3cadf132d 100644--- a/crates/enc-ffmpeg/Cargo.toml+++ b/crates/enc-ffmpeg/Cargo.toml@@ -19,8 +19,10 @@ cap-frame-converter = { path = "../frame-converter" }windows = { workspace = true, features = ["Win32_Storage_FileSystem"] }[dev-dependencies]-libc = "0.2"tempfile = "3.20.0"+[target.'cfg(target_os = "macos")'.dev-dependencies]+libc = "0.2"+[lints]workspace = truediff --git a/crates/enc-ffmpeg/examples/recording-audio-benchmark.rs b/crates/enc-ffmpeg/examples/recording-audio-benchmark.rsindex ecd0a0d7b..3fb36b6d0 100644--- a/crates/enc-ffmpeg/examples/recording-audio-benchmark.rs+++ b/crates/enc-ffmpeg/examples/recording-audio-benchmark.rs@@ -6,196 +6,219 @@//! With no input file a deterministic 60 s signal (220 Hz tone + filtered//! noise) is synthesized; any ffmpeg-decodable file is decoded to 48 kHz mono.-use std::{- path::{Path, PathBuf},- time::{Duration, Instant},-};+#[cfg(target_os = "macos")]+mod benchmark {+ use std::{+ path::{Path, PathBuf},+ time::{Duration, Instant},+ };-use cap_enc_ffmpeg::fragmented_audio::FragmentedAudioFile;-use cap_media_info::AudioInfo;-use ffmpeg::{- ChannelLayout,- codec::context,- format::{self, Sample, sample::Type},- frame, media, software,-};+ use cap_enc_ffmpeg::fragmented_audio::FragmentedAudioFile;+ use cap_media_info::AudioInfo;+ use ffmpeg::{+ ChannelLayout,+ codec::context,+ format::{self, Sample, sample::Type},+ frame, media, software,+ };-const RATE: u32 = 48_000;-const FRAME_SAMPLES: usize = 960;+ const RATE: u32 = 48_000;+ const FRAME_SAMPLES: usize = 960;-fn cpu_secs() -> f64 {- let mut usage = std::mem::MaybeUninit::<libc::rusage>::zeroed();- unsafe { libc::getrusage(libc::RUSAGE_SELF, usage.as_mut_ptr()) };- let usage = unsafe { usage.assume_init() };- let secs = |time: libc::timeval| time.tv_sec as f64 + time.tv_usec as f64 / 1_000_000.0;- secs(usage.ru_utime) + secs(usage.ru_stime)-}+ fn cpu_secs() -> f64 {+ let mut usage = std::mem::MaybeUninit::<libc::rusage>::zeroed();+ unsafe { libc::getrusage(libc::RUSAGE_SELF, usage.as_mut_ptr()) };+ let usage = unsafe { usage.assume_init() };+ let secs = |time: libc::timeval| time.tv_sec as f64 + time.tv_usec as f64 / 1_000_000.0;+ secs(usage.ru_utime) + secs(usage.ru_stime)+ }-fn decode_to_mono(path: &Path) -> Vec<f32> {- let mut input = format::input(path).unwrap();- let stream = input.streams().best(media::Type::Audio).unwrap();- let stream_index = stream.index();- let mut decoder = context::Context::from_parameters(stream.parameters())- .unwrap()- .decoder()- .audio()+ fn decode_to_mono(path: &Path) -> Vec<f32> {+ let mut input = format::input(path).unwrap();+ let stream = input.streams().best(media::Type::Audio).unwrap();+ let stream_index = stream.index();+ let mut decoder = context::Context::from_parameters(stream.parameters())+ .unwrap()+ .decoder()+ .audio()+ .unwrap();+ let input_layout = if decoder.channel_layout().is_empty() {+ AudioInfo::new_raw(decoder.format(), decoder.rate(), decoder.channels())+ .channel_layout()+ } else {+ decoder.channel_layout()+ };+ let mut resampler = software::resampler(+ (decoder.format(), input_layout, decoder.rate()),+ (Sample::F32(Type::Packed), ChannelLayout::MONO, RATE),+ ).unwrap();- let input_layout = if decoder.channel_layout().is_empty() {- AudioInfo::new_raw(decoder.format(), decoder.rate(), decoder.channels()).channel_layout()- } else {- decoder.channel_layout()- };- let mut resampler = software::resampler(- (decoder.format(), input_layout, decoder.rate()),- (Sample::F32(Type::Packed), ChannelLayout::MONO, RATE),- )- .unwrap();- let mut samples = Vec::new();- let mut decoded = frame::Audio::empty();- let mut resampled = frame::Audio::empty();- for (stream, packet) in input.packets() {- if stream.index() != stream_index {- continue;+ let mut samples = Vec::new();+ let mut decoded = frame::Audio::empty();+ let mut resampled = frame::Audio::empty();+ for (stream, packet) in input.packets() {+ if stream.index() != stream_index {+ continue;+ }+ decoder.send_packet(&packet).unwrap();+ while decoder.receive_frame(&mut decoded).is_ok() {+ decoded.set_channel_layout(input_layout);+ resampler.run(&decoded, &mut resampled).unwrap();+ samples.extend_from_slice(resampled.plane::<f32>(0));+ }}- decoder.send_packet(&packet).unwrap();+ decoder.send_eof().unwrap();while decoder.receive_frame(&mut decoded).is_ok() {decoded.set_channel_layout(input_layout);resampler.run(&decoded, &mut resampled).unwrap();samples.extend_from_slice(resampled.plane::<f32>(0));}- }- decoder.send_eof().unwrap();- while decoder.receive_frame(&mut decoded).is_ok() {- decoded.set_channel_layout(input_layout);- resampler.run(&decoded, &mut resampled).unwrap();- samples.extend_from_slice(resampled.plane::<f32>(0));- }- while resampler.delay().is_some() {- resampler.flush(&mut resampled).unwrap();- if resampled.samples() == 0 {- break;+ while resampler.delay().is_some() {+ resampler.flush(&mut resampled).unwrap();+ if resampled.samples() == 0 {+ break;+ }+ samples.extend_from_slice(resampled.plane::<f32>(0));}- samples.extend_from_slice(resampled.plane::<f32>(0));+ samples}- samples-}-fn synthetic(seconds: usize) -> Vec<f32> {- let mut state = 0x9e37_79b9_u32;- let mut low = 0.0_f32;- (0..seconds * RATE as usize)- .map(|index| {- state ^= state << 13;- state ^= state >> 17;- state ^= state << 5;- let white = state as f32 / u32::MAX as f32 - 0.5;- low = 0.98f32.mul_add(low, 0.02 * white);- let time = index as f32 / RATE as f32;- 0.15 * (std::f32::consts::TAU * 220.0 * time).sin() + 0.4 * low- })- .collect()-}+ fn synthetic(seconds: usize) -> Vec<f32> {+ let mut state = 0x9e37_79b9_u32;+ let mut low = 0.0_f32;+ (0..seconds * RATE as usize)+ .map(|index| {+ state ^= state << 13;+ state ^= state >> 17;+ state ^= state << 5;+ let white = state as f32 / u32::MAX as f32 - 0.5;+ low = 0.98f32.mul_add(low, 0.02 * white);+ let time = index as f32 / RATE as f32;+ 0.15 * (std::f32::consts::TAU * 220.0 * time).sin() + 0.4 * low+ })+ .collect()+ }-fn encode_run(- path: &Path,- channels: u16,- mono: &[f32],- prefer_audiotoolbox: bool,-) -> (f64, f64, u64) {- let info = AudioInfo::new_raw(Sample::F32(Type::Packed), RATE, channels);- let cpu_started = cpu_secs();- let wall_started = Instant::now();- let mut file =- FragmentedAudioFile::init_with_encoder_preference(path.into(), info, prefer_audiotoolbox)- .unwrap();- for (index, chunk) in mono.chunks(FRAME_SAMPLES).enumerate() {- let mut frame = frame::Audio::new(- Sample::F32(Type::Packed),- chunk.len(),- info.channel_layout(),- );- frame.set_rate(RATE);- for (i, bytes) in frame.data_mut(0)[..chunk.len() * usize::from(channels) * 4]- .chunks_exact_mut(4)- .enumerate()- {- bytes.copy_from_slice(&chunk[i / usize::from(channels)].to_le_bytes());- }- file.queue_frame(- frame,- Duration::from_secs_f64((index * FRAME_SAMPLES) as f64 / f64::from(RATE)),+ fn encode_run(+ path: &Path,+ channels: u16,+ mono: &[f32],+ prefer_audiotoolbox: bool,+ ) -> (f64, f64, u64) {+ let info = AudioInfo::new_raw(Sample::F32(Type::Packed), RATE, channels);+ let cpu_started = cpu_secs();+ let wall_started = Instant::now();+ let mut file = FragmentedAudioFile::init_with_encoder_preference(+ path.into(),+ info,+ prefer_audiotoolbox,).unwrap();+ let expected = if prefer_audiotoolbox { "aac_at" } else { "aac" };+ assert_eq!(+ file.encoder().codec_name().as_deref(),+ Some(expected),+ "requested {expected} but the encoder opened {:?} (aac_at init fell back)",+ file.encoder().codec_name()+ );+ for (index, chunk) in mono.chunks(FRAME_SAMPLES).enumerate() {+ let mut frame = frame::Audio::new(+ Sample::F32(Type::Packed),+ chunk.len(),+ info.channel_layout(),+ );+ frame.set_rate(RATE);+ for (i, bytes) in frame.data_mut(0)[..chunk.len() * usize::from(channels) * 4]+ .chunks_exact_mut(4)+ .enumerate()+ {+ bytes.copy_from_slice(&chunk[i / usize::from(channels)].to_le_bytes());+ }+ file.queue_frame(+ frame,+ Duration::from_secs_f64((index * FRAME_SAMPLES) as f64 / f64::from(RATE)),+ )+ .unwrap();+ }+ file.finish().unwrap().unwrap();+ let wall = wall_started.elapsed().as_secs_f64();+ let cpu = cpu_secs() - cpu_started;+ let bytes = std::fs::metadata(path).map(|meta| meta.len()).unwrap_or(0);+ (cpu, wall, bytes)}- file.finish().unwrap().unwrap();- let wall = wall_started.elapsed().as_secs_f64();- let cpu = cpu_secs() - cpu_started;- let bytes = std::fs::metadata(path).map(|meta| meta.len()).unwrap_or(0);- (cpu, wall, bytes)-}-fn median(values: &[f64]) -> f64 {- let mut sorted = values.to_vec();- sorted.sort_by(f64::total_cmp);- sorted[sorted.len() / 2]-}+ fn median(values: &[f64]) -> f64 {+ let mut sorted = values.to_vec();+ sorted.sort_by(f64::total_cmp);+ sorted[sorted.len() / 2]+ }-fn main() {- let mut arguments = std::env::args().skip(1);- let input = arguments.next().map(PathBuf::from);- let rounds = arguments- .next()- .and_then(|value| value.parse::<usize>().ok())- .unwrap_or(5)- .max(1);+ pub fn run() {+ let mut arguments = std::env::args().skip(1);+ let input = arguments.next().map(PathBuf::from);+ let rounds = arguments+ .next()+ .and_then(|value| value.parse::<usize>().ok())+ .unwrap_or(5)+ .max(1);- let mono = input- .as_deref()- .map(decode_to_mono)- .unwrap_or_else(|| synthetic(60));- let audio_secs = mono.len() as f64 / f64::from(RATE);- println!(- "input: {} ({audio_secs:.1}s of 48 kHz mono), {rounds} rounds",- input- .map(|p| p.display().to_string())- .unwrap_or_else(|| "synthetic".to_string())- );+ let mono = input+ .as_deref()+ .map(decode_to_mono)+ .unwrap_or_else(|| synthetic(60));+ let audio_secs = mono.len() as f64 / f64::from(RATE);+ println!(+ "input: {} ({audio_secs:.1}s of 48 kHz mono), {rounds} rounds",+ input+ .map(|p| p.display().to_string())+ .unwrap_or_else(|| "synthetic".to_string())+ );- let dir = std::env::temp_dir().join("cap-recording-audio-benchmark");- std::fs::create_dir_all(&dir).unwrap();+ let dir = std::env::temp_dir().join("cap-recording-audio-benchmark");+ std::fs::create_dir_all(&dir).unwrap();- let configs = [- ("aac", 1u16, false),- ("aac_at", 1, true),- ("aac", 2, false),- ("aac_at", 2, true),- ];- let mut runs = vec![(Vec::<f64>::new(), Vec::<f64>::new(), 0u64); configs.len()];- for round in 0..rounds {- for (index, &(name, channels, prefer)) in configs.iter().enumerate() {- let path = dir.join(format!("{name}-{channels}ch-{round}.m4a"));- let _ = std::fs::remove_file(&path);- let (cpu, wall, bytes) = encode_run(&path, channels, &mono, prefer);- runs[index].0.push(cpu);- runs[index].1.push(wall);- runs[index].2 = bytes;+ let configs = [+ ("aac", 1u16, false),+ ("aac_at", 1, true),+ ("aac", 2, false),+ ("aac_at", 2, true),+ ];+ let mut runs = vec![(Vec::<f64>::new(), Vec::<f64>::new(), 0u64); configs.len()];+ for round in 0..rounds {+ for (index, &(name, channels, prefer)) in configs.iter().enumerate() {+ let path = dir.join(format!("{name}-{channels}ch-{round}.m4a"));+ let _ = std::fs::remove_file(&path);+ let (cpu, wall, bytes) = encode_run(&path, channels, &mono, prefer);+ runs[index].0.push(cpu);+ runs[index].1.push(wall);+ runs[index].2 = bytes;+ }}- }- println!("encoder ch cpu_ms/s wall_s kbps");- for (index, &(name, channels, _)) in configs.iter().enumerate() {- let (cpus, walls, bytes) = &runs[index];- let cpu_ms = median(cpus) * 1_000.0 / audio_secs;- let kbps = *bytes as f64 * 8.0 / audio_secs / 1_000.0;- println!(- "{name:<8} {channels} {cpu_ms:>8.1} {:>6.2} {kbps:>6.1}",- median(walls)- );- }- for channels in [1usize, 2] {- let aac = median(&runs[channels * 2 - 2].0);- let at = median(&runs[channels * 2 - 1].0);- println!("cpu ratio aac_at/aac ({channels}ch): {:.2}", at / aac);+ println!("encoder ch cpu_ms/s wall_s kbps");+ for (index, &(name, channels, _)) in configs.iter().enumerate() {+ let (cpus, walls, bytes) = &runs[index];+ let cpu_ms = median(cpus) * 1_000.0 / audio_secs;+ let kbps = *bytes as f64 * 8.0 / audio_secs / 1_000.0;+ println!(+ "{name:<8} {channels} {cpu_ms:>8.1} {:>6.2} {kbps:>6.1}",+ median(walls)+ );+ }+ for channels in [1usize, 2] {+ let aac = median(&runs[channels * 2 - 2].0);+ let at = median(&runs[channels * 2 - 1].0);+ println!("cpu ratio aac_at/aac ({channels}ch): {:.2}", at / aac);+ }}}++fn main() {+ #[cfg(target_os = "macos")]+ benchmark::run();+ #[cfg(not(target_os = "macos"))]+ eprintln!(+ "recording-audio-benchmark compares FFmpeg aac with AudioToolbox aac_at and only runs on macOS"+ );+}diff --git a/crates/enc-ffmpeg/src/mux/fragmented_audio.rs b/crates/enc-ffmpeg/src/mux/fragmented_audio.rsindex 900d0b7fa..cf865c77c 100644--- a/crates/enc-ffmpeg/src/mux/fragmented_audio.rs+++ b/crates/enc-ffmpeg/src/mux/fragmented_audio.rs@@ -347,7 +347,7 @@ mod publication_tests {}}-#[cfg(test)]+#[cfg(all(test, target_os = "macos"))]mod recording_encoder_tests {use super::*;use ffmpeg::{@@ -391,6 +391,12 @@ mod recording_encoder_tests {prefer_audiotoolbox,).unwrap();+ assert_eq!(+ file.encoder().codec_name().as_deref(),+ Some(if prefer_audiotoolbox { "aac_at" } else { "aac" }),+ "requested AudioToolbox={prefer_audiotoolbox}, but opened {:?}",+ file.encoder().codec_name()+ );for &(start, len) in send_ranges {let mut position = start;@@ -438,6 +444,12 @@ mod recording_encoder_tests {prefer_audiotoolbox,).unwrap();+ assert_eq!(+ file.encoder().codec_name().as_deref(),+ Some(if prefer_audiotoolbox { "aac_at" } else { "aac" }),+ "requested AudioToolbox={prefer_audiotoolbox}, but opened {:?}",+ file.encoder().codec_name()+ );let mut position = 0;let mut frame_index = 0;@@ -609,7 +621,6 @@ mod recording_encoder_tests {);}- #[cfg(target_os = "macos")]#[test]fn recording_audio_uses_audiotoolbox_on_macos() {if std::env::var_os("CAP_RECORDING_FFMPEG_AAC").is_some_and(|v| !v.is_empty()) {@@ -621,7 +632,6 @@ mod recording_encoder_tests {assert_eq!(file.encoder().codec_name().as_deref(), Some("aac_at"));}- #[cfg(target_os = "macos")]#[test]fn recording_audio_silence_stays_small() {let timeline = vec![0.0_f32; 480_000];