Пример #1
0
        /// <summary>
        /// Stop Recording
        /// </summary>
        public void StopRecording()
        {
            if (captureState == WasapiCaptureState.Disposed)
            {
                throw new ObjectDisposedException(nameof(WasapiCaptureRT));
            }
            if (captureState != WasapiCaptureState.Recording)
            {
                return;
            }

            captureState = WasapiCaptureState.Stopped;
            captureTask?.Wait(5000);
            Debug.WriteLine("WasapiCaptureRT stopped");
        }
Пример #2
0
        /// <summary>
        /// Initializes the capture device. Must be called on the UI (STA) thread.
        /// If not called manually then StartRecording() will call it internally.
        /// </summary>
        public async Task InitAsync()
        {
            if (captureState == WasapiCaptureState.Disposed)
            {
                throw new ObjectDisposedException(nameof(WasapiCaptureRT));
            }
            if (captureState != WasapiCaptureState.Uninitialized)
            {
                throw new InvalidOperationException("Already initialized");
            }

/*            var icbh = new ActivateAudioInterfaceCompletionHandler(ac2 => InitializeCaptureDevice((IAudioClient)ac2));
 *            IActivateAudioInterfaceAsyncOperation activationOperation;
 *            // must be called on UI thread
 *            NativeMethods.ActivateAudioInterfaceAsync(device, IID_IAudioClient2, IntPtr.Zero, icbh, out activationOperation);
 *
 *            audioClient = new AudioClient((IAudioClient)(await icbh));
 *
 *            hEvent = NativeMethods.CreateEventExW(IntPtr.Zero, IntPtr.Zero, 0, EventAccess.EVENT_ALL_ACCESS);
 *            audioClient.SetEventHandle(hEvent);*/

            var icbh = new ActivateAudioInterfaceCompletionHandler(ac2 =>
            {
                if (this.audioClientProperties != null)
                {
                    IntPtr p = Marshal.AllocHGlobal(Marshal.SizeOf(this.audioClientProperties.Value));
                    Marshal.StructureToPtr(this.audioClientProperties.Value, p, false);
                    ac2.SetClientProperties(p);
                    Marshal.FreeHGlobal(p);
                    // TODO: consider whether we can marshal this without the need for AllocHGlobal
                }

                InitializeCaptureDevice((IAudioClient2)ac2);
                audioClient = new AudioClient((IAudioClient2)ac2);

                hEvent = NativeMethods.CreateEventExW(IntPtr.Zero, IntPtr.Zero, 0, EventAccess.EVENT_ALL_ACCESS);
                audioClient.SetEventHandle(hEvent);
            });

            IActivateAudioInterfaceAsyncOperation activationOperation;

            // must be called on UI thread
            NativeMethods.ActivateAudioInterfaceAsync(device, IID_IAudioClient2, IntPtr.Zero, icbh, out activationOperation);
            await icbh;

            captureState = WasapiCaptureState.Stopped;
        }
Пример #3
0
        /// <summary>
        /// Start Recording
        /// </summary>
        public async void StartRecording()
        {
            if (captureState == WasapiCaptureState.Disposed)
            {
                throw new ObjectDisposedException(nameof(WasapiCaptureRT));
            }
            if (captureState == WasapiCaptureState.Uninitialized)
            {
                await InitAsync();
            }

            captureState = WasapiCaptureState.Recording;

            captureTask = Task.Run(() => DoRecording());

            Debug.WriteLine("Recording...");
        }
Пример #4
0
        /// <summary>
        /// Initializes the capture device. Must be called on the UI (STA) thread.
        /// If not called manually then StartRecording() will call it internally.
        /// </summary>
        public async Task InitAsync()
        {
            if (captureState == WasapiCaptureState.Disposed)
            {
                throw new ObjectDisposedException(nameof(WasapiCaptureRT));
            }
            if (captureState != WasapiCaptureState.Uninitialized)
            {
                throw new InvalidOperationException("Already initialized");
            }

            var icbh = new ActivateAudioInterfaceCompletionHandler(ac2 => InitializeCaptureDevice((IAudioClient)ac2));
            IActivateAudioInterfaceAsyncOperation activationOperation;

            // must be called on UI thread
            NativeMethods.ActivateAudioInterfaceAsync(device, IID_IAudioClient2, IntPtr.Zero, icbh, out activationOperation);
            audioClient = new AudioClient((IAudioClient)(await icbh));

            hEvent = NativeMethods.CreateEventExW(IntPtr.Zero, IntPtr.Zero, 0, EventAccess.EVENT_ALL_ACCESS);
            audioClient.SetEventHandle(hEvent);

            captureState = WasapiCaptureState.Stopped;
        }
Пример #5
0
        /// <summary>
        /// Dispose
        /// </summary>
        public void Dispose()
        {
            if (captureState == WasapiCaptureState.Disposed)
            {
                return;
            }

            try
            {
                StopRecording();

                NativeMethods.CloseHandle(hEvent);
                audioClient?.Dispose();
            }
            catch (Exception ex)
            {
                Debug.WriteLine("Exception disposing WasapiCaptureRT: " + ex.ToString());
            }

            hEvent      = IntPtr.Zero;
            audioClient = null;

            captureState = WasapiCaptureState.Disposed;
        }