Yin-Chia Yeh | ead9146 | 2016-01-06 16:45:08 -0800 | [diff] [blame] | 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> |
Shuzhen Wang | 0ff9ae3 | 2018-12-05 18:06:12 -0800 | [diff] [blame] | 20 | #include <string> |
Yin-Chia Yeh | ead9146 | 2016-01-06 16:45:08 -0800 | [diff] [blame] | 21 | #include <hardware/camera3.h> |
Colin Cross | 7e8d4ba | 2017-05-04 16:17:42 -0700 | [diff] [blame] | 22 | #include <camera/NdkCameraDevice.h> |
Jayant Chowdhary | 6df2607 | 2018-11-06 23:55:12 -0800 | [diff] [blame] | 23 | |
| 24 | #ifdef __ANDROID_VNDK__ |
| 25 | #include "ndk_vendor/impl/ACameraDevice.h" |
Jayant Chowdhary | 6df2607 | 2018-11-06 23:55:12 -0800 | [diff] [blame] | 26 | #else |
Yin-Chia Yeh | ead9146 | 2016-01-06 16:45:08 -0800 | [diff] [blame] | 27 | #include "ACameraDevice.h" |
Avichal Rakesh | 8effe98 | 2023-11-13 18:53:40 -0800 | [diff] [blame] | 28 | #endif |
Yin-Chia Yeh | ead9146 | 2016-01-06 16:45:08 -0800 | [diff] [blame] | 29 | |
| 30 | using namespace android; |
| 31 | |
| 32 | struct ACaptureSessionOutput { |
Avichal Rakesh | 8effe98 | 2023-11-13 18:53:40 -0800 | [diff] [blame] | 33 | explicit ACaptureSessionOutput(ANativeWindow* window, bool isShared = false, |
Shuzhen Wang | 0ff9ae3 | 2018-12-05 18:06:12 -0800 | [diff] [blame] | 34 | const char* physicalCameraId = "") : |
| 35 | mWindow(window), mIsShared(isShared), mPhysicalCameraId(physicalCameraId) {}; |
Yin-Chia Yeh | ead9146 | 2016-01-06 16:45:08 -0800 | [diff] [blame] | 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 | |
Avichal Rakesh | 8effe98 | 2023-11-13 18:53:40 -0800 | [diff] [blame] | 50 | inline bool isWindowEqual(ANativeWindow* window) const { |
Avichal Rakesh | f099b23 | 2022-10-27 15:44:50 -0700 | [diff] [blame] | 51 | return mWindow == window; |
| 52 | } |
| 53 | |
| 54 | // returns true if the window was successfully added, false otherwise. |
Avichal Rakesh | 8effe98 | 2023-11-13 18:53:40 -0800 | [diff] [blame] | 55 | inline bool addSharedWindow(ANativeWindow* window) { |
Avichal Rakesh | f099b23 | 2022-10-27 15:44:50 -0700 | [diff] [blame] | 56 | auto ret = mSharedWindows.insert(window); |
| 57 | return ret.second; |
| 58 | } |
| 59 | |
| 60 | // returns the number of elements removed. |
Avichal Rakesh | 8effe98 | 2023-11-13 18:53:40 -0800 | [diff] [blame] | 61 | inline size_t removeSharedWindow(ANativeWindow* window) { |
Avichal Rakesh | f099b23 | 2022-10-27 15:44:50 -0700 | [diff] [blame] | 62 | return mSharedWindows.erase(window); |
| 63 | } |
| 64 | |
Avichal Rakesh | 8effe98 | 2023-11-13 18:53:40 -0800 | [diff] [blame] | 65 | ANativeWindow* mWindow; |
| 66 | std::set<ANativeWindow*> mSharedWindows; |
Emilian Peev | 40ead60 | 2017-09-26 15:46:36 +0100 | [diff] [blame] | 67 | bool mIsShared; |
Yin-Chia Yeh | ead9146 | 2016-01-06 16:45:08 -0800 | [diff] [blame] | 68 | int mRotation = CAMERA3_STREAM_ROTATION_0; |
Shuzhen Wang | 0ff9ae3 | 2018-12-05 18:06:12 -0800 | [diff] [blame] | 69 | std::string mPhysicalCameraId; |
Yin-Chia Yeh | ead9146 | 2016-01-06 16:45:08 -0800 | [diff] [blame] | 70 | }; |
| 71 | |
| 72 | struct ACaptureSessionOutputContainer { |
| 73 | std::set<ACaptureSessionOutput> mOutputs; |
| 74 | }; |
| 75 | |
| 76 | /** |
Jayant Chowdhary | 719b466 | 2023-03-14 20:30:57 +0000 | [diff] [blame] | 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 | /** |
Yin-Chia Yeh | ead9146 | 2016-01-06 16:45:08 -0800 | [diff] [blame] | 88 | * ACameraCaptureSession opaque struct definition |
| 89 | * Leave outside of android namespace because it's NDK struct |
| 90 | */ |
| 91 | struct ACameraCaptureSession : public RefBase { |
| 92 | public: |
Avichal Rakesh | f099b23 | 2022-10-27 15:44:50 -0700 | [diff] [blame] | 93 | #ifdef __ANDROID_VNDK__ |
| 94 | 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 |
Yin-Chia Yeh | ead9146 | 2016-01-06 16:45:08 -0800 | [diff] [blame] | 102 | ACameraCaptureSession( |
| 103 | int id, |
| 104 | const ACaptureSessionOutputContainer* outputs, |
| 105 | const ACameraCaptureSession_stateCallbacks* cb, |
Jayant Chowdhary | 6df2607 | 2018-11-06 23:55:12 -0800 | [diff] [blame] | 106 | android::acam::CameraDevice* device) : |
Yin-Chia Yeh | ead9146 | 2016-01-06 16:45:08 -0800 | [diff] [blame] | 107 | mId(id), mOutput(*outputs), mUserSessionCallback(*cb), |
| 108 | mDevice(device) {} |
Avichal Rakesh | f099b23 | 2022-10-27 15:44:50 -0700 | [diff] [blame] | 109 | #endif |
Yin-Chia Yeh | ead9146 | 2016-01-06 16:45:08 -0800 | [diff] [blame] | 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 |
| 120 | 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 | |
Yin-Chia Yeh | 309d05d | 2016-03-28 10:15:31 -0700 | [diff] [blame] | 127 | camera_status_t abortCaptures(); |
| 128 | |
Shuzhen Wang | 0ff9ae3 | 2018-12-05 18:06:12 -0800 | [diff] [blame] | 129 | template<class T> |
Yin-Chia Yeh | ead9146 | 2016-01-06 16:45:08 -0800 | [diff] [blame] | 130 | camera_status_t setRepeatingRequest( |
Shuzhen Wang | 0ff9ae3 | 2018-12-05 18:06:12 -0800 | [diff] [blame] | 131 | /*optional*/T* cbs, |
Yin-Chia Yeh | ead9146 | 2016-01-06 16:45:08 -0800 | [diff] [blame] | 132 | int numRequests, ACaptureRequest** requests, |
| 133 | /*optional*/int* captureSequenceId); |
| 134 | |
Shuzhen Wang | 0ff9ae3 | 2018-12-05 18:06:12 -0800 | [diff] [blame] | 135 | template<class T> |
Yin-Chia Yeh | ead9146 | 2016-01-06 16:45:08 -0800 | [diff] [blame] | 136 | camera_status_t capture( |
Shuzhen Wang | 0ff9ae3 | 2018-12-05 18:06:12 -0800 | [diff] [blame] | 137 | /*optional*/T* cbs, |
Yin-Chia Yeh | ead9146 | 2016-01-06 16:45:08 -0800 | [diff] [blame] | 138 | int numRequests, ACaptureRequest** requests, |
| 139 | /*optional*/int* captureSequenceId); |
| 140 | |
Emilian Peev | 40ead60 | 2017-09-26 15:46:36 +0100 | [diff] [blame] | 141 | camera_status_t updateOutputConfiguration(ACaptureSessionOutput *output); |
| 142 | |
Jayant Chowdhary | 719b466 | 2023-03-14 20:30:57 +0000 | [diff] [blame] | 143 | void setWindowPreparedCallback(void *context, |
| 144 | ACameraCaptureSession_prepareCallback cb) { |
Jayant Chowdhary | 09b368b | 2023-02-13 06:53:05 +0000 | [diff] [blame] | 145 | Mutex::Autolock _l(mSessionLock); |
Jayant Chowdhary | 719b466 | 2023-03-14 20:30:57 +0000 | [diff] [blame] | 146 | mPreparedCb.context = context; |
| 147 | mPreparedCb.onWindowPrepared = cb; |
Jayant Chowdhary | 09b368b | 2023-02-13 06:53:05 +0000 | [diff] [blame] | 148 | } |
Avichal Rakesh | 8effe98 | 2023-11-13 18:53:40 -0800 | [diff] [blame] | 149 | camera_status_t prepare(ANativeWindow *window); |
Jayant Chowdhary | 09b368b | 2023-02-13 06:53:05 +0000 | [diff] [blame] | 150 | |
Yin-Chia Yeh | ead9146 | 2016-01-06 16:45:08 -0800 | [diff] [blame] | 151 | ACameraDevice* getDevice(); |
| 152 | |
| 153 | private: |
Jayant Chowdhary | 6df2607 | 2018-11-06 23:55:12 -0800 | [diff] [blame] | 154 | friend class android::acam::CameraDevice; |
Yin-Chia Yeh | ead9146 | 2016-01-06 16:45:08 -0800 | [diff] [blame] | 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 | |
Avichal Rakesh | f099b23 | 2022-10-27 15:44:50 -0700 | [diff] [blame] | 160 | #ifdef __ANDROID_VNDK__ |
| 161 | std::shared_ptr<android::acam::CameraDevice> getDevicePtr(); |
| 162 | #else |
Jayant Chowdhary | 6df2607 | 2018-11-06 23:55:12 -0800 | [diff] [blame] | 163 | sp<android::acam::CameraDevice> getDeviceSp(); |
Avichal Rakesh | f099b23 | 2022-10-27 15:44:50 -0700 | [diff] [blame] | 164 | #endif |
Yin-Chia Yeh | ead9146 | 2016-01-06 16:45:08 -0800 | [diff] [blame] | 165 | |
| 166 | const int mId; |
| 167 | const ACaptureSessionOutputContainer mOutput; |
| 168 | const ACameraCaptureSession_stateCallbacks mUserSessionCallback; |
Avichal Rakesh | f099b23 | 2022-10-27 15:44:50 -0700 | [diff] [blame] | 169 | #ifdef __ANDROID_VNDK__ |
| 170 | const std::weak_ptr<android::acam::CameraDevice> mDevice; |
| 171 | #else |
Jayant Chowdhary | 6df2607 | 2018-11-06 23:55:12 -0800 | [diff] [blame] | 172 | const wp<android::acam::CameraDevice> mDevice; |
Avichal Rakesh | f099b23 | 2022-10-27 15:44:50 -0700 | [diff] [blame] | 173 | #endif |
| 174 | |
Yin-Chia Yeh | ead9146 | 2016-01-06 16:45:08 -0800 | [diff] [blame] | 175 | bool mIsClosed = false; |
Yin-Chia Yeh | 085dd09 | 2016-03-02 14:16:31 -0800 | [diff] [blame] | 176 | bool mClosedByApp = false; |
Jayant Chowdhary | 09b368b | 2023-02-13 06:53:05 +0000 | [diff] [blame] | 177 | ACameraCaptureSession_prepareCallbacks mPreparedCb; |
Yin-Chia Yeh | ead9146 | 2016-01-06 16:45:08 -0800 | [diff] [blame] | 178 | Mutex mSessionLock; |
| 179 | }; |
| 180 | |
| 181 | #endif // _ACAMERA_CAPTURE_SESSION_H |