public AEDev(bool resetDevice = false) { IMMDevice _Device = null; if (resetDevice) { _devId = ""; } _realEnumerator = new _AEDeviceEnumerator() as IMMDeviceEnumerator; try { if (String.IsNullOrEmpty(_devId)) { Marshal.ThrowExceptionForHR(_realEnumerator.GetDefaultAudioEndpoint(0, 1, out _Device)); Marshal.ThrowExceptionForHR(_Device.GetId(out _devId)); } else { Marshal.ThrowExceptionForHR(_realEnumerator.GetDevice(_devId, out _Device)); } devstatus state; Marshal.ThrowExceptionForHR(_Device.GetState(out state)); if (state != devstatus.DEVICE_STATE_ACTIVE) { throw new ApplicationException($"audio device is not active ({state.ToString()})"); } _RealDevice = _Device; object result; Marshal.ThrowExceptionForHR(_RealDevice.Activate(ref IID_IAudioEndpointVolume, CTX.ALL, IntPtr.Zero, out result)); _AudioEndPointVolume = result as IAudioEndpointVolume; _CallBack = new AudioEndpointVolumeCallback(this); Marshal.ThrowExceptionForHR(_AudioEndPointVolume.RegisterControlChangeNotify(_CallBack)); } catch (Exception) { // Catch if no device is found or changed device try { Marshal.ThrowExceptionForHR(_realEnumerator.GetDefaultAudioEndpoint(0, 1, out _Device)); Marshal.ThrowExceptionForHR(_Device.GetId(out _devId)); devstatus state; Marshal.ThrowExceptionForHR(_Device.GetState(out state)); if (state != devstatus.DEVICE_STATE_ACTIVE) { throw new ApplicationException($"audio device is not active ({state.ToString()})"); } _RealDevice = _Device; object result; Marshal.ThrowExceptionForHR(_RealDevice.Activate(ref IID_IAudioEndpointVolume, CTX.ALL, IntPtr.Zero, out result)); _AudioEndPointVolume = result as IAudioEndpointVolume; _CallBack = new AudioEndpointVolumeCallback(this); Marshal.ThrowExceptionForHR(_AudioEndPointVolume.RegisterControlChangeNotify(_CallBack)); } catch (Exception) { // Catch if no device is found } } }
public string GetDeviceId(IMMDevice device) { string device_id = ""; device.GetId(out device_id); return(device_id); }
private void CacheDevice(IMMDevice mDevice) { if (!DeviceIsValid(mDevice)) { return; } string id; mDevice.GetId(out id); var device = _deviceCache.FirstOrDefault(x => String.Equals(x.RealId, id, StringComparison.InvariantCultureIgnoreCase)); if (device != null) { return; } var lockAcquired = _lock.AcquireWriteLockNonReEntrant(); try { device = new CoreAudioDevice(mDevice, this); _deviceCache.Add(device); } finally { if (lockAcquired) { _lock.ExitWriteLock(); } } }
public void IMMDeviceEnumerator_GetDevice() { int result = 0; var enumerator = TestUtilities.CreateIMMDeviceEnumerator(); var allDevices = TestUtilities.CreateIMMDeviceCollection(EDataFlow.eAll, DEVICE_STATE_XXX.DEVICE_STATEMASK_ALL); foreach (var device in allDevices) { // Get the device ID. string deviceId = null; result = device.GetId(out deviceId); AssertCoreAudio.IsHResultOk(result); Assert.IsNotNull(deviceId, "The device string is null."); // Get the IMMDevice directly from the ID. IMMDevice deviceFromId = null; result = enumerator.GetDevice(deviceId, out deviceFromId); AssertCoreAudio.IsHResultOk(result); Assert.IsNotNull(deviceFromId, "The IMMDevice object is null."); // Ensure the IDs of each device match. string deviceId2 = null; result = deviceFromId.GetId(out deviceId2); AssertCoreAudio.IsHResultOk(result); Assert.IsNotNull(deviceId2, "The device string is null."); Assert.AreEqual(deviceId, deviceId2, "The device IDs are not equal."); } }
public AudioDevice(IAudioDeviceManager deviceManager, IMMDevice device, Dispatcher foregroundDispatcher) { _device = device; _deviceManager = new WeakReference <IAudioDeviceManager>(deviceManager); _dispatcher = foregroundDispatcher; _id = device.GetId(); Trace.WriteLine($"AudioDevice Create {_id}"); if (_device.GetState() == DeviceState.ACTIVE) { _deviceVolume = device.Activate <IAudioEndpointVolume>(); _deviceVolume.RegisterControlChangeNotify(this); _deviceVolume.GetMasterVolumeLevelScalar(out _volume); _isMuted = _deviceVolume.GetMute() != 0; _isRegistered = true; _meter = device.Activate <IAudioMeterInformation>(); _channels = new AudioDeviceChannelCollection(_deviceVolume, _dispatcher); _sessions = new AudioDeviceSessionCollection(this, _device, _dispatcher); _sessionFilter = new FilteredCollectionChain <IAudioDeviceSession>(_sessions.Sessions, _dispatcher); Groups = _sessionFilter.Items; } else { Groups = new ObservableCollection <IAudioDeviceSession>(); } ReadProperties(); }
internal static string GetID(IMMDevice realDevice) { string result; Marshal.ThrowExceptionForHR(realDevice.GetId(out result)); return(result); }
public AudioDeviceSessionCollection(IAudioDevice parent, IMMDevice device) { Trace.WriteLine($"AudioDeviceSessionCollection Create dev={device.GetId()}"); _parent = new WeakReference <IAudioDevice>(parent); _dispatcher = App.Current.Dispatcher; Task.Factory.StartNew(() => { try { _sessionManager = device.Activate <IAudioSessionManager2>(); _sessionManager.RegisterSessionNotification(this); var enumerator = _sessionManager.GetSessionEnumerator(); int count = enumerator.GetCount(); for (int i = 0; i < count; i++) { CreateAndAddSession(enumerator.GetSession(i)); } } catch (Exception ex) { Trace.TraceError($"{ex}"); } }); }
private CoreAudioDevice CacheDevice(IMMDevice mDevice) { if (!DeviceIsValid(mDevice)) { return(null); } string id; mDevice.GetId(out id); var device = GetDevice(id); if (device != null) { return(device); } var lockAcquired = _lock.AcquireWriteLockNonReEntrant(); try { device = new CoreAudioDevice(mDevice, this); _deviceCache.Add(device); return(device); } finally { if (lockAcquired) { _lock.ExitWriteLock(); } } }
public AEDev() { IMMDevice _Device = null; if (String.IsNullOrEmpty(_devId)) { Marshal.ThrowExceptionForHR(_realEnumerator.GetDefaultAudioEndpoint(0, 1, out _Device)); Marshal.ThrowExceptionForHR(_Device.GetId(out _devId)); } else { Marshal.ThrowExceptionForHR(_realEnumerator.GetDevice(_devId, out _Device)); } devstatus state; Marshal.ThrowExceptionForHR(_Device.GetState(out state)); if (state != devstatus.DEVICE_STATE_ACTIVE) { throw new ApplicationException(String.Format("audio device is not active ({0})", state.ToString())); } _RealDevice = _Device; object result; Marshal.ThrowExceptionForHR(_RealDevice.Activate(ref IID_IAudioEndpointVolume, CTX.ALL, IntPtr.Zero, out result)); _AudioEndPointVolume = result as IAudioEndpointVolume; _CallBack = new AudioEndpointVolumeCallback(this); Marshal.ThrowExceptionForHR(_AudioEndPointVolume.RegisterControlChangeNotify(_CallBack)); }
internal MMDevice(IMMDevice realDevice) { _RealDevice = realDevice; GetPropertyInformation(); Marshal.ThrowExceptionForHR(_RealDevice.GetId(out _id)); IMMEndpoint ep = _RealDevice as IMMEndpoint; Marshal.ThrowExceptionForHR(ep.GetDataFlow(out _dataFlow)); Marshal.ThrowExceptionForHR(_RealDevice.GetState(out _state)); if (_PropertyStore.Contains(PKEY.PKEY_DeviceInterface_FriendlyName)) { _friendlyName = (string)_PropertyStore[PKEY.PKEY_DeviceInterface_FriendlyName].Value; } if (_PropertyStore.Contains(PKEY.PKEY_DeviceInterface_Icon)) { var iconPath = (string)_PropertyStore[PKEY.PKEY_DeviceInterface_Icon].Value; _icon = DeviceIconHelper.GetIconByPath(iconPath); } if (_PropertyStore.Contains(PKEY.PKEY_DeviceInterface_RealName)) { var nameValue = _PropertyStore[PKEY.PKEY_DeviceInterface_RealName].Value; if (nameValue is string s) { _realName = s; } } }
internal MMDevice(IMMDevice realDevice) { Marshal.ThrowExceptionForHR(realDevice.GetId(out _Id)); _RealDevice = realDevice; GetProperty(); }
private AudioDeviceList GetAudioDeviceList() { string id; IMMDevice speakers = AudioUtilities.GetCurrentSpeakers(); speakers.GetId(out id); //get active playback devices, make sure we mark which one is the current default playback device return(new AudioDeviceList(AudioUtilities.GetAllActiveSpeakers().Select(d => new AudioDevice(d, d.Id == id)))); }
// Lists all devices, and for each device all processes that are currently playing sound using that device public static List <SoundInfoDevice> getSoundInfo() { List <SoundInfoDevice> soundInfoDevices = new List <SoundInfoDevice>(); DeviceEnumerator enumerator = new DeviceEnumerator(); IMMDeviceEnumerator deviceEnumerator = (IMMDeviceEnumerator)enumerator; IMMDeviceCollection deviceCollection = deviceEnumerator.EnumAudioEndpoints(EDataFlow.eRender, DeviceStatemask.DEVICE_STATE_ACTIVE); uint deviceCount = deviceCollection.GetCount(); for (uint i = 0; i < deviceCount; i++) { SoundInfoDevice soundInfoDevice = new SoundInfoDevice(); soundInfoDevices.Add(soundInfoDevice); IMMDevice device = deviceCollection.Item(i); string deviceId = device.GetId(); soundInfoDevice.ID = deviceId; IMMPropertyStore propertyStore = device.OpenPropertyStore(ProperyStoreMode.STGM_READ); PropertyKey propertyKeyDeviceDesc = new PropertyKey(); propertyKeyDeviceDesc.fmtid = new Guid(0xa45c254e, 0xdf1c, 0x4efd, 0x80, 0x20, 0x67, 0xd1, 0x46, 0xa8, 0x50, 0xe0); propertyKeyDeviceDesc.pid = 2; PropVariant deviceNamePtr = propertyStore.GetValue(ref propertyKeyDeviceDesc); string deviceName = Marshal.PtrToStringUni(deviceNamePtr.pszVal); soundInfoDevice.name = deviceName; Guid guidAudioSessionManager2 = new Guid("77AA99A0-1BD6-484F-8BC7-2C654C9A9B6F"); IAudioSessionManager2 audioSessionManager = (IAudioSessionManager2)device.Activate(ref guidAudioSessionManager2, (int)ClsCtx.CLSCTX_ALL, IntPtr.Zero); IAudioSessionEnumerator sessionEnumerator = audioSessionManager.GetSessionEnumerator(); int sessionCount = sessionEnumerator.GetCount(); for (int j = 0; j < sessionCount; j++) { IAudioSessionControl audioSessionControl = sessionEnumerator.GetSession(j); IAudioSessionControl2 audioSessionControl2 = (IAudioSessionControl2)audioSessionControl; AudioSessionState state = audioSessionControl.GetState(); if (state == AudioSessionState.AudioSessionStateActive) { SoundInfoSession soundInfoSession = new SoundInfoSession(); soundInfoDevice.sessions.Add(soundInfoSession); string displayName = audioSessionControl.GetDisplayName(); string iconPath = audioSessionControl.GetIconPath(); int processId = audioSessionControl2.GetProcessId(); string processName = Process.GetProcessById(processId).MainWindowTitle; soundInfoSession.pid = processId; soundInfoSession.windowName = processName; } } } return(soundInfoDevices); }
private static string GetDefaultDeviceId(IMMDeviceEnumerator deviceEnumerator, DataFlows dataFlow, DeviceRoles role) { IMMDevice _device = null; Marshal.ThrowExceptionForHR(deviceEnumerator.GetDefaultAudioEndpoint(dataFlow, role, out _device)); string ID; Marshal.ThrowExceptionForHR(_device.GetId(out ID)); return(ID); }
internal AudioDevice(IMMDevice device) { _device = device; string id; _device.GetId(out id); this.DeviceId = id; IPropertyStore ips; _device.OpenPropertyStore(StorageAccessMode.Read, out ips); var pk = PropertyKeys.DeviceFriendlyName; PropertyVariant val; ips.GetValue(ref pk, out val); this.Name = val.Value.ToString(); }
void RefreshDevices() { lock (this) { IMMDeviceCollection IMMDeviceCollection; _IMMDeviceEnumerator.EnumAudioEndpoints(EDataFlow.eRender, 0xF, out IMMDeviceCollection); uint deviceCount = 0; IMMDeviceCollection.GetCount(out deviceCount); for (uint n = 0; n < deviceCount; n++) { IMMDevice IMMDevice; IMMDeviceCollection.Item(n, out IMMDevice); string uid = ""; IMMDevice.GetId(out uid); if (!_PlaybackDevices.ContainsKey(uid)) { _PlaybackDevices.Add(uid, new PlaybackDevice_WindowsCoreApi(IMMDevice)); } } { IMMDevice Default; _IMMDeviceEnumerator.GetDefaultAudioEndpoint(EDataFlow.eRender, 0x0, out Default); string uid = ""; Default.GetId(out uid); if (!_PlaybackDevices.ContainsKey(uid)) { _PlaybackDevices.Add(uid, new PlaybackDevice_WindowsCoreApi(Default)); } _PlaybackDevices[uid]._Default = true; } _IMMDeviceEnumerator.EnumAudioEndpoints(EDataFlow.eCapture, 0xF, out IMMDeviceCollection); deviceCount = 0; IMMDeviceCollection.GetCount(out deviceCount); for (uint n = 0; n < deviceCount; n++) { IMMDevice IMMDevice; IMMDeviceCollection.Item(n, out IMMDevice); string uid = ""; IMMDevice.GetId(out uid); if (!_RecordingDevices.ContainsKey(uid)) { _RecordingDevices.Add(uid, new RecordingDevice_WindowsCoreApi(IMMDevice)); } } } }
/// <summary> /// Gets the friendly name of the specified device. /// </summary> /// <param name="device">The <see cref="IMMDevice"/> interface of the device.</param> /// <returns>The friendly name of the device.</returns> internal static string GetDeviceFriendlyName(IMMDevice device) { string deviceId = device.GetId(); IPropertyStore propertyStore = device.OpenPropertyStore(StgmRead); PropVariant friendlyNameProperty = propertyStore.GetValue(PKeyDeviceFriendlyName); Marshal.ReleaseComObject(propertyStore); string friendlyName = (string)friendlyNameProperty.Value; friendlyNameProperty.Clear(); return(friendlyName); }
private static bool DeviceIsValid(IMMDevice device) { try { string id; EDeviceState state; device.GetId(out id); device.GetState(out state); return(true); } catch { return(false); } }
internal MMDevice(IMMDevice realDevice) { _RealDevice = realDevice; GetPropertyInformation(); Marshal.ThrowExceptionForHR(_RealDevice.GetId(out _id)); IMMEndpoint ep = _RealDevice as IMMEndpoint; Marshal.ThrowExceptionForHR(ep.GetDataFlow(out _dataFlow)); Marshal.ThrowExceptionForHR(_RealDevice.GetState(out _state)); if (_PropertyStore.Contains(PKEY.PKEY_DeviceInterface_FriendlyName)) _friendlyName = (string)_PropertyStore[PKEY.PKEY_DeviceInterface_FriendlyName].Value; }
public IAudioDevice GetDefaultDevice(ERole eRole = ERole.eMultimedia) { IMMDevice device = null; try { device = _enumerator.GetDefaultAudioEndpoint(Flow, ERole.eMultimedia); } catch (Exception ex) when(ex.Is(HRESULT.ERROR_NOT_FOUND)) { // Expected. } _devices.TryFind(device.GetId(), out var dev); return(dev); }
private static AudioDevice CreateDevice(IMMDevice dev) { if (dev == null) { return(null); } string id; dev.GetId(out id); DEVICE_STATE state; dev.GetState(out state); Dictionary <string, object> properties = new Dictionary <string, object>(); IPropertyStore store; dev.OpenPropertyStore(STGM.STGM_READ, out store); if (store != null) { int propCount; store.GetCount(out propCount); for (int j = 0; j < propCount; j++) { PROPERTYKEY pk; if (store.GetAt(j, out pk) == 0) { PROPVARIANT value = new PROPVARIANT(); int hr = store.GetValue(ref pk, ref value); object v = value.GetValue(); try { if (value.vt != VARTYPE.VT_BLOB) // for some reason, this fails? { PropVariantClear(ref value); } } catch { } string name = pk.ToString(); properties[name] = v; } } } return(new AudioDevice(id, (AudioDeviceState)state, properties)); }
private void LoadProperties(IMMDevice device) { ComThread.Assert(); //Load values Marshal.ThrowExceptionForHR(device.GetId(out _realId)); Marshal.ThrowExceptionForHR(device.GetState(out _state)); // ReSharper disable once SuspiciousTypeConversion.Global var ep = device as IMMEndpoint; if (ep != null) { ep.GetDataFlow(out _dataFlow); } GetPropertyInformation(device); }
internal MMDevice(IMMDevice realDevice) { _RealDevice = realDevice; GetPropertyInformation(); Marshal.ThrowExceptionForHR(_RealDevice.GetId(out _id)); IMMEndpoint ep = _RealDevice as IMMEndpoint; Marshal.ThrowExceptionForHR(ep.GetDataFlow(out _dataFlow)); Marshal.ThrowExceptionForHR(_RealDevice.GetState(out _state)); if (_PropertyStore.Contains(PKEY.PKEY_DeviceInterface_FriendlyName)) { _friendlyName = (string)_PropertyStore[PKEY.PKEY_DeviceInterface_FriendlyName].Value; } }
public AudioDevice(IMMDevice device) { _device = device; _dispatcher = App.Current.Dispatcher; _id = device.GetId(); Trace.WriteLine($"AudioDevice Create {_id}"); _deviceVolume = device.Activate <IAudioEndpointVolume>(); _deviceVolume.RegisterControlChangeNotify(this); _meter = device.Activate <IAudioMeterInformation>(); _sessions = new AudioDeviceSessionCollection(this, _device); _deviceVolume.GetMasterVolumeLevelScalar(out _volume); _isMuted = _deviceVolume.GetMute() != 0; ReadDisplayName(); }
private static AudioDevice CreateDevice(IMMDevice dev) { if (dev == null) return null; string id; dev.GetId(out id); DEVICE_STATE state; dev.GetState(out state); Dictionary<string, object> properties = new Dictionary<string, object>(); IPropertyStore store; dev.OpenPropertyStore(STGM.STGM_READ, out store); if (store != null) { int propCount; store.GetCount(out propCount); for (int j = 0; j < propCount; j++) { PROPERTYKEY pk; if (store.GetAt(j, out pk) == 0) { PROPVARIANT value = new PROPVARIANT(); int hr = store.GetValue(ref pk, ref value); object v = value.GetValue(); try { if (value.vt != VARTYPE.VT_BLOB) // for some reason, this fails? { PropVariantClear(ref value); } } catch { } string name = pk.ToString(); properties[name] = v; } } } return new AudioDevice(id, (AudioDeviceState)state, properties); }
public AudioDeviceSessionCollection(IAudioDevice parent, IMMDevice device, Dispatcher foregroundDispatcher) { _parent = new WeakReference <IAudioDevice>(parent); _dispatcher = foregroundDispatcher; try { _sessionManager = device.Activate <IAudioSessionManager2>(); _sessionManager.RegisterSessionNotification(this); var enumerator = _sessionManager.GetSessionEnumerator(); int count = enumerator.GetCount(); for (int i = 0; i < count; i++) { CreateAndAddSession(enumerator.GetSession(i)); } } catch (Exception ex) { Trace.WriteLine($"AudioDeviceSessionCollection Create dev={device.GetId()} {ex}"); } }
private void QueryDefaultCommunicationsDevice() { Trace.WriteLine("AudioDeviceManager QueryDefaultCommunicationsDevice"); IMMDevice device = null; try { device = _enumerator.GetDefaultAudioEndpoint(EDataFlow.eRender, ERole.eCommunications); } catch (Exception ex) when(ex.Is(Error.ERROR_NOT_FOUND)) { // Expected. } string newDeviceId = device?.GetId(); var currentDeviceId = _defaultCommunicationsDevice?.Id; if (currentDeviceId != newDeviceId) { FindDevice(newDeviceId, out _defaultCommunicationsDevice); } }
public string GetDefaultDevice(EDataFlow dataFlow, ERole role) { IMMDevice device = null; try { _realEnumerator.GetDefaultAudioEndpoint(dataFlow, role, out device); string devId; device.GetId(out devId); return(devId); } catch (Exception) { return(null); } finally { if (device != null) { Marshal.ReleaseComObject(device); } } }
private void QueryDefaultPlaybackDevice() { Trace.WriteLine("AudioDeviceManager QueryDefaultPlaybackDevice"); IMMDevice device = null; try { device = _enumerator.GetDefaultAudioEndpoint(EDataFlow.eRender, ERole.eMultimedia); } catch (Exception ex) when(ex.Is(Error.ERROR_NOT_FOUND)) { // Expected. } string newDeviceId = device?.GetId(); var currentDeviceId = _defaultPlaybackDevice?.Id; if (currentDeviceId != newDeviceId) { _devices.TryFind(newDeviceId, out _defaultPlaybackDevice); DefaultChanged?.Invoke(this, _defaultPlaybackDevice); } }
private void LoadProperties(IMMDevice device) { ComThread.Assert(); //Load values Marshal.ThrowExceptionForHR(device.GetId(out _realId)); Marshal.ThrowExceptionForHR(device.GetState(out _state)); // ReSharper disable once SuspiciousTypeConversion.Global var ep = device as IMMEndpoint; if (ep != null) ep.GetDataFlow(out _dataFlow); GetPropertyInformation(device); }