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 #include "DeviceAsWebcamServiceManager.h"
18 #include <DeviceAsWebcamNative.h>
19 #include <UVCProvider.h>
20 #include <android/hardware_buffer_jni.h>
21 #include <log/log.h>
22 #include <unordered_set>
23
24 namespace android {
25 namespace webcam {
26
27 namespace {
28 /**
29 * Copies the given javaArr to a std::unordered_set<std::string>. javaArr must be of type String[].
30 */
stringSetFromJavaArray(jobjectArray javaArr)31 std::unordered_set<std::string> stringSetFromJavaArray(jobjectArray javaArr) {
32 JNIEnv* env = DeviceAsWebcamNative::getJNIEnvOrAbort();
33
34 std::unordered_set<std::string> ret = {};
35 jsize len = env->GetArrayLength(javaArr);
36 for (jsize i = 0; i < len; i++) {
37 jstring jStr = (jstring) env->GetObjectArrayElement(javaArr, i);
38
39 const jsize strLen = env->GetStringUTFLength(jStr);
40 const char* buf = env->GetStringUTFChars(jStr, NULL);
41
42 ret.emplace(buf, strLen); // std::string makes a copy of the buffer
43
44 env->ReleaseStringUTFChars(jStr, buf);
45 env->DeleteLocalRef(jStr);
46 }
47 return ret;
48 }
49 } // anonymous namespace
50
51 DeviceAsWebcamServiceManager* DeviceAsWebcamServiceManager::kInstance =
52 new DeviceAsWebcamServiceManager();
53
shouldStartService(jobjectArray jIgnoredNodes)54 bool DeviceAsWebcamServiceManager::shouldStartService(jobjectArray jIgnoredNodes) {
55 ALOGV("%s", __FUNCTION__);
56 std::lock_guard<std::mutex> l(mSerializationLock);
57 if (mServiceRunning) {
58 ALOGW("Service already running, don't start it again.");
59 return false;
60 }
61
62 std::unordered_set<std::string> ignoredNodes = stringSetFromJavaArray(jIgnoredNodes);
63 return (UVCProvider::getVideoNode(ignoredNodes).length() != 0);
64 }
65
setupServicesAndStartListening(JNIEnv * env,jobject javaService,jobjectArray jIgnoredNodes)66 int DeviceAsWebcamServiceManager::setupServicesAndStartListening(JNIEnv* env, jobject javaService,
67 jobjectArray jIgnoredNodes) {
68 ALOGV("%s", __FUNCTION__);
69 std::lock_guard<std::mutex> l(mSerializationLock);
70 if (mUVCProvider == nullptr) {
71 mUVCProvider = std::make_shared<UVCProvider>();
72 }
73
74 std::unordered_set<std::string> ignoredNodes = stringSetFromJavaArray(jIgnoredNodes);
75 // Set up UVC stack
76 if ((mUVCProvider->init() != Status::OK) ||
77 (mUVCProvider->startService(ignoredNodes) != Status::OK)) {
78 ALOGE("%s: Unable to init/ start service", __FUNCTION__);
79 return -1;
80 }
81 mJavaService = env->NewGlobalRef(javaService);
82 mServiceRunning = true;
83 return 0;
84 }
85
encodeImage(JNIEnv * env,jobject hardwareBuffer,jlong timestamp,jint rotation)86 int DeviceAsWebcamServiceManager::encodeImage(JNIEnv* env, jobject hardwareBuffer,
87 jlong timestamp, jint rotation) {
88 ALOGV("%s", __FUNCTION__);
89 std::lock_guard<std::mutex> l(mSerializationLock);
90 if (!mServiceRunning) {
91 ALOGE("%s called, but native service is not running. Ignoring call.", __FUNCTION__);
92 return -1;
93 }
94 AHardwareBuffer* buffer = AHardwareBuffer_fromHardwareBuffer(env, hardwareBuffer);
95 return mUVCProvider->encodeImage(buffer, timestamp, rotation);
96 }
97
setStreamConfig(bool mjpeg,uint32_t width,uint32_t height,uint32_t fps)98 void DeviceAsWebcamServiceManager::setStreamConfig(bool mjpeg, uint32_t width, uint32_t height,
99 uint32_t fps) {
100 ALOGV("%s", __FUNCTION__);
101 std::lock_guard<std::mutex> l(mSerializationLock);
102 if (!mServiceRunning) {
103 ALOGE("%s called but java foreground service is not running. No-op-ing out", __FUNCTION__);
104 return;
105 }
106 DeviceAsWebcamNative::setStreamConfig(mJavaService, mjpeg, width, height, fps);
107 }
108
startStreaming()109 void DeviceAsWebcamServiceManager::startStreaming() {
110 ALOGV("%s", __FUNCTION__);
111 std::lock_guard<std::mutex> l(mSerializationLock);
112 if (!mServiceRunning) {
113 ALOGE("%s called but java foreground service is not running. No-op-ing out", __FUNCTION__);
114 return;
115 }
116 DeviceAsWebcamNative::startStreaming(mJavaService);
117 }
118
stopStreaming()119 void DeviceAsWebcamServiceManager::stopStreaming() {
120 ALOGV("%s", __FUNCTION__);
121 std::lock_guard<std::mutex> l(mSerializationLock);
122 if (!mServiceRunning) {
123 ALOGE("%s called but java foreground service is not running. No-op-ing out", __FUNCTION__);
124 return;
125 }
126 DeviceAsWebcamNative::stopStreaming(mJavaService);
127 }
128
returnImage(long timestamp)129 void DeviceAsWebcamServiceManager::returnImage(long timestamp) {
130 ALOGV("%s", __FUNCTION__);
131 std::lock_guard<std::mutex> l(mSerializationLock);
132 if (!mServiceRunning) {
133 ALOGE("%s called but java foreground service is not running. No-op-ing out", __FUNCTION__);
134 return;
135 }
136 DeviceAsWebcamNative::returnImage(mJavaService, timestamp);
137 }
138
stopService()139 void DeviceAsWebcamServiceManager::stopService() {
140 ALOGV("%s", __FUNCTION__);
141 std::lock_guard<std::mutex> l(mSerializationLock);
142 if (!mServiceRunning) {
143 ALOGE("%s called but java foreground service is not running. No-op-ing out", __FUNCTION__);
144 return;
145 }
146 // Wait for previous stopService java call to finish (note that this should almost
147 // never actually block unless something goes really wrong).
148 if (mJniThread.joinable()) {
149 mJniThread.join();
150 }
151
152 // Send off the event on a background thread. This prevents the caller thread being stuck on
153 // Java logic.
154 mJniThread = DeviceAsWebcamNative::createJniAttachedThread(&DeviceAsWebcamNative::stopService,
155 mJavaService);
156 // Don't reset any state as the Java service's onDestroy callback will handle that for us.
157 }
158
159 // Called by the Java Foreground service when it is being destroyed. The UVCProvider may or may not
160 // be running at this point.
onDestroy()161 void DeviceAsWebcamServiceManager::onDestroy() {
162 ALOGV("%s", __FUNCTION__);
163 std::lock_guard<std::mutex> l(mSerializationLock);
164 if (!mServiceRunning) {
165 ALOGE("%s called after Java Service was already considered destroyed. No-op-ing out.",
166 __FUNCTION__);
167 return;
168 }
169
170 JNIEnv* env = DeviceAsWebcamNative::getJNIEnvOrAbort();
171 // reset all non-static state
172 mUVCProvider = nullptr;
173 env->DeleteGlobalRef(mJavaService); // let Java Service be GC'ed by the JVM
174 mJavaService = nullptr;
175 mServiceRunning = false;
176 }
177
178 } // namespace webcam
179 } // namespace android
180