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