get the app output device

This commit is contained in:
dlprows 2023-10-09 23:05:40 -06:00
parent 0f7f1fffcd
commit f1d7aeed5d

View File

@ -3,6 +3,7 @@ using System;
using System.Collections.Generic; using System.Collections.Generic;
using System.Diagnostics; using System.Diagnostics;
using System.Linq; using System.Linq;
using System.Runtime.InteropServices;
namespace FocusVolumeControl.AudioHelpers; namespace FocusVolumeControl.AudioHelpers;
@ -38,6 +39,7 @@ public class AudioHelper
public IAudioSession FindSession(List<Process> processes) public IAudioSession FindSession(List<Process> processes)
{ {
//var blah = new AudioPolicyConfigFactoryImplFor21H2();
var results = new ActiveAudioSessionWrapper(); var results = new ActiveAudioSessionWrapper();
Process bestProcessMatch = null; Process bestProcessMatch = null;
@ -72,6 +74,16 @@ public class AudioHelper
continue; continue;
} }
/*
blah.GetPersistedDefaultAudioEndpoint(sessionProcessId, DataFlow.Render, Role.Multimedia, out var persistedDeviceId);
persistedDeviceId = UnpackDeviceId(persistedDeviceId);
if(!string.IsNullOrEmpty(persistedDeviceId) && persistedDeviceId != currentDeviceId)
{
continue;
}
*/
var index = processes.FindIndex(x => x.Id == sessionProcessId || x.ProcessName == audioProcess?.ProcessName); var index = processes.FindIndex(x => x.Id == sessionProcessId || x.ProcessName == audioProcess?.ProcessName);
if (index > -1) if (index > -1)
@ -287,4 +299,124 @@ public class AudioHelper
return new SystemVolumeAudioSession(endpointVolume); return new SystemVolumeAudioSession(endpointVolume);
} }
class AudioPolicyConfigFactoryImplFor21H2
{
private readonly IAudioPolicyConfigFactoryVariantFor21H2 _factory;
internal AudioPolicyConfigFactoryImplFor21H2()
{
var iid = typeof(IAudioPolicyConfigFactoryVariantFor21H2).GUID;
Combase.RoGetActivationFactory("Windows.Media.Internal.AudioPolicyConfig", ref iid, out object factory);
_factory = (IAudioPolicyConfigFactoryVariantFor21H2)factory;
}
public uint ClearAllPersistedApplicationDefaultEndpoints()
{
return _factory.ClearAllPersistedApplicationDefaultEndpoints();
}
public uint GetPersistedDefaultAudioEndpoint(int processId, DataFlow flow, Role role, out string deviceId)
{
return _factory.GetPersistedDefaultAudioEndpoint(processId, flow, role, out deviceId);
}
public uint SetPersistedDefaultAudioEndpoint(int processId, DataFlow flow, Role role, IntPtr deviceId)
{
return _factory.SetPersistedDefaultAudioEndpoint(processId, flow, role, deviceId);
}
}
[Guid("ab3d4648-e242-459f-b02f-541c70306324")]
[InterfaceType(ComInterfaceType.InterfaceIsIInspectable)]
public interface IAudioPolicyConfigFactoryVariantFor21H2
{
int __incomplete__add_CtxVolumeChange();
int __incomplete__remove_CtxVolumeChanged();
int __incomplete__add_RingerVibrateStateChanged();
int __incomplete__remove_RingerVibrateStateChange();
int __incomplete__SetVolumeGroupGainForId();
int __incomplete__GetVolumeGroupGainForId();
int __incomplete__GetActiveVolumeGroupForEndpointId();
int __incomplete__GetVolumeGroupsForEndpoint();
int __incomplete__GetCurrentVolumeContext();
int __incomplete__SetVolumeGroupMuteForId();
int __incomplete__GetVolumeGroupMuteForId();
int __incomplete__SetRingerVibrateState();
int __incomplete__GetRingerVibrateState();
int __incomplete__SetPreferredChatApplication();
int __incomplete__ResetPreferredChatApplication();
int __incomplete__GetPreferredChatApplication();
int __incomplete__GetCurrentChatApplications();
int __incomplete__add_ChatContextChanged();
int __incomplete__remove_ChatContextChanged();
[PreserveSig]
uint SetPersistedDefaultAudioEndpoint(int processId, DataFlow flow, Role role, IntPtr deviceId);
[PreserveSig]
uint GetPersistedDefaultAudioEndpoint(int processId, DataFlow flow, Role role, [Out, MarshalAs(UnmanagedType.HString)] out string deviceId);
[PreserveSig]
uint ClearAllPersistedApplicationDefaultEndpoints();
}
[Guid("2a59116d-6c4f-45e0-a74f-707e3fef9258")]
[InterfaceType(ComInterfaceType.InterfaceIsIInspectable)]
public interface IAudioPolicyConfigFactoryVariantForDownlevel
{
int __incomplete__add_CtxVolumeChange();
int __incomplete__remove_CtxVolumeChanged();
int __incomplete__add_RingerVibrateStateChanged();
int __incomplete__remove_RingerVibrateStateChange();
int __incomplete__SetVolumeGroupGainForId();
int __incomplete__GetVolumeGroupGainForId();
int __incomplete__GetActiveVolumeGroupForEndpointId();
int __incomplete__GetVolumeGroupsForEndpoint();
int __incomplete__GetCurrentVolumeContext();
int __incomplete__SetVolumeGroupMuteForId();
int __incomplete__GetVolumeGroupMuteForId();
int __incomplete__SetRingerVibrateState();
int __incomplete__GetRingerVibrateState();
int __incomplete__SetPreferredChatApplication();
int __incomplete__ResetPreferredChatApplication();
int __incomplete__GetPreferredChatApplication();
int __incomplete__GetCurrentChatApplications();
int __incomplete__add_ChatContextChanged();
int __incomplete__remove_ChatContextChanged();
[PreserveSig]
uint SetPersistedDefaultAudioEndpoint(int processId, DataFlow flow, Role role, IntPtr deviceId);
[PreserveSig]
uint GetPersistedDefaultAudioEndpoint(int processId, DataFlow flow, Role role, [Out, MarshalAs(UnmanagedType.HString)] out string deviceId);
[PreserveSig]
uint ClearAllPersistedApplicationDefaultEndpoints();
}
static class Combase
{
[DllImport("combase.dll", PreserveSig = false)]
public static extern void RoGetActivationFactory(
[MarshalAs(UnmanagedType.HString)] string activatableClassId,
[In] ref Guid iid,
[Out, MarshalAs(UnmanagedType.IInspectable)] out Object factory);
[DllImport("combase.dll", PreserveSig = false)]
public static extern void WindowsCreateString(
[MarshalAs(UnmanagedType.LPWStr)] string src,
[In] uint length,
[Out] out IntPtr hstring);
}
private const string DEVINTERFACE_AUDIO_RENDER = "#{e6327cad-dcec-4949-ae8a-991e976a79d2}";
private const string DEVINTERFACE_AUDIO_CAPTURE = "#{2eef81be-33fa-4800-9670-1cd474972c3f}";
private const string MMDEVAPI_TOKEN = @"\\?\SWD#MMDEVAPI#";
private string UnpackDeviceId(string deviceId)
{
if (deviceId.StartsWith(MMDEVAPI_TOKEN)) deviceId = deviceId.Remove(0, MMDEVAPI_TOKEN.Length);
if (deviceId.EndsWith(DEVINTERFACE_AUDIO_RENDER)) deviceId = deviceId.Remove(deviceId.Length - DEVINTERFACE_AUDIO_RENDER.Length);
if (deviceId.EndsWith(DEVINTERFACE_AUDIO_CAPTURE)) deviceId = deviceId.Remove(deviceId.Length - DEVINTERFACE_AUDIO_CAPTURE.Length);
return deviceId;
}
} }