view src/java/org/classpath/icedtea/pulseaudio/PulseAudioClip.java @ 134:381f212496eb

2008-09-23 Omair Majid <omajid@redhat.com> * src/java/org/classpath/icedtea/pulseaudio/Operation.java (waitForComplete): Handles interrupts now. Maybe not the best way of handling them, but it works. * src/java/org/classpath/icedtea/pulseaudio/PulseAudioClip.java (ClipThread.run): Drain the stream by using the stream functions, not by a call to drain. (drain): Fixed to work the way reported in the bug report. * unittests/org/classpath/icedtea/pulseaudio/PulseAudioClipTest.java (testLoopStartStopClip): Renamed to testLoop4Times. Now uses drain to wait for the loop to complete playing. (testDrainWithoutStart): Added a timeout. (testDrainBlocksWhilePlaying): New function. Tests that drain() blocks while playing. (testLoop0Clip): Renamed to testLoop0InterruptsPlayback. (testFramePosition): New function. Checks the value of getLongFrames() from clip. (testFramePositionAfterLooping): Checks that Looping the clip still returns the correct number of frames played (not just the current frame position in the audio stream).
author Omair Majid <omajid@redhat.com>
date Tue, 23 Sep 2008 11:38:18 -0400
parents b7a5a39b31ab
children c46f6e0e7959
line wrap: on
line source

/* PulseAudioClip.java
   Copyright (C) 2008 Red Hat, Inc.

This file is part of IcedTea.

IcedTea is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public License as published by
the Free Software Foundation, version 2.

IcedTea is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
General Public License for more details.

You should have received a copy of the GNU General Public License
along with IcedTea; see the file COPYING.  If not, write to
the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
02110-1301 USA.

Linking this library statically or dynamically with other modules is
making a combined work based on this library.  Thus, the terms and
conditions of the GNU General Public License cover the whole
combination.

As a special exception, the copyright holders of this library give you
permission to link this library with independent modules to produce an
executable, regardless of the license terms of these independent
modules, and to copy and distribute the resulting executable under
terms of your choice, provided that you also meet, for each linked
independent module, the terms and conditions of the license of that
module.  An independent module is a module which is not derived from
or based on this library.  If you modify this library, you may extend
this exception to your version of the library, but you are not
obligated to do so.  If you do not wish to do so, delete this
exception statement from your version.
 */

package org.classpath.icedtea.pulseaudio;

import java.io.IOException;
import java.util.concurrent.Semaphore;

import javax.sound.sampled.AudioFormat;
import javax.sound.sampled.AudioInputStream;
import javax.sound.sampled.AudioSystem;
import javax.sound.sampled.Clip;
import javax.sound.sampled.LineUnavailableException;

import org.classpath.icedtea.pulseaudio.Stream.WriteListener;

