jdk/src/java.desktop/share/classes/com/sun/media/sound/AbstractDataLine.java
author chegar
Sun, 17 Aug 2014 15:54:13 +0100
changeset 25859 3317bb8137f4
parent 18215 jdk/src/share/classes/com/sun/media/sound/AbstractDataLine.java@b2afd66ce6db
child 38981 9521cb19e297
permissions -rw-r--r--
8054834: Modular Source Code Reviewed-by: alanb, chegar, ihse, mduigou Contributed-by: alan.bateman@oracle.com, alex.buckley@oracle.com, chris.hegarty@oracle.com, erik.joelsson@oracle.com, jonathan.gibbons@oracle.com, karen.kinnear@oracle.com, magnus.ihse.bursie@oracle.com, mandy.chung@oracle.com, mark.reinhold@oracle.com, paul.sandoz@oracle.com
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
18215
b2afd66ce6db 8006328: Improve robustness of sound classes
serb
parents: 5506
diff changeset
     2
 * Copyright (c) 1999, 2013, Oracle and/or its affiliates. All rights reserved.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
90ce3da70b43 Initial load
duke
parents:
diff changeset
     4
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
90ce3da70b43 Initial load
duke
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
     7
 * published by the Free Software Foundation.  Oracle designates this
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     8
 * particular file as subject to the "Classpath" exception as provided
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
     9
 * by Oracle in the LICENSE file that accompanied this code.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    10
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    11
 * This code is distributed in the hope that it will be useful, but WITHOUT
90ce3da70b43 Initial load
duke
parents:
diff changeset
    12
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
90ce3da70b43 Initial load
duke
parents:
diff changeset
    13
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
90ce3da70b43 Initial load
duke
parents:
diff changeset
    14
 * version 2 for more details (a copy is included in the LICENSE file that
90ce3da70b43 Initial load
duke
parents:
diff changeset
    15
 * accompanied this code).
90ce3da70b43 Initial load
duke
parents:
diff changeset
    16
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    17
 * You should have received a copy of the GNU General Public License version
90ce3da70b43 Initial load
duke
parents:
diff changeset
    18
 * 2 along with this work; if not, write to the Free Software Foundation,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    19
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    20
 *
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
    21
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
    22
 * or visit www.oracle.com if you need additional information or have any
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
    23
 * questions.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    24
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    25
90ce3da70b43 Initial load
duke
parents:
diff changeset
    26
package com.sun.media.sound;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    27
90ce3da70b43 Initial load
duke
parents:
diff changeset
    28
