예제 #1
0
        private void GetPropertyInformation()
        {
            IPropertyStore propstore;

            Marshal.ThrowExceptionForHR(deviceInterface.OpenPropertyStore(StorageAccessMode.Read, out propstore));
            propertyStore = new PropertyStore(propstore);
        }
        /// <summary>
        /// Parse and activate a device.
        /// If successful it is added to the devices list.
        /// </summary>
        /// <param name="device">The device to parse.</param>
        /// <param name="id">The unique id of the device.</param>
        private WasApiAudioDevice ParseDevice(IMMDevice device, string id)
        {
            if (device == null)
            {
                return(null);
            }

            string deviceName = $"Unknown Device ({id})";

            // Try to get the device name.
            device.OpenPropertyStore(StorageAccessMode.Read, out IPropertyStore store);
            if (store != null)
            {
                store.GetValue(ref PropertyKey.PKEY_Device_FriendlyName, out PropVariant deviceNameProp);
                object parsed = deviceNameProp.Value;
                if (parsed is string deviceNameStr)
                {
                    deviceName = deviceNameStr;
                }
            }

            // Add to the list.
            var dev = new WasApiAudioDevice(id, deviceName, device);

            _devices.Add(id, dev);
            Engine.Log.Trace($"Detected audio device - {deviceName}", MessageSource.Win32);

            return(dev);
        }
예제 #3
0
        //private static Guid IID_IAudioClient = new Guid("1CB9AD4C-DBFA-4c32-B178-C2F568A703B2");
        //private static Guid IDD_IAudioSessionManager = new Guid("BFA971F1-4D5E-40BB-935E-967039BFBEE4");
        //private static Guid IDD_IDeviceTopology = new Guid("2A07407E-6497-4A18-9787-32F79BD0D98F");
        //// ReSharper restore InconsistentNaming
        //#endregion

        #region Init
        /// <summary>
        /// Initializes the device's property store.
        /// </summary>
        /// <param name="stgmAccess">The storage-access mode to open store for.</param>
        /// <remarks>Administrative client is required for Write and ReadWrite modes.</remarks>
        public void GetPropertyInformation(StorageAccessMode stgmAccess = StorageAccessMode.Read)
        {
            IPropertyStore propstore;

            Marshal.ThrowExceptionForHR(deviceInterface.OpenPropertyStore(stgmAccess, out propstore));
            _propertyStore = new PropertyStore(propstore);
        }
예제 #4
0
 private void GetPropertyInformation()
 {
     IPropertyStore propstore;
     StorageAccessMode AccessMode = StorageAccessMode.Read;
     int msg = DeviceToUse.OpenPropertyStore(AccessMode, out propstore);
     PropStore = new PropertyStore(propstore);
 }
예제 #5
0
        private PropertyStore GetPropertyInformation()
        {
            IPropertyStore propstore;

            Marshal.ThrowExceptionForHR(_RealDevice.OpenPropertyStore(EStgmAccess.STGM_READ, out propstore));
            return(new PropertyStore(propstore));
        }
예제 #6
0
        private string GetAudioDeviceName(ref IMMDevice audioDevice)
        {
            IPropertyStore propertyStore;

            Marshal.ThrowExceptionForHR(audioDevice.OpenPropertyStore(StorageAccessMode.Read, out propertyStore));

            int numProperties;

            Marshal.ThrowExceptionForHR(propertyStore.GetCount(out numProperties));

            string deviceName = String.Empty;

            for (int propertyNum = 0; propertyNum < numProperties; ++propertyNum)
            {
                PropertyKey propertyKey;
                Marshal.ThrowExceptionForHR(propertyStore.GetAt(propertyNum, out propertyKey));

                if ((propertyKey.formatId == PKEY_Device_FriendlyName.formatId) && (propertyKey.propertyId == PKEY_Device_FriendlyName.propertyId))
                {
                    PropVariant propertyValue;
                    Marshal.ThrowExceptionForHR(propertyStore.GetValue(ref propertyKey, out propertyValue));
                    deviceName = Marshal.PtrToStringUni(propertyValue.pointerValue);
                    break;
                }
            }

            Marshal.ReleaseComObject(propertyStore);
            return(deviceName);
        }
예제 #7
0
        private PropertyStore GetPropertyInformation()
        {
            IPropertyStore underlyingPropertyStore;

            Marshal.ThrowExceptionForHR(_underlyingDevice.OpenPropertyStore(StorageAccessMode.Read, out underlyingPropertyStore));
            return(new PropertyStore(underlyingPropertyStore));
        }
