system audio capture via wasapi loopback
This commit is contained in:
Generated
+150
@@ -61,6 +61,28 @@ dependencies = [
|
|||||||
"alloc-no-stdlib",
|
"alloc-no-stdlib",
|
||||||
]
|
]
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "alsa"
|
||||||
|
version = "0.9.1"
|
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
|
checksum = "ed7572b7ba83a31e20d1b48970ee402d2e3e0537dcfe0a3ff4d6eb7508617d43"
|
||||||
|
dependencies = [
|
||||||
|
"alsa-sys",
|
||||||
|
"bitflags 2.11.1",
|
||||||
|
"cfg-if",
|
||||||
|
"libc",
|
||||||
|
]
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "alsa-sys"
|
||||||
|
version = "0.3.1"
|
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
|
checksum = "db8fee663d06c4e303404ef5f40488a53e062f89ba8bfed81f42325aafad1527"
|
||||||
|
dependencies = [
|
||||||
|
"libc",
|
||||||
|
"pkg-config",
|
||||||
|
]
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "android_system_properties"
|
name = "android_system_properties"
|
||||||
version = "0.1.5"
|
version = "0.1.5"
|
||||||
@@ -356,6 +378,7 @@ name = "bytesize"
|
|||||||
version = "0.1.0"
|
version = "0.1.0"
|
||||||
dependencies = [
|
dependencies = [
|
||||||
"anyhow",
|
"anyhow",
|
||||||
|
"cpal",
|
||||||
"futures-util",
|
"futures-util",
|
||||||
"hex",
|
"hex",
|
||||||
"image",
|
"image",
|
||||||
@@ -602,6 +625,46 @@ dependencies = [
|
|||||||
"libc",
|
"libc",
|
||||||
]
|
]
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "coreaudio-rs"
|
||||||
|
version = "0.13.0"
|
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
|
checksum = "1aae284fbaf7d27aa0e292f7677dfbe26503b0d555026f702940805a630eac17"
|
||||||
|
dependencies = [
|
||||||
|
"bitflags 1.3.2",
|
||||||
|
"libc",
|
||||||
|
"objc2-audio-toolbox",
|
||||||
|
"objc2-core-audio",
|
||||||
|
"objc2-core-audio-types",
|
||||||
|
"objc2-core-foundation",
|
||||||
|
]
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "cpal"
|
||||||
|
version = "0.16.0"
|
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
|
checksum = "cbd307f43cc2a697e2d1f8bc7a1d824b5269e052209e28883e5bc04d095aaa3f"
|
||||||
|
dependencies = [
|
||||||
|
"alsa",
|
||||||
|
"coreaudio-rs",
|
||||||
|
"dasp_sample",
|
||||||
|
"jni",
|
||||||
|
"js-sys",
|
||||||
|
"libc",
|
||||||
|
"mach2",
|
||||||
|
"ndk",
|
||||||
|
"ndk-context",
|
||||||
|
"num-derive",
|
||||||
|
"num-traits",
|
||||||
|
"objc2-audio-toolbox",
|
||||||
|
"objc2-core-audio",
|
||||||
|
"objc2-core-audio-types",
|
||||||
|
"wasm-bindgen",
|
||||||
|
"wasm-bindgen-futures",
|
||||||
|
"web-sys",
|
||||||
|
"windows 0.54.0",
|
||||||
|
]
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "cpubits"
|
name = "cpubits"
|
||||||
version = "0.1.1"
|
version = "0.1.1"
|
||||||
@@ -767,6 +830,12 @@ dependencies = [
|
|||||||
"syn 2.0.117",
|
"syn 2.0.117",
|
||||||
]
|
]
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "dasp_sample"
|
||||||
|
version = "0.11.0"
|
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
|
checksum = "0c87e182de0887fd5361989c677c4e8f5000cd9491d6d563161a8f3a5519fc7f"
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "dbus"
|
name = "dbus"
|
||||||
version = "0.9.11"
|
version = "0.9.11"
|
||||||
@@ -2272,6 +2341,15 @@ version = "0.16.2"
|
|||||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
checksum = "47bb1e988e6fb779cf720ad431242d3f03167c1b3f2b1aae7f1a94b2495b36ae"
|
checksum = "47bb1e988e6fb779cf720ad431242d3f03167c1b3f2b1aae7f1a94b2495b36ae"
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "mach2"
|
||||||
|
version = "0.4.3"
|
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
|
checksum = "d640282b302c0bb0a2a8e0233ead9035e3bed871f0b7e81fe4a1ec829765db44"
|
||||||
|
dependencies = [
|
||||||
|
"libc",
|
||||||
|
]
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "markup5ever"
|
name = "markup5ever"
|
||||||
version = "0.38.0"
|
version = "0.38.0"
|
||||||
@@ -2398,6 +2476,12 @@ dependencies = [
|
|||||||
"thiserror 1.0.69",
|
"thiserror 1.0.69",
|
||||||
]
|
]
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "ndk-context"
|
||||||
|
version = "0.1.1"
|
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
|
checksum = "27b02d87554356db9e9a873add8782d4ea6e3e58ea071a9adb9a2e8ddb884a8b"
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "ndk-sys"
|
name = "ndk-sys"
|
||||||
version = "0.6.0+11769913"
|
version = "0.6.0+11769913"
|
||||||
@@ -2547,6 +2631,21 @@ dependencies = [
|
|||||||
"objc2-foundation",
|
"objc2-foundation",
|
||||||
]
|
]
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "objc2-audio-toolbox"
|
||||||
|
version = "0.3.2"
|
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
|
checksum = "6948501a91121d6399b79abaa33a8aa4ea7857fe019f341b8c23ad6e81b79b08"
|
||||||
|
dependencies = [
|
||||||
|
"bitflags 2.11.1",
|
||||||
|
"libc",
|
||||||
|
"objc2",
|
||||||
|
"objc2-core-audio",
|
||||||
|
"objc2-core-audio-types",
|
||||||
|
"objc2-core-foundation",
|
||||||
|
"objc2-foundation",
|
||||||
|
]
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "objc2-cloud-kit"
|
name = "objc2-cloud-kit"
|
||||||
version = "0.3.2"
|
version = "0.3.2"
|
||||||
@@ -2558,6 +2657,28 @@ dependencies = [
|
|||||||
"objc2-foundation",
|
"objc2-foundation",
|
||||||
]
|
]
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "objc2-core-audio"
|
||||||
|
version = "0.3.2"
|
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
|
checksum = "e1eebcea8b0dbff5f7c8504f3107c68fc061a3eb44932051c8cf8a68d969c3b2"
|
||||||
|
dependencies = [
|
||||||
|
"dispatch2",
|
||||||
|
"objc2",
|
||||||
|
"objc2-core-audio-types",
|
||||||
|
"objc2-core-foundation",
|
||||||
|
]
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "objc2-core-audio-types"
|
||||||
|
version = "0.3.2"
|
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
|
checksum = "5a89f2ec274a0cf4a32642b2991e8b351a404d290da87bb6a9a9d8632490bd1c"
|
||||||
|
dependencies = [
|
||||||
|
"bitflags 2.11.1",
|
||||||
|
"objc2",
|
||||||
|
]
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "objc2-core-data"
|
name = "objc2-core-data"
|
||||||
version = "0.3.2"
|
version = "0.3.2"
|
||||||
@@ -5211,6 +5332,16 @@ dependencies = [
|
|||||||
"windows-version",
|
"windows-version",
|
||||||
]
|
]
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "windows"
|
||||||
|
version = "0.54.0"
|
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
|
checksum = "9252e5725dbed82865af151df558e754e4a3c2c30818359eb17465f1346a1b49"
|
||||||
|
dependencies = [
|
||||||
|
"windows-core 0.54.0",
|
||||||
|
"windows-targets 0.52.6",
|
||||||
|
]
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "windows"
|
name = "windows"
|
||||||
version = "0.61.3"
|
version = "0.61.3"
|
||||||
@@ -5267,6 +5398,16 @@ dependencies = [
|
|||||||
"windows-core 0.62.2",
|
"windows-core 0.62.2",
|
||||||
]
|
]
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "windows-core"
|
||||||
|
version = "0.54.0"
|
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
|
checksum = "12661b9c89351d684a50a8a643ce5f608e20243b9fb84687800163429f161d65"
|
||||||
|
dependencies = [
|
||||||
|
"windows-result 0.1.2",
|
||||||
|
"windows-targets 0.52.6",
|
||||||
|
]
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "windows-core"
|
name = "windows-core"
|
||||||
version = "0.61.2"
|
version = "0.61.2"
|
||||||
@@ -5369,6 +5510,15 @@ dependencies = [
|
|||||||
"windows-link 0.2.1",
|
"windows-link 0.2.1",
|
||||||
]
|
]
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "windows-result"
|
||||||
|
version = "0.1.2"
|
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
|
checksum = "5e383302e8ec8515204254685643de10811af0ed97ea37210dc26fb0032647f8"
|
||||||
|
dependencies = [
|
||||||
|
"windows-targets 0.52.6",
|
||||||
|
]
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "windows-result"
|
name = "windows-result"
|
||||||
version = "0.3.4"
|
version = "0.3.4"
|
||||||
|
|||||||
@@ -19,6 +19,7 @@ tauri-plugin-dialog = "2"
|
|||||||
tauri-plugin-global-shortcut = "2"
|
tauri-plugin-global-shortcut = "2"
|
||||||
windows-capture = "2.0"
|
windows-capture = "2.0"
|
||||||
windows = { version = "0.62", features = ["Win32_UI_WindowsAndMessaging", "Win32_Foundation"] }
|
windows = { version = "0.62", features = ["Win32_UI_WindowsAndMessaging", "Win32_Foundation"] }
|
||||||
|
cpal = "0.16"
|
||||||
tokio = { version = "1.47", features = ["full"] }
|
tokio = { version = "1.47", features = ["full"] }
|
||||||
serde = { version = "1.0", features = ["derive"] }
|
serde = { version = "1.0", features = ["derive"] }
|
||||||
serde_json = "1.0"
|
serde_json = "1.0"
|
||||||
|
|||||||
@@ -0,0 +1,77 @@
|
|||||||
|
use std::fs::File;
|
||||||
|
use std::io::{BufWriter, Write};
|
||||||
|
use std::path::PathBuf;
|
||||||
|
use std::sync::{Arc, Mutex};
|
||||||
|
|
||||||
|
use anyhow::{anyhow, Context, Result};
|
||||||
|
use cpal::traits::{DeviceTrait, HostTrait, StreamTrait};
|
||||||
|
|
||||||
|
pub struct AudioSession {
|
||||||
|
_stream: cpal::Stream,
|
||||||
|
pub output_path: PathBuf,
|
||||||
|
pub sample_rate: u32,
|
||||||
|
pub channels: u16,
|
||||||
|
writer: Arc<Mutex<Option<BufWriter<File>>>>,
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn start(output_path: PathBuf) -> Result<AudioSession> {
|
||||||
|
let host = cpal::default_host();
|
||||||
|
let device = host
|
||||||
|
.default_output_device()
|
||||||
|
.ok_or_else(|| anyhow!("no default output device for loopback"))?;
|
||||||
|
let supported = device
|
||||||
|
.default_output_config()
|
||||||
|
.context("default output config")?;
|
||||||
|
let sample_rate = supported.sample_rate().0;
|
||||||
|
let channels = supported.channels();
|
||||||
|
let stream_config: cpal::StreamConfig = supported.config();
|
||||||
|
|
||||||
|
if let Some(parent) = output_path.parent() {
|
||||||
|
std::fs::create_dir_all(parent).ok();
|
||||||
|
}
|
||||||
|
let file = File::create(&output_path).context("creating audio output file")?;
|
||||||
|
let writer = Arc::new(Mutex::new(Some(BufWriter::new(file))));
|
||||||
|
let writer_clone = writer.clone();
|
||||||
|
|
||||||
|
let stream = device
|
||||||
|
.build_input_stream(
|
||||||
|
&stream_config,
|
||||||
|
move |data: &[f32], _info: &cpal::InputCallbackInfo| {
|
||||||
|
if let Ok(mut guard) = writer_clone.lock() {
|
||||||
|
if let Some(w) = guard.as_mut() {
|
||||||
|
let bytes: &[u8] = bytemuck_cast(data);
|
||||||
|
let _ = w.write_all(bytes);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
},
|
||||||
|
|err| tracing::warn!("audio capture error: {err}"),
|
||||||
|
None,
|
||||||
|
)
|
||||||
|
.context("building input stream")?;
|
||||||
|
stream.play().context("starting audio stream")?;
|
||||||
|
|
||||||
|
Ok(AudioSession {
|
||||||
|
_stream: stream,
|
||||||
|
output_path,
|
||||||
|
sample_rate,
|
||||||
|
channels,
|
||||||
|
writer,
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
|
impl AudioSession {
|
||||||
|
pub fn stop(self) -> Result<PathBuf> {
|
||||||
|
drop(self._stream);
|
||||||
|
if let Ok(mut guard) = self.writer.lock() {
|
||||||
|
if let Some(mut w) = guard.take() {
|
||||||
|
let _ = w.flush();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
Ok(self.output_path)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
fn bytemuck_cast(data: &[f32]) -> &[u8] {
|
||||||
|
let len_bytes = std::mem::size_of_val(data);
|
||||||
|
unsafe { std::slice::from_raw_parts(data.as_ptr() as *const u8, len_bytes) }
|
||||||
|
}
|
||||||
@@ -381,6 +381,58 @@ pub fn extract_thumbnails(
|
|||||||
Ok(paths)
|
Ok(paths)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
pub fn mux_with_audio(
|
||||||
|
ffmpeg: &Path,
|
||||||
|
video: &Path,
|
||||||
|
audio_pcm: &Path,
|
||||||
|
sample_rate: u32,
|
||||||
|
channels: u16,
|
||||||
|
output: &Path,
|
||||||
|
) -> Result<()> {
|
||||||
|
if let Some(parent) = output.parent() {
|
||||||
|
std::fs::create_dir_all(parent).context("creating output parent dir")?;
|
||||||
|
}
|
||||||
|
let mut cmd = Command::new(ffmpeg);
|
||||||
|
cmd.arg("-y")
|
||||||
|
.arg("-hide_banner")
|
||||||
|
.arg("-i")
|
||||||
|
.arg(video)
|
||||||
|
.args(["-f", "f32le"])
|
||||||
|
.args(["-ar", &sample_rate.to_string()])
|
||||||
|
.args(["-ac", &channels.to_string()])
|
||||||
|
.arg("-i")
|
||||||
|
.arg(audio_pcm)
|
||||||
|
.args(["-c:v", "copy"])
|
||||||
|
.args(["-c:a", "aac"])
|
||||||
|
.args(["-b:a", "192k"])
|
||||||
|
.args(["-shortest"])
|
||||||
|
.arg(output);
|
||||||
|
run(&mut cmd, "mux")
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn concat_pcm(segments: &[PathBuf], output: &Path) -> Result<()> {
|
||||||
|
use std::io::{Read, Write};
|
||||||
|
if segments.is_empty() {
|
||||||
|
bail!("no audio segments to concatenate");
|
||||||
|
}
|
||||||
|
if let Some(parent) = output.parent() {
|
||||||
|
std::fs::create_dir_all(parent).context("creating audio concat parent")?;
|
||||||
|
}
|
||||||
|
let mut out = std::fs::File::create(output).context("creating audio concat file")?;
|
||||||
|
for seg in segments {
|
||||||
|
let mut f = std::fs::File::open(seg).with_context(|| format!("opening {seg:?}"))?;
|
||||||
|
let mut buf = vec![0u8; 64 * 1024];
|
||||||
|
loop {
|
||||||
|
let n = f.read(&mut buf)?;
|
||||||
|
if n == 0 {
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
out.write_all(&buf[..n])?;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
Ok(())
|
||||||
|
}
|
||||||
|
|
||||||
pub fn concat_segments(ffmpeg: &Path, segments: &[PathBuf], output: &Path) -> Result<()> {
|
pub fn concat_segments(ffmpeg: &Path, segments: &[PathBuf], output: &Path) -> Result<()> {
|
||||||
if segments.is_empty() {
|
if segments.is_empty() {
|
||||||
bail!("no segments to concatenate");
|
bail!("no segments to concatenate");
|
||||||
|
|||||||
+112
-12
@@ -1,3 +1,4 @@
|
|||||||
|
mod audio;
|
||||||
mod bootstrap;
|
mod bootstrap;
|
||||||
mod capture;
|
mod capture;
|
||||||
mod config;
|
mod config;
|
||||||
@@ -29,12 +30,16 @@ pub struct AppState {
|
|||||||
pub app_dir: PathBuf,
|
pub app_dir: PathBuf,
|
||||||
pub config: Mutex<Config>,
|
pub config: Mutex<Config>,
|
||||||
pub capture: Mutex<Option<capture::CaptureSession>>,
|
pub capture: Mutex<Option<capture::CaptureSession>>,
|
||||||
|
pub audio: Mutex<Option<audio::AudioSession>>,
|
||||||
pub last_clip: Mutex<Option<RecordedClip>>,
|
pub last_clip: Mutex<Option<RecordedClip>>,
|
||||||
pub session: Mutex<Option<SessionState>>,
|
pub session: Mutex<Option<SessionState>>,
|
||||||
}
|
}
|
||||||
|
|
||||||
pub struct SessionState {
|
pub struct SessionState {
|
||||||
pub segments: Vec<PathBuf>,
|
pub segments: Vec<PathBuf>,
|
||||||
|
pub audio_segments: Vec<PathBuf>,
|
||||||
|
pub audio_sample_rate: Option<u32>,
|
||||||
|
pub audio_channels: Option<u16>,
|
||||||
pub total_recorded_secs: f64,
|
pub total_recorded_secs: f64,
|
||||||
pub capture_cursor: bool,
|
pub capture_cursor: bool,
|
||||||
pub audio: bool,
|
pub audio: bool,
|
||||||
@@ -235,18 +240,26 @@ fn segment_path(app_dir: &Path, idx: usize) -> PathBuf {
|
|||||||
temp_dir(app_dir).join(format!("segment-{:03}.mp4", idx))
|
temp_dir(app_dir).join(format!("segment-{:03}.mp4", idx))
|
||||||
}
|
}
|
||||||
|
|
||||||
|
fn audio_segment_path(app_dir: &Path, idx: usize) -> PathBuf {
|
||||||
|
temp_dir(app_dir).join(format!("audio-{:03}.pcm", idx))
|
||||||
|
}
|
||||||
|
|
||||||
fn clear_temp_segments(app_dir: &Path) {
|
fn clear_temp_segments(app_dir: &Path) {
|
||||||
let temp = temp_dir(app_dir);
|
let temp = temp_dir(app_dir);
|
||||||
if let Ok(entries) = std::fs::read_dir(&temp) {
|
if let Ok(entries) = std::fs::read_dir(&temp) {
|
||||||
for entry in entries.flatten() {
|
for entry in entries.flatten() {
|
||||||
if let Some(name) = entry.file_name().to_str() {
|
if let Some(name) = entry.file_name().to_str() {
|
||||||
if name.starts_with("segment-") && name.ends_with(".mp4") {
|
if (name.starts_with("segment-") && name.ends_with(".mp4"))
|
||||||
|
|| (name.starts_with("audio-") && name.ends_with(".pcm"))
|
||||||
|
{
|
||||||
let _ = std::fs::remove_file(entry.path());
|
let _ = std::fs::remove_file(entry.path());
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
let _ = std::fs::remove_file(temp.join("recording.mp4"));
|
let _ = std::fs::remove_file(temp.join("recording.mp4"));
|
||||||
|
let _ = std::fs::remove_file(temp.join("video-concat.mp4"));
|
||||||
|
let _ = std::fs::remove_file(temp.join("audio-concat.pcm"));
|
||||||
}
|
}
|
||||||
|
|
||||||
fn set_overlay_clickthrough(app: &AppHandle, enabled: bool) {
|
fn set_overlay_clickthrough(app: &AppHandle, enabled: bool) {
|
||||||
@@ -290,10 +303,31 @@ fn start_capture(
|
|||||||
)
|
)
|
||||||
.map_err(|e| format!("{e:#}"))?;
|
.map_err(|e| format!("{e:#}"))?;
|
||||||
|
|
||||||
|
let audio_session = if cfg.capture.capture_audio {
|
||||||
|
let audio_path = audio_segment_path(&state.app_dir, 1);
|
||||||
|
match audio::start(audio_path) {
|
||||||
|
Ok(s) => Some(s),
|
||||||
|
Err(e) => {
|
||||||
|
tracing::warn!("audio capture failed to start: {e:#}");
|
||||||
|
None
|
||||||
|
}
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
None
|
||||||
|
};
|
||||||
|
|
||||||
set_overlay_clickthrough(&app, true);
|
set_overlay_clickthrough(&app, true);
|
||||||
|
|
||||||
|
let (a_sr, a_ch) = audio_session
|
||||||
|
.as_ref()
|
||||||
|
.map(|s| (Some(s.sample_rate), Some(s.channels)))
|
||||||
|
.unwrap_or((None, None));
|
||||||
|
|
||||||
let session_state = SessionState {
|
let session_state = SessionState {
|
||||||
segments: Vec::new(),
|
segments: Vec::new(),
|
||||||
|
audio_segments: Vec::new(),
|
||||||
|
audio_sample_rate: a_sr,
|
||||||
|
audio_channels: a_ch,
|
||||||
total_recorded_secs: 0.0,
|
total_recorded_secs: 0.0,
|
||||||
capture_cursor: cfg.capture.capture_cursor,
|
capture_cursor: cfg.capture.capture_cursor,
|
||||||
audio: cfg.capture.capture_audio,
|
audio: cfg.capture.capture_audio,
|
||||||
@@ -304,6 +338,7 @@ fn start_capture(
|
|||||||
|
|
||||||
*state.session.lock().expect("session mutex") = Some(session_state);
|
*state.session.lock().expect("session mutex") = Some(session_state);
|
||||||
*state.capture.lock().expect("capture mutex") = Some(session);
|
*state.capture.lock().expect("capture mutex") = Some(session);
|
||||||
|
*state.audio.lock().expect("audio mutex") = audio_session;
|
||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -318,11 +353,21 @@ fn pause_capture(state: State<'_, AppState>) -> Result<(), String> {
|
|||||||
let elapsed = session.started_at.elapsed().as_secs_f64();
|
let elapsed = session.started_at.elapsed().as_secs_f64();
|
||||||
let path = session.stop().map_err(|e| format!("{e:#}"))?;
|
let path = session.stop().map_err(|e| format!("{e:#}"))?;
|
||||||
|
|
||||||
|
let audio_path = state
|
||||||
|
.audio
|
||||||
|
.lock()
|
||||||
|
.expect("audio mutex")
|
||||||
|
.take()
|
||||||
|
.and_then(|a| a.stop().ok());
|
||||||
|
|
||||||
let mut ss = state.session.lock().expect("session mutex");
|
let mut ss = state.session.lock().expect("session mutex");
|
||||||
let ss = ss
|
let ss = ss
|
||||||
.as_mut()
|
.as_mut()
|
||||||
.ok_or_else(|| "no session state".to_string())?;
|
.ok_or_else(|| "no session state".to_string())?;
|
||||||
ss.segments.push(path);
|
ss.segments.push(path);
|
||||||
|
if let Some(ap) = audio_path {
|
||||||
|
ss.audio_segments.push(ap);
|
||||||
|
}
|
||||||
ss.total_recorded_secs += elapsed;
|
ss.total_recorded_secs += elapsed;
|
||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
@@ -356,7 +401,22 @@ fn resume_capture(state: State<'_, AppState>) -> Result<(), String> {
|
|||||||
}
|
}
|
||||||
let session = capture::start(output, ffmpeg, capture_cursor, audio, crop, fps)
|
let session = capture::start(output, ffmpeg, capture_cursor, audio, crop, fps)
|
||||||
.map_err(|e| format!("{e:#}"))?;
|
.map_err(|e| format!("{e:#}"))?;
|
||||||
|
|
||||||
|
let audio_session = if audio {
|
||||||
|
let audio_path = audio_segment_path(&state.app_dir, next_idx);
|
||||||
|
match audio::start(audio_path) {
|
||||||
|
Ok(s) => Some(s),
|
||||||
|
Err(e) => {
|
||||||
|
tracing::warn!("audio resume failed: {e:#}");
|
||||||
|
None
|
||||||
|
}
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
None
|
||||||
|
};
|
||||||
|
|
||||||
*state.capture.lock().expect("capture mutex") = Some(session);
|
*state.capture.lock().expect("capture mutex") = Some(session);
|
||||||
|
*state.audio.lock().expect("audio mutex") = audio_session;
|
||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -379,29 +439,68 @@ fn stop_capture(app: AppHandle, state: State<'_, AppState>) -> Result<RecordedCl
|
|||||||
ss.total_recorded_secs += elapsed;
|
ss.total_recorded_secs += elapsed;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
let live_audio = state.audio.lock().expect("audio mutex").take();
|
||||||
|
if let Some(a) = live_audio {
|
||||||
|
if let Ok(p) = a.stop() {
|
||||||
|
ss.audio_segments.push(p);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
if ss.segments.is_empty() {
|
if ss.segments.is_empty() {
|
||||||
return Err("no recorded segments".to_string());
|
return Err("no recorded segments".to_string());
|
||||||
}
|
}
|
||||||
|
|
||||||
let final_path = temp_dir(&state.app_dir).join("recording.mp4");
|
let temp = temp_dir(&state.app_dir);
|
||||||
if ss.segments.len() == 1 {
|
let final_path = temp.join("recording.mp4");
|
||||||
let only = ss.segments.remove(0);
|
let ffmpeg = bootstrap::ffmpeg_path(&state.app_dir);
|
||||||
if only != final_path {
|
|
||||||
std::fs::rename(&only, &final_path)
|
let video_concat = if ss.segments.len() == 1 {
|
||||||
.or_else(|_| std::fs::copy(&only, &final_path).map(|_| ()))
|
ss.segments.remove(0)
|
||||||
.map_err(|e| format!("finalizing single segment: {e}"))?;
|
|
||||||
let _ = std::fs::remove_file(&only);
|
|
||||||
}
|
|
||||||
} else {
|
} else {
|
||||||
let ffmpeg = bootstrap::ffmpeg_path(&state.app_dir);
|
|
||||||
if !ffmpeg.is_file() {
|
if !ffmpeg.is_file() {
|
||||||
return Err("ffmpeg.exe not found for concatenation".to_string());
|
return Err("ffmpeg.exe not found for concatenation".to_string());
|
||||||
}
|
}
|
||||||
encode::concat_segments(&ffmpeg, &ss.segments, &final_path)
|
let vc = temp.join("video-concat.mp4");
|
||||||
|
encode::concat_segments(&ffmpeg, &ss.segments, &vc)
|
||||||
.map_err(|e| format!("concat: {e:#}"))?;
|
.map_err(|e| format!("concat: {e:#}"))?;
|
||||||
for seg in &ss.segments {
|
for seg in &ss.segments {
|
||||||
let _ = std::fs::remove_file(seg);
|
let _ = std::fs::remove_file(seg);
|
||||||
}
|
}
|
||||||
|
vc
|
||||||
|
};
|
||||||
|
|
||||||
|
let have_audio = !ss.audio_segments.is_empty()
|
||||||
|
&& ss.audio_sample_rate.is_some()
|
||||||
|
&& ss.audio_channels.is_some();
|
||||||
|
|
||||||
|
if have_audio {
|
||||||
|
if !ffmpeg.is_file() {
|
||||||
|
return Err("ffmpeg.exe not found for audio mux".to_string());
|
||||||
|
}
|
||||||
|
let audio_concat = temp.join("audio-concat.pcm");
|
||||||
|
encode::concat_pcm(&ss.audio_segments, &audio_concat)
|
||||||
|
.map_err(|e| format!("audio concat: {e:#}"))?;
|
||||||
|
encode::mux_with_audio(
|
||||||
|
&ffmpeg,
|
||||||
|
&video_concat,
|
||||||
|
&audio_concat,
|
||||||
|
ss.audio_sample_rate.unwrap(),
|
||||||
|
ss.audio_channels.unwrap(),
|
||||||
|
&final_path,
|
||||||
|
)
|
||||||
|
.map_err(|e| format!("mux: {e:#}"))?;
|
||||||
|
if video_concat != final_path {
|
||||||
|
let _ = std::fs::remove_file(&video_concat);
|
||||||
|
}
|
||||||
|
let _ = std::fs::remove_file(&audio_concat);
|
||||||
|
for ap in &ss.audio_segments {
|
||||||
|
let _ = std::fs::remove_file(ap);
|
||||||
|
}
|
||||||
|
} else if video_concat != final_path {
|
||||||
|
std::fs::rename(&video_concat, &final_path)
|
||||||
|
.or_else(|_| std::fs::copy(&video_concat, &final_path).map(|_| ()))
|
||||||
|
.map_err(|e| format!("finalizing: {e}"))?;
|
||||||
|
let _ = std::fs::remove_file(&video_concat);
|
||||||
}
|
}
|
||||||
|
|
||||||
let clip = RecordedClip {
|
let clip = RecordedClip {
|
||||||
@@ -665,6 +764,7 @@ pub fn run() {
|
|||||||
app_dir,
|
app_dir,
|
||||||
config: Mutex::new(cfg),
|
config: Mutex::new(cfg),
|
||||||
capture: Mutex::new(None),
|
capture: Mutex::new(None),
|
||||||
|
audio: Mutex::new(None),
|
||||||
last_clip: Mutex::new(None),
|
last_clip: Mutex::new(None),
|
||||||
session: Mutex::new(None),
|
session: Mutex::new(None),
|
||||||
};
|
};
|
||||||
|
|||||||
Reference in New Issue
Block a user