public static VideoResolutionWidthHeight Get(VideoResolution videoResolution)
        {
            var videoResolutionWidthHeight = new VideoResolutionWidthHeight();

            if (videoResolution == VideoResolution.HD1080p)
            {
                videoResolutionWidthHeight.Width  = 1920;
                videoResolutionWidthHeight.Height = 1080;
            }
            else if (videoResolution == VideoResolution.HD720p)
            {
                videoResolutionWidthHeight.Width  = 1280;
                videoResolutionWidthHeight.Height = 720;
            }
            else if (videoResolution == VideoResolution.SD1024_768)
            {
                videoResolutionWidthHeight.Width  = 1024;
                videoResolutionWidthHeight.Height = 768;
            }
            else if (videoResolution == VideoResolution.SD800_600)
            {
                videoResolutionWidthHeight.Width  = 640;
                videoResolutionWidthHeight.Height = 480;
            }
            else if (videoResolution == VideoResolution.SD640_480)
            {
                videoResolutionWidthHeight.Width  = 640;
                videoResolutionWidthHeight.Height = 480;
            }

            return(videoResolutionWidthHeight);
        }
        public static void SetSupportedVideoFrameFormats(List <MediaFrameFormat> mediaFrameFormats)
        {
            VideoSettingsSupported = new JsonArray();

            var videoSettings = new List <VideoSetting>();

            for (int videoSubTypeId = 0; videoSubTypeId <= 2; videoSubTypeId++)
            {
                var videoSubType = (VideoSubtype)videoSubTypeId;

                for (int videoResolutionId = 0; videoResolutionId <= 4; videoResolutionId++)
                {
                    var videoResolution            = (VideoResolution)videoResolutionId;
                    var videoResolutionWidthHeight = VideoResolutionWidthHeight.Get(videoResolution);

                    var mediaFrameFormat = mediaFrameFormats.FirstOrDefault(m => m.Subtype == VideoSubtypeHelper.Get((VideoSubtype)videoSubType) &&
                                                                            m.VideoFormat.Width == videoResolutionWidthHeight.Width &&
                                                                            m.VideoFormat.Height == videoResolutionWidthHeight.Height);

                    if (mediaFrameFormat != null)
                    {
                        videoSettings.Add(new VideoSetting
                        {
                            VideoResolution = videoResolution,
                            VideoSubtype    = videoSubType
                        });
                    }
                }
            }

            foreach (var videoSettingGrouped in videoSettings.GroupBy(v => v.VideoSubtype))
            {
                var videoSubType           = VideoSubtypeHelper.Get(videoSettingGrouped.Key);
                var videoSettingsSupported = new JsonArray();

                foreach (var videoSetting in videoSettingGrouped)
                {
                    videoSettingsSupported.Add(JsonValue.CreateNumberValue((int)videoSetting.VideoResolution));
                }

                VideoSettingsSupported.Add(new JsonObject
                {
                    { "VideoSubtype", JsonValue.CreateStringValue(videoSubType) },
                    { "VideoResolutions", videoSettingsSupported }
                });
            }
        }
        public static async Task <VideoSetting> Read(List <MediaFrameFormat> mediaFrameFormats)
        {
            VideoSetting videoSetting            = null;
            JsonObject   configuration           = null;
            var          configurationFileExists = await ApplicationData.Current.LocalFolder.TryGetItemAsync(CONFIGURATION_FILE_NAME);

            if (configurationFileExists != null)
            {
                var configurationFile = await ApplicationData.Current.LocalFolder.GetFileAsync(CONFIGURATION_FILE_NAME);

                configuration = JsonObject.Parse(await FileIO.ReadTextAsync(configurationFile));
            }

            if (configuration != null)
            {
                var videoResolution            = (VideoResolution)configuration["VideoResolution"].GetNumber();
                var videoResolutionWidthHeight = VideoResolutionWidthHeight.Get(videoResolution);

                var videoSubType = configuration["VideoSubtype"].GetString();
                var videoQuality = configuration["VideoQuality"].GetNumber();
                var usedThreads  = configuration["UsedThreads"].GetNumber();

                var mediaFrameFormat = mediaFrameFormats.Where(m => m.Subtype == videoSubType &&
                                                               m.VideoFormat.Width == videoResolutionWidthHeight.Width &&
                                                               m.VideoFormat.Height == videoResolutionWidthHeight.Height)
                                       .OrderByDescending(m => m.FrameRate.Numerator / m.FrameRate.Denominator)
                                       .FirstOrDefault();
                if (mediaFrameFormat != null)
                {
                    videoSetting = new VideoSetting
                    {
                        VideoResolution = videoResolution,
                        VideoSubtype    = VideoSubtypeHelper.Get(videoSubType),
                        VideoQuality    = videoQuality,
                        UsedThreads     = (int)usedThreads
                    };
                }
            }
            else
            {
                for (int videoSubType = 0; videoSubType <= 2; videoSubType++)
                {
                    if (videoSetting != null)
                    {
                        break;
                    }

                    for (int videoResolutionId = 4; videoResolutionId >= 0; videoResolutionId--)
                    {
                        var videoResolutionLowWidthHeight = VideoResolutionWidthHeight.Get((VideoResolution)videoResolutionId);

                        var mediaFrameFormat = mediaFrameFormats.Where(m => m.Subtype == VideoSubtypeHelper.Get((VideoSubtype)videoSubType) &&
                                                                       m.VideoFormat.Width == videoResolutionLowWidthHeight.Width &&
                                                                       m.VideoFormat.Height == videoResolutionLowWidthHeight.Height)
                                               .OrderByDescending(m => m.FrameRate.Numerator / m.FrameRate.Denominator)
                                               .FirstOrDefault();

                        if (mediaFrameFormat != null)
                        {
                            videoSetting = new VideoSetting
                            {
                                VideoResolution = VideoResolution.SD640_480,
                                VideoSubtype    = (VideoSubtype)videoSubType,
                                VideoQuality    = 0.6,
                                UsedThreads     = 1
                            };

                            break;
                        }
                    }
                }
            }

            if (videoSetting != null)
            {
                VideoSetting = new JsonObject
                {
                    { "VideoResolution", JsonValue.CreateNumberValue((int)videoSetting.VideoResolution) },
                    { "VideoSubtype", JsonValue.CreateStringValue(VideoSubtypeHelper.Get(videoSetting.VideoSubtype)) },
                    { "VideoQuality", JsonValue.CreateNumberValue(videoSetting.VideoQuality) },
                    { "UsedThreads", JsonValue.CreateNumberValue(videoSetting.UsedThreads) }
                };

                return(videoSetting);
            }
            else
            {
                throw new Exception("Webcam not supported. Could not found correct video resolution and subtype. Please change code.");
            }
        }
