The player will consume considerable memory in different degrees when rendering and decoding videos of different quality. In order to ensure effective memory allocation of the main process, it can be considered to run the video decoding module independently with a new process, and the rendering is still retained in the main process. To achieve this, you can use the Android.view. Surface class and Service\TextureView to implement a cross-process video player.

1, TextureView

In the main process in the UI thread, setting TextureView SurfaceTextureListener, in SurfaceTextureListener onSurfaceTextureAvailable new a Surface object, And passes it to the remote service’s client, PlayerClient. With the Surface object, the PlayerClient can be delivered to the remote server through the Binder, and the video frames decoded by the remote server can be rendered to the TextureView of the main process.

new Surface(TextureView.getSurfaceTexture())
Copy the code
package android.view;
public class Surface implements Parcelable {
    ...
}
Copy the code

2, the Service

Since the client calls the server’s methods to run in the Binder thread pool, and our player needs to run in the UI thread of the current child process, the Surface object needs to be passed to the UI thread again to play properly.

3. Player

Reference android – BasicMediaDecoder, only need to modify the MediaCodecWrapper. FromVideoFormat can.

MediaCodecWrapper.fromVideoFormat(mVideoExtractor.getTrackFormat(i),surface);
Copy the code

4. Running results

After starting the playback process, you can see that there are two processes 28964 and 30978. 28964 is the main rendering process, and 30978 is the decoding process. The division of the two processes realizes the function of cross-process video playback.