// Update is called once per frame
    void Update()
    {
        // process engine messages (TODO: put in some other place)
        agora_gaming_rtc.IRtcEngine engine = agora_gaming_rtc.IRtcEngine.QueryEngine();
        if (engine == null)
        {
            return;
        }

        // render video
        RawImage rend = GetComponent <RawImage>();

        uint uid = mUid;

#if UNITY_STANDALONE_WIN || UNITY_EDITOR || UNITY_EDITOR_OSX || UNITY_STANDALONE_OSX || UNITY_ANDROID || UNITY_IOS || UNITY_IPHONE
        if (mEnable)
        {
            // create texture if not existent
            if (rend.material.mainTexture == null && rend.texture == null)
            {
                int tmpi = engine.UpdateTexture(0, uid, data, ref defWidth, ref defHeight);

                if (tmpi == -1)
                {
                    return;
                }

                if (defWidth > 0 && defHeight > 0)
                {
                    try
                    {
                        // create Texture in the first time update data
                        nativeTexture             = new Texture2D((int)defWidth, (int)defHeight, TextureFormat.RGBA32, false);
                        rend.material.mainTexture = nativeTexture;
                        rend.texture = nativeTexture;
                        nativeTexture.LoadRawTextureData(data, (int)defWidth * (int)defHeight * 4);
                        nativeTexture.Apply();
                    }
                    catch (System.Exception e)
                    {
                        Debug.Log("Exception e = " + e);
                    }
                }
            }
            else if (rend.material.mainTexture != null && rend.material.mainTexture is Texture2D)
            {
                int width  = 0;
                int height = 0;
                int tmpi   = engine.UpdateTexture(0, uid, data, ref width, ref height);
                if (tmpi == -1)
                {
                    return;
                }

                if (width == defWidth && height == defHeight)
                {
                    try
                    {
                        /*
                         *  if width and height don't change ,we only need to update data for texture, do not need to create Texture.
                         */
                        nativeTexture.LoadRawTextureData(data, (int)width * (int)height * 4);
                        nativeTexture.Apply();
                    }
                    catch (System.Exception e)
                    {
                        Debug.Log("Exception e = " + e);
                    }
                }
                else
                {
                    try
                    {
                        /*
                         * if width or height changed ,we need to create new texture.
                         */
                        defWidth                  = width;
                        defHeight                 = height;
                        nativeTexture             = null;
                        nativeTexture             = new Texture2D((int)defWidth, (int)defHeight, TextureFormat.RGBA32, false);
                        rend.material.mainTexture = nativeTexture;
                        rend.texture              = nativeTexture;
                    }
                    catch (System.Exception e)
                    {
                        Debug.Log("Exception e = " + e);
                    }
                }
            }
        }
        else
        {
            if (rend.material.mainTexture != null && rend.material.mainTexture is Texture2D)
            {
                rend.material.mainTexture = null;
            }
        }
#else
        if (mEnable)
        {
            // create texture if not existent
            if (rend.material.mainTexture == null)
            {
                System.IntPtr texPtr        = (IntPtr)engine.GenerateNativeTexture();
                Texture2D     nativeTexture = Texture2D.CreateExternalTexture(640, 360, TextureFormat.ARGB32, false, false, texPtr); // FIXME! texture size is subject to change
                rend.material.mainTexture = nativeTexture;
            }
            if (rend.material.mainTexture != null && rend.material.mainTexture is Texture2D)
            {
                Texture2D tex       = rend.material.mainTexture as Texture2D;
                int       texId     = (int)tex.GetNativeTexturePtr();
                int       texWidth  = 0;
                int       texHeight = 0;
                if (engine.UpdateTexture(texId, uid, data, ref texWidth, ref texHeight) == 0)
                {
                    // TODO: process texture then render
                }
            }
        }
        else
        {
            if (rend.material.mainTexture != null && rend.material.mainTexture is Texture2D)
            {
                Texture2D tex   = rend.material.mainTexture as Texture2D;
                int       texId = (int)tex.GetNativeTexturePtr();
                engine.DeleteTexture(texId);
                rend.material.mainTexture = null;
            }
        }
#endif
    }
