Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
41 changes: 26 additions & 15 deletions app/src/main/java/com/facebook/encapp/AsyncBufferEncoder.java
Original file line number Diff line number Diff line change
Expand Up @@ -348,18 +348,26 @@ private void sendEndOfStream() {
return; // Already sent EOS
}

// Get a buffer for EOS
Integer bufferIndex = mAvailableInputBuffers.poll();
if (bufferIndex == null) {
// Wait for a buffer
while (!mStopRequested && bufferIndex == null) {
bufferIndex = mAvailableInputBuffers.poll();
if (bufferIndex == null) {
try {
Thread.sleep(1);
} catch (InterruptedException e) {
return;
// Wait for a buffer to send EOS - with timeout
Integer bufferIndex = null;
long startTime = System.currentTimeMillis();
final long EOS_TIMEOUT_MS = 5000;

while (!mStopRequested && bufferIndex == null) {
bufferIndex = mAvailableInputBuffers.poll();
if (bufferIndex == null) {
if (System.currentTimeMillis() - startTime > EOS_TIMEOUT_MS) {
Log.e(TAG, "Timeout waiting for input buffer to send EOS - forcing completion");
mOutputDone.set(true);
synchronized (mCompletionLock) {
mCompletionLock.notifyAll();
}
return;
}
try {
Thread.sleep(1);
} catch (InterruptedException e) {
return;
}
}
}
Expand All @@ -371,6 +379,10 @@ private void sendEndOfStream() {
Log.d(TAG, "Queued EOS at frame " + mInFramesCount);
} catch (IllegalStateException e) {
Log.e(TAG, "Error queueing EOS: " + e.getMessage());
mOutputDone.set(true);
synchronized (mCompletionLock) {
mCompletionLock.notifyAll();
}
}
}
}
Expand Down Expand Up @@ -460,10 +472,9 @@ private class AsyncEncoderCallbackHandler extends MediaCodec.Callback {

@Override
public void onInputBufferAvailable(@NonNull MediaCodec codec, int index) {
// Add buffer to available queue - InputFeeder will use it
if (!mInputDone.get()) {
mAvailableInputBuffers.add(index);
}
// Always add buffers to the queue - InputFeeder needs them for EOS signaling
// even after we stop adding frames
mAvailableInputBuffers.add(index);
}

@Override
Expand Down
2 changes: 2 additions & 0 deletions app/src/main/java/com/facebook/encapp/BufferEncoder.java
Original file line number Diff line number Diff line change
Expand Up @@ -331,6 +331,8 @@ public String start() {
int size = -1;
// get the ByteBuffer where we will write the image to encode
ByteBuffer byteBuffer = mCodec.getInputBuffer(index);
Log.d(TAG, "Set runtime");
setRuntimeParameters(mInFramesCount);
while (size < 0 && !input_done) {
try {
size = queueInputBufferEncoder(
Expand Down