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" |
| 26 | #include "ndk_vendor/impl/ACameraCaptureSessionVendor.h" |
| 27 | #else |
Yin-Chia Yeh | ead9146 | 2016-01-06 16:45:08 -0800 | [diff] [blame] | 28 | #include "ACameraDevice.h" |
| 29 | |
| 30 | using namespace android; |
| 31 | |
| 32 | struct ACaptureSessionOutput { |
Shuzhen Wang | 0ff9ae3 | 2018-12-05 18:06:12 -0800 | [diff] [blame] | 33 | explicit ACaptureSessionOutput(ACameraWindowType* window, bool isShared = false, |
| 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 | f099b23 | 2022-10-27 15:44:50 -0700 | [diff] [blame] | 50 | inline bool isWindowEqual(ACameraWindowType* window) const { |
| 51 | return mWindow == window; |
| 52 | } |
| 53 | |
| 54 | // returns true if the window was successfully added, false otherwise. |
| 55 | inline bool addSharedWindow(ACameraWindowType* window) { |
| 56 | auto ret = mSharedWindows.insert(window); |
| 57 | return ret.second; |
| 58 | } |
| 59 | |
| 60 | // returns the number of elements removed. |
| 61 | inline size_t removeSharedWindow(ACameraWindowType* window) { |
| 62 | return mSharedWindows.erase(window); |
| 63 | } |
| 64 | |
Jayant Chowdhary | 6df2607 | 2018-11-06 23:55:12 -0800 | [diff] [blame] | 65 | ACameraWindowType* mWindow; |
| 66 | std::set<ACameraWindowType *> 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 | }; |
Jayant Chowdhary | 6df2607 | 2018-11-06 23:55:12 -0800 | [diff] [blame] | 71 | #endif |
Yin-Chia Yeh | ead9146 | 2016-01-06 16:45:08 -0800 | [diff] [blame] | 72 | |
| 73 | struct ACaptureSessionOutputContainer { |
| 74 | std::set<ACaptureSessionOutput> mOutputs; |
| 75 | }; |
| 76 | |
| 77 | /** |
| 78 | * ACameraCaptureSession opaque struct definition |
| 79 | * Leave outside of android namespace because it's NDK struct |
| 80 | */ |
| 81 | struct ACameraCaptureSession : public RefBase { |
| 82 | public: |
Avichal Rakesh | f099b23 | 2022-10-27 15:44:50 -0700 | [diff] [blame] | 83 | #ifdef __ANDROID_VNDK__ |
| 84 | ACameraCaptureSession( |
| 85 | int id, |
| 86 | const ACaptureSessionOutputContainer* outputs, |
| 87 | const ACameraCaptureSession_stateCallbacks* cb, |
| 88 | std::weak_ptr<android::acam::CameraDevice> device) : |
| 89 | mId(id), mOutput(*outputs), mUserSessionCallback(*cb), |
| 90 | mDevice(std::move(device)) {} |
| 91 | #else |
Yin-Chia Yeh | ead9146 | 2016-01-06 16:45:08 -0800 | [diff] [blame] | 92 | ACameraCaptureSession( |
| 93 | int id, |
| 94 | const ACaptureSessionOutputContainer* outputs, |
| 95 | const ACameraCaptureSession_stateCallbacks* cb, |
Jayant Chowdhary | 6df2607 | 2018-11-06 23:55:12 -0800 | [diff] [blame] | 96 | android::acam::CameraDevice* device) : |
Yin-Chia Yeh | ead9146 | 2016-01-06 16:45:08 -0800 | [diff] [blame] | 97 | mId(id), mOutput(*outputs), mUserSessionCallback(*cb), |
| 98 | mDevice(device) {} |
Avichal Rakesh | f099b23 | 2022-10-27 15:44:50 -0700 | [diff] [blame] | 99 | #endif |
Yin-Chia Yeh | ead9146 | 2016-01-06 16:45:08 -0800 | [diff] [blame] | 100 | |
| 101 | // This can be called in app calling close() or after some app callback is finished |
| 102 | // Make sure the caller does not hold device or session lock! |
| 103 | ~ACameraCaptureSession(); |
| 104 | |
| 105 | // No API except Session_Close will work if device is closed |
| 106 | // A session will enter closed state when one of the following happens: |
| 107 | // 1. Explicitly closed by app |
| 108 | // 2. Replaced by a newer session |
| 109 | // 3. Device is closed |
| 110 | bool isClosed() { Mutex::Autolock _l(mSessionLock); return mIsClosed; } |
| 111 | |
| 112 | // Close the session and mark app no longer need this session. |
| 113 | void closeByApp(); |
| 114 | |
| 115 | camera_status_t stopRepeating(); |
| 116 | |
Yin-Chia Yeh | 309d05d | 2016-03-28 10:15:31 -0700 | [diff] [blame] | 117 | camera_status_t abortCaptures(); |
| 118 | |
Shuzhen Wang | 0ff9ae3 | 2018-12-05 18:06:12 -0800 | [diff] [blame] | 119 | template<class T> |
Yin-Chia Yeh | ead9146 | 2016-01-06 16:45:08 -0800 | [diff] [blame] | 120 | camera_status_t setRepeatingRequest( |
Shuzhen Wang | 0ff9ae3 | 2018-12-05 18:06:12 -0800 | [diff] [blame] | 121 | /*optional*/T* cbs, |
Yin-Chia Yeh | ead9146 | 2016-01-06 16:45:08 -0800 | [diff] [blame] | 122 | int numRequests, ACaptureRequest** requests, |
| 123 | /*optional*/int* captureSequenceId); |
| 124 | |
Shuzhen Wang | 0ff9ae3 | 2018-12-05 18:06:12 -0800 | [diff] [blame] | 125 | template<class T> |
Yin-Chia Yeh | ead9146 | 2016-01-06 16:45:08 -0800 | [diff] [blame] | 126 | camera_status_t capture( |
Shuzhen Wang | 0ff9ae3 | 2018-12-05 18:06:12 -0800 | [diff] [blame] | 127 | /*optional*/T* cbs, |
Yin-Chia Yeh | ead9146 | 2016-01-06 16:45:08 -0800 | [diff] [blame] | 128 | int numRequests, ACaptureRequest** requests, |
| 129 | /*optional*/int* captureSequenceId); |
| 130 | |
Emilian Peev | 40ead60 | 2017-09-26 15:46:36 +0100 | [diff] [blame] | 131 | camera_status_t updateOutputConfiguration(ACaptureSessionOutput *output); |
| 132 | |
Yin-Chia Yeh | ead9146 | 2016-01-06 16:45:08 -0800 | [diff] [blame] | 133 | ACameraDevice* getDevice(); |
| 134 | |
| 135 | private: |
Jayant Chowdhary | 6df2607 | 2018-11-06 23:55:12 -0800 | [diff] [blame] | 136 | friend class android::acam::CameraDevice; |
Yin-Chia Yeh | ead9146 | 2016-01-06 16:45:08 -0800 | [diff] [blame] | 137 | |
| 138 | // Close session because app close camera device, camera device got ERROR_DISCONNECTED, |
| 139 | // or a new session is replacing this session. |
| 140 | void closeByDevice(); |
| 141 | |
Avichal Rakesh | f099b23 | 2022-10-27 15:44:50 -0700 | [diff] [blame] | 142 | #ifdef __ANDROID_VNDK__ |
| 143 | std::shared_ptr<android::acam::CameraDevice> getDevicePtr(); |
| 144 | #else |
Jayant Chowdhary | 6df2607 | 2018-11-06 23:55:12 -0800 | [diff] [blame] | 145 | sp<android::acam::CameraDevice> getDeviceSp(); |
Avichal Rakesh | f099b23 | 2022-10-27 15:44:50 -0700 | [diff] [blame] | 146 | #endif |
Yin-Chia Yeh | ead9146 | 2016-01-06 16:45:08 -0800 | [diff] [blame] | 147 | |
| 148 | const int mId; |
| 149 | const ACaptureSessionOutputContainer mOutput; |
| 150 | const ACameraCaptureSession_stateCallbacks mUserSessionCallback; |
Avichal Rakesh | f099b23 | 2022-10-27 15:44:50 -0700 | [diff] [blame] | 151 | #ifdef __ANDROID_VNDK__ |
| 152 | const std::weak_ptr<android::acam::CameraDevice> mDevice; |
| 153 | #else |
Jayant Chowdhary | 6df2607 | 2018-11-06 23:55:12 -0800 | [diff] [blame] | 154 | const wp<android::acam::CameraDevice> mDevice; |
Avichal Rakesh | f099b23 | 2022-10-27 15:44:50 -0700 | [diff] [blame] | 155 | #endif |
| 156 | |
Yin-Chia Yeh | ead9146 | 2016-01-06 16:45:08 -0800 | [diff] [blame] | 157 | bool mIsClosed = false; |
Yin-Chia Yeh | 085dd09 | 2016-03-02 14:16:31 -0800 | [diff] [blame] | 158 | bool mClosedByApp = false; |
Yin-Chia Yeh | ead9146 | 2016-01-06 16:45:08 -0800 | [diff] [blame] | 159 | Mutex mSessionLock; |
| 160 | }; |
| 161 | |
| 162 | #endif // _ACAMERA_CAPTURE_SESSION_H |