package a.c.d.i;

import a.a.a.b;
import com.google.common.base.Ascii;
import java.nio.ByteBuffer;
import java.util.Deque;
import java.util.LinkedList;
import java.util.concurrent.BlockingQueue;
import java.util.concurrent.LinkedBlockingQueue;
import java.util.concurrent.atomic.AtomicBoolean;

public class g {
    private static final String f = "VideoQueueManager";
    private static final int g = 512;
    private static final int h = 524288;
    private static final int i = 15;
    private static final int j = 50;
    private static final int k = 100;
    private static final int l = 5;

    /* renamed from: a  reason: collision with root package name */
    private final BlockingQueue<e> f159a = new LinkedBlockingQueue(512);
    private long b = 0;
    private final Deque<e> c = new LinkedList();
    private final AtomicBoolean d = new AtomicBoolean(false);
    private String e;

    private int a(int i2) {
        int i3 = i2 - 1;
        int i4 = i3 | (i3 >>> 1);
        int i5 = i4 | (i4 >>> 2);
        int i6 = i5 | (i5 >>> 4);
        int i7 = i6 | (i6 >>> 8);
        int i8 = i7 | (i7 >>> 16);
        if (i8 < 0) {
            return 1;
        }
        return 1 + i8;
    }

    private boolean a(ByteBuffer byteBuffer) {
        byte b2;
        if (byteBuffer == null || byteBuffer.remaining() < 5) {
            return false;
        }
        if (f.z.equals(this.e)) {
            if (byteBuffer.get(0) != 0 || byteBuffer.get(1) != 0) {
                return false;
            }
            if (byteBuffer.get(2) == 1) {
                b2 = byteBuffer.get(3);
            } else if (byteBuffer.get(2) != 0 || byteBuffer.get(3) != 1) {
                return false;
            } else {
                b2 = byteBuffer.get(4);
            }
            int i2 = b2 & Ascii.US;
            return i2 == 7 || i2 == 5;
        } else if (!f.A.equals(this.e)) {
            return true;
        } else {
            if (byteBuffer.get(0) != 0 || byteBuffer.get(1) != 0 || byteBuffer.get(2) != 0 || byteBuffer.get(3) != 1) {
                return false;
            }
            int i3 = (byteBuffer.get(4) & 126) >> 1;
            return i3 == 32 || i3 == 33 || (i3 >= 15 && i3 <= 21);
        }
    }

    public void a() {
        this.f159a.clear();
        this.d.set(false);
        this.b = 0;
    }

    public synchronized void a(e eVar) {
        if (eVar.a().capacity() <= 524288) {
            if (this.c.size() < 15) {
                this.c.push(eVar);
            }
        }
    }

    public void a(String str) {
        this.e = str;
    }

    public synchronized void a(ByteBuffer byteBuffer, long j2) {
        e eVar;
        long j3 = this.b;
        this.b = 1 + j3;
        if (j3 < 0) {
            this.b = 0;
        }
        try {
            if (this.b % 100 == 0 || (this.f159a.size() > 0 && this.f159a.size() % 5 == 0)) {
                b.d(f, "buffer queue size: " + this.f159a.size() + ", total: " + this.b);
            }
            boolean a2 = a(byteBuffer);
            if (a2 && this.d.get() && this.f159a.size() >= 50) {
                b.e(f, "we need clear queue while first key frame was in the decode queue and the buffer queue is too large");
                this.f159a.clear();
            }
            int remaining = byteBuffer.remaining();
            if (this.c.isEmpty()) {
                eVar = new e(ByteBuffer.allocate(a(remaining)), j2, a2);
                b.a(f, "allocate new buffer: " + remaining);
            } else {
                eVar = this.c.pop();
                eVar.a(j2, a2);
                if (eVar.a().capacity() < remaining) {
                    int max = Math.max(Math.min(a(remaining), 524288), remaining);
                    b.a(f, "allocate new buffer: " + eVar.a().capacity() + " < " + remaining + ", alloc " + max);
                    eVar.a(ByteBuffer.allocate(max), j2, a2);
                }
            }
            eVar.a().clear();
            eVar.a().limit(remaining);
            eVar.a().put(byteBuffer);
            eVar.a().flip();
            this.f159a.put(eVar);
        } catch (InterruptedException e2) {
            b.b(f, "Put data to buffer queue error.", e2);
        }
    }

    public void b() {
        if (this.f159a.size() >= 511) {
            this.d.set(false);
            this.f159a.clear();
        }
    }

    public e c() {
        try {
            e take = this.f159a.take();
            if (take != null && take.b()) {
                this.d.set(true);
            }
            return take;
        } catch (InterruptedException e2) {
            b.b(f, "Get data from buffer queue error.", e2);
            return null;
        }
    }

    public int d() {
        return this.f159a.size();
    }

    public boolean e() {
        return this.f159a.isEmpty();
    }
}
