1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
|
package com.sun.javafx.media.video.openmax;
import javax.media.opengl.*;
import javax.media.opengl.glu.GLU;
import com.sun.opengl.util.texture.*;
import com.sun.opengl.impl.egl.EGL;
import com.sun.opengl.impl.egl.EGLContext;
import com.sun.opengl.impl.egl.EGLDrawable;
import com.sun.opengl.impl.egl.EGLExt;
import java.net.URL;
import java.nio.ByteBuffer;
import java.io.File;
import java.io.FileNotFoundException;
import java.util.*;
public class OMXMoviePlayerImpl /* extends MoviePlayerImpl */ {
static final public int PREVIOUS = -1;
static final public int NEXT = 1;
public boolean isTransparent() {
return false;
}
long totalFrames = 0;
int width = 0;
int height = 0;
float fps = 0.0f;
int pixelBytes = 0;
int pixelType = 0;
EGLExt eglExt = null;
long eglSurface=0;
long eglDisplay=0;
long eglContext=0;
int sWidth=0, sHeight=0;
int textureNum = 4;
static class EGLImageTexture {
public EGLImageTexture(com.sun.opengl.util.texture.Texture t, long i, long s) {
System.out.println("EGLImageTexture " + t + ", image " + i + ", sync "+s);
texture = t; image = i; sync = s;
}
protected com.sun.opengl.util.texture.Texture texture;
protected long image;
protected long sync;
}
EGLImageTexture[] eglImgTexs=null;
HashMap eglImgTexsMap = new HashMap();
long moviePtr = 0;
String path = null;
URL url;
String name = null;
String nameClean = null;
int loopCount = 0;
long position = 0;
float volume = 1.0f;
float rate = 1.0f;
boolean playing = false;
boolean paused = false;
native long _createInstance(int numTextures);
native void _setStream(long moviePtr, String path);
native void _updateStreamInfo(long moviePtr);
native void _setEGLImageTexture2D(long moviePtr, int i, int tex, long image, long sync);
native void _activateInstance(long moviePtr);
native void _deactivateInstance(long moviePtr);
native void _play(long moviePtr, long position, float rate, int loopCount);
native long _stop(long moviePtr);
native void _setVolume(long moviePtr, float volume);
native long _getCurrentPosition(long moviePtr);
native long _setCurrentPosition(long moviePtr, long position);
native long _getCurrentLoaded(long moviePtr);
native long _step(long moviePtr, int direction, long position);
native void _setRate(long moviePtr, float rate);
native int _getTextureID(long moviePtr);
native int _getWidth(long moviePtr);
native int _getHeight(long moviePtr);
native long _getDuration(long moviePtr);
native void _destroyInstance(long moviePtr);
public OMXMoviePlayerImpl() {
System.out.println("OMXMoviePlayerImpl (null)");
}
public OMXMoviePlayerImpl(URL url) {
System.out.println("OMXMoviePlayerImpl ("+url+")");
setURL(url);
}
public void update() {
update(null);
}
/**
public int moduleTest() {
return _moduleTest();
}
native int _moduleTest();
*/
public void update(GL gl) {
if (moviePtr == 0) {
return;
}
System.out.println("update: totalFrames "+totalFrames);
totalFrames = _getDuration(moviePtr);
int w = _getWidth(moviePtr);
int h = _getHeight(moviePtr);
if (h != height || w!=width) {
System.out.println("update: size change "+width+"x"+height+" -> "+w+"x"+h);
width = w;
height = h;
// FIXME: TODO
// [1] EventListener model to notify clients of dimension change
// [2] Reallocate ressources (Textures, EGLImage, ..)
if(null==gl) {
gl = GLU.getCurrentGL();
}
try {
setAllEGLImageTexture2D(gl);
} catch (Exception e) {
e.printStackTrace();
throw new RuntimeException(e);
}
} else {
System.out.println("update: none");
}
}
private String replaceAll(String orig, String search, String repl) {
String dest=null;
// In case replaceAll / java.util.regex.* is not supported (-> CVM)
int i=0,j;
dest = new String();
while((j=orig.indexOf(search, i))>=0) {
dest=dest.concat(orig.substring(i, j));
dest=dest.concat(repl);
i=j+1;
}
return dest.concat(orig.substring(i, orig.length()));
}
public synchronized void setURL(URL u) {
url = u;
if (url == null) {
System.out.println("setURL (null)");
stop();
return;
}
path=null;
if (url.getProtocol() == null || "file".equals(url.getProtocol())) {
// CV only accepts absolute paths
try {
File file = new File(url.getPath());
if (!file.exists()) {
throw new RuntimeException(new FileNotFoundException(file.toString()));
}
path = file.getCanonicalPath();
System.out.println("setURL: path "+path);
} catch (Exception e) {
e.printStackTrace();
throw new RuntimeException(e);
}
}
path = replaceAll(path, "\\", "/").trim();
if(null==path) {
throw new RuntimeException("Couldn't parse stream URL: "+url);
}
System.out.println("setURL: clean path "+path);
GL gl = initGLData();
if(0!=moviePtr) {
dispose();
}
moviePtr = _createInstance(textureNum);
if (moviePtr == 0) {
throw new RuntimeException("Couldn't establish native playback: "+url);
}
_setStream(moviePtr, path);
width = _getWidth(moviePtr);
height = _getHeight(moviePtr);
if(width==0 || height==0) {
throw new RuntimeException("Illegal media dimension "+width+"x"+height);
}
System.out.println("movie size: "+width+"x"+height);
setAllEGLImageTexture2D(gl);
_activateInstance(moviePtr);
position = 0;
}
private GL initGLData() {
GL gl = GLU.getCurrentGL();
if(null==gl) {
throw new RuntimeException("No current GL");
}
/*
if(gl.isGLES2()) {
GLES2 gles2 = gl.getGLES2();
// Debug ..
//DebugGLES2 gldbg = new DebugGLES2(gles2);
//gles2.getContext().setGL(gldbg);
//gles2 = gldbg;
// Trace ..
TraceGLES2 gltrace = new TraceGLES2(gles2, System.out);
gles2.getContext().setGL(gltrace);
gl = gltrace;
}*/
EGLContext eglCtx = (EGLContext) gl.getContext();
if(null==eglCtx) {
throw new RuntimeException("No current EGL context");
}
EGLDrawable eglDrawable = (EGLDrawable) eglCtx.getGLDrawable();
if(null==eglDrawable) {
throw new RuntimeException("No valid drawable");
}
eglContext = eglCtx.getContext();
eglDisplay = eglDrawable.getDisplay();
eglSurface = eglDrawable.getSurface();
eglExt = eglCtx.getEGLExt();
if(null==eglExt) {
throw new RuntimeException("No valid EGLExt");
}
int iTmp[] = new int[1];
EGL.eglQuerySurface(eglDisplay, eglSurface, EGL.EGL_WIDTH, iTmp, 0);
sWidth=iTmp[0];
EGL.eglQuerySurface(eglDisplay, eglSurface, EGL.EGL_HEIGHT, iTmp, 0);
sHeight=iTmp[0];
System.out.println("surface size: "+width+"x"+height);
System.out.println(eglDrawable);
System.out.println(eglCtx);
System.out.println("EGL Extensions : "+EGL.eglQueryString(eglDisplay, EGL.EGL_EXTENSIONS));
System.out.println("EGL CLIENT APIs: "+EGL.eglQueryString(eglDisplay, EGL.EGL_CLIENT_APIS));
return gl;
}
private void removeAllEGLImageTexture2D() {
System.out.println("removeAllEGLImg: mp "+moviePtr+", textureNum "+textureNum);
if (moviePtr != 0) {
texture = null;
for(int i=0; i<textureNum; i++) {
eglExt.eglDestroyImage(
eglDisplay,
eglImgTexs[i].image);
eglExt.eglDestroySync(eglImgTexs[i].sync);
// JAU eglImgTexs[i].texture.dispose();
eglImgTexs[i]=null;
}
eglImgTexsMap.clear();
}
}
private void errorCheckGL(GL gl, String s) {
int e;
if( (e=gl.glGetError()) != GL.GL_NO_ERROR ) {
System.out.println("GL Error: ("+s+"): "+e);
}
}
private void errorCheckEGL(String s) {
int e;
if( (e=EGL.eglGetError()) != EGL.EGL_SUCCESS ) {
System.out.println("EGL Error: ("+s+"): "+e);
}
}
private void setAllEGLImageTexture2D(GL gl) {
if (moviePtr == 0) {
return;
}
if(null!=eglImgTexs) {
removeAllEGLImageTexture2D();
} else {
eglImgTexs = new EGLImageTexture[textureNum];
}
int[] tmp = new int[1];
int tex, e;
gl.glEnable(gl.GL_TEXTURE_2D);
errorCheckGL(gl, "i.2");
for(int i=0; i<textureNum; i++) {
String s0 = String.valueOf(i);
gl.glGenTextures(1, tmp, 0);
tex=tmp[0];
if( (e=gl.glGetError()) != GL.GL_NO_ERROR || 0>tex ) {
throw new RuntimeException("TextureName creation failed: "+e);
}
gl.glBindTexture(gl.GL_TEXTURE_2D, tex);
// create space for buffer with a texture
gl.glTexImage2D(
gl.GL_TEXTURE_2D, // target
0, // level
gl.GL_RGBA, // internal format
width, // width
height, // height
0, // border
gl.GL_RGBA, // format
gl.GL_UNSIGNED_BYTE, // type
null); // pixels -- will be provided later
gl.glTexParameteri(gl.GL_TEXTURE_2D, gl.GL_TEXTURE_MIN_FILTER, gl.GL_NEAREST);
gl.glTexParameteri(gl.GL_TEXTURE_2D, gl.GL_TEXTURE_MAG_FILTER, gl.GL_NEAREST);
long image=0, sync=0;
// create EGLImage from texture
tmp[0] = EGL.EGL_NONE;
image = eglExt.eglCreateImage(
eglDisplay,
eglContext,
eglExt.EGL_GL_TEXTURE_2D,
tex,
tmp, 0);
if (0==image) {
throw new RuntimeException("EGLImage creation failed: "+EGL.eglGetError());
}
// Create sync object so that we can be sure that gl has finished
// rendering the EGLImage texture before we tell OpenMAX to fill
// it with a new frame.
tmp[0] = EGL.EGL_NONE;
sync = eglExt.eglCreateFenceSync(
eglDisplay,
eglExt.EGL_SYNC_PRIOR_COMMANDS_COMPLETE, tmp, 0);
_setEGLImageTexture2D(moviePtr, i, tex, image, sync);
eglImgTexs[i] = new EGLImageTexture(
com.sun.opengl.util.texture.TextureIO.newTexture(tex,
javax.media.opengl.GL2.GL_TEXTURE_2D,
width,
height,
width,
height,
true),
image, sync);
eglImgTexsMap.put(new Integer(tex), eglImgTexs[i]);
}
}
public boolean isValid() {
return (moviePtr != 0);
}
public String getPath() {
return path;
}
public URL getURL() {
return url;
}
// start playing from the beginning (user frame)
public void play() {
paused = false;
if (moviePtr == 0) {
return;
}
playing = true;
_play(moviePtr, position, rate, loopCount);
}
public boolean isPlaying() {
return playing;
}
public long pause() {
paused = true;
if (moviePtr == 0) {
return 0;
}
if (height != 0 && width != 0) {
position = stop();
}
return position;
}
// returns the last played frame
public long stop() {
if (moviePtr == 0) {
return 0;
}
playing = false;
position = _stop(moviePtr);
return position;
}
public long step(int direction) {
if (moviePtr == 0) {
return 0;
}
if (isPlaying()) {
stop();
}
position = _step(moviePtr, direction, position);
return position;
}
// 0 (no volume) to 1 (max volume)
public void setVolume(float volume) {
if (moviePtr == 0) {
return;
}
if (volume < 0.0f) {
volume = 0.0f;
}
volume = volume;
_setVolume(moviePtr, volume);
}
public float getVolume() {
return volume;
}
public void setLoopCount(int loopCount) {
loopCount = loopCount;
}
public int getLoopCount() {
return loopCount;
}
// in frames
public void setPosition(long position) {
if (position < 0) {
position = 0;
} else if (position <= totalFrames) {
position = position;
}
else {
position = 0;
}
_setCurrentPosition(moviePtr, position);
}
public long getLoaded() {
if (moviePtr == 0) {
return 0;
}
return _getCurrentLoaded(moviePtr);
}
public long getPosition() {
if (moviePtr == 0) {
position = 0;
} else if (playing == true) {
long pos = _getCurrentPosition(moviePtr);
//System.out.println("getPosition " + pos + " " + position);
position = pos;
}
return position;
}
public long getEndPosition() {
return totalFrames;
}
public void setRate(float rate) {
setPlaybackRate(rate);
}
public float getRate() {
return getPlaybackRate();
}
// -1.0f - backwards normal speed
// 0.5f - 1/2x speed
// 1.0f - normal speed
// 2.0f - 2x speed
public void setPlaybackRate(float r) {
if (moviePtr == 0) {
return;
}
rate = r;
_setRate(moviePtr, rate);
}
public float getPlaybackRate() {
return rate;
}
public int getWidth() {
return width;
}
public int getHeight() {
return height;
}
public synchronized void dispose() {
removeAllEGLImageTexture2D();
if (moviePtr != 0) {
long ptr = moviePtr;
moviePtr = 0;
_destroyInstance(ptr);
eglExt=null;
eglSurface=0;
eglDisplay=0;
eglContext=0;
}
}
protected synchronized void finalize() {
if (moviePtr != 0) {
dispose();
}
}
// *** Texture impl
com.sun.opengl.util.texture.Texture texture = null; // holds the last fetched texture
public void unlockTexture() {
// FIXME: EGLSync notification ?
}
public Texture lockTexture() {
texture=null;
if (moviePtr == 0) {
return null;
}
EGLImageTexture eglImgTex = (EGLImageTexture) eglImgTexsMap.get(new Integer(_getTextureID(moviePtr)));
if(null!=eglImgTex) {
texture = eglImgTex.texture;
}
return texture;
}
}
|