Beispiel #1
0
    void OnEnable()
    {
        if (!SystemInfo.graphicsDeviceVersion.StartsWith("OpenGL"))
        {
            enabled = false;
            return;
        }

        sdiEnabled  = false;
        IOCoroutine = SdiIOCoroutine();

        timeCodeData = new int[8] {
            0, 0, 0, 0, 0, 0, 0, 0
        };
        timeCodeHandle = GCHandle.Alloc(timeCodeData, GCHandleType.Pinned);

#if !UNITY_EDITOR
        if (!GLNvSdiOptions.Load(UtyGLNvSdi.ConfigFileName, ref options))
        {
            GLNvSdiOptions.Save(UtyGLNvSdi.ConfigFileName, options);
        }
#endif

        if (options.logToFile)
        {
            UtyGLNvSdi.SdiSetupLogFile();
        }

        StartCoroutine(IOCoroutine);
    }
Beispiel #2
0
    void CreateSdiInputTextures(int count, int texWidth, int texHeight)
    {
        // Remember: if input videoFormat == SdiVideoFormat.SD_487I_59_94, that is, texHeight == 243
        // the upper field has one line more than the lower field.
        // UpperField = 244; LowerField = 243

        for (int i = 0; i < count; ++i)
        {
            if (options.inputCaptureFields && texHeight == 243 && i % 2 == 0)
            {
                sdiTexture[i] = new RenderTexture(texWidth, texHeight + 1, 32, RenderTextureFormat.ARGBFloat);
            }
            else
            {
                sdiTexture[i] = new RenderTexture(texWidth, texHeight, 32, RenderTextureFormat.ARGBFloat);
            }
            sdiTexture[i].name         = name;
            sdiTexture[i].isPowerOfTwo = false;
            sdiTexture[i].useMipMap    = false;
            sdiTexture[i].filterMode   = FilterMode.Trilinear;
            sdiTexture[i].wrapMode     = TextureWrapMode.Clamp;
            sdiTexture[i].Create();

            UtyGLNvSdi.SdiInputSetTexturePtr(i, sdiTexture[i].GetNativeTexturePtr(), sdiTexture[i].width, sdiTexture[i].height);

            // attach to texture only the existent video inputs
            int multiplier = options.inputCaptureFields ? 2 : 1;
            if (i < UtyGLNvSdi.SdiInputVideoCount() * multiplier)
            {
                sdiMaterials[i].mainTexture = sdiTexture[i];
            }
        }
    }
Beispiel #3
0
    private IEnumerator SdiInputCoroutine()
    {
        yield return(new WaitForEndOfFrame());

        UtyGLNvSdi.SdiInputSetBufferSize(options.inputRingBufferSize);
        UtyGLNvSdi.SdiInputSetCaptureFields(false);

        // Issue a plugin event with an integer identifier.
        // The plugin can distinguish between different based on this ID.
        GL.IssuePluginEvent(UtyGLNvSdi.GetSdiInputRenderEventFunc(), (int)SdiRenderEvent.Initialize);
        yield return(new WaitForEndOfFrame());

        SetupTextures();

        GL.IssuePluginEvent(UtyGLNvSdi.GetSdiInputRenderEventFunc(), (int)SdiRenderEvent.Setup);
        GL.IssuePluginEvent(UtyGLNvSdi.GetSdiInputRenderEventFunc(), (int)SdiRenderEvent.StartCapture);
        yield return(new WaitForEndOfFrame());

        sdiEnabled = true;

        while (UtyGLNvSdi.SdiInputIsCapturing())
        {
            // Wait until all frame rendering is done
            yield return(new WaitForEndOfFrame());

            // Get status of the capture (GL_SUCCESS_NV, GL_FAILURE_NV, GL_PARTIAL_SUCCESS_NV)
            //Debug.Log(UtyGLNvSdi.SdiInputCaptureStatus().ToString());

            // Capture frame from device
            GL.IssuePluginEvent(UtyGLNvSdi.GetSdiInputRenderEventFunc(), (int)SdiRenderEvent.CaptureFrame);
        }
    }
