示例#1
0
        void Reset()
        {
            _videoInput = -1;
            _mode       = null;
            _device     = null;
            _flipX      = _flipY = false;

            _deviceSelection     = SelectDeviceBy.Default;
            _modeSelection       = SelectModeBy.Default;
            _videoInputSelection = SelectDeviceBy.Default;
            _desiredDeviceNames  = new List <string>(4);
            _desiredResolutions  = new List <Vector2>(2);
            _desiredVideoInputs  = new List <AVProLiveCameraPlugin.VideoInput>(4);
            _desiredDeviceNames.Add("Logitech BRIO");
            _desiredDeviceNames.Add("Logitech HD Pro Webcam C922");
            _desiredDeviceNames.Add("Logitech HD Pro Webcam C920");
            _desiredDeviceNames.Add("Decklink Video Capture");
            _desiredDeviceNames.Add("Logitech Webcam Pro 9000");
            _desiredResolutions.Add(new Vector2(1920, 1080));
            _desiredResolutions.Add(new Vector2(1280, 720));
            _desiredResolutions.Add(new Vector2(640, 360));
            _desiredResolutions.Add(new Vector2(640, 480));
            _desiredVideoInputs.Add(AVProLiveCameraPlugin.VideoInput.Video_Serial_Digital);
            _desiredVideoInputs.Add(AVProLiveCameraPlugin.VideoInput.Video_SVideo);
            _desiredVideoInputIndex = 0;
            _maintainAspectRatio    = false;
            _desiredFrameRate       = 0f;
            _desiredFormatAny       = true;
            _desiredFormat          = AVProLiveCameraPlugin.VideoFrameFormat.YUV_422_HDYC;
            _desiredModeIndex       = -1;
            _desiredDeviceIndex     = 0;
        }
        public bool Build(int width, int height, AVProLiveCameraPlugin.VideoFrameFormat format, bool allowTransparency, bool flipX, bool flipY, bool deinterlace = false)
        {
            Reset();

            _width       = width;
            _height      = height;
            _deinterlace = deinterlace;
            _format      = format;
            _flipX       = flipX;
            _flipY       = flipY;
            //_manager = AVProLiveCameraManager.Instance;

            if (CreateMaterials())
            {
                CreateBuffers();
                CreateRenderTexture();

                switch (_format)
                {
                case AVProLiveCameraPlugin.VideoFrameFormat.RAW_BGRA32:
                    _conversionMaterial.SetTexture("_MainTex", _buffers[0]._texture);
                    break;

                case AVProLiveCameraPlugin.VideoFrameFormat.RAW_MONO8:
                    _conversionMaterial.SetFloat("_TextureWidth", _finalTexture.width);
                    _conversionMaterial.SetTexture("_MainTex", _buffers[0]._texture);
                    break;

                case AVProLiveCameraPlugin.VideoFrameFormat.YUV_422_YUY2:
                case AVProLiveCameraPlugin.VideoFrameFormat.YUV_422_UYVY:
                case AVProLiveCameraPlugin.VideoFrameFormat.YUV_422_YVYU:
                case AVProLiveCameraPlugin.VideoFrameFormat.YUV_422_HDYC:
                    _conversionMaterial.SetFloat("_TextureWidth", _finalTexture.width);
                    _conversionMaterial.SetTexture("_MainTex", _buffers[0]._texture);
                    break;

                case AVProLiveCameraPlugin.VideoFrameFormat.YUV_420_PLANAR_YV12:
                case AVProLiveCameraPlugin.VideoFrameFormat.YUV_420_PLANAR_I420:
                    _conversionMaterial.SetFloat("_TextureWidth", _finalTexture.width);
                    _conversionMaterial.SetTexture("_MainTex", _buffers[0]._texture);
                    _conversionMaterial.SetTexture("_MainU", _buffers[1]._texture);
                    _conversionMaterial.SetTexture("_MainV", _buffers[2]._texture);
                    break;
                }

                SetFlip(_flipX, _flipY);
                SetTransparency(allowTransparency);
            }
            else
            {
                Debug.LogWarning("[AVPro LiveCamera] couldn't create conversion materials");
                return(false);
            }

            return(true);
        }
    public bool Build(int width, int height, AVProLiveCameraPlugin.VideoFrameFormat format, bool flipX, bool flipY, bool deinterlace = false)
    {
        Reset();

        _width = width;
        _height = height;
        _deinterlace = deinterlace;
        _format = format;
        _flipX = flipX;
        _flipY = flipY;
        //_manager = AVProLiveCameraManager.Instance;

        if (CreateMaterials())
        {
            CreateBuffers();
            CreateRenderTexture();

            switch (_format)
            {
            case AVProLiveCameraPlugin.VideoFrameFormat.RAW_BGRA32:
                _conversionMaterial.SetTexture("_MainTex", _buffers[0]._texture);
                break;
            case AVProLiveCameraPlugin.VideoFrameFormat.RAW_MONO8:
                _conversionMaterial.SetFloat("_TextureWidth", _finalTexture.width);
                _conversionMaterial.SetTexture("_MainTex", _buffers[0]._texture);
                break;
            case AVProLiveCameraPlugin.VideoFrameFormat.YUV_422_YUY2:
            case AVProLiveCameraPlugin.VideoFrameFormat.YUV_422_UYVY:
            case AVProLiveCameraPlugin.VideoFrameFormat.YUV_422_YVYU:
            case AVProLiveCameraPlugin.VideoFrameFormat.YUV_422_HDYC:
                _conversionMaterial.SetFloat("_TextureWidth", _finalTexture.width);
                _conversionMaterial.SetTexture("_MainTex", _buffers[0]._texture);
                break;
            case AVProLiveCameraPlugin.VideoFrameFormat.YUV_420_PLANAR_YV12:
            case AVProLiveCameraPlugin.VideoFrameFormat.YUV_420_PLANAR_I420:
                _conversionMaterial.SetFloat("_TextureWidth", _finalTexture.width);
                _conversionMaterial.SetTexture("_MainTex", _buffers[0]._texture);
                _conversionMaterial.SetTexture("_MainU", _buffers[1]._texture);
                _conversionMaterial.SetTexture("_MainV", _buffers[2]._texture);
                break;
            }

            SetFlip(_flipX, _flipY);
        }
        else
        {
            Debug.LogWarning("[AVPro LiveCamera] couldn't create conversion materials");
            return false;
        }

        return true;
    }
