aboutsummaryrefslogtreecommitdiffstats
path: root/cm/jni/src/org_cyanogenmod_platform_internal_CMAudioService.cpp
blob: 9c1cc35ecceee9a183c309c1ec98bc42212dd293 (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
/*
**
** Copyright 2016, The CyanogenMod 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_NDEBUG 0

#define LOG_TAG "CMAudioService-JNI"

#include <utils/Log.h>

#include <JNIHelp.h>
#include <jni.h>
#include "core_jni_helpers.h"
#include "android_media_AudioErrors.h"

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

#include <system/audio.h>
#include <utils/threads.h>

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

namespace android {

static const char* const kClassPathName = "org/cyanogenmod/platform/internal/CMAudioService";

static jclass gArrayListClass;
static struct {
    jmethodID    add;
    jmethodID    toArray;
} gArrayListMethods;

static struct {
    jmethodID postAudioSessionEventFromNative;
} gAudioSessionEventHandlerMethods;

static jclass gAudioSessionInfoClass;
static jmethodID gAudioSessionInfoCstor;

static jobject gThiz;

static Mutex gCallbackLock;

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

static void
org_cyanogenmod_platform_internal_CMAudioService_session_info_callback(int event,
        sp<AudioSessionInfo>& info, bool added)
{
    AutoMutex _l(gCallbackLock);

    JNIEnv *env = AndroidRuntime::getJNIEnv();
    if (env == NULL) {
        return;
    }

    jobject jSession = env->NewObject(gAudioSessionInfoClass, gAudioSessionInfoCstor,
            info->mSessionId, info->mStream, info->mFlags, info->mChannelMask, info->mUid);

    env->CallVoidMethod(gThiz,
            gAudioSessionEventHandlerMethods.postAudioSessionEventFromNative,
            event, jSession, added);

    env->DeleteLocalRef(jSession);
}

static void
org_cyanogenmod_platform_internal_CMAudioService_registerAudioSessionCallback(
        JNIEnv *env, jobject thiz, jboolean enabled)
{
    if (gThiz == NULL) {
        gThiz = env->NewGlobalRef(thiz);
    }

    AudioSystem::setAudioSessionCallback( enabled ?
            org_cyanogenmod_platform_internal_CMAudioService_session_info_callback : NULL);
}

static jint
org_cyanogenmod_platform_internal_CMAudioService_listAudioSessions(JNIEnv *env, jobject thiz,
        jint streams, jobject jSessions)
{
    ALOGV("listAudioSessions");

    if (jSessions == NULL) {
        ALOGE("listAudioSessions NULL arraylist");
        return (jint)AUDIO_JAVA_BAD_VALUE;
    }
    if (!env->IsInstanceOf(jSessions, gArrayListClass)) {
        ALOGE("listAudioSessions not an arraylist");
        return (jint)AUDIO_JAVA_BAD_VALUE;
    }

    status_t status;
    Vector< sp<AudioSessionInfo>> sessions;

    status = AudioSystem::listAudioSessions((audio_stream_type_t)streams, sessions);
    if (status != NO_ERROR) {
        ALOGE("AudioSystem::listAudioSessions error %d", status);
    } else {
        ALOGV("AudioSystem::listAudioSessions count=%zu", sessions.size());
    }

    jint jStatus = nativeToJavaStatus(status);
    if (jStatus != AUDIO_JAVA_SUCCESS) {
        goto exit;
    }

    for (size_t i = 0; i < sessions.size(); i++) {
        const sp<AudioSessionInfo>& s = sessions.itemAt(i);

        jobject jSession = env->NewObject(gAudioSessionInfoClass, gAudioSessionInfoCstor,
                s->mSessionId, s->mStream, s->mFlags, s->mChannelMask, s->mUid);

        if (jSession == NULL) {
            jStatus = (jint)AUDIO_JAVA_ERROR;
            goto exit;
        }

        env->CallBooleanMethod(jSessions, gArrayListMethods.add, jSession);
        env->DeleteLocalRef(jSession);
    }

exit:
    return jStatus;
}


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

static JNINativeMethod gMethods[] = {
     {"native_listAudioSessions", "(ILjava/util/ArrayList;)I",
            (void *)org_cyanogenmod_platform_internal_CMAudioService_listAudioSessions},
     {"native_registerAudioSessionCallback", "(Z)V",
            (void *)org_cyanogenmod_platform_internal_CMAudioService_registerAudioSessionCallback},
};

int register_org_cyanogenmod_platform_internal_CMAudioService(JNIEnv *env)
{
    jclass arrayListClass = FindClassOrDie(env, "java/util/ArrayList");
    gArrayListClass = MakeGlobalRefOrDie(env, arrayListClass);
    gArrayListMethods.add = GetMethodIDOrDie(env, arrayListClass, "add", "(Ljava/lang/Object;)Z");
    gArrayListMethods.toArray = GetMethodIDOrDie(env, arrayListClass, "toArray", "()[Ljava/lang/Object;");

    jclass audioSessionInfoClass = FindClassOrDie(env, "cyanogenmod/media/AudioSessionInfo");
    gAudioSessionInfoClass = MakeGlobalRefOrDie(env, audioSessionInfoClass);
    gAudioSessionInfoCstor = GetMethodIDOrDie(env, audioSessionInfoClass, "<init>", "(IIIII)V");

    gAudioSessionEventHandlerMethods.postAudioSessionEventFromNative =
            GetMethodIDOrDie(env, env->FindClass(kClassPathName),
            "audioSessionCallbackFromNative", "(ILcyanogenmod/media/AudioSessionInfo;Z)V");

    return RegisterMethodsOrDie(env, kClassPathName, gMethods, NELEM(gMethods));
}

} /* namespace android */