blob: 68db233d2924295ae1e78a79e44b708b70a27f40 [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);
Jayant Chowdhary6df26072018-11-06 23:55:12 -080026 sp<acam::CameraDevice> dev = getDeviceSp();
Yin-Chia Yehead91462016-01-06 16:45:08 -080027 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 Chowdhary9da975d2019-08-14 15:00:24 -070036 if (mUserSessionCallback.onClosed != nullptr) {
37 (*mUserSessionCallback.onClosed)(mUserSessionCallback.context, this);
38 }
Yin-Chia Yehead91462016-01-06 16:45:08 -080039 ALOGV("~ACameraCaptureSession: %p is deleted", this);
40}
41
42void
43ACameraCaptureSession::closeByApp() {
Yin-Chia Yeh085dd092016-03-02 14:16:31 -080044 {
45 Mutex::Autolock _l(mSessionLock);
46 if (mClosedByApp) {
47 // Do not close twice
48 return;
49 }
50 mClosedByApp = true;
51 }
52
Jayant Chowdhary6df26072018-11-06 23:55:12 -080053 sp<acam::CameraDevice> dev = getDeviceSp();
Yin-Chia Yehead91462016-01-06 16:45:08 -080054 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
76camera_status_t
77ACameraCaptureSession::stopRepeating() {
Jayant Chowdhary6df26072018-11-06 23:55:12 -080078 sp<acam::CameraDevice> dev = getDeviceSp();
Yin-Chia Yehead91462016-01-06 16:45:08 -080079 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
94camera_status_t
Yin-Chia Yeh309d05d2016-03-28 10:15:31 -070095ACameraCaptureSession::abortCaptures() {
Jayant Chowdhary6df26072018-11-06 23:55:12 -080096 sp<acam::CameraDevice> dev = getDeviceSp();
Yin-Chia Yeh309d05d2016-03-28 10:15:31 -070097 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 Peev40ead602017-09-26 15:46:36 +0100112camera_status_t ACameraCaptureSession::updateOutputConfiguration(ACaptureSessionOutput *output) {
Jayant Chowdhary6df26072018-11-06 23:55:12 -0800113 sp<acam::CameraDevice> dev = getDeviceSp();
Emilian Peev40ead602017-09-26 15:46:36 +0100114 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 Yeh4dfa4cc2017-11-10 20:00:09 -0800123 ret = dev->updateOutputConfigurationLocked(output);
Emilian Peev40ead602017-09-26 15:46:36 +0100124 }
125 dev->unlockDevice();
126 return ret;
127}
128
Yin-Chia Yehead91462016-01-06 16:45:08 -0800129ACameraDevice*
130ACameraCaptureSession::getDevice() {
131 Mutex::Autolock _l(mSessionLock);
Jayant Chowdhary6df26072018-11-06 23:55:12 -0800132 sp<acam::CameraDevice> dev = getDeviceSp();
Yin-Chia Yehead91462016-01-06 16:45:08 -0800133 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
140void
141ACameraCaptureSession::closeByDevice() {
142 Mutex::Autolock _l(mSessionLock);
143 mIsClosed = true;
144}
145
Jayant Chowdhary6df26072018-11-06 23:55:12 -0800146sp<acam::CameraDevice>
Yin-Chia Yehead91462016-01-06 16:45:08 -0800147ACameraCaptureSession::getDeviceSp() {
Jayant Chowdhary6df26072018-11-06 23:55:12 -0800148 sp<acam::CameraDevice> device = mDevice.promote();
Yin-Chia Yehead91462016-01-06 16:45:08 -0800149 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