-
-
Notifications
You must be signed in to change notification settings - Fork 2.6k
Expand file tree
/
Copy pathofxAndroidVideoPlayer.cpp
More file actions
592 lines (453 loc) · 17.5 KB
/
ofxAndroidVideoPlayer.cpp
File metadata and controls
592 lines (453 loc) · 17.5 KB
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
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
/*
* ofxAndroidVideoPlayer.cpp
*
* Created on: 25/04/2013
* Author: p
*/
#include "ofxAndroidVideoPlayer.h"
#include "ofxAndroidUtils.h"
#include "ofLog.h"
#include "ofMatrix4x4.h"
using namespace std;
//---------------------------------------------------------------------------
void ofxAndroidVideoPlayer::reloadTexture(){
if(!texture.texData.bAllocated) return;
glGenTextures(1, (GLuint *)&texture.texData.textureID);
glEnable(texture.texData.textureTarget);
glBindTexture(texture.texData.textureTarget, (GLuint)texture.texData.textureID);
glTexParameterf(texture.texData.textureTarget, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
glTexParameterf(texture.texData.textureTarget, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
glTexParameterf(texture.texData.textureTarget, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
glTexParameterf(texture.texData.textureTarget, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
#ifndef TARGET_PROGRAMMABLE_GL
glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE);
#endif
glDisable(texture.texData.textureTarget);
JNIEnv *env = ofGetJNIEnv();
if (!env) {
ofLogError("ofxAndroidVideoPlayer") << "reloadTexture(): couldn't get environment using GetEnv()";
return;
}
jmethodID javasetTexture = env->GetMethodID(javaClass,"setTexture","(I)V");
if(!javasetTexture){
ofLogError("ofxAndroidVideoPlayer") << "reloadTexture(): couldn't get java setTexture for VideoPlayer";
return;
}
env->CallVoidMethod(javaVideoPlayer,javasetTexture,texture.getTextureData().textureID);
}
//---------------------------------------------------------------------------
void ofxAndroidVideoPlayer::unloadTexture(){
texture.texData.textureID=0;
}
//---------------------------------------------------------------------------
ofxAndroidVideoPlayer::ofxAndroidVideoPlayer(){
JNIEnv *env = ofGetJNIEnv();
if (!env) {
ofLogError("ofxAndroidVideoPlayer") << "constructor: couldn't get environment using GetEnv()";
return;
}
jclass localClass = env->FindClass("cc/openframeworks/OFAndroidVideoPlayer");
javaClass = (jclass) env->NewGlobalRef(localClass);
if(!javaClass){
ofLogError("ofxAndroidVideoPlayer") << "constructor: couldn't get java class for VideoPlayer";
return;
}
jmethodID constructor = env->GetMethodID(javaClass,"<init>","()V");
if(!constructor){
ofLogError("ofxAndroidVideoPlayer") << "constructor: couldn't get java constructor for VideoPlayer";
return;
}
javaVideoPlayer = env->NewObject(javaClass,constructor);
if(!javaVideoPlayer){
ofLogError("ofxAndroidVideoPlayer") << "constructor: couldn't create java VideoPlayer";
return;
}
javaVideoPlayer = (jobject)env->NewGlobalRef(javaVideoPlayer);
bIsFrameNew = false;
jfloatArray localMatrixJava = env->NewFloatArray(16);
matrixJava = (jfloatArray) env->NewGlobalRef(localMatrixJava);
ofAddListener(ofxAndroidEvents().unloadGL,this,&ofxAndroidVideoPlayer::unloadTexture);
ofAddListener(ofxAndroidEvents().reloadGL,this,&ofxAndroidVideoPlayer::reloadTexture);
}
//---------------------------------------------------------------------------
ofxAndroidVideoPlayer::~ofxAndroidVideoPlayer(){
JNIEnv *env = ofGetJNIEnv();
if(javaVideoPlayer) env->DeleteGlobalRef(javaVideoPlayer);
if(javaClass) env->DeleteGlobalRef(javaClass);
if(matrixJava) env->DeleteGlobalRef(matrixJava);
}
//---------------------------------------------------------------------------
bool ofxAndroidVideoPlayer::load(string fileName){
if(!javaVideoPlayer){
ofLogError("ofxAndroidVideoPlayer") << "load(): java soundVideoPlayer not loaded";
return false;
}
JNIEnv *env = ofGetJNIEnv();
jmethodID javaLoadMethod = env->GetMethodID(javaClass,"loadMovie","(Ljava/lang/String;)V");
if(!javaLoadMethod){
ofLogError("ofxAndroidVideoPlayer") << "load(): couldn't get java loadVideo for VideoPlayer";
return false;
}
jstring javaFileName = ofGetJNIEnv()->NewStringUTF(ofToDataPath(fileName,true).c_str());
env->CallVoidMethod(javaVideoPlayer,javaLoadMethod,javaFileName);
env->DeleteLocalRef((jobject)javaFileName);
jmethodID javaGetWidthMethod = env->GetMethodID(javaClass,"getWidth","()I");
if(!javaGetWidthMethod){
ofLogError("ofxAndroidVideoPlayer") << "loadMovie(): couldn't get java getWidth for VideoPlayer";
return false;
}
width = env->CallIntMethod(javaVideoPlayer, javaGetWidthMethod);
jmethodID javaGetHeightMethod = env->GetMethodID(javaClass,"getHeight","()I");
if(!javaGetHeightMethod){
ofLogError("ofxAndroidVideoPlayer") << "loadMovie(): couldn't get java getHeight for VideoPlayer";
return false;
}
height = env->CallIntMethod(javaVideoPlayer, javaGetHeightMethod);
ofTextureData td;
td.width = width;
td.height = height;
td.tex_w = td.width;
td.tex_h = td.height;
td.tex_t = 1; // Hack!
td.tex_u = 1;
td.textureTarget = GL_TEXTURE_EXTERNAL_OES;
td.glInternalFormat = GL_RGBA;
td.bFlipTexture = false;
// hack to initialize gl resources from outside ofTexture
texture.texData = td;
texture.texData.bAllocated = true;
reloadTexture();
ofLogVerbose("ofxAndroidVideoPlayer") << "loadMovie(): movie size: "<< width << "x" << height;
return true;
}
//---------------------------------------------------------------------------
void ofxAndroidVideoPlayer::close(){
if(!javaVideoPlayer){
ofLogError("ofxAndroidVideoPlayer") << "unloadMovie(): java VideoPlayer not loaded";
return;
}
JNIEnv *env = ofGetJNIEnv();
if (!env) {
ofLogError("ofxAndroidVideoPlayer") << "unloadMovie(): couldn't get environment using GetEnv()";
return;
}
jmethodID javaUnloadMethod = env->GetMethodID(javaClass,"unloadMovie","()V");
if(!javaUnloadMethod){
ofLogError("ofxAndroidVideoPlayer") << "unloadMovie(): couldn't get java unloadMovie for VideoPlayer";
return;
}
unloadTexture();
env->CallVoidMethod(javaVideoPlayer,javaUnloadMethod);
}
//---------------------------------------------------------------------------
void ofxAndroidVideoPlayer::update(){
JNIEnv *env = ofGetJNIEnv();
jmethodID javaUpdate = env->GetMethodID(javaClass,"update","()Z");
if(!javaUpdate){
ofLogError("ofxAndroidVideoPlayer") << "update(): couldn't get java update for VideoPlayer";
return;
}
bIsFrameNew = env->CallBooleanMethod(javaVideoPlayer,javaUpdate);
jmethodID javaGetTextureMatrix = env->GetMethodID(javaClass,"getTextureMatrix","([F)V");
if(!javaGetTextureMatrix){
ofLogError("ofxAndroidVideoPlayer") << "update(): couldn't get java javaGetTextureMatrix for VideoPlayer";
return;
}
env->CallVoidMethod(javaVideoPlayer,javaGetTextureMatrix,matrixJava);
jfloat * m = env->GetFloatArrayElements(matrixJava,0);
ofMatrix4x4 textureMatrix(m);
ofMatrix4x4 vFlipTextureMatrix;
vFlipTextureMatrix.scale(1,-1,1);
vFlipTextureMatrix.translate(0,1,0);
texture.setTextureMatrix(vFlipTextureMatrix * textureMatrix);
//texture.getTextureData().tex_t = 1.+1-matrix.getPtr()[0]; // Hack!
//texture.getTextureData().tex_u = 1.;
env->ReleaseFloatArrayElements(matrixJava,m,0);
}
//---------------------------------------------------------------------------
void ofxAndroidVideoPlayer::play(){
JNIEnv *env = ofGetJNIEnv();
jmethodID javaPlay = env->GetMethodID(javaClass,"play","()V");
if(!javaPlay){
ofLogError("ofxAndroidVideoPlayer") << "play(): couldn't get java play for VideoPlayer";
return;
}
env->CallVoidMethod(javaVideoPlayer,javaPlay);
};
//---------------------------------------------------------------------------
void ofxAndroidVideoPlayer::stop(){
JNIEnv *env = ofGetJNIEnv();
jmethodID javaStop = env->GetMethodID(javaClass,"stop","()V");
if(!javaStop){
ofLogError("ofxAndroidVideoPlayer") << "stop(): couldn't get java stop for VideoPlayer";
return;
}
env->CallVoidMethod(javaVideoPlayer,javaStop);
};
//---------------------------------------------------------------------------
bool ofxAndroidVideoPlayer::isPaused() const {
if(!javaVideoPlayer){
ofLogError("ofxAndroidVideoPlayer") << "isPaused(): java VideoPlayer not loaded";
return 0;
}
JNIEnv *env = ofGetJNIEnv();
if (!env) {
ofLogError("ofxAndroidVideoPlayer") << "isPaused(): couldn't get environment using GetEnv()";
return 0;
}
jmethodID javaIsPausedMethod = env->GetMethodID(javaClass,"isPaused","()Z");
if(!javaIsPausedMethod){
ofLogError("ofxAndroidVideoPlayer") << "isPaused(): couldn't get java isPaused for VideoPlayer";
return 0;
}
return env->CallBooleanMethod(javaVideoPlayer,javaIsPausedMethod);
};
//---------------------------------------------------------------------------
bool ofxAndroidVideoPlayer::isLoaded() const {
if(!javaVideoPlayer){
ofLogError("ofxAndroidVideoPlayer") << "isLoaded(): java VideoPlayer not loaded";
return 0;
}
JNIEnv *env = ofGetJNIEnv();
if (!env) {
ofLogError("ofxAndroidVideoPlayer") << "isLoaded(): couldn't get environment using GetEnv()";
return 0;
}
jmethodID javaIsLoadedMethod = env->GetMethodID(javaClass,"isLoaded","()Z");
if(!javaIsLoadedMethod){
ofLogError("ofxAndroidVideoPlayer") << "isLoaded(): couldn't get java isLoaded for VideoPlayer";
return 0;
}
return env->CallBooleanMethod(javaVideoPlayer,javaIsLoadedMethod);
};
//---------------------------------------------------------------------------
bool ofxAndroidVideoPlayer::isPlaying() const {
if(!javaVideoPlayer){
ofLogError("ofxAndroidVideoPlayer") << "isPlaying(): java VideoPlayer not loaded";
return 0;
}
JNIEnv *env = ofGetJNIEnv();
if (!env) {
ofLogError("ofxAndroidVideoPlayer") << "isPlaying(): couldn't get environment using GetEnv()";
return 0;
}
jmethodID javaIsPlayingMethod = env->GetMethodID(javaClass,"isPlaying","()Z");
if(!javaIsPlayingMethod){
ofLogError("ofxAndroidVideoPlayer") << "isPlaying(): couldn't get the java isPlaying for VideoPlayer";
return 0;
}
return env->CallBooleanMethod(javaVideoPlayer,javaIsPlayingMethod);
};
//---------------------------------------------------------------------------
ofTexture * ofxAndroidVideoPlayer::getTexturePtr(){
return & texture;
}
//---------------------------------------------------------------------------
float ofxAndroidVideoPlayer::getWidth() const {
return width;
};
//---------------------------------------------------------------------------
float ofxAndroidVideoPlayer::getHeight() const {
return height;
};
//---------------------------------------------------------------------------
float ofxAndroidVideoPlayer::getPosition() const {
if(!javaVideoPlayer){
ofLogError("ofxAndroidVideoPlayer") << "getPosition(): java VideoPlayer not loaded";
return 0.;
}
JNIEnv *env = ofGetJNIEnv();
if (!env) {
ofLogError("ofxAndroidVideoPlayer") << "getPosition(): couldn't get environment using GetEnv()";
return 0;
}
jmethodID javaGetPositionMethod = env->GetMethodID(javaClass,"getPosition","()F");
if(!javaGetPositionMethod){
ofLogError("ofxAndroidVideoPlayer") << "getPosition(): couldn't get java getPosition for VideoPlayer";
return 0;
}
return env->CallFloatMethod(javaVideoPlayer,javaGetPositionMethod);
};
//---------------------------------------------------------------------------
float ofxAndroidVideoPlayer::getDuration() const {
if(!javaVideoPlayer){
ofLogError("ofxAndroidVideoPlayer") << "getDuration(): java VideoPlayer not loaded";
return 0.;
}
JNIEnv *env = ofGetJNIEnv();
if (!env) {
ofLogError("ofxAndroidVideoPlayer") << "getDuration(): couldn't get environment using GetEnv()";
return 0;
}
jmethodID javaGetDurationMethod = env->GetMethodID(javaClass,"getDuration","()F");
if(!javaGetDurationMethod){
ofLogError("ofxAndroidVideoPlayer") << "getDuration(): couldn't get java getDuration for VideoPlayer";
return 0;
}
return env->CallFloatMethod(javaVideoPlayer,javaGetDurationMethod);
};
//---------------------------------------------------------------------------
bool ofxAndroidVideoPlayer::getIsMovieDone() const {
if(!javaVideoPlayer){
ofLogError("ofxAndroidVideoPlayer") << "getIsMovieDone(): java VideoPlayer not loaded";
return 0;
}
JNIEnv *env = ofGetJNIEnv();
if (!env) {
ofLogError("ofxAndroidVideoPlayer") << "getIsMovieDone(): couldn't get environment using GetEnv()";
return 0;
}
jmethodID javaIsMovieDoneMethod = env->GetMethodID(javaClass,"isMovieDone","()Z");
if(!javaIsMovieDoneMethod){
ofLogError("ofxAndroidVideoPlayer") << "getIsMovieDone(): couldn't get java isMovieDone for VideoPlayer";
return 0;
}
return env->CallBooleanMethod(javaVideoPlayer,javaIsMovieDoneMethod);
};
//---------------------------------------------------------------------------
void ofxAndroidVideoPlayer::setPosition(float pct){
if(!javaVideoPlayer){
ofLogError("ofxAndroidVideoPlayer") << "setPosition(): java VideoPlayer not loaded";
return;
}
JNIEnv *env = ofGetJNIEnv();
if (!env) {
ofLogError("ofxAndroidVideoPlayer") << "setPosition(): couldn't get environment using GetEnv()";
return;
}
jmethodID javaSetPositionMethod = env->GetMethodID(javaClass,"setPosition","(F)V");
if(!javaSetPositionMethod){
ofLogError("ofxAndroidVideoPlayer") << "setPosition(): couldn't get java setPosition for VideoPlayer";
return;
}
env->CallVoidMethod(javaVideoPlayer,javaSetPositionMethod,pct);
};
//---------------------------------------------------------------------------
void ofxAndroidVideoPlayer::setPaused(bool bPause){
if(!javaVideoPlayer){
ofLogError("ofxAndroidVideoPlayer") << "setPaused(): java VideoPlayer not loaded";
return;
}
JNIEnv *env = ofGetJNIEnv();
if (!env) {
ofLogError("ofxAndroidVideoPlayer") << "setPaused(): couldn't get environment using GetEnv()";
return;
}
jmethodID javaPausedMethod = env->GetMethodID(javaClass,"setPaused","(Z)V");
if(!javaPausedMethod){
ofLogError("ofxAndroidVideoPlayer") << "setPaused(): coulnd't get java setPaused for VideoPlayer";
return;
}
env->CallVoidMethod(javaVideoPlayer,javaPausedMethod,bPause?1:0);
};
//------------------------------------------------------------
void ofxAndroidVideoPlayer::setVolume(float vol){
if(!javaVideoPlayer){
ofLogError("ofxAndroidVideoPlayer") << "setVolume(): java VideoPlayer not loaded";
return;
}
JNIEnv *env = ofGetJNIEnv();
if (!env) {
ofLogError("ofxAndroidVideoPlayer") << "setVolume(): couldn't get environment using GetEnv()";
return;
}
jmethodID javaVolumeMethod = env->GetMethodID(javaClass,"setVolume","(F)V");
if(!javaVolumeMethod){
ofLogError("ofxAndroidVideoPlayer") << "setVolume(): couldn't get java setVolume for VideoPlayer";
return;
}
env->CallVoidMethod(javaVideoPlayer,javaVolumeMethod,vol);
};
//------------------------------------------------------------
void ofxAndroidVideoPlayer::setLoopState(ofLoopType state){
if(!javaVideoPlayer){
ofLogError("ofxAndroidVideoPlayer") << "setLoopState(): java VideoPlayer not loaded";
return;
}
JNIEnv *env = ofGetJNIEnv();
if (!env) {
ofLogError("ofxAndroidVideoPlayer") << "setLoopState(): couldn't get environment using GetEnv()";
return;
}
jmethodID javaLoopStateMethod = env->GetMethodID(javaClass,"setLoopState","(Z)V");
if(!javaLoopStateMethod){
ofLogError("ofxAndroidVideoPlayer") << "setLoopState(): couldn't get java LoopState for VideoPlayer";
return;
}
switch (state) {
case OF_LOOP_NORMAL:
env->CallVoidMethod(javaVideoPlayer,javaLoopStateMethod,1);
break;
case OF_LOOP_PALINDROME:
// TODO Palindrome loop not implemented
ofLogError("ofxAndroidVideoPlayer") << "setLoopState(): OF_LOOP_PALINDROME not implemented";
break;
case OF_LOOP_NONE:
default:
env->CallVoidMethod(javaVideoPlayer,javaLoopStateMethod,0);
break;
}
};
//------------------------------------------------------------
ofLoopType ofxAndroidVideoPlayer::getLoopState() const {
if(!javaVideoPlayer){
ofLogError("ofxAndroidVideoPlayer") << "getLoopState(): java VideoPlayer not loaded";
return OF_LOOP_NONE;
}
JNIEnv *env = ofGetJNIEnv();
if (!env) {
ofLogError("ofxAndroidVideoPlayer") << "getLoopState(): couldn't get environment using GetEnv()";
return OF_LOOP_NONE;
}
jmethodID javaGetLoopStateMethod = env->GetMethodID(javaClass,"getLoopState","()Z");
if(!javaGetLoopStateMethod){
ofLogError("ofxAndroidVideoPlayer") << "getLoopState(): couldn't get java GetLoopState for VideoPlayer";
return OF_LOOP_NONE;
}
bool loopState = env->CallBooleanMethod(javaVideoPlayer,javaGetLoopStateMethod);
if (loopState) {
return OF_LOOP_NORMAL;
} else {
return OF_LOOP_NONE;
}
};
//------------------------------------------------------------
/* Needs movie frameRate to work
int ofxAndroidVideoPlayer::getCurrentFrame() {
if(!javaVideoPlayer){
ofLogError("ofxAndroidVideoPlayer") << "getCurrentFrame(): java VideoPlayer not loaded";
return 0.;
}
JNIEnv *env = ofGetJNIEnv();
if (!env) {
ofLogError("ofxAndroidVideoPlayer") << "getCurrentFrame(): couldn't get environment using GetEnv()";
return 0;
}
jmethodID javaGetCurrentFrame = env->GetMethodID(javaClass,"getCurrentFrame","()I");
if(!javaGetCurrentFrame){
ofLogError("ofxAndroidVideoPlayer") << "getCurrentFrame(): couldn't get java getCurrentFrame for VideoPlayer";
return 0;
}
return env->CallIntMethod(javaVideoPlayer,javaGetCurrentFrame);
};
*/
//------------------------------------------------------------
/* Needs movie frameRate to work
int ofxAndroidVideoPlayer::getTotalNumFrames() {
if(!javaVideoPlayer){
ofLogError("ofxAndroidVideoPlayer") << "getTotalNumFrames(): java VideoPlayer not loaded";
return 0.;
}
JNIEnv *env = ofGetJNIEnv();
if (!env) {
ofLogError("ofxAndroidVideoPlayer") << "getTotalNumFrames(): couldn't get environment using GetEnv()";
return 0;
}
jmethodID javaGetTotalNumFrames = env->GetMethodID(javaClass,"getTotalNumFrames","()I");
if(!javaGetTotalNumFrames){
ofLogError("ofxAndroidVideoPlayer") << "getTotalNumFrames(): couldn't get java getTotalNumFrames for VideoPlayer";
return 0;
}
return env->CallIntMethod(javaVideoPlayer,javaGetTotalNumFrames);
};
*/