Skip to content

armgit / Cap

Public Imported from GitHub
0

Open source Loom alternative. Beautiful, shareable screen recordings.

fix: Webcam recording issue

Richie McIlroyed8cd03314a4
diff --git a/crates/media/src/encoders/h264.rs b/crates/media/src/encoders/h264.rs
index a61ab5f7e..7e5231229 100644
--- a/crates/media/src/encoders/h264.rs
+++ b/crates/media/src/encoders/h264.rs
@@ -43,6 +43,11 @@ impl H264Encoder {
.any(|f| f == config.pixel_format)
{
let format = ffmpeg::format::Pixel::YUV420P;
+ tracing::debug!(
+ "Converting from {:?} to {:?} for H264 encoding",
+ config.pixel_format,
+ format
+ );
(
format,
Some(
@@ -51,7 +56,10 @@ impl H264Encoder {
config.pixel_format,
format,
)
- .expect("Failed to create frame converter"),
+ .map_err(|e| {
+ tracing::error!("Failed to create converter from {:?} to YUV420P: {:?}", config.pixel_format, e);
+ MediaError::Any("Failed to create frame converter")
+ })?,
),
)
} else {
@@ -93,14 +101,25 @@ impl H264Encoder {
pub fn queue_frame(&mut self, frame: FFVideo, output: &mut format::context::Output) {
let frame = if let Some(converter) = &mut self.converter {
let mut new_frame = FFVideo::empty();
- converter.run(&frame, &mut new_frame).unwrap();
- new_frame.set_pts(frame.pts());
- new_frame
+ match converter.run(&frame, &mut new_frame) {
+ Ok(_) => {
+ new_frame.set_pts(frame.pts());
+ new_frame
+ },
+ Err(e) => {
+ tracing::error!("Failed to convert frame: {:?} from format {:?} to YUV420P", e, frame.format());
+ // Return early as we can't process this frame
+ return;
+ }
+ }
} else {
frame
};
- self.encoder.send_frame(&frame).unwrap();
+ if let Err(e) = self.encoder.send_frame(&frame) {
+ tracing::error!("Failed to send frame to encoder: {:?}", e);
+ return;
+ }
self.process_frame(output);
}
@@ -112,12 +131,18 @@ impl H264Encoder {
self.config.time_base,
output.stream(self.stream_index).unwrap().time_base(),
);
- self.packet.write_interleaved(output).unwrap();
+ if let Err(e) = self.packet.write_interleaved(output) {
+ tracing::error!("Failed to write packet: {:?}", e);
+ break;
+ }
}
}
pub fn finish(&mut self, output: &mut format::context::Output) {
- self.encoder.send_eof().unwrap();
+ if let Err(e) = self.encoder.send_eof() {
+ tracing::error!("Failed to send EOF to encoder: {:?}", e);
+ return;
+ }
self.process_frame(output);
}
}
diff --git a/crates/media/src/encoders/mp4.rs b/crates/media/src/encoders/mp4.rs
index 83968087d..40bca0aa6 100644
--- a/crates/media/src/encoders/mp4.rs
+++ b/crates/media/src/encoders/mp4.rs
@@ -56,17 +56,19 @@ impl MP4File {
}
pub fn finish(&mut self) {
- println!("MP4Encoder: Finishing encoding");
+ tracing::info!("MP4Encoder: Finishing encoding");
self.video.finish(&mut self.output);
if let Some(audio) = &mut self.audio {
- println!("MP4Encoder: Flushing audio encoder");
+ tracing::info!("MP4Encoder: Flushing audio encoder");
audio.finish(&mut self.output);
}
- println!("MP4Encoder: Writing trailer");
- self.output.write_trailer().unwrap();
+ tracing::info!("MP4Encoder: Writing trailer");
+ if let Err(e) = self.output.write_trailer() {
+ tracing::error!("Failed to write MP4 trailer: {:?}", e);
+ }
}
}
diff --git a/crates/media/src/feeds/camera.rs b/crates/media/src/feeds/camera.rs
index 5ddb3a30e..2c1fd413f 100644
--- a/crates/media/src/feeds/camera.rs
+++ b/crates/media/src/feeds/camera.rs
@@ -229,7 +229,22 @@ fn run_camera_feed(
FrameFormat::RAWRGB => Pixel::RGB24,
FrameFormat::NV12 => Pixel::NV12,
FrameFormat::GRAY => Pixel::GRAY8,
- FrameFormat::YUYV => Pixel::UYVY422,
+ FrameFormat::YUYV => {
+ let pix_fmt = if cfg!(windows) {
+ tracing::debug!("Using YUYV422 format for Windows camera in buffer_to_ffvideo");
+ Pixel::YUYV422 // This is correct for Windows
+ } else {
+ // let bytes = buffer.buffer().len() as f32;
+ // let ratio = bytes / (buffer.resolution().x() * buffer.resolution().y()) as f32;
+ // if ratio == 2.0
+
+ // nokhwa merges yuvu420 and uyvy422 into the same format, we should probably distinguish them with the frame size
+ tracing::debug!("Using UYVY422 format for non-Windows camera in buffer_to_ffvideo");
+ Pixel::UYVY422
+ };
+
+ pix_fmt
+ },
},
camera_format.width(),
camera_format.height(),
@@ -237,6 +252,8 @@ fn run_camera_feed(
)
};
+ debug!("Camera video info: {:?}", video_info);
+
let mut senders: Vec<Sender<RawCameraFrame>> = vec![];
loop {
@@ -272,7 +289,16 @@ fn run_camera_feed(
FrameFormat::RAWRGB => Pixel::RGB24,
FrameFormat::NV12 => Pixel::NV12,
FrameFormat::GRAY => Pixel::GRAY8,
- FrameFormat::YUYV => Pixel::UYVY422,
+ FrameFormat::YUYV => {
+ let pix_fmt = if cfg!(windows) {
+ tracing::debug!("Using YUYV422 format for Windows camera in buffer_to_ffvideo");
+ Pixel::YUYV422 // This is correct for Windows
+ } else {
+ tracing::debug!("Using UYVY422 format for non-Windows camera in buffer_to_ffvideo");
+ Pixel::UYVY422
+ };
+ pix_fmt
+ },
},
camera_format.width(),
camera_format.height(),
@@ -370,13 +396,15 @@ fn buffer_to_ffvideo(buffer: nokhwa::Buffer) -> FFVideo {
FrameFormat::YUYV => {
// nokhwa moment
let pix_fmt = if cfg!(windows) {
- Pixel::YUYV422
+ tracing::debug!("Using YUYV422 format for Windows camera in buffer_to_ffvideo");
+ Pixel::YUYV422 // This is correct for Windows
} else {
// let bytes = buffer.buffer().len() as f32;
// let ratio = bytes / (buffer.resolution().x() * buffer.resolution().y()) as f32;
// if ratio == 2.0
// nokhwa merges yuvu420 and uyvy422 into the same format, we should probably distinguish them with the frame size
+ tracing::debug!("Using UYVY422 format for non-Windows camera in buffer_to_ffvideo");
Pixel::UYVY422
};