29package com.jogamp.opengl.demos.av;
32import java.io.IOException;
33import java.net.URISyntaxException;
35import com.jogamp.opengl.GL2ES2;
36import com.jogamp.opengl.GLAnimatorControl;
37import com.jogamp.opengl.GLAutoDrawable;
38import com.jogamp.opengl.GLCapabilities;
39import com.jogamp.opengl.GLEventListener;
40import com.jogamp.opengl.GLException;
41import com.jogamp.opengl.GLProfile;
42import com.jogamp.opengl.GLRunnable;
43import com.jogamp.common.net.Uri;
44import com.jogamp.common.os.Clock;
45import com.jogamp.common.util.InterruptSource;
46import com.jogamp.graph.curve.Region;
47import com.jogamp.graph.curve.opengl.GLRegion;
48import com.jogamp.graph.curve.opengl.RegionRenderer;
49import com.jogamp.graph.font.Font;
50import com.jogamp.graph.font.FontScale;
51import com.jogamp.newt.Window;
52import com.jogamp.newt.event.KeyAdapter;
53import com.jogamp.newt.event.KeyEvent;
54import com.jogamp.newt.event.KeyListener;
55import com.jogamp.newt.event.WindowAdapter;
56import com.jogamp.newt.event.WindowEvent;
57import com.jogamp.newt.opengl.GLWindow;
58import com.jogamp.opengl.JoglVersion;
59import com.jogamp.opengl.demos.es2.TextureSequenceCubeES2;
60import com.jogamp.opengl.demos.graph.TextRendererGLELBase;
61import com.jogamp.opengl.demos.util.MiscUtils;
62import com.jogamp.opengl.util.Animator;
63import com.jogamp.opengl.util.GLReadBufferUtil;
64import com.jogamp.opengl.util.av.GLMediaPlayer;
65import com.jogamp.opengl.util.av.GLMediaPlayer.GLMediaEventListener;
66import com.jogamp.opengl.util.av.GLMediaPlayer.StreamException;
67import com.jogamp.opengl.util.av.GLMediaPlayerFactory;
68import com.jogamp.opengl.util.texture.TextureSequence.TextureFrame;
75 private static boolean waitForKey =
false;
76 private final float zoom0, rotx, roty;
79 private int swapInterval = 1;
80 private boolean swapIntervalSet =
true;
81 private long lastPerfPos = 0;
82 private volatile boolean resetGLState =
false;
92 _defURI = Uri.cast(
"http://archive.org/download/BigBuckBunny_328/BigBuckBunny_512kb.mp4");
93 }
catch (
final URISyntaxException e) {
107 public MovieCube() throws IOException, URISyntaxException {
113 System.err.println(
"MovieCube.0 AttributesChanges: "+eventMask+
", when "+when);
114 System.err.println(
"MovieCube.0 State: "+mp);
119 new InterruptSource.Thread() {
135 public MovieCube(
final float zoom0,
final float rotx,
final float roty,
final boolean showText)
throws IOException {
139 this.showText = showText;
144 public void playStream(
final Uri streamLoc,
final int vid,
final int aid,
final int textureCount) {
146 System.out.println(
"pC.1b "+mPlayer);
158 private static final float z_diff = 0.001f;
159 private final Font font = getFont(0, 0, 0);
160 private final float fontSize1 = 12;
161 private final float fontSize2 = 10;
163 private float pixelSize1, pixelSize2, underlineSize;
165 InfoTextRendererGLELBase(
final GLProfile glp,
final int rmode) {
171 staticRGBAColor[0] = 0.1f;
172 staticRGBAColor[1] = 0.1f;
173 staticRGBAColor[2] = 0.1f;
174 staticRGBAColor[3] = 1.0f;
181 super.init(drawable);
183 autoDrawable = drawable;
185 pixelSize1 = FontScale.toPixels(fontSize1, dpiH);
186 pixelSize2 = FontScale.toPixels(fontSize2, dpiH);
187 pixelScale = 1.0f / ( pixelSize1 * 20f );
189 final Font.Metrics metrics = font.
getMetrics();
190 final float lineGap = pixelSize1 * metrics.
getLineGap();
191 final float descent = pixelSize1 * metrics.getDescent();
192 underlineSize = lineGap - descent;
193 System.err.println(
"XXX: dpiH "+dpiH+
", fontSize "+fontSize1+
", pixelSize "+pixelSize1+
", pixelScale "+pixelScale+
", fLG "+lineGap+
", fDesc "+descent+
", underlineSize "+underlineSize);
199 screenshot.dispose(drawable.
getGL());
200 if(
null != regionFPS ) {
203 super.dispose(drawable);
206 String text1_old =
null;
209 public void display(
final GLAutoDrawable drawable) {
210 final GLAnimatorControl anim = drawable.getAnimator();
211 final float lfps =
null != anim ? anim.getLastFPS() : 0f;
212 final float tfps =
null != anim ? anim.getTotalFPS() : 0f;
213 final float pts = mPlayer.
getPTS().getCurrent() / 1000f;
219 final float aspect_h = 1f/aspect;
220 final float dy = 1f-aspect_h;
224 final float yoff1 = dy-(pixelScale*underlineSize);
227 final float yoff2 = 2f-dy;
234 final GL2ES2 gl = drawable.getGL().getGL2ES2();
235 final String ptsPrec =
null != regionFPS ?
"3.1" :
"3.0";
236 final String text1 = String.format(
"%0"+ptsPrec+
"f/%0"+ptsPrec+
"f s, %s (%01.2fx, vol %01.2f), a %01.2f, fps %02.1f -> %02.1f / %02.1f, swap %d",
239 aspect, mPlayer.
getFramerate(), lfps, tfps, drawable.getGL().getSwapInterval());
240 final String text2 = String.format(
"audio: id %d, kbps %d, codec %s",
242 final String text3 = String.format(
"video: id %d, kbps %d, codec %s",
244 final String text4 = mPlayer.
getUri().path.decode();
245 if( displayOSD &&
null != renderer ) {
246 gl.glClearColor(1.0f, 1.0f, 1.0f, 0.0f);
247 if( !text1.equals(text1_old) ) {
248 renderString(drawable, font, pixelSize1, text1, 1 , -1 , -1+z_diff, yoff1, 1f+z_diff, regionFPS.
clear(gl));
251 renderRegion(drawable, font, pixelSize1, 1 , -1 , -1+z_diff, yoff1, 1f+z_diff, regionFPS);
253 renderString(drawable, font, pixelSize2, text2, 1 , 0 , -1+z_diff, yoff2, 1f+z_diff,
true);
254 renderString(drawable, font, pixelSize2, text3, 1 , 1 , -1+z_diff, yoff2, 1f+z_diff,
true);
255 renderString(drawable, font, pixelSize2, text4, 1 , 2 , -1+z_diff, yoff2, 1f+z_diff,
true);
258 private InfoTextRendererGLELBase textRendererGLEL =
null;
259 final boolean showText;
260 private boolean displayOSD =
true;
263 final String filename = String.format(
"MovieCube-snap%02d-%03dx%03d.png", screenshot_num++, drawable.getSurfaceWidth(), drawable.getSurfaceHeight());
264 if(screenshot.
readPixels(drawable.getGL(),
false)) {
265 screenshot.
write(
new File(filename.toString()));
269 private int screenshot_num = 0;
279 }
catch (
final IOException e) {
289 public void keyReleased(
final KeyEvent e) {
293 System.err.println(
"MC "+e);
294 final int pts0 = mPlayer.
getPTS().getCurrent();
298 case KeyEvent.VK_V: {
299 switch(swapInterval) {
300 case 0: swapInterval = -1;
break;
301 case -1: swapInterval = 1;
break;
302 case 1: swapInterval = 0;
break;
303 default: swapInterval = 1;
break;
305 swapIntervalSet =
true;
308 case KeyEvent.VK_O: displayOSD = !displayOSD;
break;
309 case KeyEvent.VK_RIGHT: pts1 = pts0 + 1000;
break;
310 case KeyEvent.VK_UP: pts1 = pts0 + 10000;
break;
311 case KeyEvent.VK_PAGE_UP: pts1 = pts0 + 30000;
break;
312 case KeyEvent.VK_LEFT: pts1 = pts0 - 1000;
break;
313 case KeyEvent.VK_DOWN: pts1 = pts0 - 10000;
break;
314 case KeyEvent.VK_PAGE_DOWN: pts1 = pts0 - 30000;
break;
315 case KeyEvent.VK_HOME:
316 case KeyEvent.VK_BACK_SPACE: {
320 case KeyEvent.VK_SPACE: {
321 if( GLMediaPlayer.State.Paused == mPlayer.
getState() ) {
323 }
else if(GLMediaPlayer.State.Uninitialized == mPlayer.
getState()) {
324 playStream(mPlayer.
getUri(), GLMediaPlayer.STREAM_ID_AUTO, GLMediaPlayer.STREAM_ID_AUTO, 3 );
328 mPlayer.
pause(
false);
332 case KeyEvent.VK_MULTIPLY:
335 case KeyEvent.VK_SUBTRACT: {
344 case KeyEvent.VK_ADD: {
353 case KeyEvent.VK_M: {
355 if( audioVolume > 0.5f ) {
363 if(
null != autoDrawable) {
368 case KeyEvent.VK_ESCAPE:
370 if(
null != autoDrawable) {
371 MiscUtils.destroyWindow(autoDrawable);
384 if(
null == mPlayer) {
385 throw new InternalError(
"mPlayer null");
388 resetGLState =
false;
401 }
catch (
final Exception e) {
403 if(
null != mPlayer) {
411 System.out.println(
"play.0 "+mPlayer);
415 if (upstreamWidget instanceof
Window) {
419 }
else { added =
false; }
420 System.err.println(
"MC.init: kl-added "+added+
", "+drawable.getClass().getName());
424 textRendererGLEL =
new InfoTextRendererGLELBase(gl.
getGLProfile(), rmode);
431 if(
null == mPlayer) {
return; }
432 cube.
reshape(drawable, x, y, width, height);
437 System.err.println(Thread.currentThread()+
" MovieCube.dispose ... ");
438 disposeImpl(drawable,
true);
441 private void disposeImpl(
final GLAutoDrawable drawable,
final boolean disposePlayer) {
442 if(
null == mPlayer) {
return; }
444 if (upstreamWidget instanceof Window) {
445 final Window window = (Window) upstreamWidget;
446 window.removeKeyListener(keyAction);
449 if(
null != textRendererGLEL ) {
451 textRendererGLEL =
null;
453 if( disposePlayer ) {
464 if( swapIntervalSet ) {
466 final int _swapInterval = swapInterval;
470 System.err.println(
"Swap Interval: "+_swapInterval+
" -> "+swapInterval);
471 swapIntervalSet =
false;
473 if(
null == mPlayer) {
return; }
476 resetGLState =
false;
477 System.err.println(
"XXX resetGLState");
478 disposeImpl(drawable,
false);
483 final long currentPos = System.currentTimeMillis();
484 if( currentPos - lastPerfPos > 2000 ) {
486 lastPerfPos = currentPos;
491 public static void main(
final String[] args)
throws IOException, InterruptedException, URISyntaxException {
492 int swapInterval = 1;
497 boolean forceES2 =
false;
498 boolean forceES3 =
false;
499 boolean forceGL3 =
false;
500 boolean forceGLDef =
false;
503 final boolean origSize;
506 final String file_s=
null;
508 boolean _origSize =
false;
509 for(
int i=0; i<args.length; i++) {
510 if(args[i].equals(
"-vid")) {
513 }
else if(args[i].equals(
"-aid")) {
516 }
else if(args[i].equals(
"-width")) {
519 }
else if(args[i].equals(
"-height")) {
522 }
else if(args[i].equals(
"-osize")) {
524 }
else if(args[i].equals(
"-textureCount")) {
527 }
else if(args[i].equals(
"-url")) {
530 }
else if(args[i].equals(
"-es2")) {
532 }
else if(args[i].equals(
"-es3")) {
534 }
else if(args[i].equals(
"-gl3")) {
536 }
else if(args[i].equals(
"-gldef")) {
538 }
else if(args[i].equals(
"-vsync")) {
541 }
else if(args[i].equals(
"-wait")) {
545 origSize = _origSize;
547 Uri streamLoc =
null;
548 if(
null != url_s ) {
549 streamLoc = Uri.tryUriOrFile( url_s );
551 if(
null == streamLoc ) {
554 System.err.println(
"url_s "+url_s);
555 System.err.println(
"stream "+streamLoc);
556 System.err.println(
"vid "+vid+
", aid "+aid);
557 System.err.println(
"textureCount "+textureCount);
558 System.err.println(
"forceES2 "+forceES2);
559 System.err.println(
"forceES3 "+forceES3);
560 System.err.println(
"forceGL3 "+forceGL3);
561 System.err.println(
"forceGLDef "+forceGLDef);
562 System.err.println(
"swapInterval "+swapInterval);
570 }
else if(forceGL3) {
572 }
else if(forceES3) {
574 }
else if(forceES2) {
579 System.err.println(
"GLProfile: "+glp);
599 System.err.println(
"MovieCube.1 AttributesChanges: events_mask "+event_mask+
", when "+when);
600 System.err.println(
"MovieCube.1 State: "+mp);
615 new InterruptSource.Thread() {
626 se.printStackTrace();
628 new InterruptSource.Thread( () -> { window.
destroy(); } ).start();
632 mc.
playStream(streamLoc, vid, aid, textureCount);
Abstract Outline shape representation define the method an OutlineShape(s) is bound and rendered.
static String getRenderModeString(final int renderModes)
Returns a unique technical description string for renderModes as follows:
static final int DEFAULT_AA_SAMPLE_COUNT
Default pass2 AA sample count {@value} for Graph Region AA render-modes: VBAA_RENDERING_BIT or Region...
static final int VBAA_RENDERING_BIT
Rendering-Mode bit for Region.
A GLRegion is the OGL binding of one or more OutlineShapes Defined by its vertices and generated tria...
final GLRegion clear(final GL2ES2 gl)
Clears all buffers, i.e.
final void destroy(final GL2ES2 gl)
Delete and clear the associated OGL objects.
static GLRegion create(final GLProfile glp, int renderModes, final TextureSequence colorTexSeq, final int pass2TexUnit, final int initialVerticesCount, final int initialIndicesCount)
Create a GLRegion using the passed render mode.
static final GLCallback defaultBlendDisable
Default GL#GL_BLEND disable GLCallback, simply turning-off the GL#GL_BLEND state and turning-on depth...
static final GLCallback defaultBlendEnable
Default GL#GL_BLEND enable GLCallback, turning-off depth writing via GL#glDepthMask(boolean) if Rende...
final short getKeySymbol()
Returns the virtual key symbol reflecting the current keyboard layout.
NEWT Window events are provided for notification purposes ONLY.
An implementation of GLAutoDrawable and Window interface, using a delegated Window instance,...
final void setSize(final int width, final int height)
Sets the size of the window's client area in window units, excluding decorations.
final void setVisible(final boolean visible)
Calls setVisible(true, visible), i.e.
final void setSurfaceSize(final int pixelWidth, final int pixelHeight)
Sets the size of the window's surface in pixel units which claims the window's client area excluding ...
final void addWindowListener(final WindowListener l)
Appends the given com.jogamp.newt.event.WindowListener to the end of the list.
final void destroy()
Destroys all resources associated with this GLAutoDrawable, inclusive the GLContext.
static GLWindow create(final GLCapabilitiesImmutable caps)
Creates a new GLWindow attaching a new Window referencing a new default Screen and default Display wi...
Specifies a set of OpenGL capabilities.
A generic exception for OpenGL errors used throughout the binding as a substitute for RuntimeExceptio...
Specifies the the OpenGL profile.
static final String GLES3
The embedded OpenGL profile ES 3.x, with x >= 0.
static final String GL3
The desktop OpenGL core profile 3.x, with x >= 1.
static final String GLES2
The embedded OpenGL profile ES 2.x, with x >= 0.
static GLProfile getDefault(final AbstractGraphicsDevice device)
Returns a default GLProfile object, reflecting the best for the running platform.
static GLProfile get(final AbstractGraphicsDevice device, String profile)
Returns a GLProfile object.
static GLProfile getGL2ES2(final AbstractGraphicsDevice device)
Returns the GL2ES2 profile implementation, hence compatible w/ GL2ES2.
static StringBuilder getGLInfo(final GL gl, final StringBuilder sb)
Simple cube movie player w/ aspect ration true projection on a cube.
void reshape(final GLAutoDrawable drawable, final int x, final int y, final int width, final int height)
Called by the drawable during the first repaint after the component has been resized.
MovieCube(final float zoom0, final float rotx, final float roty, final boolean showText)
Custom constructor, user needs to issue playStream(URI, int, int, int, int) afterwards.
static void main(final String[] args)
void dispose(final GLAutoDrawable drawable)
Notifies the listener to perform the release of all OpenGL resources per GLContext,...
void setSwapInterval(final int v)
static final Uri defURI
Blender's Big Buck Bunny: 24f 416p H.264, AAC 48000 Hz, 2 ch, mpeg stream.
void printScreen(final GLAutoDrawable drawable)
void display(final GLAutoDrawable drawable)
Called by the drawable to initiate OpenGL rendering by the client.
GLMediaPlayer getGLMediaPlayer()
MovieCube()
Default constructor which also issues playStream(URI, int, int, int, int) w/ default values and polls...
static final float zoom_def
void playStream(final Uri streamLoc, final int vid, final int aid, final int textureCount)
void printScreenOnGLThread(final GLAutoDrawable drawable)
void init(final GLAutoDrawable drawable)
Called by the drawable immediately after the OpenGL context is initialized.
void reshape(final GLAutoDrawable drawable, final int x, final int y, final int width, final int height)
Called by the drawable during the first repaint after the component has been resized.
void dispose(final GLAutoDrawable drawable)
Notifies the listener to perform the release of all OpenGL resources per GLContext,...
void display(final GLAutoDrawable drawable)
Called by the drawable to initiate OpenGL rendering by the client.
void init(final GLAutoDrawable drawable)
Called by the drawable immediately after the OpenGL context is initialized.
void renderString(final GLAutoDrawable drawable, final Font font, final float pixelSize, final CharSequence text, final int column, final float tx, final float ty, final float tz, final boolean cacheRegion)
static void waitForKey(final String preMessage)
static int atoi(final String str, final int def)
final void resetFPSCounter()
Reset all performance counter (startTime, currentTime, frame number)
final void setUpdateFPSFrames(final int frames, final PrintStream out)
final synchronized boolean start()
Starts this animator, if not running.
final synchronized boolean stop()
Stops this animator.
Utility to read out the current FB to TextureData, optionally writing the data back to a texture obje...
void write(final File dest)
Write the TextureData filled by readPixels(GLAutoDrawable, boolean) to file.
boolean readPixels(final GL gl, final boolean mustFlipVertically)
Read the drawable's pixels to TextureData and Texture, if requested at construction.
float getLineGap()
Typographic line gap, a positive value.
Interface wrapper for font implementation.
Specifying NEWT's Window functionality:
void addKeyListener(KeyListener l)
Appends the given com.jogamp.newt.event.KeyListener to the end of the list.
void resetFPSCounter()
Reset all performance counter (startTime, currentTime, frame number)
A higher-level abstraction than GLDrawable which supplies an event based mechanism (GLEventListener) ...
boolean invoke(boolean wait, GLRunnable glRunnable)
Enqueues a one-shot GLRunnable, which will be executed within the next display() call after all regis...
GLAnimatorControl getAnimator()
GLEventListener disposeGLEventListener(GLEventListener listener, boolean remove)
Disposes the given listener via dispose(..) if it has been initialized and added to this queue.
GL getGL()
Returns the GL pipeline object this GLAutoDrawable uses.
Object getUpstreamWidget()
Method may return the upstream UI toolkit object holding this GLAutoDrawable instance,...
void addGLEventListener(GLEventListener listener)
Adds the given listener to the end of this drawable queue.
GL2ES2 getGL2ES2()
Casts this object to the GL2ES2 interface.
GLProfile getGLProfile()
Returns the GLProfile associated with this GL object.
void setSwapInterval(int interval)
Set the swap interval of the current context and attached onscreen GLDrawable.
int getSwapInterval()
Return the current swap interval.
boolean getSampleBuffers()
Returns whether sample buffers for full-scene antialiasing (FSAA) should be allocated for this drawab...
GLCapabilitiesImmutable getChosenGLCapabilities()
Fetches the GLCapabilitiesImmutable corresponding to the chosen OpenGL capabilities (pixel format / v...
int getSurfaceWidth()
Returns the width of this GLDrawable's surface client area in pixel units.
int getSurfaceHeight()
Returns the height of this GLDrawable's surface client area in pixel units.
Declares events which client code can use to manage OpenGL rendering into a GLAutoDrawable.