private async Task SelectFallbackSourceGroupAsync()
        {
#if USE_INFRARED
            var devices = await DeviceInformation.FindAllAsync(MediaFrameSourceGroup.GetDeviceSelector());

            foreach (var deviceInformation in devices)
            {
                var sourceGroup = await MediaFrameSourceGroup.FromIdAsync(deviceInformation.Id);

                var colorSourceInfos  = sourceGroup.SourceInfos.Where(sourceInfo => sourceInfo.SourceKind == MediaFrameSourceKind.Color);
                var enclosureLocation = colorSourceInfos.Select(sourceInfo => sourceInfo?.DeviceInformation?.EnclosureLocation).FirstOrDefault(enclosure => enclosure != null);

                if (colorSourceInfos.Any() &&
                    enclosureLocation.Panel == InfraredEnclosureLocation.Panel)
                {
                    FallbackSourceGroup    = sourceGroup;
                    ExclusiveRgbSourceInfo = colorSourceInfos.OrderByDescending(sourceInfo => sourceInfo?.MediaStreamType == ExclusiveIrSourceInfo.MediaStreamType).First();
                    ColorEnclosureLocation = enclosureLocation;
                    break;
                }
            }
#else
            SharedSourceInfo  = ExclusiveRgbSourceInfo;
            SharedSourceGroup = ExclusiveSourceGroup;
#endif
        }
Beispiel #2
0
        /// <summary>
        /// Adds a SourceGroup with given Id to the collection.
        /// </summary>
        private async Task AddDeviceAsync(string id)
        {
            var group = await MediaFrameSourceGroup.FromIdAsync(id);

            if (group != null)
            {
                await _dispatcher.RunAsync(CoreDispatcherPriority.Normal, () =>
                {
                    _sourceCollection.Add(new FrameSourceGroupModel(group));
                });
            }
        }