示例#4
0
    public bool Start(int modeIndex = -1, int videoInputIndex = -1)
    {
        if (AVProLiveCameraPlugin.StartDevice(_deviceIndex, modeIndex, videoInputIndex))
        {
            Debug.Log("[AVProLiveCamera] Started device using mode index " + modeIndex);

            // Get format mode properties
            int width  = AVProLiveCameraPlugin.GetWidth(_deviceIndex);
            int height = AVProLiveCameraPlugin.GetHeight(_deviceIndex);
            AVProLiveCameraPlugin.VideoFrameFormat format = (AVProLiveCameraPlugin.VideoFrameFormat)AVProLiveCameraPlugin.GetFormat(_deviceIndex);
            _width            = width;
            _height           = height;
            _format           = format.ToString();
            _deviceFormat     = AVProLiveCameraPlugin.GetDeviceFormat(_deviceIndex);
            _frameRate        = AVProLiveCameraPlugin.GetFrameRate(_deviceIndex);
            _frameDurationHNS = AVProLiveCameraPlugin.GetFrameDurationHNS(_deviceIndex);

            // Validate properties
            if (width <= 0 || width > MaxVideoResolution || height <= 0 || height > MaxVideoResolution)
            {
                Debug.LogWarning("[AVProLiveCamera] invalid width or height");
                Close();
                return(false);
            }

            // Create format converter
            _isTopDown = AVProLiveCameraPlugin.IsFrameTopDown(_deviceIndex);
            if (!_formatConverter.Build(width, height, format, _flipX, _isTopDown != _flipY, Deinterlace))
            {
                Debug.LogWarning("[AVProLiveCamera] unable to convert camera format");
                Close();
                return(false);
            }

            // Run camera
            IsActive             = true;
            IsRunning            = false;
            IsPicture            = false;
            IsPaused             = true;
            _lastModeIndex       = modeIndex;
            _lastVideoInputIndex = videoInputIndex;
            Play();

            return(IsRunning);
        }

        Debug.LogWarning("[AVProLiveCamera] unable to start camera");
        Close();
        return(false);
    }
