1 /* 2 * Copyright 2021 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 #ifndef ANDROID_CARSERVICE_EVS_SERVICE_WRAPPER_H 17 #define ANDROID_CARSERVICE_EVS_SERVICE_WRAPPER_H 18 19 #include "EvsCallbackThread.h" 20 #include "EvsDeathRecipient.h" 21 #include "EvsServiceCallback.h" 22 #include "StreamHandler.h" 23 24 #include <android/hardware/automotive/evs/1.1/IEvsDisplay.h> 25 #include <android/hardware/automotive/evs/1.1/IEvsEnumerator.h> 26 #include <android/hardware/automotive/evs/1.1/types.h> 27 #include <hidl/HidlTransportSupport.h> 28 #include <utils/Mutex.h> 29 #include <utils/StrongPointer.h> 30 31 #include <jni.h> 32 33 #include <mutex> 34 35 namespace android { 36 namespace automotive { 37 namespace evs { 38 39 /* 40 * This class wraps around HIDL transactions to the Extended View System service 41 * and the video stream managements. 42 */ 43 class EvsServiceContext : public EvsServiceCallback { 44 public: 45 EvsServiceContext(JavaVM* vm, jclass clazz); 46 virtual ~EvsServiceContext(); 47 48 /* 49 * Initializes the service context and connects to the native Extended View 50 * System service. 51 * 52 * @param env A pointer to the JNI environment 53 * @param env A reference to CarEvsService object 54 * @return false if it fails to connect to the native Extended View System 55 * service or to register a death recipient. 56 * true otherwise. 57 */ 58 bool initialize(JNIEnv* env, jobject thiz) ACQUIRE(mLock); 59 60 /* 61 * Requests to open a target camera device. 62 * 63 * @param id a string camera device identifier 64 * @return bool false if it has not connected to EVS service, fails to open 65 * a camera device, or fails to initialize a stream handler; 66 * true otherwise. 67 */ 68 bool openCamera(const char* id) ACQUIRE(mLock); 69 70 /* 71 * Requests to close an active camera device. 72 */ 73 void closeCamera(); 74 75 /* 76 * Requests to start a video stream from a successfully opened camera device. 77 */ 78 bool startVideoStream(); 79 80 /* 81 * Requests to stop an active video stream. 82 */ 83 void stopVideoStream(); 84 85 /* 86 * Notifies that the client finishes with this buffer. 87 * 88 * @param frame a consumed frame buffer 89 */ 90 void doneWithFrame(int bufferId); 91 92 /* 93 * Tells whether or not we're connected to the Extended View System service 94 */ isAvailable()95 bool isAvailable() ACQUIRE(mLock) { 96 std::lock_guard<std::mutex> lock(mLock); 97 return mService != nullptr; 98 } 99 100 /* 101 * Tells whether or not a target camera device is opened 102 */ isCameraOpened()103 bool isCameraOpened() ACQUIRE(mLock) { 104 std::lock_guard<std::mutex> lock(mLock); 105 return mCamera != nullptr; 106 } 107 108 /* 109 * Compares the binder interface 110 */ isEqual(const wp<hidl::base::V1_0::IBase> & who)111 bool isEqual(const wp<hidl::base::V1_0::IBase>& who) ACQUIRE(mLock) { 112 std::lock_guard<std::mutex> lock(mLock); 113 return hardware::interfacesEqual(mService, who.promote()); 114 } 115 116 /* 117 * Implements EvsServiceCallback methods 118 */ 119 void onNewEvent(hardware::automotive::evs::V1_1::EvsEventDesc) override; 120 void onNewFrame(hardware::automotive::evs::V1_1::BufferDesc) override; 121 void onServiceDied(const wp<hidl::base::V1_0::IBase>&) override; 122 123 private: 124 // Acquires the camera and the display exclusive ownerships. 125 void acquireCameraAndDisplay() ACQUIRE(mLock); 126 127 // A mutex to protect shared resources 128 mutable std::mutex mLock; 129 130 // Extended View System Enumerator service handle 131 sp<hardware::automotive::evs::V1_1::IEvsEnumerator> mService GUARDED_BY(mLock); 132 133 // A camera device opened for the rearview service 134 sp<hardware::automotive::evs::V1_1::IEvsCamera> mCamera GUARDED_BY(mLock); 135 136 // A handler of a video stream from the rearview camera device 137 sp<StreamHandler> mStreamHandler GUARDED_BY(mLock); 138 139 // Extended View System display handle. This would not be used but held by 140 // us to prevent other EVS clients from using EvsDisplay. 141 sp<hardware::automotive::evs::V1_1::IEvsDisplay> mDisplay; 142 143 // A flag to acquire a display handle only once 144 std::once_flag mDisplayAcquired; 145 146 // A death recipient of Extended View System service 147 sp<EvsDeathRecipient> mDeathRecipient GUARDED_BY(mLock); 148 149 // Java VM 150 JavaVM* mVm; 151 152 // Background thread to handle callbacks from the native Extended View 153 // System service 154 EvsCallbackThread mCallbackThread; 155 156 // Reference to CarEvsService object 157 jobject mCarEvsServiceObj; 158 159 // CarEvsService object's method to handle the accidental death of the 160 // native Extended View System service 161 jmethodID mDeathHandlerMethodId; 162 163 // CarEvsService object's method to handle a new frame buffer 164 jmethodID mFrameHandlerMethodId; 165 166 // CarEvsService object's method to handle a new stream event 167 jmethodID mEventHandlerMethodId; 168 169 // Bookkeeps descriptors of received frame buffers. 170 std::map<int, hardware::automotive::evs::V1_1::BufferDesc> mBufferRecords GUARDED_BY(mLock); 171 172 // A name of the camera device currently in use. 173 const char* mCameraIdInUse; 174 175 // Service name for EVS enumerator 176 static const char* kServiceName; 177 178 // Maximum number of frames CarEvsService can hold. This number has been 179 // chosen heuristically. 180 static constexpr int kMaxNumFramesInFlight = 6; 181 182 // EVS service reserves a display ID 255 to allow the clients to open the main 183 // display exclusively. 184 static constexpr uint8_t kExclusiveMainDisplayId = 0xFF; 185 }; 186 187 } // namespace evs 188 } // namespace automotive 189 } // namespace android 190 191 #endif // ANDROID_CARSERVICE_EVS_SERVICE_WRAPPER_H 192