/// <summary>
        /// Starts reading from the newly selected source.
        /// </summary>
        private async void SourceComboBox_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            await StopReaderAsync();

            if (SourceComboBox.SelectedItem != null)
            {
                await StartReaderAsync();

                IEnumerable <FrameFormatModel> formats = null;
                if (_mediaCapture != null && _source != null)
                {
                    formats = _source.SupportedFormats
                              .Where(format => FrameRenderer.GetSubtypeForFrameReader(_source.Info.SourceKind, format) != null)
                              .Select(format => new FrameFormatModel(format));
                }

                FormatComboBox.ItemsSource = formats;
            }
        }
Exemplo n.º 2
0
        private async void SourceComboBox_SelectionChanged2(object sender, SelectionChangedEventArgs e)
        {
            await StopReaderAsync2();

            if (SourceComboBox2.SelectedItem != null)
            {
                await StartReaderAsync2();

                // Get the formats supported by the selected source into the FormatComboBox.
                IEnumerable <FrameFormatModel> formats = null;
                if (_mediaCapture2 != null && _source2 != null)
                {
                    // Limit ourselves to formats that we can render.
                    formats = _source2.SupportedFormats
                              .Where(format => FrameRenderer.GetSubtypeForFrameReader(_source2.Info.SourceKind, format) != null)
                              .Select(format => new FrameFormatModel(format));
                }

                FormatComboBox2.ItemsSource = formats;
            }
        }
        /// <summary>
        /// Creates a frame reader from the current frame source and registers to handle its frame events.
        /// </summary>
        private async Task CreateReaderAsync()
        {
            await InitializeCaptureAsync();

            UpdateFrameSource();

            if (_source != null)
            {
                string requestedSubtype = FrameRenderer.GetSubtypeForFrameReader(_source.Info.SourceKind, _source.CurrentFormat);
                if (requestedSubtype != null)
                {
                    _reader = await _mediaCapture.CreateFrameReaderAsync(_source, requestedSubtype);

                    _reader.FrameArrived += Reader_FrameArrived;

                    _logger.Log($"Reader created on source: {_source.Info.Id}");
                }
                else
                {
                    _logger.Log($"Cannot render current format on source: {_source.Info.Id}");
                }
            }
        }
Exemplo n.º 4
0
        private async Task CreateReaderAsync2()
        {
            await InitializeCaptureAsync2();

            UpdateFrameSource2();

            if (_source2 != null)
            {
                // Ask the MediaFrameReader to use a subtype that we can render.
                string requestedSubtype = FrameRenderer.GetSubtypeForFrameReader(_source2.Info.SourceKind, _source2.CurrentFormat);
                if (requestedSubtype != null)
                {
                    _reader2 = await _mediaCapture2.CreateFrameReaderAsync(_source2, requestedSubtype);

                    _reader2.FrameArrived += Reader_FrameArrived2;

                    _logger2.Log($"Reader created on source: {_source2.Info.Id}");
                }
                else
                {
                    _logger.Log($"Cannot render current format on source: {_source2.Info.Id}");
                }
            }
        }
Exemplo n.º 5
0
        /// <summary>
        /// Switches to the next camera source and starts reading frames.
        /// </summary>
        private async Task PickNextMediaSourceWorkerAsync()
        {
            await CleanupMediaCaptureAsync();

            var allGroups = await MediaFrameSourceGroup.FindAllAsync();

            if (allGroups.Count == 0)
            {
                _logger.Log("No source groups found.");
                return;
            }

            // Pick next group in the array after each time the Next button is clicked.
            _groupSelectionIndex = (_groupSelectionIndex + 1) % allGroups.Count;
            var selectedGroup = allGroups[_groupSelectionIndex];

            _logger.Log($"Found {allGroups.Count} groups and selecting index [{_groupSelectionIndex}]: {selectedGroup.DisplayName}");

            try
            {
                // Initialize MediaCapture with selected group.
                // This can raise an exception if the source no longer exists,
                // or if the source could not be initialized.
                await InitializeMediaCaptureAsync(selectedGroup);
            }
            catch (Exception exception)
            {
                _logger.Log($"MediaCapture initialization error: {exception.Message}");
                await CleanupMediaCaptureAsync();

                return;
            }

            // Set up frame readers, register event handlers and start streaming.
            var startedKinds = new HashSet <MediaFrameSourceKind>();

            foreach (MediaFrameSource source in _mediaCapture.FrameSources.Values)
            {
                MediaFrameSourceKind kind = source.Info.SourceKind;

                // Ignore this source if we already have a source of this kind.
                if (startedKinds.Contains(kind))
                {
                    continue;
                }

                // Look for a format which the FrameRenderer can render.
                string requestedSubtype = null;
                foreach (MediaFrameFormat format in source.SupportedFormats)
                {
                    requestedSubtype = FrameRenderer.GetSubtypeForFrameReader(kind, format);
                    if (requestedSubtype != null)
                    {
                        // Tell the source to use the format we can render.
                        await source.SetFormatAsync(format);

                        break;
                    }
                }
                if (requestedSubtype == null)
                {
                    // No acceptable format was found. Ignore this source.
                    continue;
                }

                MediaFrameReader frameReader = await _mediaCapture.CreateFrameReaderAsync(source, requestedSubtype);

                frameReader.FrameArrived += FrameReader_FrameArrived;
                _sourceReaders.Add(frameReader);

                MediaFrameReaderStartStatus status = await frameReader.StartAsync();

                if (status == MediaFrameReaderStartStatus.Success)
                {
                    _logger.Log($"Started {kind} reader.");
                    startedKinds.Add(kind);
                }
                else
                {
                    _logger.Log($"Unable to start {kind} reader. Error: {status}");
                }
            }

            if (startedKinds.Count == 0)
            {
                _logger.Log($"No eligible sources in {selectedGroup.DisplayName}.");
            }
        }