Bug 1308405 - p2: Nodify and recycle sample objects. r=snorp

Differential Revision: https://phabricator.services.mozilla.com/D24588

--HG--
extra : moz-landing-system : lando
This commit is contained in:
John Lin 2019-03-28 18:05:08 +00:00
parent f5eaaf3299
commit 385270e9ab
3 changed files with 69 additions and 102 deletions

View File

@ -90,8 +90,8 @@ import org.mozilla.gecko.gfx.GeckoSurface;
}
Sample dequeued = mDequeuedSamples.remove();
dequeued.info = sample.info;
dequeued.cryptoInfo = sample.cryptoInfo;
dequeued.setBufferInfo(sample.info);
dequeued.setCryptoInfo(sample.cryptoInfo);
queueSample(dequeued);
sample.dispose();

View File

@ -21,8 +21,8 @@ public final class Sample implements Parcelable {
public static final Sample EOS;
static {
BufferInfo eosInfo = new BufferInfo();
eosInfo.set(0, 0, Long.MIN_VALUE, MediaCodec.BUFFER_FLAG_END_OF_STREAM);
EOS = new Sample(null, eosInfo, null);
EOS = new Sample();
EOS.info.set(0, 0, Long.MIN_VALUE, MediaCodec.BUFFER_FLAG_END_OF_STREAM);
}
public interface Buffer extends Parcelable {
@ -32,99 +32,20 @@ public final class Sample implements Parcelable {
void dispose();
}
private static final class ArrayBuffer implements Buffer {
private byte[] mArray;
public static final Creator<ArrayBuffer> CREATOR = new Creator<ArrayBuffer>() {
@Override
public ArrayBuffer createFromParcel(final Parcel in) {
return new ArrayBuffer(in);
}
@Override
public ArrayBuffer[] newArray(final int size) {
return new ArrayBuffer[size];
}
};
private ArrayBuffer(final Parcel in) {
mArray = in.createByteArray();
}
private ArrayBuffer(final byte[] bytes) {
mArray = bytes;
}
@Override
public int describeContents() {
return 0;
}
@Override
public void writeToParcel(final Parcel dest, final int flags) {
dest.writeByteArray(mArray);
}
@Override
public int capacity() {
return mArray != null ? mArray.length : 0;
}
@Override
public void readFromByteBuffer(final ByteBuffer src, final int offset, final int size)
throws IOException {
src.position(offset);
if (mArray == null || mArray.length != size) {
mArray = new byte[size];
}
src.get(mArray, 0, size);
}
@Override
public void writeToByteBuffer(final ByteBuffer dest, final int offset, final int size)
throws IOException {
dest.put(mArray, offset, size);
}
@Override
public void dispose() {
mArray = null;
}
}
public Buffer buffer;
@WrapForJNI
public BufferInfo info;
public BufferInfo info = new BufferInfo();
public CryptoInfo cryptoInfo;
public static Sample create() {
return create(null, new BufferInfo(), null);
}
// Simple Linked list for recycling objects.
// Used to nodify Sample objects. Do not marshal/unmarshal.
private Sample mNext;
private static Sample sPool = new Sample();
private static int sPoolSize = 1;
public static Sample create(final ByteBuffer src, final BufferInfo info,
final CryptoInfo cryptoInfo) {
ArrayBuffer buffer = new ArrayBuffer(byteArrayFromBuffer(src, info.offset, info.size));
BufferInfo bufferInfo = new BufferInfo();
bufferInfo.set(0, info.size, info.presentationTimeUs, info.flags);
return new Sample(buffer, bufferInfo, cryptoInfo);
}
public static Sample create(final SharedMemory sharedMem) {
return new Sample(new SharedMemBuffer(sharedMem), new BufferInfo(), null);
}
private Sample(final Buffer bytes, final BufferInfo info, final CryptoInfo cryptoInfo) {
public Sample() { }
public Sample(Buffer bytes) {
buffer = bytes;
this.info = info;
this.cryptoInfo = cryptoInfo;
}
private Sample(final Parcel in) {
readInfo(in);
readCrypto(in);
buffer = in.readParcelable(Sample.class.getClassLoader());
}
private void readInfo(final Parcel in) {
@ -133,7 +54,6 @@ public final class Sample implements Parcelable {
long pts = in.readLong();
int flags = in.readInt();
info = new BufferInfo();
info.set(offset, size, pts, flags);
}
@ -150,7 +70,9 @@ public final class Sample implements Parcelable {
int[] numBytesOfEncryptedData = in.createIntArray();
int numSubSamples = in.readInt();
cryptoInfo = new CryptoInfo();
if (cryptoInfo == null) {
cryptoInfo = new CryptoInfo();
}
cryptoInfo.set(numSubSamples,
numBytesOfClearData,
numBytesOfEncryptedData,
@ -164,12 +86,32 @@ public final class Sample implements Parcelable {
if (bytes != null && info.size > 0) {
buffer.readFromByteBuffer(bytes, info.offset, info.size);
}
this.info.set(0, info.size, info.presentationTimeUs, info.flags);
this.cryptoInfo = cryptoInfo;
setBufferInfo(info);
setCryptoInfo(cryptoInfo);
return this;
}
public void setBufferInfo(final BufferInfo info) {
this.info.set(0, info.size, info.presentationTimeUs, info.flags);
}
public void setCryptoInfo(final CryptoInfo crypto) {
if (crypto == null) {
cryptoInfo = null;
return;
}
if (cryptoInfo == null) {
cryptoInfo = new CryptoInfo();
}
cryptoInfo.set(crypto.numSubSamples,
crypto.numBytesOfClearData,
crypto.numBytesOfEncryptedData,
crypto.key,
crypto.iv,
crypto.mode);
}
public void dispose() {
if (isEOS()) {
return;
@ -179,8 +121,17 @@ public final class Sample implements Parcelable {
buffer.dispose();
buffer = null;
}
info = null;
cryptoInfo = null;
info.set(0, 0, 0, 0);
if (cryptoInfo != null) {
cryptoInfo.set(0, null, null, null, null, 0);
}
// Recycle it.
synchronized (CREATOR) {
this.mNext = sPool;
sPool = this;
sPoolSize++;
}
}
public boolean isEOS() {
@ -191,13 +142,29 @@ public final class Sample implements Parcelable {
public static final Creator<Sample> CREATOR = new Creator<Sample>() {
@Override
public Sample createFromParcel(final Parcel in) {
return new Sample(in);
return obtainSample(in);
}
@Override
public Sample[] newArray(final int size) {
return new Sample[size];
}
private synchronized Sample obtainSample(final Parcel in) {
Sample s = null;
if (sPoolSize > 0) {
s = sPool;
sPool = s.mNext;
s.mNext = null;
sPoolSize--;
} else {
s = new Sample();
}
s.buffer = in.readParcelable(Sample.class.getClassLoader());
s.readInfo(in);
s.readCrypto(in);
return s;
}
};
@Override
@ -207,9 +174,9 @@ public final class Sample implements Parcelable {
@Override
public void writeToParcel(final Parcel dest, final int parcelableFlags) {
dest.writeParcelable(buffer, parcelableFlags);
writeInfo(dest);
writeCrypto(dest);
dest.writeParcelable(buffer, parcelableFlags);
}
private void writeInfo(final Parcel dest) {

View File

@ -38,7 +38,7 @@ final class SamplePool {
}
if (mBufferless) {
return Sample.create();
return new Sample();
} else {
return allocateSharedMemorySample(size);
}
@ -52,7 +52,7 @@ final class SamplePool {
throw new UnsupportedOperationException(e);
}
return Sample.create(shm);
return new Sample(new SharedMemBuffer(shm));
}
private synchronized void recycle(final Sample recycled) {