src/java.desktop/solaris/native/libjsound/PLATFORM_API_SolarisOS_Utils.c
author jlaskey
Thu, 14 Nov 2019 12:39:56 -0400
branchJDK-8193209-branch
changeset 59087 effb66aab08b
parent 49289 148e29df1644
permissions -rw-r--r--
[mq]: other
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) 2002, 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
#define USE_ERROR
90ce3da70b43 Initial load
duke
parents:
diff changeset
    27
#define USE_TRACE
90ce3da70b43 Initial load
duke
parents:
diff changeset
    28
90ce3da70b43 Initial load
duke
parents:
diff changeset
    29
#include "PLATFORM_API_SolarisOS_Utils.h"
90ce3da70b43 Initial load
duke
parents:
diff changeset
    30
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
#define MAX_AUDIO_DEVICES 20
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
// not thread safe...
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
static AudioDevicePath globalADPaths[MAX_AUDIO_DEVICES];
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
static int globalADCount = -1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
static int globalADCacheTime = -1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
/* how many seconds do we cache devices */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
#define AD_CACHE_TIME 30
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
// return seconds
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
long getTimeInSeconds() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
    struct timeval tv;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
    gettimeofday(&tv, NULL);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
    return tv.tv_sec;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
int getAudioDeviceCount() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
    int count = MAX_AUDIO_DEVICES;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
    getAudioDevices(globalADPaths, &count);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
    return count;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
/* returns TRUE if the path exists at all */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
int addAudioDevice(char* path, AudioDevicePath* adPath, int* count) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
    int i;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
    int found = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
    int fileExists = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
    // not thread safe...
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
    static struct stat statBuf;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
    // get stats on the file
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
    if (stat(path, &statBuf) == 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
        // file exists.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
        fileExists = 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
        // If it is not yet in the adPath array, add it to the array
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
        for (i = 0; i < *count; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
            if (adPath[i].st_ino == statBuf.st_ino
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
                && adPath[i].st_dev == statBuf.st_dev) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
                found = 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
                break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
        if (!found) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
            adPath[*count].st_ino = statBuf.st_ino;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
            adPath[*count].st_dev = statBuf.st_dev;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
            strncpy(adPath[*count].path, path, MAX_NAME_LENGTH);
23888
89df6a57f25f 8029730: Improve audio device additions
serb
parents: 5506
diff changeset
    79
            adPath[*count].path[MAX_NAME_LENGTH - 1] = 0;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
            (*count)++;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
            TRACE1("Added audio device %s\n", path);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
    return fileExists;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
void getAudioDevices(AudioDevicePath* adPath, int* count) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
    int maxCount = *count;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
    char* audiodev;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
    char devsound[15];
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
    int i;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
    long timeInSeconds = getTimeInSeconds();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
    if (globalADCount < 0
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
        || (getTimeInSeconds() - globalADCacheTime) > AD_CACHE_TIME
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
        || (adPath != globalADPaths)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
        *count = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
        // first device, if set, is AUDIODEV variable
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
        audiodev = getenv("AUDIODEV");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
        if (audiodev != NULL && audiodev[0] != 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
            addAudioDevice(audiodev, adPath, count);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
        // then try /dev/audio
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
        addAudioDevice("/dev/audio", adPath, count);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
        // then go through all of the /dev/sound/? devices
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
        for (i = 0; i < 100; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
            sprintf(devsound, "/dev/sound/%d", i);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
            if (!addAudioDevice(devsound, adPath, count)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
                break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
        if (adPath == globalADPaths) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
            /* commit cache */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
            globalADCount = *count;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
            /* set cache time */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
            globalADCacheTime = timeInSeconds;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
    } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
        /* return cache */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
        *count = globalADCount;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
    // that's it
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
int getAudioDeviceDescriptionByIndex(int index, AudioDeviceDescription* adDesc, int getNames) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
    int count = MAX_AUDIO_DEVICES;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
    int ret = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
    getAudioDevices(globalADPaths, &count);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
    if (index>=0 && index < count) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
        ret = getAudioDeviceDescription(globalADPaths[index].path, adDesc, getNames);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
    return ret;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
int getAudioDeviceDescription(char* path, AudioDeviceDescription* adDesc, int getNames) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
    int fd;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
    int mixerMode;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
    int len;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
    audio_info_t info;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
    audio_device_t deviceInfo;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
    strncpy(adDesc->path, path, MAX_NAME_LENGTH);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
    adDesc->path[MAX_NAME_LENGTH] = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
    strcpy(adDesc->pathctl, adDesc->path);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
    strcat(adDesc->pathctl, "ctl");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
    strcpy(adDesc->name, adDesc->path);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
    adDesc->vendor[0] = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
    adDesc->version[0] = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
    adDesc->description[0] = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
    adDesc->maxSimulLines = 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
    // try to open the pseudo device and get more information
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
    fd = open(adDesc->pathctl, O_WRONLY | O_NONBLOCK);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
    if (fd >= 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
        close(fd);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
        if (getNames) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
            fd = open(adDesc->pathctl, O_RDONLY);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
            if (fd >= 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
                if (ioctl(fd, AUDIO_GETDEV, &deviceInfo) >= 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
                    strncpy(adDesc->vendor, deviceInfo.name, MAX_AUDIO_DEV_LEN);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   163
                    adDesc->vendor[MAX_AUDIO_DEV_LEN] = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   164
                    strncpy(adDesc->version, deviceInfo.version, MAX_AUDIO_DEV_LEN);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   165
                    adDesc->version[MAX_AUDIO_DEV_LEN] = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   166
                    /* add config string to the dev name
90ce3da70b43 Initial load
duke
parents:
diff changeset
   167
                     * creates a string like "/dev/audio (onboard1)"
90ce3da70b43 Initial load
duke
parents:
diff changeset
   168
                     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   169
                    len = strlen(adDesc->name) + 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   170
                    if (MAX_NAME_LENGTH - len > 3) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   171
                        strcat(adDesc->name, " (");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   172
                        strncat(adDesc->name, deviceInfo.config, MAX_NAME_LENGTH - len);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   173
                        strcat(adDesc->name, ")");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   174
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   175
                    adDesc->name[MAX_NAME_LENGTH-1] = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   176
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   177
                if (ioctl(fd, AUDIO_MIXERCTL_GET_MODE, &mixerMode) >= 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   178
                    if (mixerMode == AM_MIXER_MODE) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   179
                        TRACE1(" getAudioDeviceDescription: %s is in mixer mode\n", adDesc->path);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   180
                        adDesc->maxSimulLines = -1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   181
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   182
                } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   183
                    ERROR1("ioctl AUDIO_MIXERCTL_GET_MODE failed on %s!\n", adDesc->path);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   184
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   185
                close(fd);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   186
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   187
                ERROR1("could not open %s!\n", adDesc->pathctl);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   188
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   189
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   190
        return 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   191
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   192
    return 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   193
}