import javax.sound.sampled.AudioFormat;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    29
import javax.sound.sampled.AudioSystem;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    30
import javax.sound.sampled.Control;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
import javax.sound.sampled.DataLine;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
import javax.sound.sampled.LineEvent;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
import javax.sound.sampled.LineUnavailableException;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
/**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
 * AbstractDataLine
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
 * @author Kara Kytle
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
abstract class AbstractDataLine extends AbstractLine implements DataLine {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
    // DEFAULTS
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
    // default format
18215
b2afd66ce6db 8006328: Improve robustness of sound classes
serb
parents: 5506
diff changeset
    46
    private final AudioFormat defaultFormat;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
    // default buffer size in bytes
18215
b2afd66ce6db 8006328: Improve robustness of sound classes
serb
parents: 5506
diff changeset
    49
    private final int defaultBufferSize;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
    // the lock for synchronization
18215
b2afd66ce6db 8006328: Improve robustness of sound classes
serb
parents: 5506
diff changeset
    52
    protected final Object lock = new Object();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
    // STATE
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
    // current format
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
    protected AudioFormat format;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
    // current buffer size in bytes
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
    protected int bufferSize;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
    protected boolean running = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
    private boolean started = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
    private boolean active = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
     * Constructs a new AbstractLine.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
    protected AbstractDataLine(DataLine.Info info, AbstractMixer mixer, Control[] controls) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
        this(info, mixer, controls, null, AudioSystem.NOT_SPECIFIED);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
     * Constructs a new AbstractLine.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
    protected AbstractDataLine(DataLine.Info info, AbstractMixer mixer, Control[] controls, AudioFormat format, int bufferSize) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
        super(info, mixer, controls);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
        // record the default values
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
        if (format != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
            defaultFormat = format;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
            // default CD-quality
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
            defaultFormat = new AudioFormat(44100.0f, 16, 2, true, Platform.isBigEndian());
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
        if (bufferSize > 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
            defaultBufferSize = bufferSize;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
            // 0.5 seconds buffer
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
            defaultBufferSize = ((int) (defaultFormat.getFrameRate() / 2)) * defaultFormat.getFrameSize();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
        // set the initial values to the defaults
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
        this.format = defaultFormat;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
        this.bufferSize = defaultBufferSize;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
    // DATA LINE METHODS
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
18215
b2afd66ce6db 8006328: Improve robustness of sound classes
serb
parents: 5506
diff changeset
   103
    public final void open(AudioFormat format, int bufferSize) throws LineUnavailableException {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
        //$$fb 2001-10-09: Bug #4517739: avoiding deadlock by synchronizing to mixer !
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
        synchronized (mixer) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
            if (Printer.trace) Printer.trace("> AbstractDataLine.open(format, bufferSize) (class: "+getClass().getName());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
            // if the line is not currently open, try to open it with this format and buffer size
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
            if (!isOpen()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
                // make sure that the format is specified correctly
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
                // $$fb part of fix for 4679187: Clip.open() throws unexpected Exceptions
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
                Toolkit.isFullySpecifiedAudioFormat(format);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
                if (Printer.debug) Printer.debug("  need to open the mixer...");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
                // reserve mixer resources for this line
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
                //mixer.open(this, format, bufferSize);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
                mixer.open(this);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
                try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
                    // open the data line.  may throw LineUnavailableException.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
                    implOpen(format, bufferSize);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
                    // if we succeeded, set the open state to true and send events
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
                    setOpen(true);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
                } catch (LineUnavailableException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
                    // release mixer resources for this line and then throw the exception
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
                    mixer.close(this);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
                    throw e;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
                if (Printer.debug) Printer.debug("  dataline already open");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
                // if the line is already open and the requested format differs from the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
                // current settings, throw an IllegalStateException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
                //$$fb 2002-04-02: fix for 4661602: Buffersize is checked when re-opening line
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
                if (!format.matches(getFormat())) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
                    throw new IllegalStateException("Line is already open with format " + getFormat() +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
                                                    " and bufferSize " + getBufferSize());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
                //$$fb 2002-07-26: allow changing the buffersize of already open lines
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
                if (bufferSize > 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
                    setBufferSize(bufferSize);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
            if (Printer.trace) Printer.trace("< AbstractDataLine.open(format, bufferSize) completed");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
18215
b2afd66ce6db 8006328: Improve robustness of sound classes
serb
parents: 5506
diff changeset
   152
    public final void open(AudioFormat format) throws LineUnavailableException {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
        open(format, AudioSystem.NOT_SPECIFIED);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
     * This implementation always returns 0.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
    public int available() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
        return 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   163
90ce3da70b43 Initial load
duke
parents:
diff changeset
   164
90ce3da70b43 Initial load
duke
parents:
diff changeset
   165
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   166
     * This implementation does nothing.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   167
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   168
    public void drain() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   169
        if (Printer.trace) Printer.trace("AbstractDataLine: drain");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   170
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   171
90ce3da70b43 Initial load
duke
parents:
diff changeset
   172
90ce3da70b43 Initial load
duke
parents:
diff changeset
   173
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   174
     * This implementation does nothing.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   175
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   176
    public void flush() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   177
        if (Printer.trace) Printer.trace("AbstractDataLine: flush");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   178
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   179
90ce3da70b43 Initial load
duke
parents:
diff changeset
   180
18215
b2afd66ce6db 8006328: Improve robustness of sound classes
serb
parents: 5506
diff changeset
   181
    public final void start() {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   182
        //$$fb 2001-10-09: Bug #4517739: avoiding deadlock by synchronizing to mixer !
90ce3da70b43 Initial load
duke
parents:
diff changeset
   183
        synchronized(mixer) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   184
            if (Printer.trace) Printer.trace("> "+getClass().getName()+".start() - AbstractDataLine");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   185
90ce3da70b43 Initial load
duke
parents:
diff changeset
   186
            // $$kk: 06.06.99: if not open, this doesn't work....???
90ce3da70b43 Initial load
duke
parents:
diff changeset
   187
            if (isOpen()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   188
90ce3da70b43 Initial load
duke
parents:
diff changeset
   189
                if (!isStartedRunning()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   190
                    mixer.start(this);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   191
                    implStart();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   192
                    running = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   193
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   194
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   195
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   196
90ce3da70b43 Initial load
duke
parents:
diff changeset
   197
        synchronized(lock) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   198
            lock.notifyAll();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   199
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   200
90ce3da70b43 Initial load
duke
parents:
diff changeset
   201
        if (Printer.trace) Printer.trace("< "+getClass().getName()+".start() - AbstractDataLine");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   202
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   203
90ce3da70b43 Initial load
duke
parents:
diff changeset
   204
18215
b2afd66ce6db 8006328: Improve robustness of sound classes
serb
parents: 5506
diff changeset
   205
    public final void stop() {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   206
90ce3da70b43 Initial load
duke
parents:
diff changeset
   207
        //$$fb 2001-10-09: Bug #4517739: avoiding deadlock by synchronizing to mixer !
90ce3da70b43 Initial load
duke
parents:
diff changeset
   208
        synchronized(mixer) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   209
            if (Printer.trace) Printer.trace("> "+getClass().getName()+".stop() - AbstractDataLine");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   210
90ce3da70b43 Initial load
duke
parents:
diff changeset
   211
            // $$kk: 06.06.99: if not open, this doesn't work.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   212
            if (isOpen()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   213
90ce3da70b43 Initial load
duke
parents:
diff changeset
   214
                if (isStartedRunning()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   215
90ce3da70b43 Initial load
duke
parents:
diff changeset
   216
                    implStop();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   217
                    mixer.stop(this);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   218
90ce3da70b43 Initial load
duke
parents:
diff changeset
   219
                    running = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   220
90ce3da70b43 Initial load
duke
parents:
diff changeset
   221
                    // $$kk: 11.10.99: this is not exactly correct, but will probably work
90ce3da70b43 Initial load
duke
parents:
diff changeset
   222
                    if (started && (!isActive())) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   223
                        setStarted(false);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   224
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   225
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   226
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   227
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   228
90ce3da70b43 Initial load
duke
parents:
diff changeset
   229
        synchronized(lock) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   230
            lock.notifyAll();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   231
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   232
90ce3da70b43 Initial load
duke
parents:
diff changeset
   233
        if (Printer.trace) Printer.trace("< "+getClass().getName()+".stop() - AbstractDataLine");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   234
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   235
90ce3da70b43 Initial load
duke
parents:
diff changeset
   236
    // $$jb: 12.10.99: The official API for this is isRunning().
90ce3da70b43 Initial load
duke
parents:
diff changeset
   237
    // Per the denied RFE 4297981,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   238
    // the change to isStarted() is technically an unapproved API change.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   239
    // The 'started' variable is false when playback of data stops.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   240
    // It is changed throughout the implementation with setStarted().
90ce3da70b43 Initial load
duke
parents:
diff changeset
   241
    // This state is what should be returned by isRunning() in the API.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   242
    // Note that the 'running' variable is true between calls to
90ce3da70b43 Initial load
duke
parents:
diff changeset
   243
    // start() and stop().  This state is accessed now through the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   244
    // isStartedRunning() method, defined below.  I have not changed
90ce3da70b43 Initial load
duke
parents:
diff changeset
   245
    // the variable names at this point, since 'running' is accessed
90ce3da70b43 Initial load
duke
parents:
diff changeset
   246
    // in MixerSourceLine and MixerClip, and I want to touch as little
90ce3da70b43 Initial load
duke
parents:
diff changeset
   247
    // code as possible to change isStarted() back to isRunning().
90ce3da70b43 Initial load
duke
parents:
diff changeset
   248
18215
b2afd66ce6db 8006328: Improve robustness of sound classes
serb
parents: 5506
diff changeset
   249
    public final boolean isRunning() {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   250
        return started;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   251
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   252
18215
b2afd66ce6db 8006328: Improve robustness of sound classes
serb
parents: 5506
diff changeset
   253
    public final boolean isActive() {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   254
        return active;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   255
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   256
90ce3da70b43 Initial load
duke
parents:
diff changeset
   257
18215
b2afd66ce6db 8006328: Improve robustness of sound classes
serb
parents: 5506
diff changeset
   258
    public final long getMicrosecondPosition() {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   259
90ce3da70b43 Initial load
duke
parents:
diff changeset
   260
        long microseconds = getLongFramePosition();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   261
        if (microseconds != AudioSystem.NOT_SPECIFIED) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   262
            microseconds = Toolkit.frames2micros(getFormat(), microseconds);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   263
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   264
        return microseconds;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   265
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   266
90ce3da70b43 Initial load
duke
parents:
diff changeset
   267
18215
b2afd66ce6db 8006328: Improve robustness of sound classes
serb
parents: 5506
diff changeset
   268
    public final AudioFormat getFormat() {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   269
        return format;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   270
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   271
90ce3da70b43 Initial load
duke
parents:
diff changeset
   272
18215
b2afd66ce6db 8006328: Improve robustness of sound classes
serb
parents: 5506
diff changeset
   273
    public final int getBufferSize() {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   274
        return bufferSize;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   275
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   276
90ce3da70b43 Initial load
duke
parents:
diff changeset
   277
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   278
     * This implementation does NOT change the buffer size
90ce3da70b43 Initial load
duke
parents:
diff changeset
   279
     */
