diff --git a/record-daemon/README.md b/record-daemon/README.md index d877f57..e383693 100644 --- a/record-daemon/README.md +++ b/record-daemon/README.md @@ -124,7 +124,8 @@ The configuration file is stored at: - Linux: `~/.config/leaguerecorder/record-daemon/config.toml` - Windows: `%APPDATA%\leaguerecorder\record-daemon\config\config.toml` -Recordings and timelines go to `recordings` next to that `config` folder. +Timelines go to `recordings/timelines` next to that `config` folder. Videos go to the `output.path` +set below, which defaults to `recordings` beside it. Set `RECORD_DAEMON_HOME` to move all of it, config, recordings, logs and the IPC pipe, under one directory. Scripts and end-to-end tests use it so a run never reads or writes the real settings. diff --git a/record-daemon/src/config/mod.rs b/record-daemon/src/config/mod.rs index 1b770c9..8836912 100644 --- a/record-daemon/src/config/mod.rs +++ b/record-daemon/src/config/mod.rs @@ -14,7 +14,7 @@ pub use presets::{ }; pub use settings::{OutputSettings, Settings, VideoSettings}; -use std::path::PathBuf; +use std::path::{Path, PathBuf}; /// Default configuration file name. pub const CONFIG_FILE_NAME: &str = "config.toml"; @@ -51,3 +51,37 @@ pub fn get_data_dir() -> Option { pub fn get_default_output_dir() -> Option { get_data_dir().map(|dir| dir.join("recordings")) } + +/// Resolve the recordings folder and make it, returning the absolute path. +/// +/// The app and the daemon can run from different working directories, so a relative path +/// would name two different folders. The daemon resolves it once and keeps the result, so +/// both ends look in the same place. +pub fn prepare_output_dir(path: &Path) -> std::io::Result { + let path = if path.as_os_str().is_empty() { + get_default_output_dir().unwrap_or_else(|| PathBuf::from("./recordings")) + } else { + path.to_path_buf() + }; + let path = std::path::absolute(path)?; + std::fs::create_dir_all(&path)?; + Ok(path) +} + +#[cfg(test)] +mod tests { + use super::*; + + #[test] + fn preparing_a_folder_makes_it_absolute_and_creates_it() { + let dir = std::env::temp_dir().join(format!("record-daemon-{}", uuid::Uuid::new_v4())); + let nested = dir.join("nested"); + let path = prepare_output_dir(&nested).unwrap(); + + assert!(path.is_absolute()); + assert!(path.is_dir()); + assert!(path.ends_with("nested")); + + let _ = std::fs::remove_dir_all(dir); + } +} diff --git a/record-daemon/src/ipc/handlers.rs b/record-daemon/src/ipc/handlers.rs index f56b0bc..139a2d8 100644 --- a/record-daemon/src/ipc/handlers.rs +++ b/record-daemon/src/ipc/handlers.rs @@ -94,7 +94,19 @@ impl IpcHandlers { crate::error::IpcError::InvalidCommand("Missing settings payload".to_string()) })?; - let new_settings: Settings = serde_json::from_value(payload)?; + let mut new_settings: Settings = serde_json::from_value(payload)?; + + // Make the folder now, not on the first game. OBS cannot create it, so a path typed + // into the settings screen that does not exist would only fail once a game starts, + // with no clip to show for it. Resolving it here also gives the app the absolute path + // the daemon will write to, rather than one relative to a different working directory. + let folder = new_settings.output.path.clone(); + new_settings.output.path = crate::config::prepare_output_dir(&folder).map_err(|e| { + crate::error::ConfigError::InvalidConfig(format!( + "the recordings folder {} cannot be used: {e}", + folder.display() + )) + })?; // Validate settings if new_settings.video.frame_rate == 0 || new_settings.video.frame_rate > 240 { diff --git a/record-daemon/src/main.rs b/record-daemon/src/main.rs index c487a74..06a840e 100644 --- a/record-daemon/src/main.rs +++ b/record-daemon/src/main.rs @@ -707,11 +707,21 @@ async fn main() -> Result<()> { } } // Load configuration - let settings = if let Some(config_path) = args.config { + let mut settings = if let Some(config_path) = args.config { config::ConfigPersistence::new(config_path).load()? } else { config::load_config()? }; + + // Name the recordings folder the way the app will name it. A relative path in a + // hand-written config would otherwise mean one folder here and another in the app. + match config::prepare_output_dir(&settings.output.path) { + Ok(path) => settings.output.path = path, + Err(e) => warn!( + "recordings folder {} cannot be used: {e}", + settings.output.path.display() + ), + } // Say where the pieces live: the first thing to check when a game records without a video. info!( "config: {}, recordings: {}", diff --git a/record-daemon/src/recording/mod.rs b/record-daemon/src/recording/mod.rs index ee9b6e9..27f7524 100644 --- a/record-daemon/src/recording/mod.rs +++ b/record-daemon/src/recording/mod.rs @@ -100,6 +100,14 @@ impl RecordingEngine { let filename = self.generate_filename(game_id, champion); let output_path = settings.output.path.join(&filename); + // The settings screen can change the folder while this process runs, so the folder + // made when OBS started is not necessarily this one. OBS does not create directories: + // without this, a new folder makes every start fail and no clip is written. + if let Some(dir) = output_path.parent() { + std::fs::create_dir_all(dir) + .map_err(|e| RecordingError::OutputDirError(format!("{}: {e}", dir.display())))?; + } + let context = self .context .as_mut() diff --git a/record-daemon/src/timeline/mod.rs b/record-daemon/src/timeline/mod.rs index 61f20c0..3ef8cc5 100644 --- a/record-daemon/src/timeline/mod.rs +++ b/record-daemon/src/timeline/mod.rs @@ -8,6 +8,7 @@ pub use store::{MetadataUpdate, RecordingMetadata, TimelineStore, TimestampedEve use chrono::{DateTime, Duration, Utc}; use serde::{Deserialize, Serialize}; +use std::path::PathBuf; use uuid::Uuid; /// A timeline of events for a recording. @@ -48,6 +49,13 @@ pub struct Timeline { /// Video file name (just the filename, not the full path). #[serde(default)] pub video_file: Option, + /// Full path the video was written to, when the recorder knew it. + /// + /// The app looks a clip up by name in the recordings folder it knows. That folder can + /// change while older clips stay where they were, so the absolute path travels with the + /// timeline and a clip stays playable after the setting moves. + #[serde(default, skip_serializing_if = "Option::is_none")] + pub video_path: Option, /// What each audio track of the video carries, in the order the file holds them. #[serde(default)] pub audio_tracks: Vec, @@ -77,6 +85,7 @@ impl Timeline { raw_live_client_data: None, raw_end_game_stats: None, video_file: None, + video_path: None, audio_tracks: Vec::new(), } } diff --git a/record-daemon/src/timeline/store.rs b/record-daemon/src/timeline/store.rs index 7a49b55..f908feb 100644 --- a/record-daemon/src/timeline/store.rs +++ b/record-daemon/src/timeline/store.rs @@ -342,6 +342,7 @@ impl TimelineStore { raw_live_client_data: metadata.raw_live_client_data.clone(), raw_end_game_stats: metadata.raw_end_game_stats.clone(), video_file: metadata.video_file.clone(), + video_path: metadata.file_path.clone(), audio_tracks: metadata.audio_tracks.clone(), }) } @@ -387,6 +388,7 @@ impl TimelineStore { raw_live_client_data: metadata.raw_live_client_data, raw_end_game_stats: metadata.raw_end_game_stats, video_file: metadata.video_file, + video_path: metadata.file_path, audio_tracks: metadata.audio_tracks, }; @@ -424,6 +426,7 @@ impl TimelineStore { let raw_live_client_data = timeline.raw_live_client_data; let raw_end_game_stats = timeline.raw_end_game_stats; let video_file = timeline.video_file; + let video_path = timeline.video_path; let audio_tracks = timeline.audio_tracks; let events = timeline.events; @@ -439,7 +442,7 @@ impl TimelineStore { start_time, end_time, duration, - file_path: None, + file_path: video_path, video_file, audio_tracks, file_size: None,