Always release output buffer

If rotation changed, the dequeued output buffer was never released. Move
it to a finally block to avoid the leak.
This commit is contained in:
Romain Vimont 2018-02-07 15:50:38 +01:00
parent f39de46a39
commit d5acc8adc5

View file

@ -86,21 +86,26 @@ public class ScreenEncoder implements Device.RotationListener {
MediaCodec.BufferInfo bufferInfo = new MediaCodec.BufferInfo(); MediaCodec.BufferInfo bufferInfo = new MediaCodec.BufferInfo();
while (!checkRotationChanged() && !eof) { while (!checkRotationChanged() && !eof) {
int outputBufferId = codec.dequeueOutputBuffer(bufferInfo, -1); int outputBufferId = codec.dequeueOutputBuffer(bufferInfo, -1);
if (checkRotationChanged()) { try {
// must restart encoding with new size if (checkRotationChanged()) {
break; // must restart encoding with new size
} break;
if (outputBufferId >= 0) { }
ByteBuffer outputBuffer = codec.getOutputBuffer(outputBufferId); if (outputBufferId >= 0) {
while (outputBuffer.hasRemaining()) { ByteBuffer outputBuffer = codec.getOutputBuffer(outputBufferId);
int remaining = outputBuffer.remaining(); while (outputBuffer.hasRemaining()) {
int len = Math.min(buf.length, remaining); int remaining = outputBuffer.remaining();
// the outputBuffer is probably direct (it has no underlying array), and LocalSocket does not expose channels, int len = Math.min(buf.length, remaining);
// so we must copy the data locally to write them manually to the output stream // the outputBuffer is probably direct (it has no underlying array), and LocalSocket does not expose channels,
outputBuffer.get(buf, 0, len); // so we must copy the data locally to write them manually to the output stream
outputStream.write(buf, 0, len); outputBuffer.get(buf, 0, len);
outputStream.write(buf, 0, len);
}
}
} finally {
if (outputBufferId >= 0) {
codec.releaseOutputBuffer(outputBufferId, false);
} }
codec.releaseOutputBuffer(outputBufferId, false);
} }
} }