Пример #1
0
        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
                }
            }
        }
Пример #2
0
        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;
                }
            }
        }
Пример #3
0
        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();
        }
Пример #4
0
        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));
        }
Пример #5
0
        private EDeviceState GetState()
        {
            EDeviceState result;

            Marshal.ThrowExceptionForHR(_RealDevice.GetState(out result));

            return(result);
        }
Пример #6
0
        private static bool DeviceIsValid(IMMDevice device)
        {
            try
            {
                string       id;
                EDeviceState state;
                device.GetId(out id);
                device.GetState(out state);

                return(true);
            }
            catch
            {
                return(false);
            }
        }
Пример #7
0
        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;
        }
Пример #8
0
        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));
        }
Пример #9
0
        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);
        }
Пример #10
0
        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;
            }
        }
        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);
        }
Пример #12
0
        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);
        }