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 Write(VideoSetting videoSetting)
        {
            var localFolder       = ApplicationData.Current.LocalFolder;
            var configurationFile = await localFolder.CreateFileAsync(CONFIGURATION_FILE_NAME, CreationCollisionOption.OpenIfExists);

            var configuration = 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) }
            };

            VideoSetting = configuration;

            await FileIO.WriteTextAsync(configurationFile, configuration.Stringify());
        }
Ejemplo n.º 3
0
        private async Task WriteResponse(HttpServerRequest request, StreamSocket socket)
        {
            //System.Diagnostics.Debug.WriteLine("WriteResponse");
            var relativeUrlLower = request.Url.ToLowerInvariant();
            var outputStream     = socket.OutputStream;

            //Get javascript files
            if (relativeUrlLower.StartsWith("/javascript"))
            {
                await HttpServerResponse.WriteResponseFile(ToFolderPath(request.Url), HttpContentType.JavaScript, outputStream);
            }
            //Get css style files
            else if (relativeUrlLower.StartsWith("/styles"))
            {
                await HttpServerResponse.WriteResponseFile(ToFolderPath(request.Url), HttpContentType.Css, outputStream);
            }
            //Get video setting
            else if (relativeUrlLower.StartsWith("/videosetting"))
            {
                HttpServerResponse.WriteResponseJson(ConfigurationFile.VideoSetting.Stringify(), outputStream);
            }
            //Get supported video settings
            else if (relativeUrlLower.StartsWith("/supportedvideosettings"))
            {
                HttpServerResponse.WriteResponseJson(ConfigurationFile.VideoSettingsSupported.Stringify(), outputStream);
            }
            //Set video settings
            else if (relativeUrlLower.StartsWith("/savevideosetting"))
            {
                await _camera.Stop();

                var videoSetting = new VideoSetting
                {
                    VideoSubtype    = VideoSubtypeHelper.Get(request.Body["VideoSubtype"].GetString()),
                    VideoResolution = (VideoResolution)request.Body["VideoResolution"].GetNumber(),
                    VideoQuality    = request.Body["VideoQuality"].GetNumber(),
                    UsedThreads     = (int)request.Body["UsedThreads"].GetNumber()
                };

                await ConfigurationFile.Write(videoSetting);

                await _camera.Initialize(videoSetting);

                _camera.Start();

                HttpServerResponse.WriteResponseOk(outputStream);
            }
            //Get current camera frame
            else if (relativeUrlLower.StartsWith("/videoframe"))
            {
                if (_camera.Frame != null)
                {
                    var webSocket = new WebSocket(socket, request, _camera);
                    await webSocket.Start();
                }
                else
                {
                    HttpServerResponse.WriteResponseError("Not camera fram available. Maybe there is an error or camera is not started.", outputStream);
                }
            }
            //Get index.html page
            else
            {
                await HttpServerResponse.WriteResponseFile(@"\Html\Index.html", HttpContentType.Html, outputStream);
            }
        }
        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.º 5
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();
            });
        }