From 2b67f2a6268daf1d05904db0be11c898f511e695 Mon Sep 17 00:00:00 2001 From: andrewlewis Date: Mon, 18 Feb 2019 15:50:55 +0000 Subject: [PATCH] Avoid crashing process on OOM Catch OutOfMemoryErrors and surface them as unexpected ExoPlaybackExceptions. PiperOrigin-RevId: 234481140 --- .../exoplayer2/ExoPlaybackException.java | 33 +++++++++++++++---- .../exoplayer2/ExoPlayerImplInternal.java | 9 +++-- 2 files changed, 33 insertions(+), 9 deletions(-) diff --git a/library/core/src/main/java/com/google/android/exoplayer2/ExoPlaybackException.java b/library/core/src/main/java/com/google/android/exoplayer2/ExoPlaybackException.java index d5ceb3db30..48f82b2c45 100644 --- a/library/core/src/main/java/com/google/android/exoplayer2/ExoPlaybackException.java +++ b/library/core/src/main/java/com/google/android/exoplayer2/ExoPlaybackException.java @@ -31,11 +31,11 @@ public final class ExoPlaybackException extends Exception { /** * The type of source that produced the error. One of {@link #TYPE_SOURCE}, {@link #TYPE_RENDERER} - * or {@link #TYPE_UNEXPECTED}. + * {@link #TYPE_UNEXPECTED}, {@link #TYPE_REMOTE} or {@link #TYPE_OUT_OF_MEMORY}. */ @Documented @Retention(RetentionPolicy.SOURCE) - @IntDef({TYPE_SOURCE, TYPE_RENDERER, TYPE_UNEXPECTED, TYPE_REMOTE}) + @IntDef({TYPE_SOURCE, TYPE_RENDERER, TYPE_UNEXPECTED, TYPE_REMOTE, TYPE_OUT_OF_MEMORY}) public @interface Type {} /** * The error occurred loading data from a {@link MediaSource}. @@ -61,10 +61,12 @@ public final class ExoPlaybackException extends Exception { *

Call {@link #getMessage()} to retrieve the message associated with the error. */ public static final int TYPE_REMOTE = 3; + /** The error was an {@link OutOfMemoryError}. */ + public static final int TYPE_OUT_OF_MEMORY = 4; /** - * The type of the playback failure. One of {@link #TYPE_SOURCE}, {@link #TYPE_RENDERER} and - * {@link #TYPE_UNEXPECTED}. + * The type of the playback failure. One of {@link #TYPE_SOURCE}, {@link #TYPE_RENDERER}, {@link + * #TYPE_UNEXPECTED}, {@link #TYPE_REMOTE} and {@link #TYPE_OUT_OF_MEMORY}. */ @Type public final int type; @@ -82,7 +84,7 @@ public final class ExoPlaybackException extends Exception { * @return The created instance. */ public static ExoPlaybackException createForSource(IOException cause) { - return new ExoPlaybackException(TYPE_SOURCE, cause, C.INDEX_UNSET); + return new ExoPlaybackException(TYPE_SOURCE, cause, /* rendererIndex= */ C.INDEX_UNSET); } /** @@ -103,7 +105,7 @@ public final class ExoPlaybackException extends Exception { * @return The created instance. */ /* package */ static ExoPlaybackException createForUnexpected(RuntimeException cause) { - return new ExoPlaybackException(TYPE_UNEXPECTED, cause, C.INDEX_UNSET); + return new ExoPlaybackException(TYPE_UNEXPECTED, cause, /* rendererIndex= */ C.INDEX_UNSET); } /** @@ -116,6 +118,16 @@ public final class ExoPlaybackException extends Exception { return new ExoPlaybackException(TYPE_REMOTE, message); } + /** + * Creates an instance of type {@link #TYPE_OUT_OF_MEMORY}. + * + * @param cause The cause of the failure. + * @return The created instance. + */ + /* package */ static ExoPlaybackException createForOutOfMemoryError(OutOfMemoryError cause) { + return new ExoPlaybackException(TYPE_OUT_OF_MEMORY, cause, /* rendererIndex= */ C.INDEX_UNSET); + } + private ExoPlaybackException(@Type int type, Throwable cause, int rendererIndex) { super(cause); this.type = type; @@ -160,4 +172,13 @@ public final class ExoPlaybackException extends Exception { return (RuntimeException) Assertions.checkNotNull(cause); } + /** + * Retrieves the underlying error when {@link #type} is {@link #TYPE_OUT_OF_MEMORY}. + * + * @throws IllegalStateException If {@link #type} is not {@link #TYPE_OUT_OF_MEMORY}. + */ + public OutOfMemoryError getOutOfMemoryError() { + Assertions.checkState(type == TYPE_OUT_OF_MEMORY); + return (OutOfMemoryError) Assertions.checkNotNull(cause); + } } diff --git a/library/core/src/main/java/com/google/android/exoplayer2/ExoPlayerImplInternal.java b/library/core/src/main/java/com/google/android/exoplayer2/ExoPlayerImplInternal.java index d9113e6b64..8bdd12c049 100644 --- a/library/core/src/main/java/com/google/android/exoplayer2/ExoPlayerImplInternal.java +++ b/library/core/src/main/java/com/google/android/exoplayer2/ExoPlayerImplInternal.java @@ -389,14 +389,17 @@ import java.util.concurrent.atomic.AtomicBoolean; /* acknowledgeStop= */ false); eventHandler.obtainMessage(MSG_ERROR, ExoPlaybackException.createForSource(e)).sendToTarget(); maybeNotifyPlaybackInfoChanged(); - } catch (RuntimeException e) { + } catch (RuntimeException | OutOfMemoryError e) { Log.e(TAG, "Internal runtime error.", e); stopInternal( /* forceResetRenderers= */ true, /* resetPositionAndState= */ false, /* acknowledgeStop= */ false); - eventHandler.obtainMessage(MSG_ERROR, ExoPlaybackException.createForUnexpected(e)) - .sendToTarget(); + ExoPlaybackException error = + e instanceof OutOfMemoryError + ? ExoPlaybackException.createForOutOfMemoryError((OutOfMemoryError) e) + : ExoPlaybackException.createForUnexpected((RuntimeException) e); + eventHandler.obtainMessage(MSG_ERROR, error).sendToTarget(); maybeNotifyPlaybackInfoChanged(); } return true;