|
|
|
@ -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,12 +74,14 @@ public class TrackPlayer extends Thread implements Closeable |
|
|
|
|
{ |
|
|
|
|
try |
|
|
|
|
{ |
|
|
|
|
if (DEBUG_PRINT) |
|
|
|
|
System.out.println("DISK THREAD: waiting"); |
|
|
|
|
fillBufferWait.wait(); |
|
|
|
|
fillBufferWait.wait(5000); |
|
|
|
|
} catch (InterruptedException ex) |
|
|
|
|
{ |
|
|
|
|
// this is normal
|
|
|
|
|
} |
|
|
|
|
if (DEBUG_PRINT) |
|
|
|
|
System.out.println("DISK THREAD: kicked"); |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
@ -86,9 +89,14 @@ public class TrackPlayer extends Thread implements Closeable |
|
|
|
|
{ |
|
|
|
|
boolean notAtEnd = fillBuffer(true); |
|
|
|
|
if (!notAtEnd) |
|
|
|
|
{ |
|
|
|
|
if (DEBUG_PRINT) |
|
|
|
|
System.out.println("DISK THREAD: end of input"); |
|
|
|
|
ended = true; |
|
|
|
|
} |
|
|
|
|
synchronized (bufferFilledWait) |
|
|
|
|
{ |
|
|
|
|
if (DEBUG_PRINT) |
|
|
|
|
System.out.println("DISK THREAD: buffer filled; kicking read thread"); |
|
|
|
|
bufferFilledWait.notifyAll(); |
|
|
|
|
} |
|
|
|
@ -98,7 +106,9 @@ public class TrackPlayer extends Thread implements Closeable |
|
|
|
|
ended = true; |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
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,25 +141,30 @@ public class TrackPlayer extends Thread implements Closeable |
|
|
|
|
{ |
|
|
|
|
synchronized (fillBufferWait) |
|
|
|
|
{ |
|
|
|
|
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) |
|
|
|
|
{ |
|
|
|
|
} |
|
|
|
|
if (DEBUG_PRINT) |
|
|
|
|
System.out.println("READ THREAD: kicked"); |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
@ -172,13 +187,14 @@ 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) |
|
|
|
|
if (!read) |
|
|
|
|
{ |
|
|
|
|
if (canSkip && fails < MAX_READ_FAILS) |
|
|
|
|
fails++; |
|
|
|
|
else |
|
|
|
|
return false; |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
return true; |
|
|
|
|
} |
|
|
|
|
|
|
|
|
@ -210,6 +226,7 @@ public class TrackPlayer extends Thread implements Closeable |
|
|
|
|
byte[] bytes = new byte[bytesToRead]; |
|
|
|
|
// byte[] bytes = din.readNBytes(bytesToRead);
|
|
|
|
|
int read = input.read(bytes); |
|
|
|
|
if (DEBUG_PRINT) |
|
|
|
|
System.out.println(" Space:" + space + " Available: " + input.available() + " To read: " + bytesToRead + " Read: " + read); |
|
|
|
|
if (read < 0) |
|
|
|
|
return false; |
|
|
|
|