Skip to content

Commit dd19d93

Browse files
committed
add render error callback
1 parent 6cf5094 commit dd19d93

File tree

4 files changed

+52
-4
lines changed

4 files changed

+52
-4
lines changed

library/src/main/java/com/pedro/library/view/GlInterface.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -202,4 +202,6 @@ public interface GlInterface {
202202
void setForceRender(boolean enabled);
203203

204204
boolean isRunning();
205+
206+
void setRenderErrorCallback(RenderErrorCallback callback);
205207
}

library/src/main/java/com/pedro/library/view/GlStreamInterface.kt

Lines changed: 21 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,7 @@ class GlStreamInterface(private val context: Context): OnFrameAvailableListener,
8080
private val forceRender = ForceRenderer()
8181
var autoHandleOrientation = false
8282
private var shouldHandleOrientation = true
83+
private var renderErrorCallback: RenderErrorCallback? = null
8384

8485
private val sensorRotationManager = SensorRotationManager(context, true, true) { orientation, isPortrait ->
8586
if (autoHandleOrientation && shouldHandleOrientation) {
@@ -122,6 +123,10 @@ class GlStreamInterface(private val context: Context): OnFrameAvailableListener,
122123

123124
override fun isRunning(): Boolean = running.get()
124125

126+
override fun setRenderErrorCallback(callback: RenderErrorCallback?) {
127+
this.renderErrorCallback = callback
128+
}
129+
125130
override fun getSurfaceTexture(): SurfaceTexture {
126131
return mainRender.getSurfaceTexture()
127132
}
@@ -178,7 +183,15 @@ class GlStreamInterface(private val context: Context): OnFrameAvailableListener,
178183
surfaceManagerPhoto.eglSetup(encoderWidth, encoderHeight, surfaceManager)
179184
running.set(true)
180185
mainRender.getSurfaceTexture().setOnFrameAvailableListener(this)
181-
forceRender.start { executor?.execute { draw(true) } }
186+
forceRender.start {
187+
executor?.execute {
188+
try {
189+
draw(true)
190+
} catch (e: RuntimeException) {
191+
renderErrorCallback?.onRenderError(e) ?: throw e
192+
}
193+
}
194+
}
182195
sensorRotationManager.start()
183196
}
184197
}
@@ -266,7 +279,13 @@ class GlStreamInterface(private val context: Context): OnFrameAvailableListener,
266279

267280
override fun onFrameAvailable(surfaceTexture: SurfaceTexture?) {
268281
if (!isRunning) return
269-
executor?.execute { draw(false) }
282+
executor?.execute {
283+
try {
284+
draw(false)
285+
} catch (e: RuntimeException) {
286+
renderErrorCallback?.onRenderError(e) ?: throw e
287+
}
288+
}
270289
}
271290

272291
fun forceOrientation(forced: OrientationForced) {

library/src/main/java/com/pedro/library/view/OpenGlView.java

Lines changed: 24 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,7 @@ public class OpenGlView extends SurfaceView
7777
private ExecutorService executor = null;
7878
private final FpsLimiter fpsLimiter = new FpsLimiter();
7979
private final ForceRenderer forceRenderer = new ForceRenderer();
80+
private RenderErrorCallback renderErrorCallback = null;
8081

8182
public OpenGlView(Context context) {
8283
super(context);
@@ -220,6 +221,11 @@ public boolean isRunning() {
220221
return running.get();
221222
}
222223

224+
@Override
225+
public void setRenderErrorCallback(RenderErrorCallback callback) {
226+
this.renderErrorCallback = callback;
227+
}
228+
223229
@Override
224230
public void setEncoderSize(int width, int height) {
225231
this.encoderWidth = width;
@@ -359,7 +365,15 @@ public void start() {
359365
forceRenderer.start(() -> {
360366
ExecutorService ex = this.executor;
361367
if (ex == null) return null;
362-
ex.execute(() -> draw(true));
368+
ex.execute(() -> {
369+
try {
370+
draw(true);
371+
} catch (RuntimeException e) {
372+
RenderErrorCallback callback = renderErrorCallback;
373+
if (callback != null) callback.onRenderError(e);
374+
else throw e;
375+
}
376+
});
363377
return null;
364378
});
365379
return null;
@@ -390,7 +404,15 @@ public void onFrameAvailable(SurfaceTexture surfaceTexture) {
390404
if (!isRunning()) return;
391405
ExecutorService ex = this.executor;
392406
if (ex == null) return;
393-
ex.execute(() -> draw(false));
407+
ex.execute(() -> {
408+
try {
409+
draw(false);
410+
} catch (RuntimeException e) {
411+
RenderErrorCallback callback = renderErrorCallback;
412+
if (callback != null) callback.onRenderError(e);
413+
else throw e;
414+
}
415+
});
394416
}
395417

396418
@Override
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
package com.pedro.library.view
2+
3+
interface RenderErrorCallback {
4+
fun onRenderError(error: RuntimeException)
5+
}

0 commit comments

Comments
 (0)