Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion epf/src/main/java/com/daasuu/epf/EPlayerRenderer.java
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ public void run() {

@Override
public void onSurfaceCreated(final EGLConfig config) {
GLES20.glClearColor(0.0f, 0.0f, 0.0f, 1.0f);
GLES20.glClearColor(0.0f, 0.0f, 0.0f, 0.0f);

final int[] args = new int[1];

Expand Down Expand Up @@ -136,6 +136,8 @@ public void onSurfaceChanged(final int width, final int height) {

@Override
public void onDrawFrame(final EFramebufferObject fbo) {
GLES20.glEnable(GLES20.GL_BLEND);
GLES20.glBlendFunc(GLES20.GL_SRC_ALPHA, GLES20.GL_ONE_MINUS_SRC_ALPHA);

synchronized (this) {
if (updateSurface) {
Expand Down
19 changes: 19 additions & 0 deletions epf/src/main/java/com/daasuu/epf/EPlayerTranslucentView.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package com.daasuu.epf;

import android.content.Context;
import android.util.AttributeSet;

/**
* Created by LukeNeedham on 2020/01/14.
*/
public class EPlayerTranslucentView extends EPlayerView {

public EPlayerTranslucentView(Context context) {
this(context, null);
}

public EPlayerTranslucentView(Context context, AttributeSet attrs) {
super(context, attrs);
setZOrderOnTop(true);
}
}
41 changes: 34 additions & 7 deletions epf/src/main/java/com/daasuu/epf/EPlayerView.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package com.daasuu.epf;

import android.content.Context;
import android.graphics.PixelFormat;
import android.opengl.GLSurfaceView;
import android.util.AttributeSet;

Expand All @@ -10,6 +11,8 @@
import com.google.android.exoplayer2.SimpleExoPlayer;
import com.google.android.exoplayer2.video.VideoListener;

import static com.daasuu.epf.chooser.EConfigChooser.EGL_CONTEXT_CLIENT_VERSION;

/**
* Created by sudamasayuki on 2017/05/16.
*/
Expand All @@ -20,7 +23,14 @@ public class EPlayerView extends GLSurfaceView implements VideoListener {
private final EPlayerRenderer renderer;
private SimpleExoPlayer player;

private float videoAspect = 1f;
/* Video Aspect according to the video */
private float measuredVideoAspect = 1f;

/* Video Aspect according to the video, adjusted to the needs of the filter */
private float adjustedVideoAspect = measuredVideoAspect;

private GlFilter glFilter = null;

private PlayerScaleType playerScaleType = PlayerScaleType.RESIZE_FIT_WIDTH;

public EPlayerView(Context context) {
Expand All @@ -31,11 +41,13 @@ public EPlayerView(Context context, AttributeSet attrs) {
super(context, attrs);

setEGLContextFactory(new EContextFactory());
setEGLConfigChooser(new EConfigChooser());

setEGLConfigChooser(new EConfigChooser(8, 8, 8, 8, 16, 0, EGL_CONTEXT_CLIENT_VERSION));

getHolder().setFormat(PixelFormat.RGBA_8888);

renderer = new EPlayerRenderer(this);
setRenderer(renderer);

}

public EPlayerView setSimpleExoPlayer(SimpleExoPlayer player) {
Expand All @@ -50,7 +62,12 @@ public EPlayerView setSimpleExoPlayer(SimpleExoPlayer player) {
}

public void setGlFilter(GlFilter glFilter) {
this.glFilter = glFilter;
renderer.setGlFilter(glFilter);

adjustedVideoAspect = calculateAdjustedVideoAspect();

requestLayout();
}

public void setPlayerScaleType(PlayerScaleType playerScaleType) {
Expand All @@ -70,10 +87,10 @@ protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {

switch (playerScaleType) {
case RESIZE_FIT_WIDTH:
viewHeight = (int) (measuredWidth / videoAspect);
viewHeight = (int) (measuredWidth / adjustedVideoAspect);
break;
case RESIZE_FIT_HEIGHT:
viewWidth = (int) (measuredHeight * videoAspect);
viewWidth = (int) (measuredHeight * adjustedVideoAspect);
break;
}

Expand All @@ -95,13 +112,23 @@ public void onPause() {
@Override
public void onVideoSizeChanged(int width, int height, int unappliedRotationDegrees, float pixelWidthHeightRatio) {
// Log.d(TAG, "width = " + width + " height = " + height + " unappliedRotationDegrees = " + unappliedRotationDegrees + " pixelWidthHeightRatio = " + pixelWidthHeightRatio);
videoAspect = ((float) width / height) * pixelWidthHeightRatio;
// Log.d(TAG, "videoAspect = " + videoAspect);
measuredVideoAspect = ((float) width / height) * pixelWidthHeightRatio;
adjustedVideoAspect = calculateAdjustedVideoAspect();
// Log.d(TAG, "measuredVideoAspect = " + measuredVideoAspect);

requestLayout();
}

@Override
public void onRenderedFirstFrame() {
// do nothing
}

private float calculateAdjustedVideoAspect() {
if (glFilter == null) {
return measuredVideoAspect;
} else {
return glFilter.getVideoAspect(measuredVideoAspect);
}
}
}
4 changes: 2 additions & 2 deletions epf/src/main/java/com/daasuu/epf/chooser/EConfigChooser.java
Original file line number Diff line number Diff line change
Expand Up @@ -30,10 +30,10 @@ public class EConfigChooser implements GLSurfaceView.EGLConfigChooser {
private final int depthSize;
private final int stencilSize;

private static final int EGL_CONTEXT_CLIENT_VERSION = 2;

private static final boolean USE_RGB_888 = Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR1;

public static final int EGL_CONTEXT_CLIENT_VERSION = 2;

public EConfigChooser() {
this(
USE_RGB_888 ? 8 : 5,
Expand Down
85 changes: 85 additions & 0 deletions epf/src/main/java/com/daasuu/epf/filter/AlphaFrameFilter.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
package com.daasuu.epf.filter;

/**
* Also known as Luma Matte.
* Used for videos which comprise half of content, and half of alpha-mask / luma-matte.
* The result is a video containing only content, masked by the alpha-mask to add transparency.
* <p>
* To use this filter, you need to use a EPlayerTranslucentView rather than an ordinary EPlayerView.
* <p>
* Or, use an EPlayerView and ensure you call `EPlayerView.setZOrderOnTop(true)`
* before the surface view's containing window is attached to the window manager
*/
public class AlphaFrameFilter extends GlFilter {

private static final String VERTEX_SHADER =
"attribute vec4 aPosition;\n" +
"attribute vec4 aTextureCoord;\n" +
"varying highp vec2 vTextureCoordContent;\n" +
"varying highp vec2 vTextureCoordMask;\n" +
"void main() {\n" +
"gl_Position = aPosition;\n" +
"vTextureCoordContent = %s;\n" +
"vTextureCoordMask = %s;\n" +
"}\n";

private static final String FRAGMENT_SHADER =
"precision mediump float;\n" +
"varying highp vec2 vTextureCoordContent;\n" +
"varying highp vec2 vTextureCoordMask;\n" +
"uniform lowp sampler2D sTexture;\n" +
"void main() {\n" +
"vec4 colorContent = texture2D(sTexture, vTextureCoordContent);\n" +
"vec4 colorMask = texture2D(sTexture, vTextureCoordMask);\n" +
"gl_FragColor = vec4(colorContent.rgb, colorMask.r);\n" +
"}\n";

private AlphaMaskPosition alphaMaskPosition;

/**
* @param alphaMaskPosition the position of the alpha-mask in the video.
*/
public AlphaFrameFilter(AlphaMaskPosition alphaMaskPosition) {
super(getVertexShader(alphaMaskPosition), FRAGMENT_SHADER);
this.alphaMaskPosition = alphaMaskPosition;
}

@Override
public float getVideoAspect(float originalVideoAspect) {
float factor;
if (alphaMaskPosition == AlphaMaskPosition.TOP || alphaMaskPosition == AlphaMaskPosition.BOTTOM) {
factor = 2f;
} else {
factor = 1f / 2f;
}
return originalVideoAspect * factor;
}

private static String getVertexShader(AlphaMaskPosition alphaMaskPosition) {
String vTextureCoordContent;
String vTextureCoordMask;

// Note: LEFT and TOP are untested! If funky stuff is occurring, check here
if (alphaMaskPosition == AlphaMaskPosition.LEFT) {
vTextureCoordContent = "vec2(aTextureCoord.x*0.5+0.5, aTextureCoord.y)";
vTextureCoordMask = "vec2(aTextureCoord.x*0.5, aTextureCoord.y)";
} else if (alphaMaskPosition == AlphaMaskPosition.TOP) {
vTextureCoordContent = "vec2(aTextureCoord.x, aTextureCoord.y*0.5)";
vTextureCoordMask = "vec2(aTextureCoord.x, aTextureCoord.y*0.5+0.5)";
} else if (alphaMaskPosition == AlphaMaskPosition.RIGHT) {
vTextureCoordContent = "vec2(aTextureCoord.x*0.5, aTextureCoord.y)";
vTextureCoordMask = "vec2(aTextureCoord.x*0.5+0.5, aTextureCoord.y)";
} else if (alphaMaskPosition == AlphaMaskPosition.BOTTOM) {
vTextureCoordContent = "vec2(aTextureCoord.x, aTextureCoord.y*0.5+0.5)";
vTextureCoordMask = "vec2(aTextureCoord.x, aTextureCoord.y*0.5)";
} else {
throw new RuntimeException("No vertex shader found for alphaMaskPosition" + alphaMaskPosition);
}

return String.format(VERTEX_SHADER, vTextureCoordContent, vTextureCoordMask);
}

public enum AlphaMaskPosition {
LEFT, TOP, RIGHT, BOTTOM
}
}
4 changes: 4 additions & 0 deletions epf/src/main/java/com/daasuu/epf/filter/GlFilter.java
Original file line number Diff line number Diff line change
Expand Up @@ -130,6 +130,10 @@ public void draw(final int texName, final EFramebufferObject fbo) {
GLES20.glBindBuffer(GLES20.GL_ARRAY_BUFFER, 0);
}

public float getVideoAspect(float originalVideoAspect) {
return originalVideoAspect;
}

protected void onDraw() {
}

Expand Down