1 /*
2 * Copyright 2020, 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 #include <jni.h>
18 #include <media/MediaMetrics.h>
19
20 #define JNI_FUNCTION(RETURN_TYPE, NAME, ...) \
21 extern "C" { \
22 JNIEXPORT RETURN_TYPE Java_android_media_MediaParser_##NAME(JNIEnv* env, jobject thiz, \
23 ##__VA_ARGS__); \
24 } \
25 JNIEXPORT RETURN_TYPE Java_android_media_MediaParser_##NAME(JNIEnv* env, jobject thiz, \
26 ##__VA_ARGS__)
27
28 namespace {
29
30 constexpr char kMediaMetricsKey[] = "mediaparser";
31
32 constexpr char kAttributeParserName[] = "android.media.mediaparser.parserName";
33 constexpr char kAttributeCreatedByName[] = "android.media.mediaparser.createdByName";
34 constexpr char kAttributeParserPool[] = "android.media.mediaparser.parserPool";
35 constexpr char kAttributeLastException[] = "android.media.mediaparser.lastException";
36 constexpr char kAttributeResourceByteCount[] = "android.media.mediaparser.resourceByteCount";
37 constexpr char kAttributeDurationMillis[] = "android.media.mediaparser.durationMillis";
38 constexpr char kAttributeTrackMimeTypes[] = "android.media.mediaparser.trackMimeTypes";
39 constexpr char kAttributeTrackCodecs[] = "android.media.mediaparser.trackCodecs";
40 constexpr char kAttributeAlteredParameters[] = "android.media.mediaparser.alteredParameters";
41 constexpr char kAttributeVideoWidth[] = "android.media.mediaparser.videoWidth";
42 constexpr char kAttributeVideoHeight[] = "android.media.mediaparser.videoHeight";
43
44 // Util class to handle string resource management.
45 class JstringHandle {
46 public:
JstringHandle(JNIEnv * env,jstring value)47 JstringHandle(JNIEnv* env, jstring value) : mEnv(env), mJstringValue(value) {
48 mCstringValue = env->GetStringUTFChars(value, /* isCopy= */ nullptr);
49 }
50
~JstringHandle()51 ~JstringHandle() {
52 if (mCstringValue != nullptr) {
53 mEnv->ReleaseStringUTFChars(mJstringValue, mCstringValue);
54 }
55 }
56
value() const57 [[nodiscard]] const char* value() const {
58 return mCstringValue != nullptr ? mCstringValue : "";
59 }
60
61 JNIEnv* mEnv;
62 jstring mJstringValue;
63 const char* mCstringValue;
64 };
65
66 } // namespace
67
JNI_FUNCTION(void,nativeSubmitMetrics,jstring parserNameJstring,jboolean createdByName,jstring parserPoolJstring,jstring lastExceptionJstring,jlong resourceByteCount,jlong durationMillis,jstring trackMimeTypesJstring,jstring trackCodecsJstring,jstring alteredParameters,jint videoWidth,jint videoHeight)68 JNI_FUNCTION(void, nativeSubmitMetrics, jstring parserNameJstring, jboolean createdByName,
69 jstring parserPoolJstring, jstring lastExceptionJstring, jlong resourceByteCount,
70 jlong durationMillis, jstring trackMimeTypesJstring, jstring trackCodecsJstring,
71 jstring alteredParameters, jint videoWidth, jint videoHeight) {
72 mediametrics_handle_t item(mediametrics_create(kMediaMetricsKey));
73 mediametrics_setCString(item, kAttributeParserName,
74 JstringHandle(env, parserNameJstring).value());
75 mediametrics_setInt32(item, kAttributeCreatedByName, createdByName ? 1 : 0);
76 mediametrics_setCString(item, kAttributeParserPool,
77 JstringHandle(env, parserPoolJstring).value());
78 mediametrics_setCString(item, kAttributeLastException,
79 JstringHandle(env, lastExceptionJstring).value());
80 mediametrics_setInt64(item, kAttributeResourceByteCount, resourceByteCount);
81 mediametrics_setInt64(item, kAttributeDurationMillis, durationMillis);
82 mediametrics_setCString(item, kAttributeTrackMimeTypes,
83 JstringHandle(env, trackMimeTypesJstring).value());
84 mediametrics_setCString(item, kAttributeTrackCodecs,
85 JstringHandle(env, trackCodecsJstring).value());
86 mediametrics_setCString(item, kAttributeAlteredParameters,
87 JstringHandle(env, alteredParameters).value());
88 mediametrics_setInt32(item, kAttributeVideoWidth, videoWidth);
89 mediametrics_setInt32(item, kAttributeVideoHeight, videoHeight);
90 mediametrics_selfRecord(item);
91 mediametrics_delete(item);
92 }
93