Fix Trackplayer read data retry system, and ensure that read thread never waits for a dead disk thread.

multithread-playback
Nekojimi 3 years ago
parent f8139bd4f6
commit e207b34c0f
  1. 29
      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 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 DESIRED_BUFFER_SIZE = 3840 * 500;
private static final int MAX_READ_FAILS = 3; private static final int MAX_READ_FAILS = 3;
@ -73,12 +74,14 @@ public class TrackPlayer extends Thread implements Closeable
{ {
try try
{ {
if (DEBUG_PRINT)
System.out.println("DISK THREAD: waiting"); System.out.println("DISK THREAD: waiting");
fillBufferWait.wait(); fillBufferWait.wait(5000);
} catch (InterruptedException ex) } catch (InterruptedException ex)
{ {
// this is normal // this is normal
} }
if (DEBUG_PRINT)
System.out.println("DISK THREAD: kicked"); System.out.println("DISK THREAD: kicked");
} }
} }
@ -86,9 +89,14 @@ public class TrackPlayer extends Thread implements Closeable
{ {
boolean notAtEnd = fillBuffer(true); boolean notAtEnd = fillBuffer(true);
if (!notAtEnd) if (!notAtEnd)
{
if (DEBUG_PRINT)
System.out.println("DISK THREAD: end of input");
ended = true; ended = true;
}
synchronized (bufferFilledWait) synchronized (bufferFilledWait)
{ {
if (DEBUG_PRINT)
System.out.println("DISK THREAD: buffer filled; kicking read thread"); System.out.println("DISK THREAD: buffer filled; kicking read thread");
bufferFilledWait.notifyAll(); bufferFilledWait.notifyAll();
} }
@ -98,7 +106,9 @@ public class TrackPlayer extends Thread implements Closeable
ended = true; ended = true;
} }
} }
if (DEBUG_PRINT)
System.out.println("DISK THREAD ENDED"); System.out.println("DISK THREAD ENDED");
bufferFilledWait.notifyAll(); // kick read thread in case it was waiting for us
} }
public boolean has(int byteCount) public boolean has(int byteCount)
@ -131,25 +141,30 @@ public class TrackPlayer extends Thread implements Closeable
{ {
synchronized (fillBufferWait) synchronized (fillBufferWait)
{ {
if (!ended)
{
if (DEBUG_PRINT)
System.out.println("READ THREAD: kicking disk thread"); System.out.println("READ THREAD: kicking disk thread");
fillBufferWait.notifyAll(); // kick the disk thread to fill the buffer if needed fillBufferWait.notifyAll(); // kick the disk thread to fill the buffer if needed
} }
}
int bytes; int bytes;
synchronized (audioBuffer) synchronized (audioBuffer)
{ {
bytes = audioBuffer.getCurrentNumberOfBytes(); bytes = audioBuffer.getCurrentNumberOfBytes();
} }
if (bytes == 0) if (bytes == 0 && !ended)
{ {
synchronized (bufferFilledWait) synchronized (bufferFilledWait)
{ {
try try
{ {
System.out.println("READ THREAD: waiting for disk thread"); 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) } catch (InterruptedException ex)
{ {
} }
if (DEBUG_PRINT)
System.out.println("READ THREAD: kicked"); System.out.println("READ THREAD: kicked");
} }
} }
@ -172,13 +187,14 @@ public class TrackPlayer extends Thread implements Closeable
while (audioBuffer.getCurrentNumberOfBytes() < DESIRED_BUFFER_SIZE) while (audioBuffer.getCurrentNumberOfBytes() < DESIRED_BUFFER_SIZE)
{ {
boolean read = readData(); boolean read = readData();
if (!read && !canSkip) if (!read)
return false; {
else if (fails < MAX_READ_FAILS) if (canSkip && fails < MAX_READ_FAILS)
fails++; fails++;
else else
return false; return false;
} }
}
return true; return true;
} }
@ -210,6 +226,7 @@ public class TrackPlayer extends Thread implements Closeable
byte[] bytes = new byte[bytesToRead]; byte[] bytes = new byte[bytesToRead];
// byte[] bytes = din.readNBytes(bytesToRead); // byte[] bytes = din.readNBytes(bytesToRead);
int read = input.read(bytes); int read = input.read(bytes);
if (DEBUG_PRINT)
System.out.println(" Space:" + space + " Available: " + input.available() + " To read: " + bytesToRead + " Read: " + read); System.out.println(" Space:" + space + " Available: " + input.available() + " To read: " + bytesToRead + " Read: " + read);
if (read < 0) if (read < 0)
return false; return false;

Loading…
Cancel
Save