1 /*
2 * Copyright (C) 2023 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_TAG "DeviceAsWebcamNative"
18
19 #include <DeviceAsWebcamServiceManager.h>
20 #include <log/log.h>
21 #include <nativehelper/JNIHelp.h>
22
23 #include "DeviceAsWebcamNative.h"
24
25 /**
26 * Called by the JVM when JNI is loaded. This does not imply that the service is actually running,
27 * only that the jni library is loaded.
28 */
JNI_OnLoad(JavaVM * jvm,void *)29 jint JNI_OnLoad(JavaVM* jvm, void*) {
30 using namespace android::webcam;
31 JNIEnv* e = NULL;
32 if (jvm->GetEnv((void**)&e, JNI_VERSION_1_6)) {
33 return JNI_ERR;
34 }
35 if (DeviceAsWebcamNative::registerJNIMethods(e, jvm)) {
36 return JNI_ERR;
37 }
38 return JNI_VERSION_1_6;
39 }
40
41 namespace android {
42 namespace webcam {
43
44 // These will be initialized by registerJNIMethods
45 JavaMethods DeviceAsWebcamNative::kJavaMethods = {};
46 JavaVM* DeviceAsWebcamNative::kJVM = nullptr;
47
FindClassOrDie(JNIEnv * env,const char * class_name)48 static inline jclass FindClassOrDie(JNIEnv* env, const char* class_name) {
49 jclass clazz = env->FindClass(class_name);
50 LOG_ALWAYS_FATAL_IF(clazz == nullptr, "Unable to find class %s", class_name);
51 return clazz;
52 }
53
GetMethodIdOrDie(JNIEnv * env,jclass clazz,const char * method_name,const char * method_signature)54 static inline jmethodID GetMethodIdOrDie(JNIEnv* env, jclass clazz, const char* method_name,
55 const char* method_signature) {
56 jmethodID res = env->GetMethodID(clazz, method_name, method_signature);
57 LOG_ALWAYS_FATAL_IF(res == nullptr, "Unable to find method %s with signature %s", method_name,
58 method_signature);
59 return res;
60 }
61
62 const JNINativeMethod DeviceAsWebcamNative::sMethods[] = {
63 {"setupServicesAndStartListeningNative", "([Ljava/lang/String;)I",
64 (void*)com_android_DeviceAsWebcam_setupServicesAndStartListening},
65 {"nativeOnDestroy", "()V", (void*)com_android_DeviceAsWebcam_onDestroy},
66 {"shouldStartServiceNative", "([Ljava/lang/String;)Z",
67 (void*)com_android_DeviceAsWebcam_shouldStartService},
68 {"nativeEncodeImage", "(Landroid/hardware/HardwareBuffer;JI)I",
69 (void*)com_android_DeviceAsWebcam_encodeImage},
70 };
71
registerJNIMethods(JNIEnv * e,JavaVM * jvm)72 int DeviceAsWebcamNative::registerJNIMethods(JNIEnv* e, JavaVM* jvm) {
73 char clsName[] = "com/android/DeviceAsWebcam/DeviceAsWebcamFgService";
74 int ret = jniRegisterNativeMethods(e, clsName, sMethods, NELEM(sMethods));
75 if (ret) {
76 return JNI_ERR;
77 }
78
79 jclass clazz = FindClassOrDie(e, clsName);
80 kJavaMethods.setStreamConfig = GetMethodIdOrDie(e, clazz, "setStreamConfig", "(ZIII)V");
81 kJavaMethods.startStreaming = GetMethodIdOrDie(e, clazz, "startStreaming", "()V");
82 kJavaMethods.stopStreaming = GetMethodIdOrDie(e, clazz, "stopStreaming", "()V");
83 kJavaMethods.stopService = GetMethodIdOrDie(e, clazz, "stopService", "()V");
84 kJavaMethods.returnImage = GetMethodIdOrDie(e, clazz, "returnImage", "(J)V");
85
86 kJVM = jvm;
87 return 0;
88 }
89
com_android_DeviceAsWebcam_encodeImage(JNIEnv * env,jobject,jobject hardwareBuffer,jlong timestamp,jint rotation)90 jint DeviceAsWebcamNative::com_android_DeviceAsWebcam_encodeImage(JNIEnv* env, jobject,
91 jobject hardwareBuffer,
92 jlong timestamp,
93 jint rotation) {
94 return DeviceAsWebcamServiceManager::kInstance->encodeImage(env, hardwareBuffer, timestamp,
95 rotation);
96 }
97
com_android_DeviceAsWebcam_setupServicesAndStartListening(JNIEnv * env,jobject thiz,jobjectArray jIgnoredNodes)98 jint DeviceAsWebcamNative::com_android_DeviceAsWebcam_setupServicesAndStartListening(
99 JNIEnv* env, jobject thiz, jobjectArray jIgnoredNodes) {
100 return DeviceAsWebcamServiceManager::kInstance->setupServicesAndStartListening(env, thiz,
101 jIgnoredNodes);
102 }
103
com_android_DeviceAsWebcam_shouldStartService(JNIEnv *,jclass,jobjectArray jIgnoredNodes)104 jboolean DeviceAsWebcamNative::com_android_DeviceAsWebcam_shouldStartService(
105 JNIEnv*, jclass, jobjectArray jIgnoredNodes) {
106 return DeviceAsWebcamServiceManager::kInstance->shouldStartService(jIgnoredNodes);
107 }
108
com_android_DeviceAsWebcam_onDestroy(JNIEnv *,jobject)109 void DeviceAsWebcamNative::com_android_DeviceAsWebcam_onDestroy(JNIEnv*, jobject) {
110 DeviceAsWebcamServiceManager::kInstance->onDestroy();
111 }
112
setStreamConfig(jobject thiz,bool mjpeg,uint32_t width,uint32_t height,uint32_t fps)113 void DeviceAsWebcamNative::setStreamConfig(jobject thiz, bool mjpeg, uint32_t width,
114 uint32_t height, uint32_t fps) {
115 JNIEnv* env = getJNIEnvOrAbort();
116 jboolean jMjpeg = mjpeg ? JNI_TRUE : JNI_FALSE;
117 jint jWidth = static_cast<jint>(width);
118 jint jHeight = static_cast<jint>(height);
119 jint jFps = static_cast<jint>(fps);
120 env->CallVoidMethod(thiz, kJavaMethods.setStreamConfig, jMjpeg, jWidth, jHeight, jFps);
121 }
122
startStreaming(jobject thiz)123 void DeviceAsWebcamNative::startStreaming(jobject thiz) {
124 JNIEnv* env = getJNIEnvOrAbort();
125 env->CallVoidMethod(thiz, kJavaMethods.startStreaming);
126 }
stopStreaming(jobject thiz)127 void DeviceAsWebcamNative::stopStreaming(jobject thiz) {
128 JNIEnv* env = getJNIEnvOrAbort();
129 env->CallVoidMethod(thiz, kJavaMethods.stopStreaming);
130 }
131
returnImage(jobject thiz,long timestamp)132 void DeviceAsWebcamNative::returnImage(jobject thiz, long timestamp) {
133 JNIEnv* env = getJNIEnvOrAbort();
134 jlong jTimestamp = static_cast<jlong>(timestamp);
135 env->CallVoidMethod(thiz, kJavaMethods.returnImage, jTimestamp);
136 }
137
stopService(jobject thiz)138 void DeviceAsWebcamNative::stopService(jobject thiz) {
139 JNIEnv* env = getJNIEnvOrAbort();
140 env->CallVoidMethod(thiz, kJavaMethods.stopService);
141 }
142
getJNIEnvOrAbort()143 JNIEnv* DeviceAsWebcamNative::getJNIEnvOrAbort() {
144 JNIEnv* env = nullptr;
145 kJVM->GetEnv(reinterpret_cast<void**>(&env), JNI_VERSION_1_6);
146 if (env == nullptr) {
147 ALOGE("%s: Called from a thread not bound to the JVM", __FUNCTION__);
148 // Call abort() to force creation of a tombstone for easier debugging.
149 abort(); // :(
150 }
151 return env;
152 }
153
154 } // namespace webcam
155 } // namespace android