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 /* 18 * Manage the remote camera service native functions. 19 */ 20 #pragma once 21 #include <jni.h> 22 #include <mutex> 23 #include <thread> 24 25 namespace android { 26 namespace webcam { 27 28 class UVCProvider; 29 class DeviceAsWebcamServiceManager { 30 public: 31 // Singleton instance. All member functions/fields should be accessed through this instance. 32 // It lives indefinitely. Ctor and Dtor should not be relied on for cleanup of resources. 33 static DeviceAsWebcamServiceManager* kInstance; 34 35 // Returns true if the java service needs to be started. This is called by the USB Broadcast 36 // receiver which might multiple receive spurious calls to start the service. 37 bool shouldStartService(jobjectArray jIgnoredNodes); 38 // Inits the native side of the service. This function should be called by the Java service 39 // before any of the functions below it 40 int setupServicesAndStartListening(JNIEnv* env, jobject javaService, 41 jobjectArray jIgnoredNodes); 42 // Called by Java to encode a frame 43 int encodeImage(JNIEnv* env, jobject hardwareBuffer, jlong timestamp, jint rotation); 44 // Called by native service to set the stream configuration in the Java Service. 45 void setStreamConfig(bool mjpeg, uint32_t width, uint32_t height, uint32_t fps); 46 // Called by native service to notify the Java service to start streaming the camera. 47 void startStreaming(); 48 // Called by native service to notify the Java service to stop streaming the camera. 49 void stopStreaming(); 50 // Called by native service to return an Image to the Java service. 51 void returnImage(long timestamp); 52 // Called by the Native Service when it wants to signal the Java service to stop. 53 // This is non-blocking and does not guarantee that the Java service has stopped on return. 54 void stopService(); 55 // Called by Java when the foreground service is being destroyed. 56 void onDestroy(); 57 58 ~DeviceAsWebcamServiceManager() = default; 59 60 private: 61 DeviceAsWebcamServiceManager() = default; 62 63 std::mutex mSerializationLock; // Serializes all methods in class 64 bool mServiceRunning = false; // if this is true, then the variables underneath can be 65 // considered safe to use without further checking. 66 jobject mJavaService = nullptr; // strong reference to the current foreground service. 67 std::shared_ptr<UVCProvider> mUVCProvider; 68 std::thread mJniThread; // thread to make asynchronous calls to Java 69 }; 70 71 } // namespace webcam 72 } // namespace android 73