package androidx.media3.exoplayer.upstream; import android.os.Handler; import android.os.Looper; import android.os.Message; import android.os.SystemClock; import androidx.media3.common.util.Assertions; import androidx.media3.common.util.Log; import androidx.media3.common.util.TraceUtil; import androidx.media3.common.util.Util; import java.io.IOException; import java.util.concurrent.ExecutorService; /* loaded from: classes2.dex */ public final class Loader implements LoaderErrorThrower { private static final int ACTION_TYPE_DONT_RETRY = 2; private static final int ACTION_TYPE_DONT_RETRY_FATAL = 3; private static final int ACTION_TYPE_RETRY = 0; private static final int ACTION_TYPE_RETRY_AND_RESET_ERROR_COUNT = 1; public static final LoadErrorAction DONT_RETRY; public static final LoadErrorAction DONT_RETRY_FATAL; public static final LoadErrorAction RETRY = createRetryAction(false, -9223372036854775807L); public static final LoadErrorAction RETRY_RESET_ERROR_COUNT = createRetryAction(true, -9223372036854775807L); private static final String THREAD_NAME_PREFIX = "ExoPlayer:Loader:"; private LoadTask currentTask; private final ExecutorService downloadExecutorService; private IOException fatalError; /* loaded from: classes2.dex */ public interface Callback { void onLoadCanceled(T t, long j, long j2, boolean z); void onLoadCompleted(T t, long j, long j2); LoadErrorAction onLoadError(T t, long j, long j2, IOException iOException, int i); } /* loaded from: classes2.dex */ public interface Loadable { void cancelLoad(); void load() throws IOException; } /* loaded from: classes2.dex */ public interface ReleaseCallback { void onLoaderReleased(); } public void clearFatalError() { this.fatalError = null; } public boolean hasFatalError() { return this.fatalError != null; } public boolean isLoading() { return this.currentTask != null; } /* loaded from: classes2.dex */ public static final class UnexpectedLoaderException extends IOException { public UnexpectedLoaderException(Throwable th) { super("Unexpected " + th.getClass().getSimpleName() + ": " + th.getMessage(), th); } } static { long j = -9223372036854775807L; DONT_RETRY = new LoadErrorAction(2, j); DONT_RETRY_FATAL = new LoadErrorAction(3, j); } /* loaded from: classes2.dex */ public static final class LoadErrorAction { private final long retryDelayMillis; private final int type; public boolean isRetry() { int i = this.type; return i == 0 || i == 1; } private LoadErrorAction(int i, long j) { this.type = i; this.retryDelayMillis = j; } } public Loader(String str) { this.downloadExecutorService = Util.newSingleThreadExecutor(THREAD_NAME_PREFIX + str); } public static LoadErrorAction createRetryAction(boolean z, long j) { return new LoadErrorAction(z ? 1 : 0, j); } public long startLoading(T t, Callback callback, int i) { Looper looper = (Looper) Assertions.checkStateNotNull(Looper.myLooper()); this.fatalError = null; long elapsedRealtime = SystemClock.elapsedRealtime(); new LoadTask(looper, t, callback, i, elapsedRealtime).start(0L); return elapsedRealtime; } public void cancelLoading() { ((LoadTask) Assertions.checkStateNotNull(this.currentTask)).cancel(false); } public void release() { release(null); } public void release(ReleaseCallback releaseCallback) { LoadTask loadTask = this.currentTask; if (loadTask != null) { loadTask.cancel(true); } if (releaseCallback != null) { this.downloadExecutorService.execute(new ReleaseTask(releaseCallback)); } this.downloadExecutorService.shutdown(); } @Override // androidx.media3.exoplayer.upstream.LoaderErrorThrower public void maybeThrowError() throws IOException { maybeThrowError(Integer.MIN_VALUE); } @Override // androidx.media3.exoplayer.upstream.LoaderErrorThrower public void maybeThrowError(int i) throws IOException { IOException iOException = this.fatalError; if (iOException != null) { throw iOException; } LoadTask loadTask = this.currentTask; if (loadTask != null) { if (i == Integer.MIN_VALUE) { i = loadTask.defaultMinRetryCount; } loadTask.maybeThrowError(i); } } /* JADX INFO: Access modifiers changed from: private */ /* loaded from: classes2.dex */ public final class LoadTask extends Handler implements Runnable { private static final int MSG_FATAL_ERROR = 3; private static final int MSG_FINISH = 1; private static final int MSG_IO_EXCEPTION = 2; private static final int MSG_START = 0; private static final String TAG = "LoadTask"; private Callback callback; private boolean canceled; private IOException currentError; public final int defaultMinRetryCount; private int errorCount; private Thread executorThread; private final T loadable; private volatile boolean released; private final long startTimeMs; public LoadTask(Looper looper, T t, Callback callback, int i, long j) { super(looper); this.loadable = t; this.callback = callback; this.defaultMinRetryCount = i; this.startTimeMs = j; } public void maybeThrowError(int i) throws IOException { IOException iOException = this.currentError; if (iOException != null && this.errorCount > i) { throw iOException; } } public void start(long j) { Assertions.checkState(Loader.this.currentTask == null); Loader.this.currentTask = this; if (j > 0) { sendEmptyMessageDelayed(0, j); } else { execute(); } } public void cancel(boolean z) { this.released = z; this.currentError = null; if (hasMessages(0)) { this.canceled = true; removeMessages(0); if (!z) { sendEmptyMessage(1); } } else { synchronized (this) { this.canceled = true; this.loadable.cancelLoad(); Thread thread = this.executorThread; if (thread != null) { thread.interrupt(); } } } if (z) { finish(); long elapsedRealtime = SystemClock.elapsedRealtime(); ((Callback) Assertions.checkNotNull(this.callback)).onLoadCanceled(this.loadable, elapsedRealtime, elapsedRealtime - this.startTimeMs, true); this.callback = null; } } @Override // java.lang.Runnable public void run() { boolean z; try { synchronized (this) { z = !this.canceled; this.executorThread = Thread.currentThread(); } if (z) { TraceUtil.beginSection("load:" + this.loadable.getClass().getSimpleName()); try { this.loadable.load(); TraceUtil.endSection(); } catch (Throwable th) { TraceUtil.endSection(); throw th; } } synchronized (this) { this.executorThread = null; Thread.interrupted(); } if (this.released) { return; } sendEmptyMessage(1); } catch (IOException e) { if (this.released) { return; } obtainMessage(2, e).sendToTarget(); } catch (Error e2) { if (!this.released) { Log.e(TAG, "Unexpected error loading stream", e2); obtainMessage(3, e2).sendToTarget(); } throw e2; } catch (Exception e3) { if (this.released) { return; } Log.e(TAG, "Unexpected exception loading stream", e3); obtainMessage(2, new UnexpectedLoaderException(e3)).sendToTarget(); } catch (OutOfMemoryError e4) { if (this.released) { return; } Log.e(TAG, "OutOfMemory error loading stream", e4); obtainMessage(2, new UnexpectedLoaderException(e4)).sendToTarget(); } } @Override // android.os.Handler public void handleMessage(Message message) { long retryDelayMillis; if (this.released) { return; } if (message.what == 0) { execute(); return; } if (message.what == 3) { throw ((Error) message.obj); } finish(); long elapsedRealtime = SystemClock.elapsedRealtime(); long j = elapsedRealtime - this.startTimeMs; Callback callback = (Callback) Assertions.checkNotNull(this.callback); if (this.canceled) { callback.onLoadCanceled(this.loadable, elapsedRealtime, j, false); return; } int i = message.what; if (i == 1) { try { callback.onLoadCompleted(this.loadable, elapsedRealtime, j); return; } catch (RuntimeException e) { Log.e(TAG, "Unexpected exception handling load completed", e); Loader.this.fatalError = new UnexpectedLoaderException(e); return; } } if (i != 2) { return; } IOException iOException = (IOException) message.obj; this.currentError = iOException; int i2 = this.errorCount + 1; this.errorCount = i2; LoadErrorAction onLoadError = callback.onLoadError(this.loadable, elapsedRealtime, j, iOException, i2); if (onLoadError.type == 3) { Loader.this.fatalError = this.currentError; } else if (onLoadError.type != 2) { if (onLoadError.type == 1) { this.errorCount = 1; } if (onLoadError.retryDelayMillis != -9223372036854775807L) { retryDelayMillis = onLoadError.retryDelayMillis; } else { retryDelayMillis = getRetryDelayMillis(); } start(retryDelayMillis); } } private void execute() { this.currentError = null; Loader.this.downloadExecutorService.execute((Runnable) Assertions.checkNotNull(Loader.this.currentTask)); } private void finish() { Loader.this.currentTask = null; } private long getRetryDelayMillis() { return Math.min((this.errorCount - 1) * 1000, 5000); } } /* JADX INFO: Access modifiers changed from: private */ /* loaded from: classes2.dex */ public static final class ReleaseTask implements Runnable { private final ReleaseCallback callback; public ReleaseTask(ReleaseCallback releaseCallback) { this.callback = releaseCallback; } @Override // java.lang.Runnable public void run() { this.callback.onLoaderReleased(); } } }