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 "NdkCameraManager"
19 #define ATRACE_TAG ATRACE_TAG_CAMERA
20 
21 #include <utils/Log.h>
22 #include <utils/Trace.h>
23 
24 #include <camera/NdkCameraManager.h>
25 
26 #ifdef __ANDROID_VNDK__
27 #include "ndk_vendor/impl/ACameraManager.h"
28 #else
29 #include "impl/ACameraManager.h"
30 #endif
31 #include "impl/ACameraMetadata.h"
32 
33 using namespace android::acam;
34 
35 EXPORT
ACameraManager_create()36 ACameraManager* ACameraManager_create() {
37     ATRACE_CALL();
38     return new ACameraManager();
39 }
40 
41 EXPORT
ACameraManager_delete(ACameraManager * manager)42 void ACameraManager_delete(ACameraManager* manager) {
43     ATRACE_CALL();
44     if (manager != nullptr) {
45         delete manager;
46     }
47 }
48 
49 EXPORT
ACameraManager_getCameraIdList(ACameraManager * manager,ACameraIdList ** cameraIdList)50 camera_status_t ACameraManager_getCameraIdList(
51         ACameraManager* manager, ACameraIdList** cameraIdList) {
52     ATRACE_CALL();
53     if (manager == nullptr || cameraIdList == nullptr) {
54         ALOGE("%s: invalid argument! manager %p, cameraIdList %p",
55               __FUNCTION__, manager, cameraIdList);
56         return ACAMERA_ERROR_INVALID_PARAMETER;
57     }
58     return manager->getCameraIdList(cameraIdList);
59 }
60 
61 EXPORT
ACameraManager_deleteCameraIdList(ACameraIdList * cameraIdList)62 void ACameraManager_deleteCameraIdList(ACameraIdList* cameraIdList) {
63     ATRACE_CALL();
64     if (cameraIdList != nullptr) {
65         ACameraManager::deleteCameraIdList(cameraIdList);
66     }
67 }
68 
69 EXPORT
ACameraManager_registerAvailabilityCallback(ACameraManager *,const ACameraManager_AvailabilityCallbacks * callback)70 camera_status_t ACameraManager_registerAvailabilityCallback(
71         ACameraManager*, const ACameraManager_AvailabilityCallbacks *callback) {
72     ATRACE_CALL();
73     if (callback == nullptr) {
74         ALOGE("%s: invalid argument! callback is null!", __FUNCTION__);
75         return ACAMERA_ERROR_INVALID_PARAMETER;
76     }
77     if (callback->onCameraAvailable == nullptr || callback->onCameraUnavailable == nullptr) {
78         ALOGE("%s: invalid argument! callback %p, "
79                 "onCameraAvailable %p, onCameraUnavailable %p",
80                __FUNCTION__, callback,
81                callback->onCameraAvailable, callback->onCameraUnavailable);
82         return ACAMERA_ERROR_INVALID_PARAMETER;
83     }
84     CameraManagerGlobal::getInstance().registerAvailabilityCallback(callback);
85     return ACAMERA_OK;
86 }
87 
88 EXPORT
ACameraManager_unregisterAvailabilityCallback(ACameraManager *,const ACameraManager_AvailabilityCallbacks * callback)89 camera_status_t ACameraManager_unregisterAvailabilityCallback(
90         ACameraManager*, const ACameraManager_AvailabilityCallbacks *callback) {
91     ATRACE_CALL();
92     if (callback == nullptr) {
93         ALOGE("%s: invalid argument! callback is null!", __FUNCTION__);
94         return ACAMERA_ERROR_INVALID_PARAMETER;
95     }
96     if (callback->onCameraAvailable == nullptr || callback->onCameraUnavailable == nullptr) {
97         ALOGE("%s: invalid argument! callback %p, "
98                 "onCameraAvailable %p, onCameraUnavailable %p",
99                __FUNCTION__, callback,
100                callback->onCameraAvailable, callback->onCameraUnavailable);
101         return ACAMERA_ERROR_INVALID_PARAMETER;
102     }
103     CameraManagerGlobal::getInstance().unregisterAvailabilityCallback(callback);
104     return ACAMERA_OK;
105 }
106 
107 EXPORT
ACameraManager_registerExtendedAvailabilityCallback(ACameraManager *,const ACameraManager_ExtendedAvailabilityCallbacks * callback)108 camera_status_t ACameraManager_registerExtendedAvailabilityCallback(
109         ACameraManager* /*manager*/, const ACameraManager_ExtendedAvailabilityCallbacks *callback) {
110     ATRACE_CALL();
111     if (callback == nullptr) {
112         ALOGE("%s: invalid argument! callback is null!", __FUNCTION__);
113         return ACAMERA_ERROR_INVALID_PARAMETER;
114     }
115     if ((callback->availabilityCallbacks.onCameraAvailable == nullptr) ||
116             (callback->availabilityCallbacks.onCameraUnavailable == nullptr) ||
117             (callback->onCameraAccessPrioritiesChanged == nullptr)) {
118         ALOGE("%s: invalid argument! callback %p, "
119                 "onCameraAvailable %p, onCameraUnavailable %p onCameraAccessPrioritiesChanged %p",
120                __FUNCTION__, callback,
121                callback->availabilityCallbacks.onCameraAvailable,
122                callback->availabilityCallbacks.onCameraUnavailable,
123                callback->onCameraAccessPrioritiesChanged);
124         return ACAMERA_ERROR_INVALID_PARAMETER;
125     }
126     auto reservedEntriesCount = sizeof(callback->reserved) / sizeof(callback->reserved[0]);
127     for (size_t i = 0; i < reservedEntriesCount; i++) {
128         if (callback->reserved[i] != nullptr) {
129             ALOGE("%s: invalid argument! callback reserved entries must be set to NULL",
130                     __FUNCTION__);
131             return ACAMERA_ERROR_INVALID_PARAMETER;
132         }
133     }
134     CameraManagerGlobal::getInstance().registerExtendedAvailabilityCallback(callback);
135     return ACAMERA_OK;
136 }
137 
138 EXPORT
ACameraManager_unregisterExtendedAvailabilityCallback(ACameraManager *,const ACameraManager_ExtendedAvailabilityCallbacks * callback)139 camera_status_t ACameraManager_unregisterExtendedAvailabilityCallback(
140         ACameraManager* /*manager*/, const ACameraManager_ExtendedAvailabilityCallbacks *callback) {
141     ATRACE_CALL();
142     if (callback == nullptr) {
143         ALOGE("%s: invalid argument! callback is null!", __FUNCTION__);
144         return ACAMERA_ERROR_INVALID_PARAMETER;
145     }
146     if ((callback->availabilityCallbacks.onCameraAvailable == nullptr) ||
147             (callback->availabilityCallbacks.onCameraUnavailable == nullptr) ||
148             (callback->onCameraAccessPrioritiesChanged == nullptr)) {
149         ALOGE("%s: invalid argument! callback %p, "
150                 "onCameraAvailable %p, onCameraUnavailable %p onCameraAccessPrioritiesChanged %p",
151                __FUNCTION__, callback,
152                callback->availabilityCallbacks.onCameraAvailable,
153                callback->availabilityCallbacks.onCameraUnavailable,
154                callback->onCameraAccessPrioritiesChanged);
155         return ACAMERA_ERROR_INVALID_PARAMETER;
156     }
157     CameraManagerGlobal::getInstance().unregisterExtendedAvailabilityCallback(callback);
158     return ACAMERA_OK;
159 }
160 
161 EXPORT
ACameraManager_getCameraCharacteristics(ACameraManager * mgr,const char * cameraId,ACameraMetadata ** chars)162 camera_status_t ACameraManager_getCameraCharacteristics(
163         ACameraManager* mgr, const char* cameraId, ACameraMetadata** chars){
164     ATRACE_CALL();
165     if (mgr == nullptr || cameraId == nullptr || chars == nullptr) {
166         ALOGE("%s: invalid argument! mgr %p cameraId %p chars %p",
167                 __FUNCTION__, mgr, cameraId, chars);
168         return ACAMERA_ERROR_INVALID_PARAMETER;
169     }
170     sp<ACameraMetadata> spChars;
171     camera_status_t status = mgr->getCameraCharacteristics(cameraId, &spChars);
172     if (status != ACAMERA_OK) {
173         return status;
174     }
175     spChars->incStrong((void*) ACameraManager_getCameraCharacteristics);
176     *chars = spChars.get();
177     return ACAMERA_OK;
178 }
179 
180 EXPORT
ACameraManager_openCamera(ACameraManager * mgr,const char * cameraId,ACameraDevice_StateCallbacks * callback,ACameraDevice ** device)181 camera_status_t ACameraManager_openCamera(
182         ACameraManager* mgr, const char* cameraId,
183         ACameraDevice_StateCallbacks* callback,
184         /*out*/ACameraDevice** device) {
185     ATRACE_CALL();
186     if (mgr == nullptr || cameraId == nullptr || callback == nullptr || device == nullptr) {
187         ALOGE("%s: invalid argument! mgr %p cameraId %p callback %p device %p",
188                 __FUNCTION__, mgr, cameraId, callback, device);
189         return ACAMERA_ERROR_INVALID_PARAMETER;
190     }
191     return mgr->openCamera(cameraId, callback, device);
192 }
193 
194 #ifdef __ANDROID_VNDK__
195 EXPORT
ACameraManager_getTagFromName(ACameraManager * mgr,const char * cameraId,const char * name,uint32_t * tag)196 camera_status_t ACameraManager_getTagFromName(ACameraManager *mgr, const char* cameraId,
197         const char *name, /*out*/uint32_t *tag) {
198     ATRACE_CALL();
199     if (mgr == nullptr || cameraId == nullptr || name == nullptr) {
200         ALOGE("%s: invalid argument! mgr %p cameraId %p name %p",
201                 __FUNCTION__, mgr, cameraId, name);
202         return ACAMERA_ERROR_INVALID_PARAMETER;
203     }
204     return mgr->getTagFromName(cameraId, name, tag);
205 }
206 #endif
207