// </SnippetMultiFrameDeclarations> private async void InitMultiFrame() { // <SnippetSelectColorAndDepth> var allGroups = await MediaFrameSourceGroup.FindAllAsync(); var eligibleGroups = allGroups.Select(g => new { Group = g, // For each source kind, find the source which offers that kind of media frame, // or null if there is no such source. SourceInfos = new MediaFrameSourceInfo[] { g.SourceInfos.FirstOrDefault(info => info.SourceKind == MediaFrameSourceKind.Color), g.SourceInfos.FirstOrDefault(info => info.SourceKind == MediaFrameSourceKind.Depth) } }).Where(g => g.SourceInfos.Any(info => info != null)).ToList(); if (eligibleGroups.Count == 0) { System.Diagnostics.Debug.WriteLine("No source group with color, depth or infrared found."); return; } var selectedGroupIndex = 0; // Select the first eligible group MediaFrameSourceGroup selectedGroup = eligibleGroups[selectedGroupIndex].Group; MediaFrameSourceInfo colorSourceInfo = eligibleGroups[selectedGroupIndex].SourceInfos[0]; MediaFrameSourceInfo depthSourceInfo = eligibleGroups[selectedGroupIndex].SourceInfos[1]; // </SnippetSelectColorAndDepth> // <SnippetMultiFrameInitMediaCapture> mediaCapture = new MediaCapture(); var settings = new MediaCaptureInitializationSettings() { SourceGroup = selectedGroup, SharingMode = MediaCaptureSharingMode.ExclusiveControl, MemoryPreference = MediaCaptureMemoryPreference.Cpu, StreamingCaptureMode = StreamingCaptureMode.Video }; await mediaCapture.InitializeAsync(settings); // </SnippetMultiFrameInitMediaCapture> // <SnippetGetColorAndDepthSource> MediaFrameSource colorSource = mediaCapture.FrameSources.Values.FirstOrDefault( s => s.Info.SourceKind == MediaFrameSourceKind.Color); MediaFrameSource depthSource = mediaCapture.FrameSources.Values.FirstOrDefault( s => s.Info.SourceKind == MediaFrameSourceKind.Depth); if (colorSource == null || depthSource == null) { System.Diagnostics.Debug.WriteLine("MediaCapture doesn't have the Color and Depth streams"); return; } _colorSourceId = colorSource.Info.Id; _depthSourceId = depthSource.Info.Id; // </SnippetGetColorAndDepthSource> // <SnippetInitMultiFrameReader> _multiFrameReader = await mediaCapture.CreateMultiSourceFrameReaderAsync( new[] { colorSource, depthSource }); _multiFrameReader.FrameArrived += MultiFrameReader_FrameArrived; _frameRenderer = new FrameRenderer(imageElement); MultiSourceMediaFrameReaderStartStatus startStatus = await _multiFrameReader.StartAsync(); if (startStatus != MultiSourceMediaFrameReaderStartStatus.Success) { throw new InvalidOperationException( "Unable to start reader: " + startStatus); } this.CorrelationFailed += MainPage_CorrelationFailed; Task.Run(() => NotifyAboutCorrelationFailure(_tokenSource.Token)); // </SnippetInitMultiFrameReader> }
private async void init() { var frameSourceGroups = await MediaFrameSourceGroup.FindAllAsync(); LogError("checkpoint 1.1"); var targetGroups = frameSourceGroups.Select(g => new { Group = g, SourceInfos = new MediaFrameSourceInfo[] { g.SourceInfos.FirstOrDefault(info => info.SourceKind == MediaFrameSourceKind.Color), g.SourceInfos.FirstOrDefault(info => info.SourceKind == MediaFrameSourceKind.Depth), } }).Where(g => g.SourceInfos.Any(info => info != null)).ToList(); LogError("checkpoint 1.2"); if (targetGroups.Count == 0) { LogError("No source groups found."); return; } MediaFrameSourceGroup mediaSourceGroup = targetGroups[0].Group; LogError("checkpoint 1.3"); mediaCapture = new MediaCapture(); LogError("checkpoint 1.4"); var settings = new MediaCaptureInitializationSettings() { SourceGroup = mediaSourceGroup, SharingMode = MediaCaptureSharingMode.ExclusiveControl, MemoryPreference = MediaCaptureMemoryPreference.Cpu, StreamingCaptureMode = StreamingCaptureMode.Video }; LogError("checkpoint 1.5"); await mediaCapture.InitializeAsync(settings); LogError("checkpoint 1.6"); MediaFrameSource colorSource = mediaCapture.FrameSources.Values.FirstOrDefault( s => s.Info.SourceKind == MediaFrameSourceKind.Color); MediaFrameSource depthSource = mediaCapture.FrameSources.Values.FirstOrDefault( s => s.Info.SourceKind == MediaFrameSourceKind.Depth); LogError("checkpoint 1.7"); if (colorSource == null || depthSource == null) { LogError("Cannot find color or depth stream."); return; } MediaFrameFormat colorFormat = colorSource.SupportedFormats.Where(format => { return(format.VideoFormat.Width >= 640 && format.Subtype == MediaEncodingSubtypes.Rgb24); }).FirstOrDefault(); MediaFrameFormat depthFormat = depthSource.SupportedFormats.Where(format => { return(format.VideoFormat.Width >= 640 && format.Subtype == MediaEncodingSubtypes.D16); }).FirstOrDefault(); await colorSource.SetFormatAsync(colorFormat); await depthSource.SetFormatAsync(depthFormat); _colorSourceId = colorSource.Info.Id; _depthSourceId = depthSource.Info.Id; _frameReader = await mediaCapture.CreateMultiSourceFrameReaderAsync( new[] { colorSource, depthSource }); _frameReader.FrameArrived += FrameReader_FrameArrived; MultiSourceMediaFrameReaderStartStatus startStatus = await _frameReader.StartAsync(); if (startStatus != MultiSourceMediaFrameReaderStartStatus.Success) { throw new InvalidOperationException("Unable to start reader: " + startStatus); } this.CorrelationFailed += MainPage_CorrelationFailed; Task.Run(() => NotifyAboutCorrelationFailure(_tokenSource.Token)); }