Skip to content

Commit 390d10d

Browse files
committed
Fix encoder error handling and output locking on Windows
Improves error propagation in MP4 export by mapping encoder errors to strings. Refactors Windows output pipeline to ensure proper locking of output streams and corrects the fallback logic for encoder selection. Also removes an unused import and minor variable cleanup in Windows screen capture source.
1 parent b7e4641 commit 390d10d

File tree

4 files changed

+32
-22
lines changed

4 files changed

+32
-22
lines changed

crates/export/src/mp4.rs

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -97,10 +97,12 @@ impl Mp4ExportSettings {
9797

9898
let mut encoded_frames = 0;
9999
while let Ok(frame) = frame_rx.recv() {
100-
encoder.queue_video_frame(
101-
frame.video,
102-
Duration::from_secs_f32(encoded_frames as f32 / fps as f32),
103-
);
100+
encoder
101+
.queue_video_frame(
102+
frame.video,
103+
Duration::from_secs_f32(encoded_frames as f32 / fps as f32),
104+
)
105+
.map_err(|err| err.to_string())?;
104106
encoded_frames += 1;
105107
if let Some(audio) = frame.audio {
106108
encoder.queue_audio_frame(audio);

crates/recording/src/instant_recording.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -224,6 +224,8 @@ async fn create_pipeline(
224224
mic_feed,
225225
output_path.clone(),
226226
output_resolution,
227+
#[cfg(windows)]
228+
crate::capture_pipeline::EncoderPreferences::new(),
227229
)
228230
.await?;
229231

crates/recording/src/output_pipeline/win.rs

Lines changed: 23 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -75,9 +75,7 @@ impl Muxer for WindowsMuxer {
7575
let encoder_preferences = &config.encoder_preferences;
7676

7777
let encoder = (|| {
78-
let mut output = output.lock().unwrap();
79-
80-
let fallback = |reason: Option<String>| {
78+
let mut fallback = |reason: Option<String>| {
8179
use tracing::{error, info};
8280

8381
encoder_preferences.force_software_only();
@@ -98,9 +96,11 @@ impl Muxer for WindowsMuxer {
9896
video_config.height
9997
};
10098

99+
let mut output = output.lock().unwrap();
100+
101101
cap_enc_ffmpeg::H264Encoder::builder(video_config)
102102
.with_output_size(fallback_width, fallback_height)
103-
.and_then(|builder| builder.build(&mut output))
103+
.and_then(|builder| builder.build(&mut *output))
104104
.map(either::Right)
105105
.map_err(|e| anyhow!("ScreenSoftwareEncoder/{e}"))
106106
};
@@ -117,18 +117,25 @@ impl Muxer for WindowsMuxer {
117117
config.frame_rate,
118118
config.bitrate_multiplier,
119119
) {
120-
Ok(encoder) => match cap_mediafoundation_ffmpeg::H264StreamMuxer::new(
121-
&mut output,
122-
cap_mediafoundation_ffmpeg::MuxerConfig {
123-
width: output_size.Width as u32,
124-
height: output_size.Height as u32,
125-
fps: config.frame_rate,
126-
bitrate: encoder.bitrate(),
127-
},
128-
) {
129-
Ok(muxer) => Ok(either::Left((encoder, muxer))),
130-
Err(err) => fallback(Some(err.to_string())),
131-
},
120+
Ok(encoder) => {
121+
let muxer = {
122+
let mut output = output.lock().unwrap();
123+
cap_mediafoundation_ffmpeg::H264StreamMuxer::new(
124+
&mut *output,
125+
cap_mediafoundation_ffmpeg::MuxerConfig {
126+
width: output_size.Width as u32,
127+
height: output_size.Height as u32,
128+
fps: config.frame_rate,
129+
bitrate: encoder.bitrate(),
130+
},
131+
)
132+
};
133+
134+
match muxer {
135+
Ok(muxer) => Ok(either::Left((encoder, muxer))),
136+
Err(err) => fallback(Some(err.to_string())),
137+
}
138+
}
132139
Err(err) => fallback(Some(err.to_string())),
133140
}
134141
})();

crates/recording/src/sources/screen_capture/windows.rs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,6 @@ use futures::{
1313
FutureExt, SinkExt, StreamExt,
1414
channel::{mpsc, oneshot},
1515
};
16-
use kameo::prelude::*;
1716
use scap_direct3d::StopCapturerError;
1817
use scap_ffmpeg::*;
1918
use scap_targets::{Display, DisplayId};
@@ -179,7 +178,7 @@ impl output_pipeline::VideoSource for VideoSource {
179178
where
180179
Self: Sized,
181180
{
182-
let (mut error_tx, mut error_rx) = mpsc::channel(1);
181+
let (error_tx, mut error_rx) = mpsc::channel(1);
183182
let (ctrl_tx, ctrl_rx) = std::sync::mpsc::sync_channel::<VideoControl>(1);
184183

185184
let tokio_rt = tokio::runtime::Handle::current();

0 commit comments

Comments
 (0)