Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /**
- * 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 com.jogamp.opengl.test.junit.jogl.demos.es2.av;
- import java.io.File;
- import java.io.IOException;
- import java.net.URI;
- import java.net.URISyntaxException;
- import java.nio.FloatBuffer;
- import com.jogamp.common.net.Uri;
- import com.jogamp.common.os.Platform;
- import com.jogamp.newt.opengl.GLWindow;
- import com.jogamp.opengl.GL;
- import com.jogamp.opengl.GL2ES2;
- import com.jogamp.opengl.GLAutoDrawable;
- import com.jogamp.opengl.GLCapabilities;
- import com.jogamp.opengl.GLES2;
- import com.jogamp.opengl.GLEventListener;
- import com.jogamp.opengl.GLException;
- import com.jogamp.opengl.GLExtensions;
- import com.jogamp.opengl.GLProfile;
- import com.jogamp.opengl.GLUniformData;
- import com.jogamp.opengl.fixedfunc.GLMatrixFunc;
- import com.jogamp.opengl.util.FPSAnimator;
- import com.jogamp.opengl.util.GLArrayDataServer;
- import com.jogamp.opengl.util.PMVMatrix;
- import com.jogamp.opengl.util.av.GLMediaPlayer;
- import com.jogamp.opengl.util.av.GLMediaPlayerFactory;
- import com.jogamp.opengl.util.glsl.ShaderCode;
- import com.jogamp.opengl.util.glsl.ShaderProgram;
- import com.jogamp.opengl.util.glsl.ShaderState;
- import com.jogamp.opengl.util.texture.Texture;
- import com.jogamp.opengl.util.texture.TextureCoords;
- import com.jogamp.opengl.util.texture.TextureSequence;
- import com.jogamp.opengl.util.texture.TextureSequence.TextureFrame;
- /**
- * Simple planar movie player w/ orthogonal 1:1 projection.
- */
- public class MovieSimpleDeadlock implements GLEventListener {
- public static final int EFFECT_NORMAL = 0;
- public static final int EFFECT_GRADIENT_BOTTOM2TOP = 1<<1;
- public static final int EFFECT_TRANSPARENT = 1<<3;
- public static final String WINDOW_KEY = "window";
- public static final String PLAYER = "player";
- private long startTime;
- private int effects = EFFECT_NORMAL;
- private float alpha = 1.0f;
- private GLMediaPlayer mPlayer;
- private boolean mPlayerScaleOrig;
- private float[] verts = null;
- private GLArrayDataServer interleavedVBO;
- private volatile boolean resetGLState = false;
- private ShaderState st;
- private PMVMatrix pmvMatrix;
- private GLUniformData pmvMatrixUniform;
- private static final String shaderBasename = "texsequence_xxx";
- private static final String myTextureLookupName = "myTexture2D";
- /** Blender's Big Buck Bunny: 24f 416p H.264, AAC 48000 Hz, 2 ch, mpeg stream. */
- public static final Uri defURI;
- static {
- Uri _defURI = null;
- try {
- // Blender's Big Buck Bunny Trailer: 24f 640p VP8, Vorbis 44100Hz mono, WebM/Matroska Stream.
- // _defURI = new URI("http://video.webmfiles.org/big-buck-bunny_trailer.webm");
- // _defURI = Uri.cast("http://archive.org/download/BigBuckBunny_328/BigBuckBunny_512kb.mp4");
- _defURI = Uri.valueOf(new File("E:\\Videos\\Indy Car Edmonton.mp4"));
- } catch (final URISyntaxException e) {
- e.printStackTrace();
- }
- defURI = _defURI;
- }
- /** Custom constructor, user needs to issue {@link #initStream(URI, int, int, int)} afterwards. */
- public MovieSimpleDeadlock(final GLMediaPlayer sharedMediaPlayer) throws IllegalStateException {
- mPlayer = sharedMediaPlayer;
- mPlayerScaleOrig = false;
- mPlayer = GLMediaPlayerFactory.createDefault();
- mPlayer.attachObject(PLAYER, this);
- }
- public void initStream(final Uri streamLoc, final int vid, final int aid, final int textureCount) {
- mPlayer.initStream(streamLoc, vid, aid, textureCount);
- }
- public GLMediaPlayer getGLMediaPlayer() { return mPlayer; }
- public void setScaleOrig(final boolean v) {
- mPlayerScaleOrig = v;
- }
- public boolean hasEffect(final int e) { return 0 != ( effects & e ) ; }
- public void setEffects(final int e) { effects = e; };
- public void setTransparency(final float alpha) {
- this.effects |= EFFECT_TRANSPARENT;
- this.alpha = alpha;
- }
- public void resetGLState() {
- resetGLState = true;
- }
- private void initShader(final GL2ES2 gl) {
- // Create & Compile the shader objects
- final ShaderCode rsVp = ShaderCode.create(gl, GL2ES2.GL_VERTEX_SHADER, MovieSimpleDeadlock.class,
- "../shader", "../shader/bin", shaderBasename, true);
- final ShaderCode rsFp = ShaderCode.create(gl, GL2ES2.GL_FRAGMENT_SHADER, MovieSimpleDeadlock.class,
- "../shader", "../shader/bin", shaderBasename, true);
- boolean preludeGLSLVersion = true;
- if( GLES2.GL_TEXTURE_EXTERNAL_OES == mPlayer.getTextureTarget() ) {
- if( !gl.isExtensionAvailable(GLExtensions.OES_EGL_image_external) ) {
- throw new GLException(GLExtensions.OES_EGL_image_external+" requested but not available");
- }
- if( Platform.OSType.ANDROID == Platform.getOSType() && gl.isGLES3() ) {
- // Bug on Nexus 10, ES3 - Android 4.3, where
- // GL_OES_EGL_image_external extension directive leads to a failure _with_ '#version 300 es' !
- // P0003: Extension 'GL_OES_EGL_image_external' not supported
- preludeGLSLVersion = false;
- }
- }
- rsVp.defaultShaderCustomization(gl, preludeGLSLVersion, true);
- int rsFpPos = preludeGLSLVersion ? rsFp.addGLSLVersion(gl) : 0;
- rsFpPos = rsFp.insertShaderSource(0, rsFpPos, mPlayer.getRequiredExtensionsShaderStub());
- rsFp.addDefaultShaderPrecision(gl, rsFpPos);
- final String texLookupFuncName = mPlayer.getTextureLookupFunctionName(myTextureLookupName);
- rsFp.replaceInShaderSource(myTextureLookupName, texLookupFuncName);
- // Inject TextureSequence shader details
- final StringBuilder sFpIns = new StringBuilder();
- sFpIns.append("uniform ").append(mPlayer.getTextureSampler2DType()).append(" mgl_ActiveTexture;\n");
- sFpIns.append(mPlayer.getTextureLookupFragmentShaderImpl());
- rsFp.insertShaderSource(0, "TEXTURE-SEQUENCE-CODE-BEGIN", 0, sFpIns);
- // Create & Link the shader program
- final ShaderProgram sp = new ShaderProgram();
- sp.add(rsVp);
- sp.add(rsFp);
- if(!sp.link(gl, System.err)) {
- throw new GLException("Couldn't link program: "+sp);
- }
- // Let's manage all our states using ShaderState.
- st = new ShaderState();
- st.attachShaderProgram(gl, sp, false);
- }
- @Override
- public void init(final GLAutoDrawable drawable) {
- if(null == mPlayer) {
- throw new InternalError("mPlayer null");
- }
- if( GLMediaPlayer.State.Uninitialized == mPlayer.getState() ) {
- throw new IllegalStateException("mPlayer in uninitialized state: "+mPlayer);
- }
- final boolean hasVideo = GLMediaPlayer.STREAM_ID_NONE != mPlayer.getVID();
- resetGLState = false;
- final GL2ES2 gl = drawable.getGL().getGL2ES2();
- // if(waitForKey) {
- // UITestCase.waitForKey("Init>");
- // }
- final Texture tex;
- try {
- if(GLMediaPlayer.State.Initialized == mPlayer.getState() ) {
- mPlayer.initGL(gl);
- }
- final TextureFrame frame = mPlayer.getLastTexture();
- if( null != frame ) {
- if( !hasVideo ) {
- throw new InternalError("XXX: "+mPlayer);
- }
- tex = frame.getTexture();
- if( null == tex ) {
- throw new InternalError("XXX: "+mPlayer);
- }
- } else {
- tex = null;
- if( hasVideo ) {
- throw new InternalError("XXX: "+mPlayer);
- }
- }
- mPlayer.setTextureMinMagFilter( new int[] { GL.GL_NEAREST, GL.GL_LINEAR } );
- } catch (final Exception glex) {
- glex.printStackTrace();
- if(null != mPlayer) {
- mPlayer.destroy(gl);
- mPlayer = null;
- }
- throw new GLException(glex);
- }
- if( hasVideo ) {
- initShader(gl);
- // Push the 1st uniform down the path
- st.useProgram(gl, true);
- final int[] viewPort = new int[] { 0, 0, drawable.getSurfaceWidth(), drawable.getSurfaceHeight()};
- pmvMatrix = new PMVMatrix();
- reshapePMV(viewPort[2], viewPort[3]);
- pmvMatrixUniform = new GLUniformData("mgl_PMVMatrix", 4, 4, pmvMatrix.glGetPMvMatrixf());
- if(!st.uniform(gl, pmvMatrixUniform)) {
- throw new GLException("Error setting PMVMatrix in shader: "+st);
- }
- if(!st.uniform(gl, new GLUniformData("mgl_ActiveTexture", mPlayer.getTextureUnit()))) {
- throw new GLException("Error setting mgl_ActiveTexture in shader: "+st);
- }
- final float dWidth = drawable.getSurfaceWidth();
- final float dHeight = drawable.getSurfaceHeight();
- final float mWidth = mPlayer.getWidth();
- final float mHeight = mPlayer.getHeight();
- final float mAspect = mWidth/mHeight;
- float xs, ys;
- if(mPlayerScaleOrig && mWidth < dWidth && mHeight < dHeight) {
- xs = mWidth/2f; ys = xs / mAspect;
- } else {
- xs = dWidth/2f; ys = xs / mAspect; // w>h
- }
- verts = new float[] { -1f*xs, -1f*ys, 0f, // LB
- 1f*xs, 1f*ys, 0f // RT
- };
- {
- final float[] winLB = new float[3];
- final float[] winRT = new float[3];
- pmvMatrix.gluProject(verts[0], verts[1], verts[2], viewPort, 0, winLB, 0);
- pmvMatrix.gluProject(verts[3], verts[4], verts[5], viewPort, 0, winRT, 0);
- }
- interleavedVBO = GLArrayDataServer.createGLSLInterleaved(3+4+2, GL.GL_FLOAT, false, 3*4, GL.GL_STATIC_DRAW);
- {
- interleavedVBO.addGLSLSubArray("mgl_Vertex", 3, GL.GL_ARRAY_BUFFER);
- interleavedVBO.addGLSLSubArray("mgl_Color", 4, GL.GL_ARRAY_BUFFER);
- interleavedVBO.addGLSLSubArray("mgl_MultiTexCoord", 2, GL.GL_ARRAY_BUFFER);
- }
- updateInterleavedVBO(gl, tex);
- st.ownAttribute(interleavedVBO, true);
- gl.glClearColor(0.3f, 0.3f, 0.3f, 0.3f);
- gl.glEnable(GL.GL_DEPTH_TEST);
- st.useProgram(gl, false);
- // Let's show the completed shader state ..
- }
- mPlayer.play();
- startTime = System.currentTimeMillis();
- }
- protected void updateInterleavedVBO(final GL gl, final Texture tex) {
- final float ss = 1f, ts = 1f; // scale tex-coord
- final boolean wasEnabled = interleavedVBO.enabled();
- interleavedVBO.seal(gl, false);
- interleavedVBO.rewind();
- {
- final FloatBuffer ib = (FloatBuffer)interleavedVBO.getBuffer();
- final TextureCoords tc = tex.getImageTexCoords();
- // left-bottom
- ib.put(verts[0]); ib.put(verts[1]); ib.put(verts[2]);
- if( hasEffect(EFFECT_GRADIENT_BOTTOM2TOP) ) {
- ib.put( 0); ib.put( 0); ib.put( 0); ib.put(alpha);
- } else {
- ib.put( 1); ib.put( 1); ib.put( 1); ib.put(alpha);
- }
- ib.put( tc.left() *ss); ib.put( tc.bottom() *ts);
- // right-bottom
- ib.put(verts[3]); ib.put(verts[1]); ib.put(verts[2]);
- if( hasEffect(EFFECT_GRADIENT_BOTTOM2TOP) ) {
- ib.put( 0); ib.put( 0); ib.put( 0); ib.put(alpha);
- } else {
- ib.put( 1); ib.put( 1); ib.put( 1); ib.put(alpha);
- }
- ib.put( tc.right() *ss); ib.put( tc.bottom() *ts);
- // left-top
- ib.put(verts[0]); ib.put(verts[4]); ib.put(verts[2]);
- ib.put( 1); ib.put( 1); ib.put( 1); ib.put(alpha);
- ib.put( tc.left() *ss); ib.put( tc.top() *ts);
- // right-top
- ib.put(verts[3]); ib.put(verts[4]); ib.put(verts[2]);
- ib.put( 1); ib.put( 1); ib.put( 1); ib.put(alpha);
- ib.put( tc.right() *ss); ib.put( tc.top() *ts);
- }
- interleavedVBO.seal(gl, true);
- if( !wasEnabled ) {
- interleavedVBO.enableBuffer(gl, false);
- }
- }
- @Override
- public void reshape(final GLAutoDrawable drawable, final int x, final int y, final int width, final int height) {
- final GL2ES2 gl = drawable.getGL().getGL2ES2();
- if(null == mPlayer) { return; }
- if(null != st) {
- reshapePMV(width, height);
- st.useProgram(gl, true);
- st.uniform(gl, pmvMatrixUniform);
- st.useProgram(gl, false);
- }
- }
- private void reshapePMV(final int width, final int height) {
- pmvMatrix.glMatrixMode(GLMatrixFunc.GL_PROJECTION);
- pmvMatrix.glLoadIdentity();
- final float fw = width / 2f;
- final float fh = height/ 2f;
- pmvMatrix.glOrthof(-fw, fw, -fh, fh, -1.0f, 1.0f);
- pmvMatrix.glMatrixMode(GLMatrixFunc.GL_MODELVIEW);
- pmvMatrix.glLoadIdentity();
- pmvMatrix.glTranslatef(0, 0, 0);
- }
- @Override
- public void dispose(final GLAutoDrawable drawable) {
- disposeImpl(drawable, true);
- }
- private void disposeImpl(final GLAutoDrawable drawable, final boolean disposePlayer) {
- if(null == mPlayer) { return; }
- final GL2ES2 gl = drawable.getGL().getGL2ES2();
- if( disposePlayer ) {
- mPlayer.destroy(gl);
- mPlayer=null;
- }
- pmvMatrixUniform = null;
- if(null != pmvMatrix) {
- pmvMatrix=null;
- }
- if(null != st) {
- st.destroy(gl);
- st=null;
- }
- }
- long lastPerfPos = 0;
- @Override
- public void display(final GLAutoDrawable drawable) {
- final GL2ES2 gl = drawable.getGL().getGL2ES2();
- if(null == mPlayer) { return; }
- if( resetGLState ) {
- resetGLState = false;
- disposeImpl(drawable, false);
- init(drawable);
- reshape(drawable, 0, 0, drawable.getSurfaceWidth(), drawable.getSurfaceHeight());
- }
- final long currentPos = System.currentTimeMillis();
- if( currentPos - lastPerfPos > 2000 ) {
- lastPerfPos = currentPos;
- }
- gl.glClear(GL.GL_COLOR_BUFFER_BIT | GL.GL_DEPTH_BUFFER_BIT);
- if(null == st) {
- return;
- }
- st.useProgram(gl, true);
- pmvMatrix.glMatrixMode(GLMatrixFunc.GL_MODELVIEW);
- pmvMatrix.glLoadIdentity();
- pmvMatrix.glTranslatef(0, 0, 0);
- st.uniform(gl, pmvMatrixUniform);
- interleavedVBO.enableBuffer(gl, true);
- Texture tex = null;
- if(null!=mPlayer) {
- final TextureSequence.TextureFrame texFrame;
- texFrame=mPlayer.getNextTexture(gl);
- if(null != texFrame) {
- tex = texFrame.getTexture();
- gl.glActiveTexture(GL.GL_TEXTURE0+mPlayer.getTextureUnit());
- tex.enable(gl);
- tex.bind(gl);
- }
- }
- gl.glDrawArrays(GL.GL_TRIANGLE_STRIP, 0, 4);
- if(null != tex) {
- tex.disable(gl);
- }
- interleavedVBO.enableBuffer(gl, false);
- st.useProgram(gl, false);
- }
- static boolean loopEOS = false;
- static boolean origSize;
- public static void main(final String[] args) throws IOException, URISyntaxException {
- int width = 320;
- int height = 240;
- int[] positions = {0,100, 400,100, 0,400, 400,400};
- int textureCount = 1;
- int vid = GLMediaPlayer.STREAM_ID_AUTO;
- int aid = GLMediaPlayer.STREAM_ID_NONE;
- final int windowCount = 4;
- final GLProfile glp = GLProfile.get(GLProfile.GL2ES2);
- final GLCapabilities caps = new GLCapabilities(glp);
- caps.setBackgroundOpaque(true);
- caps.setDoubleBuffered(true);
- caps.setAlphaBits(4);
- caps.setSampleBuffers(true);
- caps.setNumSamples(4);
- final MovieSimpleDeadlock[] mss = new MovieSimpleDeadlock[windowCount];
- final GLWindow[] windows = new GLWindow[windowCount];
- for(int i=0; i<windowCount; i++) {
- mss[i] = new MovieSimpleDeadlock(null);
- mss[i].initStream(defURI, vid, aid, textureCount);
- System.out.println("Init stream " + System.currentTimeMillis());
- // windows[i] = GLWindow.create(caps);
- // windows[i].setTitle("Player "+i);
- // windows[i].setSize(width, height);
- // if (i*2 < positions.length) {
- // windows[i].setPosition(positions[i*2], positions[i*2+1]);
- // }
- }
- for(int i=0; i<windowCount; i++) {
- // windows[i].setVisible(true);
- // windows[i].addGLEventListener(mss[i]);
- // final FPSAnimator anim = new FPSAnimator(windows[i], 60);
- // anim.start();
- }
- while (true) {
- for(int i=0; i<windowCount; i++) {
- System.out.println(mss[i].mPlayer.getState());
- }
- }
- }
- }
Add Comment
Please, Sign In to add comment