18215
b2afd66ce6db 8006328: Improve robustness of sound classes
serb
parents: 5506
diff changeset
   280
    public final int setBufferSize(int newSize) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   281
        return getBufferSize();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   282
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   283
90ce3da70b43 Initial load
duke
parents:
diff changeset
   284
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   285
     * This implementation returns AudioSystem.NOT_SPECIFIED.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   286
     */
18215
b2afd66ce6db 8006328: Improve robustness of sound classes
serb
parents: 5506
diff changeset
   287
    public final float getLevel() {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   288
        return (float)AudioSystem.NOT_SPECIFIED;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   289
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   290
90ce3da70b43 Initial load
duke
parents:
diff changeset
   291
90ce3da70b43 Initial load
duke
parents:
diff changeset
   292
    // HELPER METHODS
90ce3da70b43 Initial load
duke
parents:
diff changeset
   293
90ce3da70b43 Initial load
duke
parents:
diff changeset
   294
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   295
     * running is true after start is called and before stop is called,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   296
     * regardless of whether data is actually being presented.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   297
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   298
    // $$jb: 12.10.99: calling this method isRunning() conflicts with
90ce3da70b43 Initial load
duke
parents:
diff changeset
   299
    // the official API that was once called isStarted().  Since we
90ce3da70b43 Initial load
duke
parents:
diff changeset
   300
    // use this method throughout the implementation, I am renaming
90ce3da70b43 Initial load
duke
parents:
diff changeset
   301
    // it to isStartedRunning().  This is part of backing out the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   302
    // change denied in RFE 4297981.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   303
18215
b2afd66ce6db 8006328: Improve robustness of sound classes
serb
parents: 5506
diff changeset
   304
    final boolean isStartedRunning() {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   305
        return running;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   306
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   307
90ce3da70b43 Initial load
duke
parents:
diff changeset
   308
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   309
     * This method sets the active state and generates
90ce3da70b43 Initial load
duke
parents:
diff changeset
   310
     * events if it changes.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   311
     */
18215
b2afd66ce6db 8006328: Improve robustness of sound classes
serb
parents: 5506
diff changeset
   312
    final void setActive(boolean active) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   313
90ce3da70b43 Initial load
duke
parents:
diff changeset
   314
        if (Printer.trace) Printer.trace("> AbstractDataLine: setActive(" + active + ")");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   315
90ce3da70b43 Initial load
duke
parents:
diff changeset
   316
        //boolean sendEvents = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   317
        //long position = getLongFramePosition();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   318
90ce3da70b43 Initial load
duke
parents:
diff changeset
   319
        synchronized (this) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   320
90ce3da70b43 Initial load
duke
parents:
diff changeset
   321
            //if (Printer.debug) Printer.debug("    AbstractDataLine: setActive: this.active: " + this.active);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   322
            //if (Printer.debug) Printer.debug("                                 active: " + active);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   323
90ce3da70b43 Initial load
duke
parents:
diff changeset
   324
            if (this.active != active) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   325
                this.active = active;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   326
                //sendEvents = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   327
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   328
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   329
90ce3da70b43 Initial load
duke
parents:
diff changeset
   330
        //if (Printer.debug) Printer.debug("                                 this.active: " + this.active);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   331
        //if (Printer.debug) Printer.debug("                                 sendEvents: " + sendEvents);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   332
90ce3da70b43 Initial load
duke
parents:
diff changeset
   333
90ce3da70b43 Initial load
duke
parents:
diff changeset
   334
        // $$kk: 11.19.99: take ACTIVE / INACTIVE / EOM events out;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   335
        // putting them in is technically an API change.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   336
        // do not generate ACTIVE / INACTIVE events for now
90ce3da70b43 Initial load
duke
parents:
diff changeset
   337
        // if (sendEvents) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   338
        //
90ce3da70b43 Initial load
duke
parents:
diff changeset
   339
        //      if (active) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   340
        //              sendEvents(new LineEvent(this, LineEvent.Type.ACTIVE, position));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   341
        //      } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   342
        //              sendEvents(new LineEvent(this, LineEvent.Type.INACTIVE, position));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   343
        //      }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   344
        //}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   345
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   346
90ce3da70b43 Initial load
duke
parents:
diff changeset
   347
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   348
     * This method sets the started state and generates
90ce3da70b43 Initial load
duke
parents:
diff changeset
   349
     * events if it changes.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   350
     */
18215
b2afd66ce6db 8006328: Improve robustness of sound classes
serb
parents: 5506
diff changeset
   351
    final void setStarted(boolean started) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   352
90ce3da70b43 Initial load
duke
parents:
diff changeset
   353
        if (Printer.trace) Printer.trace("> AbstractDataLine: setStarted(" + started + ")");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   354
90ce3da70b43 Initial load
duke
parents:
diff changeset
   355
        boolean sendEvents = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   356
        long position = getLongFramePosition();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   357
90ce3da70b43 Initial load
duke
parents:
diff changeset
   358
        synchronized (this) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   359
90ce3da70b43 Initial load
duke
parents:
diff changeset
   360
            //if (Printer.debug) Printer.debug("    AbstractDataLine: setStarted: this.started: " + this.started);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   361
            //if (Printer.debug) Printer.debug("                                  started: " + started);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   362
90ce3da70b43 Initial load
duke
parents:
diff changeset
   363
            if (this.started != started) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   364
                this.started = started;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   365
                sendEvents = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   366
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   367
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   368
90ce3da70b43 Initial load
duke
parents:
diff changeset
   369
        //if (Printer.debug) Printer.debug("                                  this.started: " + this.started);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   370
        //if (Printer.debug) Printer.debug("                                  sendEvents: " + sendEvents);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   371
90ce3da70b43 Initial load
duke
parents:
diff changeset
   372
        if (sendEvents) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   373
90ce3da70b43 Initial load
duke
parents:
diff changeset
   374
            if (started) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   375
                sendEvents(new LineEvent(this, LineEvent.Type.START, position));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   376
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   377
                sendEvents(new LineEvent(this, LineEvent.Type.STOP, position));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   378
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   379
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   380
        if (Printer.trace) Printer.trace("< AbstractDataLine: setStarted completed");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   381
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   382
90ce3da70b43 Initial load
duke
parents:
diff changeset
   383
90ce3da70b43 Initial load
duke
parents:
diff changeset
   384
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   385
     * This method generates a STOP event and sets the started state to false.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   386
     * It is here for historic reasons when an EOM event existed.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   387
     */
18215
b2afd66ce6db 8006328: Improve robustness of sound classes
serb
parents: 5506
diff changeset
   388
    final void setEOM() {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   389
90ce3da70b43 Initial load
duke
parents:
diff changeset
   390
        if (Printer.trace) Printer.trace("> AbstractDataLine: setEOM()");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   391
        //$$fb 2002-04-21: sometimes, 2 STOP events are generated.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   392
        // better use setStarted() to send STOP event.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   393
        setStarted(false);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   394
        if (Printer.trace) Printer.trace("< AbstractDataLine: setEOM() completed");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   395
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   396
90ce3da70b43 Initial load
duke
parents:
diff changeset
   397
90ce3da70b43 Initial load
duke
parents:
diff changeset
   398
90ce3da70b43 Initial load
duke
parents:
diff changeset
   399
90ce3da70b43 Initial load
duke
parents:
diff changeset
   400
    // OVERRIDES OF ABSTRACT LINE METHODS
90ce3da70b43 Initial load
duke
parents:
diff changeset
   401
90ce3da70b43 Initial load
duke
parents:
diff changeset
   402
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   403
     * Try to open the line with the current format and buffer size values.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   404
     * If the line is not open, these will be the defaults.  If the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   405
     * line is open, this should return quietly because the values
90ce3da70b43 Initial load
duke
parents:
diff changeset
   406
     * requested will match the current ones.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   407
     */
18215
b2afd66ce6db 8006328: Improve robustness of sound classes
serb
parents: 5506
diff changeset
   408
    public final void open() throws LineUnavailableException {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   409
90ce3da70b43 Initial load
duke
parents:
diff changeset
   410
        if (Printer.trace) Printer.trace("> "+getClass().getName()+".open() - AbstractDataLine");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   411
90ce3da70b43 Initial load
duke
parents:
diff changeset
   412
        // this may throw a LineUnavailableException.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   413
        open(format, bufferSize);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   414
        if (Printer.trace) Printer.trace("< "+getClass().getName()+".open() - AbstractDataLine");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   415
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   416
90ce3da70b43 Initial load
duke
parents:
diff changeset
   417
90ce3da70b43 Initial load
duke
parents:
diff changeset
   418
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   419
     * This should also stop the line.  The closed line should not be running or active.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   420
     * After we close the line, we reset the format and buffer size to the defaults.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   421
     */
18215
b2afd66ce6db 8006328: Improve robustness of sound classes
serb
parents: 5506
diff changeset
   422
    public final void close() {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   423
        //$$fb 2001-10-09: Bug #4517739: avoiding deadlock by synchronizing to mixer !
90ce3da70b43 Initial load
duke
parents:
diff changeset
   424
        synchronized (mixer) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   425
            if (Printer.trace) Printer.trace("> "+getClass().getName()+".close() - in AbstractDataLine.");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   426
90ce3da70b43 Initial load
duke
parents:
diff changeset
   427
            if (isOpen()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   428
90ce3da70b43 Initial load
duke
parents:
diff changeset
   429
                // stop
90ce3da70b43 Initial load
duke
parents:
diff changeset
   430
                stop();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   431
90ce3da70b43 Initial load
duke
parents:
diff changeset
   432
                // set the open state to false and send events
90ce3da70b43 Initial load
duke
parents:
diff changeset
   433
                setOpen(false);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   434
90ce3da70b43 Initial load
duke
parents:
diff changeset
   435
                // close resources for this line
90ce3da70b43 Initial load
duke
parents:
diff changeset
   436
                implClose();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   437
90ce3da70b43 Initial load
duke
parents:
diff changeset
   438
                // release mixer resources for this line
90ce3da70b43 Initial load
duke
parents:
diff changeset
   439
                mixer.close(this);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   440
90ce3da70b43 Initial load
duke
parents:
diff changeset
   441
                // reset format and buffer size to the defaults
90ce3da70b43 Initial load
duke
parents:
diff changeset
   442
                format = defaultFormat;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   443
                bufferSize = defaultBufferSize;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   444
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   445
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   446
        if (Printer.trace) Printer.trace("< "+getClass().getName()+".close() - in AbstractDataLine");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   447
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   448
90ce3da70b43 Initial load
duke
parents:
diff changeset
   449
90ce3da70b43 Initial load
duke
parents:
diff changeset
   450
    // IMPLEMENTATIONS OF ABSTRACT LINE ABSTRACE METHODS
90ce3da70b43 Initial load
duke
parents:
diff changeset
   451
90ce3da70b43 Initial load
duke
parents:
diff changeset
   452
90ce3da70b43 Initial load
duke
parents:
diff changeset
   453
    // ABSTRACT METHODS
90ce3da70b43 Initial load
duke
parents:
diff changeset
   454
90ce3da70b43 Initial load
duke
parents:
diff changeset
   455
    abstract void implOpen(AudioFormat format, int bufferSize) throws LineUnavailableException;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   456
    abstract void implClose();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   457
90ce3da70b43 Initial load
duke
parents:
diff changeset
   458
    abstract void implStart();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   459
    abstract void implStop();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   460
}