Example #2
0
    // Update is called once per frame
    void Update()
    {
        // process engine messages (TODO: put in some other place)
        agora_gaming_rtc.IRtcEngine engine = agora_gaming_rtc.IRtcEngine.QueryEngine();
        if (engine == null)
        {
            return;
        }

        while (engine.GetMessageCount() > 0)
        {
            engine.Poll();
        }

        // render video
        Renderer rend = GetComponent <Renderer> ();

        Debug.Log(" VideoSurface  render  =  " + rend);

                #if UNITY_IOS || UNITY_ANDROID
        uint uid = mUid;
        if (mEnable)
        {
            // create texture if not existent
            if (rend.material.mainTexture == null)
            {
                //Debug.Log("rend.material.mainTexture");
                System.IntPtr texPtr        = (System.IntPtr)engine.GenerateNativeTexture();
                Texture2D     nativeTexture = Texture2D.CreateExternalTexture(640, 360, TextureFormat.ARGB32, false, false, texPtr);              // FIXME! texture size is subject to change
                rend.material.mainTexture = nativeTexture;
            }

            // update texture
            if (rend.material.mainTexture != null && rend.material.mainTexture is Texture2D)
            {
                //Debug.Log("rend.material.mainTexture is Texture2D");
                //Debug.Log("rend.material.mainTexture is Texture2D  ThreadID : -->"+System.Threading.Thread.CurrentThread.ManagedThreadId);
                Texture2D tex   = rend.material.mainTexture as Texture2D;
                int       texId = (int)tex.GetNativeTexturePtr();

                // update texture (possible size changing)
                uint texWidth  = 0;
                uint texHeight = 0;
                if (engine.UpdateTexture(texId, uid, ref texWidth, ref texHeight) == 0)
                {
                    // TODO: process texture then render
                }
            }
        }
        else
        {
            if (rend.material.mainTexture != null && rend.material.mainTexture is Texture2D)
            {
                Texture2D tex   = rend.material.mainTexture as Texture2D;
                int       texId = (int)tex.GetNativeTexturePtr();
                engine.DeleteTexture(texId);
                rend.material.mainTexture = null;
            }
        }
                #endif

        // add your extra effects
        if (mAdjustTransfrom != null)
        {
            Transform trans = rend.transform;
            mAdjustTransfrom(mUid, gameObject.name, ref trans);
        }
    }
Example #3
0
    // Update is called once per frame
    void Update()
    {
        // process engine messages (TODO: put in some other place)
        agora_gaming_rtc.IRtcEngine engine = agora_gaming_rtc.IRtcEngine.QueryEngine();
        if (engine == null)
        {
            return;
        }

        while (engine.GetMessageCount() > 0)
        {
            engine.Poll();
        }

        // render video

        Image image = GetComponent <Image>();

#if UNITY_IOS || UNITY_ANDROID
        uint uid = mUid;
        if (mEnable)
        {
            // create texture if not existent
            if (testTexture == null)
            {
                System.IntPtr texPtr        = (System.IntPtr)engine.GenerateNativeTexture();
                Texture2D     nativeTexture = Texture2D.CreateExternalTexture(640, 360, TextureFormat.ARGB32, false, false, texPtr); // FIXME! texture size is subject to change

                testTexture = nativeTexture;
                Sprite m_sprite = Sprite.Create(testTexture, new Rect(0, 0, testTexture.width, testTexture.height), new Vector2(0, 0));
                image.sprite = m_sprite;
            }

            // update texture
            if (image.sprite != null && testTexture is Texture2D)
            {
                Texture2D tex   = testTexture;
                int       texId = (int)tex.GetNativeTexturePtr();

                // update texture (possible size changing)
                uint texWidth  = 0;
                uint texHeight = 0;
                if (engine.UpdateTexture(texId, uid, ref texWidth, ref texHeight) == 0)
                {
                    // TODO: process texture then render
                }
            }
        }
        else
        {
            if (image.sprite != null && testTexture is Texture2D)
            {
                Texture2D tex   = testTexture as Texture2D;
                int       texId = (int)tex.GetNativeTexturePtr();
                engine.DeleteTexture(texId);
                testTexture = null;
            }
        }
#endif

        // add your extra effects
        //if (mAdjustTransfrom != null)
        //{
        //    Transform trans = image.transform;
        //    mAdjustTransfrom(mUid, gameObject.name, ref trans);
        //}
    }