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

import android.annotation.SuppressLint;
import androidx.media3.common.util.UnstableApi;
import java.util.ArrayList;
import java.util.List;
import tv.danmaku.ijk.media.player.misc.ITrackInfo;

/* JADX INFO: loaded from: classes3.dex */
@UnstableApi
public class AbrController {
    private static final int HIGH_BW_COUNT_THRESHOLD = 30;
    public static final int LEVEL_DEBUG = 2;
    public static final int LEVEL_OFF = 0;
    private static final int PEAK_WINDOW_SIZE = 10;
    private static final String TAG = "AbrController";

    @SuppressLint({"StaticFieldLeak"})
    private static volatile AbrController instance = null;
    public static int logLevel = 0;
    private static final long segmentDurationMs = 3000;
    public volatile long bitrateEstimate;
    private long currentBitrate;
    public long rawEstimate;
    private List<ITrackInfo> trackList;
    public volatile long sGlobalLockedBitrate = -2147483647L;
    private volatile long sGlobalSelectBitrate = -2147483647L;
    private final long[] recentBufferWindow = new long[10];
    private int recentBufferCount = 0;
    private long peakBufferMs = 0;
    private final ArrayList<Long> highBandwidthList = new ArrayList<>();
    private int highBandwidthStableCount = 0;
    private boolean bitrateLock = false;

    private long adjustEstimateByRules(long j10) {
        List<ITrackInfo> list = this.trackList;
        if (list == null || list.isEmpty()) {
            return this.rawEstimate;
        }
        int size = this.trackList.size() - 1;
        long bitrate = this.trackList.get(size).getBitrate();
        double d10 = j10 / 3000.0d;
        long j11 = this.currentBitrate;
        if (j11 <= 0) {
            j11 = bitrate;
        }
        long j12 = Long.MAX_VALUE;
        int i10 = size;
        for (int i11 = 0; i11 < this.trackList.size(); i11++) {
            long jAbs = Math.abs(((long) this.trackList.get(i11).getBitrate()) - j11);
            if (jAbs < j12) {
                i10 = i11;
                j12 = jAbs;
            }
        }
        if (d10 <= 2.0d) {
            this.highBandwidthStableCount = 0;
            this.highBandwidthList.clear();
            this.bitrateLock = false;
            return Math.max(80000L, bitrate / 2);
        }
        if (d10 > 2.0d && d10 <= 4.0d) {
            this.highBandwidthStableCount = 0;
            this.highBandwidthList.clear();
            this.bitrateLock = false;
            long j13 = this.rawEstimate;
            if (j13 > 0) {
                long j14 = this.currentBitrate;
                if (j13 >= 2 * j14) {
                    return j14;
                }
            }
            return this.trackList.get(Math.min(1 + i10, size)).getBitrate();
        }
        int i12 = i10;
        long j15 = this.peakBufferMs;
        if (j15 > 0 && j15 - j10 >= 10000 && i12 < size) {
            return this.trackList.get(i12).getBitrate();
        }
        if (i12 == 0) {
            return Math.max(j11, bitrate);
        }
        if (this.bitrateLock) {
            return this.bitrateEstimate;
        }
        if (d10 >= 6.0d && i12 > 0) {
            int iMax = Math.max(0, i12 - 1);
            long bitrate2 = this.trackList.get(iMax).getBitrate();
            long bitrate3 = this.trackList.get(Math.max(0, iMax - 1)).getBitrate();
            this.highBandwidthList.add(Long.valueOf(this.rawEstimate));
            int i13 = this.highBandwidthStableCount + 1;
            this.highBandwidthStableCount = i13;
            if (i13 == 30) {
                int i14 = 0;
                long jLongValue = 0;
                for (int i15 = 30; i14 < i15; i15 = 30) {
                    jLongValue = this.highBandwidthList.get(i14).longValue() + jLongValue;
                    i14++;
                }
                long j16 = jLongValue / 30;
                if (j16 >= 3 * bitrate3) {
                    this.highBandwidthStableCount = 0;
                    this.highBandwidthList.clear();
                    this.bitrateLock = true;
                    return bitrate3;
                }
                if (j16 >= 2 * bitrate2) {
                    this.highBandwidthStableCount = 0;
                    this.highBandwidthList.clear();
                    this.bitrateLock = true;
                    return bitrate2;
                }
                this.highBandwidthList.remove(0);
                this.highBandwidthStableCount--;
            }
        }
        return Math.max(j11, bitrate);
    }

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

    public String getDownloadBitrateStr() {
        List<ITrackInfo> list;
        if (this.sGlobalSelectBitrate != -2147483647L && (list = this.trackList) != null && !list.isEmpty()) {
            for (ITrackInfo iTrackInfo : this.trackList) {
                if (iTrackInfo.getBitrate() == this.sGlobalSelectBitrate) {
                    return iTrackInfo.getWidth() + "x" + iTrackInfo.getHeight() + " @" + (iTrackInfo.getBitrate() / 1000) + " kbps";
                }
            }
        }
        return "";
    }

    public String getsGlobalSelectBitrateStr() {
        return this.sGlobalSelectBitrate < 0 ? "" : String.valueOf(this.sGlobalSelectBitrate);
    }

    public void lockBitrate(long j10) {
        this.sGlobalLockedBitrate = j10;
    }

    public void reset() {
        this.sGlobalLockedBitrate = -2147483647L;
        this.sGlobalSelectBitrate = -2147483647L;
        this.bitrateEstimate = -2147483647L;
        this.trackList = null;
        this.rawEstimate = 0L;
        this.currentBitrate = 0L;
        this.recentBufferCount = 0;
        this.peakBufferMs = 0L;
        this.highBandwidthList.clear();
        this.highBandwidthStableCount = 0;
        this.bitrateLock = false;
    }

    public void setCurrentBitrate(long j10) {
        this.currentBitrate = j10;
        this.bitrateLock = false;
        this.sGlobalSelectBitrate = j10;
    }

    public void setRawEstimate(long j10) {
        this.rawEstimate = j10;
    }

    public void setTrackList(List<ITrackInfo> list) {
        this.trackList = list;
    }

    public void unlockIndex() {
        this.sGlobalLockedBitrate = -2147483647L;
    }

    public synchronized void updateBufferInfo(long j10, long j11) {
        try {
            List<ITrackInfo> list = this.trackList;
            if (list != null && !list.isEmpty() && this.trackList.size() != 1) {
                long j12 = j10 - j11;
                long j13 = 0;
                long jMax = Math.max(0L, j12);
                long[] jArr = this.recentBufferWindow;
                int i10 = this.recentBufferCount;
                jArr[i10 % 10] = jMax;
                int i11 = i10 + 1;
                this.recentBufferCount = i11;
                int iMin = Math.min(i11, 10);
                for (int i12 = 0; i12 < iMin; i12++) {
                    long j14 = this.recentBufferWindow[i12];
                    if (j14 > j13) {
                        j13 = j14;
                    }
                }
                this.peakBufferMs = j13;
                if (this.bitrateLock) {
                    return;
                }
                this.bitrateEstimate = adjustEstimateByRules(jMax);
                return;
            }
            this.bitrateEstimate = this.rawEstimate;
        } catch (Throwable th) {
            throw th;
        }
    }
}
