jdk/src/share/classes/com/sun/media/sound/Platform.java
author serb
Thu, 07 Aug 2014 17:02:48 +0400
changeset 26030 576ffa819e23
parent 18215 b2afd66ce6db
permissions -rw-r--r--
8054372: Cleanup of com.sun.media.sound packages Reviewed-by: azvegint, prr
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
26030
576ffa819e23 8054372: Cleanup of com.sun.media.sound packages
serb
parents: 18215
diff changeset
     2
 * Copyright (c) 1999, 2014, 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
18215
b2afd66ce6db 8006328: Improve robustness of sound classes
serb
parents: 5506
diff changeset
    28
import java.security.AccessController;
b2afd66ce6db 8006328: Improve robustness of sound classes
serb
parents: 5506
diff changeset
    29
import java.security.PrivilegedAction;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    30
import java.util.StringTokenizer;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
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
 * Audio configuration class for exposing attributes specific to the platform or system.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
 * @author Kara Kytle
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
 * @author Florian Bomers
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
 */
18215
b2afd66ce6db 8006328: Improve robustness of sound classes
serb
parents: 5506
diff changeset
    40
final class Platform {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
    // STATIC FINAL CHARACTERISTICS
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
    // native library we need to load
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
    private static final String libNameMain     = "jsound";
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
    private static final String libNameALSA     = "jsoundalsa";
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
    private static final String libNameDSound   = "jsoundds";
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
    // extra libs handling: bit flags for each different library
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
    public static final int LIB_MAIN     = 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
    public static final int LIB_ALSA     = 2;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
    public static final int LIB_DSOUND   = 4;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
    // bit field of the constants above. Willbe set in loadLibraries
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
    private static int loadedLibs = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
    // features: the main native library jsound reports which feature is
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
    // contained in which lib
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
    public static final int FEATURE_MIDIIO       = 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
    public static final int FEATURE_PORTS        = 2;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
    public static final int FEATURE_DIRECT_AUDIO = 3;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
    // SYSTEM CHARACTERISTICS
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
    // vary according to hardware architecture
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
    // signed8 (use signed 8-bit values) is true for everything we support except for
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
    // the solaris sbpro card.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
    // we'll leave it here as a variable; in the future we may need this in java.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
    // wait, is that true?  i'm not sure.  i think solaris takes unsigned data?
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
    // $$kk: 03.11.99: i think solaris takes unsigned 8-bit or signed 16-bit data....
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
    private static boolean signed8;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
    // intel is little-endian.  sparc is big-endian.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
    private static boolean bigEndian;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
    static {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
        if(Printer.trace)Printer.trace(">> Platform.java: static");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
        loadLibraries();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
        readProperties();
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
     * Private constructor.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
    private Platform() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
    // METHODS FOR INTERNAL IMPLEMENTATION USE
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
     * Dummy method for forcing initialization.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
    static void initialize() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
        if(Printer.trace)Printer.trace("Platform: initialize()");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
    }
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
     * Determine whether the system is big-endian.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
    static boolean isBigEndian() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
        return bigEndian;
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 takes signed 8-bit data.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
    static boolean isSigned8() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
        return signed8;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
    // PRIVATE METHODS
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
     * Load the native library or libraries.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
    private static void loadLibraries() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
        if(Printer.trace)Printer.trace(">>Platform.loadLibraries");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
26030
576ffa819e23 8054372: Cleanup of com.sun.media.sound packages
serb
parents: 18215
diff changeset
   129
        // load the main library
576ffa819e23 8054372: Cleanup of com.sun.media.sound packages
serb
parents: 18215
diff changeset
   130
        AccessController.doPrivileged((PrivilegedAction<Void>) () -> {
576ffa819e23 8054372: Cleanup of com.sun.media.sound packages
serb
parents: 18215
diff changeset
   131
            System.loadLibrary(libNameMain);
576ffa819e23 8054372: Cleanup of com.sun.media.sound packages
serb
parents: 18215
diff changeset
   132
            return null;
576ffa819e23 8054372: Cleanup of com.sun.media.sound packages
serb
parents: 18215
diff changeset
   133
        });
576ffa819e23 8054372: Cleanup of com.sun.media.sound packages
serb
parents: 18215
diff changeset
   134
        // just for the heck of it...
576ffa819e23 8054372: Cleanup of com.sun.media.sound packages
serb
parents: 18215
diff changeset
   135
        loadedLibs |= LIB_MAIN;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
        // now try to load extra libs. They are defined at compile time in the Makefile
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
        // with the define EXTRA_SOUND_JNI_LIBS
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
        String extraLibs = nGetExtraLibraries();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
        // the string is the libraries, separated by white space
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
        StringTokenizer st = new StringTokenizer(extraLibs);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
        while (st.hasMoreTokens()) {
18215
b2afd66ce6db 8006328: Improve robustness of sound classes
serb
parents: 5506
diff changeset
   143
            final String lib = st.nextToken();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
            try {
26030
576ffa819e23 8054372: Cleanup of com.sun.media.sound packages
serb
parents: 18215
diff changeset
   145
                AccessController.doPrivileged((PrivilegedAction<Void>) () -> {
576ffa819e23 8054372: Cleanup of com.sun.media.sound packages
serb
parents: 18215
diff changeset
   146
                    System.loadLibrary(lib);
576ffa819e23 8054372: Cleanup of com.sun.media.sound packages
serb
parents: 18215
diff changeset
   147
                    return null;
18215
b2afd66ce6db 8006328: Improve robustness of sound classes
serb
parents: 5506
diff changeset
   148
                });
b2afd66ce6db 8006328: Improve robustness of sound classes
serb
parents: 5506
diff changeset
   149
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
                if (lib.equals(libNameALSA)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
                    loadedLibs |= LIB_ALSA;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
                    if (Printer.debug) Printer.debug("Loaded ALSA lib successfully.");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
                } else if (lib.equals(libNameDSound)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
                    loadedLibs |= LIB_DSOUND;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
                    if (Printer.debug) Printer.debug("Loaded DirectSound lib successfully.");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
                } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
                    if (Printer.err) Printer.err("Loaded unknown lib '"+lib+"' successfully.");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
            } catch (Throwable t) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
                if (Printer.err) Printer.err("Couldn't load library "+lib+": "+t.toString());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   163
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   164
90ce3da70b43 Initial load
duke
parents:
diff changeset
   165
90ce3da70b43 Initial load
duke
parents:
diff changeset
   166
    static boolean isMidiIOEnabled() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   167
        return isFeatureLibLoaded(FEATURE_MIDIIO);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   168
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   169
90ce3da70b43 Initial load
duke
parents:
diff changeset
   170
    static boolean isPortsEnabled() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   171
        return isFeatureLibLoaded(FEATURE_PORTS);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   172
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   173
90ce3da70b43 Initial load
duke
parents:
diff changeset
   174
    static boolean isDirectAudioEnabled() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   175
        return isFeatureLibLoaded(FEATURE_DIRECT_AUDIO);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   176
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   177
90ce3da70b43 Initial load
duke
parents:
diff changeset
   178
    private static boolean isFeatureLibLoaded(int feature) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   179
        if (Printer.debug) Printer.debug("Platform: Checking for feature "+feature+"...");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   180
        int requiredLib = nGetLibraryForFeature(feature);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   181
        boolean isLoaded = (requiredLib != 0) && ((loadedLibs & requiredLib) == requiredLib);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   182
        if (Printer.debug) Printer.debug("          ...needs library "+requiredLib+". Result is loaded="+isLoaded);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   183
        return isLoaded;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   184
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   185
90ce3da70b43 Initial load
duke
parents:
diff changeset
   186
    // the following native methods are implemented in Platform.c
90ce3da70b43 Initial load
duke
parents:
diff changeset
   187
    private native static boolean nIsBigEndian();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   188
    private native static boolean nIsSigned8();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   189
    private native static String nGetExtraLibraries();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   190
    private native static int nGetLibraryForFeature(int feature);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   191
90ce3da70b43 Initial load
duke
parents:
diff changeset
   192
90ce3da70b43 Initial load
duke
parents:
diff changeset
   193
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   194
     * Read the required system properties.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   195
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   196
    private static void readProperties() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   197
        // $$fb 2002-03-06: implement check for endianness in native. Facilitates porting !
90ce3da70b43 Initial load
duke
parents:
diff changeset
   198
        bigEndian = nIsBigEndian();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   199
        signed8 = nIsSigned8(); // Solaris on Sparc: signed, all others unsigned
90ce3da70b43 Initial load
duke
parents:
diff changeset
   200
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   201
}