예제 #8
0
        public static void listDeviceProperties(IMMDevice dev, DEVICE_SUMMARY defaultDevice)
        {
            IPropertyStore propertyStore;

            dev.OpenPropertyStore(0 /*STGM_READ*/, out propertyStore);
            PROPVARIANT property;  propertyStore.GetValue(ref PropertyKey.PKEY_Device_EnumeratorName, out property);

            System.Console.WriteLine(" EnumeratorName: " + (string)property.Value);
            propertyStore.GetValue(ref PropertyKey.PKEY_Device_FriendlyName, out property);
            System.Console.WriteLine(" FriendlyName: " + (string)property.Value);
            if (!defaultDevice.Equals(null) && defaultDevice.FriendlyName == (string)property.Value)
            {
                System.Console.WriteLine(" **Default: True**");
            }
            Marshal.ReleaseComObject(propertyStore);

            IAudioEndpointVolume epv = null;
            var epvid = typeof(IAudioEndpointVolume).GUID;

            Marshal.ThrowExceptionForHR(dev.Activate(ref epvid, /*CLSCTX_ALL*/ 23, 0, out epv));

            bool mute;  epv.GetMute(out mute);  if (mute)
            {
                System.Console.WriteLine(" **GetMute: " + mute + "**");
            }
            float vol;  epv.GetMasterVolumeLevelScalar(out vol);  System.Console.WriteLine(" GetMasterVolumeLevelScalar: " + vol);
        }
        private Dictionary <PropertyKey, object> GetProperties(IMMDevice device)
        {
            var properties = new Dictionary <PropertyKey, object>();

            //Opening in write mode, can cause exceptions to be thrown when not run as admin.
            //This tries to open in write mode if available
            try
            {
                Marshal.ThrowExceptionForHR(device.OpenPropertyStore(StorageAccessMode.ReadWrite, out _propertyStoreInteface));
                Mode = AccessMode.ReadWrite;
            }
            catch
            {
                Debug.WriteLine("Cannot open property store in write mode");
            }

            if (_propertyStoreInteface == null)
            {
                Marshal.ThrowExceptionForHR(device.OpenPropertyStore(StorageAccessMode.Read, out _propertyStoreInteface));
                Mode = AccessMode.Read;
            }
            try
            {
                uint count;
                _propertyStoreInteface.GetCount(out count);
                for (uint i = 0; i < count; i++)
                {
                    PropertyKey key;
                    PropVariant variant;
                    _propertyStoreInteface.GetAt(i, out key);

                    _propertyStoreInteface.GetValue(ref key, out variant);

                    if (variant.IsSupported())
                    {
                        properties.Add(key, variant.Value);
                    }
                }
            }
            catch
            {
                Debug.WriteLine("Cannot get property values");
                return(new Dictionary <PropertyKey, object>());
            }

            return(properties);
        }
예제 #10
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);
        }
예제 #11
0
        private void GetProperty()
        {
            if (_PropertyStore != null)
            {
                return;
            }
            IPropertyStore propstore;

            Marshal.ThrowExceptionForHR(_RealDevice.OpenPropertyStore(EStgmAccess.STGM_READ, out propstore));
            _PropertyStore = new PropertyStore(propstore);
        }
예제 #12
0
 private void GetPropertyInformation()
 {
     try
     {
         IPropertyStore propstore;
         Marshal.ThrowExceptionForHR(_RealDevice.OpenPropertyStore(EStgmAccess.STGM_READ, out propstore));
         _PropertyStore = new PropertyStore(propstore);
     }
     catch
     {
         _PropertyStore = null;
     }
 }
예제 #13
0
 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();
 }
예제 #14
0
        /// <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);
        }
예제 #15
0
        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();
        }
        public string GetDeviceName(IMMDevice device)
        {
            IPropertyStore device_prop;

            device.OpenPropertyStore(0, out device_prop);
            PROPERTYKEY prop_key = new PROPERTYKEY();

            prop_key.pid   = 2;
            prop_key.fmtid = PropertyKeys.PKEY_DeviceInterface_FriendlyName;
            PROPVARIANT prop_value;

            device_prop.GetValue(ref prop_key, out prop_value);
            string device_name = Marshal.PtrToStringAuto(prop_value.Data.AsStringPtr);

            return(device_name);
        }
예제 #17
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));
        }
