blob: 05d407b7883d6591a90102ccd508d7bb9f2fbdf1
1 | /* |
2 | * Copyright (C) 2011 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 "CameraHAL_Module " |
19 | |
20 | #include <utils/threads.h> |
21 | |
22 | #include "DebugUtils.h" |
23 | #include "CameraHal.h" |
24 | #ifdef AMLOGIC_VIRTUAL_CAMERA_SUPPORT |
25 | #include "VirtualCamHal.h" |
26 | #endif |
27 | |
28 | #include "CameraProperties.h" |
29 | #include "ExCameraParameters.h" |
30 | |
31 | static android::CameraProperties gCameraProperties; |
32 | #ifdef AMLOGIC_VIRTUAL_CAMERA_SUPPORT |
33 | static android::CameraHal* gCameraHals[MAX_CAM_NUM_ADD_VCAM-1]; |
34 | static android::VirtualCamHal* gVCameraHals; |
35 | #else |
36 | static android::CameraHal* gCameraHals[MAX_CAMERAS_SUPPORTED]; |
37 | #endif |
38 | static unsigned int gCamerasOpen = 0; |
39 | static unsigned int gCamerasSupported = 0; |
40 | static android::Mutex gCameraHalDeviceLock; |
41 | |
42 | static int camera_device_open(const hw_module_t* module, const char* name, |
43 | hw_device_t** device); |
44 | static int camera_device_close(hw_device_t* device); |
45 | static int camera_get_number_of_cameras(void); |
46 | static int camera_get_camera_info(int camera_id, struct camera_info *info); |
47 | |
48 | #ifdef CAMHAL_USER_MODE |
49 | volatile int32_t gCamHal_LogLevel = 4; |
50 | #else |
51 | volatile int32_t gCamHal_LogLevel = 6; |
52 | #endif |
53 | static void setLogLevel(void *p){ |
54 | int level = (int) p; |
55 | android_atomic_write(level, &gCamHal_LogLevel); |
56 | } |
57 | |
58 | static const char *macro_info[]={ |
59 | #ifdef CAMHAL_USER_MODE |
60 | "user mode", |
61 | #endif |
62 | #ifdef AMLOGIC_FRONT_CAMERA_SUPPORT |
63 | "front board camera", |
64 | #endif |
65 | #ifdef AMLOGIC_BACK_CAMERA_SUPPORT |
66 | "back board camera", |
67 | #endif |
68 | #ifdef AMLOGIC_USB_CAMERA_SUPPORT |
69 | "usb camera", |
70 | #endif |
71 | #ifdef AMLOGIC_TWO_CH_UVC |
72 | "usb is two channel", |
73 | #endif |
74 | #ifdef AMLOGIC_VIRTUAL_CAMERA_SUPPORT |
75 | "virtual camera enable", |
76 | #endif |
77 | #ifdef AMLOGIC_CAMERA_NONBLOCK_SUPPORT |
78 | "nonblock mode", |
79 | #endif |
80 | }; |
81 | |
82 | |
83 | static struct hw_module_methods_t camera_module_methods = { |
84 | open: camera_device_open |
85 | }; |
86 | |
87 | camera_module_t HAL_MODULE_INFO_SYM = { |
88 | common: { |
89 | tag: HARDWARE_MODULE_TAG, |
90 | version_major: 1, |
91 | version_minor: 0, |
92 | id: CAMERA_HARDWARE_MODULE_ID, |
93 | name: "CameraHal Module", |
94 | author: "Amlogic", |
95 | methods: &camera_module_methods, |
96 | dso: NULL, /* remove compilation warnings */ |
97 | reserved: {0}, /* remove compilation warnings */ |
98 | }, |
99 | get_number_of_cameras: camera_get_number_of_cameras, |
100 | get_camera_info: camera_get_camera_info, |
101 | set_callbacks: NULL, |
102 | get_vendor_tag_ops: NULL, |
103 | open_legacy: NULL, |
104 | reserved: {0}, |
105 | }; |
106 | |
107 | typedef struct aml_camera_device { |
108 | camera_device_t base; |
109 | int cameraid; |
110 | #ifdef AMLOGIC_VIRTUAL_CAMERA_SUPPORT |
111 | int type; |
112 | #endif |
113 | } aml_camera_device_t; |
114 | |
115 | /******************************************************************* |
116 | * implementation of camera_device_ops functions |
117 | *******************************************************************/ |
118 | |
119 | int camera_set_preview_window(struct camera_device * device, |
120 | struct preview_stream_ops *window) |
121 | { |
122 | int rv = -EINVAL; |
123 | aml_camera_device_t* aml_dev = NULL; |
124 | |
125 | LOG_FUNCTION_NAME; |
126 | |
127 | if(!device) |
128 | return rv; |
129 | |
130 | aml_dev = (aml_camera_device_t*) device; |
131 | |
132 | #ifdef AMLOGIC_VIRTUAL_CAMERA_SUPPORT |
133 | if( 1 == aml_dev->type ){ |
134 | return gVCameraHals->setPreviewWindow(window); |
135 | } |
136 | #endif |
137 | |
138 | rv = gCameraHals[aml_dev->cameraid]->setPreviewWindow(window); |
139 | |
140 | return rv; |
141 | } |
142 | |
143 | void camera_set_callbacks(struct camera_device * device, |
144 | camera_notify_callback notify_cb, |
145 | camera_data_callback data_cb, |
146 | camera_data_timestamp_callback data_cb_timestamp, |
147 | camera_request_memory get_memory, |
148 | void *user) |
149 | { |
150 | aml_camera_device_t* aml_dev = NULL; |
151 | |
152 | LOG_FUNCTION_NAME; |
153 | |
154 | if(!device) |
155 | return; |
156 | |
157 | aml_dev = (aml_camera_device_t*) device; |
158 | |
159 | #ifdef AMLOGIC_VIRTUAL_CAMERA_SUPPORT |
160 | if( 1 == aml_dev->type ){ |
161 | gVCameraHals->setCallbacks(notify_cb, data_cb, data_cb_timestamp, get_memory, user); |
162 | return; |
163 | } |
164 | #endif |
165 | gCameraHals[aml_dev->cameraid]->setCallbacks(notify_cb, data_cb, data_cb_timestamp, get_memory, user); |
166 | } |
167 | |
168 | void camera_enable_msg_type(struct camera_device * device, int32_t msg_type) |
169 | { |
170 | aml_camera_device_t* aml_dev = NULL; |
171 | |
172 | LOG_FUNCTION_NAME; |
173 | |
174 | if(!device) |
175 | return; |
176 | |
177 | aml_dev = (aml_camera_device_t*) device; |
178 | |
179 | #ifdef AMLOGIC_VIRTUAL_CAMERA_SUPPORT |
180 | if( 1 == aml_dev->type ){ |
181 | gVCameraHals->enableMsgType(msg_type); |
182 | return ; |
183 | } |
184 | #endif |
185 | gCameraHals[aml_dev->cameraid]->enableMsgType(msg_type); |
186 | } |
187 | |
188 | void camera_disable_msg_type(struct camera_device * device, int32_t msg_type) |
189 | { |
190 | aml_camera_device_t* aml_dev = NULL; |
191 | |
192 | LOG_FUNCTION_NAME; |
193 | |
194 | if(!device) |
195 | return; |
196 | |
197 | aml_dev = (aml_camera_device_t*) device; |
198 | |
199 | #ifdef AMLOGIC_VIRTUAL_CAMERA_SUPPORT |
200 | if( 1 == aml_dev->type ){ |
201 | gVCameraHals->disableMsgType(msg_type); |
202 | return; |
203 | } |
204 | #endif |
205 | gCameraHals[aml_dev->cameraid]->disableMsgType(msg_type); |
206 | } |
207 | |
208 | int camera_msg_type_enabled(struct camera_device * device, int32_t msg_type) |
209 | { |
210 | aml_camera_device_t* aml_dev = NULL; |
211 | |
212 | LOG_FUNCTION_NAME; |
213 | |
214 | if(!device) |
215 | return 0; |
216 | |
217 | aml_dev = (aml_camera_device_t*) device; |
218 | |
219 | return gCameraHals[aml_dev->cameraid]->msgTypeEnabled(msg_type); |
220 | } |
221 | |
222 | int camera_start_preview(struct camera_device * device) |
223 | { |
224 | int rv = -EINVAL; |
225 | aml_camera_device_t* aml_dev = NULL; |
226 | |
227 | LOG_FUNCTION_NAME; |
228 | |
229 | if(!device) |
230 | return rv; |
231 | |
232 | aml_dev = (aml_camera_device_t*) device; |
233 | |
234 | #ifdef AMLOGIC_VIRTUAL_CAMERA_SUPPORT |
235 | if( 1 == aml_dev->type ){ |
236 | return gVCameraHals->startPreview(); |
237 | } |
238 | #endif |
239 | rv = gCameraHals[aml_dev->cameraid]->startPreview(); |
240 | |
241 | return rv; |
242 | } |
243 | |
244 | void camera_stop_preview(struct camera_device * device) |
245 | { |
246 | aml_camera_device_t* aml_dev = NULL; |
247 | |
248 | LOG_FUNCTION_NAME; |
249 | |
250 | if(!device) |
251 | return; |
252 | |
253 | aml_dev = (aml_camera_device_t*) device; |
254 | |
255 | #ifdef AMLOGIC_VIRTUAL_CAMERA_SUPPORT |
256 | if( 1 == aml_dev->type ){ |
257 | gVCameraHals->stopPreview(); |
258 | return ; |
259 | } |
260 | #endif |
261 | gCameraHals[aml_dev->cameraid]->stopPreview(); |
262 | } |
263 | |
264 | int camera_preview_enabled(struct camera_device * device) |
265 | { |
266 | int rv = -EINVAL; |
267 | aml_camera_device_t* aml_dev = NULL; |
268 | |
269 | LOG_FUNCTION_NAME; |
270 | |
271 | if(!device) |
272 | return rv; |
273 | |
274 | aml_dev = (aml_camera_device_t*) device; |
275 | |
276 | #ifdef AMLOGIC_VIRTUAL_CAMERA_SUPPORT |
277 | if( 1 == aml_dev->type ){ |
278 | return gVCameraHals->previewEnabled(); |
279 | } |
280 | #endif |
281 | rv = gCameraHals[aml_dev->cameraid]->previewEnabled(); |
282 | return rv; |
283 | } |
284 | |
285 | int camera_store_meta_data_in_buffers(struct camera_device * device, int enable) |
286 | { |
287 | int rv = -EINVAL; |
288 | aml_camera_device_t* aml_dev = NULL; |
289 | |
290 | LOG_FUNCTION_NAME; |
291 | |
292 | if(!device) |
293 | return rv; |
294 | |
295 | aml_dev = (aml_camera_device_t*) device; |
296 | |
297 | // TODO: meta data buffer not current supported |
298 | #ifdef AMLOGIC_VIRTUAL_CAMERA_SUPPORT |
299 | if( 1 == aml_dev->type ){ |
300 | return gVCameraHals->storeMetaDataInBuffers(enable); |
301 | } |
302 | #endif |
303 | rv = gCameraHals[aml_dev->cameraid]->storeMetaDataInBuffers(enable); |
304 | return rv; |
305 | //return enable ? android::INVALID_OPERATION: android::OK; |
306 | } |
307 | |
308 | int camera_start_recording(struct camera_device * device) |
309 | { |
310 | int rv = -EINVAL; |
311 | aml_camera_device_t* aml_dev = NULL; |
312 | |
313 | LOG_FUNCTION_NAME; |
314 | |
315 | if(!device) |
316 | return rv; |
317 | |
318 | aml_dev = (aml_camera_device_t*) device; |
319 | |
320 | #ifdef AMLOGIC_VIRTUAL_CAMERA_SUPPORT |
321 | if( 1 == aml_dev->type ){ |
322 | return gVCameraHals->startRecording(); |
323 | } |
324 | #endif |
325 | rv = gCameraHals[aml_dev->cameraid]->startRecording(); |
326 | return rv; |
327 | } |
328 | |
329 | void camera_stop_recording(struct camera_device * device) |
330 | { |
331 | aml_camera_device_t* aml_dev = NULL; |
332 | |
333 | LOG_FUNCTION_NAME; |
334 | |
335 | if(!device) |
336 | return; |
337 | |
338 | aml_dev = (aml_camera_device_t*) device; |
339 | |
340 | #ifdef AMLOGIC_VIRTUAL_CAMERA_SUPPORT |
341 | if( 1 == aml_dev->type ){ |
342 | gVCameraHals->stopRecording(); |
343 | return; |
344 | } |
345 | #endif |
346 | gCameraHals[aml_dev->cameraid]->stopRecording(); |
347 | } |
348 | |
349 | int camera_recording_enabled(struct camera_device * device) |
350 | { |
351 | int rv = -EINVAL; |
352 | aml_camera_device_t* aml_dev = NULL; |
353 | |
354 | LOG_FUNCTION_NAME; |
355 | |
356 | if(!device) |
357 | return rv; |
358 | |
359 | aml_dev = (aml_camera_device_t*) device; |
360 | |
361 | #ifdef AMLOGIC_VIRTUAL_CAMERA_SUPPORT |
362 | if( 1 == aml_dev->type ){ |
363 | return gVCameraHals->recordingEnabled(); |
364 | } |
365 | #endif |
366 | rv = gCameraHals[aml_dev->cameraid]->recordingEnabled(); |
367 | return rv; |
368 | } |
369 | |
370 | void camera_release_recording_frame(struct camera_device * device, |
371 | const void *opaque) |
372 | { |
373 | aml_camera_device_t* aml_dev = NULL; |
374 | |
375 | LOG_FUNCTION_NAME; |
376 | |
377 | if(!device) |
378 | return; |
379 | |
380 | aml_dev = (aml_camera_device_t*) device; |
381 | |
382 | #ifdef AMLOGIC_VIRTUAL_CAMERA_SUPPORT |
383 | if( 1 == aml_dev->type ){ |
384 | gVCameraHals->releaseRecordingFrame(opaque); |
385 | return; |
386 | } |
387 | #endif |
388 | gCameraHals[aml_dev->cameraid]->releaseRecordingFrame(opaque); |
389 | } |
390 | |
391 | int camera_auto_focus(struct camera_device * device) |
392 | { |
393 | int rv = -EINVAL; |
394 | aml_camera_device_t* aml_dev = NULL; |
395 | |
396 | LOG_FUNCTION_NAME; |
397 | |
398 | if(!device) |
399 | return rv; |
400 | |
401 | aml_dev = (aml_camera_device_t*) device; |
402 | |
403 | #ifdef AMLOGIC_VIRTUAL_CAMERA_SUPPORT |
404 | if( 1 == aml_dev->type ){ |
405 | return gVCameraHals->autoFocus(); |
406 | } |
407 | #endif |
408 | rv = gCameraHals[aml_dev->cameraid]->autoFocus(); |
409 | return rv; |
410 | } |
411 | |
412 | int camera_cancel_auto_focus(struct camera_device * device) |
413 | { |
414 | int rv = -EINVAL; |
415 | aml_camera_device_t* aml_dev = NULL; |
416 | |
417 | LOG_FUNCTION_NAME; |
418 | |
419 | if(!device) |
420 | return rv; |
421 | |
422 | aml_dev = (aml_camera_device_t*) device; |
423 | |
424 | #ifdef AMLOGIC_VIRTUAL_CAMERA_SUPPORT |
425 | if( 1 == aml_dev->type ){ |
426 | return gVCameraHals->cancelAutoFocus(); |
427 | } |
428 | #endif |
429 | rv = gCameraHals[aml_dev->cameraid]->cancelAutoFocus(); |
430 | return rv; |
431 | } |
432 | |
433 | int camera_take_picture(struct camera_device * device) |
434 | { |
435 | int rv = -EINVAL; |
436 | aml_camera_device_t* aml_dev = NULL; |
437 | |
438 | LOG_FUNCTION_NAME; |
439 | |
440 | if(!device) |
441 | return rv; |
442 | |
443 | aml_dev = (aml_camera_device_t*) device; |
444 | |
445 | #ifdef AMLOGIC_VIRTUAL_CAMERA_SUPPORT |
446 | if( 1 == aml_dev->type ){ |
447 | return gVCameraHals->takePicture(); |
448 | } |
449 | #endif |
450 | rv = gCameraHals[aml_dev->cameraid]->takePicture(); |
451 | return rv; |
452 | } |
453 | |
454 | int camera_cancel_picture(struct camera_device * device) |
455 | { |
456 | int rv = -EINVAL; |
457 | aml_camera_device_t* aml_dev = NULL; |
458 | |
459 | LOG_FUNCTION_NAME; |
460 | |
461 | if(!device) |
462 | return rv; |
463 | |
464 | aml_dev = (aml_camera_device_t*) device; |
465 | |
466 | #ifdef AMLOGIC_VIRTUAL_CAMERA_SUPPORT |
467 | if( 1 == aml_dev->type ){ |
468 | return gVCameraHals->cancelPicture(); |
469 | } |
470 | #endif |
471 | rv = gCameraHals[aml_dev->cameraid]->cancelPicture(); |
472 | return rv; |
473 | } |
474 | |
475 | int camera_set_parameters(struct camera_device * device, const char *params) |
476 | { |
477 | int rv = -EINVAL; |
478 | aml_camera_device_t* aml_dev = NULL; |
479 | |
480 | LOG_FUNCTION_NAME; |
481 | |
482 | if(!device) |
483 | return rv; |
484 | |
485 | aml_dev = (aml_camera_device_t*) device; |
486 | |
487 | #ifdef AMLOGIC_VIRTUAL_CAMERA_SUPPORT |
488 | if( 1 == aml_dev->type ){ |
489 | return gVCameraHals->setParameters(params); |
490 | } |
491 | #endif |
492 | rv = gCameraHals[aml_dev->cameraid]->setParameters(params); |
493 | return rv; |
494 | } |
495 | |
496 | char* camera_get_parameters(struct camera_device * device) |
497 | { |
498 | char* param = NULL; |
499 | aml_camera_device_t* aml_dev = NULL; |
500 | |
501 | LOG_FUNCTION_NAME; |
502 | |
503 | if(!device) |
504 | return NULL; |
505 | |
506 | aml_dev = (aml_camera_device_t*) device; |
507 | |
508 | #ifdef AMLOGIC_VIRTUAL_CAMERA_SUPPORT |
509 | if( 1 == aml_dev->type ){ |
510 | return gVCameraHals->getParameters(); |
511 | } |
512 | #endif |
513 | param = gCameraHals[aml_dev->cameraid]->getParameters(); |
514 | |
515 | return param; |
516 | } |
517 | |
518 | static void camera_put_parameters(struct camera_device *device, char *parms) |
519 | { |
520 | aml_camera_device_t* aml_dev = NULL; |
521 | |
522 | LOG_FUNCTION_NAME; |
523 | |
524 | if(!device) |
525 | return; |
526 | |
527 | aml_dev = (aml_camera_device_t*) device; |
528 | |
529 | #ifdef AMLOGIC_VIRTUAL_CAMERA_SUPPORT |
530 | if( 1 == aml_dev->type ){ |
531 | gVCameraHals->putParameters(parms); |
532 | return ; |
533 | } |
534 | #endif |
535 | gCameraHals[aml_dev->cameraid]->putParameters(parms); |
536 | } |
537 | |
538 | int camera_send_command(struct camera_device * device, |
539 | int32_t cmd, int32_t arg1, int32_t arg2) |
540 | { |
541 | int rv = -EINVAL; |
542 | aml_camera_device_t* aml_dev = NULL; |
543 | |
544 | LOG_FUNCTION_NAME; |
545 | |
546 | if(!device) |
547 | return rv; |
548 | |
549 | aml_dev = (aml_camera_device_t*) device; |
550 | |
551 | #ifdef AMLOGIC_VIRTUAL_CAMERA_SUPPORT |
552 | if( 1 == aml_dev->type ){ |
553 | return gVCameraHals->sendCommand(cmd, arg1, arg2); |
554 | } |
555 | #endif |
556 | rv = gCameraHals[aml_dev->cameraid]->sendCommand(cmd, arg1, arg2); |
557 | return rv; |
558 | } |
559 | |
560 | void camera_release(struct camera_device * device) |
561 | { |
562 | aml_camera_device_t* aml_dev = NULL; |
563 | |
564 | LOG_FUNCTION_NAME; |
565 | |
566 | if(!device) |
567 | return; |
568 | |
569 | aml_dev = (aml_camera_device_t*) device; |
570 | |
571 | #ifdef AMLOGIC_VIRTUAL_CAMERA_SUPPORT |
572 | if( 1 == aml_dev->type ){ |
573 | gVCameraHals->release(); |
574 | return ; |
575 | } |
576 | #endif |
577 | gCameraHals[aml_dev->cameraid]->release(); |
578 | } |
579 | |
580 | int camera_dump(struct camera_device * device, int fd) |
581 | { |
582 | int rv = -EINVAL; |
583 | aml_camera_device_t* aml_dev = NULL; |
584 | LOG_FUNCTION_NAME; |
585 | |
586 | if(!device) |
587 | return rv; |
588 | |
589 | aml_dev = (aml_camera_device_t*) device; |
590 | |
591 | #ifdef AMLOGIC_VIRTUAL_CAMERA_SUPPORT |
592 | if( 1 == aml_dev->type ){ |
593 | return gVCameraHals->dump(fd); |
594 | } |
595 | #endif |
596 | |
597 | setLogLevel(aml_dev->base.priv); |
598 | |
599 | rv = gCameraHals[aml_dev->cameraid]->dump(fd); |
600 | return rv; |
601 | } |
602 | |
603 | extern "C" void heaptracker_free_leaked_memory(void); |
604 | |
605 | int camera_device_close(hw_device_t* device) |
606 | { |
607 | int ret = 0; |
608 | aml_camera_device_t* aml_dev = NULL; |
609 | |
610 | LOG_FUNCTION_NAME; |
611 | |
612 | android::Mutex::Autolock lock(gCameraHalDeviceLock); |
613 | |
614 | if (!device) { |
615 | ret = -EINVAL; |
616 | goto done; |
617 | } |
618 | |
619 | aml_dev = (aml_camera_device_t*) device; |
620 | |
621 | if (aml_dev) { |
622 | #ifdef AMLOGIC_VIRTUAL_CAMERA_SUPPORT |
623 | if( 1 == aml_dev->type ){ |
624 | if (gVCameraHals) { |
625 | delete gVCameraHals; |
626 | gVCameraHals = NULL; |
627 | gCamerasOpen--; |
628 | } |
629 | }else{ |
630 | if (gCameraHals[aml_dev->cameraid]) { |
631 | delete gCameraHals[aml_dev->cameraid]; |
632 | gCameraHals[aml_dev->cameraid] = NULL; |
633 | gCamerasOpen--; |
634 | } |
635 | } |
636 | #else |
637 | if (gCameraHals[aml_dev->cameraid]) { |
638 | delete gCameraHals[aml_dev->cameraid]; |
639 | gCameraHals[aml_dev->cameraid] = NULL; |
640 | gCamerasOpen--; |
641 | } |
642 | #endif |
643 | |
644 | if (aml_dev->base.ops) { |
645 | free(aml_dev->base.ops); |
646 | } |
647 | free(aml_dev); |
648 | } |
649 | done: |
650 | #ifdef HEAPTRACKER |
651 | heaptracker_free_leaked_memory(); |
652 | #endif |
653 | return ret; |
654 | } |
655 | |
656 | /******************************************************************* |
657 | * implementation of camera_module functions |
658 | *******************************************************************/ |
659 | |
660 | /* open device handle to one of the cameras |
661 | * |
662 | * assume camera service will keep singleton of each camera |
663 | * so this function will always only be called once per camera instance |
664 | */ |
665 | |
666 | int camera_device_open(const hw_module_t* module, const char* name, |
667 | hw_device_t** device) |
668 | { |
669 | int rv = 0; |
670 | int num_cameras = 0; |
671 | int cameraid; |
672 | aml_camera_device_t* camera_device = NULL; |
673 | camera_device_ops_t* camera_ops = NULL; |
674 | android::CameraHal* camera = NULL; |
675 | android::CameraProperties::Properties* properties = NULL; |
676 | |
677 | android::Mutex::Autolock lock(gCameraHalDeviceLock); |
678 | |
679 | CAMHAL_LOGIA("camera_device open"); |
680 | |
681 | if (name != NULL) { |
682 | cameraid = atoi(name); |
683 | num_cameras = gCameraProperties.camerasSupported(); |
684 | |
685 | if(cameraid > num_cameras) |
686 | { |
687 | CAMHAL_LOGEB("camera service provided cameraid out of bounds," |
688 | "cameraid = %d, num supported = %d", |
689 | cameraid, num_cameras); |
690 | rv = -EINVAL; |
691 | goto fail; |
692 | } |
693 | |
694 | #ifndef AMLOGIC_VIRTUAL_CAMERA_SUPPORT |
695 | if(gCamerasOpen >= MAX_SIMUL_CAMERAS_SUPPORTED) |
696 | { |
697 | CAMHAL_LOGEA("maximum number of cameras already open"); |
698 | rv = -ENOMEM; |
699 | goto fail; |
700 | } |
701 | #else |
702 | if((gCamerasOpen >= MAX_SIMUL_CAMERAS_SUPPORTED) && |
703 | (!gVCameraHals) ) |
704 | { |
705 | CAMHAL_LOGEA("maximum number of cameras already open"); |
706 | rv = -ENOMEM; |
707 | goto fail; |
708 | } |
709 | |
710 | CAMHAL_LOGDB("cameraid=%d, num_cameras-1=%d\n", cameraid, num_cameras-1); |
711 | CAMHAL_LOGDB("max_add-1=%d\n", gCamerasSupported-1); |
712 | |
713 | if( cameraid == (gCamerasSupported-1) ) |
714 | { |
715 | camera_device = (aml_camera_device_t*)malloc(sizeof(*camera_device)); |
716 | if(!camera_device) |
717 | { |
718 | CAMHAL_LOGEA("camera_device allocation fail"); |
719 | rv = -ENOMEM; |
720 | goto fail; |
721 | } |
722 | |
723 | camera_ops = (camera_device_ops_t*)malloc(sizeof(*camera_ops)); |
724 | if(!camera_ops) |
725 | { |
726 | CAMHAL_LOGEA("camera_ops allocation fail"); |
727 | rv = -ENOMEM; |
728 | goto fail; |
729 | } |
730 | |
731 | memset(camera_device, 0, sizeof(*camera_device)); |
732 | memset(camera_ops, 0, sizeof(*camera_ops)); |
733 | |
734 | camera_device->base.common.tag = HARDWARE_DEVICE_TAG; |
735 | camera_device->base.common.version = 0; |
736 | camera_device->base.common.module = (hw_module_t *)(module); |
737 | camera_device->base.common.close = camera_device_close; |
738 | camera_device->base.ops = camera_ops; |
739 | |
740 | camera_ops->set_preview_window = camera_set_preview_window; |
741 | camera_ops->set_callbacks = camera_set_callbacks; |
742 | camera_ops->enable_msg_type = camera_enable_msg_type; |
743 | camera_ops->disable_msg_type = camera_disable_msg_type; |
744 | camera_ops->msg_type_enabled = camera_msg_type_enabled; |
745 | camera_ops->start_preview = camera_start_preview; |
746 | camera_ops->stop_preview = camera_stop_preview; |
747 | camera_ops->preview_enabled = camera_preview_enabled; |
748 | camera_ops->store_meta_data_in_buffers = camera_store_meta_data_in_buffers; |
749 | camera_ops->start_recording = camera_start_recording; |
750 | camera_ops->stop_recording = camera_stop_recording; |
751 | camera_ops->recording_enabled = camera_recording_enabled; |
752 | camera_ops->release_recording_frame = camera_release_recording_frame; |
753 | camera_ops->auto_focus = camera_auto_focus; |
754 | camera_ops->cancel_auto_focus = camera_cancel_auto_focus; |
755 | camera_ops->take_picture = camera_take_picture; |
756 | camera_ops->cancel_picture = camera_cancel_picture; |
757 | camera_ops->set_parameters = camera_set_parameters; |
758 | camera_ops->get_parameters = camera_get_parameters; |
759 | camera_ops->put_parameters = camera_put_parameters; |
760 | camera_ops->send_command = camera_send_command; |
761 | camera_ops->release = camera_release; |
762 | camera_ops->dump = camera_dump; |
763 | |
764 | *device = &camera_device->base.common; |
765 | |
766 | // -------- vendor specific stuff -------- |
767 | |
768 | CAMHAL_LOGDB("virtual num_cameras=%d cameraid=%d", num_cameras, cameraid); |
769 | camera_device->cameraid = cameraid; |
770 | camera_device->type = 1; |
771 | |
772 | if(gCameraProperties.getProperties(cameraid, &properties) < 0) |
773 | { |
774 | CAMHAL_LOGEA("Couldn't get virtual camera properties"); |
775 | rv = -ENOMEM; |
776 | goto fail; |
777 | } |
778 | |
779 | gVCameraHals = new android::VirtualCamHal(cameraid); |
780 | CAMHAL_LOGDA("Virtual CameraHal\n"); |
781 | |
782 | if(!gVCameraHals) |
783 | { |
784 | CAMHAL_LOGEA("Couldn't create instance of VirtualCameraHal class"); |
785 | rv = -ENOMEM; |
786 | goto fail; |
787 | } |
788 | |
789 | if(properties && (gVCameraHals->initialize(properties) != android::NO_ERROR)) |
790 | { |
791 | CAMHAL_LOGEA("Couldn't initialize virtual camera instance"); |
792 | rv = -ENODEV; |
793 | goto fail; |
794 | } |
795 | |
796 | gCamerasOpen++; |
797 | |
798 | return rv; |
799 | } |
800 | #endif |
801 | |
802 | camera_device = (aml_camera_device_t*)malloc(sizeof(*camera_device)); |
803 | if(!camera_device) |
804 | { |
805 | CAMHAL_LOGEA("camera_device allocation fail"); |
806 | rv = -ENOMEM; |
807 | goto fail; |
808 | } |
809 | |
810 | camera_ops = (camera_device_ops_t*)malloc(sizeof(*camera_ops)); |
811 | if(!camera_ops) |
812 | { |
813 | CAMHAL_LOGEA("camera_ops allocation fail"); |
814 | rv = -ENOMEM; |
815 | goto fail; |
816 | } |
817 | |
818 | memset(camera_device, 0, sizeof(*camera_device)); |
819 | memset(camera_ops, 0, sizeof(*camera_ops)); |
820 | |
821 | camera_device->base.common.tag = HARDWARE_DEVICE_TAG; |
822 | camera_device->base.common.version = 0; |
823 | camera_device->base.common.module = (hw_module_t *)(module); |
824 | camera_device->base.common.close = camera_device_close; |
825 | camera_device->base.ops = camera_ops; |
826 | |
827 | camera_ops->set_preview_window = camera_set_preview_window; |
828 | camera_ops->set_callbacks = camera_set_callbacks; |
829 | camera_ops->enable_msg_type = camera_enable_msg_type; |
830 | camera_ops->disable_msg_type = camera_disable_msg_type; |
831 | camera_ops->msg_type_enabled = camera_msg_type_enabled; |
832 | camera_ops->start_preview = camera_start_preview; |
833 | camera_ops->stop_preview = camera_stop_preview; |
834 | camera_ops->preview_enabled = camera_preview_enabled; |
835 | camera_ops->store_meta_data_in_buffers = camera_store_meta_data_in_buffers; |
836 | camera_ops->start_recording = camera_start_recording; |
837 | camera_ops->stop_recording = camera_stop_recording; |
838 | camera_ops->recording_enabled = camera_recording_enabled; |
839 | camera_ops->release_recording_frame = camera_release_recording_frame; |
840 | camera_ops->auto_focus = camera_auto_focus; |
841 | camera_ops->cancel_auto_focus = camera_cancel_auto_focus; |
842 | camera_ops->take_picture = camera_take_picture; |
843 | camera_ops->cancel_picture = camera_cancel_picture; |
844 | camera_ops->set_parameters = camera_set_parameters; |
845 | camera_ops->get_parameters = camera_get_parameters; |
846 | camera_ops->put_parameters = camera_put_parameters; |
847 | camera_ops->send_command = camera_send_command; |
848 | camera_ops->release = camera_release; |
849 | camera_ops->dump = camera_dump; |
850 | |
851 | *device = &camera_device->base.common; |
852 | |
853 | // -------- vendor specific stuff -------- |
854 | |
855 | CAMHAL_LOGDB("num_cameras=%d cameraid=%d", num_cameras, cameraid); |
856 | camera_device->cameraid = cameraid; |
857 | #ifdef AMLOGIC_VIRTUAL_CAMERA_SUPPORT |
858 | camera_device->type = 0; |
859 | #endif |
860 | |
861 | if(gCameraProperties.getProperties(cameraid, &properties) < 0) |
862 | { |
863 | CAMHAL_LOGEA("Couldn't get camera properties"); |
864 | rv = -ENOMEM; |
865 | goto fail; |
866 | } |
867 | |
868 | camera = new android::CameraHal(cameraid); |
869 | |
870 | if(!camera) |
871 | { |
872 | CAMHAL_LOGEA("Couldn't create instance of CameraHal class"); |
873 | rv = -ENOMEM; |
874 | goto fail; |
875 | } |
876 | |
877 | if(properties && (camera->initialize(properties) != android::NO_ERROR)) |
878 | { |
879 | CAMHAL_LOGEA("Couldn't initialize camera instance"); |
880 | rv = -ENODEV; |
881 | goto fail; |
882 | } |
883 | |
884 | gCameraHals[cameraid] = camera; |
885 | gCamerasOpen++; |
886 | } |
887 | |
888 | return rv; |
889 | |
890 | fail: |
891 | if(camera_device) { |
892 | free(camera_device); |
893 | camera_device = NULL; |
894 | } |
895 | if(camera_ops) { |
896 | free(camera_ops); |
897 | camera_ops = NULL; |
898 | } |
899 | if(camera) { |
900 | delete camera; |
901 | camera = NULL; |
902 | } |
903 | *device = NULL; |
904 | return rv; |
905 | } |
906 | |
907 | extern "C" int CameraAdapter_CameraNum(); |
908 | int camera_get_number_of_cameras(void) |
909 | { |
910 | int num_cameras = CameraAdapter_CameraNum(); |
911 | gCamerasSupported = num_cameras; |
912 | CAMHAL_LOGDB("gCamerasSupported=%d,num_cameras=%d\n", |
913 | gCamerasSupported, num_cameras); |
914 | |
915 | #ifdef HAVE_VERSION_INFO |
916 | CAMHAL_LOGIB("\n--------------------------------\n" |
917 | "author:aml.sh multi-media team\n" |
918 | "branch name: %s\n" |
919 | "git version: %s \n" |
920 | "last changed: %s\n" |
921 | "build-time: %s\n" |
922 | "build-name: %s\n" |
923 | "uncommitted-file-num:%d\n" |
924 | "ssh user@%s, cd %s\n" |
925 | "hostname %s\n" |
926 | "--------------------------------\n", |
927 | CAMHAL_BRANCH_NAME, |
928 | CAMHAL_GIT_VERSION, |
929 | CAMHAL_LAST_CHANGED, |
930 | CAMHAL_BUILD_TIME, |
931 | CAMHAL_BUILD_NAME, |
932 | CAMHAL_GIT_UNCOMMIT_FILE_NUM, |
933 | CAMHAL_IP, CAMHAL_PATH, CAMHAL_HOSTNAME |
934 | ); |
935 | #endif |
936 | for(unsigned i = 0;i<sizeof(macro_info)/sizeof(macro_info[0]) ;i++){ |
937 | CAMHAL_LOGIB("%s\n", macro_info[i]); |
938 | } |
939 | return num_cameras; |
940 | } |
941 | |
942 | int camera_get_camera_info(int camera_id, struct camera_info *info) |
943 | { |
944 | int rv = 0; |
945 | int face_value = CAMERA_FACING_BACK; |
946 | int orientation = 0; |
947 | const char *valstr = NULL; |
948 | android::CameraProperties::Properties* properties = NULL; |
949 | |
950 | CAMHAL_LOGDB("camera_get_camera_info camera_id=%d", camera_id); |
951 | // this going to be the first call from camera service |
952 | // initialize camera properties here... |
953 | if( ( gCamerasOpen == 0 ) |
954 | && (gCameraProperties.initialize(camera_id) != android::NO_ERROR)) |
955 | { |
956 | CAMHAL_LOGEA("Unable to create or initialize CameraProperties"); |
957 | return -EINVAL; |
958 | } |
959 | |
960 | //Get camera properties for camera index |
961 | if(gCameraProperties.getProperties(camera_id, &properties) < 0) |
962 | { |
963 | CAMHAL_LOGEA("Couldn't get camera properties"); |
964 | rv = -EINVAL; |
965 | goto end; |
966 | } |
967 | |
968 | if(properties) |
969 | { |
970 | valstr = properties->get(android::CameraProperties::FACING_INDEX); |
971 | if(valstr != NULL) |
972 | { |
973 | if (strcmp(valstr, (const char *) android::ExCameraParameters::FACING_FRONT) == 0) |
974 | { |
975 | face_value = CAMERA_FACING_FRONT; |
976 | } |
977 | else if (strcmp(valstr, (const char *) android::ExCameraParameters::FACING_BACK) == 0) |
978 | { |
979 | face_value = CAMERA_FACING_BACK; |
980 | } |
981 | } |
982 | |
983 | valstr = properties->get(android::CameraProperties::ORIENTATION_INDEX); |
984 | if(valstr != NULL) |
985 | { |
986 | orientation = atoi(valstr); |
987 | } |
988 | } |
989 | else |
990 | { |
991 | CAMHAL_LOGEB("getProperties() returned a NULL property set for Camera id %d", camera_id); |
992 | } |
993 | |
994 | info->facing = face_value; |
995 | info->orientation = orientation; |
996 | |
997 | end: |
998 | return rv; |
999 | } |
1000 | |
1001 | |
1002 | |
1003 | |
1004 | |
1005 |