package com.mbridge.msdk.playercommon.exoplayer2.upstream; import android.annotation.SuppressLint; import android.os.Handler; import android.os.Looper; import android.os.Message; import android.os.SystemClock; import android.util.Log; import androidx.annotation.Nullable; import com.mbridge.msdk.playercommon.exoplayer2.util.Assertions; import com.mbridge.msdk.playercommon.exoplayer2.util.TraceUtil; import com.mbridge.msdk.playercommon.exoplayer2.util.Util; import java.io.IOException; import java.lang.annotation.Retention; import java.lang.annotation.RetentionPolicy; import java.util.concurrent.ExecutorService; /* loaded from: classes4.dex */ public final class Loader implements LoaderErrorThrower { public static final int DONT_RETRY = 2; public static final int DONT_RETRY_FATAL = 3; public static final int RETRY = 0; public static final int RETRY_RESET_ERROR_COUNT = 1; private LoadTask currentTask; private final ExecutorService downloadExecutorService; private IOException fatalError; public interface Callback { void onLoadCanceled(T t, long j, long j2, boolean z); void onLoadCompleted(T t, long j, long j2); int onLoadError(T t, long j, long j2, IOException iOException); } public interface Loadable { void cancelLoad(); void load() throws IOException, InterruptedException; } public interface ReleaseCallback { void onLoaderReleased(); } @Retention(RetentionPolicy.SOURCE) public @interface RetryAction { } public final boolean isLoading() { return this.currentTask != null; } public static final class UnexpectedLoaderException extends IOException { public UnexpectedLoaderException(Throwable th) { super("Unexpected " + th.getClass().getSimpleName() + ": " + th.getMessage(), th); } } public Loader(String str) { this.downloadExecutorService = Util.newSingleThreadExecutor(str); } public final long startLoading(T t, Callback callback, int i) { Looper myLooper = Looper.myLooper(); Assertions.checkState(myLooper != null); this.fatalError = null; long elapsedRealtime = SystemClock.elapsedRealtime(); new LoadTask(myLooper, t, callback, i, elapsedRealtime).start(0L); return elapsedRealtime; } public final void cancelLoading() { this.currentTask.cancel(false); } public final void release() { release(null); } public final void release(@Nullable 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 // com.mbridge.msdk.playercommon.exoplayer2.upstream.LoaderErrorThrower public final void maybeThrowError() throws IOException { maybeThrowError(Integer.MIN_VALUE); } @Override // com.mbridge.msdk.playercommon.exoplayer2.upstream.LoaderErrorThrower public final 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); } } @SuppressLint({"HandlerLeak"}) public final class LoadTask extends Handler implements Runnable { private static final int MSG_CANCEL = 1; private static final int MSG_END_OF_SOURCE = 2; private static final int MSG_FATAL_ERROR = 4; private static final int MSG_IO_EXCEPTION = 3; private static final int MSG_START = 0; private static final String TAG = "LoadTask"; @Nullable private Callback callback; private volatile boolean canceled; private IOException currentError; public final int defaultMinRetryCount; private int errorCount; private volatile 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 final void maybeThrowError(int i) throws IOException { IOException iOException = this.currentError; if (iOException != null && this.errorCount > i) { throw iOException; } } public final void start(long j) { Assertions.checkState(Loader.this.currentTask == null); Loader.this.currentTask = this; if (j > 0) { sendEmptyMessageDelayed(0, j); } else { execute(); } } public final void cancel(boolean z) { this.released = z; this.currentError = null; if (hasMessages(0)) { removeMessages(0); if (!z) { sendEmptyMessage(1); } } else { this.canceled = true; this.loadable.cancelLoad(); if (this.executorThread != null) { this.executorThread.interrupt(); } } if (z) { finish(); long elapsedRealtime = SystemClock.elapsedRealtime(); this.callback.onLoadCanceled(this.loadable, elapsedRealtime, elapsedRealtime - this.startTimeMs, true); this.callback = null; } } @Override // java.lang.Runnable public final void run() { try { this.executorThread = Thread.currentThread(); if (!this.canceled) { TraceUtil.beginSection("load:" + this.loadable.getClass().getSimpleName()); try { this.loadable.load(); TraceUtil.endSection(); } catch (Throwable th) { TraceUtil.endSection(); throw th; } } if (this.released) { return; } sendEmptyMessage(2); } catch (IOException e) { if (this.released) { return; } obtainMessage(3, e).sendToTarget(); } catch (OutOfMemoryError e2) { Log.e(TAG, "OutOfMemory error loading stream", e2); if (this.released) { return; } obtainMessage(3, new UnexpectedLoaderException(e2)).sendToTarget(); } catch (Error e3) { Log.e(TAG, "Unexpected error loading stream", e3); if (!this.released) { obtainMessage(4, e3).sendToTarget(); } throw e3; } catch (InterruptedException unused) { Assertions.checkState(this.canceled); if (this.released) { return; } sendEmptyMessage(2); } catch (Exception e4) { Log.e(TAG, "Unexpected exception loading stream", e4); if (this.released) { return; } obtainMessage(3, new UnexpectedLoaderException(e4)).sendToTarget(); } } @Override // android.os.Handler public final void handleMessage(Message message) { if (this.released) { return; } int i = message.what; if (i == 0) { execute(); return; } if (i == 4) { throw ((Error) message.obj); } finish(); long elapsedRealtime = SystemClock.elapsedRealtime(); long j = elapsedRealtime - this.startTimeMs; if (this.canceled) { this.callback.onLoadCanceled(this.loadable, elapsedRealtime, j, false); return; } int i2 = message.what; if (i2 == 1) { this.callback.onLoadCanceled(this.loadable, elapsedRealtime, j, false); return; } if (i2 == 2) { try { this.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 (i2 != 3) { return; } IOException iOException = (IOException) message.obj; this.currentError = iOException; int onLoadError = this.callback.onLoadError(this.loadable, elapsedRealtime, j, iOException); if (onLoadError == 3) { Loader.this.fatalError = this.currentError; } else if (onLoadError != 2) { this.errorCount = onLoadError != 1 ? 1 + this.errorCount : 1; start(getRetryDelayMillis()); } } private void execute() { this.currentError = null; Loader.this.downloadExecutorService.execute(Loader.this.currentTask); } private void finish() { Loader.this.currentTask = null; } private long getRetryDelayMillis() { return Math.min((this.errorCount - 1) * 1000, 5000); } } public static final class ReleaseTask implements Runnable { private final ReleaseCallback callback; public ReleaseTask(ReleaseCallback releaseCallback) { this.callback = releaseCallback; } @Override // java.lang.Runnable public final void run() { this.callback.onLoaderReleased(); } } }