Beispiel #3
0
        private async Task _AddAcceptableSourceGroupAsync(string deviceId)
        {
            // Accept any user facing IR camera
            var sourceGroup = await MediaFrameSourceGroup.FromIdAsync(deviceId);

            if (sourceGroup != null && (sourceGroup.SourceInfos.Count > 1) &&
                sourceGroup.SourceInfos.Any(source =>
                                            source.SourceKind == MediaFrameSourceKind.Infrared))
            {
                if (sourceGroup.SourceInfos.Any(source =>
                                                source.SourceKind == MediaFrameSourceKind.Color))
                {
                    _sourceGroups[deviceId] = sourceGroup;
                    if (!_isOpened)
                    {
                        _displayAutoEvent.Set();
                        _isOpened = true;
                    }
                }
            }
        }
        private async Task InitializeCameraAsync()
        {
            if (_mediaCapture == null)
            {
                _mediaCapture = new MediaCapture();
            }

            if (_allVideoDevices == null)
            {
                return;
            }

            if (_allVideoDevices.Count == 0)
            {
                Debug.WriteLine("ERROR: no webcam found or available");
                return;
            }

            Object storedVideoDeviceId = _localSettings.Values["CurrentVideoDeviceId"];

            if (storedVideoDeviceId == null)
            {
                storedVideoDeviceId = _allVideoDevices[0].Id;
                _localSettings.Values["CurrentVideoDeviceId"] = storedVideoDeviceId;
                CameraComboBox.SelectedIndex = 0;
                Debug.WriteLine("INFO: no webcam configured. Choosing the first available: " + _allVideoDevices[0].Name);
            }
            else
            {
                Debug.WriteLine("Loaded from Settings - CurrentVideoDeviceId: " + storedVideoDeviceId.ToString());
            }

            var mediaFrameSourceGroup = await MediaFrameSourceGroup.FromIdAsync(storedVideoDeviceId.ToString());

            MediaFrameSourceInfo selectedMediaFrameSourceInfo = null;

            foreach (MediaFrameSourceInfo sourceInfo in mediaFrameSourceGroup.SourceInfos)
            {
                if (sourceInfo.MediaStreamType == MediaStreamType.VideoRecord)
                {
                    selectedMediaFrameSourceInfo = sourceInfo;
                    break;
                }
            }
            if (selectedMediaFrameSourceInfo == null)
            {
                Debug.WriteLine("no compatible MediaSource found.");
                return;
            }

            MediaCaptureInitializationSettings mediaSettings = new MediaCaptureInitializationSettings();

            mediaSettings.VideoDeviceId        = storedVideoDeviceId.ToString();
            mediaSettings.SourceGroup          = mediaFrameSourceGroup;
            mediaSettings.StreamingCaptureMode = StreamingCaptureMode.Video;
            mediaSettings.SharingMode          = MediaCaptureSharingMode.ExclusiveControl;
            mediaSettings.MemoryPreference     = MediaCaptureMemoryPreference.Cpu;


            try
            {
                await _mediaCapture.InitializeAsync(mediaSettings);
            }
            catch (Exception ex)
            {
                System.Diagnostics.Debug.WriteLine("MediaCapture initialization failed: " + ex.Message);
                return;
            }
            Debug.WriteLine("MediaFrameCapture initialized");

            var mediaFrameSource = _mediaCapture.FrameSources[selectedMediaFrameSourceInfo.Id];

            imageElement.Source = new SoftwareBitmapSource();

            MediaRatio mediaRatio = mediaFrameSource.CurrentFormat.VideoFormat.MediaFrameFormat.FrameRate;

            _sourceFrameRate = (double)mediaRatio.Numerator / (double)mediaRatio.Denominator;
            CalculateFrameRate();

            _mediaFrameReader = await _mediaCapture.CreateFrameReaderAsync(mediaFrameSource, MediaEncodingSubtypes.Argb32);

            _mediaFrameReader.AcquisitionMode = MediaFrameReaderAcquisitionMode.Realtime;
            _mediaFrameReader.FrameArrived   += ColorFrameReader_FrameArrivedAsync;
            await _mediaFrameReader.StartAsync();

            Debug.WriteLine("MediaFrameReader StartAsync done ");

            _isInitialized = true;
            UpdateCaptureControls();
        }
        private async Task MJPEGStreamerInitAsync()
        {
            Debug.WriteLine("Trying MJPEGStreamerInitAsync");
            if (_mjpegStreamerInitialized || _mjpegStreamerIsInitializing)
            {
                return;
            }
            _mjpegStreamerInitialized    = true;
            _mjpegStreamerIsInitializing = true;

            if (_mediaCapture == null)
            {
                _mediaCapture = new MediaCapture();
            }


            Debug.WriteLine("MJPEGStreamerInitAsync: now reading  settings");
            var portSetting = _localSettings.Values["HttpServerPort"];

            if (portSetting != null)
            {
                TextBoxPort.Text = validatePortNumber(portSetting.ToString()).ToString();
                _httpServerPort  = (int)portSetting;
            }
            else
            {
                TextBoxPort.Text = _defaultPort.ToString();
                _httpServerPort  = _defaultPort;
            }

            var videoRotationInt = _localSettings.Values["VideoRotation"];

            if (videoRotationInt != null)
            {
                _videoRotation = (int)videoRotationInt;
            }

            var imageQualitySingle = _localSettings.Values["ImageQuality"];

            if (imageQualitySingle != null)
            {
                _imageQuality = (double)imageQualitySingle;
            }
            else
            {
                _imageQuality = (double)0.3;
            }
            ImageQualitySlider.Value = _imageQuality * 100;

            var frameRate = _localSettings.Values["FrameRate"];

            if (frameRate != null)
            {
                _frameRate = (int)frameRate;
            }
            else
            {
                _frameRate = (int)5;
            }
            FrameRateSlider.Value = _frameRate;



            var preview = _localSettings.Values["Preview"];

            if (preview != null)
            {
                Debug.WriteLine("preview setting found: " + (bool)preview);
                _previewVideoEnabled = (bool)preview;
            }
            else
            {
                Debug.WriteLine("no Preview setting found, setting to default ON.");
                _previewVideoEnabled = true;
            }
            PreviewToggleSwitch.IsOn = _previewVideoEnabled;
            UpdatePreviewState();


            _allVideoDevices = await DeviceInformation.FindAllAsync(DeviceClass.VideoCapture);

            var id = _localSettings.Values["CurrentVideoDeviceId"];

            if (id == null)
            {
                id = "********no setting*****";
            }

            Debug.WriteLine("_allVideoDevices read");

            _currentVideoDeviceIndex = -1;
            int count = 0;

            CameraComboBox.Items.Clear();
            foreach (DeviceInformation di in _allVideoDevices)
            {
                CameraComboBox.Items.Add(di.Name);

                if (di.Id.Equals(id.ToString(), StringComparison.OrdinalIgnoreCase))
                {
                    _currentVideoDeviceIndex     = count;
                    CameraComboBox.SelectedIndex = count;
                    Debug.WriteLine("CAMERA INDEX FOUND");
                }
                count++;

                Debug.WriteLine("Video Device: {0} ID:{1}", di.Name, di.Id);
                var mediaFrameSourceGroup = await MediaFrameSourceGroup.FromIdAsync(di.Id);

                foreach (var si in mediaFrameSourceGroup.SourceInfos)
                {
                    Debug.WriteLine("   " + si.MediaStreamType.ToString() + " " + si.SourceKind.ToString());
                }
            }

            await StartServer();

            _mjpegStreamerIsInitializing = false;
        }
        private async Task SelectExclusiveSourceGroupAsync()
        {
#if USE_INFRARED
            var devices = await DeviceInformation.FindAllAsync(MediaFrameSourceGroup.GetDeviceSelector());

            foreach (var deviceInformation in devices)
            {
                var sourceGroup = await MediaFrameSourceGroup.FromIdAsync(deviceInformation.Id);

                var   current             = new DefaultDictionary <MediaFrameSourceKind, Dictionary <MediaStreamType, MediaFrameSourceInfo> >();
                ulong currentIrCapability = 0u;

                foreach (var sourceInfo in sourceGroup.SourceInfos)
                {
                    var originSourceGroupId = sourceInfo.Id.Split('@').ElementAtOrDefault(1) ?? sourceInfo.Id;

                    EnclosureLocations[originSourceGroupId] = EnclosureLocations[originSourceGroupId] ?? deviceInformation.EnclosureLocation;

                    if (sourceInfo.MediaStreamType != MediaStreamType.VideoPreview &&
                        sourceInfo.MediaStreamType != MediaStreamType.VideoRecord)
                    {
                        continue;
                    }

                    switch (sourceInfo.SourceKind)
                    {
                    case MediaFrameSourceKind.Color:
                        current[sourceInfo.SourceKind][sourceInfo.MediaStreamType] = sourceInfo;
                        break;

                    case MediaFrameSourceKind.Infrared:
                        if (sourceInfo.Properties.TryGetValue(MF._DEVICESTREAM_ATTRIBUTE_FACEAUTH_CAPABILITY, out var capability))
                        {
                            if (capability is ulong ulCapability &&
                                (ulCapability & (
                                     KS.CAMERA_EXTENDEDPROP_FACEAUTH_MODE_ALTERNATIVE_FRAME_ILLUMINATION |
                                     KS.CAMERA_EXTENDEDPROP_FACEAUTH_MODE_BACKGROUND_SUBTRACTION)) != 0)
                            {
                                currentIrCapability = ulCapability;
                                current[sourceInfo.SourceKind][sourceInfo.MediaStreamType] = sourceInfo;
                            }
                        }
                        break;
                    }
                }

                if (current[MediaFrameSourceKind.Infrared].Any())
                {
                    var score = GetSourceGroupScore(current);
                    if (score <= ExclusiveSourceGroupScore)
                    {
                        continue;
                    }

                    var preferredMediaStreamType = current[MediaFrameSourceKind.Infrared].Keys
                                                   .Intersect(current[MediaFrameSourceKind.Color].Keys)
                                                   .DefaultIfEmpty(MediaStreamType.VideoPreview)
                                                   .OrderByDescending(mediaStreamType => mediaStreamType == MediaStreamType.VideoPreview)
                                                   .First();

                    ExclusiveIrSourceInfo     = current[MediaFrameSourceKind.Infrared].OrderByDescending(kvp => kvp.Key == preferredMediaStreamType).First().Value;
                    ExclusiveRgbSourceInfo    = current[MediaFrameSourceKind.Color].OrderByDescending(kvp => kvp.Key == ExclusiveIrSourceInfo.MediaStreamType).Select(kvp => kvp.Value).FirstOrDefault();
                    ExclusiveSourceGroup      = sourceGroup;
                    ExclusiveIrCapability     = currentIrCapability;
                    ExclusiveSourceGroupScore = score;

                    if ((ExclusiveRgbSourceInfo != null) &&
                        deviceInformation.EnclosureLocation != null)
                    {
                        break;
                    }
                }
            }

            if (ExclusiveIrSourceInfo != null)
            {
                EnclosureLocations.TryGetValue(ExclusiveIrSourceInfo.Id.Split('@').ElementAtOrDefault(1) ?? ExclusiveIrSourceInfo.Id, out InfraredEnclosureLocation);
            }

            if (ExclusiveRgbSourceInfo != null)
            {
                EnclosureLocations.TryGetValue(ExclusiveRgbSourceInfo.Id.Split('@').ElementAtOrDefault(1) ?? ExclusiveRgbSourceInfo.Id, out ColorEnclosureLocation);
            }
#else
            var sourceGroups = await MediaFrameSourceGroup.FindAllAsync();

            if (sourceGroups.Any() && sourceGroups.First() is MediaFrameSourceGroup sourceGroup)
            {
                ExclusiveRgbSourceInfo = sourceGroup.SourceInfos.OrderByDescending(si => si.MediaStreamType == MediaStreamType.VideoPreview).First();
                ExclusiveSourceGroup   = sourceGroup;
            }
#endif

            if (ExclusiveRgbSourceInfo == null)
            {
                UseFallbackSourceGroup = true;
            }
        }