Fix Trackplayer read data retry system, and ensure that read thread never waits for a dead disk thread.
This commit is contained in:
parent
f8139bd4f6
commit
e207b34c0f
|
@ -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,23 +74,30 @@ public class TrackPlayer extends Thread implements Closeable
|
||||||
{
|
{
|
||||||
try
|
try
|
||||||
{
|
{
|
||||||
System.out.println("DISK THREAD: waiting");
|
if (DEBUG_PRINT)
|
||||||
fillBufferWait.wait();
|
System.out.println("DISK THREAD: waiting");
|
||||||
|
fillBufferWait.wait(5000);
|
||||||
} catch (InterruptedException ex)
|
} catch (InterruptedException ex)
|
||||||
{
|
{
|
||||||
// this is normal
|
// this is normal
|
||||||
}
|
}
|
||||||
System.out.println("DISK THREAD: kicked");
|
if (DEBUG_PRINT)
|
||||||
|
System.out.println("DISK THREAD: kicked");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
try
|
try
|
||||||
{
|
{
|
||||||
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)
|
||||||
{
|
{
|
||||||
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();
|
bufferFilledWait.notifyAll();
|
||||||
}
|
}
|
||||||
} catch (IOException ex)
|
} catch (IOException ex)
|
||||||
|
@ -98,7 +106,9 @@ public class TrackPlayer extends Thread implements Closeable
|
||||||
ended = true;
|
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)
|
public boolean has(int byteCount)
|
||||||
|
@ -131,26 +141,31 @@ public class TrackPlayer extends Thread implements Closeable
|
||||||
{
|
{
|
||||||
synchronized (fillBufferWait)
|
synchronized (fillBufferWait)
|
||||||
{
|
{
|
||||||
System.out.println("READ THREAD: kicking disk thread");
|
if (!ended)
|
||||||
fillBufferWait.notifyAll(); // kick the disk thread to fill the buffer if needed
|
{
|
||||||
|
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;
|
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)
|
||||||
{
|
{
|
||||||
}
|
}
|
||||||
System.out.println("READ THREAD: kicked");
|
if (DEBUG_PRINT)
|
||||||
|
System.out.println("READ THREAD: kicked");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
synchronized (audioBuffer)
|
synchronized (audioBuffer)
|
||||||
|
@ -172,12 +187,13 @@ 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,7 +226,8 @@ 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);
|
||||||
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)
|
if (read < 0)
|
||||||
return false;
|
return false;
|
||||||
// queue.add(bytes);
|
// queue.add(bytes);
|
||||||
|
|
Loading…
Reference in New Issue