François Gaffie | c01fa3a | 2018-03-15 14:33:24 +0100 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2018 The Android Open Source Project |
| 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | */ |
| 16 | |
| 17 | //#define LOG_NDEBUG 0 |
| 18 | |
| 19 | #define LOG_TAG "AudioProductStrategies-JNI" |
| 20 | |
| 21 | #include <inttypes.h> |
| 22 | #include <jni.h> |
| 23 | #include <nativehelper/JNIHelp.h> |
| 24 | #include "core_jni_helpers.h" |
| 25 | |
| 26 | #include <utils/Log.h> |
| 27 | #include <vector> |
| 28 | |
| 29 | #include <media/AudioSystem.h> |
| 30 | #include <media/AudioPolicy.h> |
| 31 | |
| 32 | #include <nativehelper/ScopedUtfChars.h> |
| 33 | |
| 34 | #include "android_media_AudioAttributes.h" |
| 35 | #include "android_media_AudioErrors.h" |
| 36 | |
| 37 | // ---------------------------------------------------------------------------- |
| 38 | |
| 39 | using namespace android; |
| 40 | |
| 41 | // ---------------------------------------------------------------------------- |
Hayden Gomes | 6d69bde | 2019-04-04 13:10:13 -0700 | [diff] [blame] | 42 | static const char* const kClassPathName = "android/media/audiopolicy/AudioProductStrategy"; |
François Gaffie | c01fa3a | 2018-03-15 14:33:24 +0100 | [diff] [blame] | 43 | static const char* const kAudioProductStrategyClassPathName = |
| 44 | "android/media/audiopolicy/AudioProductStrategy"; |
| 45 | |
| 46 | static const char* const kAudioAttributesGroupsClassPathName = |
| 47 | "android/media/audiopolicy/AudioProductStrategy$AudioAttributesGroup"; |
| 48 | |
| 49 | static jclass gAudioProductStrategyClass; |
| 50 | static jmethodID gAudioProductStrategyCstor; |
| 51 | static struct { |
| 52 | jfieldID mAudioAttributesGroups; |
| 53 | jfieldID mName; |
| 54 | jfieldID mId; |
| 55 | } gAudioProductStrategyFields; |
| 56 | |
| 57 | static jclass gAudioAttributesGroupClass; |
| 58 | static jmethodID gAudioAttributesGroupCstor; |
| 59 | static struct { |
François Gaffie | adcd00a | 2018-09-18 17:06:26 +0200 | [diff] [blame] | 60 | jfieldID mVolumeGroupId; |
François Gaffie | c01fa3a | 2018-03-15 14:33:24 +0100 | [diff] [blame] | 61 | jfieldID mLegacyStreamType; |
| 62 | jfieldID mAudioAttributes; |
| 63 | } gAudioAttributesGroupsFields; |
| 64 | |
| 65 | static jclass gArrayListClass; |
| 66 | static struct { |
| 67 | jmethodID add; |
| 68 | jmethodID toArray; |
| 69 | } gArrayListMethods; |
| 70 | |
| 71 | |
| 72 | static jint convertAudioProductStrategiesFromNative( |
| 73 | JNIEnv *env, jobject *jAudioStrategy, const AudioProductStrategy &strategy) |
| 74 | { |
| 75 | jint jStatus = (jint)AUDIO_JAVA_SUCCESS; |
| 76 | jobjectArray jAudioAttributesGroups = NULL; |
| 77 | jobjectArray jAudioAttributes = NULL; |
| 78 | jobject jAudioAttribute = NULL; |
| 79 | jstring jName = NULL; |
| 80 | jint jStrategyId = NULL; |
| 81 | jint numAttributesGroups; |
| 82 | size_t indexGroup = 0; |
| 83 | |
| 84 | jName = env->NewStringUTF(strategy.getName().c_str()); |
| 85 | jStrategyId = static_cast<jint>(strategy.getId()); |
| 86 | |
| 87 | // Audio Attributes Group array |
| 88 | std::map<int, std::vector<AudioAttributes> > groups; |
| 89 | for (const auto &attr : strategy.getAudioAttributes()) { |
| 90 | int attrGroupId = attr.getGroupId(); |
| 91 | groups[attrGroupId].push_back(attr); |
| 92 | } |
| 93 | numAttributesGroups = groups.size(); |
| 94 | |
| 95 | jAudioAttributesGroups = env->NewObjectArray(numAttributesGroups, gAudioAttributesGroupClass, NULL); |
| 96 | |
| 97 | for (const auto &iter : groups) { |
| 98 | std::vector<AudioAttributes> audioAttributesGroups = iter.second; |
| 99 | jint numAttributes = audioAttributesGroups.size(); |
| 100 | jint jGroupId = iter.first; |
| 101 | jint jLegacyStreamType = audioAttributesGroups.front().getStreamType(); |
| 102 | |
| 103 | jStatus = JNIAudioAttributeHelper::getJavaArray(env, &jAudioAttributes, numAttributes); |
| 104 | if (jStatus != (jint)AUDIO_JAVA_SUCCESS) { |
| 105 | goto exit; |
| 106 | } |
| 107 | for (size_t j = 0; j < static_cast<size_t>(numAttributes); j++) { |
| 108 | auto attributes = audioAttributesGroups[j].getAttributes(); |
| 109 | |
| 110 | jStatus = JNIAudioAttributeHelper::nativeToJava(env, &jAudioAttribute, attributes); |
| 111 | if (jStatus != AUDIO_JAVA_SUCCESS) { |
| 112 | goto exit; |
| 113 | } |
| 114 | env->SetObjectArrayElement(jAudioAttributes, j, jAudioAttribute); |
| 115 | } |
| 116 | jobject jAudioAttributesGroup = env->NewObject(gAudioAttributesGroupClass, |
| 117 | gAudioAttributesGroupCstor, |
| 118 | jGroupId, |
| 119 | jLegacyStreamType, |
| 120 | jAudioAttributes); |
| 121 | env->SetObjectArrayElement(jAudioAttributesGroups, indexGroup++, jAudioAttributesGroup); |
| 122 | |
| 123 | if (jAudioAttributes != NULL) { |
| 124 | env->DeleteLocalRef(jAudioAttributes); |
| 125 | jAudioAttributes = NULL; |
| 126 | } |
| 127 | if (jAudioAttribute != NULL) { |
| 128 | env->DeleteLocalRef(jAudioAttribute); |
| 129 | jAudioAttribute = NULL; |
| 130 | } |
| 131 | if (jAudioAttributesGroup != NULL) { |
| 132 | env->DeleteLocalRef(jAudioAttributesGroup); |
| 133 | jAudioAttributesGroup = NULL; |
| 134 | } |
| 135 | } |
| 136 | *jAudioStrategy = env->NewObject(gAudioProductStrategyClass, gAudioProductStrategyCstor, |
| 137 | jName, |
| 138 | jStrategyId, |
| 139 | jAudioAttributesGroups); |
| 140 | exit: |
| 141 | if (jAudioAttributes != NULL) { |
| 142 | env->DeleteLocalRef(jAudioAttributes); |
| 143 | } |
| 144 | if (jAudioAttribute != NULL) { |
| 145 | env->DeleteLocalRef(jAudioAttribute); |
| 146 | jAudioAttribute = NULL; |
| 147 | } |
| 148 | if (jAudioAttributesGroups != NULL) { |
| 149 | env->DeleteLocalRef(jAudioAttributesGroups); |
| 150 | } |
| 151 | if (jName != NULL) { |
| 152 | env->DeleteLocalRef(jName); |
| 153 | } |
| 154 | return jStatus; |
| 155 | } |
| 156 | |
| 157 | static jint |
| 158 | android_media_AudioSystem_listAudioProductStrategies(JNIEnv *env, jobject clazz, |
| 159 | jobject jStrategies) |
| 160 | { |
| 161 | if (env == NULL) { |
| 162 | return AUDIO_JAVA_DEAD_OBJECT; |
| 163 | } |
| 164 | if (jStrategies == NULL) { |
| 165 | ALOGE("listAudioProductStrategies NULL AudioProductStrategies"); |
| 166 | return (jint)AUDIO_JAVA_BAD_VALUE; |
| 167 | } |
| 168 | if (!env->IsInstanceOf(jStrategies, gArrayListClass)) { |
| 169 | ALOGE("listAudioProductStrategies not an arraylist"); |
| 170 | return (jint)AUDIO_JAVA_BAD_VALUE; |
| 171 | } |
| 172 | |
| 173 | status_t status; |
| 174 | AudioProductStrategyVector strategies; |
| 175 | jint jStatus; |
| 176 | jobject jStrategy = NULL; |
| 177 | |
| 178 | status = AudioSystem::listAudioProductStrategies(strategies); |
| 179 | if (status != NO_ERROR) { |
| 180 | ALOGE("AudioSystem::listAudioProductStrategies error %d", status); |
| 181 | return nativeToJavaStatus(status); |
| 182 | } |
| 183 | for (const auto &strategy : strategies) { |
| 184 | jStatus = convertAudioProductStrategiesFromNative(env, &jStrategy, strategy); |
| 185 | if (jStatus != AUDIO_JAVA_SUCCESS) { |
| 186 | goto exit; |
| 187 | } |
| 188 | env->CallBooleanMethod(jStrategies, gArrayListMethods.add, jStrategy); |
| 189 | } |
| 190 | exit: |
| 191 | if (jStrategy != NULL) { |
| 192 | env->DeleteLocalRef(jStrategy); |
| 193 | } |
| 194 | return jStatus; |
| 195 | } |
| 196 | |
| 197 | /* |
| 198 | * JNI registration. |
| 199 | */ |
| 200 | static const JNINativeMethod gMethods[] = { |
| 201 | {"native_list_audio_product_strategies", "(Ljava/util/ArrayList;)I", |
| 202 | (void *)android_media_AudioSystem_listAudioProductStrategies}, |
François Gaffie | c01fa3a | 2018-03-15 14:33:24 +0100 | [diff] [blame] | 203 | }; |
| 204 | |
| 205 | int register_android_media_AudioProductStrategies(JNIEnv *env) |
| 206 | { |
| 207 | jclass arrayListClass = FindClassOrDie(env, "java/util/ArrayList"); |
| 208 | gArrayListClass = MakeGlobalRefOrDie(env, arrayListClass); |
| 209 | gArrayListMethods.add = GetMethodIDOrDie(env, arrayListClass, "add", "(Ljava/lang/Object;)Z"); |
| 210 | gArrayListMethods.toArray = GetMethodIDOrDie(env, arrayListClass, |
| 211 | "toArray", "()[Ljava/lang/Object;"); |
| 212 | |
| 213 | jclass audioProductStrategyClass = FindClassOrDie(env, kAudioProductStrategyClassPathName); |
| 214 | gAudioProductStrategyClass = MakeGlobalRefOrDie(env, audioProductStrategyClass); |
| 215 | gAudioProductStrategyCstor = GetMethodIDOrDie( |
| 216 | env, audioProductStrategyClass, "<init>", |
| 217 | "(Ljava/lang/String;I[Landroid/media/audiopolicy/AudioProductStrategy$AudioAttributesGroup;)V"); |
| 218 | gAudioProductStrategyFields.mAudioAttributesGroups = GetFieldIDOrDie( |
| 219 | env, audioProductStrategyClass, "mAudioAttributesGroups", |
| 220 | "[Landroid/media/audiopolicy/AudioProductStrategy$AudioAttributesGroup;"); |
| 221 | gAudioProductStrategyFields.mName = GetFieldIDOrDie( |
| 222 | env, audioProductStrategyClass, "mName", "Ljava/lang/String;"); |
| 223 | gAudioProductStrategyFields.mId = GetFieldIDOrDie( |
| 224 | env, audioProductStrategyClass, "mId", "I"); |
| 225 | |
| 226 | jclass audioAttributesGroupClass = FindClassOrDie(env, kAudioAttributesGroupsClassPathName); |
| 227 | gAudioAttributesGroupClass = MakeGlobalRefOrDie(env, audioAttributesGroupClass); |
| 228 | gAudioAttributesGroupCstor = GetMethodIDOrDie(env, audioAttributesGroupClass, "<init>", |
| 229 | "(II[Landroid/media/AudioAttributes;)V"); |
François Gaffie | adcd00a | 2018-09-18 17:06:26 +0200 | [diff] [blame] | 230 | gAudioAttributesGroupsFields.mVolumeGroupId = GetFieldIDOrDie( |
| 231 | env, audioAttributesGroupClass, "mVolumeGroupId", "I"); |
François Gaffie | c01fa3a | 2018-03-15 14:33:24 +0100 | [diff] [blame] | 232 | gAudioAttributesGroupsFields.mLegacyStreamType = GetFieldIDOrDie( |
| 233 | env, audioAttributesGroupClass, "mLegacyStreamType", "I"); |
| 234 | gAudioAttributesGroupsFields.mAudioAttributes = GetFieldIDOrDie( |
| 235 | env, audioAttributesGroupClass, "mAudioAttributes", |
| 236 | "[Landroid/media/AudioAttributes;"); |
| 237 | |
| 238 | env->DeleteLocalRef(audioProductStrategyClass); |
| 239 | env->DeleteLocalRef(audioAttributesGroupClass); |
| 240 | |
| 241 | return RegisterMethodsOrDie(env, kClassPathName, gMethods, NELEM(gMethods)); |
| 242 | } |