blob: 110d47aa79b57f264a214eb577cddb2a25576d61 [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
Yin-Chia Yehead91462016-01-06 16:45:08 -0800149ACameraDevice*
150ACameraCaptureSession::getDevice() {
151 Mutex::Autolock _l(mSessionLock);
Avichal Rakeshf099b232022-10-27 15:44:50 -0700152#ifdef __ANDROID_VNDK__
153 std::shared_ptr<acam::CameraDevice> dev = getDevicePtr();
154#else
Jayant Chowdhary6df26072018-11-06 23:55:12 -0800155 sp<acam::CameraDevice> dev = getDeviceSp();
Avichal Rakeshf099b232022-10-27 15:44:50 -0700156#endif
Yin-Chia Yehead91462016-01-06 16:45:08 -0800157 if (dev == nullptr) {
158 ALOGE("Error: Device associated with session %p has been closed!", this);
159 return nullptr;
160 }
161 return dev->getWrapper();
162}
163
164void
165ACameraCaptureSession::closeByDevice() {
166 Mutex::Autolock _l(mSessionLock);
167 mIsClosed = true;
168}
169
Avichal Rakeshf099b232022-10-27 15:44:50 -0700170#ifdef __ANDROID_VNDK__
171std::shared_ptr<acam::CameraDevice>
172ACameraCaptureSession::getDevicePtr() {
173 std::shared_ptr<acam::CameraDevice> device = mDevice.lock();
174 if (device == nullptr || device->isClosed()) {
175 ALOGW("Device is closed but session %d is not notified", mId);
176 return nullptr;
177 }
178 return device;
179}
180#else
Jayant Chowdhary6df26072018-11-06 23:55:12 -0800181sp<acam::CameraDevice>
Yin-Chia Yehead91462016-01-06 16:45:08 -0800182ACameraCaptureSession::getDeviceSp() {
Jayant Chowdhary6df26072018-11-06 23:55:12 -0800183 sp<acam::CameraDevice> device = mDevice.promote();
Yin-Chia Yehead91462016-01-06 16:45:08 -0800184 if (device == nullptr || device->isClosed()) {
185 ALOGW("Device is closed but session %d is not notified", mId);
186 return nullptr;
187 }
188 return device;
189}
Avichal Rakeshf099b232022-10-27 15:44:50 -0700190#endif