/// <summary> /// Gets a list of available audio capture devices. /// </summary> /// <returns> /// An array of available capture device names. /// </returns> public static string[] GetAvailableCaptureDevices() { // Get the collection of available capture devices IMMDeviceCollection deviceCollection = DeviceUtil.GetAvailableDevices(EDataFlow.Capture); string[] devices = null; int deviceCount = deviceCollection.GetCount(); devices = new string[deviceCount]; // Iterate over the collection to get the device names for (int i = 0; i < deviceCount; i++) { IMMDevice device = deviceCollection.Item(i); // Get the friendly name of the device devices[i] = DeviceUtil.GetDeviceFriendlyName(device); // Done with the device so release it Marshal.ReleaseComObject(device); } // Release the collection when done Marshal.ReleaseComObject(deviceCollection); return(devices); }
public void UpdateProcessList() { IMMDeviceEnumerator deviceEnumerator = (IMMDeviceEnumerator)(new MMDeviceEnumerator()); IMMDeviceCollection outputDevices = null; IMMDeviceCollection inputDevices = null; deviceEnumerator.EnumAudioEndpoints(EDataFlow.eRender, STATE.ACTIVE, out outputDevices); deviceEnumerator.EnumAudioEndpoints(EDataFlow.eCapture, STATE.ACTIVE, out inputDevices); uint inputDeviceCount = 0; uint outputDeviceCount = 0; inputDevices.GetCount(out inputDeviceCount); outputDevices.GetCount(out outputDeviceCount); // Loop over every output device for (uint i = 0; i < outputDeviceCount; i++) { IMMDevice device; outputDevices.Item(i, out device); AudioDevice audioDevice = new AudioDevice(device); Mapper.Mapper.DevicesAndProcesses[audioDevice] = audioDevice.GetAllSessions(); } }
/// <summary> /// Gets a collection of available devices. /// </summary> /// <param name="dataFlow">The data flow.</param> /// <returns>A collection of available devices matching the supplied role.</returns> internal static IMMDeviceCollection GetAvailableDevices(EDataFlow dataFlow) { IMMDeviceEnumerator deviceEnumerator = (IMMDeviceEnumerator) new MMDeviceEnumerator(); IMMDeviceCollection deviceCollection = deviceEnumerator.EnumAudioEndpoints(dataFlow, DeviceStateActive); Marshal.ReleaseComObject(deviceEnumerator); return(deviceCollection); }
public MMDeviceCollection(IMMDeviceCollection deviceCollection) { _deviceCollection = deviceCollection; _devicesList = new List <MMDevice>(); for (int i = 0; i < Count; i++) { _devicesList.Add(this[i]); } }
// 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); }
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> /// Returns true if an active microphone session is found. /// </summary> public bool IsAnyMicInUse() { IMMDeviceEnumerator deviceEnumerator = null; IMMDeviceCollection microphoneCollection = null; try { deviceEnumerator = (IMMDeviceEnumerator)(new MMDeviceEnumerator()); deviceEnumerator.EnumAudioEndpoints(EDataFlow.eCapture, DEVICE_STATE_XXX.DEVICE_STATE_ACTIVE, out microphoneCollection); microphoneCollection.GetCount(out uint microphoneCount); for (uint i = 0; i < microphoneCount; i++) { IMMDevice microphone = null; try { microphoneCollection.Item(i, out microphone); if (microphoneHasActiveSession(microphone)) { return(true); } } finally { if (microphone != null) { Marshal.ReleaseComObject(microphone); } } } } finally { if (microphoneCollection != null) { Marshal.ReleaseComObject(microphoneCollection); } if (deviceEnumerator != null) { Marshal.ReleaseComObject(deviceEnumerator); } } return(false); }
public List <OutputDeviceModel> GetCurrentActiveDevices(IDeviceStateNotificationHandler dsn) { var devices = new List <OutputDeviceModel>(); IMMDeviceCollection activeDevices = null; uint count = 0; try { Marshal.ThrowExceptionForHR(this.MMDeviceEnumerator.EnumAudioEndpoints(EDataFlow.Render, DeviceState.Active, out activeDevices)); activeDevices?.GetCount(out count); if (count == 0) { return(devices); } } catch (Exception e) { Logger.Error($"Failed to get output devices collection", e); return(devices); } for (uint i = 0; i < count; i++) { try { IMMDevice dev = null; activeDevices?.Item(i, out dev); if (dev == null) { continue; } var device = new MMDevice(dev); var deviceModel = device.GetOutputDeviceModel(dsn); if (deviceModel != null) { devices.Add(deviceModel); } } catch (Exception e) { Logger.Error($"Failed to enumerate output devices", e); } } return(devices); }
/// <summary> /// Gets a specific device by its friendly name. /// </summary> /// <param name="dataFlow">The data flow.</param> /// <param name="deviceDescription">The device friendly name.</param> /// <returns>The default device matching the supplied name.</returns> internal static IMMDevice GetDeviceByName(EDataFlow dataFlow, string deviceDescription) { IMMDevice device = null; IMMDeviceCollection deviceCollection = GetAvailableDevices(dataFlow); int deviceCount = deviceCollection.GetCount(); for (int i = 0; i < deviceCount; ++i) { IMMDevice dev = deviceCollection.Item(i); if (GetDeviceFriendlyName(dev) == deviceDescription) { // found the named device so stop looking device = dev; break; } Marshal.ReleaseComObject(dev); } Marshal.ReleaseComObject(deviceCollection); return(device); }
//Get all the playback audio devices public static List <AudioDeviceSummary> ListAudioDevices() { try { List <AudioDeviceSummary> deviceListSummary = new List <AudioDeviceSummary>(); IMMDeviceEnumerator deviceEnumerator = (IMMDeviceEnumerator) new MMDeviceEnumerator(); IMMDeviceCollection deviceCollection = deviceEnumerator.EnumAudioEndpoints(EDataFlow.eRender, DeviceState.ACTIVE); uint deviceCount = deviceCollection.GetCount(); for (uint deviceIndex = 0; deviceIndex < deviceCount; deviceIndex++) { IMMDevice.IMMDevice deviceItem = deviceCollection.Item(deviceIndex); //Get the audio device id string deviceId = deviceItem.GetId(); //Get the audio device name PropertyVariant propertyVariant = new PropertyVariant(); IPropertyStore propertyStore = deviceItem.OpenPropertyStore(STGM.STGM_READ); propertyStore.GetValue(ref PKEY_Device_FriendlyName, out propertyVariant); string deviceName = Marshal.PtrToStringUni(propertyVariant.pwszVal); //Add device to summary list deviceListSummary.Add(new AudioDeviceSummary() { Identifier = deviceId, Name = deviceName }); } return(deviceListSummary); } catch { Debug.WriteLine("Failed to get audio devices."); return(null); } }
internal MMDeviceCollection(IMMDeviceCollection parent) { _MMDeviceCollection = parent; }
public void EnumAudioEndpoints(DataFlow dataFlow, DeviceState deviceStates, out IMMDeviceCollection devices) { _inner.EnumAudioEndpoints(dataFlow, deviceStates, out devices); }
internal MMDeviceCollection(IMMDeviceCollection parent, EDataFlow flow, EDeviceState state) { _MMDeviceCollection = parent; DataFlow = flow; DeviceState = state; }
internal MMDeviceCollection(IMMDeviceCollection deviceCollection) { _realDeviceCollection = deviceCollection; }
internal AudioDeviceCollection(IMMDeviceCollection parent) { _underlyingCollection = parent; }
public void Dispose() { _mmDeviceCollection = null; }
internal MMDeviceCollection(IMMDeviceCollection collectionInterface) { _mmDeviceCollection = collectionInterface; }