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