示例#5
0
        public Shader GetPixelConversionShader(AVProLiveCameraPlugin.VideoFrameFormat format)
        {
            Shader result = null;

            switch (format)
            {
            case AVProLiveCameraPlugin.VideoFrameFormat.YUV_422_YUY2:
                result = _shaderYUY2;
                break;

            case AVProLiveCameraPlugin.VideoFrameFormat.YUV_422_UYVY:
                result = _shaderUYVY;
                break;

            case AVProLiveCameraPlugin.VideoFrameFormat.YUV_422_YVYU:
                result = _shaderYVYU;
                break;

            case AVProLiveCameraPlugin.VideoFrameFormat.YUV_422_HDYC:
                result = _shaderHDYC;
                break;

            case AVProLiveCameraPlugin.VideoFrameFormat.RAW_BGRA32:
                result = _shaderBGRA32;
                break;

            case AVProLiveCameraPlugin.VideoFrameFormat.RAW_MONO8:
                result = _shaderMONO8;
                break;

            case AVProLiveCameraPlugin.VideoFrameFormat.YUV_420_PLANAR_I420:
                result = _shaderI420;
                break;

            case AVProLiveCameraPlugin.VideoFrameFormat.YUV_420_PLANAR_YV12:
                result = _shaderYV12;
                break;

            default:
                Debug.LogError("[AVProLiveCamera] Unknown video format '" + format);
                break;
            }
            return(result);
        }
示例#6
0
        private static AVProLiveCameraDeviceMode GetClosestMode(AVProLiveCameraDevice device, List <Vector2> resolutions, bool maintainApectRatio, float frameRate, bool anyPixelFormat, AVProLiveCameraPlugin.VideoFrameFormat pixelFormat)
        {
            AVProLiveCameraDeviceMode result = null;

            for (int i = 0; i < resolutions.Count; i++)
            {
                result = device.GetClosestMode(Mathf.FloorToInt(resolutions[i].x), Mathf.FloorToInt(resolutions[i].y), maintainApectRatio, frameRate, anyPixelFormat, pixelFormat);
                if (result != null)
                {
                    break;
                }
            }
            return(result);
        }
