changeset 161:86fab9e2bc64

2008-10-06 Omair Majid <omajid@redhat.com> * src/java/org/classpath/icedtea/pulseaudio/PulseAudioSourceDataLine.java New variables flushed and drained. (write): Synchronized on this. (flush): Set flushed to true. (drain): Set drained to true.
author Omair Majid <omajid@redhat.com>
date Mon, 06 Oct 2008 15:30:33 -0400
parents eb2d35a91b0d
children fa7dd0d762f1
files src/java/org/classpath/icedtea/pulseaudio/PulseAudioSourceDataLine.java src/java/org/classpath/icedtea/pulseaudio/PulseAudioTargetDataLine.java
diffstat 2 files changed, 55 insertions(+), 28 deletions(-) [+]
line wrap: on
line diff
--- a/src/java/org/classpath/icedtea/pulseaudio/PulseAudioSourceDataLine.java	Mon Oct 06 11:31:34 2008 -0400
+++ b/src/java/org/classpath/icedtea/pulseaudio/PulseAudioSourceDataLine.java	Mon Oct 06 15:30:33 2008 -0400
@@ -51,6 +51,9 @@
 	private PulseAudioVolumeControl volumeControl;
 	private boolean muted;
 	private float volume;
+	
+	boolean flushed = false;
+	boolean drained = false;
 
 	public PulseAudioSourceDataLine(EventLoop eventLoop, AudioFormat[] formats,
 			AudioFormat defaultFormat) {
@@ -156,43 +159,56 @@
 
 		while (remainingLength != 0) {
 
-			synchronized (eventLoop.threadLock) {
-
-				do {
-					availableSize = stream.getWritableSize();
+			synchronized (this) {
+				
+				if (!isStarted  || !isOpen) {
+					return sizeWritten;
+				}
+				
+				if (flushed) {
+					return sizeWritten;
+				}
+				
 
-					if (availableSize < 0) {
-						return sizeWritten;
-					}
+				synchronized (eventLoop.threadLock) {
 
-					if (availableSize == 0) {
-						try {
-							eventLoop.threadLock.wait();
-						} catch (InterruptedException e) {
-							// ignore for now
-							interrupted = true;
+					do {
+						availableSize = stream.getWritableSize();
+
+						if (availableSize < 0) {
+							return sizeWritten;
 						}
 
+						if (availableSize == 0) {
+							try {
+								eventLoop.threadLock.wait();
+							} catch (InterruptedException e) {
+								// ignore for now
+								interrupted = true;
+							}
+
+						}
+
+					} while (availableSize == 0);
+
+					if (availableSize > remainingLength) {
+						availableSize = remainingLength;
 					}
 
-				} while (availableSize == 0);
-
-				if (availableSize > remainingLength) {
-					availableSize = remainingLength;
-				}
+					// only write entire frames, so round down avialableSize to
+					// a
+					// multiple of frameSize
+					availableSize = (availableSize / frameSize) * frameSize;
 
-				// only write entire frames, so round down avialableSize to a
-				// multiple of frameSize
-				availableSize = (availableSize / frameSize) * frameSize;
+					/* write a little bit of the buffer */
+					stream.write(data, position, availableSize);
 
-				/* write a little bit of the buffer */
-				stream.write(data, position, availableSize);
+					sizeWritten += availableSize;
+					position += availableSize;
+					remainingLength -= availableSize;
 
-				sizeWritten += availableSize;
-				position += availableSize;
-				remainingLength -= availableSize;
-
-				framesSinceOpen += availableSize / frameSize;
+					framesSinceOpen += availableSize / frameSize;
+				}
 			}
 		}
 
@@ -248,6 +264,10 @@
 
 		operation.waitForCompletion();
 		operation.releaseReference();
+		
+		synchronized (this) {
+			drained = true;
+		}
 
 	}
 
@@ -266,6 +286,10 @@
 		operation.waitForCompletion();
 		operation.releaseReference();
 
+		synchronized (this) {
+			flushed= true;
+		}
+		
 	}
 
 	@Override
--- a/src/java/org/classpath/icedtea/pulseaudio/PulseAudioTargetDataLine.java	Mon Oct 06 11:31:34 2008 -0400
+++ b/src/java/org/classpath/icedtea/pulseaudio/PulseAudioTargetDataLine.java	Mon Oct 06 15:30:33 2008 -0400
@@ -151,6 +151,9 @@
 		/* bytes read on each iteration of loop */
 		int bytesRead;
 
+		flushed = false;
+		drained = false;
+
 		/*
 		 * to read, we first take stuff from the fragmentBuffer
 		 */