Fix StackOverflowErrors in AudioMediaEncoder (Fixes #594)

pull/588/head
Mattia Iavarone 6 years ago
parent 7ab98932a5
commit 77f1574b1c
  1. 16
      cameraview/src/main/java/com/otaliastudios/cameraview/video/encoding/AudioMediaEncoder.java

@ -180,13 +180,22 @@ public class AudioMediaEncoder extends MediaEncoder {
LOG.w("Stop was requested. We're out of the loop. Will post an endOfStream."); LOG.w("Stop was requested. We're out of the loop. Will post an endOfStream.");
// Last input with 0 length. This will signal the endOfStream. // Last input with 0 length. This will signal the endOfStream.
// Can't use drain(true); it is only available when writing to the codec InputSurface. // Can't use drain(true); it is only available when writing to the codec InputSurface.
read(true); boolean didReadEos = false;
while (!didReadEos) {
didReadEos = read(true);
}
mAudioRecord.stop(); mAudioRecord.stop();
mAudioRecord.release(); mAudioRecord.release();
mAudioRecord = null; mAudioRecord = null;
} }
private void read(boolean endOfStream) { /**
* Returns true if we found a buffer and could proceed, false if we found no buffer
* so the operation should be performed again by the caller.
* @param endOfStream true if last read
* @return true if proceeded
*/
private boolean read(boolean endOfStream) {
mCurrentBuffer = mByteBufferPool.get(); mCurrentBuffer = mByteBufferPool.get();
if (mCurrentBuffer == null) { if (mCurrentBuffer == null) {
// This can happen and it means that encoding is slow with respect to recording. // This can happen and it means that encoding is slow with respect to recording.
@ -197,11 +206,11 @@ public class AudioMediaEncoder extends MediaEncoder {
// However, if endOfStream, we CAN'T lose this frame! // However, if endOfStream, we CAN'T lose this frame!
if (endOfStream) { if (endOfStream) {
LOG.v("read thread - eos: true - No buffer, retrying."); LOG.v("read thread - eos: true - No buffer, retrying.");
read(true); // try again
} else { } else {
LOG.w("read thread - eos: false - Skipping audio frame, encoding is too slow."); LOG.w("read thread - eos: false - Skipping audio frame, encoding is too slow.");
skipFrames(6); // sleep a bit skipFrames(6); // sleep a bit
} }
return false;
} else { } else {
mCurrentBuffer.clear(); mCurrentBuffer.clear();
// When stereo, we read twice the data here and AudioRecord will fill the buffer // When stereo, we read twice the data here and AudioRecord will fill the buffer
@ -229,6 +238,7 @@ public class AudioMediaEncoder extends MediaEncoder {
} else if (mCurrentReadBytes == AudioRecord.ERROR_BAD_VALUE) { } else if (mCurrentReadBytes == AudioRecord.ERROR_BAD_VALUE) {
LOG.e("read thread - eos:", endOfStream, "- Got AudioRecord.ERROR_BAD_VALUE"); LOG.e("read thread - eos:", endOfStream, "- Got AudioRecord.ERROR_BAD_VALUE");
} }
return true;
} }
} }

Loading…
Cancel
Save