Beispiel #4
0
    void OnEnable()
    {
        if (!SystemInfo.graphicsDeviceVersion.StartsWith("OpenGL"))
        {
            enabled = false;
            return;
        }

        sdiEnabled     = false;
        InputCoroutine = SdiInputCoroutine();


#if !UNITY_EDITOR
        if (!GLNvSdiOptions.Load(UtyGLNvSdi.ConfigFileName, ref options))
        {
            GLNvSdiOptions.Save(UtyGLNvSdi.ConfigFileName, options);
        }
#endif

        if (options.logToFile)
        {
            UtyGLNvSdi.SdiSetupLogFile();
        }

        options.inputCaptureFields = false;

        StartCoroutine(InputCoroutine);
    }
Beispiel #5
0
    void OnDisable()
    {
        if (sdiEnabled)
        {
            StopCoroutine(InputCoroutine);

            GL.IssuePluginEvent(UtyGLNvSdi.GetSdiInputRenderEventFunc(), (int)SdiRenderEvent.Shutdown);
            sdiEnabled = false;
        }
    }
Beispiel #6
0
 public void UpdateFrame()
 {
     if (sdiEnabled)
     {
         // Capture frame from device
         GL.IssuePluginEvent(UtyGLNvSdi.GetSdiInputRenderEventFunc(), (int)SdiRenderEvent.CaptureFrame);
         // Present frame
         GL.IssuePluginEvent(UtyGLNvSdi.GetSdiOutputRenderEventFunc(), (int)SdiRenderEvent.PresentFrame);
     }
 }
Beispiel #7
0
    public static System.Text.StringBuilder SdiGetLog()
    {
        if (Log == null)
        {
            Log = new System.Text.StringBuilder(LOG_MAX_SIZE);
        }

        UtyGLNvSdi.SdiGetLog(UtyGLNvSdi.Log, UtyGLNvSdi.LOG_MAX_SIZE);

        return(UtyGLNvSdi.Log);
    }
Beispiel #8
0
 public void OnReloadUI()
 {
     videoFormat.text      = options.videoFormat.ToString();
     syncSource.text       = options.syncSource.ToString();
     ringBufferIn.text     = options.inputRingBufferSize.ToString();
     ringBufferOut.text    = options.outputRingBufferSize.ToString();
     hSync.text            = options.outputHorizontalDelay.ToString();
     vSync.text            = options.outputVerticalDelay.ToString();
     droppedFramesIn.text  = UtyGLNvSdi.SdiInputDroppedFramesCount().ToString();
     droppedFramesOut.text = UtyGLNvSdi.SdiOutputDuplicatedFramesCount().ToString();
     gviTime.text          = UtyGLNvSdi.SdiInputGviTime().ToString("0.0000");
     delay.text            = options.outputDelay.ToString("0.0");
 }
Beispiel #9
0
    void OnDisable()
    {
        if (sdiEnabled)
        {
            StopCoroutine(IOCoroutine);

            GL.IssuePluginEvent(UtyGLNvSdi.GetSdiOutputRenderEventFunc(), (int)SdiRenderEvent.Shutdown);
            GL.IssuePluginEvent(UtyGLNvSdi.GetSdiInputRenderEventFunc(), (int)SdiRenderEvent.Shutdown);
            sdiEnabled = false;
        }

        DestroyTextures();
        timeCodeHandle.Free();
    }
Beispiel #10
0
    void Update()
    {
        int drop_in  = UtyGLNvSdi.SdiInputDroppedFramesCount();
        int drop_out = UtyGLNvSdi.SdiOutputDuplicatedFramesCount();

        if (dropFramesIn != drop_in || dropFramesOut != drop_out)
        {
            dropFramesIn         = drop_in;
            droppedFramesIn.text = dropFramesIn.ToString();

            dropFramesOut         = drop_out;
            droppedFramesOut.text = dropFramesOut.ToString();

            gviTime.text = UtyGLNvSdi.SdiInputGviTime().ToString("0.0000");
        }
    }
