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