1 /*
2  * Copyright 2019, 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 //#define LOG_NDEBUG 0
17 #define LOG_TAG "MediaTranscodeManager_JNI"
18 
19 #include "android_runtime/AndroidRuntime.h"
20 #include "jni.h"
21 
22 #include <nativehelper/JNIHelp.h>
23 #include <utils/Log.h>
24 
25 namespace {
26 
27 // NOTE: Keep these enums in sync with their equivalents in MediaTranscodeManager.java.
28 enum {
29     ID_INVALID = -1
30 };
31 
32 enum {
33     EVENT_JOB_STARTED = 1,
34     EVENT_JOB_PROGRESSED = 2,
35     EVENT_JOB_FINISHED = 3,
36 };
37 
38 enum {
39     RESULT_NONE = 1,
40     RESULT_SUCCESS = 2,
41     RESULT_ERROR = 3,
42     RESULT_CANCELED = 4,
43 };
44 
45 struct {
46     jmethodID postEventFromNative;
47 } gMediaTranscodeManagerClassInfo;
48 
49 using namespace android;
50 
android_media_MediaTranscodeManager_native_init(JNIEnv * env,jclass clazz)51 void android_media_MediaTranscodeManager_native_init(JNIEnv *env, jclass clazz) {
52     ALOGV("android_media_MediaTranscodeManager_native_init");
53 
54     gMediaTranscodeManagerClassInfo.postEventFromNative = env->GetMethodID(
55             clazz, "postEventFromNative", "(IJI)V");
56     LOG_ALWAYS_FATAL_IF(gMediaTranscodeManagerClassInfo.postEventFromNative == NULL,
57                         "can't find android/media/MediaTranscodeManager.postEventFromNative");
58 }
59 
android_media_MediaTranscodeManager_requestUniqueJobID(JNIEnv * env __unused,jobject thiz __unused)60 jlong android_media_MediaTranscodeManager_requestUniqueJobID(
61         JNIEnv *env __unused, jobject thiz __unused) {
62     ALOGV("android_media_MediaTranscodeManager_reserveUniqueJobID");
63     static std::atomic_int32_t sJobIDCounter{0};
64     jlong id = (jlong)++sJobIDCounter;
65     return id;
66 }
67 
android_media_MediaTranscodeManager_enqueueTranscodingRequest(JNIEnv * env,jobject thiz,jlong id,jobject request,jobject context __unused)68 jboolean android_media_MediaTranscodeManager_enqueueTranscodingRequest(
69         JNIEnv *env, jobject thiz, jlong id, jobject request, jobject context __unused) {
70     ALOGV("android_media_MediaTranscodeManager_enqueueTranscodingRequest");
71     if (!request) {
72         return ID_INVALID;
73     }
74 
75     env->CallVoidMethod(thiz, gMediaTranscodeManagerClassInfo.postEventFromNative,
76                         EVENT_JOB_FINISHED, id, RESULT_ERROR);
77     return true;
78 }
79 
android_media_MediaTranscodeManager_cancelTranscodingRequest(JNIEnv * env __unused,jobject thiz __unused,jlong jobID __unused)80 void android_media_MediaTranscodeManager_cancelTranscodingRequest(
81         JNIEnv *env __unused, jobject thiz __unused, jlong jobID __unused) {
82     ALOGV("android_media_MediaTranscodeManager_cancelTranscodingRequest");
83 }
84 
85 const JNINativeMethod gMethods[] = {
86     { "native_init", "()V",
87         (void *)android_media_MediaTranscodeManager_native_init },
88     { "native_requestUniqueJobID", "()J",
89         (void *)android_media_MediaTranscodeManager_requestUniqueJobID },
90     { "native_enqueueTranscodingRequest",
91         "(JLandroid/media/MediaTranscodeManager$TranscodingRequest;Landroid/content/Context;)Z",
92         (void *)android_media_MediaTranscodeManager_enqueueTranscodingRequest },
93     { "native_cancelTranscodingRequest", "(J)V",
94         (void *)android_media_MediaTranscodeManager_cancelTranscodingRequest },
95 };
96 
97 } // namespace anonymous
98 
register_android_media_MediaTranscodeManager(JNIEnv * env)99 int register_android_media_MediaTranscodeManager(JNIEnv *env) {
100     return AndroidRuntime::registerNativeMethods(env,
101                 "android/media/MediaTranscodeManager", gMethods, NELEM(gMethods));
102 }
103