ACameraCaptureSession.cpp revision 085dd0935c9cd19791a9a58a4df4a05e29eadca7
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 22using namespace android; 23 24ACameraCaptureSession::~ACameraCaptureSession() { 25 ALOGV("~ACameraCaptureSession: %p notify device end of life", this); 26 sp<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 (*mUserSessionCallback.onClosed)(mUserSessionCallback.context, this); 37 ALOGV("~ACameraCaptureSession: %p is deleted", this); 38} 39 40void 41ACameraCaptureSession::closeByApp() { 42 { 43 Mutex::Autolock _l(mSessionLock); 44 if (mClosedByApp) { 45 // Do not close twice 46 return; 47 } 48 mClosedByApp = true; 49 } 50 51 sp<CameraDevice> dev = getDeviceSp(); 52 if (dev != nullptr) { 53 dev->lockDeviceForSessionOps(); 54 } 55 56 { 57 Mutex::Autolock _l(mSessionLock); 58 59 if (!mIsClosed && dev != nullptr) { 60 camera_status_t ret = dev->stopRepeatingLocked(); 61 if (ret != ACAMERA_OK) { 62 ALOGE("Stop repeating request failed while closing session %p", this); 63 } 64 } 65 mIsClosed = true; 66 } 67 68 if (dev != nullptr) { 69 dev->unlockDevice(); 70 } 71 this->decStrong((void*) ACameraDevice_createCaptureSession); 72} 73 74camera_status_t 75ACameraCaptureSession::stopRepeating() { 76 sp<CameraDevice> dev = getDeviceSp(); 77 if (dev == nullptr) { 78 ALOGE("Error: Device associated with session %p has been closed!", this); 79 return ACAMERA_ERROR_SESSION_CLOSED; 80 } 81 82 camera_status_t ret; 83 dev->lockDeviceForSessionOps(); 84 { 85 Mutex::Autolock _l(mSessionLock); 86 ret = dev->stopRepeatingLocked(); 87 } 88 dev->unlockDevice(); 89 return ret; 90} 91 92camera_status_t 93ACameraCaptureSession::setRepeatingRequest( 94 /*optional*/ACameraCaptureSession_captureCallbacks* cbs, 95 int numRequests, ACaptureRequest** requests, 96 /*optional*/int* captureSequenceId) { 97 sp<CameraDevice> dev = getDeviceSp(); 98 if (dev == nullptr) { 99 ALOGE("Error: Device associated with session %p has been closed!", this); 100 return ACAMERA_ERROR_SESSION_CLOSED; 101 } 102 103 camera_status_t ret; 104 dev->lockDeviceForSessionOps(); 105 { 106 Mutex::Autolock _l(mSessionLock); 107 ret = dev->setRepeatingRequestsLocked( 108 this, cbs, numRequests, requests, captureSequenceId); 109 } 110 dev->unlockDevice(); 111 return ret; 112} 113 114camera_status_t ACameraCaptureSession::capture( 115 /*optional*/ACameraCaptureSession_captureCallbacks* cbs, 116 int numRequests, ACaptureRequest** requests, 117 /*optional*/int* captureSequenceId) { 118 sp<CameraDevice> dev = getDeviceSp(); 119 if (dev == nullptr) { 120 ALOGE("Error: Device associated with session %p has been closed!", this); 121 return ACAMERA_ERROR_SESSION_CLOSED; 122 } 123 camera_status_t ret; 124 dev->lockDeviceForSessionOps(); 125 { 126 Mutex::Autolock _l(mSessionLock); 127 ret = dev->captureLocked(this, cbs, numRequests, requests, captureSequenceId); 128 } 129 dev->unlockDevice(); 130 return ret; 131} 132 133ACameraDevice* 134ACameraCaptureSession::getDevice() { 135 Mutex::Autolock _l(mSessionLock); 136 sp<CameraDevice> dev = getDeviceSp(); 137 if (dev == nullptr) { 138 ALOGE("Error: Device associated with session %p has been closed!", this); 139 return nullptr; 140 } 141 return dev->getWrapper(); 142} 143 144void 145ACameraCaptureSession::closeByDevice() { 146 Mutex::Autolock _l(mSessionLock); 147 mIsClosed = true; 148} 149 150sp<CameraDevice> 151ACameraCaptureSession::getDeviceSp() { 152 sp<CameraDevice> device = mDevice.promote(); 153 if (device == nullptr || device->isClosed()) { 154 ALOGW("Device is closed but session %d is not notified", mId); 155 return nullptr; 156 } 157 return device; 158} 159 160 161