From 337d6c2fd351226ede8b7b2b164ffd3d67a6953f Mon Sep 17 00:00:00 2001 From: Romain Vimont Date: Tue, 14 Mar 2023 19:26:37 +0100 Subject: [PATCH] Fail on empty AudioRecord read() If read() returns 0, then there is no data. According to the documentation, it happens if the buffer is not a direct buffer: Refs #3812 --- server/src/main/java/com/genymobile/scrcpy/AudioCapture.java | 2 +- server/src/main/java/com/genymobile/scrcpy/AudioEncoder.java | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/server/src/main/java/com/genymobile/scrcpy/AudioCapture.java b/server/src/main/java/com/genymobile/scrcpy/AudioCapture.java index 9228e3d717..6bb3ce23e1 100644 --- a/server/src/main/java/com/genymobile/scrcpy/AudioCapture.java +++ b/server/src/main/java/com/genymobile/scrcpy/AudioCapture.java @@ -111,7 +111,7 @@ public void stop() { @TargetApi(Build.VERSION_CODES.N) public int read(ByteBuffer directBuffer, int size, MediaCodec.BufferInfo outBufferInfo) { int r = recorder.read(directBuffer, size); - if (r < 0) { + if (r <= 0) { return r; } diff --git a/server/src/main/java/com/genymobile/scrcpy/AudioEncoder.java b/server/src/main/java/com/genymobile/scrcpy/AudioEncoder.java index 24d685c52e..d34598311e 100644 --- a/server/src/main/java/com/genymobile/scrcpy/AudioEncoder.java +++ b/server/src/main/java/com/genymobile/scrcpy/AudioEncoder.java @@ -92,7 +92,7 @@ private void inputThread(MediaCodec mediaCodec, AudioCapture capture) throws IOE InputTask task = inputTasks.take(); ByteBuffer buffer = mediaCodec.getInputBuffer(task.index); int r = capture.read(buffer, READ_SIZE, bufferInfo); - if (r < 0) { + if (r <= 0) { throw new IOException("Could not read audio: " + r); }