public IAsyncOperation <DtoMediaDevices> GetAudioCaptureDevicesAsync()
        {
            RtcManager.Instance.EnsureRtcIsInitialized();
            var            media                 = RtcManager.Instance.Media;
            var            settings              = ApplicationData.Current.LocalSettings;
            var            audioCaptureDevices   = media.GetAudioCaptureDevices().ToArray().ToDto();
            DtoMediaDevice preferredAudioCapture = null;

            // Search for previously selected audio capture device and mark as preferred if found.
            // Otherwise fall back to default device.
            if (settings.Values.ContainsKey(MediaSettingsIds.AudioDeviceSettings))
            {
                var selectedAudioCaptureDevice = Array.Find(
                    audioCaptureDevices.Devices,
                    it => it.Id == (string)settings.Values[MediaSettingsIds.AudioDeviceSettings]);
                if (selectedAudioCaptureDevice != null)
                {
                    // Previously selected audio recording device found, mark as preferred.
                    selectedAudioCaptureDevice.IsPreferred = true;
                    preferredAudioCapture = selectedAudioCaptureDevice;
                }
            }
            if (preferredAudioCapture == null)
            {
                // Previously selected audio recording device is not found anymore,
                // probably removed.
                // Erase user preferrence and select default device.
                _audioDevice = null;
                ApplicationData.Current.LocalSettings.Values.Remove(MediaSettingsIds.AudioDeviceSettings);
            }
            return(Task.FromResult(audioCaptureDevices).AsAsyncOperation());
        }
        public IAsyncOperation <DtoMediaDevice> GetVideoDeviceAsync()
        {
            var settings = ApplicationData.Current.LocalSettings;

            if (_videoDevice == null && settings.Values.ContainsKey(MediaSettingsIds.VideoDeviceSettings))
            {
                var media = RtcManager.Instance.Media;
                var videoCaptureDevices = media.GetVideoCaptureDevices().ToArray().ToDto();
                _videoDevice = Array.Find(videoCaptureDevices.Devices,
                                          it => it.Id == (string)settings.Values[MediaSettingsIds.VideoDeviceSettings]);
            }
            return(Task.FromResult(_videoDevice).AsAsyncOperation());
        }
        public IAsyncAction SetVideoDeviceAsync(DtoMediaDevice device)
        {
            _videoDevice = device;
            var settings = ApplicationData.Current.LocalSettings;

            if (device != null)
            {
                settings.Values[MediaSettingsIds.VideoDeviceSettings] = device.Id;
                ETWEventLogger.Instance.LogEvent("Video Device Selected",
                                                 "name = " + device.Name,
                                                 DateTimeOffset.Now.ToUnixTimeMilliseconds().ToString());

                var  selectedVideoDevice       = device.FromDto();
                int  preferredCaptureWidth     = (int)settings.Values[MediaSettingsIds.PreferredVideoCaptureWidth];
                int  preferredCaptureHeight    = (int)settings.Values[MediaSettingsIds.PreferredVideoCaptureHeight];
                int  preferredCaptureFrameRate = (int)settings.Values[MediaSettingsIds.PreferredVideoCaptureFrameRate];
                bool isCapabilityValid         = false;
                if (selectedVideoDevice != null)
                {
                    var getTask = selectedVideoDevice.GetVideoCaptureCapabilities().AsTask();
                    getTask.Wait();
                    var capabilities = getTask.Result;
                    foreach (var capability in capabilities)
                    {
                        if (capability.FrameRate == preferredCaptureFrameRate &&
                            capability.Height == preferredCaptureHeight &&
                            capability.Width == preferredCaptureWidth)
                        {
                            isCapabilityValid = true;
                        }
                    }
                }
                if (!isCapabilityValid)
                {
                    preferredCaptureWidth = 640;
                    settings.Values[MediaSettingsIds.PreferredVideoCaptureWidth] = preferredCaptureWidth;
                    preferredCaptureHeight = 480;
                    settings.Values[MediaSettingsIds.PreferredVideoCaptureHeight] = preferredCaptureHeight;
                    preferredCaptureFrameRate = 30;
                    settings.Values[MediaSettingsIds.PreferredVideoCaptureFrameRate] = preferredCaptureFrameRate;
                }
                WebRTC.SetPreferredVideoCaptureFormat(preferredCaptureWidth, preferredCaptureHeight,
                                                      preferredCaptureFrameRate);
            }
            else
            {
                settings.Values.Remove(MediaSettingsIds.VideoDeviceSettings);
            }
            OnVideoDeviceSelectionChanged?.Invoke();
            return(Task.CompletedTask.AsAsyncAction());
        }
        public IAsyncOperation <DtoMediaDevices> GetVideoCaptureDevicesAsync()
        {
            RtcManager.Instance.EnsureRtcIsInitialized();
            var            media                 = RtcManager.Instance.Media;
            var            settings              = ApplicationData.Current.LocalSettings;
            var            videoCaptureDevices   = media.GetVideoCaptureDevices().ToArray().ToDto();
            DtoMediaDevice preferredVideoCapture = null;

            // Search for previously selected video capture device and mark as preferred if found.
            // Otherwise try to select first available device.
            if (settings.Values.ContainsKey(MediaSettingsIds.VideoDeviceSettings))
            {
                var selectedVideoCaptureDevice = Array.Find(
                    videoCaptureDevices.Devices,
                    it => it.Id == (string)settings.Values[MediaSettingsIds.VideoDeviceSettings]);
                if (selectedVideoCaptureDevice != null)
                {
                    // Previously selected video recording device found, mark as preferred.
                    selectedVideoCaptureDevice.IsPreferred = true;
                    preferredVideoCapture = selectedVideoCaptureDevice;
                    _videoDevice          = selectedVideoCaptureDevice;
                }
            }

            if (preferredVideoCapture == null)
            {
                // Previously selected video recording device is not found anymore,
                // probably removed.
                if (videoCaptureDevices.Devices.Length > 0)
                {
                    settings.Values[MediaSettingsIds.VideoDeviceSettings] = videoCaptureDevices.Devices[0].Id;
                    videoCaptureDevices.Devices[0].IsPreferred            = true;
                    _videoDevice = videoCaptureDevices.Devices[0];
                    OnVideoDeviceSelectionChanged?.Invoke();
                }
                else
                {
                    ApplicationData.Current.LocalSettings.Values.Remove(MediaSettingsIds.VideoDeviceSettings);
                    _videoDevice = null;
                    OnVideoDeviceSelectionChanged?.Invoke();
                }
            }
            return(Task.FromResult(videoCaptureDevices).AsAsyncOperation());
        }
        public IAsyncAction SetAudioPlayoutDeviceAsync(DtoMediaDevice device)
        {
            _audioPlayoutDevice = device;
            var settings = ApplicationData.Current.LocalSettings;

            if (device != null)
            {
                settings.Values[MediaSettingsIds.AudioPlayoutDeviceSettings] = device.Id;
                ETWEventLogger.Instance.LogEvent("Audio Playout Device Selected",
                                                 "name = " + device.Name,
                                                 DateTimeOffset.Now.ToUnixTimeMilliseconds().ToString());
            }
            else
            {
                //Use default device.
                settings.Values.Remove(MediaSettingsIds.AudioPlayoutDeviceSettings);
            }
            return(Task.CompletedTask.AsAsyncAction());
        }