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 "NdkCameraDevice"
19#define ATRACE_TAG ATRACE_TAG_CAMERA
20
21#include <utils/Log.h>
22#include <utils/Trace.h>
23
24#include <NdkCameraDevice.h>
25#include "impl/ACameraCaptureSession.h"
26
27using namespace android;
28
29EXPORT
30camera_status_t ACameraDevice_close(ACameraDevice* device) {
31    ATRACE_CALL();
32    if (device == nullptr) {
33        ALOGE("%s: invalid argument! device is null", __FUNCTION__);
34        return ACAMERA_ERROR_INVALID_PARAMETER;
35    }
36    delete device;
37    return ACAMERA_OK;
38}
39
40EXPORT
41const char* ACameraDevice_getId(const ACameraDevice* device) {
42    ATRACE_CALL();
43    if (device == nullptr) {
44        ALOGE("%s: invalid argument! device is null", __FUNCTION__);
45        return nullptr;
46    }
47    return device->getId();
48}
49
50EXPORT
51camera_status_t ACameraDevice_createCaptureRequest(
52        const ACameraDevice* device,
53        ACameraDevice_request_template templateId,
54        ACaptureRequest** request) {
55    ATRACE_CALL();
56    if (device == nullptr || request == nullptr) {
57        ALOGE("%s: invalid argument! device %p request %p",
58                __FUNCTION__, device, request);
59        return ACAMERA_ERROR_INVALID_PARAMETER;
60    }
61    switch (templateId) {
62        case TEMPLATE_PREVIEW:
63        case TEMPLATE_STILL_CAPTURE:
64        case TEMPLATE_RECORD:
65        case TEMPLATE_VIDEO_SNAPSHOT:
66        case TEMPLATE_ZERO_SHUTTER_LAG:
67        case TEMPLATE_MANUAL:
68            break;
69        default:
70            ALOGE("%s: unknown template ID %d", __FUNCTION__, templateId);
71            return ACAMERA_ERROR_INVALID_PARAMETER;
72    }
73    return device->createCaptureRequest(templateId, request);
74}
75
76EXPORT
77camera_status_t ACaptureSessionOutputContainer_create(
78        /*out*/ACaptureSessionOutputContainer** out) {
79    ATRACE_CALL();
80    if (out == nullptr) {
81        ALOGE("%s: Error: out null", __FUNCTION__);
82        return ACAMERA_ERROR_INVALID_PARAMETER;
83    }
84    *out = new ACaptureSessionOutputContainer();
85    return ACAMERA_OK;
86}
87
88EXPORT
89void ACaptureSessionOutputContainer_free(ACaptureSessionOutputContainer* container) {
90    ATRACE_CALL();
91    if (container != nullptr) {
92        delete container;
93    }
94    return;
95}
96
97EXPORT
98camera_status_t ACaptureSessionOutput_create(
99        ANativeWindow* window, /*out*/ACaptureSessionOutput** out) {
100    ATRACE_CALL();
101    if (window == nullptr || out == nullptr) {
102        ALOGE("%s: Error: bad argument. window %p, out %p",
103                __FUNCTION__, window, out);
104        return ACAMERA_ERROR_INVALID_PARAMETER;
105    }
106    *out = new ACaptureSessionOutput(window);
107    return ACAMERA_OK;
108}
109
110EXPORT
111void ACaptureSessionOutput_free(ACaptureSessionOutput* output) {
112    ATRACE_CALL();
113    if (output != nullptr) {
114        delete output;
115    }
116    return;
117}
118
119EXPORT
120camera_status_t ACaptureSessionOutputContainer_add(
121        ACaptureSessionOutputContainer* container, const ACaptureSessionOutput* output) {
122    ATRACE_CALL();
123    if (container == nullptr || output == nullptr) {
124        ALOGE("%s: Error: invalid input: container %p, output %p",
125                __FUNCTION__, container, output);
126        return ACAMERA_ERROR_INVALID_PARAMETER;
127    }
128    auto pair = container->mOutputs.insert(*output);
129    if (!pair.second) {
130        ALOGW("%s: output %p already exists!", __FUNCTION__, output);
131    }
132    return ACAMERA_OK;
133}
134
135EXPORT
136camera_status_t ACaptureSessionOutputContainer_remove(
137        ACaptureSessionOutputContainer* container, const ACaptureSessionOutput* output) {
138    ATRACE_CALL();
139    if (container == nullptr || output == nullptr) {
140        ALOGE("%s: Error: invalid input: container %p, output %p",
141                __FUNCTION__, container, output);
142        return ACAMERA_ERROR_INVALID_PARAMETER;
143    }
144    container->mOutputs.erase(*output);
145    return ACAMERA_OK;
146}
147
148EXPORT
149camera_status_t ACameraDevice_createCaptureSession(
150        ACameraDevice* device,
151        const ACaptureSessionOutputContainer*       outputs,
152        const ACameraCaptureSession_stateCallbacks* callbacks,
153        /*out*/ACameraCaptureSession** session) {
154    ATRACE_CALL();
155    if (device == nullptr || outputs == nullptr || callbacks == nullptr || session == nullptr) {
156        ALOGE("%s: Error: invalid input: device %p, outputs %p, callbacks %p, session %p",
157                __FUNCTION__, device, outputs, callbacks, session);
158        return ACAMERA_ERROR_INVALID_PARAMETER;
159    }
160    return device->createCaptureSession(outputs, callbacks, session);
161}
162