Beispiel #11
0
    void CreateTextures(int count, int texWidth, int texHeight)
    {
        for (int i = 0; i < count; ++i)
        {
            sdiTexture[i]              = new RenderTexture(texWidth, texHeight, 32, RenderTextureFormat.ARGB32);
            sdiTexture[i].name         = name;
            sdiTexture[i].isPowerOfTwo = false;
            sdiTexture[i].wrapMode     = TextureWrapMode.Clamp;
            sdiTexture[i].Create();

            UtyGLNvSdi.SdiInputSetTexturePtr(i, sdiTexture[i].GetNativeTexturePtr(), sdiTexture[i].width, sdiTexture[i].height);

            int multiplier = options.inputCaptureFields ? 2 : 1;
            if (i < UtyGLNvSdi.SdiInputVideoCount() * multiplier)
            {
                sdiMaterials[i].mainTexture = sdiTexture[i];
            }
        }
    }
Beispiel #12
0
    void SetupTextures()
    {
        for (int i = 0; i < sdiInputTexture.Length; ++i)
        {
            if (sdiInputTexture[i] == null)
            {
                continue;
            }

            if (!sdiInputTexture[i].IsCreated())
            {
                if (!sdiInputTexture[i].Create())
                {
                    Debug.LogError("Could not create sdi input texture");
                }
            }

            UtyGLNvSdi.SdiInputSetTexturePtr(i, sdiInputTexture[i].GetNativeTexturePtr(), sdiInputTexture[i].width, sdiInputTexture[i].height);
        }
    }
Beispiel #13
0
    void Start()
    {
        if (utySdi == null)
        {
            utySdi = FindObjectOfType <UtyGLNvSdi>();
        }

        if (utySdi == null)
        {
            Debug.LogError("Could not find sdi object. Disabling sdi UI");
            enabled = false;
            return;
        }

        options = utySdi.options;

        OnReloadUI();

        Invoke("OnResetDropFramesIn", 3.0f);
        Invoke("OnResetDropFramesOut", 3.0f);
        Invoke("OnReloadUI", 3.5f);
    }
Beispiel #14
0
    private IEnumerator SdiOutputCoroutine()
    {
        UtyGLNvSdi.SdiOutputSetGlobalOptions();
        UtyGLNvSdi.SdiOutputSetVideoFormat(
            options.videoFormat,
            options.syncSource,
            options.outputDelay,
            options.outputHorizontalDelay,
            options.outputVerticalDelay,
            options.outputDual,
            options.outputRingBufferSize);

        yield return(new WaitForEndOfFrame());

        GL.IssuePluginEvent(UtyGLNvSdi.GetSdiOutputRenderEventFunc(), (int)SdiRenderEvent.Initialize);
        yield return(new WaitForEndOfFrame());


        if (UtyGLNvSdi.SdiError() == 0)
        {
            GL.IssuePluginEvent(UtyGLNvSdi.GetSdiOutputRenderEventFunc(), (int)SdiRenderEvent.Setup);

            if (!SetupOutputTextures())
            {
                UnityEngine.Debug.LogError("GLNvSdi_Plugin could not setup sdi textures for output");
            }

            sdiEnabled = true;

            while (true)
            {
                yield return(new WaitForEndOfFrame());

                GL.IssuePluginEvent(UtyGLNvSdi.GetSdiOutputRenderEventFunc(), (int)SdiRenderEvent.PresentFrame);
            }
        }
    }
Beispiel #15
0
 public void OnDelayEdit()
 {
     options.outputDelay = float.Parse(this.delay.text);
     UtyGLNvSdi.SdiOutputSetDelay(options.outputDelay);
 }
Beispiel #16
0
 public void OnResetDropFramesOut()
 {
     UtyGLNvSdi.SdiOutputResetDuplicatedFramesCount();
 }
Beispiel #17
0
 public void OnResetDropFramesIn()
 {
     UtyGLNvSdi.SdiInputResetDroppedFramesCount();
 }
