From 4d75926897b898e577da218da5a47cb02e7a0caa Mon Sep 17 00:00:00 2001 From: rakusan Date: Wed, 6 Nov 2019 09:59:05 +0900 Subject: [PATCH] Add shareContext builder method to Mp4Composer and use it to create EGLContext in EncoderSurface. --- .../mp4compose/composer/EncoderSurface.java | 8 ++++---- .../daasuu/mp4compose/composer/Mp4Composer.java | 15 ++++++++++++++- .../mp4compose/composer/Mp4ComposerEngine.java | 6 ++++-- .../daasuu/mp4compose/composer/VideoComposer.java | 6 ++++-- 4 files changed, 26 insertions(+), 9 deletions(-) diff --git a/mp4compose/src/main/java/com/daasuu/mp4compose/composer/EncoderSurface.java b/mp4compose/src/main/java/com/daasuu/mp4compose/composer/EncoderSurface.java index c97da0d..5eb4f10 100755 --- a/mp4compose/src/main/java/com/daasuu/mp4compose/composer/EncoderSurface.java +++ b/mp4compose/src/main/java/com/daasuu/mp4compose/composer/EncoderSurface.java @@ -28,18 +28,18 @@ class EncoderSurface { /** * Creates an EncoderSurface from a Surface. */ - EncoderSurface(Surface surface) { + EncoderSurface(Surface surface, EGLContext shareContext) { if (surface == null) { throw new NullPointerException(); } this.surface = surface; - eglSetup(); + eglSetup(shareContext); } /** * Prepares EGL. We want a GLES 2.0 context and a surface that supports recording. */ - private void eglSetup() { + private void eglSetup(EGLContext shareContext) { eglDisplay = EGL14.eglGetDisplay(EGL14.EGL_DEFAULT_DISPLAY); if (eglDisplay == EGL14.EGL_NO_DISPLAY) { throw new RuntimeException("unable to get EGL14 display"); @@ -70,7 +70,7 @@ private void eglSetup() { EGL14.EGL_CONTEXT_CLIENT_VERSION, 2, EGL14.EGL_NONE }; - eglContext = EGL14.eglCreateContext(eglDisplay, configs[0], EGL14.EGL_NO_CONTEXT, + eglContext = EGL14.eglCreateContext(eglDisplay, configs[0], shareContext != null ? shareContext : EGL14.EGL_NO_CONTEXT, attrib_list, 0); checkEglError("eglCreateContext"); if (eglContext == null) { diff --git a/mp4compose/src/main/java/com/daasuu/mp4compose/composer/Mp4Composer.java b/mp4compose/src/main/java/com/daasuu/mp4compose/composer/Mp4Composer.java index f9b5fc5..102d5a7 100644 --- a/mp4compose/src/main/java/com/daasuu/mp4compose/composer/Mp4Composer.java +++ b/mp4compose/src/main/java/com/daasuu/mp4compose/composer/Mp4Composer.java @@ -5,6 +5,8 @@ import android.media.MediaCodec; import android.media.MediaMetadataRetriever; import android.net.Uri; +import android.opengl.EGL14; +import android.opengl.EGLContext; import android.os.Build; import android.util.Size; @@ -50,6 +52,7 @@ public class Mp4Composer { private boolean flipHorizontal = false; private long trimStartMs = 0; private long trimEndMs = -1; + private EGLContext shareContext = EGL14.EGL_NO_CONTEXT; private ExecutorService executorService; @@ -178,6 +181,11 @@ public Mp4Composer trim(final long trimStartMs, final long trimEndMs) { return this; } + public Mp4Composer shareContext(@NonNull EGLContext shareContext) { + this.shareContext = shareContext; + return this; + } + private ExecutorService getExecutorService() { if (executorService == null) { executorService = Executors.newSingleThreadExecutor(); @@ -245,6 +253,10 @@ public void onProgress(final double progress) { timeScale = 1; } + if (shareContext == null) { + shareContext = EGL14.EGL_NO_CONTEXT; + } + logger.debug(TAG, "rotation = " + (rotation.getRotation() + videoRotate)); logger.debug(TAG, "rotation = " + Rotation.fromInt(rotation.getRotation() + videoRotate)); logger.debug(TAG, "inputResolution width = " + srcVideoResolution.getWidth() + " height = " + srcVideoResolution.getHeight()); @@ -271,7 +283,8 @@ public void onProgress(final double progress) { flipVertical, flipHorizontal, trimStartMs, - trimEndMs + trimEndMs, + shareContext ); } catch (Exception e) { diff --git a/mp4compose/src/main/java/com/daasuu/mp4compose/composer/Mp4ComposerEngine.java b/mp4compose/src/main/java/com/daasuu/mp4compose/composer/Mp4ComposerEngine.java index 0879031..0def67b 100755 --- a/mp4compose/src/main/java/com/daasuu/mp4compose/composer/Mp4ComposerEngine.java +++ b/mp4compose/src/main/java/com/daasuu/mp4compose/composer/Mp4ComposerEngine.java @@ -6,6 +6,7 @@ import android.media.MediaFormat; import android.media.MediaMetadataRetriever; import android.media.MediaMuxer; +import android.opengl.EGLContext; import android.os.Build; import android.util.Size; @@ -67,7 +68,8 @@ void compose( final boolean flipVertical, final boolean flipHorizontal, final long trimStartMs, - final long trimEndMs + final long trimEndMs, + final EGLContext shareContext ) throws IOException { @@ -109,7 +111,7 @@ void compose( // setup video composer videoComposer = new VideoComposer(mediaExtractor, videoTrackIndex, actualVideoOutputFormat, muxRender, timeScale, trimStartMs, trimEndMs, logger); - videoComposer.setUp(filter, rotation, outputResolution, inputResolution, fillMode, fillModeCustomItem, flipVertical, flipHorizontal); + videoComposer.setUp(filter, rotation, outputResolution, inputResolution, fillMode, fillModeCustomItem, flipVertical, flipHorizontal, shareContext); mediaExtractor.selectTrack(videoTrackIndex); // setup audio if present and not muted diff --git a/mp4compose/src/main/java/com/daasuu/mp4compose/composer/VideoComposer.java b/mp4compose/src/main/java/com/daasuu/mp4compose/composer/VideoComposer.java index 1b534c7..5cfa674 100755 --- a/mp4compose/src/main/java/com/daasuu/mp4compose/composer/VideoComposer.java +++ b/mp4compose/src/main/java/com/daasuu/mp4compose/composer/VideoComposer.java @@ -4,6 +4,7 @@ import android.media.MediaCodec; import android.media.MediaExtractor; import android.media.MediaFormat; +import android.opengl.EGLContext; import android.util.Size; import androidx.annotation.NonNull; @@ -69,7 +70,8 @@ void setUp(GlFilter filter, FillMode fillMode, FillModeCustomItem fillModeCustomItem, final boolean flipVertical, - final boolean flipHorizontal) { + final boolean flipHorizontal, + final EGLContext shareContext) { mediaExtractor.selectTrack(trackIndex); try { encoder = MediaCodec.createEncoderByType(outputFormat.getString(MediaFormat.KEY_MIME)); @@ -77,7 +79,7 @@ void setUp(GlFilter filter, throw new IllegalStateException(e); } encoder.configure(outputFormat, null, null, MediaCodec.CONFIGURE_FLAG_ENCODE); - encoderSurface = new EncoderSurface(encoder.createInputSurface()); + encoderSurface = new EncoderSurface(encoder.createInputSurface(), shareContext); encoderSurface.makeCurrent(); encoder.start(); encoderStarted = true;