pause/resume with segment concat
This commit is contained in:
+140
-17
@@ -27,6 +27,17 @@ pub struct AppState {
|
||||
pub config: Mutex<Config>,
|
||||
pub capture: Mutex<Option<capture::CaptureSession>>,
|
||||
pub last_clip: Mutex<Option<RecordedClip>>,
|
||||
pub session: Mutex<Option<SessionState>>,
|
||||
}
|
||||
|
||||
pub struct SessionState {
|
||||
pub segments: Vec<PathBuf>,
|
||||
pub total_recorded_secs: f64,
|
||||
pub capture_cursor: bool,
|
||||
pub audio: bool,
|
||||
pub crop: Option<CaptureRect>,
|
||||
pub width: u32,
|
||||
pub height: u32,
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone, Serialize)]
|
||||
@@ -188,22 +199,43 @@ pub struct StartCaptureRequest {
|
||||
pub crop: Option<CaptureRect>,
|
||||
}
|
||||
|
||||
fn segment_path(app_dir: &Path, idx: usize) -> PathBuf {
|
||||
temp_dir(app_dir).join(format!("segment-{:03}.mp4", 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") {
|
||||
let _ = std::fs::remove_file(entry.path());
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
let _ = std::fs::remove_file(temp.join("recording.mp4"));
|
||||
}
|
||||
|
||||
#[tauri::command]
|
||||
fn start_capture(
|
||||
state: State<'_, AppState>,
|
||||
request: Option<StartCaptureRequest>,
|
||||
) -> Result<(), String> {
|
||||
let mut slot = state.capture.lock().expect("capture mutex");
|
||||
if slot.is_some() {
|
||||
return Err("a capture is already running".to_string());
|
||||
{
|
||||
let slot = state.capture.lock().expect("capture mutex");
|
||||
if slot.is_some() {
|
||||
return Err("a capture is already running".to_string());
|
||||
}
|
||||
}
|
||||
let cfg = state.config.lock().expect("config mutex").clone();
|
||||
let temp = temp_dir(&state.app_dir);
|
||||
std::fs::create_dir_all(&temp).map_err(|e| format!("creating temp dir: {e}"))?;
|
||||
let output = temp.join("recording.mp4");
|
||||
let _ = std::fs::remove_file(&output);
|
||||
clear_temp_segments(&state.app_dir);
|
||||
|
||||
let crop = request.and_then(|r| r.crop);
|
||||
let output = segment_path(&state.app_dir, 1);
|
||||
|
||||
let session = capture::start(
|
||||
output,
|
||||
cfg.capture.capture_cursor,
|
||||
@@ -211,30 +243,118 @@ fn start_capture(
|
||||
crop,
|
||||
)
|
||||
.map_err(|e| format!("{e:#}"))?;
|
||||
*slot = Some(session);
|
||||
|
||||
let session_state = SessionState {
|
||||
segments: Vec::new(),
|
||||
total_recorded_secs: 0.0,
|
||||
capture_cursor: cfg.capture.capture_cursor,
|
||||
audio: cfg.capture.capture_audio,
|
||||
crop,
|
||||
width: session.width,
|
||||
height: session.height,
|
||||
};
|
||||
|
||||
*state.session.lock().expect("session mutex") = Some(session_state);
|
||||
*state.capture.lock().expect("capture mutex") = Some(session);
|
||||
Ok(())
|
||||
}
|
||||
|
||||
#[tauri::command]
|
||||
fn stop_capture(state: State<'_, AppState>) -> Result<RecordedClip, String> {
|
||||
fn pause_capture(state: State<'_, AppState>) -> Result<(), String> {
|
||||
let session = state
|
||||
.capture
|
||||
.lock()
|
||||
.expect("capture mutex")
|
||||
.take()
|
||||
.ok_or_else(|| "no capture in progress".to_string())?;
|
||||
let duration_secs = session.started_at.elapsed().as_secs_f64();
|
||||
let width = session.width;
|
||||
let height = session.height;
|
||||
let crop = session.crop;
|
||||
let temp_path = session.stop().map_err(|e| format!("{e:#}"))?;
|
||||
let elapsed = session.started_at.elapsed().as_secs_f64();
|
||||
let path = session.stop().map_err(|e| format!("{e:#}"))?;
|
||||
|
||||
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);
|
||||
ss.total_recorded_secs += elapsed;
|
||||
Ok(())
|
||||
}
|
||||
|
||||
#[tauri::command]
|
||||
fn resume_capture(state: State<'_, AppState>) -> Result<(), String> {
|
||||
{
|
||||
let slot = state.capture.lock().expect("capture mutex");
|
||||
if slot.is_some() {
|
||||
return Err("a capture is already running".to_string());
|
||||
}
|
||||
}
|
||||
let (capture_cursor, audio, crop, next_idx) = {
|
||||
let ss = state.session.lock().expect("session mutex");
|
||||
let ss = ss
|
||||
.as_ref()
|
||||
.ok_or_else(|| "no session to resume".to_string())?;
|
||||
(
|
||||
ss.capture_cursor,
|
||||
ss.audio,
|
||||
ss.crop,
|
||||
ss.segments.len() + 1,
|
||||
)
|
||||
};
|
||||
|
||||
let output = segment_path(&state.app_dir, next_idx);
|
||||
let session = capture::start(output, capture_cursor, audio, crop)
|
||||
.map_err(|e| format!("{e:#}"))?;
|
||||
*state.capture.lock().expect("capture mutex") = Some(session);
|
||||
Ok(())
|
||||
}
|
||||
|
||||
#[tauri::command]
|
||||
fn stop_capture(state: State<'_, AppState>) -> Result<RecordedClip, String> {
|
||||
let mut ss = state
|
||||
.session
|
||||
.lock()
|
||||
.expect("session mutex")
|
||||
.take()
|
||||
.ok_or_else(|| "no session to stop".to_string())?;
|
||||
|
||||
let live = state.capture.lock().expect("capture mutex").take();
|
||||
if let Some(session) = live {
|
||||
let elapsed = session.started_at.elapsed().as_secs_f64();
|
||||
let path = session.stop().map_err(|e| format!("{e:#}"))?;
|
||||
ss.segments.push(path);
|
||||
ss.total_recorded_secs += elapsed;
|
||||
}
|
||||
|
||||
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);
|
||||
}
|
||||
} 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)
|
||||
.map_err(|e| format!("concat: {e:#}"))?;
|
||||
for seg in &ss.segments {
|
||||
let _ = std::fs::remove_file(seg);
|
||||
}
|
||||
}
|
||||
|
||||
let clip = RecordedClip {
|
||||
temp_path,
|
||||
duration_secs,
|
||||
width,
|
||||
height,
|
||||
crop,
|
||||
temp_path: final_path,
|
||||
duration_secs: ss.total_recorded_secs,
|
||||
width: ss.width,
|
||||
height: ss.height,
|
||||
crop: ss.crop,
|
||||
};
|
||||
*state.last_clip.lock().expect("clip mutex") = Some(clip.clone());
|
||||
Ok(clip)
|
||||
@@ -382,6 +502,7 @@ pub fn run() {
|
||||
config: Mutex::new(cfg),
|
||||
capture: Mutex::new(None),
|
||||
last_clip: Mutex::new(None),
|
||||
session: Mutex::new(None),
|
||||
};
|
||||
|
||||
tauri::Builder::default()
|
||||
@@ -393,6 +514,8 @@ pub fn run() {
|
||||
save_config,
|
||||
download_ffmpeg,
|
||||
start_capture,
|
||||
pause_capture,
|
||||
resume_capture,
|
||||
stop_capture,
|
||||
export,
|
||||
reveal_in_explorer,
|
||||
|
||||
Reference in New Issue
Block a user