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); |
Jayant Chowdhary | 6df2607 | 2018-11-06 23:55:12 -0800 | [diff] [blame] | 26 | sp<acam::CameraDevice> dev = getDeviceSp(); |
Yin-Chia Yeh | ead9146 | 2016-01-06 16:45:08 -0800 | [diff] [blame] | 27 | if (dev != nullptr && !dev->isClosed()) { |
| 28 | dev->lockDeviceForSessionOps(); |
| 29 | { |
| 30 | Mutex::Autolock _l(mSessionLock); |
| 31 | dev->notifySessionEndOfLifeLocked(this); |
| 32 | } |
| 33 | dev->unlockDevice(); |
| 34 | } |
| 35 | // Fire onClosed callback |
Jayant Chowdhary | 9da975d | 2019-08-14 15:00:24 -0700 | [diff] [blame] | 36 | if (mUserSessionCallback.onClosed != nullptr) { |
| 37 | (*mUserSessionCallback.onClosed)(mUserSessionCallback.context, this); |
| 38 | } |
Yin-Chia Yeh | ead9146 | 2016-01-06 16:45:08 -0800 | [diff] [blame] | 39 | ALOGV("~ACameraCaptureSession: %p is deleted", this); |
| 40 | } |
| 41 | |
| 42 | void |
| 43 | ACameraCaptureSession::closeByApp() { |
Yin-Chia Yeh | 085dd09 | 2016-03-02 14:16:31 -0800 | [diff] [blame] | 44 | { |
| 45 | Mutex::Autolock _l(mSessionLock); |
| 46 | if (mClosedByApp) { |
| 47 | // Do not close twice |
| 48 | return; |
| 49 | } |
| 50 | mClosedByApp = true; |
| 51 | } |
| 52 | |
Jayant Chowdhary | 6df2607 | 2018-11-06 23:55:12 -0800 | [diff] [blame] | 53 | sp<acam::CameraDevice> dev = getDeviceSp(); |
Yin-Chia Yeh | ead9146 | 2016-01-06 16:45:08 -0800 | [diff] [blame] | 54 | if (dev != nullptr) { |
| 55 | dev->lockDeviceForSessionOps(); |
| 56 | } |
| 57 | |
| 58 | { |
| 59 | Mutex::Autolock _l(mSessionLock); |
| 60 | |
| 61 | if (!mIsClosed && dev != nullptr) { |
| 62 | camera_status_t ret = dev->stopRepeatingLocked(); |
| 63 | if (ret != ACAMERA_OK) { |
| 64 | ALOGE("Stop repeating request failed while closing session %p", this); |
| 65 | } |
| 66 | } |
| 67 | mIsClosed = true; |
| 68 | } |
| 69 | |
| 70 | if (dev != nullptr) { |
| 71 | dev->unlockDevice(); |
| 72 | } |
| 73 | this->decStrong((void*) ACameraDevice_createCaptureSession); |
| 74 | } |
| 75 | |
| 76 | camera_status_t |
| 77 | ACameraCaptureSession::stopRepeating() { |
Jayant Chowdhary | 6df2607 | 2018-11-06 23:55:12 -0800 | [diff] [blame] | 78 | sp<acam::CameraDevice> dev = getDeviceSp(); |
Yin-Chia Yeh | ead9146 | 2016-01-06 16:45:08 -0800 | [diff] [blame] | 79 | if (dev == nullptr) { |
| 80 | ALOGE("Error: Device associated with session %p has been closed!", this); |
| 81 | return ACAMERA_ERROR_SESSION_CLOSED; |
| 82 | } |
| 83 | |
| 84 | camera_status_t ret; |
| 85 | dev->lockDeviceForSessionOps(); |
| 86 | { |
| 87 | Mutex::Autolock _l(mSessionLock); |
| 88 | ret = dev->stopRepeatingLocked(); |
| 89 | } |
| 90 | dev->unlockDevice(); |
| 91 | return ret; |
| 92 | } |
| 93 | |
| 94 | camera_status_t |
Yin-Chia Yeh | 309d05d | 2016-03-28 10:15:31 -0700 | [diff] [blame] | 95 | ACameraCaptureSession::abortCaptures() { |
Jayant Chowdhary | 6df2607 | 2018-11-06 23:55:12 -0800 | [diff] [blame] | 96 | sp<acam::CameraDevice> dev = getDeviceSp(); |
Yin-Chia Yeh | 309d05d | 2016-03-28 10:15:31 -0700 | [diff] [blame] | 97 | if (dev == nullptr) { |
| 98 | ALOGE("Error: Device associated with session %p has been closed!", this); |
| 99 | return ACAMERA_ERROR_SESSION_CLOSED; |
| 100 | } |
| 101 | |
| 102 | camera_status_t ret; |
| 103 | dev->lockDeviceForSessionOps(); |
| 104 | { |
| 105 | Mutex::Autolock _l(mSessionLock); |
| 106 | ret = dev->flushLocked(this); |
| 107 | } |
| 108 | dev->unlockDevice(); |
| 109 | return ret; |
| 110 | } |
| 111 | |
Emilian Peev | 40ead60 | 2017-09-26 15:46:36 +0100 | [diff] [blame] | 112 | camera_status_t ACameraCaptureSession::updateOutputConfiguration(ACaptureSessionOutput *output) { |
Jayant Chowdhary | 6df2607 | 2018-11-06 23:55:12 -0800 | [diff] [blame] | 113 | sp<acam::CameraDevice> dev = getDeviceSp(); |
Emilian Peev | 40ead60 | 2017-09-26 15:46:36 +0100 | [diff] [blame] | 114 | if (dev == nullptr) { |
| 115 | ALOGE("Error: Device associated with session %p has been closed!", this); |
| 116 | return ACAMERA_ERROR_SESSION_CLOSED; |
| 117 | } |
| 118 | |
| 119 | camera_status_t ret; |
| 120 | dev->lockDeviceForSessionOps(); |
| 121 | { |
| 122 | Mutex::Autolock _l(mSessionLock); |
Yin-Chia Yeh | 4dfa4cc | 2017-11-10 20:00:09 -0800 | [diff] [blame] | 123 | ret = dev->updateOutputConfigurationLocked(output); |
Emilian Peev | 40ead60 | 2017-09-26 15:46:36 +0100 | [diff] [blame] | 124 | } |
| 125 | dev->unlockDevice(); |
| 126 | return ret; |
| 127 | } |
| 128 | |
Yin-Chia Yeh | ead9146 | 2016-01-06 16:45:08 -0800 | [diff] [blame] | 129 | ACameraDevice* |
| 130 | ACameraCaptureSession::getDevice() { |
| 131 | Mutex::Autolock _l(mSessionLock); |
Jayant Chowdhary | 6df2607 | 2018-11-06 23:55:12 -0800 | [diff] [blame] | 132 | sp<acam::CameraDevice> dev = getDeviceSp(); |
Yin-Chia Yeh | ead9146 | 2016-01-06 16:45:08 -0800 | [diff] [blame] | 133 | if (dev == nullptr) { |
| 134 | ALOGE("Error: Device associated with session %p has been closed!", this); |
| 135 | return nullptr; |
| 136 | } |
| 137 | return dev->getWrapper(); |
| 138 | } |
| 139 | |
| 140 | void |
| 141 | ACameraCaptureSession::closeByDevice() { |
| 142 | Mutex::Autolock _l(mSessionLock); |
| 143 | mIsClosed = true; |
| 144 | } |
| 145 | |
Jayant Chowdhary | 6df2607 | 2018-11-06 23:55:12 -0800 | [diff] [blame] | 146 | sp<acam::CameraDevice> |
Yin-Chia Yeh | ead9146 | 2016-01-06 16:45:08 -0800 | [diff] [blame] | 147 | ACameraCaptureSession::getDeviceSp() { |
Jayant Chowdhary | 6df2607 | 2018-11-06 23:55:12 -0800 | [diff] [blame] | 148 | sp<acam::CameraDevice> device = mDevice.promote(); |
Yin-Chia Yeh | ead9146 | 2016-01-06 16:45:08 -0800 | [diff] [blame] | 149 | if (device == nullptr || device->isClosed()) { |
| 150 | ALOGW("Device is closed but session %d is not notified", mId); |
| 151 | return nullptr; |
| 152 | } |
| 153 | return device; |
| 154 | } |
| 155 | |
| 156 | |