package a.c.d.i;

import a.a.a.b;
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.AtomicInteger;

public class c {
    private static final int g = 512;
    private static final int h = 8192;
    private static final int i = 30;
    private static final int j = 100;
    private static final int k = 5;

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

    public c(String str) {
        this.b = "AudioQueueManager-" + str;
    }

    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;
    }

    public void a() {
        this.f153a.clear();
        this.c = 0;
        this.e.set(0);
    }

    public synchronized void a(ByteBuffer byteBuffer) {
        ByteBuffer byteBuffer2;
        long j2 = this.c;
        this.c = 1 + j2;
        if (j2 < 0) {
            this.c = 0;
        }
        try {
            if (this.c % 100 == 0 || (this.f153a.size() > 0 && this.f153a.size() % 5 == 0)) {
                b.d(this.b, "buffer queue size: " + this.f153a.size() + ", total: " + this.c);
            }
            int remaining = byteBuffer.remaining();
            if (this.d.isEmpty()) {
                byteBuffer2 = ByteBuffer.allocate(a(remaining));
                b.a(this.b, "allocate new buffer: " + remaining);
            } else {
                byteBuffer2 = this.d.pop();
                if (byteBuffer2.capacity() < remaining) {
                    int max = Math.max(Math.min(a(remaining), 8192), remaining);
                    b.a(this.b, "allocate new buffer: " + byteBuffer2.capacity() + " < " + remaining + ", alloc " + max);
                    byteBuffer2 = ByteBuffer.allocate(max);
                }
            }
            byteBuffer2.clear();
            byteBuffer2.limit(remaining);
            byteBuffer2.put(byteBuffer);
            byteBuffer2.flip();
            this.f153a.put(byteBuffer2);
            if (this.f153a.size() > 511) {
                ByteBuffer poll = this.f153a.poll();
                if (poll != null) {
                    this.e.addAndGet(-poll.remaining());
                }
                b.b(this.b, "something wrong, the queue have too many buffer");
            }
            this.e.addAndGet(remaining);
            synchronized (this.f) {
                this.f.notifyAll();
            }
        } catch (InterruptedException e2) {
            b.b(this.b, "Put data to buffer queue error.", e2);
        }
    }

    public ByteBuffer b() {
        try {
            ByteBuffer take = this.f153a.take();
            this.e.getAndAdd(-take.remaining());
            return take;
        } catch (InterruptedException e2) {
            b.b(this.b, "Get data from buffer queue error.", e2);
            return null;
        }
    }

    public synchronized void b(ByteBuffer byteBuffer) {
        if (byteBuffer.capacity() <= 8192) {
            if (this.d.size() < 30) {
                this.d.push(byteBuffer);
            }
        }
    }

    public int c() {
        return this.e.get();
    }

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

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

    /* JADX WARNING: Exception block dominator not found, dom blocks: [] */
    /* JADX WARNING: Missing exception handler attribute for start block: B:5:0x000d */
    public void f() {
        synchronized (this.f) {
            this.f.wait(100);
        }
    }
}
