Skip to content

Commit

Permalink
Add setFilter to video player, create AbstractVideoPlayer
Browse files Browse the repository at this point in the history
  • Loading branch information
dasisdormax committed Aug 31, 2023
1 parent 4b7f515 commit e543296
Show file tree
Hide file tree
Showing 7 changed files with 73 additions and 43 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -36,20 +36,18 @@
import com.badlogic.gdx.files.FileHandle;
import com.badlogic.gdx.graphics.GL20;
import com.badlogic.gdx.graphics.Pixmap;
import com.badlogic.gdx.graphics.Texture;
import com.badlogic.gdx.graphics.glutils.FrameBuffer;
import com.badlogic.gdx.graphics.glutils.ImmediateModeRenderer20;
import com.badlogic.gdx.graphics.glutils.ShaderProgram;
import com.badlogic.gdx.math.Matrix4;
import com.badlogic.gdx.utils.Null;

import java.io.FileNotFoundException;
import java.io.IOException;

/** Android implementation of the VideoPlayer class.
*
* @author Rob Bogie <[email protected]> */
public class VideoPlayerAndroid implements VideoPlayer, OnFrameAvailableListener {
public class VideoPlayerAndroid extends AbstractVideoPlayer implements VideoPlayer, OnFrameAvailableListener {
//@off
String vertexShaderCode =
"#define highp\n" +
Expand Down Expand Up @@ -78,7 +76,6 @@ public class VideoPlayerAndroid implements VideoPlayer, OnFrameAvailableListener
private final int[] textures = new int[1];
private SurfaceTexture videoTexture;
private FrameBuffer fbo;
private Texture frame;
private ImmediateModeRenderer20 renderer;

private MediaPlayer player;
Expand Down Expand Up @@ -231,20 +228,15 @@ public boolean update () {
renderer.vertex(1, 1, 0);
renderer.end();
fbo.end();
frame = fbo.getColorBufferTexture();
texture = fbo.getColorBufferTexture();
texture.setFilter(minFilter, magFilter);
}
return true;
}
}
return false;
}

@Override
@Null
public Texture getTexture () {
return frame;
}

/** For android, this will return whether the prepareAsync method of the android MediaPlayer is done with preparing.
*
* @return whether the buffer is filled. */
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,15 +25,13 @@
import com.badlogic.gdx.graphics.GL20;
import com.badlogic.gdx.graphics.Pixmap.Format;
import com.badlogic.gdx.graphics.Texture;
import com.badlogic.gdx.utils.Null;
import com.badlogic.gdx.video.VideoDecoder.VideoDecoderBuffers;

