system audio capture via wasapi loopback
This commit is contained in:
+112
-12
@@ -1,3 +1,4 @@
|
||||
mod audio;
|
||||
mod bootstrap;
|
||||
mod capture;
|
||||
mod config;
|
||||
@@ -29,12 +30,16 @@ pub struct AppState {
|
||||
pub app_dir: PathBuf,
|
||||
pub config: Mutex<Config>,
|
||||
pub capture: Mutex<Option<capture::CaptureSession>>,
|
||||
pub audio: Mutex<Option<audio::AudioSession>>,
|
||||
pub last_clip: Mutex<Option<RecordedClip>>,
|
||||
pub session: Mutex<Option<SessionState>>,
|
||||
}
|
||||
|
||||
pub struct SessionState {
|
||||
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 capture_cursor: 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))
|
||||
}
|
||||
|
||||
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) {
|
||||
let temp = temp_dir(app_dir);
|
||||
if let Ok(entries) = std::fs::read_dir(&temp) {
|
||||
for entry in entries.flatten() {
|
||||
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(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) {
|
||||
@@ -290,10 +303,31 @@ fn start_capture(
|
||||
)
|
||||
.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);
|
||||
|
||||
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 {
|
||||
segments: Vec::new(),
|
||||
audio_segments: Vec::new(),
|
||||
audio_sample_rate: a_sr,
|
||||
audio_channels: a_ch,
|
||||
total_recorded_secs: 0.0,
|
||||
capture_cursor: cfg.capture.capture_cursor,
|
||||
audio: cfg.capture.capture_audio,
|
||||
@@ -304,6 +338,7 @@ fn start_capture(
|
||||
|
||||
*state.session.lock().expect("session mutex") = Some(session_state);
|
||||
*state.capture.lock().expect("capture mutex") = Some(session);
|
||||
*state.audio.lock().expect("audio mutex") = audio_session;
|
||||
Ok(())
|
||||
}
|
||||
|
||||
@@ -318,11 +353,21 @@ fn pause_capture(state: State<'_, AppState>) -> Result<(), String> {
|
||||
let elapsed = session.started_at.elapsed().as_secs_f64();
|
||||
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 ss = ss
|
||||
.as_mut()
|
||||
.ok_or_else(|| "no session state".to_string())?;
|
||||
ss.segments.push(path);
|
||||
if let Some(ap) = audio_path {
|
||||
ss.audio_segments.push(ap);
|
||||
}
|
||||
ss.total_recorded_secs += elapsed;
|
||||
Ok(())
|
||||
}
|
||||
@@ -356,7 +401,22 @@ fn resume_capture(state: State<'_, AppState>) -> Result<(), String> {
|
||||
}
|
||||
let session = capture::start(output, ffmpeg, capture_cursor, audio, crop, fps)
|
||||
.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.audio.lock().expect("audio mutex") = audio_session;
|
||||
Ok(())
|
||||
}
|
||||
|
||||
@@ -379,29 +439,68 @@ fn stop_capture(app: AppHandle, state: State<'_, AppState>) -> Result<RecordedCl
|
||||
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() {
|
||||
return Err("no recorded segments".to_string());
|
||||
}
|
||||
|
||||
let final_path = temp_dir(&state.app_dir).join("recording.mp4");
|
||||
if ss.segments.len() == 1 {
|
||||
let only = ss.segments.remove(0);
|
||||
if only != final_path {
|
||||
std::fs::rename(&only, &final_path)
|
||||
.or_else(|_| std::fs::copy(&only, &final_path).map(|_| ()))
|
||||
.map_err(|e| format!("finalizing single segment: {e}"))?;
|
||||
let _ = std::fs::remove_file(&only);
|
||||
}
|
||||
let temp = temp_dir(&state.app_dir);
|
||||
let final_path = temp.join("recording.mp4");
|
||||
let ffmpeg = bootstrap::ffmpeg_path(&state.app_dir);
|
||||
|
||||
let video_concat = if ss.segments.len() == 1 {
|
||||
ss.segments.remove(0)
|
||||
} else {
|
||||
let ffmpeg = bootstrap::ffmpeg_path(&state.app_dir);
|
||||
if !ffmpeg.is_file() {
|
||||
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:#}"))?;
|
||||
for seg in &ss.segments {
|
||||
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 {
|
||||
@@ -665,6 +764,7 @@ pub fn run() {
|
||||
app_dir,
|
||||
config: Mutex::new(cfg),
|
||||
capture: Mutex::new(None),
|
||||
audio: Mutex::new(None),
|
||||
last_clip: Mutex::new(None),
|
||||
session: Mutex::new(None),
|
||||
};
|
||||
|
||||
Reference in New Issue
Block a user