예제 #1
0
        internal static OutputDeviceModel GetDeviceMock(string id, string name, IDeviceStateNotificationHandler deviceStateHandler)
        {
            var master   = MasterSessionModelTests.GetMasterMock(name, 70, false, null);
            var sessions = new ObservableCollection <AudioSessionModel>();

            sessions.Add(AudioSessionModelTests.GetSessionMock("app", 50, false));

            var deviceMock = new Mock <IDevice>();

            deviceMock.Setup(m => m.GetId()).Returns(id);

            var sProviderMock = new Mock <ISessionProvider>();

            return(new OutputDeviceModel(deviceMock.Object, deviceStateHandler, sProviderMock.Object, master,
                                         sessions));
        }
예제 #2
0
        public List <OutputDeviceModel> GetCurrentActiveDevices(IDeviceStateNotificationHandler dsn)
        {
            var devices = new List <OutputDeviceModel>();
            IMMDeviceCollection activeDevices = null;
            uint count = 0;

            try
            {
                Marshal.ThrowExceptionForHR(this.MMDeviceEnumerator.EnumAudioEndpoints(EDataFlow.Render, DeviceState.Active, out activeDevices));
                activeDevices?.GetCount(out count);
                if (count == 0)
                {
                    return(devices);
                }
            }
            catch (Exception e)
            {
                Logger.Error($"Failed to get output devices collection", e);
                return(devices);
            }

            for (uint i = 0; i < count; i++)
            {
                try
                {
                    IMMDevice dev = null;
                    activeDevices?.Item(i, out dev);
                    if (dev == null)
                    {
                        continue;
                    }
                    var device      = new MMDevice(dev);
                    var deviceModel = device.GetOutputDeviceModel(dsn);
                    if (deviceModel != null)
                    {
                        devices.Add(deviceModel);
                    }
                }
                catch (Exception e)
                {
                    Logger.Error($"Failed to enumerate output devices", e);
                }
            }
            return(devices);
        }
예제 #3
0
        internal static OutputDeviceModel GetOutputDeviceModel(this IDevice device, IDeviceStateNotificationHandler deviceStateNotificationHandler)
        {
            try
            {
                var sessionManager =
                    (IAudioSessionManager2)device.Activate(new Guid(GuidValue.External.IAudioSessionManager2));
                sessionManager.GetSessionEnumerator(out IAudioSessionEnumerator sessionEnum);

                var sessions        = device.GetCurrentSessionList(sessionEnum);
                var sessionProvider = new AudioSessionProvider(sessionManager);
                var master          = device.GetMaster();

                return(new OutputDeviceModel(device, deviceStateNotificationHandler, sessionProvider, master, sessions));
            }
            catch (Exception e)
            {
                Logger.Error("Failed to create output device model", e);
                return(null);
            }
        }
예제 #4
0
 public static DeviceProvider GetDeviceProviderMock(IDeviceEnumerator deviceEnum, IDeviceStateNotificationHandler deviceState)
 {
     return((DeviceProvider)Activator.CreateInstance(typeof(DeviceProvider),
                                                     BindingFlags.NonPublic | BindingFlags.Instance, null,
                                                     new object[] { deviceEnum, deviceState }, CultureInfo.InvariantCulture,
                                                     null));
 }