Пример #1
0
        /// <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);
        }
Пример #2
0
        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();
            }
        }
Пример #3
0
        /// <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);
        }
Пример #4
0
        public MMDeviceCollection(IMMDeviceCollection deviceCollection)
        {
            _deviceCollection = deviceCollection;

            _devicesList = new List <MMDevice>();
            for (int i = 0; i < Count; i++)
            {
                _devicesList.Add(this[i]);
            }
        }
Пример #5
0
        // 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));
                    }
                }
            }
        }
Пример #7
0
        /// <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);
        }
Пример #8
0
        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);
        }
Пример #9
0
        /// <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);
        }
Пример #10
0
        //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;
 }
Пример #12
0
 public void EnumAudioEndpoints(DataFlow dataFlow, DeviceState deviceStates, out IMMDeviceCollection devices)
 {
     _inner.EnumAudioEndpoints(dataFlow, deviceStates, out devices);
 }
Пример #13
0
 internal MMDeviceCollection(IMMDeviceCollection parent, EDataFlow flow, EDeviceState state)
 {
     _MMDeviceCollection = parent;
     DataFlow            = flow;
     DeviceState         = state;
 }
Пример #14
0
 internal MMDeviceCollection(IMMDeviceCollection deviceCollection)
 {
     _realDeviceCollection = deviceCollection;
 }
Пример #15
0
 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 AudioDeviceCollection(IMMDeviceCollection parent)
 {
     _underlyingCollection = parent;
 }
Пример #18
0
 public void Dispose()
 {
     _mmDeviceCollection = null;
 }
Пример #19
0
 internal MMDeviceCollection(IMMDeviceCollection parent)
 {
     _MMDeviceCollection = parent;
 }
 internal MMDeviceCollection(IMMDeviceCollection collectionInterface)
 {
     _mmDeviceCollection = collectionInterface;
 }