@@ -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
0 commit comments