record-daemon: keep app audio attached across recordings
OBS starts with the daemon and builds one scene, and every recording reuses it. The process captures that used to be created per recording stay in that scene, so a game or Discord window that disappears mid-session keeps feeding the mix. The captures come from a watcher that scans for a window every two seconds: a process with no window cannot be opened, so each source waits for its app to show one, and a new window means the process restarted and needs a fresh source. Settings now travel to the engine, and an audio change swaps only the device captures. Applying them used to clear the scene, which took the process captures with it.
This commit is contained in:
1 parent
9f5f1f669d
commit
365e4ad732
5 files changed
+793
-202
No files matched your search
@@ -12,7 +12,7 @@ use super::protocol::{
|
||||
use crate::config::Settings;
|
||||
use crate::error::Result;
|
||||
use crate::recording::encoder::{detect_hardware_encoders, EncoderCapability};
|
||||
use crate::recording::RecordingEngine;
|
||||
use crate::recording::{RecordingEngine, RecordingResult};
|
||||
use crate::state::DaemonStatus;
|
||||
use crate::timeline::TimelineStore;
|
||||
|
||||
@@ -109,16 +109,40 @@ impl IpcHandlers {
|
||||
// Persist settings
|
||||
crate::config::save_config(&new_settings)?;
|
||||
|
||||
self.push_settings_to_engine().await?;
|
||||
|
||||
info!("Settings updated");
|
||||
Ok(serde_json::json!({ "success": true }))
|
||||
}
|
||||
|
||||
/// Hand the current settings to the recording engine.
|
||||
///
|
||||
/// The engine keeps its own copy and rebuilds its audio sources from it, so a change made
|
||||
/// here has to travel there as well. The rebuild touches OBS, which must not happen on an
|
||||
/// async worker.
|
||||
async fn push_settings_to_engine(&self) -> Result<()> {
|
||||
let settings = self.settings.read().clone();
|
||||
let engine = self.recording.clone();
|
||||
tokio::task::spawn_blocking(move || {
|
||||
if let Some(engine) = engine.write().as_mut() {
|
||||
engine.update_settings(settings)?;
|
||||
}
|
||||
Ok::<_, crate::error::DaemonError>(())
|
||||
})
|
||||
.await
|
||||
.map_err(|e| {
|
||||
crate::error::IpcError::InvalidCommand(format!("settings task failed: {e}"))
|
||||
})??;
|
||||
Ok(())
|
||||
}
|
||||
async fn handle_reset_settings(&self) -> Result<serde_json::Value> {
|
||||
let default_settings = Settings::default();
|
||||
*self.settings.write() = default_settings.clone();
|
||||
|
||||
crate::config::save_config(&default_settings)?;
|
||||
|
||||
self.push_settings_to_engine().await?;
|
||||
|
||||
info!("Settings reset to defaults");
|
||||
Ok(serde_json::to_value(default_settings)?)
|
||||
}
|
||||
@@ -187,37 +211,49 @@ impl IpcHandlers {
|
||||
}
|
||||
|
||||
async fn handle_start_recording(&self) -> Result<serde_json::Value> {
|
||||
let mut recording_guard = self.recording.write();
|
||||
|
||||
if let Some(ref mut engine) = *recording_guard {
|
||||
// Starting an output touches OBS and hides the screen it renders; on an async worker it
|
||||
// would stall the runtime (and libobs-wrapper's own runtime panics when it is blocked).
|
||||
let recording = self.recording.clone();
|
||||
tokio::task::spawn_blocking(move || -> Result<()> {
|
||||
let mut recording_guard = recording.write();
|
||||
let engine = recording_guard.as_mut().ok_or_else(|| {
|
||||
crate::error::RecordingError::ObsInitError(
|
||||
"Recording engine not initialized".to_string(),
|
||||
)
|
||||
})?;
|
||||
if engine.is_recording() {
|
||||
return Err(crate::error::RecordingError::AlreadyRecording.into());
|
||||
}
|
||||
engine.start_recording(None, None)
|
||||
})
|
||||
.await
|
||||
.map_err(|e| {
|
||||
crate::error::IpcError::InvalidCommand(format!("start recording task failed: {e}"))
|
||||
})??;
|
||||
|
||||
engine.start_recording(None, None)?;
|
||||
|
||||
Ok(serde_json::json!({ "success": true }))
|
||||
} else {
|
||||
Err(crate::error::RecordingError::ObsInitError(
|
||||
"Recording engine not initialized".to_string(),
|
||||
)
|
||||
.into())
|
||||
}
|
||||
Ok(serde_json::json!({ "success": true }))
|
||||
}
|
||||
|
||||
async fn handle_stop_recording(&self) -> Result<serde_json::Value> {
|
||||
let mut recording_guard = self.recording.write();
|
||||
// Stopping waits for the muxer to close the file, which is exactly the blocking call
|
||||
// that must stay off the async runtime.
|
||||
let recording = self.recording.clone();
|
||||
let result = tokio::task::spawn_blocking(move || -> Result<RecordingResult> {
|
||||
let mut recording_guard = recording.write();
|
||||
let engine = recording_guard.as_mut().ok_or_else(|| {
|
||||
crate::error::RecordingError::ObsInitError(
|
||||
"Recording engine not initialized".to_string(),
|
||||
)
|
||||
})?;
|
||||
engine.stop_recording()
|
||||
})
|
||||
.await
|
||||
.map_err(|e| {
|
||||
crate::error::IpcError::InvalidCommand(format!("stop recording task failed: {e}"))
|
||||
})??;
|
||||
|
||||
if let Some(ref mut engine) = *recording_guard {
|
||||
let result = engine.stop_recording()?;
|
||||
|
||||
// Save to timeline
|
||||
self.timeline.write().add_recording(result.clone())?;
|
||||
|
||||
Ok(serde_json::to_value(result)?)
|
||||
} else {
|
||||
Err(crate::error::RecordingError::NotRecording.into())
|
||||
}
|
||||
self.timeline.write().add_recording(result.clone())?;
|
||||
Ok(serde_json::to_value(result)?)
|
||||
}
|
||||
|
||||
async fn handle_shutdown(&self) -> Result<serde_json::Value> {
|
||||
@@ -288,6 +324,8 @@ impl IpcHandlers {
|
||||
crate::config::save_config(&settings)?;
|
||||
}
|
||||
|
||||
self.push_settings_to_engine().await?;
|
||||
|
||||
let response = AudioSourceResponse { source };
|
||||
Ok(serde_json::to_value(response)?)
|
||||
}
|
||||
@@ -301,6 +339,8 @@ impl IpcHandlers {
|
||||
crate::config::save_config(&settings)?;
|
||||
}
|
||||
|
||||
self.push_settings_to_engine().await?;
|
||||
|
||||
Ok(serde_json::json!({ "success": true }))
|
||||
}
|
||||
|
||||
@@ -337,6 +377,8 @@ impl IpcHandlers {
|
||||
}
|
||||
}
|
||||
|
||||
self.push_settings_to_engine().await?;
|
||||
|
||||
let response = AudioSourceResponse { source };
|
||||
Ok(serde_json::to_value(response)?)
|
||||
}
|
||||
|
||||
Reference in new issue
Block a user