diff --git a/cameraview/src/main/gles/com/otaliastudios/cameraview/Pool.java b/cameraview/src/main/gles/com/otaliastudios/cameraview/Pool.java index 61ebebb3..4429d55c 100644 --- a/cameraview/src/main/gles/com/otaliastudios/cameraview/Pool.java +++ b/cameraview/src/main/gles/com/otaliastudios/cameraview/Pool.java @@ -32,38 +32,44 @@ class Pool { @Nullable T get() { - if (!canGet()) { - LOG.v("GET: Returning null. Too much items requested."); - return null; - } - T buffer = mQueue.poll(); if (buffer != null) { - activeCount++; - LOG.v("GET: Reusing recycled item. Count", count(), "Active", activeCount(), "Cached", cachedCount()); + activeCount++; // poll decreases, this fixes + LOG.v("GET: Reusing recycled item.", this); return buffer; } + if (!canGet()) { + LOG.v("GET: Returning null. Too much items requested.", this); + return null; + } + activeCount++; - LOG.v("GET: Creating a new item. Count", count(), "Active", activeCount(), "Cached", cachedCount()); + LOG.v("GET: Creating a new item.", this); return factory.create(); } void recycle(@NonNull T item) { - LOG.v("RECYCLE: Recycling item. Count", count(), "Active", activeCount(), "Cached", cachedCount()); + LOG.v("RECYCLE: Recycling item.", this); if (--activeCount < 0) { throw new IllegalStateException("Trying to recycle an item which makes activeCount < 0." + "This means that this or some previous items being recycled were not coming from " + - "this pool, or some item was recycled more than once."); + "this pool, or some item was recycled more than once. " + this); } if (!mQueue.offer(item)) { throw new IllegalStateException("Trying to recycle an item while the queue is full. " + "This means that this or some previous items being recycled were not coming from " + - "this pool, or some item was recycled more than once."); + "this pool, or some item was recycled more than once. " + this); } } + @NonNull + @Override + public String toString() { + return getClass().getSimpleName() + " -- count:" + count() + ", active:" + activeCount() + ", cached:" + cachedCount(); + } + final int count() { return activeCount() + cachedCount(); } diff --git a/cameraview/src/main/gles/com/otaliastudios/cameraview/TextureMediaEncoder.java b/cameraview/src/main/gles/com/otaliastudios/cameraview/TextureMediaEncoder.java index 968afdaa..6c40b58a 100644 --- a/cameraview/src/main/gles/com/otaliastudios/cameraview/TextureMediaEncoder.java +++ b/cameraview/src/main/gles/com/otaliastudios/cameraview/TextureMediaEncoder.java @@ -42,7 +42,7 @@ class TextureMediaEncoder extends VideoMediaEncoder private EglCore mEglCore; private EglWindowSurface mWindow; private EglViewport mViewport; - private Pool mFramePool = new Pool<>(30, new Pool.Factory() { + private Pool mFramePool = new Pool<>(100, new Pool.Factory() { @Override public TextureFrame create() { return new TextureFrame(); @@ -130,6 +130,7 @@ class TextureMediaEncoder extends VideoMediaEncoder mViewport.drawFrame(mConfig.textureId, transform); mWindow.setPresentationTime(frame.timestamp); mWindow.swapBuffers(); + mFramePool.recycle(frame); } @Override