예제 #18
0
        public static DEVICE_SUMMARY getDefaultDeviceSummary(int dataFlow, int role)
        {
            DEVICE_SUMMARY dev1       = new DEVICE_SUMMARY();  dev1.dataFlow = dataFlow;  dev1.role = role;
            var            enumerator = new MMDeviceEnumeratorComObject() as IMMDeviceEnumerator;
            IMMDevice      dev        = null;  Marshal.ThrowExceptionForHR(enumerator.GetDefaultAudioEndpoint(dataFlow, role, out dev));
            IPropertyStore propertyStore;  dev.OpenPropertyStore(0 /*STGM_READ*/, out propertyStore);
            PROPVARIANT    property;  propertyStore.GetValue(ref PropertyKey.PKEY_Device_FriendlyName, out property);

            dev1.FriendlyName = (string)property.Value;
            Marshal.ReleaseComObject(propertyStore);
            IAudioEndpointVolume epv = null;
            var epvid = typeof(IAudioEndpointVolume).GUID;

            Marshal.ThrowExceptionForHR(dev.Activate(ref epvid, /*CLSCTX_ALL*/ 23, 0, out epv));
            epv.GetMute(out dev1.isMuted);
            Marshal.ReleaseComObject(dev);
            return(dev1);
        }
예제 #19
0
        private string GetAudioDeviceName(ref IMMDevice audioDevice)
        {
            IPropertyStore propertyStore;

            Marshal.ThrowExceptionForHR(audioDevice.OpenPropertyStore(StorageAccessMode.Read, out propertyStore));

            int numProperties;

            Marshal.ThrowExceptionForHR(propertyStore.GetCount(out numProperties));

            string tempDeviceInstance = string.Empty;
            string devicePath         = string.Empty;

            for (int propertyNum = 0; propertyNum < numProperties; ++propertyNum)
            {
                PropertyKey propertyKey;
                Marshal.ThrowExceptionForHR(propertyStore.GetAt(propertyNum, out propertyKey));

                if ((propertyKey.formatId == PKEY_Device_Siblings.formatId) && (propertyKey.propertyId == PKEY_Device_Siblings.propertyId))
                {
                    PropVariant propertyValue;
                    Marshal.ThrowExceptionForHR(propertyStore.GetValue(ref propertyKey, out propertyValue));
                    tempDeviceInstance = Marshal.PtrToStringUni(propertyValue.innerData.pointerValue) ?? "";

                    if (regex.IsMatch(tempDeviceInstance))
                    {
                        MatchCollection collection      = regex.Matches(tempDeviceInstance);
                        GroupCollection groupCollection = collection[0].Groups;
                        devicePath = groupCollection["parent"].Value;
                        devicePath = GetSibling(devicePath);
                    }
                    else
                    {
                        devicePath = "";
                    }

                    break;
                }
            }

            Marshal.ReleaseComObject(propertyStore);
            return(devicePath);
        }
        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);
        }
예제 #21
0
 private PropertyStore OpenPropertyStore()
 {
     Marshal.ThrowExceptionForHR(_underlyingDevice.OpenPropertyStore(StorageAccessMode.Read, out IPropertyStore underlyingPropertyStore));
     return(new PropertyStore(underlyingPropertyStore));
 }
        private Dictionary<PropertyKey, object> GetProperties(IMMDevice device)
        {
            var properties = new Dictionary<PropertyKey, object>();
            //Opening in write mode, can cause exceptions to be thrown when not run as admin.
            //This tries to open in write mode if available
            try
            {
                Marshal.ThrowExceptionForHR(device.OpenPropertyStore(StorageAccessMode.ReadWrite, out _propertyStoreInteface));
                Mode = AccessMode.ReadWrite;
            }
            catch
            {
                Debug.WriteLine("Cannot open property store in write mode");
            }

            if (_propertyStoreInteface == null)
            {
                Marshal.ThrowExceptionForHR(device.OpenPropertyStore(StorageAccessMode.Read, out _propertyStoreInteface));
                Mode = AccessMode.Read;
            }
            try
            {
                uint count;
                _propertyStoreInteface.GetCount(out count);
                for (uint i = 0; i < count; i++)
                {
                    PropertyKey key;
                    PropVariant variant;
                    _propertyStoreInteface.GetAt(i, out key);

                    _propertyStoreInteface.GetValue(ref key, out variant);

                    if (variant.IsSupported())
                        properties.Add(key, variant.Value);
                }
            }
            catch
            {
                Debug.WriteLine("Cannot get property values");
                return new Dictionary<PropertyKey, object>();
            }

            return properties;
        }
