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 
~ACameraCaptureSession()24 ACameraCaptureSession::~ACameraCaptureSession() {
25     ALOGV("~ACameraCaptureSession: %p notify device end of life", this);
26     sp<acam::CameraDevice> dev = getDeviceSp();
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     if (mUserSessionCallback.onClosed != nullptr) {
37         (*mUserSessionCallback.onClosed)(mUserSessionCallback.context, this);
38     }
39     ALOGV("~ACameraCaptureSession: %p is deleted", this);
40 }
41 
42 void
closeByApp()43 ACameraCaptureSession::closeByApp() {
44     {
45         Mutex::Autolock _l(mSessionLock);
46         if (mClosedByApp) {
47             // Do not close twice
48             return;
49         }
50         mClosedByApp = true;
51     }
52 
53     sp<acam::CameraDevice> dev = getDeviceSp();
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
stopRepeating()77 ACameraCaptureSession::stopRepeating() {
78     sp<acam::CameraDevice> dev = getDeviceSp();
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
abortCaptures()95 ACameraCaptureSession::abortCaptures() {
96     sp<acam::CameraDevice> dev = getDeviceSp();
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 
updateOutputConfiguration(ACaptureSessionOutput * output)112 camera_status_t ACameraCaptureSession::updateOutputConfiguration(ACaptureSessionOutput *output) {
113     sp<acam::CameraDevice> dev = getDeviceSp();
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);
123         ret = dev->updateOutputConfigurationLocked(output);
124     }
125     dev->unlockDevice();
126     return ret;
127 }
128 
129 ACameraDevice*
getDevice()130 ACameraCaptureSession::getDevice() {
131     Mutex::Autolock _l(mSessionLock);
132     sp<acam::CameraDevice> dev = getDeviceSp();
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
closeByDevice()141 ACameraCaptureSession::closeByDevice() {
142     Mutex::Autolock _l(mSessionLock);
143     mIsClosed = true;
144 }
145 
146 sp<acam::CameraDevice>
getDeviceSp()147 ACameraCaptureSession::getDeviceSp() {
148     sp<acam::CameraDevice> device = mDevice.promote();
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 
157