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