Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/src/share/classes/com/sun/media/sound/AbstractDataLine.java
38924 views
/*1* Copyright (c) 1999, 2016, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation. Oracle designates this7* particular file as subject to the "Classpath" exception as provided8* by Oracle in the LICENSE file that accompanied this code.9*10* This code is distributed in the hope that it will be useful, but WITHOUT11* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or12* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License13* version 2 for more details (a copy is included in the LICENSE file that14* accompanied this code).15*16* You should have received a copy of the GNU General Public License version17* 2 along with this work; if not, write to the Free Software Foundation,18* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.19*20* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA21* or visit www.oracle.com if you need additional information or have any22* questions.23*/2425package com.sun.media.sound;2627import javax.sound.sampled.AudioFormat;28import javax.sound.sampled.AudioSystem;29import javax.sound.sampled.Control;30import javax.sound.sampled.DataLine;31import javax.sound.sampled.LineEvent;32import javax.sound.sampled.LineUnavailableException;333435/**36* AbstractDataLine37*38* @author Kara Kytle39*/40abstract class AbstractDataLine extends AbstractLine implements DataLine {4142// DEFAULTS4344// default format45private final AudioFormat defaultFormat;4647// default buffer size in bytes48private final int defaultBufferSize;4950// the lock for synchronization51protected final Object lock = new Object();5253// STATE5455// current format56protected AudioFormat format;5758// current buffer size in bytes59protected int bufferSize;6061private volatile boolean running;62private volatile boolean started;63private volatile boolean active;6465/**66* Constructs a new AbstractLine.67*/68protected AbstractDataLine(DataLine.Info info, AbstractMixer mixer, Control[] controls) {69this(info, mixer, controls, null, AudioSystem.NOT_SPECIFIED);70}7172/**73* Constructs a new AbstractLine.74*/75protected AbstractDataLine(DataLine.Info info, AbstractMixer mixer, Control[] controls, AudioFormat format, int bufferSize) {7677super(info, mixer, controls);7879// record the default values80if (format != null) {81defaultFormat = format;82} else {83// default CD-quality84defaultFormat = new AudioFormat(44100.0f, 16, 2, true, Platform.isBigEndian());85}86if (bufferSize > 0) {87defaultBufferSize = bufferSize;88} else {89// 0.5 seconds buffer90defaultBufferSize = ((int) (defaultFormat.getFrameRate() / 2)) * defaultFormat.getFrameSize();91}9293// set the initial values to the defaults94this.format = defaultFormat;95this.bufferSize = defaultBufferSize;96}979899// DATA LINE METHODS100101public final void open(AudioFormat format, int bufferSize) throws LineUnavailableException {102//$$fb 2001-10-09: Bug #4517739: avoiding deadlock by synchronizing to mixer !103synchronized (mixer) {104if (Printer.trace) Printer.trace("> AbstractDataLine.open(format, bufferSize) (class: "+getClass().getName());105106// if the line is not currently open, try to open it with this format and buffer size107if (!isOpen()) {108// make sure that the format is specified correctly109// $$fb part of fix for 4679187: Clip.open() throws unexpected Exceptions110Toolkit.isFullySpecifiedAudioFormat(format);111112if (Printer.debug) Printer.debug(" need to open the mixer...");113// reserve mixer resources for this line114//mixer.open(this, format, bufferSize);115mixer.open(this);116117try {118// open the data line. may throw LineUnavailableException.119implOpen(format, bufferSize);120121// if we succeeded, set the open state to true and send events122setOpen(true);123124} catch (LineUnavailableException e) {125// release mixer resources for this line and then throw the exception126mixer.close(this);127throw e;128}129} else {130if (Printer.debug) Printer.debug(" dataline already open");131132// if the line is already open and the requested format differs from the133// current settings, throw an IllegalStateException134//$$fb 2002-04-02: fix for 4661602: Buffersize is checked when re-opening line135if (!format.matches(getFormat())) {136throw new IllegalStateException("Line is already open with format " + getFormat() +137" and bufferSize " + getBufferSize());138}139//$$fb 2002-07-26: allow changing the buffersize of already open lines140if (bufferSize > 0) {141setBufferSize(bufferSize);142}143}144145if (Printer.trace) Printer.trace("< AbstractDataLine.open(format, bufferSize) completed");146}147}148149150public final void open(AudioFormat format) throws LineUnavailableException {151open(format, AudioSystem.NOT_SPECIFIED);152}153154155/**156* This implementation always returns 0.157*/158public int available() {159return 0;160}161162163/**164* This implementation does nothing.165*/166public void drain() {167if (Printer.trace) Printer.trace("AbstractDataLine: drain");168}169170171/**172* This implementation does nothing.173*/174public void flush() {175if (Printer.trace) Printer.trace("AbstractDataLine: flush");176}177178179public final void start() {180//$$fb 2001-10-09: Bug #4517739: avoiding deadlock by synchronizing to mixer !181synchronized(mixer) {182if (Printer.trace) Printer.trace("> "+getClass().getName()+".start() - AbstractDataLine");183184// $$kk: 06.06.99: if not open, this doesn't work....???185if (isOpen()) {186187if (!isStartedRunning()) {188mixer.start(this);189implStart();190running = true;191}192}193}194195synchronized(lock) {196lock.notifyAll();197}198199if (Printer.trace) Printer.trace("< "+getClass().getName()+".start() - AbstractDataLine");200}201202203public final void stop() {204205//$$fb 2001-10-09: Bug #4517739: avoiding deadlock by synchronizing to mixer !206synchronized(mixer) {207if (Printer.trace) Printer.trace("> "+getClass().getName()+".stop() - AbstractDataLine");208209// $$kk: 06.06.99: if not open, this doesn't work.210if (isOpen()) {211212if (isStartedRunning()) {213214implStop();215mixer.stop(this);216217running = false;218219// $$kk: 11.10.99: this is not exactly correct, but will probably work220if (started && (!isActive())) {221setStarted(false);222}223}224}225}226227synchronized(lock) {228lock.notifyAll();229}230231if (Printer.trace) Printer.trace("< "+getClass().getName()+".stop() - AbstractDataLine");232}233234// $$jb: 12.10.99: The official API for this is isRunning().235// Per the denied RFE 4297981,236// the change to isStarted() is technically an unapproved API change.237// The 'started' variable is false when playback of data stops.238// It is changed throughout the implementation with setStarted().239// This state is what should be returned by isRunning() in the API.240// Note that the 'running' variable is true between calls to241// start() and stop(). This state is accessed now through the242// isStartedRunning() method, defined below. I have not changed243// the variable names at this point, since 'running' is accessed244// in MixerSourceLine and MixerClip, and I want to touch as little245// code as possible to change isStarted() back to isRunning().246247public final boolean isRunning() {248return started;249}250251public final boolean isActive() {252return active;253}254255256public final long getMicrosecondPosition() {257258long microseconds = getLongFramePosition();259if (microseconds != AudioSystem.NOT_SPECIFIED) {260microseconds = Toolkit.frames2micros(getFormat(), microseconds);261}262return microseconds;263}264265266public final AudioFormat getFormat() {267return format;268}269270271public final int getBufferSize() {272return bufferSize;273}274275/**276* This implementation does NOT change the buffer size277*/278public final int setBufferSize(int newSize) {279return getBufferSize();280}281282/**283* This implementation returns AudioSystem.NOT_SPECIFIED.284*/285public final float getLevel() {286return (float)AudioSystem.NOT_SPECIFIED;287}288289290// HELPER METHODS291292/**293* running is true after start is called and before stop is called,294* regardless of whether data is actually being presented.295*/296// $$jb: 12.10.99: calling this method isRunning() conflicts with297// the official API that was once called isStarted(). Since we298// use this method throughout the implementation, I am renaming299// it to isStartedRunning(). This is part of backing out the300// change denied in RFE 4297981.301302final boolean isStartedRunning() {303return running;304}305306/**307* This method sets the active state and generates308* events if it changes.309*/310final void setActive(boolean active) {311312if (Printer.trace) Printer.trace("> AbstractDataLine: setActive(" + active + ")");313314//boolean sendEvents = false;315//long position = getLongFramePosition();316317synchronized (this) {318319//if (Printer.debug) Printer.debug(" AbstractDataLine: setActive: this.active: " + this.active);320//if (Printer.debug) Printer.debug(" active: " + active);321322if (this.active != active) {323this.active = active;324//sendEvents = true;325}326}327328//if (Printer.debug) Printer.debug(" this.active: " + this.active);329//if (Printer.debug) Printer.debug(" sendEvents: " + sendEvents);330331332// $$kk: 11.19.99: take ACTIVE / INACTIVE / EOM events out;333// putting them in is technically an API change.334// do not generate ACTIVE / INACTIVE events for now335// if (sendEvents) {336//337// if (active) {338// sendEvents(new LineEvent(this, LineEvent.Type.ACTIVE, position));339// } else {340// sendEvents(new LineEvent(this, LineEvent.Type.INACTIVE, position));341// }342//}343}344345/**346* This method sets the started state and generates347* events if it changes.348*/349final void setStarted(boolean started) {350351if (Printer.trace) Printer.trace("> AbstractDataLine: setStarted(" + started + ")");352353boolean sendEvents = false;354long position = getLongFramePosition();355356synchronized (this) {357358//if (Printer.debug) Printer.debug(" AbstractDataLine: setStarted: this.started: " + this.started);359//if (Printer.debug) Printer.debug(" started: " + started);360361if (this.started != started) {362this.started = started;363sendEvents = true;364}365}366367//if (Printer.debug) Printer.debug(" this.started: " + this.started);368//if (Printer.debug) Printer.debug(" sendEvents: " + sendEvents);369370if (sendEvents) {371372if (started) {373sendEvents(new LineEvent(this, LineEvent.Type.START, position));374} else {375sendEvents(new LineEvent(this, LineEvent.Type.STOP, position));376}377}378if (Printer.trace) Printer.trace("< AbstractDataLine: setStarted completed");379}380381382/**383* This method generates a STOP event and sets the started state to false.384* It is here for historic reasons when an EOM event existed.385*/386final void setEOM() {387388if (Printer.trace) Printer.trace("> AbstractDataLine: setEOM()");389//$$fb 2002-04-21: sometimes, 2 STOP events are generated.390// better use setStarted() to send STOP event.391setStarted(false);392if (Printer.trace) Printer.trace("< AbstractDataLine: setEOM() completed");393}394395396397398// OVERRIDES OF ABSTRACT LINE METHODS399400/**401* Try to open the line with the current format and buffer size values.402* If the line is not open, these will be the defaults. If the403* line is open, this should return quietly because the values404* requested will match the current ones.405*/406public final void open() throws LineUnavailableException {407408if (Printer.trace) Printer.trace("> "+getClass().getName()+".open() - AbstractDataLine");409410// this may throw a LineUnavailableException.411open(format, bufferSize);412if (Printer.trace) Printer.trace("< "+getClass().getName()+".open() - AbstractDataLine");413}414415416/**417* This should also stop the line. The closed line should not be running or active.418* After we close the line, we reset the format and buffer size to the defaults.419*/420public final void close() {421//$$fb 2001-10-09: Bug #4517739: avoiding deadlock by synchronizing to mixer !422synchronized (mixer) {423if (Printer.trace) Printer.trace("> "+getClass().getName()+".close() - in AbstractDataLine.");424425if (isOpen()) {426427// stop428stop();429430// set the open state to false and send events431setOpen(false);432433// close resources for this line434implClose();435436// release mixer resources for this line437mixer.close(this);438439// reset format and buffer size to the defaults440format = defaultFormat;441bufferSize = defaultBufferSize;442}443}444if (Printer.trace) Printer.trace("< "+getClass().getName()+".close() - in AbstractDataLine");445}446447448// IMPLEMENTATIONS OF ABSTRACT LINE ABSTRACE METHODS449450451// ABSTRACT METHODS452453abstract void implOpen(AudioFormat format, int bufferSize) throws LineUnavailableException;454abstract void implClose();455456abstract void implStart();457abstract void implStop();458}459460461