Skip to content

armgit / Cap

Public Imported from GitHub
0

Open source Loom alternative. Beautiful, shareable screen recordings.

fix: handle high channel count audio devices (e.g., 34-channel interfaces)

Noel Jackson65daf4445989
diff --git a/crates/editor/src/audio.rs b/crates/editor/src/audio.rs
index 0376d6d82..d2cdbf8df 100644
--- a/crates/editor/src/audio.rs
+++ b/crates/editor/src/audio.rs
@@ -264,6 +264,9 @@ impl<T: FromSampleBytes> AudioPlaybackBuffer<T> {
const PROCESSING_SAMPLES_COUNT: u32 = 1024;
pub fn new(data: Vec<AudioSegment>, output_info: AudioInfo) -> Self {
+ // Clamp output info for FFmpeg compatibility (max 8 channels)
+ let output_info = output_info.for_ffmpeg_output();
+
info!(
sample_rate = output_info.sample_rate,
channels = output_info.channels,
@@ -391,6 +394,9 @@ pub struct AudioResampler {
impl AudioResampler {
pub fn new(output_info: AudioInfo) -> Result<Self, MediaError> {
+ // Clamp output info for FFmpeg compatibility (max 8 channels)
+ let output_info = output_info.for_ffmpeg_output();
+
let mut options = Dictionary::new();
options.set("filter_size", "128");
options.set("cutoff", "0.97");
@@ -460,6 +466,10 @@ impl<T: FromSampleBytes> PrerenderedAudioBuffer<T> {
output_info: AudioInfo,
duration_secs: f64,
) -> Self {
+ // Clamp output info for FFmpeg compatibility (max 8 channels)
+ // The resampler will produce audio with this channel count
+ let output_info = output_info.for_ffmpeg_output();
+
info!(
duration_secs = duration_secs,
sample_rate = output_info.sample_rate,
diff --git a/crates/editor/src/playback.rs b/crates/editor/src/playback.rs
index ffe9740fc..000f209c6 100644
--- a/crates/editor/src/playback.rs
+++ b/crates/editor/src/playback.rs
@@ -940,6 +940,13 @@ impl AudioPlayback {
}
};
+ // Clamp output info for FFmpeg compatibility (max 8 channels)
+ // This must match what AudioPlaybackBuffer will use internally
+ base_output_info = base_output_info.for_ffmpeg_output();
+
+ // Also update the stream config to match the clamped channels
+ config.channels = base_output_info.channels as u16;
+
let sample_rate = base_output_info.sample_rate;
let buffer_size = base_output_info.buffer_size;
let channels = base_output_info.channels;
@@ -1159,8 +1166,13 @@ impl AudioPlayback {
let mut output_info = AudioInfo::from_stream_config(&supported_config);
output_info.sample_format = output_info.sample_format.packed();
+ // Clamp output info for FFmpeg compatibility (max 8 channels)
+ output_info = output_info.for_ffmpeg_output();
+
+ let mut config = supported_config.config();
+ // Match stream config channels to clamped output info
+ config.channels = output_info.channels as u16;
- let config = supported_config.config();
let sample_rate = output_info.sample_rate;
let playhead = f64::from(start_frame_number) / f64::from(fps);
diff --git a/crates/media-info/src/lib.rs b/crates/media-info/src/lib.rs
index 635fd8d77..babe21528 100644
--- a/crates/media-info/src/lib.rs
+++ b/crates/media-info/src/lib.rs
@@ -26,6 +26,8 @@ pub enum AudioInfoError {
impl AudioInfo {
/// Maximum number of audio channels supported by FFmpeg channel layouts.
/// Matches the highest channel count in `channel_layout_raw` (7.1 surround = 8 channels).
+ /// Note: Actual device channel counts may exceed this; we store the real count
+ /// but clamp to this value when creating FFmpeg frames.
pub const MAX_AUDIO_CHANNELS: u16 = 8;
pub const fn new(
@@ -71,18 +73,16 @@ impl AudioInfo {
SupportedBufferSize::Unknown => 1024,
});
- let raw_channels = config.channels();
- let channels = if Self::channel_layout_raw(raw_channels).is_some() {
- raw_channels
- } else {
- raw_channels.clamp(1, Self::MAX_AUDIO_CHANNELS)
- };
+ // Store the actual channel count from the device, even if it exceeds
+ // MAX_AUDIO_CHANNELS. This ensures audio data parsing works correctly.
+ // The clamping to supported FFmpeg layouts happens in channel_layout()
+ // and wrap_frame_with_max_channels() when creating output frames.
+ let raw_channels = config.channels().max(1); // At least 1 channel
Self {
sample_format,
sample_rate: config.sample_rate().0,
- // we do this here and only here bc we know it's cpal-related
- channels: channels.into(),
+ channels: raw_channels.into(),
time_base: FFRational(1, 1_000_000),
buffer_size,
}
@@ -146,13 +146,15 @@ impl AudioInfo {
packed_data: &[u8],
max_channels: usize,
) -> frame::Audio {
- // Handle 0 channels by treating as mono to avoid division by zero
- // and unreachable code paths. This can happen with misconfigured audio devices.
- let effective_channels = self.channels.max(1);
- let out_channels = effective_channels.min(max_channels.max(1));
+ // Use actual channel count for parsing input data (at least 1 to avoid div by zero)
+ let input_channels = self.channels.max(1);
+ // Clamp output channels to both max_channels and MAX_AUDIO_CHANNELS for FFmpeg compatibility
+ let out_channels = input_channels
+ .min(max_channels.max(1))
+ .min(Self::MAX_AUDIO_CHANNELS as usize);
let sample_size = self.sample_size();
- let packed_sample_size = sample_size * effective_channels;
+ let packed_sample_size = sample_size * input_channels;
let samples = packed_data.len() / packed_sample_size;
let mut frame = frame::Audio::new(
@@ -162,10 +164,10 @@ impl AudioInfo {
);
frame.set_rate(self.sample_rate);
- if effective_channels == 1 || (frame.is_packed() && effective_channels <= out_channels) {
+ if input_channels == 1 || (frame.is_packed() && input_channels <= out_channels) {
// frame is allocated with parameters derived from packed_data, so this is safe
frame.data_mut(0)[0..packed_data.len()].copy_from_slice(packed_data);
- } else if frame.is_packed() && effective_channels > out_channels {
+ } else if frame.is_packed() && input_channels > out_channels {
for (chunk_index, packed_chunk) in packed_data.chunks(packed_sample_size).enumerate() {
let start = chunk_index * sample_size * out_channels;
@@ -208,6 +210,12 @@ impl AudioInfo {
this.channels = this.channels.min(channels as usize);
this
}
+
+ /// Returns a version of this AudioInfo with channels clamped for FFmpeg compatibility.
+ /// FFmpeg channel layouts only support up to 8 channels (7.1 surround).
+ pub fn for_ffmpeg_output(&self) -> Self {
+ self.with_max_channels(Self::MAX_AUDIO_CHANNELS)
+ }
}
pub enum RawVideoFormat {