Beispiel #18
0
    bool SetupOutputTextures(int texWidth, int texHeight, float aspect, bool interlaced, bool outputDual)
    {
        // Check the count of cameras. The plugin support until two cameras
        if (m_Camera.Length > 4)
        {
            UnityEngine.Debug.LogWarning("GLNvSdi plugin does not support more than four cameras.");
        }

        // Disable all cameras
        for (int i = 0; i < m_Camera.Length; ++i)
        {
            m_Camera[i].enabled = false;
        }

        // Verify the amount of render textures needed
        int lTexCount = 0;

        if (options.outputDual)
        {
            lTexCount = 2;
        }
        else
        {
            lTexCount = 1;
        }

        if (interlaced)
        {
            lTexCount *= 2;
        }

        // If dual progressive output, change the order of cameras. So, the first two are active
        if (options.outputDual && !interlaced)     // dual output progressive
        {
            Camera tmp = m_Camera[1];
            m_Camera[1] = m_Camera[2];
            m_Camera[2] = tmp;

            GLNvSdiRenderTexture rt1 = m_Camera[1].GetComponent <GLNvSdiRenderTexture>();
            GLNvSdiRenderTexture rt2 = m_Camera[2].GetComponent <GLNvSdiRenderTexture>();

            if (rt1 != null && rt2 != null)
            {
                int tmp_index = rt1.sdiTextureIndex;
                rt1.sdiTextureIndex = rt2.sdiTextureIndex;
                rt2.sdiTextureIndex = tmp_index;

                rt1.material = sdiCompositeMaterial[0];
                rt2.material = sdiCompositeMaterial[1];
            }
            else
            {
                UnityEngine.Debug.LogError("Error: Could not find GLNvSdiRenderTexture when switching camera parameters");
                return(false);
            }
        }


        // Setting the cameras and directing the render to texture
        for (int i = 0; i < lTexCount; ++i)
        {
            if (m_Camera[i] != null)
            {
                m_Camera[i].aspect  = aspect;
                m_Camera[i].enabled = true;


                GLNvSdiRenderTexture sdiTex = m_Camera[i].GetComponent <GLNvSdiRenderTexture>();
                if (sdiTex != null)
                {
                    sdiTex.CreateSdiTexture(texWidth, texHeight, 32, false, "SdiRenderTex_" + m_Camera[i].name);

                    if (sdiTex.sdiRenderTarget == null)
                    {
                        UnityEngine.Debug.LogError("Could not create SdiRenderTarget for GLNvSdiRenderTexture in " + m_Camera[i].name);
                        return(false);
                    }

                    if (options.useInputVideoAsBackground)
                    {
                        if (options.inputCaptureFields)
                        {
                            sdiTex.backgroundTex = sdiTexture[i];
                        }
                        else
                        {
                            sdiTex.backgroundTex = sdiTexture[i / 2];
                        }
                    }

                    sdiTex.material = sdiCompositeMaterial[i];

#if true   // render with video
                    UtyGLNvSdi.SdiOutputSetTexturePtr(i, sdiTex.sdiRenderTarget.GetNativeTexturePtr(), sdiTex.sdiRenderTarget.width, sdiTex.sdiRenderTarget.height);
#else      // input video pasthrough
                    UtyGLNvSdi.SdiOutputSetTexturePtr(i, sdiTexture[i].GetNativeTexturePtr(), sdiTexture[i].width, sdiTexture[i].height);
#endif
                }
            }
            else
            {
                UnityEngine.Debug.LogError(m_Camera[i].name + " is null");
                return(false);
            }
        }

        return(true);
    }
