Skip to content

Commit 401c47d

Browse files
committed
add an even method to notify user about new frames availability
Signed-off-by: Umair Khan <[email protected]>
1 parent ed28fca commit 401c47d

File tree

3 files changed

+55
-1
lines changed

3 files changed

+55
-1
lines changed

src/in/omerjerk/processing/video/android/Capture.java

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -115,6 +115,11 @@ public void run() {
115115
});
116116
}
117117

118+
@Override
119+
public String getEventName() {
120+
return "captureEvent";
121+
}
122+
118123
public static String[] list() {
119124
//The following check has to be commented to make list() method static
120125
// if (applet.getPackageManager().hasSystemFeature(

src/in/omerjerk/processing/video/android/Movie.java

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,11 @@ public void onResume() {
6060
initalizeFrameBuffer();
6161
}
6262

63+
@Override
64+
public String getEventName() {
65+
return "movieEvent";
66+
}
67+
6368
private class MediaPlayerHandler extends Handler {
6469

6570
public static final int MSG_INIT_PLAYER = 0;

src/in/omerjerk/processing/video/android/VideoBase.java

Lines changed: 45 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
package in.omerjerk.processing.video.android;
22

3+
import java.lang.reflect.Method;
34
import java.nio.IntBuffer;
45

56
import in.omerjerk.processing.video.android.helpers.FullFrameRect;
@@ -29,6 +30,9 @@ public static void log(String log) {
2930
if (DEBUG)
3031
System.out.println(log);
3132
}
33+
34+
private Method eventMethod;
35+
private Object eventHandler;
3236

3337
protected GLSurfaceView glView;
3438
protected SurfaceTexture mSurfaceTexture;
@@ -49,6 +53,7 @@ public static void log(String log) {
4953

5054
public abstract void onResume();
5155
public abstract void onPause();
56+
public abstract String getEventName();
5257

5358
public VideoBase(PApplet parent) {
5459
super();
@@ -57,6 +62,8 @@ public VideoBase(PApplet parent) {
5762
parent.registerMethod("pause", this);
5863
parent.registerMethod("resume", this);
5964

65+
setEventHandlerObject(parent);
66+
6067
glView = (GLSurfaceView) parent.getSurfaceView();
6168
pg = (PGraphicsOpenGL)parent.g;
6269
// customTexture = new Texture(pg, width, height);
@@ -69,6 +76,10 @@ public VideoBase(PApplet parent) {
6976
public boolean available() {
7077
return isAvailable;
7178
}
79+
80+
public void read() {
81+
getImage(false);
82+
}
7283

7384
protected void createSurfaceTexture() {
7485
mFullScreen = new FullFrameRect(new Texture2dProgram(
@@ -160,7 +171,7 @@ public void run() {
160171
surfaceTexture.getTransformMatrix(mSTMatrix);
161172
mFullScreen.drawFrame(mTextureId, mSTMatrix);
162173

163-
getImage(false);
174+
fireEvent();
164175

165176
/*
166177
* pixelBuffer.position(0); GLES20.glReadPixels(0, 0, width,
@@ -237,4 +248,37 @@ public void loadPixels() {
237248
pixelBuffer.position(0);
238249
pixelBuffer.get(this.pixels);
239250
}
251+
252+
private void setEventHandlerObject(Object obj) {
253+
eventHandler = obj;
254+
255+
try {
256+
eventMethod = obj.getClass().getMethod(getEventName(), Capture.class);
257+
return;
258+
} catch (Exception e) {
259+
// no such method, or an error.. which is fine, just ignore
260+
}
261+
262+
// The captureEvent method may be declared as receiving Object, rather
263+
// than Capture.
264+
try {
265+
eventMethod = obj.getClass().getMethod(getEventName(), Object.class);
266+
return;
267+
} catch (Exception e) {
268+
// no such method, or an error.. which is fine, just ignore
269+
}
270+
}
271+
272+
private void fireEvent() {
273+
if (eventMethod != null) {
274+
try {
275+
eventMethod.invoke(eventHandler, this);
276+
277+
} catch (Exception e) {
278+
System.err.println("error, disabling " + getEventName() + "()");
279+
e.printStackTrace();
280+
eventMethod = null;
281+
}
282+
}
283+
}
240284
}

0 commit comments

Comments
 (0)