示例#7
0
        public AVProLiveCameraDeviceMode GetClosestMode(int width, int height, bool maintainAspectRatio, float frameRate, bool anyPixelFormat, AVProLiveCameraPlugin.VideoFrameFormat pixelFormat)
        {
            AVProLiveCameraDeviceMode result = null;

            if (width <= 0 || height <= 0)
            {
                return(result);
            }

            List <AVProLiveCameraDeviceMode> bestModes = new List <AVProLiveCameraDeviceMode>();

            // Try to find exact match to resolution
            {
                for (int i = 0; i < NumModes; i++)
                {
                    AVProLiveCameraDeviceMode mode = GetMode(i);

                    if (mode.Width == width && mode.Height == height)
                    {
                        bestModes.Add(mode);
                    }
                }
            }

            // If we haven't found an exact match, find by closest area
            if (bestModes.Count == 0)
            {
                float aspect = (float)width * (float)height;
                int   area   = width * height;
                float lowestAreaDifference = float.MaxValue;
                for (int i = 0; i < NumModes; i++)
                {
                    AVProLiveCameraDeviceMode mode = GetMode(i);

                    // Maintain aspect ratio or not
                    float modeAspect = (float)mode.Width / (float)mode.Height;
                    bool  consider   = true;
                    if (maintainAspectRatio && !Mathf.Approximately(modeAspect, aspect))
                    {
                        consider = false;
                    }

                    if (consider)
                    {
                        int modeArea       = mode.Width * mode.Height;
                        int areaDifference = Mathf.Abs(area - modeArea);
                        if (areaDifference < lowestAreaDifference)
                        {
                            result = mode;
                            lowestAreaDifference = areaDifference;
                        }
                    }
                }

                // Now that we know the closest resolution, collect all modes with that resolution
                if (result != null)
                {
                    for (int i = 0; i < NumModes; i++)
                    {
                        AVProLiveCameraDeviceMode mode = GetMode(i);
                        if (mode.Width == result.Width && mode.Height == result.Height)
                        {
                            bestModes.Add(mode);
                        }
                    }
                    result = null;
                }
            }


            // Pick best based on pixel format or frame rate
            if (bestModes.Count > 0)
            {
                if (bestModes.Count == 1)
                {
                    result = bestModes[0];
                }
                else
                {
                    bool findHighestFrameRate = (frameRate <= 0f);
                    if (findHighestFrameRate)
                    {
                        float highestFps = 0f;
                        for (int i = 0; i < bestModes.Count; i++)
                        {
                            AVProLiveCameraDeviceMode mode = bestModes[i];
                            if (mode.FPS > highestFps)
                            {
                                highestFps = mode.FPS;
                            }
                        }
                        // Remove modes that didn't have the higest fps
                        for (int i = 0; i < bestModes.Count; i++)
                        {
                            AVProLiveCameraDeviceMode mode = bestModes[i];
                            if (mode.FPS < highestFps)
                            {
                                bestModes.RemoveAt(i);
                                i = -1;
                            }
                        }
                    }
                    else
                    {
                        float lowestDelta = 1000f;
                        float closestFps  = 0f;
                        // Find the closest FPS
                        for (int i = 0; i < bestModes.Count; i++)
                        {
                            AVProLiveCameraDeviceMode mode = bestModes[i];
                            float d = Mathf.Abs(mode.FPS - frameRate);
                            if (d < lowestDelta)
                            {
                                closestFps  = mode.FPS;
                                lowestDelta = d;
                            }
                        }
                        // Remove modes that have a different frameRate
                        for (int i = 0; i < bestModes.Count; i++)
                        {
                            AVProLiveCameraDeviceMode mode = bestModes[i];
                            if (mode.FPS != closestFps)
                            {
                                bestModes.RemoveAt(i);
                                i = -1;
                            }
                        }
                    }

                    if (result == null)
                    {
                        if (bestModes.Count == 0)
                        {
                            result = null;
                        }
                        else if (bestModes.Count == 1)
                        {
                            result = bestModes[0];
                        }
                        else
                        {
                            if (anyPixelFormat)
                            {
                                string[] bestFormats = { "YUV_UYVY_HDYC", "YUV_UYVY", "YUV_YVYU", "YUV_YUY2", "ARGB32", "RGB32", "RGB24", "MJPG", "UNKNOWN" };
                                int      bestScore   = 100;
                                for (int i = 0; i < bestModes.Count; i++)
                                {
                                    int index = System.Array.IndexOf <string>(bestFormats, bestModes[i].Format);
                                    if (index >= 0 && index < bestScore)
                                    {
                                        result    = bestModes[i];
                                        bestScore = index;
                                    }
                                }
                            }
                            else
                            {
                                for (int i = 0; i < bestModes.Count; i++)
                                {
                                    if (CompareMode(bestModes[i].Format, pixelFormat))
                                    {
                                        result = bestModes[i];
                                        break;
                                    }
                                }
                            }
                        }
                    }
                }
            }

            if (result != null)
            {
                //Debug.Log(string.Format("Selected mode {0}: {1}x{2} {3} {4}", result.Index, result.Width , result.Height, result.FPS, result.Format));
            }

            return(result);
        }
