1 /*
2  * Copyright (c) 2002, 2014, Oracle and/or its affiliates. All rights reserved.
3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4  *
5  * This code is free software; you can redistribute it and/or modify it
6  * under the terms of the GNU General Public License version 2 only, as
7  * published by the Free Software Foundation.  Oracle designates this
8  * particular file as subject to the "Classpath" exception as provided
9  * by Oracle in the LICENSE file that accompanied this code.
10  *
11  * This code is distributed in the hope that it will be useful, but WITHOUT
12  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
13  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
14  * version 2 for more details (a copy is included in the LICENSE file that
15  * accompanied this code).
16  *
17  * You should have received a copy of the GNU General Public License version
18  * 2 along with this work; if not, write to the Free Software Foundation,
19  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
20  *
21  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
22  * or visit www.oracle.com if you need additional information or have any
23  * questions.
24  */
25 
26 //#define USE_TRACE
27 //#define USE_ERROR
28 
29 
30 #include <jni.h>
31 #include <jni_util.h>
32 #include "SoundDefs.h"
33 #include "DirectAudio.h"
34 #include "Utilities.h"
35 #include "com_sun_media_sound_DirectAudioDeviceProvider.h"
36 
37 
38 //////////////////////////////////////////// DirectAudioDeviceProvider ////////////////////////////////////////////
39 
getDirectAudioDeviceDescription(int mixerIndex,DirectAudioDeviceDescription * desc)40 int getDirectAudioDeviceDescription(int mixerIndex, DirectAudioDeviceDescription* desc) {
41     desc->deviceID = 0;
42     desc->maxSimulLines = 0;
43     strcpy(desc->name, "Unknown Name");
44     strcpy(desc->vendor, "Unknown Vendor");
45     strcpy(desc->description, "Unknown Description");
46     strcpy(desc->version, "Unknown Version");
47 #if USE_DAUDIO == TRUE
48     DAUDIO_GetDirectAudioDeviceDescription(mixerIndex, desc);
49 #endif // USE_DAUDIO
50     return TRUE;
51 }
52 
Java_com_sun_media_sound_DirectAudioDeviceProvider_nGetNumDevices(JNIEnv * env,jclass cls)53 JNIEXPORT jint JNICALL Java_com_sun_media_sound_DirectAudioDeviceProvider_nGetNumDevices(JNIEnv *env, jclass cls) {
54     INT32 numDevices = 0;
55 
56     TRACE0("Java_com_sun_media_sound_DirectAudioDeviceProvider_nGetNumDevices.\n");
57 
58 #if USE_DAUDIO == TRUE
59     numDevices = DAUDIO_GetDirectAudioDeviceCount();
60 #endif // USE_DAUDIO
61 
62     TRACE1("Java_com_sun_media_sound_DirectAudioDeviceProvider_nGetNumDevices returning %d.\n", (int) numDevices);
63 
64     return (jint)numDevices;
65 }
66 
Java_com_sun_media_sound_DirectAudioDeviceProvider_nNewDirectAudioDeviceInfo(JNIEnv * env,jclass cls,jint mixerIndex)67 JNIEXPORT jobject JNICALL Java_com_sun_media_sound_DirectAudioDeviceProvider_nNewDirectAudioDeviceInfo
68     (JNIEnv *env, jclass cls, jint mixerIndex) {
69 
70     jclass directAudioDeviceInfoClass;
71     jmethodID directAudioDeviceInfoConstructor;
72     DirectAudioDeviceDescription desc;
73     jobject info = NULL;
74     jstring name;
75     jstring vendor;
76     jstring description;
77     jstring version;
78 
79     TRACE1("Java_com_sun_media_sound_DirectAudioDeviceProvider_nNewDirectAudioDeviceInfo(%d).\n", mixerIndex);
80 
81     // retrieve class and constructor of DirectAudioDeviceProvider.DirectAudioDeviceInfo
82     directAudioDeviceInfoClass = (*env)->FindClass(env, IMPLEMENTATION_PACKAGE_NAME"/DirectAudioDeviceProvider$DirectAudioDeviceInfo");
83     if (directAudioDeviceInfoClass == NULL) {
84         ERROR0("Java_com_sun_media_sound_DirectAudioDeviceProvider_nNewDirectAudioDeviceInfo: directAudioDeviceInfoClass is NULL\n");
85         return NULL;
86     }
87     directAudioDeviceInfoConstructor = (*env)->GetMethodID(env, directAudioDeviceInfoClass, "<init>",
88                   "(IIILjava/lang/String;Ljava/lang/String;Ljava/lang/String;Ljava/lang/String;)V");
89     if (directAudioDeviceInfoConstructor == NULL) {
90         ERROR0("Java_com_sun_media_sound_DirectAudioDeviceProvider_nNewDirectAudioDeviceInfo: directAudioDeviceInfoConstructor is NULL\n");
91         return NULL;
92     }
93 
94     TRACE1("Get description for device %d\n", mixerIndex);
95 
96     if (getDirectAudioDeviceDescription(mixerIndex, &desc)) {
97         // create a new DirectAudioDeviceInfo object and return it
98         name = (*env)->NewStringUTF(env, desc.name);
99         CHECK_NULL_RETURN(name, info);
100         vendor = (*env)->NewStringUTF(env, desc.vendor);
101         CHECK_NULL_RETURN(vendor, info);
102         description = (*env)->NewStringUTF(env, desc.description);
103         CHECK_NULL_RETURN(description, info);
104         version = (*env)->NewStringUTF(env, desc.version);
105         CHECK_NULL_RETURN(version, info);
106         info = (*env)->NewObject(env, directAudioDeviceInfoClass,
107                                  directAudioDeviceInfoConstructor, mixerIndex,
108                                  desc.deviceID, desc.maxSimulLines,
109                                  name, vendor, description, version);
110     } else {
111         ERROR1("ERROR: getDirectAudioDeviceDescription(%d, desc) returned FALSE!\n", mixerIndex);
112     }
113 
114     TRACE0("Java_com_sun_media_sound_DirectAudioDeviceProvider_nNewDirectAudioDeviceInfo succeeded.\n");
115     return info;
116 }
117