/// <summary> /// The Initialize Audio Client /// </summary> /// <param name="audioFlow"></param> /// <param name="_deviceEnumerator"></param> private void InitializeAudioClient() { //Get Audio Client from device COMResult result = _audioDevice.Activate(typeof(IAudioClient).GUID, 0, IntPtr.Zero, out object obj); _audioClient = (IAudioClient)obj; //Get Audio Meter from device result = _audioDevice.Activate(typeof(IAudioMeterInformation).GUID, 0, IntPtr.Zero, out obj); _audioMeter = (IAudioMeterInformation)obj; //Get Audio End Point result = _audioDevice.Activate(typeof(IAudioEndpointVolume).GUID, 0, IntPtr.Zero, out obj); _audioEndpointVolume = (IAudioEndpointVolume)obj; _audioEndpointVolume.RegisterControlChangeNotify(classCallBack); //Initialize Audio Client. _sessionGuid = new Guid(); result = _audioClient.GetMixFormat(out waveFormat); AudioClientStreamFlags streamFlag = AudioClientStreamFlags.None; if (_audioDataFlow == AudioDataFlow.eRender) { streamFlag = AudioClientStreamFlags.Loopback; } result = _audioClient.Initialize(AudioClientMode.Shared, streamFlag, 10000000, 0, waveFormat, ref _sessionGuid); result = _audioClient.Start(); //Change wave format here SetupWaveFormat(waveFormat); result = _audioEndpointVolume.GetChannelCount(out _channelCount); }
private void InitializeAudio(AudioDataFlow audioFlow, IMMDeviceEnumerator deviceEnumerator) { //Get Audio Device COMResult result = deviceEnumerator.GetDefaultAudioEndpoint(audioFlow, EndPointRole.eMultimedia, out _audioDevice); //Register End point notification _notifyClient = new MMNotificationClient(); result = deviceEnumerator.RegisterEndpointNotificationCallback(_notifyClient); //Get Audio Client from device result = _audioDevice.Activate(typeof(IAudioClient).GUID, 0, IntPtr.Zero, out object obj); _audioClient = (IAudioClient)obj; //Get Audio Meter from device result = _audioDevice.Activate(typeof(IAudioMeterInformation).GUID, 0, IntPtr.Zero, out obj); _audioMeter = (IAudioMeterInformation)obj; //Initialize Audio Client. _sessionGuid = new Guid(); result = _audioClient.GetMixFormat(out waveFormat); AudioClientStreamFlags streamFlag = AudioClientStreamFlags.None; if (audioFlow == AudioDataFlow.eRender) { streamFlag = AudioClientStreamFlags.Loopback; } result = _audioClient.Initialize(AudioClientMode.Shared, streamFlag, 10000000, 0, waveFormat, ref _sessionGuid); //Get Capture Client. result = _audioClient.GetService(typeof(IAudioCaptureClient).GUID, out obj); Marshal.ThrowExceptionForHR((int)result); _audioCaptureClient = (IAudioCaptureClient)obj; result = _audioClient.Start(); //Change wave format here SetupWaveFormat(waveFormat); }
/// <summary> /// Get HID Audio Device UUID /// </summary> /// <param name="dataFlow"></param> /// <param name="pid"></param> /// <param name="vid"></param> /// <returns></returns> private string GetHIDAudioDeviceUUID(AudioDataFlow dataFlow, string pid, string vid) { COMResult result = COMResult.E_FAIL; PropertyKey proKey; PropVariant proVar; result = _deviceEnumerator.EnumAudioEndpoints(dataFlow, AudioDeviceState.DEVICE_STATE_ACTIVE, out IMMDeviceCollection devCollect); result = devCollect.GetCount(out int devCnt); for (int i = 0; i < devCnt; i++) { result = devCollect.Item(i, out IMMDevice tDev); tDev.GetId(out string devID); tDev.OpenPropertyStore(StorageAccessMode.STGM_READ, out IPropertyStore ppt); ppt.GetCount(out int ppCnt); for (int j = 0; j < ppCnt; j++) { result = ppt.GetAt(j, out proKey); result = ppt.GetValue(ref proKey, out proVar); if (proVar.DataType == System.Runtime.InteropServices.VarEnum.VT_LPWSTR || proVar.DataType == System.Runtime.InteropServices.VarEnum.VT_LPSTR) { if (proKey.formatId.ToString().Equals(PKEY_AUDIOENDPOINT_HWID.formatId.ToString()) && proKey.propertyId == PKEY_AUDIOENDPOINT_HWID.propertyId) { string hwID = proVar.Value.ToString(); if (!GetPIDVIDFromHWIDString(hwID, out string hwPid, out string hwVid)) { continue; } if (vid.Equals(hwVid, StringComparison.CurrentCultureIgnoreCase) && pid.Equals(hwPid, StringComparison.CurrentCultureIgnoreCase)) { return(devID); } } } } } return(string.Empty); }
/// <summary> /// Get IMMDevice /// </summary> /// <param name="initFlow"></param> /// <param name="pid"></param> /// <param name="vid"></param> /// <returns>Null means Failed</returns> private IMMDevice GetIMMDevice(AudioDataFlow initFlow, string pid, string vid) { COMResult result = COMResult.E_FAIL; IMMDevice _device = null; if (initFlow == AudioDataFlow.eAll) { //Not support get both render and capture. return(_device); } string devUUID = GetHIDAudioDeviceUUID(initFlow, pid, vid); if (string.IsNullOrEmpty(devUUID)) { //Failed to get UUID, Get Default Endpoint result = _deviceEnumerator.GetDefaultAudioEndpoint(initFlow, EndPointRole.eCommunications, out _device); } else { result = _deviceEnumerator.GetDevice(devUUID, out _device); } return(_device); }