package tv.danmaku.ijk.media.player.ui;

import android.annotation.SuppressLint;
import android.os.Handler;
import android.os.Looper;
import java.io.File;
import java.lang.ref.WeakReference;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.atomic.AtomicReference;

/* JADX INFO: loaded from: classes3.dex */
public class PreloadManager {
    private static final String HTTPS_PREFIX = "https://";
    private static final String HTTP_PREFIX = "http://";
    private static final String IJK_CACHE_PREFIX = "ijkio:cache:ffio:";

    @SuppressLint({"StaticFieldLeak"})
    private static volatile PreloadManager instance;
    private WeakReference<BasePlayerView> playerRef;
    private String lastPlayUrl = "";
    private String videoPlayId = "";
    public int hardwareDecode = 1;
    public boolean isPreloading = false;
    public String finalUrl = "";
    private final Handler mainHandler = new Handler(Looper.getMainLooper());

    public interface MainCallable<T> {
        T call();
    }

    private PreloadManager() {
    }

    private void applyMediaSource(BasePlayerView basePlayerView, String str, int i10, String str2) {
        String audioLanguage = IjkPlayerRuntime.getAudioLanguage();
        if (!str.startsWith(HTTP_PREFIX) && !str.startsWith(HTTPS_PREFIX)) {
            basePlayerView.setMediaSourceStart(new File(str), str2, i10, audioLanguage);
            return;
        }
        if (basePlayerView instanceof IjkVideoView) {
            str = IJK_CACHE_PREFIX.concat(str);
        }
        basePlayerView.setMediaSource(str, i10, audioLanguage);
    }

    private void clearPlayerReference() {
        WeakReference<BasePlayerView> weakReference = this.playerRef;
        if (weakReference != null) {
            weakReference.clear();
            this.playerRef = null;
        }
    }

    public static PreloadManager getInstance() {
        if (instance == null) {
            synchronized (PreloadManager.class) {
                try {
                    if (instance == null) {
                        instance = new PreloadManager();
                    }
                } finally {
                }
            }
        }
        return instance;
    }

    /* JADX INFO: Access modifiers changed from: private */
    public /* synthetic */ BasePlayerView lambda$createPlayer$0() {
        BasePlayerView ijkVideoView;
        synchronized (this) {
            try {
                WeakReference<BasePlayerView> weakReference = this.playerRef;
                ijkVideoView = weakReference == null ? null : weakReference.get();
                if (ijkVideoView == null) {
                    ijkVideoView = new IjkVideoView(IjkPlayerRuntime.requireApplicationContext());
                    this.playerRef = new WeakReference<>(ijkVideoView);
                }
            } finally {
            }
        }
        return ijkVideoView;
    }

    /* JADX INFO: Access modifiers changed from: private */
    public /* synthetic */ Object lambda$releasePreload$2() {
        WeakReference<BasePlayerView> weakReference = this.playerRef;
        BasePlayerView basePlayerView = weakReference != null ? weakReference.get() : null;
        if (basePlayerView != null) {
            resetPlayer(basePlayerView);
        }
        clearPlayerReference();
        return null;
    }

    /* JADX INFO: Access modifiers changed from: private */
    public static /* synthetic */ void lambda$runOnMainSync$3(AtomicReference atomicReference, MainCallable mainCallable, AtomicReference atomicReference2, CountDownLatch countDownLatch) {
        try {
            try {
                atomicReference.set(mainCallable.call());
            } catch (RuntimeException e10) {
                atomicReference2.set(e10);
            }
        } finally {
            countDownLatch.countDown();
        }
    }

    /* JADX INFO: Access modifiers changed from: private */
    public /* synthetic */ Object lambda$setVideoPlayer$1(BasePlayerView basePlayerView, String str, int i10, String str2) {
        clearPlayerReference();
        this.playerRef = new WeakReference<>(basePlayerView);
        resetPlayer(basePlayerView);
        basePlayerView.setPlayer(1);
        basePlayerView.decode(this.hardwareDecode);
        basePlayerView.setResizeMode(IjkPlayerRuntime.getVodVideoRatio(0));
        this.finalUrl = str;
        applyMediaSource(basePlayerView, str, i10, str2);
        return null;
    }

    private void resetPlayer(BasePlayerView basePlayerView) {
        basePlayerView.stop();
        basePlayerView.release();
    }

    private <T> T runOnMainSync(final MainCallable<T> mainCallable) {
        if (Looper.myLooper() == Looper.getMainLooper()) {
            return mainCallable.call();
        }
        final CountDownLatch countDownLatch = new CountDownLatch(1);
        final AtomicReference atomicReference = new AtomicReference();
        final AtomicReference atomicReference2 = new AtomicReference();
        this.mainHandler.post(new Runnable() { // from class: tv.danmaku.ijk.media.player.ui.c
            @Override // java.lang.Runnable
            public final void run() {
                PreloadManager.lambda$runOnMainSync$3(atomicReference, mainCallable, atomicReference2, countDownLatch);
            }
        });
        try {
            countDownLatch.await();
            if (atomicReference2.get() == null) {
                return (T) atomicReference.get();
            }
            throw ((RuntimeException) atomicReference2.get());
        } catch (InterruptedException e10) {
            Thread.currentThread().interrupt();
            throw new RuntimeException(e10);
        }
    }

    public BasePlayerView createPlayer() {
        return (BasePlayerView) runOnMainSync(new d(this, 0));
    }

    public String getFinalUrl() {
        return this.finalUrl;
    }

    public String getLastPlayUrl() {
        return this.lastPlayUrl;
    }

    public BasePlayerView getPlayer() {
        WeakReference<BasePlayerView> weakReference = this.playerRef;
        if (weakReference == null) {
            return null;
        }
        return weakReference.get();
    }

    public String getVideoPlayId() {
        return this.videoPlayId;
    }

    public void preparePlayer(String str, String str2, int i10, String str3) {
        if (str == null || str.isEmpty()) {
            return;
        }
        BasePlayerView basePlayerViewCreatePlayer = createPlayer();
        this.lastPlayUrl = str;
        if (str2 == null) {
            str2 = "";
        }
        this.videoPlayId = str2;
        IjkPlayerRuntime.logDebug("preparePlayer: ".concat(str));
        this.isPreloading = true;
        setVideoPlayer(basePlayerViewCreatePlayer, str, i10, str3);
    }

    public void releasePreload() {
        try {
            runOnMainSync(new d(this, 1));
        } catch (Exception unused) {
        } catch (Throwable th) {
            this.finalUrl = "";
            this.videoPlayId = "";
            this.lastPlayUrl = "";
            this.isPreloading = false;
            throw th;
        }
        this.finalUrl = "";
        this.videoPlayId = "";
        this.lastPlayUrl = "";
        this.isPreloading = false;
    }

    public void setVideoPlayer(final BasePlayerView basePlayerView, final String str, final int i10, final String str2) {
        if (basePlayerView == null) {
            return;
        }
        runOnMainSync(new MainCallable() { // from class: tv.danmaku.ijk.media.player.ui.e
            @Override // tv.danmaku.ijk.media.player.ui.PreloadManager.MainCallable
            public final Object call() {
                return this.f33105a.lambda$setVideoPlayer$1(basePlayerView, str, i10, str2);
            }
        });
    }
}
