Skip to content

armgit / Cap

Public Imported from GitHub
0

Open source Loom alternative. Beautiful, shareable screen recordings.

refactor(recording): extract FinishableEncoderState trait to deduplicate encoder finish logic

Richie McIlroybada32ff5014
diff --git a/crates/recording/src/output_pipeline/win_fragmented_m4s.rs b/crates/recording/src/output_pipeline/win_fragmented_m4s.rs
index b844d8f29..8e378668d 100644
--- a/crates/recording/src/output_pipeline/win_fragmented_m4s.rs
+++ b/crates/recording/src/output_pipeline/win_fragmented_m4s.rs
@@ -100,58 +100,68 @@ fn finish_encoder_thread(
wait_for_blocking_thread_finish(handle, Duration::from_secs(5), label)
}
-fn finish_segmented_encoder(
- mut state: EncoderState,
- timestamp: Duration,
- thread_label: &str,
- finish_label: &str,
-) -> anyhow::Result<()> {
- if let Err(error) = state.video_tx.send(None) {
- trace!("{thread_label} channel already closed during finish: {error}");
+trait FinishableEncoderState {
+ fn close_channel(&self, thread_label: &str);
+ fn take_handle(&mut self) -> Option<JoinHandle<anyhow::Result<()>>>;
+ fn finish_encoder(&self, timestamp: Duration, finish_label: &str) -> anyhow::Result<()>;
+}
+
+impl FinishableEncoderState for EncoderState {
+ fn close_channel(&self, thread_label: &str) {
+ if let Err(error) = self.video_tx.send(None) {
+ trace!("{thread_label} channel already closed during finish: {error}");
+ }
}
- let thread_result = state
- .encoder_handle
- .take()
- .map(|handle| finish_encoder_thread(handle, thread_label))
- .unwrap_or(BlockingThreadFinish::Clean);
+ fn take_handle(&mut self) -> Option<JoinHandle<anyhow::Result<()>>> {
+ self.encoder_handle.take()
+ }
- let thread_error = match thread_result {
- BlockingThreadFinish::Clean => None,
- BlockingThreadFinish::Failed(error) => Some(error),
- BlockingThreadFinish::TimedOut(error) => return Err(error),
- };
+ fn finish_encoder(&self, timestamp: Duration, finish_label: &str) -> anyhow::Result<()> {
+ match self.encoder.lock() {
+ Ok(mut encoder) => encoder
+ .finish_with_timestamp(timestamp)
+ .map_err(|error| anyhow!("{finish_label}: {error:#}")),
+ Err(_) => Err(anyhow!(
+ "{finish_label}: encoder mutex poisoned - recording may be corrupt or incomplete"
+ )),
+ }
+ }
+}
- let finalize_error = match state.encoder.lock() {
- Ok(mut encoder) => encoder
- .finish_with_timestamp(timestamp)
- .map_err(|error| anyhow!("{finish_label}: {error:#}"))
- .err(),
- Err(_) => Some(anyhow!(
- "{finish_label}: encoder mutex poisoned - recording may be corrupt or incomplete"
- )),
- };
+impl FinishableEncoderState for CameraEncoderState {
+ fn close_channel(&self, thread_label: &str) {
+ if let Err(error) = self.video_tx.send(None) {
+ trace!("{thread_label} channel already closed during finish: {error}");
+ }
+ }
- match (thread_error, finalize_error) {
- (None, None) => Ok(()),
- (Some(error), None) | (None, Some(error)) => Err(error),
- (Some(primary), Some(secondary)) => Err(combine_finish_errors(primary, secondary)),
+ fn take_handle(&mut self) -> Option<JoinHandle<anyhow::Result<()>>> {
+ self.encoder_handle.take()
+ }
+
+ fn finish_encoder(&self, timestamp: Duration, finish_label: &str) -> anyhow::Result<()> {
+ match self.encoder.lock() {
+ Ok(mut encoder) => encoder
+ .finish_with_timestamp(timestamp)
+ .map_err(|error| anyhow!("{finish_label}: {error:#}")),
+ Err(_) => Err(anyhow!(
+ "{finish_label}: encoder mutex poisoned - recording may be corrupt or incomplete"
+ )),
+ }
}
}
-fn finish_camera_segmented_encoder(
- mut state: CameraEncoderState,
+fn finish_segmented_encoder_impl(
+ state: &mut impl FinishableEncoderState,
timestamp: Duration,
thread_label: &str,
finish_label: &str,
) -> anyhow::Result<()> {
- if let Err(error) = state.video_tx.send(None) {
- trace!("{thread_label} channel already closed during finish: {error}");
- }
+ state.close_channel(thread_label);
let thread_result = state
- .encoder_handle
- .take()
+ .take_handle()
.map(|handle| finish_encoder_thread(handle, thread_label))
.unwrap_or(BlockingThreadFinish::Clean);
@@ -161,15 +171,7 @@ fn finish_camera_segmented_encoder(
BlockingThreadFinish::TimedOut(error) => return Err(error),
};
- let finalize_error = match state.encoder.lock() {
- Ok(mut encoder) => encoder
- .finish_with_timestamp(timestamp)
- .map_err(|error| anyhow!("{finish_label}: {error:#}"))
- .err(),
- Err(_) => Some(anyhow!(
- "{finish_label}: encoder mutex poisoned - recording may be corrupt or incomplete"
- )),
- };
+ let finalize_error = state.finish_encoder(timestamp, finish_label).err();
match (thread_error, finalize_error) {
(None, None) => Ok(()),
@@ -268,15 +270,15 @@ impl Muxer for WindowsFragmentedM4SMuxer {
}
fn finish(&mut self, timestamp: Duration) -> anyhow::Result<anyhow::Result<()>> {
- if let Some(mut state) = self.state.take() {
- if let Err(error) = finish_segmented_encoder(
- state,
+ if let Some(mut state) = self.state.take()
+ && let Err(error) = finish_segmented_encoder_impl(
+ &mut state,
timestamp,
"Windows M4S encoder",
"Failed to finish segmented encoder",
- ) {
- return Ok(Err(error));
- }
+ )
+ {
+ return Ok(Err(error));
}
Ok(Ok(()))
@@ -661,15 +663,15 @@ impl Muxer for WindowsFragmentedM4SCameraMuxer {
}
fn finish(&mut self, timestamp: Duration) -> anyhow::Result<anyhow::Result<()>> {
- if let Some(mut state) = self.state.take() {
- if let Err(error) = finish_camera_segmented_encoder(
- state,
+ if let Some(mut state) = self.state.take()
+ && let Err(error) = finish_segmented_encoder_impl(
+ &mut state,
timestamp,
"Windows M4S camera encoder",
"Failed to finish camera segmented encoder",
- ) {
- return Ok(Err(error));
- }
+ )
+ {
+ return Ok(Err(error));
}
Ok(Ok(()))