예제 #1
0
        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);
        }
 public MultiMediaNotificationListener()
 {
     if (Environment.OSVersion.Version.Major < 6)
     {
         throw new NotSupportedException("This functionality is only supported on Windows Vista or newer.");
     }
     _deviceEnumerator = (IMMDeviceEnumerator) new MMDeviceEnumerator();
     _deviceEnumerator.RegisterEndpointNotificationCallback(this);
 }
예제 #3
0
        public AudioDeviceManager()
        {
            int hr = _underlyingEnumerator.RegisterEndpointNotificationCallback(this);

            if (hr != HResult.OK)
            {
                throw Marshal.GetExceptionForHR(hr);
            }
        }
        void SetupDevices()
        {
            lock (this)
            {
                _IMMDeviceEnumerator = CreateInstance <IMMDeviceEnumerator>("BCDE0395-E52F-467C-8E3D-C4579291692E");

                _IMMDeviceEnumerator.RegisterEndpointNotificationCallback(_Callbacks);
            }
            RefreshDevices();
        }
예제 #5
0
        public MMDeviceEventManager()
        {
            _synchronizationContext = SynchronizationContext.Current;

            int hr = _deviceEnumerator.RegisterEndpointNotificationCallback(this);

            if (hr != HResult.OK)
            {
                throw Marshal.GetExceptionForHR(hr);
            }
        }
예제 #6
0
        public MMDeviceEnumerator()
        {
            if (System.Environment.OSVersion.Version.Major < 6)
            {
                throw new NotSupportedException("This functionality is only supported on Windows Vista or newer.");
            }

            /* added -> */
            _notificationClient = new MMNotificationClient(this);
            int hr = _realEnumerator.RegisterEndpointNotificationCallback(_notificationClient);

            Marshal.ThrowExceptionForHR(hr);
            /* <- added */
        }
예제 #7
0
        /// <summary>
        /// Registers a call back for Device Events
        /// </summary>
        /// <param name="client">Object implementing IMMNotificationClient type casted as IMMNotificationClient interface</param>
        /// <returns></returns>
        public int RegisterEndpointNotificationCallback([In][MarshalAs(UnmanagedType.Interface)] IMMNotificationClient client)
        {
            if (_notificationClients.Contains(client))
            {
                return(0);
            }

            var hresult = _mmDeviceEnumerator.RegisterEndpointNotificationCallback(client);

            Marshal.ThrowExceptionForHR(hresult);

            _notificationClients.Add(client);
            return(hresult);
        }
예제 #8
0
        public CoreAudioController()
        {
            ComThread.Invoke(() =>
            {
                // ReSharper disable once SuspiciousTypeConversion.Global
                _innerEnumerator = new MMDeviceEnumeratorComObject() as IMMDeviceEnumerator;

                if (_innerEnumerator == null)
                {
                    return;
                }

                _notificationClient = new MMNotificationClient(this);
                _innerEnumerator.RegisterEndpointNotificationCallback(_notificationClient);
            });

            RefreshSystemDevices();
        }
        public AudioDeviceManager(AudioDeviceKind kind)
        {
            _kind                = kind;
            _dispatcher          = Dispatcher.CurrentDispatcher;
            _devices             = new AudioDeviceCollection();
            _policyConfigService = new AudioPolicyConfigService(Flow);

            TraceLine($"Create");

            Task.Factory.StartNew(() =>
            {
                try
                {
                    _enumerator = (IMMDeviceEnumerator) new MMDeviceEnumerator();
                    _enumerator.RegisterEndpointNotificationCallback(this);

                    var devices      = _enumerator.EnumAudioEndpoints(Flow, DeviceState.ACTIVE);
                    uint deviceCount = devices.GetCount();
                    for (uint i = 0; i < deviceCount; i++)
                    {
                        ((IMMNotificationClient)this).OnDeviceAdded(devices.Item(i).GetId());
                    }

                    _dispatcher.Invoke((Action)(() =>
                    {
                        QueryDefaultDevice();
                        Loaded?.Invoke(this, null);
                    }));
                }
                catch (Exception ex)
                {
                    // Even through we're going to be broken, show the tray icon so the user can collect debug data.
                    AppTrace.LogWarning(ex);

                    _dispatcher.Invoke((Action)(() =>
                    {
                        Loaded?.Invoke(this, null);
                    }));
                }
            });

            TraceLine($"Create Exit");
        }
