1 /*
2  * Copyright (C) 2016 The Android Open Source Project
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 package org.mozilla.thirdparty.com.google.android.exoplayer2;
17 
18 import android.os.SystemClock;
19 import androidx.annotation.IntDef;
20 import androidx.annotation.Nullable;
21 import org.mozilla.thirdparty.com.google.android.exoplayer2.RendererCapabilities.FormatSupport;
22 import org.mozilla.thirdparty.com.google.android.exoplayer2.source.MediaSource;
23 import org.mozilla.thirdparty.com.google.android.exoplayer2.util.Assertions;
24 import java.io.IOException;
25 import java.lang.annotation.Documented;
26 import java.lang.annotation.Retention;
27 import java.lang.annotation.RetentionPolicy;
28 
29 /**
30  * Thrown when a non-recoverable playback failure occurs.
31  */
32 public final class ExoPlaybackException extends Exception {
33 
34   /**
35    * The type of source that produced the error. One of {@link #TYPE_SOURCE}, {@link #TYPE_RENDERER}
36    * {@link #TYPE_UNEXPECTED}, {@link #TYPE_REMOTE} or {@link #TYPE_OUT_OF_MEMORY}. Note that new
37    * types may be added in the future and error handling should handle unknown type values.
38    */
39   @Documented
40   @Retention(RetentionPolicy.SOURCE)
41   @IntDef({TYPE_SOURCE, TYPE_RENDERER, TYPE_UNEXPECTED, TYPE_REMOTE, TYPE_OUT_OF_MEMORY})
42   public @interface Type {}
43   /**
44    * The error occurred loading data from a {@link MediaSource}.
45    * <p>
46    * Call {@link #getSourceException()} to retrieve the underlying cause.
47    */
48   public static final int TYPE_SOURCE = 0;
49   /**
50    * The error occurred in a {@link Renderer}.
51    * <p>
52    * Call {@link #getRendererException()} to retrieve the underlying cause.
53    */
54   public static final int TYPE_RENDERER = 1;
55   /**
56    * The error was an unexpected {@link RuntimeException}.
57    * <p>
58    * Call {@link #getUnexpectedException()} to retrieve the underlying cause.
59    */
60   public static final int TYPE_UNEXPECTED = 2;
61   /**
62    * The error occurred in a remote component.
63    *
64    * <p>Call {@link #getMessage()} to retrieve the message associated with the error.
65    */
66   public static final int TYPE_REMOTE = 3;
67   /** The error was an {@link OutOfMemoryError}. */
68   public static final int TYPE_OUT_OF_MEMORY = 4;
69 
70   /** The {@link Type} of the playback failure. */
71   @Type public final int type;
72 
73   /**
74    * If {@link #type} is {@link #TYPE_RENDERER}, this is the index of the renderer.
75    */
76   public final int rendererIndex;
77 
78   /**
79    * If {@link #type} is {@link #TYPE_RENDERER}, this is the {@link Format} the renderer was using
80    * at the time of the exception, or null if the renderer wasn't using a {@link Format}.
81    */
82   @Nullable public final Format rendererFormat;
83 
84   /**
85    * If {@link #type} is {@link #TYPE_RENDERER}, this is the level of {@link FormatSupport} of the
86    * renderer for {@link #rendererFormat}. If {@link #rendererFormat} is null, this is {@link
87    * RendererCapabilities#FORMAT_HANDLED}.
88    */
89   @FormatSupport public final int rendererFormatSupport;
90 
91   /** The value of {@link SystemClock#elapsedRealtime()} when this exception was created. */
92   public final long timestampMs;
93 
94   @Nullable private final Throwable cause;
95 
96   /**
97    * Creates an instance of type {@link #TYPE_SOURCE}.
98    *
99    * @param cause The cause of the failure.
100    * @return The created instance.
101    */
createForSource(IOException cause)102   public static ExoPlaybackException createForSource(IOException cause) {
103     return new ExoPlaybackException(TYPE_SOURCE, cause);
104   }
105 
106   /**
107    * Creates an instance of type {@link #TYPE_RENDERER}.
108    *
109    * @param cause The cause of the failure.
110    * @param rendererIndex The index of the renderer in which the failure occurred.
111    * @param rendererFormat The {@link Format} the renderer was using at the time of the exception,
112    *     or null if the renderer wasn't using a {@link Format}.
113    * @param rendererFormatSupport The {@link FormatSupport} of the renderer for {@code
114    *     rendererFormat}. Ignored if {@code rendererFormat} is null.
115    * @return The created instance.
116    */
createForRenderer( Exception cause, int rendererIndex, @Nullable Format rendererFormat, @FormatSupport int rendererFormatSupport)117   public static ExoPlaybackException createForRenderer(
118       Exception cause,
119       int rendererIndex,
120       @Nullable Format rendererFormat,
121       @FormatSupport int rendererFormatSupport) {
122     return new ExoPlaybackException(
123         TYPE_RENDERER,
124         cause,
125         rendererIndex,
126         rendererFormat,
127         rendererFormat == null ? RendererCapabilities.FORMAT_HANDLED : rendererFormatSupport);
128   }
129 
130   /**
131    * Creates an instance of type {@link #TYPE_UNEXPECTED}.
132    *
133    * @param cause The cause of the failure.
134    * @return The created instance.
135    */
createForUnexpected(RuntimeException cause)136   public static ExoPlaybackException createForUnexpected(RuntimeException cause) {
137     return new ExoPlaybackException(TYPE_UNEXPECTED, cause);
138   }
139 
140   /**
141    * Creates an instance of type {@link #TYPE_REMOTE}.
142    *
143    * @param message The message associated with the error.
144    * @return The created instance.
145    */
createForRemote(String message)146   public static ExoPlaybackException createForRemote(String message) {
147     return new ExoPlaybackException(TYPE_REMOTE, message);
148   }
149 
150   /**
151    * Creates an instance of type {@link #TYPE_OUT_OF_MEMORY}.
152    *
153    * @param cause The cause of the failure.
154    * @return The created instance.
155    */
createForOutOfMemoryError(OutOfMemoryError cause)156   public static ExoPlaybackException createForOutOfMemoryError(OutOfMemoryError cause) {
157     return new ExoPlaybackException(TYPE_OUT_OF_MEMORY, cause);
158   }
159 
ExoPlaybackException(@ype int type, Throwable cause)160   private ExoPlaybackException(@Type int type, Throwable cause) {
161     this(
162         type,
163         cause,
164         /* rendererIndex= */ C.INDEX_UNSET,
165         /* rendererFormat= */ null,
166         /* rendererFormatSupport= */ RendererCapabilities.FORMAT_HANDLED);
167   }
168 
ExoPlaybackException( @ype int type, Throwable cause, int rendererIndex, @Nullable Format rendererFormat, @FormatSupport int rendererFormatSupport)169   private ExoPlaybackException(
170       @Type int type,
171       Throwable cause,
172       int rendererIndex,
173       @Nullable Format rendererFormat,
174       @FormatSupport int rendererFormatSupport) {
175     super(cause);
176     this.type = type;
177     this.cause = cause;
178     this.rendererIndex = rendererIndex;
179     this.rendererFormat = rendererFormat;
180     this.rendererFormatSupport = rendererFormatSupport;
181     timestampMs = SystemClock.elapsedRealtime();
182   }
183 
ExoPlaybackException(@ype int type, String message)184   private ExoPlaybackException(@Type int type, String message) {
185     super(message);
186     this.type = type;
187     rendererIndex = C.INDEX_UNSET;
188     rendererFormat = null;
189     rendererFormatSupport = RendererCapabilities.FORMAT_UNSUPPORTED_TYPE;
190     cause = null;
191     timestampMs = SystemClock.elapsedRealtime();
192   }
193 
194   /**
195    * Retrieves the underlying error when {@link #type} is {@link #TYPE_SOURCE}.
196    *
197    * @throws IllegalStateException If {@link #type} is not {@link #TYPE_SOURCE}.
198    */
getSourceException()199   public IOException getSourceException() {
200     Assertions.checkState(type == TYPE_SOURCE);
201     return (IOException) Assertions.checkNotNull(cause);
202   }
203 
204   /**
205    * Retrieves the underlying error when {@link #type} is {@link #TYPE_RENDERER}.
206    *
207    * @throws IllegalStateException If {@link #type} is not {@link #TYPE_RENDERER}.
208    */
getRendererException()209   public Exception getRendererException() {
210     Assertions.checkState(type == TYPE_RENDERER);
211     return (Exception) Assertions.checkNotNull(cause);
212   }
213 
214   /**
215    * Retrieves the underlying error when {@link #type} is {@link #TYPE_UNEXPECTED}.
216    *
217    * @throws IllegalStateException If {@link #type} is not {@link #TYPE_UNEXPECTED}.
218    */
getUnexpectedException()219   public RuntimeException getUnexpectedException() {
220     Assertions.checkState(type == TYPE_UNEXPECTED);
221     return (RuntimeException) Assertions.checkNotNull(cause);
222   }
223 
224   /**
225    * Retrieves the underlying error when {@link #type} is {@link #TYPE_OUT_OF_MEMORY}.
226    *
227    * @throws IllegalStateException If {@link #type} is not {@link #TYPE_OUT_OF_MEMORY}.
228    */
getOutOfMemoryError()229   public OutOfMemoryError getOutOfMemoryError() {
230     Assertions.checkState(type == TYPE_OUT_OF_MEMORY);
231     return (OutOfMemoryError) Assertions.checkNotNull(cause);
232   }
233 }
234