Path: blob/master/src/java.desktop/share/native/libjsound/PortMixer.c
41149 views
/*1* Copyright (c) 2002, 2014, 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_ERROR272829#include <jni.h>30#include <jni_util.h>31#include "SoundDefs.h"32#include "Ports.h"33#include "Utilities.h"34#include "com_sun_media_sound_PortMixer.h"353637//////////////////////////////////////////// PortMixer ////////////////////////////////////////////3839JNIEXPORT jlong JNICALL Java_com_sun_media_sound_PortMixer_nOpen40(JNIEnv *env, jclass cls, jint mixerIndex) {4142jlong ret = 0;43#if USE_PORTS == TRUE44ret = (jlong) (INT_PTR) PORT_Open(mixerIndex);45#endif46return ret;47}4849JNIEXPORT void JNICALL Java_com_sun_media_sound_PortMixer_nClose50(JNIEnv *env, jclass cls, jlong id) {5152#if USE_PORTS == TRUE53if (id != 0) {54PORT_Close((void*) (INT_PTR) id);55}56#endif57}5859JNIEXPORT jint JNICALL Java_com_sun_media_sound_PortMixer_nGetPortCount60(JNIEnv *env, jclass cls, jlong id) {6162jint ret = 0;63#if USE_PORTS == TRUE64if (id != 0) {65ret = (jint) PORT_GetPortCount((void*) (INT_PTR) id);66}67#endif68return ret;69}707172JNIEXPORT jint JNICALL Java_com_sun_media_sound_PortMixer_nGetPortType73(JNIEnv *env, jclass cls, jlong id, jint portIndex) {7475jint ret = 0;76TRACE1("Java_com_sun_media_sound_PortMixer_nGetPortType(%d).\n", portIndex);7778#if USE_PORTS == TRUE79if (id != 0) {80ret = (jint) PORT_GetPortType((void*) (INT_PTR) id, portIndex);81}82#endif8384TRACE1("Java_com_sun_media_sound_PortMixerProvider_nGetPortType returning %d.\n", ret);85return ret;86}8788JNIEXPORT jstring JNICALL Java_com_sun_media_sound_PortMixer_nGetPortName89(JNIEnv *env, jclass cls, jlong id, jint portIndex) {9091char str[PORT_STRING_LENGTH];92jstring jString = NULL;93TRACE1("Java_com_sun_media_sound_PortMixer_nGetPortName(%d).\n", portIndex);9495str[0] = 0;96#if USE_PORTS == TRUE97if (id != 0) {98PORT_GetPortName((void*) (INT_PTR) id, portIndex, str, PORT_STRING_LENGTH);99}100#endif101jString = (*env)->NewStringUTF(env, str);102103TRACE1("Java_com_sun_media_sound_PortMixerProvider_nGetName returning \"%s\".\n", str);104return jString;105}106107JNIEXPORT void JNICALL Java_com_sun_media_sound_PortMixer_nControlSetIntValue108(JNIEnv *env, jclass cls, jlong controlID, jint value) {109#if USE_PORTS == TRUE110if (controlID != 0) {111PORT_SetIntValue((void*) (UINT_PTR) controlID, (INT32) value);112}113#endif114}115116JNIEXPORT jint JNICALL Java_com_sun_media_sound_PortMixer_nControlGetIntValue117(JNIEnv *env, jclass cls, jlong controlID) {118INT32 ret = 0;119#if USE_PORTS == TRUE120if (controlID != 0) {121ret = PORT_GetIntValue((void*) (UINT_PTR) controlID);122}123#endif124return (jint) ret;125}126127JNIEXPORT void JNICALL Java_com_sun_media_sound_PortMixer_nControlSetFloatValue128(JNIEnv *env, jclass cls, jlong controlID, jfloat value) {129#if USE_PORTS == TRUE130if (controlID != 0) {131PORT_SetFloatValue((void*) (UINT_PTR) controlID, (float) value);132}133#endif134}135136JNIEXPORT jfloat JNICALL Java_com_sun_media_sound_PortMixer_nControlGetFloatValue137(JNIEnv *env, jclass cls, jlong controlID) {138float ret = 0;139#if USE_PORTS == TRUE140if (controlID != 0) {141ret = PORT_GetFloatValue((void*) (UINT_PTR) controlID);142}143#endif144return (jfloat) ret;145}146147/* ************************************** native control creation support ********************* */148149// contains all the needed references so that the platform dependent code can call JNI wrapper functions150typedef struct tag_ControlCreatorJNI {151// this member is seen by the platform dependent code152PortControlCreator creator;153// general JNI variables154JNIEnv *env;155// the vector to be filled with controls (initialized before usage)156jobject vector;157jmethodID vectorAddElement;158// control specific constructors (initialized on demand)159jclass boolCtrlClass;160jmethodID boolCtrlConstructor; // signature (JLjava/lang/String;)V161jclass controlClass; // class of javax.sound.sampled.Control162jclass compCtrlClass;163jmethodID compCtrlConstructor; // signature (Ljava/lang/String;[Ljavax/sound/sampled/Control;)V164jclass floatCtrlClass;165jmethodID floatCtrlConstructor1; // signature (JLjava/lang/String;FFFLjava/lang/String;)V166jmethodID floatCtrlConstructor2; // signature (JIFFFLjava/lang/String;)V167} ControlCreatorJNI;168169170void* PORT_NewBooleanControl(void* creatorV, void* controlID, char* type) {171ControlCreatorJNI* creator = (ControlCreatorJNI*) creatorV;172jobject ctrl = NULL;173jstring typeString;174175#ifdef USE_TRACE176if (((UINT_PTR) type) <= CONTROL_TYPE_MAX) {177TRACE1("PORT_NewBooleanControl: creating '%d'\n", (int) (UINT_PTR) type);178} else {179TRACE1("PORT_NewBooleanControl: creating '%s'\n", type);180}181#endif182if (!creator->boolCtrlClass) {183// retrieve class and constructor of PortMixer.BoolCtrl184creator->boolCtrlClass = (*creator->env)->FindClass(creator->env, IMPLEMENTATION_PACKAGE_NAME"/PortMixer$BoolCtrl");185if (!creator->boolCtrlClass) {186ERROR0("PORT_NewBooleanControl: boolCtrlClass is NULL\n");187return NULL;188}189creator->boolCtrlConstructor = (*creator->env)->GetMethodID(creator->env, creator->boolCtrlClass,190"<init>", "(JLjava/lang/String;)V");191if (!creator->boolCtrlConstructor) {192ERROR0("PORT_NewBooleanControl: boolCtrlConstructor is NULL\n");193return NULL;194}195}196if (type == CONTROL_TYPE_MUTE) {197type = "Mute";198}199else if (type == CONTROL_TYPE_SELECT) {200type = "Select";201}202203typeString = (*creator->env)->NewStringUTF(creator->env, type);204CHECK_NULL_RETURN(typeString, (void*) ctrl);205ctrl = (*creator->env)->NewObject(creator->env, creator->boolCtrlClass,206creator->boolCtrlConstructor,207(jlong) (UINT_PTR) controlID, typeString);208if (!ctrl) {209ERROR0("PORT_NewBooleanControl: ctrl is NULL\n");210}211if ((*creator->env)->ExceptionOccurred(creator->env)) {212ERROR0("PORT_NewBooleanControl: ExceptionOccurred!\n");213}214TRACE0("PORT_NewBooleanControl succeeded\n");215return (void*) ctrl;216}217218void* PORT_NewCompoundControl(void* creatorV, char* type, void** controls, int controlCount) {219ControlCreatorJNI* creator = (ControlCreatorJNI*) creatorV;220jobject ctrl = NULL;221jobjectArray controlArray;222int i;223jstring typeString;224225TRACE2("PORT_NewCompoundControl: creating '%s' with %d controls\n", type, controlCount);226if (!creator->compCtrlClass) {227TRACE0("PORT_NewCompoundControl: retrieve method ids\n");228// retrieve class and constructor of PortMixer.BoolCtrl229creator->compCtrlClass = (*creator->env)->FindClass(creator->env, IMPLEMENTATION_PACKAGE_NAME"/PortMixer$CompCtrl");230if (!creator->compCtrlClass) {231ERROR0("PORT_NewCompoundControl: compCtrlClass is NULL\n");232return NULL;233}234creator->compCtrlConstructor = (*creator->env)->GetMethodID(creator->env, creator->compCtrlClass,235"<init>", "(Ljava/lang/String;[Ljavax/sound/sampled/Control;)V");236if (!creator->compCtrlConstructor) {237ERROR0("PORT_NewCompoundControl: compCtrlConstructor is NULL\n");238return NULL;239}240creator->controlClass = (*creator->env)->FindClass(creator->env, JAVA_SAMPLED_PACKAGE_NAME"/Control");241if (!creator->controlClass) {242ERROR0("PORT_NewCompoundControl: controlClass is NULL\n");243return NULL;244}245}246TRACE0("PORT_NewCompoundControl: creating array\n");247// create new array for the controls248controlArray = (*creator->env)->NewObjectArray(creator->env, controlCount, creator->controlClass, (jobject) NULL);249if (!controlArray) {250ERROR0("PORT_NewCompoundControl: controlArray is NULL\n");251return NULL;252}253TRACE0("PORT_NewCompoundControl: setting array values\n");254for (i = 0; i < controlCount; i++) {255(*creator->env)->SetObjectArrayElement(creator->env, controlArray, i, (jobject) controls[i]);256}257TRACE0("PORT_NewCompoundControl: creating compound control\n");258typeString = (*creator->env)->NewStringUTF(creator->env, type);259CHECK_NULL_RETURN(typeString, (void*) ctrl);260ctrl = (*creator->env)->NewObject(creator->env, creator->compCtrlClass,261creator->compCtrlConstructor,262typeString, controlArray);263if (!ctrl) {264ERROR0("PORT_NewCompoundControl: ctrl is NULL\n");265}266if ((*creator->env)->ExceptionOccurred(creator->env)) {267ERROR0("PORT_NewCompoundControl: ExceptionOccurred!\n");268}269TRACE0("PORT_NewCompoundControl succeeded\n");270return (void*) ctrl;271}272273void* PORT_NewFloatControl(void* creatorV, void* controlID, char* type,274float min, float max, float precision, const char* units) {275ControlCreatorJNI* creator = (ControlCreatorJNI*) creatorV;276jobject ctrl = NULL;277jstring unitsString;278jstring typeString;279280#ifdef USE_TRACE281if (((UINT_PTR) type) <= CONTROL_TYPE_MAX) {282TRACE1("PORT_NewFloatControl: creating '%d'\n", (int) (UINT_PTR) type);283} else {284TRACE1("PORT_NewFloatControl: creating '%s'\n", type);285}286#endif287if (!creator->floatCtrlClass) {288// retrieve class and constructor of PortMixer.BoolCtrl289creator->floatCtrlClass = (*creator->env)->FindClass(creator->env, IMPLEMENTATION_PACKAGE_NAME"/PortMixer$FloatCtrl");290if (!creator->floatCtrlClass) {291ERROR0("PORT_NewFloatControl: floatCtrlClass is NULL\n");292return NULL;293}294creator->floatCtrlConstructor1 = (*creator->env)->GetMethodID(creator->env, creator->floatCtrlClass,295"<init>", "(JLjava/lang/String;FFFLjava/lang/String;)V");296if (!creator->floatCtrlConstructor1) {297ERROR0("PORT_NewFloatControl: floatCtrlConstructor1 is NULL\n");298return NULL;299}300creator->floatCtrlConstructor2 = (*creator->env)->GetMethodID(creator->env, creator->floatCtrlClass,301"<init>", "(JIFFFLjava/lang/String;)V");302if (!creator->floatCtrlConstructor2) {303ERROR0("PORT_NewFloatControl: floatCtrlConstructor2 is NULL\n");304return NULL;305}306}307unitsString = (*creator->env)->NewStringUTF(creator->env, units);308CHECK_NULL_RETURN(unitsString, (void*) ctrl);309if (((UINT_PTR) type) <= CONTROL_TYPE_MAX) {310// constructor with int parameter311TRACE1("PORT_NewFloatControl: calling constructor2 with type %d\n", (int) (UINT_PTR) type);312ctrl = (*creator->env)->NewObject(creator->env, creator->floatCtrlClass,313creator->floatCtrlConstructor2,314(jlong) (UINT_PTR) controlID, (jint) (UINT_PTR) type,315min, max, precision, unitsString);316} else {317TRACE0("PORT_NewFloatControl: calling constructor1\n");318// constructor with string parameter319typeString = (*creator->env)->NewStringUTF(creator->env, type);320CHECK_NULL_RETURN(typeString, (void*) ctrl);321ctrl = (*creator->env)->NewObject(creator->env, creator->floatCtrlClass,322creator->floatCtrlConstructor1,323(jlong) (UINT_PTR) controlID, typeString,324min, max, precision, unitsString);325}326if (!ctrl) {327ERROR0("PORT_NewFloatControl: ctrl is NULL!\n");328}329if ((*creator->env)->ExceptionOccurred(creator->env)) {330ERROR0("PORT_NewFloatControl: ExceptionOccurred!\n");331}332TRACE1("PORT_NewFloatControl succeeded %p\n", (void*) ctrl);333return (void*) ctrl;334}335336int PORT_AddControl(void* creatorV, void* control) {337ControlCreatorJNI* creator = (ControlCreatorJNI*) creatorV;338339TRACE1("PORT_AddControl %p\n", (void*) control);340(*creator->env)->CallVoidMethod(creator->env, creator->vector, creator->vectorAddElement, (jobject) control);341if ((*creator->env)->ExceptionOccurred(creator->env)) {342ERROR0("PORT_AddControl: ExceptionOccurred!\n");343}344TRACE0("PORT_AddControl succeeded\n");345return TRUE;346}347348JNIEXPORT void JNICALL Java_com_sun_media_sound_PortMixer_nGetControls349(JNIEnv *env, jclass cls, jlong id, jint portIndex, jobject vector) {350351ControlCreatorJNI creator;352jclass vectorClass;353354#if USE_PORTS == TRUE355if (id != 0) {356memset(&creator, 0, sizeof(ControlCreatorJNI));357creator.creator.newBooleanControl = &PORT_NewBooleanControl;358creator.creator.newCompoundControl = &PORT_NewCompoundControl;359creator.creator.newFloatControl = &PORT_NewFloatControl;360creator.creator.addControl = &PORT_AddControl;361creator.env = env;362vectorClass = (*env)->GetObjectClass(env, vector);363if (vectorClass == NULL) {364ERROR0("Java_com_sun_media_sound_PortMixer_nGetControls: vectorClass is NULL\n");365return;366}367creator.vector = vector;368creator.vectorAddElement = (*env)->GetMethodID(env, vectorClass, "addElement", "(Ljava/lang/Object;)V");369if (creator.vectorAddElement == NULL) {370ERROR0("Java_com_sun_media_sound_PortMixer_nGetControls: addElementMethodID is NULL\n");371return;372}373PORT_GetControls((void*) (UINT_PTR) id, (INT32) portIndex, (PortControlCreator*) &creator);374}375#endif376}377378379