Search code examples
androidvideoandroid-mediaplayerrenderscript

Multiple android Surface consumers - video playback to two surfaceviews


I have this use case, where video from MediaPlayer has to be delivered to two Surfaces. Unfortunately, whole Android Surface API lack's of that functionality (or at least, after studying developers site, I'm unable to find it).

I've had a simillar use case where the video was produced by a custom camera module, but after a slight modification, I was able to retrieve Bitmap from the camera so I just used lockCanvas, drawBitmap and unlockAndPost on two Surfaces. With MediaPlayer, I don't know how to retrieve Bitmap and keep playback with proper timing.

Also, I've tried to use Allocation for that purpose, with one Allocation serving as USAGE_IO_INPUT, two as USAGE_IO_OUTPUT, and with ioReceive, copyFrom, ioSend methods. But it was also an dead end. For some unknown reason, RenderScript engine is very unstable on my platform, I've had numerous errors like:

android.renderscript.RSInvalidStateException: Calling RS with no Context active.

when context passed to RenderScript.create was this from Application class, or

 Failed loading RS driver: dlopen failed: could not locate symbol .... falling back to default

(I've lost full log somewhere...). And at the end, I was not able to create proper Input Allocation type to be compatible with MediaPlayer. Due to mentioned flaws with RenderScript on my platform, I would consider this as last resort for solving this issue.

So, in conclusion: How to play video (from mp4 file) to two Surfaces? This video has to be in sync. Also, more generic question, how to play video to #X Surface's which can be dynamically added, removed during playback?


Solution

  • I've resolved my issue by having multiple instances of MediaPlayer with same video file source. When doing basic player operations like pause/play/seek, I'm just doing them on every player.