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 "NdkCameraDevice.h"
27#include <NdkCaptureRequest.h>
28#include <NdkCameraCaptureSession.h>
29#include "impl/ACameraCaptureSession.h"
30
31using namespace android;
32
33EXPORT
34void ACameraCaptureSession_close(ACameraCaptureSession* session) {
35    ATRACE_CALL();
36    if (session != nullptr) {
37        session->closeByApp();
38    }
39    return;
40}
41
42EXPORT
43camera_status_t ACameraCaptureSession_getDevice(
44        ACameraCaptureSession* session, ACameraDevice **device) {
45    ATRACE_CALL();
46    if (session == nullptr || device == nullptr) {
47        ALOGE("%s: Error: invalid input: session %p, device %p",
48                __FUNCTION__, session, device);
49        return ACAMERA_ERROR_INVALID_PARAMETER;
50    }
51
52    if (session->isClosed()) {
53        ALOGE("%s: session %p is already closed", __FUNCTION__, session);
54        *device = nullptr;
55        return ACAMERA_ERROR_SESSION_CLOSED;
56    }
57
58    *device = session->getDevice();
59    if (*device == nullptr) {
60        // Should not reach here
61        ALOGE("%s: unknown failure: device is null", __FUNCTION__);
62        return ACAMERA_ERROR_UNKNOWN;
63    }
64    return ACAMERA_OK;
65}
66
67EXPORT
68camera_status_t ACameraCaptureSession_capture(
69        ACameraCaptureSession* session, /*optional*/ACameraCaptureSession_captureCallbacks* cbs,
70        int numRequests, ACaptureRequest** requests,
71        /*optional*/int* captureSequenceId) {
72    ATRACE_CALL();
73    if (session == nullptr || requests == nullptr || numRequests < 1) {
74        ALOGE("%s: Error: invalid input: session %p, numRequest %d, requests %p",
75                __FUNCTION__, session, numRequests, requests);
76        return ACAMERA_ERROR_INVALID_PARAMETER;
77    }
78
79    if (session->isClosed()) {
80        ALOGE("%s: session %p is already closed", __FUNCTION__, session);
81        *captureSequenceId = CAPTURE_SEQUENCE_ID_NONE;
82        return ACAMERA_ERROR_SESSION_CLOSED;
83    }
84
85    return session->capture(cbs, numRequests, requests, captureSequenceId);
86}
87
88EXPORT
89camera_status_t ACameraCaptureSession_setRepeatingRequest(
90        ACameraCaptureSession* session, /*optional*/ACameraCaptureSession_captureCallbacks* cbs,
91        int numRequests, ACaptureRequest** requests,
92        /*optional*/int* captureSequenceId) {
93    ATRACE_CALL();
94    if (session == nullptr || requests == nullptr || numRequests < 1) {
95        ALOGE("%s: Error: invalid input: session %p, numRequest %d, requests %p",
96                __FUNCTION__, session, numRequests, requests);
97        return ACAMERA_ERROR_INVALID_PARAMETER;
98    }
99
100    if (session->isClosed()) {
101        ALOGE("%s: session %p is already closed", __FUNCTION__, session);
102        *captureSequenceId = CAPTURE_SEQUENCE_ID_NONE;
103        return ACAMERA_ERROR_SESSION_CLOSED;
104    }
105
106    return session->setRepeatingRequest(cbs, numRequests, requests, captureSequenceId);
107}
108
109EXPORT
110camera_status_t ACameraCaptureSession_stopRepeating(ACameraCaptureSession* session) {
111    ATRACE_CALL();
112    if (session == nullptr) {
113        ALOGE("%s: Error: session is null", __FUNCTION__);
114        return ACAMERA_ERROR_INVALID_PARAMETER;
115    }
116
117    if (session->isClosed()) {
118        ALOGE("%s: session %p is already closed", __FUNCTION__, session);
119        return ACAMERA_ERROR_SESSION_CLOSED;
120    }
121    return session->stopRepeating();
122}
123
124EXPORT
125camera_status_t ACameraCaptureSession_abortCaptures(ACameraCaptureSession* session) {
126    ATRACE_CALL();
127    if (session == nullptr) {
128        ALOGE("%s: Error: session is null", __FUNCTION__);
129        return ACAMERA_ERROR_INVALID_PARAMETER;
130    }
131
132    if (session->isClosed()) {
133        ALOGE("%s: session %p is already closed", __FUNCTION__, session);
134        return ACAMERA_ERROR_SESSION_CLOSED;
135    }
136    return session->abortCaptures();
137}
138