0N/A/*
6321N/A * Copyright (c) 1999, 2013, Oracle and/or its affiliates. All rights reserved.
0N/A * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
0N/A *
0N/A * This code is free software; you can redistribute it and/or modify it
0N/A * under the terms of the GNU General Public License version 2 only, as
2362N/A * published by the Free Software Foundation. Oracle designates this
0N/A * particular file as subject to the "Classpath" exception as provided
2362N/A * by Oracle in the LICENSE file that accompanied this code.
0N/A *
0N/A * This code is distributed in the hope that it will be useful, but WITHOUT
0N/A * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
0N/A * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
0N/A * version 2 for more details (a copy is included in the LICENSE file that
0N/A * accompanied this code).
0N/A *
0N/A * You should have received a copy of the GNU General Public License version
0N/A * 2 along with this work; if not, write to the Free Software Foundation,
0N/A * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
0N/A *
2362N/A * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
2362N/A * or visit www.oracle.com if you need additional information or have any
2362N/A * questions.
0N/A */
0N/A
0N/Apackage com.sun.media.sound;
0N/A
6321N/Aimport java.util.Map;
0N/Aimport java.util.Vector;
6321N/Aimport java.util.WeakHashMap;
0N/A
0N/Aimport javax.sound.sampled.AudioSystem;
0N/Aimport javax.sound.sampled.Control;
0N/Aimport javax.sound.sampled.Line;
0N/Aimport javax.sound.sampled.LineEvent;
0N/Aimport javax.sound.sampled.LineListener;
0N/Aimport javax.sound.sampled.LineUnavailableException;
0N/A
0N/A
0N/A/**
0N/A * AbstractLine
0N/A *
0N/A * @author Kara Kytle
0N/A */
0N/Aabstract class AbstractLine implements Line {
0N/A
6321N/A protected final Line.Info info;
0N/A protected Control[] controls;
6321N/A AbstractMixer mixer;
0N/A private boolean open = false;
6321N/A private final Vector listeners = new Vector();
0N/A
0N/A /**
6321N/A * Contains event dispatcher per thread group.
0N/A */
6321N/A private static final Map<ThreadGroup, EventDispatcher> dispatchers =
6321N/A new WeakHashMap<>();
0N/A
0N/A /**
0N/A * Constructs a new AbstractLine.
0N/A * @param mixer the mixer with which this line is associated
0N/A * @param controls set of supported controls
0N/A */
0N/A protected AbstractLine(Line.Info info, AbstractMixer mixer, Control[] controls) {
0N/A
0N/A if (controls == null) {
0N/A controls = new Control[0];
0N/A }
0N/A
0N/A this.info = info;
0N/A this.mixer = mixer;
0N/A this.controls = controls;
0N/A }
0N/A
0N/A
0N/A // LINE METHODS
0N/A
6321N/A public final Line.Info getLineInfo() {
0N/A return info;
0N/A }
0N/A
0N/A
6321N/A public final boolean isOpen() {
0N/A return open;
0N/A }
0N/A
0N/A
6321N/A public final void addLineListener(LineListener listener) {
0N/A synchronized(listeners) {
0N/A if ( ! (listeners.contains(listener)) ) {
0N/A listeners.addElement(listener);
0N/A }
0N/A }
0N/A }
0N/A
0N/A
0N/A /**
0N/A * Removes an audio listener.
0N/A * @param listener listener to remove
0N/A */
6321N/A public final void removeLineListener(LineListener listener) {
0N/A listeners.removeElement(listener);
0N/A }
0N/A
0N/A
0N/A /**
0N/A * Obtains the set of controls supported by the
0N/A * line. If no controls are supported, returns an
0N/A * array of length 0.
0N/A * @return control set
0N/A */
6321N/A public final Control[] getControls() {
0N/A Control[] returnedArray = new Control[controls.length];
0N/A
0N/A for (int i = 0; i < controls.length; i++) {
0N/A returnedArray[i] = controls[i];
0N/A }
0N/A
0N/A return returnedArray;
0N/A }
0N/A
0N/A
6321N/A public final boolean isControlSupported(Control.Type controlType) {
0N/A // protect against a NullPointerException
0N/A if (controlType == null) {
0N/A return false;
0N/A }
0N/A
0N/A for (int i = 0; i < controls.length; i++) {
0N/A if (controlType == controls[i].getType()) {
0N/A return true;
0N/A }
0N/A }
0N/A
0N/A return false;
0N/A }
0N/A
0N/A
6321N/A public final Control getControl(Control.Type controlType) {
0N/A // protect against a NullPointerException
0N/A if (controlType != null) {
0N/A
0N/A for (int i = 0; i < controls.length; i++) {
0N/A if (controlType == controls[i].getType()) {
0N/A return controls[i];
0N/A }
0N/A }
0N/A }
0N/A
0N/A throw new IllegalArgumentException("Unsupported control type: " + controlType);
0N/A }
0N/A
0N/A
0N/A // HELPER METHODS
0N/A
0N/A
0N/A /**
0N/A * This method sets the open state and generates
0N/A * events if it changes.
0N/A */
6321N/A final void setOpen(boolean open) {
0N/A
0N/A if (Printer.trace) Printer.trace("> "+getClass().getName()+" (AbstractLine): setOpen(" + open + ") this.open: " + this.open);
0N/A
0N/A boolean sendEvents = false;
0N/A long position = getLongFramePosition();
0N/A
0N/A synchronized (this) {
0N/A if (this.open != open) {
0N/A this.open = open;
0N/A sendEvents = true;
0N/A }
0N/A }
0N/A
0N/A if (sendEvents) {
0N/A if (open) {
0N/A sendEvents(new LineEvent(this, LineEvent.Type.OPEN, position));
0N/A } else {
0N/A sendEvents(new LineEvent(this, LineEvent.Type.CLOSE, position));
0N/A }
0N/A }
0N/A if (Printer.trace) Printer.trace("< "+getClass().getName()+" (AbstractLine): setOpen(" + open + ") this.open: " + this.open);
0N/A }
0N/A
0N/A
0N/A /**
0N/A * Send line events.
0N/A */
6321N/A final void sendEvents(LineEvent event) {
6321N/A getEventDispatcher().sendAudioEvents(event, listeners);
0N/A }
0N/A
0N/A
0N/A /**
0N/A * This is an error in the API: getFramePosition
0N/A * should return a long value. At CD quality,
0N/A * the int value wraps around after 13 hours.
0N/A */
0N/A public final int getFramePosition() {
0N/A return (int) getLongFramePosition();
0N/A }
0N/A
0N/A
0N/A /**
0N/A * Return the frame position in a long value
0N/A * This implementation returns AudioSystem.NOT_SPECIFIED.
0N/A */
0N/A public long getLongFramePosition() {
0N/A return AudioSystem.NOT_SPECIFIED;
0N/A }
0N/A
0N/A
0N/A // $$kk: 06.03.99: returns the mixer used in construction.
0N/A // this is a hold-over from when there was a public method like
0N/A // this on line and should be fixed!!
6321N/A final AbstractMixer getMixer() {
0N/A return mixer;
0N/A }
0N/A
6321N/A final EventDispatcher getEventDispatcher() {
6321N/A // create and start the global event thread
6321N/A //TODO need a way to stop this thread when the engine is done
6321N/A final ThreadGroup tg = Thread.currentThread().getThreadGroup();
6321N/A synchronized (dispatchers) {
6321N/A EventDispatcher eventDispatcher = dispatchers.get(tg);
6321N/A if (eventDispatcher == null) {
6321N/A eventDispatcher = new EventDispatcher();
6321N/A dispatchers.put(tg, eventDispatcher);
6321N/A eventDispatcher.start();
6321N/A }
6321N/A return eventDispatcher;
6321N/A }
0N/A }
0N/A
0N/A // ABSTRACT METHODS
0N/A
0N/A public abstract void open() throws LineUnavailableException;
0N/A public abstract void close();
0N/A}