2 * Copyright (c) 2011 Samsung Electronics Co., Ltd All Rights Reserved
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
23 #include <camera_internal.h>
24 #include <camera_private.h>
31 #define LOG_TAG "TIZEN_N_CAMERA"
33 #define LIB_CAMERA_DEVICE_MANAGER PATH_LIBDIR"/libcamera_device_manager.so"
35 typedef struct _cdm_symbol_table {
37 const char *func_name;
42 int camera_start_evas_rendering(camera_h camera)
44 return _camera_start_evas_rendering(camera);
48 int camera_stop_evas_rendering(camera_h camera, bool keep_screen)
50 return _camera_stop_evas_rendering(camera, keep_screen);
54 int camera_set_ecore_wl_display(camera_h camera, void *ecore_wl_window)
56 return _camera_set_display(camera, MM_DISPLAY_TYPE_OVERLAY_EXT, ecore_wl_window);
61 void camera_create_preview_frame(camera_stream_data_s *stream, int num_buffer_fd,
62 tbm_bo_handle *buffer_bo_handle, tbm_bo_handle *data_bo_handle, camera_preview_data_s *frame)
65 unsigned char *buf_pos = NULL;
67 if (!stream || !buffer_bo_handle || !frame) {
68 CAM_LOG_ERROR("invalid param %p,%p,%p", stream, buffer_bo_handle, frame);
73 if (stream->format == MM_PIXEL_FORMAT_ITLV_JPEG_UYVY)
74 frame->format = MM_PIXEL_FORMAT_UYVY;
76 frame->format = stream->format;
78 frame->width = stream->width;
79 frame->height = stream->height;
80 frame->timestamp = stream->timestamp;
81 frame->num_of_planes = stream->num_planes;
83 if (num_buffer_fd == 0) {
86 if (!data_bo_handle || !data_bo_handle->ptr) {
87 CAM_LOG_ERROR("NULL pointer");
91 buf_pos = data_bo_handle->ptr;
93 if (stream->format == MM_PIXEL_FORMAT_ENCODED_H264) {
94 frame->data.encoded_plane.data = buf_pos;
95 frame->data.encoded_plane.size = stream->data.encoded.length_data;
96 frame->data.encoded_plane.is_delta_frame = (bool)stream->data.encoded.is_delta_frame;
97 total_size = stream->data.encoded.length_data;
98 } else if (stream->format == MM_PIXEL_FORMAT_ENCODED_MJPEG) {
99 frame->data.encoded_plane.data = buf_pos;
100 frame->data.encoded_plane.size = stream->data.encoded.length_data;
101 total_size = stream->data.encoded.length_data;
102 } else if (stream->format == MM_PIXEL_FORMAT_INVZ) {
103 frame->data.depth_plane.data = buf_pos;
104 frame->data.depth_plane.size = stream->data.depth.length_data;
105 total_size = stream->data.depth.length_data;
106 } else if (stream->format == MM_PIXEL_FORMAT_RGBA ||
107 stream->format == MM_PIXEL_FORMAT_ARGB) {
108 frame->data.rgb_plane.data = buf_pos;
109 frame->data.rgb_plane.size = stream->data.rgb.length_data;
110 total_size = stream->data.rgb.length_data;
112 switch (stream->num_planes) {
114 frame->data.single_plane.yuv = buf_pos;
115 frame->data.single_plane.size = stream->data.yuv420.length_yuv;
116 total_size = stream->data.yuv420.length_yuv;
119 frame->data.double_plane.y = buf_pos;
120 frame->data.double_plane.y_size = stream->data.yuv420sp.length_y;
121 buf_pos += stream->data.yuv420sp.length_y;
122 frame->data.double_plane.uv = buf_pos;
123 frame->data.double_plane.uv_size = stream->data.yuv420sp.length_uv;
124 total_size = stream->data.yuv420sp.length_y + \
125 stream->data.yuv420sp.length_uv;
128 frame->data.triple_plane.y = buf_pos;
129 frame->data.triple_plane.y_size = stream->data.yuv420p.length_y;
130 buf_pos += stream->data.yuv420p.length_y;
131 frame->data.triple_plane.u = buf_pos;
132 frame->data.triple_plane.u_size = stream->data.yuv420p.length_u;
133 buf_pos += stream->data.yuv420p.length_u;
134 frame->data.triple_plane.v = buf_pos;
135 frame->data.triple_plane.v_size = stream->data.yuv420p.length_v;
136 total_size = stream->data.yuv420p.length_y + \
137 stream->data.yuv420p.length_u + \
138 stream->data.yuv420p.length_v;
147 switch (stream->num_planes) {
150 frame->data.single_plane.yuv = buffer_bo_handle[0].ptr;
151 frame->data.single_plane.size = stream->data.yuv420.length_yuv;
152 total_size = stream->data.yuv420.length_yuv;
156 frame->data.double_plane.y = buffer_bo_handle[0].ptr;
157 if (stream->num_planes == (unsigned int)num_buffer_fd)
158 frame->data.double_plane.uv = buffer_bo_handle[1].ptr;
160 frame->data.double_plane.uv = buffer_bo_handle[0].ptr + stream->data.yuv420sp.length_y;
161 frame->data.double_plane.y_size = stream->data.yuv420sp.length_y;
162 frame->data.double_plane.uv_size = stream->data.yuv420sp.length_uv;
163 total_size = stream->data.yuv420sp.length_y + \
164 stream->data.yuv420sp.length_uv;
168 frame->data.triple_plane.y = buffer_bo_handle[0].ptr;
169 if (stream->num_planes == (unsigned int)num_buffer_fd) {
170 frame->data.triple_plane.u = buffer_bo_handle[1].ptr;
171 frame->data.triple_plane.v = buffer_bo_handle[2].ptr;
173 frame->data.triple_plane.u = buffer_bo_handle[0].ptr + stream->data.yuv420p.length_y;
174 frame->data.triple_plane.v = buffer_bo_handle[1].ptr + stream->data.yuv420p.length_u;
176 frame->data.triple_plane.y_size = stream->data.yuv420p.length_y;
177 frame->data.triple_plane.u_size = stream->data.yuv420p.length_u;
178 frame->data.triple_plane.v_size = stream->data.yuv420p.length_v;
179 total_size = stream->data.yuv420p.length_y + \
180 stream->data.yuv420p.length_u + \
181 stream->data.yuv420p.length_v;
189 CAM_LOG_DEBUG("format[%d], res[%dx%d], size[%d], plane num[%d]",
190 frame->format, frame->width, frame->height, total_size, frame->num_of_planes);
194 int camera_create_network(camera_device_e device, camera_h *camera)
196 return _camera_create_private(device, true, camera);
200 int camera_device_manager_initialize(camera_device_manager_h *manager)
203 int ret = CAMERA_ERROR_NONE;
204 void *dl_handle = NULL;
205 camera_device_manager *new_manager = g_new0(camera_device_manager, 1);
206 cdm_symbol_table sym_table[] = {
207 {(void **)&new_manager->initialize, "cdm_initialize"},
208 {(void **)&new_manager->deinitialize, "cdm_deinitialize"},
209 {(void **)&new_manager->get_device_list, "cdm_get_device_list"},
210 {(void **)&new_manager->add_device_list_changed_cb, "cdm_add_device_list_changed_cb"},
211 {(void **)&new_manager->remove_device_list_changed_cb, "cdm_remove_device_list_changed_cb"},
215 CAM_LOG_ERROR("NULL manager");
216 ret = CAMERA_ERROR_INVALID_PARAMETER;
217 goto _INITIALIZE_FAILED;
220 dl_handle = dlopen(LIB_CAMERA_DEVICE_MANAGER, RTLD_NOW);
222 CAM_LOG_ERROR("dlopen[%s] failed[%s]", LIB_CAMERA_DEVICE_MANAGER, dlerror());
223 ret = CAMERA_ERROR_NOT_SUPPORTED;
224 goto _INITIALIZE_FAILED;
228 for (i = 0 ; i < G_N_ELEMENTS(sym_table) ; i++) {
229 *sym_table[i].func_ptr = dlsym(dl_handle, sym_table[i].func_name);
230 if (*sym_table[i].func_ptr == NULL) {
231 CAM_LOG_ERROR("symbol failed[%s]", sym_table[i].func_name);
232 ret = CAMERA_ERROR_INVALID_OPERATION;
233 goto _INITIALIZE_FAILED;
237 ret = new_manager->initialize();
238 if (ret != CAMERA_ERROR_NONE) {
239 CAM_LOG_ERROR("failed[0x%x]", ret);
240 goto _INITIALIZE_FAILED;
243 new_manager->dl_handle = dl_handle;
244 *manager = (camera_device_manager_h)new_manager;
246 CAM_LOG_INFO("camera device manager[%p]", new_manager);
248 return CAMERA_ERROR_NONE;
258 int camera_device_manager_deinitialize(camera_device_manager_h manager)
260 int ret = CAMERA_ERROR_NONE;
261 camera_device_manager *m = (camera_device_manager *)manager;
264 CAM_LOG_ERROR("NULL manager");
265 return CAMERA_ERROR_INVALID_PARAMETER;
268 ret = m->deinitialize();
269 if (ret != CAMERA_ERROR_NONE) {
270 CAM_LOG_ERROR("failed[0x%x]", ret);
274 dlclose(m->dl_handle);
275 memset(m, 0x0, sizeof(camera_device_manager));
278 CAM_LOG_INFO("finalized");
284 int camera_device_manager_get_device_list(camera_device_manager_h manager, camera_device_list_s *list)
286 int ret = CAMERA_ERROR_NONE;
288 camera_device_manager *m = (camera_device_manager *)manager;
291 CAM_LOG_ERROR("NULL parameter[%p,%p]", m, list);
292 return CAMERA_ERROR_INVALID_PARAMETER;
295 CAM_LOG_INFO("enter");
297 ret = m->get_device_list(list);
298 if (ret != CAMERA_ERROR_NONE) {
299 CAM_LOG_ERROR("failed[0x%x]", ret);
303 CAM_LOG_INFO("device count[%d]", list->count);
305 for (i = 0 ; i < list->count ; i++) {
306 CAM_LOG_INFO(" [%d] : type[%d], device index[%d], name[%s], id[%s]",
307 i, list->device[i].type, list->device[i].index,
308 list->device[i].name, list->device[i].id);
315 int camera_device_manager_add_device_list_changed_cb(camera_device_manager_h manager,
316 camera_device_list_changed_cb callback, void *user_data, int *cb_id)
318 int ret = CAMERA_ERROR_NONE;
319 camera_device_manager *m = (camera_device_manager *)manager;
321 if (!m || !callback || !cb_id) {
322 CAM_LOG_ERROR("NULL parameter[%p,%p,%p]", m, callback, cb_id);
323 return CAMERA_ERROR_INVALID_PARAMETER;
326 CAM_LOG_INFO("enter");
328 ret = m->add_device_list_changed_cb(callback, user_data, cb_id);
329 if (ret != CAMERA_ERROR_NONE) {
330 CAM_LOG_ERROR("failed[0x%x]", ret);
334 CAM_LOG_INFO("cb_id[%d] added", *cb_id);
340 int camera_device_manager_remove_device_list_changed_cb(camera_device_manager_h manager, int cb_id)
342 int ret = CAMERA_ERROR_NONE;
343 camera_device_manager *m = (camera_device_manager *)manager;
346 CAM_LOG_ERROR("NULL manager");
347 return CAMERA_ERROR_INVALID_PARAMETER;
350 CAM_LOG_INFO("enter - cb_id[%d]", cb_id);
352 ret = m->remove_device_list_changed_cb(cb_id);
353 if (ret != CAMERA_ERROR_NONE) {
354 CAM_LOG_ERROR("failed[0x%x]", ret);
358 CAM_LOG_INFO("cb_id[%d] removed", cb_id);
364 int camera_attr_set_flash_brightness(camera_h camera, int level)
366 int ret = CAMERA_ERROR_NONE;
367 camera_cli_s *pc = (camera_cli_s *)camera;
368 muse_camera_api_e api = MUSE_CAMERA_API_ATTR_SET_FLASH_BRIGHTNESS;
369 camera_msg_param param;
371 if (!pc || !pc->cb_info) {
372 CAM_LOG_ERROR("NULL handle");
373 return CAMERA_ERROR_INVALID_PARAMETER;
376 CAM_LOG_INFO("Enter");
378 CAMERA_MSG_PARAM_SET(param, INT, level);
380 _camera_msg_send_param1(api, pc->cb_info, &ret, ¶m, CAMERA_CB_TIMEOUT);
382 CAM_LOG_INFO("ret : 0x%x", ret);
388 int camera_attr_get_flash_brightness(camera_h camera, int *level)
390 int ret = CAMERA_ERROR_NONE;
391 camera_cli_s *pc = (camera_cli_s *)camera;
392 muse_camera_api_e api = MUSE_CAMERA_API_ATTR_GET_FLASH_BRIGHTNESS;
394 if (!pc || !pc->cb_info || !level) {
395 CAM_LOG_ERROR("NULL pointer %p %p", pc, level);
396 return CAMERA_ERROR_INVALID_PARAMETER;
399 CAM_LOG_INFO("Enter");
401 _camera_msg_send(api, NULL, pc->cb_info, &ret, CAMERA_CB_TIMEOUT);
403 if (ret == CAMERA_ERROR_NONE)
404 *level = pc->cb_info->get_int[MUSE_CAMERA_GET_INT_FLASH_BRIGHTNESS];
406 CAM_LOG_INFO("ret : 0x%x", ret);
412 int camera_attr_get_flash_brightness_range(camera_h camera, int *min, int *max)
414 int ret = CAMERA_ERROR_NONE;
415 camera_cli_s *pc = (camera_cli_s *)camera;
416 muse_camera_api_e api = MUSE_CAMERA_API_ATTR_GET_FLASH_BRIGHTNESS_RANGE;
418 if (!pc || !pc->cb_info || !min || !max) {
419 CAM_LOG_ERROR("NULL pointer %p %p %p", pc, min, max);
420 return CAMERA_ERROR_INVALID_PARAMETER;
423 CAM_LOG_INFO("Enter");
425 _camera_msg_send(api, NULL, pc->cb_info, &ret, CAMERA_CB_TIMEOUT);
427 if (ret == CAMERA_ERROR_NONE) {
428 *min = pc->cb_info->get_int_pair[MUSE_CAMERA_GET_INT_PAIR_FLASH_BRIGHTNESS_RANGE][0];
429 *max = pc->cb_info->get_int_pair[MUSE_CAMERA_GET_INT_PAIR_FLASH_BRIGHTNESS_RANGE][1];
432 CAM_LOG_INFO("ret : 0x%x", ret);
438 int camera_attr_set_focus_level(camera_h camera, int level)
440 int ret = CAMERA_ERROR_NONE;
441 camera_cli_s *pc = (camera_cli_s *)camera;
442 muse_camera_api_e api = MUSE_CAMERA_API_ATTR_SET_FOCUS_LEVEL;
443 camera_msg_param param;
445 if (!pc || !pc->cb_info) {
446 CAM_LOG_ERROR("NULL handle");
447 return CAMERA_ERROR_INVALID_PARAMETER;
450 CAM_LOG_INFO("Enter");
452 CAMERA_MSG_PARAM_SET(param, INT, level);
454 _camera_msg_send_param1(api, pc->cb_info, &ret, ¶m, CAMERA_CB_TIMEOUT);
456 CAM_LOG_INFO("ret : 0x%x", ret);
462 int camera_attr_get_focus_level(camera_h camera, int *level)
464 int ret = CAMERA_ERROR_NONE;
465 camera_cli_s *pc = (camera_cli_s *)camera;
466 muse_camera_api_e api = MUSE_CAMERA_API_ATTR_GET_FOCUS_LEVEL;
468 if (!pc || !pc->cb_info || !level) {
469 CAM_LOG_ERROR("NULL pointer %p %p", pc, level);
470 return CAMERA_ERROR_INVALID_PARAMETER;
473 CAM_LOG_INFO("Enter");
475 _camera_msg_send(api, NULL, pc->cb_info, &ret, CAMERA_CB_TIMEOUT);
477 if (ret == CAMERA_ERROR_NONE)
478 *level = pc->cb_info->get_int[MUSE_CAMERA_GET_INT_FOCUS_LEVEL];
480 CAM_LOG_INFO("ret : 0x%x", ret);
486 int camera_attr_get_focus_level_range(camera_h camera, int *min, int *max)
488 int ret = CAMERA_ERROR_NONE;
489 camera_cli_s *pc = (camera_cli_s *)camera;
490 muse_camera_api_e api = MUSE_CAMERA_API_ATTR_GET_FOCUS_LEVEL_RANGE;
492 if (!pc || !pc->cb_info || !min || !max) {
493 CAM_LOG_ERROR("NULL pointer %p %p %p", pc, min, max);
494 return CAMERA_ERROR_INVALID_PARAMETER;
497 CAM_LOG_INFO("Enter");
499 _camera_msg_send(api, NULL, pc->cb_info, &ret, CAMERA_CB_TIMEOUT);
501 if (ret == CAMERA_ERROR_NONE) {
502 *min = pc->cb_info->get_int_pair[MUSE_CAMERA_GET_INT_PAIR_FOCUS_LEVEL_RANGE][0];
503 *max = pc->cb_info->get_int_pair[MUSE_CAMERA_GET_INT_PAIR_FOCUS_LEVEL_RANGE][1];
506 CAM_LOG_INFO("ret : 0x%x", ret);
512 int camera_set_extra_preview_cb(camera_h camera, camera_extra_preview_cb callback, void *user_data)
514 int ret = CAMERA_ERROR_NONE;
515 camera_cli_s *pc = (camera_cli_s *)camera;
516 muse_camera_api_e api = MUSE_CAMERA_API_SET_EXTRA_PREVIEW_CB;
518 if (!pc || !pc->cb_info || !callback) {
519 CAM_LOG_ERROR("NULL pointer %p %p", pc, callback);
520 return CAMERA_ERROR_INVALID_PARAMETER;
523 CAM_LOG_INFO("Enter");
525 _camera_msg_send(api, NULL, pc->cb_info, &ret, CAMERA_CB_TIMEOUT);
527 if (ret == CAMERA_ERROR_NONE) {
528 g_mutex_lock(&pc->cb_info->user_cb_mutex[MUSE_CAMERA_EVENT_TYPE_EXTRA_PREVIEW]);
530 pc->cb_info->user_cb[MUSE_CAMERA_EVENT_TYPE_EXTRA_PREVIEW] = callback;
531 pc->cb_info->user_data[MUSE_CAMERA_EVENT_TYPE_EXTRA_PREVIEW] = user_data;
533 g_mutex_unlock(&pc->cb_info->user_cb_mutex[MUSE_CAMERA_EVENT_TYPE_EXTRA_PREVIEW]);
536 CAM_LOG_INFO("ret : 0x%x", ret);
542 int camera_unset_extra_preview_cb(camera_h camera)
544 int ret = CAMERA_ERROR_NONE;
545 camera_cli_s *pc = (camera_cli_s *)camera;
546 muse_camera_api_e api = MUSE_CAMERA_API_UNSET_EXTRA_PREVIEW_CB;
548 if (!pc || !pc->cb_info) {
549 CAM_LOG_ERROR("NULL handle");
550 return CAMERA_ERROR_INVALID_PARAMETER;
553 CAM_LOG_INFO("Enter");
555 _camera_msg_send(api, NULL, pc->cb_info, &ret, CAMERA_CB_TIMEOUT);
557 if (ret == CAMERA_ERROR_NONE) {
558 g_mutex_lock(&pc->cb_info->user_cb_mutex[MUSE_CAMERA_EVENT_TYPE_EXTRA_PREVIEW]);
560 pc->cb_info->user_cb[MUSE_CAMERA_EVENT_TYPE_EXTRA_PREVIEW] = NULL;
561 pc->cb_info->user_data[MUSE_CAMERA_EVENT_TYPE_EXTRA_PREVIEW] = NULL;
563 g_mutex_unlock(&pc->cb_info->user_cb_mutex[MUSE_CAMERA_EVENT_TYPE_EXTRA_PREVIEW]);
566 CAM_LOG_INFO("ret : 0x%x", ret);
572 int camera_set_extra_preview_stream_format(camera_h camera, int stream_id, camera_pixel_format_e pixel_format, int width, int height, int fps)
574 int ret = CAMERA_ERROR_NONE;
576 int stream_format[4] = {pixel_format, width, height, fps};
578 camera_cli_s *pc = (camera_cli_s *)camera;
579 muse_camera_api_e api = MUSE_CAMERA_API_SET_EXTRA_PREVIEW_STREAM_FORMAT;
581 if (!pc || !pc->cb_info) {
582 CAM_LOG_ERROR("NULL handle");
583 return CAMERA_ERROR_INVALID_PARAMETER;
586 CAM_LOG_INFO("Enter - stream_id[%d],[%d,%dx%d,%d]",
587 stream_id, pixel_format, width, height, fps);
589 msg = muse_core_msg_new(api,
590 MUSE_TYPE_INT, "stream_id", stream_id,
591 MUSE_TYPE_ARRAY, "stream_format", 4, stream_format,
594 CAM_LOG_ERROR("msg creation failed: api %d", api);
595 return CAMERA_ERROR_OUT_OF_MEMORY;
598 if (pc->cb_info->is_server_connected) {
599 _camera_update_api_waiting(pc->cb_info, api, 1);
601 g_mutex_lock(&pc->cb_info->fd_lock);
602 send_ret = muse_core_msg_send(pc->cb_info->fd, msg);
603 g_mutex_unlock(&pc->cb_info->fd_lock);
607 CAM_LOG_ERROR("message send failed");
608 ret = CAMERA_ERROR_INVALID_OPERATION;
610 ret = _camera_client_wait_for_cb_return(api, pc->cb_info, CAMERA_CB_TIMEOUT);
613 _camera_update_api_waiting(pc->cb_info, api, -1);
615 muse_core_msg_free(msg);
617 CAM_LOG_INFO("ret : 0x%x", ret);
623 int camera_get_extra_preview_stream_format(camera_h camera, int stream_id, camera_pixel_format_e *pixel_format, int *width, int *height, int *fps)
625 int ret = CAMERA_ERROR_NONE;
626 camera_cli_s *pc = (camera_cli_s *)camera;
627 camera_msg_param param;
628 muse_camera_api_e api = MUSE_CAMERA_API_GET_EXTRA_PREVIEW_STREAM_FORMAT;
630 if (!pc || !pc->cb_info || !pixel_format || !width || !height || !fps) {
631 CAM_LOG_ERROR("NULL pointer %p %p %p %p %p", pc, pixel_format, width, height, fps);
632 return CAMERA_ERROR_INVALID_PARAMETER;
635 CAM_LOG_INFO("Enter - stream_id[%d]", stream_id);
637 CAMERA_MSG_PARAM_SET(param, INT, stream_id);
639 _camera_msg_send_param1(api, pc->cb_info, &ret, ¶m, CAMERA_CB_TIMEOUT);
641 if (ret == CAMERA_ERROR_NONE) {
642 *pixel_format = (camera_pixel_format_e)pc->cb_info->get_extra_preview_stream_format[0];
643 *width = pc->cb_info->get_extra_preview_stream_format[1];
644 *height = pc->cb_info->get_extra_preview_stream_format[2];
645 *fps = pc->cb_info->get_extra_preview_stream_format[3];
648 CAM_LOG_INFO("ret : 0x%x", ret);