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