예제 #23
0
 /// <summary>
 /// Initializes the device's property store.
 /// </summary>
 /// <param name="stgmAccess">The storage-access mode to open store for.</param>
 /// <remarks>Administrative client is required for Write and ReadWrite modes.</remarks>
 private void InitializePropertyInformation(StorageAccessMode stgmAccess = StorageAccessMode.Read)
 {
     Marshal.ThrowExceptionForHR(_mmDevice.OpenPropertyStore(stgmAccess, out var propstore));
     _propertyStore = new PropertyStore(propstore);
 }
예제 #24
0
        static void Main(string[] args)
        {
            // Uncomment this line of code to allow for debugging
            while (!System.Diagnostics.Debugger.IsAttached)
            {
                System.Threading.Thread.Sleep(100);
            }

            // Test Bed
            //SpeakerMode.SpeakerModeExternal; //Possibly SPDIF out?
            //SpeakerMode.SpeakerModeHeadPhone; //Possible headset out?


            ManagementObjectSearcher objSearcher = new ManagementObjectSearcher(
                "SELECT * FROM Win32_SoundDevice");

            ManagementObjectCollection objCollection = objSearcher.Get();

            ManagementObject soundblaster = null;

            foreach (ManagementObject obj in objCollection)
            {
                foreach (PropertyData property in obj.Properties)
                {
                    Console.Out.WriteLine(String.Format("{0}:{1}", property.Name, property.Value));

                    if (property.Name.Equals("Name") && property.Value.ToString().Contains("Sound Blaster"))
                    {
                        soundblaster = obj;
                    }
                }
            }

            Creative.Platform.Devices.Models.DeviceManager dm = DeviceManager.Instance;
            //.Collections.Concurrent.ConcurrentDictionary<string, DeviceEndpoint> des = dm.MixerDiscoveredDeviceEndpoints;
            //DeviceEndpoint mixerDevice = dm.GetMixerDeviceEndpoints(new Device {d });

            //  Define an IMM Device
            Creative.Platform.CoreAudio.Interfaces.IMMDeviceEnumerator imde = (Creative.Platform.CoreAudio.Interfaces.IMMDeviceEnumerator) new Creative.Platform.CoreAudio.Classes.MMDeviceEnumerator();

            IMMDevice imd = null;

            foreach (DeviceEndpoint de in dm.MixerDiscoveredDeviceEndpoints.Values)
            {
                int device_id = imde.GetDevice(de.DeviceEndpointId, out imd);
                if (imd != null)
                {
                    break;
                }
            }


            //  Generate a DeviceEndpoint
            DefaultDeviceEndpointFactory ddef = new Creative.Platform.Devices.Models.DefaultDeviceEndpointFactory();
            DeviceEndpoint dep = ddef.CreateDeviceEndPoint(imd);

            List <IDeviceRepository> dev_reps = dep.GetDeviceRepositories();

            //  Create the SoundCore
            //Creative.Platform.Devices.Features.SoundCore.SoundCoreRepository scr =
            Creative.Platform.Devices.Features.Apo.ApoInfoWrapper      aiw = new Creative.Platform.Devices.Features.Apo.ApoInfoWrapper(dep);
            Creative.Platform.Devices.Features.Apo.ApoFeatureChecker   apc = new Creative.Platform.Devices.Features.Apo.ApoFeatureChecker(aiw);
            Creative.Platform.Devices.Features.Apo.PropStoreRepository psr = new Creative.Platform.Devices.Features.Apo.PropStoreRepository(dep.DeviceEndpointId, apc);

            IPropertyStore properties;

            imd.OpenPropertyStore(0u, out properties);
            CDCDeviceRepositoryInitializer cdcri = new CDCDeviceRepositoryInitializer();

            cdcri.Initialize(dep, properties);

            psr.GetSpeakerConfig();

            //Creative.Platform.Devices.Features.SpeakerConfigs.SpeakerConfigService scs = new SpeakerConfigService(dep);
            //Creative.Platform.Devices.Features.SpeakerConfigs.SoundCoreSpeakerConfigFeature scsrf = new SoundCoreSpeakerConfigFeature(dep,,, scs);

            //Creative.Platform.Devices.Features.SoundCore.SoundCoreRepository scr = new Creative.Platform.Devices.Features.SoundCore.SoundCoreRepository()

            try
            {
                psr.SetSpeakerConfig((int)SpeakerMode.SpeakerModeHeadPhone);
            }
            catch (Exception e)
            {
            }
            //scr.SetSpeakerConfig((int)SpeakerMode.SpeakerModeHeadPhone);

            //SDWrapper.Run(args);
        }