From 2ca852e00c4fbe6f002ad1c4efb630c4d90e8a27 Mon Sep 17 00:00:00 2001 From: Richie McIlroy <33632126+richiemcilroy@users.noreply.github.com> Date: Tue, 14 Jan 2025 13:00:38 +0000 Subject: [PATCH] Revert "fix: Windows/Mac compatibility for webcam" This reverts commit 58fa7a891bee91cb6e42d5ac5ad717bc1c6610fd. --- crates/media/src/feeds/camera.rs | 92 ++++++++++++-------------------- package.json | 2 +- 2 files changed, 34 insertions(+), 60 deletions(-) diff --git a/crates/media/src/feeds/camera.rs b/crates/media/src/feeds/camera.rs index d9f23a3c..f9be1bac 100644 --- a/crates/media/src/feeds/camera.rs +++ b/crates/media/src/feeds/camera.rs @@ -356,23 +356,15 @@ impl FrameConverter { camera_format.frame_rate(), ); - // Create FFmpeg converter with platform-specific pixel format - let initial_pixel_format = if camera_format.format() == FrameFormat::YUYV { - #[cfg(target_os = "macos")] - { - ffmpeg::format::Pixel::UYVY422 - } - #[cfg(not(target_os = "macos"))] - { - ffmpeg::format::Pixel::YUYV422 - } - } else { - video_info.pixel_format - }; - + // Create FFmpeg converter let context = ffmpeg::software::converter( (video_info.width, video_info.height), - initial_pixel_format, + if camera_format.format() == FrameFormat::YUYV { + // Try YUYV422 first, if that doesn't work the converter will be recreated with UYVY422 + ffmpeg::format::Pixel::YUYV422 + } else { + video_info.pixel_format + }, ffmpeg::format::Pixel::RGBA, ) .unwrap(); @@ -390,55 +382,37 @@ impl FrameConverter { match self.format { FrameFormat::YUYV => { - // Use platform-specific pixel format - #[cfg(target_os = "macos")] - let pixel_format = ffmpeg::format::Pixel::UYVY422; - #[cfg(not(target_os = "macos"))] - let pixel_format = ffmpeg::format::Pixel::YUYV422; - - // Create input frame with platform-specific format - let mut input_frame = - FFVideo::new(pixel_format, resolution.width(), resolution.height()); - - let stride = resolution.width() as usize * 2; - let src = buffer.buffer(); - - // Copy data line by line - { - let dst_stride = input_frame.stride(0); - let dst = input_frame.data_mut(0); - for y in 0..resolution.height() as usize { - let src_offset = y * stride; - let dst_offset = y * dst_stride; - dst[dst_offset..dst_offset + stride] - .copy_from_slice(&src[src_offset..src_offset + stride]); - } - } - - // Create output frame - let mut rgba_frame = FFVideo::new( - ffmpeg::format::Pixel::RGBA, - resolution.width(), - resolution.height(), - ); - - // Convert the frame - if self.context.run(&input_frame, &mut rgba_frame).is_ok() { - rgba_frame.data(0).to_vec() + // Try converting with YUYV422 first + let result = self.convert_with_ffmpeg(buffer, resolution); + + // If conversion fails (black/corrupted output), try with UYVY422 + if result.iter().all(|&x| x == 0) { + // Recreate context with UYVY422 + self.context = ffmpeg::software::converter( + (self.video_info.width, self.video_info.height), + ffmpeg::format::Pixel::UYVY422, + ffmpeg::format::Pixel::RGBA, + ) + .unwrap(); + + // Try converting again with UYVY422 + self.convert_with_ffmpeg(buffer, resolution) } else { - vec![0; (resolution.width() * resolution.height() * 4) as usize] + result } - } + }, _ => match &self.hw_converter { - Some(HwConverter::NV12(converter)) => converter.convert( - NV12Input::from_buffer( - buffer.buffer(), + Some(HwConverter::NV12(converter)) => { + converter.convert( + NV12Input::from_buffer( + buffer.buffer(), + resolution.width(), + resolution.height(), + ), resolution.width(), resolution.height(), - ), - resolution.width(), - resolution.height(), - ), + ) + } _ => self.convert_with_ffmpeg(buffer, resolution), }, } diff --git a/package.json b/package.json index 8067f864..33c09c85 100644 --- a/package.json +++ b/package.json @@ -2,7 +2,7 @@ "type": "module", "scripts": { "build": "dotenv -e .env -- turbo run build", - "dev": "(RUST_BACKTRACE=1 cd apps/web && docker compose up -d && cd ../..) && dotenv -e .env -- pnpm --dir packages/database db:generate && dotenv -e .env -- pnpm --dir packages/database db:push && dotenv -e .env -- turbo run dev --filter=!@cap/storybook --no-cache --concurrency 12", + "dev": "(cd apps/web && docker compose up -d && cd ../..) && dotenv -e .env -- pnpm --dir packages/database db:generate && dotenv -e .env -- pnpm --dir packages/database db:push && dotenv -e .env -- turbo run dev --filter=!@cap/storybook --no-cache --concurrency 12", "dev:manual": "dotenv -e .env -- turbo run dev --filter=!@cap/storybook --no-cache --concurrency 1", "lint": "turbo run lint", "format": "prettier --write \"**/*.{ts,tsx,md}\"",