예제 #10
0
        internal static bool AudioDevicesClientOpen()
        {
            bool result = true;

            if (pm_AudioDevicesCallback == null)
            {
                try
                {
                    IMMDeviceEnumerator deviceEnumerator = (IMMDeviceEnumerator)(new MMDeviceEnumerator());
                    pm_AudioDevicesCallback = new AudioDevicesClient();
                    deviceEnumerator.RegisterEndpointNotificationCallback(pm_AudioDevicesCallback);
                    pm_AudioDevicesEventArgs = new SystemAudioDevicesEventArgs();
                    if (deviceEnumerator != null)
                    {
                        Marshal.ReleaseComObject(deviceEnumerator);
                    }
                }
                catch { result = false; }
            }
            return(result);
        }
예제 #11
0
        public AudioDeviceManager(Dispatcher dispatcher)
        {
            Trace.WriteLine("AudioDeviceManager Create");

            _dispatcher          = dispatcher;
            _devices             = new AudioDeviceCollection();
            _policyConfigService = new AudioPolicyConfigService();

            Task.Factory.StartNew(() =>
            {
                try
                {
                    _enumerator = (IMMDeviceEnumerator) new MMDeviceEnumerator();
                    _enumerator.RegisterEndpointNotificationCallback(this);

                    var devices      = _enumerator.EnumAudioEndpoints(EDataFlow.eRender, DeviceState.ACTIVE);
                    uint deviceCount = devices.GetCount();
                    for (uint i = 0; i < deviceCount; i++)
                    {
                        ((IMMNotificationClient)this).OnDeviceAdded(devices.Item(i).GetId());
                    }

                    // Trigger default logic to register for volume change
                    _dispatcher.BeginInvoke((Action)(() =>
                    {
                        QueryDefaultPlaybackDevice();
                        Loaded?.Invoke(this, null);
                    }));
                }
                catch (Exception ex) when(ex.Is(Error.AUDCLNT_E_DEVICE_INVALIDATED))
                {
                    // Expected in some cases.
                }
            });

            Trace.WriteLine("AudioDeviceManager Create Exit");
        }
        internal WasApiAudioContext(IMMDeviceEnumerator enumerator)
        {
            _enumerator = enumerator;
            int error = _enumerator.EnumAudioEndpoints(DataFlow.Render, DeviceState.Active, out IMMDeviceCollection collection);

            if (error != 0)
            {
                Win32Platform.CheckError("Couldn't detect audio devices.", true);
                return;
            }

            // Get initial devices.
            error = collection.GetCount(out int count);
            if (error != 0)
            {
                Win32Platform.CheckError("Couldn't detect the number of audio devices.", true);
                return;
            }

            for (var i = 0; i < count; i++)
            {
                error = collection.Item(i, out IMMDevice device);
                if (error != 0)
                {
                    Win32Platform.CheckError($"Couldn't retrieve audio device of index {i}.");
                    continue;
                }

                ParseDevice(device);
            }

            // Find the default device.
            SetDefaultDevice();

            // Register to audio device events.
            _enumerator.RegisterEndpointNotificationCallback(this);
        }
예제 #13
0
 /// <summary>
 /// Registers a call back for Device Events
 /// </summary>
 /// <param name="client">Object implementing IMMNotificationClient type casted as IMMNotificationClient interface</param>
 /// <returns></returns>
 public int RegisterEndpointNotificationCallback(
     [In][MarshalAs(UnmanagedType.Interface)] IMMNotificationClient client)
 {
     return(realEnumerator.RegisterEndpointNotificationCallback(client));
 }
예제 #14
0
 /// <summary>
 /// Registers a client's notification callback interface.
 /// </summary>
 /// <param name="client">The <see cref="IMMNotificationClient"/> interface that the client is registering for notification callbacks.</param>
 /// <returns>An HRESULT code indicating whether the operation passed of failed.</returns>
 public void RegisterEndpointNotificationCallback(IMMNotificationClient client)
 {
     Marshal.ThrowExceptionForHR(_realEnumerator.RegisterEndpointNotificationCallback(client));
 }
 public void RegisterEndpointNotificationCallback(IMMNotificationClient client)
 {
     _realEnumerator.RegisterEndpointNotificationCallback(client);
 }
예제 #16
0
 public void RegisterEndpointNotificationCallback(IMMNotificationClient client)
 {
     _inner.RegisterEndpointNotificationCallback(client);
 }