public class PulseAudioClip extends PulseAudioDataLine implements Clip,
		PulseAudioPlaybackLine {

	private byte[] data = null;

	private boolean muted;
	private float volume;

	// these are frame indices. so counted from 0
	private int currentFrame = 0;
	private int frameCount = 0;
	private int startFrame = 0;
	private int endFrame = 0;
	private int framesSinceOpen = 0;

	public static final String DEFAULT_CLIP_NAME = "Clip";

	private Object clipLock = new Object();
	private boolean clipThreadStarted;
	private int loopsLeft;
	private Semaphore clipSemaphore = new Semaphore(1);

	private class ClipThread extends Thread {
		@Override
		public void run() {

			clipThreadStarted = true;
			while (loopsLeft >= 0) {
				writeFrames(currentFrame, endFrame + 1);
				if (Thread.interrupted()) {
					// Thread.currentThread().interrupt();
					clipThreadStarted = false;
					break;
				}

				try {
					// if loop(0) has been called from the mainThread,
					// wait until loopsLeft has been set
					clipSemaphore.acquire();
					if (loopsLeft == 0) {
						System.out.println("Reading to the end of the file");
						writeFrames(endFrame, getFrameLength());
						break;
					} else {
						synchronized (clipLock) {
							currentFrame = startFrame;
							loopsLeft--;
						}
					}
					clipSemaphore.release();
				} catch (InterruptedException e) {
					break;
				}

			}

			Operation operation;

			synchronized (eventLoop.threadLock) {
				operation = stream.drain();
			}

			operation.waitForCompletion();
			operation.releaseReference();

		}
	}

	private ClipThread clipThread;

	private void writeFrames(int startingFrame, int lastFrame) {

		WriteListener writeListener = new WriteListener() {
			@Override
			public void update() {
				eventLoop.threadLock.notifyAll();
			}
		};

		stream.addWriteListener(writeListener);

		int remainingFrames = lastFrame - startingFrame - 1;
		while (remainingFrames > 0) {
			synchronized (eventLoop.threadLock) {
				int availableSize;

				do {
					availableSize = stream.getWritableSize();
					if (availableSize < 0) {
						Thread.currentThread().interrupt();
						stream.removeWriteListener(writeListener);
						return;
					}
					if (availableSize == 0) {
						try {
							eventLoop.threadLock.wait();
						} catch (InterruptedException e) {
							// clean up and return
							Thread.currentThread().interrupt();
							stream.removeWriteListener(writeListener);
							return;
						}
					}

				} while (availableSize == 0);

				int framesToWrite = Math.min(remainingFrames, availableSize
						/ getFormat().getFrameSize());
				stream.write(data, currentFrame * getFormat().getFrameSize(),
						framesToWrite * getFormat().getFrameSize());
				remainingFrames -= framesToWrite;
				currentFrame += framesToWrite;
				if (Thread.interrupted()) {
					Thread.currentThread().interrupt();
					break;
				}
				// System.out.println("remaining frames" + remainingFrames);
			}
		}

		stream.removeWriteListener(writeListener);

	}

	public PulseAudioClip(EventLoop eventLoop, AudioFormat[] formats,
			AudioFormat defaultFormat) {
		supportedFormats = formats;
		this.eventLoop = eventLoop;
		this.defaultFormat = defaultFormat;
		this.currentFormat = defaultFormat;
		this.volume = PulseAudioVolumeControl.MAX_VOLUME;

		clipThread = new ClipThread();

	}

	protected void connectLine(int bufferSize) throws LineUnavailableException {
		StreamBufferAttributes bufferAttributes = new StreamBufferAttributes(
				bufferSize, bufferSize / 2, bufferSize / 2, bufferSize / 2, 0);

		stream.connectForPlayback(Stream.DEFAULT_DEVICE, bufferAttributes);
	}

	@Override
	public int available() {
		return 0; // a clip always returns 0
	}

	@Override
	public void close() {
		if (!isOpen) {
			throw new IllegalStateException("line already closed");
		}

		clipThread.interrupt();

		try {
			clipThread.join();
		} catch (InterruptedException e) {
			e.printStackTrace();
		}

		PulseAudioMixer mixer = PulseAudioMixer.getInstance();
		mixer.removeSourceLine(this);

		super.close();

	}

	/*
	 * 
	 * drain() on a Clip should block until the entire clip has finished playing
	 * http://bugs.sun.com/bugdatabase/view_bug.do?bug_id=4732218
	 * 
	 * 
	 * @see org.classpath.icedtea.pulseaudio.PulseAudioDataLine#drain()
	 */
	@Override
	public void drain() {
		if (!isOpen) {
			throw new IllegalStateException("line not open");
		}

		while (clipThread != null && clipThread.isAlive()) {
			try {
				clipThread.join();
			} catch (InterruptedException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}

		Operation operation;

		synchronized (eventLoop.threadLock) {
			operation = stream.drain();
		}

		operation.waitForCompletion();
		operation.releaseReference();

	}

	@Override
	public void flush() {
		if (!isOpen) {
			throw new IllegalStateException("line not open");
		}

		Operation operation;
		synchronized (eventLoop.threadLock) {
			operation = stream.flush();
			operation.waitForCompletion();
		}
		operation.releaseReference();

	}

	@Override
	public int getFrameLength() {
		if (!isOpen) {
			return AudioSystem.NOT_SPECIFIED;
		}

		return frameCount;
	}

	@Override
	public int getFramePosition() {
		if (!isOpen) {
			throw new IllegalStateException("Line not open");
		}

		return (int) framesSinceOpen;
	}

	@Override
	public long getLongFramePosition() {
		if (!isOpen) {
			throw new IllegalStateException("Line not open");
		}

		synchronized (clipLock) {
			return framesSinceOpen;
		}
	}

	@Override
	public long getMicrosecondLength() {
		if (!isOpen) {
			return AudioSystem.NOT_SPECIFIED;
		}
		synchronized (clipLock) {
			return frameCount / currentFormat.getFrameSize();
		}
	}

	@Override
	public long getMicrosecondPosition() {
		if (!isOpen) {
			throw new IllegalStateException("Line not open");
		}

		synchronized (clipLock) {
			return framesSinceOpen / currentFormat.getFrameSize();
		}
	}

	@Override
	public void loop(int count) {
		if (!isOpen) {
			throw new IllegalStateException("Line not open");
		}

		System.out.println("Loop " + count + " called");

		if (clipThreadStarted && count != 0) {
			// Do nothing; behavior not specified by the Java API
			return;
		}

		super.start();

		synchronized (clipLock) {
			if (currentFrame > endFrame) {
				loopsLeft = 0;
			} else {
				loopsLeft = count;
			}
		}
		if (!clipThread.isAlive()) {
			clipThread = new ClipThread();
			clipThread.start();
		}

	}

	@Override
	public void open() throws LineUnavailableException {
		throw new IllegalArgumentException("open() on a Clip is not allowed");
	}

	@Override
	public void open(AudioFormat format, byte[] data, int offset, int bufferSize)
			throws LineUnavailableException {

		super.open(format);
		this.data = new byte[bufferSize];
		System.arraycopy(data, offset, this.data, 0, bufferSize);
		frameCount = bufferSize / format.getFrameSize();

		PulseAudioVolumeControl volumeControl = new PulseAudioVolumeControl(
				this, eventLoop);
		PulseAudioMuteControl muteControl = new PulseAudioMuteControl(this,
				volumeControl);
		controls.add(volumeControl);
		controls.add(muteControl);

		PulseAudioMixer mixer = PulseAudioMixer.getInstance();
		mixer.addSourceLine(this);

		isOpen = true;

	}

	public byte[] native_setVolume(float value) {
		return stream.native_setVolume(value);
	}

	public boolean isMuted() {
		return muted;
	}

	public void setMuted(boolean value) {
		muted = value;
	}

	public float getVolume() {
		return this.volume;
	}

	public void setVolume(float value) {
		this.volume = value;

	}

	@Override
	public void open(AudioInputStream stream) throws LineUnavailableException,
			IOException {
		byte[] buffer = new byte[(int) (stream.getFrameLength() * stream
				.getFormat().getFrameSize())];
		stream.read(buffer, 0, buffer.length);

		open(stream.getFormat(), buffer, 0, buffer.length);

	}

	@Override
	public void setFramePosition(int frames) {
		if (!isOpen) {
			throw new IllegalStateException("Line not open");
		}

		if (frames > frameCount) {
			throw new IllegalArgumentException("incorreft frame value");
		}

		synchronized (clipLock) {
			currentFrame = frames;
		}

	}

	@Override
	public void setLoopPoints(int start, int end) {
		if (!isOpen) {
			throw new IllegalStateException("Line not open");
		}

		if (end == -1) {
			end = frameCount;
		}

		if (end < start) {
			throw new IllegalArgumentException(
					"ending point must be greater than or equal to the starting point");
		}

		synchronized (clipLock) {
			startFrame = start;
			endFrame = end;
		}

	}

	@Override
	public void setMicrosecondPosition(long microseconds) {
		if (!isOpen) {
			throw new IllegalStateException("Line not open");
		}

		float frameIndex = microseconds * currentFormat.getFrameRate();
		synchronized (clipLock) {
			currentFrame = (int) frameIndex;
		}

	}

	@Override
	public void start() {
		if (!isOpen) {
			throw new IllegalStateException("Line not open");
		}

		if (isStarted) {
			throw new IllegalStateException("already started");
		}

		super.start();

		if (!clipThread.isAlive()) {
			synchronized (clipLock) {
				loopsLeft = 0;
			}
			clipThread = new ClipThread();
			clipThread.start();
		}

	}

	public void stop() {
		if (!isOpen) {
			throw new IllegalStateException("Line not open");
		}

		if (!isStarted) {
			throw new IllegalStateException("not started, so cant stop");
		}

		if (clipThread.isAlive()) {
			clipThread.interrupt();
		}
		try {
			clipThread.join();
		} catch (InterruptedException e) {
			e.printStackTrace();
		}
		synchronized (clipLock) {
			currentFrame = 0;
			loopsLeft = 0;
		}

		super.stop();

	}

}