jdk/src/share/classes/com/sun/media/sound/Platform.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: 1846
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: 1846
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: 1846
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: 1846
diff changeset
    21
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 1846
diff changeset
    22
 * or visit www.oracle.com if you need additional information or have any
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 1846
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.StringTokenizer;
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
 * Audio configuration class for exposing attributes specific to the platform or system.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
 * @author Kara Kytle
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
 * @author Florian Bomers
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
class Platform {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
    // STATIC FINAL CHARACTERISTICS
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
    // native library we need to load
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
    private static final String libNameMain     = "jsound";
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
    private static final String libNameALSA     = "jsoundalsa";
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
    private static final String libNameDSound   = "jsoundds";
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
    // extra libs handling: bit flags for each different library
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
    public static final int LIB_MAIN     = 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
    public static final int LIB_ALSA     = 2;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
    public static final int LIB_DSOUND   = 4;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
    // bit field of the constants above. Willbe set in loadLibraries
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
    private static int loadedLibs = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
    // features: the main native library jsound reports which feature is
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
    // contained in which lib
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
    public static final int FEATURE_MIDIIO       = 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
    public static final int FEATURE_PORTS        = 2;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
    public static final int FEATURE_DIRECT_AUDIO = 3;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
    // SYSTEM CHARACTERISTICS
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
    // vary according to hardware architecture
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
    // signed8 (use signed 8-bit values) is true for everything we support except for
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
    // the solaris sbpro card.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
    // we'll leave it here as a variable; in the future we may need this in java.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
    // wait, is that true?  i'm not sure.  i think solaris takes unsigned data?
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
    // $$kk: 03.11.99: i think solaris takes unsigned 8-bit or signed 16-bit data....
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
    private static boolean signed8;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
    // intel is little-endian.  sparc is big-endian.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
    private static boolean bigEndian;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
    // this is the value of the "java.home" system property.  i am looking it up here
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
    // for use when trying to load the soundbank, just so
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
    // that all the privileged code is localized in this file....
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
    private static String javahome;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
    // this is the value of the "java.class.path" system property
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
    private static String classpath;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
    static {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
        if(Printer.trace)Printer.trace(">> Platform.java: static");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
        loadLibraries();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
        readProperties();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
     * Private constructor.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
    private Platform() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
    // METHODS FOR INTERNAL IMPLEMENTATION USE
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
     * Dummy method for forcing initialization.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
    static void initialize() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
        if(Printer.trace)Printer.trace("Platform: initialize()");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
    }
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
     * Determine whether the system is big-endian.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
    static boolean isBigEndian() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
        return bigEndian;
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
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
     * Determine whether the system takes signed 8-bit data.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
    static boolean isSigned8() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
        return signed8;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
     * Obtain javahome.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
     * $$kk: 04.16.99: this is *bad*!!
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
    static String getJavahome() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
        return javahome;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
     * Obtain classpath.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
     * $$jb: 04.21.99: this is *bad* too!!
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
    static String getClasspath() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
        return classpath;
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
    // PRIVATE METHODS
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
     * Load the native library or libraries.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
    private static void loadLibraries() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
        if(Printer.trace)Printer.trace(">>Platform.loadLibraries");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
        try {
1846
4a53d636e2f4 6702956: OpenJDK: replace encumbered code (software synthesizer)
amenkov
parents: 2
diff changeset
   159
            // load the main library
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
            JSSecurityManager.loadLibrary(libNameMain);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
            // just for the heck of it...
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
            loadedLibs |= LIB_MAIN;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   163
        } catch (SecurityException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   164
            if(Printer.err)Printer.err("Security exception loading main native library.  JavaSound requires access to these resources.");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   165
            throw(e);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   166
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   167
90ce3da70b43 Initial load
duke
parents:
diff changeset
   168
        // now try to load extra libs. They are defined at compile time in the Makefile
90ce3da70b43 Initial load
duke
parents:
diff changeset
   169
        // with the define EXTRA_SOUND_JNI_LIBS
90ce3da70b43 Initial load
duke
parents:
diff changeset
   170
        String extraLibs = nGetExtraLibraries();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   171
        // the string is the libraries, separated by white space
90ce3da70b43 Initial load
duke
parents:
diff changeset
   172
        StringTokenizer st = new StringTokenizer(extraLibs);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   173
        while (st.hasMoreTokens()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   174
            String lib = st.nextToken();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   175
            try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   176
                JSSecurityManager.loadLibrary(lib);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   177
                if (lib.equals(libNameALSA)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   178
                    loadedLibs |= LIB_ALSA;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   179
                    if (Printer.debug) Printer.debug("Loaded ALSA lib successfully.");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   180
                } else if (lib.equals(libNameDSound)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   181
                    loadedLibs |= LIB_DSOUND;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   182
                    if (Printer.debug) Printer.debug("Loaded DirectSound lib successfully.");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   183
                } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   184
                    if (Printer.err) Printer.err("Loaded unknown lib '"+lib+"' successfully.");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   185
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   186
            } catch (Throwable t) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   187
                if (Printer.err) Printer.err("Couldn't load library "+lib+": "+t.toString());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   188
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   189
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   190
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   191
90ce3da70b43 Initial load
duke
parents:
diff changeset
   192
90ce3da70b43 Initial load
duke
parents:
diff changeset
   193
    static boolean isMidiIOEnabled() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   194
        return isFeatureLibLoaded(FEATURE_MIDIIO);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   195
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   196
90ce3da70b43 Initial load
duke
parents:
diff changeset
   197
    static boolean isPortsEnabled() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   198
        return isFeatureLibLoaded(FEATURE_PORTS);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   199
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   200
90ce3da70b43 Initial load
duke
parents:
diff changeset
   201
    static boolean isDirectAudioEnabled() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   202
        return isFeatureLibLoaded(FEATURE_DIRECT_AUDIO);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   203
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   204
90ce3da70b43 Initial load
duke
parents:
diff changeset
   205
    private static boolean isFeatureLibLoaded(int feature) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   206
        if (Printer.debug) Printer.debug("Platform: Checking for feature "+feature+"...");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   207
        int requiredLib = nGetLibraryForFeature(feature);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   208
        boolean isLoaded = (requiredLib != 0) && ((loadedLibs & requiredLib) == requiredLib);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   209
        if (Printer.debug) Printer.debug("          ...needs library "+requiredLib+". Result is loaded="+isLoaded);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   210
        return isLoaded;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   211
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   212
90ce3da70b43 Initial load
duke
parents:
diff changeset
   213
    // the following native methods are implemented in Platform.c
90ce3da70b43 Initial load
duke
parents:
diff changeset
   214
    private native static boolean nIsBigEndian();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   215
    private native static boolean nIsSigned8();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   216
    private native static String nGetExtraLibraries();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   217
    private native static int nGetLibraryForFeature(int feature);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   218
90ce3da70b43 Initial load
duke
parents:
diff changeset
   219
90ce3da70b43 Initial load
duke
parents:
diff changeset
   220
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   221
     * Read the required system properties.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   222
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   223
    private static void readProperties() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   224
        // $$fb 2002-03-06: implement check for endianness in native. Facilitates porting !
90ce3da70b43 Initial load
duke
parents:
diff changeset
   225
        bigEndian = nIsBigEndian();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   226
        signed8 = nIsSigned8(); // Solaris on Sparc: signed, all others unsigned
90ce3da70b43 Initial load
duke
parents:
diff changeset
   227
        javahome = JSSecurityManager.getProperty("java.home");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   228
        classpath = JSSecurityManager.getProperty("java.class.path");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   229
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   230
}