Files
leaguerecorder/app/src/daemon.rs
T
vhaudiquet eabc3780c9 record: keep a track per audio source and let the review mute them
The recorder put every source on one mixer track, so the file had one audio
stream and the review screen could not tell the game's sound from the
microphone or from Discord.

The daemon now gives each source a track: 1 for the game, 2 for the
microphone, then one per application in the settings, up to OBS's six. Every
track above the first gets an audio encoder and the output is told which
tracks to write, which is what libobs's muxer reads to lay the streams out in
track order. The timeline's new `audio_tracks` names what each stream holds,
with empty names for the tracks no source fed.

The app opens one player per stream, copied out of the clip into the audio
cache, and the transport carries a toggle per channel. A channel that is off
is neither heard nor kept: clip and highlight exports mix only the tracks
left on. Recordings made before this change have one stream and play as they
did.
2026-11-19 18:37:20 +01:00

175 lines
5.7 KiB
Rust

//! Finding, starting and logging the record-daemon process.
//!
//! Ported from `tauri-app/src-tauri/src/daemon_ipc.rs`. The daemon binary ships next to the
//! app; inside a checkout it is found in `record-daemon/target/{release,debug}`.
use std::path::{Path, PathBuf};
use std::time::Duration;
use crate::ipc::Client;
/// Set this to point the app and the daemon at a scratch directory, as scripts do.
pub const HOME_ENV: &str = "RECORD_DAEMON_HOME";
/// Where the review screen keeps one audio file per track of a clip.
pub fn audio_cache_dir() -> PathBuf {
data_dir()
.map(|p| p.join("audio-cache"))
.unwrap_or_else(|| PathBuf::from("./audio-cache"))
}
/// The daemon's data directory, shared with the daemon itself.
pub fn data_dir() -> Option<PathBuf> {
if let Some(home) = std::env::var_os(HOME_ENV).filter(|value| !value.is_empty()) {
return Some(PathBuf::from(home).join("data"));
}
directories::ProjectDirs::from("com", "leaguerecorder", "record-daemon")
.map(|dirs| dirs.data_dir().to_path_buf())
}
/// Where recordings and timelines live.
pub fn recordings_dir() -> PathBuf {
data_dir()
.map(|p| p.join("recordings"))
.unwrap_or_else(|| PathBuf::from("./recordings"))
}
/// Where the daemon writes its log.
pub fn log_file() -> Option<PathBuf> {
let dir = data_dir()?.join("logs");
std::fs::create_dir_all(&dir).ok()?;
Some(dir.join("daemon.log"))
}
/// Find the record-daemon binary next to the app, in an installed `resources` folder, or in a
/// development checkout.
///
/// A checkout can hold both `target/debug` and `target/release`, one of them stale; the newest
/// build wins, so the app talks to the daemon that was built last ("cargo build" then "cargo
/// run", rather than a release build from another week).
pub fn find_binary() -> Option<PathBuf> {
let name = if cfg!(windows) {
"record-daemon.exe"
} else {
"record-daemon"
};
if let Ok(exe) = std::env::current_exe() {
let exe_dir = exe.parent()?;
let beside = exe_dir.join(name);
if beside.exists() {
return Some(beside);
}
if let Some(parent) = exe_dir.parent() {
let resources = parent.join("resources").join(name);
if resources.exists() {
return Some(resources);
}
}
if let Some(found) = newest_build(exe_dir, name) {
return Some(found);
}
}
newest_build(&std::env::current_dir().ok()?, name)
}
/// The most recently built daemon in a checkout, walking up from `start`.
fn newest_build(start: &Path, name: &str) -> Option<PathBuf> {
let mut dir = start.to_path_buf();
for _ in 0..6 {
let candidates = [
dir.join("record-daemon/target/release").join(name),
dir.join("record-daemon/target/debug").join(name),
];
if let Some(found) = candidates
.into_iter()
.filter_map(|path| {
let modified = std::fs::metadata(&path).ok()?.modified().ok()?;
Some((modified, path))
})
.max_by_key(|(modified, _)| *modified)
.map(|(_, path)| path)
{
return Some(found);
}
if !dir.pop() {
break;
}
}
None
}
/// Start the daemon detached, so it outlives the app.
pub fn spawn() -> Result<PathBuf, String> {
let binary = find_binary().ok_or_else(|| {
"record-daemon was not found next to the app or in record-daemon/target".to_string()
})?;
let log = log_file().ok_or_else(|| "no data directory for the daemon log".to_string())?;
let mut command = std::process::Command::new(&binary);
command.arg("--foreground").arg("--log-file").arg(&log);
if let Some(dir) = binary.parent() {
// The daemon finds its bundled OBS plugin data relative to itself.
command.current_dir(dir);
}
#[cfg(windows)]
{
use std::os::windows::process::CommandExt;
// CREATE_NEW_PROCESS_GROUP | DETACHED_PROCESS
command.creation_flags(0x0000_0200 | 0x0000_0008);
}
command
.stdin(std::process::Stdio::null())
.stdout(std::process::Stdio::null())
.stderr(std::process::Stdio::null());
command
.spawn()
.map_err(|e| format!("cannot start record-daemon: {e}"))?;
Ok(binary)
}
/// Wait up to `timeout` for the daemon to answer, starting it if it isn't running yet.
pub async fn ensure_running() -> Result<(), String> {
let client = Client::new();
if client.is_running().await {
return Ok(());
}
spawn()?;
let deadline = std::time::Instant::now() + Duration::from_secs(30);
loop {
if client.is_running().await {
return Ok(());
}
if std::time::Instant::now() >= deadline {
return Err("record-daemon did not answer within 30 s".to_string());
}
tokio::time::sleep(Duration::from_millis(500)).await;
}
}
/// The last `n` lines of the daemon log.
pub fn read_logs(n: usize) -> Result<String, String> {
let path = log_file().ok_or_else(|| "no data directory for the daemon log".to_string())?;
let text = std::fs::read_to_string(&path)
.map_err(|e| format!("cannot read {}: {e}", path.display()))?;
let lines: Vec<&str> = text.lines().rev().take(n).collect();
Ok(lines.into_iter().rev().collect::<Vec<_>>().join("\n"))
}
/// The log path as text, for the About tab.
pub fn log_path() -> String {
log_file()
.map(|p| p.display().to_string())
.unwrap_or_default()
}
/// Open a folder.
pub fn open_folder(path: &Path) {
#[cfg(windows)]
{
let _ = std::process::Command::new("explorer").arg(path).spawn();
}
#[cfg(not(windows))]
let _ = path;
}