示例#1
0
        VideoEncodingProperties GetVideoEncodingPropertiesForCameraParams(UnityEngine.Windows.WebCam.CameraParameters cameraParams)
        {
            var allPropertySets = _mediaCapture.VideoDeviceController.GetAvailableMediaStreamProperties(STREAM_TYPE).Select((x) => x as VideoEncodingProperties)
                                  .Where((x) =>
            {
                if (x == null)
                {
                    return(false);
                }
                if (x.FrameRate.Denominator == 0)
                {
                    return(false);
                }

                double calculatedFrameRate = (double)x.FrameRate.Numerator / (double)x.FrameRate.Denominator;

                return
                (x.Width == (uint)cameraParams.cameraResolutionWidth &&
                 x.Height == (uint)cameraParams.cameraResolutionHeight &&
                 (int)Math.Round(calculatedFrameRate) == cameraParams.frameRate);
            });     //Returns IEnumerable<VideoEncodingProperties>

            if (allPropertySets.Count() == 0)
            {
                throw new Exception("Could not find an encoding property set that matches the given camera parameters.");
            }

            var chosenPropertySet = allPropertySets.FirstOrDefault();

            return(chosenPropertySet);
        }
示例#2
0
        /// <summary>
        /// Asynchronously starts video mode.
        ///
        /// Activates the web camera with the various settings specified in CameraParameters.
        /// Only one VideoCapture instance can start the video mode at any given time.
        /// After starting the video mode, you listen for new video frame samples via the VideoCapture.FrameSampleAcquired event,
        /// or by calling VideoCapture.RequestNextFrameSample() when will return the next available sample.
        /// While in video mode, more power will be consumed so make sure that you call VideoCapture.StopVideoModeAsync qhen you can afford the start/stop video mode overhead.
        /// </summary>
        /// <param name="setupParams">Parameters that change how video mode is used.</param>
        /// <param name="onVideoModeStartedCallback">This callback will be invoked once video mode has been activated.</param>
        public async void StartVideoModeAsync(UnityEngine.Windows.WebCam.CameraParameters setupParams, OnVideoModeStartedCallback onVideoModeStartedCallback)
        {
            var mediaFrameSource = _mediaCapture.FrameSources[_frameSourceInfo.Id]; //Returns a MediaFrameSource

            if (mediaFrameSource == null)
            {
                onVideoModeStartedCallback?.Invoke(new VideoCaptureResult(1, ResultType.UnknownError, false));
                return;
            }

            var pixelFormat = ConvertCapturePixelFormatToMediaEncodingSubtype(setupParams.pixelFormat);

            _frameReader = await _mediaCapture.CreateFrameReaderAsync(mediaFrameSource, pixelFormat);

            _frameReader.FrameArrived += HandleFrameArrived;
            await _frameReader.StartAsync();

            VideoEncodingProperties properties = GetVideoEncodingPropertiesForCameraParams(setupParams);

            // Historical context: https://github.com/VulcanTechnologies/HoloLensCameraStream/issues/6
            if (setupParams.rotateImage180Degrees)
            {
                properties.Properties.Add(ROTATION_KEY, 180);
            }

            //	gr: taken from here https://forums.hololens.com/discussion/2009/mixedrealitycapture
            IVideoEffectDefinition ved = new VideoMRCSettings(setupParams.enableHolograms, setupParams.enableVideoStabilization, setupParams.videoStabilizationBufferSize, setupParams.hologramOpacity);
            await _mediaCapture.AddVideoEffectAsync(ved, MediaStreamType.VideoPreview);

            await _mediaCapture.VideoDeviceController.SetMediaStreamPropertiesAsync(STREAM_TYPE, properties);

            onVideoModeStartedCallback?.Invoke(new VideoCaptureResult(0, ResultType.Success, true));
        }
        // -------------------------------------------

        /*
         * OnPhotoCaptureCreated
         */
        private void OnPhotoCaptureCreated(UnityEngine.Windows.WebCam.PhotoCapture captureObject)
        {
            m_photoCaptureObject = captureObject;

            Resolution cameraResolution = UnityEngine.Windows.WebCam.PhotoCapture.SupportedResolutions.OrderByDescending((res) => res.width * res.height).First();

            UnityEngine.Windows.WebCam.CameraParameters m_cameraParameters = new UnityEngine.Windows.WebCam.CameraParameters();
            m_cameraParameters.hologramOpacity        = 0.0f;
            m_cameraParameters.cameraResolutionWidth  = cameraResolution.width;
            m_cameraParameters.cameraResolutionHeight = cameraResolution.height;
            m_cameraParameters.pixelFormat            = UnityEngine.Windows.WebCam.CapturePixelFormat.BGRA32;
        }