1 /* 2 * Copyright (C) 2016 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 _ACAMERA_CAPTURE_SESSION_H 17 #define _ACAMERA_CAPTURE_SESSION_H 18 19 #include <set> 20 #include <string> 21 #include <hardware/camera3.h> 22 #include <camera/NdkCameraDevice.h> 23 24 #ifdef __ANDROID_VNDK__ 25 #include "ndk_vendor/impl/ACameraDevice.h" 26 #else 27 #include "ACameraDevice.h" 28 #endif 29 30 using namespace android; 31 32 struct ACaptureSessionOutput { 33 explicit ACaptureSessionOutput(ANativeWindow* window, bool isShared = false, 34 const char* physicalCameraId = "") : mWindowACaptureSessionOutput35 mWindow(window), mIsShared(isShared), mPhysicalCameraId(physicalCameraId) {}; 36 37 bool operator == (const ACaptureSessionOutput& other) const { 38 return mWindow == other.mWindow; 39 } 40 bool operator != (const ACaptureSessionOutput& other) const { 41 return mWindow != other.mWindow; 42 } 43 bool operator < (const ACaptureSessionOutput& other) const { 44 return mWindow < other.mWindow; 45 } 46 bool operator > (const ACaptureSessionOutput& other) const { 47 return mWindow > other.mWindow; 48 } 49 isWindowEqualACaptureSessionOutput50 inline bool isWindowEqual(ANativeWindow* window) const { 51 return mWindow == window; 52 } 53 54 // returns true if the window was successfully added, false otherwise. addSharedWindowACaptureSessionOutput55 inline bool addSharedWindow(ANativeWindow* window) { 56 auto ret = mSharedWindows.insert(window); 57 return ret.second; 58 } 59 60 // returns the number of elements removed. removeSharedWindowACaptureSessionOutput61 inline size_t removeSharedWindow(ANativeWindow* window) { 62 return mSharedWindows.erase(window); 63 } 64 65 ANativeWindow* mWindow; 66 std::set<ANativeWindow*> mSharedWindows; 67 bool mIsShared; 68 int mRotation = CAMERA3_STREAM_ROTATION_0; 69 std::string mPhysicalCameraId; 70 }; 71 72 struct ACaptureSessionOutputContainer { 73 std::set<ACaptureSessionOutput> mOutputs; 74 }; 75 76 /** 77 * Capture session state callbacks used in {@link ACameraDevice_setPrepareCallbacks} 78 */ 79 typedef struct ACameraCaptureSession_prepareCallbacks { 80 /// optional application context. This will be passed in the context 81 /// parameter of the {@link onWindowPrepared} callback. 82 void* context; 83 84 ACameraCaptureSession_prepareCallback onWindowPrepared; 85 } ACameraCaptureSession_prepareCallbacks; 86 87 /** 88 * ACameraCaptureSession opaque struct definition 89 * Leave outside of android namespace because it's NDK struct 90 */ 91 struct ACameraCaptureSession : public RefBase { 92 public: 93 #ifdef __ANDROID_VNDK__ ACameraCaptureSessionACameraCaptureSession94 ACameraCaptureSession( 95 int id, 96 const ACaptureSessionOutputContainer* outputs, 97 const ACameraCaptureSession_stateCallbacks* cb, 98 std::weak_ptr<android::acam::CameraDevice> device) : 99 mId(id), mOutput(*outputs), mUserSessionCallback(*cb), 100 mDevice(std::move(device)) {} 101 #else 102 ACameraCaptureSession( 103 int id, 104 const ACaptureSessionOutputContainer* outputs, 105 const ACameraCaptureSession_stateCallbacks* cb, 106 android::acam::CameraDevice* device) : 107 mId(id), mOutput(*outputs), mUserSessionCallback(*cb), 108 mDevice(device) {} 109 #endif 110 111 // This can be called in app calling close() or after some app callback is finished 112 // Make sure the caller does not hold device or session lock! 113 ~ACameraCaptureSession(); 114 115 // No API except Session_Close will work if device is closed 116 // A session will enter closed state when one of the following happens: 117 // 1. Explicitly closed by app 118 // 2. Replaced by a newer session 119 // 3. Device is closed isClosedACameraCaptureSession120 bool isClosed() { Mutex::Autolock _l(mSessionLock); return mIsClosed; } 121 122 // Close the session and mark app no longer need this session. 123 void closeByApp(); 124 125 camera_status_t stopRepeating(); 126 127 camera_status_t abortCaptures(); 128 129 template<class T> 130 camera_status_t setRepeatingRequest( 131 /*optional*/T* cbs, 132 int numRequests, ACaptureRequest** requests, 133 /*optional*/int* captureSequenceId); 134 135 template<class T> 136 camera_status_t capture( 137 /*optional*/T* cbs, 138 int numRequests, ACaptureRequest** requests, 139 /*optional*/int* captureSequenceId); 140 141 camera_status_t updateOutputConfiguration(ACaptureSessionOutput *output); 142 setWindowPreparedCallbackACameraCaptureSession143 void setWindowPreparedCallback(void *context, 144 ACameraCaptureSession_prepareCallback cb) { 145 Mutex::Autolock _l(mSessionLock); 146 mPreparedCb.context = context; 147 mPreparedCb.onWindowPrepared = cb; 148 } 149 camera_status_t prepare(ANativeWindow *window); 150 151 ACameraDevice* getDevice(); 152 153 private: 154 friend class android::acam::CameraDevice; 155 156 // Close session because app close camera device, camera device got ERROR_DISCONNECTED, 157 // or a new session is replacing this session. 158 void closeByDevice(); 159 160 #ifdef __ANDROID_VNDK__ 161 std::shared_ptr<android::acam::CameraDevice> getDevicePtr(); 162 #else 163 sp<android::acam::CameraDevice> getDeviceSp(); 164 #endif 165 166 const int mId; 167 const ACaptureSessionOutputContainer mOutput; 168 const ACameraCaptureSession_stateCallbacks mUserSessionCallback; 169 #ifdef __ANDROID_VNDK__ 170 const std::weak_ptr<android::acam::CameraDevice> mDevice; 171 #else 172 const wp<android::acam::CameraDevice> mDevice; 173 #endif 174 175 bool mIsClosed = false; 176 bool mClosedByApp = false; 177 ACameraCaptureSession_prepareCallbacks mPreparedCb; 178 Mutex mSessionLock; 179 }; 180 181 #endif // _ACAMERA_CAPTURE_SESSION_H 182