Skip to content

Commit

Permalink
Bug 1308405 - p5: Memorize sample buffers in CodecProxy. r=snorp
Browse files Browse the repository at this point in the history
Differential Revision: https://phabricator.services.mozilla.com/D24591

--HG--
extra : moz-landing-system : lando
  • Loading branch information
jhlin committed Mar 28, 2019
1 parent c668fd7 commit 364290c
Show file tree
Hide file tree
Showing 3 changed files with 48 additions and 16 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@

import java.io.IOException;
import java.nio.ByteBuffer;
import java.util.HashMap;
import java.util.Map;
import java.util.Queue;
import java.util.concurrent.ConcurrentLinkedQueue;

Expand All @@ -35,6 +37,9 @@ public final class CodecProxy {
private Queue<Sample> mSurfaceOutputs = new ConcurrentLinkedQueue<>();
private boolean mFlushed = true;

private Map<Integer, SampleBuffer> mInputBuffers = new HashMap<>();
private Map<Integer, SampleBuffer> mOutputBuffers = new HashMap<>();

public interface Callbacks {
void onInputStatus(long timestamp, boolean processed);
void onOutputFormatChanged(MediaFormat format);
Expand Down Expand Up @@ -227,11 +232,7 @@ public synchronized boolean input(final ByteBuffer bytes, final BufferInfo info,

try {
Sample s = mRemote.dequeueInput(info.size);
if (bytes != null && info.size > 0) {
SampleBuffer buffer = mRemote.getInputBuffer(s.bufferId);
buffer.readFromByteBuffer(bytes, info.offset, info.size);
buffer.dispose();
}
fillInputBuffer(s.bufferId, bytes, info.offset, info.size);
return sendInput(s.set(info, cryptoInfo));
} catch (RemoteException | NullPointerException e) {
Log.e(LOGTAG, "fail to dequeue input buffer", e);
Expand All @@ -243,6 +244,21 @@ public synchronized boolean input(final ByteBuffer bytes, final BufferInfo info,
return false;
}

private void fillInputBuffer(final int bufferId, final ByteBuffer bytes,
final int offset, final int size) throws RemoteException, IOException {
if (bytes == null || size == 0) {
Log.w(LOGTAG, "empty input");
return;
}

SampleBuffer buffer = mInputBuffers.get(bufferId);
if (buffer == null) {
buffer = mRemote.getInputBuffer(bufferId);
mInputBuffers.put(bufferId, buffer);
}
buffer.readFromByteBuffer(bytes, offset, size);
}

private boolean sendInput(final Sample sample) {
try {
mRemote.queueInput(sample);
Expand Down Expand Up @@ -308,6 +324,15 @@ public boolean release() {
mSurfaceOutputs.clear();
}

for (SampleBuffer b : mInputBuffers.values()) {
b.dispose();
}
mInputBuffers.clear();
for (SampleBuffer b : mOutputBuffers.values()) {
b.dispose();
}
mOutputBuffers.clear();

try {
RemoteManager.getInstance().releaseCodec(this);
} catch (DeadObjectException e) {
Expand Down Expand Up @@ -390,11 +415,16 @@ private SampleBuffer getOutputBuffer(final int id) {
return null;
}

try {
return mRemote.getOutputBuffer(id);
} catch (Exception e) {
Log.e(LOGTAG, "cannot get buffer#" + id, e);
return null;
SampleBuffer buffer = mOutputBuffers.get(id);
if (buffer == null) {
try {
buffer = mRemote.getOutputBuffer(id);
mOutputBuffers.put(id, buffer);
} catch (Exception e) {
Log.e(LOGTAG, "cannot get buffer#" + id, e);
return null;
}
}
return buffer;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@ public void readFromByteBuffer(final ByteBuffer src, final int offset, final int
}
try {
nativeReadFromDirectBuffer(src, mSharedMem.getPointer(), offset, size);
mSharedMem.flush();
} catch (NullPointerException e) {
throw new IOException(e);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -85,16 +85,17 @@ public SharedMemory(final int id, final int size) throws NoSuchMethodException,
}

public void flush() {
if (mBackedFile == null) {
close();
if (!mIsMapped) {
return;
}

unmap(mHandle, mSize);
mHandle = 0;
mIsMapped = false;
}

public void close() {
if (mIsMapped) {
unmap(mHandle, mSize);
mHandle = 0;
}
flush();

if (mDescriptor != null) {
try {
Expand Down

0 comments on commit 364290c

Please sign in to comment.