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 | |
| 17 | //#define LOG_NDEBUG 0 |
| 18 | #define LOG_TAG "ACameraCaptureSession" |
| 19 | |
| 20 | #include "ACameraCaptureSession.h" |
| 21 | |
| 22 | using namespace android; |
| 23 | |
| 24 | ACameraCaptureSession::~ACameraCaptureSession() { |
| 25 | ALOGV("~ACameraCaptureSession: %p notify device end of life", this); |
Avichal Rakesh | f099b23 | 2022-10-27 15:44:50 -0700 | [diff] [blame] | 26 | #ifdef __ANDROID_VNDK__ |
| 27 | std::shared_ptr<acam::CameraDevice> dev = getDevicePtr(); |
| 28 | #else |
Jayant Chowdhary | 6df2607 | 2018-11-06 23:55:12 -0800 | [diff] [blame] | 29 | sp<acam::CameraDevice> dev = getDeviceSp(); |
Avichal Rakesh | f099b23 | 2022-10-27 15:44:50 -0700 | [diff] [blame] | 30 | #endif |
Yin-Chia Yeh | ead9146 | 2016-01-06 16:45:08 -0800 | [diff] [blame] | 31 | if (dev != nullptr && !dev->isClosed()) { |
| 32 | dev->lockDeviceForSessionOps(); |
| 33 | { |
| 34 | Mutex::Autolock _l(mSessionLock); |
| 35 | dev->notifySessionEndOfLifeLocked(this); |
| 36 | } |
| 37 | dev->unlockDevice(); |
| 38 | } |
| 39 | // Fire onClosed callback |
Jayant Chowdhary | 9da975d | 2019-08-14 15:00:24 -0700 | [diff] [blame] | 40 | if (mUserSessionCallback.onClosed != nullptr) { |
| 41 | (*mUserSessionCallback.onClosed)(mUserSessionCallback.context, this); |
| 42 | } |
Yin-Chia Yeh | ead9146 | 2016-01-06 16:45:08 -0800 | [diff] [blame] | 43 | ALOGV("~ACameraCaptureSession: %p is deleted", this); |
| 44 | } |
| 45 | |
| 46 | void |
| 47 | ACameraCaptureSession::closeByApp() { |
Yin-Chia Yeh | 085dd09 | 2016-03-02 14:16:31 -0800 | [diff] [blame] | 48 | { |
| 49 | Mutex::Autolock _l(mSessionLock); |
| 50 | if (mClosedByApp) { |
| 51 | // Do not close twice |
| 52 | return; |
| 53 | } |
| 54 | mClosedByApp = true; |
| 55 | } |
| 56 | |
Avichal Rakesh | f099b23 | 2022-10-27 15:44:50 -0700 | [diff] [blame] | 57 | #ifdef __ANDROID_VNDK__ |
| 58 | std::shared_ptr<acam::CameraDevice> dev = getDevicePtr(); |
| 59 | #else |
Jayant Chowdhary | 6df2607 | 2018-11-06 23:55:12 -0800 | [diff] [blame] | 60 | sp<acam::CameraDevice> dev = getDeviceSp(); |
Avichal Rakesh | f099b23 | 2022-10-27 15:44:50 -0700 | [diff] [blame] | 61 | #endif |
Yin-Chia Yeh | ead9146 | 2016-01-06 16:45:08 -0800 | [diff] [blame] | 62 | if (dev != nullptr) { |
| 63 | dev->lockDeviceForSessionOps(); |
| 64 | } |
| 65 | |
| 66 | { |
| 67 | Mutex::Autolock _l(mSessionLock); |
| 68 | |
| 69 | if (!mIsClosed && dev != nullptr) { |
| 70 | camera_status_t ret = dev->stopRepeatingLocked(); |
| 71 | if (ret != ACAMERA_OK) { |
| 72 | ALOGE("Stop repeating request failed while closing session %p", this); |
| 73 | } |
| 74 | } |
| 75 | mIsClosed = true; |
| 76 | } |
| 77 | |
| 78 | if (dev != nullptr) { |
| 79 | dev->unlockDevice(); |
| 80 | } |
| 81 | this->decStrong((void*) ACameraDevice_createCaptureSession); |
| 82 | } |
| 83 | |
| 84 | camera_status_t |
| 85 | ACameraCaptureSession::stopRepeating() { |
Avichal Rakesh | f099b23 | 2022-10-27 15:44:50 -0700 | [diff] [blame] | 86 | #ifdef __ANDROID_VNDK__ |
| 87 | std::shared_ptr<acam::CameraDevice> dev = getDevicePtr(); |
| 88 | #else |
Jayant Chowdhary | 6df2607 | 2018-11-06 23:55:12 -0800 | [diff] [blame] | 89 | sp<acam::CameraDevice> dev = getDeviceSp(); |
Avichal Rakesh | f099b23 | 2022-10-27 15:44:50 -0700 | [diff] [blame] | 90 | #endif |
Yin-Chia Yeh | ead9146 | 2016-01-06 16:45:08 -0800 | [diff] [blame] | 91 | if (dev == nullptr) { |
| 92 | ALOGE("Error: Device associated with session %p has been closed!", this); |
| 93 | return ACAMERA_ERROR_SESSION_CLOSED; |
| 94 | } |
| 95 | |
| 96 | camera_status_t ret; |
| 97 | dev->lockDeviceForSessionOps(); |
| 98 | { |
| 99 | Mutex::Autolock _l(mSessionLock); |
| 100 | ret = dev->stopRepeatingLocked(); |
| 101 | } |
| 102 | dev->unlockDevice(); |
| 103 | return ret; |
| 104 | } |
| 105 | |
| 106 | camera_status_t |
Yin-Chia Yeh | 309d05d | 2016-03-28 10:15:31 -0700 | [diff] [blame] | 107 | ACameraCaptureSession::abortCaptures() { |
Avichal Rakesh | f099b23 | 2022-10-27 15:44:50 -0700 | [diff] [blame] | 108 | #ifdef __ANDROID_VNDK__ |
| 109 | std::shared_ptr<acam::CameraDevice> dev = getDevicePtr(); |
| 110 | #else |
Jayant Chowdhary | 6df2607 | 2018-11-06 23:55:12 -0800 | [diff] [blame] | 111 | sp<acam::CameraDevice> dev = getDeviceSp(); |
Avichal Rakesh | f099b23 | 2022-10-27 15:44:50 -0700 | [diff] [blame] | 112 | #endif |
Yin-Chia Yeh | 309d05d | 2016-03-28 10:15:31 -0700 | [diff] [blame] | 113 | if (dev == nullptr) { |
| 114 | ALOGE("Error: Device associated with session %p has been closed!", this); |
| 115 | return ACAMERA_ERROR_SESSION_CLOSED; |
| 116 | } |
| 117 | |
| 118 | camera_status_t ret; |
| 119 | dev->lockDeviceForSessionOps(); |
| 120 | { |
| 121 | Mutex::Autolock _l(mSessionLock); |
| 122 | ret = dev->flushLocked(this); |
| 123 | } |
| 124 | dev->unlockDevice(); |
| 125 | return ret; |
| 126 | } |
| 127 | |
Emilian Peev | 40ead60 | 2017-09-26 15:46:36 +0100 | [diff] [blame] | 128 | camera_status_t ACameraCaptureSession::updateOutputConfiguration(ACaptureSessionOutput *output) { |
Avichal Rakesh | f099b23 | 2022-10-27 15:44:50 -0700 | [diff] [blame] | 129 | #ifdef __ANDROID_VNDK__ |
| 130 | std::shared_ptr<acam::CameraDevice> dev = getDevicePtr(); |
| 131 | #else |
Jayant Chowdhary | 6df2607 | 2018-11-06 23:55:12 -0800 | [diff] [blame] | 132 | sp<acam::CameraDevice> dev = getDeviceSp(); |
Avichal Rakesh | f099b23 | 2022-10-27 15:44:50 -0700 | [diff] [blame] | 133 | #endif |
Emilian Peev | 40ead60 | 2017-09-26 15:46:36 +0100 | [diff] [blame] | 134 | if (dev == nullptr) { |
| 135 | ALOGE("Error: Device associated with session %p has been closed!", this); |
| 136 | return ACAMERA_ERROR_SESSION_CLOSED; |
| 137 | } |
| 138 | |
| 139 | camera_status_t ret; |
| 140 | dev->lockDeviceForSessionOps(); |
| 141 | { |
| 142 | Mutex::Autolock _l(mSessionLock); |
Yin-Chia Yeh | 4dfa4cc | 2017-11-10 20:00:09 -0800 | [diff] [blame] | 143 | ret = dev->updateOutputConfigurationLocked(output); |
Emilian Peev | 40ead60 | 2017-09-26 15:46:36 +0100 | [diff] [blame] | 144 | } |
| 145 | dev->unlockDevice(); |
| 146 | return ret; |
| 147 | } |
| 148 | |
Avichal Rakesh | 8effe98 | 2023-11-13 18:53:40 -0800 | [diff] [blame^] | 149 | camera_status_t ACameraCaptureSession::prepare(ANativeWindow* window) { |
Jayant Chowdhary | 09b368b | 2023-02-13 06:53:05 +0000 | [diff] [blame] | 150 | #ifdef __ANDROID_VNDK__ |
| 151 | std::shared_ptr<acam::CameraDevice> dev = getDevicePtr(); |
| 152 | #else |
| 153 | sp<acam::CameraDevice> dev = getDeviceSp(); |
| 154 | #endif |
| 155 | if (dev == nullptr) { |
| 156 | ALOGE("Error: Device associated with session %p has been closed!", this); |
| 157 | return ACAMERA_ERROR_SESSION_CLOSED; |
| 158 | } |
| 159 | |
| 160 | camera_status_t ret; |
| 161 | dev->lockDeviceForSessionOps(); |
| 162 | { |
| 163 | Mutex::Autolock _l(mSessionLock); |
| 164 | ret = dev->prepareLocked(window); |
| 165 | } |
| 166 | dev->unlockDevice(); |
| 167 | return ret; |
| 168 | } |
| 169 | |
Yin-Chia Yeh | ead9146 | 2016-01-06 16:45:08 -0800 | [diff] [blame] | 170 | ACameraDevice* |
| 171 | ACameraCaptureSession::getDevice() { |
| 172 | Mutex::Autolock _l(mSessionLock); |
Avichal Rakesh | f099b23 | 2022-10-27 15:44:50 -0700 | [diff] [blame] | 173 | #ifdef __ANDROID_VNDK__ |
| 174 | std::shared_ptr<acam::CameraDevice> dev = getDevicePtr(); |
| 175 | #else |
Jayant Chowdhary | 6df2607 | 2018-11-06 23:55:12 -0800 | [diff] [blame] | 176 | sp<acam::CameraDevice> dev = getDeviceSp(); |
Avichal Rakesh | f099b23 | 2022-10-27 15:44:50 -0700 | [diff] [blame] | 177 | #endif |
Yin-Chia Yeh | ead9146 | 2016-01-06 16:45:08 -0800 | [diff] [blame] | 178 | if (dev == nullptr) { |
| 179 | ALOGE("Error: Device associated with session %p has been closed!", this); |
| 180 | return nullptr; |
| 181 | } |
| 182 | return dev->getWrapper(); |
| 183 | } |
| 184 | |
| 185 | void |
| 186 | ACameraCaptureSession::closeByDevice() { |
| 187 | Mutex::Autolock _l(mSessionLock); |
| 188 | mIsClosed = true; |
| 189 | } |
| 190 | |
Avichal Rakesh | f099b23 | 2022-10-27 15:44:50 -0700 | [diff] [blame] | 191 | #ifdef __ANDROID_VNDK__ |
| 192 | std::shared_ptr<acam::CameraDevice> |
| 193 | ACameraCaptureSession::getDevicePtr() { |
| 194 | std::shared_ptr<acam::CameraDevice> device = mDevice.lock(); |
| 195 | if (device == nullptr || device->isClosed()) { |
| 196 | ALOGW("Device is closed but session %d is not notified", mId); |
| 197 | return nullptr; |
| 198 | } |
| 199 | return device; |
| 200 | } |
| 201 | #else |
Jayant Chowdhary | 6df2607 | 2018-11-06 23:55:12 -0800 | [diff] [blame] | 202 | sp<acam::CameraDevice> |
Yin-Chia Yeh | ead9146 | 2016-01-06 16:45:08 -0800 | [diff] [blame] | 203 | ACameraCaptureSession::getDeviceSp() { |
Jayant Chowdhary | 6df2607 | 2018-11-06 23:55:12 -0800 | [diff] [blame] | 204 | sp<acam::CameraDevice> device = mDevice.promote(); |
Yin-Chia Yeh | ead9146 | 2016-01-06 16:45:08 -0800 | [diff] [blame] | 205 | if (device == nullptr || device->isClosed()) { |
| 206 | ALOGW("Device is closed but session %d is not notified", mId); |
| 207 | return nullptr; |
| 208 | } |
| 209 | return device; |
| 210 | } |
Avichal Rakesh | f099b23 | 2022-10-27 15:44:50 -0700 | [diff] [blame] | 211 | #endif |