forked from Doikki/DKVideoPlayer
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request Doikki#789 from Doikki/dev
接入GLSurfaceView,实现视频加水印功能
- Loading branch information
Showing
13 changed files
with
581 additions
and
8 deletions.
There are no files selected for viewing
34 changes: 34 additions & 0 deletions
34
dkplayer-sample/src/main/assets/bitmap_overlay_video_processor_fragment.glsl
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
// Copyright 2020 The Android Open Source Project | ||
// | ||
// Licensed under the Apache License, Version 2.0 (the "License"); | ||
// you may not use this file except in compliance with the License. | ||
// You may obtain a copy of the License at | ||
// | ||
// http://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, software | ||
// distributed under the License is distributed on an "AS IS" BASIS, | ||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
// See the License for the specific language governing permissions and | ||
// limitations under the License. | ||
|
||
#extension GL_OES_EGL_image_external : require | ||
precision mediump float; | ||
// External texture containing video decoder output. | ||
uniform samplerExternalOES uTexSampler0; | ||
// Texture containing the overlap bitmap. | ||
uniform sampler2D uTexSampler1; | ||
// Horizontal scaling factor for the overlap bitmap. | ||
uniform float uScaleX; | ||
// Vertical scaling factory for the overlap bitmap. | ||
uniform float uScaleY; | ||
varying vec2 vTexCoords; | ||
void main() { | ||
vec4 videoColor = texture2D(uTexSampler0, vTexCoords); | ||
vec4 overlayColor = texture2D(uTexSampler1, | ||
vec2(vTexCoords.x * uScaleX, | ||
vTexCoords.y * uScaleY)); | ||
// Blend the video decoder output and the overlay bitmap. | ||
gl_FragColor = videoColor * (1.0 - overlayColor.a) | ||
+ overlayColor * overlayColor.a; | ||
} |
21 changes: 21 additions & 0 deletions
21
dkplayer-sample/src/main/assets/bitmap_overlay_video_processor_vertex.glsl
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
// Copyright 2020 The Android Open Source Project | ||
// | ||
// Licensed under the Apache License, Version 2.0 (the "License"); | ||
// you may not use this file except in compliance with the License. | ||
// You may obtain a copy of the License at | ||
// | ||
// http://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, software | ||
// distributed under the License is distributed on an "AS IS" BASIS, | ||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
// See the License for the specific language governing permissions and | ||
// limitations under the License. | ||
attribute vec4 aFramePosition; | ||
attribute vec4 aTexCoords; | ||
uniform mat4 uTexTransform; | ||
varying vec2 vTexCoords; | ||
void main() { | ||
gl_Position = aFramePosition; | ||
vTexCoords = (uTexTransform * aTexCoords).xy; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
142 changes: 142 additions & 0 deletions
142
...ample/src/main/java/xyz/doikki/dkplayer/widget/render/gl/BitmapOverlayVideoProcessor.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,142 @@ | ||
/* | ||
* Copyright (C) 2020 The Android Open Source Project | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
package xyz.doikki.dkplayer.widget.render.gl; | ||
|
||
import static com.google.android.exoplayer2.util.Assertions.checkNotNull; | ||
|
||
import android.content.Context; | ||
import android.content.pm.PackageManager; | ||
import android.graphics.Bitmap; | ||
import android.graphics.Canvas; | ||
import android.graphics.Color; | ||
import android.graphics.Paint; | ||
import android.graphics.drawable.BitmapDrawable; | ||
import android.opengl.GLES20; | ||
import android.opengl.GLUtils; | ||
|
||
import com.google.android.exoplayer2.util.GlProgram; | ||
import com.google.android.exoplayer2.util.GlUtil; | ||
|
||
import java.io.IOException; | ||
|
||
import javax.microedition.khronos.opengles.GL10; | ||
|
||
/** | ||
* Video processor that demonstrates how to overlay a bitmap on video output using a GL shader. The | ||
* bitmap is drawn using an Android {@link Canvas}. | ||
*/ | ||
/* package */ final class BitmapOverlayVideoProcessor implements GLSurfaceRenderView.VideoProcessor { | ||
|
||
private static final int OVERLAY_WIDTH = 512; | ||
private static final int OVERLAY_HEIGHT = 256; | ||
|
||
private final Context context; | ||
private final Paint paint; | ||
private final int[] textures; | ||
private final Bitmap overlayBitmap; | ||
private final Bitmap logoBitmap; | ||
private final Canvas overlayCanvas; | ||
|
||
private GlProgram program; | ||
|
||
private float bitmapScaleX; | ||
private float bitmapScaleY; | ||
|
||
public BitmapOverlayVideoProcessor(Context context) { | ||
this.context = context.getApplicationContext(); | ||
paint = new Paint(); | ||
paint.setTextSize(64); | ||
paint.setAntiAlias(true); | ||
paint.setARGB(0xFF, 0xFF, 0x00, 0x00); | ||
textures = new int[1]; | ||
overlayBitmap = Bitmap.createBitmap(OVERLAY_WIDTH, OVERLAY_HEIGHT, Bitmap.Config.ARGB_8888); | ||
overlayCanvas = new Canvas(overlayBitmap); | ||
try { | ||
logoBitmap = | ||
((BitmapDrawable) | ||
context.getPackageManager().getApplicationIcon(context.getPackageName())) | ||
.getBitmap(); | ||
} catch (PackageManager.NameNotFoundException e) { | ||
throw new IllegalStateException(e); | ||
} | ||
} | ||
|
||
@Override | ||
public void initialize() { | ||
try { | ||
program = | ||
new GlProgram( | ||
context, | ||
/* vertexShaderFilePath= */ "bitmap_overlay_video_processor_vertex.glsl", | ||
/* fragmentShaderFilePath= */ "bitmap_overlay_video_processor_fragment.glsl"); | ||
} catch (IOException e) { | ||
throw new IllegalStateException(e); | ||
} | ||
program.setBufferAttribute( | ||
"aFramePosition", | ||
GlUtil.getNormalizedCoordinateBounds(), | ||
GlUtil.HOMOGENEOUS_COORDINATE_VECTOR_SIZE); | ||
program.setBufferAttribute( | ||
"aTexCoords", | ||
GlUtil.getTextureCoordinateBounds(), | ||
GlUtil.HOMOGENEOUS_COORDINATE_VECTOR_SIZE); | ||
GLES20.glGenTextures(1, textures, 0); | ||
GLES20.glBindTexture(GL10.GL_TEXTURE_2D, textures[0]); | ||
GLES20.glTexParameterf(GL10.GL_TEXTURE_2D, GL10.GL_TEXTURE_MIN_FILTER, GL10.GL_NEAREST); | ||
GLES20.glTexParameterf(GL10.GL_TEXTURE_2D, GL10.GL_TEXTURE_MAG_FILTER, GL10.GL_LINEAR); | ||
GLES20.glTexParameterf(GL10.GL_TEXTURE_2D, GL10.GL_TEXTURE_WRAP_S, GL10.GL_REPEAT); | ||
GLES20.glTexParameterf(GL10.GL_TEXTURE_2D, GL10.GL_TEXTURE_WRAP_T, GL10.GL_REPEAT); | ||
GLUtils.texImage2D(GL10.GL_TEXTURE_2D, /* level= */ 0, overlayBitmap, /* border= */ 0); | ||
} | ||
|
||
@Override | ||
public void setSurfaceSize(int width, int height) { | ||
bitmapScaleX = (float) width / OVERLAY_WIDTH; | ||
bitmapScaleY = (float) height / OVERLAY_HEIGHT; | ||
} | ||
|
||
@Override | ||
public void draw(int frameTexture, float[] transformMatrix) { | ||
// Draw to the canvas and store it in a texture. | ||
String text = "视频水印"; | ||
overlayBitmap.eraseColor(Color.TRANSPARENT); | ||
overlayCanvas.drawBitmap(logoBitmap, /* left= */ 32, /* top= */ 32, paint); | ||
overlayCanvas.drawText(text, /* x= */ 200, /* y= */ 130, paint); | ||
GLES20.glBindTexture(GL10.GL_TEXTURE_2D, textures[0]); | ||
GLUtils.texSubImage2D( | ||
GL10.GL_TEXTURE_2D, /* level= */ 0, /* xoffset= */ 0, /* yoffset= */ 0, overlayBitmap); | ||
GlUtil.checkGlError(); | ||
|
||
// Run the shader program. | ||
GlProgram program = checkNotNull(this.program); | ||
program.setSamplerTexIdUniform("uTexSampler0", frameTexture, /* texUnitIndex= */ 0); | ||
program.setSamplerTexIdUniform("uTexSampler1", textures[0], /* texUnitIndex= */ 1); | ||
program.setFloatUniform("uScaleX", bitmapScaleX); | ||
program.setFloatUniform("uScaleY", bitmapScaleY); | ||
program.setFloatsUniform("uTexTransform", transformMatrix); | ||
program.bindAttributesAndUniforms(); | ||
GLES20.glClear(GLES20.GL_COLOR_BUFFER_BIT); | ||
GLES20.glDrawArrays(GLES20.GL_TRIANGLE_STRIP, /* first= */ 0, /* count= */ 4); | ||
GlUtil.checkGlError(); | ||
} | ||
|
||
@Override | ||
public void release() { | ||
if (program != null) { | ||
program.delete(); | ||
} | ||
} | ||
} |
Oops, something went wrong.