Skip to content

Commit 98401e3

Browse files
committed
eos fix
1 parent ab18ff1 commit 98401e3

2 files changed

Lines changed: 28 additions & 15 deletions

File tree

app/src/main/java/com/facebook/encapp/AsyncBufferEncoder.java

Lines changed: 26 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -348,18 +348,26 @@ private void sendEndOfStream() {
348348
return; // Already sent EOS
349349
}
350350

351-
// Get a buffer for EOS
352-
Integer bufferIndex = mAvailableInputBuffers.poll();
353-
if (bufferIndex == null) {
354-
// Wait for a buffer
355-
while (!mStopRequested && bufferIndex == null) {
356-
bufferIndex = mAvailableInputBuffers.poll();
357-
if (bufferIndex == null) {
358-
try {
359-
Thread.sleep(1);
360-
} catch (InterruptedException e) {
361-
return;
351+
// Wait for a buffer to send EOS - with timeout
352+
Integer bufferIndex = null;
353+
long startTime = System.currentTimeMillis();
354+
final long EOS_TIMEOUT_MS = 5000;
355+
356+
while (!mStopRequested && bufferIndex == null) {
357+
bufferIndex = mAvailableInputBuffers.poll();
358+
if (bufferIndex == null) {
359+
if (System.currentTimeMillis() - startTime > EOS_TIMEOUT_MS) {
360+
Log.e(TAG, "Timeout waiting for input buffer to send EOS - forcing completion");
361+
mOutputDone.set(true);
362+
synchronized (mCompletionLock) {
363+
mCompletionLock.notifyAll();
362364
}
365+
return;
366+
}
367+
try {
368+
Thread.sleep(1);
369+
} catch (InterruptedException e) {
370+
return;
363371
}
364372
}
365373
}
@@ -371,6 +379,10 @@ private void sendEndOfStream() {
371379
Log.d(TAG, "Queued EOS at frame " + mInFramesCount);
372380
} catch (IllegalStateException e) {
373381
Log.e(TAG, "Error queueing EOS: " + e.getMessage());
382+
mOutputDone.set(true);
383+
synchronized (mCompletionLock) {
384+
mCompletionLock.notifyAll();
385+
}
374386
}
375387
}
376388
}
@@ -460,10 +472,9 @@ private class AsyncEncoderCallbackHandler extends MediaCodec.Callback {
460472

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

469480
@Override

app/src/main/java/com/facebook/encapp/BufferEncoder.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -331,6 +331,8 @@ public String start() {
331331
int size = -1;
332332
// get the ByteBuffer where we will write the image to encode
333333
ByteBuffer byteBuffer = mCodec.getInputBuffer(index);
334+
Log.d(TAG, "Set runtime");
335+
setRuntimeParameters(mInFramesCount);
334336
while (size < 0 && !input_done) {
335337
try {
336338
size = queueInputBufferEncoder(

0 commit comments

Comments
 (0)