Skip to content

Commit

Permalink
Merge pull request #52 from rakusan/master
Browse files Browse the repository at this point in the history
This allows shareContext parameter to be passed to eglCreateContext.
  • Loading branch information
MasayukiSuda authored Nov 6, 2019
2 parents 292d063 + 4d75926 commit 063767e
Show file tree
Hide file tree
Showing 4 changed files with 26 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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");
Expand Down Expand Up @@ -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) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -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;

Expand Down Expand Up @@ -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();
Expand Down Expand Up @@ -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());
Expand All @@ -271,7 +283,8 @@ public void onProgress(final double progress) {
flipVertical,
flipHorizontal,
trimStartMs,
trimEndMs
trimEndMs,
shareContext
);

} catch (Exception e) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -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 {


Expand Down Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -69,15 +70,16 @@ 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));
} catch (IOException e) {
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;
Expand Down

0 comments on commit 063767e

Please sign in to comment.