/** Desktop implementation of the VideoPlayer
*
* @author Rob Bogie [email protected] */
abstract public class CommonVideoPlayerDesktop implements VideoPlayer {
abstract public class CommonVideoPlayerDesktop extends AbstractVideoPlayer implements VideoPlayer {
VideoDecoder decoder;
Texture texture;
Music audio;
long startTime = 0;
boolean showAlreadyDecodedFrame = false;
Expand Down Expand Up @@ -135,7 +133,10 @@ public boolean update () {
if (!showAlreadyDecodedFrame) {
ByteBuffer videoData = decoder.nextVideoFrame();
if (videoData != null) {
if (texture == null) texture = new Texture(getTextureWidth(), getTextureHeight(), Format.RGB888);
if (texture == null) {
texture = new Texture(getTextureWidth(), getTextureHeight(), Format.RGB888);
texture.setFilter(minFilter, magFilter);
}
texture.bind();
Gdx.gl.glTexImage2D(GL20.GL_TEXTURE_2D, 0, GL20.GL_RGB, getTextureWidth(), getTextureHeight(), 0, GL20.GL_RGB,
GL20.GL_UNSIGNED_BYTE, videoData);
Expand Down Expand Up @@ -168,12 +169,6 @@ public boolean update () {
return false;
}

@Override
@Null
public Texture getTexture () {
return texture;
}

/** Will return whether the buffer is filled. At the time of writing, the buffer used can store 10 frames of video. You can
* find the value in jni/VideoDecoder.h
*
Expand Down
25 changes: 10 additions & 15 deletions gdx-video-gwt/src/com/badlogic/gdx/video/VideoPlayerGwt.java
Original file line number Diff line number Diff line change
Expand Up @@ -21,13 +21,11 @@
import com.badlogic.gdx.backends.gwt.GwtGL20;
import com.badlogic.gdx.files.FileHandle;
import com.badlogic.gdx.graphics.*;
import com.badlogic.gdx.utils.Null;
import com.google.gwt.media.client.Video;

public class VideoPlayerGwt implements VideoPlayer {
public class VideoPlayerGwt extends AbstractVideoPlayer implements VideoPlayer {
private FileHandle currentFile;
private final Video v = Video.createIfSupported();
private Texture frame;
private int width, height;
private VideoSizeListener videoSizeListener;

Expand Down Expand Up @@ -55,12 +53,15 @@ public boolean update () {
if (videoSizeListener != null) videoSizeListener.onVideoSize(width, height);
}
if ((!v.isPaused() || v.getCurrentTime() == 0) && isBuffered() && width * height > 0) {
if (frame != null && (frame.getWidth() != width || frame.getHeight() != height)) {
frame.dispose();
frame = null;
if (texture != null && (texture.getWidth() != width || texture.getHeight() != height)) {
texture.dispose();
texture = null;
}
if (frame == null) frame = new Texture(width, height, Pixmap.Format.RGB888);
frame.bind();
if (texture == null) {
texture = new Texture(width, height, Pixmap.Format.RGB888);
texture.setFilter(minFilter, magFilter);
}
texture.bind();
((GwtGL20)Gdx.gl).gl.texImage2D(GL20.GL_TEXTURE_2D, 0, GL20.GL_RGB, GL20.GL_RGB, GL20.GL_UNSIGNED_BYTE,
v.getVideoElement());
return true;
Expand All @@ -69,12 +70,6 @@ public boolean update () {
return false;
}

@Override
@Null
public Texture getTexture () {
return frame;
}

@Override
public boolean isBuffered () {
return v != null && v.getBuffered().length() > 0;
Expand Down Expand Up @@ -142,7 +137,7 @@ public int getCurrentTimestamp () {

@Override
public void dispose () {
if (frame != null) frame.dispose();
if (texture != null) texture.dispose();
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@
/** iOS implementation of the VideoPlayer class.
*
* @author Maximilian Wende <[email protected]> */
public class VideoPlayerIos implements VideoPlayer {
public class VideoPlayerIos extends AbstractVideoPlayer implements VideoPlayer {

protected FileHandle file;
protected AVAsset asset;
Expand All @@ -74,7 +74,6 @@ public class VideoPlayerIos implements VideoPlayer {
CMVideoFormatDescription videoFormat;
CMVideoDimensions videoDimensions;
AVPlayerItemVideoOutput videoOutput;
private Texture texture;

/* --- Audio --- */
AVAssetTrack audioTrack;
Expand Down Expand Up @@ -203,6 +202,7 @@ protected void updateTextureFromBuffer (CVImageBuffer buffer) {
int texHeight = (int)(pixelBuffer.getDataSize() / bpr);
if (texture == null) {
texture = new Texture(texWidth, texHeight, Pixmap.Format.RGB888);
texture.setFilter(minFilter, magFilter);
}
texture.bind();
pixelBuffer.lockBaseAddress(CVPixelBufferLockFlags.ReadOnly);
Expand All @@ -224,11 +224,6 @@ public boolean update () {
return true;
}

@Override
public Texture getTexture () {
return texture;
}

@Override
public boolean isBuffered () {
return playerIsPrerolled;
Expand Down
42 changes: 42 additions & 0 deletions gdx-video/src/com/badlogic/gdx/video/AbstractVideoPlayer.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
/*******************************************************************************
* Copyright 2023 See AUTHORS file.
*
* 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 com.badlogic.gdx.video;

import com.badlogic.gdx.graphics.Texture;
import com.badlogic.gdx.graphics.Texture.TextureFilter;
import com.badlogic.gdx.utils.Null;

public abstract class AbstractVideoPlayer implements VideoPlayer {
protected Texture texture;
protected TextureFilter minFilter = TextureFilter.Linear;
protected TextureFilter magFilter = TextureFilter.Linear;

@Null
@Override
public final Texture getTexture () {
return texture;
}

@Override
public void setFilter (TextureFilter minFilter, TextureFilter magFilter) {
if (this.minFilter == minFilter && this.magFilter == magFilter) return;
this.minFilter = minFilter;
this.magFilter = magFilter;
if (texture == null) return;
texture.setFilter(minFilter, magFilter);
}
}
5 changes: 5 additions & 0 deletions gdx-video/src/com/badlogic/gdx/video/VideoPlayer.java
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@

import com.badlogic.gdx.files.FileHandle;
import com.badlogic.gdx.graphics.Texture;
import com.badlogic.gdx.graphics.Texture.TextureFilter;
import com.badlogic.gdx.utils.Disposable;
import com.badlogic.gdx.utils.Null;

Expand Down Expand Up @@ -124,4 +125,8 @@ interface CompletionListener {
void setLooping (boolean looping);

boolean isLooping ();

/** This sets the texture filtering used when displaying the video on screen.
* @see Texture#setFilter(TextureFilter minFilter, TextureFilter magFilter) */
void setFilter (TextureFilter minFilter, TextureFilter magFilter);
}
6 changes: 6 additions & 0 deletions gdx-video/src/com/badlogic/gdx/video/VideoPlayerStub.java
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@

import com.badlogic.gdx.files.FileHandle;
import com.badlogic.gdx.graphics.Texture;
import com.badlogic.gdx.graphics.Texture.TextureFilter;
import com.badlogic.gdx.utils.Null;

class VideoPlayerStub implements VideoPlayer {
Expand Down Expand Up @@ -107,4 +108,9 @@ public void setLooping (boolean looping) {
public boolean isLooping () {
return false;
}

@Override
public void setFilter (TextureFilter minFilter, TextureFilter magFilter) {

}
}

0 comments on commit e543296

Please sign in to comment.