blob: 449c0b4b1aab0b05a2d49d972fbb6d85834205ff [file] [log] [blame]
Yin-Chia Yehead91462016-01-06 16:45:08 -08001/*
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
22using namespace android;
23
24ACameraCaptureSession::~ACameraCaptureSession() {
25 ALOGV("~ACameraCaptureSession: %p notify device end of life", this);
Avichal Rakeshf099b232022-10-27 15:44:50 -070026#ifdef __ANDROID_VNDK__
27 std::shared_ptr<acam::CameraDevice> dev = getDevicePtr();
28#else
Jayant Chowdhary6df26072018-11-06 23:55:12 -080029 sp<acam::CameraDevice> dev = getDeviceSp();
Avichal Rakeshf099b232022-10-27 15:44:50 -070030#endif
Yin-Chia Yehead91462016-01-06 16:45:08 -080031 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 Chowdhary9da975d2019-08-14 15:00:24 -070040 if (mUserSessionCallback.onClosed != nullptr) {
41 (*mUserSessionCallback.onClosed)(mUserSessionCallback.context, this);
42 }
Yin-Chia Yehead91462016-01-06 16:45:08 -080043 ALOGV("~ACameraCaptureSession: %p is deleted", this);
44}
45
46void
47ACameraCaptureSession::closeByApp() {
Yin-Chia Yeh085dd092016-03-02 14:16:31 -080048 {
49 Mutex::Autolock _l(mSessionLock);
50 if (mClosedByApp) {
51 // Do not close twice
52 return;
53 }
54 mClosedByApp = true;
55 }
56
Avichal Rakeshf099b232022-10-27 15:44:50 -070057#ifdef __ANDROID_VNDK__
58 std::shared_ptr<acam::CameraDevice> dev = getDevicePtr();
59#else
Jayant Chowdhary6df26072018-11-06 23:55:12 -080060 sp<acam::CameraDevice> dev = getDeviceSp();
Avichal Rakeshf099b232022-10-27 15:44:50 -070061#endif
Yin-Chia Yehead91462016-01-06 16:45:08 -080062 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
84camera_status_t
85ACameraCaptureSession::stopRepeating() {
Avichal Rakeshf099b232022-10-27 15:44:50 -070086#ifdef __ANDROID_VNDK__
87 std::shared_ptr<acam::CameraDevice> dev = getDevicePtr();
88#else
Jayant Chowdhary6df26072018-11-06 23:55:12 -080089 sp<acam::CameraDevice> dev = getDeviceSp();
Avichal Rakeshf099b232022-10-27 15:44:50 -070090#endif
Yin-Chia Yehead91462016-01-06 16:45:08 -080091 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
106camera_status_t
Yin-Chia Yeh309d05d2016-03-28 10:15:31 -0700107ACameraCaptureSession::abortCaptures() {
Avichal Rakeshf099b232022-10-27 15:44:50 -0700108#ifdef __ANDROID_VNDK__
109 std::shared_ptr<acam::CameraDevice> dev = getDevicePtr();
110#else
Jayant Chowdhary6df26072018-11-06 23:55:12 -0800111 sp<acam::CameraDevice> dev = getDeviceSp();
Avichal Rakeshf099b232022-10-27 15:44:50 -0700112#endif
Yin-Chia Yeh309d05d2016-03-28 10:15:31 -0700113 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 Peev40ead602017-09-26 15:46:36 +0100128camera_status_t ACameraCaptureSession::updateOutputConfiguration(ACaptureSessionOutput *output) {
Avichal Rakeshf099b232022-10-27 15:44:50 -0700129#ifdef __ANDROID_VNDK__
130 std::shared_ptr<acam::CameraDevice> dev = getDevicePtr();
131#else
Jayant Chowdhary6df26072018-11-06 23:55:12 -0800132 sp<acam::CameraDevice> dev = getDeviceSp();
Avichal Rakeshf099b232022-10-27 15:44:50 -0700133#endif
Emilian Peev40ead602017-09-26 15:46:36 +0100134 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 Yeh4dfa4cc2017-11-10 20:00:09 -0800143 ret = dev->updateOutputConfigurationLocked(output);
Emilian Peev40ead602017-09-26 15:46:36 +0100144 }
145 dev->unlockDevice();
146 return ret;
147}
148
Avichal Rakesh8effe982023-11-13 18:53:40 -0800149camera_status_t ACameraCaptureSession::prepare(ANativeWindow* window) {
Jayant Chowdhary09b368b2023-02-13 06:53:05 +0000150#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 Yehead91462016-01-06 16:45:08 -0800170ACameraDevice*
171ACameraCaptureSession::getDevice() {
172 Mutex::Autolock _l(mSessionLock);
Avichal Rakeshf099b232022-10-27 15:44:50 -0700173#ifdef __ANDROID_VNDK__
174 std::shared_ptr<acam::CameraDevice> dev = getDevicePtr();
175#else
Jayant Chowdhary6df26072018-11-06 23:55:12 -0800176 sp<acam::CameraDevice> dev = getDeviceSp();
Avichal Rakeshf099b232022-10-27 15:44:50 -0700177#endif
Yin-Chia Yehead91462016-01-06 16:45:08 -0800178 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
185void
186ACameraCaptureSession::closeByDevice() {
187 Mutex::Autolock _l(mSessionLock);
188 mIsClosed = true;
189}
190
Avichal Rakeshf099b232022-10-27 15:44:50 -0700191#ifdef __ANDROID_VNDK__
192std::shared_ptr<acam::CameraDevice>
193ACameraCaptureSession::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 Chowdhary6df26072018-11-06 23:55:12 -0800202sp<acam::CameraDevice>
Yin-Chia Yehead91462016-01-06 16:45:08 -0800203ACameraCaptureSession::getDeviceSp() {
Jayant Chowdhary6df26072018-11-06 23:55:12 -0800204 sp<acam::CameraDevice> device = mDevice.promote();
Yin-Chia Yehead91462016-01-06 16:45:08 -0800205 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 Rakeshf099b232022-10-27 15:44:50 -0700211#endif