Path: blob/master/src/java.desktop/macosx/native/libjsound/PLATFORM_API_MacOSX_Utils.cpp
41149 views
/*1* Copyright (c) 2003, 2012, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation. Oracle designates this7* particular file as subject to the "Classpath" exception as provided8* by Oracle in the LICENSE file that accompanied this code.9*10* This code is distributed in the hope that it will be useful, but WITHOUT11* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or12* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License13* version 2 for more details (a copy is included in the LICENSE file that14* accompanied this code).15*16* You should have received a copy of the GNU General Public License version17* 2 along with this work; if not, write to the Free Software Foundation,18* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.19*20* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA21* or visit www.oracle.com if you need additional information or have any22* questions.23*/2425//#define USE_TRACE26//#define USE_ERROR2728#include "PLATFORM_API_MacOSX_Utils.h"2930int MACOSX_DAUDIO_Init() {31static int initialized = 0;32if (!initialized) {33CFRunLoopRef runLoop = NULL;3435OSStatus err = SetAudioObjectProperty(kAudioObjectSystemObject, kAudioObjectPropertyScopeGlobal,36kAudioHardwarePropertyRunLoop, sizeof(CFRunLoopRef), &runLoop);3738if (err) {39OS_ERROR0(err, "MACOSX_DAUDIO_Init(kAudioHardwarePropertyRunLoop)");40} else {41TRACE0("MACOSX_DAUDIO_Init(kAudioHardwarePropertyRunLoop): OK\n");42initialized = 1;43}44}45return initialized;46}4748DeviceList::DeviceList(): count(0), devices(NULL) {49MACOSX_DAUDIO_Init();5051AudioObjectPropertyAddress address = {kAudioHardwarePropertyDevices,52kAudioObjectPropertyScopeGlobal, kAudioObjectPropertyElementMaster};53OSStatus err = AudioObjectAddPropertyListener(kAudioObjectSystemObject, &address, NotificationCallback, this);54if (err) {55OS_ERROR0(err, "AudioObjectAddPropertyListener(kAudioHardwarePropertyDevices)");56} else {57TRACE0("AudioObjectAddPropertyListener(kAudioHardwarePropertyDevices): OK\n");58}59}6061DeviceList::~DeviceList() {62Free();6364AudioObjectPropertyAddress address = {kAudioHardwarePropertyDevices,65kAudioObjectPropertyScopeGlobal, kAudioObjectPropertyElementMaster};66AudioObjectRemovePropertyListener(kAudioObjectSystemObject, &address, NotificationCallback, this);67}6869OSStatus DeviceList::Refresh() {70MutexLock::Locker locker(lock);71Free();7273OSStatus err;74UInt32 size;75err = GetAudioObjectPropertySize(kAudioObjectSystemObject, kAudioObjectPropertyScopeGlobal, kAudioHardwarePropertyDevices, &size);76if (err == noErr) {77devices = (AudioDeviceID *)malloc(size);78err = GetAudioObjectProperty(kAudioObjectSystemObject, kAudioObjectPropertyScopeGlobal, kAudioHardwarePropertyDevices, &size, devices);79if (err == noErr) {80count = size/sizeof(AudioDeviceID);81}82}83if (err) {84OS_ERROR0(err, "DeviceList::Refresh");85Free();86}87#ifdef USE_TRACE88TRACE1("<<DeviceList::Refresh, %d devices {", count);89for (int i=0; i<count; i++) {90if (i > 0)91TRACE0(", ");92TRACE1("0x%x", (int)devices[i]);93}94TRACE0("}\n");95#endif9697return err;98}99100int DeviceList::GetCount() {101MutexLock::Locker locker(lock);102return count;103}104105AudioDeviceID DeviceList::GetDeviceID(int index) {106MutexLock::Locker locker(lock);107return index < 0 ? 0 : index >= count ? 0 : devices[index];108}109110bool DeviceList::GetDeviceInfo(int index, AudioDeviceID *pDeviceID, int stringLength, char *name, char *vendor, char *description, char *version) {111MutexLock::Locker locker(lock);112if (index < 0 || index >= count) {113return false;114}115116AudioDeviceID deviceID = devices[index];117if (pDeviceID != NULL)118*pDeviceID = deviceID;119120OSStatus err = noErr;121122if (name != NULL || description != NULL) {123CFStringRef cfName = NULL;124err = GetAudioObjectProperty(deviceID, kAudioObjectPropertyScopeGlobal,125kAudioObjectPropertyName, sizeof(cfName), &cfName, 1);126if (err == noErr) {127if (name != NULL)128CFStringGetCString(cfName, name, stringLength, kCFStringEncodingUTF8);129if (description)130CFStringGetCString(cfName, description, stringLength, kCFStringEncodingUTF8);131CFRelease(cfName);132}133}134135if (vendor != NULL) {136CFStringRef cfManufacturer = NULL;137err = GetAudioObjectProperty(deviceID, kAudioObjectPropertyScopeGlobal,138kAudioObjectPropertyManufacturer, sizeof(cfManufacturer), &cfManufacturer, 1);139if (err == noErr) {140CFStringGetCString(cfManufacturer, vendor, stringLength, kCFStringEncodingUTF8);141CFRelease(cfManufacturer);142}143}144145return true;146}147148void DeviceList::Free() {149if (devices != NULL) {150free(devices);151devices = NULL;152count = 0;153}154}155156/*static*/157OSStatus DeviceList::NotificationCallback(AudioObjectID inObjectID,158UInt32 inNumberAddresses, const AudioObjectPropertyAddress inAddresses[], void *inClientData)159{160DeviceList *pThis = (DeviceList *)inClientData;161162for (UInt32 i=0; i<inNumberAddresses; i++) {163switch (inAddresses[i].mSelector) {164case kAudioHardwarePropertyDevices:165TRACE0("NOTIFICATION: kAudioHardwarePropertyDevices\n");166break;167}168}169170return noErr;171}172173174175AudioDeviceID GetDefaultDevice(int isSource) {176AudioDeviceID deviceID;177OSStatus err = GetAudioObjectProperty(kAudioObjectSystemObject, kAudioObjectPropertyScopeGlobal,178isSource ? kAudioHardwarePropertyDefaultOutputDevice : kAudioHardwarePropertyDefaultInputDevice,179sizeof(deviceID), &deviceID, 1);180if (err) {181OS_ERROR1(err, "GetDefaultDevice(isSource=%d)", isSource);182return 0;183}184return deviceID;185}186187int GetChannelCount(AudioDeviceID deviceID, int isSource) {188int result = 0;189OSStatus err;190UInt32 size, i;191AudioObjectPropertyScope scope = isSource ? kAudioDevicePropertyScopeOutput : kAudioDevicePropertyScopeInput;192193err = GetAudioObjectPropertySize(deviceID, scope, kAudioDevicePropertyStreamConfiguration, &size);194if (err) {195OS_ERROR2(err, "GetChannelCount(getSize), deviceID=0x%x, isSource=%d", (int)deviceID, isSource);196} else {197AudioBufferList *pBufferList = (AudioBufferList *)malloc(size);198memset(pBufferList, 0, size);199err = GetAudioObjectProperty(deviceID, scope, kAudioDevicePropertyStreamConfiguration, &size, pBufferList);200if (err == noErr) {201for (i=0; i<pBufferList->mNumberBuffers; i++) {202result += pBufferList->mBuffers[i].mNumberChannels;203}204} else {205OS_ERROR2(err, "GetChannelCount(getData), deviceID=0x%x, isSource=%d", (int)deviceID, isSource);206}207free(pBufferList);208}209TRACE2("GetChannelCount (deviceID=0x%x): total %d channels\n", (int)deviceID, result);210return result;211}212213float GetSampleRate(AudioDeviceID deviceID, int isSource) {214Float64 result;215AudioObjectPropertyScope scope = isSource ? kAudioDevicePropertyScopeOutput : kAudioDevicePropertyScopeInput;216OSStatus err = GetAudioObjectProperty(deviceID, scope, kAudioDevicePropertyActualSampleRate, sizeof(result), &result, 1);217if (err) {218OS_ERROR2(err, "GetSampleRate(ActualSampleRate), deviceID=0x%x, isSource=%d", (int)deviceID, isSource);219// try to get NominalSampleRate220err = GetAudioObjectProperty(deviceID, scope, kAudioDevicePropertyNominalSampleRate, sizeof(result), &result, 1);221if (err) {222OS_ERROR2(err, "GetSampleRate(NominalSampleRate), deviceID=0x%x, isSource=%d", (int)deviceID, isSource);223return 0;224}225}226return (float)result;227}228229230OSStatus GetAudioObjectPropertySize(AudioObjectID object, AudioObjectPropertyScope scope, AudioObjectPropertySelector prop, UInt32 *size)231{232const AudioObjectPropertyAddress address = {prop, scope, kAudioObjectPropertyElementMaster};233OSStatus err;234235err = AudioObjectGetPropertyDataSize(object, &address, 0, NULL, size);236237return err;238}239240OSStatus GetAudioObjectProperty(AudioObjectID object, AudioObjectPropertyScope scope, AudioObjectPropertySelector prop, UInt32 *size, void *data)241{242const AudioObjectPropertyAddress address = {prop, scope, kAudioObjectPropertyElementMaster};243OSStatus err;244245err = AudioObjectGetPropertyData(object, &address, 0, NULL, size, data);246247return err;248}249250OSStatus GetAudioObjectProperty(AudioObjectID object, AudioObjectPropertyScope scope, AudioObjectPropertySelector prop, UInt32 size, void *data, int checkSize)251{252const AudioObjectPropertyAddress address = {prop, scope, kAudioObjectPropertyElementMaster};253UInt32 oldSize = size;254OSStatus err;255256err = AudioObjectGetPropertyData(object, &address, 0, NULL, &size, data);257258if (!err && checkSize && size != oldSize)259return kAudioHardwareBadPropertySizeError;260return err;261}262263// wrapper for AudioObjectSetPropertyData (kAudioObjectPropertyElementMaster)264OSStatus SetAudioObjectProperty(AudioObjectID object, AudioObjectPropertyScope scope, AudioObjectPropertySelector prop, UInt32 size, void *data)265{266AudioObjectPropertyAddress address = {prop, scope, kAudioObjectPropertyElementMaster};267268OSStatus err = AudioObjectSetPropertyData(object, &address, 0, NULL, size, data);269270return err;271}272273274