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