Esempio n. 1
0
        public override void Render()
        {
            if (m_Video != null)
            {
                if (m_UseFastOesPath)
                {
                    // This is needed for at least Unity 5.5.0, otherwise it just renders black in OES mode
                    GL.InvalidateState();
                }
                AndroidMediaPlayer.IssuePluginEvent(Native.AVPPluginEvent.PlayerUpdate, m_iPlayerIndex);
                if (m_UseFastOesPath)
                {
                    GL.InvalidateState();
                }

                // Check if we can create the texture
                // Scan for a change in resolution
                int newWidth  = -1;
                int newHeight = -1;
                if (m_Texture != null)
                {
#if DLL_METHODS
                    newWidth  = Native._GetWidth(m_iPlayerIndex);
                    newHeight = Native._GetHeight(m_iPlayerIndex);
#else
                    newWidth  = m_Video.Call <int>("GetWidth");
                    newHeight = m_Video.Call <int>("GetHeight");
#endif
                    if (newWidth != m_Width || newHeight != m_Height)
                    {
                        m_Texture       = null;
                        m_TextureHandle = 0;
                    }
                }
#if DLL_METHODS
                int textureHandle = Native._GetTextureHandle(m_iPlayerIndex);
#else
                int textureHandle = m_Video.Call <int>("GetTextureHandle");
#endif
                if (textureHandle != 0 && textureHandle != m_TextureHandle)
                {
                    // Already got? (from above)
                    if (newWidth == -1 || newHeight == -1)
                    {
#if DLL_METHODS
                        newWidth  = Native._GetWidth(m_iPlayerIndex);
                        newHeight = Native._GetHeight(m_iPlayerIndex);
#else
                        newWidth  = m_Video.Call <int>("GetWidth");
                        newHeight = m_Video.Call <int>("GetHeight");
#endif
                    }

                    if (Mathf.Max(newWidth, newHeight) > SystemInfo.maxTextureSize)
                    {
                        m_Width         = newWidth;
                        m_Height        = newHeight;
                        m_TextureHandle = textureHandle;
                        Debug.LogError("[AVProVideo] Video dimensions larger than maxTextureSize");
                    }
                    else if (newWidth > 0 && newHeight > 0)
                    {
                        m_Width         = newWidth;
                        m_Height        = newHeight;
                        m_TextureHandle = textureHandle;

                        switch (m_API)
                        {
                        case Android.VideoApi.MediaPlayer:
                            _playerDescription = "MediaPlayer";
                            break;

                        case Android.VideoApi.ExoPlayer:
                            _playerDescription = "ExoPlayer";
                            break;

                        default:
                            _playerDescription = "UnknownPlayer";
                            break;
                        }

                        if (m_UseFastOesPath)
                        {
                            _playerDescription += " OES";
                        }
                        else
                        {
                            _playerDescription += " NonOES";
                        }

                        Helper.LogInfo("Using playback path: " + _playerDescription + " (" + m_Width + "x" + m_Height + "@" + GetVideoFrameRate().ToString("F2") + ")");

                        // NOTE: From Unity 5.4.x when using OES textures, an error "OPENGL NATIVE PLUG-IN ERROR: GL_INVALID_OPERATION: Operation illegal in current state" will be logged.
                        // We assume this is because we're passing in TextureFormat.RGBA32 which isn't the true texture format.  This error should be safe to ignore.
                        m_Texture = Texture2D.CreateExternalTexture(m_Width, m_Height, TextureFormat.RGBA32, false, false, new System.IntPtr(textureHandle));
                        if (m_Texture != null)
                        {
                            ApplyTextureProperties(m_Texture);
                        }
                        Helper.LogInfo("Texture ID: " + textureHandle);
                    }
                }

#if AVPROVIDEO_FIXREGRESSION_TEXTUREQUALITY_UNITY542
                // In Unity 5.4.2 and above the video texture turns black when changing the TextureQuality in the Quality Settings
                // The code below gets around this issue.  A bug report has been sent to Unity.  So far we have tested and replicated the
                // "bug" in Windows only, but a user has reported it in Android too.
                // Texture.GetNativeTexturePtr() must sync with the rendering thread, so this is a large performance hit!
                if (_textureQuality != QualitySettings.masterTextureLimit)
                {
                    if (m_Texture != null && textureHandle > 0 && m_Texture.GetNativeTexturePtr() == System.IntPtr.Zero)
                    {
                        //Debug.Log("RECREATING");
                        m_Texture.UpdateExternalTexture(new System.IntPtr(textureHandle));
                    }

                    _textureQuality = QualitySettings.masterTextureLimit;
                }
#endif
            }
        }