Can an Android (Java not C/C++) plugin alter unity textures?

1.7k views Asked by At

I'm trying to use an Android plugin to get around the fact that Unity does not support Video Textures on mobile. I do this by getting the texture ID of the texture that will be used for video (supplied by Texture2D.GetNativeTextureID()) which i then pass into the Java Plugin.

The plugin then follows a standard MediaPlayer implementation to play the video into a Surface Texture, which has been assigned the aforementioned texture ID.

When i call the MediaPlayer.Start() method, LogCat outputs as if the media player is working normally/as hoped, however the texture never changes in the app. I'm calling the plugin (and even initializing it) from the OnPreRender() method of a monobehaviour script, along with making sure to call GL.InvalidateState() too.

Here's a look at the relevant code:

Constructor:

public VideoPlayer(String _path, Context _cont) {
    m_MediaPlayer = new MediaPlayer();
    try {
        Uri uri = Uri.parse(_path);
        m_MediaPlayer.setDataSource(_cont, uri);
        m_MediaPlayer.setOnPreparedListener(this);
    }
    catch (IOException e) {
        Log.e(TAG, "Error setting data source: " + e.getMessage());
    }
}

Surface Texture Prep:

public void PrepareVideo(int _texPtr) {
    try {
        m_SurfTex = new SurfaceTexture(_texPtr);
        m_SurfTex.setOnFrameAvailableListener(this);
        Log.i(TAG, "Surface Texture ready");
        Surface surface = new Surface(m_SurfTex);
        m_MediaPlayer.setSurface(surface);
        Log.i(TAG, "Surface Ready");
        surface.release();
        m_Ready = true;
        m_MediaPlayer.prepare();
    }
    catch (IOException | IllegalStateException e) {
        Log.e(TAG, "Error preparing player: " + e.getMessage());
        m_Ready = false;
    }
}

I'd like to know if this type of process is possible with my current setup, or will have to look into writing something using the NDK instead? Specifically, can a surface texture alter a texture produced in Unity?

2

There are 2 answers

2
sh3rifme On BEST ANSWER

It would appears that while i have found nothing explicitly states that this CAN be done, there is no real information or any examples of it having been done before.

I'll be attempting a re-working of this in native C++ code.

4
RobotRock On

Did you try creating the surfacetexture in the unity rendering thread, for example by overriding runglthreadsjobs in an extended UnityPlayer? That would make sure it bound to the correct glcontext AFAIK.

I have tried sharing contexts, but couldn't get it to work as documentation is sparse and unity is pretty closed down.

In unity 4.2 its a lot easier apparently.