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 /**
18  * @addtogroup Camera
19  * @{
20  */
21 
22 /**
23  * @file NdkCameraManager.h
24  */
25 
26 /*
27  * This file defines an NDK API.
28  * Do not remove methods.
29  * Do not change method signatures.
30  * Do not change the value of constants.
31  * Do not change the size of any of the classes defined in here.
32  * Do not reference types that are not part of the NDK.
33  * Do not #include files that aren't part of the NDK.
34  */
35 
36 #ifndef _NDK_CAMERA_MANAGER_H
37 #define _NDK_CAMERA_MANAGER_H
38 
39 #include <sys/cdefs.h>
40 
41 #include "NdkCameraError.h"
42 #include "NdkCameraMetadata.h"
43 #include "NdkCameraDevice.h"
44 
45 __BEGIN_DECLS
46 
47 #if __ANDROID_API__ >= 24
48 
49 /**
50  * ACameraManager is opaque type that provides access to camera service.
51  *
52  * A pointer can be obtained using {@link ACameraManager_create} method.
53  */
54 typedef struct ACameraManager ACameraManager;
55 
56 /**
57  * Create ACameraManager instance.
58  *
59  * <p>The ACameraManager is responsible for
60  * detecting, characterizing, and connecting to {@link ACameraDevice}s.</p>
61  *
62  * <p>The caller must call {@link ACameraManager_delete} to free the resources once it is done
63  * using the ACameraManager instance.</p>
64  *
65  * @return a {@link ACameraManager} instance.
66  *
67  */
68 ACameraManager* ACameraManager_create() __INTRODUCED_IN(24);
69 
70 /**
71  * <p>Delete the {@link ACameraManager} instance and free its resources. </p>
72  *
73  * @param manager the {@link ACameraManager} instance to be deleted.
74  */
75 void ACameraManager_delete(ACameraManager* manager) __INTRODUCED_IN(24);
76 
77 /**
78  * Create a list of currently connected camera devices, including
79  * cameras that may be in use by other camera API clients.
80  *
81  * <p>Non-removable cameras use integers starting at 0 for their
82  * identifiers, while removable cameras have a unique identifier for each
83  * individual device, even if they are the same model.</p>
84  *
85  * <p>ACameraManager_getCameraIdList will allocate and return an {@link ACameraIdList}.
86  * The caller must call {@link ACameraManager_deleteCameraIdList} to free the memory</p>
87  *
88  * <p>Note: the returned camera list might be a subset to the output of <a href=
89  * "https://developer.android.com/reference/android/hardware/camera2/CameraManager.html#getCameraIdList()">
90  * SDK CameraManager#getCameraIdList API</a> as the NDK API does not support some legacy camera
91  * hardware.</p>
92  *
93  * @param manager the {@link ACameraManager} of interest
94  * @param cameraIdList the output {@link ACameraIdList} will be filled in here if the method call
95  *        succeeds.
96  *
97  * @return <ul>
98  *         <li>{@link ACAMERA_OK} if the method call succeeds.</li>
99  *         <li>{@link ACAMERA_ERROR_INVALID_PARAMETER} if manager or cameraIdList is NULL.</li>
100  *         <li>{@link ACAMERA_ERROR_CAMERA_DISCONNECTED} if connection to camera service fails.</li>
101  *         <li>{@link ACAMERA_ERROR_NOT_ENOUGH_MEMORY} if allocating memory fails.</li></ul>
102  */
103 camera_status_t ACameraManager_getCameraIdList(ACameraManager* manager,
104         /*out*/ACameraIdList** cameraIdList) __INTRODUCED_IN(24);
105 
106 /**
107  * Delete a list of camera devices allocated via {@link ACameraManager_getCameraIdList}.
108  *
109  * @param cameraIdList the {@link ACameraIdList} to be deleted.
110  */
111 void ACameraManager_deleteCameraIdList(ACameraIdList* cameraIdList) __INTRODUCED_IN(24);
112 
113 /**
114  * Definition of camera availability callbacks.
115  *
116  * @param context The optional application context provided by user in
117  *                {@link ACameraManager_AvailabilityCallbacks}.
118  * @param cameraId The ID of the camera device whose availability is changing. The memory of this
119  *                 argument is owned by camera framework and will become invalid immediately after
120  *                 this callback returns.
121  */
122 typedef void (*ACameraManager_AvailabilityCallback)(void* context,
123         const char* cameraId);
124 
125 /**
126  * A listener for camera devices becoming available or unavailable to open.
127  *
128  * <p>Cameras become available when they are no longer in use, or when a new
129  * removable camera is connected. They become unavailable when some
130  * application or service starts using a camera, or when a removable camera
131  * is disconnected.</p>
132  *
133  * @see ACameraManager_registerAvailabilityCallback
134  */
135 typedef struct ACameraManager_AvailabilityListener {
136     /// Optional application context.
137     void*                               context;
138     /// Called when a camera becomes available
139     ACameraManager_AvailabilityCallback onCameraAvailable;
140     /// Called when a camera becomes unavailable
141     ACameraManager_AvailabilityCallback onCameraUnavailable;
142 } ACameraManager_AvailabilityCallbacks;
143 
144 /**
145  * Register camera availability callbacks.
146  *
147  * <p>onCameraUnavailable will be called whenever a camera device is opened by any camera API client.
148  * Other camera API clients may still be able to open such a camera device, evicting the existing
149  * client if they have higher priority than the existing client of a camera device.
150  * See {@link ACameraManager_openCamera} for more details.</p>
151  *
152  * <p>The callbacks will be called on a dedicated thread shared among all ACameraManager
153  * instances.</p>
154  *
155  * <p>Since this callback will be registered with the camera service, remember to unregister it
156  * once it is no longer needed; otherwise the callback will continue to receive events
157  * indefinitely and it may prevent other resources from being released. Specifically, the
158  * callbacks will be invoked independently of the general activity lifecycle and independently
159  * of the state of individual ACameraManager instances.</p>
160  *
161  * @param manager the {@link ACameraManager} of interest.
162  * @param callback the {@link ACameraManager_AvailabilityCallbacks} to be registered.
163  *
164  * @return <ul>
165  *         <li>{@link ACAMERA_OK} if the method call succeeds.</li>
166  *         <li>{@link ACAMERA_ERROR_INVALID_PARAMETER} if manager or callback is NULL, or
167  *                  {ACameraManager_AvailabilityCallbacks#onCameraAvailable} or
168  *                  {ACameraManager_AvailabilityCallbacks#onCameraUnavailable} is NULL.</li></ul>
169  */
170 camera_status_t ACameraManager_registerAvailabilityCallback(
171         ACameraManager* manager,
172         const ACameraManager_AvailabilityCallbacks* callback) __INTRODUCED_IN(24);
173 
174 /**
175  * Unregister camera availability callbacks.
176  *
177  * <p>Removing a callback that isn't registered has no effect.</p>
178  *
179  * @param manager the {@link ACameraManager} of interest.
180  * @param callback the {@link ACameraManager_AvailabilityCallbacks} to be unregistered.
181  *
182  * @return <ul>
183  *         <li>{@link ACAMERA_OK} if the method call succeeds.</li>
184  *         <li>{@link ACAMERA_ERROR_INVALID_PARAMETER} if callback,
185  *                  {ACameraManager_AvailabilityCallbacks#onCameraAvailable} or
186  *                  {ACameraManager_AvailabilityCallbacks#onCameraUnavailable} is NULL.</li></ul>
187  */
188 camera_status_t ACameraManager_unregisterAvailabilityCallback(
189         ACameraManager* manager,
190         const ACameraManager_AvailabilityCallbacks* callback) __INTRODUCED_IN(24);
191 
192 /**
193  * Query the capabilities of a camera device. These capabilities are
194  * immutable for a given camera.
195  *
196  * <p>See {@link ACameraMetadata} document and {@link NdkCameraMetadataTags.h} for more details.</p>
197  *
198  * <p>The caller must call {@link ACameraMetadata_free} to free the memory of the output
199  * characteristics.</p>
200  *
201  * @param manager the {@link ACameraManager} of interest.
202  * @param cameraId the ID string of the camera device of interest.
203  * @param characteristics the output {@link ACameraMetadata} will be filled here if the method call
204  *        succeeeds.
205  *
206  * @return <ul>
207  *         <li>{@link ACAMERA_OK} if the method call succeeds.</li>
208  *         <li>{@link ACAMERA_ERROR_INVALID_PARAMETER} if manager, cameraId, or characteristics
209  *                  is NULL, or cameraId does not match any camera devices connected.</li>
210  *         <li>{@link ACAMERA_ERROR_CAMERA_DISCONNECTED} if connection to camera service fails.</li>
211  *         <li>{@link ACAMERA_ERROR_NOT_ENOUGH_MEMORY} if allocating memory fails.</li>
212  *         <li>{@link ACAMERA_ERROR_UNKNOWN} if the method fails for some other reasons.</li></ul>
213  */
214 camera_status_t ACameraManager_getCameraCharacteristics(
215         ACameraManager* manager, const char* cameraId,
216         /*out*/ACameraMetadata** characteristics) __INTRODUCED_IN(24);
217 
218 /**
219  * Open a connection to a camera with the given ID. The opened camera device will be
220  * returned in the `device` parameter.
221  *
222  * <p>Use {@link ACameraManager_getCameraIdList} to get the list of available camera
223  * devices. Note that even if an id is listed, open may fail if the device
224  * is disconnected between the calls to {@link ACameraManager_getCameraIdList} and
225  * {@link ACameraManager_openCamera}, or if a higher-priority camera API client begins using the
226  * camera device.</p>
227  *
228  * <p>Devices for which the
229  * {@link ACameraManager_AvailabilityCallbacks#onCameraUnavailable} callback has been called due to
230  * the device being in use by a lower-priority, background camera API client can still potentially
231  * be opened by calling this method when the calling camera API client has a higher priority
232  * than the current camera API client using this device.  In general, if the top, foreground
233  * activity is running within your application process, your process will be given the highest
234  * priority when accessing the camera, and this method will succeed even if the camera device is
235  * in use by another camera API client. Any lower-priority application that loses control of the
236  * camera in this way will receive an
237  * {@link ACameraDevice_StateCallbacks#onDisconnected} callback.</p>
238  *
239  * <p>Once the camera is successfully opened,the ACameraDevice can then be set up
240  * for operation by calling {@link ACameraDevice_createCaptureSession} and
241  * {@link ACameraDevice_createCaptureRequest}.</p>
242  *
243  * <p>If the camera becomes disconnected after this function call returns,
244  * {@link ACameraDevice_StateCallbacks#onDisconnected} with a
245  * ACameraDevice in the disconnected state will be called.</p>
246  *
247  * <p>If the camera runs into error after this function call returns,
248  * {@link ACameraDevice_StateCallbacks#onError} with a
249  * ACameraDevice in the error state will be called.</p>
250  *
251  * @param manager the {@link ACameraManager} of interest.
252  * @param cameraId the ID string of the camera device to be opened.
253  * @param callback the {@link ACameraDevice_StateCallbacks} associated with the opened camera device.
254  * @param device the opened {@link ACameraDevice} will be filled here if the method call succeeds.
255  *
256  * @return <ul>
257  *         <li>{@link ACAMERA_OK} if the method call succeeds.</li>
258  *         <li>{@link ACAMERA_ERROR_INVALID_PARAMETER} if manager, cameraId, callback, or device
259  *                  is NULL, or cameraId does not match any camera devices connected.</li>
260  *         <li>{@link ACAMERA_ERROR_CAMERA_DISCONNECTED} if connection to camera service fails.</li>
261  *         <li>{@link ACAMERA_ERROR_NOT_ENOUGH_MEMORY} if allocating memory fails.</li>
262  *         <li>{@link ACAMERA_ERROR_CAMERA_IN_USE} if camera device is being used by a higher
263  *                   priority camera API client.</li>
264  *         <li>{@link ACAMERA_ERROR_MAX_CAMERA_IN_USE} if the system-wide limit for number of open
265  *                   cameras or camera resources has been reached, and more camera devices cannot be
266  *                   opened until previous instances are closed.</li>
267  *         <li>{@link ACAMERA_ERROR_CAMERA_DISABLED} if the camera is disabled due to a device
268  *                   policy, and cannot be opened.</li>
269  *         <li>{@link ACAMERA_ERROR_PERMISSION_DENIED} if the application does not have permission
270  *                   to open camera.</li>
271  *         <li>{@link ACAMERA_ERROR_UNKNOWN} if the method fails for some other reasons.</li></ul>
272  */
273 camera_status_t ACameraManager_openCamera(
274         ACameraManager* manager, const char* cameraId,
275         ACameraDevice_StateCallbacks* callback,
276         /*out*/ACameraDevice** device) __INTRODUCED_IN(24);
277 
278 #endif /* __ANDROID_API__ >= 24 */
279 
280 #if __ANDROID_API__ >= 29
281 
282 /**
283  * Definition of camera access permission change callback.
284  *
285  * <p>Notification that camera access priorities have changed and the camera may
286  * now be openable. An application that was previously denied camera access due to
287  * a higher-priority user already using the camera, or that was disconnected from an
288  * active camera session due to a higher-priority user trying to open the camera,
289  * should try to open the camera again if it still wants to use it.  Note that
290  * multiple applications may receive this callback at the same time, and only one of
291  * them will succeed in opening the camera in practice, depending on exact access
292  * priority levels and timing. This method is useful in cases where multiple
293  * applications may be in the resumed state at the same time, and the user switches
294  * focus between them, or if the current camera-using application moves between
295  * full-screen and Picture-in-Picture (PiP) states. In such cases, the camera
296  * available/unavailable callbacks will not be invoked, but another application may
297  * now have higher priority for camera access than the current camera-using
298  * application.</p>
299 
300  * @param context The optional application context provided by user in
301  *                {@link ACameraManager_AvailabilityListener}.
302  */
303 typedef void (*ACameraManager_AccessPrioritiesChangedCallback)(void* context);
304 
305 /**
306  * A listener for camera devices becoming available/unavailable to open or when
307  * the camera access permissions change.
308  *
309  * <p>Cameras become available when they are no longer in use, or when a new
310  * removable camera is connected. They become unavailable when some
311  * application or service starts using a camera, or when a removable camera
312  * is disconnected.</p>
313  *
314  * @see ACameraManager_registerExtendedAvailabilityCallback
315  */
316 typedef struct ACameraManager_ExtendedAvailabilityListener {
317     ///
318     ACameraManager_AvailabilityCallbacks availabilityCallbacks;
319 
320     /// Called when there is camera access permission change
321     ACameraManager_AccessPrioritiesChangedCallback onCameraAccessPrioritiesChanged;
322 
323     /// Reserved for future use, please ensure that all entries are set to NULL
324     void *reserved[6];
325 } ACameraManager_ExtendedAvailabilityCallbacks;
326 
327 /**
328  * Register camera extended availability callbacks.
329  *
330  * <p>onCameraUnavailable will be called whenever a camera device is opened by any camera API
331  * client. Other camera API clients may still be able to open such a camera device, evicting the
332  * existing client if they have higher priority than the existing client of a camera device.
333  * See {@link ACameraManager_openCamera} for more details.</p>
334  *
335  * <p>The callbacks will be called on a dedicated thread shared among all ACameraManager
336  * instances.</p>
337  *
338  * <p>Since this callback will be registered with the camera service, remember to unregister it
339  * once it is no longer needed; otherwise the callback will continue to receive events
340  * indefinitely and it may prevent other resources from being released. Specifically, the
341  * callbacks will be invoked independently of the general activity lifecycle and independently
342  * of the state of individual ACameraManager instances.</p>
343  *
344  * @param manager the {@link ACameraManager} of interest.
345  * @param callback the {@link ACameraManager_ExtendedAvailabilityCallbacks} to be registered.
346  *
347  * @return <ul>
348  *         <li>{@link ACAMERA_OK} if the method call succeeds.</li>
349  *         <li>{@link ACAMERA_ERROR_INVALID_PARAMETER} if manager or callback is NULL, or
350  *                  {ACameraManager_ExtendedAvailabilityCallbacks#onCameraAccessPrioritiesChanged}
351  *                  or {ACameraManager_AvailabilityCallbacks#onCameraAvailable} or
352  *                  {ACameraManager_AvailabilityCallbacks#onCameraUnavailable} is NULL.</li></ul>
353  */
354 camera_status_t ACameraManager_registerExtendedAvailabilityCallback(
355         ACameraManager* manager,
356         const ACameraManager_ExtendedAvailabilityCallbacks* callback) __INTRODUCED_IN(29);
357 
358 /**
359  * Unregister camera extended availability callbacks.
360  *
361  * <p>Removing a callback that isn't registered has no effect.</p>
362  *
363  * @param manager the {@link ACameraManager} of interest.
364  * @param callback the {@link ACameraManager_ExtendedAvailabilityCallbacks} to be unregistered.
365  *
366  * @return <ul>
367  *         <li>{@link ACAMERA_OK} if the method call succeeds.</li>
368  *         <li>{@link ACAMERA_ERROR_INVALID_PARAMETER} if callback,
369  *                  {ACameraManager_ExtendedAvailabilityCallbacks#onCameraAccessPrioritiesChanged}
370  *                  or {ACameraManager_AvailabilityCallbacks#onCameraAvailable} or
371  *                  {ACameraManager_AvailabilityCallbacks#onCameraUnavailable} is NULL.</li></ul>
372  */
373 camera_status_t ACameraManager_unregisterExtendedAvailabilityCallback(
374         ACameraManager* manager,
375         const ACameraManager_ExtendedAvailabilityCallbacks* callback) __INTRODUCED_IN(29);
376 
377 #ifdef __ANDROID_VNDK__
378 /**
379  * Retrieve the tag value, given the tag name and camera id.
380  * This method is device specific since some metadata might be defined by device manufacturers
381  * and might only be accessible for specific cameras.
382  * @param manager The {@link ACameraManager} of interest.
383  * @param cameraId The cameraId, which is used to query camera characteristics.
384  * @param name The name of the tag being queried.
385  * @param tag The output tag assigned by this method.
386  *
387  * @return ACAMERA_OK only if the function call was successful.
388  */
389 camera_status_t ACameraManager_getTagFromName(ACameraManager *manager, const char* cameraId,
390         const char *name, /*out*/uint32_t *tag)
391         __INTRODUCED_IN(29);
392 #endif
393 
394 #endif /* __ANDROID_API__ >= 29 */
395 
396 __END_DECLS
397 
398 #endif /* _NDK_CAMERA_MANAGER_H */
399 
400 /** @} */
401