Create a new UnsynchronizedPopulatedBufferList implementation that can be used for better direct submit performance

This commit is contained in:
Cameron Gutman 2015-03-09 01:35:53 -05:00
parent 83e874cdb6
commit 678269c561
4 changed files with 128 additions and 24 deletions

View File

@ -2,14 +2,15 @@ package com.limelight.nvstream.av.audio;
import com.limelight.LimeLog; import com.limelight.LimeLog;
import com.limelight.nvstream.av.ByteBufferDescriptor; import com.limelight.nvstream.av.ByteBufferDescriptor;
import com.limelight.nvstream.av.PopulatedBufferList;
import com.limelight.nvstream.av.RtpPacket; import com.limelight.nvstream.av.RtpPacket;
import com.limelight.nvstream.av.SequenceHelper; import com.limelight.nvstream.av.SequenceHelper;
import com.limelight.nvstream.av.buffer.AbstractPopulatedBufferList;
import com.limelight.nvstream.av.buffer.AtomicPopulatedBufferList;
public class AudioDepacketizer { public class AudioDepacketizer {
private static final int DU_LIMIT = 30; private static final int DU_LIMIT = 30;
private PopulatedBufferList<ByteBufferDescriptor> decodedUnits; private AbstractPopulatedBufferList<ByteBufferDescriptor> decodedUnits;
// Direct submit state // Direct submit state
private AudioRenderer directSubmitRenderer; private AudioRenderer directSubmitRenderer;
@ -28,7 +29,7 @@ public class AudioDepacketizer {
this.directSubmitData = new byte[OpusDecoder.getMaxOutputShorts()*2]; this.directSubmitData = new byte[OpusDecoder.getMaxOutputShorts()*2];
} }
else { else {
decodedUnits = new PopulatedBufferList<ByteBufferDescriptor>(DU_LIMIT, new PopulatedBufferList.BufferFactory() { decodedUnits = new AtomicPopulatedBufferList<ByteBufferDescriptor>(DU_LIMIT, new AbstractPopulatedBufferList.BufferFactory() {
public Object createFreeBuffer() { public Object createFreeBuffer() {
return new ByteBufferDescriptor(new byte[OpusDecoder.getMaxOutputShorts()*2], 0, OpusDecoder.getMaxOutputShorts()*2); return new ByteBufferDescriptor(new byte[OpusDecoder.getMaxOutputShorts()*2], 0, OpusDecoder.getMaxOutputShorts()*2);
} }

View File

@ -0,0 +1,41 @@
package com.limelight.nvstream.av.buffer;
public abstract class AbstractPopulatedBufferList<T> {
protected final int maxQueueSize;
protected final BufferFactory factory;
public AbstractPopulatedBufferList(int maxQueueSize, BufferFactory factory) {
this.factory = factory;
this.maxQueueSize = maxQueueSize;
}
public abstract int getPopulatedCount();
public abstract int getFreeCount();
public abstract T pollFreeObject();
public abstract void addPopulatedObject(T object);
public abstract void freePopulatedObject(T object);
public void clearPopulatedObjects() {
T object;
while ((object = pollPopulatedObject()) != null) {
freePopulatedObject(object);
}
}
public abstract T pollPopulatedObject();
public abstract T peekPopulatedObject();
public T takePopulatedObject() throws InterruptedException {
throw new UnsupportedOperationException("Blocking is unsupported on this buffer list");
}
public static interface BufferFactory {
public Object createFreeBuffer();
public void cleanupObject(Object o);
}
}

View File

@ -1,16 +1,14 @@
package com.limelight.nvstream.av; package com.limelight.nvstream.av.buffer;
import java.util.concurrent.ArrayBlockingQueue; import java.util.concurrent.ArrayBlockingQueue;
public class PopulatedBufferList<T> { public class AtomicPopulatedBufferList<T> extends AbstractPopulatedBufferList<T> {
private ArrayBlockingQueue<T> populatedList; private final ArrayBlockingQueue<T> populatedList;
private ArrayBlockingQueue<T> freeList; private final ArrayBlockingQueue<T> freeList;
private BufferFactory factory;
@SuppressWarnings("unchecked") @SuppressWarnings("unchecked")
public PopulatedBufferList(int maxQueueSize, BufferFactory factory) { public AtomicPopulatedBufferList(int maxQueueSize, BufferFactory factory) {
this.factory = factory; super(maxQueueSize, factory);
this.populatedList = new ArrayBlockingQueue<T>(maxQueueSize, false); this.populatedList = new ArrayBlockingQueue<T>(maxQueueSize, false);
this.freeList = new ArrayBlockingQueue<T>(maxQueueSize, false); this.freeList = new ArrayBlockingQueue<T>(maxQueueSize, false);
@ -20,48 +18,44 @@ public class PopulatedBufferList<T> {
} }
} }
@Override
public int getPopulatedCount() { public int getPopulatedCount() {
return populatedList.size(); return populatedList.size();
} }
@Override
public int getFreeCount() { public int getFreeCount() {
return freeList.size(); return freeList.size();
} }
@Override
public T pollFreeObject() { public T pollFreeObject() {
return freeList.poll(); return freeList.poll();
} }
@Override
public void addPopulatedObject(T object) { public void addPopulatedObject(T object) {
populatedList.add(object); populatedList.add(object);
} }
@Override
public void freePopulatedObject(T object) { public void freePopulatedObject(T object) {
factory.cleanupObject(object); factory.cleanupObject(object);
freeList.add(object); freeList.add(object);
} }
public void clearPopulatedObjects() { @Override
T object;
while ((object = populatedList.poll()) != null) {
freePopulatedObject(object);
}
}
public T pollPopulatedObject() { public T pollPopulatedObject() {
return populatedList.poll(); return populatedList.poll();
} }
@Override
public T peekPopulatedObject() { public T peekPopulatedObject() {
return populatedList.peek(); return populatedList.peek();
} }
@Override
public T takePopulatedObject() throws InterruptedException { public T takePopulatedObject() throws InterruptedException {
return populatedList.take(); return populatedList.take();
} }
public static interface BufferFactory {
public Object createFreeBuffer();
public void cleanupObject(Object o);
}
} }

View File

@ -0,0 +1,68 @@
package com.limelight.nvstream.av.buffer;
import java.util.ArrayList;
public class UnsynchronizedPopulatedBufferList<T> extends AbstractPopulatedBufferList<T> {
private final ArrayList<T> populatedList;
private final ArrayList<T> freeList;
@SuppressWarnings("unchecked")
public UnsynchronizedPopulatedBufferList(int maxQueueSize, BufferFactory factory) {
super(maxQueueSize, factory);
this.populatedList = new ArrayList<T>(maxQueueSize);
this.freeList = new ArrayList<T>(maxQueueSize);
for (int i = 0; i < maxQueueSize; i++) {
freeList.add((T) factory.createFreeBuffer());
}
}
@Override
public int getPopulatedCount() {
return populatedList.size();
}
@Override
public int getFreeCount() {
return freeList.size();
}
@Override
public T pollFreeObject() {
if (freeList.isEmpty()) {
return null;
}
return freeList.remove(0);
}
@Override
public void addPopulatedObject(T object) {
populatedList.add(object);
}
@Override
public void freePopulatedObject(T object) {
factory.cleanupObject(object);
freeList.add(object);
}
@Override
public T pollPopulatedObject() {
if (populatedList.isEmpty()) {
return null;
}
return populatedList.remove(0);
}
@Override
public T peekPopulatedObject() {
if (populatedList.isEmpty()) {
return null;
}
return populatedList.get(0);
}
}