Skip to content

armgit / Cap

Public Imported from GitHub
0

Open source Loom alternative. Beautiful, shareable screen recordings.

Use frame format instead of camera format as source of truth (#134)

Brendan Allan42485c7f008b
diff --git a/Cargo.lock b/Cargo.lock
index e2eafa68f..8373f524a 100644
--- a/Cargo.lock
+++ b/Cargo.lock
@@ -3760,7 +3760,7 @@ checksum = "72ef4a56884ca558e5ddb05a1d1e7e1bfd9a68d9ed024c21704cc98872dae1bb"
[[package]]
name = "nokhwa"
version = "0.10.3"
-source = "git+https://github.com/filleduchaos/nokhwa?rev=408354f6bcdd#408354f6bcddb00bfc2cb4f9e62ce3854e438ade"
+source = "git+https://github.com/capsoftware/nokhwa?rev=3f7e33a7f49c#3f7e33a7f49c037690b11a2e2463777c36389350"
dependencies = [
"flume 0.10.14",
"image 0.24.9",
@@ -3774,7 +3774,7 @@ dependencies = [
[[package]]
name = "nokhwa-bindings-macos"
version = "0.2.0"
-source = "git+https://github.com/filleduchaos/nokhwa?rev=408354f6bcdd#408354f6bcddb00bfc2cb4f9e62ce3854e438ade"
+source = "git+https://github.com/capsoftware/nokhwa?rev=3f7e33a7f49c#3f7e33a7f49c037690b11a2e2463777c36389350"
dependencies = [
"block",
"cocoa-foundation 0.1.2",
@@ -3789,7 +3789,7 @@ dependencies = [
[[package]]
name = "nokhwa-core"
version = "0.1.0"
-source = "git+https://github.com/filleduchaos/nokhwa?rev=408354f6bcdd#408354f6bcddb00bfc2cb4f9e62ce3854e438ade"
+source = "git+https://github.com/capsoftware/nokhwa?rev=3f7e33a7f49c#3f7e33a7f49c037690b11a2e2463777c36389350"
dependencies = [
"bytes",
"image 0.24.9",
diff --git a/apps/desktop/src/routes/(window-chrome)/index.tsx b/apps/desktop/src/routes/(window-chrome)/index.tsx
index 1f94382e2..b12e6b308 100644
--- a/apps/desktop/src/routes/(window-chrome)/index.tsx
+++ b/apps/desktop/src/routes/(window-chrome)/index.tsx
@@ -1,4 +1,4 @@
-import { Button, SwitchTab } from "@cap/ui-solid";
+import { Button } from "@cap/ui-solid";
import { Select as KSelect } from "@kobalte/core/select";
import { cache, createAsync, redirect, useNavigate } from "@solidjs/router";
import { createMutation, createQuery } from "@tanstack/solid-query";
diff --git a/crates/media/Cargo.toml b/crates/media/Cargo.toml
index cb2946239..ff99bdc43 100644
--- a/crates/media/Cargo.toml
+++ b/crates/media/Cargo.toml
@@ -13,7 +13,7 @@ ffmpeg.workspace = true
ffmpeg-sys-next.workspace = true
flume = "0.11.0"
indexmap = "2.5.0"
-nokhwa = { git = "https://github.com/filleduchaos/nokhwa", rev = "408354f6bcdd", features = [
+nokhwa = { git = "https://github.com/capsoftware/nokhwa", rev = "3f7e33a7f49c", features = [
"input-avfoundation",
"serialize",
] }
@@ -31,4 +31,4 @@ core-foundation = "0.10.0"
objc = "0.2.7"
objc-foundation = "0.1.1"
objc2-foundation = { version = "0.2.2", features = ["NSValue"] }
-nokhwa-bindings-macos = { git = "https://github.com/filleduchaos/nokhwa", rev = "408354f6bcdd" }
+nokhwa-bindings-macos = { git = "https://github.com/capsoftware/nokhwa", rev = "3f7e33a7f49c" }
diff --git a/crates/media/src/data.rs b/crates/media/src/data.rs
index 470d087f1..5099319bf 100644
--- a/crates/media/src/data.rs
+++ b/crates/media/src/data.rs
@@ -22,7 +22,7 @@ pub enum RawAudioFormat {
pub enum RawVideoFormat {
Bgra,
Mjpeg,
- Yuyv,
+ Uyvy,
RawRgb,
Nv12,
Gray,
@@ -112,7 +112,7 @@ impl VideoInfo {
pixel_format: match format {
RawVideoFormat::Bgra => Pixel::BGRA,
RawVideoFormat::Mjpeg => Pixel::YUVJ422P,
- RawVideoFormat::Yuyv => Pixel::UYVY422,
+ RawVideoFormat::Uyvy => Pixel::UYVY422,
RawVideoFormat::RawRgb => Pixel::RGB24,
RawVideoFormat::Nv12 => Pixel::NV12,
RawVideoFormat::Gray => Pixel::GRAY8,
diff --git a/crates/media/src/feeds/camera.rs b/crates/media/src/feeds/camera.rs
index 2e5354d2f..f19f57d2b 100644
--- a/crates/media/src/feeds/camera.rs
+++ b/crates/media/src/feeds/camera.rs
@@ -2,6 +2,7 @@ use ffmpeg::software::scaling;
use flume::{Receiver, Sender, TryRecvError};
use nokhwa::{pixel_format::*, utils::*, Camera};
use std::{
+ io::Write,
thread::{self, JoinHandle},
time::Instant,
};
@@ -134,18 +135,27 @@ fn find_camera(selected_camera: &String) -> Result<CameraInfo, MediaError> {
}
fn create_camera(info: &CameraInfo) -> Result<Camera, MediaError> {
+ dbg!(info);
+
// TODO: Make selected format more flexible
// let format = RequestedFormat::new::<RgbAFormat>(RequestedFormatType::AbsoluteHighestResolution);
- let format =
- RequestedFormat::new::<RgbAFormat>(RequestedFormatType::Closest(CameraFormat::new(
- Resolution {
- width_x: 1920,
- height_y: 1080,
- },
- FrameFormat::YUYV,
- 30,
- )));
+ let format = RequestedFormat::new::<RgbAFormat>(RequestedFormatType::ClosestIgnoringFormat {
+ resolution: Resolution {
+ width_x: 1920,
+ height_y: 1080,
+ },
+ frame_rate: 30,
+ });
+
let index = info.index().clone();
+
+ #[cfg(target_os = "macos")]
+ {
+ let device = nokhwa_bindings_macos::AVCaptureDevice::new(&index).unwrap();
+ let formats = device.supported_formats()?;
+ dbg!(formats);
+ }
+
Ok(Camera::new(index, format)?)
}
@@ -153,6 +163,8 @@ fn find_and_create_camera(selected_camera: &String) -> Result<(CameraInfo, Camer
let info = find_camera(selected_camera)?;
let camera = create_camera(&info)?;
+ dbg!(camera.camera_format());
+
Ok((info, camera))
}
@@ -193,8 +205,6 @@ fn run_camera_feed(
}
};
- let mut converter = FrameConverter::build(camera.camera_format());
-
if let Err(error) = camera.open_stream() {
error!("Failed to open camera stream: {:?}", error);
ready_signal.send(Err(error.into())).unwrap();
@@ -202,8 +212,9 @@ fn run_camera_feed(
}
info!("Camera stream opened successfully");
- ready_signal.send(Ok(converter.video_info)).unwrap();
- println!("Launched camera feed");
+
+ let mut converter = None;
+ let mut ready_signal = Some(ready_signal);
loop {
match control.try_recv() {
@@ -244,7 +255,7 @@ fn run_camera_feed(
.send(Ok((new_info, new_converter.video_info)))
.unwrap();
camera = new_camera;
- converter = new_converter;
+ // converter = new_converter;
} else {
eprintln!(
"Unable to switch to {camera_name}. Still using previous camera"
@@ -262,6 +273,22 @@ fn run_camera_feed(
// Actual data capture
match camera.frame() {
Ok(raw_buffer) => {
+ let converter = converter.get_or_insert_with(|| {
+ let mut format = camera.camera_format();
+ format.set_format(raw_buffer.source_frame_format());
+ let converter = FrameConverter::build(format);
+ if let Some(ready_signal) = ready_signal.take() {
+ ready_signal.send(Ok(converter.video_info)).unwrap();
+ }
+ converter
+ });
+
+ if converter.format != raw_buffer.source_frame_format() {
+ let mut format = camera.camera_format();
+ format.set_format(raw_buffer.source_frame_format());
+ *converter = FrameConverter::build(format);
+ }
+
// TODO: Merge fix in nokhwa lib to use presentation timestamps from the system, like scap does
let captured_at = Instant::now();
@@ -300,13 +327,14 @@ fn run_camera_feed(
struct FrameConverter {
video_info: VideoInfo,
context: scaling::Context,
+ pub format: FrameFormat,
}
impl FrameConverter {
fn build(camera_format: CameraFormat) -> Self {
let format = match camera_format.format() {
FrameFormat::MJPEG => RawVideoFormat::Mjpeg,
- FrameFormat::YUYV => RawVideoFormat::Yuyv,
+ FrameFormat::YUYV => RawVideoFormat::Uyvy,
FrameFormat::NV12 => RawVideoFormat::Nv12,
FrameFormat::GRAY => RawVideoFormat::Gray,
FrameFormat::RAWRGB => RawVideoFormat::RawRgb,
@@ -330,6 +358,7 @@ impl FrameConverter {
Self {
video_info,
context,
+ format: camera_format.format(),
}
}