Beispiel #19
0
    bool SetupOutputTextures()
    {
        float lAspect       = 0;
        bool  lIsInterlaced = false;

        UtyGLNvSdi.GetSizeFromVideoFormat(options.videoFormat, ref m_TexWidth, ref m_TexHeight, ref lAspect, ref lIsInterlaced);

        // Check the count of cameras. The plugin support until two cameras
        if (m_Camera.Length > 4)
        {
            UnityEngine.Debug.LogWarning("GLNvSdi plugin does not support more than four cameras.");
        }

        // Disable all cameras
        for (int i = 0; i < m_Camera.Length; ++i)
        {
            m_Camera[i].enabled = false;
        }

        // Verify the amount of render textures needed
        int lTexCount = 0;

        if (options.outputDual)
        {
            lTexCount = 2;
        }
        else
        {
            lTexCount = 1;
        }

        if (lIsInterlaced)
        {
            lTexCount *= 2;
        }

        // If dual progressive output, change the order of cameras. So, the first two are active
        if (options.outputDual && !lIsInterlaced)     // dual output progressive
        {
            Camera tmp = m_Camera[1];
            m_Camera[1] = m_Camera[2];
            m_Camera[2] = tmp;

            GLNvSdiRenderTexture rt1 = m_Camera[1].GetComponent <GLNvSdiRenderTexture>();
            GLNvSdiRenderTexture rt2 = m_Camera[2].GetComponent <GLNvSdiRenderTexture>();

            if (rt1 != null && rt2 != null)
            {
                int tmp_index = rt1.sdiTextureIndex;
                rt1.sdiTextureIndex = rt2.sdiTextureIndex;
                rt2.sdiTextureIndex = tmp_index;
            }
            else
            {
                UnityEngine.Debug.LogError("Error: Could not find GLNvSdiRenderTexture when switching camera parameters");
                return(false);
            }
        }


        // Setting the cameras and directing the render to texture
        for (int i = 0; i < lTexCount; ++i)
        {
            if (m_Camera[i] != null)
            {
                m_Camera[i].aspect  = lAspect;
                m_Camera[i].enabled = true;


                GLNvSdiRenderTexture sdiTex = m_Camera[i].GetComponent <GLNvSdiRenderTexture>();
                if (sdiTex != null)
                {
                    sdiTex.CreateSdiTexture(m_TexWidth, m_TexHeight, 32, false, "RenderTex_" + m_Camera[i].name);

                    if (sdiTex.sdiRenderTarget == null)
                    {
                        UnityEngine.Debug.LogError("Could not create SdiRenderTarget for GLNvSdiRenderTexture in " + m_Camera[i].name);
                        return(false);
                    }

                    UtyGLNvSdi.SdiOutputSetTexturePtr(i, sdiTex.sdiRenderTarget.GetNativeTexturePtr(), sdiTex.sdiRenderTarget.width, sdiTex.sdiRenderTarget.height);
                    //UtyGLNvSdi.SdiOutputSetTexturePtr(i, outputTexture[i].GetNativeTexturePtr(), outputTexture[i].width, outputTexture[i].height);
                }
                else
                {
                    UnityEngine.Debug.LogError("Could not find GLNvSdiRenderTexture in " + m_Camera[i].name);
                    return(false);
                }
            }
            else
            {
                UnityEngine.Debug.LogError(m_Camera[i].name + " is null");
                return(false);
            }
        }

        // If the options to invert fields is marked, we have to invert
        // the textures id already sent to plugin
        if (options.outputInvertFields)
        {
            UtyGLNvSdi.SdiOutputInvertFields(options.outputInvertFields);
        }

        return(true);
    }
Beispiel #20
0
    bool SetupOutputTextures()
    {
        int   texWidth      = 1920; // HD=1920, SD=720
        int   texHeight     = 1080; // HD=1080, SD=486
        float lAspect       = 0;
        bool  lIsInterlaced = false;

        UtyGLNvSdi.GetSizeFromVideoFormat(options.videoFormat, ref texWidth, ref texHeight, ref lAspect, ref lIsInterlaced);

        // Check the count of cameras. The plugin support until two cameras
        if (outputTexture.Length > 4)
        {
            UnityEngine.Debug.LogWarning("GLNvSdi plugin does not support more than four cameras.");
        }

        // Verify the amount of render textures needed
        int lTexCount = 0;

        if (options.outputDual)
        {
            lTexCount = 2;
        }
        else
        {
            lTexCount = 1;
        }

        if (lIsInterlaced)
        {
            lTexCount *= 2;
        }


        // If dual progressive output, change the order of cameras. So, the first two are active
        if (options.outputDual && !lIsInterlaced)     // dual output progressive
        {
            RenderTexture tmp = outputTexture[1];
            outputTexture[1] = outputTexture[2];
            outputTexture[2] = tmp;
        }


        // Setting the cameras and directing the render to texture
        for (int i = 0; i < lTexCount; ++i)
        {
            if (outputTexture[i] != null)
            {
                //
                // Checking texture sizes
                //
                if (texWidth != outputTexture[i].width || texHeight != outputTexture[i].height)
                {
                    UnityEngine.Debug.LogError("The size of RenderTexture does not match with sdi video options");
                    return(false);
                }

                //
                // Check if the texture has been created
                //
                if (!outputTexture[i].IsCreated())
                {
                    if (!outputTexture[i].Create())
                    {
                        UnityEngine.Debug.LogError("Could not create Output Sdi RenderTexture");
                    }
                }

                //
                // Sending texture id to plugin
                //
                UtyGLNvSdi.SdiOutputSetTexturePtr(i, outputTexture[i].GetNativeTexturePtr(), outputTexture[i].width, outputTexture[i].height);
            }
            else
            {
                UnityEngine.Debug.LogError("Sdi Output RenderTexture is null");
                return(false);
            }
        }

        // If the options to invert fields is marked, we have to invert
        // the textures id already sent to plugin
        if (options.outputInvertFields)
        {
            UtyGLNvSdi.SdiOutputInvertFields(options.outputInvertFields);
        }

        return(true);
    }
