/** * Copyright 2012 JogAmp Community. All rights reserved. * * Redistribution and use in source and binary forms, with or without modification, are * permitted provided that the following conditions are met: * * 1. Redistributions of source code must retain the above copyright notice, this list of * conditions and the following disclaimer. * * 2. Redistributions in binary form must reproduce the above copyright notice, this list * of conditions and the following disclaimer in the documentation and/or other materials * provided with the distribution. * * THIS SOFTWARE IS PROVIDED BY JogAmp Community ``AS IS'' AND ANY EXPRESS OR IMPLIED * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND * FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL JogAmp Community OR * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON * ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. * * The views and conclusions contained in the software and documentation are those of the * authors and should not be interpreted as representing official policies, either expressed * or implied, of JogAmp Community. */ package jogamp.opengl.util.av; import java.io.IOException; import java.net.URLConnection; import java.util.ArrayList; import java.util.HashMap; import java.util.Iterator; import javax.media.opengl.GL; import javax.media.opengl.GL2; import javax.media.opengl.GLES2; import javax.media.opengl.GLException; import com.jogamp.opengl.util.av.GLMediaPlayer; import com.jogamp.opengl.util.texture.Texture; import com.jogamp.opengl.util.texture.TextureSequence; /** * After object creation an implementation may customize the behavior: *
* See {@link GLMediaPlayer}. *
*/ public abstract class GLMediaPlayerImpl implements GLMediaPlayer { protected static final String unknown = "unknown"; protected State state; protected int textureCount; protected int textureTarget; protected int textureFormat; protected int textureInternalFormat; protected int textureType; protected int texUnit; protected int[] texMinMagFilter = { GL.GL_NEAREST, GL.GL_NEAREST }; protected int[] texWrapST = { GL.GL_CLAMP_TO_EDGE, GL.GL_CLAMP_TO_EDGE }; protected URLConnection urlConn = null; protected float playSpeed = 1.0f; /** Shall be set by the {@link #initGLStreamImpl(GL, int[])} method implementation. */ protected int width = 0; /** Shall be set by the {@link #initGLStreamImpl(GL, int[])} method implementation. */ protected int height = 0; /** Video fps. Shall be set by the {@link #initGLStreamImpl(GL, int[])} method implementation. */ protected float fps = 0; /** Stream bps. Shall be set by the {@link #initGLStreamImpl(GL, int[])} method implementation. */ protected int bps_stream = 0; /** Video bps. Shall be set by the {@link #initGLStreamImpl(GL, int[])} method implementation. */ protected int bps_video = 0; /** Audio bps. Shall be set by the {@link #initGLStreamImpl(GL, int[])} method implementation. */ protected int bps_audio = 0; /** In frames. Shall be set by the {@link #initGLStreamImpl(GL, int[])} method implementation. */ protected int totalFrames = 0; /** In ms. Shall be set by the {@link #initGLStreamImpl(GL, int[])} method implementation. */ protected int duration = 0; /** Shall be set by the {@link #initGLStreamImpl(GL, int[])} method implementation. */ protected String acodec = unknown; /** Shall be set by the {@link #initGLStreamImpl(GL, int[])} method implementation. */ protected String vcodec = unknown; protected int frameNumber = 0; protected TextureSequence.TextureFrame[] texFrames = null; protected HashMaptexture2D
,
* if not overridden by specialization.
*/
@Override
public String getTextureLookupFunctionName(String desiredFuncName) throws IllegalStateException {
if(State.Uninitialized == state) {
throw new IllegalStateException("Instance not initialized: "+this);
}
return "texture2D";
}
/**
* {@inheritDoc}
*
* This implementation simply returns an empty string since it's using
* the build-in function texture2D
,
* if not overridden by specialization.
*/
@Override
public String getTextureLookupFragmentShaderImpl() throws IllegalStateException {
if(State.Uninitialized == state) {
throw new IllegalStateException("Instance not initialized: "+this);
}
return "";
}
@Override
public final synchronized float getPlaySpeed() {
return playSpeed;
}
@Override
public final synchronized void setPlaySpeed(float rate) {
if(State.Uninitialized != state && setPlaySpeedImpl(rate)) {
playSpeed = rate;
}
if(DEBUG) { System.err.println("SetPlaySpeed: "+toString()); }
}
protected abstract boolean setPlaySpeedImpl(float rate);
public final State start() {
switch(state) {
case Stopped:
case Paused:
if(startImpl()) {
state = State.Playing;
}
}
if(DEBUG) { System.err.println("Start: "+toString()); }
return state;
}
protected abstract boolean startImpl();
public final State pause() {
if(State.Playing == state && pauseImpl()) {
state = State.Paused;
}
if(DEBUG) { System.err.println("Pause: "+toString()); }
return state;
}
protected abstract boolean pauseImpl();
public final State stop() {
switch(state) {
case Playing:
case Paused:
if(stopImpl()) {
state = State.Stopped;
}
}
if(DEBUG) { System.err.println("Stop: "+toString()); }
return state;
}
protected abstract boolean stopImpl();
@Override
public final int getCurrentPosition() {
if(State.Uninitialized != state) {
return getCurrentPositionImpl();
}
return 0;
}
protected abstract int getCurrentPositionImpl();
public final int seek(int msec) {
final int cp;
switch(state) {
case Stopped:
case Playing:
case Paused:
cp = seekImpl(msec);
break;
default:
cp = 0;
}
if(DEBUG) { System.err.println("Seek("+msec+"): "+toString()); }
return cp;
}
protected abstract int seekImpl(int msec);
public final State getState() { return state; }
@Override
public final State initGLStream(GL gl, URLConnection urlConn) throws IllegalStateException, GLException, IOException {
if(State.Uninitialized != state) {
throw new IllegalStateException("Instance not in state "+State.Uninitialized+", but "+state+", "+this);
}
this.urlConn = urlConn;
if (this.urlConn != null) {
try {
if(null != gl) {
if(null!=texFrames) {
// re-init ..
removeAllImageTextures(gl);
} else {
texFrames = new TextureSequence.TextureFrame[textureCount];
}
final int[] tex = new int[textureCount];
{
gl.glGenTextures(textureCount, tex, 0);
final int err = gl.glGetError();
if( GL.GL_NO_ERROR != err ) {
throw new RuntimeException("TextureNames creation failed (num: "+textureCount+"): err "+toHexString(err));
}
}
initGLStreamImpl(gl, tex);
for(int i=0; i