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