1 /*
2  * Copyright (C) 2015 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 "NdkCameraCaptureSession"
19 #define ATRACE_TAG ATRACE_TAG_CAMERA
20 
21 #include <utils/Log.h>
22 #include <utils/Mutex.h>
23 #include <utils/StrongPointer.h>
24 #include <utils/Trace.h>
25 
26 #include <camera/NdkCameraDevice.h>
27 #include <camera/NdkCaptureRequest.h>
28 #include <camera/NdkCameraCaptureSession.h>
29 #include "impl/ACameraCaptureSession.h"
30 
31 #include "impl/ACameraCaptureSession.inc"
32 
33 using namespace android;
34 
35 EXPORT
ACameraCaptureSession_close(ACameraCaptureSession * session)36 void ACameraCaptureSession_close(ACameraCaptureSession* session) {
37     ATRACE_CALL();
38     if (session != nullptr) {
39         session->closeByApp();
40     }
41     return;
42 }
43 
44 EXPORT
ACameraCaptureSession_getDevice(ACameraCaptureSession * session,ACameraDevice ** device)45 camera_status_t ACameraCaptureSession_getDevice(
46         ACameraCaptureSession* session, ACameraDevice **device) {
47     ATRACE_CALL();
48     if (session == nullptr || device == nullptr) {
49         ALOGE("%s: Error: invalid input: session %p, device %p",
50                 __FUNCTION__, session, device);
51         return ACAMERA_ERROR_INVALID_PARAMETER;
52     }
53 
54     if (session->isClosed()) {
55         ALOGE("%s: session %p is already closed", __FUNCTION__, session);
56         *device = nullptr;
57         return ACAMERA_ERROR_SESSION_CLOSED;
58     }
59 
60     *device = session->getDevice();
61     if (*device == nullptr) {
62         // Should not reach here
63         ALOGE("%s: unknown failure: device is null", __FUNCTION__);
64         return ACAMERA_ERROR_UNKNOWN;
65     }
66     return ACAMERA_OK;
67 }
68 
69 EXPORT
ACameraCaptureSession_capture(ACameraCaptureSession * session,ACameraCaptureSession_captureCallbacks * cbs,int numRequests,ACaptureRequest ** requests,int * captureSequenceId)70 camera_status_t ACameraCaptureSession_capture(
71         ACameraCaptureSession* session, /*optional*/ACameraCaptureSession_captureCallbacks* cbs,
72         int numRequests, ACaptureRequest** requests,
73         /*optional*/int* captureSequenceId) {
74     ATRACE_CALL();
75     if (session == nullptr || requests == nullptr || numRequests < 1) {
76         ALOGE("%s: Error: invalid input: session %p, numRequest %d, requests %p",
77                 __FUNCTION__, session, numRequests, requests);
78         return ACAMERA_ERROR_INVALID_PARAMETER;
79     }
80 
81     if (session->isClosed()) {
82         ALOGE("%s: session %p is already closed", __FUNCTION__, session);
83         if (captureSequenceId != nullptr) {
84             *captureSequenceId = CAPTURE_SEQUENCE_ID_NONE;
85         }
86         return ACAMERA_ERROR_SESSION_CLOSED;
87     }
88 
89     return session->capture(
90             cbs, numRequests, requests, captureSequenceId);
91 }
92 
93 EXPORT
ACameraCaptureSession_logicalCamera_capture(ACameraCaptureSession * session,ACameraCaptureSession_logicalCamera_captureCallbacks * lcbs,int numRequests,ACaptureRequest ** requests,int * captureSequenceId)94 camera_status_t ACameraCaptureSession_logicalCamera_capture(
95         ACameraCaptureSession* session,
96         /*optional*/ACameraCaptureSession_logicalCamera_captureCallbacks* lcbs,
97         int numRequests, ACaptureRequest** requests,
98         /*optional*/int* captureSequenceId) {
99     ATRACE_CALL();
100     if (session == nullptr || requests == nullptr || numRequests < 1) {
101         ALOGE("%s: Error: invalid input: session %p, numRequest %d, requests %p",
102                 __FUNCTION__, session, numRequests, requests);
103         return ACAMERA_ERROR_INVALID_PARAMETER;
104     }
105 
106     if (session->isClosed()) {
107         ALOGE("%s: session %p is already closed", __FUNCTION__, session);
108         if (captureSequenceId) {
109             *captureSequenceId = CAPTURE_SEQUENCE_ID_NONE;
110         }
111         return ACAMERA_ERROR_SESSION_CLOSED;
112     }
113 
114     return session->capture(
115             lcbs, numRequests, requests, captureSequenceId);
116 }
117 
118 EXPORT
ACameraCaptureSession_setRepeatingRequest(ACameraCaptureSession * session,ACameraCaptureSession_captureCallbacks * cbs,int numRequests,ACaptureRequest ** requests,int * captureSequenceId)119 camera_status_t ACameraCaptureSession_setRepeatingRequest(
120         ACameraCaptureSession* session, /*optional*/ACameraCaptureSession_captureCallbacks* cbs,
121         int numRequests, ACaptureRequest** requests,
122         /*optional*/int* captureSequenceId) {
123     ATRACE_CALL();
124     if (session == nullptr || requests == nullptr || numRequests < 1) {
125         ALOGE("%s: Error: invalid input: session %p, numRequest %d, requests %p",
126                 __FUNCTION__, session, numRequests, requests);
127         return ACAMERA_ERROR_INVALID_PARAMETER;
128     }
129 
130     if (session->isClosed()) {
131         ALOGE("%s: session %p is already closed", __FUNCTION__, session);
132         if (captureSequenceId) {
133             *captureSequenceId = CAPTURE_SEQUENCE_ID_NONE;
134         }
135         return ACAMERA_ERROR_SESSION_CLOSED;
136     }
137 
138     return session->setRepeatingRequest(cbs, numRequests, requests, captureSequenceId);
139 }
140 
141 EXPORT
ACameraCaptureSession_logicalCamera_setRepeatingRequest(ACameraCaptureSession * session,ACameraCaptureSession_logicalCamera_captureCallbacks * lcbs,int numRequests,ACaptureRequest ** requests,int * captureSequenceId)142 camera_status_t ACameraCaptureSession_logicalCamera_setRepeatingRequest(
143         ACameraCaptureSession* session,
144         /*optional*/ACameraCaptureSession_logicalCamera_captureCallbacks* lcbs,
145         int numRequests, ACaptureRequest** requests,
146         /*optional*/int* captureSequenceId) {
147     ATRACE_CALL();
148     if (session == nullptr || requests == nullptr || numRequests < 1) {
149         ALOGE("%s: Error: invalid input: session %p, numRequest %d, requests %p",
150                 __FUNCTION__, session, numRequests, requests);
151         return ACAMERA_ERROR_INVALID_PARAMETER;
152     }
153 
154     if (session->isClosed()) {
155         ALOGE("%s: session %p is already closed", __FUNCTION__, session);
156         if (captureSequenceId) {
157             *captureSequenceId = CAPTURE_SEQUENCE_ID_NONE;
158         }
159         return ACAMERA_ERROR_SESSION_CLOSED;
160     }
161 
162     return session->setRepeatingRequest(lcbs, numRequests, requests, captureSequenceId);
163 }
164 
165 EXPORT
ACameraCaptureSession_stopRepeating(ACameraCaptureSession * session)166 camera_status_t ACameraCaptureSession_stopRepeating(ACameraCaptureSession* session) {
167     ATRACE_CALL();
168     if (session == nullptr) {
169         ALOGE("%s: Error: session is null", __FUNCTION__);
170         return ACAMERA_ERROR_INVALID_PARAMETER;
171     }
172 
173     if (session->isClosed()) {
174         ALOGE("%s: session %p is already closed", __FUNCTION__, session);
175         return ACAMERA_ERROR_SESSION_CLOSED;
176     }
177     return session->stopRepeating();
178 }
179 
180 EXPORT
ACameraCaptureSession_abortCaptures(ACameraCaptureSession * session)181 camera_status_t ACameraCaptureSession_abortCaptures(ACameraCaptureSession* session) {
182     ATRACE_CALL();
183     if (session == nullptr) {
184         ALOGE("%s: Error: session is null", __FUNCTION__);
185         return ACAMERA_ERROR_INVALID_PARAMETER;
186     }
187 
188     if (session->isClosed()) {
189         ALOGE("%s: session %p is already closed", __FUNCTION__, session);
190         return ACAMERA_ERROR_SESSION_CLOSED;
191     }
192     return session->abortCaptures();
193 }
194 
195 EXPORT
ACameraCaptureSession_updateSharedOutput(ACameraCaptureSession * session,ACaptureSessionOutput * output)196 camera_status_t ACameraCaptureSession_updateSharedOutput(ACameraCaptureSession* session,
197         ACaptureSessionOutput* output) {
198     ATRACE_CALL();
199     if (session == nullptr) {
200         ALOGE("%s: Error: session is null", __FUNCTION__);
201         return ACAMERA_ERROR_INVALID_PARAMETER;
202     }
203 
204     if (session->isClosed()) {
205         ALOGE("%s: session %p is already closed", __FUNCTION__, session);
206         return ACAMERA_ERROR_SESSION_CLOSED;
207     }
208     return session->updateOutputConfiguration(output);
209 }
210