• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 #ifdef __ANDROID_VNDK__
27     std::shared_ptr<acam::CameraDevice> dev = getDevicePtr();
28 #else
29     sp<acam::CameraDevice> dev = getDeviceSp();
30 #endif
31     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
40     if (mUserSessionCallback.onClosed != nullptr) {
41         (*mUserSessionCallback.onClosed)(mUserSessionCallback.context, this);
42     }
43     ALOGV("~ACameraCaptureSession: %p is deleted", this);
44 }
45 
46 void
closeByApp()47 ACameraCaptureSession::closeByApp() {
48     {
49         Mutex::Autolock _l(mSessionLock);
50         if (mClosedByApp) {
51             // Do not close twice
52             return;
53         }
54         mClosedByApp = true;
55     }
56 
57 #ifdef __ANDROID_VNDK__
58     std::shared_ptr<acam::CameraDevice> dev = getDevicePtr();
59 #else
60     sp<acam::CameraDevice> dev = getDeviceSp();
61 #endif
62     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 
84 camera_status_t
stopRepeating()85 ACameraCaptureSession::stopRepeating() {
86 #ifdef __ANDROID_VNDK__
87     std::shared_ptr<acam::CameraDevice> dev = getDevicePtr();
88 #else
89     sp<acam::CameraDevice> dev = getDeviceSp();
90 #endif
91     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         if (dev->isSharedMode() && !dev->isPrimaryClient()) {
100             dev->unlockDevice();
101             return ACAMERA_ERROR_UNSUPPORTED_OPERATION;
102         }
103         Mutex::Autolock _l(mSessionLock);
104         ret = dev->stopRepeatingLocked();
105     }
106     dev->unlockDevice();
107     return ret;
108 }
109 
stopStreaming()110 camera_status_t ACameraCaptureSession::stopStreaming() {
111 #ifdef __ANDROID_VNDK__
112     std::shared_ptr<acam::CameraDevice> dev = getDevicePtr();
113 #else
114     sp<acam::CameraDevice> dev = getDeviceSp();
115 #endif
116     if (dev == nullptr) {
117         ALOGE("Error: Device associated with session %p has been closed!", this);
118         return ACAMERA_ERROR_SESSION_CLOSED;
119     }
120 
121     camera_status_t ret;
122     dev->lockDeviceForSessionOps();
123     {
124         Mutex::Autolock _l(mSessionLock);
125         ret = dev->stopStreamingLocked();
126     }
127     dev->unlockDevice();
128     return ret;
129 }
130 
131 camera_status_t
abortCaptures()132 ACameraCaptureSession::abortCaptures() {
133 #ifdef __ANDROID_VNDK__
134     std::shared_ptr<acam::CameraDevice> dev = getDevicePtr();
135 #else
136     sp<acam::CameraDevice> dev = getDeviceSp();
137 #endif
138     if (dev == nullptr) {
139         ALOGE("Error: Device associated with session %p has been closed!", this);
140         return ACAMERA_ERROR_SESSION_CLOSED;
141     }
142     camera_status_t ret;
143     dev->lockDeviceForSessionOps();
144     {
145         if (dev->isSharedMode() && !dev->isPrimaryClient()) {
146             dev->unlockDevice();
147             return ACAMERA_ERROR_UNSUPPORTED_OPERATION;
148         }
149         Mutex::Autolock _l(mSessionLock);
150         ret = dev->flushLocked(this);
151     }
152     dev->unlockDevice();
153     return ret;
154 }
155 
updateOutputConfiguration(ACaptureSessionOutput * output)156 camera_status_t ACameraCaptureSession::updateOutputConfiguration(ACaptureSessionOutput *output) {
157 #ifdef __ANDROID_VNDK__
158     std::shared_ptr<acam::CameraDevice> dev = getDevicePtr();
159 #else
160     sp<acam::CameraDevice> dev = getDeviceSp();
161 #endif
162     if (dev == nullptr) {
163         ALOGE("Error: Device associated with session %p has been closed!", this);
164         return ACAMERA_ERROR_SESSION_CLOSED;
165     }
166 
167     camera_status_t ret;
168     dev->lockDeviceForSessionOps();
169     {
170         if (dev->isSharedMode()) {
171             dev->unlockDevice();
172             return ACAMERA_ERROR_UNSUPPORTED_OPERATION;
173         }
174         Mutex::Autolock _l(mSessionLock);
175         ret = dev->updateOutputConfigurationLocked(output);
176     }
177     dev->unlockDevice();
178     return ret;
179 }
180 
prepare(ANativeWindow * window)181 camera_status_t ACameraCaptureSession::prepare(ANativeWindow* window) {
182 #ifdef __ANDROID_VNDK__
183     std::shared_ptr<acam::CameraDevice> dev = getDevicePtr();
184 #else
185     sp<acam::CameraDevice> dev = getDeviceSp();
186 #endif
187     if (dev == nullptr) {
188         ALOGE("Error: Device associated with session %p has been closed!", this);
189         return ACAMERA_ERROR_SESSION_CLOSED;
190     }
191 
192     camera_status_t ret;
193     dev->lockDeviceForSessionOps();
194     {
195         if (dev->isSharedMode()) {
196             dev->unlockDevice();
197             return ACAMERA_ERROR_UNSUPPORTED_OPERATION;
198         }
199         Mutex::Autolock _l(mSessionLock);
200         ret = dev->prepareLocked(window);
201     }
202     dev->unlockDevice();
203     return ret;
204 }
205 
206 ACameraDevice*
getDevice()207 ACameraCaptureSession::getDevice() {
208     Mutex::Autolock _l(mSessionLock);
209 #ifdef __ANDROID_VNDK__
210     std::shared_ptr<acam::CameraDevice> dev = getDevicePtr();
211 #else
212     sp<acam::CameraDevice> dev = getDeviceSp();
213 #endif
214     if (dev == nullptr) {
215         ALOGE("Error: Device associated with session %p has been closed!", this);
216         return nullptr;
217     }
218     return dev->getWrapper();
219 }
220 
221 void
closeByDevice()222 ACameraCaptureSession::closeByDevice() {
223     Mutex::Autolock _l(mSessionLock);
224     mIsClosed = true;
225 }
226 
227 #ifdef __ANDROID_VNDK__
228 std::shared_ptr<acam::CameraDevice>
getDevicePtr()229 ACameraCaptureSession::getDevicePtr() {
230     std::shared_ptr<acam::CameraDevice> device = mDevice.lock();
231     if (device == nullptr || device->isClosed()) {
232         ALOGW("Device is closed but session %d is not notified", mId);
233         return nullptr;
234     }
235     return device;
236 }
237 #else
238 sp<acam::CameraDevice>
getDeviceSp()239 ACameraCaptureSession::getDeviceSp() {
240     sp<acam::CameraDevice> device = mDevice.promote();
241     if (device == nullptr || device->isClosed()) {
242         ALOGW("Device is closed but session %d is not notified", mId);
243         return nullptr;
244     }
245     return device;
246 }
247 #endif
248