program AudioDevice;
{$APPTYPE CONSOLE}
{$R *.res}
uses
System.SysUtils,
Winapi.Windows,
Winapi.ActiveX,
Winapi.PropSys;
const
IID_IAudioSessionControl: TGUID = '
{F4B1A599-7266-4319-A8CA-E70ACB11E8CD}';
IID_IAudioSessionControl2: TGUID = '
{bfb7ff88-7239-4fc9-8fa2-07c950be9c6d}';
IID_IAudioSessionManager: TGUID = '
{BFA971F1-4D5E-40BB-935E-967039BFBEE4}';
IID_IAudioSessionManager2: TGUID = '
{77AA99A0-1BD6-484F-8BC7-2C654C9A9B6F}';
CLASS_IMMDeviceEnumerator: TGUID = '
{BCDE0395-E52F-467C-8E3D-C4579291692E}';
IID_IMMDeviceEnumerator: TGUID = '
{A95664D2-9614-4F35-A746-DE8DB63617E6}';
IID_IMMDevice: TGUID = '
{D666063F-1587-4E43-81F1-B948E807363F}';
IID_IMMDeviceCollection: TGUID = '
{0BD7A1BE-7A1A-44DB-8397-CC5392387B5E}';
IID_IMMAudioEndpointVolume: TGUID = '
{5CDF2C82-841E-4546-9722-0CF74078229A}';
IID_IAudioMeterInformation: TGUID = '
{C02216F6-8C67-4B5B-9D00-D008E73E0064}';
IID_IAudioEndpointVolumeCallback: TGUID = '
{657804FA-D6AD-4496-8A60-352752AF4F89}';
IID_IMMNotificationClient: TGUID = '
{7991EEC9-7E89-4D85-8390-6C703CEC60C0}';
DEVICE_STATE_ACTIVE = $00000001;
DEVICE_STATE_UNPLUGGED = $00000002;
DEVICE_STATE_NOTPRESENT = $00000004;
DEVICE_STATEMASK_ALL = $00000007;
type
EDataFlow = TOleEnum;
const
eRender = $00000000;
eCapture = $00000001;
eAll = $00000002;
EDataFlow_enum_count = $00000003;
type
ERole = TOleEnum;
const
eConsole = $00000000;
eMultimedia = $00000001;
eCommunications = $00000002;
ERole_enum_count = $00000003;
type
IAudioSessionEvents =
interface
end;
IAudioSessionControl =
interface(IUnknown)
['
{F4B1A599-7266-4319-A8CA-E70ACB11E8CD}']
function GetState(
out pRetVal: uint): HRESULT;
stdcall;
function GetDisplayName(
out pRetVal: LPWSTR): HRESULT;
stdcall;
function SetDisplayName(Value: LPCWSTR; EventContext: pGuid): HRESULT;
stdcall;
function GetIconPath(
out pRetVal: LPWSTR): HRESULT;
stdcall;
function SetIconPath(Value: LPCWSTR; EventContext: pGuid): HRESULT;
stdcall;
function GetGroupingParam(pRetVal: pGuid): HRESULT;
stdcall;
function SetGroupingParam(OverrideValue, EventContext: pGuid): HRESULT;
stdcall;
function RegisterAudioSessionNotification(
const NewNotifications: IAudioSessionEvents)
: HRESULT;
stdcall;
function UnregisterAudioSessionNotification(
const NewNotifications: IAudioSessionEvents)
: HRESULT;
stdcall;
end;
IAudioSessionControl2 =
interface(IAudioSessionControl)
[IID_IAudioSessionControl2]
function GetSessionIdentifier(
out pRetVal: LPWSTR): HRESULT;
stdcall;
function GetSessionInstanceIdentifier(
out pRetVal: LPWSTR): HRESULT;
stdcall;
function GetProcessId(
out pRetVal: DWord): HRESULT;
stdcall;
// indicates whether the session is a system sounds session.
function IsSystemSoundsSession(): HRESULT;
stdcall;
// S_OK = The session is a system sounds session.
// S_FALSE = The session is not a system sounds session.
function SetDuckingPreference(
const optOut: Boolean): HRESULT;
stdcall;
// A BOOL variable that enables or disables system auto-ducking.
end;
IAudioSessionEnumerator =
interface(IUnknown)
['
{E2F5BB11-0570-40CA-ACDD-3AA01277DEE8}']
function GetCount(
out SessionCount: Integer): HRESULT;
stdcall;
function GetSession(
const SessionCount: Integer;
out Session: IAudioSessionControl): HRESULT;
stdcall;
end;
IAudioSessionNotification =
interface
end;
IAudioVolumeDuckNotification =
interface
end;
ISimpleAudioVolume =
interface(IUnknown)
['
{87CE5498-68D6-44E5-9215-6DA47EF883D8}']
function SetMasterVolume(fLevel: Single; EventContext: pGuid): HRESULT;
stdcall;
function GetMasterVolume(
out fLevel: Single): HRESULT;
stdcall;
function SetMute(bMute: LongBool; EventContext: pGuid): HRESULT;
stdcall;
function GetMute(
out bMute: LongBool): HRESULT;
stdcall;
end;
IAudioSessionManager =
interface(IUnknown)
[IID_IAudioSessionManager]
function GetAudioSessionControl(AudioSessionGuid: pGuid; StreamFlag: uint;
out SessionControl: IAudioSessionControl)
: HRESULT;
stdcall;
function GetSimpleAudioVolume(AudioSessionGuid: pGuid; StreamFlag: uint;
out AudioVolume: ISimpleAudioVolume)
: HRESULT;
stdcall;
end;
IAudioSessionManager2 =
interface(IAudioSessionManager)
[IID_IAudioSessionManager2]
function GetSessionEnumerator(
out SessionEnum: IAudioSessionEnumerator): HRESULT;
stdcall;
function RegisterSessionNotification(SessionNotification: IAudioSessionNotification): HRESULT;
stdcall;
function UnregisterSessionNotification(SessionNotification: IAudioSessionNotification): HRESULT;
stdcall;
function RegisterDuckNotification(
const sessionID: LPCWSTR;
const duckNotification: IAudioVolumeDuckNotification)
: HRESULT;
stdcall;
function UnregisterDuckNotification(
const duckNotification: IAudioVolumeDuckNotification): HRESULT;
stdcall;
end;
IAudioEndpointVolumeCallback =
interface(IUnknown)
[IID_IAudioEndpointVolumeCallback]
end;
IMMAudioEndpointVolume =
interface(IUnknown)
[IID_IMMAudioEndpointVolume]
function RegisterControlChangeNotify(AudioEndPtVol: IAudioEndpointVolumeCallback): Integer;
stdcall;
function UnregisterControlChangeNotify(AudioEndPtVol: IAudioEndpointVolumeCallback): Integer;
stdcall;
function GetChannelCount(
out PInteger): Integer;
stdcall;
function SetMasterVolumeLevel(fLevelDB: Double; pguidEventContext: TGUID): Integer;
stdcall;
function SetMasterVolumeLevelScalar(fLevelDB: Double; pguidEventContext: TGUID): Integer;
stdcall;
function GetMasterVolumeLevel(
out fLevelDB: Double): Integer;
stdcall;
function GetMasterVolumeLevelScaler(
out fLevel: Double): Integer;
stdcall;
function SetChannelVolumeLevel(nChannel: Integer; fLevelDB: Double; pguidEventContext: TGUID): Integer;
stdcall;
function SetChannelVolumeLevelScalar(nChannel: Integer; fLevelDB: Double; pguidEventContext: TGUID): Integer;
stdcall;
function GetChannelVolumeLevel(nChannel: Integer;
out fLevelDB: Double): Integer;
stdcall;
function GetChannelVolumeLevelScalar(nChannel: Integer;
out fLevel: Double): Integer;
stdcall;
function SetMute(bMute: Boolean; pguidEventContext: TGUID): Integer;
stdcall;
function GetMute(
out bMute: Boolean): Integer;
stdcall;
function GetVolumeStepInfo(pnStep: Integer;
out pnStepCount: Integer): Integer;
stdcall;
function VolumeStepUp(pguidEventContext: TGUID): Integer;
stdcall;
function VolumeStepDown(pguidEventContext: TGUID): Integer;
stdcall;
function QueryHardwareSupport(
out pdwHardwareSupportMask): Integer;
stdcall;
function GetVolumeRange(
out pflVolumeMindB: Double;
out pflVolumeMaxdB: Double;
out pflVolumeIncrementdB: Double)
: Integer;
stdcall;
end;
IMMDevice =
interface(IUnknown)
[IID_IMMDevice]
function Activate(
const iid: TGUID; dwClsCtx: uint; pActivationParams: PPropVariant;
out ppInterface: IUnknown)
: HRESULT;
stdcall;
function OpenPropertyStore(stgmAccess: Integer;
out ppProperties: IPropertyStore): HRESULT;
stdcall;
function GetId(ppstrId: PWChar): HRESULT;
stdcall;
function GetState(
var pdwState: uint): HRESULT;
stdcall;
end;
IMMDeviceCollection =
interface(IUnknown)
[IID_IMMDeviceCollection]
function GetCount(
var pcDevices: uint): HRESULT;
stdcall;
function Item(nDevice: uint;
out ppDevice: IMMDevice): HRESULT;
stdcall;
end;
IMMNotificationClient =
interface(IUnknown)
[IID_IMMNotificationClient]
end;
IMMDeviceEnumerator =
interface(IUnknown)
[IID_IMMDeviceEnumerator]
function EnumAudioEndpoints(dataFlow: EDataFlow; dwStateMask: DWord;
out ppDevices: IMMDeviceCollection): HRESULT;
stdcall;
function GetDefaultAudioEndpoint(dataFlow: EDataFlow; role: ERole;
out ppEndpoint: IMMDevice): HRESULT;
stdcall;
function GetDevice(pwstrId: PWChar;
out ppDevice: IMMDevice): HRESULT;
stdcall;
function RegisterEndpointNotificationCallback(
var pClient: IMMNotificationClient): HRESULT;
stdcall;
function UnregisterEndpointNotificationCallback(
var pClient: IMMNotificationClient): HRESULT;
stdcall;
end;
procedure PrintDefaultSessionControlDisplayName;
var
DeviceEnumerator: IMMDeviceEnumerator;
SessionManager: IAudioSessionManager;
SessionManager2: IAudioSessionManager2;
AudioDevice: IMMDevice;
SessionControl2: IAudioSessionControl2;
SessionEnumerator: IAudioSessionEnumerator;
SessionControl: IAudioSessionControl;
HR: HRESULT;
SessionCount: Integer;
pRetVal: PWideChar;
begin
SessionCount := 0;
HR := CoInitializeEx(
nil, 0);
if Succeeded(HR)
then
begin
HR := CoCreateInstance(CLASS_IMMDeviceEnumerator,
nil, CLSCTX_ALL, IID_IMMDeviceEnumerator, DeviceEnumerator);
if Succeeded(HR)
then
begin
HR := DeviceEnumerator.GetDefaultAudioEndpoint(eRender, eConsole, AudioDevice);
if Succeeded(HR)
then
begin
HR := AudioDevice.Activate(IID_IAudioSessionManager, CLSCTX_INPROC_SERVER,
nil, IUnknown(SessionManager));
if Succeeded(HR)
then
begin
if Supports(SessionManager, IID_IAudioSessionManager2, SessionManager2)
then
begin
HR := SessionManager2.GetSessionEnumerator(SessionEnumerator);
if Succeeded(HR)
then
begin
HR := SessionEnumerator.GetSession(SessionCount, SessionControl);
if Succeeded(HR)
then
begin
if Supports(SessionControl, IID_IAudioSessionControl2, SessionControl2)
then
begin
SessionControl2.GetDisplayName(pRetVal);
Writeln('
DisplayName: ', pRetVal);
CoTaskMemFree(pRetVal);
SessionControl2.GetIconPath(pRetVal);
Writeln('
IconPath: ', pRetVal);
CoTaskMemFree(pRetVal);
end;
end;
end;
end;
end;
end;
end;
CoUninitialize;
end;
end;
begin
try
PrintDefaultSessionControlDisplayName;
Readln;
except
on E:
Exception do
begin
Writeln(E.ClassName, '
: ', E.
Message);
Readln;
end;
end;
end.