Beispiel #21
0
    private IEnumerator SdiIOCoroutine()
    {
        yield return(new WaitForEndOfFrame());

        UtyGLNvSdi.SdiInputSetBufferSize(options.inputRingBufferSize);
        UtyGLNvSdi.SdiInputSetCaptureFields(options.inputCaptureFields);

        //
        // Input/Ouput Initialization
        //
        GL.IssuePluginEvent(UtyGLNvSdi.GetSdiInputRenderEventFunc(), (int)SdiRenderEvent.Initialize);
        GL.IssuePluginEvent(UtyGLNvSdi.GetSdiOutputRenderEventFunc(), (int)SdiRenderEvent.Initialize);
        yield return(new WaitForEndOfFrame());


        if (UtyGLNvSdi.SdiError() == 0)
        {
            //
            // Input Setup
            //
            if (options.inputCaptureFields)
            {
                CreateSdiInputTextures(8, UtyGLNvSdi.SdiInputWidth(), UtyGLNvSdi.SdiInputHeight() / 2);
            }
            else
            {
                CreateSdiInputTextures(4, UtyGLNvSdi.SdiInputWidth(), UtyGLNvSdi.SdiInputHeight());
            }

            GL.IssuePluginEvent(UtyGLNvSdi.GetSdiInputRenderEventFunc(), (int)SdiRenderEvent.Setup);
            GL.IssuePluginEvent(UtyGLNvSdi.GetSdiInputRenderEventFunc(), (int)SdiRenderEvent.StartCapture);
            yield return(new WaitForEndOfFrame());

            //
            // Output Setup
            //
            UtyGLNvSdi.SdiOutputSetGlobalOptions();
            UtyGLNvSdi.SdiOutputSetVideoFormat(
                options.videoFormat,
                options.syncSource,
                options.outputDelay,
                options.outputHorizontalDelay,
                options.outputVerticalDelay,
                options.outputDual,
                options.outputRingBufferSize);

            UtyGLNvSdi.SdiOutputComputePresentTimeFromCapture(true);

            yield return(new WaitForEndOfFrame());


            GL.IssuePluginEvent(UtyGLNvSdi.GetSdiOutputRenderEventFunc(), (int)SdiRenderEvent.Setup);


            int   texWidth   = 1920;
            int   texHeight  = 1080;
            bool  interlaced = true;
            float aspect     = 1.0f;

            UtyGLNvSdi.GetSizeFromVideoFormat(options.videoFormat, ref texWidth, ref texHeight, ref aspect, ref interlaced);
            if (!SetupOutputTextures(texWidth, texHeight, aspect, interlaced, options.outputDual))
            {
                UnityEngine.Debug.LogError("GLNvSdi_Plugin could not setup sdi textures for input/output");
            }

            sdiEnabled = true;

            //while (true)
            //{
            //    // Wait until all frame rendering is done
            //    yield return new WaitForEndOfFrame();

            //    // Capture frame from device
            //    GL.IssuePluginEvent(UtyGLNvSdi.GetSdiInputRenderEventFunc(), (int)SdiRenderEvent.CaptureFrame);
            //    // Present frame
            //    GL.IssuePluginEvent(UtyGLNvSdi.GetSdiOutputRenderEventFunc(), (int)SdiRenderEvent.PresentFrame);

            //}
        }
    }