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