public async Task <CaptureCapabilities> GetVideoCaptureCapabilitiesAsync(MediaDevice device) { if (_videoCaptureCap.ContainsKey(device.Id)) { return(_videoCaptureCap[device.Id]); } var settings = new MediaCaptureInitializationSettings { VideoDeviceId = device.Id, MediaCategory = MediaCategory.Communications }; using (var capture = new MediaCapture()) { await capture.InitializeAsync(settings); var caps = capture.VideoDeviceController.GetAvailableMediaStreamProperties(MediaStreamType.VideoRecord); var arr = new List <CaptureCapability>(); foreach (var cap in caps) { if (cap.Type != "Video") { continue; } var videoCap = (VideoEncodingProperties)cap; if (videoCap.FrameRate.Denominator == 0 || videoCap.FrameRate.Numerator == 0 || videoCap.Width == 0 || videoCap.Height == 0) { continue; } var captureCap = new CaptureCapability { Width = videoCap.Width, Height = videoCap.Height, FrameRate = videoCap.FrameRate.Numerator / videoCap.FrameRate.Denominator }; captureCap.FrameRateDescription = $"{captureCap.FrameRate} fps"; captureCap.ResolutionDescription = $"{captureCap.Width} x {captureCap.Height}"; captureCap.PixelAspectRatio = new MediaRatio { Numerator = videoCap.PixelAspectRatio.Numerator, Denominator = videoCap.PixelAspectRatio.Denominator }; captureCap.FullDescription = $"{captureCap.ResolutionDescription} {captureCap.FrameRateDescription}"; arr.Add(captureCap); } _videoCaptureCap.Add(device.Id, new CaptureCapabilities { Capabilities = arr.GroupBy(o => o.FullDescription).Select(o => o.First()).ToArray() }); return(_videoCaptureCap[device.Id]); } }
private void SetSelectedCapResItem() { var opCap = GetVideoCaptureCapabilitiesAsync(SelectedCamera); opCap.ContinueWith(caps => { var fpsList = from cap in caps.Result.Capabilities where cap.ResolutionDescription == SelectedCapResItem select cap; var t = CoreApplication.MainView.CoreWindow.Dispatcher.RunAsync(CoreDispatcherPriority.Normal, async() => { CaptureCapability defaultFps = null; uint selectedCapFpsFrameRate = 0; if (_localSettings.Values[SelectedFrameRateId] != null) { selectedCapFpsFrameRate = (uint)_localSettings.Values[SelectedFrameRateId]; } if (AllCapFps == null) { AllCapFps = new ObservableCollection <CaptureCapability>(); } else { AllCapFps.Clear(); } foreach (var fps in fpsList) { if (selectedCapFpsFrameRate != 0 && fps.FrameRate == selectedCapFpsFrameRate) { defaultFps = fps; } AllCapFps.Add(fps); if (defaultFps == null) { defaultFps = fps; } } SelectedCapFpsItem = defaultFps; if (SelectedCapFpsItem == null) { return; } await _mediaSettings.SetPreferredVideoCaptureFormatAsync( new VideoCaptureFormat((int)SelectedCapFpsItem.Width, (int)SelectedCapFpsItem.Height, (int)SelectedCapFpsItem.FrameRate)); }); var uiTask = _dispatcher.RunAsync(CoreDispatcherPriority.Normal, () => { OnPropertyChanged(nameof(AllCapFps)); }); }); }
public static DtoCaptureCapability ToDto(this CaptureCapability obj) { return(new DtoCaptureCapability { FrameRate = obj.FrameRate, FrameRateDescription = obj.FrameRateDescription, FullDescription = obj.FullDescription, Height = obj.Height, PixelAspectRatio = ToDto(obj.PixelAspectRatio), ResolutionDescription = obj.ResolutionDescription, Width = obj.Width }); }
private async void SetSelectedCamera() { var capRes = new List <string>(); CaptureCapabilities captureCapabilities = null; if (SelectedCamera == null) { return; } try { captureCapabilities = await _mediaSettings.GetVideoCaptureCapabilitiesAsync(SelectedCamera); } catch (Exception ex) { while (ex is AggregateException && ex.InnerException != null) { ex = ex.InnerException; } string errorMsg = "SetSelectedCamera: Failed to GetVideoCaptureCapabilities (Error: " + ex.Message + ")"; Debug.WriteLine(errorMsg); var msgDialog = new MessageDialog(errorMsg); await msgDialog.ShowAsync(); return; } if (captureCapabilities == null) { string errorMsg = "SetSelectedCamera: Failed to GetVideoCaptureCapabilities (Result is null)"; Debug.WriteLine(errorMsg); var msgDialog = new MessageDialog(errorMsg); await msgDialog.ShowAsync(); return; } var uniqueRes = captureCapabilities.Capabilities.GroupBy(test => test.ResolutionDescription) .Select(grp => grp.FirstOrDefault()).ToList(); CaptureCapability defaultResolution = null; foreach (var resolution in uniqueRes) { if (defaultResolution == null) { defaultResolution = resolution; } capRes.Add(resolution.ResolutionDescription); if ((resolution.Width == 640) && (resolution.Height == 480)) { defaultResolution = resolution; } } string selectedCapResItem = string.Empty; if (_localSettings.Values[nameof(SelectedCapResItem)] != null) { selectedCapResItem = (string)_localSettings.Values[nameof(SelectedCapResItem)]; } AllCapRes = new ObservableCollection <string>(capRes); if (!string.IsNullOrEmpty(selectedCapResItem) && AllCapRes.Contains(selectedCapResItem)) { SelectedCapResItem = selectedCapResItem; } else { SelectedCapResItem = defaultResolution.ResolutionDescription; } }