From e207b34c0ffaca4ab66d746081d5819ee61e8fb0 Mon Sep 17 00:00:00 2001 From: Nekojimi Date: Thu, 23 Jun 2022 21:03:10 +0100 Subject: [PATCH] Fix Trackplayer read data retry system, and ensure that read thread never waits for a dead disk thread. --- .../java/moe/nekojimi/chords/TrackPlayer.java | 51 ++++++++++++------- 1 file changed, 34 insertions(+), 17 deletions(-) diff --git a/src/main/java/moe/nekojimi/chords/TrackPlayer.java b/src/main/java/moe/nekojimi/chords/TrackPlayer.java index ec9ac93..9d24f8b 100644 --- a/src/main/java/moe/nekojimi/chords/TrackPlayer.java +++ b/src/main/java/moe/nekojimi/chords/TrackPlayer.java @@ -24,6 +24,7 @@ import org.apache.commons.io.input.buffer.CircularByteBuffer; public class TrackPlayer extends Thread implements Closeable { + private static final boolean DEBUG_PRINT = false; private static final int DESIRED_BUFFER_SIZE = 3840 * 500; private static final int MAX_READ_FAILS = 3; @@ -73,23 +74,30 @@ public class TrackPlayer extends Thread implements Closeable { try { - System.out.println("DISK THREAD: waiting"); - fillBufferWait.wait(); + if (DEBUG_PRINT) + System.out.println("DISK THREAD: waiting"); + fillBufferWait.wait(5000); } catch (InterruptedException ex) { // this is normal } - System.out.println("DISK THREAD: kicked"); + if (DEBUG_PRINT) + System.out.println("DISK THREAD: kicked"); } } try { boolean notAtEnd = fillBuffer(true); if (!notAtEnd) + { + if (DEBUG_PRINT) + System.out.println("DISK THREAD: end of input"); ended = true; + } synchronized (bufferFilledWait) { - System.out.println("DISK THREAD: buffer filled; kicking read thread"); + if (DEBUG_PRINT) + System.out.println("DISK THREAD: buffer filled; kicking read thread"); bufferFilledWait.notifyAll(); } } catch (IOException ex) @@ -98,7 +106,9 @@ public class TrackPlayer extends Thread implements Closeable ended = true; } } - System.out.println("DISK THREAD ENDED"); + if (DEBUG_PRINT) + System.out.println("DISK THREAD ENDED"); + bufferFilledWait.notifyAll(); // kick read thread in case it was waiting for us } public boolean has(int byteCount) @@ -131,26 +141,31 @@ public class TrackPlayer extends Thread implements Closeable { synchronized (fillBufferWait) { - System.out.println("READ THREAD: kicking disk thread"); - fillBufferWait.notifyAll(); // kick the disk thread to fill the buffer if needed + if (!ended) + { + if (DEBUG_PRINT) + System.out.println("READ THREAD: kicking disk thread"); + fillBufferWait.notifyAll(); // kick the disk thread to fill the buffer if needed + } } int bytes; synchronized (audioBuffer) { bytes = audioBuffer.getCurrentNumberOfBytes(); } - if (bytes == 0) + if (bytes == 0 && !ended) { synchronized (bufferFilledWait) { try { System.out.println("READ THREAD: waiting for disk thread"); - bufferFilledWait.wait(); // wait for disk thread to fill the buffer + bufferFilledWait.wait(5000); // wait for disk thread to fill the buffer } catch (InterruptedException ex) { } - System.out.println("READ THREAD: kicked"); + if (DEBUG_PRINT) + System.out.println("READ THREAD: kicked"); } } synchronized (audioBuffer) @@ -172,12 +187,13 @@ public class TrackPlayer extends Thread implements Closeable while (audioBuffer.getCurrentNumberOfBytes() < DESIRED_BUFFER_SIZE) { boolean read = readData(); - if (!read && !canSkip) - return false; - else if (fails < MAX_READ_FAILS) - fails++; - else - return false; + if (!read) + { + if (canSkip && fails < MAX_READ_FAILS) + fails++; + else + return false; + } } return true; } @@ -210,7 +226,8 @@ public class TrackPlayer extends Thread implements Closeable byte[] bytes = new byte[bytesToRead]; // byte[] bytes = din.readNBytes(bytesToRead); int read = input.read(bytes); - System.out.println(" Space:" + space + " Available: " + input.available() + " To read: " + bytesToRead + " Read: " + read); + if (DEBUG_PRINT) + System.out.println(" Space:" + space + " Available: " + input.available() + " To read: " + bytesToRead + " Read: " + read); if (read < 0) return false; // queue.add(bytes);