Specify which function is triggering a TimeoutException

PiperOrigin-RevId: 326666629
This commit is contained in:
olly 2020-08-14 17:07:47 +01:00 committed by kim-vde
parent 977f0d1ed0
commit 94b66f4c24
2 changed files with 92 additions and 12 deletions

View File

@ -27,6 +27,7 @@ import java.io.IOException;
import java.lang.annotation.Documented;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.util.concurrent.TimeoutException;
/**
* Thrown when a non-recoverable playback failure occurs.
@ -35,12 +36,20 @@ public final class ExoPlaybackException extends Exception {
/**
* The type of source that produced the error. One of {@link #TYPE_SOURCE}, {@link #TYPE_RENDERER}
* {@link #TYPE_UNEXPECTED}, {@link #TYPE_REMOTE} or {@link #TYPE_OUT_OF_MEMORY}. Note that new
* types may be added in the future and error handling should handle unknown type values.
* {@link #TYPE_UNEXPECTED}, {@link #TYPE_REMOTE}, {@link #TYPE_OUT_OF_MEMORY} or {@link
* #TYPE_TIMEOUT}. Note that new types may be added in the future and error handling should handle
* unknown type values.
*/
@Documented
@Retention(RetentionPolicy.SOURCE)
@IntDef({TYPE_SOURCE, TYPE_RENDERER, TYPE_UNEXPECTED, TYPE_REMOTE, TYPE_OUT_OF_MEMORY})
@IntDef({
TYPE_SOURCE,
TYPE_RENDERER,
TYPE_UNEXPECTED,
TYPE_REMOTE,
TYPE_OUT_OF_MEMORY,
TYPE_TIMEOUT
})
public @interface Type {}
/**
* The error occurred loading data from a {@link MediaSource}.
@ -68,10 +77,34 @@ public final class ExoPlaybackException extends Exception {
public static final int TYPE_REMOTE = 3;
/** The error was an {@link OutOfMemoryError}. */
public static final int TYPE_OUT_OF_MEMORY = 4;
/** The error was a {@link TimeoutException}. */
public static final int TYPE_TIMEOUT = 5;
/** The {@link Type} of the playback failure. */
@Type public final int type;
/**
* The operation which produced the timeout error. One of {@link #TIMEOUT_OPERATION_RELEASE},
* {@link #TIMEOUT_OPERATION_SET_FOREGROUND_MODE} or {@link #TIMEOUT_OPERATION_UNDEFINED}. Note
* that new operations may be added in the future and error handling should handle unknown
* operation values.
*/
@Documented
@Retention(RetentionPolicy.SOURCE)
@IntDef({
TIMEOUT_OPERATION_UNDEFINED,
TIMEOUT_OPERATION_RELEASE,
TIMEOUT_OPERATION_SET_FOREGROUND_MODE
})
public @interface TimeoutOperation {}
/** The operation where this error occurred is not defined. */
public static final int TIMEOUT_OPERATION_UNDEFINED = 0;
/** The error occurred in {@link ExoPlayer#release}. */
public static final int TIMEOUT_OPERATION_RELEASE = 1;
/** The error occurred in {@link ExoPlayer#setForegroundMode}. */
public static final int TIMEOUT_OPERATION_SET_FOREGROUND_MODE = 2;
/** If {@link #type} is {@link #TYPE_RENDERER}, this is the name of the renderer. */
@Nullable public final String rendererName;
@ -91,6 +124,11 @@ public final class ExoPlaybackException extends Exception {
*/
@FormatSupport public final int rendererFormatSupport;
/**
* If {@link #type} is {@link #TYPE_TIMEOUT}, this is the operation where the timeout happened.
*/
@TimeoutOperation public final int timeoutOperation;
/** The value of {@link SystemClock#elapsedRealtime()} when this exception was created. */
public final long timestampMs;
@ -136,7 +174,8 @@ public final class ExoPlaybackException extends Exception {
rendererName,
rendererIndex,
rendererFormat,
rendererFormat == null ? RendererCapabilities.FORMAT_HANDLED : rendererFormatSupport);
rendererFormat == null ? RendererCapabilities.FORMAT_HANDLED : rendererFormatSupport,
TIMEOUT_OPERATION_UNDEFINED);
}
/**
@ -169,6 +208,26 @@ public final class ExoPlaybackException extends Exception {
return new ExoPlaybackException(TYPE_OUT_OF_MEMORY, cause);
}
/**
* Creates an instance of type {@link #TYPE_TIMEOUT}.
*
* @param cause The cause of the failure.
* @param timeoutOperation The operation that caused this timeout.
* @return The created instance.
*/
public static ExoPlaybackException createForTimeoutError(
TimeoutException cause, @TimeoutOperation int timeoutOperation) {
return new ExoPlaybackException(
TYPE_OUT_OF_MEMORY,
cause,
/* customMessage= */ null,
/* rendererName= */ null,
/* rendererIndex= */ C.INDEX_UNSET,
/* rendererFormat= */ null,
/* rendererFormatSupport= */ RendererCapabilities.FORMAT_HANDLED,
timeoutOperation);
}
private ExoPlaybackException(@Type int type, Throwable cause) {
this(
type,
@ -177,7 +236,8 @@ public final class ExoPlaybackException extends Exception {
/* rendererName= */ null,
/* rendererIndex= */ C.INDEX_UNSET,
/* rendererFormat= */ null,
/* rendererFormatSupport= */ RendererCapabilities.FORMAT_HANDLED);
/* rendererFormatSupport= */ RendererCapabilities.FORMAT_HANDLED,
TIMEOUT_OPERATION_UNDEFINED);
}
private ExoPlaybackException(@Type int type, String message) {
@ -188,7 +248,8 @@ public final class ExoPlaybackException extends Exception {
/* rendererName= */ null,
/* rendererIndex= */ C.INDEX_UNSET,
/* rendererFormat= */ null,
/* rendererFormatSupport= */ RendererCapabilities.FORMAT_HANDLED);
/* rendererFormatSupport= */ RendererCapabilities.FORMAT_HANDLED,
/* timeoutOperation= */ TIMEOUT_OPERATION_UNDEFINED);
}
private ExoPlaybackException(
@ -198,7 +259,8 @@ public final class ExoPlaybackException extends Exception {
@Nullable String rendererName,
int rendererIndex,
@Nullable Format rendererFormat,
@FormatSupport int rendererFormatSupport) {
@FormatSupport int rendererFormatSupport,
@TimeoutOperation int timeoutOperation) {
this(
deriveMessage(
type,
@ -214,6 +276,7 @@ public final class ExoPlaybackException extends Exception {
rendererFormat,
rendererFormatSupport,
/* mediaPeriodId= */ null,
timeoutOperation,
/* timestampMs= */ SystemClock.elapsedRealtime());
}
@ -226,6 +289,7 @@ public final class ExoPlaybackException extends Exception {
@Nullable Format rendererFormat,
@FormatSupport int rendererFormatSupport,
@Nullable MediaSource.MediaPeriodId mediaPeriodId,
@TimeoutOperation int timeoutOperation,
long timestampMs) {
super(message, cause);
this.type = type;
@ -235,6 +299,7 @@ public final class ExoPlaybackException extends Exception {
this.rendererFormat = rendererFormat;
this.rendererFormatSupport = rendererFormatSupport;
this.mediaPeriodId = mediaPeriodId;
this.timeoutOperation = timeoutOperation;
this.timestampMs = timestampMs;
}
@ -278,6 +343,16 @@ public final class ExoPlaybackException extends Exception {
return (OutOfMemoryError) Assertions.checkNotNull(cause);
}
/**
* Retrieves the underlying error when {@link #type} is {@link #TYPE_TIMEOUT}.
*
* @throws IllegalStateException If {@link #type} is not {@link #TYPE_TIMEOUT}.
*/
public TimeoutException getTimeoutError() {
Assertions.checkState(type == TYPE_TIMEOUT);
return (TimeoutException) Assertions.checkNotNull(cause);
}
/**
* Returns a copy of this exception with the provided {@link MediaSource.MediaPeriodId}.
*
@ -296,6 +371,7 @@ public final class ExoPlaybackException extends Exception {
rendererFormat,
rendererFormatSupport,
mediaPeriodId,
timeoutOperation,
timestampMs);
}
@ -329,6 +405,9 @@ public final class ExoPlaybackException extends Exception {
case TYPE_OUT_OF_MEMORY:
message = "Out of memory error";
break;
case TYPE_TIMEOUT:
message = "Timeout error";
break;
case TYPE_UNEXPECTED:
default:
message = "Unexpected runtime error";

View File

@ -683,9 +683,9 @@ import java.util.concurrent.TimeoutException;
notifyListeners(
listener ->
listener.onPlayerError(
ExoPlaybackException.createForUnexpected(
new RuntimeException(
new TimeoutException("Setting foreground mode timed out.")))));
ExoPlaybackException.createForTimeoutError(
new TimeoutException("Setting foreground mode timed out."),
ExoPlaybackException.TIMEOUT_OPERATION_SET_FOREGROUND_MODE)));
}
}
}
@ -724,8 +724,9 @@ import java.util.concurrent.TimeoutException;
notifyListeners(
listener ->
listener.onPlayerError(
ExoPlaybackException.createForUnexpected(
new RuntimeException(new TimeoutException("Player release timed out.")))));
ExoPlaybackException.createForTimeoutError(
new TimeoutException("Player release timed out."),
ExoPlaybackException.TIMEOUT_OPERATION_RELEASE)));
}
playbackInfoUpdateHandler.removeCallbacksAndMessages(null);
if (analyticsCollector != null) {