author | serb |
Wed, 18 May 2016 20:40:17 +0300 | |
changeset 38981 | 9521cb19e297 |
parent 25859 | 3317bb8137f4 |
child 40444 | afabcfc2f3ef |
permissions | -rw-r--r-- |
2 | 1 |
/* |
38981
9521cb19e297
8156169: Some sound tests rarely hangs because of incorrect synchronization
serb
parents:
25859
diff
changeset
|
2 |
* Copyright (c) 1999, 2016, 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.midi.*; |
|
29 |
||
30 |
||
31 |
||
32 |
/** |
|
33 |
* MidiInDevice class representing functionality of MidiIn devices. |
|
34 |
* |
|
35 |
* @author David Rivas |
|
36 |
* @author Kara Kytle |
|
37 |
* @author Florian Bomers |
|
38 |
*/ |
|
18215 | 39 |
final class MidiInDevice extends AbstractMidiDevice implements Runnable { |
2 | 40 |
|
38981
9521cb19e297
8156169: Some sound tests rarely hangs because of incorrect synchronization
serb
parents:
25859
diff
changeset
|
41 |
private volatile Thread midiInThread; |
2 | 42 |
|
43 |
// CONSTRUCTOR |
|
44 |
||
45 |
MidiInDevice(AbstractMidiDeviceProvider.Info info) { |
|
46 |
super(info); |
|
47 |
if(Printer.trace) Printer.trace("MidiInDevice CONSTRUCTOR"); |
|
48 |
} |
|
49 |
||
50 |
||
51 |
// IMPLEMENTATION OF ABSTRACT MIDI DEVICE METHODS |
|
52 |
||
53 |
// $$kk: 06.24.99: i have this both opening and starting the midi in device. |
|
54 |
// may want to separate these?? |
|
55 |
protected synchronized void implOpen() throws MidiUnavailableException { |
|
56 |
if (Printer.trace) Printer.trace("> MidiInDevice: implOpen()"); |
|
57 |
||
58 |
int index = ((MidiInDeviceProvider.MidiInDeviceInfo)getDeviceInfo()).getIndex(); |
|
59 |
id = nOpen(index); // can throw MidiUnavailableException |
|
60 |
||
61 |
if (id == 0) { |
|
62 |
throw new MidiUnavailableException("Unable to open native device"); |
|
63 |
} |
|
64 |
||
65 |
// create / start a thread to get messages |
|
66 |
if (midiInThread == null) { |
|
67 |
midiInThread = JSSecurityManager.createThread(this, |
|
68 |
"Java Sound MidiInDevice Thread", // name |
|
69 |
false, // daemon |
|
70 |
-1, // priority |
|
71 |
true); // doStart |
|
72 |
} |
|
73 |
||
74 |
nStart(id); // can throw MidiUnavailableException |
|
75 |
if (Printer.trace) Printer.trace("< MidiInDevice: implOpen() completed"); |
|
76 |
} |
|
77 |
||
78 |
||
79 |
// $$kk: 06.24.99: i have this both stopping and closing the midi in device. |
|
80 |
// may want to separate these?? |
|
81 |
protected synchronized void implClose() { |
|
82 |
if (Printer.trace) Printer.trace("> MidiInDevice: implClose()"); |
|
83 |
long oldId = id; |
|
84 |
id = 0; |
|
85 |
||
86 |
super.implClose(); |
|
87 |
||
88 |
// close the device |
|
89 |
nStop(oldId); |
|
90 |
if (midiInThread != null) { |
|
91 |
try { |
|
92 |
midiInThread.join(1000); |
|
93 |
} catch (InterruptedException e) { |
|
94 |
// IGNORE EXCEPTION |
|
95 |
} |
|
96 |
} |
|
97 |
nClose(oldId); |
|
98 |
if (Printer.trace) Printer.trace("< MidiInDevice: implClose() completed"); |
|
99 |
} |
|
100 |
||
101 |
||
102 |
public long getMicrosecondPosition() { |
|
103 |
long timestamp = -1; |
|
104 |
if (isOpen()) { |
|
105 |
timestamp = nGetTimeStamp(id); |
|
106 |
} |
|
107 |
return timestamp; |
|
108 |
} |
|
109 |
||
110 |
||
111 |
// OVERRIDES OF ABSTRACT MIDI DEVICE METHODS |
|
112 |
||
113 |
||
114 |
protected boolean hasTransmitters() { |
|
115 |
return true; |
|
116 |
} |
|
117 |
||
118 |
||
119 |
protected Transmitter createTransmitter() { |
|
120 |
return new MidiInTransmitter(); |
|
121 |
} |
|
122 |
||
123 |
/** |
|
124 |
* An own class to distinguish the class name from |
|
125 |
* the transmitter of other devices |
|
126 |
*/ |
|
18215 | 127 |
private final class MidiInTransmitter extends BasicTransmitter { |
2 | 128 |
private MidiInTransmitter() { |
129 |
super(); |
|
130 |
} |
|
131 |
} |
|
132 |
||
133 |
// RUNNABLE METHOD |
|
134 |
||
135 |
public void run() { |
|
136 |
// while the device is started, keep trying to get messages. |
|
137 |
// this thread returns from native code whenever stop() or close() is called |
|
138 |
while (id!=0) { |
|
139 |
// go into native code and retrieve messages |
|
140 |
nGetMessages(id); |
|
141 |
if (id!=0) { |
|
142 |
try { |
|
143 |
Thread.sleep(1); |
|
144 |
} catch (InterruptedException e) {} |
|
145 |
} |
|
146 |
} |
|
147 |
if(Printer.verbose) Printer.verbose("MidiInDevice Thread exit"); |
|
148 |
// let the thread exit |
|
149 |
midiInThread = null; |
|
150 |
} |
|
151 |
||
152 |
||
153 |
// CALLBACKS FROM NATIVE |
|
154 |
||
155 |
/** |
|
156 |
* Callback from native code when a short MIDI event is received from hardware. |
|
157 |
* @param packedMsg: status | data1 << 8 | data2 << 8 |
|
158 |
* @param timeStamp time-stamp in microseconds |
|
159 |
*/ |
|
160 |
void callbackShortMessage(int packedMsg, long timeStamp) { |
|
161 |
if (packedMsg == 0 || id == 0) { |
|
162 |
return; |
|
163 |
} |
|
164 |
||
165 |
/*if(Printer.verbose) { |
|
166 |
int status = packedMsg & 0xFF; |
|
167 |
int data1 = (packedMsg & 0xFF00)>>8; |
|
168 |
int data2 = (packedMsg & 0xFF0000)>>16; |
|
169 |
Printer.verbose(">> MidiInDevice callbackShortMessage: status: " + status + " data1: " + data1 + " data2: " + data2 + " timeStamp: " + timeStamp); |
|
170 |
}*/ |
|
171 |
||
172 |
getTransmitterList().sendMessage(packedMsg, timeStamp); |
|
173 |
} |
|
174 |
||
175 |
void callbackLongMessage(byte[] data, long timeStamp) { |
|
176 |
if (id == 0 || data == null) { |
|
177 |
return; |
|
178 |
} |
|
179 |
getTransmitterList().sendMessage(data, timeStamp); |
|
180 |
} |
|
181 |
||
182 |
// NATIVE METHODS |
|
183 |
||
184 |
private native long nOpen(int index) throws MidiUnavailableException; |
|
185 |
private native void nClose(long id); |
|
186 |
||
187 |
private native void nStart(long id) throws MidiUnavailableException; |
|
188 |
private native void nStop(long id); |
|
189 |
private native long nGetTimeStamp(long id); |
|
190 |
||
191 |
// go into native code and get messages. May be blocking |
|
192 |
private native void nGetMessages(long id); |
|
193 |
||
194 |
||
195 |
} |