Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: Cursor implementation + HW accel #339

Merged
merged 18 commits into from
Feb 25, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 0 additions & 2 deletions .cargo/config.toml

This file was deleted.

1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -38,3 +38,4 @@ apps/storybook/storybook-static

*.tsbuildinfo
.cargo/config.toml
tauri.windows.conf.json
22 changes: 14 additions & 8 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 5 additions & 3 deletions apps/desktop/scripts/prepare.js
Original file line number Diff line number Diff line change
Expand Up @@ -33,9 +33,8 @@ async function semverToWIXCompatibleVersion(cargoFilePath) {
const numMatch = buildOrPrerelease.match(/\d+$/);
build = numMatch ? parseInt(numMatch[0]) : 0;
}
const wixVersion = `${major}.${minor}.${patch}${
build === 0 ? "" : `.${build}`
}`;
const wixVersion = `${major}.${minor}.${patch}${build === 0 ? "" : `.${build}`
}`;
if (wixVersion !== ver)
console.log(`Using wix-compatible version ${ver} --> ${wixVersion}`);
return wixVersion;
Expand Down Expand Up @@ -80,6 +79,9 @@ export async function createTauriPlatformConfigs(
baseConfig = {
...baseConfig,
bundle: {
resources: {
"../../../target/ffmpeg/bin/*.dll": "./",
},
windows: {
wix: {
version: await semverToWIXCompatibleVersion(
Expand Down
1 change: 1 addition & 0 deletions apps/desktop/src-tauri/tauri.conf.json
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@
},
"bundle": {
"active": true,
"createUpdaterArtifacts": true,
"targets": "all",
"icon": [
"icons/32x32.png",
Expand Down
1 change: 0 additions & 1 deletion apps/desktop/src-tauri/tauri.prod.conf.json
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@
}
},
"bundle": {
"createUpdaterArtifacts": true,
"macOS": {
"entitlements": "Entitlements.plist"
},
Expand Down
8 changes: 6 additions & 2 deletions apps/desktop/src-tauri/tauri.windows.conf.json
Original file line number Diff line number Diff line change
@@ -1,8 +1,12 @@
{
"$schema": "https://schema.tauri.app/config/2",
"bundle": {
"resources": {
"../../../target/ffmpeg/bin/*.dll": "./"
},
"windows": {
"wix": {
"version": "0.3.26"
}
}
}
}
}
2 changes: 1 addition & 1 deletion apps/desktop/src/routes/(window-chrome)/(main).tsx
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import {
useQueryClient,
} from "@tanstack/solid-query";
import { getVersion } from "@tauri-apps/api/app";
import { getCurrentWindow } from "@tauri-apps/api/window";
import { availableMonitors, getCurrentWindow } from "@tauri-apps/api/window";
import { LogicalSize } from "@tauri-apps/api/window";
import { cx } from "cva";
import {
Expand Down
12 changes: 6 additions & 6 deletions apps/desktop/src/routes/(window-chrome)/settings.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -96,12 +96,12 @@ export default function Settings(props: RouteSectionProps) {
name: "Feedback",
icon: IconLucideMessageSquarePlus,
},
{
type: "button",
name: "Live Chat",
icon: IconLucideMessageCircle,
onClick: handleLiveChat,
},
// {
// type: "button",
// name: "Live Chat",
// icon: IconLucideMessageCircle,
// onClick: handleLiveChat,
// },
{
type: "link",
href: "changelog",
Expand Down
14 changes: 7 additions & 7 deletions apps/desktop/src/routes/(window-chrome)/settings/general.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,13 @@ const settingsList: Array<{
pro?: boolean;
onChange?: (value: boolean) => Promise<void>;
}> = [
{
key: "autoCreateShareableLink",
label: "Automatically generate shareable link after recording",
description:
"When enabled, a shareable link will be created automatically after stopping the recording. You'll be redirected to the URL while the upload continues in the background.",
pro: true,
},
{
key: "uploadIndividualFiles",
label: "Upload individual recording files when creating shareable link",
Expand All @@ -46,13 +53,6 @@ const settingsList: Array<{
platforms: ["macos"],
description: "Use haptics on Force Touch™ trackpads",
},
{
key: "autoCreateShareableLink",
label: "Automatically create shareable link after recording",
description:
"When enabled, a shareable link will be created automatically after stopping the recording. You'll be redirected to the URL while the upload continues in the background.",
pro: true,
},
{
key: "disableAutoOpenLinks",
label: "Disable automatic link opening",
Expand Down
5 changes: 3 additions & 2 deletions apps/desktop/src/routes/editor/Timeline.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -185,8 +185,9 @@ export function Timeline() {
else {
let delta: number = 0;

if (platform() === "macos" && e.shiftKey) delta = e.deltaX;
else if (platform() === "windows" && e.shiftKey) delta = e.deltaY;
if (platform() === "macos")
delta = e.shiftKey ? e.deltaX : e.deltaY;
else delta = e.deltaY;

state.timelineTransform.setPosition(
state.timelineTransform.position + secsPerPixel() * delta
Expand Down
66 changes: 34 additions & 32 deletions crates/ffmpeg-hw-device/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,13 @@

use ffmpeg::{
codec,
format::Pixel,

Check warning on line 8 in crates/ffmpeg-hw-device/src/lib.rs

View workflow job for this annotation

GitHub Actions / Clippy

unused import: `format::Pixel`

warning: unused import: `format::Pixel` --> crates/ffmpeg-hw-device/src/lib.rs:8:5 | 8 | format::Pixel, | ^^^^^^^^^^^^^ | = note: `#[warn(unused_imports)]` on by default
frame::{self, Video},
};
use ffmpeg_sys_next::{
av_buffer_ref, av_buffer_unref, av_hwdevice_ctx_create, av_hwframe_transfer_data,
avcodec_get_hw_config, AVBufferRef, AVCodecContext, AVHWDeviceType, AVPixelFormat,
AV_CODEC_HW_CONFIG_METHOD_HW_DEVICE_CTX,
avcodec_get_hw_config, AVBufferRef, AVCodecContext, AVCodecHWConfig, AVHWDeviceType,
AVPixelFormat, AV_CODEC_HW_CONFIG_METHOD_HW_DEVICE_CTX,
};

thread_local! {
Expand Down Expand Up @@ -41,7 +41,6 @@

pub struct HwDevice {
pub device_type: AVHWDeviceType,
pub pix_fmt: Pixel,
ctx: *mut AVBufferRef,
}

Expand Down Expand Up @@ -70,58 +69,61 @@
}

pub trait CodecContextExt {
fn try_use_hw_device(
&mut self,
device_type: AVHWDeviceType,
pix_fmt: Pixel,
) -> Result<HwDevice, &'static str>;
fn try_use_hw_device(&mut self, device_type: AVHWDeviceType) -> Result<HwDevice, &'static str>;
}

impl CodecContextExt for codec::decoder::decoder::Decoder {
fn try_use_hw_device(
&mut self,
device_type: AVHWDeviceType,
pix_fmt: Pixel,
) -> Result<HwDevice, &'static str> {
fn try_use_hw_device(&mut self, device_type: AVHWDeviceType) -> Result<HwDevice, &'static str> {
let codec = self.codec().ok_or("no codec")?;

unsafe {
let mut i = 0;
loop {
let config = avcodec_get_hw_config(codec.as_ptr(), i);
if config.is_null() {
return Err("no hw config");
}

if (*config).methods & (AV_CODEC_HW_CONFIG_METHOD_HW_DEVICE_CTX as i32) == 1 {
HW_PIX_FMT.set((*config).pix_fmt);
break;
}

i += 1;
}

let context = self.as_mut_ptr();

(*context).get_format = Some(get_format);
let Some(hw_config) = codec.hw_configs().find(|&config| {
(*config).device_type == device_type
&& (*config).methods & (AV_CODEC_HW_CONFIG_METHOD_HW_DEVICE_CTX as i32) == 1
}) else {
return Err("no hw config");
};

let mut hw_device_ctx = null_mut();

if av_hwdevice_ctx_create(&mut hw_device_ctx, device_type, null(), null_mut(), 0) < 0 {
return Err("failed to create hw device context");
}

HW_PIX_FMT.set((*hw_config).pix_fmt);

let context = self.as_mut_ptr();

(*context).get_format = Some(get_format);
(*context).hw_device_ctx = av_buffer_ref(hw_device_ctx);

Ok(HwDevice {
device_type,
ctx: hw_device_ctx,
pix_fmt,
})
}
}
}

pub trait CodecExt {
fn hw_configs(&self) -> impl Iterator<Item = *const AVCodecHWConfig>;
}

impl CodecExt for codec::codec::Codec {
fn hw_configs(&self) -> impl Iterator<Item = *const AVCodecHWConfig> {
let mut i = 0;

std::iter::from_fn(move || {
let config = unsafe { avcodec_get_hw_config(self.as_ptr(), i) };
if config.is_null() {
return None;
}
i += 1;
Some(config)
})
}
}

// impl CodecContextExt for codec::encoder::video::Video {
// fn try_use_hw_device(
// &mut self,
Expand Down
2 changes: 1 addition & 1 deletion crates/flags/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,6 @@ pub struct Flags {
}

pub const FLAGS: Flags = Flags {
record_mouse_state: false, // cfg!(debug_assertions),
record_mouse_state: cfg!(debug_assertions),
split: false,
};
39 changes: 32 additions & 7 deletions crates/media/src/encoders/h264.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand All @@ -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 {
Expand Down Expand Up @@ -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);
}
Expand All @@ -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);
}
}
Expand Down
Loading
Loading