示例#8
0
        private static bool CompareMode(string internalMode, AVProLiveCameraPlugin.VideoFrameFormat pluginMode)
        {
            if (internalMode == pluginMode.ToString())
            {
                return(true);
            }

            bool result = false;

            switch (pluginMode)
            {
            case AVProLiveCameraPlugin.VideoFrameFormat.RAW_BGRA32:
                result = (internalMode == "BGRA32" || internalMode == "ARGB32" || internalMode == "RGB32");
                break;

            case AVProLiveCameraPlugin.VideoFrameFormat.YUV_422_YUY2:
                result = (internalMode == "YUV_YUY2");
                break;

            case AVProLiveCameraPlugin.VideoFrameFormat.YUV_422_UYVY:
                result = (internalMode == "YUV_UYVY");
                break;

            case AVProLiveCameraPlugin.VideoFrameFormat.YUV_422_YVYU:
                result = (internalMode == "YUV_YVYU");
                break;

            case AVProLiveCameraPlugin.VideoFrameFormat.YUV_422_HDYC:
                result = (internalMode == "YUV_UYVY_HDYC");
                break;

            case AVProLiveCameraPlugin.VideoFrameFormat.YUV_420_PLANAR_YV12:
                result = (internalMode == "YUV_PLANAR_YV12");
                break;

            case AVProLiveCameraPlugin.VideoFrameFormat.YUV_420_PLANAR_I420:
                result = (internalMode == "YUV_PLANAR_I420");
                break;

            case AVProLiveCameraPlugin.VideoFrameFormat.RAW_RGB24:
                result = (internalMode == "RGB24");
                break;

            case AVProLiveCameraPlugin.VideoFrameFormat.RAW_MONO8:
                result = (internalMode == "MONO8" || internalMode == "Mono_Y800");
                break;

            case AVProLiveCameraPlugin.VideoFrameFormat.RGB_10BPP:
                result = (internalMode == "RGB_10BPP" || internalMode == "RGBX_10BPP" || internalMode == "RGBXLE_10BPP");
                break;

            case AVProLiveCameraPlugin.VideoFrameFormat.YUV_10BPP_V210:
                result = (internalMode == "YUV_10BPP_V210");
                break;

            case AVProLiveCameraPlugin.VideoFrameFormat.MPEG:
                result = (internalMode == "MJPG");
                break;
            }

            return(result);
        }
        public bool Start(AVProLiveCameraDeviceMode mode, int videoInputIndex = -1)
        {
            // Resolve the internal mode index
            int internalModeIndex = -1;

            if (mode != null)
            {
                internalModeIndex = mode.InternalIndex;
            }

            // Start the device
            if (AVProLiveCameraPlugin.StartDevice(_deviceIndex, internalModeIndex, videoInputIndex))
            {
                int modeIndex = -1;
                if (mode != null)
                {
                    for (int i = 0; i < _modes.Count; i++)
                    {
                        if (_modes[i] == mode)
                        {
                            modeIndex = i;
                            break;
                        }
                    }
                }
                Debug.Log("[AVProLiveCamera] Started device using mode index " + modeIndex + " (internal index " + internalModeIndex + ")");

                // Get format mode properties
                int width  = AVProLiveCameraPlugin.GetWidth(_deviceIndex);
                int height = AVProLiveCameraPlugin.GetHeight(_deviceIndex);
                AVProLiveCameraPlugin.VideoFrameFormat format = (AVProLiveCameraPlugin.VideoFrameFormat)AVProLiveCameraPlugin.GetFormat(_deviceIndex);
                _width            = width;
                _height           = height;
                _format           = format.ToString();
                _deviceFormat     = AVProLiveCameraPlugin.GetDeviceFormat(_deviceIndex);
                _frameRate        = AVProLiveCameraPlugin.GetFrameRate(_deviceIndex);
                _frameDurationHNS = AVProLiveCameraPlugin.GetFrameDurationHNS(_deviceIndex);

                // Validate properties
                if (width <= 0 || width > MaxVideoResolution || height <= 0 || height > MaxVideoResolution)
                {
                    Debug.LogWarning("[AVProLiveCamera] invalid width or height");
                    Close();
                    return(false);
                }

                // Create format converter
                _isTopDown = AVProLiveCameraPlugin.IsFrameTopDown(_deviceIndex);
                bool allowTransparency = (SupportsTransparency && AllowTransparency);
                if (!_formatConverter.Build(width, height, format, allowTransparency, _flipX, _isTopDown != _flipY, Deinterlace))
                {
                    Debug.LogWarning("[AVProLiveCamera] unable to convert camera format");
                    Close();
                    return(false);
                }

                // Run camera
                IsActive             = true;
                IsRunning            = false;
                IsPicture            = false;
                IsPaused             = true;
                _lastModeIndex       = modeIndex;
                _lastVideoInputIndex = videoInputIndex;
                Play();

                return(IsRunning);
            }

            Debug.LogWarning("[AVProLiveCamera] unable to start camera");
            Close();
            return(false);
        }