diff --git a/record-daemon/src/recording/obs_context.rs b/record-daemon/src/recording/obs_context.rs index c84fb96..dd327b6 100644 --- a/record-daemon/src/recording/obs_context.rs +++ b/record-daemon/src/recording/obs_context.rs @@ -18,10 +18,12 @@ use libobs_wrapper::context::ObsContext as LibObsContext; use libobs_wrapper::data::object::ObsObjectTrait; use libobs_wrapper::data::ObsData; use libobs_wrapper::data::ObsObjectBuilder; +use libobs_wrapper::enums::ObsLogLevel; +use libobs_wrapper::logger::ObsLogger; use libobs_wrapper::sources::{ObsSourceBuilder, ObsSourceRef}; use libobs_wrapper::utils::{SourceInfo, StartupInfo}; use serde_json::json; -use tracing::{info, warn}; +use tracing::{debug, error, info, warn}; use super::encoder::{best_available_encoder, EncoderCapability}; use crate::config::{AudioSettings, AudioSourceType, VideoSettings}; @@ -50,6 +52,36 @@ fn apply_volume(source: &ObsSourceRef, percent: u32) { } } +/// The `window` value an application audio capture source reads. +/// +/// win-wasapi parses it as `title:class:exe` and looks for a window whose fields match, and it +/// reads this key alone. Passing `executable`, `title` and `class` on their own left the source +/// with no target and the recordings silent. `:` and `#` are escaped the way libobs decodes them +/// (`libobs/util/windows/window-helpers.c`), so a title with a colon still yields three fields. +#[cfg(target_os = "windows")] +fn window_string(title: &str, class: &str, executable: &str) -> String { + let escape = |field: &str| field.replace('#', "#22").replace(':', "#3A"); + format!("{}:{}:{}", escape(title), escape(class), escape(executable)) +} + +/// libobs writes its own warnings to stdout, and a daemon started in the background has no one +/// reading that. A failed game hook or a dropped audio device is exactly what explains a silent +/// recording, so OBS's log goes into the daemon log with everything else. +#[derive(Debug)] +struct ObsLog; + +impl ObsLogger for ObsLog { + fn log(&mut self, level: ObsLogLevel, msg: String) { + let msg = msg.trim_end(); + match level { + ObsLogLevel::Error => error!(target: "obs", "{msg}"), + ObsLogLevel::Warning => warn!(target: "obs", "{msg}"), + ObsLogLevel::Info => info!(target: "obs", "{msg}"), + ObsLogLevel::Debug => debug!(target: "obs", "{msg}"), + } + } +} + /// Builder for OBS context. pub struct ObsContextBuilder { video_settings: Option, @@ -157,7 +189,9 @@ impl ObsContext { warn!("libobs version mismatch with wrapper, continuing anyway"); } - let info = StartupInfo::default().set_video_info(video_info); + let info = StartupInfo::default() + .set_video_info(video_info) + .set_logger(Box::new(ObsLog)); let context = catch_native("creating the OBS context", || { LibObsContext::new(info).map_err(|e| { RecordingError::ObsInitError(format!("failed to create OBS context: {:?}", e)) @@ -413,9 +447,11 @@ fn add_audio_sources( game_plugin, "game_audio", json!({ - "executable": "League of Legends.exe", - "title": "League of Legends (TM) Client", - "class": "RiotWindowClass", + "window": window_string( + "League of Legends (TM) Client", + "RiotWindowClass", + "League of Legends.exe" + ), "priority": 2 }), audio.game_volume, @@ -441,7 +477,7 @@ fn add_audio_sources( let (plugin, settings) = match app.source_type { AudioSourceType::Application => ( game_plugin, - json!({ "executable": app.device, "priority": 2 }), + json!({ "window": window_string("", "", &app.device), "priority": 2 }), ), AudioSourceType::DeviceOutput => (out_plugin, json!({ "device_id": app.device })), AudioSourceType::DeviceInput => (mic_plugin, json!({ "device_id": app.device })),