Dongwon Kang | bf98d54 | 2018-09-11 14:40:23 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2017, 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 | |
Ray Essick | c535a55 | 2019-01-18 11:38:15 -0800 | [diff] [blame] | 17 | #define LOG_TAG "MediaMetricsJNI" |
| 18 | |
Andy Hung | 7334848 | 2019-10-23 16:54:53 -0700 | [diff] [blame] | 19 | #include <binder/Parcel.h> |
Dongwon Kang | bf98d54 | 2018-09-11 14:40:23 -0700 | [diff] [blame] | 20 | #include <jni.h> |
Ray Essick | 81fbc5b | 2019-12-07 06:24:59 -0800 | [diff] [blame] | 21 | #include <media/MediaMetricsItem.h> |
Dongwon Kang | bf98d54 | 2018-09-11 14:40:23 -0700 | [diff] [blame] | 22 | #include <nativehelper/JNIHelp.h> |
| 23 | |
| 24 | #include "android_media_MediaMetricsJNI.h" |
Robert Shih | 4354a96 | 2019-11-10 12:09:08 -0800 | [diff] [blame] | 25 | #include "android_os_Parcel.h" |
Andy Hung | f2310c7 | 2019-10-23 16:54:53 -0700 | [diff] [blame^] | 26 | #include "android_runtime/AndroidRuntime.h" |
Dongwon Kang | bf98d54 | 2018-09-11 14:40:23 -0700 | [diff] [blame] | 27 | |
Marco Nelissen | a9a802f | 2019-09-24 09:27:22 -0700 | [diff] [blame] | 28 | // This source file is compiled and linked into: |
Ray Essick | c535a55 | 2019-01-18 11:38:15 -0800 | [diff] [blame] | 29 | // core/jni/ (libandroid_runtime.so) |
Ray Essick | c535a55 | 2019-01-18 11:38:15 -0800 | [diff] [blame] | 30 | |
Dongwon Kang | bf98d54 | 2018-09-11 14:40:23 -0700 | [diff] [blame] | 31 | namespace android { |
| 32 | |
Andy Hung | 7334848 | 2019-10-23 16:54:53 -0700 | [diff] [blame] | 33 | namespace { |
| 34 | struct BundleHelper { |
| 35 | BundleHelper(JNIEnv* _env, jobject _bundle) |
| 36 | : env(_env) |
| 37 | , clazzBundle(env->FindClass("android/os/PersistableBundle")) |
| 38 | , putIntID(env->GetMethodID(clazzBundle, "putInt", "(Ljava/lang/String;I)V")) |
| 39 | , putLongID(env->GetMethodID(clazzBundle, "putLong", "(Ljava/lang/String;J)V")) |
| 40 | , putDoubleID(env->GetMethodID(clazzBundle, "putDouble", "(Ljava/lang/String;D)V")) |
| 41 | , putStringID(env->GetMethodID(clazzBundle, |
| 42 | "putString", "(Ljava/lang/String;Ljava/lang/String;)V")) |
| 43 | , constructID(env->GetMethodID(clazzBundle, "<init>", "()V")) |
| 44 | , bundle(_bundle == nullptr ? env->NewObject(clazzBundle, constructID) : _bundle) |
| 45 | { } |
| 46 | |
| 47 | JNIEnv* const env; |
| 48 | const jclass clazzBundle; |
| 49 | const jmethodID putIntID; |
| 50 | const jmethodID putLongID; |
| 51 | const jmethodID putDoubleID; |
| 52 | const jmethodID putStringID; |
| 53 | const jmethodID constructID; |
| 54 | jobject const bundle; |
| 55 | |
Ray Essick | 81fbc5b | 2019-12-07 06:24:59 -0800 | [diff] [blame] | 56 | // We use templated put to access mediametrics::Item based on data type not type enum. |
Andy Hung | 7334848 | 2019-10-23 16:54:53 -0700 | [diff] [blame] | 57 | // See std::variant and std::visit. |
| 58 | template<typename T> |
| 59 | void put(jstring keyName, const T& value) = delete; |
| 60 | |
| 61 | template<> |
| 62 | void put(jstring keyName, const int32_t& value) { |
| 63 | env->CallVoidMethod(bundle, putIntID, keyName, (jint)value); |
| 64 | } |
| 65 | |
| 66 | template<> |
| 67 | void put(jstring keyName, const int64_t& value) { |
| 68 | env->CallVoidMethod(bundle, putLongID, keyName, (jlong)value); |
| 69 | } |
| 70 | |
| 71 | template<> |
| 72 | void put(jstring keyName, const double& value) { |
| 73 | env->CallVoidMethod(bundle, putDoubleID, keyName, (jdouble)value); |
| 74 | } |
| 75 | |
| 76 | template<> |
| 77 | void put(jstring keyName, const char * const& value) { |
| 78 | env->CallVoidMethod(bundle, putStringID, keyName, env->NewStringUTF(value)); |
| 79 | } |
| 80 | |
| 81 | template<> |
| 82 | void put(jstring keyName, char * const& value) { |
| 83 | env->CallVoidMethod(bundle, putStringID, keyName, env->NewStringUTF(value)); |
| 84 | } |
| 85 | |
| 86 | template<> |
| 87 | void put(jstring keyName, const std::pair<int64_t, int64_t>& value) { |
| 88 | ; // rate is currently ignored |
| 89 | } |
| 90 | |
| 91 | // We allow both jstring and non-jstring variants. |
| 92 | template<typename T> |
| 93 | void put(const char *keyName, const T& value) { |
| 94 | put(env->NewStringUTF(keyName), value); |
| 95 | } |
| 96 | }; |
| 97 | } // namespace |
| 98 | |
Dongwon Kang | bf98d54 | 2018-09-11 14:40:23 -0700 | [diff] [blame] | 99 | // place the attributes into a java PersistableBundle object |
Andy Hung | 7334848 | 2019-10-23 16:54:53 -0700 | [diff] [blame] | 100 | jobject MediaMetricsJNI::writeMetricsToBundle( |
Ray Essick | 81fbc5b | 2019-12-07 06:24:59 -0800 | [diff] [blame] | 101 | JNIEnv* env, mediametrics::Item *item, jobject bundle) |
Andy Hung | 7334848 | 2019-10-23 16:54:53 -0700 | [diff] [blame] | 102 | { |
| 103 | BundleHelper bh(env, bundle); |
Dongwon Kang | bf98d54 | 2018-09-11 14:40:23 -0700 | [diff] [blame] | 104 | |
Andy Hung | 7334848 | 2019-10-23 16:54:53 -0700 | [diff] [blame] | 105 | if (bh.bundle == nullptr) { |
| 106 | ALOGE("%s: unable to create Bundle", __func__); |
| 107 | return nullptr; |
Dongwon Kang | bf98d54 | 2018-09-11 14:40:23 -0700 | [diff] [blame] | 108 | } |
| 109 | |
Andy Hung | 7334848 | 2019-10-23 16:54:53 -0700 | [diff] [blame] | 110 | bh.put("__key", item->getKey().c_str()); |
| 111 | if (item->getPid() != -1) { |
| 112 | bh.put("__pid", (int32_t)item->getPid()); |
Dongwon Kang | bf98d54 | 2018-09-11 14:40:23 -0700 | [diff] [blame] | 113 | } |
Andy Hung | 7334848 | 2019-10-23 16:54:53 -0700 | [diff] [blame] | 114 | if (item->getTimestamp() > 0) { |
| 115 | bh.put("__timestamp", (int64_t)item->getTimestamp()); |
Ray Essick | c535a55 | 2019-01-18 11:38:15 -0800 | [diff] [blame] | 116 | } |
Andy Hung | 7334848 | 2019-10-23 16:54:53 -0700 | [diff] [blame] | 117 | if (item->getUid() != -1) { |
| 118 | bh.put("__uid", (int32_t)item->getUid()); |
Ray Essick | c535a55 | 2019-01-18 11:38:15 -0800 | [diff] [blame] | 119 | } |
Andy Hung | 7334848 | 2019-10-23 16:54:53 -0700 | [diff] [blame] | 120 | for (const auto &prop : *item) { |
| 121 | const char *name = prop.getName(); |
| 122 | if (name == nullptr) continue; |
| 123 | prop.visit([&] (auto &value) { bh.put(name, value); }); |
Ray Essick | c535a55 | 2019-01-18 11:38:15 -0800 | [diff] [blame] | 124 | } |
Andy Hung | 7334848 | 2019-10-23 16:54:53 -0700 | [diff] [blame] | 125 | return bh.bundle; |
Ray Essick | c535a55 | 2019-01-18 11:38:15 -0800 | [diff] [blame] | 126 | } |
| 127 | |
Andy Hung | f2310c7 | 2019-10-23 16:54:53 -0700 | [diff] [blame^] | 128 | // Implementation of MediaMetrics.native_submit_bytebuffer(), |
| 129 | // Delivers the byte buffer to the mediametrics service. |
| 130 | static jint android_media_MediaMetrics_submit_bytebuffer( |
| 131 | JNIEnv* env, jobject thiz, jobject byteBuffer, jint length) |
| 132 | { |
| 133 | const jbyte* buffer = |
| 134 | reinterpret_cast<const jbyte*>(env->GetDirectBufferAddress(byteBuffer)); |
| 135 | if (buffer == nullptr) { |
| 136 | ALOGE("Error retrieving source of audio data to play, can't play"); |
| 137 | return (jint)BAD_VALUE; |
| 138 | } |
| 139 | |
| 140 | // TODO: directly record item to MediaMetrics service. |
| 141 | mediametrics::Item item; |
| 142 | if (item.readFromByteString((char *)buffer, length) != NO_ERROR) { |
| 143 | ALOGW("%s: cannot read from byte string", __func__); |
| 144 | return (jint)BAD_VALUE; |
| 145 | } |
| 146 | item.selfrecord(); |
| 147 | return (jint)NO_ERROR; |
| 148 | } |
| 149 | |
Robert Shih | 4354a96 | 2019-11-10 12:09:08 -0800 | [diff] [blame] | 150 | // Helper function to convert a native PersistableBundle to a Java |
| 151 | // PersistableBundle. |
| 152 | jobject MediaMetricsJNI::nativeToJavaPersistableBundle(JNIEnv *env, |
| 153 | os::PersistableBundle* nativeBundle) { |
| 154 | if (env == NULL || nativeBundle == NULL) { |
| 155 | ALOGE("Unexpected NULL parmeter"); |
| 156 | return NULL; |
| 157 | } |
| 158 | |
| 159 | // Create a Java parcel with the native parcel data. |
| 160 | // Then create a new PersistableBundle with that parcel as a parameter. |
| 161 | jobject jParcel = android::createJavaParcelObject(env); |
| 162 | if (jParcel == NULL) { |
| 163 | ALOGE("Failed to create a Java Parcel."); |
| 164 | return NULL; |
| 165 | } |
| 166 | |
| 167 | android::Parcel* nativeParcel = android::parcelForJavaObject(env, jParcel); |
| 168 | if (nativeParcel == NULL) { |
| 169 | ALOGE("Failed to get the native Parcel."); |
| 170 | return NULL; |
| 171 | } |
| 172 | |
| 173 | android::status_t result = nativeBundle->writeToParcel(nativeParcel); |
| 174 | nativeParcel->setDataPosition(0); |
| 175 | if (result != android::OK) { |
| 176 | ALOGE("Failed to write nativeBundle to Parcel: %d.", result); |
| 177 | return NULL; |
| 178 | } |
| 179 | |
| 180 | #define STATIC_INIT_JNI(T, obj, method, globalref, ...) \ |
| 181 | static T obj{};\ |
| 182 | if (obj == NULL) { \ |
| 183 | obj = method(__VA_ARGS__); \ |
| 184 | if (obj == NULL) { \ |
| 185 | ALOGE("%s can't find " #obj, __func__); \ |
| 186 | return NULL; \ |
| 187 | } else { \ |
| 188 | obj = globalref; \ |
| 189 | }\ |
| 190 | } \ |
| 191 | |
| 192 | STATIC_INIT_JNI(jclass, clazzBundle, env->FindClass, |
| 193 | static_cast<jclass>(env->NewGlobalRef(clazzBundle)), |
| 194 | "android/os/PersistableBundle"); |
| 195 | STATIC_INIT_JNI(jfieldID, bundleCreatorId, env->GetStaticFieldID, |
| 196 | bundleCreatorId, |
| 197 | clazzBundle, "CREATOR", "Landroid/os/Parcelable$Creator;"); |
| 198 | STATIC_INIT_JNI(jobject, bundleCreator, env->GetStaticObjectField, |
| 199 | env->NewGlobalRef(bundleCreator), |
| 200 | clazzBundle, bundleCreatorId); |
| 201 | STATIC_INIT_JNI(jclass, clazzCreator, env->FindClass, |
| 202 | static_cast<jclass>(env->NewGlobalRef(clazzCreator)), |
| 203 | "android/os/Parcelable$Creator"); |
| 204 | STATIC_INIT_JNI(jmethodID, createFromParcelId, env->GetMethodID, |
| 205 | createFromParcelId, |
| 206 | clazzCreator, "createFromParcel", "(Landroid/os/Parcel;)Ljava/lang/Object;"); |
| 207 | |
| 208 | jobject newBundle = env->CallObjectMethod(bundleCreator, createFromParcelId, jParcel); |
| 209 | if (newBundle == NULL) { |
| 210 | ALOGE("Failed to create a new PersistableBundle " |
| 211 | "from the createFromParcel call."); |
| 212 | } |
| 213 | |
| 214 | return newBundle; |
| 215 | } |
| 216 | |
Andy Hung | f2310c7 | 2019-10-23 16:54:53 -0700 | [diff] [blame^] | 217 | // ---------------------------------------------------------------------------- |
Dongwon Kang | bf98d54 | 2018-09-11 14:40:23 -0700 | [diff] [blame] | 218 | |
Andy Hung | f2310c7 | 2019-10-23 16:54:53 -0700 | [diff] [blame^] | 219 | static constexpr JNINativeMethod gMethods[] = { |
| 220 | {"native_submit_bytebuffer", "(Ljava/nio/ByteBuffer;I)I", |
| 221 | (void *)android_media_MediaMetrics_submit_bytebuffer}, |
| 222 | }; |
| 223 | |
| 224 | // Registers the native methods, called from core/jni/AndroidRuntime.cpp |
| 225 | int register_android_media_MediaMetrics(JNIEnv *env) |
| 226 | { |
| 227 | return AndroidRuntime::registerNativeMethods( |
| 228 | env, "android/media/MediaMetrics", gMethods, std::size(gMethods)); |
| 229 | } |
| 230 | |
| 231 | }; // namespace android |