diff --git a/crates/muninn-daemon/src/main.rs b/crates/muninn-daemon/src/main.rs index 783f683..07c50c4 100644 --- a/crates/muninn-daemon/src/main.rs +++ b/crates/muninn-daemon/src/main.rs @@ -128,6 +128,7 @@ struct Options { capture_video: bool, capture_audio: bool, audio_device: String, + audio_source_id_override: Option, video_sources: Vec, audio_sources: Vec, audio_sample_rate: u32, @@ -166,6 +167,18 @@ struct CatalogSource { label: String, } +#[derive(Clone, Copy, Debug, PartialEq, Eq)] +enum AudioSourceKind { + Loopback, + Input, +} + +#[derive(Clone, Debug, PartialEq, Eq)] +struct AudioSourceSpec { + kind: AudioSourceKind, + device: String, +} + #[derive(Clone, Debug, PartialEq, Eq)] enum MediaTransport { Srt, @@ -415,11 +428,11 @@ fn tick_capture_stream_commands( if command.host_id != options.host_id { continue; } - if latest_command_by_stream - .get(&command.stream_id) - .is_some_and(|command_id| command_id != &command.command_id) - { - continue; + if let Some(latest_command_id) = latest_command_by_stream.get(&command.stream_id) { + if latest_command_id != &command.command_id { + supersede_capture_stream_command(&mut node, command, latest_command_id)?; + continue; + } } match (command.action.as_str(), command.state.as_str()) { ("start", "pending") => { @@ -445,6 +458,31 @@ fn tick_capture_stream_commands( .collect()) } +fn supersede_capture_stream_command( + node: &mut cultmesh_rs::CultMeshNode, + command: MuninnCaptureStreamCommandRecord, + latest_command_id: &str, +) -> Result<()> { + if capture_stream_command_is_terminal(&command.state) { + return Ok(()); + } + let command_id = command.command_id.clone(); + node.put( + &command_id, + &MuninnCaptureStreamCommandRecord { + state: "completed".to_string(), + detail: format!("Superseded by newer command {latest_command_id}."), + updated_at: timestamp()?, + ..command + }, + )?; + Ok(()) +} + +fn capture_stream_command_is_terminal(state: &str) -> bool { + matches!(state, "completed" | "failed") +} + fn reap_capture_stream_children( options: &Options, active: &mut Vec, @@ -615,9 +653,11 @@ fn start_capture_stream_command( session.command_id ), updated_at: timestamp()?, - ..command + ..command.clone() }; node.put(&running.command_id, &running)?; + session.command_id = running.command_id.clone(); + session.command = running; return Ok(()); } } @@ -801,11 +841,10 @@ fn spawn_capture_stream_activation( args.push("--no-video".to_string()); } if command_requests_audio(command) { - if let Some(audio_device) = audio_source_device(command_audio_source_id(command)) { - args.extend(["--audio-device".to_string(), audio_device]); - } else { - args.extend(["--audio-device".to_string(), options.audio_device.clone()]); - } + args.extend([ + "--audio-source-id".to_string(), + command_audio_source_id(command).to_string(), + ]); } else { args.push("--no-audio".to_string()); } @@ -866,6 +905,11 @@ fn video_source_label_for_options(options: &Options) -> String { fn audio_source_id_for_options(options: &Options) -> String { if options.capture_audio { + if let Some(source_id) = options.audio_source_id_override.as_ref() { + if parse_audio_source_spec(source_id).is_some() { + return source_id.clone(); + } + } format!("wasapi-loopback:{}", options.audio_device) } else { MUNINN_DISABLED_AUDIO_SOURCE_ID.to_string() @@ -873,7 +917,13 @@ fn audio_source_id_for_options(options: &Options) -> String { } fn audio_source_label_for_options(options: &Options) -> String { - format!("{} loopback ({})", options.host_id, options.audio_device) + let spec = audio_source_spec_for_options(options); + match spec.kind { + AudioSourceKind::Loopback => { + format!("{} loopback ({})", options.host_id, spec.device) + } + AudioSourceKind::Input => format!("{} input ({})", options.host_id, spec.device), + } } fn video_source_catalog(options: &Options) -> Vec { @@ -904,11 +954,34 @@ fn video_source_output_index(source_id: &str) -> Option { .and_then(|value| value.parse::().ok()) } -fn audio_source_device(source_id: &str) -> Option { - source_id - .strip_prefix("wasapi-loopback:") - .map(|value| value.to_string()) - .filter(|value| !value.is_empty()) +fn parse_audio_source_spec(source_id: &str) -> Option { + let (kind, device) = if let Some(value) = source_id.strip_prefix("wasapi-loopback:") { + (AudioSourceKind::Loopback, value) + } else if let Some(value) = source_id.strip_prefix("wasapi-input:") { + (AudioSourceKind::Input, value) + } else { + return None; + }; + let device = device.trim(); + if device.is_empty() { + return None; + } + Some(AudioSourceSpec { + kind, + device: device.to_string(), + }) +} + +fn audio_source_spec_for_options(options: &Options) -> AudioSourceSpec { + if let Some(source_id) = options.audio_source_id_override.as_ref() { + if let Some(spec) = parse_audio_source_spec(source_id) { + return spec; + } + } + AudioSourceSpec { + kind: AudioSourceKind::Loopback, + device: options.audio_device.clone(), + } } fn safe_log_component(value: &str) -> String { @@ -5605,12 +5678,14 @@ fn capture_stream_status(options: Options) -> Result<()> { println!( "No Muninn capture stream commands found for host {}{}{}.", status_options.host_id, - if !status_options.stream_filter_explicit || status_options.stream_id.trim().is_empty() { + if !status_options.stream_filter_explicit || status_options.stream_id.trim().is_empty() + { "" } else { " stream " }, - if !status_options.stream_filter_explicit || status_options.stream_id.trim().is_empty() { + if !status_options.stream_filter_explicit || status_options.stream_id.trim().is_empty() + { "" } else { status_options.stream_id.as_str() @@ -6149,6 +6224,7 @@ fn muninn_rudp_video_gop_frames(options: &Options) -> u32 { } fn loopback_args(options: &Options) -> Vec { + let audio_source = audio_source_spec_for_options(options); vec![ "-NoProfile".to_string(), "-ExecutionPolicy".to_string(), @@ -6162,7 +6238,17 @@ fn loopback_args(options: &Options) -> Vec { "-Channels".to_string(), options.audio_channels.to_string(), "-Device".to_string(), - options.audio_device.clone(), + audio_source.device, + "-DataFlow".to_string(), + match audio_source.kind { + AudioSourceKind::Loopback => "Render".to_string(), + AudioSourceKind::Input => "Capture".to_string(), + }, + "-Loopback:$".to_string() + + match audio_source.kind { + AudioSourceKind::Loopback => "true", + AudioSourceKind::Input => "false", + }, ] } @@ -6453,6 +6539,7 @@ impl Options { capture_video: true, capture_audio: true, audio_device: "Realtek".to_string(), + audio_source_id_override: None, video_sources: Vec::new(), audio_sources: Vec::new(), audio_sample_rate: 48000, @@ -6567,6 +6654,10 @@ impl Options { "--no-video" => options.capture_video = false, "--no-audio" => options.capture_audio = false, "--audio-device" => options.audio_device = take_value(&mut args, "--audio-device")?, + "--audio-source-id" => { + options.audio_source_id_override = + Some(take_value(&mut args, "--audio-source-id")?) + } "--video-source" => options.video_sources.push(parse_catalog_source(&take_value( &mut args, "--video-source", @@ -6846,7 +6937,7 @@ fn parse_catalog_source(value: &str) -> Result { } fn help_text() -> &'static str { - "Usage: muninn [serve|activate|request-stream|capture-stream-status|request-move-light|move-light-status|move-identity-status|move-source-status|move-state-status|claim-move-host|quest-access-status] [--store ] [--activate-store ] [--stream-action ] [--target-host ] [--port ] [--obs-target-host ] [--obs-port ] [--media-transport ] [--media-packet-bytes ] [--rudp-video-bitrate-kbps ] [--rudp-latency-budget-ms ] [--video-source ] [--audio-source ] [--no-video] [--no-audio] [--loopback-script ] [--ffmpeg ] [--capture-command-rudp-bind ] [--capture-command-rudp-target ] [--obs-catalog-rudp-target ] [--move-state =] [--move-host ] [--move-evidence-stream ] [--move-evidence-verse ] [--quest-adb] [--quest-serial ] [--quest-input-stream ] [--quest-pose-stream ] [--quest-video-input-stream ] [--idunn-rudp-health ] [--idunn-daemon ] [--idunn-health-contract ] [--dry-run] [--health]\n\nMuninn is Odin's portable telemetry Verse assembler. serve publishes cheap typed telemetry affordances, optional Quest USB access surfaces, and the explicitly configured Move runtime; when serve receives --move-state, --move-host, or --move-evidence-stream it may publish source-local Move controller state, typed Move identity records, a CultMesh Move evidence stream, and keep USB-attached PS Moves claimed to that explicit Bluetooth host; serve also consumes typed capture stream commands from --activate-store or --capture-command-rudp-bind and owns the local ffmpeg/loopback activation child lifecycle, and may project the OBS stream catalog to a local OBS plugin over --obs-catalog-rudp-target; activate starts an explicitly requested local stream over SRT or CultNet RUDP as a daemon child; request-stream publishes a typed capture stream command for Muninn serve to execute, either into the activation store or over --capture-command-rudp-target; capture-stream-status reads typed capture stream command receipts; use --no-video or --no-audio to request one leg without the other when the transport is CultNet RUDP; request-move-light publishes a typed Move light command for Muninn serve to execute; move-light-status reads typed command receipts; move-identity-status reads typed Move identity records; move-source-status prints live Move source discovery; move-state-status reads typed controller-state records; claim-move-host assigns USB-attached PS Moves to a Bluetooth host; quest-access-status reads typed Quest access state. In --health mode, the Idunn RUDP flags publish typed daemon health to Idunn while preserving command-probe exit semantics." + "Usage: muninn [serve|activate|request-stream|capture-stream-status|request-move-light|move-light-status|move-identity-status|move-source-status|move-state-status|claim-move-host|quest-access-status] [--store ] [--activate-store ] [--stream-action ] [--target-host ] [--port ] [--obs-target-host ] [--obs-port ] [--media-transport ] [--media-packet-bytes ] [--rudp-video-bitrate-kbps ] [--rudp-latency-budget-ms ] [--video-source ] [--audio-source ] [--audio-source-id ] [--no-video] [--no-audio] [--loopback-script ] [--ffmpeg ] [--capture-command-rudp-bind ] [--capture-command-rudp-target ] [--obs-catalog-rudp-target ] [--move-state =] [--move-host ] [--move-evidence-stream ] [--move-evidence-verse ] [--quest-adb] [--quest-serial ] [--quest-input-stream ] [--quest-pose-stream ] [--quest-video-input-stream ] [--idunn-rudp-health ] [--idunn-daemon ] [--idunn-health-contract ] [--dry-run] [--health]\n\nMuninn is Odin's portable telemetry Verse assembler. serve publishes cheap typed telemetry affordances, optional Quest USB access surfaces, and the explicitly configured Move runtime; when serve receives --move-state, --move-host, or --move-evidence-stream it may publish source-local Move controller state, typed Move identity records, a CultMesh Move evidence stream, and keep USB-attached PS Moves claimed to that explicit Bluetooth host; serve also consumes typed capture stream commands from --activate-store or --capture-command-rudp-bind and owns the local ffmpeg/loopback activation child lifecycle, and may project the OBS stream catalog to a local OBS plugin over --obs-catalog-rudp-target; activate starts an explicitly requested local stream over SRT or CultNet RUDP as a daemon child; request-stream publishes a typed capture stream command for Muninn serve to execute, either into the activation store or over --capture-command-rudp-target; capture-stream-status reads typed capture stream command receipts; use --no-video or --no-audio to request one leg without the other when the transport is CultNet RUDP; request-move-light publishes a typed Move light command for Muninn serve to execute; move-light-status reads typed command receipts; move-identity-status reads typed Move identity records; move-source-status prints live Move source discovery; move-state-status reads typed controller-state records; claim-move-host assigns USB-attached PS Moves to a Bluetooth host; quest-access-status reads typed Quest access state. In --health mode, the Idunn RUDP flags publish typed daemon health to Idunn while preserving command-probe exit semantics." } fn parse_move_state_source(value: &str) -> Result { @@ -6949,6 +7040,8 @@ mod tests { "wasapi-loopback:Realtek=Raven Realtek loopback", "--audio-source", "wasapi-loopback:Headphones=Raven headphones loopback", + "--audio-source", + "wasapi-input:Microphone=Raven microphone input", ] .into_iter() .map(String::from), @@ -6978,11 +7071,33 @@ mod tests { CatalogSource { id: "wasapi-loopback:Headphones".to_string(), label: "Raven headphones loopback".to_string(), + }, + CatalogSource { + id: "wasapi-input:Microphone".to_string(), + label: "Raven microphone input".to_string(), } ] ); } + #[test] + fn parse_audio_source_spec_accepts_loopback_and_input_sources() { + assert_eq!( + parse_audio_source_spec("wasapi-loopback:Realtek"), + Some(AudioSourceSpec { + kind: AudioSourceKind::Loopback, + device: "Realtek".to_string(), + }) + ); + assert_eq!( + parse_audio_source_spec("wasapi-input:Microphone"), + Some(AudioSourceSpec { + kind: AudioSourceKind::Input, + device: "Microphone".to_string(), + }) + ); + } + #[test] fn obs_source_catalog_falls_back_to_configured_capture_pair() { let options = Options::parse( @@ -7067,6 +7182,25 @@ mod tests { ); } + #[test] + fn loopback_args_switch_to_capture_mode_for_input_sources() { + let options = Options::parse( + [ + "activate", + "--audio-source-id", + "wasapi-input:Microphone (USB Audio)", + ] + .into_iter() + .map(String::from), + ) + .unwrap(); + + let args = loopback_args(&options); + assert!(args.contains(&"Capture".to_string())); + assert!(args.contains(&"-Loopback:$false".to_string())); + assert!(args.contains(&"Microphone (USB Audio)".to_string())); + } + #[test] fn parse_rejects_activation_when_both_media_legs_are_disabled() { let error = Options::parse( @@ -7417,7 +7551,8 @@ mod tests { combined.command_id = "combined".to_string(); assert!(!capture_stream_commands_start_equivalent( - &video_only, &combined + &video_only, + &combined )); assert_eq!( command_audio_source_id(&video_only), @@ -8392,6 +8527,78 @@ mod tests { assert!(!options.stream_filter_explicit); } + #[test] + fn tick_capture_stream_commands_supersedes_older_nonterminal_receipts() { + let store_path = std::env::temp_dir().join(format!( + "muninn-capture-command-supersede-{}.cc", + timestamp_ns().unwrap() + )); + let options = Options::parse( + [ + "serve", + "--host", + "raven", + "--store", + store_path.to_str().unwrap(), + "--activate-store", + store_path.to_str().unwrap(), + ] + .into_iter() + .map(String::from), + ) + .unwrap(); + let mut older = build_capture_stream_command( + &Options::parse( + [ + "request-stream", + "--host", + "raven", + "--stream", + "muninn.probe.video.rudp", + ] + .into_iter() + .map(String::from), + ) + .unwrap(), + ) + .unwrap(); + older.command_id = "older-command".to_string(); + older.updated_at = "unix-1000".to_string(); + older.state = "pending".to_string(); + + let mut newer = older.clone(); + newer.command_id = "newer-command".to_string(); + newer.updated_at = "unix-2000".to_string(); + newer.state = "completed".to_string(); + newer.detail = "already handled elsewhere".to_string(); + + let mut node = open_node(&options, "muninn-capture-command-supersede-test").unwrap(); + node.put(&older.command_id, &older).unwrap(); + node.put(&newer.command_id, &newer).unwrap(); + drop(node); + + let active = &mut Vec::new(); + let active_stream_ids = tick_capture_stream_commands(&options, active).unwrap(); + + assert!(active_stream_ids.is_empty()); + + let node = open_node(&options, "muninn-capture-command-supersede-status").unwrap(); + let superseded = node + .get_required::("older-command") + .unwrap(); + let latest = node + .get_required::("newer-command") + .unwrap(); + assert_eq!(superseded.state, "completed"); + assert_eq!( + superseded.detail, + "Superseded by newer command newer-command." + ); + assert_eq!(latest.state, "completed"); + assert_eq!(latest.detail, "already handled elsewhere"); + let _ = fs::remove_file(store_path); + } + #[test] fn move_state_status_accepts_move_filter() { let options = Options::parse( diff --git a/scripts/restart-muninn.ps1 b/scripts/restart-muninn.ps1 index 237ec6b..6984b2d 100644 --- a/scripts/restart-muninn.ps1 +++ b/scripts/restart-muninn.ps1 @@ -4,6 +4,7 @@ param( [string] $StorePath = "C:\Meta\Odin\state\muninn.telemetry.cc", [string] $ActivateStorePath = "C:\Meta\Odin\state\muninn.activate.cc", [string] $LogRoot = "C:\Meta\Odin\logs\muninn", + [string] $LocalLoopbackScript = (Join-Path $PSScriptRoot "wasapi-loopback-capture.ps1"), [string] $LoopbackScript = "C:\Meta\Odin\Muninn\scripts\wasapi-loopback-capture.ps1", [string] $Ffmpeg = "C:\Users\Madman's Lullaby\AppData\Local\Microsoft\WinGet\Links\ffmpeg.exe", [string] $TargetHost = "192.168.1.66", @@ -28,6 +29,10 @@ param( $ErrorActionPreference = "Stop" +if (-not (Test-Path -LiteralPath $LocalLoopbackScript)) { + throw "Local Muninn loopback script not found at $LocalLoopbackScript" +} + function Set-AsciiFile { param( [Parameter(Mandatory = $true)] [string] $Path, @@ -296,15 +301,16 @@ $hostLabel = if ($hostArgumentIndex -ge 0 -and ($hostArgumentIndex + 1) -lt $arg } $hasExplicitVideoSources = @($arguments | Where-Object { $_ -eq '--video-source' }).Count -gt 0 $hasExplicitAudioSources = @($arguments | Where-Object { $_ -eq '--audio-source' }).Count -gt 0 -if (-not ('Muninn.RenderEndpointDiscovery' -as [type])) { +if (-not ('Muninn.AudioEndpointDiscovery' -as [type])) { Add-Type -TypeDefinition @" using System; using System.Collections.Generic; using System.Runtime.InteropServices; namespace Muninn { - public static class RenderEndpointDiscovery { + public static class AudioEndpointDiscovery { const int eRender = 0; + const int eCapture = 1; const uint DEVICE_STATE_ACTIVE = 0x00000001; static readonly PROPERTYKEY DeviceFriendlyNameKey = new PROPERTYKEY( new Guid("A45C254E-DF1C-4EFD-8020-67D146A850E0"), @@ -382,11 +388,11 @@ namespace Muninn { [DllImport("ole32.dll")] static extern int PropVariantClear(ref PROPVARIANT variant); - public static string[] FriendlyNames() { + public static string[] FriendlyNames(int dataFlow) { var names = new List(); IMMDeviceEnumerator enumerator = (IMMDeviceEnumerator)(new MMDeviceEnumerator()); IMMDeviceCollection devices; - Marshal.ThrowExceptionForHR(enumerator.EnumAudioEndpoints(eRender, DEVICE_STATE_ACTIVE, out devices)); + Marshal.ThrowExceptionForHR(enumerator.EnumAudioEndpoints(dataFlow, DEVICE_STATE_ACTIVE, out devices)); try { uint count; Marshal.ThrowExceptionForHR(devices.GetCount(out count)); @@ -458,16 +464,20 @@ if (-not $hasExplicitVideoSources) { } } if (-not $hasExplicitAudioSources) { - $audioNames = @([Muninn.RenderEndpointDiscovery]::FriendlyNames() | Sort-Object -Unique) - if ($audioNames.Count -lt 1) { + $loopbackNames = @([Muninn.AudioEndpointDiscovery]::FriendlyNames(0) | Sort-Object -Unique) + $inputNames = @([Muninn.AudioEndpointDiscovery]::FriendlyNames(1) | Sort-Object -Unique) + if ($loopbackNames.Count -lt 1 -and $inputNames.Count -lt 1) { $audioDeviceIndex = [Array]::IndexOf($arguments, '--audio-device') if ($audioDeviceIndex -ge 0 -and ($audioDeviceIndex + 1) -lt $arguments.Count) { - $audioNames = @($arguments[$audioDeviceIndex + 1]) + $loopbackNames = @($arguments[$audioDeviceIndex + 1]) } } - foreach ($audioName in $audioNames) { + foreach ($audioName in $loopbackNames) { $arguments += @('--audio-source', ('wasapi-loopback:{0}={1} loopback ({0})' -f $audioName.Trim(), $hostLabel)) } + foreach ($audioName in $inputNames) { + $arguments += @('--audio-source', ('wasapi-input:{0}={1} input ({0})' -f $audioName.Trim(), $hostLabel)) + } } '@ @@ -738,6 +748,7 @@ foreach (`$pattern in @( "@ $uploadSpecs = @( + @{ LocalPath = $LocalLoopbackScript; RemotePath = ($LoopbackScript -replace "\\", "/") }, @{ LocalPath = $localServePs; RemotePath = ($servePsPath -replace "\\", "/") }, @{ LocalPath = $localServeVbs; RemotePath = ($serveVbsPath -replace "\\", "/") }, @{ LocalPath = $localServeCmd; RemotePath = ($serveCmdPath -replace "\\", "/") }, diff --git a/scripts/wasapi-loopback-capture.ps1 b/scripts/wasapi-loopback-capture.ps1 new file mode 100644 index 0000000..249c5ae --- /dev/null +++ b/scripts/wasapi-loopback-capture.ps1 @@ -0,0 +1,474 @@ +param( + [string]$Output = "-", + [string]$Device = "", + [double]$Seconds = 0, + [int]$SampleRate = 48000, + [int]$Channels = 2, + [ValidateSet("Render", "Capture")] + [string]$DataFlow = "Render", + [bool]$Loopback = $true, + [ValidateSet("Console", "Multimedia", "Communications")] + [string]$Role = "Console" +) + +$ErrorActionPreference = "Stop" + +$source = @" +using System; +using System.IO; +using System.Runtime.InteropServices; +using System.Threading; + +namespace Mimir { + public static class WasapiLoopbackCapture { + const int eRender = 0; + const int eCapture = 1; + const int eConsole = 0; + const int eMultimedia = 1; + const int eCommunications = 2; + const int CLSCTX_ALL = 23; + const uint DEVICE_STATE_ACTIVE = 0x00000001; + const uint AUDCLNT_SHAREMODE_SHARED = 0; + const uint AUDCLNT_STREAMFLAGS_LOOPBACK = 0x00020000; + const int AUDCLNT_BUFFERFLAGS_SILENT = 0x2; + static readonly Guid IID_IAudioClient = new Guid("1CB9AD4C-DBFA-4c32-B178-C2F568A703B2"); + static readonly Guid IID_IAudioCaptureClient = new Guid("C8ADBD64-E71E-48a0-A4DE-185C395CD317"); + static readonly Guid PcmSubformat = new Guid("00000001-0000-0010-8000-00aa00389b71"); + static readonly Guid FloatSubformat = new Guid("00000003-0000-0010-8000-00aa00389b71"); + static readonly PROPERTYKEY DeviceFriendlyNameKey = new PROPERTYKEY( + new Guid("A45C254E-DF1C-4EFD-8020-67D146A850E0"), + 14); + + [StructLayout(LayoutKind.Sequential, Pack = 2)] + struct WAVEFORMATEX { + public ushort wFormatTag; + public ushort nChannels; + public uint nSamplesPerSec; + public uint nAvgBytesPerSec; + public ushort nBlockAlign; + public ushort wBitsPerSample; + public ushort cbSize; + } + + [StructLayout(LayoutKind.Sequential, Pack = 2)] + struct WAVEFORMATEXTENSIBLE { + public WAVEFORMATEX Format; + public ushort wValidBitsPerSample; + public uint dwChannelMask; + public Guid SubFormat; + } + + [StructLayout(LayoutKind.Sequential)] + struct PROPERTYKEY { + public Guid fmtid; + public uint pid; + + public PROPERTYKEY(Guid formatId, uint propertyId) { + fmtid = formatId; + pid = propertyId; + } + } + + [StructLayout(LayoutKind.Sequential)] + struct PROPVARIANT { + public ushort vt; + public ushort wReserved1; + public ushort wReserved2; + public ushort wReserved3; + public IntPtr pointerValue; + public int intValue; + } + + [ComImport] + [Guid("BCDE0395-E52F-467C-8E3D-C4579291692E")] + class MMDeviceEnumerator { } + + [ComImport] + [Guid("A95664D2-9614-4F35-A746-DE8DB63617E6")] + [InterfaceType(ComInterfaceType.InterfaceIsIUnknown)] + interface IMMDeviceEnumerator { + [PreserveSig] + int EnumAudioEndpoints(int dataFlow, uint stateMask, out IMMDeviceCollection devices); + [PreserveSig] + int GetDefaultAudioEndpoint(int dataFlow, int role, out IMMDevice endpoint); + } + + [ComImport] + [Guid("0BD7A1BE-7A1A-44DB-8397-CC5392387B5E")] + [InterfaceType(ComInterfaceType.InterfaceIsIUnknown)] + interface IMMDeviceCollection { + [PreserveSig] + int GetCount(out uint count); + [PreserveSig] + int Item(uint index, out IMMDevice device); + } + + [ComImport] + [Guid("D666063F-1587-4E43-81F1-B948E807363F")] + [InterfaceType(ComInterfaceType.InterfaceIsIUnknown)] + interface IMMDevice { + [PreserveSig] + int Activate(ref Guid iid, int dwClsCtx, IntPtr pActivationParams, out IAudioClient audioClient); + [PreserveSig] + int OpenPropertyStore(uint access, out IPropertyStore properties); + } + + [ComImport] + [Guid("886D8EEB-8CF2-4446-8D02-CDBA1DBDCF99")] + [InterfaceType(ComInterfaceType.InterfaceIsIUnknown)] + interface IPropertyStore { + [PreserveSig] + int GetCount(out uint propertyCount); + [PreserveSig] + int GetAt(uint propertyIndex, out PROPERTYKEY key); + [PreserveSig] + int GetValue(ref PROPERTYKEY key, out PROPVARIANT value); + [PreserveSig] + int SetValue(ref PROPERTYKEY key, ref PROPVARIANT value); + [PreserveSig] + int Commit(); + } + + [ComImport] + [Guid("1CB9AD4C-DBFA-4c32-B178-C2F568A703B2")] + [InterfaceType(ComInterfaceType.InterfaceIsIUnknown)] + interface IAudioClient { + [PreserveSig] + int Initialize(uint shareMode, uint streamFlags, long hnsBufferDuration, long hnsPeriodicity, IntPtr pFormat, IntPtr audioSessionGuid); + [PreserveSig] + int GetBufferSize(out uint bufferSize); + [PreserveSig] + int GetStreamLatency(out long latency); + [PreserveSig] + int GetCurrentPadding(out uint currentPadding); + [PreserveSig] + int IsFormatSupported(uint shareMode, IntPtr pFormat, out IntPtr closestMatch); + [PreserveSig] + int GetMixFormat(out IntPtr deviceFormat); + [PreserveSig] + int GetDevicePeriod(out long defaultDevicePeriod, out long minimumDevicePeriod); + [PreserveSig] + int Start(); + [PreserveSig] + int Stop(); + [PreserveSig] + int Reset(); + [PreserveSig] + int SetEventHandle(IntPtr eventHandle); + [PreserveSig] + int GetService(ref Guid iid, out IAudioCaptureClient captureClient); + } + + [ComImport] + [Guid("C8ADBD64-E71E-48a0-A4DE-185C395CD317")] + [InterfaceType(ComInterfaceType.InterfaceIsIUnknown)] + interface IAudioCaptureClient { + [PreserveSig] + int GetBuffer(out IntPtr data, out uint frames, out uint flags, out ulong devicePosition, out ulong qpcPosition); + [PreserveSig] + int ReleaseBuffer(uint frames); + [PreserveSig] + int GetNextPacketSize(out uint frames); + } + + [DllImport("ole32.dll")] + static extern int PropVariantClear(ref PROPVARIANT variant); + + public static void Capture(string outputPath, string deviceSubstring, double seconds, int targetRate, int targetChannels, string dataFlowName, bool loopback, string roleName) { + Stream output = IsStdoutPath(outputPath) ? Console.OpenStandardOutput() : new FileStream(outputPath, FileMode.Create, FileAccess.Write, FileShare.Read); + try { + IMMDeviceEnumerator enumerator = (IMMDeviceEnumerator)(new MMDeviceEnumerator()); + int dataFlow = DataFlowFromName(dataFlowName); + uint streamFlags = loopback ? AUDCLNT_STREAMFLAGS_LOOPBACK : 0u; + IMMDevice device = ResolveDevice(enumerator, dataFlow, roleName, deviceSubstring); + try { + IAudioClient audioClient; + Guid audioClientId = IID_IAudioClient; + Check(device.Activate(ref audioClientId, CLSCTX_ALL, IntPtr.Zero, out audioClient), "Activate IAudioClient"); + IntPtr mixFormatPtr; + Check(audioClient.GetMixFormat(out mixFormatPtr), "GetMixFormat"); + WAVEFORMATEX fmt = Marshal.PtrToStructure(mixFormatPtr); + Console.Error.WriteLine("MixFormat tag={0} channels={1} rate={2} bits={3} blockAlign={4} cbSize={5}", fmt.wFormatTag, fmt.nChannels, fmt.nSamplesPerSec, fmt.wBitsPerSample, fmt.nBlockAlign, fmt.cbSize); + IntPtr activeFormatPtr = mixFormatPtr; + IntPtr fallbackFormatPtr = IntPtr.Zero; + IntPtr closest = IntPtr.Zero; + int support = audioClient.IsFormatSupported(AUDCLNT_SHAREMODE_SHARED, activeFormatPtr, out closest); + Console.Error.WriteLine("MixFormatSupport hr=0x{0:X8} closest={1}", support, closest != IntPtr.Zero); + if (support != 0 && closest != IntPtr.Zero) { + activeFormatPtr = closest; + fmt = Marshal.PtrToStructure(activeFormatPtr); + Console.Error.WriteLine("ClosestFormat tag={0} channels={1} rate={2} bits={3} blockAlign={4} cbSize={5}", fmt.wFormatTag, fmt.nChannels, fmt.nSamplesPerSec, fmt.wBitsPerSample, fmt.nBlockAlign, fmt.cbSize); + } + int init = audioClient.Initialize(AUDCLNT_SHAREMODE_SHARED, streamFlags, 0, 0, activeFormatPtr, IntPtr.Zero); + if (init < 0) { + fallbackFormatPtr = MakeWaveFormatPcm((ushort)targetChannels, (uint)targetRate, 16); + activeFormatPtr = fallbackFormatPtr; + fmt = Marshal.PtrToStructure(activeFormatPtr); + Console.Error.WriteLine("RetryFormat tag={0} channels={1} rate={2} bits={3} blockAlign={4} cbSize={5}", fmt.wFormatTag, fmt.nChannels, fmt.nSamplesPerSec, fmt.wBitsPerSample, fmt.nBlockAlign, fmt.cbSize); + IntPtr retryClosest = IntPtr.Zero; + int retrySupport = audioClient.IsFormatSupported(AUDCLNT_SHAREMODE_SHARED, activeFormatPtr, out retryClosest); + Console.Error.WriteLine("RetryFormatSupport hr=0x{0:X8} closest={1}", retrySupport, retryClosest != IntPtr.Zero); + if (retrySupport != 0 && retryClosest != IntPtr.Zero) { + Marshal.FreeHGlobal(fallbackFormatPtr); + fallbackFormatPtr = IntPtr.Zero; + activeFormatPtr = retryClosest; + fmt = Marshal.PtrToStructure(activeFormatPtr); + Console.Error.WriteLine("RetryClosestFormat tag={0} channels={1} rate={2} bits={3} blockAlign={4} cbSize={5}", fmt.wFormatTag, fmt.nChannels, fmt.nSamplesPerSec, fmt.wBitsPerSample, fmt.nBlockAlign, fmt.cbSize); + } + init = audioClient.Initialize(AUDCLNT_SHAREMODE_SHARED, streamFlags, 0, 0, activeFormatPtr, IntPtr.Zero); + } + Check(init, loopback ? "Initialize loopback" : "Initialize capture"); + IAudioCaptureClient captureClient; + Guid captureClientId = IID_IAudioCaptureClient; + Check(audioClient.GetService(ref captureClientId, out captureClient), "GetService IAudioCaptureClient"); + Check(audioClient.Start(), "Start"); + DateTime end = seconds > 0 ? DateTime.UtcNow.AddSeconds(seconds) : DateTime.MaxValue; + byte[] pcm = new byte[8192]; + byte[] silence = new byte[0]; + int sourceRate = Math.Max(1, (int)fmt.nSamplesPerSec); + double sourceFramesPerTargetFrame = sourceRate / (double)Math.Max(1, targetRate); + long sourceFramesSeen = 0; + double nextOutputSourceFrame = 0.0; + float[] previousFrame = new float[Math.Max(1, targetChannels)]; + bool hasPreviousFrame = false; + int idleFramesPerChunk = Math.Max(1, (int)Math.Round(Math.Max(1, targetRate) * 0.005)); + int idleBytes = checked(idleFramesPerChunk * Math.Max(1, targetChannels) * 4); + try { + while (DateTime.UtcNow < end) { + uint packetFrames; + Check(captureClient.GetNextPacketSize(out packetFrames), "GetNextPacketSize"); + if (packetFrames == 0) { + if (silence.Length < idleBytes) { + silence = new byte[idleBytes]; + } + output.Write(silence, 0, idleBytes); + output.Flush(); + Thread.Sleep(5); + continue; + } + IntPtr data; + uint frames; + uint flags; + ulong devicePosition; + ulong qpcPosition; + Check(captureClient.GetBuffer(out data, out frames, out flags, out devicePosition, out qpcPosition), "GetBuffer"); + try { + int sourceChannels = Math.Max(1, (int)fmt.nChannels); + int sourceBytesPerFrame = (int)fmt.nBlockAlign; + int sourceBits = (int)fmt.wBitsPerSample; + int sourceBytesPerSample = Math.Max(1, sourceBits / 8); + int frameBytes = targetChannels * 4; + long packetStart = sourceFramesSeen; + long packetEnd = packetStart + frames; + int outputFrames = 0; + double countCursor = nextOutputSourceFrame; + while (countCursor < packetEnd) { + outputFrames++; + countCursor += sourceFramesPerTargetFrame; + } + int needed = checked(outputFrames * frameBytes); + if (pcm.Length < needed) pcm = new byte[needed]; + int dst = 0; + float[] left = new float[targetChannels]; + float[] right = new float[targetChannels]; + while (nextOutputSourceFrame < packetEnd) { + long leftAbsolute = (long)Math.Floor(nextOutputSourceFrame); + long rightAbsolute = Math.Min(packetEnd - 1, leftAbsolute + 1); + double fraction = nextOutputSourceFrame - leftAbsolute; + ReadFrame(leftAbsolute, packetStart, data, flags, sourceChannels, sourceBytesPerFrame, sourceBytesPerSample, sourceBits, fmt.wFormatTag, activeFormatPtr, previousFrame, hasPreviousFrame, left); + ReadFrame(rightAbsolute, packetStart, data, flags, sourceChannels, sourceBytesPerFrame, sourceBytesPerSample, sourceBits, fmt.wFormatTag, activeFormatPtr, previousFrame, hasPreviousFrame, right); + for (int ch = 0; ch < targetChannels; ch++) { + float sample = (float)(left[ch] + (right[ch] - left[ch]) * fraction); + byte[] bytes = BitConverter.GetBytes(sample); + Buffer.BlockCopy(bytes, 0, pcm, dst, 4); + dst += 4; + } + nextOutputSourceFrame += sourceFramesPerTargetFrame; + } + if (frames > 0) { + ReadFrame(packetEnd - 1, packetStart, data, flags, sourceChannels, sourceBytesPerFrame, sourceBytesPerSample, sourceBits, fmt.wFormatTag, activeFormatPtr, previousFrame, false, previousFrame); + hasPreviousFrame = true; + } + output.Write(pcm, 0, needed); + output.Flush(); + sourceFramesSeen = packetEnd; + } + finally { + Check(captureClient.ReleaseBuffer(frames), "ReleaseBuffer"); + } + } + } + finally { + audioClient.Stop(); + Marshal.FreeCoTaskMem(mixFormatPtr); + if (fallbackFormatPtr != IntPtr.Zero) Marshal.FreeHGlobal(fallbackFormatPtr); + if (closest != IntPtr.Zero) Marshal.FreeCoTaskMem(closest); + Marshal.ReleaseComObject(captureClient); + Marshal.ReleaseComObject(audioClient); + } + } + finally { + Marshal.ReleaseComObject(device); + Marshal.ReleaseComObject(enumerator); + } + } + finally { + if (!IsStdoutPath(outputPath)) output.Dispose(); + } + } + + static IMMDevice ResolveDevice(IMMDeviceEnumerator enumerator, int dataFlow, string roleName, string deviceSubstring) { + if (String.IsNullOrWhiteSpace(deviceSubstring)) { + IMMDevice defaultDevice; + Check(enumerator.GetDefaultAudioEndpoint(dataFlow, RoleFromName(roleName), out defaultDevice), "GetDefaultAudioEndpoint"); + Console.Error.WriteLine("SelectedAudioDevice dataFlow={0} default role={1} name=\"{2}\"", dataFlow == eCapture ? "capture" : "render", roleName, DeviceFriendlyName(defaultDevice)); + return defaultDevice; + } + + IMMDeviceCollection devices; + Check(enumerator.EnumAudioEndpoints(dataFlow, DEVICE_STATE_ACTIVE, out devices), "EnumAudioEndpoints"); + try { + uint count; + Check(devices.GetCount(out count), "GetCount"); + for (uint index = 0; index < count; index++) { + IMMDevice device; + Check(devices.Item(index, out device), "Item"); + string friendlyName = ""; + try { + friendlyName = DeviceFriendlyName(device); + if (friendlyName.IndexOf(deviceSubstring, StringComparison.OrdinalIgnoreCase) >= 0) { + Console.Error.WriteLine("SelectedAudioDevice dataFlow={0} match=\"{1}\" name=\"{2}\"", dataFlow == eCapture ? "capture" : "render", deviceSubstring, friendlyName); + return device; + } + } + catch { + Marshal.ReleaseComObject(device); + throw; + } + Marshal.ReleaseComObject(device); + } + } + finally { + Marshal.ReleaseComObject(devices); + } + + throw new InvalidOperationException("No active " + (dataFlow == eCapture ? "capture" : "render") + " endpoint matched --device \"" + deviceSubstring + "\""); + } + + static string DeviceFriendlyName(IMMDevice device) { + IPropertyStore store; + Check(device.OpenPropertyStore(0, out store), "OpenPropertyStore"); + try { + PROPERTYKEY key = DeviceFriendlyNameKey; + PROPVARIANT value; + Check(store.GetValue(ref key, out value), "GetValue FriendlyName"); + try { + if (value.vt == 31 && value.pointerValue != IntPtr.Zero) { + return Marshal.PtrToStringUni(value.pointerValue) ?? ""; + } + return ""; + } + finally { + PropVariantClear(ref value); + } + } + finally { + Marshal.ReleaseComObject(store); + } + } + + static bool IsStdoutPath(string outputPath) { + return String.Equals(outputPath, "-", StringComparison.Ordinal) || + String.Equals(outputPath, "stdout", StringComparison.OrdinalIgnoreCase); + } + + static void ReadFrame(long absoluteFrame, long packetStart, IntPtr data, uint flags, int sourceChannels, int sourceBytesPerFrame, int sourceBytesPerSample, int sourceBits, ushort tag, IntPtr formatPtr, float[] previousFrame, bool hasPreviousFrame, float[] target) { + if (absoluteFrame < packetStart) { + for (int ch = 0; ch < target.Length; ch++) target[ch] = hasPreviousFrame ? previousFrame[ch] : 0f; + return; + } + + int packetFrame = checked((int)(absoluteFrame - packetStart)); + for (int ch = 0; ch < target.Length; ch++) { + float sample = 0f; + if ((flags & AUDCLNT_BUFFERFLAGS_SILENT) == 0) { + int srcCh = Math.Min(ch, sourceChannels - 1); + IntPtr src = IntPtr.Add(data, packetFrame * sourceBytesPerFrame + srcCh * sourceBytesPerSample); + sample = ReadSample(src, sourceBits, tag, formatPtr); + } + target[ch] = sample; + } + } + + static void Check(int hr, string stage) { + if (hr < 0) { + Exception inner = Marshal.GetExceptionForHR(hr); + throw new InvalidOperationException(stage + " failed: 0x" + hr.ToString("X8") + " " + (inner == null ? "" : inner.Message), inner); + } + } + + static int RoleFromName(string roleName) { + if (String.Equals(roleName, "Multimedia", StringComparison.OrdinalIgnoreCase)) return eMultimedia; + if (String.Equals(roleName, "Communications", StringComparison.OrdinalIgnoreCase)) return eCommunications; + return eConsole; + } + + static int DataFlowFromName(string dataFlowName) { + if (String.Equals(dataFlowName, "Capture", StringComparison.OrdinalIgnoreCase)) return eCapture; + return eRender; + } + + static IntPtr MakeWaveFormatPcm(ushort channels, uint sampleRate, ushort bits) { + WAVEFORMATEX fmt = new WAVEFORMATEX(); + fmt.wFormatTag = 1; + fmt.nChannels = channels; + fmt.nSamplesPerSec = sampleRate; + fmt.wBitsPerSample = bits; + fmt.nBlockAlign = (ushort)(channels * bits / 8); + fmt.nAvgBytesPerSec = sampleRate * fmt.nBlockAlign; + fmt.cbSize = 0; + IntPtr ptr = Marshal.AllocHGlobal(Marshal.SizeOf(typeof(WAVEFORMATEX))); + Marshal.StructureToPtr(fmt, ptr, false); + return ptr; + } + + static float ReadSample(IntPtr source, int bits, ushort tag, IntPtr formatPtr) { + bool isFloat = tag == 3; + if (tag == 65534) { + WAVEFORMATEXTENSIBLE ext = Marshal.PtrToStructure(formatPtr); + isFloat = ext.SubFormat == FloatSubformat; + } + if (isFloat && bits == 32) { + return Math.Max(-1f, Math.Min(1f, (float)Marshal.PtrToStructure(source, typeof(float)))); + } + if (bits == 16) { + return Marshal.ReadInt16(source) / 32768f; + } + if (bits == 24) { + int b0 = Marshal.ReadByte(source, 0); + int b1 = Marshal.ReadByte(source, 1); + int b2 = Marshal.ReadByte(source, 2); + int value = b0 | (b1 << 8) | (b2 << 16); + if ((value & 0x800000) != 0) value |= unchecked((int)0xff000000); + return Math.Max(-1f, Math.Min(1f, value / 8388608f)); + } + if (bits == 32) { + if (tag == 65534) { + WAVEFORMATEXTENSIBLE ext = Marshal.PtrToStructure(formatPtr); + if (ext.SubFormat == PcmSubformat && ext.wValidBitsPerSample > 0 && ext.wValidBitsPerSample < 32) { + int value = Marshal.ReadInt32(source); + int shifted = value >> (32 - ext.wValidBitsPerSample); + return Math.Max(-1f, Math.Min(1f, shifted / (float)(1 << (ext.wValidBitsPerSample - 1)))); + } + } + return Math.Max(-1f, Math.Min(1f, Marshal.ReadInt32(source) / 2147483648f)); + } + return 0f; + } + } +} +"@ + +Add-Type -TypeDefinition $source +$effectiveOutput = if ($Output -eq "stdout") { "-" } else { $Output } +[Mimir.WasapiLoopbackCapture]::Capture($effectiveOutput, $Device, $Seconds, $SampleRate, $Channels, $DataFlow, $Loopback, $Role)