Ejemplo n.º 4
0
        public async Task Initialize(VideoSetting videoSetting)
        {
            await DispatcherHelper.RunAndAwaitAsync(CoreApplication.MainView.CoreWindow.Dispatcher, CoreDispatcherPriority.Normal, async() =>
//            await CoreApplication.MainView.CoreWindow.Dispatcher.RunAsyncRunAndAwaitAsync(CoreDispatcherPriority.Normal, async () =>
            {
                _threadsCount   = videoSetting.UsedThreads;
                _stoppedThreads = videoSetting.UsedThreads;

                _lastFrameAdded.Start();

                _imageQuality         = new BitmapPropertySet();
                var imageQualityValue = new BitmapTypedValue(videoSetting.VideoQuality, Windows.Foundation.PropertyType.Single);
                _imageQuality.Add("ImageQuality", imageQualityValue);

                _mediaCapture = new MediaCapture();

                var frameSourceGroups = await MediaFrameSourceGroup.FindAllAsync();

                var settings = new MediaCaptureInitializationSettings()
                {
                    SharingMode = MediaCaptureSharingMode.ExclusiveControl,

                    //With CPU the results contain always SoftwareBitmaps, otherwise with GPU
                    //they preferring D3DSurface
                    MemoryPreference = MediaCaptureMemoryPreference.Cpu,

                    //Capture only video, no audio
                    StreamingCaptureMode = StreamingCaptureMode.Video
                };

                await _mediaCapture.InitializeAsync(settings);

                var mediaFrameSource      = _mediaCapture.FrameSources.First().Value;
                var videoDeviceController = mediaFrameSource.Controller.VideoDeviceController;

                videoDeviceController.DesiredOptimization = Windows.Media.Devices.MediaCaptureOptimization.Quality;
                videoDeviceController.PrimaryUse          = Windows.Media.Devices.CaptureUse.Video;

                //Set exposure (auto light adjustment)
                if (_mediaCapture.VideoDeviceController.Exposure.Capabilities.Supported &&
                    _mediaCapture.VideoDeviceController.Exposure.Capabilities.AutoModeSupported)
                {
                    _mediaCapture.VideoDeviceController.Exposure.TrySetAuto(true);
                }

                var videoResolutionWidthHeight = VideoResolutionWidthHeight.Get(videoSetting.VideoResolution);
                var videoSubType = VideoSubtypeHelper.Get(videoSetting.VideoSubtype);

                //Set resolution, frame rate and video subtyp
                var videoFormat = mediaFrameSource.SupportedFormats.Where(sf => sf.VideoFormat.Width == videoResolutionWidthHeight.Width &&
                                                                          sf.VideoFormat.Height == videoResolutionWidthHeight.Height &&
                                                                          sf.Subtype == videoSubType)
                                  .OrderByDescending(m => m.FrameRate.Numerator / m.FrameRate.Denominator)
                                  .First();

                await mediaFrameSource.SetFormatAsync(videoFormat);

                _mediaFrameReader = await _mediaCapture.CreateFrameReaderAsync(mediaFrameSource);
                await _mediaFrameReader.StartAsync();
            });
        }