summaryrefslogtreecommitdiffstats
path: root/core/jni/android_media_AudioSystem.cpp
blob: 692610ea42ccad3b6e2359f1776bc67a165dacf5 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
/* //device/libs/android_runtime/android_media_AudioSystem.cpp
**
** Copyright 2006, The Android Open Source Project
**
** Licensed under the Apache License, Version 2.0 (the "License");
** you may not use this file except in compliance with the License.
** You may obtain a copy of the License at
**
**     http://www.apache.org/licenses/LICENSE-2.0
**
** Unless required by applicable law or agreed to in writing, software
** distributed under the License is distributed on an "AS IS" BASIS,
** WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
** See the License for the specific language governing permissions and
** limitations under the License.
*/

#define LOG_TAG "AudioSystem"
#include "utils/Log.h"

#include <stdio.h>
#include <unistd.h>
#include <fcntl.h>
#include <math.h>

#include "jni.h"
#include "JNIHelp.h"
#include "android_runtime/AndroidRuntime.h"

#include <media/AudioSystem.h>
#include <media/AudioTrack.h>

// ----------------------------------------------------------------------------

using namespace android;

enum AudioError {
    kAudioStatusOk = 0,
    kAudioStatusError = 1,
    kAudioStatusMediaServerDied = 100
};

static int check_AudioSystem_Command(status_t status)
{
    if (status == NO_ERROR) {
        return kAudioStatusOk;
    } else {
        return kAudioStatusError;
    }
}

static int
android_media_AudioSystem_setVolume(JNIEnv *env, jobject clazz, jint type, jint volume)
{
    LOGV("setVolume(%d)", int(volume));
    
    return check_AudioSystem_Command(AudioSystem::setStreamVolume(type, AudioSystem::linearToLog(volume)));
}

static int
android_media_AudioSystem_getVolume(JNIEnv *env, jobject clazz, jint type)
{
    float v;
    int v_int = -1;
    if (AudioSystem::getStreamVolume(int(type), &v) == NO_ERROR) {
        v_int = AudioSystem::logToLinear(v);
    }
    return v_int;
}

static int
android_media_AudioSystem_muteMicrophone(JNIEnv *env, jobject thiz, jboolean on)
{
    return check_AudioSystem_Command(AudioSystem::muteMicrophone(on));
}

static jboolean
android_media_AudioSystem_isMicrophoneMuted(JNIEnv *env, jobject thiz)
{
    bool state = false;
    AudioSystem::isMicrophoneMuted(&state);
    return state;
}

static int
android_media_AudioSystem_setRouting(JNIEnv *env, jobject clazz, jint mode, jint routes, jint mask)
{
    return check_AudioSystem_Command(AudioSystem::setRouting(mode, uint32_t(routes), uint32_t(mask)));
}

static jint
android_media_AudioSystem_getRouting(JNIEnv *env, jobject clazz, jint mode)
{
    uint32_t routes = -1;
    AudioSystem::getRouting(mode, &routes);
    return jint(routes);
}

static int
android_media_AudioSystem_setMode(JNIEnv *env, jobject clazz, jint mode)
{
    return check_AudioSystem_Command(AudioSystem::setMode(mode));
}

static jint
android_media_AudioSystem_getMode(JNIEnv *env, jobject clazz)
{
    int mode = AudioSystem::MODE_INVALID;
    AudioSystem::getMode(&mode);
    return jint(mode);
}

static jboolean
android_media_AudioSystem_isMusicActive(JNIEnv *env, jobject thiz)
{
    bool state = false;
    AudioSystem::isMusicActive(&state);
    return state;
}

// Temporary interface, do not use
// TODO: Replace with a more generic key:value get/set mechanism
static void
android_media_AudioSystem_setParameter(JNIEnv *env, jobject thiz, jstring key, jstring value)
{
    const char *c_key = env->GetStringUTFChars(key, NULL);
    const char *c_value = env->GetStringUTFChars(value, NULL);
    AudioSystem::setParameter(c_key, c_value);
    env->ReleaseStringUTFChars(key, c_key);
    env->ReleaseStringUTFChars(value, c_value);
}

void android_media_AudioSystem_error_callback(status_t err)
{
    JNIEnv *env = AndroidRuntime::getJNIEnv();
    jclass clazz = env->FindClass("android/media/AudioSystem");

    int error;

    switch (err) {
    case DEAD_OBJECT:
        error = kAudioStatusMediaServerDied;
        break;
    case NO_ERROR:
        error = kAudioStatusOk;
        break;
    default:
        error = kAudioStatusError;
        break;
    }

    env->CallStaticVoidMethod(clazz, env->GetStaticMethodID(clazz, "errorCallbackFromNative","(I)V"), error);
}

// ----------------------------------------------------------------------------

static JNINativeMethod gMethods[] = {
    {"setVolume",           "(II)I",    (void *)android_media_AudioSystem_setVolume},
    {"getVolume",           "(I)I",     (void *)android_media_AudioSystem_getVolume},
    {"setParameter",        "(Ljava/lang/String;Ljava/lang/String;)V", (void *)android_media_AudioSystem_setParameter},
    {"muteMicrophone",      "(Z)I",     (void *)android_media_AudioSystem_muteMicrophone},
    {"isMicrophoneMuted",   "()Z",      (void *)android_media_AudioSystem_isMicrophoneMuted},
    {"setRouting",          "(III)I",   (void *)android_media_AudioSystem_setRouting},
    {"getRouting",          "(I)I",     (void *)android_media_AudioSystem_getRouting},
    {"setMode",             "(I)I",     (void *)android_media_AudioSystem_setMode},
    {"getMode",             "()I",      (void *)android_media_AudioSystem_getMode},
    {"isMusicActive",       "()Z",      (void *)android_media_AudioSystem_isMusicActive},
};

const char* const kClassPathName = "android/media/AudioSystem";

int register_android_media_AudioSystem(JNIEnv *env)
{
    AudioSystem::setErrorCallback(android_media_AudioSystem_error_callback);
    
    return AndroidRuntime::registerNativeMethods(env,
                "android/media/AudioSystem", gMethods, NELEM(gMethods));
}