public void Initialize( AudioClientShareMode shareMode, AudioClientStreamFlags streamFlags, long bufferDuration, long periodicity, WAVEFORMATEXTENSIBLE format, Guid audioSessionGuid) { int hr = _RealClient.Initialize(shareMode, streamFlags, bufferDuration, periodicity, format, ref audioSessionGuid); Marshal.ThrowExceptionForHR(hr); if ((streamFlags & AudioClientStreamFlags.EventCallback) != 0) { _audioSampleReady = new AutoResetEvent(false); IntPtr eventHandle = CreateEventEx(IntPtr.Zero, "audioSampleReady", CreateEventFlags.None, AccessRight.Synchronize | AccessRight.EventModifyState); _audioSampleReady.SafeWaitHandle = new Microsoft.Win32.SafeHandles.SafeWaitHandle(eventHandle, true); hr = _RealClient.SetEventHandle(eventHandle); Marshal.ThrowExceptionForHR(hr); _audioSampleReadyRegistered = ThreadPool.RegisterWaitForSingleObject( _audioSampleReady, new WaitOrTimerCallback(sampleReady), null, -1, false); } _isInitialized = true; }
/// <summary> /// Set the Event Handle for buffer synchro. /// </summary> /// <param name="eventWaitHandle">The Wait Handle to setup</param> public void SetEventHandle(IntPtr eventWaitHandle) { audioClientInterface.SetEventHandle(eventWaitHandle); }
/// <summary> /// Set the Event Handle for buffer synchro. /// </summary> /// <param name="eventWaitHandle">The Wait Handle to setup</param> public void SetEventHandle(EventWaitHandle eventWaitHandle) { _realAudioClient.SetEventHandle(eventWaitHandle.SafeWaitHandle.DangerousGetHandle()); }
/// <summary> /// Set the Event Handle for buffer synchro. /// </summary> /// <param name="eventWaitHandle">The Wait Handle to setup</param> public void SetEventHandle(EventWaitHandle eventWaitHandle) { audioClientInterface.SetEventHandle(eventWaitHandle.SafeWaitHandle.DangerousGetHandle()); }
public WaveFormatEx Open() { StopSilenceThread(); Log.Information("Opening DesktopAudio"); IMMDeviceEnumerator deviceEnumerator = null; IntPtr mixFormatPtr = IntPtr.Zero; try { bool render = true; deviceEnumerator = Activator.CreateInstance(typeof(MMDeviceEnumerator)) as IMMDeviceEnumerator; var res = deviceEnumerator.GetDefaultAudioEndpoint( render ? DataFlowEnum.Render : DataFlowEnum.Capture, render ? RoleEnum.Console : RoleEnum.Communications, out _endpoint); if (render) { StartSilenceGeneration(); } Checked(_endpoint.Activate(ref IAudioClientId, ClsCtxEnum.All, IntPtr.Zero, out var obj), "Activate"); _audioClient = (IAudioClient)obj; Checked(_audioClient.GetMixFormat(out mixFormatPtr), "GetMixFormat"); WaveFormatEx outputFormat = (WaveFormatEx)Marshal.PtrToStructure(mixFormatPtr, typeof(WaveFormatEx)); if (!render) // for render it is checked in the StartSilenceGeneration(); { CheckFormat(outputFormat); } _bytesPerFrame = outputFormat.BlockAlign; var flags = AudioClientStreamFlagsEnum.StreamFlagsEventCallback | (render ? AudioClientStreamFlagsEnum.StreamFlagsLoopback : AudioClientStreamFlagsEnum.None); Checked(_audioClient.Initialize(AudioClientShareModeEnum.Shared, flags, 10_000_000 * 5, 0, mixFormatPtr, Guid.Empty), "Initialize"); Checked(_audioClient.GetService(IAudioCaptureClientId, out var captureObj), "GetService"); _audioCaptureClient = (IAudioCaptureClient)captureObj; #pragma warning disable CS0618 // Type or member is obsolete Checked(_audioClient.SetEventHandle(_dataAvailable.Handle), "SetEventHandle"); #pragma warning restore CS0618 // Type or member is obsolete Checked(_audioClient.Start(), "Start"); return(outputFormat); } catch (Exception e) { Core.LogError(e, "Open desktop audio failed"); StopSilenceThread(); ReleaseComFields(); throw; } finally { if (mixFormatPtr != IntPtr.Zero) { Marshal.FreeCoTaskMem(mixFormatPtr); } ReleaseComObject(ref deviceEnumerator); } }